src/widgets/dropdown.cpp
author frosch
Sat, 28 Jun 2008 15:44:24 +0000
changeset 11092 e4fce2b3cded
parent 10751 ebd94f2d6385
permissions -rw-r--r--
(svn r13649) -Codechange: Split the GfxFillRect() special flags from 'color' into their own parameter.
8780
6f3f3ec6c69a (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$ */
6f3f3ec6c69a (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
10429
1b99254f9607 (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: 10399
diff changeset
     3
/** @file dropdown.cpp Implementation of the dropdown widget. */
1b99254f9607 (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: 10399
diff changeset
     4
8780
6f3f3ec6c69a (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"
6f3f3ec6c69a (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"
6f3f3ec6c69a (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"
6f3f3ec6c69a (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"
6f3f3ec6c69a (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"
6f3f3ec6c69a (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"
6f3f3ec6c69a (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"
6f3f3ec6c69a (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"
8797
e5a05b5700fe (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: 8795
diff changeset
    13
#include "../core/math_func.hpp"
8780
6f3f3ec6c69a (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"
6f3f3ec6c69a (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"
6f3f3ec6c69a (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
6f3f3ec6c69a (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"
6f3f3ec6c69a (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"
6f3f3ec6c69a (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
6f3f3ec6c69a (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
6f3f3ec6c69a (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
{
6f3f3ec6c69a (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;
6f3f3ec6c69a (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
}
6f3f3ec6c69a (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
10141
3ac62c310231 (svn r12672) -Codechange: Add support for variably-sized drop down list items.
peter1138
parents: 10120
diff changeset
    25
uint DropDownListItem::Height(uint width) const
3ac62c310231 (svn r12672) -Codechange: Add support for variably-sized drop down list items.
peter1138
parents: 10120
diff changeset
    26
{
3ac62c310231 (svn r12672) -Codechange: Add support for variably-sized drop down list items.
peter1138
parents: 10120
diff changeset
    27
	return 10;
3ac62c310231 (svn r12672) -Codechange: Add support for variably-sized drop down list items.
peter1138
parents: 10120
diff changeset
    28
}
3ac62c310231 (svn r12672) -Codechange: Add support for variably-sized drop down list items.
peter1138
parents: 10120
diff changeset
    29
8780
6f3f3ec6c69a (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
6f3f3ec6c69a (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
{
6f3f3ec6c69a (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;
6f3f3ec6c69a (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
}
6f3f3ec6c69a (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
6f3f3ec6c69a (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
6f3f3ec6c69a (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
{
6f3f3ec6c69a (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]);
6f3f3ec6c69a (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;
6f3f3ec6c69a (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
}
6f3f3ec6c69a (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
10144
52d27a99a331 (svn r12675) -Codechange: Add support for custom drawing of drop down list items.
peter1138
parents: 10141
diff changeset
    41
void DropDownListItem::Draw(int x, int y, uint width, uint height, bool sel) const
52d27a99a331 (svn r12675) -Codechange: Add support for custom drawing of drop down list items.
peter1138
parents: 10141
diff changeset
    42
{
52d27a99a331 (svn r12675) -Codechange: Add support for custom drawing of drop down list items.
peter1138
parents: 10141
diff changeset
    43
	DrawStringTruncated(x + 2, y, this->String(), sel ? TC_WHITE : TC_BLACK, x + width);
52d27a99a331 (svn r12675) -Codechange: Add support for custom drawing of drop down list items.
peter1138
parents: 10141
diff changeset
    44
}
52d27a99a331 (svn r12675) -Codechange: Add support for custom drawing of drop down list items.
peter1138
parents: 10141
diff changeset
    45
8795
c2f94c3c1b76 (svn r11863) -Fix (r11848): One day I'll learn C++... Delete all items in a drop down list before deleting the list.
peter1138
parents: 8790
diff changeset
    46
/**
c2f94c3c1b76 (svn r11863) -Fix (r11848): One day I'll learn C++... Delete all items in a drop down list before deleting the list.
peter1138
parents: 8790
diff changeset
    47
 * Delete all items of a drop down list and the list itself
c2f94c3c1b76 (svn r11863) -Fix (r11848): One day I'll learn C++... Delete all items in a drop down list before deleting the list.
peter1138
parents: 8790
diff changeset
    48
 * @param list List to delete.
c2f94c3c1b76 (svn r11863) -Fix (r11848): One day I'll learn C++... Delete all items in a drop down list before deleting the list.
peter1138
parents: 8790
diff changeset
    49
 */
c2f94c3c1b76 (svn r11863) -Fix (r11848): One day I'll learn C++... Delete all items in a drop down list before deleting the list.
peter1138
parents: 8790
diff changeset
    50
static void DeleteDropDownList(DropDownList *list)
c2f94c3c1b76 (svn r11863) -Fix (r11848): One day I'll learn C++... Delete all items in a drop down list before deleting the list.
peter1138
parents: 8790
diff changeset
    51
{
c2f94c3c1b76 (svn r11863) -Fix (r11848): One day I'll learn C++... Delete all items in a drop down list before deleting the list.
peter1138
parents: 8790
diff changeset
    52
	for (DropDownList::iterator it = list->begin(); it != list->end(); ++it) {
c2f94c3c1b76 (svn r11863) -Fix (r11848): One day I'll learn C++... Delete all items in a drop down list before deleting the list.
peter1138
parents: 8790
diff changeset
    53
		DropDownListItem *item = *it;
c2f94c3c1b76 (svn r11863) -Fix (r11848): One day I'll learn C++... Delete all items in a drop down list before deleting the list.
peter1138
parents: 8790
diff changeset
    54
		delete item;
c2f94c3c1b76 (svn r11863) -Fix (r11848): One day I'll learn C++... Delete all items in a drop down list before deleting the list.
peter1138
parents: 8790
diff changeset
    55
	}
c2f94c3c1b76 (svn r11863) -Fix (r11848): One day I'll learn C++... Delete all items in a drop down list before deleting the list.
peter1138
parents: 8790
diff changeset
    56
	delete list;
c2f94c3c1b76 (svn r11863) -Fix (r11848): One day I'll learn C++... Delete all items in a drop down list before deleting the list.
peter1138
parents: 8790
diff changeset
    57
}
c2f94c3c1b76 (svn r11863) -Fix (r11848): One day I'll learn C++... Delete all items in a drop down list before deleting the list.
peter1138
parents: 8790
diff changeset
    58
10509
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
    59
static const Widget _dropdown_menu_widgets[] = {
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
    60
{      WWT_PANEL,   RESIZE_NONE,     0,     0, 0,     0, 0, 0x0, STR_NULL},
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
    61
{  WWT_SCROLLBAR,   RESIZE_NONE,     0,     0, 0,     0, 0, 0x0, STR_0190_SCROLL_BAR_SCROLLS_LIST},
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
    62
{   WIDGETS_END},
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
    63
};
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
    64
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
    65
struct DropdownWindow : Window {
8780
6f3f3ec6c69a (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;
6f3f3ec6c69a (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;
6f3f3ec6c69a (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;
6f3f3ec6c69a (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;
9039
dc7fc84d1372 (svn r12121) -Fix [FS#1764]: Drop down item indexes are now an int instead of a byte (Yexo)
peter1138
parents: 8854
diff changeset
    70
	int selected_index;
8780
6f3f3ec6c69a (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;
6f3f3ec6c69a (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;
8797
e5a05b5700fe (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: 8795
diff changeset
    73
	int scrolling;
8780
6f3f3ec6c69a (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
10625
3c1f6a26db71 (svn r13169) -Codechange: remove a (now) unneeded parameter of one of the Window constructors.
rubidium
parents: 10595
diff changeset
    75
	DropdownWindow(int x, int y, int width, int height, const Widget *widget) : Window(x, y, width, height, WC_DROPDOWN_MENU, widget)
10509
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
    76
	{
10728
01b05f87517f (svn r13278) -Fix: Missing call to FindWindowPlacementAndResize() for drop down lists
peter1138
parents: 10625
diff changeset
    77
		this->FindWindowPlacementAndResize(width, height);
8780
6f3f3ec6c69a (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
	}
6f3f3ec6c69a (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
10509
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
    80
	~DropdownWindow()
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
    81
	{
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
    82
		Window *w2 = FindWindowById(this->parent_wnd_class, this->parent_wnd_num);
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
    83
		if (w2 != NULL) {
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
    84
			w2->RaiseWidget(this->parent_button);
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
    85
			w2->InvalidateWidget(this->parent_button);
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
    86
		}
8780
6f3f3ec6c69a (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
10509
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
    88
		DeleteDropDownList(this->list);
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
    89
	}
8780
6f3f3ec6c69a (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
10531
72bdb97b2bc6 (svn r13075) -Codechange: Allow any value for a dropdown item instead of just positive.
peter1138
parents: 10509
diff changeset
    91
	bool GetDropDownItem(int &value)
10509
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
    92
	{
10531
72bdb97b2bc6 (svn r13075) -Codechange: Allow any value for a dropdown item instead of just positive.
peter1138
parents: 10509
diff changeset
    93
		if (GetWidgetFromPos(this, _cursor.pos.x - this->left, _cursor.pos.y - this->top) < 0) return false;
8780
6f3f3ec6c69a (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
10509
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
    95
		int y     = _cursor.pos.y - this->top - 2;
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
    96
		int width = this->widget[0].right - 3;
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
    97
		int pos   = this->vscroll.pos;
8797
e5a05b5700fe (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: 8795
diff changeset
    98
10509
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
    99
		const DropDownList *list = this->list;
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   100
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   101
		for (DropDownList::const_iterator it = list->begin(); it != list->end(); ++it) {
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   102
			/* Skip items that are scrolled up */
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   103
			if (--pos >= 0) continue;
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   104
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   105
			const DropDownListItem *item = *it;
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   106
			int item_height = item->Height(width);
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   107
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   108
			if (y < item_height) {
10531
72bdb97b2bc6 (svn r13075) -Codechange: Allow any value for a dropdown item instead of just positive.
peter1138
parents: 10509
diff changeset
   109
				if (item->masked || item->String() == STR_NULL) return false;
72bdb97b2bc6 (svn r13075) -Codechange: Allow any value for a dropdown item instead of just positive.
peter1138
parents: 10509
diff changeset
   110
				value = item->result;
72bdb97b2bc6 (svn r13075) -Codechange: Allow any value for a dropdown item instead of just positive.
peter1138
parents: 10509
diff changeset
   111
				return true;
8780
6f3f3ec6c69a (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
			}
6f3f3ec6c69a (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
10509
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   114
			y -= item_height;
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   115
		}
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   116
10531
72bdb97b2bc6 (svn r13075) -Codechange: Allow any value for a dropdown item instead of just positive.
peter1138
parents: 10509
diff changeset
   117
		return false;
10509
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   118
	}
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   119
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   120
	virtual void OnPaint()
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   121
	{
10595
7957c71b0dfe (svn r13139) -Codechange: move DrawWindowWidgets and DrawWindowViewport to the Window class and remove Window from their naming.
rubidium
parents: 10531
diff changeset
   122
		this->DrawWidgets();
10509
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   123
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   124
		int x = 1;
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   125
		int y = 2;
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   126
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   127
		int sel    = this->selected_index;
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   128
		int width  = this->widget[0].right - 3;
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   129
		int height = this->widget[0].bottom;
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   130
		int pos    = this->vscroll.pos;
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   131
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   132
		DropDownList *list = this->list;
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   133
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   134
		for (DropDownList::const_iterator it = list->begin(); it != list->end(); ++it) {
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   135
			const DropDownListItem *item = *it;
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   136
			int item_height = item->Height(width);
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   137
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   138
			/* Skip items that are scrolled up */
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   139
			if (--pos >= 0) continue;
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   140
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   141
			if (y + item_height < height) {
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   142
				if (item->String() != STR_NULL) {
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   143
					if (sel == item->result) GfxFillRect(x + 1, y, x + width, y + item_height - 1, 0);
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   144
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   145
					item->Draw(x, y, width, 10, sel == item->result);
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   146
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   147
					if (item->masked) {
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   148
						GfxFillRect(x, y, x + width, y + item_height - 1,
11092
e4fce2b3cded (svn r13649) -Codechange: Split the GfxFillRect() special flags from 'color' into their own parameter.
frosch
parents: 10751
diff changeset
   149
							_colour_gradient[this->widget[0].color][5], FILLRECT_CHECKER
10509
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   150
						);
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   151
					}
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   152
				} else {
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   153
					int c1 = _colour_gradient[this->widget[0].color][3];
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   154
					int c2 = _colour_gradient[this->widget[0].color][7];
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   155
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   156
					GfxFillRect(x + 1, y + 3, x + this->width - 5, y + 3, c1);
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   157
					GfxFillRect(x + 1, y + 4, x + this->width - 5, y + 4, c2);
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   158
				}
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   159
			}
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   160
			y += item_height;
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   161
		}
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   162
	};
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   163
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   164
	virtual void OnClick(Point pt, int widget)
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   165
	{
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   166
		if (widget != 0) return;
10531
72bdb97b2bc6 (svn r13075) -Codechange: Allow any value for a dropdown item instead of just positive.
peter1138
parents: 10509
diff changeset
   167
		int item;
72bdb97b2bc6 (svn r13075) -Codechange: Allow any value for a dropdown item instead of just positive.
peter1138
parents: 10509
diff changeset
   168
		if (this->GetDropDownItem(item)) {
10509
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   169
			this->click_delay = 4;
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   170
			this->selected_index = item;
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   171
			this->SetDirty();
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   172
		}
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   173
	}
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   174
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   175
	virtual void OnTick()
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   176
	{
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   177
		if (this->scrolling == -1) {
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   178
			this->vscroll.pos = max(0, this->vscroll.pos - 1);
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   179
			this->SetDirty();
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   180
		} else if (this->scrolling == 1) {
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   181
			this->vscroll.pos = min(this->vscroll.count - this->vscroll.cap, this->vscroll.pos + 1);
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   182
			this->SetDirty();
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   183
		}
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   184
		this->scrolling = 0;
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   185
	}
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   186
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   187
	virtual void OnMouseLoop()
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   188
	{
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   189
		Window *w2 = FindWindowById(this->parent_wnd_class, this->parent_wnd_num);
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   190
		if (w2 == NULL) {
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   191
			delete this;
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   192
			return;
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   193
		}
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   194
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   195
		if (this->click_delay != 0 && --this->click_delay == 0) {
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   196
			w2->OnDropdownSelect(this->parent_button, this->selected_index);
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   197
			delete this;
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   198
			return;
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   199
		}
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   200
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   201
		if (this->drag_mode) {
10531
72bdb97b2bc6 (svn r13075) -Codechange: Allow any value for a dropdown item instead of just positive.
peter1138
parents: 10509
diff changeset
   202
			int item;
10509
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   203
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   204
			if (!_left_button_clicked) {
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   205
				this->drag_mode = false;
10531
72bdb97b2bc6 (svn r13075) -Codechange: Allow any value for a dropdown item instead of just positive.
peter1138
parents: 10509
diff changeset
   206
				if (!this->GetDropDownItem(item)) return;
10509
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   207
				this->click_delay = 2;
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   208
			} else {
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   209
				if (_cursor.pos.y <= this->top + 2) {
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   210
					/* Cursor is above the list, set scroll up */
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   211
					this->scrolling = -1;
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   212
					return;
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   213
				} else if (_cursor.pos.y >= this->top + this->height - 2) {
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   214
					/* Cursor is below list, set scroll down */
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   215
					this->scrolling = 1;
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   216
					return;
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   217
				}
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   218
10531
72bdb97b2bc6 (svn r13075) -Codechange: Allow any value for a dropdown item instead of just positive.
peter1138
parents: 10509
diff changeset
   219
				if (!this->GetDropDownItem(item)) return;
8780
6f3f3ec6c69a (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
			}
6f3f3ec6c69a (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
10509
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   222
			this->selected_index = item;
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   223
			this->SetDirty();
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   224
		}
8780
6f3f3ec6c69a (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
	}
10509
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   226
};
8780
6f3f3ec6c69a (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
10113
0233cd056e70 (svn r12644) -Codechange: rework the order gui a little to prepare it for some future changes.
rubidium
parents: 10083
diff changeset
   228
void ShowDropDownList(Window *w, DropDownList *list, int selected, int button, uint width)
8780
6f3f3ec6c69a (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
{
6f3f3ec6c69a (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);
6f3f3ec6c69a (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
6f3f3ec6c69a (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);
6f3f3ec6c69a (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
6f3f3ec6c69a (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) {
8795
c2f94c3c1b76 (svn r11863) -Fix (r11848): One day I'll learn C++... Delete all items in a drop down list before deleting the list.
peter1138
parents: 8790
diff changeset
   235
		DeleteDropDownList(list);
8780
6f3f3ec6c69a (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;
6f3f3ec6c69a (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
	}
6f3f3ec6c69a (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
6f3f3ec6c69a (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);
6f3f3ec6c69a (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);
6f3f3ec6c69a (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
6f3f3ec6c69a (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
6f3f3ec6c69a (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. */
6f3f3ec6c69a (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];
6f3f3ec6c69a (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
6f3f3ec6c69a (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 */
8831
c13fdbe2783a (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: 8797
diff changeset
   247
	int top = w->top + wi->bottom + 1;
10141
3ac62c310231 (svn r12672) -Codechange: Add support for variably-sized drop down list items.
peter1138
parents: 10120
diff changeset
   248
3ac62c310231 (svn r12672) -Codechange: Add support for variably-sized drop down list items.
peter1138
parents: 10120
diff changeset
   249
	/* Total length of list */
3ac62c310231 (svn r12672) -Codechange: Add support for variably-sized drop down list items.
peter1138
parents: 10120
diff changeset
   250
	int list_height = 0;
3ac62c310231 (svn r12672) -Codechange: Add support for variably-sized drop down list items.
peter1138
parents: 10120
diff changeset
   251
3ac62c310231 (svn r12672) -Codechange: Add support for variably-sized drop down list items.
peter1138
parents: 10120
diff changeset
   252
	for (DropDownList::const_iterator it = list->begin(); it != list->end(); ++it) {
3ac62c310231 (svn r12672) -Codechange: Add support for variably-sized drop down list items.
peter1138
parents: 10120
diff changeset
   253
		DropDownListItem *item = *it;
3ac62c310231 (svn r12672) -Codechange: Add support for variably-sized drop down list items.
peter1138
parents: 10120
diff changeset
   254
		list_height += item->Height(width);
3ac62c310231 (svn r12672) -Codechange: Add support for variably-sized drop down list items.
peter1138
parents: 10120
diff changeset
   255
	}
3ac62c310231 (svn r12672) -Codechange: Add support for variably-sized drop down list items.
peter1138
parents: 10120
diff changeset
   256
3ac62c310231 (svn r12672) -Codechange: Add support for variably-sized drop down list items.
peter1138
parents: 10120
diff changeset
   257
	/* Height of window visible */
3ac62c310231 (svn r12672) -Codechange: Add support for variably-sized drop down list items.
peter1138
parents: 10120
diff changeset
   258
	int height = list_height;
8780
6f3f3ec6c69a (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
6f3f3ec6c69a (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 */
6f3f3ec6c69a (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);
6f3f3ec6c69a (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;
6f3f3ec6c69a (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
6f3f3ec6c69a (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;
6f3f3ec6c69a (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
6f3f3ec6c69a (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 */
10141
3ac62c310231 (svn r12672) -Codechange: Add support for variably-sized drop down list items.
peter1138
parents: 10120
diff changeset
   267
	if (top + height + 4 >= screen_bottom) {
8780
6f3f3ec6c69a (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);
6f3f3ec6c69a (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;
6f3f3ec6c69a (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
6f3f3ec6c69a (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 */
8831
c13fdbe2783a (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: 8797
diff changeset
   272
		if (w->top + wi->top - height > screen_top) {
10141
3ac62c310231 (svn r12672) -Codechange: Add support for variably-sized drop down list items.
peter1138
parents: 10120
diff changeset
   273
			top = w->top + wi->top - height - 4;
8780
6f3f3ec6c69a (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 {
6f3f3ec6c69a (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
6f3f3ec6c69a (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 */
10751
ebd94f2d6385 (svn r13301) -Fix [FS#1997]: resolve more MSVC 9 x64 warnings.
rubidium
parents: 10728
diff changeset
   277
			int avg_height = list_height / (int)list->size();
10141
3ac62c310231 (svn r12672) -Codechange: Add support for variably-sized drop down list items.
peter1138
parents: 10120
diff changeset
   278
			int rows = (screen_bottom - 4 - top) / avg_height;
3ac62c310231 (svn r12672) -Codechange: Add support for variably-sized drop down list items.
peter1138
parents: 10120
diff changeset
   279
			height = rows * avg_height;
8780
6f3f3ec6c69a (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;
6f3f3ec6c69a (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
		}
6f3f3ec6c69a (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
	}
6f3f3ec6c69a (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
10113
0233cd056e70 (svn r12644) -Codechange: rework the order gui a little to prepare it for some future changes.
rubidium
parents: 10083
diff changeset
   284
	if (width == 0) width = wi->right - wi->left + 1;
0233cd056e70 (svn r12644) -Codechange: rework the order gui a little to prepare it for some future changes.
rubidium
parents: 10083
diff changeset
   285
10509
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   286
	DropdownWindow *dw = new DropdownWindow(
8854
6ec8a7e74a2a (svn r11924) -Codechange: All dropdowns now use the dropdown widget, so compatibility code can be removed
peter1138
parents: 8831
diff changeset
   287
		w->left + wi->left,
8780
6f3f3ec6c69a (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,
10113
0233cd056e70 (svn r12644) -Codechange: rework the order gui a little to prepare it for some future changes.
rubidium
parents: 10083
diff changeset
   289
		width,
10141
3ac62c310231 (svn r12672) -Codechange: Add support for variably-sized drop down list items.
peter1138
parents: 10120
diff changeset
   290
		height + 4,
8780
6f3f3ec6c69a (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);
6f3f3ec6c69a (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
6f3f3ec6c69a (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;
10120
185a6a9d5d10 (svn r12651) -Fix (r12644) [FS#1915]: Dropdown widget width should be one pixel less than window width, not one more.
peter1138
parents: 10113
diff changeset
   294
	dw->widget[0].right = width - 1;
10141
3ac62c310231 (svn r12672) -Codechange: Add support for variably-sized drop down list items.
peter1138
parents: 10120
diff changeset
   295
	dw->widget[0].bottom = height + 3;
8780
6f3f3ec6c69a (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
6f3f3ec6c69a (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);
6f3f3ec6c69a (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
6f3f3ec6c69a (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) {
6f3f3ec6c69a (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
6f3f3ec6c69a (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 */
6f3f3ec6c69a (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;
6f3f3ec6c69a (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;
6f3f3ec6c69a (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;
10141
3ac62c310231 (svn r12672) -Codechange: Add support for variably-sized drop down list items.
peter1138
parents: 10120
diff changeset
   305
		dw->widget[1].bottom = dw->widget[0].bottom;
8780
6f3f3ec6c69a (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;
6f3f3ec6c69a (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
10141
3ac62c310231 (svn r12672) -Codechange: Add support for variably-sized drop down list items.
peter1138
parents: 10120
diff changeset
   308
		/* Capacity is the average number of items visible */
10751
ebd94f2d6385 (svn r13301) -Fix [FS#1997]: resolve more MSVC 9 x64 warnings.
rubidium
parents: 10728
diff changeset
   309
		dw->vscroll.cap   = height * (uint16)list->size() / list_height;
ebd94f2d6385 (svn r13301) -Fix [FS#1997]: resolve more MSVC 9 x64 warnings.
rubidium
parents: 10728
diff changeset
   310
		dw->vscroll.count = (uint16)list->size();
8780
6f3f3ec6c69a (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
	}
6f3f3ec6c69a (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
6f3f3ec6c69a (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;
6f3f3ec6c69a (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;
6f3f3ec6c69a (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
10509
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   316
	dw->parent_wnd_class = w->window_class;
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   317
	dw->parent_wnd_num   = w->window_number;
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   318
	dw->parent_button    = button;
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   319
	dw->list             = list;
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   320
	dw->selected_index   = selected;
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   321
	dw->click_delay      = 0;
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   322
	dw->drag_mode        = true;
8780
6f3f3ec6c69a (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
}
6f3f3ec6c69a (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
10113
0233cd056e70 (svn r12644) -Codechange: rework the order gui a little to prepare it for some future changes.
rubidium
parents: 10083
diff changeset
   325
void ShowDropDownMenu(Window *w, const StringID *strings, int selected, int button, uint32 disabled_mask, uint32 hidden_mask, uint width)
8780
6f3f3ec6c69a (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
{
8795
c2f94c3c1b76 (svn r11863) -Fix (r11848): One day I'll learn C++... Delete all items in a drop down list before deleting the list.
peter1138
parents: 8790
diff changeset
   327
	/* Don't create a new list if we're just closing an existing menu */
c2f94c3c1b76 (svn r11863) -Fix (r11848): One day I'll learn C++... Delete all items in a drop down list before deleting the list.
peter1138
parents: 8790
diff changeset
   328
	if (w->IsWidgetLowered(button)) {
c2f94c3c1b76 (svn r11863) -Fix (r11848): One day I'll learn C++... Delete all items in a drop down list before deleting the list.
peter1138
parents: 8790
diff changeset
   329
		DeleteWindowById(WC_DROPDOWN_MENU, 0);
c2f94c3c1b76 (svn r11863) -Fix (r11848): One day I'll learn C++... Delete all items in a drop down list before deleting the list.
peter1138
parents: 8790
diff changeset
   330
		return;
c2f94c3c1b76 (svn r11863) -Fix (r11848): One day I'll learn C++... Delete all items in a drop down list before deleting the list.
peter1138
parents: 8790
diff changeset
   331
	}
c2f94c3c1b76 (svn r11863) -Fix (r11848): One day I'll learn C++... Delete all items in a drop down list before deleting the list.
peter1138
parents: 8790
diff changeset
   332
8780
6f3f3ec6c69a (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;
6f3f3ec6c69a (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();
6f3f3ec6c69a (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
6f3f3ec6c69a (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++) {
6f3f3ec6c69a (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)) {
6f3f3ec6c69a (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)));
6f3f3ec6c69a (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
		}
6f3f3ec6c69a (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++;
6f3f3ec6c69a (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
	}
6f3f3ec6c69a (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
6f3f3ec6c69a (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? */
6f3f3ec6c69a (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) {
8795
c2f94c3c1b76 (svn r11863) -Fix (r11848): One day I'll learn C++... Delete all items in a drop down list before deleting the list.
peter1138
parents: 8790
diff changeset
   345
		DeleteDropDownList(list);
8780
6f3f3ec6c69a (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;
6f3f3ec6c69a (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
	}
6f3f3ec6c69a (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
10113
0233cd056e70 (svn r12644) -Codechange: rework the order gui a little to prepare it for some future changes.
rubidium
parents: 10083
diff changeset
   349
	ShowDropDownList(w, list, selected, button, width);
8780
6f3f3ec6c69a (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
}
6f3f3ec6c69a (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
10083
9fb63cf5cc90 (svn r12619) -Codechange: lots of documentation about window related stuff. Patch by Alberth.
rubidium
parents: 9039
diff changeset
   352
/**
9fb63cf5cc90 (svn r12619) -Codechange: lots of documentation about window related stuff. Patch by Alberth.
rubidium
parents: 9039
diff changeset
   353
 * Delete the drop-down menu from window \a pw
9fb63cf5cc90 (svn r12619) -Codechange: lots of documentation about window related stuff. Patch by Alberth.
rubidium
parents: 9039
diff changeset
   354
 * @param pw Parent window of the drop-down menu window
9fb63cf5cc90 (svn r12619) -Codechange: lots of documentation about window related stuff. Patch by Alberth.
rubidium
parents: 9039
diff changeset
   355
 */
8780
6f3f3ec6c69a (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)
6f3f3ec6c69a (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
{
6f3f3ec6c69a (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;
6f3f3ec6c69a (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) {
6f3f3ec6c69a (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;
6f3f3ec6c69a (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
10509
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   362
		DropdownWindow *dw = dynamic_cast<DropdownWindow*>(*wz);
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   363
		if (pw->window_class == dw->parent_wnd_class &&
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   364
				pw->window_number == dw->parent_wnd_num) {
371b59387f92 (svn r13052) -Codechange: make a class of the DropdownWindow.
rubidium
parents: 10486
diff changeset
   365
			delete dw;
8780
6f3f3ec6c69a (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;
6f3f3ec6c69a (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
		}
6f3f3ec6c69a (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
	}
6f3f3ec6c69a (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
}
6f3f3ec6c69a (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