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