]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/Menus.h
* Menu:
[lyx.git] / src / frontends / qt4 / Menus.h
1 // -*- C++ -*-
2 /**
3  * \file Menus.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 John Levon
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #ifndef MENUS_H
14 #define MENUS_H
15
16 #include "FuncStatus.h"
17 #include "FuncRequest.h"
18
19 #include "support/strfwd.h"
20
21 #include <QObject>
22 #include <QHash>
23
24 #include <boost/shared_ptr.hpp>
25
26 #include <vector>
27
28 class QMenu;
29
30 namespace lyx {
31
32 class Lexer;
33 class Buffer;
34 class Toc;
35
36 namespace frontend {
37
38 class Menu;
39 class GuiView;
40 class GuiPopupMenu;
41 class GuiView;
42
43 ///
44 class MenuItem {
45 public:
46         /// The type of elements that can be in a menu
47         enum Kind {
48                 ///
49                 Command,
50                 ///
51                 Submenu,
52                 ///
53                 Separator,
54                 /** This is the list of last opened file,
55                     typically for the File menu. */
56                 Lastfiles,
57                 /** This is the list of opened Documents,
58                     typically for the Documents menu. */
59                 Documents,
60                 /** This is the bookmarks */
61                 Bookmarks,
62                 ///
63                 Toc,
64                 /** This is a list of viewable formats
65                     typically for the File->View menu. */
66                 ViewFormats,
67                 /** This is a list of updatable formats
68                     typically for the File->Update menu. */
69                 UpdateFormats,
70                 /** This is a list of exportable formats
71                     typically for the File->Export menu. */
72                 ExportFormats,
73                 /** This is a list of importable formats
74                     typically for the File->Export menu. */
75                 ImportFormats,
76                 /** This is the list of elements available
77                  * for insertion into document. */
78                 CharStyles,
79                 /** This is the list of user-configurable
80                 insets to insert into document */
81                 Custom,
82                 /** This is the list of XML elements to
83                 insert into the document */
84                 Elements,
85                 /** This is the list of floats that we can
86                     insert a list for. */
87                 FloatListInsert,
88                 /** This is the list of floats that we can
89                     insert. */
90                 FloatInsert,
91                 /** This is the list of selections that can
92                     be pasted. */
93                 PasteRecent,
94                 /** toolbars */
95                 Toolbars,
96                 /** Available branches in document */
97                 Branches
98         };
99
100         explicit MenuItem(Kind kind);
101
102         MenuItem(Kind kind,
103                  QString const & label,
104                  QString const & submenu = QString(),
105                  bool optional = false);
106
107         MenuItem(Kind kind,
108                  QString const & label,
109                  FuncRequest const & func,
110                  bool optional = false);
111
112         /// This one is just to please boost::shared_ptr<>
113         ~MenuItem();
114         /// The label of a given menuitem
115         QString label() const;
116         /// The keyboard shortcut (usually underlined in the entry)
117         QString shortcut() const;
118         /// The complete label, with label and shortcut separated by a '|'
119         QString fulllabel() const { return label_;}
120         /// The kind of entry
121         Kind kind() const { return kind_; }
122         /// the action (if relevant)
123         FuncRequest const & func() const { return func_; }
124         /// returns true if the entry should be ommited when disabled
125         bool optional() const { return optional_; }
126         /// returns the status of the lfun associated with this entry
127         FuncStatus const & status() const { return status_; }
128         /// returns the status of the lfun associated with this entry
129         FuncStatus & status() { return status_; }
130         /// returns the status of the lfun associated with this entry
131         void status(FuncStatus const & status) { status_ = status; }
132         ///returns the binding associated to this action.
133         QString binding() const;
134         /// the description of the  submenu (if relevant)
135         QString const & submenuname() const { return submenuname_; }
136         /// set the description of the  submenu
137         void submenuname(QString const & name) { submenuname_ = name; }
138         ///
139         Menu * submenu() const { return submenu_.get(); }
140         ///
141         void setSubmenu(Menu * menu);
142
143 private:
144         ///
145         Kind kind_;
146         ///
147         QString label_;
148         ///
149         FuncRequest func_;
150         ///
151         QString submenuname_;
152         ///
153         bool optional_;
154         ///
155         FuncStatus status_;
156         ///
157         boost::shared_ptr<Menu> submenu_;
158 };
159
160
161 ///
162 class Menu {
163 public:
164         ///
165         typedef std::vector<MenuItem> ItemList;
166         ///
167         typedef ItemList::const_iterator const_iterator;
168
169         ///
170         explicit Menu(QString const & name = QString()) : name_(name) {}
171
172         ///
173         void read(Lexer &);
174         ///
175         QString const & name() const { return name_; }
176         ///
177         bool empty() const { return items_.empty(); }
178         /// Clear the menu content.
179         void clear() { items_.clear(); }
180         ///
181         size_t size() const { return items_.size(); }
182         ///
183         MenuItem const & operator[](size_t) const;
184         ///
185         const_iterator begin() const { return items_.begin(); }
186         ///
187         const_iterator end() const { return items_.end(); }
188         
189         // search for func in this menu iteratively, and put menu
190         // names in a stack.
191         bool searchMenu(FuncRequest const & func, std::vector<docstring> & names)
192                 const;
193
194 private:
195         friend class Menus;
196         ///
197         bool hasFunc(FuncRequest const &) const;
198         /// Add the menu item unconditionally
199         void add(MenuItem const & item) { items_.push_back(item); }
200         /// Checks the associated FuncRequest status before adding the
201         /// menu item.
202         void addWithStatusCheck(MenuItem const &);
203         // Check whether the menu shortcuts are unique
204         void checkShortcuts() const;
205         ///
206         void expandLastfiles();
207         void expandDocuments();
208         void expandBookmarks();
209         void expandFormats(MenuItem::Kind kind, Buffer const * buf);
210         void expandFloatListInsert(Buffer const * buf);
211         void expandFloatInsert(Buffer const * buf);
212         void expandFlexInsert(Buffer const * buf, std::string s);
213         void expandToc2(Toc const & toc_list, size_t from, size_t to, int depth);
214         void expandToc(Buffer const * buf);
215         void expandPasteRecent();
216         void expandToolbars();
217         void expandBranches(Buffer const * buf);
218
219         ItemList items_;
220         ///
221         QString name_;
222 };
223
224
225 class Menus
226 {
227 public:
228         ///
229         typedef std::vector<Menu> MenuList;
230         ///
231         typedef MenuList::const_iterator const_iterator;
232         ///
233         typedef MenuList::iterator iterator;
234
235
236         Menus() {}
237
238         ///
239         void fillMenuBar(GuiView * view);
240
241         /// \return a top-level submenu given its name.
242         QMenu * menu(QString const & name);
243
244         /// update the state of the menuitems - not needed
245         void updateView();
246         ///
247         void read(Lexer &);
248         ///
249         void add(Menu const &);
250         ///
251         bool hasMenu(QString const &) const;
252         ///
253         Menu & getMenu(QString const &);
254         ///
255         Menu const & getMenu(QString const &) const;
256         ///
257         Menu const & getMenubar() const;
258         ///
259         bool empty() const { return menulist_.empty(); }
260         /** This defines a menu whose entries list the FuncRequests
261             that will be removed by expand() in other menus. This is
262             used by the Qt/Mac code
263         */
264         void setSpecialMenu(Menu const & menu) { specialmenu_ = menu; }
265         ///
266         Menu const & specialMenu() { return specialmenu_; }
267
268         /// Expands some special entries of the menu
269         /** The entries with the following kind are expanded to a
270             sequence of Command MenuItems: Lastfiles, Documents,
271             ViewFormats, ExportFormats, UpdateFormats, Branches
272         */
273         void expand(Menu const & frommenu, Menu & tomenu,
274                     Buffer const *) const;
275         ///
276         const_iterator begin() const { return menulist_.begin(); }
277         ///
278         iterator begin() { return menulist_.begin(); }
279         ///
280         const_iterator end() const { return menulist_.end(); }
281         ///
282         iterator end() { return menulist_.end(); }
283
284 private:
285         ///
286         MenuList menulist_;
287         ///
288         Menu menubar_;
289         ///
290         Menu specialmenu_;
291
292 private:
293         /// Initialize specific MACOS X menubar
294         void macxMenuBarInit(GuiView * view);
295
296         typedef QHash<QString, GuiPopupMenu *> NameMap;
297
298         /// name to menu for \c menu() method.
299         NameMap name_map_;
300 };
301
302 } // namespace frontend
303 } // namespace lyx
304
305 #endif // MENUS_H