]> git.lyx.org Git - lyx.git/blob - src/MenuBackend.h
* Painter.h:
[lyx.git] / src / MenuBackend.h
1 // -*- C++ -*-
2 /**
3  * \file MenuBackend.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Lars Gullik Bjønnes
8  * \author Jean-Marc Lasgouttes
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #ifndef MENUBACKEND_H
14 #define MENUBACKEND_H
15
16 #include "FuncStatus.h"
17 #include "funcrequest.h"
18
19 #include <boost/shared_ptr.hpp>
20
21 #include <vector>
22
23
24 namespace lyx {
25
26 class LyXLex;
27 class Buffer;
28 class Menu;
29
30 ///
31 class MenuItem {
32 public:
33         /// The type of elements that can be in a menu
34         enum Kind {
35                 ///
36                 Command,
37                 ///
38                 Submenu,
39                 ///
40                 Separator,
41                 /** This is the list of last opened file,
42                     typically for the File menu. */
43                 Lastfiles,
44                 /** This is the list of opened Documents,
45                     typically for the Documents menu. */
46                 Documents,
47                 ///
48                 Toc,
49                 /** This is a list of viewable formats
50                     typically for the File->View menu. */
51                 ViewFormats,
52                 /** This is a list of updatable formats
53                     typically for the File->Update menu. */
54                 UpdateFormats,
55                 /** This is a list of exportable formats
56                     typically for the File->Export menu. */
57                 ExportFormats,
58                 /** This is a list of importable formats
59                     typically for the File->Export menu. */
60                 ImportFormats,
61                 /** This is the list of elements available
62                  * for insertion into document. */
63                 CharStyles,
64                 /** This is the list of floats that we can
65                     insert a list for. */
66                 FloatListInsert,
67                 /** This is the list of floats that we can
68                     insert. */
69                 FloatInsert,
70                 /** This is the list of selections that can
71                     be pasted. */
72                 PasteRecent,
73                 /** Available branches in document */
74                 Branches
75         };
76
77         explicit MenuItem(Kind kind);
78
79         MenuItem(Kind kind,
80                  docstring const & label,
81                  docstring const & submenu = docstring(),
82                  bool optional = false);
83
84         MenuItem(Kind kind,
85                  docstring const & label,
86                  FuncRequest const & func,
87                  bool optional = false);
88
89         /// This one is just to please boost::shared_ptr<>
90         ~MenuItem();
91         /// The label of a given menuitem
92         docstring const label() const;
93         /// The keyboard shortcut (usually underlined in the entry)
94         docstring const shortcut() const;
95         /// The complete label, with label and shortcut separated by a '|'
96         docstring const fulllabel() const { return label_;}
97         /// The kind of entry
98         Kind kind() const { return kind_; }
99         /// the action (if relevant)
100         FuncRequest const & func() const { return func_; }
101         /// returns true if the entry should be ommited when disabled
102         bool optional() const { return optional_; }
103         /// returns the status of the lfun associated with this entry
104         FuncStatus const & status() const { return status_; }
105         /// returns the status of the lfun associated with this entry
106         FuncStatus & status() { return status_; }
107         /// returns the status of the lfun associated with this entry
108         void status(FuncStatus const & status) { status_ = status; }
109         /// returns the binding associated to this action
110         docstring const binding() const;
111         /// the description of the  submenu (if relevant)
112         docstring const & submenuname() const { return submenuname_; }
113         /// set the description of the  submenu
114         void submenuname(docstring const & name) { submenuname_ = name; }
115         ///
116         Menu * submenu() const { return submenu_.get(); }
117         ///
118         void submenu(Menu * menu);
119
120 private:
121         //friend class MenuBackend;
122         ///
123         Kind kind_;
124         ///
125         docstring label_;
126         ///
127         FuncRequest func_;
128         ///
129         docstring submenuname_;
130         ///
131         bool optional_;
132         ///
133         FuncStatus status_;
134         ///
135         boost::shared_ptr<Menu> submenu_;
136 };
137
138
139 ///
140 class Menu {
141 public:
142         ///
143         typedef std::vector<MenuItem> ItemList;
144         ///
145         typedef ItemList::const_iterator const_iterator;
146         ///
147         typedef ItemList::size_type size_type;
148         ///
149         explicit Menu(docstring const & name = docstring())
150                 : name_(name) {}
151         /// Add the menu item unconditionally
152         Menu & add(MenuItem const &);
153         /// Checks the associated FuncRequest status before adding the
154         /// menu item.
155         Menu & addWithStatusCheck(MenuItem const &);
156         ///
157         Menu & read(LyXLex &);
158         ///
159         docstring const & name() const { return name_; }
160         ///
161         bool empty() const { return items_.empty(); }
162         /// Clear the menu content.
163         void clear() { items_.clear(); }
164         ///
165         ItemList::size_type size() const { return items_.size(); }
166         ///
167         MenuItem const & operator[](size_type) const;
168         ///
169         bool hasFunc(FuncRequest const &) const;
170         ///
171         const_iterator begin() const {
172                 return items_.begin();
173         }
174         ///
175         const_iterator end() const {
176                 return items_.end();
177         }
178
179         // Check whether the menu shortcuts are unique
180         void checkShortcuts() const;
181
182 private:
183         friend class MenuBackend;
184         ///
185         ItemList items_;
186         ///
187         docstring name_;
188 };
189
190
191 ///
192 class MenuBackend {
193 public:
194         ///
195         typedef std::vector<Menu> MenuList;
196         ///
197         typedef MenuList::const_iterator const_iterator;
198         ///
199         typedef MenuList::iterator iterator;
200         ///
201         MenuBackend() : specialmenu_(0) {}
202         ///
203         void read(LyXLex &);
204         ///
205         void add(Menu const &);
206         ///
207         bool hasMenu(docstring const &) const;
208         ///
209         Menu & getMenu(docstring const &);
210         ///
211         Menu const & getMenu(docstring const &) const;
212         ///
213         Menu const & getMenubar() const;
214         ///
215         bool empty() const { return menulist_.empty(); }
216         /** This defines a menu whose entries list the FuncRequests
217             will be removed by expand() in other menus. This is used by
218             the Qt/Mac code
219         */
220         void specialMenu(docstring const &);
221         /// Expands some special entries of the menu
222         /** The entries with the following kind are expanded to a
223             sequence of Command MenuItems: Lastfiles, Documents,
224             ViewFormats, ExportFormats, UpdateFormats, Branches
225         */
226         void expand(Menu const & frommenu, Menu & tomenu,
227                     Buffer const *) const;
228         ///
229         const_iterator begin() const {
230                 return menulist_.begin();
231         }
232         ///
233         iterator begin() {
234                 return menulist_.begin();
235         }
236         ///
237         const_iterator end() const {
238                 return menulist_.end();
239         }
240         ///
241         iterator end() {
242                 return menulist_.end();
243         }
244 private:
245         ///
246         MenuList menulist_;
247         ///
248         Menu menubar_;
249         ///
250         Menu * specialmenu_;
251 };
252
253 ///
254 extern MenuBackend menubackend;
255
256
257 } // namespace lyx
258
259 #endif /* MENUBACKEND_H */