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 "settings_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 #include "textfile_gui.h"
00038 
00039 
00040 
00041 static const StringID _units_dropdown[] = {
00042   STR_GAME_OPTIONS_MEASURING_UNITS_IMPERIAL,
00043   STR_GAME_OPTIONS_MEASURING_UNITS_METRIC,
00044   STR_GAME_OPTIONS_MEASURING_UNITS_SI,
00045   INVALID_STRING_ID
00046 };
00047 
00048 static const StringID _driveside_dropdown[] = {
00049   STR_GAME_OPTIONS_ROAD_VEHICLES_DROPDOWN_LEFT,
00050   STR_GAME_OPTIONS_ROAD_VEHICLES_DROPDOWN_RIGHT,
00051   INVALID_STRING_ID
00052 };
00053 
00054 static const StringID _autosave_dropdown[] = {
00055   STR_GAME_OPTIONS_AUTOSAVE_DROPDOWN_OFF,
00056   STR_GAME_OPTIONS_AUTOSAVE_DROPDOWN_EVERY_1_MONTH,
00057   STR_GAME_OPTIONS_AUTOSAVE_DROPDOWN_EVERY_3_MONTHS,
00058   STR_GAME_OPTIONS_AUTOSAVE_DROPDOWN_EVERY_6_MONTHS,
00059   STR_GAME_OPTIONS_AUTOSAVE_DROPDOWN_EVERY_12_MONTHS,
00060   INVALID_STRING_ID,
00061 };
00062 
00063 int _nb_orig_names = SPECSTR_TOWNNAME_LAST - SPECSTR_TOWNNAME_START + 1; 
00064 static StringID *_grf_names = NULL; 
00065 static int _nb_grf_names = 0;       
00066 
00068 void InitGRFTownGeneratorNames()
00069 {
00070   free(_grf_names);
00071   _grf_names = GetGRFTownNameList();
00072   _nb_grf_names = 0;
00073   for (StringID *s = _grf_names; *s != INVALID_STRING_ID; s++) _nb_grf_names++;
00074 }
00075 
00081 static inline StringID TownName(int town_name)
00082 {
00083   if (town_name < _nb_orig_names) return STR_GAME_OPTIONS_TOWN_NAME_ORIGINAL_ENGLISH + town_name;
00084   town_name -= _nb_orig_names;
00085   if (town_name < _nb_grf_names) return _grf_names[town_name];
00086   return STR_UNDEFINED;
00087 }
00088 
00093 static int GetCurRes()
00094 {
00095   int i;
00096 
00097   for (i = 0; i != _num_resolutions; i++) {
00098     if ((int)_resolutions[i].width == _screen.width &&
00099         (int)_resolutions[i].height == _screen.height) {
00100       break;
00101     }
00102   }
00103   return i;
00104 }
00105 
00106 static void ShowCustCurrency();
00107 
00108 template <class T>
00109 static DropDownList *BuiltSetDropDownList(int *selected_index)
00110 {
00111   int n = T::GetNumSets();
00112   *selected_index = T::GetIndexOfUsedSet();
00113 
00114   DropDownList *list = new DropDownList();
00115   for (int i = 0; i < n; i++) {
00116     list->push_back(new DropDownListCharStringItem(T::GetSet(i)->name, i, (_game_mode == GM_MENU) ? false : (*selected_index != i)));
00117   }
00118 
00119   return list;
00120 }
00121 
00123 template <class TBaseSet>
00124 struct BaseSetTextfileWindow : public TextfileWindow {
00125   const TBaseSet* baseset; 
00126   StringID content_type;   
00127 
00128   BaseSetTextfileWindow(TextfileType file_type, const TBaseSet* baseset, StringID content_type) : TextfileWindow(file_type), baseset(baseset), content_type(content_type)
00129   {
00130     const char *textfile = this->baseset->GetTextfile(file_type);
00131     this->LoadTextfile(textfile, BASESET_DIR);
00132   }
00133 
00134   /* virtual */ void SetStringParameters(int widget) const
00135   {
00136     if (widget == WID_TF_CAPTION) {
00137       SetDParam(0, content_type);
00138       SetDParamStr(1, this->baseset->name);
00139     }
00140   }
00141 };
00142 
00149 template <class TBaseSet>
00150 void ShowBaseSetTextfileWindow(TextfileType file_type, const TBaseSet* baseset, StringID content_type)
00151 {
00152   DeleteWindowByClass(WC_TEXTFILE);
00153   new BaseSetTextfileWindow<TBaseSet>(file_type, baseset, content_type);
00154 }
00155 
00156 struct GameOptionsWindow : Window {
00157   GameSettings *opt;
00158   bool reload;
00159 
00160   GameOptionsWindow(const WindowDesc *desc) : Window()
00161   {
00162     this->opt = &GetGameSettings();
00163     this->reload = false;
00164 
00165     this->InitNested(desc, WN_GAME_OPTIONS_GAME_OPTIONS);
00166     this->OnInvalidateData(0);
00167   }
00168 
00169   ~GameOptionsWindow()
00170   {
00171     DeleteWindowById(WC_CUSTOM_CURRENCY, 0);
00172     if (this->reload) _switch_mode = SM_MENU;
00173   }
00174 
00181   DropDownList *BuildDropDownList(int widget, int *selected_index) const
00182   {
00183     DropDownList *list = NULL;
00184     switch (widget) {
00185       case WID_GO_CURRENCY_DROPDOWN: { // Setup currencies dropdown
00186         list = new DropDownList();
00187         *selected_index = this->opt->locale.currency;
00188         StringID *items = BuildCurrencyDropdown();
00189         uint disabled = _game_mode == GM_MENU ? 0 : ~GetMaskOfAllowedCurrencies();
00190         int custom_index = -1;
00191 
00192         /* Add non-custom currencies; sorted naturally */
00193         for (uint i = 0; *items != INVALID_STRING_ID; items++, i++) {
00194           if (*items == STR_GAME_OPTIONS_CURRENCY_CUSTOM) {
00195             custom_index = i;
00196           } else {
00197             list->push_back(new DropDownListStringItem(*items, i, HasBit(disabled, i)));
00198           }
00199         }
00200         list->sort(DropDownListStringItem::NatSortFunc);
00201 
00202         /* Append custom currency at the end */
00203         if (custom_index >= 0) {
00204           list->push_back(new DropDownListItem(-1, false)); // separator line
00205           list->push_back(new DropDownListStringItem(STR_GAME_OPTIONS_CURRENCY_CUSTOM, custom_index, HasBit(disabled, custom_index)));
00206         }
00207         break;
00208       }
00209 
00210       case WID_GO_DISTANCE_DROPDOWN: { // Setup distance unit dropdown
00211         list = new DropDownList();
00212         *selected_index = this->opt->locale.units;
00213         const StringID *items = _units_dropdown;
00214         for (uint i = 0; *items != INVALID_STRING_ID; items++, i++) {
00215           list->push_back(new DropDownListStringItem(*items, i, false));
00216         }
00217         break;
00218       }
00219 
00220       case WID_GO_ROADSIDE_DROPDOWN: { // Setup road-side dropdown
00221         list = new DropDownList();
00222         *selected_index = this->opt->vehicle.road_side;
00223         const StringID *items = _driveside_dropdown;
00224         uint disabled = 0;
00225 
00226         /* You can only change the drive side if you are in the menu or ingame with
00227          * no vehicles present. In a networking game only the server can change it */
00228         extern bool RoadVehiclesAreBuilt();
00229         if ((_game_mode != GM_MENU && RoadVehiclesAreBuilt()) || (_networking && !_network_server)) {
00230           disabled = ~(1 << this->opt->vehicle.road_side); // disable the other value
00231         }
00232 
00233         for (uint i = 0; *items != INVALID_STRING_ID; items++, i++) {
00234           list->push_back(new DropDownListStringItem(*items, i, HasBit(disabled, i)));
00235         }
00236         break;
00237       }
00238 
00239       case WID_GO_TOWNNAME_DROPDOWN: { // Setup townname dropdown
00240         list = new DropDownList();
00241         *selected_index = this->opt->game_creation.town_name;
00242 
00243         int enabled_item = (_game_mode == GM_MENU || Town::GetNumItems() == 0) ? -1 : *selected_index;
00244 
00245         /* Add and sort original townnames generators */
00246         for (int i = 0; i < _nb_orig_names; i++) {
00247           list->push_back(new DropDownListStringItem(STR_GAME_OPTIONS_TOWN_NAME_ORIGINAL_ENGLISH + i, i, enabled_item != i && enabled_item >= 0));
00248         }
00249         list->sort(DropDownListStringItem::NatSortFunc);
00250 
00251         /* Add and sort newgrf townnames generators */
00252         DropDownList newgrf_names;
00253         for (int i = 0; i < _nb_grf_names; i++) {
00254           int result = _nb_orig_names + i;
00255           newgrf_names.push_back(new DropDownListStringItem(_grf_names[i], result, enabled_item != result && enabled_item >= 0));
00256         }
00257         newgrf_names.sort(DropDownListStringItem::NatSortFunc);
00258 
00259         /* Insert newgrf_names at the top of the list */
00260         if (newgrf_names.size() > 0) {
00261           newgrf_names.push_back(new DropDownListItem(-1, false)); // separator line
00262           list->splice(list->begin(), newgrf_names);
00263         }
00264         break;
00265       }
00266 
00267       case WID_GO_AUTOSAVE_DROPDOWN: { // Setup autosave dropdown
00268         list = new DropDownList();
00269         *selected_index = _settings_client.gui.autosave;
00270         const StringID *items = _autosave_dropdown;
00271         for (uint i = 0; *items != INVALID_STRING_ID; items++, i++) {
00272           list->push_back(new DropDownListStringItem(*items, i, false));
00273         }
00274         break;
00275       }
00276 
00277       case WID_GO_LANG_DROPDOWN: { // Setup interface language dropdown
00278         list = new DropDownList();
00279         for (uint i = 0; i < _languages.Length(); i++) {
00280           if (&_languages[i] == _current_language) *selected_index = i;
00281           list->push_back(new DropDownListStringItem(SPECSTR_LANGUAGE_START + i, i, false));
00282         }
00283         list->sort(DropDownListStringItem::NatSortFunc);
00284         break;
00285       }
00286 
00287       case WID_GO_RESOLUTION_DROPDOWN: // Setup resolution dropdown
00288         list = new DropDownList();
00289         *selected_index = GetCurRes();
00290         for (int i = 0; i < _num_resolutions; i++) {
00291           list->push_back(new DropDownListStringItem(SPECSTR_RESOLUTION_START + i, i, false));
00292         }
00293         break;
00294 
00295       case WID_GO_SCREENSHOT_DROPDOWN: // Setup screenshot format dropdown
00296         list = new DropDownList();
00297         *selected_index = _cur_screenshot_format;
00298         for (uint i = 0; i < _num_screenshot_formats; i++) {
00299           if (!GetScreenshotFormatSupports_32bpp(i) && BlitterFactoryBase::GetCurrentBlitter()->GetScreenDepth() == 32) continue;
00300           list->push_back(new DropDownListStringItem(SPECSTR_SCREENSHOT_START + i, i, false));
00301         }
00302         break;
00303 
00304       case WID_GO_BASE_GRF_DROPDOWN:
00305         list = BuiltSetDropDownList<BaseGraphics>(selected_index);
00306         break;
00307 
00308       case WID_GO_BASE_SFX_DROPDOWN:
00309         list = BuiltSetDropDownList<BaseSounds>(selected_index);
00310         break;
00311 
00312       case WID_GO_BASE_MUSIC_DROPDOWN:
00313         list = BuiltSetDropDownList<BaseMusic>(selected_index);
00314         break;
00315 
00316       default:
00317         return NULL;
00318     }
00319 
00320     return list;
00321   }
00322 
00323   virtual void SetStringParameters(int widget) const
00324   {
00325     switch (widget) {
00326       case WID_GO_CURRENCY_DROPDOWN:   SetDParam(0, _currency_specs[this->opt->locale.currency].name); break;
00327       case WID_GO_DISTANCE_DROPDOWN:   SetDParam(0, STR_GAME_OPTIONS_MEASURING_UNITS_IMPERIAL + this->opt->locale.units); break;
00328       case WID_GO_ROADSIDE_DROPDOWN:   SetDParam(0, STR_GAME_OPTIONS_ROAD_VEHICLES_DROPDOWN_LEFT + this->opt->vehicle.road_side); break;
00329       case WID_GO_TOWNNAME_DROPDOWN:   SetDParam(0, TownName(this->opt->game_creation.town_name)); break;
00330       case WID_GO_AUTOSAVE_DROPDOWN:   SetDParam(0, _autosave_dropdown[_settings_client.gui.autosave]); break;
00331       case WID_GO_LANG_DROPDOWN:       SetDParamStr(0, _current_language->own_name); break;
00332       case WID_GO_RESOLUTION_DROPDOWN: SetDParam(0, GetCurRes() == _num_resolutions ? STR_GAME_OPTIONS_RESOLUTION_OTHER : SPECSTR_RESOLUTION_START + GetCurRes()); break;
00333       case WID_GO_SCREENSHOT_DROPDOWN: SetDParam(0, SPECSTR_SCREENSHOT_START + _cur_screenshot_format); break;
00334       case WID_GO_BASE_GRF_DROPDOWN:   SetDParamStr(0, BaseGraphics::GetUsedSet()->name); break;
00335       case WID_GO_BASE_GRF_STATUS:     SetDParam(0, BaseGraphics::GetUsedSet()->GetNumInvalid()); break;
00336       case WID_GO_BASE_SFX_DROPDOWN:   SetDParamStr(0, BaseSounds::GetUsedSet()->name); break;
00337       case WID_GO_BASE_MUSIC_DROPDOWN: SetDParamStr(0, BaseMusic::GetUsedSet()->name); break;
00338       case WID_GO_BASE_MUSIC_STATUS:   SetDParam(0, BaseMusic::GetUsedSet()->GetNumInvalid()); break;
00339     }
00340   }
00341 
00342   virtual void DrawWidget(const Rect &r, int widget) const
00343   {
00344     switch (widget) {
00345       case WID_GO_BASE_GRF_DESCRIPTION:
00346         SetDParamStr(0, BaseGraphics::GetUsedSet()->GetDescription(GetCurrentLanguageIsoCode()));
00347         DrawStringMultiLine(r.left, r.right, r.top, UINT16_MAX, STR_BLACK_RAW_STRING);
00348         break;
00349 
00350       case WID_GO_BASE_SFX_DESCRIPTION:
00351         SetDParamStr(0, BaseSounds::GetUsedSet()->GetDescription(GetCurrentLanguageIsoCode()));
00352         DrawStringMultiLine(r.left, r.right, r.top, UINT16_MAX, STR_BLACK_RAW_STRING);
00353         break;
00354 
00355       case WID_GO_BASE_MUSIC_DESCRIPTION:
00356         SetDParamStr(0, BaseMusic::GetUsedSet()->GetDescription(GetCurrentLanguageIsoCode()));
00357         DrawStringMultiLine(r.left, r.right, r.top, UINT16_MAX, STR_BLACK_RAW_STRING);
00358         break;
00359     }
00360   }
00361 
00362   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00363   {
00364     switch (widget) {
00365       case WID_GO_BASE_GRF_DESCRIPTION:
00366         /* Find the biggest description for the default size. */
00367         for (int i = 0; i < BaseGraphics::GetNumSets(); i++) {
00368           SetDParamStr(0, BaseGraphics::GetSet(i)->GetDescription(GetCurrentLanguageIsoCode()));
00369           size->height = max(size->height, (uint)GetStringHeight(STR_BLACK_RAW_STRING, size->width));
00370         }
00371         break;
00372 
00373       case WID_GO_BASE_GRF_STATUS:
00374         /* Find the biggest description for the default size. */
00375         for (int i = 0; i < BaseGraphics::GetNumSets(); i++) {
00376           uint invalid_files = BaseGraphics::GetSet(i)->GetNumInvalid();
00377           if (invalid_files == 0) continue;
00378 
00379           SetDParam(0, invalid_files);
00380           *size = maxdim(*size, GetStringBoundingBox(STR_GAME_OPTIONS_BASE_GRF_STATUS));
00381         }
00382         break;
00383 
00384       case WID_GO_BASE_SFX_DESCRIPTION:
00385         /* Find the biggest description for the default size. */
00386         for (int i = 0; i < BaseSounds::GetNumSets(); i++) {
00387           SetDParamStr(0, BaseSounds::GetSet(i)->GetDescription(GetCurrentLanguageIsoCode()));
00388           size->height = max(size->height, (uint)GetStringHeight(STR_BLACK_RAW_STRING, size->width));
00389         }
00390         break;
00391 
00392       case WID_GO_BASE_MUSIC_DESCRIPTION:
00393         /* Find the biggest description for the default size. */
00394         for (int i = 0; i < BaseMusic::GetNumSets(); i++) {
00395           SetDParamStr(0, BaseMusic::GetSet(i)->GetDescription(GetCurrentLanguageIsoCode()));
00396           size->height = max(size->height, (uint)GetStringHeight(STR_BLACK_RAW_STRING, size->width));
00397         }
00398         break;
00399 
00400       case WID_GO_BASE_MUSIC_STATUS:
00401         /* Find the biggest description for the default size. */
00402         for (int i = 0; i < BaseMusic::GetNumSets(); i++) {
00403           uint invalid_files = BaseMusic::GetSet(i)->GetNumInvalid();
00404           if (invalid_files == 0) continue;
00405 
00406           SetDParam(0, invalid_files);
00407           *size = maxdim(*size, GetStringBoundingBox(STR_GAME_OPTIONS_BASE_MUSIC_STATUS));
00408         }
00409         break;
00410 
00411       default: {
00412         int selected;
00413         DropDownList *list = this->BuildDropDownList(widget, &selected);
00414         if (list != NULL) {
00415           /* Find the biggest item for the default size. */
00416           for (DropDownList::iterator it = list->begin(); it != list->end(); it++) {
00417             static const Dimension extra = {WD_DROPDOWNTEXT_LEFT + WD_DROPDOWNTEXT_RIGHT, WD_DROPDOWNTEXT_TOP + WD_DROPDOWNTEXT_BOTTOM};
00418             Dimension string_dim;
00419             int width = (*it)->Width();
00420             string_dim.width = width + extra.width;
00421             string_dim.height = (*it)->Height(width) + extra.height;
00422             *size = maxdim(*size, string_dim);
00423             delete *it;
00424           }
00425           delete list;
00426         }
00427       }
00428     }
00429   }
00430 
00431   virtual void OnClick(Point pt, int widget, int click_count)
00432   {
00433     if (widget >= WID_GO_BASE_GRF_TEXTFILE && widget < WID_GO_BASE_GRF_TEXTFILE + TFT_END) {
00434       if (BaseGraphics::GetUsedSet() == NULL) return;
00435 
00436       ShowBaseSetTextfileWindow((TextfileType)(widget - WID_GO_BASE_GRF_TEXTFILE), BaseGraphics::GetUsedSet(), STR_CONTENT_TYPE_BASE_GRAPHICS);
00437       return;
00438     }
00439     if (widget >= WID_GO_BASE_SFX_TEXTFILE && widget < WID_GO_BASE_SFX_TEXTFILE + TFT_END) {
00440       if (BaseSounds::GetUsedSet() == NULL) return;
00441 
00442       ShowBaseSetTextfileWindow((TextfileType)(widget - WID_GO_BASE_SFX_TEXTFILE), BaseSounds::GetUsedSet(), STR_CONTENT_TYPE_BASE_SOUNDS);
00443       return;
00444     }
00445     if (widget >= WID_GO_BASE_MUSIC_TEXTFILE && widget < WID_GO_BASE_MUSIC_TEXTFILE + TFT_END) {
00446       if (BaseMusic::GetUsedSet() == NULL) return;
00447 
00448       ShowBaseSetTextfileWindow((TextfileType)(widget - WID_GO_BASE_MUSIC_TEXTFILE), BaseMusic::GetUsedSet(), STR_CONTENT_TYPE_BASE_MUSIC);
00449       return;
00450     }
00451     switch (widget) {
00452       case WID_GO_FULLSCREEN_BUTTON: // Click fullscreen on/off
00453         /* try to toggle full-screen on/off */
00454         if (!ToggleFullScreen(!_fullscreen)) {
00455           ShowErrorMessage(STR_ERROR_FULLSCREEN_FAILED, INVALID_STRING_ID, WL_ERROR);
00456         }
00457         this->SetWidgetLoweredState(WID_GO_FULLSCREEN_BUTTON, _fullscreen);
00458         this->SetDirty();
00459         break;
00460 
00461       default: {
00462         int selected;
00463         DropDownList *list = this->BuildDropDownList(widget, &selected);
00464         if (list != NULL) {
00465           ShowDropDownList(this, list, selected, widget);
00466         }
00467         break;
00468       }
00469     }
00470   }
00471 
00477   template <class T>
00478   void SetMediaSet(int index)
00479   {
00480     if (_game_mode == GM_MENU) {
00481       const char *name = T::GetSet(index)->name;
00482 
00483       free(T::ini_set);
00484       T::ini_set = strdup(name);
00485 
00486       T::SetSet(name);
00487       this->reload = true;
00488       this->InvalidateData();
00489     }
00490   }
00491 
00492   virtual void OnDropdownSelect(int widget, int index)
00493   {
00494     switch (widget) {
00495       case WID_GO_CURRENCY_DROPDOWN: // Currency
00496         if (index == CUSTOM_CURRENCY_ID) ShowCustCurrency();
00497         this->opt->locale.currency = index;
00498         ReInitAllWindows();
00499         break;
00500 
00501       case WID_GO_DISTANCE_DROPDOWN: // Measuring units
00502         this->opt->locale.units = index;
00503         MarkWholeScreenDirty();
00504         break;
00505 
00506       case WID_GO_ROADSIDE_DROPDOWN: // Road side
00507         if (this->opt->vehicle.road_side != index) { // only change if setting changed
00508           uint i;
00509           if (GetSettingFromName("vehicle.road_side", &i) == NULL) NOT_REACHED();
00510           SetSettingValue(i, index);
00511           MarkWholeScreenDirty();
00512         }
00513         break;
00514 
00515       case WID_GO_TOWNNAME_DROPDOWN: // Town names
00516         if (_game_mode == GM_MENU || Town::GetNumItems() == 0) {
00517           this->opt->game_creation.town_name = index;
00518           SetWindowDirty(WC_GAME_OPTIONS, WN_GAME_OPTIONS_GAME_OPTIONS);
00519         }
00520         break;
00521 
00522       case WID_GO_AUTOSAVE_DROPDOWN: // Autosave options
00523         _settings_client.gui.autosave = index;
00524         this->SetDirty();
00525         break;
00526 
00527       case WID_GO_LANG_DROPDOWN: // Change interface language
00528         ReadLanguagePack(&_languages[index]);
00529         DeleteWindowByClass(WC_QUERY_STRING);
00530         CheckForMissingGlyphs();
00531         UpdateAllVirtCoords();
00532         ReInitAllWindows();
00533         break;
00534 
00535       case WID_GO_RESOLUTION_DROPDOWN: // Change resolution
00536         if (index < _num_resolutions && ChangeResInGame(_resolutions[index].width, _resolutions[index].height)) {
00537           this->SetDirty();
00538         }
00539         break;
00540 
00541       case WID_GO_SCREENSHOT_DROPDOWN: // Change screenshot format
00542         SetScreenshotFormat(index);
00543         this->SetDirty();
00544         break;
00545 
00546       case WID_GO_BASE_GRF_DROPDOWN:
00547         this->SetMediaSet<BaseGraphics>(index);
00548         break;
00549 
00550       case WID_GO_BASE_SFX_DROPDOWN:
00551         this->SetMediaSet<BaseSounds>(index);
00552         break;
00553 
00554       case WID_GO_BASE_MUSIC_DROPDOWN:
00555         this->SetMediaSet<BaseMusic>(index);
00556         break;
00557     }
00558   }
00559 
00565   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
00566   {
00567     if (!gui_scope) return;
00568     this->SetWidgetLoweredState(WID_GO_FULLSCREEN_BUTTON, _fullscreen);
00569 
00570     bool missing_files = BaseGraphics::GetUsedSet()->GetNumMissing() == 0;
00571     this->GetWidget<NWidgetCore>(WID_GO_BASE_GRF_STATUS)->SetDataTip(missing_files ? STR_EMPTY : STR_GAME_OPTIONS_BASE_GRF_STATUS, STR_NULL);
00572 
00573     for (TextfileType tft = TFT_BEGIN; tft < TFT_END; tft++) {
00574       this->SetWidgetDisabledState(WID_GO_BASE_GRF_TEXTFILE + tft, BaseGraphics::GetUsedSet() == NULL || BaseGraphics::GetUsedSet()->GetTextfile(tft) == NULL);
00575       this->SetWidgetDisabledState(WID_GO_BASE_SFX_TEXTFILE + tft, BaseSounds::GetUsedSet() == NULL || BaseSounds::GetUsedSet()->GetTextfile(tft) == NULL);
00576       this->SetWidgetDisabledState(WID_GO_BASE_MUSIC_TEXTFILE + tft, BaseMusic::GetUsedSet() == NULL || BaseMusic::GetUsedSet()->GetTextfile(tft) == NULL);
00577     }
00578 
00579     missing_files = BaseMusic::GetUsedSet()->GetNumInvalid() == 0;
00580     this->GetWidget<NWidgetCore>(WID_GO_BASE_MUSIC_STATUS)->SetDataTip(missing_files ? STR_EMPTY : STR_GAME_OPTIONS_BASE_MUSIC_STATUS, STR_NULL);
00581   }
00582 };
00583 
00584 static const NWidgetPart _nested_game_options_widgets[] = {
00585   NWidget(NWID_HORIZONTAL),
00586     NWidget(WWT_CLOSEBOX, COLOUR_GREY),
00587     NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_GAME_OPTIONS_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00588   EndContainer(),
00589   NWidget(WWT_PANEL, COLOUR_GREY, WID_GO_BACKGROUND), SetPIP(6, 6, 10),
00590     NWidget(NWID_HORIZONTAL), SetPIP(10, 10, 10),
00591       NWidget(NWID_VERTICAL), SetPIP(0, 6, 0),
00592         NWidget(WWT_FRAME, COLOUR_GREY), SetDataTip(STR_GAME_OPTIONS_CURRENCY_UNITS_FRAME, STR_NULL),
00593           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),
00594         EndContainer(),
00595         NWidget(WWT_FRAME, COLOUR_GREY), SetDataTip(STR_GAME_OPTIONS_ROAD_VEHICLES_FRAME, STR_NULL),
00596           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),
00597         EndContainer(),
00598         NWidget(WWT_FRAME, COLOUR_GREY), SetDataTip(STR_GAME_OPTIONS_AUTOSAVE_FRAME, STR_NULL),
00599           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),
00600         EndContainer(),
00601         NWidget(WWT_FRAME, COLOUR_GREY), SetDataTip(STR_GAME_OPTIONS_RESOLUTION, STR_NULL),
00602           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),
00603           NWidget(NWID_HORIZONTAL),
00604             NWidget(WWT_TEXT, COLOUR_GREY), SetMinimalSize(0, 12), SetFill(1, 0), SetDataTip(STR_GAME_OPTIONS_FULLSCREEN, STR_NULL),
00605             NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_GO_FULLSCREEN_BUTTON), SetMinimalSize(21, 9), SetDataTip(STR_EMPTY, STR_GAME_OPTIONS_FULLSCREEN_TOOLTIP),
00606           EndContainer(),
00607         EndContainer(),
00608       EndContainer(),
00609 
00610       NWidget(NWID_VERTICAL), SetPIP(0, 6, 0),
00611         NWidget(WWT_FRAME, COLOUR_GREY), SetDataTip(STR_GAME_OPTIONS_MEASURING_UNITS_FRAME, STR_NULL),
00612           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),
00613         EndContainer(),
00614         NWidget(WWT_FRAME, COLOUR_GREY), SetDataTip(STR_GAME_OPTIONS_TOWN_NAMES_FRAME, STR_NULL),
00615           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),
00616         EndContainer(),
00617         NWidget(WWT_FRAME, COLOUR_GREY), SetDataTip(STR_GAME_OPTIONS_LANGUAGE, STR_NULL),
00618           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),
00619         EndContainer(),
00620         NWidget(WWT_FRAME, COLOUR_GREY), SetDataTip(STR_GAME_OPTIONS_SCREENSHOT_FORMAT, STR_NULL),
00621           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),
00622         EndContainer(),
00623         NWidget(NWID_SPACER), SetMinimalSize(0, 0), SetFill(0, 1),
00624       EndContainer(),
00625     EndContainer(),
00626 
00627     NWidget(WWT_FRAME, COLOUR_GREY), SetDataTip(STR_GAME_OPTIONS_BASE_GRF, STR_NULL), SetPadding(0, 10, 0, 10),
00628       NWidget(NWID_HORIZONTAL), SetPIP(0, 30, 0),
00629         NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_GO_BASE_GRF_DROPDOWN), SetMinimalSize(150, 12), SetDataTip(STR_BLACK_RAW_STRING, STR_GAME_OPTIONS_BASE_GRF_TOOLTIP),
00630         NWidget(WWT_TEXT, COLOUR_GREY, WID_GO_BASE_GRF_STATUS), SetMinimalSize(150, 12), SetDataTip(STR_EMPTY, STR_NULL), SetFill(1, 0),
00631       EndContainer(),
00632       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, 6, 0),
00633       NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(7, 0, 7),
00634         NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_GO_BASE_GRF_TEXTFILE + TFT_README), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_TEXTFILE_VIEW_README, STR_NULL),
00635         NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_GO_BASE_GRF_TEXTFILE + TFT_CHANGELOG), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_TEXTFILE_VIEW_CHANGELOG, STR_NULL),
00636         NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_GO_BASE_GRF_TEXTFILE + TFT_LICENSE), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_TEXTFILE_VIEW_LICENCE, STR_NULL),
00637       EndContainer(),
00638     EndContainer(),
00639 
00640     NWidget(WWT_FRAME, COLOUR_GREY), SetDataTip(STR_GAME_OPTIONS_BASE_SFX, STR_NULL), SetPadding(0, 10, 0, 10),
00641       NWidget(NWID_HORIZONTAL), SetPIP(0, 30, 0),
00642         NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_GO_BASE_SFX_DROPDOWN), SetMinimalSize(150, 12), SetDataTip(STR_BLACK_RAW_STRING, STR_GAME_OPTIONS_BASE_SFX_TOOLTIP),
00643         NWidget(NWID_SPACER), SetFill(1, 0),
00644       EndContainer(),
00645       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, 6, 0),
00646       NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(7, 0, 7),
00647         NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_GO_BASE_SFX_TEXTFILE + TFT_README), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_TEXTFILE_VIEW_README, STR_NULL),
00648         NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_GO_BASE_SFX_TEXTFILE + TFT_CHANGELOG), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_TEXTFILE_VIEW_CHANGELOG, STR_NULL),
00649         NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_GO_BASE_SFX_TEXTFILE + TFT_LICENSE), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_TEXTFILE_VIEW_LICENCE, STR_NULL),
00650       EndContainer(),
00651     EndContainer(),
00652 
00653     NWidget(WWT_FRAME, COLOUR_GREY), SetDataTip(STR_GAME_OPTIONS_BASE_MUSIC, STR_NULL), SetPadding(0, 10, 0, 10),
00654       NWidget(NWID_HORIZONTAL), SetPIP(0, 30, 0),
00655         NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_GO_BASE_MUSIC_DROPDOWN), SetMinimalSize(150, 12), SetDataTip(STR_BLACK_RAW_STRING, STR_GAME_OPTIONS_BASE_MUSIC_TOOLTIP),
00656         NWidget(WWT_TEXT, COLOUR_GREY, WID_GO_BASE_MUSIC_STATUS), SetMinimalSize(150, 12), SetDataTip(STR_EMPTY, STR_NULL), SetFill(1, 0),
00657       EndContainer(),
00658       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, 6, 0),
00659       NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(7, 0, 7),
00660         NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_GO_BASE_MUSIC_TEXTFILE + TFT_README), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_TEXTFILE_VIEW_README, STR_NULL),
00661         NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_GO_BASE_MUSIC_TEXTFILE + TFT_CHANGELOG), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_TEXTFILE_VIEW_CHANGELOG, STR_NULL),
00662         NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_GO_BASE_MUSIC_TEXTFILE + TFT_LICENSE), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_TEXTFILE_VIEW_LICENCE, STR_NULL),
00663       EndContainer(),
00664     EndContainer(),
00665   EndContainer(),
00666 };
00667 
00668 static const WindowDesc _game_options_desc(
00669   WDP_CENTER, 0, 0,
00670   WC_GAME_OPTIONS, WC_NONE,
00671   WDF_UNCLICK_BUTTONS,
00672   _nested_game_options_widgets, lengthof(_nested_game_options_widgets)
00673 );
00674 
00676 void ShowGameOptions()
00677 {
00678   DeleteWindowByClass(WC_GAME_OPTIONS);
00679   new GameOptionsWindow(&_game_options_desc);
00680 }
00681 
00682 extern void StartupEconomy();
00683 
00684 void SetDifficultyLevel(int mode, DifficultySettings *gm_opt);
00685 
00686 class GameDifficultyWindow : public Window {
00687 private:
00688   /* Temporary holding place of values in the difficulty window until 'Save' is clicked */
00689   GameSettings opt_mod_temp;
00690 
00691 public:
00693   static const uint GAME_DIFFICULTY_NUM = 18;
00695   static const uint WIDGETS_PER_DIFFICULTY = 3;
00696 
00697   GameDifficultyWindow(const WindowDesc *desc) : Window()
00698   {
00699     this->InitNested(desc, WN_GAME_OPTIONS_GAME_DIFFICULTY);
00700 
00701     /* Setup disabled buttons when creating window
00702      * disable all other difficulty buttons during gameplay except for 'custom' */
00703     this->SetWidgetsDisabledState(_game_mode != GM_MENU,
00704       WID_GD_LVL_EASY,
00705       WID_GD_LVL_MEDIUM,
00706       WID_GD_LVL_HARD,
00707       WID_GD_LVL_CUSTOM,
00708       WIDGET_LIST_END);
00709     this->SetWidgetDisabledState(WID_GD_HIGHSCORE, _game_mode == GM_EDITOR || _networking); // highscore chart in multiplayer
00710     this->SetWidgetDisabledState(WID_GD_ACCEPT, _networking && !_network_server); // Save-button in multiplayer (and if client)
00711 
00712     /* Read data */
00713     this->OnInvalidateData(GOID_DIFFICULTY_CHANGED);
00714   }
00715 
00716   virtual void SetStringParameters(int widget) const
00717   {
00718     widget -= WID_GD_OPTIONS_START;
00719     if (widget < 0 || (widget % 3) != 2) return;
00720 
00721     widget /= 3;
00722 
00723     uint i;
00724     const SettingDesc *sd = GetSettingFromName("difficulty.max_no_competitors", &i) + widget;
00725     int32 value = (int32)ReadValue(GetVariableAddress(&this->opt_mod_temp, &sd->save), sd->save.conv);
00726     SetDParam(0, sd->desc.str_val + value);
00727   }
00728 
00729   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00730   {
00731     /* Only for the 'descriptions' */
00732     int index = widget - WID_GD_OPTIONS_START;
00733     if (index < 0 || (index % 3) != 2) return;
00734 
00735     index /= 3;
00736 
00737     uint i;
00738     const SettingDesc *sd = GetSettingFromName("difficulty.max_no_competitors", &i) + index;
00739     const SettingDescBase *sdb = &sd->desc;
00740 
00741     /* Get the string and try all strings from the smallest to the highest value */
00742     StringID str = this->GetWidget<NWidgetCore>(widget)->widget_data;
00743     for (int32 value = sdb->min; (uint32)value <= sdb->max; value += sdb->interval) {
00744       SetDParam(0, sdb->str_val + value);
00745       *size = maxdim(*size, GetStringBoundingBox(str));
00746     }
00747   }
00748 
00749   virtual void OnClick(Point pt, int widget, int click_count)
00750   {
00751     if (widget >= WID_GD_OPTIONS_START) {
00752       widget -= WID_GD_OPTIONS_START;
00753       if ((widget % 3) == 2) return;
00754 
00755       /* Don't allow clients to make any changes */
00756       if (_networking && !_network_server) return;
00757 
00758       uint i;
00759       const SettingDesc *sd = GetSettingFromName("difficulty.max_no_competitors", &i) + (widget / 3);
00760       const SettingDescBase *sdb = &sd->desc;
00761 
00762       int32 val = (int32)ReadValue(GetVariableAddress(&this->opt_mod_temp, &sd->save), sd->save.conv);
00763       if (widget % 3 == 1) {
00764         /* Increase button clicked */
00765         val = min(val + sdb->interval, (int32)sdb->max);
00766       } else {
00767         /* Decrease button clicked */
00768         val -= sdb->interval;
00769         val = max(val, sdb->min);
00770       }
00771 
00772       /* save value in temporary variable */
00773       WriteValue(GetVariableAddress(&this->opt_mod_temp, &sd->save), sd->save.conv, val);
00774       this->RaiseWidget(WID_GD_LVL_EASY + this->opt_mod_temp.difficulty.diff_level);
00775       SetDifficultyLevel(3, &this->opt_mod_temp.difficulty); // set difficulty level to custom
00776       this->LowerWidget(WID_GD_LVL_CUSTOM);
00777       this->InvalidateData();
00778 
00779       if (widget / 3 == 0 &&
00780           AI::GetInfoList()->size() == 0 &&
00781           this->opt_mod_temp.difficulty.max_no_competitors != 0) {
00782         ShowErrorMessage(STR_WARNING_NO_SUITABLE_AI, INVALID_STRING_ID, WL_CRITICAL);
00783       }
00784       return;
00785     }
00786 
00787     switch (widget) {
00788       case WID_GD_LVL_EASY:
00789       case WID_GD_LVL_MEDIUM:
00790       case WID_GD_LVL_HARD:
00791       case WID_GD_LVL_CUSTOM:
00792         /* temporarily change difficulty level */
00793         this->RaiseWidget(WID_GD_LVL_EASY + this->opt_mod_temp.difficulty.diff_level);
00794         SetDifficultyLevel(widget - WID_GD_LVL_EASY, &this->opt_mod_temp.difficulty);
00795         this->LowerWidget(WID_GD_LVL_EASY + this->opt_mod_temp.difficulty.diff_level);
00796         this->InvalidateData();
00797         break;
00798 
00799       case WID_GD_HIGHSCORE: // Highscore Table
00800         ShowHighscoreTable(this->opt_mod_temp.difficulty.diff_level, -1);
00801         break;
00802 
00803       case WID_GD_ACCEPT: { // Save button - save changes
00804         GameSettings *opt_ptr = &GetGameSettings();
00805 
00806         uint i;
00807         GetSettingFromName("difficulty.diff_level", &i);
00808         DoCommandP(0, i, this->opt_mod_temp.difficulty.diff_level, CMD_CHANGE_SETTING);
00809 
00810         const SettingDesc *sd = GetSettingFromName("difficulty.max_no_competitors", &i);
00811         for (uint btn = 0; btn != GAME_DIFFICULTY_NUM; btn++, sd++) {
00812           int32 new_val = (int32)ReadValue(GetVariableAddress(&this->opt_mod_temp, &sd->save), sd->save.conv);
00813           int32 cur_val = (int32)ReadValue(GetVariableAddress(opt_ptr, &sd->save), sd->save.conv);
00814           /* if setting has changed, change it */
00815           if (new_val != cur_val) {
00816             DoCommandP(0, i + btn, new_val, CMD_CHANGE_SETTING);
00817           }
00818         }
00819         delete this;
00820         /* If we are in the editor, we should reload the economy.
00821          * This way when you load a game, the max loan and interest rate
00822          * are loaded correctly. */
00823         if (_game_mode == GM_EDITOR) StartupEconomy();
00824         break;
00825       }
00826 
00827       case WID_GD_CANCEL: // Cancel button - close window, abandon changes
00828         delete this;
00829         break;
00830     }
00831   }
00832 
00838   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
00839   {
00840     if (!gui_scope) return;
00841 
00842     if (data == GOID_DIFFICULTY_CHANGED) {
00843       /* Window was created or settings were changed on server. Reread everything. */
00844 
00845       /* Copy current settings (ingame or in intro) to temporary holding place
00846        * change that when setting stuff, copy back on clicking 'OK' */
00847       this->opt_mod_temp = GetGameSettings();
00848 
00849       this->LowerWidget(WID_GD_LVL_EASY + this->opt_mod_temp.difficulty.diff_level);
00850     }
00851 
00852     uint i;
00853     const SettingDesc *sd = GetSettingFromName("difficulty.max_no_competitors", &i);
00854     for (i = 0; i < GAME_DIFFICULTY_NUM; i++, sd++) {
00855       const SettingDescBase *sdb = &sd->desc;
00856       /* skip deprecated difficulty options */
00857       if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue;
00858       int32 value = (int32)ReadValue(GetVariableAddress(&this->opt_mod_temp, &sd->save), sd->save.conv);
00859       bool disable = (sd->desc.flags & SGF_NEWGAME_ONLY) &&
00860           (_game_mode == GM_NORMAL ||
00861           (_game_mode == GM_EDITOR && (sd->desc.flags & SGF_SCENEDIT_TOO) == 0));
00862 
00863       this->SetWidgetDisabledState(WID_GD_OPTIONS_START + i * 3 + 0, disable || sdb->min == value);
00864       this->SetWidgetDisabledState(WID_GD_OPTIONS_START + i * 3 + 1, disable || sdb->max == (uint32)value);
00865     }
00866   }
00867 };
00868 
00869 static NWidgetBase *MakeDifficultyOptionsWidgets(int *biggest_index)
00870 {
00871   NWidgetVertical *vert_desc = new NWidgetVertical;
00872 
00873   int widnum = WID_GD_OPTIONS_START;
00874   uint i, j;
00875   const SettingDesc *sd = GetSettingFromName("difficulty.max_no_competitors", &i);
00876 
00877   for (i = 0, j = 0; i < GameDifficultyWindow::GAME_DIFFICULTY_NUM; i++, sd++, widnum += GameDifficultyWindow::WIDGETS_PER_DIFFICULTY) {
00878     if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue;
00879 
00880     NWidgetHorizontal *hor = new NWidgetHorizontal;
00881 
00882     /* [<] button. */
00883     NWidgetLeaf *leaf = new NWidgetLeaf(WWT_PUSHARROWBTN, COLOUR_YELLOW, widnum, AWV_DECREASE, STR_TOOLTIP_HSCROLL_BAR_SCROLLS_LIST);
00884     hor->Add(leaf);
00885 
00886     /* [>] button. */
00887     leaf = new NWidgetLeaf(WWT_PUSHARROWBTN, COLOUR_YELLOW, widnum + 1, AWV_INCREASE, STR_TOOLTIP_HSCROLL_BAR_SCROLLS_LIST);
00888     hor->Add(leaf);
00889 
00890     /* Some spacing between the text and the description */
00891     NWidgetSpacer *spacer = new NWidgetSpacer(5, 0);
00892     hor->Add(spacer);
00893 
00894     /* Descriptive text. */
00895     leaf = new NWidgetLeaf(WWT_TEXT, COLOUR_YELLOW, widnum + 2, STR_DIFFICULTY_LEVEL_SETTING_MAXIMUM_NO_COMPETITORS + (j++), STR_NULL);
00896     leaf->SetFill(1, 0);
00897     hor->Add(leaf);
00898     vert_desc->Add(hor);
00899 
00900     /* Space vertically */
00901     vert_desc->Add(new NWidgetSpacer(0, 2));
00902   }
00903   *biggest_index = widnum - 1;
00904   return vert_desc;
00905 }
00906 
00907 
00909 static const NWidgetPart _nested_game_difficulty_widgets[] = {
00910   NWidget(WWT_CAPTION, COLOUR_MAUVE), SetDataTip(STR_DIFFICULTY_LEVEL_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00911   NWidget(WWT_PANEL, COLOUR_MAUVE),
00912     NWidget(NWID_VERTICAL), SetPIP(2, 0, 2),
00913       NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(10, 0, 10),
00914         NWidget(WWT_TEXTBTN, COLOUR_YELLOW, WID_GD_LVL_EASY), SetDataTip(STR_DIFFICULTY_LEVEL_EASY, STR_NULL), SetFill(1, 0),
00915         NWidget(WWT_TEXTBTN, COLOUR_YELLOW, WID_GD_LVL_MEDIUM), SetDataTip(STR_DIFFICULTY_LEVEL_MEDIUM, STR_NULL), SetFill(1, 0),
00916         NWidget(WWT_TEXTBTN, COLOUR_YELLOW, WID_GD_LVL_HARD), SetDataTip(STR_DIFFICULTY_LEVEL_HARD, STR_NULL), SetFill(1, 0),
00917         NWidget(WWT_TEXTBTN, COLOUR_YELLOW, WID_GD_LVL_CUSTOM), SetDataTip(STR_DIFFICULTY_LEVEL_CUSTOM, STR_NULL), SetFill(1, 0),
00918       EndContainer(),
00919       NWidget(NWID_HORIZONTAL), SetPIP(10, 0, 10),
00920         NWidget(WWT_PUSHTXTBTN, COLOUR_GREEN, WID_GD_HIGHSCORE), SetDataTip(STR_DIFFICULTY_LEVEL_HIGH_SCORE_BUTTON, STR_NULL), SetFill(1, 0),
00921       EndContainer(),
00922     EndContainer(),
00923   EndContainer(),
00924   NWidget(WWT_PANEL, COLOUR_MAUVE),
00925     NWidget(NWID_VERTICAL), SetPIP(3, 0, 1),
00926       NWidget(NWID_HORIZONTAL), SetPIP(5, 0, 5),
00927         NWidgetFunction(MakeDifficultyOptionsWidgets),
00928       EndContainer(),
00929     EndContainer(),
00930   EndContainer(),
00931   NWidget(WWT_PANEL, COLOUR_MAUVE),
00932     NWidget(NWID_VERTICAL), SetPIP(2, 0, 2),
00933       NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(10, 0, 10),
00934         NWidget(NWID_SPACER), SetFill(1, 0),
00935         NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_GD_ACCEPT), SetDataTip(STR_DIFFICULTY_LEVEL_SAVE, STR_NULL), SetFill(1, 0),
00936         NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_GD_CANCEL), SetDataTip(STR_BUTTON_CANCEL, STR_NULL), SetFill(1, 0),
00937         NWidget(NWID_SPACER), SetFill(1, 0),
00938       EndContainer(),
00939     EndContainer(),
00940   EndContainer(),
00941 };
00942 
00944 static const WindowDesc _game_difficulty_desc(
00945   WDP_CENTER, 0, 0,
00946   WC_GAME_OPTIONS, WC_NONE,
00947   WDF_UNCLICK_BUTTONS,
00948   _nested_game_difficulty_widgets, lengthof(_nested_game_difficulty_widgets)
00949 );
00950 
00952 void ShowGameDifficulty()
00953 {
00954   DeleteWindowByClass(WC_GAME_OPTIONS);
00955   new GameDifficultyWindow(&_game_difficulty_desc);
00956 }
00957 
00958 static int SETTING_HEIGHT = 11;    
00959 static const int LEVEL_WIDTH = 15; 
00960 
00965 enum SettingEntryFlags {
00966   SEF_LEFT_DEPRESSED  = 0x01, 
00967   SEF_RIGHT_DEPRESSED = 0x02, 
00968   SEF_BUTTONS_MASK = (SEF_LEFT_DEPRESSED | SEF_RIGHT_DEPRESSED), 
00969 
00970   SEF_LAST_FIELD = 0x04, 
00971 
00972   /* Entry kind */
00973   SEF_SETTING_KIND = 0x10, 
00974   SEF_SUBTREE_KIND = 0x20, 
00975   SEF_KIND_MASK    = (SEF_SETTING_KIND | SEF_SUBTREE_KIND), 
00976 };
00977 
00978 struct SettingsPage; // Forward declaration
00979 
00981 struct SettingEntrySubtree {
00982   SettingsPage *page; 
00983   bool folded;        
00984   StringID title;     
00985 };
00986 
00988 struct SettingEntrySetting {
00989   const char *name;           
00990   const SettingDesc *setting; 
00991   uint index;                 
00992 };
00993 
00995 struct SettingEntry {
00996   byte flags; 
00997   byte level; 
00998   union {
00999     SettingEntrySetting entry; 
01000     SettingEntrySubtree sub;   
01001   } d; 
01002 
01003   SettingEntry(const char *nm);
01004   SettingEntry(SettingsPage *sub, StringID title);
01005 
01006   void Init(byte level, bool last_field);
01007   void FoldAll();
01008   void SetButtons(byte new_val);
01009 
01010   uint Length() const;
01011   SettingEntry *FindEntry(uint row, uint *cur_row);
01012   uint GetMaxHelpHeight(int maxw);
01013 
01014   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, SettingEntry *selected);
01015 
01020   inline StringID GetHelpText()
01021   {
01022     assert((this->flags & SEF_KIND_MASK) == SEF_SETTING_KIND);
01023     return this->d.entry.setting->desc.str_help;
01024   }
01025 
01026   void SetValueDParams(uint first_param, int32 value);
01027 
01028 private:
01029   void DrawSetting(GameSettings *settings_ptr, int x, int y, int max_x, int state, bool highlight);
01030 };
01031 
01033 struct SettingsPage {
01034   SettingEntry *entries; 
01035   byte num;              
01036 
01037   void Init(byte level = 0);
01038   void FoldAll();
01039 
01040   uint Length() const;
01041   SettingEntry *FindEntry(uint row, uint *cur_row) const;
01042   uint GetMaxHelpHeight(int maxw);
01043 
01044   uint Draw(GameSettings *settings_ptr, int base_x, int base_y, int max_x, uint first_row, uint max_row, SettingEntry *selected, uint cur_row = 0, uint parent_last = 0) const;
01045 };
01046 
01047 
01048 /* == SettingEntry methods == */
01049 
01054 SettingEntry::SettingEntry(const char *nm)
01055 {
01056   this->flags = SEF_SETTING_KIND;
01057   this->level = 0;
01058   this->d.entry.name = nm;
01059   this->d.entry.setting = NULL;
01060   this->d.entry.index = 0;
01061 }
01062 
01068 SettingEntry::SettingEntry(SettingsPage *sub, StringID title)
01069 {
01070   this->flags = SEF_SUBTREE_KIND;
01071   this->level = 0;
01072   this->d.sub.page = sub;
01073   this->d.sub.folded = true;
01074   this->d.sub.title = title;
01075 }
01076 
01082 void SettingEntry::Init(byte level, bool last_field)
01083 {
01084   this->level = level;
01085   if (last_field) this->flags |= SEF_LAST_FIELD;
01086 
01087   switch (this->flags & SEF_KIND_MASK) {
01088     case SEF_SETTING_KIND:
01089       this->d.entry.setting = GetSettingFromName(this->d.entry.name, &this->d.entry.index);
01090       assert(this->d.entry.setting != NULL);
01091       break;
01092     case SEF_SUBTREE_KIND:
01093       this->d.sub.page->Init(level + 1);
01094       break;
01095     default: NOT_REACHED();
01096   }
01097 }
01098 
01100 void SettingEntry::FoldAll()
01101 {
01102   switch (this->flags & SEF_KIND_MASK) {
01103     case SEF_SETTING_KIND:
01104       break;
01105 
01106     case SEF_SUBTREE_KIND:
01107       this->d.sub.folded = true;
01108       this->d.sub.page->FoldAll();
01109       break;
01110 
01111     default: NOT_REACHED();
01112   }
01113 }
01114 
01115 
01121 void SettingEntry::SetButtons(byte new_val)
01122 {
01123   assert((new_val & ~SEF_BUTTONS_MASK) == 0); // Should not touch any flags outside the buttons
01124   this->flags = (this->flags & ~SEF_BUTTONS_MASK) | new_val;
01125 }
01126 
01128 uint SettingEntry::Length() const
01129 {
01130   switch (this->flags & SEF_KIND_MASK) {
01131     case SEF_SETTING_KIND:
01132       return 1;
01133     case SEF_SUBTREE_KIND:
01134       if (this->d.sub.folded) return 1; // Only displaying the title
01135 
01136       return 1 + this->d.sub.page->Length(); // 1 extra row for the title
01137     default: NOT_REACHED();
01138   }
01139 }
01140 
01147 SettingEntry *SettingEntry::FindEntry(uint row_num, uint *cur_row)
01148 {
01149   if (row_num == *cur_row) return this;
01150 
01151   switch (this->flags & SEF_KIND_MASK) {
01152     case SEF_SETTING_KIND:
01153       (*cur_row)++;
01154       break;
01155     case SEF_SUBTREE_KIND:
01156       (*cur_row)++; // add one for row containing the title
01157       if (this->d.sub.folded) {
01158         break;
01159       }
01160 
01161       /* sub-page is visible => search it too */
01162       return this->d.sub.page->FindEntry(row_num, cur_row);
01163     default: NOT_REACHED();
01164   }
01165   return NULL;
01166 }
01167 
01173 uint SettingEntry::GetMaxHelpHeight(int maxw)
01174 {
01175   switch (this->flags & SEF_KIND_MASK) {
01176     case SEF_SETTING_KIND: return GetStringHeight(this->GetHelpText(), maxw);
01177     case SEF_SUBTREE_KIND: return this->d.sub.page->GetMaxHelpHeight(maxw);
01178     default:               NOT_REACHED();
01179   }
01180 }
01181 
01209 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, SettingEntry *selected)
01210 {
01211   if (cur_row >= max_row) return cur_row;
01212 
01213   bool rtl = _current_text_dir == TD_RTL;
01214   int offset = rtl ? -4 : 4;
01215   int level_width = rtl ? -LEVEL_WIDTH : LEVEL_WIDTH;
01216 
01217   int x = rtl ? right : left;
01218   int y = base_y;
01219   if (cur_row >= first_row) {
01220     int colour = _colour_gradient[COLOUR_ORANGE][4];
01221     y = base_y + (cur_row - first_row) * SETTING_HEIGHT; // Compute correct y start position
01222 
01223     /* Draw vertical for parent nesting levels */
01224     for (uint lvl = 0; lvl < this->level; lvl++) {
01225       if (!HasBit(parent_last, lvl)) GfxDrawLine(x + offset, y, x + offset, y + SETTING_HEIGHT - 1, colour);
01226       x += level_width;
01227     }
01228     /* draw own |- prefix */
01229     int halfway_y = y + SETTING_HEIGHT / 2;
01230     int bottom_y = (flags & SEF_LAST_FIELD) ? halfway_y : y + SETTING_HEIGHT - 1;
01231     GfxDrawLine(x + offset, y, x + offset, bottom_y, colour);
01232     /* Small horizontal line from the last vertical line */
01233     GfxDrawLine(x + offset, halfway_y, x + level_width - offset, halfway_y, colour);
01234     x += level_width;
01235   }
01236 
01237   switch (this->flags & SEF_KIND_MASK) {
01238     case SEF_SETTING_KIND:
01239       if (cur_row >= first_row) {
01240         this->DrawSetting(settings_ptr, rtl ? left : x, rtl ? x : right, y, this->flags & SEF_BUTTONS_MASK,
01241             this == selected);
01242       }
01243       cur_row++;
01244       break;
01245     case SEF_SUBTREE_KIND:
01246       if (cur_row >= first_row) {
01247         DrawSprite((this->d.sub.folded ? SPR_CIRCLE_FOLDED : SPR_CIRCLE_UNFOLDED), PAL_NONE, rtl ? x - 8 : x, y + (SETTING_HEIGHT - 11) / 2);
01248         DrawString(rtl ? left : x + 12, rtl ? x - 12 : right, y, this->d.sub.title);
01249       }
01250       cur_row++;
01251       if (!this->d.sub.folded) {
01252         if (this->flags & SEF_LAST_FIELD) {
01253           assert(this->level < sizeof(parent_last));
01254           SetBit(parent_last, this->level); // Add own last-field state
01255         }
01256 
01257         cur_row = this->d.sub.page->Draw(settings_ptr, left, right, base_y, first_row, max_row, selected, cur_row, parent_last);
01258       }
01259       break;
01260     default: NOT_REACHED();
01261   }
01262   return cur_row;
01263 }
01264 
01265 static const void *ResolveVariableAddress(const GameSettings *settings_ptr, const SettingDesc *sd)
01266 {
01267   if ((sd->desc.flags & SGF_PER_COMPANY) != 0) {
01268     if (Company::IsValidID(_local_company) && _game_mode != GM_MENU) {
01269       return GetVariableAddress(&Company::Get(_local_company)->settings, &sd->save);
01270     } else {
01271       return GetVariableAddress(&_settings_client.company, &sd->save);
01272     }
01273   } else {
01274     return GetVariableAddress(settings_ptr, &sd->save);
01275   }
01276 }
01277 
01283 void SettingEntry::SetValueDParams(uint first_param, int32 value)
01284 {
01285   assert((this->flags & SEF_KIND_MASK) == SEF_SETTING_KIND);
01286   const SettingDescBase *sdb = &this->d.entry.setting->desc;
01287   if (sdb->cmd == SDT_BOOLX) {
01288     SetDParam(first_param++, value != 0 ? STR_CONFIG_SETTING_ON : STR_CONFIG_SETTING_OFF);
01289   } else {
01290     if ((sdb->flags & SGF_MULTISTRING) != 0) {
01291       SetDParam(first_param++, sdb->str_val - sdb->min + value);
01292     } else if ((sdb->flags & SGF_DISPLAY_ABS) != 0) {
01293       SetDParam(first_param++, sdb->str_val + ((value >= 0) ? 1 : 0));
01294       value = abs(value);
01295     } else {
01296       SetDParam(first_param++, sdb->str_val + ((value == 0 && (sdb->flags & SGF_0ISDISABLED) != 0) ? 1 : 0));
01297     }
01298     SetDParam(first_param++, value);
01299   }
01300 }
01301 
01311 void SettingEntry::DrawSetting(GameSettings *settings_ptr, int left, int right, int y, int state, bool highlight)
01312 {
01313   const SettingDesc *sd = this->d.entry.setting;
01314   const SettingDescBase *sdb = &sd->desc;
01315   const void *var = ResolveVariableAddress(settings_ptr, sd);
01316   bool editable = true;
01317 
01318   bool rtl = _current_text_dir == TD_RTL;
01319   uint buttons_left = rtl ? right + 1 - SETTING_BUTTON_WIDTH : left;
01320   uint text_left  = left + (rtl ? 0 : SETTING_BUTTON_WIDTH + 5);
01321   uint text_right = right - (rtl ? SETTING_BUTTON_WIDTH + 5 : 0);
01322   uint button_y = y + (SETTING_HEIGHT - SETTING_BUTTON_HEIGHT) / 2;
01323 
01324   /* We do not allow changes of some items when we are a client in a networkgame */
01325   if (!(sd->save.conv & SLF_NO_NETWORK_SYNC) && _networking && !_network_server && !(sdb->flags & SGF_PER_COMPANY)) editable = false;
01326   if ((sdb->flags & SGF_NETWORK_ONLY) && !_networking) editable = false;
01327   if ((sdb->flags & SGF_NO_NETWORK) && _networking) editable = false;
01328 
01329   SetDParam(0, highlight ? STR_ORANGE_STRING1_WHITE : STR_ORANGE_STRING1_LTBLUE);
01330   int32 value = (int32)ReadValue(var, sd->save.conv);
01331   if (sdb->cmd == SDT_BOOLX) {
01332     /* Draw checkbox for boolean-value either on/off */
01333     DrawBoolButton(buttons_left, button_y, value != 0, editable);
01334   } else if ((sdb->flags & SGF_MULTISTRING) != 0) {
01335     /* Draw [v] button for settings of an enum-type */
01336     DrawDropDownButton(buttons_left, button_y, COLOUR_YELLOW, state != 0, editable);
01337   } else {
01338     /* Draw [<][>] boxes for settings of an integer-type */
01339     DrawArrowButtons(buttons_left, button_y, COLOUR_YELLOW, state,
01340         editable && value != (sdb->flags & SGF_0ISDISABLED ? 0 : sdb->min), editable && (uint32)value != sdb->max);
01341   }
01342   this->SetValueDParams(1, value);
01343   DrawString(text_left, text_right, y, sdb->str, highlight ? TC_WHITE : TC_LIGHT_BLUE);
01344 }
01345 
01346 
01347 /* == SettingsPage methods == */
01348 
01353 void SettingsPage::Init(byte level)
01354 {
01355   for (uint field = 0; field < this->num; field++) {
01356     this->entries[field].Init(level, field + 1 == num);
01357   }
01358 }
01359 
01361 void SettingsPage::FoldAll()
01362 {
01363   for (uint field = 0; field < this->num; field++) {
01364     this->entries[field].FoldAll();
01365   }
01366 }
01367 
01369 uint SettingsPage::Length() const
01370 {
01371   uint length = 0;
01372   for (uint field = 0; field < this->num; field++) {
01373     length += this->entries[field].Length();
01374   }
01375   return length;
01376 }
01377 
01384 SettingEntry *SettingsPage::FindEntry(uint row_num, uint *cur_row) const
01385 {
01386   SettingEntry *pe = NULL;
01387 
01388   for (uint field = 0; field < this->num; field++) {
01389     pe = this->entries[field].FindEntry(row_num, cur_row);
01390     if (pe != NULL) {
01391       break;
01392     }
01393   }
01394   return pe;
01395 }
01396 
01402 uint SettingsPage::GetMaxHelpHeight(int maxw)
01403 {
01404   uint biggest = 0;
01405   for (uint field = 0; field < this->num; field++) {
01406     biggest = max(biggest, this->entries[field].GetMaxHelpHeight(maxw));
01407   }
01408   return biggest;
01409 }
01410 
01429 uint SettingsPage::Draw(GameSettings *settings_ptr, int left, int right, int base_y, uint first_row, uint max_row, SettingEntry *selected, uint cur_row, uint parent_last) const
01430 {
01431   if (cur_row >= max_row) return cur_row;
01432 
01433   for (uint i = 0; i < this->num; i++) {
01434     cur_row = this->entries[i].Draw(settings_ptr, left, right, base_y, first_row, max_row, cur_row, parent_last, selected);
01435     if (cur_row >= max_row) {
01436       break;
01437     }
01438   }
01439   return cur_row;
01440 }
01441 
01442 
01443 static SettingEntry _settings_ui_display[] = {
01444   SettingEntry("gui.date_format_in_default_names"),
01445   SettingEntry("gui.population_in_label"),
01446   SettingEntry("gui.measure_tooltip"),
01447   SettingEntry("gui.loading_indicators"),
01448   SettingEntry("gui.liveries"),
01449   SettingEntry("gui.show_track_reservation"),
01450   SettingEntry("gui.expenses_layout"),
01451   SettingEntry("gui.smallmap_land_colour"),
01452   SettingEntry("gui.zoom_min"),
01453   SettingEntry("gui.zoom_max"),
01454   SettingEntry("gui.graph_line_thickness"),
01455 };
01457 static SettingsPage _settings_ui_display_page = {_settings_ui_display, lengthof(_settings_ui_display)};
01458 
01459 static SettingEntry _settings_ui_interaction[] = {
01460   SettingEntry("gui.window_snap_radius"),
01461   SettingEntry("gui.window_soft_limit"),
01462   SettingEntry("gui.link_terraform_toolbar"),
01463   SettingEntry("gui.prefer_teamchat"),
01464   SettingEntry("gui.autoscroll"),
01465   SettingEntry("gui.reverse_scroll"),
01466   SettingEntry("gui.smooth_scroll"),
01467   SettingEntry("gui.left_mouse_btn_scrolling"),
01468   /* While the horizontal scrollwheel scrolling is written as general code, only
01469    *  the cocoa (OSX) driver generates input for it.
01470    *  Since it's also able to completely disable the scrollwheel will we display it on all platforms anyway */
01471   SettingEntry("gui.scrollwheel_scrolling"),
01472   SettingEntry("gui.scrollwheel_multiplier"),
01473 #ifdef __APPLE__
01474   /* We might need to emulate a right mouse button on mac */
01475   SettingEntry("gui.right_mouse_btn_emulation"),
01476 #endif
01477 };
01479 static SettingsPage _settings_ui_interaction_page = {_settings_ui_interaction, lengthof(_settings_ui_interaction)};
01480 
01481 static SettingEntry _settings_ui[] = {
01482   SettingEntry(&_settings_ui_display_page, STR_CONFIG_SETTING_DISPLAY_OPTIONS),
01483   SettingEntry(&_settings_ui_interaction_page, STR_CONFIG_SETTING_INTERACTION),
01484   SettingEntry("gui.show_finances"),
01485   SettingEntry("gui.errmsg_duration"),
01486   SettingEntry("gui.hover_delay"),
01487   SettingEntry("gui.toolbar_pos"),
01488   SettingEntry("gui.statusbar_pos"),
01489   SettingEntry("gui.newgrf_default_palette"),
01490   SettingEntry("gui.pause_on_newgame"),
01491   SettingEntry("gui.advanced_vehicle_list"),
01492   SettingEntry("gui.timetable_in_ticks"),
01493   SettingEntry("gui.timetable_arrival_departure"),
01494   SettingEntry("gui.quick_goto"),
01495   SettingEntry("gui.default_rail_type"),
01496   SettingEntry("gui.disable_unsuitable_building"),
01497   SettingEntry("gui.persistent_buildingtools"),
01498   SettingEntry("gui.coloured_news_year"),
01499 };
01501 static SettingsPage _settings_ui_page = {_settings_ui, lengthof(_settings_ui)};
01502 
01503 static SettingEntry _settings_construction_signals[] = {
01504   SettingEntry("construction.train_signal_side"),
01505   SettingEntry("gui.enable_signal_gui"),
01506   SettingEntry("gui.drag_signals_density"),
01507   SettingEntry("gui.drag_signals_fixed_distance"),
01508   SettingEntry("gui.semaphore_build_before"),
01509   SettingEntry("gui.default_signal_type"),
01510   SettingEntry("gui.cycle_signal_types"),
01511 };
01513 static SettingsPage _settings_construction_signals_page = {_settings_construction_signals, lengthof(_settings_construction_signals)};
01514 
01515 static SettingEntry _settings_construction[] = {
01516   SettingEntry(&_settings_construction_signals_page, STR_CONFIG_SETTING_CONSTRUCTION_SIGNALS),
01517   SettingEntry("construction.build_on_slopes"),
01518   SettingEntry("construction.autoslope"),
01519   SettingEntry("construction.extra_dynamite"),
01520   SettingEntry("construction.max_bridge_length"),
01521   SettingEntry("construction.max_tunnel_length"),
01522   SettingEntry("station.never_expire_airports"),
01523   SettingEntry("construction.freeform_edges"),
01524   SettingEntry("construction.extra_tree_placement"),
01525   SettingEntry("construction.command_pause_level"),
01526   SettingEntry("game_creation.head_to_head_areas"),
01527 };
01529 static SettingsPage _settings_construction_page = {_settings_construction, lengthof(_settings_construction)};
01530 
01531 static SettingEntry _settings_stations_cargo[] = {
01532   SettingEntry("order.improved_load"),
01533   SettingEntry("order.gradual_loading"),
01534   SettingEntry("order.selectgoods"),
01535 };
01537 static SettingsPage _settings_stations_cargo_page = {_settings_stations_cargo, lengthof(_settings_stations_cargo)};
01538 
01539 static SettingEntry _settings_stations[] = {
01540   SettingEntry(&_settings_stations_cargo_page, STR_CONFIG_SETTING_STATIONS_CARGOHANDLING),
01541   SettingEntry("station.adjacent_stations"),
01542   SettingEntry("station.distant_join_stations"),
01543   SettingEntry("station.station_spread"),
01544   SettingEntry("economy.station_noise_level"),
01545   SettingEntry("station.modified_catchment"),
01546   SettingEntry("construction.road_stop_on_town_road"),
01547   SettingEntry("construction.road_stop_on_competitor_road"),
01548 };
01550 static SettingsPage _settings_stations_page = {_settings_stations, lengthof(_settings_stations)};
01551 
01552 static SettingEntry _settings_economy_towns[] = {
01553   SettingEntry("economy.bribe"),
01554   SettingEntry("economy.exclusive_rights"),
01555   SettingEntry("economy.fund_roads"),
01556   SettingEntry("economy.fund_buildings"),
01557   SettingEntry("economy.town_layout"),
01558   SettingEntry("economy.allow_town_roads"),
01559   SettingEntry("economy.allow_town_level_crossings"),
01560   SettingEntry("economy.found_town"),
01561   SettingEntry("economy.mod_road_rebuild"),
01562   SettingEntry("economy.town_growth_rate"),
01563   SettingEntry("economy.larger_towns"),
01564   SettingEntry("economy.initial_city_size"),
01565 };
01567 static SettingsPage _settings_economy_towns_page = {_settings_economy_towns, lengthof(_settings_economy_towns)};
01568 
01569 static SettingEntry _settings_economy_industries[] = {
01570   SettingEntry("construction.raw_industry_construction"),
01571   SettingEntry("construction.industry_platform"),
01572   SettingEntry("economy.multiple_industry_per_town"),
01573   SettingEntry("game_creation.oil_refinery_limit"),
01574 };
01576 static SettingsPage _settings_economy_industries_page = {_settings_economy_industries, lengthof(_settings_economy_industries)};
01577 
01578 static SettingEntry _settings_economy_scripts[] = {
01579   SettingEntry("script.script_max_opcode_till_suspend"),
01580 };
01582 static SettingsPage _settings_economy_scripts_page = {_settings_economy_scripts, lengthof(_settings_economy_scripts)};
01583 
01584 static SettingEntry _settings_economy[] = {
01585   SettingEntry(&_settings_economy_towns_page, STR_CONFIG_SETTING_ECONOMY_TOWNS),
01586   SettingEntry(&_settings_economy_industries_page, STR_CONFIG_SETTING_ECONOMY_INDUSTRIES),
01587   SettingEntry(&_settings_economy_scripts_page, STR_CONFIG_SETTING_ECONOMY_SCRIPTS),
01588   SettingEntry("economy.inflation"),
01589   SettingEntry("economy.smooth_economy"),
01590   SettingEntry("economy.feeder_payment_share"),
01591   SettingEntry("economy.infrastructure_maintenance"),
01592 };
01594 static SettingsPage _settings_economy_page = {_settings_economy, lengthof(_settings_economy)};
01595 
01596 static SettingEntry _settings_ai_npc[] = {
01597   SettingEntry("ai.ai_in_multiplayer"),
01598   SettingEntry("ai.ai_disable_veh_train"),
01599   SettingEntry("ai.ai_disable_veh_roadveh"),
01600   SettingEntry("ai.ai_disable_veh_aircraft"),
01601   SettingEntry("ai.ai_disable_veh_ship"),
01602 };
01604 static SettingsPage _settings_ai_npc_page = {_settings_ai_npc, lengthof(_settings_ai_npc)};
01605 
01606 static SettingEntry _settings_ai[] = {
01607   SettingEntry(&_settings_ai_npc_page, STR_CONFIG_SETTING_AI_NPC),
01608   SettingEntry("economy.give_money"),
01609   SettingEntry("economy.allow_shares"),
01610 };
01612 static SettingsPage _settings_ai_page = {_settings_ai, lengthof(_settings_ai)};
01613 
01614 static SettingEntry _settings_vehicles_routing[] = {
01615   SettingEntry("pf.pathfinder_for_trains"),
01616   SettingEntry("pf.forbid_90_deg"),
01617   SettingEntry("pf.pathfinder_for_roadvehs"),
01618   SettingEntry("pf.roadveh_queue"),
01619   SettingEntry("pf.pathfinder_for_ships"),
01620 };
01622 static SettingsPage _settings_vehicles_routing_page = {_settings_vehicles_routing, lengthof(_settings_vehicles_routing)};
01623 
01624 static SettingEntry _settings_vehicles_autorenew[] = {
01625   SettingEntry("company.engine_renew"),
01626   SettingEntry("company.engine_renew_months"),
01627   SettingEntry("company.engine_renew_money"),
01628 };
01630 static SettingsPage _settings_vehicles_autorenew_page = {_settings_vehicles_autorenew, lengthof(_settings_vehicles_autorenew)};
01631 
01632 static SettingEntry _settings_vehicles_servicing[] = {
01633   SettingEntry("vehicle.servint_ispercent"),
01634   SettingEntry("vehicle.servint_trains"),
01635   SettingEntry("vehicle.servint_roadveh"),
01636   SettingEntry("vehicle.servint_ships"),
01637   SettingEntry("vehicle.servint_aircraft"),
01638   SettingEntry("order.no_servicing_if_no_breakdowns"),
01639   SettingEntry("order.serviceathelipad"),
01640 };
01642 static SettingsPage _settings_vehicles_servicing_page = {_settings_vehicles_servicing, lengthof(_settings_vehicles_servicing)};
01643 
01644 static SettingEntry _settings_vehicles_trains[] = {
01645   SettingEntry("pf.reverse_at_signals"),
01646   SettingEntry("vehicle.train_acceleration_model"),
01647   SettingEntry("vehicle.train_slope_steepness"),
01648   SettingEntry("vehicle.max_train_length"),
01649   SettingEntry("vehicle.wagon_speed_limits"),
01650   SettingEntry("vehicle.disable_elrails"),
01651   SettingEntry("vehicle.freight_trains"),
01652   SettingEntry("gui.stop_location"),
01653 };
01655 static SettingsPage _settings_vehicles_trains_page = {_settings_vehicles_trains, lengthof(_settings_vehicles_trains)};
01656 
01657 static SettingEntry _settings_vehicles[] = {
01658   SettingEntry(&_settings_vehicles_routing_page, STR_CONFIG_SETTING_VEHICLES_ROUTING),
01659   SettingEntry(&_settings_vehicles_autorenew_page, STR_CONFIG_SETTING_VEHICLES_AUTORENEW),
01660   SettingEntry(&_settings_vehicles_servicing_page, STR_CONFIG_SETTING_VEHICLES_SERVICING),
01661   SettingEntry(&_settings_vehicles_trains_page, STR_CONFIG_SETTING_VEHICLES_TRAINS),
01662   SettingEntry("gui.new_nonstop"),
01663   SettingEntry("gui.order_review_system"),
01664   SettingEntry("gui.vehicle_income_warn"),
01665   SettingEntry("gui.lost_vehicle_warn"),
01666   SettingEntry("vehicle.never_expire_vehicles"),
01667   SettingEntry("vehicle.max_trains"),
01668   SettingEntry("vehicle.max_roadveh"),
01669   SettingEntry("vehicle.max_aircraft"),
01670   SettingEntry("vehicle.max_ships"),
01671   SettingEntry("vehicle.plane_speed"),
01672   SettingEntry("vehicle.plane_crashes"),
01673   SettingEntry("vehicle.dynamic_engines"),
01674   SettingEntry("vehicle.roadveh_acceleration_model"),
01675   SettingEntry("vehicle.roadveh_slope_steepness"),
01676   SettingEntry("vehicle.smoke_amount"),
01677 };
01679 static SettingsPage _settings_vehicles_page = {_settings_vehicles, lengthof(_settings_vehicles)};
01680 
01681 static SettingEntry _settings_main[] = {
01682   SettingEntry(&_settings_ui_page,           STR_CONFIG_SETTING_GUI),
01683   SettingEntry(&_settings_construction_page, STR_CONFIG_SETTING_CONSTRUCTION),
01684   SettingEntry(&_settings_vehicles_page,     STR_CONFIG_SETTING_VEHICLES),
01685   SettingEntry(&_settings_stations_page,     STR_CONFIG_SETTING_STATIONS),
01686   SettingEntry(&_settings_economy_page,      STR_CONFIG_SETTING_ECONOMY),
01687   SettingEntry(&_settings_ai_page,           STR_CONFIG_SETTING_AI),
01688 };
01689 
01691 static SettingsPage _settings_main_page = {_settings_main, lengthof(_settings_main)};
01692 
01693 struct GameSettingsWindow : Window {
01694   static const int SETTINGTREE_LEFT_OFFSET   = 5; 
01695   static const int SETTINGTREE_RIGHT_OFFSET  = 5; 
01696   static const int SETTINGTREE_TOP_OFFSET    = 5; 
01697   static const int SETTINGTREE_BOTTOM_OFFSET = 5; 
01698 
01699   static GameSettings *settings_ptr; 
01700 
01701   SettingEntry *valuewindow_entry;   
01702   SettingEntry *clicked_entry;       
01703   SettingEntry *last_clicked;        
01704   SettingEntry *valuedropdown_entry; 
01705   bool closing_dropdown;             
01706 
01707   Scrollbar *vscroll;
01708 
01709   GameSettingsWindow(const WindowDesc *desc) : Window()
01710   {
01711     static bool first_time = true;
01712 
01713     settings_ptr = &GetGameSettings();
01714 
01715     /* Build up the dynamic settings-array only once per OpenTTD session */
01716     if (first_time) {
01717       _settings_main_page.Init();
01718       first_time = false;
01719     } else {
01720       _settings_main_page.FoldAll(); // Close all sub-pages
01721     }
01722 
01723     this->valuewindow_entry = NULL; // No setting entry for which a entry window is opened
01724     this->clicked_entry = NULL; // No numeric setting buttons are depressed
01725     this->last_clicked = NULL;
01726     this->valuedropdown_entry = NULL;
01727     this->closing_dropdown = false;
01728 
01729     this->CreateNestedTree(desc);
01730     this->vscroll = this->GetScrollbar(WID_GS_SCROLLBAR);
01731     this->FinishInitNested(desc, WN_GAME_OPTIONS_GAME_SETTINGS);
01732 
01733     this->vscroll->SetCount(_settings_main_page.Length());
01734   }
01735 
01736   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
01737   {
01738     switch (widget) {
01739       case WID_GS_OPTIONSPANEL:
01740         resize->height = SETTING_HEIGHT = max(11, FONT_HEIGHT_NORMAL + 1);
01741         resize->width  = 1;
01742 
01743         size->height = 5 * resize->height + SETTINGTREE_TOP_OFFSET + SETTINGTREE_BOTTOM_OFFSET;
01744         break;
01745 
01746       case WID_GS_HELP_TEXT: {
01747         static const StringID setting_types[] = {
01748           STR_CONFIG_SETTING_TYPE_CLIENT,
01749           STR_CONFIG_SETTING_TYPE_COMPANY_MENU, STR_CONFIG_SETTING_TYPE_COMPANY_INGAME,
01750           STR_CONFIG_SETTING_TYPE_GAME_MENU, STR_CONFIG_SETTING_TYPE_GAME_INGAME,
01751         };
01752         for (uint i = 0; i < lengthof(setting_types); i++) {
01753           SetDParam(0, setting_types[i]);
01754           size->width = max(size->width, GetStringBoundingBox(STR_CONFIG_SETTING_TYPE).width);
01755         }
01756         size->height = 2 * FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL +
01757             max(size->height, _settings_main_page.GetMaxHelpHeight(size->width));
01758         break;
01759       }
01760 
01761       default:
01762         break;
01763     }
01764   }
01765 
01766   virtual void OnPaint()
01767   {
01768     if (this->closing_dropdown) {
01769       this->closing_dropdown = false;
01770       assert(this->valuedropdown_entry != NULL);
01771       this->valuedropdown_entry->SetButtons(0);
01772       this->valuedropdown_entry = NULL;
01773     }
01774     this->DrawWidgets();
01775   }
01776 
01777   virtual void DrawWidget(const Rect &r, int widget) const
01778   {
01779     switch (widget) {
01780       case WID_GS_OPTIONSPANEL:
01781         _settings_main_page.Draw(settings_ptr, r.left + SETTINGTREE_LEFT_OFFSET, r.right - SETTINGTREE_RIGHT_OFFSET, r.top + SETTINGTREE_TOP_OFFSET,
01782             this->vscroll->GetPosition(), this->vscroll->GetPosition() + this->vscroll->GetCapacity(), this->last_clicked);
01783         break;
01784 
01785       case WID_GS_HELP_TEXT:
01786         if (this->last_clicked != NULL) {
01787           const SettingDesc *sd = this->last_clicked->d.entry.setting;
01788 
01789           int y = r.top;
01790           if (sd->desc.flags & SGF_PER_COMPANY) {
01791             SetDParam(0, _game_mode == GM_MENU ? STR_CONFIG_SETTING_TYPE_COMPANY_MENU : STR_CONFIG_SETTING_TYPE_COMPANY_INGAME);
01792           } else if (sd->save.conv & SLF_NOT_IN_SAVE) {
01793             SetDParam(0, STR_CONFIG_SETTING_TYPE_CLIENT);
01794           } else {
01795             SetDParam(0, _game_mode == GM_MENU ? STR_CONFIG_SETTING_TYPE_GAME_MENU : STR_CONFIG_SETTING_TYPE_GAME_INGAME);
01796           }
01797           DrawString(r.left, r.right, y, STR_CONFIG_SETTING_TYPE);
01798           y += FONT_HEIGHT_NORMAL;
01799 
01800           int32 default_value = ReadValue(&sd->desc.def, sd->save.conv);
01801           this->last_clicked->SetValueDParams(0, default_value);
01802           DrawString(r.left, r.right, y, STR_CONFIG_SETTING_DEFAULT_VALUE);
01803           y += FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL;
01804 
01805           DrawStringMultiLine(r.left, r.right, y, r.bottom, this->last_clicked->GetHelpText(), TC_WHITE);
01806         }
01807         break;
01808 
01809       default:
01810         break;
01811     }
01812   }
01813 
01818   void SetDisplayedHelpText(SettingEntry *pe)
01819   {
01820     if (this->last_clicked != pe) this->SetDirty();
01821     this->last_clicked = pe;
01822   }
01823 
01824   virtual void OnClick(Point pt, int widget, int click_count)
01825   {
01826     if (widget != WID_GS_OPTIONSPANEL) return;
01827 
01828     uint btn = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_GS_OPTIONSPANEL, SETTINGTREE_TOP_OFFSET);
01829     if (btn == INT_MAX) return;
01830 
01831     uint cur_row = 0;
01832     SettingEntry *pe = _settings_main_page.FindEntry(btn, &cur_row);
01833 
01834     if (pe == NULL) return;  // Clicked below the last setting of the page
01835 
01836     int x = (_current_text_dir == TD_RTL ? this->width - 1 - pt.x : pt.x) - SETTINGTREE_LEFT_OFFSET - (pe->level + 1) * LEVEL_WIDTH;  // Shift x coordinate
01837     if (x < 0) return;  // Clicked left of the entry
01838 
01839     if ((pe->flags & SEF_KIND_MASK) == SEF_SUBTREE_KIND) {
01840       this->SetDisplayedHelpText(NULL);
01841       pe->d.sub.folded = !pe->d.sub.folded; // Flip 'folded'-ness of the sub-page
01842 
01843       this->vscroll->SetCount(_settings_main_page.Length());
01844       this->SetDirty();
01845       return;
01846     }
01847 
01848     assert((pe->flags & SEF_KIND_MASK) == SEF_SETTING_KIND);
01849     const SettingDesc *sd = pe->d.entry.setting;
01850 
01851     /* return if action is only active in network, or only settable by server */
01852     if ((!(sd->save.conv & SLF_NO_NETWORK_SYNC) && _networking && !_network_server && !(sd->desc.flags & SGF_PER_COMPANY)) ||
01853         ((sd->desc.flags & SGF_NETWORK_ONLY) && !_networking) || ((sd->desc.flags & SGF_NO_NETWORK) && _networking)) {
01854       this->SetDisplayedHelpText(pe);
01855       return;
01856     }
01857 
01858     const void *var = ResolveVariableAddress(settings_ptr, sd);
01859     int32 value = (int32)ReadValue(var, sd->save.conv);
01860 
01861     /* clicked on the icon on the left side. Either scroller, bool on/off or dropdown */
01862     if (x < SETTING_BUTTON_WIDTH && (sd->desc.flags & SGF_MULTISTRING)) {
01863       const SettingDescBase *sdb = &sd->desc;
01864       this->SetDisplayedHelpText(pe);
01865 
01866       if (this->valuedropdown_entry == pe) {
01867         /* unclick the dropdown */
01868         HideDropDownMenu(this);
01869         this->closing_dropdown = false;
01870         this->valuedropdown_entry->SetButtons(0);
01871         this->valuedropdown_entry = NULL;
01872       } else {
01873         if (this->valuedropdown_entry != NULL) this->valuedropdown_entry->SetButtons(0);
01874         this->closing_dropdown = false;
01875 
01876         const NWidgetBase *wid = this->GetWidget<NWidgetBase>(WID_GS_OPTIONSPANEL);
01877         int rel_y = (pt.y - (int)wid->pos_y - SETTINGTREE_TOP_OFFSET) % wid->resize_y;
01878 
01879         Rect wi_rect;
01880         wi_rect.left = pt.x - (_current_text_dir == TD_RTL ? SETTING_BUTTON_WIDTH - 1 - x : x);
01881         wi_rect.right = wi_rect.left + SETTING_BUTTON_WIDTH - 1;
01882         wi_rect.top = pt.y - rel_y + (SETTING_HEIGHT - SETTING_BUTTON_HEIGHT) / 2;
01883         wi_rect.bottom = wi_rect.top + SETTING_BUTTON_HEIGHT - 1;
01884 
01885         /* For dropdowns we also have to check the y position thoroughly, the mouse may not above the just opening dropdown */
01886         if (pt.y >= wi_rect.top && pt.y <= wi_rect.bottom) {
01887           this->valuedropdown_entry = pe;
01888           this->valuedropdown_entry->SetButtons(SEF_LEFT_DEPRESSED);
01889 
01890           DropDownList *list = new DropDownList();
01891           for (int i = sdb->min; i <= (int)sdb->max; i++) {
01892             list->push_back(new DropDownListStringItem(sdb->str_val + i - sdb->min, i, false));
01893           }
01894 
01895           ShowDropDownListAt(this, list, value, -1, wi_rect, COLOUR_ORANGE, true);
01896         }
01897       }
01898       this->SetDirty();
01899     } else if (x < SETTING_BUTTON_WIDTH) {
01900       this->SetDisplayedHelpText(pe);
01901       const SettingDescBase *sdb = &sd->desc;
01902       int32 oldvalue = value;
01903 
01904       switch (sdb->cmd) {
01905         case SDT_BOOLX: value ^= 1; break;
01906         case SDT_ONEOFMANY:
01907         case SDT_NUMX: {
01908           /* Add a dynamic step-size to the scroller. In a maximum of
01909            * 50-steps you should be able to get from min to max,
01910            * unless specified otherwise in the 'interval' variable
01911            * of the current setting. */
01912           uint32 step = (sdb->interval == 0) ? ((sdb->max - sdb->min) / 50) : sdb->interval;
01913           if (step == 0) step = 1;
01914 
01915           /* don't allow too fast scrolling */
01916           if ((this->flags & WF_TIMEOUT) && this->timeout_timer > 1) {
01917             _left_button_clicked = false;
01918             return;
01919           }
01920 
01921           /* Increase or decrease the value and clamp it to extremes */
01922           if (x >= SETTING_BUTTON_WIDTH / 2) {
01923             value += step;
01924             if (sdb->min < 0) {
01925               assert((int32)sdb->max >= 0);
01926               if (value > (int32)sdb->max) value = (int32)sdb->max;
01927             } else {
01928               if ((uint32)value > sdb->max) value = (int32)sdb->max;
01929             }
01930             if (value < sdb->min) value = sdb->min; // skip between "disabled" and minimum
01931           } else {
01932             value -= step;
01933             if (value < sdb->min) value = (sdb->flags & SGF_0ISDISABLED) ? 0 : sdb->min;
01934           }
01935 
01936           /* Set up scroller timeout for numeric values */
01937           if (value != oldvalue) {
01938             if (this->clicked_entry != NULL) { // Release previous buttons if any
01939               this->clicked_entry->SetButtons(0);
01940             }
01941             this->clicked_entry = pe;
01942             this->clicked_entry->SetButtons((x >= SETTING_BUTTON_WIDTH / 2) != (_current_text_dir == TD_RTL) ? SEF_RIGHT_DEPRESSED : SEF_LEFT_DEPRESSED);
01943             this->SetTimeout();
01944             _left_button_clicked = false;
01945           }
01946           break;
01947         }
01948 
01949         default: NOT_REACHED();
01950       }
01951 
01952       if (value != oldvalue) {
01953         if ((sd->desc.flags & SGF_PER_COMPANY) != 0) {
01954           SetCompanySetting(pe->d.entry.index, value);
01955         } else {
01956           SetSettingValue(pe->d.entry.index, value);
01957         }
01958         this->SetDirty();
01959       }
01960     } else {
01961       /* Only open editbox if clicked for the second time, and only for types where it is sensible for. */
01962       if (this->last_clicked == pe && sd->desc.cmd != SDT_BOOLX && !(sd->desc.flags & SGF_MULTISTRING)) {
01963         /* Show the correct currency-translated value */
01964         if (sd->desc.flags & SGF_CURRENCY) value *= _currency->rate;
01965 
01966         this->valuewindow_entry = pe;
01967         SetDParam(0, value);
01968         ShowQueryString(STR_JUST_INT, STR_CONFIG_SETTING_QUERY_CAPTION, 10, this, CS_NUMERAL, QSF_ENABLE_DEFAULT);
01969       }
01970       this->SetDisplayedHelpText(pe);
01971     }
01972   }
01973 
01974   virtual void OnTimeout()
01975   {
01976     if (this->clicked_entry != NULL) { // On timeout, release any depressed buttons
01977       this->clicked_entry->SetButtons(0);
01978       this->clicked_entry = NULL;
01979       this->SetDirty();
01980     }
01981   }
01982 
01983   virtual void OnQueryTextFinished(char *str)
01984   {
01985     /* The user pressed cancel */
01986     if (str == NULL) return;
01987 
01988     assert(this->valuewindow_entry != NULL);
01989     assert((this->valuewindow_entry->flags & SEF_KIND_MASK) == SEF_SETTING_KIND);
01990     const SettingDesc *sd = this->valuewindow_entry->d.entry.setting;
01991 
01992     int32 value;
01993     if (!StrEmpty(str)) {
01994       value = atoi(str);
01995 
01996       /* Save the correct currency-translated value */
01997       if (sd->desc.flags & SGF_CURRENCY) value /= _currency->rate;
01998     } else {
01999       value = (int32)(size_t)sd->desc.def;
02000     }
02001 
02002     if ((sd->desc.flags & SGF_PER_COMPANY) != 0) {
02003       SetCompanySetting(this->valuewindow_entry->d.entry.index, value);
02004     } else {
02005       SetSettingValue(this->valuewindow_entry->d.entry.index, value);
02006     }
02007     this->SetDirty();
02008   }
02009 
02010   virtual void OnDropdownSelect(int widget, int index)
02011   {
02012     assert(this->valuedropdown_entry != NULL);
02013     const SettingDesc *sd = this->valuedropdown_entry->d.entry.setting;
02014     assert(sd->desc.flags & SGF_MULTISTRING);
02015 
02016     if ((sd->desc.flags & SGF_PER_COMPANY) != 0) {
02017       SetCompanySetting(this->valuedropdown_entry->d.entry.index, index);
02018     } else {
02019       SetSettingValue(this->valuedropdown_entry->d.entry.index, index);
02020     }
02021 
02022     this->SetDirty();
02023   }
02024 
02025   virtual void OnDropdownClose(Point pt, int widget, int index, bool instant_close)
02026   {
02027     /* We cannot raise the dropdown button just yet. OnClick needs some hint, whether
02028      * the same dropdown button was clicked again, and then not open the dropdown again.
02029      * So, we only remember that it was closed, and process it on the next OnPaint, which is
02030      * after OnClick. */
02031     assert(this->valuedropdown_entry != NULL);
02032     this->closing_dropdown = true;
02033     this->SetDirty();
02034   }
02035 
02036   virtual void OnResize()
02037   {
02038     this->vscroll->SetCapacityFromWidget(this, WID_GS_OPTIONSPANEL, SETTINGTREE_TOP_OFFSET + SETTINGTREE_BOTTOM_OFFSET);
02039   }
02040 };
02041 
02042 GameSettings *GameSettingsWindow::settings_ptr = NULL;
02043 
02044 static const NWidgetPart _nested_settings_selection_widgets[] = {
02045   NWidget(NWID_HORIZONTAL),
02046     NWidget(WWT_CLOSEBOX, COLOUR_MAUVE),
02047     NWidget(WWT_CAPTION, COLOUR_MAUVE), SetDataTip(STR_CONFIG_SETTING_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
02048   EndContainer(),
02049   NWidget(NWID_HORIZONTAL),
02050     NWidget(WWT_PANEL, COLOUR_MAUVE, WID_GS_OPTIONSPANEL), SetMinimalSize(400, 174), SetScrollbar(WID_GS_SCROLLBAR), EndContainer(),
02051     NWidget(NWID_VERTICAL),
02052       NWidget(NWID_VSCROLLBAR, COLOUR_MAUVE, WID_GS_SCROLLBAR),
02053     EndContainer(),
02054   EndContainer(),
02055   NWidget(WWT_PANEL, COLOUR_MAUVE), SetMinimalSize(400, 40),
02056     NWidget(WWT_EMPTY, INVALID_COLOUR, WID_GS_HELP_TEXT), SetMinimalSize(300, 25), SetFill(1, 1), SetResize(1, 0),
02057         SetPadding(WD_FRAMETEXT_TOP, WD_FRAMETEXT_RIGHT, WD_FRAMETEXT_BOTTOM, WD_FRAMETEXT_LEFT),
02058     NWidget(NWID_HORIZONTAL),
02059       NWidget(NWID_SPACER, INVALID_COLOUR), SetFill(1, 1), SetResize(1, 0),
02060       NWidget(WWT_RESIZEBOX, COLOUR_MAUVE),
02061     EndContainer(),
02062   EndContainer(),
02063 };
02064 
02065 static const WindowDesc _settings_selection_desc(
02066   WDP_CENTER, 510, 450,
02067   WC_GAME_OPTIONS, WC_NONE,
02068   0,
02069   _nested_settings_selection_widgets, lengthof(_nested_settings_selection_widgets)
02070 );
02071 
02073 void ShowGameSettings()
02074 {
02075   DeleteWindowByClass(WC_GAME_OPTIONS);
02076   new GameSettingsWindow(&_settings_selection_desc);
02077 }
02078 
02079 
02089 void DrawArrowButtons(int x, int y, Colours button_colour, byte state, bool clickable_left, bool clickable_right)
02090 {
02091   int colour = _colour_gradient[button_colour][2];
02092 
02093   DrawFrameRect(x,                            y, x + SETTING_BUTTON_WIDTH / 2 - 1, y + SETTING_BUTTON_HEIGHT - 1, button_colour, (state == 1) ? FR_LOWERED : FR_NONE);
02094   DrawFrameRect(x + SETTING_BUTTON_WIDTH / 2, y, x + SETTING_BUTTON_WIDTH     - 1, y + SETTING_BUTTON_HEIGHT - 1, button_colour, (state == 2) ? FR_LOWERED : FR_NONE);
02095   DrawSprite(SPR_ARROW_LEFT, PAL_NONE, x + WD_IMGBTN_LEFT, y + WD_IMGBTN_TOP);
02096   DrawSprite(SPR_ARROW_RIGHT, PAL_NONE, x + WD_IMGBTN_LEFT + SETTING_BUTTON_WIDTH / 2, y + WD_IMGBTN_TOP);
02097 
02098   /* Grey out the buttons that aren't clickable */
02099   bool rtl = _current_text_dir == TD_RTL;
02100   if (rtl ? !clickable_right : !clickable_left) {
02101     GfxFillRect(x                            + 1, y, x + SETTING_BUTTON_WIDTH / 2 - 1, y + SETTING_BUTTON_HEIGHT - 2, colour, FILLRECT_CHECKER);
02102   }
02103   if (rtl ? !clickable_left : !clickable_right) {
02104     GfxFillRect(x + SETTING_BUTTON_WIDTH / 2 + 1, y, x + SETTING_BUTTON_WIDTH     - 1, y + SETTING_BUTTON_HEIGHT - 2, colour, FILLRECT_CHECKER);
02105   }
02106 }
02107 
02116 void DrawDropDownButton(int x, int y, Colours button_colour, bool state, bool clickable)
02117 {
02118   static const char *DOWNARROW = "\xEE\x8A\xAA";
02119 
02120   int colour = _colour_gradient[button_colour][2];
02121 
02122   DrawFrameRect(x, y, x + SETTING_BUTTON_WIDTH - 1, y + SETTING_BUTTON_HEIGHT - 1, button_colour, state ? FR_LOWERED : FR_NONE);
02123   DrawString(x + (state ? 1 : 0), x + SETTING_BUTTON_WIDTH - (state ? 0 : 1), y + (state ? 2 : 1), DOWNARROW, TC_BLACK, SA_HOR_CENTER);
02124 
02125   if (!clickable) {
02126     GfxFillRect(x +  1, y, x + SETTING_BUTTON_WIDTH - 1, y + SETTING_BUTTON_HEIGHT - 2, colour, FILLRECT_CHECKER);
02127   }
02128 }
02129 
02137 void DrawBoolButton(int x, int y, bool state, bool clickable)
02138 {
02139   static const Colours _bool_ctabs[2][2] = {{COLOUR_CREAM, COLOUR_RED}, {COLOUR_DARK_GREEN, COLOUR_GREEN}};
02140   DrawFrameRect(x, y, x + SETTING_BUTTON_WIDTH - 1, y + SETTING_BUTTON_HEIGHT - 1, _bool_ctabs[state][clickable], state ? FR_LOWERED : FR_NONE);
02141 }
02142 
02143 struct CustomCurrencyWindow : Window {
02144   int query_widget;
02145 
02146   CustomCurrencyWindow(const WindowDesc *desc) : Window()
02147   {
02148     this->InitNested(desc);
02149 
02150     SetButtonState();
02151   }
02152 
02153   void SetButtonState()
02154   {
02155     this->SetWidgetDisabledState(WID_CC_RATE_DOWN, _custom_currency.rate == 1);
02156     this->SetWidgetDisabledState(WID_CC_RATE_UP, _custom_currency.rate == UINT16_MAX);
02157     this->SetWidgetDisabledState(WID_CC_YEAR_DOWN, _custom_currency.to_euro == CF_NOEURO);
02158     this->SetWidgetDisabledState(WID_CC_YEAR_UP, _custom_currency.to_euro == MAX_YEAR);
02159   }
02160 
02161   virtual void SetStringParameters(int widget) const
02162   {
02163     switch (widget) {
02164       case WID_CC_RATE:      SetDParam(0, 1); SetDParam(1, 1);            break;
02165       case WID_CC_SEPARATOR: SetDParamStr(0, _custom_currency.separator); break;
02166       case WID_CC_PREFIX:    SetDParamStr(0, _custom_currency.prefix);    break;
02167       case WID_CC_SUFFIX:    SetDParamStr(0, _custom_currency.suffix);    break;
02168       case WID_CC_YEAR:
02169         SetDParam(0, (_custom_currency.to_euro != CF_NOEURO) ? STR_CURRENCY_SWITCH_TO_EURO : STR_CURRENCY_SWITCH_TO_EURO_NEVER);
02170         SetDParam(1, _custom_currency.to_euro);
02171         break;
02172 
02173       case WID_CC_PREVIEW:
02174         SetDParam(0, 10000);
02175         break;
02176     }
02177   }
02178 
02179   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
02180   {
02181     switch (widget) {
02182       /* Set the appropriate width for the edit 'buttons' */
02183       case WID_CC_SEPARATOR_EDIT:
02184       case WID_CC_PREFIX_EDIT:
02185       case WID_CC_SUFFIX_EDIT:
02186         size->width  = this->GetWidget<NWidgetBase>(WID_CC_RATE_DOWN)->smallest_x + this->GetWidget<NWidgetBase>(WID_CC_RATE_UP)->smallest_x;
02187         break;
02188 
02189       /* Make sure the window is wide enough for the widest exchange rate */
02190       case WID_CC_RATE:
02191         SetDParam(0, 1);
02192         SetDParam(1, INT32_MAX);
02193         *size = GetStringBoundingBox(STR_CURRENCY_EXCHANGE_RATE);
02194         break;
02195     }
02196   }
02197 
02198   virtual void OnClick(Point pt, int widget, int click_count)
02199   {
02200     int line = 0;
02201     int len = 0;
02202     StringID str = 0;
02203     CharSetFilter afilter = CS_ALPHANUMERAL;
02204 
02205     switch (widget) {
02206       case WID_CC_RATE_DOWN:
02207         if (_custom_currency.rate > 1) _custom_currency.rate--;
02208         if (_custom_currency.rate == 1) this->DisableWidget(WID_CC_RATE_DOWN);
02209         this->EnableWidget(WID_CC_RATE_UP);
02210         break;
02211 
02212       case WID_CC_RATE_UP:
02213         if (_custom_currency.rate < UINT16_MAX) _custom_currency.rate++;
02214         if (_custom_currency.rate == UINT16_MAX) this->DisableWidget(WID_CC_RATE_UP);
02215         this->EnableWidget(WID_CC_RATE_DOWN);
02216         break;
02217 
02218       case WID_CC_RATE:
02219         SetDParam(0, _custom_currency.rate);
02220         str = STR_JUST_INT;
02221         len = 5;
02222         line = WID_CC_RATE;
02223         afilter = CS_NUMERAL;
02224         break;
02225 
02226       case WID_CC_SEPARATOR_EDIT:
02227       case WID_CC_SEPARATOR:
02228         SetDParamStr(0, _custom_currency.separator);
02229         str = STR_JUST_RAW_STRING;
02230         len = 1;
02231         line = WID_CC_SEPARATOR;
02232         break;
02233 
02234       case WID_CC_PREFIX_EDIT:
02235       case WID_CC_PREFIX:
02236         SetDParamStr(0, _custom_currency.prefix);
02237         str = STR_JUST_RAW_STRING;
02238         len = 12;
02239         line = WID_CC_PREFIX;
02240         break;
02241 
02242       case WID_CC_SUFFIX_EDIT:
02243       case WID_CC_SUFFIX:
02244         SetDParamStr(0, _custom_currency.suffix);
02245         str = STR_JUST_RAW_STRING;
02246         len = 12;
02247         line = WID_CC_SUFFIX;
02248         break;
02249 
02250       case WID_CC_YEAR_DOWN:
02251         _custom_currency.to_euro = (_custom_currency.to_euro <= 2000) ? CF_NOEURO : _custom_currency.to_euro - 1;
02252         if (_custom_currency.to_euro == CF_NOEURO) this->DisableWidget(WID_CC_YEAR_DOWN);
02253         this->EnableWidget(WID_CC_YEAR_UP);
02254         break;
02255 
02256       case WID_CC_YEAR_UP:
02257         _custom_currency.to_euro = Clamp(_custom_currency.to_euro + 1, 2000, MAX_YEAR);
02258         if (_custom_currency.to_euro == MAX_YEAR) this->DisableWidget(WID_CC_YEAR_UP);
02259         this->EnableWidget(WID_CC_YEAR_DOWN);
02260         break;
02261 
02262       case WID_CC_YEAR:
02263         SetDParam(0, _custom_currency.to_euro);
02264         str = STR_JUST_INT;
02265         len = 7;
02266         line = WID_CC_YEAR;
02267         afilter = CS_NUMERAL;
02268         break;
02269     }
02270 
02271     if (len != 0) {
02272       this->query_widget = line;
02273       ShowQueryString(str, STR_CURRENCY_CHANGE_PARAMETER, len + 1, this, afilter, QSF_NONE);
02274     }
02275 
02276     this->SetTimeout();
02277     this->SetDirty();
02278   }
02279 
02280   virtual void OnQueryTextFinished(char *str)
02281   {
02282     if (str == NULL) return;
02283 
02284     switch (this->query_widget) {
02285       case WID_CC_RATE:
02286         _custom_currency.rate = Clamp(atoi(str), 1, UINT16_MAX);
02287         break;
02288 
02289       case WID_CC_SEPARATOR: // Thousands seperator
02290         strecpy(_custom_currency.separator, str, lastof(_custom_currency.separator));
02291         break;
02292 
02293       case WID_CC_PREFIX:
02294         strecpy(_custom_currency.prefix, str, lastof(_custom_currency.prefix));
02295         break;
02296 
02297       case WID_CC_SUFFIX:
02298         strecpy(_custom_currency.suffix, str, lastof(_custom_currency.suffix));
02299         break;
02300 
02301       case WID_CC_YEAR: { // Year to switch to euro
02302         int val = atoi(str);
02303 
02304         _custom_currency.to_euro = (val < 2000 ? CF_NOEURO : min(val, MAX_YEAR));
02305         break;
02306       }
02307     }
02308     MarkWholeScreenDirty();
02309     SetButtonState();
02310   }
02311 
02312   virtual void OnTimeout()
02313   {
02314     this->SetDirty();
02315   }
02316 };
02317 
02318 static const NWidgetPart _nested_cust_currency_widgets[] = {
02319   NWidget(NWID_HORIZONTAL),
02320     NWidget(WWT_CLOSEBOX, COLOUR_GREY),
02321     NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_CURRENCY_WINDOW, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
02322   EndContainer(),
02323   NWidget(WWT_PANEL, COLOUR_GREY),
02324     NWidget(NWID_VERTICAL, NC_EQUALSIZE), SetPIP(7, 3, 0),
02325       NWidget(NWID_HORIZONTAL), SetPIP(10, 0, 5),
02326         NWidget(WWT_PUSHARROWBTN, COLOUR_YELLOW, WID_CC_RATE_DOWN), SetDataTip(AWV_DECREASE, STR_CURRENCY_DECREASE_EXCHANGE_RATE_TOOLTIP),
02327         NWidget(WWT_PUSHARROWBTN, COLOUR_YELLOW, WID_CC_RATE_UP), SetDataTip(AWV_INCREASE, STR_CURRENCY_INCREASE_EXCHANGE_RATE_TOOLTIP),
02328         NWidget(NWID_SPACER), SetMinimalSize(5, 0),
02329         NWidget(WWT_TEXT, COLOUR_BLUE, WID_CC_RATE), SetDataTip(STR_CURRENCY_EXCHANGE_RATE, STR_CURRENCY_SET_EXCHANGE_RATE_TOOLTIP), SetFill(1, 0),
02330       EndContainer(),
02331       NWidget(NWID_HORIZONTAL), SetPIP(10, 0, 5),
02332         NWidget(WWT_PUSHBTN, COLOUR_DARK_BLUE, WID_CC_SEPARATOR_EDIT), SetDataTip(0x0, STR_CURRENCY_SET_CUSTOM_CURRENCY_SEPARATOR_TOOLTIP), SetFill(0, 1),
02333         NWidget(NWID_SPACER), SetMinimalSize(5, 0),
02334         NWidget(WWT_TEXT, COLOUR_BLUE, WID_CC_SEPARATOR), SetDataTip(STR_CURRENCY_SEPARATOR, STR_CURRENCY_SET_CUSTOM_CURRENCY_SEPARATOR_TOOLTIP), SetFill(1, 0),
02335       EndContainer(),
02336       NWidget(NWID_HORIZONTAL), SetPIP(10, 0, 5),
02337         NWidget(WWT_PUSHBTN, COLOUR_DARK_BLUE, WID_CC_PREFIX_EDIT), SetDataTip(0x0, STR_CURRENCY_SET_CUSTOM_CURRENCY_PREFIX_TOOLTIP), SetFill(0, 1),
02338         NWidget(NWID_SPACER), SetMinimalSize(5, 0),
02339         NWidget(WWT_TEXT, COLOUR_BLUE, WID_CC_PREFIX), SetDataTip(STR_CURRENCY_PREFIX, STR_CURRENCY_SET_CUSTOM_CURRENCY_PREFIX_TOOLTIP), SetFill(1, 0),
02340       EndContainer(),
02341       NWidget(NWID_HORIZONTAL), SetPIP(10, 0, 5),
02342         NWidget(WWT_PUSHBTN, COLOUR_DARK_BLUE, WID_CC_SUFFIX_EDIT), SetDataTip(0x0, STR_CURRENCY_SET_CUSTOM_CURRENCY_SUFFIX_TOOLTIP), SetFill(0, 1),
02343         NWidget(NWID_SPACER), SetMinimalSize(5, 0),
02344         NWidget(WWT_TEXT, COLOUR_BLUE, WID_CC_SUFFIX), SetDataTip(STR_CURRENCY_SUFFIX, STR_CURRENCY_SET_CUSTOM_CURRENCY_SUFFIX_TOOLTIP), SetFill(1, 0),
02345       EndContainer(),
02346       NWidget(NWID_HORIZONTAL), SetPIP(10, 0, 5),
02347         NWidget(WWT_PUSHARROWBTN, COLOUR_YELLOW, WID_CC_YEAR_DOWN), SetDataTip(AWV_DECREASE, STR_CURRENCY_DECREASE_CUSTOM_CURRENCY_TO_EURO_TOOLTIP),
02348         NWidget(WWT_PUSHARROWBTN, COLOUR_YELLOW, WID_CC_YEAR_UP), SetDataTip(AWV_INCREASE, STR_CURRENCY_INCREASE_CUSTOM_CURRENCY_TO_EURO_TOOLTIP),
02349         NWidget(NWID_SPACER), SetMinimalSize(5, 0),
02350         NWidget(WWT_TEXT, COLOUR_BLUE, WID_CC_YEAR), SetDataTip(STR_JUST_STRING, STR_CURRENCY_SET_CUSTOM_CURRENCY_TO_EURO_TOOLTIP), SetFill(1, 0),
02351       EndContainer(),
02352     EndContainer(),
02353     NWidget(WWT_LABEL, COLOUR_BLUE, WID_CC_PREVIEW),
02354                 SetDataTip(STR_CURRENCY_PREVIEW, STR_CURRENCY_CUSTOM_CURRENCY_PREVIEW_TOOLTIP), SetPadding(15, 1, 18, 2),
02355   EndContainer(),
02356 };
02357 
02358 static const WindowDesc _cust_currency_desc(
02359   WDP_CENTER, 0, 0,
02360   WC_CUSTOM_CURRENCY, WC_NONE,
02361   WDF_UNCLICK_BUTTONS,
02362   _nested_cust_currency_widgets, lengthof(_nested_cust_currency_widgets)
02363 );
02364 
02366 static void ShowCustCurrency()
02367 {
02368   DeleteWindowById(WC_CUSTOM_CURRENCY, 0);
02369   new CustomCurrencyWindow(&_cust_currency_desc);
02370 }