Feature: Ctrl-click to bulk edit timetable speeds/waiting times (#10265)
[openttd-github.git] / src / timetable_gui.cpp
blobf6b1e3da931e9702b74d21308d3ee9d7c204225f
1 /*
2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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/>.
6 */
8 /** @file timetable_gui.cpp GUI for time tabling. */
10 #include "stdafx.h"
11 #include "command_func.h"
12 #include "gui.h"
13 #include "window_gui.h"
14 #include "window_func.h"
15 #include "textbuf_gui.h"
16 #include "strings_func.h"
17 #include "vehicle_base.h"
18 #include "string_func.h"
19 #include "gfx_func.h"
20 #include "company_func.h"
21 #include "date_func.h"
22 #include "date_gui.h"
23 #include "vehicle_gui.h"
24 #include "settings_type.h"
25 #include "timetable_cmd.h"
26 #include <cstdint>
28 #include "widgets/timetable_widget.h"
30 #include "table/sprites.h"
31 #include "table/strings.h"
33 #include "safeguards.h"
35 /** Container for the arrival/departure dates of a vehicle */
36 struct TimetableArrivalDeparture {
37 Ticks arrival; ///< The arrival time
38 Ticks departure; ///< The departure time
41 /**
42 * Set the timetable parameters in the format as described by the setting.
43 * @param param1 the first DParam to fill
44 * @param param2 the second DParam to fill
45 * @param ticks the number of ticks to 'draw'
47 void SetTimetableParams(int param1, int param2, Ticks ticks)
49 if (_settings_client.gui.timetable_in_ticks) {
50 SetDParam(param1, STR_TIMETABLE_TICKS);
51 SetDParam(param2, ticks);
52 } else {
53 SetDParam(param1, STR_TIMETABLE_DAYS);
54 SetDParam(param2, ticks / DAY_TICKS);
58 /**
59 * Check whether it is possible to determine how long the order takes.
60 * @param order the order to check.
61 * @param travelling whether we are interested in the travel or the wait part.
62 * @return true if the travel/wait time can be used.
64 static bool CanDetermineTimeTaken(const Order *order, bool travelling)
66 /* Current order is conditional */
67 if (order->IsType(OT_CONDITIONAL) || order->IsType(OT_IMPLICIT)) return false;
68 /* No travel time and we have not already finished travelling */
69 if (travelling && !order->IsTravelTimetabled()) return false;
70 /* No wait time but we are loading at this timetabled station */
71 if (!travelling && !order->IsWaitTimetabled() && order->IsType(OT_GOTO_STATION) &&
72 !(order->GetNonStopType() & ONSF_NO_STOP_AT_DESTINATION_STATION)) {
73 return false;
76 return true;
80 /**
81 * Fill the table with arrivals and departures
82 * @param v Vehicle which must have at least 2 orders.
83 * @param start order index to start at
84 * @param travelling Are we still in the travelling part of the start order
85 * @param table Fill in arrival and departures including intermediate orders
86 * @param offset Add this value to result and all arrivals and departures
88 static void FillTimetableArrivalDepartureTable(const Vehicle *v, VehicleOrderID start, bool travelling, TimetableArrivalDeparture *table, Ticks offset)
90 assert(table != nullptr);
91 assert(v->GetNumOrders() >= 2);
92 assert(start < v->GetNumOrders());
94 Ticks sum = offset;
95 VehicleOrderID i = start;
96 const Order *order = v->GetOrder(i);
98 /* Pre-initialize with unknown time */
99 for (int i = 0; i < v->GetNumOrders(); ++i) {
100 table[i].arrival = table[i].departure = INVALID_TICKS;
103 /* Cyclically loop over all orders until we reach the current one again.
104 * As we may start at the current order, do a post-checking loop */
105 do {
106 /* Automatic orders don't influence the overall timetable;
107 * they just add some untimetabled entries, but the time till
108 * the next non-implicit order can still be known. */
109 if (!order->IsType(OT_IMPLICIT)) {
110 if (travelling || i != start) {
111 if (!CanDetermineTimeTaken(order, true)) return;
112 sum += order->GetTimetabledTravel();
113 table[i].arrival = sum;
116 if (!CanDetermineTimeTaken(order, false)) return;
117 sum += order->GetTimetabledWait();
118 table[i].departure = sum;
121 ++i;
122 order = order->next;
123 if (i >= v->GetNumOrders()) {
124 i = 0;
125 assert(order == nullptr);
126 order = v->orders->GetFirstOrder();
128 } while (i != start);
130 /* When loading at a scheduled station we still have to treat the
131 * travelling part of the first order. */
132 if (!travelling) {
133 if (!CanDetermineTimeTaken(order, true)) return;
134 sum += order->GetTimetabledTravel();
135 table[i].arrival = sum;
141 * Callback for when a time has been chosen to start the time table
142 * @param w the window related to the setting of the date
143 * @param date the actually chosen date
145 static void ChangeTimetableStartCallback(const Window *w, Date date, void *data)
147 Command<CMD_SET_TIMETABLE_START>::Post(STR_ERROR_CAN_T_TIMETABLE_VEHICLE, (VehicleID)w->window_number, reinterpret_cast<std::uintptr_t>(data) != 0, date);
151 struct TimetableWindow : Window {
152 int sel_index;
153 const Vehicle *vehicle; ///< Vehicle monitored by the window.
154 bool show_expected; ///< Whether we show expected arrival or scheduled
155 uint deparr_time_width; ///< The width of the departure/arrival time
156 uint deparr_abbr_width; ///< The width of the departure/arrival abbreviation
157 Scrollbar *vscroll;
158 bool query_is_speed_query; ///< The currently open query window is a speed query and not a time query.
159 bool set_start_date_all; ///< Set start date using minutes text entry for all timetable entries (ctrl-click) action
160 bool change_timetable_all; ///< Set wait time or speed for all timetable entries (ctrl-click) action
162 TimetableWindow(WindowDesc *desc, WindowNumber window_number) :
163 Window(desc),
164 sel_index(-1),
165 vehicle(Vehicle::Get(window_number)),
166 show_expected(true)
168 this->CreateNestedTree();
169 this->vscroll = this->GetScrollbar(WID_VT_SCROLLBAR);
170 this->UpdateSelectionStates();
171 this->FinishInitNested(window_number);
173 this->owner = this->vehicle->owner;
177 * Build the arrival-departure list for a given vehicle
178 * @param v the vehicle to make the list for
179 * @param table the table to fill
180 * @return if next arrival will be early
182 static bool BuildArrivalDepartureList(const Vehicle *v, TimetableArrivalDeparture *table)
184 assert(HasBit(v->vehicle_flags, VF_TIMETABLE_STARTED));
186 bool travelling = (!v->current_order.IsType(OT_LOADING) || v->current_order.GetNonStopType() == ONSF_STOP_EVERYWHERE);
187 Ticks start_time = _date_fract - v->current_order_time;
189 FillTimetableArrivalDepartureTable(v, v->cur_real_order_index % v->GetNumOrders(), travelling, table, start_time);
191 return (travelling && v->lateness_counter < 0);
194 void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
196 switch (widget) {
197 case WID_VT_ARRIVAL_DEPARTURE_PANEL:
198 SetDParamMaxValue(0, MAX_YEAR * DAYS_IN_YEAR, 0, FS_SMALL);
199 this->deparr_time_width = GetStringBoundingBox(STR_JUST_DATE_TINY).width;
200 this->deparr_abbr_width = std::max(GetStringBoundingBox(STR_TIMETABLE_ARRIVAL_ABBREVIATION).width, GetStringBoundingBox(STR_TIMETABLE_DEPARTURE_ABBREVIATION).width);
201 size->width = this->deparr_abbr_width + WidgetDimensions::scaled.hsep_wide + this->deparr_time_width + padding.width;
202 FALLTHROUGH;
204 case WID_VT_ARRIVAL_DEPARTURE_SELECTION:
205 case WID_VT_TIMETABLE_PANEL:
206 resize->height = FONT_HEIGHT_NORMAL;
207 size->height = 8 * resize->height + padding.height;
208 break;
210 case WID_VT_SUMMARY_PANEL:
211 size->height = 2 * FONT_HEIGHT_NORMAL + padding.height;
212 break;
216 int GetOrderFromTimetableWndPt(int y, const Vehicle *v)
218 uint sel = (y - this->GetWidget<NWidgetBase>(WID_VT_TIMETABLE_PANEL)->pos_y - WidgetDimensions::scaled.framerect.top) / FONT_HEIGHT_NORMAL;
220 if (sel >= this->vscroll->GetCapacity()) return INVALID_ORDER;
222 sel += this->vscroll->GetPosition();
224 return (sel < v->GetNumOrders() * 2u) ? sel : INVALID_ORDER;
228 * Some data on this window has become invalid.
229 * @param data Information about the changed data.
230 * @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.
232 void OnInvalidateData(int data = 0, bool gui_scope = true) override
234 switch (data) {
235 case VIWD_AUTOREPLACE:
236 /* Autoreplace replaced the vehicle */
237 this->vehicle = Vehicle::Get(this->window_number);
238 break;
240 case VIWD_REMOVE_ALL_ORDERS:
241 /* Removed / replaced all orders (after deleting / sharing) */
242 if (this->sel_index == -1) break;
244 this->CloseChildWindows();
245 this->sel_index = -1;
246 break;
248 case VIWD_MODIFY_ORDERS:
249 if (!gui_scope) break;
250 this->UpdateSelectionStates();
251 this->ReInit();
252 break;
254 default: {
255 if (gui_scope) break; // only do this once; from command scope
257 /* Moving an order. If one of these is INVALID_VEH_ORDER_ID, then
258 * the order is being created / removed */
259 if (this->sel_index == -1) break;
261 VehicleOrderID from = GB(data, 0, 8);
262 VehicleOrderID to = GB(data, 8, 8);
264 if (from == to) break; // no need to change anything
266 /* if from == INVALID_VEH_ORDER_ID, one order was added; if to == INVALID_VEH_ORDER_ID, one order was removed */
267 uint old_num_orders = this->vehicle->GetNumOrders() - (uint)(from == INVALID_VEH_ORDER_ID) + (uint)(to == INVALID_VEH_ORDER_ID);
269 VehicleOrderID selected_order = (this->sel_index + 1) / 2;
270 if (selected_order == old_num_orders) selected_order = 0; // when last travel time is selected, it belongs to order 0
272 bool travel = HasBit(this->sel_index, 0);
274 if (from != selected_order) {
275 /* Moving from preceding order? */
276 selected_order -= (int)(from <= selected_order);
277 /* Moving to preceding order? */
278 selected_order += (int)(to <= selected_order);
279 } else {
280 /* Now we are modifying the selected order */
281 if (to == INVALID_VEH_ORDER_ID) {
282 /* Deleting selected order */
283 this->CloseChildWindows();
284 this->sel_index = -1;
285 break;
286 } else {
287 /* Moving selected order */
288 selected_order = to;
292 /* recompute new sel_index */
293 this->sel_index = 2 * selected_order - (int)travel;
294 /* travel time of first order needs special handling */
295 if (this->sel_index == -1) this->sel_index = this->vehicle->GetNumOrders() * 2 - 1;
296 break;
302 void OnPaint() override
304 const Vehicle *v = this->vehicle;
305 int selected = this->sel_index;
307 this->vscroll->SetCount(v->GetNumOrders() * 2);
309 if (v->owner == _local_company) {
310 bool disable = true;
311 if (selected != -1) {
312 const Order *order = v->GetOrder(((selected + 1) / 2) % v->GetNumOrders());
313 if (selected % 2 == 1) {
314 disable = order != nullptr && (order->IsType(OT_CONDITIONAL) || order->IsType(OT_IMPLICIT));
315 } else {
316 disable = order == nullptr || ((!order->IsType(OT_GOTO_STATION) || (order->GetNonStopType() & ONSF_NO_STOP_AT_DESTINATION_STATION)) && !order->IsType(OT_CONDITIONAL));
319 bool disable_speed = disable || selected % 2 != 1 || v->type == VEH_AIRCRAFT;
321 this->SetWidgetDisabledState(WID_VT_CHANGE_TIME, disable);
322 this->SetWidgetDisabledState(WID_VT_CLEAR_TIME, disable);
323 this->SetWidgetDisabledState(WID_VT_CHANGE_SPEED, disable_speed);
324 this->SetWidgetDisabledState(WID_VT_CLEAR_SPEED, disable_speed);
325 this->SetWidgetDisabledState(WID_VT_SHARED_ORDER_LIST, !v->IsOrderListShared());
327 this->SetWidgetDisabledState(WID_VT_START_DATE, v->orders == nullptr);
328 this->SetWidgetDisabledState(WID_VT_RESET_LATENESS, v->orders == nullptr);
329 this->SetWidgetDisabledState(WID_VT_AUTOFILL, v->orders == nullptr);
330 } else {
331 this->DisableWidget(WID_VT_START_DATE);
332 this->DisableWidget(WID_VT_CHANGE_TIME);
333 this->DisableWidget(WID_VT_CLEAR_TIME);
334 this->DisableWidget(WID_VT_CHANGE_SPEED);
335 this->DisableWidget(WID_VT_CLEAR_SPEED);
336 this->DisableWidget(WID_VT_RESET_LATENESS);
337 this->DisableWidget(WID_VT_AUTOFILL);
338 this->DisableWidget(WID_VT_SHARED_ORDER_LIST);
341 this->SetWidgetLoweredState(WID_VT_AUTOFILL, HasBit(v->vehicle_flags, VF_AUTOFILL_TIMETABLE));
343 this->DrawWidgets();
346 void SetStringParameters(int widget) const override
348 switch (widget) {
349 case WID_VT_CAPTION: SetDParam(0, this->vehicle->index); break;
350 case WID_VT_EXPECTED: SetDParam(0, this->show_expected ? STR_TIMETABLE_EXPECTED : STR_TIMETABLE_SCHEDULED); break;
354 void DrawWidget(const Rect &r, int widget) const override
356 const Vehicle *v = this->vehicle;
357 int selected = this->sel_index;
359 switch (widget) {
360 case WID_VT_TIMETABLE_PANEL: {
361 Rect tr = r.Shrink(WidgetDimensions::scaled.framerect);
362 int i = this->vscroll->GetPosition();
363 VehicleOrderID order_id = (i + 1) / 2;
364 bool final_order = false;
366 bool rtl = _current_text_dir == TD_RTL;
367 SetDParamMaxValue(0, v->GetNumOrders(), 2);
368 int index_column_width = GetStringBoundingBox(STR_ORDER_INDEX).width + 2 * GetSpriteSize(rtl ? SPR_ARROW_RIGHT : SPR_ARROW_LEFT).width + WidgetDimensions::scaled.hsep_normal;
369 int middle = rtl ? tr.right - index_column_width : tr.left + index_column_width;
371 const Order *order = v->GetOrder(order_id);
372 while (order != nullptr) {
373 /* Don't draw anything if it extends past the end of the window. */
374 if (!this->vscroll->IsVisible(i)) break;
376 if (i % 2 == 0) {
377 DrawOrderString(v, order, order_id, tr.top, i == selected, true, tr.left, middle, tr.right);
379 order_id++;
381 if (order_id >= v->GetNumOrders()) {
382 order = v->GetOrder(0);
383 final_order = true;
384 } else {
385 order = order->next;
387 } else {
388 StringID string;
389 TextColour colour = (i == selected) ? TC_WHITE : TC_BLACK;
390 if (order->IsType(OT_CONDITIONAL)) {
391 string = STR_TIMETABLE_NO_TRAVEL;
392 } else if (order->IsType(OT_IMPLICIT)) {
393 string = STR_TIMETABLE_NOT_TIMETABLEABLE;
394 colour = ((i == selected) ? TC_SILVER : TC_GREY) | TC_NO_SHADE;
395 } else if (!order->IsTravelTimetabled()) {
396 if (order->GetTravelTime() > 0) {
397 SetTimetableParams(0, 1, order->GetTravelTime());
398 string = order->GetMaxSpeed() != UINT16_MAX ?
399 STR_TIMETABLE_TRAVEL_FOR_SPEED_ESTIMATED :
400 STR_TIMETABLE_TRAVEL_FOR_ESTIMATED;
401 } else {
402 string = order->GetMaxSpeed() != UINT16_MAX ?
403 STR_TIMETABLE_TRAVEL_NOT_TIMETABLED_SPEED :
404 STR_TIMETABLE_TRAVEL_NOT_TIMETABLED;
406 } else {
407 SetTimetableParams(0, 1, order->GetTimetabledTravel());
408 string = order->GetMaxSpeed() != UINT16_MAX ?
409 STR_TIMETABLE_TRAVEL_FOR_SPEED : STR_TIMETABLE_TRAVEL_FOR;
411 SetDParam(2, order->GetMaxSpeed());
413 DrawString(rtl ? tr.left : middle, rtl ? middle : tr.right, tr.top, string, colour);
415 if (final_order) break;
418 i++;
419 tr.top += FONT_HEIGHT_NORMAL;
421 break;
424 case WID_VT_ARRIVAL_DEPARTURE_PANEL: {
425 /* Arrival and departure times are handled in an all-or-nothing approach,
426 * i.e. are only shown if we can calculate all times.
427 * Excluding order lists with only one order makes some things easier.
429 Ticks total_time = v->orders != nullptr ? v->orders->GetTimetableDurationIncomplete() : 0;
430 if (total_time <= 0 || v->GetNumOrders() <= 1 || !HasBit(v->vehicle_flags, VF_TIMETABLE_STARTED)) break;
432 TimetableArrivalDeparture *arr_dep = AllocaM(TimetableArrivalDeparture, v->GetNumOrders());
433 const VehicleOrderID cur_order = v->cur_real_order_index % v->GetNumOrders();
435 VehicleOrderID earlyID = BuildArrivalDepartureList(v, arr_dep) ? cur_order : (VehicleOrderID)INVALID_VEH_ORDER_ID;
437 Rect tr = r.Shrink(WidgetDimensions::scaled.framerect);
438 bool show_late = this->show_expected && v->lateness_counter > DAY_TICKS;
439 Ticks offset = show_late ? 0 : -v->lateness_counter;
441 bool rtl = _current_text_dir == TD_RTL;
442 Rect abbr = tr.WithWidth(this->deparr_abbr_width, rtl);
443 Rect time = tr.WithWidth(this->deparr_time_width, !rtl);
445 for (int i = this->vscroll->GetPosition(); i / 2 < v->GetNumOrders(); ++i) { // note: i is also incremented in the loop
446 /* Don't draw anything if it extends past the end of the window. */
447 if (!this->vscroll->IsVisible(i)) break;
449 if (i % 2 == 0) {
450 if (arr_dep[i / 2].arrival != INVALID_TICKS) {
451 DrawString(abbr.left, abbr.right, tr.top, STR_TIMETABLE_ARRIVAL_ABBREVIATION, i == selected ? TC_WHITE : TC_BLACK);
452 if (this->show_expected && i / 2 == earlyID) {
453 SetDParam(0, _date + arr_dep[i / 2].arrival / DAY_TICKS);
454 DrawString(time.left, time.right, tr.top, STR_JUST_DATE_TINY, TC_GREEN);
455 } else {
456 SetDParam(0, _date + (arr_dep[i / 2].arrival + offset) / DAY_TICKS);
457 DrawString(time.left, time.right, tr.top, STR_JUST_DATE_TINY,
458 show_late ? TC_RED : i == selected ? TC_WHITE : TC_BLACK);
461 } else {
462 if (arr_dep[i / 2].departure != INVALID_TICKS) {
463 DrawString(abbr.left, abbr.right, tr.top, STR_TIMETABLE_DEPARTURE_ABBREVIATION, i == selected ? TC_WHITE : TC_BLACK);
464 SetDParam(0, _date + (arr_dep[i/2].departure + offset) / DAY_TICKS);
465 DrawString(time.left, time.right, tr.top, STR_JUST_DATE_TINY,
466 show_late ? TC_RED : i == selected ? TC_WHITE : TC_BLACK);
469 tr.top += FONT_HEIGHT_NORMAL;
471 break;
474 case WID_VT_SUMMARY_PANEL: {
475 Rect tr = r.Shrink(WidgetDimensions::scaled.framerect);
477 Ticks total_time = v->orders != nullptr ? v->orders->GetTimetableDurationIncomplete() : 0;
478 if (total_time != 0) {
479 SetTimetableParams(0, 1, total_time);
480 DrawString(tr, v->orders->IsCompleteTimetable() ? STR_TIMETABLE_TOTAL_TIME : STR_TIMETABLE_TOTAL_TIME_INCOMPLETE);
482 tr.top += FONT_HEIGHT_NORMAL;
484 if (v->timetable_start != 0) {
485 /* We are running towards the first station so we can start the
486 * timetable at the given time. */
487 SetDParam(0, STR_JUST_DATE_TINY);
488 SetDParam(1, v->timetable_start);
489 DrawString(tr, STR_TIMETABLE_STATUS_START_AT);
490 } else if (!HasBit(v->vehicle_flags, VF_TIMETABLE_STARTED)) {
491 /* We aren't running on a timetable yet, so how can we be "on time"
492 * when we aren't even "on service"/"on duty"? */
493 DrawString(tr, STR_TIMETABLE_STATUS_NOT_STARTED);
494 } else if (v->lateness_counter == 0 || (!_settings_client.gui.timetable_in_ticks && v->lateness_counter / DAY_TICKS == 0)) {
495 DrawString(tr, STR_TIMETABLE_STATUS_ON_TIME);
496 } else {
497 SetTimetableParams(0, 1, abs(v->lateness_counter));
498 DrawString(tr, v->lateness_counter < 0 ? STR_TIMETABLE_STATUS_EARLY : STR_TIMETABLE_STATUS_LATE);
500 break;
505 static inline std::tuple<VehicleOrderID, ModifyTimetableFlags> PackTimetableArgs(const Vehicle *v, uint selected, bool speed)
507 uint order_number = (selected + 1) / 2;
508 ModifyTimetableFlags mtf = (selected % 2 == 1) ? (speed ? MTF_TRAVEL_SPEED : MTF_TRAVEL_TIME) : MTF_WAIT_TIME;
510 if (order_number >= v->GetNumOrders()) order_number = 0;
512 return { order_number, mtf };
515 void OnClick(Point pt, int widget, int click_count) override
517 const Vehicle *v = this->vehicle;
519 switch (widget) {
520 case WID_VT_ORDER_VIEW: // Order view button
521 ShowOrdersWindow(v);
522 break;
524 case WID_VT_TIMETABLE_PANEL: { // Main panel.
525 int selected = GetOrderFromTimetableWndPt(pt.y, v);
527 this->CloseChildWindows();
528 this->sel_index = (selected == INVALID_ORDER || selected == this->sel_index) ? -1 : selected;
529 break;
532 case WID_VT_START_DATE: // Change the date that the timetable starts.
533 ShowSetDateWindow(this, v->index, _date, _cur_year, _cur_year + 15, ChangeTimetableStartCallback, reinterpret_cast<void *>(static_cast<uintptr_t>(v->orders->IsCompleteTimetable() && _ctrl_pressed)));
534 break;
536 case WID_VT_CHANGE_TIME: { // "Wait For" button.
537 int selected = this->sel_index;
538 VehicleOrderID real = (selected + 1) / 2;
540 if (real >= v->GetNumOrders()) real = 0;
542 const Order *order = v->GetOrder(real);
543 StringID current = STR_EMPTY;
545 if (order != nullptr) {
546 uint time = (selected % 2 == 1) ? order->GetTravelTime() : order->GetWaitTime();
547 if (!_settings_client.gui.timetable_in_ticks) time /= DAY_TICKS;
549 if (time != 0) {
550 SetDParam(0, time);
551 current = STR_JUST_INT;
555 this->query_is_speed_query = false;
556 this->change_timetable_all = _ctrl_pressed && (order != nullptr);
557 ShowQueryString(current, STR_TIMETABLE_CHANGE_TIME, 31, this, CS_NUMERAL, QSF_ACCEPT_UNCHANGED);
558 break;
561 case WID_VT_CHANGE_SPEED: { // Change max speed button.
562 int selected = this->sel_index;
563 VehicleOrderID real = (selected + 1) / 2;
565 if (real >= v->GetNumOrders()) real = 0;
567 StringID current = STR_EMPTY;
568 const Order *order = v->GetOrder(real);
569 if (order != nullptr) {
570 if (order->GetMaxSpeed() != UINT16_MAX) {
571 SetDParam(0, ConvertKmhishSpeedToDisplaySpeed(order->GetMaxSpeed()));
572 current = STR_JUST_INT;
576 this->query_is_speed_query = true;
577 this->change_timetable_all = _ctrl_pressed && (order != nullptr);
578 ShowQueryString(current, STR_TIMETABLE_CHANGE_SPEED, 31, this, CS_NUMERAL, QSF_NONE);
579 break;
582 case WID_VT_CLEAR_TIME: { // Clear waiting time.
583 auto [order_id, mtf] = PackTimetableArgs(v, this->sel_index, false);
584 if (_ctrl_pressed) {
585 Command<CMD_BULK_CHANGE_TIMETABLE>::Post(STR_ERROR_CAN_T_TIMETABLE_VEHICLE, v->index, mtf, 0);
586 } else {
587 Command<CMD_CHANGE_TIMETABLE>::Post(STR_ERROR_CAN_T_TIMETABLE_VEHICLE, v->index, order_id, mtf, 0);
589 break;
592 case WID_VT_CLEAR_SPEED: { // Clear max speed button.
593 auto [order_id, mtf] = PackTimetableArgs(v, this->sel_index, true);
594 if (_ctrl_pressed) {
595 Command<CMD_BULK_CHANGE_TIMETABLE>::Post(STR_ERROR_CAN_T_TIMETABLE_VEHICLE, v->index, mtf, UINT16_MAX);
596 } else {
597 Command<CMD_CHANGE_TIMETABLE>::Post(STR_ERROR_CAN_T_TIMETABLE_VEHICLE, v->index, order_id, mtf, UINT16_MAX);
599 break;
602 case WID_VT_RESET_LATENESS: // Reset the vehicle's late counter.
603 Command<CMD_SET_VEHICLE_ON_TIME>::Post(STR_ERROR_CAN_T_TIMETABLE_VEHICLE, v->index);
604 break;
606 case WID_VT_AUTOFILL: { // Autofill the timetable.
607 Command<CMD_AUTOFILL_TIMETABLE>::Post(STR_ERROR_CAN_T_TIMETABLE_VEHICLE, v->index, !HasBit(v->vehicle_flags, VF_AUTOFILL_TIMETABLE), _ctrl_pressed);
608 break;
611 case WID_VT_EXPECTED:
612 this->show_expected = !this->show_expected;
613 break;
615 case WID_VT_SHARED_ORDER_LIST:
616 ShowVehicleListWindow(v);
617 break;
620 this->SetDirty();
623 void OnQueryTextFinished(char *str) override
625 if (str == nullptr) return;
627 const Vehicle *v = this->vehicle;
629 uint64 val = StrEmpty(str) ? 0 : strtoul(str, nullptr, 10);
630 if (this->query_is_speed_query) {
631 val = ConvertDisplaySpeedToKmhishSpeed(val);
632 } else {
633 if (!_settings_client.gui.timetable_in_ticks) val *= DAY_TICKS;
636 auto [order_id, mtf] = PackTimetableArgs(v, this->sel_index, this->query_is_speed_query);
638 if (this->change_timetable_all) {
639 Command<CMD_BULK_CHANGE_TIMETABLE>::Post(STR_ERROR_CAN_T_TIMETABLE_VEHICLE, v->index, mtf, std::min<uint32>(val, UINT16_MAX));
640 } else {
641 Command<CMD_CHANGE_TIMETABLE>::Post(STR_ERROR_CAN_T_TIMETABLE_VEHICLE, v->index, order_id, mtf, std::min<uint32>(val, UINT16_MAX));
645 void OnResize() override
647 /* Update the scroll bar */
648 this->vscroll->SetCapacityFromWidget(this, WID_VT_TIMETABLE_PANEL, WidgetDimensions::scaled.framerect.Vertical());
652 * Update the selection state of the arrival/departure data
654 void UpdateSelectionStates()
656 this->GetWidget<NWidgetStacked>(WID_VT_ARRIVAL_DEPARTURE_SELECTION)->SetDisplayedPlane(_settings_client.gui.timetable_arrival_departure ? 0 : SZSP_NONE);
657 this->GetWidget<NWidgetStacked>(WID_VT_EXPECTED_SELECTION)->SetDisplayedPlane(_settings_client.gui.timetable_arrival_departure ? 0 : 1);
661 static const NWidgetPart _nested_timetable_widgets[] = {
662 NWidget(NWID_HORIZONTAL),
663 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
664 NWidget(WWT_CAPTION, COLOUR_GREY, WID_VT_CAPTION), SetDataTip(STR_TIMETABLE_TITLE, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
665 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_VT_ORDER_VIEW), SetMinimalSize(61, 14), SetDataTip( STR_TIMETABLE_ORDER_VIEW, STR_TIMETABLE_ORDER_VIEW_TOOLTIP),
666 NWidget(WWT_SHADEBOX, COLOUR_GREY),
667 NWidget(WWT_DEFSIZEBOX, COLOUR_GREY),
668 NWidget(WWT_STICKYBOX, COLOUR_GREY),
669 EndContainer(),
670 NWidget(NWID_HORIZONTAL),
671 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(),
672 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_VT_ARRIVAL_DEPARTURE_SELECTION),
673 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(),
674 EndContainer(),
675 NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_VT_SCROLLBAR),
676 EndContainer(),
677 NWidget(WWT_PANEL, COLOUR_GREY, WID_VT_SUMMARY_PANEL), SetMinimalSize(400, 22), SetResize(1, 0), EndContainer(),
678 NWidget(NWID_HORIZONTAL),
679 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
680 NWidget(NWID_VERTICAL, NC_EQUALSIZE),
681 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),
682 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),
683 EndContainer(),
684 NWidget(NWID_VERTICAL, NC_EQUALSIZE),
685 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),
686 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),
687 EndContainer(),
688 NWidget(NWID_VERTICAL, NC_EQUALSIZE),
689 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),
690 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),
691 EndContainer(),
692 NWidget(NWID_VERTICAL, NC_EQUALSIZE),
693 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_VT_AUTOFILL), SetResize(1, 0), SetFill(1, 1), SetDataTip(STR_TIMETABLE_AUTOFILL, STR_TIMETABLE_AUTOFILL_TOOLTIP),
694 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_VT_EXPECTED_SELECTION),
695 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_VT_EXPECTED), SetResize(1, 0), SetFill(1, 1), SetDataTip(STR_BLACK_STRING, STR_TIMETABLE_EXPECTED_TOOLTIP),
696 NWidget(WWT_PANEL, COLOUR_GREY), SetResize(1, 0), SetFill(1, 1), EndContainer(),
697 EndContainer(),
698 EndContainer(),
699 EndContainer(),
700 NWidget(NWID_VERTICAL, NC_EQUALSIZE),
701 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),
702 NWidget(WWT_RESIZEBOX, COLOUR_GREY), SetFill(0, 1),
703 EndContainer(),
704 EndContainer(),
707 static WindowDesc _timetable_desc(
708 WDP_AUTO, "view_vehicle_timetable", 400, 130,
709 WC_VEHICLE_TIMETABLE, WC_VEHICLE_VIEW,
710 WDF_CONSTRUCTION,
711 _nested_timetable_widgets, lengthof(_nested_timetable_widgets)
715 * Show the timetable for a given vehicle.
716 * @param v The vehicle to show the timetable for.
718 void ShowTimetableWindow(const Vehicle *v)
720 CloseWindowById(WC_VEHICLE_DETAILS, v->index, false);
721 CloseWindowById(WC_VEHICLE_ORDERS, v->index, false);
722 AllocateWindowDescFront<TimetableWindow>(&_timetable_desc, v->index);