# newgrf_gui.cpp

Go to the documentation of this file.

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