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