# newgrf_gui.cpp

Go to the documentation of this file.

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