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