Silence uninitialised variable warnings in SSE blitters
[openttd/fttd.git] / src / fios_gui.cpp
blob2dd75d52fb0405a6ab11c5b930f16e1fa7255da0
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 fios_gui.cpp GUIs for loading/saving games, scenarios, heightmaps, ... */
12 #include "stdafx.h"
13 #include "saveload/saveload.h"
14 #include "error.h"
15 #include "gui.h"
16 #include "gfx_func.h"
17 #include "command_func.h"
18 #include "network/network.h"
19 #include "network/network_content.h"
20 #include "strings_func.h"
21 #include "fileio_func.h"
22 #include "fios.h"
23 #include "window_func.h"
24 #include "tilehighlight_func.h"
25 #include "querystring_gui.h"
26 #include "engine_func.h"
27 #include "landscape_type.h"
28 #include "date_func.h"
29 #include "core/geometry_func.hpp"
30 #include "gamelog.h"
32 #include "widgets/fios_widget.h"
34 #include "table/sprites.h"
35 #include "table/strings.h"
37 SaveLoadDialogMode _saveload_mode;
38 LoadCheckData _load_check_data; ///< Data loaded from save during SL_LOAD_CHECK.
40 static bool _fios_path_changed;
41 static bool _savegame_sort_dirty;
44 /**
45 * Reset read data.
47 void LoadCheckData::Clear()
49 this->checkable = false;
50 this->error.str = INVALID_STRING_ID;
51 this->error.data = NULL;
53 this->map_size_x = this->map_size_y = 256; // Default for old savegames which do not store mapsize.
54 this->current_date = 0;
55 memset(&this->settings, 0, sizeof(this->settings));
57 const CompanyPropertiesMap::iterator end = this->companies.End();
58 for (CompanyPropertiesMap::iterator it = this->companies.Begin(); it != end; it++) {
59 delete it->second;
61 companies.Clear();
63 this->gamelog.clear();
65 ClearGRFConfigList(&this->grfconfig);
68 /** Load game/scenario with optional content download */
69 static const NWidgetPart _nested_load_dialog_widgets[] = {
70 NWidget(NWID_HORIZONTAL),
71 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
72 NWidget(WWT_CAPTION, COLOUR_GREY, WID_SL_CAPTION),
73 NWidget(WWT_DEFSIZEBOX, COLOUR_GREY),
74 EndContainer(),
75 NWidget(WWT_PANEL, COLOUR_GREY, WID_SL_BACKGROUND), SetFill(1, 0), SetResize(1, 0), EndContainer(),
76 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
77 NWidget(NWID_VERTICAL),
78 NWidget(NWID_HORIZONTAL),
79 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
80 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SL_SORT_BYNAME), SetDataTip(STR_SORT_BY_CAPTION_NAME, STR_TOOLTIP_SORT_ORDER), SetFill(1, 0), SetResize(1, 0),
81 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SL_SORT_BYDATE), SetDataTip(STR_SORT_BY_CAPTION_DATE, STR_TOOLTIP_SORT_ORDER), SetFill(1, 0), SetResize(1, 0),
82 EndContainer(),
83 NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_SL_HOME_BUTTON), SetMinimalSize(12, 12), SetDataTip(SPR_HOUSE_ICON, STR_SAVELOAD_HOME_BUTTON),
84 EndContainer(),
85 NWidget(WWT_PANEL, COLOUR_GREY, WID_SL_FILE_BACKGROUND),
86 NWidget(NWID_HORIZONTAL),
87 NWidget(WWT_INSET, COLOUR_GREY, WID_SL_DRIVES_DIRECTORIES_LIST), SetFill(1, 1), SetPadding(2, 1, 2, 2),
88 SetDataTip(0x0, STR_SAVELOAD_LIST_TOOLTIP), SetResize(1, 10), SetScrollbar(WID_SL_SCROLLBAR), EndContainer(),
89 NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_SL_SCROLLBAR),
90 EndContainer(),
91 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_SL_CONTENT_DOWNLOAD_SEL),
92 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SL_CONTENT_DOWNLOAD), SetResize(1, 0),
93 SetDataTip(STR_INTRO_ONLINE_CONTENT, STR_INTRO_TOOLTIP_ONLINE_CONTENT),
94 EndContainer(),
95 EndContainer(),
96 EndContainer(),
97 NWidget(WWT_PANEL, COLOUR_GREY),
98 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_SL_DETAILS), SetResize(1, 1), SetFill(1, 1),
99 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SL_MISSING_NEWGRFS), SetDataTip(STR_NEWGRF_SETTINGS_FIND_MISSING_CONTENT_BUTTON, STR_NEWGRF_SETTINGS_FIND_MISSING_CONTENT_TOOLTIP), SetFill(1, 0), SetResize(1, 0),
100 NWidget(NWID_HORIZONTAL),
101 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
102 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SL_NEWGRF_INFO), SetDataTip(STR_INTRO_NEWGRF_SETTINGS, STR_NULL), SetFill(1, 0), SetResize(1, 0),
103 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SL_LOAD_BUTTON), SetDataTip(STR_SAVELOAD_LOAD_BUTTON, STR_SAVELOAD_LOAD_TOOLTIP), SetFill(1, 0), SetResize(1, 0),
104 EndContainer(),
105 NWidget(WWT_RESIZEBOX, COLOUR_GREY),
106 EndContainer(),
107 EndContainer(),
108 EndContainer(),
111 /** Load heightmap with content download */
112 static const NWidgetPart _nested_load_heightmap_dialog_widgets[] = {
113 NWidget(NWID_HORIZONTAL),
114 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
115 NWidget(WWT_CAPTION, COLOUR_GREY, WID_SL_CAPTION),
116 NWidget(WWT_DEFSIZEBOX, COLOUR_GREY),
117 EndContainer(),
118 NWidget(WWT_PANEL, COLOUR_GREY, WID_SL_BACKGROUND), SetFill(1, 0), SetResize(1, 0), EndContainer(),
119 NWidget(NWID_VERTICAL),
120 NWidget(NWID_HORIZONTAL),
121 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
122 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SL_SORT_BYNAME), SetDataTip(STR_SORT_BY_CAPTION_NAME, STR_TOOLTIP_SORT_ORDER), SetFill(1, 0), SetResize(1, 0),
123 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SL_SORT_BYDATE), SetDataTip(STR_SORT_BY_CAPTION_DATE, STR_TOOLTIP_SORT_ORDER), SetFill(1, 0), SetResize(1, 0),
124 EndContainer(),
125 NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_SL_HOME_BUTTON), SetMinimalSize(12, 12), SetDataTip(SPR_HOUSE_ICON, STR_SAVELOAD_HOME_BUTTON),
126 EndContainer(),
127 NWidget(WWT_PANEL, COLOUR_GREY, WID_SL_FILE_BACKGROUND),
128 NWidget(NWID_HORIZONTAL),
129 NWidget(WWT_INSET, COLOUR_GREY, WID_SL_DRIVES_DIRECTORIES_LIST), SetFill(1, 1), SetPadding(2, 1, 2, 2),
130 SetDataTip(0x0, STR_SAVELOAD_LIST_TOOLTIP), SetResize(1, 10), SetScrollbar(WID_SL_SCROLLBAR), EndContainer(),
131 NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_SL_SCROLLBAR),
132 EndContainer(),
133 NWidget(NWID_HORIZONTAL),
134 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SL_CONTENT_DOWNLOAD), SetResize(1, 0),
135 SetDataTip(STR_INTRO_ONLINE_CONTENT, STR_INTRO_TOOLTIP_ONLINE_CONTENT),
136 NWidget(WWT_RESIZEBOX, COLOUR_GREY),
137 EndContainer(),
138 EndContainer(),
139 EndContainer(),
142 /** Save game/scenario */
143 static const NWidgetPart _nested_save_dialog_widgets[] = {
144 NWidget(NWID_HORIZONTAL),
145 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
146 NWidget(WWT_CAPTION, COLOUR_GREY, WID_SL_CAPTION),
147 NWidget(WWT_DEFSIZEBOX, COLOUR_GREY),
148 EndContainer(),
149 NWidget(WWT_PANEL, COLOUR_GREY, WID_SL_BACKGROUND), SetFill(1, 0), SetResize(1, 0), EndContainer(),
150 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
151 NWidget(NWID_VERTICAL),
152 NWidget(NWID_HORIZONTAL),
153 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
154 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SL_SORT_BYNAME), SetDataTip(STR_SORT_BY_CAPTION_NAME, STR_TOOLTIP_SORT_ORDER), SetFill(1, 0), SetResize(1, 0),
155 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SL_SORT_BYDATE), SetDataTip(STR_SORT_BY_CAPTION_DATE, STR_TOOLTIP_SORT_ORDER), SetFill(1, 0), SetResize(1, 0),
156 EndContainer(),
157 NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_SL_HOME_BUTTON), SetMinimalSize(12, 12), SetDataTip(SPR_HOUSE_ICON, STR_SAVELOAD_HOME_BUTTON),
158 EndContainer(),
159 NWidget(WWT_PANEL, COLOUR_GREY, WID_SL_FILE_BACKGROUND),
160 NWidget(NWID_HORIZONTAL),
161 NWidget(WWT_INSET, COLOUR_GREY, WID_SL_DRIVES_DIRECTORIES_LIST), SetPadding(2, 1, 0, 2),
162 SetDataTip(0x0, STR_SAVELOAD_LIST_TOOLTIP), SetResize(1, 10), SetScrollbar(WID_SL_SCROLLBAR), EndContainer(),
163 NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_SL_SCROLLBAR),
164 EndContainer(),
165 NWidget(WWT_EDITBOX, COLOUR_GREY, WID_SL_SAVE_OSK_TITLE), SetPadding(3, 2, 2, 2), SetFill(1, 0), SetResize(1, 0),
166 SetDataTip(STR_SAVELOAD_OSKTITLE, STR_SAVELOAD_EDITBOX_TOOLTIP),
167 EndContainer(),
168 NWidget(NWID_HORIZONTAL),
169 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SL_DELETE_SELECTION), SetDataTip(STR_SAVELOAD_DELETE_BUTTON, STR_SAVELOAD_DELETE_TOOLTIP), SetFill(1, 0), SetResize(1, 0),
170 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SL_SAVE_GAME), SetDataTip(STR_SAVELOAD_SAVE_BUTTON, STR_SAVELOAD_SAVE_TOOLTIP), SetFill(1, 0), SetResize(1, 0),
171 EndContainer(),
172 EndContainer(),
173 NWidget(WWT_PANEL, COLOUR_GREY),
174 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_SL_DETAILS), SetResize(1, 1), SetFill(1, 1),
175 NWidget(NWID_HORIZONTAL),
176 NWidget(NWID_SPACER), SetResize(1, 0), SetFill(1, 1),
177 NWidget(WWT_RESIZEBOX, COLOUR_GREY),
178 EndContainer(),
179 EndContainer(),
180 EndContainer(),
183 /** Colours for fios types, indexed by #FiosType. */
184 const TextColour _fios_colours[] = {
185 TC_LIGHT_BLUE, TC_DARK_GREEN, TC_DARK_GREEN, TC_ORANGE, TC_LIGHT_BROWN,
186 TC_ORANGE, TC_LIGHT_BROWN, TC_ORANGE, TC_ORANGE, TC_YELLOW
189 void BuildFileList()
191 _fios_path_changed = true;
192 FiosFreeSavegameList();
194 switch (_saveload_mode) {
195 case SLD_LOAD_SCENARIO:
196 case SLD_SAVE_SCENARIO:
197 FiosGetScenarioList(_saveload_mode); break;
198 case SLD_SAVE_HEIGHTMAP:
199 case SLD_LOAD_HEIGHTMAP:
200 FiosGetHeightmapList(_saveload_mode); break;
202 default: FiosGetSavegameList(_saveload_mode); break;
205 /* Invalidate saveload window */
206 InvalidateWindowData(WC_SAVELOAD, 0, 2, true);
209 static void MakeSortedSaveGameList()
211 uint sort_start = 0;
212 uint sort_end = 0;
214 /* Directories are always above the files (FIOS_TYPE_DIR)
215 * Drives (A:\ (windows only) are always under the files (FIOS_TYPE_DRIVE)
216 * Only sort savegames/scenarios, not directories
218 for (const FiosItem *item = _fios_items.Begin(); item != _fios_items.End(); item++) {
219 switch (item->type) {
220 case FIOS_TYPE_DIR: sort_start++; break;
221 case FIOS_TYPE_PARENT: sort_start++; break;
222 case FIOS_TYPE_DRIVE: sort_end++; break;
223 default: break;
227 uint s_amount = _fios_items.Length() - sort_start - sort_end;
228 QSortT(_fios_items.Get(sort_start), s_amount, CompareFiosItems);
231 struct SaveLoadWindow : public Window {
232 private:
233 QueryString filename_editbox; ///< Filename editbox.
234 FiosItem o_dir;
235 const FiosItem *selected;
236 Scrollbar *vscroll;
237 public:
239 /** Generate a default save filename. */
240 void GenerateFileName()
242 GenerateDefaultSaveName(this->filename_editbox.text.buf, &this->filename_editbox.text.buf[this->filename_editbox.text.max_bytes - 1]);
243 this->filename_editbox.text.UpdateSize();
246 SaveLoadWindow(WindowDesc *desc, SaveLoadDialogMode mode) : Window(desc), filename_editbox(64)
248 static const StringID saveload_captions[] = {
249 STR_SAVELOAD_LOAD_CAPTION,
250 STR_SAVELOAD_LOAD_SCENARIO,
251 STR_SAVELOAD_SAVE_CAPTION,
252 STR_SAVELOAD_SAVE_SCENARIO,
253 STR_SAVELOAD_LOAD_HEIGHTMAP,
254 STR_SAVELOAD_SAVE_HEIGHTMAP,
256 assert((uint)mode < lengthof(saveload_captions));
258 /* Use an array to define what will be the current file type being handled
259 * by current file mode */
260 switch (mode) {
261 case SLD_SAVE_GAME: this->GenerateFileName(); break;
262 case SLD_SAVE_HEIGHTMAP:
263 case SLD_SAVE_SCENARIO: this->filename_editbox.text.Assign("UNNAMED"); break;
264 default: break;
267 this->querystrings[WID_SL_SAVE_OSK_TITLE] = &this->filename_editbox;
268 this->filename_editbox.ok_button = WID_SL_SAVE_GAME;
270 this->CreateNestedTree(true);
271 if (mode == SLD_LOAD_GAME) this->GetWidget<NWidgetStacked>(WID_SL_CONTENT_DOWNLOAD_SEL)->SetDisplayedPlane(SZSP_HORIZONTAL);
272 this->GetWidget<NWidgetCore>(WID_SL_CAPTION)->widget_data = saveload_captions[mode];
273 this->vscroll = this->GetScrollbar(WID_SL_SCROLLBAR);
275 this->FinishInitNested(0);
277 this->LowerWidget(WID_SL_DRIVES_DIRECTORIES_LIST);
279 /* pause is only used in single-player, non-editor mode, non-menu mode. It
280 * will be unpaused in the WE_DESTROY event handler. */
281 if (_game_mode != GM_MENU && !_networking && _game_mode != GM_EDITOR) {
282 DoCommandP(0, PM_PAUSED_SAVELOAD, 1, CMD_PAUSE);
284 SetObjectToPlace(SPR_CURSOR_ZZZ, PAL_NONE, HT_NONE, WC_MAIN_WINDOW, 0);
286 this->OnInvalidateData(0);
288 ResetObjectToPlace();
290 o_dir.type = FIOS_TYPE_DIRECT;
291 switch (_saveload_mode) {
292 case SLD_SAVE_GAME:
293 case SLD_LOAD_GAME:
294 FioGetDirectory(o_dir.name, lengthof(o_dir.name), SAVE_DIR);
295 break;
297 case SLD_SAVE_SCENARIO:
298 case SLD_LOAD_SCENARIO:
299 FioGetDirectory(o_dir.name, lengthof(o_dir.name), SCENARIO_DIR);
300 break;
302 case SLD_SAVE_HEIGHTMAP:
303 case SLD_LOAD_HEIGHTMAP:
304 FioGetDirectory(o_dir.name, lengthof(o_dir.name), HEIGHTMAP_DIR);
305 break;
307 default:
308 strecpy(o_dir.name, _personal_dir, lastof(o_dir.name));
311 /* Focus the edit box by default in the save windows */
312 if (_saveload_mode == SLD_SAVE_GAME || _saveload_mode == SLD_SAVE_SCENARIO || _saveload_mode == SLD_SAVE_HEIGHTMAP) {
313 this->SetFocusedWidget(WID_SL_SAVE_OSK_TITLE);
317 virtual ~SaveLoadWindow()
319 /* pause is only used in single-player, non-editor mode, non menu mode */
320 if (!_networking && _game_mode != GM_EDITOR && _game_mode != GM_MENU) {
321 DoCommandP(0, PM_PAUSED_SAVELOAD, 0, CMD_PAUSE);
323 FiosFreeSavegameList();
326 virtual void DrawWidget(const Rect &r, int widget) const
328 switch (widget) {
329 case WID_SL_SORT_BYNAME:
330 case WID_SL_SORT_BYDATE:
331 if (((_savegame_sort_order & SORT_BY_NAME) != 0) == (widget == WID_SL_SORT_BYNAME)) {
332 this->DrawSortButtonState(widget, _savegame_sort_order & SORT_DESCENDING ? SBS_DOWN : SBS_UP);
334 break;
336 case WID_SL_BACKGROUND: {
337 static const char *path = NULL;
338 static StringID str = STR_ERROR_UNABLE_TO_READ_DRIVE;
339 static uint64 tot = 0;
341 if (_fios_path_changed) {
342 str = FiosGetDescText(&path, &tot);
343 _fios_path_changed = false;
346 if (str != STR_ERROR_UNABLE_TO_READ_DRIVE) SetDParam(0, tot);
347 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + FONT_HEIGHT_NORMAL + WD_FRAMERECT_TOP, str);
348 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + WD_FRAMERECT_TOP, path, TC_BLACK);
349 break;
352 case WID_SL_DRIVES_DIRECTORIES_LIST: {
353 GfxFillRect(r.left + 1, r.top + 1, r.right, r.bottom, PC_BLACK);
355 uint y = r.top + WD_FRAMERECT_TOP;
356 for (uint pos = this->vscroll->GetPosition(); pos < _fios_items.Length(); pos++) {
357 const FiosItem *item = _fios_items.Get(pos);
359 if (item == this->selected) {
360 GfxFillRect(r.left + 1, y, r.right, y + this->resize.step_height, PC_DARK_BLUE);
362 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, item->title, _fios_colours[item->type]);
363 y += this->resize.step_height;
364 if (y >= this->vscroll->GetCapacity() * this->resize.step_height + r.top + WD_FRAMERECT_TOP) break;
366 break;
369 case WID_SL_DETAILS: {
370 GfxFillRect(r.left + WD_FRAMERECT_LEFT, r.top + WD_FRAMERECT_TOP,
371 r.right - WD_FRAMERECT_RIGHT, r.top + FONT_HEIGHT_NORMAL * 2 + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM, PC_GREY);
372 DrawString(r.left, r.right, r.top + FONT_HEIGHT_NORMAL / 2 + WD_FRAMERECT_TOP, STR_SAVELOAD_DETAIL_CAPTION, TC_FROMSTRING, SA_HOR_CENTER);
374 if (this->selected == NULL) break;
376 uint y = r.top + FONT_HEIGHT_NORMAL * 2 + WD_PAR_VSEP_NORMAL + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
377 uint y_max = r.bottom - FONT_HEIGHT_NORMAL - WD_FRAMERECT_BOTTOM;
379 if (y > y_max) break;
380 if (!_load_check_data.checkable) {
381 /* Old savegame, no information available */
382 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_SAVELOAD_DETAIL_NOT_AVAILABLE);
383 y += FONT_HEIGHT_NORMAL;
384 } else if (_load_check_data.error.str != INVALID_STRING_ID) {
385 /* Incompatible / broken savegame */
386 SetDParamStr(0, _load_check_data.error.data);
387 y = DrawStringMultiLine(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT,
388 y, r.bottom - WD_FRAMERECT_BOTTOM, _load_check_data.error.str, TC_RED);
389 } else {
390 /* Mapsize */
391 SetDParam(0, _load_check_data.map_size_x);
392 SetDParam(1, _load_check_data.map_size_y);
393 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_SERVER_LIST_MAP_SIZE);
394 y += FONT_HEIGHT_NORMAL;
395 if (y > y_max) break;
397 /* Climate */
398 byte landscape = _load_check_data.settings.game_creation.landscape;
399 if (landscape < NUM_LANDSCAPE) {
400 SetDParam(0, STR_CHEAT_SWITCH_CLIMATE_TEMPERATE_LANDSCAPE + landscape);
401 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_SERVER_LIST_LANDSCAPE);
402 y += FONT_HEIGHT_NORMAL;
405 y += WD_PAR_VSEP_NORMAL;
406 if (y > y_max) break;
408 /* Start date (if available) */
409 if (_load_check_data.settings.game_creation.starting_year != 0) {
410 SetDParam(0, ConvertYMDToDate(_load_check_data.settings.game_creation.starting_year, 0, 1));
411 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_SERVER_LIST_START_DATE);
412 y += FONT_HEIGHT_NORMAL;
414 if (y > y_max) break;
416 /* Hide current date for scenarios */
417 if (_saveload_mode != SLD_LOAD_SCENARIO && _saveload_mode != SLD_SAVE_SCENARIO) {
418 /* Current date */
419 SetDParam(0, _load_check_data.current_date);
420 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_SERVER_LIST_CURRENT_DATE);
421 y += FONT_HEIGHT_NORMAL;
424 /* Hide the NewGRF stuff when saving. We also hide the button. */
425 if (_saveload_mode == SLD_LOAD_GAME || _saveload_mode == SLD_LOAD_SCENARIO) {
426 y += WD_PAR_VSEP_NORMAL;
427 if (y > y_max) break;
429 /* NewGrf compatibility */
430 SetDParam(0, _load_check_data.grfconfig == NULL ? STR_NEWGRF_LIST_NONE :
431 STR_NEWGRF_LIST_ALL_FOUND + _load_check_data.grf_compatibility);
432 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_SAVELOAD_DETAIL_GRFSTATUS);
433 y += FONT_HEIGHT_NORMAL;
435 if (y > y_max) break;
437 /* Hide the company stuff for scenarios */
438 if (_saveload_mode != SLD_LOAD_SCENARIO && _saveload_mode != SLD_SAVE_SCENARIO) {
439 y += FONT_HEIGHT_NORMAL;
440 if (y > y_max) break;
442 /* Companies / AIs */
443 CompanyPropertiesMap::const_iterator end = _load_check_data.companies.End();
444 for (CompanyPropertiesMap::const_iterator it = _load_check_data.companies.Begin(); it != end; it++) {
445 SetDParam(0, it->first + 1);
446 const CompanyProperties &c = *it->second;
447 if (c.name != NULL) {
448 SetDParam(1, STR_JUST_RAW_STRING);
449 SetDParamStr(2, c.name);
450 } else {
451 SetDParam(1, c.name_1);
452 SetDParam(2, c.name_2);
454 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_SAVELOAD_DETAIL_COMPANY_INDEX);
455 y += FONT_HEIGHT_NORMAL;
456 if (y > y_max) break;
460 break;
465 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
467 switch (widget) {
468 case WID_SL_BACKGROUND:
469 size->height = 2 * FONT_HEIGHT_NORMAL + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
470 break;
472 case WID_SL_DRIVES_DIRECTORIES_LIST:
473 resize->height = FONT_HEIGHT_NORMAL;
474 size->height = resize->height * 10 + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
475 break;
476 case WID_SL_SORT_BYNAME:
477 case WID_SL_SORT_BYDATE: {
478 Dimension d = GetStringBoundingBox(this->GetWidget<NWidgetCore>(widget)->widget_data);
479 d.width += padding.width + WD_SORTBUTTON_ARROW_WIDTH * 2; // Doubled since the string is centred and it also looks better.
480 d.height += padding.height;
481 *size = maxdim(*size, d);
482 break;
487 virtual void OnPaint()
489 if (_savegame_sort_dirty) {
490 _savegame_sort_dirty = false;
491 MakeSortedSaveGameList();
494 this->vscroll->SetCount(_fios_items.Length());
495 this->DrawWidgets();
498 virtual void OnClick(Point pt, int widget, int click_count)
500 switch (widget) {
501 case WID_SL_SORT_BYNAME: // Sort save names by name
502 _savegame_sort_order = (_savegame_sort_order == SORT_BY_NAME) ?
503 SORT_BY_NAME | SORT_DESCENDING : SORT_BY_NAME;
504 _savegame_sort_dirty = true;
505 this->SetDirty();
506 break;
508 case WID_SL_SORT_BYDATE: // Sort save names by date
509 _savegame_sort_order = (_savegame_sort_order == SORT_BY_DATE) ?
510 SORT_BY_DATE | SORT_DESCENDING : SORT_BY_DATE;
511 _savegame_sort_dirty = true;
512 this->SetDirty();
513 break;
515 case WID_SL_HOME_BUTTON: // OpenTTD 'button', jumps to OpenTTD directory
516 FiosBrowseTo(&o_dir);
517 this->InvalidateData();
518 break;
520 case WID_SL_LOAD_BUTTON:
521 if (this->selected != NULL && !_load_check_data.HasErrors() && (_load_check_data.grf_compatibility != GLC_NOT_FOUND || _settings_client.gui.UserIsAllowedToChangeNewGRFs())) {
522 _switch_mode = (_game_mode == GM_EDITOR) ? SM_LOAD_SCENARIO : SM_LOAD_GAME;
524 const char *name = FiosBrowseTo(this->selected);
525 SetFiosType(this->selected->type);
527 strecpy(_file_to_saveload.name, name, lastof(_file_to_saveload.name));
528 strecpy(_file_to_saveload.title, this->selected->title, lastof(_file_to_saveload.title));
529 ClearErrorMessages();
530 delete this;
532 break;
534 case WID_SL_NEWGRF_INFO:
535 if (_load_check_data.HasNewGrfs()) {
536 ShowNewGRFSettings(false, false, false, &_load_check_data.grfconfig);
538 break;
540 case WID_SL_MISSING_NEWGRFS:
541 if (!_network_available) {
542 ShowErrorMessage(STR_NETWORK_ERROR_NOTAVAILABLE, INVALID_STRING_ID, WL_ERROR);
543 } else {
544 #if defined(ENABLE_NETWORK)
545 ShowMissingContentWindow(_load_check_data.grfconfig);
546 #endif
548 break;
550 case WID_SL_DRIVES_DIRECTORIES_LIST: { // Click the listbox
551 int y = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_SL_DRIVES_DIRECTORIES_LIST, WD_FRAMERECT_TOP);
552 if (y == INT_MAX) return;
554 const FiosItem *file = _fios_items.Get(y);
556 const char *name = FiosBrowseTo(file);
557 if (name != NULL) {
558 if (click_count == 1) {
559 if (this->selected != file) {
560 this->selected = file;
561 _load_check_data.Clear();
563 if (file->type == FIOS_TYPE_FILE || file->type == FIOS_TYPE_SCENARIO) {
564 LoadGame(name, SL_LOAD_CHECK, NO_DIRECTORY);
567 this->InvalidateData(1);
569 if (_saveload_mode == SLD_SAVE_GAME || _saveload_mode == SLD_SAVE_SCENARIO || _saveload_mode == SLD_SAVE_HEIGHTMAP) {
570 /* Copy clicked name to editbox */
571 this->filename_editbox.text.Assign(file->title);
572 this->SetWidgetDirty(WID_SL_SAVE_OSK_TITLE);
574 } else if (!_load_check_data.HasErrors()) {
575 this->selected = file;
576 if (_saveload_mode == SLD_LOAD_GAME || _saveload_mode == SLD_LOAD_SCENARIO) {
577 this->OnClick(pt, WID_SL_LOAD_BUTTON, 1);
578 } else if (_saveload_mode == SLD_LOAD_HEIGHTMAP) {
579 SetFiosType(file->type);
580 strecpy(_file_to_saveload.name, name, lastof(_file_to_saveload.name));
581 strecpy(_file_to_saveload.title, file->title, lastof(_file_to_saveload.title));
583 delete this;
584 ShowHeightmapLoad();
587 } else {
588 /* Changed directory, need refresh. */
589 this->InvalidateData();
591 break;
594 case WID_SL_CONTENT_DOWNLOAD:
595 if (!_network_available) {
596 ShowErrorMessage(STR_NETWORK_ERROR_NOTAVAILABLE, INVALID_STRING_ID, WL_ERROR);
597 } else {
598 #if defined(ENABLE_NETWORK)
599 switch (_saveload_mode) {
600 default: NOT_REACHED();
601 case SLD_LOAD_SCENARIO: ShowNetworkContentListWindow(NULL, CONTENT_TYPE_SCENARIO); break;
602 case SLD_LOAD_HEIGHTMAP: ShowNetworkContentListWindow(NULL, CONTENT_TYPE_HEIGHTMAP); break;
604 #endif
606 break;
608 case WID_SL_DELETE_SELECTION: // Delete
609 break;
611 case WID_SL_SAVE_GAME: // Save game
612 /* Note, this is also called via the OSK; and we need to lower the button. */
613 this->HandleButtonClick(WID_SL_SAVE_GAME);
614 break;
618 virtual EventState OnKeyPress(WChar key, uint16 keycode)
620 if (keycode == WKC_ESC) {
621 delete this;
622 return ES_HANDLED;
625 return ES_NOT_HANDLED;
628 virtual void OnTimeout()
630 /* This test protects against using widgets 11 and 12 which are only available
631 * in those saveload modes. */
632 if (!(_saveload_mode == SLD_SAVE_GAME || _saveload_mode == SLD_SAVE_SCENARIO || _saveload_mode == SLD_SAVE_HEIGHTMAP)) return;
634 if (this->IsWidgetLowered(WID_SL_DELETE_SELECTION)) { // Delete button clicked
635 if (!FiosDelete(this->filename_editbox.text.buf)) {
636 ShowErrorMessage(STR_ERROR_UNABLE_TO_DELETE_FILE, INVALID_STRING_ID, WL_ERROR);
637 } else {
638 this->InvalidateData();
639 /* Reset file name to current date on successful delete */
640 if (_saveload_mode == SLD_SAVE_GAME) GenerateFileName();
642 } else if (this->IsWidgetLowered(WID_SL_SAVE_GAME)) { // Save button clicked
643 if (_saveload_mode == SLD_SAVE_GAME || _saveload_mode == SLD_SAVE_SCENARIO) {
644 _switch_mode = SM_SAVE_GAME;
645 FiosMakeSavegameName(_file_to_saveload.name, this->filename_editbox.text.buf, sizeof(_file_to_saveload.name));
646 } else {
647 _switch_mode = SM_SAVE_HEIGHTMAP;
648 FiosMakeHeightmapName(_file_to_saveload.name, this->filename_editbox.text.buf, sizeof(_file_to_saveload.name));
651 /* In the editor set up the vehicle engines correctly (date might have changed) */
652 if (_game_mode == GM_EDITOR) StartupEngines();
656 virtual void OnResize()
658 this->vscroll->SetCapacityFromWidget(this, WID_SL_DRIVES_DIRECTORIES_LIST);
662 * Some data on this window has become invalid.
663 * @param data Information about the changed data.
664 * @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.
666 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
668 switch (data) {
669 case 0:
670 /* Rescan files */
671 this->selected = NULL;
672 _load_check_data.Clear();
673 if (!gui_scope) break;
674 BuildFileList();
675 /* FALL THROUGH */
676 case 1:
677 /* Selection changes */
678 if (!gui_scope) break;
679 if (_saveload_mode == SLD_LOAD_GAME || _saveload_mode == SLD_LOAD_SCENARIO) {
680 this->SetWidgetDisabledState(WID_SL_LOAD_BUTTON,
681 this->selected == NULL || _load_check_data.HasErrors() || !(_load_check_data.grf_compatibility != GLC_NOT_FOUND || _settings_client.gui.UserIsAllowedToChangeNewGRFs()));
682 this->SetWidgetDisabledState(WID_SL_NEWGRF_INFO,
683 !_load_check_data.HasNewGrfs());
684 this->SetWidgetDisabledState(WID_SL_MISSING_NEWGRFS,
685 !_load_check_data.HasNewGrfs() || _load_check_data.grf_compatibility == GLC_ALL_GOOD);
687 break;
688 case 2:
689 /* _fios_items changed */
690 this->vscroll->SetCount(_fios_items.Length());
691 this->selected = NULL;
692 _load_check_data.Clear();
693 break;
698 /** Load game/scenario */
699 static WindowDesc _load_dialog_desc(
700 WDP_CENTER, "load_game", 500, 294,
701 WC_SAVELOAD, WC_NONE,
703 _nested_load_dialog_widgets, lengthof(_nested_load_dialog_widgets)
706 /** Load heightmap */
707 static WindowDesc _load_heightmap_dialog_desc(
708 WDP_CENTER, "load_heightmap", 257, 320,
709 WC_SAVELOAD, WC_NONE,
711 _nested_load_heightmap_dialog_widgets, lengthof(_nested_load_heightmap_dialog_widgets)
714 /** Save game/scenario */
715 static WindowDesc _save_dialog_desc(
716 WDP_CENTER, "save_game", 500, 294,
717 WC_SAVELOAD, WC_NONE,
719 _nested_save_dialog_widgets, lengthof(_nested_save_dialog_widgets)
723 * These values are used to convert the file/operations mode into a corresponding file type.
724 * So each entry, as expressed by the related comment, is based on the enum
726 static const FileType _file_modetotype[] = {
727 FT_SAVEGAME, // used for SLD_LOAD_GAME
728 FT_SCENARIO, // used for SLD_LOAD_SCENARIO
729 FT_SAVEGAME, // used for SLD_SAVE_GAME
730 FT_SCENARIO, // used for SLD_SAVE_SCENARIO
731 FT_HEIGHTMAP, // used for SLD_LOAD_HEIGHTMAP
732 FT_HEIGHTMAP, // used for SLD_SAVE_HEIGHTMAP
736 * Launch save/load dialog in the given mode.
737 * @param mode Save/load mode.
739 void ShowSaveLoadDialog(SaveLoadDialogMode mode)
741 DeleteWindowById(WC_SAVELOAD, 0);
743 WindowDesc *sld;
744 switch (mode) {
745 case SLD_SAVE_GAME:
746 case SLD_SAVE_SCENARIO:
747 case SLD_SAVE_HEIGHTMAP:
748 sld = &_save_dialog_desc; break;
749 case SLD_LOAD_HEIGHTMAP:
750 sld = &_load_heightmap_dialog_desc; break;
751 default:
752 sld = &_load_dialog_desc; break;
755 _saveload_mode = mode;
756 _file_to_saveload.filetype = _file_modetotype[mode];
758 new SaveLoadWindow(sld, mode);
761 void SetFiosType(const byte fiostype)
763 switch (fiostype) {
764 case FIOS_TYPE_FILE:
765 case FIOS_TYPE_SCENARIO:
766 _file_to_saveload.mode = SL_LOAD;
767 break;
769 case FIOS_TYPE_OLDFILE:
770 case FIOS_TYPE_OLD_SCENARIO:
771 _file_to_saveload.mode = SL_OLD_LOAD;
772 break;
774 #ifdef WITH_PNG
775 case FIOS_TYPE_PNG:
776 _file_to_saveload.mode = SL_PNG;
777 break;
778 #endif /* WITH_PNG */
780 case FIOS_TYPE_BMP:
781 _file_to_saveload.mode = SL_BMP;
782 break;
784 default:
785 _file_to_saveload.mode = SL_INVALID;
786 break;