]> git.lyx.org Git - features.git/blob - src/MenuBackend.h
Add code to check shortcuts in menu files
[features.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-2000 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 "support/lstrings.h"
23 #include <vector>
24
25 class LyXLex;
26 class Buffer;
27
28 ///
29 class MenuItem {
30 public:
31         /// The type of elements that can be in a menu
32         enum Kind {
33                 ///
34                 Command,
35                 ///
36                 Submenu,
37                 ///
38                 Separator,
39                 /** This is the list of last opened file,
40                     typically for the File menu. */
41                 Lastfiles,
42                 /** This is the list of opened Documents,
43                     typically for the Documents menu. */
44                 Documents,
45                 ///
46                 Toc,
47                 ///
48                 References,
49                 /** This is a list of viewable formats
50                     typically for the File->View menu. */
51                 ViewFormats,
52                 /** This is a list of updatable formats
53                     typically for the File->Update menu. */
54                 UpdateFormats,
55                 /** This is a list of exportable formats
56                     typically for the File->Export menu. */
57                 ExportFormats
58         };
59         /// Create a Command type MenuItem
60         MenuItem(Kind kind, 
61                  string const & label = string(), 
62                  string const & command = string(), 
63                  bool optional = false);
64         MenuItem(Kind kind,
65                  string const & label, 
66                  int action, 
67                  bool optional = false)
68                 : kind_(kind), label_(label),
69                   action_(action), submenu_(), optional_(optional) {}
70  
71         /// The label of a given menuitem
72         string const label() const { return token(label_, '|', 0); }
73         /// The keyboard shortcut (usually underlined in the entry)
74         string const shortcut() const { return token(label_, '|', 1); }
75         /// The complete label, with label and shortcut separated by a '|'
76         string const fulllabel() const { return label_;}
77         /// The kind of entry
78         Kind kind() const { return kind_; } 
79         /// the action (if relevant)
80         int action() const { return action_; }
81         /// the description of the  submenu (if relevant)
82         string const & submenu() const { return submenu_; }
83         /// returns true if the entry should be ommited when disabled
84         bool optional() const { return optional_; }
85 private:
86         ///
87         Kind kind_;
88         ///
89         string label_;
90         ///
91         int action_;
92         ///
93         string submenu_;
94         ///
95         bool optional_;
96 };
97
98
99 ///
100 class Menu {
101 public:
102         ///
103         typedef std::vector<MenuItem> ItemList;
104         ///
105         typedef ItemList::const_iterator const_iterator;
106         ///
107         explicit Menu(string const & name = string(), bool mb = false) 
108                 : menubar_(mb), name_(name) {}
109         ///
110         Menu & add(MenuItem const &);
111         ///
112         Menu & read(LyXLex &);
113         /// Expands some special entries of the menu
114         /** The entries with the following kind are expanded to a
115             sequence of Command MenuItems: Lastfiles, Documents,
116             ViewFormats, ExportFormats, UpdateFormats
117         */
118         void expand(Menu & tomenu, Buffer *) const;
119         /// 
120         bool menubar() const { return menubar_; }
121         /// 
122         string const & name() const { return name_; }
123         ///
124         bool empty() const { return items_.empty(); }
125         ///
126         ItemList::size_type size() const { return items_.size(); }
127         ///
128         const_iterator begin() const {
129                 return items_.begin();
130         }
131         ///
132         const_iterator end() const {
133                 return items_.end();
134         }
135
136         // Check whether the menu shortcuts are unique
137         void checkShortcuts() const;
138         
139 private:
140         ///
141         ItemList items_;
142         ///
143         bool menubar_;
144         ///
145         string name_;
146 };
147
148
149 ///
150 class MenuBackend {
151 public:
152         ///
153         typedef std::vector<Menu> MenuList;
154         ///
155         typedef MenuList::const_iterator const_iterator;
156         ///
157         void read(LyXLex &);
158         /// Set default values for menu structure.
159         void defaults();
160         ///
161         void add(Menu const &);
162         ///
163         bool hasMenu (string const &) const;
164         ///
165         Menu & getMenu (string const &);
166         ///
167         Menu const & getMenu (string const &) const;
168         //
169         bool empty() const { return menulist_.empty(); }
170         ///
171         const_iterator begin() const {
172                 return menulist_.begin();
173         }
174         ///
175         const_iterator end() const {
176                 return menulist_.end();
177         }
178 private:
179         ///
180         MenuList menulist_;
181 };
182
183 ///
184 extern MenuBackend menubackend;
185
186 #endif /* MENUBACKEND_H */