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 "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 
00033 #include "widgets/newgrf_widget.h"
00034 #include "widgets/misc_widget.h"
00035 
00036 #include "table/sprites.h"
00037 
00041 void ShowNewGRFError()
00042 {
00043   /* Do not show errors when entering the main screen */
00044   if (_game_mode == GM_MENU) return;
00045 
00046   for (const GRFConfig *c = _grfconfig; c != NULL; c = c->next) {
00047     /* We only want to show fatal errors */
00048     if (c->error == NULL || c->error->severity != STR_NEWGRF_ERROR_MSG_FATAL) continue;
00049 
00050     SetDParam   (0, c->error->custom_message == NULL ? c->error->message : STR_JUST_RAW_STRING);
00051     SetDParamStr(1, c->error->custom_message);
00052     SetDParamStr(2, c->filename);
00053     SetDParamStr(3, c->error->data);
00054     for (uint i = 0; i < lengthof(c->error->param_value); i++) {
00055       SetDParam(4 + i, c->error->param_value[i]);
00056     }
00057     ShowErrorMessage(STR_NEWGRF_ERROR_FATAL_POPUP, INVALID_STRING_ID, WL_CRITICAL);
00058     break;
00059   }
00060 }
00061 
00062 static void ShowNewGRFInfo(const GRFConfig *c, uint x, uint y, uint right, uint bottom, bool show_params)
00063 {
00064   if (c->error != NULL) {
00065     char message[512];
00066     SetDParamStr(0, c->error->custom_message); // is skipped by built-in messages
00067     SetDParamStr(1, c->filename);
00068     SetDParamStr(2, c->error->data);
00069     for (uint i = 0; i < lengthof(c->error->param_value); i++) {
00070       SetDParam(3 + i, c->error->param_value[i]);
00071     }
00072     GetString(message, c->error->custom_message == NULL ? c->error->message : STR_JUST_RAW_STRING, lastof(message));
00073 
00074     SetDParamStr(0, message);
00075     y = DrawStringMultiLine(x, right, y, bottom, c->error->severity);
00076   }
00077 
00078   /* Draw filename or not if it is not known (GRF sent over internet) */
00079   if (c->filename != NULL) {
00080     SetDParamStr(0, c->filename);
00081     y = DrawStringMultiLine(x, right, y, bottom, STR_NEWGRF_SETTINGS_FILENAME);
00082   }
00083 
00084   /* Prepare and draw GRF ID */
00085   char buff[256];
00086   snprintf(buff, lengthof(buff), "%08X", BSWAP32(c->ident.grfid));
00087   SetDParamStr(0, buff);
00088   y = DrawStringMultiLine(x, right, y, bottom, STR_NEWGRF_SETTINGS_GRF_ID);
00089 
00090   if ((_settings_client.gui.newgrf_developer_tools || _settings_client.gui.newgrf_show_old_versions) && c->version != 0) {
00091     SetDParam(0, c->version);
00092     y = DrawStringMultiLine(x, right, y, bottom, STR_NEWGRF_SETTINGS_VERSION);
00093   }
00094   if ((_settings_client.gui.newgrf_developer_tools || _settings_client.gui.newgrf_show_old_versions) && c->min_loadable_version != 0) {
00095     SetDParam(0, c->min_loadable_version);
00096     y = DrawStringMultiLine(x, right, y, bottom, STR_NEWGRF_SETTINGS_MIN_VERSION);
00097   }
00098 
00099   /* Prepare and draw MD5 sum */
00100   md5sumToString(buff, lastof(buff), c->ident.md5sum);
00101   SetDParamStr(0, buff);
00102   y = DrawStringMultiLine(x, right, y, bottom, STR_NEWGRF_SETTINGS_MD5SUM);
00103 
00104   /* Show GRF parameter list */
00105   if (show_params) {
00106     if (c->num_params > 0) {
00107       GRFBuildParamList(buff, c, lastof(buff));
00108       SetDParam(0, STR_JUST_RAW_STRING);
00109       SetDParamStr(1, buff);
00110     } else {
00111       SetDParam(0, STR_LAND_AREA_INFORMATION_LOCAL_AUTHORITY_NONE);
00112     }
00113     y = DrawStringMultiLine(x, right, y, bottom, STR_NEWGRF_SETTINGS_PARAMETER);
00114 
00115     /* Draw the palette of the NewGRF */
00116     if (c->palette & GRFP_BLT_32BPP) {
00117       SetDParamStr(0, (c->palette & GRFP_USE_WINDOWS) ? "Windows / 32 bpp" : "DOS / 32 bpp");
00118     } else {
00119       SetDParamStr(0, (c->palette & GRFP_USE_WINDOWS) ? "Windows" : "DOS");
00120     }
00121     y = DrawStringMultiLine(x, right, y, bottom, STR_NEWGRF_SETTINGS_PALETTE);
00122   }
00123 
00124   /* Show flags */
00125   if (c->status == GCS_NOT_FOUND)       y = DrawStringMultiLine(x, right, y, bottom, STR_NEWGRF_SETTINGS_NOT_FOUND);
00126   if (c->status == GCS_DISABLED)        y = DrawStringMultiLine(x, right, y, bottom, STR_NEWGRF_SETTINGS_DISABLED);
00127   if (HasBit(c->flags, GCF_INVALID))    y = DrawStringMultiLine(x, right, y, bottom, STR_NEWGRF_SETTINGS_INCOMPATIBLE);
00128   if (HasBit(c->flags, GCF_COMPATIBLE)) y = DrawStringMultiLine(x, right, y, bottom, STR_NEWGRF_COMPATIBLE_LOADED);
00129 
00130   /* Draw GRF info if it exists */
00131   if (!StrEmpty(c->GetDescription())) {
00132     SetDParam(0, STR_JUST_RAW_STRING);
00133     SetDParamStr(1, c->GetDescription());
00134     y = DrawStringMultiLine(x, right, y, bottom, STR_BLACK_STRING);
00135   } else {
00136     y = DrawStringMultiLine(x, right, y, bottom, STR_NEWGRF_SETTINGS_NO_INFO);
00137   }
00138 }
00139 
00143 struct NewGRFParametersWindow : public Window {
00144   static GRFParameterInfo dummy_parameter_info; 
00145   GRFConfig *grf_config; 
00146   uint clicked_button;   
00147   bool clicked_increase; 
00148   bool clicked_dropdown; 
00149   bool closing_dropdown; 
00150   int timeout;           
00151   uint clicked_row;      
00152   int line_height;       
00153   Scrollbar *vscroll;
00154   bool action14present;  
00155   bool editable;         
00156 
00157   NewGRFParametersWindow(const WindowDesc *desc, GRFConfig *c, bool editable) : Window(),
00158     grf_config(c),
00159     clicked_button(UINT_MAX),
00160     clicked_dropdown(false),
00161     closing_dropdown(false),
00162     timeout(0),
00163     clicked_row(UINT_MAX),
00164     editable(editable)
00165   {
00166     this->action14present = (c->num_valid_params != lengthof(c->param) || c->param_info.Length() != 0);
00167 
00168     this->CreateNestedTree(desc);
00169     this->vscroll = this->GetScrollbar(WID_NP_SCROLLBAR);
00170     this->GetWidget<NWidgetStacked>(WID_NP_SHOW_NUMPAR)->SetDisplayedPlane(this->action14present ? SZSP_HORIZONTAL : 0);
00171     this->GetWidget<NWidgetStacked>(WID_NP_SHOW_DESCRIPTION)->SetDisplayedPlane(this->action14present ? 0 : SZSP_HORIZONTAL);
00172     this->FinishInitNested(desc);  // Initializes 'this->line_height' as side effect.
00173 
00174     this->SetWidgetDisabledState(WID_NP_RESET, !this->editable);
00175 
00176     this->InvalidateData();
00177   }
00178 
00184   static GRFParameterInfo *GetDummyParameterInfo(uint nr)
00185   {
00186     dummy_parameter_info.param_nr = nr;
00187     return &dummy_parameter_info;
00188   }
00189 
00190   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00191   {
00192     switch (widget) {
00193       case WID_NP_NUMPAR_DEC:
00194       case WID_NP_NUMPAR_INC: {
00195         size->width = size->height = FONT_HEIGHT_NORMAL;
00196         break;
00197       }
00198 
00199       case WID_NP_NUMPAR: {
00200         SetDParam(0, 999);
00201         Dimension d = GetStringBoundingBox(this->GetWidget<NWidgetCore>(widget)->widget_data);
00202         d.width += padding.width;
00203         d.height += padding.height;
00204         *size = maxdim(*size, d);
00205         break;
00206       }
00207 
00208       case WID_NP_BACKGROUND:
00209         this->line_height = FONT_HEIGHT_NORMAL + WD_MATRIX_TOP + WD_MATRIX_BOTTOM;
00210 
00211         resize->width = 1;
00212         resize->height = this->line_height;
00213         size->height = GB(this->GetWidget<NWidgetCore>(widget)->widget_data, MAT_ROW_START, MAT_ROW_BITS) * this->line_height;
00214         break;
00215 
00216       case WID_NP_DESCRIPTION:
00217         /* Minimum size of 4 lines. The 500 is the default size of the window. */
00218         Dimension suggestion = {500 - WD_FRAMERECT_LEFT - WD_FRAMERECT_RIGHT, FONT_HEIGHT_NORMAL * 4 + WD_TEXTPANEL_TOP + WD_TEXTPANEL_BOTTOM};
00219         for (uint i = 0; i < this->grf_config->param_info.Length(); i++) {
00220           const GRFParameterInfo *par_info = this->grf_config->param_info[i];
00221           if (par_info == NULL) continue;
00222           const char *desc = GetGRFStringFromGRFText(par_info->desc);
00223           if (desc == NULL) continue;
00224           Dimension d = GetStringMultiLineBoundingBox(desc, suggestion);
00225           d.height += WD_TEXTPANEL_TOP + WD_TEXTPANEL_BOTTOM;
00226           suggestion = maxdim(d, suggestion);
00227         }
00228         size->height = suggestion.height;
00229         break;
00230     }
00231   }
00232 
00233   virtual void SetStringParameters(int widget) const
00234   {
00235     switch (widget) {
00236       case WID_NP_NUMPAR:
00237         SetDParam(0, this->vscroll->GetCount());
00238         break;
00239     }
00240   }
00241 
00242   virtual void DrawWidget(const Rect &r, int widget) const
00243   {
00244     if (widget == WID_NP_DESCRIPTION) {
00245       const GRFParameterInfo *par_info = (this->clicked_row < this->grf_config->param_info.Length()) ? this->grf_config->param_info[this->clicked_row] : NULL;
00246       if (par_info == NULL) return;
00247       const char *desc = GetGRFStringFromGRFText(par_info->desc);
00248       if (desc == NULL) return;
00249       DrawStringMultiLine(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + WD_TEXTPANEL_TOP, r.bottom - WD_TEXTPANEL_BOTTOM, desc, TC_BLACK);
00250       return;
00251     } else if (widget != WID_NP_BACKGROUND) {
00252       return;
00253     }
00254 
00255     bool rtl = _current_text_dir == TD_RTL;
00256     uint buttons_left = rtl ? r.right - SETTING_BUTTON_WIDTH - 3 : r.left + 4;
00257     uint text_left    = r.left + (rtl ? WD_FRAMERECT_LEFT : SETTING_BUTTON_WIDTH + 8);
00258     uint text_right   = r.right - (rtl ? SETTING_BUTTON_WIDTH + 8 : WD_FRAMERECT_RIGHT);
00259 
00260     int y = r.top;
00261     int button_y_offset = (this->line_height - SETTING_BUTTON_HEIGHT) / 2;
00262     for (uint i = this->vscroll->GetPosition(); this->vscroll->IsVisible(i) && i < this->vscroll->GetCount(); i++) {
00263       GRFParameterInfo *par_info = (i < this->grf_config->param_info.Length()) ? this->grf_config->param_info[i] : NULL;
00264       if (par_info == NULL) par_info = GetDummyParameterInfo(i);
00265       uint32 current_value = par_info->GetValue(this->grf_config);
00266       bool selected = (i == this->clicked_row);
00267 
00268       if (par_info->type == PTYPE_BOOL) {
00269         DrawBoolButton(buttons_left, y + button_y_offset, current_value != 0, this->editable);
00270         SetDParam(2, par_info->GetValue(this->grf_config) == 0 ? STR_CONFIG_SETTING_OFF : STR_CONFIG_SETTING_ON);
00271       } else if (par_info->type == PTYPE_UINT_ENUM) {
00272         if (par_info->complete_labels) {
00273           DrawDropDownButton(buttons_left, y + button_y_offset, COLOUR_YELLOW, this->clicked_row == i && this->clicked_dropdown, this->editable);
00274         } else {
00275           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);
00276         }
00277         SetDParam(2, STR_JUST_INT);
00278         SetDParam(3, current_value);
00279         if (par_info->value_names.Contains(current_value)) {
00280           const char *label = GetGRFStringFromGRFText(par_info->value_names.Find(current_value)->second);
00281           if (label != NULL) {
00282             SetDParam(2, STR_JUST_RAW_STRING);
00283             SetDParamStr(3, label);
00284           }
00285         }
00286       }
00287 
00288       const char *name = GetGRFStringFromGRFText(par_info->name);
00289       if (name != NULL) {
00290         SetDParam(0, STR_JUST_RAW_STRING);
00291         SetDParamStr(1, name);
00292       } else {
00293         SetDParam(0, STR_NEWGRF_PARAMETERS_DEFAULT_NAME);
00294         SetDParam(1, i + 1);
00295       }
00296 
00297       DrawString(text_left, text_right, y + WD_MATRIX_TOP, STR_NEWGRF_PARAMETERS_SETTING, selected ? TC_WHITE : TC_LIGHT_BLUE);
00298       y += this->line_height;
00299     }
00300   }
00301 
00302   virtual void OnPaint()
00303   {
00304     if (this->closing_dropdown) {
00305       this->closing_dropdown = false;
00306       this->clicked_dropdown = false;
00307     }
00308     this->DrawWidgets();
00309   }
00310 
00311   virtual void OnClick(Point pt, int widget, int click_count)
00312   {
00313     switch (widget) {
00314       case WID_NP_NUMPAR_DEC:
00315         if (this->editable && !this->action14present && this->grf_config->num_params > 0) {
00316           this->grf_config->num_params--;
00317           this->InvalidateData();
00318           SetWindowDirty(WC_GAME_OPTIONS, WN_GAME_OPTIONS_NEWGRF_STATE);
00319         }
00320         break;
00321 
00322       case WID_NP_NUMPAR_INC: {
00323         GRFConfig *c = this->grf_config;
00324         if (this->editable && !this->action14present && c->num_params < c->num_valid_params) {
00325           c->param[c->num_params++] = 0;
00326           this->InvalidateData();
00327           SetWindowDirty(WC_GAME_OPTIONS, WN_GAME_OPTIONS_NEWGRF_STATE);
00328         }
00329         break;
00330       }
00331 
00332       case WID_NP_BACKGROUND: {
00333         if (!this->editable) break;
00334         uint num = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_NP_BACKGROUND);
00335         if (num >= this->vscroll->GetCount()) break;
00336         if (this->clicked_row != num) {
00337           DeleteChildWindows(WC_QUERY_STRING);
00338           HideDropDownMenu(this);
00339           this->clicked_row = num;
00340           this->clicked_dropdown = false;
00341         }
00342 
00343         const NWidgetBase *wid = this->GetWidget<NWidgetBase>(WID_NP_BACKGROUND);
00344         int x = pt.x - wid->pos_x;
00345         if (_current_text_dir == TD_RTL) x = wid->current_x - 1 - x;
00346         x -= 4;
00347 
00348         GRFParameterInfo *par_info = (num < this->grf_config->param_info.Length()) ? this->grf_config->param_info[num] : NULL;
00349         if (par_info == NULL) par_info = GetDummyParameterInfo(num);
00350 
00351         /* One of the arrows is clicked */
00352         uint32 old_val = par_info->GetValue(this->grf_config);
00353         if (par_info->type != PTYPE_BOOL && IsInsideMM(x, 0, SETTING_BUTTON_WIDTH) && par_info->complete_labels) {
00354           if (this->clicked_dropdown) {
00355             /* unclick the dropdown */
00356             HideDropDownMenu(this);
00357             this->clicked_dropdown = false;
00358             this->closing_dropdown = false;
00359           } else {
00360             const NWidgetBase *wid = this->GetWidget<NWidgetBase>(WID_NP_BACKGROUND);
00361             int rel_y = (pt.y - (int)wid->pos_y) % this->line_height;
00362 
00363             Rect wi_rect;
00364             wi_rect.left = pt.x - (_current_text_dir == TD_RTL ? SETTING_BUTTON_WIDTH - 1 - x : x);;
00365             wi_rect.right = wi_rect.left + SETTING_BUTTON_WIDTH - 1;
00366             wi_rect.top = pt.y - rel_y + (this->line_height - SETTING_BUTTON_HEIGHT) / 2;
00367             wi_rect.bottom = wi_rect.top + SETTING_BUTTON_HEIGHT - 1;
00368 
00369             /* For dropdowns we also have to check the y position thoroughly, the mouse may not above the just opening dropdown */
00370             if (pt.y >= wi_rect.top && pt.y <= wi_rect.bottom) {
00371               this->clicked_dropdown = true;
00372               this->closing_dropdown = false;
00373 
00374               DropDownList *list = new DropDownList();
00375               for (uint32 i = par_info->min_value; i <= par_info->max_value; i++) {
00376                 list->push_back(new DropDownListCharStringItem(GetGRFStringFromGRFText(par_info->value_names.Find(i)->second), i, false));
00377               }
00378 
00379               ShowDropDownListAt(this, list, old_val, -1, wi_rect, COLOUR_ORANGE, true);
00380             }
00381           }
00382         } else if (IsInsideMM(x, 0, SETTING_BUTTON_WIDTH)) {
00383           uint32 val = old_val;
00384           if (par_info->type == PTYPE_BOOL) {
00385             val = !val;
00386           } else {
00387             if (x >= SETTING_BUTTON_WIDTH / 2) {
00388               /* Increase button clicked */
00389               if (val < par_info->max_value) val++;
00390               this->clicked_increase = true;
00391             } else {
00392               /* Decrease button clicked */
00393               if (val > par_info->min_value) val--;
00394               this->clicked_increase = false;
00395             }
00396           }
00397           if (val != old_val) {
00398             par_info->SetValue(this->grf_config, val);
00399 
00400             this->clicked_button = num;
00401             this->timeout = 5;
00402           }
00403         } else if (par_info->type == PTYPE_UINT_ENUM && !par_info->complete_labels && click_count >= 2) {
00404           /* Display a query box so users can enter a custom value. */
00405           SetDParam(0, old_val);
00406           ShowQueryString(STR_JUST_INT, STR_CONFIG_SETTING_QUERY_CAPTION, 10, this, CS_NUMERAL, QSF_NONE);
00407         }
00408         this->SetDirty();
00409         break;
00410       }
00411 
00412       case WID_NP_RESET:
00413         if (!this->editable) break;
00414         this->grf_config->SetParameterDefaults();
00415         this->InvalidateData();
00416         SetWindowDirty(WC_GAME_OPTIONS, WN_GAME_OPTIONS_NEWGRF_STATE);
00417         break;
00418 
00419       case WID_NP_ACCEPT:
00420         delete this;
00421         break;
00422     }
00423   }
00424 
00425   virtual void OnQueryTextFinished(char *str)
00426   {
00427     if (StrEmpty(str)) return;
00428     int32 value = atoi(str);
00429     GRFParameterInfo *par_info = ((uint)this->clicked_row < this->grf_config->param_info.Length()) ? this->grf_config->param_info[this->clicked_row] : NULL;
00430     if (par_info == NULL) par_info = GetDummyParameterInfo(this->clicked_row);
00431     uint32 val = Clamp<uint32>(value, par_info->min_value, par_info->max_value);
00432     par_info->SetValue(this->grf_config, val);
00433     this->SetDirty();
00434   }
00435 
00436   virtual void OnDropdownSelect(int widget, int index)
00437   {
00438     assert(this->clicked_dropdown);
00439     GRFParameterInfo *par_info = ((uint)this->clicked_row < this->grf_config->param_info.Length()) ? this->grf_config->param_info[this->clicked_row] : NULL;
00440     if (par_info == NULL) par_info = GetDummyParameterInfo(this->clicked_row);
00441     par_info->SetValue(this->grf_config, index);
00442     this->SetDirty();
00443   }
00444 
00445   virtual void OnDropdownClose(Point pt, int widget, int index, bool instant_close)
00446   {
00447     /* We cannot raise the dropdown button just yet. OnClick needs some hint, whether
00448      * the same dropdown button was clicked again, and then not open the dropdown again.
00449      * So, we only remember that it was closed, and process it on the next OnPaint, which is
00450      * after OnClick. */
00451     assert(this->clicked_dropdown);
00452     this->closing_dropdown = true;
00453     this->SetDirty();
00454   }
00455 
00456   virtual void OnResize()
00457   {
00458     NWidgetCore *nwi = this->GetWidget<NWidgetCore>(WID_NP_BACKGROUND);
00459     this->vscroll->SetCapacity(nwi->current_y / this->line_height);
00460     nwi->widget_data = (this->vscroll->GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
00461   }
00462 
00468   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
00469   {
00470     if (!gui_scope) return;
00471     if (!this->action14present) {
00472       this->SetWidgetDisabledState(WID_NP_NUMPAR_DEC, !this->editable || this->grf_config->num_params == 0);
00473       this->SetWidgetDisabledState(WID_NP_NUMPAR_INC, !this->editable || this->grf_config->num_params >= this->grf_config->num_valid_params);
00474     }
00475 
00476     this->vscroll->SetCount(this->action14present ? this->grf_config->num_valid_params : this->grf_config->num_params);
00477     if (this->clicked_row != UINT_MAX && this->clicked_row >= this->vscroll->GetCount()) {
00478       this->clicked_row = UINT_MAX;
00479       DeleteChildWindows(WC_QUERY_STRING);
00480     }
00481   }
00482 
00483   virtual void OnTick()
00484   {
00485     if (--this->timeout == 0) {
00486       this->clicked_button = UINT_MAX;
00487       this->SetDirty();
00488     }
00489   }
00490 };
00491 GRFParameterInfo NewGRFParametersWindow::dummy_parameter_info(0);
00492 
00493 
00494 static const NWidgetPart _nested_newgrf_parameter_widgets[] = {
00495   NWidget(NWID_HORIZONTAL),
00496     NWidget(WWT_CLOSEBOX, COLOUR_MAUVE),
00497     NWidget(WWT_CAPTION, COLOUR_MAUVE), SetDataTip(STR_NEWGRF_PARAMETERS_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00498   EndContainer(),
00499   NWidget(NWID_SELECTION, INVALID_COLOUR, WID_NP_SHOW_NUMPAR),
00500     NWidget(WWT_PANEL, COLOUR_MAUVE), SetResize(1, 0), SetFill(1, 0), SetPIP(4, 0, 4),
00501       NWidget(NWID_HORIZONTAL), SetPIP(4, 0, 4),
00502         NWidget(WWT_PUSHARROWBTN, COLOUR_YELLOW, WID_NP_NUMPAR_DEC), SetMinimalSize(12, 12), SetDataTip(AWV_DECREASE, STR_NULL),
00503         NWidget(WWT_PUSHARROWBTN, COLOUR_YELLOW, WID_NP_NUMPAR_INC), SetMinimalSize(12, 12), SetDataTip(AWV_INCREASE, STR_NULL),
00504         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),
00505       EndContainer(),
00506     EndContainer(),
00507   EndContainer(),
00508   NWidget(NWID_HORIZONTAL),
00509     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),
00510     NWidget(NWID_VSCROLLBAR, COLOUR_MAUVE, WID_NP_SCROLLBAR),
00511   EndContainer(),
00512   NWidget(NWID_SELECTION, INVALID_COLOUR, WID_NP_SHOW_DESCRIPTION),
00513     NWidget(WWT_PANEL, COLOUR_MAUVE, WID_NP_DESCRIPTION), SetResize(1, 0), SetFill(1, 0),
00514     EndContainer(),
00515   EndContainer(),
00516   NWidget(NWID_HORIZONTAL),
00517     NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
00518       NWidget(WWT_PUSHTXTBTN, COLOUR_MAUVE, WID_NP_ACCEPT), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_NEWGRF_PARAMETERS_CLOSE, STR_NULL),
00519       NWidget(WWT_PUSHTXTBTN, COLOUR_MAUVE, WID_NP_RESET), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_NEWGRF_PARAMETERS_RESET, STR_NEWGRF_PARAMETERS_RESET_TOOLTIP),
00520     EndContainer(),
00521     NWidget(WWT_RESIZEBOX, COLOUR_MAUVE),
00522   EndContainer(),
00523 };
00524 
00526 static const WindowDesc _newgrf_parameters_desc(
00527   WDP_CENTER, 500, 208,
00528   WC_GRF_PARAMETERS, WC_NONE,
00529   WDF_UNCLICK_BUTTONS,
00530   _nested_newgrf_parameter_widgets, lengthof(_nested_newgrf_parameter_widgets)
00531 );
00532 
00533 static void OpenGRFParameterWindow(GRFConfig *c, bool editable)
00534 {
00535   DeleteWindowByClass(WC_GRF_PARAMETERS);
00536   new NewGRFParametersWindow(&_newgrf_parameters_desc, c, editable);
00537 }
00538 
00540 struct NewGRFTextfileWindow : public TextfileWindow {
00541   const GRFConfig *grf_config; 
00542 
00543   NewGRFTextfileWindow(TextfileType file_type, const GRFConfig *c) : TextfileWindow(file_type), grf_config(c)
00544   {
00545     const char *textfile = this->grf_config->GetTextfile(file_type);
00546     this->LoadTextfile(textfile, NEWGRF_DIR);
00547   }
00548 
00549   /* virtual */ void SetStringParameters(int widget) const
00550   {
00551     if (widget == WID_TF_CAPTION) {
00552       SetDParam(0, STR_CONTENT_TYPE_NEWGRF);
00553       SetDParamStr(1, this->grf_config->GetName());
00554     }
00555   }
00556 };
00557 
00558 void ShowNewGRFTextfileWindow(TextfileType file_type, const GRFConfig *c)
00559 {
00560   DeleteWindowByClass(WC_TEXTFILE);
00561   new NewGRFTextfileWindow(file_type, c);
00562 }
00563 
00564 static GRFPresetList _grf_preset_list;
00565 
00566 class DropDownListPresetItem : public DropDownListItem {
00567 public:
00568   DropDownListPresetItem(int result) : DropDownListItem(result, false) {}
00569 
00570   virtual ~DropDownListPresetItem() {}
00571 
00572   bool Selectable() const
00573   {
00574     return true;
00575   }
00576 
00577   void Draw(int left, int right, int top, int bottom, bool sel, int bg_colour) const
00578   {
00579     DrawString(left + 2, right + 2, top, _grf_preset_list[this->result], sel ? TC_WHITE : TC_BLACK);
00580   }
00581 };
00582 
00583 static void NewGRFConfirmationCallback(Window *w, bool confirmed);
00584 
00588 struct NewGRFWindow : public QueryStringBaseWindow, NewGRFScanCallback {
00589   typedef GUIList<const GRFConfig *, StringFilter &> GUIGRFConfigList;
00590 
00591   static const uint EDITBOX_MAX_SIZE   =  50;
00592 
00593   static Listing   last_sorting;   
00594   static Filtering last_filtering; 
00595   static GUIGRFConfigList::SortFunction   * const sorter_funcs[]; 
00596   static GUIGRFConfigList::FilterFunction * const filter_funcs[]; 
00597 
00598   GUIGRFConfigList avails;    
00599   const GRFConfig *avail_sel; 
00600   int avail_pos;              
00601   StringFilter string_filter; 
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     string_filter.SetFilterTerm(this->edit_str_buf);
01301     this->avails.SetFilterState(!string_filter.IsEmpty());
01302     this->avails.ForceRebuild();
01303     this->InvalidateData(0);
01304   }
01305 
01306   virtual void OnDragDrop(Point pt, int widget)
01307   {
01308     if (!this->editable) return;
01309 
01310     if (widget == WID_NS_FILE_LIST) {
01311       if (this->active_sel != NULL) {
01312         /* Get pointer to the selected file in the active list. */
01313         int from_pos = 0;
01314         GRFConfig **from_prev;
01315         for (from_prev = &this->actives; *from_prev != this->active_sel; from_prev = &(*from_prev)->next, from_pos++) {}
01316 
01317         /* Gets the drag-and-drop destination offset. Ignore the last dummy line. */
01318         int to_pos = min(this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_NS_FILE_LIST), this->vscroll->GetCount() - 2);
01319         if (to_pos != from_pos) { // Don't move NewGRF file over itself.
01320           /* Get pointer to destination position. */
01321           GRFConfig **to_prev = &this->actives;
01322           for (int i = from_pos < to_pos ? -1 : 0; *to_prev != NULL && i < to_pos; to_prev = &(*to_prev)->next, i++) {}
01323 
01324           /* Detach NewGRF file from its original position. */
01325           *from_prev = this->active_sel->next;
01326 
01327           /* Attach NewGRF file to its new position. */
01328           this->active_sel->next = *to_prev;
01329           *to_prev = this->active_sel;
01330 
01331           this->vscroll->ScrollTowards(to_pos);
01332           this->preset = -1;
01333           this->InvalidateData();
01334         }
01335       } else if (this->avail_sel != NULL) {
01336         int to_pos = min(this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_NS_FILE_LIST), this->vscroll->GetCount() - 1);
01337         this->AddGRFToActive(to_pos);
01338       }
01339     } else if (widget == WID_NS_AVAIL_LIST && this->active_sel != NULL) {
01340       /* Remove active NewGRF file by dragging it over available list. */
01341       Point dummy = {-1, -1};
01342       this->OnClick(dummy, WID_NS_REMOVE, 1);
01343     }
01344 
01345     ResetObjectToPlace();
01346 
01347     if (this->active_over != -1) {
01348       /* End of drag-and-drop, hide dragged destination highlight. */
01349       this->SetWidgetDirty(this->active_over == -2 ? WID_NS_AVAIL_LIST : WID_NS_FILE_LIST);
01350       this->active_over = -1;
01351     }
01352   }
01353 
01354   virtual void OnMouseDrag(Point pt, int widget)
01355   {
01356     if (!this->editable) return;
01357 
01358     if (widget == WID_NS_FILE_LIST && (this->active_sel != NULL || this->avail_sel != NULL)) {
01359       /* An NewGRF file is dragged over the active list. */
01360       int to_pos = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_NS_FILE_LIST);
01361       /* Skip the last dummy line if the source is from the active list. */
01362       to_pos = min(to_pos, this->vscroll->GetCount() - (this->active_sel != NULL ? 2 : 1));
01363 
01364       if (to_pos != this->active_over) {
01365         this->active_over = to_pos;
01366         this->SetWidgetDirty(WID_NS_FILE_LIST);
01367       }
01368     } else if (widget == WID_NS_AVAIL_LIST && this->active_sel != NULL) {
01369       this->active_over = -2;
01370       this->SetWidgetDirty(WID_NS_AVAIL_LIST);
01371     } else if (this->active_over != -1) {
01372       this->SetWidgetDirty(this->active_over == -2 ? WID_NS_AVAIL_LIST : WID_NS_FILE_LIST);
01373       this->active_over = -1;
01374     }
01375   }
01376 
01377 private:
01379   static int CDECL NameSorter(const GRFConfig * const *a, const GRFConfig * const *b)
01380   {
01381     int i = strnatcmp((*a)->GetName(), (*b)->GetName()); // Sort by name (natural sorting).
01382     if (i != 0) return i;
01383 
01384     i = (*a)->version - (*b)->version;
01385     if (i != 0) return i;
01386 
01387     return memcmp((*a)->ident.md5sum, (*b)->ident.md5sum, lengthof((*b)->ident.md5sum));
01388   }
01389 
01391   static bool CDECL TagNameFilter(const GRFConfig * const *a, StringFilter &filter)
01392   {
01393     filter.ResetState();
01394     filter.AddLine((*a)->GetName());
01395     filter.AddLine((*a)->filename);
01396     filter.AddLine((*a)->GetDescription());
01397     return filter.GetState();;
01398   }
01399 
01400   void BuildAvailables()
01401   {
01402     if (!this->avails.NeedRebuild()) return;
01403 
01404     this->avails.Clear();
01405 
01406     for (const GRFConfig *c = _all_grfs; c != NULL; c = c->next) {
01407       bool found = false;
01408       for (const GRFConfig *grf = this->actives; grf != NULL && !found; grf = grf->next) found = grf->ident.HasGrfIdentifier(c->ident.grfid, c->ident.md5sum);
01409       if (found) continue;
01410 
01411       if (_settings_client.gui.newgrf_show_old_versions) {
01412         *this->avails.Append() = c;
01413       } else {
01414         const GRFConfig *best = FindGRFConfig(c->ident.grfid, HasBit(c->flags, GCF_INVALID) ? FGCM_NEWEST : FGCM_NEWEST_VALID);
01415         /*
01416          * If the best version is 0, then all NewGRF with this GRF ID
01417          * have version 0, so for backward compatability reasons we
01418          * want to show them all.
01419          * If we are the best version, then we definitely want to
01420          * show that NewGRF!.
01421          */
01422         if (best->version == 0 || best->ident.HasGrfIdentifier(c->ident.grfid, c->ident.md5sum)) {
01423           *this->avails.Append() = c;
01424         }
01425       }
01426     }
01427 
01428     this->avails.Filter(this->string_filter);
01429     this->avails.Compact();
01430     this->avails.RebuildDone();
01431     this->avails.Sort();
01432 
01433     if (this->avail_sel != NULL) {
01434       this->avail_pos = this->avails.FindIndex(this->avail_sel);
01435       if (this->avail_pos < 0) this->avail_sel = NULL;
01436     }
01437 
01438     this->vscroll2->SetCount(this->avails.Length()); // Update the scrollbar
01439   }
01440 
01446   bool AddGRFToActive(int ins_pos = -1)
01447   {
01448     if (this->avail_sel == NULL || !this->editable || HasBit(this->avail_sel->flags, GCF_INVALID)) return false;
01449 
01450     GRFConfig **entry = NULL;
01451     GRFConfig **list;
01452     /* Find last entry in the list, checking for duplicate grfid on the way */
01453     for (list = &this->actives; *list != NULL; list = &(*list)->next, ins_pos--) {
01454       if (ins_pos == 0) entry = list; // Insert position? Save.
01455       if ((*list)->ident.grfid == this->avail_sel->ident.grfid) {
01456         ShowErrorMessage(STR_NEWGRF_DUPLICATE_GRFID, INVALID_STRING_ID, WL_INFO);
01457         return false;
01458       }
01459     }
01460     if (entry == NULL) entry = list;
01461 
01462     GRFConfig *c = new GRFConfig(*this->avail_sel); // Copy GRF details from scanned list.
01463     c->SetParameterDefaults();
01464 
01465     /* Insert GRF config to configuration list. */
01466     c->next = *entry;
01467     *entry = c;
01468 
01469     /* Select next (or previous, if last one) item in the list. */
01470     int new_pos = this->avail_pos + 1;
01471     if (new_pos >= (int)this->avails.Length()) new_pos = this->avail_pos - 1;
01472     this->avail_pos = new_pos;
01473     if (new_pos >= 0) this->avail_sel = this->avails[new_pos];
01474 
01475     this->avails.ForceRebuild();
01476     this->InvalidateData(GOID_NEWGRF_LIST_EDITED);
01477     return true;
01478   }
01479 };
01480 
01481 #if defined(ENABLE_NETWORK)
01482 
01486 void ShowMissingContentWindow(const GRFConfig *list)
01487 {
01488   /* Only show the things in the current list, or everything when nothing's selected */
01489   ContentVector cv;
01490   for (const GRFConfig *c = list; c != NULL; c = c->next) {
01491     if (c->status != GCS_NOT_FOUND && !HasBit(c->flags, GCF_COMPATIBLE)) continue;
01492 
01493     ContentInfo *ci = new ContentInfo();
01494     ci->type = CONTENT_TYPE_NEWGRF;
01495     ci->state = ContentInfo::DOES_NOT_EXIST;
01496     ttd_strlcpy(ci->name, c->GetName(), lengthof(ci->name));
01497     ci->unique_id = BSWAP32(c->ident.grfid);
01498     memcpy(ci->md5sum, HasBit(c->flags, GCF_COMPATIBLE) ? c->original_md5sum : c->ident.md5sum, sizeof(ci->md5sum));
01499     *cv.Append() = ci;
01500   }
01501   ShowNetworkContentListWindow(cv.Length() == 0 ? NULL : &cv, CONTENT_TYPE_NEWGRF);
01502 }
01503 #endif
01504 
01505 Listing NewGRFWindow::last_sorting     = {false, 0};
01506 Filtering NewGRFWindow::last_filtering = {false, 0};
01507 
01508 NewGRFWindow::GUIGRFConfigList::SortFunction * const NewGRFWindow::sorter_funcs[] = {
01509   &NameSorter,
01510 };
01511 
01512 NewGRFWindow::GUIGRFConfigList::FilterFunction * const NewGRFWindow::filter_funcs[] = {
01513   &TagNameFilter,
01514 };
01515 
01522 class NWidgetNewGRFDisplay : public NWidgetContainer {
01523 public:
01524   static const uint INTER_LIST_SPACING;      
01525   static const uint INTER_COLUMN_SPACING;    
01526   static const uint MAX_EXTRA_INFO_WIDTH;    
01527   static const uint MIN_EXTRA_FOR_3_COLUMNS; 
01528 
01529   NWidgetBase *avs; 
01530   NWidgetBase *acs; 
01531   NWidgetBase *inf; 
01532   bool editable;    
01533 
01534   NWidgetNewGRFDisplay(NWidgetBase *avs, NWidgetBase *acs, NWidgetBase *inf) : NWidgetContainer(NWID_HORIZONTAL)
01535   {
01536     this->avs = avs;
01537     this->acs = acs;
01538     this->inf = inf;
01539 
01540     this->Add(this->avs);
01541     this->Add(this->acs);
01542     this->Add(this->inf);
01543 
01544     this->editable = true; // Temporary setting, 'real' value is set in SetupSmallestSize().
01545   }
01546 
01547   virtual void SetupSmallestSize(Window *w, bool init_array)
01548   {
01549     /* Copy state flag from the window. */
01550     assert(dynamic_cast<NewGRFWindow *>(w) != NULL);
01551     NewGRFWindow *ngw = (NewGRFWindow *)w;
01552     this->editable = ngw->editable;
01553 
01554     this->avs->SetupSmallestSize(w, init_array);
01555     this->acs->SetupSmallestSize(w, init_array);
01556     this->inf->SetupSmallestSize(w, init_array);
01557 
01558     uint min_avs_width = this->avs->smallest_x + this->avs->padding_left + this->avs->padding_right;
01559     uint min_acs_width = this->acs->smallest_x + this->acs->padding_left + this->acs->padding_right;
01560     uint min_inf_width = this->inf->smallest_x + this->inf->padding_left + this->inf->padding_right;
01561 
01562     uint min_avs_height = this->avs->smallest_y + this->avs->padding_top + this->avs->padding_bottom;
01563     uint min_acs_height = this->acs->smallest_y + this->acs->padding_top + this->acs->padding_bottom;
01564     uint min_inf_height = this->inf->smallest_y + this->inf->padding_top + this->inf->padding_bottom;
01565 
01566     /* Smallest window is in two column mode. */
01567     this->smallest_x = max(min_avs_width, min_acs_width) + INTER_COLUMN_SPACING + min_inf_width;
01568     this->smallest_y = max(min_inf_height, min_acs_height + INTER_LIST_SPACING + min_avs_height);
01569 
01570     /* Filling. */
01571     this->fill_x = LeastCommonMultiple(this->avs->fill_x, this->acs->fill_x);
01572     if (this->inf->fill_x > 0 && (this->fill_x == 0 || this->fill_x > this->inf->fill_x)) this->fill_x = this->inf->fill_x;
01573 
01574     this->fill_y = this->avs->fill_y;
01575     if (this->acs->fill_y > 0 && (this->fill_y == 0 || this->fill_y > this->acs->fill_y)) this->fill_y = this->acs->fill_y;
01576     this->fill_y = LeastCommonMultiple(this->fill_y, this->inf->fill_y);
01577 
01578     /* Resizing. */
01579     this->resize_x = LeastCommonMultiple(this->avs->resize_x, this->acs->resize_x);
01580     if (this->inf->resize_x > 0 && (this->resize_x == 0 || this->resize_x > this->inf->resize_x)) this->resize_x = this->inf->resize_x;
01581 
01582     this->resize_y = this->avs->resize_y;
01583     if (this->acs->resize_y > 0 && (this->resize_y == 0 || this->resize_y > this->acs->resize_y)) this->resize_y = this->acs->resize_y;
01584     this->resize_y = LeastCommonMultiple(this->resize_y, this->inf->resize_y);
01585 
01586     /* Make sure the height suits the 3 column (resp. not-editable) format; the 2 column format can easily fill space between the lists */
01587     this->smallest_y = ComputeMaxSize(min_acs_height, this->smallest_y + this->resize_y - 1, this->resize_y);
01588   }
01589 
01590   virtual void AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)
01591   {
01592     this->StoreSizePosition(sizing, x, y, given_width, given_height);
01593 
01594     uint min_avs_width = this->avs->smallest_x + this->avs->padding_left + this->avs->padding_right;
01595     uint min_acs_width = this->acs->smallest_x + this->acs->padding_left + this->acs->padding_right;
01596     uint min_inf_width = this->inf->smallest_x + this->inf->padding_left + this->inf->padding_right;
01597 
01598     uint min_list_width = max(min_avs_width, min_acs_width); // Smallest width of the lists such that they have equal width (incl padding).
01599     uint avs_extra_width = min_list_width - min_avs_width;   // Additional width needed for avs to reach min_list_width.
01600     uint acs_extra_width = min_list_width - min_acs_width;   // Additional width needed for acs to reach min_list_width.
01601 
01602     /* Use 2 or 3 colmuns? */
01603     uint min_three_columns = min_avs_width + min_acs_width + min_inf_width + 2 * INTER_COLUMN_SPACING;
01604     uint min_two_columns   = min_list_width + min_inf_width + INTER_COLUMN_SPACING;
01605     bool use_three_columns = this->editable && (min_three_columns + MIN_EXTRA_FOR_3_COLUMNS <= given_width);
01606 
01607     /* Info panel is a seperate column in both modes. Compute its width first. */
01608     uint extra_width, inf_width;
01609     if (use_three_columns) {
01610       extra_width = given_width - min_three_columns;
01611       inf_width = min(MAX_EXTRA_INFO_WIDTH, extra_width / 2);
01612     } else {
01613       extra_width = given_width - min_two_columns;
01614       inf_width = min(MAX_EXTRA_INFO_WIDTH, extra_width / 2);
01615     }
01616     inf_width = ComputeMaxSize(this->inf->smallest_x, this->inf->smallest_x + inf_width, this->inf->GetHorizontalStepSize(sizing));
01617     extra_width -= inf_width - this->inf->smallest_x;
01618 
01619     uint inf_height = ComputeMaxSize(this->inf->smallest_y, given_height, this->inf->GetVerticalStepSize(sizing));
01620 
01621     if (use_three_columns) {
01622       /* Three column display, first make both lists equally wide, then divide whatever is left between both lists.
01623        * Only keep track of what avs gets, all other space goes to acs. */
01624       uint avs_width = min(avs_extra_width, extra_width);
01625       extra_width -= avs_width;
01626       extra_width -= min(acs_extra_width, extra_width);
01627       avs_width += extra_width / 2;
01628 
01629       avs_width = ComputeMaxSize(this->avs->smallest_x, this->avs->smallest_x + avs_width, this->avs->GetHorizontalStepSize(sizing));
01630 
01631       uint acs_width = given_width - // Remaining space, including horizontal padding.
01632           inf_width - this->inf->padding_left - this->inf->padding_right -
01633           avs_width - this->avs->padding_left - this->avs->padding_right - 2 * INTER_COLUMN_SPACING;
01634       acs_width = ComputeMaxSize(min_acs_width, acs_width, this->acs->GetHorizontalStepSize(sizing)) -
01635           this->acs->padding_left - this->acs->padding_right;
01636 
01637       /* Never use fill_y on these; the minimal size is choosen, so that the 3 column view looks nice */
01638       uint avs_height = ComputeMaxSize(this->avs->smallest_y, given_height, this->avs->resize_y);
01639       uint acs_height = ComputeMaxSize(this->acs->smallest_y, given_height, this->acs->resize_y);
01640 
01641       /* Assign size and position to the childs. */
01642       if (rtl) {
01643         x += this->inf->padding_left;
01644         this->inf->AssignSizePosition(sizing, x, y + this->inf->padding_top, inf_width, inf_height, rtl);
01645         x += inf_width + this->inf->padding_right + INTER_COLUMN_SPACING;
01646       } else {
01647         x += this->avs->padding_left;
01648         this->avs->AssignSizePosition(sizing, x, y + this->avs->padding_top, avs_width, avs_height, rtl);
01649         x += avs_width + this->avs->padding_right + INTER_COLUMN_SPACING;
01650       }
01651 
01652       x += this->acs->padding_left;
01653       this->acs->AssignSizePosition(sizing, x, y + this->acs->padding_top, acs_width, acs_height, rtl);
01654       x += acs_width + this->acs->padding_right + INTER_COLUMN_SPACING;
01655 
01656       if (rtl) {
01657         x += this->avs->padding_left;
01658         this->avs->AssignSizePosition(sizing, x, y + this->avs->padding_top, avs_width, avs_height, rtl);
01659       } else {
01660         x += this->inf->padding_left;
01661         this->inf->AssignSizePosition(sizing, x, y + this->inf->padding_top, inf_width, inf_height, rtl);
01662       }
01663     } else {
01664       /* Two columns, all space in extra_width goes to both lists. Since the lists are underneath each other,
01665        * the column is min_list_width wide at least. */
01666       uint avs_width = ComputeMaxSize(this->avs->smallest_x, this->avs->smallest_x + avs_extra_width + extra_width,
01667           this->avs->GetHorizontalStepSize(sizing));
01668       uint acs_width = ComputeMaxSize(this->acs->smallest_x, this->acs->smallest_x + acs_extra_width + extra_width,
01669           this->acs->GetHorizontalStepSize(sizing));
01670 
01671       uint min_avs_height = (!this->editable) ? 0 : this->avs->smallest_y + this->avs->padding_top + this->avs->padding_bottom + INTER_LIST_SPACING;
01672       uint min_acs_height = this->acs->smallest_y + this->acs->padding_top + this->acs->padding_bottom;
01673       uint extra_height = given_height - min_acs_height - min_avs_height;
01674 
01675       /* Never use fill_y on these; instead use the INTER_LIST_SPACING as filler */
01676       uint avs_height = ComputeMaxSize(this->avs->smallest_y, this->avs->smallest_y + extra_height / 2, this->avs->resize_y);
01677       if (this->editable) extra_height -= avs_height - this->avs->smallest_y;
01678       uint acs_height = ComputeMaxSize(this->acs->smallest_y, this->acs->smallest_y + extra_height, this->acs->resize_y);
01679 
01680       /* Assign size and position to the childs. */
01681       if (rtl) {
01682         x += this->inf->padding_left;
01683         this->inf->AssignSizePosition(sizing, x, y + this->inf->padding_top, inf_width, inf_height, rtl);
01684         x += inf_width + this->inf->padding_right + INTER_COLUMN_SPACING;
01685 
01686         this->acs->AssignSizePosition(sizing, x + this->acs->padding_left, y + this->acs->padding_top, acs_width, acs_height, rtl);
01687         if (this->editable) {
01688           this->avs->AssignSizePosition(sizing, x + this->avs->padding_left, y + given_height - avs_height - this->avs->padding_bottom, avs_width, avs_height, rtl);
01689         } else {
01690           this->avs->AssignSizePosition(sizing, 0, 0, this->avs->smallest_x, this->avs->smallest_y, rtl);
01691         }
01692       } else {
01693         this->acs->AssignSizePosition(sizing, x + this->acs->padding_left, y + this->acs->padding_top, acs_width, acs_height, rtl);
01694         if (this->editable) {
01695           this->avs->AssignSizePosition(sizing, x + this->avs->padding_left, y + given_height - avs_height - this->avs->padding_bottom, avs_width, avs_height, rtl);
01696         } else {
01697           this->avs->AssignSizePosition(sizing, 0, 0, this->avs->smallest_x, this->avs->smallest_y, rtl);
01698         }
01699         uint dx = this->acs->current_x + this->acs->padding_left + this->acs->padding_right;
01700         if (this->editable) {
01701           dx = max(dx, this->avs->current_x + this->avs->padding_left + this->avs->padding_right);
01702         }
01703         x += dx + INTER_COLUMN_SPACING + this->inf->padding_left;
01704         this->inf->AssignSizePosition(sizing, x, y + this->inf->padding_top, inf_width, inf_height, rtl);
01705       }
01706     }
01707   }
01708 
01709   virtual NWidgetCore *GetWidgetFromPos(int x, int y)
01710   {
01711     if (!IsInsideBS(x, this->pos_x, this->current_x) || !IsInsideBS(y, this->pos_y, this->current_y)) return NULL;
01712 
01713     NWidgetCore *nw = (this->editable) ? this->avs->GetWidgetFromPos(x, y) : NULL;
01714     if (nw == NULL) nw = this->acs->GetWidgetFromPos(x, y);
01715     if (nw == NULL) nw = this->inf->GetWidgetFromPos(x, y);
01716     return nw;
01717   }
01718 
01719   virtual void Draw(const Window *w)
01720   {
01721     if (this->editable) this->avs->Draw(w);
01722     this->acs->Draw(w);
01723     this->inf->Draw(w);
01724   }
01725 };
01726 
01727 const uint NWidgetNewGRFDisplay::INTER_LIST_SPACING      = WD_RESIZEBOX_WIDTH + 1;
01728 const uint NWidgetNewGRFDisplay::INTER_COLUMN_SPACING    = WD_RESIZEBOX_WIDTH;
01729 const uint NWidgetNewGRFDisplay::MAX_EXTRA_INFO_WIDTH    = 150;
01730 const uint NWidgetNewGRFDisplay::MIN_EXTRA_FOR_3_COLUMNS = 50;
01731 
01732 static const NWidgetPart _nested_newgrf_actives_widgets[] = {
01733   /* Left side, presets. */
01734   NWidget(NWID_HORIZONTAL),
01735     NWidget(WWT_TEXT, COLOUR_MAUVE), SetDataTip(STR_NEWGRF_SETTINGS_SELECT_PRESET, STR_NULL),
01736         SetPadding(0, WD_FRAMETEXT_RIGHT, 0, 0),
01737     NWidget(WWT_DROPDOWN, COLOUR_YELLOW, WID_NS_PRESET_LIST), SetFill(1, 0), SetResize(1, 0),
01738         SetDataTip(STR_JUST_STRING, STR_NEWGRF_SETTINGS_PRESET_LIST_TOOLTIP),
01739   EndContainer(),
01740   NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
01741     NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_PRESET_SAVE), SetFill(1, 0), SetResize(1, 0),
01742         SetDataTip(STR_NEWGRF_SETTINGS_PRESET_SAVE, STR_NEWGRF_SETTINGS_PRESET_SAVE_TOOLTIP),
01743     NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_PRESET_DELETE), SetFill(1, 0), SetResize(1, 0),
01744         SetDataTip(STR_NEWGRF_SETTINGS_PRESET_DELETE, STR_NEWGRF_SETTINGS_PRESET_DELETE_TOOLTIP),
01745   EndContainer(),
01746 
01747   NWidget(NWID_SPACER), SetMinimalSize(0, WD_RESIZEBOX_WIDTH), SetResize(1, 0), SetFill(1, 0),
01748   NWidget(WWT_PANEL, COLOUR_MAUVE),
01749     NWidget(WWT_LABEL, COLOUR_MAUVE), SetDataTip(STR_NEWGRF_SETTINGS_ACTIVE_LIST, STR_NULL),
01750         SetFill(1, 0), SetResize(1, 0), SetPadding(3, WD_FRAMETEXT_RIGHT, 0, WD_FRAMETEXT_LEFT),
01751     /* Left side, active grfs. */
01752     NWidget(NWID_HORIZONTAL), SetPadding(0, 2, 0, 2),
01753       NWidget(WWT_PANEL, COLOUR_MAUVE),
01754         NWidget(WWT_INSET, COLOUR_MAUVE, WID_NS_FILE_LIST), SetMinimalSize(100, 1), SetPadding(2, 2, 2, 2),
01755             SetFill(1, 1), SetResize(1, 1), SetScrollbar(WID_NS_SCROLLBAR), SetDataTip(STR_NULL, STR_NEWGRF_SETTINGS_FILE_TOOLTIP),
01756         EndContainer(),
01757       EndContainer(),
01758       NWidget(NWID_VSCROLLBAR, COLOUR_MAUVE, WID_NS_SCROLLBAR),
01759     EndContainer(),
01760     /* Buttons. */
01761     NWidget(NWID_SELECTION, INVALID_COLOUR, WID_NS_SHOW_REMOVE),
01762       NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPadding(2, 2, 2, 2), SetPIP(0, WD_RESIZEBOX_WIDTH, 0),
01763         NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_REMOVE), SetFill(1, 0), SetResize(1, 0),
01764             SetDataTip(STR_NEWGRF_SETTINGS_REMOVE, STR_NEWGRF_SETTINGS_REMOVE_TOOLTIP),
01765         NWidget(NWID_VERTICAL),
01766           NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_MOVE_UP), SetFill(1, 0), SetResize(1, 0),
01767               SetDataTip(STR_NEWGRF_SETTINGS_MOVEUP, STR_NEWGRF_SETTINGS_MOVEUP_TOOLTIP),
01768           NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_MOVE_DOWN), SetFill(1, 0), SetResize(1, 0),
01769               SetDataTip(STR_NEWGRF_SETTINGS_MOVEDOWN, STR_NEWGRF_SETTINGS_MOVEDOWN_TOOLTIP),
01770         EndContainer(),
01771       EndContainer(),
01772 
01773       NWidget(NWID_VERTICAL, NC_EQUALSIZE), SetPadding(2, 2, 2, 2),
01774         NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_RESCAN_FILES2), SetFill(1, 0), SetResize(1, 0),
01775             SetDataTip(STR_NEWGRF_SETTINGS_RESCAN_FILES, STR_NEWGRF_SETTINGS_RESCAN_FILES_TOOLTIP),
01776         NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_CONTENT_DOWNLOAD2), SetFill(1, 0), SetResize(1, 0),
01777             SetDataTip(STR_INTRO_ONLINE_CONTENT, STR_INTRO_TOOLTIP_ONLINE_CONTENT),
01778       EndContainer(),
01779     EndContainer(),
01780   EndContainer(),
01781 };
01782 
01783 static const NWidgetPart _nested_newgrf_availables_widgets[] = {
01784   NWidget(WWT_PANEL, COLOUR_MAUVE),
01785     NWidget(WWT_LABEL, COLOUR_MAUVE), SetDataTip(STR_NEWGRF_SETTINGS_INACTIVE_LIST, STR_NULL),
01786         SetFill(1, 0), SetResize(1, 0), SetPadding(3, WD_FRAMETEXT_RIGHT, 0, WD_FRAMETEXT_LEFT),
01787     /* Left side, available grfs, filter edit box. */
01788     NWidget(NWID_HORIZONTAL), SetPadding(WD_TEXTPANEL_TOP, 0, WD_TEXTPANEL_BOTTOM, 0),
01789         SetPIP(WD_FRAMETEXT_LEFT, WD_FRAMETEXT_RIGHT, WD_FRAMETEXT_RIGHT),
01790       NWidget(WWT_TEXT, COLOUR_MAUVE), SetFill(0, 1), SetDataTip(STR_NEWGRF_FILTER_TITLE, STR_NULL),
01791       NWidget(WWT_EDITBOX, COLOUR_MAUVE, WID_NS_FILTER), SetFill(1, 0), SetMinimalSize(50, 12), SetResize(1, 0),
01792           SetDataTip(STR_LIST_FILTER_OSKTITLE, STR_LIST_FILTER_TOOLTIP),
01793     EndContainer(),
01794     /* Left side, available grfs. */
01795     NWidget(NWID_HORIZONTAL), SetPadding(0, 2, 0, 2),
01796       NWidget(WWT_PANEL, COLOUR_MAUVE),
01797         NWidget(WWT_INSET, COLOUR_MAUVE, WID_NS_AVAIL_LIST), SetMinimalSize(100, 1), SetPadding(2, 2, 2, 2),
01798             SetFill(1, 1), SetResize(1, 1), SetScrollbar(WID_NS_SCROLL2BAR),
01799         EndContainer(),
01800       EndContainer(),
01801       NWidget(NWID_VSCROLLBAR, COLOUR_MAUVE, WID_NS_SCROLL2BAR),
01802     EndContainer(),
01803     /* Left side, available grfs, buttons. */
01804     NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPadding(2, 2, 2, 2), SetPIP(0, WD_RESIZEBOX_WIDTH, 0),
01805       NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_ADD), SetFill(1, 0), SetResize(1, 0),
01806           SetDataTip(STR_NEWGRF_SETTINGS_ADD, STR_NEWGRF_SETTINGS_ADD_FILE_TOOLTIP),
01807       NWidget(NWID_VERTICAL),
01808         NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_RESCAN_FILES), SetFill(1, 0), SetResize(1, 0),
01809             SetDataTip(STR_NEWGRF_SETTINGS_RESCAN_FILES, STR_NEWGRF_SETTINGS_RESCAN_FILES_TOOLTIP),
01810         NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_CONTENT_DOWNLOAD), SetFill(1, 0), SetResize(1, 0),
01811             SetDataTip(STR_INTRO_ONLINE_CONTENT, STR_INTRO_TOOLTIP_ONLINE_CONTENT),
01812       EndContainer(),
01813     EndContainer(),
01814   EndContainer(),
01815 };
01816 
01817 static const NWidgetPart _nested_newgrf_infopanel_widgets[] = {
01818   /* Right side, info panel. */
01819   NWidget(NWID_VERTICAL), SetPadding(0, 0, 2, 0),
01820     NWidget(WWT_PANEL, COLOUR_MAUVE), SetPadding(0, 0, 2, 0),
01821       NWidget(WWT_EMPTY, COLOUR_MAUVE, WID_NS_NEWGRF_INFO_TITLE), SetFill(1, 0), SetResize(1, 0),
01822       NWidget(WWT_EMPTY, COLOUR_MAUVE, WID_NS_NEWGRF_INFO), SetFill(1, 1), SetResize(1, 1), SetMinimalSize(150, 100),
01823     EndContainer(),
01824     NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_OPEN_URL), SetFill(1, 0), SetResize(1, 0),
01825         SetDataTip(STR_CONTENT_OPEN_URL, STR_CONTENT_OPEN_URL_TOOLTIP),
01826     NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_NEWGRF_TEXTFILE + TFT_README), SetFill(1, 0), SetResize(1, 0),
01827         SetDataTip(STR_TEXTFILE_VIEW_README, STR_NULL),
01828     NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
01829       NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_NEWGRF_TEXTFILE + TFT_CHANGELOG), SetFill(1, 0), SetResize(1, 0),
01830           SetDataTip(STR_TEXTFILE_VIEW_CHANGELOG, STR_NULL),
01831       NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_NEWGRF_TEXTFILE + TFT_LICENSE), SetFill(1, 0), SetResize(1, 0),
01832           SetDataTip(STR_TEXTFILE_VIEW_LICENCE, STR_NULL),
01833     EndContainer(),
01834   EndContainer(),
01835   NWidget(NWID_SELECTION, INVALID_COLOUR, WID_NS_SHOW_APPLY),
01836     /* Right side, buttons. */
01837     NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(0, WD_RESIZEBOX_WIDTH, 0),
01838       NWidget(NWID_VERTICAL),
01839         NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_SET_PARAMETERS), SetFill(1, 0), SetResize(1, 0),
01840             SetDataTip(STR_NEWGRF_SETTINGS_SET_PARAMETERS, STR_NULL),
01841         NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_TOGGLE_PALETTE), SetFill(1, 0), SetResize(1, 0),
01842             SetDataTip(STR_NEWGRF_SETTINGS_TOGGLE_PALETTE, STR_NEWGRF_SETTINGS_TOGGLE_PALETTE_TOOLTIP),
01843       EndContainer(),
01844       NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_APPLY_CHANGES), SetFill(1, 0), SetResize(1, 0),
01845           SetDataTip(STR_NEWGRF_SETTINGS_APPLY_CHANGES, STR_NULL),
01846     EndContainer(),
01847     NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_VIEW_PARAMETERS), SetFill(1, 0), SetResize(1, 0),
01848       SetDataTip(STR_NEWGRF_SETTINGS_SHOW_PARAMETERS, STR_NULL),
01849   EndContainer(),
01850 };
01851 
01853 NWidgetBase* NewGRFDisplay(int *biggest_index)
01854 {
01855   NWidgetBase *avs = MakeNWidgets(_nested_newgrf_availables_widgets, lengthof(_nested_newgrf_availables_widgets), biggest_index, NULL);
01856 
01857   int biggest2;
01858   NWidgetBase *acs = MakeNWidgets(_nested_newgrf_actives_widgets, lengthof(_nested_newgrf_actives_widgets), &biggest2, NULL);
01859   *biggest_index = max(*biggest_index, biggest2);
01860 
01861   NWidgetBase *inf = MakeNWidgets(_nested_newgrf_infopanel_widgets, lengthof(_nested_newgrf_infopanel_widgets), &biggest2, NULL);
01862   *biggest_index = max(*biggest_index, biggest2);
01863 
01864   return new NWidgetNewGRFDisplay(avs, acs, inf);
01865 }
01866 
01867 /* Widget definition of the manage newgrfs window */
01868 static const NWidgetPart _nested_newgrf_widgets[] = {
01869   NWidget(NWID_HORIZONTAL),
01870     NWidget(WWT_CLOSEBOX, COLOUR_MAUVE),
01871     NWidget(WWT_CAPTION, COLOUR_MAUVE), SetDataTip(STR_NEWGRF_SETTINGS_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
01872   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 const WindowDesc _newgrf_desc(
01885   WDP_CENTER, 300, 263,
01886   WC_GAME_OPTIONS, WC_NONE,
01887   WDF_UNCLICK_BUTTONS,
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 const WindowDesc _scan_progress_desc(
01956   WDP_CENTER, 0, 0,
01957   WC_MODAL_PROGRESS, WC_NONE,
01958   WDF_UNCLICK_BUTTONS,
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(), last_name(NULL), scanned(0)
01969   {
01970     this->InitNested(&_scan_progress_desc, 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         SetDParam(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         SetDParam(0, 9999);
01993         SetDParam(1, 9999);
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 }