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