# station_gui.cpp

Go to the documentation of this file.

    00001 /* $Id: station_gui.cpp 24419 2012-07-20 19:28:51Z rubidium $ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
    00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
    00008  */
    00009
    00012 #include "stdafx.h"
    00013 #include "debug.h"
    00014 #include "gui.h"
    00015 #include "textbuf_gui.h"
    00016 #include "company_func.h"
    00017 #include "command_func.h"
    00018 #include "vehicle_gui.h"
    00019 #include "cargotype.h"
    00020 #include "station_gui.h"
    00021 #include "strings_func.h"
    00022 #include "window_func.h"
    00023 #include "viewport_func.h"
    00024 #include "widgets/dropdown_func.h"
    00025 #include "station_base.h"
    00026 #include "waypoint_base.h"
    00027 #include "tilehighlight_func.h"
    00028 #include "company_base.h"
    00029 #include "sortlist_type.h"
    00030 #include "core/geometry_func.hpp"
    00031 #include "vehiclelist.h"
    00032
    00033 #include "widgets/station_widget.h"
    00034
    00035 #include "table/strings.h"
    00036
    00047 int DrawStationCoverageAreaText(int left, int right, int top, StationCoverageType sct, int rad, bool supplies)
    00048 {
    00049   TileIndex tile = TileVirtXY(_thd.pos.x, _thd.pos.y);
    00050   uint32 cargo_mask = 0;
    00051   if (_thd.drawstyle == HT_RECT && tile < MapSize()) {
    00052     CargoArray cargoes;
    00053     if (supplies) {
    00054       cargoes = GetProductionAroundTiles(tile, _thd.size.x / TILE_SIZE, _thd.size.y / TILE_SIZE, rad);
    00055     } else {
    00056       cargoes = GetAcceptanceAroundTiles(tile, _thd.size.x / TILE_SIZE, _thd.size.y / TILE_SIZE, rad);
    00057     }
    00058
    00059     /* Convert cargo counts to a set of cargo bits, and draw the result. */
    00060     for (CargoID i = 0; i < NUM_CARGO; i++) {
    00061       switch (sct) {
    00062         case SCT_PASSENGERS_ONLY: if (!IsCargoInClass(i, CC_PASSENGERS)) continue; break;
    00063         case SCT_NON_PASSENGERS_ONLY: if (IsCargoInClass(i, CC_PASSENGERS)) continue; break;
    00064         case SCT_ALL: break;
    00065         default: NOT_REACHED();
    00066       }
    00067       if (cargoes[i] >= (supplies ? 1U : 8U)) SetBit(cargo_mask, i);
    00068     }
    00069   }
    00070   SetDParam(0, cargo_mask);
    00071   return DrawStringMultiLine(left, right, top, INT32_MAX, supplies ? STR_STATION_BUILD_SUPPLIES_CARGO : STR_STATION_BUILD_ACCEPTS_CARGO);
    00072 }
    00073
    00079 void CheckRedrawStationCoverage(const Window *w)
    00080 {
    00081   if (_thd.dirty & 1) {
    00082     _thd.dirty &= ~1;
    00083     w->SetDirty();
    00084   }
    00085 }
    00086
    00102 static void StationsWndShowStationRating(int left, int right, int y, CargoID type, uint amount, byte rating)
    00103 {
    00104   static const uint units_full  = 576;
    00105   static const uint rating_full = 224;
    00106
    00107   const CargoSpec *cs = CargoSpec::Get(type);
    00108   if (!cs->IsValid()) return;
    00109
    00110   int colour = cs->rating_colour;
    00111   uint w = (minu(amount, units_full) + 5) / 36;
    00112
    00113   int height = GetCharacterHeight(FS_SMALL);
    00114
    00115   /* Draw total cargo (limited) on station (fits into 16 pixels) */
    00116   if (w != 0) GfxFillRect(left, y, left + w - 1, y + height, colour);
    00117
    00118   /* Draw a one pixel-wide bar of additional cargo meter, useful
    00119    * for stations with only a small amount (<=30) */
    00120   if (w == 0) {
    00121     uint rest = amount / 5;
    00122     if (rest != 0) {
    00123       w += left;
    00124       GfxFillRect(w, y + height - rest, w, y + height, colour);
    00125     }
    00126   }
    00127
    00128   DrawString(left + 1, right, y, cs->abbrev, TC_BLACK);
    00129
    00130   /* Draw green/red ratings bar (fits into 14 pixels) */
    00131   y += height + 2;
    00132   GfxFillRect(left + 1, y, left + 14, y, PC_RED);
    00133   rating = minu(rating, rating_full) / 16;
    00134   if (rating != 0) GfxFillRect(left + 1, y, left + rating, y, PC_GREEN);
    00135 }
    00136
    00137 typedef GUIList<const Station*> GUIStationList;
    00138
    00142 class CompanyStationsWindow : public Window
    00143 {
    00144 protected:
    00145   /* Runtime saved values */
    00146   static Listing last_sorting;
    00147   static byte facilities;               // types of stations of interest
    00148   static bool include_empty;            // whether we should include stations without waiting cargo
    00149   static const uint32 cargo_filter_max;
    00150   static uint32 cargo_filter;           // bitmap of cargo types to include
    00151   static const Station *last_station;
    00152
    00153   /* Constants for sorting stations */
    00154   static const StringID sorter_names[];
    00155   static GUIStationList::SortFunction * const sorter_funcs[];
    00156
    00157   GUIStationList stations;
    00158   Scrollbar *vscroll;
    00159
    00165   void BuildStationsList(const Owner owner)
    00166   {
    00167     if (!this->stations.NeedRebuild()) return;
    00168
    00169     DEBUG(misc, 3, "Building station list for company %d", owner);
    00170
    00171     this->stations.Clear();
    00172
    00173     const Station *st;
    00174     FOR_ALL_STATIONS(st) {
    00175       if (st->owner == owner || (st->owner == OWNER_NONE && HasStationInUse(st->index, true, owner))) {
    00176         if (this->facilities & st->facilities) { // only stations with selected facilities
    00177           int num_waiting_cargo = 0;
    00178           for (CargoID j = 0; j < NUM_CARGO; j++) {
    00179             if (HasBit(st->goods[j].acceptance_pickup, GoodsEntry::GES_PICKUP)) {
    00180               num_waiting_cargo++; // count number of waiting cargo
    00181               if (HasBit(this->cargo_filter, j)) {
    00182                 *this->stations.Append() = st;
    00183                 break;
    00184               }
    00185             }
    00186           }
    00187           /* stations without waiting cargo */
    00188           if (num_waiting_cargo == 0 && this->include_empty) {
    00189             *this->stations.Append() = st;
    00190           }
    00191         }
    00192       }
    00193     }
    00194
    00195     this->stations.Compact();
    00196     this->stations.RebuildDone();
    00197
    00198     this->vscroll->SetCount(this->stations.Length()); // Update the scrollbar
    00199   }
    00200
    00202   static int CDECL StationNameSorter(const Station * const *a, const Station * const *b)
    00203   {
    00204     static char buf_cache[64];
    00205     char buf[64];
    00206
    00207     SetDParam(0, (*a)->index);
    00208     GetString(buf, STR_STATION_NAME, lastof(buf));
    00209
    00210     if (*b != last_station) {
    00211       last_station = *b;
    00212       SetDParam(0, (*b)->index);
    00213       GetString(buf_cache, STR_STATION_NAME, lastof(buf_cache));
    00214     }
    00215
    00216     return strcmp(buf, buf_cache);
    00217   }
    00218
    00220   static int CDECL StationTypeSorter(const Station * const *a, const Station * const *b)
    00221   {
    00222     return (*a)->facilities - (*b)->facilities;
    00223   }
    00224
    00226   static int CDECL StationWaitingSorter(const Station * const *a, const Station * const *b)
    00227   {
    00228     Money diff = 0;
    00229
    00230     CargoID j;
    00231     FOR_EACH_SET_CARGO_ID(j, cargo_filter) {
    00232       if (!(*a)->goods[j].cargo.Empty()) diff += GetTransportedGoodsIncome((*a)->goods[j].cargo.Count(), 20, 50, j);
    00233       if (!(*b)->goods[j].cargo.Empty()) diff -= GetTransportedGoodsIncome((*b)->goods[j].cargo.Count(), 20, 50, j);
    00234     }
    00235
    00236     return ClampToI32(diff);
    00237   }
    00238
    00240   static int CDECL StationRatingMaxSorter(const Station * const *a, const Station * const *b)
    00241   {
    00242     byte maxr1 = 0;
    00243     byte maxr2 = 0;
    00244
    00245     CargoID j;
    00246     FOR_EACH_SET_CARGO_ID(j, cargo_filter) {
    00247       if (HasBit((*a)->goods[j].acceptance_pickup, GoodsEntry::GES_PICKUP)) maxr1 = max(maxr1, (*a)->goods[j].rating);
    00248       if (HasBit((*b)->goods[j].acceptance_pickup, GoodsEntry::GES_PICKUP)) maxr2 = max(maxr2, (*b)->goods[j].rating);
    00249     }
    00250
    00251     return maxr1 - maxr2;
    00252   }
    00253
    00255   static int CDECL StationRatingMinSorter(const Station * const *a, const Station * const *b)
    00256   {
    00257     byte minr1 = 255;
    00258     byte minr2 = 255;
    00259
    00260     for (CargoID j = 0; j < NUM_CARGO; j++) {
    00261       if (!HasBit(cargo_filter, j)) continue;
    00262       if (HasBit((*a)->goods[j].acceptance_pickup, GoodsEntry::GES_PICKUP)) minr1 = min(minr1, (*a)->goods[j].rating);
    00263       if (HasBit((*b)->goods[j].acceptance_pickup, GoodsEntry::GES_PICKUP)) minr2 = min(minr2, (*b)->goods[j].rating);
    00264     }
    00265
    00266     return -(minr1 - minr2);
    00267   }
    00268
    00270   void SortStationsList()
    00271   {
    00272     if (!this->stations.Sort()) return;
    00273
    00274     /* Reset name sorter sort cache */
    00275     this->last_station = NULL;
    00276
    00277     /* Set the modified widget dirty */
    00278     this->SetWidgetDirty(WID_STL_LIST);
    00279   }
    00280
    00281 public:
    00282   CompanyStationsWindow(const WindowDesc *desc, WindowNumber window_number) : Window()
    00283   {
    00284     this->stations.SetListing(this->last_sorting);
    00285     this->stations.SetSortFuncs(this->sorter_funcs);
    00286     this->stations.ForceRebuild();
    00287     this->stations.NeedResort();
    00288     this->SortStationsList();
    00289
    00290     this->CreateNestedTree(desc);
    00291     this->vscroll = this->GetScrollbar(WID_STL_SCROLLBAR);
    00292     this->FinishInitNested(desc, window_number);
    00293     this->owner = (Owner)this->window_number;
    00294
    00295     CargoID cid;
    00296     FOR_EACH_SET_CARGO_ID(cid, this->cargo_filter) {
    00297       if (CargoSpec::Get(cid)->IsValid()) this->LowerWidget(WID_STL_CARGOSTART + cid);
    00298     }
    00299
    00300     if (this->cargo_filter == this->cargo_filter_max) this->cargo_filter = _cargo_mask;
    00301
    00302     for (uint i = 0; i < 5; i++) {
    00303       if (HasBit(this->facilities, i)) this->LowerWidget(i + WID_STL_TRAIN);
    00304     }
    00305     this->SetWidgetLoweredState(WID_STL_NOCARGOWAITING, this->include_empty);
    00306
    00307     this->GetWidget<NWidgetCore>(WID_STL_SORTDROPBTN)->widget_data = this->sorter_names[this->stations.SortType()];
    00308   }
    00309
    00310   ~CompanyStationsWindow()
    00311   {
    00312     this->last_sorting = this->stations.GetListing();
    00313   }
    00314
    00315   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
    00316   {
    00317     switch (widget) {
    00318       case WID_STL_SORTBY: {
    00319         Dimension d = GetStringBoundingBox(this->GetWidget<NWidgetCore>(widget)->widget_data);
    00320         d.width += padding.width + WD_SORTBUTTON_ARROW_WIDTH * 2; // Doubled since the string is centred and it also looks better.
    00321         d.height += padding.height;
    00322         *size = maxdim(*size, d);
    00323         break;
    00324       }
    00325
    00326       case WID_STL_SORTDROPBTN: {
    00327         Dimension d = {0, 0};
    00328         for (int i = 0; this->sorter_names[i] != INVALID_STRING_ID; i++) {
    00329           d = maxdim(d, GetStringBoundingBox(this->sorter_names[i]));
    00330         }
    00331         d.width += padding.width;
    00332         d.height += padding.height;
    00333         *size = maxdim(*size, d);
    00334         break;
    00335       }
    00336
    00337       case WID_STL_LIST:
    00338         resize->height = FONT_HEIGHT_NORMAL;
    00339         size->height = WD_FRAMERECT_TOP + 5 * resize->height + WD_FRAMERECT_BOTTOM;
    00340         break;
    00341
    00342       case WID_STL_TRAIN:
    00343       case WID_STL_TRUCK:
    00344       case WID_STL_BUS:
    00345       case WID_STL_AIRPLANE:
    00346       case WID_STL_SHIP:
    00347         size->height = max<uint>(FONT_HEIGHT_SMALL, 10) + padding.height;
    00348         break;
    00349
    00350       case WID_STL_CARGOALL:
    00351       case WID_STL_FACILALL:
    00352       case WID_STL_NOCARGOWAITING: {
    00353         Dimension d = GetStringBoundingBox(widget == WID_STL_NOCARGOWAITING ? STR_ABBREV_NONE : STR_ABBREV_ALL);
    00354         d.width  += padding.width + 2;
    00355         d.height += padding.height;
    00356         *size = maxdim(*size, d);
    00357         break;
    00358       }
    00359
    00360       default:
    00361         if (widget >= WID_STL_CARGOSTART) {
    00362           const CargoSpec *cs = CargoSpec::Get(widget - WID_STL_CARGOSTART);
    00363           if (cs->IsValid()) {
    00364             Dimension d = GetStringBoundingBox(cs->abbrev);
    00365             d.width  += padding.width + 2;
    00366             d.height += padding.height;
    00367             *size = maxdim(*size, d);
    00368           }
    00369         }
    00370         break;
    00371     }
    00372   }
    00373
    00374   virtual void OnPaint()
    00375   {
    00376     this->BuildStationsList((Owner)this->window_number);
    00377     this->SortStationsList();
    00378
    00379     this->DrawWidgets();
    00380   }
    00381
    00382   virtual void DrawWidget(const Rect &r, int widget) const
    00383   {
    00384     switch (widget) {
    00385       case WID_STL_SORTBY:
    00386         /* draw arrow pointing up/down for ascending/descending sorting */
    00387         this->DrawSortButtonState(WID_STL_SORTBY, this->stations.IsDescSortOrder() ? SBS_DOWN : SBS_UP);
    00388         break;
    00389
    00390       case WID_STL_LIST: {
    00391         bool rtl = _current_text_dir == TD_RTL;
    00392         int max = min(this->vscroll->GetPosition() + this->vscroll->GetCapacity(), this->stations.Length());
    00393         int y = r.top + WD_FRAMERECT_TOP;
    00394         for (int i = this->vscroll->GetPosition(); i < max; ++i) { // do until max number of stations of owner
    00395           const Station *st = this->stations[i];
    00396           assert(st->xy != INVALID_TILE);
    00397
    00398           /* Do not do the complex check HasStationInUse here, it may be even false
    00399            * when the order had been removed and the station list hasn't been removed yet */
    00400           assert(st->owner == owner || st->owner == OWNER_NONE);
    00401
    00402           SetDParam(0, st->index);
    00403           SetDParam(1, st->facilities);
    00404           int x = DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_STATION_LIST_STATION);
    00405           x += rtl ? -5 : 5;
    00406
    00407           /* show cargo waiting and station ratings */
    00408           for (CargoID j = 0; j < NUM_CARGO; j++) {
    00409             if (!st->goods[j].cargo.Empty()) {
    00410               /* For RTL we work in exactly the opposite direction. So
    00411                * decrement the space needed first, then draw to the left
    00412                * instead of drawing to the left and then incrementing
    00413                * the space. */
    00414               if (rtl) {
    00415                 x -= 20;
    00416                 if (x < r.left + WD_FRAMERECT_LEFT) break;
    00417               }
    00418               StationsWndShowStationRating(x, x + 16, y, j, st->goods[j].cargo.Count(), st->goods[j].rating);
    00419               if (!rtl) {
    00420                 x += 20;
    00421                 if (x > r.right - WD_FRAMERECT_RIGHT) break;
    00422               }
    00423             }
    00424           }
    00425           y += FONT_HEIGHT_NORMAL;
    00426         }
    00427
    00428         if (this->vscroll->GetCount() == 0) { // company has no stations
    00429           DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_STATION_LIST_NONE);
    00430           return;
    00431         }
    00432         break;
    00433       }
    00434
    00435       case WID_STL_NOCARGOWAITING: {
    00436         int cg_ofst = this->IsWidgetLowered(widget) ? 2 : 1;
    00437         DrawString(r.left + cg_ofst, r.right + cg_ofst, r.top + cg_ofst, STR_ABBREV_NONE, TC_BLACK, SA_HOR_CENTER);
    00438         break;
    00439       }
    00440
    00441       case WID_STL_CARGOALL: {
    00442         int cg_ofst = this->IsWidgetLowered(widget) ? 2 : 1;
    00443         DrawString(r.left + cg_ofst, r.right + cg_ofst, r.top + cg_ofst, STR_ABBREV_ALL, TC_BLACK, SA_HOR_CENTER);
    00444         break;
    00445       }
    00446
    00447       case WID_STL_FACILALL: {
    00448         int cg_ofst = this->IsWidgetLowered(widget) ? 2 : 1;
    00449         DrawString(r.left + cg_ofst, r.right + cg_ofst, r.top + cg_ofst, STR_ABBREV_ALL, TC_BLACK);
    00450         break;
    00451       }
    00452
    00453       default:
    00454         if (widget >= WID_STL_CARGOSTART) {
    00455           const CargoSpec *cs = CargoSpec::Get(widget - WID_STL_CARGOSTART);
    00456           if (cs->IsValid()) {
    00457             int cg_ofst = HasBit(this->cargo_filter, cs->Index()) ? 2 : 1;
    00458             GfxFillRect(r.left + cg_ofst, r.top + cg_ofst, r.right - 2 + cg_ofst, r.bottom - 2 + cg_ofst, cs->rating_colour);
    00459             DrawString(r.left + cg_ofst, r.right + cg_ofst, r.top + cg_ofst, cs->abbrev, TC_BLACK, SA_HOR_CENTER);
    00460           }
    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; // click out of list bound
    00480
    00481         const Station *st = this->stations[id_v];
    00482         /* do not check HasStationInUse - it is slow and may be invalid */
    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 < NUM_CARGO; i++) {
    00525           const CargoSpec *cs = CargoSpec::Get(i);
    00526           if (cs->IsValid()) this->LowerWidget(WID_STL_CARGOSTART + i);
    00527         }
    00528         this->LowerWidget(WID_STL_NOCARGOWAITING);
    00529
    00530         this->cargo_filter = _cargo_mask;
    00531         this->include_empty = true;
    00532         this->stations.ForceRebuild();
    00533         this->SetDirty();
    00534         break;
    00535       }
    00536
    00537       case WID_STL_SORTBY: // flip sorting method asc/desc
    00538         this->stations.ToggleSortOrder();
    00539         this->SetTimeout();
    00540         this->LowerWidget(WID_STL_SORTBY);
    00541         this->SetDirty();
    00542         break;
    00543
    00544       case WID_STL_SORTDROPBTN: // select sorting criteria dropdown menu
    00545         ShowDropDownMenu(this, this->sorter_names, this->stations.SortType(), WID_STL_SORTDROPBTN, 0, 0);
    00546         break;
    00547
    00548       case WID_STL_NOCARGOWAITING:
    00549         if (_ctrl_pressed) {
    00550           this->include_empty = !this->include_empty;
    00551           this->ToggleWidgetLoweredState(WID_STL_NOCARGOWAITING);
    00552         } else {
    00553           for (uint i = 0; i < NUM_CARGO; i++) {
    00554             const CargoSpec *cs = CargoSpec::Get(i);
    00555             if (cs->IsValid()) this->RaiseWidget(WID_STL_CARGOSTART + i);
    00556           }
    00557
    00558           this->cargo_filter = 0;
    00559           this->include_empty = true;
    00560
    00561           this->LowerWidget(WID_STL_NOCARGOWAITING);
    00562         }
    00563         this->stations.ForceRebuild();
    00564         this->SetDirty();
    00565         break;
    00566
    00567       default:
    00568         if (widget >= WID_STL_CARGOSTART) { // change cargo_filter
    00569           /* Determine the selected cargo type */
    00570           const CargoSpec *cs = CargoSpec::Get(widget - WID_STL_CARGOSTART);
    00571           if (!cs->IsValid()) break;
    00572
    00573           if (_ctrl_pressed) {
    00574             ToggleBit(this->cargo_filter, cs->Index());
    00575             this->ToggleWidgetLoweredState(widget);
    00576           } else {
    00577             for (uint i = 0; i < NUM_CARGO; i++) {
    00578               const CargoSpec *cs = CargoSpec::Get(i);
    00579               if (cs->IsValid()) this->RaiseWidget(WID_STL_CARGOSTART + i);
    00580             }
    00581             this->RaiseWidget(WID_STL_NOCARGOWAITING);
    00582
    00583             this->cargo_filter = 0;
    00584             this->include_empty = false;
    00585
    00586             SetBit(this->cargo_filter, cs->Index());
    00587             this->LowerWidget(widget);
    00588           }
    00589           this->stations.ForceRebuild();
    00590           this->SetDirty();
    00591         }
    00592         break;
    00593     }
    00594   }
    00595
    00596   virtual void OnDropdownSelect(int widget, int index)
    00597   {
    00598     if (this->stations.SortType() != index) {
    00599       this->stations.SetSortType(index);
    00600
    00601       /* Display the current sort variant */
    00602       this->GetWidget<NWidgetCore>(WID_STL_SORTDROPBTN)->widget_data = this->sorter_names[this->stations.SortType()];
    00603
    00604       this->SetDirty();
    00605     }
    00606   }
    00607
    00608   virtual void OnTick()
    00609   {
    00610     if (_pause_mode != PM_UNPAUSED) return;
    00611     if (this->stations.NeedResort()) {
    00612       DEBUG(misc, 3, "Periodic rebuild station list company %d", this->window_number);
    00613       this->SetDirty();
    00614     }
    00615   }
    00616
    00617   virtual void OnTimeout()
    00618   {
    00619     this->RaiseWidget(WID_STL_SORTBY);
    00620     this->SetDirty();
    00621   }
    00622
    00623   virtual void OnResize()
    00624   {
    00625     this->vscroll->SetCapacityFromWidget(this, WID_STL_LIST, WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM);
    00626   }
    00627
    00633   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
    00634   {
    00635     if (data == 0) {
    00636       /* This needs to be done in command-scope to enforce rebuilding before resorting invalid data */
    00637       this->stations.ForceRebuild();
    00638     } else {
    00639       this->stations.ForceResort();
    00640     }
    00641   }
    00642 };
    00643
    00644 Listing CompanyStationsWindow::last_sorting = {false, 0};
    00645 byte CompanyStationsWindow::facilities = FACIL_TRAIN | FACIL_TRUCK_STOP | FACIL_BUS_STOP | FACIL_AIRPORT | FACIL_DOCK;
    00646 bool CompanyStationsWindow::include_empty = true;
    00647 const uint32 CompanyStationsWindow::cargo_filter_max = UINT32_MAX;
    00648 uint32 CompanyStationsWindow::cargo_filter = UINT32_MAX;
    00649 const Station *CompanyStationsWindow::last_station = NULL;
    00650
    00651 /* Availible station sorting functions */
    00652 GUIStationList::SortFunction * const CompanyStationsWindow::sorter_funcs[] = {
    00653   &StationNameSorter,
    00654   &StationTypeSorter,
    00655   &StationWaitingSorter,
    00656   &StationRatingMaxSorter,
    00657   &StationRatingMinSorter
    00658 };
    00659
    00660 /* Names of the sorting functions */
    00661 const StringID CompanyStationsWindow::sorter_names[] = {
    00662   STR_SORT_BY_NAME,
    00663   STR_SORT_BY_FACILITY,
    00664   STR_SORT_BY_WAITING,
    00665   STR_SORT_BY_RATING_MAX,
    00666   STR_SORT_BY_RATING_MIN,
    00667   INVALID_STRING_ID
    00668 };
    00669
    00675 static NWidgetBase *CargoWidgets(int *biggest_index)
    00676 {
    00677   NWidgetHorizontal *container = new NWidgetHorizontal();
    00678
    00679   for (uint i = 0; i < NUM_CARGO; i++) {
    00680     const CargoSpec *cs = CargoSpec::Get(i);
    00681     if (cs->IsValid()) {
    00682       NWidgetBackground *panel = new NWidgetBackground(WWT_PANEL, COLOUR_GREY, WID_STL_CARGOSTART + i);
    00683       panel->SetMinimalSize(14, 11);
    00684       panel->SetResize(0, 0);
    00685       panel->SetFill(0, 1);
    00686       panel->SetDataTip(0, STR_STATION_LIST_USE_CTRL_TO_SELECT_MORE);
    00687       container->Add(panel);
    00688     } else {
    00689       NWidgetLeaf *nwi = new NWidgetLeaf(WWT_EMPTY, COLOUR_GREY, WID_STL_CARGOSTART + i, 0x0, STR_NULL);
    00690       nwi->SetMinimalSize(0, 11);
    00691       nwi->SetResize(0, 0);
    00692       nwi->SetFill(0, 1);
    00693       container->Add(nwi);
    00694     }
    00695   }
    00696   *biggest_index = WID_STL_CARGOSTART + NUM_CARGO;
    00697   return container;
    00698 }
    00699
    00700 static const NWidgetPart _nested_company_stations_widgets[] = {
    00701   NWidget(NWID_HORIZONTAL),
    00702     NWidget(WWT_CLOSEBOX, COLOUR_GREY),
    00703     NWidget(WWT_CAPTION, COLOUR_GREY, WID_STL_CAPTION), SetDataTip(STR_STATION_LIST_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    00704     NWidget(WWT_SHADEBOX, COLOUR_GREY),
    00705     NWidget(WWT_STICKYBOX, COLOUR_GREY),
    00706   EndContainer(),
    00707   NWidget(NWID_HORIZONTAL),
    00708     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),
    00709     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),
    00710     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),
    00711     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),
    00712     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),
    00713     NWidget(WWT_PUSHBTN, COLOUR_GREY, WID_STL_FACILALL), SetMinimalSize(14, 11), SetDataTip(0x0, STR_STATION_LIST_SELECT_ALL_FACILITIES), SetFill(0, 1),
    00714     NWidget(WWT_PANEL, COLOUR_GREY), SetMinimalSize(5, 11), SetFill(0, 1), EndContainer(),
    00715     NWidgetFunction(CargoWidgets),
    00716     NWidget(WWT_PANEL, COLOUR_GREY, WID_STL_NOCARGOWAITING), SetMinimalSize(14, 11), SetDataTip(0x0, STR_STATION_LIST_NO_WAITING_CARGO), SetFill(0, 1), EndContainer(),
    00717     NWidget(WWT_PUSHBTN, COLOUR_GREY, WID_STL_CARGOALL), SetMinimalSize(14, 11), SetDataTip(0x0, STR_STATION_LIST_SELECT_ALL_TYPES), SetFill(0, 1),
    00718     NWidget(WWT_PANEL, COLOUR_GREY), SetDataTip(0x0, STR_NULL), SetResize(1, 0), SetFill(1, 1), EndContainer(),
    00719   EndContainer(),
    00720   NWidget(NWID_HORIZONTAL),
    00721     NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_STL_SORTBY), SetMinimalSize(81, 12), SetDataTip(STR_BUTTON_SORT_BY, STR_TOOLTIP_SORT_ORDER),
    00722     NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_STL_SORTDROPBTN), SetMinimalSize(163, 12), SetDataTip(STR_SORT_BY_NAME, STR_TOOLTIP_SORT_CRITERIA), // widget_data gets overwritten.
    00723     NWidget(WWT_PANEL, COLOUR_GREY), SetDataTip(0x0, STR_NULL), SetResize(1, 0), SetFill(1, 1), EndContainer(),
    00724   EndContainer(),
    00725   NWidget(NWID_HORIZONTAL),
    00726     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(),
    00727     NWidget(NWID_VERTICAL),
    00728       NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_STL_SCROLLBAR),
    00729       NWidget(WWT_RESIZEBOX, COLOUR_GREY),
    00730     EndContainer(),
    00731   EndContainer(),
    00732 };
    00733
    00734 static const WindowDesc _company_stations_desc(
    00735   WDP_AUTO, 358, 162,
    00736   WC_STATION_LIST, WC_NONE,
    00737   WDF_UNCLICK_BUTTONS,
    00738   _nested_company_stations_widgets, lengthof(_nested_company_stations_widgets)
    00739 );
    00740
    00746 void ShowCompanyStations(CompanyID company)
    00747 {
    00748   if (!Company::IsValidID(company)) return;
    00749
    00750   AllocateWindowDescFront<CompanyStationsWindow>(&_company_stations_desc, company);
    00751 }
    00752
    00753 static const NWidgetPart _nested_station_view_widgets[] = {
    00754   NWidget(NWID_HORIZONTAL),
    00755     NWidget(WWT_CLOSEBOX, COLOUR_GREY),
    00756     NWidget(WWT_CAPTION, COLOUR_GREY, WID_SV_CAPTION), SetDataTip(STR_STATION_VIEW_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    00757     NWidget(WWT_SHADEBOX, COLOUR_GREY),
    00758     NWidget(WWT_STICKYBOX, COLOUR_GREY),
    00759   EndContainer(),
    00760   NWidget(NWID_HORIZONTAL),
    00761     NWidget(WWT_PANEL, COLOUR_GREY, WID_SV_WAITING), SetMinimalSize(237, 52), SetResize(1, 10), SetScrollbar(WID_SV_SCROLLBAR), EndContainer(),
    00762     NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_SV_SCROLLBAR),
    00763   EndContainer(),
    00764   NWidget(WWT_PANEL, COLOUR_GREY, WID_SV_ACCEPT_RATING_LIST), SetMinimalSize(249, 32), SetResize(1, 0), EndContainer(),
    00765   NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
    00766     NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SV_LOCATION), SetMinimalSize(60, 12), SetResize(1, 0), SetFill(1, 1),
    00767         SetDataTip(STR_BUTTON_LOCATION, STR_STATION_VIEW_CENTER_TOOLTIP),
    00768     NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SV_ACCEPTS_RATINGS), SetMinimalSize(61, 12), SetResize(1, 0), SetFill(1, 1),
    00769         SetDataTip(STR_STATION_VIEW_RATINGS_BUTTON, STR_STATION_VIEW_RATINGS_TOOLTIP),
    00770     NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SV_RENAME), SetMinimalSize(60, 12), SetResize(1, 0), SetFill(1, 1),
    00771         SetDataTip(STR_BUTTON_RENAME, STR_STATION_VIEW_RENAME_TOOLTIP),
    00772     NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SV_TRAINS), SetMinimalSize(14, 12), SetFill(0, 1), SetDataTip(STR_TRAIN, STR_STATION_VIEW_SCHEDULED_TRAINS_TOOLTIP),
    00773     NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SV_ROADVEHS), SetMinimalSize(14, 12), SetFill(0, 1), SetDataTip(STR_LORRY, STR_STATION_VIEW_SCHEDULED_ROAD_VEHICLES_TOOLTIP),
    00774     NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SV_SHIPS), SetMinimalSize(14, 12), SetFill(0, 1), SetDataTip(STR_SHIP, STR_STATION_VIEW_SCHEDULED_SHIPS_TOOLTIP),
    00775     NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SV_PLANES),  SetMinimalSize(14, 12), SetFill(0, 1), SetDataTip(STR_PLANE, STR_STATION_VIEW_SCHEDULED_AIRCRAFT_TOOLTIP),
    00776     NWidget(WWT_RESIZEBOX, COLOUR_GREY),
    00777   EndContainer(),
    00778 };
    00779
    00790 static void DrawCargoIcons(CargoID i, uint waiting, int left, int right, int y)
    00791 {
    00792   uint num = min((waiting + 5) / 10, (right - left) / 10); // maximum is width / 10 icons so it won't overflow
    00793   if (num == 0) return;
    00794
    00795   SpriteID sprite = CargoSpec::Get(i)->GetCargoIcon();
    00796
    00797   int x = _current_text_dir == TD_RTL ? right - num * 10 : left;
    00798   do {
    00799     DrawSprite(sprite, PAL_NONE, x, y);
    00800     x += 10;
    00801   } while (--num);
    00802 }
    00803
    00804 struct CargoData {
    00805   CargoID cargo;
    00806   StationID source;
    00807   uint count;
    00808
    00809   CargoData(CargoID cargo, StationID source, uint count) :
    00810     cargo(cargo),
    00811     source(source),
    00812     count(count)
    00813   { }
    00814 };
    00815
    00816 typedef std::list<CargoData> CargoDataList;
    00817
    00821 struct StationViewWindow : public Window {
    00822   uint32 cargo;
    00823   uint16 cargo_rows[NUM_CARGO];
    00824   uint expand_shrink_width;
    00825   int rating_lines;
    00826   int accepts_lines;
    00827   Scrollbar *vscroll;
    00828
    00830   enum AcceptListHeight {
    00831     ALH_RATING  = 13,
    00832     ALH_ACCEPTS = 3,
    00833   };
    00834
    00835   StationViewWindow(const WindowDesc *desc, WindowNumber window_number) : Window()
    00836   {
    00837     this->rating_lines  = ALH_RATING;
    00838     this->accepts_lines = ALH_ACCEPTS;
    00839
    00840     this->CreateNestedTree(desc);
    00841     this->vscroll = this->GetScrollbar(WID_SV_SCROLLBAR);
    00842     /* Nested widget tree creation is done in two steps to ensure that this->GetWidget<NWidgetCore>(WID_SV_ACCEPTS_RATINGS) exists in UpdateWidgetSize(). */
    00843     this->FinishInitNested(desc, window_number);
    00844
    00845     Owner owner = Station::Get(window_number)->owner;
    00846     if (owner != OWNER_NONE) this->owner = owner;
    00847   }
    00848
    00849   ~StationViewWindow()
    00850   {
    00851     Owner owner = Station::Get(this->window_number)->owner;
    00852     DeleteWindowById(WC_TRAINS_LIST,   VehicleListIdentifier(VL_STATION_LIST, VEH_TRAIN,    owner, this->window_number).Pack(), false);
    00853     DeleteWindowById(WC_ROADVEH_LIST,  VehicleListIdentifier(VL_STATION_LIST, VEH_ROAD,     owner, this->window_number).Pack(), false);
    00854     DeleteWindowById(WC_SHIPS_LIST,    VehicleListIdentifier(VL_STATION_LIST, VEH_SHIP,     owner, this->window_number).Pack(), false);
    00855     DeleteWindowById(WC_AIRCRAFT_LIST, VehicleListIdentifier(VL_STATION_LIST, VEH_AIRCRAFT, owner, this->window_number).Pack(), false);
    00856   }
    00857
    00858   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
    00859   {
    00860     switch (widget) {
    00861       case WID_SV_WAITING:
    00862         resize->height = FONT_HEIGHT_NORMAL;
    00863         size->height = WD_FRAMERECT_TOP + 5 * resize->height + WD_FRAMERECT_BOTTOM;
    00864         this->expand_shrink_width = max(GetStringBoundingBox("-").width, GetStringBoundingBox("+").width) + WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
    00865         break;
    00866
    00867       case WID_SV_ACCEPT_RATING_LIST:
    00868         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;
    00869         break;
    00870     }
    00871   }
    00872
    00873   virtual void OnPaint()
    00874   {
    00875     CargoDataList cargolist;
    00876     uint32 transfers = 0;
    00877     this->OrderWaitingCargo(&cargolist, &transfers);
    00878
    00879     this->vscroll->SetCount((int)cargolist.size() + 1); // update scrollbar
    00880
    00881     /* disable some buttons */
    00882     const Station *st = Station::Get(this->window_number);
    00883     this->SetWidgetDisabledState(WID_SV_RENAME,   st->owner != _local_company);
    00884     this->SetWidgetDisabledState(WID_SV_TRAINS,   !(st->facilities & FACIL_TRAIN));
    00885     this->SetWidgetDisabledState(WID_SV_ROADVEHS, !(st->facilities & FACIL_TRUCK_STOP) && !(st->facilities & FACIL_BUS_STOP));
    00886     this->SetWidgetDisabledState(WID_SV_SHIPS,    !(st->facilities & FACIL_DOCK));
    00887     this->SetWidgetDisabledState(WID_SV_PLANES,   !(st->facilities & FACIL_AIRPORT));
    00888
    00889     this->DrawWidgets();
    00890
    00891     if (!this->IsShaded()) {
    00892       /* Draw 'accepted cargo' or 'cargo ratings'. */
    00893       const NWidgetBase *wid = this->GetWidget<NWidgetBase>(WID_SV_ACCEPT_RATING_LIST);
    00894       const Rect r = {wid->pos_x, wid->pos_y, wid->pos_x + wid->current_x - 1, wid->pos_y + wid->current_y - 1};
    00895       if (this->GetWidget<NWidgetCore>(WID_SV_ACCEPTS_RATINGS)->widget_data == STR_STATION_VIEW_RATINGS_BUTTON) {
    00896         int lines = this->DrawAcceptedCargo(r);
    00897         if (lines > this->accepts_lines) { // Resize the widget, and perform re-initialization of the window.
    00898           this->accepts_lines = lines;
    00899           this->ReInit();
    00900           return;
    00901         }
    00902       } else {
    00903         int lines = this->DrawCargoRatings(r);
    00904         if (lines > this->rating_lines) { // Resize the widget, and perform re-initialization of the window.
    00905           this->rating_lines = lines;
    00906           this->ReInit();
    00907           return;
    00908         }
    00909       }
    00910
    00911       /* Draw waiting cargo. */
    00912       NWidgetBase *nwi = this->GetWidget<NWidgetBase>(WID_SV_WAITING);
    00913       Rect waiting_rect = {nwi->pos_x, nwi->pos_y, nwi->pos_x + nwi->current_x - 1, nwi->pos_y + nwi->current_y - 1};
    00914       this->DrawWaitingCargo(waiting_rect, cargolist, transfers);
    00915     }
    00916   }
    00917
    00918   virtual void SetStringParameters(int widget) const
    00919   {
    00920     if (widget == WID_SV_CAPTION) {
    00921       const Station *st = Station::Get(this->window_number);
    00922       SetDParam(0, st->index);
    00923       SetDParam(1, st->facilities);
    00924     }
    00925   }
    00926
    00933   void OrderWaitingCargo(CargoDataList *cargolist, uint32 *transfers)
    00934   {
    00935     assert(cargolist->size() == 0);
    00936     *transfers = 0;
    00937
    00938     StationID station_id = this->window_number;
    00939     const Station *st = Station::Get(station_id);
    00940
    00941     /* count types of cargoes waiting in station */
    00942     for (CargoID i = 0; i < NUM_CARGO; i++) {
    00943       if (st->goods[i].cargo.Empty()) {
    00944         this->cargo_rows[i] = 0;
    00945       } else {
    00946         /* Add an entry for total amount of cargo of this type waiting. */
    00947         cargolist->push_back(CargoData(i, INVALID_STATION, st->goods[i].cargo.Count()));
    00948
    00949         /* Set the row for this cargo entry for the expand/hide button */
    00950         this->cargo_rows[i] = (uint16)cargolist->size();
    00951
    00952         /* Add an entry for each distinct cargo source. */
    00953         const StationCargoList::List *packets = st->goods[i].cargo.Packets();
    00954         for (StationCargoList::ConstIterator it(packets->begin()); it != packets->end(); it++) {
    00955           const CargoPacket *cp = *it;
    00956           if (cp->SourceStation() != station_id) {
    00957             bool added = false;
    00958
    00959             /* Enable the expand/hide button for this cargo type */
    00960             SetBit(*transfers, i);
    00961
    00962             /* Don't add cargo lines if not expanded */
    00963             if (!HasBit(this->cargo, i)) break;
    00964
    00965             /* Check if we already have this source in the list */
    00966             for (CargoDataList::iterator jt(cargolist->begin()); jt != cargolist->end(); jt++) {
    00967               CargoData *cd = &(*jt);
    00968               if (cd->cargo == i && cd->source == cp->SourceStation()) {
    00969                 cd->count += cp->Count();
    00970                 added = true;
    00971                 break;
    00972               }
    00973             }
    00974
    00975             if (!added) cargolist->push_back(CargoData(i, cp->SourceStation(), cp->Count()));
    00976           }
    00977         }
    00978       }
    00979     }
    00980   }
    00981
    00988   void DrawWaitingCargo(const Rect &r, const CargoDataList &cargolist, uint32 transfers) const
    00989   {
    00990     int y = r.top + WD_FRAMERECT_TOP;
    00991     int pos = this->vscroll->GetPosition();
    00992
    00993     const Station *st = Station::Get(this->window_number);
    00994     if (--pos < 0) {
    00995       StringID str = STR_JUST_NOTHING;
    00996       for (CargoID i = 0; i < NUM_CARGO; i++) {
    00997         if (!st->goods[i].cargo.Empty()) str = STR_EMPTY;
    00998       }
    00999       SetDParam(0, str);
    01000       DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_STATION_VIEW_WAITING_TITLE);
    01001       y += FONT_HEIGHT_NORMAL;
    01002     }
    01003
    01004     bool rtl = _current_text_dir == TD_RTL;
    01005     int text_left    = rtl ? r.left + this->expand_shrink_width : r.left + WD_FRAMERECT_LEFT;
    01006     int text_right   = rtl ? r.right - WD_FRAMERECT_LEFT : r.right - this->expand_shrink_width;
    01007     int shrink_left  = rtl ? r.left + WD_FRAMERECT_LEFT : r.right - this->expand_shrink_width + WD_FRAMERECT_LEFT;
    01008     int shrink_right = rtl ? r.left + this->expand_shrink_width - WD_FRAMERECT_RIGHT : r.right - WD_FRAMERECT_RIGHT;
    01009
    01010
    01011     int maxrows = this->vscroll->GetCapacity();
    01012     for (CargoDataList::const_iterator it = cargolist.begin(); it != cargolist.end() && pos > -maxrows; ++it) {
    01013       if (--pos < 0) {
    01014         const CargoData *cd = &(*it);
    01015         if (cd->source == INVALID_STATION) {
    01016           /* Heading */
    01017           DrawCargoIcons(cd->cargo, cd->count, r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y);
    01018           SetDParam(0, cd->cargo);
    01019           SetDParam(1, cd->count);
    01020           if (HasBit(transfers, cd->cargo)) {
    01021             /* This cargo has transfers waiting so show the expand or shrink 'button' */
    01022             const char *sym = HasBit(this->cargo, cd->cargo) ? "-" : "+";
    01023             DrawString(text_left, text_right, y, STR_STATION_VIEW_WAITING_CARGO, TC_FROMSTRING, SA_RIGHT);
    01024             DrawString(shrink_left, shrink_right, y, sym, TC_YELLOW, SA_RIGHT);
    01025           } else {
    01026             DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_STATION_VIEW_WAITING_CARGO, TC_FROMSTRING, SA_RIGHT);
    01027           }
    01028         } else {
    01029           SetDParam(0, cd->cargo);
    01030           SetDParam(1, cd->count);
    01031           SetDParam(2, cd->source);
    01032           DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_STATION_VIEW_EN_ROUTE_FROM, TC_FROMSTRING, SA_RIGHT);
    01033         }
    01034
    01035         y += FONT_HEIGHT_NORMAL;
    01036       }
    01037     }
    01038   }
    01039
    01045   int DrawAcceptedCargo(const Rect &r) const
    01046   {
    01047     const Station *st = Station::Get(this->window_number);
    01048
    01049     uint32 cargo_mask = 0;
    01050     for (CargoID i = 0; i < NUM_CARGO; i++) {
    01051       if (HasBit(st->goods[i].acceptance_pickup, GoodsEntry::GES_ACCEPTANCE)) SetBit(cargo_mask, i);
    01052     }
    01053     SetDParam(0, cargo_mask);
    01054     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);
    01055     return CeilDiv(bottom - r.top - WD_FRAMERECT_TOP, FONT_HEIGHT_NORMAL);
    01056   }
    01057
    01063   int DrawCargoRatings(const Rect &r) const
    01064   {
    01065     const Station *st = Station::Get(this->window_number);
    01066     int y = r.top + WD_FRAMERECT_TOP;
    01067
    01068     DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_STATION_VIEW_CARGO_RATINGS_TITLE);
    01069     y += FONT_HEIGHT_NORMAL;
    01070
    01071     const CargoSpec *cs;
    01072     FOR_ALL_SORTED_STANDARD_CARGOSPECS(cs) {
    01073       const GoodsEntry *ge = &st->goods[cs->Index()];
    01074       if (!HasBit(ge->acceptance_pickup, GoodsEntry::GES_PICKUP)) continue;
    01075
    01076       SetDParam(0, cs->name);
    01077       SetDParam(2, ToPercent8(ge->rating));
    01078       SetDParam(1, STR_CARGO_RATING_APPALLING + (ge->rating >> 5));
    01079       DrawString(r.left + WD_FRAMERECT_LEFT + 6, r.right - WD_FRAMERECT_RIGHT - 6, y, STR_STATION_VIEW_CARGO_RATING);
    01080       y += FONT_HEIGHT_NORMAL;
    01081     }
    01082     return CeilDiv(y - r.top - WD_FRAMERECT_TOP, FONT_HEIGHT_NORMAL);
    01083   }
    01084
    01085   void HandleCargoWaitingClick(int row)
    01086   {
    01087     if (row == 0) return;
    01088
    01089     for (CargoID c = 0; c < NUM_CARGO; c++) {
    01090       if (this->cargo_rows[c] == row) {
    01091         ToggleBit(this->cargo, c);
    01092         this->SetWidgetDirty(WID_SV_WAITING);
    01093         break;
    01094       }
    01095     }
    01096   }
    01097
    01098   virtual void OnClick(Point pt, int widget, int click_count)
    01099   {
    01100     switch (widget) {
    01101       case WID_SV_WAITING:
    01102         this->HandleCargoWaitingClick(this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_SV_WAITING, WD_FRAMERECT_TOP, FONT_HEIGHT_NORMAL));
    01103         break;
    01104
    01105       case WID_SV_LOCATION:
    01106         if (_ctrl_pressed) {
    01107           ShowExtraViewPortWindow(Station::Get(this->window_number)->xy);
    01108         } else {
    01109           ScrollMainWindowToTile(Station::Get(this->window_number)->xy);
    01110         }
    01111         break;
    01112
    01113       case WID_SV_ACCEPTS_RATINGS: {
    01114         /* Swap between 'accepts' and 'ratings' view. */
    01115         int height_change;
    01116         NWidgetCore *nwi = this->GetWidget<NWidgetCore>(WID_SV_ACCEPTS_RATINGS);
    01117         if (this->GetWidget<NWidgetCore>(WID_SV_ACCEPTS_RATINGS)->widget_data == STR_STATION_VIEW_RATINGS_BUTTON) {
    01118           nwi->SetDataTip(STR_STATION_VIEW_ACCEPTS_BUTTON, STR_STATION_VIEW_ACCEPTS_TOOLTIP); // Switch to accepts view.
    01119           height_change = this->rating_lines - this->accepts_lines;
    01120         } else {
    01121           nwi->SetDataTip(STR_STATION_VIEW_RATINGS_BUTTON, STR_STATION_VIEW_RATINGS_TOOLTIP); // Switch to ratings view.
    01122           height_change = this->accepts_lines - this->rating_lines;
    01123         }
    01124         this->ReInit(0, height_change * FONT_HEIGHT_NORMAL);
    01125         break;
    01126       }
    01127
    01128       case WID_SV_RENAME:
    01129         SetDParam(0, this->window_number);
    01130         ShowQueryString(STR_STATION_NAME, STR_STATION_VIEW_RENAME_STATION_CAPTION, MAX_LENGTH_STATION_NAME_CHARS,
    01131             this, CS_ALPHANUMERAL, QSF_ENABLE_DEFAULT | QSF_LEN_IN_CHARS);
    01132         break;
    01133
    01134       case WID_SV_TRAINS:   // Show list of scheduled trains to this station
    01135       case WID_SV_ROADVEHS: // Show list of scheduled road-vehicles to this station
    01136       case WID_SV_SHIPS:    // Show list of scheduled ships to this station
    01137       case WID_SV_PLANES: { // Show list of scheduled aircraft to this station
    01138         Owner owner = Station::Get(this->window_number)->owner;
    01139         ShowVehicleListWindow(owner, (VehicleType)(widget - WID_SV_TRAINS), (StationID)this->window_number);
    01140         break;
    01141       }
    01142     }
    01143   }
    01144
    01145   virtual void OnQueryTextFinished(char *str)
    01146   {
    01147     if (str == NULL) return;
    01148
    01149     DoCommandP(0, this->window_number, 0, CMD_RENAME_STATION | CMD_MSG(STR_ERROR_CAN_T_RENAME_STATION), NULL, str);
    01150   }
    01151
    01152   virtual void OnResize()
    01153   {
    01154     this->vscroll->SetCapacityFromWidget(this, WID_SV_WAITING, WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM);
    01155   }
    01156 };
    01157
    01158
    01159 static const WindowDesc _station_view_desc(
    01160   WDP_AUTO, 249, 110,
    01161   WC_STATION_VIEW, WC_NONE,
    01162   WDF_UNCLICK_BUTTONS,
    01163   _nested_station_view_widgets, lengthof(_nested_station_view_widgets)
    01164 );
    01165
    01171 void ShowStationViewWindow(StationID station)
    01172 {
    01173   AllocateWindowDescFront<StationViewWindow>(&_station_view_desc, station);
    01174 }
    01175
    01177 struct TileAndStation {
    01178   TileIndex tile;
    01179   StationID station;
    01180 };
    01181
    01182 static SmallVector<TileAndStation, 8> _deleted_stations_nearby;
    01183 static SmallVector<StationID, 8> _stations_nearby_list;
    01184
    01192 template <class T>
    01193 static bool AddNearbyStation(TileIndex tile, void *user_data)
    01194 {
    01195   TileArea *ctx = (TileArea *)user_data;
    01196
    01197   /* First check if there were deleted stations here */
    01198   for (uint i = 0; i < _deleted_stations_nearby.Length(); i++) {
    01199     TileAndStation *ts = _deleted_stations_nearby.Get(i);
    01200     if (ts->tile == tile) {
    01201       *_stations_nearby_list.Append() = _deleted_stations_nearby[i].station;
    01202       _deleted_stations_nearby.Erase(ts);
    01203       i--;
    01204     }
    01205   }
    01206
    01207   /* Check if own station and if we stay within station spread */
    01208   if (!IsTileType(tile, MP_STATION)) return false;
    01209
    01210   StationID sid = GetStationIndex(tile);
    01211
    01212   /* This station is (likely) a waypoint */
    01213   if (!T::IsValidID(sid)) return false;
    01214
    01215   T *st = T::Get(sid);
    01216   if (st->owner != _local_company || _stations_nearby_list.Contains(sid)) return false;
    01217
    01218   if (st->rect.BeforeAddRect(ctx->tile, ctx->w, ctx->h, StationRect::ADD_TEST).Succeeded()) {
    01219     *_stations_nearby_list.Append() = sid;
    01220   }
    01221
    01222   return false; // We want to include *all* nearby stations
    01223 }
    01224
    01234 template <class T>
    01235 static const T *FindStationsNearby(TileArea ta, bool distant_join)
    01236 {
    01237   TileArea ctx = ta;
    01238
    01239   _stations_nearby_list.Clear();
    01240   _deleted_stations_nearby.Clear();
    01241
    01242   /* Check the inside, to return, if we sit on another station */
    01243   TILE_AREA_LOOP(t, ta) {
    01244     if (t < MapSize() && IsTileType(t, MP_STATION) && T::IsValidID(GetStationIndex(t))) return T::GetByTile(t);
    01245   }
    01246
    01247   /* Look for deleted stations */
    01248   const BaseStation *st;
    01249   FOR_ALL_BASE_STATIONS(st) {
    01250     if (T::IsExpected(st) && !st->IsInUse() && st->owner == _local_company) {
    01251       /* Include only within station spread (yes, it is strictly less than) */
    01252       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) {
    01253         TileAndStation *ts = _deleted_stations_nearby.Append();
    01254         ts->tile = st->xy;
    01255         ts->station = st->index;
    01256
    01257         /* Add the station when it's within where we're going to build */
    01258         if (IsInsideBS(TileX(st->xy), TileX(ctx.tile), ctx.w) &&
    01259             IsInsideBS(TileY(st->xy), TileY(ctx.tile), ctx.h)) {
    01260           AddNearbyStation<T>(st->xy, &ctx);
    01261         }
    01262       }
    01263     }
    01264   }
    01265
    01266   /* Only search tiles where we have a chance to stay within the station spread.
    01267    * The complete check needs to be done in the callback as we don't know the
    01268    * extent of the found station, yet. */
    01269   if (distant_join && min(ta.w, ta.h) >= _settings_game.station.station_spread) return NULL;
    01270   uint max_dist = distant_join ? _settings_game.station.station_spread - min(ta.w, ta.h) : 1;
    01271
    01272   TileIndex tile = TILE_ADD(ctx.tile, TileOffsByDir(DIR_N));
    01273   CircularTileSearch(&tile, max_dist, ta.w, ta.h, AddNearbyStation<T>, &ctx);
    01274
    01275   return NULL;
    01276 }
    01277
    01278 static const NWidgetPart _nested_select_station_widgets[] = {
    01279   NWidget(NWID_HORIZONTAL),
    01280     NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
    01281     NWidget(WWT_CAPTION, COLOUR_DARK_GREEN, WID_JS_CAPTION), SetDataTip(STR_JOIN_STATION_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    01282   EndContainer(),
    01283   NWidget(NWID_HORIZONTAL),
    01284     NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_JS_PANEL), SetResize(1, 0), SetScrollbar(WID_JS_SCROLLBAR), EndContainer(),
    01285     NWidget(NWID_VERTICAL),
    01286       NWidget(NWID_VSCROLLBAR, COLOUR_DARK_GREEN, WID_JS_SCROLLBAR),
    01287       NWidget(WWT_RESIZEBOX, COLOUR_DARK_GREEN),
    01288     EndContainer(),
    01289   EndContainer(),
    01290 };
    01291
    01296 template <class T>
    01297 struct SelectStationWindow : Window {
    01298   CommandContainer select_station_cmd;
    01299   TileArea area;
    01300   Scrollbar *vscroll;
    01301
    01302   SelectStationWindow(const WindowDesc *desc, CommandContainer cmd, TileArea ta) :
    01303     Window(),
    01304     select_station_cmd(cmd),
    01305     area(ta)
    01306   {
    01307     this->CreateNestedTree(desc);
    01308     this->vscroll = this->GetScrollbar(WID_JS_SCROLLBAR);
    01309     this->GetWidget<NWidgetCore>(WID_JS_CAPTION)->widget_data = T::EXPECTED_FACIL == FACIL_WAYPOINT ? STR_JOIN_WAYPOINT_CAPTION : STR_JOIN_STATION_CAPTION;
    01310     this->FinishInitNested(desc, 0);
    01311     this->OnInvalidateData(0);
    01312   }
    01313
    01314   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
    01315   {
    01316     if (widget != WID_JS_PANEL) return;
    01317
    01318     /* Determine the widest string */
    01319     Dimension d = GetStringBoundingBox(T::EXPECTED_FACIL == FACIL_WAYPOINT ? STR_JOIN_WAYPOINT_CREATE_SPLITTED_WAYPOINT : STR_JOIN_STATION_CREATE_SPLITTED_STATION);
    01320     for (uint i = 0; i < _stations_nearby_list.Length(); i++) {
    01321       const T *st = T::Get(_stations_nearby_list[i]);
    01322       SetDParam(0, st->index);
    01323       SetDParam(1, st->facilities);
    01324       d = maxdim(d, GetStringBoundingBox(T::EXPECTED_FACIL == FACIL_WAYPOINT ? STR_STATION_LIST_WAYPOINT : STR_STATION_LIST_STATION));
    01325     }
    01326
    01327     resize->height = d.height;
    01328     d.height *= 5;
    01329     d.width += WD_FRAMERECT_RIGHT + WD_FRAMERECT_LEFT;
    01330     d.height += WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
    01331     *size = d;
    01332   }
    01333
    01334   virtual void DrawWidget(const Rect &r, int widget) const
    01335   {
    01336     if (widget != WID_JS_PANEL) return;
    01337
    01338     uint y = r.top + WD_FRAMERECT_TOP;
    01339     if (this->vscroll->GetPosition() == 0) {
    01340       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);
    01341       y += this->resize.step_height;
    01342     }
    01343
    01344     for (uint i = max<uint>(1, this->vscroll->GetPosition()); i <= _stations_nearby_list.Length(); ++i, y += this->resize.step_height) {
    01345       /* Don't draw anything if it extends past the end of the window. */
    01346       if (i - this->vscroll->GetPosition() >= this->vscroll->GetCapacity()) break;
    01347
    01348       const T *st = T::Get(_stations_nearby_list[i - 1]);
    01349       SetDParam(0, st->index);
    01350       SetDParam(1, st->facilities);
    01351       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);
    01352     }
    01353   }
    01354
    01355   virtual void OnClick(Point pt, int widget, int click_count)
    01356   {
    01357     if (widget != WID_JS_PANEL) return;
    01358
    01359     uint st_index = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_JS_PANEL, WD_FRAMERECT_TOP);
    01360     bool distant_join = (st_index > 0);
    01361     if (distant_join) st_index--;
    01362
    01363     if (distant_join && st_index >= _stations_nearby_list.Length()) return;
    01364
    01365     /* Insert station to be joined into stored command */
    01366     SB(this->select_station_cmd.p2, 16, 16,
    01367        (distant_join ? _stations_nearby_list[st_index] : NEW_STATION));
    01368
    01369     /* Execute stored Command */
    01370     DoCommandP(&this->select_station_cmd);
    01371
    01372     /* Close Window; this might cause double frees! */
    01373     DeleteWindowById(WC_SELECT_STATION, 0);
    01374   }
    01375
    01376   virtual void OnTick()
    01377   {
    01378     if (_thd.dirty & 2) {
    01379       _thd.dirty &= ~2;
    01380       this->SetDirty();
    01381     }
    01382   }
    01383
    01384   virtual void OnResize()
    01385   {
    01386     this->vscroll->SetCapacityFromWidget(this, WID_JS_PANEL, WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM);
    01387   }
    01388
    01394   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
    01395   {
    01396     if (!gui_scope) return;
    01397     FindStationsNearby<T>(this->area, true);
    01398     this->vscroll->SetCount(_stations_nearby_list.Length() + 1);
    01399     this->SetDirty();
    01400   }
    01401 };
    01402
    01403 static const WindowDesc _select_station_desc(
    01404   WDP_AUTO, 200, 180,
    01405   WC_SELECT_STATION, WC_NONE,
    01406   WDF_CONSTRUCTION,
    01407   _nested_select_station_widgets, lengthof(_nested_select_station_widgets)
    01408 );
    01409
    01410
    01418 template <class T>
    01419 static bool StationJoinerNeeded(CommandContainer cmd, TileArea ta)
    01420 {
    01421   /* Only show selection if distant join is enabled in the settings */
    01422   if (!_settings_game.station.distant_join_stations) return false;
    01423
    01424   /* If a window is already opened and we didn't ctrl-click,
    01425    * return true (i.e. just flash the old window) */
    01426   Window *selection_window = FindWindowById(WC_SELECT_STATION, 0);
    01427   if (selection_window != NULL) {
    01428     /* Abort current distant-join and start new one */
    01429     delete selection_window;
    01430     UpdateTileSelection();
    01431   }
    01432
    01433   /* only show the popup, if we press ctrl */
    01434   if (!_ctrl_pressed) return false;
    01435
    01436   /* Now check if we could build there */
    01437   if (DoCommand(&cmd, CommandFlagsToDCFlags(GetCommandFlags(cmd.cmd))).Failed()) return false;
    01438
    01439   /* Test for adjacent station or station below selection.
    01440    * If adjacent-stations is disabled and we are building next to a station, do not show the selection window.
    01441    * but join the other station immediately. */
    01442   const T *st = FindStationsNearby<T>(ta, false);
    01443   return st == NULL && (_settings_game.station.adjacent_stations || _stations_nearby_list.Length() == 0);
    01444 }
    01445
    01452 template <class T>
    01453 void ShowSelectBaseStationIfNeeded(CommandContainer cmd, TileArea ta)
    01454 {
    01455   if (StationJoinerNeeded<T>(cmd, ta)) {
    01456     if (!_settings_client.gui.persistent_buildingtools) ResetObjectToPlace();
    01457     new SelectStationWindow<T>(&_select_station_desc, cmd, ta);
    01458   } else {
    01459     DoCommandP(&cmd);
    01460   }
    01461 }
    01462
    01468 void ShowSelectStationIfNeeded(CommandContainer cmd, TileArea ta)
    01469 {
    01470   ShowSelectBaseStationIfNeeded<Station>(cmd, ta);
    01471 }
    01472
    01478 void ShowSelectWaypointIfNeeded(CommandContainer cmd, TileArea ta)
    01479 {
    01480   ShowSelectBaseStationIfNeeded<Waypoint>(cmd, ta);
    01481 }