(svn r13151) -Codechange: use an enum instead of bool as return type of OnKeyPress/OnCTRLStateChange to make it obvious what the return values mean.
authorrubidium
Sat, 17 May 2008 23:11:06 +0000
changeset 9285 235e5e2d7f55
parent 9284 e2987a51f382
child 9286 041f0027ca74
(svn r13151) -Codechange: use an enum instead of bool as return type of OnKeyPress/OnCTRLStateChange to make it obvious what the return values mean.
src/bridge_gui.cpp
src/console.cpp
src/depot_gui.cpp
src/genworld_gui.cpp
src/main_gui.cpp
src/misc_gui.cpp
src/network/network_gui.cpp
src/news_gui.cpp
src/order_gui.cpp
src/querystring_gui.h
src/signs_gui.cpp
src/window.cpp
src/window_gui.h
--- a/src/bridge_gui.cpp	Sat May 17 22:52:51 2008 +0000
+++ b/src/bridge_gui.cpp	Sat May 17 23:11:06 2008 +0000
@@ -121,16 +121,16 @@
 		}
 	}
 
-	virtual bool OnKeyPress(uint16 key, uint16 keycode)
+	virtual EventState OnKeyPress(uint16 key, uint16 keycode)
 	{
 		const uint8 i = keycode - '1';
 		if (i < 9 && i < this->bridges->list_length) {
 			/* Build the requested bridge */
 			this->BuildBridge(i);
 			delete this;
-			return false;
+			return ES_HANDLED;
 		}
-		return true;
+		return ES_NOT_HANDLED;
 	}
 
 	virtual void OnClick(Point pt, int widget)
--- a/src/console.cpp	Sat May 17 22:52:51 2008 +0000
+++ b/src/console.cpp	Sat May 17 23:11:06 2008 +0000
@@ -126,7 +126,7 @@
 		if (HandleCaret(&_iconsole_cmdline)) this->SetDirty();
 	}
 
-	virtual bool OnKeyPress(uint16 key, uint16 keycode)
+	virtual EventState OnKeyPress(uint16 key, uint16 keycode)
 	{
 		switch (keycode) {
 			case WKC_UP:
@@ -230,10 +230,10 @@
 					IConsoleResetHistoryPos();
 					this->SetDirty();
 				} else {
-					return true;
+					return ES_NOT_HANDLED;
 				}
 		}
-		return false;
+		return ES_HANDLED;
 	}
 };
 
--- a/src/depot_gui.cpp	Sat May 17 22:52:51 2008 +0000
+++ b/src/depot_gui.cpp	Sat May 17 23:11:06 2008 +0000
@@ -961,14 +961,14 @@
 		ResizeDepotButtons(this);
 	}
 
-	virtual bool OnCTRLStateChange()
+	virtual EventState OnCTRLStateChange()
 	{
 		if (this->sel != INVALID_VEHICLE) {
 			_cursor.vehchain = _ctrl_pressed;
 			this->InvalidateWidget(DEPOT_WIDGET_MATRIX);
 		}
 
-		return true;
+		return ES_HANDLED;
 	}
 };
 
--- a/src/genworld_gui.cpp	Sat May 17 22:52:51 2008 +0000
+++ b/src/genworld_gui.cpp	Sat May 17 23:11:06 2008 +0000
@@ -470,17 +470,17 @@
 		this->HandleEditBox(GLAND_RANDOM_EDITBOX);
 	}
 
-	virtual bool OnKeyPress(uint16 key, uint16 keycode)
+	virtual EventState OnKeyPress(uint16 key, uint16 keycode)
 	{
-		bool cont;
-		this->HandleEditBoxKey(GLAND_RANDOM_EDITBOX, key, keycode, cont);
+		EventState state;
+		this->HandleEditBoxKey(GLAND_RANDOM_EDITBOX, key, keycode, state);
 		/* the seed is unsigned, therefore atoi cannot be used.
 			* As 2^32 - 1 (MAX_UVALUE(uint32)) is a 'magic' value
 			* (use random seed) it should not be possible to be
 			* entered into the input field; the generate seed
 			* button can be used instead. */
 		_patches_newgame.generation_seed = minu(strtoul(this->edit_str_buf, NULL, sizeof(this->edit_str_buf) - 1), MAX_UVALUE(uint32) - 1);
-		return cont;
+		return state;
 	}
 
 	virtual void OnDropdownSelect(int widget, int index)
--- a/src/main_gui.cpp	Sat May 17 22:52:51 2008 +0000
+++ b/src/main_gui.cpp	Sat May 17 23:11:06 2008 +0000
@@ -239,34 +239,34 @@
 		}
 	}
 
-	virtual bool OnKeyPress(uint16 key, uint16 keycode)
+	virtual EventState OnKeyPress(uint16 key, uint16 keycode)
 	{
 		switch (keycode) {
 			case 'Q' | WKC_CTRL:
 			case 'Q' | WKC_META:
 				HandleExitGameRequest();
-				return true;
+				return ES_HANDLED;
 		}
 
 		/* Disable all key shortcuts, except quit shortcuts when
 		* generating the world, otherwise they create threading
 		* problem during the generating, resulting in random
 		* assertions that are hard to trigger and debug */
-		if (IsGeneratingWorld()) return true;
+		if (IsGeneratingWorld()) return ES_NOT_HANDLED;
 
 		if (keycode == WKC_BACKQUOTE) {
 			IConsoleSwitch();
-			return false;
+			return ES_HANDLED;
 		}
 
 		if (keycode == ('B' | WKC_CTRL)) {
 			extern bool _draw_bounding_boxes;
 			_draw_bounding_boxes = !_draw_bounding_boxes;
 			MarkWholeScreenDirty();
-			return false;
+			return ES_HANDLED;
 		}
 
-		if (_game_mode == GM_MENU) return true;
+		if (_game_mode == GM_MENU) return ES_NOT_HANDLED;
 
 		switch (keycode) {
 			case 'C':
@@ -372,9 +372,9 @@
 				break;
 #endif
 
-			default: return true;
+			default: return ES_NOT_HANDLED;
 		}
-		return false;
+		return ES_HANDLED;
 	}
 
 	virtual void OnScroll(Point delta)
--- a/src/misc_gui.cpp	Sat May 17 22:52:51 2008 +0000
+++ b/src/misc_gui.cpp	Sat May 17 23:11:06 2008 +0000
@@ -412,11 +412,11 @@
 		_switch_mode_errorstr = INVALID_STRING_ID;
 	}
 
-	virtual bool OnKeyPress(uint16 key, uint16 keycode)
+	virtual EventState OnKeyPress(uint16 key, uint16 keycode)
 	{
-		if (keycode != WKC_SPACE) return true;
+		if (keycode != WKC_SPACE) return ES_NOT_HANDLED;
 		delete this;
-		return false;
+		return ES_HANDLED;
 	}
 };
 
@@ -883,9 +883,9 @@
 	return false;
 }
 
-int QueryString::HandleEditBoxKey(Window *w, int wid, uint16 key, uint16 keycode, bool &cont)
+int QueryString::HandleEditBoxKey(Window *w, int wid, uint16 key, uint16 keycode, Window::EventState &state)
 {
-	cont = false;
+	state = Window::ES_HANDLED;
 
 	switch (keycode) {
 		case WKC_ESC: return 2;
@@ -913,7 +913,7 @@
 			if (IsValidChar(key, this->afilter)) {
 				if (InsertTextBufferChar(&this->text, key)) w->InvalidateWidget(wid);
 			} else { // key wasn't caught. Continue only if standard entry specified
-				cont = (this->afilter == CS_ALPHANUMERAL);
+				state = (this->afilter == CS_ALPHANUMERAL) ? Window::ES_HANDLED : Window::ES_NOT_HANDLED;
 			}
 	}
 
@@ -963,9 +963,9 @@
 	_cur_dpi = old_dpi;
 }
 
-int QueryStringBaseWindow::HandleEditBoxKey(int wid, uint16 key, uint16 keycode, bool &cont)
+int QueryStringBaseWindow::HandleEditBoxKey(int wid, uint16 key, uint16 keycode, EventState &state)
 {
-	return this->QueryString::HandleEditBoxKey(this, wid, key, keycode, cont);
+	return this->QueryString::HandleEditBoxKey(this, wid, key, keycode, state);
 }
 
 void QueryStringBaseWindow::HandleEditBox(int wid)
@@ -1038,15 +1038,15 @@
 		this->HandleEditBox(QUERY_STR_WIDGET_TEXT);
 	}
 
-	virtual bool OnKeyPress(uint16 key, uint16 keycode)
+	virtual EventState OnKeyPress(uint16 key, uint16 keycode)
 	{
-		bool cont;
-		switch (this->HandleEditBoxKey(QUERY_STR_WIDGET_TEXT, key, keycode, cont)) {
+		EventState state;
+		switch (this->HandleEditBoxKey(QUERY_STR_WIDGET_TEXT, key, keycode, state)) {
 			case 1: this->OnOk(); // Enter pressed, confirms change
 			/* FALL THROUGH */
 			case 2: delete this; break; // ESC pressed, closes window, abandons changes
 		}
-		return cont;
+		return state;
 	}
 
 	~QueryStringWindow()
@@ -1174,7 +1174,7 @@
 		}
 	}
 
-	virtual bool OnKeyPress(uint16 key, uint16 keycode)
+	virtual EventState OnKeyPress(uint16 key, uint16 keycode)
 	{
 		/* ESC closes the window, Enter confirms the action */
 		switch (keycode) {
@@ -1187,9 +1187,9 @@
 				/* Fallthrough */
 			case WKC_ESC:
 				delete this;
-				return false;
+				return ES_HANDLED;
 		}
-		return true;
+		return ES_NOT_HANDLED;
 	}
 };
 
@@ -1523,20 +1523,20 @@
 		}
 	}
 
-	virtual bool OnKeyPress(uint16 key, uint16 keycode)
+	virtual EventState OnKeyPress(uint16 key, uint16 keycode)
 	{
 		if (keycode == WKC_ESC) {
 			delete this;
-			return false;
+			return ES_HANDLED;
 		}
 
-		bool cont = true;
+		EventState state = ES_NOT_HANDLED;
 		if ((_saveload_mode == SLD_SAVE_GAME || _saveload_mode == SLD_SAVE_SCENARIO) &&
-				this->HandleEditBoxKey(10, key, keycode, cont) == 1) { // Press Enter
+				this->HandleEditBoxKey(10, key, keycode, state) == 1) { // Press Enter
 			this->HandleButtonClick(12);
 		}
 
-		return cont;
+		return state;
 	}
 
 	virtual void OnTimeout()
--- a/src/network/network_gui.cpp	Sat May 17 22:52:51 2008 +0000
+++ b/src/network/network_gui.cpp	Sat May 17 23:11:06 2008 +0000
@@ -543,9 +543,9 @@
 		this->SetDirty();
 	}
 
-	virtual bool OnKeyPress(uint16 key, uint16 keycode)
+	virtual EventState OnKeyPress(uint16 key, uint16 keycode)
 	{
-		bool cont = true;
+		EventState state = ES_NOT_HANDLED;
 		if (this->field != NGWW_PLAYER) {
 			if (this->server != NULL) {
 				if (keycode == WKC_DELETE) { // Press 'delete' to remove servers
@@ -554,10 +554,10 @@
 					this->server = NULL;
 				}
 			}
-			return cont;
+			return state;
 		}
 
-		if (this->HandleEditBoxKey(NGWW_PLAYER, keycode, key, cont) == 1) return cont; // enter pressed
+		if (this->HandleEditBoxKey(NGWW_PLAYER, keycode, key, state) == 1) return state; // enter pressed
 
 		/* The name is only allowed when it starts with a letter! */
 		if (StrEmpty(this->edit_str_buf) && this->edit_str_buf[0] != ' ') {
@@ -565,7 +565,7 @@
 		} else {
 			ttd_strlcpy(_network_player_name, "Player", lengthof(_network_player_name));
 		}
-		return cont;
+		return state;
 	}
 
 	virtual void OnQueryTextFinished(char *str)
@@ -892,16 +892,16 @@
 		if (this->field == NSSW_GAMENAME) this->HandleEditBox(NSSW_GAMENAME);
 	}
 
-	virtual bool OnKeyPress(uint16 key, uint16 keycode)
+	virtual EventState OnKeyPress(uint16 key, uint16 keycode)
 	{
-		bool cont = true;
+		EventState state = ES_NOT_HANDLED;
 		if (this->field == NSSW_GAMENAME) {
-			if (this->HandleEditBoxKey(NSSW_GAMENAME, key, keycode, cont) == 1) return cont; // enter pressed
+			if (this->HandleEditBoxKey(NSSW_GAMENAME, key, keycode, state) == 1) return state; // enter pressed
 
 			ttd_strlcpy(_network_server_name, this->text.buf, sizeof(_network_server_name));
 		}
 
-		return cont;
+		return state;
 	}
 
 	virtual void OnQueryTextFinished(char *str)
@@ -1878,21 +1878,21 @@
 		this->HandleEditBox(2);
 	}
 
-	virtual bool OnKeyPress(uint16 key, uint16 keycode)
+	virtual EventState OnKeyPress(uint16 key, uint16 keycode)
 	{
-		bool cont = true;
+		EventState state = ES_NOT_HANDLED;
 		if (keycode == WKC_TAB) {
 			ChatTabCompletion();
 		} else {
 			_chat_tab_completion_active = false;
-			switch (this->HandleEditBoxKey(2, key, keycode, cont)) {
+			switch (this->HandleEditBoxKey(2, key, keycode, state)) {
 				case 1: /* Return */
 					SendChat(this->text.buf, this->dtype, this->dest);
 				/* FALLTHROUGH */
 				case 2: /* Escape */ delete this; break;
 			}
 		}
-		return cont;
+		return state;
 	}
 };
 
@@ -1985,10 +1985,10 @@
 		this->HandleEditBox(4);
 	}
 
-	virtual bool OnKeyPress(uint16 key, uint16 keycode)
+	virtual EventState OnKeyPress(uint16 key, uint16 keycode)
 	{
-		bool cont;
-		switch (this->HandleEditBoxKey(4, key, keycode, cont)) {
+		EventState state;
+		switch (this->HandleEditBoxKey(4, key, keycode, state)) {
 			case 1: // Return
 				this->OnOk();
 				/* FALL THROUGH */
@@ -1997,7 +1997,7 @@
 				delete this;
 				break;
 		}
-		return cont;
+		return state;
 	}
 };
 
--- a/src/news_gui.cpp	Sat May 17 22:52:51 2008 +0000
+++ b/src/news_gui.cpp	Sat May 17 23:11:06 2008 +0000
@@ -308,14 +308,14 @@
 		}
 	}
 
-	virtual bool OnKeyPress(uint16 key, uint16 keycode)
+	virtual EventState OnKeyPress(uint16 key, uint16 keycode)
 	{
 		if (keycode == WKC_SPACE) {
 			/* Don't continue. */
 			delete this;
-			return false;
+			return ES_HANDLED;
 		}
-		return true;
+		return ES_NOT_HANDLED;
 	}
 
 	virtual void OnInvalidateData(int data)
--- a/src/order_gui.cpp	Sat May 17 22:52:51 2008 +0000
+++ b/src/order_gui.cpp	Sat May 17 23:11:06 2008 +0000
@@ -926,7 +926,7 @@
 		ResetObjectToPlace();
 	}
 
-	virtual bool OnKeyPress(uint16 key, uint16 keycode)
+	virtual EventState OnKeyPress(uint16 key, uint16 keycode)
 	{
 		static const KeyToEvent keytoevent[] = {
 			{'D', OrderClick_Skip},
@@ -939,15 +939,15 @@
 			//('?', OrderClick_Service},
 		};
 
-		if (this->vehicle->owner != _local_player) return true;
+		if (this->vehicle->owner != _local_player) return ES_NOT_HANDLED;
 
 		for (uint i = 0; i < lengthof(keytoevent); i++) {
 			if (keycode == keytoevent[i].keycode) {
 				keytoevent[i].proc(this, -1);
-				return false;
+				return ES_HANDLED;
 			}
 		}
-		return true;
+		return ES_NOT_HANDLED;
 	}
 
 	virtual void OnPlaceObject(Point pt, TileIndex tile)
--- a/src/querystring_gui.h	Sat May 17 22:52:51 2008 +0000
+++ b/src/querystring_gui.h	Sat May 17 23:11:06 2008 +0000
@@ -17,7 +17,7 @@
 
 	void DrawEditBox(Window *w, int wid);
 	void HandleEditBox(Window *w, int wid);
-	int HandleEditBoxKey(Window *w, int wid, uint16 key, uint16 keycode, bool &cont);
+	int HandleEditBoxKey(Window *w, int wid, uint16 key, uint16 keycode, Window::EventState &state);
 };
 
 struct QueryStringBaseWindow : public Window, public QueryString {
@@ -30,7 +30,7 @@
 
 	void DrawEditBox(int wid);
 	void HandleEditBox(int wid);
-	int HandleEditBoxKey(int wid, uint16 key, uint16 keycode, bool &cont);
+	int HandleEditBoxKey(int wid, uint16 key, uint16 keycode, EventState &state);
 };
 
 void ShowOnScreenKeyboard(QueryStringBaseWindow *parent, int button, int cancel, int ok);
--- a/src/signs_gui.cpp	Sat May 17 22:52:51 2008 +0000
+++ b/src/signs_gui.cpp	Sat May 17 23:11:06 2008 +0000
@@ -255,10 +255,10 @@
 		}
 	}
 
-	virtual bool OnKeyPress(uint16 key, uint16 keycode)
+	virtual EventState OnKeyPress(uint16 key, uint16 keycode)
 	{
-		bool cont = true;
-		switch (this->HandleEditBoxKey(QUERY_EDIT_SIGN_WIDGET_TEXT, key, keycode, cont)) {
+		EventState state = ES_NOT_HANDLED;
+		switch (this->HandleEditBoxKey(QUERY_EDIT_SIGN_WIDGET_TEXT, key, keycode, state)) {
 			case 1: // Enter pressed, confirms change
 				RenameSign(this->cur_sign, this->text.buf);
 				/* FALL THROUGH */
@@ -267,7 +267,7 @@
 				delete this;
 				break;
 		}
-		return cont;
+		return state;
 	}
 
 	virtual void OnMouseLoop()
--- a/src/window.cpp	Sat May 17 22:52:51 2008 +0000
+++ b/src/window.cpp	Sat May 17 23:11:06 2008 +0000
@@ -68,7 +68,7 @@
 	this->HandleWindowEvent(&e);
 }
 
-bool Window::OnKeyPress(uint16 key, uint16 keycode)
+Window::EventState Window::OnKeyPress(uint16 key, uint16 keycode)
 {
 	WindowEvent e;
 	e.event = WE_KEYPRESS;
@@ -77,17 +77,17 @@
 	e.we.keypress.cont    = true;
 	this->HandleWindowEvent(&e);
 
-	return e.we.keypress.cont;
+	return e.we.keypress.cont ? ES_NOT_HANDLED : ES_HANDLED;
 }
 
-bool Window::OnCTRLStateChange()
+Window::EventState Window::OnCTRLStateChange()
 {
 	WindowEvent e;
 	e.event = WE_CTRL_CHANGED;
 	e.we.ctrl.cont = true;
 	this->HandleWindowEvent(&e);
 
-	return e.we.ctrl.cont;
+	return e.we.ctrl.cont ? ES_NOT_HANDLED : ES_HANDLED;
 }
 
 void Window::OnClick(Point pt, int widget)
@@ -1802,8 +1802,7 @@
 				w->window_class != WC_COMPANY_PASSWORD_WINDOW) {
 			continue;
 		}
-		;
-		if (!w->OnKeyPress(key, keycode)) return;
+		if (w->OnKeyPress(key, keycode) == Window::ES_HANDLED) return;
 	}
 
 	Window *w = FindWindowById(WC_MAIN_TOOLBAR, 0);
@@ -1819,7 +1818,7 @@
 	/* Call the event, start with the uppermost window. */
 	for (Window* const *wz = _last_z_window; wz != _z_windows;) {
 		Window *w = *--wz;
-		if (!w->OnCTRLStateChange()) break;
+		if (w->OnCTRLStateChange() == Window::ES_HANDLED) return;
 	}
 }
 
--- a/src/window_gui.h	Sat May 17 22:52:51 2008 +0000
+++ b/src/window_gui.h	Sat May 17 23:11:06 2008 +0000
@@ -266,6 +266,11 @@
  * Data structure for an opened window
  */
 struct Window : ZeroedMemoryAllocator {
+	enum EventState {
+		ES_HANDLED,
+		ES_NOT_HANDLED,
+	};
+
 private:
 	WindowProc *wndproc;   ///< Event handler function for the window. Do not use directly, call HandleWindowEvent() instead.
 	void HandleWindowEvent(WindowEvent *e);
@@ -345,17 +350,17 @@
 	 * A key has been pressed.
 	 * @param key     the Unicode value of the key.
 	 * @param keycode the untranslated key code including shift state.
-	 * @return true if the key press has been handled and no other
+	 * @return ES_HANDLED if the key press has been handled and no other
 	 *         window should receive the event.
 	 */
-	virtual bool OnKeyPress(uint16 key, uint16 keycode);
+	virtual EventState OnKeyPress(uint16 key, uint16 keycode);
 
 	/**
 	 * The state of the control key has changed
-	 * @return true if the change has been handled and no other
+	 * @return ES_HANDLED if the change has been handled and no other
 	 *         window should receive the event.
 	 */
-	virtual bool OnCTRLStateChange();
+	virtual EventState OnCTRLStateChange();
 
 
 	/**