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