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