newgrf_gui.cpp

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