Go to the documentation of this file.
10 #include "../stdafx.h"
11 #include "../window_gui.h"
12 #include "../string_func.h"
13 #include "../strings_func.h"
14 #include "../window_func.h"
15 #include "../guitimer_func.h"
16 #include "../zoom_func.h"
21 #include "../safeguards.h"
24 void DropDownListItem::Draw(
const Rect &r,
bool sel, Colours bg_colour)
const
34 uint DropDownListStringItem::Width()
const
37 GetString(buffer, this->String(),
lastof(buffer));
41 void DropDownListStringItem::Draw(
const Rect &r,
bool sel, Colours bg_colour)
const
44 DrawString(ir.left, ir.right, r.top, this->String(), sel ? TC_WHITE : TC_BLACK);
56 char buffer1[512], buffer2[512];
62 StringID DropDownListParamStringItem::String()
const
68 StringID DropDownListCharStringItem::String()
const
77 this->sprite_y = dim.height;
80 uint DropDownListIconItem::Height(uint width)
const
85 uint DropDownListIconItem::Width()
const
90 void DropDownListIconItem::Draw(
const Rect &r,
bool sel, Colours bg_colour)
const
99 void DropDownListIconItem::SetDimension(
Dimension d)
104 static const NWidgetPart _nested_dropdown_menu_widgets[] = {
117 _nested_dropdown_menu_widgets,
lengthof(_nested_dropdown_menu_widgets)
148 assert(this->list.size() > 0);
156 uint items_width = size.width - (scroll ? NWidgetScrollbar::GetVerticalDimension().width : 0);
171 for (
const auto &item : this->list) {
172 list_height += item->Height(items_width);
176 this->vscroll->
SetCapacity(size.height * (uint16)this->list.size() / list_height);
177 this->vscroll->
SetCount((uint16)this->list.size());
180 this->parent_button = button;
181 this->selected_index = selected;
182 this->click_delay = 0;
183 this->drag_mode =
true;
207 this->instant_close =
false;
231 for (
const auto &item : this->list) {
233 if (--pos >= 0)
continue;
235 int item_height = item->Height(
width);
237 if (y < item_height) {
238 if (item->masked || !item->Selectable())
return false;
239 value = item->result;
253 Colours colour = this->GetWidget<NWidgetCore>(widget)->colour;
258 for (
const auto &item : this->list) {
259 int item_height = item->Height(ir.
Width());
262 if (--pos >= 0)
continue;
264 if (y + item_height - 1 <= ir.bottom) {
265 bool selected = (this->selected_index == item->result);
268 item->Draw({ir.left, y, ir.right, y + item_height - 1}, selected, colour);
283 this->click_delay = 4;
284 this->selected_index = item;
291 if (!this->scrolling_timer.
Elapsed(delta_ms))
return;
294 if (this->scrolling != 0) {
308 if (this->click_delay != 0 && --this->click_delay == 0) {
316 if (this->drag_mode) {
320 this->drag_mode =
false;
322 if (this->instant_close) this->
Close();
325 this->click_delay = 2;
327 if (_cursor.
pos.y <= this->top + 2) {
329 this->scrolling = -1;
331 }
else if (_cursor.
pos.y >= this->top + this->height - 2) {
340 if (this->selected_index != item) {
341 this->selected_index = item;
366 int top = w->
top + wi_rect.bottom + 1;
369 uint width = wi_rect.
Width();
372 uint max_item_width = 0;
377 for (
const auto &item : list) {
378 height += item->Height(width);
379 if (auto_width) max_item_width = std::max(max_item_width, item->Width());
394 if (height > available_height) {
399 if (available_height_above > available_height) {
401 available_height = available_height_above;
405 if (height > available_height) {
407 uint avg_height = height / (uint)list.size();
410 uint rows = std::max<uint>(available_height / avg_height, 1);
411 height = rows * avg_height;
414 max_item_width += NWidgetScrollbar::GetVerticalDimension().width;
423 if (auto_width) width = std::max(width, max_item_width);
452 Rect wi_rect = nwi->GetCurrentRect();
453 Colours wi_colour = nwi->
colour;
470 ShowDropDownListAt(w, std::move(list), selected, button, wi_rect, wi_colour, auto_width, instant_close);
489 if (!
HasBit(hidden_mask, i)) {
494 if (!list.empty())
ShowDropDownList(w, std::move(list), selected, button, width);
DropdownWindow(Window *parent, DropDownList &&list, int selected, int button, bool instant_close, const Point &position, const Dimension &size, Colours wi_colour, bool scroll)
Create a dropdown menu.
int parent_button
Parent widget number where the window is dropped from.
StringID string
String ID of item.
String list item with parameters.
Dimensions (a width and height) of a rectangle in 2D.
const Scrollbar * GetScrollbar(uint widnum) const
Return the Scrollbar to a widget index.
Rect Shrink(int s) const
Copy and shrink Rect by s pixels.
int selected_index
Index of the selected item in the list.
std::vector< std::unique_ptr< const DropDownListItem > > DropDownList
A drop down list is a collection of drop down list items.
void CreateNestedTree(bool fill_nested=true)
Perform the first part of the initialization of a nested widget tree.
void OnRealtimeTick(uint delta_ms) override
Called periodically.
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
Point OnInitialPosition(int16 sm_width, int16 sm_height, int window_number) override
Compute the initial position of the window.
@ WDF_NO_FOCUS
This window won't get focus/make any other window lose focus when click.
@ FILLRECT_CHECKER
Draw only every second pixel, used for greying-out.
int DrawString(int left, int right, int top, const char *str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
Draw string, possibly truncated to make it fit in its allocated space.
virtual void OnDropdownClose(Point pt, int widget, int index, bool instant_close)
A dropdown window associated to this window has been closed.
void DrawWidget(const Rect &r, int widget) const override
Draw the contents of a nested widget.
byte _colour_gradient[COLOUR_END][8]
All 16 colour gradients 8 colours per gradient from darkest (0) to lightest (7)
static void SetDParam(uint n, uint64 v)
Set a string parameter v at index n in the global string parameter array.
void Close() override
Hide the window and all its child windows, and mark them for a later deletion.
Dimension GetStringBoundingBox(const char *str, FontSize start_fontsize)
Return the string dimension in pixels.
void OnClick(Point pt, int widget, int click_count) override
A click with the left mouse button has been made on the window.
GUITimer scrolling_timer
Timer for auto-scroll of the item list.
uint32 SpriteID
The number of a sprite, without mapping bits and colourtables.
High level window description.
const NWID * GetWidget(uint widnum) const
Get the nested widget with number widnum from the nested widget tree.
@ WF_WHITE_BORDER
Window white border counter bit mask.
void SetDirty() const
Mark entire window as dirty (in need of re-paint)
bool GetDropDownItem(int &value)
Find the dropdown item under the cursor.
uint Horizontal() const
Get total horizontal padding of RectPadding.
void ShowDropDownList(Window *w, DropDownList &&list, int selected, int button, uint width, bool auto_width, bool instant_close)
Show a drop down list.
byte click_delay
Timer to delay selection.
int GetMainViewTop()
Return the top of the main view available for general use.
virtual void OnDropdownSelect(int widget, int index)
A dropdown option associated to this window has been selected.
const DropDownList list
List with dropdown menu items.
Window * parent
Parent window.
int left
x position of left edge of the window
WindowFlags flags
Window flags.
Rect Indent(int indent, bool end) const
Copy Rect and indent it from its position.
Rect WithWidth(int width, bool end) const
Copy Rect and set its width.
void DrawSprite(SpriteID img, PaletteID pal, int x, int y, const SubSprite *sub, ZoomLevel zoom)
Draw a sprite, not in a viewport.
Coordinates of a point in 2D.
void ShowDropDownMenu(Window *w, const StringID *strings, int selected, int button, uint32 disabled_mask, uint32 hidden_mask, uint width)
Show a dropdown menu window near a widget of the parent window.
static const uint8 PC_BLACK
Black palette colour.
WindowNumber window_number
Window number within the window class.
void GfxFillRect(int left, int top, int right, int bottom, int colour, FillRectMode mode)
Applies a certain FillRectMode-operation to a rectangle [left, right] x [top, bottom] on the screen.
bool instant_close
Close the window when the mouse button is raised.
uint Vertical() const
Get total vertical padding of RectPadding.
@ WC_NONE
No window, redirects to WC_MAIN_WINDOW.
Point position
Position of the topleft corner of the window.
int scrolling
If non-zero, auto-scroll the item list (one time).
Dimension GetSpriteSize(SpriteID sprid, Point *offset, ZoomLevel zoom)
Get the size of a sprite.
uint64 decode_params[10]
Parameters of the string.
uint32 StringID
Numeric value that represents a string, independent of the selected language.
void OnFocusLost(bool closing) override
The window has lost focus.
#define FONT_HEIGHT_NORMAL
Height of characters in the normal (FS_NORMAL) font.
uint32 PaletteID
The number of the palette.
void CloseWindowByClass(WindowClass cls)
Close all windows of a given class.
void FinishInitNested(WindowNumber window_number=0)
Perform the second part of the initialization of a nested widget tree.
int top
y position of top edge of the window
bool Elapsed(uint delta)
Test if a timer has elapsed.
void ShowDropDownListAt(Window *w, DropDownList &&list, int selected, int button, Rect wi_rect, Colours wi_colour, bool auto_width, bool instant_close)
Show a drop down list.
static const uint MILLISECONDS_PER_TICK
The number of milliseconds per game tick.
static int CenterBounds(int min, int max, int size)
Determine where to draw a centred object inside a widget.
static T SetBit(T &x, const uint8 y)
Set a bit in a variable.
#define lengthof(x)
Return the length of an fixed size array.
int width
width of the window (number of pixels to the right in x direction)
@ WDP_MANUAL
Manually align the window (so no automatic location finding)
void OnMouseLoop() override
Called for every mouse loop run, which is at least once per (game) tick.
int strnatcmp(const char *s1, const char *s2, bool ignore_garbage_at_front)
Compares two strings using case insensitive natural sort.
Data structure for an opened window.
int Width() const
Get width of Rect.
void SetWidgetDirty(byte widget_index) const
Invalidate a widget, i.e.
int GetMainViewBottom()
Return the bottom of the main view available for general use.
Specification of a rectangle with absolute coordinates of all edges.
void LowerWidget(byte widget_index)
Marks a widget as lowered.
Point pos
logical mouse position
@ WC_DROPDOWN_MENU
Drop down menu; Window numbers:
#define CLRBITS(x, y)
Clears several bits in a variable.
#define lastof(x)
Get the last element of an fixed size array.
bool _left_button_clicked
Is left mouse button clicked?
@ TD_RTL
Text is written right-to-left by default.
TextDirection _current_text_dir
Text direction of the currently selected language.
void SetDParamStr(uint n, const char *str)
This function is used to "bind" a C string to a OpenTTD dparam slot.
static const StringID INVALID_STRING_ID
Constant representing an invalid string (16bit in case it is used in savegames)
virtual void Close()
Hide the window and all its child windows, and mark them for a later deletion.
static bool NatSortFunc(std::unique_ptr< const DropDownListItem > const &first, std::unique_ptr< const DropDownListItem > const &second)
Natural sorting comparator function for DropDownList::sort().