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