# settings_gui.cpp

Go to the documentation of this file.

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