Translations update
[openttd/fttd.git] / src / settings_gui.cpp
blobd8e7c70b1f5110c4461406e995ac6ba2b6c63cc0
1 /* $Id$ */
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
8 */
10 /** @file settings_gui.cpp GUI for settings. */
12 #include "stdafx.h"
13 #include "currency.h"
14 #include "error.h"
15 #include "settings_gui.h"
16 #include "textbuf_gui.h"
17 #include "command_func.h"
18 #include "screenshot.h"
19 #include "network/network.h"
20 #include "town.h"
21 #include "settings_internal.h"
22 #include "townnamegen.h"
23 #include "newgrf_townname.h"
24 #include "strings_func.h"
25 #include "window_func.h"
26 #include "string.h"
27 #include "widgets/dropdown_type.h"
28 #include "widgets/dropdown_func.h"
29 #include "highscore.h"
30 #include "base_media_base.h"
31 #include "company_base.h"
32 #include "company_func.h"
33 #include "viewport_func.h"
34 #include "core/geometry_func.hpp"
35 #include "ai/ai.hpp"
36 #include "blitter/blitter.h"
37 #include "language.h"
38 #include "textfile.h"
39 #include "stringfilter_type.h"
40 #include "querystring_gui.h"
41 #include "spritecache.h"
44 static const StringID _driveside_dropdown[] = {
45 STR_GAME_OPTIONS_ROAD_VEHICLES_DROPDOWN_LEFT,
46 STR_GAME_OPTIONS_ROAD_VEHICLES_DROPDOWN_RIGHT,
47 INVALID_STRING_ID
50 static const StringID _autosave_dropdown[] = {
51 STR_GAME_OPTIONS_AUTOSAVE_DROPDOWN_OFF,
52 STR_GAME_OPTIONS_AUTOSAVE_DROPDOWN_EVERY_1_MONTH,
53 STR_GAME_OPTIONS_AUTOSAVE_DROPDOWN_EVERY_3_MONTHS,
54 STR_GAME_OPTIONS_AUTOSAVE_DROPDOWN_EVERY_6_MONTHS,
55 STR_GAME_OPTIONS_AUTOSAVE_DROPDOWN_EVERY_12_MONTHS,
56 INVALID_STRING_ID,
59 static const StringID _gui_zoom_dropdown[] = {
60 STR_GAME_OPTIONS_GUI_ZOOM_DROPDOWN_NORMAL,
61 STR_GAME_OPTIONS_GUI_ZOOM_DROPDOWN_2X_ZOOM,
62 STR_GAME_OPTIONS_GUI_ZOOM_DROPDOWN_4X_ZOOM,
63 INVALID_STRING_ID,
66 static StringID *_grf_names = NULL; ///< Pointer to town names defined by NewGRFs.
67 static int _nb_grf_names = 0; ///< Number of town names defined by NewGRFs.
69 static Dimension _circle_size; ///< Dimension of the circle +/- icon. This is here as not all users are within the class of the settings window.
71 /** Read the variable encoded in a setting description. */
72 static int64 ReadVariable (const GameSettings *settings_ptr, const SettingDesc *sd)
74 const void *object;
75 if ((sd->desc.flags & SGF_PER_COMPANY) == 0) {
76 object = settings_ptr;
77 } else if (Company::IsValidID(_local_company) && _game_mode != GM_MENU) {
78 object = &Company::Get(_local_company)->settings;
79 } else {
80 object = &_settings_client.company;
83 const void *ptr = sd->save.get_variable_address (object);
84 return ReadValue (ptr, sd->save.conv);
87 /** Allocate memory for the NewGRF town names. */
88 void InitGRFTownGeneratorNames()
90 free(_grf_names);
91 _grf_names = GetGRFTownNameList();
92 _nb_grf_names = 0;
93 for (StringID *s = _grf_names; *s != INVALID_STRING_ID; s++) _nb_grf_names++;
96 /**
97 * Get a town name.
98 * @param town_name Number of the wanted town name.
99 * @return Name of the town as string ID.
101 static inline StringID TownName(int town_name)
103 if (town_name < (int)N_ORIG_TOWN_NAME_GEN) return STR_GAME_OPTIONS_TOWN_NAME_ORIGINAL_ENGLISH + town_name;
104 town_name -= N_ORIG_TOWN_NAME_GEN;
105 if (town_name < _nb_grf_names) return _grf_names[town_name];
106 return STR_UNDEFINED;
110 * Get index of the current screen resolution.
111 * @return Index of the current screen resolution if it is a known resolution, #_num_resolutions otherwise.
113 static int GetCurRes()
115 int i;
117 for (i = 0; i != _num_resolutions; i++) {
118 if ((int)_resolutions[i].width == _screen_width &&
119 (int)_resolutions[i].height == _screen_height) {
120 break;
123 return i;
126 static void ShowCustCurrency();
128 template <class T>
129 static DropDownList *BuiltSetDropDownList(int *selected_index)
131 int n = T::GetNumSets();
132 *selected_index = T::GetIndexOfUsedSet();
134 DropDownList *list = new DropDownList();
135 for (int i = 0; i < n; i++) {
136 *list->Append() = new DropDownListCharStringItem(T::GetSet(i)->get_name(), i, (_game_mode == GM_MENU) ? false : (*selected_index != i));
139 return list;
142 /** Window for displaying the textfile of a BaseSet. */
143 template <class TBaseSet>
144 struct BaseSetTextfileWindow : public TextfileWindow {
145 const TBaseSet* baseset; ///< View the textfile of this BaseSet.
146 StringID content_type; ///< STR_CONTENT_TYPE_xxx for title.
148 BaseSetTextfileWindow (TextfileType file_type, const TBaseSet* baseset, StringID content_type)
149 : TextfileWindow (baseset->GetTextfile (file_type)),
150 baseset(baseset), content_type(content_type)
152 this->CheckForMissingGlyphs();
155 /* virtual */ void SetStringParameters(int widget) const
157 if (widget == WID_TF_CAPTION) {
158 SetDParam(0, content_type);
159 SetDParamStr(1, this->baseset->get_name());
165 * Open the BaseSet version of the textfile window.
166 * @param file_type The type of textfile to display.
167 * @param baseset The BaseSet to use.
168 * @param content_type STR_CONTENT_TYPE_xxx for title.
170 template <class TBaseSet>
171 void ShowBaseSetTextfileWindow(TextfileType file_type, const TBaseSet* baseset, StringID content_type)
173 DeleteWindowByClass(WC_TEXTFILE);
174 new BaseSetTextfileWindow<TBaseSet>(file_type, baseset, content_type);
177 struct GameOptionsWindow : Window {
178 GameSettings *opt;
179 bool reload;
181 GameOptionsWindow (const WindowDesc *desc) :
182 Window (desc), opt (&GetGameSettings()), reload (false)
184 this->InitNested(WN_GAME_OPTIONS_GAME_OPTIONS);
185 this->OnInvalidateData(0);
188 void OnDelete (void) FINAL_OVERRIDE
190 DeleteWindowById(WC_CUSTOM_CURRENCY, 0);
191 if (this->reload) _switch_mode = SM_MENU;
195 * Build the dropdown list for a specific widget.
196 * @param widget Widget to build list for
197 * @param selected_index Currently selected item
198 * @return the built dropdown list, or NULL if the widget has no dropdown menu.
200 DropDownList *BuildDropDownList(int widget, int *selected_index) const
202 DropDownList *list = NULL;
203 switch (widget) {
204 case WID_GO_CURRENCY_DROPDOWN: { // Setup currencies dropdown
205 list = new DropDownList();
206 *selected_index = this->opt->locale.currency;
207 StringID *items = BuildCurrencyDropdown();
208 uint64 disabled = _game_mode == GM_MENU ? 0LL : ~GetMaskOfAllowedCurrencies();
210 /* Add non-custom currencies; sorted naturally */
211 for (uint i = 0; i < CURRENCY_END; items++, i++) {
212 if (i == CURRENCY_CUSTOM) continue;
213 *list->Append() = new DropDownListStringItem(*items, i, HasBit(disabled, i));
215 QSortT(list->Begin(), list->Length(), DropDownListStringItem::NatSortFunc);
217 /* Append custom currency at the end */
218 *list->Append() = new DropDownListItem(-1, false); // separator line
219 *list->Append() = new DropDownListStringItem(STR_GAME_OPTIONS_CURRENCY_CUSTOM, CURRENCY_CUSTOM, HasBit(disabled, CURRENCY_CUSTOM));
220 break;
223 case WID_GO_ROADSIDE_DROPDOWN: { // Setup road-side dropdown
224 list = new DropDownList();
225 *selected_index = this->opt->vehicle.road_side;
226 const StringID *items = _driveside_dropdown;
227 uint disabled = 0;
229 /* You can only change the drive side if you are in the menu or ingame with
230 * no vehicles present. In a networking game only the server can change it */
231 extern bool RoadVehiclesAreBuilt();
232 if ((_game_mode != GM_MENU && RoadVehiclesAreBuilt()) || (_networking && !_network_server)) {
233 disabled = ~(1 << this->opt->vehicle.road_side); // disable the other value
236 for (uint i = 0; *items != INVALID_STRING_ID; items++, i++) {
237 *list->Append() = new DropDownListStringItem(*items, i, HasBit(disabled, i));
239 break;
242 case WID_GO_TOWNNAME_DROPDOWN: { // Setup townname dropdown
243 list = new DropDownList();
244 *selected_index = this->opt->game_creation.town_name;
246 int enabled_item = (_game_mode == GM_MENU || Town::GetNumItems() == 0) ? -1 : *selected_index;
248 /* Add and sort newgrf townnames generators */
249 for (int i = 0; i < _nb_grf_names; i++) {
250 int result = N_ORIG_TOWN_NAME_GEN + i;
251 *list->Append() = new DropDownListStringItem (_grf_names[i], result, enabled_item != result && enabled_item >= 0);
253 QSortT(list->Begin(), list->Length(), DropDownListStringItem::NatSortFunc);
255 int newgrf_size = list->Length();
256 /* Insert newgrf_names at the top of the list */
257 if (newgrf_size > 0) {
258 *list->Append() = new DropDownListItem(-1, false); // separator line
259 newgrf_size++;
262 /* Add and sort original townnames generators */
263 for (uint i = 0; i < N_ORIG_TOWN_NAME_GEN; i++) {
264 *list->Append() = new DropDownListStringItem(STR_GAME_OPTIONS_TOWN_NAME_ORIGINAL_ENGLISH + i, i, enabled_item != (int)i && enabled_item >= 0);
266 QSortT(list->Begin() + newgrf_size, list->Length() - newgrf_size, DropDownListStringItem::NatSortFunc);
267 break;
270 case WID_GO_AUTOSAVE_DROPDOWN: { // Setup autosave dropdown
271 list = new DropDownList();
272 *selected_index = _settings_client.gui.autosave;
273 const StringID *items = _autosave_dropdown;
274 for (uint i = 0; *items != INVALID_STRING_ID; items++, i++) {
275 *list->Append() = new DropDownListStringItem(*items, i, false);
277 break;
280 case WID_GO_LANG_DROPDOWN: { // Setup interface language dropdown
281 list = new DropDownList();
282 for (uint i = 0; i < _languages.Length(); i++) {
283 if (&_languages[i] == _current_language) *selected_index = i;
284 *list->Append() = new DropDownListStringItem(SPECSTR_LANGUAGE_START + i, i, false);
286 QSortT(list->Begin(), list->Length(), DropDownListStringItem::NatSortFunc);
287 break;
290 case WID_GO_RESOLUTION_DROPDOWN: // Setup resolution dropdown
291 if (_num_resolutions == 0) break;
293 list = new DropDownList();
294 *selected_index = GetCurRes();
295 for (int i = 0; i < _num_resolutions; i++) {
296 *list->Append() = new DropDownListStringItem(SPECSTR_RESOLUTION_START + i, i, false);
298 break;
300 case WID_GO_GUI_ZOOM_DROPDOWN: {
301 list = new DropDownList();
302 *selected_index = ZOOM_LVL_OUT_4X - _gui_zoom;
303 const StringID *items = _gui_zoom_dropdown;
304 for (int i = 0; *items != INVALID_STRING_ID; items++, i++) {
305 *list->Append() = new DropDownListStringItem(*items, i, _settings_client.gui.zoom_min > ZOOM_LVL_OUT_4X - i);
307 break;
310 case WID_GO_SCREENSHOT_DROPDOWN: // Setup screenshot format dropdown
311 list = new DropDownList();
312 *selected_index = _cur_screenshot_format;
313 for (uint i = 0; i < _num_screenshot_formats; i++) {
314 if (!GetScreenshotFormatSupports_32bpp(i) && Blitter::get()->GetScreenDepth() == 32) continue;
315 *list->Append() = new DropDownListStringItem(SPECSTR_SCREENSHOT_START + i, i, false);
317 break;
319 case WID_GO_BASE_GRF_DROPDOWN:
320 list = BuiltSetDropDownList<BaseGraphics>(selected_index);
321 break;
323 case WID_GO_BASE_SFX_DROPDOWN:
324 list = BuiltSetDropDownList<BaseSounds>(selected_index);
325 break;
327 case WID_GO_BASE_MUSIC_DROPDOWN:
328 list = BuiltSetDropDownList<BaseMusic>(selected_index);
329 break;
331 default:
332 return NULL;
335 return list;
338 virtual void SetStringParameters(int widget) const
340 switch (widget) {
341 case WID_GO_CURRENCY_DROPDOWN: SetDParam(0, _currency_specs[this->opt->locale.currency].name); break;
342 case WID_GO_ROADSIDE_DROPDOWN: SetDParam(0, STR_GAME_OPTIONS_ROAD_VEHICLES_DROPDOWN_LEFT + this->opt->vehicle.road_side); break;
343 case WID_GO_TOWNNAME_DROPDOWN: SetDParam(0, TownName(this->opt->game_creation.town_name)); break;
344 case WID_GO_AUTOSAVE_DROPDOWN: SetDParam(0, _autosave_dropdown[_settings_client.gui.autosave]); break;
345 case WID_GO_LANG_DROPDOWN: SetDParamStr(0, _current_language->own_name); break;
346 case WID_GO_RESOLUTION_DROPDOWN: SetDParam(0, GetCurRes() == _num_resolutions ? STR_GAME_OPTIONS_RESOLUTION_OTHER : SPECSTR_RESOLUTION_START + GetCurRes()); break;
347 case WID_GO_GUI_ZOOM_DROPDOWN: SetDParam(0, _gui_zoom_dropdown[ZOOM_LVL_OUT_4X - _gui_zoom]); break;
348 case WID_GO_SCREENSHOT_DROPDOWN: SetDParam(0, SPECSTR_SCREENSHOT_START + _cur_screenshot_format); break;
349 case WID_GO_BASE_GRF_DROPDOWN: SetDParamStr(0, BaseGraphics::GetUsedSet()->get_name()); break;
350 case WID_GO_BASE_GRF_STATUS: SetDParam(0, BaseGraphics::GetUsedSet()->GetNumInvalid()); break;
351 case WID_GO_BASE_SFX_DROPDOWN: SetDParamStr(0, BaseSounds::GetUsedSet()->get_name()); break;
352 case WID_GO_BASE_MUSIC_DROPDOWN: SetDParamStr(0, BaseMusic::GetUsedSet()->get_name()); break;
353 case WID_GO_BASE_MUSIC_STATUS: SetDParam(0, BaseMusic::GetUsedSet()->GetNumInvalid()); break;
357 void DrawWidget (BlitArea *dpi, const Rect &r, int widget) const OVERRIDE
359 const BaseSetDesc *set;
361 switch (widget) {
362 case WID_GO_BASE_GRF_DESCRIPTION:
363 set = BaseGraphics::GetUsedSet();
364 break;
366 case WID_GO_BASE_SFX_DESCRIPTION:
367 set = BaseSounds::GetUsedSet();
368 break;
370 case WID_GO_BASE_MUSIC_DESCRIPTION:
371 set = BaseMusic::GetUsedSet();
372 break;
374 default: return;
377 SetDParamStr (0, set->get_desc (GetCurrentLanguageIsoCode()));
378 DrawStringMultiLine (dpi, r.left, r.right, r.top, UINT16_MAX, STR_BLACK_RAW_STRING);
381 template <class T>
382 static void UpdateSetDescWidgetSize (Dimension *size)
384 /* Find the biggest description for the default size. */
385 for (int i = 0; i < T::GetNumSets(); i++) {
386 SetDParamStr (0, T::GetSet(i)->get_desc (GetCurrentLanguageIsoCode()));
387 size->height = max (size->height, GetStringHeight (STR_BLACK_RAW_STRING, size->width));
391 template <class T, StringID str>
392 static void UpdateSetStatusWidgetSize (Dimension *size)
394 /* Find the biggest description for the default size. */
395 for (int i = 0; i < T::GetNumSets(); i++) {
396 uint invalid_files = T::GetSet(i)->GetNumInvalid();
397 if (invalid_files == 0) continue;
399 SetDParam (0, invalid_files);
400 *size = maxdim (*size, GetStringBoundingBox (str));
404 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
406 switch (widget) {
407 case WID_GO_BASE_GRF_DESCRIPTION:
408 UpdateSetDescWidgetSize<BaseGraphics> (size);
409 break;
411 case WID_GO_BASE_GRF_STATUS:
412 UpdateSetStatusWidgetSize <BaseGraphics, STR_GAME_OPTIONS_BASE_GRF_STATUS> (size);
413 break;
415 case WID_GO_BASE_SFX_DESCRIPTION:
416 UpdateSetDescWidgetSize<BaseSounds> (size);
417 break;
419 case WID_GO_BASE_MUSIC_DESCRIPTION:
420 UpdateSetDescWidgetSize<BaseMusic> (size);
421 break;
423 case WID_GO_BASE_MUSIC_STATUS:
424 UpdateSetStatusWidgetSize <BaseMusic, STR_GAME_OPTIONS_BASE_MUSIC_STATUS> (size);
425 break;
427 default: {
428 int selected;
429 DropDownList *list = this->BuildDropDownList(widget, &selected);
430 if (list != NULL) {
431 /* Find the biggest item for the default size. */
432 for (const DropDownListItem * const *it = list->Begin(); it != list->End(); it++) {
433 Dimension string_dim;
434 int width = (*it)->Width();
435 string_dim.width = width + padding.width;
436 string_dim.height = (*it)->Height(width) + padding.height;
437 *size = maxdim(*size, string_dim);
439 delete list;
445 virtual void OnClick(Point pt, int widget, int click_count)
447 if (widget >= WID_GO_BASE_GRF_TEXTFILE && widget < WID_GO_BASE_GRF_TEXTFILE + TFT_END) {
448 if (BaseGraphics::GetUsedSet() == NULL) return;
450 ShowBaseSetTextfileWindow((TextfileType)(widget - WID_GO_BASE_GRF_TEXTFILE), BaseGraphics::GetUsedSet(), STR_CONTENT_TYPE_BASE_GRAPHICS);
451 return;
453 if (widget >= WID_GO_BASE_SFX_TEXTFILE && widget < WID_GO_BASE_SFX_TEXTFILE + TFT_END) {
454 if (BaseSounds::GetUsedSet() == NULL) return;
456 ShowBaseSetTextfileWindow((TextfileType)(widget - WID_GO_BASE_SFX_TEXTFILE), BaseSounds::GetUsedSet(), STR_CONTENT_TYPE_BASE_SOUNDS);
457 return;
459 if (widget >= WID_GO_BASE_MUSIC_TEXTFILE && widget < WID_GO_BASE_MUSIC_TEXTFILE + TFT_END) {
460 if (BaseMusic::GetUsedSet() == NULL) return;
462 ShowBaseSetTextfileWindow((TextfileType)(widget - WID_GO_BASE_MUSIC_TEXTFILE), BaseMusic::GetUsedSet(), STR_CONTENT_TYPE_BASE_MUSIC);
463 return;
465 switch (widget) {
466 case WID_GO_FULLSCREEN_BUTTON: // Click fullscreen on/off
467 /* try to toggle full-screen on/off */
468 if (!ToggleFullScreen(!_fullscreen)) {
469 ShowErrorMessage(STR_ERROR_FULLSCREEN_FAILED, INVALID_STRING_ID, WL_ERROR);
471 this->SetWidgetLoweredState(WID_GO_FULLSCREEN_BUTTON, _fullscreen);
472 this->SetDirty();
473 break;
475 default: {
476 int selected;
477 DropDownList *list = this->BuildDropDownList(widget, &selected);
478 if (list != NULL) {
479 ShowDropDownList(this, list, selected, widget);
480 } else {
481 if (widget == WID_GO_RESOLUTION_DROPDOWN) ShowErrorMessage(STR_ERROR_RESOLUTION_LIST_FAILED, INVALID_STRING_ID, WL_ERROR);
483 break;
489 * Set the base media set.
490 * @param index the index of the media set
491 * @tparam T class of media set
493 template <class T>
494 void SetMediaSet(int index)
496 if (_game_mode == GM_MENU) {
497 const char *name = T::GetSet(index)->get_name();
499 free(T::ini_set);
500 T::ini_set = xstrdup(name);
502 T::SetSet(name);
503 this->reload = true;
504 this->InvalidateData();
508 virtual void OnDropdownSelect(int widget, int index)
510 switch (widget) {
511 case WID_GO_CURRENCY_DROPDOWN: // Currency
512 if (index == CURRENCY_CUSTOM) ShowCustCurrency();
513 this->opt->locale.currency = index;
514 ReInitAllWindows();
515 break;
517 case WID_GO_ROADSIDE_DROPDOWN: // Road side
518 if (this->opt->vehicle.road_side != index) { // only change if setting changed
519 uint i;
520 if (GetSettingFromName("vehicle.road_side", &i) == NULL) NOT_REACHED();
521 SetSettingValue(i, index);
522 MarkWholeScreenDirty();
524 break;
526 case WID_GO_TOWNNAME_DROPDOWN: // Town names
527 if (_game_mode == GM_MENU || Town::GetNumItems() == 0) {
528 this->opt->game_creation.town_name = index;
529 SetWindowDirty(WC_GAME_OPTIONS, WN_GAME_OPTIONS_GAME_OPTIONS);
531 break;
533 case WID_GO_AUTOSAVE_DROPDOWN: // Autosave options
534 _settings_client.gui.autosave = index;
535 this->SetDirty();
536 break;
538 case WID_GO_LANG_DROPDOWN: // Change interface language
539 ReadLanguagePack(&_languages[index]);
540 DeleteWindowByClass(WC_QUERY_STRING);
541 CheckForMissingGlyphs();
542 UpdateAllVirtCoords();
543 ReInitAllWindows();
544 break;
546 case WID_GO_RESOLUTION_DROPDOWN: // Change resolution
547 if (index < _num_resolutions && ChangeResInGame(_resolutions[index].width, _resolutions[index].height)) {
548 this->SetDirty();
550 break;
552 case WID_GO_GUI_ZOOM_DROPDOWN:
553 GfxClearSpriteCache();
554 _gui_zoom = (ZoomLevel)(ZOOM_LVL_OUT_4X - index);
555 UpdateCursorSize();
556 LoadStringWidthTable();
557 break;
559 case WID_GO_SCREENSHOT_DROPDOWN: // Change screenshot format
560 SetScreenshotFormat(index);
561 this->SetDirty();
562 break;
564 case WID_GO_BASE_GRF_DROPDOWN:
565 this->SetMediaSet<BaseGraphics>(index);
566 break;
568 case WID_GO_BASE_SFX_DROPDOWN:
569 this->SetMediaSet<BaseSounds>(index);
570 break;
572 case WID_GO_BASE_MUSIC_DROPDOWN:
573 this->SetMediaSet<BaseMusic>(index);
574 break;
578 template <class T, class S, GameOptionsWidgets W>
579 void SetBaseSetWidgetsDisabledState (void)
581 const S *set = T::GetUsedSet();
582 for (TextfileType tft = TFT_BEGIN; tft < TFT_END; tft++) {
583 this->SetWidgetDisabledState (W + tft,
584 set == NULL || !set->GetTextfile(tft).valid());
589 * Some data on this window has become invalid.
590 * @param data Information about the changed data. @see GameOptionsInvalidationData
591 * @param gui_scope Whether the call is done from GUI scope. You may not do everything when not in GUI scope. See #InvalidateWindowData() for details.
593 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
595 if (!gui_scope) return;
596 this->SetWidgetLoweredState(WID_GO_FULLSCREEN_BUTTON, _fullscreen);
598 bool missing_files = BaseGraphics::GetUsedSet()->GetNumMissing() == 0;
599 this->GetWidget<NWidgetCore>(WID_GO_BASE_GRF_STATUS)->SetDataTip(missing_files ? STR_EMPTY : STR_GAME_OPTIONS_BASE_GRF_STATUS, STR_NULL);
601 this->SetBaseSetWidgetsDisabledState <BaseGraphics, GraphicsSet, WID_GO_BASE_GRF_TEXTFILE> ();
602 this->SetBaseSetWidgetsDisabledState <BaseSounds, SoundsSet, WID_GO_BASE_SFX_TEXTFILE> ();
603 this->SetBaseSetWidgetsDisabledState <BaseMusic, MusicSet, WID_GO_BASE_MUSIC_TEXTFILE> ();
605 missing_files = BaseMusic::GetUsedSet()->GetNumInvalid() == 0;
606 this->GetWidget<NWidgetCore>(WID_GO_BASE_MUSIC_STATUS)->SetDataTip(missing_files ? STR_EMPTY : STR_GAME_OPTIONS_BASE_MUSIC_STATUS, STR_NULL);
610 static const NWidgetPart _nested_game_options_widgets[] = {
611 NWidget(NWID_HORIZONTAL),
612 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
613 NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_GAME_OPTIONS_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
614 EndContainer(),
615 NWidget(WWT_PANEL, COLOUR_GREY, WID_GO_BACKGROUND), SetPIP(6, 6, 10),
616 NWidget(NWID_HORIZONTAL), SetPIP(10, 10, 10),
617 NWidget(NWID_VERTICAL), SetPIP(0, 6, 0),
618 NWidget(WWT_FRAME, COLOUR_GREY), SetDataTip(STR_GAME_OPTIONS_CURRENCY_UNITS_FRAME, STR_NULL),
619 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),
620 EndContainer(),
621 NWidget(WWT_FRAME, COLOUR_GREY), SetDataTip(STR_GAME_OPTIONS_ROAD_VEHICLES_FRAME, STR_NULL),
622 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),
623 EndContainer(),
624 NWidget(WWT_FRAME, COLOUR_GREY), SetDataTip(STR_GAME_OPTIONS_AUTOSAVE_FRAME, STR_NULL),
625 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),
626 EndContainer(),
627 NWidget(WWT_FRAME, COLOUR_GREY), SetDataTip(STR_GAME_OPTIONS_RESOLUTION, STR_NULL),
628 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),
629 NWidget(NWID_HORIZONTAL),
630 NWidget(WWT_TEXT, COLOUR_GREY), SetMinimalSize(0, 12), SetFill(1, 0), SetDataTip(STR_GAME_OPTIONS_FULLSCREEN, STR_NULL),
631 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_GO_FULLSCREEN_BUTTON), SetMinimalSize(21, 9), SetDataTip(STR_EMPTY, STR_GAME_OPTIONS_FULLSCREEN_TOOLTIP),
632 EndContainer(),
633 EndContainer(),
634 NWidget(WWT_FRAME, COLOUR_GREY), SetDataTip(STR_GAME_OPTIONS_GUI_ZOOM_FRAME, STR_NULL),
635 NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_GO_GUI_ZOOM_DROPDOWN), SetMinimalSize(150, 12), SetDataTip(STR_BLACK_STRING, STR_GAME_OPTIONS_GUI_ZOOM_DROPDOWN_TOOLTIP), SetFill(1, 0),
636 EndContainer(),
637 EndContainer(),
639 NWidget(NWID_VERTICAL), SetPIP(0, 6, 0),
640 NWidget(WWT_FRAME, COLOUR_GREY), SetDataTip(STR_GAME_OPTIONS_TOWN_NAMES_FRAME, STR_NULL),
641 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),
642 EndContainer(),
643 NWidget(WWT_FRAME, COLOUR_GREY), SetDataTip(STR_GAME_OPTIONS_LANGUAGE, STR_NULL),
644 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),
645 EndContainer(),
646 NWidget(WWT_FRAME, COLOUR_GREY), SetDataTip(STR_GAME_OPTIONS_SCREENSHOT_FORMAT, STR_NULL),
647 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),
648 EndContainer(),
649 NWidget(NWID_SPACER), SetMinimalSize(0, 0), SetFill(0, 1),
650 EndContainer(),
651 EndContainer(),
653 NWidget(WWT_FRAME, COLOUR_GREY), SetDataTip(STR_GAME_OPTIONS_BASE_GRF, STR_NULL), SetPadding(0, 10, 0, 10),
654 NWidget(NWID_HORIZONTAL), SetPIP(0, 30, 0),
655 NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_GO_BASE_GRF_DROPDOWN), SetMinimalSize(150, 12), SetDataTip(STR_BLACK_RAW_STRING, STR_GAME_OPTIONS_BASE_GRF_TOOLTIP),
656 NWidget(WWT_TEXT, COLOUR_GREY, WID_GO_BASE_GRF_STATUS), SetMinimalSize(150, 12), SetDataTip(STR_EMPTY, STR_NULL), SetFill(1, 0),
657 EndContainer(),
658 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),
659 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(7, 0, 7),
660 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),
661 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),
662 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),
663 EndContainer(),
664 EndContainer(),
666 NWidget(WWT_FRAME, COLOUR_GREY), SetDataTip(STR_GAME_OPTIONS_BASE_SFX, STR_NULL), SetPadding(0, 10, 0, 10),
667 NWidget(NWID_HORIZONTAL), SetPIP(0, 30, 0),
668 NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_GO_BASE_SFX_DROPDOWN), SetMinimalSize(150, 12), SetDataTip(STR_BLACK_RAW_STRING, STR_GAME_OPTIONS_BASE_SFX_TOOLTIP),
669 NWidget(NWID_SPACER), SetFill(1, 0),
670 EndContainer(),
671 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),
672 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(7, 0, 7),
673 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),
674 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),
675 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),
676 EndContainer(),
677 EndContainer(),
679 NWidget(WWT_FRAME, COLOUR_GREY), SetDataTip(STR_GAME_OPTIONS_BASE_MUSIC, STR_NULL), SetPadding(0, 10, 0, 10),
680 NWidget(NWID_HORIZONTAL), SetPIP(0, 30, 0),
681 NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_GO_BASE_MUSIC_DROPDOWN), SetMinimalSize(150, 12), SetDataTip(STR_BLACK_RAW_STRING, STR_GAME_OPTIONS_BASE_MUSIC_TOOLTIP),
682 NWidget(WWT_TEXT, COLOUR_GREY, WID_GO_BASE_MUSIC_STATUS), SetMinimalSize(150, 12), SetDataTip(STR_EMPTY, STR_NULL), SetFill(1, 0),
683 EndContainer(),
684 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),
685 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(7, 0, 7),
686 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),
687 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),
688 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),
689 EndContainer(),
690 EndContainer(),
691 EndContainer(),
694 static WindowDesc::Prefs _game_options_prefs ("settings_game");
696 static const WindowDesc _game_options_desc(
697 WDP_CENTER, 0, 0,
698 WC_GAME_OPTIONS, WC_NONE,
700 _nested_game_options_widgets, lengthof(_nested_game_options_widgets),
701 &_game_options_prefs
704 /** Open the game options window. */
705 void ShowGameOptions()
707 DeleteWindowByClass(WC_GAME_OPTIONS);
708 new GameOptionsWindow(&_game_options_desc);
711 static int SETTING_HEIGHT = 11; ///< Height of a single setting in the tree view in pixels
712 static const int LEVEL_WIDTH = 15; ///< Indenting width of a sub-page in pixels
715 * Flags for #SettingEntry
716 * @note The #SEF_BUTTONS_MASK matches expectations of the formal parameter 'state' of #DrawArrowButtons
718 enum SettingEntryFlags {
719 SEF_LEFT_DEPRESSED = 0x01, ///< Of a numeric setting entry, the left button is depressed
720 SEF_RIGHT_DEPRESSED = 0x02, ///< Of a numeric setting entry, the right button is depressed
721 SEF_BUTTONS_MASK = (SEF_LEFT_DEPRESSED | SEF_RIGHT_DEPRESSED), ///< Bit-mask for button flags
723 SEF_LAST_FIELD = 0x04, ///< This entry is the last one in a (sub-)page
724 SEF_FILTERED = 0x08, ///< Entry is hidden by the string filter
726 /* Entry kind */
727 SEF_SETTING_KIND = 0x10, ///< Entry kind: Entry is a setting
728 SEF_SUBTREE_KIND = 0x20, ///< Entry kind: Entry is a sub-tree
729 SEF_KIND_MASK = (SEF_SETTING_KIND | SEF_SUBTREE_KIND), ///< Bit-mask for fetching entry kind
732 struct SettingsPage; // Forward declaration
734 /** Data fields for a sub-page (#SEF_SUBTREE_KIND kind)*/
735 struct SettingEntrySubtree {
736 SettingsPage *page; ///< Pointer to the sub-page
737 bool folded; ///< Sub-page is folded (not visible except for its title)
738 StringID title; ///< Title of the sub-page
741 /** Data fields for a single setting (#SEF_SETTING_KIND kind) */
742 struct SettingEntrySetting {
743 const char *name; ///< Name of the setting
744 const SettingDesc *setting; ///< Setting description of the setting
745 uint index; ///< Index of the setting in the settings table
748 /** How the list of advanced settings is filtered. */
749 enum RestrictionMode {
750 RM_BASIC, ///< Display settings associated to the "basic" list.
751 RM_ADVANCED, ///< Display settings associated to the "advanced" list.
752 RM_ALL, ///< List all settings regardless of the default/newgame/... values.
753 RM_CHANGED_AGAINST_DEFAULT, ///< Show only settings which are different compared to default values.
754 RM_CHANGED_AGAINST_NEW, ///< Show only settings which are different compared to the user's new game setting values.
755 RM_END, ///< End for iteration.
757 DECLARE_POSTFIX_INCREMENT(RestrictionMode)
759 /** Filter for settings list. */
760 struct SettingFilter {
761 StringFilter string; ///< Filter string.
762 RestrictionMode min_cat; ///< Minimum category needed to display all filtered strings (#RM_BASIC, #RM_ADVANCED, or #RM_ALL).
763 bool type_hides; ///< Whether the type hides filtered strings.
764 RestrictionMode mode; ///< Filter based on category.
765 SettingType type; ///< Filter based on type.
768 /** Data structure describing a single setting in a tab */
769 struct SettingEntry {
770 byte flags; ///< Flags of the setting entry. @see SettingEntryFlags
771 byte level; ///< Nesting level of this setting entry
772 union {
773 SettingEntrySetting entry; ///< Data fields if entry is a setting
774 SettingEntrySubtree sub; ///< Data fields if entry is a sub-page
775 } d; ///< Data fields for each kind
777 SettingEntry(const char *nm);
778 SettingEntry(SettingsPage *sub, StringID title);
780 void Init(byte level);
781 void FoldAll();
782 void UnFoldAll();
783 void SetButtons(byte new_val);
786 * Set whether this is the last visible entry of the parent node.
787 * @param last_field Value to set
789 void SetLastField(bool last_field) { if (last_field) SETBITS(this->flags, SEF_LAST_FIELD); else CLRBITS(this->flags, SEF_LAST_FIELD); }
791 uint Length() const;
792 void GetFoldingState(bool &all_folded, bool &all_unfolded) const;
793 bool IsVisible(const SettingEntry *item) const;
794 SettingEntry *FindEntry(uint row, uint *cur_row);
795 uint GetMaxHelpHeight(int maxw);
797 bool IsFiltered() const;
798 bool UpdateFilterState(SettingFilter &filter, bool force_visible);
800 uint Draw (GameSettings *settings_ptr, BlitArea *dpi, int base_x,
801 int base_y, int max_x, uint first_row, uint max_row,
802 uint cur_row, uint parent_last, SettingEntry *selected);
805 * Get the help text of a single setting.
806 * @return The requested help text.
808 inline StringID GetHelpText()
810 assert((this->flags & SEF_KIND_MASK) == SEF_SETTING_KIND);
811 return this->d.entry.setting->desc.str_help;
814 void SetValueDParams(uint first_param, int32 value);
816 private:
817 void DrawSetting (GameSettings *settings_ptr, BlitArea *dpi,
818 int x, int y, int max_x, int state, bool highlight);
820 bool IsVisibleByRestrictionMode(RestrictionMode mode) const;
823 /** Data structure describing one page of settings in the settings window. */
824 struct SettingsPage {
825 SettingEntry *entries; ///< Array of setting entries of the page.
826 byte num; ///< Number of entries on the page (statically filled).
828 void Init(byte level = 0);
829 void FoldAll();
830 void UnFoldAll();
832 uint Length() const;
833 void GetFoldingState(bool &all_folded, bool &all_unfolded) const;
834 bool IsVisible(const SettingEntry *item) const;
835 SettingEntry *FindEntry(uint row, uint *cur_row) const;
836 uint GetMaxHelpHeight(int maxw);
838 bool UpdateFilterState(SettingFilter &filter, bool force_visible);
840 uint Draw (GameSettings *settings_ptr, BlitArea *dpi, int base_x,
841 int base_y, int max_x, uint first_row, uint max_row,
842 SettingEntry *selected, uint cur_row = 0, uint parent_last = 0) const;
846 /* == SettingEntry methods == */
849 * Constructor for a single setting in the 'advanced settings' window
850 * @param nm Name of the setting in the setting table
852 SettingEntry::SettingEntry(const char *nm)
854 this->flags = SEF_SETTING_KIND;
855 this->level = 0;
856 this->d.entry.name = nm;
857 this->d.entry.setting = NULL;
858 this->d.entry.index = 0;
862 * Constructor for a sub-page in the 'advanced settings' window
863 * @param sub Sub-page
864 * @param title Title of the sub-page
866 SettingEntry::SettingEntry(SettingsPage *sub, StringID title)
868 this->flags = SEF_SUBTREE_KIND;
869 this->level = 0;
870 this->d.sub.page = sub;
871 this->d.sub.folded = true;
872 this->d.sub.title = title;
876 * Initialization of a setting entry
877 * @param level Page nesting level of this entry
879 void SettingEntry::Init(byte level)
881 this->level = level;
883 switch (this->flags & SEF_KIND_MASK) {
884 case SEF_SETTING_KIND:
885 this->d.entry.setting = GetSettingFromName(this->d.entry.name, &this->d.entry.index);
886 assert(this->d.entry.setting != NULL);
887 break;
888 case SEF_SUBTREE_KIND:
889 this->d.sub.page->Init(level + 1);
890 break;
891 default: NOT_REACHED();
895 /** Recursively close all (filtered) folds of sub-pages */
896 void SettingEntry::FoldAll()
898 if (this->IsFiltered()) return;
899 switch (this->flags & SEF_KIND_MASK) {
900 case SEF_SETTING_KIND:
901 break;
903 case SEF_SUBTREE_KIND:
904 this->d.sub.folded = true;
905 this->d.sub.page->FoldAll();
906 break;
908 default: NOT_REACHED();
912 /** Recursively open all (filtered) folds of sub-pages */
913 void SettingEntry::UnFoldAll()
915 if (this->IsFiltered()) return;
916 switch (this->flags & SEF_KIND_MASK) {
917 case SEF_SETTING_KIND:
918 break;
920 case SEF_SUBTREE_KIND:
921 this->d.sub.folded = false;
922 this->d.sub.page->UnFoldAll();
923 break;
925 default: NOT_REACHED();
930 * Recursively accumulate the folding state of the (filtered) tree.
931 * @param[in,out] all_folded Set to false, if one entry is not folded.
932 * @param[in,out] all_unfolded Set to false, if one entry is folded.
934 void SettingEntry::GetFoldingState(bool &all_folded, bool &all_unfolded) const
936 if (this->IsFiltered()) return;
937 switch (this->flags & SEF_KIND_MASK) {
938 case SEF_SETTING_KIND:
939 break;
941 case SEF_SUBTREE_KIND:
942 if (this->d.sub.folded) {
943 all_unfolded = false;
944 } else {
945 all_folded = false;
947 this->d.sub.page->GetFoldingState(all_folded, all_unfolded);
948 break;
950 default: NOT_REACHED();
955 * Check whether an entry is visible and not folded or filtered away.
956 * Note: This does not consider the scrolling range; it might still require scrolling to make the setting really visible.
957 * @param item Entry to search for.
958 * @return true if entry is visible.
960 bool SettingEntry::IsVisible(const SettingEntry *item) const
962 if (this->IsFiltered()) return false;
963 if (this == item) return true;
965 switch (this->flags & SEF_KIND_MASK) {
966 case SEF_SETTING_KIND:
967 return false;
969 case SEF_SUBTREE_KIND:
970 return !this->d.sub.folded && this->d.sub.page->IsVisible(item);
972 default: NOT_REACHED();
977 * Set the button-depressed flags (#SEF_LEFT_DEPRESSED and #SEF_RIGHT_DEPRESSED) to a specified value
978 * @param new_val New value for the button flags
979 * @see SettingEntryFlags
981 void SettingEntry::SetButtons(byte new_val)
983 assert((new_val & ~SEF_BUTTONS_MASK) == 0); // Should not touch any flags outside the buttons
984 this->flags = (this->flags & ~SEF_BUTTONS_MASK) | new_val;
987 /** Return numbers of rows needed to display the (filtered) entry */
988 uint SettingEntry::Length() const
990 if (this->IsFiltered()) return 0;
991 switch (this->flags & SEF_KIND_MASK) {
992 case SEF_SETTING_KIND:
993 return 1;
994 case SEF_SUBTREE_KIND:
995 if (this->d.sub.folded) return 1; // Only displaying the title
997 return 1 + this->d.sub.page->Length(); // 1 extra row for the title
998 default: NOT_REACHED();
1003 * Find setting entry at row \a row_num
1004 * @param row_num Index of entry to return
1005 * @param cur_row Current row number
1006 * @return The requested setting entry or \c NULL if it not found (folded or filtered)
1008 SettingEntry *SettingEntry::FindEntry(uint row_num, uint *cur_row)
1010 if (this->IsFiltered()) return NULL;
1011 if (row_num == *cur_row) return this;
1013 switch (this->flags & SEF_KIND_MASK) {
1014 case SEF_SETTING_KIND:
1015 (*cur_row)++;
1016 break;
1017 case SEF_SUBTREE_KIND:
1018 (*cur_row)++; // add one for row containing the title
1019 if (this->d.sub.folded) {
1020 break;
1023 /* sub-page is visible => search it too */
1024 return this->d.sub.page->FindEntry(row_num, cur_row);
1025 default: NOT_REACHED();
1027 return NULL;
1031 * Get the biggest height of the help text(s), if the width is at least \a maxw. Help text gets wrapped if needed.
1032 * @param maxw Maximal width of a line help text.
1033 * @return Biggest height needed to display any help text of this node (and its descendants).
1035 uint SettingEntry::GetMaxHelpHeight(int maxw)
1037 switch (this->flags & SEF_KIND_MASK) {
1038 case SEF_SETTING_KIND: return GetStringHeight(this->GetHelpText(), maxw);
1039 case SEF_SUBTREE_KIND: return this->d.sub.page->GetMaxHelpHeight(maxw);
1040 default: NOT_REACHED();
1045 * Check whether an entry is hidden due to filters
1046 * @return true if hidden.
1048 bool SettingEntry::IsFiltered() const
1050 return (this->flags & SEF_FILTERED) != 0;
1054 * Checks whether an entry shall be made visible based on the restriction mode.
1055 * @param mode The current status of the restriction drop down box.
1056 * @return true if the entry shall be visible.
1058 bool SettingEntry::IsVisibleByRestrictionMode(RestrictionMode mode) const
1060 /* There shall not be any restriction, i.e. all settings shall be visible. */
1061 if (mode == RM_ALL) return true;
1063 GameSettings *settings_ptr = &GetGameSettings();
1064 assert((this->flags & SEF_KIND_MASK) == SEF_SETTING_KIND);
1065 const SettingDesc *sd = this->d.entry.setting;
1067 if (mode == RM_BASIC) return (this->d.entry.setting->desc.cat & SC_BASIC_LIST) != 0;
1068 if (mode == RM_ADVANCED) return (this->d.entry.setting->desc.cat & SC_ADVANCED_LIST) != 0;
1070 /* Read the current value. */
1071 int64 current_value = ReadVariable (settings_ptr, sd);
1073 int64 filter_value;
1075 if (mode == RM_CHANGED_AGAINST_DEFAULT) {
1076 /* This entry shall only be visible, if the value deviates from its default value. */
1078 /* Read the default value. */
1079 filter_value = ReadValue(&sd->desc.def, sd->save.conv);
1080 } else {
1081 assert(mode == RM_CHANGED_AGAINST_NEW);
1082 /* This entry shall only be visible, if the value deviates from
1083 * its value is used when starting a new game. */
1085 /* Make sure we're not comparing the new game settings against itself. */
1086 assert(settings_ptr != &_settings_newgame);
1088 /* Read the new game's value. */
1089 filter_value = ReadVariable (&_settings_newgame, sd);
1092 return current_value != filter_value;
1096 * Update the filter state.
1097 * @param filter Filter
1098 * @param force_visible Whether to force all items visible, no matter what (due to filter text; not affected by restriction drop down box).
1099 * @return true if item remains visible
1101 bool SettingEntry::UpdateFilterState(SettingFilter &filter, bool force_visible)
1103 CLRBITS(this->flags, SEF_FILTERED);
1105 bool visible = true;
1106 switch (this->flags & SEF_KIND_MASK) {
1107 case SEF_SETTING_KIND: {
1108 const SettingDesc *sd = this->d.entry.setting;
1109 if (!force_visible && !filter.string.IsEmpty()) {
1110 /* Process the search text filter for this item. */
1111 filter.string.ResetState();
1113 const SettingDescBase *sdb = &sd->desc;
1115 SetDParam(0, STR_EMPTY);
1116 filter.string.AddLine(sdb->str);
1117 filter.string.AddLine(this->GetHelpText());
1119 visible = filter.string.GetState();
1121 if (visible) {
1122 if (filter.type != ST_ALL && sd->GetType() != filter.type) {
1123 filter.type_hides = true;
1124 visible = false;
1126 if (!this->IsVisibleByRestrictionMode(filter.mode)) {
1127 while (filter.min_cat < RM_ALL && (filter.min_cat == filter.mode || !this->IsVisibleByRestrictionMode(filter.min_cat))) filter.min_cat++;
1128 visible = false;
1131 break;
1133 case SEF_SUBTREE_KIND: {
1134 if (!force_visible && !filter.string.IsEmpty()) {
1135 filter.string.ResetState();
1136 filter.string.AddLine(this->d.sub.title);
1137 force_visible = filter.string.GetState();
1139 visible = this->d.sub.page->UpdateFilterState(filter, force_visible);
1140 break;
1142 default: NOT_REACHED();
1145 if (!visible) SETBITS(this->flags, SEF_FILTERED);
1146 return visible;
1152 * Draw a row in the settings panel.
1154 * See SettingsPage::Draw() for an explanation about how drawing is performed.
1156 * The \a parent_last parameter ensures that the vertical lines at the left are
1157 * only drawn when another entry follows, that it prevents output like
1158 * \verbatim
1159 * |-- setting
1160 * |-- (-) - Title
1161 * | |-- setting
1162 * | |-- setting
1163 * \endverbatim
1164 * The left-most vertical line is not wanted. It is prevented by setting the
1165 * appropriate bit in the \a parent_last parameter.
1167 * @param settings_ptr Pointer to current values of all settings
1168 * @param dpi Area to draw on
1169 * @param left Left-most position in window/panel to start drawing \a first_row
1170 * @param right Right-most x position to draw strings at.
1171 * @param base_y Upper-most position in window/panel to start drawing \a first_row
1172 * @param first_row First row number to draw
1173 * @param max_row Row-number to stop drawing (the row-number of the row below the last row to draw)
1174 * @param cur_row Current row number (internal variable)
1175 * @param parent_last Last-field booleans of parent page level (page level \e i sets bit \e i to 1 if it is its last field)
1176 * @param selected Selected entry by the user.
1177 * @return Row number of the next row to draw
1179 uint SettingEntry::Draw (GameSettings *settings_ptr, BlitArea *dpi,
1180 int left, int right, int base_y, uint first_row, uint max_row,
1181 uint cur_row, uint parent_last, SettingEntry *selected)
1183 if (this->IsFiltered()) return cur_row;
1184 if (cur_row >= max_row) return cur_row;
1186 bool rtl = _current_text_dir == TD_RTL;
1187 int offset = rtl ? -4 : 4;
1188 int level_width = rtl ? -LEVEL_WIDTH : LEVEL_WIDTH;
1190 int x = rtl ? right : left;
1191 int y = base_y;
1192 if (cur_row >= first_row) {
1193 int colour = _colour_gradient[COLOUR_ORANGE][4];
1194 y = base_y + (cur_row - first_row) * SETTING_HEIGHT; // Compute correct y start position
1196 /* Draw vertical for parent nesting levels */
1197 for (uint lvl = 0; lvl < this->level; lvl++) {
1198 if (!HasBit(parent_last, lvl)) GfxDrawLine (dpi, x + offset, y, x + offset, y + SETTING_HEIGHT - 1, colour);
1199 x += level_width;
1201 /* draw own |- prefix */
1202 int halfway_y = y + SETTING_HEIGHT / 2;
1203 int bottom_y = (flags & SEF_LAST_FIELD) ? halfway_y : y + SETTING_HEIGHT - 1;
1204 GfxDrawLine (dpi, x + offset, y, x + offset, bottom_y, colour);
1205 /* Small horizontal line from the last vertical line */
1206 GfxDrawLine (dpi, x + offset, halfway_y, x + level_width - offset, halfway_y, colour);
1207 x += level_width;
1210 switch (this->flags & SEF_KIND_MASK) {
1211 case SEF_SETTING_KIND:
1212 if (cur_row >= first_row) {
1213 this->DrawSetting (settings_ptr, dpi, rtl ? left : x, rtl ? x : right, y, this->flags & SEF_BUTTONS_MASK,
1214 this == selected);
1216 cur_row++;
1217 break;
1218 case SEF_SUBTREE_KIND:
1219 if (cur_row >= first_row) {
1220 DrawSprite (dpi, (this->d.sub.folded ? SPR_CIRCLE_FOLDED : SPR_CIRCLE_UNFOLDED), PAL_NONE, rtl ? x - _circle_size.width : x, y + (SETTING_HEIGHT - _circle_size.height) / 2);
1221 DrawString (dpi, rtl ? left : x + _circle_size.width + 2, rtl ? x - _circle_size.width - 2 : right, y + (SETTING_HEIGHT - FONT_HEIGHT_NORMAL) / 2, this->d.sub.title);
1223 cur_row++;
1224 if (!this->d.sub.folded) {
1225 if (this->flags & SEF_LAST_FIELD) {
1226 assert(this->level < sizeof(parent_last));
1227 SetBit(parent_last, this->level); // Add own last-field state
1230 cur_row = this->d.sub.page->Draw (settings_ptr, dpi, left, right, base_y, first_row, max_row, selected, cur_row, parent_last);
1232 break;
1233 default: NOT_REACHED();
1235 return cur_row;
1239 * Set the DParams for drawing the value of a setting.
1240 * @param first_param First DParam to use
1241 * @param value Setting value to set params for.
1243 void SettingEntry::SetValueDParams(uint first_param, int32 value)
1245 assert((this->flags & SEF_KIND_MASK) == SEF_SETTING_KIND);
1246 const SettingDescBase *sdb = &this->d.entry.setting->desc;
1247 if (sdb->cmd == SDT_BOOLX) {
1248 SetDParam(first_param++, value != 0 ? STR_CONFIG_SETTING_ON : STR_CONFIG_SETTING_OFF);
1249 } else {
1250 if ((sdb->flags & SGF_MULTISTRING) != 0) {
1251 SetDParam(first_param++, sdb->str_val - sdb->min + value);
1252 } else if ((sdb->flags & SGF_DISPLAY_ABS) != 0) {
1253 SetDParam(first_param++, sdb->str_val + ((value >= 0) ? 1 : 0));
1254 value = abs(value);
1255 } else {
1256 SetDParam(first_param++, sdb->str_val + ((value == 0 && (sdb->flags & SGF_0ISDISABLED) != 0) ? 1 : 0));
1258 SetDParam(first_param++, value);
1263 * Private function to draw setting value (button + text + current value)
1264 * @param settings_ptr Pointer to current values of all settings
1265 * @param dpi Area to draw on
1266 * @param left Left-most position in window/panel to start drawing
1267 * @param right Right-most position in window/panel to draw
1268 * @param y Upper-most position in window/panel to start drawing
1269 * @param state State of the left + right arrow buttons to draw for the setting
1270 * @param highlight Highlight entry.
1272 void SettingEntry::DrawSetting (GameSettings *settings_ptr, BlitArea *dpi,
1273 int left, int right, int y, int state, bool highlight)
1275 const SettingDesc *sd = this->d.entry.setting;
1276 const SettingDescBase *sdb = &sd->desc;
1277 int32 value = ReadVariable (settings_ptr, sd);
1279 bool rtl = _current_text_dir == TD_RTL;
1280 uint buttons_left = rtl ? right + 1 - SETTING_BUTTON_WIDTH : left;
1281 uint text_left = left + (rtl ? 0 : SETTING_BUTTON_WIDTH + 5);
1282 uint text_right = right - (rtl ? SETTING_BUTTON_WIDTH + 5 : 0);
1283 uint button_y = y + (SETTING_HEIGHT - SETTING_BUTTON_HEIGHT) / 2;
1285 /* We do not allow changes of some items when we are a client in a networkgame */
1286 bool editable = sd->IsEditable();
1288 SetDParam(0, highlight ? STR_ORANGE_STRING1_WHITE : STR_ORANGE_STRING1_LTBLUE);
1289 if (sdb->cmd == SDT_BOOLX) {
1290 /* Draw checkbox for boolean-value either on/off */
1291 DrawBoolButton (dpi, buttons_left, button_y, value != 0, editable);
1292 } else if ((sdb->flags & SGF_MULTISTRING) != 0) {
1293 /* Draw [v] button for settings of an enum-type */
1294 DrawDropDownButton (dpi, buttons_left, button_y, COLOUR_YELLOW, state != 0, editable);
1295 } else {
1296 /* Draw [<][>] boxes for settings of an integer-type */
1297 DrawArrowButtons (dpi, buttons_left, button_y, COLOUR_YELLOW, state,
1298 editable && value != (sdb->flags & SGF_0ISDISABLED ? 0 : sdb->min), editable && (uint32)value != sdb->max);
1300 this->SetValueDParams(1, value);
1301 DrawString (dpi, text_left, text_right, y + (SETTING_HEIGHT - FONT_HEIGHT_NORMAL) / 2, sdb->str, highlight ? TC_WHITE : TC_LIGHT_BLUE);
1305 /* == SettingsPage methods == */
1308 * Initialization of an entire setting page
1309 * @param level Nesting level of this page (internal variable, do not provide a value for it when calling)
1311 void SettingsPage::Init(byte level)
1313 for (uint field = 0; field < this->num; field++) {
1314 this->entries[field].Init(level);
1318 /** Recursively close all folds of sub-pages */
1319 void SettingsPage::FoldAll()
1321 for (uint field = 0; field < this->num; field++) {
1322 this->entries[field].FoldAll();
1326 /** Recursively open all folds of sub-pages */
1327 void SettingsPage::UnFoldAll()
1329 for (uint field = 0; field < this->num; field++) {
1330 this->entries[field].UnFoldAll();
1335 * Recursively accumulate the folding state of the tree.
1336 * @param[in,out] all_folded Set to false, if one entry is not folded.
1337 * @param[in,out] all_unfolded Set to false, if one entry is folded.
1339 void SettingsPage::GetFoldingState(bool &all_folded, bool &all_unfolded) const
1341 for (uint field = 0; field < this->num; field++) {
1342 this->entries[field].GetFoldingState(all_folded, all_unfolded);
1347 * Update the filter state.
1348 * @param filter Filter
1349 * @param force_visible Whether to force all items visible, no matter what
1350 * @return true if item remains visible
1352 bool SettingsPage::UpdateFilterState(SettingFilter &filter, bool force_visible)
1354 bool visible = false;
1355 bool first_visible = true;
1356 for (int field = this->num - 1; field >= 0; field--) {
1357 visible |= this->entries[field].UpdateFilterState(filter, force_visible);
1358 this->entries[field].SetLastField(first_visible);
1359 if (visible && first_visible) first_visible = false;
1361 return visible;
1366 * Check whether an entry is visible and not folded or filtered away.
1367 * Note: This does not consider the scrolling range; it might still require scrolling ot make the setting really visible.
1368 * @param item Entry to search for.
1369 * @return true if entry is visible.
1371 bool SettingsPage::IsVisible(const SettingEntry *item) const
1373 for (uint field = 0; field < this->num; field++) {
1374 if (this->entries[field].IsVisible(item)) return true;
1376 return false;
1379 /** Return number of rows needed to display the whole page */
1380 uint SettingsPage::Length() const
1382 uint length = 0;
1383 for (uint field = 0; field < this->num; field++) {
1384 length += this->entries[field].Length();
1386 return length;
1390 * Find the setting entry at row number \a row_num
1391 * @param row_num Index of entry to return
1392 * @param cur_row Variable used for keeping track of the current row number. Should point to memory initialized to \c 0 when first called.
1393 * @return The requested setting entry or \c NULL if it does not exist
1395 SettingEntry *SettingsPage::FindEntry(uint row_num, uint *cur_row) const
1397 SettingEntry *pe = NULL;
1399 for (uint field = 0; field < this->num; field++) {
1400 pe = this->entries[field].FindEntry(row_num, cur_row);
1401 if (pe != NULL) {
1402 break;
1405 return pe;
1409 * Get the biggest height of the help texts, if the width is at least \a maxw. Help text gets wrapped if needed.
1410 * @param maxw Maximal width of a line help text.
1411 * @return Biggest height needed to display any help text of this (sub-)tree.
1413 uint SettingsPage::GetMaxHelpHeight(int maxw)
1415 uint biggest = 0;
1416 for (uint field = 0; field < this->num; field++) {
1417 biggest = max(biggest, this->entries[field].GetMaxHelpHeight(maxw));
1419 return biggest;
1423 * Draw a selected part of the settings page.
1425 * The scrollbar uses rows of the page, while the page data structure is a tree of #SettingsPage and #SettingEntry objects.
1426 * As a result, the drawing routing traverses the tree from top to bottom, counting rows in \a cur_row until it reaches \a first_row.
1427 * Then it enables drawing rows while traversing until \a max_row is reached, at which point drawing is terminated.
1429 * @param settings_ptr Pointer to current values of all settings
1430 * @param dpi Area to raw on
1431 * @param left Left-most position in window/panel to start drawing of each setting row
1432 * @param right Right-most position in window/panel to draw at
1433 * @param base_y Upper-most position in window/panel to start drawing of row number \a first_row
1434 * @param first_row Number of first row to draw
1435 * @param max_row Row-number to stop drawing (the row-number of the row below the last row to draw)
1436 * @param cur_row Current row number (internal variable)
1437 * @param parent_last Last-field booleans of parent page level (page level \e i sets bit \e i to 1 if it is its last field)
1438 * @param selected Selected entry by the user.
1439 * @return Row number of the next row to draw
1441 uint SettingsPage::Draw (GameSettings *settings_ptr, BlitArea *dpi,
1442 int left, int right, int base_y, uint first_row, uint max_row,
1443 SettingEntry *selected, uint cur_row, uint parent_last) const
1445 if (cur_row >= max_row) return cur_row;
1447 for (uint i = 0; i < this->num; i++) {
1448 cur_row = this->entries[i].Draw (settings_ptr, dpi, left, right, base_y, first_row, max_row, cur_row, parent_last, selected);
1449 if (cur_row >= max_row) {
1450 break;
1453 return cur_row;
1457 static SettingEntry _settings_localisation[] = {
1458 SettingEntry("locale.units_velocity"),
1459 SettingEntry("locale.units_power"),
1460 SettingEntry("locale.units_weight"),
1461 SettingEntry("locale.units_volume"),
1462 SettingEntry("locale.units_force"),
1463 SettingEntry("locale.units_height"),
1464 SettingEntry("gui.date_format_in_default_names"),
1466 /** Localisation options sub-page */
1467 static SettingsPage _settings_localisation_page = {_settings_localisation, lengthof(_settings_localisation)};
1469 static SettingEntry _settings_graphics[] = {
1470 SettingEntry("gui.zoom_min"),
1471 SettingEntry("gui.zoom_max"),
1472 SettingEntry("gui.smallmap_land_colour"),
1473 SettingEntry("gui.graph_line_thickness"),
1475 /** Graphics options sub-page */
1476 static SettingsPage _settings_graphics_page = {_settings_graphics, lengthof(_settings_graphics)};
1478 static SettingEntry _settings_sound[] = {
1479 SettingEntry("sound.click_beep"),
1480 SettingEntry("sound.confirm"),
1481 SettingEntry("sound.news_ticker"),
1482 SettingEntry("sound.news_full"),
1483 SettingEntry("sound.new_year"),
1484 SettingEntry("sound.disaster"),
1485 SettingEntry("sound.vehicle"),
1486 SettingEntry("sound.ambient"),
1488 /** Sound effects sub-page */
1489 static SettingsPage _settings_sound_page = {_settings_sound, lengthof(_settings_sound)};
1491 static SettingEntry _settings_interface_general[] = {
1492 SettingEntry("gui.osk_activation"),
1493 SettingEntry("gui.hover_delay_ms"),
1494 SettingEntry("gui.errmsg_duration"),
1495 SettingEntry("gui.window_snap_radius"),
1496 SettingEntry("gui.window_soft_limit"),
1498 /** Interface/General sub-page */
1499 static SettingsPage _settings_interface_general_page = {_settings_interface_general, lengthof(_settings_interface_general)};
1501 static SettingEntry _settings_interface_viewports[] = {
1502 SettingEntry("gui.auto_scrolling"),
1503 SettingEntry("gui.reverse_scroll"),
1504 SettingEntry("gui.smooth_scroll"),
1505 SettingEntry("gui.left_mouse_btn_scrolling"),
1506 /* While the horizontal scrollwheel scrolling is written as general code, only
1507 * the cocoa (OSX) driver generates input for it.
1508 * Since it's also able to completely disable the scrollwheel will we display it on all platforms anyway */
1509 SettingEntry("gui.scrollwheel_scrolling"),
1510 SettingEntry("gui.scrollwheel_multiplier"),
1511 #ifdef __APPLE__
1512 /* We might need to emulate a right mouse button on mac */
1513 SettingEntry("gui.right_mouse_btn_emulation"),
1514 #endif
1515 SettingEntry("gui.population_in_label"),
1516 SettingEntry("gui.liveries"),
1517 SettingEntry("construction.train_signal_side"),
1518 SettingEntry("gui.measure_tooltip"),
1519 SettingEntry("gui.loading_indicators"),
1520 SettingEntry("gui.show_track_reservation"),
1522 /** Interface/Viewports sub-page */
1523 static SettingsPage _settings_interface_viewports_page = {_settings_interface_viewports, lengthof(_settings_interface_viewports)};
1525 static SettingEntry _settings_interface_construction[] = {
1526 SettingEntry("gui.link_terraform_toolbar"),
1527 SettingEntry("gui.enable_signal_gui"),
1528 SettingEntry("gui.persistent_buildingtools"),
1529 SettingEntry("gui.quick_goto"),
1530 SettingEntry("gui.default_rail_type"),
1531 SettingEntry("gui.disable_unsuitable_building"),
1533 /** Interface/Construction sub-page */
1534 static SettingsPage _settings_interface_construction_page = {_settings_interface_construction, lengthof(_settings_interface_construction)};
1536 static SettingEntry _settings_interface[] = {
1537 SettingEntry(&_settings_interface_general_page, STR_CONFIG_SETTING_INTERFACE_GENERAL),
1538 SettingEntry(&_settings_interface_viewports_page, STR_CONFIG_SETTING_INTERFACE_VIEWPORTS),
1539 SettingEntry(&_settings_interface_construction_page, STR_CONFIG_SETTING_INTERFACE_CONSTRUCTION),
1540 SettingEntry("gui.autosave"),
1541 SettingEntry("gui.toolbar_pos"),
1542 SettingEntry("gui.statusbar_pos"),
1543 SettingEntry("gui.prefer_teamchat"),
1544 SettingEntry("gui.advanced_vehicle_list"),
1545 SettingEntry("gui.timetable_in_ticks"),
1546 SettingEntry("gui.timetable_arrival_departure"),
1547 SettingEntry("gui.expenses_layout"),
1550 /** Interface subpage */
1551 static SettingsPage _settings_interface_page = {_settings_interface, lengthof(_settings_interface)};
1553 static SettingEntry _settings_advisors[] = {
1554 SettingEntry("gui.coloured_news_year"),
1555 SettingEntry("news_display.general"),
1556 SettingEntry("news_display.new_vehicles"),
1557 SettingEntry("news_display.accident"),
1558 SettingEntry("news_display.company_info"),
1559 SettingEntry("news_display.acceptance"),
1560 SettingEntry("news_display.arrival_player"),
1561 SettingEntry("news_display.arrival_other"),
1562 SettingEntry("news_display.advice"),
1563 SettingEntry("gui.order_review_system"),
1564 SettingEntry("gui.vehicle_income_warn"),
1565 SettingEntry("gui.lost_vehicle_warn"),
1566 SettingEntry("gui.show_finances"),
1567 SettingEntry("news_display.economy"),
1568 SettingEntry("news_display.subsidies"),
1569 SettingEntry("news_display.open"),
1570 SettingEntry("news_display.close"),
1571 SettingEntry("news_display.production_player"),
1572 SettingEntry("news_display.production_other"),
1573 SettingEntry("news_display.production_nobody"),
1575 /** Interface/News sub-page */
1576 static SettingsPage _settings_advisors_page = {_settings_advisors, lengthof(_settings_advisors)};
1578 static SettingEntry _settings_company[] = {
1579 SettingEntry("gui.semaphore_build_before"),
1580 SettingEntry("gui.default_signal_type"),
1581 SettingEntry("gui.cycle_signal_types"),
1582 SettingEntry("gui.drag_signals_fixed_distance"),
1583 SettingEntry("gui.new_nonstop"),
1584 SettingEntry("gui.stop_location"),
1585 SettingEntry("company.servicing_if_no_breakdowns"),
1586 SettingEntry("company.engine_renew"),
1587 SettingEntry("company.engine_renew_months"),
1588 SettingEntry("company.engine_renew_money"),
1589 SettingEntry("vehicle.servint_ispercent"),
1590 SettingEntry("vehicle.servint_trains"),
1591 SettingEntry("vehicle.servint_roadveh"),
1592 SettingEntry("vehicle.servint_ships"),
1593 SettingEntry("vehicle.servint_aircraft"),
1596 /** Company subpage */
1597 static SettingsPage _settings_company_page = {_settings_company, lengthof(_settings_company)};
1599 static SettingEntry _settings_accounting[] = {
1600 SettingEntry("economy.inflation"),
1601 SettingEntry("difficulty.initial_interest"),
1602 SettingEntry("difficulty.max_loan"),
1603 SettingEntry("difficulty.subsidy_multiplier"),
1604 SettingEntry("economy.feeder_payment_share"),
1605 SettingEntry("economy.infrastructure_maintenance"),
1606 SettingEntry("difficulty.vehicle_costs"),
1607 SettingEntry("difficulty.construction_cost"),
1609 /** Accounting sub-page */
1610 static SettingsPage _settings_accounting_page = {_settings_accounting, lengthof(_settings_accounting)};
1612 static SettingEntry _settings_vehicles_physics[] = {
1613 SettingEntry("vehicle.train_acceleration_model"),
1614 SettingEntry("vehicle.train_slope_steepness"),
1615 SettingEntry("vehicle.wagon_speed_limits"),
1616 SettingEntry("vehicle.freight_trains"),
1617 SettingEntry("vehicle.roadveh_acceleration_model"),
1618 SettingEntry("vehicle.roadveh_slope_steepness"),
1619 SettingEntry("vehicle.smoke_amount"),
1620 SettingEntry("vehicle.plane_speed"),
1622 /** Vehicles/Physics sub-page */
1623 static SettingsPage _settings_vehicles_physics_page = {_settings_vehicles_physics, lengthof(_settings_vehicles_physics)};
1625 static SettingEntry _settings_vehicles_routing[] = {
1626 SettingEntry("difficulty.line_reverse_mode"),
1627 SettingEntry("pf.reverse_at_signals"),
1628 SettingEntry("pf.forbid_90_deg"),
1629 SettingEntry("pf.roadveh_queue"),
1630 SettingEntry("pf.pathfinder_for_ships"),
1632 /** Vehicles/Routing sub-page */
1633 static SettingsPage _settings_vehicles_routing_page = {_settings_vehicles_routing, lengthof(_settings_vehicles_routing)};
1635 static SettingEntry _settings_vehicles[] = {
1636 SettingEntry(&_settings_vehicles_physics_page, STR_CONFIG_SETTING_VEHICLES_PHYSICS),
1637 SettingEntry(&_settings_vehicles_routing_page, STR_CONFIG_SETTING_VEHICLES_ROUTING),
1638 SettingEntry("order.serviceathelipad"),
1639 SettingEntry("vehicle.never_expire_vehicles"),
1640 SettingEntry("vehicle.max_trains"),
1641 SettingEntry("vehicle.max_roadveh"),
1642 SettingEntry("vehicle.max_aircraft"),
1643 SettingEntry("vehicle.max_ships"),
1644 SettingEntry("vehicle.max_train_length"),
1646 /** Vehicles sub-page */
1647 static SettingsPage _settings_vehicles_page = {_settings_vehicles, lengthof(_settings_vehicles)};
1649 static SettingEntry _settings_construction[] = {
1650 SettingEntry("construction.command_pause_level"),
1651 SettingEntry("construction.build_on_slopes"),
1652 SettingEntry("construction.autoslope"),
1653 SettingEntry("construction.extra_dynamite"),
1654 SettingEntry("construction.max_heightlevel"),
1655 SettingEntry("construction.max_bridge_height"),
1656 SettingEntry("construction.max_bridge_length"),
1657 SettingEntry("construction.max_tunnel_length"),
1658 SettingEntry("construction.freeform_edges"),
1660 /** Construction sub-page */
1661 static SettingsPage _settings_construction_page = {_settings_construction, lengthof(_settings_construction)};
1663 static SettingEntry _settings_stations_cargo[] = {
1664 SettingEntry("order.improved_load"),
1665 SettingEntry("order.gradual_loading"),
1666 SettingEntry("order.selectgoods"),
1668 /** Cargo handling sub-page */
1669 static SettingsPage _settings_stations_cargo_page = {_settings_stations_cargo, lengthof(_settings_stations_cargo)};
1671 static SettingEntry _settings_stations[] = {
1672 SettingEntry(&_settings_stations_cargo_page, STR_CONFIG_SETTING_STATIONS_CARGOHANDLING),
1673 SettingEntry("station.adjacent_stations"),
1674 SettingEntry("station.distant_join_stations"),
1675 SettingEntry("station.station_spread"),
1676 SettingEntry("construction.road_stop_on_town_road"),
1677 SettingEntry("construction.road_stop_on_competitor_road"),
1678 SettingEntry("station.never_expire_airports"),
1679 SettingEntry("vehicle.disable_elrails"),
1681 /** Limitations sub-page */
1682 static SettingsPage _settings_stations_page = {_settings_stations, lengthof(_settings_stations)};
1684 static SettingEntry _settings_disasters[] = {
1685 SettingEntry("difficulty.disasters"),
1686 SettingEntry("difficulty.economy"),
1687 SettingEntry("difficulty.vehicle_breakdowns"),
1688 SettingEntry("vehicle.plane_crashes"),
1690 /** Disasters sub-page */
1691 static SettingsPage _settings_disasters_page = {_settings_disasters, lengthof(_settings_disasters)};
1693 static SettingEntry _settings_genworld[] = {
1694 SettingEntry("game_creation.landscape"),
1695 SettingEntry("game_creation.land_generator"),
1696 SettingEntry("difficulty.terrain_type"),
1697 SettingEntry("game_creation.tgen_smoothness"),
1698 SettingEntry("game_creation.variety"),
1699 SettingEntry("game_creation.snow_line_height"),
1700 SettingEntry("game_creation.amount_of_rivers"),
1701 SettingEntry("game_creation.tree_placer"),
1702 SettingEntry("vehicle.road_side"),
1703 SettingEntry("economy.larger_towns"),
1704 SettingEntry("economy.initial_city_size"),
1705 SettingEntry("economy.town_layout"),
1706 SettingEntry("difficulty.industry_density"),
1707 SettingEntry("gui.pause_on_newgame"),
1710 /** Genworld subpage */
1711 static SettingsPage _settings_genworld_page = {_settings_genworld, lengthof(_settings_genworld)};
1713 static SettingEntry _settings_environment_authorities[] = {
1714 SettingEntry("difficulty.town_council_tolerance"),
1715 SettingEntry("economy.bribe"),
1716 SettingEntry("economy.exclusive_rights"),
1717 SettingEntry("economy.fund_roads"),
1718 SettingEntry("economy.fund_buildings"),
1719 SettingEntry("economy.station_noise_level"),
1721 /** Environment/Authorities sub-page */
1722 static SettingsPage _settings_environment_authorities_page = {_settings_environment_authorities, lengthof(_settings_environment_authorities)};
1724 static SettingEntry _settings_environment_towns[] = {
1725 SettingEntry("economy.town_growth_rate"),
1726 SettingEntry("economy.allow_town_roads"),
1727 SettingEntry("economy.allow_town_level_crossings"),
1728 SettingEntry("economy.mod_road_rebuild"),
1729 SettingEntry("economy.found_town"),
1731 /** Environment/Towns sub-page */
1732 static SettingsPage _settings_environment_towns_page = {_settings_environment_towns, lengthof(_settings_environment_towns)};
1734 static SettingEntry _settings_environment_industries[] = {
1735 SettingEntry("construction.raw_industry_construction"),
1736 SettingEntry("construction.industry_platform"),
1737 SettingEntry("economy.multiple_industry_per_town"),
1738 SettingEntry("game_creation.oil_refinery_limit"),
1739 SettingEntry("economy.smooth_economy"),
1741 /** Environment/Industries subpage */
1742 static SettingsPage _settings_environment_industries_page = {_settings_environment_industries, lengthof(_settings_environment_industries)};
1744 static SettingEntry _settings_environment_cdist[] = {
1745 SettingEntry("linkgraph.recalc_time"),
1746 SettingEntry("linkgraph.recalc_interval"),
1747 SettingEntry("linkgraph.distribution_pax"),
1748 SettingEntry("linkgraph.distribution_mail"),
1749 SettingEntry("linkgraph.distribution_armoured"),
1750 SettingEntry("linkgraph.distribution_default"),
1751 SettingEntry("linkgraph.accuracy"),
1752 SettingEntry("linkgraph.demand_distance"),
1753 SettingEntry("linkgraph.demand_size"),
1754 SettingEntry("linkgraph.short_path_saturation"),
1756 /** Environment/Cargo Distribution sub-page */
1757 static SettingsPage _settings_environment_cdist_page = {_settings_environment_cdist, lengthof(_settings_environment_cdist)};
1759 static SettingEntry _settings_environment[] = {
1760 SettingEntry(&_settings_environment_authorities_page, STR_CONFIG_SETTING_ENVIRONMENT_AUTHORITIES),
1761 SettingEntry(&_settings_environment_towns_page, STR_CONFIG_SETTING_ENVIRONMENT_TOWNS),
1762 SettingEntry(&_settings_environment_industries_page, STR_CONFIG_SETTING_ENVIRONMENT_INDUSTRIES),
1763 SettingEntry(&_settings_environment_cdist_page, STR_CONFIG_SETTING_ENVIRONMENT_CARGODIST),
1764 SettingEntry("station.modified_catchment"),
1765 SettingEntry("construction.extra_tree_placement"),
1767 /** Environment sub-page */
1768 static SettingsPage _settings_environment_page = {_settings_environment, lengthof(_settings_environment)};
1770 static SettingEntry _settings_ai_npc[] = {
1771 SettingEntry("script.settings_profile"),
1772 SettingEntry("script.script_max_opcode_till_suspend"),
1773 SettingEntry("difficulty.competitor_speed"),
1774 SettingEntry("ai.ai_in_multiplayer"),
1775 SettingEntry("ai.ai_disable_veh_train"),
1776 SettingEntry("ai.ai_disable_veh_roadveh"),
1777 SettingEntry("ai.ai_disable_veh_aircraft"),
1778 SettingEntry("ai.ai_disable_veh_ship"),
1780 /** Computer players sub-page */
1781 static SettingsPage _settings_ai_npc_page = {_settings_ai_npc, lengthof(_settings_ai_npc)};
1783 static SettingEntry _settings_ai[] = {
1784 SettingEntry(&_settings_ai_npc_page, STR_CONFIG_SETTING_AI_NPC),
1785 SettingEntry("economy.give_money"),
1786 SettingEntry("economy.allow_shares"),
1788 /** AI sub-page */
1789 static SettingsPage _settings_ai_page = {_settings_ai, lengthof(_settings_ai)};
1791 static SettingEntry _settings_main[] = {
1792 SettingEntry(&_settings_localisation_page, STR_CONFIG_SETTING_LOCALISATION),
1793 SettingEntry(&_settings_graphics_page, STR_CONFIG_SETTING_GRAPHICS),
1794 SettingEntry(&_settings_sound_page, STR_CONFIG_SETTING_SOUND),
1795 SettingEntry(&_settings_interface_page, STR_CONFIG_SETTING_INTERFACE),
1796 SettingEntry(&_settings_advisors_page, STR_CONFIG_SETTING_ADVISORS),
1797 SettingEntry(&_settings_company_page, STR_CONFIG_SETTING_COMPANY),
1798 SettingEntry(&_settings_accounting_page, STR_CONFIG_SETTING_ACCOUNTING),
1799 SettingEntry(&_settings_vehicles_page, STR_CONFIG_SETTING_VEHICLES),
1800 SettingEntry(&_settings_construction_page, STR_CONFIG_SETTING_INTERFACE_CONSTRUCTION),
1801 SettingEntry(&_settings_stations_page, STR_CONFIG_SETTING_STATIONS),
1802 SettingEntry(&_settings_disasters_page, STR_CONFIG_SETTING_ACCIDENTS),
1803 SettingEntry(&_settings_genworld_page, STR_CONFIG_SETTING_GENWORLD),
1804 SettingEntry(&_settings_environment_page, STR_CONFIG_SETTING_ENVIRONMENT),
1805 SettingEntry(&_settings_ai_page, STR_CONFIG_SETTING_AI),
1808 /** Main page, holding all advanced settings */
1809 static SettingsPage _settings_main_page = {_settings_main, lengthof(_settings_main)};
1811 static const StringID _game_settings_restrict_dropdown[] = {
1812 STR_CONFIG_SETTING_RESTRICT_BASIC, // RM_BASIC
1813 STR_CONFIG_SETTING_RESTRICT_ADVANCED, // RM_ADVANCED
1814 STR_CONFIG_SETTING_RESTRICT_ALL, // RM_ALL
1815 STR_CONFIG_SETTING_RESTRICT_CHANGED_AGAINST_DEFAULT, // RM_CHANGED_AGAINST_DEFAULT
1816 STR_CONFIG_SETTING_RESTRICT_CHANGED_AGAINST_NEW, // RM_CHANGED_AGAINST_NEW
1818 assert_compile(lengthof(_game_settings_restrict_dropdown) == RM_END);
1820 /** Warnings about hidden search results. */
1821 enum WarnHiddenResult {
1822 WHR_NONE, ///< Nothing was filtering matches away.
1823 WHR_CATEGORY, ///< Category setting filtered matches away.
1824 WHR_TYPE, ///< Type setting filtered matches away.
1825 WHR_CATEGORY_TYPE, ///< Both category and type settings filtered matches away.
1828 /** Window to edit settings of the game. */
1829 struct GameSettingsWindow : Window {
1830 static const int SETTINGTREE_LEFT_OFFSET = 5; ///< Position of left edge of setting values
1831 static const int SETTINGTREE_RIGHT_OFFSET = 5; ///< Position of right edge of setting values
1832 static const int SETTINGTREE_TOP_OFFSET = 5; ///< Position of top edge of setting values
1833 static const int SETTINGTREE_BOTTOM_OFFSET = 5; ///< Position of bottom edge of setting values
1835 static GameSettings *settings_ptr; ///< Pointer to the game settings being displayed and modified.
1837 SettingEntry *valuewindow_entry; ///< If non-NULL, pointer to setting for which a value-entering window has been opened.
1838 SettingEntry *clicked_entry; ///< If non-NULL, pointer to a clicked numeric setting (with a depressed left or right button).
1839 SettingEntry *last_clicked; ///< If non-NULL, pointer to the last clicked setting.
1840 SettingEntry *valuedropdown_entry; ///< If non-NULL, pointer to the value for which a dropdown window is currently opened.
1841 bool closing_dropdown; ///< True, if the dropdown list is currently closing.
1843 SettingFilter filter; ///< Filter for the list.
1844 QueryStringN<50> filter_editbox; ///< Filter editbox;
1845 bool manually_changed_folding; ///< Whether the user expanded/collapsed something manually.
1846 WarnHiddenResult warn_missing; ///< Whether and how to warn about missing search results.
1847 int warn_lines; ///< Number of lines used for warning about missing search results.
1849 Scrollbar *vscroll;
1851 GameSettingsWindow (const WindowDesc *desc) : Window (desc),
1852 valuewindow_entry (NULL), clicked_entry (NULL),
1853 last_clicked (NULL), valuedropdown_entry (NULL),
1854 closing_dropdown (false), filter(), filter_editbox(),
1855 manually_changed_folding (false), warn_missing (WHR_NONE),
1856 warn_lines (0), vscroll (NULL)
1858 static bool first_time = true;
1860 this->warn_missing = WHR_NONE;
1861 this->warn_lines = 0;
1862 this->filter.mode = (RestrictionMode)_settings_client.gui.settings_restriction_mode;
1863 this->filter.min_cat = RM_ALL;
1864 this->filter.type = ST_ALL;
1865 this->filter.type_hides = false;
1866 this->settings_ptr = &GetGameSettings();
1868 _circle_size = maxdim(GetSpriteSize(SPR_CIRCLE_FOLDED), GetSpriteSize(SPR_CIRCLE_UNFOLDED));
1869 /* Build up the dynamic settings-array only once per OpenTTD session */
1870 if (first_time) {
1871 _settings_main_page.Init();
1872 first_time = false;
1873 } else {
1874 _settings_main_page.FoldAll(); // Close all sub-pages
1877 this->valuewindow_entry = NULL; // No setting entry for which a entry window is opened
1878 this->clicked_entry = NULL; // No numeric setting buttons are depressed
1879 this->last_clicked = NULL;
1880 this->valuedropdown_entry = NULL;
1881 this->closing_dropdown = false;
1882 this->manually_changed_folding = false;
1884 this->CreateNestedTree();
1885 this->vscroll = this->GetScrollbar(WID_GS_SCROLLBAR);
1886 this->InitNested(WN_GAME_OPTIONS_GAME_SETTINGS);
1888 this->querystrings[WID_GS_FILTER] = &this->filter_editbox;
1889 this->filter_editbox.cancel_button = QueryString::ACTION_CLEAR;
1890 this->SetFocusedWidget(WID_GS_FILTER);
1892 this->InvalidateData();
1895 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
1897 switch (widget) {
1898 case WID_GS_OPTIONSPANEL:
1899 resize->height = SETTING_HEIGHT = max(max<int>(_circle_size.height, SETTING_BUTTON_HEIGHT), FONT_HEIGHT_NORMAL) + 1;
1900 resize->width = 1;
1902 size->height = 5 * resize->height + SETTINGTREE_TOP_OFFSET + SETTINGTREE_BOTTOM_OFFSET;
1903 break;
1905 case WID_GS_HELP_TEXT: {
1906 static const StringID setting_types[] = {
1907 STR_CONFIG_SETTING_TYPE_CLIENT,
1908 STR_CONFIG_SETTING_TYPE_COMPANY_MENU, STR_CONFIG_SETTING_TYPE_COMPANY_INGAME,
1909 STR_CONFIG_SETTING_TYPE_GAME_MENU, STR_CONFIG_SETTING_TYPE_GAME_INGAME,
1911 for (uint i = 0; i < lengthof(setting_types); i++) {
1912 SetDParam(0, setting_types[i]);
1913 size->width = max(size->width, GetStringBoundingBox(STR_CONFIG_SETTING_TYPE).width);
1915 size->height = 2 * FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL +
1916 max(size->height, _settings_main_page.GetMaxHelpHeight(size->width));
1917 break;
1920 case WID_GS_RESTRICT_CATEGORY:
1921 case WID_GS_RESTRICT_TYPE:
1922 size->width = max(GetStringBoundingBox(STR_CONFIG_SETTING_RESTRICT_CATEGORY).width, GetStringBoundingBox(STR_CONFIG_SETTING_RESTRICT_TYPE).width);
1923 break;
1925 default:
1926 break;
1930 void OnPaint (BlitArea *dpi) OVERRIDE
1932 if (this->closing_dropdown) {
1933 this->closing_dropdown = false;
1934 assert(this->valuedropdown_entry != NULL);
1935 this->valuedropdown_entry->SetButtons(0);
1936 this->valuedropdown_entry = NULL;
1939 /* Reserve the correct number of lines for the 'some search results are hidden' notice in the central settings display panel. */
1940 const NWidgetBase *panel = this->GetWidget<NWidgetBase>(WID_GS_OPTIONSPANEL);
1941 StringID warn_str = STR_CONFIG_SETTING_CATEGORY_HIDES - 1 + this->warn_missing;
1942 int new_warn_lines;
1943 if (this->warn_missing == WHR_NONE) {
1944 new_warn_lines = 0;
1945 } else {
1946 SetDParam(0, _game_settings_restrict_dropdown[this->filter.min_cat]);
1947 new_warn_lines = GetStringLineCount(warn_str, panel->current_x);
1949 if (this->warn_lines != new_warn_lines) {
1950 this->vscroll->SetCount(this->vscroll->GetCount() - this->warn_lines + new_warn_lines);
1951 this->warn_lines = new_warn_lines;
1954 this->DrawWidgets (dpi);
1956 /* Draw the 'some search results are hidden' notice. */
1957 if (this->warn_missing != WHR_NONE) {
1958 const int left = panel->pos_x;
1959 const int right = left + panel->current_x - 1;
1960 const int top = panel->pos_y + WD_FRAMETEXT_TOP + (SETTING_HEIGHT - FONT_HEIGHT_NORMAL) * this->warn_lines / 2;
1961 SetDParam(0, _game_settings_restrict_dropdown[this->filter.min_cat]);
1962 if (this->warn_lines == 1) {
1963 /* If the warning fits at one line, center it. */
1964 DrawString (dpi, left + WD_FRAMETEXT_LEFT, right - WD_FRAMETEXT_RIGHT, top, warn_str, TC_FROMSTRING, SA_HOR_CENTER);
1965 } else {
1966 DrawStringMultiLine (dpi, left + WD_FRAMERECT_LEFT, right - WD_FRAMERECT_RIGHT, top, INT32_MAX, warn_str, TC_FROMSTRING, SA_HOR_CENTER);
1971 virtual void SetStringParameters(int widget) const
1973 switch (widget) {
1974 case WID_GS_RESTRICT_DROPDOWN:
1975 SetDParam(0, _game_settings_restrict_dropdown[this->filter.mode]);
1976 break;
1978 case WID_GS_TYPE_DROPDOWN:
1979 switch (this->filter.type) {
1980 case ST_GAME: SetDParam(0, _game_mode == GM_MENU ? STR_CONFIG_SETTING_TYPE_DROPDOWN_GAME_MENU : STR_CONFIG_SETTING_TYPE_DROPDOWN_GAME_INGAME); break;
1981 case ST_COMPANY: SetDParam(0, _game_mode == GM_MENU ? STR_CONFIG_SETTING_TYPE_DROPDOWN_COMPANY_MENU : STR_CONFIG_SETTING_TYPE_DROPDOWN_COMPANY_INGAME); break;
1982 case ST_CLIENT: SetDParam(0, STR_CONFIG_SETTING_TYPE_DROPDOWN_CLIENT); break;
1983 default: SetDParam(0, STR_CONFIG_SETTING_TYPE_DROPDOWN_ALL); break;
1985 break;
1989 DropDownList *BuildDropDownList(int widget) const
1991 DropDownList *list = NULL;
1992 switch (widget) {
1993 case WID_GS_RESTRICT_DROPDOWN:
1994 list = new DropDownList();
1996 for (int mode = 0; mode != RM_END; mode++) {
1997 /* If we are in adv. settings screen for the new game's settings,
1998 * we don't want to allow comparing with new game's settings. */
1999 bool disabled = mode == RM_CHANGED_AGAINST_NEW && settings_ptr == &_settings_newgame;
2001 *list->Append() = new DropDownListStringItem(_game_settings_restrict_dropdown[mode], mode, disabled);
2003 break;
2005 case WID_GS_TYPE_DROPDOWN:
2006 list = new DropDownList();
2007 *list->Append() = new DropDownListStringItem(STR_CONFIG_SETTING_TYPE_DROPDOWN_ALL, ST_ALL, false);
2008 *list->Append() = new DropDownListStringItem(_game_mode == GM_MENU ? STR_CONFIG_SETTING_TYPE_DROPDOWN_GAME_MENU : STR_CONFIG_SETTING_TYPE_DROPDOWN_GAME_INGAME, ST_GAME, false);
2009 *list->Append() = new DropDownListStringItem(_game_mode == GM_MENU ? STR_CONFIG_SETTING_TYPE_DROPDOWN_COMPANY_MENU : STR_CONFIG_SETTING_TYPE_DROPDOWN_COMPANY_INGAME, ST_COMPANY, false);
2010 *list->Append() = new DropDownListStringItem(STR_CONFIG_SETTING_TYPE_DROPDOWN_CLIENT, ST_CLIENT, false);
2011 break;
2013 return list;
2016 void DrawWidget (BlitArea *dpi, const Rect &r, int widget) const OVERRIDE
2018 switch (widget) {
2019 case WID_GS_OPTIONSPANEL: {
2020 int top_pos = r.top + SETTINGTREE_TOP_OFFSET + 1 + this->warn_lines * SETTING_HEIGHT;
2021 uint last_row = this->vscroll->GetPosition() + this->vscroll->GetCapacity() - this->warn_lines;
2022 int next_row = _settings_main_page.Draw (settings_ptr, dpi, r.left + SETTINGTREE_LEFT_OFFSET, r.right - SETTINGTREE_RIGHT_OFFSET, top_pos,
2023 this->vscroll->GetPosition(), last_row, this->last_clicked);
2024 if (next_row == 0) DrawString (dpi, r.left + SETTINGTREE_LEFT_OFFSET, r.right - SETTINGTREE_RIGHT_OFFSET, top_pos, STR_CONFIG_SETTINGS_NONE);
2025 break;
2028 case WID_GS_HELP_TEXT:
2029 if (this->last_clicked != NULL) {
2030 const SettingDesc *sd = this->last_clicked->d.entry.setting;
2032 int y = r.top;
2033 switch (sd->GetType()) {
2034 case ST_COMPANY: SetDParam(0, _game_mode == GM_MENU ? STR_CONFIG_SETTING_TYPE_COMPANY_MENU : STR_CONFIG_SETTING_TYPE_COMPANY_INGAME); break;
2035 case ST_CLIENT: SetDParam(0, STR_CONFIG_SETTING_TYPE_CLIENT); break;
2036 case ST_GAME: SetDParam(0, _game_mode == GM_MENU ? STR_CONFIG_SETTING_TYPE_GAME_MENU : STR_CONFIG_SETTING_TYPE_GAME_INGAME); break;
2037 default: NOT_REACHED();
2039 DrawString (dpi, r.left, r.right, y, STR_CONFIG_SETTING_TYPE);
2040 y += FONT_HEIGHT_NORMAL;
2042 int32 default_value = ReadValue(&sd->desc.def, sd->save.conv);
2043 this->last_clicked->SetValueDParams(0, default_value);
2044 DrawString (dpi, r.left, r.right, y, STR_CONFIG_SETTING_DEFAULT_VALUE);
2045 y += FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL;
2047 DrawStringMultiLine (dpi, r.left, r.right, y, r.bottom, this->last_clicked->GetHelpText(), TC_WHITE);
2049 break;
2051 default:
2052 break;
2057 * Set the entry that should have its help text displayed, and mark the window dirty so it gets repainted.
2058 * @param pe Setting to display help text of, use \c NULL to stop displaying help of the currently displayed setting.
2060 void SetDisplayedHelpText(SettingEntry *pe)
2062 if (this->last_clicked != pe) this->SetDirty();
2063 this->last_clicked = pe;
2066 virtual void OnClick(Point pt, int widget, int click_count)
2068 switch (widget) {
2069 case WID_GS_EXPAND_ALL:
2070 this->manually_changed_folding = true;
2071 _settings_main_page.UnFoldAll();
2072 this->InvalidateData();
2073 break;
2075 case WID_GS_COLLAPSE_ALL:
2076 this->manually_changed_folding = true;
2077 _settings_main_page.FoldAll();
2078 this->InvalidateData();
2079 break;
2081 case WID_GS_RESTRICT_DROPDOWN: {
2082 DropDownList *list = this->BuildDropDownList(widget);
2083 if (list != NULL) {
2084 ShowDropDownList(this, list, this->filter.mode, widget);
2086 break;
2089 case WID_GS_TYPE_DROPDOWN: {
2090 DropDownList *list = this->BuildDropDownList(widget);
2091 if (list != NULL) {
2092 ShowDropDownList(this, list, this->filter.type, widget);
2094 break;
2098 if (widget != WID_GS_OPTIONSPANEL) return;
2100 uint btn = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_GS_OPTIONSPANEL, SETTINGTREE_TOP_OFFSET);
2101 if (btn == INT_MAX || (int)btn < this->warn_lines) return;
2102 btn -= this->warn_lines;
2104 uint cur_row = 0;
2105 SettingEntry *pe = _settings_main_page.FindEntry(btn, &cur_row);
2107 if (pe == NULL) return; // Clicked below the last setting of the page
2109 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
2110 if (x < 0) return; // Clicked left of the entry
2112 if ((pe->flags & SEF_KIND_MASK) == SEF_SUBTREE_KIND) {
2113 this->SetDisplayedHelpText(NULL);
2114 pe->d.sub.folded = !pe->d.sub.folded; // Flip 'folded'-ness of the sub-page
2116 this->manually_changed_folding = true;
2118 this->InvalidateData();
2119 return;
2122 assert((pe->flags & SEF_KIND_MASK) == SEF_SETTING_KIND);
2123 const SettingDesc *sd = pe->d.entry.setting;
2125 /* return if action is only active in network, or only settable by server */
2126 if (!sd->IsEditable()) {
2127 this->SetDisplayedHelpText(pe);
2128 return;
2131 int32 value = ReadVariable (settings_ptr, sd);
2133 /* clicked on the icon on the left side. Either scroller, bool on/off or dropdown */
2134 if (x < SETTING_BUTTON_WIDTH && (sd->desc.flags & SGF_MULTISTRING)) {
2135 const SettingDescBase *sdb = &sd->desc;
2136 this->SetDisplayedHelpText(pe);
2138 if (this->valuedropdown_entry == pe) {
2139 /* unclick the dropdown */
2140 HideDropDownMenu(this);
2141 this->closing_dropdown = false;
2142 this->valuedropdown_entry->SetButtons(0);
2143 this->valuedropdown_entry = NULL;
2144 } else {
2145 if (this->valuedropdown_entry != NULL) this->valuedropdown_entry->SetButtons(0);
2146 this->closing_dropdown = false;
2148 const NWidgetBase *wid = this->GetWidget<NWidgetBase>(WID_GS_OPTIONSPANEL);
2149 int rel_y = (pt.y - (int)wid->pos_y - SETTINGTREE_TOP_OFFSET) % wid->resize_y;
2151 Rect wi_rect;
2152 wi_rect.left = pt.x - (_current_text_dir == TD_RTL ? SETTING_BUTTON_WIDTH - 1 - x : x);
2153 wi_rect.right = wi_rect.left + SETTING_BUTTON_WIDTH - 1;
2154 wi_rect.top = pt.y - rel_y + (SETTING_HEIGHT - SETTING_BUTTON_HEIGHT) / 2;
2155 wi_rect.bottom = wi_rect.top + SETTING_BUTTON_HEIGHT - 1;
2157 /* For dropdowns we also have to check the y position thoroughly, the mouse may not above the just opening dropdown */
2158 if (pt.y >= wi_rect.top && pt.y <= wi_rect.bottom) {
2159 this->valuedropdown_entry = pe;
2160 this->valuedropdown_entry->SetButtons(SEF_LEFT_DEPRESSED);
2162 DropDownList *list = new DropDownList();
2163 for (int i = sdb->min; i <= (int)sdb->max; i++) {
2164 *list->Append() = new DropDownListStringItem(sdb->str_val + i - sdb->min, i, false);
2167 ShowDropDownListAt(this, list, value, -1, wi_rect, COLOUR_ORANGE, true);
2170 this->SetDirty();
2171 } else if (x < SETTING_BUTTON_WIDTH) {
2172 this->SetDisplayedHelpText(pe);
2173 const SettingDescBase *sdb = &sd->desc;
2174 int32 oldvalue = value;
2176 switch (sdb->cmd) {
2177 case SDT_BOOLX: value ^= 1; break;
2178 case SDT_ONEOFMANY:
2179 case SDT_NUMX: {
2180 /* Add a dynamic step-size to the scroller. In a maximum of
2181 * 50-steps you should be able to get from min to max,
2182 * unless specified otherwise in the 'interval' variable
2183 * of the current setting. */
2184 uint32 step = (sdb->interval == 0) ? ((sdb->max - sdb->min) / 50) : sdb->interval;
2185 if (step == 0) step = 1;
2187 /* don't allow too fast scrolling */
2188 if ((this->flags & WF_TIMEOUT) && this->timeout_timer > 1) {
2189 _left_button_clicked = false;
2190 return;
2193 /* Increase or decrease the value and clamp it to extremes */
2194 if (x >= SETTING_BUTTON_WIDTH / 2) {
2195 value += step;
2196 if (sdb->min < 0) {
2197 assert((int32)sdb->max >= 0);
2198 if (value > (int32)sdb->max) value = (int32)sdb->max;
2199 } else {
2200 if ((uint32)value > sdb->max) value = (int32)sdb->max;
2202 if (value < sdb->min) value = sdb->min; // skip between "disabled" and minimum
2203 } else {
2204 value -= step;
2205 if (value < sdb->min) value = (sdb->flags & SGF_0ISDISABLED) ? 0 : sdb->min;
2208 /* Set up scroller timeout for numeric values */
2209 if (value != oldvalue) {
2210 if (this->clicked_entry != NULL) { // Release previous buttons if any
2211 this->clicked_entry->SetButtons(0);
2213 this->clicked_entry = pe;
2214 this->clicked_entry->SetButtons((x >= SETTING_BUTTON_WIDTH / 2) != (_current_text_dir == TD_RTL) ? SEF_RIGHT_DEPRESSED : SEF_LEFT_DEPRESSED);
2215 this->SetTimeout();
2216 _left_button_clicked = false;
2218 break;
2221 default: NOT_REACHED();
2224 if (value != oldvalue) {
2225 if ((sd->desc.flags & SGF_PER_COMPANY) != 0) {
2226 SetCompanySetting(pe->d.entry.index, value);
2227 } else {
2228 SetSettingValue(pe->d.entry.index, value);
2230 this->SetDirty();
2232 } else {
2233 /* Only open editbox if clicked for the second time, and only for types where it is sensible for. */
2234 if (this->last_clicked == pe && sd->desc.cmd != SDT_BOOLX && !(sd->desc.flags & SGF_MULTISTRING)) {
2235 /* Show the correct currency-translated value */
2236 if (sd->desc.flags & SGF_CURRENCY) value *= _currency->rate;
2238 this->valuewindow_entry = pe;
2239 SetDParam(0, value);
2240 ShowQueryString(STR_JUST_INT, STR_CONFIG_SETTING_QUERY_CAPTION, 10, this, CS_NUMERAL, QSF_ENABLE_DEFAULT);
2242 this->SetDisplayedHelpText(pe);
2246 virtual void OnTimeout()
2248 if (this->clicked_entry != NULL) { // On timeout, release any depressed buttons
2249 this->clicked_entry->SetButtons(0);
2250 this->clicked_entry = NULL;
2251 this->SetDirty();
2255 virtual void OnQueryTextFinished(char *str)
2257 /* The user pressed cancel */
2258 if (str == NULL) return;
2260 assert(this->valuewindow_entry != NULL);
2261 assert((this->valuewindow_entry->flags & SEF_KIND_MASK) == SEF_SETTING_KIND);
2262 const SettingDesc *sd = this->valuewindow_entry->d.entry.setting;
2264 int32 value;
2265 if (!StrEmpty(str)) {
2266 value = atoi(str);
2268 /* Save the correct currency-translated value */
2269 if (sd->desc.flags & SGF_CURRENCY) value /= _currency->rate;
2270 } else {
2271 value = (int32)(size_t)sd->desc.def;
2274 if ((sd->desc.flags & SGF_PER_COMPANY) != 0) {
2275 SetCompanySetting(this->valuewindow_entry->d.entry.index, value);
2276 } else {
2277 SetSettingValue(this->valuewindow_entry->d.entry.index, value);
2279 this->SetDirty();
2282 virtual void OnDropdownSelect(int widget, int index)
2284 switch (widget) {
2285 case WID_GS_RESTRICT_DROPDOWN:
2286 this->filter.mode = (RestrictionMode)index;
2287 if (this->filter.mode == RM_CHANGED_AGAINST_DEFAULT ||
2288 this->filter.mode == RM_CHANGED_AGAINST_NEW) {
2290 if (!this->manually_changed_folding) {
2291 /* Expand all when selecting 'changes'. Update the filter state first, in case it becomes less restrictive in some cases. */
2292 _settings_main_page.UpdateFilterState(this->filter, false);
2293 _settings_main_page.UnFoldAll();
2295 } else {
2296 /* Non-'changes' filter. Save as default. */
2297 _settings_client.gui.settings_restriction_mode = this->filter.mode;
2299 this->InvalidateData();
2300 break;
2302 case WID_GS_TYPE_DROPDOWN:
2303 this->filter.type = (SettingType)index;
2304 this->InvalidateData();
2305 break;
2307 default:
2308 if (widget < 0) {
2309 /* Deal with drop down boxes on the panel. */
2310 assert(this->valuedropdown_entry != NULL);
2311 const SettingDesc *sd = this->valuedropdown_entry->d.entry.setting;
2312 assert(sd->desc.flags & SGF_MULTISTRING);
2314 if ((sd->desc.flags & SGF_PER_COMPANY) != 0) {
2315 SetCompanySetting(this->valuedropdown_entry->d.entry.index, index);
2316 } else {
2317 SetSettingValue(this->valuedropdown_entry->d.entry.index, index);
2320 this->SetDirty();
2322 break;
2326 virtual void OnDropdownClose(Point pt, int widget, int index, bool instant_close)
2328 if (widget >= 0) {
2329 /* Normally the default implementation of OnDropdownClose() takes care of
2330 * a few things. We want that behaviour here too, but only for
2331 * "normal" dropdown boxes. The special dropdown boxes added for every
2332 * setting that needs one can't have this call. */
2333 Window::OnDropdownClose(pt, widget, index, instant_close);
2334 } else {
2335 /* We cannot raise the dropdown button just yet. OnClick needs some hint, whether
2336 * the same dropdown button was clicked again, and then not open the dropdown again.
2337 * So, we only remember that it was closed, and process it on the next OnPaint, which is
2338 * after OnClick. */
2339 assert(this->valuedropdown_entry != NULL);
2340 this->closing_dropdown = true;
2341 this->SetDirty();
2345 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
2347 if (!gui_scope) return;
2349 /* Update which settings are to be visible. */
2350 RestrictionMode min_level = (this->filter.mode <= RM_ALL) ? this->filter.mode : RM_BASIC;
2351 this->filter.min_cat = min_level;
2352 this->filter.type_hides = false;
2353 _settings_main_page.UpdateFilterState(this->filter, false);
2355 if (this->filter.string.IsEmpty()) {
2356 this->warn_missing = WHR_NONE;
2357 } else if (min_level < this->filter.min_cat) {
2358 this->warn_missing = this->filter.type_hides ? WHR_CATEGORY_TYPE : WHR_CATEGORY;
2359 } else {
2360 this->warn_missing = this->filter.type_hides ? WHR_TYPE : WHR_NONE;
2362 this->vscroll->SetCount(_settings_main_page.Length() + this->warn_lines);
2364 if (this->last_clicked != NULL && !_settings_main_page.IsVisible(this->last_clicked)) {
2365 this->SetDisplayedHelpText(NULL);
2368 bool all_folded = true;
2369 bool all_unfolded = true;
2370 _settings_main_page.GetFoldingState(all_folded, all_unfolded);
2371 this->SetWidgetDisabledState(WID_GS_EXPAND_ALL, all_unfolded);
2372 this->SetWidgetDisabledState(WID_GS_COLLAPSE_ALL, all_folded);
2375 virtual void OnEditboxChanged(int wid)
2377 if (wid == WID_GS_FILTER) {
2378 this->filter.string.SetFilterTerm(this->filter_editbox.GetText());
2379 if (!this->filter.string.IsEmpty() && !this->manually_changed_folding) {
2380 /* User never expanded/collapsed single pages and entered a filter term.
2381 * Expand everything, to save weird expand clicks, */
2382 _settings_main_page.UnFoldAll();
2384 this->InvalidateData();
2388 virtual void OnResize()
2390 this->vscroll->SetCapacityFromWidget(this, WID_GS_OPTIONSPANEL, SETTINGTREE_TOP_OFFSET + SETTINGTREE_BOTTOM_OFFSET);
2394 GameSettings *GameSettingsWindow::settings_ptr = NULL;
2396 static const NWidgetPart _nested_settings_selection_widgets[] = {
2397 NWidget(NWID_HORIZONTAL),
2398 NWidget(WWT_CLOSEBOX, COLOUR_MAUVE),
2399 NWidget(WWT_CAPTION, COLOUR_MAUVE), SetDataTip(STR_CONFIG_SETTING_TREE_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
2400 NWidget(WWT_DEFSIZEBOX, COLOUR_MAUVE),
2401 EndContainer(),
2402 NWidget(WWT_PANEL, COLOUR_MAUVE),
2403 NWidget(NWID_VERTICAL), SetPIP(0, WD_PAR_VSEP_NORMAL, 0), SetPadding(WD_TEXTPANEL_TOP, 0, WD_TEXTPANEL_BOTTOM, 0),
2404 NWidget(NWID_HORIZONTAL), SetPIP(WD_FRAMETEXT_LEFT, WD_FRAMETEXT_RIGHT, WD_FRAMETEXT_RIGHT),
2405 NWidget(WWT_TEXT, COLOUR_MAUVE, WID_GS_RESTRICT_CATEGORY), SetDataTip(STR_CONFIG_SETTING_RESTRICT_CATEGORY, STR_NULL),
2406 NWidget(WWT_DROPDOWN, COLOUR_MAUVE, WID_GS_RESTRICT_DROPDOWN), SetMinimalSize(100, 12), SetDataTip(STR_BLACK_STRING, STR_CONFIG_SETTING_RESTRICT_DROPDOWN_HELPTEXT), SetFill(1, 0), SetResize(1, 0),
2407 EndContainer(),
2408 NWidget(NWID_HORIZONTAL), SetPIP(WD_FRAMETEXT_LEFT, WD_FRAMETEXT_RIGHT, WD_FRAMETEXT_RIGHT),
2409 NWidget(WWT_TEXT, COLOUR_MAUVE, WID_GS_RESTRICT_TYPE), SetDataTip(STR_CONFIG_SETTING_RESTRICT_TYPE, STR_NULL),
2410 NWidget(WWT_DROPDOWN, COLOUR_MAUVE, WID_GS_TYPE_DROPDOWN), SetMinimalSize(100, 12), SetDataTip(STR_BLACK_STRING, STR_CONFIG_SETTING_TYPE_DROPDOWN_HELPTEXT), SetFill(1, 0), SetResize(1, 0),
2411 EndContainer(),
2412 EndContainer(),
2413 NWidget(NWID_HORIZONTAL), SetPadding(0, 0, WD_TEXTPANEL_BOTTOM, 0),
2414 SetPIP(WD_FRAMETEXT_LEFT, WD_FRAMETEXT_RIGHT, WD_FRAMETEXT_RIGHT),
2415 NWidget(WWT_TEXT, COLOUR_MAUVE), SetFill(0, 1), SetDataTip(STR_CONFIG_SETTING_FILTER_TITLE, STR_NULL),
2416 NWidget(WWT_EDITBOX, COLOUR_MAUVE, WID_GS_FILTER), SetFill(1, 0), SetMinimalSize(50, 12), SetResize(1, 0),
2417 SetDataTip(STR_LIST_FILTER_OSKTITLE, STR_LIST_FILTER_TOOLTIP),
2418 EndContainer(),
2419 EndContainer(),
2420 NWidget(NWID_HORIZONTAL),
2421 NWidget(WWT_PANEL, COLOUR_MAUVE, WID_GS_OPTIONSPANEL), SetMinimalSize(400, 174), SetScrollbar(WID_GS_SCROLLBAR), EndContainer(),
2422 NWidget(NWID_VSCROLLBAR, COLOUR_MAUVE, WID_GS_SCROLLBAR),
2423 EndContainer(),
2424 NWidget(WWT_PANEL, COLOUR_MAUVE), SetMinimalSize(400, 40),
2425 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_GS_HELP_TEXT), SetMinimalSize(300, 25), SetFill(1, 1), SetResize(1, 0),
2426 SetPadding(WD_FRAMETEXT_TOP, WD_FRAMETEXT_RIGHT, WD_FRAMETEXT_BOTTOM, WD_FRAMETEXT_LEFT),
2427 EndContainer(),
2428 NWidget(NWID_HORIZONTAL),
2429 NWidget(WWT_PUSHTXTBTN, COLOUR_MAUVE, WID_GS_EXPAND_ALL), SetDataTip(STR_CONFIG_SETTING_EXPAND_ALL, STR_NULL),
2430 NWidget(WWT_PUSHTXTBTN, COLOUR_MAUVE, WID_GS_COLLAPSE_ALL), SetDataTip(STR_CONFIG_SETTING_COLLAPSE_ALL, STR_NULL),
2431 NWidget(WWT_PANEL, COLOUR_MAUVE), SetFill(1, 0), SetResize(1, 0),
2432 EndContainer(),
2433 NWidget(WWT_RESIZEBOX, COLOUR_MAUVE),
2434 EndContainer(),
2437 static WindowDesc::Prefs _settings_selection_prefs ("settings");
2439 static const WindowDesc _settings_selection_desc(
2440 WDP_CENTER, 510, 450,
2441 WC_GAME_OPTIONS, WC_NONE,
2443 _nested_settings_selection_widgets, lengthof(_nested_settings_selection_widgets),
2444 &_settings_selection_prefs
2447 /** Open advanced settings window. */
2448 void ShowGameSettings()
2450 DeleteWindowByClass(WC_GAME_OPTIONS);
2451 new GameSettingsWindow(&_settings_selection_desc);
2456 * Draw [<][>] boxes.
2457 * @param dpi the area to draw on
2458 * @param x the x position to draw
2459 * @param y the y position to draw
2460 * @param button_colour the colour of the button
2461 * @param state 0 = none clicked, 1 = first clicked, 2 = second clicked
2462 * @param clickable_left is the left button clickable?
2463 * @param clickable_right is the right button clickable?
2465 void DrawArrowButtons (BlitArea *dpi, int x, int y, Colours button_colour,
2466 byte state, bool clickable_left, bool clickable_right)
2468 int colour = _colour_gradient[button_colour][2];
2469 Dimension dim = NWidgetScrollbar::GetHorizontalDimension();
2471 DrawFrameRect (dpi, x, y, x + dim.width - 1, y + dim.height - 1, button_colour, (state == 1) ? FR_LOWERED : FR_NONE);
2472 DrawFrameRect (dpi, x + dim.width, y, x + dim.width + dim.width - 1, y + dim.height - 1, button_colour, (state == 2) ? FR_LOWERED : FR_NONE);
2473 DrawSprite (dpi, SPR_ARROW_LEFT, PAL_NONE, x + WD_IMGBTN_LEFT, y + WD_IMGBTN_TOP);
2474 DrawSprite (dpi, SPR_ARROW_RIGHT, PAL_NONE, x + WD_IMGBTN_LEFT + dim.width, y + WD_IMGBTN_TOP);
2476 /* Grey out the buttons that aren't clickable */
2477 bool rtl = _current_text_dir == TD_RTL;
2478 if (rtl ? !clickable_right : !clickable_left) {
2479 GfxFillRect (dpi, x + 1, y, x + dim.width - 1, y + dim.height - 2, colour, FILLRECT_CHECKER);
2481 if (rtl ? !clickable_left : !clickable_right) {
2482 GfxFillRect (dpi, x + dim.width + 1, y, x + dim.width + dim.width - 1, y + dim.height - 2, colour, FILLRECT_CHECKER);
2487 * Draw a dropdown button.
2488 * @param dpi the area to draw on
2489 * @param x the x position to draw
2490 * @param y the y position to draw
2491 * @param button_colour the colour of the button
2492 * @param state true = lowered
2493 * @param clickable is the button clickable?
2495 void DrawDropDownButton (BlitArea *dpi, int x, int y, Colours button_colour,
2496 bool state, bool clickable)
2498 int colour = _colour_gradient[button_colour][2];
2500 DrawFrameRect (dpi, x, y, x + SETTING_BUTTON_WIDTH - 1, y + SETTING_BUTTON_HEIGHT - 1, button_colour, state ? FR_LOWERED : FR_NONE);
2501 DrawSprite (dpi, SPR_ARROW_DOWN, PAL_NONE, x + (SETTING_BUTTON_WIDTH - NWidgetScrollbar::GetVerticalDimension().width) / 2 + state, y + 2 + state);
2503 if (!clickable) {
2504 GfxFillRect (dpi, x + 1, y, x + SETTING_BUTTON_WIDTH - 1, y + SETTING_BUTTON_HEIGHT - 2, colour, FILLRECT_CHECKER);
2509 * Draw a toggle button.
2510 * @param dpi the area to draw on
2511 * @param x the x position to draw
2512 * @param y the y position to draw
2513 * @param state true = lowered
2514 * @param clickable is the button clickable?
2516 void DrawBoolButton (BlitArea *dpi, int x, int y, bool state, bool clickable)
2518 static const Colours _bool_ctabs[2][2] = {{COLOUR_CREAM, COLOUR_RED}, {COLOUR_DARK_GREEN, COLOUR_GREEN}};
2519 DrawFrameRect (dpi, x, y, x + SETTING_BUTTON_WIDTH - 1, y + SETTING_BUTTON_HEIGHT - 1, _bool_ctabs[state][clickable], state ? FR_LOWERED : FR_NONE);
2522 struct CustomCurrencyWindow : Window {
2523 int query_widget;
2525 CustomCurrencyWindow (const WindowDesc *desc) :
2526 Window (desc), query_widget (0)
2528 this->InitNested();
2530 SetButtonState();
2533 void SetButtonState()
2535 this->SetWidgetDisabledState(WID_CC_RATE_DOWN, _custom_currency.rate == 1);
2536 this->SetWidgetDisabledState(WID_CC_RATE_UP, _custom_currency.rate == UINT16_MAX);
2537 this->SetWidgetDisabledState(WID_CC_YEAR_DOWN, _custom_currency.to_euro == CF_NOEURO);
2538 this->SetWidgetDisabledState(WID_CC_YEAR_UP, _custom_currency.to_euro == MAX_YEAR);
2541 virtual void SetStringParameters(int widget) const
2543 switch (widget) {
2544 case WID_CC_RATE: SetDParam(0, 1); SetDParam(1, 1); break;
2545 case WID_CC_SEPARATOR: SetDParamStr(0, _custom_currency.separator); break;
2546 case WID_CC_PREFIX: SetDParamStr(0, _custom_currency.prefix); break;
2547 case WID_CC_SUFFIX: SetDParamStr(0, _custom_currency.suffix); break;
2548 case WID_CC_YEAR:
2549 SetDParam(0, (_custom_currency.to_euro != CF_NOEURO) ? STR_CURRENCY_SWITCH_TO_EURO : STR_CURRENCY_SWITCH_TO_EURO_NEVER);
2550 SetDParam(1, _custom_currency.to_euro);
2551 break;
2553 case WID_CC_PREVIEW:
2554 SetDParam(0, 10000);
2555 break;
2559 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
2561 switch (widget) {
2562 /* Set the appropriate width for the edit 'buttons' */
2563 case WID_CC_SEPARATOR_EDIT:
2564 case WID_CC_PREFIX_EDIT:
2565 case WID_CC_SUFFIX_EDIT:
2566 size->width = this->GetWidget<NWidgetBase>(WID_CC_RATE_DOWN)->smallest_x + this->GetWidget<NWidgetBase>(WID_CC_RATE_UP)->smallest_x;
2567 break;
2569 /* Make sure the window is wide enough for the widest exchange rate */
2570 case WID_CC_RATE:
2571 SetDParam(0, 1);
2572 SetDParam(1, INT32_MAX);
2573 *size = GetStringBoundingBox(STR_CURRENCY_EXCHANGE_RATE);
2574 break;
2578 virtual void OnClick(Point pt, int widget, int click_count)
2580 int line = 0;
2581 int len = 0;
2582 StringID str = 0;
2583 CharSetFilter afilter = CS_ALPHANUMERAL;
2584 QueryStringFlags flags = QSF_NONE;
2586 switch (widget) {
2587 case WID_CC_RATE_DOWN:
2588 if (_custom_currency.rate > 1) _custom_currency.rate--;
2589 if (_custom_currency.rate == 1) this->DisableWidget(WID_CC_RATE_DOWN);
2590 this->EnableWidget(WID_CC_RATE_UP);
2591 break;
2593 case WID_CC_RATE_UP:
2594 if (_custom_currency.rate < UINT16_MAX) _custom_currency.rate++;
2595 if (_custom_currency.rate == UINT16_MAX) this->DisableWidget(WID_CC_RATE_UP);
2596 this->EnableWidget(WID_CC_RATE_DOWN);
2597 break;
2599 case WID_CC_RATE:
2600 SetDParam(0, _custom_currency.rate);
2601 str = STR_JUST_INT;
2602 len = 5;
2603 line = WID_CC_RATE;
2604 afilter = CS_NUMERAL;
2605 break;
2607 case WID_CC_SEPARATOR_EDIT:
2608 case WID_CC_SEPARATOR:
2609 SetDParamStr(0, _custom_currency.separator);
2610 str = STR_JUST_RAW_STRING;
2611 len = 1;
2612 line = WID_CC_SEPARATOR;
2613 flags = QSF_LEN_IN_CHARS;
2614 break;
2616 case WID_CC_PREFIX_EDIT:
2617 case WID_CC_PREFIX:
2618 SetDParamStr(0, _custom_currency.prefix);
2619 str = STR_JUST_RAW_STRING;
2620 len = 12;
2621 line = WID_CC_PREFIX;
2622 break;
2624 case WID_CC_SUFFIX_EDIT:
2625 case WID_CC_SUFFIX:
2626 SetDParamStr(0, _custom_currency.suffix);
2627 str = STR_JUST_RAW_STRING;
2628 len = 12;
2629 line = WID_CC_SUFFIX;
2630 break;
2632 case WID_CC_YEAR_DOWN:
2633 _custom_currency.to_euro = (_custom_currency.to_euro <= 2000) ? CF_NOEURO : _custom_currency.to_euro - 1;
2634 if (_custom_currency.to_euro == CF_NOEURO) this->DisableWidget(WID_CC_YEAR_DOWN);
2635 this->EnableWidget(WID_CC_YEAR_UP);
2636 break;
2638 case WID_CC_YEAR_UP:
2639 _custom_currency.to_euro = Clamp(_custom_currency.to_euro + 1, 2000, MAX_YEAR);
2640 if (_custom_currency.to_euro == MAX_YEAR) this->DisableWidget(WID_CC_YEAR_UP);
2641 this->EnableWidget(WID_CC_YEAR_DOWN);
2642 break;
2644 case WID_CC_YEAR:
2645 SetDParam(0, _custom_currency.to_euro);
2646 str = STR_JUST_INT;
2647 len = 7;
2648 line = WID_CC_YEAR;
2649 afilter = CS_NUMERAL;
2650 break;
2653 if (len != 0) {
2654 this->query_widget = line;
2655 ShowQueryString(str, STR_CURRENCY_CHANGE_PARAMETER, len + 1, this, afilter, flags);
2658 this->SetTimeout();
2659 this->SetDirty();
2662 virtual void OnQueryTextFinished(char *str)
2664 if (str == NULL) return;
2666 switch (this->query_widget) {
2667 case WID_CC_RATE:
2668 _custom_currency.rate = Clamp(atoi(str), 1, UINT16_MAX);
2669 break;
2671 case WID_CC_SEPARATOR: // Thousands separator
2672 bstrcpy (_custom_currency.separator, str);
2673 break;
2675 case WID_CC_PREFIX:
2676 bstrcpy (_custom_currency.prefix, str);
2677 break;
2679 case WID_CC_SUFFIX:
2680 bstrcpy (_custom_currency.suffix, str);
2681 break;
2683 case WID_CC_YEAR: { // Year to switch to euro
2684 int val = atoi(str);
2686 _custom_currency.to_euro = (val < 2000 ? CF_NOEURO : min(val, MAX_YEAR));
2687 break;
2690 MarkWholeScreenDirty();
2691 SetButtonState();
2694 virtual void OnTimeout()
2696 this->SetDirty();
2700 static const NWidgetPart _nested_cust_currency_widgets[] = {
2701 NWidget(NWID_HORIZONTAL),
2702 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
2703 NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_CURRENCY_WINDOW, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
2704 EndContainer(),
2705 NWidget(WWT_PANEL, COLOUR_GREY),
2706 NWidget(NWID_VERTICAL, NC_EQUALSIZE), SetPIP(7, 3, 0),
2707 NWidget(NWID_HORIZONTAL), SetPIP(10, 0, 5),
2708 NWidget(WWT_PUSHARROWBTN, COLOUR_YELLOW, WID_CC_RATE_DOWN), SetDataTip(AWV_DECREASE, STR_CURRENCY_DECREASE_EXCHANGE_RATE_TOOLTIP),
2709 NWidget(WWT_PUSHARROWBTN, COLOUR_YELLOW, WID_CC_RATE_UP), SetDataTip(AWV_INCREASE, STR_CURRENCY_INCREASE_EXCHANGE_RATE_TOOLTIP),
2710 NWidget(NWID_SPACER), SetMinimalSize(5, 0),
2711 NWidget(WWT_TEXT, COLOUR_BLUE, WID_CC_RATE), SetDataTip(STR_CURRENCY_EXCHANGE_RATE, STR_CURRENCY_SET_EXCHANGE_RATE_TOOLTIP), SetFill(1, 0),
2712 EndContainer(),
2713 NWidget(NWID_HORIZONTAL), SetPIP(10, 0, 5),
2714 NWidget(WWT_PUSHBTN, COLOUR_DARK_BLUE, WID_CC_SEPARATOR_EDIT), SetDataTip(0x0, STR_CURRENCY_SET_CUSTOM_CURRENCY_SEPARATOR_TOOLTIP), SetFill(0, 1),
2715 NWidget(NWID_SPACER), SetMinimalSize(5, 0),
2716 NWidget(WWT_TEXT, COLOUR_BLUE, WID_CC_SEPARATOR), SetDataTip(STR_CURRENCY_SEPARATOR, STR_CURRENCY_SET_CUSTOM_CURRENCY_SEPARATOR_TOOLTIP), SetFill(1, 0),
2717 EndContainer(),
2718 NWidget(NWID_HORIZONTAL), SetPIP(10, 0, 5),
2719 NWidget(WWT_PUSHBTN, COLOUR_DARK_BLUE, WID_CC_PREFIX_EDIT), SetDataTip(0x0, STR_CURRENCY_SET_CUSTOM_CURRENCY_PREFIX_TOOLTIP), SetFill(0, 1),
2720 NWidget(NWID_SPACER), SetMinimalSize(5, 0),
2721 NWidget(WWT_TEXT, COLOUR_BLUE, WID_CC_PREFIX), SetDataTip(STR_CURRENCY_PREFIX, STR_CURRENCY_SET_CUSTOM_CURRENCY_PREFIX_TOOLTIP), SetFill(1, 0),
2722 EndContainer(),
2723 NWidget(NWID_HORIZONTAL), SetPIP(10, 0, 5),
2724 NWidget(WWT_PUSHBTN, COLOUR_DARK_BLUE, WID_CC_SUFFIX_EDIT), SetDataTip(0x0, STR_CURRENCY_SET_CUSTOM_CURRENCY_SUFFIX_TOOLTIP), SetFill(0, 1),
2725 NWidget(NWID_SPACER), SetMinimalSize(5, 0),
2726 NWidget(WWT_TEXT, COLOUR_BLUE, WID_CC_SUFFIX), SetDataTip(STR_CURRENCY_SUFFIX, STR_CURRENCY_SET_CUSTOM_CURRENCY_SUFFIX_TOOLTIP), SetFill(1, 0),
2727 EndContainer(),
2728 NWidget(NWID_HORIZONTAL), SetPIP(10, 0, 5),
2729 NWidget(WWT_PUSHARROWBTN, COLOUR_YELLOW, WID_CC_YEAR_DOWN), SetDataTip(AWV_DECREASE, STR_CURRENCY_DECREASE_CUSTOM_CURRENCY_TO_EURO_TOOLTIP),
2730 NWidget(WWT_PUSHARROWBTN, COLOUR_YELLOW, WID_CC_YEAR_UP), SetDataTip(AWV_INCREASE, STR_CURRENCY_INCREASE_CUSTOM_CURRENCY_TO_EURO_TOOLTIP),
2731 NWidget(NWID_SPACER), SetMinimalSize(5, 0),
2732 NWidget(WWT_TEXT, COLOUR_BLUE, WID_CC_YEAR), SetDataTip(STR_JUST_STRING, STR_CURRENCY_SET_CUSTOM_CURRENCY_TO_EURO_TOOLTIP), SetFill(1, 0),
2733 EndContainer(),
2734 EndContainer(),
2735 NWidget(WWT_LABEL, COLOUR_BLUE, WID_CC_PREVIEW),
2736 SetDataTip(STR_CURRENCY_PREVIEW, STR_CURRENCY_CUSTOM_CURRENCY_PREVIEW_TOOLTIP), SetPadding(15, 1, 18, 2),
2737 EndContainer(),
2740 static const WindowDesc _cust_currency_desc(
2741 WDP_CENTER, 0, 0,
2742 WC_CUSTOM_CURRENCY, WC_NONE,
2744 _nested_cust_currency_widgets, lengthof(_nested_cust_currency_widgets)
2747 /** Open custom currency window. */
2748 static void ShowCustCurrency()
2750 DeleteWindowById(WC_CUSTOM_CURRENCY, 0);
2751 new CustomCurrencyWindow(&_cust_currency_desc);