Codechange: use int32_t instead of uint16_t for scroll bar position/size/capacity

(cherry picked from commit d09b5aaeba)

# Conflicts:
#	src/newgrf_debug_gui.cpp
#	src/newgrf_gui.cpp
#	src/widget_type.h
pull/684/head
Rubidium 3 months ago committed by Jonathan G Rennison
parent 2219b8e378
commit 5d8e40cad4

@ -487,8 +487,8 @@ public:
break; break;
case WID_AP_AIRPORT_LIST: { case WID_AP_AIRPORT_LIST: {
int num_clicked = this->vscroll->GetScrolledRowFromWidget(pt.y, this, widget, 0, this->line_height); int32_t num_clicked = this->vscroll->GetScrolledRowFromWidget(pt.y, this, widget, 0, this->line_height);
if (num_clicked == INT_MAX) break; if (num_clicked == INT32_MAX) break;
const AirportSpec *as = AirportClass::Get(_selected_airport_class)->GetSpec(num_clicked); const AirportSpec *as = AirportClass::Get(_selected_airport_class)->GetSpec(num_clicked);
if (as->IsAvailable()) this->SelectOtherAirport(num_clicked); if (as->IsAvailable()) this->SelectOtherAirport(num_clicked);
break; break;

@ -403,9 +403,9 @@ public:
/* We need to find the departure corresponding to where the user clicked. */ /* We need to find the departure corresponding to where the user clicked. */
uint32_t id_v = (pt.y - this->GetWidget<NWidgetBase>(WID_DB_LIST)->pos_y) / this->entry_height; uint32_t id_v = (pt.y - this->GetWidget<NWidgetBase>(WID_DB_LIST)->pos_y) / this->entry_height;
if (id_v >= this->vscroll->GetCapacity()) return; // click out of bounds if (id_v >= (uint32_t)this->vscroll->GetCapacity()) return; // click out of bounds
id_v += this->vscroll->GetPosition(); id_v += (uint32_t)this->vscroll->GetPosition();
if (id_v >= (this->departures->size() + this->arrivals->size())) return; // click out of list bound if (id_v >= (this->departures->size() + this->arrivals->size())) return; // click out of list bound
@ -769,7 +769,7 @@ void DeparturesWindow<Twaypoint>::DrawDeparturesListItems(const Rect &r) const
} }
} }
if (i < this->vscroll->GetPosition()) { if (i < (uint32_t)this->vscroll->GetPosition()) {
continue; continue;
} }

@ -461,10 +461,10 @@ struct DepotWindow : Window {
} }
ym = (y - matrix_widget->pos_y) % this->resize.step_height; ym = (y - matrix_widget->pos_y) % this->resize.step_height;
int row = this->vscroll->GetScrolledRowFromWidget(y, this, WID_D_MATRIX); int32_t row = this->vscroll->GetScrolledRowFromWidget(y, this, WID_D_MATRIX);
uint pos = (row * this->num_columns) + xt; uint pos = (row * this->num_columns) + xt;
if (row == INT_MAX || this->vehicle_list.size() + this->wagon_list.size() <= pos) { if (row == INT32_MAX || this->vehicle_list.size() + this->wagon_list.size() <= pos) {
/* Clicking on 'line' / 'block' without a vehicle */ /* Clicking on 'line' / 'block' without a vehicle */
if (this->type == VEH_TRAIN) { if (this->type == VEH_TRAIN) {
/* End the dragging */ /* End the dragging */

@ -596,7 +596,7 @@ struct FramerateWindow : Window {
void DrawElementTimesColumn(const Rect &r, StringID heading_str, const CachedDecimal *values) const void DrawElementTimesColumn(const Rect &r, StringID heading_str, const CachedDecimal *values) const
{ {
const Scrollbar *sb = this->GetScrollbar(WID_FRW_SCROLLBAR); const Scrollbar *sb = this->GetScrollbar(WID_FRW_SCROLLBAR);
uint16_t skip = sb->GetPosition(); int32_t skip = sb->GetPosition();
int drawable = this->num_displayed; int drawable = this->num_displayed;
int y = r.top; int y = r.top;
DrawString(r.left, r.right, y, heading_str, TC_FROMSTRING, SA_CENTER, true); DrawString(r.left, r.right, y, heading_str, TC_FROMSTRING, SA_CENTER, true);
@ -618,7 +618,7 @@ struct FramerateWindow : Window {
void DrawElementAllocationsColumn(const Rect &r) const void DrawElementAllocationsColumn(const Rect &r) const
{ {
const Scrollbar *sb = this->GetScrollbar(WID_FRW_SCROLLBAR); const Scrollbar *sb = this->GetScrollbar(WID_FRW_SCROLLBAR);
uint16_t skip = sb->GetPosition(); int32_t skip = sb->GetPosition();
int drawable = this->num_displayed; int drawable = this->num_displayed;
int y = r.top; int y = r.top;
DrawString(r.left, r.right, y, STR_FRAMERATE_MEMORYUSE, TC_FROMSTRING, SA_CENTER, true); DrawString(r.left, r.right, y, STR_FRAMERATE_MEMORYUSE, TC_FROMSTRING, SA_CENTER, true);
@ -652,7 +652,7 @@ struct FramerateWindow : Window {
case WID_FRW_TIMES_NAMES: { case WID_FRW_TIMES_NAMES: {
/* Render a column of titles for performance element names */ /* Render a column of titles for performance element names */
const Scrollbar *sb = this->GetScrollbar(WID_FRW_SCROLLBAR); const Scrollbar *sb = this->GetScrollbar(WID_FRW_SCROLLBAR);
uint16_t skip = sb->GetPosition(); int32_t skip = sb->GetPosition();
int drawable = this->num_displayed; int drawable = this->num_displayed;
int y = r.top + GetCharacterHeight(FS_NORMAL) + WidgetDimensions::scaled.vsep_normal; // first line contains headings in the value columns int y = r.top + GetCharacterHeight(FS_NORMAL) + WidgetDimensions::scaled.vsep_normal; // first line contains headings in the value columns
for (PerformanceElement e : DISPLAY_ORDER_PFE) { for (PerformanceElement e : DISPLAY_ORDER_PFE) {
@ -696,8 +696,8 @@ struct FramerateWindow : Window {
case WID_FRW_TIMES_AVERAGE: { case WID_FRW_TIMES_AVERAGE: {
/* Open time graph windows when clicking detail measurement lines */ /* Open time graph windows when clicking detail measurement lines */
const Scrollbar *sb = this->GetScrollbar(WID_FRW_SCROLLBAR); const Scrollbar *sb = this->GetScrollbar(WID_FRW_SCROLLBAR);
int line = sb->GetScrolledRowFromWidget(pt.y, this, widget, WidgetDimensions::scaled.vsep_normal + GetCharacterHeight(FS_NORMAL)); int32_t line = sb->GetScrolledRowFromWidget(pt.y, this, widget, WidgetDimensions::scaled.vsep_normal + GetCharacterHeight(FS_NORMAL));
if (line != INT_MAX) { if (line != INT32_MAX) {
line++; line++;
/* Find the visible line that was clicked */ /* Find the visible line that was clicked */
for (PerformanceElement e : DISPLAY_ORDER_PFE) { for (PerformanceElement e : DISPLAY_ORDER_PFE) {

@ -887,7 +887,7 @@ struct ExcludingCargoBaseGraphWindow : BaseGraphWindow {
} }
case WID_ECBG_MATRIX: { case WID_ECBG_MATRIX: {
uint row = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_ECBG_MATRIX); int32_t row = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_ECBG_MATRIX);
if (row >= this->vscroll->GetCount()) return; if (row >= this->vscroll->GetCount()) return;
for (const CargoSpec *cs : _sorted_standard_cargo_specs) { for (const CargoSpec *cs : _sorted_standard_cargo_specs) {
@ -2027,7 +2027,7 @@ struct StationCargoGraphWindow final : BaseGraphWindow {
} }
case WID_SCG_MATRIX: { case WID_SCG_MATRIX: {
uint row = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_SCG_MATRIX); int32_t row = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_SCG_MATRIX);
if (row >= this->vscroll->GetCount()) return; if (row >= this->vscroll->GetCount()) return;
for (const CargoSpec *cs : _sorted_standard_cargo_specs) { for (const CargoSpec *cs : _sorted_standard_cargo_specs) {

@ -339,7 +339,7 @@ struct NewGRFInspectWindow : Window {
Scrollbar *vscroll; Scrollbar *vscroll;
int first_variable_line_index = 0; int32_t first_variable_line_index = 0;
bool redraw_panel = false; bool redraw_panel = false;
bool redraw_scrollbar = false; bool redraw_scrollbar = false;
@ -569,7 +569,7 @@ struct NewGRFInspectWindow : Window {
const void *base = nih->GetInstance(index); const void *base = nih->GetInstance(index);
const void *base_spec = nih->GetSpec(index); const void *base_spec = nih->GetSpec(index);
uint i = 0; int32_t i = 0;
auto guard = scope_guard([&]() { auto guard = scope_guard([&]() {
if (this->log_console) { if (this->log_console) {
@ -577,12 +577,12 @@ struct NewGRFInspectWindow : Window {
DEBUG(misc, 0, "*** END ***"); DEBUG(misc, 0, "*** END ***");
} }
uint count = std::min<uint>(UINT16_MAX, i); const int32_t count = i;
if (vscroll->GetCount() != count) { if (vscroll->GetCount() != count) {
/* Not nice and certainly a hack, but it beats duplicating /* Not nice and certainly a hack, but it beats duplicating
* this whole function just to count the actual number of * this whole function just to count the actual number of
* elements. Especially because they need to be redrawn. */ * elements. Especially because they need to be redrawn. */
uint position = this->vscroll->GetPosition(); const int32_t position = this->vscroll->GetPosition();
const_cast<NewGRFInspectWindow*>(this)->vscroll->SetCount(count); const_cast<NewGRFInspectWindow*>(this)->vscroll->SetCount(count);
const_cast<NewGRFInspectWindow*>(this)->redraw_scrollbar = true; const_cast<NewGRFInspectWindow*>(this)->redraw_scrollbar = true;
if (position != this->vscroll->GetPosition()) { if (position != this->vscroll->GetPosition()) {
@ -921,8 +921,8 @@ struct NewGRFInspectWindow : Window {
case WID_NGRFI_MAINPANEL: { case WID_NGRFI_MAINPANEL: {
/* Get the line, make sure it's within the boundaries. */ /* Get the line, make sure it's within the boundaries. */
int line = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_NGRFI_MAINPANEL, WidgetDimensions::scaled.framerect.top); int32_t line = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_NGRFI_MAINPANEL, WidgetDimensions::scaled.framerect.top);
if (line == INT_MAX) return; if (line == INT32_MAX) return;
if (this->sprite_dump) { if (this->sprite_dump) {
if (_ctrl_pressed) { if (_ctrl_pressed) {

@ -151,12 +151,12 @@ static void ShowNewGRFInfo(const GRFConfig *c, const Rect &r, bool show_params)
struct NewGRFParametersWindow : public Window { struct NewGRFParametersWindow : public Window {
static GRFParameterInfo dummy_parameter_info; ///< Dummy info in case a newgrf didn't provide info about some parameter. static GRFParameterInfo dummy_parameter_info; ///< Dummy info in case a newgrf didn't provide info about some parameter.
GRFConfig *grf_config; ///< Set the parameters of this GRFConfig. GRFConfig *grf_config; ///< Set the parameters of this GRFConfig.
uint clicked_button; ///< The row in which a button was clicked or UINT_MAX. int32_t clicked_button; ///< The row in which a button was clicked or INT_MAX when none is selected.
bool clicked_increase; ///< True if the increase button was clicked, false for the decrease button. bool clicked_increase; ///< True if the increase button was clicked, false for the decrease button.
bool clicked_dropdown; ///< Whether the dropdown is open. bool clicked_dropdown; ///< Whether the dropdown is open.
bool closing_dropdown; ///< True, if the dropdown list is currently closing. bool closing_dropdown; ///< True, if the dropdown list is currently closing.
GUITimer timeout; ///< How long before we unpress the last-pressed button? GUITimer timeout; ///< How long before we unpress the last-pressed button?
uint clicked_row; ///< The selected parameter int32_t clicked_row; ///< The selected parameter, or INT_MAX when none is selected.
int line_height; ///< Height of a row in the matrix widget. int line_height; ///< Height of a row in the matrix widget.
Scrollbar *vscroll; Scrollbar *vscroll;
bool action14present; ///< True if action14 information is present. bool action14present; ///< True if action14 information is present.
@ -164,10 +164,10 @@ struct NewGRFParametersWindow : public Window {
NewGRFParametersWindow(WindowDesc *desc, bool is_baseset, GRFConfig *c, bool editable) : Window(desc), NewGRFParametersWindow(WindowDesc *desc, bool is_baseset, GRFConfig *c, bool editable) : Window(desc),
grf_config(c), grf_config(c),
clicked_button(UINT_MAX), clicked_button(INT32_MAX),
clicked_dropdown(false), clicked_dropdown(false),
closing_dropdown(false), closing_dropdown(false),
clicked_row(UINT_MAX), clicked_row(INT32_MAX),
editable(editable) editable(editable)
{ {
this->action14present = (c->num_valid_params != c->param.size() || !c->param_info.empty()); this->action14present = (c->num_valid_params != c->param.size() || !c->param_info.empty());
@ -294,7 +294,7 @@ struct NewGRFParametersWindow : public Window {
int button_y_offset = (this->line_height - SETTING_BUTTON_HEIGHT) / 2; int button_y_offset = (this->line_height - SETTING_BUTTON_HEIGHT) / 2;
int text_y_offset = (this->line_height - GetCharacterHeight(FS_NORMAL)) / 2; int text_y_offset = (this->line_height - GetCharacterHeight(FS_NORMAL)) / 2;
for (uint i = this->vscroll->GetPosition(); this->vscroll->IsVisible(i) && i < this->vscroll->GetCount(); i++) { for (int32_t i = this->vscroll->GetPosition(); this->vscroll->IsVisible(i) && i < this->vscroll->GetCount(); i++) {
GRFParameterInfo &par_info = this->GetParameterInfo(i); GRFParameterInfo &par_info = this->GetParameterInfo(i);
uint32_t current_value = par_info.GetValue(this->grf_config); uint32_t current_value = par_info.GetValue(this->grf_config);
bool selected = (i == this->clicked_row); bool selected = (i == this->clicked_row);
@ -366,7 +366,7 @@ struct NewGRFParametersWindow : public Window {
case WID_NP_BACKGROUND: { case WID_NP_BACKGROUND: {
if (!this->editable) break; if (!this->editable) break;
uint num = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_NP_BACKGROUND); int32_t num = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_NP_BACKGROUND);
if (num >= this->vscroll->GetCount()) break; if (num >= this->vscroll->GetCount()) break;
if (this->clicked_row != num) { if (this->clicked_row != num) {
@ -505,8 +505,8 @@ struct NewGRFParametersWindow : public Window {
} }
this->vscroll->SetCount(this->action14present ? this->grf_config->num_valid_params : this->grf_config->num_params); this->vscroll->SetCount(this->action14present ? this->grf_config->num_valid_params : this->grf_config->num_params);
if (this->clicked_row != UINT_MAX && this->clicked_row >= this->vscroll->GetCount()) { if (this->clicked_row != INT32_MAX && this->clicked_row >= this->vscroll->GetCount()) {
this->clicked_row = UINT_MAX; this->clicked_row = INT32_MAX;
CloseChildWindows(WC_QUERY_STRING); CloseChildWindows(WC_QUERY_STRING);
} }
} }
@ -514,7 +514,7 @@ struct NewGRFParametersWindow : public Window {
void OnRealtimeTick(uint delta_ms) override void OnRealtimeTick(uint delta_ms) override
{ {
if (timeout.Elapsed(delta_ms)) { if (timeout.Elapsed(delta_ms)) {
this->clicked_button = UINT_MAX; this->clicked_button = INT32_MAX;
this->SetDirty(); this->SetDirty();
} }
} }

@ -1577,8 +1577,8 @@ private:
*/ */
VehicleOrderID GetOrderFromPt(int y) VehicleOrderID GetOrderFromPt(int y)
{ {
int sel = this->vscroll->GetScrolledRowFromWidget(y, this, WID_O_ORDER_LIST, WidgetDimensions::scaled.framerect.top); int32_t sel = this->vscroll->GetScrolledRowFromWidget(y, this, WID_O_ORDER_LIST, WidgetDimensions::scaled.framerect.top);
if (sel == INT_MAX) return INVALID_VEH_ORDER_ID; if (sel == INT32_MAX) return INVALID_VEH_ORDER_ID;
/* One past the orders is the 'End of Orders' line. */ /* One past the orders is the 'End of Orders' line. */
assert(IsInsideBS(sel, 0, vehicle->GetNumOrders() + 1)); assert(IsInsideBS(sel, 0, vehicle->GetNumOrders() + 1));
return sel; return sel;

@ -699,13 +699,13 @@ private:
int GetInstructionFromPt(int y) int GetInstructionFromPt(int y)
{ {
NWidgetBase *nwid = this->GetWidget<NWidgetBase>(PROGRAM_WIDGET_INSTRUCTION_LIST); NWidgetBase *nwid = this->GetWidget<NWidgetBase>(PROGRAM_WIDGET_INSTRUCTION_LIST);
int sel = (y - nwid->pos_y - WidgetDimensions::scaled.framerect.top) / nwid->resize_y; // Selected line int32_t sel = (y - nwid->pos_y - WidgetDimensions::scaled.framerect.top) / nwid->resize_y; // Selected line
if ((uint)sel >= this->vscroll->GetCapacity()) return -1; if (sel >= this->vscroll->GetCapacity()) return -1;
sel += this->vscroll->GetPosition(); sel += this->vscroll->GetPosition();
return (sel <= int(this->instructions.size()) && sel >= 0) ? sel : -1; return (sel <= (int32_t)(this->instructions.size()) && sel >= 0) ? sel : -1;
} }
void RebuildInstructionList() void RebuildInstructionList()

@ -912,7 +912,7 @@ struct SchdispatchWindow : GeneralVehicleWindow {
int xm = x % this->resize.step_width; int xm = x % this->resize.step_width;
if (xt >= this->num_columns) return { nullptr, false }; if (xt >= this->num_columns) return { nullptr, false };
uint row = y / this->resize.step_height; int32_t row = y / this->resize.step_height;
if (row >= this->vscroll->GetCapacity()) return { nullptr, false }; if (row >= this->vscroll->GetCapacity()) return { nullptr, false };
uint pos = ((row + this->vscroll->GetPosition()) * this->num_columns) + xt; uint pos = ((row + this->vscroll->GetPosition()) * this->num_columns) + xt;

@ -3066,8 +3066,8 @@ struct GameSettingsWindow : Window {
if (widget != WID_GS_OPTIONSPANEL) return; if (widget != WID_GS_OPTIONSPANEL) return;
uint btn = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_GS_OPTIONSPANEL, WidgetDimensions::scaled.framerect.top); int32_t btn = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_GS_OPTIONSPANEL, WidgetDimensions::scaled.framerect.top);
if (btn == INT_MAX || (int)btn < this->warn_lines) return; if (btn == INT32_MAX || btn < this->warn_lines) return;
btn -= this->warn_lines; btn -= this->warn_lines;
uint cur_row = 0; uint cur_row = 0;

@ -2587,7 +2587,7 @@ struct SelectStationWindow : Window {
tr.top += this->resize.step_height; tr.top += this->resize.step_height;
} }
for (uint i = std::max<uint>(1, this->vscroll->GetPosition()); i <= _stations_nearby_list.size(); ++i, tr.top += this->resize.step_height) { for (int32_t i = std::max<int32_t>(1, this->vscroll->GetPosition()); (size_t)i <= _stations_nearby_list.size(); ++i, tr.top += this->resize.step_height) {
/* Don't draw anything if it extends past the end of the window. */ /* Don't draw anything if it extends past the end of the window. */
if (i - this->vscroll->GetPosition() >= this->vscroll->GetCapacity()) break; if (i - this->vscroll->GetPosition() >= this->vscroll->GetCapacity()) break;

@ -498,12 +498,12 @@ protected:
* Get the total height of the content displayed in this window. * Get the total height of the content displayed in this window.
* @return the height in pixels * @return the height in pixels
*/ */
uint GetContentHeight() int32_t GetContentHeight()
{ {
this->EnsureStoryPageElementLayout(); this->EnsureStoryPageElementLayout();
/* The largest bottom coordinate of any element is the height of the content */ /* The largest bottom coordinate of any element is the height of the content */
uint max_y = std::accumulate(this->layout_cache.begin(), this->layout_cache.end(), 0, [](uint max_y, const LayoutCacheElement &ce) -> uint { return std::max<uint>(max_y, ce.bounds.bottom); }); int32_t max_y = std::accumulate(this->layout_cache.begin(), this->layout_cache.end(), 0, [](int32_t max_y, const LayoutCacheElement &ce) -> int32_t { return std::max<int32_t>(max_y, ce.bounds.bottom); });
return max_y; return max_y;
} }

@ -431,8 +431,8 @@ struct TimetableWindow : GeneralVehicleWindow {
int GetOrderFromTimetableWndPt(int y, [[maybe_unused]] const Vehicle *v) int GetOrderFromTimetableWndPt(int y, [[maybe_unused]] const Vehicle *v)
{ {
int sel = this->vscroll->GetScrolledRowFromWidget(y, this, WID_VT_TIMETABLE_PANEL, WidgetDimensions::scaled.framerect.top); int32_t sel = this->vscroll->GetScrolledRowFromWidget(y, this, WID_VT_TIMETABLE_PANEL, WidgetDimensions::scaled.framerect.top);
if (sel == INT_MAX) return INVALID_ORDER; if (sel == INT32_MAX) return INVALID_ORDER;
assert(IsInsideBS(sel, 0, v->GetNumOrders() * 2)); assert(IsInsideBS(sel, 0, v->GetNumOrders() * 2));
return sel; return sel;
} }

@ -2862,9 +2862,9 @@ private:
int GetItemIndexFromPt(int y) int GetItemIndexFromPt(int y)
{ {
NWidgetBase *nwid = this->GetWidget<NWidgetBase>(TR_WIDGET_INSTRUCTION_LIST); NWidgetBase *nwid = this->GetWidget<NWidgetBase>(TR_WIDGET_INSTRUCTION_LIST);
int sel = (y - nwid->pos_y - WidgetDimensions::scaled.framerect.top) / nwid->resize_y; // Selected line int32_t sel = (y - nwid->pos_y - WidgetDimensions::scaled.framerect.top) / nwid->resize_y; // Selected line
if ((uint)sel >= this->vscroll->GetCapacity()) return -1; if (sel >= this->vscroll->GetCapacity()) return -1;
sel += this->vscroll->GetPosition(); sel += this->vscroll->GetPosition();

@ -2330,13 +2330,13 @@ void NWidgetViewport::UpdateViewportCoordinates(Window *w)
* @param widget Widget number of the widget clicked in. * @param widget Widget number of the widget clicked in.
* @param padding Amount of empty space between the widget edge and the top of the first row. Default value is \c 0. * @param padding Amount of empty space between the widget edge and the top of the first row. Default value is \c 0.
* @param line_height Height of a single row. A negative value means using the vertical resize step of the widget. * @param line_height Height of a single row. A negative value means using the vertical resize step of the widget.
* @return Row number clicked at. If clicked at a wrong position, #INT_MAX is returned. * @return Row number clicked at. If clicked at a wrong position, #Scrollbar::npos is returned.
*/ */
int Scrollbar::GetScrolledRowFromWidget(int clickpos, const Window * const w, WidgetID widget, int padding, int line_height) const Scrollbar::size_type Scrollbar::GetScrolledRowFromWidget(int clickpos, const Window * const w, WidgetID widget, int padding, int line_height) const
{ {
uint pos = w->GetRowFromWidget(clickpos, widget, padding, line_height); int pos = w->GetRowFromWidget(clickpos, widget, padding, line_height);
if (pos != INT_MAX) pos += this->GetPosition(); if (pos != INT_MAX) pos += this->GetPosition();
return (pos >= this->GetCount()) ? INT_MAX : pos; return (pos < 0 || pos >= this->GetCount()) ? Scrollbar::npos : pos;
} }
/** /**

@ -707,12 +707,16 @@ public:
* Scrollbar data structure * Scrollbar data structure
*/ */
class Scrollbar { class Scrollbar {
public:
using size_type = int32_t;
static constexpr size_type max_size_type = std::numeric_limits<size_type>::max();
static constexpr size_type npos = max_size_type;
private: private:
const bool is_vertical; ///< Scrollbar has vertical orientation. const bool is_vertical; ///< Scrollbar has vertical orientation.
uint16_t count; ///< Number of elements in the list. size_type count; ///< Number of elements in the list.
uint16_t cap; ///< Number of visible elements of the scroll bar. size_type cap; ///< Number of visible elements of the scroll bar.
uint16_t pos; ///< Index of first visible item of the list. size_type pos; ///< Index of first visible item of the list.
uint16_t stepsize; ///< Distance to scroll, when pressing the buttons or using the wheel. size_type stepsize; ///< Distance to scroll, when pressing the buttons or using the wheel.
public: public:
/** Stepping sizes when scrolling */ /** Stepping sizes when scrolling */
@ -730,7 +734,7 @@ public:
* Gets the number of elements in the list * Gets the number of elements in the list
* @return the number of elements * @return the number of elements
*/ */
inline uint16_t GetCount() const inline size_type GetCount() const
{ {
return this->count; return this->count;
} }
@ -739,7 +743,7 @@ public:
* Gets the number of visible elements of the scrollbar * Gets the number of visible elements of the scrollbar
* @return the number of visible elements * @return the number of visible elements
*/ */
inline uint16_t GetCapacity() const inline size_type GetCapacity() const
{ {
return this->cap; return this->cap;
} }
@ -748,7 +752,7 @@ public:
* Gets the position of the first visible element in the list * Gets the position of the first visible element in the list
* @return the position of the element * @return the position of the element
*/ */
inline uint16_t GetPosition() const inline size_type GetPosition() const
{ {
return this->pos; return this->pos;
} }
@ -758,7 +762,7 @@ public:
* @param item to check * @param item to check
* @return true iff the item is visible * @return true iff the item is visible
*/ */
inline bool IsVisible(uint16_t item) const inline bool IsVisible(size_type item) const
{ {
return IsInsideBS(item, this->GetPosition(), this->GetCapacity()); return IsInsideBS(item, this->GetPosition(), this->GetCapacity());
} }
@ -780,7 +784,7 @@ public:
{ {
assert(stepsize > 0); assert(stepsize > 0);
this->stepsize = ClampTo<uint16_t>(stepsize); this->stepsize = ClampTo<size_type>(stepsize);
} }
/** /**
@ -790,9 +794,9 @@ public:
*/ */
void SetCount(size_t num) void SetCount(size_t num)
{ {
assert(num <= MAX_UVALUE(uint16_t)); assert(num < Scrollbar::max_size_type);
this->count = ClampTo<uint16_t>(num); this->count = ClampTo<size_type>(num);
/* Ensure position is within bounds */ /* Ensure position is within bounds */
this->SetPosition(this->pos); this->SetPosition(this->pos);
} }
@ -804,9 +808,9 @@ public:
*/ */
void SetCapacity(size_t capacity) void SetCapacity(size_t capacity)
{ {
assert(capacity <= MAX_UVALUE(uint16_t)); assert(capacity < Scrollbar::max_size_type);
this->cap = ClampTo<uint16_t>(capacity); this->cap = ClampTo<size_type>(capacity);
/* Ensure position is within bounds */ /* Ensure position is within bounds */
this->SetPosition(this->pos); this->SetPosition(this->pos);
} }
@ -818,9 +822,9 @@ public:
* @param position the position of the element * @param position the position of the element
* @return true iff the position has changed * @return true iff the position has changed
*/ */
bool SetPosition(int position) bool SetPosition(size_type position)
{ {
uint16_t old_pos = this->pos; size_type old_pos = this->pos;
this->pos = Clamp(position, 0, std::max(this->count - this->cap, 0)); this->pos = Clamp(position, 0, std::max(this->count - this->cap, 0));
return this->pos != old_pos; return this->pos != old_pos;
} }
@ -849,7 +853,7 @@ public:
* the window depending on where in the list it was. * the window depending on where in the list it was.
* @param position the position to scroll towards. * @param position the position to scroll towards.
*/ */
void ScrollTowards(int position) void ScrollTowards(size_type position)
{ {
if (position < this->GetPosition()) { if (position < this->GetPosition()) {
/* scroll up to the item */ /* scroll up to the item */
@ -860,7 +864,7 @@ public:
} }
} }
int GetScrolledRowFromWidget(int clickpos, const Window * const w, WidgetID widget, int padding = 0, int line_height = -1) const; size_type GetScrolledRowFromWidget(int clickpos, const Window * const w, WidgetID widget, int padding = 0, int line_height = -1) const;
/** /**
* Get a pair of iterators for the range of visible elements in a container. * Get a pair of iterators for the range of visible elements in a container.
@ -870,7 +874,7 @@ public:
template <typename Tcontainer> template <typename Tcontainer>
auto GetVisibleRangeIterators(Tcontainer &container) const auto GetVisibleRangeIterators(Tcontainer &container) const
{ {
assert(this->GetCount() == container.size()); // Scrollbar and container size must match. assert((size_t)this->GetCount() == container.size()); // Scrollbar and container size must match.
auto first = std::next(std::begin(container), this->GetPosition()); auto first = std::next(std::begin(container), this->GetPosition());
auto last = std::next(first, std::min<size_t>(this->GetCapacity(), this->GetCount() - this->GetPosition())); auto last = std::next(first, std::min<size_t>(this->GetCapacity(), this->GetCount() - this->GetPosition()));
return std::make_pair(first, last); return std::make_pair(first, last);
@ -889,9 +893,9 @@ public:
template <typename Tcontainer> template <typename Tcontainer>
typename Tcontainer::iterator GetScrolledItemFromWidget(Tcontainer &container, int clickpos, const Window * const w, WidgetID widget, int padding = 0, int line_height = -1) const typename Tcontainer::iterator GetScrolledItemFromWidget(Tcontainer &container, int clickpos, const Window * const w, WidgetID widget, int padding = 0, int line_height = -1) const
{ {
assert(this->GetCount() == container.size()); // Scrollbar and container size must match. assert((size_t)this->GetCount() == container.size()); // Scrollbar and container size must match.
int row = this->GetScrolledRowFromWidget(clickpos, w, widget, padding, line_height); size_type row = this->GetScrolledRowFromWidget(clickpos, w, widget, padding, line_height);
if (row == INT_MAX) return std::end(container); if (row == Scrollbar::npos) return std::end(container);
typename Tcontainer::iterator it = std::begin(container); typename Tcontainer::iterator it = std::begin(container);
std::advance(it, row); std::advance(it, row);

Loading…
Cancel
Save