Index: src/console_gui.cpp =================================================================== --- src/console_gui.cpp (revision 14916) +++ src/console_gui.cpp (working copy) @@ -153,7 +153,6 @@ IConsoleWindow(const WindowDesc *desc) : Window(desc) { _iconsole_mode = ICONSOLE_OPENED; - _no_scroll++; // override cursor arrows; the gamefield will not scroll this->height = _screen.height / 3; this->width = _screen.width; @@ -162,7 +161,6 @@ ~IConsoleWindow() { _iconsole_mode = ICONSOLE_CLOSED; - _no_scroll--; } virtual void OnPaint() @@ -183,7 +181,7 @@ DoDrawString(_iconsole_cmdline.buf, 10 + delta, this->height - ICON_LINE_HEIGHT, CC_COMMAND); - if (_iconsole_cmdline.caret) { + if (_focused_window == this && _iconsole_cmdline.caret) { DoDrawString("_", 10 + delta + _iconsole_cmdline.caretxoffs, this->height - ICON_LINE_HEIGHT, TC_WHITE); } } Index: src/genworld_gui.cpp =================================================================== --- src/genworld_gui.cpp (revision 14916) +++ src/genworld_gui.cpp (working copy) @@ -283,6 +283,7 @@ /* snprintf() always outputs trailing '\0', so whole buffer can be used */ snprintf(this->edit_str_buf, this->edit_str_size, "%u", _settings_newgame.game_creation.generation_seed); InitializeTextBuffer(&this->text, this->edit_str_buf, this->edit_str_size, 120); + this->SetFocusedWidget(GLAND_RANDOM_EDITBOX); this->caption = STR_NULL; this->afilter = CS_NUMERAL; @@ -449,6 +450,7 @@ _settings_newgame.game_creation.snow_line_height = Clamp(_settings_newgame.game_creation.snow_line_height + widget - GLAND_SNOW_LEVEL_TEXT, 2, MAX_SNOWLINE_HEIGHT); } + _left_button_clicked = false; break; Index: src/misc_gui.cpp =================================================================== --- src/misc_gui.cpp (revision 14916) +++ src/misc_gui.cpp (working copy) @@ -965,8 +965,28 @@ return false; } +bool QueryString::HasEditBoxFocus(Window *w, int wid) const +{ + if (w->window_class == WC_OSK) { + if (_focused_window == w->parent && + w->parent->focused_widget && + w->parent->focused_widget->type == WWT_EDITBOX) { + return true; + } + } + if (w->IsWidgetGlobalyFocused(wid)) { + return true; + } + + return false; +} + HandleEditBoxResult QueryString::HandleEditBoxKey(Window *w, int wid, uint16 key, uint16 keycode, Window::EventState &state) { + if (!QueryString::HasEditBoxFocus(w, wid)) { + return HEBR_NOT_FOCUSED; + } + state = Window::ES_HANDLED; switch (keycode) { @@ -1004,7 +1024,7 @@ void QueryString::HandleEditBox(Window *w, int wid) { - if (HandleCaret(&this->text)) w->InvalidateWidget(wid); + if (HasEditBoxFocus(w, wid) && HandleCaret(&this->text)) w->InvalidateWidget(wid); } void QueryString::DrawEditBox(Window *w, int wid) @@ -1040,7 +1060,7 @@ if (tb->caretxoffs + delta < 0) delta = -tb->caretxoffs; DoDrawString(tb->buf, delta, 0, TC_YELLOW); - if (tb->caret) DoDrawString("_", tb->caretxoffs + delta, 0, TC_WHITE); + if (HasEditBoxFocus(w, wid) && tb->caret) DoDrawString("_", tb->caretxoffs + delta, 0, TC_WHITE); _cur_dpi = old_dpi; } @@ -1078,6 +1098,7 @@ QueryStringWindow(uint16 size, const WindowDesc *desc, Window *parent) : QueryStringBaseWindow(size, desc) { this->parent = parent; + this->SetFocusedWidget(QUERY_STR_WIDGET_TEXT); this->FindWindowPlacementAndResize(desc); } @@ -1136,6 +1157,7 @@ case HEBR_CONFIRM: this->OnOk(); /* FALL THROUGH */ case HEBR_CANCEL: delete this; break; // close window, abandon changes + case HEBR_NOT_FOCUSED: break; } return state; } @@ -1186,7 +1208,6 @@ void ShowQueryString(StringID str, StringID caption, uint maxsize, uint maxwidth, Window *parent, CharSetFilter afilter, QueryStringFlags flags) { DeleteWindowById(WC_QUERY_STRING, 0); - DeleteWindowById(WC_SAVELOAD, 0); QueryStringWindow *w = new QueryStringWindow(maxsize, &_query_string_desc, parent); @@ -1492,6 +1513,11 @@ strecpy(o_dir.name, _personal_dir, lastof(o_dir.name)); } + /* Focus the edit box by default in the save windows */ + if (_saveload_mode == SLD_SAVE_GAME || _saveload_mode == SLD_SAVE_SCENARIO) { + this->SetFocusedWidget(10); + } + this->vscroll.cap = 10; this->resize.step_width = 2; this->resize.step_height = 10; @@ -1699,7 +1725,6 @@ void ShowSaveLoadDialog(SaveLoadDialogMode mode) { - DeleteWindowById(WC_QUERY_STRING, 0); DeleteWindowById(WC_SAVELOAD, 0); const WindowDesc *sld; Index: src/tar_type.h =================================================================== --- src/tar_type.h (revision 14916) +++ src/tar_type.h (working copy) @@ -1,10 +1,10 @@ /* $Id$ */ -/** @file tar_type.h Structs, typedefs and macros used for TAR file handling. */ - #ifndef TAR_TYPE_H #define TAR_TYPE_H +/** @file tar_type.h Structs, typedefs and macros used for TAR file handling. */ + #include #include Index: src/querystring_gui.h =================================================================== --- src/querystring_gui.h (revision 14916) +++ src/querystring_gui.h (working copy) @@ -16,6 +16,7 @@ HEBR_EDITING = 0, // Other key pressed. HEBR_CONFIRM, // Return or enter key pressed. HEBR_CANCEL, // Escape key pressed. + HEBR_NOT_FOCUSED, // Edit box widget not focused. }; /** @@ -43,6 +44,7 @@ free((void*)this->orig); } + bool HasEditBoxFocus(Window *w, int wid) const; void DrawEditBox(Window *w, int wid); void HandleEditBox(Window *w, int wid); HandleEditBoxResult HandleEditBoxKey(Window *w, int wid, uint16 key, uint16 keycode, Window::EventState &state); Index: src/window_gui.h =================================================================== --- src/window_gui.h (revision 14916) +++ src/window_gui.h (working copy) @@ -229,6 +229,7 @@ Widget *widget; ///< Widgets of the window uint widget_count; ///< Number of widgets of the window uint32 desc_flags; ///< Window/widgets default flags setting, @see WindowDefaultFlag + const Widget *focused_widget; ///< Currently focused widget or 0, if no widget have focus Window *parent; ///< Parent window Window *z_front; ///< The window in front of us in z-order @@ -244,6 +245,9 @@ void HideWidget(byte widget_index); void ShowWidget(byte widget_index); bool IsWidgetHidden(byte widget_index) const; + void SetFocusedWidget(byte widget_index); + bool IsWidgetGlobalyFocused(byte widget_index) const; + bool IsWidgetFocused(byte widget_index) const; void SetWidgetLoweredState(byte widget_index, bool lowered_stat); void ToggleWidgetLoweredState(byte widget_index); void LowerWidget(byte widget_index); @@ -273,8 +277,17 @@ */ virtual void OnPaint() {} + /** + * Called when window gains focus + */ + virtual void OnFocus() {} /** + * Called when window looses focus + */ + virtual void OnFocusLost() {} + + /** * A key has been pressed. * @param key the Unicode value of the key. * @param keycode the untranslated key code including shift state. @@ -546,6 +559,7 @@ /* window.cpp */ extern Window *_z_front_window; extern Window *_z_back_window; +extern Window *_focused_window; /** Iterate over all windows */ #define FOR_ALL_WINDOWS_FROM_BACK_FROM(w, start) for (w = start; w != NULL; w = w->z_front) if (w->window_class != WC_INVALID) @@ -553,12 +567,6 @@ #define FOR_ALL_WINDOWS_FROM_BACK(w) FOR_ALL_WINDOWS_FROM_BACK_FROM(w, _z_back_window) #define FOR_ALL_WINDOWS_FROM_FRONT(w) FOR_ALL_WINDOWS_FROM_FRONT_FROM(w, _z_front_window) -/** - * Disable scrolling of the main viewport when an input-window is active. - * This contains the count of windows with a textbox in them. - */ -extern byte _no_scroll; - extern Point _cursorpos_drag_start; extern int _scrollbar_start_pos; @@ -578,6 +586,10 @@ Window *GetCallbackWnd(); +void SetFocusedWindow(Window *w); +const Widget *GetGlobalyFocusedWidget(); +bool EditBoxInGlobalFocus(); + void ScrollbarClickHandler(Window *w, const Widget *wi, int x, int y); void ResizeButtons(Window *w, byte left, byte right); @@ -673,7 +685,40 @@ return HasBit(this->widget[widget_index].display_flags, WIDG_HIDDEN); } + /** + * Set focus within this window to given widget. The function however don't + * change which window that has focus. + * @param widget_index : index of the widget in the window to set focus to + */ +inline void Window::SetFocusedWidget(byte widget_index) +{ + if (widget_index < this->widget_count) { + this->focused_widget = &this->widget[widget_index]; + } +} + /** + * Check if given widget has user input focus. This means that both the window + * has focus and that the given widget has focus within the window. + * @param widget_index : index of the widget in the window to check + * @return true if given widget is the focused window in this window and this window has focus + */ +inline bool Window::IsWidgetGlobalyFocused(byte widget_index) const +{ + return _focused_window == this && IsWidgetFocused(widget_index); +} + +/** + * Check if given widget is focused within this window + * @param widget_index : index of the widget in the window to check + * @return true if given widget is the focused window in this window + */ +inline bool Window::IsWidgetFocused(byte widget_index) const +{ + return this->focused_widget == &this->widget[widget_index]; +} + +/** * Sets the lowered/raised status of a widget. * @param widget_index index of this widget in the window * @param lowered_stat status to use ie: lowered = true, raised = false Index: src/newgrf_text.h =================================================================== --- src/newgrf_text.h (revision 14916) +++ src/newgrf_text.h (working copy) @@ -1,10 +1,11 @@ /* $Id$ */ - -/** @file newgrf_text.h Header of Action 04 "universal holder" structure and functions */ - #ifndef NEWGRF_TEXT_H #define NEWGRF_TEXT_H +/** @file newgrf_text.h + * Header of Action 04 "universal holder" structure and functions + */ + StringID AddGRFString(uint32 grfid, uint16 stringid, byte langid, bool new_scheme, const char *text_to_add, StringID def_string); StringID GetGRFStringID(uint32 grfid, uint16 stringid); const char *GetGRFStringPtr(uint16 stringid); Index: src/window.cpp =================================================================== --- src/window.cpp (revision 14916) +++ src/window.cpp (working copy) @@ -36,7 +36,13 @@ /** List of windows opened at the screen sorted from the back. */ Window *_z_back_window = NULL; -byte _no_scroll; +/* + * Window that currently have focus. - The main purpose is to generate + * FocusLost events, not to give next window in z-order focus when a + * window is closed. + */ +Window *_focused_window; + Point _cursorpos_drag_start; int _scrollbar_start_pos; @@ -48,8 +54,53 @@ byte _special_mouse_mode; +/** + * Gets the globaly focused widget. Which is the focused widget of the focused window. + * @return A pointer to the globaly focused Widget, or NULL if there is no globaly focused widget. + */ +void SetFocusedWindow(Window *w) +{ + if (_focused_window == w) { + return; + } + /* Invalidate focused widget */ + if (_focused_window && _focused_window->focused_widget) { + uint focused_widget_id = _focused_window->focused_widget - _focused_window->widget; + _focused_window->InvalidateWidget(focused_widget_id); + } + + /* Remember which window was previously focused */ + Window *old_focused = _focused_window; + _focused_window = w; + + /* So we can inform it that it lost focus */ + if (old_focused) old_focused->OnFocusLost(); + if (_focused_window) _focused_window->OnFocus(); +} + /** + * Check if the console is focused + * @return returns true if the focused window is a console, else false + */ +const Widget *GetGlobalyFocusedWidget() +{ + return _focused_window != NULL ? _focused_window->focused_widget : NULL; +} + +/** + * Check if an edit box is in global focus. That is if focused window + * has a edit box as focused widget, or if a console is focused. + * @return returns true if an edit box is in global focus or if the focused window is a console, else false + */ +bool EditBoxInGlobalFocus() +{ + const Widget *wi = GetGlobalyFocusedWidget(); + + return wi != NULL && wi->type == WWT_EDITBOX; +} + +/** * Sets the enabled/disabled status of a list of widgets. * By default, widgets are enabled. * On certain conditions, they have to be disabled. @@ -161,9 +212,30 @@ */ static void DispatchLeftClickEvent(Window *w, int x, int y, bool double_click) { + bool focused_widget_changed = false; int widget = 0; if (w->desc_flags & WDF_DEF_WIDGET) { widget = GetWidgetFromPos(w, x, y); + + /* If clicked on a window that previously did dot have focus */ + if (_focused_window != w && + w->window_class != WC_MAIN_TOOLBAR && // So users can open a window using the main toolbar without loosing focus on a text edit box + w->window_class != WC_STATUS_BAR && // Same, but for the status bar at the bottom + !(w->desc_flags & WDF_STD_BTN && widget == 0)) { // Don't change focused window if 'X' (close button) was clicked + focused_widget_changed = true; + if (_focused_window) { + _focused_window->OnFocusLost(); + + /* The window that lost focus may have had opened a OSK, window so close it, unless the user has clicked on the OSK window. */ + if (w->window_class != WC_OSK) { + Window *osk_window = FindWindowById(WC_OSK, 0); + delete osk_window; + } + } + SetFocusedWindow(w); + w->OnFocus(); + } + if (widget < 0) return; // exit if clicked outside of widgets /* don't allow any interaction if the button has been disabled */ @@ -171,6 +243,21 @@ const Widget *wi = &w->widget[widget]; + /* Clicked on a widget that is not disabled. + * So unless the clicked widget is the caption bar, change focus to this widget */ + if (wi->type != WWT_CAPTION) { + /* Close the OSK window if a edit box loses focus */ + if (w->focused_widget && w->focused_widget->type == WWT_EDITBOX && // An edit box was previously selected + w->focused_widget != wi && // and focus is going to change + w->window_class != WC_OSK) { // and it is not the OSK window + Window *osk_window = FindWindowById(WC_OSK, 0); + delete osk_window; + } + + if (w->focused_widget != wi) focused_widget_changed = true; + w->focused_widget = wi; + } + if (wi->type & WWB_MASK) { /* special widget handling for buttons*/ switch (wi->type) { @@ -182,7 +269,7 @@ } } else if (wi->type == WWT_SCROLLBAR || wi->type == WWT_SCROLL2BAR || wi->type == WWT_HSCROLLBAR) { ScrollbarClickHandler(w, wi, x, y); - } else if (wi->type == WWT_EDITBOX) { + } else if (wi->type == WWT_EDITBOX && !focused_widget_changed) { // Only open the OSK window if clicking on an allready focused edit box /* Open the OSK window if clicked on an edit box */ QueryStringBaseWindow *qs = dynamic_cast(w); if (qs != NULL) { @@ -428,19 +515,15 @@ /* Prevent Mouseover() from resetting mouse-over coordinates on a non-existing window */ if (_mouseover_last_w == this) _mouseover_last_w = NULL; + /* Make sure we don't try to access this window as the focused window when it don't exist anymore. */ + if (_focused_window == this) _focused_window = NULL; + this->DeleteChildWindows(); if (this->viewport != NULL) DeleteWindowViewport(this); this->SetDirty(); - if (this->widget != NULL) { - for (const Widget *wi = this->widget; wi->type != WWT_LAST; wi++) { - if (wi->type == WWT_EDITBOX) _no_scroll--; - } - free(this->widget); - } - this->window_class = WC_INVALID; } @@ -643,10 +726,6 @@ w->widget = MallocT(index); memcpy(w->widget, widget, sizeof(*w->widget) * index); w->widget_count = index - 1; - - for (const Widget *wi = w->widget; wi->type != WWT_LAST; wi++) { - if (wi->type == WWT_EDITBOX) _no_scroll++; - } } else { w->widget = NULL; w->widget_count = 0; @@ -679,12 +758,17 @@ this->width = min_width; this->height = min_height; AssignWidgetToWindow(this, widget); + this->focused_widget = 0; this->resize.width = min_width; this->resize.height = min_height; this->resize.step_width = 1; this->resize.step_height = 1; this->window_number = window_number; + /* Give focus to the opened winodw unless it is the OSK window or a text box + * of focused window has focus (so we don't interupt typing). */ + if (this->window_class != WC_OSK && !EditBoxInGlobalFocus()) SetFocusedWindow(this); + /* Hacky way of specifying always-on-top windows. These windows are * always above other windows because they are moved below them. * status-bar is above news-window because it has been created earlier. @@ -1069,8 +1153,8 @@ _z_back_window = NULL; _z_front_window = NULL; + _focused_window = NULL; _mouseover_last_w = NULL; - _no_scroll = 0; _scrolling_viewport = 0; } @@ -1617,11 +1701,6 @@ */ void HandleKeypress(uint32 raw_key) { - /* Stores if a window with a textfield for typing is open - * If this is the case, keypress events are only passed to windows with text fields and - * to thein this main toolbar. */ - bool query_open = false; - /* * During the generation of the world, there might be * another thread that is currently building for example @@ -1651,29 +1730,20 @@ */ if (key == 0 && keycode == 0) return; - /* check if we have a query string window open before allowing hotkeys */ - if (FindWindowById(WC_QUERY_STRING, 0) != NULL || - FindWindowById(WC_SEND_NETWORK_MSG, 0) != NULL || - FindWindowById(WC_GENERATE_LANDSCAPE, 0) != NULL || - FindWindowById(WC_CONSOLE, 0) != NULL || - FindWindowById(WC_SAVELOAD, 0) != NULL || - FindWindowById(WC_COMPANY_PASSWORD_WINDOW, 0) != NULL) { - query_open = true; + /* Check if the focused window has a focused editbox */ + if (_focused_window != NULL && + (_focused_window->window_class == WC_CONSOLE || // The console does not have an edit box so a special case is needed + (_focused_window->focused_widget != NULL && + _focused_window->focused_widget->type == WWT_EDITBOX))) + { + /* All input will in this case go to the focused window */ + _focused_window->OnKeyPress(key, keycode); + return; } /* Call the event, start with the uppermost window. */ Window *w; FOR_ALL_WINDOWS_FROM_FRONT(w) { - /* if a query window is open, only call the event for certain window types */ - if (query_open && - w->window_class != WC_QUERY_STRING && - w->window_class != WC_SEND_NETWORK_MSG && - w->window_class != WC_GENERATE_LANDSCAPE && - w->window_class != WC_CONSOLE && - w->window_class != WC_SAVELOAD && - w->window_class != WC_COMPANY_PASSWORD_WINDOW) { - continue; - } if (w->OnKeyPress(key, keycode) == Window::ES_HANDLED) return; } @@ -1789,7 +1859,11 @@ static void HandleKeyScrolling() { - if (_dirkeys && !_no_scroll) { + /* + * Check that any of the dirkeys is pressed and that the focused window + * dont has an edit-box as focused widget. + */ + if (_dirkeys && !(_focused_window != NULL && _focused_window->focused_widget != NULL && _focused_window->focused_widget->type == WWT_EDITBOX)) { int factor = _shift_pressed ? 50 : 10; ScrollMainViewport(scrollamt[_dirkeys][0] * factor, scrollamt[_dirkeys][1] * factor); } Index: src/network/network_chat_gui.cpp =================================================================== --- src/network/network_chat_gui.cpp (revision 14916) +++ src/network/network_chat_gui.cpp (working copy) @@ -275,6 +275,7 @@ this->dest = dest; this->afilter = CS_ALPHANUMERAL; InitializeTextBuffer(&this->text, this->edit_str_buf, this->edit_str_size, 0); + this->SetFocusedWidget(2); InvalidateWindowData(WC_NEWS_WINDOW, 0, this->height); @@ -477,6 +478,7 @@ SendChat(this->text.buf, this->dtype, this->dest); /* FALLTHROUGH */ case HEBR_CANCEL: delete this; break; + case HEBR_NOT_FOCUSED: break; } } return state; Index: src/network/network_gui.cpp =================================================================== --- src/network/network_gui.cpp (revision 14916) +++ src/network/network_gui.cpp (working copy) @@ -315,6 +315,7 @@ ttd_strlcpy(this->edit_str_buf, _settings_client.network.client_name, this->edit_str_size); this->afilter = CS_ALPHANUMERAL; InitializeTextBuffer(&this->text, this->edit_str_buf, this->edit_str_size, 120); + this->SetFocusedWidget(NGWW_CLIENT); UpdateNetworkGameWindow(true); @@ -859,6 +860,7 @@ this->afilter = CS_ALPHANUMERAL; InitializeTextBuffer(&this->text, this->edit_str_buf, this->edit_str_size, 160); + this->SetFocusedWidget(NSSW_GAMENAME); this->field = NSSW_GAMENAME; @@ -1862,6 +1864,7 @@ this->parent = parent; this->afilter = CS_ALPHANUMERAL; InitializeTextBuffer(&this->text, this->edit_str_buf, this->edit_str_size, 0); + this->SetFocusedWidget(NCPWW_PASSWORD); this->FindWindowPlacementAndResize(desc); } Index: src/osk_gui.cpp =================================================================== --- src/osk_gui.cpp (revision 14916) +++ src/osk_gui.cpp (working copy) @@ -137,6 +137,21 @@ bool delete_this = false; switch (widget) { + case OSK_WIDGET_TEXT: + /* Find the edit box of the parent window and give focus to that */ + for (uint i = 0; i < this->parent->widget_count; i++) { + Widget &wi = this->parent->widget[i]; + if (wi.type == WWT_EDITBOX) { + this->parent->focused_widget = &wi; + break; + } + } + + /* Give focus to parent window */ + SetFocusedWindow(this->parent); + + break; + case OSK_WIDGET_BACKSPACE: if (DeleteTextBufferChar(&this->qs->text, WKC_BACKSPACE)) this->InvalidateWidget(OSK_WIDGET_TEXT); break; Index: src/signs_gui.cpp =================================================================== --- src/signs_gui.cpp (revision 14916) +++ src/signs_gui.cpp (working copy) @@ -203,6 +203,7 @@ this->LowerWidget(QUERY_EDIT_SIGN_WIDGET_TEXT); UpdateSignEditWindow(si); + this->SetFocusedWidget(QUERY_EDIT_SIGN_WIDGET_TEXT); this->FindWindowPlacementAndResize(desc); } @@ -223,6 +224,7 @@ InitializeTextBuffer(&this->text, this->edit_str_buf, this->edit_str_size, MAX_LENGTH_SIGN_NAME_PIXELS); this->InvalidateWidget(QUERY_EDIT_SIGN_WIDGET_TEXT); + this->SetFocusedWidget(QUERY_EDIT_SIGN_WIDGET_TEXT); } /** @@ -354,9 +356,8 @@ void ShowRenameSignWindow(const Sign *si) { - /* Delete all other edit windows and the save window */ + /* Delete all other edit windows */ DeleteWindowById(WC_QUERY_STRING, 0); - DeleteWindowById(WC_SAVELOAD, 0); new SignWindow(&_query_sign_edit_desc, si); }