Codechange: Use cached town, station, industry names for list window sorting
[openttd-github.git] / src / widgets / dropdown_type.h
blobc7d3251d68d1a44910110d6d8a1b23a2d871109c
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 <http://www.gnu.org/licenses/>.
6 */
8 /** @file dropdown_type.h Types related to the drop down widget. */
10 #ifndef WIDGETS_DROPDOWN_TYPE_H
11 #define WIDGETS_DROPDOWN_TYPE_H
13 #include "../window_type.h"
14 #include "../gfx_func.h"
15 #include "../core/smallvec_type.hpp"
16 #include "table/strings.h"
18 /**
19 * Base list item class from which others are derived. If placed in a list it
20 * will appear as a horizontal line in the menu.
22 class DropDownListItem {
23 public:
24 int result; ///< Result code to return to window on selection
25 bool masked; ///< Masked and unselectable item
27 DropDownListItem(int result, bool masked) : result(result), masked(masked) {}
28 virtual ~DropDownListItem() {}
30 virtual bool Selectable() const { return false; }
31 virtual uint Height(uint width) const { return FONT_HEIGHT_NORMAL; }
32 virtual uint Width() const { return 0; }
33 virtual void Draw(int left, int right, int top, int bottom, bool sel, Colours bg_colour) const;
36 /**
37 * Common string list item.
39 class DropDownListStringItem : public DropDownListItem {
40 public:
41 StringID string; ///< String ID of item
43 DropDownListStringItem(StringID string, int result, bool masked) : DropDownListItem(result, masked), string(string) {}
45 bool Selectable() const override { return true; }
46 uint Width() const override;
47 void Draw(int left, int right, int top, int bottom, bool sel, Colours bg_colour) const override;
48 virtual StringID String() const { return this->string; }
50 static bool NatSortFunc(std::unique_ptr<const DropDownListItem> const &first, std::unique_ptr<const DropDownListItem> const &second);
53 /**
54 * String list item with parameters.
56 class DropDownListParamStringItem : public DropDownListStringItem {
57 public:
58 uint64 decode_params[10]; ///< Parameters of the string
60 DropDownListParamStringItem(StringID string, int result, bool masked) : DropDownListStringItem(string, result, masked) {}
62 StringID String() const override;
63 void SetParam(uint index, uint64 value) { decode_params[index] = value; }
66 /**
67 * List item containing a C char string.
69 class DropDownListCharStringItem : public DropDownListStringItem {
70 public:
71 const char *raw_string;
73 DropDownListCharStringItem(const char *raw_string, int result, bool masked) : DropDownListStringItem(STR_JUST_RAW_STRING, result, masked), raw_string(raw_string) {}
75 StringID String() const override;
78 /**
79 * List item with icon and string.
81 class DropDownListIconItem : public DropDownListParamStringItem {
82 SpriteID sprite;
83 PaletteID pal;
84 Dimension dim;
85 uint sprite_y;
86 uint text_y;
87 public:
88 DropDownListIconItem(SpriteID sprite, PaletteID pal, StringID string, int result, bool masked);
90 uint Height(uint width) const override;
91 uint Width() const override;
92 void Draw(int left, int right, int top, int bottom, bool sel, Colours bg_colour) const override;
93 void SetDimension(Dimension d);
96 /**
97 * A drop down list is a collection of drop down list items.
99 typedef std::vector<std::unique_ptr<const DropDownListItem>> DropDownList;
101 void ShowDropDownListAt(Window *w, DropDownList &&list, int selected, int button, Rect wi_rect, Colours wi_colour, bool auto_width = false, bool instant_close = false);
103 void ShowDropDownList(Window *w, DropDownList &&list, int selected, int button, uint width = 0, bool auto_width = false, bool instant_close = false);
105 #endif /* WIDGETS_DROPDOWN_TYPE_H */