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