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 #include "town.h"
00033
00034 #include "widgets/station_widget.h"
00035
00036 #include "table/strings.h"
00037
00038 #include <set>
00039 #include <vector>
00040
00051 int DrawStationCoverageAreaText(int left, int right, int top, StationCoverageType sct, int rad, bool supplies)
00052 {
00053 TileIndex tile = TileVirtXY(_thd.pos.x, _thd.pos.y);
00054 uint32 cargo_mask = 0;
00055 if (_thd.drawstyle == HT_RECT && tile < MapSize()) {
00056 CargoArray cargoes;
00057 if (supplies) {
00058 cargoes = GetProductionAroundTiles(tile, _thd.size.x / TILE_SIZE, _thd.size.y / TILE_SIZE, rad);
00059 } else {
00060 cargoes = GetAcceptanceAroundTiles(tile, _thd.size.x / TILE_SIZE, _thd.size.y / TILE_SIZE, rad);
00061 }
00062
00063
00064 for (CargoID i = 0; i < NUM_CARGO; i++) {
00065 switch (sct) {
00066 case SCT_PASSENGERS_ONLY: if (!IsCargoInClass(i, CC_PASSENGERS)) continue; break;
00067 case SCT_NON_PASSENGERS_ONLY: if (IsCargoInClass(i, CC_PASSENGERS)) continue; break;
00068 case SCT_ALL: break;
00069 default: NOT_REACHED();
00070 }
00071 if (cargoes[i] >= (supplies ? 1U : 8U)) SetBit(cargo_mask, i);
00072 }
00073 }
00074 SetDParam(0, cargo_mask);
00075 return DrawStringMultiLine(left, right, top, INT32_MAX, supplies ? STR_STATION_BUILD_SUPPLIES_CARGO : STR_STATION_BUILD_ACCEPTS_CARGO);
00076 }
00077
00083 void CheckRedrawStationCoverage(const Window *w)
00084 {
00085 if (_thd.dirty & 1) {
00086 _thd.dirty &= ~1;
00087 w->SetDirty();
00088 }
00089 }
00090
00106 static void StationsWndShowStationRating(int left, int right, int y, CargoID type, uint amount, byte rating)
00107 {
00108 static const uint units_full = 576;
00109 static const uint rating_full = 224;
00110
00111 const CargoSpec *cs = CargoSpec::Get(type);
00112 if (!cs->IsValid()) return;
00113
00114 int colour = cs->rating_colour;
00115 TextColour tc = GetContrastColour(colour);
00116 uint w = (minu(amount, units_full) + 5) / 36;
00117
00118 int height = GetCharacterHeight(FS_SMALL);
00119
00120
00121 if (w != 0) GfxFillRect(left, y, left + w - 1, y + height, colour);
00122
00123
00124
00125 if (w == 0) {
00126 uint rest = amount / 5;
00127 if (rest != 0) {
00128 w += left;
00129 GfxFillRect(w, y + height - rest, w, y + height, colour);
00130 }
00131 }
00132
00133 DrawString(left + 1, right, y, cs->abbrev, tc);
00134
00135
00136 y += height + 2;
00137 GfxFillRect(left + 1, y, left + 14, y, PC_RED);
00138 rating = minu(rating, rating_full) / 16;
00139 if (rating != 0) GfxFillRect(left + 1, y, left + rating, y, PC_GREEN);
00140 }
00141
00142 typedef GUIList<const Station*> GUIStationList;
00143
00147 class CompanyStationsWindow : public Window
00148 {
00149 protected:
00150
00151 static Listing last_sorting;
00152 static byte facilities;
00153 static bool include_empty;
00154 static const uint32 cargo_filter_max;
00155 static uint32 cargo_filter;
00156 static const Station *last_station;
00157
00158
00159 static const StringID sorter_names[];
00160 static GUIStationList::SortFunction * const sorter_funcs[];
00161
00162 GUIStationList stations;
00163 Scrollbar *vscroll;
00164
00170 void BuildStationsList(const Owner owner)
00171 {
00172 if (!this->stations.NeedRebuild()) return;
00173
00174 DEBUG(misc, 3, "Building station list for company %d", owner);
00175
00176 this->stations.Clear();
00177
00178 const Station *st;
00179 FOR_ALL_STATIONS(st) {
00180 if (st->owner == owner || (st->owner == OWNER_NONE && HasStationInUse(st->index, true, owner))) {
00181 if (this->facilities & st->facilities) {
00182 int num_waiting_cargo = 0;
00183 for (CargoID j = 0; j < NUM_CARGO; j++) {
00184 if (HasBit(st->goods[j].acceptance_pickup, GoodsEntry::GES_PICKUP)) {
00185 num_waiting_cargo++;
00186 if (HasBit(this->cargo_filter, j)) {
00187 *this->stations.Append() = st;
00188 break;
00189 }
00190 }
00191 }
00192
00193 if (num_waiting_cargo == 0 && this->include_empty) {
00194 *this->stations.Append() = st;
00195 }
00196 }
00197 }
00198 }
00199
00200 this->stations.Compact();
00201 this->stations.RebuildDone();
00202
00203 this->vscroll->SetCount(this->stations.Length());
00204 }
00205
00207 static int CDECL StationNameSorter(const Station * const *a, const Station * const *b)
00208 {
00209 static char buf_cache[64];
00210 char buf[64];
00211
00212 SetDParam(0, (*a)->index);
00213 GetString(buf, STR_STATION_NAME, lastof(buf));
00214
00215 if (*b != last_station) {
00216 last_station = *b;
00217 SetDParam(0, (*b)->index);
00218 GetString(buf_cache, STR_STATION_NAME, lastof(buf_cache));
00219 }
00220
00221 return strcmp(buf, buf_cache);
00222 }
00223
00225 static int CDECL StationTypeSorter(const Station * const *a, const Station * const *b)
00226 {
00227 return (*a)->facilities - (*b)->facilities;
00228 }
00229
00231 static int CDECL StationWaitingSorter(const Station * const *a, const Station * const *b)
00232 {
00233 Money diff = 0;
00234
00235 CargoID j;
00236 FOR_EACH_SET_CARGO_ID(j, cargo_filter) {
00237 if (!(*a)->goods[j].cargo.Empty()) diff += GetTransportedGoodsIncome((*a)->goods[j].cargo.Count(), 20, 50, j);
00238 if (!(*b)->goods[j].cargo.Empty()) diff -= GetTransportedGoodsIncome((*b)->goods[j].cargo.Count(), 20, 50, j);
00239 }
00240
00241 return ClampToI32(diff);
00242 }
00243
00245 static int CDECL StationRatingMaxSorter(const Station * const *a, const Station * const *b)
00246 {
00247 byte maxr1 = 0;
00248 byte maxr2 = 0;
00249
00250 CargoID j;
00251 FOR_EACH_SET_CARGO_ID(j, cargo_filter) {
00252 if (HasBit((*a)->goods[j].acceptance_pickup, GoodsEntry::GES_PICKUP)) maxr1 = max(maxr1, (*a)->goods[j].rating);
00253 if (HasBit((*b)->goods[j].acceptance_pickup, GoodsEntry::GES_PICKUP)) maxr2 = max(maxr2, (*b)->goods[j].rating);
00254 }
00255
00256 return maxr1 - maxr2;
00257 }
00258
00260 static int CDECL StationRatingMinSorter(const Station * const *a, const Station * const *b)
00261 {
00262 byte minr1 = 255;
00263 byte minr2 = 255;
00264
00265 for (CargoID j = 0; j < NUM_CARGO; j++) {
00266 if (!HasBit(cargo_filter, j)) continue;
00267 if (HasBit((*a)->goods[j].acceptance_pickup, GoodsEntry::GES_PICKUP)) minr1 = min(minr1, (*a)->goods[j].rating);
00268 if (HasBit((*b)->goods[j].acceptance_pickup, GoodsEntry::GES_PICKUP)) minr2 = min(minr2, (*b)->goods[j].rating);
00269 }
00270
00271 return -(minr1 - minr2);
00272 }
00273
00275 void SortStationsList()
00276 {
00277 if (!this->stations.Sort()) return;
00278
00279
00280 this->last_station = NULL;
00281
00282
00283 this->SetWidgetDirty(WID_STL_LIST);
00284 }
00285
00286 public:
00287 CompanyStationsWindow(const WindowDesc *desc, WindowNumber window_number) : Window()
00288 {
00289 this->stations.SetListing(this->last_sorting);
00290 this->stations.SetSortFuncs(this->sorter_funcs);
00291 this->stations.ForceRebuild();
00292 this->stations.NeedResort();
00293 this->SortStationsList();
00294
00295 this->CreateNestedTree(desc);
00296 this->vscroll = this->GetScrollbar(WID_STL_SCROLLBAR);
00297 this->FinishInitNested(desc, window_number);
00298 this->owner = (Owner)this->window_number;
00299
00300 const CargoSpec *cs;
00301 FOR_ALL_SORTED_STANDARD_CARGOSPECS(cs) {
00302 if (!HasBit(this->cargo_filter, cs->Index())) continue;
00303 this->LowerWidget(WID_STL_CARGOSTART + index);
00304 }
00305
00306 if (this->cargo_filter == this->cargo_filter_max) this->cargo_filter = _cargo_mask;
00307
00308 for (uint i = 0; i < 5; i++) {
00309 if (HasBit(this->facilities, i)) this->LowerWidget(i + WID_STL_TRAIN);
00310 }
00311 this->SetWidgetLoweredState(WID_STL_NOCARGOWAITING, this->include_empty);
00312
00313 this->GetWidget<NWidgetCore>(WID_STL_SORTDROPBTN)->widget_data = this->sorter_names[this->stations.SortType()];
00314 }
00315
00316 ~CompanyStationsWindow()
00317 {
00318 this->last_sorting = this->stations.GetListing();
00319 }
00320
00321 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00322 {
00323 switch (widget) {
00324 case WID_STL_SORTBY: {
00325 Dimension d = GetStringBoundingBox(this->GetWidget<NWidgetCore>(widget)->widget_data);
00326 d.width += padding.width + WD_SORTBUTTON_ARROW_WIDTH * 2;
00327 d.height += padding.height;
00328 *size = maxdim(*size, d);
00329 break;
00330 }
00331
00332 case WID_STL_SORTDROPBTN: {
00333 Dimension d = {0, 0};
00334 for (int i = 0; this->sorter_names[i] != INVALID_STRING_ID; i++) {
00335 d = maxdim(d, GetStringBoundingBox(this->sorter_names[i]));
00336 }
00337 d.width += padding.width;
00338 d.height += padding.height;
00339 *size = maxdim(*size, d);
00340 break;
00341 }
00342
00343 case WID_STL_LIST:
00344 resize->height = FONT_HEIGHT_NORMAL;
00345 size->height = WD_FRAMERECT_TOP + 5 * resize->height + WD_FRAMERECT_BOTTOM;
00346 break;
00347
00348 case WID_STL_TRAIN:
00349 case WID_STL_TRUCK:
00350 case WID_STL_BUS:
00351 case WID_STL_AIRPLANE:
00352 case WID_STL_SHIP:
00353 size->height = max<uint>(FONT_HEIGHT_SMALL, 10) + padding.height;
00354 break;
00355
00356 case WID_STL_CARGOALL:
00357 case WID_STL_FACILALL:
00358 case WID_STL_NOCARGOWAITING: {
00359 Dimension d = GetStringBoundingBox(widget == WID_STL_NOCARGOWAITING ? STR_ABBREV_NONE : STR_ABBREV_ALL);
00360 d.width += padding.width + 2;
00361 d.height += padding.height;
00362 *size = maxdim(*size, d);
00363 break;
00364 }
00365
00366 default:
00367 if (widget >= WID_STL_CARGOSTART) {
00368 Dimension d = GetStringBoundingBox(_sorted_cargo_specs[widget - WID_STL_CARGOSTART]->abbrev);
00369 d.width += padding.width + 2;
00370 d.height += padding.height;
00371 *size = maxdim(*size, d);
00372 }
00373 break;
00374 }
00375 }
00376
00377 virtual void OnPaint()
00378 {
00379 this->BuildStationsList((Owner)this->window_number);
00380 this->SortStationsList();
00381
00382 this->DrawWidgets();
00383 }
00384
00385 virtual void DrawWidget(const Rect &r, int widget) const
00386 {
00387 switch (widget) {
00388 case WID_STL_SORTBY:
00389
00390 this->DrawSortButtonState(WID_STL_SORTBY, this->stations.IsDescSortOrder() ? SBS_DOWN : SBS_UP);
00391 break;
00392
00393 case WID_STL_LIST: {
00394 bool rtl = _current_text_dir == TD_RTL;
00395 int max = min(this->vscroll->GetPosition() + this->vscroll->GetCapacity(), this->stations.Length());
00396 int y = r.top + WD_FRAMERECT_TOP;
00397 for (int i = this->vscroll->GetPosition(); i < max; ++i) {
00398 const Station *st = this->stations[i];
00399 assert(st->xy != INVALID_TILE);
00400
00401
00402
00403 assert(st->owner == owner || st->owner == OWNER_NONE);
00404
00405 SetDParam(0, st->index);
00406 SetDParam(1, st->facilities);
00407 int x = DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_STATION_LIST_STATION);
00408 x += rtl ? -5 : 5;
00409
00410
00411 for (uint j = 0; j < _sorted_standard_cargo_specs_size; j++) {
00412 CargoID cid = _sorted_cargo_specs[j]->Index();
00413 if (!st->goods[cid].cargo.Empty()) {
00414
00415
00416
00417
00418 if (rtl) {
00419 x -= 20;
00420 if (x < r.left + WD_FRAMERECT_LEFT) break;
00421 }
00422 StationsWndShowStationRating(x, x + 16, y, cid, st->goods[cid].cargo.Count(), st->goods[cid].rating);
00423 if (!rtl) {
00424 x += 20;
00425 if (x > r.right - WD_FRAMERECT_RIGHT) break;
00426 }
00427 }
00428 }
00429 y += FONT_HEIGHT_NORMAL;
00430 }
00431
00432 if (this->vscroll->GetCount() == 0) {
00433 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_STATION_LIST_NONE);
00434 return;
00435 }
00436 break;
00437 }
00438
00439 case WID_STL_NOCARGOWAITING: {
00440 int cg_ofst = this->IsWidgetLowered(widget) ? 2 : 1;
00441 DrawString(r.left + cg_ofst, r.right + cg_ofst, r.top + cg_ofst, STR_ABBREV_NONE, TC_BLACK, SA_HOR_CENTER);
00442 break;
00443 }
00444
00445 case WID_STL_CARGOALL: {
00446 int cg_ofst = this->IsWidgetLowered(widget) ? 2 : 1;
00447 DrawString(r.left + cg_ofst, r.right + cg_ofst, r.top + cg_ofst, STR_ABBREV_ALL, TC_BLACK, SA_HOR_CENTER);
00448 break;
00449 }
00450
00451 case WID_STL_FACILALL: {
00452 int cg_ofst = this->IsWidgetLowered(widget) ? 2 : 1;
00453 DrawString(r.left + cg_ofst, r.right + cg_ofst, r.top + cg_ofst, STR_ABBREV_ALL, TC_BLACK);
00454 break;
00455 }
00456
00457 default:
00458 if (widget >= WID_STL_CARGOSTART) {
00459 const CargoSpec *cs = _sorted_cargo_specs[widget - WID_STL_CARGOSTART];
00460 int cg_ofst = HasBit(this->cargo_filter, cs->Index()) ? 2 : 1;
00461 GfxFillRect(r.left + cg_ofst, r.top + cg_ofst, r.right - 2 + cg_ofst, r.bottom - 2 + cg_ofst, cs->rating_colour);
00462 TextColour tc = GetContrastColour(cs->rating_colour);
00463 DrawString(r.left + cg_ofst, r.right + cg_ofst, r.top + cg_ofst, cs->abbrev, tc, SA_HOR_CENTER);
00464 }
00465 break;
00466 }
00467 }
00468
00469 virtual void SetStringParameters(int widget) const
00470 {
00471 if (widget == WID_STL_CAPTION) {
00472 SetDParam(0, this->window_number);
00473 SetDParam(1, this->vscroll->GetCount());
00474 }
00475 }
00476
00477 virtual void OnClick(Point pt, int widget, int click_count)
00478 {
00479 switch (widget) {
00480 case WID_STL_LIST: {
00481 uint id_v = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_STL_LIST, 0, FONT_HEIGHT_NORMAL);
00482 if (id_v >= this->stations.Length()) return;
00483
00484 const Station *st = this->stations[id_v];
00485
00486 assert(st->owner == (Owner)this->window_number || st->owner == OWNER_NONE);
00487
00488 if (_ctrl_pressed) {
00489 ShowExtraViewPortWindow(st->xy);
00490 } else {
00491 ScrollMainWindowToTile(st->xy);
00492 }
00493 break;
00494 }
00495
00496 case WID_STL_TRAIN:
00497 case WID_STL_TRUCK:
00498 case WID_STL_BUS:
00499 case WID_STL_AIRPLANE:
00500 case WID_STL_SHIP:
00501 if (_ctrl_pressed) {
00502 ToggleBit(this->facilities, widget - WID_STL_TRAIN);
00503 this->ToggleWidgetLoweredState(widget);
00504 } else {
00505 uint i;
00506 FOR_EACH_SET_BIT(i, this->facilities) {
00507 this->RaiseWidget(i + WID_STL_TRAIN);
00508 }
00509 this->facilities = 1 << (widget - WID_STL_TRAIN);
00510 this->LowerWidget(widget);
00511 }
00512 this->stations.ForceRebuild();
00513 this->SetDirty();
00514 break;
00515
00516 case WID_STL_FACILALL:
00517 for (uint i = WID_STL_TRAIN; i <= WID_STL_SHIP; i++) {
00518 this->LowerWidget(i);
00519 }
00520
00521 this->facilities = FACIL_TRAIN | FACIL_TRUCK_STOP | FACIL_BUS_STOP | FACIL_AIRPORT | FACIL_DOCK;
00522 this->stations.ForceRebuild();
00523 this->SetDirty();
00524 break;
00525
00526 case WID_STL_CARGOALL: {
00527 for (uint i = 0; i < _sorted_standard_cargo_specs_size; i++) {
00528 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->SetDirty();
00542 break;
00543
00544 case WID_STL_SORTDROPBTN:
00545 ShowDropDownMenu(this, this->sorter_names, this->stations.SortType(), WID_STL_SORTDROPBTN, 0, 0);
00546 break;
00547
00548 case WID_STL_NOCARGOWAITING:
00549 if (_ctrl_pressed) {
00550 this->include_empty = !this->include_empty;
00551 this->ToggleWidgetLoweredState(WID_STL_NOCARGOWAITING);
00552 } else {
00553 for (uint i = 0; i < _sorted_standard_cargo_specs_size; i++) {
00554 this->RaiseWidget(WID_STL_CARGOSTART + i);
00555 }
00556
00557 this->cargo_filter = 0;
00558 this->include_empty = true;
00559
00560 this->LowerWidget(WID_STL_NOCARGOWAITING);
00561 }
00562 this->stations.ForceRebuild();
00563 this->SetDirty();
00564 break;
00565
00566 default:
00567 if (widget >= WID_STL_CARGOSTART) {
00568
00569 const CargoSpec *cs = _sorted_cargo_specs[widget - WID_STL_CARGOSTART];
00570
00571 if (_ctrl_pressed) {
00572 ToggleBit(this->cargo_filter, cs->Index());
00573 this->ToggleWidgetLoweredState(widget);
00574 } else {
00575 for (uint i = 0; i < _sorted_standard_cargo_specs_size; i++) {
00576 this->RaiseWidget(WID_STL_CARGOSTART + i);
00577 }
00578 this->RaiseWidget(WID_STL_NOCARGOWAITING);
00579
00580 this->cargo_filter = 0;
00581 this->include_empty = false;
00582
00583 SetBit(this->cargo_filter, cs->Index());
00584 this->LowerWidget(widget);
00585 }
00586 this->stations.ForceRebuild();
00587 this->SetDirty();
00588 }
00589 break;
00590 }
00591 }
00592
00593 virtual void OnDropdownSelect(int widget, int index)
00594 {
00595 if (this->stations.SortType() != index) {
00596 this->stations.SetSortType(index);
00597
00598
00599 this->GetWidget<NWidgetCore>(WID_STL_SORTDROPBTN)->widget_data = this->sorter_names[this->stations.SortType()];
00600
00601 this->SetDirty();
00602 }
00603 }
00604
00605 virtual void OnTick()
00606 {
00607 if (_pause_mode != PM_UNPAUSED) return;
00608 if (this->stations.NeedResort()) {
00609 DEBUG(misc, 3, "Periodic rebuild station list company %d", this->window_number);
00610 this->SetDirty();
00611 }
00612 }
00613
00614 virtual void OnResize()
00615 {
00616 this->vscroll->SetCapacityFromWidget(this, WID_STL_LIST, WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM);
00617 }
00618
00624 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
00625 {
00626 if (data == 0) {
00627
00628 this->stations.ForceRebuild();
00629 } else {
00630 this->stations.ForceResort();
00631 }
00632 }
00633 };
00634
00635 Listing CompanyStationsWindow::last_sorting = {false, 0};
00636 byte CompanyStationsWindow::facilities = FACIL_TRAIN | FACIL_TRUCK_STOP | FACIL_BUS_STOP | FACIL_AIRPORT | FACIL_DOCK;
00637 bool CompanyStationsWindow::include_empty = true;
00638 const uint32 CompanyStationsWindow::cargo_filter_max = UINT32_MAX;
00639 uint32 CompanyStationsWindow::cargo_filter = UINT32_MAX;
00640 const Station *CompanyStationsWindow::last_station = NULL;
00641
00642
00643 GUIStationList::SortFunction * const CompanyStationsWindow::sorter_funcs[] = {
00644 &StationNameSorter,
00645 &StationTypeSorter,
00646 &StationWaitingSorter,
00647 &StationRatingMaxSorter,
00648 &StationRatingMinSorter
00649 };
00650
00651
00652 const StringID CompanyStationsWindow::sorter_names[] = {
00653 STR_SORT_BY_NAME,
00654 STR_SORT_BY_FACILITY,
00655 STR_SORT_BY_WAITING,
00656 STR_SORT_BY_RATING_MAX,
00657 STR_SORT_BY_RATING_MIN,
00658 INVALID_STRING_ID
00659 };
00660
00666 static NWidgetBase *CargoWidgets(int *biggest_index)
00667 {
00668 NWidgetHorizontal *container = new NWidgetHorizontal();
00669
00670 for (uint i = 0; i < _sorted_standard_cargo_specs_size; i++) {
00671 NWidgetBackground *panel = new NWidgetBackground(WWT_PANEL, COLOUR_GREY, WID_STL_CARGOSTART + i);
00672 panel->SetMinimalSize(14, 11);
00673 panel->SetResize(0, 0);
00674 panel->SetFill(0, 1);
00675 panel->SetDataTip(0, STR_STATION_LIST_USE_CTRL_TO_SELECT_MORE);
00676 container->Add(panel);
00677 }
00678 *biggest_index = WID_STL_CARGOSTART + _sorted_standard_cargo_specs_size;
00679 return container;
00680 }
00681
00682 static const NWidgetPart _nested_company_stations_widgets[] = {
00683 NWidget(NWID_HORIZONTAL),
00684 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
00685 NWidget(WWT_CAPTION, COLOUR_GREY, WID_STL_CAPTION), SetDataTip(STR_STATION_LIST_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00686 NWidget(WWT_SHADEBOX, COLOUR_GREY),
00687 NWidget(WWT_STICKYBOX, COLOUR_GREY),
00688 EndContainer(),
00689 NWidget(NWID_HORIZONTAL),
00690 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),
00691 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),
00692 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),
00693 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),
00694 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),
00695 NWidget(WWT_PUSHBTN, COLOUR_GREY, WID_STL_FACILALL), SetMinimalSize(14, 11), SetDataTip(0x0, STR_STATION_LIST_SELECT_ALL_FACILITIES), SetFill(0, 1),
00696 NWidget(WWT_PANEL, COLOUR_GREY), SetMinimalSize(5, 11), SetFill(0, 1), EndContainer(),
00697 NWidgetFunction(CargoWidgets),
00698 NWidget(WWT_PANEL, COLOUR_GREY, WID_STL_NOCARGOWAITING), SetMinimalSize(14, 11), SetDataTip(0x0, STR_STATION_LIST_NO_WAITING_CARGO), SetFill(0, 1), EndContainer(),
00699 NWidget(WWT_PUSHBTN, COLOUR_GREY, WID_STL_CARGOALL), SetMinimalSize(14, 11), SetDataTip(0x0, STR_STATION_LIST_SELECT_ALL_TYPES), SetFill(0, 1),
00700 NWidget(WWT_PANEL, COLOUR_GREY), SetDataTip(0x0, STR_NULL), SetResize(1, 0), SetFill(1, 1), EndContainer(),
00701 EndContainer(),
00702 NWidget(NWID_HORIZONTAL),
00703 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_STL_SORTBY), SetMinimalSize(81, 12), SetDataTip(STR_BUTTON_SORT_BY, STR_TOOLTIP_SORT_ORDER),
00704 NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_STL_SORTDROPBTN), SetMinimalSize(163, 12), SetDataTip(STR_SORT_BY_NAME, STR_TOOLTIP_SORT_CRITERIA),
00705 NWidget(WWT_PANEL, COLOUR_GREY), SetDataTip(0x0, STR_NULL), SetResize(1, 0), SetFill(1, 1), EndContainer(),
00706 EndContainer(),
00707 NWidget(NWID_HORIZONTAL),
00708 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(),
00709 NWidget(NWID_VERTICAL),
00710 NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_STL_SCROLLBAR),
00711 NWidget(WWT_RESIZEBOX, COLOUR_GREY),
00712 EndContainer(),
00713 EndContainer(),
00714 };
00715
00716 static const WindowDesc _company_stations_desc(
00717 WDP_AUTO, 358, 162,
00718 WC_STATION_LIST, WC_NONE,
00719 0,
00720 _nested_company_stations_widgets, lengthof(_nested_company_stations_widgets)
00721 );
00722
00728 void ShowCompanyStations(CompanyID company)
00729 {
00730 if (!Company::IsValidID(company)) return;
00731
00732 AllocateWindowDescFront<CompanyStationsWindow>(&_company_stations_desc, company);
00733 }
00734
00735 static const NWidgetPart _nested_station_view_widgets[] = {
00736 NWidget(NWID_HORIZONTAL),
00737 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
00738 NWidget(WWT_CAPTION, COLOUR_GREY, WID_SV_CAPTION), SetDataTip(STR_STATION_VIEW_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00739 NWidget(WWT_SHADEBOX, COLOUR_GREY),
00740 NWidget(WWT_STICKYBOX, COLOUR_GREY),
00741 EndContainer(),
00742 NWidget(NWID_HORIZONTAL),
00743 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SV_SORT_ORDER), SetMinimalSize(81, 12), SetFill(1, 1), SetDataTip(STR_BUTTON_SORT_BY, STR_TOOLTIP_SORT_ORDER),
00744 NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_SV_SORT_BY), SetMinimalSize(168, 12), SetResize(1, 0), SetFill(0, 1), SetDataTip(0x0, STR_TOOLTIP_SORT_CRITERIA),
00745 EndContainer(),
00746 NWidget(NWID_HORIZONTAL),
00747 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_SV_GROUP), SetMinimalSize(81, 12), SetFill(1, 1), SetDataTip(STR_STATION_VIEW_GROUP, 0x0),
00748 NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_SV_GROUP_BY), SetMinimalSize(168, 12), SetResize(1, 0), SetFill(0, 1), SetDataTip(0x0, STR_TOOLTIP_GROUP_ORDER),
00749 EndContainer(),
00750 NWidget(NWID_HORIZONTAL),
00751 NWidget(WWT_PANEL, COLOUR_GREY, WID_SV_WAITING), SetMinimalSize(237, 44), SetResize(1, 10), SetScrollbar(WID_SV_SCROLLBAR), EndContainer(),
00752 NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_SV_SCROLLBAR),
00753 EndContainer(),
00754 NWidget(WWT_PANEL, COLOUR_GREY, WID_SV_ACCEPT_RATING_LIST), SetMinimalSize(249, 23), SetResize(1, 0), EndContainer(),
00755 NWidget(NWID_HORIZONTAL),
00756 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
00757 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SV_LOCATION), SetMinimalSize(45, 12), SetResize(1, 0), SetFill(1, 1),
00758 SetDataTip(STR_BUTTON_LOCATION, STR_STATION_VIEW_CENTER_TOOLTIP),
00759 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SV_ACCEPTS_RATINGS), SetMinimalSize(46, 12), SetResize(1, 0), SetFill(1, 1),
00760 SetDataTip(STR_STATION_VIEW_RATINGS_BUTTON, STR_STATION_VIEW_RATINGS_TOOLTIP),
00761 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SV_RENAME), SetMinimalSize(45, 12), SetResize(1, 0), SetFill(1, 1),
00762 SetDataTip(STR_BUTTON_RENAME, STR_STATION_VIEW_RENAME_TOOLTIP),
00763 EndContainer(),
00764 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_SV_CLOSE_AIRPORT), SetMinimalSize(45, 12), SetResize(1, 0), SetFill(1, 1),
00765 SetDataTip(STR_STATION_VIEW_CLOSE_AIRPORT, STR_STATION_VIEW_CLOSE_AIRPORT_TOOLTIP),
00766 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SV_TRAINS), SetMinimalSize(14, 12), SetFill(0, 1), SetDataTip(STR_TRAIN, STR_STATION_VIEW_SCHEDULED_TRAINS_TOOLTIP),
00767 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SV_ROADVEHS), SetMinimalSize(14, 12), SetFill(0, 1), SetDataTip(STR_LORRY, STR_STATION_VIEW_SCHEDULED_ROAD_VEHICLES_TOOLTIP),
00768 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SV_SHIPS), SetMinimalSize(14, 12), SetFill(0, 1), SetDataTip(STR_SHIP, STR_STATION_VIEW_SCHEDULED_SHIPS_TOOLTIP),
00769 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SV_PLANES), SetMinimalSize(14, 12), SetFill(0, 1), SetDataTip(STR_PLANE, STR_STATION_VIEW_SCHEDULED_AIRCRAFT_TOOLTIP),
00770 NWidget(WWT_RESIZEBOX, COLOUR_GREY),
00771 EndContainer(),
00772 };
00773
00784 static void DrawCargoIcons(CargoID i, uint waiting, int left, int right, int y)
00785 {
00786 uint num = min((waiting + 5) / 10, (right - left) / 10);
00787 if (num == 0) return;
00788
00789 SpriteID sprite = CargoSpec::Get(i)->GetCargoIcon();
00790
00791 int x = _current_text_dir == TD_RTL ? left : right - num * 10;
00792 do {
00793 DrawSprite(sprite, PAL_NONE, x, y);
00794 x += 10;
00795 } while (--num);
00796 }
00797
00798 enum SortOrder {
00799 SO_DESCENDING,
00800 SO_ASCENDING
00801 };
00802
00803 class CargoDataEntry;
00804
00805 enum CargoSortType {
00806 ST_AS_GROUPING,
00807 ST_COUNT,
00808 ST_STATION_STRING,
00809 ST_STATION_ID,
00810 ST_CARGO_ID,
00811 };
00812
00813 class CargoSorter {
00814 public:
00815 CargoSorter(CargoSortType t = ST_STATION_ID, SortOrder o = SO_ASCENDING) : type(t), order(o) {}
00816 CargoSortType GetSortType() {return this->type;}
00817 bool operator()(const CargoDataEntry *cd1, const CargoDataEntry *cd2) const;
00818
00819 private:
00820 CargoSortType type;
00821 SortOrder order;
00822
00823 template<class Tid>
00824 bool SortId(Tid st1, Tid st2) const;
00825 bool SortCount(const CargoDataEntry *cd1, const CargoDataEntry *cd2) const;
00826 bool SortStation (StationID st1, StationID st2) const;
00827 };
00828
00829 typedef std::set<CargoDataEntry *, CargoSorter> CargoDataSet;
00830
00836 class CargoDataEntry {
00837 public:
00838 CargoDataEntry();
00839 ~CargoDataEntry();
00840
00846 CargoDataEntry *InsertOrRetrieve(StationID station)
00847 {
00848 return this->InsertOrRetrieve<StationID>(station);
00849 }
00850
00856 CargoDataEntry *InsertOrRetrieve(CargoID cargo)
00857 {
00858 return this->InsertOrRetrieve<CargoID>(cargo);
00859 }
00860
00861 void Update(uint count);
00862
00867 void Remove(StationID station)
00868 {
00869 CargoDataEntry t(station);
00870 this->Remove(&t);
00871 }
00872
00877 void Remove(CargoID cargo)
00878 {
00879 CargoDataEntry t(cargo);
00880 this->Remove(&t);
00881 }
00882
00888 CargoDataEntry *Retrieve(StationID station) const
00889 {
00890 CargoDataEntry t(station);
00891 return this->Retrieve(this->children->find(&t));
00892 }
00893
00899 CargoDataEntry *Retrieve(CargoID cargo) const
00900 {
00901 CargoDataEntry t(cargo);
00902 return this->Retrieve(this->children->find(&t));
00903 }
00904
00905 void Resort(CargoSortType type, SortOrder order);
00906
00910 StationID GetStation() const { return this->station; }
00911
00915 CargoID GetCargo() const { return this->cargo; }
00916
00920 uint GetCount() const { return this->count; }
00921
00925 CargoDataEntry *GetParent() const { return this->parent; }
00926
00930 uint GetNumChildren() const { return this->num_children; }
00931
00935 CargoDataSet::iterator Begin() const { return this->children->begin(); }
00936
00940 CargoDataSet::iterator End() const { return this->children->end(); }
00941
00942 void Clear();
00943 private:
00944
00945 CargoDataEntry(StationID st, uint c, CargoDataEntry *p);
00946 CargoDataEntry(CargoID car, uint c, CargoDataEntry *p);
00947 CargoDataEntry(StationID st);
00948 CargoDataEntry(CargoID car);
00949
00950 CargoDataEntry *Retrieve(CargoDataSet::iterator i) const;
00951
00952 template<class Tid>
00953 CargoDataEntry *InsertOrRetrieve(Tid s);
00954
00955 void Remove(CargoDataEntry *comp);
00956 void IncrementSize();
00957
00958 CargoDataEntry *parent;
00959 const union {
00960 StationID station;
00961 struct {
00962 CargoID cargo;
00963 bool transfers;
00964 };
00965 };
00966 uint num_children;
00967 uint count;
00968 CargoDataSet *children;
00969 };
00970
00971 CargoDataEntry::CargoDataEntry() :
00972 parent(NULL),
00973 station(INVALID_STATION),
00974 num_children(0),
00975 count(0),
00976 children(new CargoDataSet(CargoSorter(ST_CARGO_ID)))
00977 {}
00978
00979 CargoDataEntry::CargoDataEntry(CargoID cargo, uint count, CargoDataEntry *parent) :
00980 parent(parent),
00981 cargo(cargo),
00982 num_children(0),
00983 count(count),
00984 children(new CargoDataSet)
00985 {}
00986
00987 CargoDataEntry::CargoDataEntry(StationID station, uint count, CargoDataEntry *parent) :
00988 parent(parent),
00989 station(station),
00990 num_children(0),
00991 count(count),
00992 children(new CargoDataSet)
00993 {}
00994
00995 CargoDataEntry::CargoDataEntry(StationID station) :
00996 parent(NULL),
00997 station(station),
00998 num_children(0),
00999 count(0),
01000 children(NULL)
01001 {}
01002
01003 CargoDataEntry::CargoDataEntry(CargoID cargo) :
01004 parent(NULL),
01005 cargo(cargo),
01006 num_children(0),
01007 count(0),
01008 children(NULL)
01009 {}
01010
01011 CargoDataEntry::~CargoDataEntry()
01012 {
01013 this->Clear();
01014 delete this->children;
01015 }
01016
01020 void CargoDataEntry::Clear()
01021 {
01022 if (this->children != NULL) {
01023 for (CargoDataSet::iterator i = this->children->begin(); i != this->children->end(); ++i) {
01024 assert(*i != this);
01025 delete *i;
01026 }
01027 this->children->clear();
01028 }
01029 if (this->parent != NULL) this->parent->count -= this->count;
01030 this->count = 0;
01031 this->num_children = 0;
01032 }
01033
01040 void CargoDataEntry::Remove(CargoDataEntry *child)
01041 {
01042 CargoDataSet::iterator i = this->children->find(child);
01043 if (i != this->children->end()) {
01044 delete *i;
01045 this->children->erase(i);
01046 }
01047 }
01048
01055 template<class Tid>
01056 CargoDataEntry *CargoDataEntry::InsertOrRetrieve(Tid child_id)
01057 {
01058 CargoDataEntry tmp(child_id);
01059 CargoDataSet::iterator i = this->children->find(&tmp);
01060 if (i == this->children->end()) {
01061 IncrementSize();
01062 return *(this->children->insert(new CargoDataEntry(child_id, 0, this)).first);
01063 } else {
01064 CargoDataEntry *ret = *i;
01065 assert(this->children->value_comp().GetSortType() != ST_COUNT);
01066 return ret;
01067 }
01068 }
01069
01075 void CargoDataEntry::Update(uint count)
01076 {
01077 this->count += count;
01078 if (this->parent != NULL) this->parent->Update(count);
01079 }
01080
01084 void CargoDataEntry::IncrementSize()
01085 {
01086 ++this->num_children;
01087 if (this->parent != NULL) this->parent->IncrementSize();
01088 }
01089
01090 void CargoDataEntry::Resort(CargoSortType type, SortOrder order)
01091 {
01092 CargoDataSet *new_subs = new CargoDataSet(this->children->begin(), this->children->end(), CargoSorter(type, order));
01093 delete this->children;
01094 this->children = new_subs;
01095 }
01096
01097 CargoDataEntry *CargoDataEntry::Retrieve(CargoDataSet::iterator i) const
01098 {
01099 if (i == this->children->end()) {
01100 return NULL;
01101 } else {
01102 assert(this->children->value_comp().GetSortType() != ST_COUNT);
01103 return *i;
01104 }
01105 }
01106
01107 bool CargoSorter::operator()(const CargoDataEntry *cd1, const CargoDataEntry *cd2) const
01108 {
01109 switch (this->type) {
01110 case ST_STATION_ID:
01111 return this->SortId<StationID>(cd1->GetStation(), cd2->GetStation());
01112 case ST_CARGO_ID:
01113 return this->SortId<CargoID>(cd1->GetCargo(), cd2->GetCargo());
01114 case ST_COUNT:
01115 return this->SortCount(cd1, cd2);
01116 case ST_STATION_STRING:
01117 return this->SortStation(cd1->GetStation(), cd2->GetStation());
01118 default:
01119 NOT_REACHED();
01120 }
01121 }
01122
01123 template<class Tid>
01124 bool CargoSorter::SortId(Tid st1, Tid st2) const
01125 {
01126 return (this->order == SO_ASCENDING) ? st1 < st2 : st2 < st1;
01127 }
01128
01129 bool CargoSorter::SortCount(const CargoDataEntry *cd1, const CargoDataEntry *cd2) const
01130 {
01131 uint c1 = cd1->GetCount();
01132 uint c2 = cd2->GetCount();
01133 if (c1 == c2) {
01134 return this->SortStation(cd1->GetStation(), cd2->GetStation());
01135 } else if (this->order == SO_ASCENDING) {
01136 return c1 < c2;
01137 } else {
01138 return c2 < c1;
01139 }
01140 }
01141
01142 bool CargoSorter::SortStation(StationID st1, StationID st2) const
01143 {
01144 static char buf1[MAX_LENGTH_STATION_NAME_CHARS];
01145 static char buf2[MAX_LENGTH_STATION_NAME_CHARS];
01146
01147 if (!Station::IsValidID(st1)) {
01148 return Station::IsValidID(st2) ? this->order == SO_ASCENDING : this->SortId(st1, st2);
01149 } else if (!Station::IsValidID(st2)) {
01150 return order == SO_DESCENDING;
01151 }
01152
01153 SetDParam(0, st1);
01154 GetString(buf1, STR_STATION_NAME, lastof(buf1));
01155 SetDParam(0, st2);
01156 GetString(buf2, STR_STATION_NAME, lastof(buf2));
01157
01158 int res = strcmp(buf1, buf2);
01159 if (res == 0) {
01160 return this->SortId(st1, st2);
01161 } else {
01162 return (this->order == SO_ASCENDING) ? res < 0 : res > 0;
01163 }
01164 }
01165
01169 struct StationViewWindow : public Window {
01173 struct RowDisplay {
01174 RowDisplay(CargoDataEntry *f, StationID n) : filter(f), next_station(n) {}
01175 RowDisplay(CargoDataEntry *f, CargoID n) : filter(f), next_cargo(n) {}
01176
01180 CargoDataEntry *filter;
01181 union {
01185 StationID next_station;
01186
01190 CargoID next_cargo;
01191 };
01192 };
01193
01194 typedef std::vector<RowDisplay> CargoDataVector;
01195
01196 static const int NUM_COLUMNS = 4;
01197
01201 enum Invalidation {
01202 INV_FLOWS = 0x100,
01203 INV_CARGO = 0x200
01204 };
01205
01209 enum Grouping {
01210 GR_SOURCE,
01211 GR_NEXT,
01212 GR_DESTINATION,
01213 GR_CARGO,
01214 };
01215
01219 enum Mode {
01220 MODE_WAITING,
01221 MODE_PLANNED
01222 };
01223
01224 uint expand_shrink_width;
01225 int rating_lines;
01226 int accepts_lines;
01227 Scrollbar *vscroll;
01228
01230 enum AcceptListHeight {
01231 ALH_RATING = 13,
01232 ALH_ACCEPTS = 3,
01233 };
01234
01235 static const StringID _sort_names[];
01236 static const StringID _group_names[];
01237
01244 CargoSortType sortings[NUM_COLUMNS];
01245
01247 SortOrder sort_orders[NUM_COLUMNS];
01248
01249 int scroll_to_row;
01250 int grouping_index;
01251 Mode current_mode;
01252 Grouping groupings[NUM_COLUMNS];
01253
01254 CargoDataEntry expanded_rows;
01255 CargoDataEntry cached_destinations;
01256 CargoDataVector displayed_rows;
01257
01258 StationViewWindow(const WindowDesc *desc, WindowNumber window_number) : Window(),
01259 scroll_to_row(INT_MAX), grouping_index(0)
01260 {
01261 this->rating_lines = ALH_RATING;
01262 this->accepts_lines = ALH_ACCEPTS;
01263
01264 this->CreateNestedTree(desc);
01265 this->vscroll = this->GetScrollbar(WID_SV_SCROLLBAR);
01266
01267 this->FinishInitNested(desc, window_number);
01268
01269 this->groupings[0] = GR_CARGO;
01270 this->sortings[0] = ST_AS_GROUPING;
01271 this->SelectGroupBy(_settings_client.gui.station_gui_group_order);
01272 this->SelectSortBy(_settings_client.gui.station_gui_sort_by);
01273 this->sort_orders[0] = SO_ASCENDING;
01274 this->SelectSortOrder((SortOrder)_settings_client.gui.station_gui_sort_order);
01275 Owner owner = Station::Get(window_number)->owner;
01276 if (owner != OWNER_NONE) this->owner = owner;
01277 }
01278
01279 ~StationViewWindow()
01280 {
01281 Owner owner = Station::Get(this->window_number)->owner;
01282 DeleteWindowById(WC_TRAINS_LIST, VehicleListIdentifier(VL_STATION_LIST, VEH_TRAIN, owner, this->window_number).Pack(), false);
01283 DeleteWindowById(WC_ROADVEH_LIST, VehicleListIdentifier(VL_STATION_LIST, VEH_ROAD, owner, this->window_number).Pack(), false);
01284 DeleteWindowById(WC_SHIPS_LIST, VehicleListIdentifier(VL_STATION_LIST, VEH_SHIP, owner, this->window_number).Pack(), false);
01285 DeleteWindowById(WC_AIRCRAFT_LIST, VehicleListIdentifier(VL_STATION_LIST, VEH_AIRCRAFT, owner, this->window_number).Pack(), false);
01286 }
01287
01298 void ShowCargo(CargoDataEntry *data, CargoID cargo, StationID source, StationID next, StationID dest, uint count)
01299 {
01300 if (count == 0) return;
01301 bool auto_distributed = _settings_game.linkgraph.GetDistributionType(cargo) != DT_MANUAL;
01302 const CargoDataEntry *expand = &this->expanded_rows;
01303 for (int i = 0; i < NUM_COLUMNS && expand != NULL; ++i) {
01304 switch (groupings[i]) {
01305 case GR_CARGO:
01306 assert(i == 0);
01307 data = data->InsertOrRetrieve(cargo);
01308 data->transfers = (source != this->window_number);
01309 expand = expand->Retrieve(cargo);
01310 break;
01311 case GR_SOURCE:
01312 if (auto_distributed || source != this->window_number) {
01313 data = data->InsertOrRetrieve(source);
01314 expand = expand->Retrieve(source);
01315 }
01316 break;
01317 case GR_NEXT:
01318 if (auto_distributed) {
01319 data = data->InsertOrRetrieve(next);
01320 expand = expand->Retrieve(next);
01321 }
01322 break;
01323 case GR_DESTINATION:
01324 if (auto_distributed) {
01325 data = data->InsertOrRetrieve(dest);
01326 expand = expand->Retrieve(dest);
01327 }
01328 break;
01329 }
01330 }
01331 data->Update(count);
01332 }
01333
01334 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
01335 {
01336 switch (widget) {
01337 case WID_SV_WAITING:
01338 resize->height = FONT_HEIGHT_NORMAL;
01339 size->height = WD_FRAMERECT_TOP + 4 * resize->height + WD_FRAMERECT_BOTTOM;
01340 this->expand_shrink_width = max(GetStringBoundingBox("-").width, GetStringBoundingBox("+").width) + WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
01341 break;
01342
01343 case WID_SV_ACCEPT_RATING_LIST:
01344 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;
01345 break;
01346
01347 case WID_SV_CLOSE_AIRPORT:
01348 if (!(Station::Get(this->window_number)->facilities & FACIL_AIRPORT)) {
01349
01350 size->width = 0;
01351 resize->width = 0;
01352 fill->width = 0;
01353 }
01354 break;
01355 }
01356 }
01357
01358 virtual void OnPaint()
01359 {
01360 const Station *st = Station::Get(this->window_number);
01361 CargoDataEntry cargo;
01362 BuildCargoList(&cargo, st);
01363
01364 this->vscroll->SetCount(cargo.GetNumChildren());
01365
01366
01367 this->SetWidgetDisabledState(WID_SV_RENAME, st->owner != _local_company);
01368 this->SetWidgetDisabledState(WID_SV_TRAINS, !(st->facilities & FACIL_TRAIN));
01369 this->SetWidgetDisabledState(WID_SV_ROADVEHS, !(st->facilities & FACIL_TRUCK_STOP) && !(st->facilities & FACIL_BUS_STOP));
01370 this->SetWidgetDisabledState(WID_SV_SHIPS, !(st->facilities & FACIL_DOCK));
01371 this->SetWidgetDisabledState(WID_SV_PLANES, !(st->facilities & FACIL_AIRPORT));
01372 this->SetWidgetDisabledState(WID_SV_CLOSE_AIRPORT, !(st->facilities & FACIL_AIRPORT) || st->owner != _local_company || st->owner == OWNER_NONE);
01373 this->SetWidgetLoweredState(WID_SV_CLOSE_AIRPORT, (st->facilities & FACIL_AIRPORT) && (st->airport.flags & AIRPORT_CLOSED_block) != 0);
01374
01375 this->DrawWidgets();
01376
01377 if (!this->IsShaded()) {
01378
01379 const NWidgetBase *wid = this->GetWidget<NWidgetBase>(WID_SV_ACCEPT_RATING_LIST);
01380 const Rect r = {wid->pos_x, wid->pos_y, wid->pos_x + wid->current_x - 1, wid->pos_y + wid->current_y - 1};
01381 if (this->GetWidget<NWidgetCore>(WID_SV_ACCEPTS_RATINGS)->widget_data == STR_STATION_VIEW_RATINGS_BUTTON) {
01382 int lines = this->DrawAcceptedCargo(r);
01383 if (lines > this->accepts_lines) {
01384 this->accepts_lines = lines;
01385 this->ReInit();
01386 return;
01387 }
01388 } else {
01389 int lines = this->DrawCargoRatings(r);
01390 if (lines > this->rating_lines) {
01391 this->rating_lines = lines;
01392 this->ReInit();
01393 return;
01394 }
01395 }
01396
01397
01398 this->DrawSortButtonState(WID_SV_SORT_ORDER, sort_orders[1] == SO_ASCENDING ? SBS_UP : SBS_DOWN);
01399
01400 int pos = this->vscroll->GetPosition();
01401
01402 int maxrows = this->vscroll->GetCapacity();
01403
01404 displayed_rows.clear();
01405
01406
01407 NWidgetBase *nwi = this->GetWidget<NWidgetBase>(WID_SV_WAITING);
01408 Rect waiting_rect = {nwi->pos_x, nwi->pos_y, nwi->pos_x + nwi->current_x - 1, nwi->pos_y + nwi->current_y - 1};
01409 this->DrawEntries(&cargo, waiting_rect, pos, maxrows, 0);
01410 scroll_to_row = INT_MAX;
01411 }
01412 }
01413
01414 virtual void SetStringParameters(int widget) const
01415 {
01416 const Station *st = Station::Get(this->window_number);
01417 SetDParam(0, st->index);
01418 SetDParam(1, st->facilities);
01419 }
01420
01426 void RecalcDestinations(CargoID i)
01427 {
01428 const Station *st = Station::Get(this->window_number);
01429 CargoDataEntry *cargo_entry = cached_destinations.InsertOrRetrieve(i);
01430 cargo_entry->Clear();
01431
01432 const FlowStatMap &flows = st->goods[i].flows;
01433 for (FlowStatMap::const_iterator it = flows.begin(); it != flows.end(); ++it) {
01434 StationID from = it->first;
01435 CargoDataEntry *source_entry = cargo_entry->InsertOrRetrieve(from);
01436 const FlowStat::SharesMap *shares = it->second.GetShares();
01437 uint32 prev_count = 0;
01438 for (FlowStat::SharesMap::const_iterator flow_it = shares->begin(); flow_it != shares->end(); ++flow_it) {
01439 StationID via = flow_it->second;
01440 CargoDataEntry *via_entry = source_entry->InsertOrRetrieve(via);
01441 if (via == this->window_number) {
01442 via_entry->InsertOrRetrieve(via)->Update(flow_it->first - prev_count);
01443 } else {
01444 EstimateDestinations(i, from, via, flow_it->first - prev_count, via_entry);
01445 }
01446 prev_count = flow_it->first;
01447 }
01448 }
01449 }
01450
01460 void EstimateDestinations(CargoID cargo, StationID source, StationID next, uint count, CargoDataEntry *dest)
01461 {
01462 if (Station::IsValidID(next) && Station::IsValidID(source)) {
01463 CargoDataEntry tmp;
01464 const FlowStatMap &flowmap = Station::Get(next)->goods[cargo].flows;
01465 FlowStatMap::const_iterator map_it = flowmap.find(source);
01466 if (map_it != flowmap.end()) {
01467 const FlowStat::SharesMap *shares = map_it->second.GetShares();
01468 uint32 prev_count = 0;
01469 for (FlowStat::SharesMap::const_iterator i = shares->begin(); i != shares->end(); ++i) {
01470 tmp.InsertOrRetrieve(i->second)->Update(i->first - prev_count);
01471 prev_count = i->first;
01472 }
01473 }
01474
01475 if (tmp.GetCount() == 0) {
01476 dest->InsertOrRetrieve(INVALID_STATION)->Update(count);
01477 } else {
01478 uint sum_estimated = 0;
01479 while (sum_estimated < count) {
01480 for (CargoDataSet::iterator i = tmp.Begin(); i != tmp.End() && sum_estimated < count; ++i) {
01481 CargoDataEntry *child = *i;
01482 uint estimate = DivideApprox(child->GetCount() * count, tmp.GetCount());
01483 if (estimate == 0) estimate = 1;
01484
01485 sum_estimated += estimate;
01486 if (sum_estimated > count) {
01487 estimate -= sum_estimated - count;
01488 sum_estimated = count;
01489 }
01490
01491 if (estimate > 0) {
01492 if (child->GetStation() == next) {
01493 dest->InsertOrRetrieve(next)->Update(estimate);
01494 } else {
01495 EstimateDestinations(cargo, source, child->GetStation(), estimate, dest);
01496 }
01497 }
01498 }
01499
01500 }
01501 }
01502 } else {
01503 dest->InsertOrRetrieve(INVALID_STATION)->Update(count);
01504 }
01505 }
01506
01513 void BuildFlowList(CargoID i, const FlowStatMap &flows, CargoDataEntry *cargo)
01514 {
01515 const CargoDataEntry *source_dest = this->cached_destinations.Retrieve(i);
01516 for (FlowStatMap::const_iterator it = flows.begin(); it != flows.end(); ++it) {
01517 StationID from = it->first;
01518 const CargoDataEntry *source_entry = source_dest->Retrieve(from);
01519 const FlowStat::SharesMap *shares = it->second.GetShares();
01520 for (FlowStat::SharesMap::const_iterator flow_it = shares->begin(); flow_it != shares->end(); ++flow_it) {
01521 const CargoDataEntry *via_entry = source_entry->Retrieve(flow_it->second);
01522 for (CargoDataSet::iterator dest_it = via_entry->Begin(); dest_it != via_entry->End(); ++dest_it) {
01523 CargoDataEntry *dest_entry = *dest_it;
01524 ShowCargo(cargo, i, from, flow_it->second, dest_entry->GetStation(), dest_entry->GetCount());
01525 }
01526 }
01527 }
01528 }
01529
01536 void BuildCargoList(CargoID i, const StationCargoList &packets, CargoDataEntry *cargo)
01537 {
01538 const CargoDataEntry *source_dest = this->cached_destinations.Retrieve(i);
01539 for (StationCargoList::ConstIterator it = packets.Packets()->begin(); it != packets.Packets()->end(); it++) {
01540 const CargoPacket *cp = *it;
01541 StationID next = it.GetKey();
01542
01543 const CargoDataEntry *source_entry = source_dest->Retrieve(cp->SourceStation());
01544 if (source_entry == NULL) {
01545 this->ShowCargo(cargo, i, cp->SourceStation(), next, INVALID_STATION, cp->Count());
01546 continue;
01547 }
01548
01549 const CargoDataEntry *via_entry = source_entry->Retrieve(next);
01550 if (via_entry == NULL) {
01551 this->ShowCargo(cargo, i, cp->SourceStation(), next, INVALID_STATION, cp->Count());
01552 continue;
01553 }
01554
01555 for (CargoDataSet::iterator dest_it = via_entry->Begin(); dest_it != via_entry->End(); ++dest_it) {
01556 CargoDataEntry *dest_entry = *dest_it;
01557 uint val = DivideApprox(cp->Count() * dest_entry->GetCount(), via_entry->GetCount());
01558 this->ShowCargo(cargo, i, cp->SourceStation(), next, dest_entry->GetStation(), val);
01559 }
01560 }
01561 this->ShowCargo(cargo, i, NEW_STATION, NEW_STATION, NEW_STATION, packets.ReservedCount());
01562 }
01563
01569 void BuildCargoList(CargoDataEntry *cargo, const Station *st)
01570 {
01571 for (CargoID i = 0; i < NUM_CARGO; i++) {
01572
01573 if (this->cached_destinations.Retrieve(i) == NULL) {
01574 this->RecalcDestinations(i);
01575 }
01576
01577 if (this->current_mode == MODE_WAITING) {
01578 this->BuildCargoList(i, st->goods[i].cargo, cargo);
01579 } else {
01580 this->BuildFlowList(i, st->goods[i].flows, cargo);
01581 }
01582 }
01583 }
01584
01589 void SetDisplayedRow(const CargoDataEntry *data)
01590 {
01591 std::list<StationID> stations;
01592 const CargoDataEntry *parent = data->GetParent();
01593 if (parent->GetParent() == NULL) {
01594 this->displayed_rows.push_back(RowDisplay(&this->expanded_rows, data->GetCargo()));
01595 return;
01596 }
01597
01598 StationID next = data->GetStation();
01599 while (parent->GetParent()->GetParent() != NULL) {
01600 stations.push_back(parent->GetStation());
01601 parent = parent->GetParent();
01602 }
01603
01604 CargoID cargo = parent->GetCargo();
01605 CargoDataEntry *filter = this->expanded_rows.Retrieve(cargo);
01606 while (!stations.empty()) {
01607 filter = filter->Retrieve(stations.back());
01608 stations.pop_back();
01609 }
01610
01611 this->displayed_rows.push_back(RowDisplay(filter, next));
01612 }
01613
01622 StringID GetEntryString(StationID station, StringID here, StringID other_station, StringID any)
01623 {
01624 if (station == this->window_number) {
01625 return here;
01626 } else if (station == INVALID_STATION) {
01627 return any;
01628 } else if (station == NEW_STATION) {
01629 return STR_STATION_VIEW_RESERVED;
01630 } else {
01631 SetDParam(2, station);
01632 return other_station;
01633 }
01634 }
01635
01643 StringID SearchNonStop(CargoDataEntry *cd, StationID station, int column)
01644 {
01645 CargoDataEntry *parent = cd->GetParent();
01646 for (int i = column - 1; i > 0; --i) {
01647 if (this->groupings[i] == GR_DESTINATION) {
01648 if (parent->GetStation() == station) {
01649 return STR_STATION_VIEW_NONSTOP;
01650 } else {
01651 return STR_STATION_VIEW_VIA;
01652 }
01653 }
01654 parent = parent->GetParent();
01655 }
01656
01657 if (this->groupings[column + 1] == GR_DESTINATION) {
01658 CargoDataSet::iterator begin = cd->Begin();
01659 CargoDataSet::iterator end = cd->End();
01660 if (begin != end && ++(cd->Begin()) == end && (*(begin))->GetStation() == station) {
01661 return STR_STATION_VIEW_NONSTOP;
01662 } else {
01663 return STR_STATION_VIEW_VIA;
01664 }
01665 }
01666
01667 return STR_STATION_VIEW_VIA;
01668 }
01669
01680 int DrawEntries(CargoDataEntry *entry, Rect &r, int pos, int maxrows, int column, CargoID cargo = CT_INVALID)
01681 {
01682 if (this->sortings[column] == ST_AS_GROUPING) {
01683 if (this->groupings[column] != GR_CARGO) {
01684 entry->Resort(ST_STATION_STRING, this->sort_orders[column]);
01685 }
01686 } else {
01687 entry->Resort(ST_COUNT, this->sort_orders[column]);
01688 }
01689 for (CargoDataSet::iterator i = entry->Begin(); i != entry->End(); ++i) {
01690 CargoDataEntry *cd = *i;
01691
01692 Grouping grouping = this->groupings[column];
01693 if (grouping == GR_CARGO) cargo = cd->GetCargo();
01694 bool auto_distributed = _settings_game.linkgraph.GetDistributionType(cargo) != DT_MANUAL;
01695
01696 if (pos > -maxrows && pos <= 0) {
01697 StringID str = STR_EMPTY;
01698 int y = r.top + WD_FRAMERECT_TOP - pos * FONT_HEIGHT_NORMAL;
01699 SetDParam(0, cargo);
01700 SetDParam(1, cd->GetCount());
01701
01702 if (this->groupings[column] == GR_CARGO) {
01703 str = STR_STATION_VIEW_WAITING_CARGO;
01704 DrawCargoIcons(cd->GetCargo(), cd->GetCount(), r.left + WD_FRAMERECT_LEFT + this->expand_shrink_width, r.right - WD_FRAMERECT_RIGHT - this->expand_shrink_width, y);
01705 } else {
01706 if (!auto_distributed) grouping = GR_SOURCE;
01707 StationID station = cd->GetStation();
01708
01709 switch (grouping) {
01710 case GR_SOURCE:
01711 str = this->GetEntryString(station, STR_STATION_VIEW_FROM_HERE, STR_STATION_VIEW_FROM, STR_STATION_VIEW_FROM_ANY);
01712 break;
01713 case GR_NEXT:
01714 str = this->GetEntryString(station, STR_STATION_VIEW_VIA_HERE, STR_STATION_VIEW_VIA, STR_STATION_VIEW_VIA_ANY);
01715 if (str == STR_STATION_VIEW_VIA) str = this->SearchNonStop(cd, station, column);
01716 break;
01717 case GR_DESTINATION:
01718 str = this->GetEntryString(station, STR_STATION_VIEW_TO_HERE, STR_STATION_VIEW_TO, STR_STATION_VIEW_TO_ANY);
01719 break;
01720 default:
01721 NOT_REACHED();
01722 }
01723 if (pos == -this->scroll_to_row && Station::IsValidID(station)) {
01724 ScrollMainWindowToTile(Station::Get(station)->xy);
01725 }
01726 }
01727
01728 bool rtl = _current_text_dir == TD_RTL;
01729 int text_left = rtl ? r.left + this->expand_shrink_width : r.left + WD_FRAMERECT_LEFT + column * this->expand_shrink_width;
01730 int text_right = rtl ? r.right - WD_FRAMERECT_LEFT - column * this->expand_shrink_width : r.right - this->expand_shrink_width;
01731 int shrink_left = rtl ? r.left + WD_FRAMERECT_LEFT : r.right - this->expand_shrink_width + WD_FRAMERECT_LEFT;
01732 int shrink_right = rtl ? r.left + this->expand_shrink_width - WD_FRAMERECT_RIGHT : r.right - WD_FRAMERECT_RIGHT;
01733
01734 DrawString(text_left, text_right, y, str);
01735
01736 if (column < NUM_COLUMNS - 1) {
01737 const char *sym = NULL;
01738 if (cd->GetNumChildren() > 0) {
01739 sym = "-";
01740 } else if (auto_distributed && str != STR_STATION_VIEW_RESERVED) {
01741 sym = "+";
01742 } else {
01743
01744 const StationCargoList &list = Station::Get(this->window_number)->goods[cargo].cargo;
01745 if (grouping == GR_CARGO && (list.ReservedCount() > 0 || cd->transfers)) {
01746 sym = "+";
01747 }
01748 }
01749 if (sym) DrawString(shrink_left, shrink_right, y, sym, TC_YELLOW);
01750 }
01751 this->SetDisplayedRow(cd);
01752 }
01753 --pos;
01754 if (auto_distributed || column == 0) {
01755 pos = this->DrawEntries(cd, r, pos, maxrows, column + 1, cargo);
01756 }
01757 }
01758 return pos;
01759 }
01760
01766 int DrawAcceptedCargo(const Rect &r) const
01767 {
01768 const Station *st = Station::Get(this->window_number);
01769
01770 uint32 cargo_mask = 0;
01771 for (CargoID i = 0; i < NUM_CARGO; i++) {
01772 if (HasBit(st->goods[i].acceptance_pickup, GoodsEntry::GES_ACCEPTANCE)) SetBit(cargo_mask, i);
01773 }
01774 SetDParam(0, cargo_mask);
01775 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);
01776 return CeilDiv(bottom - r.top - WD_FRAMERECT_TOP, FONT_HEIGHT_NORMAL);
01777 }
01778
01784 int DrawCargoRatings(const Rect &r) const
01785 {
01786 const Station *st = Station::Get(this->window_number);
01787 int y = r.top + WD_FRAMERECT_TOP;
01788
01789 if (st->town->exclusive_counter > 0) {
01790 SetDParam(0, st->town->exclusivity);
01791 y = DrawStringMultiLine(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, r.bottom, st->town->exclusivity == st->owner ? STR_STATIOV_VIEW_EXCLUSIVE_RIGHTS_SELF : STR_STATIOV_VIEW_EXCLUSIVE_RIGHTS_COMPANY);
01792 y += WD_PAR_VSEP_WIDE;
01793 }
01794
01795 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_STATION_VIEW_SUPPLY_RATINGS_TITLE);
01796 y += FONT_HEIGHT_NORMAL;
01797
01798 const CargoSpec *cs;
01799 FOR_ALL_SORTED_STANDARD_CARGOSPECS(cs) {
01800 const GoodsEntry *ge = &st->goods[cs->Index()];
01801 if (!HasBit(ge->acceptance_pickup, GoodsEntry::GES_PICKUP)) continue;
01802
01803 SetDParam(0, cs->name);
01804 SetDParam(1, ge->supply);
01805 SetDParam(3, ToPercent8(ge->rating));
01806 SetDParam(2, STR_CARGO_RATING_APPALLING + (ge->rating >> 5));
01807 DrawString(r.left + WD_FRAMERECT_LEFT + 6, r.right - WD_FRAMERECT_RIGHT - 6, y, STR_STATION_VIEW_CARGO_SUPPLY_RATING);
01808 y += FONT_HEIGHT_NORMAL;
01809 }
01810 return CeilDiv(y - r.top - WD_FRAMERECT_TOP, FONT_HEIGHT_NORMAL);
01811 }
01812
01818 template<class Tid>
01819 void HandleCargoWaitingClick(CargoDataEntry *filter, Tid next)
01820 {
01821 if (filter->Retrieve(next) != NULL) {
01822 filter->Remove(next);
01823 } else {
01824 filter->InsertOrRetrieve(next);
01825 }
01826 }
01827
01832 void HandleCargoWaitingClick(int row)
01833 {
01834 if (row < 0 || (uint)row >= this->displayed_rows.size()) return;
01835 if (_ctrl_pressed) {
01836 this->scroll_to_row = row;
01837 } else {
01838 RowDisplay &display = this->displayed_rows[row];
01839 if (display.filter == &this->expanded_rows) {
01840 this->HandleCargoWaitingClick<CargoID>(display.filter, display.next_cargo);
01841 } else {
01842 this->HandleCargoWaitingClick<StationID>(display.filter, display.next_station);
01843 }
01844 }
01845 this->SetWidgetDirty(WID_SV_WAITING);
01846 }
01847
01848 virtual void OnClick(Point pt, int widget, int click_count)
01849 {
01850 switch (widget) {
01851 case WID_SV_WAITING:
01852 this->HandleCargoWaitingClick(this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_SV_WAITING, WD_FRAMERECT_TOP, FONT_HEIGHT_NORMAL) - this->vscroll->GetPosition());
01853 break;
01854
01855 case WID_SV_LOCATION:
01856 if (_ctrl_pressed) {
01857 ShowExtraViewPortWindow(Station::Get(this->window_number)->xy);
01858 } else {
01859 ScrollMainWindowToTile(Station::Get(this->window_number)->xy);
01860 }
01861 break;
01862
01863 case WID_SV_ACCEPTS_RATINGS: {
01864
01865 int height_change;
01866 NWidgetCore *nwi = this->GetWidget<NWidgetCore>(WID_SV_ACCEPTS_RATINGS);
01867 if (this->GetWidget<NWidgetCore>(WID_SV_ACCEPTS_RATINGS)->widget_data == STR_STATION_VIEW_RATINGS_BUTTON) {
01868 nwi->SetDataTip(STR_STATION_VIEW_ACCEPTS_BUTTON, STR_STATION_VIEW_ACCEPTS_TOOLTIP);
01869 height_change = this->rating_lines - this->accepts_lines;
01870 } else {
01871 nwi->SetDataTip(STR_STATION_VIEW_RATINGS_BUTTON, STR_STATION_VIEW_RATINGS_TOOLTIP);
01872 height_change = this->accepts_lines - this->rating_lines;
01873 }
01874 this->ReInit(0, height_change * FONT_HEIGHT_NORMAL);
01875 break;
01876 }
01877
01878 case WID_SV_RENAME:
01879 SetDParam(0, this->window_number);
01880 ShowQueryString(STR_STATION_NAME, STR_STATION_VIEW_RENAME_STATION_CAPTION, MAX_LENGTH_STATION_NAME_CHARS,
01881 this, CS_ALPHANUMERAL, QSF_ENABLE_DEFAULT | QSF_LEN_IN_CHARS);
01882 break;
01883
01884 case WID_SV_CLOSE_AIRPORT:
01885 DoCommandP(0, this->window_number, 0, CMD_OPEN_CLOSE_AIRPORT);
01886 break;
01887
01888 case WID_SV_TRAINS:
01889 case WID_SV_ROADVEHS:
01890 case WID_SV_SHIPS:
01891 case WID_SV_PLANES: {
01892 Owner owner = Station::Get(this->window_number)->owner;
01893 ShowVehicleListWindow(owner, (VehicleType)(widget - WID_SV_TRAINS), (StationID)this->window_number);
01894 break;
01895 }
01896
01897 case WID_SV_SORT_BY: {
01898 ShowDropDownMenu(this, _sort_names, this->current_mode, WID_SV_SORT_BY, 0, 0);
01899 break;
01900 }
01901
01902 case WID_SV_GROUP_BY: {
01903 ShowDropDownMenu(this, _group_names, this->grouping_index, WID_SV_GROUP_BY, 0, 0);
01904 break;
01905 }
01906
01907 case WID_SV_SORT_ORDER: {
01908 this->SelectSortOrder(this->sort_orders[1] == SO_ASCENDING ? SO_DESCENDING : SO_ASCENDING);
01909 this->SetTimeout();
01910 this->LowerWidget(WID_SV_SORT_ORDER);
01911 break;
01912 }
01913 }
01914 }
01915
01920 void SelectSortOrder(SortOrder order)
01921 {
01922 this->sort_orders[1] = this->sort_orders[2] = this->sort_orders[3] = order;
01923 _settings_client.gui.station_gui_sort_order = this->sort_orders[1];
01924 this->SetDirty();
01925 }
01926
01931 void SelectSortBy(int index)
01932 {
01933 _settings_client.gui.station_gui_sort_by = index;
01934 switch (_sort_names[index]) {
01935 case STR_STATION_VIEW_WAITING_STATION:
01936 this->current_mode = MODE_WAITING;
01937 this->sortings[1] = this->sortings[2] = this->sortings[3] = ST_AS_GROUPING;
01938 break;
01939 case STR_STATION_VIEW_WAITING_AMOUNT:
01940 this->current_mode = MODE_WAITING;
01941 this->sortings[1] = this->sortings[2] = this->sortings[3] = ST_COUNT;
01942 break;
01943 case STR_STATION_VIEW_PLANNED_STATION:
01944 this->current_mode = MODE_PLANNED;
01945 this->sortings[1] = this->sortings[2] = this->sortings[3] = ST_AS_GROUPING;
01946 break;
01947 case STR_STATION_VIEW_PLANNED_AMOUNT:
01948 this->current_mode = MODE_PLANNED;
01949 this->sortings[1] = this->sortings[2] = this->sortings[3] = ST_COUNT;
01950 break;
01951 default:
01952 NOT_REACHED();
01953 }
01954
01955 this->GetWidget<NWidgetCore>(WID_SV_SORT_BY)->widget_data = _sort_names[index];
01956 this->SetDirty();
01957 }
01958
01963 void SelectGroupBy(int index)
01964 {
01965 this->grouping_index = index;
01966 _settings_client.gui.station_gui_group_order = index;
01967 this->GetWidget<NWidgetCore>(WID_SV_GROUP_BY)->widget_data = _group_names[index];
01968 switch (_group_names[index]) {
01969 case STR_STATION_VIEW_GROUP_S_V_D:
01970 this->groupings[1] = GR_SOURCE;
01971 this->groupings[2] = GR_NEXT;
01972 this->groupings[3] = GR_DESTINATION;
01973 break;
01974 case STR_STATION_VIEW_GROUP_S_D_V:
01975 this->groupings[1] = GR_SOURCE;
01976 this->groupings[2] = GR_DESTINATION;
01977 this->groupings[3] = GR_NEXT;
01978 break;
01979 case STR_STATION_VIEW_GROUP_V_S_D:
01980 this->groupings[1] = GR_NEXT;
01981 this->groupings[2] = GR_SOURCE;
01982 this->groupings[3] = GR_DESTINATION;
01983 break;
01984 case STR_STATION_VIEW_GROUP_V_D_S:
01985 this->groupings[1] = GR_NEXT;
01986 this->groupings[2] = GR_DESTINATION;
01987 this->groupings[3] = GR_SOURCE;
01988 break;
01989 case STR_STATION_VIEW_GROUP_D_S_V:
01990 this->groupings[1] = GR_DESTINATION;
01991 this->groupings[2] = GR_SOURCE;
01992 this->groupings[3] = GR_NEXT;
01993 break;
01994 case STR_STATION_VIEW_GROUP_D_V_S:
01995 this->groupings[1] = GR_DESTINATION;
01996 this->groupings[2] = GR_NEXT;
01997 this->groupings[3] = GR_SOURCE;
01998 break;
01999 }
02000 this->SetDirty();
02001 }
02002
02003 virtual void OnDropdownSelect(int widget, int index)
02004 {
02005 if (widget == WID_SV_SORT_BY) {
02006 this->SelectSortBy(index);
02007 } else {
02008 this->SelectGroupBy(index);
02009 }
02010 }
02011
02012 virtual void OnQueryTextFinished(char *str)
02013 {
02014 if (str == NULL) return;
02015
02016 DoCommandP(0, this->window_number, 0, CMD_RENAME_STATION | CMD_MSG(STR_ERROR_CAN_T_RENAME_STATION), NULL, str);
02017 }
02018
02019 virtual void OnResize()
02020 {
02021 this->vscroll->SetCapacityFromWidget(this, WID_SV_WAITING, WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM);
02022 }
02023
02029 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
02030 {
02031 if (gui_scope) {
02032 if (data >= 0 && data < NUM_CARGO) {
02033 this->cached_destinations.Remove((CargoID)data);
02034 } else {
02035 this->ReInit();
02036 }
02037 }
02038 }
02039 };
02040
02041 const StringID StationViewWindow::_sort_names[] = {
02042 STR_STATION_VIEW_WAITING_STATION,
02043 STR_STATION_VIEW_WAITING_AMOUNT,
02044 STR_STATION_VIEW_PLANNED_STATION,
02045 STR_STATION_VIEW_PLANNED_AMOUNT,
02046 INVALID_STRING_ID
02047 };
02048
02049 const StringID StationViewWindow::_group_names[] = {
02050 STR_STATION_VIEW_GROUP_S_V_D,
02051 STR_STATION_VIEW_GROUP_S_D_V,
02052 STR_STATION_VIEW_GROUP_V_S_D,
02053 STR_STATION_VIEW_GROUP_V_D_S,
02054 STR_STATION_VIEW_GROUP_D_S_V,
02055 STR_STATION_VIEW_GROUP_D_V_S,
02056 INVALID_STRING_ID
02057 };
02058
02059 static const WindowDesc _station_view_desc(
02060 WDP_AUTO, 249, 117,
02061 WC_STATION_VIEW, WC_NONE,
02062 0,
02063 _nested_station_view_widgets, lengthof(_nested_station_view_widgets)
02064 );
02065
02071 void ShowStationViewWindow(StationID station)
02072 {
02073 AllocateWindowDescFront<StationViewWindow>(&_station_view_desc, station);
02074 }
02075
02077 struct TileAndStation {
02078 TileIndex tile;
02079 StationID station;
02080 };
02081
02082 static SmallVector<TileAndStation, 8> _deleted_stations_nearby;
02083 static SmallVector<StationID, 8> _stations_nearby_list;
02084
02092 template <class T>
02093 static bool AddNearbyStation(TileIndex tile, void *user_data)
02094 {
02095 TileArea *ctx = (TileArea *)user_data;
02096
02097
02098 for (uint i = 0; i < _deleted_stations_nearby.Length(); i++) {
02099 TileAndStation *ts = _deleted_stations_nearby.Get(i);
02100 if (ts->tile == tile) {
02101 *_stations_nearby_list.Append() = _deleted_stations_nearby[i].station;
02102 _deleted_stations_nearby.Erase(ts);
02103 i--;
02104 }
02105 }
02106
02107
02108 if (!IsTileType(tile, MP_STATION)) return false;
02109
02110 StationID sid = GetStationIndex(tile);
02111
02112
02113 if (!T::IsValidID(sid)) return false;
02114
02115 T *st = T::Get(sid);
02116 if (st->owner != _local_company || _stations_nearby_list.Contains(sid)) return false;
02117
02118 if (st->rect.BeforeAddRect(ctx->tile, ctx->w, ctx->h, StationRect::ADD_TEST).Succeeded()) {
02119 *_stations_nearby_list.Append() = sid;
02120 }
02121
02122 return false;
02123 }
02124
02134 template <class T>
02135 static const T *FindStationsNearby(TileArea ta, bool distant_join)
02136 {
02137 TileArea ctx = ta;
02138
02139 _stations_nearby_list.Clear();
02140 _deleted_stations_nearby.Clear();
02141
02142
02143 TILE_AREA_LOOP(t, ta) {
02144 if (t < MapSize() && IsTileType(t, MP_STATION) && T::IsValidID(GetStationIndex(t))) return T::GetByTile(t);
02145 }
02146
02147
02148 const BaseStation *st;
02149 FOR_ALL_BASE_STATIONS(st) {
02150 if (T::IsExpected(st) && !st->IsInUse() && st->owner == _local_company) {
02151
02152 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) {
02153 TileAndStation *ts = _deleted_stations_nearby.Append();
02154 ts->tile = st->xy;
02155 ts->station = st->index;
02156
02157
02158 if (IsInsideBS(TileX(st->xy), TileX(ctx.tile), ctx.w) &&
02159 IsInsideBS(TileY(st->xy), TileY(ctx.tile), ctx.h)) {
02160 AddNearbyStation<T>(st->xy, &ctx);
02161 }
02162 }
02163 }
02164 }
02165
02166
02167
02168
02169 if (distant_join && min(ta.w, ta.h) >= _settings_game.station.station_spread) return NULL;
02170 uint max_dist = distant_join ? _settings_game.station.station_spread - min(ta.w, ta.h) : 1;
02171
02172 TileIndex tile = TILE_ADD(ctx.tile, TileOffsByDir(DIR_N));
02173 CircularTileSearch(&tile, max_dist, ta.w, ta.h, AddNearbyStation<T>, &ctx);
02174
02175 return NULL;
02176 }
02177
02178 static const NWidgetPart _nested_select_station_widgets[] = {
02179 NWidget(NWID_HORIZONTAL),
02180 NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
02181 NWidget(WWT_CAPTION, COLOUR_DARK_GREEN, WID_JS_CAPTION), SetDataTip(STR_JOIN_STATION_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
02182 EndContainer(),
02183 NWidget(NWID_HORIZONTAL),
02184 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_JS_PANEL), SetResize(1, 0), SetScrollbar(WID_JS_SCROLLBAR), EndContainer(),
02185 NWidget(NWID_VERTICAL),
02186 NWidget(NWID_VSCROLLBAR, COLOUR_DARK_GREEN, WID_JS_SCROLLBAR),
02187 NWidget(WWT_RESIZEBOX, COLOUR_DARK_GREEN),
02188 EndContainer(),
02189 EndContainer(),
02190 };
02191
02196 template <class T>
02197 struct SelectStationWindow : Window {
02198 CommandContainer select_station_cmd;
02199 TileArea area;
02200 Scrollbar *vscroll;
02201
02202 SelectStationWindow(const WindowDesc *desc, CommandContainer cmd, TileArea ta) :
02203 Window(),
02204 select_station_cmd(cmd),
02205 area(ta)
02206 {
02207 this->CreateNestedTree(desc);
02208 this->vscroll = this->GetScrollbar(WID_JS_SCROLLBAR);
02209 this->GetWidget<NWidgetCore>(WID_JS_CAPTION)->widget_data = T::EXPECTED_FACIL == FACIL_WAYPOINT ? STR_JOIN_WAYPOINT_CAPTION : STR_JOIN_STATION_CAPTION;
02210 this->FinishInitNested(desc, 0);
02211 this->OnInvalidateData(0);
02212 }
02213
02214 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
02215 {
02216 if (widget != WID_JS_PANEL) return;
02217
02218
02219 Dimension d = GetStringBoundingBox(T::EXPECTED_FACIL == FACIL_WAYPOINT ? STR_JOIN_WAYPOINT_CREATE_SPLITTED_WAYPOINT : STR_JOIN_STATION_CREATE_SPLITTED_STATION);
02220 for (uint i = 0; i < _stations_nearby_list.Length(); i++) {
02221 const T *st = T::Get(_stations_nearby_list[i]);
02222 SetDParam(0, st->index);
02223 SetDParam(1, st->facilities);
02224 d = maxdim(d, GetStringBoundingBox(T::EXPECTED_FACIL == FACIL_WAYPOINT ? STR_STATION_LIST_WAYPOINT : STR_STATION_LIST_STATION));
02225 }
02226
02227 resize->height = d.height;
02228 d.height *= 5;
02229 d.width += WD_FRAMERECT_RIGHT + WD_FRAMERECT_LEFT;
02230 d.height += WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
02231 *size = d;
02232 }
02233
02234 virtual void DrawWidget(const Rect &r, int widget) const
02235 {
02236 if (widget != WID_JS_PANEL) return;
02237
02238 uint y = r.top + WD_FRAMERECT_TOP;
02239 if (this->vscroll->GetPosition() == 0) {
02240 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);
02241 y += this->resize.step_height;
02242 }
02243
02244 for (uint i = max<uint>(1, this->vscroll->GetPosition()); i <= _stations_nearby_list.Length(); ++i, y += this->resize.step_height) {
02245
02246 if (i - this->vscroll->GetPosition() >= this->vscroll->GetCapacity()) break;
02247
02248 const T *st = T::Get(_stations_nearby_list[i - 1]);
02249 SetDParam(0, st->index);
02250 SetDParam(1, st->facilities);
02251 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);
02252 }
02253 }
02254
02255 virtual void OnClick(Point pt, int widget, int click_count)
02256 {
02257 if (widget != WID_JS_PANEL) return;
02258
02259 uint st_index = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_JS_PANEL, WD_FRAMERECT_TOP);
02260 bool distant_join = (st_index > 0);
02261 if (distant_join) st_index--;
02262
02263 if (distant_join && st_index >= _stations_nearby_list.Length()) return;
02264
02265
02266 SB(this->select_station_cmd.p2, 16, 16,
02267 (distant_join ? _stations_nearby_list[st_index] : NEW_STATION));
02268
02269
02270 DoCommandP(&this->select_station_cmd);
02271
02272
02273 DeleteWindowById(WC_SELECT_STATION, 0);
02274 }
02275
02276 virtual void OnTick()
02277 {
02278 if (_thd.dirty & 2) {
02279 _thd.dirty &= ~2;
02280 this->SetDirty();
02281 }
02282 }
02283
02284 virtual void OnResize()
02285 {
02286 this->vscroll->SetCapacityFromWidget(this, WID_JS_PANEL, WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM);
02287 }
02288
02294 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
02295 {
02296 if (!gui_scope) return;
02297 FindStationsNearby<T>(this->area, true);
02298 this->vscroll->SetCount(_stations_nearby_list.Length() + 1);
02299 this->SetDirty();
02300 }
02301 };
02302
02303 static const WindowDesc _select_station_desc(
02304 WDP_AUTO, 200, 180,
02305 WC_SELECT_STATION, WC_NONE,
02306 WDF_CONSTRUCTION,
02307 _nested_select_station_widgets, lengthof(_nested_select_station_widgets)
02308 );
02309
02310
02318 template <class T>
02319 static bool StationJoinerNeeded(CommandContainer cmd, TileArea ta)
02320 {
02321
02322 if (!_settings_game.station.distant_join_stations) return false;
02323
02324
02325
02326 Window *selection_window = FindWindowById(WC_SELECT_STATION, 0);
02327 if (selection_window != NULL) {
02328
02329 delete selection_window;
02330 UpdateTileSelection();
02331 }
02332
02333
02334 if (!_ctrl_pressed) return false;
02335
02336
02337 if (DoCommand(&cmd, CommandFlagsToDCFlags(GetCommandFlags(cmd.cmd))).Failed()) return false;
02338
02339
02340
02341
02342 const T *st = FindStationsNearby<T>(ta, false);
02343 return st == NULL && (_settings_game.station.adjacent_stations || _stations_nearby_list.Length() == 0);
02344 }
02345
02352 template <class T>
02353 void ShowSelectBaseStationIfNeeded(CommandContainer cmd, TileArea ta)
02354 {
02355 if (StationJoinerNeeded<T>(cmd, ta)) {
02356 if (!_settings_client.gui.persistent_buildingtools) ResetObjectToPlace();
02357 new SelectStationWindow<T>(&_select_station_desc, cmd, ta);
02358 } else {
02359 DoCommandP(&cmd);
02360 }
02361 }
02362
02368 void ShowSelectStationIfNeeded(CommandContainer cmd, TileArea ta)
02369 {
02370 ShowSelectBaseStationIfNeeded<Station>(cmd, ta);
02371 }
02372
02378 void ShowSelectWaypointIfNeeded(CommandContainer cmd, TileArea ta)
02379 {
02380 ShowSelectBaseStationIfNeeded<Waypoint>(cmd, ta);
02381 }