# station_gui.cpp

Go to the documentation of this file.

    00001 /* $Id: station_gui.cpp 18731 2010-01-05 16:59:57Z 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 "openttd.h"
    00014 #include "debug.h"
    00015 #include "gui.h"
    00016 #include "window_gui.h"
    00017 #include "textbuf_gui.h"
    00018 #include "company_func.h"
    00019 #include "command_func.h"
    00020 #include "vehicle_gui.h"
    00021 #include "cargotype.h"
    00022 #include "station_gui.h"
    00023 #include "strings_func.h"
    00024 #include "window_func.h"
    00025 #include "viewport_func.h"
    00026 #include "gfx_func.h"
    00027 #include "widgets/dropdown_func.h"
    00028 #include "station_base.h"
    00029 #include "waypoint_base.h"
    00030 #include "tilehighlight_func.h"
    00031 #include "company_base.h"
    00032 #include "sortlist_type.h"
    00033
    00034 #include "table/strings.h"
    00035 #include "table/sprites.h"
    00036
    00044 int DrawCargoListText(uint32 cargo_mask, const Rect &r, StringID prefix)
    00045 {
    00046   bool first = true;
    00047   char string[512];
    00048   char *b = InlineString(string, prefix);
    00049
    00050   for (CargoID i = 0; i < NUM_CARGO; i++) {
    00051     if (b >= lastof(string) - (1 + 2 * 4)) break; // ',' or ' ' and two calls to Utf8Encode()
    00052     if (HasBit(cargo_mask, i)) {
    00053       if (first) {
    00054         first = false;
    00055       } else {
    00056         /* Add a comma if this is not the first item */
    00057         *b++ = ',';
    00058         *b++ = ' ';
    00059       }
    00060       b = InlineString(b, CargoSpec::Get(i)->name);
    00061     }
    00062   }
    00063
    00064   /* If first is still true then no cargo is accepted */
    00065   if (first) b = InlineString(b, STR_JUST_NOTHING);
    00066
    00067   *b = '\0';
    00068
    00069   /* Make sure we detect any buffer overflow */
    00070   assert(b < endof(string));
    00071
    00072   SetDParamStr(0, string);
    00073   return DrawStringMultiLine(r.left, r.right, r.top, r.bottom, STR_JUST_RAW_STRING);
    00074 }
    00075
    00086 int DrawStationCoverageAreaText(int left, int right, int top, StationCoverageType sct, int rad, bool supplies)
    00087 {
    00088   TileIndex tile = TileVirtXY(_thd.pos.x, _thd.pos.y);
    00089   if (tile < MapSize()) {
    00090     CargoArray cargos;
    00091     if (supplies) {
    00092       cargos = GetProductionAroundTiles(tile, _thd.size.x / TILE_SIZE, _thd.size.y / TILE_SIZE, rad);
    00093     } else {
    00094       cargos = GetAcceptanceAroundTiles(tile, _thd.size.x / TILE_SIZE, _thd.size.y / TILE_SIZE, rad);
    00095     }
    00096
    00097     /* Convert cargo counts to a set of cargo bits, and draw the result. */
    00098     uint32 cargo_mask = 0;
    00099     for (CargoID i = 0; i < NUM_CARGO; i++) {
    00100       switch (sct) {
    00101         case SCT_PASSENGERS_ONLY: if (!IsCargoInClass(i, CC_PASSENGERS)) continue; break;
    00102         case SCT_NON_PASSENGERS_ONLY: if (IsCargoInClass(i, CC_PASSENGERS)) continue; break;
    00103         case SCT_ALL: break;
    00104         default: NOT_REACHED();
    00105       }
    00106       if (cargos[i] >= (supplies ? 1U : 8U)) SetBit(cargo_mask, i);
    00107     }
    00108     Rect r = {left, top, right, INT32_MAX};
    00109     return DrawCargoListText(cargo_mask, r, supplies ? STR_STATION_BUILD_SUPPLIES_CARGO : STR_STATION_BUILD_ACCEPTS_CARGO);
    00110   }
    00111
    00112   return top;
    00113 }
    00114
    00120 void CheckRedrawStationCoverage(const Window *w)
    00121 {
    00122   if (_thd.dirty & 1) {
    00123     _thd.dirty &= ~1;
    00124     w->SetDirty();
    00125   }
    00126 }
    00127
    00143 static void StationsWndShowStationRating(int left, int right, int y, CargoID type, uint amount, byte rating)
    00144 {
    00145   static const uint units_full  = 576;
    00146   static const uint rating_full = 224;
    00147
    00148   const CargoSpec *cs = CargoSpec::Get(type);
    00149   if (!cs->IsValid()) return;
    00150
    00151   int colour = cs->rating_colour;
    00152   uint w = (minu(amount, units_full) + 5) / 36;
    00153
    00154   int height = GetCharacterHeight(FS_SMALL);
    00155
    00156   /* Draw total cargo (limited) on station (fits into 16 pixels) */
    00157   if (w != 0) GfxFillRect(left, y, left + w - 1, y + height, colour);
    00158
    00159   /* Draw a one pixel-wide bar of additional cargo meter, useful
    00160    * for stations with only a small amount (<=30) */
    00161   if (w == 0) {
    00162     uint rest = amount / 5;
    00163     if (rest != 0) {
    00164       w += left;
    00165       GfxFillRect(w, y + height - rest, w, y + height, colour);
    00166     }
    00167   }
    00168
    00169   DrawString(left + 1, right, y, cs->abbrev, TC_BLACK);
    00170
    00171   /* Draw green/red ratings bar (fits into 14 pixels) */
    00172   y += height + 2;
    00173   GfxFillRect(left + 1, y, left + 14, y, 0xB8);
    00174   rating = minu(rating, rating_full) / 16;
    00175   if (rating != 0) GfxFillRect(left + 1, y, left + rating, y, 0xD0);
    00176 }
    00177
    00178 typedef GUIList<const Station*> GUIStationList;
    00179
    00181 enum StationListWidgets {
    00182   SLW_CAPTION,
    00183   SLW_LIST,
    00184   SLW_SCROLLBAR,
    00185
    00186   SLW_TRAIN,
    00187   SLW_TRUCK,
    00188   SLW_BUS,
    00189   SLW_AIRPLANE,
    00190   SLW_SHIP,
    00191   SLW_FACILALL,
    00192
    00193   SLW_NOCARGOWAITING,
    00194   SLW_CARGOALL,
    00195
    00196   SLW_SORTBY,
    00197   SLW_SORTDROPBTN,
    00198
    00199   SLW_CARGOSTART,
    00200 };
    00201
    00205 class CompanyStationsWindow : public Window
    00206 {
    00207 protected:
    00208   /* Runtime saved values */
    00209   static Listing last_sorting;
    00210   static byte facilities;               // types of stations of interest
    00211   static bool include_empty;            // whether we should include stations without waiting cargo
    00212   static const uint32 cargo_filter_max;
    00213   static uint32 cargo_filter;           // bitmap of cargo types to include
    00214   static const Station *last_station;
    00215
    00216   /* Constants for sorting stations */
    00217   static const StringID sorter_names[];
    00218   static GUIStationList::SortFunction * const sorter_funcs[];
    00219
    00220   GUIStationList stations;
    00221
    00222
    00228   void BuildStationsList(const Owner owner)
    00229   {
    00230     if (!this->stations.NeedRebuild()) return;
    00231
    00232     DEBUG(misc, 3, "Building station list for company %d", owner);
    00233
    00234     this->stations.Clear();
    00235
    00236     const Station *st;
    00237     FOR_ALL_STATIONS(st) {
    00238       if (st->owner == owner || (st->owner == OWNER_NONE && HasStationInUse(st->index, owner))) {
    00239         if (this->facilities & st->facilities) { // only stations with selected facilities
    00240           int num_waiting_cargo = 0;
    00241           for (CargoID j = 0; j < NUM_CARGO; j++) {
    00242             if (!st->goods[j].cargo.Empty()) {
    00243               num_waiting_cargo++; // count number of waiting cargo
    00244               if (HasBit(this->cargo_filter, j)) {
    00245                 *this->stations.Append() = st;
    00246                 break;
    00247               }
    00248             }
    00249           }
    00250           /* stations without waiting cargo */
    00251           if (num_waiting_cargo == 0 && this->include_empty) {
    00252             *this->stations.Append() = st;
    00253           }
    00254         }
    00255       }
    00256     }
    00257
    00258     this->stations.Compact();
    00259     this->stations.RebuildDone();
    00260
    00261     this->vscroll.SetCount(this->stations.Length()); // Update the scrollbar
    00262   }
    00263
    00265   static int CDECL StationNameSorter(const Station * const *a, const Station * const *b)
    00266   {
    00267     static char buf_cache[64];
    00268     char buf[64];
    00269
    00270     SetDParam(0, (*a)->index);
    00271     GetString(buf, STR_STATION_NAME, lastof(buf));
    00272
    00273     if (*b != last_station) {
    00274       last_station = *b;
    00275       SetDParam(0, (*b)->index);
    00276       GetString(buf_cache, STR_STATION_NAME, lastof(buf_cache));
    00277     }
    00278
    00279     return strcmp(buf, buf_cache);
    00280   }
    00281
    00283   static int CDECL StationTypeSorter(const Station * const *a, const Station * const *b)
    00284   {
    00285     return (*a)->facilities - (*b)->facilities;
    00286   }
    00287
    00289   static int CDECL StationWaitingSorter(const Station * const *a, const Station * const *b)
    00290   {
    00291     Money diff = 0;
    00292
    00293     for (CargoID j = 0; j < NUM_CARGO; j++) {
    00294       if (!HasBit(cargo_filter, j)) continue;
    00295       if (!(*a)->goods[j].cargo.Empty()) diff += GetTransportedGoodsIncome((*a)->goods[j].cargo.Count(), 20, 50, j);
    00296       if (!(*b)->goods[j].cargo.Empty()) diff -= GetTransportedGoodsIncome((*b)->goods[j].cargo.Count(), 20, 50, j);
    00297     }
    00298
    00299     return ClampToI32(diff);
    00300   }
    00301
    00303   static int CDECL StationRatingMaxSorter(const Station * const *a, const Station * const *b)
    00304   {
    00305     byte maxr1 = 0;
    00306     byte maxr2 = 0;
    00307
    00308     for (CargoID j = 0; j < NUM_CARGO; j++) {
    00309       if (!HasBit(cargo_filter, j)) continue;
    00310       if (HasBit((*a)->goods[j].acceptance_pickup, GoodsEntry::PICKUP)) maxr1 = max(maxr1, (*a)->goods[j].rating);
    00311       if (HasBit((*b)->goods[j].acceptance_pickup, GoodsEntry::PICKUP)) maxr2 = max(maxr2, (*b)->goods[j].rating);
    00312     }
    00313
    00314     return maxr1 - maxr2;
    00315   }
    00316
    00318   static int CDECL StationRatingMinSorter(const Station * const *a, const Station * const *b)
    00319   {
    00320     byte minr1 = 255;
    00321     byte minr2 = 255;
    00322
    00323     for (CargoID j = 0; j < NUM_CARGO; j++) {
    00324       if (!HasBit(cargo_filter, j)) continue;
    00325       if (HasBit((*a)->goods[j].acceptance_pickup, GoodsEntry::PICKUP)) minr1 = min(minr1, (*a)->goods[j].rating);
    00326       if (HasBit((*b)->goods[j].acceptance_pickup, GoodsEntry::PICKUP)) minr2 = min(minr2, (*b)->goods[j].rating);
    00327     }
    00328
    00329     return -(minr1 - minr2);
    00330   }
    00331
    00333   void SortStationsList()
    00334   {
    00335     if (!this->stations.Sort()) return;
    00336
    00337     /* Reset name sorter sort cache */
    00338     this->last_station = NULL;
    00339
    00340     /* Set the modified widget dirty */
    00341     this->SetWidgetDirty(SLW_LIST);
    00342   }
    00343
    00344 public:
    00345   CompanyStationsWindow(const WindowDesc *desc, WindowNumber window_number) : Window()
    00346   {
    00347     this->stations.SetListing(this->last_sorting);
    00348     this->stations.SetSortFuncs(this->sorter_funcs);
    00349     this->stations.ForceRebuild();
    00350     this->stations.NeedResort();
    00351     this->SortStationsList();
    00352
    00353     this->InitNested(desc, window_number);
    00354     this->owner = (Owner)this->window_number;
    00355
    00356     for (uint i = 0; i < NUM_CARGO; i++) {
    00357       const CargoSpec *cs = CargoSpec::Get(i);
    00358       if (cs->IsValid() && HasBit(this->cargo_filter, i)) this->LowerWidget(SLW_CARGOSTART + i);
    00359     }
    00360
    00361     if (this->cargo_filter == this->cargo_filter_max) this->cargo_filter = _cargo_mask;
    00362
    00363     for (uint i = 0; i < 5; i++) {
    00364       if (HasBit(this->facilities, i)) this->LowerWidget(i + SLW_TRAIN);
    00365     }
    00366     this->SetWidgetLoweredState(SLW_FACILALL, this->facilities == (FACIL_TRAIN | FACIL_TRUCK_STOP | FACIL_BUS_STOP | FACIL_AIRPORT | FACIL_DOCK));
    00367     this->SetWidgetLoweredState(SLW_CARGOALL, this->cargo_filter == _cargo_mask && this->include_empty);
    00368     this->SetWidgetLoweredState(SLW_NOCARGOWAITING, this->include_empty);
    00369
    00370     this->GetWidget<NWidgetCore>(SLW_SORTDROPBTN)->widget_data = this->sorter_names[this->stations.SortType()];
    00371   }
    00372
    00373   ~CompanyStationsWindow()
    00374   {
    00375     this->last_sorting = this->stations.GetListing();
    00376   }
    00377
    00378   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
    00379   {
    00380     switch (widget) {
    00381       case SLW_SORTBY: {
    00382         Dimension d = GetStringBoundingBox(this->GetWidget<NWidgetCore>(widget)->widget_data);
    00383         d.width += padding.width + WD_SORTBUTTON_ARROW_WIDTH * 2; // Doubled since the word is centered, also looks nice.
    00384         d.height += padding.height;
    00385         *size = maxdim(*size, d);
    00386         break;
    00387       }
    00388
    00389       case SLW_SORTDROPBTN: {
    00390         Dimension d = {0, 0};
    00391         for (int i = 0; this->sorter_names[i] != INVALID_STRING_ID; i++) {
    00392           d = maxdim(d, GetStringBoundingBox(this->sorter_names[i]));
    00393         }
    00394         d.width += padding.width;
    00395         d.height += padding.height;
    00396         *size = maxdim(*size, d);
    00397         break;
    00398       }
    00399
    00400       case SLW_LIST:
    00401         resize->height = FONT_HEIGHT_NORMAL;
    00402         size->height = WD_FRAMERECT_TOP + 5 * resize->height + WD_FRAMERECT_BOTTOM;
    00403         break;
    00404
    00405       case SLW_TRAIN:
    00406       case SLW_TRUCK:
    00407       case SLW_BUS:
    00408       case SLW_AIRPLANE:
    00409       case SLW_SHIP:
    00410         size->height = max<uint>(FONT_HEIGHT_SMALL, 10) + padding.height;
    00411         break;
    00412
    00413       case SLW_CARGOALL:
    00414       case SLW_FACILALL:
    00415       case SLW_NOCARGOWAITING: {
    00416         Dimension d = GetStringBoundingBox(widget == SLW_NOCARGOWAITING ? STR_ABBREV_NONE : STR_ABBREV_ALL);
    00417         d.width  += padding.width + 2;
    00418         d.height += padding.height;
    00419         *size = maxdim(*size, d);
    00420         break;
    00421       }
    00422
    00423       default:
    00424         if (widget >= SLW_CARGOSTART) {
    00425           const CargoSpec *cs = CargoSpec::Get(widget - SLW_CARGOSTART);
    00426           if (cs->IsValid()) {
    00427             Dimension d = GetStringBoundingBox(cs->abbrev);
    00428             d.width  += padding.width + 2;
    00429             d.height += padding.height;
    00430             *size = maxdim(*size, d);
    00431           }
    00432         }
    00433         break;
    00434     }
    00435   }
    00436
    00437   virtual void OnPaint()
    00438   {
    00439     this->BuildStationsList((Owner)this->window_number);
    00440     this->SortStationsList();
    00441
    00442     this->DrawWidgets();
    00443   }
    00444
    00445   virtual void DrawWidget(const Rect &r, int widget) const
    00446   {
    00447     switch (widget) {
    00448       case SLW_SORTBY:
    00449         /* draw arrow pointing up/down for ascending/descending sorting */
    00450         this->DrawSortButtonState(SLW_SORTBY, this->stations.IsDescSortOrder() ? SBS_DOWN : SBS_UP);
    00451         break;
    00452
    00453       case SLW_LIST: {
    00454         bool rtl = _dynlang.text_dir == TD_RTL;
    00455         int max = min(this->vscroll.GetPosition() + this->vscroll.GetCapacity(), this->stations.Length());
    00456         int y = r.top + WD_FRAMERECT_TOP;
    00457         for (int i = this->vscroll.GetPosition(); i < max; ++i) { // do until max number of stations of owner
    00458           const Station *st = this->stations[i];
    00459           assert(st->xy != INVALID_TILE);
    00460
    00461           /* Do not do the complex check HasStationInUse here, it may be even false
    00462            * when the order had been removed and the station list hasn't been removed yet */
    00463           assert(st->owner == owner || st->owner == OWNER_NONE);
    00464
    00465           SetDParam(0, st->index);
    00466           SetDParam(1, st->facilities);
    00467           int x = DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_STATION_LIST_STATION);
    00468           x += rtl ? -5 : 5;
    00469
    00470           /* show cargo waiting and station ratings */
    00471           for (CargoID j = 0; j < NUM_CARGO; j++) {
    00472             if (!st->goods[j].cargo.Empty()) {
    00473               /* For RTL we work in exactly the opposite direction. So
    00474                * decrement the space needed first, then draw to the left
    00475                * instead of drawing to the left and then incrementing
    00476                * the space. */
    00477               if (rtl) {
    00478                 x -= 20;
    00479                 if (x < r.left + WD_FRAMERECT_LEFT) break;
    00480               }
    00481               StationsWndShowStationRating(x, x + 16, y, j, st->goods[j].cargo.Count(), st->goods[j].rating);
    00482               if (!rtl) {
    00483                 x += 20;
    00484                 if (x > r.right - WD_FRAMERECT_RIGHT) break;
    00485               }
    00486             }
    00487           }
    00488           y += FONT_HEIGHT_NORMAL;
    00489         }
    00490
    00491         if (this->vscroll.GetCount() == 0) { // company has no stations
    00492           DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_STATION_LIST_NONE);
    00493           return;
    00494         }
    00495         break;
    00496       }
    00497
    00498       case SLW_NOCARGOWAITING: {
    00499         int cg_ofst = this->IsWidgetLowered(widget) ? 2 : 1;
    00500         DrawString(r.left + cg_ofst, r.right + cg_ofst, r.top + cg_ofst, STR_ABBREV_NONE, TC_BLACK, SA_CENTER);
    00501         break;
    00502       }
    00503
    00504       case SLW_CARGOALL: {
    00505         int cg_ofst = this->IsWidgetLowered(widget) ? 2 : 1;
    00506         DrawString(r.left + cg_ofst, r.right + cg_ofst, r.top + cg_ofst, STR_ABBREV_ALL, TC_BLACK, SA_CENTER);
    00507         break;
    00508       }
    00509
    00510       case SLW_FACILALL: {
    00511         int cg_ofst = this->IsWidgetLowered(widget) ? 2 : 1;
    00512         DrawString(r.left + cg_ofst, r.right + cg_ofst, r.top + cg_ofst, STR_ABBREV_ALL, TC_BLACK);
    00513         break;
    00514       }
    00515
    00516       default:
    00517         if (widget >= SLW_CARGOSTART) {
    00518           const CargoSpec *cs = CargoSpec::Get(widget - SLW_CARGOSTART);
    00519           if (cs->IsValid()) {
    00520             int cg_ofst = HasBit(this->cargo_filter, cs->Index()) ? 2 : 1;
    00521             GfxFillRect(r.left + cg_ofst, r.top + cg_ofst, r.right - 2 + cg_ofst, r.bottom - 2 + cg_ofst, cs->rating_colour);
    00522             DrawString(r.left + cg_ofst, r.right + cg_ofst, r.top + cg_ofst, cs->abbrev, TC_BLACK, SA_CENTER);
    00523           }
    00524         }
    00525         break;
    00526     }
    00527   }
    00528
    00529   virtual void SetStringParameters(int widget) const
    00530   {
    00531     if (widget == SLW_CAPTION) {
    00532       SetDParam(0, this->window_number);
    00533       SetDParam(1, this->vscroll.GetCount());
    00534     }
    00535   }
    00536
    00537   virtual void OnClick(Point pt, int widget)
    00538   {
    00539     switch (widget) {
    00540       case SLW_LIST: {
    00541         uint32 id_v = (pt.y - this->GetWidget<NWidgetBase>(SLW_LIST)->pos_y - WD_FRAMERECT_TOP) / FONT_HEIGHT_NORMAL;
    00542
    00543         if (id_v >= this->vscroll.GetCapacity()) return; // click out of bounds
    00544
    00545         id_v += this->vscroll.GetPosition();
    00546
    00547         if (id_v >= this->stations.Length()) return; // click out of list bound
    00548
    00549         const Station *st = this->stations[id_v];
    00550         /* do not check HasStationInUse - it is slow and may be invalid */
    00551         assert(st->owner == (Owner)this->window_number || st->owner == OWNER_NONE);
    00552
    00553         if (_ctrl_pressed) {
    00554           ShowExtraViewPortWindow(st->xy);
    00555         } else {
    00556           ScrollMainWindowToTile(st->xy);
    00557         }
    00558         break;
    00559       }
    00560
    00561       case SLW_TRAIN:
    00562       case SLW_TRUCK:
    00563       case SLW_BUS:
    00564       case SLW_AIRPLANE:
    00565       case SLW_SHIP:
    00566         if (_ctrl_pressed) {
    00567           ToggleBit(this->facilities, widget - SLW_TRAIN);
    00568           this->ToggleWidgetLoweredState(widget);
    00569         } else {
    00570           uint i;
    00571           FOR_EACH_SET_BIT(i, this->facilities) {
    00572             this->RaiseWidget(i + SLW_TRAIN);
    00573           }
    00574           SetBit(this->facilities, widget - SLW_TRAIN);
    00575           this->LowerWidget(widget);
    00576         }
    00577         this->SetWidgetLoweredState(SLW_FACILALL, this->facilities == (FACIL_TRAIN | FACIL_TRUCK_STOP | FACIL_BUS_STOP | FACIL_AIRPORT | FACIL_DOCK));
    00578         this->stations.ForceRebuild();
    00579         this->SetDirty();
    00580         break;
    00581
    00582       case SLW_FACILALL:
    00583         for (uint i = 0; i < 5; i++) {
    00584           this->LowerWidget(i + SLW_TRAIN);
    00585         }
    00586         this->LowerWidget(SLW_FACILALL);
    00587
    00588         this->facilities = FACIL_TRAIN | FACIL_TRUCK_STOP | FACIL_BUS_STOP | FACIL_AIRPORT | FACIL_DOCK;
    00589         this->stations.ForceRebuild();
    00590         this->SetDirty();
    00591         break;
    00592
    00593       case SLW_CARGOALL: {
    00594         for (uint i = 0; i < NUM_CARGO; i++) {
    00595           const CargoSpec *cs = CargoSpec::Get(i);
    00596           if (cs->IsValid()) this->LowerWidget(SLW_CARGOSTART + i);
    00597         }
    00598         this->LowerWidget(SLW_NOCARGOWAITING);
    00599         this->LowerWidget(SLW_CARGOALL);
    00600
    00601         this->cargo_filter = _cargo_mask;
    00602         this->include_empty = true;
    00603         this->stations.ForceRebuild();
    00604         this->SetDirty();
    00605         break;
    00606       }
    00607
    00608       case SLW_SORTBY: // flip sorting method asc/desc
    00609         this->stations.ToggleSortOrder();
    00610         this->flags4 |= WF_TIMEOUT_BEGIN;
    00611         this->LowerWidget(SLW_SORTBY);
    00612         this->SetDirty();
    00613         break;
    00614
    00615       case SLW_SORTDROPBTN: // select sorting criteria dropdown menu
    00616         ShowDropDownMenu(this, this->sorter_names, this->stations.SortType(), SLW_SORTDROPBTN, 0, 0);
    00617         break;
    00618
    00619       case SLW_NOCARGOWAITING:
    00620         if (_ctrl_pressed) {
    00621           this->include_empty = !this->include_empty;
    00622           this->ToggleWidgetLoweredState(SLW_NOCARGOWAITING);
    00623         } else {
    00624           for (uint i = 0; i < NUM_CARGO; i++) {
    00625             const CargoSpec *cs = CargoSpec::Get(i);
    00626             if (cs->IsValid()) this->RaiseWidget(SLW_CARGOSTART + i);
    00627           }
    00628
    00629           this->cargo_filter = 0;
    00630           this->include_empty = true;
    00631
    00632           this->LowerWidget(SLW_NOCARGOWAITING);
    00633         }
    00634         this->SetWidgetLoweredState(SLW_CARGOALL, this->cargo_filter == _cargo_mask && this->include_empty);
    00635         this->stations.ForceRebuild();
    00636         this->SetDirty();
    00637         break;
    00638
    00639       default:
    00640         if (widget >= SLW_CARGOSTART) { // change cargo_filter
    00641           /* Determine the selected cargo type */
    00642           const CargoSpec *cs = CargoSpec::Get(widget - SLW_CARGOSTART);
    00643           if (!cs->IsValid()) break;
    00644
    00645           if (_ctrl_pressed) {
    00646             ToggleBit(this->cargo_filter, cs->Index());
    00647             this->ToggleWidgetLoweredState(widget);
    00648           } else {
    00649             for (uint i = 0; i < NUM_CARGO; i++) {
    00650               const CargoSpec *cs = CargoSpec::Get(i);
    00651               if (cs->IsValid()) this->RaiseWidget(SLW_CARGOSTART + i);
    00652             }
    00653             this->RaiseWidget(SLW_NOCARGOWAITING);
    00654
    00655             this->cargo_filter = 0;
    00656             this->include_empty = false;
    00657
    00658             SetBit(this->cargo_filter, cs->Index());
    00659             this->LowerWidget(widget);
    00660           }
    00661           this->SetWidgetLoweredState(SLW_CARGOALL, this->cargo_filter == _cargo_mask && this->include_empty);
    00662           this->stations.ForceRebuild();
    00663           this->SetDirty();
    00664         }
    00665         break;
    00666     }
    00667   }
    00668
    00669   virtual void OnDropdownSelect(int widget, int index)
    00670   {
    00671     if (this->stations.SortType() != index) {
    00672       this->stations.SetSortType(index);
    00673
    00674       /* Display the current sort variant */
    00675       this->GetWidget<NWidgetCore>(SLW_SORTDROPBTN)->widget_data = this->sorter_names[this->stations.SortType()];
    00676
    00677       this->SetDirty();
    00678     }
    00679   }
    00680
    00681   virtual void OnTick()
    00682   {
    00683     if (_pause_mode != PM_UNPAUSED) return;
    00684     if (this->stations.NeedResort()) {
    00685       DEBUG(misc, 3, "Periodic rebuild station list company %d", this->window_number);
    00686       this->SetDirty();
    00687     }
    00688   }
    00689
    00690   virtual void OnTimeout()
    00691   {
    00692     this->RaiseWidget(SLW_SORTBY);
    00693     this->SetDirty();
    00694   }
    00695
    00696   virtual void OnResize()
    00697   {
    00698     this->vscroll.SetCapacityFromWidget(this, SLW_LIST, WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM);
    00699   }
    00700
    00701   virtual void OnInvalidateData(int data)
    00702   {
    00703     if (data == 0) {
    00704       this->stations.ForceRebuild();
    00705     } else {
    00706       this->stations.ForceResort();
    00707     }
    00708   }
    00709 };
    00710
    00711 Listing CompanyStationsWindow::last_sorting = {false, 0};
    00712 byte CompanyStationsWindow::facilities = FACIL_TRAIN | FACIL_TRUCK_STOP | FACIL_BUS_STOP | FACIL_AIRPORT | FACIL_DOCK;
    00713 bool CompanyStationsWindow::include_empty = true;
    00714 const uint32 CompanyStationsWindow::cargo_filter_max = UINT32_MAX;
    00715 uint32 CompanyStationsWindow::cargo_filter = UINT32_MAX;
    00716 const Station *CompanyStationsWindow::last_station = NULL;
    00717
    00718 /* Availible station sorting functions */
    00719 GUIStationList::SortFunction * const CompanyStationsWindow::sorter_funcs[] = {
    00720   &StationNameSorter,
    00721   &StationTypeSorter,
    00722   &StationWaitingSorter,
    00723   &StationRatingMaxSorter,
    00724   &StationRatingMinSorter
    00725 };
    00726
    00727 /* Names of the sorting functions */
    00728 const StringID CompanyStationsWindow::sorter_names[] = {
    00729   STR_SORT_BY_NAME,
    00730   STR_SORT_BY_FACILITY,
    00731   STR_SORT_BY_WAITING,
    00732   STR_SORT_BY_RATING_MAX,
    00733   STR_SORT_BY_RATING_MIN,
    00734   INVALID_STRING_ID
    00735 };
    00736
    00741 static NWidgetBase *CargoWidgets(int *biggest_index)
    00742 {
    00743   NWidgetHorizontal *container = new NWidgetHorizontal();
    00744
    00745   for (uint i = 0; i < NUM_CARGO; i++) {
    00746     const CargoSpec *cs = CargoSpec::Get(i);
    00747     if (cs->IsValid()) {
    00748       NWidgetBackground *panel = new NWidgetBackground(WWT_PANEL, COLOUR_GREY, SLW_CARGOSTART + i);
    00749       panel->SetMinimalSize(14, 11);
    00750       panel->SetResize(0, 0);
    00751       panel->SetFill(0, 1);
    00752       panel->SetDataTip(0, STR_STATION_LIST_USE_CTRL_TO_SELECT_MORE);
    00753       container->Add(panel);
    00754     } else {
    00755       NWidgetLeaf *nwi = new NWidgetLeaf(WWT_EMPTY, COLOUR_GREY, SLW_CARGOSTART + i, 0x0, STR_NULL);
    00756       nwi->SetMinimalSize(0, 11);
    00757       nwi->SetResize(0, 0);
    00758       nwi->SetFill(0, 1);
    00759       container->Add(nwi);
    00760     }
    00761   }
    00762   *biggest_index = SLW_CARGOSTART + NUM_CARGO;
    00763   return container;
    00764 }
    00765
    00766 static const NWidgetPart _nested_company_stations_widgets[] = {
    00767   NWidget(NWID_HORIZONTAL),
    00768     NWidget(WWT_CLOSEBOX, COLOUR_GREY),
    00769     NWidget(WWT_CAPTION, COLOUR_GREY, SLW_CAPTION), SetDataTip(STR_STATION_LIST_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    00770     NWidget(WWT_SHADEBOX, COLOUR_GREY),
    00771     NWidget(WWT_STICKYBOX, COLOUR_GREY),
    00772   EndContainer(),
    00773   NWidget(NWID_HORIZONTAL),
    00774     NWidget(WWT_TEXTBTN, COLOUR_GREY, SLW_TRAIN), SetMinimalSize(14, 11), SetDataTip(STR_TRAIN, STR_STATION_LIST_USE_CTRL_TO_SELECT_MORE), SetFill(0, 1),
    00775     NWidget(WWT_TEXTBTN, COLOUR_GREY, SLW_TRUCK), SetMinimalSize(14, 11), SetDataTip(STR_LORRY, STR_STATION_LIST_USE_CTRL_TO_SELECT_MORE), SetFill(0, 1),
    00776     NWidget(WWT_TEXTBTN, COLOUR_GREY, SLW_BUS), SetMinimalSize(14, 11), SetDataTip(STR_BUS, STR_STATION_LIST_USE_CTRL_TO_SELECT_MORE), SetFill(0, 1),
    00777     NWidget(WWT_TEXTBTN, COLOUR_GREY, SLW_AIRPLANE), SetMinimalSize(14, 11), SetDataTip(STR_PLANE, STR_STATION_LIST_USE_CTRL_TO_SELECT_MORE), SetFill(0, 1),
    00778     NWidget(WWT_TEXTBTN, COLOUR_GREY, SLW_SHIP), SetMinimalSize(14, 11), SetDataTip(STR_SHIP, STR_STATION_LIST_USE_CTRL_TO_SELECT_MORE), SetFill(0, 1),
    00779     NWidget(WWT_PANEL, COLOUR_GREY, SLW_FACILALL), SetMinimalSize(14, 11), SetDataTip(0x0, STR_STATION_LIST_SELECT_ALL_FACILITIES), SetFill(0, 1), EndContainer(),
    00780     NWidget(WWT_PANEL, COLOUR_GREY), SetMinimalSize(5, 11), SetFill(0, 1), EndContainer(),
    00781     NWidgetFunction(CargoWidgets),
    00782     NWidget(WWT_PANEL, COLOUR_GREY, SLW_NOCARGOWAITING), SetMinimalSize(14, 11), SetDataTip(0x0, STR_STATION_LIST_NO_WAITING_CARGO), SetFill(0, 1), EndContainer(),
    00783     NWidget(WWT_PANEL, COLOUR_GREY, SLW_CARGOALL), SetMinimalSize(14, 11), SetDataTip(0x0, STR_STATION_LIST_SELECT_ALL_TYPES), SetFill(0, 1), EndContainer(),
    00784     NWidget(WWT_PANEL, COLOUR_GREY), SetDataTip(0x0, STR_NULL), SetResize(1, 0), SetFill(1, 1), EndContainer(),
    00785   EndContainer(),
    00786   NWidget(NWID_HORIZONTAL),
    00787     NWidget(WWT_TEXTBTN, COLOUR_GREY, SLW_SORTBY), SetMinimalSize(81, 12), SetDataTip(STR_BUTTON_SORT_BY, STR_TOOLTIP_SORT_ORDER),
    00788     NWidget(WWT_DROPDOWN, COLOUR_GREY, SLW_SORTDROPBTN), SetMinimalSize(163, 12), SetDataTip(STR_SORT_BY_NAME, STR_TOOLTIP_SORT_CRITERIAP), // widget_data gets overwritten.
    00789     NWidget(WWT_PANEL, COLOUR_GREY), SetDataTip(0x0, STR_NULL), SetResize(1, 0), SetFill(1, 1), EndContainer(),
    00790   EndContainer(),
    00791   NWidget(NWID_HORIZONTAL),
    00792     NWidget(WWT_PANEL, COLOUR_GREY, SLW_LIST), SetMinimalSize(346, 125), SetResize(1, 10), SetDataTip(0x0, STR_STATION_LIST_TOOLTIP), EndContainer(),
    00793     NWidget(NWID_VERTICAL),
    00794       NWidget(WWT_SCROLLBAR, COLOUR_GREY, SLW_SCROLLBAR),
    00795       NWidget(WWT_RESIZEBOX, COLOUR_GREY),
    00796     EndContainer(),
    00797   EndContainer(),
    00798 };
    00799
    00800 static const WindowDesc _company_stations_desc(
    00801   WDP_AUTO, 358, 162,
    00802   WC_STATION_LIST, WC_NONE,
    00803   0,
    00804   _nested_company_stations_widgets, lengthof(_nested_company_stations_widgets)
    00805 );
    00806
    00812 void ShowCompanyStations(CompanyID company)
    00813 {
    00814   if (!Company::IsValidID(company)) return;
    00815
    00816   AllocateWindowDescFront<CompanyStationsWindow>(&_company_stations_desc, company);
    00817 }
    00818
    00819 static const NWidgetPart _nested_station_view_widgets[] = {
    00820   NWidget(NWID_HORIZONTAL),
    00821     NWidget(WWT_CLOSEBOX, COLOUR_GREY),
    00822     NWidget(WWT_CAPTION, COLOUR_GREY, SVW_CAPTION), SetDataTip(STR_STATION_VIEW_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    00823     NWidget(WWT_SHADEBOX, COLOUR_GREY),
    00824     NWidget(WWT_STICKYBOX, COLOUR_GREY),
    00825   EndContainer(),
    00826   NWidget(NWID_HORIZONTAL),
    00827     NWidget(WWT_PANEL, COLOUR_GREY, SVW_WAITING), SetMinimalSize(237, 52), SetResize(1, 10), EndContainer(),
    00828     NWidget(WWT_SCROLLBAR, COLOUR_GREY, SVW_SCROLLBAR),
    00829   EndContainer(),
    00830   NWidget(WWT_PANEL, COLOUR_GREY, SVW_ACCEPTLIST), SetMinimalSize(249, 32), SetResize(1, 0), EndContainer(),
    00831   NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
    00832     NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, SVW_LOCATION), SetMinimalSize(60, 12), SetResize(1, 0), SetFill(1, 1),
    00833         SetDataTip(STR_BUTTON_LOCATION, STR_STATION_VIEW_CENTER_TOOLTIP),
    00834     NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, SVW_ACCEPTS), SetMinimalSize(61, 12), SetResize(1, 0), SetFill(1, 1),
    00835         SetDataTip(STR_STATION_VIEW_RATINGS_BUTTON, STR_STATION_VIEW_RATINGS_TOOLTIP),
    00836     NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, SVW_RENAME), SetMinimalSize(60, 12), SetResize(1, 0), SetFill(1, 1),
    00837         SetDataTip(STR_BUTTON_RENAME, STR_STATION_VIEW_RENAME_TOOLTIP),
    00838     NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, SVW_TRAINS), SetMinimalSize(14, 12), SetFill(0, 1), SetDataTip(STR_TRAIN, STR_STATION_VIEW_SCHEDULED_TRAINS_TOOLTIP),
    00839     NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, SVW_ROADVEHS), SetMinimalSize(14, 12), SetFill(0, 1), SetDataTip(STR_LORRY, STR_STATION_VIEW_SCHEDULED_ROAD_VEHICLES_TOOLTIP),
    00840     NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, SVW_PLANES),  SetMinimalSize(14, 12), SetFill(0, 1), SetDataTip(STR_PLANE, STR_STATION_VIEW_SCHEDULED_AIRCRAFT_TOOLTIP),
    00841     NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, SVW_SHIPS), SetMinimalSize(14, 12), SetFill(0, 1), SetDataTip(STR_SHIP, STR_STATION_VIEW_SCHEDULED_SHIPS_TOOLTIP),
    00842     NWidget(WWT_RESIZEBOX, COLOUR_GREY),
    00843   EndContainer(),
    00844 };
    00845
    00856 static void DrawCargoIcons(CargoID i, uint waiting, int left, int right, int y)
    00857 {
    00858   uint num = min((waiting + 5) / 10, (right - left) / 10); // maximum is width / 10 icons so it won't overflow
    00859   if (num == 0) return;
    00860
    00861   SpriteID sprite = CargoSpec::Get(i)->GetCargoIcon();
    00862
    00863   int x = _dynlang.text_dir == TD_RTL ? right - num * 10 : left;
    00864   do {
    00865     DrawSprite(sprite, PAL_NONE, x, y);
    00866     x += 10;
    00867   } while (--num);
    00868 }
    00869
    00870 struct CargoData {
    00871   CargoID cargo;
    00872   StationID source;
    00873   uint count;
    00874
    00875   CargoData(CargoID cargo, StationID source, uint count) :
    00876     cargo(cargo),
    00877     source(source),
    00878     count(count)
    00879   { }
    00880 };
    00881
    00882 typedef std::list<CargoData> CargoDataList;
    00883
    00887 struct StationViewWindow : public Window {
    00888   uint32 cargo;
    00889   uint16 cargo_rows[NUM_CARGO];
    00890   uint expand_shrink_width;
    00891
    00893   enum AcceptListHeight {
    00894     ALH_RATING  = 13,
    00895     ALH_ACCEPTS = 3,
    00896   };
    00897
    00898   StationViewWindow(const WindowDesc *desc, WindowNumber window_number) : Window()
    00899   {
    00900     this->CreateNestedTree(desc);
    00901     /* Nested widget tree creation is done in two steps to ensure that this->GetWidget<NWidgetCore>(SVW_ACCEPTS) exists in UpdateWidgetSize(). */
    00902     this->FinishInitNested(desc, window_number);
    00903
    00904     Owner owner = Station::Get(window_number)->owner;
    00905     if (owner != OWNER_NONE) this->owner = owner;
    00906   }
    00907
    00908   ~StationViewWindow()
    00909   {
    00910     WindowNumber wno = (this->window_number << 16) | VLW_STATION_LIST | Station::Get(this->window_number)->owner;
    00911
    00912     DeleteWindowById(WC_TRAINS_LIST, wno | (VEH_TRAIN << 11), false);
    00913     DeleteWindowById(WC_ROADVEH_LIST, wno | (VEH_ROAD << 11), false);
    00914     DeleteWindowById(WC_SHIPS_LIST, wno | (VEH_SHIP << 11), false);
    00915     DeleteWindowById(WC_AIRCRAFT_LIST, wno | (VEH_AIRCRAFT << 11), false);
    00916   }
    00917
    00918   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
    00919   {
    00920     switch (widget) {
    00921       case SVW_WAITING:
    00922         resize->height = FONT_HEIGHT_NORMAL;
    00923         size->height = WD_FRAMERECT_TOP + 5 * resize->height + WD_FRAMERECT_BOTTOM;
    00924         this->expand_shrink_width = max(GetStringBoundingBox("-").width, GetStringBoundingBox("+").width) + WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
    00925         break;
    00926
    00927       case SVW_ACCEPTLIST:
    00928         size->height = WD_FRAMERECT_TOP + ((this->GetWidget<NWidgetCore>(SVW_ACCEPTS)->widget_data == STR_STATION_VIEW_RATINGS_BUTTON) ? ALH_ACCEPTS : ALH_RATING) * FONT_HEIGHT_NORMAL + WD_FRAMERECT_BOTTOM;
    00929         break;
    00930     }
    00931   }
    00932
    00933   virtual void OnPaint()
    00934   {
    00935     CargoDataList cargolist;
    00936     uint32 transfers = 0;
    00937     this->OrderWaitingCargo(&cargolist, &transfers);
    00938
    00939     this->vscroll.SetCount((int)cargolist.size() + 1); // update scrollbar
    00940
    00941     /* disable some buttons */
    00942     const Station *st = Station::Get(this->window_number);
    00943     this->SetWidgetDisabledState(SVW_RENAME,   st->owner != _local_company);
    00944     this->SetWidgetDisabledState(SVW_TRAINS,   !(st->facilities & FACIL_TRAIN));
    00945     this->SetWidgetDisabledState(SVW_ROADVEHS, !(st->facilities & FACIL_TRUCK_STOP) && !(st->facilities & FACIL_BUS_STOP));
    00946     this->SetWidgetDisabledState(SVW_PLANES,   !(st->facilities & FACIL_AIRPORT));
    00947     this->SetWidgetDisabledState(SVW_SHIPS,    !(st->facilities & FACIL_DOCK));
    00948
    00949     this->DrawWidgets();
    00950
    00951     if (!this->IsShaded()) {
    00952       NWidgetBase *nwi = this->GetWidget<NWidgetBase>(SVW_WAITING);
    00953       Rect waiting_rect = {nwi->pos_x, nwi->pos_y, nwi->pos_x + nwi->current_x - 1, nwi->pos_y + nwi->current_y - 1};
    00954       this->DrawWaitingCargo(waiting_rect, cargolist, transfers);
    00955     }
    00956   }
    00957
    00958   virtual void DrawWidget(const Rect &r, int widget) const
    00959   {
    00960     if (widget != SVW_ACCEPTLIST) return;
    00961
    00962     if (this->GetWidget<NWidgetCore>(SVW_ACCEPTS)->widget_data == STR_STATION_VIEW_RATINGS_BUTTON) {
    00963       this->DrawAcceptedCargo(r);
    00964     } else {
    00965       this->DrawCargoRatings(r);
    00966     }
    00967   }
    00968
    00969   virtual void SetStringParameters(int widget) const
    00970   {
    00971     if (widget == SVW_CAPTION) {
    00972       const Station *st = Station::Get(this->window_number);
    00973       SetDParam(0, st->index);
    00974       SetDParam(1, st->facilities);
    00975     }
    00976   }
    00977
    00983   void OrderWaitingCargo(CargoDataList *cargolist, uint32 *transfers)
    00984   {
    00985     assert(cargolist->size() == 0);
    00986     *transfers = 0;
    00987
    00988     StationID station_id = this->window_number;
    00989     const Station *st = Station::Get(station_id);
    00990
    00991     /* count types of cargos waiting in station */
    00992     for (CargoID i = 0; i < NUM_CARGO; i++) {
    00993       if (st->goods[i].cargo.Empty()) {
    00994         this->cargo_rows[i] = 0;
    00995       } else {
    00996         /* Add an entry for total amount of cargo of this type waiting. */
    00997         cargolist->push_back(CargoData(i, INVALID_STATION, st->goods[i].cargo.Count()));
    00998
    00999         /* Set the row for this cargo entry for the expand/hide button */
    01000         this->cargo_rows[i] = (uint16)cargolist->size();
    01001
    01002         /* Add an entry for each distinct cargo source. */
    01003         const StationCargoList::List *packets = st->goods[i].cargo.Packets();
    01004         for (StationCargoList::ConstIterator it(packets->begin()); it != packets->end(); it++) {
    01005           const CargoPacket *cp = *it;
    01006           if (cp->SourceStation() != station_id) {
    01007             bool added = false;
    01008
    01009             /* Enable the expand/hide button for this cargo type */
    01010             SetBit(*transfers, i);
    01011
    01012             /* Don't add cargo lines if not expanded */
    01013             if (!HasBit(this->cargo, i)) break;
    01014
    01015             /* Check if we already have this source in the list */
    01016             for (CargoDataList::iterator jt(cargolist->begin()); jt != cargolist->end(); jt++) {
    01017               CargoData *cd = &(*jt);
    01018               if (cd->cargo == i && cd->source == cp->SourceStation()) {
    01019                 cd->count += cp->Count();
    01020                 added = true;
    01021                 break;
    01022               }
    01023             }
    01024
    01025             if (!added) cargolist->push_back(CargoData(i, cp->SourceStation(), cp->Count()));
    01026           }
    01027         }
    01028       }
    01029     }
    01030   }
    01031
    01037   void DrawWaitingCargo(const Rect &r, const CargoDataList &cargolist, uint32 transfers) const
    01038   {
    01039     int y = r.top + WD_FRAMERECT_TOP;
    01040     int pos = this->vscroll.GetPosition();
    01041
    01042     const Station *st = Station::Get(this->window_number);
    01043     if (--pos < 0) {
    01044       StringID str = STR_JUST_NOTHING;
    01045       for (CargoID i = 0; i < NUM_CARGO; i++) {
    01046         if (!st->goods[i].cargo.Empty()) str = STR_EMPTY;
    01047       }
    01048       SetDParam(0, str);
    01049       DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_STATION_VIEW_WAITING_TITLE);
    01050       y += FONT_HEIGHT_NORMAL;
    01051     }
    01052
    01053     bool rtl = _dynlang.text_dir == TD_RTL;
    01054     int text_left    = rtl ? r.left + this->expand_shrink_width : r.left + WD_FRAMERECT_LEFT;
    01055     int text_right   = rtl ? r.right - WD_FRAMERECT_LEFT : r.right - this->expand_shrink_width;
    01056     int shrink_left  = rtl ? r.left + WD_FRAMERECT_LEFT : r.right - this->expand_shrink_width + WD_FRAMERECT_LEFT;
    01057     int shrink_right = rtl ? r.left + this->expand_shrink_width - WD_FRAMERECT_RIGHT : r.right - WD_FRAMERECT_RIGHT;
    01058
    01059
    01060     int maxrows = this->vscroll.GetCapacity();
    01061     for (CargoDataList::const_iterator it = cargolist.begin(); it != cargolist.end() && pos > -maxrows; ++it) {
    01062       if (--pos < 0) {
    01063         const CargoData *cd = &(*it);
    01064         if (cd->source == INVALID_STATION) {
    01065           /* Heading */
    01066           DrawCargoIcons(cd->cargo, cd->count, r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y);
    01067           SetDParam(0, cd->cargo);
    01068           SetDParam(1, cd->count);
    01069           if (HasBit(transfers, cd->cargo)) {
    01070             /* This cargo has transfers waiting so show the expand or shrink 'button' */
    01071             const char *sym = HasBit(this->cargo, cd->cargo) ? "-" : "+";
    01072             DrawString(text_left, text_right, y, STR_STATION_VIEW_WAITING_CARGO, TC_FROMSTRING, SA_RIGHT);
    01073             DrawString(shrink_left, shrink_right, y, sym, TC_YELLOW, SA_RIGHT);
    01074           } else {
    01075             DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_STATION_VIEW_WAITING_CARGO, TC_FROMSTRING, SA_RIGHT);
    01076           }
    01077         } else {
    01078           SetDParam(0, cd->cargo);
    01079           SetDParam(1, cd->count);
    01080           SetDParam(2, cd->source);
    01081           DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_STATION_VIEW_EN_ROUTE_FROM, TC_FROMSTRING, SA_RIGHT);
    01082         }
    01083
    01084         y += FONT_HEIGHT_NORMAL;
    01085       }
    01086     }
    01087   }
    01088
    01092   void DrawAcceptedCargo(const Rect &r) const
    01093   {
    01094     const Station *st = Station::Get(this->window_number);
    01095
    01096     uint32 cargo_mask = 0;
    01097     for (CargoID i = 0; i < NUM_CARGO; i++) {
    01098       if (HasBit(st->goods[i].acceptance_pickup, GoodsEntry::ACCEPTANCE)) SetBit(cargo_mask, i);
    01099     }
    01100     Rect s = {r.left + WD_FRAMERECT_LEFT, r.top + WD_FRAMERECT_TOP, r.right - WD_FRAMERECT_RIGHT, r.bottom - WD_FRAMERECT_BOTTOM};
    01101     DrawCargoListText(cargo_mask, s, STR_STATION_VIEW_ACCEPTS_CARGO);
    01102   }
    01103
    01107   void DrawCargoRatings(const Rect &r) const
    01108   {
    01109     const Station *st = Station::Get(this->window_number);
    01110     int y = r.top + WD_FRAMERECT_TOP;
    01111
    01112     DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_STATION_VIEW_CARGO_RATINGS_TITLE);
    01113     y += FONT_HEIGHT_NORMAL;
    01114
    01115     const CargoSpec *cs;
    01116     FOR_ALL_CARGOSPECS(cs) {
    01117       const GoodsEntry *ge = &st->goods[cs->Index()];
    01118       if (!HasBit(ge->acceptance_pickup, GoodsEntry::PICKUP)) continue;
    01119
    01120       SetDParam(0, cs->name);
    01121       SetDParam(2, ToPercent8(ge->rating));
    01122       SetDParam(1, STR_CARGO_RATING_APPALLING + (ge->rating >> 5));
    01123       DrawString(r.left + WD_FRAMERECT_LEFT + 6, r.right - WD_FRAMERECT_RIGHT - 6, y, STR_STATION_VIEW_CARGO_RATING);
    01124       y += FONT_HEIGHT_NORMAL;
    01125     }
    01126   }
    01127
    01128   void HandleCargoWaitingClick(int row)
    01129   {
    01130     if (row == 0) return;
    01131
    01132     for (CargoID c = 0; c < NUM_CARGO; c++) {
    01133       if (this->cargo_rows[c] == row) {
    01134         ToggleBit(this->cargo, c);
    01135         this->SetWidgetDirty(SVW_WAITING);
    01136         break;
    01137       }
    01138     }
    01139   }
    01140
    01141   virtual void OnClick(Point pt, int widget)
    01142   {
    01143     switch (widget) {
    01144       case SVW_WAITING:
    01145         this->HandleCargoWaitingClick((pt.y - this->GetWidget<NWidgetBase>(SVW_WAITING)->pos_y - WD_FRAMERECT_TOP) / FONT_HEIGHT_NORMAL + this->vscroll.GetPosition());
    01146         break;
    01147
    01148       case SVW_LOCATION:
    01149         if (_ctrl_pressed) {
    01150           ShowExtraViewPortWindow(Station::Get(this->window_number)->xy);
    01151         } else {
    01152           ScrollMainWindowToTile(Station::Get(this->window_number)->xy);
    01153         }
    01154         break;
    01155
    01156       case SVW_RATINGS: {
    01157         /* Swap between 'accepts' and 'ratings' view. */
    01158         int height_change;
    01159         NWidgetCore *nwi = this->GetWidget<NWidgetCore>(SVW_RATINGS);
    01160         if (this->GetWidget<NWidgetCore>(SVW_RATINGS)->widget_data == STR_STATION_VIEW_RATINGS_BUTTON) {
    01161           nwi->SetDataTip(STR_STATION_VIEW_ACCEPTS_BUTTON, STR_STATION_VIEW_ACCEPTS_TOOLTIP); // Switch to accepts view.
    01162           height_change = ALH_RATING - ALH_ACCEPTS;
    01163         } else {
    01164           nwi->SetDataTip(STR_STATION_VIEW_RATINGS_BUTTON, STR_STATION_VIEW_RATINGS_TOOLTIP); // Switch to ratings view.
    01165           height_change = ALH_ACCEPTS - ALH_RATING;
    01166         }
    01167         this->ReInit(0, height_change * FONT_HEIGHT_NORMAL);
    01168         break;
    01169       }
    01170
    01171       case SVW_RENAME:
    01172         SetDParam(0, this->window_number);
    01173         ShowQueryString(STR_STATION_NAME, STR_STATION_VIEW_RENAME_STATION_CAPTION, MAX_LENGTH_STATION_NAME_BYTES, MAX_LENGTH_STATION_NAME_PIXELS,
    01174             this, CS_ALPHANUMERAL, QSF_ENABLE_DEFAULT);
    01175         break;
    01176
    01177       case SVW_TRAINS: { // Show a list of scheduled trains to this station
    01178         const Station *st = Station::Get(this->window_number);
    01179         ShowVehicleListWindow(st->owner, VEH_TRAIN, (StationID)this->window_number);
    01180         break;
    01181       }
    01182
    01183       case SVW_ROADVEHS: { // Show a list of scheduled road-vehicles to this station
    01184         const Station *st = Station::Get(this->window_number);
    01185         ShowVehicleListWindow(st->owner, VEH_ROAD, (StationID)this->window_number);
    01186         break;
    01187       }
    01188
    01189       case SVW_PLANES: { // Show a list of scheduled aircraft to this station
    01190         const Station *st = Station::Get(this->window_number);
    01191         /* Since oilrigs have no owners, show the scheduled aircraft of local company */
    01192         Owner owner = (st->owner == OWNER_NONE) ? _local_company : st->owner;
    01193         ShowVehicleListWindow(owner, VEH_AIRCRAFT, (StationID)this->window_number);
    01194         break;
    01195       }
    01196
    01197       case SVW_SHIPS: { // Show a list of scheduled ships to this station
    01198         const Station *st = Station::Get(this->window_number);
    01199         /* Since oilrigs/bouys have no owners, show the scheduled ships of local company */
    01200         Owner owner = (st->owner == OWNER_NONE) ? _local_company : st->owner;
    01201         ShowVehicleListWindow(owner, VEH_SHIP, (StationID)this->window_number);
    01202         break;
    01203       }
    01204     }
    01205   }
    01206
    01207   virtual void OnQueryTextFinished(char *str)
    01208   {
    01209     if (str == NULL) return;
    01210
    01211     DoCommandP(0, this->window_number, 0, CMD_RENAME_STATION | CMD_MSG(STR_ERROR_CAN_T_RENAME_STATION), NULL, str);
    01212   }
    01213
    01214   virtual void OnResize()
    01215   {
    01216     this->vscroll.SetCapacityFromWidget(this, SVW_WAITING, WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM);
    01217   }
    01218 };
    01219
    01220
    01221 static const WindowDesc _station_view_desc(
    01222   WDP_AUTO, 249, 110,
    01223   WC_STATION_VIEW, WC_NONE,
    01224   WDF_UNCLICK_BUTTONS,
    01225   _nested_station_view_widgets, lengthof(_nested_station_view_widgets)
    01226 );
    01227
    01233 void ShowStationViewWindow(StationID station)
    01234 {
    01235   AllocateWindowDescFront<StationViewWindow>(&_station_view_desc, station);
    01236 }
    01237
    01239 struct TileAndStation {
    01240   TileIndex tile;
    01241   StationID station;
    01242 };
    01243
    01244 static SmallVector<TileAndStation, 8> _deleted_stations_nearby;
    01245 static SmallVector<StationID, 8> _stations_nearby_list;
    01246
    01254 template <class T>
    01255 static bool AddNearbyStation(TileIndex tile, void *user_data)
    01256 {
    01257   TileArea *ctx = (TileArea *)user_data;
    01258
    01259   /* First check if there were deleted stations here */
    01260   for (uint i = 0; i < _deleted_stations_nearby.Length(); i++) {
    01261     TileAndStation *ts = _deleted_stations_nearby.Get(i);
    01262     if (ts->tile == tile) {
    01263       *_stations_nearby_list.Append() = _deleted_stations_nearby[i].station;
    01264       _deleted_stations_nearby.Erase(ts);
    01265       i--;
    01266     }
    01267   }
    01268
    01269   /* Check if own station and if we stay within station spread */
    01270   if (!IsTileType(tile, MP_STATION)) return false;
    01271
    01272   StationID sid = GetStationIndex(tile);
    01273
    01274   /* This station is (likely) a waypoint */
    01275   if (!T::IsValidID(sid)) return false;
    01276
    01277   T *st = T::Get(sid);
    01278   if (st->owner != _local_company || _stations_nearby_list.Contains(sid)) return false;
    01279
    01280   if (st->rect.BeforeAddRect(ctx->tile, ctx->w, ctx->h, StationRect::ADD_TEST)) {
    01281     *_stations_nearby_list.Append() = sid;
    01282   }
    01283
    01284   return false; // We want to include *all* nearby stations
    01285 }
    01286
    01296 template <class T>
    01297 static const T *FindStationsNearby(TileArea ta, bool distant_join)
    01298 {
    01299   TileArea ctx = ta;
    01300
    01301   _stations_nearby_list.Clear();
    01302   _deleted_stations_nearby.Clear();
    01303
    01304   /* Check the inside, to return, if we sit on another station */
    01305   TILE_AREA_LOOP(t, ta) {
    01306     if (t < MapSize() && IsTileType(t, MP_STATION) && T::IsValidID(GetStationIndex(t))) return T::GetByTile(t);
    01307   }
    01308
    01309   /* Look for deleted stations */
    01310   const BaseStation *st;
    01311   FOR_ALL_BASE_STATIONS(st) {
    01312     if (T::IsExpected(st) && !st->IsInUse() && st->owner == _local_company) {
    01313       /* Include only within station spread (yes, it is strictly less than) */
    01314       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) {
    01315         TileAndStation *ts = _deleted_stations_nearby.Append();
    01316         ts->tile = st->xy;
    01317         ts->station = st->index;
    01318
    01319         /* Add the station when it's within where we're going to build */
    01320         if (IsInsideBS(TileX(st->xy), TileX(ctx.tile), ctx.w) &&
    01321             IsInsideBS(TileY(st->xy), TileY(ctx.tile), ctx.h)) {
    01322           AddNearbyStation<T>(st->xy, &ctx);
    01323         }
    01324       }
    01325     }
    01326   }
    01327
    01328   /* Only search tiles where we have a chance to stay within the station spread.
    01329    * The complete check needs to be done in the callback as we don't know the
    01330    * extent of the found station, yet. */
    01331   if (distant_join && min(ta.w, ta.h) >= _settings_game.station.station_spread) return NULL;
    01332   uint max_dist = distant_join ? _settings_game.station.station_spread - min(ta.w, ta.h) : 1;
    01333
    01334   TileIndex tile = TILE_ADD(ctx.tile, TileOffsByDir(DIR_N));
    01335   CircularTileSearch(&tile, max_dist, ta.w, ta.h, AddNearbyStation<T>, &ctx);
    01336
    01337   return NULL;
    01338 }
    01339
    01340 enum JoinStationWidgets {
    01341   JSW_WIDGET_CAPTION,
    01342   JSW_PANEL,
    01343   JSW_SCROLLBAR,
    01344 };
    01345
    01346 static const NWidgetPart _nested_select_station_widgets[] = {
    01347   NWidget(NWID_HORIZONTAL),
    01348     NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
    01349     NWidget(WWT_CAPTION, COLOUR_DARK_GREEN, JSW_WIDGET_CAPTION), SetDataTip(STR_JOIN_STATION_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    01350   EndContainer(),
    01351   NWidget(NWID_HORIZONTAL),
    01352     NWidget(WWT_PANEL, COLOUR_DARK_GREEN, JSW_PANEL), SetResize(1, 0), EndContainer(),
    01353     NWidget(NWID_VERTICAL),
    01354       NWidget(WWT_SCROLLBAR, COLOUR_DARK_GREEN, JSW_SCROLLBAR),
    01355       NWidget(WWT_RESIZEBOX, COLOUR_DARK_GREEN),
    01356     EndContainer(),
    01357   EndContainer(),
    01358 };
    01359
    01364 template <class T>
    01365 struct SelectStationWindow : Window {
    01366   CommandContainer select_station_cmd;
    01367   TileArea area;
    01368
    01369   SelectStationWindow(const WindowDesc *desc, CommandContainer cmd, TileArea ta) :
    01370     Window(),
    01371     select_station_cmd(cmd),
    01372     area(ta)
    01373   {
    01374     this->CreateNestedTree(desc);
    01375     this->GetWidget<NWidgetCore>(JSW_WIDGET_CAPTION)->widget_data = T::EXPECTED_FACIL == FACIL_WAYPOINT ? STR_JOIN_WAYPOINT_CAPTION : STR_JOIN_STATION_CAPTION;
    01376     this->FinishInitNested(desc, 0);
    01377     this->OnInvalidateData(0);
    01378   }
    01379
    01380   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
    01381   {
    01382     if (widget != JSW_PANEL) return;
    01383
    01384     /* Determine the widest string */
    01385     Dimension d = GetStringBoundingBox(T::EXPECTED_FACIL == FACIL_WAYPOINT ? STR_JOIN_WAYPOINT_CREATE_SPLITTED_WAYPOINT : STR_JOIN_STATION_CREATE_SPLITTED_STATION);
    01386     for (uint i = 0; i < _stations_nearby_list.Length(); i++) {
    01387       const T *st = T::Get(_stations_nearby_list[i]);
    01388       SetDParam(0, st->index);
    01389       SetDParam(1, st->facilities);
    01390       d = maxdim(d, GetStringBoundingBox(T::EXPECTED_FACIL == FACIL_WAYPOINT ? STR_STATION_LIST_WAYPOINT : STR_STATION_LIST_STATION));
    01391     }
    01392
    01393     resize->height = d.height;
    01394     d.height *= 5;
    01395     d.width += WD_FRAMERECT_RIGHT + WD_FRAMERECT_LEFT;
    01396     d.height += WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
    01397     *size = d;
    01398   }
    01399
    01400   virtual void OnPaint()
    01401   {
    01402     this->DrawWidgets();
    01403   }
    01404
    01405   virtual void DrawWidget(const Rect &r, int widget) const
    01406   {
    01407     if (widget != JSW_PANEL) return;
    01408
    01409     uint y = r.top + WD_FRAMERECT_TOP;
    01410     if (this->vscroll.GetPosition() == 0) {
    01411       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);
    01412       y += this->resize.step_height;
    01413     }
    01414
    01415     for (uint i = max<uint>(1, this->vscroll.GetPosition()); i <= _stations_nearby_list.Length(); ++i, y += this->resize.step_height) {
    01416       /* Don't draw anything if it extends past the end of the window. */
    01417       if (i - this->vscroll.GetPosition() >= this->vscroll.GetCapacity()) break;
    01418
    01419       const T *st = T::Get(_stations_nearby_list[i - 1]);
    01420       SetDParam(0, st->index);
    01421       SetDParam(1, st->facilities);
    01422       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);
    01423     }
    01424   }
    01425
    01426   virtual void OnClick(Point pt, int widget)
    01427   {
    01428     if (widget != JSW_PANEL) return;
    01429
    01430     uint32 st_index = (pt.y - this->GetWidget<NWidgetBase>(JSW_PANEL)->pos_y - WD_FRAMERECT_TOP) / this->resize.step_height + this->vscroll.GetPosition();
    01431     bool distant_join = (st_index > 0);
    01432     if (distant_join) st_index--;
    01433
    01434     if (distant_join && st_index >= _stations_nearby_list.Length()) return;
    01435
    01436     /* Insert station to be joined into stored command */
    01437     SB(this->select_station_cmd.p2, 16, 16,
    01438        (distant_join ? _stations_nearby_list[st_index] : NEW_STATION));
    01439
    01440     /* Execute stored Command */
    01441     DoCommandP(&this->select_station_cmd);
    01442
    01443     /* Close Window; this might cause double frees! */
    01444     DeleteWindowById(WC_SELECT_STATION, 0);
    01445   }
    01446
    01447   virtual void OnTick()
    01448   {
    01449     if (_thd.dirty & 2) {
    01450       _thd.dirty &= ~2;
    01451       this->SetDirty();
    01452     }
    01453   }
    01454
    01455   virtual void OnResize()
    01456   {
    01457     this->vscroll.SetCapacityFromWidget(this, JSW_PANEL, WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM);
    01458   }
    01459
    01460   virtual void OnInvalidateData(int data)
    01461   {
    01462     FindStationsNearby<T>(this->area, true);
    01463     this->vscroll.SetCount(_stations_nearby_list.Length() + 1);
    01464     this->SetDirty();
    01465   }
    01466 };
    01467
    01468 static const WindowDesc _select_station_desc(
    01469   WDP_AUTO, 200, 180,
    01470   WC_SELECT_STATION, WC_NONE,
    01471   WDF_CONSTRUCTION,
    01472   _nested_select_station_widgets, lengthof(_nested_select_station_widgets)
    01473 );
    01474
    01475
    01483 template <class T>
    01484 static bool StationJoinerNeeded(CommandContainer cmd, TileArea ta)
    01485 {
    01486   /* Only show selection if distant join is enabled in the settings */
    01487   if (!_settings_game.station.distant_join_stations) return false;
    01488
    01489   /* If a window is already opened and we didn't ctrl-click,
    01490    * return true (i.e. just flash the old window) */
    01491   Window *selection_window = FindWindowById(WC_SELECT_STATION, 0);
    01492   if (selection_window != NULL) {
    01493     if (!_ctrl_pressed) return true;
    01494
    01495     /* Abort current distant-join and start new one */
    01496     delete selection_window;
    01497     UpdateTileSelection();
    01498   }
    01499
    01500   /* only show the popup, if we press ctrl */
    01501   if (!_ctrl_pressed) return false;
    01502
    01503   /* Now check if we could build there */
    01504   if (CmdFailed(DoCommand(&cmd, CommandFlagsToDCFlags(GetCommandFlags(cmd.cmd))))) return false;
    01505
    01506   /* Test for adjacent station or station below selection.
    01507    * If adjacent-stations is disabled and we are building next to a station, do not show the selection window.
    01508    * but join the other station immediatelly. */
    01509   const T *st = FindStationsNearby<T>(ta, false);
    01510   return st == NULL && (_settings_game.station.adjacent_stations || _stations_nearby_list.Length() == 0);
    01511 }
    01512
    01519 template <class T>
    01520 void ShowSelectBaseStationIfNeeded(CommandContainer cmd, TileArea ta)
    01521 {
    01522   if (StationJoinerNeeded<T>(cmd, ta)) {
    01523     if (!_settings_client.gui.persistent_buildingtools) ResetObjectToPlace();
    01524     if (BringWindowToFrontById(WC_SELECT_STATION, 0)) return;
    01525     new SelectStationWindow<T>(&_select_station_desc, cmd, ta);
    01526   } else {
    01527     DoCommandP(&cmd);
    01528   }
    01529 }
    01530
    01536 void ShowSelectStationIfNeeded(CommandContainer cmd, TileArea ta)
    01537 {
    01538   ShowSelectBaseStationIfNeeded<Station>(cmd, ta);
    01539 }
    01540
    01546 void ShowSelectWaypointIfNeeded(CommandContainer cmd, TileArea ta)
    01547 {
    01548   ShowSelectBaseStationIfNeeded<Waypoint>(cmd, ta);
    01549 }