Codechange: Use cached town, station, industry names for list window sorting
[openttd-github.git] / src / newgrf_gui.cpp
blob3b462f427b6a0ccd2a7678fed5d830f7aed629f8
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 /* We only want to show fatal errors */
51 if (c->error == nullptr || c->error->severity != STR_NEWGRF_ERROR_MSG_FATAL) continue;
53 SetDParam (0, c->error->custom_message == nullptr ? 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 ShowErrorMessage(STR_NEWGRF_ERROR_FATAL_POPUP, INVALID_STRING_ID, WL_CRITICAL);
61 break;
65 static void ShowNewGRFInfo(const GRFConfig *c, uint x, uint y, uint right, uint bottom, bool show_params)
67 if (c->error != nullptr) {
68 char message[512];
69 SetDParamStr(0, c->error->custom_message); // is skipped by built-in messages
70 SetDParamStr(1, c->filename);
71 SetDParamStr(2, c->error->data);
72 for (uint i = 0; i < lengthof(c->error->param_value); i++) {
73 SetDParam(3 + i, c->error->param_value[i]);
75 GetString(message, c->error->custom_message == nullptr ? c->error->message : STR_JUST_RAW_STRING, lastof(message));
77 SetDParamStr(0, message);
78 y = DrawStringMultiLine(x, right, y, bottom, c->error->severity);
81 /* Draw filename or not if it is not known (GRF sent over internet) */
82 if (c->filename != nullptr) {
83 SetDParamStr(0, c->filename);
84 y = DrawStringMultiLine(x, right, y, bottom, STR_NEWGRF_SETTINGS_FILENAME);
87 /* Prepare and draw GRF ID */
88 char buff[256];
89 seprintf(buff, lastof(buff), "%08X", BSWAP32(c->ident.grfid));
90 SetDParamStr(0, buff);
91 y = DrawStringMultiLine(x, right, y, bottom, STR_NEWGRF_SETTINGS_GRF_ID);
93 if ((_settings_client.gui.newgrf_developer_tools || _settings_client.gui.newgrf_show_old_versions) && c->version != 0) {
94 SetDParam(0, c->version);
95 y = DrawStringMultiLine(x, right, y, bottom, STR_NEWGRF_SETTINGS_VERSION);
97 if ((_settings_client.gui.newgrf_developer_tools || _settings_client.gui.newgrf_show_old_versions) && c->min_loadable_version != 0) {
98 SetDParam(0, c->min_loadable_version);
99 y = DrawStringMultiLine(x, right, y, bottom, STR_NEWGRF_SETTINGS_MIN_VERSION);
102 /* Prepare and draw MD5 sum */
103 md5sumToString(buff, lastof(buff), c->ident.md5sum);
104 SetDParamStr(0, buff);
105 y = DrawStringMultiLine(x, right, y, bottom, STR_NEWGRF_SETTINGS_MD5SUM);
107 /* Show GRF parameter list */
108 if (show_params) {
109 if (c->num_params > 0) {
110 GRFBuildParamList(buff, c, lastof(buff));
111 SetDParam(0, STR_JUST_RAW_STRING);
112 SetDParamStr(1, buff);
113 } else {
114 SetDParam(0, STR_NEWGRF_SETTINGS_PARAMETER_NONE);
116 y = DrawStringMultiLine(x, right, y, bottom, STR_NEWGRF_SETTINGS_PARAMETER);
118 /* Draw the palette of the NewGRF */
119 if (c->palette & GRFP_BLT_32BPP) {
120 SetDParam(0, (c->palette & GRFP_USE_WINDOWS) ? STR_NEWGRF_SETTINGS_PALETTE_LEGACY_32BPP : STR_NEWGRF_SETTINGS_PALETTE_DEFAULT_32BPP);
121 } else {
122 SetDParam(0, (c->palette & GRFP_USE_WINDOWS) ? STR_NEWGRF_SETTINGS_PALETTE_LEGACY : STR_NEWGRF_SETTINGS_PALETTE_DEFAULT);
124 y = DrawStringMultiLine(x, right, y, bottom, STR_NEWGRF_SETTINGS_PALETTE);
127 /* Show flags */
128 if (c->status == GCS_NOT_FOUND) y = DrawStringMultiLine(x, right, y, bottom, STR_NEWGRF_SETTINGS_NOT_FOUND);
129 if (c->status == GCS_DISABLED) y = DrawStringMultiLine(x, right, y, bottom, STR_NEWGRF_SETTINGS_DISABLED);
130 if (HasBit(c->flags, GCF_INVALID)) y = DrawStringMultiLine(x, right, y, bottom, STR_NEWGRF_SETTINGS_INCOMPATIBLE);
131 if (HasBit(c->flags, GCF_COMPATIBLE)) y = DrawStringMultiLine(x, right, y, bottom, STR_NEWGRF_COMPATIBLE_LOADED);
133 /* Draw GRF info if it exists */
134 if (!StrEmpty(c->GetDescription())) {
135 SetDParamStr(0, c->GetDescription());
136 y = DrawStringMultiLine(x, right, y, bottom, STR_BLACK_RAW_STRING);
137 } else {
138 y = DrawStringMultiLine(x, right, y, bottom, STR_NEWGRF_SETTINGS_NO_INFO);
143 * Window for setting the parameters of a NewGRF.
145 struct NewGRFParametersWindow : public Window {
146 static GRFParameterInfo dummy_parameter_info; ///< Dummy info in case a newgrf didn't provide info about some parameter.
147 GRFConfig *grf_config; ///< Set the parameters of this GRFConfig.
148 uint clicked_button; ///< The row in which a button was clicked or UINT_MAX.
149 bool clicked_increase; ///< True if the increase button was clicked, false for the decrease button.
150 bool clicked_dropdown; ///< Whether the dropdown is open.
151 bool closing_dropdown; ///< True, if the dropdown list is currently closing.
152 GUITimer timeout; ///< How long before we unpress the last-pressed button?
153 uint clicked_row; ///< The selected parameter
154 int line_height; ///< Height of a row in the matrix widget.
155 Scrollbar *vscroll;
156 bool action14present; ///< True if action14 information is present.
157 bool editable; ///< Allow editing parameters.
159 NewGRFParametersWindow(WindowDesc *desc, GRFConfig *c, bool editable) : Window(desc),
160 grf_config(c),
161 clicked_button(UINT_MAX),
162 clicked_dropdown(false),
163 closing_dropdown(false),
164 clicked_row(UINT_MAX),
165 editable(editable)
167 this->action14present = (c->num_valid_params != lengthof(c->param) || c->param_info.size() != 0);
169 this->CreateNestedTree();
170 this->vscroll = this->GetScrollbar(WID_NP_SCROLLBAR);
171 this->GetWidget<NWidgetStacked>(WID_NP_SHOW_NUMPAR)->SetDisplayedPlane(this->action14present ? SZSP_HORIZONTAL : 0);
172 this->GetWidget<NWidgetStacked>(WID_NP_SHOW_DESCRIPTION)->SetDisplayedPlane(this->action14present ? 0 : SZSP_HORIZONTAL);
173 this->FinishInitNested(); // Initializes 'this->line_height' as side effect.
175 this->SetWidgetDisabledState(WID_NP_RESET, !this->editable);
177 this->InvalidateData();
181 * Get a dummy parameter-info object with default information.
182 * @param nr The param number that should be changed.
183 * @return GRFParameterInfo with dummy information about the given parameter.
185 static GRFParameterInfo *GetDummyParameterInfo(uint nr)
187 dummy_parameter_info.param_nr = nr;
188 return &dummy_parameter_info;
191 void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
193 switch (widget) {
194 case WID_NP_NUMPAR_DEC:
195 case WID_NP_NUMPAR_INC: {
196 size->width = max(SETTING_BUTTON_WIDTH / 2, FONT_HEIGHT_NORMAL);
197 size->height = max(SETTING_BUTTON_HEIGHT, FONT_HEIGHT_NORMAL);
198 break;
201 case WID_NP_NUMPAR: {
202 SetDParamMaxValue(0, lengthof(this->grf_config->param));
203 Dimension d = GetStringBoundingBox(this->GetWidget<NWidgetCore>(widget)->widget_data);
204 d.width += padding.width;
205 d.height += padding.height;
206 *size = maxdim(*size, d);
207 break;
210 case WID_NP_BACKGROUND:
211 this->line_height = max(SETTING_BUTTON_HEIGHT, FONT_HEIGHT_NORMAL) + WD_MATRIX_TOP + WD_MATRIX_BOTTOM;
213 resize->width = 1;
214 resize->height = this->line_height;
215 size->height = 5 * this->line_height;
216 break;
218 case WID_NP_DESCRIPTION:
219 /* Minimum size of 4 lines. The 500 is the default size of the window. */
220 Dimension suggestion = {500 - WD_FRAMERECT_LEFT - WD_FRAMERECT_RIGHT, (uint)FONT_HEIGHT_NORMAL * 4 + WD_TEXTPANEL_TOP + WD_TEXTPANEL_BOTTOM};
221 for (uint i = 0; i < this->grf_config->param_info.size(); i++) {
222 const GRFParameterInfo *par_info = this->grf_config->param_info[i];
223 if (par_info == nullptr) continue;
224 const char *desc = GetGRFStringFromGRFText(par_info->desc);
225 if (desc == nullptr) continue;
226 Dimension d = GetStringMultiLineBoundingBox(desc, suggestion);
227 d.height += WD_TEXTPANEL_TOP + WD_TEXTPANEL_BOTTOM;
228 suggestion = maxdim(d, suggestion);
230 size->height = suggestion.height;
231 break;
235 void SetStringParameters(int widget) const override
237 switch (widget) {
238 case WID_NP_NUMPAR:
239 SetDParam(0, this->vscroll->GetCount());
240 break;
244 void DrawWidget(const Rect &r, int widget) const override
246 if (widget == WID_NP_DESCRIPTION) {
247 const GRFParameterInfo *par_info = (this->clicked_row < this->grf_config->param_info.size()) ? this->grf_config->param_info[this->clicked_row] : nullptr;
248 if (par_info == nullptr) return;
249 const char *desc = GetGRFStringFromGRFText(par_info->desc);
250 if (desc == nullptr) return;
251 DrawStringMultiLine(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + WD_TEXTPANEL_TOP, r.bottom - WD_TEXTPANEL_BOTTOM, desc, TC_BLACK);
252 return;
253 } else if (widget != WID_NP_BACKGROUND) {
254 return;
257 bool rtl = _current_text_dir == TD_RTL;
258 uint buttons_left = rtl ? r.right - SETTING_BUTTON_WIDTH - 3 : r.left + 4;
259 uint text_left = r.left + (rtl ? WD_FRAMERECT_LEFT : SETTING_BUTTON_WIDTH + 8);
260 uint text_right = r.right - (rtl ? SETTING_BUTTON_WIDTH + 8 : WD_FRAMERECT_RIGHT);
262 int y = r.top;
263 int button_y_offset = (this->line_height - SETTING_BUTTON_HEIGHT) / 2;
264 int text_y_offset = (this->line_height - FONT_HEIGHT_NORMAL) / 2;
265 for (uint i = this->vscroll->GetPosition(); this->vscroll->IsVisible(i) && i < this->vscroll->GetCount(); i++) {
266 GRFParameterInfo *par_info = (i < this->grf_config->param_info.size()) ? this->grf_config->param_info[i] : nullptr;
267 if (par_info == nullptr) par_info = GetDummyParameterInfo(i);
268 uint32 current_value = par_info->GetValue(this->grf_config);
269 bool selected = (i == this->clicked_row);
271 if (par_info->type == PTYPE_BOOL) {
272 DrawBoolButton(buttons_left, y + button_y_offset, current_value != 0, this->editable);
273 SetDParam(2, par_info->GetValue(this->grf_config) == 0 ? STR_CONFIG_SETTING_OFF : STR_CONFIG_SETTING_ON);
274 } else if (par_info->type == PTYPE_UINT_ENUM) {
275 if (par_info->complete_labels) {
276 DrawDropDownButton(buttons_left, y + button_y_offset, COLOUR_YELLOW, this->clicked_row == i && this->clicked_dropdown, this->editable);
277 } else {
278 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);
280 SetDParam(2, STR_JUST_INT);
281 SetDParam(3, current_value);
282 if (par_info->value_names.Contains(current_value)) {
283 const char *label = GetGRFStringFromGRFText(par_info->value_names.Find(current_value)->second);
284 if (label != nullptr) {
285 SetDParam(2, STR_JUST_RAW_STRING);
286 SetDParamStr(3, label);
291 const char *name = GetGRFStringFromGRFText(par_info->name);
292 if (name != nullptr) {
293 SetDParam(0, STR_JUST_RAW_STRING);
294 SetDParamStr(1, name);
295 } else {
296 SetDParam(0, STR_NEWGRF_PARAMETERS_DEFAULT_NAME);
297 SetDParam(1, i + 1);
300 DrawString(text_left, text_right, y + text_y_offset, STR_NEWGRF_PARAMETERS_SETTING, selected ? TC_WHITE : TC_LIGHT_BLUE);
301 y += this->line_height;
305 void OnPaint() override
307 if (this->closing_dropdown) {
308 this->closing_dropdown = false;
309 this->clicked_dropdown = false;
311 this->DrawWidgets();
314 void OnClick(Point pt, int widget, int click_count) override
316 switch (widget) {
317 case WID_NP_NUMPAR_DEC:
318 if (this->editable && !this->action14present && this->grf_config->num_params > 0) {
319 this->grf_config->num_params--;
320 this->InvalidateData();
321 SetWindowDirty(WC_GAME_OPTIONS, WN_GAME_OPTIONS_NEWGRF_STATE);
323 break;
325 case WID_NP_NUMPAR_INC: {
326 GRFConfig *c = this->grf_config;
327 if (this->editable && !this->action14present && c->num_params < c->num_valid_params) {
328 c->param[c->num_params++] = 0;
329 this->InvalidateData();
330 SetWindowDirty(WC_GAME_OPTIONS, WN_GAME_OPTIONS_NEWGRF_STATE);
332 break;
335 case WID_NP_BACKGROUND: {
336 if (!this->editable) break;
337 uint num = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_NP_BACKGROUND);
338 if (num >= this->vscroll->GetCount()) break;
339 if (this->clicked_row != num) {
340 DeleteChildWindows(WC_QUERY_STRING);
341 HideDropDownMenu(this);
342 this->clicked_row = num;
343 this->clicked_dropdown = false;
346 const NWidgetBase *wid = this->GetWidget<NWidgetBase>(WID_NP_BACKGROUND);
347 int x = pt.x - wid->pos_x;
348 if (_current_text_dir == TD_RTL) x = wid->current_x - 1 - x;
349 x -= 4;
351 GRFParameterInfo *par_info = (num < this->grf_config->param_info.size()) ? this->grf_config->param_info[num] : nullptr;
352 if (par_info == nullptr) par_info = GetDummyParameterInfo(num);
354 /* One of the arrows is clicked */
355 uint32 old_val = par_info->GetValue(this->grf_config);
356 if (par_info->type != PTYPE_BOOL && IsInsideMM(x, 0, SETTING_BUTTON_WIDTH) && par_info->complete_labels) {
357 if (this->clicked_dropdown) {
358 /* unclick the dropdown */
359 HideDropDownMenu(this);
360 this->clicked_dropdown = false;
361 this->closing_dropdown = false;
362 } else {
363 const NWidgetBase *wid = this->GetWidget<NWidgetBase>(WID_NP_BACKGROUND);
364 int rel_y = (pt.y - (int)wid->pos_y) % this->line_height;
366 Rect wi_rect;
367 wi_rect.left = pt.x - (_current_text_dir == TD_RTL ? SETTING_BUTTON_WIDTH - 1 - x : x);;
368 wi_rect.right = wi_rect.left + SETTING_BUTTON_WIDTH - 1;
369 wi_rect.top = pt.y - rel_y + (this->line_height - SETTING_BUTTON_HEIGHT) / 2;
370 wi_rect.bottom = wi_rect.top + SETTING_BUTTON_HEIGHT - 1;
372 /* For dropdowns we also have to check the y position thoroughly, the mouse may not above the just opening dropdown */
373 if (pt.y >= wi_rect.top && pt.y <= wi_rect.bottom) {
374 this->clicked_dropdown = true;
375 this->closing_dropdown = false;
377 DropDownList list;
378 for (uint32 i = par_info->min_value; i <= par_info->max_value; i++) {
379 list.emplace_back(new DropDownListCharStringItem(GetGRFStringFromGRFText(par_info->value_names.Find(i)->second), i, false));
382 ShowDropDownListAt(this, std::move(list), old_val, -1, wi_rect, COLOUR_ORANGE, true);
385 } else if (IsInsideMM(x, 0, SETTING_BUTTON_WIDTH)) {
386 uint32 val = old_val;
387 if (par_info->type == PTYPE_BOOL) {
388 val = !val;
389 } else {
390 if (x >= SETTING_BUTTON_WIDTH / 2) {
391 /* Increase button clicked */
392 if (val < par_info->max_value) val++;
393 this->clicked_increase = true;
394 } else {
395 /* Decrease button clicked */
396 if (val > par_info->min_value) val--;
397 this->clicked_increase = false;
400 if (val != old_val) {
401 par_info->SetValue(this->grf_config, val);
403 this->clicked_button = num;
404 this->timeout.SetInterval(150);
406 } else if (par_info->type == PTYPE_UINT_ENUM && !par_info->complete_labels && click_count >= 2) {
407 /* Display a query box so users can enter a custom value. */
408 SetDParam(0, old_val);
409 ShowQueryString(STR_JUST_INT, STR_CONFIG_SETTING_QUERY_CAPTION, 10, this, CS_NUMERAL, QSF_NONE);
411 this->SetDirty();
412 break;
415 case WID_NP_RESET:
416 if (!this->editable) break;
417 this->grf_config->SetParameterDefaults();
418 this->InvalidateData();
419 SetWindowDirty(WC_GAME_OPTIONS, WN_GAME_OPTIONS_NEWGRF_STATE);
420 break;
422 case WID_NP_ACCEPT:
423 delete this;
424 break;
428 void OnQueryTextFinished(char *str) override
430 if (StrEmpty(str)) return;
431 int32 value = atoi(str);
432 GRFParameterInfo *par_info = ((uint)this->clicked_row < this->grf_config->param_info.size()) ? this->grf_config->param_info[this->clicked_row] : nullptr;
433 if (par_info == nullptr) par_info = GetDummyParameterInfo(this->clicked_row);
434 uint32 val = Clamp<uint32>(value, par_info->min_value, par_info->max_value);
435 par_info->SetValue(this->grf_config, val);
436 this->SetDirty();
439 void OnDropdownSelect(int widget, int index) override
441 assert(this->clicked_dropdown);
442 GRFParameterInfo *par_info = ((uint)this->clicked_row < this->grf_config->param_info.size()) ? this->grf_config->param_info[this->clicked_row] : nullptr;
443 if (par_info == nullptr) par_info = GetDummyParameterInfo(this->clicked_row);
444 par_info->SetValue(this->grf_config, index);
445 this->SetDirty();
448 void OnDropdownClose(Point pt, int widget, int index, bool instant_close) override
450 /* We cannot raise the dropdown button just yet. OnClick needs some hint, whether
451 * the same dropdown button was clicked again, and then not open the dropdown again.
452 * So, we only remember that it was closed, and process it on the next OnPaint, which is
453 * after OnClick. */
454 assert(this->clicked_dropdown);
455 this->closing_dropdown = true;
456 this->SetDirty();
459 void OnResize() override
461 this->vscroll->SetCapacityFromWidget(this, WID_NP_BACKGROUND);
465 * Some data on this window has become invalid.
466 * @param data Information about the changed data.
467 * @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.
469 void OnInvalidateData(int data = 0, bool gui_scope = true) override
471 if (!gui_scope) return;
472 if (!this->action14present) {
473 this->SetWidgetDisabledState(WID_NP_NUMPAR_DEC, !this->editable || this->grf_config->num_params == 0);
474 this->SetWidgetDisabledState(WID_NP_NUMPAR_INC, !this->editable || this->grf_config->num_params >= this->grf_config->num_valid_params);
477 this->vscroll->SetCount(this->action14present ? this->grf_config->num_valid_params : this->grf_config->num_params);
478 if (this->clicked_row != UINT_MAX && this->clicked_row >= this->vscroll->GetCount()) {
479 this->clicked_row = UINT_MAX;
480 DeleteChildWindows(WC_QUERY_STRING);
484 void OnRealtimeTick(uint delta_ms) override
486 if (timeout.Elapsed(delta_ms)) {
487 this->clicked_button = UINT_MAX;
488 this->SetDirty();
492 GRFParameterInfo NewGRFParametersWindow::dummy_parameter_info(0);
495 static const NWidgetPart _nested_newgrf_parameter_widgets[] = {
496 NWidget(NWID_HORIZONTAL),
497 NWidget(WWT_CLOSEBOX, COLOUR_MAUVE),
498 NWidget(WWT_CAPTION, COLOUR_MAUVE), SetDataTip(STR_NEWGRF_PARAMETERS_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
499 NWidget(WWT_DEFSIZEBOX, COLOUR_MAUVE),
500 EndContainer(),
501 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_NP_SHOW_NUMPAR),
502 NWidget(WWT_PANEL, COLOUR_MAUVE), SetResize(1, 0), SetFill(1, 0), SetPIP(4, 0, 4),
503 NWidget(NWID_HORIZONTAL), SetPIP(4, 0, 4),
504 NWidget(WWT_PUSHARROWBTN, COLOUR_YELLOW, WID_NP_NUMPAR_DEC), SetMinimalSize(12, 12), SetDataTip(AWV_DECREASE, STR_NULL),
505 NWidget(WWT_PUSHARROWBTN, COLOUR_YELLOW, WID_NP_NUMPAR_INC), SetMinimalSize(12, 12), SetDataTip(AWV_INCREASE, STR_NULL),
506 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),
507 EndContainer(),
508 EndContainer(),
509 EndContainer(),
510 NWidget(NWID_HORIZONTAL),
511 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),
512 NWidget(NWID_VSCROLLBAR, COLOUR_MAUVE, WID_NP_SCROLLBAR),
513 EndContainer(),
514 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_NP_SHOW_DESCRIPTION),
515 NWidget(WWT_PANEL, COLOUR_MAUVE, WID_NP_DESCRIPTION), SetResize(1, 0), SetFill(1, 0),
516 EndContainer(),
517 EndContainer(),
518 NWidget(NWID_HORIZONTAL),
519 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
520 NWidget(WWT_PUSHTXTBTN, COLOUR_MAUVE, WID_NP_ACCEPT), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_NEWGRF_PARAMETERS_CLOSE, STR_NULL),
521 NWidget(WWT_PUSHTXTBTN, COLOUR_MAUVE, WID_NP_RESET), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_NEWGRF_PARAMETERS_RESET, STR_NEWGRF_PARAMETERS_RESET_TOOLTIP),
522 EndContainer(),
523 NWidget(WWT_RESIZEBOX, COLOUR_MAUVE),
524 EndContainer(),
527 /** Window definition for the change grf parameters window */
528 static WindowDesc _newgrf_parameters_desc(
529 WDP_CENTER, "settings_newgrf_config", 500, 208,
530 WC_GRF_PARAMETERS, WC_NONE,
532 _nested_newgrf_parameter_widgets, lengthof(_nested_newgrf_parameter_widgets)
535 static void OpenGRFParameterWindow(GRFConfig *c, bool editable)
537 DeleteWindowByClass(WC_GRF_PARAMETERS);
538 new NewGRFParametersWindow(&_newgrf_parameters_desc, c, editable);
541 /** Window for displaying the textfile of a NewGRF. */
542 struct NewGRFTextfileWindow : public TextfileWindow {
543 const GRFConfig *grf_config; ///< View the textfile of this GRFConfig.
545 NewGRFTextfileWindow(TextfileType file_type, const GRFConfig *c) : TextfileWindow(file_type), grf_config(c)
547 const char *textfile = this->grf_config->GetTextfile(file_type);
548 this->LoadTextfile(textfile, NEWGRF_DIR);
551 void SetStringParameters(int widget) const override
553 if (widget == WID_TF_CAPTION) {
554 SetDParam(0, STR_CONTENT_TYPE_NEWGRF);
555 SetDParamStr(1, this->grf_config->GetName());
560 void ShowNewGRFTextfileWindow(TextfileType file_type, const GRFConfig *c)
562 DeleteWindowById(WC_TEXTFILE, file_type);
563 new NewGRFTextfileWindow(file_type, c);
566 typedef std::map<uint32, const GRFConfig *> GrfIdMap; ///< Map of grfid to the grf config.
569 * Add all grf configs from \a c into the map.
570 * @param c Grf list to add.
571 * @param grfid_map Map to add them to.
573 static void FillGrfidMap(const GRFConfig *c, GrfIdMap *grfid_map)
575 while (c != nullptr) {
576 std::pair<uint32, const GRFConfig *> p(c->ident.grfid, c);
577 grfid_map->insert(p);
578 c = c->next;
582 static void NewGRFConfirmationCallback(Window *w, bool confirmed);
583 static void ShowSavePresetWindow(const char *initial_text);
586 * Window for showing NewGRF files
588 struct NewGRFWindow : public Window, NewGRFScanCallback {
589 typedef GUIList<const GRFConfig *, StringFilter &> GUIGRFConfigList;
591 static const uint EDITBOX_MAX_SIZE = 50;
593 static Listing last_sorting; ///< Default sorting of #GUIGRFConfigList.
594 static Filtering last_filtering; ///< Default filtering of #GUIGRFConfigList.
595 static GUIGRFConfigList::SortFunction * const sorter_funcs[]; ///< Sort functions of the #GUIGRFConfigList.
596 static GUIGRFConfigList::FilterFunction * const filter_funcs[]; ///< Filter functions of the #GUIGRFConfigList.
598 GUIGRFConfigList avails; ///< Available (non-active) grfs.
599 const GRFConfig *avail_sel; ///< Currently selected available grf. \c nullptr is none is selected.
600 int avail_pos; ///< Index of #avail_sel if existing, else \c -1.
601 StringFilter string_filter; ///< Filter for available grf.
602 QueryString filter_editbox; ///< Filter editbox;
604 StringList grf_presets; ///< List of known NewGRF presets.
606 GRFConfig *actives; ///< Temporary active grf list to which changes are made.
607 GRFConfig *active_sel; ///< Selected active grf item.
609 GRFConfig **orig_list; ///< List active grfs in the game. Used as initial value, may be updated by the window.
610 bool editable; ///< Is the window editable?
611 bool show_params; ///< Are the grf-parameters shown in the info-panel?
612 bool execute; ///< On pressing 'apply changes' are grf changes applied immediately, or only list is updated.
613 int preset; ///< Selected preset or \c -1 if none selected.
614 int active_over; ///< Active GRF item over which another one is dragged, \c -1 if none.
616 Scrollbar *vscroll;
617 Scrollbar *vscroll2;
619 NewGRFWindow(WindowDesc *desc, bool editable, bool show_params, bool execute, GRFConfig **orig_list) : Window(desc), filter_editbox(EDITBOX_MAX_SIZE)
621 this->avail_sel = nullptr;
622 this->avail_pos = -1;
623 this->active_sel = nullptr;
624 this->actives = nullptr;
625 this->orig_list = orig_list;
626 this->editable = editable;
627 this->execute = execute;
628 this->show_params = show_params;
629 this->preset = -1;
630 this->active_over = -1;
632 CopyGRFConfigList(&this->actives, *orig_list, false);
633 this->grf_presets = GetGRFPresetList();
635 this->CreateNestedTree();
636 this->vscroll = this->GetScrollbar(WID_NS_SCROLLBAR);
637 this->vscroll2 = this->GetScrollbar(WID_NS_SCROLL2BAR);
639 this->GetWidget<NWidgetStacked>(WID_NS_SHOW_REMOVE)->SetDisplayedPlane(this->editable ? 0 : 1);
640 this->GetWidget<NWidgetStacked>(WID_NS_SHOW_APPLY)->SetDisplayedPlane(this->editable ? 0 : this->show_params ? 1 : SZSP_HORIZONTAL);
641 this->FinishInitNested(WN_GAME_OPTIONS_NEWGRF_STATE);
643 this->querystrings[WID_NS_FILTER] = &this->filter_editbox;
644 this->filter_editbox.cancel_button = QueryString::ACTION_CLEAR;
645 if (editable) {
646 this->SetFocusedWidget(WID_NS_FILTER);
647 } else {
648 this->DisableWidget(WID_NS_FILTER);
651 this->avails.SetListing(this->last_sorting);
652 this->avails.SetFiltering(this->last_filtering);
653 this->avails.SetSortFuncs(this->sorter_funcs);
654 this->avails.SetFilterFuncs(this->filter_funcs);
655 this->avails.ForceRebuild();
657 this->OnInvalidateData(GOID_NEWGRF_LIST_EDITED);
660 ~NewGRFWindow()
662 DeleteWindowByClass(WC_GRF_PARAMETERS);
663 DeleteWindowByClass(WC_TEXTFILE);
664 DeleteWindowByClass(WC_SAVE_PRESET);
666 if (this->editable && !this->execute) {
667 CopyGRFConfigList(this->orig_list, this->actives, true);
668 ResetGRFConfig(false);
669 ReloadNewGRFData();
672 /* Remove the temporary copy of grf-list used in window */
673 ClearGRFConfigList(&this->actives);
677 * Test whether the currently active set of NewGRFs can be upgraded with the available NewGRFs.
678 * @return Whether an upgrade is possible.
680 bool CanUpgradeCurrent()
682 GrfIdMap grfid_map;
683 FillGrfidMap(this->actives, &grfid_map);
685 for (const GRFConfig *a = _all_grfs; a != nullptr; a = a->next) {
686 GrfIdMap::const_iterator iter = grfid_map.find(a->ident.grfid);
687 if (iter != grfid_map.end() && a->version > iter->second->version) return true;
689 return false;
692 /** Upgrade the currently active set of NewGRFs. */
693 void UpgradeCurrent()
695 GrfIdMap grfid_map;
696 FillGrfidMap(this->actives, &grfid_map);
698 for (const GRFConfig *a = _all_grfs; a != nullptr; a = a->next) {
699 GrfIdMap::iterator iter = grfid_map.find(a->ident.grfid);
700 if (iter == grfid_map.end() || iter->second->version >= a->version) continue;
702 GRFConfig **c = &this->actives;
703 while (*c != iter->second) c = &(*c)->next;
704 GRFConfig *d = new GRFConfig(*a);
705 d->next = (*c)->next;
706 d->CopyParams(**c);
707 if (this->active_sel == *c) {
708 DeleteWindowByClass(WC_GRF_PARAMETERS);
709 DeleteWindowByClass(WC_TEXTFILE);
710 this->active_sel = nullptr;
712 delete *c;
713 *c = d;
714 iter->second = d;
718 void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
720 switch (widget) {
721 case WID_NS_FILE_LIST:
723 Dimension d = maxdim(GetSpriteSize(SPR_SQUARE), GetSpriteSize(SPR_WARNING_SIGN));
724 resize->height = max(d.height + 2U, FONT_HEIGHT_NORMAL + 2U);
725 size->height = max(size->height, WD_FRAMERECT_TOP + 6 * resize->height + WD_FRAMERECT_BOTTOM);
726 break;
729 case WID_NS_AVAIL_LIST:
730 resize->height = max(12, FONT_HEIGHT_NORMAL + 2);
731 size->height = max(size->height, WD_FRAMERECT_TOP + 8 * resize->height + WD_FRAMERECT_BOTTOM);
732 break;
734 case WID_NS_NEWGRF_INFO_TITLE: {
735 Dimension dim = GetStringBoundingBox(STR_NEWGRF_SETTINGS_INFO_TITLE);
736 size->height = max(size->height, dim.height + WD_FRAMETEXT_TOP + WD_FRAMETEXT_BOTTOM);
737 size->width = max(size->width, dim.width + WD_FRAMETEXT_LEFT + WD_FRAMETEXT_RIGHT);
738 break;
741 case WID_NS_NEWGRF_INFO:
742 size->height = max(size->height, WD_FRAMERECT_TOP + 10 * FONT_HEIGHT_NORMAL + WD_FRAMERECT_BOTTOM + padding.height + 2);
743 break;
745 case WID_NS_PRESET_LIST: {
746 Dimension d = GetStringBoundingBox(STR_NUM_CUSTOM);
747 for (const auto &i : this->grf_presets) {
748 SetDParamStr(0, i.c_str());
749 d = maxdim(d, GetStringBoundingBox(STR_JUST_RAW_STRING));
751 d.width += padding.width;
752 *size = maxdim(d, *size);
753 break;
756 case WID_NS_CONTENT_DOWNLOAD:
757 case WID_NS_CONTENT_DOWNLOAD2: {
758 Dimension d = GetStringBoundingBox(STR_NEWGRF_SETTINGS_FIND_MISSING_CONTENT_BUTTON);
759 *size = maxdim(d, GetStringBoundingBox(STR_INTRO_ONLINE_CONTENT));
760 size->width += padding.width;
761 size->height += padding.height;
762 break;
767 void OnResize() override
769 this->vscroll->SetCapacityFromWidget(this, WID_NS_FILE_LIST);
770 this->vscroll2->SetCapacityFromWidget(this, WID_NS_AVAIL_LIST);
773 void SetStringParameters(int widget) const override
775 switch (widget) {
776 case WID_NS_PRESET_LIST:
777 if (this->preset == -1) {
778 SetDParam(0, STR_NUM_CUSTOM);
779 } else {
780 SetDParam(0, STR_JUST_RAW_STRING);
781 SetDParamStr(1, this->grf_presets[this->preset].c_str());
783 break;
788 * Pick the palette for the sprite of the grf to display.
789 * @param c grf to display.
790 * @return Palette for the sprite.
792 inline PaletteID GetPalette(const GRFConfig *c) const
794 PaletteID pal;
796 /* Pick a colour */
797 switch (c->status) {
798 case GCS_NOT_FOUND:
799 case GCS_DISABLED:
800 pal = PALETTE_TO_RED;
801 break;
802 case GCS_ACTIVATED:
803 pal = PALETTE_TO_GREEN;
804 break;
805 default:
806 pal = PALETTE_TO_BLUE;
807 break;
810 /* Do not show a "not-failure" colour when it actually failed to load */
811 if (pal != PALETTE_TO_RED) {
812 if (HasBit(c->flags, GCF_STATIC)) {
813 pal = PALETTE_TO_GREY;
814 } else if (HasBit(c->flags, GCF_COMPATIBLE)) {
815 pal = PALETTE_TO_ORANGE;
819 return pal;
822 void DrawWidget(const Rect &r, int widget) const override
824 switch (widget) {
825 case WID_NS_FILE_LIST: {
826 GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, PC_BLACK);
828 uint step_height = this->GetWidget<NWidgetBase>(WID_NS_FILE_LIST)->resize_y;
829 uint y = r.top + WD_FRAMERECT_TOP;
830 Dimension square = GetSpriteSize(SPR_SQUARE);
831 Dimension warning = GetSpriteSize(SPR_WARNING_SIGN);
832 int square_offset_y = (step_height - square.height) / 2;
833 int warning_offset_y = (step_height - warning.height) / 2;
834 int offset_y = (step_height - FONT_HEIGHT_NORMAL) / 2;
836 bool rtl = _current_text_dir == TD_RTL;
837 uint text_left = rtl ? r.left + WD_FRAMERECT_LEFT : r.left + square.width + 15;
838 uint text_right = rtl ? r.right - square.width - 15 : r.right - WD_FRAMERECT_RIGHT;
839 uint square_left = rtl ? r.right - square.width - 5 : r.left + 5;
840 uint warning_left = rtl ? r.right - square.width - warning.width - 10 : r.left + square.width + 10;
842 int i = 0;
843 for (const GRFConfig *c = this->actives; c != nullptr; c = c->next, i++) {
844 if (this->vscroll->IsVisible(i)) {
845 const char *text = c->GetName();
846 bool h = (this->active_sel == c);
847 PaletteID pal = this->GetPalette(c);
849 if (h) {
850 GfxFillRect(r.left + 1, y, r.right - 1, y + step_height - 1, PC_DARK_BLUE);
851 } else if (i == this->active_over) {
852 /* Get index of current selection. */
853 int active_sel_pos = 0;
854 for (GRFConfig *c = this->actives; c != nullptr && c != this->active_sel; c = c->next, active_sel_pos++) {}
855 if (active_sel_pos != this->active_over) {
856 uint top = this->active_over < active_sel_pos ? y + 1 : y + step_height - 2;
857 GfxFillRect(r.left + WD_FRAMERECT_LEFT, top - 1, r.right - WD_FRAMERECT_RIGHT, top + 1, PC_GREY);
860 DrawSprite(SPR_SQUARE, pal, square_left, y + square_offset_y);
861 if (c->error != nullptr) DrawSprite(SPR_WARNING_SIGN, 0, warning_left, y + warning_offset_y);
862 uint txtoffset = c->error == nullptr ? 0 : warning.width;
863 DrawString(text_left + (rtl ? 0 : txtoffset), text_right - (rtl ? txtoffset : 0), y + offset_y, text, h ? TC_WHITE : TC_ORANGE);
864 y += step_height;
867 if (i == this->active_over && this->vscroll->IsVisible(i)) { // Highlight is after the last GRF entry.
868 GfxFillRect(r.left + WD_FRAMERECT_LEFT, y, r.right - WD_FRAMERECT_RIGHT, y + 2, PC_GREY);
870 break;
873 case WID_NS_AVAIL_LIST: {
874 GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, this->active_over == -2 ? PC_DARK_GREY : PC_BLACK);
876 uint step_height = this->GetWidget<NWidgetBase>(WID_NS_AVAIL_LIST)->resize_y;
877 int offset_y = (step_height - FONT_HEIGHT_NORMAL) / 2;
878 uint y = r.top + WD_FRAMERECT_TOP;
879 uint min_index = this->vscroll2->GetPosition();
880 uint max_index = min(min_index + this->vscroll2->GetCapacity(), (uint)this->avails.size());
882 for (uint i = min_index; i < max_index; i++) {
883 const GRFConfig *c = this->avails[i];
884 bool h = (c == this->avail_sel);
885 const char *text = c->GetName();
887 if (h) GfxFillRect(r.left + 1, y, r.right - 1, y + step_height - 1, PC_DARK_BLUE);
888 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y + offset_y, text, h ? TC_WHITE : TC_SILVER);
889 y += step_height;
891 break;
894 case WID_NS_NEWGRF_INFO_TITLE:
895 /* Create the nice grayish rectangle at the details top. */
896 GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, PC_DARK_BLUE);
897 DrawString(r.left, r.right, (r.top + r.bottom - FONT_HEIGHT_NORMAL) / 2, STR_NEWGRF_SETTINGS_INFO_TITLE, TC_FROMSTRING, SA_HOR_CENTER);
898 break;
900 case WID_NS_NEWGRF_INFO: {
901 const GRFConfig *selected = this->active_sel;
902 if (selected == nullptr) selected = this->avail_sel;
903 if (selected != nullptr) {
904 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);
906 break;
911 void OnClick(Point pt, int widget, int click_count) override
913 if (widget >= WID_NS_NEWGRF_TEXTFILE && widget < WID_NS_NEWGRF_TEXTFILE + TFT_END) {
914 if (this->active_sel == nullptr && this->avail_sel == nullptr) return;
916 ShowNewGRFTextfileWindow((TextfileType)(widget - WID_NS_NEWGRF_TEXTFILE), this->active_sel != nullptr ? this->active_sel : this->avail_sel);
917 return;
920 switch (widget) {
921 case WID_NS_PRESET_LIST: {
922 DropDownList list;
924 /* Add 'None' option for clearing list */
925 list.emplace_back(new DropDownListStringItem(STR_NONE, -1, false));
927 for (uint i = 0; i < this->grf_presets.size(); i++) {
928 list.emplace_back(new DropDownListCharStringItem(this->grf_presets[i].c_str(), i, false));
931 this->DeleteChildWindows(WC_QUERY_STRING); // Remove the parameter query window
932 ShowDropDownList(this, std::move(list), this->preset, WID_NS_PRESET_LIST);
933 break;
936 case WID_NS_OPEN_URL: {
937 const GRFConfig *c = (this->avail_sel == nullptr) ? this->active_sel : this->avail_sel;
939 extern void OpenBrowser(const char *url);
940 OpenBrowser(c->GetURL());
941 break;
944 case WID_NS_PRESET_SAVE:
945 ShowSavePresetWindow((this->preset == -1) ? nullptr : this->grf_presets[this->preset].c_str());
946 break;
948 case WID_NS_PRESET_DELETE:
949 if (this->preset == -1) return;
951 DeleteGRFPresetFromConfig(this->grf_presets[this->preset].c_str());
952 this->grf_presets = GetGRFPresetList();
953 this->preset = -1;
954 this->InvalidateData();
955 this->DeleteChildWindows(WC_QUERY_STRING); // Remove the parameter query window
956 break;
958 case WID_NS_MOVE_UP: { // Move GRF up
959 if (this->active_sel == nullptr || !this->editable) break;
961 int pos = 0;
962 for (GRFConfig **pc = &this->actives; *pc != nullptr; pc = &(*pc)->next, pos++) {
963 GRFConfig *c = *pc;
964 if (c->next == this->active_sel) {
965 c->next = this->active_sel->next;
966 this->active_sel->next = c;
967 *pc = this->active_sel;
968 break;
971 this->vscroll->ScrollTowards(pos);
972 this->preset = -1;
973 this->InvalidateData();
974 break;
977 case WID_NS_MOVE_DOWN: { // Move GRF down
978 if (this->active_sel == nullptr || !this->editable) break;
980 int pos = 1; // Start at 1 as we swap the selected newgrf with the next one
981 for (GRFConfig **pc = &this->actives; *pc != nullptr; pc = &(*pc)->next, pos++) {
982 GRFConfig *c = *pc;
983 if (c == this->active_sel) {
984 *pc = c->next;
985 c->next = c->next->next;
986 (*pc)->next = c;
987 break;
990 this->vscroll->ScrollTowards(pos);
991 this->preset = -1;
992 this->InvalidateData();
993 break;
996 case WID_NS_FILE_LIST: { // Select an active GRF.
997 ResetObjectToPlace();
999 uint i = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_NS_FILE_LIST);
1001 GRFConfig *c;
1002 for (c = this->actives; c != nullptr && i > 0; c = c->next, i--) {}
1004 if (this->active_sel != c) {
1005 DeleteWindowByClass(WC_GRF_PARAMETERS);
1006 DeleteWindowByClass(WC_TEXTFILE);
1008 this->active_sel = c;
1009 this->avail_sel = nullptr;
1010 this->avail_pos = -1;
1012 this->InvalidateData();
1013 if (click_count == 1) {
1014 if (this->editable && this->active_sel != nullptr) SetObjectToPlaceWnd(SPR_CURSOR_MOUSE, PAL_NONE, HT_DRAG, this);
1015 break;
1017 /* With double click, continue */
1018 FALLTHROUGH;
1021 case WID_NS_REMOVE: { // Remove GRF
1022 if (this->active_sel == nullptr || !this->editable) break;
1023 DeleteWindowByClass(WC_GRF_PARAMETERS);
1024 DeleteWindowByClass(WC_TEXTFILE);
1026 /* Choose the next GRF file to be the selected file. */
1027 GRFConfig *newsel = this->active_sel->next;
1028 for (GRFConfig **pc = &this->actives; *pc != nullptr; pc = &(*pc)->next) {
1029 GRFConfig *c = *pc;
1030 /* If the new selection is empty (i.e. we're deleting the last item
1031 * in the list, pick the file just before the selected file */
1032 if (newsel == nullptr && c->next == this->active_sel) newsel = c;
1034 if (c == this->active_sel) {
1035 if (newsel == c) newsel = nullptr;
1037 *pc = c->next;
1038 delete c;
1039 break;
1043 this->active_sel = newsel;
1044 this->preset = -1;
1045 this->avail_pos = -1;
1046 this->avail_sel = nullptr;
1047 this->avails.ForceRebuild();
1048 this->InvalidateData(GOID_NEWGRF_LIST_EDITED);
1049 break;
1052 case WID_NS_UPGRADE: { // Upgrade GRF.
1053 if (!this->editable || this->actives == nullptr) break;
1054 UpgradeCurrent();
1055 this->InvalidateData(GOID_NEWGRF_LIST_EDITED);
1056 break;
1059 case WID_NS_AVAIL_LIST: { // Select a non-active GRF.
1060 ResetObjectToPlace();
1062 uint i = this->vscroll2->GetScrolledRowFromWidget(pt.y, this, WID_NS_AVAIL_LIST);
1063 this->active_sel = nullptr;
1064 DeleteWindowByClass(WC_GRF_PARAMETERS);
1065 if (i < this->avails.size()) {
1066 if (this->avail_sel != this->avails[i]) DeleteWindowByClass(WC_TEXTFILE);
1067 this->avail_sel = this->avails[i];
1068 this->avail_pos = i;
1070 this->InvalidateData();
1071 if (click_count == 1) {
1072 if (this->editable && this->avail_sel != nullptr && !HasBit(this->avail_sel->flags, GCF_INVALID)) SetObjectToPlaceWnd(SPR_CURSOR_MOUSE, PAL_NONE, HT_DRAG, this);
1073 break;
1075 /* With double click, continue */
1076 FALLTHROUGH;
1079 case WID_NS_ADD:
1080 if (this->avail_sel == nullptr || !this->editable || HasBit(this->avail_sel->flags, GCF_INVALID)) break;
1082 this->AddGRFToActive();
1083 break;
1085 case WID_NS_APPLY_CHANGES: // Apply changes made to GRF list
1086 if (!this->editable) break;
1087 if (this->execute) {
1088 ShowQuery(
1089 STR_NEWGRF_POPUP_CAUTION_CAPTION,
1090 STR_NEWGRF_CONFIRMATION_TEXT,
1091 this,
1092 NewGRFConfirmationCallback
1094 } else {
1095 CopyGRFConfigList(this->orig_list, this->actives, true);
1096 ResetGRFConfig(false);
1097 ReloadNewGRFData();
1099 this->DeleteChildWindows(WC_QUERY_STRING); // Remove the parameter query window
1100 break;
1102 case WID_NS_VIEW_PARAMETERS:
1103 case WID_NS_SET_PARAMETERS: { // Edit parameters
1104 if (this->active_sel == nullptr || !this->show_params || this->active_sel->num_valid_params == 0) break;
1106 OpenGRFParameterWindow(this->active_sel, this->editable);
1107 break;
1110 case WID_NS_TOGGLE_PALETTE:
1111 if (this->active_sel != nullptr && this->editable) {
1112 this->active_sel->palette ^= GRFP_USE_MASK;
1113 this->SetDirty();
1115 break;
1117 case WID_NS_CONTENT_DOWNLOAD:
1118 case WID_NS_CONTENT_DOWNLOAD2:
1119 if (!_network_available) {
1120 ShowErrorMessage(STR_NETWORK_ERROR_NOTAVAILABLE, INVALID_STRING_ID, WL_ERROR);
1121 } else {
1122 this->DeleteChildWindows(WC_QUERY_STRING); // Remove the parameter query window
1124 ShowMissingContentWindow(this->actives);
1126 break;
1128 case WID_NS_RESCAN_FILES:
1129 case WID_NS_RESCAN_FILES2:
1130 ScanNewGRFFiles(this);
1131 break;
1135 void OnNewGRFsScanned() override
1137 if (this->active_sel == nullptr) DeleteWindowByClass(WC_TEXTFILE);
1138 this->avail_sel = nullptr;
1139 this->avail_pos = -1;
1140 this->avails.ForceRebuild();
1141 this->DeleteChildWindows(WC_QUERY_STRING); // Remove the parameter query window
1144 void OnDropdownSelect(int widget, int index) override
1146 if (!this->editable) return;
1148 ClearGRFConfigList(&this->actives);
1149 this->preset = index;
1151 if (index != -1) {
1152 this->actives = LoadGRFPresetFromConfig(this->grf_presets[index].c_str());
1154 this->avails.ForceRebuild();
1156 ResetObjectToPlace();
1157 DeleteWindowByClass(WC_GRF_PARAMETERS);
1158 DeleteWindowByClass(WC_TEXTFILE);
1159 this->active_sel = nullptr;
1160 this->InvalidateData(GOID_NEWGRF_PRESET_LOADED);
1163 void OnQueryTextFinished(char *str) override
1165 if (str == nullptr) return;
1167 SaveGRFPresetToConfig(str, this->actives);
1168 this->grf_presets = GetGRFPresetList();
1170 /* Switch to this preset */
1171 for (uint i = 0; i < this->grf_presets.size(); i++) {
1172 if (this->grf_presets[i] == str) {
1173 this->preset = i;
1174 break;
1178 this->InvalidateData();
1182 * Some data on this window has become invalid.
1183 * @param data Information about the changed data. @see GameOptionsInvalidationData
1184 * @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.
1186 void OnInvalidateData(int data = 0, bool gui_scope = true) override
1188 if (!gui_scope) return;
1189 switch (data) {
1190 default:
1191 /* Nothing important to do */
1192 break;
1194 case GOID_NEWGRF_RESCANNED:
1195 /* Search the list for items that are now found and mark them as such. */
1196 for (GRFConfig **l = &this->actives; *l != nullptr; l = &(*l)->next) {
1197 GRFConfig *c = *l;
1198 bool compatible = HasBit(c->flags, GCF_COMPATIBLE);
1199 if (c->status != GCS_NOT_FOUND && !compatible) continue;
1201 const GRFConfig *f = FindGRFConfig(c->ident.grfid, FGCM_EXACT, compatible ? c->original_md5sum : c->ident.md5sum);
1202 if (f == nullptr || HasBit(f->flags, GCF_INVALID)) continue;
1204 *l = new GRFConfig(*f);
1205 (*l)->next = c->next;
1207 if (this->active_sel == c) this->active_sel = *l;
1209 delete c;
1212 this->avails.ForceRebuild();
1213 FALLTHROUGH;
1215 case GOID_NEWGRF_LIST_EDITED:
1216 this->preset = -1;
1217 FALLTHROUGH;
1219 case GOID_NEWGRF_PRESET_LOADED: {
1220 /* Update scrollbars */
1221 int i = 0;
1222 for (const GRFConfig *c = this->actives; c != nullptr; c = c->next, i++) {}
1224 this->vscroll->SetCount(i + 1); // Reserve empty space for drag and drop handling.
1226 if (this->avail_pos >= 0) this->vscroll2->ScrollTowards(this->avail_pos);
1227 break;
1231 this->BuildAvailables();
1233 this->SetWidgetsDisabledState(!this->editable,
1234 WID_NS_PRESET_LIST,
1235 WID_NS_APPLY_CHANGES,
1236 WID_NS_TOGGLE_PALETTE,
1237 WIDGET_LIST_END
1239 this->SetWidgetDisabledState(WID_NS_ADD, !this->editable || this->avail_sel == nullptr || HasBit(this->avail_sel->flags, GCF_INVALID));
1240 this->SetWidgetDisabledState(WID_NS_UPGRADE, !this->editable || this->actives == nullptr || !this->CanUpgradeCurrent());
1242 bool disable_all = this->active_sel == nullptr || !this->editable;
1243 this->SetWidgetsDisabledState(disable_all,
1244 WID_NS_REMOVE,
1245 WID_NS_MOVE_UP,
1246 WID_NS_MOVE_DOWN,
1247 WIDGET_LIST_END
1250 const GRFConfig *c = (this->avail_sel == nullptr) ? this->active_sel : this->avail_sel;
1251 for (TextfileType tft = TFT_BEGIN; tft < TFT_END; tft++) {
1252 this->SetWidgetDisabledState(WID_NS_NEWGRF_TEXTFILE + tft, c == nullptr || c->GetTextfile(tft) == nullptr);
1254 this->SetWidgetDisabledState(WID_NS_OPEN_URL, c == nullptr || StrEmpty(c->GetURL()));
1256 this->SetWidgetDisabledState(WID_NS_SET_PARAMETERS, !this->show_params || this->active_sel == nullptr || this->active_sel->num_valid_params == 0);
1257 this->SetWidgetDisabledState(WID_NS_VIEW_PARAMETERS, !this->show_params || this->active_sel == nullptr || this->active_sel->num_valid_params == 0);
1258 this->SetWidgetDisabledState(WID_NS_TOGGLE_PALETTE, disable_all ||
1259 (!(_settings_client.gui.newgrf_developer_tools || _settings_client.gui.scenario_developer) && ((c->palette & GRFP_GRF_MASK) != GRFP_GRF_UNSET)));
1261 if (!disable_all) {
1262 /* All widgets are now enabled, so disable widgets we can't use */
1263 if (this->active_sel == this->actives) this->DisableWidget(WID_NS_MOVE_UP);
1264 if (this->active_sel->next == nullptr) this->DisableWidget(WID_NS_MOVE_DOWN);
1267 this->SetWidgetDisabledState(WID_NS_PRESET_DELETE, this->preset == -1);
1269 bool has_missing = false;
1270 bool has_compatible = false;
1271 for (const GRFConfig *c = this->actives; !has_missing && c != nullptr; c = c->next) {
1272 has_missing |= c->status == GCS_NOT_FOUND;
1273 has_compatible |= HasBit(c->flags, GCF_COMPATIBLE);
1275 uint32 widget_data;
1276 StringID tool_tip;
1277 if (has_missing || has_compatible) {
1278 widget_data = STR_NEWGRF_SETTINGS_FIND_MISSING_CONTENT_BUTTON;
1279 tool_tip = STR_NEWGRF_SETTINGS_FIND_MISSING_CONTENT_TOOLTIP;
1280 } else {
1281 widget_data = STR_INTRO_ONLINE_CONTENT;
1282 tool_tip = STR_INTRO_TOOLTIP_ONLINE_CONTENT;
1284 this->GetWidget<NWidgetCore>(WID_NS_CONTENT_DOWNLOAD)->widget_data = widget_data;
1285 this->GetWidget<NWidgetCore>(WID_NS_CONTENT_DOWNLOAD)->tool_tip = tool_tip;
1286 this->GetWidget<NWidgetCore>(WID_NS_CONTENT_DOWNLOAD2)->widget_data = widget_data;
1287 this->GetWidget<NWidgetCore>(WID_NS_CONTENT_DOWNLOAD2)->tool_tip = tool_tip;
1289 this->SetWidgetDisabledState(WID_NS_PRESET_SAVE, has_missing);
1292 EventState OnKeyPress(WChar key, uint16 keycode) override
1294 if (!this->editable) return ES_NOT_HANDLED;
1296 switch (keycode) {
1297 case WKC_UP:
1298 /* scroll up by one */
1299 if (this->avail_pos > 0) this->avail_pos--;
1300 break;
1302 case WKC_DOWN:
1303 /* scroll down by one */
1304 if (this->avail_pos < (int)this->avails.size() - 1) this->avail_pos++;
1305 break;
1307 case WKC_PAGEUP:
1308 /* scroll up a page */
1309 this->avail_pos = (this->avail_pos < this->vscroll2->GetCapacity()) ? 0 : this->avail_pos - this->vscroll2->GetCapacity();
1310 break;
1312 case WKC_PAGEDOWN:
1313 /* scroll down a page */
1314 this->avail_pos = min(this->avail_pos + this->vscroll2->GetCapacity(), (int)this->avails.size() - 1);
1315 break;
1317 case WKC_HOME:
1318 /* jump to beginning */
1319 this->avail_pos = 0;
1320 break;
1322 case WKC_END:
1323 /* jump to end */
1324 this->avail_pos = (uint)this->avails.size() - 1;
1325 break;
1327 default:
1328 return ES_NOT_HANDLED;
1331 if (this->avails.size() == 0) this->avail_pos = -1;
1332 if (this->avail_pos >= 0) {
1333 this->active_sel = nullptr;
1334 DeleteWindowByClass(WC_GRF_PARAMETERS);
1335 if (this->avail_sel != this->avails[this->avail_pos]) DeleteWindowByClass(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 = 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 = 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 = 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 DeleteWindowByClass(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 strecpy(ci->name, c->GetName(), lastof(ci->name));
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 = max(min_avs_width, min_acs_width) + INTER_COLUMN_SPACING + min_inf_width;
1624 this->smallest_y = 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 = 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 = min(MAX_EXTRA_INFO_WIDTH, extra_width / 2);
1668 } else {
1669 extra_width = given_width - min_two_columns;
1670 inf_width = 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 = min(avs_extra_width, extra_width);
1681 extra_width -= avs_width;
1682 extra_width -= 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 = 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 = max(*biggest_index, biggest2);
1919 NWidgetBase *inf = MakeNWidgets(_nested_newgrf_infopanel_widgets, lengthof(_nested_newgrf_infopanel_widgets), &biggest2, nullptr);
1920 *biggest_index = 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 DeleteWindowByClass(WC_GRF_PARAMETERS);
1959 DeleteWindowByClass(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();
1977 w->InvalidateData();
1979 ReInitAllWindows();
1980 DeleteWindowByClass(WC_BUILD_OBJECT);
1987 * Setup the NewGRF gui
1988 * @param editable allow the user to make changes to the grfconfig in the window
1989 * @param show_params show information about what parameters are set for the grf files
1990 * @param exec_changes if changes are made to the list (editable is true), apply these
1991 * changes immediately or only update the list
1992 * @param config pointer to a linked-list of grfconfig's that will be shown
1994 void ShowNewGRFSettings(bool editable, bool show_params, bool exec_changes, GRFConfig **config)
1996 DeleteWindowByClass(WC_GAME_OPTIONS);
1997 new NewGRFWindow(&_newgrf_desc, editable, show_params, exec_changes, config);
2000 /** Widget parts of the save preset window. */
2001 static const NWidgetPart _nested_save_preset_widgets[] = {
2002 NWidget(NWID_HORIZONTAL),
2003 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
2004 NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_SAVE_PRESET_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
2005 NWidget(WWT_DEFSIZEBOX, COLOUR_GREY),
2006 EndContainer(),
2007 NWidget(WWT_PANEL, COLOUR_GREY),
2008 NWidget(NWID_HORIZONTAL),
2009 NWidget(WWT_INSET, COLOUR_GREY, WID_SVP_PRESET_LIST), SetPadding(2, 1, 0, 2),
2010 SetDataTip(0x0, STR_SAVE_PRESET_LIST_TOOLTIP), SetResize(1, 10), SetScrollbar(WID_SVP_SCROLLBAR), EndContainer(),
2011 NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_SVP_SCROLLBAR),
2012 EndContainer(),
2013 NWidget(WWT_EDITBOX, COLOUR_GREY, WID_SVP_EDITBOX), SetPadding(3, 2, 2, 2), SetFill(1, 0), SetResize(1, 0),
2014 SetDataTip(STR_SAVE_PRESET_TITLE, STR_SAVE_PRESET_EDITBOX_TOOLTIP),
2015 EndContainer(),
2016 NWidget(NWID_HORIZONTAL),
2017 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SVP_CANCEL), SetDataTip(STR_SAVE_PRESET_CANCEL, STR_SAVE_PRESET_CANCEL_TOOLTIP), SetFill(1, 0), SetResize(1, 0),
2018 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SVP_SAVE), SetDataTip(STR_SAVE_PRESET_SAVE, STR_SAVE_PRESET_SAVE_TOOLTIP), SetFill(1, 0), SetResize(1, 0),
2019 NWidget(WWT_RESIZEBOX, COLOUR_GREY),
2020 EndContainer(),
2023 /** Window description of the preset save window. */
2024 static WindowDesc _save_preset_desc(
2025 WDP_CENTER, "save_preset", 140, 110,
2026 WC_SAVE_PRESET, WC_GAME_OPTIONS,
2027 WDF_MODAL,
2028 _nested_save_preset_widgets, lengthof(_nested_save_preset_widgets)
2031 /** Class for the save preset window. */
2032 struct SavePresetWindow : public Window {
2033 QueryString presetname_editbox; ///< Edit box of the save preset.
2034 StringList presets; ///< Available presets.
2035 Scrollbar *vscroll; ///< Pointer to the scrollbar widget.
2036 int selected; ///< Selected entry in the preset list, or \c -1 if none selected.
2039 * Constructor of the save preset window.
2040 * @param initial_text Initial text to display in the edit box, or \c nullptr.
2042 SavePresetWindow(const char *initial_text) : Window(&_save_preset_desc), presetname_editbox(32)
2044 this->presets = GetGRFPresetList();
2045 this->selected = -1;
2046 if (initial_text != nullptr) {
2047 for (uint i = 0; i < this->presets.size(); i++) {
2048 if (this->presets[i] == initial_text) {
2049 this->selected = i;
2050 break;
2055 this->querystrings[WID_SVP_EDITBOX] = &this->presetname_editbox;
2056 this->presetname_editbox.ok_button = WID_SVP_SAVE;
2057 this->presetname_editbox.cancel_button = WID_SVP_CANCEL;
2059 this->CreateNestedTree();
2060 this->vscroll = this->GetScrollbar(WID_SVP_SCROLLBAR);
2061 this->FinishInitNested(0);
2063 this->vscroll->SetCount((uint)this->presets.size());
2064 this->SetFocusedWidget(WID_SVP_EDITBOX);
2065 if (initial_text != nullptr) this->presetname_editbox.text.Assign(initial_text);
2068 ~SavePresetWindow()
2072 void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
2074 switch (widget) {
2075 case WID_SVP_PRESET_LIST: {
2076 resize->height = FONT_HEIGHT_NORMAL + 2U;
2077 size->height = 0;
2078 for (uint i = 0; i < this->presets.size(); i++) {
2079 Dimension d = GetStringBoundingBox(this->presets[i].c_str());
2080 size->width = max(size->width, d.width + WD_FRAMETEXT_LEFT + WD_FRAMETEXT_RIGHT);
2081 resize->height = max(resize->height, d.height);
2083 size->height = ClampU((uint)this->presets.size(), 5, 20) * resize->height + 1;
2084 break;
2089 void DrawWidget(const Rect &r, int widget) const override
2091 switch (widget) {
2092 case WID_SVP_PRESET_LIST: {
2093 GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, PC_BLACK);
2095 uint step_height = this->GetWidget<NWidgetBase>(WID_SVP_PRESET_LIST)->resize_y;
2096 int offset_y = (step_height - FONT_HEIGHT_NORMAL) / 2;
2097 uint y = r.top + WD_FRAMERECT_TOP;
2098 uint min_index = this->vscroll->GetPosition();
2099 uint max_index = min(min_index + this->vscroll->GetCapacity(), (uint)this->presets.size());
2101 for (uint i = min_index; i < max_index; i++) {
2102 if ((int)i == this->selected) GfxFillRect(r.left + 1, y, r.right - 1, y + step_height - 2, PC_DARK_BLUE);
2104 const char *text = this->presets[i].c_str();
2105 DrawString(r.left + WD_FRAMERECT_LEFT, r.right, y + offset_y, text, ((int)i == this->selected) ? TC_WHITE : TC_SILVER);
2106 y += step_height;
2108 break;
2113 void OnClick(Point pt, int widget, int click_count) override
2115 switch (widget) {
2116 case WID_SVP_PRESET_LIST: {
2117 uint row = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_SVP_PRESET_LIST);
2118 if (row < this->presets.size()) {
2119 this->selected = row;
2120 this->presetname_editbox.text.Assign(this->presets[row].c_str());
2121 this->SetWidgetDirty(WID_SVP_PRESET_LIST);
2122 this->SetWidgetDirty(WID_SVP_EDITBOX);
2124 break;
2127 case WID_SVP_CANCEL:
2128 delete this;
2129 break;
2131 case WID_SVP_SAVE: {
2132 Window *w = FindWindowById(WC_GAME_OPTIONS, WN_GAME_OPTIONS_NEWGRF_STATE);
2133 if (w != nullptr && !StrEmpty(this->presetname_editbox.text.buf)) w->OnQueryTextFinished(this->presetname_editbox.text.buf);
2134 delete this;
2135 break;
2140 void OnResize() override
2142 this->vscroll->SetCapacityFromWidget(this, WID_SVP_PRESET_LIST);
2147 * Open the window for saving a preset.
2148 * @param initial_text Initial text to display in the edit box, or \c nullptr.
2150 static void ShowSavePresetWindow(const char *initial_text)
2152 DeleteWindowByClass(WC_SAVE_PRESET);
2153 new SavePresetWindow(initial_text);
2156 /** Widgets for the progress window. */
2157 static const NWidgetPart _nested_scan_progress_widgets[] = {
2158 NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_NEWGRF_SCAN_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
2159 NWidget(WWT_PANEL, COLOUR_GREY),
2160 NWidget(NWID_HORIZONTAL), SetPIP(20, 0, 20),
2161 NWidget(NWID_VERTICAL), SetPIP(11, 8, 11),
2162 NWidget(WWT_LABEL, INVALID_COLOUR), SetDataTip(STR_NEWGRF_SCAN_MESSAGE, STR_NULL), SetFill(1, 0),
2163 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_SP_PROGRESS_BAR), SetFill(1, 0),
2164 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_SP_PROGRESS_TEXT), SetFill(1, 0),
2165 EndContainer(),
2166 EndContainer(),
2167 EndContainer(),
2170 /** Description of the widgets and other settings of the window. */
2171 static WindowDesc _scan_progress_desc(
2172 WDP_CENTER, nullptr, 0, 0,
2173 WC_MODAL_PROGRESS, WC_NONE,
2175 _nested_scan_progress_widgets, lengthof(_nested_scan_progress_widgets)
2178 /** Window for showing the progress of NewGRF scanning. */
2179 struct ScanProgressWindow : public Window {
2180 char *last_name; ///< The name of the last 'seen' NewGRF.
2181 int scanned; ///< The number of NewGRFs that we have seen.
2183 /** Create the window. */
2184 ScanProgressWindow() : Window(&_scan_progress_desc), last_name(nullptr), scanned(0)
2186 this->InitNested(1);
2189 /** Free the last name buffer. */
2190 ~ScanProgressWindow()
2192 free(last_name);
2195 void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
2197 switch (widget) {
2198 case WID_SP_PROGRESS_BAR: {
2199 SetDParamMaxValue(0, 100);
2200 *size = GetStringBoundingBox(STR_GENERATION_PROGRESS);
2201 /* We need some spacing for the 'border' */
2202 size->height += 8;
2203 size->width += 8;
2204 break;
2207 case WID_SP_PROGRESS_TEXT:
2208 SetDParamMaxDigits(0, 4);
2209 SetDParamMaxDigits(1, 4);
2210 /* We really don't know the width. We could determine it by scanning the NewGRFs,
2211 * but this is the status window for scanning them... */
2212 size->width = max(400U, GetStringBoundingBox(STR_NEWGRF_SCAN_STATUS).width);
2213 size->height = FONT_HEIGHT_NORMAL * 2 + WD_PAR_VSEP_NORMAL;
2214 break;
2218 void DrawWidget(const Rect &r, int widget) const override
2220 switch (widget) {
2221 case WID_SP_PROGRESS_BAR: {
2222 /* Draw the % complete with a bar and a text */
2223 DrawFrameRect(r.left, r.top, r.right, r.bottom, COLOUR_GREY, FR_BORDERONLY);
2224 uint percent = scanned * 100 / max(1U, _settings_client.gui.last_newgrf_count);
2225 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);
2226 SetDParam(0, percent);
2227 DrawString(r.left, r.right, r.top + 5, STR_GENERATION_PROGRESS, TC_FROMSTRING, SA_HOR_CENTER);
2228 break;
2231 case WID_SP_PROGRESS_TEXT:
2232 SetDParam(0, this->scanned);
2233 SetDParam(1, _settings_client.gui.last_newgrf_count);
2234 DrawString(r.left, r.right, r.top, STR_NEWGRF_SCAN_STATUS, TC_FROMSTRING, SA_HOR_CENTER);
2236 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);
2237 break;
2242 * Update the NewGRF scan status.
2243 * @param num The number of NewGRFs scanned so far.
2244 * @param name The name of the last scanned NewGRF.
2246 void UpdateNewGRFScanStatus(uint num, const char *name)
2248 free(this->last_name);
2249 if (name == nullptr) {
2250 char buf[256];
2251 GetString(buf, STR_NEWGRF_SCAN_ARCHIVES, lastof(buf));
2252 this->last_name = stredup(buf);
2253 } else {
2254 this->last_name = stredup(name);
2256 this->scanned = num;
2257 if (num > _settings_client.gui.last_newgrf_count) _settings_client.gui.last_newgrf_count = num;
2259 this->SetDirty();
2264 * Update the NewGRF scan status.
2265 * @param num The number of NewGRFs scanned so far.
2266 * @param name The name of the last scanned NewGRF.
2268 void UpdateNewGRFScanStatus(uint num, const char *name)
2270 ScanProgressWindow *w = dynamic_cast<ScanProgressWindow *>(FindWindowByClass(WC_MODAL_PROGRESS));
2271 if (w == nullptr) w = new ScanProgressWindow();
2272 w->UpdateNewGRFScanStatus(num, name);