# fios_gui.cpp

Go to the documentation of this file.

    00001 /* $Id: fios_gui.cpp 26652 2014-06-17 19:08:07Z frosch $ */
    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, NC_EQUALSIZE),
    00137         NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SL_CONTENT_DOWNLOAD), SetResize(1, 0), SetFill(1, 0),
    00138             SetDataTip(STR_INTRO_ONLINE_CONTENT, STR_INTRO_TOOLTIP_ONLINE_CONTENT),
    00139         NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SL_LOAD_BUTTON), SetResize(1, 0), SetFill(1, 0),
    00140             SetDataTip(STR_SAVELOAD_LOAD_BUTTON, STR_SAVELOAD_LOAD_HEIGHTMAP_TOOLTIP),
    00141         NWidget(WWT_RESIZEBOX, COLOUR_GREY),
    00142       EndContainer(),
    00143     EndContainer(),
    00144   EndContainer(),
    00145 };
    00146
    00148 static const NWidgetPart _nested_save_dialog_widgets[] = {
    00149   NWidget(NWID_HORIZONTAL),
    00150     NWidget(WWT_CLOSEBOX, COLOUR_GREY),
    00151     NWidget(WWT_CAPTION, COLOUR_GREY, WID_SL_CAPTION),
    00152     NWidget(WWT_DEFSIZEBOX, COLOUR_GREY),
    00153   EndContainer(),
    00154   NWidget(WWT_PANEL, COLOUR_GREY, WID_SL_BACKGROUND), SetFill(1, 0), SetResize(1, 0), EndContainer(),
    00155   NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
    00156     NWidget(NWID_VERTICAL),
    00157       NWidget(NWID_HORIZONTAL),
    00158         NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
    00159           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),
    00160           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),
    00161         EndContainer(),
    00162         NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_SL_HOME_BUTTON), SetMinimalSize(12, 12), SetDataTip(SPR_HOUSE_ICON, STR_SAVELOAD_HOME_BUTTON),
    00163       EndContainer(),
    00164       NWidget(WWT_PANEL, COLOUR_GREY, WID_SL_FILE_BACKGROUND),
    00165         NWidget(NWID_HORIZONTAL),
    00166           NWidget(WWT_INSET, COLOUR_GREY, WID_SL_DRIVES_DIRECTORIES_LIST), SetPadding(2, 1, 0, 2),
    00167               SetDataTip(0x0, STR_SAVELOAD_LIST_TOOLTIP), SetResize(1, 10), SetScrollbar(WID_SL_SCROLLBAR), EndContainer(),
    00168           NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_SL_SCROLLBAR),
    00169         EndContainer(),
    00170         NWidget(WWT_EDITBOX, COLOUR_GREY, WID_SL_SAVE_OSK_TITLE), SetPadding(3, 2, 2, 2), SetFill(1, 0), SetResize(1, 0),
    00171             SetDataTip(STR_SAVELOAD_OSKTITLE, STR_SAVELOAD_EDITBOX_TOOLTIP),
    00172       EndContainer(),
    00173       NWidget(NWID_HORIZONTAL),
    00174         NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SL_DELETE_SELECTION), SetDataTip(STR_SAVELOAD_DELETE_BUTTON, STR_SAVELOAD_DELETE_TOOLTIP), SetFill(1, 0), SetResize(1, 0),
    00175         NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SL_SAVE_GAME),        SetDataTip(STR_SAVELOAD_SAVE_BUTTON, STR_SAVELOAD_SAVE_TOOLTIP),     SetFill(1, 0), SetResize(1, 0),
    00176       EndContainer(),
    00177     EndContainer(),
    00178     NWidget(WWT_PANEL, COLOUR_GREY),
    00179       NWidget(WWT_EMPTY, INVALID_COLOUR, WID_SL_DETAILS), SetResize(1, 1), SetFill(1, 1),
    00180       NWidget(NWID_HORIZONTAL),
    00181         NWidget(NWID_SPACER), SetResize(1, 0), SetFill(1, 1),
    00182         NWidget(WWT_RESIZEBOX, COLOUR_GREY),
    00183       EndContainer(),
    00184     EndContainer(),
    00185   EndContainer(),
    00186 };
    00187
    00189 const TextColour _fios_colours[] = {
    00190   TC_LIGHT_BLUE, TC_DARK_GREEN,  TC_DARK_GREEN, TC_ORANGE, TC_LIGHT_BROWN,
    00191   TC_ORANGE,     TC_LIGHT_BROWN, TC_ORANGE,     TC_ORANGE, TC_YELLOW
    00192 };
    00193
    00194 void BuildFileList()
    00195 {
    00196   _fios_path_changed = true;
    00197   FiosFreeSavegameList();
    00198
    00199   switch (_saveload_mode) {
    00200     case SLD_LOAD_SCENARIO:
    00201     case SLD_SAVE_SCENARIO:
    00202       FiosGetScenarioList(_saveload_mode); break;
    00203     case SLD_SAVE_HEIGHTMAP:
    00204     case SLD_LOAD_HEIGHTMAP:
    00205       FiosGetHeightmapList(_saveload_mode); break;
    00206
    00207     default: FiosGetSavegameList(_saveload_mode); break;
    00208   }
    00209
    00210   /* Invalidate saveload window */
    00211   InvalidateWindowData(WC_SAVELOAD, 0, 2, true);
    00212 }
    00213
    00214 static void MakeSortedSaveGameList()
    00215 {
    00216   uint sort_start = 0;
    00217   uint sort_end = 0;
    00218
    00219   /* Directories are always above the files (FIOS_TYPE_DIR)
    00220    * Drives (A:\ (windows only) are always under the files (FIOS_TYPE_DRIVE)
    00221    * Only sort savegames/scenarios, not directories
    00222    */
    00223   for (const FiosItem *item = _fios_items.Begin(); item != _fios_items.End(); item++) {
    00224     switch (item->type) {
    00225       case FIOS_TYPE_DIR:    sort_start++; break;
    00226       case FIOS_TYPE_PARENT: sort_start++; break;
    00227       case FIOS_TYPE_DRIVE:  sort_end++;   break;
    00228       default: break;
    00229     }
    00230   }
    00231
    00232   uint s_amount = _fios_items.Length() - sort_start - sort_end;
    00233   QSortT(_fios_items.Get(sort_start), s_amount, CompareFiosItems);
    00234 }
    00235
    00236 struct SaveLoadWindow : public Window {
    00237 private:
    00238   QueryString filename_editbox;
    00239   FiosItem o_dir;
    00240   const FiosItem *selected;
    00241   Scrollbar *vscroll;
    00242 public:
    00243
    00245   void GenerateFileName()
    00246   {
    00247     GenerateDefaultSaveName(this->filename_editbox.text.buf, &this->filename_editbox.text.buf[this->filename_editbox.text.max_bytes - 1]);
    00248     this->filename_editbox.text.UpdateSize();
    00249   }
    00250
    00251   SaveLoadWindow(WindowDesc *desc, SaveLoadDialogMode mode) : Window(desc), filename_editbox(64)
    00252   {
    00253     static const StringID saveload_captions[] = {
    00254       STR_SAVELOAD_LOAD_CAPTION,
    00255       STR_SAVELOAD_LOAD_SCENARIO,
    00256       STR_SAVELOAD_SAVE_CAPTION,
    00257       STR_SAVELOAD_SAVE_SCENARIO,
    00258       STR_SAVELOAD_LOAD_HEIGHTMAP,
    00259       STR_SAVELOAD_SAVE_HEIGHTMAP,
    00260     };
    00261     assert((uint)mode < lengthof(saveload_captions));
    00262
    00263     /* Use an array to define what will be the current file type being handled
    00264      * by current file mode */
    00265     switch (mode) {
    00266       case SLD_SAVE_GAME:     this->GenerateFileName(); break;
    00267       case SLD_SAVE_HEIGHTMAP:
    00268       case SLD_SAVE_SCENARIO: this->filename_editbox.text.Assign("UNNAMED"); break;
    00269       default:                break;
    00270     }
    00271
    00272     this->querystrings[WID_SL_SAVE_OSK_TITLE] = &this->filename_editbox;
    00273     this->filename_editbox.ok_button = WID_SL_SAVE_GAME;
    00274
    00275     this->CreateNestedTree(true);
    00276     if (mode == SLD_LOAD_GAME) this->GetWidget<NWidgetStacked>(WID_SL_CONTENT_DOWNLOAD_SEL)->SetDisplayedPlane(SZSP_HORIZONTAL);
    00277     this->GetWidget<NWidgetCore>(WID_SL_CAPTION)->widget_data = saveload_captions[mode];
    00278     this->vscroll = this->GetScrollbar(WID_SL_SCROLLBAR);
    00279
    00280     this->FinishInitNested(0);
    00281
    00282     this->LowerWidget(WID_SL_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_SAVE_HEIGHTMAP:
    00308       case SLD_LOAD_HEIGHTMAP:
    00309         FioGetDirectory(o_dir.name, lengthof(o_dir.name), HEIGHTMAP_DIR);
    00310         break;
    00311
    00312       default:
    00313         strecpy(o_dir.name, _personal_dir, lastof(o_dir.name));
    00314     }
    00315
    00316     /* Focus the edit box by default in the save windows */
    00317     if (_saveload_mode == SLD_SAVE_GAME || _saveload_mode == SLD_SAVE_SCENARIO || _saveload_mode == SLD_SAVE_HEIGHTMAP) {
    00318       this->SetFocusedWidget(WID_SL_SAVE_OSK_TITLE);
    00319     }
    00320   }
    00321
    00322   virtual ~SaveLoadWindow()
    00323   {
    00324     /* pause is only used in single-player, non-editor mode, non menu mode */
    00325     if (!_networking && _game_mode != GM_EDITOR && _game_mode != GM_MENU) {
    00326       DoCommandP(0, PM_PAUSED_SAVELOAD, 0, CMD_PAUSE);
    00327     }
    00328     FiosFreeSavegameList();
    00329   }
    00330
    00331   virtual void DrawWidget(const Rect &r, int widget) const
    00332   {
    00333     switch (widget) {
    00334       case WID_SL_SORT_BYNAME:
    00335       case WID_SL_SORT_BYDATE:
    00336         if (((_savegame_sort_order & SORT_BY_NAME) != 0) == (widget == WID_SL_SORT_BYNAME)) {
    00337           this->DrawSortButtonState(widget, _savegame_sort_order & SORT_DESCENDING ? SBS_DOWN : SBS_UP);
    00338         }
    00339         break;
    00340
    00341       case WID_SL_BACKGROUND: {
    00342         static const char *path = NULL;
    00343         static StringID str = STR_ERROR_UNABLE_TO_READ_DRIVE;
    00344         static uint64 tot = 0;
    00345
    00346         if (_fios_path_changed) {
    00347           str = FiosGetDescText(&path, &tot);
    00348           _fios_path_changed = false;
    00349         }
    00350
    00351         if (str != STR_ERROR_UNABLE_TO_READ_DRIVE) SetDParam(0, tot);
    00352         DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + FONT_HEIGHT_NORMAL + WD_FRAMERECT_TOP, str);
    00353         DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + WD_FRAMERECT_TOP, path, TC_BLACK);
    00354         break;
    00355       }
    00356
    00357       case WID_SL_DRIVES_DIRECTORIES_LIST: {
    00358         GfxFillRect(r.left + 1, r.top + 1, r.right, r.bottom, PC_BLACK);
    00359
    00360         uint y = r.top + WD_FRAMERECT_TOP;
    00361         for (uint pos = this->vscroll->GetPosition(); pos < _fios_items.Length(); pos++) {
    00362           const FiosItem *item = _fios_items.Get(pos);
    00363
    00364           if (item == this->selected) {
    00365             GfxFillRect(r.left + 1, y, r.right, y + this->resize.step_height, PC_DARK_BLUE);
    00366           }
    00367           DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, item->title, _fios_colours[item->type]);
    00368           y += this->resize.step_height;
    00369           if (y >= this->vscroll->GetCapacity() * this->resize.step_height + r.top + WD_FRAMERECT_TOP) break;
    00370         }
    00371         break;
    00372       }
    00373
    00374       case WID_SL_DETAILS: {
    00375         GfxFillRect(r.left + WD_FRAMERECT_LEFT, r.top + WD_FRAMERECT_TOP,
    00376             r.right - WD_FRAMERECT_RIGHT, r.top + FONT_HEIGHT_NORMAL * 2 + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM, PC_GREY);
    00377         DrawString(r.left, r.right, r.top + FONT_HEIGHT_NORMAL / 2 + WD_FRAMERECT_TOP, STR_SAVELOAD_DETAIL_CAPTION, TC_FROMSTRING, SA_HOR_CENTER);
    00378
    00379         if (this->selected == NULL) break;
    00380
    00381         uint y = r.top + FONT_HEIGHT_NORMAL * 2 + WD_PAR_VSEP_NORMAL + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
    00382         uint y_max = r.bottom - FONT_HEIGHT_NORMAL - WD_FRAMERECT_BOTTOM;
    00383
    00384         if (y > y_max) break;
    00385         if (!_load_check_data.checkable) {
    00386           /* Old savegame, no information available */
    00387           DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_SAVELOAD_DETAIL_NOT_AVAILABLE);
    00388           y += FONT_HEIGHT_NORMAL;
    00389         } else if (_load_check_data.error != INVALID_STRING_ID) {
    00390           /* Incompatible / broken savegame */
    00391           SetDParamStr(0, _load_check_data.error_data);
    00392           y = DrawStringMultiLine(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT,
    00393               y, r.bottom - WD_FRAMERECT_BOTTOM, _load_check_data.error, TC_RED);
    00394         } else {
    00395           /* Mapsize */
    00396           SetDParam(0, _load_check_data.map_size_x);
    00397           SetDParam(1, _load_check_data.map_size_y);
    00398           DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_SERVER_LIST_MAP_SIZE);
    00399           y += FONT_HEIGHT_NORMAL;
    00400           if (y > y_max) break;
    00401
    00402           /* Climate */
    00403           byte landscape = _load_check_data.settings.game_creation.landscape;
    00404           if (landscape < NUM_LANDSCAPE) {
    00405             SetDParam(0, STR_CHEAT_SWITCH_CLIMATE_TEMPERATE_LANDSCAPE + landscape);
    00406             DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_SERVER_LIST_LANDSCAPE);
    00407             y += FONT_HEIGHT_NORMAL;
    00408           }
    00409
    00410           y += WD_PAR_VSEP_NORMAL;
    00411           if (y > y_max) break;
    00412
    00413           /* Start date (if available) */
    00414           if (_load_check_data.settings.game_creation.starting_year != 0) {
    00415             SetDParam(0, ConvertYMDToDate(_load_check_data.settings.game_creation.starting_year, 0, 1));
    00416             DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_SERVER_LIST_START_DATE);
    00417             y += FONT_HEIGHT_NORMAL;
    00418           }
    00419           if (y > y_max) break;
    00420
    00421           /* Hide current date for scenarios */
    00422           if (_saveload_mode != SLD_LOAD_SCENARIO && _saveload_mode != SLD_SAVE_SCENARIO) {
    00423             /* Current date */
    00424             SetDParam(0, _load_check_data.current_date);
    00425             DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_SERVER_LIST_CURRENT_DATE);
    00426             y += FONT_HEIGHT_NORMAL;
    00427           }
    00428
    00429           /* Hide the NewGRF stuff when saving. We also hide the button. */
    00430           if (_saveload_mode == SLD_LOAD_GAME || _saveload_mode == SLD_LOAD_SCENARIO) {
    00431             y += WD_PAR_VSEP_NORMAL;
    00432             if (y > y_max) break;
    00433
    00434             /* NewGrf compatibility */
    00435             SetDParam(0, _load_check_data.grfconfig == NULL ? STR_NEWGRF_LIST_NONE :
    00436                 STR_NEWGRF_LIST_ALL_FOUND + _load_check_data.grf_compatibility);
    00437             DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_SAVELOAD_DETAIL_GRFSTATUS);
    00438             y += FONT_HEIGHT_NORMAL;
    00439           }
    00440           if (y > y_max) break;
    00441
    00442           /* Hide the company stuff for scenarios */
    00443           if (_saveload_mode != SLD_LOAD_SCENARIO && _saveload_mode != SLD_SAVE_SCENARIO) {
    00444             y += FONT_HEIGHT_NORMAL;
    00445             if (y > y_max) break;
    00446
    00447             /* Companies / AIs */
    00448             CompanyPropertiesMap::const_iterator end = _load_check_data.companies.End();
    00449             for (CompanyPropertiesMap::const_iterator it = _load_check_data.companies.Begin(); it != end; it++) {
    00450               SetDParam(0, it->first + 1);
    00451               const CompanyProperties &c = *it->second;
    00452               if (c.name != NULL) {
    00453                 SetDParam(1, STR_JUST_RAW_STRING);
    00454                 SetDParamStr(2, c.name);
    00455               } else {
    00456                 SetDParam(1, c.name_1);
    00457                 SetDParam(2, c.name_2);
    00458               }
    00459               DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_SAVELOAD_DETAIL_COMPANY_INDEX);
    00460               y += FONT_HEIGHT_NORMAL;
    00461               if (y > y_max) break;
    00462             }
    00463           }
    00464         }
    00465         break;
    00466       }
    00467     }
    00468   }
    00469
    00470   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
    00471   {
    00472     switch (widget) {
    00473       case WID_SL_BACKGROUND:
    00474         size->height = 2 * FONT_HEIGHT_NORMAL + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
    00475         break;
    00476
    00477       case WID_SL_DRIVES_DIRECTORIES_LIST:
    00478         resize->height = FONT_HEIGHT_NORMAL;
    00479         size->height = resize->height * 10 + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
    00480         break;
    00481       case WID_SL_SORT_BYNAME:
    00482       case WID_SL_SORT_BYDATE: {
    00483         Dimension d = GetStringBoundingBox(this->GetWidget<NWidgetCore>(widget)->widget_data);
    00484         d.width += padding.width + WD_SORTBUTTON_ARROW_WIDTH * 2; // Doubled since the string is centred and it also looks better.
    00485         d.height += padding.height;
    00486         *size = maxdim(*size, d);
    00487         break;
    00488       }
    00489     }
    00490   }
    00491
    00492   virtual void OnPaint()
    00493   {
    00494     if (_savegame_sort_dirty) {
    00495       _savegame_sort_dirty = false;
    00496       MakeSortedSaveGameList();
    00497     }
    00498
    00499     this->vscroll->SetCount(_fios_items.Length());
    00500     this->DrawWidgets();
    00501   }
    00502
    00503   virtual void OnClick(Point pt, int widget, int click_count)
    00504   {
    00505     switch (widget) {
    00506       case WID_SL_SORT_BYNAME: // Sort save names by name
    00507         _savegame_sort_order = (_savegame_sort_order == SORT_BY_NAME) ?
    00508           SORT_BY_NAME | SORT_DESCENDING : SORT_BY_NAME;
    00509         _savegame_sort_dirty = true;
    00510         this->SetDirty();
    00511         break;
    00512
    00513       case WID_SL_SORT_BYDATE: // Sort save names by date
    00514         _savegame_sort_order = (_savegame_sort_order == SORT_BY_DATE) ?
    00515           SORT_BY_DATE | SORT_DESCENDING : SORT_BY_DATE;
    00516         _savegame_sort_dirty = true;
    00517         this->SetDirty();
    00518         break;
    00519
    00520       case WID_SL_HOME_BUTTON: // OpenTTD 'button', jumps to OpenTTD directory
    00521         FiosBrowseTo(&o_dir);
    00522         this->InvalidateData();
    00523         break;
    00524
    00525       case WID_SL_LOAD_BUTTON:
    00526         if (this->selected != NULL && !_load_check_data.HasErrors()) {
    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
    00533           if (_saveload_mode == SLD_LOAD_HEIGHTMAP) {
    00534             delete this;
    00535             ShowHeightmapLoad();
    00536           } else if (!_load_check_data.HasNewGrfs() || _load_check_data.grf_compatibility != GLC_NOT_FOUND || _settings_client.gui.UserIsAllowedToChangeNewGRFs()) {
    00537             _switch_mode = (_game_mode == GM_EDITOR) ? SM_LOAD_SCENARIO : SM_LOAD_GAME;
    00538             ClearErrorMessages();
    00539             delete this;
    00540           }
    00541         }
    00542         break;
    00543
    00544       case WID_SL_NEWGRF_INFO:
    00545         if (_load_check_data.HasNewGrfs()) {
    00546           ShowNewGRFSettings(false, false, false, &_load_check_data.grfconfig);
    00547         }
    00548         break;
    00549
    00550       case WID_SL_MISSING_NEWGRFS:
    00551         if (!_network_available) {
    00552           ShowErrorMessage(STR_NETWORK_ERROR_NOTAVAILABLE, INVALID_STRING_ID, WL_ERROR);
    00553         } else if (_load_check_data.HasNewGrfs()) {
    00554 #if defined(ENABLE_NETWORK)
    00555           ShowMissingContentWindow(_load_check_data.grfconfig);
    00556 #endif
    00557         }
    00558         break;
    00559
    00560       case WID_SL_DRIVES_DIRECTORIES_LIST: { // Click the listbox
    00561         int y = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_SL_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_SAVE_GAME || _saveload_mode == SLD_SAVE_SCENARIO || _saveload_mode == SLD_SAVE_HEIGHTMAP) {
    00580               /* Copy clicked name to editbox */
    00581               this->filename_editbox.text.Assign(file->title);
    00582               this->SetWidgetDirty(WID_SL_SAVE_OSK_TITLE);
    00583             }
    00584           } else if (!_load_check_data.HasErrors()) {
    00585             this->selected = file;
    00586             if (_saveload_mode == SLD_LOAD_GAME || _saveload_mode == SLD_LOAD_SCENARIO) {
    00587               this->OnClick(pt, WID_SL_LOAD_BUTTON, 1);
    00588             } else if (_saveload_mode == SLD_LOAD_HEIGHTMAP) {
    00589               SetFiosType(file->type);
    00590               strecpy(_file_to_saveload.name, name, lastof(_file_to_saveload.name));
    00591               strecpy(_file_to_saveload.title, file->title, lastof(_file_to_saveload.title));
    00592
    00593               delete this;
    00594               ShowHeightmapLoad();
    00595             }
    00596           }
    00597         } else {
    00598           /* Changed directory, need refresh. */
    00599           this->InvalidateData();
    00600         }
    00601         break;
    00602       }
    00603
    00604       case WID_SL_CONTENT_DOWNLOAD:
    00605         if (!_network_available) {
    00606           ShowErrorMessage(STR_NETWORK_ERROR_NOTAVAILABLE, INVALID_STRING_ID, WL_ERROR);
    00607         } else {
    00608 #if defined(ENABLE_NETWORK)
    00609           switch (_saveload_mode) {
    00610             default: NOT_REACHED();
    00611             case SLD_LOAD_SCENARIO:  ShowNetworkContentListWindow(NULL, CONTENT_TYPE_SCENARIO);  break;
    00612             case SLD_LOAD_HEIGHTMAP: ShowNetworkContentListWindow(NULL, CONTENT_TYPE_HEIGHTMAP); break;
    00613           }
    00614 #endif
    00615         }
    00616         break;
    00617
    00618       case WID_SL_DELETE_SELECTION: // Delete
    00619         break;
    00620
    00621       case WID_SL_SAVE_GAME: // Save game
    00622         /* Note, this is also called via the OSK; and we need to lower the button. */
    00623         this->HandleButtonClick(WID_SL_SAVE_GAME);
    00624         break;
    00625     }
    00626   }
    00627
    00628   virtual EventState OnKeyPress(WChar key, uint16 keycode)
    00629   {
    00630     if (keycode == WKC_ESC) {
    00631       delete this;
    00632       return ES_HANDLED;
    00633     }
    00634
    00635     return ES_NOT_HANDLED;
    00636   }
    00637
    00638   virtual void OnTimeout()
    00639   {
    00640     /* This test protects against using widgets 11 and 12 which are only available
    00641      * in those saveload modes. */
    00642     if (!(_saveload_mode == SLD_SAVE_GAME || _saveload_mode == SLD_SAVE_SCENARIO || _saveload_mode == SLD_SAVE_HEIGHTMAP)) return;
    00643
    00644     if (this->IsWidgetLowered(WID_SL_DELETE_SELECTION)) { // Delete button clicked
    00645       if (!FiosDelete(this->filename_editbox.text.buf)) {
    00646         ShowErrorMessage(STR_ERROR_UNABLE_TO_DELETE_FILE, INVALID_STRING_ID, WL_ERROR);
    00647       } else {
    00648         this->InvalidateData();
    00649         /* Reset file name to current date on successful delete */
    00650         if (_saveload_mode == SLD_SAVE_GAME) GenerateFileName();
    00651       }
    00652     } else if (this->IsWidgetLowered(WID_SL_SAVE_GAME)) { // Save button clicked
    00653       if (_saveload_mode  == SLD_SAVE_GAME || _saveload_mode == SLD_SAVE_SCENARIO) {
    00654         _switch_mode = SM_SAVE_GAME;
    00655         FiosMakeSavegameName(_file_to_saveload.name, this->filename_editbox.text.buf, sizeof(_file_to_saveload.name));
    00656       } else {
    00657         _switch_mode = SM_SAVE_HEIGHTMAP;
    00658         FiosMakeHeightmapName(_file_to_saveload.name, this->filename_editbox.text.buf, sizeof(_file_to_saveload.name));
    00659       }
    00660
    00661       /* In the editor set up the vehicle engines correctly (date might have changed) */
    00662       if (_game_mode == GM_EDITOR) StartupEngines();
    00663     }
    00664   }
    00665
    00666   virtual void OnResize()
    00667   {
    00668     this->vscroll->SetCapacityFromWidget(this, WID_SL_DRIVES_DIRECTORIES_LIST);
    00669   }
    00670
    00676   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
    00677   {
    00678     switch (data) {
    00679       case 0:
    00680         /* Rescan files */
    00681         this->selected = NULL;
    00682         _load_check_data.Clear();
    00683         if (!gui_scope) break;
    00684         BuildFileList();
    00685         /* FALL THROUGH */
    00686       case 1:
    00687         /* Selection changes */
    00688         if (!gui_scope) break;
    00689         if (_saveload_mode == SLD_LOAD_HEIGHTMAP) {
    00690           this->SetWidgetDisabledState(WID_SL_LOAD_BUTTON, this->selected == NULL || _load_check_data.HasErrors());
    00691         }
    00692         if (_saveload_mode == SLD_LOAD_GAME || _saveload_mode == SLD_LOAD_SCENARIO) {
    00693           this->SetWidgetDisabledState(WID_SL_LOAD_BUTTON,
    00694               this->selected == NULL || _load_check_data.HasErrors() || !(!_load_check_data.HasNewGrfs() || _load_check_data.grf_compatibility != GLC_NOT_FOUND || _settings_client.gui.UserIsAllowedToChangeNewGRFs()));
    00695           this->SetWidgetDisabledState(WID_SL_NEWGRF_INFO,
    00696               !_load_check_data.HasNewGrfs());
    00697           this->SetWidgetDisabledState(WID_SL_MISSING_NEWGRFS,
    00698               !_load_check_data.HasNewGrfs() || _load_check_data.grf_compatibility == GLC_ALL_GOOD);
    00699         }
    00700         break;
    00701       case 2:
    00702         /* _fios_items changed */
    00703         this->vscroll->SetCount(_fios_items.Length());
    00704         this->selected = NULL;
    00705         _load_check_data.Clear();
    00706         break;
    00707     }
    00708   }
    00709 };
    00710
    00712 static WindowDesc _load_dialog_desc(
    00713   WDP_CENTER, "load_game", 500, 294,
    00714   WC_SAVELOAD, WC_NONE,
    00715   0,
    00716   _nested_load_dialog_widgets, lengthof(_nested_load_dialog_widgets)
    00717 );
    00718
    00720 static WindowDesc _load_heightmap_dialog_desc(
    00721   WDP_CENTER, "load_heightmap", 257, 320,
    00722   WC_SAVELOAD, WC_NONE,
    00723   0,
    00724   _nested_load_heightmap_dialog_widgets, lengthof(_nested_load_heightmap_dialog_widgets)
    00725 );
    00726
    00728 static WindowDesc _save_dialog_desc(
    00729   WDP_CENTER, "save_game", 500, 294,
    00730   WC_SAVELOAD, WC_NONE,
    00731   0,
    00732   _nested_save_dialog_widgets, lengthof(_nested_save_dialog_widgets)
    00733 );
    00734
    00739 static const FileType _file_modetotype[] = {
    00740   FT_SAVEGAME,  // used for SLD_LOAD_GAME
    00741   FT_SCENARIO,  // used for SLD_LOAD_SCENARIO
    00742   FT_SAVEGAME,  // used for SLD_SAVE_GAME
    00743   FT_SCENARIO,  // used for SLD_SAVE_SCENARIO
    00744   FT_HEIGHTMAP, // used for SLD_LOAD_HEIGHTMAP
    00745   FT_HEIGHTMAP, // used for SLD_SAVE_HEIGHTMAP
    00746 };
    00747
    00752 void ShowSaveLoadDialog(SaveLoadDialogMode mode)
    00753 {
    00754   DeleteWindowById(WC_SAVELOAD, 0);
    00755
    00756   WindowDesc *sld;
    00757   switch (mode) {
    00758     case SLD_SAVE_GAME:
    00759     case SLD_SAVE_SCENARIO:
    00760     case SLD_SAVE_HEIGHTMAP:
    00761       sld = &_save_dialog_desc; break;
    00762     case SLD_LOAD_HEIGHTMAP:
    00763       sld = &_load_heightmap_dialog_desc; break;
    00764     default:
    00765       sld = &_load_dialog_desc; break;
    00766   }
    00767
    00768   _saveload_mode = mode;
    00769   _file_to_saveload.filetype = _file_modetotype[mode];
    00770
    00771   new SaveLoadWindow(sld, mode);
    00772 }
    00773
    00774 void SetFiosType(const byte fiostype)
    00775 {
    00776   switch (fiostype) {
    00777     case FIOS_TYPE_FILE:
    00778     case FIOS_TYPE_SCENARIO:
    00779       _file_to_saveload.mode = SL_LOAD;
    00780       break;
    00781
    00782     case FIOS_TYPE_OLDFILE:
    00783     case FIOS_TYPE_OLD_SCENARIO:
    00784       _file_to_saveload.mode = SL_OLD_LOAD;
    00785       break;
    00786
    00787 #ifdef WITH_PNG
    00788     case FIOS_TYPE_PNG:
    00789       _file_to_saveload.mode = SL_PNG;
    00790       break;
    00791 #endif /* WITH_PNG */
    00792
    00793     case FIOS_TYPE_BMP:
    00794       _file_to_saveload.mode = SL_BMP;
    00795       break;
    00796
    00797     default:
    00798       _file_to_saveload.mode = SL_INVALID;
    00799       break;
    00800   }
    00801 }