]> git.lyx.org Git - lyx.git/blob - src/MenuBackend.h
fix #1411
[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 "support/std_string.h"
20
21 #include <boost/shared_ptr.hpp>
22
23 #include <vector>
24
25 class LyXLex;
26 class LyXView;
27 class Menu;
28
29 ///
30 class MenuItem {
31 public:
32         /// The type of elements that can be in a menu
33         enum Kind {
34                 ///
35                 Command,
36                 ///
37                 Submenu,
38                 ///
39                 Separator,
40                 /** This is the list of last opened file,
41                     typically for the File menu. */
42                 Lastfiles,
43                 /** This is the list of opened Documents,
44                     typically for the Documents menu. */
45                 Documents,
46                 ///
47                 Toc,
48                 /** This is a list of viewable formats
49                     typically for the File->View menu. */
50                 ViewFormats,
51                 /** This is a list of updatable formats
52                     typically for the File->Update menu. */
53                 UpdateFormats,
54                 /** This is a list of exportable formats
55                     typically for the File->Export menu. */
56                 ExportFormats,
57                 /** This is a list of importable formats
58                     typically for the File->Export menu. */
59                 ImportFormats,
60                 /** This is the list of floats that we can
61                     insert a list for. */
62                 FloatListInsert,
63                 /** This is the list of floats that we can
64                     insert. */
65                 FloatInsert,
66                 /** This is the list of selections that can
67                     be pasted. */
68                 PasteRecent,
69                 /** Available branches in document */
70                 Branches
71         };
72
73         explicit MenuItem(Kind kind);
74
75         MenuItem(Kind kind,
76                  string const & label,
77                  string const & command = string(),
78                  bool optional = false);
79
80         MenuItem(Kind kind,
81                  string const & label,
82                  FuncRequest const & func,
83                  bool optional = false);
84
85         /// This one is just to please boost::shared_ptr<>
86         ~MenuItem();
87         /// The label of a given menuitem
88         string const label() const;
89         /// The keyboard shortcut (usually underlined in the entry)
90         string const shortcut() const;
91         /// The complete label, with label and shortcut separated by a '|'
92         string const fulllabel() const { return label_;}
93         /// The kind of entry
94         Kind kind() const { return kind_; }
95         /// the action (if relevant)
96         FuncRequest const & func() const { return func_; }
97         /// returns true if the entry should be ommited when disabled
98         bool optional() const { return optional_; }
99         /// returns the status of the lfun associated with this entry
100         FuncStatus const & status() const { return status_; }
101         /// returns the status of the lfun associated with this entry
102         FuncStatus & status() { return status_; }
103         /// returns the status of the lfun associated with this entry
104         void status(FuncStatus const & status) { status_ = status; }
105         /// returns the binding associated to this action
106         string const binding() const;
107         /// the description of the  submenu (if relevant)
108         string const & submenuname() const { return submenuname_; }
109         /// set the description of the  submenu
110         void submenuname(string const & name) { submenuname_ = name; }
111         ///
112         Menu * submenu() const { return submenu_.get(); }
113         ///
114         void submenu(Menu * menu);
115
116 private:
117         //friend class MenuBackend;
118         ///
119         Kind kind_;
120         ///
121         string label_;
122         ///
123         FuncRequest func_;
124         ///
125         string submenuname_;
126         ///
127         bool optional_;
128         ///
129         FuncStatus status_;
130         ///
131         boost::shared_ptr<Menu> submenu_;
132 };
133
134
135 ///
136 class Menu {
137 public:
138         ///
139         typedef std::vector<MenuItem> ItemList;
140         ///
141         typedef ItemList::const_iterator const_iterator;
142         ///
143         typedef ItemList::size_type size_type;
144         ///
145         explicit Menu(string const & name = string())
146                 : name_(name) {}
147         ///
148         Menu & add(MenuItem const &, LyXView const * view = 0);
149         ///
150         Menu & read(LyXLex &);
151         ///
152         string const & name() const { return name_; }
153         ///
154         bool empty() const { return items_.empty(); }
155         ///
156         ItemList::size_type size() const { return items_.size(); }
157         ///
158         bool hasSubmenu(string const &) const;
159         ///
160         const_iterator begin() const {
161                 return items_.begin();
162         }
163         ///
164         const_iterator end() const {
165                 return items_.end();
166         }
167
168         // Check whether the menu shortcuts are unique
169         void checkShortcuts() const;
170
171 private:
172         friend class MenuBackend;
173         ///
174         ItemList items_;
175         ///
176         string name_;
177 };
178
179
180 ///
181 class MenuBackend {
182 public:
183         ///
184         typedef std::vector<Menu> MenuList;
185         ///
186         typedef MenuList::const_iterator const_iterator;
187         ///
188         void read(LyXLex &);
189         ///
190         void add(Menu const &);
191         ///
192         bool hasMenu(string const &) const;
193         ///
194         Menu & getMenu(string const &);
195         ///
196         Menu const & getMenu(string const &) const;
197         ///
198         Menu const & getMenubar() const;
199         ///
200         bool empty() const { return menulist_.empty(); }
201         /// Expands some special entries of the menu
202         /** The entries with the following kind are expanded to a
203             sequence of Command MenuItems: Lastfiles, Documents,
204             ViewFormats, ExportFormats, UpdateFormats, Branches
205         */
206         void expand(Menu const & frommenu, Menu & tomenu,
207                     LyXView const *) const;
208         ///
209         const_iterator begin() const {
210                 return menulist_.begin();
211         }
212         ///
213         const_iterator end() const {
214                 return menulist_.end();
215         }
216 private:
217         ///
218         MenuList menulist_;
219         ///
220         Menu menubar_;
221 };
222
223 ///
224 extern MenuBackend menubackend;
225
226 #endif /* MENUBACKEND_H */