12 #include "../stdafx.h" 13 #include "../window_gui.h" 14 #include "../string_func.h" 15 #include "../strings_func.h" 16 #include "../window_func.h" 17 #include "../guitimer_func.h" 22 #include "../safeguards.h" 25 void DropDownListItem::Draw(
int left,
int right,
int top,
int bottom,
bool sel, Colours bg_colour)
const 30 int mid = top + this->Height(0) / 2;
31 GfxFillRect(left + 1, mid - 2, right - 1, mid - 2, c1);
32 GfxFillRect(left + 1, mid - 1, right - 1, mid - 1, c2);
35 uint DropDownListStringItem::Width()
const 38 GetString(buffer, this->String(),
lastof(buffer));
42 void DropDownListStringItem::Draw(
int left,
int right,
int top,
int bottom,
bool sel, Colours bg_colour)
const 56 char buffer1[512], buffer2[512];
57 GetString(buffer1, static_cast<const DropDownListStringItem*>(first.get())->String(),
lastof(buffer1));
58 GetString(buffer2, static_cast<const DropDownListStringItem*>(second.get())->String(),
lastof(buffer2));
62 StringID DropDownListParamStringItem::String()
const 64 for (uint i = 0; i <
lengthof(this->decode_params); i++)
SetDParam(i, this->decode_params[i]);
68 StringID DropDownListCharStringItem::String()
const 78 this->sprite_y = (FONT_HEIGHT_NORMAL - dim.height) / 2;
86 uint DropDownListIconItem::Height(uint width)
const 91 uint DropDownListIconItem::Width()
const 96 void DropDownListIconItem::Draw(
int left,
int right,
int top,
int bottom,
bool sel, Colours bg_colour)
const 103 void DropDownListIconItem::SetDimension(
Dimension d)
108 static const NWidgetPart _nested_dropdown_menu_widgets[] = {
121 _nested_dropdown_menu_widgets,
lengthof(_nested_dropdown_menu_widgets)
152 :
Window(&_dropdown_desc), list(std::move(list))
154 assert(this->list.size() > 0);
156 this->position = position;
158 this->CreateNestedTree();
162 uint items_width = size.width - (scroll ? NWidgetScrollbar::GetVerticalDimension().width : 0);
172 this->FinishInitNested(0);
177 for (
const auto &item : this->list) {
178 list_height += item->Height(items_width);
182 this->vscroll->
SetCapacity(size.height * (uint16)this->list.size() / list_height);
183 this->vscroll->
SetCount((uint16)this->list.size());
187 this->parent_button = button;
188 this->selected_index = selected;
189 this->click_delay = 0;
190 this->drag_mode =
true;
191 this->instant_close = instant_close;
207 w2->
OnDropdownClose(pt, this->parent_button, this->selected_index, this->instant_close);
213 return this->position;
226 int y = _cursor.
pos.y - this->top - nwi->
pos_y - 2;
230 for (
const auto &item : this->list) {
232 if (--pos >= 0)
continue;
234 int item_height = item->Height(width);
236 if (y < item_height) {
237 if (item->masked || !item->Selectable())
return false;
238 value = item->result;
252 Colours colour = this->GetWidget<NWidgetCore>(widget)->colour;
256 for (
const auto &item : this->list) {
257 int item_height = item->Height(r.right - r.left + 1);
260 if (--pos >= 0)
continue;
262 if (y + item_height < r.bottom) {
263 bool selected = (this->selected_index == item->result);
266 item->Draw(r.left, r.right, y, y + item_height, selected, colour);
280 if (this->GetDropDownItem(item)) {
281 this->click_delay = 4;
282 this->selected_index = item;
289 if (!this->scrolling_timer.
Elapsed(delta_ms))
return;
292 if (this->scrolling != 0) {
312 if (this->click_delay != 0 && --this->click_delay == 0) {
323 if (this->drag_mode) {
327 this->drag_mode =
false;
328 if (!this->GetDropDownItem(item)) {
329 if (this->instant_close)
delete this;
332 this->click_delay = 2;
334 if (_cursor.
pos.y <= this->top + 2) {
336 this->scrolling = -1;
338 }
else if (_cursor.
pos.y >= this->top + this->height - 2) {
344 if (!this->GetDropDownItem(item))
return;
347 if (this->selected_index != item) {
348 this->selected_index = item;
373 int top = w->
top + wi_rect.bottom + 1;
376 uint width = wi_rect.right - wi_rect.left + 1;
379 uint max_item_width = 0;
384 for (
const auto &item : list) {
385 height += item->Height(width);
386 if (auto_width) max_item_width =
max(max_item_width, item->Width() + 5);
399 if (height > available_height) {
404 if (available_height_above > available_height) {
406 available_height = available_height_above;
410 if (height > available_height) {
412 uint avg_height = height / (uint)list.size();
415 assert(available_height >= avg_height);
418 uint rows = available_height / avg_height;
419 height = rows * avg_height;
422 max_item_width += NWidgetScrollbar::GetVerticalDimension().width;
427 top = w->
top + wi_rect.top - height - 4;
431 if (auto_width) width =
max(width, max_item_width);
461 wi_rect.left = nwi->
pos_x;
463 wi_rect.top = nwi->
pos_y;
465 Colours wi_colour = nwi->
colour;
476 wi_rect.left = wi_rect.right + 1 - width;
478 wi_rect.right = wi_rect.left + width - 1;
482 ShowDropDownListAt(w, std::move(list), selected, button, wi_rect, wi_colour, auto_width, instant_close);
501 if (!
HasBit(hidden_mask, i)) {
506 if (!list.empty())
ShowDropDownList(w, std::move(list), selected, button, width);
517 FOR_ALL_WINDOWS_FROM_BACK(w) {
521 assert(dw !=
nullptr);
526 return parent_button;
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.
uint32 PaletteID
The number of the palette.
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...
Point pos
logical mouse position
virtual Point OnInitialPosition(int16 sm_width, int16 sm_height, int window_number)
Compute the initial position of the window.
High level window description.
int left
x position of left edge of the window
bool instant_close
Close the window when the mouse button is raised.
void SetWidgetDirty(byte widget_index) const
Invalidate a widget, i.e.
Window * FindWindowById(WindowClass cls, WindowNumber number)
Find a window by its class and window number.
bool Elapsed(uint delta)
Test if a timer has elapsed.
byte _colour_gradient[COLOUR_END][8]
All 16 colour gradients 8 colours per gradient from darkest (0) to lightest (7)
WindowClass parent_wnd_class
Parent window class.
int selected_index
Index of the selected item in the list.
int top
y position of top edge of the window
virtual void OnMouseLoop()
Called for every mouse loop run, which is at least once per (game) tick.
#define lastof(x)
Get the last element of an fixed size array.
WindowClass
Window classes.
#define CLRBITS(x, y)
Clears several bits in a variable.
static T max(const T a, const T b)
Returns the maximum of two values.
int HideDropDownMenu(Window *pw)
Delete the drop-down menu from window pw.
bool _left_button_clicked
Is left mouse button clicked?
void ShowDropDownList(Window *w, DropDownList &&list, int selected, int button, uint width, bool auto_width, bool instant_close)
Show a drop down list.
int GetMainViewBottom()
Return the bottom of the main view available for general use.
Data structure for an opened window.
byte click_delay
Timer to delay selection.
void SetDParamStr(uint n, const char *str)
This function is used to "bind" a C string to a OpenTTD dparam slot.
std::vector< std::unique_ptr< const DropDownListItem > > DropDownList
A drop down list is a collection of drop down list items.
#define FONT_HEIGHT_NORMAL
Height of characters in the normal (FS_NORMAL) font.
bool masked
Masked and unselectable item.
static bool NatSortFunc(std::unique_ptr< const DropDownListItem > const &first, std::unique_ptr< const DropDownListItem > const &second)
Natural sorting comparator function for DropDownList::sort().
int GetMainViewTop()
Return the top of the main view available for general use.
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.
const DropDownList list
List with dropdown menu items.
static const uint MILLISECONDS_PER_TICK
The number of milliseconds per game tick.
void LowerWidget(byte widget_index)
Marks a widget as lowered.
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.
virtual void OnDropdownSelect(int widget, int index)
A dropdown option associated to this window has been selected.
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.
#define lengthof(x)
Return the length of an fixed size array.
Types related to the drop down widget.
uint32 StringID
Numeric value that represents a string, independent of the selected language.
int scrolling
If non-zero, auto-scroll the item list (one time).
static const uint8 PC_BLACK
Black palette colour.
Dimension GetStringBoundingBox(const char *str, FontSize start_fontsize)
Return the string dimension in pixels.
Draw only every second pixel, used for greying-out.
No window, redirects to WC_MAIN_WINDOW.
void DeleteWindowById(WindowClass cls, WindowNumber number, bool force)
Delete a window by its class and window number (if it is open).
WindowNumber parent_wnd_num
Parent window number.
bool GetDropDownItem(int &value)
Find the dropdown item under the cursor.
virtual void OnClick(Point pt, int widget, int click_count)
A click with the left mouse button has been made on the window.
int strnatcmp(const char *s1, const char *s2, bool ignore_garbage_at_front)
Compares two strings using case insensitive natural sort.
uint32 SpriteID
The number of a sprite, without mapping bits and colourtables.
TextDirection _current_text_dir
Text direction of the currently selected language.
int result
Result code to return to window on selection.
This window won't get focus/make any other window lose focus when click.
Point position
Position of the topleft corner of the window.
GUITimer scrolling_timer
Timer for auto-scroll of the item list.
int parent_button
Parent widget number where the window is dropped from.
virtual void DrawWidget(const Rect &r, int widget) const
Draw the contents of a nested widget.
Coordinates of a point in 2D.
Dimension GetSpriteSize(SpriteID sprid, Point *offset, ZoomLevel zoom)
Get the size of a sprite.
static const StringID INVALID_STRING_ID
Constant representing an invalid string (16bit in case it is used in savegames)
Offset at right to draw the frame rectangular area.
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
int32 WindowNumber
Number to differentiate different windows of the same class.
WindowClass window_class
Window class.
Specification of a rectangle with absolute coordinates of all edges.
Text is written right-to-left by default.
WindowNumber window_number
Window number within the window class.
String list item with parameters.
Manually align the window (so no automatic location finding)
virtual void OnDropdownClose(Point pt, int widget, int index, bool instant_close)
A dropdown window associated to this window has been closed.
virtual void OnRealtimeTick(uint delta_ms)
Called periodically.
const NWID * GetWidget(uint widnum) const
Get the nested widget with number widnum from the nested widget tree.
Window white border counter bit mask.
Dimensions (a width and height) of a rectangle in 2D.
Offset at left to draw the frame rectangular area.
void DrawSprite(SpriteID img, PaletteID pal, int x, int y, const SubSprite *sub, ZoomLevel zoom)
Draw a sprite, not in a viewport.
Drop down menu; Window numbers:
static void SetDParam(uint n, uint64 v)
Set a string parameter v at index n in the global string parameter array.