# fios_gui.cpp

Go to the documentation of this file.

    00001 /* $Id: fios_gui.cpp 22467 2011-05-15 18:06:13Z rubidium $ */
    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   /* Invalidate saveload window */
    00216   InvalidateWindowData(WC_SAVELOAD, 0, 2, true);
    00217 }
    00218
    00219 static void MakeSortedSaveGameList()
    00220 {
    00221   uint sort_start = 0;
    00222   uint sort_end = 0;
    00223
    00224   /* Directories are always above the files (FIOS_TYPE_DIR)
    00225    * Drives (A:\ (windows only) are always under the files (FIOS_TYPE_DRIVE)
    00226    * Only sort savegames/scenarios, not directories
    00227    */
    00228   for (const FiosItem *item = _fios_items.Begin(); item != _fios_items.End(); item++) {
    00229     switch (item->type) {
    00230       case FIOS_TYPE_DIR:    sort_start++; break;
    00231       case FIOS_TYPE_PARENT: sort_start++; break;
    00232       case FIOS_TYPE_DRIVE:  sort_end++;   break;
    00233       default: break;
    00234     }
    00235   }
    00236
    00237   uint s_amount = _fios_items.Length() - sort_start - sort_end;
    00238   QSortT(_fios_items.Get(sort_start), s_amount, CompareFiosItems);
    00239 }
    00240
    00241 struct SaveLoadWindow : public QueryStringBaseWindow {
    00242 private:
    00243   FiosItem o_dir;
    00244   const FiosItem *selected;
    00245   Scrollbar *vscroll;
    00246 public:
    00247
    00248   void GenerateFileName()
    00249   {
    00250     GenerateDefaultSaveName(this->edit_str_buf, &this->edit_str_buf[this->edit_str_size - 1]);
    00251   }
    00252
    00253   SaveLoadWindow(const WindowDesc *desc, SaveLoadDialogMode mode) : QueryStringBaseWindow(64)
    00254   {
    00255     static const StringID saveload_captions[] = {
    00256       STR_SAVELOAD_LOAD_CAPTION,
    00257       STR_SAVELOAD_LOAD_SCENARIO,
    00258       STR_SAVELOAD_SAVE_CAPTION,
    00259       STR_SAVELOAD_SAVE_SCENARIO,
    00260       STR_SAVELOAD_LOAD_HEIGHTMAP,
    00261     };
    00262     assert((uint)mode < lengthof(saveload_captions));
    00263
    00264     /* Use an array to define what will be the current file type being handled
    00265      * by current file mode */
    00266     switch (mode) {
    00267       case SLD_SAVE_GAME:     this->GenerateFileName(); break;
    00268       case SLD_SAVE_SCENARIO: strecpy(this->edit_str_buf, "UNNAMED", &this->edit_str_buf[edit_str_size - 1]); break;
    00269       default:                break;
    00270     }
    00271
    00272     this->afilter = CS_ALPHANUMERAL;
    00273     InitializeTextBuffer(&this->text, this->edit_str_buf, this->edit_str_size, 240);
    00274
    00275     this->CreateNestedTree(desc, true);
    00276     if (mode == SLD_LOAD_GAME) this->GetWidget<NWidgetStacked>(SLWW_CONTENT_DOWNLOAD_SEL)->SetDisplayedPlane(SZSP_HORIZONTAL);
    00277     this->GetWidget<NWidgetCore>(SLWW_WINDOWTITLE)->widget_data = saveload_captions[mode];
    00278     this->vscroll = this->GetScrollbar(SLWW_SCROLLBAR);
    00279
    00280     this->FinishInitNested(desc, 0);
    00281
    00282     this->LowerWidget(SLWW_DRIVES_DIRECTORIES_LIST);
    00283
    00284     /* pause is only used in single-player, non-editor mode, non-menu mode. It
    00285      * will be unpaused in the WE_DESTROY event handler. */
    00286     if (_game_mode != GM_MENU && !_networking && _game_mode != GM_EDITOR) {
    00287       DoCommandP(0, PM_PAUSED_SAVELOAD, 1, CMD_PAUSE);
    00288     }
    00289     SetObjectToPlace(SPR_CURSOR_ZZZ, PAL_NONE, HT_NONE, WC_MAIN_WINDOW, 0);
    00290
    00291     this->OnInvalidateData(0);
    00292
    00293     ResetObjectToPlace();
    00294
    00295     o_dir.type = FIOS_TYPE_DIRECT;
    00296     switch (_saveload_mode) {
    00297       case SLD_SAVE_GAME:
    00298       case SLD_LOAD_GAME:
    00299         FioGetDirectory(o_dir.name, lengthof(o_dir.name), SAVE_DIR);
    00300         break;
    00301
    00302       case SLD_SAVE_SCENARIO:
    00303       case SLD_LOAD_SCENARIO:
    00304         FioGetDirectory(o_dir.name, lengthof(o_dir.name), SCENARIO_DIR);
    00305         break;
    00306
    00307       case SLD_LOAD_HEIGHTMAP:
    00308         FioGetDirectory(o_dir.name, lengthof(o_dir.name), HEIGHTMAP_DIR);
    00309         break;
    00310
    00311       default:
    00312         strecpy(o_dir.name, _personal_dir, lastof(o_dir.name));
    00313     }
    00314
    00315     /* Focus the edit box by default in the save windows */
    00316     if (_saveload_mode == SLD_SAVE_GAME || _saveload_mode == SLD_SAVE_SCENARIO) {
    00317       this->SetFocusedWidget(SLWW_SAVE_OSK_TITLE);
    00318     }
    00319   }
    00320
    00321   virtual ~SaveLoadWindow()
    00322   {
    00323     /* pause is only used in single-player, non-editor mode, non menu mode */
    00324     if (!_networking && _game_mode != GM_EDITOR && _game_mode != GM_MENU) {
    00325       DoCommandP(0, PM_PAUSED_SAVELOAD, 0, CMD_PAUSE);
    00326     }
    00327     FiosFreeSavegameList();
    00328   }
    00329
    00330   virtual void DrawWidget(const Rect &r, int widget) const
    00331   {
    00332     switch (widget) {
    00333       case SLWW_SORT_BYNAME:
    00334       case SLWW_SORT_BYDATE:
    00335         if (((_savegame_sort_order & SORT_BY_NAME) != 0) == (widget == SLWW_SORT_BYNAME)) {
    00336           this->DrawSortButtonState(widget, _savegame_sort_order & SORT_DESCENDING ? SBS_DOWN : SBS_UP);
    00337         }
    00338         break;
    00339
    00340       case SLWW_BACKGROUND: {
    00341         static const char *path = NULL;
    00342         static StringID str = STR_ERROR_UNABLE_TO_READ_DRIVE;
    00343         static uint64 tot = 0;
    00344
    00345         if (_fios_path_changed) {
    00346           str = FiosGetDescText(&path, &tot);
    00347           _fios_path_changed = false;
    00348         }
    00349
    00350         if (str != STR_ERROR_UNABLE_TO_READ_DRIVE) SetDParam(0, tot);
    00351         DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + FONT_HEIGHT_NORMAL + WD_FRAMERECT_TOP, str);
    00352         DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + WD_FRAMERECT_TOP, path, TC_BLACK);
    00353         break;
    00354       }
    00355
    00356       case SLWW_DRIVES_DIRECTORIES_LIST: {
    00357         GfxFillRect(r.left + 1, r.top + 1, r.right, r.bottom, 0xD7);
    00358
    00359         uint y = r.top + WD_FRAMERECT_TOP;
    00360         for (uint pos = this->vscroll->GetPosition(); pos < _fios_items.Length(); pos++) {
    00361           const FiosItem *item = _fios_items.Get(pos);
    00362
    00363           if (item == this->selected) {
    00364             GfxFillRect(r.left + 1, y, r.right, y + this->resize.step_height, 156);
    00365           }
    00366           DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, item->title, _fios_colours[item->type]);
    00367           y += this->resize.step_height;
    00368           if (y >= this->vscroll->GetCapacity() * this->resize.step_height + r.top + WD_FRAMERECT_TOP) break;
    00369         }
    00370         break;
    00371       }
    00372
    00373       case SLWW_DETAILS: {
    00374         GfxFillRect(r.left + WD_FRAMERECT_LEFT, r.top + WD_FRAMERECT_TOP,
    00375             r.right - WD_FRAMERECT_RIGHT, r.top + FONT_HEIGHT_NORMAL * 2 + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM, 0x0A);
    00376         DrawString(r.left, r.right, r.top + FONT_HEIGHT_NORMAL / 2 + WD_FRAMERECT_TOP, STR_SAVELOAD_DETAIL_CAPTION, TC_FROMSTRING, SA_HOR_CENTER);
    00377
    00378         if (this->selected == NULL) break;
    00379
    00380         uint y = r.top + FONT_HEIGHT_NORMAL * 2 + WD_PAR_VSEP_NORMAL + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
    00381         uint y_max = r.bottom - FONT_HEIGHT_NORMAL - WD_FRAMERECT_BOTTOM;
    00382
    00383         if (y > y_max) break;
    00384         if (!_load_check_data.checkable) {
    00385           /* Old savegame, no information available */
    00386           DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_SAVELOAD_DETAIL_NOT_AVAILABLE);
    00387           y += FONT_HEIGHT_NORMAL;
    00388         } else if (_load_check_data.error != INVALID_STRING_ID) {
    00389           /* Incompatible / broken savegame */
    00390           SetDParamStr(0, _load_check_data.error_data);
    00391           y = DrawStringMultiLine(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT,
    00392               y, r.bottom - WD_FRAMERECT_BOTTOM, _load_check_data.error, TC_RED);
    00393         } else {
    00394           /* Mapsize */
    00395           SetDParam(0, _load_check_data.map_size_x);
    00396           SetDParam(1, _load_check_data.map_size_y);
    00397           DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_SERVER_LIST_MAP_SIZE);
    00398           y += FONT_HEIGHT_NORMAL;
    00399           if (y > y_max) break;
    00400
    00401           /* Climate */
    00402           byte landscape = _load_check_data.settings.game_creation.landscape;
    00403           if (landscape < NUM_LANDSCAPE) {
    00404             SetDParam(0, STR_CHEAT_SWITCH_CLIMATE_TEMPERATE_LANDSCAPE + landscape);
    00405             DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_SERVER_LIST_LANDSCAPE);
    00406             y += FONT_HEIGHT_NORMAL;
    00407           }
    00408
    00409           y += WD_PAR_VSEP_NORMAL;
    00410           if (y > y_max) break;
    00411
    00412           /* Start date (if available) */
    00413           if (_load_check_data.settings.game_creation.starting_year != 0) {
    00414             SetDParam(0, ConvertYMDToDate(_load_check_data.settings.game_creation.starting_year, 0, 1));
    00415             DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_SERVER_LIST_START_DATE);
    00416             y += FONT_HEIGHT_NORMAL;
    00417           }
    00418           if (y > y_max) break;
    00419
    00420           /* Hide current date for scenarios */
    00421           if (_saveload_mode != SLD_LOAD_SCENARIO && _saveload_mode != SLD_SAVE_SCENARIO) {
    00422             /* Current date */
    00423             SetDParam(0, _load_check_data.current_date);
    00424             DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_SERVER_LIST_CURRENT_DATE);
    00425             y += FONT_HEIGHT_NORMAL;
    00426           }
    00427
    00428           /* Hide the NewGRF stuff when saving. We also hide the button. */
    00429           if (_saveload_mode == SLD_LOAD_GAME || _saveload_mode == SLD_LOAD_SCENARIO) {
    00430             y += WD_PAR_VSEP_NORMAL;
    00431             if (y > y_max) break;
    00432
    00433             /* NewGrf compatibility */
    00434             SetDParam(0, _load_check_data.grfconfig == NULL ? STR_NEWGRF_LIST_NONE :
    00435                 STR_NEWGRF_LIST_ALL_FOUND + _load_check_data.grf_compatibility);
    00436             DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_SAVELOAD_DETAIL_GRFSTATUS);
    00437             y += FONT_HEIGHT_NORMAL;
    00438           }
    00439           if (y > y_max) break;
    00440
    00441           /* Hide the company stuff for scenarios */
    00442           if (_saveload_mode != SLD_LOAD_SCENARIO && _saveload_mode != SLD_SAVE_SCENARIO) {
    00443             y += FONT_HEIGHT_NORMAL;
    00444             if (y > y_max) break;
    00445
    00446             /* Companies / AIs */
    00447             CompanyPropertiesMap::const_iterator end = _load_check_data.companies.End();
    00448             for (CompanyPropertiesMap::const_iterator it = _load_check_data.companies.Begin(); it != end; it++) {
    00449               SetDParam(0, it->first + 1);
    00450               const CompanyProperties &c = *it->second;
    00451               if (c.name != NULL) {
    00452                 SetDParam(1, STR_JUST_RAW_STRING);
    00453                 SetDParamStr(2, c.name);
    00454               } else {
    00455                 SetDParam(1, c.name_1);
    00456                 SetDParam(2, c.name_2);
    00457               }
    00458               DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_SAVELOAD_DETAIL_COMPANY_INDEX);
    00459               y += FONT_HEIGHT_NORMAL;
    00460               if (y > y_max) break;
    00461             }
    00462           }
    00463         }
    00464         break;
    00465       }
    00466     }
    00467   }
    00468
    00469   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
    00470   {
    00471     switch (widget) {
    00472       case SLWW_BACKGROUND:
    00473         size->height = 2 * FONT_HEIGHT_NORMAL + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
    00474         break;
    00475
    00476       case SLWW_DRIVES_DIRECTORIES_LIST:
    00477         resize->height = FONT_HEIGHT_NORMAL;
    00478         size->height = resize->height * 10 + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
    00479         break;
    00480       case SLWW_SORT_BYNAME:
    00481       case SLWW_SORT_BYDATE: {
    00482         Dimension d = GetStringBoundingBox(this->GetWidget<NWidgetCore>(widget)->widget_data);
    00483         d.width += padding.width + WD_SORTBUTTON_ARROW_WIDTH * 2; // Doubled since the string is centred and it also looks better.
    00484         d.height += padding.height;
    00485         *size = maxdim(*size, d);
    00486         break;
    00487       }
    00488     }
    00489   }
    00490
    00491   virtual void OnPaint()
    00492   {
    00493     if (_savegame_sort_dirty) {
    00494       _savegame_sort_dirty = false;
    00495       MakeSortedSaveGameList();
    00496     }
    00497
    00498     this->vscroll->SetCount(_fios_items.Length());
    00499     this->DrawWidgets();
    00500
    00501     if (_saveload_mode == SLD_SAVE_GAME || _saveload_mode == SLD_SAVE_SCENARIO) {
    00502       this->DrawEditBox(SLWW_SAVE_OSK_TITLE);
    00503     }
    00504   }
    00505
    00506   virtual void OnClick(Point pt, int widget, int click_count)
    00507   {
    00508     switch (widget) {
    00509       case SLWW_SORT_BYNAME: // Sort save names by name
    00510         _savegame_sort_order = (_savegame_sort_order == SORT_BY_NAME) ?
    00511           SORT_BY_NAME | SORT_DESCENDING : SORT_BY_NAME;
    00512         _savegame_sort_dirty = true;
    00513         this->SetDirty();
    00514         break;
    00515
    00516       case SLWW_SORT_BYDATE: // Sort save names by date
    00517         _savegame_sort_order = (_savegame_sort_order == SORT_BY_DATE) ?
    00518           SORT_BY_DATE | SORT_DESCENDING : SORT_BY_DATE;
    00519         _savegame_sort_dirty = true;
    00520         this->SetDirty();
    00521         break;
    00522
    00523       case SLWW_HOME_BUTTON: // OpenTTD 'button', jumps to OpenTTD directory
    00524         FiosBrowseTo(&o_dir);
    00525         this->InvalidateData();
    00526         break;
    00527
    00528       case SLWW_LOAD_BUTTON:
    00529         if (this->selected != NULL && !_load_check_data.HasErrors() && (_load_check_data.grf_compatibility != GLC_NOT_FOUND || _settings_client.gui.UserIsAllowedToChangeNewGRFs())) {
    00530           _switch_mode = (_game_mode == GM_EDITOR) ? SM_LOAD_SCENARIO : SM_LOAD;
    00531
    00532           const char *name = FiosBrowseTo(this->selected);
    00533           SetFiosType(this->selected->type);
    00534
    00535           strecpy(_file_to_saveload.name, name, lastof(_file_to_saveload.name));
    00536           strecpy(_file_to_saveload.title, this->selected->title, lastof(_file_to_saveload.title));
    00537
    00538           delete this;
    00539         }
    00540         break;
    00541
    00542       case SLWW_NEWGRF_INFO:
    00543         if (_load_check_data.HasNewGrfs()) {
    00544           ShowNewGRFSettings(false, false, false, &_load_check_data.grfconfig);
    00545         }
    00546         break;
    00547
    00548       case SLWW_DRIVES_DIRECTORIES_LIST: { // Click the listbox
    00549         int y = this->vscroll->GetScrolledRowFromWidget(pt.y, this, SLWW_DRIVES_DIRECTORIES_LIST, WD_FRAMERECT_TOP);
    00550         if (y == INT_MAX) return;
    00551
    00552         const FiosItem *file = _fios_items.Get(y);
    00553
    00554         const char *name = FiosBrowseTo(file);
    00555         if (name != NULL) {
    00556           if (click_count == 1) {
    00557             if (this->selected != file) {
    00558               this->selected = file;
    00559               _load_check_data.Clear();
    00560
    00561               if (file->type == FIOS_TYPE_FILE || file->type == FIOS_TYPE_SCENARIO) {
    00562                 SaveOrLoad(name, SL_LOAD_CHECK, NO_DIRECTORY, false);
    00563               }
    00564
    00565               this->InvalidateData(1);
    00566             }
    00567             if (_saveload_mode == SLD_SAVE_GAME || _saveload_mode == SLD_SAVE_SCENARIO) {
    00568               /* Copy clicked name to editbox */
    00569               ttd_strlcpy(this->text.buf, file->title, this->text.max_bytes);
    00570               UpdateTextBufferSize(&this->text);
    00571               this->SetWidgetDirty(SLWW_SAVE_OSK_TITLE);
    00572             }
    00573           } else if (!_load_check_data.HasErrors()) {
    00574             this->selected = file;
    00575             if (_saveload_mode == SLD_LOAD_GAME || _saveload_mode == SLD_LOAD_SCENARIO) {
    00576               this->OnClick(pt, SLWW_LOAD_BUTTON, 1);
    00577             } else if (_saveload_mode == SLD_LOAD_HEIGHTMAP) {
    00578               SetFiosType(file->type);
    00579               strecpy(_file_to_saveload.name, name, lastof(_file_to_saveload.name));
    00580               strecpy(_file_to_saveload.title, file->title, lastof(_file_to_saveload.title));
    00581
    00582               delete this;
    00583               ShowHeightmapLoad();
    00584             }
    00585           }
    00586         } else {
    00587           /* Changed directory, need refresh. */
    00588           this->InvalidateData();
    00589         }
    00590         break;
    00591       }
    00592
    00593       case SLWW_CONTENT_DOWNLOAD:
    00594         if (!_network_available) {
    00595           ShowErrorMessage(STR_NETWORK_ERROR_NOTAVAILABLE, INVALID_STRING_ID, WL_ERROR);
    00596         } else {
    00597 #if defined(ENABLE_NETWORK)
    00598           switch (_saveload_mode) {
    00599             default: NOT_REACHED();
    00600             case SLD_LOAD_SCENARIO:  ShowNetworkContentListWindow(NULL, CONTENT_TYPE_SCENARIO);  break;
    00601             case SLD_LOAD_HEIGHTMAP: ShowNetworkContentListWindow(NULL, CONTENT_TYPE_HEIGHTMAP); break;
    00602           }
    00603 #endif
    00604         }
    00605         break;
    00606
    00607       case SLWW_DELETE_SELECTION: case SLWW_SAVE_GAME: // Delete, Save game
    00608         break;
    00609     }
    00610   }
    00611
    00612   virtual void OnMouseLoop()
    00613   {
    00614     if (_saveload_mode == SLD_SAVE_GAME || _saveload_mode == SLD_SAVE_SCENARIO) {
    00615       this->HandleEditBox(SLWW_SAVE_OSK_TITLE);
    00616     }
    00617   }
    00618
    00619   virtual EventState OnKeyPress(uint16 key, uint16 keycode)
    00620   {
    00621     if (keycode == WKC_ESC) {
    00622       delete this;
    00623       return ES_HANDLED;
    00624     }
    00625
    00626     EventState state = ES_NOT_HANDLED;
    00627     if ((_saveload_mode == SLD_SAVE_GAME || _saveload_mode == SLD_SAVE_SCENARIO) &&
    00628         this->HandleEditBoxKey(SLWW_SAVE_OSK_TITLE, key, keycode, state) == HEBR_CONFIRM) {
    00629       this->HandleButtonClick(SLWW_SAVE_GAME);
    00630     }
    00631
    00632     return state;
    00633   }
    00634
    00635   virtual void OnTimeout()
    00636   {
    00637     /* This test protects against using widgets 11 and 12 which are only available
    00638      * in those two saveload mode */
    00639     if (!(_saveload_mode == SLD_SAVE_GAME || _saveload_mode == SLD_SAVE_SCENARIO)) return;
    00640
    00641     if (this->IsWidgetLowered(SLWW_DELETE_SELECTION)) { // Delete button clicked
    00642       if (!FiosDelete(this->text.buf)) {
    00643         ShowErrorMessage(STR_ERROR_UNABLE_TO_DELETE_FILE, INVALID_STRING_ID, WL_ERROR);
    00644       } else {
    00645         this->InvalidateData();
    00646         /* Reset file name to current date on successful delete */
    00647         if (_saveload_mode == SLD_SAVE_GAME) GenerateFileName();
    00648       }
    00649
    00650       UpdateTextBufferSize(&this->text);
    00651     } else if (this->IsWidgetLowered(SLWW_SAVE_GAME)) { // Save button clicked
    00652       _switch_mode = SM_SAVE;
    00653       FiosMakeSavegameName(_file_to_saveload.name, this->text.buf, sizeof(_file_to_saveload.name));
    00654
    00655       /* In the editor set up the vehicle engines correctly (date might have changed) */
    00656       if (_game_mode == GM_EDITOR) StartupEngines();
    00657     }
    00658   }
    00659
    00660   virtual void OnResize()
    00661   {
    00662     this->vscroll->SetCapacityFromWidget(this, SLWW_DRIVES_DIRECTORIES_LIST);
    00663   }
    00664
    00670   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
    00671   {
    00672     switch (data) {
    00673       case 0:
    00674         /* Rescan files */
    00675         this->selected = NULL;
    00676         _load_check_data.Clear();
    00677         if (!gui_scope) break;
    00678         BuildFileList();
    00679         /* FALL THROUGH */
    00680       case 1:
    00681         /* Selection changes */
    00682         if (!gui_scope) break;
    00683         if (_saveload_mode == SLD_LOAD_GAME || _saveload_mode == SLD_LOAD_SCENARIO) {
    00684           this->SetWidgetDisabledState(SLWW_LOAD_BUTTON,
    00685               this->selected == NULL || _load_check_data.HasErrors() || !(_load_check_data.grf_compatibility != GLC_NOT_FOUND || _settings_client.gui.UserIsAllowedToChangeNewGRFs()));
    00686           this->SetWidgetDisabledState(SLWW_NEWGRF_INFO,
    00687               !_load_check_data.HasNewGrfs());
    00688         }
    00689         break;
    00690       case 2:
    00691         /* _fios_items changed */
    00692         this->vscroll->SetCount(_fios_items.Length());
    00693         this->selected = NULL;
    00694         _load_check_data.Clear();
    00695         break;
    00696     }
    00697   }
    00698 };
    00699
    00701 static const WindowDesc _load_dialog_desc(
    00702   WDP_CENTER, 500, 294,
    00703   WC_SAVELOAD, WC_NONE,
    00704   WDF_UNCLICK_BUTTONS,
    00705   _nested_load_dialog_widgets, lengthof(_nested_load_dialog_widgets)
    00706 );
    00707
    00709 static const WindowDesc _load_heightmap_dialog_desc(
    00710   WDP_CENTER, 257, 320,
    00711   WC_SAVELOAD, WC_NONE,
    00712   WDF_UNCLICK_BUTTONS,
    00713   _nested_load_heightmap_dialog_widgets, lengthof(_nested_load_heightmap_dialog_widgets)
    00714 );
    00715
    00717 static const WindowDesc _save_dialog_desc(
    00718   WDP_CENTER, 500, 294,
    00719   WC_SAVELOAD, WC_NONE,
    00720   WDF_UNCLICK_BUTTONS,
    00721   _nested_save_dialog_widgets, lengthof(_nested_save_dialog_widgets)
    00722 );
    00723
    00728 static const FileType _file_modetotype[] = {
    00729   FT_SAVEGAME,
    00730   FT_SCENARIO,
    00731   FT_SAVEGAME,
    00732   FT_SCENARIO,
    00733   FT_HEIGHTMAP,
    00734 };
    00735
    00736 void ShowSaveLoadDialog(SaveLoadDialogMode mode)
    00737 {
    00738   DeleteWindowById(WC_SAVELOAD, 0);
    00739
    00740   const WindowDesc *sld;
    00741   switch (mode) {
    00742     case SLD_SAVE_GAME:
    00743     case SLD_SAVE_SCENARIO:
    00744       sld = &_save_dialog_desc; break;
    00745     case SLD_LOAD_HEIGHTMAP:
    00746       sld = &_load_heightmap_dialog_desc; break;
    00747     default:
    00748       sld = &_load_dialog_desc; break;
    00749   }
    00750
    00751   _saveload_mode = mode;
    00752   _file_to_saveload.filetype = _file_modetotype[mode];
    00753
    00754   new SaveLoadWindow(sld, mode);
    00755 }
    00756
    00757 void SetFiosType(const byte fiostype)
    00758 {
    00759   switch (fiostype) {
    00760     case FIOS_TYPE_FILE:
    00761     case FIOS_TYPE_SCENARIO:
    00762       _file_to_saveload.mode = SL_LOAD;
    00763       break;
    00764
    00765     case FIOS_TYPE_OLDFILE:
    00766     case FIOS_TYPE_OLD_SCENARIO:
    00767       _file_to_saveload.mode = SL_OLD_LOAD;
    00768       break;
    00769
    00770 #ifdef WITH_PNG
    00771     case FIOS_TYPE_PNG:
    00772       _file_to_saveload.mode = SL_PNG;
    00773       break;
    00774 #endif /* WITH_PNG */
    00775
    00776     case FIOS_TYPE_BMP:
    00777       _file_to_saveload.mode = SL_BMP;
    00778       break;
    00779
    00780     default:
    00781       _file_to_saveload.mode = SL_INVALID;
    00782       break;
    00783   }
    00784 }