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