src/widgets/dropdown.cpp
author peter1138
Sun, 03 Aug 2008 17:35:08 +0000
changeset 9834 7474149531c1
parent 9827 338aac16d11b
child 9859 28606a486c52
permissions -rw-r--r--
(svn r13977) -Codechange: Let ResizeWindowForWidget() handle hidden (zero height or zero width) widgets.
8284
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
     1
/* $Id$ */
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
     2
9111
48ce04029fe4 (svn r12971) -Documentation: add @file in files that missed them and add something more than whitespace as description of files that don't have a description.
rubidium
parents: 9082
diff changeset
     3
/** @file dropdown.cpp Implementation of the dropdown widget. */
48ce04029fe4 (svn r12971) -Documentation: add @file in files that missed them and add something more than whitespace as description of files that don't have a description.
rubidium
parents: 9082
diff changeset
     4
8284
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
     5
#include "../stdafx.h"
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
     6
#include "../openttd.h"
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
     7
#include "../strings_type.h"
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
     8
#include "../window_gui.h"
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
     9
#include "../strings_func.h"
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
    10
#include "../strings_type.h"
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
    11
#include "../gfx_func.h"
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
    12
#include "../window_func.h"
8301
9f8d82ea6667 (svn r11865) -Feature(tte): Support scrolling of drop down lists when in drag mode by moving the pointer above or below the list.
peter1138
parents: 8299
diff changeset
    13
#include "../core/math_func.hpp"
8284
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
    14
#include "dropdown_type.h"
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
    15
#include "dropdown_func.h"
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
    16
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
    17
#include "../table/sprites.h"
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
    18
#include "table/strings.h"
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
    19
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
    20
StringID DropDownListItem::String() const
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
    21
{
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
    22
	return STR_NULL;
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
    23
}
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
    24
8905
609b1121b3b3 (svn r12672) -Codechange: Add support for variably-sized drop down list items.
peter1138
parents: 8884
diff changeset
    25
uint DropDownListItem::Height(uint width) const
609b1121b3b3 (svn r12672) -Codechange: Add support for variably-sized drop down list items.
peter1138
parents: 8884
diff changeset
    26
{
609b1121b3b3 (svn r12672) -Codechange: Add support for variably-sized drop down list items.
peter1138
parents: 8884
diff changeset
    27
	return 10;
609b1121b3b3 (svn r12672) -Codechange: Add support for variably-sized drop down list items.
peter1138
parents: 8884
diff changeset
    28
}
609b1121b3b3 (svn r12672) -Codechange: Add support for variably-sized drop down list items.
peter1138
parents: 8884
diff changeset
    29
8284
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
    30
StringID DropDownListStringItem::String() const
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
    31
{
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
    32
	return this->string;
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
    33
}
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
    34
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
    35
StringID DropDownListParamStringItem::String() const
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
    36
{
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
    37
	for (uint i = 0; i < lengthof(this->decode_params); i++) SetDParam(i, this->decode_params[i]);
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
    38
	return this->string;
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
    39
}
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
    40
8907
c6949b09d3b3 (svn r12675) -Codechange: Add support for custom drawing of drop down list items.
peter1138
parents: 8905
diff changeset
    41
void DropDownListItem::Draw(int x, int y, uint width, uint height, bool sel) const
c6949b09d3b3 (svn r12675) -Codechange: Add support for custom drawing of drop down list items.
peter1138
parents: 8905
diff changeset
    42
{
c6949b09d3b3 (svn r12675) -Codechange: Add support for custom drawing of drop down list items.
peter1138
parents: 8905
diff changeset
    43
	DrawStringTruncated(x + 2, y, this->String(), sel ? TC_WHITE : TC_BLACK, x + width);
c6949b09d3b3 (svn r12675) -Codechange: Add support for custom drawing of drop down list items.
peter1138
parents: 8905
diff changeset
    44
}
c6949b09d3b3 (svn r12675) -Codechange: Add support for custom drawing of drop down list items.
peter1138
parents: 8905
diff changeset
    45
8299
e8b5a9aef71a (svn r11863) -Fix (r11848): One day I'll learn C++... Delete all items in a drop down list before deleting the list.
peter1138
parents: 8294
diff changeset
    46
/**
e8b5a9aef71a (svn r11863) -Fix (r11848): One day I'll learn C++... Delete all items in a drop down list before deleting the list.
peter1138
parents: 8294
diff changeset
    47
 * Delete all items of a drop down list and the list itself
e8b5a9aef71a (svn r11863) -Fix (r11848): One day I'll learn C++... Delete all items in a drop down list before deleting the list.
peter1138
parents: 8294
diff changeset
    48
 * @param list List to delete.
e8b5a9aef71a (svn r11863) -Fix (r11848): One day I'll learn C++... Delete all items in a drop down list before deleting the list.
peter1138
parents: 8294
diff changeset
    49
 */
e8b5a9aef71a (svn r11863) -Fix (r11848): One day I'll learn C++... Delete all items in a drop down list before deleting the list.
peter1138
parents: 8294
diff changeset
    50
static void DeleteDropDownList(DropDownList *list)
e8b5a9aef71a (svn r11863) -Fix (r11848): One day I'll learn C++... Delete all items in a drop down list before deleting the list.
peter1138
parents: 8294
diff changeset
    51
{
e8b5a9aef71a (svn r11863) -Fix (r11848): One day I'll learn C++... Delete all items in a drop down list before deleting the list.
peter1138
parents: 8294
diff changeset
    52
	for (DropDownList::iterator it = list->begin(); it != list->end(); ++it) {
e8b5a9aef71a (svn r11863) -Fix (r11848): One day I'll learn C++... Delete all items in a drop down list before deleting the list.
peter1138
parents: 8294
diff changeset
    53
		DropDownListItem *item = *it;
e8b5a9aef71a (svn r11863) -Fix (r11848): One day I'll learn C++... Delete all items in a drop down list before deleting the list.
peter1138
parents: 8294
diff changeset
    54
		delete item;
e8b5a9aef71a (svn r11863) -Fix (r11848): One day I'll learn C++... Delete all items in a drop down list before deleting the list.
peter1138
parents: 8294
diff changeset
    55
	}
e8b5a9aef71a (svn r11863) -Fix (r11848): One day I'll learn C++... Delete all items in a drop down list before deleting the list.
peter1138
parents: 8294
diff changeset
    56
	delete list;
e8b5a9aef71a (svn r11863) -Fix (r11848): One day I'll learn C++... Delete all items in a drop down list before deleting the list.
peter1138
parents: 8294
diff changeset
    57
}
e8b5a9aef71a (svn r11863) -Fix (r11848): One day I'll learn C++... Delete all items in a drop down list before deleting the list.
peter1138
parents: 8294
diff changeset
    58
9189
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
    59
static const Widget _dropdown_menu_widgets[] = {
9827
338aac16d11b (svn r13970) -Codechange: Replace numbers with Colours enum on network, toolbar and dropdown guis.
belugas
parents: 9607
diff changeset
    60
{      WWT_PANEL,   RESIZE_NONE,  COLOUR_END,     0, 0,     0, 0, 0x0, STR_NULL},
338aac16d11b (svn r13970) -Codechange: Replace numbers with Colours enum on network, toolbar and dropdown guis.
belugas
parents: 9607
diff changeset
    61
{  WWT_SCROLLBAR,   RESIZE_NONE,  COLOUR_END,     0, 0,     0, 0, 0x0, STR_0190_SCROLL_BAR_SCROLLS_LIST},
9189
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
    62
{   WIDGETS_END},
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
    63
};
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
    64
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
    65
struct DropdownWindow : Window {
8284
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
    66
	WindowClass parent_wnd_class;
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
    67
	WindowNumber parent_wnd_num;
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
    68
	byte parent_button;
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
    69
	DropDownList *list;
8543
87899423775d (svn r12121) -Fix [FS#1764]: Drop down item indexes are now an int instead of a byte (Yexo)
peter1138
parents: 8358
diff changeset
    70
	int selected_index;
8284
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
    71
	byte click_delay;
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
    72
	bool drag_mode;
8301
9f8d82ea6667 (svn r11865) -Feature(tte): Support scrolling of drop down lists when in drag mode by moving the pointer above or below the list.
peter1138
parents: 8299
diff changeset
    73
	int scrolling;
8284
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
    74
9301
3b20dfb6c4e8 (svn r13169) -Codechange: remove a (now) unneeded parameter of one of the Window constructors.
rubidium
parents: 9273
diff changeset
    75
	DropdownWindow(int x, int y, int width, int height, const Widget *widget) : Window(x, y, width, height, WC_DROPDOWN_MENU, widget)
9189
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
    76
	{
9374
4e9c2b65768e (svn r13278) -Fix: Missing call to FindWindowPlacementAndResize() for drop down lists
peter1138
parents: 9301
diff changeset
    77
		this->FindWindowPlacementAndResize(width, height);
8284
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
    78
	}
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
    79
9189
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
    80
	~DropdownWindow()
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
    81
	{
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
    82
		Window *w2 = FindWindowById(this->parent_wnd_class, this->parent_wnd_num);
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
    83
		if (w2 != NULL) {
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
    84
			w2->RaiseWidget(this->parent_button);
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
    85
			w2->InvalidateWidget(this->parent_button);
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
    86
		}
8284
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
    87
9189
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
    88
		DeleteDropDownList(this->list);
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
    89
	}
8284
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
    90
9209
2987efebe01d (svn r13075) -Codechange: Allow any value for a dropdown item instead of just positive.
peter1138
parents: 9189
diff changeset
    91
	bool GetDropDownItem(int &value)
9189
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
    92
	{
9209
2987efebe01d (svn r13075) -Codechange: Allow any value for a dropdown item instead of just positive.
peter1138
parents: 9189
diff changeset
    93
		if (GetWidgetFromPos(this, _cursor.pos.x - this->left, _cursor.pos.y - this->top) < 0) return false;
8284
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
    94
9189
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
    95
		int y     = _cursor.pos.y - this->top - 2;
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
    96
		int width = this->widget[0].right - 3;
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
    97
		int pos   = this->vscroll.pos;
8301
9f8d82ea6667 (svn r11865) -Feature(tte): Support scrolling of drop down lists when in drag mode by moving the pointer above or below the list.
peter1138
parents: 8299
diff changeset
    98
9189
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
    99
		const DropDownList *list = this->list;
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   100
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   101
		for (DropDownList::const_iterator it = list->begin(); it != list->end(); ++it) {
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   102
			/* Skip items that are scrolled up */
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   103
			if (--pos >= 0) continue;
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   104
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   105
			const DropDownListItem *item = *it;
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   106
			int item_height = item->Height(width);
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   107
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   108
			if (y < item_height) {
9209
2987efebe01d (svn r13075) -Codechange: Allow any value for a dropdown item instead of just positive.
peter1138
parents: 9189
diff changeset
   109
				if (item->masked || item->String() == STR_NULL) return false;
2987efebe01d (svn r13075) -Codechange: Allow any value for a dropdown item instead of just positive.
peter1138
parents: 9189
diff changeset
   110
				value = item->result;
2987efebe01d (svn r13075) -Codechange: Allow any value for a dropdown item instead of just positive.
peter1138
parents: 9189
diff changeset
   111
				return true;
8284
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
   112
			}
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
   113
9189
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   114
			y -= item_height;
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   115
		}
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   116
9209
2987efebe01d (svn r13075) -Codechange: Allow any value for a dropdown item instead of just positive.
peter1138
parents: 9189
diff changeset
   117
		return false;
9189
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   118
	}
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   119
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   120
	virtual void OnPaint()
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   121
	{
9273
35e0224ea8f1 (svn r13139) -Codechange: move DrawWindowWidgets and DrawWindowViewport to the Window class and remove Window from their naming.
rubidium
parents: 9209
diff changeset
   122
		this->DrawWidgets();
9189
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   123
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   124
		int x = 1;
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   125
		int y = 2;
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   126
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   127
		int sel    = this->selected_index;
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   128
		int width  = this->widget[0].right - 3;
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   129
		int height = this->widget[0].bottom;
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   130
		int pos    = this->vscroll.pos;
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   131
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   132
		DropDownList *list = this->list;
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   133
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   134
		for (DropDownList::const_iterator it = list->begin(); it != list->end(); ++it) {
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   135
			const DropDownListItem *item = *it;
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   136
			int item_height = item->Height(width);
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   137
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   138
			/* Skip items that are scrolled up */
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   139
			if (--pos >= 0) continue;
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   140
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   141
			if (y + item_height < height) {
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   142
				if (item->String() != STR_NULL) {
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   143
					if (sel == item->result) GfxFillRect(x + 1, y, x + width, y + item_height - 1, 0);
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   144
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   145
					item->Draw(x, y, width, 10, sel == item->result);
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   146
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   147
					if (item->masked) {
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   148
						GfxFillRect(x, y, x + width, y + item_height - 1,
9607
5a5728fb702a (svn r13649) -Codechange: Split the GfxFillRect() special flags from 'color' into their own parameter.
frosch
parents: 9390
diff changeset
   149
							_colour_gradient[this->widget[0].color][5], FILLRECT_CHECKER
9189
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   150
						);
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   151
					}
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   152
				} else {
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   153
					int c1 = _colour_gradient[this->widget[0].color][3];
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   154
					int c2 = _colour_gradient[this->widget[0].color][7];
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   155
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   156
					GfxFillRect(x + 1, y + 3, x + this->width - 5, y + 3, c1);
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   157
					GfxFillRect(x + 1, y + 4, x + this->width - 5, y + 4, c2);
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   158
				}
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   159
			}
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   160
			y += item_height;
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   161
		}
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   162
	};
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   163
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   164
	virtual void OnClick(Point pt, int widget)
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   165
	{
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   166
		if (widget != 0) return;
9209
2987efebe01d (svn r13075) -Codechange: Allow any value for a dropdown item instead of just positive.
peter1138
parents: 9189
diff changeset
   167
		int item;
2987efebe01d (svn r13075) -Codechange: Allow any value for a dropdown item instead of just positive.
peter1138
parents: 9189
diff changeset
   168
		if (this->GetDropDownItem(item)) {
9189
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   169
			this->click_delay = 4;
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   170
			this->selected_index = item;
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   171
			this->SetDirty();
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   172
		}
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   173
	}
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   174
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   175
	virtual void OnTick()
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   176
	{
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   177
		if (this->scrolling == -1) {
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   178
			this->vscroll.pos = max(0, this->vscroll.pos - 1);
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   179
			this->SetDirty();
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   180
		} else if (this->scrolling == 1) {
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   181
			this->vscroll.pos = min(this->vscroll.count - this->vscroll.cap, this->vscroll.pos + 1);
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   182
			this->SetDirty();
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   183
		}
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   184
		this->scrolling = 0;
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   185
	}
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   186
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   187
	virtual void OnMouseLoop()
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   188
	{
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   189
		Window *w2 = FindWindowById(this->parent_wnd_class, this->parent_wnd_num);
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   190
		if (w2 == NULL) {
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   191
			delete this;
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   192
			return;
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   193
		}
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   194
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   195
		if (this->click_delay != 0 && --this->click_delay == 0) {
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   196
			w2->OnDropdownSelect(this->parent_button, this->selected_index);
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   197
			delete this;
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   198
			return;
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   199
		}
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   200
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   201
		if (this->drag_mode) {
9209
2987efebe01d (svn r13075) -Codechange: Allow any value for a dropdown item instead of just positive.
peter1138
parents: 9189
diff changeset
   202
			int item;
9189
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   203
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   204
			if (!_left_button_clicked) {
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   205
				this->drag_mode = false;
9209
2987efebe01d (svn r13075) -Codechange: Allow any value for a dropdown item instead of just positive.
peter1138
parents: 9189
diff changeset
   206
				if (!this->GetDropDownItem(item)) return;
9189
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   207
				this->click_delay = 2;
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   208
			} else {
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   209
				if (_cursor.pos.y <= this->top + 2) {
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   210
					/* Cursor is above the list, set scroll up */
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   211
					this->scrolling = -1;
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   212
					return;
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   213
				} else if (_cursor.pos.y >= this->top + this->height - 2) {
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   214
					/* Cursor is below list, set scroll down */
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   215
					this->scrolling = 1;
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   216
					return;
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   217
				}
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   218
9209
2987efebe01d (svn r13075) -Codechange: Allow any value for a dropdown item instead of just positive.
peter1138
parents: 9189
diff changeset
   219
				if (!this->GetDropDownItem(item)) return;
8284
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
   220
			}
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
   221
9189
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   222
			this->selected_index = item;
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   223
			this->SetDirty();
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   224
		}
8284
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
   225
	}
9189
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   226
};
8284
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
   227
8877
f09f03b0dc64 (svn r12644) -Codechange: rework the order gui a little to prepare it for some future changes.
rubidium
parents: 8857
diff changeset
   228
void ShowDropDownList(Window *w, DropDownList *list, int selected, int button, uint width)
8284
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
   229
{
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
   230
	bool is_dropdown_menu_shown = w->IsWidgetLowered(button);
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
   231
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
   232
	DeleteWindowById(WC_DROPDOWN_MENU, 0);
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
   233
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
   234
	if (is_dropdown_menu_shown) {
8299
e8b5a9aef71a (svn r11863) -Fix (r11848): One day I'll learn C++... Delete all items in a drop down list before deleting the list.
peter1138
parents: 8294
diff changeset
   235
		DeleteDropDownList(list);
8284
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
   236
		return;
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
   237
	}
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
   238
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
   239
	w->LowerWidget(button);
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
   240
	w->InvalidateWidget(button);
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
   241
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
   242
	/* Our parent's button widget is used to determine where to place the drop
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
   243
	 * down list window. */
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
   244
	const Widget *wi = &w->widget[button];
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
   245
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
   246
	/* The preferred position is just below the dropdown calling widget */
8335
09a2e049b3a8 (svn r11901) -Add: add two widgets for dropdowns, one raised and one inset, to eventually replace use of two widgets for each dropdown control.
peter1138
parents: 8301
diff changeset
   247
	int top = w->top + wi->bottom + 1;
8905
609b1121b3b3 (svn r12672) -Codechange: Add support for variably-sized drop down list items.
peter1138
parents: 8884
diff changeset
   248
609b1121b3b3 (svn r12672) -Codechange: Add support for variably-sized drop down list items.
peter1138
parents: 8884
diff changeset
   249
	/* Total length of list */
609b1121b3b3 (svn r12672) -Codechange: Add support for variably-sized drop down list items.
peter1138
parents: 8884
diff changeset
   250
	int list_height = 0;
609b1121b3b3 (svn r12672) -Codechange: Add support for variably-sized drop down list items.
peter1138
parents: 8884
diff changeset
   251
609b1121b3b3 (svn r12672) -Codechange: Add support for variably-sized drop down list items.
peter1138
parents: 8884
diff changeset
   252
	for (DropDownList::const_iterator it = list->begin(); it != list->end(); ++it) {
609b1121b3b3 (svn r12672) -Codechange: Add support for variably-sized drop down list items.
peter1138
parents: 8884
diff changeset
   253
		DropDownListItem *item = *it;
609b1121b3b3 (svn r12672) -Codechange: Add support for variably-sized drop down list items.
peter1138
parents: 8884
diff changeset
   254
		list_height += item->Height(width);
609b1121b3b3 (svn r12672) -Codechange: Add support for variably-sized drop down list items.
peter1138
parents: 8884
diff changeset
   255
	}
609b1121b3b3 (svn r12672) -Codechange: Add support for variably-sized drop down list items.
peter1138
parents: 8884
diff changeset
   256
609b1121b3b3 (svn r12672) -Codechange: Add support for variably-sized drop down list items.
peter1138
parents: 8884
diff changeset
   257
	/* Height of window visible */
609b1121b3b3 (svn r12672) -Codechange: Add support for variably-sized drop down list items.
peter1138
parents: 8884
diff changeset
   258
	int height = list_height;
8284
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
   259
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
   260
	/* Check if the status bar is visible, as we don't want to draw over it */
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
   261
	Window *w3 = FindWindowById(WC_STATUS_BAR, 0);
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
   262
	int screen_bottom = w3 == NULL ? _screen.height : w3->top;
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
   263
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
   264
	bool scroll = false;
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
   265
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
   266
	/* Check if the dropdown will fully fit below the widget */
8905
609b1121b3b3 (svn r12672) -Codechange: Add support for variably-sized drop down list items.
peter1138
parents: 8884
diff changeset
   267
	if (top + height + 4 >= screen_bottom) {
8284
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
   268
		w3 = FindWindowById(WC_MAIN_TOOLBAR, 0);
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
   269
		int screen_top = w3 == NULL ? 0 : w3->top + w3->height;
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
   270
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
   271
		/* If not, check if it will fit above the widget */
8335
09a2e049b3a8 (svn r11901) -Add: add two widgets for dropdowns, one raised and one inset, to eventually replace use of two widgets for each dropdown control.
peter1138
parents: 8301
diff changeset
   272
		if (w->top + wi->top - height > screen_top) {
8905
609b1121b3b3 (svn r12672) -Codechange: Add support for variably-sized drop down list items.
peter1138
parents: 8884
diff changeset
   273
			top = w->top + wi->top - height - 4;
8284
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
   274
		} else {
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
   275
			/* ... and lastly if it won't, enable the scroll bar and fit the
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
   276
			 * list in below the widget */
9390
88d36f907e96 (svn r13301) -Fix [FS#1997]: resolve more MSVC 9 x64 warnings.
rubidium
parents: 9374
diff changeset
   277
			int avg_height = list_height / (int)list->size();
8905
609b1121b3b3 (svn r12672) -Codechange: Add support for variably-sized drop down list items.
peter1138
parents: 8884
diff changeset
   278
			int rows = (screen_bottom - 4 - top) / avg_height;
609b1121b3b3 (svn r12672) -Codechange: Add support for variably-sized drop down list items.
peter1138
parents: 8884
diff changeset
   279
			height = rows * avg_height;
8284
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
   280
			scroll = true;
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
   281
		}
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
   282
	}
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
   283
8877
f09f03b0dc64 (svn r12644) -Codechange: rework the order gui a little to prepare it for some future changes.
rubidium
parents: 8857
diff changeset
   284
	if (width == 0) width = wi->right - wi->left + 1;
f09f03b0dc64 (svn r12644) -Codechange: rework the order gui a little to prepare it for some future changes.
rubidium
parents: 8857
diff changeset
   285
9189
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   286
	DropdownWindow *dw = new DropdownWindow(
8358
cbd1a077e375 (svn r11924) -Codechange: All dropdowns now use the dropdown widget, so compatibility code can be removed
peter1138
parents: 8335
diff changeset
   287
		w->left + wi->left,
8284
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
   288
		top,
8877
f09f03b0dc64 (svn r12644) -Codechange: rework the order gui a little to prepare it for some future changes.
rubidium
parents: 8857
diff changeset
   289
		width,
8905
609b1121b3b3 (svn r12672) -Codechange: Add support for variably-sized drop down list items.
peter1138
parents: 8884
diff changeset
   290
		height + 4,
8284
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
   291
		_dropdown_menu_widgets);
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
   292
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
   293
	dw->widget[0].color = wi->color;
8884
cebc13b06266 (svn r12651) -Fix (r12644) [FS#1915]: Dropdown widget width should be one pixel less than window width, not one more.
peter1138
parents: 8877
diff changeset
   294
	dw->widget[0].right = width - 1;
8905
609b1121b3b3 (svn r12672) -Codechange: Add support for variably-sized drop down list items.
peter1138
parents: 8884
diff changeset
   295
	dw->widget[0].bottom = height + 3;
8284
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
   296
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
   297
	dw->SetWidgetHiddenState(1, !scroll);
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
   298
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
   299
	if (scroll) {
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
   300
		/* We're scrolling, so enable the scroll bar and shrink the list by
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
   301
		 * the scrollbar's width */
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
   302
		dw->widget[1].color  = wi->color;
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
   303
		dw->widget[1].right  = dw->widget[0].right;
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
   304
		dw->widget[1].left   = dw->widget[1].right - 11;
8905
609b1121b3b3 (svn r12672) -Codechange: Add support for variably-sized drop down list items.
peter1138
parents: 8884
diff changeset
   305
		dw->widget[1].bottom = dw->widget[0].bottom;
8284
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
   306
		dw->widget[0].right -= 12;
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
   307
8905
609b1121b3b3 (svn r12672) -Codechange: Add support for variably-sized drop down list items.
peter1138
parents: 8884
diff changeset
   308
		/* Capacity is the average number of items visible */
9390
88d36f907e96 (svn r13301) -Fix [FS#1997]: resolve more MSVC 9 x64 warnings.
rubidium
parents: 9374
diff changeset
   309
		dw->vscroll.cap   = height * (uint16)list->size() / list_height;
88d36f907e96 (svn r13301) -Fix [FS#1997]: resolve more MSVC 9 x64 warnings.
rubidium
parents: 9374
diff changeset
   310
		dw->vscroll.count = (uint16)list->size();
8284
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
   311
	}
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
   312
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
   313
	dw->desc_flags = WDF_DEF_WIDGET;
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
   314
	dw->flags4 &= ~WF_WHITE_BORDER_MASK;
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
   315
9189
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   316
	dw->parent_wnd_class = w->window_class;
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   317
	dw->parent_wnd_num   = w->window_number;
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   318
	dw->parent_button    = button;
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   319
	dw->list             = list;
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   320
	dw->selected_index   = selected;
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   321
	dw->click_delay      = 0;
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   322
	dw->drag_mode        = true;
8284
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
   323
}
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
   324
8877
f09f03b0dc64 (svn r12644) -Codechange: rework the order gui a little to prepare it for some future changes.
rubidium
parents: 8857
diff changeset
   325
void ShowDropDownMenu(Window *w, const StringID *strings, int selected, int button, uint32 disabled_mask, uint32 hidden_mask, uint width)
8284
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
   326
{
8299
e8b5a9aef71a (svn r11863) -Fix (r11848): One day I'll learn C++... Delete all items in a drop down list before deleting the list.
peter1138
parents: 8294
diff changeset
   327
	/* Don't create a new list if we're just closing an existing menu */
e8b5a9aef71a (svn r11863) -Fix (r11848): One day I'll learn C++... Delete all items in a drop down list before deleting the list.
peter1138
parents: 8294
diff changeset
   328
	if (w->IsWidgetLowered(button)) {
e8b5a9aef71a (svn r11863) -Fix (r11848): One day I'll learn C++... Delete all items in a drop down list before deleting the list.
peter1138
parents: 8294
diff changeset
   329
		DeleteWindowById(WC_DROPDOWN_MENU, 0);
e8b5a9aef71a (svn r11863) -Fix (r11848): One day I'll learn C++... Delete all items in a drop down list before deleting the list.
peter1138
parents: 8294
diff changeset
   330
		return;
e8b5a9aef71a (svn r11863) -Fix (r11848): One day I'll learn C++... Delete all items in a drop down list before deleting the list.
peter1138
parents: 8294
diff changeset
   331
	}
e8b5a9aef71a (svn r11863) -Fix (r11848): One day I'll learn C++... Delete all items in a drop down list before deleting the list.
peter1138
parents: 8294
diff changeset
   332
8284
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
   333
	uint result = 0;
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
   334
	DropDownList *list = new DropDownList();
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
   335
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
   336
	for (uint i = 0; strings[i] != INVALID_STRING_ID; i++) {
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
   337
		if (!HasBit(hidden_mask, i)) {
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
   338
			list->push_back(new DropDownListStringItem(strings[i], result, HasBit(disabled_mask, i)));
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
   339
		}
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
   340
		result++;
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
   341
	}
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
   342
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
   343
	/* No entries in the list? */
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
   344
	if (list->size() == 0) {
8299
e8b5a9aef71a (svn r11863) -Fix (r11848): One day I'll learn C++... Delete all items in a drop down list before deleting the list.
peter1138
parents: 8294
diff changeset
   345
		DeleteDropDownList(list);
8284
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
   346
		return;
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
   347
	}
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
   348
8877
f09f03b0dc64 (svn r12644) -Codechange: rework the order gui a little to prepare it for some future changes.
rubidium
parents: 8857
diff changeset
   349
	ShowDropDownList(w, list, selected, button, width);
8284
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
   350
}
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
   351
8857
7593af4962e6 (svn r12619) -Codechange: lots of documentation about window related stuff. Patch by Alberth.
rubidium
parents: 8543
diff changeset
   352
/**
7593af4962e6 (svn r12619) -Codechange: lots of documentation about window related stuff. Patch by Alberth.
rubidium
parents: 8543
diff changeset
   353
 * Delete the drop-down menu from window \a pw
7593af4962e6 (svn r12619) -Codechange: lots of documentation about window related stuff. Patch by Alberth.
rubidium
parents: 8543
diff changeset
   354
 * @param pw Parent window of the drop-down menu window
7593af4962e6 (svn r12619) -Codechange: lots of documentation about window related stuff. Patch by Alberth.
rubidium
parents: 8543
diff changeset
   355
 */
8284
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
   356
void HideDropDownMenu(Window *pw)
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
   357
{
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
   358
	Window **wz;
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
   359
	FOR_ALL_WINDOWS(wz) {
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
   360
		if ((*wz)->window_class != WC_DROPDOWN_MENU) continue;
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
   361
9189
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   362
		DropdownWindow *dw = dynamic_cast<DropdownWindow*>(*wz);
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   363
		if (pw->window_class == dw->parent_wnd_class &&
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   364
				pw->window_number == dw->parent_wnd_num) {
128a1809a977 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 9166
diff changeset
   365
			delete dw;
8284
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
   366
			break;
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
   367
		}
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
   368
	}
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
   369
}
ebdc5ba08874 (svn r11848) -Codechange: New class-based drop down list functionality. Lists are now dynamically generated, and can include parameters, or be extended however needed.
peter1138
parents:
diff changeset
   370