# rail_gui.cpp

Go to the documentation of this file.

    00001 /* $Id$ */
    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 "gui.h"
    00014 #include "window_gui.h"
    00015 #include "station_gui.h"
    00016 #include "terraform_gui.h"
    00017 #include "viewport_func.h"
    00018 #include "command_func.h"
    00019 #include "waypoint_func.h"
    00020 #include "newgrf_station.h"
    00021 #include "company_base.h"
    00022 #include "strings_func.h"
    00023 #include "window_func.h"
    00024 #include "date_func.h"
    00025 #include "sound_func.h"
    00026 #include "company_func.h"
    00027 #include "widgets/dropdown_type.h"
    00028 #include "tunnelbridge.h"
    00029 #include "tilehighlight_func.h"
    00030 #include "spritecache.h"
    00031 #include "core/geometry_func.hpp"
    00032 #include "hotkeys.h"
    00033 #include "engine_base.h"
    00034
    00035 #include "station_map.h"
    00036 #include "tunnelbridge_map.h"
    00037
    00038 #include "table/strings.h"
    00039
    00040 static RailType _cur_railtype;
    00041 static bool _remove_button_clicked;
    00042 static DiagDirection _build_depot_direction;
    00043 static byte _waypoint_count = 1;
    00044 static byte _cur_waypoint_type;
    00045 static bool _convert_signal_button;
    00046 static SignalVariant _cur_signal_variant;
    00047 static SignalType _cur_signal_type;
    00048
    00049 /* Map the setting: default_signal_type to the corresponding signal type */
    00050 static const SignalType _default_signal_type[] = {SIGTYPE_NORMAL, SIGTYPE_PBS, SIGTYPE_PBS_ONEWAY};
    00051
    00052 struct RailStationGUISettings {
    00053   Axis orientation;
    00054
    00055   bool newstations;
    00056   StationClassID station_class;
    00057   byte station_type;
    00058   byte station_count;
    00059 };
    00060 static RailStationGUISettings _railstation;
    00061
    00062
    00063 static void HandleStationPlacement(TileIndex start, TileIndex end);
    00064 static void ShowBuildTrainDepotPicker(Window *parent);
    00065 static void ShowBuildWaypointPicker(Window *parent);
    00066 static void ShowStationBuilder(Window *parent);
    00067 static void ShowSignalBuilder(Window *parent);
    00068
    00069 void CcPlaySound1E(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
    00070 {
    00071   if (result.Succeeded()) SndPlayTileFx(SND_20_SPLAT_2, tile);
    00072 }
    00073
    00074 static void GenericPlaceRail(TileIndex tile, int cmd)
    00075 {
    00076   DoCommandP(tile, _cur_railtype, cmd,
    00077       _remove_button_clicked ?
    00078       CMD_REMOVE_SINGLE_RAIL | CMD_MSG(STR_ERROR_CAN_T_REMOVE_RAILROAD_TRACK) :
    00079       CMD_BUILD_SINGLE_RAIL | CMD_MSG(STR_ERROR_CAN_T_BUILD_RAILROAD_TRACK),
    00080       CcPlaySound1E);
    00081 }
    00082
    00090 static void PlaceExtraDepotRail(TileIndex tile, DiagDirection dir, Track track)
    00091 {
    00092   if (GetRailTileType(tile) != RAIL_TILE_NORMAL) return;
    00093   if ((GetTrackBits(tile) & DiagdirReachesTracks(dir)) == 0) return;
    00094
    00095   DoCommandP(tile, _cur_railtype, track, CMD_BUILD_SINGLE_RAIL);
    00096 }
    00097
    00099 static const Track _place_depot_extra_track[12] = {
    00100   TRACK_LEFT,  TRACK_UPPER, TRACK_UPPER, TRACK_RIGHT, // First additional track for directions 0..3
    00101   TRACK_X,     TRACK_Y,     TRACK_X,     TRACK_Y,     // Second additional track
    00102   TRACK_LOWER, TRACK_LEFT,  TRACK_RIGHT, TRACK_LOWER, // Third additional track
    00103 };
    00104
    00106 static const DiagDirection _place_depot_extra_dir[12] = {
    00107   DIAGDIR_SE, DIAGDIR_SW, DIAGDIR_SE, DIAGDIR_SW,
    00108   DIAGDIR_SW, DIAGDIR_NW, DIAGDIR_NE, DIAGDIR_SE,
    00109   DIAGDIR_NW, DIAGDIR_NE, DIAGDIR_NW, DIAGDIR_NE,
    00110 };
    00111
    00112 void CcRailDepot(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
    00113 {
    00114   if (result.Failed()) return;
    00115
    00116   DiagDirection dir = (DiagDirection)p2;
    00117
    00118   SndPlayTileFx(SND_20_SPLAT_2, tile);
    00119   if (!_settings_client.gui.persistent_buildingtools) ResetObjectToPlace();
    00120
    00121   tile += TileOffsByDiagDir(dir);
    00122
    00123   if (IsTileType(tile, MP_RAILWAY)) {
    00124     PlaceExtraDepotRail(tile, _place_depot_extra_dir[dir], _place_depot_extra_track[dir]);
    00125     PlaceExtraDepotRail(tile, _place_depot_extra_dir[dir + 4], _place_depot_extra_track[dir + 4]);
    00126     PlaceExtraDepotRail(tile, _place_depot_extra_dir[dir + 8], _place_depot_extra_track[dir + 8]);
    00127   }
    00128 }
    00129
    00134 static void PlaceRail_Waypoint(TileIndex tile)
    00135 {
    00136   if (_remove_button_clicked) {
    00137     VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_REMOVE_STATION);
    00138     return;
    00139   }
    00140
    00141   Axis axis = GetAxisForNewWaypoint(tile);
    00142   if (IsValidAxis(axis)) {
    00143     /* Valid tile for waypoints */
    00144     VpStartPlaceSizing(tile, axis == AXIS_X ? VPM_FIX_X : VPM_FIX_Y, DDSP_BUILD_STATION);
    00145   } else {
    00146     /* Tile where we can't build rail waypoints. This is always going to fail,
    00147      * but provides the user with a proper error message. */
    00148     DoCommandP(tile, 1 << 8 | 1 << 16, STAT_CLASS_WAYP | INVALID_STATION << 16, CMD_BUILD_RAIL_WAYPOINT | CMD_MSG(STR_ERROR_CAN_T_BUILD_TRAIN_WAYPOINT));
    00149   }
    00150 }
    00151
    00152 void CcStation(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
    00153 {
    00154   if (result.Failed()) return;
    00155
    00156   SndPlayTileFx(SND_20_SPLAT_2, tile);
    00157   /* Only close the station builder window if the default station and non persistent building is chosen. */
    00158   if (_railstation.station_class == STAT_CLASS_DFLT && _railstation.station_type == 0 && !_settings_client.gui.persistent_buildingtools) ResetObjectToPlace();
    00159 }
    00160
    00165 static void PlaceRail_Station(TileIndex tile)
    00166 {
    00167   if (_remove_button_clicked) {
    00168     VpStartPlaceSizing(tile, VPM_X_AND_Y_LIMITED, DDSP_REMOVE_STATION);
    00169     VpSetPlaceSizingLimit(-1);
    00170   } else if (_settings_client.gui.station_dragdrop) {
    00171     VpStartPlaceSizing(tile, VPM_X_AND_Y_LIMITED, DDSP_BUILD_STATION);
    00172     VpSetPlaceSizingLimit(_settings_game.station.station_spread);
    00173   } else {
    00174     uint32 p1 = _cur_railtype | _railstation.orientation << 4 | _settings_client.gui.station_numtracks << 8 | _settings_client.gui.station_platlength << 16 | _ctrl_pressed << 24;
    00175     uint32 p2 = _railstation.station_class | _railstation.station_type << 8 | INVALID_STATION << 16;
    00176
    00177     int w = _settings_client.gui.station_numtracks;
    00178     int h = _settings_client.gui.station_platlength;
    00179     if (!_railstation.orientation) Swap(w, h);
    00180
    00181     CommandContainer cmdcont = { tile, p1, p2, CMD_BUILD_RAIL_STATION | CMD_MSG(STR_ERROR_CAN_T_BUILD_RAILROAD_STATION), CcStation, "" };
    00182     ShowSelectStationIfNeeded(cmdcont, TileArea(tile, w, h));
    00183   }
    00184 }
    00185
    00191 static void GenericPlaceSignals(TileIndex tile)
    00192 {
    00193   TrackBits trackbits = TrackStatusToTrackBits(GetTileTrackStatus(tile, TRANSPORT_RAIL, 0));
    00194
    00195   if (trackbits & TRACK_BIT_VERT) { // N-S direction
    00196     trackbits = (_tile_fract_coords.x <= _tile_fract_coords.y) ? TRACK_BIT_RIGHT : TRACK_BIT_LEFT;
    00197   }
    00198
    00199   if (trackbits & TRACK_BIT_HORZ) { // E-W direction
    00200     trackbits = (_tile_fract_coords.x + _tile_fract_coords.y <= 15) ? TRACK_BIT_UPPER : TRACK_BIT_LOWER;
    00201   }
    00202
    00203   Track track = FindFirstTrack(trackbits);
    00204
    00205   if (_remove_button_clicked) {
    00206     DoCommandP(tile, track, 0, CMD_REMOVE_SIGNALS | CMD_MSG(STR_ERROR_CAN_T_REMOVE_SIGNALS_FROM), CcPlaySound1E);
    00207   } else {
    00208     const Window *w = FindWindowById(WC_BUILD_SIGNAL, 0);
    00209
    00210     /* Map the setting cycle_signal_types to the lower and upper allowed signal type. */
    00211     static const uint cycle_bounds[] = {SIGTYPE_NORMAL | (SIGTYPE_LAST_NOPBS << 3), SIGTYPE_PBS | (SIGTYPE_LAST << 3), SIGTYPE_NORMAL | (SIGTYPE_LAST << 3)};
    00212
    00213     /* various bitstuffed elements for CmdBuildSingleSignal() */
    00214     uint32 p1 = track;
    00215
    00216     if (w != NULL) {
    00217       /* signal GUI is used */
    00218       SB(p1, 3, 1, _ctrl_pressed);
    00219       SB(p1, 4, 1, _cur_signal_variant);
    00220       SB(p1, 5, 3, _cur_signal_type);
    00221       SB(p1, 8, 1, _convert_signal_button);
    00222       SB(p1, 9, 6, cycle_bounds[_settings_client.gui.cycle_signal_types]);
    00223     } else {
    00224       SB(p1, 3, 1, _ctrl_pressed);
    00225       SB(p1, 4, 1, (_cur_year < _settings_client.gui.semaphore_build_before ? SIG_SEMAPHORE : SIG_ELECTRIC));
    00226       SB(p1, 5, 3, _default_signal_type[_settings_client.gui.default_signal_type]);
    00227       SB(p1, 8, 1, 0);
    00228       SB(p1, 9, 6, cycle_bounds[_settings_client.gui.cycle_signal_types]);
    00229     }
    00230
    00231     DoCommandP(tile, p1, 0, CMD_BUILD_SIGNALS |
    00232         CMD_MSG((w != NULL && _convert_signal_button) ? STR_ERROR_SIGNAL_CAN_T_CONVERT_SIGNALS_HERE : STR_ERROR_CAN_T_BUILD_SIGNALS_HERE),
    00233         CcPlaySound1E);
    00234   }
    00235 }
    00236
    00242 static void PlaceRail_Bridge(TileIndex tile, Window *w)
    00243 {
    00244   if (IsBridgeTile(tile)) {
    00245     TileIndex other_tile = GetOtherTunnelBridgeEnd(tile);
    00246     Point pt = {0, 0};
    00247     w->OnPlaceMouseUp(VPM_X_OR_Y, DDSP_BUILD_BRIDGE, pt, tile, other_tile);
    00248   } else {
    00249     VpStartPlaceSizing(tile, VPM_X_OR_Y, DDSP_BUILD_BRIDGE);
    00250   }
    00251 }
    00252
    00254 void CcBuildRailTunnel(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
    00255 {
    00256   if (result.Succeeded()) {
    00257     SndPlayTileFx(SND_20_SPLAT_2, tile);
    00258     if (!_settings_client.gui.persistent_buildingtools) ResetObjectToPlace();
    00259   } else {
    00260     SetRedErrorSquare(_build_tunnel_endtile);
    00261   }
    00262 }
    00263
    00264
    00266 enum RailToolbarWidgets {
    00267   RTW_CAPTION,
    00268   RTW_BUILD_NS,
    00269   RTW_BUILD_X,
    00270   RTW_BUILD_EW,
    00271   RTW_BUILD_Y,
    00272   RTW_AUTORAIL,
    00273   RTW_DEMOLISH,
    00274   RTW_BUILD_DEPOT,
    00275   RTW_BUILD_WAYPOINT,
    00276   RTW_BUILD_STATION,
    00277   RTW_BUILD_SIGNALS,
    00278   RTW_BUILD_BRIDGE,
    00279   RTW_BUILD_TUNNEL,
    00280   RTW_REMOVE,
    00281   RTW_CONVERT_RAIL,
    00282 };
    00283
    00284
    00289 static void ToggleRailButton_Remove(Window *w)
    00290 {
    00291   DeleteWindowById(WC_SELECT_STATION, 0);
    00292   w->ToggleWidgetLoweredState(RTW_REMOVE);
    00293   w->SetWidgetDirty(RTW_REMOVE);
    00294   _remove_button_clicked = w->IsWidgetLowered(RTW_REMOVE);
    00295   SetSelectionRed(_remove_button_clicked);
    00296 }
    00297
    00303 static bool RailToolbar_CtrlChanged(Window *w)
    00304 {
    00305   if (w->IsWidgetDisabled(RTW_REMOVE)) return false;
    00306
    00307   /* allow ctrl to switch remove mode only for these widgets */
    00308   for (uint i = RTW_BUILD_NS; i <= RTW_BUILD_STATION; i++) {
    00309     if ((i <= RTW_AUTORAIL || i >= RTW_BUILD_WAYPOINT) && w->IsWidgetLowered(i)) {
    00310       ToggleRailButton_Remove(w);
    00311       return true;
    00312     }
    00313   }
    00314
    00315   return false;
    00316 }
    00317
    00318
    00324 static void BuildRailClick_Remove(Window *w)
    00325 {
    00326   if (w->IsWidgetDisabled(RTW_REMOVE)) return;
    00327   ToggleRailButton_Remove(w);
    00328   SndPlayFx(SND_15_BEEP);
    00329
    00330   /* handle station builder */
    00331   if (w->IsWidgetLowered(RTW_BUILD_STATION)) {
    00332     if (_remove_button_clicked) {
    00333       /* starting drag & drop remove */
    00334       if (!_settings_client.gui.station_dragdrop) {
    00335         SetTileSelectSize(1, 1);
    00336       } else {
    00337         VpSetPlaceSizingLimit(-1);
    00338       }
    00339     } else {
    00340       /* starting station build mode */
    00341       if (!_settings_client.gui.station_dragdrop) {
    00342         int x = _settings_client.gui.station_numtracks;
    00343         int y = _settings_client.gui.station_platlength;
    00344         if (_railstation.orientation == 0) Swap(x, y);
    00345         SetTileSelectSize(x, y);
    00346       } else {
    00347         VpSetPlaceSizingLimit(_settings_game.station.station_spread);
    00348       }
    00349     }
    00350   }
    00351 }
    00352
    00353 static void DoRailroadTrack(int mode)
    00354 {
    00355   DoCommandP(TileVirtXY(_thd.selstart.x, _thd.selstart.y), TileVirtXY(_thd.selend.x, _thd.selend.y), _cur_railtype | (mode << 4),
    00356       _remove_button_clicked ?
    00357       CMD_REMOVE_RAILROAD_TRACK | CMD_MSG(STR_ERROR_CAN_T_REMOVE_RAILROAD_TRACK) :
    00358       CMD_BUILD_RAILROAD_TRACK  | CMD_MSG(STR_ERROR_CAN_T_BUILD_RAILROAD_TRACK));
    00359 }
    00360
    00361 static void HandleAutodirPlacement()
    00362 {
    00363   int trackstat = _thd.drawstyle & HT_DIR_MASK; // 0..5
    00364
    00365   if (_thd.drawstyle & HT_RAIL) { // one tile case
    00366     GenericPlaceRail(TileVirtXY(_thd.selend.x, _thd.selend.y), trackstat);
    00367     return;
    00368   }
    00369
    00370   DoRailroadTrack(trackstat);
    00371 }
    00372
    00379 static void HandleAutoSignalPlacement()
    00380 {
    00381   uint32 p2 = GB(_thd.drawstyle, 0, 3); // 0..5
    00382
    00383   if ((_thd.drawstyle & HT_DRAG_MASK) == HT_RECT) { // one tile case
    00384     GenericPlaceSignals(TileVirtXY(_thd.selend.x, _thd.selend.y));
    00385     return;
    00386   }
    00387
    00388   const Window *w = FindWindowById(WC_BUILD_SIGNAL, 0);
    00389
    00390   if (w != NULL) {
    00391     /* signal GUI is used */
    00392     SB(p2,  3, 1, 0);
    00393     SB(p2,  4, 1, _cur_signal_variant);
    00394     SB(p2,  6, 1, _ctrl_pressed);
    00395     SB(p2,  7, 3, _cur_signal_type);
    00396     SB(p2, 24, 8, _settings_client.gui.drag_signals_density);
    00397   } else {
    00398     SB(p2,  3, 1, 0);
    00399     SB(p2,  4, 1, (_cur_year < _settings_client.gui.semaphore_build_before ? SIG_SEMAPHORE : SIG_ELECTRIC));
    00400     SB(p2,  6, 1, _ctrl_pressed);
    00401     SB(p2,  7, 3, _default_signal_type[_settings_client.gui.default_signal_type]);
    00402     SB(p2, 24, 8, _settings_client.gui.drag_signals_density);
    00403   }
    00404
    00405   /* _settings_client.gui.drag_signals_density is given as a parameter such that each user
    00406    * in a network game can specify his/her own signal density */
    00407   DoCommandP(TileVirtXY(_thd.selstart.x, _thd.selstart.y), TileVirtXY(_thd.selend.x, _thd.selend.y), p2,
    00408       _remove_button_clicked ?
    00409       CMD_REMOVE_SIGNAL_TRACK | CMD_MSG(STR_ERROR_CAN_T_REMOVE_SIGNALS_FROM) :
    00410       CMD_BUILD_SIGNAL_TRACK  | CMD_MSG(STR_ERROR_CAN_T_BUILD_SIGNALS_HERE),
    00411       CcPlaySound1E);
    00412 }
    00413
    00414
    00416 struct BuildRailToolbarWindow : Window {
    00417   RailType railtype;
    00418   int last_user_action;
    00419
    00420   BuildRailToolbarWindow(const WindowDesc *desc, WindowNumber window_number, RailType railtype) : Window()
    00421   {
    00422     this->InitNested(desc);
    00423     this->SetupRailToolbar(railtype);
    00424     this->DisableWidget(RTW_REMOVE);
    00425     this->last_user_action = WIDGET_LIST_END;
    00426
    00427     if (_settings_client.gui.link_terraform_toolbar) ShowTerraformToolbar(this);
    00428   }
    00429
    00430   ~BuildRailToolbarWindow()
    00431   {
    00432     if (_settings_client.gui.link_terraform_toolbar) DeleteWindowById(WC_SCEN_LAND_GEN, 0, false);
    00433   }
    00434
    00439   void SetupRailToolbar(RailType railtype)
    00440   {
    00441     this->railtype = railtype;
    00442     const RailtypeInfo *rti = GetRailTypeInfo(railtype);
    00443
    00444     assert(railtype < RAILTYPE_END);
    00445     this->GetWidget<NWidgetCore>(RTW_BUILD_NS)->widget_data     = rti->gui_sprites.build_ns_rail;
    00446     this->GetWidget<NWidgetCore>(RTW_BUILD_X)->widget_data      = rti->gui_sprites.build_x_rail;
    00447     this->GetWidget<NWidgetCore>(RTW_BUILD_EW)->widget_data     = rti->gui_sprites.build_ew_rail;
    00448     this->GetWidget<NWidgetCore>(RTW_BUILD_Y)->widget_data      = rti->gui_sprites.build_y_rail;
    00449     this->GetWidget<NWidgetCore>(RTW_AUTORAIL)->widget_data     = rti->gui_sprites.auto_rail;
    00450     this->GetWidget<NWidgetCore>(RTW_BUILD_DEPOT)->widget_data  = rti->gui_sprites.build_depot;
    00451     this->GetWidget<NWidgetCore>(RTW_CONVERT_RAIL)->widget_data = rti->gui_sprites.convert_rail;
    00452     this->GetWidget<NWidgetCore>(RTW_BUILD_TUNNEL)->widget_data = rti->gui_sprites.build_tunnel;
    00453   }
    00454
    00459   void ModifyRailType(RailType railtype)
    00460   {
    00461     this->SetupRailToolbar(railtype);
    00462     this->ReInit();
    00463   }
    00464
    00465   void UpdateRemoveWidgetStatus(int clicked_widget)
    00466   {
    00467     switch (clicked_widget) {
    00468       case RTW_REMOVE:
    00469         /* If it is the removal button that has been clicked, do nothing,
    00470          * as it is up to the other buttons to drive removal status */
    00471         return;
    00472
    00473       case RTW_BUILD_NS:
    00474       case RTW_BUILD_X:
    00475       case RTW_BUILD_EW:
    00476       case RTW_BUILD_Y:
    00477       case RTW_AUTORAIL:
    00478       case RTW_BUILD_WAYPOINT:
    00479       case RTW_BUILD_STATION:
    00480       case RTW_BUILD_SIGNALS:
    00481         /* Removal button is enabled only if the rail/signal/waypoint/station
    00482          * button is still lowered.  Once raised, it has to be disabled */
    00483         this->SetWidgetDisabledState(RTW_REMOVE, !this->IsWidgetLowered(clicked_widget));
    00484         break;
    00485
    00486       default:
    00487         /* When any other buttons than rail/signal/waypoint/station, raise and
    00488          * disable the removal button */
    00489         this->DisableWidget(RTW_REMOVE);
    00490         this->RaiseWidget(RTW_REMOVE);
    00491         break;
    00492     }
    00493   }
    00494
    00495   virtual void SetStringParameters(int widget) const
    00496   {
    00497     if (widget == RTW_CAPTION) {
    00498       const RailtypeInfo *rti = GetRailTypeInfo(this->railtype);
    00499       if (rti->max_speed > 0) {
    00500         SetDParam(0, STR_TOOLBAR_RAILTYPE_VELOCITY);
    00501         SetDParam(1, rti->strings.toolbar_caption);
    00502         SetDParam(2, rti->max_speed);
    00503       } else {
    00504         SetDParam(0, rti->strings.toolbar_caption);
    00505       }
    00506     }
    00507   }
    00508
    00509   virtual void OnClick(Point pt, int widget, int click_count)
    00510   {
    00511     if (widget < RTW_BUILD_NS) return;
    00512
    00513     _remove_button_clicked = false;
    00514     switch (widget) {
    00515       case RTW_BUILD_NS:
    00516         HandlePlacePushButton(this, RTW_BUILD_NS, GetRailTypeInfo(_cur_railtype)->cursor.rail_ns, HT_LINE | HT_DIR_VL);
    00517         this->last_user_action = widget;
    00518         break;
    00519
    00520       case RTW_BUILD_X:
    00521         HandlePlacePushButton(this, RTW_BUILD_X, GetRailTypeInfo(_cur_railtype)->cursor.rail_swne, HT_LINE | HT_DIR_X);
    00522         this->last_user_action = widget;
    00523         break;
    00524
    00525       case RTW_BUILD_EW:
    00526         HandlePlacePushButton(this, RTW_BUILD_EW, GetRailTypeInfo(_cur_railtype)->cursor.rail_ew, HT_LINE | HT_DIR_HL);
    00527         this->last_user_action = widget;
    00528         break;
    00529
    00530       case RTW_BUILD_Y:
    00531         HandlePlacePushButton(this, RTW_BUILD_Y, GetRailTypeInfo(_cur_railtype)->cursor.rail_nwse, HT_LINE | HT_DIR_Y);
    00532         this->last_user_action = widget;
    00533         break;
    00534
    00535       case RTW_AUTORAIL:
    00536         HandlePlacePushButton(this, RTW_AUTORAIL, GetRailTypeInfo(_cur_railtype)->cursor.autorail, HT_RAIL);
    00537         this->last_user_action = widget;
    00538         break;
    00539
    00540       case RTW_DEMOLISH:
    00541         HandlePlacePushButton(this, RTW_DEMOLISH, ANIMCURSOR_DEMOLISH, HT_RECT | HT_DIAGONAL);
    00542         this->last_user_action = widget;
    00543         break;
    00544
    00545       case RTW_BUILD_DEPOT:
    00546         if (HandlePlacePushButton(this, RTW_BUILD_DEPOT, GetRailTypeInfo(_cur_railtype)->cursor.depot, HT_RECT)) {
    00547           ShowBuildTrainDepotPicker(this);
    00548           this->last_user_action = widget;
    00549         }
    00550         break;
    00551
    00552       case RTW_BUILD_WAYPOINT:
    00553         this->last_user_action = widget;
    00554         _waypoint_count = StationClass::GetCount(STAT_CLASS_WAYP);
    00555         if (HandlePlacePushButton(this, RTW_BUILD_WAYPOINT, SPR_CURSOR_WAYPOINT, HT_RECT) && _waypoint_count > 1) {
    00556           ShowBuildWaypointPicker(this);
    00557         }
    00558         break;
    00559
    00560       case RTW_BUILD_STATION:
    00561         if (HandlePlacePushButton(this, RTW_BUILD_STATION, SPR_CURSOR_RAIL_STATION, HT_RECT)) {
    00562           ShowStationBuilder(this);
    00563           this->last_user_action = widget;
    00564         }
    00565         break;
    00566
    00567       case RTW_BUILD_SIGNALS: {
    00568         this->last_user_action = widget;
    00569         bool started = HandlePlacePushButton(this, RTW_BUILD_SIGNALS, ANIMCURSOR_BUILDSIGNALS, HT_RECT);
    00570         if (started && _settings_client.gui.enable_signal_gui != _ctrl_pressed) {
    00571           ShowSignalBuilder(this);
    00572         }
    00573         break;
    00574       }
    00575
    00576       case RTW_BUILD_BRIDGE:
    00577         HandlePlacePushButton(this, RTW_BUILD_BRIDGE, SPR_CURSOR_BRIDGE, HT_RECT);
    00578         this->last_user_action = widget;
    00579         break;
    00580
    00581       case RTW_BUILD_TUNNEL:
    00582         HandlePlacePushButton(this, RTW_BUILD_TUNNEL, GetRailTypeInfo(_cur_railtype)->cursor.tunnel, HT_SPECIAL);
    00583         this->last_user_action = widget;
    00584         break;
    00585
    00586       case RTW_REMOVE:
    00587         BuildRailClick_Remove(this);
    00588         break;
    00589
    00590       case RTW_CONVERT_RAIL:
    00591         HandlePlacePushButton(this, RTW_CONVERT_RAIL, GetRailTypeInfo(_cur_railtype)->cursor.convert, HT_RECT);
    00592         this->last_user_action = widget;
    00593         break;
    00594
    00595       default: NOT_REACHED();
    00596     }
    00597     this->UpdateRemoveWidgetStatus(widget);
    00598     if (_ctrl_pressed) RailToolbar_CtrlChanged(this);
    00599   }
    00600
    00601   virtual EventState OnKeyPress(uint16 key, uint16 keycode)
    00602   {
    00603     int num = CheckHotkeyMatch(railtoolbar_hotkeys, keycode, this);
    00604     if (num == -1) return ES_NOT_HANDLED;
    00605     this->OnClick(Point(), num, 1);
    00606     MarkTileDirtyByTile(TileVirtXY(_thd.pos.x, _thd.pos.y)); // redraw tile selection
    00607     return ES_HANDLED;
    00608   }
    00609
    00610   virtual void OnPlaceObject(Point pt, TileIndex tile)
    00611   {
    00612     switch (this->last_user_action) {
    00613       case RTW_BUILD_NS:
    00614         VpStartPlaceSizing(tile, VPM_FIX_VERTICAL | VPM_RAILDIRS, DDSP_PLACE_RAIL);
    00615         break;
    00616
    00617       case RTW_BUILD_X:
    00618         VpStartPlaceSizing(tile, VPM_FIX_Y | VPM_RAILDIRS, DDSP_PLACE_RAIL);
    00619         break;
    00620
    00621       case RTW_BUILD_EW:
    00622         VpStartPlaceSizing(tile, VPM_FIX_HORIZONTAL | VPM_RAILDIRS, DDSP_PLACE_RAIL);
    00623         break;
    00624
    00625       case RTW_BUILD_Y:
    00626         VpStartPlaceSizing(tile, VPM_FIX_X | VPM_RAILDIRS, DDSP_PLACE_RAIL);
    00627         break;
    00628
    00629       case RTW_AUTORAIL:
    00630         VpStartPlaceSizing(tile, VPM_RAILDIRS, DDSP_PLACE_RAIL);
    00631         break;
    00632
    00633       case RTW_DEMOLISH:
    00634         PlaceProc_DemolishArea(tile);
    00635         break;
    00636
    00637       case RTW_BUILD_DEPOT:
    00638         DoCommandP(tile, _cur_railtype, _build_depot_direction,
    00639             CMD_BUILD_TRAIN_DEPOT | CMD_MSG(STR_ERROR_CAN_T_BUILD_TRAIN_DEPOT),
    00640             CcRailDepot);
    00641         break;
    00642
    00643       case RTW_BUILD_WAYPOINT:
    00644         PlaceRail_Waypoint(tile);
    00645         break;
    00646
    00647       case RTW_BUILD_STATION:
    00648         PlaceRail_Station(tile);
    00649         break;
    00650
    00651       case RTW_BUILD_SIGNALS:
    00652         VpStartPlaceSizing(tile, VPM_SIGNALDIRS, DDSP_BUILD_SIGNALS);
    00653         break;
    00654
    00655       case RTW_BUILD_BRIDGE:
    00656         PlaceRail_Bridge(tile, this);
    00657         break;
    00658
    00659       case RTW_BUILD_TUNNEL:
    00660         DoCommandP(tile, _cur_railtype | (TRANSPORT_RAIL << 8), 0, CMD_BUILD_TUNNEL | CMD_MSG(STR_ERROR_CAN_T_BUILD_TUNNEL_HERE), CcBuildRailTunnel);
    00661         break;
    00662
    00663       case RTW_CONVERT_RAIL:
    00664         VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_CONVERT_RAIL);
    00665         break;
    00666
    00667       default: NOT_REACHED();
    00668     }
    00669   }
    00670
    00671   virtual void OnPlaceDrag(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt)
    00672   {
    00673     /* no dragging if you have pressed the convert button */
    00674     if (FindWindowById(WC_BUILD_SIGNAL, 0) != NULL && _convert_signal_button && this->IsWidgetLowered(RTW_BUILD_SIGNALS)) return;
    00675
    00676     VpSelectTilesWithMethod(pt.x, pt.y, select_method);
    00677   }
    00678
    00679   virtual void OnPlaceMouseUp(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt, TileIndex start_tile, TileIndex end_tile)
    00680   {
    00681     if (pt.x != -1) {
    00682       switch (select_proc) {
    00683         default: NOT_REACHED();
    00684         case DDSP_BUILD_BRIDGE:
    00685           if (!_settings_client.gui.persistent_buildingtools) ResetObjectToPlace();
    00686           ShowBuildBridgeWindow(start_tile, end_tile, TRANSPORT_RAIL, _cur_railtype);
    00687           break;
    00688
    00689         case DDSP_PLACE_RAIL:
    00690           HandleAutodirPlacement();
    00691           break;
    00692
    00693         case DDSP_BUILD_SIGNALS:
    00694           HandleAutoSignalPlacement();
    00695           break;
    00696
    00697         case DDSP_DEMOLISH_AREA:
    00698           GUIPlaceProcDragXY(select_proc, start_tile, end_tile);
    00699           break;
    00700
    00701         case DDSP_CONVERT_RAIL:
    00702           DoCommandP(end_tile, start_tile, _cur_railtype, CMD_CONVERT_RAIL | CMD_MSG(STR_ERROR_CAN_T_CONVERT_RAIL), CcPlaySound10);
    00703           break;
    00704
    00705         case DDSP_REMOVE_STATION:
    00706         case DDSP_BUILD_STATION:
    00707           if (this->IsWidgetLowered(RTW_BUILD_STATION)) {
    00708             /* Station */
    00709             if (_remove_button_clicked) {
    00710               DoCommandP(end_tile, start_tile, _ctrl_pressed ? 0 : 1, CMD_REMOVE_FROM_RAIL_STATION | CMD_MSG(STR_ERROR_CAN_T_REMOVE_PART_OF_STATION), CcPlaySound1E);
    00711             } else {
    00712               HandleStationPlacement(start_tile, end_tile);
    00713             }
    00714           } else {
    00715             /* Waypoint */
    00716             if (_remove_button_clicked) {
    00717               DoCommandP(end_tile, start_tile, _ctrl_pressed ? 0 : 1, CMD_REMOVE_FROM_RAIL_WAYPOINT | CMD_MSG(STR_ERROR_CAN_T_REMOVE_TRAIN_WAYPOINT), CcPlaySound1E);
    00718             } else {
    00719               TileArea ta(start_tile, end_tile);
    00720               uint32 p1 = _cur_railtype | (select_method == VPM_FIX_X ? AXIS_X : AXIS_Y) << 4 | ta.w << 8 | ta.h << 16 | _ctrl_pressed << 24;
    00721               uint32 p2 = STAT_CLASS_WAYP | _cur_waypoint_type << 8 | INVALID_STATION << 16;
    00722
    00723               CommandContainer cmdcont = { ta.tile, p1, p2, CMD_BUILD_RAIL_WAYPOINT | CMD_MSG(STR_ERROR_CAN_T_BUILD_TRAIN_WAYPOINT), CcPlaySound1E, "" };
    00724               ShowSelectWaypointIfNeeded(cmdcont, ta);
    00725             }
    00726           }
    00727           break;
    00728       }
    00729     }
    00730   }
    00731
    00732   virtual void OnPlaceObjectAbort()
    00733   {
    00734     this->RaiseButtons();
    00735     this->DisableWidget(RTW_REMOVE);
    00736     this->SetWidgetDirty(RTW_REMOVE);
    00737
    00738     DeleteWindowById(WC_BUILD_SIGNAL, TRANSPORT_RAIL);
    00739     DeleteWindowById(WC_BUILD_STATION, TRANSPORT_RAIL);
    00740     DeleteWindowById(WC_BUILD_DEPOT, TRANSPORT_RAIL);
    00741     DeleteWindowById(WC_SELECT_STATION, 0);
    00742     DeleteWindowByClass(WC_BUILD_BRIDGE);
    00743   }
    00744
    00745   virtual void OnPlacePresize(Point pt, TileIndex tile)
    00746   {
    00747     DoCommand(tile, _cur_railtype | (TRANSPORT_RAIL << 8), 0, DC_AUTO, CMD_BUILD_TUNNEL);
    00748     VpSetPresizeRange(tile, _build_tunnel_endtile == 0 ? tile : _build_tunnel_endtile);
    00749   }
    00750
    00751   virtual EventState OnCTRLStateChange()
    00752   {
    00753     /* do not toggle Remove button by Ctrl when placing station */
    00754     if (!this->IsWidgetLowered(RTW_BUILD_STATION) && !this->IsWidgetLowered(RTW_BUILD_WAYPOINT) && RailToolbar_CtrlChanged(this)) return ES_HANDLED;
    00755     return ES_NOT_HANDLED;
    00756   }
    00757
    00758   static Hotkey<BuildRailToolbarWindow> railtoolbar_hotkeys[];
    00759 };
    00760
    00761 const uint16 _railtoolbar_autorail_keys[] = {'5', 'A' | WKC_GLOBAL_HOTKEY, 0};
    00762
    00763 Hotkey<BuildRailToolbarWindow> BuildRailToolbarWindow::railtoolbar_hotkeys[] = {
    00764   Hotkey<BuildRailToolbarWindow>('1', "build_ns", RTW_BUILD_NS),
    00765   Hotkey<BuildRailToolbarWindow>('2', "build_x", RTW_BUILD_X),
    00766   Hotkey<BuildRailToolbarWindow>('3', "build_ew", RTW_BUILD_EW),
    00767   Hotkey<BuildRailToolbarWindow>('4', "build_y", RTW_BUILD_Y),
    00768   Hotkey<BuildRailToolbarWindow>(_railtoolbar_autorail_keys, "autorail", RTW_AUTORAIL),
    00769   Hotkey<BuildRailToolbarWindow>('6', "demolish", RTW_DEMOLISH),
    00770   Hotkey<BuildRailToolbarWindow>('7', "depot", RTW_BUILD_DEPOT),
    00771   Hotkey<BuildRailToolbarWindow>('8', "waypoint", RTW_BUILD_WAYPOINT),
    00772   Hotkey<BuildRailToolbarWindow>('9', "station", RTW_BUILD_STATION),
    00773   Hotkey<BuildRailToolbarWindow>('S', "signal", RTW_BUILD_SIGNALS),
    00774   Hotkey<BuildRailToolbarWindow>('B', "bridge", RTW_BUILD_BRIDGE),
    00775   Hotkey<BuildRailToolbarWindow>('T', "tunnel", RTW_BUILD_TUNNEL),
    00776   Hotkey<BuildRailToolbarWindow>('R', "remove", RTW_REMOVE),
    00777   Hotkey<BuildRailToolbarWindow>('C', "convert", RTW_CONVERT_RAIL),
    00778   HOTKEY_LIST_END(BuildRailToolbarWindow)
    00779 };
    00780 Hotkey<BuildRailToolbarWindow> *_railtoolbar_hotkeys = BuildRailToolbarWindow::railtoolbar_hotkeys;
    00781
    00782 static const NWidgetPart _nested_build_rail_widgets[] = {
    00783   NWidget(NWID_HORIZONTAL),
    00784     NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
    00785     NWidget(WWT_CAPTION, COLOUR_DARK_GREEN, RTW_CAPTION), SetDataTip(STR_WHITE_STRING, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    00786     NWidget(WWT_STICKYBOX, COLOUR_DARK_GREEN),
    00787   EndContainer(),
    00788   NWidget(NWID_HORIZONTAL),
    00789     NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, RTW_BUILD_NS),
    00790             SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_RAIL_NS, STR_RAIL_TOOLBAR_TOOLTIP_BUILD_RAILROAD_TRACK),
    00791     NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, RTW_BUILD_X),
    00792             SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_RAIL_NE, STR_RAIL_TOOLBAR_TOOLTIP_BUILD_RAILROAD_TRACK),
    00793     NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, RTW_BUILD_EW),
    00794             SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_RAIL_EW, STR_RAIL_TOOLBAR_TOOLTIP_BUILD_RAILROAD_TRACK),
    00795     NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, RTW_BUILD_Y),
    00796             SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_RAIL_NW, STR_RAIL_TOOLBAR_TOOLTIP_BUILD_RAILROAD_TRACK),
    00797     NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, RTW_AUTORAIL),
    00798             SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_AUTORAIL, STR_RAIL_TOOLBAR_TOOLTIP_BUILD_AUTORAIL),
    00799
    00800     NWidget(WWT_PANEL, COLOUR_DARK_GREEN), SetMinimalSize(4, 22), SetDataTip(0x0, STR_NULL), EndContainer(),
    00801
    00802     NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, RTW_DEMOLISH),
    00803             SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_DYNAMITE, STR_TOOLTIP_DEMOLISH_BUILDINGS_ETC),
    00804     NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, RTW_BUILD_DEPOT),
    00805             SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_DEPOT_RAIL, STR_RAIL_TOOLBAR_TOOLTIP_BUILD_TRAIN_DEPOT_FOR_BUILDING),
    00806     NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, RTW_BUILD_WAYPOINT),
    00807             SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_WAYPOINT, STR_RAIL_TOOLBAR_TOOLTIP_CONVERT_RAIL_TO_WAYPOINT),
    00808     NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, RTW_BUILD_STATION),
    00809             SetFill(0, 1), SetMinimalSize(42, 22), SetDataTip(SPR_IMG_RAIL_STATION, STR_RAIL_TOOLBAR_TOOLTIP_BUILD_RAILROAD_STATION),
    00810     NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, RTW_BUILD_SIGNALS),
    00811             SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_RAIL_SIGNALS, STR_RAIL_TOOLBAR_TOOLTIP_BUILD_RAILROAD_SIGNALS),
    00812     NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, RTW_BUILD_BRIDGE),
    00813             SetFill(0, 1), SetMinimalSize(42, 22), SetDataTip(SPR_IMG_BRIDGE, STR_RAIL_TOOLBAR_TOOLTIP_BUILD_RAILROAD_BRIDGE),
    00814     NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, RTW_BUILD_TUNNEL),
    00815             SetFill(0, 1), SetMinimalSize(20, 22), SetDataTip(SPR_IMG_TUNNEL_RAIL, STR_RAIL_TOOLBAR_TOOLTIP_BUILD_RAILROAD_TUNNEL),
    00816     NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, RTW_REMOVE),
    00817             SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_REMOVE, STR_RAIL_TOOLBAR_TOOLTIP_TOGGLE_BUILD_REMOVE_FOR),
    00818     NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, RTW_CONVERT_RAIL),
    00819             SetFill(0, 1), SetMinimalSize(22, 22), SetDataTip(SPR_IMG_CONVERT_RAIL, STR_RAIL_TOOLBAR_TOOLTIP_CONVERT_RAIL),
    00820   EndContainer(),
    00821 };
    00822
    00823 static const WindowDesc _build_rail_desc(
    00824   WDP_ALIGN_TOOLBAR, 0, 0,
    00825   WC_BUILD_TOOLBAR, WC_NONE,
    00826   WDF_CONSTRUCTION,
    00827   _nested_build_rail_widgets, lengthof(_nested_build_rail_widgets)
    00828 );
    00829
    00830
    00839 Window *ShowBuildRailToolbar(RailType railtype)
    00840 {
    00841   if (!Company::IsValidID(_local_company)) return NULL;
    00842   if (!ValParamRailtype(railtype)) return NULL;
    00843
    00844   DeleteWindowByClass(WC_BUILD_TOOLBAR);
    00845   _cur_railtype = railtype;
    00846   _remove_button_clicked = false;
    00847   return new BuildRailToolbarWindow(&_build_rail_desc, TRANSPORT_RAIL, railtype);
    00848 }
    00849
    00850 EventState RailToolbarGlobalHotkeys(uint16 key, uint16 keycode)
    00851 {
    00852   extern RailType _last_built_railtype;
    00853   int num = CheckHotkeyMatch<BuildRailToolbarWindow>(_railtoolbar_hotkeys, keycode, NULL, true);
    00854   if (num == -1) return ES_NOT_HANDLED;
    00855   Window *w = ShowBuildRailToolbar(_last_built_railtype);
    00856   if (w == NULL) return ES_NOT_HANDLED;
    00857   return w->OnKeyPress(key, keycode);
    00858 }
    00859
    00860 /* TODO: For custom stations, respect their allowed platforms/lengths bitmasks!
    00861  * --pasky */
    00862
    00863 static void HandleStationPlacement(TileIndex start, TileIndex end)
    00864 {
    00865   TileArea ta(start, end);
    00866   uint numtracks = ta.w;
    00867   uint platlength = ta.h;
    00868
    00869   if (_railstation.orientation == AXIS_X) Swap(numtracks, platlength);
    00870
    00871   uint32 p1 = _cur_railtype | _railstation.orientation << 4 | numtracks << 8 | platlength << 16 | _ctrl_pressed << 24;
    00872   uint32 p2 = _railstation.station_class | _railstation.station_type << 8 | INVALID_STATION << 16;
    00873
    00874   CommandContainer cmdcont = { ta.tile, p1, p2, CMD_BUILD_RAIL_STATION | CMD_MSG(STR_ERROR_CAN_T_BUILD_RAILROAD_STATION), CcStation, "" };
    00875   ShowSelectStationIfNeeded(cmdcont, ta);
    00876 }
    00877
    00879 enum BuildRailStationWidgets {
    00880   BRSW_PLATFORM_DIR_X,
    00881   BRSW_PLATFORM_DIR_Y,
    00882
    00883   BRSW_PLATFORM_NUM_1,
    00884   BRSW_PLATFORM_NUM_2,
    00885   BRSW_PLATFORM_NUM_3,
    00886   BRSW_PLATFORM_NUM_4,
    00887   BRSW_PLATFORM_NUM_5,
    00888   BRSW_PLATFORM_NUM_6,
    00889   BRSW_PLATFORM_NUM_7,
    00890
    00891   BRSW_PLATFORM_LEN_1,
    00892   BRSW_PLATFORM_LEN_2,
    00893   BRSW_PLATFORM_LEN_3,
    00894   BRSW_PLATFORM_LEN_4,
    00895   BRSW_PLATFORM_LEN_5,
    00896   BRSW_PLATFORM_LEN_6,
    00897   BRSW_PLATFORM_LEN_7,
    00898
    00899   BRSW_PLATFORM_DRAG_N_DROP,
    00900
    00901   BRSW_HIGHLIGHT_OFF,
    00902   BRSW_HIGHLIGHT_ON,
    00903   BRSW_COVERAGE_TEXTS,
    00904
    00905   BRSW_MATRIX,
    00906   BRSW_IMAGE,
    00907   BRSW_MATRIX_SCROLL,
    00908
    00909   BRSW_SHOW_NEWST_ADDITIONS,
    00910   BRSW_SHOW_NEWST_MATRIX,
    00911   BRSW_SHOW_NEWST_RESIZE,
    00912   BRSW_SHOW_NEWST_TYPE,
    00913   BRSW_NEWST_LIST,
    00914   BRSW_NEWST_SCROLL,
    00915
    00916   BRSW_PLATFORM_NUM_BEGIN = BRSW_PLATFORM_NUM_1 - 1,
    00917   BRSW_PLATFORM_LEN_BEGIN = BRSW_PLATFORM_LEN_1 - 1,
    00918 };
    00919
    00920 struct BuildRailStationWindow : public PickerWindowBase {
    00921 private:
    00922   uint line_height;
    00923   uint coverage_height;
    00924   Scrollbar *vscroll;
    00925   Scrollbar *vscroll2;
    00926
    00932   void CheckSelectedSize(const StationSpec *statspec)
    00933   {
    00934     if (statspec == NULL || _settings_client.gui.station_dragdrop) return;
    00935
    00936     /* If current number of tracks is not allowed, make it as big as possible (which is always less than currently selected) */
    00937     if (HasBit(statspec->disallowed_platforms, _settings_client.gui.station_numtracks - 1)) {
    00938       this->RaiseWidget(_settings_client.gui.station_numtracks + BRSW_PLATFORM_NUM_BEGIN);
    00939       _settings_client.gui.station_numtracks = 1;
    00940       while (HasBit(statspec->disallowed_platforms, _settings_client.gui.station_numtracks - 1)) {
    00941         _settings_client.gui.station_numtracks++;
    00942       }
    00943       this->LowerWidget(_settings_client.gui.station_numtracks + BRSW_PLATFORM_NUM_BEGIN);
    00944     }
    00945
    00946     if (HasBit(statspec->disallowed_lengths, _settings_client.gui.station_platlength - 1)) {
    00947       this->RaiseWidget(_settings_client.gui.station_platlength + BRSW_PLATFORM_LEN_BEGIN);
    00948       _settings_client.gui.station_platlength = 1;
    00949       while (HasBit(statspec->disallowed_lengths, _settings_client.gui.station_platlength - 1)) {
    00950         _settings_client.gui.station_platlength++;
    00951       }
    00952       this->LowerWidget(_settings_client.gui.station_platlength + BRSW_PLATFORM_LEN_BEGIN);
    00953     }
    00954   }
    00955
    00956 public:
    00957   BuildRailStationWindow(const WindowDesc *desc, Window *parent, bool newstation) : PickerWindowBase(parent)
    00958   {
    00959     this->coverage_height = 2 * FONT_HEIGHT_NORMAL + 3 * WD_PAR_VSEP_NORMAL;
    00960     this->vscroll = NULL;
    00961     _railstation.newstations = newstation;
    00962
    00963     this->CreateNestedTree(desc);
    00964     NWidgetStacked *newst_additions = this->GetWidget<NWidgetStacked>(BRSW_SHOW_NEWST_ADDITIONS);
    00965     newst_additions->SetDisplayedPlane(newstation ? 0 : SZSP_NONE);
    00966     newst_additions = this->GetWidget<NWidgetStacked>(BRSW_SHOW_NEWST_MATRIX);
    00967     newst_additions->SetDisplayedPlane(newstation ? 0 : SZSP_NONE);
    00968     newst_additions = this->GetWidget<NWidgetStacked>(BRSW_SHOW_NEWST_RESIZE);
    00969     newst_additions->SetDisplayedPlane(newstation ? 0 : SZSP_NONE);
    00970     this->FinishInitNested(desc, TRANSPORT_RAIL);
    00971
    00972     this->LowerWidget(_railstation.orientation + BRSW_PLATFORM_DIR_X);
    00973     if (_settings_client.gui.station_dragdrop) {
    00974       this->LowerWidget(BRSW_PLATFORM_DRAG_N_DROP);
    00975     } else {
    00976       this->LowerWidget(_settings_client.gui.station_numtracks + BRSW_PLATFORM_NUM_BEGIN);
    00977       this->LowerWidget(_settings_client.gui.station_platlength + BRSW_PLATFORM_LEN_BEGIN);
    00978     }
    00979     this->SetWidgetLoweredState(BRSW_HIGHLIGHT_OFF, !_settings_client.gui.station_show_coverage);
    00980     this->SetWidgetLoweredState(BRSW_HIGHLIGHT_ON, _settings_client.gui.station_show_coverage);
    00981
    00982     if (!newstation || _railstation.station_class >= (int)StationClass::GetCount()) {
    00983       /* New stations are not available or changed, so ensure the default station
    00984        * type is 'selected'. */
    00985       _railstation.station_class = STAT_CLASS_DFLT;
    00986       _railstation.station_type = 0;
    00987       this->vscroll2 = NULL;
    00988     }
    00989     if (newstation) {
    00990       _railstation.station_count = StationClass::GetCount(_railstation.station_class);
    00991       _railstation.station_type = min(_railstation.station_type, _railstation.station_count - 1);
    00992
    00993       int count = 0;
    00994       for (uint i = 0; i < StationClass::GetCount(); i++) {
    00995         if (i == STAT_CLASS_WAYP) continue;
    00996         count++;
    00997       }
    00998       this->vscroll = this->GetScrollbar(BRSW_NEWST_SCROLL);
    00999       this->vscroll->SetCount(count);
    01000       this->vscroll->SetCapacity(GB(this->GetWidget<NWidgetCore>(BRSW_NEWST_LIST)->widget_data, MAT_ROW_START, MAT_ROW_BITS));
    01001       this->vscroll->SetPosition(Clamp(_railstation.station_class - 2, 0, max(this->vscroll->GetCount() - this->vscroll->GetCapacity(), 0)));
    01002
    01003       this->vscroll2 = this->GetScrollbar(BRSW_MATRIX_SCROLL);
    01004       NWidgetMatrix *matrix = this->GetWidget<NWidgetMatrix>(BRSW_MATRIX);
    01005       matrix->SetScrollbar(this->vscroll2);
    01006       matrix->SetCount(_railstation.station_count);
    01007       matrix->SetClicked(_railstation.station_type);
    01008     }
    01009   }
    01010
    01011   virtual ~BuildRailStationWindow()
    01012   {
    01013     DeleteWindowById(WC_SELECT_STATION, 0);
    01014   }
    01015
    01016   virtual void OnPaint()
    01017   {
    01018     bool newstations = _railstation.newstations;
    01019     const StationSpec *statspec = newstations ? StationClass::Get(_railstation.station_class, _railstation.station_type) : NULL;
    01020
    01021     if (_settings_client.gui.station_dragdrop) {
    01022       SetTileSelectSize(1, 1);
    01023     } else {
    01024       int x = _settings_client.gui.station_numtracks;
    01025       int y = _settings_client.gui.station_platlength;
    01026       if (_railstation.orientation == AXIS_X) Swap(x, y);
    01027       if (!_remove_button_clicked) {
    01028         SetTileSelectSize(x, y);
    01029       }
    01030     }
    01031
    01032     int rad = (_settings_game.station.modified_catchment) ? CA_TRAIN : CA_UNMODIFIED;
    01033
    01034     if (_settings_client.gui.station_show_coverage) SetTileSelectBigSize(-rad, -rad, 2 * rad, 2 * rad);
    01035
    01036     for (uint bits = 0; bits < 7; bits++) {
    01037       bool disable = bits >= _settings_game.station.station_spread;
    01038       if (statspec == NULL) {
    01039         this->SetWidgetDisabledState(bits + BRSW_PLATFORM_NUM_1, disable);
    01040         this->SetWidgetDisabledState(bits + BRSW_PLATFORM_LEN_1, disable);
    01041       } else {
    01042         this->SetWidgetDisabledState(bits + BRSW_PLATFORM_NUM_1, HasBit(statspec->disallowed_platforms, bits) || disable);
    01043         this->SetWidgetDisabledState(bits + BRSW_PLATFORM_LEN_1, HasBit(statspec->disallowed_lengths,   bits) || disable);
    01044       }
    01045     }
    01046
    01047     this->DrawWidgets();
    01048
    01049     /* 'Accepts' and 'Supplies' texts. */
    01050     NWidgetBase *cov = this->GetWidget<NWidgetBase>(BRSW_COVERAGE_TEXTS);
    01051     int top = cov->pos_y + WD_PAR_VSEP_NORMAL;
    01052     int left = cov->pos_x + WD_FRAMERECT_LEFT;
    01053     int right = cov->pos_x + cov->current_x - WD_FRAMERECT_RIGHT;
    01054     int bottom = cov->pos_y + cov->current_y;
    01055     top = DrawStationCoverageAreaText(left, right, top, SCT_ALL, rad, false) + WD_PAR_VSEP_NORMAL;
    01056     top = DrawStationCoverageAreaText(left, right, top, SCT_ALL, rad, true) + WD_PAR_VSEP_NORMAL;
    01057     /* Resize the coverage text space if the text is not equally long as the window. */
    01058     if (top != bottom) {
    01059       this->coverage_height += top - bottom;
    01060       this->ReInit();
    01061     }
    01062   }
    01063
    01064   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
    01065   {
    01066     switch (widget) {
    01067       case BRSW_NEWST_LIST: {
    01068         Dimension d = {0, 0};
    01069         for (uint i = 0; i < StationClass::GetCount(); i++) {
    01070           if (i == STAT_CLASS_WAYP) continue;
    01071           SetDParam(0, StationClass::GetName((StationClassID)i));
    01072           d = maxdim(d, GetStringBoundingBox(STR_BLACK_STRING));
    01073         }
    01074         size->width = max(size->width, d.width + padding.width);
    01075         this->line_height = FONT_HEIGHT_NORMAL + WD_MATRIX_TOP + WD_MATRIX_BOTTOM;
    01076         size->height = GB(this->GetWidget<NWidgetCore>(widget)->widget_data, MAT_ROW_START, MAT_ROW_BITS) * this->line_height;
    01077         resize->height = this->line_height;
    01078         break;
    01079       }
    01080
    01081       case BRSW_SHOW_NEWST_TYPE: {
    01082         if (!_railstation.newstations) {
    01083           size->width = 0;
    01084           size->height = 0;
    01085           break;
    01086         }
    01087
    01088         /* If newstations exist, compute the non-zero minimal size. */
    01089         Dimension d = {0, 0};
    01090         StringID str = this->GetWidget<NWidgetCore>(widget)->widget_data;
    01091         for (StationClassID statclass = STAT_CLASS_BEGIN; statclass < (StationClassID)StationClass::GetCount(); statclass++) {
    01092           if (statclass == STAT_CLASS_WAYP) continue;
    01093           for (uint16 j = 0; j < StationClass::GetCount(statclass); j++) {
    01094             const StationSpec *statspec = StationClass::Get(statclass, j);
    01095             SetDParam(0, (statspec != NULL && statspec->name != 0) ? statspec->name : STR_STATION_CLASS_DFLT);
    01096             d = maxdim(d, GetStringBoundingBox(str));
    01097           }
    01098         }
    01099         size->width = max(size->width, d.width + padding.width);
    01100         break;
    01101       }
    01102
    01103       case BRSW_COVERAGE_TEXTS:
    01104         size->height = this->coverage_height;
    01105         break;
    01106
    01107       case BRSW_MATRIX:
    01108         fill->height = 1;
    01109         resize->height = 1;
    01110         break;
    01111     }
    01112   }
    01113
    01114   virtual void DrawWidget(const Rect &r, int widget) const
    01115   {
    01116     DrawPixelInfo tmp_dpi;
    01117
    01118     switch (GB(widget, 0, 16)) {
    01119       case BRSW_PLATFORM_DIR_X:
    01120         /* Set up a clipping area for the '/' station preview */
    01121         if (FillDrawPixelInfo(&tmp_dpi, r.left, r.top, r.right - r.left + 1, r.bottom - r.top + 1)) {
    01122           DrawPixelInfo *old_dpi = _cur_dpi;
    01123           _cur_dpi = &tmp_dpi;
    01124           if (!DrawStationTile(32, 16, _cur_railtype, AXIS_X, _railstation.station_class, _railstation.station_type)) {
    01125             StationPickerDrawSprite(32, 16, STATION_RAIL, _cur_railtype, INVALID_ROADTYPE, 2);
    01126           }
    01127           _cur_dpi = old_dpi;
    01128         }
    01129         break;
    01130
    01131       case BRSW_PLATFORM_DIR_Y:
    01132         /* Set up a clipping area for the '\' station preview */
    01133         if (FillDrawPixelInfo(&tmp_dpi, r.left, r.top, r.right - r.left + 1, r.bottom - r.top + 1)) {
    01134           DrawPixelInfo *old_dpi = _cur_dpi;
    01135           _cur_dpi = &tmp_dpi;
    01136           if (!DrawStationTile(32, 16, _cur_railtype, AXIS_Y, _railstation.station_class, _railstation.station_type)) {
    01137             StationPickerDrawSprite(32, 16, STATION_RAIL, _cur_railtype, INVALID_ROADTYPE, 3);
    01138           }
    01139           _cur_dpi = old_dpi;
    01140         }
    01141         break;
    01142
    01143       case BRSW_NEWST_LIST: {
    01144         uint statclass = 0;
    01145         uint row = 0;
    01146         for (uint i = 0; i < StationClass::GetCount(); i++) {
    01147           if (i == STAT_CLASS_WAYP) continue;
    01148           if (this->vscroll->IsVisible(statclass)) {
    01149             SetDParam(0, StationClass::GetName((StationClassID)i));
    01150             DrawString(r.left + WD_MATRIX_LEFT, r.right - WD_MATRIX_RIGHT, row * this->line_height + r.top + WD_MATRIX_TOP, STR_JUST_STRING,
    01151                 (StationClassID)i == _railstation.station_class ? TC_WHITE : TC_BLACK);
    01152             row++;
    01153           }
    01154           statclass++;
    01155         }
    01156         break;
    01157       }
    01158
    01159       case BRSW_IMAGE: {
    01160         byte type = GB(widget, 16, 16);
    01161         assert(type < _railstation.station_count);
    01162         /* Check station availability callback */
    01163         const StationSpec *statspec = StationClass::Get(_railstation.station_class, type);
    01164         if (statspec != NULL && HasBit(statspec->callback_mask, CBM_STATION_AVAIL) &&
    01165             GB(GetStationCallback(CBID_STATION_AVAILABILITY, 0, 0, statspec, NULL, INVALID_TILE), 0, 8) == 0) {
    01166           GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, 0, FILLRECT_CHECKER);
    01167         }
    01168
    01169         /* Set up a clipping area for the station preview. */
    01170         if (FillDrawPixelInfo(&tmp_dpi, r.left + 1, r.top + 1, r.right - (r.left + 1) + 1, r.bottom - 1 - (r.top + 1) + 1)) {
    01171           DrawPixelInfo *old_dpi = _cur_dpi;
    01172           _cur_dpi = &tmp_dpi;
    01173           if (!DrawStationTile(31, 29, _cur_railtype, _railstation.orientation, _railstation.station_class, type)) {
    01174             StationPickerDrawSprite(31, 29, STATION_RAIL, _cur_railtype, INVALID_ROADTYPE, 2 + _railstation.orientation);
    01175           }
    01176           _cur_dpi = old_dpi;
    01177         }
    01178         break;
    01179       }
    01180     }
    01181   }
    01182
    01183   virtual void OnResize()
    01184   {
    01185     if (this->vscroll != NULL) { // New stations available.
    01186       this->vscroll->SetCapacityFromWidget(this, BRSW_NEWST_LIST);
    01187       this->GetWidget<NWidgetCore>(BRSW_NEWST_LIST)->widget_data = (this->vscroll->GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
    01188     }
    01189   }
    01190
    01191   virtual void SetStringParameters(int widget) const
    01192   {
    01193     if (widget == BRSW_SHOW_NEWST_TYPE) {
    01194       const StationSpec *statspec = StationClass::Get(_railstation.station_class, _railstation.station_type);
    01195       SetDParam(0, (statspec != NULL && statspec->name != 0) ? statspec->name : STR_STATION_CLASS_DFLT);
    01196     }
    01197   }
    01198
    01199   virtual void OnClick(Point pt, int widget, int click_count)
    01200   {
    01201     switch (GB(widget, 0, 16)) {
    01202       case BRSW_PLATFORM_DIR_X:
    01203       case BRSW_PLATFORM_DIR_Y:
    01204         this->RaiseWidget(_railstation.orientation + BRSW_PLATFORM_DIR_X);
    01205         _railstation.orientation = (Axis)(widget - BRSW_PLATFORM_DIR_X);
    01206         this->LowerWidget(_railstation.orientation + BRSW_PLATFORM_DIR_X);
    01207         SndPlayFx(SND_15_BEEP);
    01208         this->SetDirty();
    01209         DeleteWindowById(WC_SELECT_STATION, 0);
    01210         break;
    01211
    01212       case BRSW_PLATFORM_NUM_1:
    01213       case BRSW_PLATFORM_NUM_2:
    01214       case BRSW_PLATFORM_NUM_3:
    01215       case BRSW_PLATFORM_NUM_4:
    01216       case BRSW_PLATFORM_NUM_5:
    01217       case BRSW_PLATFORM_NUM_6:
    01218       case BRSW_PLATFORM_NUM_7: {
    01219         this->RaiseWidget(_settings_client.gui.station_numtracks + BRSW_PLATFORM_NUM_BEGIN);
    01220         this->RaiseWidget(BRSW_PLATFORM_DRAG_N_DROP);
    01221
    01222         _settings_client.gui.station_numtracks = widget - BRSW_PLATFORM_NUM_BEGIN;
    01223         _settings_client.gui.station_dragdrop = false;
    01224
    01225         _settings_client.gui.station_dragdrop = false;
    01226
    01227         const StationSpec *statspec = _railstation.newstations ? StationClass::Get(_railstation.station_class, _railstation.station_type) : NULL;
    01228         if (statspec != NULL && HasBit(statspec->disallowed_lengths, _settings_client.gui.station_platlength - 1)) {
    01229           /* The previously selected number of platforms in invalid */
    01230           for (uint i = 0; i < 7; i++) {
    01231             if (!HasBit(statspec->disallowed_lengths, i)) {
    01232               this->RaiseWidget(_settings_client.gui.station_platlength + BRSW_PLATFORM_LEN_BEGIN);
    01233               _settings_client.gui.station_platlength = i + 1;
    01234               break;
    01235             }
    01236           }
    01237         }
    01238
    01239         this->LowerWidget(_settings_client.gui.station_numtracks + BRSW_PLATFORM_NUM_BEGIN);
    01240         this->LowerWidget(_settings_client.gui.station_platlength + BRSW_PLATFORM_LEN_BEGIN);
    01241         SndPlayFx(SND_15_BEEP);
    01242         this->SetDirty();
    01243         DeleteWindowById(WC_SELECT_STATION, 0);
    01244         break;
    01245       }
    01246
    01247       case BRSW_PLATFORM_LEN_1:
    01248       case BRSW_PLATFORM_LEN_2:
    01249       case BRSW_PLATFORM_LEN_3:
    01250       case BRSW_PLATFORM_LEN_4:
    01251       case BRSW_PLATFORM_LEN_5:
    01252       case BRSW_PLATFORM_LEN_6:
    01253       case BRSW_PLATFORM_LEN_7: {
    01254         this->RaiseWidget(_settings_client.gui.station_platlength + BRSW_PLATFORM_LEN_BEGIN);
    01255         this->RaiseWidget(BRSW_PLATFORM_DRAG_N_DROP);
    01256
    01257         _settings_client.gui.station_platlength = widget - BRSW_PLATFORM_LEN_BEGIN;
    01258         _settings_client.gui.station_dragdrop = false;
    01259
    01260         _settings_client.gui.station_dragdrop = false;
    01261
    01262         const StationSpec *statspec = _railstation.newstations ? StationClass::Get(_railstation.station_class, _railstation.station_type) : NULL;
    01263         if (statspec != NULL && HasBit(statspec->disallowed_platforms, _settings_client.gui.station_numtracks - 1)) {
    01264           /* The previously selected number of tracks in invalid */
    01265           for (uint i = 0; i < 7; i++) {
    01266             if (!HasBit(statspec->disallowed_platforms, i)) {
    01267               this->RaiseWidget(_settings_client.gui.station_numtracks + BRSW_PLATFORM_NUM_BEGIN);
    01268               _settings_client.gui.station_numtracks = i + 1;
    01269               break;
    01270             }
    01271           }
    01272         }
    01273
    01274         this->LowerWidget(_settings_client.gui.station_numtracks + BRSW_PLATFORM_NUM_BEGIN);
    01275         this->LowerWidget(_settings_client.gui.station_platlength + BRSW_PLATFORM_LEN_BEGIN);
    01276         SndPlayFx(SND_15_BEEP);
    01277         this->SetDirty();
    01278         DeleteWindowById(WC_SELECT_STATION, 0);
    01279         break;
    01280       }
    01281
    01282       case BRSW_PLATFORM_DRAG_N_DROP: {
    01283         _settings_client.gui.station_dragdrop ^= true;
    01284
    01285         this->ToggleWidgetLoweredState(BRSW_PLATFORM_DRAG_N_DROP);
    01286
    01287         /* get the first allowed length/number of platforms */
    01288         const StationSpec *statspec = _railstation.newstations ? StationClass::Get(_railstation.station_class, _railstation.station_type) : NULL;
    01289         if (statspec != NULL && HasBit(statspec->disallowed_lengths, _settings_client.gui.station_platlength - 1)) {
    01290           for (uint i = 0; i < 7; i++) {
    01291             if (!HasBit(statspec->disallowed_lengths, i)) {
    01292               this->RaiseWidget(_settings_client.gui.station_platlength + BRSW_PLATFORM_LEN_BEGIN);
    01293               _settings_client.gui.station_platlength = i + 1;
    01294               break;
    01295             }
    01296           }
    01297         }
    01298         if (statspec != NULL && HasBit(statspec->disallowed_platforms, _settings_client.gui.station_numtracks - 1)) {
    01299           for (uint i = 0; i < 7; i++) {
    01300             if (!HasBit(statspec->disallowed_platforms, i)) {
    01301               this->RaiseWidget(_settings_client.gui.station_numtracks + BRSW_PLATFORM_NUM_BEGIN);
    01302               _settings_client.gui.station_numtracks = i + 1;
    01303               break;
    01304             }
    01305           }
    01306         }
    01307
    01308         this->SetWidgetLoweredState(_settings_client.gui.station_numtracks + BRSW_PLATFORM_NUM_BEGIN, !_settings_client.gui.station_dragdrop);
    01309         this->SetWidgetLoweredState(_settings_client.gui.station_platlength + BRSW_PLATFORM_LEN_BEGIN, !_settings_client.gui.station_dragdrop);
    01310         SndPlayFx(SND_15_BEEP);
    01311         this->SetDirty();
    01312         DeleteWindowById(WC_SELECT_STATION, 0);
    01313         break;
    01314       }
    01315
    01316       case BRSW_HIGHLIGHT_OFF:
    01317       case BRSW_HIGHLIGHT_ON:
    01318         _settings_client.gui.station_show_coverage = (widget != BRSW_HIGHLIGHT_OFF);
    01319
    01320         this->SetWidgetLoweredState(BRSW_HIGHLIGHT_OFF, !_settings_client.gui.station_show_coverage);
    01321         this->SetWidgetLoweredState(BRSW_HIGHLIGHT_ON, _settings_client.gui.station_show_coverage);
    01322         SndPlayFx(SND_15_BEEP);
    01323         this->SetDirty();
    01324         break;
    01325
    01326       case BRSW_NEWST_LIST: {
    01327         int y = this->vscroll->GetScrolledRowFromWidget(pt.y, this, BRSW_NEWST_LIST, 0, this->line_height);
    01328         if (y >= (int)StationClass::GetCount()) return;
    01329         for (uint i = 0; i < StationClass::GetCount(); i++) {
    01330           if (i == STAT_CLASS_WAYP) continue;
    01331           if (y == 0) {
    01332             if (_railstation.station_class != (StationClassID)i) {
    01333               _railstation.station_class = (StationClassID)i;
    01334               _railstation.station_count = StationClass::GetCount(_railstation.station_class);
    01335               _railstation.station_type  = min((int)_railstation.station_type, max(0, (int)_railstation.station_count - 1));
    01336
    01337               this->CheckSelectedSize(StationClass::Get(_railstation.station_class, _railstation.station_type));
    01338
    01339               NWidgetMatrix *matrix = this->GetWidget<NWidgetMatrix>(BRSW_MATRIX);
    01340               matrix->SetCount(_railstation.station_count);
    01341               matrix->SetClicked(_railstation.station_type);
    01342             }
    01343             SndPlayFx(SND_15_BEEP);
    01344             this->SetDirty();
    01345             DeleteWindowById(WC_SELECT_STATION, 0);
    01346             break;
    01347           }
    01348           y--;
    01349         }
    01350         break;
    01351       }
    01352
    01353       case BRSW_IMAGE: {
    01354         int y = GB(widget, 16, 16);
    01355         if (y >= _railstation.station_count) return;
    01356
    01357         /* Check station availability callback */
    01358         const StationSpec *statspec = StationClass::Get(_railstation.station_class, y);
    01359         if (statspec != NULL && HasBit(statspec->callback_mask, CBM_STATION_AVAIL) &&
    01360             GB(GetStationCallback(CBID_STATION_AVAILABILITY, 0, 0, statspec, NULL, INVALID_TILE), 0, 8) == 0) return;
    01361
    01362         _railstation.station_type = y;
    01363
    01364         this->CheckSelectedSize(statspec);
    01365         this->GetWidget<NWidgetMatrix>(BRSW_MATRIX)->SetClicked(_railstation.station_type);
    01366
    01367         SndPlayFx(SND_15_BEEP);
    01368         this->SetDirty();
    01369         DeleteWindowById(WC_SELECT_STATION, 0);
    01370         break;
    01371       }
    01372     }
    01373   }
    01374
    01375   virtual void OnTick()
    01376   {
    01377     CheckRedrawStationCoverage(this);
    01378   }
    01379 };
    01380
    01381 static const NWidgetPart _nested_station_builder_widgets[] = {
    01382   NWidget(NWID_HORIZONTAL),
    01383     NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
    01384     NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_STATION_BUILD_RAIL_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    01385   EndContainer(),
    01386   NWidget(WWT_PANEL, COLOUR_DARK_GREEN),
    01387     NWidget(NWID_HORIZONTAL),
    01388       NWidget(NWID_VERTICAL),
    01389         NWidget(NWID_SELECTION, INVALID_COLOUR, BRSW_SHOW_NEWST_ADDITIONS),
    01390           NWidget(NWID_HORIZONTAL), SetPIP(7, 0, 7), SetPadding(2, 0, 1, 0),
    01391             NWidget(WWT_MATRIX, COLOUR_GREY, BRSW_NEWST_LIST), SetMinimalSize(122, 71), SetFill(1, 0),
    01392                 SetDataTip(0x501, STR_STATION_BUILD_STATION_CLASS_TOOLTIP), SetScrollbar(BRSW_NEWST_SCROLL),
    01393             NWidget(NWID_VSCROLLBAR, COLOUR_GREY, BRSW_NEWST_SCROLL),
    01394           EndContainer(),
    01395         EndContainer(),
    01396         NWidget(WWT_LABEL, COLOUR_DARK_GREEN), SetMinimalSize(144, 11), SetDataTip(STR_STATION_BUILD_ORIENTATION, STR_NULL), SetPadding(1, 2, 0, 2),
    01397         NWidget(NWID_HORIZONTAL),
    01398           NWidget(NWID_SPACER), SetMinimalSize(7, 0), SetFill(1, 0),
    01399           NWidget(WWT_PANEL, COLOUR_GREY, BRSW_PLATFORM_DIR_X), SetMinimalSize(66, 48), SetFill(0, 0), SetDataTip(0x0, STR_STATION_BUILD_RAILROAD_ORIENTATION_TOOLTIP), EndContainer(),
    01400           NWidget(NWID_SPACER), SetMinimalSize(2, 0), SetFill(1, 0),
    01401           NWidget(WWT_PANEL, COLOUR_GREY, BRSW_PLATFORM_DIR_Y), SetMinimalSize(66, 48), SetFill(0, 0), SetDataTip(0x0, STR_STATION_BUILD_RAILROAD_ORIENTATION_TOOLTIP), EndContainer(),
    01402           NWidget(NWID_SPACER), SetMinimalSize(7, 0), SetFill(1, 0),
    01403         EndContainer(),
    01404         NWidget(WWT_LABEL, COLOUR_DARK_GREEN, BRSW_SHOW_NEWST_TYPE), SetMinimalSize(144, 11), SetDataTip(STR_ORANGE_STRING, STR_NULL), SetPadding(1, 2, 4, 2),
    01405         NWidget(WWT_LABEL, COLOUR_DARK_GREEN), SetMinimalSize(144, 11), SetDataTip(STR_STATION_BUILD_NUMBER_OF_TRACKS, STR_NULL), SetPadding(0, 2, 0, 2),
    01406         NWidget(NWID_HORIZONTAL),
    01407           NWidget(NWID_SPACER), SetFill(1, 0),
    01408           NWidget(WWT_TEXTBTN, COLOUR_GREY, BRSW_PLATFORM_NUM_1), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_1, STR_STATION_BUILD_NUMBER_OF_TRACKS_TOOLTIP),
    01409           NWidget(WWT_TEXTBTN, COLOUR_GREY, BRSW_PLATFORM_NUM_2), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_2, STR_STATION_BUILD_NUMBER_OF_TRACKS_TOOLTIP),
    01410           NWidget(WWT_TEXTBTN, COLOUR_GREY, BRSW_PLATFORM_NUM_3), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_3, STR_STATION_BUILD_NUMBER_OF_TRACKS_TOOLTIP),
    01411           NWidget(WWT_TEXTBTN, COLOUR_GREY, BRSW_PLATFORM_NUM_4), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_4, STR_STATION_BUILD_NUMBER_OF_TRACKS_TOOLTIP),
    01412           NWidget(WWT_TEXTBTN, COLOUR_GREY, BRSW_PLATFORM_NUM_5), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_5, STR_STATION_BUILD_NUMBER_OF_TRACKS_TOOLTIP),
    01413           NWidget(WWT_TEXTBTN, COLOUR_GREY, BRSW_PLATFORM_NUM_6), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_6, STR_STATION_BUILD_NUMBER_OF_TRACKS_TOOLTIP),
    01414           NWidget(WWT_TEXTBTN, COLOUR_GREY, BRSW_PLATFORM_NUM_7), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_7, STR_STATION_BUILD_NUMBER_OF_TRACKS_TOOLTIP),
    01415           NWidget(NWID_SPACER), SetFill(1, 0),
    01416         EndContainer(),
    01417         NWidget(WWT_LABEL, COLOUR_DARK_GREEN), SetMinimalSize(144, 11), SetDataTip(STR_STATION_BUILD_PLATFORM_LENGTH, STR_NULL), SetPadding(2, 2, 0, 2),
    01418         NWidget(NWID_HORIZONTAL),
    01419           NWidget(NWID_SPACER), SetFill(1, 0),
    01420           NWidget(WWT_TEXTBTN, COLOUR_GREY, BRSW_PLATFORM_LEN_1), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_1, STR_STATION_BUILD_PLATFORM_LENGTH_TOOLTIP),
    01421           NWidget(WWT_TEXTBTN, COLOUR_GREY, BRSW_PLATFORM_LEN_2), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_2, STR_STATION_BUILD_PLATFORM_LENGTH_TOOLTIP),
    01422           NWidget(WWT_TEXTBTN, COLOUR_GREY, BRSW_PLATFORM_LEN_3), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_3, STR_STATION_BUILD_PLATFORM_LENGTH_TOOLTIP),
    01423           NWidget(WWT_TEXTBTN, COLOUR_GREY, BRSW_PLATFORM_LEN_4), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_4, STR_STATION_BUILD_PLATFORM_LENGTH_TOOLTIP),
    01424           NWidget(WWT_TEXTBTN, COLOUR_GREY, BRSW_PLATFORM_LEN_5), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_5, STR_STATION_BUILD_PLATFORM_LENGTH_TOOLTIP),
    01425           NWidget(WWT_TEXTBTN, COLOUR_GREY, BRSW_PLATFORM_LEN_6), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_6, STR_STATION_BUILD_PLATFORM_LENGTH_TOOLTIP),
    01426           NWidget(WWT_TEXTBTN, COLOUR_GREY, BRSW_PLATFORM_LEN_7), SetMinimalSize(15, 12), SetDataTip(STR_BLACK_7, STR_STATION_BUILD_PLATFORM_LENGTH_TOOLTIP),
    01427           NWidget(NWID_SPACER), SetFill(1, 0),
    01428         EndContainer(),
    01429         NWidget(NWID_SPACER), SetMinimalSize(0, 2),
    01430         NWidget(NWID_HORIZONTAL),
    01431           NWidget(NWID_SPACER), SetMinimalSize(2, 0), SetFill(1, 0),
    01432           NWidget(WWT_TEXTBTN, COLOUR_GREY, BRSW_PLATFORM_DRAG_N_DROP), SetMinimalSize(75, 12), SetDataTip(STR_STATION_BUILD_DRAG_DROP, STR_STATION_BUILD_DRAG_DROP_TOOLTIP),
    01433           NWidget(NWID_SPACER), SetMinimalSize(2, 0), SetFill(1, 0),
    01434         EndContainer(),
    01435         NWidget(WWT_LABEL, COLOUR_DARK_GREEN), SetMinimalSize(144, 11), SetDataTip(STR_STATION_BUILD_COVERAGE_AREA_TITLE, STR_NULL), SetPadding(3, 2, 0, 2),
    01436         NWidget(NWID_HORIZONTAL),
    01437           NWidget(NWID_SPACER), SetMinimalSize(2, 0), SetFill(1, 0),
    01438           NWidget(WWT_TEXTBTN, COLOUR_GREY, BRSW_HIGHLIGHT_OFF), SetMinimalSize(60, 12),
    01439                         SetDataTip(STR_STATION_BUILD_COVERAGE_OFF, STR_STATION_BUILD_COVERAGE_AREA_OFF_TOOLTIP),
    01440           NWidget(WWT_TEXTBTN, COLOUR_GREY, BRSW_HIGHLIGHT_ON), SetMinimalSize(60, 12),
    01441                         SetDataTip(STR_STATION_BUILD_COVERAGE_ON, STR_STATION_BUILD_COVERAGE_AREA_ON_TOOLTIP),
    01442           NWidget(NWID_SPACER), SetMinimalSize(2, 0), SetFill(1, 0),
    01443         EndContainer(),
    01444       EndContainer(),
    01445       NWidget(NWID_SELECTION, INVALID_COLOUR, BRSW_SHOW_NEWST_MATRIX),
    01446         /* We need an additional background for the matrix, as the matrix cannot handle the scrollbar due to not being an NWidgetCore. */
    01447         NWidget(WWT_PANEL, COLOUR_DARK_GREEN), SetScrollbar(BRSW_MATRIX_SCROLL),
    01448           NWidget(NWID_HORIZONTAL),
    01449             NWidget(NWID_MATRIX, COLOUR_DARK_GREEN, BRSW_MATRIX), SetScrollbar(BRSW_MATRIX_SCROLL), SetPIP(0, 2, 0), SetPadding(2, 0, 0, 0),
    01450               NWidget(WWT_PANEL, COLOUR_DARK_GREEN, BRSW_IMAGE), SetMinimalSize(66, 60),
    01451                   SetFill(0, 0), SetResize(0, 0), SetDataTip(0x0, STR_STATION_BUILD_STATION_TYPE_TOOLTIP), SetScrollbar(BRSW_MATRIX_SCROLL),
    01452               EndContainer(),
    01453             EndContainer(),
    01454             NWidget(NWID_VSCROLLBAR, COLOUR_DARK_GREEN, BRSW_MATRIX_SCROLL),
    01455           EndContainer(),
    01456         EndContainer(),
    01457       EndContainer(),
    01458     EndContainer(),
    01459     NWidget(NWID_HORIZONTAL),
    01460       NWidget(WWT_EMPTY, INVALID_COLOUR, BRSW_COVERAGE_TEXTS), SetFill(1, 1), SetResize(1, 0),
    01461       NWidget(NWID_SELECTION, INVALID_COLOUR, BRSW_SHOW_NEWST_RESIZE),
    01462         NWidget(NWID_VERTICAL),
    01463           NWidget(WWT_PANEL, COLOUR_DARK_GREEN), SetFill(0, 1), EndContainer(),
    01464           NWidget(WWT_RESIZEBOX, COLOUR_DARK_GREEN),
    01465         EndContainer(),
    01466       EndContainer(),
    01467     EndContainer(),
    01468   EndContainer(),
    01469 };
    01470
    01472 static const WindowDesc _station_builder_desc(
    01473   WDP_AUTO, 350, 0,
    01474   WC_BUILD_STATION, WC_BUILD_TOOLBAR,
    01475   WDF_CONSTRUCTION,
    01476   _nested_station_builder_widgets, lengthof(_nested_station_builder_widgets)
    01477 );
    01478
    01480 static void ShowStationBuilder(Window *parent)
    01481 {
    01482   bool newstations = StationClass::GetCount() > 2 || StationClass::GetCount(STAT_CLASS_DFLT) != 1;
    01483   new BuildRailStationWindow(&_station_builder_desc, parent, newstations);
    01484 }
    01485
    01487 enum BuildSignalWidgets {
    01488   BSW_SEMAPHORE_NORM,
    01489   BSW_SEMAPHORE_ENTRY,
    01490   BSW_SEMAPHORE_EXIT,
    01491   BSW_SEMAPHORE_COMBO,
    01492   BSW_SEMAPHORE_PBS,
    01493   BSW_SEMAPHORE_PBS_OWAY,
    01494   BSW_ELECTRIC_NORM,
    01495   BSW_ELECTRIC_ENTRY,
    01496   BSW_ELECTRIC_EXIT,
    01497   BSW_ELECTRIC_COMBO,
    01498   BSW_ELECTRIC_PBS,
    01499   BSW_ELECTRIC_PBS_OWAY,
    01500   BSW_CONVERT,
    01501   BSW_DRAG_SIGNALS_DENSITY,
    01502   BSW_DRAG_SIGNALS_DENSITY_LABEL,
    01503   BSW_DRAG_SIGNALS_DENSITY_DECREASE,
    01504   BSW_DRAG_SIGNALS_DENSITY_INCREASE,
    01505 };
    01506
    01507 struct BuildSignalWindow : public PickerWindowBase {
    01508 private:
    01516   void DrawSignalSprite(byte widget_index, SpriteID image) const
    01517   {
    01518     /* First get the right image, which is one later for 'green' signals. */
    01519     image += this->IsWidgetLowered(widget_index);
    01520
    01521     /* Next get the actual sprite so we can calculate the right offsets. */
    01522     const Sprite *sprite = GetSprite(image, ST_NORMAL);
    01523
    01524     /* For the x offset we want the sprite to be centered, so undo the offset
    01525      * for sprite drawing and add half of the sprite's width. For the y offset
    01526      * we want the sprite to be aligned on the bottom, so again we undo the
    01527      * offset for sprite drawing and assume it is the bottom of the sprite. */
    01528     int sprite_center_x_offset = sprite->x_offs + sprite->width / 2;
    01529     int sprite_bottom_y_offset = sprite->height + sprite->y_offs;
    01530
    01531     /* Next we want to know where on the window to draw. Calculate the center
    01532      * and the bottom of the area to draw. */
    01533     const NWidgetBase *widget = this->GetWidget<NWidgetBase>(widget_index);
    01534     int widget_center_x = widget->pos_x + widget->current_x / 2;
    01535     int widget_bottom_y = widget->pos_y + widget->current_y - 2;
    01536
    01537     /* Finally we draw the signal. */
    01538     DrawSprite(image, PAL_NONE,
    01539         widget_center_x - sprite_center_x_offset + this->IsWidgetLowered(widget_index),
    01540         widget_bottom_y - sprite_bottom_y_offset + this->IsWidgetLowered(widget_index));
    01541   }
    01542
    01543 public:
    01544   BuildSignalWindow(const WindowDesc *desc, Window *parent) : PickerWindowBase(parent)
    01545   {
    01546     this->InitNested(desc, TRANSPORT_RAIL);
    01547     this->OnInvalidateData();
    01548   }
    01549
    01550   virtual void SetStringParameters(int widget) const
    01551   {
    01552     switch (widget) {
    01553       case BSW_DRAG_SIGNALS_DENSITY_LABEL:
    01554         SetDParam(0, _settings_client.gui.drag_signals_density);
    01555         break;
    01556     }
    01557   }
    01558
    01559   virtual void DrawWidget(const Rect &r, int widget) const
    01560   {
    01561     if (IsInsideMM(widget, BSW_SEMAPHORE_NORM, BSW_ELECTRIC_PBS_OWAY + 1)) {
    01562       /* We need to do some custom sprite widget drawing for the signals. */
    01563       const SpriteID _signal_lookup[] = {
    01564         SPR_IMG_SIGNAL_SEMAPHORE_NORM,  SPR_IMG_SIGNAL_SEMAPHORE_ENTRY, SPR_IMG_SIGNAL_SEMAPHORE_EXIT,
    01565         SPR_IMG_SIGNAL_SEMAPHORE_COMBO, SPR_IMG_SIGNAL_SEMAPHORE_PBS,   SPR_IMG_SIGNAL_SEMAPHORE_PBS_OWAY,
    01566
    01567         SPR_IMG_SIGNAL_ELECTRIC_NORM,  SPR_IMG_SIGNAL_ELECTRIC_ENTRY, SPR_IMG_SIGNAL_ELECTRIC_EXIT,
    01568         SPR_IMG_SIGNAL_ELECTRIC_COMBO, SPR_IMG_SIGNAL_ELECTRIC_PBS,   SPR_IMG_SIGNAL_ELECTRIC_PBS_OWAY
    01569       };
    01570
    01571       this->DrawSignalSprite(widget, _signal_lookup[widget - BSW_SEMAPHORE_NORM]);
    01572     }
    01573   }
    01574
    01575   virtual void OnClick(Point pt, int widget, int click_count)
    01576   {
    01577     switch (widget) {
    01578       case BSW_SEMAPHORE_NORM:
    01579       case BSW_SEMAPHORE_ENTRY:
    01580       case BSW_SEMAPHORE_EXIT:
    01581       case BSW_SEMAPHORE_COMBO:
    01582       case BSW_SEMAPHORE_PBS:
    01583       case BSW_SEMAPHORE_PBS_OWAY:
    01584       case BSW_ELECTRIC_NORM:
    01585       case BSW_ELECTRIC_ENTRY:
    01586       case BSW_ELECTRIC_EXIT:
    01587       case BSW_ELECTRIC_COMBO:
    01588       case BSW_ELECTRIC_PBS:
    01589       case BSW_ELECTRIC_PBS_OWAY:
    01590         this->RaiseWidget((_cur_signal_variant == SIG_ELECTRIC ? BSW_ELECTRIC_NORM : BSW_SEMAPHORE_NORM) + _cur_signal_type);
    01591
    01592         _cur_signal_type = (SignalType)((uint)((widget - BSW_SEMAPHORE_NORM) % (SIGTYPE_LAST + 1)));
    01593         _cur_signal_variant = widget >= BSW_ELECTRIC_NORM ? SIG_ELECTRIC : SIG_SEMAPHORE;
    01594         break;
    01595
    01596       case BSW_CONVERT:
    01597         _convert_signal_button = !_convert_signal_button;
    01598         break;
    01599
    01600       case BSW_DRAG_SIGNALS_DENSITY_DECREASE:
    01601         if (_settings_client.gui.drag_signals_density > 1) {
    01602           _settings_client.gui.drag_signals_density--;
    01603           SetWindowDirty(WC_GAME_OPTIONS, 0);
    01604         }
    01605         break;
    01606
    01607       case BSW_DRAG_SIGNALS_DENSITY_INCREASE:
    01608         if (_settings_client.gui.drag_signals_density < 20) {
    01609           _settings_client.gui.drag_signals_density++;
    01610           SetWindowDirty(WC_GAME_OPTIONS, 0);
    01611         }
    01612         break;
    01613
    01614       default: break;
    01615     }
    01616
    01617     this->InvalidateData();
    01618   }
    01619
    01625   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
    01626   {
    01627     if (!gui_scope) return;
    01628     this->LowerWidget((_cur_signal_variant == SIG_ELECTRIC ? BSW_ELECTRIC_NORM : BSW_SEMAPHORE_NORM) + _cur_signal_type);
    01629
    01630     this->SetWidgetLoweredState(BSW_CONVERT, _convert_signal_button);
    01631
    01632     this->SetWidgetDisabledState(BSW_DRAG_SIGNALS_DENSITY_DECREASE, _settings_client.gui.drag_signals_density == 1);
    01633     this->SetWidgetDisabledState(BSW_DRAG_SIGNALS_DENSITY_INCREASE, _settings_client.gui.drag_signals_density == 20);
    01634   }
    01635 };
    01636
    01638 static const NWidgetPart _nested_signal_builder_widgets[] = {
    01639   NWidget(NWID_HORIZONTAL),
    01640     NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
    01641     NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_BUILD_SIGNAL_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    01642   EndContainer(),
    01643   NWidget(NWID_VERTICAL, NC_EQUALSIZE),
    01644     NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
    01645       NWidget(WWT_PANEL, COLOUR_DARK_GREEN, BSW_SEMAPHORE_NORM), SetDataTip(STR_NULL, STR_BUILD_SIGNAL_SEMAPHORE_NORM_TOOLTIP), EndContainer(), SetFill(1, 1),
    01646       NWidget(WWT_PANEL, COLOUR_DARK_GREEN, BSW_SEMAPHORE_ENTRY), SetDataTip(STR_NULL, STR_BUILD_SIGNAL_SEMAPHORE_ENTRY_TOOLTIP), EndContainer(), SetFill(1, 1),
    01647       NWidget(WWT_PANEL, COLOUR_DARK_GREEN, BSW_SEMAPHORE_EXIT), SetDataTip(STR_NULL, STR_BUILD_SIGNAL_SEMAPHORE_EXIT_TOOLTIP), EndContainer(), SetFill(1, 1),
    01648       NWidget(WWT_PANEL, COLOUR_DARK_GREEN, BSW_SEMAPHORE_COMBO), SetDataTip(STR_NULL, STR_BUILD_SIGNAL_SEMAPHORE_COMBO_TOOLTIP), EndContainer(), SetFill(1, 1),
    01649       NWidget(WWT_PANEL, COLOUR_DARK_GREEN, BSW_SEMAPHORE_PBS), SetDataTip(STR_NULL, STR_BUILD_SIGNAL_SEMAPHORE_PBS_TOOLTIP), EndContainer(), SetFill(1, 1),
    01650       NWidget(WWT_PANEL, COLOUR_DARK_GREEN, BSW_SEMAPHORE_PBS_OWAY), SetDataTip(STR_NULL, STR_BUILD_SIGNAL_SEMAPHORE_PBS_OWAY_TOOLTIP), EndContainer(), SetFill(1, 1),
    01651       NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, BSW_CONVERT), SetDataTip(SPR_IMG_SIGNAL_CONVERT, STR_BUILD_SIGNAL_CONVERT_TOOLTIP), SetFill(1, 1),
    01652     EndContainer(),
    01653     NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
    01654       NWidget(WWT_PANEL, COLOUR_DARK_GREEN, BSW_ELECTRIC_NORM), SetDataTip(STR_NULL, STR_BUILD_SIGNAL_ELECTRIC_NORM_TOOLTIP), EndContainer(), SetFill(1, 1),
    01655       NWidget(WWT_PANEL, COLOUR_DARK_GREEN, BSW_ELECTRIC_ENTRY), SetDataTip(STR_NULL, STR_BUILD_SIGNAL_ELECTRIC_ENTRY_TOOLTIP), EndContainer(), SetFill(1, 1),
    01656       NWidget(WWT_PANEL, COLOUR_DARK_GREEN, BSW_ELECTRIC_EXIT), SetDataTip(STR_NULL, STR_BUILD_SIGNAL_ELECTRIC_EXIT_TOOLTIP), EndContainer(), SetFill(1, 1),
    01657       NWidget(WWT_PANEL, COLOUR_DARK_GREEN, BSW_ELECTRIC_COMBO), SetDataTip(STR_NULL, STR_BUILD_SIGNAL_ELECTRIC_COMBO_TOOLTIP), EndContainer(), SetFill(1, 1),
    01658       NWidget(WWT_PANEL, COLOUR_DARK_GREEN, BSW_ELECTRIC_PBS), SetDataTip(STR_NULL, STR_BUILD_SIGNAL_ELECTRIC_PBS_TOOLTIP), EndContainer(), SetFill(1, 1),
    01659       NWidget(WWT_PANEL, COLOUR_DARK_GREEN, BSW_ELECTRIC_PBS_OWAY), SetDataTip(STR_NULL, STR_BUILD_SIGNAL_ELECTRIC_PBS_OWAY_TOOLTIP), EndContainer(), SetFill(1, 1),
    01660       NWidget(WWT_PANEL, COLOUR_DARK_GREEN, BSW_DRAG_SIGNALS_DENSITY), SetDataTip(STR_NULL, STR_BUILD_SIGNAL_DRAG_SIGNALS_DENSITY_TOOLTIP), SetFill(1, 1),
    01661         NWidget(WWT_LABEL, COLOUR_DARK_GREEN, BSW_DRAG_SIGNALS_DENSITY_LABEL), SetDataTip(STR_ORANGE_INT, STR_BUILD_SIGNAL_DRAG_SIGNALS_DENSITY_TOOLTIP), SetFill(1, 1),
    01662         NWidget(NWID_HORIZONTAL), SetPIP(2, 0, 2),
    01663           NWidget(NWID_SPACER), SetFill(1, 0),
    01664           NWidget(WWT_PUSHARROWBTN, COLOUR_GREY, BSW_DRAG_SIGNALS_DENSITY_DECREASE), SetMinimalSize(9, 12), SetDataTip(AWV_DECREASE, STR_BUILD_SIGNAL_DRAG_SIGNALS_DENSITY_DECREASE_TOOLTIP),
    01665           NWidget(WWT_PUSHARROWBTN, COLOUR_GREY, BSW_DRAG_SIGNALS_DENSITY_INCREASE), SetMinimalSize(9, 12), SetDataTip(AWV_INCREASE, STR_BUILD_SIGNAL_DRAG_SIGNALS_DENSITY_INCREASE_TOOLTIP),
    01666           NWidget(NWID_SPACER), SetFill(1, 0),
    01667         EndContainer(),
    01668         NWidget(NWID_SPACER), SetMinimalSize(0, 2), SetFill(1, 0),
    01669       EndContainer(),
    01670     EndContainer(),
    01671   EndContainer(),
    01672 };
    01673
    01675 static const WindowDesc _signal_builder_desc(
    01676   WDP_AUTO, 0, 0,
    01677   WC_BUILD_SIGNAL, WC_BUILD_TOOLBAR,
    01678   WDF_UNCLICK_BUTTONS | WDF_CONSTRUCTION,
    01679   _nested_signal_builder_widgets, lengthof(_nested_signal_builder_widgets)
    01680 );
    01681
    01685 static void ShowSignalBuilder(Window *parent)
    01686 {
    01687   new BuildSignalWindow(&_signal_builder_desc, parent);
    01688 }
    01689
    01691 enum BuildRailDepotWidgets {
    01692   BRDW_DEPOT_NE,
    01693   BRDW_DEPOT_SE,
    01694   BRDW_DEPOT_SW,
    01695   BRDW_DEPOT_NW,
    01696 };
    01697
    01698 struct BuildRailDepotWindow : public PickerWindowBase {
    01699   BuildRailDepotWindow(const WindowDesc *desc, Window *parent) : PickerWindowBase(parent)
    01700   {
    01701     this->InitNested(desc, TRANSPORT_RAIL);
    01702     this->LowerWidget(_build_depot_direction + BRDW_DEPOT_NE);
    01703   }
    01704
    01705   virtual void DrawWidget(const Rect &r, int widget) const
    01706   {
    01707     if (!IsInsideMM(widget, BRDW_DEPOT_NE, BRDW_DEPOT_NW + 1)) return;
    01708
    01709     DrawTrainDepotSprite(r.left - 1, r.top, widget - BRDW_DEPOT_NE + DIAGDIR_NE, _cur_railtype);
    01710   }
    01711
    01712   virtual void OnClick(Point pt, int widget, int click_count)
    01713   {
    01714     switch (widget) {
    01715       case BRDW_DEPOT_NE:
    01716       case BRDW_DEPOT_SE:
    01717       case BRDW_DEPOT_SW:
    01718       case BRDW_DEPOT_NW:
    01719         this->RaiseWidget(_build_depot_direction + BRDW_DEPOT_NE);
    01720         _build_depot_direction = (DiagDirection)(widget - BRDW_DEPOT_NE);
    01721         this->LowerWidget(_build_depot_direction + BRDW_DEPOT_NE);
    01722         SndPlayFx(SND_15_BEEP);
    01723         this->SetDirty();
    01724         break;
    01725     }
    01726   }
    01727 };
    01728
    01730 static const NWidgetPart _nested_build_depot_widgets[] = {
    01731   NWidget(NWID_HORIZONTAL),
    01732     NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
    01733     NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_BUILD_DEPOT_TRAIN_ORIENTATION_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    01734   EndContainer(),
    01735   NWidget(WWT_PANEL, COLOUR_DARK_GREEN),
    01736     NWidget(NWID_SPACER), SetMinimalSize(0, 3),
    01737     NWidget(NWID_HORIZONTAL_LTR),
    01738       NWidget(NWID_SPACER), SetMinimalSize(3, 0), SetFill(1, 0),
    01739       NWidget(NWID_VERTICAL),
    01740         NWidget(WWT_PANEL, COLOUR_GREY, BRDW_DEPOT_NW), SetMinimalSize(66, 50), SetDataTip(0x0, STR_BUILD_DEPOT_TRAIN_ORIENTATION_TOOLTIP),
    01741         EndContainer(),
    01742         NWidget(NWID_SPACER), SetMinimalSize(0, 2),
    01743         NWidget(WWT_PANEL, COLOUR_GREY, BRDW_DEPOT_SW), SetMinimalSize(66, 50), SetDataTip(0x0, STR_BUILD_DEPOT_TRAIN_ORIENTATION_TOOLTIP),
    01744         EndContainer(),
    01745       EndContainer(),
    01746       NWidget(NWID_SPACER), SetMinimalSize(2, 0),
    01747       NWidget(NWID_VERTICAL),
    01748         NWidget(WWT_PANEL, COLOUR_GREY, BRDW_DEPOT_NE), SetMinimalSize(66, 50), SetDataTip(0x0, STR_BUILD_DEPOT_TRAIN_ORIENTATION_TOOLTIP),
    01749         EndContainer(),
    01750         NWidget(NWID_SPACER), SetMinimalSize(0, 2),
    01751         NWidget(WWT_PANEL, COLOUR_GREY, BRDW_DEPOT_SE), SetMinimalSize(66, 50), SetDataTip(0x0, STR_BUILD_DEPOT_TRAIN_ORIENTATION_TOOLTIP),
    01752         EndContainer(),
    01753       EndContainer(),
    01754       NWidget(NWID_SPACER), SetMinimalSize(3, 0), SetFill(1, 0),
    01755     EndContainer(),
    01756     NWidget(NWID_SPACER), SetMinimalSize(0, 3),
    01757   EndContainer(),
    01758 };
    01759
    01760 static const WindowDesc _build_depot_desc(
    01761   WDP_AUTO, 0, 0,
    01762   WC_BUILD_DEPOT, WC_BUILD_TOOLBAR,
    01763   WDF_CONSTRUCTION,
    01764   _nested_build_depot_widgets, lengthof(_nested_build_depot_widgets)
    01765 );
    01766
    01767 static void ShowBuildTrainDepotPicker(Window *parent)
    01768 {
    01769   new BuildRailDepotWindow(&_build_depot_desc, parent);
    01770 }
    01771
    01773 enum BuildRailWaypointWidgets {
    01774   BRWW_WAYPOINT_MATRIX,
    01775   BRWW_WAYPOINT,
    01776   BRWW_SCROLL,
    01777 };
    01778
    01779 struct BuildRailWaypointWindow : PickerWindowBase {
    01780   BuildRailWaypointWindow(const WindowDesc *desc, Window *parent) : PickerWindowBase(parent)
    01781   {
    01782     this->CreateNestedTree(desc);
    01783
    01784     NWidgetMatrix *matrix = this->GetWidget<NWidgetMatrix>(BRWW_WAYPOINT_MATRIX);
    01785     matrix->SetScrollbar(this->GetScrollbar(BRWW_SCROLL));
    01786
    01787     this->FinishInitNested(desc, TRANSPORT_RAIL);
    01788
    01789     matrix->SetCount(_waypoint_count);
    01790     matrix->SetClicked(_cur_waypoint_type);
    01791   }
    01792
    01793   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
    01794   {
    01795     switch (widget) {
    01796       case BRWW_WAYPOINT_MATRIX:
    01797         /* Three blobs high and wide. */
    01798         size->width  += resize->width  * 2;
    01799         size->height += resize->height * 2;
    01800
    01801         /* Resizing in X direction only at blob size, but at pixel level in Y. */
    01802         resize->height = 1;
    01803         break;
    01804     }
    01805   }
    01806
    01807   virtual void DrawWidget(const Rect &r, int widget) const
    01808   {
    01809     switch (GB(widget, 0, 16)) {
    01810       case BRWW_WAYPOINT: {
    01811         byte type = GB(widget, 16, 16);
    01812         const StationSpec *statspec = StationClass::Get(STAT_CLASS_WAYP, type);
    01813         DrawWaypointSprite(r.left + TILE_PIXELS, r.bottom - TILE_PIXELS, type, _cur_railtype);
    01814
    01815         if (statspec != NULL &&
    01816             HasBit(statspec->callback_mask, CBM_STATION_AVAIL) &&
    01817             GB(GetStationCallback(CBID_STATION_AVAILABILITY, 0, 0, statspec, NULL, INVALID_TILE), 0, 8) == 0) {
    01818           GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, 0, FILLRECT_CHECKER);
    01819         }
    01820       }
    01821     }
    01822   }
    01823
    01824   virtual void OnClick(Point pt, int widget, int click_count)
    01825   {
    01826     switch (GB(widget, 0, 16)) {
    01827       case BRWW_WAYPOINT: {
    01828         byte type = GB(widget, 16, 16);
    01829         this->GetWidget<NWidgetMatrix>(BRWW_WAYPOINT_MATRIX)->SetClicked(_cur_waypoint_type);
    01830
    01831         /* Check station availability callback */
    01832         const StationSpec *statspec = StationClass::Get(STAT_CLASS_WAYP, type);
    01833         if (statspec != NULL &&
    01834             HasBit(statspec->callback_mask, CBM_STATION_AVAIL) &&
    01835             GB(GetStationCallback(CBID_STATION_AVAILABILITY, 0, 0, statspec, NULL, INVALID_TILE), 0, 8) == 0) return;
    01836
    01837         _cur_waypoint_type = type;
    01838         this->GetWidget<NWidgetMatrix>(BRWW_WAYPOINT_MATRIX)->SetClicked(_cur_waypoint_type);
    01839         SndPlayFx(SND_15_BEEP);
    01840         this->SetDirty();
    01841         break;
    01842       }
    01843     }
    01844   }
    01845 };
    01846
    01848 static const NWidgetPart _nested_build_waypoint_widgets[] = {
    01849   NWidget(NWID_HORIZONTAL),
    01850     NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
    01851     NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_WAYPOINT_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    01852   EndContainer(),
    01853   NWidget(NWID_HORIZONTAL),
    01854     NWidget(NWID_MATRIX, COLOUR_DARK_GREEN, BRWW_WAYPOINT_MATRIX), SetPIP(3, 2, 3), SetScrollbar(BRWW_SCROLL),
    01855       NWidget(WWT_PANEL, COLOUR_DARK_GREEN, BRWW_WAYPOINT), SetMinimalSize(66, 60), SetDataTip(0x0, STR_WAYPOINT_GRAPHICS_TOOLTIP), EndContainer(),
    01856     EndContainer(),
    01857     NWidget(NWID_VERTICAL),
    01858       NWidget(NWID_VSCROLLBAR, COLOUR_DARK_GREEN, BRWW_SCROLL),
    01859       NWidget(WWT_RESIZEBOX, COLOUR_DARK_GREEN),
    01860     EndContainer(),
    01861   EndContainer(),
    01862 };
    01863
    01864 static const WindowDesc _build_waypoint_desc(
    01865   WDP_AUTO, 0, 0,
    01866   WC_BUILD_DEPOT, WC_BUILD_TOOLBAR,
    01867   WDF_CONSTRUCTION,
    01868   _nested_build_waypoint_widgets, lengthof(_nested_build_waypoint_widgets)
    01869 );
    01870
    01871 static void ShowBuildWaypointPicker(Window *parent)
    01872 {
    01873   new BuildRailWaypointWindow(&_build_waypoint_desc, parent);
    01874 }
    01875
    01879 void InitializeRailGui()
    01880 {
    01881   _build_depot_direction = DIAGDIR_NW;
    01882 }
    01883
    01888 void ReinitGuiAfterToggleElrail(bool disable)
    01889 {
    01890   extern RailType _last_built_railtype;
    01891   if (disable && _last_built_railtype == RAILTYPE_ELECTRIC) {
    01892     _last_built_railtype = _cur_railtype = RAILTYPE_RAIL;
    01893     BuildRailToolbarWindow *w = dynamic_cast<BuildRailToolbarWindow *>(FindWindowById(WC_BUILD_TOOLBAR, TRANSPORT_RAIL));
    01894     if (w != NULL) w->ModifyRailType(_cur_railtype);
    01895   }
    01896   MarkWholeScreenDirty();
    01897 }
    01898
    01900 static void SetDefaultRailGui()
    01901 {
    01902   if (_local_company == COMPANY_SPECTATOR || !Company::IsValidID(_local_company)) return;
    01903
    01904   extern RailType _last_built_railtype;
    01905   RailType rt = (RailType)(_settings_client.gui.default_rail_type + RAILTYPE_END);
    01906   if (rt == DEF_RAILTYPE_MOST_USED) {
    01907     /* Find the most used rail type */
    01908     RailType count[RAILTYPE_END];
    01909     memset(count, 0, sizeof(count));
    01910     for (TileIndex t = 0; t < MapSize(); t++) {
    01911       if (IsTileType(t, MP_RAILWAY) || IsLevelCrossingTile(t) || HasStationTileRail(t) ||
    01912           (IsTileType(t, MP_TUNNELBRIDGE) && GetTunnelBridgeTransportType(t) == TRANSPORT_RAIL)) {
    01913         count[GetRailType(t)]++;
    01914       }
    01915     }
    01916
    01917     rt = RAILTYPE_RAIL;
    01918     for (RailType r = RAILTYPE_ELECTRIC; r < RAILTYPE_END; r++) {
    01919       if (count[r] >= count[rt]) rt = r;
    01920     }
    01921
    01922     /* No rail, just get the first available one */
    01923     if (count[rt] == 0) rt = DEF_RAILTYPE_FIRST;
    01924   }
    01925   switch (rt) {
    01926     case DEF_RAILTYPE_FIRST:
    01927       rt = RAILTYPE_RAIL;
    01928       while (rt < RAILTYPE_END && !HasRailtypeAvail(_local_company, rt)) rt++;
    01929       break;
    01930
    01931     case DEF_RAILTYPE_LAST:
    01932       rt = GetBestRailtype(_local_company);
    01933       break;
    01934
    01935     default:
    01936       break;
    01937   }
    01938
    01939   _last_built_railtype = _cur_railtype = rt;
    01940   BuildRailToolbarWindow *w = dynamic_cast<BuildRailToolbarWindow *>(FindWindowById(WC_BUILD_TOOLBAR, TRANSPORT_RAIL));
    01941   if (w != NULL) w->ModifyRailType(_cur_railtype);
    01942 }
    01943
    01950 bool ResetSignalVariant(int32 p = 0)
    01951 {
    01952   SignalVariant new_variant = (_cur_year < _settings_client.gui.semaphore_build_before ? SIG_SEMAPHORE : SIG_ELECTRIC);
    01953
    01954   if (new_variant != _cur_signal_variant) {
    01955     Window *w = FindWindowById(WC_BUILD_SIGNAL, 0);
    01956     if (w != NULL) {
    01957       w->SetDirty();
    01958       w->RaiseWidget((_cur_signal_variant == SIG_ELECTRIC ? BSW_ELECTRIC_NORM : BSW_SEMAPHORE_NORM) + _cur_signal_type);
    01959     }
    01960     _cur_signal_variant = new_variant;
    01961   }
    01962
    01963   return true;
    01964 }
    01965
    01970 void InitializeRailGUI()
    01971 {
    01972   SetDefaultRailGui();
    01973
    01974   _convert_signal_button = false;
    01975   _cur_signal_type = _default_signal_type[_settings_client.gui.default_signal_type];
    01976   ResetSignalVariant();
    01977 }
    01978
    01985 static bool CompareRailTypes(const DropDownListItem *first, const DropDownListItem *second)
    01986 {
    01987   return GetRailTypeInfo((RailType)first->result)->sorting_order < GetRailTypeInfo((RailType)second->result)->sorting_order;
    01988 }
    01989
    01995 DropDownList *GetRailTypeDropDownList(bool for_replacement)
    01996 {
    01997   RailTypes used_railtypes = RAILTYPES_NONE;
    01998
    01999   /* Find the used railtypes. */
    02000   Engine *e;
    02001   FOR_ALL_ENGINES_OF_TYPE(e, VEH_TRAIN) {
    02002     if (!HasBit(e->info.climates, _settings_game.game_creation.landscape)) continue;
    02003
    02004     used_railtypes |= GetRailTypeInfo(e->u.rail.railtype)->introduces_railtypes;
    02005   }
    02006
    02007   /* Get the date introduced railtypes as well. */
    02008   used_railtypes = AddDateIntroducedRailTypes(used_railtypes, MAX_DAY);
    02009
    02010   const Company *c = Company::Get(_local_company);
    02011   DropDownList *list = new DropDownList();
    02012   for (RailType rt = RAILTYPE_BEGIN; rt != RAILTYPE_END; rt++) {
    02013     /* If it's not used ever, don't show it to the user. */
    02014     if (!HasBit(used_railtypes, rt)) continue;
    02015
    02016     const RailtypeInfo *rti = GetRailTypeInfo(rt);
    02017     /* Skip rail type if it has no label */
    02018     if (rti->label == 0) continue;
    02019
    02020     StringID str = for_replacement ? rti->strings.replace_text : (rti->max_speed > 0 ? STR_TOOLBAR_RAILTYPE_VELOCITY : STR_JUST_STRING);
    02021     DropDownListParamStringItem *item = new DropDownListParamStringItem(str, rt, !HasBit(c->avail_railtypes, rt));
    02022     item->SetParam(0, rti->strings.menu_text);
    02023     item->SetParam(1, rti->max_speed);
    02024     list->push_back(item);
    02025   }
    02026   list->sort(CompareRailTypes);
    02027   return list;
    02028 }