settings_gui.cpp

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