/
Toggle.h
103 lines (73 loc) · 2.54 KB
/
Toggle.h
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
#pragma once
#include "ieventmanager.h"
#include <functional>
#include <sigc++/connection.h>
#include "Event.h"
#include <wx/event.h>
class wxToggleButton;
class wxCommandEvent;
namespace ui
{
/* greebo: A Toggle object has a state (toggled = TRUE/FALSE) and a callback that
* is invoked on toggle.
*
* A Toggle can be connected to a wxMenuItem/wxToggleButton/wxToolBarToolBase via the
* corresponding method.
*
* Use the updateWidget() method to export the current state of the Toggle object to
* the connected widgets.
*/
class Toggle :
public Event,
public wxEvtHandler
{
private:
// The callback to be performed on toggle()
ToggleCallback _callback;
protected:
typedef std::set<wxMenuItem*> MenuItems;
MenuItems _menuItems;
std::map<IMenuElementPtr, sigc::connection> _menuElements;
typedef std::set<wxToolBarToolBase*> ToolItems;
ToolItems _toolItems;
typedef std::set<wxToggleButton*> Buttons;
Buttons _buttons;
bool _callbackActive;
// The toggled state of this object
bool _toggled;
public:
Toggle(const ToggleCallback& callback);
virtual ~Toggle();
// Returns usually false, because a Toggle is never empty
virtual bool empty() const;
// Set the toggled state to true/false, according to <toggled> and update
// any associated widgets or notify any callbacks.
virtual bool setToggled(const bool toggled);
// Update the "active" state of the connected widgets
virtual void updateWidgets();
// On key press >> toggle the internal state
virtual void keyDown();
// Returns true if the internal state is true
bool isToggled() const;
// Returns true for this and all derived classes
virtual bool isToggle() const;
virtual void connectMenuItem(wxMenuItem* item);
virtual void disconnectMenuItem(wxMenuItem* item);
virtual void connectToolItem(wxToolBarToolBase* item);
virtual void disconnectToolItem(wxToolBarToolBase* item);
virtual void connectToggleButton(wxToggleButton* button);
virtual void disconnectToggleButton(wxToggleButton* button);
// Invoke the registered callback and update/notify
virtual void toggle();
virtual void connectAccelerator(IAccelerator& accel);
virtual void disconnectAccelerators();
virtual void connectMenuItem(const IMenuElementPtr& item) override;
virtual void disconnectMenuItem(const IMenuElementPtr& item) override;
protected:
virtual void onMenuItemClicked(wxCommandEvent& ev);
virtual void onToolItemClicked(wxCommandEvent& ev);
virtual void onToggleButtonClicked(wxCommandEvent& ev);
void onItemActivated();
}; // class Toggle
typedef std::shared_ptr<Toggle> TogglePtr;
}