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