Changeset - r11832:0a9347eb1597
[Not reviewed]
master
0 2 0
alberth - 16 years ago 2009-05-04 18:42:49
alberth@openttd.org
(svn r16227) -Codechange: Added nested widgets for error windows, tooltip window, and dropdown window.
2 files changed with 69 insertions and 9 deletions:
0 comments (0 inline, 0 general)
src/misc_gui.cpp
Show inline comments
 
@@ -439,33 +439,70 @@ enum ErrorMessageWidgets {
 
	EMW_MESSAGE,
 
};
 

	
 
static const Widget _errmsg_widgets[] = {
 
{   WWT_CLOSEBOX,   RESIZE_NONE,    COLOUR_RED,     0,    10,     0,    13, STR_BLACK_CROSS,           STR_TOOLTIP_CLOSE_WINDOW},
 
{    WWT_CAPTION,   RESIZE_NONE,    COLOUR_RED,    11,   239,     0,    13, STR_ERROR_MESSAGE_CAPTION, STR_NULL},
 
{      WWT_PANEL,   RESIZE_BOTTOM,  COLOUR_RED,     0,   239,    14,    45, 0x0,                       STR_NULL},
 
{      WWT_EMPTY,   RESIZE_NONE,    COLOUR_RED,     0,     0,     0,     0, 0x0,                       STR_NULL},
 
{      WWT_EMPTY,   RESIZE_NONE,    COLOUR_RED,     2,   237,    14,    45, 0x0,                       STR_NULL},
 
{    WIDGETS_END},
 
};
 

	
 
static const NWidgetPart _nested_errmsg_widgets[] = {
 
	NWidget(NWID_LAYERED),
 
		NWidget(NWID_VERTICAL),
 
			NWidget(NWID_HORIZONTAL),
 
				NWidget(WWT_CLOSEBOX, COLOUR_RED, EMW_CLOSE),
 
				NWidget(WWT_CAPTION, COLOUR_RED, EMW_CAPTION), SetDataTip(STR_ERROR_MESSAGE_CAPTION, STR_NULL),
 
			EndContainer(),
 
			NWidget(WWT_PANEL, COLOUR_RED, EMW_PANEL),
 
				NWidget(WWT_EMPTY, COLOUR_RED, EMW_MESSAGE), SetPadding(0, 2, 0, 2), SetMinimalSize(236, 32),
 
				NWidget(NWID_SPACER), SetResize(0, 1),
 
			EndContainer(),
 
		EndContainer(),
 
		NWidget(NWID_VERTICAL),
 
			NWidget(NWID_HORIZONTAL),
 
				NWidget(WWT_EMPTY, COLOUR_RED, EMW_FACE), SetMinimalSize(1, 1), SetFill(false, false),
 
				NWidget(NWID_SPACER), SetFill(1, 0),
 
			EndContainer(),
 
			NWidget(NWID_SPACER), SetFill(1, 1), SetResize(0, 1),
 
		EndContainer(),
 
	EndContainer(),
 
};
 

	
 
static const Widget _errmsg_face_widgets[] = {
 
{   WWT_CLOSEBOX,   RESIZE_NONE,    COLOUR_RED,     0,    10,     0,    13, STR_BLACK_CROSS,                         STR_TOOLTIP_CLOSE_WINDOW},
 
{    WWT_CAPTION,   RESIZE_NONE,    COLOUR_RED,    11,   333,     0,    13, STR_ERROR_MESSAGE_CAPTION_OTHER_COMPANY, STR_NULL},
 
{      WWT_PANEL,   RESIZE_BOTTOM,  COLOUR_RED,     0,   333,    14,   136, 0x0,                                     STR_NULL},
 
{      WWT_EMPTY,   RESIZE_NONE,    COLOUR_RED,     2,    92,    16,   135, 0x0,                                     STR_NULL},
 
{      WWT_EMPTY,   RESIZE_NONE,    COLOUR_RED,    94,   331,    14,   136, 0x0,                                     STR_NULL},
 
{   WIDGETS_END},
 
};
 

	
 
static const NWidgetPart _nested_errmsg_face_widgets[] = {
 
		NWidget(NWID_HORIZONTAL),
 
			NWidget(WWT_CLOSEBOX, COLOUR_RED, EMW_CLOSE),
 
			NWidget(WWT_CAPTION, COLOUR_RED, EMW_CAPTION), SetDataTip(STR_ERROR_MESSAGE_CAPTION_OTHER_COMPANY, STR_NULL),
 
		EndContainer(),
 
		NWidget(WWT_PANEL, COLOUR_RED, EMW_PANEL),
 
			NWidget(NWID_HORIZONTAL), SetPIP(2, 1, 2),
 
				NWidget(WWT_EMPTY, COLOUR_RED, EMW_FACE), SetMinimalSize(91, 120), SetPadding(2, 0, 1, 0),
 
				NWidget(WWT_EMPTY, COLOUR_RED, EMW_MESSAGE), SetMinimalSize(238, 123),
 
			EndContainer(),
 
			NWidget(NWID_SPACER), SetResize(0, 1),
 
		EndContainer(),
 
	EndContainer(),
 
};
 

	
 
struct ErrmsgWindow : public Window {
 
private:
 
	uint duration;
 
	uint64 decode_params[20];
 
	StringID message_1;
 
	StringID message_2;
 
	bool show_company_manager_face;
 

	
 
	int y[4];
 

	
 
public:
 
	ErrmsgWindow(Point pt, int width, int height, StringID msg1, StringID msg2, const Widget *widget, bool show_company_manager_face) :
 
@@ -548,24 +585,27 @@ public:
 
	}
 

	
 
	virtual EventState OnKeyPress(uint16 key, uint16 keycode)
 
	{
 
		if (keycode != WKC_SPACE) return ES_NOT_HANDLED;
 
		delete this;
 
		return ES_HANDLED;
 
	}
 
};
 

	
 
void ShowErrorMessage(StringID msg_1, StringID msg_2, int x, int y)
 
{
 
	static Widget *generated_errmsg_widgets = NULL;
 
	static Widget *generated_errmsg_face_widgets = NULL;
 

	
 
	DeleteWindowById(WC_ERRMSG, 0);
 

	
 
	if (!_settings_client.gui.errmsg_duration) return;
 

	
 
	if (msg_2 == STR_NULL) msg_2 = STR_EMPTY;
 

	
 
	Point pt;
 
	const ViewPort *vp;
 

	
 
	if (msg_1 != STR_ERROR_OWNED_BY || GetDParam(2) >= MAX_COMPANIES) {
 
		if ((x | y) != 0) {
 
			pt = RemapCoords2(x, y);
 
@@ -574,36 +614,42 @@ void ShowErrorMessage(StringID msg_1, St
 
			/* move x pos to opposite corner */
 
			pt.x = UnScaleByZoom(pt.x - vp->virtual_left, vp->zoom) + vp->left;
 
			pt.x = (pt.x < (_screen.width >> 1)) ? _screen.width - 260 : 20;
 

	
 
			/* move y pos to opposite corner */
 
			pt.y = UnScaleByZoom(pt.y - vp->virtual_top, vp->zoom) + vp->top;
 
			pt.y = (pt.y < (_screen.height >> 1)) ? _screen.height - 80 : 100;
 

	
 
		} else {
 
			pt.x = (_screen.width - 240) >> 1;
 
			pt.y = (_screen.height - 46) >> 1;
 
		}
 
		new ErrmsgWindow(pt, 240, 46, msg_1, msg_2, _errmsg_widgets, false);
 

	
 
		const Widget *wid = InitializeWidgetArrayFromNestedWidgets(_nested_errmsg_widgets, lengthof(_nested_errmsg_widgets),
 
													_errmsg_widgets, &generated_errmsg_widgets);
 
		new ErrmsgWindow(pt, 240, 46, msg_1, msg_2, wid, false);
 
	} else {
 
		if ((x | y) != 0) {
 
			pt = RemapCoords2(x, y);
 
			vp = FindWindowById(WC_MAIN_WINDOW, 0)->viewport;
 
			pt.x = Clamp(UnScaleByZoom(pt.x - vp->virtual_left, vp->zoom) + vp->left - (334 / 2),  0, _screen.width  - 334);
 
			pt.y = Clamp(UnScaleByZoom(pt.y - vp->virtual_top,  vp->zoom) + vp->top  - (137 / 2), 22, _screen.height - 137);
 
		} else {
 
			pt.x = (_screen.width  - 334) >> 1;
 
			pt.y = (_screen.height - 137) >> 1;
 
		}
 
		new ErrmsgWindow(pt, 334, 137, msg_1, msg_2, _errmsg_face_widgets, true);
 

	
 
		const Widget *wid = InitializeWidgetArrayFromNestedWidgets(_nested_errmsg_face_widgets, lengthof(_nested_errmsg_face_widgets),
 
													_errmsg_face_widgets, &generated_errmsg_face_widgets);
 
		new ErrmsgWindow(pt, 334, 137, msg_1, msg_2, wid, true);
 
	}
 
}
 

	
 
void ShowEstimatedCostOrIncome(Money cost, int x, int y)
 
{
 
	StringID msg = STR_MESSAGE_ESTIMATED_COST;
 

	
 
	if (cost < 0) {
 
		cost = -cost;
 
		msg = STR_MESSAGE_ESTIMATED_INCOME;
 
	}
 
	SetDParam(0, cost);
 
@@ -653,24 +699,28 @@ void HideFillingPercent(TextEffectID *te
 
{
 
	if (*te_id == INVALID_TE_ID) return;
 

	
 
	RemoveTextEffect(*te_id);
 
	*te_id = INVALID_TE_ID;
 
}
 

	
 
static const Widget _tooltips_widgets[] = {
 
{      WWT_PANEL,   RESIZE_NONE,  COLOUR_GREY,     0,   199,     0,    31, 0x0, STR_NULL},
 
{   WIDGETS_END},
 
};
 

	
 
static const NWidgetPart _nested_tooltips_widgets[] = {
 
	NWidget(WWT_PANEL, COLOUR_GREY, 0), SetMinimalSize(200, 32), EndContainer(),
 
};
 

	
 
struct TooltipsWindow : public Window
 
{
 
	StringID string_id;
 
	byte paramcount;
 
	uint64 params[5];
 
	bool use_left_mouse_button;
 

	
 
	TooltipsWindow(int x, int y, int width, int height, const Widget *widget,
 
								 StringID str, uint paramcount, const uint64 params[], bool use_left_mouse_button) :
 
			Window(x, y, width, height, WC_TOOLTIPS, widget)
 
	{
 
		this->string_id = str;
 
@@ -705,49 +755,53 @@ struct TooltipsWindow : public Window
 
		if (this->use_left_mouse_button ? !_left_button_down : !_right_button_down) delete this;
 
	}
 
};
 

	
 
/** Shows a tooltip
 
 * @param str String to be displayed
 
 * @param paramcount number of params to deal with
 
 * @param params (optional) up to 5 pieces of additional information that may be added to a tooltip
 
 * @param use_left_mouse_button close the tooltip when the left (true) or right (false) mousebutton is released
 
 */
 
void GuiShowTooltips(StringID str, uint paramcount, const uint64 params[], bool use_left_mouse_button)
 
{
 
	static Widget *generated_tooltips_widgets = NULL;
 

	
 
	DeleteWindowById(WC_TOOLTIPS, 0);
 

	
 
	if (str == STR_NULL) return;
 

	
 
	for (uint i = 0; i != paramcount; i++) SetDParam(i, params[i]);
 
	char buffer[512];
 
	GetString(buffer, str, lastof(buffer));
 

	
 
	Dimension br = GetStringBoundingBox(buffer);
 
	br.width += 6; br.height += 4; // increase slightly to have some space around the box
 

	
 
	/* Cut tooltip length to 200 pixels max, wrap to new line if longer */
 
	if (br.width > 200) {
 
		br.height += ((br.width - 4) / 176) * 10;
 
		br.width = 200;
 
	}
 

	
 
	/* Correctly position the tooltip position, watch out for window and cursor size
 
	 * Clamp value to below main toolbar and above statusbar. If tooltip would
 
	 * go below window, flip it so it is shown above the cursor */
 
	int y = Clamp(_cursor.pos.y + _cursor.size.y + _cursor.offs.y + 5, 22, _screen.height - 12);
 
	if (y + br.height > _screen.height - 12) y = _cursor.pos.y + _cursor.offs.y - br.height - 5;
 
	int x = Clamp(_cursor.pos.x - (br.width >> 1), 0, _screen.width - br.width);
 

	
 
	new TooltipsWindow(x, y, br.width, br.height, _tooltips_widgets, str, paramcount, params, use_left_mouse_button);
 
	const Widget *wid = InitializeWidgetArrayFromNestedWidgets(_nested_tooltips_widgets, lengthof(_nested_tooltips_widgets),
 
													_tooltips_widgets, &generated_tooltips_widgets);
 
	new TooltipsWindow(x, y, br.width, br.height, wid, str, paramcount, params, use_left_mouse_button);
 
}
 

	
 

	
 
static int DrawStationCoverageText(const AcceptedCargo cargo,
 
	int str_x, int str_y, StationCoverageType sct, bool supplies)
 
{
 
	bool first = true;
 

	
 
	char string[512];
 
	char *b = InlineString(string, supplies ? STR_STATION_BUILD_SUPPLIES_CARGO : STR_STATION_BUILD_ACCEPTS_CARGO);
 

	
 
	for (CargoID i = 0; i < NUM_CARGO; i++) {
src/widgets/dropdown.cpp
Show inline comments
 
@@ -60,24 +60,31 @@ static void DeleteDropDownList(DropDownL
 
		DropDownListItem *item = *it;
 
		delete item;
 
	}
 
	delete list;
 
}
 

	
 
static const Widget _dropdown_menu_widgets[] = {
 
{      WWT_PANEL,   RESIZE_NONE,  COLOUR_END,     0, 0,     0, 0, 0x0, STR_NULL},
 
{  WWT_SCROLLBAR,   RESIZE_NONE,  COLOUR_END,     0, 0,     0, 0, 0x0, STR_TOOLTIP_VSCROLL_BAR_SCROLLS_LIST},
 
{   WIDGETS_END},
 
};
 

	
 
static const NWidgetPart _nested_dropdown_menu_widgets[] = {
 
	NWidget(NWID_LAYERED),
 
		NWidget(WWT_PANEL, COLOUR_END, 0), SetMinimalSize(1, 1), EndContainer(),
 
		NWidget(WWT_SCROLLBAR, COLOUR_END, 1), SetMinimalSize(1, 1),
 
	EndContainer(),
 
};
 

	
 
struct DropdownWindow : Window {
 
	WindowClass parent_wnd_class;
 
	WindowNumber parent_wnd_num;
 
	byte parent_button;
 
	DropDownList *list;
 
	int selected_index;
 
	byte click_delay;
 
	bool drag_mode;
 
	bool instant_close;
 
	int scrolling;
 

	
 
	DropdownWindow(int x, int y, int width, int height, const Widget *widget) : Window(x, y, width, height, WC_DROPDOWN_MENU, widget)
 
@@ -229,24 +236,26 @@ struct DropdownWindow : Window {
 

	
 
				if (!this->GetDropDownItem(item)) return;
 
			}
 

	
 
			this->selected_index = item;
 
			this->SetDirty();
 
		}
 
	}
 
};
 

	
 
void ShowDropDownList(Window *w, DropDownList *list, int selected, int button, uint width, bool auto_width, bool instant_close)
 
{
 
	static Widget *generated_dropdown_menu_widgets = NULL;
 

	
 
	DeleteWindowById(WC_DROPDOWN_MENU, 0);
 

	
 
	w->LowerWidget(button);
 
	w->InvalidateWidget(button);
 

	
 
	/* Our parent's button widget is used to determine where to place the drop
 
	 * down list window. */
 
	const Widget *wi = &w->widget[button];
 

	
 
	/* The preferred position is just below the dropdown calling widget */
 
	int top = w->top + wi->bottom + 1;
 

	
 
@@ -293,30 +302,27 @@ void ShowDropDownList(Window *w, DropDow
 
			int avg_height = list_height / (int)list->size();
 
			int rows = (screen_bottom - 4 - top) / avg_height;
 
			height = rows * avg_height;
 
			scroll = true;
 
			/* Add space for the scroll bar if we automatically determined
 
			 * the width of the list. */
 
			max_item_width += 12;
 
		}
 
	}
 

	
 
	if (auto_width) width = max(width, max_item_width);
 

	
 
	DropdownWindow *dw = new DropdownWindow(
 
		w->left + wi->left,
 
		top,
 
		width,
 
		height + 4,
 
		_dropdown_menu_widgets);
 
	const Widget *wid = InitializeWidgetArrayFromNestedWidgets(_nested_dropdown_menu_widgets, lengthof(_nested_dropdown_menu_widgets),
 
													_dropdown_menu_widgets, &generated_dropdown_menu_widgets);
 
	DropdownWindow *dw = new DropdownWindow(w->left + wi->left, top, width, height + 4, wid);
 

	
 
	dw->widget[0].colour = wi->colour;
 
	dw->widget[0].right = width - 1;
 
	dw->widget[0].bottom = height + 3;
 

	
 
	dw->SetWidgetHiddenState(1, !scroll);
 

	
 
	if (scroll) {
 
		/* We're scrolling, so enable the scroll bar and shrink the list by
 
		 * the scrollbar's width */
 
		dw->widget[1].colour = wi->colour;
 
		dw->widget[1].right  = dw->widget[0].right;
0 comments (0 inline, 0 general)