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