Rearrange storage of reserved tracks for railway tiles
[openttd/fttd.git] / src / misc_gui.cpp
blob413904a94f06ff27fe9c832a12bce562c9f6ee42
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 "map/slope.h"
30 #include "widgets/misc_widget.h"
32 #include "table/strings.h"
34 /** Method to open the OSK. */
35 enum OskActivation {
36 OSKA_DISABLED, ///< The OSK shall not be activated at all.
37 OSKA_DOUBLE_CLICK, ///< Double click on the edit box opens OSK.
38 OSKA_SINGLE_CLICK, ///< Single click after focus click opens OSK.
39 OSKA_IMMEDIATELY, ///< Focusing click already opens OSK.
43 static const NWidgetPart _nested_land_info_widgets[] = {
44 NWidget(NWID_HORIZONTAL),
45 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
46 NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_LAND_AREA_INFORMATION_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
47 NWidget(WWT_DEBUGBOX, COLOUR_GREY),
48 EndContainer(),
49 NWidget(WWT_PANEL, COLOUR_GREY, WID_LI_BACKGROUND), EndContainer(),
52 static WindowDesc _land_info_desc(
53 WDP_AUTO, "land_info", 0, 0,
54 WC_LAND_INFO, WC_NONE,
56 _nested_land_info_widgets, lengthof(_nested_land_info_widgets)
59 class LandInfoWindow : public Window {
60 enum LandInfoLines {
61 LAND_INFO_CENTERED_LINES = 12, ///< Up to 12 centered lines
62 LAND_INFO_MULTICENTER_LINE = LAND_INFO_CENTERED_LINES, ///< One multicenter line
63 LAND_INFO_LINE_END,
66 static const uint LAND_INFO_LINE_BUFF_SIZE = 512;
68 public:
69 char landinfo_data[LAND_INFO_LINE_END][LAND_INFO_LINE_BUFF_SIZE];
70 TileIndex tile;
72 virtual void DrawWidget(const Rect &r, int widget) const
74 if (widget != WID_LI_BACKGROUND) return;
76 uint y = r.top + WD_TEXTPANEL_TOP;
77 for (uint i = 0; i < LAND_INFO_CENTERED_LINES; i++) {
78 if (StrEmpty(this->landinfo_data[i])) break;
80 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);
81 y += FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL;
82 if (i == 0) y += 4;
85 if (!StrEmpty(this->landinfo_data[LAND_INFO_MULTICENTER_LINE])) {
86 SetDParamStr(0, this->landinfo_data[LAND_INFO_MULTICENTER_LINE]);
87 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);
91 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
93 if (widget != WID_LI_BACKGROUND) return;
95 size->height = WD_TEXTPANEL_TOP + WD_TEXTPANEL_BOTTOM;
96 for (uint i = 0; i < LAND_INFO_CENTERED_LINES; i++) {
97 if (StrEmpty(this->landinfo_data[i])) break;
99 uint width = GetStringBoundingBox(this->landinfo_data[i]).width + WD_FRAMETEXT_LEFT + WD_FRAMETEXT_RIGHT;
100 size->width = max(size->width, width);
102 size->height += FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL;
103 if (i == 0) size->height += 4;
106 if (!StrEmpty(this->landinfo_data[LAND_INFO_MULTICENTER_LINE])) {
107 uint width = GetStringBoundingBox(this->landinfo_data[LAND_INFO_MULTICENTER_LINE]).width + WD_FRAMETEXT_LEFT + WD_FRAMETEXT_RIGHT;
108 size->width = max(size->width, min(300u, width));
109 SetDParamStr(0, this->landinfo_data[LAND_INFO_MULTICENTER_LINE]);
110 size->height += GetStringHeight(STR_JUST_RAW_STRING, size->width - WD_FRAMETEXT_LEFT - WD_FRAMETEXT_RIGHT);
114 LandInfoWindow(TileIndex tile) : Window(&_land_info_desc), tile(tile)
116 this->InitNested();
118 #if defined(_DEBUG)
119 # define LANDINFOD_LEVEL 0
120 #else
121 # define LANDINFOD_LEVEL 1
122 #endif
123 DEBUG(misc, LANDINFOD_LEVEL, "TILE: %#x (%i,%i)", tile, TileX(tile), TileY(tile));
124 DEBUG(misc, LANDINFOD_LEVEL, "zh = %#x", _mth[tile]);
125 DEBUG(misc, LANDINFOD_LEVEL, "m0 = %#x", _mc[tile].m0);
126 DEBUG(misc, LANDINFOD_LEVEL, "m1 = %#x", _mc[tile].m1);
127 DEBUG(misc, LANDINFOD_LEVEL, "m2 = %#x", _mc[tile].m2);
128 DEBUG(misc, LANDINFOD_LEVEL, "m3 = %#x", _mc[tile].m3);
129 DEBUG(misc, LANDINFOD_LEVEL, "m4 = %#x", _mc[tile].m4);
130 DEBUG(misc, LANDINFOD_LEVEL, "m5 = %#x", _mc[tile].m5);
131 DEBUG(misc, LANDINFOD_LEVEL, "m7 = %#x", _mc[tile].m7);
132 #undef LANDINFOD_LEVEL
135 virtual void OnInit()
137 Town *t = ClosestTownFromTile(tile, _settings_game.economy.dist_local_authority);
139 /* Because build_date is not set yet in every TileDesc, we make sure it is empty */
140 TileDesc td;
142 td.build_date = INVALID_DATE;
144 /* Most tiles have only one owner, but
145 * - drivethrough roadstops can be build on town owned roads (up to 2 owners) and
146 * - roads can have up to four owners (railroad, road, tram, 3rd-roadtype "highway").
148 td.owner_type[0] = STR_LAND_AREA_INFORMATION_OWNER; // At least one owner is displayed, though it might be "N/A".
149 td.owner_type[1] = STR_NULL; // STR_NULL results in skipping the owner
150 td.owner_type[2] = STR_NULL;
151 td.owner_type[3] = STR_NULL;
152 td.owner[0] = OWNER_NONE;
153 td.owner[1] = OWNER_NONE;
154 td.owner[2] = OWNER_NONE;
155 td.owner[3] = OWNER_NONE;
157 td.station_class = STR_NULL;
158 td.station_name = STR_NULL;
159 td.airport_class = STR_NULL;
160 td.airport_name = STR_NULL;
161 td.airport_tile_name = STR_NULL;
162 td.rail_speed = 0;
164 td.grf = NULL;
166 CargoArray acceptance;
167 AddAcceptedCargo(tile, acceptance, NULL);
168 GetTileDesc(tile, &td);
170 uint line_nr = 0;
172 /* Tiletype */
173 SetDParam(0, td.dparam[0]);
174 GetString(this->landinfo_data[line_nr], td.str, lastof(this->landinfo_data[line_nr]));
175 line_nr++;
177 /* Up to four owners */
178 for (uint i = 0; i < 4; i++) {
179 if (td.owner_type[i] == STR_NULL) continue;
181 SetDParam(0, STR_LAND_AREA_INFORMATION_OWNER_N_A);
182 if (td.owner[i] != OWNER_NONE && td.owner[i] != OWNER_WATER) GetNameOfOwner(td.owner[i], tile);
183 GetString(this->landinfo_data[line_nr], td.owner_type[i], lastof(this->landinfo_data[line_nr]));
184 line_nr++;
187 /* Cost to clear/revenue when cleared */
188 StringID str = STR_LAND_AREA_INFORMATION_COST_TO_CLEAR_N_A;
189 Company *c = Company::GetIfValid(_local_company);
190 if (c != NULL) {
191 Money old_money = c->money;
192 c->money = INT64_MAX;
193 assert(_current_company == _local_company);
194 CommandCost costclear = DoCommand(tile, 0, 0, DC_NONE, CMD_LANDSCAPE_CLEAR);
195 c->money = old_money;
196 if (costclear.Succeeded()) {
197 Money cost = costclear.GetCost();
198 if (cost < 0) {
199 cost = -cost; // Negate negative cost to a positive revenue
200 str = STR_LAND_AREA_INFORMATION_REVENUE_WHEN_CLEARED;
201 } else {
202 str = STR_LAND_AREA_INFORMATION_COST_TO_CLEAR;
204 SetDParam(0, cost);
207 GetString(this->landinfo_data[line_nr], str, lastof(this->landinfo_data[line_nr]));
208 line_nr++;
210 /* Location */
211 char tmp[16];
212 snprintf(tmp, lengthof(tmp), "0x%.4X", tile);
213 SetDParam(0, TileX(tile));
214 SetDParam(1, TileY(tile));
215 SetDParam(2, GetTileZ(tile));
216 SetDParamStr(3, tmp);
217 GetString(this->landinfo_data[line_nr], STR_LAND_AREA_INFORMATION_LANDINFO_COORDS, lastof(this->landinfo_data[line_nr]));
218 line_nr++;
220 /* Local authority */
221 SetDParam(0, STR_LAND_AREA_INFORMATION_LOCAL_AUTHORITY_NONE);
222 if (t != NULL) {
223 SetDParam(0, STR_TOWN_NAME);
224 SetDParam(1, t->index);
226 GetString(this->landinfo_data[line_nr], STR_LAND_AREA_INFORMATION_LOCAL_AUTHORITY, lastof(this->landinfo_data[line_nr]));
227 line_nr++;
229 /* Build date */
230 if (td.build_date != INVALID_DATE) {
231 SetDParam(0, td.build_date);
232 GetString(this->landinfo_data[line_nr], STR_LAND_AREA_INFORMATION_BUILD_DATE, lastof(this->landinfo_data[line_nr]));
233 line_nr++;
236 /* Station class */
237 if (td.station_class != STR_NULL) {
238 SetDParam(0, td.station_class);
239 GetString(this->landinfo_data[line_nr], STR_LAND_AREA_INFORMATION_STATION_CLASS, lastof(this->landinfo_data[line_nr]));
240 line_nr++;
243 /* Station type name */
244 if (td.station_name != STR_NULL) {
245 SetDParam(0, td.station_name);
246 GetString(this->landinfo_data[line_nr], STR_LAND_AREA_INFORMATION_STATION_TYPE, lastof(this->landinfo_data[line_nr]));
247 line_nr++;
250 /* Airport class */
251 if (td.airport_class != STR_NULL) {
252 SetDParam(0, td.airport_class);
253 GetString(this->landinfo_data[line_nr], STR_LAND_AREA_INFORMATION_AIRPORT_CLASS, lastof(this->landinfo_data[line_nr]));
254 line_nr++;
257 /* Airport name */
258 if (td.airport_name != STR_NULL) {
259 SetDParam(0, td.airport_name);
260 GetString(this->landinfo_data[line_nr], STR_LAND_AREA_INFORMATION_AIRPORT_NAME, lastof(this->landinfo_data[line_nr]));
261 line_nr++;
264 /* Airport tile name */
265 if (td.airport_tile_name != STR_NULL) {
266 SetDParam(0, td.airport_tile_name);
267 GetString(this->landinfo_data[line_nr], STR_LAND_AREA_INFORMATION_AIRPORTTILE_NAME, lastof(this->landinfo_data[line_nr]));
268 line_nr++;
271 /* Rail speed limit */
272 if (td.rail_speed != 0) {
273 SetDParam(0, td.rail_speed);
274 GetString(this->landinfo_data[line_nr], STR_LANG_AREA_INFORMATION_RAIL_SPEED_LIMIT, lastof(this->landinfo_data[line_nr]));
275 line_nr++;
278 /* NewGRF name */
279 if (td.grf != NULL) {
280 SetDParamStr(0, td.grf);
281 GetString(this->landinfo_data[line_nr], STR_LAND_AREA_INFORMATION_NEWGRF_NAME, lastof(this->landinfo_data[line_nr]));
282 line_nr++;
285 assert(line_nr < LAND_INFO_CENTERED_LINES);
287 /* Mark last line empty */
288 this->landinfo_data[line_nr][0] = '\0';
290 /* Cargo acceptance is displayed in a extra multiline */
291 char *strp = GetString(this->landinfo_data[LAND_INFO_MULTICENTER_LINE], STR_LAND_AREA_INFORMATION_CARGO_ACCEPTED, lastof(this->landinfo_data[LAND_INFO_MULTICENTER_LINE]));
292 bool found = false;
294 for (CargoID i = 0; i < NUM_CARGO; ++i) {
295 if (acceptance[i] > 0) {
296 /* Add a comma between each item. */
297 if (found) {
298 *strp++ = ',';
299 *strp++ = ' ';
301 found = true;
303 /* If the accepted value is less than 8, show it in 1/8:ths */
304 if (acceptance[i] < 8) {
305 SetDParam(0, acceptance[i]);
306 SetDParam(1, CargoSpec::Get(i)->name);
307 strp = GetString(strp, STR_LAND_AREA_INFORMATION_CARGO_EIGHTS, lastof(this->landinfo_data[LAND_INFO_MULTICENTER_LINE]));
308 } else {
309 strp = GetString(strp, CargoSpec::Get(i)->name, lastof(this->landinfo_data[LAND_INFO_MULTICENTER_LINE]));
313 if (!found) this->landinfo_data[LAND_INFO_MULTICENTER_LINE][0] = '\0';
316 virtual bool IsNewGRFInspectable() const
318 return ::IsNewGRFInspectable(GetGrfSpecFeature(this->tile), this->tile);
321 virtual void ShowNewGRFInspectWindow() const
323 ::ShowNewGRFInspectWindow(GetGrfSpecFeature(this->tile), this->tile);
327 * Some data on this window has become invalid.
328 * @param data Information about the changed data.
329 * @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.
331 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
333 if (!gui_scope) return;
334 switch (data) {
335 case 1:
336 /* ReInit, "debug" sprite might have changed */
337 this->ReInit();
338 break;
344 * Show land information window.
345 * @param tile The tile to show information about.
347 void ShowLandInfo(TileIndex tile)
349 DeleteWindowById(WC_LAND_INFO, 0);
350 new LandInfoWindow(tile);
353 static const NWidgetPart _nested_about_widgets[] = {
354 NWidget(NWID_HORIZONTAL),
355 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
356 NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_ABOUT_OPENTTD, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
357 EndContainer(),
358 NWidget(WWT_PANEL, COLOUR_GREY), SetPIP(4, 2, 4),
359 NWidget(WWT_LABEL, COLOUR_GREY), SetDataTip(STR_ABOUT_ORIGINAL_COPYRIGHT, STR_NULL),
360 NWidget(WWT_LABEL, COLOUR_GREY), SetDataTip(STR_ABOUT_VERSION, STR_NULL),
361 NWidget(WWT_FRAME, COLOUR_GREY), SetPadding(0, 5, 1, 5),
362 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_A_SCROLLING_TEXT),
363 EndContainer(),
364 NWidget(WWT_LABEL, COLOUR_GREY, WID_A_WEBSITE), SetDataTip(STR_BLACK_RAW_STRING, STR_NULL),
365 NWidget(WWT_LABEL, COLOUR_GREY), SetDataTip(STR_ABOUT_COPYRIGHT_OPENTTD, STR_NULL),
366 EndContainer(),
369 static WindowDesc _about_desc(
370 WDP_CENTER, NULL, 0, 0,
371 WC_GAME_OPTIONS, WC_NONE,
373 _nested_about_widgets, lengthof(_nested_about_widgets)
376 static const char * const _credits[] = {
377 "Original design by Chris Sawyer",
378 "Original graphics by Simon Foster",
380 "The OpenTTD team (in alphabetical order):",
381 " Albert Hofkamp (Alberth) - GUI expert",
382 " Jean-Fran\xC3\xA7ois Claeys (Belugas) - GUI, newindustries and more",
383 " Matthijs Kooijman (blathijs) - Pathfinder-guru, pool rework",
384 " Christoph Elsenhans (frosch) - General coding",
385 " Ulf Hermann (fonsinchen) - Cargo Distribution",
386 " Lo\xC3\xAF""c Guilloux (glx) - Windows Expert",
387 " Michael Lutz (michi_cc) - Path based signals",
388 " Owen Rudge (orudge) - Forum host, OS/2 port",
389 " Peter Nelson (peter1138) - Spiritual descendant from NewGRF gods",
390 " Ingo von Borstel (planetmaker) - Support",
391 " Remko Bijker (Rubidium) - Lead coder and way more",
392 " Zden\xC4\x9Bk Sojka (SmatZ) - Bug finder and fixer",
393 " Jos\xC3\xA9 Soler (Terkhen) - General coding",
394 " Thijs Marinussen (Yexo) - AI Framework",
395 " Leif Linse (Zuu) - AI/Game Script",
397 "Inactive Developers:",
398 " Bjarni Corfitzen (Bjarni) - MacOSX port, coder and vehicles",
399 " Victor Fischer (Celestar) - Programming everywhere you need him to",
400 " Tam\xC3\xA1s Farag\xC3\xB3 (Darkvater) - Ex-Lead coder",
401 " Jaroslav Mazanec (KUDr) - YAPG (Yet Another Pathfinder God) ;)",
402 " Jonathan Coome (Maedhros) - High priest of the NewGRF Temple",
403 " Attila B\xC3\xA1n (MiHaMiX) - Developer WebTranslator 1 and 2",
404 " Christoph Mallon (Tron) - Programmer, code correctness police",
406 "Retired Developers:",
407 " Ludvig Strigeus (ludde) - OpenTTD author, main coder (0.1 - 0.3.3)",
408 " Serge Paquet (vurlix) - Assistant project manager, coder (0.1 - 0.3.3)",
409 " Dominik Scherer (dominik81) - Lead programmer, GUI expert (0.3.0 - 0.3.6)",
410 " Benedikt Br\xC3\xBCggemeier (skidd13) - Bug fixer and code reworker",
411 " Patric Stout (TrueBrain) - NoProgrammer (0.3 - 1.2), sys op (active)",
413 "Special thanks go out to:",
414 " Josef Drexler - For his great work on TTDPatch",
415 " Marcin Grzegorczyk - Track foundations and for describing TTD internals",
416 " Petr Baudi\xC5\xA1 (pasky) - Many patches, newGRF support",
417 " Simon Sasburg (HackyKid) - Many bugfixes he has blessed us with",
418 " Stefan Mei\xC3\x9Fner (sign_de) - For his work on the console",
419 " Mike Ragsdale - OpenTTD installer",
420 " Cian Duffy (MYOB) - BeOS port / manual writing",
421 " Christian Rosentreter (tokai) - MorphOS / AmigaOS port",
422 " Richard Kempton (richK) - additional airports, initial TGP implementation",
424 " Alberto Demichelis - Squirrel scripting language \xC2\xA9 2003-2008",
425 " L. Peter Deutsch - MD5 implementation \xC2\xA9 1999, 2000, 2002",
426 " Michael Blunck - Pre-signals and semaphores \xC2\xA9 2003",
427 " George - Canal/Lock graphics \xC2\xA9 2003-2004",
428 " Andrew Parkhouse (andythenorth) - River graphics",
429 " David Dallaston (Pikka) - Tram tracks",
430 " All Translators - Who made OpenTTD a truly international game",
431 " Bug Reporters - Without whom OpenTTD would still be full of bugs!",
434 "And last but not least:",
435 " Chris Sawyer - For an amazing game!"
438 struct AboutWindow : public Window {
439 int text_position; ///< The top of the scrolling text
440 byte counter; ///< Used to scroll the text every 5 ticks
441 int line_height; ///< The height of a single line
442 static const int num_visible_lines = 19; ///< The number of lines visible simultaneously
444 AboutWindow() : Window(&_about_desc)
446 this->InitNested(WN_GAME_OPTIONS_ABOUT);
448 this->counter = 5;
449 this->text_position = this->GetWidget<NWidgetBase>(WID_A_SCROLLING_TEXT)->pos_y + this->GetWidget<NWidgetBase>(WID_A_SCROLLING_TEXT)->current_y;
452 virtual void SetStringParameters(int widget) const
454 if (widget == WID_A_WEBSITE) SetDParamStr(0, "Website: http://www.openttd.org");
457 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
459 if (widget != WID_A_SCROLLING_TEXT) return;
461 this->line_height = FONT_HEIGHT_NORMAL;
463 Dimension d;
464 d.height = this->line_height * num_visible_lines;
466 d.width = 0;
467 for (uint i = 0; i < lengthof(_credits); i++) {
468 d.width = max(d.width, GetStringBoundingBox(_credits[i]).width);
470 *size = maxdim(*size, d);
473 virtual void DrawWidget(const Rect &r, int widget) const
475 if (widget != WID_A_SCROLLING_TEXT) return;
477 int y = this->text_position;
479 /* Show all scrolling _credits */
480 for (uint i = 0; i < lengthof(_credits); i++) {
481 if (y >= r.top + 7 && y < r.bottom - this->line_height) {
482 DrawString(r.left, r.right, y, _credits[i], TC_BLACK, SA_LEFT | SA_FORCE);
484 y += this->line_height;
488 virtual void OnTick()
490 if (--this->counter == 0) {
491 this->counter = 5;
492 this->text_position--;
493 /* If the last text has scrolled start a new from the start */
494 if (this->text_position < (int)(this->GetWidget<NWidgetBase>(WID_A_SCROLLING_TEXT)->pos_y - lengthof(_credits) * this->line_height)) {
495 this->text_position = this->GetWidget<NWidgetBase>(WID_A_SCROLLING_TEXT)->pos_y + this->GetWidget<NWidgetBase>(WID_A_SCROLLING_TEXT)->current_y;
497 this->SetDirty();
502 void ShowAboutWindow()
504 DeleteWindowByClass(WC_GAME_OPTIONS);
505 new AboutWindow();
509 * Display estimated costs.
510 * @param cost Estimated cost (or income if negative).
511 * @param x X position of the notification window.
512 * @param y Y position of the notification window.
514 void ShowEstimatedCostOrIncome(Money cost, int x, int y)
516 StringID msg = STR_MESSAGE_ESTIMATED_COST;
518 if (cost < 0) {
519 cost = -cost;
520 msg = STR_MESSAGE_ESTIMATED_INCOME;
522 SetDParam(0, cost);
523 ShowErrorMessage(msg, INVALID_STRING_ID, WL_INFO, x, y);
527 * Display animated income or costs on the map.
528 * @param x World X position of the animation location.
529 * @param y World Y position of the animation location.
530 * @param z World Z position of the animation location.
531 * @param cost Estimated cost (or income if negative).
533 void ShowCostOrIncomeAnimation(int x, int y, int z, Money cost)
535 Point pt = RemapCoords(x, y, z);
536 StringID msg = STR_INCOME_FLOAT_COST;
538 if (cost < 0) {
539 cost = -cost;
540 msg = STR_INCOME_FLOAT_INCOME;
542 SetDParam(0, cost);
543 AddTextEffect(msg, pt.x, pt.y, DAY_TICKS, TE_RISING);
547 * Display animated feeder income.
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 transfer Estimated feeder income.
552 * @param income Real income from goods being delivered to their final destination.
554 void ShowFeederIncomeAnimation(int x, int y, int z, Money transfer, Money income)
556 Point pt = RemapCoords(x, y, z);
558 SetDParam(0, transfer);
559 if (income == 0) {
560 AddTextEffect(STR_FEEDER, pt.x, pt.y, DAY_TICKS, TE_RISING);
561 } else {
562 StringID msg = STR_FEEDER_COST;
563 if (income < 0) {
564 income = -income;
565 msg = STR_FEEDER_INCOME;
567 SetDParam(1, income);
568 AddTextEffect(msg, pt.x, pt.y, DAY_TICKS, TE_RISING);
573 * Display vehicle loading indicators.
574 * @param x World X position of the animation location.
575 * @param y World Y position of the animation location.
576 * @param z World Z position of the animation location.
577 * @param percent Estimated feeder income.
578 * @param string String which is drawn on the map.
579 * @return TextEffectID to be used for future updates of the loading indicators.
581 TextEffectID ShowFillingPercent(int x, int y, int z, uint8 percent, StringID string)
583 Point pt = RemapCoords(x, y, z);
585 assert(string != STR_NULL);
587 SetDParam(0, percent);
588 return AddTextEffect(string, pt.x, pt.y, 0, TE_STATIC);
592 * Update vehicle loading indicators.
593 * @param te_id TextEffectID to be updated.
594 * @param string String wich is printed.
596 void UpdateFillingPercent(TextEffectID te_id, uint8 percent, StringID string)
598 assert(string != STR_NULL);
600 SetDParam(0, percent);
601 UpdateTextEffect(te_id, string);
605 * Hide vehicle loading indicators.
606 * @param *te_id TextEffectID which is supposed to be hidden.
608 void HideFillingPercent(TextEffectID *te_id)
610 if (*te_id == INVALID_TE_ID) return;
612 RemoveTextEffect(*te_id);
613 *te_id = INVALID_TE_ID;
616 static const NWidgetPart _nested_tooltips_widgets[] = {
617 NWidget(WWT_PANEL, COLOUR_GREY, WID_TT_BACKGROUND), SetMinimalSize(200, 32), EndContainer(),
620 static WindowDesc _tool_tips_desc(
621 WDP_MANUAL, NULL, 0, 0, // Coordinates and sizes are not used,
622 WC_TOOLTIPS, WC_NONE,
624 _nested_tooltips_widgets, lengthof(_nested_tooltips_widgets)
627 /** Window for displaying a tooltip. */
628 struct TooltipsWindow : public Window
630 StringID string_id; ///< String to display as tooltip.
631 byte paramcount; ///< Number of string parameters in #string_id.
632 uint64 params[5]; ///< The string parameters.
633 TooltipCloseCondition close_cond; ///< Condition for closing the window.
635 TooltipsWindow(Window *parent, StringID str, uint paramcount, const uint64 params[], TooltipCloseCondition close_tooltip) : Window(&_tool_tips_desc)
637 this->parent = parent;
638 this->string_id = str;
639 assert_compile(sizeof(this->params[0]) == sizeof(params[0]));
640 assert(paramcount <= lengthof(this->params));
641 memcpy(this->params, params, sizeof(this->params[0]) * paramcount);
642 this->paramcount = paramcount;
643 this->close_cond = close_tooltip;
645 this->InitNested();
647 CLRBITS(this->flags, WF_WHITE_BORDER);
650 virtual Point OnInitialPosition(int16 sm_width, int16 sm_height, int window_number)
652 /* Find the free screen space between the main toolbar at the top, and the statusbar at the bottom.
653 * Add a fixed distance 2 so the tooltip floats free from both bars.
655 int scr_top = GetMainViewTop() + 2;
656 int scr_bot = GetMainViewBottom() - 2;
658 Point pt;
660 /* Correctly position the tooltip position, watch out for window and cursor size
661 * Clamp value to below main toolbar and above statusbar. If tooltip would
662 * go below window, flip it so it is shown above the cursor */
663 pt.y = Clamp(_cursor.pos.y + _cursor.size.y + _cursor.offs.y + 5, scr_top, scr_bot);
664 if (pt.y + sm_height > scr_bot) pt.y = min(_cursor.pos.y + _cursor.offs.y - 5, scr_bot) - sm_height;
665 pt.x = sm_width >= _screen.width ? 0 : Clamp(_cursor.pos.x - (sm_width >> 1), 0, _screen.width - sm_width);
667 return pt;
670 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
672 /* There is only one widget. */
673 for (uint i = 0; i != this->paramcount; i++) SetDParam(i, this->params[i]);
675 size->width = min(GetStringBoundingBox(this->string_id).width, 194);
676 size->height = GetStringHeight(this->string_id, size->width);
678 /* Increase slightly to have some space around the box. */
679 size->width += 2 + WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
680 size->height += 2 + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
683 virtual void DrawWidget(const Rect &r, int widget) const
685 /* There is only one widget. */
686 GfxFillRect(r.left, r.top, r.right, r.bottom, PC_BLACK);
687 GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, PC_LIGHT_YELLOW);
689 for (uint arg = 0; arg < this->paramcount; arg++) {
690 SetDParam(arg, this->params[arg]);
692 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);
695 virtual void OnMouseLoop()
697 /* Always close tooltips when the cursor is not in our window. */
698 if (!_cursor.in_window) {
699 delete this;
700 return;
703 /* We can show tooltips while dragging tools. These are shown as long as
704 * we are dragging the tool. Normal tooltips work with hover or rmb. */
705 switch (this->close_cond) {
706 case TCC_RIGHT_CLICK: if (!_right_button_down) delete this; break;
707 case TCC_LEFT_CLICK: if (!_left_button_down) delete this; break;
708 case TCC_HOVER: if (!_mouse_hovering) delete this; break;
714 * Shows a tooltip
715 * @param parent The window this tooltip is related to.
716 * @param str String to be displayed
717 * @param paramcount number of params to deal with
718 * @param params (optional) up to 5 pieces of additional information that may be added to a tooltip
719 * @param use_left_mouse_button close the tooltip when the left (true) or right (false) mouse button is released
721 void GuiShowTooltips(Window *parent, StringID str, uint paramcount, const uint64 params[], TooltipCloseCondition close_tooltip)
723 DeleteWindowById(WC_TOOLTIPS, 0);
725 if (str == STR_NULL) return;
727 new TooltipsWindow(parent, str, paramcount, params, close_tooltip);
730 void QueryString::HandleEditBox(Window *w, int wid)
732 if (w->IsWidgetGloballyFocused(wid) && this->text.HandleCaret()) {
733 w->SetWidgetDirty(wid);
735 /* For the OSK also invalidate the parent window */
736 if (w->window_class == WC_OSK) w->InvalidateData();
740 void QueryString::DrawEditBox(const Window *w, int wid) const
742 const NWidgetLeaf *wi = w->GetWidget<NWidgetLeaf>(wid);
744 assert((wi->type & WWT_MASK) == WWT_EDITBOX);
746 bool rtl = _current_text_dir == TD_RTL;
747 Dimension sprite_size = GetSpriteSize(rtl ? SPR_IMG_DELETE_RIGHT : SPR_IMG_DELETE_LEFT);
748 int clearbtn_width = sprite_size.width + WD_IMGBTN_LEFT + WD_IMGBTN_RIGHT;
750 int clearbtn_left = wi->pos_x + (rtl ? 0 : wi->current_x - clearbtn_width);
751 int clearbtn_right = wi->pos_x + (rtl ? clearbtn_width : wi->current_x) - 1;
752 int left = wi->pos_x + (rtl ? clearbtn_width : 0);
753 int right = wi->pos_x + (rtl ? wi->current_x : wi->current_x - clearbtn_width) - 1;
755 int top = wi->pos_y;
756 int bottom = wi->pos_y + wi->current_y - 1;
758 DrawFrameRect(clearbtn_left, top, clearbtn_right, bottom, wi->colour, wi->IsLowered() ? FR_LOWERED : FR_NONE);
759 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));
760 if (this->text.bytes == 1) GfxFillRect(clearbtn_left + 1, top + 1, clearbtn_right - 1, bottom - 1, _colour_gradient[wi->colour & 0xF][2], FILLRECT_CHECKER);
762 DrawFrameRect(left, top, right, bottom, wi->colour, FR_LOWERED | FR_DARKENED);
763 GfxFillRect(left + 1, top + 1, right - 1, bottom - 1, PC_BLACK);
765 /* Limit the drawing of the string inside the widget boundaries */
766 DrawPixelInfo dpi;
767 if (!FillDrawPixelInfo(&dpi, left + WD_FRAMERECT_LEFT, top + WD_FRAMERECT_TOP, right - left - WD_FRAMERECT_RIGHT, bottom - top - WD_FRAMERECT_BOTTOM)) return;
769 DrawPixelInfo *old_dpi = _cur_dpi;
770 _cur_dpi = &dpi;
772 /* We will take the current widget length as maximum width, with a small
773 * space reserved at the end for the caret to show */
774 const Textbuf *tb = &this->text;
775 int delta = min(0, (right - left) - tb->pixels - 10);
777 if (tb->caretxoffs + delta < 0) delta = -tb->caretxoffs;
779 /* If we have a marked area, draw a background highlight. */
780 if (tb->marklength != 0) GfxFillRect(delta + tb->markxoffs, 0, delta + tb->markxoffs + tb->marklength - 1, bottom - top, PC_GREY);
782 DrawString(delta, tb->pixels, 0, tb->buf, TC_YELLOW);
783 bool focussed = w->IsWidgetGloballyFocused(wid) || IsOSKOpenedFor(w, wid);
784 if (focussed && tb->caret) {
785 int caret_width = GetStringBoundingBox("_").width;
786 DrawString(tb->caretxoffs + delta, tb->caretxoffs + delta + caret_width, 0, "_", TC_WHITE);
789 _cur_dpi = old_dpi;
793 * Get the current caret position.
794 * @param w Window the edit box is in.
795 * @param wid Widget index.
796 * @return Top-left location of the caret, relative to the window.
798 Point QueryString::GetCaretPosition(const Window *w, int wid) const
800 const NWidgetLeaf *wi = w->GetWidget<NWidgetLeaf>(wid);
802 assert((wi->type & WWT_MASK) == WWT_EDITBOX);
804 bool rtl = _current_text_dir == TD_RTL;
805 Dimension sprite_size = GetSpriteSize(rtl ? SPR_IMG_DELETE_RIGHT : SPR_IMG_DELETE_LEFT);
806 int clearbtn_width = sprite_size.width + WD_IMGBTN_LEFT + WD_IMGBTN_RIGHT;
808 int left = wi->pos_x + (rtl ? clearbtn_width : 0);
809 int right = wi->pos_x + (rtl ? wi->current_x : wi->current_x - clearbtn_width) - 1;
811 /* Clamp caret position to be inside out current width. */
812 const Textbuf *tb = &this->text;
813 int delta = min(0, (right - left) - tb->pixels - 10);
814 if (tb->caretxoffs + delta < 0) delta = -tb->caretxoffs;
816 Point pt = {left + WD_FRAMERECT_LEFT + tb->caretxoffs + delta, wi->pos_y + WD_FRAMERECT_TOP};
817 return pt;
821 * Get the bounding rectangle for a range of the query string.
822 * @param w Window the edit box is in.
823 * @param wid Widget index.
824 * @param from Start of the string range.
825 * @param to End of the string range.
826 * @return Rectangle encompassing the string range, relative to the window.
828 Rect QueryString::GetBoundingRect(const Window *w, int wid, const char *from, const char *to) const
830 const NWidgetLeaf *wi = w->GetWidget<NWidgetLeaf>(wid);
832 assert((wi->type & WWT_MASK) == WWT_EDITBOX);
834 bool rtl = _current_text_dir == TD_RTL;
835 Dimension sprite_size = GetSpriteSize(rtl ? SPR_IMG_DELETE_RIGHT : SPR_IMG_DELETE_LEFT);
836 int clearbtn_width = sprite_size.width + WD_IMGBTN_LEFT + WD_IMGBTN_RIGHT;
838 int left = wi->pos_x + (rtl ? clearbtn_width : 0);
839 int right = wi->pos_x + (rtl ? wi->current_x : wi->current_x - clearbtn_width) - 1;
841 int top = wi->pos_y + WD_FRAMERECT_TOP;
842 int bottom = wi->pos_y + wi->current_y - 1 - WD_FRAMERECT_BOTTOM;
844 /* Clamp caret position to be inside our current width. */
845 const Textbuf *tb = &this->text;
846 int delta = min(0, (right - left) - tb->pixels - 10);
847 if (tb->caretxoffs + delta < 0) delta = -tb->caretxoffs;
849 /* Get location of first and last character. */
850 Point p1 = GetCharPosInString(tb->buf, from, FS_NORMAL);
851 Point p2 = from != to ? GetCharPosInString(tb->buf, to, FS_NORMAL) : p1;
853 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 };
855 return r;
859 * Get the character that is rendered at a position.
860 * @param w Window the edit box is in.
861 * @param wid Widget index.
862 * @param pt Position to test.
863 * @return Pointer to the character at the position or NULL if no character is at the position.
865 const char *QueryString::GetCharAtPosition(const Window *w, int wid, const Point &pt) const
867 const NWidgetLeaf *wi = w->GetWidget<NWidgetLeaf>(wid);
869 assert((wi->type & WWT_MASK) == WWT_EDITBOX);
871 bool rtl = _current_text_dir == TD_RTL;
872 Dimension sprite_size = GetSpriteSize(rtl ? SPR_IMG_DELETE_RIGHT : SPR_IMG_DELETE_LEFT);
873 int clearbtn_width = sprite_size.width + WD_IMGBTN_LEFT + WD_IMGBTN_RIGHT;
875 int left = wi->pos_x + (rtl ? clearbtn_width : 0);
876 int right = wi->pos_x + (rtl ? wi->current_x : wi->current_x - clearbtn_width) - 1;
878 int top = wi->pos_y + WD_FRAMERECT_TOP;
879 int bottom = wi->pos_y + wi->current_y - 1 - WD_FRAMERECT_BOTTOM;
881 if (!IsInsideMM(pt.y, top, bottom)) return NULL;
883 /* Clamp caret position to be inside our current width. */
884 const Textbuf *tb = &this->text;
885 int delta = min(0, (right - left) - tb->pixels - 10);
886 if (tb->caretxoffs + delta < 0) delta = -tb->caretxoffs;
888 return ::GetCharAtPosition(tb->buf, pt.x - delta - left);
891 void QueryString::ClickEditBox(Window *w, Point pt, int wid, int click_count, bool focus_changed)
893 const NWidgetLeaf *wi = w->GetWidget<NWidgetLeaf>(wid);
895 assert((wi->type & WWT_MASK) == WWT_EDITBOX);
897 bool rtl = _current_text_dir == TD_RTL;
898 int clearbtn_width = GetSpriteSize(rtl ? SPR_IMG_DELETE_RIGHT : SPR_IMG_DELETE_LEFT).width;
900 int clearbtn_left = wi->pos_x + (rtl ? 0 : wi->current_x - clearbtn_width);
902 if (IsInsideBS(pt.x, clearbtn_left, clearbtn_width)) {
903 if (this->text.bytes > 1) {
904 this->text.DeleteAll();
905 w->HandleButtonClick(wid);
906 w->OnEditboxChanged(wid);
908 return;
911 if (w->window_class != WC_OSK && _settings_client.gui.osk_activation != OSKA_DISABLED &&
912 (!focus_changed || _settings_client.gui.osk_activation == OSKA_IMMEDIATELY) &&
913 (click_count == 2 || _settings_client.gui.osk_activation != OSKA_DOUBLE_CLICK)) {
914 /* Open the OSK window */
915 ShowOnScreenKeyboard(w, wid);
919 /** Class for the string query window. */
920 struct QueryStringWindow : public Window
922 QueryString editbox; ///< Editbox.
923 QueryStringFlags flags; ///< Flags controlling behaviour of the window.
925 QueryStringWindow(StringID str, StringID caption, uint max_bytes, uint max_chars, WindowDesc *desc, Window *parent, CharSetFilter afilter, QueryStringFlags flags) :
926 Window(desc), editbox(max_bytes, max_chars)
928 char *last_of = &this->editbox.text.buf[this->editbox.text.max_bytes - 1];
929 GetString(this->editbox.text.buf, str, last_of);
930 str_validate(this->editbox.text.buf, last_of, SVS_NONE);
932 /* Make sure the name isn't too long for the text buffer in the number of
933 * characters (not bytes). max_chars also counts the '\0' characters. */
934 while (Utf8StringLength(this->editbox.text.buf) + 1 > this->editbox.text.max_chars) {
935 *Utf8PrevChar(this->editbox.text.buf + strlen(this->editbox.text.buf)) = '\0';
938 this->editbox.text.UpdateSize();
940 if ((flags & QSF_ACCEPT_UNCHANGED) == 0) this->editbox.orig = strdup(this->editbox.text.buf);
942 this->querystrings[WID_QS_TEXT] = &this->editbox;
943 this->editbox.caption = caption;
944 this->editbox.cancel_button = WID_QS_CANCEL;
945 this->editbox.ok_button = WID_QS_OK;
946 this->editbox.text.afilter = afilter;
947 this->flags = flags;
949 this->InitNested(WN_QUERY_STRING);
951 this->parent = parent;
953 this->SetFocusedWidget(WID_QS_TEXT);
956 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
958 if (widget == WID_QS_DEFAULT && (this->flags & QSF_ENABLE_DEFAULT) == 0) {
959 /* We don't want this widget to show! */
960 fill->width = 0;
961 resize->width = 0;
962 size->width = 0;
966 virtual void SetStringParameters(int widget) const
968 if (widget == WID_QS_CAPTION) SetDParam(0, this->editbox.caption);
971 void OnOk()
973 if (this->editbox.orig == NULL || strcmp(this->editbox.text.buf, this->editbox.orig) != 0) {
974 /* If the parent is NULL, the editbox is handled by general function
975 * HandleOnEditText */
976 if (this->parent != NULL) {
977 this->parent->OnQueryTextFinished(this->editbox.text.buf);
978 } else {
979 HandleOnEditText(this->editbox.text.buf);
981 this->editbox.handled = true;
985 virtual void OnClick(Point pt, int widget, int click_count)
987 switch (widget) {
988 case WID_QS_DEFAULT:
989 this->editbox.text.DeleteAll();
990 /* FALL THROUGH */
991 case WID_QS_OK:
992 this->OnOk();
993 /* FALL THROUGH */
994 case WID_QS_CANCEL:
995 delete this;
996 break;
1000 ~QueryStringWindow()
1002 if (!this->editbox.handled && this->parent != NULL) {
1003 Window *parent = this->parent;
1004 this->parent = NULL; // so parent doesn't try to delete us again
1005 parent->OnQueryTextFinished(NULL);
1010 static const NWidgetPart _nested_query_string_widgets[] = {
1011 NWidget(NWID_HORIZONTAL),
1012 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
1013 NWidget(WWT_CAPTION, COLOUR_GREY, WID_QS_CAPTION), SetDataTip(STR_WHITE_STRING, STR_NULL),
1014 EndContainer(),
1015 NWidget(WWT_PANEL, COLOUR_GREY),
1016 NWidget(WWT_EDITBOX, COLOUR_GREY, WID_QS_TEXT), SetMinimalSize(256, 12), SetFill(1, 1), SetPadding(2, 2, 2, 2),
1017 EndContainer(),
1018 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
1019 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_QS_DEFAULT), SetMinimalSize(87, 12), SetFill(1, 1), SetDataTip(STR_BUTTON_DEFAULT, STR_NULL),
1020 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_QS_CANCEL), SetMinimalSize(86, 12), SetFill(1, 1), SetDataTip(STR_BUTTON_CANCEL, STR_NULL),
1021 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_QS_OK), SetMinimalSize(87, 12), SetFill(1, 1), SetDataTip(STR_BUTTON_OK, STR_NULL),
1022 EndContainer(),
1025 static WindowDesc _query_string_desc(
1026 WDP_CENTER, "query_string", 0, 0,
1027 WC_QUERY_STRING, WC_NONE,
1029 _nested_query_string_widgets, lengthof(_nested_query_string_widgets)
1033 * Show a query popup window with a textbox in it.
1034 * @param str StringID for the text shown in the textbox
1035 * @param caption StringID of text shown in caption of querywindow
1036 * @param maxsize maximum size in bytes or characters (including terminating '\0') depending on flags
1037 * @param parent pointer to a Window that will handle the events (ok/cancel) of this
1038 * window. If NULL, results are handled by global function HandleOnEditText
1039 * @param afilter filters out unwanted character input
1040 * @param flags various flags, @see QueryStringFlags
1042 void ShowQueryString(StringID str, StringID caption, uint maxsize, Window *parent, CharSetFilter afilter, QueryStringFlags flags)
1044 DeleteWindowByClass(WC_QUERY_STRING);
1045 new QueryStringWindow(str, caption, ((flags & QSF_LEN_IN_CHARS) ? MAX_CHAR_LENGTH : 1) * maxsize, maxsize, &_query_string_desc, parent, afilter, flags);
1049 * Window used for asking the user a YES/NO question.
1051 struct QueryWindow : public Window {
1052 QueryCallbackProc *proc; ///< callback function executed on closing of popup. Window* points to parent, bool is true if 'yes' clicked, false otherwise
1053 uint64 params[10]; ///< local copy of _decode_parameters
1054 StringID message; ///< message shown for query window
1055 StringID caption; ///< title of window
1057 QueryWindow(WindowDesc *desc, StringID caption, StringID message, Window *parent, QueryCallbackProc *callback) : Window(desc)
1059 /* Create a backup of the variadic arguments to strings because it will be
1060 * overridden pretty often. We will copy these back for drawing */
1061 CopyOutDParam(this->params, 0, lengthof(this->params));
1062 this->caption = caption;
1063 this->message = message;
1064 this->proc = callback;
1066 this->InitNested(WN_CONFIRM_POPUP_QUERY);
1068 this->parent = parent;
1069 this->left = parent->left + (parent->width / 2) - (this->width / 2);
1070 this->top = parent->top + (parent->height / 2) - (this->height / 2);
1073 ~QueryWindow()
1075 if (this->proc != NULL) this->proc(this->parent, false);
1078 virtual void SetStringParameters(int widget) const
1080 switch (widget) {
1081 case WID_Q_CAPTION:
1082 CopyInDParam(1, this->params, lengthof(this->params));
1083 SetDParam(0, this->caption);
1084 break;
1086 case WID_Q_TEXT:
1087 CopyInDParam(0, this->params, lengthof(this->params));
1088 break;
1092 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
1094 if (widget != WID_Q_TEXT) return;
1096 Dimension d = GetStringMultiLineBoundingBox(this->message, *size);
1097 d.width += WD_FRAMETEXT_LEFT + WD_FRAMETEXT_RIGHT;
1098 d.height += WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
1099 *size = d;
1102 virtual void DrawWidget(const Rect &r, int widget) const
1104 if (widget != WID_Q_TEXT) return;
1106 DrawStringMultiLine(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, r.top + WD_FRAMERECT_TOP, r.bottom - WD_FRAMERECT_BOTTOM,
1107 this->message, TC_FROMSTRING, SA_CENTER);
1110 virtual void OnClick(Point pt, int widget, int click_count)
1112 switch (widget) {
1113 case WID_Q_YES: {
1114 /* in the Generate New World window, clicking 'Yes' causes
1115 * DeleteNonVitalWindows() to be called - we shouldn't be in a window then */
1116 QueryCallbackProc *proc = this->proc;
1117 Window *parent = this->parent;
1118 /* Prevent the destructor calling the callback function */
1119 this->proc = NULL;
1120 delete this;
1121 if (proc != NULL) {
1122 proc(parent, true);
1123 proc = NULL;
1125 break;
1127 case WID_Q_NO:
1128 delete this;
1129 break;
1133 virtual EventState OnKeyPress(WChar key, uint16 keycode)
1135 /* ESC closes the window, Enter confirms the action */
1136 switch (keycode) {
1137 case WKC_RETURN:
1138 case WKC_NUM_ENTER:
1139 if (this->proc != NULL) {
1140 this->proc(this->parent, true);
1141 this->proc = NULL;
1143 /* FALL THROUGH */
1144 case WKC_ESC:
1145 delete this;
1146 return ES_HANDLED;
1148 return ES_NOT_HANDLED;
1152 static const NWidgetPart _nested_query_widgets[] = {
1153 NWidget(NWID_HORIZONTAL),
1154 NWidget(WWT_CLOSEBOX, COLOUR_RED),
1155 NWidget(WWT_CAPTION, COLOUR_RED, WID_Q_CAPTION), SetDataTip(STR_JUST_STRING, STR_NULL),
1156 EndContainer(),
1157 NWidget(WWT_PANEL, COLOUR_RED), SetPIP(8, 15, 8),
1158 NWidget(WWT_TEXT, COLOUR_RED, WID_Q_TEXT), SetMinimalSize(200, 12),
1159 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(20, 29, 20),
1160 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_Q_NO), SetMinimalSize(71, 12), SetDataTip(STR_QUIT_NO, STR_NULL),
1161 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_Q_YES), SetMinimalSize(71, 12), SetDataTip(STR_QUIT_YES, STR_NULL),
1162 EndContainer(),
1163 EndContainer(),
1166 static WindowDesc _query_desc(
1167 WDP_CENTER, NULL, 0, 0,
1168 WC_CONFIRM_POPUP_QUERY, WC_NONE,
1169 WDF_MODAL,
1170 _nested_query_widgets, lengthof(_nested_query_widgets)
1174 * Show a modal confirmation window with standard 'yes' and 'no' buttons
1175 * The window is aligned to the centre of its parent.
1176 * @param caption string shown as window caption
1177 * @param message string that will be shown for the window
1178 * @param parent pointer to parent window, if this pointer is NULL the parent becomes
1179 * the main window WC_MAIN_WINDOW
1180 * @param callback callback function pointer to set in the window descriptor
1182 void ShowQuery(StringID caption, StringID message, Window *parent, QueryCallbackProc *callback)
1184 if (parent == NULL) parent = FindWindowById(WC_MAIN_WINDOW, 0);
1186 const Window *w;
1187 FOR_ALL_WINDOWS_FROM_BACK(w) {
1188 if (w->window_class != WC_CONFIRM_POPUP_QUERY) continue;
1190 const QueryWindow *qw = (const QueryWindow *)w;
1191 if (qw->parent != parent || qw->proc != callback) continue;
1193 delete qw;
1194 break;
1197 new QueryWindow(&_query_desc, caption, message, parent, callback);