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
 
@@ -83,23 +83,30 @@ struct GraphLegendWindow : Window {
 
		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
 
@@ -1192,14 +1199,18 @@ struct PerformanceRatingDetailWindow : W
 
		}
 
	}
 
};
 

	
 
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,
 
@@ -1218,17 +1229,23 @@ static NWidgetBase *MakePerformanceDetai
 
		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;
 

	
 
@@ -1249,12 +1266,13 @@ static NWidgetBase *MakeCompanyButtonRow
 
		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);
src/news_gui.cpp
Show inline comments
 
@@ -1078,13 +1078,19 @@ 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++) {
 
@@ -1107,12 +1113,13 @@ static NWidgetBase *MakeNewsSettingLines
 
		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),
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"
 
@@ -333,15 +334,16 @@ static const int INTER_KEY_SPACE = 2; //
 
 * @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);
 
@@ -352,90 +354,92 @@ static void AddKey(NWidgetHorizontal *ho
 
			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),
src/widget.cpp
Show inline comments
 
@@ -2252,15 +2252,17 @@ bool CompareWidgetArrays(const Widget *o
 
 * #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;
 

	
 
@@ -2285,26 +2287,31 @@ static int MakeNWidget(const NWidgetPart
 

	
 
			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;
 
@@ -2388,12 +2395,13 @@ static int MakeNWidget(const NWidgetPart
 
				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++;
 
	}
 

	
 
@@ -2402,26 +2410,28 @@ static int MakeNWidget(const NWidgetPart
 

	
 
/**
 
 * 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;
 

	
 
@@ -2430,13 +2440,13 @@ static int MakeWidgetTree(const NWidgetP
 
		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?
 
@@ -2452,14 +2462,15 @@ static int MakeWidgetTree(const NWidgetP
 
 * @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.
src/widget_type.h
Show inline comments
 
@@ -558,14 +558,18 @@ struct NWidgetPartPaddings {
 
/** 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 {
0 comments (0 inline, 0 general)