# misc_gui.cpp

Go to the documentation of this file.

    00001 /* $Id: misc_gui.cpp 18300 2009-11-26 16:53:41Z rubidium $ */
    00002
    00005 #include "stdafx.h"
    00006 #include "openttd.h"
    00007 #include "debug.h"
    00008 #include "landscape.h"
    00009 #include "newgrf_text.h"
    00010 #include "saveload/saveload.h"
    00011 #include "tile_map.h"
    00012 #include "gui.h"
    00013 #include "station_gui.h"
    00014 #include "viewport_func.h"
    00015 #include "gfx_func.h"
    00016 #include "station_func.h"
    00017 #include "command_func.h"
    00018 #include "company_func.h"
    00019 #include "town.h"
    00020 #include "network/network.h"
    00021 #include "network/network_content.h"
    00022 #include "variables.h"
    00023 #include "company_base.h"
    00024 #include "texteff.hpp"
    00025 #include "cargotype.h"
    00026 #include "company_manager_face.h"
    00027 #include "strings_func.h"
    00028 #include "fileio_func.h"
    00029 #include "fios.h"
    00030 #include "zoom_func.h"
    00031 #include "window_func.h"
    00032 #include "string_func.h"
    00033 #include "newgrf_cargo.h"
    00034 #include "tilehighlight_func.h"
    00035 #include "querystring_gui.h"
    00036
    00037 #include "table/strings.h"
    00038
    00039 /* Variables to display file lists */
    00040 SaveLoadDialogMode _saveload_mode;
    00041
    00042
    00043 static bool _fios_path_changed;
    00044 static bool _savegame_sort_dirty;
    00045 int _caret_timer;
    00046
    00047 static const Widget _land_info_widgets[] = {
    00048 {   WWT_CLOSEBOX,   RESIZE_NONE,  COLOUR_GREY,     0,    10,     0,    13, STR_00C5,                       STR_018B_CLOSE_WINDOW},
    00049 {    WWT_CAPTION,   RESIZE_NONE,  COLOUR_GREY,    11,   299,     0,    13, STR_01A3_LAND_AREA_INFORMATION, STR_018C_WINDOW_TITLE_DRAG_THIS},
    00050 {      WWT_PANEL, RESIZE_BOTTOM,  COLOUR_GREY,     0,   299,    14,    99, 0x0,                            STR_NULL},
    00051 {    WIDGETS_END},
    00052 };
    00053
    00054 static const WindowDesc _land_info_desc(
    00055   WDP_AUTO, WDP_AUTO, 300, 100, 300, 100,
    00056   WC_LAND_INFO, WC_NONE,
    00057   WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET,
    00058   _land_info_widgets
    00059 );
    00060
    00061 class LandInfoWindow : public Window {
    00062   enum {
    00063     LAND_INFO_CENTERED_LINES   = 12,
    00064     LAND_INFO_MULTICENTER_LINE = LAND_INFO_CENTERED_LINES,
    00065     LAND_INFO_LINE_END,
    00066
    00067     LAND_INFO_LINE_BUFF_SIZE = 512,
    00068   };
    00069
    00070 public:
    00071   char landinfo_data[LAND_INFO_LINE_END][LAND_INFO_LINE_BUFF_SIZE];
    00072
    00073   virtual void OnPaint()
    00074   {
    00075     this->DrawWidgets();
    00076
    00077     uint y = 21;
    00078     for (uint i = 0; i < LAND_INFO_CENTERED_LINES; i++) {
    00079       if (StrEmpty(this->landinfo_data[i])) break;
    00080
    00081       DoDrawStringCentered(150, y, this->landinfo_data[i], i == 0 ? TC_LIGHT_BLUE : TC_FROMSTRING);
    00082       y += i == 0 ? 16 : 12;
    00083     }
    00084
    00085     y += 6;
    00086
    00087     if (!StrEmpty(this->landinfo_data[LAND_INFO_MULTICENTER_LINE])) {
    00088       SetDParamStr(0, this->landinfo_data[LAND_INFO_MULTICENTER_LINE]);
    00089       DrawStringMultiCenter(150, y, STR_JUST_RAW_STRING, this->width - 4);
    00090     }
    00091   }
    00092
    00093   LandInfoWindow(TileIndex tile) : Window(&_land_info_desc) {
    00094     Company *c = GetCompany(IsValidCompanyID(_local_company) ? _local_company : COMPANY_FIRST);
    00095     Town *t = ClosestTownFromTile(tile, _settings_game.economy.dist_local_authority);
    00096
    00097     Money old_money = c->money;
    00098     c->money = INT64_MAX;
    00099     CommandCost costclear = DoCommand(tile, 0, 0, DC_NONE, CMD_LANDSCAPE_CLEAR);
    00100     c->money = old_money;
    00101
    00102     /* Because build_date is not set yet in every TileDesc, we make sure it is empty */
    00103     TileDesc td;
    00104     AcceptedCargo ac;
    00105
    00106     td.build_date = INVALID_DATE;
    00107
    00108     /* Most tiles have only one owner, but
    00109      *  - drivethrough roadstops can be build on town owned roads (up to 2 owners) and
    00110      *  - roads can have up to four owners (railroad, road, tram, 3rd-roadtype "highway").
    00111      */
    00112     td.owner_type[0] = STR_01A7_OWNER; // At least one owner is displayed, though it might be "N/A".
    00113     td.owner_type[1] = STR_NULL;       // STR_NULL results in skipping the owner
    00114     td.owner_type[2] = STR_NULL;
    00115     td.owner_type[3] = STR_NULL;
    00116     td.owner[0] = OWNER_NONE;
    00117     td.owner[1] = OWNER_NONE;
    00118     td.owner[2] = OWNER_NONE;
    00119     td.owner[3] = OWNER_NONE;
    00120
    00121     td.station_class = STR_NULL;
    00122     td.station_name = STR_NULL;
    00123
    00124     td.grf = NULL;
    00125
    00126     GetAcceptedCargo(tile, ac);
    00127     GetTileDesc(tile, &td);
    00128
    00129     uint line_nr = 0;
    00130
    00131     /* Tiletype */
    00132     SetDParam(0, td.dparam[0]);
    00133     GetString(this->landinfo_data[line_nr], td.str, lastof(this->landinfo_data[line_nr]));
    00134     line_nr++;
    00135
    00136     /* Up to four owners */
    00137     for (uint i = 0; i < 4; i++) {
    00138       if (td.owner_type[i] == STR_NULL) continue;
    00139
    00140       SetDParam(0, STR_01A6_N_A);
    00141       if (td.owner[i] != OWNER_NONE && td.owner[i] != OWNER_WATER) GetNameOfOwner(td.owner[i], tile);
    00142       GetString(this->landinfo_data[line_nr], td.owner_type[i], lastof(this->landinfo_data[line_nr]));
    00143       line_nr++;
    00144     }
    00145
    00146     /* Cost to clear/revenue when cleared */
    00147     StringID str = STR_01A4_COST_TO_CLEAR_N_A;
    00148     if (CmdSucceeded(costclear)) {
    00149       Money cost = costclear.GetCost();
    00150       if (cost < 0) {
    00151         cost = -cost; // Negate negative cost to a positive revenue
    00152         str = STR_REVENUE_WHEN_CLEARED;
    00153       } else {
    00154         str = STR_01A5_COST_TO_CLEAR;
    00155       }
    00156       SetDParam(0, cost);
    00157     }
    00158     GetString(this->landinfo_data[line_nr], str, lastof(this->landinfo_data[line_nr]));
    00159     line_nr++;
    00160
    00161     /* Location */
    00162     char tmp[16];
    00163     snprintf(tmp, lengthof(tmp), "0x%.4X", tile);
    00164     SetDParam(0, TileX(tile));
    00165     SetDParam(1, TileY(tile));
    00166     SetDParam(2, TileHeight(tile));
    00167     SetDParamStr(3, tmp);
    00168     GetString(this->landinfo_data[line_nr], STR_LANDINFO_COORDS, lastof(this->landinfo_data[line_nr]));
    00169     line_nr++;
    00170
    00171     /* Local authority */
    00172     SetDParam(0, STR_01A9_NONE);
    00173     if (t != NULL && t->IsValid()) {
    00174       SetDParam(0, STR_TOWN);
    00175       SetDParam(1, t->index);
    00176     }
    00177     GetString(this->landinfo_data[line_nr], STR_01A8_LOCAL_AUTHORITY, lastof(this->landinfo_data[line_nr]));
    00178     line_nr++;
    00179
    00180     /* Build date */
    00181     if (td.build_date != INVALID_DATE) {
    00182       SetDParam(0, td.build_date);
    00183       GetString(this->landinfo_data[line_nr], STR_BUILD_DATE, lastof(this->landinfo_data[line_nr]));
    00184       line_nr++;
    00185     }
    00186
    00187     /* Station class */
    00188     if (td.station_class != STR_NULL) {
    00189       SetDParam(0, td.station_class);
    00190       GetString(this->landinfo_data[line_nr], STR_TILEDESC_STATION_CLASS, lastof(this->landinfo_data[line_nr]));
    00191       line_nr++;
    00192     }
    00193
    00194     /* Station type name */
    00195     if (td.station_name != STR_NULL) {
    00196       SetDParam(0, td.station_name);
    00197       GetString(this->landinfo_data[line_nr], STR_TILEDESC_STATION_TYPE, lastof(this->landinfo_data[line_nr]));
    00198       line_nr++;
    00199     }
    00200
    00201     /* NewGRF name */
    00202     if (td.grf != NULL) {
    00203       SetDParamStr(0, td.grf);
    00204       GetString(this->landinfo_data[line_nr], STR_TILEDESC_NEWGRF_NAME, lastof(this->landinfo_data[line_nr]));
    00205       line_nr++;
    00206     }
    00207
    00208     assert(line_nr < LAND_INFO_CENTERED_LINES);
    00209
    00210     /* Mark last line empty */
    00211     this->landinfo_data[line_nr][0] = '\0';
    00212
    00213     /* Cargo acceptance is displayed in a extra multiline */
    00214     char *strp = GetString(this->landinfo_data[LAND_INFO_MULTICENTER_LINE], STR_01CE_CARGO_ACCEPTED, lastof(this->landinfo_data[LAND_INFO_MULTICENTER_LINE]));
    00215     bool found = false;
    00216
    00217     for (CargoID i = 0; i < NUM_CARGO; ++i) {
    00218       if (ac[i] > 0) {
    00219         /* Add a comma between each item. */
    00220         if (found) {
    00221           *strp++ = ',';
    00222           *strp++ = ' ';
    00223         }
    00224         found = true;
    00225
    00226         /* If the accepted value is less than 8, show it in 1/8:ths */
    00227         if (ac[i] < 8) {
    00228           SetDParam(0, ac[i]);
    00229           SetDParam(1, GetCargo(i)->name);
    00230           strp = GetString(strp, STR_01D1_8, lastof(this->landinfo_data[LAND_INFO_MULTICENTER_LINE]));
    00231         } else {
    00232           strp = GetString(strp, GetCargo(i)->name, lastof(this->landinfo_data[LAND_INFO_MULTICENTER_LINE]));
    00233         }
    00234       }
    00235     }
    00236     if (!found) this->landinfo_data[LAND_INFO_MULTICENTER_LINE][0] = '\0';
    00237
    00238     if (found) line_nr += 2;
    00239
    00240     if (line_nr > 6) ResizeWindow(this, 0, 12 * (line_nr - 6));
    00241
    00242     this->FindWindowPlacementAndResize(&_land_info_desc);
    00243
    00244 #if defined(_DEBUG)
    00245 # define LANDINFOD_LEVEL 0
    00246 #else
    00247 # define LANDINFOD_LEVEL 1
    00248 #endif
    00249     DEBUG(misc, LANDINFOD_LEVEL, "TILE: %#x (%i,%i)", tile, TileX(tile), TileY(tile));
    00250     DEBUG(misc, LANDINFOD_LEVEL, "type_height  = %#x", _m[tile].type_height);
    00251     DEBUG(misc, LANDINFOD_LEVEL, "m1           = %#x", _m[tile].m1);
    00252     DEBUG(misc, LANDINFOD_LEVEL, "m2           = %#x", _m[tile].m2);
    00253     DEBUG(misc, LANDINFOD_LEVEL, "m3           = %#x", _m[tile].m3);
    00254     DEBUG(misc, LANDINFOD_LEVEL, "m4           = %#x", _m[tile].m4);
    00255     DEBUG(misc, LANDINFOD_LEVEL, "m5           = %#x", _m[tile].m5);
    00256     DEBUG(misc, LANDINFOD_LEVEL, "m6           = %#x", _m[tile].m6);
    00257     DEBUG(misc, LANDINFOD_LEVEL, "m7           = %#x", _me[tile].m7);
    00258 #undef LANDINFOD_LEVEL
    00259   }
    00260 };
    00261
    00262 static void Place_LandInfo(TileIndex tile)
    00263 {
    00264   DeleteWindowById(WC_LAND_INFO, 0);
    00265   new LandInfoWindow(tile);
    00266 }
    00267
    00268 void PlaceLandBlockInfo()
    00269 {
    00270   if (_cursor.sprite == SPR_CURSOR_QUERY) {
    00271     ResetObjectToPlace();
    00272   } else {
    00273     _place_proc = Place_LandInfo;
    00274     SetObjectToPlace(SPR_CURSOR_QUERY, PAL_NONE, VHM_RECT, WC_MAIN_TOOLBAR, 0);
    00275   }
    00276 }
    00277
    00278 static const Widget _about_widgets[] = {
    00279 {   WWT_CLOSEBOX,   RESIZE_NONE,  COLOUR_GREY,     0,    10,     0,    13, STR_00C5,         STR_018B_CLOSE_WINDOW},
    00280 {    WWT_CAPTION,   RESIZE_NONE,  COLOUR_GREY,    11,   419,     0,    13, STR_015B_OPENTTD, STR_NULL},
    00281 {      WWT_PANEL,   RESIZE_NONE,  COLOUR_GREY,     0,   419,    14,   271, 0x0,              STR_NULL},
    00282 {      WWT_FRAME,   RESIZE_NONE,  COLOUR_GREY,     5,   414,    40,   245, STR_NULL,         STR_NULL},
    00283 {    WIDGETS_END},
    00284 };
    00285
    00286 static const WindowDesc _about_desc(
    00287   WDP_CENTER, WDP_CENTER, 420, 272, 420, 272,
    00288   WC_GAME_OPTIONS, WC_NONE,
    00289   WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET,
    00290   _about_widgets
    00291 );
    00292
    00293 struct AboutWindow : public Window {
    00294   int scroll_height;
    00295   uint16 counter;
    00296
    00297   AboutWindow() : Window(&_about_desc)
    00298   {
    00299     this->counter = 5;
    00300     this->scroll_height = this->height - 40;
    00301     this->FindWindowPlacementAndResize(&_about_desc);
    00302   }
    00303
    00304   virtual void OnPaint()
    00305   {
    00306     static const char *credits[] = {
    00307       /*************************************************************************
    00308        *                      maximum length of string which fits in window   -^*/
    00309       "Original design by Chris Sawyer",
    00310       "Original graphics by Simon Foster",
    00311       "",
    00312       "The OpenTTD team (in alphabetical order):",
    00313       "  Albert Hofkamp (Alberth) - GUI expert",
    00314       "  Jean-Francois Claeys (Belugas) - GUI, newindustries and more",
    00315       "  Bjarni Corfitzen (Bjarni) - MacOSX port, coder and vehicles",
    00316       "  Matthijs Kooijman (blathijs) - Pathfinder-guru, pool rework",
    00317       "  Victor Fischer (Celestar) - Programming everywhere you need him to",
    00318       "  Christoph Elsenhans (frosch) - General coding",
    00319       "  Lo\xC3\xAF""c Guilloux (glx) - Windows Expert",
    00320       "  Michael Lutz (michi_cc) - Path based signals",
    00321       "  Owen Rudge (orudge) - Forum host, OS/2 port",
    00322       "  Peter Nelson (peter1138) - Spiritual descendant from newGRF gods",
    00323       "  Remko Bijker (Rubidium) - Lead coder and way more",
    00324       "  Zden\xC4\x9Bk Sojka (SmatZ) - Bug finder and fixer",
    00325       "  Thijs Marinussen (Yexo) - AI Framework",
    00326       "",
    00327       "Inactive Developers:",
    00328       "  Tam\xC3\xA1s Farag\xC3\xB3 (Darkvater) - Ex-Lead coder",
    00329       "  Jaroslav Mazanec (KUDr) - YAPG (Yet Another Pathfinder God) ;)",
    00330       "  Jonathan Coome (Maedhros) - High priest of the NewGRF Temple",
    00331       "  Attila B\xC3\xA1n (MiHaMiX) - Developer WebTranslator 1 and 2",
    00332       "  Christoph Mallon (Tron) - Programmer, code correctness police",
    00333       "",
    00334       "Retired Developers:",
    00335       "  Ludvig Strigeus (ludde) - OpenTTD author, main coder (0.1 - 0.3.3)",
    00336       "  Serge Paquet (vurlix) - Assistant project manager, coder (0.1 - 0.3.3)",
    00337       "  Dominik Scherer (dominik81) - Lead programmer, GUI expert (0.3.0 - 0.3.6)",
    00338       "  Benedikt Br\xC3\xBCggemeier (skidd13) - Bug fixer and code reworker",
    00339       "  Patric Stout (TrueLight) - Programmer (0.3 - pre0.7), sys op (active)",
    00340       "",
    00341       "Special thanks go out to:",
    00342       "  Josef Drexler - For his great work on TTDPatch",
    00343       "  Marcin Grzegorczyk - For his documentation of TTD internals",
    00344       "  Petr Baudi\xC5\xA1 (pasky) - Many patches, newGRF support",
    00345       "  Stefan Mei\xC3\x9Fner (sign_de) - For his work on the console",
    00346       "  Simon Sasburg (HackyKid) - Many bugfixes he has blessed us with",
    00347       "  Cian Duffy (MYOB) - BeOS port / manual writing",
    00348       "  Christian Rosentreter (tokai) - MorphOS / AmigaOS port",
    00349       "  Richard Kempton (richK) - additional airports, initial TGP implementation",
    00350       "",
    00351       "  Alberto Demichelis - Squirrel scripting language \xC2\xA9 2003-2008",
    00352       "  Markus F.X.J. Oberhumer - (Mini)LZO for loading old savegames \xC2\xA9 1996-2008",
    00353       "  L. Peter Deutsch - MD5 implementation \xC2\xA9 1999, 2000, 2002",
    00354       "  Michael Blunck - Pre-Signals and Semaphores \xC2\xA9 2003",
    00355       "  George - Canal/Lock graphics \xC2\xA9 2003-2004",
    00356       "  David Dallaston - Tram tracks",
    00357       "  Marcin Grzegorczyk - Foundations for Tracks on Slopes",
    00358       "  All Translators - Who made OpenTTD a truly international game",
    00359       "  Bug Reporters - Without whom OpenTTD would still be full of bugs!",
    00360       "",
    00361       "",
    00362       "And last but not least:",
    00363       "  Chris Sawyer - For an amazing game!"
    00364     };
    00365
    00366     this->DrawWidgets();
    00367
    00368     /* Show original copyright and revision version */
    00369     DrawStringCentered(210, 17, STR_00B6_ORIGINAL_COPYRIGHT, TC_FROMSTRING);
    00370     DrawStringCentered(210, 17 + 10, STR_00B7_VERSION, TC_FROMSTRING);
    00371
    00372     int y = this->scroll_height;
    00373
    00374     /* Show all scrolling credits */
    00375     for (uint i = 0; i < lengthof(credits); i++) {
    00376       if (y >= 50 && y < (this->height - 40)) {
    00377         DoDrawString(credits[i], 10, y, TC_BLACK);
    00378       }
    00379       y += 10;
    00380     }
    00381
    00382     /* If the last text has scrolled start a new from the start */
    00383     if (y < 50) this->scroll_height = this->height - 40;
    00384
    00385     DoDrawStringCentered(210, this->height - 25, "Website: http://www.openttd.org", TC_BLACK);
    00386     DrawStringCentered(210, this->height - 15, STR_00BA_COPYRIGHT_OPENTTD, TC_FROMSTRING);
    00387   }
    00388
    00389   virtual void OnTick()
    00390   {
    00391     if (--this->counter == 0) {
    00392       this->counter = 5;
    00393       this->scroll_height--;
    00394       this->SetDirty();
    00395     }
    00396   }
    00397 };
    00398
    00399 void ShowAboutWindow()
    00400 {
    00401   DeleteWindowById(WC_GAME_OPTIONS, 0);
    00402   new AboutWindow();
    00403 }
    00404
    00405 static const Widget _errmsg_widgets[] = {
    00406 {   WWT_CLOSEBOX,   RESIZE_NONE,    COLOUR_RED,     0,    10,     0,    13, STR_00C5,         STR_018B_CLOSE_WINDOW},
    00407 {    WWT_CAPTION,   RESIZE_NONE,    COLOUR_RED,    11,   239,     0,    13, STR_00B2_MESSAGE, STR_NULL},
    00408 {      WWT_PANEL,   RESIZE_BOTTOM,  COLOUR_RED,     0,   239,    14,    45, 0x0,              STR_NULL},
    00409 {    WIDGETS_END},
    00410 };
    00411
    00412 static const Widget _errmsg_face_widgets[] = {
    00413 {   WWT_CLOSEBOX,   RESIZE_NONE,    COLOUR_RED,     0,    10,     0,    13, STR_00C5,              STR_018B_CLOSE_WINDOW},
    00414 {    WWT_CAPTION,   RESIZE_NONE,    COLOUR_RED,    11,   333,     0,    13, STR_00B3_MESSAGE_FROM, STR_NULL},
    00415 {      WWT_PANEL,   RESIZE_BOTTOM,  COLOUR_RED,     0,   333,    14,   136, 0x0,                   STR_NULL},
    00416 {   WIDGETS_END},
    00417 };
    00418
    00419 struct ErrmsgWindow : public Window {
    00420 private:
    00421   uint duration;
    00422   uint64 decode_params[20];
    00423   StringID message_1;
    00424   StringID message_2;
    00425   bool show_company_manager_face;
    00426
    00427   int y[2];
    00428
    00429 public:
    00430   ErrmsgWindow(Point pt, int width, int height, StringID msg1, StringID msg2, const Widget *widget, bool show_company_manager_face) :
    00431       Window(pt.x, pt.y, width, height, WC_ERRMSG, widget),
    00432       show_company_manager_face(show_company_manager_face)
    00433   {
    00434     this->duration = _settings_client.gui.errmsg_duration;
    00435     CopyOutDParam(this->decode_params, 0, lengthof(this->decode_params));
    00436     this->message_1 = msg1;
    00437     this->message_2 = msg2;
    00438     this->desc_flags = WDF_STD_BTN | WDF_DEF_WIDGET;
    00439
    00440     SwitchToErrorRefStack();
    00441     RewindTextRefStack();
    00442
    00443     assert(msg2 != INVALID_STRING_ID);
    00444
    00445     int h2 = 3 + GetStringHeight(msg2, width - 2); // msg2 is printed first
    00446     int h1 = (msg1 == INVALID_STRING_ID) ? 0 : 3 + GetStringHeight(msg1, width - 2);
    00447
    00448     SwitchToNormalRefStack();
    00449
    00450     int h = 15 + h1 + h2;
    00451     height = max<int>(height, h);
    00452
    00453     if (msg1 == INVALID_STRING_ID) {
    00454       /* only 1 line will be printed */
    00455       y[1] = (height - 15) / 2 + 15 - 5;
    00456     } else {
    00457       int over = (height - h) / 4;
    00458
    00459       y[1] = 15 + h2 / 2 + 1 - 5 + over;
    00460       y[0] = height - 3 - h1 / 2 - 5 - over;
    00461     }
    00462
    00463     this->FindWindowPlacementAndResize(width, height);
    00464   }
    00465
    00466   virtual void OnPaint()
    00467   {
    00468     CopyInDParam(0, this->decode_params, lengthof(this->decode_params));
    00469     this->DrawWidgets();
    00470     CopyInDParam(0, this->decode_params, lengthof(this->decode_params));
    00471
    00472     /* If the error message comes from a NewGRF, we must use the text ref. stack reserved for error messages.
    00473      * If the message doesn't come from a NewGRF, it won't use the TTDP-style text ref. stack, so we won't hurt anything
    00474      */
    00475     SwitchToErrorRefStack();
    00476     RewindTextRefStack();
    00477
    00478     if (this->show_company_manager_face) {
    00479       const Company *c = GetCompany((CompanyID)GetDParamX(this->decode_params, 2));
    00480       DrawCompanyManagerFace(c->face, c->colour, 2, 16);
    00481     }
    00482
    00483     DrawStringMultiCenter(this->width - 120, y[1], this->message_2, this->width - 2);
    00484     if (this->message_1 != INVALID_STRING_ID) DrawStringMultiCenter(this->width - 120, y[0], this->message_1, this->width - 2);
    00485
    00486     /* Switch back to the normal text ref. stack for NewGRF texts */
    00487     SwitchToNormalRefStack();
    00488   }
    00489
    00490   virtual void OnMouseLoop()
    00491   {
    00492     if (_right_button_down) delete this;
    00493   }
    00494
    00495   virtual void OnHundredthTick()
    00496   {
    00497     if (--this->duration == 0) delete this;
    00498   }
    00499
    00500   ~ErrmsgWindow()
    00501   {
    00502     SetRedErrorSquare(INVALID_TILE);
    00503     extern StringID _switch_mode_errorstr;
    00504     _switch_mode_errorstr = INVALID_STRING_ID;
    00505   }
    00506
    00507   virtual EventState OnKeyPress(uint16 key, uint16 keycode)
    00508   {
    00509     if (keycode != WKC_SPACE) return ES_NOT_HANDLED;
    00510     delete this;
    00511     return ES_HANDLED;
    00512   }
    00513 };
    00514
    00515 void ShowErrorMessage(StringID msg_1, StringID msg_2, int x, int y)
    00516 {
    00517   DeleteWindowById(WC_ERRMSG, 0);
    00518
    00519   if (!_settings_client.gui.errmsg_duration) return;
    00520
    00521   if (msg_2 == STR_NULL) msg_2 = STR_EMPTY;
    00522
    00523   Point pt;
    00524   const ViewPort *vp;
    00525
    00526   if (msg_1 != STR_013B_OWNED_BY || GetDParam(2) >= MAX_COMPANIES) {
    00527     if ((x | y) != 0) {
    00528       pt = RemapCoords2(x, y);
    00529       vp = FindWindowById(WC_MAIN_WINDOW, 0)->viewport;
    00530
    00531       /* move x pos to opposite corner */
    00532       pt.x = UnScaleByZoom(pt.x - vp->virtual_left, vp->zoom) + vp->left;
    00533       pt.x = (pt.x < (_screen.width >> 1)) ? _screen.width - 260 : 20;
    00534
    00535       /* move y pos to opposite corner */
    00536       pt.y = UnScaleByZoom(pt.y - vp->virtual_top, vp->zoom) + vp->top;
    00537       pt.y = (pt.y < (_screen.height >> 1)) ? _screen.height - 80 : 100;
    00538
    00539     } else {
    00540       pt.x = (_screen.width - 240) >> 1;
    00541       pt.y = (_screen.height - 46) >> 1;
    00542     }
    00543     new ErrmsgWindow(pt, 240, 46, msg_1, msg_2, _errmsg_widgets, false);
    00544   } else {
    00545     if ((x | y) != 0) {
    00546       pt = RemapCoords2(x, y);
    00547       vp = FindWindowById(WC_MAIN_WINDOW, 0)->viewport;
    00548       pt.x = Clamp(UnScaleByZoom(pt.x - vp->virtual_left, vp->zoom) + vp->left - (334 / 2),  0, _screen.width  - 334);
    00549       pt.y = Clamp(UnScaleByZoom(pt.y - vp->virtual_top,  vp->zoom) + vp->top  - (137 / 2), 22, _screen.height - 137);
    00550     } else {
    00551       pt.x = (_screen.width  - 334) >> 1;
    00552       pt.y = (_screen.height - 137) >> 1;
    00553     }
    00554     new ErrmsgWindow(pt, 334, 137, msg_1, msg_2, _errmsg_face_widgets, true);
    00555   }
    00556 }
    00557
    00558 void ShowEstimatedCostOrIncome(Money cost, int x, int y)
    00559 {
    00560   StringID msg = STR_0805_ESTIMATED_COST;
    00561
    00562   if (cost < 0) {
    00563     cost = -cost;
    00564     msg = STR_0807_ESTIMATED_INCOME;
    00565   }
    00566   SetDParam(0, cost);
    00567   ShowErrorMessage(INVALID_STRING_ID, msg, x, y);
    00568 }
    00569
    00570 void ShowCostOrIncomeAnimation(int x, int y, int z, Money cost)
    00571 {
    00572   Point pt = RemapCoords(x, y, z);
    00573   StringID msg = STR_0801_COST;
    00574
    00575   if (cost < 0) {
    00576     cost = -cost;
    00577     msg = STR_0803_INCOME;
    00578   }
    00579   SetDParam(0, cost);
    00580   AddTextEffect(msg, pt.x, pt.y, 0x250, TE_RISING);
    00581 }
    00582
    00583 void ShowFeederIncomeAnimation(int x, int y, int z, Money cost)
    00584 {
    00585   Point pt = RemapCoords(x, y, z);
    00586
    00587   SetDParam(0, cost);
    00588   AddTextEffect(STR_FEEDER, pt.x, pt.y, 0x250, TE_RISING);
    00589 }
    00590
    00591 TextEffectID ShowFillingPercent(int x, int y, int z, uint8 percent, StringID string)
    00592 {
    00593   Point pt = RemapCoords(x, y, z);
    00594
    00595   assert(string != STR_NULL);
    00596
    00597   SetDParam(0, percent);
    00598   return AddTextEffect(string, pt.x, pt.y, 0xFFFF, TE_STATIC);
    00599 }
    00600
    00601 void UpdateFillingPercent(TextEffectID te_id, uint8 percent, StringID string)
    00602 {
    00603   assert(string != STR_NULL);
    00604
    00605   SetDParam(0, percent);
    00606   UpdateTextEffect(te_id, string);
    00607 }
    00608
    00609 void HideFillingPercent(TextEffectID *te_id)
    00610 {
    00611   if (*te_id == INVALID_TE_ID) return;
    00612
    00613   RemoveTextEffect(*te_id);
    00614   *te_id = INVALID_TE_ID;
    00615 }
    00616
    00617 static const Widget _tooltips_widgets[] = {
    00618 {      WWT_PANEL,   RESIZE_NONE,  COLOUR_GREY,     0,   199,     0,    31, 0x0, STR_NULL},
    00619 {   WIDGETS_END},
    00620 };
    00621
    00622 struct TooltipsWindow : public Window
    00623 {
    00624   StringID string_id;
    00625   byte paramcount;
    00626   uint64 params[5];
    00627   bool use_left_mouse_button;
    00628
    00629   TooltipsWindow(int x, int y, int width, int height, const Widget *widget,
    00630                  StringID str, uint paramcount, const uint64 params[], bool use_left_mouse_button) :
    00631       Window(x, y, width, height, WC_TOOLTIPS, widget)
    00632   {
    00633     this->string_id = str;
    00634     assert(sizeof(this->params[0]) == sizeof(params[0]));
    00635     assert(paramcount <= lengthof(this->params));
    00636     memcpy(this->params, params, sizeof(this->params[0]) * paramcount);
    00637     this->paramcount = paramcount;
    00638     this->use_left_mouse_button = use_left_mouse_button;
    00639
    00640     this->flags4 &= ~WF_WHITE_BORDER_MASK; // remove white-border from tooltip
    00641     this->widget[0].right = width;
    00642     this->widget[0].bottom = height;
    00643
    00644     FindWindowPlacementAndResize(width, height);
    00645   }
    00646
    00647   virtual void OnPaint()
    00648   {
    00649     GfxFillRect(0, 0, this->width - 1, this->height - 1, 0);
    00650     GfxFillRect(1, 1, this->width - 2, this->height - 2, 0x44);
    00651
    00652     for (uint arg = 0; arg < this->paramcount; arg++) {
    00653       SetDParam(arg, this->params[arg]);
    00654     }
    00655     DrawStringMultiCenter((this->width >> 1), (this->height >> 1) - 5, this->string_id, this->width - 2);
    00656   }
    00657
    00658   virtual void OnMouseLoop()
    00659   {
    00660     /* We can show tooltips while dragging tools. These are shown as long as
    00661      * we are dragging the tool. Normal tooltips work with rmb */
    00662     if (this->use_left_mouse_button ? !_left_button_down : !_right_button_down) delete this;
    00663   }
    00664 };
    00665
    00672 void GuiShowTooltips(StringID str, uint paramcount, const uint64 params[], bool use_left_mouse_button)
    00673 {
    00674   DeleteWindowById(WC_TOOLTIPS, 0);
    00675
    00676   if (str == STR_NULL) return;
    00677
    00678   for (uint i = 0; i != paramcount; i++) SetDParam(i, params[i]);
    00679   char buffer[512];
    00680   GetString(buffer, str, lastof(buffer));
    00681
    00682   Dimension br = GetStringBoundingBox(buffer);
    00683   br.width += 6; br.height += 4; // increase slightly to have some space around the box
    00684
    00685   /* Cut tooltip length to 200 pixels max, wrap to new line if longer */
    00686   if (br.width > 200) {
    00687     br.height += ((br.width - 4) / 176) * 10;
    00688     br.width = 200;
    00689   }
    00690
    00691   /* Correctly position the tooltip position, watch out for window and cursor size
    00692    * Clamp value to below main toolbar and above statusbar. If tooltip would
    00693    * go below window, flip it so it is shown above the cursor */
    00694   int y = Clamp(_cursor.pos.y + _cursor.size.y + _cursor.offs.y + 5, 22, _screen.height - 12);
    00695   if (y + br.height > _screen.height - 12) y = _cursor.pos.y + _cursor.offs.y - br.height - 5;
    00696   int x = Clamp(_cursor.pos.x - (br.width >> 1), 0, _screen.width - br.width);
    00697
    00698   new TooltipsWindow(x, y, br.width, br.height, _tooltips_widgets, str, paramcount, params, use_left_mouse_button);
    00699 }
    00700
    00701
    00702 static int DrawStationCoverageText(const AcceptedCargo cargo,
    00703   int str_x, int str_y, StationCoverageType sct, bool supplies)
    00704 {
    00705   bool first = true;
    00706
    00707   char string[512];
    00708   char *b = InlineString(string, supplies ? STR_SUPPLIES : STR_000D_ACCEPTS);
    00709
    00710   for (CargoID i = 0; i < NUM_CARGO; i++) {
    00711     if (b >= lastof(string) - (1 + 2 * 4)) break; // ',' or ' ' and two calls to Utf8Encode()
    00712     switch (sct) {
    00713       case SCT_PASSENGERS_ONLY: if (!IsCargoInClass(i, CC_PASSENGERS)) continue; break;
    00714       case SCT_NON_PASSENGERS_ONLY: if (IsCargoInClass(i, CC_PASSENGERS)) continue; break;
    00715       case SCT_ALL: break;
    00716       default: NOT_REACHED();
    00717     }
    00718     if (cargo[i] >= (supplies ? 1U : 8U)) {
    00719       if (first) {
    00720         first = false;
    00721       } else {
    00722         /* Add a comma if this is not the first item */
    00723         *b++ = ',';
    00724         *b++ = ' ';
    00725       }
    00726       b = InlineString(b, GetCargo(i)->name);
    00727     }
    00728   }
    00729
    00730   /* If first is still true then no cargo is accepted */
    00731   if (first) b = InlineString(b, STR_00D0_NOTHING);
    00732
    00733   *b = '\0';
    00734
    00735   /* Make sure we detect any buffer overflow */
    00736   assert(b < endof(string));
    00737
    00738   SetDParamStr(0, string);
    00739   return DrawStringMultiLine(str_x, str_y, STR_JUST_RAW_STRING, 144);
    00740 }
    00741
    00751 int DrawStationCoverageAreaText(int sx, int sy, StationCoverageType sct, int rad, bool supplies)
    00752 {
    00753   TileIndex tile = TileVirtXY(_thd.pos.x, _thd.pos.y);
    00754   AcceptedCargo cargo;
    00755   if (tile < MapSize()) {
    00756     if (supplies) {
    00757       GetProductionAroundTiles(cargo, tile, _thd.size.x / TILE_SIZE, _thd.size.y / TILE_SIZE , rad);
    00758     } else {
    00759       GetAcceptanceAroundTiles(cargo, tile, _thd.size.x / TILE_SIZE, _thd.size.y / TILE_SIZE , rad);
    00760     }
    00761     return sy + DrawStationCoverageText(cargo, sx, sy, sct, supplies);
    00762   }
    00763
    00764   return sy;
    00765 }
    00766
    00767 void CheckRedrawStationCoverage(const Window *w)
    00768 {
    00769   if (_thd.dirty & 1) {
    00770     _thd.dirty &= ~1;
    00771     SetWindowDirty(w);
    00772   }
    00773 }
    00774
    00775 /* Delete a character at the caret position in a text buf.
    00776  * If backspace is set, delete the character before the caret,
    00777  * else delete the character after it. */
    00778 static void DelChar(Textbuf *tb, bool backspace)
    00779 {
    00780   WChar c;
    00781   char *s = tb->buf + tb->caretpos;
    00782
    00783   if (backspace) s = Utf8PrevChar(s);
    00784
    00785   uint16 len = (uint16)Utf8Decode(&c, s);
    00786   uint width = GetCharacterWidth(FS_NORMAL, c);
    00787
    00788   tb->width  -= width;
    00789   if (backspace) {
    00790     tb->caretpos   -= len;
    00791     tb->caretxoffs -= width;
    00792   }
    00793
    00794   /* Move the remaining characters over the marker */
    00795   memmove(s, s + len, tb->size - (s - tb->buf) - len);
    00796   tb->size -= len;
    00797 }
    00798
    00806 bool DeleteTextBufferChar(Textbuf *tb, int delmode)
    00807 {
    00808   if (delmode == WKC_BACKSPACE && tb->caretpos != 0) {
    00809     DelChar(tb, true);
    00810     return true;
    00811   } else if (delmode == WKC_DELETE && tb->caretpos < tb->size - 1) {
    00812     DelChar(tb, false);
    00813     return true;
    00814   }
    00815
    00816   return false;
    00817 }
    00818
    00823 void DeleteTextBufferAll(Textbuf *tb)
    00824 {
    00825   memset(tb->buf, 0, tb->maxsize);
    00826   tb->size = 1;
    00827   tb->width = tb->caretpos = tb->caretxoffs = 0;
    00828 }
    00829
    00838 bool InsertTextBufferChar(Textbuf *tb, WChar key)
    00839 {
    00840   const byte charwidth = GetCharacterWidth(FS_NORMAL, key);
    00841   uint16 len = (uint16)Utf8CharLen(key);
    00842   if (tb->size + len <= tb->maxsize && (tb->maxwidth == 0 || tb->width + charwidth <= tb->maxwidth)) {
    00843     memmove(tb->buf + tb->caretpos + len, tb->buf + tb->caretpos, tb->size - tb->caretpos);
    00844     Utf8Encode(tb->buf + tb->caretpos, key);
    00845     tb->size  += len;
    00846     tb->width += charwidth;
    00847
    00848     tb->caretpos   += len;
    00849     tb->caretxoffs += charwidth;
    00850     return true;
    00851   }
    00852   return false;
    00853 }
    00854
    00862 bool MoveTextBufferPos(Textbuf *tb, int navmode)
    00863 {
    00864   switch (navmode) {
    00865     case WKC_LEFT:
    00866       if (tb->caretpos != 0) {
    00867         WChar c;
    00868         const char *s = Utf8PrevChar(tb->buf + tb->caretpos);
    00869         Utf8Decode(&c, s);
    00870         tb->caretpos    = s - tb->buf; // -= (tb->buf + tb->caretpos - s)
    00871         tb->caretxoffs -= GetCharacterWidth(FS_NORMAL, c);
    00872
    00873         return true;
    00874       }
    00875       break;
    00876
    00877     case WKC_RIGHT:
    00878       if (tb->caretpos < tb->size - 1) {
    00879         WChar c;
    00880
    00881         tb->caretpos   += (uint16)Utf8Decode(&c, tb->buf + tb->caretpos);
    00882         tb->caretxoffs += GetCharacterWidth(FS_NORMAL, c);
    00883
    00884         return true;
    00885       }
    00886       break;
    00887
    00888     case WKC_HOME:
    00889       tb->caretpos = 0;
    00890       tb->caretxoffs = 0;
    00891       return true;
    00892
    00893     case WKC_END:
    00894       tb->caretpos = tb->size - 1;
    00895       tb->caretxoffs = tb->width;
    00896       return true;
    00897
    00898     default:
    00899       break;
    00900   }
    00901
    00902   return false;
    00903 }
    00904
    00914 void InitializeTextBuffer(Textbuf *tb, char *buf, uint16 maxsize, uint16 maxwidth)
    00915 {
    00916   assert(maxsize != 0);
    00917
    00918   tb->buf      = buf;
    00919   tb->maxsize  = maxsize;
    00920   tb->maxwidth = maxwidth;
    00921   tb->caret    = true;
    00922   UpdateTextBufferSize(tb);
    00923 }
    00924
    00931 void UpdateTextBufferSize(Textbuf *tb)
    00932 {
    00933   const char *buf = tb->buf;
    00934
    00935   tb->width = 0;
    00936   tb->size = 1; // terminating zero
    00937
    00938   WChar c;
    00939   while ((c = Utf8Consume(&buf)) != '\0') {
    00940     tb->width += GetCharacterWidth(FS_NORMAL, c);
    00941     tb->size += Utf8CharLen(c);
    00942   }
    00943
    00944   assert(tb->size <= tb->maxsize);
    00945
    00946   tb->caretpos = tb->size - 1;
    00947   tb->caretxoffs = tb->width;
    00948 }
    00949
    00950 bool HandleCaret(Textbuf *tb)
    00951 {
    00952   /* caret changed? */
    00953   bool b = !!(_caret_timer & 0x20);
    00954
    00955   if (b != tb->caret) {
    00956     tb->caret = b;
    00957     return true;
    00958   }
    00959   return false;
    00960 }
    00961
    00962 bool QueryString::HasEditBoxFocus(const Window *w, int wid) const
    00963 {
    00964   return ((w->window_class == WC_OSK &&
    00965       _focused_window == w->parent &&
    00966       w->parent->focused_widget &&
    00967       w->parent->focused_widget->type == WWT_EDITBOX) ||
    00968       w->IsWidgetGloballyFocused(wid));
    00969 }
    00970
    00971 HandleEditBoxResult QueryString::HandleEditBoxKey(Window *w, int wid, uint16 key, uint16 keycode, Window::EventState &state)
    00972 {
    00973   if (!QueryString::HasEditBoxFocus(w, wid)) return HEBR_NOT_FOCUSED;
    00974
    00975   state = Window::ES_HANDLED;
    00976
    00977   switch (keycode) {
    00978     case WKC_ESC: return HEBR_CANCEL;
    00979
    00980     case WKC_RETURN: case WKC_NUM_ENTER: return HEBR_CONFIRM;
    00981
    00982     case (WKC_CTRL | 'V'):
    00983       if (InsertTextBufferClipboard(&this->text)) w->InvalidateWidget(wid);
    00984       break;
    00985
    00986     case (WKC_CTRL | 'U'):
    00987       DeleteTextBufferAll(&this->text);
    00988       w->InvalidateWidget(wid);
    00989       break;
    00990
    00991     case WKC_BACKSPACE: case WKC_DELETE:
    00992       if (DeleteTextBufferChar(&this->text, keycode)) w->InvalidateWidget(wid);
    00993       break;
    00994
    00995     case WKC_LEFT: case WKC_RIGHT: case WKC_END: case WKC_HOME:
    00996       if (MoveTextBufferPos(&this->text, keycode)) w->InvalidateWidget(wid);
    00997       break;
    00998
    00999     default:
    01000       if (IsValidChar(key, this->afilter)) {
    01001         if (InsertTextBufferChar(&this->text, key)) w->InvalidateWidget(wid);
    01002       } else {
    01003         state = Window::ES_NOT_HANDLED;
    01004       }
    01005   }
    01006
    01007   return HEBR_EDITING;
    01008 }
    01009
    01010 void QueryString::HandleEditBox(Window *w, int wid)
    01011 {
    01012   if (HasEditBoxFocus(w, wid) && HandleCaret(&this->text)) {
    01013     w->InvalidateWidget(wid);
    01014     /* When we're not the OSK, notify 'our' OSK to redraw the widget,
    01015      * so the caret changes appropriately. */
    01016     if (w->window_class != WC_OSK) {
    01017       Window *w_osk = FindWindowById(WC_OSK, 0);
    01018       if (w_osk != NULL && w_osk->parent == w) w_osk->OnInvalidateData();
    01019     }
    01020   }
    01021 }
    01022
    01023 void QueryString::DrawEditBox(Window *w, int wid)
    01024 {
    01025   const Widget *wi = &w->widget[wid];
    01026
    01027   assert((wi->type & WWT_MASK) == WWT_EDITBOX);
    01028
    01029   GfxFillRect(wi->left + 1, wi->top + 1, wi->right - 1, wi->bottom - 1, 215);
    01030
    01031   DrawPixelInfo dpi;
    01032   int delta;
    01033
    01034   /* Limit the drawing of the string inside the widget boundaries */
    01035   if (!FillDrawPixelInfo(&dpi,
    01036       wi->left + 4,
    01037       wi->top + 1,
    01038       wi->right - wi->left - 4,
    01039       wi->bottom - wi->top - 1)) {
    01040     return;
    01041   }
    01042
    01043   DrawPixelInfo *old_dpi = _cur_dpi;
    01044   _cur_dpi = &dpi;
    01045
    01046   /* We will take the current widget length as maximum width, with a small
    01047    * space reserved at the end for the caret to show */
    01048   const Textbuf *tb = &this->text;
    01049
    01050   delta = (wi->right - wi->left) - tb->width - 10;
    01051   if (delta > 0) delta = 0;
    01052
    01053   if (tb->caretxoffs + delta < 0) delta = -tb->caretxoffs;
    01054
    01055   DoDrawString(tb->buf, delta, 0, TC_YELLOW);
    01056   if (HasEditBoxFocus(w, wid) && tb->caret) DoDrawString("_", tb->caretxoffs + delta, 0, TC_WHITE);
    01057
    01058   _cur_dpi = old_dpi;
    01059 }
    01060
    01061 HandleEditBoxResult QueryStringBaseWindow::HandleEditBoxKey(int wid, uint16 key, uint16 keycode, EventState &state)
    01062 {
    01063   return this->QueryString::HandleEditBoxKey(this, wid, key, keycode, state);
    01064 }
    01065
    01066 void QueryStringBaseWindow::HandleEditBox(int wid)
    01067 {
    01068   this->QueryString::HandleEditBox(this, wid);
    01069 }
    01070
    01071 void QueryStringBaseWindow::DrawEditBox(int wid)
    01072 {
    01073   this->QueryString::DrawEditBox(this, wid);
    01074 }
    01075
    01076 void QueryStringBaseWindow::OnOpenOSKWindow(int wid)
    01077 {
    01078   ShowOnScreenKeyboard(this, wid, 0, 0);
    01079 }
    01080
    01081 enum QueryStringWidgets {
    01082   QUERY_STR_WIDGET_TEXT = 3,
    01083   QUERY_STR_WIDGET_DEFAULT,
    01084   QUERY_STR_WIDGET_CANCEL,
    01085   QUERY_STR_WIDGET_OK
    01086 };
    01087
    01088
    01089 struct QueryStringWindow : public QueryStringBaseWindow
    01090 {
    01091   QueryStringWindow(uint16 size, const WindowDesc *desc, Window *parent) : QueryStringBaseWindow(size, desc)
    01092   {
    01093     this->parent = parent;
    01094     this->SetFocusedWidget(QUERY_STR_WIDGET_TEXT);
    01095
    01096     this->FindWindowPlacementAndResize(desc);
    01097   }
    01098
    01099   virtual void OnPaint()
    01100   {
    01101     SetDParam(0, this->caption);
    01102     this->DrawWidgets();
    01103
    01104     this->DrawEditBox(QUERY_STR_WIDGET_TEXT);
    01105   }
    01106
    01107   void OnOk()
    01108   {
    01109     if (this->orig == NULL || strcmp(this->text.buf, this->orig) != 0) {
    01110       /* If the parent is NULL, the editbox is handled by general function
    01111        * HandleOnEditText */
    01112       if (this->parent != NULL) {
    01113         this->parent->OnQueryTextFinished(this->text.buf);
    01114       } else {
    01115         HandleOnEditText(this->text.buf);
    01116       }
    01117       this->handled = true;
    01118     }
    01119   }
    01120
    01121   virtual void OnClick(Point pt, int widget)
    01122   {
    01123     switch (widget) {
    01124       case QUERY_STR_WIDGET_DEFAULT:
    01125         this->text.buf[0] = '\0';
    01126         /* Fallthrough */
    01127       case QUERY_STR_WIDGET_OK:
    01128         this->OnOk();
    01129         /* Fallthrough */
    01130       case QUERY_STR_WIDGET_CANCEL:
    01131         delete this;
    01132         break;
    01133     }
    01134   }
    01135
    01136   virtual void OnMouseLoop()
    01137   {
    01138     this->HandleEditBox(QUERY_STR_WIDGET_TEXT);
    01139   }
    01140
    01141   virtual EventState OnKeyPress(uint16 key, uint16 keycode)
    01142   {
    01143     EventState state = ES_NOT_HANDLED;
    01144     switch (this->HandleEditBoxKey(QUERY_STR_WIDGET_TEXT, key, keycode, state)) {
    01145       default: NOT_REACHED();
    01146       case HEBR_EDITING: {
    01147         Window *osk = FindWindowById(WC_OSK, 0);
    01148         if (osk != NULL && osk->parent == this) osk->OnInvalidateData();
    01149       } break;
    01150       case HEBR_CONFIRM: this->OnOk();
    01151       /* FALL THROUGH */
    01152       case HEBR_CANCEL: delete this; break; // close window, abandon changes
    01153       case HEBR_NOT_FOCUSED: break;
    01154     }
    01155     return state;
    01156   }
    01157
    01158   virtual void OnOpenOSKWindow(int wid)
    01159   {
    01160     ShowOnScreenKeyboard(this, wid, QUERY_STR_WIDGET_CANCEL, QUERY_STR_WIDGET_OK);
    01161   }
    01162
    01163   ~QueryStringWindow()
    01164   {
    01165     if (!this->handled && this->parent != NULL) {
    01166       Window *parent = this->parent;
    01167       this->parent = NULL; // so parent doesn't try to delete us again
    01168       parent->OnQueryTextFinished(NULL);
    01169     }
    01170   }
    01171 };
    01172
    01173 static const Widget _query_string_widgets[] = {
    01174 {   WWT_CLOSEBOX,   RESIZE_NONE,  COLOUR_GREY,     0,    10,     0,    13, STR_00C5,        STR_018B_CLOSE_WINDOW},
    01175 {    WWT_CAPTION,   RESIZE_NONE,  COLOUR_GREY,    11,   259,     0,    13, STR_012D,        STR_NULL},
    01176 {      WWT_PANEL,   RESIZE_NONE,  COLOUR_GREY,     0,   259,    14,    29, 0x0,             STR_NULL},
    01177 {    WWT_EDITBOX,   RESIZE_NONE,  COLOUR_GREY,     2,   257,    16,    27, 0x0,             STR_NULL}, // QUERY_STR_WIDGET_TEXT
    01178 {    WWT_TEXTBTN,   RESIZE_NONE,  COLOUR_GREY,     0,    86,    30,    41, STR_DEFAULT,     STR_NULL}, // QUERY_STR_WIDGET_DEFAULT
    01179 {    WWT_TEXTBTN,   RESIZE_NONE,  COLOUR_GREY,    87,   172,    30,    41, STR_012E_CANCEL, STR_NULL}, // QUERY_STR_WIDGET_CANCEL
    01180 {    WWT_TEXTBTN,   RESIZE_NONE,  COLOUR_GREY,   173,   259,    30,    41, STR_012F_OK,     STR_NULL}, // QUERY_STR_WIDGET_OK
    01181 {   WIDGETS_END},
    01182 };
    01183
    01184 static const WindowDesc _query_string_desc(
    01185   190, 219, 260, 42, 260, 42,
    01186   WC_QUERY_STRING, WC_NONE,
    01187   WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET,
    01188   _query_string_widgets
    01189 );
    01190
    01201 void ShowQueryString(StringID str, StringID caption, uint maxsize, uint maxwidth, Window *parent, CharSetFilter afilter, QueryStringFlags flags)
    01202 {
    01203   DeleteWindowById(WC_QUERY_STRING, 0);
    01204
    01205   QueryStringWindow *w = new QueryStringWindow(maxsize, &_query_string_desc, parent);
    01206
    01207   GetString(w->edit_str_buf, str, &w->edit_str_buf[maxsize - 1]);
    01208   w->edit_str_buf[maxsize - 1] = '\0';
    01209
    01210   if ((flags & QSF_ACCEPT_UNCHANGED) == 0) w->orig = strdup(w->edit_str_buf);
    01211
    01212   if ((flags & QSF_ENABLE_DEFAULT) == 0) {
    01213     /* without the "Default" button, make "Cancel" and "OK" buttons wider */
    01214     w->SetWidgetHiddenState(QUERY_STR_WIDGET_DEFAULT, true);
    01215     w->widget[QUERY_STR_WIDGET_CANCEL].left  = 0;
    01216     w->widget[QUERY_STR_WIDGET_CANCEL].right = w->width / 2 - 1;
    01217     w->widget[QUERY_STR_WIDGET_OK].left      = w->width / 2;
    01218     w->widget[QUERY_STR_WIDGET_OK].right     = w->width - 1;
    01219   }
    01220
    01221   w->LowerWidget(QUERY_STR_WIDGET_TEXT);
    01222   w->caption = caption;
    01223   w->afilter = afilter;
    01224   InitializeTextBuffer(&w->text, w->edit_str_buf, maxsize, maxwidth);
    01225 }
    01226
    01227
    01228 enum QueryWidgets {
    01229   QUERY_WIDGET_CAPTION = 1,
    01230   QUERY_WIDGET_NO = 3,
    01231   QUERY_WIDGET_YES
    01232 };
    01233
    01237 struct QueryWindow : public Window {
    01238   QueryCallbackProc *proc;
    01239   uint64 params[10];
    01240   StringID message;
    01241
    01242   QueryWindow(const WindowDesc *desc, StringID caption, StringID message, Window *parent, QueryCallbackProc *callback) : Window(desc)
    01243   {
    01244     if (parent == NULL) parent = FindWindowById(WC_MAIN_WINDOW, 0);
    01245     this->parent = parent;
    01246     this->left = parent->left + (parent->width / 2) - (this->width / 2);
    01247     this->top = parent->top + (parent->height / 2) - (this->height / 2);
    01248
    01249     /* Create a backup of the variadic arguments to strings because it will be
    01250      * overridden pretty often. We will copy these back for drawing */
    01251     CopyOutDParam(this->params, 0, lengthof(this->params));
    01252     this->widget[QUERY_WIDGET_CAPTION].data = caption;
    01253     this->message    = message;
    01254     this->proc       = callback;
    01255
    01256     this->FindWindowPlacementAndResize(desc);
    01257   }
    01258
    01259   ~QueryWindow()
    01260   {
    01261     if (this->proc != NULL) this->proc(this->parent, false);
    01262   }
    01263
    01264   virtual void OnPaint()
    01265   {
    01266     CopyInDParam(0, this->params, lengthof(this->params));
    01267     this->DrawWidgets();
    01268     CopyInDParam(0, this->params, lengthof(this->params));
    01269
    01270     DrawStringMultiCenter(this->width / 2, (this->height / 2) - 10, this->message, this->width - 2);
    01271   }
    01272
    01273   virtual void OnClick(Point pt, int widget)
    01274   {
    01275     switch (widget) {
    01276       case QUERY_WIDGET_YES: {
    01277         /* in the Generate New World window, clicking 'Yes' causes
    01278          * DeleteNonVitalWindows() to be called - we shouldn't be in a window then */
    01279         QueryCallbackProc *proc = this->proc;
    01280         Window *parent = this->parent;
    01281         /* Prevent the destructor calling the callback function */
    01282         this->proc = NULL;
    01283         delete this;
    01284         if (proc != NULL) {
    01285           proc(parent, true);
    01286           proc = NULL;
    01287         }
    01288       } break;
    01289       case QUERY_WIDGET_NO:
    01290         delete this;
    01291         break;
    01292     }
    01293   }
    01294
    01295   virtual EventState OnKeyPress(uint16 key, uint16 keycode)
    01296   {
    01297     /* ESC closes the window, Enter confirms the action */
    01298     switch (keycode) {
    01299       case WKC_RETURN:
    01300       case WKC_NUM_ENTER:
    01301         if (this->proc != NULL) {
    01302           this->proc(this->parent, true);
    01303           this->proc = NULL;
    01304         }
    01305         /* Fallthrough */
    01306       case WKC_ESC:
    01307         delete this;
    01308         return ES_HANDLED;
    01309     }
    01310     return ES_NOT_HANDLED;
    01311   }
    01312 };
    01313
    01314
    01315 static const Widget _query_widgets[] = {
    01316 {  WWT_CLOSEBOX, RESIZE_NONE,  COLOUR_RED,      0,  10,   0,  13, STR_00C5,        STR_018B_CLOSE_WINDOW},
    01317 {   WWT_CAPTION, RESIZE_NONE,  COLOUR_RED,     11, 209,   0,  13, STR_NULL,        STR_NULL},
    01318 {     WWT_PANEL, RESIZE_NONE,  COLOUR_RED,      0, 209,  14,  81, 0x0, /*OVERRIDE*/STR_NULL},
    01319 {WWT_PUSHTXTBTN, RESIZE_NONE,  COLOUR_YELLOW,  20,  90,  62,  73, STR_00C9_NO,     STR_NULL},
    01320 {WWT_PUSHTXTBTN, RESIZE_NONE,  COLOUR_YELLOW, 120, 190,  62,  73, STR_00C8_YES,    STR_NULL},
    01321 {   WIDGETS_END },
    01322 };
    01323
    01324 static const WindowDesc _query_desc(
    01325   WDP_CENTER, WDP_CENTER, 210, 82, 210, 82,
    01326   WC_CONFIRM_POPUP_QUERY, WC_NONE,
    01327   WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_UNCLICK_BUTTONS | WDF_DEF_WIDGET | WDF_MODAL,
    01328   _query_widgets
    01329 );
    01330
    01340 void ShowQuery(StringID caption, StringID message, Window *parent, QueryCallbackProc *callback)
    01341 {
    01342   new QueryWindow(&_query_desc, caption, message, parent, callback);
    01343 }
    01344
    01345
    01346 static const Widget _load_dialog_widgets[] = {
    01347 {   WWT_CLOSEBOX,   RESIZE_NONE,  COLOUR_GREY,     0,    10,     0,    13, STR_00C5,                 STR_018B_CLOSE_WINDOW},
    01348 {    WWT_CAPTION,  RESIZE_RIGHT,  COLOUR_GREY,    11,   256,     0,    13, STR_NULL,                 STR_018C_WINDOW_TITLE_DRAG_THIS},
    01349 { WWT_PUSHTXTBTN,   RESIZE_NONE,  COLOUR_GREY,     0,   127,    14,    25, STR_SORT_BY_NAME,         STR_SORT_ORDER_TIP},
    01350 { WWT_PUSHTXTBTN,   RESIZE_NONE,  COLOUR_GREY,   128,   256,    14,    25, STR_SORT_BY_DATE,         STR_SORT_ORDER_TIP},
    01351 {      WWT_PANEL,  RESIZE_RIGHT,  COLOUR_GREY,     0,   256,    26,    47, 0x0,                      STR_NULL},
    01352 {      WWT_PANEL,     RESIZE_RB,  COLOUR_GREY,     0,   256,    48,   153, 0x0,                      STR_NULL},
    01353 { WWT_PUSHIMGBTN,     RESIZE_LR,  COLOUR_GREY,   245,   256,    48,    59, SPR_HOUSE_ICON,           STR_SAVELOAD_HOME_BUTTON},
    01354 {      WWT_INSET,     RESIZE_RB,  COLOUR_GREY,     2,   243,    50,   139, 0x0,                      STR_400A_LIST_OF_DRIVES_DIRECTORIES},
    01355 {  WWT_SCROLLBAR,    RESIZE_LRB,  COLOUR_GREY,   245,   256,    60,   141, 0x0,                      STR_0190_SCROLL_BAR_SCROLLS_LIST},
    01356 { WWT_PUSHTXTBTN,    RESIZE_RTB,  COLOUR_GREY,     0,   243,   142,   153, STR_CONTENT_INTRO_BUTTON, STR_CONTENT_INTRO_BUTTON_TIP},
    01357 {  WWT_RESIZEBOX,   RESIZE_LRTB,  COLOUR_GREY,   245,   256,   142,   153, 0x0,                      STR_RESIZE_BUTTON},
    01358 {   WIDGETS_END},
    01359 };
    01360
    01361 static const Widget _save_dialog_widgets[] = {
    01362 {   WWT_CLOSEBOX,   RESIZE_NONE,  COLOUR_GREY,     0,    10,     0,    13, STR_00C5,         STR_018B_CLOSE_WINDOW},
    01363 {    WWT_CAPTION,  RESIZE_RIGHT,  COLOUR_GREY,    11,   256,     0,    13, STR_NULL,         STR_018C_WINDOW_TITLE_DRAG_THIS},
    01364 { WWT_PUSHTXTBTN,   RESIZE_NONE,  COLOUR_GREY,     0,   127,    14,    25, STR_SORT_BY_NAME, STR_SORT_ORDER_TIP},
    01365 { WWT_PUSHTXTBTN,   RESIZE_NONE,  COLOUR_GREY,   128,   256,    14,    25, STR_SORT_BY_DATE, STR_SORT_ORDER_TIP},
    01366 {      WWT_PANEL,  RESIZE_RIGHT,  COLOUR_GREY,     0,   256,    26,    47, 0x0,              STR_NULL},
    01367 {      WWT_PANEL,     RESIZE_RB,  COLOUR_GREY,     0,   256,    48,   167, 0x0,              STR_NULL},
    01368 { WWT_PUSHIMGBTN,     RESIZE_LR,  COLOUR_GREY,   245,   256,    48,    59, SPR_HOUSE_ICON,   STR_SAVELOAD_HOME_BUTTON},
    01369 {      WWT_INSET,     RESIZE_RB,  COLOUR_GREY,     2,   243,    50,   150, 0x0,              STR_400A_LIST_OF_DRIVES_DIRECTORIES},
    01370 {  WWT_SCROLLBAR,    RESIZE_LRB,  COLOUR_GREY,   245,   256,    60,   151, 0x0,              STR_0190_SCROLL_BAR_SCROLLS_LIST},
    01371 {      WWT_PANEL,    RESIZE_RTB,  COLOUR_GREY,     0,   256,   152,     0, 0x0,              STR_NULL},
    01372 {    WWT_EDITBOX,    RESIZE_RTB,  COLOUR_GREY,     2,   254,   154,   165, STR_SAVE_OSKTITLE, STR_400B_CURRENTLY_SELECTED_NAME},
    01373 { WWT_PUSHTXTBTN,     RESIZE_TB,  COLOUR_GREY,     0,   127,   168,   179, STR_4003_DELETE,  STR_400C_DELETE_THE_CURRENTLY_SELECTED},
    01374 { WWT_PUSHTXTBTN,     RESIZE_TB,  COLOUR_GREY,   128,   244,   168,   179, STR_4002_SAVE,    STR_400D_SAVE_THE_CURRENT_GAME_USING},
    01375 {  WWT_RESIZEBOX,   RESIZE_LRTB,  COLOUR_GREY,   245,   256,   168,   179, 0x0,              STR_RESIZE_BUTTON},
    01376 {   WIDGETS_END},
    01377 };
    01378
    01379 /* Colours for fios types */
    01380 const TextColour _fios_colours[] = {
    01381   TC_LIGHT_BLUE, TC_DARK_GREEN,  TC_DARK_GREEN, TC_ORANGE, TC_LIGHT_BROWN,
    01382   TC_ORANGE,     TC_LIGHT_BROWN, TC_ORANGE,     TC_ORANGE, TC_YELLOW
    01383 };
    01384
    01385 void BuildFileList()
    01386 {
    01387   _fios_path_changed = true;
    01388   FiosFreeSavegameList();
    01389
    01390   switch (_saveload_mode) {
    01391     case SLD_NEW_GAME:
    01392     case SLD_LOAD_SCENARIO:
    01393     case SLD_SAVE_SCENARIO:
    01394       FiosGetScenarioList(_saveload_mode); break;
    01395     case SLD_LOAD_HEIGHTMAP:
    01396       FiosGetHeightmapList(_saveload_mode); break;
    01397
    01398     default: FiosGetSavegameList(_saveload_mode); break;
    01399   }
    01400 }
    01401
    01402 static void DrawFiosTexts(uint maxw)
    01403 {
    01404   static const char *path = NULL;
    01405   static StringID str = STR_4006_UNABLE_TO_READ_DRIVE;
    01406   static uint64 tot = 0;
    01407
    01408   if (_fios_path_changed) {
    01409     str = FiosGetDescText(&path, &tot);
    01410     _fios_path_changed = false;
    01411   }
    01412
    01413   if (str != STR_4006_UNABLE_TO_READ_DRIVE) SetDParam(0, tot);
    01414   DrawString(2, 37, str, TC_FROMSTRING);
    01415   DoDrawStringTruncated(path, 2, 27, TC_BLACK, maxw);
    01416 }
    01417
    01418 static void MakeSortedSaveGameList()
    01419 {
    01420   uint sort_start = 0;
    01421   uint sort_end = 0;
    01422
    01423   /* Directories are always above the files (FIOS_TYPE_DIR)
    01424    * Drives (A:\ (windows only) are always under the files (FIOS_TYPE_DRIVE)
    01425    * Only sort savegames/scenarios, not directories
    01426    */
    01427   for (const FiosItem *item = _fios_items.Begin(); item != _fios_items.End(); item++) {
    01428     switch (item->type) {
    01429       case FIOS_TYPE_DIR:    sort_start++; break;
    01430       case FIOS_TYPE_PARENT: sort_start++; break;
    01431       case FIOS_TYPE_DRIVE:  sort_end++;   break;
    01432       default: break;
    01433     }
    01434   }
    01435
    01436   uint s_amount = _fios_items.Length() - sort_start - sort_end;
    01437   if (s_amount > 0) {
    01438     qsort(_fios_items.Get(sort_start), s_amount, sizeof(FiosItem), compare_FiosItems);
    01439   }
    01440 }
    01441
    01442 extern void StartupEngines();
    01443
    01444 struct SaveLoadWindow : public QueryStringBaseWindow {
    01445 private:
    01446   enum SaveLoadWindowWidgets {
    01447     SLWW_CLOSE = 0,
    01448     SLWW_WINDOWTITLE,
    01449     SLWW_SORT_BYNAME,
    01450     SLWW_SORT_BYDATE,
    01451     SLWW_HOME_BUTTON = 6,
    01452     SLWW_DRIVES_DIRECTORIES_LIST,
    01453     SLWW_CONTENT_DOWNLOAD = 9,
    01454     SLWW_SAVE_OSK_TITLE,
    01455     SLWW_DELETE_SELECTION,
    01456     SLWW_SAVE_GAME,
    01457   };
    01458
    01459   FiosItem o_dir;
    01460 public:
    01461
    01462   void GenerateFileName()
    01463   {
    01464     GenerateDefaultSaveName(this->edit_str_buf, &this->edit_str_buf[this->edit_str_size - 1]);
    01465   }
    01466
    01467   SaveLoadWindow(const WindowDesc *desc, SaveLoadDialogMode mode) : QueryStringBaseWindow(64, desc)
    01468   {
    01469     static const StringID saveload_captions[] = {
    01470       STR_4001_LOAD_GAME,
    01471       STR_0298_LOAD_SCENARIO,
    01472       STR_4000_SAVE_GAME,
    01473       STR_0299_SAVE_SCENARIO,
    01474       STR_LOAD_HEIGHTMAP,
    01475     };
    01476
    01477     this->vscroll.cap = 10;
    01478     this->resize.step_width = 2;
    01479     this->resize.step_height = 10;
    01480
    01481     SetObjectToPlace(SPR_CURSOR_ZZZ, PAL_NONE, VHM_NONE, WC_MAIN_WINDOW, 0);
    01482
    01483     /* Use an array to define what will be the current file type being handled
    01484      * by current file mode */
    01485     switch (mode) {
    01486       case SLD_LOAD_GAME:
    01487         this->HideWidget(SLWW_CONTENT_DOWNLOAD);
    01488         this->widget[SLWW_DRIVES_DIRECTORIES_LIST].bottom += this->widget[SLWW_CONTENT_DOWNLOAD].bottom - this->widget[SLWW_CONTENT_DOWNLOAD].top;
    01489         break;
    01490
    01491       case SLD_LOAD_SCENARIO:
    01492       case SLD_LOAD_HEIGHTMAP:
    01493         this->vscroll.cap--;
    01494
    01495       case SLD_SAVE_GAME:     this->GenerateFileName(); break;
    01496       case SLD_SAVE_SCENARIO: strecpy(this->edit_str_buf, "UNNAMED", &this->edit_str_buf[edit_str_size - 1]); break;
    01497       default:                break;
    01498     }
    01499
    01500     assert((uint)mode < lengthof(saveload_captions));
    01501
    01502     this->widget[SLWW_WINDOWTITLE].data = saveload_captions[mode];
    01503     this->LowerWidget(SLWW_DRIVES_DIRECTORIES_LIST);
    01504
    01505     this->afilter = CS_ALPHANUMERAL;
    01506     InitializeTextBuffer(&this->text, this->edit_str_buf, this->edit_str_size, 240);
    01507
    01508     /* pause is only used in single-player, non-editor mode, non-menu mode. It
    01509      * will be unpaused in the WE_DESTROY event handler. */
    01510     if (_game_mode != GM_MENU && !_networking && _game_mode != GM_EDITOR) {
    01511       if (_pause_game >= 0) DoCommandP(0, 1, 0, CMD_PAUSE);
    01512     }
    01513
    01514     BuildFileList();
    01515
    01516     ResetObjectToPlace();
    01517
    01518     o_dir.type = FIOS_TYPE_DIRECT;
    01519     switch (_saveload_mode) {
    01520       case SLD_SAVE_GAME:
    01521       case SLD_LOAD_GAME:
    01522         FioGetDirectory(o_dir.name, lengthof(o_dir.name), SAVE_DIR);
    01523         break;
    01524
    01525       case SLD_SAVE_SCENARIO:
    01526       case SLD_LOAD_SCENARIO:
    01527         FioGetDirectory(o_dir.name, lengthof(o_dir.name), SCENARIO_DIR);
    01528         break;
    01529
    01530       case SLD_LOAD_HEIGHTMAP:
    01531         FioGetDirectory(o_dir.name, lengthof(o_dir.name), HEIGHTMAP_DIR);
    01532         break;
    01533
    01534       default:
    01535         strecpy(o_dir.name, _personal_dir, lastof(o_dir.name));
    01536     }
    01537
    01538     /* Focus the edit box by default in the save windows */
    01539     if (_saveload_mode == SLD_SAVE_GAME || _saveload_mode == SLD_SAVE_SCENARIO) {
    01540       this->SetFocusedWidget(SLWW_SAVE_OSK_TITLE);
    01541     }
    01542
    01543     this->FindWindowPlacementAndResize(desc);
    01544   }
    01545
    01546   virtual ~SaveLoadWindow()
    01547   {
    01548     /* pause is only used in single-player, non-editor mode, non menu mode */
    01549     if (!_networking && _game_mode != GM_EDITOR && _game_mode != GM_MENU) {
    01550       if (_pause_game >= 0) DoCommandP(0, 0, 0, CMD_PAUSE);
    01551     }
    01552     FiosFreeSavegameList();
    01553   }
    01554
    01555   virtual void OnPaint()
    01556   {
    01557     int y;
    01558
    01559     SetVScrollCount(this, _fios_items.Length());
    01560     this->DrawWidgets();
    01561     DrawFiosTexts(this->width);
    01562
    01563     if (_savegame_sort_dirty) {
    01564       _savegame_sort_dirty = false;
    01565       MakeSortedSaveGameList();
    01566     }
    01567
    01568     const Widget *widg = &this->widget[SLWW_DRIVES_DIRECTORIES_LIST];
    01569     GfxFillRect(widg->left + 1, widg->top + 1, widg->right, widg->bottom, 0xD7);
    01570     this->DrawSortButtonState(_savegame_sort_order & SORT_BY_NAME ? SLWW_SORT_BYNAME : SLWW_SORT_BYDATE, _savegame_sort_order & SORT_DESCENDING ? SBS_DOWN : SBS_UP);
    01571
    01572     y = widg->top + 1;
    01573     for (uint pos = this->vscroll.pos; pos < _fios_items.Length(); pos++) {
    01574       const FiosItem *item = _fios_items.Get(pos);
    01575
    01576       DoDrawStringTruncated(item->title, 4, y, _fios_colours[item->type], this->width - 18);
    01577       y += 10;
    01578       if (y >= this->vscroll.cap * 10 + widg->top + 1) break;
    01579     }
    01580
    01581     if (_saveload_mode == SLD_SAVE_GAME || _saveload_mode == SLD_SAVE_SCENARIO) {
    01582       this->DrawEditBox(SLWW_SAVE_OSK_TITLE);
    01583     }
    01584   }
    01585
    01586   virtual void OnClick(Point pt, int widget)
    01587   {
    01588     switch (widget) {
    01589       case SLWW_SORT_BYNAME: // Sort save names by name
    01590         _savegame_sort_order = (_savegame_sort_order == SORT_BY_NAME) ?
    01591           SORT_BY_NAME | SORT_DESCENDING : SORT_BY_NAME;
    01592         _savegame_sort_dirty = true;
    01593         this->SetDirty();
    01594         break;
    01595
    01596       case SLWW_SORT_BYDATE: // Sort save names by date
    01597         _savegame_sort_order = (_savegame_sort_order == SORT_BY_DATE) ?
    01598           SORT_BY_DATE | SORT_DESCENDING : SORT_BY_DATE;
    01599         _savegame_sort_dirty = true;
    01600         this->SetDirty();
    01601         break;
    01602
    01603       case SLWW_HOME_BUTTON: // OpenTTD 'button', jumps to OpenTTD directory
    01604         FiosBrowseTo(&o_dir);
    01605         this->SetDirty();
    01606         BuildFileList();
    01607         break;
    01608
    01609       case SLWW_DRIVES_DIRECTORIES_LIST: { // Click the listbox
    01610         int y = (pt.y - this->widget[widget].top - 1) / 10;
    01611
    01612         if (y < 0 || (y += this->vscroll.pos) >= this->vscroll.count) return;
    01613
    01614         const FiosItem *file = _fios_items.Get(y);
    01615
    01616         const char *name = FiosBrowseTo(file);
    01617         if (name != NULL) {
    01618           if (_saveload_mode == SLD_LOAD_GAME || _saveload_mode == SLD_LOAD_SCENARIO) {
    01619             _switch_mode = (_game_mode == GM_EDITOR) ? SM_LOAD_SCENARIO : SM_LOAD;
    01620
    01621             SetFiosType(file->type);
    01622             strecpy(_file_to_saveload.name, name, lastof(_file_to_saveload.name));
    01623             strecpy(_file_to_saveload.title, file->title, lastof(_file_to_saveload.title));
    01624
    01625             delete this;
    01626           } else if (_saveload_mode == SLD_LOAD_HEIGHTMAP) {
    01627             SetFiosType(file->type);
    01628             strecpy(_file_to_saveload.name, name, lastof(_file_to_saveload.name));
    01629             strecpy(_file_to_saveload.title, file->title, lastof(_file_to_saveload.title));
    01630
    01631             delete this;
    01632             ShowHeightmapLoad();
    01633           } else {
    01634             /* SLD_SAVE_GAME, SLD_SAVE_SCENARIO copy clicked name to editbox */
    01635             ttd_strlcpy(this->text.buf, file->title, this->text.maxsize);
    01636             UpdateTextBufferSize(&this->text);
    01637             this->InvalidateWidget(SLWW_SAVE_OSK_TITLE);
    01638           }
    01639         } else {
    01640           /* Changed directory, need repaint. */
    01641           this->SetDirty();
    01642           BuildFileList();
    01643         }
    01644         break;
    01645       }
    01646
    01647       case SLWW_CONTENT_DOWNLOAD:
    01648         if (!_network_available) {
    01649           ShowErrorMessage(INVALID_STRING_ID, STR_NETWORK_ERR_NOTAVAILABLE, 0, 0);
    01650         } else {
    01651 #if defined(ENABLE_NETWORK)
    01652           switch (_saveload_mode) {
    01653             default: NOT_REACHED();
    01654             case SLD_LOAD_SCENARIO:  ShowNetworkContentListWindow(NULL, CONTENT_TYPE_SCENARIO);  break;
    01655             case SLD_LOAD_HEIGHTMAP: ShowNetworkContentListWindow(NULL, CONTENT_TYPE_HEIGHTMAP); break;
    01656           }
    01657 #endif
    01658         }
    01659         break;
    01660
    01661       case SLWW_DELETE_SELECTION: case SLWW_SAVE_GAME: // Delete, Save game
    01662         break;
    01663     }
    01664   }
    01665
    01666   virtual void OnMouseLoop()
    01667   {
    01668     if (_saveload_mode == SLD_SAVE_GAME || _saveload_mode == SLD_SAVE_SCENARIO) {
    01669       this->HandleEditBox(SLWW_SAVE_OSK_TITLE);
    01670     }
    01671   }
    01672
    01673   virtual EventState OnKeyPress(uint16 key, uint16 keycode)
    01674   {
    01675     if (keycode == WKC_ESC) {
    01676       delete this;
    01677       return ES_HANDLED;
    01678     }
    01679
    01680     EventState state = ES_NOT_HANDLED;
    01681     if ((_saveload_mode == SLD_SAVE_GAME || _saveload_mode == SLD_SAVE_SCENARIO) &&
    01682         this->HandleEditBoxKey(SLWW_SAVE_OSK_TITLE, key, keycode, state) == HEBR_CONFIRM) {
    01683       this->HandleButtonClick(SLWW_SAVE_GAME);
    01684     }
    01685
    01686     return state;
    01687   }
    01688
    01689   virtual void OnTimeout()
    01690   {
    01691     /* This test protects against using widgets 11 and 12 which are only available
    01692      * in those two saveload mode */
    01693     if (!(_saveload_mode == SLD_SAVE_GAME || _saveload_mode == SLD_SAVE_SCENARIO)) return;
    01694
    01695     if (this->IsWidgetLowered(SLWW_DELETE_SELECTION)) { // Delete button clicked
    01696       if (!FiosDelete(this->text.buf)) {
    01697         ShowErrorMessage(INVALID_STRING_ID, STR_4008_UNABLE_TO_DELETE_FILE, 0, 0);
    01698       } else {
    01699         BuildFileList();
    01700         /* Reset file name to current date on successful delete */
    01701         if (_saveload_mode == SLD_SAVE_GAME) GenerateFileName();
    01702       }
    01703
    01704       UpdateTextBufferSize(&this->text);
    01705       this->SetDirty();
    01706     } else if (this->IsWidgetLowered(SLWW_SAVE_GAME)) { // Save button clicked
    01707       _switch_mode = SM_SAVE;
    01708       FiosMakeSavegameName(_file_to_saveload.name, this->text.buf, sizeof(_file_to_saveload.name));
    01709
    01710       /* In the editor set up the vehicle engines correctly (date might have changed) */
    01711       if (_game_mode == GM_EDITOR) StartupEngines();
    01712     }
    01713   }
    01714
    01715   virtual void OnResize(Point new_size, Point delta)
    01716   {
    01717     /* Widget 2 and 3 have to go with halve speed, make it so obiwan */
    01718     uint diff = delta.x / 2;
    01719     this->widget[SLWW_SORT_BYNAME].right += diff;
    01720     this->widget[SLWW_SORT_BYDATE].left  += diff;
    01721     this->widget[SLWW_SORT_BYDATE].right += delta.x;
    01722
    01723     /* Same for widget 11 and 12 in save-dialog */
    01724     if (_saveload_mode == SLD_SAVE_GAME || _saveload_mode == SLD_SAVE_SCENARIO) {
    01725       this->widget[SLWW_DELETE_SELECTION].right += diff;
    01726       this->widget[SLWW_SAVE_GAME].left  += diff;
    01727       this->widget[SLWW_SAVE_GAME].right += delta.x;
    01728     }
    01729
    01730     this->vscroll.cap += delta.y / 10;
    01731   }
    01732
    01733   virtual void OnInvalidateData(int data)
    01734   {
    01735     BuildFileList();
    01736   }
    01737 };
    01738
    01739 static const WindowDesc _load_dialog_desc(
    01740   WDP_CENTER, WDP_CENTER, 257, 154, 257, 294,
    01741   WC_SAVELOAD, WC_NONE,
    01742   WDF_STD_TOOLTIPS | WDF_DEF_WIDGET | WDF_STD_BTN | WDF_UNCLICK_BUTTONS | WDF_RESIZABLE,
    01743   _load_dialog_widgets
    01744 );
    01745
    01746 static const WindowDesc _save_dialog_desc(
    01747   WDP_CENTER, WDP_CENTER, 257, 180, 257, 320,
    01748   WC_SAVELOAD, WC_NONE,
    01749   WDF_STD_TOOLTIPS | WDF_DEF_WIDGET | WDF_STD_BTN | WDF_UNCLICK_BUTTONS | WDF_RESIZABLE,
    01750   _save_dialog_widgets
    01751 );
    01752
    01755 static const FileType _file_modetotype[] = {
    01756   FT_SAVEGAME,
    01757   FT_SCENARIO,
    01758   FT_SAVEGAME,
    01759   FT_SCENARIO,
    01760   FT_HEIGHTMAP,
    01761   FT_SAVEGAME,
    01762 };
    01763
    01764 void ShowSaveLoadDialog(SaveLoadDialogMode mode)
    01765 {
    01766   DeleteWindowById(WC_SAVELOAD, 0);
    01767
    01768   const WindowDesc *sld;
    01769   switch (mode) {
    01770     case SLD_SAVE_GAME:
    01771     case SLD_SAVE_SCENARIO:
    01772       sld = &_save_dialog_desc; break;
    01773     default:
    01774       sld = &_load_dialog_desc; break;
    01775   }
    01776
    01777   _saveload_mode = mode;
    01778   _file_to_saveload.filetype = _file_modetotype[mode];
    01779
    01780   new SaveLoadWindow(sld, mode);
    01781 }
    01782
    01783 void RedrawAutosave()
    01784 {
    01785   SetWindowDirty(FindWindowById(WC_STATUS_BAR, 0));
    01786 }
    01787
    01788 void SetFiosType(const byte fiostype)
    01789 {
    01790   switch (fiostype) {
    01791     case FIOS_TYPE_FILE:
    01792     case FIOS_TYPE_SCENARIO:
    01793       _file_to_saveload.mode = SL_LOAD;
    01794       break;
    01795
    01796     case FIOS_TYPE_OLDFILE:
    01797     case FIOS_TYPE_OLD_SCENARIO:
    01798       _file_to_saveload.mode = SL_OLD_LOAD;
    01799       break;
    01800
    01801 #ifdef WITH_PNG
    01802     case FIOS_TYPE_PNG:
    01803       _file_to_saveload.mode = SL_PNG;
    01804       break;
    01805 #endif /* WITH_PNG */
    01806
    01807     case FIOS_TYPE_BMP:
    01808       _file_to_saveload.mode = SL_BMP;
    01809       break;
    01810
    01811     default:
    01812       _file_to_saveload.mode = SL_INVALID;
    01813       break;
    01814   }
    01815 }