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