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