(svn r27953) -Cleanup: Adjust other languages for r27952
[openttd.git] / src / misc_gui.cpp
blob03500c2a26b518684288b1cd9a93fb0020c572ff
1 /* $Id$ */
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
8 */
10 /** @file misc_gui.cpp GUIs for a number of misc windows. */
12 #include "stdafx.h"
13 #include "debug.h"
14 #include "landscape.h"
15 #include "error.h"
16 #include "gui.h"
17 #include "command_func.h"
18 #include "company_func.h"
19 #include "town.h"
20 #include "string_func.h"
21 #include "company_base.h"
22 #include "texteff.hpp"
23 #include "strings_func.h"
24 #include "window_func.h"
25 #include "querystring_gui.h"
26 #include "core/geometry_func.hpp"
27 #include "newgrf_debug.h"
28 #include "zoom_func.h"
30 #include "widgets/misc_widget.h"
32 #include "table/strings.h"
34 #include "safeguards.h"
36 /** Method to open the OSK. */
37 enum OskActivation {
38 OSKA_DISABLED, ///< The OSK shall not be activated at all.
39 OSKA_DOUBLE_CLICK, ///< Double click on the edit box opens OSK.
40 OSKA_SINGLE_CLICK, ///< Single click after focus click opens OSK.
41 OSKA_IMMEDIATELY, ///< Focusing click already opens OSK.
45 static const NWidgetPart _nested_land_info_widgets[] = {
46 NWidget(NWID_HORIZONTAL),
47 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
48 NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_LAND_AREA_INFORMATION_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
49 NWidget(WWT_DEBUGBOX, COLOUR_GREY),
50 EndContainer(),
51 NWidget(WWT_PANEL, COLOUR_GREY, WID_LI_BACKGROUND), EndContainer(),
54 static WindowDesc _land_info_desc(
55 WDP_AUTO, "land_info", 0, 0,
56 WC_LAND_INFO, WC_NONE,
58 _nested_land_info_widgets, lengthof(_nested_land_info_widgets)
61 class LandInfoWindow : public Window {
62 enum LandInfoLines {
63 LAND_INFO_CENTERED_LINES = 32, ///< Up to 32 centered lines (arbitrary limit)
64 LAND_INFO_MULTICENTER_LINE = LAND_INFO_CENTERED_LINES, ///< One multicenter line
65 LAND_INFO_LINE_END,
68 static const uint LAND_INFO_LINE_BUFF_SIZE = 512;
70 public:
71 char landinfo_data[LAND_INFO_LINE_END][LAND_INFO_LINE_BUFF_SIZE];
72 TileIndex tile;
74 virtual void DrawWidget(const Rect &r, int widget) const
76 if (widget != WID_LI_BACKGROUND) return;
78 uint y = r.top + WD_TEXTPANEL_TOP;
79 for (uint i = 0; i < LAND_INFO_CENTERED_LINES; i++) {
80 if (StrEmpty(this->landinfo_data[i])) break;
82 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);
83 y += FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL;
84 if (i == 0) y += 4;
87 if (!StrEmpty(this->landinfo_data[LAND_INFO_MULTICENTER_LINE])) {
88 SetDParamStr(0, this->landinfo_data[LAND_INFO_MULTICENTER_LINE]);
89 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);
93 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
95 if (widget != WID_LI_BACKGROUND) return;
97 size->height = WD_TEXTPANEL_TOP + WD_TEXTPANEL_BOTTOM;
98 for (uint i = 0; i < LAND_INFO_CENTERED_LINES; i++) {
99 if (StrEmpty(this->landinfo_data[i])) break;
101 uint width = GetStringBoundingBox(this->landinfo_data[i]).width + WD_FRAMETEXT_LEFT + WD_FRAMETEXT_RIGHT;
102 size->width = max(size->width, width);
104 size->height += FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL;
105 if (i == 0) size->height += 4;
108 if (!StrEmpty(this->landinfo_data[LAND_INFO_MULTICENTER_LINE])) {
109 uint width = GetStringBoundingBox(this->landinfo_data[LAND_INFO_MULTICENTER_LINE]).width + WD_FRAMETEXT_LEFT + WD_FRAMETEXT_RIGHT;
110 size->width = max(size->width, min(300u, width));
111 SetDParamStr(0, this->landinfo_data[LAND_INFO_MULTICENTER_LINE]);
112 size->height += GetStringHeight(STR_JUST_RAW_STRING, size->width - WD_FRAMETEXT_LEFT - WD_FRAMETEXT_RIGHT);
116 LandInfoWindow(TileIndex tile) : Window(&_land_info_desc), tile(tile)
118 this->InitNested();
120 #if defined(_DEBUG)
121 # define LANDINFOD_LEVEL 0
122 #else
123 # define LANDINFOD_LEVEL 1
124 #endif
125 DEBUG(misc, LANDINFOD_LEVEL, "TILE: %#x (%i,%i)", tile, TileX(tile), TileY(tile));
126 DEBUG(misc, LANDINFOD_LEVEL, "type = %#x", _m[tile].type);
127 DEBUG(misc, LANDINFOD_LEVEL, "height = %#x", _m[tile].height);
128 DEBUG(misc, LANDINFOD_LEVEL, "m1 = %#x", _m[tile].m1);
129 DEBUG(misc, LANDINFOD_LEVEL, "m2 = %#x", _m[tile].m2);
130 DEBUG(misc, LANDINFOD_LEVEL, "m3 = %#x", _m[tile].m3);
131 DEBUG(misc, LANDINFOD_LEVEL, "m4 = %#x", _m[tile].m4);
132 DEBUG(misc, LANDINFOD_LEVEL, "m5 = %#x", _m[tile].m5);
133 DEBUG(misc, LANDINFOD_LEVEL, "m6 = %#x", _me[tile].m6);
134 DEBUG(misc, LANDINFOD_LEVEL, "m7 = %#x", _me[tile].m7);
135 #undef LANDINFOD_LEVEL
138 virtual void OnInit()
140 Town *t = ClosestTownFromTile(tile, _settings_game.economy.dist_local_authority);
142 /* Because build_date is not set yet in every TileDesc, we make sure it is empty */
143 TileDesc td;
145 td.build_date = INVALID_DATE;
147 /* Most tiles have only one owner, but
148 * - drivethrough roadstops can be build on town owned roads (up to 2 owners) and
149 * - roads can have up to four owners (railroad, road, tram, 3rd-roadtype "highway").
151 td.owner_type[0] = STR_LAND_AREA_INFORMATION_OWNER; // At least one owner is displayed, though it might be "N/A".
152 td.owner_type[1] = STR_NULL; // STR_NULL results in skipping the owner
153 td.owner_type[2] = STR_NULL;
154 td.owner_type[3] = STR_NULL;
155 td.owner[0] = OWNER_NONE;
156 td.owner[1] = OWNER_NONE;
157 td.owner[2] = OWNER_NONE;
158 td.owner[3] = OWNER_NONE;
160 td.station_class = STR_NULL;
161 td.station_name = STR_NULL;
162 td.airport_class = STR_NULL;
163 td.airport_name = STR_NULL;
164 td.airport_tile_name = STR_NULL;
165 td.railtype = STR_NULL;
166 td.rail_speed = 0;
167 td.road_speed = 0;
169 td.grf = NULL;
171 CargoArray acceptance;
172 AddAcceptedCargo(tile, acceptance, NULL);
173 GetTileDesc(tile, &td);
175 uint line_nr = 0;
177 /* Tiletype */
178 SetDParam(0, td.dparam[0]);
179 GetString(this->landinfo_data[line_nr], td.str, lastof(this->landinfo_data[line_nr]));
180 line_nr++;
182 /* Up to four owners */
183 for (uint i = 0; i < 4; i++) {
184 if (td.owner_type[i] == STR_NULL) continue;
186 SetDParam(0, STR_LAND_AREA_INFORMATION_OWNER_N_A);
187 if (td.owner[i] != OWNER_NONE && td.owner[i] != OWNER_WATER) GetNameOfOwner(td.owner[i], tile);
188 GetString(this->landinfo_data[line_nr], td.owner_type[i], lastof(this->landinfo_data[line_nr]));
189 line_nr++;
192 /* Cost to clear/revenue when cleared */
193 StringID str = STR_LAND_AREA_INFORMATION_COST_TO_CLEAR_N_A;
194 Company *c = Company::GetIfValid(_local_company);
195 if (c != NULL) {
196 Money old_money = c->money;
197 c->money = INT64_MAX;
198 assert(_current_company == _local_company);
199 CommandCost costclear = DoCommand(tile, 0, 0, DC_NONE, CMD_LANDSCAPE_CLEAR);
200 c->money = old_money;
201 if (costclear.Succeeded()) {
202 Money cost = costclear.GetCost();
203 if (cost < 0) {
204 cost = -cost; // Negate negative cost to a positive revenue
205 str = STR_LAND_AREA_INFORMATION_REVENUE_WHEN_CLEARED;
206 } else {
207 str = STR_LAND_AREA_INFORMATION_COST_TO_CLEAR;
209 SetDParam(0, cost);
212 GetString(this->landinfo_data[line_nr], str, lastof(this->landinfo_data[line_nr]));
213 line_nr++;
215 /* Location */
216 char tmp[16];
217 seprintf(tmp, lastof(tmp), "0x%.4X", tile);
218 SetDParam(0, TileX(tile));
219 SetDParam(1, TileY(tile));
220 SetDParam(2, GetTileZ(tile));
221 SetDParamStr(3, tmp);
222 GetString(this->landinfo_data[line_nr], STR_LAND_AREA_INFORMATION_LANDINFO_COORDS, lastof(this->landinfo_data[line_nr]));
223 line_nr++;
225 /* Local authority */
226 SetDParam(0, STR_LAND_AREA_INFORMATION_LOCAL_AUTHORITY_NONE);
227 if (t != NULL) {
228 SetDParam(0, STR_TOWN_NAME);
229 SetDParam(1, t->index);
231 GetString(this->landinfo_data[line_nr], STR_LAND_AREA_INFORMATION_LOCAL_AUTHORITY, lastof(this->landinfo_data[line_nr]));
232 line_nr++;
234 /* Build date */
235 if (td.build_date != INVALID_DATE) {
236 SetDParam(0, td.build_date);
237 GetString(this->landinfo_data[line_nr], STR_LAND_AREA_INFORMATION_BUILD_DATE, lastof(this->landinfo_data[line_nr]));
238 line_nr++;
241 /* Station class */
242 if (td.station_class != STR_NULL) {
243 SetDParam(0, td.station_class);
244 GetString(this->landinfo_data[line_nr], STR_LAND_AREA_INFORMATION_STATION_CLASS, lastof(this->landinfo_data[line_nr]));
245 line_nr++;
248 /* Station type name */
249 if (td.station_name != STR_NULL) {
250 SetDParam(0, td.station_name);
251 GetString(this->landinfo_data[line_nr], STR_LAND_AREA_INFORMATION_STATION_TYPE, lastof(this->landinfo_data[line_nr]));
252 line_nr++;
255 /* Airport class */
256 if (td.airport_class != STR_NULL) {
257 SetDParam(0, td.airport_class);
258 GetString(this->landinfo_data[line_nr], STR_LAND_AREA_INFORMATION_AIRPORT_CLASS, lastof(this->landinfo_data[line_nr]));
259 line_nr++;
262 /* Airport name */
263 if (td.airport_name != STR_NULL) {
264 SetDParam(0, td.airport_name);
265 GetString(this->landinfo_data[line_nr], STR_LAND_AREA_INFORMATION_AIRPORT_NAME, lastof(this->landinfo_data[line_nr]));
266 line_nr++;
269 /* Airport tile name */
270 if (td.airport_tile_name != STR_NULL) {
271 SetDParam(0, td.airport_tile_name);
272 GetString(this->landinfo_data[line_nr], STR_LAND_AREA_INFORMATION_AIRPORTTILE_NAME, lastof(this->landinfo_data[line_nr]));
273 line_nr++;
276 /* Rail type name */
277 if (td.railtype != STR_NULL) {
278 SetDParam(0, td.railtype);
279 GetString(this->landinfo_data[line_nr], STR_LANG_AREA_INFORMATION_RAIL_TYPE, lastof(this->landinfo_data[line_nr]));
280 line_nr++;
283 /* Rail speed limit */
284 if (td.rail_speed != 0) {
285 SetDParam(0, td.rail_speed);
286 GetString(this->landinfo_data[line_nr], STR_LANG_AREA_INFORMATION_RAIL_SPEED_LIMIT, lastof(this->landinfo_data[line_nr]));
287 line_nr++;
290 /* Road speed limit */
291 if (td.road_speed != 0) {
292 SetDParam(0, td.road_speed);
293 GetString(this->landinfo_data[line_nr], STR_LANG_AREA_INFORMATION_ROAD_SPEED_LIMIT, lastof(this->landinfo_data[line_nr]));
294 line_nr++;
297 /* NewGRF name */
298 if (td.grf != NULL) {
299 SetDParamStr(0, td.grf);
300 GetString(this->landinfo_data[line_nr], STR_LAND_AREA_INFORMATION_NEWGRF_NAME, lastof(this->landinfo_data[line_nr]));
301 line_nr++;
304 assert(line_nr < LAND_INFO_CENTERED_LINES);
306 /* Mark last line empty */
307 this->landinfo_data[line_nr][0] = '\0';
309 /* Cargo acceptance is displayed in a extra multiline */
310 char *strp = GetString(this->landinfo_data[LAND_INFO_MULTICENTER_LINE], STR_LAND_AREA_INFORMATION_CARGO_ACCEPTED, lastof(this->landinfo_data[LAND_INFO_MULTICENTER_LINE]));
311 bool found = false;
313 for (CargoID i = 0; i < NUM_CARGO; ++i) {
314 if (acceptance[i] > 0) {
315 /* Add a comma between each item. */
316 if (found) {
317 *strp++ = ',';
318 *strp++ = ' ';
320 found = true;
322 /* If the accepted value is less than 8, show it in 1/8:ths */
323 if (acceptance[i] < 8) {
324 SetDParam(0, acceptance[i]);
325 SetDParam(1, CargoSpec::Get(i)->name);
326 strp = GetString(strp, STR_LAND_AREA_INFORMATION_CARGO_EIGHTS, lastof(this->landinfo_data[LAND_INFO_MULTICENTER_LINE]));
327 } else {
328 strp = GetString(strp, CargoSpec::Get(i)->name, lastof(this->landinfo_data[LAND_INFO_MULTICENTER_LINE]));
332 if (!found) this->landinfo_data[LAND_INFO_MULTICENTER_LINE][0] = '\0';
335 virtual bool IsNewGRFInspectable() const
337 return ::IsNewGRFInspectable(GetGrfSpecFeature(this->tile), this->tile);
340 virtual void ShowNewGRFInspectWindow() const
342 ::ShowNewGRFInspectWindow(GetGrfSpecFeature(this->tile), this->tile);
346 * Some data on this window has become invalid.
347 * @param data Information about the changed data.
348 * @param gui_scope Whether the call is done from GUI scope. You may not do everything when not in GUI scope. See #InvalidateWindowData() for details.
350 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
352 if (!gui_scope) return;
353 switch (data) {
354 case 1:
355 /* ReInit, "debug" sprite might have changed */
356 this->ReInit();
357 break;
363 * Show land information window.
364 * @param tile The tile to show information about.
366 void ShowLandInfo(TileIndex tile)
368 DeleteWindowById(WC_LAND_INFO, 0);
369 new LandInfoWindow(tile);
372 static const NWidgetPart _nested_about_widgets[] = {
373 NWidget(NWID_HORIZONTAL),
374 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
375 NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_ABOUT_OPENTTD, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
376 EndContainer(),
377 NWidget(WWT_PANEL, COLOUR_GREY), SetPIP(4, 2, 4),
378 NWidget(WWT_LABEL, COLOUR_GREY), SetDataTip(STR_ABOUT_ORIGINAL_COPYRIGHT, STR_NULL),
379 NWidget(WWT_LABEL, COLOUR_GREY), SetDataTip(STR_ABOUT_VERSION, STR_NULL),
380 NWidget(WWT_FRAME, COLOUR_GREY), SetPadding(0, 5, 1, 5),
381 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_A_SCROLLING_TEXT),
382 EndContainer(),
383 NWidget(WWT_LABEL, COLOUR_GREY, WID_A_WEBSITE), SetDataTip(STR_BLACK_RAW_STRING, STR_NULL),
384 NWidget(WWT_LABEL, COLOUR_GREY), SetDataTip(STR_ABOUT_COPYRIGHT_OPENTTD, STR_NULL),
385 EndContainer(),
388 static WindowDesc _about_desc(
389 WDP_CENTER, NULL, 0, 0,
390 WC_GAME_OPTIONS, WC_NONE,
392 _nested_about_widgets, lengthof(_nested_about_widgets)
395 static const char * const _credits[] = {
396 "Original design by Chris Sawyer",
397 "Original graphics by Simon Foster",
399 "The OpenTTD team (in alphabetical order):",
400 " Albert Hofkamp (Alberth) - GUI expert (since 0.7)",
401 " Matthijs Kooijman (blathijs) - Pathfinder-guru, Debian port (since 0.3)",
402 " Ulf Hermann (fonsinchen) - Cargo Distribution (since 1.3)",
403 " Christoph Elsenhans (frosch) - General coding (since 0.6)",
404 " Lo\xC3\xAF""c Guilloux (glx) - General / Windows Expert (since 0.4.5)",
405 " Michael Lutz (michi_cc) - Path based signals (since 0.7)",
406 " Owen Rudge (orudge) - Forum host, OS/2 port (since 0.1)",
407 " Peter Nelson (peter1138) - Spiritual descendant from NewGRF gods (since 0.4.5)",
408 " Ingo von Borstel (planetmaker) - General, Support (since 1.1)",
409 " Remko Bijker (Rubidium) - Lead coder and way more (since 0.4.5)",
410 " Jos\xC3\xA9 Soler (Terkhen) - General coding (since 1.0)",
411 " Leif Linse (Zuu) - AI/Game Script (since 1.2)",
413 "Inactive Developers:",
414 " Jean-Fran\xC3\xA7ois Claeys (Belugas) - GUI, NewGRF and more (0.4.5 - 1.0)",
415 " Bjarni Corfitzen (Bjarni) - MacOSX port, coder and vehicles (0.3 - 0.7)",
416 " Victor Fischer (Celestar) - Programming everywhere you need him to (0.3 - 0.6)",
417 " Jaroslav Mazanec (KUDr) - YAPG (Yet Another Pathfinder God) ;) (0.4.5 - 0.6)",
418 " Jonathan Coome (Maedhros) - High priest of the NewGRF Temple (0.5 - 0.6)",
419 " Attila B\xC3\xA1n (MiHaMiX) - Developer WebTranslator 1 and 2 (0.3 - 0.5)",
420 " Zden\xC4\x9Bk Sojka (SmatZ) - Bug finder and fixer (0.6 - 1.3)",
421 " Christoph Mallon (Tron) - Programmer, code correctness police (0.3 - 0.5)",
422 " Patric Stout (TrueBrain) - NoAI, NoGo, Network (0.3 - 1.2), sys op (active)",
423 " Thijs Marinussen (Yexo) - AI Framework, General (0.6 - 1.3)",
425 "Retired Developers:",
426 " Tam\xC3\xA1s Farag\xC3\xB3 (Darkvater) - Ex-Lead coder (0.3 - 0.5)",
427 " Dominik Scherer (dominik81) - Lead programmer, GUI expert (0.3 - 0.3)",
428 " Emil Djupfeld (egladil) - MacOSX (0.4.5 - 0.6)",
429 " Simon Sasburg (HackyKid) - Many bugfixes (0.4 - 0.4.5)",
430 " Ludvig Strigeus (ludde) - Original author of OpenTTD, main coder (0.1 - 0.3)",
431 " Cian Duffy (MYOB) - BeOS port / manual writing (0.1 - 0.3)",
432 " Petr Baudi\xC5\xA1 (pasky) - Many patches, NewGRF support (0.3 - 0.3)",
433 " Benedikt Br\xC3\xBCggemeier (skidd13) - Bug fixer and code reworker (0.6 - 0.7)",
434 " Serge Paquet (vurlix) - 2nd contributor after ludde (0.1 - 0.3)",
436 "Special thanks go out to:",
437 " Josef Drexler - For his great work on TTDPatch",
438 " Marcin Grzegorczyk - Track foundations and for describing TTD internals",
439 " Stefan Mei\xC3\x9Fner (sign_de) - For his work on the console",
440 " Mike Ragsdale - OpenTTD installer",
441 " Christian Rosentreter (tokai) - MorphOS / AmigaOS port",
442 " Richard Kempton (richK) - additional airports, initial TGP implementation",
444 " Alberto Demichelis - Squirrel scripting language \xC2\xA9 2003-2008",
445 " L. Peter Deutsch - MD5 implementation \xC2\xA9 1999, 2000, 2002",
446 " Michael Blunck - Pre-signals and semaphores \xC2\xA9 2003",
447 " George - Canal/Lock graphics \xC2\xA9 2003-2004",
448 " Andrew Parkhouse (andythenorth) - River graphics",
449 " David Dallaston (Pikka) - Tram tracks",
450 " All Translators - Who made OpenTTD a truly international game",
451 " Bug Reporters - Without whom OpenTTD would still be full of bugs!",
454 "And last but not least:",
455 " Chris Sawyer - For an amazing game!"
458 struct AboutWindow : public Window {
459 int text_position; ///< The top of the scrolling text
460 byte counter; ///< Used to scroll the text every 5 ticks
461 int line_height; ///< The height of a single line
462 static const int num_visible_lines = 19; ///< The number of lines visible simultaneously
464 AboutWindow() : Window(&_about_desc)
466 this->InitNested(WN_GAME_OPTIONS_ABOUT);
468 this->counter = 5;
469 this->text_position = this->GetWidget<NWidgetBase>(WID_A_SCROLLING_TEXT)->pos_y + this->GetWidget<NWidgetBase>(WID_A_SCROLLING_TEXT)->current_y;
472 virtual void SetStringParameters(int widget) const
474 if (widget == WID_A_WEBSITE) SetDParamStr(0, "Website: http://www.openttd.org");
477 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
479 if (widget != WID_A_SCROLLING_TEXT) return;
481 this->line_height = FONT_HEIGHT_NORMAL;
483 Dimension d;
484 d.height = this->line_height * num_visible_lines;
486 d.width = 0;
487 for (uint i = 0; i < lengthof(_credits); i++) {
488 d.width = max(d.width, GetStringBoundingBox(_credits[i]).width);
490 *size = maxdim(*size, d);
493 virtual void DrawWidget(const Rect &r, int widget) const
495 if (widget != WID_A_SCROLLING_TEXT) return;
497 int y = this->text_position;
499 /* Show all scrolling _credits */
500 for (uint i = 0; i < lengthof(_credits); i++) {
501 if (y >= r.top + 7 && y < r.bottom - this->line_height) {
502 DrawString(r.left, r.right, y, _credits[i], TC_BLACK, SA_LEFT | SA_FORCE);
504 y += this->line_height;
508 virtual void OnTick()
510 if (--this->counter == 0) {
511 this->counter = 5;
512 this->text_position--;
513 /* If the last text has scrolled start a new from the start */
514 if (this->text_position < (int)(this->GetWidget<NWidgetBase>(WID_A_SCROLLING_TEXT)->pos_y - lengthof(_credits) * this->line_height)) {
515 this->text_position = this->GetWidget<NWidgetBase>(WID_A_SCROLLING_TEXT)->pos_y + this->GetWidget<NWidgetBase>(WID_A_SCROLLING_TEXT)->current_y;
517 this->SetDirty();
522 void ShowAboutWindow()
524 DeleteWindowByClass(WC_GAME_OPTIONS);
525 new AboutWindow();
529 * Display estimated costs.
530 * @param cost Estimated cost (or income if negative).
531 * @param x X position of the notification window.
532 * @param y Y position of the notification window.
534 void ShowEstimatedCostOrIncome(Money cost, int x, int y)
536 StringID msg = STR_MESSAGE_ESTIMATED_COST;
538 if (cost < 0) {
539 cost = -cost;
540 msg = STR_MESSAGE_ESTIMATED_INCOME;
542 SetDParam(0, cost);
543 ShowErrorMessage(msg, INVALID_STRING_ID, WL_INFO, x, y);
547 * Display animated income or costs on the map.
548 * @param x World X position of the animation location.
549 * @param y World Y position of the animation location.
550 * @param z World Z position of the animation location.
551 * @param cost Estimated cost (or income if negative).
553 void ShowCostOrIncomeAnimation(int x, int y, int z, Money cost)
555 Point pt = RemapCoords(x, y, z);
556 StringID msg = STR_INCOME_FLOAT_COST;
558 if (cost < 0) {
559 cost = -cost;
560 msg = STR_INCOME_FLOAT_INCOME;
562 SetDParam(0, cost);
563 AddTextEffect(msg, pt.x, pt.y, DAY_TICKS, TE_RISING);
567 * Display animated feeder income.
568 * @param x World X position of the animation location.
569 * @param y World Y position of the animation location.
570 * @param z World Z position of the animation location.
571 * @param transfer Estimated feeder income.
572 * @param income Real income from goods being delivered to their final destination.
574 void ShowFeederIncomeAnimation(int x, int y, int z, Money transfer, Money income)
576 Point pt = RemapCoords(x, y, z);
578 SetDParam(0, transfer);
579 if (income == 0) {
580 AddTextEffect(STR_FEEDER, pt.x, pt.y, DAY_TICKS, TE_RISING);
581 } else {
582 StringID msg = STR_FEEDER_COST;
583 if (income < 0) {
584 income = -income;
585 msg = STR_FEEDER_INCOME;
587 SetDParam(1, income);
588 AddTextEffect(msg, pt.x, pt.y, DAY_TICKS, TE_RISING);
593 * Display vehicle loading indicators.
594 * @param x World X position of the animation location.
595 * @param y World Y position of the animation location.
596 * @param z World Z position of the animation location.
597 * @param percent Estimated feeder income.
598 * @param string String which is drawn on the map.
599 * @return TextEffectID to be used for future updates of the loading indicators.
601 TextEffectID ShowFillingPercent(int x, int y, int z, uint8 percent, StringID string)
603 Point pt = RemapCoords(x, y, z);
605 assert(string != STR_NULL);
607 SetDParam(0, percent);
608 return AddTextEffect(string, pt.x, pt.y, 0, TE_STATIC);
612 * Update vehicle loading indicators.
613 * @param te_id TextEffectID to be updated.
614 * @param string String which is printed.
616 void UpdateFillingPercent(TextEffectID te_id, uint8 percent, StringID string)
618 assert(string != STR_NULL);
620 SetDParam(0, percent);
621 UpdateTextEffect(te_id, string);
625 * Hide vehicle loading indicators.
626 * @param *te_id TextEffectID which is supposed to be hidden.
628 void HideFillingPercent(TextEffectID *te_id)
630 if (*te_id == INVALID_TE_ID) return;
632 RemoveTextEffect(*te_id);
633 *te_id = INVALID_TE_ID;
636 static const NWidgetPart _nested_tooltips_widgets[] = {
637 NWidget(WWT_PANEL, COLOUR_GREY, WID_TT_BACKGROUND), SetMinimalSize(200, 32), EndContainer(),
640 static WindowDesc _tool_tips_desc(
641 WDP_MANUAL, NULL, 0, 0, // Coordinates and sizes are not used,
642 WC_TOOLTIPS, WC_NONE,
643 WDF_NO_FOCUS,
644 _nested_tooltips_widgets, lengthof(_nested_tooltips_widgets)
647 /** Window for displaying a tooltip. */
648 struct TooltipsWindow : public Window
650 StringID string_id; ///< String to display as tooltip.
651 byte paramcount; ///< Number of string parameters in #string_id.
652 uint64 params[5]; ///< The string parameters.
653 TooltipCloseCondition close_cond; ///< Condition for closing the window.
655 TooltipsWindow(Window *parent, StringID str, uint paramcount, const uint64 params[], TooltipCloseCondition close_tooltip) : Window(&_tool_tips_desc)
657 this->parent = parent;
658 this->string_id = str;
659 assert_compile(sizeof(this->params[0]) == sizeof(params[0]));
660 assert(paramcount <= lengthof(this->params));
661 memcpy(this->params, params, sizeof(this->params[0]) * paramcount);
662 this->paramcount = paramcount;
663 this->close_cond = close_tooltip;
665 this->InitNested();
667 CLRBITS(this->flags, WF_WHITE_BORDER);
670 virtual Point OnInitialPosition(int16 sm_width, int16 sm_height, int window_number)
672 /* Find the free screen space between the main toolbar at the top, and the statusbar at the bottom.
673 * Add a fixed distance 2 so the tooltip floats free from both bars.
675 int scr_top = GetMainViewTop() + 2;
676 int scr_bot = GetMainViewBottom() - 2;
678 Point pt;
680 /* Correctly position the tooltip position, watch out for window and cursor size
681 * Clamp value to below main toolbar and above statusbar. If tooltip would
682 * go below window, flip it so it is shown above the cursor */
683 pt.y = Clamp(_cursor.pos.y + _cursor.total_size.y + _cursor.total_offs.y + 5, scr_top, scr_bot);
684 if (pt.y + sm_height > scr_bot) pt.y = min(_cursor.pos.y + _cursor.total_offs.y - 5, scr_bot) - sm_height;
685 pt.x = sm_width >= _screen.width ? 0 : Clamp(_cursor.pos.x - (sm_width >> 1), 0, _screen.width - sm_width);
687 return pt;
690 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
692 /* There is only one widget. */
693 for (uint i = 0; i != this->paramcount; i++) SetDParam(i, this->params[i]);
695 size->width = min(GetStringBoundingBox(this->string_id).width, ScaleGUITrad(194));
696 size->height = GetStringHeight(this->string_id, size->width);
698 /* Increase slightly to have some space around the box. */
699 size->width += 2 + WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
700 size->height += 2 + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
703 virtual void DrawWidget(const Rect &r, int widget) const
705 /* There is only one widget. */
706 GfxFillRect(r.left, r.top, r.right, r.bottom, PC_BLACK);
707 GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, PC_LIGHT_YELLOW);
709 for (uint arg = 0; arg < this->paramcount; arg++) {
710 SetDParam(arg, this->params[arg]);
712 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);
715 virtual void OnMouseLoop()
717 /* Always close tooltips when the cursor is not in our window. */
718 if (!_cursor.in_window) {
719 delete this;
720 return;
723 /* We can show tooltips while dragging tools. These are shown as long as
724 * we are dragging the tool. Normal tooltips work with hover or rmb. */
725 switch (this->close_cond) {
726 case TCC_RIGHT_CLICK: if (!_right_button_down) delete this; break;
727 case TCC_LEFT_CLICK: if (!_left_button_down) delete this; break;
728 case TCC_HOVER: if (!_mouse_hovering) delete this; break;
734 * Shows a tooltip
735 * @param parent The window this tooltip is related to.
736 * @param str String to be displayed
737 * @param paramcount number of params to deal with
738 * @param params (optional) up to 5 pieces of additional information that may be added to a tooltip
739 * @param use_left_mouse_button close the tooltip when the left (true) or right (false) mouse button is released
741 void GuiShowTooltips(Window *parent, StringID str, uint paramcount, const uint64 params[], TooltipCloseCondition close_tooltip)
743 DeleteWindowById(WC_TOOLTIPS, 0);
745 if (str == STR_NULL) return;
747 new TooltipsWindow(parent, str, paramcount, params, close_tooltip);
750 void QueryString::HandleEditBox(Window *w, int wid)
752 if (w->IsWidgetGloballyFocused(wid) && this->text.HandleCaret()) {
753 w->SetWidgetDirty(wid);
755 /* For the OSK also invalidate the parent window */
756 if (w->window_class == WC_OSK) w->InvalidateData();
760 void QueryString::DrawEditBox(const Window *w, int wid) const
762 const NWidgetLeaf *wi = w->GetWidget<NWidgetLeaf>(wid);
764 assert((wi->type & WWT_MASK) == WWT_EDITBOX);
766 bool rtl = _current_text_dir == TD_RTL;
767 Dimension sprite_size = GetSpriteSize(rtl ? SPR_IMG_DELETE_RIGHT : SPR_IMG_DELETE_LEFT);
768 int clearbtn_width = sprite_size.width + WD_IMGBTN_LEFT + WD_IMGBTN_RIGHT;
770 int clearbtn_left = wi->pos_x + (rtl ? 0 : wi->current_x - clearbtn_width);
771 int clearbtn_right = wi->pos_x + (rtl ? clearbtn_width : wi->current_x) - 1;
772 int left = wi->pos_x + (rtl ? clearbtn_width : 0);
773 int right = wi->pos_x + (rtl ? wi->current_x : wi->current_x - clearbtn_width) - 1;
775 int top = wi->pos_y;
776 int bottom = wi->pos_y + wi->current_y - 1;
778 DrawFrameRect(clearbtn_left, top, clearbtn_right, bottom, wi->colour, wi->IsLowered() ? FR_LOWERED : FR_NONE);
779 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));
780 if (this->text.bytes == 1) GfxFillRect(clearbtn_left + 1, top + 1, clearbtn_right - 1, bottom - 1, _colour_gradient[wi->colour & 0xF][2], FILLRECT_CHECKER);
782 DrawFrameRect(left, top, right, bottom, wi->colour, FR_LOWERED | FR_DARKENED);
783 GfxFillRect(left + 1, top + 1, right - 1, bottom - 1, PC_BLACK);
785 /* Limit the drawing of the string inside the widget boundaries */
786 DrawPixelInfo dpi;
787 if (!FillDrawPixelInfo(&dpi, left + WD_FRAMERECT_LEFT, top + WD_FRAMERECT_TOP, right - left - WD_FRAMERECT_RIGHT, bottom - top - WD_FRAMERECT_BOTTOM)) return;
789 DrawPixelInfo *old_dpi = _cur_dpi;
790 _cur_dpi = &dpi;
792 /* We will take the current widget length as maximum width, with a small
793 * space reserved at the end for the caret to show */
794 const Textbuf *tb = &this->text;
795 int delta = min(0, (right - left) - tb->pixels - 10);
797 if (tb->caretxoffs + delta < 0) delta = -tb->caretxoffs;
799 /* If we have a marked area, draw a background highlight. */
800 if (tb->marklength != 0) GfxFillRect(delta + tb->markxoffs, 0, delta + tb->markxoffs + tb->marklength - 1, bottom - top, PC_GREY);
802 DrawString(delta, tb->pixels, 0, tb->buf, TC_YELLOW);
803 bool focussed = w->IsWidgetGloballyFocused(wid) || IsOSKOpenedFor(w, wid);
804 if (focussed && tb->caret) {
805 int caret_width = GetStringBoundingBox("_").width;
806 DrawString(tb->caretxoffs + delta, tb->caretxoffs + delta + caret_width, 0, "_", TC_WHITE);
809 _cur_dpi = old_dpi;
813 * Get the current caret position.
814 * @param w Window the edit box is in.
815 * @param wid Widget index.
816 * @return Top-left location of the caret, relative to the window.
818 Point QueryString::GetCaretPosition(const Window *w, int wid) const
820 const NWidgetLeaf *wi = w->GetWidget<NWidgetLeaf>(wid);
822 assert((wi->type & WWT_MASK) == WWT_EDITBOX);
824 bool rtl = _current_text_dir == TD_RTL;
825 Dimension sprite_size = GetSpriteSize(rtl ? SPR_IMG_DELETE_RIGHT : SPR_IMG_DELETE_LEFT);
826 int clearbtn_width = sprite_size.width + WD_IMGBTN_LEFT + WD_IMGBTN_RIGHT;
828 int left = wi->pos_x + (rtl ? clearbtn_width : 0);
829 int right = wi->pos_x + (rtl ? wi->current_x : wi->current_x - clearbtn_width) - 1;
831 /* Clamp caret position to be inside out current width. */
832 const Textbuf *tb = &this->text;
833 int delta = min(0, (right - left) - tb->pixels - 10);
834 if (tb->caretxoffs + delta < 0) delta = -tb->caretxoffs;
836 Point pt = {left + WD_FRAMERECT_LEFT + tb->caretxoffs + delta, (int)wi->pos_y + WD_FRAMERECT_TOP};
837 return pt;
841 * Get the bounding rectangle for a range of the query string.
842 * @param w Window the edit box is in.
843 * @param wid Widget index.
844 * @param from Start of the string range.
845 * @param to End of the string range.
846 * @return Rectangle encompassing the string range, relative to the window.
848 Rect QueryString::GetBoundingRect(const Window *w, int wid, const char *from, const char *to) const
850 const NWidgetLeaf *wi = w->GetWidget<NWidgetLeaf>(wid);
852 assert((wi->type & WWT_MASK) == WWT_EDITBOX);
854 bool rtl = _current_text_dir == TD_RTL;
855 Dimension sprite_size = GetSpriteSize(rtl ? SPR_IMG_DELETE_RIGHT : SPR_IMG_DELETE_LEFT);
856 int clearbtn_width = sprite_size.width + WD_IMGBTN_LEFT + WD_IMGBTN_RIGHT;
858 int left = wi->pos_x + (rtl ? clearbtn_width : 0);
859 int right = wi->pos_x + (rtl ? wi->current_x : wi->current_x - clearbtn_width) - 1;
861 int top = wi->pos_y + WD_FRAMERECT_TOP;
862 int bottom = wi->pos_y + wi->current_y - 1 - WD_FRAMERECT_BOTTOM;
864 /* Clamp caret position to be inside our current width. */
865 const Textbuf *tb = &this->text;
866 int delta = min(0, (right - left) - tb->pixels - 10);
867 if (tb->caretxoffs + delta < 0) delta = -tb->caretxoffs;
869 /* Get location of first and last character. */
870 Point p1 = GetCharPosInString(tb->buf, from, FS_NORMAL);
871 Point p2 = from != to ? GetCharPosInString(tb->buf, to, FS_NORMAL) : p1;
873 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 };
875 return r;
879 * Get the character that is rendered at a position.
880 * @param w Window the edit box is in.
881 * @param wid Widget index.
882 * @param pt Position to test.
883 * @return Pointer to the character at the position or NULL if no character is at the position.
885 const char *QueryString::GetCharAtPosition(const Window *w, int wid, const Point &pt) const
887 const NWidgetLeaf *wi = w->GetWidget<NWidgetLeaf>(wid);
889 assert((wi->type & WWT_MASK) == WWT_EDITBOX);
891 bool rtl = _current_text_dir == TD_RTL;
892 Dimension sprite_size = GetSpriteSize(rtl ? SPR_IMG_DELETE_RIGHT : SPR_IMG_DELETE_LEFT);
893 int clearbtn_width = sprite_size.width + WD_IMGBTN_LEFT + WD_IMGBTN_RIGHT;
895 int left = wi->pos_x + (rtl ? clearbtn_width : 0);
896 int right = wi->pos_x + (rtl ? wi->current_x : wi->current_x - clearbtn_width) - 1;
898 int top = wi->pos_y + WD_FRAMERECT_TOP;
899 int bottom = wi->pos_y + wi->current_y - 1 - WD_FRAMERECT_BOTTOM;
901 if (!IsInsideMM(pt.y, top, bottom)) return NULL;
903 /* Clamp caret position to be inside our current width. */
904 const Textbuf *tb = &this->text;
905 int delta = min(0, (right - left) - tb->pixels - 10);
906 if (tb->caretxoffs + delta < 0) delta = -tb->caretxoffs;
908 return ::GetCharAtPosition(tb->buf, pt.x - delta - left);
911 void QueryString::ClickEditBox(Window *w, Point pt, int wid, int click_count, bool focus_changed)
913 const NWidgetLeaf *wi = w->GetWidget<NWidgetLeaf>(wid);
915 assert((wi->type & WWT_MASK) == WWT_EDITBOX);
917 bool rtl = _current_text_dir == TD_RTL;
918 int clearbtn_width = GetSpriteSize(rtl ? SPR_IMG_DELETE_RIGHT : SPR_IMG_DELETE_LEFT).width;
920 int clearbtn_left = wi->pos_x + (rtl ? 0 : wi->current_x - clearbtn_width);
922 if (IsInsideBS(pt.x, clearbtn_left, clearbtn_width)) {
923 if (this->text.bytes > 1) {
924 this->text.DeleteAll();
925 w->HandleButtonClick(wid);
926 w->OnEditboxChanged(wid);
928 return;
931 if (w->window_class != WC_OSK && _settings_client.gui.osk_activation != OSKA_DISABLED &&
932 (!focus_changed || _settings_client.gui.osk_activation == OSKA_IMMEDIATELY) &&
933 (click_count == 2 || _settings_client.gui.osk_activation != OSKA_DOUBLE_CLICK)) {
934 /* Open the OSK window */
935 ShowOnScreenKeyboard(w, wid);
939 /** Class for the string query window. */
940 struct QueryStringWindow : public Window
942 QueryString editbox; ///< Editbox.
943 QueryStringFlags flags; ///< Flags controlling behaviour of the window.
945 QueryStringWindow(StringID str, StringID caption, uint max_bytes, uint max_chars, WindowDesc *desc, Window *parent, CharSetFilter afilter, QueryStringFlags flags) :
946 Window(desc), editbox(max_bytes, max_chars)
948 char *last_of = &this->editbox.text.buf[this->editbox.text.max_bytes - 1];
949 GetString(this->editbox.text.buf, str, last_of);
950 str_validate(this->editbox.text.buf, last_of, SVS_NONE);
952 /* Make sure the name isn't too long for the text buffer in the number of
953 * characters (not bytes). max_chars also counts the '\0' characters. */
954 while (Utf8StringLength(this->editbox.text.buf) + 1 > this->editbox.text.max_chars) {
955 *Utf8PrevChar(this->editbox.text.buf + strlen(this->editbox.text.buf)) = '\0';
958 this->editbox.text.UpdateSize();
960 if ((flags & QSF_ACCEPT_UNCHANGED) == 0) this->editbox.orig = stredup(this->editbox.text.buf);
962 this->querystrings[WID_QS_TEXT] = &this->editbox;
963 this->editbox.caption = caption;
964 this->editbox.cancel_button = WID_QS_CANCEL;
965 this->editbox.ok_button = WID_QS_OK;
966 this->editbox.text.afilter = afilter;
967 this->flags = flags;
969 this->InitNested(WN_QUERY_STRING);
971 this->parent = parent;
973 this->SetFocusedWidget(WID_QS_TEXT);
976 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
978 if (widget == WID_QS_DEFAULT && (this->flags & QSF_ENABLE_DEFAULT) == 0) {
979 /* We don't want this widget to show! */
980 fill->width = 0;
981 resize->width = 0;
982 size->width = 0;
986 virtual void SetStringParameters(int widget) const
988 if (widget == WID_QS_CAPTION) SetDParam(0, this->editbox.caption);
991 void OnOk()
993 if (this->editbox.orig == NULL || strcmp(this->editbox.text.buf, this->editbox.orig) != 0) {
994 /* If the parent is NULL, the editbox is handled by general function
995 * HandleOnEditText */
996 if (this->parent != NULL) {
997 this->parent->OnQueryTextFinished(this->editbox.text.buf);
998 } else {
999 HandleOnEditText(this->editbox.text.buf);
1001 this->editbox.handled = true;
1005 virtual void OnClick(Point pt, int widget, int click_count)
1007 switch (widget) {
1008 case WID_QS_DEFAULT:
1009 this->editbox.text.DeleteAll();
1010 FALLTHROUGH;
1012 case WID_QS_OK:
1013 this->OnOk();
1014 FALLTHROUGH;
1016 case WID_QS_CANCEL:
1017 delete this;
1018 break;
1022 ~QueryStringWindow()
1024 if (!this->editbox.handled && this->parent != NULL) {
1025 Window *parent = this->parent;
1026 this->parent = NULL; // so parent doesn't try to delete us again
1027 parent->OnQueryTextFinished(NULL);
1032 static const NWidgetPart _nested_query_string_widgets[] = {
1033 NWidget(NWID_HORIZONTAL),
1034 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
1035 NWidget(WWT_CAPTION, COLOUR_GREY, WID_QS_CAPTION), SetDataTip(STR_WHITE_STRING, STR_NULL),
1036 EndContainer(),
1037 NWidget(WWT_PANEL, COLOUR_GREY),
1038 NWidget(WWT_EDITBOX, COLOUR_GREY, WID_QS_TEXT), SetMinimalSize(256, 12), SetFill(1, 1), SetPadding(2, 2, 2, 2),
1039 EndContainer(),
1040 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
1041 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_QS_DEFAULT), SetMinimalSize(87, 12), SetFill(1, 1), SetDataTip(STR_BUTTON_DEFAULT, STR_NULL),
1042 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_QS_CANCEL), SetMinimalSize(86, 12), SetFill(1, 1), SetDataTip(STR_BUTTON_CANCEL, STR_NULL),
1043 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_QS_OK), SetMinimalSize(87, 12), SetFill(1, 1), SetDataTip(STR_BUTTON_OK, STR_NULL),
1044 EndContainer(),
1047 static WindowDesc _query_string_desc(
1048 WDP_CENTER, "query_string", 0, 0,
1049 WC_QUERY_STRING, WC_NONE,
1051 _nested_query_string_widgets, lengthof(_nested_query_string_widgets)
1055 * Show a query popup window with a textbox in it.
1056 * @param str StringID for the text shown in the textbox
1057 * @param caption StringID of text shown in caption of querywindow
1058 * @param maxsize maximum size in bytes or characters (including terminating '\0') depending on flags
1059 * @param parent pointer to a Window that will handle the events (ok/cancel) of this
1060 * window. If NULL, results are handled by global function HandleOnEditText
1061 * @param afilter filters out unwanted character input
1062 * @param flags various flags, @see QueryStringFlags
1064 void ShowQueryString(StringID str, StringID caption, uint maxsize, Window *parent, CharSetFilter afilter, QueryStringFlags flags)
1066 DeleteWindowByClass(WC_QUERY_STRING);
1067 new QueryStringWindow(str, caption, ((flags & QSF_LEN_IN_CHARS) ? MAX_CHAR_LENGTH : 1) * maxsize, maxsize, &_query_string_desc, parent, afilter, flags);
1071 * Window used for asking the user a YES/NO question.
1073 struct QueryWindow : public Window {
1074 QueryCallbackProc *proc; ///< callback function executed on closing of popup. Window* points to parent, bool is true if 'yes' clicked, false otherwise
1075 uint64 params[10]; ///< local copy of _decode_parameters
1076 StringID message; ///< message shown for query window
1077 StringID caption; ///< title of window
1079 QueryWindow(WindowDesc *desc, StringID caption, StringID message, Window *parent, QueryCallbackProc *callback) : Window(desc)
1081 /* Create a backup of the variadic arguments to strings because it will be
1082 * overridden pretty often. We will copy these back for drawing */
1083 CopyOutDParam(this->params, 0, lengthof(this->params));
1084 this->caption = caption;
1085 this->message = message;
1086 this->proc = callback;
1088 this->InitNested(WN_CONFIRM_POPUP_QUERY);
1090 this->parent = parent;
1091 this->left = parent->left + (parent->width / 2) - (this->width / 2);
1092 this->top = parent->top + (parent->height / 2) - (this->height / 2);
1095 ~QueryWindow()
1097 if (this->proc != NULL) this->proc(this->parent, false);
1100 virtual void SetStringParameters(int widget) const
1102 switch (widget) {
1103 case WID_Q_CAPTION:
1104 CopyInDParam(1, this->params, lengthof(this->params));
1105 SetDParam(0, this->caption);
1106 break;
1108 case WID_Q_TEXT:
1109 CopyInDParam(0, this->params, lengthof(this->params));
1110 break;
1114 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
1116 if (widget != WID_Q_TEXT) return;
1118 Dimension d = GetStringMultiLineBoundingBox(this->message, *size);
1119 d.width += WD_FRAMETEXT_LEFT + WD_FRAMETEXT_RIGHT;
1120 d.height += WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
1121 *size = d;
1124 virtual void DrawWidget(const Rect &r, int widget) const
1126 if (widget != WID_Q_TEXT) return;
1128 DrawStringMultiLine(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, r.top + WD_FRAMERECT_TOP, r.bottom - WD_FRAMERECT_BOTTOM,
1129 this->message, TC_FROMSTRING, SA_CENTER);
1132 virtual void OnClick(Point pt, int widget, int click_count)
1134 switch (widget) {
1135 case WID_Q_YES: {
1136 /* in the Generate New World window, clicking 'Yes' causes
1137 * DeleteNonVitalWindows() to be called - we shouldn't be in a window then */
1138 QueryCallbackProc *proc = this->proc;
1139 Window *parent = this->parent;
1140 /* Prevent the destructor calling the callback function */
1141 this->proc = NULL;
1142 delete this;
1143 if (proc != NULL) {
1144 proc(parent, true);
1145 proc = NULL;
1147 break;
1149 case WID_Q_NO:
1150 delete this;
1151 break;
1155 virtual EventState OnKeyPress(WChar key, uint16 keycode)
1157 /* ESC closes the window, Enter confirms the action */
1158 switch (keycode) {
1159 case WKC_RETURN:
1160 case WKC_NUM_ENTER:
1161 if (this->proc != NULL) {
1162 this->proc(this->parent, true);
1163 this->proc = NULL;
1165 FALLTHROUGH;
1167 case WKC_ESC:
1168 delete this;
1169 return ES_HANDLED;
1171 return ES_NOT_HANDLED;
1175 static const NWidgetPart _nested_query_widgets[] = {
1176 NWidget(NWID_HORIZONTAL),
1177 NWidget(WWT_CLOSEBOX, COLOUR_RED),
1178 NWidget(WWT_CAPTION, COLOUR_RED, WID_Q_CAPTION), SetDataTip(STR_JUST_STRING, STR_NULL),
1179 EndContainer(),
1180 NWidget(WWT_PANEL, COLOUR_RED), SetPIP(8, 15, 8),
1181 NWidget(WWT_TEXT, COLOUR_RED, WID_Q_TEXT), SetMinimalSize(200, 12),
1182 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(20, 29, 20),
1183 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_Q_NO), SetMinimalSize(71, 12), SetFill(1, 1), SetDataTip(STR_QUIT_NO, STR_NULL),
1184 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_Q_YES), SetMinimalSize(71, 12), SetFill(1, 1), SetDataTip(STR_QUIT_YES, STR_NULL),
1185 EndContainer(),
1186 EndContainer(),
1189 static WindowDesc _query_desc(
1190 WDP_CENTER, NULL, 0, 0,
1191 WC_CONFIRM_POPUP_QUERY, WC_NONE,
1192 WDF_MODAL,
1193 _nested_query_widgets, lengthof(_nested_query_widgets)
1197 * Show a modal confirmation window with standard 'yes' and 'no' buttons
1198 * The window is aligned to the centre of its parent.
1199 * @param caption string shown as window caption
1200 * @param message string that will be shown for the window
1201 * @param parent pointer to parent window, if this pointer is NULL the parent becomes
1202 * the main window WC_MAIN_WINDOW
1203 * @param callback callback function pointer to set in the window descriptor
1205 void ShowQuery(StringID caption, StringID message, Window *parent, QueryCallbackProc *callback)
1207 if (parent == NULL) parent = FindWindowById(WC_MAIN_WINDOW, 0);
1209 const Window *w;
1210 FOR_ALL_WINDOWS_FROM_BACK(w) {
1211 if (w->window_class != WC_CONFIRM_POPUP_QUERY) continue;
1213 const QueryWindow *qw = (const QueryWindow *)w;
1214 if (qw->parent != parent || qw->proc != callback) continue;
1216 delete qw;
1217 break;
1220 new QueryWindow(&_query_desc, caption, message, parent, callback);