settings_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 "currency.h"
00014 #include "error.h"
00015 #include "gui.h"
00016 #include "textbuf_gui.h"
00017 #include "command_func.h"
00018 #include "screenshot.h"
00019 #include "network/network.h"
00020 #include "town.h"
00021 #include "settings_internal.h"
00022 #include "newgrf_townname.h"
00023 #include "strings_func.h"
00024 #include "window_func.h"
00025 #include "string_func.h"
00026 #include "widgets/dropdown_type.h"
00027 #include "widgets/dropdown_func.h"
00028 #include "highscore.h"
00029 #include "base_media_base.h"
00030 #include "company_base.h"
00031 #include "company_func.h"
00032 #include "viewport_func.h"
00033 #include "core/geometry_func.hpp"
00034 #include "ai/ai.hpp"
00035 #include "language.h"
00036 
00037 #include "table/sprites.h"
00038 #include "table/strings.h"
00039 #include <map>
00040 
00041 static const StringID _units_dropdown[] = {
00042   STR_GAME_OPTIONS_MEASURING_UNITS_IMPERIAL,
00043   STR_GAME_OPTIONS_MEASURING_UNITS_METRIC,
00044   STR_GAME_OPTIONS_MEASURING_UNITS_SI,
00045   INVALID_STRING_ID
00046 };
00047 
00048 static const StringID _driveside_dropdown[] = {
00049   STR_GAME_OPTIONS_ROAD_VEHICLES_DROPDOWN_LEFT,
00050   STR_GAME_OPTIONS_ROAD_VEHICLES_DROPDOWN_RIGHT,
00051   INVALID_STRING_ID
00052 };
00053 
00054 static const StringID _autosave_dropdown[] = {
00055   STR_GAME_OPTIONS_AUTOSAVE_DROPDOWN_OFF,
00056   STR_GAME_OPTIONS_AUTOSAVE_DROPDOWN_EVERY_1_MONTH,
00057   STR_GAME_OPTIONS_AUTOSAVE_DROPDOWN_EVERY_3_MONTHS,
00058   STR_GAME_OPTIONS_AUTOSAVE_DROPDOWN_EVERY_6_MONTHS,
00059   STR_GAME_OPTIONS_AUTOSAVE_DROPDOWN_EVERY_12_MONTHS,
00060   INVALID_STRING_ID,
00061 };
00062 
00063 int _nb_orig_names = SPECSTR_TOWNNAME_LAST - SPECSTR_TOWNNAME_START + 1; 
00064 static StringID *_grf_names = NULL; 
00065 static int _nb_grf_names = 0;       
00066 
00068 void InitGRFTownGeneratorNames()
00069 {
00070   free(_grf_names);
00071   _grf_names = GetGRFTownNameList();
00072   _nb_grf_names = 0;
00073   for (StringID *s = _grf_names; *s != INVALID_STRING_ID; s++) _nb_grf_names++;
00074 }
00075 
00081 static inline StringID TownName(int town_name)
00082 {
00083   if (town_name < _nb_orig_names) return STR_GAME_OPTIONS_TOWN_NAME_ORIGINAL_ENGLISH + town_name;
00084   town_name -= _nb_orig_names;
00085   if (town_name < _nb_grf_names) return _grf_names[town_name];
00086   return STR_UNDEFINED;
00087 }
00088 
00093 static int GetCurRes()
00094 {
00095   int i;
00096 
00097   for (i = 0; i != _num_resolutions; i++) {
00098     if ((int)_resolutions[i].width == _screen.width &&
00099         (int)_resolutions[i].height == _screen.height) {
00100       break;
00101     }
00102   }
00103   return i;
00104 }
00105 
00107 enum GameOptionsWidgets {
00108   GOW_BACKGROUND,             
00109   GOW_CURRENCY_DROPDOWN,      
00110   GOW_DISTANCE_DROPDOWN,      
00111   GOW_ROADSIDE_DROPDOWN,      
00112   GOW_TOWNNAME_DROPDOWN,      
00113   GOW_AUTOSAVE_DROPDOWN,      
00114   GOW_LANG_DROPDOWN,          
00115   GOW_RESOLUTION_DROPDOWN,    
00116   GOW_FULLSCREEN_BUTTON,      
00117   GOW_SCREENSHOT_DROPDOWN,    
00118   GOW_BASE_GRF_DROPDOWN,      
00119   GOW_BASE_GRF_STATUS,        
00120   GOW_BASE_GRF_DESCRIPTION,   
00121   GOW_BASE_SFX_DROPDOWN,      
00122   GOW_BASE_SFX_DESCRIPTION,   
00123   GOW_BASE_MUSIC_DROPDOWN,    
00124   GOW_BASE_MUSIC_STATUS,      
00125   GOW_BASE_MUSIC_DESCRIPTION, 
00126 };
00127 
00128 static void ShowCustCurrency();
00129 
00130 template <class T>
00131 static DropDownList *BuiltSetDropDownList(int *selected_index)
00132 {
00133   int n = T::GetNumSets();
00134   *selected_index = T::GetIndexOfUsedSet();
00135 
00136   DropDownList *list = new DropDownList();
00137   for (int i = 0; i < n; i++) {
00138     list->push_back(new DropDownListCharStringItem(T::GetSet(i)->name, i, (_game_mode == GM_MENU) ? false : (*selected_index != i)));
00139   }
00140 
00141   return list;
00142 }
00143 
00144 struct GameOptionsWindow : Window {
00145   GameSettings *opt;
00146   bool reload;
00147 
00148   GameOptionsWindow(const WindowDesc *desc) : Window()
00149   {
00150     this->opt = &GetGameSettings();
00151     this->reload = false;
00152 
00153     this->InitNested(desc);
00154     this->OnInvalidateData(0);
00155   }
00156 
00157   ~GameOptionsWindow()
00158   {
00159     DeleteWindowById(WC_CUSTOM_CURRENCY, 0);
00160     if (this->reload) _switch_mode = SM_MENU;
00161   }
00162 
00169   DropDownList *BuildDropDownList(int widget, int *selected_index) const
00170   {
00171     DropDownList *list = NULL;
00172     switch (widget) {
00173       case GOW_CURRENCY_DROPDOWN: { // Setup currencies dropdown
00174         list = new DropDownList();
00175         *selected_index = this->opt->locale.currency;
00176         StringID *items = BuildCurrencyDropdown();
00177         uint disabled = _game_mode == GM_MENU ? 0 : ~GetMaskOfAllowedCurrencies();
00178         int custom_index = -1;
00179 
00180         /* Add non-custom currencies; sorted naturally */
00181         for (uint i = 0; *items != INVALID_STRING_ID; items++, i++) {
00182           if (*items == STR_GAME_OPTIONS_CURRENCY_CUSTOM) {
00183             custom_index = i;
00184           } else {
00185             list->push_back(new DropDownListStringItem(*items, i, HasBit(disabled, i)));
00186           }
00187         }
00188         list->sort(DropDownListStringItem::NatSortFunc);
00189 
00190         /* Append custom currency at the end */
00191         if (custom_index >= 0) {
00192           list->push_back(new DropDownListItem(-1, false)); // separator line
00193           list->push_back(new DropDownListStringItem(STR_GAME_OPTIONS_CURRENCY_CUSTOM, custom_index, HasBit(disabled, custom_index)));
00194         }
00195         break;
00196       }
00197 
00198       case GOW_DISTANCE_DROPDOWN: { // Setup distance unit dropdown
00199         list = new DropDownList();
00200         *selected_index = this->opt->locale.units;
00201         const StringID *items = _units_dropdown;
00202         for (uint i = 0; *items != INVALID_STRING_ID; items++, i++) {
00203           list->push_back(new DropDownListStringItem(*items, i, false));
00204         }
00205         break;
00206       }
00207 
00208       case GOW_ROADSIDE_DROPDOWN: { // Setup road-side dropdown
00209         list = new DropDownList();
00210         *selected_index = this->opt->vehicle.road_side;
00211         const StringID *items = _driveside_dropdown;
00212         uint disabled = 0;
00213 
00214         /* You can only change the drive side if you are in the menu or ingame with
00215          * no vehicles present. In a networking game only the server can change it */
00216         extern bool RoadVehiclesAreBuilt();
00217         if ((_game_mode != GM_MENU && RoadVehiclesAreBuilt()) || (_networking && !_network_server)) {
00218           disabled = ~(1 << this->opt->vehicle.road_side); // disable the other value
00219         }
00220 
00221         for (uint i = 0; *items != INVALID_STRING_ID; items++, i++) {
00222           list->push_back(new DropDownListStringItem(*items, i, HasBit(disabled, i)));
00223         }
00224         break;
00225       }
00226 
00227       case GOW_TOWNNAME_DROPDOWN: { // Setup townname dropdown
00228         list = new DropDownList();
00229         *selected_index = this->opt->game_creation.town_name;
00230 
00231         int enabled_item = (_game_mode == GM_MENU || Town::GetNumItems() == 0) ? -1 : *selected_index;
00232 
00233         /* Add and sort original townnames generators */
00234         for (int i = 0; i < _nb_orig_names; i++) {
00235           list->push_back(new DropDownListStringItem(STR_GAME_OPTIONS_TOWN_NAME_ORIGINAL_ENGLISH + i, i, enabled_item != i && enabled_item >= 0));
00236         }
00237         list->sort(DropDownListStringItem::NatSortFunc);
00238 
00239         /* Add and sort newgrf townnames generators */
00240         DropDownList newgrf_names;
00241         for (int i = 0; i < _nb_grf_names; i++) {
00242           int result = _nb_orig_names + i;
00243           newgrf_names.push_back(new DropDownListStringItem(_grf_names[i], result, enabled_item != result && enabled_item >= 0));
00244         }
00245         newgrf_names.sort(DropDownListStringItem::NatSortFunc);
00246 
00247         /* Insert newgrf_names at the top of the list */
00248         if (newgrf_names.size() > 0) {
00249           newgrf_names.push_back(new DropDownListItem(-1, false)); // separator line
00250           list->splice(list->begin(), newgrf_names);
00251         }
00252         break;
00253       }
00254 
00255       case GOW_AUTOSAVE_DROPDOWN: { // Setup autosave dropdown
00256         list = new DropDownList();
00257         *selected_index = _settings_client.gui.autosave;
00258         const StringID *items = _autosave_dropdown;
00259         for (uint i = 0; *items != INVALID_STRING_ID; items++, i++) {
00260           list->push_back(new DropDownListStringItem(*items, i, false));
00261         }
00262         break;
00263       }
00264 
00265       case GOW_LANG_DROPDOWN: { // Setup interface language dropdown
00266         list = new DropDownList();
00267         for (uint i = 0; i < _languages.Length(); i++) {
00268           if (&_languages[i] == _current_language) *selected_index = i;
00269           list->push_back(new DropDownListStringItem(SPECSTR_LANGUAGE_START + i, i, false));
00270         }
00271         list->sort(DropDownListStringItem::NatSortFunc);
00272         break;
00273       }
00274 
00275       case GOW_RESOLUTION_DROPDOWN: // Setup resolution dropdown
00276         list = new DropDownList();
00277         *selected_index = GetCurRes();
00278         for (int i = 0; i < _num_resolutions; i++) {
00279           list->push_back(new DropDownListStringItem(SPECSTR_RESOLUTION_START + i, i, false));
00280         }
00281         break;
00282 
00283       case GOW_SCREENSHOT_DROPDOWN: // Setup screenshot format dropdown
00284         list = new DropDownList();
00285         *selected_index = _cur_screenshot_format;
00286         for (uint i = 0; i < _num_screenshot_formats; i++) {
00287           list->push_back(new DropDownListStringItem(SPECSTR_SCREENSHOT_START + i, i, false));
00288         }
00289         break;
00290 
00291       case GOW_BASE_GRF_DROPDOWN:
00292         list = BuiltSetDropDownList<BaseGraphics>(selected_index);
00293         break;
00294 
00295       case GOW_BASE_SFX_DROPDOWN:
00296         list = BuiltSetDropDownList<BaseSounds>(selected_index);
00297         break;
00298 
00299       case GOW_BASE_MUSIC_DROPDOWN:
00300         list = BuiltSetDropDownList<BaseMusic>(selected_index);
00301         break;
00302 
00303       default:
00304         return NULL;
00305     }
00306 
00307     return list;
00308   }
00309 
00310   virtual void SetStringParameters(int widget) const
00311   {
00312     switch (widget) {
00313       case GOW_CURRENCY_DROPDOWN:   SetDParam(0, _currency_specs[this->opt->locale.currency].name); break;
00314       case GOW_DISTANCE_DROPDOWN:   SetDParam(0, STR_GAME_OPTIONS_MEASURING_UNITS_IMPERIAL + this->opt->locale.units); break;
00315       case GOW_ROADSIDE_DROPDOWN:   SetDParam(0, STR_GAME_OPTIONS_ROAD_VEHICLES_DROPDOWN_LEFT + this->opt->vehicle.road_side); break;
00316       case GOW_TOWNNAME_DROPDOWN:   SetDParam(0, TownName(this->opt->game_creation.town_name)); break;
00317       case GOW_AUTOSAVE_DROPDOWN:   SetDParam(0, _autosave_dropdown[_settings_client.gui.autosave]); break;
00318       case GOW_LANG_DROPDOWN:       SetDParamStr(0, _current_language->own_name); break;
00319       case GOW_RESOLUTION_DROPDOWN: SetDParam(0, GetCurRes() == _num_resolutions ? STR_GAME_OPTIONS_RESOLUTION_OTHER : SPECSTR_RESOLUTION_START + GetCurRes()); break;
00320       case GOW_SCREENSHOT_DROPDOWN: SetDParam(0, SPECSTR_SCREENSHOT_START + _cur_screenshot_format); break;
00321       case GOW_BASE_GRF_DROPDOWN:   SetDParamStr(0, BaseGraphics::GetUsedSet()->name); break;
00322       case GOW_BASE_GRF_STATUS:     SetDParam(0, BaseGraphics::GetUsedSet()->GetNumInvalid()); break;
00323       case GOW_BASE_SFX_DROPDOWN:   SetDParamStr(0, BaseSounds::GetUsedSet()->name); break;
00324       case GOW_BASE_MUSIC_DROPDOWN: SetDParamStr(0, BaseMusic::GetUsedSet()->name); break;
00325       case GOW_BASE_MUSIC_STATUS:   SetDParam(0, BaseMusic::GetUsedSet()->GetNumInvalid()); break;
00326     }
00327   }
00328 
00329   virtual void DrawWidget(const Rect &r, int widget) const
00330   {
00331     switch (widget) {
00332       case GOW_BASE_GRF_DESCRIPTION:
00333         SetDParamStr(0, BaseGraphics::GetUsedSet()->GetDescription(GetCurrentLanguageIsoCode()));
00334         DrawStringMultiLine(r.left, r.right, r.top, UINT16_MAX, STR_BLACK_RAW_STRING);
00335         break;
00336 
00337       case GOW_BASE_SFX_DESCRIPTION:
00338         SetDParamStr(0, BaseSounds::GetUsedSet()->GetDescription(GetCurrentLanguageIsoCode()));
00339         DrawStringMultiLine(r.left, r.right, r.top, UINT16_MAX, STR_BLACK_RAW_STRING);
00340         break;
00341 
00342       case GOW_BASE_MUSIC_DESCRIPTION:
00343         SetDParamStr(0, BaseMusic::GetUsedSet()->GetDescription(GetCurrentLanguageIsoCode()));
00344         DrawStringMultiLine(r.left, r.right, r.top, UINT16_MAX, STR_BLACK_RAW_STRING);
00345         break;
00346     }
00347   }
00348 
00349   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00350   {
00351     switch (widget) {
00352       case GOW_BASE_GRF_DESCRIPTION:
00353         /* Find the biggest description for the default size. */
00354         for (int i = 0; i < BaseGraphics::GetNumSets(); i++) {
00355           SetDParamStr(0, BaseGraphics::GetSet(i)->GetDescription(GetCurrentLanguageIsoCode()));
00356           size->height = max(size->height, (uint)GetStringHeight(STR_BLACK_RAW_STRING, size->width));
00357         }
00358         break;
00359 
00360       case GOW_BASE_GRF_STATUS:
00361         /* Find the biggest description for the default size. */
00362         for (int i = 0; i < BaseGraphics::GetNumSets(); i++) {
00363           uint invalid_files = BaseGraphics::GetSet(i)->GetNumInvalid();
00364           if (invalid_files == 0) continue;
00365 
00366           SetDParam(0, invalid_files);
00367           *size = maxdim(*size, GetStringBoundingBox(STR_GAME_OPTIONS_BASE_GRF_STATUS));
00368         }
00369         break;
00370 
00371       case GOW_BASE_SFX_DESCRIPTION:
00372         /* Find the biggest description for the default size. */
00373         for (int i = 0; i < BaseSounds::GetNumSets(); i++) {
00374           SetDParamStr(0, BaseSounds::GetSet(i)->GetDescription(GetCurrentLanguageIsoCode()));
00375           size->height = max(size->height, (uint)GetStringHeight(STR_BLACK_RAW_STRING, size->width));
00376         }
00377         break;
00378 
00379       case GOW_BASE_MUSIC_DESCRIPTION:
00380         /* Find the biggest description for the default size. */
00381         for (int i = 0; i < BaseMusic::GetNumSets(); i++) {
00382           SetDParamStr(0, BaseMusic::GetSet(i)->GetDescription(GetCurrentLanguageIsoCode()));
00383           size->height = max(size->height, (uint)GetStringHeight(STR_BLACK_RAW_STRING, size->width));
00384         }
00385         break;
00386 
00387       case GOW_BASE_MUSIC_STATUS:
00388         /* Find the biggest description for the default size. */
00389         for (int i = 0; i < BaseMusic::GetNumSets(); i++) {
00390           uint invalid_files = BaseMusic::GetSet(i)->GetNumInvalid();
00391           if (invalid_files == 0) continue;
00392 
00393           SetDParam(0, invalid_files);
00394           *size = maxdim(*size, GetStringBoundingBox(STR_GAME_OPTIONS_BASE_MUSIC_STATUS));
00395         }
00396         break;
00397 
00398       default: {
00399         int selected;
00400         DropDownList *list = this->BuildDropDownList(widget, &selected);
00401         if (list != NULL) {
00402           /* Find the biggest item for the default size. */
00403           for (DropDownList::iterator it = list->begin(); it != list->end(); it++) {
00404             static const Dimension extra = {WD_DROPDOWNTEXT_LEFT + WD_DROPDOWNTEXT_RIGHT, WD_DROPDOWNTEXT_TOP + WD_DROPDOWNTEXT_BOTTOM};
00405             Dimension string_dim;
00406             int width = (*it)->Width();
00407             string_dim.width = width + extra.width;
00408             string_dim.height = (*it)->Height(width) + extra.height;
00409             *size = maxdim(*size, string_dim);
00410             delete *it;
00411           }
00412           delete list;
00413         }
00414       }
00415     }
00416   }
00417 
00418   virtual void OnClick(Point pt, int widget, int click_count)
00419   {
00420     switch (widget) {
00421       case GOW_FULLSCREEN_BUTTON: // Click fullscreen on/off
00422         /* try to toggle full-screen on/off */
00423         if (!ToggleFullScreen(!_fullscreen)) {
00424           ShowErrorMessage(STR_ERROR_FULLSCREEN_FAILED, INVALID_STRING_ID, WL_ERROR);
00425         }
00426         this->SetWidgetLoweredState(GOW_FULLSCREEN_BUTTON, _fullscreen);
00427         this->SetDirty();
00428         break;
00429 
00430       default: {
00431         int selected;
00432         DropDownList *list = this->BuildDropDownList(widget, &selected);
00433         if (list != NULL) {
00434           ShowDropDownList(this, list, selected, widget);
00435         }
00436         break;
00437       }
00438     }
00439   }
00440 
00446   template <class T>
00447   void SetMediaSet(int index)
00448   {
00449     if (_game_mode == GM_MENU) {
00450       const char *name = T::GetSet(index)->name;
00451 
00452       free(T::ini_set);
00453       T::ini_set = strdup(name);
00454 
00455       T::SetSet(name);
00456       this->reload = true;
00457       this->InvalidateData();
00458     }
00459   }
00460 
00461   virtual void OnDropdownSelect(int widget, int index)
00462   {
00463     switch (widget) {
00464       case GOW_CURRENCY_DROPDOWN: // Currency
00465         if (index == CUSTOM_CURRENCY_ID) ShowCustCurrency();
00466         this->opt->locale.currency = index;
00467         ReInitAllWindows();
00468         break;
00469 
00470       case GOW_DISTANCE_DROPDOWN: // Measuring units
00471         this->opt->locale.units = index;
00472         MarkWholeScreenDirty();
00473         break;
00474 
00475       case GOW_ROADSIDE_DROPDOWN: // Road side
00476         if (this->opt->vehicle.road_side != index) { // only change if setting changed
00477           uint i;
00478           if (GetSettingFromName("vehicle.road_side", &i) == NULL) NOT_REACHED();
00479           SetSettingValue(i, index);
00480           MarkWholeScreenDirty();
00481         }
00482         break;
00483 
00484       case GOW_TOWNNAME_DROPDOWN: // Town names
00485         if (_game_mode == GM_MENU || Town::GetNumItems() == 0) {
00486           this->opt->game_creation.town_name = index;
00487           SetWindowDirty(WC_GAME_OPTIONS, 0);
00488         }
00489         break;
00490 
00491       case GOW_AUTOSAVE_DROPDOWN: // Autosave options
00492         _settings_client.gui.autosave = index;
00493         this->SetDirty();
00494         break;
00495 
00496       case GOW_LANG_DROPDOWN: // Change interface language
00497         ReadLanguagePack(&_languages[index]);
00498         DeleteWindowByClass(WC_QUERY_STRING);
00499         CheckForMissingGlyphs();
00500         UpdateAllVirtCoords();
00501         ReInitAllWindows();
00502         break;
00503 
00504       case GOW_RESOLUTION_DROPDOWN: // Change resolution
00505         if (index < _num_resolutions && ChangeResInGame(_resolutions[index].width, _resolutions[index].height)) {
00506           this->SetDirty();
00507         }
00508         break;
00509 
00510       case GOW_SCREENSHOT_DROPDOWN: // Change screenshot format
00511         SetScreenshotFormat(index);
00512         this->SetDirty();
00513         break;
00514 
00515       case GOW_BASE_GRF_DROPDOWN:
00516         this->SetMediaSet<BaseGraphics>(index);
00517         break;
00518 
00519       case GOW_BASE_SFX_DROPDOWN:
00520         this->SetMediaSet<BaseSounds>(index);
00521         break;
00522 
00523       case GOW_BASE_MUSIC_DROPDOWN:
00524         this->SetMediaSet<BaseMusic>(index);
00525         break;
00526     }
00527   }
00528 
00534   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
00535   {
00536     if (!gui_scope) return;
00537     this->SetWidgetLoweredState(GOW_FULLSCREEN_BUTTON, _fullscreen);
00538 
00539     bool missing_files = BaseGraphics::GetUsedSet()->GetNumMissing() == 0;
00540     this->GetWidget<NWidgetCore>(GOW_BASE_GRF_STATUS)->SetDataTip(missing_files ? STR_EMPTY : STR_GAME_OPTIONS_BASE_GRF_STATUS, STR_NULL);
00541 
00542     missing_files = BaseMusic::GetUsedSet()->GetNumInvalid() == 0;
00543     this->GetWidget<NWidgetCore>(GOW_BASE_MUSIC_STATUS)->SetDataTip(missing_files ? STR_EMPTY : STR_GAME_OPTIONS_BASE_MUSIC_STATUS, STR_NULL);
00544   }
00545 };
00546 
00547 static const NWidgetPart _nested_game_options_widgets[] = {
00548   NWidget(NWID_HORIZONTAL),
00549     NWidget(WWT_CLOSEBOX, COLOUR_GREY),
00550     NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_GAME_OPTIONS_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00551   EndContainer(),
00552   NWidget(WWT_PANEL, COLOUR_GREY, GOW_BACKGROUND), SetPIP(6, 6, 10),
00553     NWidget(NWID_HORIZONTAL), SetPIP(10, 10, 10),
00554       NWidget(NWID_VERTICAL), SetPIP(0, 6, 0),
00555         NWidget(WWT_FRAME, COLOUR_GREY), SetDataTip(STR_GAME_OPTIONS_CURRENCY_UNITS_FRAME, STR_NULL),
00556           NWidget(WWT_DROPDOWN, COLOUR_GREY, GOW_CURRENCY_DROPDOWN), SetMinimalSize(150, 12), SetDataTip(STR_BLACK_STRING, STR_GAME_OPTIONS_CURRENCY_UNITS_DROPDOWN_TOOLTIP), SetFill(1, 0),
00557         EndContainer(),
00558         NWidget(WWT_FRAME, COLOUR_GREY), SetDataTip(STR_GAME_OPTIONS_ROAD_VEHICLES_FRAME, STR_NULL),
00559           NWidget(WWT_DROPDOWN, COLOUR_GREY, GOW_ROADSIDE_DROPDOWN), SetMinimalSize(150, 12), SetDataTip(STR_BLACK_STRING, STR_GAME_OPTIONS_ROAD_VEHICLES_DROPDOWN_TOOLTIP), SetFill(1, 0),
00560         EndContainer(),
00561         NWidget(WWT_FRAME, COLOUR_GREY), SetDataTip(STR_GAME_OPTIONS_AUTOSAVE_FRAME, STR_NULL),
00562           NWidget(WWT_DROPDOWN, COLOUR_GREY, GOW_AUTOSAVE_DROPDOWN), SetMinimalSize(150, 12), SetDataTip(STR_BLACK_STRING, STR_GAME_OPTIONS_AUTOSAVE_DROPDOWN_TOOLTIP), SetFill(1, 0),
00563         EndContainer(),
00564         NWidget(WWT_FRAME, COLOUR_GREY), SetDataTip(STR_GAME_OPTIONS_RESOLUTION, STR_NULL),
00565           NWidget(WWT_DROPDOWN, COLOUR_GREY, GOW_RESOLUTION_DROPDOWN), SetMinimalSize(150, 12), SetDataTip(STR_BLACK_STRING, STR_GAME_OPTIONS_RESOLUTION_TOOLTIP), SetFill(1, 0), SetPadding(0, 0, 3, 0),
00566           NWidget(NWID_HORIZONTAL),
00567             NWidget(WWT_TEXT, COLOUR_GREY), SetMinimalSize(0, 12), SetFill(1, 0), SetDataTip(STR_GAME_OPTIONS_FULLSCREEN, STR_NULL),
00568             NWidget(WWT_TEXTBTN, COLOUR_GREY, GOW_FULLSCREEN_BUTTON), SetMinimalSize(21, 9), SetDataTip(STR_EMPTY, STR_GAME_OPTIONS_FULLSCREEN_TOOLTIP),
00569           EndContainer(),
00570         EndContainer(),
00571       EndContainer(),
00572 
00573       NWidget(NWID_VERTICAL), SetPIP(0, 6, 0),
00574         NWidget(WWT_FRAME, COLOUR_GREY), SetDataTip(STR_GAME_OPTIONS_MEASURING_UNITS_FRAME, STR_NULL),
00575           NWidget(WWT_DROPDOWN, COLOUR_GREY, GOW_DISTANCE_DROPDOWN), SetMinimalSize(150, 12), SetDataTip(STR_BLACK_STRING, STR_GAME_OPTIONS_MEASURING_UNITS_DROPDOWN_TOOLTIP), SetFill(1, 0),
00576         EndContainer(),
00577         NWidget(WWT_FRAME, COLOUR_GREY), SetDataTip(STR_GAME_OPTIONS_TOWN_NAMES_FRAME, STR_NULL),
00578           NWidget(WWT_DROPDOWN, COLOUR_GREY, GOW_TOWNNAME_DROPDOWN), SetMinimalSize(150, 12), SetDataTip(STR_BLACK_STRING, STR_GAME_OPTIONS_TOWN_NAMES_DROPDOWN_TOOLTIP), SetFill(1, 0),
00579         EndContainer(),
00580         NWidget(WWT_FRAME, COLOUR_GREY), SetDataTip(STR_GAME_OPTIONS_LANGUAGE, STR_NULL),
00581           NWidget(WWT_DROPDOWN, COLOUR_GREY, GOW_LANG_DROPDOWN), SetMinimalSize(150, 12), SetDataTip(STR_BLACK_RAW_STRING, STR_GAME_OPTIONS_LANGUAGE_TOOLTIP), SetFill(1, 0),
00582         EndContainer(),
00583         NWidget(WWT_FRAME, COLOUR_GREY), SetDataTip(STR_GAME_OPTIONS_SCREENSHOT_FORMAT, STR_NULL),
00584           NWidget(WWT_DROPDOWN, COLOUR_GREY, GOW_SCREENSHOT_DROPDOWN), SetMinimalSize(150, 12), SetDataTip(STR_BLACK_STRING, STR_GAME_OPTIONS_SCREENSHOT_FORMAT_TOOLTIP), SetFill(1, 0),
00585         EndContainer(),
00586         NWidget(NWID_SPACER), SetMinimalSize(0, 0), SetFill(0, 1),
00587       EndContainer(),
00588     EndContainer(),
00589 
00590     NWidget(WWT_FRAME, COLOUR_GREY), SetDataTip(STR_GAME_OPTIONS_BASE_GRF, STR_NULL), SetPadding(0, 10, 0, 10),
00591       NWidget(NWID_HORIZONTAL), SetPIP(0, 30, 0),
00592         NWidget(WWT_DROPDOWN, COLOUR_GREY, GOW_BASE_GRF_DROPDOWN), SetMinimalSize(150, 12), SetDataTip(STR_BLACK_RAW_STRING, STR_GAME_OPTIONS_BASE_GRF_TOOLTIP),
00593         NWidget(WWT_TEXT, COLOUR_GREY, GOW_BASE_GRF_STATUS), SetMinimalSize(150, 12), SetDataTip(STR_EMPTY, STR_NULL), SetFill(1, 0),
00594       EndContainer(),
00595       NWidget(WWT_TEXT, COLOUR_GREY, GOW_BASE_GRF_DESCRIPTION), SetMinimalSize(330, 0), SetDataTip(STR_EMPTY, STR_GAME_OPTIONS_BASE_GRF_DESCRIPTION_TOOLTIP), SetFill(1, 0), SetPadding(6, 0, 0, 0),
00596     EndContainer(),
00597 
00598     NWidget(WWT_FRAME, COLOUR_GREY), SetDataTip(STR_GAME_OPTIONS_BASE_SFX, STR_NULL), SetPadding(0, 10, 0, 10),
00599       NWidget(NWID_HORIZONTAL), SetPIP(0, 30, 0),
00600         NWidget(WWT_DROPDOWN, COLOUR_GREY, GOW_BASE_SFX_DROPDOWN), SetMinimalSize(150, 12), SetDataTip(STR_BLACK_RAW_STRING, STR_GAME_OPTIONS_BASE_SFX_TOOLTIP),
00601         NWidget(NWID_SPACER), SetFill(1, 0),
00602       EndContainer(),
00603       NWidget(WWT_TEXT, COLOUR_GREY, GOW_BASE_SFX_DESCRIPTION), SetMinimalSize(330, 0), SetDataTip(STR_EMPTY, STR_GAME_OPTIONS_BASE_SFX_DESCRIPTION_TOOLTIP), SetFill(1, 0), SetPadding(6, 0, 0, 0),
00604     EndContainer(),
00605 
00606     NWidget(WWT_FRAME, COLOUR_GREY), SetDataTip(STR_GAME_OPTIONS_BASE_MUSIC, STR_NULL), SetPadding(0, 10, 0, 10),
00607       NWidget(NWID_HORIZONTAL), SetPIP(0, 30, 0),
00608         NWidget(WWT_DROPDOWN, COLOUR_GREY, GOW_BASE_MUSIC_DROPDOWN), SetMinimalSize(150, 12), SetDataTip(STR_BLACK_RAW_STRING, STR_GAME_OPTIONS_BASE_MUSIC_TOOLTIP),
00609         NWidget(WWT_TEXT, COLOUR_GREY, GOW_BASE_MUSIC_STATUS), SetMinimalSize(150, 12), SetDataTip(STR_EMPTY, STR_NULL), SetFill(1, 0),
00610       EndContainer(),
00611       NWidget(WWT_TEXT, COLOUR_GREY, GOW_BASE_MUSIC_DESCRIPTION), SetMinimalSize(330, 0), SetDataTip(STR_EMPTY, STR_GAME_OPTIONS_BASE_MUSIC_DESCRIPTION_TOOLTIP), SetFill(1, 0), SetPadding(6, 0, 0, 0),
00612     EndContainer(),
00613   EndContainer(),
00614 };
00615 
00616 static const WindowDesc _game_options_desc(
00617   WDP_CENTER, 0, 0,
00618   WC_GAME_OPTIONS, WC_NONE,
00619   WDF_UNCLICK_BUTTONS,
00620   _nested_game_options_widgets, lengthof(_nested_game_options_widgets)
00621 );
00622 
00624 void ShowGameOptions()
00625 {
00626   DeleteWindowById(WC_GAME_OPTIONS, 0);
00627   new GameOptionsWindow(&_game_options_desc);
00628 }
00629 
00630 extern void StartupEconomy();
00631 
00632 
00633 /* Names of the game difficulty settings window */
00634 enum GameDifficultyWidgets {
00635   GDW_LVL_EASY,
00636   GDW_LVL_MEDIUM,
00637   GDW_LVL_HARD,
00638   GDW_LVL_CUSTOM,
00639   GDW_HIGHSCORE,
00640   GDW_ACCEPT,
00641   GDW_CANCEL,
00642 
00643   GDW_OPTIONS_START,
00644 };
00645 
00646 void SetDifficultyLevel(int mode, DifficultySettings *gm_opt);
00647 
00648 class GameDifficultyWindow : public Window {
00649 private:
00650   /* Temporary holding place of values in the difficulty window until 'Save' is clicked */
00651   GameSettings opt_mod_temp;
00652 
00653 public:
00655   static const uint GAME_DIFFICULTY_NUM = 18;
00657   static const uint WIDGETS_PER_DIFFICULTY = 3;
00658 
00659   GameDifficultyWindow(const WindowDesc *desc) : Window()
00660   {
00661     this->InitNested(desc);
00662 
00663     /* Setup disabled buttons when creating window
00664      * disable all other difficulty buttons during gameplay except for 'custom' */
00665     this->SetWidgetsDisabledState(_game_mode != GM_MENU,
00666       GDW_LVL_EASY,
00667       GDW_LVL_MEDIUM,
00668       GDW_LVL_HARD,
00669       GDW_LVL_CUSTOM,
00670       WIDGET_LIST_END);
00671     this->SetWidgetDisabledState(GDW_HIGHSCORE, _game_mode == GM_EDITOR || _networking); // highscore chart in multiplayer
00672     this->SetWidgetDisabledState(GDW_ACCEPT, _networking && !_network_server); // Save-button in multiplayer (and if client)
00673 
00674     /* Read data */
00675     this->OnInvalidateData(GOID_DIFFICULTY_CHANGED);
00676   }
00677 
00678   virtual void SetStringParameters(int widget) const
00679   {
00680     widget -= GDW_OPTIONS_START;
00681     if (widget < 0 || (widget % 3) != 2) return;
00682 
00683     widget /= 3;
00684 
00685     uint i;
00686     const SettingDesc *sd = GetSettingFromName("difficulty.max_no_competitors", &i) + widget;
00687     int32 value = (int32)ReadValue(GetVariableAddress(&this->opt_mod_temp, &sd->save), sd->save.conv);
00688     SetDParam(0, sd->desc.val_str + value);
00689   }
00690 
00691   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00692   {
00693     /* Only for the 'descriptions' */
00694     int index = widget - GDW_OPTIONS_START;
00695     if (index < 0 || (index % 3) != 2) return;
00696 
00697     index /= 3;
00698 
00699     uint i;
00700     const SettingDesc *sd = GetSettingFromName("difficulty.max_no_competitors", &i) + index;
00701     const SettingDescBase *sdb = &sd->desc;
00702 
00703     /* Get the string and try all strings from the smallest to the highest value */
00704     StringID str = this->GetWidget<NWidgetCore>(widget)->widget_data;
00705     for (int32 value = sdb->min; (uint32)value <= sdb->max; value += sdb->interval) {
00706       SetDParam(0, sdb->val_str + value);
00707       *size = maxdim(*size, GetStringBoundingBox(str));
00708     }
00709   }
00710 
00711   virtual void OnClick(Point pt, int widget, int click_count)
00712   {
00713     if (widget >= GDW_OPTIONS_START) {
00714       widget -= GDW_OPTIONS_START;
00715       if ((widget % 3) == 2) return;
00716 
00717       /* Don't allow clients to make any changes */
00718       if (_networking && !_network_server) return;
00719 
00720       uint i;
00721       const SettingDesc *sd = GetSettingFromName("difficulty.max_no_competitors", &i) + (widget / 3);
00722       const SettingDescBase *sdb = &sd->desc;
00723 
00724       int32 val = (int32)ReadValue(GetVariableAddress(&this->opt_mod_temp, &sd->save), sd->save.conv);
00725       if (widget % 3 == 1) {
00726         /* Increase button clicked */
00727         val = min(val + sdb->interval, (int32)sdb->max);
00728       } else {
00729         /* Decrease button clicked */
00730         val -= sdb->interval;
00731         val = max(val, sdb->min);
00732       }
00733 
00734       /* save value in temporary variable */
00735       WriteValue(GetVariableAddress(&this->opt_mod_temp, &sd->save), sd->save.conv, val);
00736       this->RaiseWidget(GDW_LVL_EASY + this->opt_mod_temp.difficulty.diff_level);
00737       SetDifficultyLevel(3, &this->opt_mod_temp.difficulty); // set difficulty level to custom
00738       this->LowerWidget(GDW_LVL_CUSTOM);
00739       this->InvalidateData();
00740 
00741       if (widget / 3 == 0 &&
00742           AI::GetInfoList()->size() == 0 &&
00743           this->opt_mod_temp.difficulty.max_no_competitors != 0) {
00744         ShowErrorMessage(STR_WARNING_NO_SUITABLE_AI, INVALID_STRING_ID, WL_CRITICAL);
00745       }
00746       return;
00747     }
00748 
00749     switch (widget) {
00750       case GDW_LVL_EASY:
00751       case GDW_LVL_MEDIUM:
00752       case GDW_LVL_HARD:
00753       case GDW_LVL_CUSTOM:
00754         /* temporarily change difficulty level */
00755         this->RaiseWidget(GDW_LVL_EASY + this->opt_mod_temp.difficulty.diff_level);
00756         SetDifficultyLevel(widget - GDW_LVL_EASY, &this->opt_mod_temp.difficulty);
00757         this->LowerWidget(GDW_LVL_EASY + this->opt_mod_temp.difficulty.diff_level);
00758         this->InvalidateData();
00759         break;
00760 
00761       case GDW_HIGHSCORE: // Highscore Table
00762         ShowHighscoreTable(this->opt_mod_temp.difficulty.diff_level, -1);
00763         break;
00764 
00765       case GDW_ACCEPT: { // Save button - save changes
00766         GameSettings *opt_ptr = &GetGameSettings();
00767 
00768         uint i;
00769         GetSettingFromName("difficulty.diff_level", &i);
00770         DoCommandP(0, i, this->opt_mod_temp.difficulty.diff_level, CMD_CHANGE_SETTING);
00771 
00772         const SettingDesc *sd = GetSettingFromName("difficulty.max_no_competitors", &i);
00773         for (uint btn = 0; btn != GAME_DIFFICULTY_NUM; btn++, sd++) {
00774           int32 new_val = (int32)ReadValue(GetVariableAddress(&this->opt_mod_temp, &sd->save), sd->save.conv);
00775           int32 cur_val = (int32)ReadValue(GetVariableAddress(opt_ptr, &sd->save), sd->save.conv);
00776           /* if setting has changed, change it */
00777           if (new_val != cur_val) {
00778             DoCommandP(0, i + btn, new_val, CMD_CHANGE_SETTING);
00779           }
00780         }
00781         delete this;
00782         /* If we are in the editor, we should reload the economy.
00783          * This way when you load a game, the max loan and interest rate
00784          * are loaded correctly. */
00785         if (_game_mode == GM_EDITOR) StartupEconomy();
00786         break;
00787       }
00788 
00789       case GDW_CANCEL: // Cancel button - close window, abandon changes
00790         delete this;
00791         break;
00792     }
00793   }
00794 
00800   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
00801   {
00802     if (!gui_scope) return;
00803 
00804     if (data == GOID_DIFFICULTY_CHANGED) {
00805       /* Window was created or settings were changed on server. Reread everything. */
00806 
00807       /* Copy current settings (ingame or in intro) to temporary holding place
00808        * change that when setting stuff, copy back on clicking 'OK' */
00809       this->opt_mod_temp = GetGameSettings();
00810 
00811       this->LowerWidget(GDW_LVL_EASY + this->opt_mod_temp.difficulty.diff_level);
00812     }
00813 
00814     uint i;
00815     const SettingDesc *sd = GetSettingFromName("difficulty.max_no_competitors", &i);
00816     for (i = 0; i < GAME_DIFFICULTY_NUM; i++, sd++) {
00817       const SettingDescBase *sdb = &sd->desc;
00818       /* skip deprecated difficulty options */
00819       if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue;
00820       int32 value = (int32)ReadValue(GetVariableAddress(&this->opt_mod_temp, &sd->save), sd->save.conv);
00821       bool disable = (sd->desc.flags & SGF_NEWGAME_ONLY) &&
00822           (_game_mode == GM_NORMAL ||
00823           (_game_mode == GM_EDITOR && (sd->desc.flags & SGF_SCENEDIT_TOO) == 0));
00824 
00825       this->SetWidgetDisabledState(GDW_OPTIONS_START + i * 3 + 0, disable || sdb->min == value);
00826       this->SetWidgetDisabledState(GDW_OPTIONS_START + i * 3 + 1, disable || sdb->max == (uint32)value);
00827     }
00828   }
00829 };
00830 
00831 static NWidgetBase *MakeDifficultyOptionsWidgets(int *biggest_index)
00832 {
00833   NWidgetVertical *vert_desc = new NWidgetVertical;
00834 
00835   int widnum = GDW_OPTIONS_START;
00836   uint i, j;
00837   const SettingDesc *sd = GetSettingFromName("difficulty.max_no_competitors", &i);
00838 
00839   for (i = 0, j = 0; i < GameDifficultyWindow::GAME_DIFFICULTY_NUM; i++, sd++, widnum += GameDifficultyWindow::WIDGETS_PER_DIFFICULTY) {
00840     if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue;
00841 
00842     NWidgetHorizontal *hor = new NWidgetHorizontal;
00843 
00844     /* [<] button. */
00845     NWidgetLeaf *leaf = new NWidgetLeaf(WWT_PUSHARROWBTN, COLOUR_YELLOW, widnum, AWV_DECREASE, STR_TOOLTIP_HSCROLL_BAR_SCROLLS_LIST);
00846     hor->Add(leaf);
00847 
00848     /* [>] button. */
00849     leaf = new NWidgetLeaf(WWT_PUSHARROWBTN, COLOUR_YELLOW, widnum + 1, AWV_INCREASE, STR_TOOLTIP_HSCROLL_BAR_SCROLLS_LIST);
00850     hor->Add(leaf);
00851 
00852     /* Some spacing between the text and the description */
00853     NWidgetSpacer *spacer = new NWidgetSpacer(5, 0);
00854     hor->Add(spacer);
00855 
00856     /* Descriptive text. */
00857     leaf = new NWidgetLeaf(WWT_TEXT, COLOUR_YELLOW, widnum + 2, STR_DIFFICULTY_LEVEL_SETTING_MAXIMUM_NO_COMPETITORS + (j++), STR_NULL);
00858     leaf->SetFill(1, 0);
00859     hor->Add(leaf);
00860     vert_desc->Add(hor);
00861 
00862     /* Space vertically */
00863     vert_desc->Add(new NWidgetSpacer(0, 2));
00864   }
00865   *biggest_index = widnum - 1;
00866   return vert_desc;
00867 }
00868 
00869 
00871 static const NWidgetPart _nested_game_difficulty_widgets[] = {
00872   NWidget(WWT_CAPTION, COLOUR_MAUVE), SetDataTip(STR_DIFFICULTY_LEVEL_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00873   NWidget(WWT_PANEL, COLOUR_MAUVE),
00874     NWidget(NWID_VERTICAL), SetPIP(2, 0, 2),
00875       NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(10, 0, 10),
00876         NWidget(WWT_TEXTBTN, COLOUR_YELLOW, GDW_LVL_EASY), SetDataTip(STR_DIFFICULTY_LEVEL_EASY, STR_NULL), SetFill(1, 0),
00877         NWidget(WWT_TEXTBTN, COLOUR_YELLOW, GDW_LVL_MEDIUM), SetDataTip(STR_DIFFICULTY_LEVEL_MEDIUM, STR_NULL), SetFill(1, 0),
00878         NWidget(WWT_TEXTBTN, COLOUR_YELLOW, GDW_LVL_HARD), SetDataTip(STR_DIFFICULTY_LEVEL_HARD, STR_NULL), SetFill(1, 0),
00879         NWidget(WWT_TEXTBTN, COLOUR_YELLOW, GDW_LVL_CUSTOM), SetDataTip(STR_DIFFICULTY_LEVEL_CUSTOM, STR_NULL), SetFill(1, 0),
00880       EndContainer(),
00881       NWidget(NWID_HORIZONTAL), SetPIP(10, 0, 10),
00882         NWidget(WWT_PUSHTXTBTN, COLOUR_GREEN, GDW_HIGHSCORE), SetDataTip(STR_DIFFICULTY_LEVEL_HIGH_SCORE_BUTTON, STR_NULL), SetFill(1, 0),
00883       EndContainer(),
00884     EndContainer(),
00885   EndContainer(),
00886   NWidget(WWT_PANEL, COLOUR_MAUVE),
00887     NWidget(NWID_VERTICAL), SetPIP(3, 0, 1),
00888       NWidget(NWID_HORIZONTAL), SetPIP(5, 0, 5),
00889         NWidgetFunction(MakeDifficultyOptionsWidgets),
00890       EndContainer(),
00891     EndContainer(),
00892   EndContainer(),
00893   NWidget(WWT_PANEL, COLOUR_MAUVE),
00894     NWidget(NWID_VERTICAL), SetPIP(2, 0, 2),
00895       NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(10, 0, 10),
00896         NWidget(NWID_SPACER), SetFill(1, 0),
00897         NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, GDW_ACCEPT), SetDataTip(STR_DIFFICULTY_LEVEL_SAVE, STR_NULL), SetFill(1, 0),
00898         NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, GDW_CANCEL), SetDataTip(STR_BUTTON_CANCEL, STR_NULL), SetFill(1, 0),
00899         NWidget(NWID_SPACER), SetFill(1, 0),
00900       EndContainer(),
00901     EndContainer(),
00902   EndContainer(),
00903 };
00904 
00906 static const WindowDesc _game_difficulty_desc(
00907   WDP_CENTER, 0, 0,
00908   WC_GAME_OPTIONS, WC_NONE,
00909   WDF_UNCLICK_BUTTONS,
00910   _nested_game_difficulty_widgets, lengthof(_nested_game_difficulty_widgets)
00911 );
00912 
00914 void ShowGameDifficulty()
00915 {
00916   DeleteWindowById(WC_GAME_OPTIONS, 0);
00917   new GameDifficultyWindow(&_game_difficulty_desc);
00918 }
00919 
00920 static int SETTING_HEIGHT = 11;    
00921 static const int LEVEL_WIDTH = 15; 
00922 
00927 enum SettingEntryFlags {
00928   SEF_LEFT_DEPRESSED  = 0x01, 
00929   SEF_RIGHT_DEPRESSED = 0x02, 
00930   SEF_BUTTONS_MASK = (SEF_LEFT_DEPRESSED | SEF_RIGHT_DEPRESSED), 
00931 
00932   SEF_LAST_FIELD = 0x04, 
00933 
00934   /* Entry kind */
00935   SEF_SETTING_KIND = 0x10, 
00936   SEF_SUBTREE_KIND = 0x20, 
00937   SEF_KIND_MASK    = (SEF_SETTING_KIND | SEF_SUBTREE_KIND), 
00938 };
00939 
00940 struct SettingsPage; // Forward declaration
00941 
00943 struct SettingEntrySubtree {
00944   SettingsPage *page; 
00945   bool folded;        
00946   StringID title;     
00947 };
00948 
00950 struct SettingEntrySetting {
00951   const char *name;           
00952   const SettingDesc *setting; 
00953   uint index;                 
00954 };
00955 
00957 struct SettingEntry {
00958   byte flags; 
00959   byte level; 
00960   union {
00961     SettingEntrySetting entry; 
00962     SettingEntrySubtree sub;   
00963   } d; 
00964 
00965   SettingEntry(const char *nm);
00966   SettingEntry(SettingsPage *sub, StringID title);
00967 
00968   void Init(byte level, bool last_field);
00969   void FoldAll();
00970   void SetButtons(byte new_val);
00971 
00972   uint Length() const;
00973   SettingEntry *FindEntry(uint row, uint *cur_row);
00974 
00975   uint Draw(GameSettings *settings_ptr, int base_x, int base_y, int max_x, uint first_row, uint max_row, uint cur_row, uint parent_last);
00976 
00977 private:
00978   void DrawSetting(GameSettings *settings_ptr, const SettingDesc *sd, int x, int y, int max_x, int state);
00979 };
00980 
00982 struct SettingsPage {
00983   SettingEntry *entries; 
00984   byte num;              
00985 
00986   void Init(byte level = 0);
00987   void FoldAll();
00988 
00989   uint Length() const;
00990   SettingEntry *FindEntry(uint row, uint *cur_row) const;
00991 
00992   uint Draw(GameSettings *settings_ptr, int base_x, int base_y, int max_x, uint first_row, uint max_row, uint cur_row = 0, uint parent_last = 0) const;
00993 };
00994 
00995 
00996 /* == SettingEntry methods == */
00997 
01002 SettingEntry::SettingEntry(const char *nm)
01003 {
01004   this->flags = SEF_SETTING_KIND;
01005   this->level = 0;
01006   this->d.entry.name = nm;
01007   this->d.entry.setting = NULL;
01008   this->d.entry.index = 0;
01009 }
01010 
01016 SettingEntry::SettingEntry(SettingsPage *sub, StringID title)
01017 {
01018   this->flags = SEF_SUBTREE_KIND;
01019   this->level = 0;
01020   this->d.sub.page = sub;
01021   this->d.sub.folded = true;
01022   this->d.sub.title = title;
01023 }
01024 
01030 void SettingEntry::Init(byte level, bool last_field)
01031 {
01032   this->level = level;
01033   if (last_field) this->flags |= SEF_LAST_FIELD;
01034 
01035   switch (this->flags & SEF_KIND_MASK) {
01036     case SEF_SETTING_KIND:
01037       this->d.entry.setting = GetSettingFromName(this->d.entry.name, &this->d.entry.index);
01038       assert(this->d.entry.setting != NULL);
01039       break;
01040     case SEF_SUBTREE_KIND:
01041       this->d.sub.page->Init(level + 1);
01042       break;
01043     default: NOT_REACHED();
01044   }
01045 }
01046 
01048 void SettingEntry::FoldAll()
01049 {
01050   switch (this->flags & SEF_KIND_MASK) {
01051     case SEF_SETTING_KIND:
01052       break;
01053 
01054     case SEF_SUBTREE_KIND:
01055       this->d.sub.folded = true;
01056       this->d.sub.page->FoldAll();
01057       break;
01058 
01059     default: NOT_REACHED();
01060   }
01061 }
01062 
01063 
01069 void SettingEntry::SetButtons(byte new_val)
01070 {
01071   assert((new_val & ~SEF_BUTTONS_MASK) == 0); // Should not touch any flags outside the buttons
01072   this->flags = (this->flags & ~SEF_BUTTONS_MASK) | new_val;
01073 }
01074 
01076 uint SettingEntry::Length() const
01077 {
01078   switch (this->flags & SEF_KIND_MASK) {
01079     case SEF_SETTING_KIND:
01080       return 1;
01081     case SEF_SUBTREE_KIND:
01082       if (this->d.sub.folded) return 1; // Only displaying the title
01083 
01084       return 1 + this->d.sub.page->Length(); // 1 extra row for the title
01085     default: NOT_REACHED();
01086   }
01087 }
01088 
01095 SettingEntry *SettingEntry::FindEntry(uint row_num, uint *cur_row)
01096 {
01097   if (row_num == *cur_row) return this;
01098 
01099   switch (this->flags & SEF_KIND_MASK) {
01100     case SEF_SETTING_KIND:
01101       (*cur_row)++;
01102       break;
01103     case SEF_SUBTREE_KIND:
01104       (*cur_row)++; // add one for row containing the title
01105       if (this->d.sub.folded) {
01106         break;
01107       }
01108 
01109       /* sub-page is visible => search it too */
01110       return this->d.sub.page->FindEntry(row_num, cur_row);
01111     default: NOT_REACHED();
01112   }
01113   return NULL;
01114 }
01115 
01142 uint SettingEntry::Draw(GameSettings *settings_ptr, int left, int right, int base_y, uint first_row, uint max_row, uint cur_row, uint parent_last)
01143 {
01144   if (cur_row >= max_row) return cur_row;
01145 
01146   bool rtl = _current_text_dir == TD_RTL;
01147   int offset = rtl ? -4 : 4;
01148   int level_width = rtl ? -LEVEL_WIDTH : LEVEL_WIDTH;
01149 
01150   int x = rtl ? right : left;
01151   int y = base_y;
01152   if (cur_row >= first_row) {
01153     int colour = _colour_gradient[COLOUR_ORANGE][4];
01154     y = base_y + (cur_row - first_row) * SETTING_HEIGHT; // Compute correct y start position
01155 
01156     /* Draw vertical for parent nesting levels */
01157     for (uint lvl = 0; lvl < this->level; lvl++) {
01158       if (!HasBit(parent_last, lvl)) GfxDrawLine(x + offset, y, x + offset, y + SETTING_HEIGHT - 1, colour);
01159       x += level_width;
01160     }
01161     /* draw own |- prefix */
01162     int halfway_y = y + SETTING_HEIGHT / 2;
01163     int bottom_y = (flags & SEF_LAST_FIELD) ? halfway_y : y + SETTING_HEIGHT - 1;
01164     GfxDrawLine(x + offset, y, x + offset, bottom_y, colour);
01165     /* Small horizontal line from the last vertical line */
01166     GfxDrawLine(x + offset, halfway_y, x + level_width - offset, halfway_y, colour);
01167     x += level_width;
01168   }
01169 
01170   switch (this->flags & SEF_KIND_MASK) {
01171     case SEF_SETTING_KIND:
01172       if (cur_row >= first_row) {
01173         DrawSetting(settings_ptr, this->d.entry.setting, rtl ? left : x, rtl ? x : right, y, this->flags & SEF_BUTTONS_MASK);
01174       }
01175       cur_row++;
01176       break;
01177     case SEF_SUBTREE_KIND:
01178       if (cur_row >= first_row) {
01179         DrawSprite((this->d.sub.folded ? SPR_CIRCLE_FOLDED : SPR_CIRCLE_UNFOLDED), PAL_NONE, rtl ? x - 8 : x, y + (SETTING_HEIGHT - 11) / 2);
01180         DrawString(rtl ? left : x + 12, rtl ? x - 12 : right, y, this->d.sub.title);
01181       }
01182       cur_row++;
01183       if (!this->d.sub.folded) {
01184         if (this->flags & SEF_LAST_FIELD) {
01185           assert(this->level < sizeof(parent_last));
01186           SetBit(parent_last, this->level); // Add own last-field state
01187         }
01188 
01189         cur_row = this->d.sub.page->Draw(settings_ptr, left, right, base_y, first_row, max_row, cur_row, parent_last);
01190       }
01191       break;
01192     default: NOT_REACHED();
01193   }
01194   return cur_row;
01195 }
01196 
01197 static const void *ResolveVariableAddress(const GameSettings *settings_ptr, const SettingDesc *sd)
01198 {
01199   if ((sd->desc.flags & SGF_PER_COMPANY) != 0) {
01200     if (Company::IsValidID(_local_company) && _game_mode != GM_MENU) {
01201       return GetVariableAddress(&Company::Get(_local_company)->settings, &sd->save);
01202     } else {
01203       return GetVariableAddress(&_settings_client.company, &sd->save);
01204     }
01205   } else {
01206     return GetVariableAddress(settings_ptr, &sd->save);
01207   }
01208 }
01209 
01219 void SettingEntry::DrawSetting(GameSettings *settings_ptr, const SettingDesc *sd, int left, int right, int y, int state)
01220 {
01221   const SettingDescBase *sdb = &sd->desc;
01222   const void *var = ResolveVariableAddress(settings_ptr, sd);
01223   bool editable = true;
01224   bool disabled = false;
01225 
01226   bool rtl = _current_text_dir == TD_RTL;
01227   uint buttons_left = rtl ? right - 19 : left;
01228   uint text_left  = left + (rtl ? 0 : 25);
01229   uint text_right = right - (rtl ? 25 : 0);
01230   uint button_y = y + (SETTING_HEIGHT - 11) / 2;
01231 
01232   /* We do not allow changes of some items when we are a client in a networkgame */
01233   if (!(sd->save.conv & SLF_NO_NETWORK_SYNC) && _networking && !_network_server && !(sdb->flags & SGF_PER_COMPANY)) editable = false;
01234   if ((sdb->flags & SGF_NETWORK_ONLY) && !_networking) editable = false;
01235   if ((sdb->flags & SGF_NO_NETWORK) && _networking) editable = false;
01236 
01237   if (sdb->cmd == SDT_BOOLX) {
01238     static const Colours _bool_ctabs[2][2] = {{COLOUR_CREAM, COLOUR_RED}, {COLOUR_DARK_GREEN, COLOUR_GREEN}};
01239     /* Draw checkbox for boolean-value either on/off */
01240     bool on = ReadValue(var, sd->save.conv) != 0;
01241 
01242     DrawFrameRect(buttons_left, button_y, buttons_left + 19, button_y + 8, _bool_ctabs[!!on][!!editable], on ? FR_LOWERED : FR_NONE);
01243     SetDParam(0, on ? STR_CONFIG_SETTING_ON : STR_CONFIG_SETTING_OFF);
01244   } else {
01245     int32 value;
01246 
01247     value = (int32)ReadValue(var, sd->save.conv);
01248 
01249     /* Draw [<][>] boxes for settings of an integer-type */
01250     DrawArrowButtons(buttons_left, button_y, COLOUR_YELLOW, state, editable && value != (sdb->flags & SGF_0ISDISABLED ? 0 : sdb->min), editable && (uint32)value != sdb->max);
01251 
01252     disabled = (value == 0) && (sdb->flags & SGF_0ISDISABLED);
01253     if (disabled) {
01254       SetDParam(0, STR_CONFIG_SETTING_DISABLED);
01255     } else {
01256       if (sdb->flags & SGF_CURRENCY) {
01257         SetDParam(0, STR_JUST_CURRENCY_LONG);
01258       } else if (sdb->flags & SGF_MULTISTRING) {
01259         SetDParam(0, sdb->val_str - sdb->min + value);
01260       } else {
01261         SetDParam(0, (sdb->flags & SGF_NOCOMMA) ? STR_JUST_INT : STR_JUST_COMMA);
01262       }
01263       SetDParam(1, value);
01264     }
01265   }
01266   DrawString(text_left, text_right, y, (sdb->str) + disabled);
01267 }
01268 
01269 
01270 /* == SettingsPage methods == */
01271 
01276 void SettingsPage::Init(byte level)
01277 {
01278   for (uint field = 0; field < this->num; field++) {
01279     this->entries[field].Init(level, field + 1 == num);
01280   }
01281 }
01282 
01284 void SettingsPage::FoldAll()
01285 {
01286   for (uint field = 0; field < this->num; field++) {
01287     this->entries[field].FoldAll();
01288   }
01289 }
01290 
01292 uint SettingsPage::Length() const
01293 {
01294   uint length = 0;
01295   for (uint field = 0; field < this->num; field++) {
01296     length += this->entries[field].Length();
01297   }
01298   return length;
01299 }
01300 
01307 SettingEntry *SettingsPage::FindEntry(uint row_num, uint *cur_row) const
01308 {
01309   SettingEntry *pe = NULL;
01310 
01311   for (uint field = 0; field < this->num; field++) {
01312     pe = this->entries[field].FindEntry(row_num, cur_row);
01313     if (pe != NULL) {
01314       break;
01315     }
01316   }
01317   return pe;
01318 }
01319 
01337 uint SettingsPage::Draw(GameSettings *settings_ptr, int left, int right, int base_y, uint first_row, uint max_row, uint cur_row, uint parent_last) const
01338 {
01339   if (cur_row >= max_row) return cur_row;
01340 
01341   for (uint i = 0; i < this->num; i++) {
01342     cur_row = this->entries[i].Draw(settings_ptr, left, right, base_y, first_row, max_row, cur_row, parent_last);
01343     if (cur_row >= max_row) {
01344       break;
01345     }
01346   }
01347   return cur_row;
01348 }
01349 
01350 
01351 static SettingEntry _settings_ui_display[] = {
01352   SettingEntry("gui.date_format_in_default_names"),
01353   SettingEntry("gui.population_in_label"),
01354   SettingEntry("gui.measure_tooltip"),
01355   SettingEntry("gui.loading_indicators"),
01356   SettingEntry("gui.liveries"),
01357   SettingEntry("gui.show_track_reservation"),
01358   SettingEntry("gui.expenses_layout"),
01359   SettingEntry("gui.smallmap_land_colour"),
01360   SettingEntry("gui.zoom_min"),
01361   SettingEntry("gui.zoom_max"),
01362 };
01364 static SettingsPage _settings_ui_display_page = {_settings_ui_display, lengthof(_settings_ui_display)};
01365 
01366 static SettingEntry _settings_ui_interaction[] = {
01367   SettingEntry("gui.window_snap_radius"),
01368   SettingEntry("gui.window_soft_limit"),
01369   SettingEntry("gui.link_terraform_toolbar"),
01370   SettingEntry("gui.prefer_teamchat"),
01371   SettingEntry("gui.autoscroll"),
01372   SettingEntry("gui.reverse_scroll"),
01373   SettingEntry("gui.smooth_scroll"),
01374   SettingEntry("gui.left_mouse_btn_scrolling"),
01375   /* While the horizontal scrollwheel scrolling is written as general code, only
01376    *  the cocoa (OSX) driver generates input for it.
01377    *  Since it's also able to completely disable the scrollwheel will we display it on all platforms anyway */
01378   SettingEntry("gui.scrollwheel_scrolling"),
01379   SettingEntry("gui.scrollwheel_multiplier"),
01380 #ifdef __APPLE__
01381   /* We might need to emulate a right mouse button on mac */
01382   SettingEntry("gui.right_mouse_btn_emulation"),
01383 #endif
01384 };
01386 static SettingsPage _settings_ui_interaction_page = {_settings_ui_interaction, lengthof(_settings_ui_interaction)};
01387 
01388 static SettingEntry _settings_ui[] = {
01389   SettingEntry(&_settings_ui_display_page, STR_CONFIG_SETTING_DISPLAY_OPTIONS),
01390   SettingEntry(&_settings_ui_interaction_page, STR_CONFIG_SETTING_INTERACTION),
01391   SettingEntry("gui.show_finances"),
01392   SettingEntry("gui.errmsg_duration"),
01393   SettingEntry("gui.hover_delay"),
01394   SettingEntry("gui.toolbar_pos"),
01395   SettingEntry("gui.statusbar_pos"),
01396   SettingEntry("gui.newgrf_default_palette"),
01397   SettingEntry("gui.pause_on_newgame"),
01398   SettingEntry("gui.advanced_vehicle_list"),
01399   SettingEntry("gui.timetable_in_ticks"),
01400   SettingEntry("gui.timetable_arrival_departure"),
01401   SettingEntry("gui.quick_goto"),
01402   SettingEntry("gui.default_rail_type"),
01403   SettingEntry("gui.disable_unsuitable_building"),
01404   SettingEntry("gui.persistent_buildingtools"),
01405   SettingEntry("gui.coloured_news_year"),
01406 };
01408 static SettingsPage _settings_ui_page = {_settings_ui, lengthof(_settings_ui)};
01409 
01410 static SettingEntry _settings_construction_signals[] = {
01411   SettingEntry("construction.signal_side"),
01412   SettingEntry("gui.enable_signal_gui"),
01413   SettingEntry("gui.drag_signals_density"),
01414   SettingEntry("gui.semaphore_build_before"),
01415   SettingEntry("gui.default_signal_type"),
01416   SettingEntry("gui.cycle_signal_types"),
01417 };
01419 static SettingsPage _settings_construction_signals_page = {_settings_construction_signals, lengthof(_settings_construction_signals)};
01420 
01421 static SettingEntry _settings_construction[] = {
01422   SettingEntry(&_settings_construction_signals_page, STR_CONFIG_SETTING_CONSTRUCTION_SIGNALS),
01423   SettingEntry("construction.build_on_slopes"),
01424   SettingEntry("construction.autoslope"),
01425   SettingEntry("construction.extra_dynamite"),
01426   SettingEntry("construction.max_bridge_length"),
01427   SettingEntry("construction.max_tunnel_length"),
01428   SettingEntry("station.never_expire_airports"),
01429   SettingEntry("construction.freeform_edges"),
01430   SettingEntry("construction.extra_tree_placement"),
01431   SettingEntry("construction.command_pause_level"),
01432 };
01434 static SettingsPage _settings_construction_page = {_settings_construction, lengthof(_settings_construction)};
01435 
01436 static SettingEntry _settings_stations_cargo[] = {
01437   SettingEntry("order.improved_load"),
01438   SettingEntry("order.gradual_loading"),
01439   SettingEntry("order.selectgoods"),
01440 };
01442 static SettingsPage _settings_stations_cargo_page = {_settings_stations_cargo, lengthof(_settings_stations_cargo)};
01443 
01444 static SettingEntry _settings_stations[] = {
01445   SettingEntry(&_settings_stations_cargo_page, STR_CONFIG_SETTING_STATIONS_CARGOHANDLING),
01446   SettingEntry("station.adjacent_stations"),
01447   SettingEntry("station.distant_join_stations"),
01448   SettingEntry("station.station_spread"),
01449   SettingEntry("economy.station_noise_level"),
01450   SettingEntry("station.modified_catchment"),
01451   SettingEntry("construction.road_stop_on_town_road"),
01452   SettingEntry("construction.road_stop_on_competitor_road"),
01453 };
01455 static SettingsPage _settings_stations_page = {_settings_stations, lengthof(_settings_stations)};
01456 
01457 static SettingEntry _settings_economy_towns[] = {
01458   SettingEntry("economy.bribe"),
01459   SettingEntry("economy.exclusive_rights"),
01460   SettingEntry("economy.fund_roads"),
01461   SettingEntry("economy.fund_buildings"),
01462   SettingEntry("economy.town_layout"),
01463   SettingEntry("economy.allow_town_roads"),
01464   SettingEntry("economy.allow_town_level_crossings"),
01465   SettingEntry("economy.found_town"),
01466   SettingEntry("economy.mod_road_rebuild"),
01467   SettingEntry("economy.town_growth_rate"),
01468   SettingEntry("economy.larger_towns"),
01469   SettingEntry("economy.initial_city_size"),
01470 };
01472 static SettingsPage _settings_economy_towns_page = {_settings_economy_towns, lengthof(_settings_economy_towns)};
01473 
01474 static SettingEntry _settings_economy_industries[] = {
01475   SettingEntry("construction.raw_industry_construction"),
01476   SettingEntry("construction.industry_platform"),
01477   SettingEntry("economy.multiple_industry_per_town"),
01478   SettingEntry("game_creation.oil_refinery_limit"),
01479 };
01481 static SettingsPage _settings_economy_industries_page = {_settings_economy_industries, lengthof(_settings_economy_industries)};
01482 
01483 static SettingEntry _settings_economy_scripts[] = {
01484   SettingEntry("script.script_max_opcode_till_suspend"),
01485 };
01487 static SettingsPage _settings_economy_scripts_page = {_settings_economy_scripts, lengthof(_settings_economy_scripts)};
01488 
01489 static SettingEntry _settings_economy[] = {
01490   SettingEntry(&_settings_economy_towns_page, STR_CONFIG_SETTING_ECONOMY_TOWNS),
01491   SettingEntry(&_settings_economy_industries_page, STR_CONFIG_SETTING_ECONOMY_INDUSTRIES),
01492   SettingEntry(&_settings_economy_scripts_page, STR_CONFIG_SETTING_ECONOMY_SCRIPTS),
01493   SettingEntry("economy.inflation"),
01494   SettingEntry("economy.smooth_economy"),
01495   SettingEntry("economy.feeder_payment_share"),
01496   SettingEntry("economy.infrastructure_maintenance"),
01497 };
01499 static SettingsPage _settings_economy_page = {_settings_economy, lengthof(_settings_economy)};
01500 
01501 static SettingEntry _settings_linkgraph[] = {
01502   SettingEntry("linkgraph.recalc_interval"),
01503   SettingEntry("linkgraph.distribution_pax"),
01504   SettingEntry("linkgraph.distribution_mail"),
01505   SettingEntry("linkgraph.distribution_armoured"),
01506   SettingEntry("linkgraph.distribution_default"),
01507   SettingEntry("linkgraph.accuracy"),
01508   SettingEntry("linkgraph.demand_distance"),
01509   SettingEntry("linkgraph.demand_size"),
01510   SettingEntry("linkgraph.short_path_saturation"),
01511 };
01513 static SettingsPage _settings_linkgraph_page = {_settings_linkgraph, lengthof(_settings_linkgraph)};
01514 
01515 static SettingEntry _settings_ai_npc[] = {
01516   SettingEntry("ai.ai_in_multiplayer"),
01517   SettingEntry("ai.ai_disable_veh_train"),
01518   SettingEntry("ai.ai_disable_veh_roadveh"),
01519   SettingEntry("ai.ai_disable_veh_aircraft"),
01520   SettingEntry("ai.ai_disable_veh_ship"),
01521 };
01523 static SettingsPage _settings_ai_npc_page = {_settings_ai_npc, lengthof(_settings_ai_npc)};
01524 
01525 static SettingEntry _settings_ai[] = {
01526   SettingEntry(&_settings_ai_npc_page, STR_CONFIG_SETTING_AI_NPC),
01527   SettingEntry("economy.give_money"),
01528   SettingEntry("economy.allow_shares"),
01529 };
01531 static SettingsPage _settings_ai_page = {_settings_ai, lengthof(_settings_ai)};
01532 
01533 static SettingEntry _settings_vehicles_routing[] = {
01534   SettingEntry("pf.pathfinder_for_trains"),
01535   SettingEntry("pf.forbid_90_deg"),
01536   SettingEntry("pf.pathfinder_for_roadvehs"),
01537   SettingEntry("pf.roadveh_queue"),
01538   SettingEntry("pf.pathfinder_for_ships"),
01539 };
01541 static SettingsPage _settings_vehicles_routing_page = {_settings_vehicles_routing, lengthof(_settings_vehicles_routing)};
01542 
01543 static SettingEntry _settings_vehicles_autorenew[] = {
01544   SettingEntry("company.engine_renew"),
01545   SettingEntry("company.engine_renew_months"),
01546   SettingEntry("company.engine_renew_money"),
01547 };
01549 static SettingsPage _settings_vehicles_autorenew_page = {_settings_vehicles_autorenew, lengthof(_settings_vehicles_autorenew)};
01550 
01551 static SettingEntry _settings_vehicles_servicing[] = {
01552   SettingEntry("vehicle.servint_ispercent"),
01553   SettingEntry("vehicle.servint_trains"),
01554   SettingEntry("vehicle.servint_roadveh"),
01555   SettingEntry("vehicle.servint_ships"),
01556   SettingEntry("vehicle.servint_aircraft"),
01557   SettingEntry("order.no_servicing_if_no_breakdowns"),
01558   SettingEntry("order.serviceathelipad"),
01559 };
01561 static SettingsPage _settings_vehicles_servicing_page = {_settings_vehicles_servicing, lengthof(_settings_vehicles_servicing)};
01562 
01563 static SettingEntry _settings_vehicles_trains[] = {
01564   SettingEntry("pf.reverse_at_signals"),
01565   SettingEntry("vehicle.train_acceleration_model"),
01566   SettingEntry("vehicle.train_slope_steepness"),
01567   SettingEntry("vehicle.max_train_length"),
01568   SettingEntry("vehicle.wagon_speed_limits"),
01569   SettingEntry("vehicle.disable_elrails"),
01570   SettingEntry("vehicle.freight_trains"),
01571   SettingEntry("gui.stop_location"),
01572 };
01574 static SettingsPage _settings_vehicles_trains_page = {_settings_vehicles_trains, lengthof(_settings_vehicles_trains)};
01575 
01576 static SettingEntry _settings_vehicles[] = {
01577   SettingEntry(&_settings_vehicles_routing_page, STR_CONFIG_SETTING_VEHICLES_ROUTING),
01578   SettingEntry(&_settings_vehicles_autorenew_page, STR_CONFIG_SETTING_VEHICLES_AUTORENEW),
01579   SettingEntry(&_settings_vehicles_servicing_page, STR_CONFIG_SETTING_VEHICLES_SERVICING),
01580   SettingEntry(&_settings_vehicles_trains_page, STR_CONFIG_SETTING_VEHICLES_TRAINS),
01581   SettingEntry("gui.new_nonstop"),
01582   SettingEntry("gui.order_review_system"),
01583   SettingEntry("gui.vehicle_income_warn"),
01584   SettingEntry("gui.lost_vehicle_warn"),
01585   SettingEntry("vehicle.never_expire_vehicles"),
01586   SettingEntry("vehicle.max_trains"),
01587   SettingEntry("vehicle.max_roadveh"),
01588   SettingEntry("vehicle.max_aircraft"),
01589   SettingEntry("vehicle.max_ships"),
01590   SettingEntry("vehicle.plane_speed"),
01591   SettingEntry("vehicle.plane_crashes"),
01592   SettingEntry("vehicle.dynamic_engines"),
01593   SettingEntry("vehicle.roadveh_acceleration_model"),
01594   SettingEntry("vehicle.roadveh_slope_steepness"),
01595   SettingEntry("vehicle.smoke_amount"),
01596 };
01598 static SettingsPage _settings_vehicles_page = {_settings_vehicles, lengthof(_settings_vehicles)};
01599 
01600 static SettingEntry _settings_main[] = {
01601   SettingEntry(&_settings_ui_page,           STR_CONFIG_SETTING_GUI),
01602   SettingEntry(&_settings_construction_page, STR_CONFIG_SETTING_CONSTRUCTION),
01603   SettingEntry(&_settings_vehicles_page,     STR_CONFIG_SETTING_VEHICLES),
01604   SettingEntry(&_settings_stations_page,     STR_CONFIG_SETTING_STATIONS),
01605   SettingEntry(&_settings_economy_page,      STR_CONFIG_SETTING_ECONOMY),
01606   SettingEntry(&_settings_linkgraph_page,    STR_CONFIG_SETTING_LINKGRAPH),
01607   SettingEntry(&_settings_ai_page,           STR_CONFIG_SETTING_AI),
01608 };
01609 
01611 static SettingsPage _settings_main_page = {_settings_main, lengthof(_settings_main)};
01612 
01614 enum GameSettingsWidgets {
01615   SETTINGSEL_OPTIONSPANEL, 
01616   SETTINGSEL_SCROLLBAR,    
01617 };
01618 
01619 struct GameSettingsWindow : Window {
01620   static const int SETTINGTREE_LEFT_OFFSET   = 5; 
01621   static const int SETTINGTREE_RIGHT_OFFSET  = 5; 
01622   static const int SETTINGTREE_TOP_OFFSET    = 5; 
01623   static const int SETTINGTREE_BOTTOM_OFFSET = 5; 
01624 
01625   static GameSettings *settings_ptr;  
01626 
01627   SettingEntry *valuewindow_entry; 
01628   SettingEntry *clicked_entry; 
01629 
01630   Scrollbar *vscroll;
01631 
01632   GameSettingsWindow(const WindowDesc *desc) : Window()
01633   {
01634     static bool first_time = true;
01635 
01636     settings_ptr = &GetGameSettings();
01637 
01638     /* Build up the dynamic settings-array only once per OpenTTD session */
01639     if (first_time) {
01640       _settings_main_page.Init();
01641       first_time = false;
01642     } else {
01643       _settings_main_page.FoldAll(); // Close all sub-pages
01644     }
01645 
01646     this->valuewindow_entry = NULL; // No setting entry for which a entry window is opened
01647     this->clicked_entry = NULL; // No numeric setting buttons are depressed
01648 
01649     this->CreateNestedTree(desc);
01650     this->vscroll = this->GetScrollbar(SETTINGSEL_SCROLLBAR);
01651     this->FinishInitNested(desc, 0);
01652 
01653     this->vscroll->SetCount(_settings_main_page.Length());
01654   }
01655 
01656   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
01657   {
01658     if (widget != SETTINGSEL_OPTIONSPANEL) return;
01659 
01660     resize->height = SETTING_HEIGHT = max(11, FONT_HEIGHT_NORMAL + 1);
01661     resize->width  = 1;
01662 
01663     size->height = 5 * resize->height + SETTINGTREE_TOP_OFFSET + SETTINGTREE_BOTTOM_OFFSET;
01664   }
01665 
01666   virtual void DrawWidget(const Rect &r, int widget) const
01667   {
01668     if (widget != SETTINGSEL_OPTIONSPANEL) return;
01669 
01670     _settings_main_page.Draw(settings_ptr, r.left + SETTINGTREE_LEFT_OFFSET, r.right - SETTINGTREE_RIGHT_OFFSET, r.top + SETTINGTREE_TOP_OFFSET,
01671         this->vscroll->GetPosition(), this->vscroll->GetPosition() + this->vscroll->GetCapacity());
01672   }
01673 
01674   virtual void OnClick(Point pt, int widget, int click_count)
01675   {
01676     if (widget != SETTINGSEL_OPTIONSPANEL) return;
01677 
01678     uint btn = this->vscroll->GetScrolledRowFromWidget(pt.y, this, SETTINGSEL_OPTIONSPANEL, SETTINGTREE_TOP_OFFSET - 1);
01679     if (btn == INT_MAX) return;
01680 
01681     uint cur_row = 0;
01682     SettingEntry *pe = _settings_main_page.FindEntry(btn, &cur_row);
01683 
01684     if (pe == NULL) return;  // Clicked below the last setting of the page
01685 
01686     int x = (_current_text_dir == TD_RTL ? this->width - pt.x : pt.x) - SETTINGTREE_LEFT_OFFSET - (pe->level + 1) * LEVEL_WIDTH;  // Shift x coordinate
01687     if (x < 0) return;  // Clicked left of the entry
01688 
01689     if ((pe->flags & SEF_KIND_MASK) == SEF_SUBTREE_KIND) {
01690       pe->d.sub.folded = !pe->d.sub.folded; // Flip 'folded'-ness of the sub-page
01691 
01692       this->vscroll->SetCount(_settings_main_page.Length());
01693       this->SetDirty();
01694       return;
01695     }
01696 
01697     assert((pe->flags & SEF_KIND_MASK) == SEF_SETTING_KIND);
01698     const SettingDesc *sd = pe->d.entry.setting;
01699 
01700     /* return if action is only active in network, or only settable by server */
01701     if (!(sd->save.conv & SLF_NO_NETWORK_SYNC) && _networking && !_network_server && !(sd->desc.flags & SGF_PER_COMPANY)) return;
01702     if ((sd->desc.flags & SGF_NETWORK_ONLY) && !_networking) return;
01703     if ((sd->desc.flags & SGF_NO_NETWORK) && _networking) return;
01704 
01705     const void *var = ResolveVariableAddress(settings_ptr, sd);
01706     int32 value = (int32)ReadValue(var, sd->save.conv);
01707 
01708     /* clicked on the icon on the left side. Either scroller or bool on/off */
01709     if (x < 21) {
01710       const SettingDescBase *sdb = &sd->desc;
01711       int32 oldvalue = value;
01712 
01713       switch (sdb->cmd) {
01714         case SDT_BOOLX: value ^= 1; break;
01715         case SDT_ONEOFMANY:
01716         case SDT_NUMX: {
01717           /* Add a dynamic step-size to the scroller. In a maximum of
01718            * 50-steps you should be able to get from min to max,
01719            * unless specified otherwise in the 'interval' variable
01720            * of the current setting. */
01721           uint32 step = (sdb->interval == 0) ? ((sdb->max - sdb->min) / 50) : sdb->interval;
01722           if (step == 0) step = 1;
01723 
01724           /* don't allow too fast scrolling */
01725           if ((this->flags4 & WF_TIMEOUT_MASK) > WF_TIMEOUT_TRIGGER) {
01726             _left_button_clicked = false;
01727             return;
01728           }
01729 
01730           /* Increase or decrease the value and clamp it to extremes */
01731           if (x >= 10) {
01732             value += step;
01733             if (sdb->min < 0) {
01734               assert((int32)sdb->max >= 0);
01735               if (value > (int32)sdb->max) value = (int32)sdb->max;
01736             } else {
01737               if ((uint32)value > sdb->max) value = (int32)sdb->max;
01738             }
01739             if (value < sdb->min) value = sdb->min; // skip between "disabled" and minimum
01740           } else {
01741             value -= step;
01742             if (value < sdb->min) value = (sdb->flags & SGF_0ISDISABLED) ? 0 : sdb->min;
01743           }
01744 
01745           /* Set up scroller timeout for numeric values */
01746           if (value != oldvalue && !(sd->desc.flags & SGF_MULTISTRING)) {
01747             if (this->clicked_entry != NULL) { // Release previous buttons if any
01748               this->clicked_entry->SetButtons(0);
01749             }
01750             this->clicked_entry = pe;
01751             this->clicked_entry->SetButtons((x >= 10) != (_current_text_dir == TD_RTL) ? SEF_RIGHT_DEPRESSED : SEF_LEFT_DEPRESSED);
01752             this->flags4 |= WF_TIMEOUT_BEGIN;
01753             _left_button_clicked = false;
01754           }
01755           break;
01756         }
01757 
01758         default: NOT_REACHED();
01759       }
01760 
01761       if (value != oldvalue) {
01762         if ((sd->desc.flags & SGF_PER_COMPANY) != 0) {
01763           SetCompanySetting(pe->d.entry.index, value);
01764         } else {
01765           SetSettingValue(pe->d.entry.index, value);
01766         }
01767         this->SetDirty();
01768       }
01769     } else {
01770       /* only open editbox for types that its sensible for */
01771       if (sd->desc.cmd != SDT_BOOLX && !(sd->desc.flags & SGF_MULTISTRING)) {
01772         /* Show the correct currency-translated value */
01773         if (sd->desc.flags & SGF_CURRENCY) value *= _currency->rate;
01774 
01775         this->valuewindow_entry = pe;
01776         SetDParam(0, value);
01777         ShowQueryString(STR_JUST_INT, STR_CONFIG_SETTING_QUERY_CAPTION, 10, this, CS_NUMERAL, QSF_ENABLE_DEFAULT);
01778       }
01779     }
01780   }
01781 
01782   virtual void OnTimeout()
01783   {
01784     if (this->clicked_entry != NULL) { // On timeout, release any depressed buttons
01785       this->clicked_entry->SetButtons(0);
01786       this->clicked_entry = NULL;
01787       this->SetDirty();
01788     }
01789   }
01790 
01791   virtual void OnQueryTextFinished(char *str)
01792   {
01793     /* The user pressed cancel */
01794     if (str == NULL) return;
01795 
01796     assert(this->valuewindow_entry != NULL);
01797     assert((this->valuewindow_entry->flags & SEF_KIND_MASK) == SEF_SETTING_KIND);
01798     const SettingDesc *sd = this->valuewindow_entry->d.entry.setting;
01799 
01800     int32 value;
01801     if (!StrEmpty(str)) {
01802       value = atoi(str);
01803 
01804       /* Save the correct currency-translated value */
01805       if (sd->desc.flags & SGF_CURRENCY) value /= _currency->rate;
01806     } else {
01807       value = (int32)(size_t)sd->desc.def;
01808     }
01809 
01810     if ((sd->desc.flags & SGF_PER_COMPANY) != 0) {
01811       SetCompanySetting(this->valuewindow_entry->d.entry.index, value);
01812     } else {
01813       SetSettingValue(this->valuewindow_entry->d.entry.index, value);
01814     }
01815     this->SetDirty();
01816   }
01817 
01818   virtual void OnResize()
01819   {
01820     this->vscroll->SetCapacityFromWidget(this, SETTINGSEL_OPTIONSPANEL, SETTINGTREE_TOP_OFFSET + SETTINGTREE_BOTTOM_OFFSET);
01821   }
01822 };
01823 
01824 GameSettings *GameSettingsWindow::settings_ptr = NULL;
01825 
01826 static const NWidgetPart _nested_settings_selection_widgets[] = {
01827   NWidget(NWID_HORIZONTAL),
01828     NWidget(WWT_CLOSEBOX, COLOUR_MAUVE),
01829     NWidget(WWT_CAPTION, COLOUR_MAUVE), SetDataTip(STR_CONFIG_SETTING_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
01830   EndContainer(),
01831   NWidget(NWID_HORIZONTAL),
01832     NWidget(WWT_PANEL, COLOUR_MAUVE, SETTINGSEL_OPTIONSPANEL), SetMinimalSize(400, 174), SetScrollbar(SETTINGSEL_SCROLLBAR), EndContainer(),
01833     NWidget(NWID_VERTICAL),
01834       NWidget(NWID_VSCROLLBAR, COLOUR_MAUVE, SETTINGSEL_SCROLLBAR),
01835       NWidget(WWT_RESIZEBOX, COLOUR_MAUVE),
01836     EndContainer(),
01837   EndContainer(),
01838 };
01839 
01840 static const WindowDesc _settings_selection_desc(
01841   WDP_CENTER, 450, 397,
01842   WC_GAME_OPTIONS, WC_NONE,
01843   0,
01844   _nested_settings_selection_widgets, lengthof(_nested_settings_selection_widgets)
01845 );
01846 
01848 void ShowGameSettings()
01849 {
01850   DeleteWindowById(WC_GAME_OPTIONS, 0);
01851   new GameSettingsWindow(&_settings_selection_desc);
01852 }
01853 
01854 
01864 void DrawArrowButtons(int x, int y, Colours button_colour, byte state, bool clickable_left, bool clickable_right)
01865 {
01866   int colour = _colour_gradient[button_colour][2];
01867 
01868   DrawFrameRect(x,      y + 1, x +  9, y + 9, button_colour, (state == 1) ? FR_LOWERED : FR_NONE);
01869   DrawFrameRect(x + 10, y + 1, x + 19, y + 9, button_colour, (state == 2) ? FR_LOWERED : FR_NONE);
01870   DrawSprite(SPR_ARROW_LEFT, PAL_NONE, x + WD_IMGBTN_LEFT, y + WD_IMGBTN_TOP);
01871   DrawSprite(SPR_ARROW_RIGHT, PAL_NONE, x + WD_IMGBTN_LEFT + 10, y + WD_IMGBTN_TOP);
01872 
01873   /* Grey out the buttons that aren't clickable */
01874   bool rtl = _current_text_dir == TD_RTL;
01875   if (rtl ? !clickable_right : !clickable_left) {
01876     GfxFillRect(x +  1, y + 1, x +  1 + 8, y + 8, colour, FILLRECT_CHECKER);
01877   }
01878   if (rtl ? !clickable_left : !clickable_right) {
01879     GfxFillRect(x + 11, y + 1, x + 11 + 8, y + 8, colour, FILLRECT_CHECKER);
01880   }
01881 }
01882 
01884 enum CustomCurrencyWidgets {
01885   CUSTCURR_RATE_DOWN,
01886   CUSTCURR_RATE_UP,
01887   CUSTCURR_RATE,
01888   CUSTCURR_SEPARATOR_EDIT,
01889   CUSTCURR_SEPARATOR,
01890   CUSTCURR_PREFIX_EDIT,
01891   CUSTCURR_PREFIX,
01892   CUSTCURR_SUFFIX_EDIT,
01893   CUSTCURR_SUFFIX,
01894   CUSTCURR_YEAR_DOWN,
01895   CUSTCURR_YEAR_UP,
01896   CUSTCURR_YEAR,
01897   CUSTCURR_PREVIEW,
01898 };
01899 
01900 struct CustomCurrencyWindow : Window {
01901   int query_widget;
01902 
01903   CustomCurrencyWindow(const WindowDesc *desc) : Window()
01904   {
01905     this->InitNested(desc);
01906 
01907     SetButtonState();
01908   }
01909 
01910   void SetButtonState()
01911   {
01912     this->SetWidgetDisabledState(CUSTCURR_RATE_DOWN, _custom_currency.rate == 1);
01913     this->SetWidgetDisabledState(CUSTCURR_RATE_UP, _custom_currency.rate == UINT16_MAX);
01914     this->SetWidgetDisabledState(CUSTCURR_YEAR_DOWN, _custom_currency.to_euro == CF_NOEURO);
01915     this->SetWidgetDisabledState(CUSTCURR_YEAR_UP, _custom_currency.to_euro == MAX_YEAR);
01916   }
01917 
01918   virtual void SetStringParameters(int widget) const
01919   {
01920     switch (widget) {
01921       case CUSTCURR_RATE:      SetDParam(0, 1); SetDParam(1, 1);            break;
01922       case CUSTCURR_SEPARATOR: SetDParamStr(0, _custom_currency.separator); break;
01923       case CUSTCURR_PREFIX:    SetDParamStr(0, _custom_currency.prefix);    break;
01924       case CUSTCURR_SUFFIX:    SetDParamStr(0, _custom_currency.suffix);    break;
01925       case CUSTCURR_YEAR:
01926         SetDParam(0, (_custom_currency.to_euro != CF_NOEURO) ? STR_CURRENCY_SWITCH_TO_EURO : STR_CURRENCY_SWITCH_TO_EURO_NEVER);
01927         SetDParam(1, _custom_currency.to_euro);
01928         break;
01929 
01930       case CUSTCURR_PREVIEW:
01931         SetDParam(0, 10000);
01932         break;
01933     }
01934   }
01935 
01936   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
01937   {
01938     switch (widget) {
01939       /* Set the appropriate width for the edit 'buttons' */
01940       case CUSTCURR_SEPARATOR_EDIT:
01941       case CUSTCURR_PREFIX_EDIT:
01942       case CUSTCURR_SUFFIX_EDIT:
01943         size->width  = this->GetWidget<NWidgetBase>(CUSTCURR_RATE_DOWN)->smallest_x + this->GetWidget<NWidgetBase>(CUSTCURR_RATE_UP)->smallest_x;
01944         break;
01945 
01946       /* Make sure the window is wide enough for the widest exchange rate */
01947       case CUSTCURR_RATE:
01948         SetDParam(0, 1);
01949         SetDParam(1, INT32_MAX);
01950         *size = GetStringBoundingBox(STR_CURRENCY_EXCHANGE_RATE);
01951         break;
01952     }
01953   }
01954 
01955   virtual void OnClick(Point pt, int widget, int click_count)
01956   {
01957     int line = 0;
01958     int len = 0;
01959     StringID str = 0;
01960     CharSetFilter afilter = CS_ALPHANUMERAL;
01961 
01962     switch (widget) {
01963       case CUSTCURR_RATE_DOWN:
01964         if (_custom_currency.rate > 1) _custom_currency.rate--;
01965         if (_custom_currency.rate == 1) this->DisableWidget(CUSTCURR_RATE_DOWN);
01966         this->EnableWidget(CUSTCURR_RATE_UP);
01967         break;
01968 
01969       case CUSTCURR_RATE_UP:
01970         if (_custom_currency.rate < UINT16_MAX) _custom_currency.rate++;
01971         if (_custom_currency.rate == UINT16_MAX) this->DisableWidget(CUSTCURR_RATE_UP);
01972         this->EnableWidget(CUSTCURR_RATE_DOWN);
01973         break;
01974 
01975       case CUSTCURR_RATE:
01976         SetDParam(0, _custom_currency.rate);
01977         str = STR_JUST_INT;
01978         len = 5;
01979         line = CUSTCURR_RATE;
01980         afilter = CS_NUMERAL;
01981         break;
01982 
01983       case CUSTCURR_SEPARATOR_EDIT:
01984       case CUSTCURR_SEPARATOR:
01985         SetDParamStr(0, _custom_currency.separator);
01986         str = STR_JUST_RAW_STRING;
01987         len = 1;
01988         line = CUSTCURR_SEPARATOR;
01989         break;
01990 
01991       case CUSTCURR_PREFIX_EDIT:
01992       case CUSTCURR_PREFIX:
01993         SetDParamStr(0, _custom_currency.prefix);
01994         str = STR_JUST_RAW_STRING;
01995         len = 12;
01996         line = CUSTCURR_PREFIX;
01997         break;
01998 
01999       case CUSTCURR_SUFFIX_EDIT:
02000       case CUSTCURR_SUFFIX:
02001         SetDParamStr(0, _custom_currency.suffix);
02002         str = STR_JUST_RAW_STRING;
02003         len = 12;
02004         line = CUSTCURR_SUFFIX;
02005         break;
02006 
02007       case CUSTCURR_YEAR_DOWN:
02008         _custom_currency.to_euro = (_custom_currency.to_euro <= 2000) ? CF_NOEURO : _custom_currency.to_euro - 1;
02009         if (_custom_currency.to_euro == CF_NOEURO) this->DisableWidget(CUSTCURR_YEAR_DOWN);
02010         this->EnableWidget(CUSTCURR_YEAR_UP);
02011         break;
02012 
02013       case CUSTCURR_YEAR_UP:
02014         _custom_currency.to_euro = Clamp(_custom_currency.to_euro + 1, 2000, MAX_YEAR);
02015         if (_custom_currency.to_euro == MAX_YEAR) this->DisableWidget(CUSTCURR_YEAR_UP);
02016         this->EnableWidget(CUSTCURR_YEAR_DOWN);
02017         break;
02018 
02019       case CUSTCURR_YEAR:
02020         SetDParam(0, _custom_currency.to_euro);
02021         str = STR_JUST_INT;
02022         len = 7;
02023         line = CUSTCURR_YEAR;
02024         afilter = CS_NUMERAL;
02025         break;
02026     }
02027 
02028     if (len != 0) {
02029       this->query_widget = line;
02030       ShowQueryString(str, STR_CURRENCY_CHANGE_PARAMETER, len + 1, this, afilter, QSF_NONE);
02031     }
02032 
02033     this->flags4 |= WF_TIMEOUT_BEGIN;
02034     this->SetDirty();
02035   }
02036 
02037   virtual void OnQueryTextFinished(char *str)
02038   {
02039     if (str == NULL) return;
02040 
02041     switch (this->query_widget) {
02042       case CUSTCURR_RATE:
02043         _custom_currency.rate = Clamp(atoi(str), 1, UINT16_MAX);
02044         break;
02045 
02046       case CUSTCURR_SEPARATOR: // Thousands seperator
02047         strecpy(_custom_currency.separator, str, lastof(_custom_currency.separator));
02048         break;
02049 
02050       case CUSTCURR_PREFIX:
02051         strecpy(_custom_currency.prefix, str, lastof(_custom_currency.prefix));
02052         break;
02053 
02054       case CUSTCURR_SUFFIX:
02055         strecpy(_custom_currency.suffix, str, lastof(_custom_currency.suffix));
02056         break;
02057 
02058       case CUSTCURR_YEAR: { // Year to switch to euro
02059         int val = atoi(str);
02060 
02061         _custom_currency.to_euro = (val < 2000 ? CF_NOEURO : min(val, MAX_YEAR));
02062         break;
02063       }
02064     }
02065     MarkWholeScreenDirty();
02066     SetButtonState();
02067   }
02068 
02069   virtual void OnTimeout()
02070   {
02071     this->SetDirty();
02072   }
02073 };
02074 
02075 static const NWidgetPart _nested_cust_currency_widgets[] = {
02076   NWidget(NWID_HORIZONTAL),
02077     NWidget(WWT_CLOSEBOX, COLOUR_GREY),
02078     NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_CURRENCY_WINDOW, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
02079   EndContainer(),
02080   NWidget(WWT_PANEL, COLOUR_GREY),
02081     NWidget(NWID_VERTICAL, NC_EQUALSIZE), SetPIP(7, 3, 0),
02082       NWidget(NWID_HORIZONTAL), SetPIP(10, 0, 5),
02083         NWidget(WWT_PUSHARROWBTN, COLOUR_YELLOW, CUSTCURR_RATE_DOWN), SetDataTip(AWV_DECREASE, STR_CURRENCY_DECREASE_EXCHANGE_RATE_TOOLTIP),
02084         NWidget(WWT_PUSHARROWBTN, COLOUR_YELLOW, CUSTCURR_RATE_UP), SetDataTip(AWV_INCREASE, STR_CURRENCY_INCREASE_EXCHANGE_RATE_TOOLTIP),
02085         NWidget(NWID_SPACER), SetMinimalSize(5, 0),
02086         NWidget(WWT_TEXT, COLOUR_BLUE, CUSTCURR_RATE), SetDataTip(STR_CURRENCY_EXCHANGE_RATE, STR_CURRENCY_SET_EXCHANGE_RATE_TOOLTIP), SetFill(1, 0),
02087       EndContainer(),
02088       NWidget(NWID_HORIZONTAL), SetPIP(10, 0, 5),
02089         NWidget(WWT_PUSHBTN, COLOUR_DARK_BLUE, CUSTCURR_SEPARATOR_EDIT), SetDataTip(0x0, STR_CURRENCY_SET_CUSTOM_CURRENCY_SEPARATOR_TOOLTIP), SetFill(0, 1),
02090         NWidget(NWID_SPACER), SetMinimalSize(5, 0),
02091         NWidget(WWT_TEXT, COLOUR_BLUE, CUSTCURR_SEPARATOR), SetDataTip(STR_CURRENCY_SEPARATOR, STR_CURRENCY_SET_CUSTOM_CURRENCY_SEPARATOR_TOOLTIP), SetFill(1, 0),
02092       EndContainer(),
02093       NWidget(NWID_HORIZONTAL), SetPIP(10, 0, 5),
02094         NWidget(WWT_PUSHBTN, COLOUR_DARK_BLUE, CUSTCURR_PREFIX_EDIT), SetDataTip(0x0, STR_CURRENCY_SET_CUSTOM_CURRENCY_PREFIX_TOOLTIP), SetFill(0, 1),
02095         NWidget(NWID_SPACER), SetMinimalSize(5, 0),
02096         NWidget(WWT_TEXT, COLOUR_BLUE, CUSTCURR_PREFIX), SetDataTip(STR_CURRENCY_PREFIX, STR_CURRENCY_SET_CUSTOM_CURRENCY_PREFIX_TOOLTIP), SetFill(1, 0),
02097       EndContainer(),
02098       NWidget(NWID_HORIZONTAL), SetPIP(10, 0, 5),
02099         NWidget(WWT_PUSHBTN, COLOUR_DARK_BLUE, CUSTCURR_SUFFIX_EDIT), SetDataTip(0x0, STR_CURRENCY_SET_CUSTOM_CURRENCY_SUFFIX_TOOLTIP), SetFill(0, 1),
02100         NWidget(NWID_SPACER), SetMinimalSize(5, 0),
02101         NWidget(WWT_TEXT, COLOUR_BLUE, CUSTCURR_SUFFIX), SetDataTip(STR_CURRENCY_SUFFIX, STR_CURRENCY_SET_CUSTOM_CURRENCY_SUFFIX_TOOLTIP), SetFill(1, 0),
02102       EndContainer(),
02103       NWidget(NWID_HORIZONTAL), SetPIP(10, 0, 5),
02104         NWidget(WWT_PUSHARROWBTN, COLOUR_YELLOW, CUSTCURR_YEAR_DOWN), SetDataTip(AWV_DECREASE, STR_CURRENCY_DECREASE_CUSTOM_CURRENCY_TO_EURO_TOOLTIP),
02105         NWidget(WWT_PUSHARROWBTN, COLOUR_YELLOW, CUSTCURR_YEAR_UP), SetDataTip(AWV_INCREASE, STR_CURRENCY_INCREASE_CUSTOM_CURRENCY_TO_EURO_TOOLTIP),
02106         NWidget(NWID_SPACER), SetMinimalSize(5, 0),
02107         NWidget(WWT_TEXT, COLOUR_BLUE, CUSTCURR_YEAR), SetDataTip(STR_JUST_STRING, STR_CURRENCY_SET_CUSTOM_CURRENCY_TO_EURO_TOOLTIP), SetFill(1, 0),
02108       EndContainer(),
02109     EndContainer(),
02110     NWidget(WWT_LABEL, COLOUR_BLUE, CUSTCURR_PREVIEW),
02111                 SetDataTip(STR_CURRENCY_PREVIEW, STR_CURRENCY_CUSTOM_CURRENCY_PREVIEW_TOOLTIP), SetPadding(15, 1, 18, 2),
02112   EndContainer(),
02113 };
02114 
02115 static const WindowDesc _cust_currency_desc(
02116   WDP_CENTER, 0, 0,
02117   WC_CUSTOM_CURRENCY, WC_NONE,
02118   WDF_UNCLICK_BUTTONS,
02119   _nested_cust_currency_widgets, lengthof(_nested_cust_currency_widgets)
02120 );
02121 
02123 static void ShowCustCurrency()
02124 {
02125   DeleteWindowById(WC_CUSTOM_CURRENCY, 0);
02126   new CustomCurrencyWindow(&_cust_currency_desc);
02127 }