-
Notifications
You must be signed in to change notification settings - Fork 47
/
GlobalKeyEventFilter.cpp
170 lines (138 loc) · 5.11 KB
/
GlobalKeyEventFilter.cpp
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include "GlobalKeyEventFilter.h"
#include "imousetoolmanager.h"
#include <wx/event.h>
#include <wx/window.h>
#include <wx/textctrl.h>
#include <wx/combobox.h>
#include <wx/spinctrl.h>
#include <wx/stc/stc.h>
#include "wxutil/TreeView.h"
#include "wxutil/Modifier.h"
#include "wxutil/dialog/DialogBase.h"
#include "itextstream.h"
#include "EventManager.h"
namespace ui
{
namespace
{
// Depending on our platform detecting the wxutil::TreeView from an eventobject
// might be tricky. in wxMSW the events are fired from an internal wxDataViewMainWindow
// and the parent is a TreeView, in wxGTK the events are fired on the TreeView* itself
wxutil::TreeView* getTreeView(wxWindow* window)
{
wxutil::TreeView* view = dynamic_cast<wxutil::TreeView*>(window);
return view != nullptr ? view : dynamic_cast<wxutil::TreeView*>(window->GetParent());
}
}
GlobalKeyEventFilter::GlobalKeyEventFilter(EventManager& eventManager) :
_eventManager(eventManager)
{
wxEvtHandler::AddFilter(this);
}
GlobalKeyEventFilter::~GlobalKeyEventFilter()
{
wxEvtHandler::RemoveFilter(this);
}
int GlobalKeyEventFilter::FilterEvent(wxEvent& event)
{
const wxEventType eventType = event.GetEventType();
if (eventType == wxEVT_KEY_DOWN || eventType == wxEVT_KEY_UP)
{
wxKeyEvent& keyEvent = static_cast<wxKeyEvent&>(event);
switch (checkEvent(keyEvent))
{
case EventShouldBeIgnored:
return Event_Skip;
case EventAlreadyProcessed:
return Event_Processed;
case EventShouldBeProcessed:
{
// Attempt to find an accelerator
bool acceleratorFound = handleAccelerator(keyEvent);
// Update the status bar in any case
GlobalMouseToolManager().updateStatusbar(wxutil::Modifier::GetStateForKeyEvent(keyEvent));
return acceleratorFound ? Event_Processed : Event_Skip;
}
};
}
// Continue processing the event normally for non-key events.
return Event_Skip;
}
GlobalKeyEventFilter::EventCheckResult GlobalKeyEventFilter::checkEvent(wxKeyEvent& keyEvent)
{
// Check if the event object can handle the event
wxWindow* window = dynamic_cast<wxWindow*>(keyEvent.GetEventObject());
if (!window) return EventShouldBeIgnored;
if (window->GetEventHandler()->ProcessEvent(keyEvent))
{
// The control handled this event, so don't check for accelerators
return EventAlreadyProcessed;
}
// Don't catch key events when a blocking dialog window is in focus
wxWindow* topLevelParent = wxGetTopLevelParent(window);
if (dynamic_cast<wxutil::DialogBase*>(topLevelParent))
{
return EventShouldBeIgnored;
}
wxObject* eventObject = keyEvent.GetEventObject();
// Don't eat key events of text controls
if (wxDynamicCast(eventObject, wxTextCtrl) || wxDynamicCast(eventObject, wxStyledTextCtrl) ||
wxDynamicCast(eventObject, wxComboBox) || wxDynamicCast(eventObject, wxSpinCtrl) ||
wxDynamicCast(eventObject, wxSpinCtrlDouble))
{
// For tool windows we let the ESC key propagate, since it's used to
// de-select stuff.
return keyEvent.GetKeyCode() == WXK_ESCAPE ? EventShouldBeProcessed : EventShouldBeIgnored;
}
// Special handling for our treeviews with type ahead search
// Treeviews are special, the actual wxWindows receiving/generating the event are the
// privately implemented wxDataViewMainWindows, so let's attempt identifying that case
// Check for keys with Alt or Ctrl modifers, these are not handled by the treeview search
wxutil::TreeView* treeView = getTreeView(window);
if (treeView != nullptr && !keyEvent.ControlDown() && !keyEvent.AltDown())
{
// We have a modifier-less key event in a wxutil::TreeView. It will be passed through
// in the general case. The ESC key will be caught if the treeview is not search mode.
if (keyEvent.GetKeyCode() == WXK_ESCAPE)
{
// The ESC key should only be passed to the dataview if the search popup is actually shown
if (treeView->HasActiveSearchPopup())
{
return EventShouldBeIgnored;
}
}
else // non-escape key, no modifiers, don't process it
{
return EventShouldBeIgnored;
}
}
return EventShouldBeProcessed;
}
bool GlobalKeyEventFilter::handleAccelerator(wxKeyEvent& keyEvent)
{
// The event stays unhandled by the control that it's triggered on
// check if an accelerator matches this event
#if 1
return _eventManager.handleKeyEvent(keyEvent);
#else
AcceleratorList accelList = _eventManager.findAccelerator(keyEvent);
if (!accelList.empty())
{
// Pass the execute() call to all found accelerators
for (const auto& accelerator : accelList)
{
if (keyEvent.GetEventType() == wxEVT_KEY_DOWN)
{
accelerator->keyDown();
}
else
{
accelerator->keyUp();
}
}
return true; // handled
}
return false; // no accelerator found
#endif
}
} // namespace