2008-01-14 17:10:58 +01:00
|
|
|
/* $Id$ */
|
|
|
|
|
2009-08-21 22:21:05 +02:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* 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.
|
|
|
|
* 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.
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2008-05-06 17:11:33 +02:00
|
|
|
/** @file dropdown.cpp Implementation of the dropdown widget. */
|
|
|
|
|
2008-01-14 17:10:58 +01:00
|
|
|
#include "../stdafx.h"
|
|
|
|
#include "../window_gui.h"
|
2011-04-30 16:24:23 +02:00
|
|
|
#include "../string_func.h"
|
2008-01-14 17:10:58 +01:00
|
|
|
#include "../strings_func.h"
|
|
|
|
#include "../window_func.h"
|
|
|
|
#include "dropdown_type.h"
|
|
|
|
|
2011-12-15 23:22:55 +01:00
|
|
|
#include "dropdown_widget.h"
|
|
|
|
|
2014-04-23 22:13:33 +02:00
|
|
|
#include "../safeguards.h"
|
|
|
|
|
2008-01-14 17:10:58 +01:00
|
|
|
|
2009-03-21 20:31:47 +01:00
|
|
|
void DropDownListItem::Draw(int left, int right, int top, int bottom, bool sel, int bg_colour) const
|
2008-01-14 17:10:58 +01:00
|
|
|
{
|
2008-08-06 09:10:40 +02:00
|
|
|
int c1 = _colour_gradient[bg_colour][3];
|
|
|
|
int c2 = _colour_gradient[bg_colour][7];
|
2008-01-14 17:10:58 +01:00
|
|
|
|
2009-03-28 13:47:29 +01:00
|
|
|
int mid = top + this->Height(0) / 2;
|
|
|
|
GfxFillRect(left + 1, mid - 2, right - 1, mid - 2, c1);
|
|
|
|
GfxFillRect(left + 1, mid - 1, right - 1, mid - 1, c2);
|
2008-04-12 22:32:18 +02:00
|
|
|
}
|
|
|
|
|
2008-08-07 20:11:09 +02:00
|
|
|
uint DropDownListStringItem::Width() const
|
|
|
|
{
|
|
|
|
char buffer[512];
|
|
|
|
GetString(buffer, this->String(), lastof(buffer));
|
|
|
|
return GetStringBoundingBox(buffer).width;
|
|
|
|
}
|
|
|
|
|
2009-03-21 20:31:47 +01:00
|
|
|
void DropDownListStringItem::Draw(int left, int right, int top, int bottom, bool sel, int bg_colour) const
|
2008-01-14 17:10:58 +01:00
|
|
|
{
|
2009-11-27 17:18:03 +01:00
|
|
|
DrawString(left + WD_FRAMERECT_LEFT, right - WD_FRAMERECT_RIGHT, top, this->String(), sel ? TC_WHITE : TC_BLACK);
|
2008-01-14 17:10:58 +01:00
|
|
|
}
|
|
|
|
|
2011-04-10 17:01:14 +02:00
|
|
|
/**
|
|
|
|
* Natural sorting comparator function for DropDownList::sort().
|
|
|
|
* @param first Left side of comparison.
|
|
|
|
* @param second Right side of comparison.
|
|
|
|
* @return true if \a first precedes \a second.
|
|
|
|
* @warning All items in the list need to be derivates of DropDownListStringItem.
|
|
|
|
*/
|
2013-11-24 15:46:26 +01:00
|
|
|
/* static */ int DropDownListStringItem::NatSortFunc(const DropDownListItem * const *first, const DropDownListItem * const * second)
|
2011-04-10 17:01:14 +02:00
|
|
|
{
|
|
|
|
char buffer1[512], buffer2[512];
|
2013-11-24 15:46:26 +01:00
|
|
|
GetString(buffer1, static_cast<const DropDownListStringItem*>(*first)->String(), lastof(buffer1));
|
|
|
|
GetString(buffer2, static_cast<const DropDownListStringItem*>(*second)->String(), lastof(buffer2));
|
|
|
|
return strnatcmp(buffer1, buffer2);
|
2011-04-10 17:01:14 +02:00
|
|
|
}
|
|
|
|
|
2008-01-14 17:10:58 +01:00
|
|
|
StringID DropDownListParamStringItem::String() const
|
|
|
|
{
|
|
|
|
for (uint i = 0; i < lengthof(this->decode_params); i++) SetDParam(i, this->decode_params[i]);
|
|
|
|
return this->string;
|
|
|
|
}
|
|
|
|
|
2011-04-10 16:56:14 +02:00
|
|
|
StringID DropDownListCharStringItem::String() const
|
2009-02-07 01:37:35 +01:00
|
|
|
{
|
2011-04-10 16:56:14 +02:00
|
|
|
SetDParamStr(0, this->raw_string);
|
|
|
|
return this->string;
|
2009-02-07 01:37:35 +01:00
|
|
|
}
|
|
|
|
|
2009-05-04 20:42:49 +02:00
|
|
|
static const NWidgetPart _nested_dropdown_menu_widgets[] = {
|
2009-11-07 15:40:37 +01:00
|
|
|
NWidget(NWID_HORIZONTAL),
|
2011-12-16 17:27:45 +01:00
|
|
|
NWidget(WWT_PANEL, COLOUR_END, WID_DM_ITEMS), SetMinimalSize(1, 1), SetScrollbar(WID_DM_SCROLL), EndContainer(),
|
|
|
|
NWidget(NWID_SELECTION, INVALID_COLOUR, WID_DM_SHOW_SCROLL),
|
|
|
|
NWidget(NWID_VSCROLLBAR, COLOUR_END, WID_DM_SCROLL),
|
2010-08-12 13:53:11 +02:00
|
|
|
EndContainer(),
|
2009-05-04 20:42:49 +02:00
|
|
|
EndContainer(),
|
|
|
|
};
|
|
|
|
|
2013-05-26 21:23:42 +02:00
|
|
|
static WindowDesc _dropdown_desc(
|
2013-05-26 21:25:01 +02:00
|
|
|
WDP_MANUAL, NULL, 0, 0,
|
2009-11-07 15:40:37 +01:00
|
|
|
WC_DROPDOWN_MENU, WC_NONE,
|
2015-02-01 13:23:39 +01:00
|
|
|
WDF_NO_FOCUS,
|
2009-11-15 11:26:01 +01:00
|
|
|
_nested_dropdown_menu_widgets, lengthof(_nested_dropdown_menu_widgets)
|
2009-11-07 15:40:37 +01:00
|
|
|
);
|
|
|
|
|
2009-11-07 12:26:23 +01:00
|
|
|
/** Drop-down menu window */
|
2008-05-11 20:18:18 +02:00
|
|
|
struct DropdownWindow : Window {
|
2009-08-14 20:41:03 +02:00
|
|
|
WindowClass parent_wnd_class; ///< Parent window class.
|
|
|
|
WindowNumber parent_wnd_num; ///< Parent window number.
|
2012-06-01 12:42:46 +02:00
|
|
|
int parent_button; ///< Parent widget number where the window is dropped from.
|
2013-11-24 15:46:26 +01:00
|
|
|
const DropDownList *list; ///< List with dropdown menu items.
|
2009-11-01 21:50:00 +01:00
|
|
|
int selected_index; ///< Index of the selected item in the list.
|
2009-11-07 12:26:23 +01:00
|
|
|
byte click_delay; ///< Timer to delay selection.
|
2008-01-14 17:10:58 +01:00
|
|
|
bool drag_mode;
|
2009-11-07 15:40:37 +01:00
|
|
|
bool instant_close; ///< Close the window when the mouse button is raised.
|
2009-11-07 12:26:23 +01:00
|
|
|
int scrolling; ///< If non-zero, auto-scroll the item list (one time).
|
2009-11-07 15:40:37 +01:00
|
|
|
Point position; ///< Position of the topleft corner of the window.
|
2010-08-12 10:37:01 +02:00
|
|
|
Scrollbar *vscroll;
|
2009-11-07 12:26:23 +01:00
|
|
|
|
2010-08-01 21:22:34 +02:00
|
|
|
/**
|
|
|
|
* Create a dropdown menu.
|
2009-11-07 12:44:38 +01:00
|
|
|
* @param parent Parent window.
|
|
|
|
* @param list Dropdown item list.
|
|
|
|
* @param selected Index of the selected item in the list.
|
|
|
|
* @param button Widget of the parent window doing the dropdown.
|
2009-11-07 15:40:37 +01:00
|
|
|
* @param instant_close Close the window when the mouse button is raised.
|
|
|
|
* @param position Topleft position of the dropdown menu window.
|
2009-11-07 12:44:38 +01:00
|
|
|
* @param size Size of the dropdown menu window.
|
|
|
|
* @param wi_colour Colour of the parent widget.
|
|
|
|
* @param scroll Dropdown menu has a scrollbar.
|
2009-11-07 12:26:23 +01:00
|
|
|
*/
|
2013-11-24 15:46:26 +01:00
|
|
|
DropdownWindow(Window *parent, const DropDownList *list, int selected, int button, bool instant_close, const Point &position, const Dimension &size, Colours wi_colour, bool scroll)
|
2013-05-26 21:23:42 +02:00
|
|
|
: Window(&_dropdown_desc)
|
2008-05-11 20:18:18 +02:00
|
|
|
{
|
2014-06-05 19:14:16 +02:00
|
|
|
assert(list->Length() > 0);
|
|
|
|
|
2009-11-07 15:40:37 +01:00
|
|
|
this->position = position;
|
2009-11-07 12:44:38 +01:00
|
|
|
|
2013-05-26 21:23:42 +02:00
|
|
|
this->CreateNestedTree();
|
2009-11-07 12:44:38 +01:00
|
|
|
|
2011-12-16 17:27:45 +01:00
|
|
|
this->vscroll = this->GetScrollbar(WID_DM_SCROLL);
|
2010-08-12 10:37:01 +02:00
|
|
|
|
2011-10-11 10:07:47 +02:00
|
|
|
uint items_width = size.width - (scroll ? NWidgetScrollbar::GetVerticalDimension().width : 0);
|
2011-12-16 17:27:45 +01:00
|
|
|
NWidgetCore *nwi = this->GetWidget<NWidgetCore>(WID_DM_ITEMS);
|
2009-11-07 18:45:49 +01:00
|
|
|
nwi->SetMinimalSize(items_width, size.height + 4);
|
2009-11-07 15:40:37 +01:00
|
|
|
nwi->colour = wi_colour;
|
2009-11-07 12:44:38 +01:00
|
|
|
|
2011-12-16 17:27:45 +01:00
|
|
|
nwi = this->GetWidget<NWidgetCore>(WID_DM_SCROLL);
|
2010-08-12 13:53:11 +02:00
|
|
|
nwi->colour = wi_colour;
|
|
|
|
|
2011-12-16 17:27:45 +01:00
|
|
|
this->GetWidget<NWidgetStacked>(WID_DM_SHOW_SCROLL)->SetDisplayedPlane(scroll ? 0 : SZSP_NONE);
|
2009-11-07 12:44:38 +01:00
|
|
|
|
2013-05-26 21:23:42 +02:00
|
|
|
this->FinishInitNested(0);
|
2011-12-15 20:54:23 +01:00
|
|
|
CLRBITS(this->flags, WF_WHITE_BORDER);
|
2009-11-07 12:44:38 +01:00
|
|
|
|
2009-11-07 15:40:37 +01:00
|
|
|
/* Total length of list */
|
|
|
|
int list_height = 0;
|
2013-11-24 15:46:26 +01:00
|
|
|
for (const DropDownListItem * const *it = list->Begin(); it != list->End(); ++it) {
|
|
|
|
const DropDownListItem *item = *it;
|
2009-11-07 18:45:49 +01:00
|
|
|
list_height += item->Height(items_width);
|
2009-11-07 15:40:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Capacity is the average number of items visible */
|
2013-11-24 15:46:26 +01:00
|
|
|
this->vscroll->SetCapacity(size.height * (uint16)list->Length() / list_height);
|
|
|
|
this->vscroll->SetCount((uint16)list->Length());
|
2009-11-07 15:40:37 +01:00
|
|
|
|
2009-11-07 12:44:38 +01:00
|
|
|
this->parent_wnd_class = parent->window_class;
|
|
|
|
this->parent_wnd_num = parent->window_number;
|
|
|
|
this->parent_button = button;
|
|
|
|
this->list = list;
|
|
|
|
this->selected_index = selected;
|
|
|
|
this->click_delay = 0;
|
|
|
|
this->drag_mode = true;
|
|
|
|
this->instant_close = instant_close;
|
2008-05-11 20:18:18 +02:00
|
|
|
}
|
2008-01-14 17:10:58 +01:00
|
|
|
|
2008-05-11 20:18:18 +02:00
|
|
|
~DropdownWindow()
|
|
|
|
{
|
2012-06-01 12:42:46 +02:00
|
|
|
/* Make the dropdown "invisible", so it doesn't affect new window placement.
|
|
|
|
* Also mark it dirty in case the callback deals with the screen. (e.g. screenshots). */
|
|
|
|
this->window_class = WC_INVALID;
|
|
|
|
this->SetDirty();
|
|
|
|
|
2008-05-11 20:18:18 +02:00
|
|
|
Window *w2 = FindWindowById(this->parent_wnd_class, this->parent_wnd_num);
|
|
|
|
if (w2 != NULL) {
|
2012-06-01 12:42:46 +02:00
|
|
|
Point pt = _cursor.pos;
|
|
|
|
pt.x -= w2->left;
|
|
|
|
pt.y -= w2->top;
|
|
|
|
w2->OnDropdownClose(pt, this->parent_button, this->selected_index, this->instant_close);
|
2008-05-11 20:18:18 +02:00
|
|
|
}
|
2013-11-24 15:46:26 +01:00
|
|
|
delete this->list;
|
2008-05-11 20:18:18 +02:00
|
|
|
}
|
2008-01-14 17:10:58 +01:00
|
|
|
|
2013-05-26 21:23:42 +02:00
|
|
|
virtual Point OnInitialPosition(int16 sm_width, int16 sm_height, int window_number)
|
2009-11-07 15:40:37 +01:00
|
|
|
{
|
|
|
|
return this->position;
|
|
|
|
}
|
|
|
|
|
2010-08-01 21:22:34 +02:00
|
|
|
/**
|
|
|
|
* Find the dropdown item under the cursor.
|
2018-10-28 03:17:36 +01:00
|
|
|
* @param[out] value Selected item, if function returns \c true.
|
2009-11-07 15:40:37 +01:00
|
|
|
* @return Cursor points to a dropdown item.
|
|
|
|
*/
|
2008-05-13 23:11:38 +02:00
|
|
|
bool GetDropDownItem(int &value)
|
2008-05-11 20:18:18 +02:00
|
|
|
{
|
2008-05-13 23:11:38 +02:00
|
|
|
if (GetWidgetFromPos(this, _cursor.pos.x - this->left, _cursor.pos.y - this->top) < 0) return false;
|
2008-01-14 17:10:58 +01:00
|
|
|
|
2011-12-16 17:27:45 +01:00
|
|
|
NWidgetBase *nwi = this->GetWidget<NWidgetBase>(WID_DM_ITEMS);
|
2009-11-07 15:40:37 +01:00
|
|
|
int y = _cursor.pos.y - this->top - nwi->pos_y - 2;
|
|
|
|
int width = nwi->current_x - 4;
|
2010-08-12 10:37:01 +02:00
|
|
|
int pos = this->vscroll->GetPosition();
|
2008-01-14 17:10:58 +01:00
|
|
|
|
2008-05-11 20:18:18 +02:00
|
|
|
const DropDownList *list = this->list;
|
2008-04-12 22:32:18 +02:00
|
|
|
|
2013-11-24 15:46:26 +01:00
|
|
|
for (const DropDownListItem * const *it = list->Begin(); it != list->End(); ++it) {
|
2008-05-11 20:18:18 +02:00
|
|
|
/* Skip items that are scrolled up */
|
|
|
|
if (--pos >= 0) continue;
|
2008-04-12 22:32:18 +02:00
|
|
|
|
2008-05-11 20:18:18 +02:00
|
|
|
const DropDownListItem *item = *it;
|
|
|
|
int item_height = item->Height(width);
|
2008-01-14 17:10:58 +01:00
|
|
|
|
2008-05-11 20:18:18 +02:00
|
|
|
if (y < item_height) {
|
2008-08-06 09:10:40 +02:00
|
|
|
if (item->masked || !item->Selectable()) return false;
|
2008-05-13 23:11:38 +02:00
|
|
|
value = item->result;
|
|
|
|
return true;
|
2008-05-11 20:18:18 +02:00
|
|
|
}
|
2008-01-14 17:10:58 +01:00
|
|
|
|
2008-05-11 20:18:18 +02:00
|
|
|
y -= item_height;
|
|
|
|
}
|
2008-01-14 17:10:58 +01:00
|
|
|
|
2008-05-13 23:11:38 +02:00
|
|
|
return false;
|
2008-05-11 20:18:18 +02:00
|
|
|
}
|
2008-01-14 17:10:58 +01:00
|
|
|
|
2009-11-07 15:40:37 +01:00
|
|
|
virtual void DrawWidget(const Rect &r, int widget) const
|
|
|
|
{
|
2011-12-16 17:27:45 +01:00
|
|
|
if (widget != WID_DM_ITEMS) return;
|
2008-04-12 22:32:18 +02:00
|
|
|
|
2013-11-26 16:12:54 +01:00
|
|
|
Colours colour = this->GetWidget<NWidgetCore>(widget)->colour;
|
2008-04-12 22:32:18 +02:00
|
|
|
|
2009-11-07 15:40:37 +01:00
|
|
|
int y = r.top + 2;
|
2010-08-12 10:37:01 +02:00
|
|
|
int pos = this->vscroll->GetPosition();
|
2013-11-24 15:46:26 +01:00
|
|
|
for (const DropDownListItem * const *it = this->list->Begin(); it != this->list->End(); ++it) {
|
2008-05-11 20:18:18 +02:00
|
|
|
const DropDownListItem *item = *it;
|
2009-11-07 15:40:37 +01:00
|
|
|
int item_height = item->Height(r.right - r.left + 1);
|
2008-01-14 17:10:58 +01:00
|
|
|
|
2008-05-11 20:18:18 +02:00
|
|
|
/* Skip items that are scrolled up */
|
|
|
|
if (--pos >= 0) continue;
|
2008-01-14 17:10:58 +01:00
|
|
|
|
2009-11-07 15:40:37 +01:00
|
|
|
if (y + item_height < r.bottom) {
|
|
|
|
bool selected = (this->selected_index == item->result);
|
2011-05-06 23:13:29 +02:00
|
|
|
if (selected) GfxFillRect(r.left + 2, y, r.right - 1, y + item_height - 1, PC_BLACK);
|
2008-08-06 09:10:40 +02:00
|
|
|
|
2011-10-11 15:13:20 +02:00
|
|
|
item->Draw(r.left, r.right, y, y + item_height, selected, colour);
|
2008-08-06 09:10:40 +02:00
|
|
|
|
|
|
|
if (item->masked) {
|
2009-11-07 15:40:37 +01:00
|
|
|
GfxFillRect(r.left + 1, y, r.right - 1, y + item_height - 1, _colour_gradient[colour][5], FILLRECT_CHECKER);
|
2008-05-11 20:18:18 +02:00
|
|
|
}
|
2008-01-14 17:10:58 +01:00
|
|
|
}
|
2008-05-11 20:18:18 +02:00
|
|
|
y += item_height;
|
|
|
|
}
|
2009-11-07 15:40:37 +01:00
|
|
|
}
|
2008-05-11 20:18:18 +02:00
|
|
|
|
2010-01-30 19:34:48 +01:00
|
|
|
virtual void OnClick(Point pt, int widget, int click_count)
|
2008-05-11 20:18:18 +02:00
|
|
|
{
|
2011-12-16 17:27:45 +01:00
|
|
|
if (widget != WID_DM_ITEMS) return;
|
2008-05-13 23:11:38 +02:00
|
|
|
int item;
|
|
|
|
if (this->GetDropDownItem(item)) {
|
2008-05-11 20:18:18 +02:00
|
|
|
this->click_delay = 4;
|
|
|
|
this->selected_index = item;
|
|
|
|
this->SetDirty();
|
|
|
|
}
|
|
|
|
}
|
2008-01-14 17:10:58 +01:00
|
|
|
|
2008-05-11 20:18:18 +02:00
|
|
|
virtual void OnTick()
|
|
|
|
{
|
2010-01-28 03:30:59 +01:00
|
|
|
if (this->scrolling != 0) {
|
2010-08-12 10:37:01 +02:00
|
|
|
int pos = this->vscroll->GetPosition();
|
2010-01-28 03:30:59 +01:00
|
|
|
|
2010-08-12 10:37:01 +02:00
|
|
|
this->vscroll->UpdatePosition(this->scrolling);
|
2010-01-28 03:30:59 +01:00
|
|
|
this->scrolling = 0;
|
|
|
|
|
2010-08-12 10:37:01 +02:00
|
|
|
if (pos != this->vscroll->GetPosition()) {
|
2010-01-28 03:30:59 +01:00
|
|
|
this->SetDirty();
|
|
|
|
}
|
|
|
|
}
|
2008-05-11 20:18:18 +02:00
|
|
|
}
|
2008-01-14 17:10:58 +01:00
|
|
|
|
2008-05-11 20:18:18 +02:00
|
|
|
virtual void OnMouseLoop()
|
|
|
|
{
|
|
|
|
Window *w2 = FindWindowById(this->parent_wnd_class, this->parent_wnd_num);
|
|
|
|
if (w2 == NULL) {
|
|
|
|
delete this;
|
|
|
|
return;
|
|
|
|
}
|
2008-01-14 17:10:58 +01:00
|
|
|
|
2008-05-11 20:18:18 +02:00
|
|
|
if (this->click_delay != 0 && --this->click_delay == 0) {
|
2010-10-24 22:23:45 +02:00
|
|
|
/* Make the dropdown "invisible", so it doesn't affect new window placement.
|
|
|
|
* Also mark it dirty in case the callback deals with the screen. (e.g. screenshots). */
|
2010-05-22 21:04:56 +02:00
|
|
|
this->window_class = WC_INVALID;
|
2010-10-24 22:23:45 +02:00
|
|
|
this->SetDirty();
|
2010-05-22 21:04:56 +02:00
|
|
|
|
2008-05-11 20:18:18 +02:00
|
|
|
w2->OnDropdownSelect(this->parent_button, this->selected_index);
|
|
|
|
delete this;
|
|
|
|
return;
|
|
|
|
}
|
2008-01-15 17:30:08 +01:00
|
|
|
|
2008-05-11 20:18:18 +02:00
|
|
|
if (this->drag_mode) {
|
2008-05-13 23:11:38 +02:00
|
|
|
int item;
|
2008-05-11 20:18:18 +02:00
|
|
|
|
|
|
|
if (!_left_button_clicked) {
|
|
|
|
this->drag_mode = false;
|
2008-08-07 20:26:13 +02:00
|
|
|
if (!this->GetDropDownItem(item)) {
|
2012-06-01 12:42:46 +02:00
|
|
|
if (this->instant_close) delete this;
|
2008-08-07 20:26:13 +02:00
|
|
|
return;
|
|
|
|
}
|
2008-05-11 20:18:18 +02:00
|
|
|
this->click_delay = 2;
|
|
|
|
} else {
|
|
|
|
if (_cursor.pos.y <= this->top + 2) {
|
|
|
|
/* Cursor is above the list, set scroll up */
|
|
|
|
this->scrolling = -1;
|
|
|
|
return;
|
|
|
|
} else if (_cursor.pos.y >= this->top + this->height - 2) {
|
|
|
|
/* Cursor is below list, set scroll down */
|
|
|
|
this->scrolling = 1;
|
|
|
|
return;
|
2008-01-14 17:10:58 +01:00
|
|
|
}
|
|
|
|
|
2008-05-13 23:11:38 +02:00
|
|
|
if (!this->GetDropDownItem(item)) return;
|
2008-01-14 17:10:58 +01:00
|
|
|
}
|
|
|
|
|
2010-01-28 03:30:59 +01:00
|
|
|
if (this->selected_index != item) {
|
|
|
|
this->selected_index = item;
|
|
|
|
this->SetDirty();
|
|
|
|
}
|
2008-05-11 20:18:18 +02:00
|
|
|
}
|
2008-01-14 17:10:58 +01:00
|
|
|
}
|
2008-05-11 20:18:18 +02:00
|
|
|
};
|
2008-01-14 17:10:58 +01:00
|
|
|
|
2012-06-01 12:43:50 +02:00
|
|
|
/**
|
|
|
|
* Show a drop down list.
|
|
|
|
* @param w Parent window for the list.
|
|
|
|
* @param list Prepopulated DropDownList. Will be deleted when the list is
|
|
|
|
* closed.
|
|
|
|
* @param selected The initially selected list item.
|
|
|
|
* @param button The widget which is passed to Window::OnDropdownSelect and OnDropdownClose.
|
|
|
|
* Unless you override those functions, this should be then widget index of the dropdown button.
|
|
|
|
* @param wi_rect Coord of the parent drop down button, used to position the dropdown menu.
|
|
|
|
* @param auto_width The width is determined by the widest item in the list,
|
|
|
|
* in this case only one of \a left or \a right is used (depending on text direction).
|
|
|
|
* @param instant_close Set to true if releasing mouse button should close the
|
|
|
|
* list regardless of where the cursor is.
|
|
|
|
*/
|
2013-11-24 15:46:26 +01:00
|
|
|
void ShowDropDownListAt(Window *w, const DropDownList *list, int selected, int button, Rect wi_rect, Colours wi_colour, bool auto_width, bool instant_close)
|
2008-01-14 17:10:58 +01:00
|
|
|
{
|
|
|
|
DeleteWindowById(WC_DROPDOWN_MENU, 0);
|
|
|
|
|
|
|
|
/* The preferred position is just below the dropdown calling widget */
|
2009-07-12 12:02:10 +02:00
|
|
|
int top = w->top + wi_rect.bottom + 1;
|
2008-04-12 22:32:18 +02:00
|
|
|
|
2012-06-01 12:43:50 +02:00
|
|
|
/* The preferred width equals the calling widget */
|
|
|
|
uint width = wi_rect.right - wi_rect.left + 1;
|
2008-08-25 18:07:10 +02:00
|
|
|
|
2014-07-16 22:38:36 +02:00
|
|
|
/* Longest item in the list, if auto_width is enabled */
|
2008-08-25 18:07:10 +02:00
|
|
|
uint max_item_width = 0;
|
2008-08-07 20:11:09 +02:00
|
|
|
|
2008-04-12 22:32:18 +02:00
|
|
|
/* Total length of list */
|
2014-07-08 22:07:50 +02:00
|
|
|
int height = 0;
|
2008-04-12 22:32:18 +02:00
|
|
|
|
2013-11-24 15:46:26 +01:00
|
|
|
for (const DropDownListItem * const *it = list->Begin(); it != list->End(); ++it) {
|
|
|
|
const DropDownListItem *item = *it;
|
2014-07-08 22:07:50 +02:00
|
|
|
height += item->Height(width);
|
2014-07-16 22:38:36 +02:00
|
|
|
if (auto_width) max_item_width = max(max_item_width, item->Width() + 5);
|
2008-04-12 22:32:18 +02:00
|
|
|
}
|
|
|
|
|
2008-01-14 17:10:58 +01:00
|
|
|
/* Check if the status bar is visible, as we don't want to draw over it */
|
2009-11-07 18:24:04 +01:00
|
|
|
int screen_bottom = GetMainViewBottom();
|
2008-01-14 17:10:58 +01:00
|
|
|
bool scroll = false;
|
|
|
|
|
|
|
|
/* Check if the dropdown will fully fit below the widget */
|
2008-04-12 22:32:18 +02:00
|
|
|
if (top + height + 4 >= screen_bottom) {
|
2008-01-14 17:10:58 +01:00
|
|
|
/* If not, check if it will fit above the widget */
|
2017-03-23 21:07:59 +01:00
|
|
|
int screen_top = GetMainViewTop();
|
|
|
|
if (w->top + wi_rect.top > screen_top + height) {
|
2009-07-12 12:02:10 +02:00
|
|
|
top = w->top + wi_rect.top - height - 4;
|
2008-01-14 17:10:58 +01:00
|
|
|
} else {
|
2017-03-23 21:07:59 +01:00
|
|
|
/* If it doesn't fit above the widget, we need to enable a scrollbar... */
|
2014-07-08 22:07:50 +02:00
|
|
|
int avg_height = height / (int)list->Length();
|
2008-01-14 17:10:58 +01:00
|
|
|
scroll = true;
|
2017-03-23 21:07:59 +01:00
|
|
|
|
|
|
|
/* ... and choose whether to put the list above or below the widget. */
|
|
|
|
bool put_above = false;
|
|
|
|
int available_height = screen_bottom - w->top - wi_rect.bottom;
|
|
|
|
if (w->top + wi_rect.top - screen_top > available_height) {
|
|
|
|
// Put it above.
|
|
|
|
available_height = w->top + wi_rect.top - screen_top;
|
|
|
|
put_above = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check at least there is space for one item. */
|
|
|
|
assert(available_height >= avg_height);
|
|
|
|
|
|
|
|
/* And lastly, fit the list... */
|
|
|
|
int rows = available_height / avg_height;
|
|
|
|
height = rows * avg_height;
|
|
|
|
|
2008-08-07 20:11:09 +02:00
|
|
|
/* Add space for the scroll bar if we automatically determined
|
|
|
|
* the width of the list. */
|
2011-10-11 10:07:47 +02:00
|
|
|
max_item_width += NWidgetScrollbar::GetVerticalDimension().width;
|
2017-03-23 21:07:59 +01:00
|
|
|
|
|
|
|
/* ... and set the top position if needed. */
|
|
|
|
if (put_above) {
|
|
|
|
top = w->top + wi_rect.top - height - 4;
|
|
|
|
}
|
2008-01-14 17:10:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-25 18:07:10 +02:00
|
|
|
if (auto_width) width = max(width, max_item_width);
|
|
|
|
|
2015-08-10 22:24:13 +02:00
|
|
|
Point dw_pos = { w->left + (_current_text_dir == TD_RTL ? wi_rect.right + 1 - (int)width : wi_rect.left), top};
|
|
|
|
Dimension dw_size = {width, (uint)height};
|
2009-11-07 15:40:37 +01:00
|
|
|
new DropdownWindow(w, list, selected, button, instant_close, dw_pos, dw_size, wi_colour, scroll);
|
2008-01-14 17:10:58 +01:00
|
|
|
}
|
|
|
|
|
2012-06-01 12:43:50 +02:00
|
|
|
/**
|
|
|
|
* Show a drop down list.
|
|
|
|
* @param w Parent window for the list.
|
|
|
|
* @param list Prepopulated DropDownList. Will be deleted when the list is
|
|
|
|
* closed.
|
|
|
|
* @param selected The initially selected list item.
|
|
|
|
* @param button The widget within the parent window that is used to determine
|
|
|
|
* the list's location.
|
|
|
|
* @param width Override the width determined by the selected widget.
|
|
|
|
* @param auto_width Maximum width is determined by the widest item in the list.
|
|
|
|
* @param instant_close Set to true if releasing mouse button should close the
|
|
|
|
* list regardless of where the cursor is.
|
|
|
|
*/
|
2013-11-24 15:46:26 +01:00
|
|
|
void ShowDropDownList(Window *w, const DropDownList *list, int selected, int button, uint width, bool auto_width, bool instant_close)
|
2012-06-01 12:43:50 +02:00
|
|
|
{
|
|
|
|
/* Our parent's button widget is used to determine where to place the drop
|
|
|
|
* down list window. */
|
|
|
|
Rect wi_rect;
|
|
|
|
NWidgetCore *nwi = w->GetWidget<NWidgetCore>(button);
|
|
|
|
wi_rect.left = nwi->pos_x;
|
|
|
|
wi_rect.right = nwi->pos_x + nwi->current_x - 1;
|
|
|
|
wi_rect.top = nwi->pos_y;
|
|
|
|
wi_rect.bottom = nwi->pos_y + nwi->current_y - 1;
|
|
|
|
Colours wi_colour = nwi->colour;
|
|
|
|
|
|
|
|
if ((nwi->type & WWT_MASK) == NWID_BUTTON_DROPDOWN) {
|
|
|
|
nwi->disp_flags |= ND_DROPDOWN_ACTIVE;
|
|
|
|
} else {
|
|
|
|
w->LowerWidget(button);
|
|
|
|
}
|
|
|
|
w->SetWidgetDirty(button);
|
|
|
|
|
|
|
|
if (width != 0) {
|
|
|
|
if (_current_text_dir == TD_RTL) {
|
|
|
|
wi_rect.left = wi_rect.right + 1 - width;
|
|
|
|
} else {
|
|
|
|
wi_rect.right = wi_rect.left + width - 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ShowDropDownListAt(w, list, selected, button, wi_rect, wi_colour, auto_width, instant_close);
|
|
|
|
}
|
|
|
|
|
2010-08-01 21:22:34 +02:00
|
|
|
/**
|
|
|
|
* Show a dropdown menu window near a widget of the parent window.
|
2012-01-01 18:22:32 +01:00
|
|
|
* The result code of the items is their index in the \a strings list.
|
2009-11-01 21:50:00 +01:00
|
|
|
* @param w Parent window that wants the dropdown menu.
|
|
|
|
* @param strings Menu list, end with #INVALID_STRING_ID
|
|
|
|
* @param selected Index of initial selected item.
|
2012-01-01 18:22:32 +01:00
|
|
|
* @param button Button widget number of the parent window \a w that wants the dropdown menu.
|
2013-10-11 17:41:55 +02:00
|
|
|
* @param disabled_mask Bitmask for disabled items (items with their bit set are displayed, but not selectable in the dropdown list).
|
|
|
|
* @param hidden_mask Bitmask for hidden items (items with their bit set are not copied to the dropdown list).
|
2012-01-01 18:22:32 +01:00
|
|
|
* @param width Width of the dropdown menu. If \c 0, use the width of parent widget \a button.
|
2009-11-01 21:50:00 +01:00
|
|
|
*/
|
2008-04-10 01:00:12 +02:00
|
|
|
void ShowDropDownMenu(Window *w, const StringID *strings, int selected, int button, uint32 disabled_mask, uint32 hidden_mask, uint width)
|
2008-01-14 17:10:58 +01:00
|
|
|
{
|
|
|
|
DropDownList *list = new DropDownList();
|
|
|
|
|
|
|
|
for (uint i = 0; strings[i] != INVALID_STRING_ID; i++) {
|
|
|
|
if (!HasBit(hidden_mask, i)) {
|
2013-11-24 15:46:26 +01:00
|
|
|
*list->Append() = new DropDownListStringItem(strings[i], i, HasBit(disabled_mask, i));
|
2008-01-14 17:10:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No entries in the list? */
|
2013-11-24 15:46:26 +01:00
|
|
|
if (list->Length() == 0) {
|
|
|
|
delete list;
|
2008-01-14 17:10:58 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-04-10 01:00:12 +02:00
|
|
|
ShowDropDownList(w, list, selected, button, width);
|
2008-01-14 17:10:58 +01:00
|
|
|
}
|
|
|
|
|
2008-04-07 22:28:58 +02:00
|
|
|
/**
|
|
|
|
* Delete the drop-down menu from window \a pw
|
|
|
|
* @param pw Parent window of the drop-down menu window
|
2009-08-14 20:41:03 +02:00
|
|
|
* @return Parent widget number if the drop-down was found and closed, \c -1 if the window was not found.
|
2008-04-07 22:28:58 +02:00
|
|
|
*/
|
2009-02-01 23:32:07 +01:00
|
|
|
int HideDropDownMenu(Window *pw)
|
2008-01-14 17:10:58 +01:00
|
|
|
{
|
2009-01-07 00:19:59 +01:00
|
|
|
Window *w;
|
|
|
|
FOR_ALL_WINDOWS_FROM_BACK(w) {
|
|
|
|
if (w->window_class != WC_DROPDOWN_MENU) continue;
|
2008-01-14 17:10:58 +01:00
|
|
|
|
2009-01-07 00:19:59 +01:00
|
|
|
DropdownWindow *dw = dynamic_cast<DropdownWindow*>(w);
|
2013-11-26 14:21:25 +01:00
|
|
|
assert(dw != NULL);
|
2008-05-11 20:18:18 +02:00
|
|
|
if (pw->window_class == dw->parent_wnd_class &&
|
|
|
|
pw->window_number == dw->parent_wnd_num) {
|
2009-02-01 23:32:07 +01:00
|
|
|
int parent_button = dw->parent_button;
|
2008-05-11 20:18:18 +02:00
|
|
|
delete dw;
|
2009-02-01 23:32:07 +01:00
|
|
|
return parent_button;
|
2008-01-14 17:10:58 +01:00
|
|
|
}
|
|
|
|
}
|
2009-02-01 23:32:07 +01:00
|
|
|
|
|
|
|
return -1;
|
2008-01-14 17:10:58 +01:00
|
|
|
}
|
|
|
|
|