Remove ZeroedMemoryAllocator as a base class of Window
[openttd/fttd.git] / src / timetable_gui.cpp
blob89f0e2ffc8855ea38f93062c511c98b4dac43031
1 /* $Id$ */
3 /*
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/>.
8 */
10 /** @file timetable_gui.cpp GUI for time tabling. */
12 #include "stdafx.h"
13 #include "command_func.h"
14 #include "gui.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.h"
21 #include "gfx_func.h"
22 #include "company_func.h"
23 #include "date_func.h"
24 #include "date_gui.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
39 /**
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);
50 } else {
51 SetDParam(param1, STR_TIMETABLE_DAYS);
52 SetDParam(param2, ticks / DAY_TICKS);
56 /**
57 * Check whether it is possible to determine how long the order takes.
58 * @param order the order to check.
59 * @param travelling whether we are interested in the travel or the wait part.
60 * @return true if the travel/wait time can be used.
62 static bool CanDetermineTimeTaken(const Order *order, bool travelling)
64 /* Current order is conditional */
65 if (order->IsType(OT_CONDITIONAL) || order->IsType(OT_IMPLICIT)) return false;
66 /* No travel time and we have not already finished travelling */
67 if (travelling && !order->IsTravelTimetabled()) return false;
68 /* No wait time but we are loading at this timetabled station */
69 if (!travelling && !order->IsWaitTimetabled() && order->IsType(OT_GOTO_STATION) &&
70 !(order->GetNonStopType() & ONSF_NO_STOP_AT_DESTINATION_STATION)) {
71 return false;
74 return true;
78 /**
79 * Fill the table with arrivals and departures
80 * @param v Vehicle which must have at least 2 orders.
81 * @param start order index to start at
82 * @param travelling Are we still in the travelling part of the start order
83 * @param table Fill in arrival and departures including intermediate orders
84 * @param offset Add this value to result and all arrivals and departures
86 static void FillTimetableArrivalDepartureTable(const Vehicle *v, VehicleOrderID start, bool travelling, TimetableArrivalDeparture *table, Ticks offset)
88 assert(table != NULL);
89 assert(v->GetNumOrders() >= 2);
90 assert(start < v->GetNumOrders());
92 Ticks sum = offset;
93 VehicleOrderID i = start;
94 const Order *order = v->GetOrder(i);
96 /* Pre-initialize with unknown time */
97 for (int i = 0; i < v->GetNumOrders(); ++i) {
98 table[i].arrival = table[i].departure = INVALID_TICKS;
101 /* Cyclically loop over all orders until we reach the current one again.
102 * As we may start at the current order, do a post-checking loop */
103 do {
104 /* Automatic orders don't influence the overall timetable;
105 * they just add some untimetabled entries, but the time till
106 * the next non-implicit order can still be known. */
107 if (!order->IsType(OT_IMPLICIT)) {
108 if (travelling || i != start) {
109 if (!CanDetermineTimeTaken(order, true)) return;
110 sum += order->GetTimetabledTravel();
111 table[i].arrival = sum;
114 if (!CanDetermineTimeTaken(order, false)) return;
115 sum += order->GetTimetabledWait();
116 table[i].departure = sum;
119 ++i;
120 order = order->next;
121 if (i >= v->GetNumOrders()) {
122 i = 0;
123 assert(order == NULL);
124 order = v->orders.list->GetFirstOrder();
126 } while (i != start);
128 /* When loading at a scheduled station we still have to treat the
129 * travelling part of the first order. */
130 if (!travelling) {
131 if (!CanDetermineTimeTaken(order, true)) return;
132 sum += order->GetTimetabledTravel();
133 table[i].arrival = sum;
139 * Callback for when a time has been chosen to start the time table
140 * @param window the window related to the setting of the date
141 * @param date the actually chosen date
143 static void ChangeTimetableStartCallback(const Window *w, Date date)
145 DoCommandP(0, w->window_number, date, CMD_SET_TIMETABLE_START);
149 struct TimetableWindow : Window {
150 int sel_index;
151 const Vehicle *vehicle; ///< Vehicle monitored by the window.
152 bool show_expected; ///< Whether we show expected arrival or scheduled
153 uint deparr_time_width; ///< The width of the departure/arrival time
154 uint deparr_abbr_width; ///< The width of the departure/arrival abbreviation
155 Scrollbar *vscroll;
156 bool query_is_speed_query; ///< The currently open query window is a speed query and not a time query.
158 TimetableWindow (const WindowDesc *desc, WindowNumber window_number) :
159 Window(desc),
160 sel_index(-1),
161 vehicle(Vehicle::Get(window_number)),
162 show_expected(true),
163 deparr_time_width(0),
164 deparr_abbr_width(0),
165 vscroll(NULL),
166 query_is_speed_query(false)
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 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
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 = max(GetStringBoundingBox(STR_TIMETABLE_ARRIVAL_ABBREVIATION).width, GetStringBoundingBox(STR_TIMETABLE_DEPARTURE_ABBREVIATION).width);
201 size->width = WD_FRAMERECT_LEFT + this->deparr_abbr_width + 10 + this->deparr_time_width + WD_FRAMERECT_RIGHT;
202 /* FALL THROUGH */
203 case WID_VT_ARRIVAL_DEPARTURE_SELECTION:
204 case WID_VT_TIMETABLE_PANEL:
205 resize->height = FONT_HEIGHT_NORMAL;
206 size->height = WD_FRAMERECT_TOP + 8 * resize->height + WD_FRAMERECT_BOTTOM;
207 break;
209 case WID_VT_SUMMARY_PANEL:
210 size->height = WD_FRAMERECT_TOP + 2 * FONT_HEIGHT_NORMAL + WD_FRAMERECT_BOTTOM;
211 break;
215 int GetOrderFromTimetableWndPt(int y, const Vehicle *v)
217 int sel = (y - this->GetWidget<NWidgetBase>(WID_VT_TIMETABLE_PANEL)->pos_y - WD_FRAMERECT_TOP) / FONT_HEIGHT_NORMAL;
219 if ((uint)sel >= this->vscroll->GetCapacity()) return INVALID_ORDER;
221 sel += this->vscroll->GetPosition();
223 return (sel < v->GetNumOrders() * 2 && sel >= 0) ? sel : INVALID_ORDER;
227 * Some data on this window has become invalid.
228 * @param data Information about the changed data.
229 * @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.
231 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
233 switch (data) {
234 case VIWD_AUTOREPLACE:
235 /* Autoreplace replaced the vehicle */
236 this->vehicle = Vehicle::Get(this->window_number);
237 break;
239 case VIWD_REMOVE_ALL_ORDERS:
240 /* Removed / replaced all orders (after deleting / sharing) */
241 if (this->sel_index == -1) break;
243 this->DeleteChildWindows();
244 this->sel_index = -1;
245 break;
247 case VIWD_MODIFY_ORDERS:
248 if (!gui_scope) break;
249 this->UpdateSelectionStates();
250 this->ReInit();
251 break;
253 default: {
254 if (gui_scope) break; // only do this once; from command scope
256 /* Moving an order. If one of these is INVALID_VEH_ORDER_ID, then
257 * the order is being created / removed */
258 if (this->sel_index == -1) break;
260 VehicleOrderID from = GB(data, 0, 8);
261 VehicleOrderID to = GB(data, 8, 8);
263 if (from == to) break; // no need to change anything
265 /* if from == INVALID_VEH_ORDER_ID, one order was added; if to == INVALID_VEH_ORDER_ID, one order was removed */
266 uint old_num_orders = this->vehicle->GetNumOrders() - (uint)(from == INVALID_VEH_ORDER_ID) + (uint)(to == INVALID_VEH_ORDER_ID);
268 VehicleOrderID selected_order = (this->sel_index + 1) / 2;
269 if (selected_order == old_num_orders) selected_order = 0; // when last travel time is selected, it belongs to order 0
271 bool travel = HasBit(this->sel_index, 0);
273 if (from != selected_order) {
274 /* Moving from preceding order? */
275 selected_order -= (int)(from <= selected_order);
276 /* Moving to preceding order? */
277 selected_order += (int)(to <= selected_order);
278 } else {
279 /* Now we are modifying the selected order */
280 if (to == INVALID_VEH_ORDER_ID) {
281 /* Deleting selected order */
282 this->DeleteChildWindows();
283 this->sel_index = -1;
284 break;
285 } else {
286 /* Moving selected order */
287 selected_order = to;
291 /* recompute new sel_index */
292 this->sel_index = 2 * selected_order - (int)travel;
293 /* travel time of first order needs special handling */
294 if (this->sel_index == -1) this->sel_index = this->vehicle->GetNumOrders() * 2 - 1;
295 break;
301 virtual void OnPaint()
303 const Vehicle *v = this->vehicle;
304 int selected = this->sel_index;
306 this->vscroll->SetCount(v->GetNumOrders() * 2);
308 if (v->owner == _local_company) {
309 bool disable = true;
310 if (selected != -1) {
311 const Order *order = v->GetOrder(((selected + 1) / 2) % v->GetNumOrders());
312 if (selected % 2 == 1) {
313 disable = order != NULL && (order->IsType(OT_CONDITIONAL) || order->IsType(OT_IMPLICIT));
314 } else {
315 disable = order == NULL || ((!order->IsType(OT_GOTO_STATION) || (order->GetNonStopType() & ONSF_NO_STOP_AT_DESTINATION_STATION)) && !order->IsType(OT_CONDITIONAL));
318 bool disable_speed = disable || selected % 2 != 1 || v->type == VEH_AIRCRAFT;
320 this->SetWidgetDisabledState(WID_VT_CHANGE_TIME, disable);
321 this->SetWidgetDisabledState(WID_VT_CLEAR_TIME, disable);
322 this->SetWidgetDisabledState(WID_VT_CHANGE_SPEED, disable_speed);
323 this->SetWidgetDisabledState(WID_VT_CLEAR_SPEED, disable_speed);
324 this->SetWidgetDisabledState(WID_VT_SHARED_ORDER_LIST, !v->IsOrderListShared());
326 this->SetWidgetDisabledState(WID_VT_START_DATE, v->orders.list == NULL);
327 this->SetWidgetDisabledState(WID_VT_RESET_LATENESS, v->orders.list == NULL);
328 this->SetWidgetDisabledState(WID_VT_AUTOFILL, v->orders.list == NULL);
329 } else {
330 this->DisableWidget(WID_VT_START_DATE);
331 this->DisableWidget(WID_VT_CHANGE_TIME);
332 this->DisableWidget(WID_VT_CLEAR_TIME);
333 this->DisableWidget(WID_VT_CHANGE_SPEED);
334 this->DisableWidget(WID_VT_CLEAR_SPEED);
335 this->DisableWidget(WID_VT_RESET_LATENESS);
336 this->DisableWidget(WID_VT_AUTOFILL);
337 this->DisableWidget(WID_VT_SHARED_ORDER_LIST);
340 this->SetWidgetLoweredState(WID_VT_AUTOFILL, HasBit(v->vehicle_flags, VF_AUTOFILL_TIMETABLE));
342 this->DrawWidgets();
345 virtual void SetStringParameters(int widget) const
347 switch (widget) {
348 case WID_VT_CAPTION: SetDParam(0, this->vehicle->index); break;
349 case WID_VT_EXPECTED: SetDParam(0, this->show_expected ? STR_TIMETABLE_EXPECTED : STR_TIMETABLE_SCHEDULED); break;
353 virtual void DrawWidget(const Rect &r, int widget) const
355 const Vehicle *v = this->vehicle;
356 int selected = this->sel_index;
358 switch (widget) {
359 case WID_VT_TIMETABLE_PANEL: {
360 int y = r.top + WD_FRAMERECT_TOP;
361 int i = this->vscroll->GetPosition();
362 VehicleOrderID order_id = (i + 1) / 2;
363 bool final_order = false;
365 bool rtl = _current_text_dir == TD_RTL;
366 SetDParamMaxValue(0, v->GetNumOrders(), 2);
367 int index_column_width = GetStringBoundingBox(STR_ORDER_INDEX).width + 2 * GetSpriteSize(rtl ? SPR_ARROW_RIGHT : SPR_ARROW_LEFT).width + 3;
368 int middle = rtl ? r.right - WD_FRAMERECT_RIGHT - index_column_width : r.left + WD_FRAMERECT_LEFT + index_column_width;
370 const Order *order = v->GetOrder(order_id);
371 while (order != NULL) {
372 /* Don't draw anything if it extends past the end of the window. */
373 if (!this->vscroll->IsVisible(i)) break;
375 if (i % 2 == 0) {
376 DrawOrderString(v, order, order_id, y, i == selected, true, r.left + WD_FRAMERECT_LEFT, middle, r.right - WD_FRAMERECT_RIGHT);
378 order_id++;
380 if (order_id >= v->GetNumOrders()) {
381 order = v->GetOrder(0);
382 final_order = true;
383 } else {
384 order = order->next;
386 } else {
387 StringID string;
388 TextColour colour = (i == selected) ? TC_WHITE : TC_BLACK;
389 if (order->IsType(OT_CONDITIONAL)) {
390 string = STR_TIMETABLE_NO_TRAVEL;
391 } else if (order->IsType(OT_IMPLICIT)) {
392 string = STR_TIMETABLE_NOT_TIMETABLEABLE;
393 colour = ((i == selected) ? TC_SILVER : TC_GREY) | TC_NO_SHADE;
394 } else if (!order->IsTravelTimetabled()) {
395 if (order->GetTravelTime() > 0) {
396 SetTimetableParams(0, 1, order->GetTravelTime());
397 string = order->GetMaxSpeed() != UINT16_MAX ?
398 STR_TIMETABLE_TRAVEL_FOR_SPEED_ESTIMATED :
399 STR_TIMETABLE_TRAVEL_FOR_ESTIMATED;
400 } else {
401 string = order->GetMaxSpeed() != UINT16_MAX ?
402 STR_TIMETABLE_TRAVEL_NOT_TIMETABLED_SPEED :
403 STR_TIMETABLE_TRAVEL_NOT_TIMETABLED;
405 } else {
406 SetTimetableParams(0, 1, order->GetTimetabledTravel());
407 string = order->GetMaxSpeed() != UINT16_MAX ?
408 STR_TIMETABLE_TRAVEL_FOR_SPEED : STR_TIMETABLE_TRAVEL_FOR;
410 SetDParam(2, order->GetMaxSpeed());
412 DrawString(rtl ? r.left + WD_FRAMERECT_LEFT : middle, rtl ? middle : r.right - WD_FRAMERECT_LEFT, y, string, colour);
414 if (final_order) break;
417 i++;
418 y += FONT_HEIGHT_NORMAL;
420 break;
423 case WID_VT_ARRIVAL_DEPARTURE_PANEL: {
424 /* Arrival and departure times are handled in an all-or-nothing approach,
425 * i.e. are only shown if we can calculate all times.
426 * Excluding order lists with only one order makes some things easier.
428 Ticks total_time = v->orders.list != NULL ? v->orders.list->GetTimetableDurationIncomplete() : 0;
429 if (total_time <= 0 || v->GetNumOrders() <= 1 || !HasBit(v->vehicle_flags, VF_TIMETABLE_STARTED)) break;
431 TimetableArrivalDeparture *arr_dep = AllocaM(TimetableArrivalDeparture, v->GetNumOrders());
432 const VehicleOrderID cur_order = v->cur_real_order_index % v->GetNumOrders();
434 VehicleOrderID earlyID = BuildArrivalDepartureList(v, arr_dep) ? cur_order : (VehicleOrderID)INVALID_VEH_ORDER_ID;
436 int y = r.top + WD_FRAMERECT_TOP;
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 int abbr_left = rtl ? r.right - WD_FRAMERECT_RIGHT - this->deparr_abbr_width : r.left + WD_FRAMERECT_LEFT;
443 int abbr_right = rtl ? r.right - WD_FRAMERECT_RIGHT : r.left + WD_FRAMERECT_LEFT + this->deparr_abbr_width;
444 int time_left = rtl ? r.left + WD_FRAMERECT_LEFT : r.right - WD_FRAMERECT_RIGHT - this->deparr_time_width;
445 int time_right = rtl ? r.left + WD_FRAMERECT_LEFT + this->deparr_time_width : r.right - WD_FRAMERECT_RIGHT;
447 for (int i = this->vscroll->GetPosition(); i / 2 < v->GetNumOrders(); ++i) { // note: i is also incremented in the loop
448 /* Don't draw anything if it extends past the end of the window. */
449 if (!this->vscroll->IsVisible(i)) break;
451 if (i % 2 == 0) {
452 if (arr_dep[i / 2].arrival != INVALID_TICKS) {
453 DrawString(abbr_left, abbr_right, y, STR_TIMETABLE_ARRIVAL_ABBREVIATION, i == selected ? TC_WHITE : TC_BLACK);
454 if (this->show_expected && i / 2 == earlyID) {
455 SetDParam(0, _date + arr_dep[i / 2].arrival / DAY_TICKS);
456 DrawString(time_left, time_right, y, STR_JUST_DATE_TINY, TC_GREEN);
457 } else {
458 SetDParam(0, _date + (arr_dep[i / 2].arrival + offset) / DAY_TICKS);
459 DrawString(time_left, time_right, y, STR_JUST_DATE_TINY,
460 show_late ? TC_RED : i == selected ? TC_WHITE : TC_BLACK);
463 } else {
464 if (arr_dep[i / 2].departure != INVALID_TICKS) {
465 DrawString(abbr_left, abbr_right, y, STR_TIMETABLE_DEPARTURE_ABBREVIATION, i == selected ? TC_WHITE : TC_BLACK);
466 SetDParam(0, _date + (arr_dep[i/2].departure + offset) / DAY_TICKS);
467 DrawString(time_left, time_right, y, STR_JUST_DATE_TINY,
468 show_late ? TC_RED : i == selected ? TC_WHITE : TC_BLACK);
471 y += FONT_HEIGHT_NORMAL;
473 break;
476 case WID_VT_SUMMARY_PANEL: {
477 int y = r.top + WD_FRAMERECT_TOP;
479 Ticks total_time = v->orders.list != NULL ? v->orders.list->GetTimetableDurationIncomplete() : 0;
480 if (total_time != 0) {
481 SetTimetableParams(0, 1, total_time);
482 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);
484 y += FONT_HEIGHT_NORMAL;
486 if (v->timetable_start != 0) {
487 /* We are running towards the first station so we can start the
488 * timetable at the given time. */
489 SetDParam(0, STR_JUST_DATE_TINY);
490 SetDParam(1, v->timetable_start);
491 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_TIMETABLE_STATUS_START_AT);
492 } else if (!HasBit(v->vehicle_flags, VF_TIMETABLE_STARTED)) {
493 /* We aren't running on a timetable yet, so how can we be "on time"
494 * when we aren't even "on service"/"on duty"? */
495 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_TIMETABLE_STATUS_NOT_STARTED);
496 } else if (v->lateness_counter == 0 || (!_settings_client.gui.timetable_in_ticks && v->lateness_counter / DAY_TICKS == 0)) {
497 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_TIMETABLE_STATUS_ON_TIME);
498 } else {
499 SetTimetableParams(0, 1, abs(v->lateness_counter));
500 DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, v->lateness_counter < 0 ? STR_TIMETABLE_STATUS_EARLY : STR_TIMETABLE_STATUS_LATE);
502 break;
507 static inline uint32 PackTimetableArgs(const Vehicle *v, uint selected, bool speed)
509 uint order_number = (selected + 1) / 2;
510 ModifyTimetableFlags mtf = (selected % 2 == 1) ? (speed ? MTF_TRAVEL_SPEED : MTF_TRAVEL_TIME) : MTF_WAIT_TIME;
512 if (order_number >= v->GetNumOrders()) order_number = 0;
514 return v->index | (order_number << 20) | (mtf << 28);
517 virtual void OnClick(Point pt, int widget, int click_count)
519 const Vehicle *v = this->vehicle;
521 switch (widget) {
522 case WID_VT_ORDER_VIEW: // Order view button
523 ShowOrdersWindow(v);
524 break;
526 case WID_VT_TIMETABLE_PANEL: { // Main panel.
527 int selected = GetOrderFromTimetableWndPt(pt.y, v);
529 this->DeleteChildWindows();
530 this->sel_index = (selected == INVALID_ORDER || selected == this->sel_index) ? -1 : selected;
531 break;
534 case WID_VT_START_DATE: // Change the date that the timetable starts.
535 ShowSetDateWindow(this, v->index | (v->orders.list->IsCompleteTimetable() && _ctrl_pressed ? 1U << 20 : 0), _date, _cur_year, _cur_year + 15, ChangeTimetableStartCallback);
536 break;
538 case WID_VT_CHANGE_TIME: { // "Wait For" button.
539 int selected = this->sel_index;
540 VehicleOrderID real = (selected + 1) / 2;
542 if (real >= v->GetNumOrders()) real = 0;
544 const Order *order = v->GetOrder(real);
545 StringID current = STR_EMPTY;
547 if (order != NULL) {
548 uint time = (selected % 2 == 1) ? order->GetTravelTime() : order->GetWaitTime();
549 if (!_settings_client.gui.timetable_in_ticks) time /= DAY_TICKS;
551 if (time != 0) {
552 SetDParam(0, time);
553 current = STR_JUST_INT;
557 this->query_is_speed_query = false;
558 ShowQueryString(current, STR_TIMETABLE_CHANGE_TIME, 31, this, CS_NUMERAL, QSF_ACCEPT_UNCHANGED);
559 break;
562 case WID_VT_CHANGE_SPEED: { // Change max speed button.
563 int selected = this->sel_index;
564 VehicleOrderID real = (selected + 1) / 2;
566 if (real >= v->GetNumOrders()) real = 0;
568 StringID current = STR_EMPTY;
569 const Order *order = v->GetOrder(real);
570 if (order != NULL) {
571 if (order->GetMaxSpeed() != UINT16_MAX) {
572 SetDParam(0, ConvertKmhishSpeedToDisplaySpeed(order->GetMaxSpeed()));
573 current = STR_JUST_INT;
577 this->query_is_speed_query = true;
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 uint32 p1 = PackTimetableArgs(v, this->sel_index, false);
584 DoCommandP(0, p1, 0, CMD_CHANGE_TIMETABLE);
585 break;
588 case WID_VT_CLEAR_SPEED: { // Clear max speed button.
589 uint32 p1 = PackTimetableArgs(v, this->sel_index, true);
590 DoCommandP(0, p1, UINT16_MAX, CMD_CHANGE_TIMETABLE);
591 break;
594 case WID_VT_RESET_LATENESS: // Reset the vehicle's late counter.
595 DoCommandP(0, v->index, 0, CMD_SET_VEHICLE_ON_TIME);
596 break;
598 case WID_VT_AUTOFILL: { // Autofill the timetable.
599 uint32 p2 = 0;
600 if (!HasBit(v->vehicle_flags, VF_AUTOFILL_TIMETABLE)) SetBit(p2, 0);
601 if (_ctrl_pressed) SetBit(p2, 1);
602 DoCommandP(0, v->index, p2, CMD_AUTOFILL_TIMETABLE);
603 break;
606 case WID_VT_EXPECTED:
607 this->show_expected = !this->show_expected;
608 break;
610 case WID_VT_SHARED_ORDER_LIST:
611 ShowVehicleListWindow(v);
612 break;
615 this->SetDirty();
618 virtual void OnQueryTextFinished(char *str)
620 if (str == NULL) return;
622 const Vehicle *v = this->vehicle;
624 uint32 p1 = PackTimetableArgs(v, this->sel_index, this->query_is_speed_query);
626 uint64 val = StrEmpty(str) ? 0 : strtoul(str, NULL, 10);
627 if (this->query_is_speed_query) {
628 val = ConvertDisplaySpeedToKmhishSpeed(val);
629 } else {
630 if (!_settings_client.gui.timetable_in_ticks) val *= DAY_TICKS;
633 uint32 p2 = minu(val, UINT16_MAX);
635 DoCommandP(0, p1, p2, CMD_CHANGE_TIMETABLE);
638 virtual void OnResize()
640 /* Update the scroll bar */
641 this->vscroll->SetCapacityFromWidget(this, WID_VT_TIMETABLE_PANEL, WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM);
645 * Update the selection state of the arrival/departure data
647 void UpdateSelectionStates()
649 this->GetWidget<NWidgetStacked>(WID_VT_ARRIVAL_DEPARTURE_SELECTION)->SetDisplayedPlane(_settings_client.gui.timetable_arrival_departure ? 0 : SZSP_NONE);
650 this->GetWidget<NWidgetStacked>(WID_VT_EXPECTED_SELECTION)->SetDisplayedPlane(_settings_client.gui.timetable_arrival_departure ? 0 : 1);
654 static const NWidgetPart _nested_timetable_widgets[] = {
655 NWidget(NWID_HORIZONTAL),
656 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
657 NWidget(WWT_CAPTION, COLOUR_GREY, WID_VT_CAPTION), SetDataTip(STR_TIMETABLE_TITLE, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
658 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_VT_ORDER_VIEW), SetMinimalSize(61, 14), SetDataTip( STR_TIMETABLE_ORDER_VIEW, STR_TIMETABLE_ORDER_VIEW_TOOLTIP),
659 NWidget(WWT_SHADEBOX, COLOUR_GREY),
660 NWidget(WWT_DEFSIZEBOX, COLOUR_GREY),
661 NWidget(WWT_STICKYBOX, COLOUR_GREY),
662 EndContainer(),
663 NWidget(NWID_HORIZONTAL),
664 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(),
665 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_VT_ARRIVAL_DEPARTURE_SELECTION),
666 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(),
667 EndContainer(),
668 NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_VT_SCROLLBAR),
669 EndContainer(),
670 NWidget(WWT_PANEL, COLOUR_GREY, WID_VT_SUMMARY_PANEL), SetMinimalSize(400, 22), SetResize(1, 0), EndContainer(),
671 NWidget(NWID_HORIZONTAL),
672 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
673 NWidget(NWID_VERTICAL, NC_EQUALSIZE),
674 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),
675 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),
676 EndContainer(),
677 NWidget(NWID_VERTICAL, NC_EQUALSIZE),
678 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),
679 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),
680 EndContainer(),
681 NWidget(NWID_VERTICAL, NC_EQUALSIZE),
682 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),
683 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),
684 EndContainer(),
685 NWidget(NWID_VERTICAL, NC_EQUALSIZE),
686 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_VT_AUTOFILL), SetResize(1, 0), SetFill(1, 1), SetDataTip(STR_TIMETABLE_AUTOFILL, STR_TIMETABLE_AUTOFILL_TOOLTIP),
687 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_VT_EXPECTED_SELECTION),
688 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_VT_EXPECTED), SetResize(1, 0), SetFill(1, 1), SetDataTip(STR_BLACK_STRING, STR_TIMETABLE_EXPECTED_TOOLTIP),
689 NWidget(WWT_PANEL, COLOUR_GREY), SetResize(1, 0), SetFill(1, 1), EndContainer(),
690 EndContainer(),
691 EndContainer(),
692 EndContainer(),
693 NWidget(NWID_VERTICAL, NC_EQUALSIZE),
694 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),
695 NWidget(WWT_RESIZEBOX, COLOUR_GREY), SetFill(0, 1),
696 EndContainer(),
697 EndContainer(),
700 static WindowDesc::Prefs _timetable_prefs ("view_vehicle_timetable");
702 static const WindowDesc _timetable_desc(
703 WDP_AUTO, 400, 130,
704 WC_VEHICLE_TIMETABLE, WC_VEHICLE_VIEW,
705 WDF_CONSTRUCTION,
706 _nested_timetable_widgets, lengthof(_nested_timetable_widgets),
707 &_timetable_prefs
711 * Show the timetable for a given vehicle.
712 * @param v The vehicle to show the timetable for.
714 void ShowTimetableWindow(const Vehicle *v)
716 DeleteWindowById(WC_VEHICLE_DETAILS, v->index, false);
717 DeleteWindowById(WC_VEHICLE_ORDERS, v->index, false);
718 AllocateWindowDescFront<TimetableWindow>(&_timetable_desc, v->index);