(svn r14534) -Codechange [FS#2382]: Enumify magic return values of HandleEditBox function (Zuu)

This commit is contained in:
glx 2008-10-25 19:59:11 +00:00
parent 7df85e8a45
commit a6bfd7f15b
5 changed files with 32 additions and 18 deletions

View File

@ -960,14 +960,14 @@ bool HandleCaret(Textbuf *tb)
return false; return false;
} }
int QueryString::HandleEditBoxKey(Window *w, int wid, uint16 key, uint16 keycode, Window::EventState &state) HandleEditBoxResult QueryString::HandleEditBoxKey(Window *w, int wid, uint16 key, uint16 keycode, Window::EventState &state)
{ {
state = Window::ES_HANDLED; state = Window::ES_HANDLED;
switch (keycode) { switch (keycode) {
case WKC_ESC: return 2; case WKC_ESC: return HEBR_CANCEL;
case WKC_RETURN: case WKC_NUM_ENTER: return 1; case WKC_RETURN: case WKC_NUM_ENTER: return HEBR_CONFIRM;
case (WKC_CTRL | 'V'): case (WKC_CTRL | 'V'):
if (InsertTextBufferClipboard(&this->text)) w->InvalidateWidget(wid); if (InsertTextBufferClipboard(&this->text)) w->InvalidateWidget(wid);
@ -994,7 +994,7 @@ int QueryString::HandleEditBoxKey(Window *w, int wid, uint16 key, uint16 keycode
} }
} }
return 0; return HEBR_EDITING;
} }
void QueryString::HandleEditBox(Window *w, int wid) void QueryString::HandleEditBox(Window *w, int wid)
@ -1124,13 +1124,13 @@ struct QueryStringWindow : public QueryStringBaseWindow
EventState state; EventState state;
switch (this->HandleEditBoxKey(QUERY_STR_WIDGET_TEXT, key, keycode, state)) { switch (this->HandleEditBoxKey(QUERY_STR_WIDGET_TEXT, key, keycode, state)) {
default: NOT_REACHED(); default: NOT_REACHED();
case 0: { case HEBR_EDITING: {
Window *osk = FindWindowById(WC_OSK, 0); Window *osk = FindWindowById(WC_OSK, 0);
if (osk != NULL && osk->parent == this) osk->OnInvalidateData(); if (osk != NULL && osk->parent == this) osk->OnInvalidateData();
} break; } break;
case 1: this->OnOk(); // Enter pressed, confirms change case HEBR_CONFIRM: this->OnOk();
/* FALL THROUGH */ /* FALL THROUGH */
case 2: delete this; break; // ESC pressed, closes window, abandons changes case HEBR_CANCEL: delete this; break; // close window, abandon changes
} }
return state; return state;
} }
@ -1624,7 +1624,7 @@ struct SaveLoadWindow : public QueryStringBaseWindow {
EventState state = ES_NOT_HANDLED; EventState state = ES_NOT_HANDLED;
if ((_saveload_mode == SLD_SAVE_GAME || _saveload_mode == SLD_SAVE_SCENARIO) && if ((_saveload_mode == SLD_SAVE_GAME || _saveload_mode == SLD_SAVE_SCENARIO) &&
this->HandleEditBoxKey(10, key, keycode, state) == 1) { // Press Enter this->HandleEditBoxKey(10, key, keycode, state) == HEBR_CONFIRM) {
this->HandleButtonClick(12); this->HandleButtonClick(12);
} }

View File

@ -468,14 +468,14 @@ struct NetworkChatWindow : public QueryStringBaseWindow {
_chat_tab_completion_active = false; _chat_tab_completion_active = false;
switch (this->HandleEditBoxKey(2, key, keycode, state)) { switch (this->HandleEditBoxKey(2, key, keycode, state)) {
default: NOT_REACHED(); default: NOT_REACHED();
case 0: { case HEBR_EDITING: {
Window *osk = FindWindowById(WC_OSK, 0); Window *osk = FindWindowById(WC_OSK, 0);
if (osk != NULL && osk->parent == this) osk->OnInvalidateData(); if (osk != NULL && osk->parent == this) osk->OnInvalidateData();
} break; } break;
case 1: /* Return */ case HEBR_CONFIRM:
SendChat(this->text.buf, this->dtype, this->dest); SendChat(this->text.buf, this->dtype, this->dest);
/* FALLTHROUGH */ /* FALLTHROUGH */
case 2: /* Escape */ delete this; break; case HEBR_CANCEL: delete this; break;
} }
} }
return state; return state;

View File

@ -668,7 +668,7 @@ public:
return state; return state;
} }
if (this->HandleEditBoxKey(NGWW_CLIENT, key, keycode, state) == 1) return state; // enter pressed if (this->HandleEditBoxKey(NGWW_CLIENT, key, keycode, state) == HEBR_CONFIRM) return state;
/* The name is only allowed when it starts with a letter! */ /* The name is only allowed when it starts with a letter! */
if (!StrEmpty(this->edit_str_buf) && this->edit_str_buf[0] != ' ') { if (!StrEmpty(this->edit_str_buf) && this->edit_str_buf[0] != ' ') {
@ -1047,7 +1047,7 @@ struct NetworkStartServerWindow : public QueryStringBaseWindow {
{ {
EventState state = ES_NOT_HANDLED; EventState state = ES_NOT_HANDLED;
if (this->field == NSSW_GAMENAME) { if (this->field == NSSW_GAMENAME) {
if (this->HandleEditBoxKey(NSSW_GAMENAME, key, keycode, state) == 1) return state; // enter pressed if (this->HandleEditBoxKey(NSSW_GAMENAME, key, keycode, state) == HEBR_CONFIRM) return state;
ttd_strlcpy(_settings_client.network.server_name, this->text.buf, sizeof(_settings_client.network.server_name)); ttd_strlcpy(_settings_client.network.server_name, this->text.buf, sizeof(_settings_client.network.server_name));
} }
@ -1906,11 +1906,13 @@ struct NetworkCompanyPasswordWindow : public QueryStringBaseWindow {
{ {
EventState state; EventState state;
switch (this->HandleEditBoxKey(4, key, keycode, state)) { switch (this->HandleEditBoxKey(4, key, keycode, state)) {
case 1: // Return default: break;
case HEBR_CONFIRM:
this->OnOk(); this->OnOk();
/* FALL THROUGH */ /* FALL THROUGH */
case 2: // Escape case HEBR_CANCEL:
delete this; delete this;
break; break;
} }

View File

@ -8,6 +8,16 @@
#include "textbuf_gui.h" #include "textbuf_gui.h"
#include "window_gui.h" #include "window_gui.h"
/**
* Return values for HandleEditBoxKey
*/
enum HandleEditBoxResult
{
HEBR_EDITING = 0, // Other key pressed.
HEBR_CONFIRM, // Return or enter key pressed.
HEBR_CANCEL, // Escape key pressed.
};
/** /**
* Data stored about a string that can be modified in the GUI * Data stored about a string that can be modified in the GUI
*/ */
@ -35,7 +45,7 @@ struct QueryString {
void DrawEditBox(Window *w, int wid); void DrawEditBox(Window *w, int wid);
void HandleEditBox(Window *w, int wid); void HandleEditBox(Window *w, int wid);
int HandleEditBoxKey(Window *w, int wid, uint16 key, uint16 keycode, Window::EventState &state); HandleEditBoxResult HandleEditBoxKey(Window *w, int wid, uint16 key, uint16 keycode, Window::EventState &state);
}; };
struct QueryStringBaseWindow : public Window, public QueryString { struct QueryStringBaseWindow : public Window, public QueryString {

View File

@ -309,11 +309,13 @@ struct SignWindow : QueryStringBaseWindow, SignList {
{ {
EventState state = ES_NOT_HANDLED; EventState state = ES_NOT_HANDLED;
switch (this->HandleEditBoxKey(QUERY_EDIT_SIGN_WIDGET_TEXT, key, keycode, state)) { switch (this->HandleEditBoxKey(QUERY_EDIT_SIGN_WIDGET_TEXT, key, keycode, state)) {
case 1: // Enter pressed, confirms change default: break;
case HEBR_CONFIRM:
if (RenameSign(this->cur_sign, this->text.buf)) break; if (RenameSign(this->cur_sign, this->text.buf)) break;
/* FALL THROUGH */ /* FALL THROUGH */
case 2: // ESC pressed, closes window, abandons changes case HEBR_CANCEL: // close window, abandon changes
delete this; delete this;
break; break;
} }