# misc_gui.cpp

Go to the documentation of this file.

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