Translations update
[openttd/fttd.git] / src / fios_gui.cpp
blobafaff7e1c45cec451cee582a3a04aac99a86efd2
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 "string.h"
14 #include "saveload/saveload.h"
15 #include "error.h"
16 #include "gui.h"
17 #include "gfx_func.h"
18 #include "command_func.h"
19 #include "network/network.h"
20 #include "network/network_content.h"
21 #include "strings_func.h"
22 #include "fileio_func.h"
23 #include "fios.h"
24 #include "window_func.h"
25 #include "tilehighlight_func.h"
26 #include "querystring_gui.h"
27 #include "engine_func.h"
28 #include "landscape_type.h"
29 #include "date_func.h"
30 #include "core/geometry_func.hpp"
31 #include "gamelog.h"
33 #include "widgets/fios_widget.h"
35 #include "table/sprites.h"
36 #include "table/strings.h"
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, NC_EQUALSIZE),
134 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SL_CONTENT_DOWNLOAD), SetResize(1, 0), SetFill(1, 0),
135 SetDataTip(STR_INTRO_ONLINE_CONTENT, STR_INTRO_TOOLTIP_ONLINE_CONTENT),
136 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SL_LOAD_BUTTON), SetResize(1, 0), SetFill(1, 0),
137 SetDataTip(STR_SAVELOAD_LOAD_BUTTON, STR_SAVELOAD_LOAD_HEIGHTMAP_TOOLTIP),
138 NWidget(WWT_RESIZEBOX, COLOUR_GREY),
139 EndContainer(),
140 EndContainer(),
141 EndContainer(),
144 /** Save game/scenario */
145 static const NWidgetPart _nested_save_dialog_widgets[] = {
146 NWidget(NWID_HORIZONTAL),
147 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
148 NWidget(WWT_CAPTION, COLOUR_GREY, WID_SL_CAPTION),
149 NWidget(WWT_DEFSIZEBOX, COLOUR_GREY),
150 EndContainer(),
151 NWidget(WWT_PANEL, COLOUR_GREY, WID_SL_BACKGROUND), SetFill(1, 0), SetResize(1, 0), EndContainer(),
152 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
153 NWidget(NWID_VERTICAL),
154 NWidget(NWID_HORIZONTAL),
155 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
156 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),
157 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),
158 EndContainer(),
159 NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_SL_HOME_BUTTON), SetMinimalSize(12, 12), SetDataTip(SPR_HOUSE_ICON, STR_SAVELOAD_HOME_BUTTON),
160 EndContainer(),
161 NWidget(WWT_PANEL, COLOUR_GREY, WID_SL_FILE_BACKGROUND),
162 NWidget(NWID_HORIZONTAL),
163 NWidget(WWT_INSET, COLOUR_GREY, WID_SL_DRIVES_DIRECTORIES_LIST), SetPadding(2, 1, 0, 2),
164 SetDataTip(0x0, STR_SAVELOAD_LIST_TOOLTIP), SetResize(1, 10), SetScrollbar(WID_SL_SCROLLBAR), EndContainer(),
165 NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_SL_SCROLLBAR),
166 EndContainer(),
167 NWidget(WWT_EDITBOX, COLOUR_GREY, WID_SL_SAVE_OSK_TITLE), SetPadding(3, 2, 2, 2), SetFill(1, 0), SetResize(1, 0),
168 SetDataTip(STR_SAVELOAD_OSKTITLE, STR_SAVELOAD_EDITBOX_TOOLTIP),
169 EndContainer(),
170 NWidget(NWID_HORIZONTAL),
171 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SL_DELETE_SELECTION), SetDataTip(STR_SAVELOAD_DELETE_BUTTON, STR_SAVELOAD_DELETE_TOOLTIP), SetFill(1, 0), SetResize(1, 0),
172 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SL_SAVE_GAME), SetDataTip(STR_SAVELOAD_SAVE_BUTTON, STR_SAVELOAD_SAVE_TOOLTIP), SetFill(1, 0), SetResize(1, 0),
173 EndContainer(),
174 EndContainer(),
175 NWidget(WWT_PANEL, COLOUR_GREY),
176 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_SL_DETAILS), SetResize(1, 1), SetFill(1, 1),
177 NWidget(NWID_HORIZONTAL),
178 NWidget(NWID_SPACER), SetResize(1, 0), SetFill(1, 1),
179 NWidget(WWT_RESIZEBOX, COLOUR_GREY),
180 EndContainer(),
181 EndContainer(),
182 EndContainer(),
185 /** Text colours of #DetailedFileType fios entries in the window. */
186 static const TextColour _fios_colours[] = {
187 TC_LIGHT_BROWN, // DFT_OLD_GAME_FILE
188 TC_ORANGE, // DFT_GAME_FILE
189 TC_YELLOW, // DFT_HEIGHTMAP_BMP
190 TC_ORANGE, // DFT_HEIGHTMAP_PNG
191 TC_LIGHT_BLUE, // DFT_FIOS_DRIVE
192 TC_DARK_GREEN, // DFT_FIOS_PARENT
193 TC_DARK_GREEN, // DFT_FIOS_DIR
194 TC_ORANGE, // DFT_FIOS_DIRECT
199 * Sort the collected list save games prior to displaying it in the save/load gui.
200 * @param [inout] file_list List of save game files found in the directory.
202 static void SortSaveGameList(FileList &file_list)
204 uint sort_start = 0;
205 uint sort_end = 0;
207 /* Directories are always above the files (FIOS_TYPE_DIR)
208 * Drives (A:\ (windows only) are always under the files (FIOS_TYPE_DRIVE)
209 * Only sort savegames/scenarios, not directories
211 for (const FiosItem *item = file_list.Begin(); item != file_list.End(); item++) {
212 switch (item->type) {
213 case FIOS_TYPE_DIR: sort_start++; break;
214 case FIOS_TYPE_PARENT: sort_start++; break;
215 case FIOS_TYPE_DRIVE: sort_end++; break;
216 default: break;
220 uint s_amount = file_list.Length() - sort_start - sort_end;
221 QSortT(file_list.Get(sort_start), s_amount, CompareFiosItems);
224 struct SaveLoadWindow : public Window {
225 private:
226 QueryStringN<64> filename_editbox; ///< Filename editbox.
227 AbstractFileType abstract_filetype; /// Type of file to select.
228 SaveLoadOperation fop; ///< File operation to perform.
229 FileList fios_items; ///< Save game list.
230 FiosItem o_dir;
231 const FiosItem *selected; ///< Selected game in #fios_items, or \c NULL.
232 Scrollbar *vscroll;
233 public:
235 /** Generate a default save filename. */
236 void GenerateFileName()
238 GenerateDefaultSaveName (&this->filename_editbox);
239 this->filename_editbox.UpdateSize();
242 SaveLoadWindow (const WindowDesc *desc, AbstractFileType abstract_filetype, SaveLoadOperation fop)
243 : Window (desc), filename_editbox(),
244 abstract_filetype (abstract_filetype), fop (fop),
245 o_dir(), selected (NULL), vscroll (NULL)
247 assert(this->fop == SLO_SAVE || this->fop == SLO_LOAD);
249 /* For saving, construct an initial file name. */
250 if (this->fop == SLO_SAVE) {
251 switch (this->abstract_filetype) {
252 case FT_SAVEGAME:
253 this->GenerateFileName();
254 break;
256 case FT_SCENARIO:
257 case FT_HEIGHTMAP:
258 this->filename_editbox.Assign("UNNAMED");
259 break;
261 default:
262 NOT_REACHED();
265 this->querystrings[WID_SL_SAVE_OSK_TITLE] = &this->filename_editbox;
266 this->filename_editbox.ok_button = WID_SL_SAVE_GAME;
268 this->CreateNestedTree();
269 if (this->fop == SLO_LOAD && this->abstract_filetype == FT_SAVEGAME) {
270 this->GetWidget<NWidgetStacked>(WID_SL_CONTENT_DOWNLOAD_SEL)->SetDisplayedPlane(SZSP_HORIZONTAL);
273 /* Select caption string of the window. */
274 StringID caption_string;
275 switch (this->abstract_filetype) {
276 case FT_SAVEGAME:
277 caption_string = (this->fop == SLO_SAVE) ? STR_SAVELOAD_SAVE_CAPTION : STR_SAVELOAD_LOAD_CAPTION;
278 break;
280 case FT_SCENARIO:
281 caption_string = (this->fop == SLO_SAVE) ? STR_SAVELOAD_SAVE_SCENARIO : STR_SAVELOAD_LOAD_SCENARIO;
282 break;
284 case FT_HEIGHTMAP:
285 caption_string = (this->fop == SLO_SAVE) ? STR_SAVELOAD_SAVE_HEIGHTMAP : STR_SAVELOAD_LOAD_HEIGHTMAP;
286 break;
288 default:
289 NOT_REACHED();
291 this->GetWidget<NWidgetCore>(WID_SL_CAPTION)->widget_data = caption_string;
293 this->vscroll = this->GetScrollbar(WID_SL_SCROLLBAR);
294 this->InitNested(0);
296 this->LowerWidget(WID_SL_DRIVES_DIRECTORIES_LIST);
298 /* pause is only used in single-player, non-editor mode, non-menu mode. It
299 * will be unpaused in the WE_DESTROY event handler. */
300 if (_game_mode != GM_MENU && !_networking && _game_mode != GM_EDITOR) {
301 DoCommandP(0, PM_PAUSED_SAVELOAD, 1, CMD_PAUSE);
303 SetPointerMode (POINTER_NONE, WC_MAIN_WINDOW, 0, SPR_CURSOR_ZZZ);
305 this->OnInvalidateData(0);
307 ResetPointerMode();
309 /* Select the initial directory. */
310 o_dir.type = FIOS_TYPE_DIRECT;
311 o_dir.mtime = 0;
312 o_dir.title[0] = '\0';
313 switch (this->abstract_filetype) {
314 case FT_SAVEGAME:
315 FioGetDirectory(o_dir.name, lengthof(o_dir.name), SAVE_DIR);
316 break;
318 case FT_SCENARIO:
319 FioGetDirectory(o_dir.name, lengthof(o_dir.name), SCENARIO_DIR);
320 break;
322 case FT_HEIGHTMAP:
323 FioGetDirectory(o_dir.name, lengthof(o_dir.name), HEIGHTMAP_DIR);
324 break;
326 default:
327 bstrcpy (o_dir.name, _personal_dir);
330 /* Focus the edit box by default in the save windows */
331 if (this->fop == SLO_SAVE) this->SetFocusedWidget(WID_SL_SAVE_OSK_TITLE);
334 void OnDelete (void) FINAL_OVERRIDE
336 /* pause is only used in single-player, non-editor mode, non menu mode */
337 if (!_networking && _game_mode != GM_EDITOR && _game_mode != GM_MENU) {
338 DoCommandP(0, PM_PAUSED_SAVELOAD, 0, CMD_PAUSE);
342 void DrawWidget (BlitArea *dpi, const Rect &r, int widget) const OVERRIDE
344 switch (widget) {
345 case WID_SL_SORT_BYNAME:
346 case WID_SL_SORT_BYDATE:
347 if (((_savegame_sort_order & SORT_BY_NAME) != 0) == (widget == WID_SL_SORT_BYNAME)) {
348 this->DrawSortButtonState (dpi, widget, _savegame_sort_order & SORT_DESCENDING ? SBS_DOWN : SBS_UP);
350 break;
352 case WID_SL_BACKGROUND: {
353 static const char *path = NULL;
354 static StringID str = STR_ERROR_UNABLE_TO_READ_DRIVE;
355 static uint64 tot = 0;
357 if (_fios_path_changed) {
358 path = FiosGetPath();
359 str = FiosGetDiskFreeSpace (path, &tot) ? STR_SAVELOAD_BYTES_FREE : STR_ERROR_UNABLE_TO_READ_DRIVE;
360 _fios_path_changed = false;
363 if (str != STR_ERROR_UNABLE_TO_READ_DRIVE) SetDParam(0, tot);
364 DrawString (dpi, r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + FONT_HEIGHT_NORMAL + WD_FRAMERECT_TOP, str);
365 DrawString (dpi, r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + WD_FRAMERECT_TOP, path, TC_BLACK);
366 break;
369 case WID_SL_DRIVES_DIRECTORIES_LIST: {
370 GfxFillRect (dpi, r.left + 1, r.top + 1, r.right, r.bottom, PC_BLACK);
372 uint y = r.top + WD_FRAMERECT_TOP;
373 for (uint pos = this->vscroll->GetPosition(); pos < this->fios_items.Length(); pos++) {
374 const FiosItem *item = this->fios_items.Get(pos);
376 if (item == this->selected) {
377 GfxFillRect (dpi, r.left + 1, y, r.right, y + this->resize.step_height, PC_DARK_BLUE);
379 DrawString (dpi, r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, item->title, _fios_colours[GetDetailedFileType(item->type)]);
380 y += this->resize.step_height;
381 if (y >= this->vscroll->GetCapacity() * this->resize.step_height + r.top + WD_FRAMERECT_TOP) break;
383 break;
386 case WID_SL_DETAILS: {
387 GfxFillRect (dpi, r.left + WD_FRAMERECT_LEFT, r.top + WD_FRAMERECT_TOP,
388 r.right - WD_FRAMERECT_RIGHT, r.top + FONT_HEIGHT_NORMAL * 2 + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM, PC_GREY);
389 DrawString (dpi, r.left, r.right, r.top + FONT_HEIGHT_NORMAL / 2 + WD_FRAMERECT_TOP, STR_SAVELOAD_DETAIL_CAPTION, TC_FROMSTRING, SA_HOR_CENTER);
391 if (this->selected == NULL) break;
393 uint y = r.top + FONT_HEIGHT_NORMAL * 2 + WD_PAR_VSEP_NORMAL + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
394 uint y_max = r.bottom - FONT_HEIGHT_NORMAL - WD_FRAMERECT_BOTTOM;
396 if (y > y_max) break;
397 if (!_load_check_data.checkable) {
398 /* Old savegame, no information available */
399 DrawString (dpi, r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_SAVELOAD_DETAIL_NOT_AVAILABLE);
400 y += FONT_HEIGHT_NORMAL;
401 } else if (_load_check_data.error.str != INVALID_STRING_ID) {
402 /* Incompatible / broken savegame */
403 SetDParamStr(0, _load_check_data.error.data);
404 y = DrawStringMultiLine (dpi, r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT,
405 y, r.bottom - WD_FRAMERECT_BOTTOM, _load_check_data.error.str, TC_RED);
406 } else {
407 /* Mapsize */
408 SetDParam(0, _load_check_data.map_size_x);
409 SetDParam(1, _load_check_data.map_size_y);
410 DrawString (dpi, r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_SERVER_LIST_MAP_SIZE);
411 y += FONT_HEIGHT_NORMAL;
412 if (y > y_max) break;
414 /* Climate */
415 byte landscape = _load_check_data.settings.game_creation.landscape;
416 if (landscape < NUM_LANDSCAPE) {
417 SetDParam(0, STR_CHEAT_SWITCH_CLIMATE_TEMPERATE_LANDSCAPE + landscape);
418 DrawString (dpi, r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_SERVER_LIST_LANDSCAPE);
419 y += FONT_HEIGHT_NORMAL;
422 y += WD_PAR_VSEP_NORMAL;
423 if (y > y_max) break;
425 /* Start date (if available) */
426 if (_load_check_data.settings.game_creation.starting_year != 0) {
427 SetDParam(0, ConvertYMDToDate(_load_check_data.settings.game_creation.starting_year, 0, 1));
428 DrawString (dpi, r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_SERVER_LIST_START_DATE);
429 y += FONT_HEIGHT_NORMAL;
431 if (y > y_max) break;
433 /* Hide current date for scenarios */
434 if (this->abstract_filetype != FT_SCENARIO) {
435 /* Current date */
436 SetDParam(0, _load_check_data.current_date);
437 DrawString (dpi, r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_SERVER_LIST_CURRENT_DATE);
438 y += FONT_HEIGHT_NORMAL;
441 /* Hide the NewGRF stuff when saving. We also hide the button. */
442 if (this->fop == SLO_LOAD && (this->abstract_filetype == FT_SAVEGAME || this->abstract_filetype == FT_SCENARIO)) {
443 y += WD_PAR_VSEP_NORMAL;
444 if (y > y_max) break;
446 /* NewGrf compatibility */
447 SetDParam(0, _load_check_data.grfconfig == NULL ? STR_NEWGRF_LIST_NONE :
448 STR_NEWGRF_LIST_ALL_FOUND + _load_check_data.grf_compatibility);
449 DrawString (dpi, r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_SAVELOAD_DETAIL_GRFSTATUS);
450 y += FONT_HEIGHT_NORMAL;
452 if (y > y_max) break;
454 /* Hide the company stuff for scenarios */
455 if (this->abstract_filetype != FT_SCENARIO) {
456 y += FONT_HEIGHT_NORMAL;
457 if (y > y_max) break;
459 /* Companies / AIs */
460 CompanyPropertiesMap::const_iterator end = _load_check_data.companies.End();
461 for (CompanyPropertiesMap::const_iterator it = _load_check_data.companies.Begin(); it != end; it++) {
462 SetDParam(0, it->first + 1);
463 const CompanyProperties &c = *it->second;
464 if (c.name != NULL) {
465 SetDParam(1, STR_JUST_RAW_STRING);
466 SetDParamStr(2, c.name);
467 } else {
468 SetDParam(1, c.name_1);
469 SetDParam(2, c.name_2);
471 DrawString (dpi, r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_SAVELOAD_DETAIL_COMPANY_INDEX);
472 y += FONT_HEIGHT_NORMAL;
473 if (y > y_max) break;
477 break;
482 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
484 switch (widget) {
485 case WID_SL_BACKGROUND:
486 size->height = 2 * FONT_HEIGHT_NORMAL + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
487 break;
489 case WID_SL_DRIVES_DIRECTORIES_LIST:
490 resize->height = FONT_HEIGHT_NORMAL;
491 size->height = resize->height * 10 + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
492 break;
493 case WID_SL_SORT_BYNAME:
494 case WID_SL_SORT_BYDATE: {
495 Dimension d = GetStringBoundingBox(this->GetWidget<NWidgetCore>(widget)->widget_data);
496 d.width += padding.width + Window::SortButtonWidth() * 2; // Doubled since the string is centred and it also looks better.
497 d.height += padding.height;
498 *size = maxdim(*size, d);
499 break;
504 void OnPaint (BlitArea *dpi) OVERRIDE
506 if (_savegame_sort_dirty) {
507 _savegame_sort_dirty = false;
508 SortSaveGameList(this->fios_items);
511 this->vscroll->SetCount(this->fios_items.Length());
512 this->DrawWidgets (dpi);
515 virtual void OnClick(Point pt, int widget, int click_count)
517 switch (widget) {
518 case WID_SL_SORT_BYNAME: // Sort save names by name
519 _savegame_sort_order = (_savegame_sort_order == SORT_BY_NAME) ?
520 SORT_BY_NAME | SORT_DESCENDING : SORT_BY_NAME;
521 _savegame_sort_dirty = true;
522 this->SetDirty();
523 break;
525 case WID_SL_SORT_BYDATE: // Sort save names by date
526 _savegame_sort_order = (_savegame_sort_order == SORT_BY_DATE) ?
527 SORT_BY_DATE | SORT_DESCENDING : SORT_BY_DATE;
528 _savegame_sort_dirty = true;
529 this->SetDirty();
530 break;
532 case WID_SL_HOME_BUTTON: // OpenTTD 'button', jumps to OpenTTD directory
533 FiosBrowseTo(&o_dir);
534 this->InvalidateData();
535 break;
537 case WID_SL_LOAD_BUTTON:
538 if (this->selected != NULL && !_load_check_data.HasErrors()) {
539 const char *name = FiosBrowseTo(this->selected);
540 _file_to_saveload.SetMode(this->selected->type);
541 _file_to_saveload.SetName(name);
542 _file_to_saveload.SetTitle(this->selected->title);
544 if (this->abstract_filetype == FT_HEIGHTMAP) {
545 this->Delete();
546 ShowHeightmapLoad();
548 } else if (!_load_check_data.HasNewGrfs() || _load_check_data.grf_compatibility != GLC_NOT_FOUND || _settings_client.gui.UserIsAllowedToChangeNewGRFs()) {
549 _switch_mode = (_game_mode == GM_EDITOR) ? SM_LOAD_SCENARIO : SM_LOAD_GAME;
550 ClearErrorMessages();
551 this->Delete();
554 break;
556 case WID_SL_NEWGRF_INFO:
557 if (_load_check_data.HasNewGrfs()) {
558 ShowNewGRFSettings(false, false, false, &_load_check_data.grfconfig);
560 break;
562 case WID_SL_MISSING_NEWGRFS:
563 if (!_network_available) {
564 ShowErrorMessage(STR_NETWORK_ERROR_NOTAVAILABLE, INVALID_STRING_ID, WL_ERROR);
565 } else if (_load_check_data.HasNewGrfs()) {
566 #if defined(ENABLE_NETWORK)
567 ShowMissingContentWindow(_load_check_data.grfconfig);
568 #endif
570 break;
572 case WID_SL_DRIVES_DIRECTORIES_LIST: { // Click the listbox
573 int y = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_SL_DRIVES_DIRECTORIES_LIST, WD_FRAMERECT_TOP);
574 if (y == INT_MAX) return;
576 const FiosItem *file = this->fios_items.Get(y);
578 const char *name = FiosBrowseTo(file);
579 if (name != NULL) {
580 if (click_count == 1) {
581 if (this->selected != file) {
582 this->selected = file;
583 _load_check_data.Clear();
585 if (GetDetailedFileType(file->type) == DFT_GAME_FILE) {
586 /* Other detailed file types cannot be checked before. */
587 LoadGame (name, true, false, NO_DIRECTORY);
590 this->InvalidateData(1);
592 if (this->fop == SLO_SAVE) {
593 /* Copy clicked name to editbox */
594 this->filename_editbox.Assign(file->title);
595 this->SetWidgetDirty(WID_SL_SAVE_OSK_TITLE);
597 } else if (!_load_check_data.HasErrors()) {
598 this->selected = file;
599 if (this->fop == SLO_LOAD) {
600 if (this->abstract_filetype == FT_SAVEGAME || this->abstract_filetype == FT_SCENARIO) {
601 this->OnClick(pt, WID_SL_LOAD_BUTTON, 1);
602 } else {
603 assert(this->abstract_filetype == FT_HEIGHTMAP);
604 _file_to_saveload.SetMode(file->type);
605 _file_to_saveload.SetName(name);
606 _file_to_saveload.SetTitle(file->title);
608 this->Delete();
609 ShowHeightmapLoad();
613 } else {
614 /* Changed directory, need refresh. */
615 this->InvalidateData();
617 break;
620 case WID_SL_CONTENT_DOWNLOAD:
621 if (!_network_available) {
622 ShowErrorMessage(STR_NETWORK_ERROR_NOTAVAILABLE, INVALID_STRING_ID, WL_ERROR);
623 } else {
624 #if defined(ENABLE_NETWORK)
625 assert(this->fop == SLO_LOAD);
626 switch (this->abstract_filetype) {
627 default: NOT_REACHED();
628 case FT_SCENARIO: ShowNetworkContentListWindow(NULL, CONTENT_TYPE_SCENARIO); break;
629 case FT_HEIGHTMAP: ShowNetworkContentListWindow(NULL, CONTENT_TYPE_HEIGHTMAP); break;
631 #endif
633 break;
635 case WID_SL_DELETE_SELECTION: // Delete
636 break;
638 case WID_SL_SAVE_GAME: // Save game
639 /* Note, this is also called via the OSK; and we need to lower the button. */
640 this->HandleButtonClick(WID_SL_SAVE_GAME);
641 break;
645 virtual EventState OnKeyPress(WChar key, uint16 keycode)
647 if (keycode == WKC_ESC) {
648 this->Delete();
649 return ES_HANDLED;
652 return ES_NOT_HANDLED;
655 virtual void OnTimeout()
657 /* Widgets WID_SL_DELETE_SELECTION and WID_SL_SAVE_GAME only exist when saving to a file. */
658 if (this->fop != SLO_SAVE) return;
660 if (this->IsWidgetLowered(WID_SL_DELETE_SELECTION)) { // Delete button clicked
661 if (!FiosDelete(this->filename_editbox.GetText())) {
662 ShowErrorMessage(STR_ERROR_UNABLE_TO_DELETE_FILE, INVALID_STRING_ID, WL_ERROR);
663 } else {
664 this->InvalidateData();
665 /* Reset file name to current date on successful delete */
666 if (this->abstract_filetype == FT_SAVEGAME) GenerateFileName();
668 } else if (this->IsWidgetLowered(WID_SL_SAVE_GAME)) { // Save button clicked
669 if (this->abstract_filetype == FT_SAVEGAME || this->abstract_filetype == FT_SCENARIO) {
670 _switch_mode = SM_SAVE_GAME;
671 FiosMakeSavegameName(_file_to_saveload.name, this->filename_editbox.GetText(), sizeof(_file_to_saveload.name));
672 } else {
673 _switch_mode = SM_SAVE_HEIGHTMAP;
674 FiosMakeHeightmapName(_file_to_saveload.name, this->filename_editbox.GetText(), sizeof(_file_to_saveload.name));
677 /* In the editor set up the vehicle engines correctly (date might have changed) */
678 if (_game_mode == GM_EDITOR) StartupEngines();
682 virtual void OnResize()
684 this->vscroll->SetCapacityFromWidget(this, WID_SL_DRIVES_DIRECTORIES_LIST);
688 * Some data on this window has become invalid.
689 * @param data Information about the changed data.
690 * @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.
692 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
694 switch (data) {
695 case 0:
696 /* Rescan files */
697 this->selected = NULL;
698 _load_check_data.Clear();
699 if (!gui_scope) break;
701 _fios_path_changed = true;
702 this->fios_items.BuildFileList(this->abstract_filetype, this->fop);
703 this->vscroll->SetCount(this->fios_items.Length());
704 this->selected = NULL;
705 _load_check_data.Clear();
706 /* FALL THROUGH */
707 case 1:
708 /* Selection changes */
709 if (!gui_scope) break;
711 if (this->fop != SLO_LOAD) break;
713 switch (this->abstract_filetype) {
714 case FT_HEIGHTMAP:
715 this->SetWidgetDisabledState(WID_SL_LOAD_BUTTON, this->selected == NULL || _load_check_data.HasErrors());
716 break;
718 case FT_SAVEGAME:
719 case FT_SCENARIO: {
720 bool disabled = this->selected == NULL || _load_check_data.HasErrors();
721 if (!_settings_client.gui.UserIsAllowedToChangeNewGRFs()) {
722 disabled |= _load_check_data.HasNewGrfs() && _load_check_data.grf_compatibility == GLC_NOT_FOUND;
724 this->SetWidgetDisabledState(WID_SL_LOAD_BUTTON, disabled);
725 this->SetWidgetDisabledState(WID_SL_NEWGRF_INFO, !_load_check_data.HasNewGrfs());
726 this->SetWidgetDisabledState(WID_SL_MISSING_NEWGRFS,
727 !_load_check_data.HasNewGrfs() || _load_check_data.grf_compatibility == GLC_ALL_GOOD);
728 break;
731 default:
732 NOT_REACHED();
734 break;
739 /** Load game/scenario */
740 static WindowDesc::Prefs _load_dialog_prefs ("load_game");
742 /** Load game/scenario */
743 static const WindowDesc _load_dialog_desc(
744 WDP_CENTER, 500, 294,
745 WC_SAVELOAD, WC_NONE,
747 _nested_load_dialog_widgets, lengthof(_nested_load_dialog_widgets),
748 &_load_dialog_prefs
751 /** Load heightmap */
752 static WindowDesc::Prefs _load_heightmap_dialog_prefs ("load_heightmap");
754 /** Load heightmap */
755 static const WindowDesc _load_heightmap_dialog_desc(
756 WDP_CENTER, 257, 320,
757 WC_SAVELOAD, WC_NONE,
759 _nested_load_heightmap_dialog_widgets, lengthof(_nested_load_heightmap_dialog_widgets),
760 &_load_heightmap_dialog_prefs
763 /** Save game/scenario */
764 static WindowDesc::Prefs _save_dialog_prefs ("save_game");
766 /** Save game/scenario */
767 static const WindowDesc _save_dialog_desc(
768 WDP_CENTER, 500, 294,
769 WC_SAVELOAD, WC_NONE,
771 _nested_save_dialog_widgets, lengthof(_nested_save_dialog_widgets),
772 &_save_dialog_prefs
776 * Launch save/load dialog in the given mode.
777 * @param abstract_filetype Kind of file to handle.
778 * @param fop File operation to perform (load or save).
780 void ShowSaveLoadDialog(AbstractFileType abstract_filetype, SaveLoadOperation fop)
782 DeleteWindowById(WC_SAVELOAD, 0);
784 const WindowDesc *sld;
785 if (fop == SLO_SAVE) {
786 sld = &_save_dialog_desc;
787 } else {
788 /* Dialogue for loading a file. */
789 sld = (abstract_filetype == FT_HEIGHTMAP) ? &_load_heightmap_dialog_desc : &_load_dialog_desc;
792 _file_to_saveload.abstract_ftype = abstract_filetype;
794 new SaveLoadWindow(sld, abstract_filetype, fop);