-
Notifications
You must be signed in to change notification settings - Fork 47
/
Event.h
126 lines (97 loc) · 3.37 KB
/
Event.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#pragma once
#include "Accelerator.h"
#include <typeinfo>
#include <algorithm>
#include <iostream>
#include <wx/menuitem.h>
#include <wx/toolbar.h>
#include <regex>
#include "string/replace.h"
namespace ui
{
/* The base class for an Event.
*
* Provides methods to enable/disable the event and to connect wxWidgets
*/
class Event :
public IEvent
{
protected:
// If false, the command is ignored and not executed.
bool _enabled;
public:
// Constructor
Event() :
_enabled(true)
{}
virtual ~Event() {}
// Enables/disables this command according to the passed bool <enabled>
virtual void setEnabled(const bool enabled) {
_enabled = enabled;
}
virtual bool isToggle() const {
return false;
}
virtual bool setToggled(const bool toggled) {
return false;
}
virtual bool empty() const {
// This is the base class for an event, it's empty by default
return true;
}
virtual void keyUp() {}
virtual void keyDown() {}
virtual void updateWidgets() {}
// Empty standard implementation
virtual void connectTopLevelWindow(wxTopLevelWindow* widget) {}
virtual void disconnectTopLevelWindow(wxTopLevelWindow* widget) {}
virtual void connectMenuItem(wxMenuItem* item) {}
virtual void disconnectMenuItem(wxMenuItem* item) {}
virtual void connectToolItem(const wxToolBarToolBase* item) {}
virtual void disconnectToolItem(const wxToolBarToolBase* item) {}
virtual void connectToggleButton(wxToggleButton* button) {}
virtual void disconnectToggleButton(wxToggleButton* button) {}
public:
static void setMenuItemAccelerator(wxMenuItem* item, Accelerator& accel)
{
wxString accelText = accel.getString(true);
setMenuItemAccelerator(item, accelText);
}
static void setMenuItemAccelerator(wxMenuItem* item, const wxString& accelText)
{
// Cut off any existing accelerators
wxString caption = item->GetItemLabel().BeforeFirst('\t');
// greebo: Accelerators seem to globally catch the key events, add a space to fool wxWidgets
item->SetItemLabel(caption + "\t " + accelText);
}
static void clearMenuItemAccelerator(wxMenuItem* item)
{
item->SetItemLabel(item->GetItemLabel().BeforeFirst('\t'));
}
static void setToolItemAccelerator(const wxToolBarToolBase* tool, Accelerator& accel)
{
setToolItemAccelerator(tool, accel.getString(true));
}
static void setToolItemAccelerator(const wxToolBarToolBase* tool, const std::string& accelText)
{
if (tool->GetToolBar() == nullptr)
{
return;
}
tool->GetToolBar()->SetToolShortHelp(tool->GetId(), getCleanToolItemHelpText(tool) +
(!accelText.empty() ? " (" + string::replace_all_copy(accelText, "~", "-") + ")" : ""));
}
static void clearToolItemAccelerator(const wxToolBarToolBase* tool)
{
// Remove the accelerator from this tool
tool->GetToolBar()->SetToolShortHelp(tool->GetId(), getCleanToolItemHelpText(tool));
}
static std::string getCleanToolItemHelpText(const wxToolBarToolBase* tool)
{
std::string prevHelp = tool->GetShortHelp().ToStdString();
// Use a regex to cut off the trailing " (Ctrl-X)"
std::regex expr("\\s\\(.+\\)$");
return std::regex_replace(prevHelp, expr, "");
}
};
}