changeset 13626:0fcc5f6fa927 draft

(svn r18150) -Codechange: make the horizontal scrollbar scroll in the opposite direction with RTL
author rubidium <rubidium@openttd.org>
date Tue, 17 Nov 2009 18:45:57 +0000
parents 36774c0b0d8b
children 159d7fbe4130
files src/widget.cpp src/window.cpp
diffstat 2 files changed, 30 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/src/widget.cpp
+++ b/src/widget.cpp
@@ -30,30 +30,35 @@
  * @param sb     Scrollbar list data
  * @param top    Top position of the scrollbar (top position of the up-button)
  * @param bottom Bottom position of the scrollbar (bottom position of the down-button)
+ * @param horizontal Whether the scrollbar is horizontal or not
  * @return A Point, with x containing the top coordinate of the draggable part, and
  *                       y containing the bottom coordinate of the draggable part
  */
-static Point HandleScrollbarHittest(const Scrollbar *sb, int top, int bottom)
+static Point HandleScrollbarHittest(const Scrollbar *sb, int top, int bottom, bool horizontal)
 {
-	Point pt;
-	int height, count, pos, cap;
-
+	/* Base for reversion */
+	int rev_base = top + bottom;
 	top += 10;   // top    points to just below the up-button
 	bottom -= 9; // bottom points to top of the down-button
 
-	height = (bottom - top);
-
-	pos = sb->GetPosition();
-	count = sb->GetCount();
-	cap = sb->GetCapacity();
+	int height = (bottom - top);
+	int pos = sb->GetPosition();
+	int count = sb->GetCount();
+	int cap = sb->GetCapacity();
 
 	if (count != 0) top += height * pos / count;
 
 	if (cap > count) cap = count;
 	if (count != 0) bottom -= (count - pos - cap) * height / count;
 
-	pt.x = top;
-	pt.y = bottom - 1;
+	Point pt;
+	if (horizontal && _dynlang.text_dir == TD_RTL) {
+		pt.x = rev_base - (bottom - 1);
+		pt.y = rev_base - top;
+	} else {
+		pt.x = top;
+		pt.y = bottom - 1;
+	}
 	return pt;
 }
 
@@ -70,6 +75,7 @@
 {
 	int pos;
 	Scrollbar *sb;
+	bool rtl = false;
 
 	switch (wtp) {
 		case WWT_SCROLLBAR:
@@ -88,12 +94,13 @@
 			sb = &w->vscroll2;
 			break;
 
-		case  WWT_HSCROLLBAR:
+		case WWT_HSCROLLBAR:
 			/* horizontal scroller */
 			w->flags4 &= ~WF_SCROLL2;
 			w->flags4 |= WF_HSCROLL;
 			pos = x;
 			sb = &w->hscroll;
+			rtl = _dynlang.text_dir == TD_RTL;
 			break;
 
 		default: NOT_REACHED();
@@ -103,7 +110,7 @@
 		w->flags4 |= WF_SCROLL_UP;
 		if (_scroller_click_timeout == 0) {
 			_scroller_click_timeout = 6;
-			sb->UpdatePosition(-1);
+			sb->UpdatePosition(rtl ? 1 : -1);
 		}
 		_left_button_clicked = false;
 	} else if (pos >= ma - 10) {
@@ -112,16 +119,16 @@
 
 		if (_scroller_click_timeout == 0) {
 			_scroller_click_timeout = 6;
-			sb->UpdatePosition(1);
+			sb->UpdatePosition(rtl ? -1 : 1);
 		}
 		_left_button_clicked = false;
 	} else {
-		Point pt = HandleScrollbarHittest(sb, mi, ma);
+		Point pt = HandleScrollbarHittest(sb, mi, ma, wtp == WWT_HSCROLLBAR);
 
 		if (pos < pt.x) {
-			sb->UpdatePosition(-sb->GetCapacity());
+			sb->UpdatePosition(rtl ? sb->GetCapacity() : -sb->GetCapacity());
 		} else if (pos > pt.y) {
-			sb->UpdatePosition(sb->GetCapacity());
+			sb->UpdatePosition(rtl ? -sb->GetCapacity() : sb->GetCapacity());
 		} else {
 			_scrollbar_start_pos = pt.x - mi - 9;
 			_scrollbar_size = ma - mi - 23;
@@ -158,7 +165,7 @@
 			ma = nw->pos_y + nw->current_y;
 			break;
 
-		case  WWT_HSCROLLBAR:
+		case WWT_HSCROLLBAR:
 			/* horizontal scroller */
 			mi = nw->pos_x;
 			ma = nw->pos_x + nw->current_x;
@@ -357,7 +364,7 @@
 	GfxFillRect(r.left + 7, r.top + 10, r.left + 7, r.bottom - 10, c1);
 	GfxFillRect(r.left + 8, r.top + 10, r.left + 8, r.bottom - 10, c2);
 
-	Point pt = HandleScrollbarHittest(scrollbar, r.top, r.bottom);
+	Point pt = HandleScrollbarHittest(scrollbar, r.top, r.bottom, false);
 	DrawFrameRect(r.left, pt.x, r.right, pt.y, colour, bar_dragged ? FR_LOWERED : FR_NONE);
 }
 
@@ -392,7 +399,7 @@
 	GfxFillRect(r.left + 10, r.top + 8, r.right - 10, r.top + 8, c2);
 
 	/* draw actual scrollbar */
-	Point pt = HandleScrollbarHittest(scrollbar, r.left, r.right);
+	Point pt = HandleScrollbarHittest(scrollbar, r.left, r.right, true);
 	DrawFrameRect(pt.x, r.top, pt.y, r.bottom, colour, bar_dragged ? FR_LOWERED : FR_NONE);
 }
 
--- a/src/window.cpp
+++ b/src/window.cpp
@@ -1676,10 +1676,12 @@
 
 			int i;
 			Scrollbar *sb;
+			bool rtl = false;
 
 			if (w->flags4 & WF_HSCROLL) {
 				sb = &w->hscroll;
 				i = _cursor.pos.x - _cursorpos_drag_start.x;
+				rtl = _dynlang.text_dir == TD_RTL;
 			} else if (w->flags4 & WF_SCROLL2) {
 				sb = &w->vscroll2;
 				i = _cursor.pos.y - _cursorpos_drag_start.y;
@@ -1690,6 +1692,7 @@
 
 			/* Find the item we want to move to and make sure it's inside bounds. */
 			int pos = min(max(0, i + _scrollbar_start_pos) * sb->GetCount() / _scrollbar_size, max(0, sb->GetCount() - sb->GetCapacity()));
+			if (rtl) pos = sb->GetCount() - sb->GetCapacity() - pos;
 			if (pos != sb->GetPosition()) {
 				sb->SetPosition(pos);
 				w->SetDirty();