Fix #9521: Don't load at just removed docks that were part of a multi-dock station...
[openttd-github.git] / src / newgrf_gui.cpp
blob1b7ab44f6cb27e9b49d24c0ee34c2e30f2c483e4
1 /*
2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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/>.
6 */
8 /** @file newgrf_gui.cpp GUI to change NewGRF settings. */
10 #include "stdafx.h"
11 #include "error.h"
12 #include "settings_gui.h"
13 #include "newgrf.h"
14 #include "strings_func.h"
15 #include "window_func.h"
16 #include "gamelog.h"
17 #include "settings_type.h"
18 #include "settings_func.h"
19 #include "widgets/dropdown_type.h"
20 #include "widgets/dropdown_func.h"
21 #include "network/network.h"
22 #include "network/network_content.h"
23 #include "sortlist_type.h"
24 #include "stringfilter_type.h"
25 #include "querystring_gui.h"
26 #include "core/geometry_func.hpp"
27 #include "newgrf_text.h"
28 #include "textfile_gui.h"
29 #include "tilehighlight_func.h"
30 #include "fios.h"
31 #include "guitimer_func.h"
33 #include "widgets/newgrf_widget.h"
34 #include "widgets/misc_widget.h"
36 #include "table/sprites.h"
38 #include <map>
39 #include "safeguards.h"
41 /**
42 * Show the first NewGRF error we can find.
44 void ShowNewGRFError()
46 /* Do not show errors when entering the main screen */
47 if (_game_mode == GM_MENU) return;
49 for (const GRFConfig *c = _grfconfig; c != nullptr; c = c->next) {
50 /* Only show Fatal and Error level messages */
51 if (c->error == nullptr || (c->error->severity != STR_NEWGRF_ERROR_MSG_FATAL && c->error->severity != STR_NEWGRF_ERROR_MSG_ERROR)) continue;
53 SetDParam (0, c->error->message != STR_NULL ? c->error->message : STR_JUST_RAW_STRING);
54 SetDParamStr(1, c->error->custom_message);
55 SetDParamStr(2, c->filename);
56 SetDParamStr(3, c->error->data);
57 for (uint i = 0; i < lengthof(c->error->param_value); i++) {
58 SetDParam(4 + i, c->error->param_value[i]);
60 if (c->error->severity == STR_NEWGRF_ERROR_MSG_FATAL) {
61 ShowErrorMessage(STR_NEWGRF_ERROR_FATAL_POPUP, INVALID_STRING_ID, WL_CRITICAL);
62 } else {
63 ShowErrorMessage(STR_NEWGRF_ERROR_POPUP, INVALID_STRING_ID, WL_ERROR);
65 break;
69 static void ShowNewGRFInfo(const GRFConfig *c, uint x, uint y, uint right, uint bottom, bool show_params)
71 if (c->error != nullptr) {
72 char message[512];
73 SetDParamStr(0, c->error->custom_message); // is skipped by built-in messages
74 SetDParamStr(1, c->filename);
75 SetDParamStr(2, c->error->data);
76 for (uint i = 0; i < lengthof(c->error->param_value); i++) {
77 SetDParam(3 + i, c->error->param_value[i]);
79 GetString(message, c->error->message != STR_NULL ? c->error->message : STR_JUST_RAW_STRING, lastof(message));
81 SetDParamStr(0, message);
82 y = DrawStringMultiLine(x, right, y, bottom, c->error->severity);
85 /* Draw filename or not if it is not known (GRF sent over internet) */
86 if (c->filename != nullptr) {
87 SetDParamStr(0, c->filename);
88 y = DrawStringMultiLine(x, right, y, bottom, STR_NEWGRF_SETTINGS_FILENAME);
91 /* Prepare and draw GRF ID */
92 char buff[256];
93 seprintf(buff, lastof(buff), "%08X", BSWAP32(c->ident.grfid));
94 SetDParamStr(0, buff);
95 y = DrawStringMultiLine(x, right, y, bottom, STR_NEWGRF_SETTINGS_GRF_ID);
97 if ((_settings_client.gui.newgrf_developer_tools || _settings_client.gui.newgrf_show_old_versions) && c->version != 0) {
98 SetDParam(0, c->version);
99 y = DrawStringMultiLine(x, right, y, bottom, STR_NEWGRF_SETTINGS_VERSION);
101 if ((_settings_client.gui.newgrf_developer_tools || _settings_client.gui.newgrf_show_old_versions) && c->min_loadable_version != 0) {
102 SetDParam(0, c->min_loadable_version);
103 y = DrawStringMultiLine(x, right, y, bottom, STR_NEWGRF_SETTINGS_MIN_VERSION);
106 /* Prepare and draw MD5 sum */
107 md5sumToString(buff, lastof(buff), c->ident.md5sum);
108 SetDParamStr(0, buff);
109 y = DrawStringMultiLine(x, right, y, bottom, STR_NEWGRF_SETTINGS_MD5SUM);
111 /* Show GRF parameter list */
112 if (show_params) {
113 if (c->num_params > 0) {
114 GRFBuildParamList(buff, c, lastof(buff));
115 SetDParam(0, STR_JUST_RAW_STRING);
116 SetDParamStr(1, buff);
117 } else {
118 SetDParam(0, STR_NEWGRF_SETTINGS_PARAMETER_NONE);
120 y = DrawStringMultiLine(x, right, y, bottom, STR_NEWGRF_SETTINGS_PARAMETER);
122 /* Draw the palette of the NewGRF */
123 if (c->palette & GRFP_BLT_32BPP) {
124 SetDParam(0, (c->palette & GRFP_USE_WINDOWS) ? STR_NEWGRF_SETTINGS_PALETTE_LEGACY_32BPP : STR_NEWGRF_SETTINGS_PALETTE_DEFAULT_32BPP);
125 } else {
126 SetDParam(0, (c->palette & GRFP_USE_WINDOWS) ? STR_NEWGRF_SETTINGS_PALETTE_LEGACY : STR_NEWGRF_SETTINGS_PALETTE_DEFAULT);
128 y = DrawStringMultiLine(x, right, y, bottom, STR_NEWGRF_SETTINGS_PALETTE);
131 /* Show flags */
132 if (c->status == GCS_NOT_FOUND) y = DrawStringMultiLine(x, right, y, bottom, STR_NEWGRF_SETTINGS_NOT_FOUND);
133 if (c->status == GCS_DISABLED) y = DrawStringMultiLine(x, right, y, bottom, STR_NEWGRF_SETTINGS_DISABLED);
134 if (HasBit(c->flags, GCF_INVALID)) y = DrawStringMultiLine(x, right, y, bottom, STR_NEWGRF_SETTINGS_INCOMPATIBLE);
135 if (HasBit(c->flags, GCF_COMPATIBLE)) y = DrawStringMultiLine(x, right, y, bottom, STR_NEWGRF_COMPATIBLE_LOADED);
137 /* Draw GRF info if it exists */
138 if (!StrEmpty(c->GetDescription())) {
139 SetDParamStr(0, c->GetDescription());
140 y = DrawStringMultiLine(x, right, y, bottom, STR_BLACK_RAW_STRING);
141 } else {
142 y = DrawStringMultiLine(x, right, y, bottom, STR_NEWGRF_SETTINGS_NO_INFO);
147 * Window for setting the parameters of a NewGRF.
149 struct NewGRFParametersWindow : public Window {
150 static GRFParameterInfo dummy_parameter_info; ///< Dummy info in case a newgrf didn't provide info about some parameter.
151 GRFConfig *grf_config; ///< Set the parameters of this GRFConfig.
152 uint clicked_button; ///< The row in which a button was clicked or UINT_MAX.
153 bool clicked_increase; ///< True if the increase button was clicked, false for the decrease button.
154 bool clicked_dropdown; ///< Whether the dropdown is open.
155 bool closing_dropdown; ///< True, if the dropdown list is currently closing.
156 GUITimer timeout; ///< How long before we unpress the last-pressed button?
157 uint clicked_row; ///< The selected parameter
158 int line_height; ///< Height of a row in the matrix widget.
159 Scrollbar *vscroll;
160 bool action14present; ///< True if action14 information is present.
161 bool editable; ///< Allow editing parameters.
163 NewGRFParametersWindow(WindowDesc *desc, GRFConfig *c, bool editable) : Window(desc),
164 grf_config(c),
165 clicked_button(UINT_MAX),
166 clicked_dropdown(false),
167 closing_dropdown(false),
168 clicked_row(UINT_MAX),
169 editable(editable)
171 this->action14present = (c->num_valid_params != lengthof(c->param) || c->param_info.size() != 0);
173 this->CreateNestedTree();
174 this->vscroll = this->GetScrollbar(WID_NP_SCROLLBAR);
175 this->GetWidget<NWidgetStacked>(WID_NP_SHOW_NUMPAR)->SetDisplayedPlane(this->action14present ? SZSP_HORIZONTAL : 0);
176 this->GetWidget<NWidgetStacked>(WID_NP_SHOW_DESCRIPTION)->SetDisplayedPlane(this->action14present ? 0 : SZSP_HORIZONTAL);
177 this->FinishInitNested(); // Initializes 'this->line_height' as side effect.
179 this->SetWidgetDisabledState(WID_NP_RESET, !this->editable);
181 this->InvalidateData();
185 * Get a dummy parameter-info object with default information.
186 * @param nr The param number that should be changed.
187 * @return GRFParameterInfo with dummy information about the given parameter.
189 static GRFParameterInfo *GetDummyParameterInfo(uint nr)
191 dummy_parameter_info.param_nr = nr;
192 return &dummy_parameter_info;
195 void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
197 switch (widget) {
198 case WID_NP_NUMPAR_DEC:
199 case WID_NP_NUMPAR_INC: {
200 size->width = std::max(SETTING_BUTTON_WIDTH / 2, FONT_HEIGHT_NORMAL);
201 size->height = std::max(SETTING_BUTTON_HEIGHT, FONT_HEIGHT_NORMAL);
202 break;
205 case WID_NP_NUMPAR: {
206 SetDParamMaxValue(0, lengthof(this->grf_config->param));
207 Dimension d = GetStringBoundingBox(this->GetWidget<NWidgetCore>(widget)->widget_data);
208 d.width += padding.width;
209 d.height += padding.height;
210 *size = maxdim(*size, d);
211 break;
214 case WID_NP_BACKGROUND:
215 this->line_height = std::max(SETTING_BUTTON_HEIGHT, FONT_HEIGHT_NORMAL) + WD_MATRIX_TOP + WD_MATRIX_BOTTOM;
217 resize->width = 1;
218 resize->height = this->line_height;
219 size->height = 5 * this->line_height;
220 break;
222 case WID_NP_DESCRIPTION:
223 /* Minimum size of 4 lines. The 500 is the default size of the window. */
224 Dimension suggestion = {500 - WD_FRAMERECT_LEFT - WD_FRAMERECT_RIGHT, (uint)FONT_HEIGHT_NORMAL * 4 + WD_TEXTPANEL_TOP + WD_TEXTPANEL_BOTTOM};
225 for (uint i = 0; i < this->grf_config->param_info.size(); i++) {
226 const GRFParameterInfo *par_info = this->grf_config->param_info[i];
227 if (par_info == nullptr) continue;
228 const char *desc = GetGRFStringFromGRFText(par_info->desc);
229 if (desc == nullptr) continue;
230 Dimension d = GetStringMultiLineBoundingBox(desc, suggestion);
231 d.height += WD_TEXTPANEL_TOP + WD_TEXTPANEL_BOTTOM;
232 suggestion = maxdim(d, suggestion);
234 size->height = suggestion.height;
235 break;
239 void SetStringParameters(int widget) const override
241 switch (widget) {
242 case WID_NP_NUMPAR:
243 SetDParam(0, this->vscroll->GetCount());
244 break;
248 void DrawWidget(const Rect &r, int widget) const override
250 if (widget == WID_NP_DESCRIPTION) {
251 const GRFParameterInfo *par_info = (this->clicked_row < this->grf_config->param_info.size()) ? this->grf_config->param_info[this->clicked_row] : nullptr;
252 if (par_info == nullptr) return;
253 const char *desc = GetGRFStringFromGRFText(par_info->desc);
254 if (desc == nullptr) return;
255 DrawStringMultiLine(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + WD_TEXTPANEL_TOP, r.bottom - WD_TEXTPANEL_BOTTOM, desc, TC_BLACK);
256 return;
257 } else if (widget != WID_NP_BACKGROUND) {
258 return;
261 bool rtl = _current_text_dir == TD_RTL;
262 uint buttons_left = rtl ? r.right - SETTING_BUTTON_WIDTH - 3 : r.left + 4;
263 uint text_left = r.left + (rtl ? WD_FRAMERECT_LEFT : SETTING_BUTTON_WIDTH + 8);
264 uint text_right = r.right - (rtl ? SETTING_BUTTON_WIDTH + 8 : WD_FRAMERECT_RIGHT);
266 int y = r.top;
267 int button_y_offset = (this->line_height - SETTING_BUTTON_HEIGHT) / 2;
268 int text_y_offset = (this->line_height - FONT_HEIGHT_NORMAL) / 2;
269 for (uint i = this->vscroll->GetPosition(); this->vscroll->IsVisible(i) && i < this->vscroll->GetCount(); i++) {
270 GRFParameterInfo *par_info = (i < this->grf_config->param_info.size()) ? this->grf_config->param_info[i] : nullptr;
271 if (par_info == nullptr) par_info = GetDummyParameterInfo(i);
272 uint32 current_value = par_info->GetValue(this->grf_config);
273 bool selected = (i == this->clicked_row);
275 if (par_info->type == PTYPE_BOOL) {
276 DrawBoolButton(buttons_left, y + button_y_offset, current_value != 0, this->editable);
277 SetDParam(2, par_info->GetValue(this->grf_config) == 0 ? STR_CONFIG_SETTING_OFF : STR_CONFIG_SETTING_ON);
278 } else if (par_info->type == PTYPE_UINT_ENUM) {
279 if (par_info->complete_labels) {
280 DrawDropDownButton(buttons_left, y + button_y_offset, COLOUR_YELLOW, this->clicked_row == i && this->clicked_dropdown, this->editable);
281 } else {
282 DrawArrowButtons(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);
284 SetDParam(2, STR_JUST_INT);
285 SetDParam(3, current_value);
286 if (par_info->value_names.Contains(current_value)) {
287 const char *label = GetGRFStringFromGRFText(par_info->value_names.Find(current_value)->second);
288 if (label != nullptr) {
289 SetDParam(2, STR_JUST_RAW_STRING);
290 SetDParamStr(3, label);
295 const char *name = GetGRFStringFromGRFText(par_info->name);
296 if (name != nullptr) {
297 SetDParam(0, STR_JUST_RAW_STRING);
298 SetDParamStr(1, name);
299 } else {
300 SetDParam(0, STR_NEWGRF_PARAMETERS_DEFAULT_NAME);
301 SetDParam(1, i + 1);
304 DrawString(text_left, text_right, y + text_y_offset, STR_NEWGRF_PARAMETERS_SETTING, selected ? TC_WHITE : TC_LIGHT_BLUE);
305 y += this->line_height;
309 void OnPaint() override
311 if (this->closing_dropdown) {
312 this->closing_dropdown = false;
313 this->clicked_dropdown = false;
315 this->DrawWidgets();
318 void OnClick(Point pt, int widget, int click_count) override
320 switch (widget) {
321 case WID_NP_NUMPAR_DEC:
322 if (this->editable && !this->action14present && this->grf_config->num_params > 0) {
323 this->grf_config->num_params--;
324 this->InvalidateData();
325 SetWindowDirty(WC_GAME_OPTIONS, WN_GAME_OPTIONS_NEWGRF_STATE);
327 break;
329 case WID_NP_NUMPAR_INC: {
330 GRFConfig *c = this->grf_config;
331 if (this->editable && !this->action14present && c->num_params < c->num_valid_params) {
332 c->param[c->num_params++] = 0;
333 this->InvalidateData();
334 SetWindowDirty(WC_GAME_OPTIONS, WN_GAME_OPTIONS_NEWGRF_STATE);
336 break;
339 case WID_NP_BACKGROUND: {
340 if (!this->editable) break;
341 uint num = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_NP_BACKGROUND);
342 if (num >= this->vscroll->GetCount()) break;
343 if (this->clicked_row != num) {
344 this->CloseChildWindows(WC_QUERY_STRING);
345 HideDropDownMenu(this);
346 this->clicked_row = num;
347 this->clicked_dropdown = false;
350 const NWidgetBase *wid = this->GetWidget<NWidgetBase>(WID_NP_BACKGROUND);
351 int x = pt.x - wid->pos_x;
352 if (_current_text_dir == TD_RTL) x = wid->current_x - 1 - x;
353 x -= 4;
355 GRFParameterInfo *par_info = (num < this->grf_config->param_info.size()) ? this->grf_config->param_info[num] : nullptr;
356 if (par_info == nullptr) par_info = GetDummyParameterInfo(num);
358 /* One of the arrows is clicked */
359 uint32 old_val = par_info->GetValue(this->grf_config);
360 if (par_info->type != PTYPE_BOOL && IsInsideMM(x, 0, SETTING_BUTTON_WIDTH) && par_info->complete_labels) {
361 if (this->clicked_dropdown) {
362 /* unclick the dropdown */
363 HideDropDownMenu(this);
364 this->clicked_dropdown = false;
365 this->closing_dropdown = false;
366 } else {
367 const NWidgetBase *wid = this->GetWidget<NWidgetBase>(WID_NP_BACKGROUND);
368 int rel_y = (pt.y - (int)wid->pos_y) % this->line_height;
370 Rect wi_rect;
371 wi_rect.left = pt.x - (_current_text_dir == TD_RTL ? SETTING_BUTTON_WIDTH - 1 - x : x);;
372 wi_rect.right = wi_rect.left + SETTING_BUTTON_WIDTH - 1;
373 wi_rect.top = pt.y - rel_y + (this->line_height - SETTING_BUTTON_HEIGHT) / 2;
374 wi_rect.bottom = wi_rect.top + SETTING_BUTTON_HEIGHT - 1;
376 /* For dropdowns we also have to check the y position thoroughly, the mouse may not above the just opening dropdown */
377 if (pt.y >= wi_rect.top && pt.y <= wi_rect.bottom) {
378 this->clicked_dropdown = true;
379 this->closing_dropdown = false;
381 DropDownList list;
382 for (uint32 i = par_info->min_value; i <= par_info->max_value; i++) {
383 list.emplace_back(new DropDownListCharStringItem(GetGRFStringFromGRFText(par_info->value_names.Find(i)->second), i, false));
386 ShowDropDownListAt(this, std::move(list), old_val, -1, wi_rect, COLOUR_ORANGE, true);
389 } else if (IsInsideMM(x, 0, SETTING_BUTTON_WIDTH)) {
390 uint32 val = old_val;
391 if (par_info->type == PTYPE_BOOL) {
392 val = !val;
393 } else {
394 if (x >= SETTING_BUTTON_WIDTH / 2) {
395 /* Increase button clicked */
396 if (val < par_info->max_value) val++;
397 this->clicked_increase = true;
398 } else {
399 /* Decrease button clicked */
400 if (val > par_info->min_value) val--;
401 this->clicked_increase = false;
404 if (val != old_val) {
405 par_info->SetValue(this->grf_config, val);
407 this->clicked_button = num;
408 this->timeout.SetInterval(150);
410 } else if (par_info->type == PTYPE_UINT_ENUM && !par_info->complete_labels && click_count >= 2) {
411 /* Display a query box so users can enter a custom value. */
412 SetDParam(0, old_val);
413 ShowQueryString(STR_JUST_INT, STR_CONFIG_SETTING_QUERY_CAPTION, 10, this, CS_NUMERAL, QSF_NONE);
415 this->SetDirty();
416 break;
419 case WID_NP_RESET:
420 if (!this->editable) break;
421 this->grf_config->SetParameterDefaults();
422 this->InvalidateData();
423 SetWindowDirty(WC_GAME_OPTIONS, WN_GAME_OPTIONS_NEWGRF_STATE);
424 break;
426 case WID_NP_ACCEPT:
427 this->Close();
428 break;
432 void OnQueryTextFinished(char *str) override
434 if (StrEmpty(str)) return;
435 int32 value = atoi(str);
436 GRFParameterInfo *par_info = ((uint)this->clicked_row < this->grf_config->param_info.size()) ? this->grf_config->param_info[this->clicked_row] : nullptr;
437 if (par_info == nullptr) par_info = GetDummyParameterInfo(this->clicked_row);
438 uint32 val = Clamp<uint32>(value, par_info->min_value, par_info->max_value);
439 par_info->SetValue(this->grf_config, val);
440 this->SetDirty();
443 void OnDropdownSelect(int widget, int index) override
445 assert(this->clicked_dropdown);
446 GRFParameterInfo *par_info = ((uint)this->clicked_row < this->grf_config->param_info.size()) ? this->grf_config->param_info[this->clicked_row] : nullptr;
447 if (par_info == nullptr) par_info = GetDummyParameterInfo(this->clicked_row);
448 par_info->SetValue(this->grf_config, index);
449 this->SetDirty();
452 void OnDropdownClose(Point pt, int widget, int index, bool instant_close) override
454 /* We cannot raise the dropdown button just yet. OnClick needs some hint, whether
455 * the same dropdown button was clicked again, and then not open the dropdown again.
456 * So, we only remember that it was closed, and process it on the next OnPaint, which is
457 * after OnClick. */
458 assert(this->clicked_dropdown);
459 this->closing_dropdown = true;
460 this->SetDirty();
463 void OnResize() override
465 this->vscroll->SetCapacityFromWidget(this, WID_NP_BACKGROUND);
469 * Some data on this window has become invalid.
470 * @param data Information about the changed data.
471 * @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.
473 void OnInvalidateData(int data = 0, bool gui_scope = true) override
475 if (!gui_scope) return;
476 if (!this->action14present) {
477 this->SetWidgetDisabledState(WID_NP_NUMPAR_DEC, !this->editable || this->grf_config->num_params == 0);
478 this->SetWidgetDisabledState(WID_NP_NUMPAR_INC, !this->editable || this->grf_config->num_params >= this->grf_config->num_valid_params);
481 this->vscroll->SetCount(this->action14present ? this->grf_config->num_valid_params : this->grf_config->num_params);
482 if (this->clicked_row != UINT_MAX && this->clicked_row >= this->vscroll->GetCount()) {
483 this->clicked_row = UINT_MAX;
484 this->CloseChildWindows(WC_QUERY_STRING);
488 void OnRealtimeTick(uint delta_ms) override
490 if (timeout.Elapsed(delta_ms)) {
491 this->clicked_button = UINT_MAX;
492 this->SetDirty();
496 GRFParameterInfo NewGRFParametersWindow::dummy_parameter_info(0);
499 static const NWidgetPart _nested_newgrf_parameter_widgets[] = {
500 NWidget(NWID_HORIZONTAL),
501 NWidget(WWT_CLOSEBOX, COLOUR_MAUVE),
502 NWidget(WWT_CAPTION, COLOUR_MAUVE), SetDataTip(STR_NEWGRF_PARAMETERS_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
503 NWidget(WWT_DEFSIZEBOX, COLOUR_MAUVE),
504 EndContainer(),
505 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_NP_SHOW_NUMPAR),
506 NWidget(WWT_PANEL, COLOUR_MAUVE), SetResize(1, 0), SetFill(1, 0), SetPIP(4, 0, 4),
507 NWidget(NWID_HORIZONTAL), SetPIP(4, 0, 4),
508 NWidget(WWT_PUSHARROWBTN, COLOUR_YELLOW, WID_NP_NUMPAR_DEC), SetMinimalSize(12, 12), SetDataTip(AWV_DECREASE, STR_NULL),
509 NWidget(WWT_PUSHARROWBTN, COLOUR_YELLOW, WID_NP_NUMPAR_INC), SetMinimalSize(12, 12), SetDataTip(AWV_INCREASE, STR_NULL),
510 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),
511 EndContainer(),
512 EndContainer(),
513 EndContainer(),
514 NWidget(NWID_HORIZONTAL),
515 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),
516 NWidget(NWID_VSCROLLBAR, COLOUR_MAUVE, WID_NP_SCROLLBAR),
517 EndContainer(),
518 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_NP_SHOW_DESCRIPTION),
519 NWidget(WWT_PANEL, COLOUR_MAUVE, WID_NP_DESCRIPTION), SetResize(1, 0), SetFill(1, 0),
520 EndContainer(),
521 EndContainer(),
522 NWidget(NWID_HORIZONTAL),
523 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
524 NWidget(WWT_PUSHTXTBTN, COLOUR_MAUVE, WID_NP_ACCEPT), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_NEWGRF_PARAMETERS_CLOSE, STR_NULL),
525 NWidget(WWT_PUSHTXTBTN, COLOUR_MAUVE, WID_NP_RESET), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_NEWGRF_PARAMETERS_RESET, STR_NEWGRF_PARAMETERS_RESET_TOOLTIP),
526 EndContainer(),
527 NWidget(WWT_RESIZEBOX, COLOUR_MAUVE),
528 EndContainer(),
531 /** Window definition for the change grf parameters window */
532 static WindowDesc _newgrf_parameters_desc(
533 WDP_CENTER, "settings_newgrf_config", 500, 208,
534 WC_GRF_PARAMETERS, WC_NONE,
536 _nested_newgrf_parameter_widgets, lengthof(_nested_newgrf_parameter_widgets)
539 static void OpenGRFParameterWindow(GRFConfig *c, bool editable)
541 CloseWindowByClass(WC_GRF_PARAMETERS);
542 new NewGRFParametersWindow(&_newgrf_parameters_desc, c, editable);
545 /** Window for displaying the textfile of a NewGRF. */
546 struct NewGRFTextfileWindow : public TextfileWindow {
547 const GRFConfig *grf_config; ///< View the textfile of this GRFConfig.
549 NewGRFTextfileWindow(TextfileType file_type, const GRFConfig *c) : TextfileWindow(file_type), grf_config(c)
551 const char *textfile = this->grf_config->GetTextfile(file_type);
552 this->LoadTextfile(textfile, NEWGRF_DIR);
555 void SetStringParameters(int widget) const override
557 if (widget == WID_TF_CAPTION) {
558 SetDParam(0, STR_CONTENT_TYPE_NEWGRF);
559 SetDParamStr(1, this->grf_config->GetName());
564 void ShowNewGRFTextfileWindow(TextfileType file_type, const GRFConfig *c)
566 CloseWindowById(WC_TEXTFILE, file_type);
567 new NewGRFTextfileWindow(file_type, c);
570 typedef std::map<uint32, const GRFConfig *> GrfIdMap; ///< Map of grfid to the grf config.
573 * Add all grf configs from \a c into the map.
574 * @param c Grf list to add.
575 * @param grfid_map Map to add them to.
577 static void FillGrfidMap(const GRFConfig *c, GrfIdMap *grfid_map)
579 while (c != nullptr) {
580 std::pair<uint32, const GRFConfig *> p(c->ident.grfid, c);
581 grfid_map->insert(p);
582 c = c->next;
586 static void NewGRFConfirmationCallback(Window *w, bool confirmed);
587 static void ShowSavePresetWindow(const char *initial_text);
590 * Window for showing NewGRF files
592 struct NewGRFWindow : public Window, NewGRFScanCallback {
593 typedef GUIList<const GRFConfig *, StringFilter &> GUIGRFConfigList;
595 static const uint EDITBOX_MAX_SIZE = 50;
597 static Listing last_sorting; ///< Default sorting of #GUIGRFConfigList.
598 static Filtering last_filtering; ///< Default filtering of #GUIGRFConfigList.
599 static GUIGRFConfigList::SortFunction * const sorter_funcs[]; ///< Sort functions of the #GUIGRFConfigList.
600 static GUIGRFConfigList::FilterFunction * const filter_funcs[]; ///< Filter functions of the #GUIGRFConfigList.
602 GUIGRFConfigList avails; ///< Available (non-active) grfs.
603 const GRFConfig *avail_sel; ///< Currently selected available grf. \c nullptr is none is selected.
604 int avail_pos; ///< Index of #avail_sel if existing, else \c -1.
605 StringFilter string_filter; ///< Filter for available grf.
606 QueryString filter_editbox; ///< Filter editbox;
608 StringList grf_presets; ///< List of known NewGRF presets.
610 GRFConfig *actives; ///< Temporary active grf list to which changes are made.
611 GRFConfig *active_sel; ///< Selected active grf item.
613 GRFConfig **orig_list; ///< List active grfs in the game. Used as initial value, may be updated by the window.
614 bool editable; ///< Is the window editable?
615 bool show_params; ///< Are the grf-parameters shown in the info-panel?
616 bool execute; ///< On pressing 'apply changes' are grf changes applied immediately, or only list is updated.
617 int preset; ///< Selected preset or \c -1 if none selected.
618 int active_over; ///< Active GRF item over which another one is dragged, \c -1 if none.
619 bool modified; ///< The list of active NewGRFs has been modified since the last time they got saved.
621 Scrollbar *vscroll;
622 Scrollbar *vscroll2;
624 NewGRFWindow(WindowDesc *desc, bool editable, bool show_params, bool execute, GRFConfig **orig_list) : Window(desc), filter_editbox(EDITBOX_MAX_SIZE)
626 this->avail_sel = nullptr;
627 this->avail_pos = -1;
628 this->active_sel = nullptr;
629 this->actives = nullptr;
630 this->orig_list = orig_list;
631 this->editable = editable;
632 this->execute = execute;
633 this->show_params = show_params;
634 this->preset = -1;
635 this->active_over = -1;
637 CopyGRFConfigList(&this->actives, *orig_list, false);
638 this->grf_presets = GetGRFPresetList();
640 this->CreateNestedTree();
641 this->vscroll = this->GetScrollbar(WID_NS_SCROLLBAR);
642 this->vscroll2 = this->GetScrollbar(WID_NS_SCROLL2BAR);
644 this->GetWidget<NWidgetStacked>(WID_NS_SHOW_REMOVE)->SetDisplayedPlane(this->editable ? 0 : 1);
645 this->GetWidget<NWidgetStacked>(WID_NS_SHOW_APPLY)->SetDisplayedPlane(this->editable ? 0 : this->show_params ? 1 : SZSP_HORIZONTAL);
646 this->FinishInitNested(WN_GAME_OPTIONS_NEWGRF_STATE);
648 this->querystrings[WID_NS_FILTER] = &this->filter_editbox;
649 this->filter_editbox.cancel_button = QueryString::ACTION_CLEAR;
650 if (editable) {
651 this->SetFocusedWidget(WID_NS_FILTER);
652 } else {
653 this->DisableWidget(WID_NS_FILTER);
656 this->avails.SetListing(this->last_sorting);
657 this->avails.SetFiltering(this->last_filtering);
658 this->avails.SetSortFuncs(this->sorter_funcs);
659 this->avails.SetFilterFuncs(this->filter_funcs);
660 this->avails.ForceRebuild();
662 this->OnInvalidateData(GOID_NEWGRF_CURRENT_LOADED);
665 void Close() override
667 CloseWindowByClass(WC_GRF_PARAMETERS);
668 CloseWindowByClass(WC_TEXTFILE);
669 CloseWindowByClass(WC_SAVE_PRESET);
671 if (this->editable && this->modified && !this->execute && !_exit_game) {
672 CopyGRFConfigList(this->orig_list, this->actives, true);
673 ResetGRFConfig(false);
674 ReloadNewGRFData();
677 this->Window::Close();
680 ~NewGRFWindow()
682 /* Remove the temporary copy of grf-list used in window */
683 ClearGRFConfigList(&this->actives);
687 * Test whether the currently active set of NewGRFs can be upgraded with the available NewGRFs.
688 * @return Whether an upgrade is possible.
690 bool CanUpgradeCurrent()
692 GrfIdMap grfid_map;
693 FillGrfidMap(this->actives, &grfid_map);
695 for (const GRFConfig *a = _all_grfs; a != nullptr; a = a->next) {
696 GrfIdMap::const_iterator iter = grfid_map.find(a->ident.grfid);
697 if (iter != grfid_map.end() && a->version > iter->second->version) return true;
699 return false;
702 /** Upgrade the currently active set of NewGRFs. */
703 void UpgradeCurrent()
705 GrfIdMap grfid_map;
706 FillGrfidMap(this->actives, &grfid_map);
708 for (const GRFConfig *a = _all_grfs; a != nullptr; a = a->next) {
709 GrfIdMap::iterator iter = grfid_map.find(a->ident.grfid);
710 if (iter == grfid_map.end() || iter->second->version >= a->version) continue;
712 GRFConfig **c = &this->actives;
713 while (*c != iter->second) c = &(*c)->next;
714 GRFConfig *d = new GRFConfig(*a);
715 d->next = (*c)->next;
716 d->CopyParams(**c);
717 if (this->active_sel == *c) {
718 CloseWindowByClass(WC_GRF_PARAMETERS);
719 CloseWindowByClass(WC_TEXTFILE);
720 this->active_sel = nullptr;
722 delete *c;
723 *c = d;
724 iter->second = d;
728 void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
730 switch (widget) {
731 case WID_NS_FILE_LIST:
733 Dimension d = maxdim(GetSpriteSize(SPR_SQUARE), GetSpriteSize(SPR_WARNING_SIGN));
734 resize->height = std::max(d.height + 2U, FONT_HEIGHT_NORMAL + 2U);
735 size->height = std::max(size->height, WD_FRAMERECT_TOP + 6 * resize->height + WD_FRAMERECT_BOTTOM);
736 break;
739 case WID_NS_AVAIL_LIST:
740 resize->height = std::max(12, FONT_HEIGHT_NORMAL + 2);
741 size->height = std::max(size->height, WD_FRAMERECT_TOP + 8 * resize->height + WD_FRAMERECT_BOTTOM);
742 break;
744 case WID_NS_NEWGRF_INFO_TITLE: {
745 Dimension dim = GetStringBoundingBox(STR_NEWGRF_SETTINGS_INFO_TITLE);
746 size->height = std::max(size->height, dim.height + WD_FRAMETEXT_TOP + WD_FRAMETEXT_BOTTOM);
747 size->width = std::max(size->width, dim.width + WD_FRAMETEXT_LEFT + WD_FRAMETEXT_RIGHT);
748 break;
751 case WID_NS_NEWGRF_INFO:
752 size->height = std::max(size->height, WD_FRAMERECT_TOP + 10 * FONT_HEIGHT_NORMAL + WD_FRAMERECT_BOTTOM + padding.height + 2);
753 break;
755 case WID_NS_PRESET_LIST: {
756 Dimension d = GetStringBoundingBox(STR_NUM_CUSTOM);
757 for (const auto &i : this->grf_presets) {
758 SetDParamStr(0, i);
759 d = maxdim(d, GetStringBoundingBox(STR_JUST_RAW_STRING));
761 d.width += padding.width;
762 *size = maxdim(d, *size);
763 break;
766 case WID_NS_CONTENT_DOWNLOAD:
767 case WID_NS_CONTENT_DOWNLOAD2: {
768 Dimension d = GetStringBoundingBox(STR_NEWGRF_SETTINGS_FIND_MISSING_CONTENT_BUTTON);
769 *size = maxdim(d, GetStringBoundingBox(STR_INTRO_ONLINE_CONTENT));
770 size->width += padding.width;
771 size->height += padding.height;
772 break;
777 void OnResize() override
779 this->vscroll->SetCapacityFromWidget(this, WID_NS_FILE_LIST);
780 this->vscroll2->SetCapacityFromWidget(this, WID_NS_AVAIL_LIST);
783 void SetStringParameters(int widget) const override
785 switch (widget) {
786 case WID_NS_PRESET_LIST:
787 if (this->preset == -1) {
788 SetDParam(0, STR_NUM_CUSTOM);
789 } else {
790 SetDParam(0, STR_JUST_RAW_STRING);
791 SetDParamStr(1, this->grf_presets[this->preset]);
793 break;
798 * Pick the palette for the sprite of the grf to display.
799 * @param c grf to display.
800 * @return Palette for the sprite.
802 inline PaletteID GetPalette(const GRFConfig *c) const
804 PaletteID pal;
806 /* Pick a colour */
807 switch (c->status) {
808 case GCS_NOT_FOUND:
809 case GCS_DISABLED:
810 pal = PALETTE_TO_RED;
811 break;
812 case GCS_ACTIVATED:
813 pal = PALETTE_TO_GREEN;
814 break;
815 default:
816 pal = PALETTE_TO_BLUE;
817 break;
820 /* Do not show a "not-failure" colour when it actually failed to load */
821 if (pal != PALETTE_TO_RED) {
822 if (HasBit(c->flags, GCF_STATIC)) {
823 pal = PALETTE_TO_GREY;
824 } else if (HasBit(c->flags, GCF_COMPATIBLE)) {
825 pal = PALETTE_TO_ORANGE;
829 return pal;
832 void DrawWidget(const Rect &r, int widget) const override
834 switch (widget) {
835 case WID_NS_FILE_LIST: {
836 GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, PC_BLACK);
838 uint step_height = this->GetWidget<NWidgetBase>(WID_NS_FILE_LIST)->resize_y;
839 uint y = r.top + WD_FRAMERECT_TOP;
840 Dimension square = GetSpriteSize(SPR_SQUARE);
841 Dimension warning = GetSpriteSize(SPR_WARNING_SIGN);
842 int square_offset_y = (step_height - square.height) / 2;
843 int warning_offset_y = (step_height - warning.height) / 2;
844 int offset_y = (step_height - FONT_HEIGHT_NORMAL) / 2;
846 bool rtl = _current_text_dir == TD_RTL;
847 uint text_left = rtl ? r.left + WD_FRAMERECT_LEFT : r.left + square.width + 15;
848 uint text_right = rtl ? r.right - square.width - 15 : r.right - WD_FRAMERECT_RIGHT;
849 uint square_left = rtl ? r.right - square.width - 5 : r.left + 5;
850 uint warning_left = rtl ? r.right - square.width - warning.width - 10 : r.left + square.width + 10;
852 int i = 0;
853 for (const GRFConfig *c = this->actives; c != nullptr; c = c->next, i++) {
854 if (this->vscroll->IsVisible(i)) {
855 const char *text = c->GetName();
856 bool h = (this->active_sel == c);
857 PaletteID pal = this->GetPalette(c);
859 if (h) {
860 GfxFillRect(r.left + 1, y, r.right - 1, y + step_height - 1, PC_DARK_BLUE);
861 } else if (i == this->active_over) {
862 /* Get index of current selection. */
863 int active_sel_pos = 0;
864 for (GRFConfig *c = this->actives; c != nullptr && c != this->active_sel; c = c->next, active_sel_pos++) {}
865 if (active_sel_pos != this->active_over) {
866 uint top = this->active_over < active_sel_pos ? y + 1 : y + step_height - 2;
867 GfxFillRect(r.left + WD_FRAMERECT_LEFT, top - 1, r.right - WD_FRAMERECT_RIGHT, top + 1, PC_GREY);
870 DrawSprite(SPR_SQUARE, pal, square_left, y + square_offset_y);
871 if (c->error != nullptr) DrawSprite(SPR_WARNING_SIGN, 0, warning_left, y + warning_offset_y);
872 uint txtoffset = c->error == nullptr ? 0 : warning.width;
873 DrawString(text_left + (rtl ? 0 : txtoffset), text_right - (rtl ? txtoffset : 0), y + offset_y, text, h ? TC_WHITE : TC_ORANGE);
874 y += step_height;
877 if (i == this->active_over && this->vscroll->IsVisible(i)) { // Highlight is after the last GRF entry.
878 GfxFillRect(r.left + WD_FRAMERECT_LEFT, y, r.right - WD_FRAMERECT_RIGHT, y + 2, PC_GREY);
880 break;
883 case WID_NS_AVAIL_LIST: {
884 GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, this->active_over == -2 ? PC_DARK_GREY : PC_BLACK);
886 uint step_height = this->GetWidget<NWidgetBase>(WID_NS_AVAIL_LIST)->resize_y;
887 int offset_y = (step_height - FONT_HEIGHT_NORMAL) / 2;
888 uint y = r.top + WD_FRAMERECT_TOP;
889 uint min_index = this->vscroll2->GetPosition();
890 uint max_index = std::min(min_index + this->vscroll2->GetCapacity(), (uint)this->avails.size());
892 for (uint i = min_index; i < max_index; i++) {
893 const GRFConfig *c = this->avails[i];
894 bool h = (c == this->avail_sel);
895 const char *text = c->GetName();
897 if (h) GfxFillRect(r.left + 1, y, r.right - 1, y + step_height - 1, PC_DARK_BLUE);
898 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y + offset_y, text, h ? TC_WHITE : TC_SILVER);
899 y += step_height;
901 break;
904 case WID_NS_NEWGRF_INFO_TITLE:
905 /* Create the nice grayish rectangle at the details top. */
906 GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, PC_DARK_BLUE);
907 DrawString(r.left, r.right, (r.top + r.bottom - FONT_HEIGHT_NORMAL) / 2, STR_NEWGRF_SETTINGS_INFO_TITLE, TC_FROMSTRING, SA_HOR_CENTER);
908 break;
910 case WID_NS_NEWGRF_INFO: {
911 const GRFConfig *selected = this->active_sel;
912 if (selected == nullptr) selected = this->avail_sel;
913 if (selected != nullptr) {
914 ShowNewGRFInfo(selected, r.left + WD_FRAMERECT_LEFT, r.top + WD_FRAMERECT_TOP, r.right - WD_FRAMERECT_RIGHT, r.bottom - WD_FRAMERECT_BOTTOM, this->show_params);
916 break;
921 void OnClick(Point pt, int widget, int click_count) override
923 if (widget >= WID_NS_NEWGRF_TEXTFILE && widget < WID_NS_NEWGRF_TEXTFILE + TFT_END) {
924 if (this->active_sel == nullptr && this->avail_sel == nullptr) return;
926 ShowNewGRFTextfileWindow((TextfileType)(widget - WID_NS_NEWGRF_TEXTFILE), this->active_sel != nullptr ? this->active_sel : this->avail_sel);
927 return;
930 switch (widget) {
931 case WID_NS_PRESET_LIST: {
932 DropDownList list;
934 /* Add 'None' option for clearing list */
935 list.emplace_back(new DropDownListStringItem(STR_NONE, -1, false));
937 for (uint i = 0; i < this->grf_presets.size(); i++) {
938 list.emplace_back(new DropDownListCharStringItem(this->grf_presets[i], i, false));
941 this->CloseChildWindows(WC_QUERY_STRING); // Remove the parameter query window
942 ShowDropDownList(this, std::move(list), this->preset, WID_NS_PRESET_LIST);
943 break;
946 case WID_NS_OPEN_URL: {
947 const GRFConfig *c = (this->avail_sel == nullptr) ? this->active_sel : this->avail_sel;
949 extern void OpenBrowser(const char *url);
950 OpenBrowser(c->GetURL());
951 break;
954 case WID_NS_PRESET_SAVE:
955 ShowSavePresetWindow((this->preset == -1) ? nullptr : this->grf_presets[this->preset].c_str());
956 break;
958 case WID_NS_PRESET_DELETE:
959 if (this->preset == -1) return;
961 DeleteGRFPresetFromConfig(this->grf_presets[this->preset].c_str());
962 this->grf_presets = GetGRFPresetList();
963 this->preset = -1;
964 this->InvalidateData();
965 this->CloseChildWindows(WC_QUERY_STRING); // Remove the parameter query window
966 break;
968 case WID_NS_MOVE_UP: { // Move GRF up
969 if (this->active_sel == nullptr || !this->editable) break;
971 int pos = 0;
972 for (GRFConfig **pc = &this->actives; *pc != nullptr; pc = &(*pc)->next, pos++) {
973 GRFConfig *c = *pc;
974 if (c->next == this->active_sel) {
975 c->next = this->active_sel->next;
976 this->active_sel->next = c;
977 *pc = this->active_sel;
978 break;
981 this->vscroll->ScrollTowards(pos);
982 this->preset = -1;
983 this->InvalidateData(GOID_NEWGRF_LIST_EDITED);
984 break;
987 case WID_NS_MOVE_DOWN: { // Move GRF down
988 if (this->active_sel == nullptr || !this->editable) break;
990 int pos = 1; // Start at 1 as we swap the selected newgrf with the next one
991 for (GRFConfig **pc = &this->actives; *pc != nullptr; pc = &(*pc)->next, pos++) {
992 GRFConfig *c = *pc;
993 if (c == this->active_sel) {
994 *pc = c->next;
995 c->next = c->next->next;
996 (*pc)->next = c;
997 break;
1000 this->vscroll->ScrollTowards(pos);
1001 this->preset = -1;
1002 this->InvalidateData(GOID_NEWGRF_LIST_EDITED);
1003 break;
1006 case WID_NS_FILE_LIST: { // Select an active GRF.
1007 ResetObjectToPlace();
1009 uint i = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_NS_FILE_LIST);
1011 GRFConfig *c;
1012 for (c = this->actives; c != nullptr && i > 0; c = c->next, i--) {}
1014 if (this->active_sel != c) {
1015 CloseWindowByClass(WC_GRF_PARAMETERS);
1016 CloseWindowByClass(WC_TEXTFILE);
1018 this->active_sel = c;
1019 this->avail_sel = nullptr;
1020 this->avail_pos = -1;
1022 this->InvalidateData();
1023 if (click_count == 1) {
1024 if (this->editable && this->active_sel != nullptr) SetObjectToPlaceWnd(SPR_CURSOR_MOUSE, PAL_NONE, HT_DRAG, this);
1025 break;
1027 /* With double click, continue */
1028 FALLTHROUGH;
1031 case WID_NS_REMOVE: { // Remove GRF
1032 if (this->active_sel == nullptr || !this->editable) break;
1033 CloseWindowByClass(WC_GRF_PARAMETERS);
1034 CloseWindowByClass(WC_TEXTFILE);
1036 /* Choose the next GRF file to be the selected file. */
1037 GRFConfig *newsel = this->active_sel->next;
1038 for (GRFConfig **pc = &this->actives; *pc != nullptr; pc = &(*pc)->next) {
1039 GRFConfig *c = *pc;
1040 /* If the new selection is empty (i.e. we're deleting the last item
1041 * in the list, pick the file just before the selected file */
1042 if (newsel == nullptr && c->next == this->active_sel) newsel = c;
1044 if (c == this->active_sel) {
1045 if (newsel == c) newsel = nullptr;
1047 *pc = c->next;
1048 delete c;
1049 break;
1053 this->active_sel = newsel;
1054 this->preset = -1;
1055 this->avail_pos = -1;
1056 this->avail_sel = nullptr;
1057 this->avails.ForceRebuild();
1058 this->InvalidateData(GOID_NEWGRF_LIST_EDITED);
1059 break;
1062 case WID_NS_UPGRADE: { // Upgrade GRF.
1063 if (!this->editable || this->actives == nullptr) break;
1064 UpgradeCurrent();
1065 this->InvalidateData(GOID_NEWGRF_LIST_EDITED);
1066 break;
1069 case WID_NS_AVAIL_LIST: { // Select a non-active GRF.
1070 ResetObjectToPlace();
1072 uint i = this->vscroll2->GetScrolledRowFromWidget(pt.y, this, WID_NS_AVAIL_LIST);
1073 this->active_sel = nullptr;
1074 CloseWindowByClass(WC_GRF_PARAMETERS);
1075 if (i < this->avails.size()) {
1076 if (this->avail_sel != this->avails[i]) CloseWindowByClass(WC_TEXTFILE);
1077 this->avail_sel = this->avails[i];
1078 this->avail_pos = i;
1080 this->InvalidateData();
1081 if (click_count == 1) {
1082 if (this->editable && this->avail_sel != nullptr && !HasBit(this->avail_sel->flags, GCF_INVALID)) SetObjectToPlaceWnd(SPR_CURSOR_MOUSE, PAL_NONE, HT_DRAG, this);
1083 break;
1085 /* With double click, continue */
1086 FALLTHROUGH;
1089 case WID_NS_ADD:
1090 if (this->avail_sel == nullptr || !this->editable || HasBit(this->avail_sel->flags, GCF_INVALID)) break;
1092 this->AddGRFToActive();
1093 break;
1095 case WID_NS_APPLY_CHANGES: // Apply changes made to GRF list
1096 if (!this->editable) break;
1097 if (this->execute) {
1098 ShowQuery(
1099 STR_NEWGRF_POPUP_CAUTION_CAPTION,
1100 STR_NEWGRF_CONFIRMATION_TEXT,
1101 this,
1102 NewGRFConfirmationCallback
1104 } else {
1105 CopyGRFConfigList(this->orig_list, this->actives, true);
1106 ResetGRFConfig(false);
1107 ReloadNewGRFData();
1108 this->InvalidateData(GOID_NEWGRF_CHANGES_APPLIED);
1110 this->CloseChildWindows(WC_QUERY_STRING); // Remove the parameter query window
1111 break;
1113 case WID_NS_VIEW_PARAMETERS:
1114 case WID_NS_SET_PARAMETERS: { // Edit parameters
1115 if (this->active_sel == nullptr || !this->show_params || this->active_sel->num_valid_params == 0) break;
1117 OpenGRFParameterWindow(this->active_sel, this->editable);
1118 this->InvalidateData(GOID_NEWGRF_CHANGES_MADE);
1119 break;
1122 case WID_NS_TOGGLE_PALETTE:
1123 if (this->active_sel != nullptr && this->editable) {
1124 this->active_sel->palette ^= GRFP_USE_MASK;
1125 this->SetDirty();
1126 this->InvalidateData(GOID_NEWGRF_CHANGES_MADE);
1128 break;
1130 case WID_NS_CONTENT_DOWNLOAD:
1131 case WID_NS_CONTENT_DOWNLOAD2:
1132 if (!_network_available) {
1133 ShowErrorMessage(STR_NETWORK_ERROR_NOTAVAILABLE, INVALID_STRING_ID, WL_ERROR);
1134 } else {
1135 this->CloseChildWindows(WC_QUERY_STRING); // Remove the parameter query window
1137 ShowMissingContentWindow(this->actives);
1139 break;
1141 case WID_NS_RESCAN_FILES:
1142 case WID_NS_RESCAN_FILES2:
1143 RequestNewGRFScan(this);
1144 break;
1148 void OnNewGRFsScanned() override
1150 if (this->active_sel == nullptr) CloseWindowByClass(WC_TEXTFILE);
1151 this->avail_sel = nullptr;
1152 this->avail_pos = -1;
1153 this->avails.ForceRebuild();
1154 this->CloseChildWindows(WC_QUERY_STRING); // Remove the parameter query window
1157 void OnDropdownSelect(int widget, int index) override
1159 if (!this->editable) return;
1161 ClearGRFConfigList(&this->actives);
1162 this->preset = index;
1164 if (index != -1) {
1165 this->actives = LoadGRFPresetFromConfig(this->grf_presets[index].c_str());
1167 this->avails.ForceRebuild();
1169 ResetObjectToPlace();
1170 CloseWindowByClass(WC_GRF_PARAMETERS);
1171 CloseWindowByClass(WC_TEXTFILE);
1172 this->active_sel = nullptr;
1173 this->InvalidateData(GOID_NEWGRF_CHANGES_MADE);
1176 void OnQueryTextFinished(char *str) override
1178 if (str == nullptr) return;
1180 SaveGRFPresetToConfig(str, this->actives);
1181 this->grf_presets = GetGRFPresetList();
1183 /* Switch to this preset */
1184 for (uint i = 0; i < this->grf_presets.size(); i++) {
1185 if (this->grf_presets[i] == str) {
1186 this->preset = i;
1187 break;
1191 this->InvalidateData();
1195 * Updates the scroll bars for the active and inactive NewGRF lists.
1197 void UpdateScrollBars()
1199 /* Update scrollbars */
1200 int i = 0;
1201 for (const GRFConfig *c = this->actives; c != nullptr; c = c->next, i++) {}
1203 this->vscroll->SetCount(i + 1); // Reserve empty space for drag and drop handling.
1205 if (this->avail_pos >= 0) this->vscroll2->ScrollTowards(this->avail_pos);
1209 * Some data on this window has become invalid.
1210 * @param data Information about the changed data. @see GameOptionsInvalidationData
1211 * @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.
1213 void OnInvalidateData(int data = 0, bool gui_scope = true) override
1215 if (!gui_scope) return;
1216 switch (data) {
1217 default:
1218 /* Nothing important to do */
1219 break;
1221 case GOID_NEWGRF_RESCANNED:
1222 /* Search the list for items that are now found and mark them as such. */
1223 for (GRFConfig **l = &this->actives; *l != nullptr; l = &(*l)->next) {
1224 GRFConfig *c = *l;
1225 bool compatible = HasBit(c->flags, GCF_COMPATIBLE);
1226 if (c->status != GCS_NOT_FOUND && !compatible) continue;
1228 const GRFConfig *f = FindGRFConfig(c->ident.grfid, FGCM_EXACT, compatible ? c->original_md5sum : c->ident.md5sum);
1229 if (f == nullptr || HasBit(f->flags, GCF_INVALID)) continue;
1231 *l = new GRFConfig(*f);
1232 (*l)->next = c->next;
1234 if (this->active_sel == c) this->active_sel = *l;
1236 delete c;
1239 this->avails.ForceRebuild();
1240 FALLTHROUGH;
1242 case GOID_NEWGRF_CURRENT_LOADED:
1243 this->modified = false;
1244 UpdateScrollBars();
1245 break;
1247 case GOID_NEWGRF_LIST_EDITED:
1248 this->preset = -1;
1249 FALLTHROUGH;
1251 case GOID_NEWGRF_CHANGES_MADE:
1252 UpdateScrollBars();
1254 /* Changes have been made to the list of active NewGRFs */
1255 this->modified = true;
1257 break;
1259 case GOID_NEWGRF_CHANGES_APPLIED:
1260 /* No changes have been made to the list of active NewGRFs since the last time the changes got applied */
1261 this->modified = false;
1262 break;
1265 this->BuildAvailables();
1267 this->SetWidgetDisabledState(WID_NS_APPLY_CHANGES, !((this->editable && this->modified) || _settings_client.gui.newgrf_developer_tools));
1268 this->SetWidgetsDisabledState(!this->editable,
1269 WID_NS_PRESET_LIST,
1270 WID_NS_TOGGLE_PALETTE,
1271 WIDGET_LIST_END
1273 this->SetWidgetDisabledState(WID_NS_ADD, !this->editable || this->avail_sel == nullptr || HasBit(this->avail_sel->flags, GCF_INVALID));
1274 this->SetWidgetDisabledState(WID_NS_UPGRADE, !this->editable || this->actives == nullptr || !this->CanUpgradeCurrent());
1276 bool disable_all = this->active_sel == nullptr || !this->editable;
1277 this->SetWidgetsDisabledState(disable_all,
1278 WID_NS_REMOVE,
1279 WID_NS_MOVE_UP,
1280 WID_NS_MOVE_DOWN,
1281 WIDGET_LIST_END
1284 const GRFConfig *c = (this->avail_sel == nullptr) ? this->active_sel : this->avail_sel;
1285 for (TextfileType tft = TFT_BEGIN; tft < TFT_END; tft++) {
1286 this->SetWidgetDisabledState(WID_NS_NEWGRF_TEXTFILE + tft, c == nullptr || c->GetTextfile(tft) == nullptr);
1288 this->SetWidgetDisabledState(WID_NS_OPEN_URL, c == nullptr || StrEmpty(c->GetURL()));
1290 this->SetWidgetDisabledState(WID_NS_SET_PARAMETERS, !this->show_params || this->active_sel == nullptr || this->active_sel->num_valid_params == 0);
1291 this->SetWidgetDisabledState(WID_NS_VIEW_PARAMETERS, !this->show_params || this->active_sel == nullptr || this->active_sel->num_valid_params == 0);
1292 this->SetWidgetDisabledState(WID_NS_TOGGLE_PALETTE, disable_all ||
1293 (!(_settings_client.gui.newgrf_developer_tools || _settings_client.gui.scenario_developer) && ((c->palette & GRFP_GRF_MASK) != GRFP_GRF_UNSET)));
1295 if (!disable_all) {
1296 /* All widgets are now enabled, so disable widgets we can't use */
1297 if (this->active_sel == this->actives) this->DisableWidget(WID_NS_MOVE_UP);
1298 if (this->active_sel->next == nullptr) this->DisableWidget(WID_NS_MOVE_DOWN);
1301 this->SetWidgetDisabledState(WID_NS_PRESET_DELETE, this->preset == -1);
1303 bool has_missing = false;
1304 bool has_compatible = false;
1305 for (const GRFConfig *c = this->actives; !has_missing && c != nullptr; c = c->next) {
1306 has_missing |= c->status == GCS_NOT_FOUND;
1307 has_compatible |= HasBit(c->flags, GCF_COMPATIBLE);
1309 uint32 widget_data;
1310 StringID tool_tip;
1311 if (has_missing || has_compatible) {
1312 widget_data = STR_NEWGRF_SETTINGS_FIND_MISSING_CONTENT_BUTTON;
1313 tool_tip = STR_NEWGRF_SETTINGS_FIND_MISSING_CONTENT_TOOLTIP;
1314 } else {
1315 widget_data = STR_INTRO_ONLINE_CONTENT;
1316 tool_tip = STR_INTRO_TOOLTIP_ONLINE_CONTENT;
1318 this->GetWidget<NWidgetCore>(WID_NS_CONTENT_DOWNLOAD)->widget_data = widget_data;
1319 this->GetWidget<NWidgetCore>(WID_NS_CONTENT_DOWNLOAD)->tool_tip = tool_tip;
1320 this->GetWidget<NWidgetCore>(WID_NS_CONTENT_DOWNLOAD2)->widget_data = widget_data;
1321 this->GetWidget<NWidgetCore>(WID_NS_CONTENT_DOWNLOAD2)->tool_tip = tool_tip;
1323 this->SetWidgetDisabledState(WID_NS_PRESET_SAVE, has_missing);
1326 EventState OnKeyPress(WChar key, uint16 keycode) override
1328 if (!this->editable) return ES_NOT_HANDLED;
1330 if (this->vscroll2->UpdateListPositionOnKeyPress(this->avail_pos, keycode) == ES_NOT_HANDLED) return ES_NOT_HANDLED;
1332 if (this->avail_pos >= 0) {
1333 this->active_sel = nullptr;
1334 CloseWindowByClass(WC_GRF_PARAMETERS);
1335 if (this->avail_sel != this->avails[this->avail_pos]) CloseWindowByClass(WC_TEXTFILE);
1336 this->avail_sel = this->avails[this->avail_pos];
1337 this->vscroll2->ScrollTowards(this->avail_pos);
1338 this->InvalidateData(0);
1341 return ES_HANDLED;
1344 void OnEditboxChanged(int wid) override
1346 if (!this->editable) return;
1348 string_filter.SetFilterTerm(this->filter_editbox.text.buf);
1349 this->avails.SetFilterState(!string_filter.IsEmpty());
1350 this->avails.ForceRebuild();
1351 this->InvalidateData(0);
1354 void OnDragDrop(Point pt, int widget) override
1356 if (!this->editable) return;
1358 if (widget == WID_NS_FILE_LIST) {
1359 if (this->active_sel != nullptr) {
1360 /* Get pointer to the selected file in the active list. */
1361 int from_pos = 0;
1362 GRFConfig **from_prev;
1363 for (from_prev = &this->actives; *from_prev != this->active_sel; from_prev = &(*from_prev)->next, from_pos++) {}
1365 /* Gets the drag-and-drop destination offset. Ignore the last dummy line. */
1366 int to_pos = std::min(this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_NS_FILE_LIST), this->vscroll->GetCount() - 2);
1367 if (to_pos != from_pos) { // Don't move NewGRF file over itself.
1368 /* Get pointer to destination position. */
1369 GRFConfig **to_prev = &this->actives;
1370 for (int i = from_pos < to_pos ? -1 : 0; *to_prev != nullptr && i < to_pos; to_prev = &(*to_prev)->next, i++) {}
1372 /* Detach NewGRF file from its original position. */
1373 *from_prev = this->active_sel->next;
1375 /* Attach NewGRF file to its new position. */
1376 this->active_sel->next = *to_prev;
1377 *to_prev = this->active_sel;
1379 this->vscroll->ScrollTowards(to_pos);
1380 this->preset = -1;
1381 this->InvalidateData();
1383 } else if (this->avail_sel != nullptr) {
1384 int to_pos = std::min(this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_NS_FILE_LIST), this->vscroll->GetCount() - 1);
1385 this->AddGRFToActive(to_pos);
1387 } else if (widget == WID_NS_AVAIL_LIST && this->active_sel != nullptr) {
1388 /* Remove active NewGRF file by dragging it over available list. */
1389 Point dummy = {-1, -1};
1390 this->OnClick(dummy, WID_NS_REMOVE, 1);
1393 ResetObjectToPlace();
1395 if (this->active_over != -1) {
1396 /* End of drag-and-drop, hide dragged destination highlight. */
1397 this->SetWidgetDirty(this->active_over == -2 ? WID_NS_AVAIL_LIST : WID_NS_FILE_LIST);
1398 this->active_over = -1;
1402 void OnMouseDrag(Point pt, int widget) override
1404 if (!this->editable) return;
1406 if (widget == WID_NS_FILE_LIST && (this->active_sel != nullptr || this->avail_sel != nullptr)) {
1407 /* An NewGRF file is dragged over the active list. */
1408 int to_pos = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_NS_FILE_LIST);
1409 /* Skip the last dummy line if the source is from the active list. */
1410 to_pos = std::min(to_pos, this->vscroll->GetCount() - (this->active_sel != nullptr ? 2 : 1));
1412 if (to_pos != this->active_over) {
1413 this->active_over = to_pos;
1414 this->SetWidgetDirty(WID_NS_FILE_LIST);
1416 } else if (widget == WID_NS_AVAIL_LIST && this->active_sel != nullptr) {
1417 this->active_over = -2;
1418 this->SetWidgetDirty(WID_NS_AVAIL_LIST);
1419 } else if (this->active_over != -1) {
1420 this->SetWidgetDirty(this->active_over == -2 ? WID_NS_AVAIL_LIST : WID_NS_FILE_LIST);
1421 this->active_over = -1;
1425 private:
1426 /** Sort grfs by name. */
1427 static bool NameSorter(const GRFConfig * const &a, const GRFConfig * const &b)
1429 int i = strnatcmp(a->GetName(), b->GetName(), true); // Sort by name (natural sorting).
1430 if (i != 0) return i < 0;
1432 i = a->version - b->version;
1433 if (i != 0) return i < 0;
1435 return memcmp(a->ident.md5sum, b->ident.md5sum, lengthof(b->ident.md5sum)) < 0;
1438 /** Filter grfs by tags/name */
1439 static bool CDECL TagNameFilter(const GRFConfig * const *a, StringFilter &filter)
1441 filter.ResetState();
1442 filter.AddLine((*a)->GetName());
1443 filter.AddLine((*a)->filename);
1444 filter.AddLine((*a)->GetDescription());
1445 return filter.GetState();;
1448 void BuildAvailables()
1450 if (!this->avails.NeedRebuild()) return;
1452 this->avails.clear();
1454 for (const GRFConfig *c = _all_grfs; c != nullptr; c = c->next) {
1455 bool found = false;
1456 for (const GRFConfig *grf = this->actives; grf != nullptr && !found; grf = grf->next) found = grf->ident.HasGrfIdentifier(c->ident.grfid, c->ident.md5sum);
1457 if (found) continue;
1459 if (_settings_client.gui.newgrf_show_old_versions) {
1460 this->avails.push_back(c);
1461 } else {
1462 const GRFConfig *best = FindGRFConfig(c->ident.grfid, HasBit(c->flags, GCF_INVALID) ? FGCM_NEWEST : FGCM_NEWEST_VALID);
1464 * If the best version is 0, then all NewGRF with this GRF ID
1465 * have version 0, so for backward compatibility reasons we
1466 * want to show them all.
1467 * If we are the best version, then we definitely want to
1468 * show that NewGRF!.
1470 if (best->version == 0 || best->ident.HasGrfIdentifier(c->ident.grfid, c->ident.md5sum)) {
1471 this->avails.push_back(c);
1476 this->avails.Filter(this->string_filter);
1477 this->avails.shrink_to_fit();
1478 this->avails.RebuildDone();
1479 this->avails.Sort();
1481 if (this->avail_sel != nullptr) {
1482 this->avail_pos = find_index(this->avails, this->avail_sel);
1483 if (this->avail_pos == -1) {
1484 this->avail_sel = nullptr;
1488 this->vscroll2->SetCount((uint)this->avails.size()); // Update the scrollbar
1492 * Insert a GRF into the active list.
1493 * @param ins_pos Insert GRF at this position.
1494 * @return True if the GRF was successfully added.
1496 bool AddGRFToActive(int ins_pos = -1)
1498 if (this->avail_sel == nullptr || !this->editable || HasBit(this->avail_sel->flags, GCF_INVALID)) return false;
1500 CloseWindowByClass(WC_TEXTFILE);
1502 uint count = 0;
1503 GRFConfig **entry = nullptr;
1504 GRFConfig **list;
1505 /* Find last entry in the list, checking for duplicate grfid on the way */
1506 for (list = &this->actives; *list != nullptr; list = &(*list)->next, ins_pos--) {
1507 if (ins_pos == 0) entry = list; // Insert position? Save.
1508 if ((*list)->ident.grfid == this->avail_sel->ident.grfid) {
1509 ShowErrorMessage(STR_NEWGRF_DUPLICATE_GRFID, INVALID_STRING_ID, WL_INFO);
1510 return false;
1512 if (!HasBit((*list)->flags, GCF_STATIC)) count++;
1514 if (entry == nullptr) entry = list;
1515 if (count >= NETWORK_MAX_GRF_COUNT) {
1516 ShowErrorMessage(STR_NEWGRF_TOO_MANY_NEWGRFS, INVALID_STRING_ID, WL_INFO);
1517 return false;
1520 GRFConfig *c = new GRFConfig(*this->avail_sel); // Copy GRF details from scanned list.
1521 c->SetParameterDefaults();
1523 /* Insert GRF config to configuration list. */
1524 c->next = *entry;
1525 *entry = c;
1527 /* Select next (or previous, if last one) item in the list. */
1528 int new_pos = this->avail_pos + 1;
1529 if (new_pos >= (int)this->avails.size()) new_pos = this->avail_pos - 1;
1530 this->avail_pos = new_pos;
1531 if (new_pos >= 0) this->avail_sel = this->avails[new_pos];
1533 this->avails.ForceRebuild();
1534 this->InvalidateData(GOID_NEWGRF_LIST_EDITED);
1535 return true;
1540 * Show the content list window with all missing grfs from the given list.
1541 * @param list The list of grfs to check for missing / not exactly matching ones.
1543 void ShowMissingContentWindow(const GRFConfig *list)
1545 /* Only show the things in the current list, or everything when nothing's selected */
1546 ContentVector cv;
1547 for (const GRFConfig *c = list; c != nullptr; c = c->next) {
1548 if (c->status != GCS_NOT_FOUND && !HasBit(c->flags, GCF_COMPATIBLE)) continue;
1550 ContentInfo *ci = new ContentInfo();
1551 ci->type = CONTENT_TYPE_NEWGRF;
1552 ci->state = ContentInfo::DOES_NOT_EXIST;
1553 ci->name = c->GetName();
1554 ci->unique_id = BSWAP32(c->ident.grfid);
1555 memcpy(ci->md5sum, HasBit(c->flags, GCF_COMPATIBLE) ? c->original_md5sum : c->ident.md5sum, sizeof(ci->md5sum));
1556 cv.push_back(ci);
1558 ShowNetworkContentListWindow(cv.size() == 0 ? nullptr : &cv, CONTENT_TYPE_NEWGRF);
1561 Listing NewGRFWindow::last_sorting = {false, 0};
1562 Filtering NewGRFWindow::last_filtering = {false, 0};
1564 NewGRFWindow::GUIGRFConfigList::SortFunction * const NewGRFWindow::sorter_funcs[] = {
1565 &NameSorter,
1568 NewGRFWindow::GUIGRFConfigList::FilterFunction * const NewGRFWindow::filter_funcs[] = {
1569 &TagNameFilter,
1573 * Custom nested widget container for the NewGRF gui.
1574 * Depending on the space in the gui, it uses either
1575 * - two column mode, put the #acs and the #avs underneath each other and the #inf next to it, or
1576 * - three column mode, put the #avs, #acs, and #inf each in its own column.
1578 class NWidgetNewGRFDisplay : public NWidgetContainer {
1579 public:
1580 static const uint INTER_LIST_SPACING; ///< Empty vertical space between both lists in the 2 column mode.
1581 static const uint INTER_COLUMN_SPACING; ///< Empty horizontal space between two columns.
1582 static const uint MAX_EXTRA_INFO_WIDTH; ///< Maximal additional width given to the panel.
1583 static const uint MIN_EXTRA_FOR_3_COLUMNS; ///< Minimal additional width needed before switching to 3 columns.
1585 NWidgetBase *avs; ///< Widget with the available grfs list and buttons.
1586 NWidgetBase *acs; ///< Widget with the active grfs list and buttons.
1587 NWidgetBase *inf; ///< Info panel.
1588 bool editable; ///< Editable status of the parent NewGRF window (if \c false, drop all widgets that make the window editable).
1590 NWidgetNewGRFDisplay(NWidgetBase *avs, NWidgetBase *acs, NWidgetBase *inf) : NWidgetContainer(NWID_HORIZONTAL)
1592 this->avs = avs;
1593 this->acs = acs;
1594 this->inf = inf;
1596 this->Add(this->avs);
1597 this->Add(this->acs);
1598 this->Add(this->inf);
1600 this->editable = true; // Temporary setting, 'real' value is set in SetupSmallestSize().
1603 void SetupSmallestSize(Window *w, bool init_array) override
1605 /* Copy state flag from the window. */
1606 assert(dynamic_cast<NewGRFWindow *>(w) != nullptr);
1607 NewGRFWindow *ngw = (NewGRFWindow *)w;
1608 this->editable = ngw->editable;
1610 this->avs->SetupSmallestSize(w, init_array);
1611 this->acs->SetupSmallestSize(w, init_array);
1612 this->inf->SetupSmallestSize(w, init_array);
1614 uint min_avs_width = this->avs->smallest_x + this->avs->padding_left + this->avs->padding_right;
1615 uint min_acs_width = this->acs->smallest_x + this->acs->padding_left + this->acs->padding_right;
1616 uint min_inf_width = this->inf->smallest_x + this->inf->padding_left + this->inf->padding_right;
1618 uint min_avs_height = this->avs->smallest_y + this->avs->padding_top + this->avs->padding_bottom;
1619 uint min_acs_height = this->acs->smallest_y + this->acs->padding_top + this->acs->padding_bottom;
1620 uint min_inf_height = this->inf->smallest_y + this->inf->padding_top + this->inf->padding_bottom;
1622 /* Smallest window is in two column mode. */
1623 this->smallest_x = std::max(min_avs_width, min_acs_width) + INTER_COLUMN_SPACING + min_inf_width;
1624 this->smallest_y = std::max(min_inf_height, min_acs_height + INTER_LIST_SPACING + min_avs_height);
1626 /* Filling. */
1627 this->fill_x = LeastCommonMultiple(this->avs->fill_x, this->acs->fill_x);
1628 if (this->inf->fill_x > 0 && (this->fill_x == 0 || this->fill_x > this->inf->fill_x)) this->fill_x = this->inf->fill_x;
1630 this->fill_y = this->avs->fill_y;
1631 if (this->acs->fill_y > 0 && (this->fill_y == 0 || this->fill_y > this->acs->fill_y)) this->fill_y = this->acs->fill_y;
1632 this->fill_y = LeastCommonMultiple(this->fill_y, this->inf->fill_y);
1634 /* Resizing. */
1635 this->resize_x = LeastCommonMultiple(this->avs->resize_x, this->acs->resize_x);
1636 if (this->inf->resize_x > 0 && (this->resize_x == 0 || this->resize_x > this->inf->resize_x)) this->resize_x = this->inf->resize_x;
1638 this->resize_y = this->avs->resize_y;
1639 if (this->acs->resize_y > 0 && (this->resize_y == 0 || this->resize_y > this->acs->resize_y)) this->resize_y = this->acs->resize_y;
1640 this->resize_y = LeastCommonMultiple(this->resize_y, this->inf->resize_y);
1642 /* Make sure the height suits the 3 column (resp. not-editable) format; the 2 column format can easily fill space between the lists */
1643 this->smallest_y = ComputeMaxSize(min_acs_height, this->smallest_y + this->resize_y - 1, this->resize_y);
1646 void AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl) override
1648 this->StoreSizePosition(sizing, x, y, given_width, given_height);
1650 uint min_avs_width = this->avs->smallest_x + this->avs->padding_left + this->avs->padding_right;
1651 uint min_acs_width = this->acs->smallest_x + this->acs->padding_left + this->acs->padding_right;
1652 uint min_inf_width = this->inf->smallest_x + this->inf->padding_left + this->inf->padding_right;
1654 uint min_list_width = std::max(min_avs_width, min_acs_width); // Smallest width of the lists such that they have equal width (incl padding).
1655 uint avs_extra_width = min_list_width - min_avs_width; // Additional width needed for avs to reach min_list_width.
1656 uint acs_extra_width = min_list_width - min_acs_width; // Additional width needed for acs to reach min_list_width.
1658 /* Use 2 or 3 columns? */
1659 uint min_three_columns = min_avs_width + min_acs_width + min_inf_width + 2 * INTER_COLUMN_SPACING;
1660 uint min_two_columns = min_list_width + min_inf_width + INTER_COLUMN_SPACING;
1661 bool use_three_columns = this->editable && (min_three_columns + MIN_EXTRA_FOR_3_COLUMNS <= given_width);
1663 /* Info panel is a separate column in both modes. Compute its width first. */
1664 uint extra_width, inf_width;
1665 if (use_three_columns) {
1666 extra_width = given_width - min_three_columns;
1667 inf_width = std::min(MAX_EXTRA_INFO_WIDTH, extra_width / 2);
1668 } else {
1669 extra_width = given_width - min_two_columns;
1670 inf_width = std::min(MAX_EXTRA_INFO_WIDTH, extra_width / 2);
1672 inf_width = ComputeMaxSize(this->inf->smallest_x, this->inf->smallest_x + inf_width, this->inf->GetHorizontalStepSize(sizing));
1673 extra_width -= inf_width - this->inf->smallest_x;
1675 uint inf_height = ComputeMaxSize(this->inf->smallest_y, given_height, this->inf->GetVerticalStepSize(sizing));
1677 if (use_three_columns) {
1678 /* Three column display, first make both lists equally wide, then divide whatever is left between both lists.
1679 * Only keep track of what avs gets, all other space goes to acs. */
1680 uint avs_width = std::min(avs_extra_width, extra_width);
1681 extra_width -= avs_width;
1682 extra_width -= std::min(acs_extra_width, extra_width);
1683 avs_width += extra_width / 2;
1685 avs_width = ComputeMaxSize(this->avs->smallest_x, this->avs->smallest_x + avs_width, this->avs->GetHorizontalStepSize(sizing));
1687 uint acs_width = given_width - // Remaining space, including horizontal padding.
1688 inf_width - this->inf->padding_left - this->inf->padding_right -
1689 avs_width - this->avs->padding_left - this->avs->padding_right - 2 * INTER_COLUMN_SPACING;
1690 acs_width = ComputeMaxSize(min_acs_width, acs_width, this->acs->GetHorizontalStepSize(sizing)) -
1691 this->acs->padding_left - this->acs->padding_right;
1693 /* Never use fill_y on these; the minimal size is chosen, so that the 3 column view looks nice */
1694 uint avs_height = ComputeMaxSize(this->avs->smallest_y, given_height, this->avs->resize_y);
1695 uint acs_height = ComputeMaxSize(this->acs->smallest_y, given_height, this->acs->resize_y);
1697 /* Assign size and position to the children. */
1698 if (rtl) {
1699 x += this->inf->padding_left;
1700 this->inf->AssignSizePosition(sizing, x, y + this->inf->padding_top, inf_width, inf_height, rtl);
1701 x += inf_width + this->inf->padding_right + INTER_COLUMN_SPACING;
1702 } else {
1703 x += this->avs->padding_left;
1704 this->avs->AssignSizePosition(sizing, x, y + this->avs->padding_top, avs_width, avs_height, rtl);
1705 x += avs_width + this->avs->padding_right + INTER_COLUMN_SPACING;
1708 x += this->acs->padding_left;
1709 this->acs->AssignSizePosition(sizing, x, y + this->acs->padding_top, acs_width, acs_height, rtl);
1710 x += acs_width + this->acs->padding_right + INTER_COLUMN_SPACING;
1712 if (rtl) {
1713 x += this->avs->padding_left;
1714 this->avs->AssignSizePosition(sizing, x, y + this->avs->padding_top, avs_width, avs_height, rtl);
1715 } else {
1716 x += this->inf->padding_left;
1717 this->inf->AssignSizePosition(sizing, x, y + this->inf->padding_top, inf_width, inf_height, rtl);
1719 } else {
1720 /* Two columns, all space in extra_width goes to both lists. Since the lists are underneath each other,
1721 * the column is min_list_width wide at least. */
1722 uint avs_width = ComputeMaxSize(this->avs->smallest_x, this->avs->smallest_x + avs_extra_width + extra_width,
1723 this->avs->GetHorizontalStepSize(sizing));
1724 uint acs_width = ComputeMaxSize(this->acs->smallest_x, this->acs->smallest_x + acs_extra_width + extra_width,
1725 this->acs->GetHorizontalStepSize(sizing));
1727 uint min_avs_height = (!this->editable) ? 0 : this->avs->smallest_y + this->avs->padding_top + this->avs->padding_bottom + INTER_LIST_SPACING;
1728 uint min_acs_height = this->acs->smallest_y + this->acs->padding_top + this->acs->padding_bottom;
1729 uint extra_height = given_height - min_acs_height - min_avs_height;
1731 /* Never use fill_y on these; instead use the INTER_LIST_SPACING as filler */
1732 uint avs_height = ComputeMaxSize(this->avs->smallest_y, this->avs->smallest_y + extra_height / 2, this->avs->resize_y);
1733 if (this->editable) extra_height -= avs_height - this->avs->smallest_y;
1734 uint acs_height = ComputeMaxSize(this->acs->smallest_y, this->acs->smallest_y + extra_height, this->acs->resize_y);
1736 /* Assign size and position to the children. */
1737 if (rtl) {
1738 x += this->inf->padding_left;
1739 this->inf->AssignSizePosition(sizing, x, y + this->inf->padding_top, inf_width, inf_height, rtl);
1740 x += inf_width + this->inf->padding_right + INTER_COLUMN_SPACING;
1742 this->acs->AssignSizePosition(sizing, x + this->acs->padding_left, y + this->acs->padding_top, acs_width, acs_height, rtl);
1743 if (this->editable) {
1744 this->avs->AssignSizePosition(sizing, x + this->avs->padding_left, y + given_height - avs_height - this->avs->padding_bottom, avs_width, avs_height, rtl);
1745 } else {
1746 this->avs->AssignSizePosition(sizing, 0, 0, this->avs->smallest_x, this->avs->smallest_y, rtl);
1748 } else {
1749 this->acs->AssignSizePosition(sizing, x + this->acs->padding_left, y + this->acs->padding_top, acs_width, acs_height, rtl);
1750 if (this->editable) {
1751 this->avs->AssignSizePosition(sizing, x + this->avs->padding_left, y + given_height - avs_height - this->avs->padding_bottom, avs_width, avs_height, rtl);
1752 } else {
1753 this->avs->AssignSizePosition(sizing, 0, 0, this->avs->smallest_x, this->avs->smallest_y, rtl);
1755 uint dx = this->acs->current_x + this->acs->padding_left + this->acs->padding_right;
1756 if (this->editable) {
1757 dx = std::max(dx, this->avs->current_x + this->avs->padding_left + this->avs->padding_right);
1759 x += dx + INTER_COLUMN_SPACING + this->inf->padding_left;
1760 this->inf->AssignSizePosition(sizing, x, y + this->inf->padding_top, inf_width, inf_height, rtl);
1765 NWidgetCore *GetWidgetFromPos(int x, int y) override
1767 if (!IsInsideBS(x, this->pos_x, this->current_x) || !IsInsideBS(y, this->pos_y, this->current_y)) return nullptr;
1769 NWidgetCore *nw = (this->editable) ? this->avs->GetWidgetFromPos(x, y) : nullptr;
1770 if (nw == nullptr) nw = this->acs->GetWidgetFromPos(x, y);
1771 if (nw == nullptr) nw = this->inf->GetWidgetFromPos(x, y);
1772 return nw;
1775 void Draw(const Window *w) override
1777 if (this->editable) this->avs->Draw(w);
1778 this->acs->Draw(w);
1779 this->inf->Draw(w);
1783 const uint NWidgetNewGRFDisplay::INTER_LIST_SPACING = WD_RESIZEBOX_WIDTH + 1;
1784 const uint NWidgetNewGRFDisplay::INTER_COLUMN_SPACING = WD_RESIZEBOX_WIDTH;
1785 const uint NWidgetNewGRFDisplay::MAX_EXTRA_INFO_WIDTH = 150;
1786 const uint NWidgetNewGRFDisplay::MIN_EXTRA_FOR_3_COLUMNS = 50;
1788 static const NWidgetPart _nested_newgrf_actives_widgets[] = {
1789 /* Left side, presets. */
1790 NWidget(NWID_HORIZONTAL),
1791 NWidget(WWT_TEXT, COLOUR_MAUVE), SetDataTip(STR_NEWGRF_SETTINGS_SELECT_PRESET, STR_NULL),
1792 SetPadding(0, WD_FRAMETEXT_RIGHT, 0, 0),
1793 NWidget(WWT_DROPDOWN, COLOUR_YELLOW, WID_NS_PRESET_LIST), SetFill(1, 0), SetResize(1, 0),
1794 SetDataTip(STR_JUST_STRING, STR_NEWGRF_SETTINGS_PRESET_LIST_TOOLTIP),
1795 EndContainer(),
1796 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
1797 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_PRESET_SAVE), SetFill(1, 0), SetResize(1, 0),
1798 SetDataTip(STR_NEWGRF_SETTINGS_PRESET_SAVE, STR_NEWGRF_SETTINGS_PRESET_SAVE_TOOLTIP),
1799 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_PRESET_DELETE), SetFill(1, 0), SetResize(1, 0),
1800 SetDataTip(STR_NEWGRF_SETTINGS_PRESET_DELETE, STR_NEWGRF_SETTINGS_PRESET_DELETE_TOOLTIP),
1801 EndContainer(),
1803 NWidget(NWID_SPACER), SetMinimalSize(0, WD_RESIZEBOX_WIDTH), SetResize(1, 0), SetFill(1, 0),
1804 NWidget(WWT_PANEL, COLOUR_MAUVE),
1805 NWidget(WWT_LABEL, COLOUR_MAUVE), SetDataTip(STR_NEWGRF_SETTINGS_ACTIVE_LIST, STR_NULL),
1806 SetFill(1, 0), SetResize(1, 0), SetPadding(3, WD_FRAMETEXT_RIGHT, 0, WD_FRAMETEXT_LEFT),
1807 /* Left side, active grfs. */
1808 NWidget(NWID_HORIZONTAL), SetPadding(0, 2, 0, 2),
1809 NWidget(WWT_PANEL, COLOUR_MAUVE),
1810 NWidget(WWT_INSET, COLOUR_MAUVE, WID_NS_FILE_LIST), SetMinimalSize(100, 1), SetPadding(2, 2, 2, 2),
1811 SetFill(1, 1), SetResize(1, 1), SetScrollbar(WID_NS_SCROLLBAR), SetDataTip(STR_NULL, STR_NEWGRF_SETTINGS_FILE_TOOLTIP),
1812 EndContainer(),
1813 EndContainer(),
1814 NWidget(NWID_VSCROLLBAR, COLOUR_MAUVE, WID_NS_SCROLLBAR),
1815 EndContainer(),
1816 /* Buttons. */
1817 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_NS_SHOW_REMOVE),
1818 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPadding(2, 2, 2, 2), SetPIP(0, WD_RESIZEBOX_WIDTH, 0),
1819 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_REMOVE), SetFill(1, 0), SetResize(1, 0),
1820 SetDataTip(STR_NEWGRF_SETTINGS_REMOVE, STR_NEWGRF_SETTINGS_REMOVE_TOOLTIP),
1821 NWidget(NWID_VERTICAL),
1822 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_MOVE_UP), SetFill(1, 0), SetResize(1, 0),
1823 SetDataTip(STR_NEWGRF_SETTINGS_MOVEUP, STR_NEWGRF_SETTINGS_MOVEUP_TOOLTIP),
1824 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_MOVE_DOWN), SetFill(1, 0), SetResize(1, 0),
1825 SetDataTip(STR_NEWGRF_SETTINGS_MOVEDOWN, STR_NEWGRF_SETTINGS_MOVEDOWN_TOOLTIP),
1826 EndContainer(),
1827 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_UPGRADE), SetFill(1, 0), SetResize(1, 0),
1828 SetDataTip(STR_NEWGRF_SETTINGS_UPGRADE, STR_NEWGRF_SETTINGS_UPGRADE_TOOLTIP),
1829 EndContainer(),
1831 NWidget(NWID_VERTICAL, NC_EQUALSIZE), SetPadding(2, 2, 2, 2),
1832 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_RESCAN_FILES2), SetFill(1, 0), SetResize(1, 0),
1833 SetDataTip(STR_NEWGRF_SETTINGS_RESCAN_FILES, STR_NEWGRF_SETTINGS_RESCAN_FILES_TOOLTIP),
1834 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_CONTENT_DOWNLOAD2), SetFill(1, 0), SetResize(1, 0),
1835 SetDataTip(STR_INTRO_ONLINE_CONTENT, STR_INTRO_TOOLTIP_ONLINE_CONTENT),
1836 EndContainer(),
1837 EndContainer(),
1838 EndContainer(),
1841 static const NWidgetPart _nested_newgrf_availables_widgets[] = {
1842 NWidget(WWT_PANEL, COLOUR_MAUVE),
1843 NWidget(WWT_LABEL, COLOUR_MAUVE), SetDataTip(STR_NEWGRF_SETTINGS_INACTIVE_LIST, STR_NULL),
1844 SetFill(1, 0), SetResize(1, 0), SetPadding(3, WD_FRAMETEXT_RIGHT, 0, WD_FRAMETEXT_LEFT),
1845 /* Left side, available grfs, filter edit box. */
1846 NWidget(NWID_HORIZONTAL), SetPadding(WD_TEXTPANEL_TOP, 0, WD_TEXTPANEL_BOTTOM, 0),
1847 SetPIP(WD_FRAMETEXT_LEFT, WD_FRAMETEXT_RIGHT, WD_FRAMETEXT_RIGHT),
1848 NWidget(WWT_TEXT, COLOUR_MAUVE), SetFill(0, 1), SetDataTip(STR_NEWGRF_FILTER_TITLE, STR_NULL),
1849 NWidget(WWT_EDITBOX, COLOUR_MAUVE, WID_NS_FILTER), SetFill(1, 0), SetMinimalSize(50, 12), SetResize(1, 0),
1850 SetDataTip(STR_LIST_FILTER_OSKTITLE, STR_LIST_FILTER_TOOLTIP),
1851 EndContainer(),
1852 /* Left side, available grfs. */
1853 NWidget(NWID_HORIZONTAL), SetPadding(0, 2, 0, 2),
1854 NWidget(WWT_PANEL, COLOUR_MAUVE),
1855 NWidget(WWT_INSET, COLOUR_MAUVE, WID_NS_AVAIL_LIST), SetMinimalSize(100, 1), SetPadding(2, 2, 2, 2),
1856 SetFill(1, 1), SetResize(1, 1), SetScrollbar(WID_NS_SCROLL2BAR),
1857 EndContainer(),
1858 EndContainer(),
1859 NWidget(NWID_VSCROLLBAR, COLOUR_MAUVE, WID_NS_SCROLL2BAR),
1860 EndContainer(),
1861 /* Left side, available grfs, buttons. */
1862 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPadding(2, 2, 2, 2), SetPIP(0, WD_RESIZEBOX_WIDTH, 0),
1863 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_ADD), SetFill(1, 0), SetResize(1, 0),
1864 SetDataTip(STR_NEWGRF_SETTINGS_ADD, STR_NEWGRF_SETTINGS_ADD_FILE_TOOLTIP),
1865 NWidget(NWID_VERTICAL),
1866 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_RESCAN_FILES), SetFill(1, 0), SetResize(1, 0),
1867 SetDataTip(STR_NEWGRF_SETTINGS_RESCAN_FILES, STR_NEWGRF_SETTINGS_RESCAN_FILES_TOOLTIP),
1868 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_CONTENT_DOWNLOAD), SetFill(1, 0), SetResize(1, 0),
1869 SetDataTip(STR_INTRO_ONLINE_CONTENT, STR_INTRO_TOOLTIP_ONLINE_CONTENT),
1870 EndContainer(),
1871 EndContainer(),
1872 EndContainer(),
1875 static const NWidgetPart _nested_newgrf_infopanel_widgets[] = {
1876 /* Right side, info panel. */
1877 NWidget(NWID_VERTICAL), SetPadding(0, 0, 2, 0),
1878 NWidget(WWT_PANEL, COLOUR_MAUVE), SetPadding(0, 0, 2, 0),
1879 NWidget(WWT_EMPTY, COLOUR_MAUVE, WID_NS_NEWGRF_INFO_TITLE), SetFill(1, 0), SetResize(1, 0),
1880 NWidget(WWT_EMPTY, COLOUR_MAUVE, WID_NS_NEWGRF_INFO), SetFill(1, 1), SetResize(1, 1), SetMinimalSize(150, 100),
1881 EndContainer(),
1882 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_OPEN_URL), SetFill(1, 0), SetResize(1, 0),
1883 SetDataTip(STR_CONTENT_OPEN_URL, STR_CONTENT_OPEN_URL_TOOLTIP),
1884 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_NEWGRF_TEXTFILE + TFT_README), SetFill(1, 0), SetResize(1, 0),
1885 SetDataTip(STR_TEXTFILE_VIEW_README, STR_NULL),
1886 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
1887 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_NEWGRF_TEXTFILE + TFT_CHANGELOG), SetFill(1, 0), SetResize(1, 0),
1888 SetDataTip(STR_TEXTFILE_VIEW_CHANGELOG, STR_NULL),
1889 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_NEWGRF_TEXTFILE + TFT_LICENSE), SetFill(1, 0), SetResize(1, 0),
1890 SetDataTip(STR_TEXTFILE_VIEW_LICENCE, STR_NULL),
1891 EndContainer(),
1892 EndContainer(),
1893 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_NS_SHOW_APPLY),
1894 /* Right side, buttons. */
1895 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(0, WD_RESIZEBOX_WIDTH, 0),
1896 NWidget(NWID_VERTICAL),
1897 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_SET_PARAMETERS), SetFill(1, 0), SetResize(1, 0),
1898 SetDataTip(STR_NEWGRF_SETTINGS_SET_PARAMETERS, STR_NULL),
1899 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_TOGGLE_PALETTE), SetFill(1, 0), SetResize(1, 0),
1900 SetDataTip(STR_NEWGRF_SETTINGS_TOGGLE_PALETTE, STR_NEWGRF_SETTINGS_TOGGLE_PALETTE_TOOLTIP),
1901 EndContainer(),
1902 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_APPLY_CHANGES), SetFill(1, 0), SetResize(1, 0),
1903 SetDataTip(STR_NEWGRF_SETTINGS_APPLY_CHANGES, STR_NULL),
1904 EndContainer(),
1905 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_VIEW_PARAMETERS), SetFill(1, 0), SetResize(1, 0),
1906 SetDataTip(STR_NEWGRF_SETTINGS_SHOW_PARAMETERS, STR_NULL),
1907 EndContainer(),
1910 /** Construct nested container widget for managing the lists and the info panel of the NewGRF GUI. */
1911 NWidgetBase* NewGRFDisplay(int *biggest_index)
1913 NWidgetBase *avs = MakeNWidgets(_nested_newgrf_availables_widgets, lengthof(_nested_newgrf_availables_widgets), biggest_index, nullptr);
1915 int biggest2;
1916 NWidgetBase *acs = MakeNWidgets(_nested_newgrf_actives_widgets, lengthof(_nested_newgrf_actives_widgets), &biggest2, nullptr);
1917 *biggest_index = std::max(*biggest_index, biggest2);
1919 NWidgetBase *inf = MakeNWidgets(_nested_newgrf_infopanel_widgets, lengthof(_nested_newgrf_infopanel_widgets), &biggest2, nullptr);
1920 *biggest_index = std::max(*biggest_index, biggest2);
1922 return new NWidgetNewGRFDisplay(avs, acs, inf);
1925 /* Widget definition of the manage newgrfs window */
1926 static const NWidgetPart _nested_newgrf_widgets[] = {
1927 NWidget(NWID_HORIZONTAL),
1928 NWidget(WWT_CLOSEBOX, COLOUR_MAUVE),
1929 NWidget(WWT_CAPTION, COLOUR_MAUVE), SetDataTip(STR_NEWGRF_SETTINGS_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
1930 NWidget(WWT_DEFSIZEBOX, COLOUR_MAUVE),
1931 EndContainer(),
1932 NWidget(WWT_PANEL, COLOUR_MAUVE),
1933 NWidgetFunction(NewGRFDisplay), SetPadding(WD_RESIZEBOX_WIDTH, WD_RESIZEBOX_WIDTH, 2, WD_RESIZEBOX_WIDTH),
1934 /* Resize button. */
1935 NWidget(NWID_HORIZONTAL),
1936 NWidget(NWID_SPACER), SetFill(1, 0), SetResize(1, 0),
1937 NWidget(WWT_RESIZEBOX, COLOUR_MAUVE),
1938 EndContainer(),
1939 EndContainer(),
1942 /* Window definition of the manage newgrfs window */
1943 static WindowDesc _newgrf_desc(
1944 WDP_CENTER, "settings_newgrf", 300, 263,
1945 WC_GAME_OPTIONS, WC_NONE,
1947 _nested_newgrf_widgets, lengthof(_nested_newgrf_widgets)
1951 * Callback function for the newgrf 'apply changes' confirmation window
1952 * @param w Window which is calling this callback
1953 * @param confirmed boolean value, true when yes was clicked, false otherwise
1955 static void NewGRFConfirmationCallback(Window *w, bool confirmed)
1957 if (confirmed) {
1958 CloseWindowByClass(WC_GRF_PARAMETERS);
1959 CloseWindowByClass(WC_TEXTFILE);
1960 NewGRFWindow *nw = dynamic_cast<NewGRFWindow*>(w);
1962 GamelogStartAction(GLAT_GRF);
1963 GamelogGRFUpdate(_grfconfig, nw->actives); // log GRF changes
1964 CopyGRFConfigList(nw->orig_list, nw->actives, false);
1965 ReloadNewGRFData();
1966 GamelogStopAction();
1968 /* Show new, updated list */
1969 GRFConfig *c;
1970 int i = 0;
1971 for (c = nw->actives; c != nullptr && c != nw->active_sel; c = c->next, i++) {}
1972 CopyGRFConfigList(&nw->actives, *nw->orig_list, false);
1973 for (c = nw->actives; c != nullptr && i > 0; c = c->next, i--) {}
1974 nw->active_sel = c;
1975 nw->avails.ForceRebuild();
1976 nw->modified = false;
1978 w->InvalidateData();
1980 ReInitAllWindows(false);
1981 CloseWindowByClass(WC_BUILD_OBJECT);
1988 * Setup the NewGRF gui
1989 * @param editable allow the user to make changes to the grfconfig in the window
1990 * @param show_params show information about what parameters are set for the grf files
1991 * @param exec_changes if changes are made to the list (editable is true), apply these
1992 * changes immediately or only update the list
1993 * @param config pointer to a linked-list of grfconfig's that will be shown
1995 void ShowNewGRFSettings(bool editable, bool show_params, bool exec_changes, GRFConfig **config)
1997 CloseWindowByClass(WC_GAME_OPTIONS);
1998 new NewGRFWindow(&_newgrf_desc, editable, show_params, exec_changes, config);
2001 /** Widget parts of the save preset window. */
2002 static const NWidgetPart _nested_save_preset_widgets[] = {
2003 NWidget(NWID_HORIZONTAL),
2004 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
2005 NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_SAVE_PRESET_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
2006 NWidget(WWT_DEFSIZEBOX, COLOUR_GREY),
2007 EndContainer(),
2008 NWidget(WWT_PANEL, COLOUR_GREY),
2009 NWidget(NWID_HORIZONTAL),
2010 NWidget(WWT_INSET, COLOUR_GREY, WID_SVP_PRESET_LIST), SetPadding(2, 1, 0, 2),
2011 SetDataTip(0x0, STR_SAVE_PRESET_LIST_TOOLTIP), SetResize(1, 10), SetScrollbar(WID_SVP_SCROLLBAR), EndContainer(),
2012 NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_SVP_SCROLLBAR),
2013 EndContainer(),
2014 NWidget(WWT_EDITBOX, COLOUR_GREY, WID_SVP_EDITBOX), SetPadding(3, 2, 2, 2), SetFill(1, 0), SetResize(1, 0),
2015 SetDataTip(STR_SAVE_PRESET_TITLE, STR_SAVE_PRESET_EDITBOX_TOOLTIP),
2016 EndContainer(),
2017 NWidget(NWID_HORIZONTAL),
2018 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SVP_CANCEL), SetDataTip(STR_SAVE_PRESET_CANCEL, STR_SAVE_PRESET_CANCEL_TOOLTIP), SetFill(1, 0), SetResize(1, 0),
2019 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SVP_SAVE), SetDataTip(STR_SAVE_PRESET_SAVE, STR_SAVE_PRESET_SAVE_TOOLTIP), SetFill(1, 0), SetResize(1, 0),
2020 NWidget(WWT_RESIZEBOX, COLOUR_GREY),
2021 EndContainer(),
2024 /** Window description of the preset save window. */
2025 static WindowDesc _save_preset_desc(
2026 WDP_CENTER, "save_preset", 140, 110,
2027 WC_SAVE_PRESET, WC_GAME_OPTIONS,
2028 WDF_MODAL,
2029 _nested_save_preset_widgets, lengthof(_nested_save_preset_widgets)
2032 /** Class for the save preset window. */
2033 struct SavePresetWindow : public Window {
2034 QueryString presetname_editbox; ///< Edit box of the save preset.
2035 StringList presets; ///< Available presets.
2036 Scrollbar *vscroll; ///< Pointer to the scrollbar widget.
2037 int selected; ///< Selected entry in the preset list, or \c -1 if none selected.
2040 * Constructor of the save preset window.
2041 * @param initial_text Initial text to display in the edit box, or \c nullptr.
2043 SavePresetWindow(const char *initial_text) : Window(&_save_preset_desc), presetname_editbox(32)
2045 this->presets = GetGRFPresetList();
2046 this->selected = -1;
2047 if (initial_text != nullptr) {
2048 for (uint i = 0; i < this->presets.size(); i++) {
2049 if (this->presets[i] == initial_text) {
2050 this->selected = i;
2051 break;
2056 this->querystrings[WID_SVP_EDITBOX] = &this->presetname_editbox;
2057 this->presetname_editbox.ok_button = WID_SVP_SAVE;
2058 this->presetname_editbox.cancel_button = WID_SVP_CANCEL;
2060 this->CreateNestedTree();
2061 this->vscroll = this->GetScrollbar(WID_SVP_SCROLLBAR);
2062 this->FinishInitNested(0);
2064 this->vscroll->SetCount((uint)this->presets.size());
2065 this->SetFocusedWidget(WID_SVP_EDITBOX);
2066 if (initial_text != nullptr) this->presetname_editbox.text.Assign(initial_text);
2069 ~SavePresetWindow()
2073 void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
2075 switch (widget) {
2076 case WID_SVP_PRESET_LIST: {
2077 resize->height = FONT_HEIGHT_NORMAL + 2U;
2078 size->height = 0;
2079 for (uint i = 0; i < this->presets.size(); i++) {
2080 Dimension d = GetStringBoundingBox(this->presets[i].c_str());
2081 size->width = std::max(size->width, d.width + WD_FRAMETEXT_LEFT + WD_FRAMETEXT_RIGHT);
2082 resize->height = std::max(resize->height, d.height);
2084 size->height = ClampU((uint)this->presets.size(), 5, 20) * resize->height + 1;
2085 break;
2090 void DrawWidget(const Rect &r, int widget) const override
2092 switch (widget) {
2093 case WID_SVP_PRESET_LIST: {
2094 GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, PC_BLACK);
2096 uint step_height = this->GetWidget<NWidgetBase>(WID_SVP_PRESET_LIST)->resize_y;
2097 int offset_y = (step_height - FONT_HEIGHT_NORMAL) / 2;
2098 uint y = r.top + WD_FRAMERECT_TOP;
2099 uint min_index = this->vscroll->GetPosition();
2100 uint max_index = std::min(min_index + this->vscroll->GetCapacity(), (uint)this->presets.size());
2102 for (uint i = min_index; i < max_index; i++) {
2103 if ((int)i == this->selected) GfxFillRect(r.left + 1, y, r.right - 1, y + step_height - 2, PC_DARK_BLUE);
2105 const char *text = this->presets[i].c_str();
2106 DrawString(r.left + WD_FRAMERECT_LEFT, r.right, y + offset_y, text, ((int)i == this->selected) ? TC_WHITE : TC_SILVER);
2107 y += step_height;
2109 break;
2114 void OnClick(Point pt, int widget, int click_count) override
2116 switch (widget) {
2117 case WID_SVP_PRESET_LIST: {
2118 uint row = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_SVP_PRESET_LIST);
2119 if (row < this->presets.size()) {
2120 this->selected = row;
2121 this->presetname_editbox.text.Assign(this->presets[row].c_str());
2122 this->SetWidgetDirty(WID_SVP_PRESET_LIST);
2123 this->SetWidgetDirty(WID_SVP_EDITBOX);
2125 break;
2128 case WID_SVP_CANCEL:
2129 this->Close();
2130 break;
2132 case WID_SVP_SAVE: {
2133 Window *w = FindWindowById(WC_GAME_OPTIONS, WN_GAME_OPTIONS_NEWGRF_STATE);
2134 if (w != nullptr && !StrEmpty(this->presetname_editbox.text.buf)) w->OnQueryTextFinished(this->presetname_editbox.text.buf);
2135 this->Close();
2136 break;
2141 void OnResize() override
2143 this->vscroll->SetCapacityFromWidget(this, WID_SVP_PRESET_LIST);
2148 * Open the window for saving a preset.
2149 * @param initial_text Initial text to display in the edit box, or \c nullptr.
2151 static void ShowSavePresetWindow(const char *initial_text)
2153 CloseWindowByClass(WC_SAVE_PRESET);
2154 new SavePresetWindow(initial_text);
2157 /** Widgets for the progress window. */
2158 static const NWidgetPart _nested_scan_progress_widgets[] = {
2159 NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_NEWGRF_SCAN_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
2160 NWidget(WWT_PANEL, COLOUR_GREY),
2161 NWidget(NWID_HORIZONTAL), SetPIP(20, 0, 20),
2162 NWidget(NWID_VERTICAL), SetPIP(11, 8, 11),
2163 NWidget(WWT_LABEL, INVALID_COLOUR), SetDataTip(STR_NEWGRF_SCAN_MESSAGE, STR_NULL), SetFill(1, 0),
2164 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_SP_PROGRESS_BAR), SetFill(1, 0),
2165 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_SP_PROGRESS_TEXT), SetFill(1, 0),
2166 EndContainer(),
2167 EndContainer(),
2168 EndContainer(),
2171 /** Description of the widgets and other settings of the window. */
2172 static WindowDesc _scan_progress_desc(
2173 WDP_CENTER, nullptr, 0, 0,
2174 WC_MODAL_PROGRESS, WC_NONE,
2176 _nested_scan_progress_widgets, lengthof(_nested_scan_progress_widgets)
2179 /** Window for showing the progress of NewGRF scanning. */
2180 struct ScanProgressWindow : public Window {
2181 char *last_name; ///< The name of the last 'seen' NewGRF.
2182 int scanned; ///< The number of NewGRFs that we have seen.
2184 /** Create the window. */
2185 ScanProgressWindow() : Window(&_scan_progress_desc), last_name(nullptr), scanned(0)
2187 this->InitNested(1);
2190 /** Free the last name buffer. */
2191 ~ScanProgressWindow()
2193 free(last_name);
2196 void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
2198 switch (widget) {
2199 case WID_SP_PROGRESS_BAR: {
2200 SetDParamMaxValue(0, 100);
2201 *size = GetStringBoundingBox(STR_GENERATION_PROGRESS);
2202 /* We need some spacing for the 'border' */
2203 size->height += 8;
2204 size->width += 8;
2205 break;
2208 case WID_SP_PROGRESS_TEXT:
2209 SetDParamMaxDigits(0, 4);
2210 SetDParamMaxDigits(1, 4);
2211 /* We really don't know the width. We could determine it by scanning the NewGRFs,
2212 * but this is the status window for scanning them... */
2213 size->width = std::max(400U, GetStringBoundingBox(STR_NEWGRF_SCAN_STATUS).width);
2214 size->height = FONT_HEIGHT_NORMAL * 2 + WD_PAR_VSEP_NORMAL;
2215 break;
2219 void DrawWidget(const Rect &r, int widget) const override
2221 switch (widget) {
2222 case WID_SP_PROGRESS_BAR: {
2223 /* Draw the % complete with a bar and a text */
2224 DrawFrameRect(r.left, r.top, r.right, r.bottom, COLOUR_GREY, FR_BORDERONLY);
2225 uint percent = scanned * 100 / std::max(1U, _settings_client.gui.last_newgrf_count);
2226 DrawFrameRect(r.left + 1, r.top + 1, (int)((r.right - r.left - 2) * percent / 100) + r.left + 1, r.bottom - 1, COLOUR_MAUVE, FR_NONE);
2227 SetDParam(0, percent);
2228 DrawString(r.left, r.right, r.top + 5, STR_GENERATION_PROGRESS, TC_FROMSTRING, SA_HOR_CENTER);
2229 break;
2232 case WID_SP_PROGRESS_TEXT:
2233 SetDParam(0, this->scanned);
2234 SetDParam(1, _settings_client.gui.last_newgrf_count);
2235 DrawString(r.left, r.right, r.top, STR_NEWGRF_SCAN_STATUS, TC_FROMSTRING, SA_HOR_CENTER);
2237 DrawString(r.left, r.right, r.top + FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL, this->last_name == nullptr ? "" : this->last_name, TC_BLACK, SA_HOR_CENTER);
2238 break;
2243 * Update the NewGRF scan status.
2244 * @param num The number of NewGRFs scanned so far.
2245 * @param name The name of the last scanned NewGRF.
2247 void UpdateNewGRFScanStatus(uint num, const char *name)
2249 free(this->last_name);
2250 if (name == nullptr) {
2251 char buf[256];
2252 GetString(buf, STR_NEWGRF_SCAN_ARCHIVES, lastof(buf));
2253 this->last_name = stredup(buf);
2254 } else {
2255 this->last_name = stredup(name);
2257 this->scanned = num;
2258 if (num > _settings_client.gui.last_newgrf_count) _settings_client.gui.last_newgrf_count = num;
2260 this->SetDirty();
2265 * Update the NewGRF scan status.
2266 * @param num The number of NewGRFs scanned so far.
2267 * @param name The name of the last scanned NewGRF.
2269 void UpdateNewGRFScanStatus(uint num, const char *name)
2271 ScanProgressWindow *w = dynamic_cast<ScanProgressWindow *>(FindWindowByClass(WC_MODAL_PROGRESS));
2272 if (w == nullptr) w = new ScanProgressWindow();
2273 w->UpdateNewGRFScanStatus(num, name);