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