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