From e5cd6ce45c6d71f33f7de82d25dc8292977cfa2d Mon Sep 17 00:00:00 2001 From: pelya Date: Fri, 31 Mar 2017 03:38:06 +0300 Subject: [PATCH] Clicking widgets is broken, the focus changes at random --- src/widget.cpp | 11 ++++++++--- src/window.cpp | 21 +++++---------------- src/window_gui.h | 2 +- 3 files changed, 14 insertions(+), 20 deletions(-) diff --git a/src/widget.cpp b/src/widget.cpp index e0f235f7c4..3039260a89 100644 --- a/src/widget.cpp +++ b/src/widget.cpp @@ -86,12 +86,14 @@ static Point HandleScrollbarHittest(const Scrollbar *sb, int top, int bottom, bo * @param ma Maximum coordinate of the scroll bar. * @param x The X coordinate of the mouse click. * @param y The Y coordinate of the mouse click. + * @return true if scrollbar slider is pressed */ -static void ScrollbarClickPositioning(Window *w, NWidgetScrollbar *sb, int x, int y, int mi, int ma) +static bool ScrollbarClickPositioning(Window *w, NWidgetScrollbar *sb, int x, int y, int mi, int ma) { int pos; int button_size; bool rtl = false; + bool sliderPressed = false; if (sb->type == NWID_HSCROLLBAR) { pos = x; @@ -132,10 +134,12 @@ static void ScrollbarClickPositioning(Window *w, NWidgetScrollbar *sb, int x, in _scrollbar_size -= button_size; w->scrolling_scrollbar = sb->index; _cursorpos_drag_start = _cursor.pos; + sliderPressed = true; } } w->SetDirty(); + return sliderPressed; } /** @@ -145,8 +149,9 @@ static void ScrollbarClickPositioning(Window *w, NWidgetScrollbar *sb, int x, in * @param nw Pointer to the scrollbar widget. * @param x The X coordinate of the mouse click. * @param y The Y coordinate of the mouse click. + * @return true if scrollbar slider is pressed */ -void ScrollbarClickHandler(Window *w, NWidgetCore *nw, int x, int y) +bool ScrollbarClickHandler(Window *w, NWidgetCore *nw, int x, int y) { int mi, ma; @@ -159,7 +164,7 @@ void ScrollbarClickHandler(Window *w, NWidgetCore *nw, int x, int y) } NWidgetScrollbar *scrollbar = dynamic_cast(nw); assert(scrollbar != NULL); - ScrollbarClickPositioning(w, scrollbar, x, y, mi, ma); + return ScrollbarClickPositioning(w, scrollbar, x, y, mi, ma); } /** diff --git a/src/window.cpp b/src/window.cpp index fd9d8524a0..c7273d11f0 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -646,12 +646,10 @@ static void ChangeFocusedWindow(Window *w, int x, int y) NWidgetCore *nw = w->nested_root->GetWidgetFromPos(x, y); WidgetType widget_type = (nw != NULL) ? nw->type : WWT_EMPTY; - bool focused_widget_changed = false; /* If clicked on a window that previously did dot have focus */ if (_focused_window != w && // We already have focus, right? (w->window_desc->flags & WDF_NO_FOCUS) == 0 && // Don't lose focus to toolbars widget_type != WWT_CLOSEBOX) { // Don't change focused window if 'X' (close button) was clicked - focused_widget_changed = true; SetFocusedWindow(w); } @@ -675,15 +673,14 @@ static void ChangeFocusedWindow(Window *w, int x, int y) * a user has the edit box focused and then click on another window and * then back again on the edit box (to type some text). */ - focused_widget_changed |= w->SetFocusedWidget(widget_index); + w->SetFocusedWidget(widget_index); } // Special cases for scrollable widgets and resize button switch (widget_type) { case NWID_VSCROLLBAR: case NWID_HSCROLLBAR: - _dragging_widget = true; - ScrollbarClickHandler(w, nw, x, y); + if (ScrollbarClickHandler(w, nw, x, y)) _dragging_widget = true; break; case WWT_RESIZEBOX: @@ -711,8 +708,6 @@ static void SendLeftClickEventToWindow(Window *w, int x, int y, int click_count) if (w->nested_focus != NULL) nw = w->GetWidget(w->nested_focus->index); WidgetType widget_type = (nw != NULL) ? nw->type : WWT_EMPTY; - bool focused_widget_changed = false; // Only used for OSK window - if (nw == NULL) return; // exit if clicked outside of widgets /* don't allow any interaction if the button has been disabled */ @@ -731,7 +726,7 @@ static void SendLeftClickEventToWindow(Window *w, int x, int y, int click_count) switch (widget_type) { case WWT_EDITBOX: { QueryString *query = w->GetQueryString(widget_index); - if (query != NULL) query->ClickEditBox(w, pt, widget_index, click_count, focused_widget_changed); + if (query != NULL) query->ClickEditBox(w, pt, widget_index, click_count, false); break; } @@ -827,15 +822,9 @@ static void DispatchLeftButtonDownEvent(Window *w, int x, int y, int click_count */ static void DispatchLeftButtonUpEvent(Window *w, int x, int y) { - if (_settings_client.gui.windows_titlebars || _dragging_window) return; - if (_focused_window != NULL && w->window_class != WC_MAIN_TOOLBAR && w->window_class != WC_MAIN_TOOLBAR_RIGHT) { - // Send event to the window and widget which were initially under the mouse cursor - SendLeftClickEventToWindow(_focused_window, _left_button_down_pos.x - _focused_window->left, _left_button_down_pos.y - _focused_window->top, 1); - } else { - // Special case, such as toolbar buttons - SendLeftClickEventToWindow(w, x, y, 1); - } _dragging_widget = false; + if (_settings_client.gui.windows_titlebars || _dragging_window) return; + SendLeftClickEventToWindow(w, x, y, 1); } /** diff --git a/src/window_gui.h b/src/window_gui.h index ea7d732550..67c84e65da 100644 --- a/src/window_gui.h +++ b/src/window_gui.h @@ -912,6 +912,6 @@ extern SpecialMouseMode _special_mouse_mode; void SetFocusedWindow(Window *w); -void ScrollbarClickHandler(Window *w, NWidgetCore *nw, int x, int y); +bool ScrollbarClickHandler(Window *w, NWidgetCore *nw, int x, int y); #endif /* WINDOW_GUI_H */