# 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 "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   EndContainer(),
    00077   NWidget(WWT_PANEL, COLOUR_GREY, WID_SL_BACKGROUND), SetFill(1, 0), SetResize(1, 0), EndContainer(),
    00078   NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
    00079     NWidget(NWID_VERTICAL),
    00080       NWidget(NWID_HORIZONTAL),
    00081         NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
    00082           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),
    00083           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),
    00084         EndContainer(),
    00085         NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_SL_HOME_BUTTON), SetMinimalSize(12, 12), SetDataTip(SPR_HOUSE_ICON, STR_SAVELOAD_HOME_BUTTON),
    00086       EndContainer(),
    00087       NWidget(WWT_PANEL, COLOUR_GREY, WID_SL_FILE_BACKGROUND),
    00088         NWidget(NWID_HORIZONTAL),
    00089           NWidget(WWT_INSET, COLOUR_GREY, WID_SL_DRIVES_DIRECTORIES_LIST), SetFill(1, 1), SetPadding(2, 1, 2, 2),
    00090               SetDataTip(0x0, STR_SAVELOAD_LIST_TOOLTIP), SetResize(1, 10), SetScrollbar(WID_SL_SCROLLBAR), EndContainer(),
    00091           NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_SL_SCROLLBAR),
    00092         EndContainer(),
    00093         NWidget(NWID_SELECTION, INVALID_COLOUR, WID_SL_CONTENT_DOWNLOAD_SEL),
    00094           NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SL_CONTENT_DOWNLOAD), SetResize(1, 0),
    00095               SetDataTip(STR_INTRO_ONLINE_CONTENT, STR_INTRO_TOOLTIP_ONLINE_CONTENT),
    00096         EndContainer(),
    00097       EndContainer(),
    00098     EndContainer(),
    00099     NWidget(WWT_PANEL, COLOUR_GREY),
    00100       NWidget(WWT_EMPTY, INVALID_COLOUR, WID_SL_DETAILS), SetResize(1, 1), SetFill(1, 1),
    00101       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),
    00102       NWidget(NWID_HORIZONTAL),
    00103         NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
    00104           NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SL_NEWGRF_INFO), SetDataTip(STR_INTRO_NEWGRF_SETTINGS, STR_NULL), SetFill(1, 0), SetResize(1, 0),
    00105           NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SL_LOAD_BUTTON), SetDataTip(STR_SAVELOAD_LOAD_BUTTON, STR_SAVELOAD_LOAD_TOOLTIP), SetFill(1, 0), SetResize(1, 0),
    00106         EndContainer(),
    00107         NWidget(WWT_RESIZEBOX, COLOUR_GREY),
    00108       EndContainer(),
    00109     EndContainer(),
    00110   EndContainer(),
    00111 };
    00112
    00114 static const NWidgetPart _nested_load_heightmap_dialog_widgets[] = {
    00115   NWidget(NWID_HORIZONTAL),
    00116     NWidget(WWT_CLOSEBOX, COLOUR_GREY),
    00117     NWidget(WWT_CAPTION, COLOUR_GREY, WID_SL_CAPTION),
    00118   EndContainer(),
    00119   NWidget(WWT_PANEL, COLOUR_GREY, WID_SL_BACKGROUND), SetFill(1, 0), SetResize(1, 0), EndContainer(),
    00120   NWidget(NWID_VERTICAL),
    00121     NWidget(NWID_HORIZONTAL),
    00122       NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
    00123         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),
    00124         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),
    00125       EndContainer(),
    00126       NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_SL_HOME_BUTTON), SetMinimalSize(12, 12), SetDataTip(SPR_HOUSE_ICON, STR_SAVELOAD_HOME_BUTTON),
    00127     EndContainer(),
    00128     NWidget(WWT_PANEL, COLOUR_GREY, WID_SL_FILE_BACKGROUND),
    00129       NWidget(NWID_HORIZONTAL),
    00130         NWidget(WWT_INSET, COLOUR_GREY, WID_SL_DRIVES_DIRECTORIES_LIST), SetFill(1, 1), SetPadding(2, 1, 2, 2),
    00131             SetDataTip(0x0, STR_SAVELOAD_LIST_TOOLTIP), SetResize(1, 10), SetScrollbar(WID_SL_SCROLLBAR), EndContainer(),
    00132         NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_SL_SCROLLBAR),
    00133       EndContainer(),
    00134       NWidget(NWID_HORIZONTAL),
    00135         NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SL_CONTENT_DOWNLOAD), SetResize(1, 0),
    00136             SetDataTip(STR_INTRO_ONLINE_CONTENT, STR_INTRO_TOOLTIP_ONLINE_CONTENT),
    00137         NWidget(WWT_RESIZEBOX, COLOUR_GREY),
    00138       EndContainer(),
    00139     EndContainer(),
    00140   EndContainer(),
    00141 };
    00142
    00144 static const NWidgetPart _nested_save_dialog_widgets[] = {
    00145   NWidget(NWID_HORIZONTAL),
    00146     NWidget(WWT_CLOSEBOX, COLOUR_GREY),
    00147     NWidget(WWT_CAPTION, COLOUR_GREY, WID_SL_CAPTION),
    00148   EndContainer(),
    00149   NWidget(WWT_PANEL, COLOUR_GREY, WID_SL_BACKGROUND), SetFill(1, 0), SetResize(1, 0), EndContainer(),
    00150   NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
    00151     NWidget(NWID_VERTICAL),
    00152       NWidget(NWID_HORIZONTAL),
    00153         NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
    00154           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),
    00155           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),
    00156         EndContainer(),
    00157         NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_SL_HOME_BUTTON), SetMinimalSize(12, 12), SetDataTip(SPR_HOUSE_ICON, STR_SAVELOAD_HOME_BUTTON),
    00158       EndContainer(),
    00159       NWidget(WWT_PANEL, COLOUR_GREY, WID_SL_FILE_BACKGROUND),
    00160         NWidget(NWID_HORIZONTAL),
    00161           NWidget(WWT_INSET, COLOUR_GREY, WID_SL_DRIVES_DIRECTORIES_LIST), SetPadding(2, 1, 0, 2),
    00162               SetDataTip(0x0, STR_SAVELOAD_LIST_TOOLTIP), SetResize(1, 10), SetScrollbar(WID_SL_SCROLLBAR), EndContainer(),
    00163           NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_SL_SCROLLBAR),
    00164         EndContainer(),
    00165         NWidget(WWT_EDITBOX, COLOUR_GREY, WID_SL_SAVE_OSK_TITLE), SetPadding(3, 2, 2, 2), SetFill(1, 0), SetResize(1, 0),
    00166             SetDataTip(STR_SAVELOAD_OSKTITLE, STR_SAVELOAD_EDITBOX_TOOLTIP),
    00167       EndContainer(),
    00168       NWidget(NWID_HORIZONTAL),
    00169         NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SL_DELETE_SELECTION), SetDataTip(STR_SAVELOAD_DELETE_BUTTON, STR_SAVELOAD_DELETE_TOOLTIP), SetFill(1, 0), SetResize(1, 0),
    00170         NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SL_SAVE_GAME),        SetDataTip(STR_SAVELOAD_SAVE_BUTTON, STR_SAVELOAD_SAVE_TOOLTIP),     SetFill(1, 0), SetResize(1, 0),
    00171       EndContainer(),
    00172     EndContainer(),
    00173     NWidget(WWT_PANEL, COLOUR_GREY),
    00174       NWidget(WWT_EMPTY, INVALID_COLOUR, WID_SL_DETAILS), SetResize(1, 1), SetFill(1, 1),
    00175       NWidget(NWID_HORIZONTAL),
    00176         NWidget(NWID_SPACER), SetResize(1, 0), SetFill(1, 1),
    00177         NWidget(WWT_RESIZEBOX, COLOUR_GREY),
    00178       EndContainer(),
    00179     EndContainer(),
    00180   EndContainer(),
    00181 };
    00182
    00184 const TextColour _fios_colours[] = {
    00185   TC_LIGHT_BLUE, TC_DARK_GREEN,  TC_DARK_GREEN, TC_ORANGE, TC_LIGHT_BROWN,
    00186   TC_ORANGE,     TC_LIGHT_BROWN, TC_ORANGE,     TC_ORANGE, TC_YELLOW
    00187 };
    00188
    00189 void BuildFileList()
    00190 {
    00191   _fios_path_changed = true;
    00192   FiosFreeSavegameList();
    00193
    00194   switch (_saveload_mode) {
    00195     case SLD_LOAD_SCENARIO:
    00196     case SLD_SAVE_SCENARIO:
    00197       FiosGetScenarioList(_saveload_mode); break;
    00198     case SLD_SAVE_HEIGHTMAP:
    00199     case SLD_LOAD_HEIGHTMAP:
    00200       FiosGetHeightmapList(_saveload_mode); break;
    00201
    00202     default: FiosGetSavegameList(_saveload_mode); break;
    00203   }
    00204
    00205   /* Invalidate saveload window */
    00206   InvalidateWindowData(WC_SAVELOAD, 0, 2, true);
    00207 }
    00208
    00209 static void MakeSortedSaveGameList()
    00210 {
    00211   uint sort_start = 0;
    00212   uint sort_end = 0;
    00213
    00214   /* Directories are always above the files (FIOS_TYPE_DIR)
    00215    * Drives (A:\ (windows only) are always under the files (FIOS_TYPE_DRIVE)
    00216    * Only sort savegames/scenarios, not directories
    00217    */
    00218   for (const FiosItem *item = _fios_items.Begin(); item != _fios_items.End(); item++) {
    00219     switch (item->type) {
    00220       case FIOS_TYPE_DIR:    sort_start++; break;
    00221       case FIOS_TYPE_PARENT: sort_start++; break;
    00222       case FIOS_TYPE_DRIVE:  sort_end++;   break;
    00223       default: break;
    00224     }
    00225   }
    00226
    00227   uint s_amount = _fios_items.Length() - sort_start - sort_end;
    00228   QSortT(_fios_items.Get(sort_start), s_amount, CompareFiosItems);
    00229 }
    00230
    00231 struct SaveLoadWindow : public QueryStringBaseWindow {
    00232 private:
    00233   FiosItem o_dir;
    00234   const FiosItem *selected;
    00235   Scrollbar *vscroll;
    00236 public:
    00237
    00239   void GenerateFileName()
    00240   {
    00241     GenerateDefaultSaveName(this->edit_str_buf, &this->edit_str_buf[this->edit_str_size - 1]);
    00242   }
    00243
    00244   SaveLoadWindow(const WindowDesc *desc, SaveLoadDialogMode mode) : QueryStringBaseWindow(64)
    00245   {
    00246     static const StringID saveload_captions[] = {
    00247       STR_SAVELOAD_LOAD_CAPTION,
    00248       STR_SAVELOAD_LOAD_SCENARIO,
    00249       STR_SAVELOAD_SAVE_CAPTION,
    00250       STR_SAVELOAD_SAVE_SCENARIO,
    00251       STR_SAVELOAD_LOAD_HEIGHTMAP,
    00252       STR_SAVELOAD_SAVE_HEIGHTMAP,
    00253     };
    00254     assert((uint)mode < lengthof(saveload_captions));
    00255
    00256     /* Use an array to define what will be the current file type being handled
    00257      * by current file mode */
    00258     switch (mode) {
    00259       case SLD_SAVE_GAME:     this->GenerateFileName(); break;
    00260       case SLD_SAVE_HEIGHTMAP:
    00261       case SLD_SAVE_SCENARIO: strecpy(this->edit_str_buf, "UNNAMED", &this->edit_str_buf[edit_str_size - 1]); break;
    00262       default:                break;
    00263     }
    00264
    00265     this->afilter = CS_ALPHANUMERAL;
    00266     InitializeTextBuffer(&this->text, this->edit_str_buf, this->edit_str_size);
    00267
    00268     this->CreateNestedTree(desc, true);
    00269     if (mode == SLD_LOAD_GAME) this->GetWidget<NWidgetStacked>(WID_SL_CONTENT_DOWNLOAD_SEL)->SetDisplayedPlane(SZSP_HORIZONTAL);
    00270     this->GetWidget<NWidgetCore>(WID_SL_CAPTION)->widget_data = saveload_captions[mode];
    00271     this->vscroll = this->GetScrollbar(WID_SL_SCROLLBAR);
    00272
    00273     this->FinishInitNested(desc, 0);
    00274
    00275     this->LowerWidget(WID_SL_DRIVES_DIRECTORIES_LIST);
    00276
    00277     /* pause is only used in single-player, non-editor mode, non-menu mode. It
    00278      * will be unpaused in the WE_DESTROY event handler. */
    00279     if (_game_mode != GM_MENU && !_networking && _game_mode != GM_EDITOR) {
    00280       DoCommandP(0, PM_PAUSED_SAVELOAD, 1, CMD_PAUSE);
    00281     }
    00282     SetObjectToPlace(SPR_CURSOR_ZZZ, PAL_NONE, HT_NONE, WC_MAIN_WINDOW, 0);
    00283
    00284     this->OnInvalidateData(0);
    00285
    00286     ResetObjectToPlace();
    00287
    00288     o_dir.type = FIOS_TYPE_DIRECT;
    00289     switch (_saveload_mode) {
    00290       case SLD_SAVE_GAME:
    00291       case SLD_LOAD_GAME:
    00292         FioGetDirectory(o_dir.name, lengthof(o_dir.name), SAVE_DIR);
    00293         break;
    00294
    00295       case SLD_SAVE_SCENARIO:
    00296       case SLD_LOAD_SCENARIO:
    00297         FioGetDirectory(o_dir.name, lengthof(o_dir.name), SCENARIO_DIR);
    00298         break;
    00299
    00300       case SLD_SAVE_HEIGHTMAP:
    00301       case SLD_LOAD_HEIGHTMAP:
    00302         FioGetDirectory(o_dir.name, lengthof(o_dir.name), HEIGHTMAP_DIR);
    00303         break;
    00304
    00305       default:
    00306         strecpy(o_dir.name, _personal_dir, lastof(o_dir.name));
    00307     }
    00308
    00309     /* Focus the edit box by default in the save windows */
    00310     if (_saveload_mode == SLD_SAVE_GAME || _saveload_mode == SLD_SAVE_SCENARIO || _saveload_mode == SLD_SAVE_HEIGHTMAP) {
    00311       this->SetFocusedWidget(WID_SL_SAVE_OSK_TITLE);
    00312     }
    00313   }
    00314
    00315   virtual ~SaveLoadWindow()
    00316   {
    00317     /* pause is only used in single-player, non-editor mode, non menu mode */
    00318     if (!_networking && _game_mode != GM_EDITOR && _game_mode != GM_MENU) {
    00319       DoCommandP(0, PM_PAUSED_SAVELOAD, 0, CMD_PAUSE);
    00320     }
    00321     FiosFreeSavegameList();
    00322   }
    00323
    00324   virtual void DrawWidget(const Rect &r, int widget) const
    00325   {
    00326     switch (widget) {
    00327       case WID_SL_SORT_BYNAME:
    00328       case WID_SL_SORT_BYDATE:
    00329         if (((_savegame_sort_order & SORT_BY_NAME) != 0) == (widget == WID_SL_SORT_BYNAME)) {
    00330           this->DrawSortButtonState(widget, _savegame_sort_order & SORT_DESCENDING ? SBS_DOWN : SBS_UP);
    00331         }
    00332         break;
    00333
    00334       case WID_SL_BACKGROUND: {
    00335         static const char *path = NULL;
    00336         static StringID str = STR_ERROR_UNABLE_TO_READ_DRIVE;
    00337         static uint64 tot = 0;
    00338
    00339         if (_fios_path_changed) {
    00340           str = FiosGetDescText(&path, &tot);
    00341           _fios_path_changed = false;
    00342         }
    00343
    00344         if (str != STR_ERROR_UNABLE_TO_READ_DRIVE) SetDParam(0, tot);
    00345         DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + FONT_HEIGHT_NORMAL + WD_FRAMERECT_TOP, str);
    00346         DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + WD_FRAMERECT_TOP, path, TC_BLACK);
    00347         break;
    00348       }
    00349
    00350       case WID_SL_DRIVES_DIRECTORIES_LIST: {
    00351         GfxFillRect(r.left + 1, r.top + 1, r.right, r.bottom, PC_BLACK);
    00352
    00353         uint y = r.top + WD_FRAMERECT_TOP;
    00354         for (uint pos = this->vscroll->GetPosition(); pos < _fios_items.Length(); pos++) {
    00355           const FiosItem *item = _fios_items.Get(pos);
    00356
    00357           if (item == this->selected) {
    00358             GfxFillRect(r.left + 1, y, r.right, y + this->resize.step_height, PC_DARK_BLUE);
    00359           }
    00360           DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, item->title, _fios_colours[item->type]);
    00361           y += this->resize.step_height;
    00362           if (y >= this->vscroll->GetCapacity() * this->resize.step_height + r.top + WD_FRAMERECT_TOP) break;
    00363         }
    00364         break;
    00365       }
    00366
    00367       case WID_SL_DETAILS: {
    00368         GfxFillRect(r.left + WD_FRAMERECT_LEFT, r.top + WD_FRAMERECT_TOP,
    00369             r.right - WD_FRAMERECT_RIGHT, r.top + FONT_HEIGHT_NORMAL * 2 + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM, PC_GREY);
    00370         DrawString(r.left, r.right, r.top + FONT_HEIGHT_NORMAL / 2 + WD_FRAMERECT_TOP, STR_SAVELOAD_DETAIL_CAPTION, TC_FROMSTRING, SA_HOR_CENTER);
    00371
    00372         if (this->selected == NULL) break;
    00373
    00374         uint y = r.top + FONT_HEIGHT_NORMAL * 2 + WD_PAR_VSEP_NORMAL + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
    00375         uint y_max = r.bottom - FONT_HEIGHT_NORMAL - WD_FRAMERECT_BOTTOM;
    00376
    00377         if (y > y_max) break;
    00378         if (!_load_check_data.checkable) {
    00379           /* Old savegame, no information available */
    00380           DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_SAVELOAD_DETAIL_NOT_AVAILABLE);
    00381           y += FONT_HEIGHT_NORMAL;
    00382         } else if (_load_check_data.error != INVALID_STRING_ID) {
    00383           /* Incompatible / broken savegame */
    00384           SetDParamStr(0, _load_check_data.error_data);
    00385           y = DrawStringMultiLine(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT,
    00386               y, r.bottom - WD_FRAMERECT_BOTTOM, _load_check_data.error, TC_RED);
    00387         } else {
    00388           /* Mapsize */
    00389           SetDParam(0, _load_check_data.map_size_x);
    00390           SetDParam(1, _load_check_data.map_size_y);
    00391           DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_SERVER_LIST_MAP_SIZE);
    00392           y += FONT_HEIGHT_NORMAL;
    00393           if (y > y_max) break;
    00394
    00395           /* Climate */
    00396           byte landscape = _load_check_data.settings.game_creation.landscape;
    00397           if (landscape < NUM_LANDSCAPE) {
    00398             SetDParam(0, STR_CHEAT_SWITCH_CLIMATE_TEMPERATE_LANDSCAPE + landscape);
    00399             DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_SERVER_LIST_LANDSCAPE);
    00400             y += FONT_HEIGHT_NORMAL;
    00401           }
    00402
    00403           y += WD_PAR_VSEP_NORMAL;
    00404           if (y > y_max) break;
    00405
    00406           /* Start date (if available) */
    00407           if (_load_check_data.settings.game_creation.starting_year != 0) {
    00408             SetDParam(0, ConvertYMDToDate(_load_check_data.settings.game_creation.starting_year, 0, 1));
    00409             DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_SERVER_LIST_START_DATE);
    00410             y += FONT_HEIGHT_NORMAL;
    00411           }
    00412           if (y > y_max) break;
    00413
    00414           /* Hide current date for scenarios */
    00415           if (_saveload_mode != SLD_LOAD_SCENARIO && _saveload_mode != SLD_SAVE_SCENARIO) {
    00416             /* Current date */
    00417             SetDParam(0, _load_check_data.current_date);
    00418             DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_SERVER_LIST_CURRENT_DATE);
    00419             y += FONT_HEIGHT_NORMAL;
    00420           }
    00421
    00422           /* Hide the NewGRF stuff when saving. We also hide the button. */
    00423           if (_saveload_mode == SLD_LOAD_GAME || _saveload_mode == SLD_LOAD_SCENARIO) {
    00424             y += WD_PAR_VSEP_NORMAL;
    00425             if (y > y_max) break;
    00426
    00427             /* NewGrf compatibility */
    00428             SetDParam(0, _load_check_data.grfconfig == NULL ? STR_NEWGRF_LIST_NONE :
    00429                 STR_NEWGRF_LIST_ALL_FOUND + _load_check_data.grf_compatibility);
    00430             DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_SAVELOAD_DETAIL_GRFSTATUS);
    00431             y += FONT_HEIGHT_NORMAL;
    00432           }
    00433           if (y > y_max) break;
    00434
    00435           /* Hide the company stuff for scenarios */
    00436           if (_saveload_mode != SLD_LOAD_SCENARIO && _saveload_mode != SLD_SAVE_SCENARIO) {
    00437             y += FONT_HEIGHT_NORMAL;
    00438             if (y > y_max) break;
    00439
    00440             /* Companies / AIs */
    00441             CompanyPropertiesMap::const_iterator end = _load_check_data.companies.End();
    00442             for (CompanyPropertiesMap::const_iterator it = _load_check_data.companies.Begin(); it != end; it++) {
    00443               SetDParam(0, it->first + 1);
    00444               const CompanyProperties &c = *it->second;
    00445               if (c.name != NULL) {
    00446                 SetDParam(1, STR_JUST_RAW_STRING);
    00447                 SetDParamStr(2, c.name);
    00448               } else {
    00449                 SetDParam(1, c.name_1);
    00450                 SetDParam(2, c.name_2);
    00451               }
    00452               DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_SAVELOAD_DETAIL_COMPANY_INDEX);
    00453               y += FONT_HEIGHT_NORMAL;
    00454               if (y > y_max) break;
    00455             }
    00456           }
    00457         }
    00458         break;
    00459       }
    00460     }
    00461   }
    00462
    00463   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
    00464   {
    00465     switch (widget) {
    00466       case WID_SL_BACKGROUND:
    00467         size->height = 2 * FONT_HEIGHT_NORMAL + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
    00468         break;
    00469
    00470       case WID_SL_DRIVES_DIRECTORIES_LIST:
    00471         resize->height = FONT_HEIGHT_NORMAL;
    00472         size->height = resize->height * 10 + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
    00473         break;
    00474       case WID_SL_SORT_BYNAME:
    00475       case WID_SL_SORT_BYDATE: {
    00476         Dimension d = GetStringBoundingBox(this->GetWidget<NWidgetCore>(widget)->widget_data);
    00477         d.width += padding.width + WD_SORTBUTTON_ARROW_WIDTH * 2; // Doubled since the string is centred and it also looks better.
    00478         d.height += padding.height;
    00479         *size = maxdim(*size, d);
    00480         break;
    00481       }
    00482     }
    00483   }
    00484
    00485   virtual void OnPaint()
    00486   {
    00487     if (_savegame_sort_dirty) {
    00488       _savegame_sort_dirty = false;
    00489       MakeSortedSaveGameList();
    00490     }
    00491
    00492     this->vscroll->SetCount(_fios_items.Length());
    00493     this->DrawWidgets();
    00494
    00495     if (_saveload_mode == SLD_SAVE_GAME || _saveload_mode == SLD_SAVE_SCENARIO || _saveload_mode == SLD_SAVE_HEIGHTMAP) {
    00496       this->DrawEditBox(WID_SL_SAVE_OSK_TITLE);
    00497     }
    00498   }
    00499
    00500   virtual void OnClick(Point pt, int widget, int click_count)
    00501   {
    00502     switch (widget) {
    00503       case WID_SL_SORT_BYNAME: // Sort save names by name
    00504         _savegame_sort_order = (_savegame_sort_order == SORT_BY_NAME) ?
    00505           SORT_BY_NAME | SORT_DESCENDING : SORT_BY_NAME;
    00506         _savegame_sort_dirty = true;
    00507         this->SetDirty();
    00508         break;
    00509
    00510       case WID_SL_SORT_BYDATE: // Sort save names by date
    00511         _savegame_sort_order = (_savegame_sort_order == SORT_BY_DATE) ?
    00512           SORT_BY_DATE | SORT_DESCENDING : SORT_BY_DATE;
    00513         _savegame_sort_dirty = true;
    00514         this->SetDirty();
    00515         break;
    00516
    00517       case WID_SL_HOME_BUTTON: // OpenTTD 'button', jumps to OpenTTD directory
    00518         FiosBrowseTo(&o_dir);
    00519         this->InvalidateData();
    00520         break;
    00521
    00522       case WID_SL_LOAD_BUTTON:
    00523         if (this->selected != NULL && !_load_check_data.HasErrors() && (_load_check_data.grf_compatibility != GLC_NOT_FOUND || _settings_client.gui.UserIsAllowedToChangeNewGRFs())) {
    00524           _switch_mode = (_game_mode == GM_EDITOR) ? SM_LOAD_SCENARIO : SM_LOAD_GAME;
    00525
    00526           const char *name = FiosBrowseTo(this->selected);
    00527           SetFiosType(this->selected->type);
    00528
    00529           strecpy(_file_to_saveload.name, name, lastof(_file_to_saveload.name));
    00530           strecpy(_file_to_saveload.title, this->selected->title, lastof(_file_to_saveload.title));
    00531           ClearErrorMessages();
    00532           delete this;
    00533         }
    00534         break;
    00535
    00536       case WID_SL_NEWGRF_INFO:
    00537         if (_load_check_data.HasNewGrfs()) {
    00538           ShowNewGRFSettings(false, false, false, &_load_check_data.grfconfig);
    00539         }
    00540         break;
    00541
    00542       case WID_SL_MISSING_NEWGRFS:
    00543         if (!_network_available) {
    00544           ShowErrorMessage(STR_NETWORK_ERROR_NOTAVAILABLE, INVALID_STRING_ID, WL_ERROR);
    00545         } else {
    00546 #if defined(ENABLE_NETWORK)
    00547           ShowMissingContentWindow(_load_check_data.grfconfig);
    00548 #endif
    00549         }
    00550         break;
    00551
    00552       case WID_SL_DRIVES_DIRECTORIES_LIST: { // Click the listbox
    00553         int y = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_SL_DRIVES_DIRECTORIES_LIST, WD_FRAMERECT_TOP);
    00554         if (y == INT_MAX) return;
    00555
    00556         const FiosItem *file = _fios_items.Get(y);
    00557
    00558         const char *name = FiosBrowseTo(file);
    00559         if (name != NULL) {
    00560           if (click_count == 1) {
    00561             if (this->selected != file) {
    00562               this->selected = file;
    00563               _load_check_data.Clear();
    00564
    00565               if (file->type == FIOS_TYPE_FILE || file->type == FIOS_TYPE_SCENARIO) {
    00566                 SaveOrLoad(name, SL_LOAD_CHECK, NO_DIRECTORY, false);
    00567               }
    00568
    00569               this->InvalidateData(1);
    00570             }
    00571             if (_saveload_mode == SLD_SAVE_GAME || _saveload_mode == SLD_SAVE_SCENARIO || _saveload_mode == SLD_SAVE_HEIGHTMAP) {
    00572               /* Copy clicked name to editbox */
    00573               ttd_strlcpy(this->text.buf, file->title, this->text.max_bytes);
    00574               UpdateTextBufferSize(&this->text);
    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: case WID_SL_SAVE_GAME: // Delete, Save game
    00612         break;
    00613     }
    00614   }
    00615
    00616   virtual void OnMouseLoop()
    00617   {
    00618     if (_saveload_mode == SLD_SAVE_GAME || _saveload_mode == SLD_SAVE_SCENARIO || _saveload_mode == SLD_SAVE_HEIGHTMAP) {
    00619       this->HandleEditBox(WID_SL_SAVE_OSK_TITLE);
    00620     }
    00621   }
    00622
    00623   virtual EventState OnKeyPress(uint16 key, uint16 keycode)
    00624   {
    00625     if (keycode == WKC_ESC) {
    00626       delete this;
    00627       return ES_HANDLED;
    00628     }
    00629
    00630     EventState state = ES_NOT_HANDLED;
    00631     if ((_saveload_mode == SLD_SAVE_GAME || _saveload_mode == SLD_SAVE_SCENARIO || _saveload_mode == SLD_SAVE_HEIGHTMAP) &&
    00632         this->HandleEditBoxKey(WID_SL_SAVE_OSK_TITLE, key, keycode, state) == HEBR_CONFIRM) {
    00633       this->HandleButtonClick(WID_SL_SAVE_GAME);
    00634     }
    00635
    00636     return state;
    00637   }
    00638
    00639   virtual void OnTimeout()
    00640   {
    00641     /* This test protects against using widgets 11 and 12 which are only available
    00642      * in those saveload modes. */
    00643     if (!(_saveload_mode == SLD_SAVE_GAME || _saveload_mode == SLD_SAVE_SCENARIO || _saveload_mode == SLD_SAVE_HEIGHTMAP)) return;
    00644
    00645     if (this->IsWidgetLowered(WID_SL_DELETE_SELECTION)) { // Delete button clicked
    00646       if (!FiosDelete(this->text.buf)) {
    00647         ShowErrorMessage(STR_ERROR_UNABLE_TO_DELETE_FILE, INVALID_STRING_ID, WL_ERROR);
    00648       } else {
    00649         this->InvalidateData();
    00650         /* Reset file name to current date on successful delete */
    00651         if (_saveload_mode == SLD_SAVE_GAME) GenerateFileName();
    00652       }
    00653
    00654       UpdateTextBufferSize(&this->text);
    00655     } else if (this->IsWidgetLowered(WID_SL_SAVE_GAME)) { // Save button clicked
    00656       if (_saveload_mode  == SLD_SAVE_GAME || _saveload_mode == SLD_SAVE_SCENARIO) {
    00657         _switch_mode = SM_SAVE_GAME;
    00658         FiosMakeSavegameName(_file_to_saveload.name, this->text.buf, sizeof(_file_to_saveload.name));
    00659       } else {
    00660         _switch_mode = SM_SAVE_HEIGHTMAP;
    00661         FiosMakeHeightmapName(_file_to_saveload.name, this->text.buf, sizeof(_file_to_saveload.name));
    00662       }
    00663
    00664       /* In the editor set up the vehicle engines correctly (date might have changed) */
    00665       if (_game_mode == GM_EDITOR) StartupEngines();
    00666     }
    00667   }
    00668
    00669   virtual void OnResize()
    00670   {
    00671     this->vscroll->SetCapacityFromWidget(this, WID_SL_DRIVES_DIRECTORIES_LIST);
    00672   }
    00673
    00679   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
    00680   {
    00681     switch (data) {
    00682       case 0:
    00683         /* Rescan files */
    00684         this->selected = NULL;
    00685         _load_check_data.Clear();
    00686         if (!gui_scope) break;
    00687         BuildFileList();
    00688         /* FALL THROUGH */
    00689       case 1:
    00690         /* Selection changes */
    00691         if (!gui_scope) break;
    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.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 const WindowDesc _load_dialog_desc(
    00713   WDP_CENTER, 500, 294,
    00714   WC_SAVELOAD, WC_NONE,
    00715   WDF_UNCLICK_BUTTONS,
    00716   _nested_load_dialog_widgets, lengthof(_nested_load_dialog_widgets)
    00717 );
    00718
    00720 static const WindowDesc _load_heightmap_dialog_desc(
    00721   WDP_CENTER, 257, 320,
    00722   WC_SAVELOAD, WC_NONE,
    00723   WDF_UNCLICK_BUTTONS,
    00724   _nested_load_heightmap_dialog_widgets, lengthof(_nested_load_heightmap_dialog_widgets)
    00725 );
    00726
    00728 static const WindowDesc _save_dialog_desc(
    00729   WDP_CENTER, 500, 294,
    00730   WC_SAVELOAD, WC_NONE,
    00731   WDF_UNCLICK_BUTTONS,
    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   const 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 }