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