OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

misc_gui.cpp
Go to the documentation of this file.
1 /* $Id: misc_gui.cpp 27560 2016-05-11 20:48:46Z matthijs $ */
2
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 */
9
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"
29
30 #include "widgets/misc_widget.h"
31
32 #include "table/strings.h"
33
34 #include "safeguards.h"
35
37 enum OskActivation {
38 OSKA_DISABLED,
39 OSKA_DOUBLE_CLICK,
40 OSKA_SINGLE_CLICK,
41 OSKA_IMMEDIATELY,
42 };
43
44
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(),
52 };
53
54 static WindowDesc _land_info_desc(
55 WDP_AUTO, "land_info", 0, 0,
56 WC_LAND_INFO, WC_NONE,
57 0,
58 _nested_land_info_widgets, lengthof(_nested_land_info_widgets)
59 );
60
61 class LandInfoWindow : public Window {
62 enum LandInfoLines {
63 LAND_INFO_CENTERED_LINES = 32,
64 LAND_INFO_MULTICENTER_LINE = LAND_INFO_CENTERED_LINES,
65 LAND_INFO_LINE_END,
66 };
67
68 static const uint LAND_INFO_LINE_BUFF_SIZE = 512;
69
70 public:
71 char landinfo_data[LAND_INFO_LINE_END][LAND_INFO_LINE_BUFF_SIZE];
72 TileIndex tile;
73
74 virtual void DrawWidget(const Rect &r, int widget) const
75 {
76 if (widget != WID_LI_BACKGROUND) return;
77
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;
81
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;
85 }
86
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);
90 }
91 }
92
93 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
94 {
95 if (widget != WID_LI_BACKGROUND) return;
96
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;
100
101 uint width = GetStringBoundingBox(this->landinfo_data[i]).width + WD_FRAMETEXT_LEFT + WD_FRAMETEXT_RIGHT;
102 size->width = max(size->width, width);
103
104 size->height += FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL;
105 if (i == 0) size->height += 4;
106 }
107
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);
113 }
114 }
115
116 LandInfoWindow(TileIndex tile) : Window(&_land_info_desc), tile(tile)
117 {
118 this->InitNested();
119
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
136 }
137
138 virtual void OnInit()
139 {
140 Town *t = ClosestTownFromTile(tile, _settings_game.economy.dist_local_authority);
141
142 /* Because build_date is not set yet in every TileDesc, we make sure it is empty */
143 TileDesc td;
144
145 td.build_date = INVALID_DATE;
146
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").
150 */
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;
159
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.rail_speed = 0;
166 td.road_speed = 0;
167
168 td.grf = NULL;
169
170 CargoArray acceptance;
171 AddAcceptedCargo(tile, acceptance, NULL);
172 GetTileDesc(tile, &td);
173
174 uint line_nr = 0;
175
176 /* Tiletype */
177 SetDParam(0, td.dparam[0]);
178 GetString(this->landinfo_data[line_nr], td.str, lastof(this->landinfo_data[line_nr]));
179 line_nr++;
180
181 /* Up to four owners */
182 for (uint i = 0; i < 4; i++) {
183 if (td.owner_type[i] == STR_NULL) continue;
184
185 SetDParam(0, STR_LAND_AREA_INFORMATION_OWNER_N_A);
186 if (td.owner[i] != OWNER_NONE && td.owner[i] != OWNER_WATER) GetNameOfOwner(td.owner[i], tile);
187 GetString(this->landinfo_data[line_nr], td.owner_type[i], lastof(this->landinfo_data[line_nr]));
188 line_nr++;
189 }
190
191 /* Cost to clear/revenue when cleared */
192 StringID str = STR_LAND_AREA_INFORMATION_COST_TO_CLEAR_N_A;
193 Company *c = Company::GetIfValid(_local_company);
194 if (c != NULL) {
195 Money old_money = c->money;
196 c->money = INT64_MAX;
197 assert(_current_company == _local_company);
198 CommandCost costclear = DoCommand(tile, 0, 0, DC_NONE, CMD_LANDSCAPE_CLEAR);
199 c->money = old_money;
200 if (costclear.Succeeded()) {
201 Money cost = costclear.GetCost();
202 if (cost < 0) {
203 cost = -cost; // Negate negative cost to a positive revenue
204 str = STR_LAND_AREA_INFORMATION_REVENUE_WHEN_CLEARED;
205 } else {
206 str = STR_LAND_AREA_INFORMATION_COST_TO_CLEAR;
207 }
208 SetDParam(0, cost);
209 }
210 }
211 GetString(this->landinfo_data[line_nr], str, lastof(this->landinfo_data[line_nr]));
212 line_nr++;
213
214 /* Location */
215 char tmp[16];
216 seprintf(tmp, lastof(tmp), "0x%.4X", tile);
217 SetDParam(0, TileX(tile));
218 SetDParam(1, TileY(tile));
219 SetDParam(2, GetTileZ(tile));
220 SetDParamStr(3, tmp);
221 GetString(this->landinfo_data[line_nr], STR_LAND_AREA_INFORMATION_LANDINFO_COORDS, lastof(this->landinfo_data[line_nr]));
222 line_nr++;
223
224 /* Local authority */
225 SetDParam(0, STR_LAND_AREA_INFORMATION_LOCAL_AUTHORITY_NONE);
226 if (t != NULL) {
227 SetDParam(0, STR_TOWN_NAME);
228 SetDParam(1, t->index);
229 }
230 GetString(this->landinfo_data[line_nr], STR_LAND_AREA_INFORMATION_LOCAL_AUTHORITY, lastof(this->landinfo_data[line_nr]));
231 line_nr++;
232
233 /* Build date */
234 if (td.build_date != INVALID_DATE) {
235 SetDParam(0, td.build_date);
236 GetString(this->landinfo_data[line_nr], STR_LAND_AREA_INFORMATION_BUILD_DATE, lastof(this->landinfo_data[line_nr]));
237 line_nr++;
238 }
239
240 /* Station class */
241 if (td.station_class != STR_NULL) {
242 SetDParam(0, td.station_class);
243 GetString(this->landinfo_data[line_nr], STR_LAND_AREA_INFORMATION_STATION_CLASS, lastof(this->landinfo_data[line_nr]));
244 line_nr++;
245 }
246
247 /* Station type name */
248 if (td.station_name != STR_NULL) {
249 SetDParam(0, td.station_name);
250 GetString(this->landinfo_data[line_nr], STR_LAND_AREA_INFORMATION_STATION_TYPE, lastof(this->landinfo_data[line_nr]));
251 line_nr++;
252 }
253
254 /* Airport class */
255 if (td.airport_class != STR_NULL) {
256 SetDParam(0, td.airport_class);
257 GetString(this->landinfo_data[line_nr], STR_LAND_AREA_INFORMATION_AIRPORT_CLASS, lastof(this->landinfo_data[line_nr]));
258 line_nr++;
259 }
260
261 /* Airport name */
262 if (td.airport_name != STR_NULL) {
263 SetDParam(0, td.airport_name);
264 GetString(this->landinfo_data[line_nr], STR_LAND_AREA_INFORMATION_AIRPORT_NAME, lastof(this->landinfo_data[line_nr]));
265 line_nr++;
266 }
267
268 /* Airport tile name */
269 if (td.airport_tile_name != STR_NULL) {
270 SetDParam(0, td.airport_tile_name);
271 GetString(this->landinfo_data[line_nr], STR_LAND_AREA_INFORMATION_AIRPORTTILE_NAME, lastof(this->landinfo_data[line_nr]));
272 line_nr++;
273 }
274
275 /* Rail speed limit */
276 if (td.rail_speed != 0) {
277 SetDParam(0, td.rail_speed);
278 GetString(this->landinfo_data[line_nr], STR_LANG_AREA_INFORMATION_RAIL_SPEED_LIMIT, lastof(this->landinfo_data[line_nr]));
279 line_nr++;
280 }
281
282 /* Road speed limit */
283 if (td.road_speed != 0) {
284 SetDParam(0, td.road_speed);
285 GetString(this->landinfo_data[line_nr], STR_LANG_AREA_INFORMATION_ROAD_SPEED_LIMIT, lastof(this->landinfo_data[line_nr]));
286 line_nr++;
287 }
288
289 /* NewGRF name */
290 if (td.grf != NULL) {
291 SetDParamStr(0, td.grf);
292 GetString(this->landinfo_data[line_nr], STR_LAND_AREA_INFORMATION_NEWGRF_NAME, lastof(this->landinfo_data[line_nr]));
293 line_nr++;
294 }
295
296 assert(line_nr < LAND_INFO_CENTERED_LINES);
297
298 /* Mark last line empty */
299 this->landinfo_data[line_nr][0] = '\0';
300
301 /* Cargo acceptance is displayed in a extra multiline */
302 char *strp = GetString(this->landinfo_data[LAND_INFO_MULTICENTER_LINE], STR_LAND_AREA_INFORMATION_CARGO_ACCEPTED, lastof(this->landinfo_data[LAND_INFO_MULTICENTER_LINE]));
303 bool found = false;
304
305 for (CargoID i = 0; i < NUM_CARGO; ++i) {
306 if (acceptance[i] > 0) {
307 /* Add a comma between each item. */
308 if (found) {
309 *strp++ = ',';
310 *strp++ = ' ';
311 }
312 found = true;
313
314 /* If the accepted value is less than 8, show it in 1/8:ths */
315 if (acceptance[i] < 8) {
316 SetDParam(0, acceptance[i]);
317 SetDParam(1, CargoSpec::Get(i)->name);
318 strp = GetString(strp, STR_LAND_AREA_INFORMATION_CARGO_EIGHTS, lastof(this->landinfo_data[LAND_INFO_MULTICENTER_LINE]));
319 } else {
320 strp = GetString(strp, CargoSpec::Get(i)->name, lastof(this->landinfo_data[LAND_INFO_MULTICENTER_LINE]));
321 }
322 }
323 }
324 if (!found) this->landinfo_data[LAND_INFO_MULTICENTER_LINE][0] = '\0';
325 }
326
327 virtual bool IsNewGRFInspectable() const
328 {
329 return ::IsNewGRFInspectable(GetGrfSpecFeature(this->tile), this->tile);
330 }
331
332 virtual void ShowNewGRFInspectWindow() const
333 {
334 ::ShowNewGRFInspectWindow(GetGrfSpecFeature(this->tile), this->tile);
335 }
336
342 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
343 {
344 if (!gui_scope) return;
345 switch (data) {
346 case 1:
347 /* ReInit, "debug" sprite might have changed */
348 this->ReInit();
349 break;
350 }
351 }
352 };
353
358 void ShowLandInfo(TileIndex tile)
359 {
360 DeleteWindowById(WC_LAND_INFO, 0);
361 new LandInfoWindow(tile);
362 }
363
364 static const NWidgetPart _nested_about_widgets[] = {
365 NWidget(NWID_HORIZONTAL),
366 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
367 NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_ABOUT_OPENTTD, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
368 EndContainer(),
369 NWidget(WWT_PANEL, COLOUR_GREY), SetPIP(4, 2, 4),
370 NWidget(WWT_LABEL, COLOUR_GREY), SetDataTip(STR_ABOUT_ORIGINAL_COPYRIGHT, STR_NULL),
371 NWidget(WWT_LABEL, COLOUR_GREY), SetDataTip(STR_ABOUT_VERSION, STR_NULL),
372 NWidget(WWT_FRAME, COLOUR_GREY), SetPadding(0, 5, 1, 5),
373 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_A_SCROLLING_TEXT),
374 EndContainer(),
375 NWidget(WWT_LABEL, COLOUR_GREY, WID_A_WEBSITE), SetDataTip(STR_BLACK_RAW_STRING, STR_NULL),
376 NWidget(WWT_LABEL, COLOUR_GREY), SetDataTip(STR_ABOUT_COPYRIGHT_OPENTTD, STR_NULL),
377 EndContainer(),
378 };
379
380 static WindowDesc _about_desc(
381 WDP_CENTER, NULL, 0, 0,
382 WC_GAME_OPTIONS, WC_NONE,
383 0,
384 _nested_about_widgets, lengthof(_nested_about_widgets)
385 );
386
387 static const char * const _credits[] = {
388 "Original design by Chris Sawyer",
389 "Original graphics by Simon Foster",
390 "",
391 "The OpenTTD team (in alphabetical order):",
392 " Albert Hofkamp (Alberth) - GUI expert (since 0.7)",
393 " Matthijs Kooijman (blathijs) - Pathfinder-guru, Debian port (since 0.3)",
394 " Ulf Hermann (fonsinchen) - Cargo Distribution (since 1.3)",
395 " Christoph Elsenhans (frosch) - General coding (since 0.6)",
396 " Lo\xC3\xAF""c Guilloux (glx) - General / Windows Expert (since 0.4.5)",
397 " Michael Lutz (michi_cc) - Path based signals (since 0.7)",
398 " Owen Rudge (orudge) - Forum host, OS/2 port (since 0.1)",
399 " Peter Nelson (peter1138) - Spiritual descendant from NewGRF gods (since 0.4.5)",
400 " Ingo von Borstel (planetmaker) - General, Support (since 1.1)",
401 " Remko Bijker (Rubidium) - Lead coder and way more (since 0.4.5)",
402 " Jos\xC3\xA9 Soler (Terkhen) - General coding (since 1.0)",
403 " Leif Linse (Zuu) - AI/Game Script (since 1.2)",
404 "",
405 "Inactive Developers:",
406 " Jean-Fran\xC3\xA7ois Claeys (Belugas) - GUI, NewGRF and more (0.4.5 - 1.0)",
407 " Bjarni Corfitzen (Bjarni) - MacOSX port, coder and vehicles (0.3 - 0.7)",
408 " Victor Fischer (Celestar) - Programming everywhere you need him to (0.3 - 0.6)",
409 " Jaroslav Mazanec (KUDr) - YAPG (Yet Another Pathfinder God) ;) (0.4.5 - 0.6)",
410 " Jonathan Coome (Maedhros) - High priest of the NewGRF Temple (0.5 - 0.6)",
411 " Attila B\xC3\xA1n (MiHaMiX) - Developer WebTranslator 1 and 2 (0.3 - 0.5)",
412 " Zden\xC4\x9Bk Sojka (SmatZ) - Bug finder and fixer (0.6 - 1.3)",
413 " Christoph Mallon (Tron) - Programmer, code correctness police (0.3 - 0.5)",
414 " Patric Stout (TrueBrain) - NoAI, NoGo, Network (0.3 - 1.2), sys op (active)",
415 " Thijs Marinussen (Yexo) - AI Framework, General (0.6 - 1.3)",
416 "",
417 "Retired Developers:",
418 " Tam\xC3\xA1s Farag\xC3\xB3 (Darkvater) - Ex-Lead coder (0.3 - 0.5)",
419 " Dominik Scherer (dominik81) - Lead programmer, GUI expert (0.3 - 0.3)",
420 " Emil Djupfeld (egladil) - MacOSX (0.4.5 - 0.6)",
421 " Simon Sasburg (HackyKid) - Many bugfixes (0.4 - 0.4.5)",
422 " Ludvig Strigeus (ludde) - Original author of OpenTTD, main coder (0.1 - 0.3)",
423 " Cian Duffy (MYOB) - BeOS port / manual writing (0.1 - 0.3)",
424 " Petr Baudi\xC5\xA1 (pasky) - Many patches, NewGRF support (0.3 - 0.3)",
425 " Benedikt Br\xC3\xBCggemeier (skidd13) - Bug fixer and code reworker (0.6 - 0.7)",
426 " Serge Paquet (vurlix) - 2nd contributor after ludde (0.1 - 0.3)",
427 "",
428 "Special thanks go out to:",
429 " Josef Drexler - For his great work on TTDPatch",
430 " Marcin Grzegorczyk - Track foundations and for describing TTD internals",
431 " Stefan Mei\xC3\x9Fner (sign_de) - For his work on the console",
432 " Mike Ragsdale - OpenTTD installer",
433 " Christian Rosentreter (tokai) - MorphOS / AmigaOS port",
434 " Richard Kempton (richK) - additional airports, initial TGP implementation",
435 "",
436 " Alberto Demichelis - Squirrel scripting language \xC2\xA9 2003-2008",
437 " L. Peter Deutsch - MD5 implementation \xC2\xA9 1999, 2000, 2002",
438 " Michael Blunck - Pre-signals and semaphores \xC2\xA9 2003",
439 " George - Canal/Lock graphics \xC2\xA9 2003-2004",
440 " Andrew Parkhouse (andythenorth) - River graphics",
441 " David Dallaston (Pikka) - Tram tracks",
442 " All Translators - Who made OpenTTD a truly international game",
443 " Bug Reporters - Without whom OpenTTD would still be full of bugs!",
444 "",
445 "",
446 "And last but not least:",
447 " Chris Sawyer - For an amazing game!"
448 };
449
450 struct AboutWindow : public Window {
451 int text_position;
452 byte counter;
453 int line_height;
454 static const int num_visible_lines = 19;
455
456 AboutWindow() : Window(&_about_desc)
457 {
458 this->InitNested(WN_GAME_OPTIONS_ABOUT);
459
460 this->counter = 5;
461 this->text_position = this->GetWidget<NWidgetBase>(WID_A_SCROLLING_TEXT)->pos_y + this->GetWidget<NWidgetBase>(WID_A_SCROLLING_TEXT)->current_y;
462 }
463
464 virtual void SetStringParameters(int widget) const
465 {
466 if (widget == WID_A_WEBSITE) SetDParamStr(0, "Website: http://www.openttd.org");
467 }
468
469 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
470 {
471 if (widget != WID_A_SCROLLING_TEXT) return;
472
473 this->line_height = FONT_HEIGHT_NORMAL;
474
475 Dimension d;
476 d.height = this->line_height * num_visible_lines;
477
478 d.width = 0;
479 for (uint i = 0; i < lengthof(_credits); i++) {
480 d.width = max(d.width, GetStringBoundingBox(_credits[i]).width);
481 }
482 *size = maxdim(*size, d);
483 }
484
485 virtual void DrawWidget(const Rect &r, int widget) const
486 {
487 if (widget != WID_A_SCROLLING_TEXT) return;
488
489 int y = this->text_position;
490
491 /* Show all scrolling _credits */
492 for (uint i = 0; i < lengthof(_credits); i++) {
493 if (y >= r.top + 7 && y < r.bottom - this->line_height) {
494 DrawString(r.left, r.right, y, _credits[i], TC_BLACK, SA_LEFT | SA_FORCE);
495 }
496 y += this->line_height;
497 }
498 }
499
500 virtual void OnTick()
501 {
502 if (--this->counter == 0) {
503 this->counter = 5;
504 this->text_position--;
505 /* If the last text has scrolled start a new from the start */
506 if (this->text_position < (int)(this->GetWidget<NWidgetBase>(WID_A_SCROLLING_TEXT)->pos_y - lengthof(_credits) * this->line_height)) {
507 this->text_position = this->GetWidget<NWidgetBase>(WID_A_SCROLLING_TEXT)->pos_y + this->GetWidget<NWidgetBase>(WID_A_SCROLLING_TEXT)->current_y;
508 }
509 this->SetDirty();
510 }
511 }
512 };
513
514 void ShowAboutWindow()
515 {
516 DeleteWindowByClass(WC_GAME_OPTIONS);
517 new AboutWindow();
518 }
519
526 void ShowEstimatedCostOrIncome(Money cost, int x, int y)
527 {
528 StringID msg = STR_MESSAGE_ESTIMATED_COST;
529
530 if (cost < 0) {
531 cost = -cost;
532 msg = STR_MESSAGE_ESTIMATED_INCOME;
533 }
534 SetDParam(0, cost);
535 ShowErrorMessage(msg, INVALID_STRING_ID, WL_INFO, x, y);
536 }
537
545 void ShowCostOrIncomeAnimation(int x, int y, int z, Money cost)
546 {
547 Point pt = RemapCoords(x, y, z);
548 StringID msg = STR_INCOME_FLOAT_COST;
549
550 if (cost < 0) {
551 cost = -cost;
552 msg = STR_INCOME_FLOAT_INCOME;
553 }
554 SetDParam(0, cost);
555 AddTextEffect(msg, pt.x, pt.y, DAY_TICKS, TE_RISING);
556 }
557
566 void ShowFeederIncomeAnimation(int x, int y, int z, Money transfer, Money income)
567 {
568 Point pt = RemapCoords(x, y, z);
569
570 SetDParam(0, transfer);
571 if (income == 0) {
572 AddTextEffect(STR_FEEDER, pt.x, pt.y, DAY_TICKS, TE_RISING);
573 } else {
574 StringID msg = STR_FEEDER_COST;
575 if (income < 0) {
576 income = -income;
577 msg = STR_FEEDER_INCOME;
578 }
579 SetDParam(1, income);
580 AddTextEffect(msg, pt.x, pt.y, DAY_TICKS, TE_RISING);
581 }
582 }
583
593 TextEffectID ShowFillingPercent(int x, int y, int z, uint8 percent, StringID string)
594 {
595 Point pt = RemapCoords(x, y, z);
596
597 assert(string != STR_NULL);
598
599 SetDParam(0, percent);
600 return AddTextEffect(string, pt.x, pt.y, 0, TE_STATIC);
601 }
602
608 void UpdateFillingPercent(TextEffectID te_id, uint8 percent, StringID string)
609 {
610 assert(string != STR_NULL);
611
612 SetDParam(0, percent);
613 UpdateTextEffect(te_id, string);
614 }
615
620 void HideFillingPercent(TextEffectID *te_id)
621 {
622 if (*te_id == INVALID_TE_ID) return;
623
624 RemoveTextEffect(*te_id);
625 *te_id = INVALID_TE_ID;
626 }
627
628 static const NWidgetPart _nested_tooltips_widgets[] = {
629 NWidget(WWT_PANEL, COLOUR_GREY, WID_TT_BACKGROUND), SetMinimalSize(200, 32), EndContainer(),
630 };
631
632 static WindowDesc _tool_tips_desc(
633 WDP_MANUAL, NULL, 0, 0, // Coordinates and sizes are not used,
634 WC_TOOLTIPS, WC_NONE,
635 WDF_NO_FOCUS,
636 _nested_tooltips_widgets, lengthof(_nested_tooltips_widgets)
637 );
638
640 struct TooltipsWindow : public Window
641 {
642 StringID string_id;
643 byte paramcount;
644 uint64 params[5];
645 TooltipCloseCondition close_cond;
646
647 TooltipsWindow(Window *parent, StringID str, uint paramcount, const uint64 params[], TooltipCloseCondition close_tooltip) : Window(&_tool_tips_desc)
648 {
649 this->parent = parent;
650 this->string_id = str;
651 assert_compile(sizeof(this->params[0]) == sizeof(params[0]));
652 assert(paramcount <= lengthof(this->params));
653 memcpy(this->params, params, sizeof(this->params[0]) * paramcount);
654 this->paramcount = paramcount;
655 this->close_cond = close_tooltip;
656
657 this->InitNested();
658
659 CLRBITS(this->flags, WF_WHITE_BORDER);
660 }
661
662 virtual Point OnInitialPosition(int16 sm_width, int16 sm_height, int window_number)
663 {
664 /* Find the free screen space between the main toolbar at the top, and the statusbar at the bottom.
665 * Add a fixed distance 2 so the tooltip floats free from both bars.
666 */
667 int scr_top = GetMainViewTop() + 2;
668 int scr_bot = GetMainViewBottom() - 2;
669
670 Point pt;
671
672 /* Correctly position the tooltip position, watch out for window and cursor size
673 * Clamp value to below main toolbar and above statusbar. If tooltip would
674 * go below window, flip it so it is shown above the cursor */
675 pt.y = Clamp(_cursor.pos.y + _cursor.size.y + _cursor.offs.y + 5, scr_top, scr_bot);
676 if (pt.y + sm_height > scr_bot) pt.y = min(_cursor.pos.y + _cursor.offs.y - 5, scr_bot) - sm_height;
677 pt.x = sm_width >= _screen.width ? 0 : Clamp(_cursor.pos.x - (sm_width >> 1), 0, _screen.width - sm_width);
678
679 return pt;
680 }
681
682 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
683 {
684 /* There is only one widget. */
685 for (uint i = 0; i != this->paramcount; i++) SetDParam(i, this->params[i]);
686
687 size->width = min(GetStringBoundingBox(this->string_id).width, ScaleGUITrad(194));
688 size->height = GetStringHeight(this->string_id, size->width);
689
690 /* Increase slightly to have some space around the box. */
691 size->width += 2 + WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
692 size->height += 2 + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
693 }
694
695 virtual void DrawWidget(const Rect &r, int widget) const
696 {
697 /* There is only one widget. */
698 GfxFillRect(r.left, r.top, r.right, r.bottom, PC_BLACK);
699 GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, PC_LIGHT_YELLOW);
700
701 for (uint arg = 0; arg < this->paramcount; arg++) {
702 SetDParam(arg, this->params[arg]);
703 }
704 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);
705 }
706
707 virtual void OnMouseLoop()
708 {
709 /* Always close tooltips when the cursor is not in our window. */
710 if (!_cursor.in_window) {
711 delete this;
712 return;
713 }
714
715 /* We can show tooltips while dragging tools. These are shown as long as
716 * we are dragging the tool. Normal tooltips work with hover or rmb. */
717 switch (this->close_cond) {
718 case TCC_RIGHT_CLICK: if (!_right_button_down) delete this; break;
719 case TCC_LEFT_CLICK: if (!_left_button_down) delete this; break;
720 case TCC_HOVER: if (!_mouse_hovering) delete this; break;
721 }
722 }
723 };
724
733 void GuiShowTooltips(Window *parent, StringID str, uint paramcount, const uint64 params[], TooltipCloseCondition close_tooltip)
734 {
735 DeleteWindowById(WC_TOOLTIPS, 0);
736
737 if (str == STR_NULL) return;
738
739 new TooltipsWindow(parent, str, paramcount, params, close_tooltip);
740 }
741
742 void QueryString::HandleEditBox(Window *w, int wid)
743 {
744 if (w->IsWidgetGloballyFocused(wid) && this->text.HandleCaret()) {
745 w->SetWidgetDirty(wid);
746
747 /* For the OSK also invalidate the parent window */
748 if (w->window_class == WC_OSK) w->InvalidateData();
749 }
750 }
751
752 void QueryString::DrawEditBox(const Window *w, int wid) const
753 {
754 const NWidgetLeaf *wi = w->GetWidget<NWidgetLeaf>(wid);
755
756 assert((wi->type & WWT_MASK) == WWT_EDITBOX);
757
758 bool rtl = _current_text_dir == TD_RTL;
759 Dimension sprite_size = GetSpriteSize(rtl ? SPR_IMG_DELETE_RIGHT : SPR_IMG_DELETE_LEFT);
760 int clearbtn_width = sprite_size.width + WD_IMGBTN_LEFT + WD_IMGBTN_RIGHT;
761
762 int clearbtn_left = wi->pos_x + (rtl ? 0 : wi->current_x - clearbtn_width);
763 int clearbtn_right = wi->pos_x + (rtl ? clearbtn_width : wi->current_x) - 1;
764 int left = wi->pos_x + (rtl ? clearbtn_width : 0);
765 int right = wi->pos_x + (rtl ? wi->current_x : wi->current_x - clearbtn_width) - 1;
766
767 int top = wi->pos_y;
768 int bottom = wi->pos_y + wi->current_y - 1;
769
770 DrawFrameRect(clearbtn_left, top, clearbtn_right, bottom, wi->colour, wi->IsLowered() ? FR_LOWERED : FR_NONE);
771 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));
772 if (this->text.bytes == 1) GfxFillRect(clearbtn_left + 1, top + 1, clearbtn_right - 1, bottom - 1, _colour_gradient[wi->colour & 0xF][2], FILLRECT_CHECKER);
773
774 DrawFrameRect(left, top, right, bottom, wi->colour, FR_LOWERED | FR_DARKENED);
775 GfxFillRect(left + 1, top + 1, right - 1, bottom - 1, PC_BLACK);
776
777 /* Limit the drawing of the string inside the widget boundaries */
778 DrawPixelInfo dpi;
779 if (!FillDrawPixelInfo(&dpi, left + WD_FRAMERECT_LEFT, top + WD_FRAMERECT_TOP, right - left - WD_FRAMERECT_RIGHT, bottom - top - WD_FRAMERECT_BOTTOM)) return;
780
781 DrawPixelInfo *old_dpi = _cur_dpi;
782 _cur_dpi = &dpi;
783
784 /* We will take the current widget length as maximum width, with a small
785 * space reserved at the end for the caret to show */
786 const Textbuf *tb = &this->text;
787 int delta = min(0, (right - left) - tb->pixels - 10);
788
789 if (tb->caretxoffs + delta < 0) delta = -tb->caretxoffs;
790
791 /* If we have a marked area, draw a background highlight. */
792 if (tb->marklength != 0) GfxFillRect(delta + tb->markxoffs, 0, delta + tb->markxoffs + tb->marklength - 1, bottom - top, PC_GREY);
793
794 DrawString(delta, tb->pixels, 0, tb->buf, TC_YELLOW);
795 bool focussed = w->IsWidgetGloballyFocused(wid) || IsOSKOpenedFor(w, wid);
796 if (focussed && tb->caret) {
797 int caret_width = GetStringBoundingBox("_").width;
798 DrawString(tb->caretxoffs + delta, tb->caretxoffs + delta + caret_width, 0, "_", TC_WHITE);
799 }
800
801 _cur_dpi = old_dpi;
802 }
803
810 Point QueryString::GetCaretPosition(const Window *w, int wid) const
811 {
812 const NWidgetLeaf *wi = w->GetWidget<NWidgetLeaf>(wid);
813
814 assert((wi->type & WWT_MASK) == WWT_EDITBOX);
815
816 bool rtl = _current_text_dir == TD_RTL;
817 Dimension sprite_size = GetSpriteSize(rtl ? SPR_IMG_DELETE_RIGHT : SPR_IMG_DELETE_LEFT);
818 int clearbtn_width = sprite_size.width + WD_IMGBTN_LEFT + WD_IMGBTN_RIGHT;
819
820 int left = wi->pos_x + (rtl ? clearbtn_width : 0);
821 int right = wi->pos_x + (rtl ? wi->current_x : wi->current_x - clearbtn_width) - 1;
822
823 /* Clamp caret position to be inside out current width. */
824 const Textbuf *tb = &this->text;
825 int delta = min(0, (right - left) - tb->pixels - 10);
826 if (tb->caretxoffs + delta < 0) delta = -tb->caretxoffs;
827
828 Point pt = {left + WD_FRAMERECT_LEFT + tb->caretxoffs + delta, (int)wi->pos_y + WD_FRAMERECT_TOP};
829 return pt;
830 }
831
840 Rect QueryString::GetBoundingRect(const Window *w, int wid, const char *from, const char *to) const
841 {
842 const NWidgetLeaf *wi = w->GetWidget<NWidgetLeaf>(wid);
843
844 assert((wi->type & WWT_MASK) == WWT_EDITBOX);
845
846 bool rtl = _current_text_dir == TD_RTL;
847 Dimension sprite_size = GetSpriteSize(rtl ? SPR_IMG_DELETE_RIGHT : SPR_IMG_DELETE_LEFT);
848 int clearbtn_width = sprite_size.width + WD_IMGBTN_LEFT + WD_IMGBTN_RIGHT;
849
850 int left = wi->pos_x + (rtl ? clearbtn_width : 0);
851 int right = wi->pos_x + (rtl ? wi->current_x : wi->current_x - clearbtn_width) - 1;
852
853 int top = wi->pos_y + WD_FRAMERECT_TOP;
854 int bottom = wi->pos_y + wi->current_y - 1 - WD_FRAMERECT_BOTTOM;
855
856 /* Clamp caret position to be inside our current width. */
857 const Textbuf *tb = &this->text;
858 int delta = min(0, (right - left) - tb->pixels - 10);
859 if (tb->caretxoffs + delta < 0) delta = -tb->caretxoffs;
860
861 /* Get location of first and last character. */
862 Point p1 = GetCharPosInString(tb->buf, from, FS_NORMAL);
863 Point p2 = from != to ? GetCharPosInString(tb->buf, to, FS_NORMAL) : p1;
864
865 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 };
866
867 return r;
868 }
869
877 const char *QueryString::GetCharAtPosition(const Window *w, int wid, const Point &pt) const
878 {
879 const NWidgetLeaf *wi = w->GetWidget<NWidgetLeaf>(wid);
880
881 assert((wi->type & WWT_MASK) == WWT_EDITBOX);
882
883 bool rtl = _current_text_dir == TD_RTL;
884 Dimension sprite_size = GetSpriteSize(rtl ? SPR_IMG_DELETE_RIGHT : SPR_IMG_DELETE_LEFT);
885 int clearbtn_width = sprite_size.width + WD_IMGBTN_LEFT + WD_IMGBTN_RIGHT;
886
887 int left = wi->pos_x + (rtl ? clearbtn_width : 0);
888 int right = wi->pos_x + (rtl ? wi->current_x : wi->current_x - clearbtn_width) - 1;
889
890 int top = wi->pos_y + WD_FRAMERECT_TOP;
891 int bottom = wi->pos_y + wi->current_y - 1 - WD_FRAMERECT_BOTTOM;
892
893 if (!IsInsideMM(pt.y, top, bottom)) return NULL;
894
895 /* Clamp caret position to be inside our current width. */
896 const Textbuf *tb = &this->text;
897 int delta = min(0, (right - left) - tb->pixels - 10);
898 if (tb->caretxoffs + delta < 0) delta = -tb->caretxoffs;
899
900 return ::GetCharAtPosition(tb->buf, pt.x - delta - left);
901 }
902
903 void QueryString::ClickEditBox(Window *w, Point pt, int wid, int click_count, bool focus_changed)
904 {
905 const NWidgetLeaf *wi = w->GetWidget<NWidgetLeaf>(wid);
906
907 assert((wi->type & WWT_MASK) == WWT_EDITBOX);
908
909 bool rtl = _current_text_dir == TD_RTL;
910 int clearbtn_width = GetSpriteSize(rtl ? SPR_IMG_DELETE_RIGHT : SPR_IMG_DELETE_LEFT).width;
911
912 int clearbtn_left = wi->pos_x + (rtl ? 0 : wi->current_x - clearbtn_width);
913
914 if (IsInsideBS(pt.x, clearbtn_left, clearbtn_width)) {
915 if (this->text.bytes > 1) {
916 this->text.DeleteAll();
917 w->HandleButtonClick(wid);
918 w->OnEditboxChanged(wid);
919 }
920 return;
921 }
922
923 if (w->window_class != WC_OSK && _settings_client.gui.osk_activation != OSKA_DISABLED &&
924 (!focus_changed || _settings_client.gui.osk_activation == OSKA_IMMEDIATELY) &&
925 (click_count == 2 || _settings_client.gui.osk_activation != OSKA_DOUBLE_CLICK)) {
926 /* Open the OSK window */
927 ShowOnScreenKeyboard(w, wid);
928 }
929 }
930
932 struct QueryStringWindow : public Window
933 {
934 QueryString editbox;
935 QueryStringFlags flags;
936
937 QueryStringWindow(StringID str, StringID caption, uint max_bytes, uint max_chars, WindowDesc *desc, Window *parent, CharSetFilter afilter, QueryStringFlags flags) :
938 Window(desc), editbox(max_bytes, max_chars)
939 {
940 char *last_of = &this->editbox.text.buf[this->editbox.text.max_bytes - 1];
941 GetString(this->editbox.text.buf, str, last_of);
942 str_validate(this->editbox.text.buf, last_of, SVS_NONE);
943
944 /* Make sure the name isn't too long for the text buffer in the number of
945 * characters (not bytes). max_chars also counts the '\0' characters. */
946 while (Utf8StringLength(this->editbox.text.buf) + 1 > this->editbox.text.max_chars) {
947 *Utf8PrevChar(this->editbox.text.buf + strlen(this->editbox.text.buf)) = '\0';
948 }
949
950 this->editbox.text.UpdateSize();
951
952 if ((flags & QSF_ACCEPT_UNCHANGED) == 0) this->editbox.orig = stredup(this->editbox.text.buf);
953
954 this->querystrings[WID_QS_TEXT] = &this->editbox;
955 this->editbox.caption = caption;
956 this->editbox.cancel_button = WID_QS_CANCEL;
957 this->editbox.ok_button = WID_QS_OK;
958 this->editbox.text.afilter = afilter;
959 this->flags = flags;
960
961 this->InitNested(WN_QUERY_STRING);
962
963 this->parent = parent;
964
965 this->SetFocusedWidget(WID_QS_TEXT);
966 }
967
968 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
969 {
970 if (widget == WID_QS_DEFAULT && (this->flags & QSF_ENABLE_DEFAULT) == 0) {
971 /* We don't want this widget to show! */
972 fill->width = 0;
973 resize->width = 0;
974 size->width = 0;
975 }
976 }
977
978 virtual void SetStringParameters(int widget) const
979 {
980 if (widget == WID_QS_CAPTION) SetDParam(0, this->editbox.caption);
981 }
982
983 void OnOk()
984 {
985 if (this->editbox.orig == NULL || strcmp(this->editbox.text.buf, this->editbox.orig) != 0) {
986 /* If the parent is NULL, the editbox is handled by general function
987 * HandleOnEditText */
988 if (this->parent != NULL) {
989 this->parent->OnQueryTextFinished(this->editbox.text.buf);
990 } else {
991 HandleOnEditText(this->editbox.text.buf);
992 }
993 this->editbox.handled = true;
994 }
995 }
996
997 virtual void OnClick(Point pt, int widget, int click_count)
998 {
999 switch (widget) {
1000 case WID_QS_DEFAULT:
1001 this->editbox.text.DeleteAll();
1002 /* FALL THROUGH */
1003 case WID_QS_OK:
1004 this->OnOk();
1005 /* FALL THROUGH */
1006 case WID_QS_CANCEL:
1007 delete this;
1008 break;
1009 }
1010 }
1011
1012 ~QueryStringWindow()
1013 {
1014 if (!this->editbox.handled && this->parent != NULL) {
1015 Window *parent = this->parent;
1016 this->parent = NULL; // so parent doesn't try to delete us again
1017 parent->OnQueryTextFinished(NULL);
1018 }
1019 }
1020 };
1021
1022 static const NWidgetPart _nested_query_string_widgets[] = {
1023 NWidget(NWID_HORIZONTAL),
1024 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
1025 NWidget(WWT_CAPTION, COLOUR_GREY, WID_QS_CAPTION), SetDataTip(STR_WHITE_STRING, STR_NULL),
1026 EndContainer(),
1027 NWidget(WWT_PANEL, COLOUR_GREY),
1028 NWidget(WWT_EDITBOX, COLOUR_GREY, WID_QS_TEXT), SetMinimalSize(256, 12), SetFill(1, 1), SetPadding(2, 2, 2, 2),
1029 EndContainer(),
1030 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
1031 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_QS_DEFAULT), SetMinimalSize(87, 12), SetFill(1, 1), SetDataTip(STR_BUTTON_DEFAULT, STR_NULL),
1032 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_QS_CANCEL), SetMinimalSize(86, 12), SetFill(1, 1), SetDataTip(STR_BUTTON_CANCEL, STR_NULL),
1033 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_QS_OK), SetMinimalSize(87, 12), SetFill(1, 1), SetDataTip(STR_BUTTON_OK, STR_NULL),
1034 EndContainer(),
1035 };
1036
1037 static WindowDesc _query_string_desc(
1038 WDP_CENTER, "query_string", 0, 0,
1039 WC_QUERY_STRING, WC_NONE,
1040 0,
1041 _nested_query_string_widgets, lengthof(_nested_query_string_widgets)
1042 );
1043
1054 void ShowQueryString(StringID str, StringID caption, uint maxsize, Window *parent, CharSetFilter afilter, QueryStringFlags flags)
1055 {
1056 DeleteWindowByClass(WC_QUERY_STRING);
1057 new QueryStringWindow(str, caption, ((flags & QSF_LEN_IN_CHARS) ? MAX_CHAR_LENGTH : 1) * maxsize, maxsize, &_query_string_desc, parent, afilter, flags);
1058 }
1059
1063 struct QueryWindow : public Window {
1064 QueryCallbackProc *proc;
1065 uint64 params[10];
1066 StringID message;
1067 StringID caption;
1068
1069 QueryWindow(WindowDesc *desc, StringID caption, StringID message, Window *parent, QueryCallbackProc *callback) : Window(desc)
1070 {
1071 /* Create a backup of the variadic arguments to strings because it will be
1072 * overridden pretty often. We will copy these back for drawing */
1073 CopyOutDParam(this->params, 0, lengthof(this->params));
1074 this->caption = caption;
1075 this->message = message;
1076 this->proc = callback;
1077
1078 this->InitNested(WN_CONFIRM_POPUP_QUERY);
1079
1080 this->parent = parent;
1081 this->left = parent->left + (parent->width / 2) - (this->width / 2);
1082 this->top = parent->top + (parent->height / 2) - (this->height / 2);
1083 }
1084
1085 ~QueryWindow()
1086 {
1087 if (this->proc != NULL) this->proc(this->parent, false);
1088 }
1089
1090 virtual void SetStringParameters(int widget) const
1091 {
1092 switch (widget) {
1093 case WID_Q_CAPTION:
1094 CopyInDParam(1, this->params, lengthof(this->params));
1095 SetDParam(0, this->caption);
1096 break;
1097
1098 case WID_Q_TEXT:
1099 CopyInDParam(0, this->params, lengthof(this->params));
1100 break;
1101 }
1102 }
1103
1104 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
1105 {
1106 if (widget != WID_Q_TEXT) return;
1107
1108 Dimension d = GetStringMultiLineBoundingBox(this->message, *size);
1109 d.width += WD_FRAMETEXT_LEFT + WD_FRAMETEXT_RIGHT;
1110 d.height += WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
1111 *size = d;
1112 }
1113
1114 virtual void DrawWidget(const Rect &r, int widget) const
1115 {
1116 if (widget != WID_Q_TEXT) return;
1117
1118 DrawStringMultiLine(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, r.top + WD_FRAMERECT_TOP, r.bottom - WD_FRAMERECT_BOTTOM,
1119 this->message, TC_FROMSTRING, SA_CENTER);
1120 }
1121
1122 virtual void OnClick(Point pt, int widget, int click_count)
1123 {
1124 switch (widget) {
1125 case WID_Q_YES: {
1126 /* in the Generate New World window, clicking 'Yes' causes
1127 * DeleteNonVitalWindows() to be called - we shouldn't be in a window then */
1128 QueryCallbackProc *proc = this->proc;
1129 Window *parent = this->parent;
1130 /* Prevent the destructor calling the callback function */
1131 this->proc = NULL;
1132 delete this;
1133 if (proc != NULL) {
1134 proc(parent, true);
1135 proc = NULL;
1136 }
1137 break;
1138 }
1139 case WID_Q_NO:
1140 delete this;
1141 break;
1142 }
1143 }
1144
1145 virtual EventState OnKeyPress(WChar key, uint16 keycode)
1146 {
1147 /* ESC closes the window, Enter confirms the action */
1148 switch (keycode) {
1149 case WKC_RETURN:
1150 case WKC_NUM_ENTER:
1151 if (this->proc != NULL) {
1152 this->proc(this->parent, true);
1153 this->proc = NULL;
1154 }
1155 /* FALL THROUGH */
1156 case WKC_ESC:
1157 delete this;
1158 return ES_HANDLED;
1159 }
1160 return ES_NOT_HANDLED;
1161 }
1162 };
1163
1164 static const NWidgetPart _nested_query_widgets[] = {
1165 NWidget(NWID_HORIZONTAL),
1166 NWidget(WWT_CLOSEBOX, COLOUR_RED),
1167 NWidget(WWT_CAPTION, COLOUR_RED, WID_Q_CAPTION), SetDataTip(STR_JUST_STRING, STR_NULL),
1168 EndContainer(),
1169 NWidget(WWT_PANEL, COLOUR_RED), SetPIP(8, 15, 8),
1170 NWidget(WWT_TEXT, COLOUR_RED, WID_Q_TEXT), SetMinimalSize(200, 12),
1171 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(20, 29, 20),
1172 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_Q_NO), SetMinimalSize(71, 12), SetFill(1, 1), SetDataTip(STR_QUIT_NO, STR_NULL),
1173 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, WID_Q_YES), SetMinimalSize(71, 12), SetFill(1, 1), SetDataTip(STR_QUIT_YES, STR_NULL),
1174 EndContainer(),
1175 EndContainer(),
1176 };
1177
1178 static WindowDesc _query_desc(
1179 WDP_CENTER, NULL, 0, 0,
1180 WC_CONFIRM_POPUP_QUERY, WC_NONE,
1181 WDF_MODAL,
1182 _nested_query_widgets, lengthof(_nested_query_widgets)
1183 );
1184
1194 void ShowQuery(StringID caption, StringID message, Window *parent, QueryCallbackProc *callback)
1195 {
1196 if (parent == NULL) parent = FindWindowById(WC_MAIN_WINDOW, 0);
1197
1198 const Window *w;
1199 FOR_ALL_WINDOWS_FROM_BACK(w) {
1200 if (w->window_class != WC_CONFIRM_POPUP_QUERY) continue;
1201
1202 const QueryWindow *qw = (const QueryWindow *)w;
1203 if (qw->parent != parent || qw->proc != callback) continue;
1204
1205 delete qw;
1206 break;
1207 }
1208
1209 new QueryWindow(&_query_desc, caption, message, parent, callback);
1210 }