# newgrf_gui.cpp

Go to the documentation of this file.

    00001 /* $Id$ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * 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.
    00006  * 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.
    00007  * 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/>.
    00008  */
    00009
    00012 #include "stdafx.h"
    00013 #include "error.h"
    00014 #include "gui.h"
    00015 #include "newgrf.h"
    00016 #include "strings_func.h"
    00017 #include "window_func.h"
    00018 #include "gamelog.h"
    00019 #include "settings_type.h"
    00020 #include "settings_func.h"
    00021 #include "widgets/dropdown_type.h"
    00022 #include "network/network.h"
    00023 #include "network/network_content.h"
    00024 #include "sortlist_type.h"
    00025 #include "querystring_gui.h"
    00026 #include "core/geometry_func.hpp"
    00027 #include "newgrf_text.h"
    00028 #include "fileio_func.h"
    00029 #include "fontcache.h"
    00030
    00031 #include "widgets/newgrf_widget.h"
    00032
    00033 #include "table/sprites.h"
    00034
    00038 void ShowNewGRFError()
    00039 {
    00040   /* Do not show errors when entering the main screen */
    00041   if (_game_mode == GM_MENU) return;
    00042
    00043   for (const GRFConfig *c = _grfconfig; c != NULL; c = c->next) {
    00044     /* We only want to show fatal errors */
    00045     if (c->error == NULL || c->error->severity != STR_NEWGRF_ERROR_MSG_FATAL) continue;
    00046
    00047     SetDParam   (0, c->error->custom_message == NULL ? c->error->message : STR_JUST_RAW_STRING);
    00048     SetDParamStr(1, c->error->custom_message);
    00049     SetDParamStr(2, c->filename);
    00050     SetDParamStr(3, c->error->data);
    00051     for (uint i = 0; i < lengthof(c->error->param_value); i++) {
    00052       SetDParam(4 + i, c->error->param_value[i]);
    00053     }
    00054     ShowErrorMessage(STR_NEWGRF_ERROR_FATAL_POPUP, INVALID_STRING_ID, WL_CRITICAL);
    00055     break;
    00056   }
    00057 }
    00058
    00059 static void ShowNewGRFInfo(const GRFConfig *c, uint x, uint y, uint right, uint bottom, bool show_params)
    00060 {
    00061   if (c->error != NULL) {
    00062     char message[512];
    00063     SetDParamStr(0, c->error->custom_message); // is skipped by built-in messages
    00064     SetDParamStr(1, c->filename);
    00065     SetDParamStr(2, c->error->data);
    00066     for (uint i = 0; i < lengthof(c->error->param_value); i++) {
    00067       SetDParam(3 + i, c->error->param_value[i]);
    00068     }
    00069     GetString(message, c->error->custom_message == NULL ? c->error->message : STR_JUST_RAW_STRING, lastof(message));
    00070
    00071     SetDParamStr(0, message);
    00072     y = DrawStringMultiLine(x, right, y, bottom, c->error->severity);
    00073   }
    00074
    00075   /* Draw filename or not if it is not known (GRF sent over internet) */
    00076   if (c->filename != NULL) {
    00077     SetDParamStr(0, c->filename);
    00078     y = DrawStringMultiLine(x, right, y, bottom, STR_NEWGRF_SETTINGS_FILENAME);
    00079   }
    00080
    00081   /* Prepare and draw GRF ID */
    00082   char buff[256];
    00083   snprintf(buff, lengthof(buff), "%08X", BSWAP32(c->ident.grfid));
    00084   SetDParamStr(0, buff);
    00085   y = DrawStringMultiLine(x, right, y, bottom, STR_NEWGRF_SETTINGS_GRF_ID);
    00086
    00087   if ((_settings_client.gui.newgrf_developer_tools || _settings_client.gui.newgrf_show_old_versions) && c->version != 0) {
    00088     SetDParam(0, c->version);
    00089     y = DrawStringMultiLine(x, right, y, bottom, STR_NEWGRF_SETTINGS_VERSION);
    00090   }
    00091   if ((_settings_client.gui.newgrf_developer_tools || _settings_client.gui.newgrf_show_old_versions) && c->min_loadable_version != 0) {
    00092     SetDParam(0, c->min_loadable_version);
    00093     y = DrawStringMultiLine(x, right, y, bottom, STR_NEWGRF_SETTINGS_MIN_VERSION);
    00094   }
    00095
    00096   /* Prepare and draw MD5 sum */
    00097   md5sumToString(buff, lastof(buff), c->ident.md5sum);
    00098   SetDParamStr(0, buff);
    00099   y = DrawStringMultiLine(x, right, y, bottom, STR_NEWGRF_SETTINGS_MD5SUM);
    00100
    00101   /* Show GRF parameter list */
    00102   if (show_params) {
    00103     if (c->num_params > 0) {
    00104       GRFBuildParamList(buff, c, lastof(buff));
    00105       SetDParam(0, STR_JUST_RAW_STRING);
    00106       SetDParamStr(1, buff);
    00107     } else {
    00108       SetDParam(0, STR_LAND_AREA_INFORMATION_LOCAL_AUTHORITY_NONE);
    00109     }
    00110     y = DrawStringMultiLine(x, right, y, bottom, STR_NEWGRF_SETTINGS_PARAMETER);
    00111
    00112     /* Draw the palette of the NewGRF */
    00113     if (c->palette & GRFP_BLT_32BPP) {
    00114       SetDParamStr(0, (c->palette & GRFP_USE_WINDOWS) ? "Windows / 32 bpp" : "DOS / 32 bpp");
    00115     } else {
    00116       SetDParamStr(0, (c->palette & GRFP_USE_WINDOWS) ? "Windows" : "DOS");
    00117     }
    00118     y = DrawStringMultiLine(x, right, y, bottom, STR_NEWGRF_SETTINGS_PALETTE);
    00119   }
    00120
    00121   /* Show flags */
    00122   if (c->status == GCS_NOT_FOUND)       y = DrawStringMultiLine(x, right, y, bottom, STR_NEWGRF_SETTINGS_NOT_FOUND);
    00123   if (c->status == GCS_DISABLED)        y = DrawStringMultiLine(x, right, y, bottom, STR_NEWGRF_SETTINGS_DISABLED);
    00124   if (HasBit(c->flags, GCF_INVALID))    y = DrawStringMultiLine(x, right, y, bottom, STR_NEWGRF_SETTINGS_INCOMPATIBLE);
    00125   if (HasBit(c->flags, GCF_COMPATIBLE)) y = DrawStringMultiLine(x, right, y, bottom, STR_NEWGRF_COMPATIBLE_LOADED);
    00126
    00127   /* Draw GRF info if it exists */
    00128   if (!StrEmpty(c->GetDescription())) {
    00129     SetDParam(0, STR_JUST_RAW_STRING);
    00130     SetDParamStr(1, c->GetDescription());
    00131     y = DrawStringMultiLine(x, right, y, bottom, STR_BLACK_STRING);
    00132   } else {
    00133     y = DrawStringMultiLine(x, right, y, bottom, STR_NEWGRF_SETTINGS_NO_INFO);
    00134   }
    00135 }
    00136
    00140 struct NewGRFParametersWindow : public Window {
    00141   static GRFParameterInfo dummy_parameter_info;
    00142   GRFConfig *grf_config;
    00143   uint clicked_button;
    00144   bool clicked_increase;
    00145   int timeout;
    00146   uint clicked_row;
    00147   int line_height;
    00148   Scrollbar *vscroll;
    00149   bool action14present;
    00150   bool editable;
    00151
    00152   NewGRFParametersWindow(const WindowDesc *desc, GRFConfig *c, bool editable) : Window(),
    00153     grf_config(c),
    00154     clicked_button(UINT_MAX),
    00155     timeout(0),
    00156     clicked_row(UINT_MAX),
    00157     editable(editable)
    00158   {
    00159     this->action14present = (c->num_valid_params != lengthof(c->param) || c->param_info.Length() != 0);
    00160
    00161     this->CreateNestedTree(desc);
    00162     this->vscroll = this->GetScrollbar(WID_NP_SCROLLBAR);
    00163     this->GetWidget<NWidgetStacked>(WID_NP_SHOW_NUMPAR)->SetDisplayedPlane(this->action14present ? SZSP_HORIZONTAL : 0);
    00164     this->GetWidget<NWidgetStacked>(WID_NP_SHOW_DESCRIPTION)->SetDisplayedPlane(this->action14present ? 0 : SZSP_HORIZONTAL);
    00165     this->FinishInitNested(desc);  // Initializes 'this->line_height' as side effect.
    00166
    00167     this->SetWidgetDisabledState(WID_NP_RESET, !this->editable);
    00168
    00169     this->InvalidateData();
    00170   }
    00171
    00177   static GRFParameterInfo *GetDummyParameterInfo(uint nr)
    00178   {
    00179     dummy_parameter_info.param_nr = nr;
    00180     return &dummy_parameter_info;
    00181   }
    00182
    00183   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
    00184   {
    00185     switch (widget) {
    00186       case WID_NP_NUMPAR_DEC:
    00187       case WID_NP_NUMPAR_INC: {
    00188         size->width = size->height = FONT_HEIGHT_NORMAL;
    00189         break;
    00190       }
    00191
    00192       case WID_NP_NUMPAR: {
    00193         SetDParam(0, 999);
    00194         Dimension d = GetStringBoundingBox(this->GetWidget<NWidgetCore>(widget)->widget_data);
    00195         d.width += padding.width;
    00196         d.height += padding.height;
    00197         *size = maxdim(*size, d);
    00198         break;
    00199       }
    00200
    00201       case WID_NP_BACKGROUND:
    00202         this->line_height = FONT_HEIGHT_NORMAL + WD_MATRIX_TOP + WD_MATRIX_BOTTOM;
    00203
    00204         resize->width = 1;
    00205         resize->height = this->line_height;
    00206         size->height = GB(this->GetWidget<NWidgetCore>(widget)->widget_data, MAT_ROW_START, MAT_ROW_BITS) * this->line_height;
    00207         break;
    00208
    00209       case WID_NP_DESCRIPTION:
    00210         /* Minimum size of 4 lines. The 500 is the default size of the window. */
    00211         Dimension suggestion = {500 - WD_FRAMERECT_LEFT - WD_FRAMERECT_RIGHT, FONT_HEIGHT_NORMAL * 4 + WD_TEXTPANEL_TOP + WD_TEXTPANEL_BOTTOM};
    00212         for (uint i = 0; i < this->grf_config->param_info.Length(); i++) {
    00213           const GRFParameterInfo *par_info = this->grf_config->param_info[i];
    00214           if (par_info == NULL) continue;
    00215           const char *desc = GetGRFStringFromGRFText(par_info->desc);
    00216           if (desc == NULL) continue;
    00217           Dimension d = GetStringMultiLineBoundingBox(desc, suggestion);
    00218           d.height += WD_TEXTPANEL_TOP + WD_TEXTPANEL_BOTTOM;
    00219           suggestion = maxdim(d, suggestion);
    00220         }
    00221         size->height = suggestion.height;
    00222         break;
    00223     }
    00224   }
    00225
    00226   virtual void SetStringParameters(int widget) const
    00227   {
    00228     switch (widget) {
    00229       case WID_NP_NUMPAR:
    00230         SetDParam(0, this->vscroll->GetCount());
    00231         break;
    00232     }
    00233   }
    00234
    00235   virtual void DrawWidget(const Rect &r, int widget) const
    00236   {
    00237     if (widget == WID_NP_DESCRIPTION) {
    00238       const GRFParameterInfo *par_info = (this->clicked_row < this->grf_config->param_info.Length()) ? this->grf_config->param_info[this->clicked_row] : NULL;
    00239       if (par_info == NULL) return;
    00240       const char *desc = GetGRFStringFromGRFText(par_info->desc);
    00241       if (desc == NULL) return;
    00242       DrawStringMultiLine(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + WD_TEXTPANEL_TOP, r.bottom - WD_TEXTPANEL_BOTTOM, desc, TC_BLACK);
    00243       return;
    00244     } else if (widget != WID_NP_BACKGROUND) {
    00245       return;
    00246     }
    00247
    00248     bool rtl = _current_text_dir == TD_RTL;
    00249     uint buttons_left = rtl ? r.right - 23 : r.left + 4;
    00250     uint text_left    = r.left + (rtl ? WD_FRAMERECT_LEFT : 28);
    00251     uint text_right   = r.right - (rtl ? 28 : WD_FRAMERECT_RIGHT);
    00252
    00253     int y = r.top;
    00254     for (uint i = this->vscroll->GetPosition(); this->vscroll->IsVisible(i) && i < this->vscroll->GetCount(); i++) {
    00255       GRFParameterInfo *par_info = (i < this->grf_config->param_info.Length()) ? this->grf_config->param_info[i] : NULL;
    00256       if (par_info == NULL) par_info = GetDummyParameterInfo(i);
    00257       uint32 current_value = par_info->GetValue(this->grf_config);
    00258       bool selected = (i == this->clicked_row);
    00259
    00260       if (par_info->type == PTYPE_BOOL) {
    00261         DrawBoolButton(buttons_left, y + 2, current_value != 0, this->editable);
    00262         SetDParam(2, par_info->GetValue(this->grf_config) == 0 ? STR_CONFIG_SETTING_OFF : STR_CONFIG_SETTING_ON);
    00263       } else if (par_info->type == PTYPE_UINT_ENUM) {
    00264         DrawArrowButtons(buttons_left, y + 2, 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);
    00265         SetDParam(2, STR_JUST_INT);
    00266         SetDParam(3, current_value);
    00267         if (par_info->value_names.Contains(current_value)) {
    00268           const char *label = GetGRFStringFromGRFText(par_info->value_names.Find(current_value)->second);
    00269           if (label != NULL) {
    00270             SetDParam(2, STR_JUST_RAW_STRING);
    00271             SetDParamStr(3, label);
    00272           }
    00273         }
    00274       }
    00275
    00276       const char *name = GetGRFStringFromGRFText(par_info->name);
    00277       if (name != NULL) {
    00278         SetDParam(0, STR_JUST_RAW_STRING);
    00279         SetDParamStr(1, name);
    00280       } else {
    00281         SetDParam(0, STR_NEWGRF_PARAMETERS_DEFAULT_NAME);
    00282         SetDParam(1, i + 1);
    00283       }
    00284
    00285       DrawString(text_left, text_right, y + WD_MATRIX_TOP, STR_NEWGRF_PARAMETERS_SETTING, selected ? TC_WHITE : TC_LIGHT_BLUE);
    00286       y += this->line_height;
    00287     }
    00288   }
    00289
    00290   virtual void OnClick(Point pt, int widget, int click_count)
    00291   {
    00292     switch (widget) {
    00293       case WID_NP_NUMPAR_DEC:
    00294         if (this->editable && !this->action14present && this->grf_config->num_params > 0) {
    00295           this->grf_config->num_params--;
    00296           this->InvalidateData();
    00297           SetWindowDirty(WC_GAME_OPTIONS, WN_GAME_OPTIONS_NEWGRF_STATE);
    00298         }
    00299         break;
    00300
    00301       case WID_NP_NUMPAR_INC: {
    00302         GRFConfig *c = this->grf_config;
    00303         if (this->editable && !this->action14present && c->num_params < c->num_valid_params) {
    00304           c->param[c->num_params++] = 0;
    00305           this->InvalidateData();
    00306           SetWindowDirty(WC_GAME_OPTIONS, WN_GAME_OPTIONS_NEWGRF_STATE);
    00307         }
    00308         break;
    00309       }
    00310
    00311       case WID_NP_BACKGROUND: {
    00312         if (!this->editable) break;
    00313         uint num = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_NP_BACKGROUND);
    00314         if (num >= this->vscroll->GetCount()) break;
    00315         if (this->clicked_row != num) {
    00316           DeleteChildWindows(WC_QUERY_STRING);
    00317           this->clicked_row = num;
    00318         }
    00319
    00320         const NWidgetBase *wid = this->GetWidget<NWidgetBase>(WID_NP_BACKGROUND);
    00321         int x = pt.x - wid->pos_x;
    00322         if (_current_text_dir == TD_RTL) x = wid->current_x - x;
    00323         x -= 4;
    00324
    00325         GRFParameterInfo *par_info = (num < this->grf_config->param_info.Length()) ? this->grf_config->param_info[num] : NULL;
    00326         if (par_info == NULL) par_info = GetDummyParameterInfo(num);
    00327
    00328         /* One of the arrows is clicked */
    00329         if (IsInsideMM(x, 0, 21)) {
    00330           uint32 val = par_info->GetValue(this->grf_config);
    00331           uint32 old_val = val;
    00332           if (par_info->type == PTYPE_BOOL) {
    00333             val = !val;
    00334           } else {
    00335             if (x >= 10) {
    00336               /* Increase button clicked */
    00337               if (val < par_info->max_value) val++;
    00338               this->clicked_increase = true;
    00339             } else {
    00340               /* Decrease button clicked */
    00341               if (val > par_info->min_value) val--;
    00342               this->clicked_increase = false;
    00343             }
    00344           }
    00345           if (val != old_val) {
    00346             par_info->SetValue(this->grf_config, val);
    00347
    00348             this->clicked_button = num;
    00349             this->timeout = 5;
    00350           }
    00351         } else if (par_info->type == PTYPE_UINT_ENUM && click_count >= 2) {
    00352           /* Display a query box so users can enter a custom value. */
    00353           SetDParam(0, this->grf_config->param[num]);
    00354           ShowQueryString(STR_JUST_INT, STR_CONFIG_SETTING_QUERY_CAPTION, 10, this, CS_NUMERAL, QSF_NONE);
    00355         }
    00356         this->SetDirty();
    00357         break;
    00358       }
    00359
    00360       case WID_NP_RESET:
    00361         if (!this->editable) break;
    00362         this->grf_config->SetParameterDefaults();
    00363         this->InvalidateData();
    00364         SetWindowDirty(WC_GAME_OPTIONS, WN_GAME_OPTIONS_NEWGRF_STATE);
    00365         break;
    00366
    00367       case WID_NP_ACCEPT:
    00368         delete this;
    00369         break;
    00370     }
    00371   }
    00372
    00373   virtual void OnQueryTextFinished(char *str)
    00374   {
    00375     if (StrEmpty(str)) return;
    00376     int32 value = atoi(str);
    00377     GRFParameterInfo *par_info = ((uint)this->clicked_row < this->grf_config->param_info.Length()) ? this->grf_config->param_info[this->clicked_row] : NULL;
    00378     if (par_info == NULL) par_info = GetDummyParameterInfo(this->clicked_row);
    00379     uint32 val = Clamp<uint32>(value, par_info->min_value, par_info->max_value);
    00380     par_info->SetValue(this->grf_config, val);
    00381     this->SetDirty();
    00382   }
    00383
    00384   virtual void OnResize()
    00385   {
    00386     NWidgetCore *nwi = this->GetWidget<NWidgetCore>(WID_NP_BACKGROUND);
    00387     this->vscroll->SetCapacity(nwi->current_y / this->line_height);
    00388     nwi->widget_data = (this->vscroll->GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
    00389   }
    00390
    00396   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
    00397   {
    00398     if (!gui_scope) return;
    00399     if (!this->action14present) {
    00400       this->SetWidgetDisabledState(WID_NP_NUMPAR_DEC, !this->editable || this->grf_config->num_params == 0);
    00401       this->SetWidgetDisabledState(WID_NP_NUMPAR_INC, !this->editable || this->grf_config->num_params >= this->grf_config->num_valid_params);
    00402     }
    00403
    00404     this->vscroll->SetCount(this->action14present ? this->grf_config->num_valid_params : this->grf_config->num_params);
    00405     if (this->clicked_row != UINT_MAX && this->clicked_row >= this->vscroll->GetCount()) {
    00406       this->clicked_row = UINT_MAX;
    00407       DeleteChildWindows(WC_QUERY_STRING);
    00408     }
    00409   }
    00410
    00411   virtual void OnTick()
    00412   {
    00413     if (--this->timeout == 0) {
    00414       this->clicked_button = UINT_MAX;
    00415       this->SetDirty();
    00416     }
    00417   }
    00418 };
    00419 GRFParameterInfo NewGRFParametersWindow::dummy_parameter_info(0);
    00420
    00421
    00422 static const NWidgetPart _nested_newgrf_parameter_widgets[] = {
    00423   NWidget(NWID_HORIZONTAL),
    00424     NWidget(WWT_CLOSEBOX, COLOUR_MAUVE),
    00425     NWidget(WWT_CAPTION, COLOUR_MAUVE), SetDataTip(STR_NEWGRF_PARAMETERS_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    00426   EndContainer(),
    00427   NWidget(NWID_SELECTION, INVALID_COLOUR, WID_NP_SHOW_NUMPAR),
    00428     NWidget(WWT_PANEL, COLOUR_MAUVE), SetResize(1, 0), SetFill(1, 0), SetPIP(4, 0, 4),
    00429       NWidget(NWID_HORIZONTAL), SetPIP(4, 0, 4),
    00430         NWidget(WWT_PUSHARROWBTN, COLOUR_YELLOW, WID_NP_NUMPAR_DEC), SetMinimalSize(12, 12), SetDataTip(AWV_DECREASE, STR_NULL),
    00431         NWidget(WWT_PUSHARROWBTN, COLOUR_YELLOW, WID_NP_NUMPAR_INC), SetMinimalSize(12, 12), SetDataTip(AWV_INCREASE, STR_NULL),
    00432         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),
    00433       EndContainer(),
    00434     EndContainer(),
    00435   EndContainer(),
    00436   NWidget(NWID_HORIZONTAL),
    00437     NWidget(WWT_MATRIX, COLOUR_MAUVE, WID_NP_BACKGROUND), SetMinimalSize(188, 182), SetResize(1, 1), SetFill(1, 0), SetDataTip(0x501, STR_NULL), SetScrollbar(WID_NP_SCROLLBAR),
    00438     NWidget(NWID_VSCROLLBAR, COLOUR_MAUVE, WID_NP_SCROLLBAR),
    00439   EndContainer(),
    00440   NWidget(NWID_SELECTION, INVALID_COLOUR, WID_NP_SHOW_DESCRIPTION),
    00441     NWidget(WWT_PANEL, COLOUR_MAUVE, WID_NP_DESCRIPTION), SetResize(1, 0), SetFill(1, 0),
    00442     EndContainer(),
    00443   EndContainer(),
    00444   NWidget(NWID_HORIZONTAL),
    00445     NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
    00446       NWidget(WWT_PUSHTXTBTN, COLOUR_MAUVE, WID_NP_ACCEPT), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_NEWGRF_PARAMETERS_CLOSE, STR_NULL),
    00447       NWidget(WWT_PUSHTXTBTN, COLOUR_MAUVE, WID_NP_RESET), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_NEWGRF_PARAMETERS_RESET, STR_NEWGRF_PARAMETERS_RESET_TOOLTIP),
    00448     EndContainer(),
    00449     NWidget(WWT_RESIZEBOX, COLOUR_MAUVE),
    00450   EndContainer(),
    00451 };
    00452
    00454 static const WindowDesc _newgrf_parameters_desc(
    00455   WDP_CENTER, 500, 208,
    00456   WC_GRF_PARAMETERS, WC_NONE,
    00457   WDF_UNCLICK_BUTTONS,
    00458   _nested_newgrf_parameter_widgets, lengthof(_nested_newgrf_parameter_widgets)
    00459 );
    00460
    00461 static void OpenGRFParameterWindow(GRFConfig *c, bool editable)
    00462 {
    00463   DeleteWindowByClass(WC_GRF_PARAMETERS);
    00464   new NewGRFParametersWindow(&_newgrf_parameters_desc, c, editable);
    00465 }
    00466
    00468 struct NewGRFTextfileWindow : public Window, MissingGlyphSearcher {
    00469   const GRFConfig *grf_config;
    00470   TextfileType file_type;
    00471   int line_height;
    00472   Scrollbar *vscroll;
    00473   Scrollbar *hscroll;
    00474   char *text;
    00475   SmallVector<const char *, 64> lines;
    00476   uint max_length;
    00477
    00478   static const int TOP_SPACING    = WD_FRAMETEXT_TOP;
    00479   static const int BOTTOM_SPACING = WD_FRAMETEXT_BOTTOM;
    00480
    00481   NewGRFTextfileWindow(const WindowDesc *desc, const GRFConfig *c, TextfileType file_type) : Window(), grf_config(c), file_type(file_type)
    00482   {
    00483     this->CreateNestedTree(desc);
    00484     this->GetWidget<NWidgetCore>(WID_NT_CAPTION)->SetDataTip(STR_NEWGRF_README_CAPTION + file_type, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS);
    00485     this->vscroll = this->GetScrollbar(WID_NT_VSCROLLBAR);
    00486     this->hscroll = this->GetScrollbar(WID_NT_HSCROLLBAR);
    00487     this->FinishInitNested(desc);
    00488
    00489     this->LoadTextfile();
    00490   }
    00491
    00492   ~NewGRFTextfileWindow()
    00493   {
    00494     free(this->text);
    00495   }
    00496
    00497   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
    00498   {
    00499     switch (widget) {
    00500       case WID_NT_BACKGROUND:
    00501         this->line_height = FONT_HEIGHT_MONO + 2;
    00502         resize->height = this->line_height;
    00503
    00504         size->height = 4 * resize->height + TOP_SPACING + BOTTOM_SPACING; // At least 4 lines are visible.
    00505         size->width = max(200u, size->width); // At least 200 pixels wide.
    00506         break;
    00507     }
    00508   }
    00509
    00510   virtual void SetStringParameters(int widget) const
    00511   {
    00512     if (widget == WID_NT_CAPTION) SetDParamStr(0, this->grf_config->GetName());
    00513   }
    00514
    00515   virtual void DrawWidget(const Rect &r, int widget) const
    00516   {
    00517     if (widget != WID_NT_BACKGROUND) return;
    00518
    00519     int width = r.right - r.left + 1 - WD_BEVEL_LEFT - WD_BEVEL_RIGHT;
    00520     int height = r.bottom - r.top + 1 - WD_BEVEL_LEFT - WD_BEVEL_RIGHT;
    00521
    00522     DrawPixelInfo new_dpi;
    00523     if (!FillDrawPixelInfo(&new_dpi, r.left + WD_BEVEL_LEFT, r.top, width, height)) return;
    00524     DrawPixelInfo *old_dpi = _cur_dpi;
    00525     _cur_dpi = &new_dpi;
    00526
    00527     int left, right;
    00528     if (_current_text_dir == TD_RTL) {
    00529       left = width + WD_BEVEL_RIGHT - WD_FRAMETEXT_RIGHT - this->hscroll->GetCount();
    00530       right = width + WD_BEVEL_RIGHT - WD_FRAMETEXT_RIGHT - 1 + this->hscroll->GetPosition();
    00531     } else {
    00532       left = WD_FRAMETEXT_LEFT - WD_BEVEL_LEFT - this->hscroll->GetPosition();
    00533       right = WD_FRAMETEXT_LEFT - WD_BEVEL_LEFT + this->hscroll->GetCount() - 1;
    00534     }
    00535     int top = TOP_SPACING;
    00536     for (uint i = 0; i < this->vscroll->GetCapacity() && i + this->vscroll->GetPosition() < this->lines.Length(); i++) {
    00537       DrawString(left, right, top + i * this->line_height, this->lines[i + this->vscroll->GetPosition()], TC_WHITE, SA_LEFT, false, FS_MONO);
    00538     }
    00539
    00540     _cur_dpi = old_dpi;
    00541   }
    00542
    00543   virtual void OnResize()
    00544   {
    00545     this->vscroll->SetCapacityFromWidget(this, WID_NT_BACKGROUND, TOP_SPACING + BOTTOM_SPACING);
    00546     this->hscroll->SetCapacityFromWidget(this, WID_NT_BACKGROUND);
    00547   }
    00548
    00549 private:
    00550   uint search_iterator;
    00551
    00552   /* virtual */ void Reset()
    00553   {
    00554     this->search_iterator = 0;
    00555   }
    00556
    00557   FontSize DefaultSize()
    00558   {
    00559     return FS_MONO;
    00560   }
    00561
    00562   const char *NextString()
    00563   {
    00564     if (this->search_iterator >= this->lines.Length()) return NULL;
    00565
    00566     return this->lines[this->search_iterator++];
    00567   }
    00568
    00569   /* virtual */ bool Monospace()
    00570   {
    00571     return true;
    00572   }
    00573
    00574   /* virtual */ void SetFontNames(FreeTypeSettings *settings, const char *font_name)
    00575   {
    00576 #ifdef WITH_FREETYPE
    00577     strecpy(settings->mono_font, font_name, lastof(settings->mono_font));
    00578 #endif /* WITH_FREETYPE */
    00579   }
    00580
    00584   void LoadTextfile()
    00585   {
    00586     this->lines.Clear();
    00587
    00588     /* Does GRF have a file of the demanded type? */
    00589     const char *textfile = this->grf_config->GetTextfile(file_type);
    00590     if (textfile == NULL) return;
    00591
    00592     /* Get text from file */
    00593     size_t filesize;
    00594     FILE *handle = FioFOpenFile(textfile, "rb", NEWGRF_DIR, &filesize);
    00595     if (handle == NULL) return;
    00596
    00597     this->text = ReallocT(this->text, filesize + 1);
    00598     size_t read = fread(this->text, 1, filesize, handle);
    00599     fclose(handle);
    00600
    00601     if (read != filesize) return;
    00602
    00603     this->text[filesize] = '\0';
    00604
    00605     /* Replace tabs and line feeds with a space since str_validate removes those. */
    00606     for (char *p = this->text; *p != '\0'; p++) {
    00607       if (*p == '\t' || *p == '\r') *p = ' ';
    00608     }
    00609
    00610     /* Check for the byte-order-mark, and skip it if needed. */
    00611     char *p = this->text + (strncmp("\xEF\xBB\xBF", this->text, 3) == 0 ? 3 : 0);
    00612
    00613     /* Make sure the string is a valid UTF-8 sequence. */
    00614     str_validate(p, this->text + filesize, SVS_REPLACE_WITH_QUESTION_MARK | SVS_ALLOW_NEWLINE);
    00615
    00616     /* Split the string on newlines. */
    00617     *this->lines.Append() = p;
    00618     for (; *p != '\0'; p++) {
    00619       if (*p == '\n') {
    00620         *p = '\0';
    00621         *this->lines.Append() = p + 1;
    00622       }
    00623     }
    00624
    00625     CheckForMissingGlyphs(true, this);
    00626
    00627     /* Initialize scrollbars */
    00628     this->vscroll->SetCount(this->lines.Length());
    00629
    00630     this->max_length = 0;
    00631     for (uint i = 0; i < this->lines.Length(); i++) {
    00632       this->max_length = max(this->max_length, GetStringBoundingBox(this->lines[i], FS_MONO).width);
    00633     }
    00634     this->hscroll->SetCount(this->max_length + WD_FRAMETEXT_LEFT + WD_FRAMETEXT_RIGHT);
    00635     this->hscroll->SetStepSize(10); // Speed up horizontal scrollbar
    00636   }
    00637 };
    00638
    00639 static const NWidgetPart _nested_newgrf_textfile_widgets[] = {
    00640   NWidget(NWID_HORIZONTAL),
    00641     NWidget(WWT_CLOSEBOX, COLOUR_MAUVE),
    00642     NWidget(WWT_CAPTION, COLOUR_MAUVE, WID_NT_CAPTION), SetDataTip(STR_NULL, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    00643   EndContainer(),
    00644   NWidget(NWID_HORIZONTAL),
    00645     NWidget(WWT_PANEL, COLOUR_MAUVE, WID_NT_BACKGROUND), SetMinimalSize(200, 125), SetResize(1, 12), SetScrollbar(WID_NT_VSCROLLBAR),
    00646     EndContainer(),
    00647     NWidget(NWID_VERTICAL),
    00648       NWidget(NWID_VSCROLLBAR, COLOUR_MAUVE, WID_NT_VSCROLLBAR),
    00649     EndContainer(),
    00650   EndContainer(),
    00651   NWidget(NWID_HORIZONTAL),
    00652     NWidget(NWID_HSCROLLBAR, COLOUR_MAUVE, WID_NT_HSCROLLBAR),
    00653     NWidget(WWT_RESIZEBOX, COLOUR_MAUVE),
    00654   EndContainer(),
    00655 };
    00656
    00658 static const WindowDesc _newgrf_textfile_desc(
    00659   WDP_CENTER, 630, 460,
    00660   WC_NEWGRF_TEXTFILE, WC_NONE,
    00661   WDF_UNCLICK_BUTTONS,
    00662   _nested_newgrf_textfile_widgets, lengthof(_nested_newgrf_textfile_widgets)
    00663 );
    00664
    00665 void ShowNewGRFTextfileWindow(const GRFConfig *c, TextfileType file_type)
    00666 {
    00667   DeleteWindowByClass(WC_NEWGRF_TEXTFILE);
    00668   new NewGRFTextfileWindow(&_newgrf_textfile_desc, c, file_type);
    00669 }
    00670
    00671 static GRFPresetList _grf_preset_list;
    00672
    00673 class DropDownListPresetItem : public DropDownListItem {
    00674 public:
    00675   DropDownListPresetItem(int result) : DropDownListItem(result, false) {}
    00676
    00677   virtual ~DropDownListPresetItem() {}
    00678
    00679   bool Selectable() const
    00680   {
    00681     return true;
    00682   }
    00683
    00684   void Draw(int left, int right, int top, int bottom, bool sel, int bg_colour) const
    00685   {
    00686     DrawString(left + 2, right + 2, top, _grf_preset_list[this->result], sel ? TC_WHITE : TC_BLACK);
    00687   }
    00688 };
    00689
    00690 static void NewGRFConfirmationCallback(Window *w, bool confirmed);
    00691
    00695 struct NewGRFWindow : public QueryStringBaseWindow, NewGRFScanCallback {
    00696   typedef GUIList<const GRFConfig *> GUIGRFConfigList;
    00697
    00698   static const uint EDITBOX_MAX_SIZE   =  50;
    00699
    00700   static Listing   last_sorting;
    00701   static Filtering last_filtering;
    00702   static GUIGRFConfigList::SortFunction   * const sorter_funcs[];
    00703   static GUIGRFConfigList::FilterFunction * const filter_funcs[];
    00704
    00705   GUIGRFConfigList avails;
    00706   const GRFConfig *avail_sel;
    00707   int avail_pos;
    00708
    00709   GRFConfig *actives;
    00710   GRFConfig *active_sel;
    00711
    00712   GRFConfig **orig_list;
    00713   bool editable;
    00714   bool show_params;
    00715   bool execute;
    00716   int preset;
    00717
    00718   Scrollbar *vscroll;
    00719   Scrollbar *vscroll2;
    00720
    00721   NewGRFWindow(const WindowDesc *desc, bool editable, bool show_params, bool execute, GRFConfig **orig_list) : QueryStringBaseWindow(EDITBOX_MAX_SIZE)
    00722   {
    00723     this->avail_sel   = NULL;
    00724     this->avail_pos   = -1;
    00725     this->active_sel  = NULL;
    00726     this->actives     = NULL;
    00727     this->orig_list   = orig_list;
    00728     this->editable    = editable;
    00729     this->execute     = execute;
    00730     this->show_params = show_params;
    00731     this->preset      = -1;
    00732
    00733     CopyGRFConfigList(&this->actives, *orig_list, false);
    00734     GetGRFPresetList(&_grf_preset_list);
    00735
    00736     this->CreateNestedTree(desc);
    00737     this->vscroll = this->GetScrollbar(WID_NS_SCROLLBAR);
    00738     this->vscroll2 = this->GetScrollbar(WID_NS_SCROLL2BAR);
    00739
    00740     this->GetWidget<NWidgetStacked>(WID_NS_SHOW_REMOVE)->SetDisplayedPlane(this->editable ? 0 : 1);
    00741     this->GetWidget<NWidgetStacked>(WID_NS_SHOW_APPLY)->SetDisplayedPlane(this->editable ? 0 : this->show_params ? 1 : SZSP_HORIZONTAL);
    00742     this->FinishInitNested(desc, WN_GAME_OPTIONS_NEWGRF_STATE);
    00743
    00744     InitializeTextBuffer(&this->text, this->edit_str_buf, this->edit_str_size);
    00745     this->SetFocusedWidget(WID_NS_FILTER);
    00746
    00747     this->avails.SetListing(this->last_sorting);
    00748     this->avails.SetFiltering(this->last_filtering);
    00749     this->avails.SetSortFuncs(this->sorter_funcs);
    00750     this->avails.SetFilterFuncs(this->filter_funcs);
    00751     this->avails.ForceRebuild();
    00752
    00753     this->OnInvalidateData(GOID_NEWGRF_LIST_EDITED);
    00754   }
    00755
    00756   ~NewGRFWindow()
    00757   {
    00758     DeleteWindowByClass(WC_GRF_PARAMETERS);
    00759     DeleteWindowByClass(WC_NEWGRF_TEXTFILE);
    00760
    00761     if (this->editable && !this->execute) {
    00762       CopyGRFConfigList(this->orig_list, this->actives, true);
    00763       ResetGRFConfig(false);
    00764       ReloadNewGRFData();
    00765     }
    00766
    00767     /* Remove the temporary copy of grf-list used in window */
    00768     ClearGRFConfigList(&this->actives);
    00769     _grf_preset_list.Clear();
    00770   }
    00771
    00772   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
    00773   {
    00774     switch (widget) {
    00775       case WID_NS_FILE_LIST:
    00776       {
    00777         Dimension d = maxdim(GetSpriteSize(SPR_SQUARE), GetSpriteSize(SPR_WARNING_SIGN));
    00778         resize->height = max(d.height + 2U, FONT_HEIGHT_NORMAL + 2U);
    00779         size->height = max(size->height, WD_FRAMERECT_TOP + 6 * resize->height + WD_FRAMERECT_BOTTOM);
    00780         break;
    00781       }
    00782
    00783       case WID_NS_AVAIL_LIST:
    00784         resize->height = max(12, FONT_HEIGHT_NORMAL + 2);
    00785         size->height = max(size->height, WD_FRAMERECT_TOP + 8 * resize->height + WD_FRAMERECT_BOTTOM);
    00786         break;
    00787
    00788       case WID_NS_NEWGRF_INFO_TITLE: {
    00789         Dimension dim = GetStringBoundingBox(STR_NEWGRF_SETTINGS_INFO_TITLE);
    00790         size->height = max(size->height, dim.height + WD_FRAMETEXT_TOP + WD_FRAMETEXT_BOTTOM);
    00791         size->width  = max(size->width,  dim.width  + WD_FRAMETEXT_LEFT + WD_FRAMETEXT_RIGHT);
    00792         break;
    00793       }
    00794
    00795       case WID_NS_NEWGRF_INFO:
    00796         size->height = max(size->height, WD_FRAMERECT_TOP + 10 * FONT_HEIGHT_NORMAL + WD_FRAMERECT_BOTTOM + padding.height + 2);
    00797         break;
    00798
    00799       case WID_NS_PRESET_LIST: {
    00800         Dimension d = GetStringBoundingBox(STR_NUM_CUSTOM);
    00801         for (uint i = 0; i < _grf_preset_list.Length(); i++) {
    00802           if (_grf_preset_list[i] != NULL) {
    00803             SetDParamStr(0, _grf_preset_list[i]);
    00804             d = maxdim(d, GetStringBoundingBox(STR_JUST_RAW_STRING));
    00805           }
    00806         }
    00807         d.width += padding.width;
    00808         *size = maxdim(d, *size);
    00809         break;
    00810       }
    00811
    00812       case WID_NS_CONTENT_DOWNLOAD:
    00813       case WID_NS_CONTENT_DOWNLOAD2: {
    00814         Dimension d = GetStringBoundingBox(STR_NEWGRF_SETTINGS_FIND_MISSING_CONTENT_BUTTON);
    00815         *size = maxdim(d, GetStringBoundingBox(STR_INTRO_ONLINE_CONTENT));
    00816         size->width  += padding.width;
    00817         size->height += padding.height;
    00818         break;
    00819       }
    00820     }
    00821   }
    00822
    00823   virtual void OnResize()
    00824   {
    00825     this->vscroll->SetCapacityFromWidget(this, WID_NS_FILE_LIST);
    00826     this->vscroll2->SetCapacityFromWidget(this, WID_NS_AVAIL_LIST);
    00827   }
    00828
    00829   virtual void SetStringParameters(int widget) const
    00830   {
    00831     switch (widget) {
    00832       case WID_NS_PRESET_LIST:
    00833         if (this->preset == -1) {
    00834           SetDParam(0, STR_NUM_CUSTOM);
    00835         } else {
    00836           SetDParam(0, STR_JUST_RAW_STRING);
    00837           SetDParamStr(1, _grf_preset_list[this->preset]);
    00838         }
    00839         break;
    00840     }
    00841   }
    00842
    00843   virtual void OnPaint()
    00844   {
    00845     this->DrawWidgets();
    00846     if (this->editable) this->DrawEditBox(WID_NS_FILTER);
    00847   }
    00848
    00854   inline PaletteID GetPalette(const GRFConfig *c) const
    00855   {
    00856     PaletteID pal;
    00857
    00858     /* Pick a colour */
    00859     switch (c->status) {
    00860       case GCS_NOT_FOUND:
    00861       case GCS_DISABLED:
    00862         pal = PALETTE_TO_RED;
    00863         break;
    00864       case GCS_ACTIVATED:
    00865         pal = PALETTE_TO_GREEN;
    00866         break;
    00867       default:
    00868         pal = PALETTE_TO_BLUE;
    00869         break;
    00870     }
    00871
    00872     /* Do not show a "not-failure" colour when it actually failed to load */
    00873     if (pal != PALETTE_TO_RED) {
    00874       if (HasBit(c->flags, GCF_STATIC)) {
    00875         pal = PALETTE_TO_GREY;
    00876       } else if (HasBit(c->flags, GCF_COMPATIBLE)) {
    00877         pal = PALETTE_TO_ORANGE;
    00878       }
    00879     }
    00880
    00881     return pal;
    00882   }
    00883
    00884   virtual void DrawWidget(const Rect &r, int widget) const
    00885   {
    00886     switch (widget) {
    00887       case WID_NS_FILE_LIST: {
    00888         GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, PC_BLACK);
    00889
    00890         uint step_height = this->GetWidget<NWidgetBase>(WID_NS_FILE_LIST)->resize_y;
    00891         uint y = r.top + WD_FRAMERECT_TOP;
    00892         Dimension square = GetSpriteSize(SPR_SQUARE);
    00893         Dimension warning = GetSpriteSize(SPR_WARNING_SIGN);
    00894         int square_offset_y = (step_height - square.height) / 2;
    00895         int warning_offset_y = (step_height - warning.height) / 2;
    00896         int offset_y = (step_height - FONT_HEIGHT_NORMAL) / 2;
    00897
    00898         bool rtl = _current_text_dir == TD_RTL;
    00899         uint text_left    = rtl ? r.left + WD_FRAMERECT_LEFT : r.left + square.width + 15;
    00900         uint text_right   = rtl ? r.right - square.width - 15 : r.right - WD_FRAMERECT_RIGHT;
    00901         uint square_left  = rtl ? r.right - square.width - 5 : r.left + 5;
    00902         uint warning_left = rtl ? r.right - square.width - warning.width - 10 : r.left + square.width + 10;
    00903
    00904         int i = 0;
    00905         for (const GRFConfig *c = this->actives; c != NULL; c = c->next, i++) {
    00906           if (this->vscroll->IsVisible(i)) {
    00907             const char *text = c->GetName();
    00908             bool h = (this->active_sel == c);
    00909             PaletteID pal = this->GetPalette(c);
    00910
    00911             if (h) GfxFillRect(r.left + 1, y, r.right - 1, y + step_height - 1, PC_DARK_BLUE);
    00912             DrawSprite(SPR_SQUARE, pal, square_left, y + square_offset_y);
    00913             if (c->error != NULL) DrawSprite(SPR_WARNING_SIGN, 0, warning_left, y + warning_offset_y);
    00914             uint txtoffset = c->error == NULL ? 0 : warning.width;
    00915             DrawString(text_left + (rtl ? 0 : txtoffset), text_right - (rtl ? txtoffset : 0), y + offset_y, text, h ? TC_WHITE : TC_ORANGE);
    00916             y += step_height;
    00917           }
    00918         }
    00919         break;
    00920       }
    00921
    00922       case WID_NS_AVAIL_LIST: {
    00923         GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, PC_BLACK);
    00924
    00925         uint step_height = this->GetWidget<NWidgetBase>(WID_NS_AVAIL_LIST)->resize_y;
    00926         int offset_y = (step_height - FONT_HEIGHT_NORMAL) / 2;
    00927         uint y = r.top + WD_FRAMERECT_TOP;
    00928         uint min_index = this->vscroll2->GetPosition();
    00929         uint max_index = min(min_index + this->vscroll2->GetCapacity(), this->avails.Length());
    00930
    00931         for (uint i = min_index; i < max_index; i++) {
    00932           const GRFConfig *c = this->avails[i];
    00933           bool h = (c == this->avail_sel);
    00934           const char *text = c->GetName();
    00935
    00936           if (h) GfxFillRect(r.left + 1, y, r.right - 1, y + step_height - 1, PC_DARK_BLUE);
    00937           DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y + offset_y, text, h ? TC_WHITE : TC_SILVER);
    00938           y += step_height;
    00939         }
    00940         break;
    00941       }
    00942
    00943       case WID_NS_NEWGRF_INFO_TITLE:
    00944         /* Create the nice grayish rectangle at the details top. */
    00945         GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, PC_DARK_BLUE);
    00946         DrawString(r.left, r.right, (r.top + r.bottom - FONT_HEIGHT_NORMAL) / 2, STR_NEWGRF_SETTINGS_INFO_TITLE, TC_FROMSTRING, SA_HOR_CENTER);
    00947         break;
    00948
    00949       case WID_NS_NEWGRF_INFO: {
    00950         const GRFConfig *selected = this->active_sel;
    00951         if (selected == NULL) selected = this->avail_sel;
    00952         if (selected != NULL) {
    00953           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);
    00954         }
    00955         break;
    00956       }
    00957     }
    00958   }
    00959
    00960   virtual void OnClick(Point pt, int widget, int click_count)
    00961   {
    00962     if (widget >= WID_NS_NEWGRF_TEXTFILE && widget < WID_NS_NEWGRF_TEXTFILE + TFT_END) {
    00963       if (this->active_sel == NULL && this->avail_sel == NULL) return;
    00964
    00965       ShowNewGRFTextfileWindow(this->active_sel != NULL ? this->active_sel : this->avail_sel, (TextfileType)(widget - WID_NS_NEWGRF_TEXTFILE));
    00966       return;
    00967     }
    00968
    00969     switch (widget) {
    00970       case WID_NS_PRESET_LIST: {
    00971         DropDownList *list = new DropDownList();
    00972
    00973         /* Add 'None' option for clearing list */
    00974         list->push_back(new DropDownListStringItem(STR_NONE, -1, false));
    00975
    00976         for (uint i = 0; i < _grf_preset_list.Length(); i++) {
    00977           if (_grf_preset_list[i] != NULL) {
    00978             list->push_back(new DropDownListPresetItem(i));
    00979           }
    00980         }
    00981
    00982         this->DeleteChildWindows(WC_QUERY_STRING); // Remove the parameter query window
    00983         ShowDropDownList(this, list, this->preset, WID_NS_PRESET_LIST);
    00984         break;
    00985       }
    00986
    00987       case WID_NS_OPEN_URL: {
    00988         const GRFConfig *c = (this->avail_sel == NULL) ? this->active_sel : this->avail_sel;
    00989
    00990         extern void OpenBrowser(const char *url);
    00991         OpenBrowser(c->GetURL());
    00992         break;
    00993       }
    00994
    00995       case WID_NS_PRESET_SAVE:
    00996         ShowQueryString(STR_EMPTY, STR_NEWGRF_SETTINGS_PRESET_SAVE_QUERY, 32, this, CS_ALPHANUMERAL, QSF_NONE);
    00997         break;
    00998
    00999       case WID_NS_PRESET_DELETE:
    01000         if (this->preset == -1) return;
    01001
    01002         DeleteGRFPresetFromConfig(_grf_preset_list[this->preset]);
    01003         GetGRFPresetList(&_grf_preset_list);
    01004         this->preset = -1;
    01005         this->InvalidateData();
    01006         this->DeleteChildWindows(WC_QUERY_STRING); // Remove the parameter query window
    01007         break;
    01008
    01009       case WID_NS_MOVE_UP: { // Move GRF up
    01010         if (this->active_sel == NULL || !this->editable) break;
    01011
    01012         int pos = 0;
    01013         for (GRFConfig **pc = &this->actives; *pc != NULL; pc = &(*pc)->next, pos++) {
    01014           GRFConfig *c = *pc;
    01015           if (c->next == this->active_sel) {
    01016             c->next = this->active_sel->next;
    01017             this->active_sel->next = c;
    01018             *pc = this->active_sel;
    01019             break;
    01020           }
    01021         }
    01022         this->vscroll->ScrollTowards(pos);
    01023         this->preset = -1;
    01024         this->InvalidateData();
    01025         break;
    01026       }
    01027
    01028       case WID_NS_MOVE_DOWN: { // Move GRF down
    01029         if (this->active_sel == NULL || !this->editable) break;
    01030
    01031         int pos = 1; // Start at 1 as we swap the selected newgrf with the next one
    01032         for (GRFConfig **pc = &this->actives; *pc != NULL; pc = &(*pc)->next, pos++) {
    01033           GRFConfig *c = *pc;
    01034           if (c == this->active_sel) {
    01035             *pc = c->next;
    01036             c->next = c->next->next;
    01037             (*pc)->next = c;
    01038             break;
    01039           }
    01040         }
    01041         this->vscroll->ScrollTowards(pos);
    01042         this->preset = -1;
    01043         this->InvalidateData();
    01044         break;
    01045       }
    01046
    01047       case WID_NS_FILE_LIST: { // Select an active GRF.
    01048         uint i = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_NS_FILE_LIST);
    01049
    01050         GRFConfig *c;
    01051         for (c = this->actives; c != NULL && i > 0; c = c->next, i--) {}
    01052
    01053         if (this->active_sel != c) DeleteWindowByClass(WC_GRF_PARAMETERS);
    01054         this->active_sel = c;
    01055         this->avail_sel = NULL;
    01056         this->avail_pos = -1;
    01057
    01058         this->InvalidateData();
    01059         if (click_count == 1) break;
    01060         /* FALL THROUGH, with double click. */
    01061       }
    01062
    01063       case WID_NS_REMOVE: { // Remove GRF
    01064         if (this->active_sel == NULL || !this->editable) break;
    01065         DeleteWindowByClass(WC_GRF_PARAMETERS);
    01066
    01067         /* Choose the next GRF file to be the selected file. */
    01068         GRFConfig *newsel = this->active_sel->next;
    01069         for (GRFConfig **pc = &this->actives; *pc != NULL; pc = &(*pc)->next) {
    01070           GRFConfig *c = *pc;
    01071           /* If the new selection is empty (i.e. we're deleting the last item
    01072            * in the list, pick the file just before the selected file */
    01073           if (newsel == NULL && c->next == this->active_sel) newsel = c;
    01074
    01075           if (c == this->active_sel) {
    01076             *pc = c->next;
    01077             delete c;
    01078             break;
    01079           }
    01080         }
    01081
    01082         this->active_sel = newsel;
    01083         this->preset = -1;
    01084         this->avail_pos = -1;
    01085         this->avail_sel = NULL;
    01086         this->avails.ForceRebuild();
    01087         this->InvalidateData(GOID_NEWGRF_LIST_EDITED);
    01088         break;
    01089       }
    01090
    01091       case WID_NS_AVAIL_LIST: { // Select a non-active GRF.
    01092         uint i = this->vscroll2->GetScrolledRowFromWidget(pt.y, this, WID_NS_AVAIL_LIST);
    01093         this->active_sel = NULL;
    01094         DeleteWindowByClass(WC_GRF_PARAMETERS);
    01095         if (i < this->avails.Length()) {
    01096           this->avail_sel = this->avails[i];
    01097           this->avail_pos = i;
    01098         }
    01099         this->InvalidateData();
    01100         if (click_count == 1) break;
    01101         /* FALL THROUGH, with double click. */
    01102       }
    01103
    01104       case WID_NS_ADD: {
    01105         if (this->avail_sel == NULL || !this->editable || HasBit(this->avail_sel->flags, GCF_INVALID)) break;
    01106
    01107         GRFConfig **list;
    01108         /* Find last entry in the list, checking for duplicate grfid on the way */
    01109         for (list = &this->actives; *list != NULL; list = &(*list)->next) {
    01110           if ((*list)->ident.grfid == this->avail_sel->ident.grfid) {
    01111             ShowErrorMessage(STR_NEWGRF_DUPLICATE_GRFID, INVALID_STRING_ID, WL_INFO);
    01112             return;
    01113           }
    01114         }
    01115
    01116         GRFConfig *c = new GRFConfig(*this->avail_sel); // Copy GRF details from scanned list.
    01117         c->SetParameterDefaults();
    01118         *list = c; // Append GRF config to configuration list.
    01119
    01120         /* Select next (or previous, if last one) item in the list. */
    01121         int new_pos = this->avail_pos + 1;
    01122         if (new_pos >= (int)this->avails.Length()) new_pos = this->avail_pos - 1;
    01123         this->avail_pos = new_pos;
    01124         if (new_pos >= 0) this->avail_sel = this->avails[new_pos];
    01125
    01126         this->avails.ForceRebuild();
    01127         this->InvalidateData(GOID_NEWGRF_LIST_EDITED);
    01128         break;
    01129       }
    01130
    01131       case WID_NS_APPLY_CHANGES: // Apply changes made to GRF list
    01132         if (!this->editable) break;
    01133         if (this->execute) {
    01134           ShowQuery(
    01135             STR_NEWGRF_POPUP_CAUTION_CAPTION,
    01136             STR_NEWGRF_CONFIRMATION_TEXT,
    01137             this,
    01138             NewGRFConfirmationCallback
    01139           );
    01140         } else {
    01141           CopyGRFConfigList(this->orig_list, this->actives, true);
    01142           ResetGRFConfig(false);
    01143           ReloadNewGRFData();
    01144         }
    01145         this->DeleteChildWindows(WC_QUERY_STRING); // Remove the parameter query window
    01146         break;
    01147
    01148       case WID_NS_VIEW_PARAMETERS:
    01149       case WID_NS_SET_PARAMETERS: { // Edit parameters
    01150         if (this->active_sel == NULL || !this->show_params || this->active_sel->num_valid_params == 0) break;
    01151
    01152         OpenGRFParameterWindow(this->active_sel, this->editable);
    01153         break;
    01154       }
    01155
    01156       case WID_NS_TOGGLE_PALETTE:
    01157         if (this->active_sel != NULL || !this->editable) {
    01158           this->active_sel->palette ^= GRFP_USE_MASK;
    01159           this->SetDirty();
    01160         }
    01161         break;
    01162
    01163       case WID_NS_CONTENT_DOWNLOAD:
    01164       case WID_NS_CONTENT_DOWNLOAD2:
    01165         if (!_network_available) {
    01166           ShowErrorMessage(STR_NETWORK_ERROR_NOTAVAILABLE, INVALID_STRING_ID, WL_ERROR);
    01167         } else {
    01168 #if defined(ENABLE_NETWORK)
    01169           this->DeleteChildWindows(WC_QUERY_STRING); // Remove the parameter query window
    01170
    01171           ShowMissingContentWindow(this->actives);
    01172 #endif
    01173         }
    01174         break;
    01175
    01176       case WID_NS_RESCAN_FILES:
    01177       case WID_NS_RESCAN_FILES2:
    01178         ScanNewGRFFiles(this);
    01179         break;
    01180     }
    01181   }
    01182
    01183   virtual void OnNewGRFsScanned()
    01184   {
    01185     this->avail_sel = NULL;
    01186     this->avail_pos = -1;
    01187     this->avails.ForceRebuild();
    01188     this->DeleteChildWindows(WC_QUERY_STRING);  // Remove the parameter query window
    01189     this->DeleteChildWindows(WC_NEWGRF_TEXTFILE); // Remove the view textfile window
    01190   }
    01191
    01192   virtual void OnDropdownSelect(int widget, int index)
    01193   {
    01194     if (!this->editable) return;
    01195
    01196     ClearGRFConfigList(&this->actives);
    01197     this->preset = index;
    01198
    01199     if (index != -1) {
    01200       this->actives = LoadGRFPresetFromConfig(_grf_preset_list[index]);
    01201     }
    01202     this->avails.ForceRebuild();
    01203
    01204     DeleteWindowByClass(WC_GRF_PARAMETERS);
    01205     this->active_sel = NULL;
    01206     this->InvalidateData(GOID_NEWGRF_PRESET_LOADED);
    01207   }
    01208
    01209   virtual void OnQueryTextFinished(char *str)
    01210   {
    01211     if (str == NULL) return;
    01212
    01213     SaveGRFPresetToConfig(str, this->actives);
    01214     GetGRFPresetList(&_grf_preset_list);
    01215
    01216     /* Switch to this preset */
    01217     for (uint i = 0; i < _grf_preset_list.Length(); i++) {
    01218       if (_grf_preset_list[i] != NULL && strcmp(_grf_preset_list[i], str) == 0) {
    01219         this->preset = i;
    01220         break;
    01221       }
    01222     }
    01223
    01224     this->InvalidateData();
    01225   }
    01226
    01232   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
    01233   {
    01234     if (!gui_scope) return;
    01235     switch (data) {
    01236       default:
    01237         /* Nothing important to do */
    01238         break;
    01239
    01240       case GOID_NEWGRF_RESCANNED:
    01241         /* Search the list for items that are now found and mark them as such. */
    01242         for (GRFConfig **l = &this->actives; *l != NULL; l = &(*l)->next) {
    01243           GRFConfig *c = *l;
    01244           bool compatible = HasBit(c->flags, GCF_COMPATIBLE);
    01245           if (c->status != GCS_NOT_FOUND && !compatible) continue;
    01246
    01247           const GRFConfig *f = FindGRFConfig(c->ident.grfid, FGCM_EXACT, compatible ? c->original_md5sum : c->ident.md5sum);
    01248           if (f == NULL || HasBit(f->flags, GCF_INVALID)) continue;
    01249
    01250           *l = new GRFConfig(*f);
    01251           (*l)->next = c->next;
    01252
    01253           if (active_sel == c) active_sel = *l;
    01254
    01255           delete c;
    01256         }
    01257
    01258         this->avails.ForceRebuild();
    01259         /* FALL THROUGH */
    01260       case GOID_NEWGRF_LIST_EDITED:
    01261         this->preset = -1;
    01262         /* FALL THROUGH */
    01263       case GOID_NEWGRF_PRESET_LOADED: {
    01264         /* Update scrollbars */
    01265         int i = 0;
    01266         for (const GRFConfig *c = this->actives; c != NULL; c = c->next, i++) {}
    01267
    01268         this->vscroll->SetCapacityFromWidget(this, WID_NS_FILE_LIST);
    01269         this->vscroll->SetCount(i);
    01270
    01271         this->vscroll2->SetCapacityFromWidget(this, WID_NS_AVAIL_LIST);
    01272         if (this->avail_pos >= 0) this->vscroll2->ScrollTowards(this->avail_pos);
    01273         break;
    01274       }
    01275     }
    01276
    01277     this->BuildAvailables();
    01278
    01279     this->SetWidgetsDisabledState(!this->editable,
    01280       WID_NS_PRESET_LIST,
    01281       WID_NS_APPLY_CHANGES,
    01282       WID_NS_TOGGLE_PALETTE,
    01283       WIDGET_LIST_END
    01284     );
    01285     this->SetWidgetDisabledState(WID_NS_ADD, !this->editable || this->avail_sel == NULL || HasBit(this->avail_sel->flags, GCF_INVALID));
    01286
    01287     bool disable_all = this->active_sel == NULL || !this->editable;
    01288     this->SetWidgetsDisabledState(disable_all,
    01289       WID_NS_REMOVE,
    01290       WID_NS_MOVE_UP,
    01291       WID_NS_MOVE_DOWN,
    01292       WIDGET_LIST_END
    01293     );
    01294
    01295     const GRFConfig *c = (this->avail_sel == NULL) ? this->active_sel : this->avail_sel;
    01296     for (TextfileType tft = TFT_BEGIN; tft < TFT_END; tft++) {
    01297       this->SetWidgetDisabledState(WID_NS_NEWGRF_TEXTFILE + tft, c == NULL || c->GetTextfile(tft) == NULL);
    01298     }
    01299     this->SetWidgetDisabledState(WID_NS_OPEN_URL, c == NULL || StrEmpty(c->GetURL()));
    01300
    01301     this->SetWidgetDisabledState(WID_NS_SET_PARAMETERS, !this->show_params || this->active_sel == NULL || this->active_sel->num_valid_params == 0);
    01302     this->SetWidgetDisabledState(WID_NS_VIEW_PARAMETERS, !this->show_params || this->active_sel == NULL || this->active_sel->num_valid_params == 0);
    01303     this->SetWidgetDisabledState(WID_NS_TOGGLE_PALETTE, disable_all);
    01304
    01305     if (!disable_all) {
    01306       /* All widgets are now enabled, so disable widgets we can't use */
    01307       if (this->active_sel == this->actives)    this->DisableWidget(WID_NS_MOVE_UP);
    01308       if (this->active_sel->next == NULL)       this->DisableWidget(WID_NS_MOVE_DOWN);
    01309       if (this->active_sel->IsOpenTTDBaseGRF()) this->DisableWidget(WID_NS_REMOVE);
    01310     }
    01311
    01312     this->SetWidgetDisabledState(WID_NS_PRESET_DELETE, this->preset == -1);
    01313
    01314     bool has_missing = false;
    01315     bool has_compatible = false;
    01316     for (const GRFConfig *c = this->actives; !has_missing && c != NULL; c = c->next) {
    01317       has_missing    |= c->status == GCS_NOT_FOUND;
    01318       has_compatible |= HasBit(c->flags, GCF_COMPATIBLE);
    01319     }
    01320     uint16 widget_data;
    01321     StringID tool_tip;
    01322     if (has_missing || has_compatible) {
    01323       widget_data = STR_NEWGRF_SETTINGS_FIND_MISSING_CONTENT_BUTTON;
    01324       tool_tip    = STR_NEWGRF_SETTINGS_FIND_MISSING_CONTENT_TOOLTIP;
    01325     } else {
    01326       widget_data = STR_INTRO_ONLINE_CONTENT;
    01327       tool_tip    = STR_INTRO_TOOLTIP_ONLINE_CONTENT;
    01328     }
    01329     this->GetWidget<NWidgetCore>(WID_NS_CONTENT_DOWNLOAD)->widget_data  = widget_data;
    01330     this->GetWidget<NWidgetCore>(WID_NS_CONTENT_DOWNLOAD)->tool_tip     = tool_tip;
    01331     this->GetWidget<NWidgetCore>(WID_NS_CONTENT_DOWNLOAD2)->widget_data = widget_data;
    01332     this->GetWidget<NWidgetCore>(WID_NS_CONTENT_DOWNLOAD2)->tool_tip    = tool_tip;
    01333
    01334     this->SetWidgetDisabledState(WID_NS_PRESET_SAVE, has_missing);
    01335   }
    01336
    01337   virtual void OnMouseLoop()
    01338   {
    01339     if (this->editable) this->HandleEditBox(WID_NS_FILTER);
    01340   }
    01341
    01342   virtual EventState OnKeyPress(uint16 key, uint16 keycode)
    01343   {
    01344     if (!this->editable) return ES_NOT_HANDLED;
    01345
    01346     switch (keycode) {
    01347       case WKC_UP:
    01348         /* scroll up by one */
    01349         if (this->avail_pos > 0) this->avail_pos--;
    01350         break;
    01351
    01352       case WKC_DOWN:
    01353         /* scroll down by one */
    01354         if (this->avail_pos < (int)this->avails.Length() - 1) this->avail_pos++;
    01355         break;
    01356
    01357       case WKC_PAGEUP:
    01358         /* scroll up a page */
    01359         this->avail_pos = (this->avail_pos < this->vscroll2->GetCapacity()) ? 0 : this->avail_pos - this->vscroll2->GetCapacity();
    01360         break;
    01361
    01362       case WKC_PAGEDOWN:
    01363         /* scroll down a page */
    01364         this->avail_pos = min(this->avail_pos + this->vscroll2->GetCapacity(), (int)this->avails.Length() - 1);
    01365         break;
    01366
    01367       case WKC_HOME:
    01368         /* jump to beginning */
    01369         this->avail_pos = 0;
    01370         break;
    01371
    01372       case WKC_END:
    01373         /* jump to end */
    01374         this->avail_pos = this->avails.Length() - 1;
    01375         break;
    01376
    01377       default: {
    01378         /* Handle editbox input */
    01379         EventState state = ES_NOT_HANDLED;
    01380         if (this->HandleEditBoxKey(WID_NS_FILTER, key, keycode, state) == HEBR_EDITING) {
    01381           this->OnOSKInput(WID_NS_FILTER);
    01382         }
    01383         return state;
    01384       }
    01385     }
    01386
    01387     if (this->avails.Length() == 0) this->avail_pos = -1;
    01388     if (this->avail_pos >= 0) {
    01389       this->avail_sel = this->avails[this->avail_pos];
    01390       this->vscroll2->ScrollTowards(this->avail_pos);
    01391       this->InvalidateData(0);
    01392     }
    01393
    01394     return ES_HANDLED;
    01395   }
    01396
    01397   virtual void OnOSKInput(int wid)
    01398   {
    01399     if (!this->editable) return;
    01400
    01401     this->avails.SetFilterState(!StrEmpty(this->edit_str_buf));
    01402     this->avails.ForceRebuild();
    01403     this->InvalidateData(0);
    01404   }
    01405
    01406 private:
    01408   static int CDECL NameSorter(const GRFConfig * const *a, const GRFConfig * const *b)
    01409   {
    01410     int i = strnatcmp((*a)->GetName(), (*b)->GetName()); // Sort by name (natural sorting).
    01411     if (i != 0) return i;
    01412
    01413     i = (*a)->version - (*b)->version;
    01414     if (i != 0) return i;
    01415
    01416     return memcmp((*a)->ident.md5sum, (*b)->ident.md5sum, lengthof((*b)->ident.md5sum));
    01417   }
    01418
    01420   static bool CDECL TagNameFilter(const GRFConfig * const *a, const char *filter_string)
    01421   {
    01422     if (strcasestr((*a)->GetName(), filter_string) != NULL) return true;
    01423     if ((*a)->filename != NULL && strcasestr((*a)->filename, filter_string) != NULL) return true;
    01424     if ((*a)->GetDescription() != NULL && strcasestr((*a)->GetDescription(), filter_string) != NULL) return true;
    01425     return false;
    01426   }
    01427
    01428   void BuildAvailables()
    01429   {
    01430     if (!this->avails.NeedRebuild()) return;
    01431
    01432     this->avails.Clear();
    01433
    01434     for (const GRFConfig *c = _all_grfs; c != NULL; c = c->next) {
    01435       bool found = false;
    01436       for (const GRFConfig *grf = this->actives; grf != NULL && !found; grf = grf->next) found = grf->ident.HasGrfIdentifier(c->ident.grfid, c->ident.md5sum);
    01437       if (found) continue;
    01438
    01439       if (_settings_client.gui.newgrf_show_old_versions) {
    01440         *this->avails.Append() = c;
    01441       } else {
    01442         const GRFConfig *best = FindGRFConfig(c->ident.grfid, HasBit(c->flags, GCF_INVALID) ? FGCM_NEWEST : FGCM_NEWEST_VALID);
    01443         /*
    01444          * If the best version is 0, then all NewGRF with this GRF ID
    01445          * have version 0, so for backward compatability reasons we
    01446          * want to show them all.
    01447          * If we are the best version, then we definitely want to
    01448          * show that NewGRF!.
    01449          */
    01450         if (best->version == 0 || best->ident.HasGrfIdentifier(c->ident.grfid, c->ident.md5sum)) {
    01451           *this->avails.Append() = c;
    01452         }
    01453       }
    01454     }
    01455
    01456     this->avails.Filter(this->edit_str_buf);
    01457     this->avails.Compact();
    01458     this->avails.RebuildDone();
    01459     this->avails.Sort();
    01460
    01461     if (this->avail_sel != NULL) {
    01462       this->avail_pos = this->avails.FindIndex(this->avail_sel);
    01463       if (this->avail_pos < 0) this->avail_sel = NULL;
    01464     }
    01465
    01466     this->vscroll2->SetCount(this->avails.Length()); // Update the scrollbar
    01467   }
    01468 };
    01469
    01470 #if defined(ENABLE_NETWORK)
    01471
    01475 void ShowMissingContentWindow(const GRFConfig *list)
    01476 {
    01477   /* Only show the things in the current list, or everything when nothing's selected */
    01478   ContentVector cv;
    01479   for (const GRFConfig *c = list; c != NULL; c = c->next) {
    01480     if (c->status != GCS_NOT_FOUND && !HasBit(c->flags, GCF_COMPATIBLE)) continue;
    01481
    01482     ContentInfo *ci = new ContentInfo();
    01483     ci->type = CONTENT_TYPE_NEWGRF;
    01484     ci->state = ContentInfo::DOES_NOT_EXIST;
    01485     ttd_strlcpy(ci->name, c->GetName(), lengthof(ci->name));
    01486     ci->unique_id = BSWAP32(c->ident.grfid);
    01487     memcpy(ci->md5sum, HasBit(c->flags, GCF_COMPATIBLE) ? c->original_md5sum : c->ident.md5sum, sizeof(ci->md5sum));
    01488     *cv.Append() = ci;
    01489   }
    01490   ShowNetworkContentListWindow(cv.Length() == 0 ? NULL : &cv, CONTENT_TYPE_NEWGRF);
    01491 }
    01492 #endif
    01493
    01494 Listing NewGRFWindow::last_sorting     = {false, 0};
    01495 Filtering NewGRFWindow::last_filtering = {false, 0};
    01496
    01497 NewGRFWindow::GUIGRFConfigList::SortFunction * const NewGRFWindow::sorter_funcs[] = {
    01498   &NameSorter,
    01499 };
    01500
    01501 NewGRFWindow::GUIGRFConfigList::FilterFunction * const NewGRFWindow::filter_funcs[] = {
    01502   &TagNameFilter,
    01503 };
    01504
    01511 class NWidgetNewGRFDisplay : public NWidgetContainer {
    01512 public:
    01513   static const uint INTER_LIST_SPACING;
    01514   static const uint INTER_COLUMN_SPACING;
    01515   static const uint MAX_EXTRA_INFO_WIDTH;
    01516   static const uint MIN_EXTRA_FOR_3_COLUMNS;
    01517
    01518   NWidgetBase *avs;
    01519   NWidgetBase *acs;
    01520   NWidgetBase *inf;
    01521   bool editable;
    01522
    01523   NWidgetNewGRFDisplay(NWidgetBase *avs, NWidgetBase *acs, NWidgetBase *inf) : NWidgetContainer(NWID_HORIZONTAL)
    01524   {
    01525     this->avs = avs;
    01526     this->acs = acs;
    01527     this->inf = inf;
    01528
    01529     this->Add(this->avs);
    01530     this->Add(this->acs);
    01531     this->Add(this->inf);
    01532
    01533     this->editable = true; // Temporary setting, 'real' value is set in SetupSmallestSize().
    01534   }
    01535
    01536   virtual void SetupSmallestSize(Window *w, bool init_array)
    01537   {
    01538     /* Copy state flag from the window. */
    01539     assert(dynamic_cast<NewGRFWindow *>(w) != NULL);
    01540     NewGRFWindow *ngw = (NewGRFWindow *)w;
    01541     this->editable = ngw->editable;
    01542
    01543     this->avs->SetupSmallestSize(w, init_array);
    01544     this->acs->SetupSmallestSize(w, init_array);
    01545     this->inf->SetupSmallestSize(w, init_array);
    01546
    01547     uint min_avs_width = this->avs->smallest_x + this->avs->padding_left + this->avs->padding_right;
    01548     uint min_acs_width = this->acs->smallest_x + this->acs->padding_left + this->acs->padding_right;
    01549     uint min_inf_width = this->inf->smallest_x + this->inf->padding_left + this->inf->padding_right;
    01550
    01551     uint min_avs_height = this->avs->smallest_y + this->avs->padding_top + this->avs->padding_bottom;
    01552     uint min_acs_height = this->acs->smallest_y + this->acs->padding_top + this->acs->padding_bottom;
    01553     uint min_inf_height = this->inf->smallest_y + this->inf->padding_top + this->inf->padding_bottom;
    01554
    01555     /* Smallest window is in two column mode. */
    01556     this->smallest_x = max(min_avs_width, min_acs_width) + INTER_COLUMN_SPACING + min_inf_width;
    01557     this->smallest_y = max(min_inf_height, min_acs_height + INTER_LIST_SPACING + min_avs_height);
    01558
    01559     /* Filling. */
    01560     this->fill_x = LeastCommonMultiple(this->avs->fill_x, this->acs->fill_x);
    01561     if (this->inf->fill_x > 0 && (this->fill_x == 0 || this->fill_x > this->inf->fill_x)) this->fill_x = this->inf->fill_x;
    01562
    01563     this->fill_y = this->avs->fill_y;
    01564     if (this->acs->fill_y > 0 && (this->fill_y == 0 || this->fill_y > this->acs->fill_y)) this->fill_y = this->acs->fill_y;
    01565     this->fill_y = LeastCommonMultiple(this->fill_y, this->inf->fill_y);
    01566
    01567     /* Resizing. */
    01568     this->resize_x = LeastCommonMultiple(this->avs->resize_x, this->acs->resize_x);
    01569     if (this->inf->resize_x > 0 && (this->resize_x == 0 || this->resize_x > this->inf->resize_x)) this->resize_x = this->inf->resize_x;
    01570
    01571     this->resize_y = this->avs->resize_y;
    01572     if (this->acs->resize_y > 0 && (this->resize_y == 0 || this->resize_y > this->acs->resize_y)) this->resize_y = this->acs->resize_y;
    01573     this->resize_y = LeastCommonMultiple(this->resize_y, this->inf->resize_y);
    01574
    01575     /* Make sure the height suits the 3 column (resp. not-editable) format; the 2 column format can easily fill space between the lists */
    01576     this->smallest_y = ComputeMaxSize(min_acs_height, this->smallest_y + this->resize_y - 1, this->resize_y);
    01577   }
    01578
    01579   virtual void AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)
    01580   {
    01581     this->StoreSizePosition(sizing, x, y, given_width, given_height);
    01582
    01583     uint min_avs_width = this->avs->smallest_x + this->avs->padding_left + this->avs->padding_right;
    01584     uint min_acs_width = this->acs->smallest_x + this->acs->padding_left + this->acs->padding_right;
    01585     uint min_inf_width = this->inf->smallest_x + this->inf->padding_left + this->inf->padding_right;
    01586
    01587     uint min_list_width = max(min_avs_width, min_acs_width); // Smallest width of the lists such that they have equal width (incl padding).
    01588     uint avs_extra_width = min_list_width - min_avs_width;   // Additional width needed for avs to reach min_list_width.
    01589     uint acs_extra_width = min_list_width - min_acs_width;   // Additional width needed for acs to reach min_list_width.
    01590
    01591     /* Use 2 or 3 colmuns? */
    01592     uint min_three_columns = min_avs_width + min_acs_width + min_inf_width + 2 * INTER_COLUMN_SPACING;
    01593     uint min_two_columns   = min_list_width + min_inf_width + INTER_COLUMN_SPACING;
    01594     bool use_three_columns = this->editable && (min_three_columns + MIN_EXTRA_FOR_3_COLUMNS <= given_width);
    01595
    01596     /* Info panel is a seperate column in both modes. Compute its width first. */
    01597     uint extra_width, inf_width;
    01598     if (use_three_columns) {
    01599       extra_width = given_width - min_three_columns;
    01600       inf_width = min(MAX_EXTRA_INFO_WIDTH, extra_width / 2);
    01601     } else {
    01602       extra_width = given_width - min_two_columns;
    01603       inf_width = min(MAX_EXTRA_INFO_WIDTH, extra_width / 2);
    01604     }
    01605     inf_width = ComputeMaxSize(this->inf->smallest_x, this->inf->smallest_x + inf_width, this->inf->GetHorizontalStepSize(sizing));
    01606     extra_width -= inf_width - this->inf->smallest_x;
    01607
    01608     uint inf_height = ComputeMaxSize(this->inf->smallest_y, given_height, this->inf->GetVerticalStepSize(sizing));
    01609
    01610     if (use_three_columns) {
    01611       /* Three column display, first make both lists equally wide, then divide whatever is left between both lists.
    01612        * Only keep track of what avs gets, all other space goes to acs. */
    01613       uint avs_width = min(avs_extra_width, extra_width);
    01614       extra_width -= avs_width;
    01615       extra_width -= min(acs_extra_width, extra_width);
    01616       avs_width += extra_width / 2;
    01617
    01618       avs_width = ComputeMaxSize(this->avs->smallest_x, this->avs->smallest_x + avs_width, this->avs->GetHorizontalStepSize(sizing));
    01619
    01620       uint acs_width = given_width - // Remaining space, including horizontal padding.
    01621           inf_width - this->inf->padding_left - this->inf->padding_right -
    01622           avs_width - this->avs->padding_left - this->avs->padding_right - 2 * INTER_COLUMN_SPACING;
    01623       acs_width = ComputeMaxSize(min_acs_width, acs_width, this->acs->GetHorizontalStepSize(sizing)) -
    01624           this->acs->padding_left - this->acs->padding_right;
    01625
    01626       /* Never use fill_y on these; the minimal size is choosen, so that the 3 column view looks nice */
    01627       uint avs_height = ComputeMaxSize(this->avs->smallest_y, given_height, this->avs->resize_y);
    01628       uint acs_height = ComputeMaxSize(this->acs->smallest_y, given_height, this->acs->resize_y);
    01629
    01630       /* Assign size and position to the childs. */
    01631       if (rtl) {
    01632         x += this->inf->padding_left;
    01633         this->inf->AssignSizePosition(sizing, x, y + this->inf->padding_top, inf_width, inf_height, rtl);
    01634         x += inf_width + this->inf->padding_right + INTER_COLUMN_SPACING;
    01635       } else {
    01636         x += this->avs->padding_left;
    01637         this->avs->AssignSizePosition(sizing, x, y + this->avs->padding_top, avs_width, avs_height, rtl);
    01638         x += avs_width + this->avs->padding_right + INTER_COLUMN_SPACING;
    01639       }
    01640
    01641       x += this->acs->padding_left;
    01642       this->acs->AssignSizePosition(sizing, x, y + this->acs->padding_top, acs_width, acs_height, rtl);
    01643       x += acs_width + this->acs->padding_right + INTER_COLUMN_SPACING;
    01644
    01645       if (rtl) {
    01646         x += this->avs->padding_left;
    01647         this->avs->AssignSizePosition(sizing, x, y + this->avs->padding_top, avs_width, avs_height, rtl);
    01648       } else {
    01649         x += this->inf->padding_left;
    01650         this->inf->AssignSizePosition(sizing, x, y + this->inf->padding_top, inf_width, inf_height, rtl);
    01651       }
    01652     } else {
    01653       /* Two columns, all space in extra_width goes to both lists. Since the lists are underneath each other,
    01654        * the column is min_list_width wide at least. */
    01655       uint avs_width = ComputeMaxSize(this->avs->smallest_x, this->avs->smallest_x + avs_extra_width + extra_width,
    01656           this->avs->GetHorizontalStepSize(sizing));
    01657       uint acs_width = ComputeMaxSize(this->acs->smallest_x, this->acs->smallest_x + acs_extra_width + extra_width,
    01658           this->acs->GetHorizontalStepSize(sizing));
    01659
    01660       uint min_avs_height = (!this->editable) ? 0 : this->avs->smallest_y + this->avs->padding_top + this->avs->padding_bottom + INTER_LIST_SPACING;
    01661       uint min_acs_height = this->acs->smallest_y + this->acs->padding_top + this->acs->padding_bottom;
    01662       uint extra_height = given_height - min_acs_height - min_avs_height;
    01663
    01664       /* Never use fill_y on these; instead use the INTER_LIST_SPACING as filler */
    01665       uint avs_height = ComputeMaxSize(this->avs->smallest_y, this->avs->smallest_y + extra_height / 2, this->avs->resize_y);
    01666       if (this->editable) extra_height -= avs_height - this->avs->smallest_y;
    01667       uint acs_height = ComputeMaxSize(this->acs->smallest_y, this->acs->smallest_y + extra_height, this->acs->resize_y);
    01668
    01669       /* Assign size and position to the childs. */
    01670       if (rtl) {
    01671         x += this->inf->padding_left;
    01672         this->inf->AssignSizePosition(sizing, x, y + this->inf->padding_top, inf_width, inf_height, rtl);
    01673         x += inf_width + this->inf->padding_right + INTER_COLUMN_SPACING;
    01674
    01675         this->acs->AssignSizePosition(sizing, x + this->acs->padding_left, y + this->acs->padding_top, acs_width, acs_height, rtl);
    01676         if (this->editable) {
    01677           this->avs->AssignSizePosition(sizing, x + this->avs->padding_left, y + given_height - avs_height - this->avs->padding_bottom, avs_width, avs_height, rtl);
    01678         } else {
    01679           this->avs->AssignSizePosition(sizing, 0, 0, this->avs->smallest_x, this->avs->smallest_y, rtl);
    01680         }
    01681       } else {
    01682         this->acs->AssignSizePosition(sizing, x + this->acs->padding_left, y + this->acs->padding_top, acs_width, acs_height, rtl);
    01683         if (this->editable) {
    01684           this->avs->AssignSizePosition(sizing, x + this->avs->padding_left, y + given_height - avs_height - this->avs->padding_bottom, avs_width, avs_height, rtl);
    01685         } else {
    01686           this->avs->AssignSizePosition(sizing, 0, 0, this->avs->smallest_x, this->avs->smallest_y, rtl);
    01687         }
    01688         uint dx = this->acs->current_x + this->acs->padding_left + this->acs->padding_right;
    01689         if (this->editable) {
    01690           dx = max(dx, this->avs->current_x + this->avs->padding_left + this->avs->padding_right);
    01691         }
    01692         x += dx + INTER_COLUMN_SPACING + this->inf->padding_left;
    01693         this->inf->AssignSizePosition(sizing, x, y + this->inf->padding_top, inf_width, inf_height, rtl);
    01694       }
    01695     }
    01696   }
    01697
    01698   virtual NWidgetCore *GetWidgetFromPos(int x, int y)
    01699   {
    01700     if (!IsInsideBS(x, this->pos_x, this->current_x) || !IsInsideBS(y, this->pos_y, this->current_y)) return NULL;
    01701
    01702     NWidgetCore *nw = (this->editable) ? this->avs->GetWidgetFromPos(x, y) : NULL;
    01703     if (nw == NULL) nw = this->acs->GetWidgetFromPos(x, y);
    01704     if (nw == NULL) nw = this->inf->GetWidgetFromPos(x, y);
    01705     return nw;
    01706   }
    01707
    01708   virtual void Draw(const Window *w)
    01709   {
    01710     if (this->editable) this->avs->Draw(w);
    01711     this->acs->Draw(w);
    01712     this->inf->Draw(w);
    01713   }
    01714 };
    01715
    01716 const uint NWidgetNewGRFDisplay::INTER_LIST_SPACING      = WD_RESIZEBOX_WIDTH + 1;
    01717 const uint NWidgetNewGRFDisplay::INTER_COLUMN_SPACING    = WD_RESIZEBOX_WIDTH;
    01718 const uint NWidgetNewGRFDisplay::MAX_EXTRA_INFO_WIDTH    = 150;
    01719 const uint NWidgetNewGRFDisplay::MIN_EXTRA_FOR_3_COLUMNS = 50;
    01720
    01721 static const NWidgetPart _nested_newgrf_actives_widgets[] = {
    01722   /* Left side, presets. */
    01723   NWidget(NWID_HORIZONTAL),
    01724     NWidget(WWT_TEXT, COLOUR_MAUVE), SetDataTip(STR_NEWGRF_SETTINGS_SELECT_PRESET, STR_NULL),
    01725         SetPadding(0, WD_FRAMETEXT_RIGHT, 0, 0),
    01726     NWidget(WWT_DROPDOWN, COLOUR_YELLOW, WID_NS_PRESET_LIST), SetFill(1, 0), SetResize(1, 0),
    01727         SetDataTip(STR_JUST_STRING, STR_NEWGRF_SETTINGS_PRESET_LIST_TOOLTIP),
    01728   EndContainer(),
    01729   NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
    01730     NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_PRESET_SAVE), SetFill(1, 0), SetResize(1, 0),
    01731         SetDataTip(STR_NEWGRF_SETTINGS_PRESET_SAVE, STR_NEWGRF_SETTINGS_PRESET_SAVE_TOOLTIP),
    01732     NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_PRESET_DELETE), SetFill(1, 0), SetResize(1, 0),
    01733         SetDataTip(STR_NEWGRF_SETTINGS_PRESET_DELETE, STR_NEWGRF_SETTINGS_PRESET_DELETE_TOOLTIP),
    01734   EndContainer(),
    01735
    01736   NWidget(NWID_SPACER), SetMinimalSize(0, WD_RESIZEBOX_WIDTH), SetResize(1, 0), SetFill(1, 0),
    01737   NWidget(WWT_PANEL, COLOUR_MAUVE),
    01738     NWidget(WWT_LABEL, COLOUR_MAUVE), SetDataTip(STR_NEWGRF_SETTINGS_ACTIVE_LIST, STR_NULL),
    01739         SetFill(1, 0), SetResize(1, 0), SetPadding(3, WD_FRAMETEXT_RIGHT, 0, WD_FRAMETEXT_LEFT),
    01740     /* Left side, active grfs. */
    01741     NWidget(NWID_HORIZONTAL), SetPadding(0, 2, 0, 2),
    01742       NWidget(WWT_PANEL, COLOUR_MAUVE),
    01743         NWidget(WWT_INSET, COLOUR_MAUVE, WID_NS_FILE_LIST), SetMinimalSize(100, 1), SetPadding(2, 2, 2, 2),
    01744             SetFill(1, 1), SetResize(1, 1), SetScrollbar(WID_NS_SCROLLBAR), SetDataTip(STR_NULL, STR_NEWGRF_SETTINGS_FILE_TOOLTIP),
    01745         EndContainer(),
    01746       EndContainer(),
    01747       NWidget(NWID_VSCROLLBAR, COLOUR_MAUVE, WID_NS_SCROLLBAR),
    01748     EndContainer(),
    01749     /* Buttons. */
    01750     NWidget(NWID_SELECTION, INVALID_COLOUR, WID_NS_SHOW_REMOVE),
    01751       NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPadding(2, 2, 2, 2), SetPIP(0, WD_RESIZEBOX_WIDTH, 0),
    01752         NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_REMOVE), SetFill(1, 0), SetResize(1, 0),
    01753             SetDataTip(STR_NEWGRF_SETTINGS_REMOVE, STR_NEWGRF_SETTINGS_REMOVE_TOOLTIP),
    01754         NWidget(NWID_VERTICAL),
    01755           NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_MOVE_UP), SetFill(1, 0), SetResize(1, 0),
    01756               SetDataTip(STR_NEWGRF_SETTINGS_MOVEUP, STR_NEWGRF_SETTINGS_MOVEUP_TOOLTIP),
    01757           NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_MOVE_DOWN), SetFill(1, 0), SetResize(1, 0),
    01758               SetDataTip(STR_NEWGRF_SETTINGS_MOVEDOWN, STR_NEWGRF_SETTINGS_MOVEDOWN_TOOLTIP),
    01759         EndContainer(),
    01760       EndContainer(),
    01761
    01762       NWidget(NWID_VERTICAL, NC_EQUALSIZE), SetPadding(2, 2, 2, 2),
    01763         NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_RESCAN_FILES2), SetFill(1, 0), SetResize(1, 0),
    01764             SetDataTip(STR_NEWGRF_SETTINGS_RESCAN_FILES, STR_NEWGRF_SETTINGS_RESCAN_FILES_TOOLTIP),
    01765         NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_CONTENT_DOWNLOAD2), SetFill(1, 0), SetResize(1, 0),
    01766             SetDataTip(STR_INTRO_ONLINE_CONTENT, STR_INTRO_TOOLTIP_ONLINE_CONTENT),
    01767       EndContainer(),
    01768     EndContainer(),
    01769   EndContainer(),
    01770 };
    01771
    01772 static const NWidgetPart _nested_newgrf_availables_widgets[] = {
    01773   NWidget(WWT_PANEL, COLOUR_MAUVE),
    01774     NWidget(WWT_LABEL, COLOUR_MAUVE), SetDataTip(STR_NEWGRF_SETTINGS_INACTIVE_LIST, STR_NULL),
    01775         SetFill(1, 0), SetResize(1, 0), SetPadding(3, WD_FRAMETEXT_RIGHT, 0, WD_FRAMETEXT_LEFT),
    01776     /* Left side, available grfs, filter edit box. */
    01777     NWidget(NWID_HORIZONTAL), SetPadding(WD_TEXTPANEL_TOP, 0, WD_TEXTPANEL_BOTTOM, 0),
    01778         SetPIP(WD_FRAMETEXT_LEFT, WD_FRAMETEXT_RIGHT, WD_FRAMETEXT_RIGHT),
    01779       NWidget(WWT_TEXT, COLOUR_MAUVE), SetFill(0, 1), SetDataTip(STR_NEWGRF_FILTER_TITLE, STR_NULL),
    01780       NWidget(WWT_EDITBOX, COLOUR_MAUVE, WID_NS_FILTER), SetFill(1, 0), SetMinimalSize(50, 12), SetResize(1, 0),
    01781           SetDataTip(STR_LIST_FILTER_OSKTITLE, STR_LIST_FILTER_TOOLTIP),
    01782     EndContainer(),
    01783     /* Left side, available grfs. */
    01784     NWidget(NWID_HORIZONTAL), SetPadding(0, 2, 0, 2),
    01785       NWidget(WWT_PANEL, COLOUR_MAUVE),
    01786         NWidget(WWT_INSET, COLOUR_MAUVE, WID_NS_AVAIL_LIST), SetMinimalSize(100, 1), SetPadding(2, 2, 2, 2),
    01787             SetFill(1, 1), SetResize(1, 1), SetScrollbar(WID_NS_SCROLL2BAR),
    01788         EndContainer(),
    01789       EndContainer(),
    01790       NWidget(NWID_VSCROLLBAR, COLOUR_MAUVE, WID_NS_SCROLL2BAR),
    01791     EndContainer(),
    01792     /* Left side, available grfs, buttons. */
    01793     NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPadding(2, 2, 2, 2), SetPIP(0, WD_RESIZEBOX_WIDTH, 0),
    01794       NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_ADD), SetFill(1, 0), SetResize(1, 0),
    01795           SetDataTip(STR_NEWGRF_SETTINGS_ADD, STR_NEWGRF_SETTINGS_ADD_FILE_TOOLTIP),
    01796       NWidget(NWID_VERTICAL),
    01797         NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_RESCAN_FILES), SetFill(1, 0), SetResize(1, 0),
    01798             SetDataTip(STR_NEWGRF_SETTINGS_RESCAN_FILES, STR_NEWGRF_SETTINGS_RESCAN_FILES_TOOLTIP),
    01799         NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_CONTENT_DOWNLOAD), SetFill(1, 0), SetResize(1, 0),
    01800             SetDataTip(STR_INTRO_ONLINE_CONTENT, STR_INTRO_TOOLTIP_ONLINE_CONTENT),
    01801       EndContainer(),
    01802     EndContainer(),
    01803   EndContainer(),
    01804 };
    01805
    01806 static const NWidgetPart _nested_newgrf_infopanel_widgets[] = {
    01807   /* Right side, info panel. */
    01808   NWidget(NWID_VERTICAL), SetPadding(0, 0, 2, 0),
    01809     NWidget(WWT_PANEL, COLOUR_MAUVE), SetPadding(0, 0, 2, 0),
    01810       NWidget(WWT_EMPTY, COLOUR_MAUVE, WID_NS_NEWGRF_INFO_TITLE), SetFill(1, 0), SetResize(1, 0),
    01811       NWidget(WWT_EMPTY, COLOUR_MAUVE, WID_NS_NEWGRF_INFO), SetFill(1, 1), SetResize(1, 1), SetMinimalSize(150, 100),
    01812     EndContainer(),
    01813     NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_OPEN_URL), SetFill(1, 0), SetResize(1, 0),
    01814         SetDataTip(STR_CONTENT_OPEN_URL, STR_CONTENT_OPEN_URL_TOOLTIP),
    01815     NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_NEWGRF_TEXTFILE + TFT_README), SetFill(1, 0), SetResize(1, 0),
    01816         SetDataTip(STR_NEWGRF_SETTINGS_VIEW_README, STR_NULL),
    01817     NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
    01818       NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_NEWGRF_TEXTFILE + TFT_CHANGELOG), SetFill(1, 0), SetResize(1, 0),
    01819           SetDataTip(STR_NEWGRF_SETTINGS_VIEW_CHANGELOG, STR_NULL),
    01820       NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_NEWGRF_TEXTFILE + TFT_LICENSE), SetFill(1, 0), SetResize(1, 0),
    01821           SetDataTip(STR_NEWGRF_SETTINGS_VIEW_LICENSE, STR_NULL),
    01822     EndContainer(),
    01823   EndContainer(),
    01824   NWidget(NWID_SELECTION, INVALID_COLOUR, WID_NS_SHOW_APPLY),
    01825     /* Right side, buttons. */
    01826     NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(0, WD_RESIZEBOX_WIDTH, 0),
    01827       NWidget(NWID_VERTICAL),
    01828         NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_SET_PARAMETERS), SetFill(1, 0), SetResize(1, 0),
    01829             SetDataTip(STR_NEWGRF_SETTINGS_SET_PARAMETERS, STR_NULL),
    01830         NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_TOGGLE_PALETTE), SetFill(1, 0), SetResize(1, 0),
    01831             SetDataTip(STR_NEWGRF_SETTINGS_TOGGLE_PALETTE, STR_NEWGRF_SETTINGS_TOGGLE_PALETTE_TOOLTIP),
    01832       EndContainer(),
    01833       NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_APPLY_CHANGES), SetFill(1, 0), SetResize(1, 0),
    01834           SetDataTip(STR_NEWGRF_SETTINGS_APPLY_CHANGES, STR_NULL),
    01835     EndContainer(),
    01836     NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_VIEW_PARAMETERS), SetFill(1, 0), SetResize(1, 0),
    01837       SetDataTip(STR_NEWGRF_SETTINGS_SHOW_PARAMETERS, STR_NULL),
    01838   EndContainer(),
    01839 };
    01840
    01842 NWidgetBase* NewGRFDisplay(int *biggest_index)
    01843 {
    01844   NWidgetBase *avs = MakeNWidgets(_nested_newgrf_availables_widgets, lengthof(_nested_newgrf_availables_widgets), biggest_index, NULL);
    01845
    01846   int biggest2;
    01847   NWidgetBase *acs = MakeNWidgets(_nested_newgrf_actives_widgets, lengthof(_nested_newgrf_actives_widgets), &biggest2, NULL);
    01848   *biggest_index = max(*biggest_index, biggest2);
    01849
    01850   NWidgetBase *inf = MakeNWidgets(_nested_newgrf_infopanel_widgets, lengthof(_nested_newgrf_infopanel_widgets), &biggest2, NULL);
    01851   *biggest_index = max(*biggest_index, biggest2);
    01852
    01853   return new NWidgetNewGRFDisplay(avs, acs, inf);
    01854 }
    01855
    01856 /* Widget definition of the manage newgrfs window */
    01857 static const NWidgetPart _nested_newgrf_widgets[] = {
    01858   NWidget(NWID_HORIZONTAL),
    01859     NWidget(WWT_CLOSEBOX, COLOUR_MAUVE),
    01860     NWidget(WWT_CAPTION, COLOUR_MAUVE), SetDataTip(STR_NEWGRF_SETTINGS_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    01861   EndContainer(),
    01862   NWidget(WWT_PANEL, COLOUR_MAUVE),
    01863     NWidgetFunction(NewGRFDisplay), SetPadding(WD_RESIZEBOX_WIDTH, WD_RESIZEBOX_WIDTH, 2, WD_RESIZEBOX_WIDTH),
    01864     /* Resize button. */
    01865     NWidget(NWID_HORIZONTAL),
    01866       NWidget(NWID_SPACER), SetFill(1, 0), SetResize(1, 0),
    01867       NWidget(WWT_RESIZEBOX, COLOUR_MAUVE),
    01868     EndContainer(),
    01869   EndContainer(),
    01870 };
    01871
    01872 /* Window definition of the manage newgrfs window */
    01873 static const WindowDesc _newgrf_desc(
    01874   WDP_CENTER, 300, 263,
    01875   WC_GAME_OPTIONS, WC_NONE,
    01876   WDF_UNCLICK_BUTTONS,
    01877   _nested_newgrf_widgets, lengthof(_nested_newgrf_widgets)
    01878 );
    01879
    01885 static void NewGRFConfirmationCallback(Window *w, bool confirmed)
    01886 {
    01887   if (confirmed) {
    01888     DeleteWindowByClass(WC_GRF_PARAMETERS);
    01889     NewGRFWindow *nw = dynamic_cast<NewGRFWindow*>(w);
    01890
    01891     GamelogStartAction(GLAT_GRF);
    01892     GamelogGRFUpdate(_grfconfig, nw->actives); // log GRF changes
    01893     CopyGRFConfigList(nw->orig_list, nw->actives, false);
    01894     ReloadNewGRFData();
    01895     GamelogStopAction();
    01896
    01897     /* Show new, updated list */
    01898     GRFConfig *c;
    01899     int i = 0;
    01900     for (c = nw->actives; c != NULL && c != nw->active_sel; c = c->next, i++) {}
    01901     CopyGRFConfigList(&nw->actives, *nw->orig_list, false);
    01902     for (c = nw->actives; c != NULL && i > 0; c = c->next, i--) {}
    01903     nw->active_sel = c;
    01904     nw->avails.ForceRebuild();
    01905
    01906     w->InvalidateData();
    01907
    01908     ReInitAllWindows();
    01909     DeleteWindowByClass(WC_BUILD_OBJECT);
    01910   }
    01911 }
    01912
    01913
    01914
    01923 void ShowNewGRFSettings(bool editable, bool show_params, bool exec_changes, GRFConfig **config)
    01924 {
    01925   DeleteWindowByClass(WC_GAME_OPTIONS);
    01926   new NewGRFWindow(&_newgrf_desc, editable, show_params, exec_changes, config);
    01927 }
    01928
    01930 static const NWidgetPart _nested_scan_progress_widgets[] = {
    01931   NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_NEWGRF_SCAN_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    01932   NWidget(WWT_PANEL, COLOUR_GREY),
    01933     NWidget(NWID_HORIZONTAL), SetPIP(20, 0, 20),
    01934       NWidget(NWID_VERTICAL), SetPIP(11, 8, 11),
    01935         NWidget(WWT_LABEL, INVALID_COLOUR), SetDataTip(STR_NEWGRF_SCAN_MESSAGE, STR_NULL), SetFill(1, 0),
    01936         NWidget(WWT_EMPTY, INVALID_COLOUR, WID_SP_PROGRESS_BAR), SetFill(1, 0),
    01937         NWidget(WWT_EMPTY, INVALID_COLOUR, WID_SP_PROGRESS_TEXT), SetFill(1, 0),
    01938       EndContainer(),
    01939     EndContainer(),
    01940   EndContainer(),
    01941 };
    01942
    01944 static const WindowDesc _scan_progress_desc(
    01945   WDP_CENTER, 0, 0,
    01946   WC_MODAL_PROGRESS, WC_NONE,
    01947   WDF_UNCLICK_BUTTONS,
    01948   _nested_scan_progress_widgets, lengthof(_nested_scan_progress_widgets)
    01949 );
    01950
    01952 struct ScanProgressWindow : public Window {
    01953   char *last_name;
    01954   int scanned;
    01955
    01957   ScanProgressWindow() : Window(), last_name(NULL), scanned(0)
    01958   {
    01959     this->InitNested(&_scan_progress_desc, 1);
    01960   }
    01961
    01963   ~ScanProgressWindow()
    01964   {
    01965     free(last_name);
    01966   }
    01967
    01968   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
    01969   {
    01970     switch (widget) {
    01971       case WID_SP_PROGRESS_BAR: {
    01972         SetDParam(0, 100);
    01973         *size = GetStringBoundingBox(STR_GENERATION_PROGRESS);
    01974         /* We need some spacing for the 'border' */
    01975         size->height += 8;
    01976         size->width += 8;
    01977         break;
    01978       }
    01979
    01980       case WID_SP_PROGRESS_TEXT:
    01981         SetDParam(0, 9999);
    01982         SetDParam(1, 9999);
    01983         /* We really don't know the width. We could determine it by scanning the NewGRFs,
    01984          * but this is the status window for scanning them... */
    01985         size->width = max(400U, GetStringBoundingBox(STR_NEWGRF_SCAN_STATUS).width);
    01986         size->height = FONT_HEIGHT_NORMAL * 2 + WD_PAR_VSEP_NORMAL;
    01987         break;
    01988     }
    01989   }
    01990
    01991   virtual void DrawWidget(const Rect &r, int widget) const
    01992   {
    01993     switch (widget) {
    01994       case WID_SP_PROGRESS_BAR: {
    01995         /* Draw the % complete with a bar and a text */
    01996         DrawFrameRect(r.left, r.top, r.right, r.bottom, COLOUR_GREY, FR_BORDERONLY);
    01997         uint percent = scanned * 100 / max(1U, _settings_client.gui.last_newgrf_count);
    01998         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);
    01999         SetDParam(0, percent);
    02000         DrawString(r.left, r.right, r.top + 5, STR_GENERATION_PROGRESS, TC_FROMSTRING, SA_HOR_CENTER);
    02001         break;
    02002       }
    02003
    02004       case WID_SP_PROGRESS_TEXT:
    02005         SetDParam(0, this->scanned);
    02006         SetDParam(1, _settings_client.gui.last_newgrf_count);
    02007         DrawString(r.left, r.right, r.top, STR_NEWGRF_SCAN_STATUS, TC_FROMSTRING, SA_HOR_CENTER);
    02008
    02009         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);
    02010         break;
    02011     }
    02012   }
    02013
    02019   void UpdateNewGRFScanStatus(uint num, const char *name)
    02020   {
    02021     free(this->last_name);
    02022     if (name == NULL) {
    02023       char buf[256];
    02024       GetString(buf, STR_NEWGRF_SCAN_ARCHIVES, lastof(buf));
    02025       this->last_name = strdup(buf);
    02026     } else {
    02027       this->last_name = strdup(name);
    02028     }
    02029     this->scanned = num;
    02030     if (num > _settings_client.gui.last_newgrf_count) _settings_client.gui.last_newgrf_count = num;
    02031
    02032     this->SetDirty();
    02033   }
    02034 };
    02035
    02041 void UpdateNewGRFScanStatus(uint num, const char *name)
    02042 {
    02043   ScanProgressWindow *w  = dynamic_cast<ScanProgressWindow *>(FindWindowByClass(WC_MODAL_PROGRESS));
    02044   if (w == NULL) w = new ScanProgressWindow();
    02045   w->UpdateNewGRFScanStatus(num, name);
    02046 }