summaryrefslogtreecommitdiff
path: root/src/window.cpp
diff options
context:
space:
mode:
authorfrosch <frosch@openttd.org>2013-06-15 15:30:16 +0000
committerfrosch <frosch@openttd.org>2013-06-15 15:30:16 +0000
commit62913838237c474419c68124ea7aebd74e72c628 (patch)
tree2570798d8f5a5667e8fed3395c2e9e742b96639b /src/window.cpp
parent1b8b1f3f6cbd65b73399b1d39aacb7fb674a091c (diff)
downloadopenttd-62913838237c474419c68124ea7aebd74e72c628.tar.xz
(svn r25411) -Add: Window::OnHotkey
Diffstat (limited to 'src/window.cpp')
-rw-r--r--src/window.cpp38
1 files changed, 36 insertions, 2 deletions
diff --git a/src/window.cpp b/src/window.cpp
index 40b710044..297143ebe 100644
--- a/src/window.cpp
+++ b/src/window.cpp
@@ -86,7 +86,7 @@ char *_windows_file;
/** Window description constructor. */
WindowDesc::WindowDesc(WindowPosition def_pos, const char *ini_key, int16 def_width, int16 def_height,
WindowClass window_class, WindowClass parent_class, uint32 flags,
- const NWidgetPart *nwid_parts, int16 nwid_length) :
+ const NWidgetPart *nwid_parts, int16 nwid_length, HotkeyList *hotkeys) :
default_pos(def_pos),
default_width(def_width),
default_height(def_height),
@@ -96,6 +96,7 @@ WindowDesc::WindowDesc(WindowPosition def_pos, const char *ini_key, int16 def_wi
flags(flags),
nwid_parts(nwid_parts),
nwid_length(nwid_length),
+ hotkeys(hotkeys),
pref_sticky(false),
pref_width(0),
pref_height(0)
@@ -469,6 +470,29 @@ void Window::SetWidgetDirty(byte widget_index) const
}
/**
+ * A hotkey has been pressed.
+ * @param hotkey Hotkey index, by default a widget index of a button or editbox.
+ * @return #ES_HANDLED if the key press has been handled, and the hotkey is not unavailable for some reason.
+ */
+EventState Window::OnHotkey(int hotkey)
+{
+ if (hotkey < 0) return ES_NOT_HANDLED;
+
+ NWidgetCore *nw = this->GetWidget<NWidgetCore>(hotkey);
+ if (nw == NULL || nw->IsDisabled()) return ES_NOT_HANDLED;
+
+ if (nw->type == WWT_EDITBOX) {
+ /* Focus editbox */
+ this->SetFocusedWidget(hotkey);
+ SetFocusedWindow(this);
+ } else {
+ /* Click button */
+ this->OnClick(Point(), hotkey, 1);
+ }
+ return ES_HANDLED;
+}
+
+/**
* Do all things to make a button look clicked and mark it to be
* unclicked in a few ticks.
* @param widget the widget to "click"
@@ -2456,12 +2480,22 @@ void HandleKeypress(uint32 raw_key)
Window *w;
FOR_ALL_WINDOWS_FROM_FRONT(w) {
if (w->window_class == WC_MAIN_TOOLBAR) continue;
+ if (w->window_desc->hotkeys != NULL) {
+ int hotkey = w->window_desc->hotkeys->CheckMatch(keycode);
+ if (hotkey >= 0 && w->OnHotkey(hotkey) == ES_HANDLED) return;
+ }
if (w->OnKeyPress(key, keycode) == ES_HANDLED) return;
}
w = FindWindowById(WC_MAIN_TOOLBAR, 0);
/* When there is no toolbar w is null, check for that */
- if (w != NULL && w->OnKeyPress(key, keycode) == ES_HANDLED) return;
+ if (w != NULL) {
+ if (w->window_desc->hotkeys != NULL) {
+ int hotkey = w->window_desc->hotkeys->CheckMatch(keycode);
+ if (hotkey >= 0 && w->OnHotkey(hotkey) == ES_HANDLED) return;
+ }
+ if (w->OnKeyPress(key, keycode) == ES_HANDLED) return;
+ }
HandleGlobalHotkeys(key, keycode);
}