diff --git a/src/window.cpp b/src/window.cpp index aed4065b27..f3868ccb97 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -249,11 +249,11 @@ static void StartWindowSizing(Window *w, bool to_left); */ 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); + bool focused_widget_changed = false; /* If clicked on a window that previously did dot have focus */ if (_focused_window != w && (w->desc_flags & WDF_NO_FOCUS) == 0 && // Don't lose focus to toolbars @@ -286,12 +286,7 @@ static void DispatchLeftClickEvent(Window *w, int x, int y, bool double_click) DeleteWindowById(WC_OSK, 0); } - if (w->focused_widget != wi) { - /* Repaint the widget that lost focus. A focused edit box may else leave the caret on the screen. */ - if (w->focused_widget != NULL) w->InvalidateWidget(w->focused_widget - w->widget); - focused_widget_changed = true; - w->focused_widget = wi; - } + focused_widget_changed = w->SetFocusedWidget(widget); } if (wi->type & WWB_PUSHBUTTON) { diff --git a/src/window_gui.h b/src/window_gui.h index b035a9cfcf..6f819cf286 100644 --- a/src/window_gui.h +++ b/src/window_gui.h @@ -266,16 +266,20 @@ public: /** * Set focus within this window to the given widget. The function however doesn't change which window has focus. * @param widget_index Index of the widget in the window to set the focus to. + * @return Focus has changed. */ - inline void SetFocusedWidget(byte widget_index) + inline bool SetFocusedWidget(byte widget_index) { - if (widget_index < this->widget_count) { - /* Repaint the widget that lost focus. A focused edit box may else leave the caret on the screen. */ - if (this->focused_widget != NULL && this->focused_widget - this->widget != widget_index) { - this->InvalidateWidget(this->focused_widget - this->widget); - } - this->focused_widget = &this->widget[widget_index]; + if (widget_index >= this->widget_count || this->widget + widget_index == this->focused_widget) { + return false; } + + if (this->focused_widget != NULL) { + /* Repaint the widget that lost focus. A focused edit box may else leave the caret on the screen. */ + this->InvalidateWidget(this->focused_widget - this->widget); + } + this->focused_widget = &this->widget[widget_index]; + return true; } /**