# HG changeset patch # User alberth # Date 2009-07-15 22:17:08 # Node ID 6cdec1f68c3f93b74d7ae82bed3a7eee2fe37ab4 # Parent a1db9214fc64c297ec2e2a98822b322bd96fc071 (svn r16837) -Codechange: Collect largest used index while constructing nested widget tree. diff --git a/src/graph_gui.cpp b/src/graph_gui.cpp --- a/src/graph_gui.cpp +++ b/src/graph_gui.cpp @@ -86,7 +86,13 @@ struct GraphLegendWindow : Window { } }; -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(); @@ -97,6 +103,7 @@ static NWidgetBase *MakeNWidgetCompanyLi panel->SetDataTip(0x0, STR_GRAPH_KEY_COMPANY_SELECTION); vert->Add(panel); } + *biggest_index = GLW_LAST_COMPANY; return vert; } @@ -1195,8 +1202,12 @@ 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, @@ -1221,11 +1232,17 @@ static NWidgetBase *MakePerformanceDetai 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. @@ -1252,6 +1269,7 @@ static NWidgetBase *MakeCompanyButtonRow 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) { diff --git a/src/news_gui.cpp b/src/news_gui.cpp --- a/src/news_gui.cpp +++ b/src/news_gui.cpp @@ -1081,7 +1081,13 @@ 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; @@ -1110,6 +1116,7 @@ static NWidgetBase *MakeNewsSettingLines vert->Add(hor); } + *biggest_index = widnum - 1; return vert; } diff --git a/src/osk_gui.cpp b/src/osk_gui.cpp --- a/src/osk_gui.cpp +++ b/src/osk_gui.cpp @@ -3,6 +3,7 @@ /** @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" @@ -336,9 +337,10 @@ static const int INTER_KEY_SPACE = 2; // * @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); @@ -355,84 +357,86 @@ static void AddKey(NWidgetHorizontal *ho 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; } diff --git a/src/widget.cpp b/src/widget.cpp --- a/src/widget.cpp +++ b/src/widget.cpp @@ -2255,9 +2255,11 @@ bool CompareWidgetArrays(const Widget *o * @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; @@ -2288,6 +2290,7 @@ static int MakeNWidget(const NWidgetPart 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; @@ -2297,11 +2300,15 @@ static int MakeNWidget(const NWidgetPart *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: @@ -2391,6 +2398,7 @@ static int MakeNWidget(const NWidgetPart 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++; @@ -2405,9 +2413,11 @@ static int MakeNWidget(const NWidgetPart * @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(parent); @@ -2418,7 +2428,7 @@ static int MakeWidgetTree(const NWidgetP 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; @@ -2433,7 +2443,7 @@ static int MakeWidgetTree(const NWidgetP 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; } @@ -2455,8 +2465,9 @@ static int MakeWidgetTree(const NWidgetP */ 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; } diff --git a/src/widget_type.h b/src/widget_type.h --- a/src/widget_type.h +++ b/src/widget_type.h @@ -561,8 +561,12 @@ 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 */