Factor out OnClick dispatching in Window::HandleEditBoxKey
[openttd/fttd.git] / src / newgrf_gui.cpp
blobdb203a9bb1c2ec150396e00c9f2d52a8b5feb471
1 /* $Id$ */
3 /*
4 * This file is part of OpenTTD.
5 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
6 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 */
10 /** @file newgrf_gui.cpp GUI to change NewGRF settings. */
12 #include "stdafx.h"
13 #include "error.h"
14 #include "string.h"
15 #include "settings_gui.h"
16 #include "newgrf.h"
17 #include "strings_func.h"
18 #include "window_func.h"
19 #include "gamelog.h"
20 #include "settings_type.h"
21 #include "settings_func.h"
22 #include "widgets/dropdown_type.h"
23 #include "widgets/dropdown_func.h"
24 #include "network/network.h"
25 #include "network/network_content.h"
26 #include "sortlist_type.h"
27 #include "stringfilter_type.h"
28 #include "querystring_gui.h"
29 #include "core/geometry_func.hpp"
30 #include "newgrf_text.h"
31 #include "textfile.h"
32 #include "tilehighlight_func.h"
33 #include "fios.h"
35 #include "widgets/newgrf_widget.h"
36 #include "widgets/misc_widget.h"
38 #include "table/sprites.h"
40 #include <map>
42 /**
43 * Show the first NewGRF error we can find.
45 void ShowNewGRFError()
47 /* Do not show errors when entering the main screen */
48 if (_game_mode == GM_MENU) return;
50 for (const GRFConfig *c = _grfconfig; c != NULL; c = c->next) {
51 /* We only want to show fatal errors */
52 if (c->error == NULL || c->error->severity != STR_NEWGRF_ERROR_MSG_FATAL) continue;
54 SetDParam (0, c->error->custom_message == NULL ? c->error->message : STR_JUST_RAW_STRING);
55 SetDParamStr(1, c->error->custom_message);
56 SetDParamStr(2, c->filename);
57 SetDParamStr(3, c->error->data);
58 for (uint i = 0; i < lengthof(c->error->param_value); i++) {
59 SetDParam(4 + i, c->error->param_value[i]);
61 ShowErrorMessage(STR_NEWGRF_ERROR_FATAL_POPUP, INVALID_STRING_ID, WL_CRITICAL);
62 break;
66 static void ShowNewGRFInfo (const GRFConfig *c, BlitArea *dpi,
67 uint x, uint y, uint right, uint bottom, bool show_params)
69 if (c->error != NULL) {
70 char message[512];
71 SetDParamStr(0, c->error->custom_message); // is skipped by built-in messages
72 SetDParamStr(1, c->filename);
73 SetDParamStr(2, c->error->data);
74 for (uint i = 0; i < lengthof(c->error->param_value); i++) {
75 SetDParam(3 + i, c->error->param_value[i]);
77 GetString (message, c->error->custom_message == NULL ? c->error->message : STR_JUST_RAW_STRING);
79 SetDParamStr(0, message);
80 y = DrawStringMultiLine (dpi, x, right, y, bottom, c->error->severity);
83 /* Draw filename or not if it is not known (GRF sent over internet) */
84 if (c->filename != NULL) {
85 SetDParamStr(0, c->filename);
86 y = DrawStringMultiLine (dpi, x, right, y, bottom, STR_NEWGRF_SETTINGS_FILENAME);
89 /* Prepare and draw GRF ID */
90 char buff [36];
91 bstrfmt (buff, "%08X", BSWAP32(c->ident.grfid));
92 SetDParamStr(0, buff);
93 y = DrawStringMultiLine (dpi, x, right, y, bottom, STR_NEWGRF_SETTINGS_GRF_ID);
95 if ((_settings_client.gui.newgrf_developer_tools || _settings_client.gui.newgrf_show_old_versions) && c->version != 0) {
96 SetDParam(0, c->version);
97 y = DrawStringMultiLine (dpi, x, right, y, bottom, STR_NEWGRF_SETTINGS_VERSION);
99 if ((_settings_client.gui.newgrf_developer_tools || _settings_client.gui.newgrf_show_old_versions) && c->min_loadable_version != 0) {
100 SetDParam(0, c->min_loadable_version);
101 y = DrawStringMultiLine (dpi, x, right, y, bottom, STR_NEWGRF_SETTINGS_MIN_VERSION);
104 /* Prepare and draw MD5 sum */
105 md5sumToString (buff, c->ident.md5sum);
106 SetDParamStr(0, buff);
107 y = DrawStringMultiLine (dpi, x, right, y, bottom, STR_NEWGRF_SETTINGS_MD5SUM);
109 /* Show GRF parameter list */
110 if (show_params) {
111 sstring<512> buff;
112 if (c->num_params > 0) {
113 GRFBuildParamList (&buff, c);
114 SetDParam(0, STR_JUST_RAW_STRING);
115 SetDParamStr (1, buff.c_str());
116 } else {
117 SetDParam(0, STR_LAND_AREA_INFORMATION_LOCAL_AUTHORITY_NONE);
119 y = DrawStringMultiLine (dpi, x, right, y, bottom, STR_NEWGRF_SETTINGS_PARAMETER);
121 /* Draw the palette of the NewGRF */
122 if (c->palette & GRFP_BLT_32BPP) {
123 SetDParamStr(0, (c->palette & GRFP_USE_WINDOWS) ? "Legacy (W) / 32 bpp" : "Default (D) / 32 bpp");
124 } else {
125 SetDParamStr(0, (c->palette & GRFP_USE_WINDOWS) ? "Legacy (W)" : "Default (D)");
127 y = DrawStringMultiLine (dpi, x, right, y, bottom, STR_NEWGRF_SETTINGS_PALETTE);
130 /* Show flags */
131 if (c->status == GCS_NOT_FOUND) y = DrawStringMultiLine (dpi, x, right, y, bottom, STR_NEWGRF_SETTINGS_NOT_FOUND);
132 if (c->status == GCS_DISABLED) y = DrawStringMultiLine (dpi, x, right, y, bottom, STR_NEWGRF_SETTINGS_DISABLED);
133 if (HasBit(c->flags, GCF_INVALID)) y = DrawStringMultiLine (dpi, x, right, y, bottom, STR_NEWGRF_SETTINGS_INCOMPATIBLE);
134 if (HasBit(c->flags, GCF_COMPATIBLE)) y = DrawStringMultiLine (dpi, x, right, y, bottom, STR_NEWGRF_COMPATIBLE_LOADED);
136 /* Draw GRF info if it exists */
137 if (!StrEmpty(c->GetDescription())) {
138 SetDParamStr(0, c->GetDescription());
139 y = DrawStringMultiLine (dpi, x, right, y, bottom, STR_BLACK_RAW_STRING);
140 } else {
141 y = DrawStringMultiLine (dpi, x, right, y, bottom, STR_NEWGRF_SETTINGS_NO_INFO);
146 * Window for setting the parameters of a NewGRF.
148 struct NewGRFParametersWindow : public Window {
149 static GRFParameterInfo dummy_parameter_info; ///< Dummy info in case a newgrf didn't provide info about some parameter.
150 GRFConfig *grf_config; ///< Set the parameters of this GRFConfig.
151 uint clicked_button; ///< The row in which a button was clicked or UINT_MAX.
152 bool clicked_increase; ///< True if the increase button was clicked, false for the decrease button.
153 bool clicked_dropdown; ///< Whether the dropdown is open.
154 bool closing_dropdown; ///< True, if the dropdown list is currently closing.
155 int timeout; ///< How long before we unpress the last-pressed button?
156 uint clicked_row; ///< The selected parameter
157 int line_height; ///< Height of a row in the matrix widget.
158 Scrollbar *vscroll;
159 bool action14present; ///< True if action14 information is present.
160 bool editable; ///< Allow editing parameters.
162 NewGRFParametersWindow (const WindowDesc *desc, GRFConfig *c, bool editable) : Window(desc),
163 grf_config(c),
164 clicked_button(UINT_MAX),
165 clicked_increase(false),
166 clicked_dropdown(false),
167 closing_dropdown(false),
168 timeout(0),
169 clicked_row(UINT_MAX),
170 line_height(0),
171 vscroll(NULL),
172 action14present ((c->num_valid_params != lengthof(c->param) || c->param_info.Length() != 0)),
173 editable(editable)
175 this->CreateNestedTree();
176 this->vscroll = this->GetScrollbar(WID_NP_SCROLLBAR);
177 this->GetWidget<NWidgetStacked>(WID_NP_SHOW_NUMPAR)->SetDisplayedPlane(this->action14present ? SZSP_HORIZONTAL : 0);
178 this->GetWidget<NWidgetStacked>(WID_NP_SHOW_DESCRIPTION)->SetDisplayedPlane(this->action14present ? 0 : SZSP_HORIZONTAL);
179 this->InitNested(); // Initializes 'this->line_height' as side effect.
181 this->SetWidgetDisabledState(WID_NP_RESET, !this->editable);
183 this->InvalidateData();
187 * Get a dummy parameter-info object with default information.
188 * @param nr The param number that should be changed.
189 * @return GRFParameterInfo with dummy information about the given parameter.
191 static GRFParameterInfo *GetDummyParameterInfo(uint nr)
193 dummy_parameter_info.param_nr = nr;
194 return &dummy_parameter_info;
197 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
199 switch (widget) {
200 case WID_NP_NUMPAR_DEC:
201 case WID_NP_NUMPAR_INC: {
202 size->width = max(SETTING_BUTTON_WIDTH / 2, FONT_HEIGHT_NORMAL);
203 size->height = max(SETTING_BUTTON_HEIGHT, FONT_HEIGHT_NORMAL);
204 break;
207 case WID_NP_NUMPAR: {
208 SetDParamMaxValue(0, lengthof(this->grf_config->param));
209 Dimension d = GetStringBoundingBox(this->GetWidget<NWidgetCore>(widget)->widget_data);
210 d.width += padding.width;
211 d.height += padding.height;
212 *size = maxdim(*size, d);
213 break;
216 case WID_NP_BACKGROUND:
217 this->line_height = max(SETTING_BUTTON_HEIGHT, FONT_HEIGHT_NORMAL) + WD_MATRIX_TOP + WD_MATRIX_BOTTOM;
219 resize->width = 1;
220 resize->height = this->line_height;
221 size->height = 5 * this->line_height;
222 break;
224 case WID_NP_DESCRIPTION:
225 /* Minimum size of 4 lines. The 500 is the default size of the window. */
226 const uint width = 500 - WD_FRAMERECT_LEFT - WD_FRAMERECT_RIGHT;
227 uint hmax = FONT_HEIGHT_NORMAL * 4;
228 for (uint i = 0; i < this->grf_config->param_info.Length(); i++) {
229 const GRFParameterInfo *par_info = this->grf_config->param_info[i];
230 if (par_info == NULL) continue;
231 const char *desc = par_info->desc.get_string();
232 if (desc == NULL) continue;
233 uint h = GetStringHeight (desc, width);
234 hmax = max (h, hmax);
236 size->height = hmax + WD_TEXTPANEL_TOP + WD_TEXTPANEL_BOTTOM;
237 break;
241 virtual void SetStringParameters(int widget) const
243 switch (widget) {
244 case WID_NP_NUMPAR:
245 SetDParam(0, this->vscroll->GetCount());
246 break;
250 void DrawWidget (BlitArea *dpi, const Rect &r, int widget) const OVERRIDE
252 if (widget == WID_NP_DESCRIPTION) {
253 const GRFParameterInfo *par_info = (this->clicked_row < this->grf_config->param_info.Length()) ? this->grf_config->param_info[this->clicked_row] : NULL;
254 if (par_info == NULL) return;
255 const char *desc = par_info->desc.get_string();
256 if (desc == NULL) return;
257 DrawStringMultiLine (dpi, r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + WD_TEXTPANEL_TOP, r.bottom - WD_TEXTPANEL_BOTTOM, desc, TC_BLACK);
258 return;
259 } else if (widget != WID_NP_BACKGROUND) {
260 return;
263 bool rtl = _current_text_dir == TD_RTL;
264 uint buttons_left = rtl ? r.right - SETTING_BUTTON_WIDTH - 3 : r.left + 4;
265 uint text_left = r.left + (rtl ? WD_FRAMERECT_LEFT : SETTING_BUTTON_WIDTH + 8);
266 uint text_right = r.right - (rtl ? SETTING_BUTTON_WIDTH + 8 : WD_FRAMERECT_RIGHT);
268 int y = r.top;
269 int button_y_offset = (this->line_height - SETTING_BUTTON_HEIGHT) / 2;
270 int text_y_offset = (this->line_height - FONT_HEIGHT_NORMAL) / 2;
271 for (uint i = this->vscroll->GetPosition(); this->vscroll->IsVisible(i) && i < this->vscroll->GetCount(); i++) {
272 GRFParameterInfo *par_info = (i < this->grf_config->param_info.Length()) ? this->grf_config->param_info[i] : NULL;
273 if (par_info == NULL) par_info = GetDummyParameterInfo(i);
274 uint32 current_value = par_info->GetValue(this->grf_config);
275 bool selected = (i == this->clicked_row);
277 if (par_info->type == PTYPE_BOOL) {
278 DrawBoolButton (dpi, buttons_left, y + button_y_offset, current_value != 0, this->editable);
279 SetDParam(2, par_info->GetValue(this->grf_config) == 0 ? STR_CONFIG_SETTING_OFF : STR_CONFIG_SETTING_ON);
280 } else if (par_info->type == PTYPE_UINT_ENUM) {
281 if (par_info->complete_labels) {
282 DrawDropDownButton (dpi, buttons_left, y + button_y_offset, COLOUR_YELLOW, this->clicked_row == i && this->clicked_dropdown, this->editable);
283 } else {
284 DrawArrowButtons (dpi, buttons_left, y + button_y_offset, COLOUR_YELLOW, (this->clicked_button == i) ? 1 + (this->clicked_increase != rtl) : 0, this->editable && current_value > par_info->min_value, this->editable && current_value < par_info->max_value);
286 SetDParam(2, STR_JUST_INT);
287 SetDParam(3, current_value);
288 std::map <uint32, GRFTextMap>::iterator iter =
289 par_info->value_names.find (current_value);
290 if (iter != par_info->value_names.end()) {
291 const char *label = iter->second.get_string();
292 if (label != NULL) {
293 SetDParam(2, STR_JUST_RAW_STRING);
294 SetDParamStr(3, label);
299 const char *name = par_info->name.get_string();
300 if (name != NULL) {
301 SetDParam(0, STR_JUST_RAW_STRING);
302 SetDParamStr(1, name);
303 } else {
304 SetDParam(0, STR_NEWGRF_PARAMETERS_DEFAULT_NAME);
305 SetDParam(1, i + 1);
308 DrawString (dpi, text_left, text_right, y + text_y_offset, STR_NEWGRF_PARAMETERS_SETTING, selected ? TC_WHITE : TC_LIGHT_BLUE);
309 y += this->line_height;
313 void OnPaint (BlitArea *dpi) OVERRIDE
315 if (this->closing_dropdown) {
316 this->closing_dropdown = false;
317 this->clicked_dropdown = false;
319 this->DrawWidgets (dpi);
322 virtual void OnClick(Point pt, int widget, int click_count)
324 switch (widget) {
325 case WID_NP_NUMPAR_DEC:
326 if (this->editable && !this->action14present && this->grf_config->num_params > 0) {
327 this->grf_config->num_params--;
328 this->InvalidateData();
329 SetWindowDirty(WC_GAME_OPTIONS, WN_GAME_OPTIONS_NEWGRF_STATE);
331 break;
333 case WID_NP_NUMPAR_INC: {
334 GRFConfig *c = this->grf_config;
335 if (this->editable && !this->action14present && c->num_params < c->num_valid_params) {
336 c->param[c->num_params++] = 0;
337 this->InvalidateData();
338 SetWindowDirty(WC_GAME_OPTIONS, WN_GAME_OPTIONS_NEWGRF_STATE);
340 break;
343 case WID_NP_BACKGROUND: {
344 if (!this->editable) break;
345 uint num = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_NP_BACKGROUND);
346 if (num >= this->vscroll->GetCount()) break;
347 if (this->clicked_row != num) {
348 DeleteChildWindows(WC_QUERY_STRING);
349 HideDropDownMenu(this);
350 this->clicked_row = num;
351 this->clicked_dropdown = false;
354 const NWidgetBase *wid = this->GetWidget<NWidgetBase>(WID_NP_BACKGROUND);
355 int x = pt.x - wid->pos_x;
356 if (_current_text_dir == TD_RTL) x = wid->current_x - 1 - x;
357 x -= 4;
359 GRFParameterInfo *par_info = (num < this->grf_config->param_info.Length()) ? this->grf_config->param_info[num] : NULL;
360 if (par_info == NULL) par_info = GetDummyParameterInfo(num);
362 /* One of the arrows is clicked */
363 uint32 old_val = par_info->GetValue(this->grf_config);
364 if (par_info->type != PTYPE_BOOL && IsInsideMM(x, 0, SETTING_BUTTON_WIDTH) && par_info->complete_labels) {
365 if (this->clicked_dropdown) {
366 /* unclick the dropdown */
367 HideDropDownMenu(this);
368 this->clicked_dropdown = false;
369 this->closing_dropdown = false;
370 } else {
371 const NWidgetBase *wid = this->GetWidget<NWidgetBase>(WID_NP_BACKGROUND);
372 int rel_y = (pt.y - (int)wid->pos_y) % this->line_height;
374 Rect wi_rect;
375 wi_rect.left = pt.x - (_current_text_dir == TD_RTL ? SETTING_BUTTON_WIDTH - 1 - x : x);;
376 wi_rect.right = wi_rect.left + SETTING_BUTTON_WIDTH - 1;
377 wi_rect.top = pt.y - rel_y + (this->line_height - SETTING_BUTTON_HEIGHT) / 2;
378 wi_rect.bottom = wi_rect.top + SETTING_BUTTON_HEIGHT - 1;
380 /* For dropdowns we also have to check the y position thoroughly, the mouse may not above the just opening dropdown */
381 if (pt.y >= wi_rect.top && pt.y <= wi_rect.bottom) {
382 this->clicked_dropdown = true;
383 this->closing_dropdown = false;
385 DropDownList *list = new DropDownList();
386 for (uint32 i = par_info->min_value; i <= par_info->max_value; i++) {
387 *list->Append() = new DropDownListCharStringItem (par_info->value_names[i].get_string(), i, false);
390 ShowDropDownListAt(this, list, old_val, -1, wi_rect, COLOUR_ORANGE, true);
393 } else if (IsInsideMM(x, 0, SETTING_BUTTON_WIDTH)) {
394 uint32 val = old_val;
395 if (par_info->type == PTYPE_BOOL) {
396 val = !val;
397 } else {
398 if (x >= SETTING_BUTTON_WIDTH / 2) {
399 /* Increase button clicked */
400 if (val < par_info->max_value) val++;
401 this->clicked_increase = true;
402 } else {
403 /* Decrease button clicked */
404 if (val > par_info->min_value) val--;
405 this->clicked_increase = false;
408 if (val != old_val) {
409 par_info->SetValue(this->grf_config, val);
411 this->clicked_button = num;
412 this->timeout = 5;
414 } else if (par_info->type == PTYPE_UINT_ENUM && !par_info->complete_labels && click_count >= 2) {
415 /* Display a query box so users can enter a custom value. */
416 SetDParam(0, old_val);
417 ShowQueryString(STR_JUST_INT, STR_CONFIG_SETTING_QUERY_CAPTION, 10, this, CS_NUMERAL, QSF_NONE);
419 this->SetDirty();
420 break;
423 case WID_NP_RESET:
424 if (!this->editable) break;
425 this->grf_config->SetParameterDefaults();
426 this->InvalidateData();
427 SetWindowDirty(WC_GAME_OPTIONS, WN_GAME_OPTIONS_NEWGRF_STATE);
428 break;
430 case WID_NP_ACCEPT:
431 this->Delete();
432 break;
436 virtual void OnQueryTextFinished(char *str)
438 if (StrEmpty(str)) return;
439 int32 value = atoi(str);
440 GRFParameterInfo *par_info = ((uint)this->clicked_row < this->grf_config->param_info.Length()) ? this->grf_config->param_info[this->clicked_row] : NULL;
441 if (par_info == NULL) par_info = GetDummyParameterInfo(this->clicked_row);
442 uint32 val = Clamp<uint32>(value, par_info->min_value, par_info->max_value);
443 par_info->SetValue(this->grf_config, val);
444 this->SetDirty();
447 virtual void OnDropdownSelect(int widget, int index)
449 assert(this->clicked_dropdown);
450 GRFParameterInfo *par_info = ((uint)this->clicked_row < this->grf_config->param_info.Length()) ? this->grf_config->param_info[this->clicked_row] : NULL;
451 if (par_info == NULL) par_info = GetDummyParameterInfo(this->clicked_row);
452 par_info->SetValue(this->grf_config, index);
453 this->SetDirty();
456 virtual void OnDropdownClose(Point pt, int widget, int index, bool instant_close)
458 /* We cannot raise the dropdown button just yet. OnClick needs some hint, whether
459 * the same dropdown button was clicked again, and then not open the dropdown again.
460 * So, we only remember that it was closed, and process it on the next OnPaint, which is
461 * after OnClick. */
462 assert(this->clicked_dropdown);
463 this->closing_dropdown = true;
464 this->SetDirty();
467 virtual void OnResize()
469 this->vscroll->SetCapacityFromWidget(this, WID_NP_BACKGROUND);
473 * Some data on this window has become invalid.
474 * @param data Information about the changed data.
475 * @param gui_scope Whether the call is done from GUI scope. You may not do everything when not in GUI scope. See #InvalidateWindowData() for details.
477 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
479 if (!gui_scope) return;
480 if (!this->action14present) {
481 this->SetWidgetDisabledState(WID_NP_NUMPAR_DEC, !this->editable || this->grf_config->num_params == 0);
482 this->SetWidgetDisabledState(WID_NP_NUMPAR_INC, !this->editable || this->grf_config->num_params >= this->grf_config->num_valid_params);
485 this->vscroll->SetCount(this->action14present ? this->grf_config->num_valid_params : this->grf_config->num_params);
486 if (this->clicked_row != UINT_MAX && this->clicked_row >= this->vscroll->GetCount()) {
487 this->clicked_row = UINT_MAX;
488 DeleteChildWindows(WC_QUERY_STRING);
492 virtual void OnTick()
494 if (--this->timeout == 0) {
495 this->clicked_button = UINT_MAX;
496 this->SetDirty();
500 GRFParameterInfo NewGRFParametersWindow::dummy_parameter_info(0);
503 static const NWidgetPart _nested_newgrf_parameter_widgets[] = {
504 NWidget(NWID_HORIZONTAL),
505 NWidget(WWT_CLOSEBOX, COLOUR_MAUVE),
506 NWidget(WWT_CAPTION, COLOUR_MAUVE), SetDataTip(STR_NEWGRF_PARAMETERS_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
507 NWidget(WWT_DEFSIZEBOX, COLOUR_MAUVE),
508 EndContainer(),
509 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_NP_SHOW_NUMPAR),
510 NWidget(WWT_PANEL, COLOUR_MAUVE), SetResize(1, 0), SetFill(1, 0), SetPIP(4, 0, 4),
511 NWidget(NWID_HORIZONTAL), SetPIP(4, 0, 4),
512 NWidget(WWT_PUSHARROWBTN, COLOUR_YELLOW, WID_NP_NUMPAR_DEC), SetMinimalSize(12, 12), SetDataTip(AWV_DECREASE, STR_NULL),
513 NWidget(WWT_PUSHARROWBTN, COLOUR_YELLOW, WID_NP_NUMPAR_INC), SetMinimalSize(12, 12), SetDataTip(AWV_INCREASE, STR_NULL),
514 NWidget(WWT_TEXT, COLOUR_MAUVE, WID_NP_NUMPAR), SetResize(1, 0), SetFill(1, 0), SetPadding(0, 0, 0, 4), SetDataTip(STR_NEWGRF_PARAMETERS_NUM_PARAM, STR_NULL),
515 EndContainer(),
516 EndContainer(),
517 EndContainer(),
518 NWidget(NWID_HORIZONTAL),
519 NWidget(WWT_MATRIX, COLOUR_MAUVE, WID_NP_BACKGROUND), SetMinimalSize(188, 182), SetResize(1, 1), SetFill(1, 0), SetMatrixDataTip(1, 0, STR_NULL), SetScrollbar(WID_NP_SCROLLBAR),
520 NWidget(NWID_VSCROLLBAR, COLOUR_MAUVE, WID_NP_SCROLLBAR),
521 EndContainer(),
522 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_NP_SHOW_DESCRIPTION),
523 NWidget(WWT_PANEL, COLOUR_MAUVE, WID_NP_DESCRIPTION), SetResize(1, 0), SetFill(1, 0),
524 EndContainer(),
525 EndContainer(),
526 NWidget(NWID_HORIZONTAL),
527 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
528 NWidget(WWT_PUSHTXTBTN, COLOUR_MAUVE, WID_NP_ACCEPT), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_NEWGRF_PARAMETERS_CLOSE, STR_NULL),
529 NWidget(WWT_PUSHTXTBTN, COLOUR_MAUVE, WID_NP_RESET), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_NEWGRF_PARAMETERS_RESET, STR_NEWGRF_PARAMETERS_RESET_TOOLTIP),
530 EndContainer(),
531 NWidget(WWT_RESIZEBOX, COLOUR_MAUVE),
532 EndContainer(),
535 /** Window preferences for the change grf parameters window */
536 static WindowDesc::Prefs _newgrf_parameters_prefs ("settings_newgrf_config");
538 /** Window definition for the change grf parameters window */
539 static const WindowDesc _newgrf_parameters_desc(
540 WDP_CENTER, 500, 208,
541 WC_GRF_PARAMETERS, WC_NONE,
543 _nested_newgrf_parameter_widgets, lengthof(_nested_newgrf_parameter_widgets),
544 &_newgrf_parameters_prefs
547 static void OpenGRFParameterWindow(GRFConfig *c, bool editable)
549 DeleteWindowByClass(WC_GRF_PARAMETERS);
550 new NewGRFParametersWindow(&_newgrf_parameters_desc, c, editable);
553 /** Window for displaying the textfile of a NewGRF. */
554 struct NewGRFTextfileWindow : public TextfileWindow {
555 const GRFConfig *grf_config; ///< View the textfile of this GRFConfig.
557 NewGRFTextfileWindow (TextfileType file_type, const GRFConfig *c)
558 : TextfileWindow (c->GetTextfile(file_type)), grf_config(c)
560 this->CheckForMissingGlyphs();
563 /* virtual */ void SetStringParameters(int widget) const
565 if (widget == WID_TF_CAPTION) {
566 SetDParam(0, STR_CONTENT_TYPE_NEWGRF);
567 SetDParamStr(1, this->grf_config->GetName());
572 void ShowNewGRFTextfileWindow(TextfileType file_type, const GRFConfig *c)
574 DeleteWindowByClass(WC_TEXTFILE);
575 new NewGRFTextfileWindow(file_type, c);
578 static GRFPresetList _grf_preset_list; ///< List of known NewGRF presets. @see GetGRFPresetList
580 class DropDownListPresetItem : public DropDownListItem {
581 public:
582 DropDownListPresetItem(int result) : DropDownListItem(result, false) {}
584 virtual ~DropDownListPresetItem() {}
586 bool Selectable() const
588 return true;
591 void Draw (BlitArea *dpi, int left, int right, int top, int bottom, bool sel, int bg_colour) const OVERRIDE
593 DrawString (dpi, left + 2, right + 2, top, _grf_preset_list[this->result], sel ? TC_WHITE : TC_BLACK);
598 typedef std::map<uint32, const GRFConfig *> GrfIdMap; ///< Map of grfid to the grf config.
601 * Add all grf configs from \a c into the map.
602 * @param c Grf list to add.
603 * @param grfid_map Map to add them to.
605 static void FillGrfidMap(const GRFConfig *c, GrfIdMap *grfid_map)
607 while (c != NULL) {
608 std::pair<uint32, const GRFConfig *> p(c->ident.grfid, c);
609 grfid_map->insert(p);
610 c = c->next;
614 static void NewGRFConfirmationCallback(Window *w, bool confirmed);
615 static void ShowSavePresetWindow(const char *initial_text);
618 * Window for showing NewGRF files
620 struct NewGRFWindow : public Window, NewGRFScanCallback {
621 typedef GUIList <const GRFConfig *> GUIGRFConfigList;
623 static const uint EDITBOX_MAX_SIZE = 50;
625 static Listing last_sorting; ///< Default sorting of #GUIGRFConfigList.
626 static GUIGRFConfigList::SortFunction * const sorter_funcs[]; ///< Sort functions of the #GUIGRFConfigList.
628 GUIGRFConfigList avails; ///< Available (non-active) grfs.
629 const GRFConfig *avail_sel; ///< Currently selected available grf. \c NULL is none is selected.
630 int avail_pos; ///< Index of #avail_sel if existing, else \c -1.
631 StringFilter string_filter; ///< Filter for available grf.
632 QueryStringN<EDITBOX_MAX_SIZE> filter_editbox; ///< Filter editbox;
634 GRFConfig *actives; ///< Temporary active grf list to which changes are made.
635 GRFConfig *active_sel; ///< Selected active grf item.
637 GRFConfig **orig_list; ///< List active grfs in the game. Used as initial value, may be updated by the window.
638 bool editable; ///< Is the window editable?
639 bool show_params; ///< Are the grf-parameters shown in the info-panel?
640 bool execute; ///< On pressing 'apply changes' are grf changes applied immediately, or only list is updated.
641 int preset; ///< Selected preset or \c -1 if none selected.
642 int active_over; ///< Active GRF item over which another one is dragged, \c -1 if none.
644 Scrollbar *vscroll;
645 Scrollbar *vscroll2;
647 NewGRFWindow (const WindowDesc *desc, bool editable, bool show_params, bool execute, GRFConfig **orig_list) :
648 Window (desc), NewGRFScanCallback(),
649 avails(), avail_sel (NULL), avail_pos (-1),
650 string_filter(), filter_editbox(),
651 actives (NULL), active_sel (NULL), orig_list (orig_list),
652 editable (editable), show_params (show_params),
653 execute (execute), preset (-1), active_over (-1)
655 CopyGRFConfigList(&this->actives, *orig_list, false);
656 GetGRFPresetList(&_grf_preset_list);
658 this->CreateNestedTree();
659 this->vscroll = this->GetScrollbar(WID_NS_SCROLLBAR);
660 this->vscroll2 = this->GetScrollbar(WID_NS_SCROLL2BAR);
662 this->GetWidget<NWidgetStacked>(WID_NS_SHOW_REMOVE)->SetDisplayedPlane(this->editable ? 0 : 1);
663 this->GetWidget<NWidgetStacked>(WID_NS_SHOW_APPLY)->SetDisplayedPlane(this->editable ? 0 : this->show_params ? 1 : SZSP_HORIZONTAL);
664 this->InitNested(WN_GAME_OPTIONS_NEWGRF_STATE);
666 this->querystrings[WID_NS_FILTER] = &this->filter_editbox;
667 this->filter_editbox.cancel_button = QueryString::ACTION_CLEAR;
668 if (editable) {
669 this->SetFocusedWidget(WID_NS_FILTER);
670 } else {
671 this->DisableWidget(WID_NS_FILTER);
674 this->avails.SetListing(this->last_sorting);
675 this->avails.SetSortFuncs(this->sorter_funcs);
676 this->avails.ForceRebuild();
678 this->OnInvalidateData(GOID_NEWGRF_LIST_EDITED);
681 void OnDelete (void) FINAL_OVERRIDE
683 DeleteWindowByClass(WC_GRF_PARAMETERS);
684 DeleteWindowByClass(WC_TEXTFILE);
685 DeleteWindowByClass(WC_SAVE_PRESET);
687 if (this->editable && !this->execute) {
688 CopyGRFConfigList(this->orig_list, this->actives, true);
689 ResetGRFConfig(false);
690 ReloadNewGRFData();
693 /* Remove the temporary copy of grf-list used in window */
694 ClearGRFConfigList(&this->actives);
695 _grf_preset_list.Clear();
699 * Test whether the currently active set of NewGRFs can be upgraded with the available NewGRFs.
700 * @return Whether an upgrade is possible.
702 bool CanUpgradeCurrent()
704 GrfIdMap grfid_map;
705 FillGrfidMap(this->actives, &grfid_map);
707 for (const GRFConfig *a = _all_grfs; a != NULL; a = a->next) {
708 GrfIdMap::const_iterator iter = grfid_map.find(a->ident.grfid);
709 if (iter != grfid_map.end() && a->version > iter->second->version) return true;
711 return false;
714 /** Upgrade the currently active set of NewGRFs. */
715 void UpgradeCurrent()
717 GrfIdMap grfid_map;
718 FillGrfidMap(this->actives, &grfid_map);
720 for (const GRFConfig *a = _all_grfs; a != NULL; a = a->next) {
721 GrfIdMap::iterator iter = grfid_map.find(a->ident.grfid);
722 if (iter == grfid_map.end() || iter->second->version >= a->version) continue;
724 GRFConfig **c = &this->actives;
725 while (*c != iter->second) c = &(*c)->next;
726 GRFConfig *d = new GRFConfig(*a);
727 d->next = (*c)->next;
728 d->CopyParams(**c);
729 if (this->active_sel == *c) this->active_sel = NULL;
730 delete *c;
731 *c = d;
732 iter->second = d;
736 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
738 switch (widget) {
739 case WID_NS_FILE_LIST:
741 Dimension d = maxdim(GetSpriteSize(SPR_SQUARE), GetSpriteSize(SPR_WARNING_SIGN));
742 resize->height = max(d.height + 2U, FONT_HEIGHT_NORMAL + 2U);
743 size->height = max(size->height, WD_FRAMERECT_TOP + 6 * resize->height + WD_FRAMERECT_BOTTOM);
744 break;
747 case WID_NS_AVAIL_LIST:
748 resize->height = max(12, FONT_HEIGHT_NORMAL + 2);
749 size->height = max(size->height, WD_FRAMERECT_TOP + 8 * resize->height + WD_FRAMERECT_BOTTOM);
750 break;
752 case WID_NS_NEWGRF_INFO_TITLE: {
753 Dimension dim = GetStringBoundingBox(STR_NEWGRF_SETTINGS_INFO_TITLE);
754 size->height = max(size->height, dim.height + WD_FRAMETEXT_TOP + WD_FRAMETEXT_BOTTOM);
755 size->width = max(size->width, dim.width + WD_FRAMETEXT_LEFT + WD_FRAMETEXT_RIGHT);
756 break;
759 case WID_NS_NEWGRF_INFO:
760 size->height = max(size->height, WD_FRAMERECT_TOP + 10 * FONT_HEIGHT_NORMAL + WD_FRAMERECT_BOTTOM + padding.height + 2);
761 break;
763 case WID_NS_PRESET_LIST: {
764 Dimension d = GetStringBoundingBox(STR_NUM_CUSTOM);
765 for (uint i = 0; i < _grf_preset_list.Length(); i++) {
766 if (_grf_preset_list[i] != NULL) {
767 SetDParamStr(0, _grf_preset_list[i]);
768 d = maxdim(d, GetStringBoundingBox(STR_JUST_RAW_STRING));
771 d.width += padding.width;
772 *size = maxdim(d, *size);
773 break;
776 case WID_NS_CONTENT_DOWNLOAD:
777 case WID_NS_CONTENT_DOWNLOAD2: {
778 Dimension d = GetStringBoundingBox(STR_NEWGRF_SETTINGS_FIND_MISSING_CONTENT_BUTTON);
779 *size = maxdim(d, GetStringBoundingBox(STR_INTRO_ONLINE_CONTENT));
780 size->width += padding.width;
781 size->height += padding.height;
782 break;
787 virtual void OnResize()
789 this->vscroll->SetCapacityFromWidget(this, WID_NS_FILE_LIST);
790 this->vscroll2->SetCapacityFromWidget(this, WID_NS_AVAIL_LIST);
793 virtual void SetStringParameters(int widget) const
795 switch (widget) {
796 case WID_NS_PRESET_LIST:
797 if (this->preset == -1) {
798 SetDParam(0, STR_NUM_CUSTOM);
799 } else {
800 SetDParam(0, STR_JUST_RAW_STRING);
801 SetDParamStr(1, _grf_preset_list[this->preset]);
803 break;
808 * Pick the palette for the sprite of the grf to display.
809 * @param c grf to display.
810 * @return Palette for the sprite.
812 inline PaletteID GetPalette(const GRFConfig *c) const
814 PaletteID pal;
816 /* Pick a colour */
817 switch (c->status) {
818 case GCS_NOT_FOUND:
819 case GCS_DISABLED:
820 pal = PALETTE_TO_RED;
821 break;
822 case GCS_ACTIVATED:
823 pal = PALETTE_TO_GREEN;
824 break;
825 default:
826 pal = PALETTE_TO_BLUE;
827 break;
830 /* Do not show a "not-failure" colour when it actually failed to load */
831 if (pal != PALETTE_TO_RED) {
832 if (HasBit(c->flags, GCF_STATIC)) {
833 pal = PALETTE_TO_GREY;
834 } else if (HasBit(c->flags, GCF_COMPATIBLE)) {
835 pal = PALETTE_TO_ORANGE;
839 return pal;
842 void DrawWidget (BlitArea *dpi, const Rect &r, int widget) const OVERRIDE
844 switch (widget) {
845 case WID_NS_FILE_LIST: {
846 GfxFillRect (dpi, r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, PC_BLACK);
848 uint step_height = this->GetWidget<NWidgetBase>(WID_NS_FILE_LIST)->resize_y;
849 uint y = r.top + WD_FRAMERECT_TOP;
850 Dimension square = GetSpriteSize(SPR_SQUARE);
851 Dimension warning = GetSpriteSize(SPR_WARNING_SIGN);
852 int square_offset_y = (step_height - square.height) / 2;
853 int warning_offset_y = (step_height - warning.height) / 2;
854 int offset_y = (step_height - FONT_HEIGHT_NORMAL) / 2;
856 bool rtl = _current_text_dir == TD_RTL;
857 uint text_left = rtl ? r.left + WD_FRAMERECT_LEFT : r.left + square.width + 15;
858 uint text_right = rtl ? r.right - square.width - 15 : r.right - WD_FRAMERECT_RIGHT;
859 uint square_left = rtl ? r.right - square.width - 5 : r.left + 5;
860 uint warning_left = rtl ? r.right - square.width - warning.width - 10 : r.left + square.width + 10;
862 int i = 0;
863 for (const GRFConfig *c = this->actives; c != NULL; c = c->next, i++) {
864 if (this->vscroll->IsVisible(i)) {
865 const char *text = c->GetName();
866 bool h = (this->active_sel == c);
867 PaletteID pal = this->GetPalette(c);
869 if (h) {
870 GfxFillRect (dpi, r.left + 1, y, r.right - 1, y + step_height - 1, PC_DARK_BLUE);
871 } else if (i == this->active_over) {
872 /* Get index of current selection. */
873 int active_sel_pos = 0;
874 for (GRFConfig *c = this->actives; c != NULL && c != this->active_sel; c = c->next, active_sel_pos++) {}
875 if (active_sel_pos != this->active_over) {
876 uint top = this->active_over < active_sel_pos ? y + 1 : y + step_height - 2;
877 GfxFillRect (dpi, r.left + WD_FRAMERECT_LEFT, top - 1, r.right - WD_FRAMERECT_RIGHT, top + 1, PC_GREY);
880 DrawSprite (dpi, SPR_SQUARE, pal, square_left, y + square_offset_y);
881 if (c->error != NULL) DrawSprite (dpi, SPR_WARNING_SIGN, 0, warning_left, y + warning_offset_y);
882 uint txtoffset = c->error == NULL ? 0 : warning.width;
883 DrawString (dpi, text_left + (rtl ? 0 : txtoffset), text_right - (rtl ? txtoffset : 0), y + offset_y, text, h ? TC_WHITE : TC_ORANGE);
884 y += step_height;
887 if (i == this->active_over && this->vscroll->IsVisible(i)) { // Highlight is after the last GRF entry.
888 GfxFillRect (dpi, r.left + WD_FRAMERECT_LEFT, y, r.right - WD_FRAMERECT_RIGHT, y + 2, PC_GREY);
890 break;
893 case WID_NS_AVAIL_LIST: {
894 GfxFillRect (dpi, r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, this->active_over == -2 ? PC_DARK_GREY : PC_BLACK);
896 uint step_height = this->GetWidget<NWidgetBase>(WID_NS_AVAIL_LIST)->resize_y;
897 int offset_y = (step_height - FONT_HEIGHT_NORMAL) / 2;
898 uint y = r.top + WD_FRAMERECT_TOP;
899 uint min_index = this->vscroll2->GetPosition();
900 uint max_index = min(min_index + this->vscroll2->GetCapacity(), this->avails.Length());
902 for (uint i = min_index; i < max_index; i++) {
903 const GRFConfig *c = this->avails[i];
904 bool h = (c == this->avail_sel);
905 const char *text = c->GetName();
907 if (h) GfxFillRect (dpi, r.left + 1, y, r.right - 1, y + step_height - 1, PC_DARK_BLUE);
908 DrawString (dpi, r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y + offset_y, text, h ? TC_WHITE : TC_SILVER);
909 y += step_height;
911 break;
914 case WID_NS_NEWGRF_INFO_TITLE:
915 /* Create the nice grayish rectangle at the details top. */
916 GfxFillRect (dpi, r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, PC_DARK_BLUE);
917 DrawString (dpi, r.left, r.right, (r.top + r.bottom - FONT_HEIGHT_NORMAL) / 2, STR_NEWGRF_SETTINGS_INFO_TITLE, TC_FROMSTRING, SA_HOR_CENTER);
918 break;
920 case WID_NS_NEWGRF_INFO: {
921 const GRFConfig *selected = this->active_sel;
922 if (selected == NULL) selected = this->avail_sel;
923 if (selected != NULL) {
924 ShowNewGRFInfo (selected, dpi, r.left + WD_FRAMERECT_LEFT, r.top + WD_FRAMERECT_TOP, r.right - WD_FRAMERECT_RIGHT, r.bottom - WD_FRAMERECT_BOTTOM, this->show_params);
926 break;
931 virtual void OnClick(Point pt, int widget, int click_count)
933 if (widget >= WID_NS_NEWGRF_TEXTFILE && widget < WID_NS_NEWGRF_TEXTFILE + TFT_END) {
934 if (this->active_sel == NULL && this->avail_sel == NULL) return;
936 ShowNewGRFTextfileWindow((TextfileType)(widget - WID_NS_NEWGRF_TEXTFILE), this->active_sel != NULL ? this->active_sel : this->avail_sel);
937 return;
940 switch (widget) {
941 case WID_NS_PRESET_LIST: {
942 DropDownList *list = new DropDownList();
944 /* Add 'None' option for clearing list */
945 *list->Append() = new DropDownListStringItem(STR_NONE, -1, false);
947 for (uint i = 0; i < _grf_preset_list.Length(); i++) {
948 if (_grf_preset_list[i] != NULL) {
949 *list->Append() = new DropDownListPresetItem(i);
953 this->DeleteChildWindows(WC_QUERY_STRING); // Remove the parameter query window
954 ShowDropDownList(this, list, this->preset, WID_NS_PRESET_LIST);
955 break;
958 case WID_NS_OPEN_URL: {
959 const GRFConfig *c = (this->avail_sel == NULL) ? this->active_sel : this->avail_sel;
961 extern void OpenBrowser(const char *url);
962 OpenBrowser(c->GetURL());
963 break;
966 case WID_NS_PRESET_SAVE:
967 ShowSavePresetWindow((this->preset == -1) ? NULL : _grf_preset_list[this->preset]);
968 break;
970 case WID_NS_PRESET_DELETE:
971 if (this->preset == -1) return;
973 DeleteGRFPresetFromConfig(_grf_preset_list[this->preset]);
974 GetGRFPresetList(&_grf_preset_list);
975 this->preset = -1;
976 this->InvalidateData();
977 this->DeleteChildWindows(WC_QUERY_STRING); // Remove the parameter query window
978 break;
980 case WID_NS_MOVE_UP: { // Move GRF up
981 if (this->active_sel == NULL || !this->editable) break;
983 int pos = 0;
984 for (GRFConfig **pc = &this->actives; *pc != NULL; pc = &(*pc)->next, pos++) {
985 GRFConfig *c = *pc;
986 if (c->next == this->active_sel) {
987 c->next = this->active_sel->next;
988 this->active_sel->next = c;
989 *pc = this->active_sel;
990 break;
993 this->vscroll->ScrollTowards(pos);
994 this->preset = -1;
995 this->InvalidateData();
996 break;
999 case WID_NS_MOVE_DOWN: { // Move GRF down
1000 if (this->active_sel == NULL || !this->editable) break;
1002 int pos = 1; // Start at 1 as we swap the selected newgrf with the next one
1003 for (GRFConfig **pc = &this->actives; *pc != NULL; pc = &(*pc)->next, pos++) {
1004 GRFConfig *c = *pc;
1005 if (c == this->active_sel) {
1006 *pc = c->next;
1007 c->next = c->next->next;
1008 (*pc)->next = c;
1009 break;
1012 this->vscroll->ScrollTowards(pos);
1013 this->preset = -1;
1014 this->InvalidateData();
1015 break;
1018 case WID_NS_FILE_LIST: { // Select an active GRF.
1019 ResetPointerMode();
1021 uint i = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_NS_FILE_LIST);
1023 GRFConfig *c;
1024 for (c = this->actives; c != NULL && i > 0; c = c->next, i--) {}
1026 if (this->active_sel != c) DeleteWindowByClass(WC_GRF_PARAMETERS);
1027 this->active_sel = c;
1028 this->avail_sel = NULL;
1029 this->avail_pos = -1;
1031 this->InvalidateData();
1032 if (click_count == 1) {
1033 if (this->editable && this->active_sel != NULL) SetPointerMode (POINTER_DRAG, this, SPR_CURSOR_MOUSE);
1034 break;
1036 /* With double click, continue */
1038 FALLTHROUGH;
1040 case WID_NS_REMOVE: { // Remove GRF
1041 if (this->active_sel == NULL || !this->editable) break;
1042 DeleteWindowByClass(WC_GRF_PARAMETERS);
1044 /* Choose the next GRF file to be the selected file. */
1045 GRFConfig *newsel = this->active_sel->next;
1046 for (GRFConfig **pc = &this->actives; *pc != NULL; pc = &(*pc)->next) {
1047 GRFConfig *c = *pc;
1048 /* If the new selection is empty (i.e. we're deleting the last item
1049 * in the list, pick the file just before the selected file */
1050 if (newsel == NULL && c->next == this->active_sel) newsel = c;
1052 if (c == this->active_sel) {
1053 if (newsel == c) newsel = NULL;
1055 *pc = c->next;
1056 delete c;
1057 break;
1061 this->active_sel = newsel;
1062 this->preset = -1;
1063 this->avail_pos = -1;
1064 this->avail_sel = NULL;
1065 this->avails.ForceRebuild();
1066 this->InvalidateData(GOID_NEWGRF_LIST_EDITED);
1067 break;
1070 case WID_NS_UPGRADE: { // Upgrade GRF.
1071 if (!this->editable || this->actives == NULL) break;
1072 UpgradeCurrent();
1073 this->InvalidateData(GOID_NEWGRF_LIST_EDITED);
1074 break;
1077 case WID_NS_AVAIL_LIST: { // Select a non-active GRF.
1078 ResetPointerMode();
1080 uint i = this->vscroll2->GetScrolledRowFromWidget(pt.y, this, WID_NS_AVAIL_LIST);
1081 this->active_sel = NULL;
1082 DeleteWindowByClass(WC_GRF_PARAMETERS);
1083 if (i < this->avails.Length()) {
1084 this->avail_sel = this->avails[i];
1085 this->avail_pos = i;
1087 this->InvalidateData();
1088 if (click_count == 1) {
1089 if (this->editable && this->avail_sel != NULL && !HasBit(this->avail_sel->flags, GCF_INVALID)) SetPointerMode (POINTER_DRAG, this, SPR_CURSOR_MOUSE);
1090 break;
1092 /* With double click, continue */
1094 FALLTHROUGH;
1096 case WID_NS_ADD:
1097 if (this->avail_sel == NULL || !this->editable || HasBit(this->avail_sel->flags, GCF_INVALID)) break;
1099 this->AddGRFToActive();
1100 break;
1102 case WID_NS_APPLY_CHANGES: // Apply changes made to GRF list
1103 if (!this->editable) break;
1104 if (this->execute) {
1105 ShowQuery(
1106 STR_NEWGRF_POPUP_CAUTION_CAPTION,
1107 STR_NEWGRF_CONFIRMATION_TEXT,
1108 this,
1109 NewGRFConfirmationCallback
1111 } else {
1112 CopyGRFConfigList(this->orig_list, this->actives, true);
1113 ResetGRFConfig(false);
1114 ReloadNewGRFData();
1116 this->DeleteChildWindows(WC_QUERY_STRING); // Remove the parameter query window
1117 break;
1119 case WID_NS_VIEW_PARAMETERS:
1120 case WID_NS_SET_PARAMETERS: { // Edit parameters
1121 if (this->active_sel == NULL || !this->show_params || this->active_sel->num_valid_params == 0) break;
1123 OpenGRFParameterWindow(this->active_sel, this->editable);
1124 break;
1127 case WID_NS_TOGGLE_PALETTE:
1128 if (this->active_sel != NULL && this->editable) {
1129 this->active_sel->palette ^= GRFP_USE_MASK;
1130 this->SetDirty();
1132 break;
1134 case WID_NS_CONTENT_DOWNLOAD:
1135 case WID_NS_CONTENT_DOWNLOAD2:
1136 if (!_network_available) {
1137 ShowErrorMessage(STR_NETWORK_ERROR_NOTAVAILABLE, INVALID_STRING_ID, WL_ERROR);
1138 } else {
1139 #if defined(ENABLE_NETWORK)
1140 this->DeleteChildWindows(WC_QUERY_STRING); // Remove the parameter query window
1142 ShowMissingContentWindow(this->actives);
1143 #endif
1145 break;
1147 case WID_NS_RESCAN_FILES:
1148 case WID_NS_RESCAN_FILES2:
1149 ScanNewGRFFiles(this);
1150 break;
1154 virtual void OnNewGRFsScanned()
1156 this->avail_sel = NULL;
1157 this->avail_pos = -1;
1158 this->avails.ForceRebuild();
1159 this->DeleteChildWindows(WC_QUERY_STRING); // Remove the parameter query window
1160 this->DeleteChildWindows(WC_TEXTFILE); // Remove the view textfile window
1163 virtual void OnDropdownSelect(int widget, int index)
1165 if (!this->editable) return;
1167 ClearGRFConfigList(&this->actives);
1168 this->preset = index;
1170 if (index != -1) {
1171 this->actives = LoadGRFPresetFromConfig(_grf_preset_list[index]);
1173 this->avails.ForceRebuild();
1175 ResetPointerMode();
1176 DeleteWindowByClass(WC_GRF_PARAMETERS);
1177 this->active_sel = NULL;
1178 this->InvalidateData(GOID_NEWGRF_PRESET_LOADED);
1181 virtual void OnQueryTextFinished(char *str)
1183 if (str == NULL) return;
1185 SaveGRFPresetToConfig(str, this->actives);
1186 GetGRFPresetList(&_grf_preset_list);
1188 /* Switch to this preset */
1189 for (uint i = 0; i < _grf_preset_list.Length(); i++) {
1190 if (_grf_preset_list[i] != NULL && strcmp(_grf_preset_list[i], str) == 0) {
1191 this->preset = i;
1192 break;
1196 this->InvalidateData();
1200 * Some data on this window has become invalid.
1201 * @param data Information about the changed data. @see GameOptionsInvalidationData
1202 * @param gui_scope Whether the call is done from GUI scope. You may not do everything when not in GUI scope. See #InvalidateWindowData() for details.
1204 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
1206 if (!gui_scope) return;
1207 switch (data) {
1208 default:
1209 /* Nothing important to do */
1210 break;
1212 case GOID_NEWGRF_RESCANNED:
1213 /* Search the list for items that are now found and mark them as such. */
1214 for (GRFConfig **l = &this->actives; *l != NULL; l = &(*l)->next) {
1215 GRFConfig *c = *l;
1216 bool compatible = HasBit(c->flags, GCF_COMPATIBLE);
1217 if (c->status != GCS_NOT_FOUND && !compatible) continue;
1219 const GRFConfig *f = FindGRFConfig(c->ident.grfid, FGCM_EXACT, compatible ? c->original_md5sum : c->ident.md5sum);
1220 if (f == NULL || HasBit(f->flags, GCF_INVALID)) continue;
1222 *l = new GRFConfig(*f);
1223 (*l)->next = c->next;
1225 if (active_sel == c) active_sel = *l;
1227 delete c;
1230 this->avails.ForceRebuild();
1231 FALLTHROUGH;
1233 case GOID_NEWGRF_LIST_EDITED:
1234 this->preset = -1;
1235 FALLTHROUGH;
1237 case GOID_NEWGRF_PRESET_LOADED: {
1238 /* Update scrollbars */
1239 int i = 0;
1240 for (const GRFConfig *c = this->actives; c != NULL; c = c->next, i++) {}
1242 this->vscroll->SetCount(i + 1); // Reserve empty space for drag and drop handling.
1244 if (this->avail_pos >= 0) this->vscroll2->ScrollTowards(this->avail_pos);
1245 break;
1249 this->BuildAvailables();
1251 this->SetWidgetsDisabledState(!this->editable,
1252 WID_NS_PRESET_LIST,
1253 WID_NS_APPLY_CHANGES,
1254 WID_NS_TOGGLE_PALETTE,
1255 WIDGET_LIST_END
1257 this->SetWidgetDisabledState(WID_NS_ADD, !this->editable || this->avail_sel == NULL || HasBit(this->avail_sel->flags, GCF_INVALID));
1258 this->SetWidgetDisabledState(WID_NS_UPGRADE, !this->editable || this->actives == NULL || !this->CanUpgradeCurrent());
1260 bool disable_all = this->active_sel == NULL || !this->editable;
1261 this->SetWidgetsDisabledState(disable_all,
1262 WID_NS_REMOVE,
1263 WID_NS_MOVE_UP,
1264 WID_NS_MOVE_DOWN,
1265 WIDGET_LIST_END
1268 const GRFConfig *c = (this->avail_sel == NULL) ? this->active_sel : this->avail_sel;
1269 for (TextfileType tft = TFT_BEGIN; tft < TFT_END; tft++) {
1270 this->SetWidgetDisabledState (WID_NS_NEWGRF_TEXTFILE + tft,
1271 c == NULL || !c->GetTextfile(tft).valid());
1273 this->SetWidgetDisabledState(WID_NS_OPEN_URL, c == NULL || StrEmpty(c->GetURL()));
1275 this->SetWidgetDisabledState(WID_NS_SET_PARAMETERS, !this->show_params || this->active_sel == NULL || this->active_sel->num_valid_params == 0);
1276 this->SetWidgetDisabledState(WID_NS_VIEW_PARAMETERS, !this->show_params || this->active_sel == NULL || this->active_sel->num_valid_params == 0);
1277 this->SetWidgetDisabledState(WID_NS_TOGGLE_PALETTE, disable_all ||
1278 (!(_settings_client.gui.newgrf_developer_tools || _settings_client.gui.scenario_developer) && ((c->palette & GRFP_GRF_MASK) != GRFP_GRF_UNSET)));
1280 if (!disable_all) {
1281 /* All widgets are now enabled, so disable widgets we can't use */
1282 if (this->active_sel == this->actives) this->DisableWidget(WID_NS_MOVE_UP);
1283 if (this->active_sel->next == NULL) this->DisableWidget(WID_NS_MOVE_DOWN);
1286 this->SetWidgetDisabledState(WID_NS_PRESET_DELETE, this->preset == -1);
1288 bool has_missing = false;
1289 bool has_compatible = false;
1290 for (const GRFConfig *c = this->actives; !has_missing && c != NULL; c = c->next) {
1291 has_missing |= c->status == GCS_NOT_FOUND;
1292 has_compatible |= HasBit(c->flags, GCF_COMPATIBLE);
1294 uint32 widget_data;
1295 StringID tool_tip;
1296 if (has_missing || has_compatible) {
1297 widget_data = STR_NEWGRF_SETTINGS_FIND_MISSING_CONTENT_BUTTON;
1298 tool_tip = STR_NEWGRF_SETTINGS_FIND_MISSING_CONTENT_TOOLTIP;
1299 } else {
1300 widget_data = STR_INTRO_ONLINE_CONTENT;
1301 tool_tip = STR_INTRO_TOOLTIP_ONLINE_CONTENT;
1303 this->GetWidget<NWidgetCore>(WID_NS_CONTENT_DOWNLOAD)->widget_data = widget_data;
1304 this->GetWidget<NWidgetCore>(WID_NS_CONTENT_DOWNLOAD)->tool_tip = tool_tip;
1305 this->GetWidget<NWidgetCore>(WID_NS_CONTENT_DOWNLOAD2)->widget_data = widget_data;
1306 this->GetWidget<NWidgetCore>(WID_NS_CONTENT_DOWNLOAD2)->tool_tip = tool_tip;
1308 this->SetWidgetDisabledState(WID_NS_PRESET_SAVE, has_missing);
1311 bool OnKeyPress (WChar key, uint16 keycode) OVERRIDE
1313 if (!this->editable) return false;
1315 switch (keycode) {
1316 case WKC_UP:
1317 /* scroll up by one */
1318 if (this->avail_pos > 0) this->avail_pos--;
1319 break;
1321 case WKC_DOWN:
1322 /* scroll down by one */
1323 if (this->avail_pos < (int)this->avails.Length() - 1) this->avail_pos++;
1324 break;
1326 case WKC_PAGEUP:
1327 /* scroll up a page */
1328 this->avail_pos = (this->avail_pos < this->vscroll2->GetCapacity()) ? 0 : this->avail_pos - this->vscroll2->GetCapacity();
1329 break;
1331 case WKC_PAGEDOWN:
1332 /* scroll down a page */
1333 this->avail_pos = min(this->avail_pos + this->vscroll2->GetCapacity(), (int)this->avails.Length() - 1);
1334 break;
1336 case WKC_HOME:
1337 /* jump to beginning */
1338 this->avail_pos = 0;
1339 break;
1341 case WKC_END:
1342 /* jump to end */
1343 this->avail_pos = this->avails.Length() - 1;
1344 break;
1346 default:
1347 return false;
1350 if (this->avails.Length() == 0) this->avail_pos = -1;
1351 if (this->avail_pos >= 0) {
1352 this->avail_sel = this->avails[this->avail_pos];
1353 this->vscroll2->ScrollTowards(this->avail_pos);
1354 this->InvalidateData(0);
1357 return true;
1360 virtual void OnEditboxChanged(int wid)
1362 if (!this->editable) return;
1364 string_filter.SetFilterTerm(this->filter_editbox.GetText());
1365 this->avails.SetFilterState(!string_filter.IsEmpty());
1366 this->avails.ForceRebuild();
1367 this->InvalidateData(0);
1370 virtual void OnDragDrop(Point pt, int widget)
1372 if (!this->editable) return;
1374 if (widget == WID_NS_FILE_LIST) {
1375 if (this->active_sel != NULL) {
1376 /* Get pointer to the selected file in the active list. */
1377 int from_pos = 0;
1378 GRFConfig **from_prev;
1379 for (from_prev = &this->actives; *from_prev != this->active_sel; from_prev = &(*from_prev)->next, from_pos++) {}
1381 /* Gets the drag-and-drop destination offset. Ignore the last dummy line. */
1382 int to_pos = min(this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_NS_FILE_LIST), this->vscroll->GetCount() - 2);
1383 if (to_pos != from_pos) { // Don't move NewGRF file over itself.
1384 /* Get pointer to destination position. */
1385 GRFConfig **to_prev = &this->actives;
1386 for (int i = from_pos < to_pos ? -1 : 0; *to_prev != NULL && i < to_pos; to_prev = &(*to_prev)->next, i++) {}
1388 /* Detach NewGRF file from its original position. */
1389 *from_prev = this->active_sel->next;
1391 /* Attach NewGRF file to its new position. */
1392 this->active_sel->next = *to_prev;
1393 *to_prev = this->active_sel;
1395 this->vscroll->ScrollTowards(to_pos);
1396 this->preset = -1;
1397 this->InvalidateData();
1399 } else if (this->avail_sel != NULL) {
1400 int to_pos = min(this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_NS_FILE_LIST), this->vscroll->GetCount() - 1);
1401 this->AddGRFToActive(to_pos);
1403 } else if (widget == WID_NS_AVAIL_LIST && this->active_sel != NULL) {
1404 /* Remove active NewGRF file by dragging it over available list. */
1405 Point dummy = {-1, -1};
1406 this->OnClick(dummy, WID_NS_REMOVE, 1);
1409 ResetPointerMode();
1411 if (this->active_over != -1) {
1412 /* End of drag-and-drop, hide dragged destination highlight. */
1413 this->SetWidgetDirty(this->active_over == -2 ? WID_NS_AVAIL_LIST : WID_NS_FILE_LIST);
1414 this->active_over = -1;
1418 virtual void OnMouseDrag(Point pt, int widget)
1420 if (!this->editable) return;
1422 if (widget == WID_NS_FILE_LIST && (this->active_sel != NULL || this->avail_sel != NULL)) {
1423 /* An NewGRF file is dragged over the active list. */
1424 int to_pos = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_NS_FILE_LIST);
1425 /* Skip the last dummy line if the source is from the active list. */
1426 to_pos = min(to_pos, this->vscroll->GetCount() - (this->active_sel != NULL ? 2 : 1));
1428 if (to_pos != this->active_over) {
1429 this->active_over = to_pos;
1430 this->SetWidgetDirty(WID_NS_FILE_LIST);
1432 } else if (widget == WID_NS_AVAIL_LIST && this->active_sel != NULL) {
1433 this->active_over = -2;
1434 this->SetWidgetDirty(WID_NS_AVAIL_LIST);
1435 } else if (this->active_over != -1) {
1436 this->SetWidgetDirty(this->active_over == -2 ? WID_NS_AVAIL_LIST : WID_NS_FILE_LIST);
1437 this->active_over = -1;
1441 private:
1442 /** Sort grfs by name. */
1443 static int CDECL NameSorter(const GRFConfig * const *a, const GRFConfig * const *b)
1445 int i = strnatcmp((*a)->GetName(), (*b)->GetName(), true); // Sort by name (natural sorting).
1446 if (i != 0) return i;
1448 i = (*a)->version - (*b)->version;
1449 if (i != 0) return i;
1451 return memcmp((*a)->ident.md5sum, (*b)->ident.md5sum, lengthof((*b)->ident.md5sum));
1454 void BuildAvailables()
1456 if (!this->avails.NeedRebuild()) return;
1458 this->avails.Clear();
1460 for (const GRFConfig *c = _all_grfs; c != NULL; c = c->next) {
1461 bool found = false;
1462 for (const GRFConfig *grf = this->actives; grf != NULL && !found; grf = grf->next) found = grf->ident.matches (c->ident);
1463 if (found) continue;
1465 if (!_settings_client.gui.newgrf_show_old_versions) {
1466 const GRFConfig *best = FindGRFConfig(c->ident.grfid, HasBit(c->flags, GCF_INVALID) ? FGCM_NEWEST : FGCM_NEWEST_VALID);
1468 * If the best version is 0, then all NewGRF with this GRF ID
1469 * have version 0, so for backward compatibility reasons we
1470 * want to show them all.
1471 * If we are the best version, then we definitely want to
1472 * show that NewGRF!.
1474 if (best->version != 0 && !best->ident.matches (c->ident)) {
1475 continue;
1479 this->string_filter.ResetState();
1480 this->string_filter.AddLine (c->GetName());
1481 this->string_filter.AddLine (c->filename);
1482 this->string_filter.AddLine (c->GetDescription());
1483 if (this->string_filter.GetState()) {
1484 *this->avails.Append() = c;
1488 this->avails.Compact();
1489 this->avails.RebuildDone();
1490 this->avails.Sort();
1492 if (this->avail_sel != NULL) {
1493 this->avail_pos = this->avails.FindIndex(this->avail_sel);
1494 if (this->avail_pos < 0) this->avail_sel = NULL;
1497 this->vscroll2->SetCount(this->avails.Length()); // Update the scrollbar
1501 * Insert a GRF into the active list.
1502 * @param ins_pos Insert GRF at this position.
1503 * @return True if the GRF was successfully added.
1505 bool AddGRFToActive(int ins_pos = -1)
1507 if (this->avail_sel == NULL || !this->editable || HasBit(this->avail_sel->flags, GCF_INVALID)) return false;
1509 uint count = 0;
1510 GRFConfig **entry = NULL;
1511 GRFConfig **list;
1512 /* Find last entry in the list, checking for duplicate grfid on the way */
1513 for (list = &this->actives; *list != NULL; list = &(*list)->next, ins_pos--) {
1514 if (ins_pos == 0) entry = list; // Insert position? Save.
1515 if ((*list)->ident.grfid == this->avail_sel->ident.grfid) {
1516 ShowErrorMessage(STR_NEWGRF_DUPLICATE_GRFID, INVALID_STRING_ID, WL_INFO);
1517 return false;
1519 if (!HasBit((*list)->flags, GCF_STATIC)) count++;
1521 if (entry == NULL) entry = list;
1522 if (count >= NETWORK_MAX_GRF_COUNT) {
1523 ShowErrorMessage(STR_NEWGRF_TOO_MANY_NEWGRFS, INVALID_STRING_ID, WL_INFO);
1524 return false;
1527 GRFConfig *c = new GRFConfig(*this->avail_sel); // Copy GRF details from scanned list.
1528 c->SetParameterDefaults();
1530 /* Insert GRF config to configuration list. */
1531 c->next = *entry;
1532 *entry = c;
1534 /* Select next (or previous, if last one) item in the list. */
1535 int new_pos = this->avail_pos + 1;
1536 if (new_pos >= (int)this->avails.Length()) new_pos = this->avail_pos - 1;
1537 this->avail_pos = new_pos;
1538 if (new_pos >= 0) this->avail_sel = this->avails[new_pos];
1540 this->avails.ForceRebuild();
1541 this->InvalidateData(GOID_NEWGRF_LIST_EDITED);
1542 return true;
1546 #if defined(ENABLE_NETWORK)
1548 * Show the content list window with all missing grfs from the given list.
1549 * @param list The list of grfs to check for missing / not exactly matching ones.
1551 void ShowMissingContentWindow(const GRFConfig *list)
1553 /* Only show the things in the current list, or everything when nothing's selected */
1554 ContentVector cv;
1555 for (const GRFConfig *c = list; c != NULL; c = c->next) {
1556 if (c->status != GCS_NOT_FOUND && !HasBit(c->flags, GCF_COMPATIBLE)) continue;
1558 ContentInfo *ci = new ContentInfo();
1559 ci->type = CONTENT_TYPE_NEWGRF;
1560 ci->state = ContentInfo::DOES_NOT_EXIST;
1561 bstrcpy (ci->name, c->GetName());
1562 ci->unique_id = BSWAP32(c->ident.grfid);
1563 memcpy(ci->md5sum, HasBit(c->flags, GCF_COMPATIBLE) ? c->original_md5sum : c->ident.md5sum, sizeof(ci->md5sum));
1564 *cv.Append() = ci;
1566 ShowNetworkContentListWindow(cv.Length() == 0 ? NULL : &cv, CONTENT_TYPE_NEWGRF);
1568 #endif
1570 Listing NewGRFWindow::last_sorting = {false, 0};
1572 NewGRFWindow::GUIGRFConfigList::SortFunction * const NewGRFWindow::sorter_funcs[] = {
1573 &NameSorter,
1577 * Custom nested widget container for the NewGRF gui.
1578 * Depending on the space in the gui, it uses either
1579 * - two column mode, put the #acs and the #avs underneath each other and the #info next to it, or
1580 * - three column mode, put the #avs, #acs, and #info each in its own column.
1582 class NWidgetNewGRFDisplay : public NWidgetContainer {
1583 public:
1584 static const uint INTER_LIST_SPACING; ///< Empty vertical space between both lists in the 2 column mode.
1585 static const uint INTER_COLUMN_SPACING; ///< Empty horizontal space between two columns.
1586 static const uint MAX_EXTRA_INFO_WIDTH; ///< Maximal additional width given to the panel.
1587 static const uint MIN_EXTRA_FOR_3_COLUMNS; ///< Minimal additional width needed before switching to 3 columns.
1589 NWidgetBase *avs; ///< Widget with the available grfs list and buttons.
1590 NWidgetBase *acs; ///< Widget with the active grfs list and buttons.
1591 NWidgetBase *inf; ///< Info panel.
1592 bool editable; ///< Editable status of the parent NewGRF window (if \c false, drop all widgets that make the window editable).
1594 NWidgetNewGRFDisplay(NWidgetBase *avs, NWidgetBase *acs, NWidgetBase *inf) : NWidgetContainer(NWID_HORIZONTAL)
1596 this->avs = avs;
1597 this->acs = acs;
1598 this->inf = inf;
1600 this->Add(this->avs);
1601 this->Add(this->acs);
1602 this->Add(this->inf);
1604 this->editable = true; // Temporary setting, 'real' value is set in SetupSmallestSize().
1607 virtual void SetupSmallestSize(Window *w, bool init_array)
1609 /* Copy state flag from the window. */
1610 assert(dynamic_cast<NewGRFWindow *>(w) != NULL);
1611 NewGRFWindow *ngw = (NewGRFWindow *)w;
1612 this->editable = ngw->editable;
1614 this->avs->SetupSmallestSize(w, init_array);
1615 this->acs->SetupSmallestSize(w, init_array);
1616 this->inf->SetupSmallestSize(w, init_array);
1618 uint min_avs_width = this->avs->smallest_x + this->avs->padding_left + this->avs->padding_right;
1619 uint min_acs_width = this->acs->smallest_x + this->acs->padding_left + this->acs->padding_right;
1620 uint min_inf_width = this->inf->smallest_x + this->inf->padding_left + this->inf->padding_right;
1622 uint min_avs_height = this->avs->smallest_y + this->avs->padding_top + this->avs->padding_bottom;
1623 uint min_acs_height = this->acs->smallest_y + this->acs->padding_top + this->acs->padding_bottom;
1624 uint min_inf_height = this->inf->smallest_y + this->inf->padding_top + this->inf->padding_bottom;
1626 /* Smallest window is in two column mode. */
1627 this->smallest_x = max(min_avs_width, min_acs_width) + INTER_COLUMN_SPACING + min_inf_width;
1628 this->smallest_y = max(min_inf_height, min_acs_height + INTER_LIST_SPACING + min_avs_height);
1630 /* Filling. */
1631 this->fill_x = LeastCommonMultiple(this->avs->fill_x, this->acs->fill_x);
1632 if (this->inf->fill_x > 0 && (this->fill_x == 0 || this->fill_x > this->inf->fill_x)) this->fill_x = this->inf->fill_x;
1634 this->fill_y = this->avs->fill_y;
1635 if (this->acs->fill_y > 0 && (this->fill_y == 0 || this->fill_y > this->acs->fill_y)) this->fill_y = this->acs->fill_y;
1636 this->fill_y = LeastCommonMultiple(this->fill_y, this->inf->fill_y);
1638 /* Resizing. */
1639 this->resize_x = LeastCommonMultiple(this->avs->resize_x, this->acs->resize_x);
1640 if (this->inf->resize_x > 0 && (this->resize_x == 0 || this->resize_x > this->inf->resize_x)) this->resize_x = this->inf->resize_x;
1642 this->resize_y = this->avs->resize_y;
1643 if (this->acs->resize_y > 0 && (this->resize_y == 0 || this->resize_y > this->acs->resize_y)) this->resize_y = this->acs->resize_y;
1644 this->resize_y = LeastCommonMultiple(this->resize_y, this->inf->resize_y);
1646 /* Make sure the height suits the 3 column (resp. not-editable) format; the 2 column format can easily fill space between the lists */
1647 this->smallest_y = ComputeMaxSize(min_acs_height, this->smallest_y + this->resize_y - 1, this->resize_y);
1650 virtual void AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)
1652 this->StoreSizePosition(sizing, x, y, given_width, given_height);
1654 uint min_avs_width = this->avs->smallest_x + this->avs->padding_left + this->avs->padding_right;
1655 uint min_acs_width = this->acs->smallest_x + this->acs->padding_left + this->acs->padding_right;
1656 uint min_inf_width = this->inf->smallest_x + this->inf->padding_left + this->inf->padding_right;
1658 uint min_list_width = max(min_avs_width, min_acs_width); // Smallest width of the lists such that they have equal width (incl padding).
1659 uint avs_extra_width = min_list_width - min_avs_width; // Additional width needed for avs to reach min_list_width.
1660 uint acs_extra_width = min_list_width - min_acs_width; // Additional width needed for acs to reach min_list_width.
1662 /* Use 2 or 3 columns? */
1663 uint min_three_columns = min_avs_width + min_acs_width + min_inf_width + 2 * INTER_COLUMN_SPACING;
1664 uint min_two_columns = min_list_width + min_inf_width + INTER_COLUMN_SPACING;
1665 bool use_three_columns = this->editable && (min_three_columns + MIN_EXTRA_FOR_3_COLUMNS <= given_width);
1667 /* Info panel is a separate column in both modes. Compute its width first. */
1668 uint extra_width, inf_width;
1669 if (use_three_columns) {
1670 extra_width = given_width - min_three_columns;
1671 inf_width = min(MAX_EXTRA_INFO_WIDTH, extra_width / 2);
1672 } else {
1673 extra_width = given_width - min_two_columns;
1674 inf_width = min(MAX_EXTRA_INFO_WIDTH, extra_width / 2);
1676 inf_width = ComputeMaxSize(this->inf->smallest_x, this->inf->smallest_x + inf_width, this->inf->GetHorizontalStepSize(sizing));
1677 extra_width -= inf_width - this->inf->smallest_x;
1679 uint inf_height = ComputeMaxSize(this->inf->smallest_y, given_height, this->inf->GetVerticalStepSize(sizing));
1681 if (use_three_columns) {
1682 /* Three column display, first make both lists equally wide, then divide whatever is left between both lists.
1683 * Only keep track of what avs gets, all other space goes to acs. */
1684 uint avs_width = min(avs_extra_width, extra_width);
1685 extra_width -= avs_width;
1686 extra_width -= min(acs_extra_width, extra_width);
1687 avs_width += extra_width / 2;
1689 avs_width = ComputeMaxSize(this->avs->smallest_x, this->avs->smallest_x + avs_width, this->avs->GetHorizontalStepSize(sizing));
1691 uint acs_width = given_width - // Remaining space, including horizontal padding.
1692 inf_width - this->inf->padding_left - this->inf->padding_right -
1693 avs_width - this->avs->padding_left - this->avs->padding_right - 2 * INTER_COLUMN_SPACING;
1694 acs_width = ComputeMaxSize(min_acs_width, acs_width, this->acs->GetHorizontalStepSize(sizing)) -
1695 this->acs->padding_left - this->acs->padding_right;
1697 /* Never use fill_y on these; the minimal size is chosen, so that the 3 column view looks nice */
1698 uint avs_height = ComputeMaxSize(this->avs->smallest_y, given_height, this->avs->resize_y);
1699 uint acs_height = ComputeMaxSize(this->acs->smallest_y, given_height, this->acs->resize_y);
1701 /* Assign size and position to the children. */
1702 if (rtl) {
1703 x += this->inf->padding_left;
1704 this->inf->AssignSizePosition(sizing, x, y + this->inf->padding_top, inf_width, inf_height, rtl);
1705 x += inf_width + this->inf->padding_right + INTER_COLUMN_SPACING;
1706 } else {
1707 x += this->avs->padding_left;
1708 this->avs->AssignSizePosition(sizing, x, y + this->avs->padding_top, avs_width, avs_height, rtl);
1709 x += avs_width + this->avs->padding_right + INTER_COLUMN_SPACING;
1712 x += this->acs->padding_left;
1713 this->acs->AssignSizePosition(sizing, x, y + this->acs->padding_top, acs_width, acs_height, rtl);
1714 x += acs_width + this->acs->padding_right + INTER_COLUMN_SPACING;
1716 if (rtl) {
1717 x += this->avs->padding_left;
1718 this->avs->AssignSizePosition(sizing, x, y + this->avs->padding_top, avs_width, avs_height, rtl);
1719 } else {
1720 x += this->inf->padding_left;
1721 this->inf->AssignSizePosition(sizing, x, y + this->inf->padding_top, inf_width, inf_height, rtl);
1723 } else {
1724 /* Two columns, all space in extra_width goes to both lists. Since the lists are underneath each other,
1725 * the column is min_list_width wide at least. */
1726 uint avs_width = ComputeMaxSize(this->avs->smallest_x, this->avs->smallest_x + avs_extra_width + extra_width,
1727 this->avs->GetHorizontalStepSize(sizing));
1728 uint acs_width = ComputeMaxSize(this->acs->smallest_x, this->acs->smallest_x + acs_extra_width + extra_width,
1729 this->acs->GetHorizontalStepSize(sizing));
1731 uint min_avs_height = (!this->editable) ? 0 : this->avs->smallest_y + this->avs->padding_top + this->avs->padding_bottom + INTER_LIST_SPACING;
1732 uint min_acs_height = this->acs->smallest_y + this->acs->padding_top + this->acs->padding_bottom;
1733 uint extra_height = given_height - min_acs_height - min_avs_height;
1735 /* Never use fill_y on these; instead use the INTER_LIST_SPACING as filler */
1736 uint avs_height = ComputeMaxSize(this->avs->smallest_y, this->avs->smallest_y + extra_height / 2, this->avs->resize_y);
1737 if (this->editable) extra_height -= avs_height - this->avs->smallest_y;
1738 uint acs_height = ComputeMaxSize(this->acs->smallest_y, this->acs->smallest_y + extra_height, this->acs->resize_y);
1740 /* Assign size and position to the children. */
1741 if (rtl) {
1742 x += this->inf->padding_left;
1743 this->inf->AssignSizePosition(sizing, x, y + this->inf->padding_top, inf_width, inf_height, rtl);
1744 x += inf_width + this->inf->padding_right + INTER_COLUMN_SPACING;
1746 this->acs->AssignSizePosition(sizing, x + this->acs->padding_left, y + this->acs->padding_top, acs_width, acs_height, rtl);
1747 if (this->editable) {
1748 this->avs->AssignSizePosition(sizing, x + this->avs->padding_left, y + given_height - avs_height - this->avs->padding_bottom, avs_width, avs_height, rtl);
1749 } else {
1750 this->avs->AssignSizePosition(sizing, 0, 0, this->avs->smallest_x, this->avs->smallest_y, rtl);
1752 } else {
1753 this->acs->AssignSizePosition(sizing, x + this->acs->padding_left, y + this->acs->padding_top, acs_width, acs_height, rtl);
1754 if (this->editable) {
1755 this->avs->AssignSizePosition(sizing, x + this->avs->padding_left, y + given_height - avs_height - this->avs->padding_bottom, avs_width, avs_height, rtl);
1756 } else {
1757 this->avs->AssignSizePosition(sizing, 0, 0, this->avs->smallest_x, this->avs->smallest_y, rtl);
1759 uint dx = this->acs->current_x + this->acs->padding_left + this->acs->padding_right;
1760 if (this->editable) {
1761 dx = max(dx, this->avs->current_x + this->avs->padding_left + this->avs->padding_right);
1763 x += dx + INTER_COLUMN_SPACING + this->inf->padding_left;
1764 this->inf->AssignSizePosition(sizing, x, y + this->inf->padding_top, inf_width, inf_height, rtl);
1769 virtual NWidgetCore *GetWidgetFromPos(int x, int y)
1771 if (!IsInsideBS(x, this->pos_x, this->current_x) || !IsInsideBS(y, this->pos_y, this->current_y)) return NULL;
1773 NWidgetCore *nw = (this->editable) ? this->avs->GetWidgetFromPos(x, y) : NULL;
1774 if (nw == NULL) nw = this->acs->GetWidgetFromPos(x, y);
1775 if (nw == NULL) nw = this->inf->GetWidgetFromPos(x, y);
1776 return nw;
1779 void Draw (BlitArea *dpi, const Window *w) OVERRIDE
1781 if (this->editable) this->avs->Draw (dpi, w);
1782 this->acs->Draw (dpi, w);
1783 this->inf->Draw (dpi, w);
1787 const uint NWidgetNewGRFDisplay::INTER_LIST_SPACING = WD_RESIZEBOX_WIDTH + 1;
1788 const uint NWidgetNewGRFDisplay::INTER_COLUMN_SPACING = WD_RESIZEBOX_WIDTH;
1789 const uint NWidgetNewGRFDisplay::MAX_EXTRA_INFO_WIDTH = 150;
1790 const uint NWidgetNewGRFDisplay::MIN_EXTRA_FOR_3_COLUMNS = 50;
1792 static const NWidgetPart _nested_newgrf_actives_widgets[] = {
1793 /* Left side, presets. */
1794 NWidget(NWID_HORIZONTAL),
1795 NWidget(WWT_TEXT, COLOUR_MAUVE), SetDataTip(STR_NEWGRF_SETTINGS_SELECT_PRESET, STR_NULL),
1796 SetPadding(0, WD_FRAMETEXT_RIGHT, 0, 0),
1797 NWidget(WWT_DROPDOWN, COLOUR_YELLOW, WID_NS_PRESET_LIST), SetFill(1, 0), SetResize(1, 0),
1798 SetDataTip(STR_JUST_STRING, STR_NEWGRF_SETTINGS_PRESET_LIST_TOOLTIP),
1799 EndContainer(),
1800 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
1801 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_PRESET_SAVE), SetFill(1, 0), SetResize(1, 0),
1802 SetDataTip(STR_NEWGRF_SETTINGS_PRESET_SAVE, STR_NEWGRF_SETTINGS_PRESET_SAVE_TOOLTIP),
1803 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_PRESET_DELETE), SetFill(1, 0), SetResize(1, 0),
1804 SetDataTip(STR_NEWGRF_SETTINGS_PRESET_DELETE, STR_NEWGRF_SETTINGS_PRESET_DELETE_TOOLTIP),
1805 EndContainer(),
1807 NWidget(NWID_SPACER), SetMinimalSize(0, WD_RESIZEBOX_WIDTH), SetResize(1, 0), SetFill(1, 0),
1808 NWidget(WWT_PANEL, COLOUR_MAUVE),
1809 NWidget(WWT_LABEL, COLOUR_MAUVE), SetDataTip(STR_NEWGRF_SETTINGS_ACTIVE_LIST, STR_NULL),
1810 SetFill(1, 0), SetResize(1, 0), SetPadding(3, WD_FRAMETEXT_RIGHT, 0, WD_FRAMETEXT_LEFT),
1811 /* Left side, active grfs. */
1812 NWidget(NWID_HORIZONTAL), SetPadding(0, 2, 0, 2),
1813 NWidget(WWT_PANEL, COLOUR_MAUVE),
1814 NWidget(WWT_INSET, COLOUR_MAUVE, WID_NS_FILE_LIST), SetMinimalSize(100, 1), SetPadding(2, 2, 2, 2),
1815 SetFill(1, 1), SetResize(1, 1), SetScrollbar(WID_NS_SCROLLBAR), SetDataTip(STR_NULL, STR_NEWGRF_SETTINGS_FILE_TOOLTIP),
1816 EndContainer(),
1817 EndContainer(),
1818 NWidget(NWID_VSCROLLBAR, COLOUR_MAUVE, WID_NS_SCROLLBAR),
1819 EndContainer(),
1820 /* Buttons. */
1821 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_NS_SHOW_REMOVE),
1822 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPadding(2, 2, 2, 2), SetPIP(0, WD_RESIZEBOX_WIDTH, 0),
1823 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_REMOVE), SetFill(1, 0), SetResize(1, 0),
1824 SetDataTip(STR_NEWGRF_SETTINGS_REMOVE, STR_NEWGRF_SETTINGS_REMOVE_TOOLTIP),
1825 NWidget(NWID_VERTICAL),
1826 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_MOVE_UP), SetFill(1, 0), SetResize(1, 0),
1827 SetDataTip(STR_NEWGRF_SETTINGS_MOVEUP, STR_NEWGRF_SETTINGS_MOVEUP_TOOLTIP),
1828 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_MOVE_DOWN), SetFill(1, 0), SetResize(1, 0),
1829 SetDataTip(STR_NEWGRF_SETTINGS_MOVEDOWN, STR_NEWGRF_SETTINGS_MOVEDOWN_TOOLTIP),
1830 EndContainer(),
1831 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_UPGRADE), SetFill(1, 0), SetResize(1, 0),
1832 SetDataTip(STR_NEWGRF_SETTINGS_UPGRADE, STR_NEWGRF_SETTINGS_UPGRADE_TOOLTIP),
1833 EndContainer(),
1835 NWidget(NWID_VERTICAL, NC_EQUALSIZE), SetPadding(2, 2, 2, 2),
1836 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_RESCAN_FILES2), SetFill(1, 0), SetResize(1, 0),
1837 SetDataTip(STR_NEWGRF_SETTINGS_RESCAN_FILES, STR_NEWGRF_SETTINGS_RESCAN_FILES_TOOLTIP),
1838 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_CONTENT_DOWNLOAD2), SetFill(1, 0), SetResize(1, 0),
1839 SetDataTip(STR_INTRO_ONLINE_CONTENT, STR_INTRO_TOOLTIP_ONLINE_CONTENT),
1840 EndContainer(),
1841 EndContainer(),
1842 EndContainer(),
1845 static const NWidgetPart _nested_newgrf_availables_widgets[] = {
1846 NWidget(WWT_PANEL, COLOUR_MAUVE),
1847 NWidget(WWT_LABEL, COLOUR_MAUVE), SetDataTip(STR_NEWGRF_SETTINGS_INACTIVE_LIST, STR_NULL),
1848 SetFill(1, 0), SetResize(1, 0), SetPadding(3, WD_FRAMETEXT_RIGHT, 0, WD_FRAMETEXT_LEFT),
1849 /* Left side, available grfs, filter edit box. */
1850 NWidget(NWID_HORIZONTAL), SetPadding(WD_TEXTPANEL_TOP, 0, WD_TEXTPANEL_BOTTOM, 0),
1851 SetPIP(WD_FRAMETEXT_LEFT, WD_FRAMETEXT_RIGHT, WD_FRAMETEXT_RIGHT),
1852 NWidget(WWT_TEXT, COLOUR_MAUVE), SetFill(0, 1), SetDataTip(STR_NEWGRF_FILTER_TITLE, STR_NULL),
1853 NWidget(WWT_EDITBOX, COLOUR_MAUVE, WID_NS_FILTER), SetFill(1, 0), SetMinimalSize(50, 12), SetResize(1, 0),
1854 SetDataTip(STR_LIST_FILTER_OSKTITLE, STR_LIST_FILTER_TOOLTIP),
1855 EndContainer(),
1856 /* Left side, available grfs. */
1857 NWidget(NWID_HORIZONTAL), SetPadding(0, 2, 0, 2),
1858 NWidget(WWT_PANEL, COLOUR_MAUVE),
1859 NWidget(WWT_INSET, COLOUR_MAUVE, WID_NS_AVAIL_LIST), SetMinimalSize(100, 1), SetPadding(2, 2, 2, 2),
1860 SetFill(1, 1), SetResize(1, 1), SetScrollbar(WID_NS_SCROLL2BAR),
1861 EndContainer(),
1862 EndContainer(),
1863 NWidget(NWID_VSCROLLBAR, COLOUR_MAUVE, WID_NS_SCROLL2BAR),
1864 EndContainer(),
1865 /* Left side, available grfs, buttons. */
1866 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPadding(2, 2, 2, 2), SetPIP(0, WD_RESIZEBOX_WIDTH, 0),
1867 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_ADD), SetFill(1, 0), SetResize(1, 0),
1868 SetDataTip(STR_NEWGRF_SETTINGS_ADD, STR_NEWGRF_SETTINGS_ADD_FILE_TOOLTIP),
1869 NWidget(NWID_VERTICAL),
1870 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_RESCAN_FILES), SetFill(1, 0), SetResize(1, 0),
1871 SetDataTip(STR_NEWGRF_SETTINGS_RESCAN_FILES, STR_NEWGRF_SETTINGS_RESCAN_FILES_TOOLTIP),
1872 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_CONTENT_DOWNLOAD), SetFill(1, 0), SetResize(1, 0),
1873 SetDataTip(STR_INTRO_ONLINE_CONTENT, STR_INTRO_TOOLTIP_ONLINE_CONTENT),
1874 EndContainer(),
1875 EndContainer(),
1876 EndContainer(),
1879 static const NWidgetPart _nested_newgrf_infopanel_widgets[] = {
1880 /* Right side, info panel. */
1881 NWidget(NWID_VERTICAL), SetPadding(0, 0, 2, 0),
1882 NWidget(WWT_PANEL, COLOUR_MAUVE), SetPadding(0, 0, 2, 0),
1883 NWidget(WWT_EMPTY, COLOUR_MAUVE, WID_NS_NEWGRF_INFO_TITLE), SetFill(1, 0), SetResize(1, 0),
1884 NWidget(WWT_EMPTY, COLOUR_MAUVE, WID_NS_NEWGRF_INFO), SetFill(1, 1), SetResize(1, 1), SetMinimalSize(150, 100),
1885 EndContainer(),
1886 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_OPEN_URL), SetFill(1, 0), SetResize(1, 0),
1887 SetDataTip(STR_CONTENT_OPEN_URL, STR_CONTENT_OPEN_URL_TOOLTIP),
1888 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_NEWGRF_TEXTFILE + TFT_README), SetFill(1, 0), SetResize(1, 0),
1889 SetDataTip(STR_TEXTFILE_VIEW_README, STR_NULL),
1890 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
1891 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_NEWGRF_TEXTFILE + TFT_CHANGELOG), SetFill(1, 0), SetResize(1, 0),
1892 SetDataTip(STR_TEXTFILE_VIEW_CHANGELOG, STR_NULL),
1893 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_NEWGRF_TEXTFILE + TFT_LICENSE), SetFill(1, 0), SetResize(1, 0),
1894 SetDataTip(STR_TEXTFILE_VIEW_LICENCE, STR_NULL),
1895 EndContainer(),
1896 EndContainer(),
1897 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_NS_SHOW_APPLY),
1898 /* Right side, buttons. */
1899 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(0, WD_RESIZEBOX_WIDTH, 0),
1900 NWidget(NWID_VERTICAL),
1901 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_SET_PARAMETERS), SetFill(1, 0), SetResize(1, 0),
1902 SetDataTip(STR_NEWGRF_SETTINGS_SET_PARAMETERS, STR_NULL),
1903 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_TOGGLE_PALETTE), SetFill(1, 0), SetResize(1, 0),
1904 SetDataTip(STR_NEWGRF_SETTINGS_TOGGLE_PALETTE, STR_NEWGRF_SETTINGS_TOGGLE_PALETTE_TOOLTIP),
1905 EndContainer(),
1906 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_APPLY_CHANGES), SetFill(1, 0), SetResize(1, 0),
1907 SetDataTip(STR_NEWGRF_SETTINGS_APPLY_CHANGES, STR_NULL),
1908 EndContainer(),
1909 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_VIEW_PARAMETERS), SetFill(1, 0), SetResize(1, 0),
1910 SetDataTip(STR_NEWGRF_SETTINGS_SHOW_PARAMETERS, STR_NULL),
1911 EndContainer(),
1914 /** Construct nested container widget for managing the lists and the info panel of the NewGRF GUI. */
1915 NWidgetBase* NewGRFDisplay(int *biggest_index)
1917 NWidgetBase *avs = MakeNWidgets(_nested_newgrf_availables_widgets, lengthof(_nested_newgrf_availables_widgets), biggest_index, NULL);
1919 int biggest2;
1920 NWidgetBase *acs = MakeNWidgets(_nested_newgrf_actives_widgets, lengthof(_nested_newgrf_actives_widgets), &biggest2, NULL);
1921 *biggest_index = max(*biggest_index, biggest2);
1923 NWidgetBase *inf = MakeNWidgets(_nested_newgrf_infopanel_widgets, lengthof(_nested_newgrf_infopanel_widgets), &biggest2, NULL);
1924 *biggest_index = max(*biggest_index, biggest2);
1926 return new NWidgetNewGRFDisplay(avs, acs, inf);
1929 /* Widget definition of the manage newgrfs window */
1930 static const NWidgetPart _nested_newgrf_widgets[] = {
1931 NWidget(NWID_HORIZONTAL),
1932 NWidget(WWT_CLOSEBOX, COLOUR_MAUVE),
1933 NWidget(WWT_CAPTION, COLOUR_MAUVE), SetDataTip(STR_NEWGRF_SETTINGS_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
1934 NWidget(WWT_DEFSIZEBOX, COLOUR_MAUVE),
1935 EndContainer(),
1936 NWidget(WWT_PANEL, COLOUR_MAUVE),
1937 NWidgetFunction(NewGRFDisplay), SetPadding(WD_RESIZEBOX_WIDTH, WD_RESIZEBOX_WIDTH, 2, WD_RESIZEBOX_WIDTH),
1938 /* Resize button. */
1939 NWidget(NWID_HORIZONTAL),
1940 NWidget(NWID_SPACER), SetFill(1, 0), SetResize(1, 0),
1941 NWidget(WWT_RESIZEBOX, COLOUR_MAUVE),
1942 EndContainer(),
1943 EndContainer(),
1946 /* Window preferences of the manage newgrfs window */
1947 static WindowDesc::Prefs _newgrf_prefs ("settings_newgrf");
1949 /* Window definition of the manage newgrfs window */
1950 static const WindowDesc _newgrf_desc(
1951 WDP_CENTER, 300, 263,
1952 WC_GAME_OPTIONS, WC_NONE,
1954 _nested_newgrf_widgets, lengthof(_nested_newgrf_widgets),
1955 &_newgrf_prefs
1959 * Callback function for the newgrf 'apply changes' confirmation window
1960 * @param w Window which is calling this callback
1961 * @param confirmed boolean value, true when yes was clicked, false otherwise
1963 static void NewGRFConfirmationCallback(Window *w, bool confirmed)
1965 if (confirmed) {
1966 DeleteWindowByClass(WC_GRF_PARAMETERS);
1967 NewGRFWindow *nw = dynamic_cast<NewGRFWindow*>(w);
1969 GamelogGRFBegin();
1970 GamelogGRFUpdate(_grfconfig, nw->actives); // log GRF changes
1971 CopyGRFConfigList(nw->orig_list, nw->actives, false);
1972 ReloadNewGRFData();
1973 GamelogGRFEnd();
1975 /* Show new, updated list */
1976 GRFConfig *c;
1977 int i = 0;
1978 for (c = nw->actives; c != NULL && c != nw->active_sel; c = c->next, i++) {}
1979 CopyGRFConfigList(&nw->actives, *nw->orig_list, false);
1980 for (c = nw->actives; c != NULL && i > 0; c = c->next, i--) {}
1981 nw->active_sel = c;
1982 nw->avails.ForceRebuild();
1984 w->InvalidateData();
1986 ReInitAllWindows();
1987 DeleteWindowByClass(WC_BUILD_OBJECT);
1994 * Setup the NewGRF gui
1995 * @param editable allow the user to make changes to the grfconfig in the window
1996 * @param show_params show information about what parameters are set for the grf files
1997 * @param exec_changes if changes are made to the list (editable is true), apply these
1998 * changes immediately or only update the list
1999 * @param config pointer to a linked-list of grfconfig's that will be shown
2001 void ShowNewGRFSettings(bool editable, bool show_params, bool exec_changes, GRFConfig **config)
2003 DeleteWindowByClass(WC_GAME_OPTIONS);
2004 new NewGRFWindow(&_newgrf_desc, editable, show_params, exec_changes, config);
2007 /** Widget parts of the save preset window. */
2008 static const NWidgetPart _nested_save_preset_widgets[] = {
2009 NWidget(NWID_HORIZONTAL),
2010 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
2011 NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_SAVE_PRESET_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
2012 NWidget(WWT_DEFSIZEBOX, COLOUR_GREY),
2013 EndContainer(),
2014 NWidget(WWT_PANEL, COLOUR_GREY),
2015 NWidget(NWID_HORIZONTAL),
2016 NWidget(WWT_INSET, COLOUR_GREY, WID_SVP_PRESET_LIST), SetPadding(2, 1, 0, 2),
2017 SetDataTip(0x0, STR_SAVE_PRESET_LIST_TOOLTIP), SetResize(1, 10), SetScrollbar(WID_SVP_SCROLLBAR), EndContainer(),
2018 NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_SVP_SCROLLBAR),
2019 EndContainer(),
2020 NWidget(WWT_EDITBOX, COLOUR_GREY, WID_SVP_EDITBOX), SetPadding(3, 2, 2, 2), SetFill(1, 0), SetResize(1, 0),
2021 SetDataTip(STR_SAVE_PRESET_TITLE, STR_SAVE_PRESET_EDITBOX_TOOLTIP),
2022 EndContainer(),
2023 NWidget(NWID_HORIZONTAL),
2024 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SVP_CANCEL), SetDataTip(STR_SAVE_PRESET_CANCEL, STR_SAVE_PRESET_CANCEL_TOOLTIP), SetFill(1, 0), SetResize(1, 0),
2025 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SVP_SAVE), SetDataTip(STR_SAVE_PRESET_SAVE, STR_SAVE_PRESET_SAVE_TOOLTIP), SetFill(1, 0), SetResize(1, 0),
2026 NWidget(WWT_RESIZEBOX, COLOUR_GREY),
2027 EndContainer(),
2030 /** Window preferences of the preset save window. */
2031 static WindowDesc::Prefs _save_preset_prefs ("save_preset");
2033 /** Window description of the preset save window. */
2034 static const WindowDesc _save_preset_desc(
2035 WDP_CENTER, 140, 110,
2036 WC_SAVE_PRESET, WC_GAME_OPTIONS,
2037 WDF_MODAL,
2038 _nested_save_preset_widgets, lengthof(_nested_save_preset_widgets),
2039 &_save_preset_prefs
2042 /** Class for the save preset window. */
2043 struct SavePresetWindow : public Window {
2044 QueryStringN<32> presetname_editbox; ///< Edit box of the save preset.
2045 GRFPresetList presets; ///< Available presets.
2046 Scrollbar *vscroll; ///< Pointer to the scrollbar widget.
2047 int selected; ///< Selected entry in the preset list, or \c -1 if none selected.
2050 * Constructor of the save preset window.
2051 * @param initial_text Initial text to display in the edit box, or \c NULL.
2053 SavePresetWindow (const char *initial_text) :
2054 Window (&_save_preset_desc), presetname_editbox(),
2055 presets(), vscroll (NULL), selected (-1)
2057 GetGRFPresetList(&this->presets);
2058 if (initial_text != NULL) {
2059 for (uint i = 0; i < this->presets.Length(); i++) {
2060 if (!strcmp(initial_text, this->presets[i])) {
2061 this->selected = i;
2062 break;
2067 this->querystrings[WID_SVP_EDITBOX] = &this->presetname_editbox;
2068 this->presetname_editbox.ok_button = WID_SVP_SAVE;
2069 this->presetname_editbox.cancel_button = WID_SVP_CANCEL;
2071 this->CreateNestedTree();
2072 this->vscroll = this->GetScrollbar(WID_SVP_SCROLLBAR);
2073 this->InitNested(0);
2075 this->vscroll->SetCount(this->presets.Length());
2076 this->SetFocusedWidget(WID_SVP_EDITBOX);
2077 if (initial_text != NULL) this->presetname_editbox.Assign(initial_text);
2080 ~SavePresetWindow()
2084 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
2086 switch (widget) {
2087 case WID_SVP_PRESET_LIST: {
2088 resize->height = FONT_HEIGHT_NORMAL + 2U;
2089 size->height = 0;
2090 for (uint i = 0; i < this->presets.Length(); i++) {
2091 Dimension d = GetStringBoundingBox(this->presets[i]);
2092 size->width = max(size->width, d.width + WD_FRAMETEXT_LEFT + WD_FRAMETEXT_RIGHT);
2093 resize->height = max(resize->height, d.height);
2095 size->height = ClampU(this->presets.Length(), 5, 20) * resize->height + 1;
2096 break;
2101 void DrawWidget (BlitArea *dpi, const Rect &r, int widget) const OVERRIDE
2103 switch (widget) {
2104 case WID_SVP_PRESET_LIST: {
2105 GfxFillRect (dpi, r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, PC_BLACK);
2107 uint step_height = this->GetWidget<NWidgetBase>(WID_SVP_PRESET_LIST)->resize_y;
2108 int offset_y = (step_height - FONT_HEIGHT_NORMAL) / 2;
2109 uint y = r.top + WD_FRAMERECT_TOP;
2110 uint min_index = this->vscroll->GetPosition();
2111 uint max_index = min(min_index + this->vscroll->GetCapacity(), this->presets.Length());
2113 for (uint i = min_index; i < max_index; i++) {
2114 if ((int)i == this->selected) GfxFillRect (dpi, r.left + 1, y, r.right - 1, y + step_height - 2, PC_DARK_BLUE);
2116 const char *text = this->presets[i];
2117 DrawString (dpi, r.left + WD_FRAMERECT_LEFT, r.right, y + offset_y, text, ((int)i == this->selected) ? TC_WHITE : TC_SILVER);
2118 y += step_height;
2120 break;
2125 virtual void OnClick(Point pt, int widget, int click_count)
2127 switch (widget) {
2128 case WID_SVP_PRESET_LIST: {
2129 uint row = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_SVP_PRESET_LIST);
2130 if (row < this->presets.Length()) {
2131 this->selected = row;
2132 this->presetname_editbox.Assign(this->presets[row]);
2133 this->SetWidgetDirty(WID_SVP_PRESET_LIST);
2134 this->SetWidgetDirty(WID_SVP_EDITBOX);
2136 break;
2139 case WID_SVP_CANCEL:
2140 this->Delete();
2141 break;
2143 case WID_SVP_SAVE: {
2144 Window *w = FindWindowById(WC_GAME_OPTIONS, WN_GAME_OPTIONS_NEWGRF_STATE);
2145 if (w != NULL && !StrEmpty(this->presetname_editbox.GetText())) w->OnQueryTextFinished(this->presetname_editbox.buffer);
2146 this->Delete();
2147 break;
2152 virtual void OnResize()
2154 this->vscroll->SetCapacityFromWidget(this, WID_SVP_PRESET_LIST);
2159 * Open the window for saving a preset.
2160 * @param initial_text Initial text to display in the edit box, or \c NULL.
2162 static void ShowSavePresetWindow(const char *initial_text)
2164 DeleteWindowByClass(WC_SAVE_PRESET);
2165 new SavePresetWindow(initial_text);
2168 /** Widgets for the progress window. */
2169 static const NWidgetPart _nested_scan_progress_widgets[] = {
2170 NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_NEWGRF_SCAN_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
2171 NWidget(WWT_PANEL, COLOUR_GREY),
2172 NWidget(NWID_HORIZONTAL), SetPIP(20, 0, 20),
2173 NWidget(NWID_VERTICAL), SetPIP(11, 8, 11),
2174 NWidget(WWT_LABEL, INVALID_COLOUR), SetDataTip(STR_NEWGRF_SCAN_MESSAGE, STR_NULL), SetFill(1, 0),
2175 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_SP_PROGRESS_BAR), SetFill(1, 0),
2176 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_SP_PROGRESS_TEXT), SetFill(1, 0),
2177 EndContainer(),
2178 EndContainer(),
2179 EndContainer(),
2182 /** Description of the widgets and other settings of the window. */
2183 static const WindowDesc _scan_progress_desc(
2184 WDP_CENTER, 0, 0,
2185 WC_MODAL_PROGRESS, WC_NONE,
2187 _nested_scan_progress_widgets, lengthof(_nested_scan_progress_widgets)
2190 /** Window for showing the progress of NewGRF scanning. */
2191 struct ScanProgressWindow : public Window {
2192 char *last_name; ///< The name of the last 'seen' NewGRF.
2193 int scanned; ///< The number of NewGRFs that we have seen.
2195 /** Create the window. */
2196 ScanProgressWindow() : Window(&_scan_progress_desc), last_name(NULL), scanned(0)
2198 this->InitNested(1);
2201 /** Free the last name buffer. */
2202 ~ScanProgressWindow()
2204 free(last_name);
2207 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
2209 switch (widget) {
2210 case WID_SP_PROGRESS_BAR: {
2211 SetDParamMaxValue(0, 100);
2212 *size = GetStringBoundingBox(STR_GENERATION_PROGRESS);
2213 /* We need some spacing for the 'border' */
2214 size->height += 8;
2215 size->width += 8;
2216 break;
2219 case WID_SP_PROGRESS_TEXT:
2220 SetDParamMaxDigits(0, 4);
2221 SetDParamMaxDigits(1, 4);
2222 /* We really don't know the width. We could determine it by scanning the NewGRFs,
2223 * but this is the status window for scanning them... */
2224 size->width = max(400U, GetStringBoundingBox(STR_NEWGRF_SCAN_STATUS).width);
2225 size->height = FONT_HEIGHT_NORMAL * 2 + WD_PAR_VSEP_NORMAL;
2226 break;
2230 void DrawWidget (BlitArea *dpi, const Rect &r, int widget) const OVERRIDE
2232 switch (widget) {
2233 case WID_SP_PROGRESS_BAR: {
2234 /* Draw the % complete with a bar and a text */
2235 DrawFrameRect (dpi, r.left, r.top, r.right, r.bottom, COLOUR_GREY, FR_BORDERONLY);
2236 uint percent = scanned * 100 / max(1U, _settings_client.gui.last_newgrf_count);
2237 DrawFrameRect (dpi, r.left + 1, r.top + 1, (int)((r.right - r.left - 2) * percent / 100) + r.left + 1, r.bottom - 1, COLOUR_MAUVE, FR_NONE);
2238 SetDParam(0, percent);
2239 DrawString (dpi, r.left, r.right, r.top + 5, STR_GENERATION_PROGRESS, TC_FROMSTRING, SA_HOR_CENTER);
2240 break;
2243 case WID_SP_PROGRESS_TEXT:
2244 SetDParam(0, this->scanned);
2245 SetDParam(1, _settings_client.gui.last_newgrf_count);
2246 DrawString (dpi, r.left, r.right, r.top, STR_NEWGRF_SCAN_STATUS, TC_FROMSTRING, SA_HOR_CENTER);
2248 DrawString (dpi, r.left, r.right, r.top + FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL, this->last_name == NULL ? "" : this->last_name, TC_BLACK, SA_HOR_CENTER);
2249 break;
2254 * Update the NewGRF scan status.
2255 * @param num The number of NewGRFs scanned so far.
2256 * @param name The name of the last scanned NewGRF.
2258 void UpdateNewGRFScanStatus(uint num, const char *name)
2260 free(this->last_name);
2261 if (name == NULL) {
2262 char buf[256];
2263 GetString (buf, STR_NEWGRF_SCAN_ARCHIVES);
2264 this->last_name = xstrdup(buf);
2265 } else {
2266 this->last_name = xstrdup(name);
2268 this->scanned = num;
2269 if (num > _settings_client.gui.last_newgrf_count) _settings_client.gui.last_newgrf_count = num;
2271 this->SetDirty();
2276 * Update the NewGRF scan status.
2277 * @param num The number of NewGRFs scanned so far.
2278 * @param name The name of the last scanned NewGRF.
2280 void UpdateNewGRFScanStatus(uint num, const char *name)
2282 ScanProgressWindow *w = dynamic_cast<ScanProgressWindow *>(FindWindowByClass(WC_MODAL_PROGRESS));
2283 if (w == NULL) w = new ScanProgressWindow();
2284 w->UpdateNewGRFScanStatus(num, name);