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