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