OpenTTD Source: Scrollbar Class Reference

OpenTTD Source 14.0-beta3

Public Types | Public Member Functions | Private Attributes
Scrollbar Class Reference

Scrollbar data structure. More...

`#include <widget_type.h>`

Inheritance diagram for Scrollbar:

## Public Types
enum | ScrollbarStepping { SS_RAW, SS_SMALL, SS_BIG }
| Stepping sizes when scrolling. More...

## Public Member Functions
| Scrollbar (bool is_vertical)
uint16_t | GetCount () const
| Gets the number of elements in the list. More...
uint16_t | GetCapacity () const
| Gets the number of visible elements of the scrollbar. More...
uint16_t | GetPosition () const
| Gets the position of the first visible element in the list. More...
bool | IsVisible (uint16_t item) const
| Checks whether given current item is visible in the list. More...
bool | IsVertical () const
| Is the scrollbar vertical or not? More...
void | SetStepSize (size_t stepsize)
| Set the distance to scroll when using the buttons or the wheel. More...
void | SetCount (size_t num)
| Sets the number of elements in the list. More...
void | SetCapacity (size_t capacity)
| Set the capacity of visible elements. More...
void | SetCapacityFromWidget (Window *w, WidgetID widget, int padding=0)
| Set capacity of visible elements from the size and resize properties of a widget. More...
bool | SetPosition (int position)
| Sets the position of the first visible element. More...
bool | UpdatePosition (int difference, ScrollbarStepping unit=SS_SMALL)
| Updates the position of the first visible element by the given amount. More...
void | ScrollTowards (int position)
| Scroll towards the given position; if the item is visible nothing happens, otherwise it will be shown either at the bottom or top of the window depending on where in the list it was. More...
int | GetScrolledRowFromWidget (int clickpos, const Window *const w, WidgetID widget, int padding=0, int line_height=-1) const
| Compute the row of a scrolled widget that a user clicked in. More...
template<typename Tcontainer >
Tcontainer::iterator | GetScrolledItemFromWidget (Tcontainer &container, int clickpos, const Window *const w, WidgetID widget, int padding=0, int line_height=-1) const
| Return an iterator pointing to the element of a scrolled widget that a user clicked in. More...
EventState | UpdateListPositionOnKeyPress (int &list_position, uint16_t keycode) const
| Update the given list position as if it were on this scroll bar when the given keycode was pressed. More...

## Private Attributes
const bool | is_vertical
| Scrollbar has vertical orientation.
uint16_t | count
| Number of elements in the list.
uint16_t | cap
| Number of visible elements of the scroll bar.
uint16_t | pos
| Index of first visible item of the list.
uint16_t | stepsize
| Distance to scroll, when pressing the buttons or using the wheel.

## Detailed Description

Scrollbar data structure.

Definition at line 678 of file widget_type.h.

## Member Enumeration Documentation

## ◆ ScrollbarStepping

enum Scrollbar::ScrollbarStepping

Stepping sizes when scrolling.

Enumerator
SS_RAW | Step in single units.
SS_SMALL | Step in stepsize units.
SS_BIG | Step in cap units.

Definition at line 688 of file widget_type.h.

## Member Function Documentation

## ◆ GetCapacity()

uint16_t Scrollbar::GetCapacity | ( |  | ) | const
| inline

Gets the number of visible elements of the scrollbar.

Returns
  the number of visible elements

Definition at line 711 of file widget_type.h.

Referenced by DispatchMouseWheelEvent(), GoalListWindow::DrawListColumn(), NetworkContentListWindow::DrawMatrix(), NewGRFInspectWindow::DrawString(), HandleScrollbarHittest(), ScrollTowards(), and ScriptDebugWindow::UpdateLogScroll().

## ◆ GetCount()

uint16_t Scrollbar::GetCount | ( |  | ) | const
| inline

Gets the number of elements in the list.

Returns
  the number of elements

Definition at line 702 of file widget_type.h.

Referenced by DispatchMouseWheelEvent(), GetScrolledItemFromWidget(), GetScrolledRowFromWidget(), HandleScrollbarHittest(), NewGRFParametersWindow::OnInvalidateData(), StoryBookWindow::OnPaint(), and ScriptDebugWindow::UpdateLogScroll().

## ◆ GetPosition()

uint16_t Scrollbar::GetPosition | ( |  | ) | const
| inline

Gets the position of the first visible element in the list.

Returns
  the position of the element

Definition at line 720 of file widget_type.h.

Referenced by IndustryCargoesWindow::CalculatePositionInWidget(), ScriptDebugWindow::ChangeToScript(), TextfileWindow::CheckHyperlinkClick(), TimetableWindow::DrawArrivalDeparturePanel(), GoalListWindow::DrawListColumn(), NetworkContentListWindow::DrawMatrix(), NewGRFInspectWindow::DrawString(), TimetableWindow::DrawTimetablePanel(), GetScrolledRowFromWidget(), NWidgetMatrix::GetScrollOffsets(), HandleScrollbarHittest(), ScrollTowards(), NWidgetMatrix::SetClicked(), TextfileWindow::UpdateHistoryScrollpos(), and ScriptDebugWindow::UpdateLogScroll().

## ◆ GetScrolledItemFromWidget()

template<typename Tcontainer >

Tcontainer::iterator Scrollbar::GetScrolledItemFromWidget | ( | Tcontainer & | container,
|  | int | clickpos,
|  | const Window *const | w,
|  | WidgetID | widget,
|  | int | padding = `0`,
|  | int | line_height = `-1`
| ) |  | const
| inline

Return an iterator pointing to the element of a scrolled widget that a user clicked in.

Parameters

  container | Container of elements represented by the scrollbar.
  clickpos | Vertical position of the mouse click (without taking scrolling into account).
  w | The window the click was in.
  widget | Widget number of the widget clicked in.
  padding | Amount of empty space between the widget edge and the top of the first row. Default value is `0`.
  line_height | Height of a single row. A negative value means using the vertical resize step of the widget.

Returns
  Iterator to the element clicked at. If clicked at a wrong position, returns as interator to the end of the container.

Definition at line 845 of file widget_type.h.

References GetCount(), and GetScrolledRowFromWidget().

## ◆ GetScrolledRowFromWidget()

int Scrollbar::GetScrolledRowFromWidget | ( | int | clickpos,
|  | const Window *const | w,
|  | WidgetID | widget,
|  | int | padding = `0`,
|  | int | line_height = `-1`
| ) |  | const

Compute the row of a scrolled widget that a user clicked in.

Parameters

  clickpos | Vertical position of the mouse click (without taking scrolling into account).
  w | The window the click was in.
  widget | Widget number of the widget clicked in.
  padding | Amount of empty space between the widget edge and the top of the first row. Default value is `0`.
  line_height | Height of a single row. A negative value means using the vertical resize step of the widget.

Returns
  Row number clicked at. If clicked at a wrong position, #INT_MAX is returned.

Definition at line 2260 of file widget.cpp.

References GetCount(), GetPosition(), Window::GetRowFromWidget(), and pos.

Referenced by NetworkClientListWindow::GetButtonAtPoint(), and GetScrolledItemFromWidget().

## ◆ IsVertical()

bool Scrollbar::IsVertical | ( |  | ) | const
| inline

Is the scrollbar vertical or not?

Returns
  True iff the scrollbar is vertical.

Definition at line 739 of file widget_type.h.

References is_vertical.

Referenced by NWidgetMatrix::GetScrollOffsets(), SetCapacityFromWidget(), and NWidgetMatrix::SetCount().

## ◆ IsVisible()

bool Scrollbar::IsVisible | ( | uint16_t | item | ) | const
| inline

Checks whether given current item is visible in the list.

Parameters

  item | to check

Returns
  true iff the item is visible

Definition at line 730 of file widget_type.h.

Referenced by TimetableWindow::DrawArrivalDeparturePanel(), and OrdersWindow::OnInvalidateData().

## ◆ ScrollTowards()

void Scrollbar::ScrollTowards | ( | int | position | ) |
| inline

Scroll towards the given position; if the item is visible nothing happens, otherwise it will be shown either at the bottom or top of the window depending on where in the list it was.

Parameters

  position | the position to scroll towards.

Definition at line 821 of file widget_type.h.

References GetCapacity(), GetPosition(), and SetPosition().

Referenced by BuildObjectWindow::EnsureSelectedObjectClassIsVisible(), BuildRailStationWindow::EnsureSelectedStationClassIsVisible(), ReplaceVehicleWindow::GenerateLists(), OrdersWindow::OnInvalidateData(), NetworkContentListWindow::ScrollToSelected(), VehicleGroupWindow::SelectGroup(), NWidgetMatrix::SetClicked(), and NewGRFWindow::UpdateScrollBars().

## ◆ SetCapacity()

void Scrollbar::SetCapacity | ( | size_t | capacity | ) |
| inline

Set the capacity of visible elements.

Parameters

  capacity | the new capacity

Note
  updates the position if needed

Definition at line 774 of file widget_type.h.

References MAX_UVALUE.

Referenced by DepotWindow::OnResize(), SetCapacityFromWidget(), NWidgetMatrix::SetCount(), and DropdownWindow::UpdateSizeAndPosition().

## ◆ SetCapacityFromWidget()

void Scrollbar::SetCapacityFromWidget | ( | Window * | w,
|  | WidgetID | widget,
|  | int | padding = `0`
| ) |  |

Set capacity of visible elements from the size and resize properties of a widget.

Parameters

  w | Window.
  widget | Widget with size and resize properties.
  padding | Padding to subtract from the size.

Note
  Updates the position if needed.

Definition at line 2334 of file widget.cpp.

References NWidgetBase::current_x, NWidgetBase::current_y, Window::GetWidget(), IsVertical(), NWidgetBase::resize_x, NWidgetBase::resize_y, and SetCapacity().

Referenced by TextfileWindow::OnResize(), ScriptListWindow::OnResize(), SubsidyListWindow::OnResize(), SignListWindow::OnResize(), GoalListWindow::OnResize(), BuildBridgeWindow::OnResize(), GSConfigWindow::OnResize(), NewGRFParametersWindow::OnResize(), BuildObjectWindow::OnResize(), ScriptSettingsWindow::OnResize(), NewGRFInspectWindow::OnResize(), CompanyStationsWindow::OnResize(), BuildIndustryWindow::OnResize(), ReplaceVehicleWindow::OnResize(), SaveLoadWindow::OnResize(), TimetableWindow::OnResize(), StoryBookWindow::OnResize(), NewGRFWindow::OnResize(), NetworkGameWindow::OnResize(), NetworkContentListWindow::OnResize(), VehicleGroupWindow::OnResize(), TownDirectoryWindow::OnResize(), SelectCompanyLiveryWindow::OnResize(), SpriteAlignerWindow::OnResize(), PaymentRatesGraphWindow::OnResize(), DepotWindow::OnResize(), MessageHistoryWindow::OnResize(), ScriptDebugWindow::OnResize(), RefitWindow::OnResize(), BuildRailStationWindow::OnResize(), BuildRoadStationWindow::OnResize(), OrdersWindow::OnResize(), NetworkClientListWindow::OnResize(), IndustryDirectoryWindow::OnResize(), BuildVehicleWindow::OnResize(), StationViewWindow::OnResize(), SavePresetWindow::OnResize(), VehicleListWindow::OnResize(), SelectStationWindow< T >::OnResize(), VehicleDetailsWindow::OnResize(), GameSettingsWindow::OnResize(), and IndustryCargoesWindow::OnResize().

## ◆ SetCount()

void Scrollbar::SetCount | ( | size_t | num | ) |
| inline

Sets the number of elements in the list.

Parameters

  num | the number of elements in the list

Note
  updates the position if needed

Definition at line 760 of file widget_type.h.

References MAX_UVALUE.

Referenced by NetworkContentListWindow::BuildContentList(), BuildObjectWindow::BuildObjectClassesAvailable(), IndustryDirectoryWindow::BuildSortIndustriesList(), BuildRailStationWindow::BuildStationClassesAvailable(), CompanyStationsWindow::BuildStationsList(), IndustryCargoesWindow::ComputeCargoDisplay(), IndustryCargoesWindow::ComputeIndustryDisplay(), BuildRailStationWindow::EnsureSelectedStationClassIsVisible(), ReplaceVehicleWindow::GenerateLists(), SubsidyListWindow::OnInvalidateData(), GoalListWindow::OnInvalidateData(), NewGRFParametersWindow::OnInvalidateData(), OrdersWindow::OnInvalidateData(), SpriteAlignerWindow::OnInvalidateData(), ScriptDebugWindow::OnInvalidateData(), MessageHistoryWindow::OnInvalidateData(), SelectStationWindow< T >::OnInvalidateData(), GameSettingsWindow::OnInvalidateData(), TimetableWindow::OnPaint(), VehicleGroupWindow::OnPaint(), StoryBookWindow::OnPaint(), RefitWindow::OnPaint(), StationViewWindow::OnPaint(), BuildVehicleWindow::OnPaint(), VehicleDetailsWindow::OnPaint(), StoryBookWindow::OnResize(), GSConfigWindow::RebuildVisibleSettings(), ScriptSettingsWindow::RebuildVisibleSettings(), StoryBookWindow::RefreshSelectedPage(), SavePresetWindow::SavePresetWindow(), ScriptListWindow::ScriptListWindow(), BuildAirportWindow::SelectFirstAvailableAirport(), VehicleGroupWindow::SelectGroup(), NWidgetMatrix::SetCount(), TextfileWindow::SetupScrollbars(), ScriptDebugWindow::UpdateLogScroll(), NewGRFWindow::UpdateScrollBars(), and DropdownWindow::UpdateSizeAndPosition().

## ◆ SetPosition()

bool Scrollbar::SetPosition | ( | int | position | ) |
| inline

Sets the position of the first visible element.

Parameters

  position | the position of the element

Returns
  true iff the position has changed

Definition at line 790 of file widget_type.h.

Referenced by ReplaceVehicleWindow::OnRailRoadTypeChange(), ScrollTowards(), ScriptDebugWindow::UpdateLogScroll(), and UpdatePosition().

## ◆ SetStepSize()

void Scrollbar::SetStepSize | ( | size_t | stepsize | ) |
| inline

Set the distance to scroll when using the buttons or the wheel.

Parameters

  stepsize | Scrolling speed.

Definition at line 748 of file widget_type.h.

References stepsize.

Referenced by ScriptDebugWindow::ScriptDebugWindow(), and NWidgetMatrix::SetCount().

## ◆ UpdateListPositionOnKeyPress()

EventState Scrollbar::UpdateListPositionOnKeyPress | ( | int & | list_position,
|  | uint16_t | keycode
| ) |  | const

Update the given list position as if it were on this scroll bar when the given keycode was pressed.

This does not update the actual position of this scroll bar, that is left to the caller. It does, however use the capacity and count of the scroll bar for the bounds and amount to scroll.

When the count is 0 or the return is ES_NOT_HANDLED, then the position is not updated. With WKC_UP and WKC_DOWN the position goes one up or down respectively. With WKC_PAGEUP and WKC_PAGEDOWN the position goes one capacity up or down respectively. With WKC_HOME the first position is selected and with WKC_END the last position is selected. This function ensures that pos is in the range [0..count).

Parameters

  list_position | The current position in the list.
  key_code | The pressed key code.

Returns
  ES_NOT_HANDLED when another key than the 6 specific keys was pressed, otherwise ES_HANDLED.

Definition at line 2281 of file widget.cpp.

## ◆ UpdatePosition()

bool Scrollbar::UpdatePosition | ( | int | difference,
|  | ScrollbarStepping | unit = `SS_SMALL`
| ) |  |
| inline

Updates the position of the first visible element by the given amount.

If the position would be too low or high it will be clamped appropriately

Parameters

  difference | the amount of change requested
  unit | The stepping unit of difference

Returns
  true iff the position has changed

Definition at line 804 of file widget_type.h.

References cap, SetPosition(), SS_BIG, SS_SMALL, and stepsize.

Referenced by DispatchMouseWheelEvent(), and DropdownWindow::UpdateSizeAndPosition().

The documentation for this class was generated from the following files:

- src/widget_type.h
- src/widget.cpp