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