# fios_gui.cpp

Go to the documentation of this file.

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