Changeset - r12393:6cdec1f68c3f
[Not reviewed]
master
0 5 0
alberth - 15 years ago 2009-07-15 22:17:08
alberth@openttd.org
(svn r16837) -Codechange: Collect largest used index while constructing nested widget tree.
5 files changed with 83 insertions and 39 deletions:
0 comments (0 inline, 0 general)
src/graph_gui.cpp
Show inline comments
 
@@ -65,59 +65,66 @@ struct GraphLegendWindow : Window {
 
		FOR_ALL_COMPANIES(c) {
 
			DrawCompanyIcon(c->index, 4, 18 + c->index * 12);
 

	
 
			SetDParam(0, c->index);
 
			SetDParam(1, c->index);
 
			DrawString(21, this->width - 4, 17 + c->index * 12, STR_COMPANY_NAME_COMPANY_NUM, HasBit(_legend_excluded_companies, c->index) ? TC_BLACK : TC_WHITE);
 
		}
 
	}
 

	
 
	virtual void OnClick(Point pt, int widget)
 
	{
 
		if (!IsInsideMM(widget, GLW_FIRST_COMPANY, MAX_COMPANIES + GLW_FIRST_COMPANY)) return;
 

	
 
		ToggleBit(_legend_excluded_companies, widget - GLW_FIRST_COMPANY);
 
		this->ToggleWidgetLoweredState(widget);
 
		this->SetDirty();
 
		InvalidateWindow(WC_INCOME_GRAPH, 0);
 
		InvalidateWindow(WC_OPERATING_PROFIT, 0);
 
		InvalidateWindow(WC_DELIVERED_CARGO, 0);
 
		InvalidateWindow(WC_PERFORMANCE_HISTORY, 0);
 
		InvalidateWindow(WC_COMPANY_VALUE, 0);
 
	}
 
};
 

	
 
static NWidgetBase *MakeNWidgetCompanyLines()
 
/**
 
 * Construct a vertical list of buttons, one for each company.
 
 * @param biggest_index Storage for collecting the biggest index used in the returned tree.
 
 * @return Panel with company buttons.
 
 * @postcond \c *biggest_index contains the largest used index in the tree.
 
 */
 
static NWidgetBase *MakeNWidgetCompanyLines(int *biggest_index)
 
{
 
	NWidgetVertical *vert = new NWidgetVertical();
 

	
 
	for (int widnum = GLW_FIRST_COMPANY; widnum <= GLW_LAST_COMPANY; widnum++) {
 
		NWidgetBackground *panel = new NWidgetBackground(WWT_PANEL, COLOUR_GREY, widnum);
 
		panel->SetMinimalSize(246, 12);
 
		panel->SetFill(false, false);
 
		panel->SetDataTip(0x0, STR_GRAPH_KEY_COMPANY_SELECTION);
 
		vert->Add(panel);
 
	}
 
	*biggest_index = GLW_LAST_COMPANY;
 
	return vert;
 
}
 

	
 
static const Widget _graph_legend_widgets[] = {
 
{   WWT_CLOSEBOX,   RESIZE_NONE,  COLOUR_GREY,     0,    10,     0,    13, STR_BLACK_CROSS,       STR_TOOLTIP_CLOSE_WINDOW},           // GLW_CLOSEBOX
 
{    WWT_CAPTION,   RESIZE_NONE,  COLOUR_GREY,    11,   249,     0,    13, STR_GRAPH_KEY_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS}, // GLW_CAPTION
 
{      WWT_PANEL,   RESIZE_NONE,  COLOUR_GREY,     0,   249,    14,   195, 0x0,                   STR_NULL},                           // GLW_BACKGROUND
 
{      WWT_PANEL,   RESIZE_NONE,  COLOUR_GREY,     2,   247,    16,    27, 0x0,                   STR_GRAPH_KEY_COMPANY_SELECTION},    // GLW_FIRST_COMPANY
 
{      WWT_PANEL,   RESIZE_NONE,  COLOUR_GREY,     2,   247,    28,    39, 0x0,                   STR_GRAPH_KEY_COMPANY_SELECTION},
 
{      WWT_PANEL,   RESIZE_NONE,  COLOUR_GREY,     2,   247,    40,    51, 0x0,                   STR_GRAPH_KEY_COMPANY_SELECTION},
 
{      WWT_PANEL,   RESIZE_NONE,  COLOUR_GREY,     2,   247,    52,    63, 0x0,                   STR_GRAPH_KEY_COMPANY_SELECTION},
 
{      WWT_PANEL,   RESIZE_NONE,  COLOUR_GREY,     2,   247,    64,    75, 0x0,                   STR_GRAPH_KEY_COMPANY_SELECTION},
 
{      WWT_PANEL,   RESIZE_NONE,  COLOUR_GREY,     2,   247,    76,    87, 0x0,                   STR_GRAPH_KEY_COMPANY_SELECTION},
 
{      WWT_PANEL,   RESIZE_NONE,  COLOUR_GREY,     2,   247,    88,    99, 0x0,                   STR_GRAPH_KEY_COMPANY_SELECTION},
 
{      WWT_PANEL,   RESIZE_NONE,  COLOUR_GREY,     2,   247,   100,   111, 0x0,                   STR_GRAPH_KEY_COMPANY_SELECTION},
 
{      WWT_PANEL,   RESIZE_NONE,  COLOUR_GREY,     2,   247,   112,   123, 0x0,                   STR_GRAPH_KEY_COMPANY_SELECTION},
 
{      WWT_PANEL,   RESIZE_NONE,  COLOUR_GREY,     2,   247,   124,   135, 0x0,                   STR_GRAPH_KEY_COMPANY_SELECTION},
 
{      WWT_PANEL,   RESIZE_NONE,  COLOUR_GREY,     2,   247,   136,   147, 0x0,                   STR_GRAPH_KEY_COMPANY_SELECTION},
 
{      WWT_PANEL,   RESIZE_NONE,  COLOUR_GREY,     2,   247,   148,   159, 0x0,                   STR_GRAPH_KEY_COMPANY_SELECTION},
 
{      WWT_PANEL,   RESIZE_NONE,  COLOUR_GREY,     2,   247,   160,   171, 0x0,                   STR_GRAPH_KEY_COMPANY_SELECTION},
 
{      WWT_PANEL,   RESIZE_NONE,  COLOUR_GREY,     2,   247,   172,   183, 0x0,                   STR_GRAPH_KEY_COMPANY_SELECTION},
 
{      WWT_PANEL,   RESIZE_NONE,  COLOUR_GREY,     2,   247,   184,   195, 0x0,                   STR_GRAPH_KEY_COMPANY_SELECTION},    // GLW_LAST_COMPANY
 
{   WIDGETS_END},
 
};
 
@@ -1174,105 +1181,116 @@ struct PerformanceRatingDetailWindow : W
 
			/* Is it no on disable? */
 
			if (!this->IsWidgetDisabled(widget)) {
 
				this->RaiseWidget(company + PRW_COMPANY_FIRST);
 
				company = (CompanyID)(widget - PRW_COMPANY_FIRST);
 
				this->LowerWidget(company + PRW_COMPANY_FIRST);
 
				this->SetDirty();
 
			}
 
		}
 
	}
 

	
 
	virtual void OnTick()
 
	{
 
		if (_pause_mode != PM_UNPAUSED) return;
 

	
 
		/* Update the company score every 5 days */
 
		if (--this->timeout == 0) {
 
			this->UpdateCompanyStats();
 
			this->SetDirty();
 
		}
 
	}
 
};
 

	
 
CompanyID PerformanceRatingDetailWindow::company = INVALID_COMPANY;
 

	
 
/** Make a vertical list of panels for outputting score details. */
 
static NWidgetBase *MakePerformanceDetailPanels()
 
/** Make a vertical list of panels for outputting score details.
 
 * @param biggest_index Storage for collecting the biggest index used in the returned tree.
 
 * @return Panel with performance details.
 
 * @postcond \c *biggest_index contains the largest used index in the tree.
 
 */
 
static NWidgetBase *MakePerformanceDetailPanels(int *biggest_index)
 
{
 
	const StringID performance_tips[] = {
 
		STR_PERFORMANCE_DETAIL_VEHICLES_TIP,
 
		STR_PERFORMANCE_DETAIL_STATIONS_TIP,
 
		STR_PERFORMANCE_DETAIL_MIN_PROFIT_TIP,
 
		STR_PERFORMANCE_DETAIL_MIN_INCOME_TIP,
 
		STR_PERFORMANCE_DETAIL_MAX_INCOME_TIP,
 
		STR_PERFORMANCE_DETAIL_DELIVERED_TIP,
 
		STR_PERFORMANCE_DETAIL_CARGO_TIP,
 
		STR_PERFORMANCE_DETAIL_MONEY_TIP,
 
		STR_PERFORMANCE_DETAIL_LOAN_TIP,
 
		STR_PERFORMANCE_DETAIL_TOTAL_TIP,
 
	};
 

	
 
	assert(lengthof(performance_tips) == SCORE_END - SCORE_BEGIN);
 

	
 
	NWidgetVertical *vert = new NWidgetVertical();
 
	for (int widnum = PRW_SCORE_FIRST; widnum <= PRW_SCORE_LAST; widnum++) {
 
		NWidgetBackground *panel = new NWidgetBackground(WWT_PANEL, COLOUR_GREY, widnum);
 
		panel->SetMinimalSize(299, 20);
 
		panel->SetFill(false, false);
 
		panel->SetDataTip(0x0, performance_tips[widnum - PRW_SCORE_FIRST]);
 
		vert->Add(panel);
 
	}
 
	*biggest_index = PRW_SCORE_LAST;
 
	return vert;
 
}
 

	
 
/** Make a number of rows with button-like graphics, for enabling/disabling each company. */
 
static NWidgetBase *MakeCompanyButtonRows()
 
/**
 
 * Make a number of rows with button-like graphics, for enabling/disabling each company.
 
 * @param biggest_index Storage for collecting the biggest index used in the returned tree.
 
 * @return Panel with rows of company buttons.
 
 * @postcond \c *biggest_index contains the largest used index in the tree.
 
 */
 
static NWidgetBase *MakeCompanyButtonRows(int *biggest_index)
 
{
 
	static const int MAX_LENGTH = 8; // Maximal number of company buttons in one row.
 
	NWidgetVertical *vert = NULL; // Storage for all rows.
 
	NWidgetHorizontal *hor = NULL; // Storage for buttons in one row.
 
	int hor_length = 0;
 

	
 
	for (int widnum = PRW_COMPANY_FIRST; widnum <= PRW_COMPANY_LAST; widnum++) {
 
		/* Ensure there is room in 'hor' for another button. */
 
		if (hor_length == MAX_LENGTH) {
 
			if (vert == NULL) vert = new NWidgetVertical();
 
			vert->Add(hor);
 
			hor = NULL;
 
			hor_length = 0;
 
		}
 
		if (hor == NULL) {
 
			hor = new NWidgetHorizontal();
 
			hor_length = 0;
 
		}
 

	
 
		NWidgetBackground *panel = new NWidgetBackground(WWT_PANEL, COLOUR_GREY, widnum);
 
		panel->SetMinimalSize(37, 13);
 
		panel->SetFill(false, false);
 
		panel->SetDataTip(0x0, STR_GRAPH_KEY_COMPANY_SELECTION);
 
		hor->Add(panel);
 
		hor_length++;
 
	}
 
	*biggest_index = PRW_COMPANY_LAST;
 
	if (vert == NULL) return hor; // All buttons fit in a single row.
 

	
 
	if (hor_length > 0 && hor_length < MAX_LENGTH) {
 
		/* Last row is partial, add a spacer at the end to force all buttons to the left. */
 
		NWidgetSpacer *spc = new NWidgetSpacer(0, 0);
 
		spc->SetFill(true, false);
 
		hor->Add(spc);
 
	}
 
	if (hor != NULL) vert->Add(hor);
 
	return vert;
 
}
 

	
 
static const Widget _performance_rating_detail_widgets[] = {
 
{   WWT_CLOSEBOX,   RESIZE_NONE,  COLOUR_GREY,     0,    10,     0,    13, STR_BLACK_CROSS,        STR_TOOLTIP_CLOSE_WINDOW},            // PRW_CLOSEBOX
 
{    WWT_CAPTION,   RESIZE_NONE,  COLOUR_GREY,    11,   298,     0,    13, STR_PERFORMANCE_DETAIL, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS},  // PRW_CAPTION
 
{      WWT_PANEL,   RESIZE_NONE,  COLOUR_GREY,     0,   298,    14,    40, 0x0,                    STR_NULL},                            // PRW_BACKGROUND
 

	
 
{      WWT_PANEL,   RESIZE_NONE,  COLOUR_GREY,     0,   298,    41,    60, 0x0,                    STR_PERFORMANCE_DETAIL_VEHICLES_TIP}, // PRW_SCORE_FIRST
 
{      WWT_PANEL,   RESIZE_NONE,  COLOUR_GREY,     0,   298,    61,    80, 0x0,                    STR_PERFORMANCE_DETAIL_STATIONS_TIP},
 
{      WWT_PANEL,   RESIZE_NONE,  COLOUR_GREY,     0,   298,    81,   100, 0x0,                    STR_PERFORMANCE_DETAIL_MIN_PROFIT_TIP},
 
{      WWT_PANEL,   RESIZE_NONE,  COLOUR_GREY,     0,   298,   101,   120, 0x0,                    STR_PERFORMANCE_DETAIL_MIN_INCOME_TIP},
 
{      WWT_PANEL,   RESIZE_NONE,  COLOUR_GREY,     0,   298,   121,   140, 0x0,                    STR_PERFORMANCE_DETAIL_MAX_INCOME_TIP},
 
{      WWT_PANEL,   RESIZE_NONE,  COLOUR_GREY,     0,   298,   141,   160, 0x0,                    STR_PERFORMANCE_DETAIL_DELIVERED_TIP},
 
{      WWT_PANEL,   RESIZE_NONE,  COLOUR_GREY,     0,   298,   161,   180, 0x0,                    STR_PERFORMANCE_DETAIL_CARGO_TIP},
src/news_gui.cpp
Show inline comments
 
@@ -1060,77 +1060,84 @@ static const Widget _message_options_wid
 
	STR_MESSAGE_SOUND, STR_NULL},
 

	
 
/* List of news-setting lines (4 widgets for each line).
 
 * First widget must be number WIDGET_NEWSOPT_START_OPTION
 
 */
 
NEWS_SETTINGS_LINE(28, NT_ARRIVAL_COMPANY),
 
NEWS_SETTINGS_LINE(28, NT_ARRIVAL_OTHER),
 
NEWS_SETTINGS_LINE(28, NT_ACCIDENT),
 
NEWS_SETTINGS_LINE(28, NT_COMPANY_INFO),
 
NEWS_SETTINGS_LINE(28, NT_INDUSTRY_OPEN),
 
NEWS_SETTINGS_LINE(28, NT_INDUSTRY_CLOSE),
 
NEWS_SETTINGS_LINE(28, NT_ECONOMY),
 
NEWS_SETTINGS_LINE(28, NT_INDUSTRY_COMPANY),
 
NEWS_SETTINGS_LINE(28, NT_INDUSTRY_OTHER),
 
NEWS_SETTINGS_LINE(28, NT_INDUSTRY_NOBODY),
 
NEWS_SETTINGS_LINE(28, NT_ADVICE),
 
NEWS_SETTINGS_LINE(28, NT_NEW_VEHICLES),
 
NEWS_SETTINGS_LINE(28, NT_ACCEPTANCE),
 
NEWS_SETTINGS_LINE(28, NT_SUBSIDIES),
 
NEWS_SETTINGS_LINE(28, NT_GENERAL),
 

	
 
{   WIDGETS_END},
 
};
 

	
 
static NWidgetBase *MakeNewsSettingLines()
 
/**
 
 * Make nested widget tree for the news settings.
 
 * @param biggest_index Storage for collecting the biggest index used in the returned tree.
 
 * @return Panel with rows of news settings.
 
 * @postcond \c *biggest_index contains the largest used index in the tree.
 
 */
 
static NWidgetBase *MakeNewsSettingLines(int *biggest_index)
 
{
 
	const int NEWS_SETTING_HEIGHT = 12; // Height of one line.
 
	NWidgetVertical *vert = new NWidgetVertical;
 

	
 
	int widnum = WIDGET_NEWSOPT_START_OPTION;
 
	for (int i = 0; i < NT_END; i++) {
 
		NWidgetHorizontal *hor = new NWidgetHorizontal;
 
		hor->SetPIP(4, 0, 0);
 
		/* [<] button. */
 
		NWidgetLeaf *leaf = new NWidgetLeaf(WWT_PUSHIMGBTN, COLOUR_YELLOW, widnum++, SPR_ARROW_LEFT, STR_TOOLTIP_HSCROLL_BAR_SCROLLS_LIST);
 
		leaf->SetMinimalSize(9, NEWS_SETTING_HEIGHT);
 
		hor->Add(leaf);
 
		/* Label. */
 
		leaf = new NWidgetLeaf(WWT_PUSHTXTBTN, COLOUR_YELLOW, widnum++, STR_EMPTY, STR_NULL);
 
		leaf->SetMinimalSize(77, NEWS_SETTING_HEIGHT);
 
		hor->Add(leaf);
 
		/* [>] button. */
 
		leaf = new NWidgetLeaf(WWT_PUSHIMGBTN, COLOUR_YELLOW, widnum++, SPR_ARROW_RIGHT, STR_TOOLTIP_HSCROLL_BAR_SCROLLS_LIST);
 
		leaf->SetMinimalSize(9, NEWS_SETTING_HEIGHT);
 
		hor->Add(leaf);
 
		/* Descriptive text. */
 
		leaf = new NWidgetLeaf(WWT_TEXT, COLOUR_YELLOW, widnum++, _news_type_data[i].description, STR_NULL);
 
		leaf->SetMinimalSize(307, NEWS_SETTING_HEIGHT);
 
		leaf->SetPadding(0, 0, 0, 4);
 
		hor->Add(leaf);
 

	
 
		vert->Add(hor);
 
	}
 
	*biggest_index = widnum - 1;
 
	return vert;
 
}
 

	
 
static const NWidgetPart _nested_message_options_widgets[] = {
 
	NWidget(NWID_HORIZONTAL),
 
		NWidget(WWT_CLOSEBOX, COLOUR_BROWN, WIDGET_NEWSOPT_CLOSEBOX),
 
		NWidget(WWT_CAPTION, COLOUR_BROWN, WIDGET_NEWSOPT_CAPTION), SetDataTip(STR_NEWS_MESSAGE_OPTIONS_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
 
	EndContainer(),
 
	NWidget(WWT_PANEL, COLOUR_BROWN, WIDGET_NEWSOPT_BACKGROUND),
 
		NWidget(WWT_LABEL, COLOUR_BROWN, WIDGET_NEWSOPT_LABEL), SetMinimalSize(410, 14), SetDataTip(STR_NEWS_MESSAGE_TYPES, STR_NULL),
 
		NWidgetFunction(MakeNewsSettingLines),
 
		NWidget(NWID_SPACER), SetMinimalSize(0, 6),
 
		NWidget(NWID_VERTICAL),
 
			NWidget(NWID_HORIZONTAL), SetPadding(0, 0, 0, 4),
 
				NWidget(WWT_DROPDOWN, COLOUR_YELLOW, WIDGET_NEWSOPT_DROP_SUMMARY), SetMinimalSize(95, 12), SetDataTip(0x0, STR_NULL),
 
				NWidget(WWT_TEXT, COLOUR_YELLOW, WIDGET_NEWSOPT_LABEL_SUMMARY), SetMinimalSize(307, 12), SetDataTip(STR_MESSAGES_ALL, STR_NULL), SetPadding(0, 0, 0, 4),
 
			EndContainer(),
 
			NWidget(NWID_HORIZONTAL), SetPadding(0, 0, 0, 4),
 
				NWidget(WWT_TEXTBTN_2, COLOUR_YELLOW, WIDGET_NEWSOPT_SOUNDTICKER), SetMinimalSize(95, 12), SetDataTip(STR_STATION_BUILD_COVERAGE_OFF, STR_NULL),
 
				NWidget(WWT_TEXT, COLOUR_YELLOW, WIDGET_NEWSOPT_SOUNDTICKER_LABEL), SetMinimalSize(307, 12), SetDataTip(STR_MESSAGE_SOUND, STR_NULL), SetPadding(0, 0, 0, 4),
 
			EndContainer(),
 
		EndContainer(),
 
		NWidget(NWID_SPACER), SetMinimalSize(0, 7),
 
	EndContainer(),
src/osk_gui.cpp
Show inline comments
 
/* $Id$ */
 

	
 
/** @file osk_gui.cpp The On Screen Keyboard GUI */
 

	
 
#include "stdafx.h"
 
#include "core/math_func.hpp"
 
#include "string_func.h"
 
#include "strings_func.h"
 
#include "debug.h"
 
#include "window_func.h"
 
#include "gfx_func.h"
 
#include "querystring_gui.h"
 

	
 
#include "table/sprites.h"
 
#include "table/strings.h"
 

	
 
/** Widget numbers of the on-screen keyboard (OSK) window. */
 
enum OskWidgets {
 
	OSK_WIDGET_CAPTION,         ///< Title bar.
 
	OSK_WIDGET_TEXT_BACKGROUND, ///< Background around the edit box.
 
	OSK_WIDGET_TEXT,            ///< Edit box.
 
	OSK_WIDGET_KEYS_BACKGROUND, ///< Background of the keys.
 
	OSK_WIDGET_CANCEL,          ///< Cancel key.
 
	OSK_WIDGET_OK,              ///< Ok key.
 
	OSK_WIDGET_BACKSPACE,       ///< Backspace key.
 
	OSK_WIDGET_SPECIAL,         ///< Special key (at keyborads often used for tab key).
 
	OSK_WIDGET_CAPS,            ///< Capslock key.
 
	OSK_WIDGET_SHIFT,           ///< Shift(lock) key.
 
	OSK_WIDGET_SPACE,           ///< Space bar.
 
	OSK_WIDGET_LEFT,            ///< Cursor left key.
 
@@ -315,145 +316,148 @@ static const Widget _osk_widgets[] = {
 
{    WWT_PUSHBTN, RESIZE_NONE,  COLOUR_GREY,    84,    99,   103,   118, 0x0,    STR_NULL},
 
{    WWT_PUSHBTN, RESIZE_NONE,  COLOUR_GREY,   102,   117,   103,   118, 0x0,    STR_NULL},
 
{    WWT_PUSHBTN, RESIZE_NONE,  COLOUR_GREY,   120,   135,   103,   118, 0x0,    STR_NULL},
 
{    WWT_PUSHBTN, RESIZE_NONE,  COLOUR_GREY,   138,   153,   103,   118, 0x0,    STR_NULL},
 
{    WWT_PUSHBTN, RESIZE_NONE,  COLOUR_GREY,   156,   171,   103,   118, 0x0,    STR_NULL},
 
{    WWT_PUSHBTN, RESIZE_NONE,  COLOUR_GREY,   174,   189,   103,   118, 0x0,    STR_NULL},
 
{    WWT_PUSHBTN, RESIZE_NONE,  COLOUR_GREY,   192,   207,   103,   118, 0x0,    STR_NULL},
 
{    WWT_PUSHBTN, RESIZE_NONE,  COLOUR_GREY,   210,   225,   103,   118, 0x0,    STR_NULL},
 
{    WWT_PUSHBTN, RESIZE_NONE,  COLOUR_GREY,   228,   243,   103,   118, 0x0,    STR_NULL},            // OSK_WIDGET_ZXCVB_LAST
 

	
 
{   WIDGETS_END},
 
};
 

	
 
static const int HALF_KEY_WIDTH = 7;  // Width of 1/2 key in pixels.
 
static const int INTER_KEY_SPACE = 2; // Number of pixels between two keys.
 

	
 
/**
 
 * Add a key widget to a row of the keyboard.
 
 * @param hor     Row container to add key widget to.
 
 * @param height  Height of the key (all keys in a row should have equal height).
 
 * @param numhalf Number of 1/2 key widths that this key has.
 
 * @param widtype Widget type of the key. Must be either \c NWID_SPACER for an invisible key, or a \c WWT_* widget.
 
 * @param widnum  Widget number of the key.
 
 * @param widdata Data value of the key widget.
 
 * @param biggest_index Collected biggest widget index so far.
 
 * @note Key width is measured in 1/2 keys to allow for 1/2 key shifting between rows.
 
 */
 
static void AddKey(NWidgetHorizontal *hor, int height, int num_half, WidgetType widtype, int widnum, uint16 widdata)
 
static void AddKey(NWidgetHorizontal *hor, int height, int num_half, WidgetType widtype, int widnum, uint16 widdata, int *biggest_index)
 
{
 
	int key_width = HALF_KEY_WIDTH + (INTER_KEY_SPACE + HALF_KEY_WIDTH) * (num_half - 1);
 

	
 
	if (widtype == NWID_SPACER) {
 
		if (!hor->IsEmpty()) key_width += INTER_KEY_SPACE;
 
		NWidgetSpacer *spc = new NWidgetSpacer(key_width, height);
 
		hor->Add(spc);
 
	} else {
 
		if (!hor->IsEmpty()) {
 
			NWidgetSpacer *spc = new NWidgetSpacer(INTER_KEY_SPACE, height);
 
			hor->Add(spc);
 
		}
 
		NWidgetLeaf *leaf = new NWidgetLeaf(widtype, COLOUR_GREY, widnum, widdata, STR_NULL);
 
		leaf->SetMinimalSize(key_width, height);
 
		hor->Add(leaf);
 
	}
 

	
 
	*biggest_index = max(*biggest_index, widnum);
 
}
 

	
 
/** Construct the top row keys (cancel, ok, backspace). */
 
static NWidgetBase *MakeTopKeys()
 
static NWidgetBase *MakeTopKeys(int *biggest_index)
 
{
 
	NWidgetHorizontal *hor = new NWidgetHorizontal;
 
	int key_height = 12;
 

	
 
	AddKey(hor, key_height, 6 * 2, WWT_TEXTBTN,    OSK_WIDGET_CANCEL,    STR_QUERY_CANCEL);
 
	AddKey(hor, key_height, 6 * 2, WWT_TEXTBTN,    OSK_WIDGET_OK,        STR_QUERY_OK);
 
	AddKey(hor, key_height, 2 * 2, WWT_PUSHIMGBTN, OSK_WIDGET_BACKSPACE, SPR_OSK_BACKSPACE);
 
	AddKey(hor, key_height, 6 * 2, WWT_TEXTBTN,    OSK_WIDGET_CANCEL,    STR_QUERY_CANCEL,  biggest_index);
 
	AddKey(hor, key_height, 6 * 2, WWT_TEXTBTN,    OSK_WIDGET_OK,        STR_QUERY_OK,      biggest_index);
 
	AddKey(hor, key_height, 2 * 2, WWT_PUSHIMGBTN, OSK_WIDGET_BACKSPACE, SPR_OSK_BACKSPACE, biggest_index);
 
	return hor;
 
}
 

	
 
/** Construct the row containing the digit keys. */
 
static NWidgetBase *MakeNumberKeys()
 
static NWidgetBase *MakeNumberKeys(int *biggest_index)
 
{
 
	NWidgetHorizontal *hor = new NWidgetHorizontal;
 
	int key_height = 16;
 

	
 
	for (int widnum = OSK_WIDGET_NUMBERS_FIRST; widnum <= OSK_WIDGET_NUMBERS_LAST; widnum++) {
 
		AddKey(hor, key_height, 2, WWT_PUSHBTN, widnum, 0x0);
 
		AddKey(hor, key_height, 2, WWT_PUSHBTN, widnum, 0x0, biggest_index);
 
	}
 
	return hor;
 
}
 

	
 
/** Construct the qwerty row keys. */
 
static NWidgetBase *MakeQwertyKeys()
 
static NWidgetBase *MakeQwertyKeys(int *biggest_index)
 
{
 
	NWidgetHorizontal *hor = new NWidgetHorizontal;
 
	int key_height = 16;
 

	
 
	AddKey(hor, key_height, 3, WWT_PUSHIMGBTN, OSK_WIDGET_SPECIAL, SPR_OSK_SPECIAL);
 
	AddKey(hor, key_height, 3, WWT_PUSHIMGBTN, OSK_WIDGET_SPECIAL, SPR_OSK_SPECIAL, biggest_index);
 
	for (int widnum = OSK_WIDGET_QWERTY_FIRST; widnum <= OSK_WIDGET_QWERTY_LAST; widnum++) {
 
		AddKey(hor, key_height, 2, WWT_PUSHBTN, widnum, 0x0);
 
		AddKey(hor, key_height, 2, WWT_PUSHBTN, widnum, 0x0, biggest_index);
 
	}
 
	AddKey(hor, key_height, 1, NWID_SPACER, 0, 0);
 
	AddKey(hor, key_height, 1, NWID_SPACER, 0, 0, biggest_index);
 
	return hor;
 
}
 

	
 
/** Construct the asdfg row keys. */
 
static NWidgetBase *MakeAsdfgKeys()
 
static NWidgetBase *MakeAsdfgKeys(int *biggest_index)
 
{
 
	NWidgetHorizontal *hor = new NWidgetHorizontal;
 
	int key_height = 16;
 

	
 
	AddKey(hor, key_height, 4, WWT_IMGBTN, OSK_WIDGET_CAPS, SPR_OSK_CAPS);
 
	AddKey(hor, key_height, 4, WWT_IMGBTN, OSK_WIDGET_CAPS, SPR_OSK_CAPS, biggest_index);
 
	for (int widnum = OSK_WIDGET_ASDFG_FIRST; widnum <= OSK_WIDGET_ASDFG_LAST; widnum++) {
 
		AddKey(hor, key_height, 2, WWT_PUSHBTN, widnum, 0x0);
 
		AddKey(hor, key_height, 2, WWT_PUSHBTN, widnum, 0x0, biggest_index);
 
	}
 
	return hor;
 
}
 

	
 
/** Construct the zxcvb row keys. */
 
static NWidgetBase *MakeZxcvbKeys()
 
static NWidgetBase *MakeZxcvbKeys(int *biggest_index)
 
{
 
	NWidgetHorizontal *hor = new NWidgetHorizontal;
 
	int key_height = 16;
 

	
 
	AddKey(hor, key_height, 3, WWT_IMGBTN, OSK_WIDGET_SHIFT, SPR_OSK_SHIFT);
 
	AddKey(hor, key_height, 3, WWT_IMGBTN, OSK_WIDGET_SHIFT, SPR_OSK_SHIFT, biggest_index);
 
	for (int widnum = OSK_WIDGET_ZXCVB_FIRST; widnum <= OSK_WIDGET_ZXCVB_LAST; widnum++) {
 
		AddKey(hor, key_height, 2, WWT_PUSHBTN, widnum, 0x0);
 
		AddKey(hor, key_height, 2, WWT_PUSHBTN, widnum, 0x0, biggest_index);
 
	}
 
	AddKey(hor, key_height, 1, NWID_SPACER, 0, 0);
 
	AddKey(hor, key_height, 1, NWID_SPACER, 0, 0, biggest_index);
 
	return hor;
 
}
 

	
 
/** Construct the spacebar row keys. */
 
static NWidgetBase *MakeSpacebarKeys()
 
static NWidgetBase *MakeSpacebarKeys(int *biggest_index)
 
{
 
	NWidgetHorizontal *hor = new NWidgetHorizontal;
 
	int key_height = 16;
 

	
 
	AddKey(hor, key_height,  8, NWID_SPACER, 0, 0);
 
	AddKey(hor, key_height, 13, WWT_PUSHTXTBTN, OSK_WIDGET_SPACE, STR_EMPTY);
 
	AddKey(hor, key_height,  3, NWID_SPACER, 0, 0);
 
	AddKey(hor, key_height,  2, WWT_PUSHIMGBTN, OSK_WIDGET_LEFT,  SPR_OSK_LEFT);
 
	AddKey(hor, key_height,  2, WWT_PUSHIMGBTN, OSK_WIDGET_RIGHT, SPR_OSK_RIGHT);
 
	AddKey(hor, key_height,  8, NWID_SPACER, 0, 0, biggest_index);
 
	AddKey(hor, key_height, 13, WWT_PUSHTXTBTN, OSK_WIDGET_SPACE, STR_EMPTY, biggest_index);
 
	AddKey(hor, key_height,  3, NWID_SPACER, 0, 0, biggest_index);
 
	AddKey(hor, key_height,  2, WWT_PUSHIMGBTN, OSK_WIDGET_LEFT,  SPR_OSK_LEFT, biggest_index);
 
	AddKey(hor, key_height,  2, WWT_PUSHIMGBTN, OSK_WIDGET_RIGHT, SPR_OSK_RIGHT, biggest_index);
 
	return hor;
 
}
 

	
 

	
 
static const NWidgetPart _nested_osk_widgets[] = {
 
	NWidget(WWT_CAPTION, COLOUR_GREY, OSK_WIDGET_CAPTION), SetDataTip(STR_QUERY_CAPTION, STR_NULL),
 
	NWidget(WWT_PANEL, COLOUR_GREY, OSK_WIDGET_TEXT_BACKGROUND),
 
		NWidget(WWT_EDITBOX, COLOUR_GREY, OSK_WIDGET_TEXT), SetMinimalSize(252, 12), SetPadding(2, 2, 2, 2),
 
	EndContainer(),
 
	NWidget(WWT_PANEL, COLOUR_GREY, OSK_WIDGET_KEYS_BACKGROUND), SetPIP(5, 2, 3),
 
		NWidgetFunction(MakeTopKeys), SetPadding(0, 3, 0, 3),
 
		NWidgetFunction(MakeNumberKeys), SetPadding(0, 3, 0, 3),
 
		NWidgetFunction(MakeQwertyKeys), SetPadding(0, 3, 0, 3),
 
		NWidgetFunction(MakeAsdfgKeys), SetPadding(0, 3, 0, 3),
 
		NWidgetFunction(MakeZxcvbKeys), SetPadding(0, 3, 0, 3),
 
		NWidgetFunction(MakeSpacebarKeys), SetPadding(0, 3, 0, 3),
 
	EndContainer(),
 
};
 

	
 
static const WindowDesc _osk_desc(
 
	WDP_CENTER, WDP_CENTER, 256, 140, 256, 140,
 
	WC_OSK, WC_NONE,
 
	WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS,
 
	_osk_widgets, _nested_osk_widgets, lengthof(_nested_osk_widgets)
src/widget.cpp
Show inline comments
 
@@ -2234,95 +2234,102 @@ bool CompareWidgetArrays(const Widget *o
 
		CHECK(tooltips, "%u")
 

	
 
		if (ow->type == WWT_LAST || gw->type == WWT_LAST) break;
 
	}
 

	
 
	return same;
 

	
 
#undef CHECK
 
#undef CHECK_COORD
 
}
 

	
 
/* == Conversion code from NWidgetPart array to NWidgetBase* tree == */
 

	
 
/**
 
 * Construct a single nested widget in \a *dest from its parts.
 
 *
 
 * Construct a NWidgetBase object from a #NWidget function, and apply all
 
 * settings that follow it, until encountering a #EndContainer, another
 
 * #NWidget, or the end of the parts array.
 
 *
 
 * @param parts Array with parts of the nested widget.
 
 * @param count Length of the \a parts array.
 
 * @param dest  Address of pointer to use for returning the composed widget.
 
 * @param fill_dest Fill the composed widget with child widgets.
 
 * @param biggest_index Pointer to biggest nested widget index in the tree encountered so far.
 
 * @return Number of widget part elements used to compose the widget.
 
 * @precond \c biggest_index != NULL.
 
 */
 
static int MakeNWidget(const NWidgetPart *parts, int count, NWidgetBase **dest, bool *fill_dest)
 
static int MakeNWidget(const NWidgetPart *parts, int count, NWidgetBase **dest, bool *fill_dest, int *biggest_index)
 
{
 
	int num_used = 0;
 

	
 
	*dest = NULL;
 
	*fill_dest = false;
 

	
 
	while (count > num_used) {
 
		switch (parts->type) {
 
			case NWID_SPACER:
 
				if (*dest != NULL) return num_used;
 
				*dest = new NWidgetSpacer(0, 0);
 
				break;
 

	
 
			case NWID_HORIZONTAL:
 
				if (*dest != NULL) return num_used;
 
				*dest = new NWidgetHorizontal(parts->u.cont_flags);
 
				*fill_dest = true;
 
				break;
 

	
 
			case NWID_HORIZONTAL_LTR:
 
				if (*dest != NULL) return num_used;
 
				*dest = new NWidgetHorizontalLTR(parts->u.cont_flags);
 
				*fill_dest = true;
 
				break;
 

	
 
			case WWT_PANEL:
 
			case WWT_INSET:
 
			case WWT_FRAME:
 
				if (*dest != NULL) return num_used;
 
				*dest = new NWidgetBackground(parts->type, parts->u.widget.colour, parts->u.widget.index);
 
				*biggest_index = max(*biggest_index, (int)parts->u.widget.index);
 
				*fill_dest = true;
 
				break;
 

	
 
			case NWID_VERTICAL:
 
				if (*dest != NULL) return num_used;
 
				*dest = new NWidgetVertical(parts->u.cont_flags);
 
				*fill_dest = true;
 
				break;
 

	
 
			case WPT_FUNCTION:
 
			case WPT_FUNCTION: {
 
				if (*dest != NULL) return num_used;
 
				*dest = parts->u.func_ptr();
 
				/* Ensure proper functioning even when the called code simply writes its largest index. */
 
				int biggest = -1;
 
				*dest = parts->u.func_ptr(&biggest);
 
				*biggest_index = max(*biggest_index, biggest);
 
				*fill_dest = false;
 
				break;
 
			}
 

	
 
			case NWID_SELECTION:
 
			case NWID_LAYERED:
 
				if (*dest != NULL) return num_used;
 
				*dest = new NWidgetStacked(parts->type);
 
				*fill_dest = true;
 
				break;
 

	
 

	
 
			case WPT_RESIZE: {
 
				NWidgetResizeBase *nwrb = dynamic_cast<NWidgetResizeBase *>(*dest);
 
				if (nwrb != NULL) {
 
					assert(parts->u.xy.x >= 0 && parts->u.xy.y >= 0);
 
					nwrb->SetResize(parts->u.xy.x, parts->u.xy.y);
 
				}
 
				break;
 
			}
 

	
 
			case WPT_RESIZE_PTR: {
 
				NWidgetResizeBase *nwrb = dynamic_cast<NWidgetResizeBase *>(*dest);
 
				if (nwrb != NULL) {
 
					assert(parts->u.xy_ptr->x >= 0 && parts->u.xy_ptr->y >= 0);
 
					nwrb->SetResize(parts->u.xy_ptr->x, parts->u.xy_ptr->y);
 
				}
 
@@ -2370,114 +2377,118 @@ static int MakeNWidget(const NWidgetPart
 
				}
 
				break;
 
			}
 

	
 
			case WPT_PADDING:
 
				if (*dest != NULL) (*dest)->SetPadding(parts->u.padding.top, parts->u.padding.right, parts->u.padding.bottom, parts->u.padding.left);
 
				break;
 

	
 
			case WPT_PIPSPACE: {
 
				NWidgetPIPContainer *nwc = dynamic_cast<NWidgetPIPContainer *>(*dest);
 
				if (nwc != NULL) nwc->SetPIP(parts->u.pip.pre,  parts->u.pip.inter, parts->u.pip.post);
 

	
 
				NWidgetBackground *nwb = dynamic_cast<NWidgetBackground *>(*dest);
 
				if (nwb != NULL) nwb->SetPIP(parts->u.pip.pre,  parts->u.pip.inter, parts->u.pip.post);
 
				break;
 
			}
 

	
 
			case WPT_ENDCONTAINER:
 
				return num_used;
 

	
 
			default:
 
				if (*dest != NULL) return num_used;
 
				assert((parts->type & WWT_MASK) < NWID_HORIZONTAL);
 
				*dest = new NWidgetLeaf(parts->type, parts->u.widget.colour, parts->u.widget.index, 0x0, STR_NULL);
 
				*biggest_index = max(*biggest_index, (int)parts->u.widget.index);
 
				break;
 
		}
 
		num_used++;
 
		parts++;
 
	}
 

	
 
	return num_used;
 
}
 

	
 
/**
 
 * Build a nested widget tree by recursively filling containers with nested widgets read from their parts.
 
 * @param parts  Array with parts of the nested widgets.
 
 * @param count  Length of the \a parts array.
 
 * @param parent Container to use for storing the child widgets.
 
 * @param biggest_index Pointer to biggest nested widget index in the tree.
 
 * @return Number of widget part elements used to fill the container.
 
 * @postcond \c *biggest_index contains the largest widget index of the tree and \c -1 if no index is used.
 
 */
 
static int MakeWidgetTree(const NWidgetPart *parts, int count, NWidgetBase *parent)
 
static int MakeWidgetTree(const NWidgetPart *parts, int count, NWidgetBase *parent, int *biggest_index)
 
{
 
	/* Given parent must be either a #NWidgetContainer or a #NWidgetBackground object. */
 
	NWidgetContainer *nwid_cont = dynamic_cast<NWidgetContainer *>(parent);
 
	NWidgetBackground *nwid_parent = dynamic_cast<NWidgetBackground *>(parent);
 
	assert((nwid_cont != NULL && nwid_parent == NULL) || (nwid_cont == NULL && nwid_parent != NULL));
 

	
 
	int total_used = 0;
 
	while (true) {
 
		NWidgetBase *sub_widget = NULL;
 
		bool fill_sub = false;
 
		int num_used = MakeNWidget(parts, count - total_used, &sub_widget, &fill_sub);
 
		int num_used = MakeNWidget(parts, count - total_used, &sub_widget, &fill_sub, biggest_index);
 
		parts += num_used;
 
		total_used += num_used;
 

	
 
		/* Break out of loop when end reached */
 
		if (sub_widget == NULL) break;
 

	
 
		/* Add sub_widget to parent container. */
 
		if (nwid_cont) nwid_cont->Add(sub_widget);
 
		if (nwid_parent) nwid_parent->Add(sub_widget);
 

	
 
		/* If sub-widget is a container, recursively fill that container. */
 
		WidgetType tp = sub_widget->type;
 
		if (fill_sub && (tp == NWID_HORIZONTAL || tp == NWID_HORIZONTAL_LTR || tp == NWID_VERTICAL
 
							|| tp == WWT_PANEL || tp == WWT_FRAME || tp == WWT_INSET || tp == NWID_SELECTION || tp == NWID_LAYERED)) {
 
			int num_used = MakeWidgetTree(parts, count - total_used, sub_widget);
 
			int num_used = MakeWidgetTree(parts, count - total_used, sub_widget, biggest_index);
 
			parts += num_used;
 
			total_used += num_used;
 
		}
 
	}
 

	
 
	if (count == total_used) return total_used; // Reached the end of the array of parts?
 

	
 
	assert(total_used < count);
 
	assert(parts->type == WPT_ENDCONTAINER);
 
	return total_used + 1; // *parts is also 'used'
 
}
 

	
 
/**
 
 * Construct a nested widget tree from an array of parts.
 
 * @param parts Array with parts of the widgets.
 
 * @param count Length of the \a parts array.
 
 * @return Root of the nested widget tree, a vertical container containing the entire GUI.
 
 * @ingroup NestedWidgetParts
 
 */
 
NWidgetContainer *MakeNWidgets(const NWidgetPart *parts, int count)
 
{
 
	int biggest_index = -1;
 
	NWidgetContainer *cont = new NWidgetVertical();
 
	MakeWidgetTree(parts, count, cont);
 
	MakeWidgetTree(parts, count, cont, &biggest_index);
 
	return cont;
 
}
 

	
 
/**
 
 * Construct a #Widget array from a nested widget parts array, taking care of all the steps and checks.
 
 * Also cache the result and use the cache if possible.
 
 * @param[in] parts        Array with parts of the widgets.
 
 * @param     parts_length Length of the \a parts array.
 
 * @param[in] orig_wid     Pointer to original widget array.
 
 * @param     wid_cache    Pointer to the cache for storing the generated widget array (use \c NULL to prevent caching).
 
 * @return Cached value if available, otherwise the generated widget array. If \a wid_cache is \c NULL, the caller should free the returned array.
 
 *
 
 * @pre Before the first call, \c *wid_cache should be \c NULL.
 
 * @post The widget array stored in the \c *wid_cache should be free-ed by the caller.
 
 */
 
const Widget *InitializeWidgetArrayFromNestedWidgets(const NWidgetPart *parts, int parts_length, const Widget *orig_wid, Widget **wid_cache)
 
{
 
	const bool rtl = false; // Direction of the language is left-to-right
 

	
 
	if (wid_cache != NULL && *wid_cache != NULL) return *wid_cache;
 

	
 
	assert(parts != NULL && parts_length > 0);
 
	NWidgetContainer *nwid = MakeNWidgets(parts, parts_length);
 
	Widget *gen_wid = InitializeNWidgets(nwid, rtl);
src/widget_type.h
Show inline comments
 
@@ -540,50 +540,54 @@ bool CompareWidgetArrays(const Widget *o
 
struct NWidgetPartDataTip {
 
	uint16 data;      ///< Data value of the widget.
 
	StringID tooltip; ///< Tooltip of the widget.
 
};
 

	
 
/** Widget part for storing basic widget information.
 
 * @ingroup NestedWidgetParts */
 
struct NWidgetPartWidget {
 
	Colours colour; ///< Widget colour.
 
	int16 index;    ///< Widget index in the widget array.
 
};
 

	
 
/** Widget part for storing padding.
 
 * @ingroup NestedWidgetParts */
 
struct NWidgetPartPaddings {
 
	uint8 top, right, bottom, left; ///< Paddings for all directions.
 
};
 

	
 
/** Widget part for storing pre/inter/post spaces.
 
 * @ingroup NestedWidgetParts */
 
struct NWidgetPartPIP {
 
	uint8 pre, inter, post; ///< Amount of space before/between/after child widgets.
 
};
 

	
 
/** Pointer to function returning a nested widget. */
 
typedef NWidgetBase *NWidgetFunctionType();
 
/** Pointer to function returning a nested widget.
 
 * @param biggest_index Pointer to storage for collecting the biggest index used in the nested widget.
 
 * @return Nested widget (tree).
 
 * @postcond \c *biggest_index must contain the value of the biggest index in the returned tree.
 
 */
 
typedef NWidgetBase *NWidgetFunctionType(int *biggest_index);
 

	
 
/** Partial widget specification to allow NWidgets to be written nested.
 
 * @ingroup NestedWidgetParts */
 
struct NWidgetPart {
 
	WidgetType type;                         ///< Type of the part. @see NWidgetPartType.
 
	union {
 
		Point xy;                        ///< Part with an x/y size.
 
		Point *xy_ptr;                   ///< Part with a pointer to an x/y size.
 
		NWidgetPartDataTip data_tip;     ///< Part with a data/tooltip.
 
		NWidgetPartDataTip *datatip_ptr; ///< Part with a pointer to data/tooltip.
 
		NWidgetPartWidget widget;        ///< Part with a start of a widget.
 
		NWidgetPartPaddings padding;     ///< Part with paddings.
 
		NWidgetPartPIP pip;              ///< Part with pre/inter/post spaces.
 
		NWidgetFunctionType *func_ptr;   ///< Part with a function call.
 
		NWidContainerFlags cont_flags;   ///< Part with container flags.
 
	} u;
 
};
 

	
 
/**
 
 * Widget part function for setting the resize step.
 
 * @param dx Horizontal resize step. 0 means no horizontal resizing.
 
 * @param dy Vertical resize step. 0 means no horizontal resizing.
 
 * @ingroup NestedWidgetParts
 
 */
0 comments (0 inline, 0 general)