# misc_gui.cpp

Go to the documentation of this file.

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