newgrf_gui.cpp

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

Generated on Fri Feb 4 20:53:43 2011 for OpenTTD by  doxygen 1.6.1