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     this->GetWidget<NWidgetCore>(WID_TF_CAPTION)->SetDataTip(STR_TEXTFILE_README_CAPTION + file_type, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS);
00546 
00547     const char *textfile = this->grf_config->GetTextfile(file_type);
00548     this->LoadTextfile(textfile, NEWGRF_DIR);
00549   }
00550 
00551   /* virtual */ void SetStringParameters(int widget) const
00552   {
00553     if (widget == WID_TF_CAPTION) {
00554       SetDParam(0, STR_CONTENT_TYPE_NEWGRF);
00555       SetDParamStr(1, this->grf_config->GetName());
00556     }
00557   }
00558 };
00559 
00560 void ShowNewGRFTextfileWindow(TextfileType file_type, const GRFConfig *c)
00561 {
00562   DeleteWindowByClass(WC_TEXTFILE);
00563   new NewGRFTextfileWindow(file_type, c);
00564 }
00565 
00566 static GRFPresetList _grf_preset_list;
00567 
00568 class DropDownListPresetItem : public DropDownListItem {
00569 public:
00570   DropDownListPresetItem(int result) : DropDownListItem(result, false) {}
00571 
00572   virtual ~DropDownListPresetItem() {}
00573 
00574   bool Selectable() const
00575   {
00576     return true;
00577   }
00578 
00579   void Draw(int left, int right, int top, int bottom, bool sel, int bg_colour) const
00580   {
00581     DrawString(left + 2, right + 2, top, _grf_preset_list[this->result], sel ? TC_WHITE : TC_BLACK);
00582   }
00583 };
00584 
00585 static void NewGRFConfirmationCallback(Window *w, bool confirmed);
00586 
00590 struct NewGRFWindow : public QueryStringBaseWindow, NewGRFScanCallback {
00591   typedef GUIList<const GRFConfig *, StringFilter &> GUIGRFConfigList;
00592 
00593   static const uint EDITBOX_MAX_SIZE   =  50;
00594 
00595   static Listing   last_sorting;   
00596   static Filtering last_filtering; 
00597   static GUIGRFConfigList::SortFunction   * const sorter_funcs[]; 
00598   static GUIGRFConfigList::FilterFunction * const filter_funcs[]; 
00599 
00600   GUIGRFConfigList avails;    
00601   const GRFConfig *avail_sel; 
00602   int avail_pos;              
00603   StringFilter string_filter; 
00604 
00605   GRFConfig *actives;         
00606   GRFConfig *active_sel;      
00607 
00608   GRFConfig **orig_list;      
00609   bool editable;              
00610   bool show_params;           
00611   bool execute;               
00612   int preset;                 
00613   int active_over;            
00614 
00615   Scrollbar *vscroll;
00616   Scrollbar *vscroll2;
00617 
00618   NewGRFWindow(const WindowDesc *desc, bool editable, bool show_params, bool execute, GRFConfig **orig_list) : QueryStringBaseWindow(EDITBOX_MAX_SIZE)
00619   {
00620     this->avail_sel   = NULL;
00621     this->avail_pos   = -1;
00622     this->active_sel  = NULL;
00623     this->actives     = NULL;
00624     this->orig_list   = orig_list;
00625     this->editable    = editable;
00626     this->execute     = execute;
00627     this->show_params = show_params;
00628     this->preset      = -1;
00629     this->active_over = -1;
00630 
00631     CopyGRFConfigList(&this->actives, *orig_list, false);
00632     GetGRFPresetList(&_grf_preset_list);
00633 
00634     this->CreateNestedTree(desc);
00635     this->vscroll = this->GetScrollbar(WID_NS_SCROLLBAR);
00636     this->vscroll2 = this->GetScrollbar(WID_NS_SCROLL2BAR);
00637 
00638     this->GetWidget<NWidgetStacked>(WID_NS_SHOW_REMOVE)->SetDisplayedPlane(this->editable ? 0 : 1);
00639     this->GetWidget<NWidgetStacked>(WID_NS_SHOW_APPLY)->SetDisplayedPlane(this->editable ? 0 : this->show_params ? 1 : SZSP_HORIZONTAL);
00640     this->FinishInitNested(desc, WN_GAME_OPTIONS_NEWGRF_STATE);
00641 
00642     this->text.Initialize(this->edit_str_buf, this->edit_str_size);
00643     this->SetFocusedWidget(WID_NS_FILTER);
00644 
00645     this->avails.SetListing(this->last_sorting);
00646     this->avails.SetFiltering(this->last_filtering);
00647     this->avails.SetSortFuncs(this->sorter_funcs);
00648     this->avails.SetFilterFuncs(this->filter_funcs);
00649     this->avails.ForceRebuild();
00650 
00651     this->OnInvalidateData(GOID_NEWGRF_LIST_EDITED);
00652   }
00653 
00654   ~NewGRFWindow()
00655   {
00656     DeleteWindowByClass(WC_GRF_PARAMETERS);
00657     DeleteWindowByClass(WC_TEXTFILE);
00658 
00659     if (this->editable && !this->execute) {
00660       CopyGRFConfigList(this->orig_list, this->actives, true);
00661       ResetGRFConfig(false);
00662       ReloadNewGRFData();
00663     }
00664 
00665     /* Remove the temporary copy of grf-list used in window */
00666     ClearGRFConfigList(&this->actives);
00667     _grf_preset_list.Clear();
00668   }
00669 
00670   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00671   {
00672     switch (widget) {
00673       case WID_NS_FILE_LIST:
00674       {
00675         Dimension d = maxdim(GetSpriteSize(SPR_SQUARE), GetSpriteSize(SPR_WARNING_SIGN));
00676         resize->height = max(d.height + 2U, FONT_HEIGHT_NORMAL + 2U);
00677         size->height = max(size->height, WD_FRAMERECT_TOP + 6 * resize->height + WD_FRAMERECT_BOTTOM);
00678         break;
00679       }
00680 
00681       case WID_NS_AVAIL_LIST:
00682         resize->height = max(12, FONT_HEIGHT_NORMAL + 2);
00683         size->height = max(size->height, WD_FRAMERECT_TOP + 8 * resize->height + WD_FRAMERECT_BOTTOM);
00684         break;
00685 
00686       case WID_NS_NEWGRF_INFO_TITLE: {
00687         Dimension dim = GetStringBoundingBox(STR_NEWGRF_SETTINGS_INFO_TITLE);
00688         size->height = max(size->height, dim.height + WD_FRAMETEXT_TOP + WD_FRAMETEXT_BOTTOM);
00689         size->width  = max(size->width,  dim.width  + WD_FRAMETEXT_LEFT + WD_FRAMETEXT_RIGHT);
00690         break;
00691       }
00692 
00693       case WID_NS_NEWGRF_INFO:
00694         size->height = max(size->height, WD_FRAMERECT_TOP + 10 * FONT_HEIGHT_NORMAL + WD_FRAMERECT_BOTTOM + padding.height + 2);
00695         break;
00696 
00697       case WID_NS_PRESET_LIST: {
00698         Dimension d = GetStringBoundingBox(STR_NUM_CUSTOM);
00699         for (uint i = 0; i < _grf_preset_list.Length(); i++) {
00700           if (_grf_preset_list[i] != NULL) {
00701             SetDParamStr(0, _grf_preset_list[i]);
00702             d = maxdim(d, GetStringBoundingBox(STR_JUST_RAW_STRING));
00703           }
00704         }
00705         d.width += padding.width;
00706         *size = maxdim(d, *size);
00707         break;
00708       }
00709 
00710       case WID_NS_CONTENT_DOWNLOAD:
00711       case WID_NS_CONTENT_DOWNLOAD2: {
00712         Dimension d = GetStringBoundingBox(STR_NEWGRF_SETTINGS_FIND_MISSING_CONTENT_BUTTON);
00713         *size = maxdim(d, GetStringBoundingBox(STR_INTRO_ONLINE_CONTENT));
00714         size->width  += padding.width;
00715         size->height += padding.height;
00716         break;
00717       }
00718     }
00719   }
00720 
00721   virtual void OnResize()
00722   {
00723     this->vscroll->SetCapacityFromWidget(this, WID_NS_FILE_LIST);
00724     this->vscroll2->SetCapacityFromWidget(this, WID_NS_AVAIL_LIST);
00725   }
00726 
00727   virtual void SetStringParameters(int widget) const
00728   {
00729     switch (widget) {
00730       case WID_NS_PRESET_LIST:
00731         if (this->preset == -1) {
00732           SetDParam(0, STR_NUM_CUSTOM);
00733         } else {
00734           SetDParam(0, STR_JUST_RAW_STRING);
00735           SetDParamStr(1, _grf_preset_list[this->preset]);
00736         }
00737         break;
00738     }
00739   }
00740 
00741   virtual void OnPaint()
00742   {
00743     this->DrawWidgets();
00744     if (this->editable) this->DrawEditBox(WID_NS_FILTER);
00745   }
00746 
00752   inline PaletteID GetPalette(const GRFConfig *c) const
00753   {
00754     PaletteID pal;
00755 
00756     /* Pick a colour */
00757     switch (c->status) {
00758       case GCS_NOT_FOUND:
00759       case GCS_DISABLED:
00760         pal = PALETTE_TO_RED;
00761         break;
00762       case GCS_ACTIVATED:
00763         pal = PALETTE_TO_GREEN;
00764         break;
00765       default:
00766         pal = PALETTE_TO_BLUE;
00767         break;
00768     }
00769 
00770     /* Do not show a "not-failure" colour when it actually failed to load */
00771     if (pal != PALETTE_TO_RED) {
00772       if (HasBit(c->flags, GCF_STATIC)) {
00773         pal = PALETTE_TO_GREY;
00774       } else if (HasBit(c->flags, GCF_COMPATIBLE)) {
00775         pal = PALETTE_TO_ORANGE;
00776       }
00777     }
00778 
00779     return pal;
00780   }
00781 
00782   virtual void DrawWidget(const Rect &r, int widget) const
00783   {
00784     switch (widget) {
00785       case WID_NS_FILE_LIST: {
00786         GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, PC_BLACK);
00787 
00788         uint step_height = this->GetWidget<NWidgetBase>(WID_NS_FILE_LIST)->resize_y;
00789         uint y = r.top + WD_FRAMERECT_TOP;
00790         Dimension square = GetSpriteSize(SPR_SQUARE);
00791         Dimension warning = GetSpriteSize(SPR_WARNING_SIGN);
00792         int square_offset_y = (step_height - square.height) / 2;
00793         int warning_offset_y = (step_height - warning.height) / 2;
00794         int offset_y = (step_height - FONT_HEIGHT_NORMAL) / 2;
00795 
00796         bool rtl = _current_text_dir == TD_RTL;
00797         uint text_left    = rtl ? r.left + WD_FRAMERECT_LEFT : r.left + square.width + 15;
00798         uint text_right   = rtl ? r.right - square.width - 15 : r.right - WD_FRAMERECT_RIGHT;
00799         uint square_left  = rtl ? r.right - square.width - 5 : r.left + 5;
00800         uint warning_left = rtl ? r.right - square.width - warning.width - 10 : r.left + square.width + 10;
00801 
00802         int i = 0;
00803         for (const GRFConfig *c = this->actives; c != NULL; c = c->next, i++) {
00804           if (this->vscroll->IsVisible(i)) {
00805             const char *text = c->GetName();
00806             bool h = (this->active_sel == c);
00807             PaletteID pal = this->GetPalette(c);
00808 
00809             if (h) {
00810               GfxFillRect(r.left + 1, y, r.right - 1, y + step_height - 1, PC_DARK_BLUE);
00811             } else if (i == this->active_over) {
00812               /* Get index of current selection. */
00813               int active_sel_pos = 0;
00814               for (GRFConfig *c = this->actives; c != NULL && c != this->active_sel; c = c->next, active_sel_pos++) {}
00815               if (active_sel_pos != this->active_over) {
00816                 uint top = this->active_over < active_sel_pos ? y + 1 : y + step_height - 2;
00817                 GfxFillRect(r.left + WD_FRAMERECT_LEFT, top - 1, r.right - WD_FRAMERECT_RIGHT, top + 1, PC_GREY);
00818               }
00819             }
00820             DrawSprite(SPR_SQUARE, pal, square_left, y + square_offset_y);
00821             if (c->error != NULL) DrawSprite(SPR_WARNING_SIGN, 0, warning_left, y + warning_offset_y);
00822             uint txtoffset = c->error == NULL ? 0 : warning.width;
00823             DrawString(text_left + (rtl ? 0 : txtoffset), text_right - (rtl ? txtoffset : 0), y + offset_y, text, h ? TC_WHITE : TC_ORANGE);
00824             y += step_height;
00825           }
00826         }
00827         if (i == this->active_over && this->vscroll->IsVisible(i)) { // Highlight is after the last GRF entry.
00828           GfxFillRect(r.left + WD_FRAMERECT_LEFT, y, r.right - WD_FRAMERECT_RIGHT, y + 2, PC_GREY);
00829         }
00830         break;
00831       }
00832 
00833       case WID_NS_AVAIL_LIST: {
00834         GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, this->active_over == -2 ? PC_DARK_GREY : PC_BLACK);
00835 
00836         uint step_height = this->GetWidget<NWidgetBase>(WID_NS_AVAIL_LIST)->resize_y;
00837         int offset_y = (step_height - FONT_HEIGHT_NORMAL) / 2;
00838         uint y = r.top + WD_FRAMERECT_TOP;
00839         uint min_index = this->vscroll2->GetPosition();
00840         uint max_index = min(min_index + this->vscroll2->GetCapacity(), this->avails.Length());
00841 
00842         for (uint i = min_index; i < max_index; i++) {
00843           const GRFConfig *c = this->avails[i];
00844           bool h = (c == this->avail_sel);
00845           const char *text = c->GetName();
00846 
00847           if (h) GfxFillRect(r.left + 1, y, r.right - 1, y + step_height - 1, PC_DARK_BLUE);
00848           DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y + offset_y, text, h ? TC_WHITE : TC_SILVER);
00849           y += step_height;
00850         }
00851         break;
00852       }
00853 
00854       case WID_NS_NEWGRF_INFO_TITLE:
00855         /* Create the nice grayish rectangle at the details top. */
00856         GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, PC_DARK_BLUE);
00857         DrawString(r.left, r.right, (r.top + r.bottom - FONT_HEIGHT_NORMAL) / 2, STR_NEWGRF_SETTINGS_INFO_TITLE, TC_FROMSTRING, SA_HOR_CENTER);
00858         break;
00859 
00860       case WID_NS_NEWGRF_INFO: {
00861         const GRFConfig *selected = this->active_sel;
00862         if (selected == NULL) selected = this->avail_sel;
00863         if (selected != NULL) {
00864           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);
00865         }
00866         break;
00867       }
00868     }
00869   }
00870 
00871   virtual void OnClick(Point pt, int widget, int click_count)
00872   {
00873     if (widget >= WID_NS_NEWGRF_TEXTFILE && widget < WID_NS_NEWGRF_TEXTFILE + TFT_END) {
00874       if (this->active_sel == NULL && this->avail_sel == NULL) return;
00875 
00876       ShowNewGRFTextfileWindow((TextfileType)(widget - WID_NS_NEWGRF_TEXTFILE), this->active_sel != NULL ? this->active_sel : this->avail_sel);
00877       return;
00878     }
00879 
00880     switch (widget) {
00881       case WID_NS_PRESET_LIST: {
00882         DropDownList *list = new DropDownList();
00883 
00884         /* Add 'None' option for clearing list */
00885         list->push_back(new DropDownListStringItem(STR_NONE, -1, false));
00886 
00887         for (uint i = 0; i < _grf_preset_list.Length(); i++) {
00888           if (_grf_preset_list[i] != NULL) {
00889             list->push_back(new DropDownListPresetItem(i));
00890           }
00891         }
00892 
00893         this->DeleteChildWindows(WC_QUERY_STRING); // Remove the parameter query window
00894         ShowDropDownList(this, list, this->preset, WID_NS_PRESET_LIST);
00895         break;
00896       }
00897 
00898       case WID_NS_OPEN_URL: {
00899         const GRFConfig *c = (this->avail_sel == NULL) ? this->active_sel : this->avail_sel;
00900 
00901         extern void OpenBrowser(const char *url);
00902         OpenBrowser(c->GetURL());
00903         break;
00904       }
00905 
00906       case WID_NS_PRESET_SAVE:
00907         ShowQueryString(STR_EMPTY, STR_NEWGRF_SETTINGS_PRESET_SAVE_QUERY, 32, this, CS_ALPHANUMERAL, QSF_NONE);
00908         break;
00909 
00910       case WID_NS_PRESET_DELETE:
00911         if (this->preset == -1) return;
00912 
00913         DeleteGRFPresetFromConfig(_grf_preset_list[this->preset]);
00914         GetGRFPresetList(&_grf_preset_list);
00915         this->preset = -1;
00916         this->InvalidateData();
00917         this->DeleteChildWindows(WC_QUERY_STRING); // Remove the parameter query window
00918         break;
00919 
00920       case WID_NS_MOVE_UP: { // Move GRF up
00921         if (this->active_sel == NULL || !this->editable) break;
00922 
00923         int pos = 0;
00924         for (GRFConfig **pc = &this->actives; *pc != NULL; pc = &(*pc)->next, pos++) {
00925           GRFConfig *c = *pc;
00926           if (c->next == this->active_sel) {
00927             c->next = this->active_sel->next;
00928             this->active_sel->next = c;
00929             *pc = this->active_sel;
00930             break;
00931           }
00932         }
00933         this->vscroll->ScrollTowards(pos);
00934         this->preset = -1;
00935         this->InvalidateData();
00936         break;
00937       }
00938 
00939       case WID_NS_MOVE_DOWN: { // Move GRF down
00940         if (this->active_sel == NULL || !this->editable) break;
00941 
00942         int pos = 1; // Start at 1 as we swap the selected newgrf with the next one
00943         for (GRFConfig **pc = &this->actives; *pc != NULL; pc = &(*pc)->next, pos++) {
00944           GRFConfig *c = *pc;
00945           if (c == this->active_sel) {
00946             *pc = c->next;
00947             c->next = c->next->next;
00948             (*pc)->next = c;
00949             break;
00950           }
00951         }
00952         this->vscroll->ScrollTowards(pos);
00953         this->preset = -1;
00954         this->InvalidateData();
00955         break;
00956       }
00957 
00958       case WID_NS_FILE_LIST: { // Select an active GRF.
00959         ResetObjectToPlace();
00960 
00961         uint i = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_NS_FILE_LIST);
00962 
00963         GRFConfig *c;
00964         for (c = this->actives; c != NULL && i > 0; c = c->next, i--) {}
00965 
00966         if (this->active_sel != c) DeleteWindowByClass(WC_GRF_PARAMETERS);
00967         this->active_sel = c;
00968         this->avail_sel = NULL;
00969         this->avail_pos = -1;
00970 
00971         this->InvalidateData();
00972         if (click_count == 1) {
00973           if (this->editable && this->active_sel != NULL) SetObjectToPlaceWnd(SPR_CURSOR_MOUSE, PAL_NONE, HT_DRAG, this);
00974           break;
00975         }
00976         /* FALL THROUGH, with double click. */
00977       }
00978 
00979       case WID_NS_REMOVE: { // Remove GRF
00980         if (this->active_sel == NULL || !this->editable) break;
00981         DeleteWindowByClass(WC_GRF_PARAMETERS);
00982 
00983         /* Choose the next GRF file to be the selected file. */
00984         GRFConfig *newsel = this->active_sel->next;
00985         for (GRFConfig **pc = &this->actives; *pc != NULL; pc = &(*pc)->next) {
00986           GRFConfig *c = *pc;
00987           /* If the new selection is empty (i.e. we're deleting the last item
00988            * in the list, pick the file just before the selected file */
00989           if (newsel == NULL && c->next == this->active_sel) newsel = c;
00990 
00991           if (c == this->active_sel) {
00992             *pc = c->next;
00993             delete c;
00994             break;
00995           }
00996         }
00997 
00998         this->active_sel = newsel;
00999         this->preset = -1;
01000         this->avail_pos = -1;
01001         this->avail_sel = NULL;
01002         this->avails.ForceRebuild();
01003         this->InvalidateData(GOID_NEWGRF_LIST_EDITED);
01004         break;
01005       }
01006 
01007       case WID_NS_AVAIL_LIST: { // Select a non-active GRF.
01008         ResetObjectToPlace();
01009 
01010         uint i = this->vscroll2->GetScrolledRowFromWidget(pt.y, this, WID_NS_AVAIL_LIST);
01011         this->active_sel = NULL;
01012         DeleteWindowByClass(WC_GRF_PARAMETERS);
01013         if (i < this->avails.Length()) {
01014           this->avail_sel = this->avails[i];
01015           this->avail_pos = i;
01016         }
01017         this->InvalidateData();
01018         if (click_count == 1) {
01019           if (this->editable && this->avail_sel != NULL && !HasBit(this->avail_sel->flags, GCF_INVALID)) SetObjectToPlaceWnd(SPR_CURSOR_MOUSE, PAL_NONE, HT_DRAG, this);
01020           break;
01021         }
01022         /* FALL THROUGH, with double click. */
01023       }
01024 
01025       case WID_NS_ADD:
01026         if (this->avail_sel == NULL || !this->editable || HasBit(this->avail_sel->flags, GCF_INVALID)) break;
01027 
01028         this->AddGRFToActive();
01029         break;
01030 
01031       case WID_NS_APPLY_CHANGES: // Apply changes made to GRF list
01032         if (!this->editable) break;
01033         if (this->execute) {
01034           ShowQuery(
01035             STR_NEWGRF_POPUP_CAUTION_CAPTION,
01036             STR_NEWGRF_CONFIRMATION_TEXT,
01037             this,
01038             NewGRFConfirmationCallback
01039           );
01040         } else {
01041           CopyGRFConfigList(this->orig_list, this->actives, true);
01042           ResetGRFConfig(false);
01043           ReloadNewGRFData();
01044         }
01045         this->DeleteChildWindows(WC_QUERY_STRING); // Remove the parameter query window
01046         break;
01047 
01048       case WID_NS_VIEW_PARAMETERS:
01049       case WID_NS_SET_PARAMETERS: { // Edit parameters
01050         if (this->active_sel == NULL || !this->show_params || this->active_sel->num_valid_params == 0) break;
01051 
01052         OpenGRFParameterWindow(this->active_sel, this->editable);
01053         break;
01054       }
01055 
01056       case WID_NS_TOGGLE_PALETTE:
01057         if (this->active_sel != NULL || !this->editable) {
01058           this->active_sel->palette ^= GRFP_USE_MASK;
01059           this->SetDirty();
01060         }
01061         break;
01062 
01063       case WID_NS_CONTENT_DOWNLOAD:
01064       case WID_NS_CONTENT_DOWNLOAD2:
01065         if (!_network_available) {
01066           ShowErrorMessage(STR_NETWORK_ERROR_NOTAVAILABLE, INVALID_STRING_ID, WL_ERROR);
01067         } else {
01068 #if defined(ENABLE_NETWORK)
01069           this->DeleteChildWindows(WC_QUERY_STRING); // Remove the parameter query window
01070 
01071           ShowMissingContentWindow(this->actives);
01072 #endif
01073         }
01074         break;
01075 
01076       case WID_NS_RESCAN_FILES:
01077       case WID_NS_RESCAN_FILES2:
01078         ScanNewGRFFiles(this);
01079         break;
01080     }
01081   }
01082 
01083   virtual void OnNewGRFsScanned()
01084   {
01085     this->avail_sel = NULL;
01086     this->avail_pos = -1;
01087     this->avails.ForceRebuild();
01088     this->DeleteChildWindows(WC_QUERY_STRING); // Remove the parameter query window
01089     this->DeleteChildWindows(WC_TEXTFILE);     // Remove the view textfile window
01090   }
01091 
01092   virtual void OnDropdownSelect(int widget, int index)
01093   {
01094     if (!this->editable) return;
01095 
01096     ClearGRFConfigList(&this->actives);
01097     this->preset = index;
01098 
01099     if (index != -1) {
01100       this->actives = LoadGRFPresetFromConfig(_grf_preset_list[index]);
01101     }
01102     this->avails.ForceRebuild();
01103 
01104     ResetObjectToPlace();
01105     DeleteWindowByClass(WC_GRF_PARAMETERS);
01106     this->active_sel = NULL;
01107     this->InvalidateData(GOID_NEWGRF_PRESET_LOADED);
01108   }
01109 
01110   virtual void OnQueryTextFinished(char *str)
01111   {
01112     if (str == NULL) return;
01113 
01114     SaveGRFPresetToConfig(str, this->actives);
01115     GetGRFPresetList(&_grf_preset_list);
01116 
01117     /* Switch to this preset */
01118     for (uint i = 0; i < _grf_preset_list.Length(); i++) {
01119       if (_grf_preset_list[i] != NULL && strcmp(_grf_preset_list[i], str) == 0) {
01120         this->preset = i;
01121         break;
01122       }
01123     }
01124 
01125     this->InvalidateData();
01126   }
01127 
01133   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
01134   {
01135     if (!gui_scope) return;
01136     switch (data) {
01137       default:
01138         /* Nothing important to do */
01139         break;
01140 
01141       case GOID_NEWGRF_RESCANNED:
01142         /* Search the list for items that are now found and mark them as such. */
01143         for (GRFConfig **l = &this->actives; *l != NULL; l = &(*l)->next) {
01144           GRFConfig *c = *l;
01145           bool compatible = HasBit(c->flags, GCF_COMPATIBLE);
01146           if (c->status != GCS_NOT_FOUND && !compatible) continue;
01147 
01148           const GRFConfig *f = FindGRFConfig(c->ident.grfid, FGCM_EXACT, compatible ? c->original_md5sum : c->ident.md5sum);
01149           if (f == NULL || HasBit(f->flags, GCF_INVALID)) continue;
01150 
01151           *l = new GRFConfig(*f);
01152           (*l)->next = c->next;
01153 
01154           if (active_sel == c) active_sel = *l;
01155 
01156           delete c;
01157         }
01158 
01159         this->avails.ForceRebuild();
01160         /* FALL THROUGH */
01161       case GOID_NEWGRF_LIST_EDITED:
01162         this->preset = -1;
01163         /* FALL THROUGH */
01164       case GOID_NEWGRF_PRESET_LOADED: {
01165         /* Update scrollbars */
01166         int i = 0;
01167         for (const GRFConfig *c = this->actives; c != NULL; c = c->next, i++) {}
01168 
01169         this->vscroll->SetCapacityFromWidget(this, WID_NS_FILE_LIST);
01170         this->vscroll->SetCount(i + 1); // Reserve empty space for drag and drop handling.
01171 
01172         this->vscroll2->SetCapacityFromWidget(this, WID_NS_AVAIL_LIST);
01173         if (this->avail_pos >= 0) this->vscroll2->ScrollTowards(this->avail_pos);
01174         break;
01175       }
01176     }
01177 
01178     this->BuildAvailables();
01179 
01180     this->SetWidgetsDisabledState(!this->editable,
01181       WID_NS_PRESET_LIST,
01182       WID_NS_APPLY_CHANGES,
01183       WID_NS_TOGGLE_PALETTE,
01184       WIDGET_LIST_END
01185     );
01186     this->SetWidgetDisabledState(WID_NS_ADD, !this->editable || this->avail_sel == NULL || HasBit(this->avail_sel->flags, GCF_INVALID));
01187 
01188     bool disable_all = this->active_sel == NULL || !this->editable;
01189     this->SetWidgetsDisabledState(disable_all,
01190       WID_NS_REMOVE,
01191       WID_NS_MOVE_UP,
01192       WID_NS_MOVE_DOWN,
01193       WIDGET_LIST_END
01194     );
01195 
01196     const GRFConfig *c = (this->avail_sel == NULL) ? this->active_sel : this->avail_sel;
01197     for (TextfileType tft = TFT_BEGIN; tft < TFT_END; tft++) {
01198       this->SetWidgetDisabledState(WID_NS_NEWGRF_TEXTFILE + tft, c == NULL || c->GetTextfile(tft) == NULL);
01199     }
01200     this->SetWidgetDisabledState(WID_NS_OPEN_URL, c == NULL || StrEmpty(c->GetURL()));
01201 
01202     this->SetWidgetDisabledState(WID_NS_SET_PARAMETERS, !this->show_params || this->active_sel == NULL || this->active_sel->num_valid_params == 0);
01203     this->SetWidgetDisabledState(WID_NS_VIEW_PARAMETERS, !this->show_params || this->active_sel == NULL || this->active_sel->num_valid_params == 0);
01204     this->SetWidgetDisabledState(WID_NS_TOGGLE_PALETTE, disable_all);
01205 
01206     if (!disable_all) {
01207       /* All widgets are now enabled, so disable widgets we can't use */
01208       if (this->active_sel == this->actives)    this->DisableWidget(WID_NS_MOVE_UP);
01209       if (this->active_sel->next == NULL)       this->DisableWidget(WID_NS_MOVE_DOWN);
01210       if (this->active_sel->IsOpenTTDBaseGRF()) this->DisableWidget(WID_NS_REMOVE);
01211     }
01212 
01213     this->SetWidgetDisabledState(WID_NS_PRESET_DELETE, this->preset == -1);
01214 
01215     bool has_missing = false;
01216     bool has_compatible = false;
01217     for (const GRFConfig *c = this->actives; !has_missing && c != NULL; c = c->next) {
01218       has_missing    |= c->status == GCS_NOT_FOUND;
01219       has_compatible |= HasBit(c->flags, GCF_COMPATIBLE);
01220     }
01221     uint16 widget_data;
01222     StringID tool_tip;
01223     if (has_missing || has_compatible) {
01224       widget_data = STR_NEWGRF_SETTINGS_FIND_MISSING_CONTENT_BUTTON;
01225       tool_tip    = STR_NEWGRF_SETTINGS_FIND_MISSING_CONTENT_TOOLTIP;
01226     } else {
01227       widget_data = STR_INTRO_ONLINE_CONTENT;
01228       tool_tip    = STR_INTRO_TOOLTIP_ONLINE_CONTENT;
01229     }
01230     this->GetWidget<NWidgetCore>(WID_NS_CONTENT_DOWNLOAD)->widget_data  = widget_data;
01231     this->GetWidget<NWidgetCore>(WID_NS_CONTENT_DOWNLOAD)->tool_tip     = tool_tip;
01232     this->GetWidget<NWidgetCore>(WID_NS_CONTENT_DOWNLOAD2)->widget_data = widget_data;
01233     this->GetWidget<NWidgetCore>(WID_NS_CONTENT_DOWNLOAD2)->tool_tip    = tool_tip;
01234 
01235     this->SetWidgetDisabledState(WID_NS_PRESET_SAVE, has_missing);
01236   }
01237 
01238   virtual void OnMouseLoop()
01239   {
01240     if (this->editable) this->HandleEditBox(WID_NS_FILTER);
01241   }
01242 
01243   virtual EventState OnKeyPress(uint16 key, uint16 keycode)
01244   {
01245     if (!this->editable) return ES_NOT_HANDLED;
01246 
01247     switch (keycode) {
01248       case WKC_UP:
01249         /* scroll up by one */
01250         if (this->avail_pos > 0) this->avail_pos--;
01251         break;
01252 
01253       case WKC_DOWN:
01254         /* scroll down by one */
01255         if (this->avail_pos < (int)this->avails.Length() - 1) this->avail_pos++;
01256         break;
01257 
01258       case WKC_PAGEUP:
01259         /* scroll up a page */
01260         this->avail_pos = (this->avail_pos < this->vscroll2->GetCapacity()) ? 0 : this->avail_pos - this->vscroll2->GetCapacity();
01261         break;
01262 
01263       case WKC_PAGEDOWN:
01264         /* scroll down a page */
01265         this->avail_pos = min(this->avail_pos + this->vscroll2->GetCapacity(), (int)this->avails.Length() - 1);
01266         break;
01267 
01268       case WKC_HOME:
01269         /* jump to beginning */
01270         this->avail_pos = 0;
01271         break;
01272 
01273       case WKC_END:
01274         /* jump to end */
01275         this->avail_pos = this->avails.Length() - 1;
01276         break;
01277 
01278       default: {
01279         /* Handle editbox input */
01280         EventState state = ES_NOT_HANDLED;
01281         if (this->HandleEditBoxKey(WID_NS_FILTER, key, keycode, state) == HEBR_EDITING) {
01282           this->OnOSKInput(WID_NS_FILTER);
01283         }
01284         return state;
01285       }
01286     }
01287 
01288     if (this->avails.Length() == 0) this->avail_pos = -1;
01289     if (this->avail_pos >= 0) {
01290       this->avail_sel = this->avails[this->avail_pos];
01291       this->vscroll2->ScrollTowards(this->avail_pos);
01292       this->InvalidateData(0);
01293     }
01294 
01295     return ES_HANDLED;
01296   }
01297 
01298   virtual void OnOSKInput(int wid)
01299   {
01300     if (!this->editable) return;
01301 
01302     string_filter.SetFilterTerm(this->edit_str_buf);
01303     this->avails.SetFilterState(!string_filter.IsEmpty());
01304     this->avails.ForceRebuild();
01305     this->InvalidateData(0);
01306   }
01307 
01308   virtual void OnDragDrop(Point pt, int widget)
01309   {
01310     if (!this->editable) return;
01311 
01312     if (widget == WID_NS_FILE_LIST) {
01313       if (this->active_sel != NULL) {
01314         /* Get pointer to the selected file in the active list. */
01315         int from_pos = 0;
01316         GRFConfig **from_prev;
01317         for (from_prev = &this->actives; *from_prev != this->active_sel; from_prev = &(*from_prev)->next, from_pos++) {}
01318 
01319         /* Gets the drag-and-drop destination offset. Ignore the last dummy line. */
01320         int to_pos = min(this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_NS_FILE_LIST), this->vscroll->GetCount() - 2);
01321         if (to_pos != from_pos) { // Don't move NewGRF file over itself.
01322           /* Get pointer to destination position. */
01323           GRFConfig **to_prev = &this->actives;
01324           for (int i = from_pos < to_pos ? -1 : 0; *to_prev != NULL && i < to_pos; to_prev = &(*to_prev)->next, i++) {}
01325 
01326           /* Detach NewGRF file from its original position. */
01327           *from_prev = this->active_sel->next;
01328 
01329           /* Attach NewGRF file to its new position. */
01330           this->active_sel->next = *to_prev;
01331           *to_prev = this->active_sel;
01332 
01333           this->vscroll->ScrollTowards(to_pos);
01334           this->preset = -1;
01335           this->InvalidateData();
01336         }
01337       } else if (this->avail_sel != NULL) {
01338         int to_pos = min(this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_NS_FILE_LIST), this->vscroll->GetCount() - 1);
01339         this->AddGRFToActive(to_pos);
01340       }
01341     } else if (widget == WID_NS_AVAIL_LIST && this->active_sel != NULL) {
01342       /* Remove active NewGRF file by dragging it over available list. */
01343       Point dummy = {-1, -1};
01344       this->OnClick(dummy, WID_NS_REMOVE, 1);
01345     }
01346 
01347     ResetObjectToPlace();
01348 
01349     if (this->active_over != -1) {
01350       /* End of drag-and-drop, hide dragged destination highlight. */
01351       this->SetWidgetDirty(this->active_over == -2 ? WID_NS_AVAIL_LIST : WID_NS_FILE_LIST);
01352       this->active_over = -1;
01353     }
01354   }
01355 
01356   virtual void OnMouseDrag(Point pt, int widget)
01357   {
01358     if (!this->editable) return;
01359 
01360     if (widget == WID_NS_FILE_LIST && (this->active_sel != NULL || this->avail_sel != NULL)) {
01361       /* An NewGRF file is dragged over the active list. */
01362       int to_pos = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_NS_FILE_LIST);
01363       /* Skip the last dummy line if the source is from the active list. */
01364       to_pos = min(to_pos, this->vscroll->GetCount() - (this->active_sel != NULL ? 2 : 1));
01365 
01366       if (to_pos != this->active_over) {
01367         this->active_over = to_pos;
01368         this->SetWidgetDirty(WID_NS_FILE_LIST);
01369       }
01370     } else if (widget == WID_NS_AVAIL_LIST && this->active_sel != NULL) {
01371       this->active_over = -2;
01372       this->SetWidgetDirty(WID_NS_AVAIL_LIST);
01373     } else if (this->active_over != -1) {
01374       this->SetWidgetDirty(this->active_over == -2 ? WID_NS_AVAIL_LIST : WID_NS_FILE_LIST);
01375       this->active_over = -1;
01376     }
01377   }
01378 
01379 private:
01381   static int CDECL NameSorter(const GRFConfig * const *a, const GRFConfig * const *b)
01382   {
01383     int i = strnatcmp((*a)->GetName(), (*b)->GetName()); // Sort by name (natural sorting).
01384     if (i != 0) return i;
01385 
01386     i = (*a)->version - (*b)->version;
01387     if (i != 0) return i;
01388 
01389     return memcmp((*a)->ident.md5sum, (*b)->ident.md5sum, lengthof((*b)->ident.md5sum));
01390   }
01391 
01393   static bool CDECL TagNameFilter(const GRFConfig * const *a, StringFilter &filter)
01394   {
01395     filter.ResetState();
01396     filter.AddLine((*a)->GetName());
01397     filter.AddLine((*a)->filename);
01398     filter.AddLine((*a)->GetDescription());
01399     return filter.GetState();;
01400   }
01401 
01402   void BuildAvailables()
01403   {
01404     if (!this->avails.NeedRebuild()) return;
01405 
01406     this->avails.Clear();
01407 
01408     for (const GRFConfig *c = _all_grfs; c != NULL; c = c->next) {
01409       bool found = false;
01410       for (const GRFConfig *grf = this->actives; grf != NULL && !found; grf = grf->next) found = grf->ident.HasGrfIdentifier(c->ident.grfid, c->ident.md5sum);
01411       if (found) continue;
01412 
01413       if (_settings_client.gui.newgrf_show_old_versions) {
01414         *this->avails.Append() = c;
01415       } else {
01416         const GRFConfig *best = FindGRFConfig(c->ident.grfid, HasBit(c->flags, GCF_INVALID) ? FGCM_NEWEST : FGCM_NEWEST_VALID);
01417         /*
01418          * If the best version is 0, then all NewGRF with this GRF ID
01419          * have version 0, so for backward compatability reasons we
01420          * want to show them all.
01421          * If we are the best version, then we definitely want to
01422          * show that NewGRF!.
01423          */
01424         if (best->version == 0 || best->ident.HasGrfIdentifier(c->ident.grfid, c->ident.md5sum)) {
01425           *this->avails.Append() = c;
01426         }
01427       }
01428     }
01429 
01430     this->avails.Filter(this->string_filter);
01431     this->avails.Compact();
01432     this->avails.RebuildDone();
01433     this->avails.Sort();
01434 
01435     if (this->avail_sel != NULL) {
01436       this->avail_pos = this->avails.FindIndex(this->avail_sel);
01437       if (this->avail_pos < 0) this->avail_sel = NULL;
01438     }
01439 
01440     this->vscroll2->SetCount(this->avails.Length()); // Update the scrollbar
01441   }
01442 
01448   bool AddGRFToActive(int ins_pos = -1)
01449   {
01450     if (this->avail_sel == NULL || !this->editable || HasBit(this->avail_sel->flags, GCF_INVALID)) return false;
01451 
01452     GRFConfig **entry = NULL;
01453     GRFConfig **list;
01454     /* Find last entry in the list, checking for duplicate grfid on the way */
01455     for (list = &this->actives; *list != NULL; list = &(*list)->next, ins_pos--) {
01456       if (ins_pos == 0) entry = list; // Insert position? Save.
01457       if ((*list)->ident.grfid == this->avail_sel->ident.grfid) {
01458         ShowErrorMessage(STR_NEWGRF_DUPLICATE_GRFID, INVALID_STRING_ID, WL_INFO);
01459         return false;
01460       }
01461     }
01462     if (entry == NULL) entry = list;
01463 
01464     GRFConfig *c = new GRFConfig(*this->avail_sel); // Copy GRF details from scanned list.
01465     c->SetParameterDefaults();
01466 
01467     /* Insert GRF config to configuration list. */
01468     c->next = *entry;
01469     *entry = c;
01470 
01471     /* Select next (or previous, if last one) item in the list. */
01472     int new_pos = this->avail_pos + 1;
01473     if (new_pos >= (int)this->avails.Length()) new_pos = this->avail_pos - 1;
01474     this->avail_pos = new_pos;
01475     if (new_pos >= 0) this->avail_sel = this->avails[new_pos];
01476 
01477     this->avails.ForceRebuild();
01478     this->InvalidateData(GOID_NEWGRF_LIST_EDITED);
01479     return true;
01480   }
01481 };
01482 
01483 #if defined(ENABLE_NETWORK)
01484 
01488 void ShowMissingContentWindow(const GRFConfig *list)
01489 {
01490   /* Only show the things in the current list, or everything when nothing's selected */
01491   ContentVector cv;
01492   for (const GRFConfig *c = list; c != NULL; c = c->next) {
01493     if (c->status != GCS_NOT_FOUND && !HasBit(c->flags, GCF_COMPATIBLE)) continue;
01494 
01495     ContentInfo *ci = new ContentInfo();
01496     ci->type = CONTENT_TYPE_NEWGRF;
01497     ci->state = ContentInfo::DOES_NOT_EXIST;
01498     ttd_strlcpy(ci->name, c->GetName(), lengthof(ci->name));
01499     ci->unique_id = BSWAP32(c->ident.grfid);
01500     memcpy(ci->md5sum, HasBit(c->flags, GCF_COMPATIBLE) ? c->original_md5sum : c->ident.md5sum, sizeof(ci->md5sum));
01501     *cv.Append() = ci;
01502   }
01503   ShowNetworkContentListWindow(cv.Length() == 0 ? NULL : &cv, CONTENT_TYPE_NEWGRF);
01504 }
01505 #endif
01506 
01507 Listing NewGRFWindow::last_sorting     = {false, 0};
01508 Filtering NewGRFWindow::last_filtering = {false, 0};
01509 
01510 NewGRFWindow::GUIGRFConfigList::SortFunction * const NewGRFWindow::sorter_funcs[] = {
01511   &NameSorter,
01512 };
01513 
01514 NewGRFWindow::GUIGRFConfigList::FilterFunction * const NewGRFWindow::filter_funcs[] = {
01515   &TagNameFilter,
01516 };
01517 
01524 class NWidgetNewGRFDisplay : public NWidgetContainer {
01525 public:
01526   static const uint INTER_LIST_SPACING;      
01527   static const uint INTER_COLUMN_SPACING;    
01528   static const uint MAX_EXTRA_INFO_WIDTH;    
01529   static const uint MIN_EXTRA_FOR_3_COLUMNS; 
01530 
01531   NWidgetBase *avs; 
01532   NWidgetBase *acs; 
01533   NWidgetBase *inf; 
01534   bool editable;    
01535 
01536   NWidgetNewGRFDisplay(NWidgetBase *avs, NWidgetBase *acs, NWidgetBase *inf) : NWidgetContainer(NWID_HORIZONTAL)
01537   {
01538     this->avs = avs;
01539     this->acs = acs;
01540     this->inf = inf;
01541 
01542     this->Add(this->avs);
01543     this->Add(this->acs);
01544     this->Add(this->inf);
01545 
01546     this->editable = true; // Temporary setting, 'real' value is set in SetupSmallestSize().
01547   }
01548 
01549   virtual void SetupSmallestSize(Window *w, bool init_array)
01550   {
01551     /* Copy state flag from the window. */
01552     assert(dynamic_cast<NewGRFWindow *>(w) != NULL);
01553     NewGRFWindow *ngw = (NewGRFWindow *)w;
01554     this->editable = ngw->editable;
01555 
01556     this->avs->SetupSmallestSize(w, init_array);
01557     this->acs->SetupSmallestSize(w, init_array);
01558     this->inf->SetupSmallestSize(w, init_array);
01559 
01560     uint min_avs_width = this->avs->smallest_x + this->avs->padding_left + this->avs->padding_right;
01561     uint min_acs_width = this->acs->smallest_x + this->acs->padding_left + this->acs->padding_right;
01562     uint min_inf_width = this->inf->smallest_x + this->inf->padding_left + this->inf->padding_right;
01563 
01564     uint min_avs_height = this->avs->smallest_y + this->avs->padding_top + this->avs->padding_bottom;
01565     uint min_acs_height = this->acs->smallest_y + this->acs->padding_top + this->acs->padding_bottom;
01566     uint min_inf_height = this->inf->smallest_y + this->inf->padding_top + this->inf->padding_bottom;
01567 
01568     /* Smallest window is in two column mode. */
01569     this->smallest_x = max(min_avs_width, min_acs_width) + INTER_COLUMN_SPACING + min_inf_width;
01570     this->smallest_y = max(min_inf_height, min_acs_height + INTER_LIST_SPACING + min_avs_height);
01571 
01572     /* Filling. */
01573     this->fill_x = LeastCommonMultiple(this->avs->fill_x, this->acs->fill_x);
01574     if (this->inf->fill_x > 0 && (this->fill_x == 0 || this->fill_x > this->inf->fill_x)) this->fill_x = this->inf->fill_x;
01575 
01576     this->fill_y = this->avs->fill_y;
01577     if (this->acs->fill_y > 0 && (this->fill_y == 0 || this->fill_y > this->acs->fill_y)) this->fill_y = this->acs->fill_y;
01578     this->fill_y = LeastCommonMultiple(this->fill_y, this->inf->fill_y);
01579 
01580     /* Resizing. */
01581     this->resize_x = LeastCommonMultiple(this->avs->resize_x, this->acs->resize_x);
01582     if (this->inf->resize_x > 0 && (this->resize_x == 0 || this->resize_x > this->inf->resize_x)) this->resize_x = this->inf->resize_x;
01583 
01584     this->resize_y = this->avs->resize_y;
01585     if (this->acs->resize_y > 0 && (this->resize_y == 0 || this->resize_y > this->acs->resize_y)) this->resize_y = this->acs->resize_y;
01586     this->resize_y = LeastCommonMultiple(this->resize_y, this->inf->resize_y);
01587 
01588     /* Make sure the height suits the 3 column (resp. not-editable) format; the 2 column format can easily fill space between the lists */
01589     this->smallest_y = ComputeMaxSize(min_acs_height, this->smallest_y + this->resize_y - 1, this->resize_y);
01590   }
01591 
01592   virtual void AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)
01593   {
01594     this->StoreSizePosition(sizing, x, y, given_width, given_height);
01595 
01596     uint min_avs_width = this->avs->smallest_x + this->avs->padding_left + this->avs->padding_right;
01597     uint min_acs_width = this->acs->smallest_x + this->acs->padding_left + this->acs->padding_right;
01598     uint min_inf_width = this->inf->smallest_x + this->inf->padding_left + this->inf->padding_right;
01599 
01600     uint min_list_width = max(min_avs_width, min_acs_width); // Smallest width of the lists such that they have equal width (incl padding).
01601     uint avs_extra_width = min_list_width - min_avs_width;   // Additional width needed for avs to reach min_list_width.
01602     uint acs_extra_width = min_list_width - min_acs_width;   // Additional width needed for acs to reach min_list_width.
01603 
01604     /* Use 2 or 3 colmuns? */
01605     uint min_three_columns = min_avs_width + min_acs_width + min_inf_width + 2 * INTER_COLUMN_SPACING;
01606     uint min_two_columns   = min_list_width + min_inf_width + INTER_COLUMN_SPACING;
01607     bool use_three_columns = this->editable && (min_three_columns + MIN_EXTRA_FOR_3_COLUMNS <= given_width);
01608 
01609     /* Info panel is a seperate column in both modes. Compute its width first. */
01610     uint extra_width, inf_width;
01611     if (use_three_columns) {
01612       extra_width = given_width - min_three_columns;
01613       inf_width = min(MAX_EXTRA_INFO_WIDTH, extra_width / 2);
01614     } else {
01615       extra_width = given_width - min_two_columns;
01616       inf_width = min(MAX_EXTRA_INFO_WIDTH, extra_width / 2);
01617     }
01618     inf_width = ComputeMaxSize(this->inf->smallest_x, this->inf->smallest_x + inf_width, this->inf->GetHorizontalStepSize(sizing));
01619     extra_width -= inf_width - this->inf->smallest_x;
01620 
01621     uint inf_height = ComputeMaxSize(this->inf->smallest_y, given_height, this->inf->GetVerticalStepSize(sizing));
01622 
01623     if (use_three_columns) {
01624       /* Three column display, first make both lists equally wide, then divide whatever is left between both lists.
01625        * Only keep track of what avs gets, all other space goes to acs. */
01626       uint avs_width = min(avs_extra_width, extra_width);
01627       extra_width -= avs_width;
01628       extra_width -= min(acs_extra_width, extra_width);
01629       avs_width += extra_width / 2;
01630 
01631       avs_width = ComputeMaxSize(this->avs->smallest_x, this->avs->smallest_x + avs_width, this->avs->GetHorizontalStepSize(sizing));
01632 
01633       uint acs_width = given_width - // Remaining space, including horizontal padding.
01634           inf_width - this->inf->padding_left - this->inf->padding_right -
01635           avs_width - this->avs->padding_left - this->avs->padding_right - 2 * INTER_COLUMN_SPACING;
01636       acs_width = ComputeMaxSize(min_acs_width, acs_width, this->acs->GetHorizontalStepSize(sizing)) -
01637           this->acs->padding_left - this->acs->padding_right;
01638 
01639       /* Never use fill_y on these; the minimal size is choosen, so that the 3 column view looks nice */
01640       uint avs_height = ComputeMaxSize(this->avs->smallest_y, given_height, this->avs->resize_y);
01641       uint acs_height = ComputeMaxSize(this->acs->smallest_y, given_height, this->acs->resize_y);
01642 
01643       /* Assign size and position to the childs. */
01644       if (rtl) {
01645         x += this->inf->padding_left;
01646         this->inf->AssignSizePosition(sizing, x, y + this->inf->padding_top, inf_width, inf_height, rtl);
01647         x += inf_width + this->inf->padding_right + INTER_COLUMN_SPACING;
01648       } else {
01649         x += this->avs->padding_left;
01650         this->avs->AssignSizePosition(sizing, x, y + this->avs->padding_top, avs_width, avs_height, rtl);
01651         x += avs_width + this->avs->padding_right + INTER_COLUMN_SPACING;
01652       }
01653 
01654       x += this->acs->padding_left;
01655       this->acs->AssignSizePosition(sizing, x, y + this->acs->padding_top, acs_width, acs_height, rtl);
01656       x += acs_width + this->acs->padding_right + INTER_COLUMN_SPACING;
01657 
01658       if (rtl) {
01659         x += this->avs->padding_left;
01660         this->avs->AssignSizePosition(sizing, x, y + this->avs->padding_top, avs_width, avs_height, rtl);
01661       } else {
01662         x += this->inf->padding_left;
01663         this->inf->AssignSizePosition(sizing, x, y + this->inf->padding_top, inf_width, inf_height, rtl);
01664       }
01665     } else {
01666       /* Two columns, all space in extra_width goes to both lists. Since the lists are underneath each other,
01667        * the column is min_list_width wide at least. */
01668       uint avs_width = ComputeMaxSize(this->avs->smallest_x, this->avs->smallest_x + avs_extra_width + extra_width,
01669           this->avs->GetHorizontalStepSize(sizing));
01670       uint acs_width = ComputeMaxSize(this->acs->smallest_x, this->acs->smallest_x + acs_extra_width + extra_width,
01671           this->acs->GetHorizontalStepSize(sizing));
01672 
01673       uint min_avs_height = (!this->editable) ? 0 : this->avs->smallest_y + this->avs->padding_top + this->avs->padding_bottom + INTER_LIST_SPACING;
01674       uint min_acs_height = this->acs->smallest_y + this->acs->padding_top + this->acs->padding_bottom;
01675       uint extra_height = given_height - min_acs_height - min_avs_height;
01676 
01677       /* Never use fill_y on these; instead use the INTER_LIST_SPACING as filler */
01678       uint avs_height = ComputeMaxSize(this->avs->smallest_y, this->avs->smallest_y + extra_height / 2, this->avs->resize_y);
01679       if (this->editable) extra_height -= avs_height - this->avs->smallest_y;
01680       uint acs_height = ComputeMaxSize(this->acs->smallest_y, this->acs->smallest_y + extra_height, this->acs->resize_y);
01681 
01682       /* Assign size and position to the childs. */
01683       if (rtl) {
01684         x += this->inf->padding_left;
01685         this->inf->AssignSizePosition(sizing, x, y + this->inf->padding_top, inf_width, inf_height, rtl);
01686         x += inf_width + this->inf->padding_right + INTER_COLUMN_SPACING;
01687 
01688         this->acs->AssignSizePosition(sizing, x + this->acs->padding_left, y + this->acs->padding_top, acs_width, acs_height, rtl);
01689         if (this->editable) {
01690           this->avs->AssignSizePosition(sizing, x + this->avs->padding_left, y + given_height - avs_height - this->avs->padding_bottom, avs_width, avs_height, rtl);
01691         } else {
01692           this->avs->AssignSizePosition(sizing, 0, 0, this->avs->smallest_x, this->avs->smallest_y, rtl);
01693         }
01694       } else {
01695         this->acs->AssignSizePosition(sizing, x + this->acs->padding_left, y + this->acs->padding_top, acs_width, acs_height, rtl);
01696         if (this->editable) {
01697           this->avs->AssignSizePosition(sizing, x + this->avs->padding_left, y + given_height - avs_height - this->avs->padding_bottom, avs_width, avs_height, rtl);
01698         } else {
01699           this->avs->AssignSizePosition(sizing, 0, 0, this->avs->smallest_x, this->avs->smallest_y, rtl);
01700         }
01701         uint dx = this->acs->current_x + this->acs->padding_left + this->acs->padding_right;
01702         if (this->editable) {
01703           dx = max(dx, this->avs->current_x + this->avs->padding_left + this->avs->padding_right);
01704         }
01705         x += dx + INTER_COLUMN_SPACING + this->inf->padding_left;
01706         this->inf->AssignSizePosition(sizing, x, y + this->inf->padding_top, inf_width, inf_height, rtl);
01707       }
01708     }
01709   }
01710 
01711   virtual NWidgetCore *GetWidgetFromPos(int x, int y)
01712   {
01713     if (!IsInsideBS(x, this->pos_x, this->current_x) || !IsInsideBS(y, this->pos_y, this->current_y)) return NULL;
01714 
01715     NWidgetCore *nw = (this->editable) ? this->avs->GetWidgetFromPos(x, y) : NULL;
01716     if (nw == NULL) nw = this->acs->GetWidgetFromPos(x, y);
01717     if (nw == NULL) nw = this->inf->GetWidgetFromPos(x, y);
01718     return nw;
01719   }
01720 
01721   virtual void Draw(const Window *w)
01722   {
01723     if (this->editable) this->avs->Draw(w);
01724     this->acs->Draw(w);
01725     this->inf->Draw(w);
01726   }
01727 };
01728 
01729 const uint NWidgetNewGRFDisplay::INTER_LIST_SPACING      = WD_RESIZEBOX_WIDTH + 1;
01730 const uint NWidgetNewGRFDisplay::INTER_COLUMN_SPACING    = WD_RESIZEBOX_WIDTH;
01731 const uint NWidgetNewGRFDisplay::MAX_EXTRA_INFO_WIDTH    = 150;
01732 const uint NWidgetNewGRFDisplay::MIN_EXTRA_FOR_3_COLUMNS = 50;
01733 
01734 static const NWidgetPart _nested_newgrf_actives_widgets[] = {
01735   /* Left side, presets. */
01736   NWidget(NWID_HORIZONTAL),
01737     NWidget(WWT_TEXT, COLOUR_MAUVE), SetDataTip(STR_NEWGRF_SETTINGS_SELECT_PRESET, STR_NULL),
01738         SetPadding(0, WD_FRAMETEXT_RIGHT, 0, 0),
01739     NWidget(WWT_DROPDOWN, COLOUR_YELLOW, WID_NS_PRESET_LIST), SetFill(1, 0), SetResize(1, 0),
01740         SetDataTip(STR_JUST_STRING, STR_NEWGRF_SETTINGS_PRESET_LIST_TOOLTIP),
01741   EndContainer(),
01742   NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
01743     NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_PRESET_SAVE), SetFill(1, 0), SetResize(1, 0),
01744         SetDataTip(STR_NEWGRF_SETTINGS_PRESET_SAVE, STR_NEWGRF_SETTINGS_PRESET_SAVE_TOOLTIP),
01745     NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_PRESET_DELETE), SetFill(1, 0), SetResize(1, 0),
01746         SetDataTip(STR_NEWGRF_SETTINGS_PRESET_DELETE, STR_NEWGRF_SETTINGS_PRESET_DELETE_TOOLTIP),
01747   EndContainer(),
01748 
01749   NWidget(NWID_SPACER), SetMinimalSize(0, WD_RESIZEBOX_WIDTH), SetResize(1, 0), SetFill(1, 0),
01750   NWidget(WWT_PANEL, COLOUR_MAUVE),
01751     NWidget(WWT_LABEL, COLOUR_MAUVE), SetDataTip(STR_NEWGRF_SETTINGS_ACTIVE_LIST, STR_NULL),
01752         SetFill(1, 0), SetResize(1, 0), SetPadding(3, WD_FRAMETEXT_RIGHT, 0, WD_FRAMETEXT_LEFT),
01753     /* Left side, active grfs. */
01754     NWidget(NWID_HORIZONTAL), SetPadding(0, 2, 0, 2),
01755       NWidget(WWT_PANEL, COLOUR_MAUVE),
01756         NWidget(WWT_INSET, COLOUR_MAUVE, WID_NS_FILE_LIST), SetMinimalSize(100, 1), SetPadding(2, 2, 2, 2),
01757             SetFill(1, 1), SetResize(1, 1), SetScrollbar(WID_NS_SCROLLBAR), SetDataTip(STR_NULL, STR_NEWGRF_SETTINGS_FILE_TOOLTIP),
01758         EndContainer(),
01759       EndContainer(),
01760       NWidget(NWID_VSCROLLBAR, COLOUR_MAUVE, WID_NS_SCROLLBAR),
01761     EndContainer(),
01762     /* Buttons. */
01763     NWidget(NWID_SELECTION, INVALID_COLOUR, WID_NS_SHOW_REMOVE),
01764       NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPadding(2, 2, 2, 2), SetPIP(0, WD_RESIZEBOX_WIDTH, 0),
01765         NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_REMOVE), SetFill(1, 0), SetResize(1, 0),
01766             SetDataTip(STR_NEWGRF_SETTINGS_REMOVE, STR_NEWGRF_SETTINGS_REMOVE_TOOLTIP),
01767         NWidget(NWID_VERTICAL),
01768           NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_MOVE_UP), SetFill(1, 0), SetResize(1, 0),
01769               SetDataTip(STR_NEWGRF_SETTINGS_MOVEUP, STR_NEWGRF_SETTINGS_MOVEUP_TOOLTIP),
01770           NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_MOVE_DOWN), SetFill(1, 0), SetResize(1, 0),
01771               SetDataTip(STR_NEWGRF_SETTINGS_MOVEDOWN, STR_NEWGRF_SETTINGS_MOVEDOWN_TOOLTIP),
01772         EndContainer(),
01773       EndContainer(),
01774 
01775       NWidget(NWID_VERTICAL, NC_EQUALSIZE), SetPadding(2, 2, 2, 2),
01776         NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_RESCAN_FILES2), SetFill(1, 0), SetResize(1, 0),
01777             SetDataTip(STR_NEWGRF_SETTINGS_RESCAN_FILES, STR_NEWGRF_SETTINGS_RESCAN_FILES_TOOLTIP),
01778         NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_CONTENT_DOWNLOAD2), SetFill(1, 0), SetResize(1, 0),
01779             SetDataTip(STR_INTRO_ONLINE_CONTENT, STR_INTRO_TOOLTIP_ONLINE_CONTENT),
01780       EndContainer(),
01781     EndContainer(),
01782   EndContainer(),
01783 };
01784 
01785 static const NWidgetPart _nested_newgrf_availables_widgets[] = {
01786   NWidget(WWT_PANEL, COLOUR_MAUVE),
01787     NWidget(WWT_LABEL, COLOUR_MAUVE), SetDataTip(STR_NEWGRF_SETTINGS_INACTIVE_LIST, STR_NULL),
01788         SetFill(1, 0), SetResize(1, 0), SetPadding(3, WD_FRAMETEXT_RIGHT, 0, WD_FRAMETEXT_LEFT),
01789     /* Left side, available grfs, filter edit box. */
01790     NWidget(NWID_HORIZONTAL), SetPadding(WD_TEXTPANEL_TOP, 0, WD_TEXTPANEL_BOTTOM, 0),
01791         SetPIP(WD_FRAMETEXT_LEFT, WD_FRAMETEXT_RIGHT, WD_FRAMETEXT_RIGHT),
01792       NWidget(WWT_TEXT, COLOUR_MAUVE), SetFill(0, 1), SetDataTip(STR_NEWGRF_FILTER_TITLE, STR_NULL),
01793       NWidget(WWT_EDITBOX, COLOUR_MAUVE, WID_NS_FILTER), SetFill(1, 0), SetMinimalSize(50, 12), SetResize(1, 0),
01794           SetDataTip(STR_LIST_FILTER_OSKTITLE, STR_LIST_FILTER_TOOLTIP),
01795     EndContainer(),
01796     /* Left side, available grfs. */
01797     NWidget(NWID_HORIZONTAL), SetPadding(0, 2, 0, 2),
01798       NWidget(WWT_PANEL, COLOUR_MAUVE),
01799         NWidget(WWT_INSET, COLOUR_MAUVE, WID_NS_AVAIL_LIST), SetMinimalSize(100, 1), SetPadding(2, 2, 2, 2),
01800             SetFill(1, 1), SetResize(1, 1), SetScrollbar(WID_NS_SCROLL2BAR),
01801         EndContainer(),
01802       EndContainer(),
01803       NWidget(NWID_VSCROLLBAR, COLOUR_MAUVE, WID_NS_SCROLL2BAR),
01804     EndContainer(),
01805     /* Left side, available grfs, buttons. */
01806     NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPadding(2, 2, 2, 2), SetPIP(0, WD_RESIZEBOX_WIDTH, 0),
01807       NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_ADD), SetFill(1, 0), SetResize(1, 0),
01808           SetDataTip(STR_NEWGRF_SETTINGS_ADD, STR_NEWGRF_SETTINGS_ADD_FILE_TOOLTIP),
01809       NWidget(NWID_VERTICAL),
01810         NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_RESCAN_FILES), SetFill(1, 0), SetResize(1, 0),
01811             SetDataTip(STR_NEWGRF_SETTINGS_RESCAN_FILES, STR_NEWGRF_SETTINGS_RESCAN_FILES_TOOLTIP),
01812         NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_CONTENT_DOWNLOAD), SetFill(1, 0), SetResize(1, 0),
01813             SetDataTip(STR_INTRO_ONLINE_CONTENT, STR_INTRO_TOOLTIP_ONLINE_CONTENT),
01814       EndContainer(),
01815     EndContainer(),
01816   EndContainer(),
01817 };
01818 
01819 static const NWidgetPart _nested_newgrf_infopanel_widgets[] = {
01820   /* Right side, info panel. */
01821   NWidget(NWID_VERTICAL), SetPadding(0, 0, 2, 0),
01822     NWidget(WWT_PANEL, COLOUR_MAUVE), SetPadding(0, 0, 2, 0),
01823       NWidget(WWT_EMPTY, COLOUR_MAUVE, WID_NS_NEWGRF_INFO_TITLE), SetFill(1, 0), SetResize(1, 0),
01824       NWidget(WWT_EMPTY, COLOUR_MAUVE, WID_NS_NEWGRF_INFO), SetFill(1, 1), SetResize(1, 1), SetMinimalSize(150, 100),
01825     EndContainer(),
01826     NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_OPEN_URL), SetFill(1, 0), SetResize(1, 0),
01827         SetDataTip(STR_CONTENT_OPEN_URL, STR_CONTENT_OPEN_URL_TOOLTIP),
01828     NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_NEWGRF_TEXTFILE + TFT_README), SetFill(1, 0), SetResize(1, 0),
01829         SetDataTip(STR_TEXTFILE_VIEW_README, STR_NULL),
01830     NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
01831       NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_NEWGRF_TEXTFILE + TFT_CHANGELOG), SetFill(1, 0), SetResize(1, 0),
01832           SetDataTip(STR_TEXTFILE_VIEW_CHANGELOG, STR_NULL),
01833       NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_NEWGRF_TEXTFILE + TFT_LICENSE), SetFill(1, 0), SetResize(1, 0),
01834           SetDataTip(STR_TEXTFILE_VIEW_LICENCE, STR_NULL),
01835     EndContainer(),
01836   EndContainer(),
01837   NWidget(NWID_SELECTION, INVALID_COLOUR, WID_NS_SHOW_APPLY),
01838     /* Right side, buttons. */
01839     NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(0, WD_RESIZEBOX_WIDTH, 0),
01840       NWidget(NWID_VERTICAL),
01841         NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_SET_PARAMETERS), SetFill(1, 0), SetResize(1, 0),
01842             SetDataTip(STR_NEWGRF_SETTINGS_SET_PARAMETERS, STR_NULL),
01843         NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_TOGGLE_PALETTE), SetFill(1, 0), SetResize(1, 0),
01844             SetDataTip(STR_NEWGRF_SETTINGS_TOGGLE_PALETTE, STR_NEWGRF_SETTINGS_TOGGLE_PALETTE_TOOLTIP),
01845       EndContainer(),
01846       NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_APPLY_CHANGES), SetFill(1, 0), SetResize(1, 0),
01847           SetDataTip(STR_NEWGRF_SETTINGS_APPLY_CHANGES, STR_NULL),
01848     EndContainer(),
01849     NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_NS_VIEW_PARAMETERS), SetFill(1, 0), SetResize(1, 0),
01850       SetDataTip(STR_NEWGRF_SETTINGS_SHOW_PARAMETERS, STR_NULL),
01851   EndContainer(),
01852 };
01853 
01855 NWidgetBase* NewGRFDisplay(int *biggest_index)
01856 {
01857   NWidgetBase *avs = MakeNWidgets(_nested_newgrf_availables_widgets, lengthof(_nested_newgrf_availables_widgets), biggest_index, NULL);
01858 
01859   int biggest2;
01860   NWidgetBase *acs = MakeNWidgets(_nested_newgrf_actives_widgets, lengthof(_nested_newgrf_actives_widgets), &biggest2, NULL);
01861   *biggest_index = max(*biggest_index, biggest2);
01862 
01863   NWidgetBase *inf = MakeNWidgets(_nested_newgrf_infopanel_widgets, lengthof(_nested_newgrf_infopanel_widgets), &biggest2, NULL);
01864   *biggest_index = max(*biggest_index, biggest2);
01865 
01866   return new NWidgetNewGRFDisplay(avs, acs, inf);
01867 }
01868 
01869 /* Widget definition of the manage newgrfs window */
01870 static const NWidgetPart _nested_newgrf_widgets[] = {
01871   NWidget(NWID_HORIZONTAL),
01872     NWidget(WWT_CLOSEBOX, COLOUR_MAUVE),
01873     NWidget(WWT_CAPTION, COLOUR_MAUVE), SetDataTip(STR_NEWGRF_SETTINGS_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
01874   EndContainer(),
01875   NWidget(WWT_PANEL, COLOUR_MAUVE),
01876     NWidgetFunction(NewGRFDisplay), SetPadding(WD_RESIZEBOX_WIDTH, WD_RESIZEBOX_WIDTH, 2, WD_RESIZEBOX_WIDTH),
01877     /* Resize button. */
01878     NWidget(NWID_HORIZONTAL),
01879       NWidget(NWID_SPACER), SetFill(1, 0), SetResize(1, 0),
01880       NWidget(WWT_RESIZEBOX, COLOUR_MAUVE),
01881     EndContainer(),
01882   EndContainer(),
01883 };
01884 
01885 /* Window definition of the manage newgrfs window */
01886 static const WindowDesc _newgrf_desc(
01887   WDP_CENTER, 300, 263,
01888   WC_GAME_OPTIONS, WC_NONE,
01889   WDF_UNCLICK_BUTTONS,
01890   _nested_newgrf_widgets, lengthof(_nested_newgrf_widgets)
01891 );
01892 
01898 static void NewGRFConfirmationCallback(Window *w, bool confirmed)
01899 {
01900   if (confirmed) {
01901     DeleteWindowByClass(WC_GRF_PARAMETERS);
01902     NewGRFWindow *nw = dynamic_cast<NewGRFWindow*>(w);
01903 
01904     GamelogStartAction(GLAT_GRF);
01905     GamelogGRFUpdate(_grfconfig, nw->actives); // log GRF changes
01906     CopyGRFConfigList(nw->orig_list, nw->actives, false);
01907     ReloadNewGRFData();
01908     GamelogStopAction();
01909 
01910     /* Show new, updated list */
01911     GRFConfig *c;
01912     int i = 0;
01913     for (c = nw->actives; c != NULL && c != nw->active_sel; c = c->next, i++) {}
01914     CopyGRFConfigList(&nw->actives, *nw->orig_list, false);
01915     for (c = nw->actives; c != NULL && i > 0; c = c->next, i--) {}
01916     nw->active_sel = c;
01917     nw->avails.ForceRebuild();
01918 
01919     w->InvalidateData();
01920 
01921     ReInitAllWindows();
01922     DeleteWindowByClass(WC_BUILD_OBJECT);
01923   }
01924 }
01925 
01926 
01927 
01936 void ShowNewGRFSettings(bool editable, bool show_params, bool exec_changes, GRFConfig **config)
01937 {
01938   DeleteWindowByClass(WC_GAME_OPTIONS);
01939   new NewGRFWindow(&_newgrf_desc, editable, show_params, exec_changes, config);
01940 }
01941 
01943 static const NWidgetPart _nested_scan_progress_widgets[] = {
01944   NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_NEWGRF_SCAN_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
01945   NWidget(WWT_PANEL, COLOUR_GREY),
01946     NWidget(NWID_HORIZONTAL), SetPIP(20, 0, 20),
01947       NWidget(NWID_VERTICAL), SetPIP(11, 8, 11),
01948         NWidget(WWT_LABEL, INVALID_COLOUR), SetDataTip(STR_NEWGRF_SCAN_MESSAGE, STR_NULL), SetFill(1, 0),
01949         NWidget(WWT_EMPTY, INVALID_COLOUR, WID_SP_PROGRESS_BAR), SetFill(1, 0),
01950         NWidget(WWT_EMPTY, INVALID_COLOUR, WID_SP_PROGRESS_TEXT), SetFill(1, 0),
01951       EndContainer(),
01952     EndContainer(),
01953   EndContainer(),
01954 };
01955 
01957 static const WindowDesc _scan_progress_desc(
01958   WDP_CENTER, 0, 0,
01959   WC_MODAL_PROGRESS, WC_NONE,
01960   WDF_UNCLICK_BUTTONS,
01961   _nested_scan_progress_widgets, lengthof(_nested_scan_progress_widgets)
01962 );
01963 
01965 struct ScanProgressWindow : public Window {
01966   char *last_name; 
01967   int scanned;     
01968 
01970   ScanProgressWindow() : Window(), last_name(NULL), scanned(0)
01971   {
01972     this->InitNested(&_scan_progress_desc, 1);
01973   }
01974 
01976   ~ScanProgressWindow()
01977   {
01978     free(last_name);
01979   }
01980 
01981   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
01982   {
01983     switch (widget) {
01984       case WID_SP_PROGRESS_BAR: {
01985         SetDParam(0, 100);
01986         *size = GetStringBoundingBox(STR_GENERATION_PROGRESS);
01987         /* We need some spacing for the 'border' */
01988         size->height += 8;
01989         size->width += 8;
01990         break;
01991       }
01992 
01993       case WID_SP_PROGRESS_TEXT:
01994         SetDParam(0, 9999);
01995         SetDParam(1, 9999);
01996         /* We really don't know the width. We could determine it by scanning the NewGRFs,
01997          * but this is the status window for scanning them... */
01998         size->width = max(400U, GetStringBoundingBox(STR_NEWGRF_SCAN_STATUS).width);
01999         size->height = FONT_HEIGHT_NORMAL * 2 + WD_PAR_VSEP_NORMAL;
02000         break;
02001     }
02002   }
02003 
02004   virtual void DrawWidget(const Rect &r, int widget) const
02005   {
02006     switch (widget) {
02007       case WID_SP_PROGRESS_BAR: {
02008         /* Draw the % complete with a bar and a text */
02009         DrawFrameRect(r.left, r.top, r.right, r.bottom, COLOUR_GREY, FR_BORDERONLY);
02010         uint percent = scanned * 100 / max(1U, _settings_client.gui.last_newgrf_count);
02011         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);
02012         SetDParam(0, percent);
02013         DrawString(r.left, r.right, r.top + 5, STR_GENERATION_PROGRESS, TC_FROMSTRING, SA_HOR_CENTER);
02014         break;
02015       }
02016 
02017       case WID_SP_PROGRESS_TEXT:
02018         SetDParam(0, this->scanned);
02019         SetDParam(1, _settings_client.gui.last_newgrf_count);
02020         DrawString(r.left, r.right, r.top, STR_NEWGRF_SCAN_STATUS, TC_FROMSTRING, SA_HOR_CENTER);
02021 
02022         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);
02023         break;
02024     }
02025   }
02026 
02032   void UpdateNewGRFScanStatus(uint num, const char *name)
02033   {
02034     free(this->last_name);
02035     if (name == NULL) {
02036       char buf[256];
02037       GetString(buf, STR_NEWGRF_SCAN_ARCHIVES, lastof(buf));
02038       this->last_name = strdup(buf);
02039     } else {
02040       this->last_name = strdup(name);
02041     }
02042     this->scanned = num;
02043     if (num > _settings_client.gui.last_newgrf_count) _settings_client.gui.last_newgrf_count = num;
02044 
02045     this->SetDirty();
02046   }
02047 };
02048 
02054 void UpdateNewGRFScanStatus(uint num, const char *name)
02055 {
02056   ScanProgressWindow *w  = dynamic_cast<ScanProgressWindow *>(FindWindowByClass(WC_MODAL_PROGRESS));
02057   if (w == NULL) w = new ScanProgressWindow();
02058   w->UpdateNewGRFScanStatus(num, name);
02059 }