summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorfrosch <frosch@openttd.org>2013-03-17 13:05:45 +0000
committerfrosch <frosch@openttd.org>2013-03-17 13:05:45 +0000
commitc2c50b0c507d427caa32f71eda009bba95fac684 (patch)
treef21279587550f7711729c87c56ab67369e6c3799
parent1ddf5a0786a59431c3092673fd65a7a8aca755a3 (diff)
downloadopenttd-c2c50b0c507d427caa32f71eda009bba95fac684.tar.xz
(svn r25092) -Codechange: Deduplicate keyboard handling between console and editboxes.
-rw-r--r--src/console_gui.cpp35
-rw-r--r--src/misc_gui.cpp50
-rw-r--r--src/querystring_gui.h13
-rw-r--r--src/textbuf.cpp46
-rw-r--r--src/textbuf_type.h14
-rw-r--r--src/window.cpp19
6 files changed, 71 insertions, 106 deletions
diff --git a/src/console_gui.cpp b/src/console_gui.cpp
index e97e458ff..ca0a01853 100644
--- a/src/console_gui.cpp
+++ b/src/console_gui.cpp
@@ -290,46 +290,13 @@ struct IConsoleWindow : Window
MarkWholeScreenDirty();
break;
-#ifdef WITH_COCOA
- case (WKC_META | 'V'):
-#endif
- case (WKC_CTRL | 'V'):
- if (_iconsole_cmdline.InsertClipboard()) {
- IConsoleResetHistoryPos();
- this->SetDirty();
- }
- break;
-
case (WKC_CTRL | 'L'):
IConsoleCmdExec("clear");
break;
-#ifdef WITH_COCOA
- case (WKC_META | 'U'):
-#endif
- case (WKC_CTRL | 'U'):
- _iconsole_cmdline.DeleteAll();
- this->SetDirty();
- break;
-
- case WKC_BACKSPACE: case WKC_DELETE:
- if (_iconsole_cmdline.DeleteChar(keycode)) {
- IConsoleResetHistoryPos();
- this->SetDirty();
- }
- break;
-
- case WKC_LEFT: case WKC_RIGHT: case WKC_END: case WKC_HOME:
- if (_iconsole_cmdline.MovePos(keycode)) {
- IConsoleResetHistoryPos();
- this->SetDirty();
- }
- break;
-
default:
- if (IsValidChar(key, CS_ALPHANUMERAL)) {
+ if (_iconsole_cmdline.HandleKeyPress(key, keycode) != HKPR_NOT_HANDLED) {
IConsoleWindow::scroll = 0;
- _iconsole_cmdline.InsertChar(key);
IConsoleResetHistoryPos();
this->SetDirty();
} else {
diff --git a/src/misc_gui.cpp b/src/misc_gui.cpp
index d6303fa35..0fa450c51 100644
--- a/src/misc_gui.cpp
+++ b/src/misc_gui.cpp
@@ -725,56 +725,6 @@ void GuiShowTooltips(Window *parent, StringID str, uint paramcount, const uint64
new TooltipsWindow(parent, str, paramcount, params, close_tooltip);
}
-HandleEditBoxResult QueryString::HandleEditBoxKey(Window *w, int wid, uint16 key, uint16 keycode, EventState &state)
-{
- if (!w->IsWidgetGloballyFocused(wid)) return HEBR_NOT_FOCUSED;
-
- state = ES_HANDLED;
-
- bool edited = false;
-
- switch (keycode) {
- case WKC_ESC: return HEBR_CANCEL;
-
- case WKC_RETURN: case WKC_NUM_ENTER: return HEBR_CONFIRM;
-
-#ifdef WITH_COCOA
- case (WKC_META | 'V'):
-#endif
- case (WKC_CTRL | 'V'):
- edited = this->text.InsertClipboard();
- break;
-
-#ifdef WITH_COCOA
- case (WKC_META | 'U'):
-#endif
- case (WKC_CTRL | 'U'):
- this->text.DeleteAll();
- edited = true;
- break;
-
- case WKC_BACKSPACE: case WKC_DELETE:
- case WKC_CTRL | WKC_BACKSPACE: case WKC_CTRL | WKC_DELETE:
- edited = this->text.DeleteChar(keycode);
- break;
-
- case WKC_LEFT: case WKC_RIGHT: case WKC_END: case WKC_HOME:
- case WKC_CTRL | WKC_LEFT: case WKC_CTRL | WKC_RIGHT:
- this->text.MovePos(keycode);
- break;
-
- default:
- if (IsValidChar(key, this->text.afilter)) {
- edited = this->text.InsertChar(key);
- } else {
- state = ES_NOT_HANDLED;
- }
- break;
- }
-
- return edited ? HEBR_EDITING : HEBR_CURSOR;
-}
-
void QueryString::HandleEditBox(Window *w, int wid)
{
if (w->IsWidgetGloballyFocused(wid) && this->text.HandleCaret()) {
diff --git a/src/querystring_gui.h b/src/querystring_gui.h
index 6ec93ffd9..b2c91f354 100644
--- a/src/querystring_gui.h
+++ b/src/querystring_gui.h
@@ -17,18 +17,6 @@
#include "window_gui.h"
/**
- * Return values for HandleEditBoxKey
- */
-enum HandleEditBoxResult
-{
- HEBR_EDITING, ///< Editbox content changed.
- HEBR_CURSOR, ///< Non-text change, e.g. cursor position.
- HEBR_CONFIRM, ///< Return or enter key pressed.
- HEBR_CANCEL, ///< Escape key pressed.
- HEBR_NOT_FOCUSED, ///< Edit box widget not focused.
-};
-
-/**
* Data stored about a string that can be modified in the GUI
*/
struct QueryString {
@@ -65,7 +53,6 @@ public:
void DrawEditBox(const Window *w, int wid) const;
void ClickEditBox(Window *w, Point pt, int wid, int click_count, bool focus_changed);
void HandleEditBox(Window *w, int wid);
- HandleEditBoxResult HandleEditBoxKey(Window *w, int wid, uint16 key, uint16 keycode, EventState &state);
};
void ShowOnScreenKeyboard(Window *parent, int button);
diff --git a/src/textbuf.cpp b/src/textbuf.cpp
index 2a34a18d9..e84b14e7b 100644
--- a/src/textbuf.cpp
+++ b/src/textbuf.cpp
@@ -450,3 +450,49 @@ bool Textbuf::HandleCaret()
}
return false;
}
+
+HandleKeyPressResult Textbuf::HandleKeyPress(uint16 key, uint16 keycode)
+{
+ bool edited = false;
+
+ switch (keycode) {
+ case WKC_ESC: return HKPR_CANCEL;
+
+ case WKC_RETURN: case WKC_NUM_ENTER: return HKPR_CONFIRM;
+
+#ifdef WITH_COCOA
+ case (WKC_META | 'V'):
+#endif
+ case (WKC_CTRL | 'V'):
+ edited = this->InsertClipboard();
+ break;
+
+#ifdef WITH_COCOA
+ case (WKC_META | 'U'):
+#endif
+ case (WKC_CTRL | 'U'):
+ this->DeleteAll();
+ edited = true;
+ break;
+
+ case WKC_BACKSPACE: case WKC_DELETE:
+ case WKC_CTRL | WKC_BACKSPACE: case WKC_CTRL | WKC_DELETE:
+ edited = this->DeleteChar(keycode);
+ break;
+
+ case WKC_LEFT: case WKC_RIGHT: case WKC_END: case WKC_HOME:
+ case WKC_CTRL | WKC_LEFT: case WKC_CTRL | WKC_RIGHT:
+ this->MovePos(keycode);
+ break;
+
+ default:
+ if (IsValidChar(key, this->afilter)) {
+ edited = this->InsertChar(key);
+ } else {
+ return HKPR_NOT_HANDLED;
+ }
+ break;
+ }
+
+ return edited ? HKPR_EDITING : HKPR_CURSOR;
+}
diff --git a/src/textbuf_type.h b/src/textbuf_type.h
index e467a70da..d0b75f818 100644
--- a/src/textbuf_type.h
+++ b/src/textbuf_type.h
@@ -15,6 +15,18 @@
#include "string_type.h"
#include "strings_type.h"
+/**
+ * Return values for Textbuf::HandleKeypress
+ */
+enum HandleKeyPressResult
+{
+ HKPR_EDITING, ///< Textbuf content changed.
+ HKPR_CURSOR, ///< Non-text change, e.g. cursor position.
+ HKPR_CONFIRM, ///< Return or enter key pressed.
+ HKPR_CANCEL, ///< Escape key pressed.
+ HKPR_NOT_HANDLED, ///< Key does not affect editboxes.
+};
+
/** Helper/buffer for input fields. */
struct Textbuf {
CharSetFilter afilter; ///< Allowed characters
@@ -43,6 +55,8 @@ struct Textbuf {
bool DeleteChar(uint16 keycode);
bool MovePos(uint16 keycode);
+ HandleKeyPressResult HandleKeyPress(uint16 key, uint16 keycode);
+
bool HandleCaret();
void UpdateSize();
diff --git a/src/window.cpp b/src/window.cpp
index f818471bd..d223f5cfd 100644
--- a/src/window.cpp
+++ b/src/window.cpp
@@ -2250,26 +2250,24 @@ static bool MaybeBringWindowToFront(Window *w)
*/
EventState Window::HandleEditBoxKey(int wid, uint16 key, uint16 keycode)
{
- EventState state = ES_NOT_HANDLED;
-
QueryString *query = this->GetQueryString(wid);
- if (query == NULL) return state;
+ if (query == NULL) return ES_NOT_HANDLED;
int action = QueryString::ACTION_NOTHING;
- switch (query->HandleEditBoxKey(this, wid, key, keycode, state)) {
- case HEBR_EDITING:
+ switch (query->text.HandleKeyPress(key, keycode)) {
+ case HKPR_EDITING:
this->SetWidgetDirty(wid);
this->OnEditboxChanged(wid);
break;
- case HEBR_CURSOR:
+ case HKPR_CURSOR:
this->SetWidgetDirty(wid);
/* For the OSK also invalidate the parent window */
if (this->window_class == WC_OSK) this->InvalidateData();
break;
- case HEBR_CONFIRM:
+ case HKPR_CONFIRM:
if (this->window_class == WC_OSK) {
this->OnClick(Point(), WID_OSK_OK, 1);
} else if (query->ok_button >= 0) {
@@ -2279,7 +2277,7 @@ EventState Window::HandleEditBoxKey(int wid, uint16 key, uint16 keycode)
}
break;
- case HEBR_CANCEL:
+ case HKPR_CANCEL:
if (this->window_class == WC_OSK) {
this->OnClick(Point(), WID_OSK_CANCEL, 1);
} else if (query->cancel_button >= 0) {
@@ -2289,6 +2287,9 @@ EventState Window::HandleEditBoxKey(int wid, uint16 key, uint16 keycode)
}
break;
+ case HKPR_NOT_HANDLED:
+ return ES_NOT_HANDLED;
+
default: break;
}
@@ -2307,7 +2308,7 @@ EventState Window::HandleEditBoxKey(int wid, uint16 key, uint16 keycode)
break;
}
- return state;
+ return ES_HANDLED;
}
/**