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