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