OpenTTD
newgrf_gui.cpp
Go to the documentation of this file.
1 /* $Id: newgrf_gui.cpp 27729 2017-01-14 13:12:49Z frosch $ */
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 
34 #include "widgets/newgrf_widget.h"
35 #include "widgets/misc_widget.h"
36 
37 #include "table/sprites.h"
38 
39 #include <map>
40 #include "safeguards.h"
41 
46 {
47  /* Do not show errors when entering the main screen */
48  if (_game_mode == GM_MENU) return;
49 
50  for (const GRFConfig *c = _grfconfig; c != NULL; c = c->next) {
51  /* We only want to show fatal errors */
52  if (c->error == NULL || c->error->severity != STR_NEWGRF_ERROR_MSG_FATAL) continue;
53 
54  SetDParam (0, c->error->custom_message == NULL ? c->error->message : STR_JUST_RAW_STRING);
55  SetDParamStr(1, c->error->custom_message);
56  SetDParamStr(2, c->filename);
57  SetDParamStr(3, c->error->data);
58  for (uint i = 0; i < lengthof(c->error->param_value); i++) {
59  SetDParam(4 + i, c->error->param_value[i]);
60  }
61  ShowErrorMessage(STR_NEWGRF_ERROR_FATAL_POPUP, INVALID_STRING_ID, WL_CRITICAL);
62  break;
63  }
64 }
65 
66 static void ShowNewGRFInfo(const GRFConfig *c, uint x, uint y, uint right, uint bottom, bool show_params)
67 {
68  if (c->error != NULL) {
69  char message[512];
70  SetDParamStr(0, c->error->custom_message); // is skipped by built-in messages
71  SetDParamStr(1, c->filename);
72  SetDParamStr(2, c->error->data);
73  for (uint i = 0; i < lengthof(c->error->param_value); i++) {
74  SetDParam(3 + i, c->error->param_value[i]);
75  }
76  GetString(message, c->error->custom_message == NULL ? c->error->message : STR_JUST_RAW_STRING, lastof(message));
77 
78  SetDParamStr(0, message);
79  y = DrawStringMultiLine(x, right, y, bottom, c->error->severity);
80  }
81 
82  /* Draw filename or not if it is not known (GRF sent over internet) */
83  if (c->filename != NULL) {
84  SetDParamStr(0, c->filename);
85  y = DrawStringMultiLine(x, right, y, bottom, STR_NEWGRF_SETTINGS_FILENAME);
86  }
87 
88  /* Prepare and draw GRF ID */
89  char buff[256];
90  seprintf(buff, lastof(buff), "%08X", BSWAP32(c->ident.grfid));
91  SetDParamStr(0, buff);
92  y = DrawStringMultiLine(x, right, y, bottom, STR_NEWGRF_SETTINGS_GRF_ID);
93 
95  SetDParam(0, c->version);
96  y = DrawStringMultiLine(x, right, y, bottom, STR_NEWGRF_SETTINGS_VERSION);
97  }
100  y = DrawStringMultiLine(x, right, y, bottom, STR_NEWGRF_SETTINGS_MIN_VERSION);
101  }
102 
103  /* Prepare and draw MD5 sum */
104  md5sumToString(buff, lastof(buff), c->ident.md5sum);
105  SetDParamStr(0, buff);
106  y = DrawStringMultiLine(x, right, y, bottom, STR_NEWGRF_SETTINGS_MD5SUM);
107 
108  /* Show GRF parameter list */
109  if (show_params) {
110  if (c->num_params > 0) {
111  GRFBuildParamList(buff, c, lastof(buff));
112  SetDParam(0, STR_JUST_RAW_STRING);
113  SetDParamStr(1, buff);
114  } else {
115  SetDParam(0, STR_LAND_AREA_INFORMATION_LOCAL_AUTHORITY_NONE);
116  }
117  y = DrawStringMultiLine(x, right, y, bottom, STR_NEWGRF_SETTINGS_PARAMETER);
118 
119  /* Draw the palette of the NewGRF */
120  if (c->palette & GRFP_BLT_32BPP) {
121  SetDParamStr(0, (c->palette & GRFP_USE_WINDOWS) ? "Legacy (W) / 32 bpp" : "Default (D) / 32 bpp");
122  } else {
123  SetDParamStr(0, (c->palette & GRFP_USE_WINDOWS) ? "Legacy (W)" : "Default (D)");
124  }
125  y = DrawStringMultiLine(x, right, y, bottom, STR_NEWGRF_SETTINGS_PALETTE);
126  }
127 
128  /* Show flags */
129  if (c->status == GCS_NOT_FOUND) y = DrawStringMultiLine(x, right, y, bottom, STR_NEWGRF_SETTINGS_NOT_FOUND);
130  if (c->status == GCS_DISABLED) y = DrawStringMultiLine(x, right, y, bottom, STR_NEWGRF_SETTINGS_DISABLED);
131  if (HasBit(c->flags, GCF_INVALID)) y = DrawStringMultiLine(x, right, y, bottom, STR_NEWGRF_SETTINGS_INCOMPATIBLE);
132  if (HasBit(c->flags, GCF_COMPATIBLE)) y = DrawStringMultiLine(x, right, y, bottom, STR_NEWGRF_COMPATIBLE_LOADED);
133 
134  /* Draw GRF info if it exists */
135  if (!StrEmpty(c->GetDescription())) {
136  SetDParamStr(0, c->GetDescription());
137  y = DrawStringMultiLine(x, right, y, bottom, STR_BLACK_RAW_STRING);
138  } else {
139  y = DrawStringMultiLine(x, right, y, bottom, STR_NEWGRF_SETTINGS_NO_INFO);
140  }
141 }
142 
153  int timeout;
154  uint clicked_row;
156  Scrollbar *vscroll;
158  bool editable;
159 
160  NewGRFParametersWindow(WindowDesc *desc, GRFConfig *c, bool editable) : Window(desc),
161  grf_config(c),
162  clicked_button(UINT_MAX),
163  clicked_dropdown(false),
164  closing_dropdown(false),
165  timeout(0),
166  clicked_row(UINT_MAX),
167  editable(editable)
168  {
169  this->action14present = (c->num_valid_params != lengthof(c->param) || c->param_info.Length() != 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  {
190  return &dummy_parameter_info;
191  }
192 
193  virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
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: {
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.Length(); i++) {
224  const GRFParameterInfo *par_info = this->grf_config->param_info[i];
225  if (par_info == NULL) continue;
226  const char *desc = GetGRFStringFromGRFText(par_info->desc);
227  if (desc == NULL) continue;
228  Dimension d = GetStringMultiLineBoundingBox(desc, suggestion);
230  suggestion = maxdim(d, suggestion);
231  }
232  size->height = suggestion.height;
233  break;
234  }
235  }
236 
237  virtual void SetStringParameters(int widget) const
238  {
239  switch (widget) {
240  case WID_NP_NUMPAR:
241  SetDParam(0, this->vscroll->GetCount());
242  break;
243  }
244  }
245 
246  virtual void DrawWidget(const Rect &r, int widget) const
247  {
248  if (widget == WID_NP_DESCRIPTION) {
249  const GRFParameterInfo *par_info = (this->clicked_row < this->grf_config->param_info.Length()) ? this->grf_config->param_info[this->clicked_row] : NULL;
250  if (par_info == NULL) return;
251  const char *desc = GetGRFStringFromGRFText(par_info->desc);
252  if (desc == NULL) 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.Length()) ? this->grf_config->param_info[i] : NULL;
269  if (par_info == NULL) 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 != NULL) {
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 != NULL) {
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  virtual void OnPaint()
308  {
309  if (this->closing_dropdown) {
310  this->closing_dropdown = false;
311  this->clicked_dropdown = false;
312  }
313  this->DrawWidgets();
314  }
315 
316  virtual void OnClick(Point pt, int widget, int click_count)
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.Length()) ? this->grf_config->param_info[num] : NULL;
354  if (par_info == NULL) 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 = new DropDownList();
380  for (uint32 i = par_info->min_value; i <= par_info->max_value; i++) {
381  *list->Append() = new DropDownListCharStringItem(GetGRFStringFromGRFText(par_info->value_names.Find(i)->second), i, false);
382  }
383 
384  ShowDropDownListAt(this, 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 = 5;
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;
420  this->InvalidateData();
422  break;
423 
424  case WID_NP_ACCEPT:
425  delete this;
426  break;
427  }
428  }
429 
430  virtual void OnQueryTextFinished(char *str)
431  {
432  if (StrEmpty(str)) return;
433  int32 value = atoi(str);
434  GRFParameterInfo *par_info = ((uint)this->clicked_row < this->grf_config->param_info.Length()) ? this->grf_config->param_info[this->clicked_row] : NULL;
435  if (par_info == NULL) 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  virtual void OnDropdownSelect(int widget, int index)
442  {
443  assert(this->clicked_dropdown);
444  GRFParameterInfo *par_info = ((uint)this->clicked_row < this->grf_config->param_info.Length()) ? this->grf_config->param_info[this->clicked_row] : NULL;
445  if (par_info == NULL) par_info = GetDummyParameterInfo(this->clicked_row);
446  par_info->SetValue(this->grf_config, index);
447  this->SetDirty();
448  }
449 
450  virtual void OnDropdownClose(Point pt, int widget, int index, bool instant_close)
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  virtual void OnResize()
462  {
463  this->vscroll->SetCapacityFromWidget(this, WID_NP_BACKGROUND);
464  }
465 
471  virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
472  {
473  if (!gui_scope) return;
474  if (!this->action14present) {
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  virtual void OnTick()
487  {
488  if (--this->timeout == 0) {
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 {
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  /* virtual */ void SetStringParameters(int widget) const
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 {
565  new NewGRFTextfileWindow(file_type, c);
566 }
567 
569 
571 public:
572  DropDownListPresetItem(int result) : DropDownListItem(result, false) {}
573 
574  virtual ~DropDownListPresetItem() {}
575 
576  bool Selectable() const
577  {
578  return true;
579  }
580 
581  void Draw(int left, int right, int top, int bottom, bool sel, int bg_colour) const
582  {
583  DrawString(left + 2, right + 2, top, _grf_preset_list[this->result], sel ? TC_WHITE : TC_BLACK);
584  }
585 };
586 
587 
588 typedef std::map<uint32, const GRFConfig *> GrfIdMap;
589 
595 static void FillGrfidMap(const GRFConfig *c, GrfIdMap *grfid_map)
596 {
597  while (c != NULL) {
598  std::pair<uint32, const GRFConfig *> p(c->ident.grfid, c);
599  grfid_map->insert(p);
600  c = c->next;
601  }
602 }
603 
604 static void NewGRFConfirmationCallback(Window *w, bool confirmed);
605 static void ShowSavePresetWindow(const char *initial_text);
606 
612 
613  static const uint EDITBOX_MAX_SIZE = 50;
614 
617  static GUIGRFConfigList::SortFunction * const sorter_funcs[];
618  static GUIGRFConfigList::FilterFunction * const filter_funcs[];
619 
622  int avail_pos;
625 
628 
630  bool editable;
631  bool show_params;
632  bool execute;
633  int preset;
635 
636  Scrollbar *vscroll;
637  Scrollbar *vscroll2;
638 
639  NewGRFWindow(WindowDesc *desc, bool editable, bool show_params, bool execute, GRFConfig **orig_list) : Window(desc), filter_editbox(EDITBOX_MAX_SIZE)
640  {
641  this->avail_sel = NULL;
642  this->avail_pos = -1;
643  this->active_sel = NULL;
644  this->actives = NULL;
645  this->orig_list = orig_list;
646  this->editable = editable;
647  this->execute = execute;
648  this->show_params = show_params;
649  this->preset = -1;
650  this->active_over = -1;
651 
652  CopyGRFConfigList(&this->actives, *orig_list, false);
654 
655  this->CreateNestedTree();
656  this->vscroll = this->GetScrollbar(WID_NS_SCROLLBAR);
657  this->vscroll2 = this->GetScrollbar(WID_NS_SCROLL2BAR);
658 
659  this->GetWidget<NWidgetStacked>(WID_NS_SHOW_REMOVE)->SetDisplayedPlane(this->editable ? 0 : 1);
660  this->GetWidget<NWidgetStacked>(WID_NS_SHOW_APPLY)->SetDisplayedPlane(this->editable ? 0 : this->show_params ? 1 : SZSP_HORIZONTAL);
662 
663  this->querystrings[WID_NS_FILTER] = &this->filter_editbox;
664  this->filter_editbox.cancel_button = QueryString::ACTION_CLEAR;
665  if (editable) {
667  } else {
669  }
670 
671  this->avails.SetListing(this->last_sorting);
672  this->avails.SetFiltering(this->last_filtering);
673  this->avails.SetSortFuncs(this->sorter_funcs);
674  this->avails.SetFilterFuncs(this->filter_funcs);
675  this->avails.ForceRebuild();
676 
678  }
679 
680  ~NewGRFWindow()
681  {
685 
686  if (this->editable && !this->execute) {
687  CopyGRFConfigList(this->orig_list, this->actives, true);
688  ResetGRFConfig(false);
690  }
691 
692  /* Remove the temporary copy of grf-list used in window */
693  ClearGRFConfigList(&this->actives);
695  }
696 
701  bool CanUpgradeCurrent()
702  {
703  GrfIdMap grfid_map;
704  FillGrfidMap(this->actives, &grfid_map);
705 
706  for (const GRFConfig *a = _all_grfs; a != NULL; a = a->next) {
707  GrfIdMap::const_iterator iter = grfid_map.find(a->ident.grfid);
708  if (iter != grfid_map.end() && a->version > iter->second->version) return true;
709  }
710  return false;
711  }
712 
714  void UpgradeCurrent()
715  {
716  GrfIdMap grfid_map;
717  FillGrfidMap(this->actives, &grfid_map);
718 
719  for (const GRFConfig *a = _all_grfs; a != NULL; a = a->next) {
720  GrfIdMap::iterator iter = grfid_map.find(a->ident.grfid);
721  if (iter == grfid_map.end() || iter->second->version >= a->version) continue;
722 
723  GRFConfig **c = &this->actives;
724  while (*c != iter->second) c = &(*c)->next;
725  GRFConfig *d = new GRFConfig(*a);
726  d->next = (*c)->next;
727  d->CopyParams(**c);
728  if (this->active_sel == *c) this->active_sel = NULL;
729  delete *c;
730  *c = d;
731  iter->second = d;
732  }
733  }
734 
735  virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
736  {
737  switch (widget) {
738  case WID_NS_FILE_LIST:
739  {
740  Dimension d = maxdim(GetSpriteSize(SPR_SQUARE), GetSpriteSize(SPR_WARNING_SIGN));
741  resize->height = max(d.height + 2U, FONT_HEIGHT_NORMAL + 2U);
742  size->height = max(size->height, WD_FRAMERECT_TOP + 6 * resize->height + WD_FRAMERECT_BOTTOM);
743  break;
744  }
745 
746  case WID_NS_AVAIL_LIST:
747  resize->height = max(12, FONT_HEIGHT_NORMAL + 2);
748  size->height = max(size->height, WD_FRAMERECT_TOP + 8 * resize->height + WD_FRAMERECT_BOTTOM);
749  break;
750 
752  Dimension dim = GetStringBoundingBox(STR_NEWGRF_SETTINGS_INFO_TITLE);
753  size->height = max(size->height, dim.height + WD_FRAMETEXT_TOP + WD_FRAMETEXT_BOTTOM);
754  size->width = max(size->width, dim.width + WD_FRAMETEXT_LEFT + WD_FRAMETEXT_RIGHT);
755  break;
756  }
757 
758  case WID_NS_NEWGRF_INFO:
759  size->height = max(size->height, WD_FRAMERECT_TOP + 10 * FONT_HEIGHT_NORMAL + WD_FRAMERECT_BOTTOM + padding.height + 2);
760  break;
761 
762  case WID_NS_PRESET_LIST: {
763  Dimension d = GetStringBoundingBox(STR_NUM_CUSTOM);
764  for (uint i = 0; i < _grf_preset_list.Length(); i++) {
765  if (_grf_preset_list[i] != NULL) {
767  d = maxdim(d, GetStringBoundingBox(STR_JUST_RAW_STRING));
768  }
769  }
770  d.width += padding.width;
771  *size = maxdim(d, *size);
772  break;
773  }
774 
777  Dimension d = GetStringBoundingBox(STR_NEWGRF_SETTINGS_FIND_MISSING_CONTENT_BUTTON);
778  *size = maxdim(d, GetStringBoundingBox(STR_INTRO_ONLINE_CONTENT));
779  size->width += padding.width;
780  size->height += padding.height;
781  break;
782  }
783  }
784  }
785 
786  virtual void OnResize()
787  {
788  this->vscroll->SetCapacityFromWidget(this, WID_NS_FILE_LIST);
789  this->vscroll2->SetCapacityFromWidget(this, WID_NS_AVAIL_LIST);
790  }
791 
792  virtual void SetStringParameters(int widget) const
793  {
794  switch (widget) {
795  case WID_NS_PRESET_LIST:
796  if (this->preset == -1) {
797  SetDParam(0, STR_NUM_CUSTOM);
798  } else {
799  SetDParam(0, STR_JUST_RAW_STRING);
800  SetDParamStr(1, _grf_preset_list[this->preset]);
801  }
802  break;
803  }
804  }
805 
811  inline PaletteID GetPalette(const GRFConfig *c) const
812  {
813  PaletteID pal;
814 
815  /* Pick a colour */
816  switch (c->status) {
817  case GCS_NOT_FOUND:
818  case GCS_DISABLED:
819  pal = PALETTE_TO_RED;
820  break;
821  case GCS_ACTIVATED:
822  pal = PALETTE_TO_GREEN;
823  break;
824  default:
825  pal = PALETTE_TO_BLUE;
826  break;
827  }
828 
829  /* Do not show a "not-failure" colour when it actually failed to load */
830  if (pal != PALETTE_TO_RED) {
831  if (HasBit(c->flags, GCF_STATIC)) {
832  pal = PALETTE_TO_GREY;
833  } else if (HasBit(c->flags, GCF_COMPATIBLE)) {
834  pal = PALETTE_TO_ORANGE;
835  }
836  }
837 
838  return pal;
839  }
840 
841  virtual void DrawWidget(const Rect &r, int widget) const
842  {
843  switch (widget) {
844  case WID_NS_FILE_LIST: {
845  GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, PC_BLACK);
846 
847  uint step_height = this->GetWidget<NWidgetBase>(WID_NS_FILE_LIST)->resize_y;
848  uint y = r.top + WD_FRAMERECT_TOP;
849  Dimension square = GetSpriteSize(SPR_SQUARE);
850  Dimension warning = GetSpriteSize(SPR_WARNING_SIGN);
851  int square_offset_y = (step_height - square.height) / 2;
852  int warning_offset_y = (step_height - warning.height) / 2;
853  int offset_y = (step_height - FONT_HEIGHT_NORMAL) / 2;
854 
855  bool rtl = _current_text_dir == TD_RTL;
856  uint text_left = rtl ? r.left + WD_FRAMERECT_LEFT : r.left + square.width + 15;
857  uint text_right = rtl ? r.right - square.width - 15 : r.right - WD_FRAMERECT_RIGHT;
858  uint square_left = rtl ? r.right - square.width - 5 : r.left + 5;
859  uint warning_left = rtl ? r.right - square.width - warning.width - 10 : r.left + square.width + 10;
860 
861  int i = 0;
862  for (const GRFConfig *c = this->actives; c != NULL; c = c->next, i++) {
863  if (this->vscroll->IsVisible(i)) {
864  const char *text = c->GetName();
865  bool h = (this->active_sel == c);
866  PaletteID pal = this->GetPalette(c);
867 
868  if (h) {
869  GfxFillRect(r.left + 1, y, r.right - 1, y + step_height - 1, PC_DARK_BLUE);
870  } else if (i == this->active_over) {
871  /* Get index of current selection. */
872  int active_sel_pos = 0;
873  for (GRFConfig *c = this->actives; c != NULL && c != this->active_sel; c = c->next, active_sel_pos++) {}
874  if (active_sel_pos != this->active_over) {
875  uint top = this->active_over < active_sel_pos ? y + 1 : y + step_height - 2;
876  GfxFillRect(r.left + WD_FRAMERECT_LEFT, top - 1, r.right - WD_FRAMERECT_RIGHT, top + 1, PC_GREY);
877  }
878  }
879  DrawSprite(SPR_SQUARE, pal, square_left, y + square_offset_y);
880  if (c->error != NULL) DrawSprite(SPR_WARNING_SIGN, 0, warning_left, y + warning_offset_y);
881  uint txtoffset = c->error == NULL ? 0 : warning.width;
882  DrawString(text_left + (rtl ? 0 : txtoffset), text_right - (rtl ? txtoffset : 0), y + offset_y, text, h ? TC_WHITE : TC_ORANGE);
883  y += step_height;
884  }
885  }
886  if (i == this->active_over && this->vscroll->IsVisible(i)) { // Highlight is after the last GRF entry.
887  GfxFillRect(r.left + WD_FRAMERECT_LEFT, y, r.right - WD_FRAMERECT_RIGHT, y + 2, PC_GREY);
888  }
889  break;
890  }
891 
892  case WID_NS_AVAIL_LIST: {
893  GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, this->active_over == -2 ? PC_DARK_GREY : PC_BLACK);
894 
895  uint step_height = this->GetWidget<NWidgetBase>(WID_NS_AVAIL_LIST)->resize_y;
896  int offset_y = (step_height - FONT_HEIGHT_NORMAL) / 2;
897  uint y = r.top + WD_FRAMERECT_TOP;
898  uint min_index = this->vscroll2->GetPosition();
899  uint max_index = min(min_index + this->vscroll2->GetCapacity(), this->avails.Length());
900 
901  for (uint i = min_index; i < max_index; i++) {
902  const GRFConfig *c = this->avails[i];
903  bool h = (c == this->avail_sel);
904  const char *text = c->GetName();
905 
906  if (h) GfxFillRect(r.left + 1, y, r.right - 1, y + step_height - 1, PC_DARK_BLUE);
907  DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y + offset_y, text, h ? TC_WHITE : TC_SILVER);
908  y += step_height;
909  }
910  break;
911  }
912 
914  /* Create the nice grayish rectangle at the details top. */
915  GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, PC_DARK_BLUE);
916  DrawString(r.left, r.right, (r.top + r.bottom - FONT_HEIGHT_NORMAL) / 2, STR_NEWGRF_SETTINGS_INFO_TITLE, TC_FROMSTRING, SA_HOR_CENTER);
917  break;
918 
919  case WID_NS_NEWGRF_INFO: {
920  const GRFConfig *selected = this->active_sel;
921  if (selected == NULL) selected = this->avail_sel;
922  if (selected != NULL) {
923  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);
924  }
925  break;
926  }
927  }
928  }
929 
930  virtual void OnClick(Point pt, int widget, int click_count)
931  {
932  if (widget >= WID_NS_NEWGRF_TEXTFILE && widget < WID_NS_NEWGRF_TEXTFILE + TFT_END) {
933  if (this->active_sel == NULL && this->avail_sel == NULL) return;
934 
935  ShowNewGRFTextfileWindow((TextfileType)(widget - WID_NS_NEWGRF_TEXTFILE), this->active_sel != NULL ? this->active_sel : this->avail_sel);
936  return;
937  }
938 
939  switch (widget) {
940  case WID_NS_PRESET_LIST: {
941  DropDownList *list = new DropDownList();
942 
943  /* Add 'None' option for clearing list */
944  *list->Append() = new DropDownListStringItem(STR_NONE, -1, false);
945 
946  for (uint i = 0; i < _grf_preset_list.Length(); i++) {
947  if (_grf_preset_list[i] != NULL) {
948  *list->Append() = new DropDownListPresetItem(i);
949  }
950  }
951 
952  this->DeleteChildWindows(WC_QUERY_STRING); // Remove the parameter query window
953  ShowDropDownList(this, list, this->preset, WID_NS_PRESET_LIST);
954  break;
955  }
956 
957  case WID_NS_OPEN_URL: {
958  const GRFConfig *c = (this->avail_sel == NULL) ? this->active_sel : this->avail_sel;
959 
960  extern void OpenBrowser(const char *url);
961  OpenBrowser(c->GetURL());
962  break;
963  }
964 
965  case WID_NS_PRESET_SAVE:
966  ShowSavePresetWindow((this->preset == -1) ? NULL : _grf_preset_list[this->preset]);
967  break;
968 
970  if (this->preset == -1) return;
971 
974  this->preset = -1;
975  this->InvalidateData();
976  this->DeleteChildWindows(WC_QUERY_STRING); // Remove the parameter query window
977  break;
978 
979  case WID_NS_MOVE_UP: { // Move GRF up
980  if (this->active_sel == NULL || !this->editable) break;
981 
982  int pos = 0;
983  for (GRFConfig **pc = &this->actives; *pc != NULL; pc = &(*pc)->next, pos++) {
984  GRFConfig *c = *pc;
985  if (c->next == this->active_sel) {
986  c->next = this->active_sel->next;
987  this->active_sel->next = c;
988  *pc = this->active_sel;
989  break;
990  }
991  }
992  this->vscroll->ScrollTowards(pos);
993  this->preset = -1;
994  this->InvalidateData();
995  break;
996  }
997 
998  case WID_NS_MOVE_DOWN: { // Move GRF down
999  if (this->active_sel == NULL || !this->editable) break;
1000 
1001  int pos = 1; // Start at 1 as we swap the selected newgrf with the next one
1002  for (GRFConfig **pc = &this->actives; *pc != NULL; pc = &(*pc)->next, pos++) {
1003  GRFConfig *c = *pc;
1004  if (c == this->active_sel) {
1005  *pc = c->next;
1006  c->next = c->next->next;
1007  (*pc)->next = c;
1008  break;
1009  }
1010  }
1011  this->vscroll->ScrollTowards(pos);
1012  this->preset = -1;
1013  this->InvalidateData();
1014  break;
1015  }
1016 
1017  case WID_NS_FILE_LIST: { // Select an active GRF.
1019 
1020  uint i = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_NS_FILE_LIST);
1021 
1022  GRFConfig *c;
1023  for (c = this->actives; c != NULL && i > 0; c = c->next, i--) {}
1024 
1025  if (this->active_sel != c) DeleteWindowByClass(WC_GRF_PARAMETERS);
1026  this->active_sel = c;
1027  this->avail_sel = NULL;
1028  this->avail_pos = -1;
1029 
1030  this->InvalidateData();
1031  if (click_count == 1) {
1032  if (this->editable && this->active_sel != NULL) SetObjectToPlaceWnd(SPR_CURSOR_MOUSE, PAL_NONE, HT_DRAG, this);
1033  break;
1034  }
1035  /* FALL THROUGH, with double click. */
1036  }
1037 
1038  case WID_NS_REMOVE: { // Remove GRF
1039  if (this->active_sel == NULL || !this->editable) break;
1041 
1042  /* Choose the next GRF file to be the selected file. */
1043  GRFConfig *newsel = this->active_sel->next;
1044  for (GRFConfig **pc = &this->actives; *pc != NULL; pc = &(*pc)->next) {
1045  GRFConfig *c = *pc;
1046  /* If the new selection is empty (i.e. we're deleting the last item
1047  * in the list, pick the file just before the selected file */
1048  if (newsel == NULL && c->next == this->active_sel) newsel = c;
1049 
1050  if (c == this->active_sel) {
1051  if (newsel == c) newsel = NULL;
1052 
1053  *pc = c->next;
1054  delete c;
1055  break;
1056  }
1057  }
1058 
1059  this->active_sel = newsel;
1060  this->preset = -1;
1061  this->avail_pos = -1;
1062  this->avail_sel = NULL;
1063  this->avails.ForceRebuild();
1065  break;
1066  }
1067 
1068  case WID_NS_UPGRADE: { // Upgrade GRF.
1069  if (!this->editable || this->actives == NULL) break;
1070  UpgradeCurrent();
1072  break;
1073  }
1074 
1075  case WID_NS_AVAIL_LIST: { // Select a non-active GRF.
1077 
1078  uint i = this->vscroll2->GetScrolledRowFromWidget(pt.y, this, WID_NS_AVAIL_LIST);
1079  this->active_sel = NULL;
1081  if (i < this->avails.Length()) {
1082  this->avail_sel = this->avails[i];
1083  this->avail_pos = i;
1084  }
1085  this->InvalidateData();
1086  if (click_count == 1) {
1087  if (this->editable && this->avail_sel != NULL && !HasBit(this->avail_sel->flags, GCF_INVALID)) SetObjectToPlaceWnd(SPR_CURSOR_MOUSE, PAL_NONE, HT_DRAG, this);
1088  break;
1089  }
1090  /* FALL THROUGH, with double click. */
1091  }
1092 
1093  case WID_NS_ADD:
1094  if (this->avail_sel == NULL || !this->editable || HasBit(this->avail_sel->flags, GCF_INVALID)) break;
1095 
1096  this->AddGRFToActive();
1097  break;
1098 
1099  case WID_NS_APPLY_CHANGES: // Apply changes made to GRF list
1100  if (!this->editable) break;
1101  if (this->execute) {
1102  ShowQuery(
1103  STR_NEWGRF_POPUP_CAUTION_CAPTION,
1104  STR_NEWGRF_CONFIRMATION_TEXT,
1105  this,
1107  );
1108  } else {
1109  CopyGRFConfigList(this->orig_list, this->actives, true);
1110  ResetGRFConfig(false);
1111  ReloadNewGRFData();
1112  }
1113  this->DeleteChildWindows(WC_QUERY_STRING); // Remove the parameter query window
1114  break;
1115 
1117  case WID_NS_SET_PARAMETERS: { // Edit parameters
1118  if (this->active_sel == NULL || !this->show_params || this->active_sel->num_valid_params == 0) break;
1119 
1120  OpenGRFParameterWindow(this->active_sel, this->editable);
1121  break;
1122  }
1123 
1124  case WID_NS_TOGGLE_PALETTE:
1125  if (this->active_sel != NULL && this->editable) {
1126  this->active_sel->palette ^= GRFP_USE_MASK;
1127  this->SetDirty();
1128  }
1129  break;
1130 
1133  if (!_network_available) {
1134  ShowErrorMessage(STR_NETWORK_ERROR_NOTAVAILABLE, INVALID_STRING_ID, WL_ERROR);
1135  } else {
1136 #if defined(ENABLE_NETWORK)
1137  this->DeleteChildWindows(WC_QUERY_STRING); // Remove the parameter query window
1138 
1139  ShowMissingContentWindow(this->actives);
1140 #endif
1141  }
1142  break;
1143 
1144  case WID_NS_RESCAN_FILES:
1145  case WID_NS_RESCAN_FILES2:
1146  ScanNewGRFFiles(this);
1147  break;
1148  }
1149  }
1150 
1151  virtual void OnNewGRFsScanned()
1152  {
1153  this->avail_sel = NULL;
1154  this->avail_pos = -1;
1155  this->avails.ForceRebuild();
1156  this->DeleteChildWindows(WC_QUERY_STRING); // Remove the parameter query window
1157  this->DeleteChildWindows(WC_TEXTFILE); // Remove the view textfile window
1158  }
1159 
1160  virtual void OnDropdownSelect(int widget, int index)
1161  {
1162  if (!this->editable) return;
1163 
1164  ClearGRFConfigList(&this->actives);
1165  this->preset = index;
1166 
1167  if (index != -1) {
1168  this->actives = LoadGRFPresetFromConfig(_grf_preset_list[index]);
1169  }
1170  this->avails.ForceRebuild();
1171 
1174  this->active_sel = NULL;
1176  }
1177 
1178  virtual void OnQueryTextFinished(char *str)
1179  {
1180  if (str == NULL) return;
1181 
1182  SaveGRFPresetToConfig(str, this->actives);
1184 
1185  /* Switch to this preset */
1186  for (uint i = 0; i < _grf_preset_list.Length(); i++) {
1187  if (_grf_preset_list[i] != NULL && strcmp(_grf_preset_list[i], str) == 0) {
1188  this->preset = i;
1189  break;
1190  }
1191  }
1192 
1193  this->InvalidateData();
1194  }
1195 
1201  virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
1202  {
1203  if (!gui_scope) return;
1204  switch (data) {
1205  default:
1206  /* Nothing important to do */
1207  break;
1208 
1209  case GOID_NEWGRF_RESCANNED:
1210  /* Search the list for items that are now found and mark them as such. */
1211  for (GRFConfig **l = &this->actives; *l != NULL; l = &(*l)->next) {
1212  GRFConfig *c = *l;
1213  bool compatible = HasBit(c->flags, GCF_COMPATIBLE);
1214  if (c->status != GCS_NOT_FOUND && !compatible) continue;
1215 
1216  const GRFConfig *f = FindGRFConfig(c->ident.grfid, FGCM_EXACT, compatible ? c->original_md5sum : c->ident.md5sum);
1217  if (f == NULL || HasBit(f->flags, GCF_INVALID)) continue;
1218 
1219  *l = new GRFConfig(*f);
1220  (*l)->next = c->next;
1221 
1222  if (active_sel == c) active_sel = *l;
1223 
1224  delete c;
1225  }
1226 
1227  this->avails.ForceRebuild();
1228  /* FALL THROUGH */
1230  this->preset = -1;
1231  /* FALL THROUGH */
1233  /* Update scrollbars */
1234  int i = 0;
1235  for (const GRFConfig *c = this->actives; c != NULL; c = c->next, i++) {}
1236 
1237  this->vscroll->SetCount(i + 1); // Reserve empty space for drag and drop handling.
1238 
1239  if (this->avail_pos >= 0) this->vscroll2->ScrollTowards(this->avail_pos);
1240  break;
1241  }
1242  }
1243 
1244  this->BuildAvailables();
1245 
1246  this->SetWidgetsDisabledState(!this->editable,
1251  );
1252  this->SetWidgetDisabledState(WID_NS_ADD, !this->editable || this->avail_sel == NULL || HasBit(this->avail_sel->flags, GCF_INVALID));
1253  this->SetWidgetDisabledState(WID_NS_UPGRADE, !this->editable || this->actives == NULL || !this->CanUpgradeCurrent());
1254 
1255  bool disable_all = this->active_sel == NULL || !this->editable;
1256  this->SetWidgetsDisabledState(disable_all,
1257  WID_NS_REMOVE,
1261  );
1262 
1263  const GRFConfig *c = (this->avail_sel == NULL) ? this->active_sel : this->avail_sel;
1264  for (TextfileType tft = TFT_BEGIN; tft < TFT_END; tft++) {
1265  this->SetWidgetDisabledState(WID_NS_NEWGRF_TEXTFILE + tft, c == NULL || c->GetTextfile(tft) == NULL);
1266  }
1267  this->SetWidgetDisabledState(WID_NS_OPEN_URL, c == NULL || StrEmpty(c->GetURL()));
1268 
1269  this->SetWidgetDisabledState(WID_NS_SET_PARAMETERS, !this->show_params || this->active_sel == NULL || this->active_sel->num_valid_params == 0);
1270  this->SetWidgetDisabledState(WID_NS_VIEW_PARAMETERS, !this->show_params || this->active_sel == NULL || this->active_sel->num_valid_params == 0);
1271  this->SetWidgetDisabledState(WID_NS_TOGGLE_PALETTE, disable_all ||
1273 
1274  if (!disable_all) {
1275  /* All widgets are now enabled, so disable widgets we can't use */
1276  if (this->active_sel == this->actives) this->DisableWidget(WID_NS_MOVE_UP);
1277  if (this->active_sel->next == NULL) this->DisableWidget(WID_NS_MOVE_DOWN);
1278  }
1279 
1280  this->SetWidgetDisabledState(WID_NS_PRESET_DELETE, this->preset == -1);
1281 
1282  bool has_missing = false;
1283  bool has_compatible = false;
1284  for (const GRFConfig *c = this->actives; !has_missing && c != NULL; c = c->next) {
1285  has_missing |= c->status == GCS_NOT_FOUND;
1286  has_compatible |= HasBit(c->flags, GCF_COMPATIBLE);
1287  }
1288  uint32 widget_data;
1289  StringID tool_tip;
1290  if (has_missing || has_compatible) {
1291  widget_data = STR_NEWGRF_SETTINGS_FIND_MISSING_CONTENT_BUTTON;
1292  tool_tip = STR_NEWGRF_SETTINGS_FIND_MISSING_CONTENT_TOOLTIP;
1293  } else {
1294  widget_data = STR_INTRO_ONLINE_CONTENT;
1295  tool_tip = STR_INTRO_TOOLTIP_ONLINE_CONTENT;
1296  }
1297  this->GetWidget<NWidgetCore>(WID_NS_CONTENT_DOWNLOAD)->widget_data = widget_data;
1298  this->GetWidget<NWidgetCore>(WID_NS_CONTENT_DOWNLOAD)->tool_tip = tool_tip;
1299  this->GetWidget<NWidgetCore>(WID_NS_CONTENT_DOWNLOAD2)->widget_data = widget_data;
1300  this->GetWidget<NWidgetCore>(WID_NS_CONTENT_DOWNLOAD2)->tool_tip = tool_tip;
1301 
1302  this->SetWidgetDisabledState(WID_NS_PRESET_SAVE, has_missing);
1303  }
1304 
1305  virtual EventState OnKeyPress(WChar key, uint16 keycode)
1306  {
1307  if (!this->editable) return ES_NOT_HANDLED;
1308 
1309  switch (keycode) {
1310  case WKC_UP:
1311  /* scroll up by one */
1312  if (this->avail_pos > 0) this->avail_pos--;
1313  break;
1314 
1315  case WKC_DOWN:
1316  /* scroll down by one */
1317  if (this->avail_pos < (int)this->avails.Length() - 1) this->avail_pos++;
1318  break;
1319 
1320  case WKC_PAGEUP:
1321  /* scroll up a page */
1322  this->avail_pos = (this->avail_pos < this->vscroll2->GetCapacity()) ? 0 : this->avail_pos - this->vscroll2->GetCapacity();
1323  break;
1324 
1325  case WKC_PAGEDOWN:
1326  /* scroll down a page */
1327  this->avail_pos = min(this->avail_pos + this->vscroll2->GetCapacity(), (int)this->avails.Length() - 1);
1328  break;
1329 
1330  case WKC_HOME:
1331  /* jump to beginning */
1332  this->avail_pos = 0;
1333  break;
1334 
1335  case WKC_END:
1336  /* jump to end */
1337  this->avail_pos = this->avails.Length() - 1;
1338  break;
1339 
1340  default:
1341  return ES_NOT_HANDLED;
1342  }
1343 
1344  if (this->avails.Length() == 0) this->avail_pos = -1;
1345  if (this->avail_pos >= 0) {
1346  this->avail_sel = this->avails[this->avail_pos];
1347  this->vscroll2->ScrollTowards(this->avail_pos);
1348  this->InvalidateData(0);
1349  }
1350 
1351  return ES_HANDLED;
1352  }
1353 
1354  virtual void OnEditboxChanged(int wid)
1355  {
1356  if (!this->editable) return;
1357 
1358  string_filter.SetFilterTerm(this->filter_editbox.text.buf);
1359  this->avails.SetFilterState(!string_filter.IsEmpty());
1360  this->avails.ForceRebuild();
1361  this->InvalidateData(0);
1362  }
1363 
1364  virtual void OnDragDrop(Point pt, int widget)
1365  {
1366  if (!this->editable) return;
1367 
1368  if (widget == WID_NS_FILE_LIST) {
1369  if (this->active_sel != NULL) {
1370  /* Get pointer to the selected file in the active list. */
1371  int from_pos = 0;
1372  GRFConfig **from_prev;
1373  for (from_prev = &this->actives; *from_prev != this->active_sel; from_prev = &(*from_prev)->next, from_pos++) {}
1374 
1375  /* Gets the drag-and-drop destination offset. Ignore the last dummy line. */
1376  int to_pos = min(this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_NS_FILE_LIST), this->vscroll->GetCount() - 2);
1377  if (to_pos != from_pos) { // Don't move NewGRF file over itself.
1378  /* Get pointer to destination position. */
1379  GRFConfig **to_prev = &this->actives;
1380  for (int i = from_pos < to_pos ? -1 : 0; *to_prev != NULL && i < to_pos; to_prev = &(*to_prev)->next, i++) {}
1381 
1382  /* Detach NewGRF file from its original position. */
1383  *from_prev = this->active_sel->next;
1384 
1385  /* Attach NewGRF file to its new position. */
1386  this->active_sel->next = *to_prev;
1387  *to_prev = this->active_sel;
1388 
1389  this->vscroll->ScrollTowards(to_pos);
1390  this->preset = -1;
1391  this->InvalidateData();
1392  }
1393  } else if (this->avail_sel != NULL) {
1394  int to_pos = min(this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_NS_FILE_LIST), this->vscroll->GetCount() - 1);
1395  this->AddGRFToActive(to_pos);
1396  }
1397  } else if (widget == WID_NS_AVAIL_LIST && this->active_sel != NULL) {
1398  /* Remove active NewGRF file by dragging it over available list. */
1399  Point dummy = {-1, -1};
1400  this->OnClick(dummy, WID_NS_REMOVE, 1);
1401  }
1402 
1404 
1405  if (this->active_over != -1) {
1406  /* End of drag-and-drop, hide dragged destination highlight. */
1407  this->SetWidgetDirty(this->active_over == -2 ? WID_NS_AVAIL_LIST : WID_NS_FILE_LIST);
1408  this->active_over = -1;
1409  }
1410  }
1411 
1412  virtual void OnMouseDrag(Point pt, int widget)
1413  {
1414  if (!this->editable) return;
1415 
1416  if (widget == WID_NS_FILE_LIST && (this->active_sel != NULL || this->avail_sel != NULL)) {
1417  /* An NewGRF file is dragged over the active list. */
1418  int to_pos = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_NS_FILE_LIST);
1419  /* Skip the last dummy line if the source is from the active list. */
1420  to_pos = min(to_pos, this->vscroll->GetCount() - (this->active_sel != NULL ? 2 : 1));
1421 
1422  if (to_pos != this->active_over) {
1423  this->active_over = to_pos;
1425  }
1426  } else if (widget == WID_NS_AVAIL_LIST && this->active_sel != NULL) {
1427  this->active_over = -2;
1429  } else if (this->active_over != -1) {
1430  this->SetWidgetDirty(this->active_over == -2 ? WID_NS_AVAIL_LIST : WID_NS_FILE_LIST);
1431  this->active_over = -1;
1432  }
1433  }
1434 
1435 private:
1437  static int CDECL NameSorter(const GRFConfig * const *a, const GRFConfig * const *b)
1438  {
1439  int i = strnatcmp((*a)->GetName(), (*b)->GetName(), true); // Sort by name (natural sorting).
1440  if (i != 0) return i;
1441 
1442  i = (*a)->version - (*b)->version;
1443  if (i != 0) return i;
1444 
1445  return memcmp((*a)->ident.md5sum, (*b)->ident.md5sum, lengthof((*b)->ident.md5sum));
1446  }
1447 
1449  static bool CDECL TagNameFilter(const GRFConfig * const *a, StringFilter &filter)
1450  {
1451  filter.ResetState();
1452  filter.AddLine((*a)->GetName());
1453  filter.AddLine((*a)->filename);
1454  filter.AddLine((*a)->GetDescription());
1455  return filter.GetState();;
1456  }
1457 
1458  void BuildAvailables()
1459  {
1460  if (!this->avails.NeedRebuild()) return;
1461 
1462  this->avails.Clear();
1463 
1464  for (const GRFConfig *c = _all_grfs; c != NULL; c = c->next) {
1465  bool found = false;
1466  for (const GRFConfig *grf = this->actives; grf != NULL && !found; grf = grf->next) found = grf->ident.HasGrfIdentifier(c->ident.grfid, c->ident.md5sum);
1467  if (found) continue;
1468 
1470  *this->avails.Append() = c;
1471  } else {
1473  /*
1474  * If the best version is 0, then all NewGRF with this GRF ID
1475  * have version 0, so for backward compatibility reasons we
1476  * want to show them all.
1477  * If we are the best version, then we definitely want to
1478  * show that NewGRF!.
1479  */
1480  if (best->version == 0 || best->ident.HasGrfIdentifier(c->ident.grfid, c->ident.md5sum)) {
1481  *this->avails.Append() = c;
1482  }
1483  }
1484  }
1485 
1486  this->avails.Filter(this->string_filter);
1487  this->avails.Compact();
1488  this->avails.RebuildDone();
1489  this->avails.Sort();
1490 
1491  if (this->avail_sel != NULL) {
1492  this->avail_pos = this->avails.FindIndex(this->avail_sel);
1493  if (this->avail_pos < 0) this->avail_sel = NULL;
1494  }
1495 
1496  this->vscroll2->SetCount(this->avails.Length()); // Update the scrollbar
1497  }
1498 
1504  bool AddGRFToActive(int ins_pos = -1)
1505  {
1506  if (this->avail_sel == NULL || !this->editable || HasBit(this->avail_sel->flags, GCF_INVALID)) return false;
1507 
1508  uint count = 0;
1509  GRFConfig **entry = NULL;
1510  GRFConfig **list;
1511  /* Find last entry in the list, checking for duplicate grfid on the way */
1512  for (list = &this->actives; *list != NULL; list = &(*list)->next, ins_pos--) {
1513  if (ins_pos == 0) entry = list; // Insert position? Save.
1514  if ((*list)->ident.grfid == this->avail_sel->ident.grfid) {
1515  ShowErrorMessage(STR_NEWGRF_DUPLICATE_GRFID, INVALID_STRING_ID, WL_INFO);
1516  return false;
1517  }
1518  if (!HasBit((*list)->flags, GCF_STATIC)) count++;
1519  }
1520  if (entry == NULL) entry = list;
1521  if (count >= NETWORK_MAX_GRF_COUNT) {
1522  ShowErrorMessage(STR_NEWGRF_TOO_MANY_NEWGRFS, INVALID_STRING_ID, WL_INFO);
1523  return false;
1524  }
1525 
1526  GRFConfig *c = new GRFConfig(*this->avail_sel); // Copy GRF details from scanned list.
1527  c->SetParameterDefaults();
1528 
1529  /* Insert GRF config to configuration list. */
1530  c->next = *entry;
1531  *entry = c;
1532 
1533  /* Select next (or previous, if last one) item in the list. */
1534  int new_pos = this->avail_pos + 1;
1535  if (new_pos >= (int)this->avails.Length()) new_pos = this->avail_pos - 1;
1536  this->avail_pos = new_pos;
1537  if (new_pos >= 0) this->avail_sel = this->avails[new_pos];
1538 
1539  this->avails.ForceRebuild();
1541  return true;
1542  }
1543 };
1544 
1545 #if defined(ENABLE_NETWORK)
1546 
1551 {
1552  /* Only show the things in the current list, or everything when nothing's selected */
1553  ContentVector cv;
1554  for (const GRFConfig *c = list; c != NULL; c = c->next) {
1555  if (c->status != GCS_NOT_FOUND && !HasBit(c->flags, GCF_COMPATIBLE)) continue;
1556 
1557  ContentInfo *ci = new ContentInfo();
1558  ci->type = CONTENT_TYPE_NEWGRF;
1560  strecpy(ci->name, c->GetName(), lastof(ci->name));
1561  ci->unique_id = BSWAP32(c->ident.grfid);
1562  memcpy(ci->md5sum, HasBit(c->flags, GCF_COMPATIBLE) ? c->original_md5sum : c->ident.md5sum, sizeof(ci->md5sum));
1563  *cv.Append() = ci;
1564  }
1565  ShowNetworkContentListWindow(cv.Length() == 0 ? NULL : &cv, CONTENT_TYPE_NEWGRF);
1566 }
1567 #endif
1568 
1569 Listing NewGRFWindow::last_sorting = {false, 0};
1571 
1573  &NameSorter,
1574 };
1575 
1577  &TagNameFilter,
1578 };
1579 
1587 public:
1588  static const uint INTER_LIST_SPACING;
1589  static const uint INTER_COLUMN_SPACING;
1590  static const uint MAX_EXTRA_INFO_WIDTH;
1591  static const uint MIN_EXTRA_FOR_3_COLUMNS;
1592 
1596  bool editable;
1597 
1599  {
1600  this->avs = avs;
1601  this->acs = acs;
1602  this->inf = inf;
1603 
1604  this->Add(this->avs);
1605  this->Add(this->acs);
1606  this->Add(this->inf);
1607 
1608  this->editable = true; // Temporary setting, 'real' value is set in SetupSmallestSize().
1609  }
1610 
1611  virtual void SetupSmallestSize(Window *w, bool init_array)
1612  {
1613  /* Copy state flag from the window. */
1614  assert(dynamic_cast<NewGRFWindow *>(w) != NULL);
1615  NewGRFWindow *ngw = (NewGRFWindow *)w;
1616  this->editable = ngw->editable;
1617 
1618  this->avs->SetupSmallestSize(w, init_array);
1619  this->acs->SetupSmallestSize(w, init_array);
1620  this->inf->SetupSmallestSize(w, init_array);
1621 
1622  uint min_avs_width = this->avs->smallest_x + this->avs->padding_left + this->avs->padding_right;
1623  uint min_acs_width = this->acs->smallest_x + this->acs->padding_left + this->acs->padding_right;
1624  uint min_inf_width = this->inf->smallest_x + this->inf->padding_left + this->inf->padding_right;
1625 
1626  uint min_avs_height = this->avs->smallest_y + this->avs->padding_top + this->avs->padding_bottom;
1627  uint min_acs_height = this->acs->smallest_y + this->acs->padding_top + this->acs->padding_bottom;
1628  uint min_inf_height = this->inf->smallest_y + this->inf->padding_top + this->inf->padding_bottom;
1629 
1630  /* Smallest window is in two column mode. */
1631  this->smallest_x = max(min_avs_width, min_acs_width) + INTER_COLUMN_SPACING + min_inf_width;
1632  this->smallest_y = max(min_inf_height, min_acs_height + INTER_LIST_SPACING + min_avs_height);
1633 
1634  /* Filling. */
1635  this->fill_x = LeastCommonMultiple(this->avs->fill_x, this->acs->fill_x);
1636  if (this->inf->fill_x > 0 && (this->fill_x == 0 || this->fill_x > this->inf->fill_x)) this->fill_x = this->inf->fill_x;
1637 
1638  this->fill_y = this->avs->fill_y;
1639  if (this->acs->fill_y > 0 && (this->fill_y == 0 || this->fill_y > this->acs->fill_y)) this->fill_y = this->acs->fill_y;
1640  this->fill_y = LeastCommonMultiple(this->fill_y, this->inf->fill_y);
1641 
1642  /* Resizing. */
1643  this->resize_x = LeastCommonMultiple(this->avs->resize_x, this->acs->resize_x);
1644  if (this->inf->resize_x > 0 && (this->resize_x == 0 || this->resize_x > this->inf->resize_x)) this->resize_x = this->inf->resize_x;
1645 
1646  this->resize_y = this->avs->resize_y;
1647  if (this->acs->resize_y > 0 && (this->resize_y == 0 || this->resize_y > this->acs->resize_y)) this->resize_y = this->acs->resize_y;
1648  this->resize_y = LeastCommonMultiple(this->resize_y, this->inf->resize_y);
1649 
1650  /* Make sure the height suits the 3 column (resp. not-editable) format; the 2 column format can easily fill space between the lists */
1651  this->smallest_y = ComputeMaxSize(min_acs_height, this->smallest_y + this->resize_y - 1, this->resize_y);
1652  }
1653 
1654  virtual void AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)
1655  {
1656  this->StoreSizePosition(sizing, x, y, given_width, given_height);
1657 
1658  uint min_avs_width = this->avs->smallest_x + this->avs->padding_left + this->avs->padding_right;
1659  uint min_acs_width = this->acs->smallest_x + this->acs->padding_left + this->acs->padding_right;
1660  uint min_inf_width = this->inf->smallest_x + this->inf->padding_left + this->inf->padding_right;
1661 
1662  uint min_list_width = max(min_avs_width, min_acs_width); // Smallest width of the lists such that they have equal width (incl padding).
1663  uint avs_extra_width = min_list_width - min_avs_width; // Additional width needed for avs to reach min_list_width.
1664  uint acs_extra_width = min_list_width - min_acs_width; // Additional width needed for acs to reach min_list_width.
1665 
1666  /* Use 2 or 3 columns? */
1667  uint min_three_columns = min_avs_width + min_acs_width + min_inf_width + 2 * INTER_COLUMN_SPACING;
1668  uint min_two_columns = min_list_width + min_inf_width + INTER_COLUMN_SPACING;
1669  bool use_three_columns = this->editable && (min_three_columns + MIN_EXTRA_FOR_3_COLUMNS <= given_width);
1670 
1671  /* Info panel is a separate column in both modes. Compute its width first. */
1672  uint extra_width, inf_width;
1673  if (use_three_columns) {
1674  extra_width = given_width - min_three_columns;
1675  inf_width = min(MAX_EXTRA_INFO_WIDTH, extra_width / 2);
1676  } else {
1677  extra_width = given_width - min_two_columns;
1678  inf_width = min(MAX_EXTRA_INFO_WIDTH, extra_width / 2);
1679  }
1680  inf_width = ComputeMaxSize(this->inf->smallest_x, this->inf->smallest_x + inf_width, this->inf->GetHorizontalStepSize(sizing));
1681  extra_width -= inf_width - this->inf->smallest_x;
1682 
1683  uint inf_height = ComputeMaxSize(this->inf->smallest_y, given_height, this->inf->GetVerticalStepSize(sizing));
1684 
1685  if (use_three_columns) {
1686  /* Three column display, first make both lists equally wide, then divide whatever is left between both lists.
1687  * Only keep track of what avs gets, all other space goes to acs. */
1688  uint avs_width = min(avs_extra_width, extra_width);
1689  extra_width -= avs_width;
1690  extra_width -= min(acs_extra_width, extra_width);
1691  avs_width += extra_width / 2;
1692 
1693  avs_width = ComputeMaxSize(this->avs->smallest_x, this->avs->smallest_x + avs_width, this->avs->GetHorizontalStepSize(sizing));
1694 
1695  uint acs_width = given_width - // Remaining space, including horizontal padding.
1696  inf_width - this->inf->padding_left - this->inf->padding_right -
1697  avs_width - this->avs->padding_left - this->avs->padding_right - 2 * INTER_COLUMN_SPACING;
1698  acs_width = ComputeMaxSize(min_acs_width, acs_width, this->acs->GetHorizontalStepSize(sizing)) -
1699  this->acs->padding_left - this->acs->padding_right;
1700 
1701  /* Never use fill_y on these; the minimal size is chosen, so that the 3 column view looks nice */
1702  uint avs_height = ComputeMaxSize(this->avs->smallest_y, given_height, this->avs->resize_y);
1703  uint acs_height = ComputeMaxSize(this->acs->smallest_y, given_height, this->acs->resize_y);
1704 
1705  /* Assign size and position to the children. */
1706  if (rtl) {
1707  x += this->inf->padding_left;
1708  this->inf->AssignSizePosition(sizing, x, y + this->inf->padding_top, inf_width, inf_height, rtl);
1709  x += inf_width + this->inf->padding_right + INTER_COLUMN_SPACING;
1710  } else {
1711  x += this->avs->padding_left;
1712  this->avs->AssignSizePosition(sizing, x, y + this->avs->padding_top, avs_width, avs_height, rtl);
1713  x += avs_width + this->avs->padding_right + INTER_COLUMN_SPACING;
1714  }
1715 
1716  x += this->acs->padding_left;
1717  this->acs->AssignSizePosition(sizing, x, y + this->acs->padding_top, acs_width, acs_height, rtl);
1718  x += acs_width + this->acs->padding_right + INTER_COLUMN_SPACING;
1719 
1720  if (rtl) {
1721  x += this->avs->padding_left;
1722  this->avs->AssignSizePosition(sizing, x, y + this->avs->padding_top, avs_width, avs_height, rtl);
1723  } else {
1724  x += this->inf->padding_left;
1725  this->inf->AssignSizePosition(sizing, x, y + this->inf->padding_top, inf_width, inf_height, rtl);
1726  }
1727  } else {
1728  /* Two columns, all space in extra_width goes to both lists. Since the lists are underneath each other,
1729  * the column is min_list_width wide at least. */
1730  uint avs_width = ComputeMaxSize(this->avs->smallest_x, this->avs->smallest_x + avs_extra_width + extra_width,
1731  this->avs->GetHorizontalStepSize(sizing));
1732  uint acs_width = ComputeMaxSize(this->acs->smallest_x, this->acs->smallest_x + acs_extra_width + extra_width,
1733  this->acs->GetHorizontalStepSize(sizing));
1734 
1735  uint min_avs_height = (!this->editable) ? 0 : this->avs->smallest_y + this->avs->padding_top + this->avs->padding_bottom + INTER_LIST_SPACING;
1736  uint min_acs_height = this->acs->smallest_y + this->acs->padding_top + this->acs->padding_bottom;
1737  uint extra_height = given_height - min_acs_height - min_avs_height;
1738 
1739  /* Never use fill_y on these; instead use the INTER_LIST_SPACING as filler */
1740  uint avs_height = ComputeMaxSize(this->avs->smallest_y, this->avs->smallest_y + extra_height / 2, this->avs->resize_y);
1741  if (this->editable) extra_height -= avs_height - this->avs->smallest_y;
1742  uint acs_height = ComputeMaxSize(this->acs->smallest_y, this->acs->smallest_y + extra_height, this->acs->resize_y);
1743 
1744  /* Assign size and position to the children. */
1745  if (rtl) {
1746  x += this->inf->padding_left;
1747  this->inf->AssignSizePosition(sizing, x, y + this->inf->padding_top, inf_width, inf_height, rtl);
1748  x += inf_width + this->inf->padding_right + INTER_COLUMN_SPACING;
1749 
1750  this->acs->AssignSizePosition(sizing, x + this->acs->padding_left, y + this->acs->padding_top, acs_width, acs_height, rtl);
1751  if (this->editable) {
1752  this->avs->AssignSizePosition(sizing, x + this->avs->padding_left, y + given_height - avs_height - this->avs->padding_bottom, avs_width, avs_height, rtl);
1753  } else {
1754  this->avs->AssignSizePosition(sizing, 0, 0, this->avs->smallest_x, this->avs->smallest_y, rtl);
1755  }
1756  } else {
1757  this->acs->AssignSizePosition(sizing, x + this->acs->padding_left, y + this->acs->padding_top, acs_width, acs_height, rtl);
1758  if (this->editable) {
1759  this->avs->AssignSizePosition(sizing, x + this->avs->padding_left, y + given_height - avs_height - this->avs->padding_bottom, avs_width, avs_height, rtl);
1760  } else {
1761  this->avs->AssignSizePosition(sizing, 0, 0, this->avs->smallest_x, this->avs->smallest_y, rtl);
1762  }
1763  uint dx = this->acs->current_x + this->acs->padding_left + this->acs->padding_right;
1764  if (this->editable) {
1765  dx = max(dx, this->avs->current_x + this->avs->padding_left + this->avs->padding_right);
1766  }
1767  x += dx + INTER_COLUMN_SPACING + this->inf->padding_left;
1768  this->inf->AssignSizePosition(sizing, x, y + this->inf->padding_top, inf_width, inf_height, rtl);
1769  }
1770  }
1771  }
1772 
1773  virtual NWidgetCore *GetWidgetFromPos(int x, int y)
1774  {
1775  if (!IsInsideBS(x, this->pos_x, this->current_x) || !IsInsideBS(y, this->pos_y, this->current_y)) return NULL;
1776 
1777  NWidgetCore *nw = (this->editable) ? this->avs->GetWidgetFromPos(x, y) : NULL;
1778  if (nw == NULL) nw = this->acs->GetWidgetFromPos(x, y);
1779  if (nw == NULL) nw = this->inf->GetWidgetFromPos(x, y);
1780  return nw;
1781  }
1782 
1783  virtual void Draw(const Window *w)
1784  {
1785  if (this->editable) this->avs->Draw(w);
1786  this->acs->Draw(w);
1787  this->inf->Draw(w);
1788  }
1789 };
1790 
1795 
1796 static const NWidgetPart _nested_newgrf_actives_widgets[] = {
1797  /* Left side, presets. */
1799  NWidget(WWT_TEXT, COLOUR_MAUVE), SetDataTip(STR_NEWGRF_SETTINGS_SELECT_PRESET, STR_NULL),
1800  SetPadding(0, WD_FRAMETEXT_RIGHT, 0, 0),
1801  NWidget(WWT_DROPDOWN, COLOUR_YELLOW, WID_NS_PRESET_LIST), SetFill(1, 0), SetResize(1, 0),
1802  SetDataTip(STR_JUST_STRING, STR_NEWGRF_SETTINGS_PRESET_LIST_TOOLTIP),
1803  EndContainer(),
1805  NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_PRESET_SAVE), SetFill(1, 0), SetResize(1, 0),
1806  SetDataTip(STR_NEWGRF_SETTINGS_PRESET_SAVE, STR_NEWGRF_SETTINGS_PRESET_SAVE_TOOLTIP),
1807  NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_PRESET_DELETE), SetFill(1, 0), SetResize(1, 0),
1808  SetDataTip(STR_NEWGRF_SETTINGS_PRESET_DELETE, STR_NEWGRF_SETTINGS_PRESET_DELETE_TOOLTIP),
1809  EndContainer(),
1810 
1812  NWidget(WWT_PANEL, COLOUR_MAUVE),
1813  NWidget(WWT_LABEL, COLOUR_MAUVE), SetDataTip(STR_NEWGRF_SETTINGS_ACTIVE_LIST, STR_NULL),
1815  /* Left side, active grfs. */
1816  NWidget(NWID_HORIZONTAL), SetPadding(0, 2, 0, 2),
1817  NWidget(WWT_PANEL, COLOUR_MAUVE),
1818  NWidget(WWT_INSET, COLOUR_MAUVE, WID_NS_FILE_LIST), SetMinimalSize(100, 1), SetPadding(2, 2, 2, 2),
1819  SetFill(1, 1), SetResize(1, 1), SetScrollbar(WID_NS_SCROLLBAR), SetDataTip(STR_NULL, STR_NEWGRF_SETTINGS_FILE_TOOLTIP),
1820  EndContainer(),
1821  EndContainer(),
1822  NWidget(NWID_VSCROLLBAR, COLOUR_MAUVE, WID_NS_SCROLLBAR),
1823  EndContainer(),
1824  /* Buttons. */
1825  NWidget(NWID_SELECTION, INVALID_COLOUR, WID_NS_SHOW_REMOVE),
1827  NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_REMOVE), SetFill(1, 0), SetResize(1, 0),
1828  SetDataTip(STR_NEWGRF_SETTINGS_REMOVE, STR_NEWGRF_SETTINGS_REMOVE_TOOLTIP),
1830  NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_MOVE_UP), SetFill(1, 0), SetResize(1, 0),
1831  SetDataTip(STR_NEWGRF_SETTINGS_MOVEUP, STR_NEWGRF_SETTINGS_MOVEUP_TOOLTIP),
1832  NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_MOVE_DOWN), SetFill(1, 0), SetResize(1, 0),
1833  SetDataTip(STR_NEWGRF_SETTINGS_MOVEDOWN, STR_NEWGRF_SETTINGS_MOVEDOWN_TOOLTIP),
1834  EndContainer(),
1835  NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_UPGRADE), SetFill(1, 0), SetResize(1, 0),
1836  SetDataTip(STR_NEWGRF_SETTINGS_UPGRADE, STR_NEWGRF_SETTINGS_UPGRADE_TOOLTIP),
1837  EndContainer(),
1838 
1840  NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_RESCAN_FILES2), SetFill(1, 0), SetResize(1, 0),
1841  SetDataTip(STR_NEWGRF_SETTINGS_RESCAN_FILES, STR_NEWGRF_SETTINGS_RESCAN_FILES_TOOLTIP),
1842  NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_CONTENT_DOWNLOAD2), SetFill(1, 0), SetResize(1, 0),
1843  SetDataTip(STR_INTRO_ONLINE_CONTENT, STR_INTRO_TOOLTIP_ONLINE_CONTENT),
1844  EndContainer(),
1845  EndContainer(),
1846  EndContainer(),
1847 };
1848 
1849 static const NWidgetPart _nested_newgrf_availables_widgets[] = {
1850  NWidget(WWT_PANEL, COLOUR_MAUVE),
1851  NWidget(WWT_LABEL, COLOUR_MAUVE), SetDataTip(STR_NEWGRF_SETTINGS_INACTIVE_LIST, STR_NULL),
1853  /* Left side, available grfs, filter edit box. */
1856  NWidget(WWT_TEXT, COLOUR_MAUVE), SetFill(0, 1), SetDataTip(STR_NEWGRF_FILTER_TITLE, STR_NULL),
1857  NWidget(WWT_EDITBOX, COLOUR_MAUVE, WID_NS_FILTER), SetFill(1, 0), SetMinimalSize(50, 12), SetResize(1, 0),
1858  SetDataTip(STR_LIST_FILTER_OSKTITLE, STR_LIST_FILTER_TOOLTIP),
1859  EndContainer(),
1860  /* Left side, available grfs. */
1861  NWidget(NWID_HORIZONTAL), SetPadding(0, 2, 0, 2),
1862  NWidget(WWT_PANEL, COLOUR_MAUVE),
1863  NWidget(WWT_INSET, COLOUR_MAUVE, WID_NS_AVAIL_LIST), SetMinimalSize(100, 1), SetPadding(2, 2, 2, 2),
1865  EndContainer(),
1866  EndContainer(),
1867  NWidget(NWID_VSCROLLBAR, COLOUR_MAUVE, WID_NS_SCROLL2BAR),
1868  EndContainer(),
1869  /* Left side, available grfs, buttons. */
1871  NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_ADD), SetFill(1, 0), SetResize(1, 0),
1872  SetDataTip(STR_NEWGRF_SETTINGS_ADD, STR_NEWGRF_SETTINGS_ADD_FILE_TOOLTIP),
1874  NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_RESCAN_FILES), SetFill(1, 0), SetResize(1, 0),
1875  SetDataTip(STR_NEWGRF_SETTINGS_RESCAN_FILES, STR_NEWGRF_SETTINGS_RESCAN_FILES_TOOLTIP),
1876  NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_CONTENT_DOWNLOAD), SetFill(1, 0), SetResize(1, 0),
1877  SetDataTip(STR_INTRO_ONLINE_CONTENT, STR_INTRO_TOOLTIP_ONLINE_CONTENT),
1878  EndContainer(),
1879  EndContainer(),
1880  EndContainer(),
1881 };
1882 
1883 static const NWidgetPart _nested_newgrf_infopanel_widgets[] = {
1884  /* Right side, info panel. */
1885  NWidget(NWID_VERTICAL), SetPadding(0, 0, 2, 0),
1886  NWidget(WWT_PANEL, COLOUR_MAUVE), SetPadding(0, 0, 2, 0),
1887  NWidget(WWT_EMPTY, COLOUR_MAUVE, WID_NS_NEWGRF_INFO_TITLE), SetFill(1, 0), SetResize(1, 0),
1888  NWidget(WWT_EMPTY, COLOUR_MAUVE, WID_NS_NEWGRF_INFO), SetFill(1, 1), SetResize(1, 1), SetMinimalSize(150, 100),
1889  EndContainer(),
1890  NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_OPEN_URL), SetFill(1, 0), SetResize(1, 0),
1891  SetDataTip(STR_CONTENT_OPEN_URL, STR_CONTENT_OPEN_URL_TOOLTIP),
1892  NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_NEWGRF_TEXTFILE + TFT_README), SetFill(1, 0), SetResize(1, 0),
1893  SetDataTip(STR_TEXTFILE_VIEW_README, STR_NULL),
1896  SetDataTip(STR_TEXTFILE_VIEW_CHANGELOG, STR_NULL),
1897  NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_NEWGRF_TEXTFILE + TFT_LICENSE), SetFill(1, 0), SetResize(1, 0),
1898  SetDataTip(STR_TEXTFILE_VIEW_LICENCE, STR_NULL),
1899  EndContainer(),
1900  EndContainer(),
1901  NWidget(NWID_SELECTION, INVALID_COLOUR, WID_NS_SHOW_APPLY),
1902  /* Right side, buttons. */
1905  NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_SET_PARAMETERS), SetFill(1, 0), SetResize(1, 0),
1906  SetDataTip(STR_NEWGRF_SETTINGS_SET_PARAMETERS, STR_NULL),
1907  NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_TOGGLE_PALETTE), SetFill(1, 0), SetResize(1, 0),
1908  SetDataTip(STR_NEWGRF_SETTINGS_TOGGLE_PALETTE, STR_NEWGRF_SETTINGS_TOGGLE_PALETTE_TOOLTIP),
1909  EndContainer(),
1910  NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_APPLY_CHANGES), SetFill(1, 0), SetResize(1, 0),
1911  SetDataTip(STR_NEWGRF_SETTINGS_APPLY_CHANGES, STR_NULL),
1912  EndContainer(),
1913  NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_VIEW_PARAMETERS), SetFill(1, 0), SetResize(1, 0),
1914  SetDataTip(STR_NEWGRF_SETTINGS_SHOW_PARAMETERS, STR_NULL),
1915  EndContainer(),
1916 };
1917 
1919 NWidgetBase* NewGRFDisplay(int *biggest_index)
1920 {
1921  NWidgetBase *avs = MakeNWidgets(_nested_newgrf_availables_widgets, lengthof(_nested_newgrf_availables_widgets), biggest_index, NULL);
1922 
1923  int biggest2;
1924  NWidgetBase *acs = MakeNWidgets(_nested_newgrf_actives_widgets, lengthof(_nested_newgrf_actives_widgets), &biggest2, NULL);
1925  *biggest_index = max(*biggest_index, biggest2);
1926 
1927  NWidgetBase *inf = MakeNWidgets(_nested_newgrf_infopanel_widgets, lengthof(_nested_newgrf_infopanel_widgets), &biggest2, NULL);
1928  *biggest_index = max(*biggest_index, biggest2);
1929 
1930  return new NWidgetNewGRFDisplay(avs, acs, inf);
1931 }
1932 
1933 /* Widget definition of the manage newgrfs window */
1934 static const NWidgetPart _nested_newgrf_widgets[] = {
1936  NWidget(WWT_CLOSEBOX, COLOUR_MAUVE),
1937  NWidget(WWT_CAPTION, COLOUR_MAUVE), SetDataTip(STR_NEWGRF_SETTINGS_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
1938  NWidget(WWT_DEFSIZEBOX, COLOUR_MAUVE),
1939  EndContainer(),
1940  NWidget(WWT_PANEL, COLOUR_MAUVE),
1942  /* Resize button. */
1944  NWidget(NWID_SPACER), SetFill(1, 0), SetResize(1, 0),
1945  NWidget(WWT_RESIZEBOX, COLOUR_MAUVE),
1946  EndContainer(),
1947  EndContainer(),
1948 };
1949 
1950 /* Window definition of the manage newgrfs window */
1951 static WindowDesc _newgrf_desc(
1952  WDP_CENTER, "settings_newgrf", 300, 263,
1954  0,
1955  _nested_newgrf_widgets, lengthof(_nested_newgrf_widgets)
1956 );
1957 
1963 static void NewGRFConfirmationCallback(Window *w, bool confirmed)
1964 {
1965  if (confirmed) {
1967  NewGRFWindow *nw = dynamic_cast<NewGRFWindow*>(w);
1968 
1970  GamelogGRFUpdate(_grfconfig, nw->actives); // log GRF changes
1971  CopyGRFConfigList(nw->orig_list, nw->actives, false);
1972  ReloadNewGRFData();
1974 
1975  /* Show new, updated list */
1976  GRFConfig *c;
1977  int i = 0;
1978  for (c = nw->actives; c != NULL && c != nw->active_sel; c = c->next, i++) {}
1979  CopyGRFConfigList(&nw->actives, *nw->orig_list, false);
1980  for (c = nw->actives; c != NULL && i > 0; c = c->next, i--) {}
1981  nw->active_sel = c;
1982  nw->avails.ForceRebuild();
1983 
1984  w->InvalidateData();
1985 
1986  ReInitAllWindows();
1988  }
1989 }
1990 
1991 
1992 
2001 void ShowNewGRFSettings(bool editable, bool show_params, bool exec_changes, GRFConfig **config)
2002 {
2004  new NewGRFWindow(&_newgrf_desc, editable, show_params, exec_changes, config);
2005 }
2006 
2010  NWidget(WWT_CLOSEBOX, COLOUR_GREY),
2011  NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_SAVE_PRESET_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
2012  NWidget(WWT_DEFSIZEBOX, COLOUR_GREY),
2013  EndContainer(),
2014  NWidget(WWT_PANEL, COLOUR_GREY),
2016  NWidget(WWT_INSET, COLOUR_GREY, WID_SVP_PRESET_LIST), SetPadding(2, 1, 0, 2),
2017  SetDataTip(0x0, STR_SAVE_PRESET_LIST_TOOLTIP), SetResize(1, 10), SetScrollbar(WID_SVP_SCROLLBAR), EndContainer(),
2018  NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_SVP_SCROLLBAR),
2019  EndContainer(),
2020  NWidget(WWT_EDITBOX, COLOUR_GREY, WID_SVP_EDITBOX), SetPadding(3, 2, 2, 2), SetFill(1, 0), SetResize(1, 0),
2021  SetDataTip(STR_SAVE_PRESET_TITLE, STR_SAVE_PRESET_EDITBOX_TOOLTIP),
2022  EndContainer(),
2024  NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SVP_CANCEL), SetDataTip(STR_SAVE_PRESET_CANCEL, STR_SAVE_PRESET_CANCEL_TOOLTIP), SetFill(1, 0), SetResize(1, 0),
2025  NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SVP_SAVE), SetDataTip(STR_SAVE_PRESET_SAVE, STR_SAVE_PRESET_SAVE_TOOLTIP), SetFill(1, 0), SetResize(1, 0),
2026  NWidget(WWT_RESIZEBOX, COLOUR_GREY),
2027  EndContainer(),
2028 };
2029 
2032  WDP_CENTER, "save_preset", 140, 110,
2034  WDF_MODAL,
2036 );
2037 
2039 struct SavePresetWindow : public Window {
2043  int selected;
2044 
2049  SavePresetWindow(const char *initial_text) : Window(&_save_preset_desc), presetname_editbox(32)
2050  {
2051  GetGRFPresetList(&this->presets);
2052  this->selected = -1;
2053  if (initial_text != NULL) {
2054  for (uint i = 0; i < this->presets.Length(); i++) {
2055  if (!strcmp(initial_text, this->presets[i])) {
2056  this->selected = i;
2057  break;
2058  }
2059  }
2060  }
2061 
2062  this->querystrings[WID_SVP_EDITBOX] = &this->presetname_editbox;
2063  this->presetname_editbox.ok_button = WID_SVP_SAVE;
2064  this->presetname_editbox.cancel_button = WID_SVP_CANCEL;
2065 
2066  this->CreateNestedTree();
2067  this->vscroll = this->GetScrollbar(WID_SVP_SCROLLBAR);
2068  this->FinishInitNested(0);
2069 
2070  this->vscroll->SetCount(this->presets.Length());
2072  if (initial_text != NULL) this->presetname_editbox.text.Assign(initial_text);
2073  }
2074 
2075  ~SavePresetWindow()
2076  {
2077  }
2078 
2079  virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
2080  {
2081  switch (widget) {
2082  case WID_SVP_PRESET_LIST: {
2083  resize->height = FONT_HEIGHT_NORMAL + 2U;
2084  size->height = 0;
2085  for (uint i = 0; i < this->presets.Length(); i++) {
2086  Dimension d = GetStringBoundingBox(this->presets[i]);
2087  size->width = max(size->width, d.width + WD_FRAMETEXT_LEFT + WD_FRAMETEXT_RIGHT);
2088  resize->height = max(resize->height, d.height);
2089  }
2090  size->height = ClampU(this->presets.Length(), 5, 20) * resize->height + 1;
2091  break;
2092  }
2093  }
2094  }
2095 
2096  virtual void DrawWidget(const Rect &r, int widget) const
2097  {
2098  switch (widget) {
2099  case WID_SVP_PRESET_LIST: {
2100  GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, PC_BLACK);
2101 
2102  uint step_height = this->GetWidget<NWidgetBase>(WID_SVP_PRESET_LIST)->resize_y;
2103  int offset_y = (step_height - FONT_HEIGHT_NORMAL) / 2;
2104  uint y = r.top + WD_FRAMERECT_TOP;
2105  uint min_index = this->vscroll->GetPosition();
2106  uint max_index = min(min_index + this->vscroll->GetCapacity(), this->presets.Length());
2107 
2108  for (uint i = min_index; i < max_index; i++) {
2109  if ((int)i == this->selected) GfxFillRect(r.left + 1, y, r.right - 1, y + step_height - 2, PC_DARK_BLUE);
2110 
2111  const char *text = this->presets[i];
2112  DrawString(r.left + WD_FRAMERECT_LEFT, r.right, y + offset_y, text, ((int)i == this->selected) ? TC_WHITE : TC_SILVER);
2113  y += step_height;
2114  }
2115  break;
2116  }
2117  }
2118  }
2119 
2120  virtual void OnClick(Point pt, int widget, int click_count)
2121  {
2122  switch (widget) {
2123  case WID_SVP_PRESET_LIST: {
2124  uint row = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_SVP_PRESET_LIST);
2125  if (row < this->presets.Length()) {
2126  this->selected = row;
2127  this->presetname_editbox.text.Assign(this->presets[row]);
2130  }
2131  break;
2132  }
2133 
2134  case WID_SVP_CANCEL:
2135  delete this;
2136  break;
2137 
2138  case WID_SVP_SAVE: {
2140  if (w != NULL && !StrEmpty(this->presetname_editbox.text.buf)) w->OnQueryTextFinished(this->presetname_editbox.text.buf);
2141  delete this;
2142  break;
2143  }
2144  }
2145  }
2146 
2147  virtual void OnResize()
2148  {
2149  this->vscroll->SetCapacityFromWidget(this, WID_SVP_PRESET_LIST);
2150  }
2151 };
2152 
2157 static void ShowSavePresetWindow(const char *initial_text)
2158 {
2160  new SavePresetWindow(initial_text);
2161 }
2162 
2165  NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_NEWGRF_SCAN_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
2166  NWidget(WWT_PANEL, COLOUR_GREY),
2167  NWidget(NWID_HORIZONTAL), SetPIP(20, 0, 20),
2168  NWidget(NWID_VERTICAL), SetPIP(11, 8, 11),
2169  NWidget(WWT_LABEL, INVALID_COLOUR), SetDataTip(STR_NEWGRF_SCAN_MESSAGE, STR_NULL), SetFill(1, 0),
2170  NWidget(WWT_EMPTY, INVALID_COLOUR, WID_SP_PROGRESS_BAR), SetFill(1, 0),
2171  NWidget(WWT_EMPTY, INVALID_COLOUR, WID_SP_PROGRESS_TEXT), SetFill(1, 0),
2172  EndContainer(),
2173  EndContainer(),
2174  EndContainer(),
2175 };
2176 
2179  WDP_CENTER, NULL, 0, 0,
2181  0,
2183 );
2184 
2186 struct ScanProgressWindow : public Window {
2187  char *last_name;
2188  int scanned;
2189 
2191  ScanProgressWindow() : Window(&_scan_progress_desc), last_name(NULL), scanned(0)
2192  {
2193  this->InitNested(1);
2194  }
2195 
2198  {
2199  free(last_name);
2200  }
2201 
2202  virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
2203  {
2204  switch (widget) {
2205  case WID_SP_PROGRESS_BAR: {
2206  SetDParamMaxValue(0, 100);
2207  *size = GetStringBoundingBox(STR_GENERATION_PROGRESS);
2208  /* We need some spacing for the 'border' */
2209  size->height += 8;
2210  size->width += 8;
2211  break;
2212  }
2213 
2214  case WID_SP_PROGRESS_TEXT:
2215  SetDParamMaxDigits(0, 4);
2216  SetDParamMaxDigits(1, 4);
2217  /* We really don't know the width. We could determine it by scanning the NewGRFs,
2218  * but this is the status window for scanning them... */
2219  size->width = max(400U, GetStringBoundingBox(STR_NEWGRF_SCAN_STATUS).width);
2220  size->height = FONT_HEIGHT_NORMAL * 2 + WD_PAR_VSEP_NORMAL;
2221  break;
2222  }
2223  }
2224 
2225  virtual void DrawWidget(const Rect &r, int widget) const
2226  {
2227  switch (widget) {
2228  case WID_SP_PROGRESS_BAR: {
2229  /* Draw the % complete with a bar and a text */
2230  DrawFrameRect(r.left, r.top, r.right, r.bottom, COLOUR_GREY, FR_BORDERONLY);
2231  uint percent = scanned * 100 / max(1U, _settings_client.gui.last_newgrf_count);
2232  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);
2233  SetDParam(0, percent);
2234  DrawString(r.left, r.right, r.top + 5, STR_GENERATION_PROGRESS, TC_FROMSTRING, SA_HOR_CENTER);
2235  break;
2236  }
2237 
2238  case WID_SP_PROGRESS_TEXT:
2239  SetDParam(0, this->scanned);
2241  DrawString(r.left, r.right, r.top, STR_NEWGRF_SCAN_STATUS, TC_FROMSTRING, SA_HOR_CENTER);
2242 
2243  DrawString(r.left, r.right, r.top + FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL, this->last_name == NULL ? "" : this->last_name, TC_BLACK, SA_HOR_CENTER);
2244  break;
2245  }
2246  }
2247 
2253  void UpdateNewGRFScanStatus(uint num, const char *name)
2254  {
2255  free(this->last_name);
2256  if (name == NULL) {
2257  char buf[256];
2258  GetString(buf, STR_NEWGRF_SCAN_ARCHIVES, lastof(buf));
2259  this->last_name = stredup(buf);
2260  } else {
2261  this->last_name = stredup(name);
2262  }
2263  this->scanned = num;
2265 
2266  this->SetDirty();
2267  }
2268 };
2269 
2275 void UpdateNewGRFScanStatus(uint num, const char *name)
2276 {
2278  if (w == NULL) w = new ScanProgressWindow();
2279  w->UpdateNewGRFScanStatus(num, name);
2280 }