Debian Codegraph @

include/ui/imenu.h

raw
1 #pragma once
2
3 #include <memory>
4
5 #include <functional>
6 #include <memory>
7
8 class wxMenuItem;
9
10 namespace ui
11 {
12
13 /**
14 * An abstract MenuItem needs to provide a getWidget() method
15 * which will be packed into a parent GTK container, plus
16 * various callbacks for determining the visibility/sensitivity of
17 * this item (these are invoked before the menu is showing itself).
18 *
19 * An utility implementation of this class can be found in
20 * gtkutil::MenuItem which can be constructed from strings
21 * and std::function objects.
22 */
23 class IMenuItem
24 {
25 public:
26 /**
27 * Each menu item must return a distinct widget which is packed
28 * into the parent wxMenu
29 */
30 virtual wxMenuItem* getMenuItem() = 0;
31
32 // Callback to run when this item is selected in the menus
33 virtual void execute() = 0;
34
35 /**
36 * Returns TRUE if this item is visible and should be rendered.
37 * Default implementation returns true for convenience.
38 */
39 virtual bool isVisible()
40 {
41 return true;
42 }
43
44 /**
45 * Returns TRUE if this item is sensitive and therefore clickable.
46 * Default implementation returns true for convenience.
47 */
48 virtual bool isSensitive()
49 {
50 return true;
51 }
52
53 /**
54 * Called to let the item prepare its visual appearance. Empty default impl.
55 */
56 virtual void preShow()
57 {}
58 };
59 typedef std::shared_ptr<IMenuItem> IMenuItemPtr;
60
61 /**
62 * An abstract menu object, which can have one or more IMenuItems as children.
63 * The order in which the items are added is visually preserved.
64 */
65 class IMenu
66 {
67 public:
68 /* PUBLIC TYPES */
69
70 /**
71 * Function callback. Each menu item is associated with one of these, which
72 * is invoked when the menu item is activated.
73 */
74 typedef std::function<void (void)> Callback;
75
76 /**
77 * Sensitivity callback. This function object returns a true or false value,
78 * indicating whether the associated menu item should be clickable or not.
79 */
80 typedef std::function<bool (void)> SensitivityTest;
81
82 /**
83 * Visibility callback. This function object returns a true or false value,
84 * indicating whether the associated menu item should be visible or not.
85 */
86 typedef std::function<bool (void)> VisibilityTest;
87
88 protected:
89 /*
90 * Default test. Returns true in all cases. If a menu item does
91 * not specify its own test, this will be used to ensure the
92 * item is always visible or sensitive.
93 */
94 static bool _alwaysTrue() { return true; }
95
96 public:
97 // Convenience method, directly taking text and icon strings plus
98 // callback function objects as argument.
99 virtual void addItem(wxMenuItem* widget,
100 const Callback& callback,
101 const SensitivityTest& sensTest = SensitivityTest(_alwaysTrue),
102 const VisibilityTest& visTest = VisibilityTest(_alwaysTrue)) = 0;
103
104 // Adds a certain item to this menu
105 virtual void addItem(const IMenuItemPtr& item) = 0;
106 };
107
108 } // namespace