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