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
 
@@ -77,35 +77,42 @@ struct GraphLegendWindow : Window {
 

	
 
		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},
 
@@ -1186,55 +1193,65 @@ struct PerformanceRatingDetailWindow : W
 
		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;
 
@@ -1243,24 +1260,25 @@ static NWidgetBase *MakeCompanyButtonRow
 
		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;
 
}
 

	
src/news_gui.cpp
Show inline comments
 
@@ -1072,25 +1072,31 @@ 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);
 
@@ -1101,24 +1107,25 @@ static NWidgetBase *MakeNewsSettingLines
 
		/* [>] 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),
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 {
 
@@ -327,121 +328,124 @@ static const Widget _osk_widgets[] = {
 

	
 
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),
src/widget.cpp
Show inline comments
 
@@ -2246,27 +2246,29 @@ bool CompareWidgetArrays(const Widget *o
 

	
 
/**
 
 * 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;
 
@@ -2279,38 +2281,43 @@ static int MakeNWidget(const NWidgetPart
 

	
 
			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) {
 
@@ -2382,90 +2389,94 @@ static int MakeNWidget(const NWidgetPart
 
				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.
 
 *
src/widget_type.h
Show inline comments
 
@@ -552,26 +552,30 @@ struct NWidgetPartWidget {
 
/** 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.
0 comments (0 inline, 0 general)