changeset 15786:04cd1d957b49 draft

(svn r20460) -Codechange: Remove WF_SCROLL window flags and store the state directly in the scrollbar widget instead.
author frosch <frosch@openttd.org>
date Thu, 12 Aug 2010 09:14:34 +0000
parents d3efcc449c7d
children 6c4bec3b8401
files src/widget.cpp src/widget_type.h src/window.cpp src/window_gui.h
diffstat 4 files changed, 41 insertions(+), 70 deletions(-) [+]
line wrap: on
line diff
--- a/src/widget.cpp
+++ b/src/widget.cpp
@@ -65,46 +65,26 @@
 /**
  * Compute new position of the scrollbar after a click and updates the window flags.
  * @param w   Window on which a scroll was performed.
- * @param nw  Scrollbar
+ * @param sb  Scrollbar
  * @param mi  Minimum coordinate of the scroll bar.
  * @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.
  */
-static void ScrollbarClickPositioning(Window *w, NWidgetScrollbar *nw, int x, int y, int mi, int ma)
+static void ScrollbarClickPositioning(Window *w, NWidgetScrollbar *sb, int x, int y, int mi, int ma)
 {
 	int pos;
 	bool rtl = false;
-	Scrollbar *sb = nw;
 
-	switch (nw->type) {
-		case WWT_SCROLLBAR:
-			/* vertical scroller */
-			w->flags4 &= ~WF_HSCROLL;
-			w->flags4 &= ~WF_SCROLL2;
-			pos = y;
-			break;
-
-		case WWT_SCROLL2BAR:
-			/* 2nd vertical scroller */
-			w->flags4 &= ~WF_HSCROLL;
-			w->flags4 |= WF_SCROLL2;
-			pos = y;
-			break;
-
-		case WWT_HSCROLLBAR:
-			/* horizontal scroller */
-			w->flags4 &= ~WF_SCROLL2;
-			w->flags4 |= WF_HSCROLL;
-			pos = x;
-			rtl = _dynlang.text_dir == TD_RTL;
-			break;
-
-		default: NOT_REACHED();
+	if (sb->type == WWT_HSCROLLBAR) {
+		pos = x;
+		rtl = _dynlang.text_dir == TD_RTL;
+	} else {
+		pos = y;
 	}
 	if (pos <= mi + 9) {
 		/* Pressing the upper button? */
-		w->flags4 |= WF_SCROLL_UP;
+		SetBit(sb->disp_flags, NDB_SCROLLBAR_UP);
 		if (_scroller_click_timeout == 0) {
 			_scroller_click_timeout = 6;
 			sb->UpdatePosition(rtl ? 1 : -1);
@@ -112,7 +92,7 @@
 		_left_button_clicked = false;
 	} else if (pos >= ma - 10) {
 		/* Pressing the lower button? */
-		w->flags4 |= WF_SCROLL_DOWN;
+		SetBit(sb->disp_flags, NDB_SCROLLBAR_DOWN);
 
 		if (_scroller_click_timeout == 0) {
 			_scroller_click_timeout = 6;
@@ -120,7 +100,7 @@
 		}
 		_left_button_clicked = false;
 	} else {
-		Point pt = HandleScrollbarHittest(sb, mi, ma, nw->type == WWT_HSCROLLBAR);
+		Point pt = HandleScrollbarHittest(sb, mi, ma, sb->type == WWT_HSCROLLBAR);
 
 		if (pos < pt.x) {
 			sb->UpdatePosition(rtl ? sb->GetCapacity() : -sb->GetCapacity());
@@ -129,7 +109,7 @@
 		} else {
 			_scrollbar_start_pos = pt.x - mi - 9;
 			_scrollbar_size = ma - mi - 23;
-			w->scrolling_scrollbar = nw->index;
+			w->scrolling_scrollbar = sb->index;
 			_cursorpos_drag_start = _cursor.pos;
 		}
 	}
@@ -1718,28 +1698,14 @@
 	const DrawPixelInfo *dpi = _cur_dpi;
 	if (dpi->left > r.right || dpi->left + dpi->width <= r.left || dpi->top > r.bottom || dpi->top + dpi->height <= r.top) return;
 
-	switch (this->type) {
-		case WWT_HSCROLLBAR:
-			DrawHorizontalScrollbar(r, this->colour, (w->flags4 & (WF_SCROLL_UP | WF_HSCROLL)) == (WF_SCROLL_UP | WF_HSCROLL),
-								w->scrolling_scrollbar == this->index,
-								(w->flags4 & (WF_SCROLL_DOWN | WF_HSCROLL)) == (WF_SCROLL_DOWN | WF_HSCROLL), this);
-			break;
+	bool up_lowered = HasBit(this->disp_flags, NDB_SCROLLBAR_UP);
+	bool down_lowered = HasBit(this->disp_flags, NDB_SCROLLBAR_DOWN);
+	bool middle_lowered = (w->scrolling_scrollbar == this->index);
 
-		case WWT_SCROLLBAR:
-			assert(this->widget_data == 0);
-			DrawVerticalScrollbar(r, this->colour, (w->flags4 & (WF_SCROLL_UP | WF_HSCROLL | WF_SCROLL2)) == WF_SCROLL_UP,
-								w->scrolling_scrollbar == this->index,
-								(w->flags4 & (WF_SCROLL_DOWN | WF_HSCROLL | WF_SCROLL2)) == WF_SCROLL_DOWN, this);
-			break;
-
-		case WWT_SCROLL2BAR:
-			assert(this->widget_data == 0);
-			DrawVerticalScrollbar(r, this->colour, (w->flags4 & (WF_SCROLL_UP | WF_HSCROLL | WF_SCROLL2)) == (WF_SCROLL_UP | WF_SCROLL2),
-								w->scrolling_scrollbar == this->index,
-								(w->flags4 & (WF_SCROLL_DOWN | WF_HSCROLL | WF_SCROLL2)) == (WF_SCROLL_DOWN | WF_SCROLL2), this);
-			break;
-
-		default: NOT_REACHED();
+	if (this->type == WWT_HSCROLLBAR) {
+		DrawHorizontalScrollbar(r, this->colour, up_lowered, middle_lowered, down_lowered, this);
+	} else {
+		DrawVerticalScrollbar(r, this->colour, up_lowered, middle_lowered, down_lowered, this);
 	}
 
 	if (this->IsDisabled()) {
--- a/src/widget_type.h
+++ b/src/widget_type.h
@@ -250,6 +250,9 @@
 	NDB_SHADE_DIMMED    = 4, ///< Display dimmed colours in the viewport.
 	/* Button dropdown widget. */
 	NDB_DROPDOWN_ACTIVE = 5, ///< Dropdown menu of the button dropdown widget is active. @see #NWID_BUTTON_DRPDOWN
+	/* Scrollbar widget. */
+	NDB_SCROLLBAR_UP    = 6, ///< Up-button is lowered bit.
+	NDB_SCROLLBAR_DOWN  = 7, ///< Down-button is lowered bit.
 
 	ND_LOWERED  = 1 << NDB_LOWERED,                ///< Bit value of the lowered flag.
 	ND_DISABLED = 1 << NDB_DISABLED,               ///< Bit value of the disabled flag.
@@ -257,6 +260,8 @@
 	ND_SHADE_GREY      = 1 << NDB_SHADE_GREY,      ///< Bit value of the 'shade to grey' flag.
 	ND_SHADE_DIMMED    = 1 << NDB_SHADE_DIMMED,    ///< Bit value of the 'dimmed colours' flag.
 	ND_DROPDOWN_ACTIVE = 1 << NDB_DROPDOWN_ACTIVE, ///< Bit value of the 'dropdown active' flag.
+	ND_SCROLLBAR_UP    = 1 << NDB_SCROLLBAR_UP,    ///< Bit value of the 'scrollbar up' flag.
+	ND_SCROLLBAR_DOWN  = 1 << NDB_SCROLLBAR_DOWN,  ///< Bit value of the 'scrollbar down' flag.
 };
 DECLARE_ENUM_AS_BIT_SET(NWidgetDisplay)
 
--- a/src/window.cpp
+++ b/src/window.cpp
@@ -1380,9 +1380,15 @@
 	Window *w;
 	FOR_ALL_WINDOWS_FROM_FRONT(w) {
 		/* Unclick scrollbar buttons if they are pressed. */
-		if (w->flags4 & (WF_SCROLL_DOWN | WF_SCROLL_UP)) {
-			w->flags4 &= ~(WF_SCROLL_DOWN | WF_SCROLL_UP);
-			w->SetDirty();
+		for (uint i = 0; i < w->nested_array_size; i++) {
+			NWidgetBase *nwid = w->nested_array[i];
+			if (nwid != NULL && (nwid->type == WWT_HSCROLLBAR || nwid->type == WWT_SCROLLBAR || nwid->type == WWT_SCROLL2BAR)) {
+				NWidgetScrollbar *sb = static_cast<NWidgetScrollbar*>(nwid);
+				if (sb->disp_flags & (ND_SCROLLBAR_UP | ND_SCROLLBAR_DOWN)) {
+					sb->disp_flags &= ~(ND_SCROLLBAR_UP | ND_SCROLLBAR_DOWN);
+					sb->SetDirty(w);
+				}
+			}
 		}
 		w->OnMouseLoop();
 	}
@@ -1831,14 +1837,12 @@
 			}
 
 			int i;
-			Scrollbar *sb = w->GetScrollbar(w->scrolling_scrollbar);
+			NWidgetScrollbar *sb = w->GetWidget<NWidgetScrollbar>(w->scrolling_scrollbar);
 			bool rtl = false;
 
-			if (w->flags4 & WF_HSCROLL) {
+			if (sb->type == WWT_HSCROLLBAR) {
 				i = _cursor.pos.x - _cursorpos_drag_start.x;
 				rtl = _dynlang.text_dir == TD_RTL;
-			} else if (w->flags4 & WF_SCROLL2) {
-				i = _cursor.pos.y - _cursorpos_drag_start.y;
 			} else {
 				i = _cursor.pos.y - _cursorpos_drag_start.y;
 			}
--- a/src/window_gui.h
+++ b/src/window_gui.h
@@ -746,21 +746,17 @@
 	WF_TIMEOUT_BEGIN     = 7,       ///< The initial value for the timeout
 	WF_TIMEOUT_MASK      = 7,       ///< Window timeout counter bit mask (3 bits)
 	WF_DRAGGING          = 1 <<  3, ///< Window is being dragged
-	WF_SCROLL_UP         = 1 <<  4, ///< Upper scroll button has been pressed, @see ScrollbarClickHandler()
-	WF_SCROLL_DOWN       = 1 <<  5, ///< Lower scroll button has been pressed, @see ScrollbarClickHandler()
-	WF_SCROLL2           = 1 <<  7,
-	WF_HSCROLL           = 1 <<  8,
-	WF_SIZING_RIGHT      = 1 <<  9, ///< Window is being resized towards the right.
-	WF_SIZING_LEFT       = 1 << 10, ///< Window is being resized towards the left.
+	WF_SIZING_RIGHT      = 1 <<  4, ///< Window is being resized towards the right.
+	WF_SIZING_LEFT       = 1 <<  5, ///< Window is being resized towards the left.
 	WF_SIZING            = WF_SIZING_RIGHT | WF_SIZING_LEFT, ///< Window is being resized.
-	WF_STICKY            = 1 << 11, ///< Window is made sticky by user
+	WF_STICKY            = 1 <<  6, ///< Window is made sticky by user
 
-	WF_DISABLE_VP_SCROLL = 1 << 12, ///< Window does not do autoscroll, @see HandleAutoscroll()
+	WF_DISABLE_VP_SCROLL = 1 <<  7, ///< Window does not do autoscroll, @see HandleAutoscroll()
 
-	WF_WHITE_BORDER_ONE  = 1 << 13,
-	WF_WHITE_BORDER_MASK = 1 << 14 | WF_WHITE_BORDER_ONE,
+	WF_WHITE_BORDER_ONE  = 1 <<  8,
+	WF_WHITE_BORDER_MASK = 1 <<  9 | WF_WHITE_BORDER_ONE,
 
-	WF_CENTERED          = 1 << 15, ///< Window is centered and shall stay centered after ReInit
+	WF_CENTERED          = 1 << 10, ///< Window is centered and shall stay centered after ReInit
 };
 
 Window *BringWindowToFrontById(WindowClass cls, WindowNumber number);