# misc_gui.cpp

Go to the documentation of this file.

    00001 /* $Id: misc_gui.cpp 26412 2014-03-18 18:13:33Z planetmaker $ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
    00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
    00008  */
    00009
    00012 #include "stdafx.h"
    00013 #include "debug.h"
    00014 #include "landscape.h"
    00015 #include "error.h"
    00016 #include "gui.h"
    00017 #include "command_func.h"
    00018 #include "company_func.h"
    00019 #include "town.h"
    00020 #include "string_func.h"
    00021 #include "company_base.h"
    00022 #include "texteff.hpp"
    00023 #include "strings_func.h"
    00024 #include "window_func.h"
    00025 #include "querystring_gui.h"
    00026 #include "core/geometry_func.hpp"
    00027 #include "newgrf_debug.h"
    00028
    00029 #include "widgets/misc_widget.h"
    00030
    00031 #include "table/strings.h"
    00032
    00034 enum OskActivation {
    00035   OSKA_DISABLED,
    00036   OSKA_DOUBLE_CLICK,
    00037   OSKA_SINGLE_CLICK,
    00038   OSKA_IMMEDIATELY,
    00039 };
    00040
    00041
    00042 static const NWidgetPart _nested_land_info_widgets[] = {
    00043   NWidget(NWID_HORIZONTAL),
    00044     NWidget(WWT_CLOSEBOX, COLOUR_GREY),
    00045     NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_LAND_AREA_INFORMATION_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    00046     NWidget(WWT_DEBUGBOX, COLOUR_GREY),
    00047   EndContainer(),
    00048   NWidget(WWT_PANEL, COLOUR_GREY, WID_LI_BACKGROUND), EndContainer(),
    00049 };
    00050
    00051 static WindowDesc _land_info_desc(
    00052   WDP_AUTO, "land_info", 0, 0,
    00053   WC_LAND_INFO, WC_NONE,
    00054   0,
    00055   _nested_land_info_widgets, lengthof(_nested_land_info_widgets)
    00056 );
    00057
    00058 class LandInfoWindow : public Window {
    00059   enum LandInfoLines {
    00060     LAND_INFO_CENTERED_LINES   = 32,
    00061     LAND_INFO_MULTICENTER_LINE = LAND_INFO_CENTERED_LINES,
    00062     LAND_INFO_LINE_END,
    00063   };
    00064
    00065   static const uint LAND_INFO_LINE_BUFF_SIZE = 512;
    00066
    00067 public:
    00068   char landinfo_data[LAND_INFO_LINE_END][LAND_INFO_LINE_BUFF_SIZE];
    00069   TileIndex tile;
    00070
    00071   virtual void DrawWidget(const Rect &r, int widget) const
    00072   {
    00073     if (widget != WID_LI_BACKGROUND) return;
    00074
    00075     uint y = r.top + WD_TEXTPANEL_TOP;
    00076     for (uint i = 0; i < LAND_INFO_CENTERED_LINES; i++) {
    00077       if (StrEmpty(this->landinfo_data[i])) break;
    00078
    00079       DrawString(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, y, this->landinfo_data[i], i == 0 ? TC_LIGHT_BLUE : TC_FROMSTRING, SA_HOR_CENTER);
    00080       y += FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL;
    00081       if (i == 0) y += 4;
    00082     }
    00083
    00084     if (!StrEmpty(this->landinfo_data[LAND_INFO_MULTICENTER_LINE])) {
    00085       SetDParamStr(0, this->landinfo_data[LAND_INFO_MULTICENTER_LINE]);
    00086       DrawStringMultiLine(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, y, r.bottom - WD_TEXTPANEL_BOTTOM, STR_JUST_RAW_STRING, TC_FROMSTRING, SA_CENTER);
    00087     }
    00088   }
    00089
    00090   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
    00091   {
    00092     if (widget != WID_LI_BACKGROUND) return;
    00093
    00094     size->height = WD_TEXTPANEL_TOP + WD_TEXTPANEL_BOTTOM;
    00095     for (uint i = 0; i < LAND_INFO_CENTERED_LINES; i++) {
    00096       if (StrEmpty(this->landinfo_data[i])) break;
    00097
    00098       uint width = GetStringBoundingBox(this->landinfo_data[i]).width + WD_FRAMETEXT_LEFT + WD_FRAMETEXT_RIGHT;
    00099       size->width = max(size->width, width);
    00100
    00101       size->height += FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL;
    00102       if (i == 0) size->height += 4;
    00103     }
    00104
    00105     if (!StrEmpty(this->landinfo_data[LAND_INFO_MULTICENTER_LINE])) {
    00106       uint width = GetStringBoundingBox(this->landinfo_data[LAND_INFO_MULTICENTER_LINE]).width + WD_FRAMETEXT_LEFT + WD_FRAMETEXT_RIGHT;
    00107       size->width = max(size->width, min(300u, width));
    00108       SetDParamStr(0, this->landinfo_data[LAND_INFO_MULTICENTER_LINE]);
    00109       size->height += GetStringHeight(STR_JUST_RAW_STRING, size->width - WD_FRAMETEXT_LEFT - WD_FRAMETEXT_RIGHT);
    00110     }
    00111   }
    00112
    00113   LandInfoWindow(TileIndex tile) : Window(&_land_info_desc), tile(tile)
    00114   {
    00115     this->InitNested();
    00116
    00117 #if defined(_DEBUG)
    00118 # define LANDINFOD_LEVEL 0
    00119 #else
    00120 # define LANDINFOD_LEVEL 1
    00121 #endif
    00122     DEBUG(misc, LANDINFOD_LEVEL, "TILE: %#x (%i,%i)", tile, TileX(tile), TileY(tile));
    00123     DEBUG(misc, LANDINFOD_LEVEL, "type_height  = %#x", _m[tile].type_height);
    00124     DEBUG(misc, LANDINFOD_LEVEL, "m1           = %#x", _m[tile].m1);
    00125     DEBUG(misc, LANDINFOD_LEVEL, "m2           = %#x", _m[tile].m2);
    00126     DEBUG(misc, LANDINFOD_LEVEL, "m3           = %#x", _m[tile].m3);
    00127     DEBUG(misc, LANDINFOD_LEVEL, "m4           = %#x", _m[tile].m4);
    00128     DEBUG(misc, LANDINFOD_LEVEL, "m5           = %#x", _m[tile].m5);
    00129     DEBUG(misc, LANDINFOD_LEVEL, "m6           = %#x", _m[tile].m6);
    00130     DEBUG(misc, LANDINFOD_LEVEL, "m7           = %#x", _me[tile].m7);
    00131 #undef LANDINFOD_LEVEL
    00132   }
    00133
    00134   virtual void OnInit()
    00135   {
    00136     Town *t = ClosestTownFromTile(tile, _settings_game.economy.dist_local_authority);
    00137
    00138     /* Because build_date is not set yet in every TileDesc, we make sure it is empty */
    00139     TileDesc td;
    00140
    00141     td.build_date = INVALID_DATE;
    00142
    00143     /* Most tiles have only one owner, but
    00144      *  - drivethrough roadstops can be build on town owned roads (up to 2 owners) and
    00145      *  - roads can have up to four owners (railroad, road, tram, 3rd-roadtype "highway").
    00146      */
    00147     td.owner_type[0] = STR_LAND_AREA_INFORMATION_OWNER; // At least one owner is displayed, though it might be "N/A".
    00148     td.owner_type[1] = STR_NULL;       // STR_NULL results in skipping the owner
    00149     td.owner_type[2] = STR_NULL;
    00150     td.owner_type[3] = STR_NULL;
    00151     td.owner[0] = OWNER_NONE;
    00152     td.owner[1] = OWNER_NONE;
    00153     td.owner[2] = OWNER_NONE;
    00154     td.owner[3] = OWNER_NONE;
    00155
    00156     td.station_class = STR_NULL;
    00157     td.station_name = STR_NULL;
    00158     td.airport_class = STR_NULL;
    00159     td.airport_name = STR_NULL;
    00160     td.airport_tile_name = STR_NULL;
    00161     td.rail_speed = 0;
    00162     td.road_speed = 0;
    00163
    00164     td.grf = NULL;
    00165
    00166     CargoArray acceptance;
    00167     AddAcceptedCargo(tile, acceptance, NULL);
    00168     GetTileDesc(tile, &td);
    00169
    00170     uint line_nr = 0;
    00171
    00172     /* Tiletype */
    00173     SetDParam(0, td.dparam[0]);
    00174     GetString(this->landinfo_data[line_nr], td.str, lastof(this->landinfo_data[line_nr]));
    00175     line_nr++;
    00176
    00177     /* Up to four owners */
    00178     for (uint i = 0; i < 4; i++) {
    00179       if (td.owner_type[i] == STR_NULL) continue;
    00180
    00181       SetDParam(0, STR_LAND_AREA_INFORMATION_OWNER_N_A);
    00182       if (td.owner[i] != OWNER_NONE && td.owner[i] != OWNER_WATER) GetNameOfOwner(td.owner[i], tile);
    00183       GetString(this->landinfo_data[line_nr], td.owner_type[i], lastof(this->landinfo_data[line_nr]));
    00184       line_nr++;
    00185     }
    00186
    00187     /* Cost to clear/revenue when cleared */
    00188     StringID str = STR_LAND_AREA_INFORMATION_COST_TO_CLEAR_N_A;
    00189     Company *c = Company::GetIfValid(_local_company);
    00190     if (c != NULL) {
    00191       Money old_money = c->money;
    00192       c->money = INT64_MAX;
    00193       assert(_current_company == _local_company);
    00194       CommandCost costclear = DoCommand(tile, 0, 0, DC_NONE, CMD_LANDSCAPE_CLEAR);
    00195       c->money = old_money;
    00196       if (costclear.Succeeded()) {
    00197         Money cost = costclear.GetCost();
    00198         if (cost < 0) {
    00199           cost = -cost; // Negate negative cost to a positive revenue
    00200           str = STR_LAND_AREA_INFORMATION_REVENUE_WHEN_CLEARED;
    00201         } else {
    00202           str = STR_LAND_AREA_INFORMATION_COST_TO_CLEAR;
    00203         }
    00204         SetDParam(0, cost);
    00205       }
    00206     }
    00207     GetString(this->landinfo_data[line_nr], str, lastof(this->landinfo_data[line_nr]));
    00208     line_nr++;
    00209
    00210     /* Location */
    00211     char tmp[16];
    00212     snprintf(tmp, lengthof(tmp), "0x%.4X", tile);
    00213     SetDParam(0, TileX(tile));
    00214     SetDParam(1, TileY(tile));
    00215     SetDParam(2, GetTileZ(tile));
    00216     SetDParamStr(3, tmp);
    00217     GetString(this->landinfo_data[line_nr], STR_LAND_AREA_INFORMATION_LANDINFO_COORDS, lastof(this->landinfo_data[line_nr]));
    00218     line_nr++;
    00219
    00220     /* Local authority */
    00221     SetDParam(0, STR_LAND_AREA_INFORMATION_LOCAL_AUTHORITY_NONE);
    00222     if (t != NULL) {
    00223       SetDParam(0, STR_TOWN_NAME);
    00224       SetDParam(1, t->index);
    00225     }
    00226     GetString(this->landinfo_data[line_nr], STR_LAND_AREA_INFORMATION_LOCAL_AUTHORITY, lastof(this->landinfo_data[line_nr]));
    00227     line_nr++;
    00228
    00229     /* Build date */
    00230     if (td.build_date != INVALID_DATE) {
    00231       SetDParam(0, td.build_date);
    00232       GetString(this->landinfo_data[line_nr], STR_LAND_AREA_INFORMATION_BUILD_DATE, lastof(this->landinfo_data[line_nr]));
    00233       line_nr++;
    00234     }
    00235
    00236     /* Station class */
    00237     if (td.station_class != STR_NULL) {
    00238       SetDParam(0, td.station_class);
    00239       GetString(this->landinfo_data[line_nr], STR_LAND_AREA_INFORMATION_STATION_CLASS, lastof(this->landinfo_data[line_nr]));
    00240       line_nr++;
    00241     }
    00242
    00243     /* Station type name */
    00244     if (td.station_name != STR_NULL) {
    00245       SetDParam(0, td.station_name);
    00246       GetString(this->landinfo_data[line_nr], STR_LAND_AREA_INFORMATION_STATION_TYPE, lastof(this->landinfo_data[line_nr]));
    00247       line_nr++;
    00248     }
    00249
    00250     /* Airport class */
    00251     if (td.airport_class != STR_NULL) {
    00252       SetDParam(0, td.airport_class);
    00253       GetString(this->landinfo_data[line_nr], STR_LAND_AREA_INFORMATION_AIRPORT_CLASS, lastof(this->landinfo_data[line_nr]));
    00254       line_nr++;
    00255     }
    00256
    00257     /* Airport name */
    00258     if (td.airport_name != STR_NULL) {
    00259       SetDParam(0, td.airport_name);
    00260       GetString(this->landinfo_data[line_nr], STR_LAND_AREA_INFORMATION_AIRPORT_NAME, lastof(this->landinfo_data[line_nr]));
    00261       line_nr++;
    00262     }
    00263
    00264     /* Airport tile name */
    00265     if (td.airport_tile_name != STR_NULL) {
    00266       SetDParam(0, td.airport_tile_name);
    00267       GetString(this->landinfo_data[line_nr], STR_LAND_AREA_INFORMATION_AIRPORTTILE_NAME, lastof(this->landinfo_data[line_nr]));
    00268       line_nr++;
    00269     }
    00270
    00271     /* Rail speed limit */
    00272     if (td.rail_speed != 0) {
    00273       SetDParam(0, td.rail_speed);
    00274       GetString(this->landinfo_data[line_nr], STR_LANG_AREA_INFORMATION_RAIL_SPEED_LIMIT, lastof(this->landinfo_data[line_nr]));
    00275       line_nr++;
    00276     }
    00277
    00278     /* Road speed limit */
    00279     if (td.road_speed != 0) {
    00280       SetDParam(0, td.road_speed);
    00281       GetString(this->landinfo_data[line_nr], STR_LANG_AREA_INFORMATION_ROAD_SPEED_LIMIT, lastof(this->landinfo_data[line_nr]));
    00282       line_nr++;
    00283     }
    00284
    00285     /* NewGRF name */
    00286     if (td.grf != NULL) {
    00287       SetDParamStr(0, td.grf);
    00288       GetString(this->landinfo_data[line_nr], STR_LAND_AREA_INFORMATION_NEWGRF_NAME, lastof(this->landinfo_data[line_nr]));
    00289       line_nr++;
    00290     }
    00291
    00292     assert(line_nr < LAND_INFO_CENTERED_LINES);
    00293
    00294     /* Mark last line empty */
    00295     this->landinfo_data[line_nr][0] = '\0';
    00296
    00297     /* Cargo acceptance is displayed in a extra multiline */
    00298     char *strp = GetString(this->landinfo_data[LAND_INFO_MULTICENTER_LINE], STR_LAND_AREA_INFORMATION_CARGO_ACCEPTED, lastof(this->landinfo_data[LAND_INFO_MULTICENTER_LINE]));
    00299     bool found = false;
    00300
    00301     for (CargoID i = 0; i < NUM_CARGO; ++i) {
    00302       if (acceptance[i] > 0) {
    00303         /* Add a comma between each item. */
    00304         if (found) {
    00305           *strp++ = ',';
    00306           *strp++ = ' ';
    00307         }
    00308         found = true;
    00309
    00310         /* If the accepted value is less than 8, show it in 1/8:ths */
    00311         if (acceptance[i] < 8) {
    00312           SetDParam(0, acceptance[i]);
    00313           SetDParam(1, CargoSpec::Get(i)->name);
    00314           strp = GetString(strp, STR_LAND_AREA_INFORMATION_CARGO_EIGHTS, lastof(this->landinfo_data[LAND_INFO_MULTICENTER_LINE]));
    00315         } else {
    00316           strp = GetString(strp, CargoSpec::Get(i)->name, lastof(this->landinfo_data[LAND_INFO_MULTICENTER_LINE]));
    00317         }
    00318       }
    00319     }
    00320     if (!found) this->landinfo_data[LAND_INFO_MULTICENTER_LINE][0] = '\0';
    00321   }
    00322
    00323   virtual bool IsNewGRFInspectable() const
    00324   {
    00325     return ::IsNewGRFInspectable(GetGrfSpecFeature(this->tile), this->tile);
    00326   }
    00327
    00328   virtual void ShowNewGRFInspectWindow() const
    00329   {
    00330 		::ShowNewGRFInspectWindow(GetGrfSpecFeature(this->tile), this->tile);
    00331   }
    00332
    00338   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
    00339   {
    00340     if (!gui_scope) return;
    00341     switch (data) {
    00342       case 1:
    00343         /* ReInit, "debug" sprite might have changed */
    00344         this->ReInit();
    00345         break;
    00346     }
    00347   }
    00348 };
    00349
    00354 void ShowLandInfo(TileIndex tile)
    00355 {
    00356   DeleteWindowById(WC_LAND_INFO, 0);
    00357   new LandInfoWindow(tile);
    00358 }
    00359
    00360 static const NWidgetPart _nested_about_widgets[] = {
    00361   NWidget(NWID_HORIZONTAL),
    00362     NWidget(WWT_CLOSEBOX, COLOUR_GREY),
    00363     NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_ABOUT_OPENTTD, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    00364   EndContainer(),
    00365   NWidget(WWT_PANEL, COLOUR_GREY), SetPIP(4, 2, 4),
    00366     NWidget(WWT_LABEL, COLOUR_GREY), SetDataTip(STR_ABOUT_ORIGINAL_COPYRIGHT, STR_NULL),
    00367     NWidget(WWT_LABEL, COLOUR_GREY), SetDataTip(STR_ABOUT_VERSION, STR_NULL),
    00368     NWidget(WWT_FRAME, COLOUR_GREY), SetPadding(0, 5, 1, 5),
    00369       NWidget(WWT_EMPTY, INVALID_COLOUR, WID_A_SCROLLING_TEXT),
    00370     EndContainer(),
    00371     NWidget(WWT_LABEL, COLOUR_GREY, WID_A_WEBSITE), SetDataTip(STR_BLACK_RAW_STRING, STR_NULL),
    00372     NWidget(WWT_LABEL, COLOUR_GREY), SetDataTip(STR_ABOUT_COPYRIGHT_OPENTTD, STR_NULL),
    00373   EndContainer(),
    00374 };
    00375
    00376 static WindowDesc _about_desc(
    00377   WDP_CENTER, NULL, 0, 0,
    00378   WC_GAME_OPTIONS, WC_NONE,
    00379   0,
    00380   _nested_about_widgets, lengthof(_nested_about_widgets)
    00381 );
    00382
    00383 static const char * const _credits[] = {
    00384   "Original design by Chris Sawyer",
    00385   "Original graphics by Simon Foster",
    00386   "",
    00387   "The OpenTTD team (in alphabetical order):",
    00388   "  Albert Hofkamp (Alberth) - GUI expert",
    00389   "  Jean-Fran\xC3\xA7ois Claeys (Belugas) - GUI, newindustries and more",
    00390   "  Matthijs Kooijman (blathijs) - Pathfinder-guru, pool rework",
    00391   "  Christoph Elsenhans (frosch) - General coding",
    00392   "  Ulf Hermann (fonsinchen) - Cargo Distribution",
    00393   "  Lo\xC3\xAF""c Guilloux (glx) - Windows Expert",
    00394   "  Michael Lutz (michi_cc) - Path based signals",
    00395   "  Owen Rudge (orudge) - Forum host, OS/2 port",
    00396   "  Peter Nelson (peter1138) - Spiritual descendant from NewGRF gods",
    00397   "  Ingo von Borstel (planetmaker) - Support",
    00398   "  Remko Bijker (Rubidium) - Lead coder and way more",
    00399   "  Zden\xC4\x9Bk Sojka (SmatZ) - Bug finder and fixer",
    00400   "  Jos\xC3\xA9 Soler (Terkhen) - General coding",
    00401   "  Thijs Marinussen (Yexo) - AI Framework",
    00402   "  Leif Linse (Zuu) - AI/Game Script",
    00403   "",
    00404   "Inactive Developers:",
    00405   "  Bjarni Corfitzen (Bjarni) - MacOSX port, coder and vehicles",
    00406   "  Victor Fischer (Celestar) - Programming everywhere you need him to",
    00407   "  Tam\xC3\xA1s Farag\xC3\xB3 (Darkvater) - Ex-Lead coder",
    00408   "  Jaroslav Mazanec (KUDr) - YAPG (Yet Another Pathfinder God) ;)",
    00409   "  Jonathan Coome (Maedhros) - High priest of the NewGRF Temple",
    00410   "  Attila B\xC3\xA1n (MiHaMiX) - Developer WebTranslator 1 and 2",
    00411   "  Christoph Mallon (Tron) - Programmer, code correctness police",
    00412   "",
    00413   "Retired Developers:",
    00414   "  Ludvig Strigeus (ludde) - OpenTTD author, main coder (0.1 - 0.3.3)",
    00415   "  Serge Paquet (vurlix) - Assistant project manager, coder (0.1 - 0.3.3)",
    00416   "  Dominik Scherer (dominik81) - Lead programmer, GUI expert (0.3.0 - 0.3.6)",
    00417   "  Benedikt Br\xC3\xBCggemeier (skidd13) - Bug fixer and code reworker",
    00418   "  Patric Stout (TrueBrain) - NoProgrammer (0.3 - 1.2), sys op (active)",
    00419   "",
    00420   "Special thanks go out to:",
    00421   "  Josef Drexler - For his great work on TTDPatch",
    00422   "  Marcin Grzegorczyk - Track foundations and for describing TTD internals",
    00423   "  Petr Baudi\xC5\xA1 (pasky) - Many patches, newGRF support",
    00424   "  Simon Sasburg (HackyKid) - Many bugfixes he has blessed us with",
    00425   "  Stefan Mei\xC3\x9Fner (sign_de) - For his work on the console",
    00426   "  Mike Ragsdale - OpenTTD installer",
    00427   "  Cian Duffy (MYOB) - BeOS port / manual writing",
    00428   "  Christian Rosentreter (tokai) - MorphOS / AmigaOS port",
    00429   "  Richard Kempton (richK) - additional airports, initial TGP implementation",
    00430   "  Fleashosio - titlegame",
    00431   "",
    00432   "  Alberto Demichelis - Squirrel scripting language \xC2\xA9 2003-2008",
    00433   "  L. Peter Deutsch - MD5 implementation \xC2\xA9 1999, 2000, 2002",
    00434   "  Michael Blunck - Pre-signals and semaphores \xC2\xA9 2003",
    00435   "  George - Canal/Lock graphics \xC2\xA9 2003-2004",
    00436   "  Andrew Parkhouse (andythenorth) - River graphics",
    00437   "  David Dallaston (Pikka) - Tram tracks",
    00438   "  All Translators - Who made OpenTTD a truly international game",
    00439   "  Bug Reporters - Without whom OpenTTD would still be full of bugs!",
    00440   "",
    00441   "",
    00442   "And last but not least:",
    00443   "  Chris Sawyer - For an amazing game!"
    00444 };
    00445
    00446 struct AboutWindow : public Window {
    00447   int text_position;
    00448   byte counter;
    00449   int line_height;
    00450   static const int num_visible_lines = 19;
    00451
    00452   AboutWindow() : Window(&_about_desc)
    00453   {
    00454     this->InitNested(WN_GAME_OPTIONS_ABOUT);
    00455
    00456     this->counter = 5;
    00457     this->text_position = this->GetWidget<NWidgetBase>(WID_A_SCROLLING_TEXT)->pos_y + this->GetWidget<NWidgetBase>(WID_A_SCROLLING_TEXT)->current_y;
    00458   }
    00459
    00460   virtual void SetStringParameters(int widget) const
    00461   {
    00462     if (widget == WID_A_WEBSITE) SetDParamStr(0, "Website: http://www.openttd.org");
    00463   }
    00464
    00465   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
    00466   {
    00467     if (widget != WID_A_SCROLLING_TEXT) return;
    00468
    00469     this->line_height = FONT_HEIGHT_NORMAL;
    00470
    00471     Dimension d;
    00472     d.height = this->line_height * num_visible_lines;
    00473
    00474     d.width = 0;
    00475     for (uint i = 0; i < lengthof(_credits); i++) {
    00476       d.width = max(d.width, GetStringBoundingBox(_credits[i]).width);
    00477     }
    00478     *size = maxdim(*size, d);
    00479   }
    00480
    00481   virtual void DrawWidget(const Rect &r, int widget) const
    00482   {
    00483     if (widget != WID_A_SCROLLING_TEXT) return;
    00484
    00485     int y = this->text_position;
    00486
    00487     /* Show all scrolling _credits */
    00488     for (uint i = 0; i < lengthof(_credits); i++) {
    00489       if (y >= r.top + 7 && y < r.bottom - this->line_height) {
    00490         DrawString(r.left, r.right, y, _credits[i], TC_BLACK, SA_LEFT | SA_FORCE);
    00491       }
    00492       y += this->line_height;
    00493     }
    00494   }
    00495
    00496   virtual void OnTick()
    00497   {
    00498     if (--this->counter == 0) {
    00499       this->counter = 5;
    00500       this->text_position--;
    00501       /* If the last text has scrolled start a new from the start */
    00502       if (this->text_position < (int)(this->GetWidget<NWidgetBase>(WID_A_SCROLLING_TEXT)->pos_y - lengthof(_credits) * this->line_height)) {
    00503         this->text_position = this->GetWidget<NWidgetBase>(WID_A_SCROLLING_TEXT)->pos_y + this->GetWidget<NWidgetBase>(WID_A_SCROLLING_TEXT)->current_y;
    00504       }
    00505       this->SetDirty();
    00506     }
    00507   }
    00508 };
    00509
    00510 void ShowAboutWindow()
    00511 {
    00512   DeleteWindowByClass(WC_GAME_OPTIONS);
    00513   new AboutWindow();
    00514 }
    00515
    00522 void ShowEstimatedCostOrIncome(Money cost, int x, int y)
    00523 {
    00524   StringID msg = STR_MESSAGE_ESTIMATED_COST;
    00525
    00526   if (cost < 0) {
    00527     cost = -cost;
    00528     msg = STR_MESSAGE_ESTIMATED_INCOME;
    00529   }
    00530   SetDParam(0, cost);
    00531   ShowErrorMessage(msg, INVALID_STRING_ID, WL_INFO, x, y);
    00532 }
    00533
    00541 void ShowCostOrIncomeAnimation(int x, int y, int z, Money cost)
    00542 {
    00543   Point pt = RemapCoords(x, y, z);
    00544   StringID msg = STR_INCOME_FLOAT_COST;
    00545
    00546   if (cost < 0) {
    00547     cost = -cost;
    00548     msg = STR_INCOME_FLOAT_INCOME;
    00549   }
    00550   SetDParam(0, cost);
    00551   AddTextEffect(msg, pt.x, pt.y, DAY_TICKS, TE_RISING);
    00552 }
    00553
    00562 void ShowFeederIncomeAnimation(int x, int y, int z, Money transfer, Money income)
    00563 {
    00564   Point pt = RemapCoords(x, y, z);
    00565
    00566   SetDParam(0, transfer);
    00567   if (income == 0) {
    00568     AddTextEffect(STR_FEEDER, pt.x, pt.y, DAY_TICKS, TE_RISING);
    00569   } else {
    00570     StringID msg = STR_FEEDER_COST;
    00571     if (income < 0) {
    00572       income = -income;
    00573       msg = STR_FEEDER_INCOME;
    00574     }
    00575     SetDParam(1, income);
    00576     AddTextEffect(msg, pt.x, pt.y, DAY_TICKS, TE_RISING);
    00577   }
    00578 }
    00579
    00589 TextEffectID ShowFillingPercent(int x, int y, int z, uint8 percent, StringID string)
    00590 {
    00591   Point pt = RemapCoords(x, y, z);
    00592
    00593   assert(string != STR_NULL);
    00594
    00595   SetDParam(0, percent);
    00596   return AddTextEffect(string, pt.x, pt.y, 0, TE_STATIC);
    00597 }
    00598
    00604 void UpdateFillingPercent(TextEffectID te_id, uint8 percent, StringID string)
    00605 {
    00606   assert(string != STR_NULL);
    00607
    00608   SetDParam(0, percent);
    00609   UpdateTextEffect(te_id, string);
    00610 }
    00611
    00616 void HideFillingPercent(TextEffectID *te_id)
    00617 {
    00618   if (*te_id == INVALID_TE_ID) return;
    00619
    00620   RemoveTextEffect(*te_id);
    00621   *te_id = INVALID_TE_ID;
    00622 }
    00623
    00624 static const NWidgetPart _nested_tooltips_widgets[] = {
    00625   NWidget(WWT_PANEL, COLOUR_GREY, WID_TT_BACKGROUND), SetMinimalSize(200, 32), EndContainer(),
    00626 };
    00627
    00628 static WindowDesc _tool_tips_desc(
    00629   WDP_MANUAL, NULL, 0, 0, // Coordinates and sizes are not used,
    00630   WC_TOOLTIPS, WC_NONE,
    00631   0,
    00632   _nested_tooltips_widgets, lengthof(_nested_tooltips_widgets)
    00633 );
    00634
    00636 struct TooltipsWindow : public Window
    00637 {
    00638   StringID string_id;
    00639   byte paramcount;
    00640   uint64 params[5];
    00641   TooltipCloseCondition close_cond;
    00642
    00643   TooltipsWindow(Window *parent, StringID str, uint paramcount, const uint64 params[], TooltipCloseCondition close_tooltip) : Window(&_tool_tips_desc)
    00644   {
    00645     this->parent = parent;
    00646     this->string_id = str;
    00647     assert_compile(sizeof(this->params[0]) == sizeof(params[0]));
    00648     assert(paramcount <= lengthof(this->params));
    00649     memcpy(this->params, params, sizeof(this->params[0]) * paramcount);
    00650     this->paramcount = paramcount;
    00651     this->close_cond = close_tooltip;
    00652
    00653     this->InitNested();
    00654
    00655     CLRBITS(this->flags, WF_WHITE_BORDER);
    00656   }
    00657
    00658   virtual Point OnInitialPosition(int16 sm_width, int16 sm_height, int window_number)
    00659   {
    00660     /* Find the free screen space between the main toolbar at the top, and the statusbar at the bottom.
    00661      * Add a fixed distance 2 so the tooltip floats free from both bars.
    00662      */
    00663     int scr_top = GetMainViewTop() + 2;
    00664     int scr_bot = GetMainViewBottom() - 2;
    00665
    00666     Point pt;
    00667
    00668     /* Correctly position the tooltip position, watch out for window and cursor size
    00669      * Clamp value to below main toolbar and above statusbar. If tooltip would
    00670      * go below window, flip it so it is shown above the cursor */
    00671     pt.y = Clamp(_cursor.pos.y + _cursor.size.y + _cursor.offs.y + 5, scr_top, scr_bot);
    00672     if (pt.y + sm_height > scr_bot) pt.y = min(_cursor.pos.y + _cursor.offs.y - 5, scr_bot) - sm_height;
    00673     pt.x = sm_width >= _screen.width ? 0 : Clamp(_cursor.pos.x - (sm_width >> 1), 0, _screen.width - sm_width);
    00674
    00675     return pt;
    00676   }
    00677
    00678   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
    00679   {
    00680     /* There is only one widget. */
    00681     for (uint i = 0; i != this->paramcount; i++) SetDParam(i, this->params[i]);
    00682
    00683     size->width  = min(GetStringBoundingBox(this->string_id).width, 194);
    00684     size->height = GetStringHeight(this->string_id, size->width);
    00685
    00686     /* Increase slightly to have some space around the box. */
    00687     size->width  += 2 + WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
    00688     size->height += 2 + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
    00689   }
    00690
    00691   virtual void DrawWidget(const Rect &r, int widget) const
    00692   {
    00693     /* There is only one widget. */
    00694     GfxFillRect(r.left, r.top, r.right, r.bottom, PC_BLACK);
    00695     GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, PC_LIGHT_YELLOW);
    00696
    00697     for (uint arg = 0; arg < this->paramcount; arg++) {
    00698       SetDParam(arg, this->params[arg]);
    00699     }
    00700     DrawStringMultiLine(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + WD_FRAMERECT_TOP, r.bottom - WD_FRAMERECT_BOTTOM, this->string_id, TC_FROMSTRING, SA_CENTER);
    00701   }
    00702
    00703   virtual void OnMouseLoop()
    00704   {
    00705     /* Always close tooltips when the cursor is not in our window. */
    00706     if (!_cursor.in_window) {
    00707       delete this;
    00708       return;
    00709     }
    00710
    00711     /* We can show tooltips while dragging tools. These are shown as long as
    00712      * we are dragging the tool. Normal tooltips work with hover or rmb. */
    00713     switch (this->close_cond) {
    00714       case TCC_RIGHT_CLICK: if (!_right_button_down) delete this; break;
    00715       case TCC_LEFT_CLICK: if (!_left_button_down) delete this; break;
    00716       case TCC_HOVER: if (!_mouse_hovering) delete this; break;
    00717     }
    00718   }
    00719 };
    00720
    00729 void GuiShowTooltips(Window *parent, StringID str, uint paramcount, const uint64 params[], TooltipCloseCondition close_tooltip)
    00730 {
    00731   DeleteWindowById(WC_TOOLTIPS, 0);
    00732
    00733   if (str == STR_NULL) return;
    00734
    00735   new TooltipsWindow(parent, str, paramcount, params, close_tooltip);
    00736 }
    00737
    00738 void QueryString::HandleEditBox(Window *w, int wid)
    00739 {
    00740   if (w->IsWidgetGloballyFocused(wid) && this->text.HandleCaret()) {
    00741     w->SetWidgetDirty(wid);
    00742
    00743     /* For the OSK also invalidate the parent window */
    00744     if (w->window_class == WC_OSK) w->InvalidateData();
    00745   }
    00746 }
    00747
    00748 void QueryString::DrawEditBox(const Window *w, int wid) const
    00749 {
    00750   const NWidgetLeaf *wi = w->GetWidget<NWidgetLeaf>(wid);
    00751
    00752   assert((wi->type & WWT_MASK) == WWT_EDITBOX);
    00753
    00754   bool rtl = _current_text_dir == TD_RTL;
    00755   Dimension sprite_size = GetSpriteSize(rtl ? SPR_IMG_DELETE_RIGHT : SPR_IMG_DELETE_LEFT);
    00756   int clearbtn_width = sprite_size.width + WD_IMGBTN_LEFT + WD_IMGBTN_RIGHT;
    00757
    00758   int clearbtn_left  = wi->pos_x + (rtl ? 0 : wi->current_x - clearbtn_width);
    00759   int clearbtn_right = wi->pos_x + (rtl ? clearbtn_width : wi->current_x) - 1;
    00760   int left   = wi->pos_x + (rtl ? clearbtn_width : 0);
    00761   int right  = wi->pos_x + (rtl ? wi->current_x : wi->current_x - clearbtn_width) - 1;
    00762
    00763   int top    = wi->pos_y;
    00764   int bottom = wi->pos_y + wi->current_y - 1;
    00765
    00766   DrawFrameRect(clearbtn_left, top, clearbtn_right, bottom, wi->colour, wi->IsLowered() ? FR_LOWERED : FR_NONE);
    00767   DrawSprite(rtl ? SPR_IMG_DELETE_RIGHT : SPR_IMG_DELETE_LEFT, PAL_NONE, clearbtn_left + WD_IMGBTN_LEFT + (wi->IsLowered() ? 1 : 0), (top + bottom - sprite_size.height) / 2 + (wi->IsLowered() ? 1 : 0));
    00768   if (this->text.bytes == 1) GfxFillRect(clearbtn_left + 1, top + 1, clearbtn_right - 1, bottom - 1, _colour_gradient[wi->colour & 0xF][2], FILLRECT_CHECKER);
    00769
    00770   DrawFrameRect(left, top, right, bottom, wi->colour, FR_LOWERED | FR_DARKENED);
    00771   GfxFillRect(left + 1, top + 1, right - 1, bottom - 1, PC_BLACK);
    00772
    00773   /* Limit the drawing of the string inside the widget boundaries */
    00774   DrawPixelInfo dpi;
    00775   if (!FillDrawPixelInfo(&dpi, left + WD_FRAMERECT_LEFT, top + WD_FRAMERECT_TOP, right - left - WD_FRAMERECT_RIGHT, bottom - top - WD_FRAMERECT_BOTTOM)) return;
    00776
    00777   DrawPixelInfo *old_dpi = _cur_dpi;
    00778   _cur_dpi = &dpi;
    00779
    00780   /* We will take the current widget length as maximum width, with a small
    00781    * space reserved at the end for the caret to show */
    00782   const Textbuf *tb = &this->text;
    00783   int delta = min(0, (right - left) - tb->pixels - 10);
    00784
    00785   if (tb->caretxoffs + delta < 0) delta = -tb->caretxoffs;
    00786
    00787   /* If we have a marked area, draw a background highlight. */
    00788   if (tb->marklength != 0) GfxFillRect(delta + tb->markxoffs, 0, delta + tb->markxoffs + tb->marklength - 1, bottom - top, PC_GREY);
    00789
    00790   DrawString(delta, tb->pixels, 0, tb->buf, TC_YELLOW);
    00791   bool focussed = w->IsWidgetGloballyFocused(wid) || IsOSKOpenedFor(w, wid);
    00792   if (focussed && tb->caret) {
    00793     int caret_width = GetStringBoundingBox("_").width;
    00794     DrawString(tb->caretxoffs + delta, tb->caretxoffs + delta + caret_width, 0, "_", TC_WHITE);
    00795   }
    00796
    00797   _cur_dpi = old_dpi;
    00798 }
    00799
    00806 Point QueryString::GetCaretPosition(const Window *w, int wid) const
    00807 {
    00808   const NWidgetLeaf *wi = w->GetWidget<NWidgetLeaf>(wid);
    00809
    00810   assert((wi->type & WWT_MASK) == WWT_EDITBOX);
    00811
    00812   bool rtl = _current_text_dir == TD_RTL;
    00813   Dimension sprite_size = GetSpriteSize(rtl ? SPR_IMG_DELETE_RIGHT : SPR_IMG_DELETE_LEFT);
    00814   int clearbtn_width = sprite_size.width + WD_IMGBTN_LEFT + WD_IMGBTN_RIGHT;
    00815
    00816   int left   = wi->pos_x + (rtl ? clearbtn_width : 0);
    00817   int right  = wi->pos_x + (rtl ? wi->current_x : wi->current_x - clearbtn_width) - 1;
    00818
    00819   /* Clamp caret position to be inside out current width. */
    00820   const Textbuf *tb = &this->text;
    00821   int delta = min(0, (right - left) - tb->pixels - 10);
    00822   if (tb->caretxoffs + delta < 0) delta = -tb->caretxoffs;
    00823
    00824   Point pt = {left + WD_FRAMERECT_LEFT + tb->caretxoffs + delta, wi->pos_y + WD_FRAMERECT_TOP};
    00825   return pt;
    00826 }
    00827
    00836 Rect QueryString::GetBoundingRect(const Window *w, int wid, const char *from, const char *to) const
    00837 {
    00838   const NWidgetLeaf *wi = w->GetWidget<NWidgetLeaf>(wid);
    00839
    00840   assert((wi->type & WWT_MASK) == WWT_EDITBOX);
    00841
    00842   bool rtl = _current_text_dir == TD_RTL;
    00843   Dimension sprite_size = GetSpriteSize(rtl ? SPR_IMG_DELETE_RIGHT : SPR_IMG_DELETE_LEFT);
    00844   int clearbtn_width = sprite_size.width + WD_IMGBTN_LEFT + WD_IMGBTN_RIGHT;
    00845
    00846   int left   = wi->pos_x + (rtl ? clearbtn_width : 0);
    00847   int right  = wi->pos_x + (rtl ? wi->current_x : wi->current_x - clearbtn_width) - 1;
    00848
    00849   int top    = wi->pos_y + WD_FRAMERECT_TOP;
    00850   int bottom = wi->pos_y + wi->current_y - 1 - WD_FRAMERECT_BOTTOM;
    00851
    00852   /* Clamp caret position to be inside our current width. */
    00853   const Textbuf *tb = &this->text;
    00854   int delta = min(0, (right - left) - tb->pixels - 10);
    00855   if (tb->caretxoffs + delta < 0) delta = -tb->caretxoffs;
    00856
    00857   /* Get location of first and last character. */
    00858   Point p1 = GetCharPosInString(tb->buf, from, FS_NORMAL);
    00859   Point p2 = from != to ? GetCharPosInString(tb->buf, to, FS_NORMAL) : p1;
    00860
    00861   Rect r = { Clamp(left + p1.x + delta + WD_FRAMERECT_LEFT, left, right), top, Clamp(left + p2.x + delta + WD_FRAMERECT_LEFT, left, right - WD_FRAMERECT_RIGHT), bottom };
    00862
    00863   return r;
    00864 }
    00865
    00873 const char *QueryString::GetCharAtPosition(const Window *w, int wid, const Point &pt) const
    00874 {
    00875   const NWidgetLeaf *wi = w->GetWidget<NWidgetLeaf>(wid);
    00876
    00877   assert((wi->type & WWT_MASK) == WWT_EDITBOX);
    00878
    00879   bool rtl = _current_text_dir == TD_RTL;
    00880   Dimension sprite_size = GetSpriteSize(rtl ? SPR_IMG_DELETE_RIGHT : SPR_IMG_DELETE_LEFT);
    00881   int clearbtn_width = sprite_size.width + WD_IMGBTN_LEFT + WD_IMGBTN_RIGHT;
    00882
    00883   int left   = wi->pos_x + (rtl ? clearbtn_width : 0);
    00884   int right  = wi->pos_x + (rtl ? wi->current_x : wi->current_x - clearbtn_width) - 1;
    00885
    00886   int top    = wi->pos_y + WD_FRAMERECT_TOP;
    00887   int bottom = wi->pos_y + wi->current_y - 1 - WD_FRAMERECT_BOTTOM;
    00888
    00889   if (!IsInsideMM(pt.y, top, bottom)) return NULL;
    00890
    00891   /* Clamp caret position to be inside our current width. */
    00892   const Textbuf *tb = &this->text;
    00893   int delta = min(0, (right - left) - tb->pixels - 10);
    00894   if (tb->caretxoffs + delta < 0) delta = -tb->caretxoffs;
    00895
    00896   return ::GetCharAtPosition(tb->buf, pt.x - delta - left);
    00897 }
    00898
    00899 void QueryString::ClickEditBox(Window *w, Point pt, int wid, int click_count, bool focus_changed)
    00900 {
    00901   const NWidgetLeaf *wi = w->GetWidget<NWidgetLeaf>(wid);
    00902
    00903   assert((wi->type & WWT_MASK) == WWT_EDITBOX);
    00904
    00905   bool rtl = _current_text_dir == TD_RTL;
    00906   int clearbtn_width = GetSpriteSize(rtl ? SPR_IMG_DELETE_RIGHT : SPR_IMG_DELETE_LEFT).width;
    00907
    00908   int clearbtn_left  = wi->pos_x + (rtl ? 0 : wi->current_x - clearbtn_width);
    00909
    00910   if (IsInsideBS(pt.x, clearbtn_left, clearbtn_width)) {
    00911     if (this->text.bytes > 1) {
    00912       this->text.DeleteAll();
    00913       w->HandleButtonClick(wid);
    00914       w->OnEditboxChanged(wid);
    00915     }
    00916     return;
    00917   }
    00918
    00919   if (w->window_class != WC_OSK && _settings_client.gui.osk_activation != OSKA_DISABLED &&
    00920     (!focus_changed || _settings_client.gui.osk_activation == OSKA_IMMEDIATELY) &&
    00921     (click_count == 2 || _settings_client.gui.osk_activation != OSKA_DOUBLE_CLICK)) {
    00922     /* Open the OSK window */
    00923     ShowOnScreenKeyboard(w, wid);
    00924   }
    00925 }
    00926
    00928 struct QueryStringWindow : public Window
    00929 {
    00930   QueryString editbox;
    00931   QueryStringFlags flags;
    00932
    00933   QueryStringWindow(StringID str, StringID caption, uint max_bytes, uint max_chars, WindowDesc *desc, Window *parent, CharSetFilter afilter, QueryStringFlags flags) :
    00934       Window(desc), editbox(max_bytes, max_chars)
    00935   {
    00936     char *last_of = &this->editbox.text.buf[this->editbox.text.max_bytes - 1];
    00937     GetString(this->editbox.text.buf, str, last_of);
    00938     str_validate(this->editbox.text.buf, last_of, SVS_NONE);
    00939
    00940     /* Make sure the name isn't too long for the text buffer in the number of
    00941      * characters (not bytes). max_chars also counts the '\0' characters. */
    00942     while (Utf8StringLength(this->editbox.text.buf) + 1 > this->editbox.text.max_chars) {
    00943       *Utf8PrevChar(this->editbox.text.buf + strlen(this->editbox.text.buf)) = '\0';
    00944     }
    00945
    00946     this->editbox.text.UpdateSize();
    00947
    00948     if ((flags & QSF_ACCEPT_UNCHANGED) == 0) this->editbox.orig = strdup(this->editbox.text.buf);
    00949
    00950     this->querystrings[WID_QS_TEXT] = &this->editbox;
    00951     this->editbox.caption = caption;
    00952     this->editbox.cancel_button = WID_QS_CANCEL;
    00953     this->editbox.ok_button = WID_QS_OK;
    00954     this->editbox.text.afilter = afilter;
    00955     this->flags = flags;
    00956
    00957     this->InitNested(WN_QUERY_STRING);
    00958
    00959     this->parent = parent;
    00960
    00961     this->SetFocusedWidget(WID_QS_TEXT);
    00962   }
    00963
    00964   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
    00965   {
    00966     if (widget == WID_QS_DEFAULT && (this->flags & QSF_ENABLE_DEFAULT) == 0) {
    00967       /* We don't want this widget to show! */
    00968       fill->width = 0;
    00969       resize->width = 0;
    00970       size->width = 0;
    00971     }
    00972   }
    00973
    00974   virtual void SetStringParameters(int widget) const
    00975   {
    00976     if (widget == WID_QS_CAPTION) SetDParam(0, this->editbox.caption);
    00977   }
    00978
    00979   void OnOk()
    00980   {
    00981     if (this->editbox.orig == NULL || strcmp(this->editbox.text.buf, this->editbox.orig) != 0) {
    00982       /* If the parent is NULL, the editbox is handled by general function
    00983        * HandleOnEditText */
    00984       if (this->parent != NULL) {
    00985         this->parent->OnQueryTextFinished(this->editbox.text.buf);
    00986       } else {
    00987         HandleOnEditText(this->editbox.text.buf);
    00988       }
    00989       this->editbox.handled = true;
    00990     }
    00991   }
    00992
    00993   virtual void OnClick(Point pt, int widget, int click_count)
    00994   {
    00995     switch (widget) {
    00996       case WID_QS_DEFAULT:
    00997         this->editbox.text.DeleteAll();
    00998         /* FALL THROUGH */
    00999       case WID_QS_OK:
    01000         this->OnOk();
    01001         /* FALL THROUGH */
    01002       case WID_QS_CANCEL:
    01003         delete this;
    01004         break;
    01005     }
    01006   }
    01007
    01008   ~QueryStringWindow()
    01009   {
    01010     if (!this->editbox.handled && this->parent != NULL) {
    01011       Window *parent = this->parent;
    01012       this->parent = NULL; // so parent doesn't try to delete us again
    01013       parent->OnQueryTextFinished(NULL);
    01014     }
    01015   }
    01016 };
    01017
    01018 static const NWidgetPart _nested_query_string_widgets[] = {
    01019   NWidget(NWID_HORIZONTAL),
    01020     NWidget(WWT_CLOSEBOX, COLOUR_GREY),
    01021     NWidget(WWT_CAPTION, COLOUR_GREY, WID_QS_CAPTION), SetDataTip(STR_WHITE_STRING, STR_NULL),
    01022   EndContainer(),
    01023   NWidget(WWT_PANEL, COLOUR_GREY),
    01024     NWidget(WWT_EDITBOX, COLOUR_GREY, WID_QS_TEXT), SetMinimalSize(256, 12), SetFill(1, 1), SetPadding(2, 2, 2, 2),
    01025   EndContainer(),
    01026   NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
    01027     NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_QS_DEFAULT), SetMinimalSize(87, 12), SetFill(1, 1), SetDataTip(STR_BUTTON_DEFAULT, STR_NULL),
    01028     NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_QS_CANCEL), SetMinimalSize(86, 12), SetFill(1, 1), SetDataTip(STR_BUTTON_CANCEL, STR_NULL),
    01029     NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_QS_OK), SetMinimalSize(87, 12), SetFill(1, 1), SetDataTip(STR_BUTTON_OK, STR_NULL),
    01030   EndContainer(),
    01031 };
    01032
    01033 static WindowDesc _query_string_desc(
    01034   WDP_CENTER, "query_string", 0, 0,
    01035   WC_QUERY_STRING, WC_NONE,
    01036   0,
    01037   _nested_query_string_widgets, lengthof(_nested_query_string_widgets)
    01038 );
    01039
    01050 void ShowQueryString(StringID str, StringID caption, uint maxsize, Window *parent, CharSetFilter afilter, QueryStringFlags flags)
    01051 {
    01052   DeleteWindowByClass(WC_QUERY_STRING);
    01053   new QueryStringWindow(str, caption, ((flags & QSF_LEN_IN_CHARS) ? MAX_CHAR_LENGTH : 1) * maxsize, maxsize, &_query_string_desc, parent, afilter, flags);
    01054 }
    01055
    01059 struct QueryWindow : public Window {
    01060   QueryCallbackProc *proc;
    01061   uint64 params[10];
    01062   StringID message;
    01063   StringID caption;
    01064
    01065   QueryWindow(WindowDesc *desc, StringID caption, StringID message, Window *parent, QueryCallbackProc *callback) : Window(desc)
    01066   {
    01067     /* Create a backup of the variadic arguments to strings because it will be
    01068      * overridden pretty often. We will copy these back for drawing */
    01069     CopyOutDParam(this->params, 0, lengthof(this->params));
    01070     this->caption = caption;
    01071     this->message = message;
    01072     this->proc    = callback;
    01073
    01074     this->InitNested(WN_CONFIRM_POPUP_QUERY);
    01075
    01076     this->parent = parent;
    01077     this->left = parent->left + (parent->width / 2) - (this->width / 2);
    01078     this->top = parent->top + (parent->height / 2) - (this->height / 2);
    01079   }
    01080
    01081   ~QueryWindow()
    01082   {
    01083     if (this->proc != NULL) this->proc(this->parent, false);
    01084   }
    01085
    01086   virtual void SetStringParameters(int widget) const
    01087   {
    01088     switch (widget) {
    01089       case WID_Q_CAPTION:
    01090         CopyInDParam(1, this->params, lengthof(this->params));
    01091         SetDParam(0, this->caption);
    01092         break;
    01093
    01094       case WID_Q_TEXT:
    01095         CopyInDParam(0, this->params, lengthof(this->params));
    01096         break;
    01097     }
    01098   }
    01099
    01100   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
    01101   {
    01102     if (widget != WID_Q_TEXT) return;
    01103
    01104     Dimension d = GetStringMultiLineBoundingBox(this->message, *size);
    01105     d.width += WD_FRAMETEXT_LEFT + WD_FRAMETEXT_RIGHT;
    01106     d.height += WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
    01107     *size = d;
    01108   }
    01109
    01110   virtual void DrawWidget(const Rect &r, int widget) const
    01111   {
    01112     if (widget != WID_Q_TEXT) return;
    01113
    01114     DrawStringMultiLine(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, r.top + WD_FRAMERECT_TOP, r.bottom - WD_FRAMERECT_BOTTOM,
    01115         this->message, TC_FROMSTRING, SA_CENTER);
    01116   }
    01117
    01118   virtual void OnClick(Point pt, int widget, int click_count)
    01119   {
    01120     switch (widget) {
    01121       case WID_Q_YES: {
    01122         /* in the Generate New World window, clicking 'Yes' causes
    01123          * DeleteNonVitalWindows() to be called - we shouldn't be in a window then */
    01124         QueryCallbackProc *proc = this->proc;
    01125         Window *parent = this->parent;
    01126         /* Prevent the destructor calling the callback function */
    01127         this->proc = NULL;
    01128         delete this;
    01129         if (proc != NULL) {
    01130           proc(parent, true);
    01131           proc = NULL;
    01132         }
    01133         break;
    01134       }
    01135       case WID_Q_NO:
    01136         delete this;
    01137         break;
    01138     }
    01139   }
    01140
    01141   virtual EventState OnKeyPress(WChar key, uint16 keycode)
    01142   {
    01143     /* ESC closes the window, Enter confirms the action */
    01144     switch (keycode) {
    01145       case WKC_RETURN:
    01146       case WKC_NUM_ENTER:
    01147         if (this->proc != NULL) {
    01148           this->proc(this->parent, true);
    01149           this->proc = NULL;
    01150         }
    01151         /* FALL THROUGH */
    01152       case WKC_ESC:
    01153         delete this;
    01154         return ES_HANDLED;
    01155     }
    01156     return ES_NOT_HANDLED;
    01157   }
    01158 };
    01159
    01160 static const NWidgetPart _nested_query_widgets[] = {
    01161   NWidget(NWID_HORIZONTAL),
    01162     NWidget(WWT_CLOSEBOX, COLOUR_RED),
    01163     NWidget(WWT_CAPTION, COLOUR_RED, WID_Q_CAPTION), SetDataTip(STR_JUST_STRING, STR_NULL),
    01164   EndContainer(),
    01165   NWidget(WWT_PANEL, COLOUR_RED), SetPIP(8, 15, 8),
    01166     NWidget(WWT_TEXT, COLOUR_RED, WID_Q_TEXT), SetMinimalSize(200, 12),
    01167     NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(20, 29, 20),
    01168       NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_Q_NO), SetMinimalSize(71, 12), SetDataTip(STR_QUIT_NO, STR_NULL),
    01169       NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_Q_YES), SetMinimalSize(71, 12), SetDataTip(STR_QUIT_YES, STR_NULL),
    01170     EndContainer(),
    01171   EndContainer(),
    01172 };
    01173
    01174 static WindowDesc _query_desc(
    01175   WDP_CENTER, NULL, 0, 0,
    01176   WC_CONFIRM_POPUP_QUERY, WC_NONE,
    01177   WDF_MODAL,
    01178   _nested_query_widgets, lengthof(_nested_query_widgets)
    01179 );
    01180
    01190 void ShowQuery(StringID caption, StringID message, Window *parent, QueryCallbackProc *callback)
    01191 {
    01192   if (parent == NULL) parent = FindWindowById(WC_MAIN_WINDOW, 0);
    01193
    01194   const Window *w;
    01195   FOR_ALL_WINDOWS_FROM_BACK(w) {
    01196     if (w->window_class != WC_CONFIRM_POPUP_QUERY) continue;
    01197
    01198     const QueryWindow *qw = (const QueryWindow *)w;
    01199     if (qw->parent != parent || qw->proc != callback) continue;
    01200
    01201     delete qw;
    01202     break;
    01203   }
    01204
    01205   new QueryWindow(&_query_desc, caption, message, parent, callback);
    01206 }