Changeset - r12245:ba404ace85f0
[Not reviewed]
master
0 3 0
alberth - 15 years ago 2009-06-26 21:25:25
alberth@openttd.org
(svn r16670) -Codechange: Containers with equally sized children are useful to have.
3 files changed with 62 insertions and 26 deletions:
0 comments (0 inline, 0 general)
src/intro_gui.cpp
Show inline comments
 
@@ -128,37 +128,37 @@ static const NWidgetPart _nested_select_
 
	NWidget(WWT_CAPTION, COLOUR_BROWN, SGI_CLOSE), SetMinimalSize(336, 14), SetDataTip(STR_INTRO_CAPTION, STR_NULL),
 
	NWidget(WWT_PANEL, COLOUR_BROWN, SGI_CAPTION),
 

	
 
		NWidget(NWID_SPACER), SetMinimalSize(0, 8),
 

	
 
		/* 'generate game' and 'load game' buttons */
 
		NWidget(NWID_HORIZONTAL),
 
		NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
 
			NWidget(NWID_SPACER), SetMinimalSize(10, 0),
 
			NWidget(WWT_PUSHTXTBTN, COLOUR_ORANGE, SGI_GENERATE_GAME), SetMinimalSize(158, 12),
 
								SetDataTip(STR_INTRO_NEW_GAME, STR_INTRO_TOOLTIP_NEW_GAME),
 
			NWidget(WWT_PUSHTXTBTN, COLOUR_ORANGE, SGI_LOAD_GAME), SetMinimalSize(158, 12),
 
								SetDataTip(STR_INTRO_LOAD_GAME, STR_INTRO_TOOLTIP_LOAD_GAME),
 
			NWidget(NWID_SPACER), SetMinimalSize(10, 0),
 
		EndContainer(),
 

	
 
		NWidget(NWID_SPACER), SetMinimalSize(0, 6),
 

	
 
		/* 'play scenario' and 'play heightmap' buttons */
 
		NWidget(NWID_HORIZONTAL),
 
		NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
 
			NWidget(NWID_SPACER), SetMinimalSize(10, 0),
 
			NWidget(WWT_PUSHTXTBTN, COLOUR_ORANGE, SGI_PLAY_SCENARIO), SetMinimalSize(158, 12),
 
								SetDataTip(STR_INTRO_PLAY_SCENARIO, STR_INTRO_TOOLTIP_PLAY_SCENARIO),
 
			NWidget(WWT_PUSHTXTBTN, COLOUR_ORANGE, SGI_PLAY_HEIGHTMAP), SetMinimalSize(158, 12),
 
								SetDataTip(STR_INTRO_PLAY_HEIGHTMAP, STR_INTRO_PLAY_HEIGHTMAP_TOOLTIP),
 
			NWidget(NWID_SPACER), SetMinimalSize(10, 0),
 
		EndContainer(),
 

	
 
		NWidget(NWID_SPACER), SetMinimalSize(0, 6),
 

	
 
		/* 'edit scenario' and 'play multiplayer' buttons */
 
		NWidget(NWID_HORIZONTAL),
 
		NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
 
			NWidget(NWID_SPACER), SetMinimalSize(10, 0),
 
			NWidget(WWT_PUSHTXTBTN, COLOUR_ORANGE, SGI_EDIT_SCENARIO), SetMinimalSize(158, 12),
 
								SetDataTip(STR_INTRO_SCENARIO_EDITOR, STR_INTRO_TOOLTIP_SCENARIO_EDITOR),
 
			NWidget(WWT_PUSHTXTBTN, COLOUR_ORANGE, SGI_PLAY_NETWORK), SetMinimalSize(158, 12),
 
								SetDataTip(STR_INTRO_MULTIPLAYER, STR_INTRO_TOOLTIP_MULTIPLAYER),
 
			NWidget(NWID_SPACER), SetMinimalSize(10, 0),
 
@@ -182,37 +182,37 @@ static const NWidgetPart _nested_select_
 
								SetDataTip(SPR_SELECT_TOYLAND, STR_INTRO_TOOLTIP_TOYLAND_LANDSCAPE),
 
		EndContainer(),
 

	
 
		NWidget(NWID_SPACER), SetMinimalSize(0, 7),
 

	
 
		/* 'game options' and 'difficulty options' buttons */
 
		NWidget(NWID_HORIZONTAL),
 
		NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
 
			NWidget(NWID_SPACER), SetMinimalSize(10, 0),
 
			NWidget(WWT_PUSHTXTBTN, COLOUR_ORANGE, SGI_OPTIONS), SetMinimalSize(158, 12),
 
								SetDataTip(STR_INTRO_GAME_OPTIONS, STR_INTRO_TOOLTIP_GAME_OPTIONS),
 
			NWidget(WWT_PUSHTXTBTN, COLOUR_ORANGE, SGI_DIFFICULTIES), SetMinimalSize(158, 12),
 
								SetDataTip(STR_INTRO_DIFFICULTY, STR_INTRO_TOOLTIP_DIFFICULTY_OPTIONS),
 
			NWidget(NWID_SPACER), SetMinimalSize(10, 0),
 
		EndContainer(),
 

	
 
		NWidget(NWID_SPACER), SetMinimalSize(0, 6),
 

	
 
		/* 'advanced settings' and 'newgrf settings' buttons */
 
		NWidget(NWID_HORIZONTAL),
 
		NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
 
			NWidget(NWID_SPACER), SetMinimalSize(10, 0),
 
			NWidget(WWT_PUSHTXTBTN, COLOUR_ORANGE, SGI_SETTINGS_OPTIONS), SetMinimalSize(158, 12),
 
								SetDataTip(STR_CONFIG_SETTING, STR_CONFIG_SETTING_TIP),
 
			NWidget(WWT_PUSHTXTBTN, COLOUR_ORANGE, SGI_GRF_SETTINGS), SetMinimalSize(158, 12),
 
								SetDataTip(STR_NEWGRF_SETTINGS_BUTTON, STR_NEWGRF_SETTINGS_BUTTON_TIP),
 
			NWidget(NWID_SPACER), SetMinimalSize(10, 0),
 
		EndContainer(),
 

	
 
		NWidget(NWID_SPACER), SetMinimalSize(0, 6),
 

	
 
		/* 'online content' and 'ai settings' buttons */
 
		NWidget(NWID_HORIZONTAL),
 
		NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
 
			NWidget(NWID_SPACER), SetMinimalSize(10, 0),
 
			NWidget(WWT_PUSHTXTBTN, COLOUR_ORANGE, SGI_CONTENT_DOWNLOAD), SetMinimalSize(158, 12),
 
								SetDataTip(STR_CONTENT_INTRO_BUTTON, STR_CONTENT_INTRO_BUTTON_TIP),
 
			NWidget(WWT_PUSHTXTBTN, COLOUR_ORANGE, SGI_AI_SETTINGS), SetMinimalSize(158, 12),
 
								SetDataTip(STR_AI_SETTINGS_BUTTON, STR_AI_SETTINGS_BUTTON_TIP),
 
			NWidget(NWID_SPACER), SetMinimalSize(10, 0),
src/widget.cpp
Show inline comments
 
@@ -1337,14 +1337,15 @@ NWidgetCore *NWidgetStacked::GetWidgetFr
 
		NWidgetCore *nwid = child_wid->GetWidgetFromPos(x, y);
 
		if (nwid != NULL) return nwid;
 
	}
 
	return NULL;
 
}
 

	
 
NWidgetPIPContainer::NWidgetPIPContainer(WidgetType tp) : NWidgetContainer(tp)
 
NWidgetPIPContainer::NWidgetPIPContainer(WidgetType tp, NWidContainerFlags flags) : NWidgetContainer(tp)
 
{
 
	this->flags = flags;
 
}
 

	
 
/**
 
 * Set additional pre/inter/post space for the container.
 
 *
 
 * @param pip_pre   Additional space in front of the first child widget (above
 
@@ -1376,13 +1377,13 @@ NWidgetCore *NWidgetPIPContainer::GetWid
 
		if (nwid != NULL) return nwid;
 
	}
 
	return NULL;
 
}
 

	
 
/** Horizontal container widget. */
 
NWidgetHorizontal::NWidgetHorizontal() : NWidgetPIPContainer(NWID_HORIZONTAL)
 
NWidgetHorizontal::NWidgetHorizontal(NWidContainerFlags flags) : NWidgetPIPContainer(NWID_HORIZONTAL, flags)
 
{
 
}
 

	
 
int NWidgetHorizontal::SetupSmallestSize()
 
{
 
	int biggest_index = -1;
 
@@ -1390,17 +1391,28 @@ int NWidgetHorizontal::SetupSmallestSize
 
	this->smallest_y = 0;   // Biggest child.
 
	this->fill_x = false;   // true if at least one child allows fill_x.
 
	this->fill_y = true;    // true if all childs allow fill_y.
 
	this->resize_x = 0;     // smallest non-zero child widget resize step.
 
	this->resize_y = 1;     // smallest common child resize step
 

	
 
	if (this->head != NULL) this->head->padding_left += this->pip_pre;
 
	/* 1. Forward call, collect biggest nested array index, and longest child length. */
 
	uint longest = 0; // Longest child found.
 
	for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
 
		int idx = child_wid->SetupSmallestSize();
 
		biggest_index = max(biggest_index, idx);
 

	
 
		longest = max(longest, child_wid->smallest_x);
 
	}
 
	/* 2. For containers that must maintain equal width, extend child minimal size. */
 
	if (this->flags & NC_EQUALSIZE) {
 
		for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
 
			if (child_wid->fill_x) child_wid->smallest_x = longest;
 
		}
 
	}
 
	/* 3. Move PIP space to the childs, compute smallest, fill, and resize values of the container. */
 
	if (this->head != NULL) this->head->padding_left += this->pip_pre;
 
	for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
 
		if (child_wid->next != NULL) {
 
			child_wid->padding_right += this->pip_inter;
 
		} else {
 
			child_wid->padding_right += this->pip_post;
 
		}
 

	
 
@@ -1500,13 +1512,13 @@ void NWidgetHorizontal::StoreWidgets(Wid
 

	
 
		child_wid = rtl ? child_wid->prev : child_wid->next;
 
	}
 
}
 

	
 
/** Horizontal left-to-right container widget. */
 
NWidgetHorizontalLTR::NWidgetHorizontalLTR() : NWidgetHorizontal()
 
NWidgetHorizontalLTR::NWidgetHorizontalLTR(NWidContainerFlags flags) : NWidgetHorizontal(flags)
 
{
 
	this->type = NWID_HORIZONTAL_LTR;
 
}
 

	
 
void NWidgetHorizontalLTR::AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool allow_resize_x, bool allow_resize_y, bool rtl)
 
{
 
@@ -1516,13 +1528,13 @@ void NWidgetHorizontalLTR::AssignSizePos
 
void NWidgetHorizontalLTR::StoreWidgets(Widget *widgets, int length, bool left_moving, bool top_moving, bool rtl)
 
{
 
	NWidgetHorizontal::StoreWidgets(widgets, length, left_moving, top_moving, false);
 
}
 

	
 
/** Vertical container widget. */
 
NWidgetVertical::NWidgetVertical() : NWidgetPIPContainer(NWID_VERTICAL)
 
NWidgetVertical::NWidgetVertical(NWidContainerFlags flags) : NWidgetPIPContainer(NWID_VERTICAL, flags)
 
{
 
}
 

	
 
int NWidgetVertical::SetupSmallestSize()
 
{
 
	int biggest_index = -1;
 
@@ -1530,17 +1542,28 @@ int NWidgetVertical::SetupSmallestSize()
 
	this->smallest_y = 0;   // Sum of minimal size of all childs.
 
	this->fill_x = true;    // true if all childs allow fill_x.
 
	this->fill_y = false;   // true if at least one child allows fill_y.
 
	this->resize_x = 1;     // smallest common child resize step
 
	this->resize_y = 0;     // smallest non-zero child widget resize step.
 

	
 
	if (this->head != NULL) this->head->padding_top += this->pip_pre;
 
	/* 1. Forward call, collect biggest nested array index, and longest child length. */
 
	uint highest = 0; // Highest child found.
 
	for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
 
		int idx = child_wid->SetupSmallestSize();
 
		biggest_index = max(biggest_index, idx);
 

	
 
		highest = max(highest, child_wid->smallest_y);
 
	}
 
	/* 2. For containers that must maintain equal width, extend child minimal size. */
 
	if (this->flags & NC_EQUALSIZE) {
 
		for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
 
			if (child_wid->fill_y) child_wid->smallest_y = highest;
 
		}
 
	}
 
	/* 3. Move PIP space to the childs, compute smallest, fill, and resize values of the container. */
 
	if (this->head != NULL) this->head->padding_top += this->pip_pre;
 
	for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
 
		if (child_wid->next != NULL) {
 
			child_wid->padding_bottom += this->pip_inter;
 
		} else {
 
			child_wid->padding_bottom += this->pip_post;
 
		}
 

	
 
@@ -2170,19 +2193,19 @@ static int MakeNWidget(const NWidgetPart
 
				if (*dest != NULL) return num_used;
 
				*dest = new NWidgetSpacer(0, 0);
 
				break;
 

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

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

	
 
			case WWT_PANEL:
 
			case WWT_INSET:
 
			case WWT_FRAME:
 
@@ -2190,13 +2213,13 @@ static int MakeNWidget(const NWidgetPart
 
				*dest = new NWidgetBackground(parts->type, parts->u.widget.colour, parts->u.widget.index);
 
				*fill_dest = true;
 
				break;
 

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

	
 
			case WPT_FUNCTION:
 
				if (*dest != NULL) return num_used;
 
				*dest = parts->u.func_ptr();
src/widget_type.h
Show inline comments
 
@@ -360,56 +360,66 @@ public:
 
	void StoreWidgets(Widget *widgets, int length, bool left_moving, bool top_moving, bool rtl);
 

	
 
	/* virtual */ void Draw(const Window *w);
 
	/* virtual */ NWidgetCore *GetWidgetFromPos(int x, int y);
 
};
 

	
 
/** Nested widget container flags, */
 
enum NWidContainerFlags {
 
	NCB_EQUALSIZE = 0, ///< Containers should keep all their (resizing) children equally large.
 

	
 
	NC_NONE = 0,                       ///< All flags cleared.
 
	NC_EQUALSIZE = 1 << NCB_EQUALSIZE, ///< Value of the #NCB_EQUALSIZE flag.
 
};
 
DECLARE_ENUM_AS_BIT_SET(NWidContainerFlags);
 

	
 
/** Container with pre/inter/post child space. */
 
class NWidgetPIPContainer : public NWidgetContainer {
 
public:
 
	NWidgetPIPContainer(WidgetType tp);
 
	NWidgetPIPContainer(WidgetType tp, NWidContainerFlags flags = NC_NONE);
 

	
 
	void SetPIP(uint8 pip_pre, uint8 pip_inter, uint8 pip_post);
 

	
 
	/* virtual */ void Draw(const Window *w);
 
	/* virtual */ NWidgetCore *GetWidgetFromPos(int x, int y);
 

	
 
protected:
 
	uint8 pip_pre;     ///< Amount of space before first widget.
 
	uint8 pip_inter;   ///< Amount of space between widgets.
 
	uint8 pip_post;    ///< Amount of space after last widget.
 
	NWidContainerFlags flags; ///< Flags of the container.
 
	uint8 pip_pre;            ///< Amount of space before first widget.
 
	uint8 pip_inter;          ///< Amount of space between widgets.
 
	uint8 pip_post;           ///< Amount of space after last widget.
 
};
 

	
 
/** Horizontal container.
 
 * @ingroup NestedWidgets */
 
class NWidgetHorizontal : public NWidgetPIPContainer {
 
public:
 
	NWidgetHorizontal();
 
	NWidgetHorizontal(NWidContainerFlags flags = NC_NONE);
 

	
 
	int SetupSmallestSize();
 
	void AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool allow_resize_x, bool allow_resize_y, bool rtl);
 

	
 
	void StoreWidgets(Widget *widgets, int length, bool left_moving, bool top_moving, bool rtl);
 
};
 

	
 
/** Horizontal container that doesn't change the direction of the widgets for RTL languages.
 
 * @ingroup NestedWidgets */
 
class NWidgetHorizontalLTR : public NWidgetHorizontal {
 
public:
 
	NWidgetHorizontalLTR();
 
	NWidgetHorizontalLTR(NWidContainerFlags flags = NC_NONE);
 

	
 
	void AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool allow_resize_x, bool allow_resize_y, bool rtl);
 

	
 
	void StoreWidgets(Widget *widgets, int length, bool left_moving, bool top_moving, bool rtl);
 
};
 

	
 
/** Vertical container.
 
 * @ingroup NestedWidgets */
 
class NWidgetVertical : public NWidgetPIPContainer {
 
public:
 
	NWidgetVertical();
 
	NWidgetVertical(NWidContainerFlags flags = NC_NONE);
 

	
 
	int SetupSmallestSize();
 
	void AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool allow_resize_x, bool allow_resize_y, bool rtl);
 

	
 
	void StoreWidgets(Widget *widgets, int length, bool left_moving, bool top_moving, bool rtl);
 
};
 
@@ -558,12 +568,13 @@ struct NWidgetPart {
 
		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.
 
@@ -760,20 +771,22 @@ static inline NWidgetPart NWidget(Widget
 

	
 
	return part;
 
}
 

	
 
/**
 
 * Widget part function for starting a new horizontal container, vertical container, or spacer widget.
 
 * @param tp Type of the new nested widget, #NWID_HORIZONTAL(_LTR), #NWID_VERTICAL, #NWID_SPACER, #NWID_SELECTION, or #NWID_LAYERED.
 
 * @param tp         Type of the new nested widget, #NWID_HORIZONTAL(_LTR), #NWID_VERTICAL, #NWID_SPACER, #NWID_SELECTION, or #NWID_LAYERED.
 
 * @param cont_flags Flags for the containers (#NWID_HORIZONTAL(_LTR) and #NWID_VERTICAL).
 
 * @ingroup NestedWidgetParts
 
 */
 
static inline NWidgetPart NWidget(WidgetType tp)
 
static inline NWidgetPart NWidget(WidgetType tp, NWidContainerFlags cont_flags = NC_NONE)
 
{
 
	NWidgetPart part;
 

	
 
	part.type = tp;
 
	part.u.cont_flags = cont_flags;
 

	
 
	return part;
 
}
 

	
 
/**
 
 * Obtain a nested widget (sub)tree from an external source.
0 comments (0 inline, 0 general)