# fios_gui.cpp

Go to the documentation of this file.

    00001 /* $Id$ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
    00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
    00008  */
    00009
    00012 #include "stdafx.h"
    00013 #include "saveload/saveload.h"
    00014 #include "error.h"
    00015 #include "gui.h"
    00016 #include "gfx_func.h"
    00017 #include "command_func.h"
    00018 #include "network/network.h"
    00019 #include "network/network_content.h"
    00020 #include "strings_func.h"
    00021 #include "fileio_func.h"
    00022 #include "fios.h"
    00023 #include "window_func.h"
    00024 #include "tilehighlight_func.h"
    00025 #include "querystring_gui.h"
    00026 #include "engine_func.h"
    00027 #include "landscape_type.h"
    00028 #include "date_func.h"
    00029 #include "core/geometry_func.hpp"
    00030 #include "gamelog.h"
    00031
    00032 #include "widgets/fios_widget.h"
    00033
    00034 #include "table/sprites.h"
    00035 #include "table/strings.h"
    00036
    00037 SaveLoadDialogMode _saveload_mode;
    00038 LoadCheckData _load_check_data;
    00039
    00040 static bool _fios_path_changed;
    00041 static bool _savegame_sort_dirty;
    00042
    00043
    00047 void LoadCheckData::Clear()
    00048 {
    00049   this->checkable = false;
    00050   this->error = INVALID_STRING_ID;
    00051   free(this->error_data);
    00052   this->error_data = NULL;
    00053
    00054   this->map_size_x = this->map_size_y = 256; // Default for old savegames which do not store mapsize.
    00055   this->current_date = 0;
    00056   memset(&this->settings, 0, sizeof(this->settings));
    00057
    00058   const CompanyPropertiesMap::iterator end = this->companies.End();
    00059   for (CompanyPropertiesMap::iterator it = this->companies.Begin(); it != end; it++) {
    00060     delete it->second;
    00061   }
    00062   companies.Clear();
    00063
    00064   GamelogFree(this->gamelog_action, this->gamelog_actions);
    00065   this->gamelog_action = NULL;
    00066   this->gamelog_actions = 0;
    00067
    00068   ClearGRFConfigList(&this->grfconfig);
    00069 }
    00070
    00072 static const NWidgetPart _nested_load_dialog_widgets[] = {
    00073   NWidget(NWID_HORIZONTAL),
    00074     NWidget(WWT_CLOSEBOX, COLOUR_GREY),
    00075     NWidget(WWT_CAPTION, COLOUR_GREY, WID_SL_CAPTION),
    00076   EndContainer(),
    00077   NWidget(WWT_PANEL, COLOUR_GREY, WID_SL_BACKGROUND), SetFill(1, 0), SetResize(1, 0), EndContainer(),
    00078   NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
    00079     NWidget(NWID_VERTICAL),
    00080       NWidget(NWID_HORIZONTAL),
    00081         NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
    00082           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),
    00083           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),
    00084         EndContainer(),
    00085         NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_SL_HOME_BUTTON), SetMinimalSize(12, 12), SetDataTip(SPR_HOUSE_ICON, STR_SAVELOAD_HOME_BUTTON),
    00086       EndContainer(),
    00087       NWidget(WWT_PANEL, COLOUR_GREY, WID_SL_FILE_BACKGROUND),
    00088         NWidget(NWID_HORIZONTAL),
    00089           NWidget(WWT_INSET, COLOUR_GREY, WID_SL_DRIVES_DIRECTORIES_LIST), SetFill(1, 1), SetPadding(2, 1, 2, 2),
    00090               SetDataTip(0x0, STR_SAVELOAD_LIST_TOOLTIP), SetResize(1, 10), SetScrollbar(WID_SL_SCROLLBAR), EndContainer(),
    00091           NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_SL_SCROLLBAR),
    00092         EndContainer(),
    00093         NWidget(NWID_SELECTION, INVALID_COLOUR, WID_SL_CONTENT_DOWNLOAD_SEL),
    00094           NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SL_CONTENT_DOWNLOAD), SetResize(1, 0),
    00095               SetDataTip(STR_INTRO_ONLINE_CONTENT, STR_INTRO_TOOLTIP_ONLINE_CONTENT),
    00096         EndContainer(),
    00097       EndContainer(),
    00098     EndContainer(),
    00099     NWidget(WWT_PANEL, COLOUR_GREY),
    00100       NWidget(WWT_EMPTY, INVALID_COLOUR, WID_SL_DETAILS), SetResize(1, 1), SetFill(1, 1),
    00101       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),
    00102       NWidget(NWID_HORIZONTAL),
    00103         NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
    00104           NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SL_NEWGRF_INFO), SetDataTip(STR_INTRO_NEWGRF_SETTINGS, STR_NULL), SetFill(1, 0), SetResize(1, 0),
    00105           NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SL_LOAD_BUTTON), SetDataTip(STR_SAVELOAD_LOAD_BUTTON, STR_SAVELOAD_LOAD_TOOLTIP), SetFill(1, 0), SetResize(1, 0),
    00106         EndContainer(),
    00107         NWidget(WWT_RESIZEBOX, COLOUR_GREY),
    00108       EndContainer(),
    00109     EndContainer(),
    00110   EndContainer(),
    00111 };
    00112
    00114 static const NWidgetPart _nested_load_heightmap_dialog_widgets[] = {
    00115   NWidget(NWID_HORIZONTAL),
    00116     NWidget(WWT_CLOSEBOX, COLOUR_GREY),
    00117     NWidget(WWT_CAPTION, COLOUR_GREY, WID_SL_CAPTION),
    00118   EndContainer(),
    00119   NWidget(WWT_PANEL, COLOUR_GREY, WID_SL_BACKGROUND), SetFill(1, 0), SetResize(1, 0), EndContainer(),
    00120   NWidget(NWID_VERTICAL),
    00121     NWidget(NWID_HORIZONTAL),
    00122       NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
    00123         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),
    00124         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),
    00125       EndContainer(),
    00126       NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_SL_HOME_BUTTON), SetMinimalSize(12, 12), SetDataTip(SPR_HOUSE_ICON, STR_SAVELOAD_HOME_BUTTON),
    00127     EndContainer(),
    00128     NWidget(WWT_PANEL, COLOUR_GREY, WID_SL_FILE_BACKGROUND),
    00129       NWidget(NWID_HORIZONTAL),
    00130         NWidget(WWT_INSET, COLOUR_GREY, WID_SL_DRIVES_DIRECTORIES_LIST), SetFill(1, 1), SetPadding(2, 1, 2, 2),
    00131             SetDataTip(0x0, STR_SAVELOAD_LIST_TOOLTIP), SetResize(1, 10), SetScrollbar(WID_SL_SCROLLBAR), EndContainer(),
    00132         NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_SL_SCROLLBAR),
    00133       EndContainer(),
    00134       NWidget(NWID_HORIZONTAL),
    00135         NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SL_CONTENT_DOWNLOAD), SetResize(1, 0),
    00136             SetDataTip(STR_INTRO_ONLINE_CONTENT, STR_INTRO_TOOLTIP_ONLINE_CONTENT),
    00137         NWidget(WWT_RESIZEBOX, COLOUR_GREY),
    00138       EndContainer(),
    00139     EndContainer(),
    00140   EndContainer(),
    00141 };
    00142
    00144 static const NWidgetPart _nested_save_dialog_widgets[] = {
    00145   NWidget(NWID_HORIZONTAL),
    00146     NWidget(WWT_CLOSEBOX, COLOUR_GREY),
    00147     NWidget(WWT_CAPTION, COLOUR_GREY, WID_SL_CAPTION),
    00148   EndContainer(),
    00149   NWidget(WWT_PANEL, COLOUR_GREY, WID_SL_BACKGROUND), SetFill(1, 0), SetResize(1, 0), EndContainer(),
    00150   NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
    00151     NWidget(NWID_VERTICAL),
    00152       NWidget(NWID_HORIZONTAL),
    00153         NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
    00154           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),
    00155           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),
    00156         EndContainer(),
    00157         NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_SL_HOME_BUTTON), SetMinimalSize(12, 12), SetDataTip(SPR_HOUSE_ICON, STR_SAVELOAD_HOME_BUTTON),
    00158       EndContainer(),
    00159       NWidget(WWT_PANEL, COLOUR_GREY, WID_SL_FILE_BACKGROUND),
    00160         NWidget(NWID_HORIZONTAL),
    00161           NWidget(WWT_INSET, COLOUR_GREY, WID_SL_DRIVES_DIRECTORIES_LIST), SetPadding(2, 1, 0, 2),
    00162               SetDataTip(0x0, STR_SAVELOAD_LIST_TOOLTIP), SetResize(1, 10), SetScrollbar(WID_SL_SCROLLBAR), EndContainer(),
    00163           NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_SL_SCROLLBAR),
    00164         EndContainer(),
    00165         NWidget(WWT_EDITBOX, COLOUR_GREY, WID_SL_SAVE_OSK_TITLE), SetPadding(3, 2, 2, 2), SetFill(1, 0), SetResize(1, 0),
    00166             SetDataTip(STR_SAVELOAD_OSKTITLE, STR_SAVELOAD_EDITBOX_TOOLTIP),
    00167       EndContainer(),
    00168       NWidget(NWID_HORIZONTAL),
    00169         NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SL_DELETE_SELECTION), SetDataTip(STR_SAVELOAD_DELETE_BUTTON, STR_SAVELOAD_DELETE_TOOLTIP), SetFill(1, 0), SetResize(1, 0),
    00170         NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SL_SAVE_GAME),        SetDataTip(STR_SAVELOAD_SAVE_BUTTON, STR_SAVELOAD_SAVE_TOOLTIP),     SetFill(1, 0), SetResize(1, 0),
    00171       EndContainer(),
    00172     EndContainer(),
    00173     NWidget(WWT_PANEL, COLOUR_GREY),
    00174       NWidget(WWT_EMPTY, INVALID_COLOUR, WID_SL_DETAILS), SetResize(1, 1), SetFill(1, 1),
    00175       NWidget(NWID_HORIZONTAL),
    00176         NWidget(NWID_SPACER), SetResize(1, 0), SetFill(1, 1),
    00177         NWidget(WWT_RESIZEBOX, COLOUR_GREY),
    00178       EndContainer(),
    00179     EndContainer(),
    00180   EndContainer(),
    00181 };
    00182
    00184 const TextColour _fios_colours[] = {
    00185   TC_LIGHT_BLUE, TC_DARK_GREEN,  TC_DARK_GREEN, TC_ORANGE, TC_LIGHT_BROWN,
    00186   TC_ORANGE,     TC_LIGHT_BROWN, TC_ORANGE,     TC_ORANGE, TC_YELLOW
    00187 };
    00188
    00189 void BuildFileList()
    00190 {
    00191   _fios_path_changed = true;
    00192   FiosFreeSavegameList();
    00193
    00194   switch (_saveload_mode) {
    00195     case SLD_LOAD_SCENARIO:
    00196     case SLD_SAVE_SCENARIO:
    00197       FiosGetScenarioList(_saveload_mode); break;
    00198     case SLD_SAVE_HEIGHTMAP:
    00199     case SLD_LOAD_HEIGHTMAP:
    00200       FiosGetHeightmapList(_saveload_mode); break;
    00201
    00202     default: FiosGetSavegameList(_saveload_mode); break;
    00203   }
    00204
    00205   /* Invalidate saveload window */
    00206   InvalidateWindowData(WC_SAVELOAD, 0, 2, true);
    00207 }
    00208
    00209 static void MakeSortedSaveGameList()
    00210 {
    00211   uint sort_start = 0;
    00212   uint sort_end = 0;
    00213
    00214   /* Directories are always above the files (FIOS_TYPE_DIR)
    00215    * Drives (A:\ (windows only) are always under the files (FIOS_TYPE_DRIVE)
    00216    * Only sort savegames/scenarios, not directories
    00217    */
    00218   for (const FiosItem *item = _fios_items.Begin(); item != _fios_items.End(); item++) {
    00219     switch (item->type) {
    00220       case FIOS_TYPE_DIR:    sort_start++; break;
    00221       case FIOS_TYPE_PARENT: sort_start++; break;
    00222       case FIOS_TYPE_DRIVE:  sort_end++;   break;
    00223       default: break;
    00224     }
    00225   }
    00226
    00227   uint s_amount = _fios_items.Length() - sort_start - sort_end;
    00228   QSortT(_fios_items.Get(sort_start), s_amount, CompareFiosItems);
    00229 }
    00230
    00231 struct SaveLoadWindow : public Window {
    00232 private:
    00233   QueryString filename_editbox;
    00234   FiosItem o_dir;
    00235   const FiosItem *selected;
    00236   Scrollbar *vscroll;
    00237 public:
    00238
    00240   void GenerateFileName()
    00241   {
    00242     GenerateDefaultSaveName(this->filename_editbox.text.buf, &this->filename_editbox.text.buf[this->filename_editbox.text.max_bytes - 1]);
    00243     this->filename_editbox.text.UpdateSize();
    00244   }
    00245
    00246   SaveLoadWindow(const WindowDesc *desc, SaveLoadDialogMode mode) : filename_editbox(64)
    00247   {
    00248     static const StringID saveload_captions[] = {
    00249       STR_SAVELOAD_LOAD_CAPTION,
    00250       STR_SAVELOAD_LOAD_SCENARIO,
    00251       STR_SAVELOAD_SAVE_CAPTION,
    00252       STR_SAVELOAD_SAVE_SCENARIO,
    00253       STR_SAVELOAD_LOAD_HEIGHTMAP,
    00254       STR_SAVELOAD_SAVE_HEIGHTMAP,
    00255     };
    00256     assert((uint)mode < lengthof(saveload_captions));
    00257
    00258     /* Use an array to define what will be the current file type being handled
    00259      * by current file mode */
    00260     switch (mode) {
    00261       case SLD_SAVE_GAME:     this->GenerateFileName(); break;
    00262       case SLD_SAVE_HEIGHTMAP:
    00263       case SLD_SAVE_SCENARIO: this->filename_editbox.text.Assign("UNNAMED"); break;
    00264       default:                break;
    00265     }
    00266
    00267     this->querystrings[WID_SL_SAVE_OSK_TITLE] = &this->filename_editbox;
    00268     this->filename_editbox.ok_button = WID_SL_SAVE_GAME;
    00269
    00270     this->CreateNestedTree(desc, true);
    00271     if (mode == SLD_LOAD_GAME) this->GetWidget<NWidgetStacked>(WID_SL_CONTENT_DOWNLOAD_SEL)->SetDisplayedPlane(SZSP_HORIZONTAL);
    00272     this->GetWidget<NWidgetCore>(WID_SL_CAPTION)->widget_data = saveload_captions[mode];
    00273     this->vscroll = this->GetScrollbar(WID_SL_SCROLLBAR);
    00274
    00275     this->FinishInitNested(desc, 0);
    00276
    00277     this->LowerWidget(WID_SL_DRIVES_DIRECTORIES_LIST);
    00278
    00279     /* pause is only used in single-player, non-editor mode, non-menu mode. It
    00280      * will be unpaused in the WE_DESTROY event handler. */
    00281     if (_game_mode != GM_MENU && !_networking && _game_mode != GM_EDITOR) {
    00282       DoCommandP(0, PM_PAUSED_SAVELOAD, 1, CMD_PAUSE);
    00283     }
    00284     SetObjectToPlace(SPR_CURSOR_ZZZ, PAL_NONE, HT_NONE, WC_MAIN_WINDOW, 0);
    00285
    00286     this->OnInvalidateData(0);
    00287
    00288     ResetObjectToPlace();
    00289
    00290     o_dir.type = FIOS_TYPE_DIRECT;
    00291     switch (_saveload_mode) {
    00292       case SLD_SAVE_GAME:
    00293       case SLD_LOAD_GAME:
    00294         FioGetDirectory(o_dir.name, lengthof(o_dir.name), SAVE_DIR);
    00295         break;
    00296
    00297       case SLD_SAVE_SCENARIO:
    00298       case SLD_LOAD_SCENARIO:
    00299         FioGetDirectory(o_dir.name, lengthof(o_dir.name), SCENARIO_DIR);
    00300         break;
    00301
    00302       case SLD_SAVE_HEIGHTMAP:
    00303       case SLD_LOAD_HEIGHTMAP:
    00304         FioGetDirectory(o_dir.name, lengthof(o_dir.name), HEIGHTMAP_DIR);
    00305         break;
    00306
    00307       default:
    00308         strecpy(o_dir.name, _personal_dir, lastof(o_dir.name));
    00309     }
    00310
    00311     /* Focus the edit box by default in the save windows */
    00312     if (_saveload_mode == SLD_SAVE_GAME || _saveload_mode == SLD_SAVE_SCENARIO || _saveload_mode == SLD_SAVE_HEIGHTMAP) {
    00313       this->SetFocusedWidget(WID_SL_SAVE_OSK_TITLE);
    00314     }
    00315   }
    00316
    00317   virtual ~SaveLoadWindow()
    00318   {
    00319     /* pause is only used in single-player, non-editor mode, non menu mode */
    00320     if (!_networking && _game_mode != GM_EDITOR && _game_mode != GM_MENU) {
    00321       DoCommandP(0, PM_PAUSED_SAVELOAD, 0, CMD_PAUSE);
    00322     }
    00323     FiosFreeSavegameList();
    00324   }
    00325
    00326   virtual void DrawWidget(const Rect &r, int widget) const
    00327   {
    00328     switch (widget) {
    00329       case WID_SL_SORT_BYNAME:
    00330       case WID_SL_SORT_BYDATE:
    00331         if (((_savegame_sort_order & SORT_BY_NAME) != 0) == (widget == WID_SL_SORT_BYNAME)) {
    00332           this->DrawSortButtonState(widget, _savegame_sort_order & SORT_DESCENDING ? SBS_DOWN : SBS_UP);
    00333         }
    00334         break;
    00335
    00336       case WID_SL_BACKGROUND: {
    00337         static const char *path = NULL;
    00338         static StringID str = STR_ERROR_UNABLE_TO_READ_DRIVE;
    00339         static uint64 tot = 0;
    00340
    00341         if (_fios_path_changed) {
    00342           str = FiosGetDescText(&path, &tot);
    00343           _fios_path_changed = false;
    00344         }
    00345
    00346         if (str != STR_ERROR_UNABLE_TO_READ_DRIVE) SetDParam(0, tot);
    00347         DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + FONT_HEIGHT_NORMAL + WD_FRAMERECT_TOP, str);
    00348         DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + WD_FRAMERECT_TOP, path, TC_BLACK);
    00349         break;
    00350       }
    00351
    00352       case WID_SL_DRIVES_DIRECTORIES_LIST: {
    00353         GfxFillRect(r.left + 1, r.top + 1, r.right, r.bottom, PC_BLACK);
    00354
    00355         uint y = r.top + WD_FRAMERECT_TOP;
    00356         for (uint pos = this->vscroll->GetPosition(); pos < _fios_items.Length(); pos++) {
    00357           const FiosItem *item = _fios_items.Get(pos);
    00358
    00359           if (item == this->selected) {
    00360             GfxFillRect(r.left + 1, y, r.right, y + this->resize.step_height, PC_DARK_BLUE);
    00361           }
    00362           DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, item->title, _fios_colours[item->type]);
    00363           y += this->resize.step_height;
    00364           if (y >= this->vscroll->GetCapacity() * this->resize.step_height + r.top + WD_FRAMERECT_TOP) break;
    00365         }
    00366         break;
    00367       }
    00368
    00369       case WID_SL_DETAILS: {
    00370         GfxFillRect(r.left + WD_FRAMERECT_LEFT, r.top + WD_FRAMERECT_TOP,
    00371             r.right - WD_FRAMERECT_RIGHT, r.top + FONT_HEIGHT_NORMAL * 2 + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM, PC_GREY);
    00372         DrawString(r.left, r.right, r.top + FONT_HEIGHT_NORMAL / 2 + WD_FRAMERECT_TOP, STR_SAVELOAD_DETAIL_CAPTION, TC_FROMSTRING, SA_HOR_CENTER);
    00373
    00374         if (this->selected == NULL) break;
    00375
    00376         uint y = r.top + FONT_HEIGHT_NORMAL * 2 + WD_PAR_VSEP_NORMAL + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
    00377         uint y_max = r.bottom - FONT_HEIGHT_NORMAL - WD_FRAMERECT_BOTTOM;
    00378
    00379         if (y > y_max) break;
    00380         if (!_load_check_data.checkable) {
    00381           /* Old savegame, no information available */
    00382           DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_SAVELOAD_DETAIL_NOT_AVAILABLE);
    00383           y += FONT_HEIGHT_NORMAL;
    00384         } else if (_load_check_data.error != INVALID_STRING_ID) {
    00385           /* Incompatible / broken savegame */
    00386           SetDParamStr(0, _load_check_data.error_data);
    00387           y = DrawStringMultiLine(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT,
    00388               y, r.bottom - WD_FRAMERECT_BOTTOM, _load_check_data.error, TC_RED);
    00389         } else {
    00390           /* Mapsize */
    00391           SetDParam(0, _load_check_data.map_size_x);
    00392           SetDParam(1, _load_check_data.map_size_y);
    00393           DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_SERVER_LIST_MAP_SIZE);
    00394           y += FONT_HEIGHT_NORMAL;
    00395           if (y > y_max) break;
    00396
    00397           /* Climate */
    00398           byte landscape = _load_check_data.settings.game_creation.landscape;
    00399           if (landscape < NUM_LANDSCAPE) {
    00400             SetDParam(0, STR_CHEAT_SWITCH_CLIMATE_TEMPERATE_LANDSCAPE + landscape);
    00401             DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_SERVER_LIST_LANDSCAPE);
    00402             y += FONT_HEIGHT_NORMAL;
    00403           }
    00404
    00405           y += WD_PAR_VSEP_NORMAL;
    00406           if (y > y_max) break;
    00407
    00408           /* Start date (if available) */
    00409           if (_load_check_data.settings.game_creation.starting_year != 0) {
    00410             SetDParam(0, ConvertYMDToDate(_load_check_data.settings.game_creation.starting_year, 0, 1));
    00411             DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_SERVER_LIST_START_DATE);
    00412             y += FONT_HEIGHT_NORMAL;
    00413           }
    00414           if (y > y_max) break;
    00415
    00416           /* Hide current date for scenarios */
    00417           if (_saveload_mode != SLD_LOAD_SCENARIO && _saveload_mode != SLD_SAVE_SCENARIO) {
    00418             /* Current date */
    00419             SetDParam(0, _load_check_data.current_date);
    00420             DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_SERVER_LIST_CURRENT_DATE);
    00421             y += FONT_HEIGHT_NORMAL;
    00422           }
    00423
    00424           /* Hide the NewGRF stuff when saving. We also hide the button. */
    00425           if (_saveload_mode == SLD_LOAD_GAME || _saveload_mode == SLD_LOAD_SCENARIO) {
    00426             y += WD_PAR_VSEP_NORMAL;
    00427             if (y > y_max) break;
    00428
    00429             /* NewGrf compatibility */
    00430             SetDParam(0, _load_check_data.grfconfig == NULL ? STR_NEWGRF_LIST_NONE :
    00431                 STR_NEWGRF_LIST_ALL_FOUND + _load_check_data.grf_compatibility);
    00432             DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_SAVELOAD_DETAIL_GRFSTATUS);
    00433             y += FONT_HEIGHT_NORMAL;
    00434           }
    00435           if (y > y_max) break;
    00436
    00437           /* Hide the company stuff for scenarios */
    00438           if (_saveload_mode != SLD_LOAD_SCENARIO && _saveload_mode != SLD_SAVE_SCENARIO) {
    00439             y += FONT_HEIGHT_NORMAL;
    00440             if (y > y_max) break;
    00441
    00442             /* Companies / AIs */
    00443             CompanyPropertiesMap::const_iterator end = _load_check_data.companies.End();
    00444             for (CompanyPropertiesMap::const_iterator it = _load_check_data.companies.Begin(); it != end; it++) {
    00445               SetDParam(0, it->first + 1);
    00446               const CompanyProperties &c = *it->second;
    00447               if (c.name != NULL) {
    00448                 SetDParam(1, STR_JUST_RAW_STRING);
    00449                 SetDParamStr(2, c.name);
    00450               } else {
    00451                 SetDParam(1, c.name_1);
    00452                 SetDParam(2, c.name_2);
    00453               }
    00454               DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_SAVELOAD_DETAIL_COMPANY_INDEX);
    00455               y += FONT_HEIGHT_NORMAL;
    00456               if (y > y_max) break;
    00457             }
    00458           }
    00459         }
    00460         break;
    00461       }
    00462     }
    00463   }
    00464
    00465   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
    00466   {
    00467     switch (widget) {
    00468       case WID_SL_BACKGROUND:
    00469         size->height = 2 * FONT_HEIGHT_NORMAL + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
    00470         break;
    00471
    00472       case WID_SL_DRIVES_DIRECTORIES_LIST:
    00473         resize->height = FONT_HEIGHT_NORMAL;
    00474         size->height = resize->height * 10 + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
    00475         break;
    00476       case WID_SL_SORT_BYNAME:
    00477       case WID_SL_SORT_BYDATE: {
    00478         Dimension d = GetStringBoundingBox(this->GetWidget<NWidgetCore>(widget)->widget_data);
    00479         d.width += padding.width + WD_SORTBUTTON_ARROW_WIDTH * 2; // Doubled since the string is centred and it also looks better.
    00480         d.height += padding.height;
    00481         *size = maxdim(*size, d);
    00482         break;
    00483       }
    00484     }
    00485   }
    00486
    00487   virtual void OnPaint()
    00488   {
    00489     if (_savegame_sort_dirty) {
    00490       _savegame_sort_dirty = false;
    00491       MakeSortedSaveGameList();
    00492     }
    00493
    00494     this->vscroll->SetCount(_fios_items.Length());
    00495     this->DrawWidgets();
    00496   }
    00497
    00498   virtual void OnClick(Point pt, int widget, int click_count)
    00499   {
    00500     switch (widget) {
    00501       case WID_SL_SORT_BYNAME: // Sort save names by name
    00502         _savegame_sort_order = (_savegame_sort_order == SORT_BY_NAME) ?
    00503           SORT_BY_NAME | SORT_DESCENDING : SORT_BY_NAME;
    00504         _savegame_sort_dirty = true;
    00505         this->SetDirty();
    00506         break;
    00507
    00508       case WID_SL_SORT_BYDATE: // Sort save names by date
    00509         _savegame_sort_order = (_savegame_sort_order == SORT_BY_DATE) ?
    00510           SORT_BY_DATE | SORT_DESCENDING : SORT_BY_DATE;
    00511         _savegame_sort_dirty = true;
    00512         this->SetDirty();
    00513         break;
    00514
    00515       case WID_SL_HOME_BUTTON: // OpenTTD 'button', jumps to OpenTTD directory
    00516         FiosBrowseTo(&o_dir);
    00517         this->InvalidateData();
    00518         break;
    00519
    00520       case WID_SL_LOAD_BUTTON:
    00521         if (this->selected != NULL && !_load_check_data.HasErrors() && (_load_check_data.grf_compatibility != GLC_NOT_FOUND || _settings_client.gui.UserIsAllowedToChangeNewGRFs())) {
    00522           _switch_mode = (_game_mode == GM_EDITOR) ? SM_LOAD_SCENARIO : SM_LOAD_GAME;
    00523
    00524           const char *name = FiosBrowseTo(this->selected);
    00525           SetFiosType(this->selected->type);
    00526
    00527           strecpy(_file_to_saveload.name, name, lastof(_file_to_saveload.name));
    00528           strecpy(_file_to_saveload.title, this->selected->title, lastof(_file_to_saveload.title));
    00529           ClearErrorMessages();
    00530           delete this;
    00531         }
    00532         break;
    00533
    00534       case WID_SL_NEWGRF_INFO:
    00535         if (_load_check_data.HasNewGrfs()) {
    00536           ShowNewGRFSettings(false, false, false, &_load_check_data.grfconfig);
    00537         }
    00538         break;
    00539
    00540       case WID_SL_MISSING_NEWGRFS:
    00541         if (!_network_available) {
    00542           ShowErrorMessage(STR_NETWORK_ERROR_NOTAVAILABLE, INVALID_STRING_ID, WL_ERROR);
    00543         } else {
    00544 #if defined(ENABLE_NETWORK)
    00545           ShowMissingContentWindow(_load_check_data.grfconfig);
    00546 #endif
    00547         }
    00548         break;
    00549
    00550       case WID_SL_DRIVES_DIRECTORIES_LIST: { // Click the listbox
    00551         int y = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_SL_DRIVES_DIRECTORIES_LIST, WD_FRAMERECT_TOP);
    00552         if (y == INT_MAX) return;
    00553
    00554         const FiosItem *file = _fios_items.Get(y);
    00555
    00556         const char *name = FiosBrowseTo(file);
    00557         if (name != NULL) {
    00558           if (click_count == 1) {
    00559             if (this->selected != file) {
    00560               this->selected = file;
    00561               _load_check_data.Clear();
    00562
    00563               if (file->type == FIOS_TYPE_FILE || file->type == FIOS_TYPE_SCENARIO) {
    00564                 SaveOrLoad(name, SL_LOAD_CHECK, NO_DIRECTORY, false);
    00565               }
    00566
    00567               this->InvalidateData(1);
    00568             }
    00569             if (_saveload_mode == SLD_SAVE_GAME || _saveload_mode == SLD_SAVE_SCENARIO || _saveload_mode == SLD_SAVE_HEIGHTMAP) {
    00570               /* Copy clicked name to editbox */
    00571               this->filename_editbox.text.Assign(file->title);
    00572               this->SetWidgetDirty(WID_SL_SAVE_OSK_TITLE);
    00573             }
    00574           } else if (!_load_check_data.HasErrors()) {
    00575             this->selected = file;
    00576             if (_saveload_mode == SLD_LOAD_GAME || _saveload_mode == SLD_LOAD_SCENARIO) {
    00577               this->OnClick(pt, WID_SL_LOAD_BUTTON, 1);
    00578             } else if (_saveload_mode == SLD_LOAD_HEIGHTMAP) {
    00579               SetFiosType(file->type);
    00580               strecpy(_file_to_saveload.name, name, lastof(_file_to_saveload.name));
    00581               strecpy(_file_to_saveload.title, file->title, lastof(_file_to_saveload.title));
    00582
    00583               delete this;
    00584               ShowHeightmapLoad();
    00585             }
    00586           }
    00587         } else {
    00588           /* Changed directory, need refresh. */
    00589           this->InvalidateData();
    00590         }
    00591         break;
    00592       }
    00593
    00594       case WID_SL_CONTENT_DOWNLOAD:
    00595         if (!_network_available) {
    00596           ShowErrorMessage(STR_NETWORK_ERROR_NOTAVAILABLE, INVALID_STRING_ID, WL_ERROR);
    00597         } else {
    00598 #if defined(ENABLE_NETWORK)
    00599           switch (_saveload_mode) {
    00600             default: NOT_REACHED();
    00601             case SLD_LOAD_SCENARIO:  ShowNetworkContentListWindow(NULL, CONTENT_TYPE_SCENARIO);  break;
    00602             case SLD_LOAD_HEIGHTMAP: ShowNetworkContentListWindow(NULL, CONTENT_TYPE_HEIGHTMAP); break;
    00603           }
    00604 #endif
    00605         }
    00606         break;
    00607
    00608       case WID_SL_DELETE_SELECTION: // Delete
    00609         break;
    00610
    00611       case WID_SL_SAVE_GAME: // Save game
    00612         /* Note, this is also called via the OSK; and we need to lower the button. */
    00613         this->HandleButtonClick(WID_SL_SAVE_GAME);
    00614         break;
    00615     }
    00616   }
    00617
    00618   virtual EventState OnKeyPress(uint16 key, uint16 keycode)
    00619   {
    00620     if (keycode == WKC_ESC) {
    00621       delete this;
    00622       return ES_HANDLED;
    00623     }
    00624
    00625     return ES_NOT_HANDLED;
    00626   }
    00627
    00628   virtual void OnTimeout()
    00629   {
    00630     /* This test protects against using widgets 11 and 12 which are only available
    00631      * in those saveload modes. */
    00632     if (!(_saveload_mode == SLD_SAVE_GAME || _saveload_mode == SLD_SAVE_SCENARIO || _saveload_mode == SLD_SAVE_HEIGHTMAP)) return;
    00633
    00634     if (this->IsWidgetLowered(WID_SL_DELETE_SELECTION)) { // Delete button clicked
    00635       if (!FiosDelete(this->filename_editbox.text.buf)) {
    00636         ShowErrorMessage(STR_ERROR_UNABLE_TO_DELETE_FILE, INVALID_STRING_ID, WL_ERROR);
    00637       } else {
    00638         this->InvalidateData();
    00639         /* Reset file name to current date on successful delete */
    00640         if (_saveload_mode == SLD_SAVE_GAME) GenerateFileName();
    00641       }
    00642     } else if (this->IsWidgetLowered(WID_SL_SAVE_GAME)) { // Save button clicked
    00643       if (_saveload_mode  == SLD_SAVE_GAME || _saveload_mode == SLD_SAVE_SCENARIO) {
    00644         _switch_mode = SM_SAVE_GAME;
    00645         FiosMakeSavegameName(_file_to_saveload.name, this->filename_editbox.text.buf, sizeof(_file_to_saveload.name));
    00646       } else {
    00647         _switch_mode = SM_SAVE_HEIGHTMAP;
    00648         FiosMakeHeightmapName(_file_to_saveload.name, this->filename_editbox.text.buf, sizeof(_file_to_saveload.name));
    00649       }
    00650
    00651       /* In the editor set up the vehicle engines correctly (date might have changed) */
    00652       if (_game_mode == GM_EDITOR) StartupEngines();
    00653     }
    00654   }
    00655
    00656   virtual void OnResize()
    00657   {
    00658     this->vscroll->SetCapacityFromWidget(this, WID_SL_DRIVES_DIRECTORIES_LIST);
    00659   }
    00660
    00666   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
    00667   {
    00668     switch (data) {
    00669       case 0:
    00670         /* Rescan files */
    00671         this->selected = NULL;
    00672         _load_check_data.Clear();
    00673         if (!gui_scope) break;
    00674         BuildFileList();
    00675         /* FALL THROUGH */
    00676       case 1:
    00677         /* Selection changes */
    00678         if (!gui_scope) break;
    00679         if (_saveload_mode == SLD_LOAD_GAME || _saveload_mode == SLD_LOAD_SCENARIO) {
    00680           this->SetWidgetDisabledState(WID_SL_LOAD_BUTTON,
    00681               this->selected == NULL || _load_check_data.HasErrors() || !(_load_check_data.grf_compatibility != GLC_NOT_FOUND || _settings_client.gui.UserIsAllowedToChangeNewGRFs()));
    00682           this->SetWidgetDisabledState(WID_SL_NEWGRF_INFO,
    00683               !_load_check_data.HasNewGrfs());
    00684           this->SetWidgetDisabledState(WID_SL_MISSING_NEWGRFS,
    00685               !_load_check_data.HasNewGrfs() || _load_check_data.grf_compatibility == GLC_ALL_GOOD);
    00686         }
    00687         break;
    00688       case 2:
    00689         /* _fios_items changed */
    00690         this->vscroll->SetCount(_fios_items.Length());
    00691         this->selected = NULL;
    00692         _load_check_data.Clear();
    00693         break;
    00694     }
    00695   }
    00696 };
    00697
    00699 static const WindowDesc _load_dialog_desc(
    00700   WDP_CENTER, 500, 294,
    00701   WC_SAVELOAD, WC_NONE,
    00702   0,
    00703   _nested_load_dialog_widgets, lengthof(_nested_load_dialog_widgets)
    00704 );
    00705
    00707 static const WindowDesc _load_heightmap_dialog_desc(
    00708   WDP_CENTER, 257, 320,
    00709   WC_SAVELOAD, WC_NONE,
    00710   0,
    00711   _nested_load_heightmap_dialog_widgets, lengthof(_nested_load_heightmap_dialog_widgets)
    00712 );
    00713
    00715 static const WindowDesc _save_dialog_desc(
    00716   WDP_CENTER, 500, 294,
    00717   WC_SAVELOAD, WC_NONE,
    00718   0,
    00719   _nested_save_dialog_widgets, lengthof(_nested_save_dialog_widgets)
    00720 );
    00721
    00726 static const FileType _file_modetotype[] = {
    00727   FT_SAVEGAME,  // used for SLD_LOAD_GAME
    00728   FT_SCENARIO,  // used for SLD_LOAD_SCENARIO
    00729   FT_SAVEGAME,  // used for SLD_SAVE_GAME
    00730   FT_SCENARIO,  // used for SLD_SAVE_SCENARIO
    00731   FT_HEIGHTMAP, // used for SLD_LOAD_HEIGHTMAP
    00732   FT_HEIGHTMAP, // used for SLD_SAVE_HEIGHTMAP
    00733 };
    00734
    00739 void ShowSaveLoadDialog(SaveLoadDialogMode mode)
    00740 {
    00741   DeleteWindowById(WC_SAVELOAD, 0);
    00742
    00743   const WindowDesc *sld;
    00744   switch (mode) {
    00745     case SLD_SAVE_GAME:
    00746     case SLD_SAVE_SCENARIO:
    00747     case SLD_SAVE_HEIGHTMAP:
    00748       sld = &_save_dialog_desc; break;
    00749     case SLD_LOAD_HEIGHTMAP:
    00750       sld = &_load_heightmap_dialog_desc; break;
    00751     default:
    00752       sld = &_load_dialog_desc; break;
    00753   }
    00754
    00755   _saveload_mode = mode;
    00756   _file_to_saveload.filetype = _file_modetotype[mode];
    00757
    00758   new SaveLoadWindow(sld, mode);
    00759 }
    00760
    00761 void SetFiosType(const byte fiostype)
    00762 {
    00763   switch (fiostype) {
    00764     case FIOS_TYPE_FILE:
    00765     case FIOS_TYPE_SCENARIO:
    00766       _file_to_saveload.mode = SL_LOAD;
    00767       break;
    00768
    00769     case FIOS_TYPE_OLDFILE:
    00770     case FIOS_TYPE_OLD_SCENARIO:
    00771       _file_to_saveload.mode = SL_OLD_LOAD;
    00772       break;
    00773
    00774 #ifdef WITH_PNG
    00775     case FIOS_TYPE_PNG:
    00776       _file_to_saveload.mode = SL_PNG;
    00777       break;
    00778 #endif /* WITH_PNG */
    00779
    00780     case FIOS_TYPE_BMP:
    00781       _file_to_saveload.mode = SL_BMP;
    00782       break;
    00783
    00784     default:
    00785       _file_to_saveload.mode = SL_INVALID;
    00786       break;
    00787   }
    00788 }