newgrf_gui.cpp

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