Rearrange conditionals in FreeTrainTrackReservation
[openttd/fttd.git] / src / misc_gui.cpp
blobba74c80c8a38d2b1b6088340d590455b5382afa0
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 = 32, ///< Up to 32 centered lines (arbitrary limit)
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 = LocalAuthorityTownFromTile(tile);
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;
163 td.road_speed = 0;
165 td.grf = NULL;
167 CargoArray acceptance;
168 AddAcceptedCargo(tile, acceptance, NULL);
169 GetTileDesc(tile, &td);
171 uint line_nr = 0;
173 /* Tiletype */
174 SetDParam(0, td.dparam[0]);
175 GetString(this->landinfo_data[line_nr], td.str, lastof(this->landinfo_data[line_nr]));
176 line_nr++;
178 /* Up to four owners */
179 for (uint i = 0; i < 4; i++) {
180 if (td.owner_type[i] == STR_NULL) continue;
182 SetDParam(0, STR_LAND_AREA_INFORMATION_OWNER_N_A);
183 if (td.owner[i] != OWNER_NONE && td.owner[i] != OWNER_WATER) GetNameOfOwner(td.owner[i], tile);
184 GetString(this->landinfo_data[line_nr], td.owner_type[i], lastof(this->landinfo_data[line_nr]));
185 line_nr++;
188 /* Cost to clear/revenue when cleared */
189 StringID str = STR_LAND_AREA_INFORMATION_COST_TO_CLEAR_N_A;
190 Company *c = Company::GetIfValid(_local_company);
191 if (c != NULL) {
192 Money old_money = c->money;
193 c->money = INT64_MAX;
194 assert(_current_company == _local_company);
195 CommandCost costclear = DoCommand(tile, 0, 0, DC_NONE, CMD_LANDSCAPE_CLEAR);
196 c->money = old_money;
197 if (costclear.Succeeded()) {
198 Money cost = costclear.GetCost();
199 if (cost < 0) {
200 cost = -cost; // Negate negative cost to a positive revenue
201 str = STR_LAND_AREA_INFORMATION_REVENUE_WHEN_CLEARED;
202 } else {
203 str = STR_LAND_AREA_INFORMATION_COST_TO_CLEAR;
205 SetDParam(0, cost);
208 GetString(this->landinfo_data[line_nr], str, lastof(this->landinfo_data[line_nr]));
209 line_nr++;
211 /* Location */
212 char tmp[16];
213 snprintf(tmp, lengthof(tmp), "0x%.4X", tile);
214 SetDParam(0, TileX(tile));
215 SetDParam(1, TileY(tile));
216 SetDParam(2, GetTileZ(tile));
217 SetDParamStr(3, tmp);
218 GetString(this->landinfo_data[line_nr], STR_LAND_AREA_INFORMATION_LANDINFO_COORDS, lastof(this->landinfo_data[line_nr]));
219 line_nr++;
221 /* Local authority */
222 SetDParam(0, STR_LAND_AREA_INFORMATION_LOCAL_AUTHORITY_NONE);
223 if (t != NULL) {
224 SetDParam(0, STR_TOWN_NAME);
225 SetDParam(1, t->index);
227 GetString(this->landinfo_data[line_nr], STR_LAND_AREA_INFORMATION_LOCAL_AUTHORITY, lastof(this->landinfo_data[line_nr]));
228 line_nr++;
230 /* Build date */
231 if (td.build_date != INVALID_DATE) {
232 SetDParam(0, td.build_date);
233 GetString(this->landinfo_data[line_nr], STR_LAND_AREA_INFORMATION_BUILD_DATE, lastof(this->landinfo_data[line_nr]));
234 line_nr++;
237 /* Station class */
238 if (td.station_class != STR_NULL) {
239 SetDParam(0, td.station_class);
240 GetString(this->landinfo_data[line_nr], STR_LAND_AREA_INFORMATION_STATION_CLASS, lastof(this->landinfo_data[line_nr]));
241 line_nr++;
244 /* Station type name */
245 if (td.station_name != STR_NULL) {
246 SetDParam(0, td.station_name);
247 GetString(this->landinfo_data[line_nr], STR_LAND_AREA_INFORMATION_STATION_TYPE, lastof(this->landinfo_data[line_nr]));
248 line_nr++;
251 /* Airport class */
252 if (td.airport_class != STR_NULL) {
253 SetDParam(0, td.airport_class);
254 GetString(this->landinfo_data[line_nr], STR_LAND_AREA_INFORMATION_AIRPORT_CLASS, lastof(this->landinfo_data[line_nr]));
255 line_nr++;
258 /* Airport name */
259 if (td.airport_name != STR_NULL) {
260 SetDParam(0, td.airport_name);
261 GetString(this->landinfo_data[line_nr], STR_LAND_AREA_INFORMATION_AIRPORT_NAME, lastof(this->landinfo_data[line_nr]));
262 line_nr++;
265 /* Airport tile name */
266 if (td.airport_tile_name != STR_NULL) {
267 SetDParam(0, td.airport_tile_name);
268 GetString(this->landinfo_data[line_nr], STR_LAND_AREA_INFORMATION_AIRPORTTILE_NAME, lastof(this->landinfo_data[line_nr]));
269 line_nr++;
272 /* Rail speed limit */
273 if (td.rail_speed != 0) {
274 SetDParam(0, td.rail_speed);
275 GetString(this->landinfo_data[line_nr], STR_LANG_AREA_INFORMATION_RAIL_SPEED_LIMIT, lastof(this->landinfo_data[line_nr]));
276 line_nr++;
279 /* Road speed limit */
280 if (td.road_speed != 0) {
281 SetDParam(0, td.road_speed);
282 GetString(this->landinfo_data[line_nr], STR_LANG_AREA_INFORMATION_ROAD_SPEED_LIMIT, lastof(this->landinfo_data[line_nr]));
283 line_nr++;
286 /* NewGRF name */
287 if (td.grf != NULL) {
288 SetDParamStr(0, td.grf);
289 GetString(this->landinfo_data[line_nr], STR_LAND_AREA_INFORMATION_NEWGRF_NAME, lastof(this->landinfo_data[line_nr]));
290 line_nr++;
293 assert(line_nr < LAND_INFO_CENTERED_LINES);
295 /* Mark last line empty */
296 this->landinfo_data[line_nr][0] = '\0';
298 /* Cargo acceptance is displayed in a extra multiline */
299 char *strp = GetString(this->landinfo_data[LAND_INFO_MULTICENTER_LINE], STR_LAND_AREA_INFORMATION_CARGO_ACCEPTED, lastof(this->landinfo_data[LAND_INFO_MULTICENTER_LINE]));
300 bool found = false;
302 for (CargoID i = 0; i < NUM_CARGO; ++i) {
303 if (acceptance[i] > 0) {
304 /* Add a comma between each item. */
305 if (found) {
306 *strp++ = ',';
307 *strp++ = ' ';
309 found = true;
311 /* If the accepted value is less than 8, show it in 1/8:ths */
312 if (acceptance[i] < 8) {
313 SetDParam(0, acceptance[i]);
314 SetDParam(1, CargoSpec::Get(i)->name);
315 strp = GetString(strp, STR_LAND_AREA_INFORMATION_CARGO_EIGHTS, lastof(this->landinfo_data[LAND_INFO_MULTICENTER_LINE]));
316 } else {
317 strp = GetString(strp, CargoSpec::Get(i)->name, lastof(this->landinfo_data[LAND_INFO_MULTICENTER_LINE]));
321 if (!found) this->landinfo_data[LAND_INFO_MULTICENTER_LINE][0] = '\0';
324 virtual bool IsNewGRFInspectable() const
326 return ::IsNewGRFInspectable(GetGrfSpecFeature(this->tile), this->tile);
329 virtual void ShowNewGRFInspectWindow() const
331 ::ShowNewGRFInspectWindow(GetGrfSpecFeature(this->tile), this->tile);
335 * Some data on this window has become invalid.
336 * @param data Information about the changed data.
337 * @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.
339 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
341 if (!gui_scope) return;
342 switch (data) {
343 case 1:
344 /* ReInit, "debug" sprite might have changed */
345 this->ReInit();
346 break;
352 * Show land information window.
353 * @param tile The tile to show information about.
355 void ShowLandInfo(TileIndex tile)
357 DeleteWindowById(WC_LAND_INFO, 0);
358 new LandInfoWindow(tile);
361 static const NWidgetPart _nested_about_widgets[] = {
362 NWidget(NWID_HORIZONTAL),
363 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
364 NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_ABOUT_OPENTTD, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
365 EndContainer(),
366 NWidget(WWT_PANEL, COLOUR_GREY), SetPIP(4, 2, 4),
367 NWidget(WWT_LABEL, COLOUR_GREY), SetDataTip(STR_ABOUT_ORIGINAL_COPYRIGHT, STR_NULL),
368 NWidget(WWT_LABEL, COLOUR_GREY), SetDataTip(STR_ABOUT_VERSION, STR_NULL),
369 NWidget(WWT_FRAME, COLOUR_GREY), SetPadding(0, 5, 1, 5),
370 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_A_SCROLLING_TEXT),
371 EndContainer(),
372 NWidget(WWT_LABEL, COLOUR_GREY, WID_A_WEBSITE), SetDataTip(STR_BLACK_RAW_STRING, STR_NULL),
373 NWidget(WWT_LABEL, COLOUR_GREY), SetDataTip(STR_ABOUT_COPYRIGHT_OPENTTD, STR_NULL),
374 EndContainer(),
377 static WindowDesc _about_desc(
378 WDP_CENTER, NULL, 0, 0,
379 WC_GAME_OPTIONS, WC_NONE,
381 _nested_about_widgets, lengthof(_nested_about_widgets)
384 static const char * const _credits[] = {
385 "Original design by Chris Sawyer",
386 "Original graphics by Simon Foster",
388 "The OpenTTD team (in alphabetical order):",
389 " Albert Hofkamp (Alberth) - GUI expert",
390 " Jean-Fran\xC3\xA7ois Claeys (Belugas) - GUI, newindustries and more",
391 " Matthijs Kooijman (blathijs) - Pathfinder-guru, pool rework",
392 " Christoph Elsenhans (frosch) - General coding",
393 " Ulf Hermann (fonsinchen) - Cargo Distribution",
394 " Lo\xC3\xAF""c Guilloux (glx) - Windows Expert",
395 " Michael Lutz (michi_cc) - Path based signals",
396 " Owen Rudge (orudge) - Forum host, OS/2 port",
397 " Peter Nelson (peter1138) - Spiritual descendant from NewGRF gods",
398 " Ingo von Borstel (planetmaker) - Support",
399 " Remko Bijker (Rubidium) - Lead coder and way more",
400 " Zden\xC4\x9Bk Sojka (SmatZ) - Bug finder and fixer",
401 " Jos\xC3\xA9 Soler (Terkhen) - General coding",
402 " Thijs Marinussen (Yexo) - AI Framework",
403 " Leif Linse (Zuu) - AI/Game Script",
405 "Inactive Developers:",
406 " Bjarni Corfitzen (Bjarni) - MacOSX port, coder and vehicles",
407 " Victor Fischer (Celestar) - Programming everywhere you need him to",
408 " Tam\xC3\xA1s Farag\xC3\xB3 (Darkvater) - Ex-Lead coder",
409 " Jaroslav Mazanec (KUDr) - YAPG (Yet Another Pathfinder God) ;)",
410 " Jonathan Coome (Maedhros) - High priest of the NewGRF Temple",
411 " Attila B\xC3\xA1n (MiHaMiX) - Developer WebTranslator 1 and 2",
412 " Christoph Mallon (Tron) - Programmer, code correctness police",
414 "Retired Developers:",
415 " Ludvig Strigeus (ludde) - OpenTTD author, main coder (0.1 - 0.3.3)",
416 " Serge Paquet (vurlix) - Assistant project manager, coder (0.1 - 0.3.3)",
417 " Dominik Scherer (dominik81) - Lead programmer, GUI expert (0.3.0 - 0.3.6)",
418 " Benedikt Br\xC3\xBCggemeier (skidd13) - Bug fixer and code reworker",
419 " Patric Stout (TrueBrain) - NoProgrammer (0.3 - 1.2), sys op (active)",
421 "Special thanks go out to:",
422 " Josef Drexler - For his great work on TTDPatch",
423 " Marcin Grzegorczyk - Track foundations and for describing TTD internals",
424 " Petr Baudi\xC5\xA1 (pasky) - Many patches, newGRF support",
425 " Simon Sasburg (HackyKid) - Many bugfixes he has blessed us with",
426 " Stefan Mei\xC3\x9Fner (sign_de) - For his work on the console",
427 " Mike Ragsdale - OpenTTD installer",
428 " Cian Duffy (MYOB) - BeOS port / manual writing",
429 " Christian Rosentreter (tokai) - MorphOS / AmigaOS port",
430 " Richard Kempton (richK) - additional airports, initial TGP implementation",
432 " Alberto Demichelis - Squirrel scripting language \xC2\xA9 2003-2008",
433 " L. Peter Deutsch - MD5 implementation \xC2\xA9 1999, 2000, 2002",
434 " Michael Blunck - Pre-signals and semaphores \xC2\xA9 2003",
435 " George - Canal/Lock graphics \xC2\xA9 2003-2004",
436 " Andrew Parkhouse (andythenorth) - River graphics",
437 " David Dallaston (Pikka) - Tram tracks",
438 " All Translators - Who made OpenTTD a truly international game",
439 " Bug Reporters - Without whom OpenTTD would still be full of bugs!",
442 "And last but not least:",
443 " Chris Sawyer - For an amazing game!"
446 struct AboutWindow : public Window {
447 int text_position; ///< The top of the scrolling text
448 byte counter; ///< Used to scroll the text every 5 ticks
449 int line_height; ///< The height of a single line
450 static const int num_visible_lines = 19; ///< The number of lines visible simultaneously
452 AboutWindow() : Window(&_about_desc)
454 this->InitNested(WN_GAME_OPTIONS_ABOUT);
456 this->counter = 5;
457 this->text_position = this->GetWidget<NWidgetBase>(WID_A_SCROLLING_TEXT)->pos_y + this->GetWidget<NWidgetBase>(WID_A_SCROLLING_TEXT)->current_y;
460 virtual void SetStringParameters(int widget) const
462 if (widget == WID_A_WEBSITE) SetDParamStr(0, "Website: http://www.openttd.org");
465 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
467 if (widget != WID_A_SCROLLING_TEXT) return;
469 this->line_height = FONT_HEIGHT_NORMAL;
471 Dimension d;
472 d.height = this->line_height * num_visible_lines;
474 d.width = 0;
475 for (uint i = 0; i < lengthof(_credits); i++) {
476 d.width = max(d.width, GetStringBoundingBox(_credits[i]).width);
478 *size = maxdim(*size, d);
481 virtual void DrawWidget(const Rect &r, int widget) const
483 if (widget != WID_A_SCROLLING_TEXT) return;
485 int y = this->text_position;
487 /* Show all scrolling _credits */
488 for (uint i = 0; i < lengthof(_credits); i++) {
489 if (y >= r.top + 7 && y < r.bottom - this->line_height) {
490 DrawString(r.left, r.right, y, _credits[i], TC_BLACK, SA_LEFT | SA_FORCE);
492 y += this->line_height;
496 virtual void OnTick()
498 if (--this->counter == 0) {
499 this->counter = 5;
500 this->text_position--;
501 /* If the last text has scrolled start a new from the start */
502 if (this->text_position < (int)(this->GetWidget<NWidgetBase>(WID_A_SCROLLING_TEXT)->pos_y - lengthof(_credits) * this->line_height)) {
503 this->text_position = this->GetWidget<NWidgetBase>(WID_A_SCROLLING_TEXT)->pos_y + this->GetWidget<NWidgetBase>(WID_A_SCROLLING_TEXT)->current_y;
505 this->SetDirty();
510 void ShowAboutWindow()
512 DeleteWindowByClass(WC_GAME_OPTIONS);
513 new AboutWindow();
517 * Display estimated costs.
518 * @param cost Estimated cost (or income if negative).
519 * @param x X position of the notification window.
520 * @param y Y position of the notification window.
522 void ShowEstimatedCostOrIncome(Money cost, int x, int y)
524 StringID msg = STR_MESSAGE_ESTIMATED_COST;
526 if (cost < 0) {
527 cost = -cost;
528 msg = STR_MESSAGE_ESTIMATED_INCOME;
530 SetDParam(0, cost);
531 ShowErrorMessage(msg, INVALID_STRING_ID, WL_INFO, x, y);
535 * Display animated income or costs on the map.
536 * @param x World X position of the animation location.
537 * @param y World Y position of the animation location.
538 * @param z World Z position of the animation location.
539 * @param cost Estimated cost (or income if negative).
541 void ShowCostOrIncomeAnimation(int x, int y, int z, Money cost)
543 Point pt = RemapCoords(x, y, z);
544 StringID msg = STR_INCOME_FLOAT_COST;
546 if (cost < 0) {
547 cost = -cost;
548 msg = STR_INCOME_FLOAT_INCOME;
550 SetDParam(0, cost);
551 AddTextEffect(msg, pt.x, pt.y, DAY_TICKS, TE_RISING);
555 * Display animated feeder income.
556 * @param x World X position of the animation location.
557 * @param y World Y position of the animation location.
558 * @param z World Z position of the animation location.
559 * @param transfer Estimated feeder income.
560 * @param income Real income from goods being delivered to their final destination.
562 void ShowFeederIncomeAnimation(int x, int y, int z, Money transfer, Money income)
564 Point pt = RemapCoords(x, y, z);
566 SetDParam(0, transfer);
567 if (income == 0) {
568 AddTextEffect(STR_FEEDER, pt.x, pt.y, DAY_TICKS, TE_RISING);
569 } else {
570 StringID msg = STR_FEEDER_COST;
571 if (income < 0) {
572 income = -income;
573 msg = STR_FEEDER_INCOME;
575 SetDParam(1, income);
576 AddTextEffect(msg, pt.x, pt.y, DAY_TICKS, TE_RISING);
581 * Display vehicle loading indicators.
582 * @param x World X position of the animation location.
583 * @param y World Y position of the animation location.
584 * @param z World Z position of the animation location.
585 * @param percent Estimated feeder income.
586 * @param string String which is drawn on the map.
587 * @return TextEffectID to be used for future updates of the loading indicators.
589 TextEffectID ShowFillingPercent(int x, int y, int z, uint8 percent, StringID string)
591 Point pt = RemapCoords(x, y, z);
593 assert(string != STR_NULL);
595 SetDParam(0, percent);
596 return AddTextEffect(string, pt.x, pt.y, 0, TE_STATIC);
600 * Update vehicle loading indicators.
601 * @param te_id TextEffectID to be updated.
602 * @param string String wich is printed.
604 void UpdateFillingPercent(TextEffectID te_id, uint8 percent, StringID string)
606 assert(string != STR_NULL);
608 SetDParam(0, percent);
609 UpdateTextEffect(te_id, string);
613 * Hide vehicle loading indicators.
614 * @param *te_id TextEffectID which is supposed to be hidden.
616 void HideFillingPercent(TextEffectID *te_id)
618 if (*te_id == INVALID_TE_ID) return;
620 RemoveTextEffect(*te_id);
621 *te_id = INVALID_TE_ID;
624 static const NWidgetPart _nested_tooltips_widgets[] = {
625 NWidget(WWT_PANEL, COLOUR_GREY, WID_TT_BACKGROUND), SetMinimalSize(200, 32), EndContainer(),
628 static WindowDesc _tool_tips_desc(
629 WDP_MANUAL, NULL, 0, 0, // Coordinates and sizes are not used,
630 WC_TOOLTIPS, WC_NONE,
632 _nested_tooltips_widgets, lengthof(_nested_tooltips_widgets)
635 /** Window for displaying a tooltip. */
636 struct TooltipsWindow : public Window
638 StringID string_id; ///< String to display as tooltip.
639 byte paramcount; ///< Number of string parameters in #string_id.
640 uint64 params[5]; ///< The string parameters.
641 TooltipCloseCondition close_cond; ///< Condition for closing the window.
643 TooltipsWindow(Window *parent, StringID str, uint paramcount, const uint64 params[], TooltipCloseCondition close_tooltip) : Window(&_tool_tips_desc)
645 this->parent = parent;
646 this->string_id = str;
647 assert_compile(sizeof(this->params[0]) == sizeof(params[0]));
648 assert(paramcount <= lengthof(this->params));
649 memcpy(this->params, params, sizeof(this->params[0]) * paramcount);
650 this->paramcount = paramcount;
651 this->close_cond = close_tooltip;
653 this->InitNested();
655 CLRBITS(this->flags, WF_WHITE_BORDER);
658 virtual Point OnInitialPosition(int16 sm_width, int16 sm_height, int window_number)
660 /* Find the free screen space between the main toolbar at the top, and the statusbar at the bottom.
661 * Add a fixed distance 2 so the tooltip floats free from both bars.
663 int scr_top = GetMainViewTop() + 2;
664 int scr_bot = GetMainViewBottom() - 2;
666 Point pt;
668 /* Correctly position the tooltip position, watch out for window and cursor size
669 * Clamp value to below main toolbar and above statusbar. If tooltip would
670 * go below window, flip it so it is shown above the cursor */
671 pt.y = Clamp(_cursor.pos.y + _cursor.size.y + _cursor.offs.y + 5, scr_top, scr_bot);
672 if (pt.y + sm_height > scr_bot) pt.y = min(_cursor.pos.y + _cursor.offs.y - 5, scr_bot) - sm_height;
673 pt.x = sm_width >= _screen.width ? 0 : Clamp(_cursor.pos.x - (sm_width >> 1), 0, _screen.width - sm_width);
675 return pt;
678 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
680 /* There is only one widget. */
681 for (uint i = 0; i != this->paramcount; i++) SetDParam(i, this->params[i]);
683 size->width = min(GetStringBoundingBox(this->string_id).width, 194);
684 size->height = GetStringHeight(this->string_id, size->width);
686 /* Increase slightly to have some space around the box. */
687 size->width += 2 + WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
688 size->height += 2 + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
691 virtual void DrawWidget(const Rect &r, int widget) const
693 /* There is only one widget. */
694 GfxFillRect(r.left, r.top, r.right, r.bottom, PC_BLACK);
695 GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, PC_LIGHT_YELLOW);
697 for (uint arg = 0; arg < this->paramcount; arg++) {
698 SetDParam(arg, this->params[arg]);
700 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);
703 virtual void OnMouseLoop()
705 /* Always close tooltips when the cursor is not in our window. */
706 if (!_cursor.in_window) {
707 delete this;
708 return;
711 /* We can show tooltips while dragging tools. These are shown as long as
712 * we are dragging the tool. Normal tooltips work with hover or rmb. */
713 switch (this->close_cond) {
714 case TCC_RIGHT_CLICK: if (!_right_button_down) delete this; break;
715 case TCC_LEFT_CLICK: if (!_left_button_down) delete this; break;
716 case TCC_HOVER: if (!_mouse_hovering) delete this; break;
722 * Shows a tooltip
723 * @param parent The window this tooltip is related to.
724 * @param str String to be displayed
725 * @param paramcount number of params to deal with
726 * @param params (optional) up to 5 pieces of additional information that may be added to a tooltip
727 * @param use_left_mouse_button close the tooltip when the left (true) or right (false) mouse button is released
729 void GuiShowTooltips(Window *parent, StringID str, uint paramcount, const uint64 params[], TooltipCloseCondition close_tooltip)
731 DeleteWindowById(WC_TOOLTIPS, 0);
733 if (str == STR_NULL) return;
735 new TooltipsWindow(parent, str, paramcount, params, close_tooltip);
738 void QueryString::HandleEditBox(Window *w, int wid)
740 if (w->IsWidgetGloballyFocused(wid) && this->text.HandleCaret()) {
741 w->SetWidgetDirty(wid);
743 /* For the OSK also invalidate the parent window */
744 if (w->window_class == WC_OSK) w->InvalidateData();
748 void QueryString::DrawEditBox(const Window *w, int wid) const
750 const NWidgetLeaf *wi = w->GetWidget<NWidgetLeaf>(wid);
752 assert((wi->type & WWT_MASK) == WWT_EDITBOX);
754 bool rtl = _current_text_dir == TD_RTL;
755 Dimension sprite_size = GetSpriteSize(rtl ? SPR_IMG_DELETE_RIGHT : SPR_IMG_DELETE_LEFT);
756 int clearbtn_width = sprite_size.width + WD_IMGBTN_LEFT + WD_IMGBTN_RIGHT;
758 int clearbtn_left = wi->pos_x + (rtl ? 0 : wi->current_x - clearbtn_width);
759 int clearbtn_right = wi->pos_x + (rtl ? clearbtn_width : wi->current_x) - 1;
760 int left = wi->pos_x + (rtl ? clearbtn_width : 0);
761 int right = wi->pos_x + (rtl ? wi->current_x : wi->current_x - clearbtn_width) - 1;
763 int top = wi->pos_y;
764 int bottom = wi->pos_y + wi->current_y - 1;
766 DrawFrameRect(clearbtn_left, top, clearbtn_right, bottom, wi->colour, wi->IsLowered() ? FR_LOWERED : FR_NONE);
767 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));
768 if (this->text.bytes == 1) GfxFillRect(clearbtn_left + 1, top + 1, clearbtn_right - 1, bottom - 1, _colour_gradient[wi->colour & 0xF][2], FILLRECT_CHECKER);
770 DrawFrameRect(left, top, right, bottom, wi->colour, FR_LOWERED | FR_DARKENED);
771 GfxFillRect(left + 1, top + 1, right - 1, bottom - 1, PC_BLACK);
773 /* Limit the drawing of the string inside the widget boundaries */
774 DrawPixelInfo dpi;
775 if (!FillDrawPixelInfo(&dpi, left + WD_FRAMERECT_LEFT, top + WD_FRAMERECT_TOP, right - left - WD_FRAMERECT_RIGHT, bottom - top - WD_FRAMERECT_BOTTOM)) return;
777 DrawPixelInfo *old_dpi = _cur_dpi;
778 _cur_dpi = &dpi;
780 /* We will take the current widget length as maximum width, with a small
781 * space reserved at the end for the caret to show */
782 const Textbuf *tb = &this->text;
783 int delta = min(0, (right - left) - tb->pixels - 10);
785 if (tb->caretxoffs + delta < 0) delta = -tb->caretxoffs;
787 /* If we have a marked area, draw a background highlight. */
788 if (tb->marklength != 0) GfxFillRect(delta + tb->markxoffs, 0, delta + tb->markxoffs + tb->marklength - 1, bottom - top, PC_GREY);
790 DrawString(delta, tb->pixels, 0, tb->buf, TC_YELLOW);
791 bool focussed = w->IsWidgetGloballyFocused(wid) || IsOSKOpenedFor(w, wid);
792 if (focussed && tb->caret) {
793 int caret_width = GetStringBoundingBox("_").width;
794 DrawString(tb->caretxoffs + delta, tb->caretxoffs + delta + caret_width, 0, "_", TC_WHITE);
797 _cur_dpi = old_dpi;
801 * Get the current caret position.
802 * @param w Window the edit box is in.
803 * @param wid Widget index.
804 * @return Top-left location of the caret, relative to the window.
806 Point QueryString::GetCaretPosition(const Window *w, int wid) const
808 const NWidgetLeaf *wi = w->GetWidget<NWidgetLeaf>(wid);
810 assert((wi->type & WWT_MASK) == WWT_EDITBOX);
812 bool rtl = _current_text_dir == TD_RTL;
813 Dimension sprite_size = GetSpriteSize(rtl ? SPR_IMG_DELETE_RIGHT : SPR_IMG_DELETE_LEFT);
814 int clearbtn_width = sprite_size.width + WD_IMGBTN_LEFT + WD_IMGBTN_RIGHT;
816 int left = wi->pos_x + (rtl ? clearbtn_width : 0);
817 int right = wi->pos_x + (rtl ? wi->current_x : wi->current_x - clearbtn_width) - 1;
819 /* Clamp caret position to be inside out current width. */
820 const Textbuf *tb = &this->text;
821 int delta = min(0, (right - left) - tb->pixels - 10);
822 if (tb->caretxoffs + delta < 0) delta = -tb->caretxoffs;
824 Point pt = {left + WD_FRAMERECT_LEFT + tb->caretxoffs + delta, wi->pos_y + WD_FRAMERECT_TOP};
825 return pt;
829 * Get the bounding rectangle for a range of the query string.
830 * @param w Window the edit box is in.
831 * @param wid Widget index.
832 * @param from Start of the string range.
833 * @param to End of the string range.
834 * @return Rectangle encompassing the string range, relative to the window.
836 Rect QueryString::GetBoundingRect(const Window *w, int wid, const char *from, const char *to) const
838 const NWidgetLeaf *wi = w->GetWidget<NWidgetLeaf>(wid);
840 assert((wi->type & WWT_MASK) == WWT_EDITBOX);
842 bool rtl = _current_text_dir == TD_RTL;
843 Dimension sprite_size = GetSpriteSize(rtl ? SPR_IMG_DELETE_RIGHT : SPR_IMG_DELETE_LEFT);
844 int clearbtn_width = sprite_size.width + WD_IMGBTN_LEFT + WD_IMGBTN_RIGHT;
846 int left = wi->pos_x + (rtl ? clearbtn_width : 0);
847 int right = wi->pos_x + (rtl ? wi->current_x : wi->current_x - clearbtn_width) - 1;
849 int top = wi->pos_y + WD_FRAMERECT_TOP;
850 int bottom = wi->pos_y + wi->current_y - 1 - WD_FRAMERECT_BOTTOM;
852 /* Clamp caret position to be inside our current width. */
853 const Textbuf *tb = &this->text;
854 int delta = min(0, (right - left) - tb->pixels - 10);
855 if (tb->caretxoffs + delta < 0) delta = -tb->caretxoffs;
857 /* Get location of first and last character. */
858 Point p1 = GetCharPosInString(tb->buf, from, FS_NORMAL);
859 Point p2 = from != to ? GetCharPosInString(tb->buf, to, FS_NORMAL) : p1;
861 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 };
863 return r;
867 * Get the character that is rendered at a position.
868 * @param w Window the edit box is in.
869 * @param wid Widget index.
870 * @param pt Position to test.
871 * @return Pointer to the character at the position or NULL if no character is at the position.
873 const char *QueryString::GetCharAtPosition(const Window *w, int wid, const Point &pt) const
875 const NWidgetLeaf *wi = w->GetWidget<NWidgetLeaf>(wid);
877 assert((wi->type & WWT_MASK) == WWT_EDITBOX);
879 bool rtl = _current_text_dir == TD_RTL;
880 Dimension sprite_size = GetSpriteSize(rtl ? SPR_IMG_DELETE_RIGHT : SPR_IMG_DELETE_LEFT);
881 int clearbtn_width = sprite_size.width + WD_IMGBTN_LEFT + WD_IMGBTN_RIGHT;
883 int left = wi->pos_x + (rtl ? clearbtn_width : 0);
884 int right = wi->pos_x + (rtl ? wi->current_x : wi->current_x - clearbtn_width) - 1;
886 int top = wi->pos_y + WD_FRAMERECT_TOP;
887 int bottom = wi->pos_y + wi->current_y - 1 - WD_FRAMERECT_BOTTOM;
889 if (!IsInsideMM(pt.y, top, bottom)) return NULL;
891 /* Clamp caret position to be inside our current width. */
892 const Textbuf *tb = &this->text;
893 int delta = min(0, (right - left) - tb->pixels - 10);
894 if (tb->caretxoffs + delta < 0) delta = -tb->caretxoffs;
896 return ::GetCharAtPosition(tb->buf, pt.x - delta - left);
899 void QueryString::ClickEditBox(Window *w, Point pt, int wid, int click_count, bool focus_changed)
901 const NWidgetLeaf *wi = w->GetWidget<NWidgetLeaf>(wid);
903 assert((wi->type & WWT_MASK) == WWT_EDITBOX);
905 bool rtl = _current_text_dir == TD_RTL;
906 int clearbtn_width = GetSpriteSize(rtl ? SPR_IMG_DELETE_RIGHT : SPR_IMG_DELETE_LEFT).width;
908 int clearbtn_left = wi->pos_x + (rtl ? 0 : wi->current_x - clearbtn_width);
910 if (IsInsideBS(pt.x, clearbtn_left, clearbtn_width)) {
911 if (this->text.bytes > 1) {
912 this->text.DeleteAll();
913 w->HandleButtonClick(wid);
914 w->OnEditboxChanged(wid);
916 return;
919 if (w->window_class != WC_OSK && _settings_client.gui.osk_activation != OSKA_DISABLED &&
920 (!focus_changed || _settings_client.gui.osk_activation == OSKA_IMMEDIATELY) &&
921 (click_count == 2 || _settings_client.gui.osk_activation != OSKA_DOUBLE_CLICK)) {
922 /* Open the OSK window */
923 ShowOnScreenKeyboard(w, wid);
927 /** Class for the string query window. */
928 struct QueryStringWindow : public Window
930 QueryString editbox; ///< Editbox.
931 QueryStringFlags flags; ///< Flags controlling behaviour of the window.
933 QueryStringWindow(StringID str, StringID caption, uint max_bytes, uint max_chars, WindowDesc *desc, Window *parent, CharSetFilter afilter, QueryStringFlags flags) :
934 Window(desc), editbox(max_bytes, max_chars)
936 char *last_of = &this->editbox.text.buf[this->editbox.text.max_bytes - 1];
937 GetString(this->editbox.text.buf, str, last_of);
938 str_validate(this->editbox.text.buf, last_of, SVS_NONE);
940 /* Make sure the name isn't too long for the text buffer in the number of
941 * characters (not bytes). max_chars also counts the '\0' characters. */
942 while (Utf8StringLength(this->editbox.text.buf) + 1 > this->editbox.text.max_chars) {
943 *Utf8PrevChar(this->editbox.text.buf + strlen(this->editbox.text.buf)) = '\0';
946 this->editbox.text.UpdateSize();
948 if ((flags & QSF_ACCEPT_UNCHANGED) == 0) this->editbox.orig = strdup(this->editbox.text.buf);
950 this->querystrings[WID_QS_TEXT] = &this->editbox;
951 this->editbox.caption = caption;
952 this->editbox.cancel_button = WID_QS_CANCEL;
953 this->editbox.ok_button = WID_QS_OK;
954 this->editbox.text.afilter = afilter;
955 this->flags = flags;
957 this->InitNested(WN_QUERY_STRING);
959 this->parent = parent;
961 this->SetFocusedWidget(WID_QS_TEXT);
964 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
966 if (widget == WID_QS_DEFAULT && (this->flags & QSF_ENABLE_DEFAULT) == 0) {
967 /* We don't want this widget to show! */
968 fill->width = 0;
969 resize->width = 0;
970 size->width = 0;
974 virtual void SetStringParameters(int widget) const
976 if (widget == WID_QS_CAPTION) SetDParam(0, this->editbox.caption);
979 void OnOk()
981 if (this->editbox.orig == NULL || strcmp(this->editbox.text.buf, this->editbox.orig) != 0) {
982 /* If the parent is NULL, the editbox is handled by general function
983 * HandleOnEditText */
984 if (this->parent != NULL) {
985 this->parent->OnQueryTextFinished(this->editbox.text.buf);
986 } else {
987 HandleOnEditText(this->editbox.text.buf);
989 this->editbox.handled = true;
993 virtual void OnClick(Point pt, int widget, int click_count)
995 switch (widget) {
996 case WID_QS_DEFAULT:
997 this->editbox.text.DeleteAll();
998 /* FALL THROUGH */
999 case WID_QS_OK:
1000 this->OnOk();
1001 /* FALL THROUGH */
1002 case WID_QS_CANCEL:
1003 delete this;
1004 break;
1008 ~QueryStringWindow()
1010 if (!this->editbox.handled && this->parent != NULL) {
1011 Window *parent = this->parent;
1012 this->parent = NULL; // so parent doesn't try to delete us again
1013 parent->OnQueryTextFinished(NULL);
1018 static const NWidgetPart _nested_query_string_widgets[] = {
1019 NWidget(NWID_HORIZONTAL),
1020 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
1021 NWidget(WWT_CAPTION, COLOUR_GREY, WID_QS_CAPTION), SetDataTip(STR_WHITE_STRING, STR_NULL),
1022 EndContainer(),
1023 NWidget(WWT_PANEL, COLOUR_GREY),
1024 NWidget(WWT_EDITBOX, COLOUR_GREY, WID_QS_TEXT), SetMinimalSize(256, 12), SetFill(1, 1), SetPadding(2, 2, 2, 2),
1025 EndContainer(),
1026 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
1027 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_QS_DEFAULT), SetMinimalSize(87, 12), SetFill(1, 1), SetDataTip(STR_BUTTON_DEFAULT, STR_NULL),
1028 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_QS_CANCEL), SetMinimalSize(86, 12), SetFill(1, 1), SetDataTip(STR_BUTTON_CANCEL, STR_NULL),
1029 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_QS_OK), SetMinimalSize(87, 12), SetFill(1, 1), SetDataTip(STR_BUTTON_OK, STR_NULL),
1030 EndContainer(),
1033 static WindowDesc _query_string_desc(
1034 WDP_CENTER, "query_string", 0, 0,
1035 WC_QUERY_STRING, WC_NONE,
1037 _nested_query_string_widgets, lengthof(_nested_query_string_widgets)
1041 * Show a query popup window with a textbox in it.
1042 * @param str StringID for the text shown in the textbox
1043 * @param caption StringID of text shown in caption of querywindow
1044 * @param maxsize maximum size in bytes or characters (including terminating '\0') depending on flags
1045 * @param parent pointer to a Window that will handle the events (ok/cancel) of this
1046 * window. If NULL, results are handled by global function HandleOnEditText
1047 * @param afilter filters out unwanted character input
1048 * @param flags various flags, @see QueryStringFlags
1050 void ShowQueryString(StringID str, StringID caption, uint maxsize, Window *parent, CharSetFilter afilter, QueryStringFlags flags)
1052 DeleteWindowByClass(WC_QUERY_STRING);
1053 new QueryStringWindow(str, caption, ((flags & QSF_LEN_IN_CHARS) ? MAX_CHAR_LENGTH : 1) * maxsize, maxsize, &_query_string_desc, parent, afilter, flags);
1057 * Window used for asking the user a YES/NO question.
1059 struct QueryWindow : public Window {
1060 QueryCallbackProc *proc; ///< callback function executed on closing of popup. Window* points to parent, bool is true if 'yes' clicked, false otherwise
1061 uint64 params[10]; ///< local copy of _decode_parameters
1062 StringID message; ///< message shown for query window
1063 StringID caption; ///< title of window
1065 QueryWindow(WindowDesc *desc, StringID caption, StringID message, Window *parent, QueryCallbackProc *callback) : Window(desc)
1067 /* Create a backup of the variadic arguments to strings because it will be
1068 * overridden pretty often. We will copy these back for drawing */
1069 CopyOutDParam(this->params, 0, lengthof(this->params));
1070 this->caption = caption;
1071 this->message = message;
1072 this->proc = callback;
1074 this->InitNested(WN_CONFIRM_POPUP_QUERY);
1076 this->parent = parent;
1077 this->left = parent->left + (parent->width / 2) - (this->width / 2);
1078 this->top = parent->top + (parent->height / 2) - (this->height / 2);
1081 ~QueryWindow()
1083 if (this->proc != NULL) this->proc(this->parent, false);
1086 virtual void SetStringParameters(int widget) const
1088 switch (widget) {
1089 case WID_Q_CAPTION:
1090 CopyInDParam(1, this->params, lengthof(this->params));
1091 SetDParam(0, this->caption);
1092 break;
1094 case WID_Q_TEXT:
1095 CopyInDParam(0, this->params, lengthof(this->params));
1096 break;
1100 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
1102 if (widget != WID_Q_TEXT) return;
1104 Dimension d = GetStringMultiLineBoundingBox(this->message, *size);
1105 d.width += WD_FRAMETEXT_LEFT + WD_FRAMETEXT_RIGHT;
1106 d.height += WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
1107 *size = d;
1110 virtual void DrawWidget(const Rect &r, int widget) const
1112 if (widget != WID_Q_TEXT) return;
1114 DrawStringMultiLine(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, r.top + WD_FRAMERECT_TOP, r.bottom - WD_FRAMERECT_BOTTOM,
1115 this->message, TC_FROMSTRING, SA_CENTER);
1118 virtual void OnClick(Point pt, int widget, int click_count)
1120 switch (widget) {
1121 case WID_Q_YES: {
1122 /* in the Generate New World window, clicking 'Yes' causes
1123 * DeleteNonVitalWindows() to be called - we shouldn't be in a window then */
1124 QueryCallbackProc *proc = this->proc;
1125 Window *parent = this->parent;
1126 /* Prevent the destructor calling the callback function */
1127 this->proc = NULL;
1128 delete this;
1129 if (proc != NULL) {
1130 proc(parent, true);
1131 proc = NULL;
1133 break;
1135 case WID_Q_NO:
1136 delete this;
1137 break;
1141 virtual EventState OnKeyPress(WChar key, uint16 keycode)
1143 /* ESC closes the window, Enter confirms the action */
1144 switch (keycode) {
1145 case WKC_RETURN:
1146 case WKC_NUM_ENTER:
1147 if (this->proc != NULL) {
1148 this->proc(this->parent, true);
1149 this->proc = NULL;
1151 /* FALL THROUGH */
1152 case WKC_ESC:
1153 delete this;
1154 return ES_HANDLED;
1156 return ES_NOT_HANDLED;
1160 static const NWidgetPart _nested_query_widgets[] = {
1161 NWidget(NWID_HORIZONTAL),
1162 NWidget(WWT_CLOSEBOX, COLOUR_RED),
1163 NWidget(WWT_CAPTION, COLOUR_RED, WID_Q_CAPTION), SetDataTip(STR_JUST_STRING, STR_NULL),
1164 EndContainer(),
1165 NWidget(WWT_PANEL, COLOUR_RED), SetPIP(8, 15, 8),
1166 NWidget(WWT_TEXT, COLOUR_RED, WID_Q_TEXT), SetMinimalSize(200, 12),
1167 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(20, 29, 20),
1168 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_Q_NO), SetMinimalSize(71, 12), SetDataTip(STR_QUIT_NO, STR_NULL),
1169 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_Q_YES), SetMinimalSize(71, 12), SetDataTip(STR_QUIT_YES, STR_NULL),
1170 EndContainer(),
1171 EndContainer(),
1174 static WindowDesc _query_desc(
1175 WDP_CENTER, NULL, 0, 0,
1176 WC_CONFIRM_POPUP_QUERY, WC_NONE,
1177 WDF_MODAL,
1178 _nested_query_widgets, lengthof(_nested_query_widgets)
1182 * Show a modal confirmation window with standard 'yes' and 'no' buttons
1183 * The window is aligned to the centre of its parent.
1184 * @param caption string shown as window caption
1185 * @param message string that will be shown for the window
1186 * @param parent pointer to parent window, if this pointer is NULL the parent becomes
1187 * the main window WC_MAIN_WINDOW
1188 * @param callback callback function pointer to set in the window descriptor
1190 void ShowQuery(StringID caption, StringID message, Window *parent, QueryCallbackProc *callback)
1192 if (parent == NULL) parent = FindWindowById(WC_MAIN_WINDOW, 0);
1194 const Window *w;
1195 FOR_ALL_WINDOWS_FROM_BACK(w) {
1196 if (w->window_class != WC_CONFIRM_POPUP_QUERY) continue;
1198 const QueryWindow *qw = (const QueryWindow *)w;
1199 if (qw->parent != parent || qw->proc != callback) continue;
1201 delete qw;
1202 break;
1205 new QueryWindow(&_query_desc, caption, message, parent, callback);