00001
00002
00003
00004
00005
00006
00007
00008
00009
00012 #include "stdafx.h"
00013 #include "debug.h"
00014 #include "gui.h"
00015 #include "textbuf_gui.h"
00016 #include "company_func.h"
00017 #include "command_func.h"
00018 #include "vehicle_gui.h"
00019 #include "cargotype.h"
00020 #include "station_gui.h"
00021 #include "strings_func.h"
00022 #include "window_func.h"
00023 #include "viewport_func.h"
00024 #include "widgets/dropdown_func.h"
00025 #include "station_base.h"
00026 #include "waypoint_base.h"
00027 #include "tilehighlight_func.h"
00028 #include "company_base.h"
00029 #include "sortlist_type.h"
00030 #include "core/geometry_func.hpp"
00031 #include "vehiclelist.h"
00032
00033 #include "widgets/station_widget.h"
00034
00035 #include "table/strings.h"
00036
00037 #include <vector>
00038
00049 int DrawStationCoverageAreaText(int left, int right, int top, StationCoverageType sct, int rad, bool supplies)
00050 {
00051 TileIndex tile = TileVirtXY(_thd.pos.x, _thd.pos.y);
00052 uint32 cargo_mask = 0;
00053 if (_thd.drawstyle == HT_RECT && tile < MapSize()) {
00054 CargoArray cargoes;
00055 if (supplies) {
00056 cargoes = GetProductionAroundTiles(tile, _thd.size.x / TILE_SIZE, _thd.size.y / TILE_SIZE, rad);
00057 } else {
00058 cargoes = GetAcceptanceAroundTiles(tile, _thd.size.x / TILE_SIZE, _thd.size.y / TILE_SIZE, rad);
00059 }
00060
00061
00062 for (CargoID i = 0; i < NUM_CARGO; i++) {
00063 switch (sct) {
00064 case SCT_PASSENGERS_ONLY: if (!IsCargoInClass(i, CC_PASSENGERS)) continue; break;
00065 case SCT_NON_PASSENGERS_ONLY: if (IsCargoInClass(i, CC_PASSENGERS)) continue; break;
00066 case SCT_ALL: break;
00067 default: NOT_REACHED();
00068 }
00069 if (cargoes[i] >= (supplies ? 1U : 8U)) SetBit(cargo_mask, i);
00070 }
00071 }
00072 SetDParam(0, cargo_mask);
00073 return DrawStringMultiLine(left, right, top, INT32_MAX, supplies ? STR_STATION_BUILD_SUPPLIES_CARGO : STR_STATION_BUILD_ACCEPTS_CARGO);
00074 }
00075
00081 void CheckRedrawStationCoverage(const Window *w)
00082 {
00083 if (_thd.dirty & 1) {
00084 _thd.dirty &= ~1;
00085 w->SetDirty();
00086 }
00087 }
00088
00104 static void StationsWndShowStationRating(int left, int right, int y, CargoID type, uint amount, byte rating)
00105 {
00106 static const uint units_full = 576;
00107 static const uint rating_full = 224;
00108
00109 const CargoSpec *cs = CargoSpec::Get(type);
00110 if (!cs->IsValid()) return;
00111
00112 int colour = cs->rating_colour;
00113 uint w = (minu(amount, units_full) + 5) / 36;
00114
00115 int height = GetCharacterHeight(FS_SMALL);
00116
00117
00118 if (w != 0) GfxFillRect(left, y, left + w - 1, y + height, colour);
00119
00120
00121
00122 if (w == 0) {
00123 uint rest = amount / 5;
00124 if (rest != 0) {
00125 w += left;
00126 GfxFillRect(w, y + height - rest, w, y + height, colour);
00127 }
00128 }
00129
00130 DrawString(left + 1, right, y, cs->abbrev, TC_BLACK);
00131
00132
00133 y += height + 2;
00134 GfxFillRect(left + 1, y, left + 14, y, PC_RED);
00135 rating = minu(rating, rating_full) / 16;
00136 if (rating != 0) GfxFillRect(left + 1, y, left + rating, y, PC_GREEN);
00137 }
00138
00139 typedef GUIList<const Station*> GUIStationList;
00140
00144 class CompanyStationsWindow : public Window
00145 {
00146 protected:
00147
00148 static Listing last_sorting;
00149 static byte facilities;
00150 static bool include_empty;
00151 static const uint32 cargo_filter_max;
00152 static uint32 cargo_filter;
00153 static const Station *last_station;
00154
00155
00156 static const StringID sorter_names[];
00157 static GUIStationList::SortFunction * const sorter_funcs[];
00158
00159 GUIStationList stations;
00160 Scrollbar *vscroll;
00161
00167 void BuildStationsList(const Owner owner)
00168 {
00169 if (!this->stations.NeedRebuild()) return;
00170
00171 DEBUG(misc, 3, "Building station list for company %d", owner);
00172
00173 this->stations.Clear();
00174
00175 const Station *st;
00176 FOR_ALL_STATIONS(st) {
00177 if (st->owner == owner || (st->owner == OWNER_NONE && HasStationInUse(st->index, true, owner))) {
00178 if (this->facilities & st->facilities) {
00179 int num_waiting_cargo = 0;
00180 for (CargoID j = 0; j < NUM_CARGO; j++) {
00181 if (HasBit(st->goods[j].acceptance_pickup, GoodsEntry::GES_PICKUP)) {
00182 num_waiting_cargo++;
00183 if (HasBit(this->cargo_filter, j)) {
00184 *this->stations.Append() = st;
00185 break;
00186 }
00187 }
00188 }
00189
00190 if (num_waiting_cargo == 0 && this->include_empty) {
00191 *this->stations.Append() = st;
00192 }
00193 }
00194 }
00195 }
00196
00197 this->stations.Compact();
00198 this->stations.RebuildDone();
00199
00200 this->vscroll->SetCount(this->stations.Length());
00201 }
00202
00204 static int CDECL StationNameSorter(const Station * const *a, const Station * const *b)
00205 {
00206 static char buf_cache[64];
00207 char buf[64];
00208
00209 SetDParam(0, (*a)->index);
00210 GetString(buf, STR_STATION_NAME, lastof(buf));
00211
00212 if (*b != last_station) {
00213 last_station = *b;
00214 SetDParam(0, (*b)->index);
00215 GetString(buf_cache, STR_STATION_NAME, lastof(buf_cache));
00216 }
00217
00218 return strcmp(buf, buf_cache);
00219 }
00220
00222 static int CDECL StationTypeSorter(const Station * const *a, const Station * const *b)
00223 {
00224 return (*a)->facilities - (*b)->facilities;
00225 }
00226
00228 static int CDECL StationWaitingSorter(const Station * const *a, const Station * const *b)
00229 {
00230 Money diff = 0;
00231
00232 CargoID j;
00233 FOR_EACH_SET_CARGO_ID(j, cargo_filter) {
00234 if (!(*a)->goods[j].cargo.Empty()) diff += GetTransportedGoodsIncome((*a)->goods[j].cargo.Count(), 20, 50, j);
00235 if (!(*b)->goods[j].cargo.Empty()) diff -= GetTransportedGoodsIncome((*b)->goods[j].cargo.Count(), 20, 50, j);
00236 }
00237
00238 return ClampToI32(diff);
00239 }
00240
00242 static int CDECL StationRatingMaxSorter(const Station * const *a, const Station * const *b)
00243 {
00244 byte maxr1 = 0;
00245 byte maxr2 = 0;
00246
00247 CargoID j;
00248 FOR_EACH_SET_CARGO_ID(j, cargo_filter) {
00249 if (HasBit((*a)->goods[j].acceptance_pickup, GoodsEntry::GES_PICKUP)) maxr1 = max(maxr1, (*a)->goods[j].rating);
00250 if (HasBit((*b)->goods[j].acceptance_pickup, GoodsEntry::GES_PICKUP)) maxr2 = max(maxr2, (*b)->goods[j].rating);
00251 }
00252
00253 return maxr1 - maxr2;
00254 }
00255
00257 static int CDECL StationRatingMinSorter(const Station * const *a, const Station * const *b)
00258 {
00259 byte minr1 = 255;
00260 byte minr2 = 255;
00261
00262 for (CargoID j = 0; j < NUM_CARGO; j++) {
00263 if (!HasBit(cargo_filter, j)) continue;
00264 if (HasBit((*a)->goods[j].acceptance_pickup, GoodsEntry::GES_PICKUP)) minr1 = min(minr1, (*a)->goods[j].rating);
00265 if (HasBit((*b)->goods[j].acceptance_pickup, GoodsEntry::GES_PICKUP)) minr2 = min(minr2, (*b)->goods[j].rating);
00266 }
00267
00268 return -(minr1 - minr2);
00269 }
00270
00272 void SortStationsList()
00273 {
00274 if (!this->stations.Sort()) return;
00275
00276
00277 this->last_station = NULL;
00278
00279
00280 this->SetWidgetDirty(WID_STL_LIST);
00281 }
00282
00283 public:
00284 CompanyStationsWindow(const WindowDesc *desc, WindowNumber window_number) : Window()
00285 {
00286 this->stations.SetListing(this->last_sorting);
00287 this->stations.SetSortFuncs(this->sorter_funcs);
00288 this->stations.ForceRebuild();
00289 this->stations.NeedResort();
00290 this->SortStationsList();
00291
00292 this->CreateNestedTree(desc);
00293 this->vscroll = this->GetScrollbar(WID_STL_SCROLLBAR);
00294 this->FinishInitNested(desc, window_number);
00295 this->owner = (Owner)this->window_number;
00296
00297 CargoID cid;
00298 FOR_EACH_SET_CARGO_ID(cid, this->cargo_filter) {
00299 if (CargoSpec::Get(cid)->IsValid()) this->LowerWidget(WID_STL_CARGOSTART + cid);
00300 }
00301
00302 if (this->cargo_filter == this->cargo_filter_max) this->cargo_filter = _cargo_mask;
00303
00304 for (uint i = 0; i < 5; i++) {
00305 if (HasBit(this->facilities, i)) this->LowerWidget(i + WID_STL_TRAIN);
00306 }
00307 this->SetWidgetLoweredState(WID_STL_NOCARGOWAITING, this->include_empty);
00308
00309 this->GetWidget<NWidgetCore>(WID_STL_SORTDROPBTN)->widget_data = this->sorter_names[this->stations.SortType()];
00310 }
00311
00312 ~CompanyStationsWindow()
00313 {
00314 this->last_sorting = this->stations.GetListing();
00315 }
00316
00317 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00318 {
00319 switch (widget) {
00320 case WID_STL_SORTBY: {
00321 Dimension d = GetStringBoundingBox(this->GetWidget<NWidgetCore>(widget)->widget_data);
00322 d.width += padding.width + WD_SORTBUTTON_ARROW_WIDTH * 2;
00323 d.height += padding.height;
00324 *size = maxdim(*size, d);
00325 break;
00326 }
00327
00328 case WID_STL_SORTDROPBTN: {
00329 Dimension d = {0, 0};
00330 for (int i = 0; this->sorter_names[i] != INVALID_STRING_ID; i++) {
00331 d = maxdim(d, GetStringBoundingBox(this->sorter_names[i]));
00332 }
00333 d.width += padding.width;
00334 d.height += padding.height;
00335 *size = maxdim(*size, d);
00336 break;
00337 }
00338
00339 case WID_STL_LIST:
00340 resize->height = FONT_HEIGHT_NORMAL;
00341 size->height = WD_FRAMERECT_TOP + 5 * resize->height + WD_FRAMERECT_BOTTOM;
00342 break;
00343
00344 case WID_STL_TRAIN:
00345 case WID_STL_TRUCK:
00346 case WID_STL_BUS:
00347 case WID_STL_AIRPLANE:
00348 case WID_STL_SHIP:
00349 size->height = max<uint>(FONT_HEIGHT_SMALL, 10) + padding.height;
00350 break;
00351
00352 case WID_STL_CARGOALL:
00353 case WID_STL_FACILALL:
00354 case WID_STL_NOCARGOWAITING: {
00355 Dimension d = GetStringBoundingBox(widget == WID_STL_NOCARGOWAITING ? STR_ABBREV_NONE : STR_ABBREV_ALL);
00356 d.width += padding.width + 2;
00357 d.height += padding.height;
00358 *size = maxdim(*size, d);
00359 break;
00360 }
00361
00362 default:
00363 if (widget >= WID_STL_CARGOSTART) {
00364 const CargoSpec *cs = CargoSpec::Get(widget - WID_STL_CARGOSTART);
00365 if (cs->IsValid()) {
00366 Dimension d = GetStringBoundingBox(cs->abbrev);
00367 d.width += padding.width + 2;
00368 d.height += padding.height;
00369 *size = maxdim(*size, d);
00370 }
00371 }
00372 break;
00373 }
00374 }
00375
00376 virtual void OnPaint()
00377 {
00378 this->BuildStationsList((Owner)this->window_number);
00379 this->SortStationsList();
00380
00381 this->DrawWidgets();
00382 }
00383
00384 virtual void DrawWidget(const Rect &r, int widget) const
00385 {
00386 switch (widget) {
00387 case WID_STL_SORTBY:
00388
00389 this->DrawSortButtonState(WID_STL_SORTBY, this->stations.IsDescSortOrder() ? SBS_DOWN : SBS_UP);
00390 break;
00391
00392 case WID_STL_LIST: {
00393 bool rtl = _current_text_dir == TD_RTL;
00394 int max = min(this->vscroll->GetPosition() + this->vscroll->GetCapacity(), this->stations.Length());
00395 int y = r.top + WD_FRAMERECT_TOP;
00396 for (int i = this->vscroll->GetPosition(); i < max; ++i) {
00397 const Station *st = this->stations[i];
00398 assert(st->xy != INVALID_TILE);
00399
00400
00401
00402 assert(st->owner == owner || st->owner == OWNER_NONE);
00403
00404 SetDParam(0, st->index);
00405 SetDParam(1, st->facilities);
00406 int x = DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_STATION_LIST_STATION);
00407 x += rtl ? -5 : 5;
00408
00409
00410 for (CargoID j = 0; j < NUM_CARGO; j++) {
00411 if (!st->goods[j].cargo.Empty()) {
00412
00413
00414
00415
00416 if (rtl) {
00417 x -= 20;
00418 if (x < r.left + WD_FRAMERECT_LEFT) break;
00419 }
00420 StationsWndShowStationRating(x, x + 16, y, j, st->goods[j].cargo.Count(), st->goods[j].rating);
00421 if (!rtl) {
00422 x += 20;
00423 if (x > r.right - WD_FRAMERECT_RIGHT) break;
00424 }
00425 }
00426 }
00427 y += FONT_HEIGHT_NORMAL;
00428 }
00429
00430 if (this->vscroll->GetCount() == 0) {
00431 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_STATION_LIST_NONE);
00432 return;
00433 }
00434 break;
00435 }
00436
00437 case WID_STL_NOCARGOWAITING: {
00438 int cg_ofst = this->IsWidgetLowered(widget) ? 2 : 1;
00439 DrawString(r.left + cg_ofst, r.right + cg_ofst, r.top + cg_ofst, STR_ABBREV_NONE, TC_BLACK, SA_HOR_CENTER);
00440 break;
00441 }
00442
00443 case WID_STL_CARGOALL: {
00444 int cg_ofst = this->IsWidgetLowered(widget) ? 2 : 1;
00445 DrawString(r.left + cg_ofst, r.right + cg_ofst, r.top + cg_ofst, STR_ABBREV_ALL, TC_BLACK, SA_HOR_CENTER);
00446 break;
00447 }
00448
00449 case WID_STL_FACILALL: {
00450 int cg_ofst = this->IsWidgetLowered(widget) ? 2 : 1;
00451 DrawString(r.left + cg_ofst, r.right + cg_ofst, r.top + cg_ofst, STR_ABBREV_ALL, TC_BLACK);
00452 break;
00453 }
00454
00455 default:
00456 if (widget >= WID_STL_CARGOSTART) {
00457 const CargoSpec *cs = CargoSpec::Get(widget - WID_STL_CARGOSTART);
00458 if (cs->IsValid()) {
00459 int cg_ofst = HasBit(this->cargo_filter, cs->Index()) ? 2 : 1;
00460 GfxFillRect(r.left + cg_ofst, r.top + cg_ofst, r.right - 2 + cg_ofst, r.bottom - 2 + cg_ofst, cs->rating_colour);
00461 DrawString(r.left + cg_ofst, r.right + cg_ofst, r.top + cg_ofst, cs->abbrev, TC_BLACK, SA_HOR_CENTER);
00462 }
00463 }
00464 break;
00465 }
00466 }
00467
00468 virtual void SetStringParameters(int widget) const
00469 {
00470 if (widget == WID_STL_CAPTION) {
00471 SetDParam(0, this->window_number);
00472 SetDParam(1, this->vscroll->GetCount());
00473 }
00474 }
00475
00476 virtual void OnClick(Point pt, int widget, int click_count)
00477 {
00478 switch (widget) {
00479 case WID_STL_LIST: {
00480 uint id_v = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_STL_LIST, 0, FONT_HEIGHT_NORMAL);
00481 if (id_v >= this->stations.Length()) return;
00482
00483 const Station *st = this->stations[id_v];
00484
00485 assert(st->owner == (Owner)this->window_number || st->owner == OWNER_NONE);
00486
00487 if (_ctrl_pressed) {
00488 ShowExtraViewPortWindow(st->xy);
00489 } else {
00490 ScrollMainWindowToTile(st->xy);
00491 }
00492 break;
00493 }
00494
00495 case WID_STL_TRAIN:
00496 case WID_STL_TRUCK:
00497 case WID_STL_BUS:
00498 case WID_STL_AIRPLANE:
00499 case WID_STL_SHIP:
00500 if (_ctrl_pressed) {
00501 ToggleBit(this->facilities, widget - WID_STL_TRAIN);
00502 this->ToggleWidgetLoweredState(widget);
00503 } else {
00504 uint i;
00505 FOR_EACH_SET_BIT(i, this->facilities) {
00506 this->RaiseWidget(i + WID_STL_TRAIN);
00507 }
00508 this->facilities = 1 << (widget - WID_STL_TRAIN);
00509 this->LowerWidget(widget);
00510 }
00511 this->stations.ForceRebuild();
00512 this->SetDirty();
00513 break;
00514
00515 case WID_STL_FACILALL:
00516 for (uint i = WID_STL_TRAIN; i <= WID_STL_SHIP; i++) {
00517 this->LowerWidget(i);
00518 }
00519
00520 this->facilities = FACIL_TRAIN | FACIL_TRUCK_STOP | FACIL_BUS_STOP | FACIL_AIRPORT | FACIL_DOCK;
00521 this->stations.ForceRebuild();
00522 this->SetDirty();
00523 break;
00524
00525 case WID_STL_CARGOALL: {
00526 for (uint i = 0; i < NUM_CARGO; i++) {
00527 const CargoSpec *cs = CargoSpec::Get(i);
00528 if (cs->IsValid()) this->LowerWidget(WID_STL_CARGOSTART + i);
00529 }
00530 this->LowerWidget(WID_STL_NOCARGOWAITING);
00531
00532 this->cargo_filter = _cargo_mask;
00533 this->include_empty = true;
00534 this->stations.ForceRebuild();
00535 this->SetDirty();
00536 break;
00537 }
00538
00539 case WID_STL_SORTBY:
00540 this->stations.ToggleSortOrder();
00541 this->SetTimeout();
00542 this->LowerWidget(WID_STL_SORTBY);
00543 this->SetDirty();
00544 break;
00545
00546 case WID_STL_SORTDROPBTN:
00547 ShowDropDownMenu(this, this->sorter_names, this->stations.SortType(), WID_STL_SORTDROPBTN, 0, 0);
00548 break;
00549
00550 case WID_STL_NOCARGOWAITING:
00551 if (_ctrl_pressed) {
00552 this->include_empty = !this->include_empty;
00553 this->ToggleWidgetLoweredState(WID_STL_NOCARGOWAITING);
00554 } else {
00555 for (uint i = 0; i < NUM_CARGO; i++) {
00556 const CargoSpec *cs = CargoSpec::Get(i);
00557 if (cs->IsValid()) this->RaiseWidget(WID_STL_CARGOSTART + i);
00558 }
00559
00560 this->cargo_filter = 0;
00561 this->include_empty = true;
00562
00563 this->LowerWidget(WID_STL_NOCARGOWAITING);
00564 }
00565 this->stations.ForceRebuild();
00566 this->SetDirty();
00567 break;
00568
00569 default:
00570 if (widget >= WID_STL_CARGOSTART) {
00571
00572 const CargoSpec *cs = CargoSpec::Get(widget - WID_STL_CARGOSTART);
00573 if (!cs->IsValid()) break;
00574
00575 if (_ctrl_pressed) {
00576 ToggleBit(this->cargo_filter, cs->Index());
00577 this->ToggleWidgetLoweredState(widget);
00578 } else {
00579 for (uint i = 0; i < NUM_CARGO; i++) {
00580 const CargoSpec *cs = CargoSpec::Get(i);
00581 if (cs->IsValid()) this->RaiseWidget(WID_STL_CARGOSTART + i);
00582 }
00583 this->RaiseWidget(WID_STL_NOCARGOWAITING);
00584
00585 this->cargo_filter = 0;
00586 this->include_empty = false;
00587
00588 SetBit(this->cargo_filter, cs->Index());
00589 this->LowerWidget(widget);
00590 }
00591 this->stations.ForceRebuild();
00592 this->SetDirty();
00593 }
00594 break;
00595 }
00596 }
00597
00598 virtual void OnDropdownSelect(int widget, int index)
00599 {
00600 if (this->stations.SortType() != index) {
00601 this->stations.SetSortType(index);
00602
00603
00604 this->GetWidget<NWidgetCore>(WID_STL_SORTDROPBTN)->widget_data = this->sorter_names[this->stations.SortType()];
00605
00606 this->SetDirty();
00607 }
00608 }
00609
00610 virtual void OnTick()
00611 {
00612 if (_pause_mode != PM_UNPAUSED) return;
00613 if (this->stations.NeedResort()) {
00614 DEBUG(misc, 3, "Periodic rebuild station list company %d", this->window_number);
00615 this->SetDirty();
00616 }
00617 }
00618
00619 virtual void OnTimeout()
00620 {
00621 this->RaiseWidget(WID_STL_SORTBY);
00622 this->SetDirty();
00623 }
00624
00625 virtual void OnResize()
00626 {
00627 this->vscroll->SetCapacityFromWidget(this, WID_STL_LIST, WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM);
00628 }
00629
00635 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
00636 {
00637 if (data == 0) {
00638
00639 this->stations.ForceRebuild();
00640 } else {
00641 this->stations.ForceResort();
00642 }
00643 }
00644 };
00645
00646 Listing CompanyStationsWindow::last_sorting = {false, 0};
00647 byte CompanyStationsWindow::facilities = FACIL_TRAIN | FACIL_TRUCK_STOP | FACIL_BUS_STOP | FACIL_AIRPORT | FACIL_DOCK;
00648 bool CompanyStationsWindow::include_empty = true;
00649 const uint32 CompanyStationsWindow::cargo_filter_max = UINT32_MAX;
00650 uint32 CompanyStationsWindow::cargo_filter = UINT32_MAX;
00651 const Station *CompanyStationsWindow::last_station = NULL;
00652
00653
00654 GUIStationList::SortFunction * const CompanyStationsWindow::sorter_funcs[] = {
00655 &StationNameSorter,
00656 &StationTypeSorter,
00657 &StationWaitingSorter,
00658 &StationRatingMaxSorter,
00659 &StationRatingMinSorter
00660 };
00661
00662
00663 const StringID CompanyStationsWindow::sorter_names[] = {
00664 STR_SORT_BY_NAME,
00665 STR_SORT_BY_FACILITY,
00666 STR_SORT_BY_WAITING,
00667 STR_SORT_BY_RATING_MAX,
00668 STR_SORT_BY_RATING_MIN,
00669 INVALID_STRING_ID
00670 };
00671
00677 static NWidgetBase *CargoWidgets(int *biggest_index)
00678 {
00679 NWidgetHorizontal *container = new NWidgetHorizontal();
00680
00681 for (uint i = 0; i < NUM_CARGO; i++) {
00682 const CargoSpec *cs = CargoSpec::Get(i);
00683 if (cs->IsValid()) {
00684 NWidgetBackground *panel = new NWidgetBackground(WWT_PANEL, COLOUR_GREY, WID_STL_CARGOSTART + i);
00685 panel->SetMinimalSize(14, 11);
00686 panel->SetResize(0, 0);
00687 panel->SetFill(0, 1);
00688 panel->SetDataTip(0, STR_STATION_LIST_USE_CTRL_TO_SELECT_MORE);
00689 container->Add(panel);
00690 } else {
00691 NWidgetLeaf *nwi = new NWidgetLeaf(WWT_EMPTY, COLOUR_GREY, WID_STL_CARGOSTART + i, 0x0, STR_NULL);
00692 nwi->SetMinimalSize(0, 11);
00693 nwi->SetResize(0, 0);
00694 nwi->SetFill(0, 1);
00695 container->Add(nwi);
00696 }
00697 }
00698 *biggest_index = WID_STL_CARGOSTART + NUM_CARGO;
00699 return container;
00700 }
00701
00702 static const NWidgetPart _nested_company_stations_widgets[] = {
00703 NWidget(NWID_HORIZONTAL),
00704 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
00705 NWidget(WWT_CAPTION, COLOUR_GREY, WID_STL_CAPTION), SetDataTip(STR_STATION_LIST_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00706 NWidget(WWT_SHADEBOX, COLOUR_GREY),
00707 NWidget(WWT_STICKYBOX, COLOUR_GREY),
00708 EndContainer(),
00709 NWidget(NWID_HORIZONTAL),
00710 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_STL_TRAIN), SetMinimalSize(14, 11), SetDataTip(STR_TRAIN, STR_STATION_LIST_USE_CTRL_TO_SELECT_MORE), SetFill(0, 1),
00711 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_STL_TRUCK), SetMinimalSize(14, 11), SetDataTip(STR_LORRY, STR_STATION_LIST_USE_CTRL_TO_SELECT_MORE), SetFill(0, 1),
00712 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_STL_BUS), SetMinimalSize(14, 11), SetDataTip(STR_BUS, STR_STATION_LIST_USE_CTRL_TO_SELECT_MORE), SetFill(0, 1),
00713 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_STL_SHIP), SetMinimalSize(14, 11), SetDataTip(STR_SHIP, STR_STATION_LIST_USE_CTRL_TO_SELECT_MORE), SetFill(0, 1),
00714 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_STL_AIRPLANE), SetMinimalSize(14, 11), SetDataTip(STR_PLANE, STR_STATION_LIST_USE_CTRL_TO_SELECT_MORE), SetFill(0, 1),
00715 NWidget(WWT_PUSHBTN, COLOUR_GREY, WID_STL_FACILALL), SetMinimalSize(14, 11), SetDataTip(0x0, STR_STATION_LIST_SELECT_ALL_FACILITIES), SetFill(0, 1),
00716 NWidget(WWT_PANEL, COLOUR_GREY), SetMinimalSize(5, 11), SetFill(0, 1), EndContainer(),
00717 NWidgetFunction(CargoWidgets),
00718 NWidget(WWT_PANEL, COLOUR_GREY, WID_STL_NOCARGOWAITING), SetMinimalSize(14, 11), SetDataTip(0x0, STR_STATION_LIST_NO_WAITING_CARGO), SetFill(0, 1), EndContainer(),
00719 NWidget(WWT_PUSHBTN, COLOUR_GREY, WID_STL_CARGOALL), SetMinimalSize(14, 11), SetDataTip(0x0, STR_STATION_LIST_SELECT_ALL_TYPES), SetFill(0, 1),
00720 NWidget(WWT_PANEL, COLOUR_GREY), SetDataTip(0x0, STR_NULL), SetResize(1, 0), SetFill(1, 1), EndContainer(),
00721 EndContainer(),
00722 NWidget(NWID_HORIZONTAL),
00723 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_STL_SORTBY), SetMinimalSize(81, 12), SetDataTip(STR_BUTTON_SORT_BY, STR_TOOLTIP_SORT_ORDER),
00724 NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_STL_SORTDROPBTN), SetMinimalSize(163, 12), SetDataTip(STR_SORT_BY_NAME, STR_TOOLTIP_SORT_CRITERIA),
00725 NWidget(WWT_PANEL, COLOUR_GREY), SetDataTip(0x0, STR_NULL), SetResize(1, 0), SetFill(1, 1), EndContainer(),
00726 EndContainer(),
00727 NWidget(NWID_HORIZONTAL),
00728 NWidget(WWT_PANEL, COLOUR_GREY, WID_STL_LIST), SetMinimalSize(346, 125), SetResize(1, 10), SetDataTip(0x0, STR_STATION_LIST_TOOLTIP), SetScrollbar(WID_STL_SCROLLBAR), EndContainer(),
00729 NWidget(NWID_VERTICAL),
00730 NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_STL_SCROLLBAR),
00731 NWidget(WWT_RESIZEBOX, COLOUR_GREY),
00732 EndContainer(),
00733 EndContainer(),
00734 };
00735
00736 static const WindowDesc _company_stations_desc(
00737 WDP_AUTO, 358, 162,
00738 WC_STATION_LIST, WC_NONE,
00739 WDF_UNCLICK_BUTTONS,
00740 _nested_company_stations_widgets, lengthof(_nested_company_stations_widgets)
00741 );
00742
00748 void ShowCompanyStations(CompanyID company)
00749 {
00750 if (!Company::IsValidID(company)) return;
00751
00752 AllocateWindowDescFront<CompanyStationsWindow>(&_company_stations_desc, company);
00753 }
00754
00755 static const NWidgetPart _nested_station_view_widgets[] = {
00756 NWidget(NWID_HORIZONTAL),
00757 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
00758 NWidget(WWT_CAPTION, COLOUR_GREY, WID_SV_CAPTION), SetDataTip(STR_STATION_VIEW_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00759 NWidget(WWT_SHADEBOX, COLOUR_GREY),
00760 NWidget(WWT_STICKYBOX, COLOUR_GREY),
00761 EndContainer(),
00762 NWidget(NWID_HORIZONTAL),
00763 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SV_SORT_ORDER), SetMinimalSize(81, 12), SetFill(1, 1), SetDataTip(STR_BUTTON_SORT_BY, STR_TOOLTIP_SORT_ORDER),
00764 NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_SV_SORT_BY), SetMinimalSize(168, 12), SetResize(1, 0), SetFill(0, 1), SetDataTip(0x0, STR_TOOLTIP_SORT_CRITERIA),
00765 EndContainer(),
00766 NWidget(NWID_HORIZONTAL),
00767 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_SV_GROUP), SetMinimalSize(81, 12), SetFill(1, 1), SetDataTip(STR_STATION_VIEW_GROUP, 0x0),
00768 NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_SV_GROUP_BY), SetMinimalSize(168, 12), SetResize(1, 0), SetFill(0, 1), SetDataTip(0x0, STR_TOOLTIP_GROUP_ORDER),
00769 EndContainer(),
00770 NWidget(NWID_HORIZONTAL),
00771 NWidget(WWT_PANEL, COLOUR_GREY, WID_SV_WAITING), SetMinimalSize(237, 44), SetResize(1, 10), SetScrollbar(WID_SV_SCROLLBAR), EndContainer(),
00772 NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_SV_SCROLLBAR),
00773 EndContainer(),
00774 NWidget(WWT_PANEL, COLOUR_GREY, WID_SV_ACCEPT_RATING_LIST), SetMinimalSize(249, 23), SetResize(1, 0), EndContainer(),
00775 NWidget(NWID_HORIZONTAL),
00776 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
00777 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SV_LOCATION), SetMinimalSize(45, 12), SetResize(1, 0), SetFill(1, 1),
00778 SetDataTip(STR_BUTTON_LOCATION, STR_STATION_VIEW_CENTER_TOOLTIP),
00779 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SV_ACCEPTS_RATINGS), SetMinimalSize(46, 12), SetResize(1, 0), SetFill(1, 1),
00780 SetDataTip(STR_STATION_VIEW_RATINGS_BUTTON, STR_STATION_VIEW_RATINGS_TOOLTIP),
00781 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SV_RENAME), SetMinimalSize(45, 12), SetResize(1, 0), SetFill(1, 1),
00782 SetDataTip(STR_BUTTON_RENAME, STR_STATION_VIEW_RENAME_TOOLTIP),
00783 EndContainer(),
00784 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_SV_CLOSE_AIRPORT), SetMinimalSize(45, 12), SetResize(1, 0), SetFill(1, 1),
00785 SetDataTip(STR_STATION_VIEW_CLOSE_AIRPORT, STR_STATION_VIEW_CLOSE_AIRPORT_TOOLTIP),
00786 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SV_TRAINS), SetMinimalSize(14, 12), SetFill(0, 1), SetDataTip(STR_TRAIN, STR_STATION_VIEW_SCHEDULED_TRAINS_TOOLTIP),
00787 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SV_ROADVEHS), SetMinimalSize(14, 12), SetFill(0, 1), SetDataTip(STR_LORRY, STR_STATION_VIEW_SCHEDULED_ROAD_VEHICLES_TOOLTIP),
00788 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SV_SHIPS), SetMinimalSize(14, 12), SetFill(0, 1), SetDataTip(STR_SHIP, STR_STATION_VIEW_SCHEDULED_SHIPS_TOOLTIP),
00789 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SV_PLANES), SetMinimalSize(14, 12), SetFill(0, 1), SetDataTip(STR_PLANE, STR_STATION_VIEW_SCHEDULED_AIRCRAFT_TOOLTIP),
00790 NWidget(WWT_RESIZEBOX, COLOUR_GREY),
00791 EndContainer(),
00792 };
00793
00804 static void DrawCargoIcons(CargoID i, uint waiting, int left, int right, int y)
00805 {
00806 uint num = min((waiting + 5) / 10, (right - left) / 10);
00807 if (num == 0) return;
00808
00809 SpriteID sprite = CargoSpec::Get(i)->GetCargoIcon();
00810
00811 int x = _current_text_dir == TD_RTL ? left : right - num * 10;
00812 do {
00813 DrawSprite(sprite, PAL_NONE, x, y);
00814 x += 10;
00815 } while (--num);
00816 }
00817
00818 CargoDataEntry::CargoDataEntry() :
00819 parent(NULL),
00820 station(INVALID_STATION),
00821 num_children(0),
00822 count(0),
00823 children(new CargoDataSet(CargoSorter(ST_CARGO_ID)))
00824 {}
00825
00826 CargoDataEntry::CargoDataEntry(CargoID cargo, uint count, CargoDataEntry *parent) :
00827 parent(parent),
00828 cargo(cargo),
00829 num_children(0),
00830 count(count),
00831 children(new CargoDataSet)
00832 {}
00833
00834 CargoDataEntry::CargoDataEntry(StationID station, uint count, CargoDataEntry *parent) :
00835 parent(parent),
00836 station(station),
00837 num_children(0),
00838 count(count),
00839 children(new CargoDataSet)
00840 {}
00841
00842 CargoDataEntry::CargoDataEntry(StationID station) :
00843 parent(NULL),
00844 station(station),
00845 num_children(0),
00846 count(0),
00847 children(NULL)
00848 {}
00849
00850 CargoDataEntry::CargoDataEntry(CargoID cargo) :
00851 parent(NULL),
00852 cargo(cargo),
00853 num_children(0),
00854 count(0),
00855 children(NULL)
00856 {}
00857
00858 CargoDataEntry::~CargoDataEntry()
00859 {
00860 this->Clear();
00861 delete this->children;
00862 }
00863
00867 void CargoDataEntry::Clear()
00868 {
00869 if (this->children != NULL) {
00870 for (CargoDataSet::iterator i = this->children->begin(); i != this->children->end(); ++i) {
00871 assert(*i != this);
00872 delete *i;
00873 }
00874 this->children->clear();
00875 }
00876 if (this->parent != NULL) this->parent->count -= this->count;
00877 this->count = 0;
00878 this->num_children = 0;
00879 }
00880
00887 void CargoDataEntry::Remove(CargoDataEntry *child)
00888 {
00889 CargoDataSet::iterator i = this->children->find(child);
00890 if (i != this->children->end()) {
00891 delete *i;
00892 this->children->erase(i);
00893 }
00894 }
00895
00902 template<class ID>
00903 CargoDataEntry *CargoDataEntry::InsertOrRetrieve(ID child_id)
00904 {
00905 CargoDataEntry tmp(child_id);
00906 CargoDataSet::iterator i = this->children->find(&tmp);
00907 if (i == this->children->end()) {
00908 IncrementSize();
00909 return *(this->children->insert(new CargoDataEntry(child_id, 0, this)).first);
00910 } else {
00911 CargoDataEntry *ret = *i;
00912 assert(this->children->value_comp().GetSortType() != ST_COUNT);
00913 return ret;
00914 }
00915 }
00916
00922 void CargoDataEntry::Update(uint count)
00923 {
00924 this->count += count;
00925 if (this->parent != NULL) this->parent->Update(count);
00926 }
00927
00931 void CargoDataEntry::IncrementSize()
00932 {
00933 ++this->num_children;
00934 if (this->parent != NULL) this->parent->IncrementSize();
00935 }
00936
00937 void CargoDataEntry::Resort(CargoSortType type, SortOrder order)
00938 {
00939 CargoDataSet *new_subs = new CargoDataSet(this->children->begin(), this->children->end(), CargoSorter(type, order));
00940 delete this->children;
00941 this->children = new_subs;
00942 }
00943
00944 CargoDataEntry *CargoDataEntry::Retrieve(CargoDataSet::iterator i) const
00945 {
00946 if (i == this->children->end()) {
00947 return NULL;
00948 } else {
00949 assert(this->children->value_comp().GetSortType() != ST_COUNT);
00950 return *i;
00951 }
00952 }
00953
00954 bool CargoSorter::operator()(const CargoDataEntry *cd1, const CargoDataEntry *cd2) const
00955 {
00956 switch (this->type) {
00957 case ST_STATION_ID:
00958 return this->SortId<StationID>(cd1->GetStation(), cd2->GetStation());
00959 break;
00960 case ST_CARGO_ID:
00961 return this->SortId<CargoID>(cd1->GetCargo(), cd2->GetCargo());
00962 break;
00963 case ST_COUNT:
00964 return this->SortCount(cd1, cd2);
00965 break;
00966 case ST_STATION_STRING:
00967 return this->SortStation(cd1->GetStation(), cd2->GetStation());
00968 break;
00969 default:
00970 NOT_REACHED();
00971 }
00972 }
00973
00974 template<class ID>
00975 bool CargoSorter::SortId(ID st1, ID st2) const
00976 {
00977 return (this->order == SO_ASCENDING) ? st1 < st2 : st2 < st1;
00978 }
00979
00980 bool CargoSorter::SortCount(const CargoDataEntry *cd1, const CargoDataEntry *cd2) const
00981 {
00982 uint c1 = cd1->GetCount();
00983 uint c2 = cd2->GetCount();
00984 if (c1 == c2) {
00985 return this->SortStation(cd1->GetStation(), cd2->GetStation());
00986 } else if (this->order == SO_ASCENDING) {
00987 return c1 < c2;
00988 } else {
00989 return c2 < c1;
00990 }
00991 }
00992
00993 bool CargoSorter::SortStation(StationID st1, StationID st2) const
00994 {
00995 static char buf1[MAX_LENGTH_STATION_NAME_CHARS];
00996 static char buf2[MAX_LENGTH_STATION_NAME_CHARS];
00997
00998 if (!Station::IsValidID(st1)) {
00999 return Station::IsValidID(st2) ? this->order == SO_ASCENDING : this->SortId(st1, st2);
01000 } else if (!Station::IsValidID(st2)) {
01001 return order == SO_DESCENDING;
01002 }
01003
01004 SetDParam(0, st1);
01005 GetString(buf1, STR_STATION_NAME, lastof(buf1));
01006 SetDParam(0, st2);
01007 GetString(buf2, STR_STATION_NAME, lastof(buf2));
01008
01009 int res = strcmp(buf1, buf2);
01010 if (res == 0) {
01011 return this->SortId(st1, st2);
01012 } else {
01013 return (this->order == SO_ASCENDING) ? res < 0 : res > 0;
01014 }
01015 }
01016
01020 struct StationViewWindow : public Window {
01024 struct RowDisplay {
01025 RowDisplay(CargoDataEntry *f, StationID n) : filter(f), next_station(n) {}
01026 RowDisplay(CargoDataEntry *f, CargoID n) : filter(f), next_cargo(n) {}
01027
01031 CargoDataEntry *filter;
01032 union {
01036 StationID next_station;
01037
01041 CargoID next_cargo;
01042 };
01043 };
01044
01045 typedef std::vector<RowDisplay> CargoDataVector;
01046
01047 static const int NUM_COLUMNS = 4;
01048
01052 enum Invalidation {
01053 INV_FLOWS = 0x100,
01054 INV_CARGO = 0x200
01055 };
01056
01060 enum Grouping {
01061 GR_SOURCE,
01062 GR_NEXT,
01063 GR_DESTINATION,
01064 GR_CARGO,
01065 };
01066
01070 enum Mode {
01071 MODE_WAITING,
01072 MODE_PLANNED
01073 };
01074
01075 uint expand_shrink_width;
01076 int rating_lines;
01077 int accepts_lines;
01078 Scrollbar *vscroll;
01079
01081 enum AcceptListHeight {
01082 ALH_RATING = 13,
01083 ALH_ACCEPTS = 3,
01084 };
01085
01086 static const StringID _sort_names[];
01087 static const StringID _group_names[];
01088
01095 CargoSortType sortings[NUM_COLUMNS];
01096
01098 SortOrder sort_orders[NUM_COLUMNS];
01099
01100 int scroll_to_row;
01101 int grouping_index;
01102 Mode current_mode;
01103 Grouping groupings[NUM_COLUMNS];
01104
01105 CargoDataEntry expanded_rows;
01106 CargoDataEntry cached_destinations;
01107 CargoDataVector displayed_rows;
01108
01109 StationViewWindow(const WindowDesc *desc, WindowNumber window_number) : Window(),
01110 scroll_to_row(INT_MAX), grouping_index(0)
01111 {
01112 this->rating_lines = ALH_RATING;
01113 this->accepts_lines = ALH_ACCEPTS;
01114
01115 this->CreateNestedTree(desc);
01116 this->vscroll = this->GetScrollbar(WID_SV_SCROLLBAR);
01117
01118 this->FinishInitNested(desc, window_number);
01119
01120 this->groupings[0] = GR_CARGO;
01121 this->sortings[0] = ST_AS_GROUPING;
01122 this->SelectGroupBy(_settings_client.gui.station_gui_group_order);
01123 this->SelectSortBy(_settings_client.gui.station_gui_sort_by);
01124 this->sort_orders[0] = SO_ASCENDING;
01125 this->SelectSortOrder((SortOrder)_settings_client.gui.station_gui_sort_order);
01126 Owner owner = Station::Get(window_number)->owner;
01127 if (owner != OWNER_NONE) this->owner = owner;
01128 }
01129
01130 ~StationViewWindow()
01131 {
01132 Owner owner = Station::Get(this->window_number)->owner;
01133 if (!Company::IsValidID(owner)) owner = _local_company;
01134 if (!Company::IsValidID(owner)) return;
01135 DeleteWindowById(WC_TRAINS_LIST, VehicleListIdentifier(VL_STATION_LIST, VEH_TRAIN, owner, this->window_number).Pack(), false);
01136 DeleteWindowById(WC_ROADVEH_LIST, VehicleListIdentifier(VL_STATION_LIST, VEH_ROAD, owner, this->window_number).Pack(), false);
01137 DeleteWindowById(WC_SHIPS_LIST, VehicleListIdentifier(VL_STATION_LIST, VEH_SHIP, owner, this->window_number).Pack(), false);
01138 DeleteWindowById(WC_AIRCRAFT_LIST, VehicleListIdentifier(VL_STATION_LIST, VEH_AIRCRAFT, owner, this->window_number).Pack(), false);
01139 }
01140
01151 void ShowCargo(CargoDataEntry *data, CargoID cargo, StationID source, StationID next, StationID dest, uint count)
01152 {
01153 if (count == 0) return;
01154 const CargoDataEntry *expand = &this->expanded_rows;
01155 for (int i = 0; i < NUM_COLUMNS && expand != NULL; ++i) {
01156 switch (groupings[i]) {
01157 case GR_CARGO:
01158 assert(i == 0);
01159 data = data->InsertOrRetrieve(cargo);
01160 expand = expand->Retrieve(cargo);
01161 break;
01162 case GR_SOURCE:
01163 data = data->InsertOrRetrieve(source);
01164 expand = expand->Retrieve(source);
01165 break;
01166 case GR_NEXT:
01167 data = data->InsertOrRetrieve(next);
01168 expand = expand->Retrieve(next);
01169 break;
01170 case GR_DESTINATION:
01171 data = data->InsertOrRetrieve(dest);
01172 expand = expand->Retrieve(dest);
01173 break;
01174 }
01175 }
01176 data->Update(count);
01177 }
01178
01179 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
01180 {
01181 switch (widget) {
01182 case WID_SV_WAITING:
01183 resize->height = FONT_HEIGHT_NORMAL;
01184 size->height = WD_FRAMERECT_TOP + 4 * resize->height + WD_FRAMERECT_BOTTOM;
01185 this->expand_shrink_width = max(GetStringBoundingBox("-").width, GetStringBoundingBox("+").width) + WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
01186 break;
01187
01188 case WID_SV_ACCEPT_RATING_LIST:
01189 size->height = WD_FRAMERECT_TOP + ((this->GetWidget<NWidgetCore>(WID_SV_ACCEPTS_RATINGS)->widget_data == STR_STATION_VIEW_RATINGS_BUTTON) ? this->accepts_lines : this->rating_lines) * FONT_HEIGHT_NORMAL + WD_FRAMERECT_BOTTOM;
01190 break;
01191
01192 case WID_SV_CLOSE_AIRPORT:
01193 if (!(Station::Get(this->window_number)->facilities & FACIL_AIRPORT)) {
01194
01195 size->width = 0;
01196 resize->width = 0;
01197 fill->width = 0;
01198 }
01199 break;
01200 }
01201 }
01202
01203 virtual void OnPaint()
01204 {
01205 const Station *st = Station::Get(this->window_number);
01206 CargoDataEntry cargo;
01207 BuildCargoList(&cargo, st);
01208
01209 this->vscroll->SetCount(cargo.GetNumChildren());
01210
01211
01212 this->SetWidgetDisabledState(WID_SV_RENAME, st->owner != _local_company);
01213 this->SetWidgetDisabledState(WID_SV_TRAINS, !(st->facilities & FACIL_TRAIN));
01214 this->SetWidgetDisabledState(WID_SV_ROADVEHS, !(st->facilities & FACIL_TRUCK_STOP) && !(st->facilities & FACIL_BUS_STOP));
01215 this->SetWidgetDisabledState(WID_SV_SHIPS, !(st->facilities & FACIL_DOCK));
01216 this->SetWidgetDisabledState(WID_SV_PLANES, !(st->facilities & FACIL_AIRPORT));
01217 this->SetWidgetDisabledState(WID_SV_CLOSE_AIRPORT, !(st->facilities & FACIL_AIRPORT) || st->owner != _local_company);
01218 this->SetWidgetLoweredState(WID_SV_CLOSE_AIRPORT, (st->facilities & FACIL_AIRPORT) && (st->airport.flags & AIRPORT_CLOSED_block) != 0);
01219
01220 SetDParam(0, st->index);
01221 SetDParam(1, st->facilities);
01222 this->DrawWidgets();
01223
01224 if (!this->IsShaded()) {
01225
01226 const NWidgetBase *wid = this->GetWidget<NWidgetBase>(WID_SV_ACCEPT_RATING_LIST);
01227 const Rect r = {wid->pos_x, wid->pos_y, wid->pos_x + wid->current_x - 1, wid->pos_y + wid->current_y - 1};
01228 if (this->GetWidget<NWidgetCore>(WID_SV_ACCEPTS_RATINGS)->widget_data == STR_STATION_VIEW_RATINGS_BUTTON) {
01229 int lines = this->DrawAcceptedCargo(r);
01230 if (lines > this->accepts_lines) {
01231 this->accepts_lines = lines;
01232 this->ReInit();
01233 return;
01234 }
01235 } else {
01236 int lines = this->DrawCargoRatings(r);
01237 if (lines > this->rating_lines) {
01238 this->rating_lines = lines;
01239 this->ReInit();
01240 return;
01241 }
01242 }
01243
01244
01245 this->DrawSortButtonState(WID_SV_SORT_ORDER, sort_orders[1] == SO_ASCENDING ? SBS_UP : SBS_DOWN);
01246
01247 int pos = this->vscroll->GetPosition();
01248
01249 int maxrows = this->vscroll->GetCapacity();
01250
01251 displayed_rows.clear();
01252
01253
01254 NWidgetBase *nwi = this->GetWidget<NWidgetBase>(WID_SV_WAITING);
01255 Rect waiting_rect = {nwi->pos_x, nwi->pos_y, nwi->pos_x + nwi->current_x - 1, nwi->pos_y + nwi->current_y - 1};
01256 this->DrawEntries(&cargo, waiting_rect, pos, maxrows, 0);
01257 scroll_to_row = INT_MAX;
01258 }
01259 }
01260
01261 virtual void SetStringParameters(int widget) const
01262 {
01263 if (widget == WID_SV_CAPTION) {
01264 const Station *st = Station::Get(this->window_number);
01265 SetDParam(0, st->index);
01266 SetDParam(1, st->facilities);
01267 }
01268 }
01269
01275 void RecalcDestinations(CargoID i)
01276 {
01277 const Station *st = Station::Get(this->window_number);
01278 CargoDataEntry *cargo_entry = cached_destinations.InsertOrRetrieve(i);
01279 cargo_entry->Clear();
01280
01281 const FlowStatMap &flows = st->goods[i].flows;
01282 for (FlowStatMap::const_iterator it = flows.begin(); it != flows.end(); ++it) {
01283 StationID from = it->first;
01284 CargoDataEntry *source_entry = cargo_entry->InsertOrRetrieve(from);
01285 const FlowStat::SharesMap *shares = it->second.GetShares();
01286 uint32 prev_count = 0;
01287 for (FlowStat::SharesMap::const_iterator flow_it = shares->begin(); flow_it != shares->end(); ++flow_it) {
01288 StationID via = flow_it->second;
01289 CargoDataEntry *via_entry = source_entry->InsertOrRetrieve(via);
01290 if (via == this->window_number) {
01291 via_entry->InsertOrRetrieve(via)->Update(flow_it->first - prev_count);
01292 } else {
01293 EstimateDestinations(i, from, via, flow_it->first - prev_count, via_entry);
01294 }
01295 prev_count = flow_it->first;
01296 }
01297 }
01298 }
01299
01309 void EstimateDestinations(CargoID cargo, StationID source, StationID next, uint count, CargoDataEntry *dest)
01310 {
01311 if (Station::IsValidID(next) && Station::IsValidID(source)) {
01312 CargoDataEntry tmp;
01313 const FlowStatMap &flowmap = Station::Get(next)->goods[cargo].flows;
01314 FlowStatMap::const_iterator map_it = flowmap.find(source);
01315 if (map_it != flowmap.end()) {
01316 const FlowStat::SharesMap *shares = map_it->second.GetShares();
01317 uint32 prev_count = 0;
01318 for (FlowStat::SharesMap::const_iterator i = shares->begin(); i != shares->end(); ++i) {
01319 tmp.InsertOrRetrieve(i->second)->Update(i->first - prev_count);
01320 prev_count = i->first;
01321 }
01322 }
01323
01324 if (tmp.GetCount() == 0) {
01325 dest->InsertOrRetrieve(INVALID_STATION)->Update(count);
01326 } else {
01327 uint sum_estimated = 0;
01328 while (sum_estimated < count) {
01329 for (CargoDataSet::iterator i = tmp.Begin(); i != tmp.End() && sum_estimated < count; ++i) {
01330 CargoDataEntry *child = *i;
01331 uint estimate = DivideApprox(child->GetCount() * count, tmp.GetCount());
01332 if (estimate == 0) estimate = 1;
01333
01334 sum_estimated += estimate;
01335 if (sum_estimated > count) {
01336 estimate -= sum_estimated - count;
01337 sum_estimated = count;
01338 }
01339
01340 if (estimate > 0) {
01341 if (child->GetStation() == next) {
01342 dest->InsertOrRetrieve(next)->Update(estimate);
01343 } else {
01344 EstimateDestinations(cargo, source, child->GetStation(), estimate, dest);
01345 }
01346 }
01347 }
01348
01349 }
01350 }
01351 } else {
01352 dest->InsertOrRetrieve(INVALID_STATION)->Update(count);
01353 }
01354 }
01355
01362 void BuildFlowList(CargoID i, const FlowStatMap &flows, CargoDataEntry *cargo)
01363 {
01364 const CargoDataEntry *source_dest = this->cached_destinations.Retrieve(i);
01365 for (FlowStatMap::const_iterator it = flows.begin(); it != flows.end(); ++it) {
01366 StationID from = it->first;
01367 const CargoDataEntry *source_entry = source_dest->Retrieve(from);
01368 const FlowStat::SharesMap *shares = it->second.GetShares();
01369 for (FlowStat::SharesMap::const_iterator flow_it = shares->begin(); flow_it != shares->end(); ++flow_it) {
01370 const CargoDataEntry *via_entry = source_entry->Retrieve(flow_it->second);
01371 for (CargoDataSet::iterator dest_it = via_entry->Begin(); dest_it != via_entry->End(); ++dest_it) {
01372 CargoDataEntry *dest_entry = *dest_it;
01373 ShowCargo(cargo, i, from, flow_it->second, dest_entry->GetStation(), dest_entry->GetCount());
01374 }
01375 }
01376 }
01377 }
01378
01385 void BuildCargoList(CargoID i, const StationCargoList &packets, CargoDataEntry *cargo)
01386 {
01387 const CargoDataEntry *source_dest = this->cached_destinations.Retrieve(i);
01388 for (StationCargoList::ConstIterator it = packets.Packets()->begin(); it != packets.Packets()->end(); it++) {
01389 const CargoPacket *cp = *it;
01390 StationID next = it.GetKey();
01391
01392 const CargoDataEntry *source_entry = source_dest->Retrieve(cp->SourceStation());
01393 if (source_entry == NULL) {
01394 ShowCargo(cargo, i, cp->SourceStation(), next, INVALID_STATION, cp->Count());
01395 continue;
01396 }
01397
01398 const CargoDataEntry *via_entry = source_entry->Retrieve(next);
01399 if (via_entry == NULL) {
01400 ShowCargo(cargo, i, cp->SourceStation(), next, INVALID_STATION, cp->Count());
01401 continue;
01402 }
01403
01404 for (CargoDataSet::iterator dest_it = via_entry->Begin(); dest_it != via_entry->End(); ++dest_it) {
01405 CargoDataEntry *dest_entry = *dest_it;
01406 uint val = DivideApprox(cp->Count() * dest_entry->GetCount(), via_entry->GetCount());
01407 ShowCargo(cargo, i, cp->SourceStation(), next, dest_entry->GetStation(), val);
01408 }
01409 }
01410 }
01411
01417 void BuildCargoList(CargoDataEntry *cargo, const Station *st)
01418 {
01419 for (CargoID i = 0; i < NUM_CARGO; i++) {
01420
01421 if (this->cached_destinations.Retrieve(i) == NULL) {
01422 this->RecalcDestinations(i);
01423 }
01424
01425 if (this->current_mode == MODE_WAITING) {
01426 BuildCargoList(i, st->goods[i].cargo, cargo);
01427 } else {
01428 BuildFlowList(i, st->goods[i].flows, cargo);
01429 }
01430 }
01431 }
01432
01437 void SetDisplayedRow(const CargoDataEntry *data)
01438 {
01439 std::list<StationID> stations;
01440 const CargoDataEntry *parent = data->GetParent();
01441 if (parent->GetParent() == NULL) {
01442 this->displayed_rows.push_back(RowDisplay(&this->expanded_rows, data->GetCargo()));
01443 return;
01444 }
01445
01446 StationID next = data->GetStation();
01447 while (parent->GetParent()->GetParent() != NULL) {
01448 stations.push_back(parent->GetStation());
01449 parent = parent->GetParent();
01450 }
01451
01452 CargoID cargo = parent->GetCargo();
01453 CargoDataEntry *filter = this->expanded_rows.Retrieve(cargo);
01454 while (!stations.empty()) {
01455 filter = filter->Retrieve(stations.back());
01456 stations.pop_back();
01457 }
01458
01459 this->displayed_rows.push_back(RowDisplay(filter, next));
01460 }
01461
01470 StringID GetEntryString(StationID station, StringID here, StringID other_station, StringID any)
01471 {
01472 if (station == this->window_number) {
01473 return here;
01474 } else if (station != INVALID_STATION) {
01475 SetDParam(2, station);
01476 return other_station;
01477 } else {
01478 return any;
01479 }
01480 }
01481
01489 StringID SearchNonStop(CargoDataEntry *cd, StationID station, int column)
01490 {
01491 CargoDataEntry *parent = cd->GetParent();
01492 for (int i = column - 1; i > 0; --i) {
01493 if (this->groupings[i] == GR_DESTINATION) {
01494 if (parent->GetStation() == station) {
01495 return STR_STATION_VIEW_NONSTOP;
01496 } else {
01497 return STR_STATION_VIEW_VIA;
01498 }
01499 }
01500 parent = parent->GetParent();
01501 }
01502
01503 if (this->groupings[column + 1] == GR_DESTINATION) {
01504 CargoDataSet::iterator begin = cd->Begin();
01505 CargoDataSet::iterator end = cd->End();
01506 if (begin != end && ++(cd->Begin()) == end && (*(begin))->GetStation() == station) {
01507 return STR_STATION_VIEW_NONSTOP;
01508 } else {
01509 return STR_STATION_VIEW_VIA;
01510 }
01511 }
01512
01513 return STR_STATION_VIEW_VIA;
01514 }
01515
01526 int DrawEntries(CargoDataEntry *entry, Rect &r, int pos, int maxrows, int column, CargoID cargo = CT_INVALID)
01527 {
01528 if (this->sortings[column] == ST_AS_GROUPING) {
01529 if (this->groupings[column] != GR_CARGO) {
01530 entry->Resort(ST_STATION_STRING, this->sort_orders[column]);
01531 }
01532 } else {
01533 entry->Resort(ST_COUNT, this->sort_orders[column]);
01534 }
01535 for (CargoDataSet::iterator i = entry->Begin(); i != entry->End(); ++i) {
01536 CargoDataEntry *cd = *i;
01537
01538 if (this->groupings[column] == GR_CARGO) cargo = cd->GetCargo();
01539
01540 if (pos > -maxrows && pos <= 0) {
01541 StringID str = STR_EMPTY;
01542 int y = r.top + WD_FRAMERECT_TOP - pos * FONT_HEIGHT_NORMAL;
01543 SetDParam(0, cargo);
01544 SetDParam(1, cd->GetCount());
01545
01546 if (this->groupings[column] == GR_CARGO) {
01547 str = STR_STATION_VIEW_WAITING_CARGO;
01548 DrawCargoIcons(cd->GetCargo(), cd->GetCount(), r.left + WD_FRAMERECT_LEFT + this->expand_shrink_width, r.right - WD_FRAMERECT_RIGHT - this->expand_shrink_width, y);
01549 } else {
01550 StationID station = cd->GetStation();
01551
01552 switch (this->groupings[column]) {
01553 case GR_SOURCE:
01554 str = this->GetEntryString(station, STR_STATION_VIEW_FROM_HERE, STR_STATION_VIEW_FROM, STR_STATION_VIEW_FROM_ANY);
01555 break;
01556 case GR_NEXT:
01557 str = this->GetEntryString(station, STR_STATION_VIEW_VIA_HERE, STR_STATION_VIEW_VIA, STR_STATION_VIEW_VIA_ANY);
01558 if (str == STR_STATION_VIEW_VIA) str = SearchNonStop(cd, station, column);
01559 break;
01560 case GR_DESTINATION:
01561 str = this->GetEntryString(station, STR_STATION_VIEW_TO_HERE, STR_STATION_VIEW_TO, STR_STATION_VIEW_TO_ANY);
01562 break;
01563 default:
01564 NOT_REACHED();
01565 }
01566 if (pos == -this->scroll_to_row && Station::IsValidID(station)) {
01567 ScrollMainWindowToTile(Station::Get(station)->xy);
01568 }
01569 }
01570
01571 bool rtl = _current_text_dir == TD_RTL;
01572 int text_left = rtl ? r.left + this->expand_shrink_width : r.left + WD_FRAMERECT_LEFT + column * this->expand_shrink_width;
01573 int text_right = rtl ? r.right - WD_FRAMERECT_LEFT - column * this->expand_shrink_width : r.right - this->expand_shrink_width;
01574 int shrink_left = rtl ? r.left + WD_FRAMERECT_LEFT : r.right - this->expand_shrink_width + WD_FRAMERECT_LEFT;
01575 int shrink_right = rtl ? r.left + this->expand_shrink_width - WD_FRAMERECT_RIGHT : r.right - WD_FRAMERECT_RIGHT;
01576
01577 DrawString(text_left, text_right, y, str);
01578
01579 if (column < NUM_COLUMNS - 1) {
01580 const char *sym = cd->GetNumChildren() > 0 ? "-" : "+";
01581 DrawString(shrink_left, shrink_right, y, sym, TC_YELLOW);
01582 }
01583 SetDisplayedRow(cd);
01584 }
01585 pos = DrawEntries(cd, r, --pos, maxrows, column + 1, cargo);
01586 }
01587 return pos;
01588 }
01589
01595 int DrawAcceptedCargo(const Rect &r) const
01596 {
01597 const Station *st = Station::Get(this->window_number);
01598
01599 uint32 cargo_mask = 0;
01600 for (CargoID i = 0; i < NUM_CARGO; i++) {
01601 if (HasBit(st->goods[i].acceptance_pickup, GoodsEntry::GES_ACCEPTANCE)) SetBit(cargo_mask, i);
01602 }
01603 SetDParam(0, cargo_mask);
01604 int bottom = DrawStringMultiLine(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + WD_FRAMERECT_TOP, INT32_MAX, STR_STATION_VIEW_ACCEPTS_CARGO);
01605 return CeilDiv(bottom - r.top - WD_FRAMERECT_TOP, FONT_HEIGHT_NORMAL);
01606 }
01607
01613 int DrawCargoRatings(const Rect &r) const
01614 {
01615 const Station *st = Station::Get(this->window_number);
01616 int y = r.top + WD_FRAMERECT_TOP;
01617
01618 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_STATION_VIEW_CARGO_RATINGS_TITLE);
01619 y += FONT_HEIGHT_NORMAL;
01620
01621 const CargoSpec *cs;
01622 FOR_ALL_SORTED_STANDARD_CARGOSPECS(cs) {
01623 const GoodsEntry *ge = &st->goods[cs->Index()];
01624 if (!HasBit(ge->acceptance_pickup, GoodsEntry::GES_PICKUP)) continue;
01625
01626 SetDParam(0, cs->name);
01627 SetDParam(1, ge->supply);
01628 SetDParam(3, ToPercent8(ge->rating));
01629 SetDParam(2, STR_CARGO_RATING_APPALLING + (ge->rating >> 5));
01630 DrawString(r.left + WD_FRAMERECT_LEFT + 6, r.right - WD_FRAMERECT_RIGHT - 6, y, STR_STATION_VIEW_CARGO_SUPPLY_RATING);
01631 y += FONT_HEIGHT_NORMAL;
01632 }
01633 return CeilDiv(y - r.top - WD_FRAMERECT_TOP, FONT_HEIGHT_NORMAL);
01634 }
01635
01641 template<class ID>
01642 void HandleCargoWaitingClick(CargoDataEntry *filter, ID next)
01643 {
01644 if (filter->Retrieve(next) != NULL) {
01645 filter->Remove(next);
01646 } else {
01647 filter->InsertOrRetrieve(next);
01648 }
01649 }
01650
01655 void HandleCargoWaitingClick(int row)
01656 {
01657 if (row < 0 || (uint)row >= this->displayed_rows.size()) return;
01658 if (_ctrl_pressed) {
01659 this->scroll_to_row = row;
01660 } else {
01661 RowDisplay &display = this->displayed_rows[row];
01662 if (display.filter == &this->expanded_rows) {
01663 this->HandleCargoWaitingClick<CargoID>(display.filter, display.next_cargo);
01664 } else {
01665 this->HandleCargoWaitingClick<StationID>(display.filter, display.next_station);
01666 }
01667 }
01668 this->SetWidgetDirty(WID_SV_WAITING);
01669 }
01670
01671 virtual void OnClick(Point pt, int widget, int click_count)
01672 {
01673 switch (widget) {
01674 case WID_SV_WAITING:
01675 this->HandleCargoWaitingClick(this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_SV_WAITING, WD_FRAMERECT_TOP, FONT_HEIGHT_NORMAL) - this->vscroll->GetPosition());
01676 break;
01677
01678 case WID_SV_LOCATION:
01679 if (_ctrl_pressed) {
01680 ShowExtraViewPortWindow(Station::Get(this->window_number)->xy);
01681 } else {
01682 ScrollMainWindowToTile(Station::Get(this->window_number)->xy);
01683 }
01684 break;
01685
01686 case WID_SV_ACCEPTS_RATINGS: {
01687
01688 int height_change;
01689 NWidgetCore *nwi = this->GetWidget<NWidgetCore>(WID_SV_ACCEPTS_RATINGS);
01690 if (this->GetWidget<NWidgetCore>(WID_SV_ACCEPTS_RATINGS)->widget_data == STR_STATION_VIEW_RATINGS_BUTTON) {
01691 nwi->SetDataTip(STR_STATION_VIEW_ACCEPTS_BUTTON, STR_STATION_VIEW_ACCEPTS_TOOLTIP);
01692 height_change = this->rating_lines - this->accepts_lines;
01693 } else {
01694 nwi->SetDataTip(STR_STATION_VIEW_RATINGS_BUTTON, STR_STATION_VIEW_RATINGS_TOOLTIP);
01695 height_change = this->accepts_lines - this->rating_lines;
01696 }
01697 this->ReInit(0, height_change * FONT_HEIGHT_NORMAL);
01698 break;
01699 }
01700
01701 case WID_SV_RENAME:
01702 SetDParam(0, this->window_number);
01703 ShowQueryString(STR_STATION_NAME, STR_STATION_VIEW_RENAME_STATION_CAPTION, MAX_LENGTH_STATION_NAME_CHARS,
01704 this, CS_ALPHANUMERAL, QSF_ENABLE_DEFAULT | QSF_LEN_IN_CHARS);
01705 break;
01706
01707 case WID_SV_CLOSE_AIRPORT:
01708 DoCommandP(0, this->window_number, 0, CMD_OPEN_CLOSE_AIRPORT);
01709 break;
01710
01711 case WID_SV_TRAINS:
01712 case WID_SV_ROADVEHS:
01713 case WID_SV_SHIPS:
01714 case WID_SV_PLANES:
01715 ShowVehicleListWindow(this->owner, (VehicleType)(widget - WID_SV_TRAINS), (StationID)this->window_number);
01716 break;
01717
01718 case WID_SV_SORT_BY: {
01719 ShowDropDownMenu(this, _sort_names, this->current_mode, WID_SV_SORT_BY, 0, 0);
01720 break;
01721 }
01722
01723 case WID_SV_GROUP_BY: {
01724 ShowDropDownMenu(this, _group_names, this->grouping_index, WID_SV_GROUP_BY, 0, 0);
01725 break;
01726 }
01727
01728 case WID_SV_SORT_ORDER: {
01729 this->SelectSortOrder(this->sort_orders[1] == SO_ASCENDING ? SO_DESCENDING : SO_ASCENDING);
01730 this->SetTimeout();
01731 this->LowerWidget(WID_SV_SORT_ORDER);
01732 break;
01733 }
01734 }
01735 }
01736
01741 void SelectSortOrder(SortOrder order)
01742 {
01743 this->sort_orders[1] = this->sort_orders[2] = this->sort_orders[3] = order;
01744 _settings_client.gui.station_gui_sort_order = this->sort_orders[1];
01745 this->SetDirty();
01746 }
01747
01752 void SelectSortBy(int index)
01753 {
01754 _settings_client.gui.station_gui_sort_by = index;
01755 switch (_sort_names[index]) {
01756 case STR_STATION_VIEW_WAITING_STATION:
01757 this->current_mode = MODE_WAITING;
01758 this->sortings[1] = this->sortings[2] = this->sortings[3] = ST_AS_GROUPING;
01759 break;
01760 case STR_STATION_VIEW_WAITING_AMOUNT:
01761 this->current_mode = MODE_WAITING;
01762 this->sortings[1] = this->sortings[2] = this->sortings[3] = ST_COUNT;
01763 break;
01764 case STR_STATION_VIEW_PLANNED_STATION:
01765 this->current_mode = MODE_PLANNED;
01766 this->sortings[1] = this->sortings[2] = this->sortings[3] = ST_AS_GROUPING;
01767 break;
01768 case STR_STATION_VIEW_PLANNED_AMOUNT:
01769 this->current_mode = MODE_PLANNED;
01770 this->sortings[1] = this->sortings[2] = this->sortings[3] = ST_COUNT;
01771 break;
01772 default:
01773 NOT_REACHED();
01774 }
01775
01776 this->GetWidget<NWidgetCore>(WID_SV_SORT_BY)->widget_data = _sort_names[index];
01777 this->SetDirty();
01778 }
01779
01784 void SelectGroupBy(int index)
01785 {
01786 this->grouping_index = index;
01787 _settings_client.gui.station_gui_group_order = index;
01788 this->GetWidget<NWidgetCore>(WID_SV_GROUP_BY)->widget_data = _group_names[index];
01789 switch (_group_names[index]) {
01790 case STR_STATION_VIEW_GROUP_S_V_D:
01791 this->groupings[1] = GR_SOURCE;
01792 this->groupings[2] = GR_NEXT;
01793 this->groupings[3] = GR_DESTINATION;
01794 break;
01795 case STR_STATION_VIEW_GROUP_S_D_V:
01796 this->groupings[1] = GR_SOURCE;
01797 this->groupings[2] = GR_DESTINATION;
01798 this->groupings[3] = GR_NEXT;
01799 break;
01800 case STR_STATION_VIEW_GROUP_V_S_D:
01801 this->groupings[1] = GR_NEXT;
01802 this->groupings[2] = GR_SOURCE;
01803 this->groupings[3] = GR_DESTINATION;
01804 break;
01805 case STR_STATION_VIEW_GROUP_V_D_S:
01806 this->groupings[1] = GR_NEXT;
01807 this->groupings[2] = GR_DESTINATION;
01808 this->groupings[3] = GR_SOURCE;
01809 break;
01810 case STR_STATION_VIEW_GROUP_D_S_V:
01811 this->groupings[1] = GR_DESTINATION;
01812 this->groupings[2] = GR_SOURCE;
01813 this->groupings[3] = GR_NEXT;
01814 break;
01815 case STR_STATION_VIEW_GROUP_D_V_S:
01816 this->groupings[1] = GR_DESTINATION;
01817 this->groupings[2] = GR_NEXT;
01818 this->groupings[3] = GR_SOURCE;
01819 break;
01820 }
01821 this->SetDirty();
01822 }
01823
01824 virtual void OnDropdownSelect(int widget, int index)
01825 {
01826 if (widget == WID_SV_SORT_BY) {
01827 this->SelectSortBy(index);
01828 } else {
01829 this->SelectGroupBy(index);
01830 }
01831 }
01832
01833 virtual void OnQueryTextFinished(char *str)
01834 {
01835 if (str == NULL) return;
01836
01837 DoCommandP(0, this->window_number, 0, CMD_RENAME_STATION | CMD_MSG(STR_ERROR_CAN_T_RENAME_STATION), NULL, str);
01838 }
01839
01840 virtual void OnResize()
01841 {
01842 this->vscroll->SetCapacityFromWidget(this, WID_SV_WAITING, WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM);
01843 }
01844
01850 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
01851 {
01852 if (gui_scope) {
01853 if (data) {
01854 this->cached_destinations.Remove((CargoID)data);
01855 } else {
01856 this->ReInit();
01857 }
01858 }
01859 }
01860 };
01861
01862 const StringID StationViewWindow::_sort_names[] = {
01863 STR_STATION_VIEW_WAITING_STATION,
01864 STR_STATION_VIEW_WAITING_AMOUNT,
01865 STR_STATION_VIEW_PLANNED_STATION,
01866 STR_STATION_VIEW_PLANNED_AMOUNT,
01867 INVALID_STRING_ID
01868 };
01869
01870 const StringID StationViewWindow::_group_names[] = {
01871 STR_STATION_VIEW_GROUP_S_V_D,
01872 STR_STATION_VIEW_GROUP_S_D_V,
01873 STR_STATION_VIEW_GROUP_V_S_D,
01874 STR_STATION_VIEW_GROUP_V_D_S,
01875 STR_STATION_VIEW_GROUP_D_S_V,
01876 STR_STATION_VIEW_GROUP_D_V_S,
01877 INVALID_STRING_ID
01878 };
01879
01880 static const WindowDesc _station_view_desc(
01881 WDP_AUTO, 249, 117,
01882 WC_STATION_VIEW, WC_NONE,
01883 WDF_UNCLICK_BUTTONS,
01884 _nested_station_view_widgets, lengthof(_nested_station_view_widgets)
01885 );
01886
01892 void ShowStationViewWindow(StationID station)
01893 {
01894 AllocateWindowDescFront<StationViewWindow>(&_station_view_desc, station);
01895 }
01896
01898 struct TileAndStation {
01899 TileIndex tile;
01900 StationID station;
01901 };
01902
01903 static SmallVector<TileAndStation, 8> _deleted_stations_nearby;
01904 static SmallVector<StationID, 8> _stations_nearby_list;
01905
01913 template <class T>
01914 static bool AddNearbyStation(TileIndex tile, void *user_data)
01915 {
01916 TileArea *ctx = (TileArea *)user_data;
01917
01918
01919 for (uint i = 0; i < _deleted_stations_nearby.Length(); i++) {
01920 TileAndStation *ts = _deleted_stations_nearby.Get(i);
01921 if (ts->tile == tile) {
01922 *_stations_nearby_list.Append() = _deleted_stations_nearby[i].station;
01923 _deleted_stations_nearby.Erase(ts);
01924 i--;
01925 }
01926 }
01927
01928
01929 if (!IsTileType(tile, MP_STATION)) return false;
01930
01931 StationID sid = GetStationIndex(tile);
01932
01933
01934 if (!T::IsValidID(sid)) return false;
01935
01936 T *st = T::Get(sid);
01937 if (st->owner != _local_company || _stations_nearby_list.Contains(sid)) return false;
01938
01939 if (st->rect.BeforeAddRect(ctx->tile, ctx->w, ctx->h, StationRect::ADD_TEST).Succeeded()) {
01940 *_stations_nearby_list.Append() = sid;
01941 }
01942
01943 return false;
01944 }
01945
01955 template <class T>
01956 static const T *FindStationsNearby(TileArea ta, bool distant_join)
01957 {
01958 TileArea ctx = ta;
01959
01960 _stations_nearby_list.Clear();
01961 _deleted_stations_nearby.Clear();
01962
01963
01964 TILE_AREA_LOOP(t, ta) {
01965 if (t < MapSize() && IsTileType(t, MP_STATION) && T::IsValidID(GetStationIndex(t))) return T::GetByTile(t);
01966 }
01967
01968
01969 const BaseStation *st;
01970 FOR_ALL_BASE_STATIONS(st) {
01971 if (T::IsExpected(st) && !st->IsInUse() && st->owner == _local_company) {
01972
01973 if (max(DistanceMax(ta.tile, st->xy), DistanceMax(TILE_ADDXY(ta.tile, ta.w - 1, ta.h - 1), st->xy)) < _settings_game.station.station_spread) {
01974 TileAndStation *ts = _deleted_stations_nearby.Append();
01975 ts->tile = st->xy;
01976 ts->station = st->index;
01977
01978
01979 if (IsInsideBS(TileX(st->xy), TileX(ctx.tile), ctx.w) &&
01980 IsInsideBS(TileY(st->xy), TileY(ctx.tile), ctx.h)) {
01981 AddNearbyStation<T>(st->xy, &ctx);
01982 }
01983 }
01984 }
01985 }
01986
01987
01988
01989
01990 if (distant_join && min(ta.w, ta.h) >= _settings_game.station.station_spread) return NULL;
01991 uint max_dist = distant_join ? _settings_game.station.station_spread - min(ta.w, ta.h) : 1;
01992
01993 TileIndex tile = TILE_ADD(ctx.tile, TileOffsByDir(DIR_N));
01994 CircularTileSearch(&tile, max_dist, ta.w, ta.h, AddNearbyStation<T>, &ctx);
01995
01996 return NULL;
01997 }
01998
01999 static const NWidgetPart _nested_select_station_widgets[] = {
02000 NWidget(NWID_HORIZONTAL),
02001 NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
02002 NWidget(WWT_CAPTION, COLOUR_DARK_GREEN, WID_JS_CAPTION), SetDataTip(STR_JOIN_STATION_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
02003 EndContainer(),
02004 NWidget(NWID_HORIZONTAL),
02005 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_JS_PANEL), SetResize(1, 0), SetScrollbar(WID_JS_SCROLLBAR), EndContainer(),
02006 NWidget(NWID_VERTICAL),
02007 NWidget(NWID_VSCROLLBAR, COLOUR_DARK_GREEN, WID_JS_SCROLLBAR),
02008 NWidget(WWT_RESIZEBOX, COLOUR_DARK_GREEN),
02009 EndContainer(),
02010 EndContainer(),
02011 };
02012
02017 template <class T>
02018 struct SelectStationWindow : Window {
02019 CommandContainer select_station_cmd;
02020 TileArea area;
02021 Scrollbar *vscroll;
02022
02023 SelectStationWindow(const WindowDesc *desc, CommandContainer cmd, TileArea ta) :
02024 Window(),
02025 select_station_cmd(cmd),
02026 area(ta)
02027 {
02028 this->CreateNestedTree(desc);
02029 this->vscroll = this->GetScrollbar(WID_JS_SCROLLBAR);
02030 this->GetWidget<NWidgetCore>(WID_JS_CAPTION)->widget_data = T::EXPECTED_FACIL == FACIL_WAYPOINT ? STR_JOIN_WAYPOINT_CAPTION : STR_JOIN_STATION_CAPTION;
02031 this->FinishInitNested(desc, 0);
02032 this->OnInvalidateData(0);
02033 }
02034
02035 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
02036 {
02037 if (widget != WID_JS_PANEL) return;
02038
02039
02040 Dimension d = GetStringBoundingBox(T::EXPECTED_FACIL == FACIL_WAYPOINT ? STR_JOIN_WAYPOINT_CREATE_SPLITTED_WAYPOINT : STR_JOIN_STATION_CREATE_SPLITTED_STATION);
02041 for (uint i = 0; i < _stations_nearby_list.Length(); i++) {
02042 const T *st = T::Get(_stations_nearby_list[i]);
02043 SetDParam(0, st->index);
02044 SetDParam(1, st->facilities);
02045 d = maxdim(d, GetStringBoundingBox(T::EXPECTED_FACIL == FACIL_WAYPOINT ? STR_STATION_LIST_WAYPOINT : STR_STATION_LIST_STATION));
02046 }
02047
02048 resize->height = d.height;
02049 d.height *= 5;
02050 d.width += WD_FRAMERECT_RIGHT + WD_FRAMERECT_LEFT;
02051 d.height += WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
02052 *size = d;
02053 }
02054
02055 virtual void DrawWidget(const Rect &r, int widget) const
02056 {
02057 if (widget != WID_JS_PANEL) return;
02058
02059 uint y = r.top + WD_FRAMERECT_TOP;
02060 if (this->vscroll->GetPosition() == 0) {
02061 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, T::EXPECTED_FACIL == FACIL_WAYPOINT ? STR_JOIN_WAYPOINT_CREATE_SPLITTED_WAYPOINT : STR_JOIN_STATION_CREATE_SPLITTED_STATION);
02062 y += this->resize.step_height;
02063 }
02064
02065 for (uint i = max<uint>(1, this->vscroll->GetPosition()); i <= _stations_nearby_list.Length(); ++i, y += this->resize.step_height) {
02066
02067 if (i - this->vscroll->GetPosition() >= this->vscroll->GetCapacity()) break;
02068
02069 const T *st = T::Get(_stations_nearby_list[i - 1]);
02070 SetDParam(0, st->index);
02071 SetDParam(1, st->facilities);
02072 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, T::EXPECTED_FACIL == FACIL_WAYPOINT ? STR_STATION_LIST_WAYPOINT : STR_STATION_LIST_STATION);
02073 }
02074 }
02075
02076 virtual void OnClick(Point pt, int widget, int click_count)
02077 {
02078 if (widget != WID_JS_PANEL) return;
02079
02080 uint st_index = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_JS_PANEL, WD_FRAMERECT_TOP);
02081 bool distant_join = (st_index > 0);
02082 if (distant_join) st_index--;
02083
02084 if (distant_join && st_index >= _stations_nearby_list.Length()) return;
02085
02086
02087 SB(this->select_station_cmd.p2, 16, 16,
02088 (distant_join ? _stations_nearby_list[st_index] : NEW_STATION));
02089
02090
02091 DoCommandP(&this->select_station_cmd);
02092
02093
02094 DeleteWindowById(WC_SELECT_STATION, 0);
02095 }
02096
02097 virtual void OnTick()
02098 {
02099 if (_thd.dirty & 2) {
02100 _thd.dirty &= ~2;
02101 this->SetDirty();
02102 }
02103 }
02104
02105 virtual void OnResize()
02106 {
02107 this->vscroll->SetCapacityFromWidget(this, WID_JS_PANEL, WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM);
02108 }
02109
02115 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
02116 {
02117 if (!gui_scope) return;
02118 FindStationsNearby<T>(this->area, true);
02119 this->vscroll->SetCount(_stations_nearby_list.Length() + 1);
02120 this->SetDirty();
02121 }
02122 };
02123
02124 static const WindowDesc _select_station_desc(
02125 WDP_AUTO, 200, 180,
02126 WC_SELECT_STATION, WC_NONE,
02127 WDF_CONSTRUCTION,
02128 _nested_select_station_widgets, lengthof(_nested_select_station_widgets)
02129 );
02130
02131
02139 template <class T>
02140 static bool StationJoinerNeeded(CommandContainer cmd, TileArea ta)
02141 {
02142
02143 if (!_settings_game.station.distant_join_stations) return false;
02144
02145
02146
02147 Window *selection_window = FindWindowById(WC_SELECT_STATION, 0);
02148 if (selection_window != NULL) {
02149
02150 delete selection_window;
02151 UpdateTileSelection();
02152 }
02153
02154
02155 if (!_ctrl_pressed) return false;
02156
02157
02158 if (DoCommand(&cmd, CommandFlagsToDCFlags(GetCommandFlags(cmd.cmd))).Failed()) return false;
02159
02160
02161
02162
02163 const T *st = FindStationsNearby<T>(ta, false);
02164 return st == NULL && (_settings_game.station.adjacent_stations || _stations_nearby_list.Length() == 0);
02165 }
02166
02173 template <class T>
02174 void ShowSelectBaseStationIfNeeded(CommandContainer cmd, TileArea ta)
02175 {
02176 if (StationJoinerNeeded<T>(cmd, ta)) {
02177 if (!_settings_client.gui.persistent_buildingtools) ResetObjectToPlace();
02178 new SelectStationWindow<T>(&_select_station_desc, cmd, ta);
02179 } else {
02180 DoCommandP(&cmd);
02181 }
02182 }
02183
02189 void ShowSelectStationIfNeeded(CommandContainer cmd, TileArea ta)
02190 {
02191 ShowSelectBaseStationIfNeeded<Station>(cmd, ta);
02192 }
02193
02199 void ShowSelectWaypointIfNeeded(CommandContainer cmd, TileArea ta)
02200 {
02201 ShowSelectBaseStationIfNeeded<Waypoint>(cmd, ta);
02202 }