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