diff --git a/src/ai/ai_gui.cpp b/src/ai/ai_gui.cpp index aa8ee11794..5aab3eaff7 100644 --- a/src/ai/ai_gui.cpp +++ b/src/ai/ai_gui.cpp @@ -906,7 +906,7 @@ struct AIDebugWindow : public Window { virtual void OnResize() { - this->vscroll.SetCapacity(this->GetWidget(AID_WIDGET_LOG_PANEL)->current_y / this->resize.step_height); + this->vscroll.SetCapacityFromWidget(this, AID_WIDGET_LOG_PANEL); } }; diff --git a/src/autoreplace_gui.cpp b/src/autoreplace_gui.cpp index be39297f37..9bcde4c02d 100644 --- a/src/autoreplace_gui.cpp +++ b/src/autoreplace_gui.cpp @@ -480,8 +480,8 @@ public: virtual void OnResize() { - this->vscroll.SetCapacity(this->GetWidget(RVW_WIDGET_LEFT_MATRIX)->current_y / this->resize.step_height); - this->vscroll2.SetCapacity(this->GetWidget(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(RVW_WIDGET_LEFT_MATRIX)->widget_data = this->GetWidget(RVW_WIDGET_RIGHT_MATRIX)->widget_data = (this->vscroll.GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START); diff --git a/src/bridge_gui.cpp b/src/bridge_gui.cpp index 5f1c919679..e64fcd8ff7 100644 --- a/src/bridge_gui.cpp +++ b/src/bridge_gui.cpp @@ -280,7 +280,7 @@ public: virtual void OnResize() { - this->vscroll.SetCapacity(this->GetWidget(BBSW_BRIDGE_LIST)->current_y / this->resize.step_height); + this->vscroll.SetCapacityFromWidget(this, BBSW_BRIDGE_LIST); this->GetWidget(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); diff --git a/src/build_vehicle_gui.cpp b/src/build_vehicle_gui.cpp index f6b7a4dfe6..d99bdea130 100644 --- a/src/build_vehicle_gui.cpp +++ b/src/build_vehicle_gui.cpp @@ -1176,9 +1176,8 @@ struct BuildVehicleWindow : Window { virtual void OnResize() { - NWidgetCore *nwi = this->GetWidget(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(BUILD_VEHICLE_WIDGET_LIST)->widget_data = (this->vscroll.GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START); } }; diff --git a/src/depot_gui.cpp b/src/depot_gui.cpp index c053fffc48..171d437d33 100644 --- a/src/depot_gui.cpp +++ b/src/depot_gui.cpp @@ -941,12 +941,12 @@ struct DepotWindow : Window { virtual void OnResize() { NWidgetCore *nwi = this->GetWidget(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); } } diff --git a/src/group_gui.cpp b/src/group_gui.cpp index 1c321c4b31..657a1e39e6 100644 --- a/src/group_gui.cpp +++ b/src/group_gui.cpp @@ -596,7 +596,7 @@ public: nwi->widget_data = (this->vscroll2.GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START); nwi = this->GetWidget(GRP_WIDGET_LIST_VEHICLE); - this->vscroll.SetCapacity(this->GetWidget(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); } diff --git a/src/industry_gui.cpp b/src/industry_gui.cpp index 28697ae2f9..73a6aa7f1c 100644 --- a/src/industry_gui.cpp +++ b/src/industry_gui.cpp @@ -445,7 +445,7 @@ public: virtual void OnResize() { /* Adjust the number of items in the matrix depending of the resize */ - this->vscroll.SetCapacity(this->GetWidget(DPIW_MATRIX_WIDGET)->current_y / this->resize.step_height); + this->vscroll.SetCapacityFromWidget(this, DPIW_MATRIX_WIDGET); this->GetWidget(DPIW_MATRIX_WIDGET)->widget_data = (this->vscroll.GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START); } @@ -1138,7 +1138,7 @@ public: virtual void OnResize() { - this->vscroll.SetCapacity(this->GetWidget(IDW_INDUSTRY_LIST)->current_y / this->resize.step_height); + this->vscroll.SetCapacityFromWidget(this, IDW_INDUSTRY_LIST); } virtual void OnHundredthTick() diff --git a/src/misc_gui.cpp b/src/misc_gui.cpp index 156fc2b3e9..d0f0690e0d 100644 --- a/src/misc_gui.cpp +++ b/src/misc_gui.cpp @@ -2023,7 +2023,7 @@ public: virtual void OnResize() { - this->vscroll.SetCapacity(this->GetWidget(SLWW_DRIVES_DIRECTORIES_LIST)->current_y / this->resize.step_height); + this->vscroll.SetCapacityFromWidget(this, SLWW_DRIVES_DIRECTORIES_LIST); } virtual void OnInvalidateData(int data) diff --git a/src/network/network_content_gui.cpp b/src/network/network_content_gui.cpp index b91aa02f2f..7d3c223db3 100644 --- a/src/network/network_content_gui.cpp +++ b/src/network/network_content_gui.cpp @@ -730,7 +730,7 @@ public: virtual void OnResize() { - this->vscroll.SetCapacity(this->GetWidget(NCLWW_MATRIX)->current_y / this->resize.step_height); + this->vscroll.SetCapacityFromWidget(this, NCLWW_MATRIX); this->GetWidget(NCLWW_MATRIX)->widget_data = (this->vscroll.GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START); } diff --git a/src/network/network_gui.cpp b/src/network/network_gui.cpp index 4ba2262666..514f385213 100644 --- a/src/network/network_gui.cpp +++ b/src/network/network_gui.cpp @@ -895,9 +895,8 @@ public: virtual void OnResize() { - NWidgetCore *nwi = this->GetWidget(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(NGWW_MATRIX)->widget_data = (this->vscroll.GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START); } }; @@ -1673,9 +1672,8 @@ struct NetworkLobbyWindow : public Window { virtual void OnResize() { - NWidgetCore *nwi = this->GetWidget(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(NLWW_MATRIX)->widget_data = (this->vscroll.GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START); } }; diff --git a/src/newgrf_gui.cpp b/src/newgrf_gui.cpp index 1515e4ba54..8b7e58dedf 100644 --- a/src/newgrf_gui.cpp +++ b/src/newgrf_gui.cpp @@ -297,7 +297,7 @@ public: virtual void OnResize() { - this->vscroll.SetCapacity(this->GetWidget(ANGRFW_GRF_LIST)->current_y / this->resize.step_height); + this->vscroll.SetCapacityFromWidget(this, ANGRFW_GRF_LIST); } virtual void OnPaint() @@ -621,9 +621,8 @@ struct NewGRFWindow : public Window { virtual void OnResize() { - NWidgetCore *nwi = this->GetWidget(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(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 @@ struct NewGRFWindow : public Window { for (c = this->list, i = 0; c != NULL; c = c->next, i++) {} - this->vscroll.SetCapacity((this->GetWidget(SNGRFS_FILE_LIST)->current_y) / this->resize.step_height); + this->vscroll.SetCapacityFromWidget(this, SNGRFS_FILE_LIST); this->GetWidget(SNGRFS_FILE_LIST)->widget_data = (this->vscroll.GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START); this->vscroll.SetCount(i); break; diff --git a/src/order_gui.cpp b/src/order_gui.cpp index 608ac3d626..8a1068c461 100644 --- a/src/order_gui.cpp +++ b/src/order_gui.cpp @@ -1289,7 +1289,7 @@ public: virtual void OnResize() { /* Update the scroll bar */ - this->vscroll.SetCapacity(this->GetWidget(ORDER_WIDGET_ORDER_LIST)->current_y / this->resize.step_height); + this->vscroll.SetCapacityFromWidget(this, ORDER_WIDGET_ORDER_LIST); } virtual void OnTimeout() diff --git a/src/settings_gui.cpp b/src/settings_gui.cpp index 37856e305f..82c45a53d4 100644 --- a/src/settings_gui.cpp +++ b/src/settings_gui.cpp @@ -1625,7 +1625,7 @@ struct GameSettingsWindow : Window { virtual void OnResize() { - this->vscroll.SetCapacity((this->GetWidget(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); } }; diff --git a/src/signs_gui.cpp b/src/signs_gui.cpp index 3b69bb0cb6..67dab304d1 100644 --- a/src/signs_gui.cpp +++ b/src/signs_gui.cpp @@ -158,7 +158,7 @@ struct SignListWindow : Window, SignList { virtual void OnResize() { - this->vscroll.SetCapacity((this->GetWidget(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) diff --git a/src/station_gui.cpp b/src/station_gui.cpp index 9d2dfb26a1..875689f4f5 100644 --- a/src/station_gui.cpp +++ b/src/station_gui.cpp @@ -604,7 +604,7 @@ public: virtual void OnResize() { - this->vscroll.SetCapacity((this->GetWidget(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 @@ struct StationViewWindow : public Window { virtual void OnResize() { - this->vscroll.SetCapacity((this->GetWidget(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 @@ struct SelectStationWindow : Window { virtual void OnResize() { - this->vscroll.SetCapacity((this->GetWidget(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) diff --git a/src/subsidy_gui.cpp b/src/subsidy_gui.cpp index 5798494fd6..8f63615f94 100644 --- a/src/subsidy_gui.cpp +++ b/src/subsidy_gui.cpp @@ -217,7 +217,7 @@ struct SubsidyListWindow : Window { virtual void OnResize() { - this->vscroll.SetCapacity(this->GetWidget(SLW_PANEL)->current_y / this->resize.step_height); + this->vscroll.SetCapacityFromWidget(this, SLW_PANEL); } virtual void OnInvalidateData(int data) diff --git a/src/timetable_gui.cpp b/src/timetable_gui.cpp index 8bf59bb99a..df9f285748 100644 --- a/src/timetable_gui.cpp +++ b/src/timetable_gui.cpp @@ -585,7 +585,7 @@ struct TimetableWindow : Window { virtual void OnResize() { /* Update the scroll bar */ - this->vscroll.SetCapacity((this->GetWidget(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); } /** diff --git a/src/town_gui.cpp b/src/town_gui.cpp index ff1b9baa64..e4cb0ab7af 100644 --- a/src/town_gui.cpp +++ b/src/town_gui.cpp @@ -845,7 +845,7 @@ public: virtual void OnResize() { - this->vscroll.SetCapacity(this->GetWidget(TDW_CENTERTOWN)->current_y / this->resize.step_height); + this->vscroll.SetCapacityFromWidget(this, TDW_CENTERTOWN); } virtual void OnInvalidateData(int data) diff --git a/src/vehicle_gui.cpp b/src/vehicle_gui.cpp index 830c022b52..0e40b94e81 100644 --- a/src/vehicle_gui.cpp +++ b/src/vehicle_gui.cpp @@ -494,9 +494,8 @@ struct RefitWindow : public Window { virtual void OnResize() { - NWidgetCore *nwi = this->GetWidget(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(VRW_MATRIX)->widget_data = (this->vscroll.GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START); } }; @@ -1243,7 +1242,7 @@ public: virtual void OnResize() { - this->vscroll.SetCapacity(this->GetWidget(VLW_WIDGET_LIST)->current_y / this->resize.step_height); + this->vscroll.SetCapacityFromWidget(this, VLW_WIDGET_LIST); this->GetWidget(VLW_WIDGET_LIST)->widget_data = (this->vscroll.GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START); } @@ -1683,7 +1682,7 @@ struct VehicleDetailsWindow : Window { { NWidgetCore *nwi = this->GetWidget(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); } } diff --git a/src/window.cpp b/src/window.cpp index 48f934f7bd..29ddcdff63 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -78,6 +78,23 @@ WindowDesc::~WindowDesc() { } +/** + * 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(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 diff --git a/src/window_gui.h b/src/window_gui.h index 3cc9f45ea9..d2635d8812 100644 --- a/src/window_gui.h +++ b/src/window_gui.h @@ -247,6 +247,8 @@ public: 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