# newgrf_gui.cpp

Go to the documentation of this file.

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