Milo  0.1
Milo:EquationManipulationProgram
menu.h
Go to the documentation of this file.
1 /* Copyright (C) 2018 - James Terman
2  *
3  * milo is free software; you can redistribute it and/or
4  * modify it under the terms of the GNU Lesser General Public
5  * License as published by the Free Software Foundation; either
6  * version 2.1 of the License, or (at your option) any later version.
7  *
8  * milo is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  * Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public
14  * License along with this library; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16  */
17 
23 #include <ncursesw/ncurses.h>
24 #include <locale.h>
25 #include <unordered_map>
26 #include <memory>
27 #include <utf8.h>
28 #include "ui.h"
29 
30 
35 {
36 public:
39  MenuBaseItem(const StringMap& tags) :
40  m_name(tags.at("name")),
41  m_type(tags.at("type")),
42  m_width(-1) {}
43  virtual ~MenuBaseItem() {}
45 
50  const std::string& getName() { return m_name; }
51 
56  const std::string& getType() { return m_type; }
57 
62  int getWidth() { if (m_width == -1) m_width = calc_width(); return m_width; }
63 
69  virtual void select()=0;
70 
75  virtual const std::string& getTitle()=0;
76 
81  virtual bool getActive()=0;
82 
90  virtual void draw(bool fHighlight, const int y0, const int x0, const int w) = 0;
92 
93 protected:
102  void draw_menu(bool fHighlight, const int y0, const int x0, const int w, const std::string& key = std::string());
103 
104 private:
107  virtual int calc_width()=0;
108 
109  const std::string m_name;
110  const std::string m_type;
111  int m_width;
112 };
113 
114 using MenuBasePtr = std::shared_ptr<MenuBaseItem>;
115 class MenuBar;
116 
120 class Menu : public MenuBaseItem
121 {
122 public:
125  Menu(const StringMap& tags) :
126  MenuBaseItem(tags),
127  m_active(tags.at("active") == "true"),
128  m_title(tags.at("title")),
129  m_parent(nullptr) {}
130  ~Menu() {}
132 
139  void drawInBar(int x, bool fHighlight = false);
140 
146  bool handleKey(int code);
147 
151  void select();
152 
157  const std::string& getTitle() { return m_title; }
158 
163  bool getActive() { return m_active; }
164 
167  int calc_width();
168 
176  void draw(bool fHighlight, const int y0, const int x0, const int w) {
177  draw_menu(fHighlight, y0, x0, w);
178  }
180 
185  void addItem(MenuBaseItem* m) { m_items.push_back(MenuBasePtr(m)); }
186 
191  Menu* getParent() { return m_parent; }
192 
197  void highlight_next(int step);
198 
202  void refresh_window();
203 
208  bool isCurrent();
209 
210  friend class MenuBar;
211 private:
212  bool m_active;
213  const std::string m_title;
214  std::vector<MenuBasePtr> m_items;
216  int m_xbar;
217  int m_wbar;
218  std::vector<MenuBasePtr>::iterator m_highlight;
219  int m_x0;
220  int m_y0;
221  int m_width;
222  int m_height;
223 
224  static Menu* m_current;
225 };
226 
227 using MenuPtr = std::shared_ptr<Menu>;
228 
232 class MenuItem : public MenuBaseItem
233 {
234 public:
237  MenuItem(const StringMap& tags) :
238  MenuBaseItem(tags),
239  m_active(tags.at("active") == "true"),
240  m_title(tags.at("title")),
241  m_action(tags.at("action")),
242  m_key((tags.at("key") == "NONE") ? std::string() : tags.at("key")) {}
243 
244  ~MenuItem() {}
246 
252  void select() { UI::MiloApp::getGlobal().doMenu(m_action); }
253 
258  const std::string& getTitle() { return m_title; }
259 
264  bool getActive() { return m_active; }
265 
268  int calc_width();
269 
277  void draw(bool fHighlight, const int y0, const int x0, const int w) {
278  draw_menu(fHighlight, y0, x0, w, m_key);
279  }
281 
282 private:
283  bool m_active;
284  const std::string m_title;
285  const std::string m_action;
286  const std::string m_key;
287 };
288 
292 class MenuLine : public MenuBaseItem
293 {
294 public:
297  MenuLine() : MenuBaseItem({ {"type", "line"}, {"name", "line"} }) {}
298  ~MenuLine() {}
300 
310  void draw(bool fHighlight, const int y0, const int x0, const int w);
311 
315  void select() {}
316 
321  const std::string& getTitle() { return m_line_title; }
322 
327  bool getActive() { return false; }
328 
331  int calc_width() { return -1; }
333 private:
334  static const std::string m_line_title;
335 };
336 
341 class MenuBar : public UI::MenuXML
342 {
343 public:
348  MenuBar(const std::string& xml);
349 
354  void add(Menu* menu);
355 
359  void draw();
360 
366  bool select(int mouse_x = 1);
367 
372  bool active() { return m_root != 0; }
373 
379  bool handleKey(int code);
380 
386  bool doMenuKey(const UI::KeyEvent& key);
387 
394  void define_menu(const StringMap& attributes);
395 
400  void define_menu_end(const std::string& name);
401 
406  void define_menu_item(const StringMap& attributes);
407 
411  void define_menu_line();
413 
414 private:
416  int m_level;
417  std::vector<MenuPtr> m_menus;
418  std::stack<Menu*> m_menu_heap;
419 
423  std::unordered_map<UI::KeyEvent, std::string> key_menu_map;
424 };
const std::string & getTitle()
Definition: menu.h:321
const std::string m_key
Key binding of menu item.
Definition: menu.h:286
static MiloApp & getGlobal()
Definition: ui.h:809
std::vector< MenuBasePtr >::iterator m_highlight
Index of item highlighted.
Definition: menu.h:218
const std::string m_type
menu type
Definition: menu.h:110
int m_width
Width of where menu.
Definition: menu.h:221
bool getActive()
Definition: menu.h:163
void draw(bool fHighlight, const int y0, const int x0, const int w)
Definition: menu.h:277
Definition: menu.h:292
virtual void select()=0
void addItem(MenuBaseItem *m)
Definition: menu.h:185
virtual void draw(bool fHighlight, const int y0, const int x0, const int w)=0
int m_wbar
Width on menubar if needed.
Definition: menu.h:217
const std::string & getName()
Definition: menu.h:50
virtual const std::string & getTitle()=0
virtual bool getActive()=0
const std::string m_title
Title of menu.
Definition: menu.h:213
Definition: menu.h:34
Menu * getParent()
Definition: menu.h:191
void select()
Definition: menu.h:252
const std::string & getTitle()
Definition: menu.h:157
std::vector< MenuBasePtr > m_items
List of menu items.
Definition: menu.h:214
Menu * m_parent
Parent menu. Menu bar if null.
Definition: menu.h:215
Definition: menu.h:232
int getWidth()
Definition: menu.h:62
MenuPtr m_root
current menu on bar open
Definition: menu.h:415
Definition: ui.h:235
std::unordered_map< std::string, std::string > StringMap
hash map for string => string
Definition: util.h:363
const std::string m_title
Title of menu item.
Definition: menu.h:284
std::stack< Menu * > m_menu_heap
Stack of menus being created.
Definition: menu.h:418
const std::string & getTitle()
Definition: menu.h:258
const std::string m_name
menu name
Definition: menu.h:109
int m_height
Height of menu.
Definition: menu.h:222
static Menu * m_current
current active menu
Definition: menu.h:224
std::unordered_map< UI::KeyEvent, std::string > key_menu_map
Definition: menu.h:423
bool active()
Definition: menu.h:372
const std::string & getType()
Definition: menu.h:56
Definition: menu.h:120
int m_width
menu width
Definition: menu.h:111
int m_x0
Horiz origin of menu.
Definition: menu.h:219
int calc_width()
Definition: menu.h:331
void draw_menu(bool fHighlight, const int y0, const int x0, const int w, const std::string &key=std::string())
Definition: menu.cpp:230
void select()
Definition: menu.h:315
virtual int calc_width()=0
Definition: menu.h:341
bool getActive()
Definition: menu.h:327
int m_xbar
Position on menubar if needed.
Definition: menu.h:216
const std::string m_action
name of action of menu item
Definition: menu.h:285
int m_y0
Vertical origin of menu.
Definition: menu.h:220
std::vector< MenuPtr > m_menus
vector of menus in menu bar
Definition: menu.h:417
bool getActive()
Definition: menu.h:264
void draw(bool fHighlight, const int y0, const int x0, const int w)
Definition: menu.h:176
bool m_active
True if active.
Definition: menu.h:212
bool m_active
True if active.
Definition: menu.h:283
void doMenu(const std::string &menuFunctionName)
Definition: ui.cpp:144
int m_level
Keep track of menu level.
Definition: menu.h:416
Definition: ui.h:849