OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

newgrf_gui.cpp
Go to the documentation of this file.
1 /* $Id: newgrf_gui.cpp 27381 2015-08-10 20:24:13Z michi_cc $ */
2
3 /*
4 * This file is part of OpenTTD.
5 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
6 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 */
9
12 #include "stdafx.h"
13 #include "error.h"
14 #include "settings_gui.h"
15 #include "newgrf.h"
16 #include "strings_func.h"
17 #include "window_func.h"
18 #include "gamelog.h"
19 #include "settings_type.h"
20 #include "settings_func.h"
21 #include "widgets/dropdown_type.h"
22 #include "widgets/dropdown_func.h"
23 #include "network/network.h"
24 #include "network/network_content.h"
25 #include "sortlist_type.h"
26 #include "stringfilter_type.h"
27 #include "querystring_gui.h"
28 #include "core/geometry_func.hpp"
29 #include "newgrf_text.h"
30 #include "textfile_gui.h"
31 #include "tilehighlight_func.h"
32 #include "fios.h"
33
34 #include "widgets/newgrf_widget.h"
35 #include "widgets/misc_widget.h"
36
37 #include "table/sprites.h"
38
39 #include <map>
40 #include "safeguards.h"
41
42 /* Maximum number of NewGRFs that may be loaded. Six reserved slots are:
43 * 0 - config, 1 - sound, 2 - base, 3 - logos, 4 - climate, 5 - extra */
44 static const int MAX_NEWGRFS = MAX_FILE_SLOTS - 6;
45
49 void ShowNewGRFError()
50 {
51 /* Do not show errors when entering the main screen */
52 if (_game_mode == GM_MENU) return;
53
54 for (const GRFConfig *c = _grfconfig; c != NULL; c = c->next) {
55 /* We only want to show fatal errors */
56 if (c->error == NULL || c->error->severity != STR_NEWGRF_ERROR_MSG_FATAL) continue;
57
58 SetDParam (0, c->error->custom_message == NULL ? c->error->message : STR_JUST_RAW_STRING);
59 SetDParamStr(1, c->error->custom_message);
60 SetDParamStr(2, c->filename);
61 SetDParamStr(3, c->error->data);
62 for (uint i = 0; i < lengthof(c->error->param_value); i++) {
63 SetDParam(4 + i, c->error->param_value[i]);
64 }
65 ShowErrorMessage(STR_NEWGRF_ERROR_FATAL_POPUP, INVALID_STRING_ID, WL_CRITICAL);
66 break;
67 }
68 }
69
70 static void ShowNewGRFInfo(const GRFConfig *c, uint x, uint y, uint right, uint bottom, bool show_params)
71 {
72 if (c->error != NULL) {
73 char message[512];
74 SetDParamStr(0, c->error->custom_message); // is skipped by built-in messages
75 SetDParamStr(1, c->filename);
76 SetDParamStr(2, c->error->data);
77 for (uint i = 0; i < lengthof(c->error->param_value); i++) {
78 SetDParam(3 + i, c->error->param_value[i]);
79 }
80 GetString(message, c->error->custom_message == NULL ? c->error->message : STR_JUST_RAW_STRING, lastof(message));
81
82 SetDParamStr(0, message);
83 y = DrawStringMultiLine(x, right, y, bottom, c->error->severity);
84 }
85
86 /* Draw filename or not if it is not known (GRF sent over internet) */
87 if (c->filename != NULL) {
88 SetDParamStr(0, c->filename);
89 y = DrawStringMultiLine(x, right, y, bottom, STR_NEWGRF_SETTINGS_FILENAME);
90 }
91
92 /* Prepare and draw GRF ID */
93 char buff[256];
94 seprintf(buff, lastof(buff), "%08X", BSWAP32(c->ident.grfid));
95 SetDParamStr(0, buff);
96 y = DrawStringMultiLine(x, right, y, bottom, STR_NEWGRF_SETTINGS_GRF_ID);
97
98 if ((_settings_client.gui.newgrf_developer_tools || _settings_client.gui.newgrf_show_old_versions) && c->version != 0) {
99 SetDParam(0, c->version);
100 y = DrawStringMultiLine(x, right, y, bottom, STR_NEWGRF_SETTINGS_VERSION);
101 }
102 if ((_settings_client.gui.newgrf_developer_tools || _settings_client.gui.newgrf_show_old_versions) && c->min_loadable_version != 0) {
103 SetDParam(0, c->min_loadable_version);
104 y = DrawStringMultiLine(x, right, y, bottom, STR_NEWGRF_SETTINGS_MIN_VERSION);
105 }
106
107 /* Prepare and draw MD5 sum */
108 md5sumToString(buff, lastof(buff), c->ident.md5sum);
109 SetDParamStr(0, buff);
110 y = DrawStringMultiLine(x, right, y, bottom, STR_NEWGRF_SETTINGS_MD5SUM);
111
112 /* Show GRF parameter list */
113 if (show_params) {
114 if (c->num_params > 0) {
115 GRFBuildParamList(buff, c, lastof(buff));
116 SetDParam(0, STR_JUST_RAW_STRING);
117 SetDParamStr(1, buff);
118 } else {
119 SetDParam(0, STR_LAND_AREA_INFORMATION_LOCAL_AUTHORITY_NONE);
120 }
121 y = DrawStringMultiLine(x, right, y, bottom, STR_NEWGRF_SETTINGS_PARAMETER);
122
123 /* Draw the palette of the NewGRF */
124 if (c->palette & GRFP_BLT_32BPP) {
125 SetDParamStr(0, (c->palette & GRFP_USE_WINDOWS) ? "Legacy (W) / 32 bpp" : "Default (D) / 32 bpp");
126 } else {
127 SetDParamStr(0, (c->palette & GRFP_USE_WINDOWS) ? "Legacy (W)" : "Default (D)");
128 }
129 y = DrawStringMultiLine(x, right, y, bottom, STR_NEWGRF_SETTINGS_PALETTE);
130 }
131
132 /* Show flags */
133 if (c->status == GCS_NOT_FOUND) y = DrawStringMultiLine(x, right, y, bottom, STR_NEWGRF_SETTINGS_NOT_FOUND);
134 if (c->status == GCS_DISABLED) y = DrawStringMultiLine(x, right, y, bottom, STR_NEWGRF_SETTINGS_DISABLED);
135 if (HasBit(c->flags, GCF_INVALID)) y = DrawStringMultiLine(x, right, y, bottom, STR_NEWGRF_SETTINGS_INCOMPATIBLE);
136 if (HasBit(c->flags, GCF_COMPATIBLE)) y = DrawStringMultiLine(x, right, y, bottom, STR_NEWGRF_COMPATIBLE_LOADED);
137
138 /* Draw GRF info if it exists */
139 if (!StrEmpty(c->GetDescription())) {
140 SetDParamStr(0, c->GetDescription());
141 y = DrawStringMultiLine(x, right, y, bottom, STR_BLACK_RAW_STRING);
142 } else {
143 y = DrawStringMultiLine(x, right, y, bottom, STR_NEWGRF_SETTINGS_NO_INFO);
144 }
145 }
146
150 struct NewGRFParametersWindow : public Window {
151 static GRFParameterInfo dummy_parameter_info;
152 GRFConfig *grf_config;
153 uint clicked_button;
154 bool clicked_increase;
155 bool clicked_dropdown;
156 bool closing_dropdown;
157 int timeout;
158 uint clicked_row;
159 int line_height;
160 Scrollbar *vscroll;
161 bool action14present;
162 bool editable;
163
164 NewGRFParametersWindow(WindowDesc *desc, GRFConfig *c, bool editable) : Window(desc),
165 grf_config(c),
166 clicked_button(UINT_MAX),
167 clicked_dropdown(false),
168 closing_dropdown(false),
169 timeout(0),
170 clicked_row(UINT_MAX),
171 editable(editable)
172 {
173 this->action14present = (c->num_valid_params != lengthof(c->param) || c->param_info.Length() != 0);
174
175 this->CreateNestedTree();
176 this->vscroll = this->GetScrollbar(WID_NP_SCROLLBAR);
177 this->GetWidget<NWidgetStacked>(WID_NP_SHOW_NUMPAR)->SetDisplayedPlane(this->action14present ? SZSP_HORIZONTAL : 0);
178 this->GetWidget<NWidgetStacked>(WID_NP_SHOW_DESCRIPTION)->SetDisplayedPlane(this->action14present ? 0 : SZSP_HORIZONTAL);
179 this->FinishInitNested(); // Initializes 'this->line_height' as side effect.
180
181 this->SetWidgetDisabledState(WID_NP_RESET, !this->editable);
182
183 this->InvalidateData();
184 }
185
191 static GRFParameterInfo *GetDummyParameterInfo(uint nr)
192 {
193 dummy_parameter_info.param_nr = nr;
194 return &dummy_parameter_info;
195 }
196
197 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
198 {
199 switch (widget) {
200 case WID_NP_NUMPAR_DEC:
201 case WID_NP_NUMPAR_INC: {
202 size->width = max(SETTING_BUTTON_WIDTH / 2, FONT_HEIGHT_NORMAL);
203 size->height = max(SETTING_BUTTON_HEIGHT, FONT_HEIGHT_NORMAL);
204 break;
205 }
206
207 case WID_NP_NUMPAR: {
208 SetDParamMaxValue(0, lengthof(this->grf_config->param));
209 Dimension d = GetStringBoundingBox(this->GetWidget<NWidgetCore>(widget)->widget_data);
210 d.width += padding.width;
211 d.height += padding.height;
212 *size = maxdim(*size, d);
213 break;
214 }
215
216 case WID_NP_BACKGROUND:
217 this->line_height = max(SETTING_BUTTON_HEIGHT, FONT_HEIGHT_NORMAL) + WD_MATRIX_TOP + WD_MATRIX_BOTTOM;
218
219 resize->width = 1;
220 resize->height = this->line_height;
221 size->height = 5 * this->line_height;
222 break;
223
224 case WID_NP_DESCRIPTION:
225 /* Minimum size of 4 lines. The 500 is the default size of the window. */
226 Dimension suggestion = {500 - WD_FRAMERECT_LEFT - WD_FRAMERECT_RIGHT, (uint)FONT_HEIGHT_NORMAL * 4 + WD_TEXTPANEL_TOP + WD_TEXTPANEL_BOTTOM};
227 for (uint i = 0; i < this->grf_config->param_info.Length(); i++) {
228 const GRFParameterInfo *par_info = this->grf_config->param_info[i];
229 if (par_info == NULL) continue;
230 const char *desc = GetGRFStringFromGRFText(par_info->desc);
231 if (desc == NULL) continue;
232 Dimension d = GetStringMultiLineBoundingBox(desc, suggestion);
233 d.height += WD_TEXTPANEL_TOP + WD_TEXTPANEL_BOTTOM;
234 suggestion = maxdim(d, suggestion);
235 }
236 size->height = suggestion.height;
237 break;
238 }
239 }
240
241 virtual void SetStringParameters(int widget) const
242 {
243 switch (widget) {
244 case WID_NP_NUMPAR:
245 SetDParam(0, this->vscroll->GetCount());
246 break;
247 }
248 }
249
250 virtual void DrawWidget(const Rect &r, int widget) const
251 {
252 if (widget == WID_NP_DESCRIPTION) {
253 const GRFParameterInfo *par_info = (this->clicked_row < this->grf_config->param_info.Length()) ? this->grf_config->param_info[this->clicked_row] : NULL;
254 if (par_info == NULL) return;
255 const char *desc = GetGRFStringFromGRFText(par_info->desc);
256 if (desc == NULL) return;
257 DrawStringMultiLine(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + WD_TEXTPANEL_TOP, r.bottom - WD_TEXTPANEL_BOTTOM, desc, TC_BLACK);
258 return;
259 } else if (widget != WID_NP_BACKGROUND) {
260 return;
261 }
262
263 bool rtl = _current_text_dir == TD_RTL;
264 uint buttons_left = rtl ? r.right - SETTING_BUTTON_WIDTH - 3 : r.left + 4;
265 uint text_left = r.left + (rtl ? WD_FRAMERECT_LEFT : SETTING_BUTTON_WIDTH + 8);
266 uint text_right = r.right - (rtl ? SETTING_BUTTON_WIDTH + 8 : WD_FRAMERECT_RIGHT);
267
268 int y = r.top;
269 int button_y_offset = (this->line_height - SETTING_BUTTON_HEIGHT) / 2;
270 int text_y_offset = (this->line_height - FONT_HEIGHT_NORMAL) / 2;
271 for (uint i = this->vscroll->GetPosition(); this->vscroll->IsVisible(i) && i < this->vscroll->GetCount(); i++) {
272 GRFParameterInfo *par_info = (i < this->grf_config->param_info.Length()) ? this->grf_config->param_info[i] : NULL;
273 if (par_info == NULL) par_info = GetDummyParameterInfo(i);
274 uint32 current_value = par_info->GetValue(this->grf_config);
275 bool selected = (i == this->clicked_row);
276
277 if (par_info->type == PTYPE_BOOL) {
278 DrawBoolButton(buttons_left, y + button_y_offset, current_value != 0, this->editable);
279 SetDParam(2, par_info->GetValue(this->grf_config) == 0 ? STR_CONFIG_SETTING_OFF : STR_CONFIG_SETTING_ON);
280 } else if (par_info->type == PTYPE_UINT_ENUM) {
281 if (par_info->complete_labels) {
282 DrawDropDownButton(buttons_left, y + button_y_offset, COLOUR_YELLOW, this->clicked_row == i && this->clicked_dropdown, this->editable);
283 } else {
284 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);
285 }
286 SetDParam(2, STR_JUST_INT);
287 SetDParam(3, current_value);
288 if (par_info->value_names.Contains(current_value)) {
289 const char *label = GetGRFStringFromGRFText(par_info->value_names.Find(current_value)->second);
290 if (label != NULL) {
291 SetDParam(2, STR_JUST_RAW_STRING);
292 SetDParamStr(3, label);
293 }
294 }
295 }
296
297 const char *name = GetGRFStringFromGRFText(par_info->name);
298 if (name != NULL) {
299 SetDParam(0, STR_JUST_RAW_STRING);
300 SetDParamStr(1, name);
301 } else {
302 SetDParam(0, STR_NEWGRF_PARAMETERS_DEFAULT_NAME);
303 SetDParam(1, i + 1);
304 }
305
306 DrawString(text_left, text_right, y + text_y_offset, STR_NEWGRF_PARAMETERS_SETTING, selected ? TC_WHITE : TC_LIGHT_BLUE);
307 y += this->line_height;
308 }
309 }
310
311 virtual void OnPaint()
312 {
313 if (this->closing_dropdown) {
314 this->closing_dropdown = false;
315 this->clicked_dropdown = false;
316 }
317 this->DrawWidgets();
318 }
319
320 virtual void OnClick(Point pt, int widget, int click_count)
321 {
322 switch (widget) {
323 case WID_NP_NUMPAR_DEC:
324 if (this->editable && !this->action14present && this->grf_config->num_params > 0) {
325 this->grf_config->num_params--;
326 this->InvalidateData();
327 SetWindowDirty(WC_GAME_OPTIONS, WN_GAME_OPTIONS_NEWGRF_STATE);
328 }
329 break;
330
331 case WID_NP_NUMPAR_INC: {
332 GRFConfig *c = this->grf_config;
333 if (this->editable && !this->action14present && c->num_params < c->num_valid_params) {
334 c->param[c->num_params++] = 0;
335 this->InvalidateData();
336 SetWindowDirty(WC_GAME_OPTIONS, WN_GAME_OPTIONS_NEWGRF_STATE);
337 }
338 break;
339 }
340
341 case WID_NP_BACKGROUND: {
342 if (!this->editable) break;
343 uint num = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_NP_BACKGROUND);
344 if (num >= this->vscroll->GetCount()) break;
345 if (this->clicked_row != num) {
346 DeleteChildWindows(WC_QUERY_STRING);
347 HideDropDownMenu(this);
348 this->clicked_row = num;
349 this->clicked_dropdown = false;
350 }
351
352 const NWidgetBase *wid = this->GetWidget<NWidgetBase>(WID_NP_BACKGROUND);
353 int x = pt.x - wid->pos_x;
354 if (_current_text_dir == TD_RTL) x = wid->current_x - 1 - x;
355 x -= 4;
356
357 GRFParameterInfo *par_info = (num < this->grf_config->param_info.Length()) ? this->grf_config->param_info[num] : NULL;
358 if (par_info == NULL) par_info = GetDummyParameterInfo(num);
359
360 /* One of the arrows is clicked */
361 uint32 old_val = par_info->GetValue(this->grf_config);
362 if (par_info->type != PTYPE_BOOL && IsInsideMM(x, 0, SETTING_BUTTON_WIDTH) && par_info->complete_labels) {
363 if (this->clicked_dropdown) {
364 /* unclick the dropdown */
365 HideDropDownMenu(this);
366 this->clicked_dropdown = false;
367 this->closing_dropdown = false;
368 } else {
369 const NWidgetBase *wid = this->GetWidget<NWidgetBase>(WID_NP_BACKGROUND);
370 int rel_y = (pt.y - (int)wid->pos_y) % this->line_height;
371
372 Rect wi_rect;
373 wi_rect.left = pt.x - (_current_text_dir == TD_RTL ? SETTING_BUTTON_WIDTH - 1 - x : x);;
374 wi_rect.right = wi_rect.left + SETTING_BUTTON_WIDTH - 1;
375 wi_rect.top = pt.y - rel_y + (this->line_height - SETTING_BUTTON_HEIGHT) / 2;
376 wi_rect.bottom = wi_rect.top + SETTING_BUTTON_HEIGHT - 1;
377
378 /* For dropdowns we also have to check the y position thoroughly, the mouse may not above the just opening dropdown */
379 if (pt.y >= wi_rect.top && pt.y <= wi_rect.bottom) {
380 this->clicked_dropdown = true;
381 this->closing_dropdown = false;
382
383 DropDownList *list = new DropDownList();
384 for (uint32 i = par_info->min_value; i <= par_info->max_value; i++) {
385 *list->Append() = new DropDownListCharStringItem(GetGRFStringFromGRFText(par_info->value_names.Find(i)->second), i, false);
386 }
387
388 ShowDropDownListAt(this, list, old_val, -1, wi_rect, COLOUR_ORANGE, true);
389 }
390 }
391 } else if (IsInsideMM(x, 0, SETTING_BUTTON_WIDTH)) {
392 uint32 val = old_val;
393 if (par_info->type == PTYPE_BOOL) {
394 val = !val;
395 } else {
396 if (x >= SETTING_BUTTON_WIDTH / 2) {
397 /* Increase button clicked */
398 if (val < par_info->max_value) val++;
399 this->clicked_increase = true;
400 } else {
401 /* Decrease button clicked */
402 if (val > par_info->min_value) val--;
403 this->clicked_increase = false;
404 }
405 }
406 if (val != old_val) {
407 par_info->SetValue(this->grf_config, val);
408
409 this->clicked_button = num;
410 this->timeout = 5;
411 }
412 } else if (par_info->type == PTYPE_UINT_ENUM && !par_info->complete_labels && click_count >= 2) {
413 /* Display a query box so users can enter a custom value. */
414 SetDParam(0, old_val);
415 ShowQueryString(STR_JUST_INT, STR_CONFIG_SETTING_QUERY_CAPTION, 10, this, CS_NUMERAL, QSF_NONE);
416 }
417 this->SetDirty();
418 break;
419 }
420
421 case WID_NP_RESET:
422 if (!this->editable) break;
423 this->grf_config->SetParameterDefaults();
424 this->InvalidateData();
425 SetWindowDirty(WC_GAME_OPTIONS, WN_GAME_OPTIONS_NEWGRF_STATE);
426 break;
427
428 case WID_NP_ACCEPT:
429 delete this;
430 break;
431 }
432 }
433
434 virtual void OnQueryTextFinished(char *str)
435 {
436 if (StrEmpty(str)) return;
437 int32 value = atoi(str);
438 GRFParameterInfo *par_info = ((uint)this->clicked_row < this->grf_config->param_info.Length()) ? this->grf_config->param_info[this->clicked_row] : NULL;
439 if (par_info == NULL) par_info = GetDummyParameterInfo(this->clicked_row);
440 uint32 val = Clamp<uint32>(value, par_info->min_value, par_info->max_value);
441 par_info->SetValue(this->grf_config, val);
442 this->SetDirty();
443 }
444
445 virtual void OnDropdownSelect(int widget, int index)
446 {
447 assert(this->clicked_dropdown);
448 GRFParameterInfo *par_info = ((uint)this->clicked_row < this->grf_config->param_info.Length()) ? this->grf_config->param_info[this->clicked_row] : NULL;
449 if (par_info == NULL) par_info = GetDummyParameterInfo(this->clicked_row);
450 par_info->SetValue(this->grf_config, index);
451 this->SetDirty();
452 }
453
454 virtual void OnDropdownClose(Point pt, int widget, int index, bool instant_close)
455 {
456 /* We cannot raise the dropdown button just yet. OnClick needs some hint, whether
457 * the same dropdown button was clicked again, and then not open the dropdown again.
458 * So, we only remember that it was closed, and process it on the next OnPaint, which is
459 * after OnClick. */
460 assert(this->clicked_dropdown);
461 this->closing_dropdown = true;
462 this->SetDirty();
463 }
464
465 virtual void OnResize()
466 {
467 this->vscroll->SetCapacityFromWidget(this, WID_NP_BACKGROUND);
468 }
469
475 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
476 {
477 if (!gui_scope) return;
478 if (!this->action14present) {
479 this->SetWidgetDisabledState(WID_NP_NUMPAR_DEC, !this->editable || this->grf_config->num_params == 0);
480 this->SetWidgetDisabledState(WID_NP_NUMPAR_INC, !this->editable || this->grf_config->num_params >= this->grf_config->num_valid_params);
481 }
482
483 this->vscroll->SetCount(this->action14present ? this->grf_config->num_valid_params : this->grf_config->num_params);
484 if (this->clicked_row != UINT_MAX && this->clicked_row >= this->vscroll->GetCount()) {
485 this->clicked_row = UINT_MAX;
486 DeleteChildWindows(WC_QUERY_STRING);
487 }
488 }
489
490 virtual void OnTick()
491 {
492 if (--this->timeout == 0) {
493 this->clicked_button = UINT_MAX;
494 this->SetDirty();
495 }
496 }
497 };
498 GRFParameterInfo NewGRFParametersWindow::dummy_parameter_info(0);
499
500
501 static const NWidgetPart _nested_newgrf_parameter_widgets[] = {
502 NWidget(NWID_HORIZONTAL),
503 NWidget(WWT_CLOSEBOX, COLOUR_MAUVE),
504 NWidget(WWT_CAPTION, COLOUR_MAUVE), SetDataTip(STR_NEWGRF_PARAMETERS_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
505 NWidget(WWT_DEFSIZEBOX, COLOUR_MAUVE),
506 EndContainer(),
507 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_NP_SHOW_NUMPAR),
508 NWidget(WWT_PANEL, COLOUR_MAUVE), SetResize(1, 0), SetFill(1, 0), SetPIP(4, 0, 4),
509 NWidget(NWID_HORIZONTAL), SetPIP(4, 0, 4),
510 NWidget(WWT_PUSHARROWBTN, COLOUR_YELLOW, WID_NP_NUMPAR_DEC), SetMinimalSize(12, 12), SetDataTip(AWV_DECREASE, STR_NULL),
511 NWidget(WWT_PUSHARROWBTN, COLOUR_YELLOW, WID_NP_NUMPAR_INC), SetMinimalSize(12, 12), SetDataTip(AWV_INCREASE, STR_NULL),
512 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),
513 EndContainer(),
514 EndContainer(),
515 EndContainer(),
516 NWidget(NWID_HORIZONTAL),
517 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),
518 NWidget(NWID_VSCROLLBAR, COLOUR_MAUVE, WID_NP_SCROLLBAR),
519 EndContainer(),
520 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_NP_SHOW_DESCRIPTION),
521 NWidget(WWT_PANEL, COLOUR_MAUVE, WID_NP_DESCRIPTION), SetResize(1, 0), SetFill(1, 0),
522 EndContainer(),
523 EndContainer(),
524 NWidget(NWID_HORIZONTAL),
525 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
526 NWidget(WWT_PUSHTXTBTN, COLOUR_MAUVE, WID_NP_ACCEPT), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_NEWGRF_PARAMETERS_CLOSE, STR_NULL),
527 NWidget(WWT_PUSHTXTBTN, COLOUR_MAUVE, WID_NP_RESET), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_NEWGRF_PARAMETERS_RESET, STR_NEWGRF_PARAMETERS_RESET_TOOLTIP),
528 EndContainer(),
529 NWidget(WWT_RESIZEBOX, COLOUR_MAUVE),
530 EndContainer(),
531 };
532
534 static WindowDesc _newgrf_parameters_desc(
535 WDP_CENTER, "settings_newgrf_config", 500, 208,
536 WC_GRF_PARAMETERS, WC_NONE,
537 0,
538 _nested_newgrf_parameter_widgets, lengthof(_nested_newgrf_parameter_widgets)
539 );
540
541 static void OpenGRFParameterWindow(GRFConfig *c, bool editable)
542 {
543 DeleteWindowByClass(WC_GRF_PARAMETERS);
544 new NewGRFParametersWindow(&_newgrf_parameters_desc, c, editable);
545 }
546
548 struct NewGRFTextfileWindow : public TextfileWindow {
549 const GRFConfig *grf_config;
550
551 NewGRFTextfileWindow(TextfileType file_type, const GRFConfig *c) : TextfileWindow(file_type), grf_config(c)
552 {
553 const char *textfile = this->grf_config->GetTextfile(file_type);
554 this->LoadTextfile(textfile, NEWGRF_DIR);
555 }
556
557 /* virtual */ void SetStringParameters(int widget) const
558 {
559 if (widget == WID_TF_CAPTION) {
560 SetDParam(0, STR_CONTENT_TYPE_NEWGRF);
561 SetDParamStr(1, this->grf_config->GetName());
562 }
563 }
564 };
565
566 void ShowNewGRFTextfileWindow(TextfileType file_type, const GRFConfig *c)
567 {
568 DeleteWindowByClass(WC_TEXTFILE);
569 new NewGRFTextfileWindow(file_type, c);
570 }
571
572 static GRFPresetList _grf_preset_list;
573
574 class DropDownListPresetItem : public DropDownListItem {
575 public:
576 DropDownListPresetItem(int result) : DropDownListItem(result, false) {}
577
578 virtual ~DropDownListPresetItem() {}
579
580 bool Selectable() const
581 {
582 return true;
583 }
584
585 void Draw(int left, int right, int top, int bottom, bool sel, int bg_colour) const
586 {
587 DrawString(left + 2, right + 2, top, _grf_preset_list[this->result], sel ? TC_WHITE : TC_BLACK);
588 }
589 };
590
591
592 typedef std::map<uint32, const GRFConfig *> GrfIdMap;
593
599 static void FillGrfidMap(const GRFConfig *c, GrfIdMap *grfid_map)
600 {
601 while (c != NULL) {
602 std::pair<uint32, const GRFConfig *> p(c->ident.grfid, c);
603 grfid_map->insert(p);
604 c = c->next;
605 }
606 }
607
608 static void NewGRFConfirmationCallback(Window *w, bool confirmed);
609 static void ShowSavePresetWindow(const char *initial_text);
610
614 struct NewGRFWindow : public Window, NewGRFScanCallback {
615 typedef GUIList<const GRFConfig *, StringFilter &> GUIGRFConfigList;
616
617 static const uint EDITBOX_MAX_SIZE = 50;
618
619 static Listing last_sorting;
620 static Filtering last_filtering;
621 static GUIGRFConfigList::SortFunction * const sorter_funcs[];
622 static GUIGRFConfigList::FilterFunction * const filter_funcs[];
623
624 GUIGRFConfigList avails;
625 const GRFConfig *avail_sel;
626 int avail_pos;
627 StringFilter string_filter;
628 QueryString filter_editbox;
629
630 GRFConfig *actives;
631 GRFConfig *active_sel;
632
633 GRFConfig **orig_list;
634 bool editable;
635 bool show_params;
636 bool execute;
637 int preset;
638 int active_over;
639
640 Scrollbar *vscroll;
641 Scrollbar *vscroll2;
642
643 NewGRFWindow(WindowDesc *desc, bool editable, bool show_params, bool execute, GRFConfig **orig_list) : Window(desc), filter_editbox(EDITBOX_MAX_SIZE)
644 {
645 this->avail_sel = NULL;
646 this->avail_pos = -1;
647 this->active_sel = NULL;
648 this->actives = NULL;
649 this->orig_list = orig_list;
650 this->editable = editable;
651 this->execute = execute;
652 this->show_params = show_params;
653 this->preset = -1;
654 this->active_over = -1;
655
656 CopyGRFConfigList(&this->actives, *orig_list, false);
657 GetGRFPresetList(&_grf_preset_list);
658
659 this->CreateNestedTree();
660 this->vscroll = this->GetScrollbar(WID_NS_SCROLLBAR);
661 this->vscroll2 = this->GetScrollbar(WID_NS_SCROLL2BAR);
662
663 this->GetWidget<NWidgetStacked>(WID_NS_SHOW_REMOVE)->SetDisplayedPlane(this->editable ? 0 : 1);
664 this->GetWidget<NWidgetStacked>(WID_NS_SHOW_APPLY)->SetDisplayedPlane(this->editable ? 0 : this->show_params ? 1 : SZSP_HORIZONTAL);
665 this->FinishInitNested(WN_GAME_OPTIONS_NEWGRF_STATE);
666
667 this->querystrings[WID_NS_FILTER] = &this->filter_editbox;
668 this->filter_editbox.cancel_button = QueryString::ACTION_CLEAR;
669 if (editable) {
670 this->SetFocusedWidget(WID_NS_FILTER);
671 } else {
672 this->DisableWidget(WID_NS_FILTER);
673 }
674
675 this->avails.SetListing(this->last_sorting);
676 this->avails.SetFiltering(this->last_filtering);
677 this->avails.SetSortFuncs(this->sorter_funcs);
678 this->avails.SetFilterFuncs(this->filter_funcs);
679 this->avails.ForceRebuild();
680
681 this->OnInvalidateData(GOID_NEWGRF_LIST_EDITED);
682 }
683
684 ~NewGRFWindow()
685 {
686 DeleteWindowByClass(WC_GRF_PARAMETERS);
687 DeleteWindowByClass(WC_TEXTFILE);
688 DeleteWindowByClass(WC_SAVE_PRESET);
689
690 if (this->editable && !this->execute) {
691 CopyGRFConfigList(this->orig_list, this->actives, true);
692 ResetGRFConfig(false);
693 ReloadNewGRFData();
694 }
695
696 /* Remove the temporary copy of grf-list used in window */
697 ClearGRFConfigList(&this->actives);
698 _grf_preset_list.Clear();
699 }
700
705 bool CanUpgradeCurrent()
706 {
707 GrfIdMap grfid_map;
708 FillGrfidMap(this->actives, &grfid_map);
709
710 for (const GRFConfig *a = _all_grfs; a != NULL; a = a->next) {
711 GrfIdMap::const_iterator iter = grfid_map.find(a->ident.grfid);
712 if (iter != grfid_map.end() && a->version > iter->second->version) return true;
713 }
714 return false;
715 }
716
718 void UpgradeCurrent()
719 {
720 GrfIdMap grfid_map;
721 FillGrfidMap(this->actives, &grfid_map);
722
723 for (const GRFConfig *a = _all_grfs; a != NULL; a = a->next) {
724 GrfIdMap::iterator iter = grfid_map.find(a->ident.grfid);
725 if (iter == grfid_map.end() || iter->second->version >= a->version) continue;
726
727 GRFConfig **c = &this->actives;
728 while (*c != iter->second) c = &(*c)->next;
729 GRFConfig *d = new GRFConfig(*a);
730 d->next = (*c)->next;
731 d->CopyParams(**c);
732 if (this->active_sel == *c) this->active_sel = NULL;
733 delete *c;
734 *c = d;
735 iter->second = d;
736 }
737 }
738
739 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
740 {
741 switch (widget) {
742 case WID_NS_FILE_LIST:
743 {
744 Dimension d = maxdim(GetSpriteSize(SPR_SQUARE), GetSpriteSize(SPR_WARNING_SIGN));
745 resize->height = max(d.height + 2U, FONT_HEIGHT_NORMAL + 2U);
746 size->height = max(size->height, WD_FRAMERECT_TOP + 6 * resize->height + WD_FRAMERECT_BOTTOM);
747 break;
748 }
749
750 case WID_NS_AVAIL_LIST:
751 resize->height = max(12, FONT_HEIGHT_NORMAL + 2);
752 size->height = max(size->height, WD_FRAMERECT_TOP + 8 * resize->height + WD_FRAMERECT_BOTTOM);
753 break;
754
755 case WID_NS_NEWGRF_INFO_TITLE: {
756 Dimension dim = GetStringBoundingBox(STR_NEWGRF_SETTINGS_INFO_TITLE);
757 size->height = max(size->height, dim.height + WD_FRAMETEXT_TOP + WD_FRAMETEXT_BOTTOM);
758 size->width = max(size->width, dim.width + WD_FRAMETEXT_LEFT + WD_FRAMETEXT_RIGHT);
759 break;
760 }
761
762 case WID_NS_NEWGRF_INFO:
763 size->height = max(size->height, WD_FRAMERECT_TOP + 10 * FONT_HEIGHT_NORMAL + WD_FRAMERECT_BOTTOM + padding.height + 2);
764 break;
765
766 case WID_NS_PRESET_LIST: {
767 Dimension d = GetStringBoundingBox(STR_NUM_CUSTOM);
768 for (uint i = 0; i < _grf_preset_list.Length(); i++) {
769 if (_grf_preset_list[i] != NULL) {
770 SetDParamStr(0, _grf_preset_list[i]);
771 d = maxdim(d, GetStringBoundingBox(STR_JUST_RAW_STRING));
772 }
773 }
774 d.width += padding.width;
775 *size = maxdim(d, *size);
776 break;
777 }
778
779 case WID_NS_CONTENT_DOWNLOAD:
780 case WID_NS_CONTENT_DOWNLOAD2: {
781 Dimension d = GetStringBoundingBox(STR_NEWGRF_SETTINGS_FIND_MISSING_CONTENT_BUTTON);
782 *size = maxdim(d, GetStringBoundingBox(STR_INTRO_ONLINE_CONTENT));
783 size->width += padding.width;
784 size->height += padding.height;
785 break;
786 }
787 }
788 }
789
790 virtual void OnResize()
791 {
792 this->vscroll->SetCapacityFromWidget(this, WID_NS_FILE_LIST);
793 this->vscroll2->SetCapacityFromWidget(this, WID_NS_AVAIL_LIST);
794 }
795
796 virtual void SetStringParameters(int widget) const
797 {
798 switch (widget) {
799 case WID_NS_PRESET_LIST:
800 if (this->preset == -1) {
801 SetDParam(0, STR_NUM_CUSTOM);
802 } else {
803 SetDParam(0, STR_JUST_RAW_STRING);
804 SetDParamStr(1, _grf_preset_list[this->preset]);
805 }
806 break;
807 }
808 }
809
815 inline PaletteID GetPalette(const GRFConfig *c) const
816 {
817 PaletteID pal;
818
819 /* Pick a colour */
820 switch (c->status) {
821 case GCS_NOT_FOUND:
822 case GCS_DISABLED:
823 pal = PALETTE_TO_RED;
824 break;
825 case GCS_ACTIVATED:
826 pal = PALETTE_TO_GREEN;
827 break;
828 default:
829 pal = PALETTE_TO_BLUE;
830 break;
831 }
832
833 /* Do not show a "not-failure" colour when it actually failed to load */
834 if (pal != PALETTE_TO_RED) {
835 if (HasBit(c->flags, GCF_STATIC)) {
836 pal = PALETTE_TO_GREY;
837 } else if (HasBit(c->flags, GCF_COMPATIBLE)) {
838 pal = PALETTE_TO_ORANGE;
839 }
840 }
841
842 return pal;
843 }
844
845 virtual void DrawWidget(const Rect &r, int widget) const
846 {
847 switch (widget) {
848 case WID_NS_FILE_LIST: {
849 GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, PC_BLACK);
850
851 uint step_height = this->GetWidget<NWidgetBase>(WID_NS_FILE_LIST)->resize_y;
852 uint y = r.top + WD_FRAMERECT_TOP;
853 Dimension square = GetSpriteSize(SPR_SQUARE);
854 Dimension warning = GetSpriteSize(SPR_WARNING_SIGN);
855 int square_offset_y = (step_height - square.height) / 2;
856 int warning_offset_y = (step_height - warning.height) / 2;
857 int offset_y = (step_height - FONT_HEIGHT_NORMAL) / 2;
858
859 bool rtl = _current_text_dir == TD_RTL;
860 uint text_left = rtl ? r.left + WD_FRAMERECT_LEFT : r.left + square.width + 15;
861 uint text_right = rtl ? r.right - square.width - 15 : r.right - WD_FRAMERECT_RIGHT;
862 uint square_left = rtl ? r.right - square.width - 5 : r.left + 5;
863 uint warning_left = rtl ? r.right - square.width - warning.width - 10 : r.left + square.width + 10;
864
865 int i = 0;
866 for (const GRFConfig *c = this->actives; c != NULL; c = c->next, i++) {
867 if (this->vscroll->IsVisible(i)) {
868 const char *text = c->GetName();
869 bool h = (this->active_sel == c);
870 PaletteID pal = this->GetPalette(c);
871
872 if (h) {
873 GfxFillRect(r.left + 1, y, r.right - 1, y + step_height - 1, PC_DARK_BLUE);
874 } else if (i == this->active_over) {
875 /* Get index of current selection. */
876 int active_sel_pos = 0;
877 for (GRFConfig *c = this->actives; c != NULL && c != this->active_sel; c = c->next, active_sel_pos++) {}
878 if (active_sel_pos != this->active_over) {
879 uint top = this->active_over < active_sel_pos ? y + 1 : y + step_height - 2;
880 GfxFillRect(r.left + WD_FRAMERECT_LEFT, top - 1, r.right - WD_FRAMERECT_RIGHT, top + 1, PC_GREY);
881 }
882 }
883 DrawSprite(SPR_SQUARE, pal, square_left, y + square_offset_y);
884 if (c->error != NULL) DrawSprite(SPR_WARNING_SIGN, 0, warning_left, y + warning_offset_y);
885 uint txtoffset = c->error == NULL ? 0 : warning.width;
886 DrawString(text_left + (rtl ? 0 : txtoffset), text_right - (rtl ? txtoffset : 0), y + offset_y, text, h ? TC_WHITE : TC_ORANGE);
887 y += step_height;
888 }
889 }
890 if (i == this->active_over && this->vscroll->IsVisible(i)) { // Highlight is after the last GRF entry.
891 GfxFillRect(r.left + WD_FRAMERECT_LEFT, y, r.right - WD_FRAMERECT_RIGHT, y + 2, PC_GREY);
892 }
893 break;
894 }
895
896 case WID_NS_AVAIL_LIST: {
897 GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, this->active_over == -2 ? PC_DARK_GREY : PC_BLACK);
898
899 uint step_height = this->GetWidget<NWidgetBase>(WID_NS_AVAIL_LIST)->resize_y;
900 int offset_y = (step_height - FONT_HEIGHT_NORMAL) / 2;
901 uint y = r.top + WD_FRAMERECT_TOP;
902 uint min_index = this->vscroll2->GetPosition();
903 uint max_index = min(min_index + this->vscroll2->GetCapacity(), this->avails.Length());
904
905 for (uint i = min_index; i < max_index; i++) {
906 const GRFConfig *c = this->avails[i];
907 bool h = (c == this->avail_sel);
908 const char *text = c->GetName();
909
910 if (h) GfxFillRect(r.left + 1, y, r.right - 1, y + step_height - 1, PC_DARK_BLUE);
911 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y + offset_y, text, h ? TC_WHITE : TC_SILVER);
912 y += step_height;
913 }
914 break;
915 }
916
917 case WID_NS_NEWGRF_INFO_TITLE:
918 /* Create the nice grayish rectangle at the details top. */
919 GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, PC_DARK_BLUE);
920 DrawString(r.left, r.right, (r.top + r.bottom - FONT_HEIGHT_NORMAL) / 2, STR_NEWGRF_SETTINGS_INFO_TITLE, TC_FROMSTRING, SA_HOR_CENTER);
921 break;
922
923 case WID_NS_NEWGRF_INFO: {
924 const GRFConfig *selected = this->active_sel;
925 if (selected == NULL) selected = this->avail_sel;
926 if (selected != NULL) {
927 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);
928 }
929 break;
930 }
931 }
932 }
933
934 virtual void OnClick(Point pt, int widget, int click_count)
935 {
936 if (widget >= WID_NS_NEWGRF_TEXTFILE && widget < WID_NS_NEWGRF_TEXTFILE + TFT_END) {
937 if (this->active_sel == NULL && this->avail_sel == NULL) return;
938
939 ShowNewGRFTextfileWindow((TextfileType)(widget - WID_NS_NEWGRF_TEXTFILE), this->active_sel != NULL ? this->active_sel : this->avail_sel);
940 return;
941 }
942
943 switch (widget) {
944 case WID_NS_PRESET_LIST: {
945 DropDownList *list = new DropDownList();
946
947 /* Add 'None' option for clearing list */
948 *list->Append() = new DropDownListStringItem(STR_NONE, -1, false);
949
950 for (uint i = 0; i < _grf_preset_list.Length(); i++) {
951 if (_grf_preset_list[i] != NULL) {
952 *list->Append() = new DropDownListPresetItem(i);
953 }
954 }
955
956 this->DeleteChildWindows(WC_QUERY_STRING); // Remove the parameter query window
957 ShowDropDownList(this, list, this->preset, WID_NS_PRESET_LIST);
958 break;
959 }
960
961 case WID_NS_OPEN_URL: {
962 const GRFConfig *c = (this->avail_sel == NULL) ? this->active_sel : this->avail_sel;
963
964 extern void OpenBrowser(const char *url);
965 OpenBrowser(c->GetURL());
966 break;
967 }
968
969 case WID_NS_PRESET_SAVE:
970 ShowSavePresetWindow((this->preset == -1) ? NULL : _grf_preset_list[this->preset]);
971 break;
972
973 case WID_NS_PRESET_DELETE:
974 if (this->preset == -1) return;
975
976 DeleteGRFPresetFromConfig(_grf_preset_list[this->preset]);
977 GetGRFPresetList(&_grf_preset_list);
978 this->preset = -1;
979 this->InvalidateData();
980 this->DeleteChildWindows(WC_QUERY_STRING); // Remove the parameter query window
981 break;
982
983 case WID_NS_MOVE_UP: { // Move GRF up
984 if (this->active_sel == NULL || !this->editable) break;
985
986 int pos = 0;
987 for (GRFConfig **pc = &this->actives; *pc != NULL; pc = &(*pc)->next, pos++) {
988 GRFConfig *c = *pc;
989 if (c->next == this->active_sel) {
990 c->next = this->active_sel->next;
991 this->active_sel->next = c;
992 *pc = this->active_sel;
993 break;
994 }
995 }
996 this->vscroll->ScrollTowards(pos);
997 this->preset = -1;
998 this->InvalidateData();
999 break;
1000 }
1001
1002 case WID_NS_MOVE_DOWN: { // Move GRF down
1003 if (this->active_sel == NULL || !this->editable) break;
1004
1005 int pos = 1; // Start at 1 as we swap the selected newgrf with the next one
1006 for (GRFConfig **pc = &this->actives; *pc != NULL; pc = &(*pc)->next, pos++) {
1007 GRFConfig *c = *pc;
1008 if (c == this->active_sel) {
1009 *pc = c->next;
1010 c->next = c->next->next;
1011 (*pc)->next = c;
1012 break;
1013 }
1014 }
1015 this->vscroll->ScrollTowards(pos);
1016 this->preset = -1;
1017 this->InvalidateData();
1018 break;
1019 }
1020
1021 case WID_NS_FILE_LIST: { // Select an active GRF.
1022 ResetObjectToPlace();
1023
1024 uint i = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_NS_FILE_LIST);
1025
1026 GRFConfig *c;
1027 for (c = this->actives; c != NULL && i > 0; c = c->next, i--) {}
1028
1029 if (this->active_sel != c) DeleteWindowByClass(WC_GRF_PARAMETERS);
1030 this->active_sel = c;
1031 this->avail_sel = NULL;
1032 this->avail_pos = -1;
1033
1034 this->InvalidateData();
1035 if (click_count == 1) {
1036 if (this->editable && this->active_sel != NULL) SetObjectToPlaceWnd(SPR_CURSOR_MOUSE, PAL_NONE, HT_DRAG, this);
1037 break;
1038 }
1039 /* FALL THROUGH, with double click. */
1040 }
1041
1042 case WID_NS_REMOVE: { // Remove GRF
1043 if (this->active_sel == NULL || !this->editable) break;
1044 DeleteWindowByClass(WC_GRF_PARAMETERS);
1045
1046 /* Choose the next GRF file to be the selected file. */
1047 GRFConfig *newsel = this->active_sel->next;
1048 for (GRFConfig **pc = &this->actives; *pc != NULL; pc = &(*pc)->next) {
1049 GRFConfig *c = *pc;
1050 /* If the new selection is empty (i.e. we're deleting the last item
1051 * in the list, pick the file just before the selected file */
1052 if (newsel == NULL && c->next == this->active_sel) newsel = c;
1053
1054 if (c == this->active_sel) {
1055 if (newsel == c) newsel = NULL;
1056
1057 *pc = c->next;
1058 delete c;
1059 break;
1060 }
1061 }
1062
1063 this->active_sel = newsel;
1064 this->preset = -1;
1065 this->avail_pos = -1;
1066 this->avail_sel = NULL;
1067 this->avails.ForceRebuild();
1068 this->InvalidateData(GOID_NEWGRF_LIST_EDITED);
1069 break;
1070 }
1071
1072 case WID_NS_UPGRADE: { // Upgrade GRF.
1073 if (!this->editable || this->actives == NULL) break;
1074 UpgradeCurrent();
1075 this->InvalidateData(GOID_NEWGRF_LIST_EDITED);
1076 break;
1077 }
1078
1079 case WID_NS_AVAIL_LIST: { // Select a non-active GRF.
1080 ResetObjectToPlace();
1081
1082 uint i = this->vscroll2->GetScrolledRowFromWidget(pt.y, this, WID_NS_AVAIL_LIST);
1083 this->active_sel = NULL;
1084 DeleteWindowByClass(WC_GRF_PARAMETERS);
1085 if (i < this->avails.Length()) {
1086 this->avail_sel = this->avails[i];
1087 this->avail_pos = i;
1088 }
1089 this->InvalidateData();
1090 if (click_count == 1) {
1091 if (this->editable && this->avail_sel != NULL && !HasBit(this->avail_sel->flags, GCF_INVALID)) SetObjectToPlaceWnd(SPR_CURSOR_MOUSE, PAL_NONE, HT_DRAG, this);
1092 break;
1093 }
1094 /* FALL THROUGH, with double click. */
1095 }
1096
1097 case WID_NS_ADD:
1098 if (this->avail_sel == NULL || !this->editable || HasBit(this->avail_sel->flags, GCF_INVALID)) break;
1099
1100 this->AddGRFToActive();
1101 break;
1102
1103 case WID_NS_APPLY_CHANGES: // Apply changes made to GRF list
1104 if (!this->editable) break;
1105 if (this->execute) {
1106 ShowQuery(
1107 STR_NEWGRF_POPUP_CAUTION_CAPTION,
1108 STR_NEWGRF_CONFIRMATION_TEXT,
1109 this,
1110 NewGRFConfirmationCallback
1111 );
1112 } else {
1113 CopyGRFConfigList(this->orig_list, this->actives, true);
1114 ResetGRFConfig(false);
1115 ReloadNewGRFData();
1116 }
1117 this->DeleteChildWindows(WC_QUERY_STRING); // Remove the parameter query window
1118 break;
1119
1120 case WID_NS_VIEW_PARAMETERS:
1121 case WID_NS_SET_PARAMETERS: { // Edit parameters
1122 if (this->active_sel == NULL || !this->show_params || this->active_sel->num_valid_params == 0) break;
1123
1124 OpenGRFParameterWindow(this->active_sel, this->editable);
1125 break;
1126 }
1127
1128 case WID_NS_TOGGLE_PALETTE:
1129 if (this->active_sel != NULL && this->editable) {
1130 this->active_sel->palette ^= GRFP_USE_MASK;
1131 this->SetDirty();
1132 }
1133 break;
1134
1135 case WID_NS_CONTENT_DOWNLOAD:
1136 case WID_NS_CONTENT_DOWNLOAD2:
1137 if (!_network_available) {
1138 ShowErrorMessage(STR_NETWORK_ERROR_NOTAVAILABLE, INVALID_STRING_ID, WL_ERROR);
1139 } else {
1140 #if defined(ENABLE_NETWORK)
1141 this->DeleteChildWindows(WC_QUERY_STRING); // Remove the parameter query window
1142
1143 ShowMissingContentWindow(this->actives);
1144 #endif
1145 }
1146 break;
1147
1148 case WID_NS_RESCAN_FILES:
1149 case WID_NS_RESCAN_FILES2:
1150 ScanNewGRFFiles(this);
1151 break;
1152 }
1153 }
1154
1155 virtual void OnNewGRFsScanned()
1156 {
1157 this->avail_sel = NULL;
1158 this->avail_pos = -1;
1159 this->avails.ForceRebuild();
1160 this->DeleteChildWindows(WC_QUERY_STRING); // Remove the parameter query window
1161 this->DeleteChildWindows(WC_TEXTFILE); // Remove the view textfile window
1162 }
1163
1164 virtual void OnDropdownSelect(int widget, int index)
1165 {
1166 if (!this->editable) return;
1167
1168 ClearGRFConfigList(&this->actives);
1169 this->preset = index;
1170
1171 if (index != -1) {
1172 this->actives = LoadGRFPresetFromConfig(_grf_preset_list[index]);
1173 }
1174 this->avails.ForceRebuild();
1175
1176 ResetObjectToPlace();
1177 DeleteWindowByClass(WC_GRF_PARAMETERS);
1178 this->active_sel = NULL;
1179 this->InvalidateData(GOID_NEWGRF_PRESET_LOADED);
1180 }
1181
1182 virtual void OnQueryTextFinished(char *str)
1183 {
1184 if (str == NULL) return;
1185
1186 SaveGRFPresetToConfig(str, this->actives);
1187 GetGRFPresetList(&_grf_preset_list);
1188
1189 /* Switch to this preset */
1190 for (uint i = 0; i < _grf_preset_list.Length(); i++) {
1191 if (_grf_preset_list[i] != NULL && strcmp(_grf_preset_list[i], str) == 0) {
1192 this->preset = i;
1193 break;
1194 }
1195 }
1196
1197 this->InvalidateData();
1198 }
1199
1205 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
1206 {
1207 if (!gui_scope) return;
1208 switch (data) {
1209 default:
1210 /* Nothing important to do */
1211 break;
1212
1213 case GOID_NEWGRF_RESCANNED:
1214 /* Search the list for items that are now found and mark them as such. */
1215 for (GRFConfig **l = &this->actives; *l != NULL; l = &(*l)->next) {
1216 GRFConfig *c = *l;
1217 bool compatible = HasBit(c->flags, GCF_COMPATIBLE);
1218 if (c->status != GCS_NOT_FOUND && !compatible) continue;
1219
1220 const GRFConfig *f = FindGRFConfig(c->ident.grfid, FGCM_EXACT, compatible ? c->original_md5sum : c->ident.md5sum);
1221 if (f == NULL || HasBit(f->flags, GCF_INVALID)) continue;
1222
1223 *l = new GRFConfig(*f);
1224 (*l)->next = c->next;
1225
1226 if (active_sel == c) active_sel = *l;
1227
1228 delete c;
1229 }
1230
1231 this->avails.ForceRebuild();
1232 /* FALL THROUGH */
1233 case GOID_NEWGRF_LIST_EDITED:
1234 this->preset = -1;
1235 /* FALL THROUGH */
1236 case GOID_NEWGRF_PRESET_LOADED: {
1237 /* Update scrollbars */
1238 int i = 0;
1239 for (const GRFConfig *c = this->actives; c != NULL; c = c->next, i++) {}
1240
1241 this->vscroll->SetCount(i + 1); // Reserve empty space for drag and drop handling.
1242
1243 if (this->avail_pos >= 0) this->vscroll2->ScrollTowards(this->avail_pos);
1244 break;
1245 }
1246 }
1247
1248 this->BuildAvailables();
1249
1250 this->SetWidgetsDisabledState(!this->editable,
1251 WID_NS_PRESET_LIST,
1252 WID_NS_APPLY_CHANGES,
1253 WID_NS_TOGGLE_PALETTE,
1254 WIDGET_LIST_END
1255 );
1256 this->SetWidgetDisabledState(WID_NS_ADD, !this->editable || this->avail_sel == NULL || HasBit(this->avail_sel->flags, GCF_INVALID));
1257 this->SetWidgetDisabledState(WID_NS_UPGRADE, !this->editable || this->actives == NULL || !this->CanUpgradeCurrent());
1258
1259 bool disable_all = this->active_sel == NULL || !this->editable;
1260 this->SetWidgetsDisabledState(disable_all,
1261 WID_NS_REMOVE,
1262 WID_NS_MOVE_UP,
1263 WID_NS_MOVE_DOWN,
1264 WIDGET_LIST_END
1265 );
1266
1267 const GRFConfig *c = (this->avail_sel == NULL) ? this->active_sel : this->avail_sel;
1268 for (TextfileType tft = TFT_BEGIN; tft < TFT_END; tft++) {
1269 this->SetWidgetDisabledState(WID_NS_NEWGRF_TEXTFILE + tft, c == NULL || c->GetTextfile(tft) == NULL);
1270 }
1271 this->SetWidgetDisabledState(WID_NS_OPEN_URL, c == NULL || StrEmpty(c->GetURL()));
1272
1273 this->SetWidgetDisabledState(WID_NS_SET_PARAMETERS, !this->show_params || this->active_sel == NULL || this->active_sel->num_valid_params == 0);
1274 this->SetWidgetDisabledState(WID_NS_VIEW_PARAMETERS, !this->show_params || this->active_sel == NULL || this->active_sel->num_valid_params == 0);
1275 this->SetWidgetDisabledState(WID_NS_TOGGLE_PALETTE, disable_all ||
1276 (!(_settings_client.gui.newgrf_developer_tools || _settings_client.gui.scenario_developer) && ((c->palette & GRFP_GRF_MASK) != GRFP_GRF_UNSET)));
1277
1278 if (!disable_all) {
1279 /* All widgets are now enabled, so disable widgets we can't use */
1280 if (this->active_sel == this->actives) this->DisableWidget(WID_NS_MOVE_UP);
1281 if (this->active_sel->next == NULL) this->DisableWidget(WID_NS_MOVE_DOWN);
1282 if (this->active_sel->IsOpenTTDBaseGRF()) this->DisableWidget(WID_NS_REMOVE);
1283 }
1284
1285 this->SetWidgetDisabledState(WID_NS_PRESET_DELETE, this->preset == -1);
1286
1287 bool has_missing = false;
1288 bool has_compatible = false;
1289 for (const GRFConfig *c = this->actives; !has_missing && c != NULL; c = c->next) {
1290 has_missing |= c->status == GCS_NOT_FOUND;
1291 has_compatible |= HasBit(c->flags, GCF_COMPATIBLE);
1292 }
1293 uint32 widget_data;
1294 StringID tool_tip;
1295 if (has_missing || has_compatible) {
1296 widget_data = STR_NEWGRF_SETTINGS_FIND_MISSING_CONTENT_BUTTON;
1297 tool_tip = STR_NEWGRF_SETTINGS_FIND_MISSING_CONTENT_TOOLTIP;
1298 } else {
1299 widget_data = STR_INTRO_ONLINE_CONTENT;
1300 tool_tip = STR_INTRO_TOOLTIP_ONLINE_CONTENT;
1301 }
1302 this->GetWidget<NWidgetCore>(WID_NS_CONTENT_DOWNLOAD)->widget_data = widget_data;
1303 this->GetWidget<NWidgetCore>(WID_NS_CONTENT_DOWNLOAD)->tool_tip = tool_tip;
1304 this->GetWidget<NWidgetCore>(WID_NS_CONTENT_DOWNLOAD2)->widget_data = widget_data;
1305 this->GetWidget<NWidgetCore>(WID_NS_CONTENT_DOWNLOAD2)->tool_tip = tool_tip;
1306
1307 this->SetWidgetDisabledState(WID_NS_PRESET_SAVE, has_missing);
1308 }
1309
1310 virtual EventState OnKeyPress(WChar key, uint16 keycode)
1311 {
1312 if (!this->editable) return ES_NOT_HANDLED;
1313
1314 switch (keycode) {
1315 case WKC_UP:
1316 /* scroll up by one */
1317 if (this->avail_pos > 0) this->avail_pos--;
1318 break;
1319
1320 case WKC_DOWN:
1321 /* scroll down by one */
1322 if (this->avail_pos < (int)this->avails.Length() - 1) this->avail_pos++;
1323 break;
1324
1325 case WKC_PAGEUP:
1326 /* scroll up a page */
1327 this->avail_pos = (this->avail_pos < this->vscroll2->GetCapacity()) ? 0 : this->avail_pos - this->vscroll2->GetCapacity();
1328 break;
1329
1330 case WKC_PAGEDOWN:
1331 /* scroll down a page */
1332 this->avail_pos = min(this->avail_pos + this->vscroll2->GetCapacity(), (int)this->avails.Length() - 1);
1333 break;
1334
1335 case WKC_HOME:
1336 /* jump to beginning */
1337 this->avail_pos = 0;
1338 break;
1339
1340 case WKC_END:
1341 /* jump to end */
1342 this->avail_pos = this->avails.Length() - 1;
1343 break;
1344
1345 default:
1346 return ES_NOT_HANDLED;
1347 }
1348
1349 if (this->avails.Length() == 0) this->avail_pos = -1;
1350 if (this->avail_pos >= 0) {
1351 this->avail_sel = this->avails[this->avail_pos];
1352 this->vscroll2->ScrollTowards(this->avail_pos);
1353 this->InvalidateData(0);
1354 }
1355
1356 return ES_HANDLED;
1357 }
1358
1359 virtual void OnEditboxChanged(int wid)
1360 {
1361 if (!this->editable) return;
1362
1363 string_filter.SetFilterTerm(this->filter_editbox.text.buf);
1364 this->avails.SetFilterState(!string_filter.IsEmpty());
1365 this->avails.ForceRebuild();
1366 this->InvalidateData(0);
1367 }
1368
1369 virtual void OnDragDrop(Point pt, int widget)
1370 {
1371 if (!this->editable) return;
1372
1373 if (widget == WID_NS_FILE_LIST) {
1374 if (this->active_sel != NULL) {
1375 /* Get pointer to the selected file in the active list. */
1376 int from_pos = 0;
1377 GRFConfig **from_prev;
1378 for (from_prev = &this->actives; *from_prev != this->active_sel; from_prev = &(*from_prev)->next, from_pos++) {}
1379
1380 /* Gets the drag-and-drop destination offset. Ignore the last dummy line. */
1381 int to_pos = min(this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_NS_FILE_LIST), this->vscroll->GetCount() - 2);
1382 if (to_pos != from_pos) { // Don't move NewGRF file over itself.
1383 /* Get pointer to destination position. */
1384 GRFConfig **to_prev = &this->actives;
1385 for (int i = from_pos < to_pos ? -1 : 0; *to_prev != NULL && i < to_pos; to_prev = &(*to_prev)->next, i++) {}
1386
1387 /* Detach NewGRF file from its original position. */
1388 *from_prev = this->active_sel->next;
1389
1390 /* Attach NewGRF file to its new position. */
1391 this->active_sel->next = *to_prev;
1392 *to_prev = this->active_sel;
1393
1394 this->vscroll->ScrollTowards(to_pos);
1395 this->preset = -1;
1396 this->InvalidateData();
1397 }
1398 } else if (this->avail_sel != NULL) {
1399 int to_pos = min(this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_NS_FILE_LIST), this->vscroll->GetCount() - 1);
1400 this->AddGRFToActive(to_pos);
1401 }
1402 } else if (widget == WID_NS_AVAIL_LIST && this->active_sel != NULL) {
1403 /* Remove active NewGRF file by dragging it over available list. */
1404 Point dummy = {-1, -1};
1405 this->OnClick(dummy, WID_NS_REMOVE, 1);
1406 }
1407
1408 ResetObjectToPlace();
1409
1410 if (this->active_over != -1) {
1411 /* End of drag-and-drop, hide dragged destination highlight. */
1412 this->SetWidgetDirty(this->active_over == -2 ? WID_NS_AVAIL_LIST : WID_NS_FILE_LIST);
1413 this->active_over = -1;
1414 }
1415 }
1416
1417 virtual void OnMouseDrag(Point pt, int widget)
1418 {
1419 if (!this->editable) return;
1420
1421 if (widget == WID_NS_FILE_LIST && (this->active_sel != NULL || this->avail_sel != NULL)) {
1422 /* An NewGRF file is dragged over the active list. */
1423 int to_pos = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_NS_FILE_LIST);
1424 /* Skip the last dummy line if the source is from the active list. */
1425 to_pos = min(to_pos, this->vscroll->GetCount() - (this->active_sel != NULL ? 2 : 1));
1426
1427 if (to_pos != this->active_over) {
1428 this->active_over = to_pos;
1429 this->SetWidgetDirty(WID_NS_FILE_LIST);
1430 }
1431 } else if (widget == WID_NS_AVAIL_LIST && this->active_sel != NULL) {
1432 this->active_over = -2;
1433 this->SetWidgetDirty(WID_NS_AVAIL_LIST);
1434 } else if (this->active_over != -1) {
1435 this->SetWidgetDirty(this->active_over == -2 ? WID_NS_AVAIL_LIST : WID_NS_FILE_LIST);
1436 this->active_over = -1;
1437 }
1438 }
1439
1440 private:
1442 static int CDECL NameSorter(const GRFConfig * const *a, const GRFConfig * const *b)
1443 {
1444 int i = strnatcmp((*a)->GetName(), (*b)->GetName(), true); // Sort by name (natural sorting).
1445 if (i != 0) return i;
1446
1447 i = (*a)->version - (*b)->version;
1448 if (i != 0) return i;
1449
1450 return memcmp((*a)->ident.md5sum, (*b)->ident.md5sum, lengthof((*b)->ident.md5sum));
1451 }
1452
1454 static bool CDECL TagNameFilter(const GRFConfig * const *a, StringFilter &filter)
1455 {
1456 filter.ResetState();
1457 filter.AddLine((*a)->GetName());
1458 filter.AddLine((*a)->filename);
1459 filter.AddLine((*a)->GetDescription());
1460 return filter.GetState();;
1461 }
1462
1463 void BuildAvailables()
1464 {
1465 if (!this->avails.NeedRebuild()) return;
1466
1467 this->avails.Clear();
1468
1469 for (const GRFConfig *c = _all_grfs; c != NULL; c = c->next) {
1470 bool found = false;
1471 for (const GRFConfig *grf = this->actives; grf != NULL && !found; grf = grf->next) found = grf->ident.HasGrfIdentifier(c->ident.grfid, c->ident.md5sum);
1472 if (found) continue;
1473
1474 if (_settings_client.gui.newgrf_show_old_versions) {
1475 *this->avails.Append() = c;
1476 } else {
1477 const GRFConfig *best = FindGRFConfig(c->ident.grfid, HasBit(c->flags, GCF_INVALID) ? FGCM_NEWEST : FGCM_NEWEST_VALID);
1478 /*
1479 * If the best version is 0, then all NewGRF with this GRF ID
1480 * have version 0, so for backward compatibility reasons we
1481 * want to show them all.
1482 * If we are the best version, then we definitely want to
1483 * show that NewGRF!.
1484 */
1485 if (best->version == 0 || best->ident.HasGrfIdentifier(c->ident.grfid, c->ident.md5sum)) {
1486 *this->avails.Append() = c;
1487 }
1488 }
1489 }
1490
1491 this->avails.Filter(this->string_filter);
1492 this->avails.Compact();
1493 this->avails.RebuildDone();
1494 this->avails.Sort();
1495
1496 if (this->avail_sel != NULL) {
1497 this->avail_pos = this->avails.FindIndex(this->avail_sel);
1498 if (this->avail_pos < 0) this->avail_sel = NULL;
1499 }
1500
1501 this->vscroll2->SetCount(this->avails.Length()); // Update the scrollbar
1502 }
1503
1509 bool AddGRFToActive(int ins_pos = -1)
1510 {
1511 if (this->avail_sel == NULL || !this->editable || HasBit(this->avail_sel->flags, GCF_INVALID)) return false;
1512
1513 int count = 0;
1514 GRFConfig **entry = NULL;
1515 GRFConfig **list;
1516 /* Find last entry in the list, checking for duplicate grfid on the way */
1517 for (list = &this->actives; *list != NULL; list = &(*list)->next, ins_pos--) {
1518 if (ins_pos == 0) entry = list; // Insert position? Save.
1519 if ((*list)->ident.grfid == this->avail_sel->ident.grfid) {
1520 ShowErrorMessage(STR_NEWGRF_DUPLICATE_GRFID, INVALID_STRING_ID, WL_INFO);
1521 return false;
1522 }
1523 count++;
1524 }
1525 if (entry == NULL) entry = list;
1526 if (count >= MAX_NEWGRFS) {
1527 ShowErrorMessage(STR_NEWGRF_TOO_MANY_NEWGRFS, INVALID_STRING_ID, WL_INFO);
1528 return false;
1529 }
1530
1531 GRFConfig *c = new GRFConfig(*this->avail_sel); // Copy GRF details from scanned list.
1532 c->SetParameterDefaults();
1533
1534 /* Insert GRF config to configuration list. */
1535 c->next = *entry;
1536 *entry = c;
1537
1538 /* Select next (or previous, if last one) item in the list. */
1539 int new_pos = this->avail_pos + 1;
1540 if (new_pos >= (int)this->avails.Length()) new_pos = this->avail_pos - 1;
1541 this->avail_pos = new_pos;
1542 if (new_pos >= 0) this->avail_sel = this->avails[new_pos];
1543
1544 this->avails.ForceRebuild();
1545 this->InvalidateData(GOID_NEWGRF_LIST_EDITED);
1546 return true;
1547 }
1548 };
1549
1550 #if defined(ENABLE_NETWORK)
1551
1555 void ShowMissingContentWindow(const GRFConfig *list)
1556 {
1557 /* Only show the things in the current list, or everything when nothing's selected */
1558 ContentVector cv;
1559 for (const GRFConfig *c = list; c != NULL; c = c->next) {
1560 if (c->status != GCS_NOT_FOUND && !HasBit(c->flags, GCF_COMPATIBLE)) continue;
1561
1562 ContentInfo *ci = new ContentInfo();
1563 ci->type = CONTENT_TYPE_NEWGRF;
1564 ci->state = ContentInfo::DOES_NOT_EXIST;
1565 strecpy(ci->name, c->GetName(), lastof(ci->name));
1566 ci->unique_id = BSWAP32(c->ident.grfid);
1567 memcpy(ci->md5sum, HasBit(c->flags, GCF_COMPATIBLE) ? c->original_md5sum : c->ident.md5sum, sizeof(ci->md5sum));
1568 *cv.Append() = ci;
1569 }
1570 ShowNetworkContentListWindow(cv.Length() == 0 ? NULL : &cv, CONTENT_TYPE_NEWGRF);
1571 }
1572 #endif
1573
1574 Listing NewGRFWindow::last_sorting = {false, 0};
1575 Filtering NewGRFWindow::last_filtering = {false, 0};
1576
1577 NewGRFWindow::GUIGRFConfigList::SortFunction * const NewGRFWindow::sorter_funcs[] = {
1578 &NameSorter,
1579 };
1580
1581 NewGRFWindow::GUIGRFConfigList::FilterFunction * const NewGRFWindow::filter_funcs[] = {
1582 &TagNameFilter,
1583 };
1584
1591 class NWidgetNewGRFDisplay : public NWidgetContainer {
1592 public:
1593 static const uint INTER_LIST_SPACING;
1594 static const uint INTER_COLUMN_SPACING;
1595 static const uint MAX_EXTRA_INFO_WIDTH;
1596 static const uint MIN_EXTRA_FOR_3_COLUMNS;
1597
1598 NWidgetBase *avs;
1599 NWidgetBase *acs;
1600 NWidgetBase *inf;
1601 bool editable;
1602
1603 NWidgetNewGRFDisplay(NWidgetBase *avs, NWidgetBase *acs, NWidgetBase *inf) : NWidgetContainer(NWID_HORIZONTAL)
1604 {
1605 this->avs = avs;
1606 this->acs = acs;
1607 this->inf = inf;
1608
1609 this->Add(this->avs);
1610 this->Add(this->acs);
1611 this->Add(this->inf);
1612
1613 this->editable = true; // Temporary setting, 'real' value is set in SetupSmallestSize().
1614 }
1615
1616 virtual void SetupSmallestSize(Window *w, bool init_array)
1617 {
1618 /* Copy state flag from the window. */
1619 assert(dynamic_cast<NewGRFWindow *>(w) != NULL);
1620 NewGRFWindow *ngw = (NewGRFWindow *)w;
1621 this->editable = ngw->editable;
1622
1623 this->avs->SetupSmallestSize(w, init_array);
1624 this->acs->SetupSmallestSize(w, init_array);
1625 this->inf->SetupSmallestSize(w, init_array);
1626
1627 uint min_avs_width = this->avs->smallest_x + this->avs->padding_left + this->avs->padding_right;
1628 uint min_acs_width = this->acs->smallest_x + this->acs->padding_left + this->acs->padding_right;
1629 uint min_inf_width = this->inf->smallest_x + this->inf->padding_left + this->inf->padding_right;
1630
1631 uint min_avs_height = this->avs->smallest_y + this->avs->padding_top + this->avs->padding_bottom;
1632 uint min_acs_height = this->acs->smallest_y + this->acs->padding_top + this->acs->padding_bottom;
1633 uint min_inf_height = this->inf->smallest_y + this->inf->padding_top + this->inf->padding_bottom;
1634
1635 /* Smallest window is in two column mode. */
1636 this->smallest_x = max(min_avs_width, min_acs_width) + INTER_COLUMN_SPACING + min_inf_width;
1637 this->smallest_y = max(min_inf_height, min_acs_height + INTER_LIST_SPACING + min_avs_height);
1638
1639 /* Filling. */
1640 this->fill_x = LeastCommonMultiple(this->avs->fill_x, this->acs->fill_x);
1641 if (this->inf->fill_x > 0 && (this->fill_x == 0 || this->fill_x > this->inf->fill_x)) this->fill_x = this->inf->fill_x;
1642
1643 this->fill_y = this->avs->fill_y;
1644 if (this->acs->fill_y > 0 && (this->fill_y == 0 || this->fill_y > this->acs->fill_y)) this->fill_y = this->acs->fill_y;
1645 this->fill_y = LeastCommonMultiple(this->fill_y, this->inf->fill_y);
1646
1647 /* Resizing. */
1648 this->resize_x = LeastCommonMultiple(this->avs->resize_x, this->acs->resize_x);
1649 if (this->inf->resize_x > 0 && (this->resize_x == 0 || this->resize_x > this->inf->resize_x)) this->resize_x = this->inf->resize_x;
1650
1651 this->resize_y = this->avs->resize_y;
1652 if (this->acs->resize_y > 0 && (this->resize_y == 0 || this->resize_y > this->acs->resize_y)) this->resize_y = this->acs->resize_y;
1653 this->resize_y = LeastCommonMultiple(this->resize_y, this->inf->resize_y);
1654
1655 /* Make sure the height suits the 3 column (resp. not-editable) format; the 2 column format can easily fill space between the lists */
1656 this->smallest_y = ComputeMaxSize(min_acs_height, this->smallest_y + this->resize_y - 1, this->resize_y);
1657 }
1658
1659 virtual void AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)
1660 {
1661 this->StoreSizePosition(sizing, x, y, given_width, given_height);
1662
1663 uint min_avs_width = this->avs->smallest_x + this->avs->padding_left + this->avs->padding_right;
1664 uint min_acs_width = this->acs->smallest_x + this->acs->padding_left + this->acs->padding_right;
1665 uint min_inf_width = this->inf->smallest_x + this->inf->padding_left + this->inf->padding_right;
1666
1667 uint min_list_width = max(min_avs_width, min_acs_width); // Smallest width of the lists such that they have equal width (incl padding).
1668 uint avs_extra_width = min_list_width - min_avs_width; // Additional width needed for avs to reach min_list_width.
1669 uint acs_extra_width = min_list_width - min_acs_width; // Additional width needed for acs to reach min_list_width.
1670
1671 /* Use 2 or 3 columns? */
1672 uint min_three_columns = min_avs_width + min_acs_width + min_inf_width + 2 * INTER_COLUMN_SPACING;
1673 uint min_two_columns = min_list_width + min_inf_width + INTER_COLUMN_SPACING;
1674 bool use_three_columns = this->editable && (min_three_columns + MIN_EXTRA_FOR_3_COLUMNS <= given_width);
1675
1676 /* Info panel is a separate column in both modes. Compute its width first. */
1677 uint extra_width, inf_width;
1678 if (use_three_columns) {
1679 extra_width = given_width - min_three_columns;
1680 inf_width = min(MAX_EXTRA_INFO_WIDTH, extra_width / 2);
1681 } else {
1682 extra_width = given_width - min_two_columns;
1683 inf_width = min(MAX_EXTRA_INFO_WIDTH, extra_width / 2);
1684 }
1685 inf_width = ComputeMaxSize(this->inf->smallest_x, this->inf->smallest_x + inf_width, this->inf->GetHorizontalStepSize(sizing));
1686 extra_width -= inf_width - this->inf->smallest_x;
1687
1688 uint inf_height = ComputeMaxSize(this->inf->smallest_y, given_height, this->inf->GetVerticalStepSize(sizing));
1689
1690 if (use_three_columns) {
1691 /* Three column display, first make both lists equally wide, then divide whatever is left between both lists.
1692 * Only keep track of what avs gets, all other space goes to acs. */
1693 uint avs_width = min(avs_extra_width, extra_width);
1694 extra_width -= avs_width;
1695 extra_width -= min(acs_extra_width, extra_width);
1696 avs_width += extra_width / 2;
1697
1698 avs_width = ComputeMaxSize(this->avs->smallest_x, this->avs->smallest_x + avs_width, this->avs->GetHorizontalStepSize(sizing));
1699
1700 uint acs_width = given_width - // Remaining space, including horizontal padding.
1701 inf_width - this->inf->padding_left - this->inf->padding_right -
1702 avs_width - this->avs->padding_left - this->avs->padding_right - 2 * INTER_COLUMN_SPACING;
1703 acs_width = ComputeMaxSize(min_acs_width, acs_width, this->acs->GetHorizontalStepSize(sizing)) -
1704 this->acs->padding_left - this->acs->padding_right;
1705
1706 /* Never use fill_y on these; the minimal size is chosen, so that the 3 column view looks nice */
1707 uint avs_height = ComputeMaxSize(this->avs->smallest_y, given_height, this->avs->resize_y);
1708 uint acs_height = ComputeMaxSize(this->acs->smallest_y, given_height, this->acs->resize_y);
1709
1710 /* Assign size and position to the children. */
1711 if (rtl) {
1712 x += this->inf->padding_left;
1713 this->inf->AssignSizePosition(sizing, x, y + this->inf->padding_top, inf_width, inf_height, rtl);
1714 x += inf_width + this->inf->padding_right + INTER_COLUMN_SPACING;
1715 } else {
1716 x += this->avs->padding_left;
1717 this->avs->AssignSizePosition(sizing, x, y + this->avs->padding_top, avs_width, avs_height, rtl);
1718 x += avs_width + this->avs->padding_right + INTER_COLUMN_SPACING;
1719 }
1720
1721 x += this->acs->padding_left;
1722 this->acs->AssignSizePosition(sizing, x, y + this->acs->padding_top, acs_width, acs_height, rtl);
1723 x += acs_width + this->acs->padding_right + INTER_COLUMN_SPACING;
1724
1725 if (rtl) {
1726 x += this->avs->padding_left;
1727 this->avs->AssignSizePosition(sizing, x, y + this->avs->padding_top, avs_width, avs_height, rtl);
1728 } else {
1729 x += this->inf->padding_left;
1730 this->inf->AssignSizePosition(sizing, x, y + this->inf->padding_top, inf_width, inf_height, rtl);
1731 }
1732 } else {
1733 /* Two columns, all space in extra_width goes to both lists. Since the lists are underneath each other,
1734 * the column is min_list_width wide at least. */
1735 uint avs_width = ComputeMaxSize(this->avs->smallest_x, this->avs->smallest_x + avs_extra_width + extra_width,
1736 this->avs->GetHorizontalStepSize(sizing));
1737 uint acs_width = ComputeMaxSize(this->acs->smallest_x, this->acs->smallest_x + acs_extra_width + extra_width,
1738 this->acs->GetHorizontalStepSize(sizing));
1739
1740 uint min_avs_height = (!this->editable) ? 0 : this->avs->smallest_y + this->avs->padding_top + this->avs->padding_bottom + INTER_LIST_SPACING;
1741 uint min_acs_height = this->acs->smallest_y + this->acs->padding_top + this->acs->padding_bottom;
1742 uint extra_height = given_height - min_acs_height - min_avs_height;
1743
1744 /* Never use fill_y on these; instead use the INTER_LIST_SPACING as filler */
1745 uint avs_height = ComputeMaxSize(this->avs->smallest_y, this->avs->smallest_y + extra_height / 2, this->avs->resize_y);
1746 if (this->editable) extra_height -= avs_height - this->avs->smallest_y;
1747 uint acs_height = ComputeMaxSize(this->acs->smallest_y, this->acs->smallest_y + extra_height, this->acs->resize_y);
1748
1749 /* Assign size and position to the children. */
1750 if (rtl) {
1751 x += this->inf->padding_left;
1752 this->inf->AssignSizePosition(sizing, x, y + this->inf->padding_top, inf_width, inf_height, rtl);
1753 x += inf_width + this->inf->padding_right + INTER_COLUMN_SPACING;
1754
1755 this->acs->AssignSizePosition(sizing, x + this->acs->padding_left, y + this->acs->padding_top, acs_width, acs_height, rtl);
1756 if (this->editable) {
1757 this->avs->AssignSizePosition(sizing, x + this->avs->padding_left, y + given_height - avs_height - this->avs->padding_bottom, avs_width, avs_height, rtl);
1758 } else {
1759 this->avs->AssignSizePosition(sizing, 0, 0, this->avs->smallest_x, this->avs->smallest_y, rtl);
1760 }
1761 } else {
1762 this->acs->AssignSizePosition(sizing, x + this->acs->padding_left, y + this->acs->padding_top, acs_width, acs_height, rtl);
1763 if (this->editable) {
1764 this->avs->AssignSizePosition(sizing, x + this->avs->padding_left, y + given_height - avs_height - this->avs->padding_bottom, avs_width, avs_height, rtl);
1765 } else {
1766 this->avs->AssignSizePosition(sizing, 0, 0, this->avs->smallest_x, this->avs->smallest_y, rtl);
1767 }
1768 uint dx = this->acs->current_x + this->acs->padding_left + this->acs->padding_right;
1769 if (this->editable) {
1770 dx = max(dx, this->avs->current_x + this->avs->padding_left + this->avs->padding_right);
1771 }
1772 x += dx + INTER_COLUMN_SPACING + this->inf->padding_left;
1773 this->inf->AssignSizePosition(sizing, x, y + this->inf->padding_top, inf_width, inf_height, rtl);
1774 }
1775 }
1776 }
1777
1778 virtual NWidgetCore *GetWidgetFromPos(int x, int y)
1779 {
1780 if (!IsInsideBS(x, this->pos_x, this->current_x) || !IsInsideBS(y, this->pos_y, this->current_y)) return NULL;
1781
1782 NWidgetCore *nw = (this->editable) ? this->avs->GetWidgetFromPos(x, y) : NULL;
1783 if (nw == NULL) nw = this->acs->GetWidgetFromPos(x, y);
1784 if (nw == NULL) nw = this->inf->GetWidgetFromPos(x, y);
1785 return nw;
1786 }
1787
1788 virtual void Draw(const Window *w)
1789 {
1790 if (this->editable) this->avs->Draw(w);
1791 this->acs->Draw(w);
1792 this->inf->Draw(w);
1793 }
1794 };
1795
1796 const uint NWidgetNewGRFDisplay::INTER_LIST_SPACING = WD_RESIZEBOX_WIDTH + 1;
1797 const uint NWidgetNewGRFDisplay::INTER_COLUMN_SPACING = WD_RESIZEBOX_WIDTH;
1798 const uint NWidgetNewGRFDisplay::MAX_EXTRA_INFO_WIDTH = 150;
1799 const uint NWidgetNewGRFDisplay::MIN_EXTRA_FOR_3_COLUMNS = 50;
1800
1801 static const NWidgetPart _nested_newgrf_actives_widgets[] = {
1802 /* Left side, presets. */
1803 NWidget(NWID_HORIZONTAL),
1804 NWidget(WWT_TEXT, COLOUR_MAUVE), SetDataTip(STR_NEWGRF_SETTINGS_SELECT_PRESET, STR_NULL),
1805 SetPadding(0, WD_FRAMETEXT_RIGHT, 0, 0),
1806 NWidget(WWT_DROPDOWN, COLOUR_YELLOW, WID_NS_PRESET_LIST), SetFill(1, 0), SetResize(1, 0),
1807 SetDataTip(STR_JUST_STRING, STR_NEWGRF_SETTINGS_PRESET_LIST_TOOLTIP),
1808 EndContainer(),
1809 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
1810 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_PRESET_SAVE), SetFill(1, 0), SetResize(1, 0),
1811 SetDataTip(STR_NEWGRF_SETTINGS_PRESET_SAVE, STR_NEWGRF_SETTINGS_PRESET_SAVE_TOOLTIP),
1812 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_PRESET_DELETE), SetFill(1, 0), SetResize(1, 0),
1813 SetDataTip(STR_NEWGRF_SETTINGS_PRESET_DELETE, STR_NEWGRF_SETTINGS_PRESET_DELETE_TOOLTIP),
1814 EndContainer(),
1815
1816 NWidget(NWID_SPACER), SetMinimalSize(0, WD_RESIZEBOX_WIDTH), SetResize(1, 0), SetFill(1, 0),
1817 NWidget(WWT_PANEL, COLOUR_MAUVE),
1818 NWidget(WWT_LABEL, COLOUR_MAUVE), SetDataTip(STR_NEWGRF_SETTINGS_ACTIVE_LIST, STR_NULL),
1819 SetFill(1, 0), SetResize(1, 0), SetPadding(3, WD_FRAMETEXT_RIGHT, 0, WD_FRAMETEXT_LEFT),
1820 /* Left side, active grfs. */
1821 NWidget(NWID_HORIZONTAL), SetPadding(0, 2, 0, 2),
1822 NWidget(WWT_PANEL, COLOUR_MAUVE),
1823 NWidget(WWT_INSET, COLOUR_MAUVE, WID_NS_FILE_LIST), SetMinimalSize(100, 1), SetPadding(2, 2, 2, 2),
1824 SetFill(1, 1), SetResize(1, 1), SetScrollbar(WID_NS_SCROLLBAR), SetDataTip(STR_NULL, STR_NEWGRF_SETTINGS_FILE_TOOLTIP),
1825 EndContainer(),
1826 EndContainer(),
1827 NWidget(NWID_VSCROLLBAR, COLOUR_MAUVE, WID_NS_SCROLLBAR),
1828 EndContainer(),
1829 /* Buttons. */
1830 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_NS_SHOW_REMOVE),
1831 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPadding(2, 2, 2, 2), SetPIP(0, WD_RESIZEBOX_WIDTH, 0),
1832 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_REMOVE), SetFill(1, 0), SetResize(1, 0),
1833 SetDataTip(STR_NEWGRF_SETTINGS_REMOVE, STR_NEWGRF_SETTINGS_REMOVE_TOOLTIP),
1834 NWidget(NWID_VERTICAL),
1835 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_MOVE_UP), SetFill(1, 0), SetResize(1, 0),
1836 SetDataTip(STR_NEWGRF_SETTINGS_MOVEUP, STR_NEWGRF_SETTINGS_MOVEUP_TOOLTIP),
1837 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_MOVE_DOWN), SetFill(1, 0), SetResize(1, 0),
1838 SetDataTip(STR_NEWGRF_SETTINGS_MOVEDOWN, STR_NEWGRF_SETTINGS_MOVEDOWN_TOOLTIP),
1839 EndContainer(),
1840 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_UPGRADE), SetFill(1, 0), SetResize(1, 0),
1841 SetDataTip(STR_NEWGRF_SETTINGS_UPGRADE, STR_NEWGRF_SETTINGS_UPGRADE_TOOLTIP),
1842 EndContainer(),
1843
1844 NWidget(NWID_VERTICAL, NC_EQUALSIZE), SetPadding(2, 2, 2, 2),
1845 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_RESCAN_FILES2), SetFill(1, 0), SetResize(1, 0),
1846 SetDataTip(STR_NEWGRF_SETTINGS_RESCAN_FILES, STR_NEWGRF_SETTINGS_RESCAN_FILES_TOOLTIP),
1847 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_CONTENT_DOWNLOAD2), SetFill(1, 0), SetResize(1, 0),
1848 SetDataTip(STR_INTRO_ONLINE_CONTENT, STR_INTRO_TOOLTIP_ONLINE_CONTENT),
1849 EndContainer(),
1850 EndContainer(),
1851 EndContainer(),
1852 };
1853
1854 static const NWidgetPart _nested_newgrf_availables_widgets[] = {
1855 NWidget(WWT_PANEL, COLOUR_MAUVE),
1856 NWidget(WWT_LABEL, COLOUR_MAUVE), SetDataTip(STR_NEWGRF_SETTINGS_INACTIVE_LIST, STR_NULL),
1857 SetFill(1, 0), SetResize(1, 0), SetPadding(3, WD_FRAMETEXT_RIGHT, 0, WD_FRAMETEXT_LEFT),
1858 /* Left side, available grfs, filter edit box. */
1859 NWidget(NWID_HORIZONTAL), SetPadding(WD_TEXTPANEL_TOP, 0, WD_TEXTPANEL_BOTTOM, 0),
1860 SetPIP(WD_FRAMETEXT_LEFT, WD_FRAMETEXT_RIGHT, WD_FRAMETEXT_RIGHT),
1861 NWidget(WWT_TEXT, COLOUR_MAUVE), SetFill(0, 1), SetDataTip(STR_NEWGRF_FILTER_TITLE, STR_NULL),
1862 NWidget(WWT_EDITBOX, COLOUR_MAUVE, WID_NS_FILTER), SetFill(1, 0), SetMinimalSize(50, 12), SetResize(1, 0),
1863 SetDataTip(STR_LIST_FILTER_OSKTITLE, STR_LIST_FILTER_TOOLTIP),
1864 EndContainer(),
1865 /* Left side, available grfs. */
1866 NWidget(NWID_HORIZONTAL), SetPadding(0, 2, 0, 2),
1867 NWidget(WWT_PANEL, COLOUR_MAUVE),
1868 NWidget(WWT_INSET, COLOUR_MAUVE, WID_NS_AVAIL_LIST), SetMinimalSize(100, 1), SetPadding(2, 2, 2, 2),
1869 SetFill(1, 1), SetResize(1, 1), SetScrollbar(WID_NS_SCROLL2BAR),
1870 EndContainer(),
1871 EndContainer(),
1872 NWidget(NWID_VSCROLLBAR, COLOUR_MAUVE, WID_NS_SCROLL2BAR),
1873 EndContainer(),
1874 /* Left side, available grfs, buttons. */
1875 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPadding(2, 2, 2, 2), SetPIP(0, WD_RESIZEBOX_WIDTH, 0),
1876 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_ADD), SetFill(1, 0), SetResize(1, 0),
1877 SetDataTip(STR_NEWGRF_SETTINGS_ADD, STR_NEWGRF_SETTINGS_ADD_FILE_TOOLTIP),
1878 NWidget(NWID_VERTICAL),
1879 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_RESCAN_FILES), SetFill(1, 0), SetResize(1, 0),
1880 SetDataTip(STR_NEWGRF_SETTINGS_RESCAN_FILES, STR_NEWGRF_SETTINGS_RESCAN_FILES_TOOLTIP),
1881 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_CONTENT_DOWNLOAD), SetFill(1, 0), SetResize(1, 0),
1882 SetDataTip(STR_INTRO_ONLINE_CONTENT, STR_INTRO_TOOLTIP_ONLINE_CONTENT),
1883 EndContainer(),
1884 EndContainer(),
1885 EndContainer(),
1886 };
1887
1888 static const NWidgetPart _nested_newgrf_infopanel_widgets[] = {
1889 /* Right side, info panel. */
1890 NWidget(NWID_VERTICAL), SetPadding(0, 0, 2, 0),
1891 NWidget(WWT_PANEL, COLOUR_MAUVE), SetPadding(0, 0, 2, 0),
1892 NWidget(WWT_EMPTY, COLOUR_MAUVE, WID_NS_NEWGRF_INFO_TITLE), SetFill(1, 0), SetResize(1, 0),
1893 NWidget(WWT_EMPTY, COLOUR_MAUVE, WID_NS_NEWGRF_INFO), SetFill(1, 1), SetResize(1, 1), SetMinimalSize(150, 100),
1894 EndContainer(),
1895 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_OPEN_URL), SetFill(1, 0), SetResize(1, 0),
1896 SetDataTip(STR_CONTENT_OPEN_URL, STR_CONTENT_OPEN_URL_TOOLTIP),
1897 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_NEWGRF_TEXTFILE + TFT_README), SetFill(1, 0), SetResize(1, 0),
1898 SetDataTip(STR_TEXTFILE_VIEW_README, STR_NULL),
1899 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
1900 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_NEWGRF_TEXTFILE + TFT_CHANGELOG), SetFill(1, 0), SetResize(1, 0),
1901 SetDataTip(STR_TEXTFILE_VIEW_CHANGELOG, STR_NULL),
1902 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_NEWGRF_TEXTFILE + TFT_LICENSE), SetFill(1, 0), SetResize(1, 0),
1903 SetDataTip(STR_TEXTFILE_VIEW_LICENCE, STR_NULL),
1904 EndContainer(),
1905 EndContainer(),
1906 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_NS_SHOW_APPLY),
1907 /* Right side, buttons. */
1908 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(0, WD_RESIZEBOX_WIDTH, 0),
1909 NWidget(NWID_VERTICAL),
1910 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_SET_PARAMETERS), SetFill(1, 0), SetResize(1, 0),
1911 SetDataTip(STR_NEWGRF_SETTINGS_SET_PARAMETERS, STR_NULL),
1912 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_TOGGLE_PALETTE), SetFill(1, 0), SetResize(1, 0),
1913 SetDataTip(STR_NEWGRF_SETTINGS_TOGGLE_PALETTE, STR_NEWGRF_SETTINGS_TOGGLE_PALETTE_TOOLTIP),
1914 EndContainer(),
1915 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_APPLY_CHANGES), SetFill(1, 0), SetResize(1, 0),
1916 SetDataTip(STR_NEWGRF_SETTINGS_APPLY_CHANGES, STR_NULL),
1917 EndContainer(),
1918 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_VIEW_PARAMETERS), SetFill(1, 0), SetResize(1, 0),
1919 SetDataTip(STR_NEWGRF_SETTINGS_SHOW_PARAMETERS, STR_NULL),
1920 EndContainer(),
1921 };
1922
1924 NWidgetBase* NewGRFDisplay(int *biggest_index)
1925 {
1926 NWidgetBase *avs = MakeNWidgets(_nested_newgrf_availables_widgets, lengthof(_nested_newgrf_availables_widgets), biggest_index, NULL);
1927
1928 int biggest2;
1929 NWidgetBase *acs = MakeNWidgets(_nested_newgrf_actives_widgets, lengthof(_nested_newgrf_actives_widgets), &biggest2, NULL);
1930 *biggest_index = max(*biggest_index, biggest2);
1931
1932 NWidgetBase *inf = MakeNWidgets(_nested_newgrf_infopanel_widgets, lengthof(_nested_newgrf_infopanel_widgets), &biggest2, NULL);
1933 *biggest_index = max(*biggest_index, biggest2);
1934
1935 return new NWidgetNewGRFDisplay(avs, acs, inf);
1936 }
1937
1938 /* Widget definition of the manage newgrfs window */
1939 static const NWidgetPart _nested_newgrf_widgets[] = {
1940 NWidget(NWID_HORIZONTAL),
1941 NWidget(WWT_CLOSEBOX, COLOUR_MAUVE),
1942 NWidget(WWT_CAPTION, COLOUR_MAUVE), SetDataTip(STR_NEWGRF_SETTINGS_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
1943 NWidget(WWT_DEFSIZEBOX, COLOUR_MAUVE),
1944 EndContainer(),
1945 NWidget(WWT_PANEL, COLOUR_MAUVE),
1946 NWidgetFunction(NewGRFDisplay), SetPadding(WD_RESIZEBOX_WIDTH, WD_RESIZEBOX_WIDTH, 2, WD_RESIZEBOX_WIDTH),
1947 /* Resize button. */
1948 NWidget(NWID_HORIZONTAL),
1949 NWidget(NWID_SPACER), SetFill(1, 0), SetResize(1, 0),
1950 NWidget(WWT_RESIZEBOX, COLOUR_MAUVE),
1951 EndContainer(),
1952 EndContainer(),
1953 };
1954
1955 /* Window definition of the manage newgrfs window */
1956 static WindowDesc _newgrf_desc(
1957 WDP_CENTER, "settings_newgrf", 300, 263,
1958 WC_GAME_OPTIONS, WC_NONE,
1959 0,
1960 _nested_newgrf_widgets, lengthof(_nested_newgrf_widgets)
1961 );
1962
1968 static void NewGRFConfirmationCallback(Window *w, bool confirmed)
1969 {
1970 if (confirmed) {
1971 DeleteWindowByClass(WC_GRF_PARAMETERS);
1972 NewGRFWindow *nw = dynamic_cast<NewGRFWindow*>(w);
1973
1974 GamelogStartAction(GLAT_GRF);
1975 GamelogGRFUpdate(_grfconfig, nw->actives); // log GRF changes
1976 CopyGRFConfigList(nw->orig_list, nw->actives, false);
1977 ReloadNewGRFData();
1978 GamelogStopAction();
1979
1980 /* Show new, updated list */
1981 GRFConfig *c;
1982 int i = 0;
1983 for (c = nw->actives; c != NULL && c != nw->active_sel; c = c->next, i++) {}
1984 CopyGRFConfigList(&nw->actives, *nw->orig_list, false);
1985 for (c = nw->actives; c != NULL && i > 0; c = c->next, i--) {}
1986 nw->active_sel = c;
1987 nw->avails.ForceRebuild();
1988
1989 w->InvalidateData();
1990
1991 ReInitAllWindows();
1992 DeleteWindowByClass(WC_BUILD_OBJECT);
1993 }
1994 }
1995
1996
1997
2006 void ShowNewGRFSettings(bool editable, bool show_params, bool exec_changes, GRFConfig **config)
2007 {
2008 DeleteWindowByClass(WC_GAME_OPTIONS);
2009 new NewGRFWindow(&_newgrf_desc, editable, show_params, exec_changes, config);
2010 }
2011
2013 static const NWidgetPart _nested_save_preset_widgets[] = {
2014 NWidget(NWID_HORIZONTAL),
2015 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
2016 NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_SAVE_PRESET_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
2017 NWidget(WWT_DEFSIZEBOX, COLOUR_GREY),
2018 EndContainer(),
2019 NWidget(WWT_PANEL, COLOUR_GREY),
2020 NWidget(NWID_HORIZONTAL),
2021 NWidget(WWT_INSET, COLOUR_GREY, WID_SVP_PRESET_LIST), SetPadding(2, 1, 0, 2),
2022 SetDataTip(0x0, STR_SAVE_PRESET_LIST_TOOLTIP), SetResize(1, 10), SetScrollbar(WID_SVP_SCROLLBAR), EndContainer(),
2023 NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_SVP_SCROLLBAR),
2024 EndContainer(),
2025 NWidget(WWT_EDITBOX, COLOUR_GREY, WID_SVP_EDITBOX), SetPadding(3, 2, 2, 2), SetFill(1, 0), SetResize(1, 0),
2026 SetDataTip(STR_SAVE_PRESET_TITLE, STR_SAVE_PRESET_EDITBOX_TOOLTIP),
2027 EndContainer(),
2028 NWidget(NWID_HORIZONTAL),
2029 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SVP_CANCEL), SetDataTip(STR_SAVE_PRESET_CANCEL, STR_SAVE_PRESET_CANCEL_TOOLTIP), SetFill(1, 0), SetResize(1, 0),
2030 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SVP_SAVE), SetDataTip(STR_SAVE_PRESET_SAVE, STR_SAVE_PRESET_SAVE_TOOLTIP), SetFill(1, 0), SetResize(1, 0),
2031 NWidget(WWT_RESIZEBOX, COLOUR_GREY),
2032 EndContainer(),
2033 };
2034
2036 static WindowDesc _save_preset_desc(
2037 WDP_CENTER, "save_preset", 140, 110,
2038 WC_SAVE_PRESET, WC_GAME_OPTIONS,
2039 WDF_MODAL,
2040 _nested_save_preset_widgets, lengthof(_nested_save_preset_widgets)
2041 );
2042
2044 struct SavePresetWindow : public Window {
2045 QueryString presetname_editbox;
2046 GRFPresetList presets;
2047 Scrollbar *vscroll;
2048 int selected;
2049
2054 SavePresetWindow(const char *initial_text) : Window(&_save_preset_desc), presetname_editbox(32)
2055 {
2056 GetGRFPresetList(&this->presets);
2057 this->selected = -1;
2058 if (initial_text != NULL) {
2059 for (uint i = 0; i < this->presets.Length(); i++) {
2060 if (!strcmp(initial_text, this->presets[i])) {
2061 this->selected = i;
2062 break;
2063 }
2064 }
2065 }
2066
2067 this->querystrings[WID_SVP_EDITBOX] = &this->presetname_editbox;
2068 this->presetname_editbox.ok_button = WID_SVP_SAVE;
2069 this->presetname_editbox.cancel_button = WID_SVP_CANCEL;
2070
2071 this->CreateNestedTree();
2072 this->vscroll = this->GetScrollbar(WID_SVP_SCROLLBAR);
2073 this->FinishInitNested(0);
2074
2075 this->vscroll->SetCount(this->presets.Length());
2076 this->SetFocusedWidget(WID_SVP_EDITBOX);
2077 if (initial_text != NULL) this->presetname_editbox.text.Assign(initial_text);
2078 }
2079
2080 ~SavePresetWindow()
2081 {
2082 }
2083
2084 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
2085 {
2086 switch (widget) {
2087 case WID_SVP_PRESET_LIST: {
2088 resize->height = FONT_HEIGHT_NORMAL + 2U;
2089 size->height = 0;
2090 for (uint i = 0; i < this->presets.Length(); i++) {
2091 Dimension d = GetStringBoundingBox(this->presets[i]);
2092 size->width = max(size->width, d.width + WD_FRAMETEXT_LEFT + WD_FRAMETEXT_RIGHT);
2093 resize->height = max(resize->height, d.height);
2094 }
2095 size->height = ClampU(this->presets.Length(), 5, 20) * resize->height + 1;
2096 break;
2097 }
2098 }
2099 }
2100
2101 virtual void DrawWidget(const Rect &r, int widget) const
2102 {
2103 switch (widget) {
2104 case WID_SVP_PRESET_LIST: {
2105 GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, PC_BLACK);
2106
2107 uint step_height = this->GetWidget<NWidgetBase>(WID_SVP_PRESET_LIST)->resize_y;
2108 int offset_y = (step_height - FONT_HEIGHT_NORMAL) / 2;
2109 uint y = r.top + WD_FRAMERECT_TOP;
2110 uint min_index = this->vscroll->GetPosition();
2111 uint max_index = min(min_index + this->vscroll->GetCapacity(), this->presets.Length());
2112
2113 for (uint i = min_index; i < max_index; i++) {
2114 if ((int)i == this->selected) GfxFillRect(r.left + 1, y, r.right - 1, y + step_height - 2, PC_DARK_BLUE);
2115
2116 const char *text = this->presets[i];
2117 DrawString(r.left + WD_FRAMERECT_LEFT, r.right, y + offset_y, text, ((int)i == this->selected) ? TC_WHITE : TC_SILVER);
2118 y += step_height;
2119 }
2120 break;
2121 }
2122 }
2123 }
2124
2125 virtual void OnClick(Point pt, int widget, int click_count)
2126 {
2127 switch (widget) {
2128 case WID_SVP_PRESET_LIST: {
2129 uint row = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_SVP_PRESET_LIST);
2130 if (row < this->presets.Length()) {
2131 this->selected = row;
2132 this->presetname_editbox.text.Assign(this->presets[row]);
2133 this->SetWidgetDirty(WID_SVP_PRESET_LIST);
2134 this->SetWidgetDirty(WID_SVP_EDITBOX);
2135 }
2136 break;
2137 }
2138
2139 case WID_SVP_CANCEL:
2140 delete this;
2141 break;
2142
2143 case WID_SVP_SAVE: {
2144 Window *w = FindWindowById(WC_GAME_OPTIONS, WN_GAME_OPTIONS_NEWGRF_STATE);
2145 if (w != NULL && !StrEmpty(this->presetname_editbox.text.buf)) w->OnQueryTextFinished(this->presetname_editbox.text.buf);
2146 delete this;
2147 break;
2148 }
2149 }
2150 }
2151
2152 virtual void OnResize()
2153 {
2154 this->vscroll->SetCapacityFromWidget(this, WID_SVP_PRESET_LIST);
2155 }
2156 };
2157
2162 static void ShowSavePresetWindow(const char *initial_text)
2163 {
2164 DeleteWindowByClass(WC_SAVE_PRESET);
2165 new SavePresetWindow(initial_text);
2166 }
2167
2169 static const NWidgetPart _nested_scan_progress_widgets[] = {
2170 NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_NEWGRF_SCAN_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
2171 NWidget(WWT_PANEL, COLOUR_GREY),
2172 NWidget(NWID_HORIZONTAL), SetPIP(20, 0, 20),
2173 NWidget(NWID_VERTICAL), SetPIP(11, 8, 11),
2174 NWidget(WWT_LABEL, INVALID_COLOUR), SetDataTip(STR_NEWGRF_SCAN_MESSAGE, STR_NULL), SetFill(1, 0),
2175 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_SP_PROGRESS_BAR), SetFill(1, 0),
2176 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_SP_PROGRESS_TEXT), SetFill(1, 0),
2177 EndContainer(),
2178 EndContainer(),
2179 EndContainer(),
2180 };
2181
2183 static WindowDesc _scan_progress_desc(
2184 WDP_CENTER, NULL, 0, 0,
2185 WC_MODAL_PROGRESS, WC_NONE,
2186 0,
2187 _nested_scan_progress_widgets, lengthof(_nested_scan_progress_widgets)
2188 );
2189
2191 struct ScanProgressWindow : public Window {
2192 char *last_name;
2193 int scanned;
2194
2196 ScanProgressWindow() : Window(&_scan_progress_desc), last_name(NULL), scanned(0)
2197 {
2198 this->InitNested(1);
2199 }
2200
2202 ~ScanProgressWindow()
2203 {
2204 free(last_name);
2205 }
2206
2207 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
2208 {
2209 switch (widget) {
2210 case WID_SP_PROGRESS_BAR: {
2211 SetDParamMaxValue(0, 100);
2212 *size = GetStringBoundingBox(STR_GENERATION_PROGRESS);
2213 /* We need some spacing for the 'border' */
2214 size->height += 8;
2215 size->width += 8;
2216 break;
2217 }
2218
2219 case WID_SP_PROGRESS_TEXT:
2220 SetDParamMaxDigits(0, 4);
2221 SetDParamMaxDigits(1, 4);
2222 /* We really don't know the width. We could determine it by scanning the NewGRFs,
2223 * but this is the status window for scanning them... */
2224 size->width = max(400U, GetStringBoundingBox(STR_NEWGRF_SCAN_STATUS).width);
2225 size->height = FONT_HEIGHT_NORMAL * 2 + WD_PAR_VSEP_NORMAL;
2226 break;
2227 }
2228 }
2229
2230 virtual void DrawWidget(const Rect &r, int widget) const
2231 {
2232 switch (widget) {
2233 case WID_SP_PROGRESS_BAR: {
2234 /* Draw the % complete with a bar and a text */
2235 DrawFrameRect(r.left, r.top, r.right, r.bottom, COLOUR_GREY, FR_BORDERONLY);
2236 uint percent = scanned * 100 / max(1U, _settings_client.gui.last_newgrf_count);
2237 DrawFrameRect(r.left + 1, r.top + 1, (int)((r.right - r.left - 2) * percent / 100) + r.left + 1, r.bottom - 1, COLOUR_MAUVE, FR_NONE);
2238 SetDParam(0, percent);
2239 DrawString(r.left, r.right, r.top + 5, STR_GENERATION_PROGRESS, TC_FROMSTRING, SA_HOR_CENTER);
2240 break;
2241 }
2242
2243 case WID_SP_PROGRESS_TEXT:
2244 SetDParam(0, this->scanned);
2245 SetDParam(1, _settings_client.gui.last_newgrf_count);
2246 DrawString(r.left, r.right, r.top, STR_NEWGRF_SCAN_STATUS, TC_FROMSTRING, SA_HOR_CENTER);
2247
2248 DrawString(r.left, r.right, r.top + FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL, this->last_name == NULL ? "" : this->last_name, TC_BLACK, SA_HOR_CENTER);
2249 break;
2250 }
2251 }
2252
2258 void UpdateNewGRFScanStatus(uint num, const char *name)
2259 {
2260 free(this->last_name);
2261 if (name == NULL) {
2262 char buf[256];
2263 GetString(buf, STR_NEWGRF_SCAN_ARCHIVES, lastof(buf));
2264 this->last_name = stredup(buf);
2265 } else {
2266 this->last_name = stredup(name);
2267 }
2268 this->scanned = num;
2269 if (num > _settings_client.gui.last_newgrf_count) _settings_client.gui.last_newgrf_count = num;
2270
2271 this->SetDirty();
2272 }
2273 };
2274
2280 void UpdateNewGRFScanStatus(uint num, const char *name)
2281 {
2282 ScanProgressWindow *w = dynamic_cast<ScanProgressWindow *>(FindWindowByClass(WC_MODAL_PROGRESS));
2283 if (w == NULL) w = new ScanProgressWindow();
2284 w->UpdateNewGRFScanStatus(num, name);
2285 }