OpenTTD Source  12.0-beta2
Scrollbar Class Reference

Scrollbar data structure. More...

#include <widget_type.h>

Inheritance diagram for Scrollbar:
NWidgetScrollbar

Public Types

enum  ScrollbarStepping { SS_RAW, SS_SMALL, SS_BIG }
 Stepping sizes when scrolling. More...
 

Public Member Functions

 Scrollbar (bool is_vertical)
 
uint16 GetCount () const
 Gets the number of elements in the list. More...
 
uint16 GetCapacity () const
 Gets the number of visible elements of the scrollbar. More...
 
uint16 GetPosition () const
 Gets the position of the first visible element in the list. More...
 
bool IsVisible (uint16 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 (uint16 stepsize)
 Set the distance to scroll when using the buttons or the wheel. More...
 
void SetCount (int num)
 Sets the number of elements in the list. More...
 
void SetCapacity (int capacity)
 Set the capacity of visible elements. More...
 
void SetCapacityFromWidget (Window *w, int 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, int widget, int padding=0) const
 Compute the row of a scrolled widget that a user clicked in. More...
 
EventState UpdateListPositionOnKeyPress (int &list_position, uint16 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 count
 Number of elements in the list.
 
uint16 cap
 Number of visible elements of the scroll bar.
 
uint16 pos
 Index of first visible item of the list.
 
uint16 stepsize
 Distance to scroll, when pressing the buttons or using the wheel.
 

Detailed Description

Scrollbar data structure.

Definition at line 629 of file widget_type.h.

Member Enumeration Documentation

◆ 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 639 of file widget_type.h.

Member Function Documentation

◆ GetCapacity()

uint16 Scrollbar::GetCapacity ( ) const
inline

Gets the number of visible elements of the scrollbar.

Returns
the number of visible elements

Definition at line 662 of file widget_type.h.

References cap.

Referenced by DispatchMouseWheelEvent(), GameSettingsWindow::DrawWidget(), HandleScrollbarHittest(), IsVisible(), AIDebugWindow::OnPaint(), and ScrollTowards().

◆ GetCount()

◆ GetPosition()

◆ GetScrolledRowFromWidget()

int Scrollbar::GetScrolledRowFromWidget ( int  clickpos,
const Window *const  w,
int  widget,
int  padding = 0 
) const

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

Parameters
clickposVertical position of the mouse click (without taking scrolling into account).
wThe window the click was in.
widgetWidget number of the widget clicked in.
paddingAmount of empty space between the widget edge and the top of the first row. Default value is 0.
Returns
Row number clicked at. If clicked at a wrong position, #INT_MAX is returned.

Definition at line 2098 of file widget.cpp.

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

Referenced by NetworkClientListWindow::GetButtonAtPoint(), AIListWindow::OnClick(), SignListWindow::OnClick(), BuildBridgeWindow::OnClick(), NewGRFParametersWindow::OnClick(), CompanyStationsWindow::OnClick(), NewGRFInspectWindow::OnClick(), StoryBookWindow::OnClick(), TownDirectoryWindow::OnClick(), MessageHistoryWindow::OnClick(), BuildVehicleWindow::OnClick(), IndustryDirectoryWindow::OnClick(), VehicleListWindow::OnClick(), StationViewWindow::OnClick(), GameSettingsWindow::OnClick(), SavePresetWindow::OnClick(), NewGRFWindow::OnDragDrop(), VehicleGroupWindow::OnMouseDrag(), NewGRFWindow::OnMouseDrag(), and NetworkClientListWindow::OnTooltip().

◆ IsVertical()

bool Scrollbar::IsVertical ( ) const
inline

Is the scrollbar vertical or not?

Returns
True iff the scrollbar is vertical.

Definition at line 690 of file widget_type.h.

References is_vertical.

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

◆ IsVisible()

bool Scrollbar::IsVisible ( uint16  item) const
inline

Checks whether given current item is visible in the list.

Parameters
itemto check
Returns
true iff the item is visible

Definition at line 681 of file widget_type.h.

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

Referenced by AISettingsWindow::DrawWidget(), 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
positionthe position to scroll towards.

Definition at line 775 of file widget_type.h.

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

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

◆ SetCapacity()

void Scrollbar::SetCapacity ( int  capacity)
inline

Set the capacity of visible elements.

Parameters
capacitythe new capacity
Note
updates the position if needed

Definition at line 726 of file widget_type.h.

References MAX_UVALUE.

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

◆ SetCapacityFromWidget()

void Scrollbar::SetCapacityFromWidget ( Window w,
int  widget,
int  padding = 0 
)

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

Parameters
wWindow.
widgetWidget with size and resize properties.
paddingPadding to subtract from the size.
Note
Updates the position if needed.

Definition at line 2172 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(), SubsidyListWindow::OnResize(), AIListWindow::OnResize(), SignListWindow::OnResize(), GoalListWindow::OnResize(), BuildBridgeWindow::OnResize(), NewGRFParametersWindow::OnResize(), BuildObjectWindow::OnResize(), AISettingsWindow::OnResize(), NewGRFInspectWindow::OnResize(), TimetableWindow::OnResize(), ReplaceVehicleWindow::OnResize(), CompanyStationsWindow::OnResize(), BuildIndustryWindow::OnResize(), NewGRFWindow::OnResize(), SaveLoadWindow::OnResize(), StoryBookWindow::OnResize(), NetworkGameWindow::OnResize(), VehicleGroupWindow::OnResize(), NetworkContentListWindow::OnResize(), TownDirectoryWindow::OnResize(), SelectCompanyLiveryWindow::OnResize(), PaymentRatesGraphWindow::OnResize(), SpriteAlignerWindow::OnResize(), DepotWindow::OnResize(), RefitWindow::OnResize(), MessageHistoryWindow::OnResize(), BuildRailStationWindow::OnResize(), AIDebugWindow::OnResize(), OrdersWindow::OnResize(), BuildVehicleWindow::OnResize(), NetworkClientListWindow::OnResize(), IndustryDirectoryWindow::OnResize(), VehicleListWindow::OnResize(), StationViewWindow::OnResize(), SavePresetWindow::OnResize(), SelectStationWindow< T >::OnResize(), GameSettingsWindow::OnResize(), VehicleDetailsWindow::OnResize(), and IndustryCargoesWindow::OnResize().

◆ SetCount()

void Scrollbar::SetCount ( int  num)
inline

Sets the number of elements in the list.

Parameters
numthe number of elements in the list
Note
updates the position if needed

Definition at line 710 of file widget_type.h.

References cap, MAX_UVALUE, and pos.

Referenced by AIListWindow::AIListWindow(), NetworkContentListWindow::BuildContentList(), BuildObjectWindow::BuildObjectClassesAvailable(), IndustryDirectoryWindow::BuildSortIndustriesList(), BuildRailStationWindow::BuildStationClassesAvailable(), CompanyStationsWindow::BuildStationsList(), IndustryCargoesWindow::ComputeCargoDisplay(), IndustryCargoesWindow::ComputeIndustryDisplay(), BuildRailStationWindow::EnsureSelectedStationClassIsVisible(), ReplaceVehicleWindow::GenerateLists(), BuildAirportWindow::OnDropdownSelect(), SubsidyListWindow::OnInvalidateData(), GoalListWindow::OnInvalidateData(), NewGRFParametersWindow::OnInvalidateData(), OrdersWindow::OnInvalidateData(), SpriteAlignerWindow::OnInvalidateData(), MessageHistoryWindow::OnInvalidateData(), AIDebugWindow::OnInvalidateData(), SelectStationWindow< T >::OnInvalidateData(), GameSettingsWindow::OnInvalidateData(), TownAuthorityWindow::OnPaint(), TimetableWindow::OnPaint(), VehicleGroupWindow::OnPaint(), StoryBookWindow::OnPaint(), RefitWindow::OnPaint(), AIDebugWindow::OnPaint(), StationViewWindow::OnPaint(), BuildVehicleWindow::OnPaint(), VehicleDetailsWindow::OnPaint(), StoryBookWindow::OnResize(), AISettingsWindow::RebuildVisibleSettings(), RefitWindow::RefreshScrollbar(), SavePresetWindow::SavePresetWindow(), VehicleGroupWindow::SelectGroup(), NWidgetMatrix::SetCount(), TextfileWindow::SetupScrollbars(), and NewGRFWindow::UpdateScrollBars().

◆ SetPosition()

bool Scrollbar::SetPosition ( int  position)
inline

Sets the position of the first visible element.

Parameters
positionthe position of the element
Returns
true iff the position has changed

Definition at line 742 of file widget_type.h.

References cap, count, and pos.

Referenced by AIDebugWindow::OnPaint(), ScrollTowards(), and UpdatePosition().

◆ SetStepSize()

void Scrollbar::SetStepSize ( uint16  stepsize)
inline

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

Parameters
stepsizeScrolling speed.

Definition at line 699 of file widget_type.h.

References stepsize.

Referenced by NWidgetMatrix::SetCount().

◆ UpdateListPositionOnKeyPress()

EventState Scrollbar::UpdateListPositionOnKeyPress ( int &  list_position,
uint16  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_positionThe current position in the list.
key_codeThe pressed key code.
Returns
ES_NOT_HANDLED when another key than the 6 specific keys was pressed, otherwise ES_HANDLED.

Definition at line 2119 of file widget.cpp.

Referenced by NetworkGameWindow::OnKeyPress(), NetworkContentListWindow::OnKeyPress(), and NewGRFWindow::OnKeyPress().

◆ 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
differencethe amount of change requested
unitThe stepping unit of difference
Returns
true iff the position has changed

Definition at line 758 of file widget_type.h.

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

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


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