Changeset - r20124:2d41107f4aeb
[Not reviewed]
master
0 6 0
frosch - 11 years ago 2013-03-17 13:05:45
frosch@openttd.org
(svn r25092) -Codechange: Deduplicate keyboard handling between console and editboxes.
6 files changed with 71 insertions and 106 deletions:
0 comments (0 inline, 0 general)
src/console_gui.cpp
Show inline comments
 
@@ -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 {
src/misc_gui.cpp
Show inline comments
 
@@ -725,56 +725,6 @@ void GuiShowTooltips(Window *parent, Str
 
	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()) {
src/querystring_gui.h
Show inline comments
 
@@ -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);
src/textbuf.cpp
Show inline comments
 
@@ -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;
 
}
src/textbuf_type.h
Show inline comments
 
@@ -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();
 

	
src/window.cpp
Show inline comments
 
@@ -2250,26 +2250,24 @@ static bool MaybeBringWindowToFront(Wind
 
 */
 
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 
 
			}
 
			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 
 
			}
 
			break;
 

	
 
		case HKPR_NOT_HANDLED:
 
			return ES_NOT_HANDLED;
 

	
 
		default: break;
 
	}
 

	
 
@@ -2307,7 +2308,7 @@ EventState Window::HandleEditBoxKey(int 
 
			break;
 
	}
 

	
 
	return state;
 
	return ES_HANDLED;
 
}
 

	
 
/**
0 comments (0 inline, 0 general)