OpenTTD Source 20260311-master-g511d3794ce
dropdown_type.h
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#ifndef DROPDOWN_TYPE_H
11#define DROPDOWN_TYPE_H
12
13#include "core/enum_type.hpp"
14#include "stringfilter_type.h"
15#include "window_type.h"
16#include "gfx_func.h"
17#include "gfx_type.h"
18#include "palette_func.h"
19#include "window_gui.h"
20
24class DropDownListItem {
25public:
26 int result;
27 bool masked;
28 bool shaded;
29
30 explicit DropDownListItem(int result, bool masked = false, bool shaded = false) : result(result), masked(masked), shaded(shaded) {}
32 virtual ~DropDownListItem() = default;
33
38 virtual void FilterText([[maybe_unused]] StringFilter &string_filter) const {}
39
44 virtual bool Selectable() const { return true; }
45
50 virtual uint Height() const { return 0; }
51
56 virtual uint Width() const { return 0; }
57
64 virtual int OnClick([[maybe_unused]] const Rect &r, [[maybe_unused]] const Point &pt) const
65 {
66 return -1;
67 }
68
77 virtual void Draw(const Rect &full, [[maybe_unused]] const Rect &r, [[maybe_unused]] bool sel, [[maybe_unused]] int click_result, Colours bg_colour) const
78 {
79 if (this->masked) GfxFillRect(full, GetColourGradient(bg_colour, SHADE_LIGHT), FILLRECT_CHECKER);
80 }
81
87 TextColour GetColour(bool sel) const
88 {
89 if (this->shaded) return (sel ? TC_SILVER : TC_GREY) | TC_NO_SHADE;
90 return sel ? TC_WHITE : TC_BLACK;
91 }
92};
93
97typedef std::vector<std::unique_ptr<const DropDownListItem>> DropDownList;
98
105using DropDownOptions = EnumBitSet<DropDownOption, uint8_t>;
106
107void ShowDropDownListAt(Window *w, DropDownList &&list, int selected, WidgetID button, Rect wi_rect, Colours wi_colour, DropDownOptions options = {}, std::string * const persistent_filter_text = nullptr);
108
109void ShowDropDownList(Window *w, DropDownList &&list, int selected, WidgetID button, uint width = 0, DropDownOptions options = {}, std::string * const persistent_filter_text = nullptr);
110
112
113void ReplaceDropDownList(Window *parent, DropDownList &&list, std::optional<int> selected_result = std::nullopt);
114
115#endif /* DROPDOWN_TYPE_H */
virtual int OnClick(const Rect &r, const Point &pt) const
Callback when this item is clicked.
bool masked
Masked and unselectable item.
TextColour GetColour(bool sel) const
Get the colour of the text.
virtual bool Selectable() const
Can this dropdown item be selected?
int result
Result value to return to window on selection.
virtual uint Width() const
The width of this item.
bool shaded
Shaded item, affects text colour.
virtual ~DropDownListItem()=default
Ensure the destructor of the sub classes are called as well.
virtual void Draw(const Rect &full, const Rect &r, bool sel, int click_result, Colours bg_colour) const
Callback for drawing this item.
virtual void FilterText(StringFilter &string_filter) const
Add text from this dropdown item to a string filter.
virtual uint Height() const
The height of this item.
Enum-as-bit-set wrapper.
std::vector< std::unique_ptr< const DropDownListItem > > DropDownList
A drop down list is a collection of drop down list items.
void ShowDropDownListAt(Window *w, DropDownList &&list, int selected, WidgetID button, Rect wi_rect, Colours wi_colour, DropDownOptions options={}, std::string *const persistent_filter_text=nullptr)
Show a drop down list.
Definition dropdown.cpp:568
DropDownOption
Configuration options for the created DropDownLists.
@ InstantClose
Set if releasing mouse button should close the list regardless of where the cursor is.
@ Persist
Set if this dropdown should stay open after an option is selected.
@ Filterable
Set if the dropdown is filterable.
void ShowDropDownList(Window *w, DropDownList &&list, int selected, WidgetID button, uint width=0, DropDownOptions options={}, std::string *const persistent_filter_text=nullptr)
Show a drop down list.
Definition dropdown.cpp:585
Dimension GetDropDownListDimension(const DropDownList &list)
Determine width and height required to fully display a DropDownList.
Definition dropdown.cpp:545
Type (helpers) for enums.
void GfxFillRect(int left, int top, int right, int bottom, const std::variant< PixelColour, PaletteID > &colour, FillRectMode mode)
Applies a certain FillRectMode-operation to a rectangle [left, right] x [top, bottom] on the screen.
Definition gfx.cpp:116
Functions related to the gfx engine.
Types related to the graphics and/or input devices.
TextColour
Colour of the strings, see _string_colourmap in table/string_colours.h or docs/ottd-colourtext-palett...
Definition gfx_type.h:307
@ TC_NO_SHADE
Do not add shading to this text colour.
Definition gfx_type.h:331
@ FILLRECT_CHECKER
Draw only every second pixel, used for greying-out.
Definition gfx_type.h:346
#define Rect
Macro that prevents name conflicts between included headers.
#define Point
Macro that prevents name conflicts between included headers.
PixelColour GetColourGradient(Colours colour, ColourShade shade)
Get colour gradient palette index.
Definition palette.cpp:393
Functions related to palettes.
Searching and filtering using a stringterm.
Dimensions (a width and height) of a rectangle in 2D.
String filter and state.
Data structure for an opened window.
Definition window_gui.h:274
Functions, definitions and such used only by the GUI.
Types related to windows.
int WidgetID
Widget ID.
Definition window_type.h:20