OpenTTD Source 20260311-master-g511d3794ce
dropdown.cpp File Reference

Implementation of the dropdown widget. More...

#include "stdafx.h"
#include "core/backup_type.hpp"
#include "dropdown_type.h"
#include "dropdown_func.h"
#include "stringfilter_type.h"
#include "querystring_gui.h"
#include "strings_func.h"
#include "sound_func.h"
#include "timer/timer.h"
#include "timer/timer_window.h"
#include "window_gui.h"
#include "window_func.h"
#include "zoom_func.h"
#include "widgets/dropdown_widget.h"
#include "table/strings.h"
#include "dropdown_common_type.h"
#include "safeguards.h"

Go to the source code of this file.

Data Structures

struct  DropdownWindow
 Drop-down menu window. More...

Functions

std::unique_ptr< DropDownListItemMakeDropDownListDividerItem ()
 Creates new DropDownListDividerItem.
std::unique_ptr< DropDownListItemMakeDropDownListStringItem (StringID str, int value, bool masked, bool shaded)
 Creates new DropDownListStringItem.
std::unique_ptr< DropDownListItemMakeDropDownListStringItem (std::string &&str, int value, bool masked, bool shaded)
 Creates new DropDownListStringItem.
std::unique_ptr< DropDownListItemMakeDropDownListIconItem (SpriteID sprite, PaletteID palette, StringID str, int value, bool masked, bool shaded)
 Creates new DropDownListIconItem.
std::unique_ptr< DropDownListItemMakeDropDownListIconItem (const Dimension &dim, SpriteID sprite, PaletteID palette, StringID str, int value, bool masked, bool shaded)
 Creates new DropDownListIconItem.
std::unique_ptr< DropDownListItemMakeDropDownListCheckedItem (bool checked, StringID str, int value, bool masked, bool shaded, uint indent)
 Creates new DropDownListCheckedItem.
void ReplaceDropDownList (Window *parent, DropDownList &&list, std::optional< int > selected_result)
Dimension GetDropDownListDimension (const DropDownList &list)
 Determine width and height required to fully display a DropDownList.
void ShowDropDownListAt (Window *w, DropDownList &&list, int selected, WidgetID button, Rect wi_rect, Colours wi_colour, DropDownOptions options, std::string *const persistent_filter_text)
 Show a drop down list.
void ShowDropDownList (Window *w, DropDownList &&list, int selected, WidgetID button, uint width, DropDownOptions options, std::string *const persistent_filter_text)
 Show a drop down list.
void ShowDropDownMenu (Window *w, std::span< const StringID > strings, int selected, WidgetID button, uint32_t disabled_mask, uint32_t hidden_mask, uint width, DropDownOptions options, std::string *const persistent_filter_text)
 Show a dropdown menu window near a widget of the parent window.

Variables

static constexpr std::initializer_list< NWidgetPart_nested_dropdown_menu_widgets
static WindowDesc _dropdown_desc (WDP_MANUAL, {}, 0, 0, WC_DROPDOWN_MENU, WC_NONE, {}, _nested_dropdown_menu_widgets)
 Window description for dropdown menus.

Detailed Description

Implementation of the dropdown widget.

Definition in file dropdown.cpp.

Function Documentation

◆ GetDropDownListDimension()

Dimension GetDropDownListDimension ( const DropDownList & list)

Determine width and height required to fully display a DropDownList.

Parameters
listThe list.
Returns
Dimension required to display the list.

Definition at line 545 of file dropdown.cpp.

References WidgetDimensions::scaled.

Referenced by DropdownWindow::UpdateSizeAndPosition(), BuildVehicleWindow::UpdateWidgetSize(), GameOptionsWindow::UpdateWidgetSize(), VehicleGroupWindow::UpdateWidgetSize(), and VehicleListWindow::UpdateWidgetSize().

◆ MakeDropDownListCheckedItem()

std::unique_ptr< DropDownListItem > MakeDropDownListCheckedItem ( bool checked,
StringID str,
int value,
bool masked,
bool shaded,
uint indent )

Creates new DropDownListCheckedItem.

Parameters
checkedWhether the tick before the string should be visible or not.
strThe string to show on this item in the drop down menu.
valueThe value to use when the item becomes selected.
maskedWhether the item should be masked out.
shadedWhether the item should be shaded.
indentBy what factor the tick and string should be indent.
Returns
Unique pointer to newly created drop down item.

Definition at line 94 of file dropdown.cpp.

References GetString().

Referenced by MakeDropDownListCheckedItem(), and ToolbarOptionsClick().

◆ MakeDropDownListDividerItem()

std::unique_ptr< DropDownListItem > MakeDropDownListDividerItem ( )

◆ MakeDropDownListIconItem() [1/2]

std::unique_ptr< DropDownListItem > MakeDropDownListIconItem ( const Dimension & dim,
SpriteID sprite,
PaletteID palette,
StringID str,
int value,
bool masked,
bool shaded )

Creates new DropDownListIconItem.

Parameters
spriteThe sprite id to use as an icon on the side of the string.
paletteThe palette to use when drawing icon.
strThe string to show on this item in the drop down menu.
valueThe value to use when the item becomes selected.
maskedWhether the item should be masked out.
shadedWhether the item should be shaded.
Returns
Unique pointer to newly created drop down item.
Parameters
dimThe rect specifying what part from the sprite should be used as an icon.

Definition at line 79 of file dropdown.cpp.

References GetString().

◆ MakeDropDownListIconItem() [2/2]

std::unique_ptr< DropDownListItem > MakeDropDownListIconItem ( SpriteID sprite,
PaletteID palette,
StringID str,
int value,
bool masked,
bool shaded )

Creates new DropDownListIconItem.

Parameters
spriteThe sprite id to use as an icon on the side of the string.
paletteThe palette to use when drawing icon.
strThe string to show on this item in the drop down menu.
valueThe value to use when the item becomes selected.
maskedWhether the item should be masked out.
shadedWhether the item should be shaded.
Returns
Unique pointer to newly created drop down item.

Definition at line 70 of file dropdown.cpp.

References GetString().

Referenced by BaseVehicleListWindow::BuildCargoDropDownList(), NWidgetBadgeFilter::GetDropDownList(), MakeDropDownListIconItem(), MakeDropDownListIconItem(), and IndustryCargoesWindow::OnClick().

◆ MakeDropDownListStringItem() [1/2]

std::unique_ptr< DropDownListItem > MakeDropDownListStringItem ( std::string && str,
int value,
bool masked,
bool shaded )

Creates new DropDownListStringItem.

Parameters
strThe string to show on this item in the drop down menu.
valueThe value to use when the item becomes selected.
maskedWhether the item should be masked out.
shadedWhether the item should be shaded.
Returns
Unique pointer to newly created drop down item.

Definition at line 55 of file dropdown.cpp.

◆ MakeDropDownListStringItem() [2/2]

◆ ReplaceDropDownList()

void ReplaceDropDownList ( Window * parent,
DropDownList && list,
std::optional< int > selected_result )

Definition at line 534 of file dropdown.cpp.

◆ ShowDropDownList()

void ShowDropDownList ( Window * w,
DropDownList && list,
int selected,
WidgetID button,
uint width,
DropDownOptions options,
std::string *const persistent_filter_text )

Show a drop down list.

Parameters
wParent window for the list.
listPrepopulated DropDownList.
selectedThe initially selected list item.
buttonThe widget within the parent window that is used to determine the list's location.
widthOverride the minimum width determined by the selected widget and list contents.
optionsDrop Down options for this menu.
persistent_filter_textOptional pointer to string for retaining filter text.

Definition at line 585 of file dropdown.cpp.

References _current_text_dir, NWidgetCore::colour, NWidgetCore::disp_flags, DropdownActive, Window::GetWidget(), NWID_BUTTON_DROPDOWN, Rect, ScaleGUITrad(), BaseBitSet< Timpl, Tvalue_type, Tstorage, Tmask >::Set(), NWidgetBase::SetDirty(), NWidgetCore::SetLowered(), ShowDropDownListAt(), SndClickBeep(), TD_RTL, and NWidgetBase::type.

Referenced by BuildAirportWindow::OnClick(), BuildVehicleWindow::OnClick(), CompanyStationsWindow::OnClick(), CreateScenarioWindow::OnClick(), EnginePreviewWindow::OnClick(), GameOptionsWindow::OnClick(), GenerateLandscapeWindow::OnClick(), IndustryCargoesWindow::OnClick(), IndustryDirectoryWindow::OnClick(), MusicTrackSelectionWindow::OnClick(), NetworkClientListWindow::OnClick(), NetworkStartServerWindow::OnClick(), NewGRFWindow::OnClick(), OrdersWindow::OnClick(), PickerWindow::OnClick(), ReplaceVehicleWindow::OnClick(), StoryBookWindow::OnClick(), TextfileWindow::OnClick(), VehicleGroupWindow::OnClick(), VehicleListWindow::OnClick(), PopupMainToolbarMenu(), SetDateWindow::ShowDateDropDown(), ShowDropDownMenu(), and ToolbarOptionsClick().

◆ ShowDropDownListAt()

void ShowDropDownListAt ( Window * w,
DropDownList && list,
int selected,
WidgetID button,
Rect wi_rect,
Colours wi_colour,
DropDownOptions options,
std::string *const persistent_filter_text )

Show a drop down list.

Parameters
wParent window for the list.
listPrepopulated DropDownList.
selectedThe initially selected list item.
buttonThe widget which is passed to Window::OnDropdownSelect and OnDropdownClose. Unless you override those functions, this should be then widget index of the dropdown button.
wi_rectCoord of the parent drop down button, used to position the dropdown menu.
wi_colourColour of the parent widget.
optionsDrop Down options for this menu.
persistent_filter_textOptional pointer to string for retaining filter text.

Definition at line 568 of file dropdown.cpp.

References CloseWindowByClass(), Rect, and WC_DROPDOWN_MENU.

Referenced by GSConfigWindow::OnClick(), NewGRFParametersWindow::OnClick(), ScriptSettingsWindow::OnClick(), NetworkClientListWindow::OnClickClientAdmin(), NetworkClientListWindow::OnClickCompanyAdmin(), and ShowDropDownList().

◆ ShowDropDownMenu()

void ShowDropDownMenu ( Window * w,
std::span< const StringID > strings,
int selected,
WidgetID button,
uint32_t disabled_mask,
uint32_t hidden_mask,
uint width,
DropDownOptions options,
std::string *const persistent_filter_text )

Show a dropdown menu window near a widget of the parent window.

The result code of the items is their index in the strings list.

Parameters
wParent window that wants the dropdown menu.
stringsMenu list.
selectedIndex of initial selected item.
buttonButton widget number of the parent window w that wants the dropdown menu.
disabled_maskBitmask for disabled items (items with their bit set are displayed, but not selectable in the dropdown list).
hidden_maskBitmask for hidden items (items with their bit set are not copied to the dropdown list).
widthMinimum width of the dropdown menu.
optionsDrop Down options for this menu
persistent_filter_textOptional pointer to string for retaining filter text.

Definition at line 627 of file dropdown.cpp.

References HasBit(), MakeDropDownListStringItem(), and ShowDropDownList().

Referenced by DisplayVehicleSortDropDown(), BuildBridgeWindow::OnClick(), CompanyStationsWindow::OnClick(), GenerateLandscapeWindow::OnClick(), IndustryDirectoryWindow::OnClick(), OrdersWindow::OnClick(), ReplaceVehicleWindow::OnClick(), StationViewWindow::OnClick(), TownDirectoryWindow::OnClick(), VehicleDetailsWindow::OnClick(), VehicleGroupWindow::OnClick(), and VehicleListWindow::OnClick().

Variable Documentation

◆ _nested_dropdown_menu_widgets

std::initializer_list<NWidgetPart> _nested_dropdown_menu_widgets
staticconstexpr
Initial value:
= {
NWidget(WWT_EDITBOX, COLOUR_END, WID_DM_FILTER), SetResize(1, 0), SetFill(1, 0), SetPadding(2), SetStringTip(STR_LIST_FILTER_OSKTITLE, STR_LIST_FILTER_TOOLTIP),
}
@ WID_DM_FILTER_SEL
Selection for item filter.
@ WID_DM_FILTER
Item filter.
@ WID_DM_FILTER_PANEL
Panel for item filter.
@ WID_DM_ITEMS
Panel showing the dropdown items.
@ WID_DM_SCROLL
Scrollbar.
@ WID_DM_SHOW_SCROLL
Hide scrollbar if too few items.
constexpr NWidgetPart SetFill(uint16_t fill_x, uint16_t fill_y)
Widget part function for setting filling.
constexpr NWidgetPart SetScrollbar(WidgetID index)
Attach a scrollbar to a widget.
constexpr NWidgetPart SetPadding(uint8_t top, uint8_t right, uint8_t bottom, uint8_t left)
Widget part function for setting additional space around a widget.
constexpr NWidgetPart SetStringTip(StringID string, StringID tip={})
Widget part function for setting the string and tooltip.
constexpr NWidgetPart EndContainer()
Widget part function for denoting the end of a container (horizontal, vertical, WWT_FRAME,...
constexpr NWidgetPart NWidget(WidgetType tp, Colours col, WidgetID idx=INVALID_WIDGET)
Widget part function for starting a new 'real' widget.
constexpr NWidgetPart SetResize(int16_t dx, int16_t dy)
Widget part function for setting the resize step.
@ WWT_EDITBOX
a textbox for typing
Definition widget_type.h:62
@ NWID_HORIZONTAL
Horizontal container.
Definition widget_type.h:66
@ WWT_PANEL
Simple depressed panel.
Definition widget_type.h:39
@ NWID_VSCROLLBAR
Vertical scrollbar.
Definition widget_type.h:76
@ NWID_VERTICAL
Vertical container.
Definition widget_type.h:68
@ NWID_SELECTION
Stacked widgets, only one visible at a time (eg in a panel with tabs).
Definition widget_type.h:71

Definition at line 99 of file dropdown.cpp.