# rail_gui.cpp

Go to the documentation of this file.

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