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