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_OPEN_URL,
00719   SNGRFS_NEWGRF_TEXTFILE,
00720   SNGRFS_SET_PARAMETERS = SNGRFS_NEWGRF_TEXTFILE + TFT_END,
00721   SNGRFS_TOGGLE_PALETTE,
00722   SNGRFS_APPLY_CHANGES,
00723   SNGRFS_RESCAN_FILES,
00724   SNGRFS_RESCAN_FILES2,
00725   SNGRFS_CONTENT_DOWNLOAD,
00726   SNGRFS_CONTENT_DOWNLOAD2,
00727   SNGRFS_SHOW_REMOVE, 
00728   SNGRFS_SHOW_APPLY,  
00729 };
00730 
00734 struct NewGRFWindow : public QueryStringBaseWindow, NewGRFScanCallback {
00735   typedef GUIList<const GRFConfig *> GUIGRFConfigList;
00736 
00737   static const uint EDITBOX_MAX_SIZE   =  50;
00738 
00739   static Listing   last_sorting;   
00740   static Filtering last_filtering; 
00741   static GUIGRFConfigList::SortFunction   * const sorter_funcs[]; 
00742   static GUIGRFConfigList::FilterFunction * const filter_funcs[]; 
00743 
00744   GUIGRFConfigList avails;    
00745   const GRFConfig *avail_sel; 
00746   int avail_pos;              
00747 
00748   GRFConfig *actives;         
00749   GRFConfig *active_sel;      
00750 
00751   GRFConfig **orig_list;      
00752   bool editable;              
00753   bool show_params;           
00754   bool execute;               
00755   int preset;                 
00756 
00757   Scrollbar *vscroll;
00758   Scrollbar *vscroll2;
00759 
00760   NewGRFWindow(const WindowDesc *desc, bool editable, bool show_params, bool execute, GRFConfig **orig_list) : QueryStringBaseWindow(EDITBOX_MAX_SIZE)
00761   {
00762     this->avail_sel   = NULL;
00763     this->avail_pos   = -1;
00764     this->active_sel  = NULL;
00765     this->actives     = NULL;
00766     this->orig_list   = orig_list;
00767     this->editable    = editable;
00768     this->execute     = execute;
00769     this->show_params = show_params;
00770     this->preset      = -1;
00771 
00772     CopyGRFConfigList(&this->actives, *orig_list, false);
00773     GetGRFPresetList(&_grf_preset_list);
00774 
00775     this->CreateNestedTree(desc);
00776     this->vscroll = this->GetScrollbar(SNGRFS_SCROLLBAR);
00777     this->vscroll2 = this->GetScrollbar(SNGRFS_SCROLL2BAR);
00778 
00779     this->GetWidget<NWidgetStacked>(SNGRFS_SHOW_REMOVE)->SetDisplayedPlane(this->editable ? 0 : 1);
00780     this->GetWidget<NWidgetStacked>(SNGRFS_SHOW_APPLY)->SetDisplayedPlane(this->editable ? 0 : SZSP_HORIZONTAL);
00781     this->FinishInitNested(desc);
00782 
00783     InitializeTextBuffer(&this->text, this->edit_str_buf, this->edit_str_size);
00784     this->SetFocusedWidget(SNGRFS_FILTER);
00785 
00786     this->avails.SetListing(this->last_sorting);
00787     this->avails.SetFiltering(this->last_filtering);
00788     this->avails.SetSortFuncs(this->sorter_funcs);
00789     this->avails.SetFilterFuncs(this->filter_funcs);
00790     this->avails.ForceRebuild();
00791 
00792     this->OnInvalidateData(GOID_NEWGRF_LIST_EDITED);
00793   }
00794 
00795   ~NewGRFWindow()
00796   {
00797     DeleteWindowByClass(WC_GRF_PARAMETERS);
00798     DeleteWindowByClass(WC_NEWGRF_TEXTFILE);
00799 
00800     if (this->editable && !this->execute) {
00801       CopyGRFConfigList(this->orig_list, this->actives, true);
00802       ResetGRFConfig(false);
00803       ReloadNewGRFData();
00804     }
00805 
00806     /* Remove the temporary copy of grf-list used in window */
00807     ClearGRFConfigList(&this->actives);
00808     _grf_preset_list.Clear();
00809   }
00810 
00811   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00812   {
00813     switch (widget) {
00814       case SNGRFS_FILE_LIST:
00815       {
00816         Dimension d = maxdim(GetSpriteSize(SPR_SQUARE), GetSpriteSize(SPR_WARNING_SIGN));
00817         resize->height = max(d.height + 2U, FONT_HEIGHT_NORMAL + 2U);
00818         size->height = max(size->height, WD_FRAMERECT_TOP + 6 * resize->height + WD_FRAMERECT_BOTTOM);
00819         break;
00820       }
00821 
00822       case SNGRFS_AVAIL_LIST:
00823         resize->height = max(12, FONT_HEIGHT_NORMAL + 2);
00824         size->height = max(size->height, WD_FRAMERECT_TOP + 8 * resize->height + WD_FRAMERECT_BOTTOM);
00825         break;
00826 
00827       case SNGRFS_NEWGRF_INFO_TITLE: {
00828         Dimension dim = GetStringBoundingBox(STR_NEWGRF_SETTINGS_INFO_TITLE);
00829         size->height = max(size->height, dim.height + WD_FRAMETEXT_TOP + WD_FRAMETEXT_BOTTOM);
00830         size->width  = max(size->width,  dim.width  + WD_FRAMETEXT_LEFT + WD_FRAMETEXT_RIGHT);
00831         break;
00832       }
00833 
00834       case SNGRFS_NEWGRF_INFO:
00835         size->height = max(size->height, WD_FRAMERECT_TOP + 10 * FONT_HEIGHT_NORMAL + WD_FRAMERECT_BOTTOM + padding.height + 2);
00836         break;
00837 
00838       case SNGRFS_PRESET_LIST: {
00839         Dimension d = GetStringBoundingBox(STR_NUM_CUSTOM);
00840         for (uint i = 0; i < _grf_preset_list.Length(); i++) {
00841           if (_grf_preset_list[i] != NULL) {
00842             SetDParamStr(0, _grf_preset_list[i]);
00843             d = maxdim(d, GetStringBoundingBox(STR_JUST_RAW_STRING));
00844           }
00845         }
00846         d.width += padding.width;
00847         *size = maxdim(d, *size);
00848         break;
00849       }
00850 
00851       case SNGRFS_CONTENT_DOWNLOAD:
00852       case SNGRFS_CONTENT_DOWNLOAD2: {
00853         Dimension d = GetStringBoundingBox(STR_NEWGRF_SETTINGS_FIND_MISSING_CONTENT_BUTTON);
00854         *size = maxdim(d, GetStringBoundingBox(STR_INTRO_ONLINE_CONTENT));
00855         size->width  += padding.width;
00856         size->height += padding.height;
00857         break;
00858       }
00859     }
00860   }
00861 
00862   virtual void OnResize()
00863   {
00864     this->vscroll->SetCapacityFromWidget(this, SNGRFS_FILE_LIST);
00865     this->vscroll2->SetCapacityFromWidget(this, SNGRFS_AVAIL_LIST);
00866   }
00867 
00868   virtual void SetStringParameters(int widget) const
00869   {
00870     switch (widget) {
00871       case SNGRFS_PRESET_LIST:
00872         if (this->preset == -1) {
00873           SetDParam(0, STR_NUM_CUSTOM);
00874         } else {
00875           SetDParam(0, STR_JUST_RAW_STRING);
00876           SetDParamStr(1, _grf_preset_list[this->preset]);
00877         }
00878         break;
00879     }
00880   }
00881 
00882   virtual void OnPaint()
00883   {
00884     this->DrawWidgets();
00885     if (this->editable) this->DrawEditBox(SNGRFS_FILTER);
00886   }
00887 
00893   FORCEINLINE PaletteID GetPalette(const GRFConfig *c) const
00894   {
00895     PaletteID pal;
00896 
00897     /* Pick a colour */
00898     switch (c->status) {
00899       case GCS_NOT_FOUND:
00900       case GCS_DISABLED:
00901         pal = PALETTE_TO_RED;
00902         break;
00903       case GCS_ACTIVATED:
00904         pal = PALETTE_TO_GREEN;
00905         break;
00906       default:
00907         pal = PALETTE_TO_BLUE;
00908         break;
00909     }
00910 
00911     /* Do not show a "not-failure" colour when it actually failed to load */
00912     if (pal != PALETTE_TO_RED) {
00913       if (HasBit(c->flags, GCF_STATIC)) {
00914         pal = PALETTE_TO_GREY;
00915       } else if (HasBit(c->flags, GCF_COMPATIBLE)) {
00916         pal = PALETTE_TO_ORANGE;
00917       }
00918     }
00919 
00920     return pal;
00921   }
00922 
00923   virtual void DrawWidget(const Rect &r, int widget) const
00924   {
00925     switch (widget) {
00926       case SNGRFS_FILE_LIST: {
00927         GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, PC_BLACK);
00928 
00929         uint step_height = this->GetWidget<NWidgetBase>(SNGRFS_FILE_LIST)->resize_y;
00930         uint y = r.top + WD_FRAMERECT_TOP;
00931         Dimension square = GetSpriteSize(SPR_SQUARE);
00932         Dimension warning = GetSpriteSize(SPR_WARNING_SIGN);
00933         int square_offset_y = (step_height - square.height) / 2;
00934         int warning_offset_y = (step_height - warning.height) / 2;
00935         int offset_y = (step_height - FONT_HEIGHT_NORMAL) / 2;
00936 
00937         bool rtl = _current_text_dir == TD_RTL;
00938         uint text_left    = rtl ? r.left + WD_FRAMERECT_LEFT : r.left + square.width + 15;
00939         uint text_right   = rtl ? r.right - square.width - 15 : r.right - WD_FRAMERECT_RIGHT;
00940         uint square_left  = rtl ? r.right - square.width - 5 : r.left + 5;
00941         uint warning_left = rtl ? r.right - square.width - warning.width - 10 : r.left + square.width + 10;
00942 
00943         int i = 0;
00944         for (const GRFConfig *c = this->actives; c != NULL; c = c->next, i++) {
00945           if (this->vscroll->IsVisible(i)) {
00946             const char *text = c->GetName();
00947             bool h = (this->active_sel == c);
00948             PaletteID pal = this->GetPalette(c);
00949 
00950             if (h) GfxFillRect(r.left + 1, y, r.right - 1, y + step_height - 1, PC_DARK_BLUE);
00951             DrawSprite(SPR_SQUARE, pal, square_left, y + square_offset_y);
00952             if (c->error != NULL) DrawSprite(SPR_WARNING_SIGN, 0, warning_left, y + warning_offset_y);
00953             uint txtoffset = c->error == NULL ? 0 : warning.width;
00954             DrawString(text_left + (rtl ? 0 : txtoffset), text_right - (rtl ? txtoffset : 0), y + offset_y, text, h ? TC_WHITE : TC_ORANGE);
00955             y += step_height;
00956           }
00957         }
00958         break;
00959       }
00960 
00961       case SNGRFS_AVAIL_LIST: {
00962         GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, PC_BLACK);
00963 
00964         uint step_height = this->GetWidget<NWidgetBase>(SNGRFS_AVAIL_LIST)->resize_y;
00965         int offset_y = (step_height - FONT_HEIGHT_NORMAL) / 2;
00966         uint y = r.top + WD_FRAMERECT_TOP;
00967         uint min_index = this->vscroll2->GetPosition();
00968         uint max_index = min(min_index + this->vscroll2->GetCapacity(), this->avails.Length());
00969 
00970         for (uint i = min_index; i < max_index; i++) {
00971           const GRFConfig *c = this->avails[i];
00972           bool h = (c == this->avail_sel);
00973           const char *text = c->GetName();
00974 
00975           if (h) GfxFillRect(r.left + 1, y, r.right - 1, y + step_height - 1, PC_DARK_BLUE);
00976           DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y + offset_y, text, h ? TC_WHITE : TC_SILVER);
00977           y += step_height;
00978         }
00979         break;
00980       }
00981 
00982       case SNGRFS_NEWGRF_INFO_TITLE:
00983         /* Create the nice grayish rectangle at the details top. */
00984         GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, PC_DARK_BLUE);
00985         DrawString(r.left, r.right, (r.top + r.bottom - FONT_HEIGHT_NORMAL) / 2, STR_NEWGRF_SETTINGS_INFO_TITLE, TC_FROMSTRING, SA_HOR_CENTER);
00986         break;
00987 
00988       case SNGRFS_NEWGRF_INFO: {
00989         const GRFConfig *selected = this->active_sel;
00990         if (selected == NULL) selected = this->avail_sel;
00991         if (selected != NULL) {
00992           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);
00993         }
00994         break;
00995       }
00996     }
00997   }
00998 
00999   virtual void OnClick(Point pt, int widget, int click_count)
01000   {
01001     if (widget >= SNGRFS_NEWGRF_TEXTFILE && widget < SNGRFS_NEWGRF_TEXTFILE + TFT_END) {
01002       if (this->active_sel == NULL && this->avail_sel == NULL) return;
01003 
01004       ShowNewGRFTextfileWindow(this->active_sel != NULL ? this->active_sel : this->avail_sel, (TextfileType)(widget - SNGRFS_NEWGRF_TEXTFILE));
01005       return;
01006     }
01007 
01008     switch (widget) {
01009       case SNGRFS_PRESET_LIST: {
01010         DropDownList *list = new DropDownList();
01011 
01012         /* Add 'None' option for clearing list */
01013         list->push_back(new DropDownListStringItem(STR_NONE, -1, false));
01014 
01015         for (uint i = 0; i < _grf_preset_list.Length(); i++) {
01016           if (_grf_preset_list[i] != NULL) {
01017             list->push_back(new DropDownListPresetItem(i));
01018           }
01019         }
01020 
01021         this->DeleteChildWindows(WC_QUERY_STRING); // Remove the parameter query window
01022         ShowDropDownList(this, list, this->preset, SNGRFS_PRESET_LIST);
01023         break;
01024       }
01025 
01026       case SNGRFS_OPEN_URL: {
01027         const GRFConfig *c = (this->avail_sel == NULL) ? this->active_sel : this->avail_sel;
01028 
01029         extern void OpenBrowser(const char *url);
01030         OpenBrowser(c->GetURL());
01031         break;
01032       }
01033 
01034       case SNGRFS_PRESET_SAVE:
01035         ShowQueryString(STR_EMPTY, STR_NEWGRF_SETTINGS_PRESET_SAVE_QUERY, 32, this, CS_ALPHANUMERAL, QSF_NONE);
01036         break;
01037 
01038       case SNGRFS_PRESET_DELETE:
01039         if (this->preset == -1) return;
01040 
01041         DeleteGRFPresetFromConfig(_grf_preset_list[this->preset]);
01042         GetGRFPresetList(&_grf_preset_list);
01043         this->preset = -1;
01044         this->InvalidateData();
01045         this->DeleteChildWindows(WC_QUERY_STRING); // Remove the parameter query window
01046         break;
01047 
01048       case SNGRFS_MOVE_UP: { // Move GRF up
01049         if (this->active_sel == NULL || !this->editable) break;
01050 
01051         int pos = 0;
01052         for (GRFConfig **pc = &this->actives; *pc != NULL; pc = &(*pc)->next, pos++) {
01053           GRFConfig *c = *pc;
01054           if (c->next == this->active_sel) {
01055             c->next = this->active_sel->next;
01056             this->active_sel->next = c;
01057             *pc = this->active_sel;
01058             break;
01059           }
01060         }
01061         this->vscroll->ScrollTowards(pos);
01062         this->preset = -1;
01063         this->InvalidateData();
01064         break;
01065       }
01066 
01067       case SNGRFS_MOVE_DOWN: { // Move GRF down
01068         if (this->active_sel == NULL || !this->editable) break;
01069 
01070         int pos = 1; // Start at 1 as we swap the selected newgrf with the next one
01071         for (GRFConfig **pc = &this->actives; *pc != NULL; pc = &(*pc)->next, pos++) {
01072           GRFConfig *c = *pc;
01073           if (c == this->active_sel) {
01074             *pc = c->next;
01075             c->next = c->next->next;
01076             (*pc)->next = c;
01077             break;
01078           }
01079         }
01080         this->vscroll->ScrollTowards(pos);
01081         this->preset = -1;
01082         this->InvalidateData();
01083         break;
01084       }
01085 
01086       case SNGRFS_FILE_LIST: { // Select an active GRF.
01087         uint i = this->vscroll->GetScrolledRowFromWidget(pt.y, this, SNGRFS_FILE_LIST);
01088 
01089         GRFConfig *c;
01090         for (c = this->actives; c != NULL && i > 0; c = c->next, i--) {}
01091 
01092         if (this->active_sel != c) DeleteWindowByClass(WC_GRF_PARAMETERS);
01093         this->active_sel = c;
01094         this->avail_sel = NULL;
01095         this->avail_pos = -1;
01096 
01097         this->InvalidateData();
01098         if (click_count == 1) break;
01099         /* FALL THROUGH, with double click. */
01100       }
01101 
01102       case SNGRFS_REMOVE: { // Remove GRF
01103         if (this->active_sel == NULL || !this->editable) break;
01104         DeleteWindowByClass(WC_GRF_PARAMETERS);
01105 
01106         /* Choose the next GRF file to be the selected file. */
01107         GRFConfig *newsel = this->active_sel->next;
01108         for (GRFConfig **pc = &this->actives; *pc != NULL; pc = &(*pc)->next) {
01109           GRFConfig *c = *pc;
01110           /* If the new selection is empty (i.e. we're deleting the last item
01111            * in the list, pick the file just before the selected file */
01112           if (newsel == NULL && c->next == this->active_sel) newsel = c;
01113 
01114           if (c == this->active_sel) {
01115             *pc = c->next;
01116             delete c;
01117             break;
01118           }
01119         }
01120 
01121         this->active_sel = newsel;
01122         this->preset = -1;
01123         this->avail_pos = -1;
01124         this->avail_sel = NULL;
01125         this->avails.ForceRebuild();
01126         this->InvalidateData(GOID_NEWGRF_LIST_EDITED);
01127         break;
01128       }
01129 
01130       case SNGRFS_AVAIL_LIST: { // Select a non-active GRF.
01131         uint i = this->vscroll2->GetScrolledRowFromWidget(pt.y, this, SNGRFS_AVAIL_LIST);
01132         this->active_sel = NULL;
01133         DeleteWindowByClass(WC_GRF_PARAMETERS);
01134         if (i < this->avails.Length()) {
01135           this->avail_sel = this->avails[i];
01136           this->avail_pos = i;
01137         }
01138         this->InvalidateData();
01139         if (click_count == 1) break;
01140         /* FALL THROUGH, with double click. */
01141       }
01142 
01143       case SNGRFS_ADD: {
01144         if (this->avail_sel == NULL || !this->editable || HasBit(this->avail_sel->flags, GCF_INVALID)) break;
01145 
01146         GRFConfig **list;
01147         /* Find last entry in the list, checking for duplicate grfid on the way */
01148         for (list = &this->actives; *list != NULL; list = &(*list)->next) {
01149           if ((*list)->ident.grfid == this->avail_sel->ident.grfid) {
01150             ShowErrorMessage(STR_NEWGRF_DUPLICATE_GRFID, INVALID_STRING_ID, WL_INFO);
01151             return;
01152           }
01153         }
01154 
01155         GRFConfig *c = new GRFConfig(*this->avail_sel); // Copy GRF details from scanned list.
01156         c->SetParameterDefaults();
01157         *list = c; // Append GRF config to configuration list.
01158 
01159         /* Select next (or previous, if last one) item in the list. */
01160         int new_pos = this->avail_pos + 1;
01161         if (new_pos >= (int)this->avails.Length()) new_pos = this->avail_pos - 1;
01162         this->avail_pos = new_pos;
01163         if (new_pos >= 0) this->avail_sel = this->avails[new_pos];
01164 
01165         this->avails.ForceRebuild();
01166         this->InvalidateData(GOID_NEWGRF_LIST_EDITED);
01167         break;
01168       }
01169 
01170       case SNGRFS_APPLY_CHANGES: // Apply changes made to GRF list
01171         if (!this->editable) break;
01172         if (this->execute) {
01173           ShowQuery(
01174             STR_NEWGRF_POPUP_CAUTION_CAPTION,
01175             STR_NEWGRF_CONFIRMATION_TEXT,
01176             this,
01177             NewGRFConfirmationCallback
01178           );
01179         } else {
01180           CopyGRFConfigList(this->orig_list, this->actives, true);
01181           ResetGRFConfig(false);
01182           ReloadNewGRFData();
01183         }
01184         this->DeleteChildWindows(WC_QUERY_STRING); // Remove the parameter query window
01185         break;
01186 
01187       case SNGRFS_SET_PARAMETERS: { // Edit parameters
01188         if (this->active_sel == NULL || !this->editable || !this->show_params || this->active_sel->num_valid_params == 0) break;
01189 
01190         OpenGRFParameterWindow(this->active_sel);
01191         break;
01192       }
01193 
01194       case SNGRFS_TOGGLE_PALETTE:
01195         if (this->active_sel != NULL || !this->editable) {
01196           this->active_sel->palette ^= GRFP_USE_MASK;
01197           this->SetDirty();
01198         }
01199         break;
01200 
01201       case SNGRFS_CONTENT_DOWNLOAD:
01202       case SNGRFS_CONTENT_DOWNLOAD2:
01203         if (!_network_available) {
01204           ShowErrorMessage(STR_NETWORK_ERROR_NOTAVAILABLE, INVALID_STRING_ID, WL_ERROR);
01205         } else {
01206 #if defined(ENABLE_NETWORK)
01207           this->DeleteChildWindows(WC_QUERY_STRING); // Remove the parameter query window
01208 
01209           ShowMissingContentWindow(this->actives);
01210 #endif
01211         }
01212         break;
01213 
01214       case SNGRFS_RESCAN_FILES:
01215       case SNGRFS_RESCAN_FILES2:
01216         ScanNewGRFFiles(this);
01217         break;
01218     }
01219   }
01220 
01221   virtual void OnNewGRFsScanned()
01222   {
01223     this->avail_sel = NULL;
01224     this->avail_pos = -1;
01225     this->avails.ForceRebuild();
01226     this->DeleteChildWindows(WC_QUERY_STRING);  // Remove the parameter query window
01227     this->DeleteChildWindows(WC_NEWGRF_TEXTFILE); // Remove the view textfile window
01228   }
01229 
01230   virtual void OnDropdownSelect(int widget, int index)
01231   {
01232     if (!this->editable) return;
01233 
01234     ClearGRFConfigList(&this->actives);
01235     this->preset = index;
01236 
01237     if (index != -1) {
01238       this->actives = LoadGRFPresetFromConfig(_grf_preset_list[index]);
01239     }
01240     this->avails.ForceRebuild();
01241 
01242     DeleteWindowByClass(WC_GRF_PARAMETERS);
01243     this->active_sel = NULL;
01244     this->InvalidateData(GOID_NEWGRF_PRESET_LOADED);
01245   }
01246 
01247   virtual void OnQueryTextFinished(char *str)
01248   {
01249     if (str == NULL) return;
01250 
01251     SaveGRFPresetToConfig(str, this->actives);
01252     GetGRFPresetList(&_grf_preset_list);
01253 
01254     /* Switch to this preset */
01255     for (uint i = 0; i < _grf_preset_list.Length(); i++) {
01256       if (_grf_preset_list[i] != NULL && strcmp(_grf_preset_list[i], str) == 0) {
01257         this->preset = i;
01258         break;
01259       }
01260     }
01261 
01262     this->InvalidateData();
01263   }
01264 
01270   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
01271   {
01272     if (!gui_scope) return;
01273     switch (data) {
01274       default:
01275         /* Nothing important to do */
01276         break;
01277 
01278       case GOID_NEWGRF_RESCANNED:
01279         /* Search the list for items that are now found and mark them as such. */
01280         for (GRFConfig **l = &this->actives; *l != NULL; l = &(*l)->next) {
01281           GRFConfig *c = *l;
01282           bool compatible = HasBit(c->flags, GCF_COMPATIBLE);
01283           if (c->status != GCS_NOT_FOUND && !compatible) continue;
01284 
01285           const GRFConfig *f = FindGRFConfig(c->ident.grfid, FGCM_EXACT, compatible ? c->original_md5sum : c->ident.md5sum);
01286           if (f == NULL || HasBit(f->flags, GCF_INVALID)) continue;
01287 
01288           *l = new GRFConfig(*f);
01289           (*l)->next = c->next;
01290 
01291           if (active_sel == c) active_sel = *l;
01292 
01293           delete c;
01294         }
01295 
01296         this->avails.ForceRebuild();
01297         /* FALL THROUGH */
01298       case GOID_NEWGRF_LIST_EDITED:
01299         this->preset = -1;
01300         /* FALL THROUGH */
01301       case GOID_NEWGRF_PRESET_LOADED: {
01302         /* Update scrollbars */
01303         int i = 0;
01304         for (const GRFConfig *c = this->actives; c != NULL; c = c->next, i++) {}
01305 
01306         this->vscroll->SetCapacityFromWidget(this, SNGRFS_FILE_LIST);
01307         this->vscroll->SetCount(i);
01308 
01309         this->vscroll2->SetCapacityFromWidget(this, SNGRFS_AVAIL_LIST);
01310         if (this->avail_pos >= 0) this->vscroll2->ScrollTowards(this->avail_pos);
01311         break;
01312       }
01313     }
01314 
01315     this->BuildAvailables();
01316 
01317     this->SetWidgetsDisabledState(!this->editable,
01318       SNGRFS_PRESET_LIST,
01319       SNGRFS_APPLY_CHANGES,
01320       SNGRFS_TOGGLE_PALETTE,
01321       WIDGET_LIST_END
01322     );
01323     this->SetWidgetDisabledState(SNGRFS_ADD, !this->editable || this->avail_sel == NULL || HasBit(this->avail_sel->flags, GCF_INVALID));
01324 
01325     bool disable_all = this->active_sel == NULL || !this->editable;
01326     this->SetWidgetsDisabledState(disable_all,
01327       SNGRFS_REMOVE,
01328       SNGRFS_MOVE_UP,
01329       SNGRFS_MOVE_DOWN,
01330       WIDGET_LIST_END
01331     );
01332 
01333     const GRFConfig *c = (this->avail_sel == NULL) ? this->active_sel : this->avail_sel;
01334     for (TextfileType tft = TFT_BEGIN; tft < TFT_END; tft++) {
01335       this->SetWidgetDisabledState(SNGRFS_NEWGRF_TEXTFILE + tft, c == NULL || c->GetTextfile(tft) == NULL);
01336     }
01337     this->SetWidgetDisabledState(SNGRFS_OPEN_URL, c == NULL || StrEmpty(c->GetURL()));
01338 
01339     this->SetWidgetDisabledState(SNGRFS_SET_PARAMETERS, !this->show_params || disable_all || this->active_sel->num_valid_params == 0);
01340     this->SetWidgetDisabledState(SNGRFS_TOGGLE_PALETTE, disable_all);
01341 
01342     if (!disable_all) {
01343       /* All widgets are now enabled, so disable widgets we can't use */
01344       if (this->active_sel == this->actives)    this->DisableWidget(SNGRFS_MOVE_UP);
01345       if (this->active_sel->next == NULL)       this->DisableWidget(SNGRFS_MOVE_DOWN);
01346       if (this->active_sel->IsOpenTTDBaseGRF()) this->DisableWidget(SNGRFS_REMOVE);
01347     }
01348 
01349     this->SetWidgetDisabledState(SNGRFS_PRESET_DELETE, this->preset == -1);
01350 
01351     bool has_missing = false;
01352     bool has_compatible = false;
01353     for (const GRFConfig *c = this->actives; !has_missing && c != NULL; c = c->next) {
01354       has_missing    |= c->status == GCS_NOT_FOUND;
01355       has_compatible |= HasBit(c->flags, GCF_COMPATIBLE);
01356     }
01357     uint16 widget_data;
01358     StringID tool_tip;
01359     if (has_missing || has_compatible) {
01360       widget_data = STR_NEWGRF_SETTINGS_FIND_MISSING_CONTENT_BUTTON;
01361       tool_tip    = STR_NEWGRF_SETTINGS_FIND_MISSING_CONTENT_TOOLTIP;
01362     } else {
01363       widget_data = STR_INTRO_ONLINE_CONTENT;
01364       tool_tip    = STR_INTRO_TOOLTIP_ONLINE_CONTENT;
01365     }
01366     this->GetWidget<NWidgetCore>(SNGRFS_CONTENT_DOWNLOAD)->widget_data  = widget_data;
01367     this->GetWidget<NWidgetCore>(SNGRFS_CONTENT_DOWNLOAD)->tool_tip     = tool_tip;
01368     this->GetWidget<NWidgetCore>(SNGRFS_CONTENT_DOWNLOAD2)->widget_data = widget_data;
01369     this->GetWidget<NWidgetCore>(SNGRFS_CONTENT_DOWNLOAD2)->tool_tip    = tool_tip;
01370 
01371     this->SetWidgetDisabledState(SNGRFS_PRESET_SAVE, has_missing);
01372   }
01373 
01374   virtual void OnMouseLoop()
01375   {
01376     if (this->editable) this->HandleEditBox(SNGRFS_FILTER);
01377   }
01378 
01379   virtual EventState OnKeyPress(uint16 key, uint16 keycode)
01380   {
01381     if (!this->editable) return ES_NOT_HANDLED;
01382 
01383     switch (keycode) {
01384       case WKC_UP:
01385         /* scroll up by one */
01386         if (this->avail_pos > 0) this->avail_pos--;
01387         break;
01388 
01389       case WKC_DOWN:
01390         /* scroll down by one */
01391         if (this->avail_pos < (int)this->avails.Length() - 1) this->avail_pos++;
01392         break;
01393 
01394       case WKC_PAGEUP:
01395         /* scroll up a page */
01396         this->avail_pos = (this->avail_pos < this->vscroll2->GetCapacity()) ? 0 : this->avail_pos - this->vscroll2->GetCapacity();
01397         break;
01398 
01399       case WKC_PAGEDOWN:
01400         /* scroll down a page */
01401         this->avail_pos = min(this->avail_pos + this->vscroll2->GetCapacity(), (int)this->avails.Length() - 1);
01402         break;
01403 
01404       case WKC_HOME:
01405         /* jump to beginning */
01406         this->avail_pos = 0;
01407         break;
01408 
01409       case WKC_END:
01410         /* jump to end */
01411         this->avail_pos = this->avails.Length() - 1;
01412         break;
01413 
01414       default: {
01415         /* Handle editbox input */
01416         EventState state = ES_NOT_HANDLED;
01417         if (this->HandleEditBoxKey(SNGRFS_FILTER, key, keycode, state) == HEBR_EDITING) {
01418           this->OnOSKInput(SNGRFS_FILTER);
01419         }
01420         return state;
01421       }
01422     }
01423 
01424     if (this->avails.Length() == 0) this->avail_pos = -1;
01425     if (this->avail_pos >= 0) {
01426       this->avail_sel = this->avails[this->avail_pos];
01427       this->vscroll2->ScrollTowards(this->avail_pos);
01428       this->InvalidateData(0);
01429     }
01430 
01431     return ES_HANDLED;
01432   }
01433 
01434   virtual void OnOSKInput(int wid)
01435   {
01436     if (!this->editable) return;
01437 
01438     this->avails.SetFilterState(!StrEmpty(this->edit_str_buf));
01439     this->avails.ForceRebuild();
01440     this->InvalidateData(0);
01441   }
01442 
01443 private:
01445   static int CDECL NameSorter(const GRFConfig * const *a, const GRFConfig * const *b)
01446   {
01447     int i = strnatcmp((*a)->GetName(), (*b)->GetName()); // Sort by name (natural sorting).
01448     if (i != 0) return i;
01449 
01450     i = (*a)->version - (*b)->version;
01451     if (i != 0) return i;
01452 
01453     return memcmp((*a)->ident.md5sum, (*b)->ident.md5sum, lengthof((*b)->ident.md5sum));
01454   }
01455 
01457   static bool CDECL TagNameFilter(const GRFConfig * const *a, const char *filter_string)
01458   {
01459     if (strcasestr((*a)->GetName(), filter_string) != NULL) return true;
01460     if ((*a)->filename != NULL && strcasestr((*a)->filename, filter_string) != NULL) return true;
01461     if ((*a)->GetDescription() != NULL && strcasestr((*a)->GetDescription(), filter_string) != NULL) return true;
01462     return false;
01463   }
01464 
01465   void BuildAvailables()
01466   {
01467     if (!this->avails.NeedRebuild()) return;
01468 
01469     this->avails.Clear();
01470 
01471     for (const GRFConfig *c = _all_grfs; c != NULL; c = c->next) {
01472       bool found = false;
01473       for (const GRFConfig *grf = this->actives; grf != NULL && !found; grf = grf->next) found = grf->ident.HasGrfIdentifier(c->ident.grfid, c->ident.md5sum);
01474       if (found) continue;
01475 
01476       if (_settings_client.gui.newgrf_show_old_versions) {
01477         *this->avails.Append() = c;
01478       } else {
01479         const GRFConfig *best = FindGRFConfig(c->ident.grfid, HasBit(c->flags, GCF_INVALID) ? FGCM_NEWEST : FGCM_NEWEST_VALID);
01480         /*
01481          * If the best version is 0, then all NewGRF with this GRF ID
01482          * have version 0, so for backward compatability reasons we
01483          * want to show them all.
01484          * If we are the best version, then we definitely want to
01485          * show that NewGRF!.
01486          */
01487         if (best->version == 0 || best->ident.HasGrfIdentifier(c->ident.grfid, c->ident.md5sum)) {
01488           *this->avails.Append() = c;
01489         }
01490       }
01491     }
01492 
01493     this->avails.Filter(this->edit_str_buf);
01494     this->avails.Compact();
01495     this->avails.RebuildDone();
01496     this->avails.Sort();
01497 
01498     if (this->avail_sel != NULL) {
01499       this->avail_pos = this->avails.FindIndex(this->avail_sel);
01500       if (this->avail_pos < 0) this->avail_sel = NULL;
01501     }
01502 
01503     this->vscroll2->SetCount(this->avails.Length()); // Update the scrollbar
01504   }
01505 };
01506 
01507 #if defined(ENABLE_NETWORK)
01508 
01512 void ShowMissingContentWindow(const GRFConfig *list)
01513 {
01514   /* Only show the things in the current list, or everything when nothing's selected */
01515   ContentVector cv;
01516   for (const GRFConfig *c = list; c != NULL; c = c->next) {
01517     if (c->status != GCS_NOT_FOUND && !HasBit(c->flags, GCF_COMPATIBLE)) continue;
01518 
01519     ContentInfo *ci = new ContentInfo();
01520     ci->type = CONTENT_TYPE_NEWGRF;
01521     ci->state = ContentInfo::DOES_NOT_EXIST;
01522     ttd_strlcpy(ci->name, c->GetName(), lengthof(ci->name));
01523     ci->unique_id = BSWAP32(c->ident.grfid);
01524     memcpy(ci->md5sum, HasBit(c->flags, GCF_COMPATIBLE) ? c->original_md5sum : c->ident.md5sum, sizeof(ci->md5sum));
01525     *cv.Append() = ci;
01526   }
01527   ShowNetworkContentListWindow(cv.Length() == 0 ? NULL : &cv, CONTENT_TYPE_NEWGRF);
01528 }
01529 #endif
01530 
01531 Listing NewGRFWindow::last_sorting     = {false, 0};
01532 Filtering NewGRFWindow::last_filtering = {false, 0};
01533 
01534 NewGRFWindow::GUIGRFConfigList::SortFunction * const NewGRFWindow::sorter_funcs[] = {
01535   &NameSorter,
01536 };
01537 
01538 NewGRFWindow::GUIGRFConfigList::FilterFunction * const NewGRFWindow::filter_funcs[] = {
01539   &TagNameFilter,
01540 };
01541 
01548 class NWidgetNewGRFDisplay : public NWidgetContainer {
01549 public:
01550   static const uint INTER_LIST_SPACING;      
01551   static const uint INTER_COLUMN_SPACING;    
01552   static const uint MAX_EXTRA_INFO_WIDTH;    
01553   static const uint MIN_EXTRA_FOR_3_COLUMNS; 
01554 
01555   NWidgetBase *avs; 
01556   NWidgetBase *acs; 
01557   NWidgetBase *inf; 
01558   bool editable;    
01559 
01560   NWidgetNewGRFDisplay(NWidgetBase *avs, NWidgetBase *acs, NWidgetBase *inf) : NWidgetContainer(NWID_HORIZONTAL)
01561   {
01562     this->avs = avs;
01563     this->acs = acs;
01564     this->inf = inf;
01565 
01566     this->Add(this->avs);
01567     this->Add(this->acs);
01568     this->Add(this->inf);
01569 
01570     this->editable = true; // Temporary setting, 'real' value is set in SetupSmallestSize().
01571   }
01572 
01573   virtual void SetupSmallestSize(Window *w, bool init_array)
01574   {
01575     /* Copy state flag from the window. */
01576     assert(dynamic_cast<NewGRFWindow *>(w) != NULL);
01577     NewGRFWindow *ngw = (NewGRFWindow *)w;
01578     this->editable = ngw->editable;
01579 
01580     this->avs->SetupSmallestSize(w, init_array);
01581     this->acs->SetupSmallestSize(w, init_array);
01582     this->inf->SetupSmallestSize(w, init_array);
01583 
01584     uint min_avs_width = this->avs->smallest_x + this->avs->padding_left + this->avs->padding_right;
01585     uint min_acs_width = this->acs->smallest_x + this->acs->padding_left + this->acs->padding_right;
01586     uint min_inf_width = this->inf->smallest_x + this->inf->padding_left + this->inf->padding_right;
01587 
01588     uint min_avs_height = this->avs->smallest_y + this->avs->padding_top + this->avs->padding_bottom;
01589     uint min_acs_height = this->acs->smallest_y + this->acs->padding_top + this->acs->padding_bottom;
01590     uint min_inf_height = this->inf->smallest_y + this->inf->padding_top + this->inf->padding_bottom;
01591 
01592     /* Smallest window is in two column mode. */
01593     this->smallest_x = max(min_avs_width, min_acs_width) + INTER_COLUMN_SPACING + min_inf_width;
01594     this->smallest_y = max(min_inf_height, min_acs_height + INTER_LIST_SPACING + min_avs_height);
01595 
01596     /* Filling. */
01597     this->fill_x = LeastCommonMultiple(this->avs->fill_x, this->acs->fill_x);
01598     if (this->inf->fill_x > 0 && (this->fill_x == 0 || this->fill_x > this->inf->fill_x)) this->fill_x = this->inf->fill_x;
01599 
01600     this->fill_y = this->avs->fill_y;
01601     if (this->acs->fill_y > 0 && (this->fill_y == 0 || this->fill_y > this->acs->fill_y)) this->fill_y = this->acs->fill_y;
01602     this->fill_y = LeastCommonMultiple(this->fill_y, this->inf->fill_y);
01603 
01604     /* Resizing. */
01605     this->resize_x = LeastCommonMultiple(this->avs->resize_x, this->acs->resize_x);
01606     if (this->inf->resize_x > 0 && (this->resize_x == 0 || this->resize_x > this->inf->resize_x)) this->resize_x = this->inf->resize_x;
01607 
01608     this->resize_y = this->avs->resize_y;
01609     if (this->acs->resize_y > 0 && (this->resize_y == 0 || this->resize_y > this->acs->resize_y)) this->resize_y = this->acs->resize_y;
01610     this->resize_y = LeastCommonMultiple(this->resize_y, this->inf->resize_y);
01611 
01612     /* Make sure the height suits the 3 column (resp. not-editable) format; the 2 column format can easily fill space between the lists */
01613     this->smallest_y = ComputeMaxSize(min_acs_height, this->smallest_y + this->resize_y - 1, this->resize_y);
01614   }
01615 
01616   virtual void AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)
01617   {
01618     this->StoreSizePosition(sizing, x, y, given_width, given_height);
01619 
01620     uint min_avs_width = this->avs->smallest_x + this->avs->padding_left + this->avs->padding_right;
01621     uint min_acs_width = this->acs->smallest_x + this->acs->padding_left + this->acs->padding_right;
01622     uint min_inf_width = this->inf->smallest_x + this->inf->padding_left + this->inf->padding_right;
01623 
01624     uint min_list_width = max(min_avs_width, min_acs_width); // Smallest width of the lists such that they have equal width (incl padding).
01625     uint avs_extra_width = min_list_width - min_avs_width;   // Additional width needed for avs to reach min_list_width.
01626     uint acs_extra_width = min_list_width - min_acs_width;   // Additional width needed for acs to reach min_list_width.
01627 
01628     /* Use 2 or 3 colmuns? */
01629     uint min_three_columns = min_avs_width + min_acs_width + min_inf_width + 2 * INTER_COLUMN_SPACING;
01630     uint min_two_columns   = min_list_width + min_inf_width + INTER_COLUMN_SPACING;
01631     bool use_three_columns = this->editable && (min_three_columns + MIN_EXTRA_FOR_3_COLUMNS <= given_width);
01632 
01633     /* Info panel is a seperate column in both modes. Compute its width first. */
01634     uint extra_width, inf_width;
01635     if (use_three_columns) {
01636       extra_width = given_width - min_three_columns;
01637       inf_width = min(MAX_EXTRA_INFO_WIDTH, extra_width / 2);
01638     } else {
01639       extra_width = given_width - min_two_columns;
01640       inf_width = min(MAX_EXTRA_INFO_WIDTH, extra_width / 2);
01641     }
01642     inf_width = ComputeMaxSize(this->inf->smallest_x, this->inf->smallest_x + inf_width, this->inf->GetHorizontalStepSize(sizing));
01643     extra_width -= inf_width - this->inf->smallest_x;
01644 
01645     uint inf_height = ComputeMaxSize(this->inf->smallest_y, given_height, this->inf->GetVerticalStepSize(sizing));
01646 
01647     if (use_three_columns) {
01648       /* Three column display, first make both lists equally wide, then divide whatever is left between both lists.
01649        * Only keep track of what avs gets, all other space goes to acs. */
01650       uint avs_width = min(avs_extra_width, extra_width);
01651       extra_width -= avs_width;
01652       extra_width -= min(acs_extra_width, extra_width);
01653       avs_width += extra_width / 2;
01654 
01655       avs_width = ComputeMaxSize(this->avs->smallest_x, this->avs->smallest_x + avs_width, this->avs->GetHorizontalStepSize(sizing));
01656 
01657       uint acs_width = given_width - // Remaining space, including horizontal padding.
01658           inf_width - this->inf->padding_left - this->inf->padding_right -
01659           avs_width - this->avs->padding_left - this->avs->padding_right - 2 * INTER_COLUMN_SPACING;
01660       acs_width = ComputeMaxSize(min_acs_width, acs_width, this->acs->GetHorizontalStepSize(sizing)) -
01661           this->acs->padding_left - this->acs->padding_right;
01662 
01663       /* Never use fill_y on these; the minimal size is choosen, so that the 3 column view looks nice */
01664       uint avs_height = ComputeMaxSize(this->avs->smallest_y, given_height, this->avs->resize_y);
01665       uint acs_height = ComputeMaxSize(this->acs->smallest_y, given_height, this->acs->resize_y);
01666 
01667       /* Assign size and position to the childs. */
01668       if (rtl) {
01669         x += this->inf->padding_left;
01670         this->inf->AssignSizePosition(sizing, x, y + this->inf->padding_top, inf_width, inf_height, rtl);
01671         x += inf_width + this->inf->padding_right + INTER_COLUMN_SPACING;
01672       } else {
01673         x += this->avs->padding_left;
01674         this->avs->AssignSizePosition(sizing, x, y + this->avs->padding_top, avs_width, avs_height, rtl);
01675         x += avs_width + this->avs->padding_right + INTER_COLUMN_SPACING;
01676       }
01677 
01678       x += this->acs->padding_left;
01679       this->acs->AssignSizePosition(sizing, x, y + this->acs->padding_top, acs_width, acs_height, rtl);
01680       x += acs_width + this->acs->padding_right + INTER_COLUMN_SPACING;
01681 
01682       if (rtl) {
01683         x += this->avs->padding_left;
01684         this->avs->AssignSizePosition(sizing, x, y + this->avs->padding_top, avs_width, avs_height, rtl);
01685       } else {
01686         x += this->inf->padding_left;
01687         this->inf->AssignSizePosition(sizing, x, y + this->inf->padding_top, inf_width, inf_height, rtl);
01688       }
01689     } else {
01690       /* Two columns, all space in extra_width goes to both lists. Since the lists are underneath each other,
01691        * the column is min_list_width wide at least. */
01692       uint avs_width = ComputeMaxSize(this->avs->smallest_x, this->avs->smallest_x + avs_extra_width + extra_width,
01693           this->avs->GetHorizontalStepSize(sizing));
01694       uint acs_width = ComputeMaxSize(this->acs->smallest_x, this->acs->smallest_x + acs_extra_width + extra_width,
01695           this->acs->GetHorizontalStepSize(sizing));
01696 
01697       uint min_avs_height = (!this->editable) ? 0 : this->avs->smallest_y + this->avs->padding_top + this->avs->padding_bottom + INTER_LIST_SPACING;
01698       uint min_acs_height = this->acs->smallest_y + this->acs->padding_top + this->acs->padding_bottom;
01699       uint extra_height = given_height - min_acs_height - min_avs_height;
01700 
01701       /* Never use fill_y on these; instead use the INTER_LIST_SPACING as filler */
01702       uint avs_height = ComputeMaxSize(this->avs->smallest_y, this->avs->smallest_y + extra_height / 2, this->avs->resize_y);
01703       if (this->editable) extra_height -= avs_height - this->avs->smallest_y;
01704       uint acs_height = ComputeMaxSize(this->acs->smallest_y, this->acs->smallest_y + extra_height, this->acs->resize_y);
01705 
01706       /* Assign size and position to the childs. */
01707       if (rtl) {
01708         x += this->inf->padding_left;
01709         this->inf->AssignSizePosition(sizing, x, y + this->inf->padding_top, inf_width, inf_height, rtl);
01710         x += inf_width + this->inf->padding_right + INTER_COLUMN_SPACING;
01711 
01712         this->acs->AssignSizePosition(sizing, x + this->acs->padding_left, y + this->acs->padding_top, acs_width, acs_height, rtl);
01713         if (this->editable) {
01714           this->avs->AssignSizePosition(sizing, x + this->avs->padding_left, y + given_height - avs_height - this->avs->padding_bottom, avs_width, avs_height, rtl);
01715         } else {
01716           this->avs->AssignSizePosition(sizing, 0, 0, this->avs->smallest_x, this->avs->smallest_y, rtl);
01717         }
01718       } else {
01719         this->acs->AssignSizePosition(sizing, x + this->acs->padding_left, y + this->acs->padding_top, acs_width, acs_height, rtl);
01720         if (this->editable) {
01721           this->avs->AssignSizePosition(sizing, x + this->avs->padding_left, y + given_height - avs_height - this->avs->padding_bottom, avs_width, avs_height, rtl);
01722         } else {
01723           this->avs->AssignSizePosition(sizing, 0, 0, this->avs->smallest_x, this->avs->smallest_y, rtl);
01724         }
01725         uint dx = this->acs->current_x + this->acs->padding_left + this->acs->padding_right;
01726         if (this->editable) {
01727           dx = max(dx, this->avs->current_x + this->avs->padding_left + this->avs->padding_right);
01728         }
01729         x += dx + INTER_COLUMN_SPACING + this->inf->padding_left;
01730         this->inf->AssignSizePosition(sizing, x, y + this->inf->padding_top, inf_width, inf_height, rtl);
01731       }
01732     }
01733   }
01734 
01735   virtual NWidgetCore *GetWidgetFromPos(int x, int y)
01736   {
01737     if (!IsInsideBS(x, this->pos_x, this->current_x) || !IsInsideBS(y, this->pos_y, this->current_y)) return NULL;
01738 
01739     NWidgetCore *nw = (this->editable) ? this->avs->GetWidgetFromPos(x, y) : NULL;
01740     if (nw == NULL) nw = this->acs->GetWidgetFromPos(x, y);
01741     if (nw == NULL) nw = this->inf->GetWidgetFromPos(x, y);
01742     return nw;
01743   }
01744 
01745   virtual void Draw(const Window *w)
01746   {
01747     if (this->editable) this->avs->Draw(w);
01748     this->acs->Draw(w);
01749     this->inf->Draw(w);
01750   }
01751 };
01752 
01753 const uint NWidgetNewGRFDisplay::INTER_LIST_SPACING      = WD_RESIZEBOX_WIDTH + 1;
01754 const uint NWidgetNewGRFDisplay::INTER_COLUMN_SPACING    = WD_RESIZEBOX_WIDTH;
01755 const uint NWidgetNewGRFDisplay::MAX_EXTRA_INFO_WIDTH    = 150;
01756 const uint NWidgetNewGRFDisplay::MIN_EXTRA_FOR_3_COLUMNS = 50;
01757 
01758 static const NWidgetPart _nested_newgrf_actives_widgets[] = {
01759   /* Left side, presets. */
01760   NWidget(NWID_HORIZONTAL),
01761     NWidget(WWT_TEXT, COLOUR_MAUVE), SetDataTip(STR_NEWGRF_SETTINGS_SELECT_PRESET, STR_NULL),
01762         SetPadding(0, WD_FRAMETEXT_RIGHT, 0, 0),
01763     NWidget(WWT_DROPDOWN, COLOUR_YELLOW, SNGRFS_PRESET_LIST), SetFill(1, 0), SetResize(1, 0),
01764         SetDataTip(STR_JUST_STRING, STR_NEWGRF_SETTINGS_PRESET_LIST_TOOLTIP),
01765   EndContainer(),
01766   NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
01767     NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, SNGRFS_PRESET_SAVE), SetFill(1, 0), SetResize(1, 0),
01768         SetDataTip(STR_NEWGRF_SETTINGS_PRESET_SAVE, STR_NEWGRF_SETTINGS_PRESET_SAVE_TOOLTIP),
01769     NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, SNGRFS_PRESET_DELETE), SetFill(1, 0), SetResize(1, 0),
01770         SetDataTip(STR_NEWGRF_SETTINGS_PRESET_DELETE, STR_NEWGRF_SETTINGS_PRESET_DELETE_TOOLTIP),
01771   EndContainer(),
01772 
01773   NWidget(NWID_SPACER), SetMinimalSize(0, WD_RESIZEBOX_WIDTH), SetResize(1, 0), SetFill(1, 0),
01774   NWidget(WWT_PANEL, COLOUR_MAUVE),
01775     NWidget(WWT_LABEL, COLOUR_MAUVE), SetDataTip(STR_NEWGRF_SETTINGS_ACTIVE_LIST, STR_NULL),
01776         SetFill(1, 0), SetResize(1, 0), SetPadding(3, WD_FRAMETEXT_RIGHT, 0, WD_FRAMETEXT_LEFT),
01777     /* Left side, active grfs. */
01778     NWidget(NWID_HORIZONTAL), SetPadding(0, 2, 0, 2),
01779       NWidget(WWT_PANEL, COLOUR_MAUVE),
01780         NWidget(WWT_INSET, COLOUR_MAUVE, SNGRFS_FILE_LIST), SetMinimalSize(100, 1), SetPadding(2, 2, 2, 2),
01781             SetFill(1, 1), SetResize(1, 1), SetScrollbar(SNGRFS_SCROLLBAR), SetDataTip(STR_NULL, STR_NEWGRF_SETTINGS_FILE_TOOLTIP),
01782         EndContainer(),
01783       EndContainer(),
01784       NWidget(NWID_VSCROLLBAR, COLOUR_MAUVE, SNGRFS_SCROLLBAR),
01785     EndContainer(),
01786     /* Buttons. */
01787     NWidget(NWID_SELECTION, INVALID_COLOUR, SNGRFS_SHOW_REMOVE),
01788       NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPadding(2, 2, 2, 2), SetPIP(0, WD_RESIZEBOX_WIDTH, 0),
01789         NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, SNGRFS_REMOVE), SetFill(1, 0), SetResize(1, 0),
01790             SetDataTip(STR_NEWGRF_SETTINGS_REMOVE, STR_NEWGRF_SETTINGS_REMOVE_TOOLTIP),
01791         NWidget(NWID_VERTICAL),
01792           NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, SNGRFS_MOVE_UP), SetFill(1, 0), SetResize(1, 0),
01793               SetDataTip(STR_NEWGRF_SETTINGS_MOVEUP, STR_NEWGRF_SETTINGS_MOVEUP_TOOLTIP),
01794           NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, SNGRFS_MOVE_DOWN), SetFill(1, 0), SetResize(1, 0),
01795               SetDataTip(STR_NEWGRF_SETTINGS_MOVEDOWN, STR_NEWGRF_SETTINGS_MOVEDOWN_TOOLTIP),
01796         EndContainer(),
01797       EndContainer(),
01798 
01799       NWidget(NWID_VERTICAL, NC_EQUALSIZE), SetPadding(2, 2, 2, 2),
01800         NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, SNGRFS_RESCAN_FILES2), SetFill(1, 0), SetResize(1, 0),
01801             SetDataTip(STR_NEWGRF_SETTINGS_RESCAN_FILES, STR_NEWGRF_SETTINGS_RESCAN_FILES_TOOLTIP),
01802         NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, SNGRFS_CONTENT_DOWNLOAD2), SetFill(1, 0), SetResize(1, 0),
01803             SetDataTip(STR_INTRO_ONLINE_CONTENT, STR_INTRO_TOOLTIP_ONLINE_CONTENT),
01804       EndContainer(),
01805     EndContainer(),
01806   EndContainer(),
01807 };
01808 
01809 static const NWidgetPart _nested_newgrf_availables_widgets[] = {
01810   NWidget(WWT_PANEL, COLOUR_MAUVE),
01811     NWidget(WWT_LABEL, COLOUR_MAUVE), SetDataTip(STR_NEWGRF_SETTINGS_INACTIVE_LIST, STR_NULL),
01812         SetFill(1, 0), SetResize(1, 0), SetPadding(3, WD_FRAMETEXT_RIGHT, 0, WD_FRAMETEXT_LEFT),
01813     /* Left side, available grfs, filter edit box. */
01814     NWidget(NWID_HORIZONTAL), SetPadding(WD_TEXTPANEL_TOP, 0, WD_TEXTPANEL_BOTTOM, 0),
01815         SetPIP(WD_FRAMETEXT_LEFT, WD_FRAMETEXT_RIGHT, WD_FRAMETEXT_RIGHT),
01816       NWidget(WWT_TEXT, COLOUR_MAUVE), SetFill(0, 1), SetDataTip(STR_NEWGRF_FILTER_TITLE, STR_NULL),
01817       NWidget(WWT_EDITBOX, COLOUR_MAUVE, SNGRFS_FILTER), SetFill(1, 0), SetMinimalSize(50, 12), SetResize(1, 0),
01818           SetDataTip(STR_LIST_FILTER_OSKTITLE, STR_LIST_FILTER_TOOLTIP),
01819     EndContainer(),
01820     /* Left side, available grfs. */
01821     NWidget(NWID_HORIZONTAL), SetPadding(0, 2, 0, 2),
01822       NWidget(WWT_PANEL, COLOUR_MAUVE),
01823         NWidget(WWT_INSET, COLOUR_MAUVE, SNGRFS_AVAIL_LIST), SetMinimalSize(100, 1), SetPadding(2, 2, 2, 2),
01824             SetFill(1, 1), SetResize(1, 1), SetScrollbar(SNGRFS_SCROLL2BAR),
01825         EndContainer(),
01826       EndContainer(),
01827       NWidget(NWID_VSCROLLBAR, COLOUR_MAUVE, SNGRFS_SCROLL2BAR),
01828     EndContainer(),
01829     /* Left side, available grfs, buttons. */
01830     NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPadding(2, 2, 2, 2), SetPIP(0, WD_RESIZEBOX_WIDTH, 0),
01831       NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, SNGRFS_ADD), SetFill(1, 0), SetResize(1, 0),
01832           SetDataTip(STR_NEWGRF_SETTINGS_ADD, STR_NEWGRF_SETTINGS_ADD_FILE_TOOLTIP),
01833       NWidget(NWID_VERTICAL),
01834         NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, SNGRFS_RESCAN_FILES), SetFill(1, 0), SetResize(1, 0),
01835             SetDataTip(STR_NEWGRF_SETTINGS_RESCAN_FILES, STR_NEWGRF_SETTINGS_RESCAN_FILES_TOOLTIP),
01836         NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, SNGRFS_CONTENT_DOWNLOAD), SetFill(1, 0), SetResize(1, 0),
01837             SetDataTip(STR_INTRO_ONLINE_CONTENT, STR_INTRO_TOOLTIP_ONLINE_CONTENT),
01838       EndContainer(),
01839     EndContainer(),
01840   EndContainer(),
01841 };
01842 
01843 static const NWidgetPart _nested_newgrf_infopanel_widgets[] = {
01844   /* Right side, info panel. */
01845   NWidget(NWID_VERTICAL), SetPadding(0, 0, 2, 0),
01846     NWidget(WWT_PANEL, COLOUR_MAUVE), SetPadding(0, 0, 2, 0),
01847       NWidget(WWT_EMPTY, COLOUR_MAUVE, SNGRFS_NEWGRF_INFO_TITLE), SetFill(1, 0), SetResize(1, 0),
01848       NWidget(WWT_EMPTY, COLOUR_MAUVE, SNGRFS_NEWGRF_INFO), SetFill(1, 1), SetResize(1, 1), SetMinimalSize(150, 100),
01849     EndContainer(),
01850     NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, SNGRFS_OPEN_URL), SetFill(1, 0), SetResize(1, 0),
01851         SetDataTip(STR_CONTENT_OPEN_URL, STR_CONTENT_OPEN_URL_TOOLTIP),
01852     NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, SNGRFS_NEWGRF_TEXTFILE + TFT_README), SetFill(1, 0), SetResize(1, 0),
01853         SetDataTip(STR_NEWGRF_SETTINGS_VIEW_README, STR_NULL),
01854     NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
01855       NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, SNGRFS_NEWGRF_TEXTFILE + TFT_CHANGELOG), SetFill(1, 0), SetResize(1, 0),
01856           SetDataTip(STR_NEWGRF_SETTINGS_VIEW_CHANGELOG, STR_NULL),
01857       NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, SNGRFS_NEWGRF_TEXTFILE + TFT_LICENSE), SetFill(1, 0), SetResize(1, 0),
01858           SetDataTip(STR_NEWGRF_SETTINGS_VIEW_LICENSE, STR_NULL),
01859     EndContainer(),
01860   EndContainer(),
01861   NWidget(NWID_SELECTION, INVALID_COLOUR, SNGRFS_SHOW_APPLY),
01862     /* Right side, buttons. */
01863     NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(0, WD_RESIZEBOX_WIDTH, 0),
01864       NWidget(NWID_VERTICAL),
01865         NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, SNGRFS_SET_PARAMETERS), SetFill(1, 0), SetResize(1, 0),
01866             SetDataTip(STR_NEWGRF_SETTINGS_SET_PARAMETERS, STR_NULL),
01867         NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, SNGRFS_TOGGLE_PALETTE), SetFill(1, 0), SetResize(1, 0),
01868             SetDataTip(STR_NEWGRF_SETTINGS_TOGGLE_PALETTE, STR_NEWGRF_SETTINGS_TOGGLE_PALETTE_TOOLTIP),
01869       EndContainer(),
01870       NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, SNGRFS_APPLY_CHANGES), SetFill(1, 0), SetResize(1, 0),
01871           SetDataTip(STR_NEWGRF_SETTINGS_APPLY_CHANGES, STR_NULL),
01872     EndContainer(),
01873   EndContainer(),
01874 };
01875 
01877 NWidgetBase* NewGRFDisplay(int *biggest_index)
01878 {
01879   NWidgetBase *avs = MakeNWidgets(_nested_newgrf_availables_widgets, lengthof(_nested_newgrf_availables_widgets), biggest_index, NULL);
01880 
01881   int biggest2;
01882   NWidgetBase *acs = MakeNWidgets(_nested_newgrf_actives_widgets, lengthof(_nested_newgrf_actives_widgets), &biggest2, NULL);
01883   *biggest_index = max(*biggest_index, biggest2);
01884 
01885   NWidgetBase *inf = MakeNWidgets(_nested_newgrf_infopanel_widgets, lengthof(_nested_newgrf_infopanel_widgets), &biggest2, NULL);
01886   *biggest_index = max(*biggest_index, biggest2);
01887 
01888   return new NWidgetNewGRFDisplay(avs, acs, inf);
01889 }
01890 
01891 /* Widget definition of the manage newgrfs window */
01892 static const NWidgetPart _nested_newgrf_widgets[] = {
01893   NWidget(NWID_HORIZONTAL),
01894     NWidget(WWT_CLOSEBOX, COLOUR_MAUVE),
01895     NWidget(WWT_CAPTION, COLOUR_MAUVE), SetDataTip(STR_NEWGRF_SETTINGS_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
01896   EndContainer(),
01897   NWidget(WWT_PANEL, COLOUR_MAUVE),
01898     NWidgetFunction(NewGRFDisplay), SetPadding(WD_RESIZEBOX_WIDTH, WD_RESIZEBOX_WIDTH, 2, WD_RESIZEBOX_WIDTH),
01899     /* Resize button. */
01900     NWidget(NWID_HORIZONTAL),
01901       NWidget(NWID_SPACER), SetFill(1, 0), SetResize(1, 0),
01902       NWidget(WWT_RESIZEBOX, COLOUR_MAUVE),
01903     EndContainer(),
01904   EndContainer(),
01905 };
01906 
01907 /* Window definition of the manage newgrfs window */
01908 static const WindowDesc _newgrf_desc(
01909   WDP_CENTER, 300, 263,
01910   WC_GAME_OPTIONS, WC_NONE,
01911   WDF_UNCLICK_BUTTONS,
01912   _nested_newgrf_widgets, lengthof(_nested_newgrf_widgets)
01913 );
01914 
01920 static void NewGRFConfirmationCallback(Window *w, bool confirmed)
01921 {
01922   if (confirmed) {
01923     DeleteWindowByClass(WC_GRF_PARAMETERS);
01924     NewGRFWindow *nw = dynamic_cast<NewGRFWindow*>(w);
01925 
01926     GamelogStartAction(GLAT_GRF);
01927     GamelogGRFUpdate(_grfconfig, nw->actives); // log GRF changes
01928     CopyGRFConfigList(nw->orig_list, nw->actives, false);
01929     ReloadNewGRFData();
01930     GamelogStopAction();
01931 
01932     /* Show new, updated list */
01933     GRFConfig *c;
01934     int i = 0;
01935     for (c = nw->actives; c != NULL && c != nw->active_sel; c = c->next, i++) {}
01936     CopyGRFConfigList(&nw->actives, *nw->orig_list, false);
01937     for (c = nw->actives; c != NULL && i > 0; c = c->next, i--) {}
01938     nw->active_sel = c;
01939     nw->avails.ForceRebuild();
01940 
01941     w->InvalidateData();
01942 
01943     ReInitAllWindows();
01944     DeleteWindowByClass(WC_BUILD_OBJECT);
01945   }
01946 }
01947 
01948 
01949 
01958 void ShowNewGRFSettings(bool editable, bool show_params, bool exec_changes, GRFConfig **config)
01959 {
01960   DeleteWindowByClass(WC_GAME_OPTIONS);
01961   new NewGRFWindow(&_newgrf_desc, editable, show_params, exec_changes, config);
01962 }
01963 
01965 enum ScanProgressWindowWidgets {
01966   SPWW_PROGRESS_BAR,  
01967   GPWW_PROGRESS_TEXT, 
01968 };
01969 
01971 static const NWidgetPart _nested_scan_progress_widgets[] = {
01972   NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_NEWGRF_SCAN_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
01973   NWidget(WWT_PANEL, COLOUR_GREY),
01974     NWidget(NWID_HORIZONTAL), SetPIP(20, 0, 20),
01975       NWidget(NWID_VERTICAL), SetPIP(11, 8, 11),
01976         NWidget(WWT_LABEL, INVALID_COLOUR), SetDataTip(STR_NEWGRF_SCAN_MESSAGE, STR_NULL), SetFill(1, 0),
01977         NWidget(WWT_EMPTY, INVALID_COLOUR, SPWW_PROGRESS_BAR), SetFill(1, 0),
01978         NWidget(WWT_EMPTY, INVALID_COLOUR, GPWW_PROGRESS_TEXT), SetFill(1, 0),
01979       EndContainer(),
01980     EndContainer(),
01981   EndContainer(),
01982 };
01983 
01985 static const WindowDesc _scan_progress_desc(
01986   WDP_CENTER, 0, 0,
01987   WC_MODAL_PROGRESS, WC_NONE,
01988   WDF_UNCLICK_BUTTONS,
01989   _nested_scan_progress_widgets, lengthof(_nested_scan_progress_widgets)
01990 );
01991 
01993 struct ScanProgressWindow : public Window {
01994   char *last_name; 
01995   int scanned;     
01996 
01998   ScanProgressWindow() : Window(), last_name(NULL), scanned(0)
01999   {
02000     this->InitNested(&_scan_progress_desc);
02001   }
02002 
02004   ~ScanProgressWindow()
02005   {
02006     free(last_name);
02007   }
02008 
02009   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
02010   {
02011     switch (widget) {
02012       case SPWW_PROGRESS_BAR: {
02013         SetDParam(0, 100);
02014         *size = GetStringBoundingBox(STR_GENERATION_PROGRESS);
02015         /* We need some spacing for the 'border' */
02016         size->height += 8;
02017         size->width += 8;
02018         break;
02019       }
02020 
02021       case GPWW_PROGRESS_TEXT:
02022         SetDParam(0, 9999);
02023         SetDParam(1, 9999);
02024         /* We really don't know the width. We could determine it by scanning the NewGRFs,
02025          * but this is the status window for scanning them... */
02026         size->width = max(400U, GetStringBoundingBox(STR_NEWGRF_SCAN_STATUS).width);
02027         size->height = FONT_HEIGHT_NORMAL * 2 + WD_PAR_VSEP_NORMAL;
02028         break;
02029     }
02030   }
02031 
02032   virtual void DrawWidget(const Rect &r, int widget) const
02033   {
02034     switch (widget) {
02035       case SPWW_PROGRESS_BAR: {
02036         /* Draw the % complete with a bar and a text */
02037         DrawFrameRect(r.left, r.top, r.right, r.bottom, COLOUR_GREY, FR_BORDERONLY);
02038         uint percent = scanned * 100 / max(1U, _settings_client.gui.last_newgrf_count);
02039         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);
02040         SetDParam(0, percent);
02041         DrawString(r.left, r.right, r.top + 5, STR_GENERATION_PROGRESS, TC_FROMSTRING, SA_HOR_CENTER);
02042         break;
02043       }
02044 
02045       case GPWW_PROGRESS_TEXT:
02046         SetDParam(0, this->scanned);
02047         SetDParam(1, _settings_client.gui.last_newgrf_count);
02048         DrawString(r.left, r.right, r.top, STR_NEWGRF_SCAN_STATUS, TC_FROMSTRING, SA_HOR_CENTER);
02049 
02050         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);
02051         break;
02052     }
02053   }
02054 
02060   void UpdateNewGRFScanStatus(uint num, const char *name)
02061   {
02062     free(this->last_name);
02063     if (name == NULL) {
02064       char buf[256];
02065       GetString(buf, STR_NEWGRF_SCAN_ARCHIVES, lastof(buf));
02066       this->last_name = strdup(buf);
02067     } else {
02068       this->last_name = strdup(name);
02069     }
02070     this->scanned = num;
02071     if (num > _settings_client.gui.last_newgrf_count) _settings_client.gui.last_newgrf_count = num;
02072 
02073     this->SetDirty();
02074   }
02075 };
02076 
02082 void UpdateNewGRFScanStatus(uint num, const char *name)
02083 {
02084   ScanProgressWindow *w  = dynamic_cast<ScanProgressWindow *>(FindWindowByClass(WC_MODAL_PROGRESS));
02085   if (w == NULL) w = new ScanProgressWindow();
02086   w->UpdateNewGRFScanStatus(num, name);
02087 }