# misc_gui.cpp

Go to the documentation of this file.

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