]> git.lyx.org Git - lyx.git/blob - src/MenuBackend.h
b7a3abeb7aba4d62754e42f5ad479f40015663c6
[lyx.git] / src / MenuBackend.h
1 /* This file is part of              -*- C++ -*-
2 * ======================================================
3 *
4 *           LyX, The Document Processor
5 *
6 *           Copyright 1995 Matthias Ettrich
7 *           Copyright 1995-2001 The LyX Team.
8 *
9 *           This file is Copyright 1999
10 *           Jean-Marc Lasgouttes
11 *
12 *======================================================*/
13
14 #ifndef MENUBACKEND_H
15 #define MENUBACKEND_H
16
17 #ifdef __GNUG__
18 #pragma interface
19 #endif
20
21 #include "LString.h"
22 #include <vector>
23 #include <boost/shared_ptr.hpp>
24
25 class LyXLex;
26 class Buffer;
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         };
67         /// Create a Command type MenuItem
68         MenuItem(Kind kind,
69                  string const & label = string(),
70                  string const & command = string(),
71                  bool optional = false);
72         MenuItem(Kind kind,
73                  string const & label,
74                  int action,
75                  bool optional = false)
76                 : kind_(kind), label_(label),
77                   action_(action), submenuname_(), optional_(optional) {}
78
79         /// This one is just to please boost::shared_ptr<>
80         ~MenuItem();
81         /// The label of a given menuitem
82         string const label() const;
83         /// The keyboard shortcut (usually underlined in the entry)
84         string const shortcut() const;
85         /// The complete label, with label and shortcut separated by a '|'
86         string const fulllabel() const { return label_;}
87         /// The kind of entry
88         Kind kind() const { return kind_; }
89         /// the action (if relevant)
90         int action() const { return action_; }
91         /// returns true if the entry should be ommited when disabled
92         bool optional() const { return optional_; }
93         /// the description of the  submenu (if relevant)
94         string const & submenuname() const { return submenuname_; }
95         /// the description of the  submenu (if relevant)
96         void submenuname(string const & name) { submenuname_ = name; }
97         ///
98         Menu * submenu() const { return submenu_.get(); }
99         ///
100         void submenu(Menu * menu) { submenu_.reset(menu); }
101
102 private:
103         //friend class MenuBackend;
104         ///
105         Kind kind_;
106         ///
107         string label_;
108         ///
109         int action_;
110         ///
111         string submenuname_;
112         ///
113         bool optional_;
114         ///
115         boost::shared_ptr<Menu> submenu_;
116 };
117
118
119 ///
120 class Menu {
121 public:
122         ///
123         typedef std::vector<MenuItem> ItemList;
124         ///
125         typedef ItemList::const_iterator const_iterator;
126         ///
127         typedef ItemList::size_type size_type;
128         ///
129         explicit Menu(string const & name = string())
130                 : name_(name) {}
131         ///
132         Menu & add(MenuItem const &);
133         ///
134         Menu & read(LyXLex &);
135         ///
136         string const & name() const { return name_; }
137         ///
138         bool empty() const { return items_.empty(); }
139         ///
140         ItemList::size_type size() const { return items_.size(); }
141         ///
142         bool hasSubmenu(string const &) const;
143         ///
144         const_iterator begin() const {
145                 return items_.begin();
146         }
147         ///
148         const_iterator end() const {
149                 return items_.end();
150         }
151
152         // Check whether the menu shortcuts are unique
153         void checkShortcuts() const;
154
155 private:
156         ///
157         ItemList items_;
158         ///
159         string name_;
160 };
161
162
163 ///
164 class MenuBackend {
165 public:
166         ///
167         typedef std::vector<Menu> MenuList;
168         ///
169         typedef MenuList::const_iterator const_iterator;
170         ///
171         void read(LyXLex &);
172         /// Set default values for menu structure.
173         void defaults();
174         ///
175         void add(Menu const &);
176         ///
177         bool hasMenu(string const &) const;
178         ///
179         Menu & getMenu (string const &);
180         ///
181         Menu const & getMenu (string const &) const;
182         ///
183         Menu const & getMenubar() const;
184         ///
185         bool empty() const { return menulist_.empty(); }
186         /// Expands some special entries of the menu
187         /** The entries with the following kind are expanded to a
188             sequence of Command MenuItems: Lastfiles, Documents,
189             ViewFormats, ExportFormats, UpdateFormats
190         */
191         void expand(Menu const & frommenu, Menu & tomenu,
192                     Buffer const *) const;
193         ///
194         const_iterator begin() const {
195                 return menulist_.begin();
196         }
197         ///
198         const_iterator end() const {
199                 return menulist_.end();
200         }
201 private:
202         ///
203         MenuList menulist_;
204         ///
205         Menu menubar_;
206 };
207
208 ///
209 extern MenuBackend menubackend;
210
211 #endif /* MENUBACKEND_H */