4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
10 /** @file timetable_gui.cpp GUI for time tabling. */
13 #include "command_func.h"
15 #include "window_gui.h"
16 #include "window_func.h"
17 #include "textbuf_gui.h"
18 #include "strings_func.h"
19 #include "vehicle_base.h"
20 #include "string_func.h"
22 #include "company_func.h"
23 #include "date_func.h"
25 #include "vehicle_gui.h"
26 #include "settings_type.h"
28 #include "widgets/timetable_widget.h"
30 #include "table/sprites.h"
31 #include "table/strings.h"
33 /** Container for the arrival/departure dates of a vehicle */
34 struct TimetableArrivalDeparture
{
35 Ticks arrival
; ///< The arrival time
36 Ticks departure
; ///< The departure time
40 * Set the timetable parameters in the format as described by the setting.
41 * @param param1 the first DParam to fill
42 * @param param2 the second DParam to fill
43 * @param ticks the number of ticks to 'draw'
45 void SetTimetableParams(int param1
, int param2
, Ticks ticks
)
47 if (_settings_client
.gui
.timetable_in_ticks
) {
48 SetDParam(param1
, STR_TIMETABLE_TICKS
);
49 SetDParam(param2
, ticks
);
51 SetDParam(param1
, STR_TIMETABLE_DAYS
);
52 SetDParam(param2
, ticks
/ DAY_TICKS
);
57 * Sets the arrival or departure string and parameters.
58 * @param param1 the first DParam to fill
59 * @param param2 the second DParam to fill
60 * @param ticks the number of ticks to 'draw'
62 static void SetArrivalDepartParams(int param1
, int param2
, Ticks ticks
)
64 SetDParam(param1
, STR_JUST_DATE_TINY
);
65 SetDParam(param2
, _date
+ (ticks
/ DAY_TICKS
));
69 * Check whether it is possible to determine how long the order takes.
70 * @param order the order to check.
71 * @param travelling whether we are interested in the travel or the wait part.
72 * @return true if the travel/wait time can be used.
74 static bool CanDetermineTimeTaken(const Order
*order
, bool travelling
)
76 /* Current order is conditional */
77 if (order
->IsType(OT_CONDITIONAL
) || order
->IsType(OT_IMPLICIT
)) return false;
78 /* No travel time and we have not already finished travelling */
79 if (travelling
&& order
->travel_time
== 0) return false;
80 /* No wait time but we are loading at this timetabled station */
81 if (!travelling
&& order
->wait_time
== 0 && order
->IsType(OT_GOTO_STATION
) && !(order
->GetNonStopType() & ONSF_NO_STOP_AT_DESTINATION_STATION
)) return false;
88 * Fill the table with arrivals and departures
89 * @param v Vehicle which must have at least 2 orders.
90 * @param start order index to start at
91 * @param travelling Are we still in the travelling part of the start order
92 * @param table Fill in arrival and departures including intermediate orders
93 * @param offset Add this value to result and all arrivals and departures
95 static void FillTimetableArrivalDepartureTable(const Vehicle
*v
, VehicleOrderID start
, bool travelling
, TimetableArrivalDeparture
*table
, Ticks offset
)
97 assert(table
!= NULL
);
98 assert(v
->GetNumOrders() >= 2);
99 assert(start
< v
->GetNumOrders());
102 VehicleOrderID i
= start
;
103 const Order
*order
= v
->GetOrder(i
);
105 /* Pre-initialize with unknown time */
106 for (int i
= 0; i
< v
->GetNumOrders(); ++i
) {
107 table
[i
].arrival
= table
[i
].departure
= INVALID_TICKS
;
110 /* Cyclically loop over all orders until we reach the current one again.
111 * As we may start at the current order, do a post-checking loop */
113 /* Automatic orders don't influence the overall timetable;
114 * they just add some untimetabled entries, but the time till
115 * the next non-implicit order can still be known. */
116 if (!order
->IsType(OT_IMPLICIT
)) {
117 if (travelling
|| i
!= start
) {
118 if (!CanDetermineTimeTaken(order
, true)) return;
119 sum
+= order
->travel_time
;
120 table
[i
].arrival
= sum
;
123 if (!CanDetermineTimeTaken(order
, false)) return;
124 sum
+= order
->wait_time
;
125 table
[i
].departure
= sum
;
130 if (i
>= v
->GetNumOrders()) {
132 assert(order
== NULL
);
133 order
= v
->orders
.list
->GetFirstOrder();
135 } while (i
!= start
);
137 /* When loading at a scheduled station we still have to treat the
138 * travelling part of the first order. */
140 if (!CanDetermineTimeTaken(order
, true)) return;
141 sum
+= order
->travel_time
;
142 table
[i
].arrival
= sum
;
148 * Callback for when a time has been chosen to start the time table
149 * @param window the window related to the setting of the date
150 * @param date the actually chosen date
152 static void ChangeTimetableStartCallback(const Window
*w
, Date date
)
154 DoCommandP(0, w
->window_number
, date
, CMD_SET_TIMETABLE_START
| CMD_MSG(STR_ERROR_CAN_T_TIMETABLE_VEHICLE
));
158 struct TimetableWindow
: Window
{
160 const Vehicle
*vehicle
; ///< Vehicle monitored by the window.
161 bool show_expected
; ///< Whether we show expected arrival or scheduled
162 uint deparr_time_width
; ///< The width of the departure/arrival time
163 uint deparr_abbr_width
; ///< The width of the departure/arrival abbreviation
165 bool query_is_speed_query
; ///< The currently open query window is a speed query and not a time query.
167 TimetableWindow(WindowDesc
*desc
, WindowNumber window_number
) :
170 vehicle(Vehicle::Get(window_number
)),
173 this->CreateNestedTree();
174 this->vscroll
= this->GetScrollbar(WID_VT_SCROLLBAR
);
175 this->UpdateSelectionStates();
176 this->FinishInitNested(window_number
);
178 this->owner
= this->vehicle
->owner
;
182 * Build the arrival-departure list for a given vehicle
183 * @param v the vehicle to make the list for
184 * @param table the table to fill
185 * @return if next arrival will be early
187 static bool BuildArrivalDepartureList(const Vehicle
*v
, TimetableArrivalDeparture
*table
)
189 assert(HasBit(v
->vehicle_flags
, VF_TIMETABLE_STARTED
));
191 bool travelling
= (!v
->current_order
.IsType(OT_LOADING
) || v
->current_order
.GetNonStopType() == ONSF_STOP_EVERYWHERE
);
192 Ticks start_time
= _date_fract
- v
->current_order_time
;
194 FillTimetableArrivalDepartureTable(v
, v
->cur_real_order_index
% v
->GetNumOrders(), travelling
, table
, start_time
);
196 return (travelling
&& v
->lateness_counter
< 0);
199 virtual void UpdateWidgetSize(int widget
, Dimension
*size
, const Dimension
&padding
, Dimension
*fill
, Dimension
*resize
)
202 case WID_VT_ARRIVAL_DEPARTURE_PANEL
:
203 SetDParamMaxValue(0, MAX_YEAR
* DAYS_IN_YEAR
, 0, FS_SMALL
);
204 this->deparr_time_width
= GetStringBoundingBox(STR_JUST_DATE_TINY
).width
;
205 this->deparr_abbr_width
= max(GetStringBoundingBox(STR_TIMETABLE_ARRIVAL_ABBREVIATION
).width
, GetStringBoundingBox(STR_TIMETABLE_DEPARTURE_ABBREVIATION
).width
);
206 size
->width
= WD_FRAMERECT_LEFT
+ this->deparr_abbr_width
+ 10 + this->deparr_time_width
+ WD_FRAMERECT_RIGHT
;
208 case WID_VT_ARRIVAL_DEPARTURE_SELECTION
:
209 case WID_VT_TIMETABLE_PANEL
:
210 resize
->height
= FONT_HEIGHT_NORMAL
;
211 size
->height
= WD_FRAMERECT_TOP
+ 8 * resize
->height
+ WD_FRAMERECT_BOTTOM
;
214 case WID_VT_SUMMARY_PANEL
:
215 size
->height
= WD_FRAMERECT_TOP
+ 2 * FONT_HEIGHT_NORMAL
+ WD_FRAMERECT_BOTTOM
;
220 int GetOrderFromTimetableWndPt(int y
, const Vehicle
*v
)
222 int sel
= (y
- this->GetWidget
<NWidgetBase
>(WID_VT_TIMETABLE_PANEL
)->pos_y
- WD_FRAMERECT_TOP
) / FONT_HEIGHT_NORMAL
;
224 if ((uint
)sel
>= this->vscroll
->GetCapacity()) return INVALID_ORDER
;
226 sel
+= this->vscroll
->GetPosition();
228 return (sel
< v
->GetNumOrders() * 2 && sel
>= 0) ? sel
: INVALID_ORDER
;
232 * Some data on this window has become invalid.
233 * @param data Information about the changed data.
234 * @param gui_scope Whether the call is done from GUI scope. You may not do everything when not in GUI scope. See #InvalidateWindowData() for details.
236 virtual void OnInvalidateData(int data
= 0, bool gui_scope
= true)
239 case VIWD_AUTOREPLACE
:
240 /* Autoreplace replaced the vehicle */
241 this->vehicle
= Vehicle::Get(this->window_number
);
244 case VIWD_REMOVE_ALL_ORDERS
:
245 /* Removed / replaced all orders (after deleting / sharing) */
246 if (this->sel_index
== -1) break;
248 this->DeleteChildWindows();
249 this->sel_index
= -1;
252 case VIWD_MODIFY_ORDERS
:
253 if (!gui_scope
) break;
254 this->UpdateSelectionStates();
259 if (gui_scope
) break; // only do this once; from command scope
261 /* Moving an order. If one of these is INVALID_VEH_ORDER_ID, then
262 * the order is being created / removed */
263 if (this->sel_index
== -1) break;
265 VehicleOrderID from
= GB(data
, 0, 8);
266 VehicleOrderID to
= GB(data
, 8, 8);
268 if (from
== to
) break; // no need to change anything
270 /* if from == INVALID_VEH_ORDER_ID, one order was added; if to == INVALID_VEH_ORDER_ID, one order was removed */
271 uint old_num_orders
= this->vehicle
->GetNumOrders() - (uint
)(from
== INVALID_VEH_ORDER_ID
) + (uint
)(to
== INVALID_VEH_ORDER_ID
);
273 VehicleOrderID selected_order
= (this->sel_index
+ 1) / 2;
274 if (selected_order
== old_num_orders
) selected_order
= 0; // when last travel time is selected, it belongs to order 0
276 bool travel
= HasBit(this->sel_index
, 0);
278 if (from
!= selected_order
) {
279 /* Moving from preceding order? */
280 selected_order
-= (int)(from
<= selected_order
);
281 /* Moving to preceding order? */
282 selected_order
+= (int)(to
<= selected_order
);
284 /* Now we are modifying the selected order */
285 if (to
== INVALID_VEH_ORDER_ID
) {
286 /* Deleting selected order */
287 this->DeleteChildWindows();
288 this->sel_index
= -1;
291 /* Moving selected order */
296 /* recompute new sel_index */
297 this->sel_index
= 2 * selected_order
- (int)travel
;
298 /* travel time of first order needs special handling */
299 if (this->sel_index
== -1) this->sel_index
= this->vehicle
->GetNumOrders() * 2 - 1;
306 virtual void OnPaint()
308 const Vehicle
*v
= this->vehicle
;
309 int selected
= this->sel_index
;
311 this->vscroll
->SetCount(v
->GetNumOrders() * 2);
313 if (v
->owner
== _local_company
) {
315 if (selected
!= -1) {
316 const Order
*order
= v
->GetOrder(((selected
+ 1) / 2) % v
->GetNumOrders());
317 if (selected
% 2 == 1) {
318 disable
= order
!= NULL
&& (order
->IsType(OT_CONDITIONAL
) || order
->IsType(OT_IMPLICIT
));
320 disable
= order
== NULL
|| ((!order
->IsType(OT_GOTO_STATION
) || (order
->GetNonStopType() & ONSF_NO_STOP_AT_DESTINATION_STATION
)) && !order
->IsType(OT_CONDITIONAL
));
323 bool disable_speed
= disable
|| selected
% 2 != 1 || v
->type
== VEH_AIRCRAFT
;
325 this->SetWidgetDisabledState(WID_VT_CHANGE_TIME
, disable
);
326 this->SetWidgetDisabledState(WID_VT_CLEAR_TIME
, disable
);
327 this->SetWidgetDisabledState(WID_VT_CHANGE_SPEED
, disable_speed
);
328 this->SetWidgetDisabledState(WID_VT_CLEAR_SPEED
, disable_speed
);
329 this->SetWidgetDisabledState(WID_VT_SHARED_ORDER_LIST
, !v
->IsOrderListShared());
331 this->SetWidgetDisabledState(WID_VT_START_DATE
, v
->orders
.list
== NULL
);
332 this->SetWidgetDisabledState(WID_VT_RESET_LATENESS
, v
->orders
.list
== NULL
);
333 this->SetWidgetDisabledState(WID_VT_AUTOFILL
, v
->orders
.list
== NULL
);
335 this->DisableWidget(WID_VT_START_DATE
);
336 this->DisableWidget(WID_VT_CHANGE_TIME
);
337 this->DisableWidget(WID_VT_CLEAR_TIME
);
338 this->DisableWidget(WID_VT_CHANGE_SPEED
);
339 this->DisableWidget(WID_VT_CLEAR_SPEED
);
340 this->DisableWidget(WID_VT_RESET_LATENESS
);
341 this->DisableWidget(WID_VT_AUTOFILL
);
342 this->DisableWidget(WID_VT_SHARED_ORDER_LIST
);
345 this->SetWidgetLoweredState(WID_VT_AUTOFILL
, HasBit(v
->vehicle_flags
, VF_AUTOFILL_TIMETABLE
));
350 virtual void SetStringParameters(int widget
) const
353 case WID_VT_CAPTION
: SetDParam(0, this->vehicle
->index
); break;
354 case WID_VT_EXPECTED
: SetDParam(0, this->show_expected
? STR_TIMETABLE_EXPECTED
: STR_TIMETABLE_SCHEDULED
); break;
358 virtual void DrawWidget(const Rect
&r
, int widget
) const
360 const Vehicle
*v
= this->vehicle
;
361 int selected
= this->sel_index
;
364 case WID_VT_TIMETABLE_PANEL
: {
365 int y
= r
.top
+ WD_FRAMERECT_TOP
;
366 int i
= this->vscroll
->GetPosition();
367 VehicleOrderID order_id
= (i
+ 1) / 2;
368 bool final_order
= false;
370 bool rtl
= _current_text_dir
== TD_RTL
;
371 SetDParamMaxValue(0, v
->GetNumOrders(), 2);
372 int index_column_width
= GetStringBoundingBox(STR_ORDER_INDEX
).width
+ 2 * GetSpriteSize(rtl
? SPR_ARROW_RIGHT
: SPR_ARROW_LEFT
).width
+ 3;
373 int middle
= rtl
? r
.right
- WD_FRAMERECT_RIGHT
- index_column_width
: r
.left
+ WD_FRAMERECT_LEFT
+ index_column_width
;
375 const Order
*order
= v
->GetOrder(order_id
);
376 while (order
!= NULL
) {
377 /* Don't draw anything if it extends past the end of the window. */
378 if (!this->vscroll
->IsVisible(i
)) break;
381 DrawOrderString(v
, order
, order_id
, y
, i
== selected
, true, r
.left
+ WD_FRAMERECT_LEFT
, middle
, r
.right
- WD_FRAMERECT_RIGHT
);
385 if (order_id
>= v
->GetNumOrders()) {
386 order
= v
->GetOrder(0);
393 TextColour colour
= (i
== selected
) ? TC_WHITE
: TC_BLACK
;
394 if (order
->IsType(OT_CONDITIONAL
)) {
395 string
= STR_TIMETABLE_NO_TRAVEL
;
396 } else if (order
->IsType(OT_IMPLICIT
)) {
397 string
= STR_TIMETABLE_NOT_TIMETABLEABLE
;
398 colour
= ((i
== selected
) ? TC_SILVER
: TC_GREY
) | TC_NO_SHADE
;
399 } else if (order
->travel_time
== 0) {
400 string
= order
->max_speed
!= UINT16_MAX
? STR_TIMETABLE_TRAVEL_NOT_TIMETABLED_SPEED
: STR_TIMETABLE_TRAVEL_NOT_TIMETABLED
;
402 SetTimetableParams(0, 1, order
->travel_time
);
403 string
= order
->max_speed
!= UINT16_MAX
? STR_TIMETABLE_TRAVEL_FOR_SPEED
: STR_TIMETABLE_TRAVEL_FOR
;
405 SetDParam(2, order
->max_speed
);
407 DrawString(rtl
? r
.left
+ WD_FRAMERECT_LEFT
: middle
, rtl
? middle
: r
.right
- WD_FRAMERECT_LEFT
, y
, string
, colour
);
409 if (final_order
) break;
413 y
+= FONT_HEIGHT_NORMAL
;
418 case WID_VT_ARRIVAL_DEPARTURE_PANEL
: {
419 /* Arrival and departure times are handled in an all-or-nothing approach,
420 * i.e. are only shown if we can calculate all times.
421 * Excluding order lists with only one order makes some things easier.
423 Ticks total_time
= v
->orders
.list
!= NULL
? v
->orders
.list
->GetTimetableDurationIncomplete() : 0;
424 if (total_time
<= 0 || v
->GetNumOrders() <= 1 || !HasBit(v
->vehicle_flags
, VF_TIMETABLE_STARTED
)) break;
426 TimetableArrivalDeparture
*arr_dep
= AllocaM(TimetableArrivalDeparture
, v
->GetNumOrders());
427 const VehicleOrderID cur_order
= v
->cur_real_order_index
% v
->GetNumOrders();
429 VehicleOrderID earlyID
= BuildArrivalDepartureList(v
, arr_dep
) ? cur_order
: (VehicleOrderID
)INVALID_VEH_ORDER_ID
;
431 int y
= r
.top
+ WD_FRAMERECT_TOP
;
433 bool show_late
= this->show_expected
&& v
->lateness_counter
> DAY_TICKS
;
434 Ticks offset
= show_late
? 0 : -v
->lateness_counter
;
436 bool rtl
= _current_text_dir
== TD_RTL
;
437 int abbr_left
= rtl
? r
.right
- WD_FRAMERECT_RIGHT
- this->deparr_abbr_width
: r
.left
+ WD_FRAMERECT_LEFT
;
438 int abbr_right
= rtl
? r
.right
- WD_FRAMERECT_RIGHT
: r
.left
+ WD_FRAMERECT_LEFT
+ this->deparr_abbr_width
;
439 int time_left
= rtl
? r
.left
+ WD_FRAMERECT_LEFT
: r
.right
- WD_FRAMERECT_RIGHT
- this->deparr_time_width
;
440 int time_right
= rtl
? r
.left
+ WD_FRAMERECT_LEFT
+ this->deparr_time_width
: r
.right
- WD_FRAMERECT_RIGHT
;
442 for (int i
= this->vscroll
->GetPosition(); i
/ 2 < v
->GetNumOrders(); ++i
) { // note: i is also incremented in the loop
443 /* Don't draw anything if it extends past the end of the window. */
444 if (!this->vscroll
->IsVisible(i
)) break;
447 if (arr_dep
[i
/ 2].arrival
!= INVALID_TICKS
) {
448 DrawString(abbr_left
, abbr_right
, y
, STR_TIMETABLE_ARRIVAL_ABBREVIATION
, i
== selected
? TC_WHITE
: TC_BLACK
);
449 if (this->show_expected
&& i
/ 2 == earlyID
) {
450 SetArrivalDepartParams(0, 1, arr_dep
[i
/ 2].arrival
);
451 DrawString(time_left
, time_right
, y
, STR_GREEN_STRING
, i
== selected
? TC_WHITE
: TC_BLACK
);
453 SetArrivalDepartParams(0, 1, arr_dep
[i
/ 2].arrival
+ offset
);
454 DrawString(time_left
, time_right
, y
, show_late
? STR_RED_STRING
: STR_JUST_STRING
, i
== selected
? TC_WHITE
: TC_BLACK
);
458 if (arr_dep
[i
/ 2].departure
!= INVALID_TICKS
) {
459 DrawString(abbr_left
, abbr_right
, y
, STR_TIMETABLE_DEPARTURE_ABBREVIATION
, i
== selected
? TC_WHITE
: TC_BLACK
);
460 SetArrivalDepartParams(0, 1, arr_dep
[i
/2].departure
+ offset
);
461 DrawString(time_left
, time_right
, y
, show_late
? STR_RED_STRING
: STR_JUST_STRING
, i
== selected
? TC_WHITE
: TC_BLACK
);
464 y
+= FONT_HEIGHT_NORMAL
;
469 case WID_VT_SUMMARY_PANEL
: {
470 int y
= r
.top
+ WD_FRAMERECT_TOP
;
472 Ticks total_time
= v
->orders
.list
!= NULL
? v
->orders
.list
->GetTimetableDurationIncomplete() : 0;
473 if (total_time
!= 0) {
474 SetTimetableParams(0, 1, total_time
);
475 DrawString(r
.left
+ WD_FRAMERECT_LEFT
, r
.right
- WD_FRAMERECT_RIGHT
, y
, v
->orders
.list
->IsCompleteTimetable() ? STR_TIMETABLE_TOTAL_TIME
: STR_TIMETABLE_TOTAL_TIME_INCOMPLETE
);
477 y
+= FONT_HEIGHT_NORMAL
;
479 if (v
->timetable_start
!= 0) {
480 /* We are running towards the first station so we can start the
481 * timetable at the given time. */
482 SetDParam(0, STR_JUST_DATE_TINY
);
483 SetDParam(1, v
->timetable_start
);
484 DrawString(r
.left
+ WD_FRAMERECT_LEFT
, r
.right
- WD_FRAMERECT_RIGHT
, y
, STR_TIMETABLE_STATUS_START_AT
);
485 } else if (!HasBit(v
->vehicle_flags
, VF_TIMETABLE_STARTED
)) {
486 /* We aren't running on a timetable yet, so how can we be "on time"
487 * when we aren't even "on service"/"on duty"? */
488 DrawString(r
.left
+ WD_FRAMERECT_LEFT
, r
.right
- WD_FRAMERECT_RIGHT
, y
, STR_TIMETABLE_STATUS_NOT_STARTED
);
489 } else if (v
->lateness_counter
== 0 || (!_settings_client
.gui
.timetable_in_ticks
&& v
->lateness_counter
/ DAY_TICKS
== 0)) {
490 DrawString(r
.left
+ WD_FRAMERECT_LEFT
, r
.right
- WD_FRAMERECT_RIGHT
, y
, STR_TIMETABLE_STATUS_ON_TIME
);
492 SetTimetableParams(0, 1, abs(v
->lateness_counter
));
493 DrawString(r
.left
+ WD_FRAMERECT_LEFT
, r
.right
- WD_FRAMERECT_RIGHT
, y
, v
->lateness_counter
< 0 ? STR_TIMETABLE_STATUS_EARLY
: STR_TIMETABLE_STATUS_LATE
);
500 static inline uint32
PackTimetableArgs(const Vehicle
*v
, uint selected
, bool speed
)
502 uint order_number
= (selected
+ 1) / 2;
503 ModifyTimetableFlags mtf
= (selected
% 2 == 1) ? (speed
? MTF_TRAVEL_SPEED
: MTF_TRAVEL_TIME
) : MTF_WAIT_TIME
;
505 if (order_number
>= v
->GetNumOrders()) order_number
= 0;
507 return v
->index
| (order_number
<< 20) | (mtf
<< 28);
510 virtual void OnClick(Point pt
, int widget
, int click_count
)
512 const Vehicle
*v
= this->vehicle
;
515 case WID_VT_ORDER_VIEW
: // Order view button
519 case WID_VT_TIMETABLE_PANEL
: { // Main panel.
520 int selected
= GetOrderFromTimetableWndPt(pt
.y
, v
);
522 this->DeleteChildWindows();
523 this->sel_index
= (selected
== INVALID_ORDER
|| selected
== this->sel_index
) ? -1 : selected
;
527 case WID_VT_START_DATE
: // Change the date that the timetable starts.
528 ShowSetDateWindow(this, v
->index
| (v
->orders
.list
->IsCompleteTimetable() && _ctrl_pressed
? 1U << 20 : 0), _date
, _cur_year
, _cur_year
+ 15, ChangeTimetableStartCallback
);
531 case WID_VT_CHANGE_TIME
: { // "Wait For" button.
532 int selected
= this->sel_index
;
533 VehicleOrderID real
= (selected
+ 1) / 2;
535 if (real
>= v
->GetNumOrders()) real
= 0;
537 const Order
*order
= v
->GetOrder(real
);
538 StringID current
= STR_EMPTY
;
541 uint time
= (selected
% 2 == 1) ? order
->travel_time
: order
->wait_time
;
542 if (!_settings_client
.gui
.timetable_in_ticks
) time
/= DAY_TICKS
;
546 current
= STR_JUST_INT
;
550 this->query_is_speed_query
= false;
551 ShowQueryString(current
, STR_TIMETABLE_CHANGE_TIME
, 31, this, CS_NUMERAL
, QSF_NONE
);
555 case WID_VT_CHANGE_SPEED
: { // Change max speed button.
556 int selected
= this->sel_index
;
557 VehicleOrderID real
= (selected
+ 1) / 2;
559 if (real
>= v
->GetNumOrders()) real
= 0;
561 StringID current
= STR_EMPTY
;
562 const Order
*order
= v
->GetOrder(real
);
564 if (order
->max_speed
!= UINT16_MAX
) {
565 SetDParam(0, ConvertKmhishSpeedToDisplaySpeed(order
->max_speed
));
566 current
= STR_JUST_INT
;
570 this->query_is_speed_query
= true;
571 ShowQueryString(current
, STR_TIMETABLE_CHANGE_SPEED
, 31, this, CS_NUMERAL
, QSF_NONE
);
575 case WID_VT_CLEAR_TIME
: { // Clear waiting time.
576 uint32 p1
= PackTimetableArgs(v
, this->sel_index
, false);
577 DoCommandP(0, p1
, 0, CMD_CHANGE_TIMETABLE
| CMD_MSG(STR_ERROR_CAN_T_TIMETABLE_VEHICLE
));
581 case WID_VT_CLEAR_SPEED
: { // Clear max speed button.
582 uint32 p1
= PackTimetableArgs(v
, this->sel_index
, true);
583 DoCommandP(0, p1
, UINT16_MAX
, CMD_CHANGE_TIMETABLE
| CMD_MSG(STR_ERROR_CAN_T_TIMETABLE_VEHICLE
));
587 case WID_VT_RESET_LATENESS
: // Reset the vehicle's late counter.
588 DoCommandP(0, v
->index
, 0, CMD_SET_VEHICLE_ON_TIME
| CMD_MSG(STR_ERROR_CAN_T_TIMETABLE_VEHICLE
));
591 case WID_VT_AUTOFILL
: { // Autofill the timetable.
593 if (!HasBit(v
->vehicle_flags
, VF_AUTOFILL_TIMETABLE
)) SetBit(p2
, 0);
594 if (_ctrl_pressed
) SetBit(p2
, 1);
595 DoCommandP(0, v
->index
, p2
, CMD_AUTOFILL_TIMETABLE
| CMD_MSG(STR_ERROR_CAN_T_TIMETABLE_VEHICLE
));
599 case WID_VT_EXPECTED
:
600 this->show_expected
= !this->show_expected
;
603 case WID_VT_SHARED_ORDER_LIST
:
604 ShowVehicleListWindow(v
);
611 virtual void OnQueryTextFinished(char *str
)
613 if (str
== NULL
) return;
615 const Vehicle
*v
= this->vehicle
;
617 uint32 p1
= PackTimetableArgs(v
, this->sel_index
, this->query_is_speed_query
);
619 uint64 val
= StrEmpty(str
) ? 0 : strtoul(str
, NULL
, 10);
620 if (this->query_is_speed_query
) {
621 val
= ConvertDisplaySpeedToKmhishSpeed(val
);
623 if (!_settings_client
.gui
.timetable_in_ticks
) val
*= DAY_TICKS
;
626 uint32 p2
= minu(val
, UINT16_MAX
);
628 DoCommandP(0, p1
, p2
, CMD_CHANGE_TIMETABLE
| CMD_MSG(STR_ERROR_CAN_T_TIMETABLE_VEHICLE
));
631 virtual void OnResize()
633 /* Update the scroll bar */
634 this->vscroll
->SetCapacityFromWidget(this, WID_VT_TIMETABLE_PANEL
, WD_FRAMERECT_TOP
+ WD_FRAMERECT_BOTTOM
);
638 * Update the selection state of the arrival/departure data
640 void UpdateSelectionStates()
642 this->GetWidget
<NWidgetStacked
>(WID_VT_ARRIVAL_DEPARTURE_SELECTION
)->SetDisplayedPlane(_settings_client
.gui
.timetable_arrival_departure
? 0 : SZSP_NONE
);
643 this->GetWidget
<NWidgetStacked
>(WID_VT_EXPECTED_SELECTION
)->SetDisplayedPlane(_settings_client
.gui
.timetable_arrival_departure
? 0 : 1);
647 static const NWidgetPart _nested_timetable_widgets
[] = {
648 NWidget(NWID_HORIZONTAL
),
649 NWidget(WWT_CLOSEBOX
, COLOUR_GREY
),
650 NWidget(WWT_CAPTION
, COLOUR_GREY
, WID_VT_CAPTION
), SetDataTip(STR_TIMETABLE_TITLE
, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS
),
651 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_VT_ORDER_VIEW
), SetMinimalSize(61, 14), SetDataTip( STR_TIMETABLE_ORDER_VIEW
, STR_TIMETABLE_ORDER_VIEW_TOOLTIP
),
652 NWidget(WWT_SHADEBOX
, COLOUR_GREY
),
653 NWidget(WWT_DEFSIZEBOX
, COLOUR_GREY
),
654 NWidget(WWT_STICKYBOX
, COLOUR_GREY
),
656 NWidget(NWID_HORIZONTAL
),
657 NWidget(WWT_PANEL
, COLOUR_GREY
, WID_VT_TIMETABLE_PANEL
), SetMinimalSize(388, 82), SetResize(1, 10), SetDataTip(STR_NULL
, STR_TIMETABLE_TOOLTIP
), SetScrollbar(WID_VT_SCROLLBAR
), EndContainer(),
658 NWidget(NWID_SELECTION
, INVALID_COLOUR
, WID_VT_ARRIVAL_DEPARTURE_SELECTION
),
659 NWidget(WWT_PANEL
, COLOUR_GREY
, WID_VT_ARRIVAL_DEPARTURE_PANEL
), SetMinimalSize(110, 0), SetFill(0, 1), SetDataTip(STR_NULL
, STR_TIMETABLE_TOOLTIP
), SetScrollbar(WID_VT_SCROLLBAR
), EndContainer(),
661 NWidget(NWID_VSCROLLBAR
, COLOUR_GREY
, WID_VT_SCROLLBAR
),
663 NWidget(WWT_PANEL
, COLOUR_GREY
, WID_VT_SUMMARY_PANEL
), SetMinimalSize(400, 22), SetResize(1, 0), EndContainer(),
664 NWidget(NWID_HORIZONTAL
),
665 NWidget(NWID_HORIZONTAL
, NC_EQUALSIZE
),
666 NWidget(NWID_VERTICAL
, NC_EQUALSIZE
),
667 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_VT_CHANGE_TIME
), SetResize(1, 0), SetFill(1, 1), SetDataTip(STR_TIMETABLE_CHANGE_TIME
, STR_TIMETABLE_WAIT_TIME_TOOLTIP
),
668 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_VT_CLEAR_TIME
), SetResize(1, 0), SetFill(1, 1), SetDataTip(STR_TIMETABLE_CLEAR_TIME
, STR_TIMETABLE_CLEAR_TIME_TOOLTIP
),
670 NWidget(NWID_VERTICAL
, NC_EQUALSIZE
),
671 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_VT_CHANGE_SPEED
), SetResize(1, 0), SetFill(1, 1), SetDataTip(STR_TIMETABLE_CHANGE_SPEED
, STR_TIMETABLE_CHANGE_SPEED_TOOLTIP
),
672 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_VT_CLEAR_SPEED
), SetResize(1, 0), SetFill(1, 1), SetDataTip(STR_TIMETABLE_CLEAR_SPEED
, STR_TIMETABLE_CLEAR_SPEED_TOOLTIP
),
674 NWidget(NWID_VERTICAL
, NC_EQUALSIZE
),
675 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_VT_START_DATE
), SetResize(1, 0), SetFill(1, 1), SetDataTip(STR_TIMETABLE_STARTING_DATE
, STR_TIMETABLE_STARTING_DATE_TOOLTIP
),
676 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_VT_RESET_LATENESS
), SetResize(1, 0), SetFill(1, 1), SetDataTip(STR_TIMETABLE_RESET_LATENESS
, STR_TIMETABLE_RESET_LATENESS_TOOLTIP
),
678 NWidget(NWID_VERTICAL
, NC_EQUALSIZE
),
679 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_VT_AUTOFILL
), SetResize(1, 0), SetFill(1, 1), SetDataTip(STR_TIMETABLE_AUTOFILL
, STR_TIMETABLE_AUTOFILL_TOOLTIP
),
680 NWidget(NWID_SELECTION
, INVALID_COLOUR
, WID_VT_EXPECTED_SELECTION
),
681 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_VT_EXPECTED
), SetResize(1, 0), SetFill(1, 1), SetDataTip(STR_BLACK_STRING
, STR_TIMETABLE_EXPECTED_TOOLTIP
),
682 NWidget(WWT_PANEL
, COLOUR_GREY
), SetResize(1, 0), SetFill(1, 1), EndContainer(),
686 NWidget(NWID_VERTICAL
, NC_EQUALSIZE
),
687 NWidget(WWT_PUSHIMGBTN
, COLOUR_GREY
, WID_VT_SHARED_ORDER_LIST
), SetFill(0, 1), SetDataTip(SPR_SHARED_ORDERS_ICON
, STR_ORDERS_VEH_WITH_SHARED_ORDERS_LIST_TOOLTIP
),
688 NWidget(WWT_RESIZEBOX
, COLOUR_GREY
), SetFill(0, 1),
693 static WindowDesc
_timetable_desc(
694 WDP_AUTO
, "view_vehicle_timetable", 400, 130,
695 WC_VEHICLE_TIMETABLE
, WC_VEHICLE_VIEW
,
697 _nested_timetable_widgets
, lengthof(_nested_timetable_widgets
)
701 * Show the timetable for a given vehicle.
702 * @param v The vehicle to show the timetable for.
704 void ShowTimetableWindow(const Vehicle
*v
)
706 DeleteWindowById(WC_VEHICLE_DETAILS
, v
->index
, false);
707 DeleteWindowById(WC_VEHICLE_ORDERS
, v
->index
, false);
708 AllocateWindowDescFront
<TimetableWindow
>(&_timetable_desc
, v
->index
);