changeset 14037:ca29deba0f1a draft

(svn r18580) -Codechange: Use widget information only for setting scrollbar capacity.
author alberth <alberth@openttd.org>
date Sun, 20 Dec 2009 20:08:39 +0000
parents 3bed2b357ff4
children 4ed6d0dbc58e
files src/ai/ai_gui.cpp src/autoreplace_gui.cpp src/bridge_gui.cpp src/build_vehicle_gui.cpp src/depot_gui.cpp src/group_gui.cpp src/industry_gui.cpp src/misc_gui.cpp src/network/network_content_gui.cpp src/network/network_gui.cpp src/newgrf_gui.cpp src/order_gui.cpp src/settings_gui.cpp src/signs_gui.cpp src/station_gui.cpp src/subsidy_gui.cpp src/timetable_gui.cpp src/town_gui.cpp src/vehicle_gui.cpp src/window.cpp src/window_gui.h
diffstat 21 files changed, 53 insertions(+), 39 deletions(-) [+]
line wrap: on
line diff
--- a/src/ai/ai_gui.cpp
+++ b/src/ai/ai_gui.cpp
@@ -906,7 +906,7 @@
 
 	virtual void OnResize()
 	{
-		this->vscroll.SetCapacity(this->GetWidget<NWidgetBase>(AID_WIDGET_LOG_PANEL)->current_y / this->resize.step_height);
+		this->vscroll.SetCapacityFromWidget(this, AID_WIDGET_LOG_PANEL);
 	}
 };
 
--- a/src/autoreplace_gui.cpp
+++ b/src/autoreplace_gui.cpp
@@ -480,8 +480,8 @@
 
 	virtual void OnResize()
 	{
-		this->vscroll.SetCapacity(this->GetWidget<NWidgetBase>(RVW_WIDGET_LEFT_MATRIX)->current_y / this->resize.step_height);
-		this->vscroll2.SetCapacity(this->GetWidget<NWidgetBase>(RVW_WIDGET_RIGHT_MATRIX)->current_y / this->resize.step_height);
+		this->vscroll.SetCapacityFromWidget(this, RVW_WIDGET_LEFT_MATRIX);
+		this->vscroll2.SetCapacityFromWidget(this, RVW_WIDGET_RIGHT_MATRIX);
 
 		this->GetWidget<NWidgetCore>(RVW_WIDGET_LEFT_MATRIX)->widget_data =
 				this->GetWidget<NWidgetCore>(RVW_WIDGET_RIGHT_MATRIX)->widget_data = (this->vscroll.GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
--- a/src/bridge_gui.cpp
+++ b/src/bridge_gui.cpp
@@ -280,7 +280,7 @@
 
 	virtual void OnResize()
 	{
-		this->vscroll.SetCapacity(this->GetWidget<NWidgetBase>(BBSW_BRIDGE_LIST)->current_y / this->resize.step_height);
+		this->vscroll.SetCapacityFromWidget(this, BBSW_BRIDGE_LIST);
 		this->GetWidget<NWidgetCore>(BBSW_BRIDGE_LIST)->widget_data = (this->vscroll.GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
 
 		this->last_size = max(this->vscroll.GetCapacity(), this->last_size);
--- a/src/build_vehicle_gui.cpp
+++ b/src/build_vehicle_gui.cpp
@@ -1176,9 +1176,8 @@
 
 	virtual void OnResize()
 	{
-		NWidgetCore *nwi = this->GetWidget<NWidgetCore>(BUILD_VEHICLE_WIDGET_LIST);
-		this->vscroll.SetCapacity(nwi->current_y / this->resize.step_height);
-		nwi->widget_data = (this->vscroll.GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
+		this->vscroll.SetCapacityFromWidget(this, BUILD_VEHICLE_WIDGET_LIST);
+		this->GetWidget<NWidgetCore>(BUILD_VEHICLE_WIDGET_LIST)->widget_data = (this->vscroll.GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
 	}
 };
 
--- a/src/depot_gui.cpp
+++ b/src/depot_gui.cpp
@@ -941,12 +941,12 @@
 	virtual void OnResize()
 	{
 		NWidgetCore *nwi = this->GetWidget<NWidgetCore>(DEPOT_WIDGET_MATRIX);
-		this->vscroll.SetCapacity(nwi->current_y / (int)this->resize.step_height);
+		this->vscroll.SetCapacityFromWidget(this, DEPOT_WIDGET_MATRIX);
 		if (this->type == VEH_TRAIN) {
 			this->hscroll.SetCapacity(nwi->current_x - this->header_width - this->count_width);
 			nwi->widget_data = (this->vscroll.GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
 		} else {
-			this->hscroll.SetCapacity(nwi->current_x / (int)this->resize.step_width);
+			this->hscroll.SetCapacityFromWidget(this, DEPOT_WIDGET_MATRIX);
 			nwi->widget_data = (this->vscroll.GetCapacity() << MAT_ROW_START) + (this->hscroll.GetCapacity() << MAT_COL_START);
 		}
 	}
--- a/src/group_gui.cpp
+++ b/src/group_gui.cpp
@@ -596,7 +596,7 @@
 		nwi->widget_data = (this->vscroll2.GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
 
 		nwi = this->GetWidget<NWidgetCore>(GRP_WIDGET_LIST_VEHICLE);
-		this->vscroll.SetCapacity(this->GetWidget<NWidgetBase>(GRP_WIDGET_LIST_VEHICLE)->current_y / this->resize.step_height);
+		this->vscroll.SetCapacityFromWidget(this, GRP_WIDGET_LIST_VEHICLE);
 		nwi->widget_data = (this->vscroll.GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
 	}
 
--- a/src/industry_gui.cpp
+++ b/src/industry_gui.cpp
@@ -445,7 +445,7 @@
 	virtual void OnResize()
 	{
 		/* Adjust the number of items in the matrix depending of the resize */
-		this->vscroll.SetCapacity(this->GetWidget<NWidgetBase>(DPIW_MATRIX_WIDGET)->current_y / this->resize.step_height);
+		this->vscroll.SetCapacityFromWidget(this, DPIW_MATRIX_WIDGET);
 		this->GetWidget<NWidgetCore>(DPIW_MATRIX_WIDGET)->widget_data = (this->vscroll.GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
 	}
 
@@ -1138,7 +1138,7 @@
 
 	virtual void OnResize()
 	{
-		this->vscroll.SetCapacity(this->GetWidget<NWidgetBase>(IDW_INDUSTRY_LIST)->current_y / this->resize.step_height);
+		this->vscroll.SetCapacityFromWidget(this, IDW_INDUSTRY_LIST);
 	}
 
 	virtual void OnHundredthTick()
--- a/src/misc_gui.cpp
+++ b/src/misc_gui.cpp
@@ -2023,7 +2023,7 @@
 
 	virtual void OnResize()
 	{
-		this->vscroll.SetCapacity(this->GetWidget<NWidgetBase>(SLWW_DRIVES_DIRECTORIES_LIST)->current_y / this->resize.step_height);
+		this->vscroll.SetCapacityFromWidget(this, SLWW_DRIVES_DIRECTORIES_LIST);
 	}
 
 	virtual void OnInvalidateData(int data)
--- a/src/network/network_content_gui.cpp
+++ b/src/network/network_content_gui.cpp
@@ -730,7 +730,7 @@
 
 	virtual void OnResize()
 	{
-		this->vscroll.SetCapacity(this->GetWidget<NWidgetBase>(NCLWW_MATRIX)->current_y / this->resize.step_height);
+		this->vscroll.SetCapacityFromWidget(this, NCLWW_MATRIX);
 		this->GetWidget<NWidgetCore>(NCLWW_MATRIX)->widget_data = (this->vscroll.GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
 	}
 
--- a/src/network/network_gui.cpp
+++ b/src/network/network_gui.cpp
@@ -895,9 +895,8 @@
 
 	virtual void OnResize()
 	{
-		NWidgetCore *nwi = this->GetWidget<NWidgetCore>(NGWW_MATRIX);
-		this->vscroll.SetCapacity(nwi->current_y / this->resize.step_height);
-		nwi->widget_data = (this->vscroll.GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
+		this->vscroll.SetCapacityFromWidget(this, NGWW_MATRIX);
+		this->GetWidget<NWidgetCore>(NGWW_MATRIX)->widget_data = (this->vscroll.GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
 	}
 };
 
@@ -1673,9 +1672,8 @@
 
 	virtual void OnResize()
 	{
-		NWidgetCore *nwi = this->GetWidget<NWidgetCore>(NLWW_MATRIX);
-		this->vscroll.SetCapacity(nwi->current_y / this->resize.step_height);
-		nwi->widget_data = (this->vscroll.GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
+		this->vscroll.SetCapacityFromWidget(this, NLWW_MATRIX);
+		this->GetWidget<NWidgetCore>(NLWW_MATRIX)->widget_data = (this->vscroll.GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
 	}
 };
 
--- a/src/newgrf_gui.cpp
+++ b/src/newgrf_gui.cpp
@@ -297,7 +297,7 @@
 
 	virtual void OnResize()
 	{
-		this->vscroll.SetCapacity(this->GetWidget<NWidgetBase>(ANGRFW_GRF_LIST)->current_y / this->resize.step_height);
+		this->vscroll.SetCapacityFromWidget(this, ANGRFW_GRF_LIST);
 	}
 
 	virtual void OnPaint()
@@ -621,9 +621,8 @@
 
 	virtual void OnResize()
 	{
-		NWidgetCore *nwi = this->GetWidget<NWidgetCore>(SNGRFS_FILE_LIST);
-		this->vscroll.SetCapacity(nwi->current_y / this->resize.step_height);
-		nwi->widget_data = (this->vscroll.GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
+		this->vscroll.SetCapacityFromWidget(this, SNGRFS_FILE_LIST);
+		this->GetWidget<NWidgetCore>(SNGRFS_FILE_LIST)->widget_data = (this->vscroll.GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
 	}
 
 	virtual void SetStringParameters(int widget) const
@@ -970,7 +969,7 @@
 
 				for (c = this->list, i = 0; c != NULL; c = c->next, i++) {}
 
-				this->vscroll.SetCapacity((this->GetWidget<NWidgetBase>(SNGRFS_FILE_LIST)->current_y) / this->resize.step_height);
+				this->vscroll.SetCapacityFromWidget(this, SNGRFS_FILE_LIST);
 				this->GetWidget<NWidgetCore>(SNGRFS_FILE_LIST)->widget_data = (this->vscroll.GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
 				this->vscroll.SetCount(i);
 				break;
--- a/src/order_gui.cpp
+++ b/src/order_gui.cpp
@@ -1289,7 +1289,7 @@
 	virtual void OnResize()
 	{
 		/* Update the scroll bar */
-		this->vscroll.SetCapacity(this->GetWidget<NWidgetBase>(ORDER_WIDGET_ORDER_LIST)->current_y / this->resize.step_height);
+		this->vscroll.SetCapacityFromWidget(this, ORDER_WIDGET_ORDER_LIST);
 	}
 
 	virtual void OnTimeout()
--- a/src/settings_gui.cpp
+++ b/src/settings_gui.cpp
@@ -1625,7 +1625,7 @@
 
 	virtual void OnResize()
 	{
-		this->vscroll.SetCapacity((this->GetWidget<NWidgetBase>(SETTINGSEL_OPTIONSPANEL)->current_y - SETTINGTREE_TOP_OFFSET - SETTINGTREE_BOTTOM_OFFSET) / this->resize.step_height);
+		this->vscroll.SetCapacityFromWidget(this, SETTINGSEL_OPTIONSPANEL, SETTINGTREE_TOP_OFFSET + SETTINGTREE_BOTTOM_OFFSET);
 	}
 };
 
--- a/src/signs_gui.cpp
+++ b/src/signs_gui.cpp
@@ -158,7 +158,7 @@
 
 	virtual void OnResize()
 	{
-		this->vscroll.SetCapacity((this->GetWidget<NWidgetBase>(SLW_LIST)->current_y - WD_FRAMERECT_TOP - WD_FRAMERECT_BOTTOM) / this->resize.step_height);
+		this->vscroll.SetCapacityFromWidget(this, SLW_LIST, WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM);
 	}
 
 	virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
--- a/src/station_gui.cpp
+++ b/src/station_gui.cpp
@@ -604,7 +604,7 @@
 
 	virtual void OnResize()
 	{
-		this->vscroll.SetCapacity((this->GetWidget<NWidgetBase>(SLW_LIST)->current_y - WD_FRAMERECT_TOP - WD_FRAMERECT_BOTTOM) / FONT_HEIGHT_NORMAL);
+		this->vscroll.SetCapacityFromWidget(this, SLW_LIST, WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM);
 	}
 
 	virtual void OnInvalidateData(int data)
@@ -1118,7 +1118,7 @@
 
 	virtual void OnResize()
 	{
-		this->vscroll.SetCapacity((this->GetWidget<NWidgetBase>(SVW_WAITING)->current_y - WD_FRAMERECT_TOP - WD_FRAMERECT_BOTTOM) / this->resize.step_height);
+		this->vscroll.SetCapacityFromWidget(this, SVW_WAITING, WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM);
 	}
 };
 
@@ -1359,7 +1359,7 @@
 
 	virtual void OnResize()
 	{
-		this->vscroll.SetCapacity((this->GetWidget<NWidgetBase>(JSW_PANEL)->current_y - WD_FRAMERECT_TOP - WD_FRAMERECT_BOTTOM) / this->resize.step_height);
+		this->vscroll.SetCapacityFromWidget(this, JSW_PANEL, WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM);
 	}
 
 	virtual void OnInvalidateData(int data)
--- a/src/subsidy_gui.cpp
+++ b/src/subsidy_gui.cpp
@@ -217,7 +217,7 @@
 
 	virtual void OnResize()
 	{
-		this->vscroll.SetCapacity(this->GetWidget<NWidgetBase>(SLW_PANEL)->current_y / this->resize.step_height);
+		this->vscroll.SetCapacityFromWidget(this, SLW_PANEL);
 	}
 
 	virtual void OnInvalidateData(int data)
--- a/src/timetable_gui.cpp
+++ b/src/timetable_gui.cpp
@@ -585,7 +585,7 @@
 	virtual void OnResize()
 	{
 		/* Update the scroll bar */
-		this->vscroll.SetCapacity((this->GetWidget<NWidgetBase>(TTV_TIMETABLE_PANEL)->current_y - WD_FRAMERECT_TOP - WD_FRAMERECT_BOTTOM) / this->resize.step_height);
+		this->vscroll.SetCapacityFromWidget(this, TTV_TIMETABLE_PANEL, WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM);
 	}
 
 	/**
--- a/src/town_gui.cpp
+++ b/src/town_gui.cpp
@@ -845,7 +845,7 @@
 
 	virtual void OnResize()
 	{
-		this->vscroll.SetCapacity(this->GetWidget<NWidgetBase>(TDW_CENTERTOWN)->current_y / this->resize.step_height);
+		this->vscroll.SetCapacityFromWidget(this, TDW_CENTERTOWN);
 	}
 
 	virtual void OnInvalidateData(int data)
--- a/src/vehicle_gui.cpp
+++ b/src/vehicle_gui.cpp
@@ -494,9 +494,8 @@
 
 	virtual void OnResize()
 	{
-		NWidgetCore *nwi = this->GetWidget<NWidgetCore>(VRW_MATRIX);
-		this->vscroll.SetCapacity(nwi->current_y / this->resize.step_height);
-		nwi->widget_data = (this->vscroll.GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
+		this->vscroll.SetCapacityFromWidget(this, VRW_MATRIX);
+		this->GetWidget<NWidgetCore>(VRW_MATRIX)->widget_data = (this->vscroll.GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
 	}
 };
 
@@ -1243,7 +1242,7 @@
 
 	virtual void OnResize()
 	{
-		this->vscroll.SetCapacity(this->GetWidget<NWidgetBase>(VLW_WIDGET_LIST)->current_y / this->resize.step_height);
+		this->vscroll.SetCapacityFromWidget(this, VLW_WIDGET_LIST);
 		this->GetWidget<NWidgetCore>(VLW_WIDGET_LIST)->widget_data = (this->vscroll.GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
 	}
 
@@ -1683,7 +1682,7 @@
 	{
 		NWidgetCore *nwi = this->GetWidget<NWidgetCore>(VLD_WIDGET_MATRIX);
 		if (nwi != NULL) {
-			this->vscroll.SetCapacity(nwi->current_y / this->resize.step_height);
+			this->vscroll.SetCapacityFromWidget(this, VLD_WIDGET_MATRIX);
 			nwi->widget_data = (this->vscroll.GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
 		}
 	}
--- a/src/window.cpp
+++ b/src/window.cpp
@@ -79,6 +79,23 @@
 }
 
 /**
+ * Set capacity of visible elements from the size and resize properties of a widget.
+ * @param w       Window.
+ * @param widget  Widget with size and resize properties.
+ * @param padding Padding to subtract from the size.
+ * @note Updates the position if needed.
+ */
+void Scrollbar::SetCapacityFromWidget(Window *w, int widget, int padding)
+{
+	NWidgetBase *nwid = w->GetWidget<NWidgetBase>(widget);
+	if (this->is_vertical) {
+		this->SetCapacity(((int)nwid->current_y - padding) / (int)nwid->resize_y);
+	} else {
+		this->SetCapacity(((int)nwid->current_x - padding) / (int)nwid->resize_x);
+	}
+}
+
+/**
  * Set the window that has the focus
  * @param w The window to set the focus on
  */
--- a/src/window_gui.h
+++ b/src/window_gui.h
@@ -247,6 +247,8 @@
 		if (this->cap + this->pos > this->count) this->pos = max(0, this->count - this->cap);
 	}
 
+	void SetCapacityFromWidget(Window *w, int widget, int padding = 0);
+
 	/**
 	 * Sets the position of the first visible element
 	 * @param position the position of the element