| 1 |
#pragma once |
| 2 |
|
| 3 |
#include <string> |
| 4 |
#include <memory> |
| 5 |
#include <functional> |
| 6 |
|
| 7 |
#include "imodule.h" |
| 8 |
|
| 9 |
class wxWindow; |
| 10 |
class wxMenuItem; |
| 11 |
class wxToolBarToolBase; |
| 12 |
class wxButton; |
| 13 |
class wxToggleButton; |
| 14 |
class wxMouseEvent; |
| 15 |
class wxKeyEvent; |
| 16 |
class wxToolBar; |
| 17 |
class wxTopLevelWindow; |
| 18 |
|
| 19 |
/* greebo: Below are the actual events that are "read" by the views/observers to |
| 20 |
* interpret the mouseclicks. */ |
| 21 |
|
| 22 |
namespace ui |
| 23 |
{ |
| 24 |
|
| 25 |
// Enum used for events tracking the key state |
| 26 |
enum KeyEventType |
| 27 |
{ |
| 28 |
KeyPressed, |
| 29 |
KeyReleased, |
| 30 |
}; |
| 31 |
typedef std::function<void (KeyEventType)> KeyStateChangeCallback; |
| 32 |
|
| 33 |
} // namespace ui |
| 34 |
|
| 35 |
class IAccelerator |
| 36 |
{ |
| 37 |
public: |
| 38 |
using Ptr = std::shared_ptr<IAccelerator>; |
| 39 |
|
| 40 |
// destructor |
| 41 |
virtual ~IAccelerator() {} |
| 42 |
|
| 43 |
// Get/set the key value |
| 44 |
virtual void setKey(const int key) = 0; |
| 45 |
virtual int getKey() const = 0; |
| 46 |
|
| 47 |
// Get/Set the modifier flags |
| 48 |
virtual void setModifiers(const unsigned int modifiers) = 0; |
| 49 |
virtual unsigned int getModifiers() const = 0; |
| 50 |
|
| 51 |
// Returns a string representation of this accelerator. |
| 52 |
// forMenu == true: returns the localised modifier strings |
| 53 |
// and is using a different separator to prevent wxWidgets |
| 54 |
// from assigning accelerators on its own. |
| 55 |
virtual std::string getString(bool forMenu) const = 0; |
| 56 |
}; |
| 57 |
|
| 58 |
class IEvent |
| 59 |
{ |
| 60 |
public: |
| 61 |
// destructor |
| 62 |
virtual ~IEvent() {} |
| 63 |
|
| 64 |
// Handles the incoming keyUp / keyDown calls |
| 65 |
virtual void keyUp() = 0; |
| 66 |
virtual void keyDown() = 0; |
| 67 |
|
| 68 |
// Enables/disables this event |
| 69 |
virtual void setEnabled(const bool enabled) = 0; |
| 70 |
|
| 71 |
// Connect a wxTopLevelWindow to this event |
| 72 |
virtual void connectTopLevelWindow(wxTopLevelWindow* widget) = 0; |
| 73 |
virtual void disconnectTopLevelWindow(wxTopLevelWindow* widget) = 0; |
| 74 |
|
| 75 |
virtual void connectToolItem(const wxToolBarToolBase* item) = 0; |
| 76 |
virtual void disconnectToolItem(const wxToolBarToolBase* item) = 0; |
| 77 |
|
| 78 |
virtual void connectMenuItem(wxMenuItem* item) = 0; |
| 79 |
virtual void disconnectMenuItem(wxMenuItem* item) = 0; |
| 80 |
|
| 81 |
virtual void connectToggleButton(wxToggleButton* button) = 0; |
| 82 |
virtual void disconnectToggleButton(wxToggleButton* button) = 0; |
| 83 |
|
| 84 |
// Exports the current state to the widgets |
| 85 |
virtual void updateWidgets() = 0; |
| 86 |
|
| 87 |
// Returns true if this event could be toggled (returns false if the event is not a Toggle). |
| 88 |
virtual bool setToggled(const bool toggled) = 0; |
| 89 |
|
| 90 |
/** greebo: Returns true if the event is a Toggle (or a subtype of a Toggle) |
| 91 |
*/ |
| 92 |
virtual bool isToggle() const = 0; |
| 93 |
|
| 94 |
// Returns true, if this is any empty Event (no command attached) |
| 95 |
virtual bool empty() const = 0; |
| 96 |
}; |
| 97 |
typedef std::shared_ptr<IEvent> IEventPtr; |
| 98 |
|
| 99 |
// Event visitor class |
| 100 |
class IEventVisitor |
| 101 |
{ |
| 102 |
public: |
| 103 |
// destructor |
| 104 |
virtual ~IEventVisitor() {} |
| 105 |
|
| 106 |
virtual void visit(const std::string& eventName, const IAccelerator& accel) = 0; |
| 107 |
}; |
| 108 |
|
| 109 |
constexpr const char* const MODULE_EVENTMANAGER("EventManager"); |
| 110 |
|
| 111 |
// The function object invoked when a ToggleEvent is changing states |
| 112 |
// The passed boolean indicates the new toggle state (true = active/toggled) |
| 113 |
typedef std::function<void(bool)> ToggleCallback; |
| 114 |
|
| 115 |
/** |
| 116 |
* \brief |
| 117 |
* A toggle function which may or may not succeed |
| 118 |
* |
| 119 |
* \return |
| 120 |
* true if the state change was successful, false if there was an error and the |
| 121 |
* toggle should remain in its previous state. |
| 122 |
*/ |
| 123 |
using AdvancedToggleCallback = std::function<bool(bool)>; |
| 124 |
|
| 125 |
class IEventManager : |
| 126 |
public RegisterableModule |
| 127 |
{ |
| 128 |
public: |
| 129 |
// Loads all accelerator bindings from the defaults in the stock input.xml |
| 130 |
virtual void resetAcceleratorBindings() = 0; |
| 131 |
|
| 132 |
// Add a command and specify the statement to execute when triggered |
| 133 |
virtual IEventPtr addCommand(const std::string& name, const std::string& statement, bool reactOnKeyUp) = 0; |
| 134 |
|
| 135 |
// Creates a new keyevent that calls the given callback when invoked |
| 136 |
virtual IEventPtr addKeyEvent(const std::string& name, const ui::KeyStateChangeCallback& keyStateChangeCallback) = 0; |
| 137 |
|
| 138 |
// Creates a new toggle event that calls the given callback when toggled |
| 139 |
virtual IEventPtr addAdvancedToggle(const std::string& name, |
| 140 |
const AdvancedToggleCallback& onToggled) = 0; |
| 141 |
virtual IEventPtr addWidgetToggle(const std::string& name) = 0; |
| 142 |
virtual IEventPtr addRegistryToggle(const std::string& name, const std::string& registryKey) = 0; |
| 143 |
|
| 144 |
/** |
| 145 |
* \brief |
| 146 |
* Add a simple toggle callback, which always succeeds. |
| 147 |
* |
| 148 |
* \see addAdvancedToggle |
| 149 |
*/ |
| 150 |
IEventPtr addToggle(const std::string& name, const ToggleCallback& onToggled) |
| 151 |
{ |
| 152 |
return addAdvancedToggle( |
| 153 |
name, [onToggled](bool v) { onToggled(v); return true; } |
| 154 |
); |
| 155 |
} |
| 156 |
|
| 157 |
// Set the according Toggle command (identified by <name>) to the bool <toggled> |
| 158 |
virtual void setToggled(const std::string& name, const bool toggled) = 0; |
| 159 |
|
| 160 |
// Returns the pointer to the command specified by the <given> commandName |
| 161 |
virtual IEventPtr findEvent(const std::string& name) = 0; |
| 162 |
|
| 163 |
// Returns the name of the event currently mapped to the given key combo |
| 164 |
virtual std::string findEventForAccelerator(wxKeyEvent& ev) = 0; |
| 165 |
|
| 166 |
// Retrieves the event name for the given IEventPtr |
| 167 |
virtual std::string getEventName(const IEventPtr& event) = 0; |
| 168 |
|
| 169 |
// Connects the given accelerator to the given command (identified by the string) |
| 170 |
virtual void connectAccelerator(wxKeyEvent& keyEvent, const std::string& command) = 0; |
| 171 |
// Disconnects the given command from any accelerators |
| 172 |
virtual void disconnectAccelerator(const std::string& command) = 0; |
| 173 |
|
| 174 |
// Returns the accelerator that is bound to the given event (name) |
| 175 |
// Returns an empty reference if either event or accelerator were not found. |
| 176 |
virtual IAccelerator::Ptr findAcceleratorForEvent(const std::string& eventName) = 0; |
| 177 |
|
| 178 |
// Register the given menu item with the given command. The event manager updates this item |
| 179 |
// when the accelerator association changes |
| 180 |
virtual void registerMenuItem(const std::string& eventName, wxMenuItem* item) = 0; |
| 181 |
virtual void unregisterMenuItem(const std::string& eventName, wxMenuItem* item) = 0; |
| 182 |
|
| 183 |
virtual void registerToolItem(const std::string& eventName, const wxToolBarToolBase* item) = 0; |
| 184 |
virtual void unregisterToolItem(const std::string& eventName, const wxToolBarToolBase* item) = 0; |
| 185 |
|
| 186 |
// Loads the shortcut->command associations from the XMLRegistry |
| 187 |
virtual void loadAccelerators() = 0; |
| 188 |
|
| 189 |
// Enables/Disables the specified command |
| 190 |
virtual void enableEvent(const std::string& eventName) = 0; |
| 191 |
virtual void disableEvent(const std::string& eventName) = 0; |
| 192 |
|
| 193 |
// Renames the given command, keeping accelerator bindings intact |
| 194 |
virtual void renameEvent(const std::string& oldEventName, const std::string& newEventName) = 0; |
| 195 |
|
| 196 |
// Removes the given event and disconnects all accelerators from it |
| 197 |
virtual void removeEvent(const std::string& eventName) = 0; |
| 198 |
|
| 199 |
// Visit each event with the given class |
| 200 |
virtual void foreachEvent(IEventVisitor& eventVisitor) = 0; |
| 201 |
|
| 202 |
/* greebo: Retrieves the string representation of the given event |
| 203 |
*/ |
| 204 |
virtual std::string getEventStr(wxKeyEvent& ev) = 0; |
| 205 |
}; |
| 206 |
|
| 207 |
// Global accessor for the event manager |
| 208 |
inline IEventManager& GlobalEventManager() |
| 209 |
{ |
| 210 |
static module::InstanceReference<IEventManager> _reference(MODULE_EVENTMANAGER); |
| 211 |
return _reference; |
| 212 |
} |