00001
00002
00003
00004
00005
00006
00007
00008
00009
00012 #include "stdafx.h"
00013 #include "debug.h"
00014 #include "landscape.h"
00015 #include "newgrf_text.h"
00016 #include "gui.h"
00017 #include "viewport_func.h"
00018 #include "gfx_func.h"
00019 #include "command_func.h"
00020 #include "company_func.h"
00021 #include "town.h"
00022 #include "string_func.h"
00023 #include "company_base.h"
00024 #include "texteff.hpp"
00025 #include "company_manager_face.h"
00026 #include "strings_func.h"
00027 #include "zoom_func.h"
00028 #include "window_func.h"
00029 #include "querystring_gui.h"
00030 #include "console_func.h"
00031 #include "core/geometry_func.hpp"
00032 #include "newgrf_debug.h"
00033
00034 #include "table/strings.h"
00035
00042 bool GetClipboardContents(char *buffer, size_t buff_len);
00043
00044 int _caret_timer;
00045
00046
00048 enum LandInfoWidgets {
00049 LIW_BACKGROUND,
00050 };
00051
00052 static const NWidgetPart _nested_land_info_widgets[] = {
00053 NWidget(NWID_HORIZONTAL),
00054 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
00055 NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_LAND_AREA_INFORMATION_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00056 NWidget(WWT_DEBUGBOX, COLOUR_GREY),
00057 EndContainer(),
00058 NWidget(WWT_PANEL, COLOUR_GREY, LIW_BACKGROUND), EndContainer(),
00059 };
00060
00061 static const WindowDesc _land_info_desc(
00062 WDP_AUTO, 0, 0,
00063 WC_LAND_INFO, WC_NONE,
00064 0,
00065 _nested_land_info_widgets, lengthof(_nested_land_info_widgets)
00066 );
00067
00068 class LandInfoWindow : public Window {
00069 enum LandInfoLines {
00070 LAND_INFO_CENTERED_LINES = 12,
00071 LAND_INFO_MULTICENTER_LINE = LAND_INFO_CENTERED_LINES,
00072 LAND_INFO_LINE_END,
00073 };
00074
00075 static const uint LAND_INFO_LINE_BUFF_SIZE = 512;
00076
00077 public:
00078 char landinfo_data[LAND_INFO_LINE_END][LAND_INFO_LINE_BUFF_SIZE];
00079 TileIndex tile;
00080
00081 virtual void DrawWidget(const Rect &r, int widget) const
00082 {
00083 if (widget != LIW_BACKGROUND) return;
00084
00085 uint y = r.top + WD_TEXTPANEL_TOP;
00086 for (uint i = 0; i < LAND_INFO_CENTERED_LINES; i++) {
00087 if (StrEmpty(this->landinfo_data[i])) break;
00088
00089 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);
00090 y += FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL;
00091 if (i == 0) y += 4;
00092 }
00093
00094 if (!StrEmpty(this->landinfo_data[LAND_INFO_MULTICENTER_LINE])) {
00095 SetDParamStr(0, this->landinfo_data[LAND_INFO_MULTICENTER_LINE]);
00096 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);
00097 }
00098 }
00099
00100 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00101 {
00102 if (widget != LIW_BACKGROUND) return;
00103
00104 size->height = WD_TEXTPANEL_TOP + WD_TEXTPANEL_BOTTOM;
00105 for (uint i = 0; i < LAND_INFO_CENTERED_LINES; i++) {
00106 if (StrEmpty(this->landinfo_data[i])) break;
00107
00108 uint width = GetStringBoundingBox(this->landinfo_data[i]).width + WD_FRAMETEXT_LEFT + WD_FRAMETEXT_RIGHT;
00109 size->width = max(size->width, width);
00110
00111 size->height += FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL;
00112 if (i == 0) size->height += 4;
00113 }
00114
00115 if (!StrEmpty(this->landinfo_data[LAND_INFO_MULTICENTER_LINE])) {
00116 uint width = GetStringBoundingBox(this->landinfo_data[LAND_INFO_MULTICENTER_LINE]).width + WD_FRAMETEXT_LEFT + WD_FRAMETEXT_RIGHT;
00117 size->width = max(size->width, min(300u, width));
00118 SetDParamStr(0, this->landinfo_data[LAND_INFO_MULTICENTER_LINE]);
00119 size->height += GetStringHeight(STR_JUST_RAW_STRING, size->width - WD_FRAMETEXT_LEFT - WD_FRAMETEXT_RIGHT);
00120 }
00121 }
00122
00123 LandInfoWindow(TileIndex tile) : Window(), tile(tile) {
00124 Town *t = ClosestTownFromTile(tile, _settings_game.economy.dist_local_authority);
00125
00126
00127 TileDesc td;
00128
00129 td.build_date = INVALID_DATE;
00130
00131
00132
00133
00134
00135 td.owner_type[0] = STR_LAND_AREA_INFORMATION_OWNER;
00136 td.owner_type[1] = STR_NULL;
00137 td.owner_type[2] = STR_NULL;
00138 td.owner_type[3] = STR_NULL;
00139 td.owner[0] = OWNER_NONE;
00140 td.owner[1] = OWNER_NONE;
00141 td.owner[2] = OWNER_NONE;
00142 td.owner[3] = OWNER_NONE;
00143
00144 td.station_class = STR_NULL;
00145 td.station_name = STR_NULL;
00146 td.airport_class = STR_NULL;
00147 td.airport_name = STR_NULL;
00148 td.airport_tile_name = STR_NULL;
00149 td.rail_speed = 0;
00150
00151 td.grf = NULL;
00152
00153 CargoArray acceptance;
00154 AddAcceptedCargo(tile, acceptance, NULL);
00155 GetTileDesc(tile, &td);
00156
00157 uint line_nr = 0;
00158
00159
00160 SetDParam(0, td.dparam[0]);
00161 GetString(this->landinfo_data[line_nr], td.str, lastof(this->landinfo_data[line_nr]));
00162 line_nr++;
00163
00164
00165 for (uint i = 0; i < 4; i++) {
00166 if (td.owner_type[i] == STR_NULL) continue;
00167
00168 SetDParam(0, STR_LAND_AREA_INFORMATION_OWNER_N_A);
00169 if (td.owner[i] != OWNER_NONE && td.owner[i] != OWNER_WATER) GetNameOfOwner(td.owner[i], tile);
00170 GetString(this->landinfo_data[line_nr], td.owner_type[i], lastof(this->landinfo_data[line_nr]));
00171 line_nr++;
00172 }
00173
00174
00175 StringID str = STR_LAND_AREA_INFORMATION_COST_TO_CLEAR_N_A;
00176 Company *c = Company::GetIfValid(_local_company);
00177 if (c != NULL) {
00178 Money old_money = c->money;
00179 c->money = INT64_MAX;
00180 CommandCost costclear = DoCommand(tile, 0, 0, DC_NONE, CMD_LANDSCAPE_CLEAR);
00181 c->money = old_money;
00182 if (costclear.Succeeded()) {
00183 Money cost = costclear.GetCost();
00184 if (cost < 0) {
00185 cost = -cost;
00186 str = STR_LAND_AREA_INFORMATION_REVENUE_WHEN_CLEARED;
00187 } else {
00188 str = STR_LAND_AREA_INFORMATION_COST_TO_CLEAR;
00189 }
00190 SetDParam(0, cost);
00191 }
00192 }
00193 GetString(this->landinfo_data[line_nr], str, lastof(this->landinfo_data[line_nr]));
00194 line_nr++;
00195
00196
00197 char tmp[16];
00198 snprintf(tmp, lengthof(tmp), "0x%.4X", tile);
00199 SetDParam(0, TileX(tile));
00200 SetDParam(1, TileY(tile));
00201 SetDParam(2, GetTileZ(tile) / TILE_HEIGHT);
00202 SetDParamStr(3, tmp);
00203 GetString(this->landinfo_data[line_nr], STR_LAND_AREA_INFORMATION_LANDINFO_COORDS, lastof(this->landinfo_data[line_nr]));
00204 line_nr++;
00205
00206
00207 SetDParam(0, STR_LAND_AREA_INFORMATION_LOCAL_AUTHORITY_NONE);
00208 if (t != NULL) {
00209 SetDParam(0, STR_TOWN_NAME);
00210 SetDParam(1, t->index);
00211 }
00212 GetString(this->landinfo_data[line_nr], STR_LAND_AREA_INFORMATION_LOCAL_AUTHORITY, lastof(this->landinfo_data[line_nr]));
00213 line_nr++;
00214
00215
00216 if (td.build_date != INVALID_DATE) {
00217 SetDParam(0, td.build_date);
00218 GetString(this->landinfo_data[line_nr], STR_LAND_AREA_INFORMATION_BUILD_DATE, lastof(this->landinfo_data[line_nr]));
00219 line_nr++;
00220 }
00221
00222
00223 if (td.station_class != STR_NULL) {
00224 SetDParam(0, td.station_class);
00225 GetString(this->landinfo_data[line_nr], STR_LAND_AREA_INFORMATION_STATION_CLASS, lastof(this->landinfo_data[line_nr]));
00226 line_nr++;
00227 }
00228
00229
00230 if (td.station_name != STR_NULL) {
00231 SetDParam(0, td.station_name);
00232 GetString(this->landinfo_data[line_nr], STR_LAND_AREA_INFORMATION_STATION_TYPE, lastof(this->landinfo_data[line_nr]));
00233 line_nr++;
00234 }
00235
00236
00237 if (td.airport_class != STR_NULL) {
00238 SetDParam(0, td.airport_class);
00239 GetString(this->landinfo_data[line_nr], STR_LAND_AREA_INFORMATION_AIRPORT_CLASS, lastof(this->landinfo_data[line_nr]));
00240 line_nr++;
00241 }
00242
00243
00244 if (td.airport_name != STR_NULL) {
00245 SetDParam(0, td.airport_name);
00246 GetString(this->landinfo_data[line_nr], STR_LAND_AREA_INFORMATION_AIRPORT_NAME, lastof(this->landinfo_data[line_nr]));
00247 line_nr++;
00248 }
00249
00250
00251 if (td.airport_tile_name != STR_NULL) {
00252 SetDParam(0, td.airport_tile_name);
00253 GetString(this->landinfo_data[line_nr], STR_LAND_AREA_INFORMATION_AIRPORTTILE_NAME, lastof(this->landinfo_data[line_nr]));
00254 line_nr++;
00255 }
00256
00257
00258 if (td.rail_speed != 0) {
00259 SetDParam(0, td.rail_speed);
00260 GetString(this->landinfo_data[line_nr], STR_LANG_AREA_INFORMATION_RAIL_SPEED_LIMIT, lastof(this->landinfo_data[line_nr]));
00261 line_nr++;
00262 }
00263
00264
00265 if (td.grf != NULL) {
00266 SetDParamStr(0, td.grf);
00267 GetString(this->landinfo_data[line_nr], STR_LAND_AREA_INFORMATION_NEWGRF_NAME, lastof(this->landinfo_data[line_nr]));
00268 line_nr++;
00269 }
00270
00271 assert(line_nr < LAND_INFO_CENTERED_LINES);
00272
00273
00274 this->landinfo_data[line_nr][0] = '\0';
00275
00276
00277 char *strp = GetString(this->landinfo_data[LAND_INFO_MULTICENTER_LINE], STR_LAND_AREA_INFORMATION_CARGO_ACCEPTED, lastof(this->landinfo_data[LAND_INFO_MULTICENTER_LINE]));
00278 bool found = false;
00279
00280 for (CargoID i = 0; i < NUM_CARGO; ++i) {
00281 if (acceptance[i] > 0) {
00282
00283 if (found) {
00284 *strp++ = ',';
00285 *strp++ = ' ';
00286 }
00287 found = true;
00288
00289
00290 if (acceptance[i] < 8) {
00291 SetDParam(0, acceptance[i]);
00292 SetDParam(1, CargoSpec::Get(i)->name);
00293 strp = GetString(strp, STR_LAND_AREA_INFORMATION_CARGO_EIGHTS, lastof(this->landinfo_data[LAND_INFO_MULTICENTER_LINE]));
00294 } else {
00295 strp = GetString(strp, CargoSpec::Get(i)->name, lastof(this->landinfo_data[LAND_INFO_MULTICENTER_LINE]));
00296 }
00297 }
00298 }
00299 if (!found) this->landinfo_data[LAND_INFO_MULTICENTER_LINE][0] = '\0';
00300
00301 this->InitNested(&_land_info_desc);
00302
00303 #if defined(_DEBUG)
00304 # define LANDINFOD_LEVEL 0
00305 #else
00306 # define LANDINFOD_LEVEL 1
00307 #endif
00308 DEBUG(misc, LANDINFOD_LEVEL, "TILE: %#x (%i,%i)", tile, TileX(tile), TileY(tile));
00309 DEBUG(misc, LANDINFOD_LEVEL, "type_height = %#x", _m[tile].type_height);
00310 DEBUG(misc, LANDINFOD_LEVEL, "m1 = %#x", _m[tile].m1);
00311 DEBUG(misc, LANDINFOD_LEVEL, "m2 = %#x", _m[tile].m2);
00312 DEBUG(misc, LANDINFOD_LEVEL, "m3 = %#x", _m[tile].m3);
00313 DEBUG(misc, LANDINFOD_LEVEL, "m4 = %#x", _m[tile].m4);
00314 DEBUG(misc, LANDINFOD_LEVEL, "m5 = %#x", _m[tile].m5);
00315 DEBUG(misc, LANDINFOD_LEVEL, "m6 = %#x", _m[tile].m6);
00316 DEBUG(misc, LANDINFOD_LEVEL, "m7 = %#x", _me[tile].m7);
00317 #undef LANDINFOD_LEVEL
00318 }
00319
00320 virtual bool IsNewGRFInspectable() const
00321 {
00322 return ::IsNewGRFInspectable(GetGrfSpecFeature(this->tile), this->tile);
00323 }
00324
00325 virtual void ShowNewGRFInspectWindow() const
00326 {
00327 ::ShowNewGRFInspectWindow(GetGrfSpecFeature(this->tile), this->tile);
00328 }
00329 };
00330
00331 void ShowLandInfo(TileIndex tile)
00332 {
00333 DeleteWindowById(WC_LAND_INFO, 0);
00334 new LandInfoWindow(tile);
00335 }
00336
00338 enum AboutWidgets {
00339 AW_SCROLLING_TEXT,
00340 AW_WEBSITE,
00341 };
00342
00343 static const NWidgetPart _nested_about_widgets[] = {
00344 NWidget(NWID_HORIZONTAL),
00345 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
00346 NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_ABOUT_OPENTTD, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00347 EndContainer(),
00348 NWidget(WWT_PANEL, COLOUR_GREY), SetPIP(4, 2, 4),
00349 NWidget(WWT_LABEL, COLOUR_GREY), SetDataTip(STR_ABOUT_ORIGINAL_COPYRIGHT, STR_NULL),
00350 NWidget(WWT_LABEL, COLOUR_GREY), SetDataTip(STR_ABOUT_VERSION, STR_NULL),
00351 NWidget(WWT_FRAME, COLOUR_GREY), SetPadding(0, 5, 1, 5),
00352 NWidget(WWT_EMPTY, INVALID_COLOUR, AW_SCROLLING_TEXT),
00353 EndContainer(),
00354 NWidget(WWT_LABEL, COLOUR_GREY, AW_WEBSITE), SetDataTip(STR_BLACK_RAW_STRING, STR_NULL),
00355 NWidget(WWT_LABEL, COLOUR_GREY), SetDataTip(STR_ABOUT_COPYRIGHT_OPENTTD, STR_NULL),
00356 EndContainer(),
00357 };
00358
00359 static const WindowDesc _about_desc(
00360 WDP_CENTER, 0, 0,
00361 WC_GAME_OPTIONS, WC_NONE,
00362 0,
00363 _nested_about_widgets, lengthof(_nested_about_widgets)
00364 );
00365
00366 static const char * const _credits[] = {
00367 "Original design by Chris Sawyer",
00368 "Original graphics by Simon Foster",
00369 "",
00370 "The OpenTTD team (in alphabetical order):",
00371 " Albert Hofkamp (Alberth) - GUI expert",
00372 " Jean-Fran\xC3\xA7ois Claeys (Belugas) - GUI, newindustries and more",
00373 " Matthijs Kooijman (blathijs) - Pathfinder-guru, pool rework",
00374 " Christoph Elsenhans (frosch) - General coding",
00375 " Lo\xC3\xAF""c Guilloux (glx) - Windows Expert",
00376 " Michael Lutz (michi_cc) - Path based signals",
00377 " Owen Rudge (orudge) - Forum host, OS/2 port",
00378 " Peter Nelson (peter1138) - Spiritual descendant from NewGRF gods",
00379 " Ingo von Borstel (planetmaker) - Support",
00380 " Remko Bijker (Rubidium) - Lead coder and way more",
00381 " Zden\xC4\x9Bk Sojka (SmatZ) - Bug finder and fixer",
00382 " Jos\xC3\xA9 Soler (Terkhen) - General coding",
00383 " Thijs Marinussen (Yexo) - AI Framework",
00384 "",
00385 "Inactive Developers:",
00386 " Bjarni Corfitzen (Bjarni) - MacOSX port, coder and vehicles",
00387 " Victor Fischer (Celestar) - Programming everywhere you need him to",
00388 " Tam\xC3\xA1s Farag\xC3\xB3 (Darkvater) - Ex-Lead coder",
00389 " Jaroslav Mazanec (KUDr) - YAPG (Yet Another Pathfinder God) ;)",
00390 " Jonathan Coome (Maedhros) - High priest of the NewGRF Temple",
00391 " Attila B\xC3\xA1n (MiHaMiX) - Developer WebTranslator 1 and 2",
00392 " Christoph Mallon (Tron) - Programmer, code correctness police",
00393 "",
00394 "Retired Developers:",
00395 " Ludvig Strigeus (ludde) - OpenTTD author, main coder (0.1 - 0.3.3)",
00396 " Serge Paquet (vurlix) - Assistant project manager, coder (0.1 - 0.3.3)",
00397 " Dominik Scherer (dominik81) - Lead programmer, GUI expert (0.3.0 - 0.3.6)",
00398 " Benedikt Br\xC3\xBCggemeier (skidd13) - Bug fixer and code reworker",
00399 " Patric Stout (TrueLight) - Programmer (0.3 - pre0.7), sys op (active)",
00400 "",
00401 "Special thanks go out to:",
00402 " Josef Drexler - For his great work on TTDPatch",
00403 " Marcin Grzegorczyk - For describing Transport Tycoon Deluxe internals",
00404 " Petr Baudi\xC5\xA1 (pasky) - Many patches, newGRF support",
00405 " Stefan Mei\xC3\x9Fner (sign_de) - For his work on the console",
00406 " Simon Sasburg (HackyKid) - Many bugfixes he has blessed us with",
00407 " Cian Duffy (MYOB) - BeOS port / manual writing",
00408 " Christian Rosentreter (tokai) - MorphOS / AmigaOS port",
00409 " Richard Kempton (richK) - additional airports, initial TGP implementation",
00410 "",
00411 " Alberto Demichelis - Squirrel scripting language \xC2\xA9 2003-2008",
00412 " L. Peter Deutsch - MD5 implementation \xC2\xA9 1999, 2000, 2002",
00413 " Michael Blunck - Pre-Signals and Semaphores \xC2\xA9 2003",
00414 " George - Canal/Lock graphics \xC2\xA9 2003-2004",
00415 " David Dallaston - Tram tracks",
00416 " Marcin Grzegorczyk - Foundations for Tracks on Slopes",
00417 " All Translators - Who made OpenTTD a truly international game",
00418 " Bug Reporters - Without whom OpenTTD would still be full of bugs!",
00419 "",
00420 "",
00421 "And last but not least:",
00422 " Chris Sawyer - For an amazing game!"
00423 };
00424
00425 struct AboutWindow : public Window {
00426 int text_position;
00427 byte counter;
00428 int line_height;
00429 static const int num_visible_lines = 19;
00430
00431 AboutWindow() : Window()
00432 {
00433 this->InitNested(&_about_desc);
00434
00435 this->counter = 5;
00436 this->text_position = this->GetWidget<NWidgetBase>(AW_SCROLLING_TEXT)->pos_y + this->GetWidget<NWidgetBase>(AW_SCROLLING_TEXT)->current_y;
00437 }
00438
00439 virtual void SetStringParameters(int widget) const
00440 {
00441 if (widget == AW_WEBSITE) SetDParamStr(0, "Website: http://www.openttd.org");
00442 }
00443
00444 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00445 {
00446 if (widget != AW_SCROLLING_TEXT) return;
00447
00448 this->line_height = FONT_HEIGHT_NORMAL;
00449
00450 Dimension d;
00451 d.height = this->line_height * num_visible_lines;
00452
00453 d.width = 0;
00454 for (uint i = 0; i < lengthof(_credits); i++) {
00455 d.width = max(d.width, GetStringBoundingBox(_credits[i]).width);
00456 }
00457 *size = maxdim(*size, d);
00458 }
00459
00460 virtual void DrawWidget(const Rect &r, int widget) const
00461 {
00462 if (widget != AW_SCROLLING_TEXT) return;
00463
00464 int y = this->text_position;
00465
00466
00467 for (uint i = 0; i < lengthof(_credits); i++) {
00468 if (y >= r.top + 7 && y < r.bottom - this->line_height) {
00469 DrawString(r.left, r.right, y, _credits[i], TC_BLACK, SA_LEFT | SA_FORCE);
00470 }
00471 y += this->line_height;
00472 }
00473 }
00474
00475 virtual void OnTick()
00476 {
00477 if (--this->counter == 0) {
00478 this->counter = 5;
00479 this->text_position--;
00480
00481 if (this->text_position < (int)(this->GetWidget<NWidgetBase>(AW_SCROLLING_TEXT)->pos_y - lengthof(_credits) * this->line_height)) {
00482 this->text_position = this->GetWidget<NWidgetBase>(AW_SCROLLING_TEXT)->pos_y + this->GetWidget<NWidgetBase>(AW_SCROLLING_TEXT)->current_y;
00483 }
00484 this->SetDirty();
00485 }
00486 }
00487 };
00488
00489 void ShowAboutWindow()
00490 {
00491 DeleteWindowById(WC_GAME_OPTIONS, 0);
00492 new AboutWindow();
00493 }
00494
00496 enum ErrorMessageWidgets {
00497 EMW_CAPTION,
00498 EMW_FACE,
00499 EMW_MESSAGE,
00500 };
00501
00502 static const NWidgetPart _nested_errmsg_widgets[] = {
00503 NWidget(NWID_HORIZONTAL),
00504 NWidget(WWT_CLOSEBOX, COLOUR_RED),
00505 NWidget(WWT_CAPTION, COLOUR_RED, EMW_CAPTION), SetDataTip(STR_ERROR_MESSAGE_CAPTION, STR_NULL),
00506 EndContainer(),
00507 NWidget(WWT_PANEL, COLOUR_RED),
00508 NWidget(WWT_EMPTY, COLOUR_RED, EMW_MESSAGE), SetPadding(0, 2, 0, 2), SetMinimalSize(236, 32),
00509 EndContainer(),
00510 };
00511
00512 static const WindowDesc _errmsg_desc(
00513 WDP_MANUAL, 0, 0,
00514 WC_ERRMSG, WC_NONE,
00515 0,
00516 _nested_errmsg_widgets, lengthof(_nested_errmsg_widgets)
00517 );
00518
00519 static const NWidgetPart _nested_errmsg_face_widgets[] = {
00520 NWidget(NWID_HORIZONTAL),
00521 NWidget(WWT_CLOSEBOX, COLOUR_RED),
00522 NWidget(WWT_CAPTION, COLOUR_RED, EMW_CAPTION), SetDataTip(STR_ERROR_MESSAGE_CAPTION_OTHER_COMPANY, STR_NULL),
00523 EndContainer(),
00524 NWidget(WWT_PANEL, COLOUR_RED),
00525 NWidget(NWID_HORIZONTAL), SetPIP(2, 1, 2),
00526 NWidget(WWT_EMPTY, COLOUR_RED, EMW_FACE), SetMinimalSize(92, 119), SetFill(0, 1), SetPadding(2, 0, 1, 0),
00527 NWidget(WWT_EMPTY, COLOUR_RED, EMW_MESSAGE), SetFill(0, 1), SetMinimalSize(238, 123),
00528 EndContainer(),
00529 EndContainer(),
00530 };
00531
00532 static const WindowDesc _errmsg_face_desc(
00533 WDP_MANUAL, 0, 0,
00534 WC_ERRMSG, WC_NONE,
00535 0,
00536 _nested_errmsg_face_widgets, lengthof(_nested_errmsg_face_widgets)
00537 );
00538
00540 struct ErrmsgWindow : public Window {
00541 private:
00542 uint duration;
00543 uint64 decode_params[20];
00544 StringID summary_msg;
00545 StringID detailed_msg;
00546 uint height_summary;
00547 uint height_detailed;
00548 Point position;
00549 CompanyID face;
00550
00551 public:
00552 ErrmsgWindow(Point pt, StringID summary_msg, StringID detailed_msg, bool no_timeout) : Window()
00553 {
00554 this->position = pt;
00555 this->duration = no_timeout ? 0 : _settings_client.gui.errmsg_duration;
00556 CopyOutDParam(this->decode_params, 0, lengthof(this->decode_params));
00557 this->summary_msg = summary_msg;
00558 this->detailed_msg = detailed_msg;
00559
00560 CompanyID company = (CompanyID)GetDParamX(this->decode_params, 2);
00561 this->face = (this->detailed_msg == STR_ERROR_OWNED_BY && company < MAX_COMPANIES) ? company : INVALID_COMPANY;
00562 const WindowDesc *desc = (face == INVALID_COMPANY) ? &_errmsg_desc : &_errmsg_face_desc;
00563
00564 assert(summary_msg != INVALID_STRING_ID);
00565
00566 this->InitNested(desc);
00567 }
00568
00569 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00570 {
00571 if (widget != EMW_MESSAGE) return;
00572
00573 CopyInDParam(0, this->decode_params, lengthof(this->decode_params));
00574
00575
00576
00577 SwitchToErrorRefStack();
00578 RewindTextRefStack();
00579
00580 int text_width = max(0, (int)size->width - WD_FRAMETEXT_LEFT - WD_FRAMETEXT_RIGHT);
00581 this->height_summary = GetStringHeight(this->summary_msg, text_width);
00582 this->height_detailed = (this->detailed_msg == INVALID_STRING_ID) ? 0 : GetStringHeight(this->detailed_msg, text_width);
00583
00584 SwitchToNormalRefStack();
00585
00586 uint panel_height = WD_FRAMERECT_TOP + this->height_summary + WD_FRAMERECT_BOTTOM;
00587 if (this->detailed_msg != INVALID_STRING_ID) panel_height += this->height_detailed + WD_PAR_VSEP_WIDE;
00588
00589 size->height = max(size->height, panel_height);
00590 }
00591
00592 virtual Point OnInitialPosition(const WindowDesc *desc, int16 sm_width, int16 sm_height, int window_number)
00593 {
00594
00595 if (this->position.x == 0 && this->position.y == 0) {
00596 Point pt = {(_screen.width - sm_width) >> 1, (_screen.height - sm_height) >> 1};
00597 return pt;
00598 }
00599
00600
00601
00602
00603 int scr_top = GetMainViewTop() + 20;
00604 int scr_bot = GetMainViewBottom() - 20;
00605
00606 Point pt = RemapCoords2(this->position.x, this->position.y);
00607 const ViewPort *vp = FindWindowById(WC_MAIN_WINDOW, 0)->viewport;
00608 if (this->face == INVALID_COMPANY) {
00609
00610 pt.x = UnScaleByZoom(pt.x - vp->virtual_left, vp->zoom) + vp->left;
00611 pt.x = (pt.x < (_screen.width >> 1)) ? _screen.width - sm_width - 20 : 20;
00612
00613
00614 pt.y = UnScaleByZoom(pt.y - vp->virtual_top, vp->zoom) + vp->top;
00615 pt.y = (pt.y < (_screen.height >> 1)) ? scr_bot - sm_height : scr_top;
00616 } else {
00617 pt.x = Clamp(UnScaleByZoom(pt.x - vp->virtual_left, vp->zoom) + vp->left - (sm_width / 2), 0, _screen.width - sm_width);
00618 pt.y = Clamp(UnScaleByZoom(pt.y - vp->virtual_top, vp->zoom) + vp->top - (sm_height / 2), scr_top, scr_bot - sm_height);
00619 }
00620 return pt;
00621 }
00622
00623 virtual void OnInvalidateData(int data)
00624 {
00625
00626 if (this->face != INVALID_COMPANY && !Company::IsValidID(this->face)) delete this;
00627 }
00628
00629 virtual void SetStringParameters(int widget) const
00630 {
00631 if (widget == EMW_CAPTION) CopyInDParam(0, this->decode_params, lengthof(this->decode_params));
00632 }
00633
00634 virtual void DrawWidget(const Rect &r, int widget) const
00635 {
00636 switch (widget) {
00637 case EMW_FACE: {
00638 const Company *c = Company::Get(this->face);
00639 DrawCompanyManagerFace(c->face, c->colour, r.left, r.top);
00640 break;
00641 }
00642
00643 case EMW_MESSAGE:
00644 CopyInDParam(0, this->decode_params, lengthof(this->decode_params));
00645 SwitchToErrorRefStack();
00646 RewindTextRefStack();
00647
00648 if (this->detailed_msg == INVALID_STRING_ID) {
00649 DrawStringMultiLine(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, r.top + WD_FRAMERECT_TOP, r.bottom - WD_FRAMERECT_BOTTOM,
00650 this->summary_msg, TC_FROMSTRING, SA_CENTER);
00651 } else {
00652 int extra = (r.bottom - r.top + 1 - this->height_summary - this->height_detailed - WD_PAR_VSEP_WIDE) / 2;
00653
00654
00655 int top = r.top + WD_FRAMERECT_TOP;
00656 int bottom = top + this->height_summary + extra;
00657 DrawStringMultiLine(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, top, bottom, this->summary_msg, TC_WHITE, SA_CENTER);
00658
00659 bottom = r.bottom - WD_FRAMERECT_BOTTOM;
00660 top = bottom - this->height_detailed - extra;
00661 DrawStringMultiLine(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, top, bottom, this->detailed_msg, TC_WHITE, SA_CENTER);
00662 }
00663
00664 SwitchToNormalRefStack();
00665 break;
00666
00667 default:
00668 break;
00669 }
00670 }
00671
00672 virtual void OnMouseLoop()
00673 {
00674
00675 if (_right_button_down && this->duration != 0) delete this;
00676 }
00677
00678 virtual void OnHundredthTick()
00679 {
00680
00681 if (this->duration != 0) {
00682 this->duration--;
00683 if (this->duration == 0) delete this;
00684 }
00685 }
00686
00687 ~ErrmsgWindow()
00688 {
00689 SetRedErrorSquare(INVALID_TILE);
00690 }
00691
00692 virtual EventState OnKeyPress(uint16 key, uint16 keycode)
00693 {
00694 if (keycode != WKC_SPACE) return ES_NOT_HANDLED;
00695 delete this;
00696 return ES_HANDLED;
00697 }
00698 };
00699
00708 void ShowErrorMessage(StringID summary_msg, StringID detailed_msg, WarningLevel wl, int x, int y)
00709 {
00710 if (summary_msg == STR_NULL) summary_msg = STR_EMPTY;
00711
00712 if (wl != WL_INFO) {
00713
00714 char buf[DRAW_STRING_BUFFER];
00715 char *b = GetString(buf, summary_msg, lastof(buf));
00716 if (detailed_msg != INVALID_STRING_ID) {
00717 b += seprintf(b, lastof(buf), " ");
00718 GetString(b, detailed_msg, lastof(buf));
00719 }
00720 switch (wl) {
00721 case WL_WARNING: IConsolePrint(CC_WARNING, buf); break;
00722 default: IConsoleError(buf); break;
00723 }
00724 }
00725
00726 bool no_timeout = wl == WL_CRITICAL;
00727
00728 if (_settings_client.gui.errmsg_duration == 0 && !no_timeout) return;
00729
00730 DeleteWindowById(WC_ERRMSG, 0);
00731
00732 Point pt = {x, y};
00733 new ErrmsgWindow(pt, summary_msg, detailed_msg, no_timeout);
00734 }
00735
00736 void ShowEstimatedCostOrIncome(Money cost, int x, int y)
00737 {
00738 StringID msg = STR_MESSAGE_ESTIMATED_COST;
00739
00740 if (cost < 0) {
00741 cost = -cost;
00742 msg = STR_MESSAGE_ESTIMATED_INCOME;
00743 }
00744 SetDParam(0, cost);
00745 ShowErrorMessage(msg, INVALID_STRING_ID, WL_INFO, x, y);
00746 }
00747
00748 void ShowCostOrIncomeAnimation(int x, int y, int z, Money cost)
00749 {
00750 Point pt = RemapCoords(x, y, z);
00751 StringID msg = STR_INCOME_FLOAT_COST;
00752
00753 if (cost < 0) {
00754 cost = -cost;
00755 msg = STR_INCOME_FLOAT_INCOME;
00756 }
00757 SetDParam(0, cost);
00758 AddTextEffect(msg, pt.x, pt.y, DAY_TICKS, TE_RISING);
00759 }
00760
00761 void ShowFeederIncomeAnimation(int x, int y, int z, Money cost)
00762 {
00763 Point pt = RemapCoords(x, y, z);
00764
00765 SetDParam(0, cost);
00766 AddTextEffect(STR_FEEDER, pt.x, pt.y, DAY_TICKS, TE_RISING);
00767 }
00768
00769 TextEffectID ShowFillingPercent(int x, int y, int z, uint8 percent, StringID string)
00770 {
00771 Point pt = RemapCoords(x, y, z);
00772
00773 assert(string != STR_NULL);
00774
00775 SetDParam(0, percent);
00776 return AddTextEffect(string, pt.x, pt.y, 0, TE_STATIC);
00777 }
00778
00779 void UpdateFillingPercent(TextEffectID te_id, uint8 percent, StringID string)
00780 {
00781 assert(string != STR_NULL);
00782
00783 SetDParam(0, percent);
00784 UpdateTextEffect(te_id, string);
00785 }
00786
00787 void HideFillingPercent(TextEffectID *te_id)
00788 {
00789 if (*te_id == INVALID_TE_ID) return;
00790
00791 RemoveTextEffect(*te_id);
00792 *te_id = INVALID_TE_ID;
00793 }
00794
00795 static const NWidgetPart _nested_tooltips_widgets[] = {
00796 NWidget(WWT_PANEL, COLOUR_GREY, 0), SetMinimalSize(200, 32), EndContainer(),
00797 };
00798
00799 static const WindowDesc _tool_tips_desc(
00800 WDP_MANUAL, 0, 0,
00801 WC_TOOLTIPS, WC_NONE,
00802 0,
00803 _nested_tooltips_widgets, lengthof(_nested_tooltips_widgets)
00804 );
00805
00807 struct TooltipsWindow : public Window
00808 {
00809 StringID string_id;
00810 byte paramcount;
00811 uint64 params[5];
00812 TooltipCloseCondition close_cond;
00813
00814 TooltipsWindow(Window *parent, StringID str, uint paramcount, const uint64 params[], TooltipCloseCondition close_tooltip) : Window()
00815 {
00816 this->parent = parent;
00817 this->string_id = str;
00818 assert_compile(sizeof(this->params[0]) == sizeof(params[0]));
00819 assert(paramcount <= lengthof(this->params));
00820 memcpy(this->params, params, sizeof(this->params[0]) * paramcount);
00821 this->paramcount = paramcount;
00822 this->close_cond = close_tooltip;
00823
00824 this->InitNested(&_tool_tips_desc);
00825
00826 this->flags4 &= ~WF_WHITE_BORDER_MASK;
00827 }
00828
00829 virtual Point OnInitialPosition(const WindowDesc *desc, int16 sm_width, int16 sm_height, int window_number)
00830 {
00831
00832
00833
00834 int scr_top = GetMainViewTop() + 2;
00835 int scr_bot = GetMainViewBottom() - 2;
00836
00837 Point pt;
00838
00839
00840
00841
00842 pt.y = Clamp(_cursor.pos.y + _cursor.size.y + _cursor.offs.y + 5, scr_top, scr_bot);
00843 if (pt.y + sm_height > scr_bot) pt.y = min(_cursor.pos.y + _cursor.offs.y - 5, scr_bot) - sm_height;
00844 pt.x = sm_width >= _screen.width ? 0 : Clamp(_cursor.pos.x - (sm_width >> 1), 0, _screen.width - sm_width);
00845
00846 return pt;
00847 }
00848
00849 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00850 {
00851
00852 for (uint i = 0; i != this->paramcount; i++) SetDParam(i, this->params[i]);
00853
00854 size->width = min(GetStringBoundingBox(this->string_id).width, 194);
00855 size->height = GetStringHeight(this->string_id, size->width);
00856
00857
00858 size->width += 2 + WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
00859 size->height += 2 + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
00860 }
00861
00862 virtual void DrawWidget(const Rect &r, int widget) const
00863 {
00864
00865 GfxFillRect(r.left, r.top, r.right, r.bottom, 0);
00866 GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, 0x44);
00867
00868 for (uint arg = 0; arg < this->paramcount; arg++) {
00869 SetDParam(arg, this->params[arg]);
00870 }
00871 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);
00872 }
00873
00874 virtual void OnMouseLoop()
00875 {
00876
00877 if (!_cursor.in_window) {
00878 delete this;
00879 return;
00880 }
00881
00882
00883
00884 switch (this->close_cond) {
00885 case TCC_RIGHT_CLICK: if (!_right_button_down) delete this; break;
00886 case TCC_LEFT_CLICK: if (!_left_button_down) delete this; break;
00887 case TCC_HOVER: if (!_mouse_hovering) delete this; break;
00888 }
00889 }
00890 };
00891
00900 void GuiShowTooltips(Window *parent, StringID str, uint paramcount, const uint64 params[], TooltipCloseCondition close_tooltip)
00901 {
00902 DeleteWindowById(WC_TOOLTIPS, 0);
00903
00904 if (str == STR_NULL) return;
00905
00906 new TooltipsWindow(parent, str, paramcount, params, close_tooltip);
00907 }
00908
00909
00910
00911
00912 static void DelChar(Textbuf *tb, bool backspace)
00913 {
00914 WChar c;
00915 char *s = tb->buf + tb->caretpos;
00916
00917 if (backspace) s = Utf8PrevChar(s);
00918
00919 uint16 len = (uint16)Utf8Decode(&c, s);
00920 uint width = GetCharacterWidth(FS_NORMAL, c);
00921
00922 tb->pixels -= width;
00923 if (backspace) {
00924 tb->caretpos -= len;
00925 tb->caretxoffs -= width;
00926 }
00927
00928
00929 memmove(s, s + len, tb->bytes - (s - tb->buf) - len);
00930 tb->bytes -= len;
00931 tb->chars--;
00932 }
00933
00941 bool DeleteTextBufferChar(Textbuf *tb, int delmode)
00942 {
00943 if (delmode == WKC_BACKSPACE && tb->caretpos != 0) {
00944 DelChar(tb, true);
00945 return true;
00946 } else if (delmode == WKC_DELETE && tb->caretpos < tb->bytes - 1) {
00947 DelChar(tb, false);
00948 return true;
00949 }
00950
00951 return false;
00952 }
00953
00958 void DeleteTextBufferAll(Textbuf *tb)
00959 {
00960 memset(tb->buf, 0, tb->max_bytes);
00961 tb->bytes = tb->chars = 1;
00962 tb->pixels = tb->caretpos = tb->caretxoffs = 0;
00963 }
00964
00973 bool InsertTextBufferChar(Textbuf *tb, WChar key)
00974 {
00975 const byte charwidth = GetCharacterWidth(FS_NORMAL, key);
00976 uint16 len = (uint16)Utf8CharLen(key);
00977 if (tb->bytes + len <= tb->max_bytes && tb->chars + 1 <= tb->max_chars && (tb->max_pixels == 0 || tb->pixels + charwidth <= tb->max_pixels)) {
00978 memmove(tb->buf + tb->caretpos + len, tb->buf + tb->caretpos, tb->bytes - tb->caretpos);
00979 Utf8Encode(tb->buf + tb->caretpos, key);
00980 tb->chars++;
00981 tb->bytes += len;
00982 tb->pixels += charwidth;
00983
00984 tb->caretpos += len;
00985 tb->caretxoffs += charwidth;
00986 return true;
00987 }
00988 return false;
00989 }
00990
00998 bool InsertTextBufferClipboard(Textbuf *tb)
00999 {
01000 char utf8_buf[512];
01001
01002 if (!GetClipboardContents(utf8_buf, lengthof(utf8_buf))) return false;
01003
01004 uint16 pixels = 0, bytes = 0, chars = 0;
01005 WChar c;
01006 for (const char *ptr = utf8_buf; (c = Utf8Consume(&ptr)) != '\0';) {
01007 if (!IsPrintable(c)) break;
01008
01009 byte len = Utf8CharLen(c);
01010 if (tb->bytes + bytes + len > tb->max_bytes) break;
01011 if (tb->chars + chars + 1 > tb->max_chars) break;
01012
01013 byte char_pixels = GetCharacterWidth(FS_NORMAL, c);
01014 if (tb->max_pixels != 0 && pixels + tb->pixels + char_pixels > tb->max_pixels) break;
01015
01016 pixels += char_pixels;
01017 bytes += len;
01018 chars++;
01019 }
01020
01021 if (bytes == 0) return false;
01022
01023 memmove(tb->buf + tb->caretpos + bytes, tb->buf + tb->caretpos, tb->bytes - tb->caretpos);
01024 memcpy(tb->buf + tb->caretpos, utf8_buf, bytes);
01025 tb->pixels += pixels;
01026 tb->caretxoffs += pixels;
01027
01028 tb->bytes += bytes;
01029 tb->chars += chars;
01030 tb->caretpos += bytes;
01031 assert(tb->bytes <= tb->max_bytes);
01032 assert(tb->chars <= tb->max_chars);
01033 tb->buf[tb->bytes - 1] = '\0';
01034
01035 return true;
01036 }
01037
01045 bool MoveTextBufferPos(Textbuf *tb, int navmode)
01046 {
01047 switch (navmode) {
01048 case WKC_LEFT:
01049 if (tb->caretpos != 0) {
01050 WChar c;
01051 const char *s = Utf8PrevChar(tb->buf + tb->caretpos);
01052 Utf8Decode(&c, s);
01053 tb->caretpos = s - tb->buf;
01054 tb->caretxoffs -= GetCharacterWidth(FS_NORMAL, c);
01055
01056 return true;
01057 }
01058 break;
01059
01060 case WKC_RIGHT:
01061 if (tb->caretpos < tb->bytes - 1) {
01062 WChar c;
01063
01064 tb->caretpos += (uint16)Utf8Decode(&c, tb->buf + tb->caretpos);
01065 tb->caretxoffs += GetCharacterWidth(FS_NORMAL, c);
01066
01067 return true;
01068 }
01069 break;
01070
01071 case WKC_HOME:
01072 tb->caretpos = 0;
01073 tb->caretxoffs = 0;
01074 return true;
01075
01076 case WKC_END:
01077 tb->caretpos = tb->bytes - 1;
01078 tb->caretxoffs = tb->pixels;
01079 return true;
01080
01081 default:
01082 break;
01083 }
01084
01085 return false;
01086 }
01087
01098 void InitializeTextBuffer(Textbuf *tb, char *buf, uint16 max_bytes, uint16 max_pixels)
01099 {
01100 InitializeTextBuffer(tb, buf, max_bytes, max_bytes, max_pixels);
01101 }
01102
01114 void InitializeTextBuffer(Textbuf *tb, char *buf, uint16 max_bytes, uint16 max_chars, uint16 max_pixels)
01115 {
01116 assert(max_bytes != 0);
01117 assert(max_chars != 0);
01118
01119 tb->buf = buf;
01120 tb->max_bytes = max_bytes;
01121 tb->max_chars = max_chars;
01122 tb->max_pixels = max_pixels;
01123 tb->caret = true;
01124 UpdateTextBufferSize(tb);
01125 }
01126
01133 void UpdateTextBufferSize(Textbuf *tb)
01134 {
01135 const char *buf = tb->buf;
01136
01137 tb->pixels = 0;
01138 tb->chars = tb->bytes = 1;
01139
01140 WChar c;
01141 while ((c = Utf8Consume(&buf)) != '\0') {
01142 tb->pixels += GetCharacterWidth(FS_NORMAL, c);
01143 tb->bytes += Utf8CharLen(c);
01144 tb->chars++;
01145 }
01146
01147 assert(tb->bytes <= tb->max_bytes);
01148 assert(tb->chars <= tb->max_chars);
01149
01150 tb->caretpos = tb->bytes - 1;
01151 tb->caretxoffs = tb->pixels;
01152 }
01153
01154 bool HandleCaret(Textbuf *tb)
01155 {
01156
01157 bool b = !!(_caret_timer & 0x20);
01158
01159 if (b != tb->caret) {
01160 tb->caret = b;
01161 return true;
01162 }
01163 return false;
01164 }
01165
01166 bool QueryString::HasEditBoxFocus(const Window *w, int wid) const
01167 {
01168 if (w->IsWidgetGloballyFocused(wid)) return true;
01169 if (w->window_class != WC_OSK || _focused_window != w->parent) return false;
01170 return w->parent->nested_focus != NULL && w->parent->nested_focus->type == WWT_EDITBOX;
01171 }
01172
01173 HandleEditBoxResult QueryString::HandleEditBoxKey(Window *w, int wid, uint16 key, uint16 keycode, EventState &state)
01174 {
01175 if (!QueryString::HasEditBoxFocus(w, wid)) return HEBR_NOT_FOCUSED;
01176
01177 state = ES_HANDLED;
01178
01179 switch (keycode) {
01180 case WKC_ESC: return HEBR_CANCEL;
01181
01182 case WKC_RETURN: case WKC_NUM_ENTER: return HEBR_CONFIRM;
01183
01184 #ifdef WITH_COCOA
01185 case (WKC_META | 'V'):
01186 #endif
01187 case (WKC_CTRL | 'V'):
01188 if (InsertTextBufferClipboard(&this->text)) w->SetWidgetDirty(wid);
01189 break;
01190
01191 #ifdef WITH_COCOA
01192 case (WKC_META | 'U'):
01193 #endif
01194 case (WKC_CTRL | 'U'):
01195 DeleteTextBufferAll(&this->text);
01196 w->SetWidgetDirty(wid);
01197 break;
01198
01199 case WKC_BACKSPACE: case WKC_DELETE:
01200 if (DeleteTextBufferChar(&this->text, keycode)) w->SetWidgetDirty(wid);
01201 break;
01202
01203 case WKC_LEFT: case WKC_RIGHT: case WKC_END: case WKC_HOME:
01204 if (MoveTextBufferPos(&this->text, keycode)) w->SetWidgetDirty(wid);
01205 break;
01206
01207 default:
01208 if (IsValidChar(key, this->afilter)) {
01209 if (InsertTextBufferChar(&this->text, key)) w->SetWidgetDirty(wid);
01210 } else {
01211 state = ES_NOT_HANDLED;
01212 }
01213 }
01214
01215 return HEBR_EDITING;
01216 }
01217
01218 void QueryString::HandleEditBox(Window *w, int wid)
01219 {
01220 if (HasEditBoxFocus(w, wid) && HandleCaret(&this->text)) {
01221 w->SetWidgetDirty(wid);
01222
01223
01224 if (w->window_class != WC_OSK) {
01225 Window *w_osk = FindWindowById(WC_OSK, 0);
01226 if (w_osk != NULL && w_osk->parent == w) w_osk->InvalidateData();
01227 }
01228 }
01229 }
01230
01231 void QueryString::DrawEditBox(Window *w, int wid)
01232 {
01233 const NWidgetBase *wi = w->GetWidget<NWidgetBase>(wid);
01234
01235 assert((wi->type & WWT_MASK) == WWT_EDITBOX);
01236 int left = wi->pos_x;
01237 int right = wi->pos_x + wi->current_x - 1;
01238 int top = wi->pos_y;
01239 int bottom = wi->pos_y + wi->current_y - 1;
01240
01241 GfxFillRect(left + 1, top + 1, right - 1, bottom - 1, 215);
01242
01243
01244 DrawPixelInfo dpi;
01245 if (!FillDrawPixelInfo(&dpi, left + WD_FRAMERECT_LEFT, top + WD_FRAMERECT_TOP, right - left - WD_FRAMERECT_RIGHT, bottom - top - WD_FRAMERECT_BOTTOM)) return;
01246
01247 DrawPixelInfo *old_dpi = _cur_dpi;
01248 _cur_dpi = &dpi;
01249
01250
01251
01252 const Textbuf *tb = &this->text;
01253 int delta = min(0, (right - left) - tb->pixels - 10);
01254
01255 if (tb->caretxoffs + delta < 0) delta = -tb->caretxoffs;
01256
01257 DrawString(delta, tb->pixels, 0, tb->buf, TC_YELLOW);
01258 if (HasEditBoxFocus(w, wid) && tb->caret) {
01259 int caret_width = GetStringBoundingBox("_").width;
01260 DrawString(tb->caretxoffs + delta, tb->caretxoffs + delta + caret_width, 0, "_", TC_WHITE);
01261 }
01262
01263 _cur_dpi = old_dpi;
01264 }
01265
01266 HandleEditBoxResult QueryStringBaseWindow::HandleEditBoxKey(int wid, uint16 key, uint16 keycode, EventState &state)
01267 {
01268 return this->QueryString::HandleEditBoxKey(this, wid, key, keycode, state);
01269 }
01270
01271 void QueryStringBaseWindow::HandleEditBox(int wid)
01272 {
01273 this->QueryString::HandleEditBox(this, wid);
01274 }
01275
01276 void QueryStringBaseWindow::DrawEditBox(int wid)
01277 {
01278 this->QueryString::DrawEditBox(this, wid);
01279 }
01280
01281 void QueryStringBaseWindow::OnOpenOSKWindow(int wid)
01282 {
01283 ShowOnScreenKeyboard(this, wid, 0, 0);
01284 }
01285
01287 enum QueryStringWidgets {
01288 QUERY_STR_WIDGET_CAPTION,
01289 QUERY_STR_WIDGET_TEXT,
01290 QUERY_STR_WIDGET_DEFAULT,
01291 QUERY_STR_WIDGET_CANCEL,
01292 QUERY_STR_WIDGET_OK
01293 };
01294
01296 struct QueryStringWindow : public QueryStringBaseWindow
01297 {
01298 QueryStringFlags flags;
01299
01300 QueryStringWindow(StringID str, StringID caption, uint max_bytes, uint max_chars, uint max_pixels, const WindowDesc *desc, Window *parent, CharSetFilter afilter, QueryStringFlags flags) :
01301 QueryStringBaseWindow(max_bytes, max_chars)
01302 {
01303 GetString(this->edit_str_buf, str, &this->edit_str_buf[max_bytes - 1]);
01304 str_validate(this->edit_str_buf, &this->edit_str_buf[max_bytes - 1], false, true);
01305
01306
01307
01308 while (Utf8StringLength(this->edit_str_buf) + 1 > max_chars) {
01309 *Utf8PrevChar(this->edit_str_buf + strlen(this->edit_str_buf)) = '\0';
01310 }
01311
01312 if ((flags & QSF_ACCEPT_UNCHANGED) == 0) this->orig = strdup(this->edit_str_buf);
01313
01314 this->caption = caption;
01315 this->afilter = afilter;
01316 this->flags = flags;
01317 InitializeTextBuffer(&this->text, this->edit_str_buf, max_bytes, max_chars, max_pixels);
01318
01319 this->InitNested(desc);
01320
01321 this->parent = parent;
01322
01323 this->SetFocusedWidget(QUERY_STR_WIDGET_TEXT);
01324 this->LowerWidget(QUERY_STR_WIDGET_TEXT);
01325 }
01326
01327 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
01328 {
01329 if (widget == QUERY_STR_WIDGET_DEFAULT && (this->flags & QSF_ENABLE_DEFAULT) == 0) {
01330
01331 fill->width = 0;
01332 resize->width = 0;
01333 size->width = 0;
01334 }
01335 }
01336
01337 virtual void OnPaint()
01338 {
01339 this->DrawWidgets();
01340
01341 this->DrawEditBox(QUERY_STR_WIDGET_TEXT);
01342 }
01343
01344 virtual void SetStringParameters(int widget) const
01345 {
01346 if (widget == QUERY_STR_WIDGET_CAPTION) SetDParam(0, this->caption);
01347 }
01348
01349 void OnOk()
01350 {
01351 if (this->orig == NULL || strcmp(this->text.buf, this->orig) != 0) {
01352
01353
01354 if (this->parent != NULL) {
01355 this->parent->OnQueryTextFinished(this->text.buf);
01356 } else {
01357 HandleOnEditText(this->text.buf);
01358 }
01359 this->handled = true;
01360 }
01361 }
01362
01363 virtual void OnClick(Point pt, int widget, int click_count)
01364 {
01365 switch (widget) {
01366 case QUERY_STR_WIDGET_DEFAULT:
01367 this->text.buf[0] = '\0';
01368
01369 case QUERY_STR_WIDGET_OK:
01370 this->OnOk();
01371
01372 case QUERY_STR_WIDGET_CANCEL:
01373 delete this;
01374 break;
01375 }
01376 }
01377
01378 virtual void OnMouseLoop()
01379 {
01380 this->HandleEditBox(QUERY_STR_WIDGET_TEXT);
01381 }
01382
01383 virtual EventState OnKeyPress(uint16 key, uint16 keycode)
01384 {
01385 EventState state = ES_NOT_HANDLED;
01386 switch (this->HandleEditBoxKey(QUERY_STR_WIDGET_TEXT, key, keycode, state)) {
01387 default: NOT_REACHED();
01388 case HEBR_EDITING: {
01389 Window *osk = FindWindowById(WC_OSK, 0);
01390 if (osk != NULL && osk->parent == this) osk->InvalidateData();
01391 break;
01392 }
01393 case HEBR_CONFIRM: this->OnOk();
01394
01395 case HEBR_CANCEL: delete this; break;
01396 case HEBR_NOT_FOCUSED: break;
01397 }
01398 return state;
01399 }
01400
01401 virtual void OnOpenOSKWindow(int wid)
01402 {
01403 ShowOnScreenKeyboard(this, wid, QUERY_STR_WIDGET_CANCEL, QUERY_STR_WIDGET_OK);
01404 }
01405
01406 ~QueryStringWindow()
01407 {
01408 if (!this->handled && this->parent != NULL) {
01409 Window *parent = this->parent;
01410 this->parent = NULL;
01411 parent->OnQueryTextFinished(NULL);
01412 }
01413 }
01414 };
01415
01416 static const NWidgetPart _nested_query_string_widgets[] = {
01417 NWidget(NWID_HORIZONTAL),
01418 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
01419 NWidget(WWT_CAPTION, COLOUR_GREY, QUERY_STR_WIDGET_CAPTION), SetDataTip(STR_WHITE_STRING, STR_NULL),
01420 EndContainer(),
01421 NWidget(WWT_PANEL, COLOUR_GREY),
01422 NWidget(WWT_EDITBOX, COLOUR_GREY, QUERY_STR_WIDGET_TEXT), SetMinimalSize(256, 12), SetFill(1, 1), SetPadding(2, 2, 2, 2),
01423 EndContainer(),
01424 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
01425 NWidget(WWT_TEXTBTN, COLOUR_GREY, QUERY_STR_WIDGET_DEFAULT), SetMinimalSize(87, 12), SetFill(1, 1), SetDataTip(STR_BUTTON_DEFAULT, STR_NULL),
01426 NWidget(WWT_TEXTBTN, COLOUR_GREY, QUERY_STR_WIDGET_CANCEL), SetMinimalSize(86, 12), SetFill(1, 1), SetDataTip(STR_BUTTON_CANCEL, STR_NULL),
01427 NWidget(WWT_TEXTBTN, COLOUR_GREY, QUERY_STR_WIDGET_OK), SetMinimalSize(87, 12), SetFill(1, 1), SetDataTip(STR_BUTTON_OK, STR_NULL),
01428 EndContainer(),
01429 };
01430
01431 static const WindowDesc _query_string_desc(
01432 WDP_AUTO, 0, 0,
01433 WC_QUERY_STRING, WC_NONE,
01434 0,
01435 _nested_query_string_widgets, lengthof(_nested_query_string_widgets)
01436 );
01437
01449 void ShowQueryString(StringID str, StringID caption, uint maxsize, uint maxwidth, Window *parent, CharSetFilter afilter, QueryStringFlags flags)
01450 {
01451 DeleteWindowById(WC_QUERY_STRING, 0);
01452 new QueryStringWindow(str, caption, ((flags & QSF_LEN_IN_CHARS) ? MAX_CHAR_LENGTH : 1) * maxsize, maxsize, maxwidth, &_query_string_desc, parent, afilter, flags);
01453 }
01454
01455
01456 enum QueryWidgets {
01457 QUERY_WIDGET_CAPTION,
01458 QUERY_WIDGET_TEXT,
01459 QUERY_WIDGET_NO,
01460 QUERY_WIDGET_YES
01461 };
01462
01466 struct QueryWindow : public Window {
01467 QueryCallbackProc *proc;
01468 uint64 params[10];
01469 StringID message;
01470 StringID caption;
01471
01472 QueryWindow(const WindowDesc *desc, StringID caption, StringID message, Window *parent, QueryCallbackProc *callback) : Window()
01473 {
01474
01475
01476 CopyOutDParam(this->params, 0, lengthof(this->params));
01477 this->caption = caption;
01478 this->message = message;
01479 this->proc = callback;
01480
01481 this->InitNested(desc);
01482
01483 this->parent = parent;
01484 this->left = parent->left + (parent->width / 2) - (this->width / 2);
01485 this->top = parent->top + (parent->height / 2) - (this->height / 2);
01486 }
01487
01488 ~QueryWindow()
01489 {
01490 if (this->proc != NULL) this->proc(this->parent, false);
01491 }
01492
01493 virtual void SetStringParameters(int widget) const
01494 {
01495 switch (widget) {
01496 case QUERY_WIDGET_CAPTION:
01497 CopyInDParam(1, this->params, lengthof(this->params));
01498 SetDParam(0, this->caption);
01499 break;
01500
01501 case QUERY_WIDGET_TEXT:
01502 CopyInDParam(0, this->params, lengthof(this->params));
01503 break;
01504 }
01505 }
01506
01507 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
01508 {
01509 if (widget != QUERY_WIDGET_TEXT) return;
01510
01511 Dimension d = GetStringMultiLineBoundingBox(this->message, *size);
01512 d.width += padding.width;
01513 d.height += padding.height;
01514 *size = d;
01515 }
01516
01517 virtual void DrawWidget(const Rect &r, int widget) const
01518 {
01519 if (widget != QUERY_WIDGET_TEXT) return;
01520
01521 DrawStringMultiLine(r.left, r.right, r.top, r.bottom, this->message, TC_FROMSTRING, SA_CENTER);
01522 }
01523
01524 virtual void OnClick(Point pt, int widget, int click_count)
01525 {
01526 switch (widget) {
01527 case QUERY_WIDGET_YES: {
01528
01529
01530 QueryCallbackProc *proc = this->proc;
01531 Window *parent = this->parent;
01532
01533 this->proc = NULL;
01534 delete this;
01535 if (proc != NULL) {
01536 proc(parent, true);
01537 proc = NULL;
01538 }
01539 break;
01540 }
01541 case QUERY_WIDGET_NO:
01542 delete this;
01543 break;
01544 }
01545 }
01546
01547 virtual EventState OnKeyPress(uint16 key, uint16 keycode)
01548 {
01549
01550 switch (keycode) {
01551 case WKC_RETURN:
01552 case WKC_NUM_ENTER:
01553 if (this->proc != NULL) {
01554 this->proc(this->parent, true);
01555 this->proc = NULL;
01556 }
01557
01558 case WKC_ESC:
01559 delete this;
01560 return ES_HANDLED;
01561 }
01562 return ES_NOT_HANDLED;
01563 }
01564 };
01565
01566 static const NWidgetPart _nested_query_widgets[] = {
01567 NWidget(NWID_HORIZONTAL),
01568 NWidget(WWT_CLOSEBOX, COLOUR_RED),
01569 NWidget(WWT_CAPTION, COLOUR_RED, QUERY_WIDGET_CAPTION), SetDataTip(STR_JUST_STRING, STR_NULL),
01570 EndContainer(),
01571 NWidget(WWT_PANEL, COLOUR_RED), SetPIP(8, 15, 8),
01572 NWidget(WWT_TEXT, COLOUR_RED, QUERY_WIDGET_TEXT), SetMinimalSize(200, 12),
01573 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(20, 29, 20),
01574 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, QUERY_WIDGET_NO), SetMinimalSize(71, 12), SetDataTip(STR_QUIT_NO, STR_NULL),
01575 NWidget(WWT_PUSHTXTBTN, COLOUR_YELLOW, QUERY_WIDGET_YES), SetMinimalSize(71, 12), SetDataTip(STR_QUIT_YES, STR_NULL),
01576 EndContainer(),
01577 EndContainer(),
01578 };
01579
01580 static const WindowDesc _query_desc(
01581 WDP_CENTER, 0, 0,
01582 WC_CONFIRM_POPUP_QUERY, WC_NONE,
01583 WDF_UNCLICK_BUTTONS | WDF_MODAL,
01584 _nested_query_widgets, lengthof(_nested_query_widgets)
01585 );
01586
01596 void ShowQuery(StringID caption, StringID message, Window *parent, QueryCallbackProc *callback)
01597 {
01598 if (parent == NULL) parent = FindWindowById(WC_MAIN_WINDOW, 0);
01599
01600 const Window *w;
01601 FOR_ALL_WINDOWS_FROM_BACK(w) {
01602 if (w->window_class != WC_CONFIRM_POPUP_QUERY) continue;
01603
01604 const QueryWindow *qw = (const QueryWindow *)w;
01605 if (qw->parent != parent || qw->proc != callback) continue;
01606
01607 delete qw;
01608 break;
01609 }
01610
01611 new QueryWindow(&_query_desc, caption, message, parent, callback);
01612 }