# rail_gui.cpp

Go to the documentation of this file.

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