# station_gui.cpp

Go to the documentation of this file.

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