newgrf_gui.cpp

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

Generated on Sun Jan 9 16:01:58 2011 for OpenTTD by  doxygen 1.6.1