File: imenu.h

package info (click to toggle)
darkradiant 3.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 41,080 kB
  • sloc: cpp: 264,743; ansic: 10,659; python: 1,852; xml: 1,650; sh: 92; makefile: 21
file content (108 lines) | stat: -rw-r--r-- 2,805 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#pragma once

#include <memory>

#include <functional>
#include <memory>

class wxMenuItem;

namespace ui
{

/**
 * An abstract MenuItem needs to provide a getWidget() method
 * which will be packed into a parent GTK container, plus
 * various callbacks for determining the visibility/sensitivity of
 * this item (these are invoked before the menu is showing itself).
 *
 * An utility implementation of this class can be found in
 * gtkutil::MenuItem which can be constructed from strings
 * and std::function objects.
 */
class IMenuItem
{
public:
	/**
	 * Each menu item must return a distinct widget which is packed
	 * into the parent wxMenu
	 */
	virtual wxMenuItem* getMenuItem() = 0;

	// Callback to run when this item is selected in the menus
	virtual void execute() = 0;

	/**
	 * Returns TRUE if this item is visible and should be rendered.
	 * Default implementation returns true for convenience.
	 */
	virtual bool isVisible()
	{
		return true;
	}

	/**
	 * Returns TRUE if this item is sensitive and therefore clickable.
	 * Default implementation returns true for convenience.
	 */
	virtual bool isSensitive()
	{
		return true;
	}

	/**
	 * Called to let the item prepare its visual appearance. Empty default impl.
	 */
	virtual void preShow()
	{}
};
typedef std::shared_ptr<IMenuItem> IMenuItemPtr;

/**
 * An abstract menu object, which can have one or more IMenuItems as children.
 * The order in which the items are added is visually preserved.
 */
class IMenu
{
public:
	/* PUBLIC TYPES */

	/**
	 * Function callback. Each menu item is associated with one of these, which
	 * is invoked when the menu item is activated.
	 */
	typedef std::function<void (void)> Callback;

	/**
	 * Sensitivity callback. This function object returns a true or false value,
	 * indicating whether the associated menu item should be clickable or not.
	 */
	typedef std::function<bool (void)> SensitivityTest;

	/**
	 * Visibility callback. This function object returns a true or false value,
	 * indicating whether the associated menu item should be visible or not.
	 */
	typedef std::function<bool (void)> VisibilityTest;

protected:
	/*
	 * Default test. Returns true in all cases. If a menu item does
	 * not specify its own test, this will be used to ensure the
	 * item is always visible or sensitive.
	 */
	static bool _alwaysTrue() { return true; }

public:
	// Convenience method, directly taking text and icon strings plus
	// callback function objects as argument.
	virtual void addItem(wxMenuItem* widget,
						 const Callback& callback,
						 const SensitivityTest& sensTest = SensitivityTest(_alwaysTrue),
						 const VisibilityTest& visTest = VisibilityTest(_alwaysTrue)) = 0;

	// Adds a certain item to this menu
	virtual void addItem(const IMenuItemPtr& item) = 0;
};

} // namespace