OpenTTD
newgrf_gui.cpp
Go to the documentation of this file.
1 /* $Id$ */
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"
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 #include "guitimer_func.h"
34 
35 #include "widgets/newgrf_widget.h"
36 #include "widgets/misc_widget.h"
37 
38 #include "table/sprites.h"
39 
40 #include <map>
41 #include "safeguards.h"
42 
47 {
48  /* Do not show errors when entering the main screen */
49  if (_game_mode == GM_MENU) return;
50 
51  for (const GRFConfig *c = _grfconfig; c != nullptr; c = c->next) {
52  /* We only want to show fatal errors */
53  if (c->error == nullptr || c->error->severity != STR_NEWGRF_ERROR_MSG_FATAL) continue;
54 
55  SetDParam (0, c->error->custom_message == nullptr ? c->error->message : STR_JUST_RAW_STRING);
56  SetDParamStr(1, c->error->custom_message);
57  SetDParamStr(2, c->filename);
58  SetDParamStr(3, c->error->data);
59  for (uint i = 0; i < lengthof(c->error->param_value); i++) {
60  SetDParam(4 + i, c->error->param_value[i]);
61  }
62  ShowErrorMessage(STR_NEWGRF_ERROR_FATAL_POPUP, INVALID_STRING_ID, WL_CRITICAL);
63  break;
64  }
65 }
66 
67 static void ShowNewGRFInfo(const GRFConfig *c, uint x, uint y, uint right, uint bottom, bool show_params)
68 {
69  if (c->error != nullptr) {
70  char message[512];
71  SetDParamStr(0, c->error->custom_message); // is skipped by built-in messages
72  SetDParamStr(1, c->filename);
73  SetDParamStr(2, c->error->data);
74  for (uint i = 0; i < lengthof(c->error->param_value); i++) {
75  SetDParam(3 + i, c->error->param_value[i]);
76  }
77  GetString(message, c->error->custom_message == nullptr ? c->error->message : STR_JUST_RAW_STRING, lastof(message));
78 
79  SetDParamStr(0, message);
80  y = DrawStringMultiLine(x, right, y, bottom, c->error->severity);
81  }
82 
83  /* Draw filename or not if it is not known (GRF sent over internet) */
84  if (c->filename != nullptr) {
85  SetDParamStr(0, c->filename);
86  y = DrawStringMultiLine(x, right, y, bottom, STR_NEWGRF_SETTINGS_FILENAME);
87  }
88 
89  /* Prepare and draw GRF ID */
90  char buff[256];
91  seprintf(buff, lastof(buff), "%08X", BSWAP32(c->ident.grfid));
92  SetDParamStr(0, buff);
93  y = DrawStringMultiLine(x, right, y, bottom, STR_NEWGRF_SETTINGS_GRF_ID);
94 
96  SetDParam(0, c->version);
97  y = DrawStringMultiLine(x, right, y, bottom, STR_NEWGRF_SETTINGS_VERSION);
98  }
101  y = DrawStringMultiLine(x, right, y, bottom, STR_NEWGRF_SETTINGS_MIN_VERSION);
102  }
103 
104  /* Prepare and draw MD5 sum */
105  md5sumToString(buff, lastof(buff), c->ident.md5sum);
106  SetDParamStr(0, buff);
107  y = DrawStringMultiLine(x, right, y, bottom, STR_NEWGRF_SETTINGS_MD5SUM);
108 
109  /* Show GRF parameter list */
110  if (show_params) {
111  if (c->num_params > 0) {
112  GRFBuildParamList(buff, c, lastof(buff));
113  SetDParam(0, STR_JUST_RAW_STRING);
114  SetDParamStr(1, buff);
115  } else {
116  SetDParam(0, STR_NEWGRF_SETTINGS_PARAMETER_NONE);
117  }
118  y = DrawStringMultiLine(x, right, y, bottom, STR_NEWGRF_SETTINGS_PARAMETER);
119 
120  /* Draw the palette of the NewGRF */
121  if (c->palette & GRFP_BLT_32BPP) {
122  SetDParam(0, (c->palette & GRFP_USE_WINDOWS) ? STR_NEWGRF_SETTINGS_PALETTE_LEGACY_32BPP : STR_NEWGRF_SETTINGS_PALETTE_DEFAULT_32BPP);
123  } else {
124  SetDParam(0, (c->palette & GRFP_USE_WINDOWS) ? STR_NEWGRF_SETTINGS_PALETTE_LEGACY : STR_NEWGRF_SETTINGS_PALETTE_DEFAULT);
125  }
126  y = DrawStringMultiLine(x, right, y, bottom, STR_NEWGRF_SETTINGS_PALETTE);
127  }
128 
129  /* Show flags */
130  if (c->status == GCS_NOT_FOUND) y = DrawStringMultiLine(x, right, y, bottom, STR_NEWGRF_SETTINGS_NOT_FOUND);
131  if (c->status == GCS_DISABLED) y = DrawStringMultiLine(x, right, y, bottom, STR_NEWGRF_SETTINGS_DISABLED);
132  if (HasBit(c->flags, GCF_INVALID)) y = DrawStringMultiLine(x, right, y, bottom, STR_NEWGRF_SETTINGS_INCOMPATIBLE);
133  if (HasBit(c->flags, GCF_COMPATIBLE)) y = DrawStringMultiLine(x, right, y, bottom, STR_NEWGRF_COMPATIBLE_LOADED);
134 
135  /* Draw GRF info if it exists */
136  if (!StrEmpty(c->GetDescription())) {
137  SetDParamStr(0, c->GetDescription());
138  y = DrawStringMultiLine(x, right, y, bottom, STR_BLACK_RAW_STRING);
139  } else {
140  y = DrawStringMultiLine(x, right, y, bottom, STR_NEWGRF_SETTINGS_NO_INFO);
141  }
142 }
143 
155  uint clicked_row;
157  Scrollbar *vscroll;
159  bool editable;
160 
161  NewGRFParametersWindow(WindowDesc *desc, GRFConfig *c, bool editable) : Window(desc),
162  grf_config(c),
163  clicked_button(UINT_MAX),
164  clicked_dropdown(false),
165  closing_dropdown(false),
166  clicked_row(UINT_MAX),
167  editable(editable)
168  {
169  this->action14present = (c->num_valid_params != lengthof(c->param) || c->param_info.size() != 0);
170 
171  this->CreateNestedTree();
172  this->vscroll = this->GetScrollbar(WID_NP_SCROLLBAR);
173  this->GetWidget<NWidgetStacked>(WID_NP_SHOW_NUMPAR)->SetDisplayedPlane(this->action14present ? SZSP_HORIZONTAL : 0);
174  this->GetWidget<NWidgetStacked>(WID_NP_SHOW_DESCRIPTION)->SetDisplayedPlane(this->action14present ? 0 : SZSP_HORIZONTAL);
175  this->FinishInitNested(); // Initializes 'this->line_height' as side effect.
176 
177  this->SetWidgetDisabledState(WID_NP_RESET, !this->editable);
178 
179  this->InvalidateData();
180  }
181 
188  {
189  dummy_parameter_info.param_nr = nr;
190  return &dummy_parameter_info;
191  }
192 
193  void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
194  {
195  switch (widget) {
196  case WID_NP_NUMPAR_DEC:
197  case WID_NP_NUMPAR_INC: {
198  size->width = max(SETTING_BUTTON_WIDTH / 2, FONT_HEIGHT_NORMAL);
200  break;
201  }
202 
203  case WID_NP_NUMPAR: {
204  SetDParamMaxValue(0, lengthof(this->grf_config->param));
205  Dimension d = GetStringBoundingBox(this->GetWidget<NWidgetCore>(widget)->widget_data);
206  d.width += padding.width;
207  d.height += padding.height;
208  *size = maxdim(*size, d);
209  break;
210  }
211 
212  case WID_NP_BACKGROUND:
214 
215  resize->width = 1;
216  resize->height = this->line_height;
217  size->height = 5 * this->line_height;
218  break;
219 
220  case WID_NP_DESCRIPTION:
221  /* Minimum size of 4 lines. The 500 is the default size of the window. */
223  for (uint i = 0; i < this->grf_config->param_info.size(); i++) {
224  const GRFParameterInfo *par_info = this->grf_config->param_info[i];
225  if (par_info == nullptr) continue;
226  const char *desc = GetGRFStringFromGRFText(par_info->desc);
227  if (desc == nullptr) continue;
228  Dimension d = GetStringMultiLineBoundingBox(desc, suggestion);
230  suggestion = maxdim(d, suggestion);
231  }
232  size->height = suggestion.height;
233  break;
234  }
235  }
236 
237  void SetStringParameters(int widget) const override
238  {
239  switch (widget) {
240  case WID_NP_NUMPAR:
241  SetDParam(0, this->vscroll->GetCount());
242  break;
243  }
244  }
245 
246  void DrawWidget(const Rect &r, int widget) const override
247  {
248  if (widget == WID_NP_DESCRIPTION) {
249  const GRFParameterInfo *par_info = (this->clicked_row < this->grf_config->param_info.size()) ? this->grf_config->param_info[this->clicked_row] : nullptr;
250  if (par_info == nullptr) return;
251  const char *desc = GetGRFStringFromGRFText(par_info->desc);
252  if (desc == nullptr) return;
253  DrawStringMultiLine(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + WD_TEXTPANEL_TOP, r.bottom - WD_TEXTPANEL_BOTTOM, desc, TC_BLACK);
254  return;
255  } else if (widget != WID_NP_BACKGROUND) {
256  return;
257  }
258 
259  bool rtl = _current_text_dir == TD_RTL;
260  uint buttons_left = rtl ? r.right - SETTING_BUTTON_WIDTH - 3 : r.left + 4;
261  uint text_left = r.left + (rtl ? WD_FRAMERECT_LEFT : SETTING_BUTTON_WIDTH + 8);
262  uint text_right = r.right - (rtl ? SETTING_BUTTON_WIDTH + 8 : WD_FRAMERECT_RIGHT);
263 
264  int y = r.top;
265  int button_y_offset = (this->line_height - SETTING_BUTTON_HEIGHT) / 2;
266  int text_y_offset = (this->line_height - FONT_HEIGHT_NORMAL) / 2;
267  for (uint i = this->vscroll->GetPosition(); this->vscroll->IsVisible(i) && i < this->vscroll->GetCount(); i++) {
268  GRFParameterInfo *par_info = (i < this->grf_config->param_info.size()) ? this->grf_config->param_info[i] : nullptr;
269  if (par_info == nullptr) par_info = GetDummyParameterInfo(i);
270  uint32 current_value = par_info->GetValue(this->grf_config);
271  bool selected = (i == this->clicked_row);
272 
273  if (par_info->type == PTYPE_BOOL) {
274  DrawBoolButton(buttons_left, y + button_y_offset, current_value != 0, this->editable);
275  SetDParam(2, par_info->GetValue(this->grf_config) == 0 ? STR_CONFIG_SETTING_OFF : STR_CONFIG_SETTING_ON);
276  } else if (par_info->type == PTYPE_UINT_ENUM) {
277  if (par_info->complete_labels) {
278  DrawDropDownButton(buttons_left, y + button_y_offset, COLOUR_YELLOW, this->clicked_row == i && this->clicked_dropdown, this->editable);
279  } else {
280  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);
281  }
282  SetDParam(2, STR_JUST_INT);
283  SetDParam(3, current_value);
284  if (par_info->value_names.Contains(current_value)) {
285  const char *label = GetGRFStringFromGRFText(par_info->value_names.Find(current_value)->second);
286  if (label != nullptr) {
287  SetDParam(2, STR_JUST_RAW_STRING);
288  SetDParamStr(3, label);
289  }
290  }
291  }
292 
293  const char *name = GetGRFStringFromGRFText(par_info->name);
294  if (name != nullptr) {
295  SetDParam(0, STR_JUST_RAW_STRING);
296  SetDParamStr(1, name);
297  } else {
298  SetDParam(0, STR_NEWGRF_PARAMETERS_DEFAULT_NAME);
299  SetDParam(1, i + 1);
300  }
301 
302  DrawString(text_left, text_right, y + text_y_offset, STR_NEWGRF_PARAMETERS_SETTING, selected ? TC_WHITE : TC_LIGHT_BLUE);
303  y += this->line_height;
304  }
305  }
306 
307  void OnPaint() override
308  {
309  if (this->closing_dropdown) {
310  this->closing_dropdown = false;
311  this->clicked_dropdown = false;
312  }
313  this->DrawWidgets();
314  }
315 
316  void OnClick(Point pt, int widget, int click_count) override
317  {
318  switch (widget) {
319  case WID_NP_NUMPAR_DEC:
320  if (this->editable && !this->action14present && this->grf_config->num_params > 0) {
321  this->grf_config->num_params--;
322  this->InvalidateData();
324  }
325  break;
326 
327  case WID_NP_NUMPAR_INC: {
328  GRFConfig *c = this->grf_config;
329  if (this->editable && !this->action14present && c->num_params < c->num_valid_params) {
330  c->param[c->num_params++] = 0;
331  this->InvalidateData();
333  }
334  break;
335  }
336 
337  case WID_NP_BACKGROUND: {
338  if (!this->editable) break;
339  uint num = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_NP_BACKGROUND);
340  if (num >= this->vscroll->GetCount()) break;
341  if (this->clicked_row != num) {
343  HideDropDownMenu(this);
344  this->clicked_row = num;
345  this->clicked_dropdown = false;
346  }
347 
348  const NWidgetBase *wid = this->GetWidget<NWidgetBase>(WID_NP_BACKGROUND);
349  int x = pt.x - wid->pos_x;
350  if (_current_text_dir == TD_RTL) x = wid->current_x - 1 - x;
351  x -= 4;
352 
353  GRFParameterInfo *par_info = (num < this->grf_config->param_info.size()) ? this->grf_config->param_info[num] : nullptr;
354  if (par_info == nullptr) par_info = GetDummyParameterInfo(num);
355 
356  /* One of the arrows is clicked */
357  uint32 old_val = par_info->GetValue(this->grf_config);
358  if (par_info->type != PTYPE_BOOL && IsInsideMM(x, 0, SETTING_BUTTON_WIDTH) && par_info->complete_labels) {
359  if (this->clicked_dropdown) {
360  /* unclick the dropdown */
361  HideDropDownMenu(this);
362  this->clicked_dropdown = false;
363  this->closing_dropdown = false;
364  } else {
365  const NWidgetBase *wid = this->GetWidget<NWidgetBase>(WID_NP_BACKGROUND);
366  int rel_y = (pt.y - (int)wid->pos_y) % this->line_height;
367 
368  Rect wi_rect;
369  wi_rect.left = pt.x - (_current_text_dir == TD_RTL ? SETTING_BUTTON_WIDTH - 1 - x : x);;
370  wi_rect.right = wi_rect.left + SETTING_BUTTON_WIDTH - 1;
371  wi_rect.top = pt.y - rel_y + (this->line_height - SETTING_BUTTON_HEIGHT) / 2;
372  wi_rect.bottom = wi_rect.top + SETTING_BUTTON_HEIGHT - 1;
373 
374  /* For dropdowns we also have to check the y position thoroughly, the mouse may not above the just opening dropdown */
375  if (pt.y >= wi_rect.top && pt.y <= wi_rect.bottom) {
376  this->clicked_dropdown = true;
377  this->closing_dropdown = false;
378 
379  DropDownList list;
380  for (uint32 i = par_info->min_value; i <= par_info->max_value; i++) {
381  list.emplace_back(new DropDownListCharStringItem(GetGRFStringFromGRFText(par_info->value_names.Find(i)->second), i, false));
382  }
383 
384  ShowDropDownListAt(this, std::move(list), old_val, -1, wi_rect, COLOUR_ORANGE, true);
385  }
386  }
387  } else if (IsInsideMM(x, 0, SETTING_BUTTON_WIDTH)) {
388  uint32 val = old_val;
389  if (par_info->type == PTYPE_BOOL) {
390  val = !val;
391  } else {
392  if (x >= SETTING_BUTTON_WIDTH / 2) {
393  /* Increase button clicked */
394  if (val < par_info->max_value) val++;
395  this->clicked_increase = true;
396  } else {
397  /* Decrease button clicked */
398  if (val > par_info->min_value) val--;
399  this->clicked_increase = false;
400  }
401  }
402  if (val != old_val) {
403  par_info->SetValue(this->grf_config, val);
404 
405  this->clicked_button = num;
406  this->timeout.SetInterval(150);
407  }
408  } else if (par_info->type == PTYPE_UINT_ENUM && !par_info->complete_labels && click_count >= 2) {
409  /* Display a query box so users can enter a custom value. */
410  SetDParam(0, old_val);
411  ShowQueryString(STR_JUST_INT, STR_CONFIG_SETTING_QUERY_CAPTION, 10, this, CS_NUMERAL, QSF_NONE);
412  }
413  this->SetDirty();
414  break;
415  }
416 
417  case WID_NP_RESET:
418  if (!this->editable) break;
419  this->grf_config->SetParameterDefaults();
420  this->InvalidateData();
422  break;
423 
424  case WID_NP_ACCEPT:
425  delete this;
426  break;
427  }
428  }
429 
430  void OnQueryTextFinished(char *str) override
431  {
432  if (StrEmpty(str)) return;
433  int32 value = atoi(str);
434  GRFParameterInfo *par_info = ((uint)this->clicked_row < this->grf_config->param_info.size()) ? this->grf_config->param_info[this->clicked_row] : nullptr;
435  if (par_info == nullptr) par_info = GetDummyParameterInfo(this->clicked_row);
436  uint32 val = Clamp<uint32>(value, par_info->min_value, par_info->max_value);
437  par_info->SetValue(this->grf_config, val);
438  this->SetDirty();
439  }
440 
441  void OnDropdownSelect(int widget, int index) override
442  {
443  assert(this->clicked_dropdown);
444  GRFParameterInfo *par_info = ((uint)this->clicked_row < this->grf_config->param_info.size()) ? this->grf_config->param_info[this->clicked_row] : nullptr;
445  if (par_info == nullptr) par_info = GetDummyParameterInfo(this->clicked_row);
446  par_info->SetValue(this->grf_config, index);
447  this->SetDirty();
448  }
449 
450  void OnDropdownClose(Point pt, int widget, int index, bool instant_close) override
451  {
452  /* We cannot raise the dropdown button just yet. OnClick needs some hint, whether
453  * the same dropdown button was clicked again, and then not open the dropdown again.
454  * So, we only remember that it was closed, and process it on the next OnPaint, which is
455  * after OnClick. */
456  assert(this->clicked_dropdown);
457  this->closing_dropdown = true;
458  this->SetDirty();
459  }
460 
461  void OnResize() override
462  {
463  this->vscroll->SetCapacityFromWidget(this, WID_NP_BACKGROUND);
464  }
465 
471  void OnInvalidateData(int data = 0, bool gui_scope = true) override
472  {
473  if (!gui_scope) return;
474  if (!this->action14present) {
475  this->SetWidgetDisabledState(WID_NP_NUMPAR_DEC, !this->editable || this->grf_config->num_params == 0);
476  this->SetWidgetDisabledState(WID_NP_NUMPAR_INC, !this->editable || this->grf_config->num_params >= this->grf_config->num_valid_params);
477  }
478 
479  this->vscroll->SetCount(this->action14present ? this->grf_config->num_valid_params : this->grf_config->num_params);
480  if (this->clicked_row != UINT_MAX && this->clicked_row >= this->vscroll->GetCount()) {
481  this->clicked_row = UINT_MAX;
483  }
484  }
485 
486  void OnRealtimeTick(uint delta_ms) override
487  {
488  if (timeout.Elapsed(delta_ms)) {
489  this->clicked_button = UINT_MAX;
490  this->SetDirty();
491  }
492  }
493 };
495 
496 
497 static const NWidgetPart _nested_newgrf_parameter_widgets[] = {
499  NWidget(WWT_CLOSEBOX, COLOUR_MAUVE),
500  NWidget(WWT_CAPTION, COLOUR_MAUVE), SetDataTip(STR_NEWGRF_PARAMETERS_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
501  NWidget(WWT_DEFSIZEBOX, COLOUR_MAUVE),
502  EndContainer(),
503  NWidget(NWID_SELECTION, INVALID_COLOUR, WID_NP_SHOW_NUMPAR),
504  NWidget(WWT_PANEL, COLOUR_MAUVE), SetResize(1, 0), SetFill(1, 0), SetPIP(4, 0, 4),
505  NWidget(NWID_HORIZONTAL), SetPIP(4, 0, 4),
506  NWidget(WWT_PUSHARROWBTN, COLOUR_YELLOW, WID_NP_NUMPAR_DEC), SetMinimalSize(12, 12), SetDataTip(AWV_DECREASE, STR_NULL),
507  NWidget(WWT_PUSHARROWBTN, COLOUR_YELLOW, WID_NP_NUMPAR_INC), SetMinimalSize(12, 12), SetDataTip(AWV_INCREASE, STR_NULL),
508  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),
509  EndContainer(),
510  EndContainer(),
511  EndContainer(),
513  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),
514  NWidget(NWID_VSCROLLBAR, COLOUR_MAUVE, WID_NP_SCROLLBAR),
515  EndContainer(),
517  NWidget(WWT_PANEL, COLOUR_MAUVE, WID_NP_DESCRIPTION), SetResize(1, 0), SetFill(1, 0),
518  EndContainer(),
519  EndContainer(),
522  NWidget(WWT_PUSHTXTBTN, COLOUR_MAUVE, WID_NP_ACCEPT), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_NEWGRF_PARAMETERS_CLOSE, STR_NULL),
523  NWidget(WWT_PUSHTXTBTN, COLOUR_MAUVE, WID_NP_RESET), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_NEWGRF_PARAMETERS_RESET, STR_NEWGRF_PARAMETERS_RESET_TOOLTIP),
524  EndContainer(),
525  NWidget(WWT_RESIZEBOX, COLOUR_MAUVE),
526  EndContainer(),
527 };
528 
531  WDP_CENTER, "settings_newgrf_config", 500, 208,
533  0,
534  _nested_newgrf_parameter_widgets, lengthof(_nested_newgrf_parameter_widgets)
535 );
536 
537 static void OpenGRFParameterWindow(GRFConfig *c, bool editable)
538 {
540  new NewGRFParametersWindow(&_newgrf_parameters_desc, c, editable);
541 }
542 
546 
547  NewGRFTextfileWindow(TextfileType file_type, const GRFConfig *c) : TextfileWindow(file_type), grf_config(c)
548  {
549  const char *textfile = this->grf_config->GetTextfile(file_type);
550  this->LoadTextfile(textfile, NEWGRF_DIR);
551  }
552 
553  void SetStringParameters(int widget) const override
554  {
555  if (widget == WID_TF_CAPTION) {
556  SetDParam(0, STR_CONTENT_TYPE_NEWGRF);
557  SetDParamStr(1, this->grf_config->GetName());
558  }
559  }
560 };
561 
562 void ShowNewGRFTextfileWindow(TextfileType file_type, const GRFConfig *c)
563 {
564  DeleteWindowById(WC_TEXTFILE, file_type);
565  new NewGRFTextfileWindow(file_type, c);
566 }
567 
568 typedef std::map<uint32, const GRFConfig *> GrfIdMap;
569 
575 static void FillGrfidMap(const GRFConfig *c, GrfIdMap *grfid_map)
576 {
577  while (c != nullptr) {
578  std::pair<uint32, const GRFConfig *> p(c->ident.grfid, c);
579  grfid_map->insert(p);
580  c = c->next;
581  }
582 }
583 
584 static void NewGRFConfirmationCallback(Window *w, bool confirmed);
585 static void ShowSavePresetWindow(const char *initial_text);
586 
592 
593  static const uint EDITBOX_MAX_SIZE = 50;
594 
597  static GUIGRFConfigList::SortFunction * const sorter_funcs[];
598  static GUIGRFConfigList::FilterFunction * const filter_funcs[];
599 
600  GUIGRFConfigList avails;
602  int avail_pos;
605 
607 
610 
612  bool editable;
613  bool show_params;
614  bool execute;
615  int preset;
617 
618  Scrollbar *vscroll;
619  Scrollbar *vscroll2;
620 
621  NewGRFWindow(WindowDesc *desc, bool editable, bool show_params, bool execute, GRFConfig **orig_list) : Window(desc), filter_editbox(EDITBOX_MAX_SIZE)
622  {
623  this->avail_sel = nullptr;
624  this->avail_pos = -1;
625  this->active_sel = nullptr;
626  this->actives = nullptr;
627  this->orig_list = orig_list;
628  this->editable = editable;
629  this->execute = execute;
630  this->show_params = show_params;
631  this->preset = -1;
632  this->active_over = -1;
633 
634  CopyGRFConfigList(&this->actives, *orig_list, false);
635  this->grf_presets = GetGRFPresetList();
636 
637  this->CreateNestedTree();
638  this->vscroll = this->GetScrollbar(WID_NS_SCROLLBAR);
639  this->vscroll2 = this->GetScrollbar(WID_NS_SCROLL2BAR);
640 
641  this->GetWidget<NWidgetStacked>(WID_NS_SHOW_REMOVE)->SetDisplayedPlane(this->editable ? 0 : 1);
642  this->GetWidget<NWidgetStacked>(WID_NS_SHOW_APPLY)->SetDisplayedPlane(this->editable ? 0 : this->show_params ? 1 : SZSP_HORIZONTAL);
644 
645  this->querystrings[WID_NS_FILTER] = &this->filter_editbox;
646  this->filter_editbox.cancel_button = QueryString::ACTION_CLEAR;
647  if (editable) {
649  } else {
651  }
652 
653  this->avails.SetListing(this->last_sorting);
654  this->avails.SetFiltering(this->last_filtering);
655  this->avails.SetSortFuncs(this->sorter_funcs);
656  this->avails.SetFilterFuncs(this->filter_funcs);
657  this->avails.ForceRebuild();
658 
660  }
661 
662  ~NewGRFWindow()
663  {
667 
668  if (this->editable && !this->execute) {
669  CopyGRFConfigList(this->orig_list, this->actives, true);
670  ResetGRFConfig(false);
672  }
673 
674  /* Remove the temporary copy of grf-list used in window */
675  ClearGRFConfigList(&this->actives);
676  }
677 
683  {
684  GrfIdMap grfid_map;
685  FillGrfidMap(this->actives, &grfid_map);
686 
687  for (const GRFConfig *a = _all_grfs; a != nullptr; a = a->next) {
688  GrfIdMap::const_iterator iter = grfid_map.find(a->ident.grfid);
689  if (iter != grfid_map.end() && a->version > iter->second->version) return true;
690  }
691  return false;
692  }
693 
696  {
697  GrfIdMap grfid_map;
698  FillGrfidMap(this->actives, &grfid_map);
699 
700  for (const GRFConfig *a = _all_grfs; a != nullptr; a = a->next) {
701  GrfIdMap::iterator iter = grfid_map.find(a->ident.grfid);
702  if (iter == grfid_map.end() || iter->second->version >= a->version) continue;
703 
704  GRFConfig **c = &this->actives;
705  while (*c != iter->second) c = &(*c)->next;
706  GRFConfig *d = new GRFConfig(*a);
707  d->next = (*c)->next;
708  d->CopyParams(**c);
709  if (this->active_sel == *c) {
712  this->active_sel = nullptr;
713  }
714  delete *c;
715  *c = d;
716  iter->second = d;
717  }
718  }
719 
720  void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
721  {
722  switch (widget) {
723  case WID_NS_FILE_LIST:
724  {
725  Dimension d = maxdim(GetSpriteSize(SPR_SQUARE), GetSpriteSize(SPR_WARNING_SIGN));
726  resize->height = max(d.height + 2U, FONT_HEIGHT_NORMAL + 2U);
727  size->height = max(size->height, WD_FRAMERECT_TOP + 6 * resize->height + WD_FRAMERECT_BOTTOM);
728  break;
729  }
730 
731  case WID_NS_AVAIL_LIST:
732  resize->height = max(12, FONT_HEIGHT_NORMAL + 2);
733  size->height = max(size->height, WD_FRAMERECT_TOP + 8 * resize->height + WD_FRAMERECT_BOTTOM);
734  break;
735 
737  Dimension dim = GetStringBoundingBox(STR_NEWGRF_SETTINGS_INFO_TITLE);
738  size->height = max(size->height, dim.height + WD_FRAMETEXT_TOP + WD_FRAMETEXT_BOTTOM);
739  size->width = max(size->width, dim.width + WD_FRAMETEXT_LEFT + WD_FRAMETEXT_RIGHT);
740  break;
741  }
742 
743  case WID_NS_NEWGRF_INFO:
744  size->height = max(size->height, WD_FRAMERECT_TOP + 10 * FONT_HEIGHT_NORMAL + WD_FRAMERECT_BOTTOM + padding.height + 2);
745  break;
746 
747  case WID_NS_PRESET_LIST: {
748  Dimension d = GetStringBoundingBox(STR_NUM_CUSTOM);
749  for (const auto &i : this->grf_presets) {
750  SetDParamStr(0, i.c_str());
751  d = maxdim(d, GetStringBoundingBox(STR_JUST_RAW_STRING));
752  }
753  d.width += padding.width;
754  *size = maxdim(d, *size);
755  break;
756  }
757 
760  Dimension d = GetStringBoundingBox(STR_NEWGRF_SETTINGS_FIND_MISSING_CONTENT_BUTTON);
761  *size = maxdim(d, GetStringBoundingBox(STR_INTRO_ONLINE_CONTENT));
762  size->width += padding.width;
763  size->height += padding.height;
764  break;
765  }
766  }
767  }
768 
769  void OnResize() override
770  {
771  this->vscroll->SetCapacityFromWidget(this, WID_NS_FILE_LIST);
772  this->vscroll2->SetCapacityFromWidget(this, WID_NS_AVAIL_LIST);
773  }
774 
775  void SetStringParameters(int widget) const override
776  {
777  switch (widget) {
778  case WID_NS_PRESET_LIST:
779  if (this->preset == -1) {
780  SetDParam(0, STR_NUM_CUSTOM);
781  } else {
782  SetDParam(0, STR_JUST_RAW_STRING);
783  SetDParamStr(1, this->grf_presets[this->preset].c_str());
784  }
785  break;
786  }
787  }
788 
794  inline PaletteID GetPalette(const GRFConfig *c) const
795  {
796  PaletteID pal;
797 
798  /* Pick a colour */
799  switch (c->status) {
800  case GCS_NOT_FOUND:
801  case GCS_DISABLED:
802  pal = PALETTE_TO_RED;
803  break;
804  case GCS_ACTIVATED:
805  pal = PALETTE_TO_GREEN;
806  break;
807  default:
808  pal = PALETTE_TO_BLUE;
809  break;
810  }
811 
812  /* Do not show a "not-failure" colour when it actually failed to load */
813  if (pal != PALETTE_TO_RED) {
814  if (HasBit(c->flags, GCF_STATIC)) {
815  pal = PALETTE_TO_GREY;
816  } else if (HasBit(c->flags, GCF_COMPATIBLE)) {
817  pal = PALETTE_TO_ORANGE;
818  }
819  }
820 
821  return pal;
822  }
823 
824  void DrawWidget(const Rect &r, int widget) const override
825  {
826  switch (widget) {
827  case WID_NS_FILE_LIST: {
828  GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, PC_BLACK);
829 
830  uint step_height = this->GetWidget<NWidgetBase>(WID_NS_FILE_LIST)->resize_y;
831  uint y = r.top + WD_FRAMERECT_TOP;
832  Dimension square = GetSpriteSize(SPR_SQUARE);
833  Dimension warning = GetSpriteSize(SPR_WARNING_SIGN);
834  int square_offset_y = (step_height - square.height) / 2;
835  int warning_offset_y = (step_height - warning.height) / 2;
836  int offset_y = (step_height - FONT_HEIGHT_NORMAL) / 2;
837 
838  bool rtl = _current_text_dir == TD_RTL;
839  uint text_left = rtl ? r.left + WD_FRAMERECT_LEFT : r.left + square.width + 15;
840  uint text_right = rtl ? r.right - square.width - 15 : r.right - WD_FRAMERECT_RIGHT;
841  uint square_left = rtl ? r.right - square.width - 5 : r.left + 5;
842  uint warning_left = rtl ? r.right - square.width - warning.width - 10 : r.left + square.width + 10;
843 
844  int i = 0;
845  for (const GRFConfig *c = this->actives; c != nullptr; c = c->next, i++) {
846  if (this->vscroll->IsVisible(i)) {
847  const char *text = c->GetName();
848  bool h = (this->active_sel == c);
849  PaletteID pal = this->GetPalette(c);
850 
851  if (h) {
852  GfxFillRect(r.left + 1, y, r.right - 1, y + step_height - 1, PC_DARK_BLUE);
853  } else if (i == this->active_over) {
854  /* Get index of current selection. */
855  int active_sel_pos = 0;
856  for (GRFConfig *c = this->actives; c != nullptr && c != this->active_sel; c = c->next, active_sel_pos++) {}
857  if (active_sel_pos != this->active_over) {
858  uint top = this->active_over < active_sel_pos ? y + 1 : y + step_height - 2;
859  GfxFillRect(r.left + WD_FRAMERECT_LEFT, top - 1, r.right - WD_FRAMERECT_RIGHT, top + 1, PC_GREY);
860  }
861  }
862  DrawSprite(SPR_SQUARE, pal, square_left, y + square_offset_y);
863  if (c->error != nullptr) DrawSprite(SPR_WARNING_SIGN, 0, warning_left, y + warning_offset_y);
864  uint txtoffset = c->error == nullptr ? 0 : warning.width;
865  DrawString(text_left + (rtl ? 0 : txtoffset), text_right - (rtl ? txtoffset : 0), y + offset_y, text, h ? TC_WHITE : TC_ORANGE);
866  y += step_height;
867  }
868  }
869  if (i == this->active_over && this->vscroll->IsVisible(i)) { // Highlight is after the last GRF entry.
870  GfxFillRect(r.left + WD_FRAMERECT_LEFT, y, r.right - WD_FRAMERECT_RIGHT, y + 2, PC_GREY);
871  }
872  break;
873  }
874 
875  case WID_NS_AVAIL_LIST: {
876  GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, this->active_over == -2 ? PC_DARK_GREY : PC_BLACK);
877 
878  uint step_height = this->GetWidget<NWidgetBase>(WID_NS_AVAIL_LIST)->resize_y;
879  int offset_y = (step_height - FONT_HEIGHT_NORMAL) / 2;
880  uint y = r.top + WD_FRAMERECT_TOP;
881  uint min_index = this->vscroll2->GetPosition();
882  uint max_index = min(min_index + this->vscroll2->GetCapacity(), (uint)this->avails.size());
883 
884  for (uint i = min_index; i < max_index; i++) {
885  const GRFConfig *c = this->avails[i];
886  bool h = (c == this->avail_sel);
887  const char *text = c->GetName();
888 
889  if (h) GfxFillRect(r.left + 1, y, r.right - 1, y + step_height - 1, PC_DARK_BLUE);
890  DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y + offset_y, text, h ? TC_WHITE : TC_SILVER);
891  y += step_height;
892  }
893  break;
894  }
895 
897  /* Create the nice grayish rectangle at the details top. */
898  GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, PC_DARK_BLUE);
899  DrawString(r.left, r.right, (r.top + r.bottom - FONT_HEIGHT_NORMAL) / 2, STR_NEWGRF_SETTINGS_INFO_TITLE, TC_FROMSTRING, SA_HOR_CENTER);
900  break;
901 
902  case WID_NS_NEWGRF_INFO: {
903  const GRFConfig *selected = this->active_sel;
904  if (selected == nullptr) selected = this->avail_sel;
905  if (selected != nullptr) {
906  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);
907  }
908  break;
909  }
910  }
911  }
912 
913  void OnClick(Point pt, int widget, int click_count) override
914  {
915  if (widget >= WID_NS_NEWGRF_TEXTFILE && widget < WID_NS_NEWGRF_TEXTFILE + TFT_END) {
916  if (this->active_sel == nullptr && this->avail_sel == nullptr) return;
917 
918  ShowNewGRFTextfileWindow((TextfileType)(widget - WID_NS_NEWGRF_TEXTFILE), this->active_sel != nullptr ? this->active_sel : this->avail_sel);
919  return;
920  }
921 
922  switch (widget) {
923  case WID_NS_PRESET_LIST: {
924  DropDownList list;
925 
926  /* Add 'None' option for clearing list */
927  list.emplace_back(new DropDownListStringItem(STR_NONE, -1, false));
928 
929  for (uint i = 0; i < this->grf_presets.size(); i++) {
930  list.emplace_back(new DropDownListCharStringItem(this->grf_presets[i].c_str(), i, false));
931  }
932 
933  this->DeleteChildWindows(WC_QUERY_STRING); // Remove the parameter query window
934  ShowDropDownList(this, std::move(list), this->preset, WID_NS_PRESET_LIST);
935  break;
936  }
937 
938  case WID_NS_OPEN_URL: {
939  const GRFConfig *c = (this->avail_sel == nullptr) ? this->active_sel : this->avail_sel;
940 
941  extern void OpenBrowser(const char *url);
942  OpenBrowser(c->GetURL());
943  break;
944  }
945 
946  case WID_NS_PRESET_SAVE:
947  ShowSavePresetWindow((this->preset == -1) ? nullptr : this->grf_presets[this->preset].c_str());
948  break;
949 
951  if (this->preset == -1) return;
952 
953  DeleteGRFPresetFromConfig(this->grf_presets[this->preset].c_str());
954  this->grf_presets = GetGRFPresetList();
955  this->preset = -1;
956  this->InvalidateData();
957  this->DeleteChildWindows(WC_QUERY_STRING); // Remove the parameter query window
958  break;
959 
960  case WID_NS_MOVE_UP: { // Move GRF up
961  if (this->active_sel == nullptr || !this->editable) break;
962 
963  int pos = 0;
964  for (GRFConfig **pc = &this->actives; *pc != nullptr; pc = &(*pc)->next, pos++) {
965  GRFConfig *c = *pc;
966  if (c->next == this->active_sel) {
967  c->next = this->active_sel->next;
968  this->active_sel->next = c;
969  *pc = this->active_sel;
970  break;
971  }
972  }
973  this->vscroll->ScrollTowards(pos);
974  this->preset = -1;
975  this->InvalidateData();
976  break;
977  }
978 
979  case WID_NS_MOVE_DOWN: { // Move GRF down
980  if (this->active_sel == nullptr || !this->editable) break;
981 
982  int pos = 1; // Start at 1 as we swap the selected newgrf with the next one
983  for (GRFConfig **pc = &this->actives; *pc != nullptr; pc = &(*pc)->next, pos++) {
984  GRFConfig *c = *pc;
985  if (c == this->active_sel) {
986  *pc = c->next;
987  c->next = c->next->next;
988  (*pc)->next = c;
989  break;
990  }
991  }
992  this->vscroll->ScrollTowards(pos);
993  this->preset = -1;
994  this->InvalidateData();
995  break;
996  }
997 
998  case WID_NS_FILE_LIST: { // Select an active GRF.
1000 
1001  uint i = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_NS_FILE_LIST);
1002 
1003  GRFConfig *c;
1004  for (c = this->actives; c != nullptr && i > 0; c = c->next, i--) {}
1005 
1006  if (this->active_sel != c) {
1009  }
1010  this->active_sel = c;
1011  this->avail_sel = nullptr;
1012  this->avail_pos = -1;
1013 
1014  this->InvalidateData();
1015  if (click_count == 1) {
1016  if (this->editable && this->active_sel != nullptr) SetObjectToPlaceWnd(SPR_CURSOR_MOUSE, PAL_NONE, HT_DRAG, this);
1017  break;
1018  }
1019  /* With double click, continue */
1020  FALLTHROUGH;
1021  }
1022 
1023  case WID_NS_REMOVE: { // Remove GRF
1024  if (this->active_sel == nullptr || !this->editable) break;
1027 
1028  /* Choose the next GRF file to be the selected file. */
1029  GRFConfig *newsel = this->active_sel->next;
1030  for (GRFConfig **pc = &this->actives; *pc != nullptr; pc = &(*pc)->next) {
1031  GRFConfig *c = *pc;
1032  /* If the new selection is empty (i.e. we're deleting the last item
1033  * in the list, pick the file just before the selected file */
1034  if (newsel == nullptr && c->next == this->active_sel) newsel = c;
1035 
1036  if (c == this->active_sel) {
1037  if (newsel == c) newsel = nullptr;
1038 
1039  *pc = c->next;
1040  delete c;
1041  break;
1042  }
1043  }
1044 
1045  this->active_sel = newsel;
1046  this->preset = -1;
1047  this->avail_pos = -1;
1048  this->avail_sel = nullptr;
1049  this->avails.ForceRebuild();
1051  break;
1052  }
1053 
1054  case WID_NS_UPGRADE: { // Upgrade GRF.
1055  if (!this->editable || this->actives == nullptr) break;
1056  UpgradeCurrent();
1058  break;
1059  }
1060 
1061  case WID_NS_AVAIL_LIST: { // Select a non-active GRF.
1063 
1064  uint i = this->vscroll2->GetScrolledRowFromWidget(pt.y, this, WID_NS_AVAIL_LIST);
1065  this->active_sel = nullptr;
1067  if (i < this->avails.size()) {
1068  if (this->avail_sel != this->avails[i]) DeleteWindowByClass(WC_TEXTFILE);
1069  this->avail_sel = this->avails[i];
1070  this->avail_pos = i;
1071  }
1072  this->InvalidateData();
1073  if (click_count == 1) {
1074  if (this->editable && this->avail_sel != nullptr && !HasBit(this->avail_sel->flags, GCF_INVALID)) SetObjectToPlaceWnd(SPR_CURSOR_MOUSE, PAL_NONE, HT_DRAG, this);
1075  break;
1076  }
1077  /* With double click, continue */
1078  FALLTHROUGH;
1079  }
1080 
1081  case WID_NS_ADD:
1082  if (this->avail_sel == nullptr || !this->editable || HasBit(this->avail_sel->flags, GCF_INVALID)) break;
1083 
1084  this->AddGRFToActive();
1085  break;
1086 
1087  case WID_NS_APPLY_CHANGES: // Apply changes made to GRF list
1088  if (!this->editable) break;
1089  if (this->execute) {
1090  ShowQuery(
1091  STR_NEWGRF_POPUP_CAUTION_CAPTION,
1092  STR_NEWGRF_CONFIRMATION_TEXT,
1093  this,
1095  );
1096  } else {
1097  CopyGRFConfigList(this->orig_list, this->actives, true);
1098  ResetGRFConfig(false);
1099  ReloadNewGRFData();
1100  }
1101  this->DeleteChildWindows(WC_QUERY_STRING); // Remove the parameter query window
1102  break;
1103 
1105  case WID_NS_SET_PARAMETERS: { // Edit parameters
1106  if (this->active_sel == nullptr || !this->show_params || this->active_sel->num_valid_params == 0) break;
1107 
1108  OpenGRFParameterWindow(this->active_sel, this->editable);
1109  break;
1110  }
1111 
1112  case WID_NS_TOGGLE_PALETTE:
1113  if (this->active_sel != nullptr && this->editable) {
1114  this->active_sel->palette ^= GRFP_USE_MASK;
1115  this->SetDirty();
1116  }
1117  break;
1118 
1121  if (!_network_available) {
1122  ShowErrorMessage(STR_NETWORK_ERROR_NOTAVAILABLE, INVALID_STRING_ID, WL_ERROR);
1123  } else {
1124  this->DeleteChildWindows(WC_QUERY_STRING); // Remove the parameter query window
1125 
1126  ShowMissingContentWindow(this->actives);
1127  }
1128  break;
1129 
1130  case WID_NS_RESCAN_FILES:
1131  case WID_NS_RESCAN_FILES2:
1132  ScanNewGRFFiles(this);
1133  break;
1134  }
1135  }
1136 
1137  void OnNewGRFsScanned() override
1138  {
1139  if (this->active_sel == nullptr) DeleteWindowByClass(WC_TEXTFILE);
1140  this->avail_sel = nullptr;
1141  this->avail_pos = -1;
1142  this->avails.ForceRebuild();
1143  this->DeleteChildWindows(WC_QUERY_STRING); // Remove the parameter query window
1144  }
1145 
1146  void OnDropdownSelect(int widget, int index) override
1147  {
1148  if (!this->editable) return;
1149 
1150  ClearGRFConfigList(&this->actives);
1151  this->preset = index;
1152 
1153  if (index != -1) {
1154  this->actives = LoadGRFPresetFromConfig(this->grf_presets[index].c_str());
1155  }
1156  this->avails.ForceRebuild();
1157 
1161  this->active_sel = nullptr;
1163  }
1164 
1165  void OnQueryTextFinished(char *str) override
1166  {
1167  if (str == nullptr) return;
1168 
1169  SaveGRFPresetToConfig(str, this->actives);
1170  this->grf_presets = GetGRFPresetList();
1171 
1172  /* Switch to this preset */
1173  for (uint i = 0; i < this->grf_presets.size(); i++) {
1174  if (this->grf_presets[i] == str) {
1175  this->preset = i;
1176  break;
1177  }
1178  }
1179 
1180  this->InvalidateData();
1181  }
1182 
1188  void OnInvalidateData(int data = 0, bool gui_scope = true) override
1189  {
1190  if (!gui_scope) return;
1191  switch (data) {
1192  default:
1193  /* Nothing important to do */
1194  break;
1195 
1196  case GOID_NEWGRF_RESCANNED:
1197  /* Search the list for items that are now found and mark them as such. */
1198  for (GRFConfig **l = &this->actives; *l != nullptr; l = &(*l)->next) {
1199  GRFConfig *c = *l;
1200  bool compatible = HasBit(c->flags, GCF_COMPATIBLE);
1201  if (c->status != GCS_NOT_FOUND && !compatible) continue;
1202 
1203  const GRFConfig *f = FindGRFConfig(c->ident.grfid, FGCM_EXACT, compatible ? c->original_md5sum : c->ident.md5sum);
1204  if (f == nullptr || HasBit(f->flags, GCF_INVALID)) continue;
1205 
1206  *l = new GRFConfig(*f);
1207  (*l)->next = c->next;
1208 
1209  if (this->active_sel == c) this->active_sel = *l;
1210 
1211  delete c;
1212  }
1213 
1214  this->avails.ForceRebuild();
1215  FALLTHROUGH;
1216 
1218  this->preset = -1;
1219  FALLTHROUGH;
1220 
1222  /* Update scrollbars */
1223  int i = 0;
1224  for (const GRFConfig *c = this->actives; c != nullptr; c = c->next, i++) {}
1225 
1226  this->vscroll->SetCount(i + 1); // Reserve empty space for drag and drop handling.
1227 
1228  if (this->avail_pos >= 0) this->vscroll2->ScrollTowards(this->avail_pos);
1229  break;
1230  }
1231  }
1232 
1233  this->BuildAvailables();
1234 
1235  this->SetWidgetsDisabledState(!this->editable,
1240  );
1241  this->SetWidgetDisabledState(WID_NS_ADD, !this->editable || this->avail_sel == nullptr || HasBit(this->avail_sel->flags, GCF_INVALID));
1242  this->SetWidgetDisabledState(WID_NS_UPGRADE, !this->editable || this->actives == nullptr || !this->CanUpgradeCurrent());
1243 
1244  bool disable_all = this->active_sel == nullptr || !this->editable;
1245  this->SetWidgetsDisabledState(disable_all,
1246  WID_NS_REMOVE,
1250  );
1251 
1252  const GRFConfig *c = (this->avail_sel == nullptr) ? this->active_sel : this->avail_sel;
1253  for (TextfileType tft = TFT_BEGIN; tft < TFT_END; tft++) {
1254  this->SetWidgetDisabledState(WID_NS_NEWGRF_TEXTFILE + tft, c == nullptr || c->GetTextfile(tft) == nullptr);
1255  }
1256  this->SetWidgetDisabledState(WID_NS_OPEN_URL, c == nullptr || StrEmpty(c->GetURL()));
1257 
1258  this->SetWidgetDisabledState(WID_NS_SET_PARAMETERS, !this->show_params || this->active_sel == nullptr || this->active_sel->num_valid_params == 0);
1259  this->SetWidgetDisabledState(WID_NS_VIEW_PARAMETERS, !this->show_params || this->active_sel == nullptr || this->active_sel->num_valid_params == 0);
1260  this->SetWidgetDisabledState(WID_NS_TOGGLE_PALETTE, disable_all ||
1262 
1263  if (!disable_all) {
1264  /* All widgets are now enabled, so disable widgets we can't use */
1265  if (this->active_sel == this->actives) this->DisableWidget(WID_NS_MOVE_UP);
1266  if (this->active_sel->next == nullptr) this->DisableWidget(WID_NS_MOVE_DOWN);
1267  }
1268 
1269  this->SetWidgetDisabledState(WID_NS_PRESET_DELETE, this->preset == -1);
1270 
1271  bool has_missing = false;
1272  bool has_compatible = false;
1273  for (const GRFConfig *c = this->actives; !has_missing && c != nullptr; c = c->next) {
1274  has_missing |= c->status == GCS_NOT_FOUND;
1275  has_compatible |= HasBit(c->flags, GCF_COMPATIBLE);
1276  }
1277  uint32 widget_data;
1278  StringID tool_tip;
1279  if (has_missing || has_compatible) {
1280  widget_data = STR_NEWGRF_SETTINGS_FIND_MISSING_CONTENT_BUTTON;
1281  tool_tip = STR_NEWGRF_SETTINGS_FIND_MISSING_CONTENT_TOOLTIP;
1282  } else {
1283  widget_data = STR_INTRO_ONLINE_CONTENT;
1284  tool_tip = STR_INTRO_TOOLTIP_ONLINE_CONTENT;
1285  }
1286  this->GetWidget<NWidgetCore>(WID_NS_CONTENT_DOWNLOAD)->widget_data = widget_data;
1287  this->GetWidget<NWidgetCore>(WID_NS_CONTENT_DOWNLOAD)->tool_tip = tool_tip;
1288  this->GetWidget<NWidgetCore>(WID_NS_CONTENT_DOWNLOAD2)->widget_data = widget_data;
1289  this->GetWidget<NWidgetCore>(WID_NS_CONTENT_DOWNLOAD2)->tool_tip = tool_tip;
1290 
1291  this->SetWidgetDisabledState(WID_NS_PRESET_SAVE, has_missing);
1292  }
1293 
1294  EventState OnKeyPress(WChar key, uint16 keycode) override
1295  {
1296  if (!this->editable) return ES_NOT_HANDLED;
1297 
1298  switch (keycode) {
1299  case WKC_UP:
1300  /* scroll up by one */
1301  if (this->avail_pos > 0) this->avail_pos--;
1302  break;
1303 
1304  case WKC_DOWN:
1305  /* scroll down by one */
1306  if (this->avail_pos < (int)this->avails.size() - 1) this->avail_pos++;
1307  break;
1308 
1309  case WKC_PAGEUP:
1310  /* scroll up a page */
1311  this->avail_pos = (this->avail_pos < this->vscroll2->GetCapacity()) ? 0 : this->avail_pos - this->vscroll2->GetCapacity();
1312  break;
1313 
1314  case WKC_PAGEDOWN:
1315  /* scroll down a page */
1316  this->avail_pos = min(this->avail_pos + this->vscroll2->GetCapacity(), (int)this->avails.size() - 1);
1317  break;
1318 
1319  case WKC_HOME:
1320  /* jump to beginning */
1321  this->avail_pos = 0;
1322  break;
1323 
1324  case WKC_END:
1325  /* jump to end */
1326  this->avail_pos = (uint)this->avails.size() - 1;
1327  break;
1328 
1329  default:
1330  return ES_NOT_HANDLED;
1331  }
1332 
1333  if (this->avails.size() == 0) this->avail_pos = -1;
1334  if (this->avail_pos >= 0) {
1335  this->active_sel = nullptr;
1337  if (this->avail_sel != this->avails[this->avail_pos]) DeleteWindowByClass(WC_TEXTFILE);
1338  this->avail_sel = this->avails[this->avail_pos];
1339  this->vscroll2->ScrollTowards(this->avail_pos);
1340  this->InvalidateData(0);
1341  }
1342 
1343  return ES_HANDLED;
1344  }
1345 
1346  void OnEditboxChanged(int wid) override
1347  {
1348  if (!this->editable) return;
1349 
1350  string_filter.SetFilterTerm(this->filter_editbox.text.buf);
1351  this->avails.SetFilterState(!string_filter.IsEmpty());
1352  this->avails.ForceRebuild();
1353  this->InvalidateData(0);
1354  }
1355 
1356  void OnDragDrop(Point pt, int widget) override
1357  {
1358  if (!this->editable) return;
1359 
1360  if (widget == WID_NS_FILE_LIST) {
1361  if (this->active_sel != nullptr) {
1362  /* Get pointer to the selected file in the active list. */
1363  int from_pos = 0;
1364  GRFConfig **from_prev;
1365  for (from_prev = &this->actives; *from_prev != this->active_sel; from_prev = &(*from_prev)->next, from_pos++) {}
1366 
1367  /* Gets the drag-and-drop destination offset. Ignore the last dummy line. */
1368  int to_pos = min(this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_NS_FILE_LIST), this->vscroll->GetCount() - 2);
1369  if (to_pos != from_pos) { // Don't move NewGRF file over itself.
1370  /* Get pointer to destination position. */
1371  GRFConfig **to_prev = &this->actives;
1372  for (int i = from_pos < to_pos ? -1 : 0; *to_prev != nullptr && i < to_pos; to_prev = &(*to_prev)->next, i++) {}
1373 
1374  /* Detach NewGRF file from its original position. */
1375  *from_prev = this->active_sel->next;
1376 
1377  /* Attach NewGRF file to its new position. */
1378  this->active_sel->next = *to_prev;
1379  *to_prev = this->active_sel;
1380 
1381  this->vscroll->ScrollTowards(to_pos);
1382  this->preset = -1;
1383  this->InvalidateData();
1384  }
1385  } else if (this->avail_sel != nullptr) {
1386  int to_pos = min(this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_NS_FILE_LIST), this->vscroll->GetCount() - 1);
1387  this->AddGRFToActive(to_pos);
1388  }
1389  } else if (widget == WID_NS_AVAIL_LIST && this->active_sel != nullptr) {
1390  /* Remove active NewGRF file by dragging it over available list. */
1391  Point dummy = {-1, -1};
1392  this->OnClick(dummy, WID_NS_REMOVE, 1);
1393  }
1394 
1396 
1397  if (this->active_over != -1) {
1398  /* End of drag-and-drop, hide dragged destination highlight. */
1399  this->SetWidgetDirty(this->active_over == -2 ? WID_NS_AVAIL_LIST : WID_NS_FILE_LIST);
1400  this->active_over = -1;
1401  }
1402  }
1403 
1404  void OnMouseDrag(Point pt, int widget) override
1405  {
1406  if (!this->editable) return;
1407 
1408  if (widget == WID_NS_FILE_LIST && (this->active_sel != nullptr || this->avail_sel != nullptr)) {
1409  /* An NewGRF file is dragged over the active list. */
1410  int to_pos = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_NS_FILE_LIST);
1411  /* Skip the last dummy line if the source is from the active list. */
1412  to_pos = min(to_pos, this->vscroll->GetCount() - (this->active_sel != nullptr ? 2 : 1));
1413 
1414  if (to_pos != this->active_over) {
1415  this->active_over = to_pos;
1417  }
1418  } else if (widget == WID_NS_AVAIL_LIST && this->active_sel != nullptr) {
1419  this->active_over = -2;
1421  } else if (this->active_over != -1) {
1422  this->SetWidgetDirty(this->active_over == -2 ? WID_NS_AVAIL_LIST : WID_NS_FILE_LIST);
1423  this->active_over = -1;
1424  }
1425  }
1426 
1427 private:
1429  static bool NameSorter(const GRFConfig * const &a, const GRFConfig * const &b)
1430  {
1431  int i = strnatcmp(a->GetName(), b->GetName(), true); // Sort by name (natural sorting).
1432  if (i != 0) return i < 0;
1433 
1434  i = a->version - b->version;
1435  if (i != 0) return i < 0;
1436 
1437  return memcmp(a->ident.md5sum, b->ident.md5sum, lengthof(b->ident.md5sum)) < 0;
1438  }
1439 
1441  static bool CDECL TagNameFilter(const GRFConfig * const *a, StringFilter &filter)
1442  {
1443  filter.ResetState();
1444  filter.AddLine((*a)->GetName());
1445  filter.AddLine((*a)->filename);
1446  filter.AddLine((*a)->GetDescription());
1447  return filter.GetState();;
1448  }
1449 
1450  void BuildAvailables()
1451  {
1452  if (!this->avails.NeedRebuild()) return;
1453 
1454  this->avails.clear();
1455 
1456  for (const GRFConfig *c = _all_grfs; c != nullptr; c = c->next) {
1457  bool found = false;
1458  for (const GRFConfig *grf = this->actives; grf != nullptr && !found; grf = grf->next) found = grf->ident.HasGrfIdentifier(c->ident.grfid, c->ident.md5sum);
1459  if (found) continue;
1460 
1462  this->avails.push_back(c);
1463  } else {
1465  /*
1466  * If the best version is 0, then all NewGRF with this GRF ID
1467  * have version 0, so for backward compatibility reasons we
1468  * want to show them all.
1469  * If we are the best version, then we definitely want to
1470  * show that NewGRF!.
1471  */
1472  if (best->version == 0 || best->ident.HasGrfIdentifier(c->ident.grfid, c->ident.md5sum)) {
1473  this->avails.push_back(c);
1474  }
1475  }
1476  }
1477 
1478  this->avails.Filter(this->string_filter);
1479  this->avails.shrink_to_fit();
1480  this->avails.RebuildDone();
1481  this->avails.Sort();
1482 
1483  if (this->avail_sel != nullptr) {
1484  this->avail_pos = find_index(this->avails, this->avail_sel);
1485  if (this->avail_pos == -1) {
1486  this->avail_sel = nullptr;
1487  }
1488  }
1489 
1490  this->vscroll2->SetCount((uint)this->avails.size()); // Update the scrollbar
1491  }
1492 
1498  bool AddGRFToActive(int ins_pos = -1)
1499  {
1500  if (this->avail_sel == nullptr || !this->editable || HasBit(this->avail_sel->flags, GCF_INVALID)) return false;
1501 
1503 
1504  uint count = 0;
1505  GRFConfig **entry = nullptr;
1506  GRFConfig **list;
1507  /* Find last entry in the list, checking for duplicate grfid on the way */
1508  for (list = &this->actives; *list != nullptr; list = &(*list)->next, ins_pos--) {
1509  if (ins_pos == 0) entry = list; // Insert position? Save.
1510  if ((*list)->ident.grfid == this->avail_sel->ident.grfid) {
1511  ShowErrorMessage(STR_NEWGRF_DUPLICATE_GRFID, INVALID_STRING_ID, WL_INFO);
1512  return false;
1513  }
1514  if (!HasBit((*list)->flags, GCF_STATIC)) count++;
1515  }
1516  if (entry == nullptr) entry = list;
1517  if (count >= NETWORK_MAX_GRF_COUNT) {
1518  ShowErrorMessage(STR_NEWGRF_TOO_MANY_NEWGRFS, INVALID_STRING_ID, WL_INFO);
1519  return false;
1520  }
1521 
1522  GRFConfig *c = new GRFConfig(*this->avail_sel); // Copy GRF details from scanned list.
1523  c->SetParameterDefaults();
1524 
1525  /* Insert GRF config to configuration list. */
1526  c->next = *entry;
1527  *entry = c;
1528 
1529  /* Select next (or previous, if last one) item in the list. */
1530  int new_pos = this->avail_pos + 1;
1531  if (new_pos >= (int)this->avails.size()) new_pos = this->avail_pos - 1;
1532  this->avail_pos = new_pos;
1533  if (new_pos >= 0) this->avail_sel = this->avails[new_pos];
1534 
1535  this->avails.ForceRebuild();
1537  return true;
1538  }
1539 };
1540 
1546 {
1547  /* Only show the things in the current list, or everything when nothing's selected */
1548  ContentVector cv;
1549  for (const GRFConfig *c = list; c != nullptr; c = c->next) {
1550  if (c->status != GCS_NOT_FOUND && !HasBit(c->flags, GCF_COMPATIBLE)) continue;
1551 
1552  ContentInfo *ci = new ContentInfo();
1553  ci->type = CONTENT_TYPE_NEWGRF;
1555  strecpy(ci->name, c->GetName(), lastof(ci->name));
1556  ci->unique_id = BSWAP32(c->ident.grfid);
1557  memcpy(ci->md5sum, HasBit(c->flags, GCF_COMPATIBLE) ? c->original_md5sum : c->ident.md5sum, sizeof(ci->md5sum));
1558  cv.push_back(ci);
1559  }
1560  ShowNetworkContentListWindow(cv.size() == 0 ? nullptr : &cv, CONTENT_TYPE_NEWGRF);
1561 }
1562 
1563 Listing NewGRFWindow::last_sorting = {false, 0};
1565 
1567  &NameSorter,
1568 };
1569 
1571  &TagNameFilter,
1572 };
1573 
1581 public:
1582  static const uint INTER_LIST_SPACING;
1583  static const uint INTER_COLUMN_SPACING;
1584  static const uint MAX_EXTRA_INFO_WIDTH;
1585  static const uint MIN_EXTRA_FOR_3_COLUMNS;
1586 
1590  bool editable;
1591 
1593  {
1594  this->avs = avs;
1595  this->acs = acs;
1596  this->inf = inf;
1597 
1598  this->Add(this->avs);
1599  this->Add(this->acs);
1600  this->Add(this->inf);
1601 
1602  this->editable = true; // Temporary setting, 'real' value is set in SetupSmallestSize().
1603  }
1604 
1605  void SetupSmallestSize(Window *w, bool init_array) override
1606  {
1607  /* Copy state flag from the window. */
1608  assert(dynamic_cast<NewGRFWindow *>(w) != nullptr);
1609  NewGRFWindow *ngw = (NewGRFWindow *)w;
1610  this->editable = ngw->editable;
1611 
1612  this->avs->SetupSmallestSize(w, init_array);
1613  this->acs->SetupSmallestSize(w, init_array);
1614  this->inf->SetupSmallestSize(w, init_array);
1615 
1616  uint min_avs_width = this->avs->smallest_x + this->avs->padding_left + this->avs->padding_right;
1617  uint min_acs_width = this->acs->smallest_x + this->acs->padding_left + this->acs->padding_right;
1618  uint min_inf_width = this->inf->smallest_x + this->inf->padding_left + this->inf->padding_right;
1619 
1620  uint min_avs_height = this->avs->smallest_y + this->avs->padding_top + this->avs->padding_bottom;
1621  uint min_acs_height = this->acs->smallest_y + this->acs->padding_top + this->acs->padding_bottom;
1622  uint min_inf_height = this->inf->smallest_y + this->inf->padding_top + this->inf->padding_bottom;
1623 
1624  /* Smallest window is in two column mode. */
1625  this->smallest_x = max(min_avs_width, min_acs_width) + INTER_COLUMN_SPACING + min_inf_width;
1626  this->smallest_y = max(min_inf_height, min_acs_height + INTER_LIST_SPACING + min_avs_height);
1627 
1628  /* Filling. */
1629  this->fill_x = LeastCommonMultiple(this->avs->fill_x, this->acs->fill_x);
1630  if (this->inf->fill_x > 0 && (this->fill_x == 0 || this->fill_x > this->inf->fill_x)) this->fill_x = this->inf->fill_x;
1631 
1632  this->fill_y = this->avs->fill_y;
1633  if (this->acs->fill_y > 0 && (this->fill_y == 0 || this->fill_y > this->acs->fill_y)) this->fill_y = this->acs->fill_y;
1634  this->fill_y = LeastCommonMultiple(this->fill_y, this->inf->fill_y);
1635 
1636  /* Resizing. */
1637  this->resize_x = LeastCommonMultiple(this->avs->resize_x, this->acs->resize_x);
1638  if (this->inf->resize_x > 0 && (this->resize_x == 0 || this->resize_x > this->inf->resize_x)) this->resize_x = this->inf->resize_x;
1639 
1640  this->resize_y = this->avs->resize_y;
1641  if (this->acs->resize_y > 0 && (this->resize_y == 0 || this->resize_y > this->acs->resize_y)) this->resize_y = this->acs->resize_y;
1642  this->resize_y = LeastCommonMultiple(this->resize_y, this->inf->resize_y);
1643 
1644  /* Make sure the height suits the 3 column (resp. not-editable) format; the 2 column format can easily fill space between the lists */
1645  this->smallest_y = ComputeMaxSize(min_acs_height, this->smallest_y + this->resize_y - 1, this->resize_y);
1646  }
1647 
1648  void AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl) override
1649  {
1650  this->StoreSizePosition(sizing, x, y, given_width, given_height);
1651 
1652  uint min_avs_width = this->avs->smallest_x + this->avs->padding_left + this->avs->padding_right;
1653  uint min_acs_width = this->acs->smallest_x + this->acs->padding_left + this->acs->padding_right;
1654  uint min_inf_width = this->inf->smallest_x + this->inf->padding_left + this->inf->padding_right;
1655 
1656  uint min_list_width = max(min_avs_width, min_acs_width); // Smallest width of the lists such that they have equal width (incl padding).
1657  uint avs_extra_width = min_list_width - min_avs_width; // Additional width needed for avs to reach min_list_width.
1658  uint acs_extra_width = min_list_width - min_acs_width; // Additional width needed for acs to reach min_list_width.
1659 
1660  /* Use 2 or 3 columns? */
1661  uint min_three_columns = min_avs_width + min_acs_width + min_inf_width + 2 * INTER_COLUMN_SPACING;
1662  uint min_two_columns = min_list_width + min_inf_width + INTER_COLUMN_SPACING;
1663  bool use_three_columns = this->editable && (min_three_columns + MIN_EXTRA_FOR_3_COLUMNS <= given_width);
1664 
1665  /* Info panel is a separate column in both modes. Compute its width first. */
1666  uint extra_width, inf_width;
1667  if (use_three_columns) {
1668  extra_width = given_width - min_three_columns;
1669  inf_width = min(MAX_EXTRA_INFO_WIDTH, extra_width / 2);
1670  } else {
1671  extra_width = given_width - min_two_columns;
1672  inf_width = min(MAX_EXTRA_INFO_WIDTH, extra_width / 2);
1673  }
1674  inf_width = ComputeMaxSize(this->inf->smallest_x, this->inf->smallest_x + inf_width, this->inf->GetHorizontalStepSize(sizing));
1675  extra_width -= inf_width - this->inf->smallest_x;
1676 
1677  uint inf_height = ComputeMaxSize(this->inf->smallest_y, given_height, this->inf->GetVerticalStepSize(sizing));
1678 
1679  if (use_three_columns) {
1680  /* Three column display, first make both lists equally wide, then divide whatever is left between both lists.
1681  * Only keep track of what avs gets, all other space goes to acs. */
1682  uint avs_width = min(avs_extra_width, extra_width);
1683  extra_width -= avs_width;
1684  extra_width -= min(acs_extra_width, extra_width);
1685  avs_width += extra_width / 2;
1686 
1687  avs_width = ComputeMaxSize(this->avs->smallest_x, this->avs->smallest_x + avs_width, this->avs->GetHorizontalStepSize(sizing));
1688 
1689  uint acs_width = given_width - // Remaining space, including horizontal padding.
1690  inf_width - this->inf->padding_left - this->inf->padding_right -
1691  avs_width - this->avs->padding_left - this->avs->padding_right - 2 * INTER_COLUMN_SPACING;
1692  acs_width = ComputeMaxSize(min_acs_width, acs_width, this->acs->GetHorizontalStepSize(sizing)) -
1693  this->acs->padding_left - this->acs->padding_right;
1694 
1695  /* Never use fill_y on these; the minimal size is chosen, so that the 3 column view looks nice */
1696  uint avs_height = ComputeMaxSize(this->avs->smallest_y, given_height, this->avs->resize_y);
1697  uint acs_height = ComputeMaxSize(this->acs->smallest_y, given_height, this->acs->resize_y);
1698 
1699  /* Assign size and position to the children. */
1700  if (rtl) {
1701  x += this->inf->padding_left;
1702  this->inf->AssignSizePosition(sizing, x, y + this->inf->padding_top, inf_width, inf_height, rtl);
1703  x += inf_width + this->inf->padding_right + INTER_COLUMN_SPACING;
1704  } else {
1705  x += this->avs->padding_left;
1706  this->avs->AssignSizePosition(sizing, x, y + this->avs->padding_top, avs_width, avs_height, rtl);
1707  x += avs_width + this->avs->padding_right + INTER_COLUMN_SPACING;
1708  }
1709 
1710  x += this->acs->padding_left;
1711  this->acs->AssignSizePosition(sizing, x, y + this->acs->padding_top, acs_width, acs_height, rtl);
1712  x += acs_width + this->acs->padding_right + INTER_COLUMN_SPACING;
1713 
1714  if (rtl) {
1715  x += this->avs->padding_left;
1716  this->avs->AssignSizePosition(sizing, x, y + this->avs->padding_top, avs_width, avs_height, rtl);
1717  } else {
1718  x += this->inf->padding_left;
1719  this->inf->AssignSizePosition(sizing, x, y + this->inf->padding_top, inf_width, inf_height, rtl);
1720  }
1721  } else {
1722  /* Two columns, all space in extra_width goes to both lists. Since the lists are underneath each other,
1723  * the column is min_list_width wide at least. */
1724  uint avs_width = ComputeMaxSize(this->avs->smallest_x, this->avs->smallest_x + avs_extra_width + extra_width,
1725  this->avs->GetHorizontalStepSize(sizing));
1726  uint acs_width = ComputeMaxSize(this->acs->smallest_x, this->acs->smallest_x + acs_extra_width + extra_width,
1727  this->acs->GetHorizontalStepSize(sizing));
1728 
1729  uint min_avs_height = (!this->editable) ? 0 : this->avs->smallest_y + this->avs->padding_top + this->avs->padding_bottom + INTER_LIST_SPACING;
1730  uint min_acs_height = this->acs->smallest_y + this->acs->padding_top + this->acs->padding_bottom;
1731  uint extra_height = given_height - min_acs_height - min_avs_height;
1732 
1733  /* Never use fill_y on these; instead use the INTER_LIST_SPACING as filler */
1734  uint avs_height = ComputeMaxSize(this->avs->smallest_y, this->avs->smallest_y + extra_height / 2, this->avs->resize_y);
1735  if (this->editable) extra_height -= avs_height - this->avs->smallest_y;
1736  uint acs_height = ComputeMaxSize(this->acs->smallest_y, this->acs->smallest_y + extra_height, this->acs->resize_y);
1737 
1738  /* Assign size and position to the children. */
1739  if (rtl) {
1740  x += this->inf->padding_left;
1741  this->inf->AssignSizePosition(sizing, x, y + this->inf->padding_top, inf_width, inf_height, rtl);
1742  x += inf_width + this->inf->padding_right + INTER_COLUMN_SPACING;
1743 
1744  this->acs->AssignSizePosition(sizing, x + this->acs->padding_left, y + this->acs->padding_top, acs_width, acs_height, rtl);
1745  if (this->editable) {
1746  this->avs->AssignSizePosition(sizing, x + this->avs->padding_left, y + given_height - avs_height - this->avs->padding_bottom, avs_width, avs_height, rtl);
1747  } else {
1748  this->avs->AssignSizePosition(sizing, 0, 0, this->avs->smallest_x, this->avs->smallest_y, rtl);
1749  }
1750  } else {
1751  this->acs->AssignSizePosition(sizing, x + this->acs->padding_left, y + this->acs->padding_top, acs_width, acs_height, rtl);
1752  if (this->editable) {
1753  this->avs->AssignSizePosition(sizing, x + this->avs->padding_left, y + given_height - avs_height - this->avs->padding_bottom, avs_width, avs_height, rtl);
1754  } else {
1755  this->avs->AssignSizePosition(sizing, 0, 0, this->avs->smallest_x, this->avs->smallest_y, rtl);
1756  }
1757  uint dx = this->acs->current_x + this->acs->padding_left + this->acs->padding_right;
1758  if (this->editable) {
1759  dx = max(dx, this->avs->current_x + this->avs->padding_left + this->avs->padding_right);
1760  }
1761  x += dx + INTER_COLUMN_SPACING + this->inf->padding_left;
1762  this->inf->AssignSizePosition(sizing, x, y + this->inf->padding_top, inf_width, inf_height, rtl);
1763  }
1764  }
1765  }
1766 
1767  NWidgetCore *GetWidgetFromPos(int x, int y) override
1768  {
1769  if (!IsInsideBS(x, this->pos_x, this->current_x) || !IsInsideBS(y, this->pos_y, this->current_y)) return nullptr;
1770 
1771  NWidgetCore *nw = (this->editable) ? this->avs->GetWidgetFromPos(x, y) : nullptr;
1772  if (nw == nullptr) nw = this->acs->GetWidgetFromPos(x, y);
1773  if (nw == nullptr) nw = this->inf->GetWidgetFromPos(x, y);
1774  return nw;
1775  }
1776 
1777  void Draw(const Window *w) override
1778  {
1779  if (this->editable) this->avs->Draw(w);
1780  this->acs->Draw(w);
1781  this->inf->Draw(w);
1782  }
1783 };
1784 
1789 
1790 static const NWidgetPart _nested_newgrf_actives_widgets[] = {
1791  /* Left side, presets. */
1793  NWidget(WWT_TEXT, COLOUR_MAUVE), SetDataTip(STR_NEWGRF_SETTINGS_SELECT_PRESET, STR_NULL),
1794  SetPadding(0, WD_FRAMETEXT_RIGHT, 0, 0),
1795  NWidget(WWT_DROPDOWN, COLOUR_YELLOW, WID_NS_PRESET_LIST), SetFill(1, 0), SetResize(1, 0),
1796  SetDataTip(STR_JUST_STRING, STR_NEWGRF_SETTINGS_PRESET_LIST_TOOLTIP),
1797  EndContainer(),
1799  NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_PRESET_SAVE), SetFill(1, 0), SetResize(1, 0),
1800  SetDataTip(STR_NEWGRF_SETTINGS_PRESET_SAVE, STR_NEWGRF_SETTINGS_PRESET_SAVE_TOOLTIP),
1801  NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_PRESET_DELETE), SetFill(1, 0), SetResize(1, 0),
1802  SetDataTip(STR_NEWGRF_SETTINGS_PRESET_DELETE, STR_NEWGRF_SETTINGS_PRESET_DELETE_TOOLTIP),
1803  EndContainer(),
1804 
1806  NWidget(WWT_PANEL, COLOUR_MAUVE),
1807  NWidget(WWT_LABEL, COLOUR_MAUVE), SetDataTip(STR_NEWGRF_SETTINGS_ACTIVE_LIST, STR_NULL),
1809  /* Left side, active grfs. */
1810  NWidget(NWID_HORIZONTAL), SetPadding(0, 2, 0, 2),
1811  NWidget(WWT_PANEL, COLOUR_MAUVE),
1812  NWidget(WWT_INSET, COLOUR_MAUVE, WID_NS_FILE_LIST), SetMinimalSize(100, 1), SetPadding(2, 2, 2, 2),
1813  SetFill(1, 1), SetResize(1, 1), SetScrollbar(WID_NS_SCROLLBAR), SetDataTip(STR_NULL, STR_NEWGRF_SETTINGS_FILE_TOOLTIP),
1814  EndContainer(),
1815  EndContainer(),
1816  NWidget(NWID_VSCROLLBAR, COLOUR_MAUVE, WID_NS_SCROLLBAR),
1817  EndContainer(),
1818  /* Buttons. */
1819  NWidget(NWID_SELECTION, INVALID_COLOUR, WID_NS_SHOW_REMOVE),
1821  NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_REMOVE), SetFill(1, 0), SetResize(1, 0),
1822  SetDataTip(STR_NEWGRF_SETTINGS_REMOVE, STR_NEWGRF_SETTINGS_REMOVE_TOOLTIP),
1824  NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_MOVE_UP), SetFill(1, 0), SetResize(1, 0),
1825  SetDataTip(STR_NEWGRF_SETTINGS_MOVEUP, STR_NEWGRF_SETTINGS_MOVEUP_TOOLTIP),
1826  NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_MOVE_DOWN), SetFill(1, 0), SetResize(1, 0),
1827  SetDataTip(STR_NEWGRF_SETTINGS_MOVEDOWN, STR_NEWGRF_SETTINGS_MOVEDOWN_TOOLTIP),
1828  EndContainer(),
1829  NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_UPGRADE), SetFill(1, 0), SetResize(1, 0),
1830  SetDataTip(STR_NEWGRF_SETTINGS_UPGRADE, STR_NEWGRF_SETTINGS_UPGRADE_TOOLTIP),
1831  EndContainer(),
1832 
1834  NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_RESCAN_FILES2), SetFill(1, 0), SetResize(1, 0),
1835  SetDataTip(STR_NEWGRF_SETTINGS_RESCAN_FILES, STR_NEWGRF_SETTINGS_RESCAN_FILES_TOOLTIP),
1836  NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_CONTENT_DOWNLOAD2), SetFill(1, 0), SetResize(1, 0),
1837  SetDataTip(STR_INTRO_ONLINE_CONTENT, STR_INTRO_TOOLTIP_ONLINE_CONTENT),
1838  EndContainer(),
1839  EndContainer(),
1840  EndContainer(),
1841 };
1842 
1843 static const NWidgetPart _nested_newgrf_availables_widgets[] = {
1844  NWidget(WWT_PANEL, COLOUR_MAUVE),
1845  NWidget(WWT_LABEL, COLOUR_MAUVE), SetDataTip(STR_NEWGRF_SETTINGS_INACTIVE_LIST, STR_NULL),
1847  /* Left side, available grfs, filter edit box. */
1850  NWidget(WWT_TEXT, COLOUR_MAUVE), SetFill(0, 1), SetDataTip(STR_NEWGRF_FILTER_TITLE, STR_NULL),
1851  NWidget(WWT_EDITBOX, COLOUR_MAUVE, WID_NS_FILTER), SetFill(1, 0), SetMinimalSize(50, 12), SetResize(1, 0),
1852  SetDataTip(STR_LIST_FILTER_OSKTITLE, STR_LIST_FILTER_TOOLTIP),
1853  EndContainer(),
1854  /* Left side, available grfs. */
1855  NWidget(NWID_HORIZONTAL), SetPadding(0, 2, 0, 2),
1856  NWidget(WWT_PANEL, COLOUR_MAUVE),
1857  NWidget(WWT_INSET, COLOUR_MAUVE, WID_NS_AVAIL_LIST), SetMinimalSize(100, 1), SetPadding(2, 2, 2, 2),
1859  EndContainer(),
1860  EndContainer(),
1861  NWidget(NWID_VSCROLLBAR, COLOUR_MAUVE, WID_NS_SCROLL2BAR),
1862  EndContainer(),
1863  /* Left side, available grfs, buttons. */
1865  NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_ADD), SetFill(1, 0), SetResize(1, 0),
1866  SetDataTip(STR_NEWGRF_SETTINGS_ADD, STR_NEWGRF_SETTINGS_ADD_FILE_TOOLTIP),
1868  NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_RESCAN_FILES), SetFill(1, 0), SetResize(1, 0),
1869  SetDataTip(STR_NEWGRF_SETTINGS_RESCAN_FILES, STR_NEWGRF_SETTINGS_RESCAN_FILES_TOOLTIP),
1870  NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_CONTENT_DOWNLOAD), SetFill(1, 0), SetResize(1, 0),
1871  SetDataTip(STR_INTRO_ONLINE_CONTENT, STR_INTRO_TOOLTIP_ONLINE_CONTENT),
1872  EndContainer(),
1873  EndContainer(),
1874  EndContainer(),
1875 };
1876 
1877 static const NWidgetPart _nested_newgrf_infopanel_widgets[] = {
1878  /* Right side, info panel. */
1879  NWidget(NWID_VERTICAL), SetPadding(0, 0, 2, 0),
1880  NWidget(WWT_PANEL, COLOUR_MAUVE), SetPadding(0, 0, 2, 0),
1881  NWidget(WWT_EMPTY, COLOUR_MAUVE, WID_NS_NEWGRF_INFO_TITLE), SetFill(1, 0), SetResize(1, 0),
1882  NWidget(WWT_EMPTY, COLOUR_MAUVE, WID_NS_NEWGRF_INFO), SetFill(1, 1), SetResize(1, 1), SetMinimalSize(150, 100),
1883  EndContainer(),
1884  NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_OPEN_URL), SetFill(1, 0), SetResize(1, 0),
1885  SetDataTip(STR_CONTENT_OPEN_URL, STR_CONTENT_OPEN_URL_TOOLTIP),
1886  NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_NEWGRF_TEXTFILE + TFT_README), SetFill(1, 0), SetResize(1, 0),
1887  SetDataTip(STR_TEXTFILE_VIEW_README, STR_NULL),
1890  SetDataTip(STR_TEXTFILE_VIEW_CHANGELOG, STR_NULL),
1891  NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_NEWGRF_TEXTFILE + TFT_LICENSE), SetFill(1, 0), SetResize(1, 0),
1892  SetDataTip(STR_TEXTFILE_VIEW_LICENCE, STR_NULL),
1893  EndContainer(),
1894  EndContainer(),
1895  NWidget(NWID_SELECTION, INVALID_COLOUR, WID_NS_SHOW_APPLY),
1896  /* Right side, buttons. */
1899  NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_SET_PARAMETERS), SetFill(1, 0), SetResize(1, 0),
1900  SetDataTip(STR_NEWGRF_SETTINGS_SET_PARAMETERS, STR_NULL),
1901  NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_TOGGLE_PALETTE), SetFill(1, 0), SetResize(1, 0),
1902  SetDataTip(STR_NEWGRF_SETTINGS_TOGGLE_PALETTE, STR_NEWGRF_SETTINGS_TOGGLE_PALETTE_TOOLTIP),
1903  EndContainer(),
1904  NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_APPLY_CHANGES), SetFill(1, 0), SetResize(1, 0),
1905  SetDataTip(STR_NEWGRF_SETTINGS_APPLY_CHANGES, STR_NULL),
1906  EndContainer(),
1907  NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_VIEW_PARAMETERS), SetFill(1, 0), SetResize(1, 0),
1908  SetDataTip(STR_NEWGRF_SETTINGS_SHOW_PARAMETERS, STR_NULL),
1909  EndContainer(),
1910 };
1911 
1913 NWidgetBase* NewGRFDisplay(int *biggest_index)
1914 {
1915  NWidgetBase *avs = MakeNWidgets(_nested_newgrf_availables_widgets, lengthof(_nested_newgrf_availables_widgets), biggest_index, nullptr);
1916 
1917  int biggest2;
1918  NWidgetBase *acs = MakeNWidgets(_nested_newgrf_actives_widgets, lengthof(_nested_newgrf_actives_widgets), &biggest2, nullptr);
1919  *biggest_index = max(*biggest_index, biggest2);
1920 
1921  NWidgetBase *inf = MakeNWidgets(_nested_newgrf_infopanel_widgets, lengthof(_nested_newgrf_infopanel_widgets), &biggest2, nullptr);
1922  *biggest_index = max(*biggest_index, biggest2);
1923 
1924  return new NWidgetNewGRFDisplay(avs, acs, inf);
1925 }
1926 
1927 /* Widget definition of the manage newgrfs window */
1928 static const NWidgetPart _nested_newgrf_widgets[] = {
1930  NWidget(WWT_CLOSEBOX, COLOUR_MAUVE),
1931  NWidget(WWT_CAPTION, COLOUR_MAUVE), SetDataTip(STR_NEWGRF_SETTINGS_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
1932  NWidget(WWT_DEFSIZEBOX, COLOUR_MAUVE),
1933  EndContainer(),
1934  NWidget(WWT_PANEL, COLOUR_MAUVE),
1936  /* Resize button. */
1938  NWidget(NWID_SPACER), SetFill(1, 0), SetResize(1, 0),
1939  NWidget(WWT_RESIZEBOX, COLOUR_MAUVE),
1940  EndContainer(),
1941  EndContainer(),
1942 };
1943 
1944 /* Window definition of the manage newgrfs window */
1945 static WindowDesc _newgrf_desc(
1946  WDP_CENTER, "settings_newgrf", 300, 263,
1948  0,
1949  _nested_newgrf_widgets, lengthof(_nested_newgrf_widgets)
1950 );
1951 
1957 static void NewGRFConfirmationCallback(Window *w, bool confirmed)
1958 {
1959  if (confirmed) {
1962  NewGRFWindow *nw = dynamic_cast<NewGRFWindow*>(w);
1963 
1965  GamelogGRFUpdate(_grfconfig, nw->actives); // log GRF changes
1966  CopyGRFConfigList(nw->orig_list, nw->actives, false);
1967  ReloadNewGRFData();
1969 
1970  /* Show new, updated list */
1971  GRFConfig *c;
1972  int i = 0;
1973  for (c = nw->actives; c != nullptr && c != nw->active_sel; c = c->next, i++) {}
1974  CopyGRFConfigList(&nw->actives, *nw->orig_list, false);
1975  for (c = nw->actives; c != nullptr && i > 0; c = c->next, i--) {}
1976  nw->active_sel = c;
1977  nw->avails.ForceRebuild();
1978 
1979  w->InvalidateData();
1980 
1981  ReInitAllWindows();
1983  }
1984 }
1985 
1986 
1987 
1996 void ShowNewGRFSettings(bool editable, bool show_params, bool exec_changes, GRFConfig **config)
1997 {
1999  new NewGRFWindow(&_newgrf_desc, editable, show_params, exec_changes, config);
2000 }
2001 
2005  NWidget(WWT_CLOSEBOX, COLOUR_GREY),
2006  NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_SAVE_PRESET_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
2007  NWidget(WWT_DEFSIZEBOX, COLOUR_GREY),
2008  EndContainer(),
2009  NWidget(WWT_PANEL, COLOUR_GREY),
2011  NWidget(WWT_INSET, COLOUR_GREY, WID_SVP_PRESET_LIST), SetPadding(2, 1, 0, 2),
2012  SetDataTip(0x0, STR_SAVE_PRESET_LIST_TOOLTIP), SetResize(1, 10), SetScrollbar(WID_SVP_SCROLLBAR), EndContainer(),
2013  NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_SVP_SCROLLBAR),
2014  EndContainer(),
2015  NWidget(WWT_EDITBOX, COLOUR_GREY, WID_SVP_EDITBOX), SetPadding(3, 2, 2, 2), SetFill(1, 0), SetResize(1, 0),
2016  SetDataTip(STR_SAVE_PRESET_TITLE, STR_SAVE_PRESET_EDITBOX_TOOLTIP),
2017  EndContainer(),
2019  NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SVP_CANCEL), SetDataTip(STR_SAVE_PRESET_CANCEL, STR_SAVE_PRESET_CANCEL_TOOLTIP), SetFill(1, 0), SetResize(1, 0),
2020  NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SVP_SAVE), SetDataTip(STR_SAVE_PRESET_SAVE, STR_SAVE_PRESET_SAVE_TOOLTIP), SetFill(1, 0), SetResize(1, 0),
2021  NWidget(WWT_RESIZEBOX, COLOUR_GREY),
2022  EndContainer(),
2023 };
2024 
2027  WDP_CENTER, "save_preset", 140, 110,
2029  WDF_MODAL,
2030  _nested_save_preset_widgets, lengthof(_nested_save_preset_widgets)
2031 );
2032 
2034 struct SavePresetWindow : public Window {
2038  int selected;
2039 
2044  SavePresetWindow(const char *initial_text) : Window(&_save_preset_desc), presetname_editbox(32)
2045  {
2046  this->presets = GetGRFPresetList();
2047  this->selected = -1;
2048  if (initial_text != nullptr) {
2049  for (uint i = 0; i < this->presets.size(); i++) {
2050  if (this->presets[i] == initial_text) {
2051  this->selected = i;
2052  break;
2053  }
2054  }
2055  }
2056 
2057  this->querystrings[WID_SVP_EDITBOX] = &this->presetname_editbox;
2058  this->presetname_editbox.ok_button = WID_SVP_SAVE;
2059  this->presetname_editbox.cancel_button = WID_SVP_CANCEL;
2060 
2061  this->CreateNestedTree();
2062  this->vscroll = this->GetScrollbar(WID_SVP_SCROLLBAR);
2063  this->FinishInitNested(0);
2064 
2065  this->vscroll->SetCount((uint)this->presets.size());
2067  if (initial_text != nullptr) this->presetname_editbox.text.Assign(initial_text);
2068  }
2069 
2070  ~SavePresetWindow()
2071  {
2072  }
2073 
2074  void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
2075  {
2076  switch (widget) {
2077  case WID_SVP_PRESET_LIST: {
2078  resize->height = FONT_HEIGHT_NORMAL + 2U;
2079  size->height = 0;
2080  for (uint i = 0; i < this->presets.size(); i++) {
2081  Dimension d = GetStringBoundingBox(this->presets[i].c_str());
2082  size->width = max(size->width, d.width + WD_FRAMETEXT_LEFT + WD_FRAMETEXT_RIGHT);
2083  resize->height = max(resize->height, d.height);
2084  }
2085  size->height = ClampU((uint)this->presets.size(), 5, 20) * resize->height + 1;
2086  break;
2087  }
2088  }
2089  }
2090 
2091  void DrawWidget(const Rect &r, int widget) const override
2092  {
2093  switch (widget) {
2094  case WID_SVP_PRESET_LIST: {
2095  GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, PC_BLACK);
2096 
2097  uint step_height = this->GetWidget<NWidgetBase>(WID_SVP_PRESET_LIST)->resize_y;
2098  int offset_y = (step_height - FONT_HEIGHT_NORMAL) / 2;
2099  uint y = r.top + WD_FRAMERECT_TOP;
2100  uint min_index = this->vscroll->GetPosition();
2101  uint max_index = min(min_index + this->vscroll->GetCapacity(), (uint)this->presets.size());
2102 
2103  for (uint i = min_index; i < max_index; i++) {
2104  if ((int)i == this->selected) GfxFillRect(r.left + 1, y, r.right - 1, y + step_height - 2, PC_DARK_BLUE);
2105 
2106  const char *text = this->presets[i].c_str();
2107  DrawString(r.left + WD_FRAMERECT_LEFT, r.right, y + offset_y, text, ((int)i == this->selected) ? TC_WHITE : TC_SILVER);
2108  y += step_height;
2109  }
2110  break;
2111  }
2112  }
2113  }
2114 
2115  void OnClick(Point pt, int widget, int click_count) override
2116  {
2117  switch (widget) {
2118  case WID_SVP_PRESET_LIST: {
2119  uint row = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_SVP_PRESET_LIST);
2120  if (row < this->presets.size()) {
2121  this->selected = row;
2122  this->presetname_editbox.text.Assign(this->presets[row].c_str());
2125  }
2126  break;
2127  }
2128 
2129  case WID_SVP_CANCEL:
2130  delete this;
2131  break;
2132 
2133  case WID_SVP_SAVE: {
2135  if (w != nullptr && !StrEmpty(this->presetname_editbox.text.buf)) w->OnQueryTextFinished(this->presetname_editbox.text.buf);
2136  delete this;
2137  break;
2138  }
2139  }
2140  }
2141 
2142  void OnResize() override
2143  {
2144  this->vscroll->SetCapacityFromWidget(this, WID_SVP_PRESET_LIST);
2145  }
2146 };
2147 
2152 static void ShowSavePresetWindow(const char *initial_text)
2153 {
2155  new SavePresetWindow(initial_text);
2156 }
2157 
2160  NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_NEWGRF_SCAN_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
2161  NWidget(WWT_PANEL, COLOUR_GREY),
2162  NWidget(NWID_HORIZONTAL), SetPIP(20, 0, 20),
2163  NWidget(NWID_VERTICAL), SetPIP(11, 8, 11),
2164  NWidget(WWT_LABEL, INVALID_COLOUR), SetDataTip(STR_NEWGRF_SCAN_MESSAGE, STR_NULL), SetFill(1, 0),
2165  NWidget(WWT_EMPTY, INVALID_COLOUR, WID_SP_PROGRESS_BAR), SetFill(1, 0),
2166  NWidget(WWT_EMPTY, INVALID_COLOUR, WID_SP_PROGRESS_TEXT), SetFill(1, 0),
2167  EndContainer(),
2168  EndContainer(),
2169  EndContainer(),
2170 };
2171 
2174  WDP_CENTER, nullptr, 0, 0,
2176  0,
2177  _nested_scan_progress_widgets, lengthof(_nested_scan_progress_widgets)
2178 );
2179 
2181 struct ScanProgressWindow : public Window {
2182  char *last_name;
2183  int scanned;
2184 
2186  ScanProgressWindow() : Window(&_scan_progress_desc), last_name(nullptr), scanned(0)
2187  {
2188  this->InitNested(1);
2189  }
2190 
2193  {
2194  free(last_name);
2195  }
2196 
2197  void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
2198  {
2199  switch (widget) {
2200  case WID_SP_PROGRESS_BAR: {
2201  SetDParamMaxValue(0, 100);
2202  *size = GetStringBoundingBox(STR_GENERATION_PROGRESS);
2203  /* We need some spacing for the 'border' */
2204  size->height += 8;
2205  size->width += 8;
2206  break;
2207  }
2208 
2209  case WID_SP_PROGRESS_TEXT:
2210  SetDParamMaxDigits(0, 4);
2211  SetDParamMaxDigits(1, 4);
2212  /* We really don't know the width. We could determine it by scanning the NewGRFs,
2213  * but this is the status window for scanning them... */
2214  size->width = max(400U, GetStringBoundingBox(STR_NEWGRF_SCAN_STATUS).width);
2215  size->height = FONT_HEIGHT_NORMAL * 2 + WD_PAR_VSEP_NORMAL;
2216  break;
2217  }
2218  }
2219 
2220  void DrawWidget(const Rect &r, int widget) const override
2221  {
2222  switch (widget) {
2223  case WID_SP_PROGRESS_BAR: {
2224  /* Draw the % complete with a bar and a text */
2225  DrawFrameRect(r.left, r.top, r.right, r.bottom, COLOUR_GREY, FR_BORDERONLY);
2226  uint percent = scanned * 100 / max(1U, _settings_client.gui.last_newgrf_count);
2227  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);
2228  SetDParam(0, percent);
2229  DrawString(r.left, r.right, r.top + 5, STR_GENERATION_PROGRESS, TC_FROMSTRING, SA_HOR_CENTER);
2230  break;
2231  }
2232 
2233  case WID_SP_PROGRESS_TEXT:
2234  SetDParam(0, this->scanned);
2236  DrawString(r.left, r.right, r.top, STR_NEWGRF_SCAN_STATUS, TC_FROMSTRING, SA_HOR_CENTER);
2237 
2238  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);
2239  break;
2240  }
2241  }
2242 
2248  void UpdateNewGRFScanStatus(uint num, const char *name)
2249  {
2250  free(this->last_name);
2251  if (name == nullptr) {
2252  char buf[256];
2253  GetString(buf, STR_NEWGRF_SCAN_ARCHIVES, lastof(buf));
2254  this->last_name = stredup(buf);
2255  } else {
2256  this->last_name = stredup(name);
2257  }
2258  this->scanned = num;
2260 
2261  this->SetDirty();
2262  }
2263 };
2264 
2270 void UpdateNewGRFScanStatus(uint num, const char *name)
2271 {
2273  if (w == nullptr) w = new ScanProgressWindow();
2274  w->UpdateNewGRFScanStatus(num, name);
2275 }
EventState
State of handling an event.
Definition: window_type.h:713
StringList presets
Available presets.
Functions related to OTTD&#39;s strings.
virtual void OnQueryTextFinished(char *str)
The query window opened from this window has closed.
Definition: window_gui.h:741
Title for Info on selected NewGRF.
Definition: newgrf_widget.h:48
Empty widget, place holder to reserve space in widget array.
Definition: widget_type.h:48
SizingType
Different forms of sizing nested widgets, using NWidgetBase::AssignSizePosition() ...
Definition: widget_type.h:111
Base types for having sorted lists in GUIs.
uint32 PaletteID
The number of the palette.
Definition: gfx_type.h:20
void RebuildDone()
Notify the sortlist that the rebuild is done.
QueryString filter_editbox
Filter editbox;.
Definition: newgrf_gui.cpp:604
Button to increase number of parameters.
Definition: newgrf_widget.h:22
void OnMouseDrag(Point pt, int widget) override
An &#39;object&#39; is being dragged at the provided position, highlight the target if possible.
SmallMap< uint32, struct GRFText * > value_names
Names for each value.
void SetValue(struct GRFConfig *config, uint32 value)
Set the value of this user-changeable parameter in the given config.
NWidgetBase * acs
Widget with the active grfs list and buttons.
static const NWidgetPart _nested_save_preset_widgets[]
Widget parts of the save preset window.
void OnResize() override
Called after the window got resized.
Definition: newgrf_gui.cpp:769
Horizontally center the text.
Definition: gfx_func.h:97
ResizeInfo resize
Resize information.
Definition: window_gui.h:324
bool editable
Allow editing parameters.
Definition: newgrf_gui.cpp:159
uint32 param_value[2]
Values of GRF parameters to show for message and custom_message.
void ScrollTowards(int position)
Scroll towards the given position; if the item is visible nothing happens, otherwise it will be shown...
Definition: widget_type.h:731
GRFConfig ** orig_list
List active grfs in the game. Used as initial value, may be updated by the window.
Definition: newgrf_gui.cpp:611
static NWidgetPart SetResize(int16 dx, int16 dy)
Widget part function for setting the resize step.
Definition: widget_type.h:930
List with available preset names.
Definition: newgrf_widget.h:66
uint32 unique_id
Unique ID; either GRF ID or shortname.
Definition: tcp_content.h:75
void GfxFillRect(int left, int top, int right, int bottom, int colour, FillRectMode mode)
Applies a certain FillRectMode-operation to a rectangle [left, right] x [top, bottom] on the screen...
Definition: gfx.cpp:112
void SetWidgetDisabledState(byte widget_index, bool disab_stat)
Sets the enabled/disabled status of a widget.
Definition: window_gui.h:394
The parameter allows a range of numbers, each of which can have a special name.
Reset button.
Definition: newgrf_widget.h:28
NWidgetBase * NewGRFDisplay(int *biggest_index)
Construct nested container widget for managing the lists and the info panel of the NewGRF GUI...
void SetWindowDirty(WindowClass cls, WindowNumber number)
Mark window as dirty (in need of repainting)
Definition: window.cpp:3199
GRFConfig * _grfconfig
First item in list of current GRF set up.
static const uint INTER_COLUMN_SPACING
Empty horizontal space between two columns.
static NWidgetPart NWidgetFunction(NWidgetFunctionType *func_ptr)
Obtain a nested widget (sub)tree from an external source.
Definition: widget_type.h:1146
uint resize_x
Horizontal resize step (0 means not resizable).
Definition: widget_type.h:166
High level window description.
Definition: window_gui.h:168
static bool CDECL TagNameFilter(const GRFConfig *const *a, StringFilter &filter)
Filter grfs by tags/name.
NewGRFs were just rescanned.
Definition: window_type.h:702
void SetParameterDefaults()
Set the default value for all parameters as specified by action14.
void SaveGRFPresetToConfig(const char *config_name, GRFConfig *config)
Save a NewGRF configuration with a preset name.
Definition: settings.cpp:1816
Panel for Info on selected NewGRF.
Definition: newgrf_widget.h:49
bool Filter(FilterFunction *decide, F filter_data)
Filter the list.
void DrawWidgets() const
Paint all widgets of a window.
Definition: widget.cpp:604
static GRFParameterInfo * GetDummyParameterInfo(uint nr)
Get a dummy parameter-info object with default information.
Definition: newgrf_gui.cpp:187
Baseclass for container widgets.
Definition: widget_type.h:368
Upgrade NewGRFs that have a newer version available.
Definition: newgrf_widget.h:42
Centered label.
Definition: widget_type.h:57
void OnResize() override
Called after the window got resized.
Scrollbar data structure.
Definition: widget_type.h:589
void SetupSmallestSize(Window *w, bool init_array) override
Compute smallest size needed by the widget.
void ClearGRFConfigList(GRFConfig **config)
Clear a GRF Config list, freeing all nodes.
GRFParameterType type
The type of this parameter.
int CDECL seprintf(char *str, const char *last, const char *format,...)
Safer implementation of snprintf; same as snprintf except:
Definition: string.cpp:409
Optional number of parameters.
Definition: newgrf_widget.h:23
void OnDropdownSelect(int widget, int index) override
A dropdown option associated to this window has been selected.
Apply changes to NewGRF config.
Definition: newgrf_widget.h:55
GUIGRFConfigList avails
Available (non-active) grfs.
Definition: newgrf_gui.cpp:600
void SetWidgetDirty(byte widget_index) const
Invalidate a widget, i.e.
Definition: window.cpp:581
Offset at top to draw the frame rectangular area.
Definition: window_gui.h:64
Progress report of landscape generation; Window numbers:
Definition: window_type.h:458
static uint ComputeMaxSize(uint base, uint max_space, uint step)
Return the biggest possible size of a nested widget.
Definition: widget_type.h:798
Normal amount of vertical space between two paragraphs of text.
Definition: window_gui.h:139
textfile; Window numbers:
Definition: window_type.h:182
Horizontal container.
Definition: widget_type.h:75
Window * FindWindowById(WindowClass cls, WindowNumber number)
Find a window by its class and window number.
Definition: window.cpp:1114
void ResetState()
Reset the matching state to process a new item.
The passed event is not handled.
Definition: window_type.h:715
bool Elapsed(uint delta)
Test if a timer has elapsed.
Definition: guitimer_func.h:57
Arrow to the right or in case of RTL to the left.
Definition: widget_type.h:38
Arrow to the left or in case of RTL to the right.
Definition: widget_type.h:37
void CDECL SetWidgetsDisabledState(bool disab_stat, int widgets,...)
Sets the enabled/disabled status of a list of widgets.
Definition: window.cpp:520
void SetSortFuncs(SortFunction *const *n_funcs)
Hand the array of sort function pointers to the sort list.
bool GetState() const
Get the matching state of the current item.
void ShowQueryString(StringID str, StringID caption, uint maxsize, Window *parent, CharSetFilter afilter, QueryStringFlags flags)
Show a query popup window with a textbox in it.
Definition: misc_gui.cpp:1121
Save list of active NewGRFs as presets.
Definition: newgrf_widget.h:36
Functions to be called to log possibly unsafe game events.
The NewGRF prefers a 32 bpp blitter.
Definition: newgrf_config.h:78
Window for showing the progress of NewGRF scanning.
Scrollbar for available NewGRF list.
Definition: newgrf_widget.h:47
std::vector< Pair >::const_iterator Find(const T &key) const
Finds given key in this map.
void GamelogStartAction(GamelogActionType at)
Stores information about new action, but doesn&#39;t allocate it Action is allocated only when there is a...
Definition: gamelog.cpp:71
Remove NewGRF from active list.
Definition: newgrf_widget.h:39
StringFilter string_filter
Filter for available grf.
Definition: newgrf_gui.cpp:603
GRFConfig * LoadGRFPresetFromConfig(const char *config_name)
Load a NewGRF configuration by preset-name.
Definition: settings.cpp:1797
GRFStatus status
NOSAVE: GRFStatus, enum.
void UpdateNewGRFScanStatus(uint num, const char *name)
Update the NewGRF scan status.
static GRFParameterInfo dummy_parameter_info
Dummy info in case a newgrf didn&#39;t provide info about some parameter.
Definition: newgrf_gui.cpp:148
char * md5sumToString(char *buf, const char *last, const uint8 md5sum[16])
Convert the md5sum to a hexadecimal string representation.
Definition: string.cpp:427
a textbox for typing
Definition: widget_type.h:71
Resize box (normally at bottom-right of a window)
Definition: widget_type.h:68
Pressed (inset) panel, most commonly used as combo box text area.
Definition: widget_type.h:51
static const int ACTION_CLEAR
Clear editbox.
static WindowDesc _save_preset_desc(WDP_CENTER, "save_preset", 140, 110, WC_SAVE_PRESET, WC_GAME_OPTIONS, WDF_MODAL, _nested_save_preset_widgets, lengthof(_nested_save_preset_widgets))
Window description of the preset save window.
NewGRF changelog.
Definition: textfile_type.h:20
bool clicked_increase
True if the increase button was clicked, false for the decrease button.
Definition: newgrf_gui.cpp:151
~ScanProgressWindow()
Free the last name buffer.
static void ShowSavePresetWindow(const char *initial_text)
Open the window for saving a preset.
int top
y position of top edge of the window
Definition: window_gui.h:320
GRF file is used statically (can be used in any MP game)
Definition: newgrf_config.h:26
int LeastCommonMultiple(int a, int b)
Compute least common multiple (lcm) of arguments a and b, the smallest integer value that is a multip...
Definition: math_func.cpp:26
uint8 num_valid_params
NOSAVE: Number of valid parameters (action 0x14)
char * custom_message
Custom message (if present)
Move NewGRF up in active list.
Definition: newgrf_widget.h:40
void ShowErrorMessage(StringID summary_msg, StringID detailed_msg, WarningLevel wl, int x=0, int y=0, const GRFFile *textref_stack_grffile=nullptr, uint textref_stack_size=0, const uint32 *textref_stack=nullptr)
Display an error message in a window.
Definition: error_gui.cpp:382
Button to decrease number of parameters.
Definition: newgrf_widget.h:21
std::map< uint32, const GRFConfig * > GrfIdMap
Map of grfid to the grf config.
Definition: newgrf_gui.cpp:568
GRFError * error
NOSAVE: Error/Warning during GRF loading (Action 0x0B)
Close box (at top-left of a window)
Definition: widget_type.h:69
Offset at top of a matrix cell.
Definition: window_gui.h:80
void OnClick(Point pt, int widget, int click_count) override
A click with the left mouse button has been made on the window.
Definition: newgrf_gui.cpp:913
The NewGRF provided no information.
Definition: newgrf_config.h:71
byte param_nr
GRF parameter to store content in.
void OnNewGRFsScanned() override
Called whenever the NewGRF scan completed.
void DrawWidget(const Rect &r, int widget) const override
Draw the contents of a nested widget.
List window of active NewGRFs.
Definition: newgrf_widget.h:44
uint8 original_md5sum[16]
MD5 checksum of original file if only a &#39;compatible&#39; file was loaded.
void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
Update size and resize step of a widget in the window.
String filter and state.
bool show_params
Are the grf-parameters shown in the info-panel?
Definition: newgrf_gui.cpp:613
#define lastof(x)
Get the last element of an fixed size array.
Definition: depend.cpp:50
void OnPaint() override
The window must be repainted.
Definition: newgrf_gui.cpp:307
StringID severity
Info / Warning / Error / Fatal.
GRF file was not found in the local cache.
Definition: newgrf_config.h:38
void SetFilterFuncs(FilterFunction *const *n_funcs)
Hand the array of filter function pointers to the sort list.
static const NWidgetPart _nested_scan_progress_widgets[]
Widgets for the progress window.
SavePresetWindow(const char *initial_text)
Constructor of the save preset window.
void DisableWidget(byte widget_index)
Sets a widget to disabled.
Definition: window_gui.h:404
Open URL of NewGRF.
Definition: newgrf_widget.h:50
const GRFConfig * FindGRFConfig(uint32 grfid, FindGRFConfigMode mode, const uint8 *md5sum, uint32 desired_version)
Find a NewGRF in the scanned list.
NewGRF readme.
Definition: textfile_type.h:19
GRFIdentifier ident
grfid and md5sum to uniquely identify newgrfs
uint smallest_x
Smallest horizontal size of the widget in a filled window.
Definition: widget_type.h:171
NWID_SELECTION to optionally display WID_NP_NUMPAR.
Definition: newgrf_widget.h:20
static T max(const T a, const T b)
Returns the maximum of two values.
Definition: math_func.hpp:26
uint32 last_newgrf_count
the numbers of NewGRFs we found during the last scan
Common string list item.
Definition: dropdown_type.h:41
bool newgrf_show_old_versions
whether to show old versions in the NewGRF list
GRF file is disabled.
Definition: newgrf_config.h:37
void CreateNestedTree(bool fill_nested=true)
Perform the first part of the initialization of a nested widget tree.
Definition: window.cpp:1812
NWidgetContainer * MakeNWidgets(const NWidgetPart *parts, int count, int *biggest_index, NWidgetContainer *container)
Construct a nested widget tree from an array of parts.
Definition: widget.cpp:2792
void SetListing(Listing l)
Import sort conditions.
Pure simple text.
Definition: widget_type.h:58
uint GetHorizontalStepSize(SizingType sizing) const
Get the horizontal sizing step.
Definition: widget_type.h:196
void DrawWidget(const Rect &r, int widget) const override
Draw the contents of a nested widget.
Definition: newgrf_gui.cpp:824
struct GRFConfig * next
NOSAVE: Next item in the linked list.
int selected
Selected entry in the preset list, or -1 if none selected.
Data structure describing what to show in the list (filter criteria).
Definition: sortlist_type.h:38
bool NeedRebuild() const
Check if a rebuild is needed.
static bool IsInsideBS(const T x, const size_t base, const size_t size)
Checks if a value is between a window started at some base point.
Definition: math_func.hpp:250
static uint ClampU(const uint a, const uint min, const uint max)
Clamp an unsigned integer between an interval.
Definition: math_func.hpp:184
A NewGRF preset was picked.
Definition: window_type.h:704
void DrawFrameRect(int left, int top, int right, int bottom, Colours colour, FrameFlags flags)
Draw frame rectangle.
Definition: widget.cpp:177
bool _network_available
is network mode available?
Definition: network.cpp:56
static const uint INTER_LIST_SPACING
Empty vertical space between both lists in the 2 column mode.
void SetCount(int num)
Sets the number of elements in the list.
Definition: widget_type.h:670
The content does not exist in the content system.
Definition: tcp_content.h:63
Partial widget specification to allow NWidgets to be written nested.
Definition: widget_type.h:910
void ForceRebuild()
Force that a rebuild is needed.
NewGRF license.
Definition: textfile_type.h:21
Base for the GUIs that have an edit box in them.
Data structure for an opened window.
Definition: window_gui.h:278
GRFConfig * _all_grfs
First item in list of all scanned NewGRFs.
void FinishInitNested(WindowNumber window_number=0)
Perform the second part of the initialization of a nested widget tree.
Definition: window.cpp:1828
static NWidgetPart SetMatrixDataTip(uint8 cols, uint8 rows, StringID tip)
Widget part function for setting the data and tooltip of WWT_MATRIX widgets.
Definition: widget_type.h:1032
void SetFilterTerm(const char *str)
Set the term to filter on.
static NWidgetPart SetPadding(uint8 top, uint8 right, uint8 bottom, uint8 left)
Widget part function for setting additional space around a widget.
Definition: widget_type.h:1046
static bool IsInsideMM(const T x, const size_t min, const size_t max)
Checks if a value is in an interval.
Definition: math_func.hpp:266
Scrollbar for active NewGRF list.
Definition: newgrf_widget.h:45
void InitNested(WindowNumber number=0)
Perform complete initialization of the Window with nested widgets, to allow use.
Definition: window.cpp:1841
bool clicked_dropdown
Whether the dropdown is open.
Definition: newgrf_gui.cpp:152
dragging items in the depot windows
Critical errors, the MessageBox is shown in all cases.
Definition: error.h:26
struct GRFText * desc
The description of this parameter.
Bottom offset of the text of the frame.
Definition: window_gui.h:75
Header of Action 04 "universal holder" structure and functions.
void SetDParamStr(uint n, const char *str)
This function is used to "bind" a C string to a OpenTTD dparam slot.
Definition: strings.cpp:281
char name[32]
Name of the content.
Definition: tcp_content.h:71
Accept button.
Definition: newgrf_widget.h:27
void ShowMissingContentWindow(const GRFConfig *list)
Show the content list window with all missing grfs from the given list.
GRFConfig * actives
Temporary active grf list to which changes are made.
Definition: newgrf_gui.cpp:608
Only numeric ones.
Definition: string_type.h:30
uint32 GetValue(struct GRFConfig *config) const
Get the value of this user-changeable parameter from the given config.
Invisible widget that takes some space.
Definition: widget_type.h:79
uint8 num_params
Number of used parameters.
static const uint8 PC_DARK_GREY
Dark grey palette colour.
Definition: gfx_func.h:206
int preset
Selected preset or -1 if none selected.
Definition: newgrf_gui.cpp:615
GUI Timers.
Functions related to errors.
Offset at bottom of a matrix cell.
Definition: window_gui.h:81
Default window size box (at top-right of a window, between WWT_SHADEBOX and WWT_STICKYBOX) ...
Definition: widget_type.h:65
static void FillGrfidMap(const GRFConfig *c, GrfIdMap *grfid_map)
Add all grf configs from c into the map.
Definition: newgrf_gui.cpp:575
char * last_name
The name of the last &#39;seen&#39; NewGRF.
int GetScrolledRowFromWidget(int clickpos, const Window *const w, int widget, int padding=0, int line_height=-1) const
Compute the row of a scrolled widget that a user clicked in.
Definition: widget.cpp:1959
std::vector< ContentInfo * > ContentVector
Vector with content info.
Multi-line description of a parameter.
Definition: newgrf_widget.h:30
EventState OnKeyPress(WChar key, uint16 keycode) override
A key has been pressed.
Information about GRF, used in the game and (part of it) in savegames.
Window for showing NewGRF files.
Definition: newgrf_gui.cpp:590
void ShowQuery(StringID caption, StringID message, Window *parent, QueryCallbackProc *callback)
Show a modal confirmation window with standard &#39;yes&#39; and &#39;no&#39; buttons The window is aligned to the ce...
Definition: misc_gui.cpp:1262
int ok_button
Widget button of parent window to simulate when pressing OK in OSK.
uint8 padding_top
Paddings added to the top of the widget. Managed by parent container widget.
Definition: widget_type.h:183
List window of available NewGRFs.
Definition: newgrf_widget.h:46
GRFConfig ** CopyGRFConfigList(GRFConfig **dst, const GRFConfig *src, bool init_only)
Copy a GRF Config list.
uint smallest_y
Smallest vertical size of the widget in a filled window.
Definition: widget_type.h:172
Rescan files (available NewGRFs).
Definition: newgrf_widget.h:56
void ShowNewGRFError()
Show the first NewGRF error we can find.
Definition: newgrf_gui.cpp:46
Open Parameters Window for selected NewGRF for editing parameters.
Definition: newgrf_widget.h:52
SmallMap< int, QueryString * > querystrings
QueryString associated to WWT_EDITBOX widgets.
Definition: window_gui.h:330
Bitmask to get only the use palette use states.
Definition: newgrf_config.h:69
Types related to the misc widgets.
Move NewGRF down in active list.
Definition: newgrf_widget.h:41
#define FONT_HEIGHT_NORMAL
Height of characters in the normal (FS_NORMAL) font.
Definition: gfx_func.h:178
static NWidgetPart SetDataTip(uint32 data, StringID tip)
Widget part function for setting the data and tooltip.
Definition: widget_type.h:1014
void AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl) override
Assign size and position to the widget.
bool Contains(const T &key) const
Tests whether a key is assigned in this map.
const char * GetDescription() const
Get the grf info.
Functions related to setting/changing the settings.
void CopyParams(const GRFConfig &src)
Copy the parameter information from the src config.
Data stored about a string that can be modified in the GUI.
char * GRFBuildParamList(char *dst, const GRFConfig *c, const char *last)
Build a string containing space separated parameter values, and terminate.
ClientSettings _settings_client
The current settings for this game.
Definition: settings.cpp:80
ScanProgressWindow()
Create the window.
Display plane with zero size vertically, and filling and resizing horizontally.
Definition: widget_type.h:389
Types related to global configuration settings.
static NWidgetPart SetMinimalSize(int16 x, int16 y)
Widget part function for setting the minimal size.
Definition: widget_type.h:947
void OnClick(Point pt, int widget, int click_count) override
A click with the left mouse button has been made on the window.
Open content download (active NewGRFs).
Definition: newgrf_widget.h:59
bool editable
Editable status of the parent NewGRF window (if false, drop all widgets that make the window editable...
The palette state is set to use the Windows palette.
Definition: newgrf_config.h:68
Definition of base types and functions in a cross-platform compatible way.
virtual NWidgetCore * GetWidgetFromPos(int x, int y)=0
Retrieve a widget by its position.
void OnInvalidateData(int data=0, bool gui_scope=true) override
Some data on this window has become invalid.
Definition: newgrf_gui.cpp:471
void OnRealtimeTick(uint delta_ms) override
Called periodically.
Definition: newgrf_gui.cpp:486
A number of safeguards to prevent using unsafe methods.
uint32 max_value
The maximal value of this parameter.
Normal push-button (no toggle button) with text caption.
Definition: widget_type.h:104
Geometry functions.
uint fill_y
Vertical fill stepsize (from initial size, 0 means not resizable).
Definition: widget_type.h:165
Simple depressed panel.
Definition: widget_type.h:50
bool closing_dropdown
True, if the dropdown list is currently closing.
Definition: newgrf_gui.cpp:153
void DeleteChildWindows(WindowClass wc=WC_INVALID) const
Delete all children a window might have in a head-recursive manner.
Definition: window.cpp:1056
Offset at bottom to draw below the text.
Definition: window_gui.h:69
static Listing last_sorting
Default sorting of #GUIGRFConfigList.
Definition: newgrf_gui.cpp:595
const Scrollbar * GetScrollbar(uint widnum) const
Return the Scrollbar to a widget index.
Definition: window.cpp:311
uint8 flags
NOSAVE: GCF_Flags, bitset.
char * stredup(const char *s, const char *last)
Create a duplicate of the given string.
Definition: string.cpp:138
Filter list of available NewGRFs.
Definition: newgrf_widget.h:43
int pos_x
Horizontal position of top-left corner of the widget in the window.
Definition: widget_type.h:177
GRF is unusable with this version of OpenTTD.
Definition: newgrf_config.h:31
State state
Whether the content info is selected (for download)
Definition: tcp_content.h:81
void SetDParamMaxDigits(uint n, uint count, FontSize size)
Set DParam n to some number that is suitable for string size computations.
Definition: strings.cpp:122
void SetStringParameters(int widget) const override
Initialize string parameters for a widget.
Definition: newgrf_gui.cpp:775
uint8 padding_left
Paddings added to the left of the widget. Managed by parent container widget. (parent container may s...
Definition: widget_type.h:186
const GRFConfig * grf_config
View the textfile of this GRFConfig.
Definition: newgrf_gui.cpp:545
int avail_pos
Index of avail_sel if existing, else -1.
Definition: newgrf_gui.cpp:602
Center the window.
Definition: window_gui.h:157
Find newest Grf, ignoring Grfs with GCF_INVALID set.
static NWidgetPart NWidget(WidgetType tp, Colours col, int16 idx=-1)
Widget part function for starting a new &#39;real&#39; widget.
Definition: widget_type.h:1114
static WindowDesc _newgrf_parameters_desc(WDP_CENTER, "settings_newgrf_config", 500, 208, WC_GRF_PARAMETERS, WC_NONE, 0, _nested_newgrf_parameter_widgets, lengthof(_nested_newgrf_parameter_widgets))
Window definition for the change grf parameters window.
void ShowNewGRFSettings(bool editable, bool show_params, bool exec_changes, GRFConfig **config)
Setup the NewGRF gui.
Offset at bottom to draw the frame rectangular area.
Definition: window_gui.h:65
Baseclass for nested widgets.
Definition: widget_type.h:126
int DrawString(int left, int right, int top, const char *str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
Draw string, possibly truncated to make it fit in its allocated space.
Definition: gfx.cpp:499
Window for setting the parameters of a NewGRF.
Definition: newgrf_gui.cpp:147
StringList grf_presets
List of known NewGRF presets.
Definition: newgrf_gui.cpp:606
void OnDropdownSelect(int widget, int index) override
A dropdown option associated to this window has been selected.
Definition: newgrf_gui.cpp:441
Basic functions/variables used all over the place.
Part of the network protocol handling content distribution.
Right offset of the text of the frame.
Definition: window_gui.h:73
#define lengthof(x)
Return the length of an fixed size array.
Definition: depend.cpp:42
Simple progress bar.
Definition: newgrf_widget.h:75
TextfileType
Additional text files accompanying Tar archives.
Definition: textfile_type.h:16
uint resize_y
Vertical resize step (0 means not resizable).
Definition: widget_type.h:167
Grid of rows and columns.
Definition: widget_type.h:59
StringID message
Default message.
static T min(const T a, const T b)
Returns the minimum of two values.
Definition: math_func.hpp:42
virtual void Draw(const Window *w)=0
Draw the widgets of the tree.
Top offset of the text of the frame.
Definition: window_gui.h:74
Left offset of the text of the frame.
Definition: window_gui.h:72
int cancel_button
Widget button of parent window to simulate when pressing CANCEL in OSK.
void DrawArrowButtons(int x, int y, Colours button_colour, byte state, bool clickable_left, bool clickable_right)
Draw [<][>] boxes.
bool Sort(SortFunction *compare)
Sort the list.
uint32 StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:18
int active_over
Active GRF item over which another one is dragged, -1 if none.
Definition: newgrf_gui.cpp:616
void DeleteWindowByClass(WindowClass cls)
Delete all windows of a given class.
Definition: window.cpp:1159
static const uint8 PC_BLACK
Black palette colour.
Definition: gfx_func.h:205
byte md5sum[16]
The MD5 checksum.
Definition: tcp_content.h:76
bool HasGrfIdentifier(uint32 grfid, const uint8 *md5sum) const
Does the identification match the provided values?
Definition: newgrf_config.h:94
#define SETTING_BUTTON_WIDTH
Width of setting buttons.
Definition: settings_gui.h:19
uint8 padding_bottom
Paddings added to the bottom of the widget. Managed by parent container widget.
Definition: widget_type.h:185
static void NewGRFConfirmationCallback(Window *w, bool confirmed)
Callback function for the newgrf &#39;apply changes&#39; confirmation window.
Build object; Window numbers:
Definition: window_type.h:371
bool CanUpgradeCurrent()
Test whether the currently active set of NewGRFs can be upgraded with the available NewGRFs...
Definition: newgrf_gui.cpp:682
Draw border only, no background.
Definition: window_gui.h:30
static const int WIDGET_LIST_END
indicate the end of widgets&#39; list for vararg functions
Definition: widget_type.h:22
List of active NewGRFs is being edited.
Definition: window_type.h:703
Text explaining what is happening.
Definition: newgrf_widget.h:76
Subdirectory for all NewGRFs.
Definition: fileio_type.h:119
GRF changed.
Definition: gamelog.h:21
Offset at top to draw above the text.
Definition: window_gui.h:68
The window is a modal child of some other window, meaning the parent is &#39;inactive&#39;.
Definition: window_gui.h:211
void GamelogStopAction()
Stops logging of any changes.
Definition: gamelog.cpp:80
uint32 version
NOSAVE: Version a NewGRF can set so only the newest NewGRF is shown.
Open content download (available NewGRFs).
Definition: newgrf_widget.h:58
void SetDirty() const
Mark entire window as dirty (in need of re-paint)
Definition: window.cpp:968
bool IsVisible(uint16 item) const
Checks whether given current item is visible in the list.
Definition: widget_type.h:641
void Assign(StringID string)
Render a string into the textbuffer.
Definition: textbuf.cpp:398
The content consists of a NewGRF.
Definition: tcp_content.h:26
void DeleteGRFPresetFromConfig(const char *config_name)
Delete a NewGRF configuration by preset name.
Definition: settings.cpp:1832
bool newgrf_developer_tools
activate NewGRF developer tools and allow modifying NewGRFs in an existing game
QueryString presetname_editbox
Edit box of the save preset.
GUITimer timeout
How long before we unpress the last-pressed button?
Definition: newgrf_gui.cpp:154
Dimension GetStringBoundingBox(const char *str, FontSize start_fontsize)
Return the string dimension in pixels.
Definition: gfx.cpp:698
bool execute
On pressing &#39;apply changes&#39; are grf changes applied immediately, or only list is updated.
Definition: newgrf_gui.cpp:614
const GRFConfig * avail_sel
Currently selected available grf. nullptr is none is selected.
Definition: newgrf_gui.cpp:601
void AddLine(const char *str)
Pass another text line from the current item to the filter.
uint16 GetCount() const
Gets the number of elements in the list.
Definition: widget_type.h:613
Dimension GetStringMultiLineBoundingBox(StringID str, const Dimension &suggestion)
Calculate string bounding box for multi-line strings.
Definition: gfx.cpp:587
Dimension maxdim(const Dimension &d1, const Dimension &d2)
Compute bounding box of both dimensions.
No window, redirects to WC_MAIN_WINDOW.
Definition: window_type.h:40
void DeleteWindowById(WindowClass cls, WindowNumber number, bool force)
Delete a window by its class and window number (if it is open).
Definition: window.cpp:1146
static const uint8 PC_GREY
Grey palette colour.
Definition: gfx_func.h:207
static const uint8 PC_DARK_BLUE
Dark blue palette colour.
Definition: gfx_func.h:224
Callback for NewGRF scanning.
const char * GetURL() const
Get the grf url.
Delete active preset.
Definition: newgrf_widget.h:37
Add NewGRF to active list.
Definition: newgrf_widget.h:38
const char * GetGRFStringFromGRFText(const GRFText *text)
Get a C-string from a GRFText-list.
void SetStringParameters(int widget) const override
Initialize string parameters for a widget.
Definition: newgrf_gui.cpp:553
char *const buf
buffer in which text is saved
Definition: textbuf_type.h:34
void SetObjectToPlaceWnd(CursorID icon, PaletteID pal, HighLightStyle mode, Window *w)
Change the cursor and mouse click/drag handling to a mode for performing special operations like tile...
Definition: viewport.cpp:3311
bool editable
Is the window editable?
Definition: newgrf_gui.cpp:612
static bool NameSorter(const GRFConfig *const &a, const GRFConfig *const &b)
Sort grfs by name.
void SetStringParameters(int widget) const override
Initialize string parameters for a widget.
Definition: newgrf_gui.cpp:237
bool scenario_developer
activate scenario developer: allow modifying NewGRFs in an existing game
void ReInitAllWindows()
Re-initialize all windows.
Definition: window.cpp:3435
virtual void AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)=0
Assign size and position to the widget.
NWID_SELECTION to optionally display parameter descriptions.
Definition: newgrf_widget.h:29
Save preset; Window numbers:
Definition: window_type.h:682
GUISettings gui
settings related to the GUI
void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
Update size and resize step of a widget in the window.
Definition: newgrf_gui.cpp:720
PaletteID GetPalette(const GRFConfig *c) const
Pick the palette for the sprite of the grf to display.
Definition: newgrf_gui.cpp:794
void OnDragDrop(Point pt, int widget) override
A dragged &#39;object&#39; has been released.
Window caption (window title between closebox and stickybox)
Definition: widget_type.h:61
Information about one grf parameter.
Window for displaying a textfile.
Definition: textfile_gui.h:23
int strnatcmp(const char *s1, const char *s2, bool ignore_garbage_at_front)
Compares two strings using case insensitive natural sort.
Definition: string.cpp:580
static bool StrEmpty(const char *s)
Check if a string buffer is empty.
Definition: string_func.h:59
Declarations for savegames operations.
void SetFilterState(bool state)
Enable or disable the filter.
void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
Update size and resize step of a widget in the window.
const char * GetTextfile(TextfileType type) const
Search a textfile file next to this NewGRF.
NWidgetBase * avs
Widget with the available grfs list and buttons.
void Draw(const Window *w) override
Draw the widgets of the tree.
uint8 palette
GRFPalette, bitset.
void OnClick(Point pt, int widget, int click_count) override
A click with the left mouse button has been made on the window.
Definition: newgrf_gui.cpp:316
int scanned
The number of NewGRFs that we have seen.
bool complete_labels
True if all values have a label.
TextDirection _current_text_dir
Text direction of the currently selected language.
Definition: strings.cpp:50
ContentType type
Type of content.
Definition: tcp_content.h:67
bool CDECL FilterFunction(const T *, F)
Signature of filter function.
Definition: sortlist_type.h:52
void OnResize() override
Called after the window got resized.
Definition: newgrf_gui.cpp:461
Searching and filtering using a stringterm.
void SetDParamMaxValue(uint n, uint64 max_value, uint min_count, FontSize size)
Set DParam n to some number that is suitable for string size computations.
Definition: strings.cpp:106
Custom nested widget container for the NewGRF gui.
std::vector< std::string > StringList
Type for a list of strings.
Definition: string_type.h:60
Vertical container.
Definition: widget_type.h:77
void ResetGRFConfig(bool defaults)
Reset the current GRF Config to either blank or newgame settings.
Scrollbar to scroll through all settings.
Definition: newgrf_widget.h:26
Functions for setting GUIs.
static NWidgetPart EndContainer()
Widget part function for denoting the end of a container (horizontal, vertical, WWT_FRAME, WWT_INSET, or WWT_PANEL).
Definition: widget_type.h:999
Open NewGRF readme, changelog (+1) or license (+2).
Definition: newgrf_widget.h:51
Find newest Grf.
char * strecpy(char *dst, const char *src, const char *last)
Copies characters from one buffer to another.
Definition: depend.cpp:68
GRFConfig * grf_config
Set the parameters of this GRFConfig.
Definition: newgrf_gui.cpp:149
uint current_x
Current horizontal size (after resizing).
Definition: widget_type.h:174
char * filename
Filename - either with or without full path.
bool action14present
True if action14 information is present.
Definition: newgrf_gui.cpp:158
int line_height
Height of a row in the matrix widget.
Definition: newgrf_gui.cpp:156
uint clicked_button
The row in which a button was clicked or UINT_MAX.
Definition: newgrf_gui.cpp:150
static GUIGRFConfigList::FilterFunction *const filter_funcs[]
Filter functions of the #GUIGRFConfigList.
Definition: newgrf_gui.cpp:598
bool SetFocusedWidget(int widget_index)
Set focus within this window to the given widget.
Definition: window.cpp:488
void OnQueryTextFinished(char *str) override
The query window opened from this window has closed.
void DrawDropDownButton(int x, int y, Colours button_colour, bool state, bool clickable)
Draw a dropdown button.
Window * FindWindowByClass(WindowClass cls)
Find any window by its class.
Definition: window.cpp:1130
void OnEditboxChanged(int wid) override
The text in an editbox has been edited.
void ScanNewGRFFiles(NewGRFScanCallback *callback)
Scan for all NewGRFs.
Button to cancel saving the preset.
Definition: newgrf_widget.h:69
Coordinates of a point in 2D.
Dimension GetSpriteSize(SpriteID sprid, Point *offset, ZoomLevel zoom)
Get the size of a sprite.
Definition: gfx.cpp:767
List item containing a C char string.
Definition: dropdown_type.h:71
NWidgetBase * inf
Info panel.
uint32 min_loadable_version
NOSAVE: Minimum compatible version a NewGRF can define.
Data structure describing how to show the list (what sort direction and criteria).
Definition: sortlist_type.h:33
Drop down list.
Definition: widget_type.h:70
std::vector< GRFParameterInfo * > param_info
NOSAVE: extra information about the parameters.
Normal push-button (no toggle button) with arrow caption.
Definition: widget_type.h:106
uint16 GetCapacity() const
Gets the number of visible elements of the scrollbar.
Definition: widget_type.h:622
The parameter is either 0 or 1.
static const StringID INVALID_STRING_ID
Constant representing an invalid string (16bit in case it is used in savegames)
Definition: strings_type.h:19
uint32 min_value
The minimal value this parameter can have.
void OnQueryTextFinished(char *str) override
The query window opened from this window has closed.
Definition: newgrf_gui.cpp:430
virtual void SetupSmallestSize(Window *w, bool init_array)=0
Compute smallest size needed by the widget.
const char * GetName() const
Get the name of this grf.
uint32 grfid
GRF ID (defined by Action 0x08)
Definition: newgrf_config.h:85
char * data
Additional data for message and custom_message.
struct GRFText * name
The name of this parameter.
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: depend.cpp:131
Scrollbar for the list available preset names.
Definition: newgrf_widget.h:67
Width of a resize box widget.
Definition: window_gui.h:112
Offset at right to draw the frame rectangular area.
Definition: window_gui.h:63
void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
Update size and resize step of a widget in the window.
Definition: newgrf_gui.cpp:193
void SetFiltering(Filtering f)
Import filter conditions.
Used for DoCommand-like (and some non-fatal AI GUI) errors/information.
Definition: error.h:23
int width
width of the window (number of pixels to the right in x direction)
Definition: window_gui.h:321
The caption of the window.
Definition: misc_widget.h:51
uint32 param[0x80]
GRF parameters.
GRF file has been activated.
Definition: newgrf_config.h:40
static NWidgetPart SetFill(uint fill_x, uint fill_y)
Widget part function for setting filling.
Definition: widget_type.h:983
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
bool AddGRFToActive(int ins_pos=-1)
Insert a GRF into the active list.
Active NewGRF preset.
Definition: newgrf_widget.h:35
Select display of the buttons below the &#39;details&#39;.
Definition: newgrf_widget.h:61
NewGRF parameters; Window numbers:
Definition: window_type.h:176
Scrollbar * vscroll
Pointer to the scrollbar widget.
GRF file does not exactly match the requested GRF (different MD5SUM), but grfid matches) ...
Definition: newgrf_config.h:27
void DrawBoolButton(int x, int y, bool state, bool clickable)
Draw a toggle button.
void SetCapacityFromWidget(Window *w, int widget, int padding=0)
Set capacity of visible elements from the size and resize properties of a widget. ...
Definition: widget.cpp:1973
void ResetObjectToPlace()
Reset the cursor and mouse mode handling back to default (normal cursor, only clicking in windows)...
Definition: viewport.cpp:3374
Specification of a rectangle with absolute coordinates of all edges.
Vertical scrollbar.
Definition: widget_type.h:84
The passed event is handled.
Definition: window_type.h:714
Text is written right-to-left by default.
Definition: strings_type.h:26
static GUIGRFConfigList::SortFunction *const sorter_funcs[]
Sort functions of the #GUIGRFConfigList.
Definition: newgrf_gui.cpp:597
Bitmask to get only the NewGRF supplied information.
Definition: newgrf_config.h:75
static const uint MAX_EXTRA_INFO_WIDTH
Maximal additional width given to the panel.
Functions related to tile highlights.
static uint32 BSWAP32(uint32 x)
Perform a 32 bits endianness bitswap on x.
Window functions not directly related to making/drawing windows.
void ReloadNewGRFData()
Reload all NewGRF files during a running game.
Definition: afterload.cpp:3234
uint8 md5sum[16]
MD5 checksum of file to distinguish files with the same GRF ID (eg. newer version of GRF) ...
Definition: newgrf_config.h:86
Rescan files (active NewGRFs).
Definition: newgrf_widget.h:57
Only find Grfs matching md5sum.
Stacked widgets, only one visible at a time (eg in a panel with tabs).
Definition: widget_type.h:80
bool SortFunction(const T &, const T &)
Signature of sort function.
Definition: sortlist_type.h:51
static const CursorID SPR_CURSOR_MOUSE
Cursor sprite numbers.
Definition: sprites.h:1369
#define SETTING_BUTTON_HEIGHT
Height of setting buttons.
Definition: settings_gui.h:21
void DrawWidget(const Rect &r, int widget) const override
Draw the contents of a nested widget.
Definition: newgrf_gui.cpp:246
Errors (eg. saving/loading failed)
Definition: error.h:25
Edit box for changing the preset name.
Definition: newgrf_widget.h:68
uint8 padding_right
Paddings added to the right of the widget. Managed by parent container widget. (parent container may ...
Definition: widget_type.h:184
void GamelogGRFUpdate(const GRFConfig *oldc, const GRFConfig *newc)
Compares two NewGRF lists and logs any change.
Definition: gamelog.cpp:719
uint32 WChar
Type for wide characters, i.e.
Definition: string_type.h:37
Button to save the preset.
Definition: newgrf_widget.h:70
static const uint MIN_EXTRA_FOR_3_COLUMNS
Minimal additional width needed before switching to 3 columns.
void DrawWidget(const Rect &r, int widget) const override
Draw the contents of a nested widget.
GRFConfig * active_sel
Selected active grf item.
Definition: newgrf_gui.cpp:609
static NWidgetPart SetScrollbar(int index)
Attach a scrollbar to a widget.
Definition: widget_type.h:1095
uint fill_x
Horizontal fill stepsize (from initial size, 0 means not resizable).
Definition: widget_type.h:164
Types related to the newgrf widgets.
Open Parameters Window for selected NewGRF for viewing parameters.
Definition: newgrf_widget.h:53
Dimensions (a width and height) of a rectangle in 2D.
Container for all important information about a piece of content.
Definition: tcp_content.h:56
Value of the NCB_EQUALSIZE flag.
Definition: widget_type.h:429
Select active list buttons (0 = normal, 1 = simple layout).
Definition: newgrf_widget.h:60
Query string window; Window numbers:
Definition: window_type.h:118
bool IsEmpty() const
Check whether any filter words were entered.
Offset at left to draw the frame rectangular area.
Definition: window_gui.h:62
This file contains all sprite-related enums and defines.
Toggle Palette of selected, active NewGRF.
Definition: newgrf_widget.h:54
static Filtering last_filtering
Default filtering of #GUIGRFConfigList.
Definition: newgrf_gui.cpp:596
Game options window; Window numbers:
Definition: window_type.h:608
int find_index(std::vector< T > const &vec, T const &item)
Helper function to get the index of an item Consider using std::set, std::unordered_set or std::flat_...
void DrawSprite(SpriteID img, PaletteID pal, int x, int y, const SubSprite *sub, ZoomLevel zoom)
Draw a sprite, not in a viewport.
Definition: gfx.cpp:832
NWidgetCore * GetWidgetFromPos(int x, int y) override
Retrieve a widget by its position.
static NWidgetPart SetPIP(uint8 pre, uint8 inter, uint8 post)
Widget part function for setting a pre/inter/post spaces.
Definition: widget_type.h:1076
static WindowDesc _scan_progress_desc(WDP_CENTER, nullptr, 0, 0, WC_MODAL_PROGRESS, WC_NONE, 0, _nested_scan_progress_widgets, lengthof(_nested_scan_progress_widgets))
Description of the widgets and other settings of the window.
int pos_y
Vertical position of top-left corner of the widget in the window.
Definition: widget_type.h:178
void InvalidateData(int data=0, bool gui_scope=true)
Mark this window&#39;s data as invalid (in need of re-computing)
Definition: window.cpp:3240
static const uint NETWORK_MAX_GRF_COUNT
Maximum number of GRFs that can be sent.
Definition: config.h:60
void ShowNetworkContentListWindow(ContentVector *cv=nullptr, ContentType type1=CONTENT_TYPE_END, ContentType type2=CONTENT_TYPE_END)
Show the content list window with a given set of content.
Window for displaying the textfile of a NewGRF.
Definition: newgrf_gui.cpp:544
void OnDropdownClose(Point pt, int widget, int index, bool instant_close) override
A dropdown window associated to this window has been closed.
Definition: newgrf_gui.cpp:450
void OnInvalidateData(int data=0, bool gui_scope=true) override
Some data on this window has become invalid.
uint clicked_row
The selected parameter.
Definition: newgrf_gui.cpp:155
void UpdateNewGRFScanStatus(uint num, const char *name)
Update the NewGRF scan status.
GUI functions related to textfiles.
int DrawStringMultiLine(int left, int right, int top, int bottom, const char *str, TextColour colour, StringAlignment align, bool underline, FontSize fontsize)
Draw string, possibly over multiple lines.
Definition: gfx.cpp:620
StringList GetGRFPresetList()
Get the list of known NewGrf presets.
Definition: settings.cpp:1777
Panel to draw the settings on.
Definition: newgrf_widget.h:25
Class for the save preset window.
uint16 GetPosition() const
Gets the position of the first visible element in the list.
Definition: widget_type.h:631
static void SetDParam(uint n, uint64 v)
Set a string parameter v at index n in the global string parameter array.
Definition: strings_func.h:201
Base class for a &#39;real&#39; widget.
Definition: widget_type.h:284
void UpgradeCurrent()
Upgrade the currently active set of NewGRFs.
Definition: newgrf_gui.cpp:695
Base for the NewGRF implementation.