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