# timetable_gui.cpp

Go to the documentation of this file.

    00001 /* $Id$ */
    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 "command_func.h"
    00014 #include "gui.h"
    00015 #include "window_gui.h"
    00016 #include "window_func.h"
    00017 #include "textbuf_gui.h"
    00018 #include "strings_func.h"
    00019 #include "vehicle_base.h"
    00020 #include "string_func.h"
    00021 #include "gfx_func.h"
    00022 #include "company_func.h"
    00023 #include "date_func.h"
    00024 #include "date_gui.h"
    00025 #include "vehicle_gui.h"
    00026 #include "settings_type.h"
    00027
    00028 #include "widgets/timetable_widget.h"
    00029
    00030 #include "table/sprites.h"
    00031 #include "table/strings.h"
    00032
    00034 struct TimetableArrivalDeparture {
    00035   Ticks arrival;
    00036   Ticks departure;
    00037 };
    00038
    00045 void SetTimetableParams(int param1, int param2, Ticks ticks)
    00046 {
    00047   if (_settings_client.gui.timetable_in_ticks) {
    00048     SetDParam(param1, STR_TIMETABLE_TICKS);
    00049     SetDParam(param2, ticks);
    00050   } else {
    00051     SetDParam(param1, STR_TIMETABLE_DAYS);
    00052     SetDParam(param2, ticks / DAY_TICKS);
    00053   }
    00054 }
    00055
    00062 static void SetArrivalDepartParams(int param1, int param2, Ticks ticks)
    00063 {
    00064   SetDParam(param1, STR_JUST_DATE_TINY);
    00065   SetDParam(param2, _date + (ticks / DAY_TICKS));
    00066 }
    00067
    00074 static bool CanDetermineTimeTaken(const Order *order, bool travelling)
    00075 {
    00076   /* Current order is conditional */
    00077   if (order->IsType(OT_CONDITIONAL) || order->IsType(OT_IMPLICIT)) return false;
    00078   /* No travel time and we have not already finished travelling */
    00079   if (travelling && order->travel_time == 0) return false;
    00080   /* No wait time but we are loading at this timetabled station */
    00081   if (!travelling && order->wait_time == 0 && order->IsType(OT_GOTO_STATION) && !(order->GetNonStopType() & ONSF_NO_STOP_AT_DESTINATION_STATION)) return false;
    00082
    00083   return true;
    00084 }
    00085
    00086
    00095 static void FillTimetableArrivalDepartureTable(const Vehicle *v, VehicleOrderID start, bool travelling, TimetableArrivalDeparture *table, Ticks offset)
    00096 {
    00097   assert(table != NULL);
    00098   assert(v->GetNumOrders() >= 2);
    00099   assert(start < v->GetNumOrders());
    00100
    00101   Ticks sum = offset;
    00102   VehicleOrderID i = start;
    00103   const Order *order = v->GetOrder(i);
    00104
    00105   /* Pre-initialize with unknown time */
    00106   for (int i = 0; i < v->GetNumOrders(); ++i) {
    00107     table[i].arrival = table[i].departure = INVALID_TICKS;
    00108   }
    00109
    00110   /* Cyclically loop over all orders until we reach the current one again.
    00111    * As we may start at the current order, do a post-checking loop */
    00112   do {
    00113     /* Automatic orders don't influence the overall timetable;
    00114      * they just add some untimetabled entries, but the time till
    00115      * the next non-implicit order can still be known. */
    00116     if (!order->IsType(OT_IMPLICIT)) {
    00117       if (travelling || i != start) {
    00118         if (!CanDetermineTimeTaken(order, true)) return;
    00119         sum += order->travel_time;
    00120         table[i].arrival = sum;
    00121       }
    00122
    00123       if (!CanDetermineTimeTaken(order, false)) return;
    00124       sum += order->wait_time;
    00125       table[i].departure = sum;
    00126     }
    00127
    00128     ++i;
    00129     order = order->next;
    00130     if (i >= v->GetNumOrders()) {
    00131       i = 0;
    00132       assert(order == NULL);
    00133       order = v->orders.list->GetFirstOrder();
    00134     }
    00135   } while (i != start);
    00136
    00137   /* When loading at a scheduled station we still have to treat the
    00138    * travelling part of the first order. */
    00139   if (!travelling) {
    00140     if (!CanDetermineTimeTaken(order, true)) return;
    00141     sum += order->travel_time;
    00142     table[i].arrival = sum;
    00143   }
    00144 }
    00145
    00146
    00152 static void ChangeTimetableStartCallback(const Window *w, Date date)
    00153 {
    00154   DoCommandP(0, w->window_number, date, CMD_SET_TIMETABLE_START | CMD_MSG(STR_ERROR_CAN_T_TIMETABLE_VEHICLE));
    00155 }
    00156
    00157
    00158 struct TimetableWindow : Window {
    00159   int sel_index;
    00160   const Vehicle *vehicle;
    00161   bool show_expected;
    00162   uint deparr_time_width;
    00163   uint deparr_abbr_width;
    00164   Scrollbar *vscroll;
    00165
    00166   TimetableWindow(const WindowDesc *desc, WindowNumber window_number) :
    00167       Window(),
    00168       sel_index(-1),
    00169       vehicle(Vehicle::Get(window_number)),
    00170       show_expected(true)
    00171   {
    00172     this->CreateNestedTree(desc);
    00173     this->vscroll = this->GetScrollbar(WID_VT_SCROLLBAR);
    00174     this->UpdateSelectionStates();
    00175     this->FinishInitNested(desc, window_number);
    00176
    00177     this->owner = this->vehicle->owner;
    00178   }
    00179
    00186   static bool BuildArrivalDepartureList(const Vehicle *v, TimetableArrivalDeparture *table)
    00187   {
    00188     assert(HasBit(v->vehicle_flags, VF_TIMETABLE_STARTED));
    00189
    00190     bool travelling = (!v->current_order.IsType(OT_LOADING) || v->current_order.GetNonStopType() == ONSF_STOP_EVERYWHERE);
    00191     Ticks start_time = _date_fract - v->current_order_time;
    00192
    00193     FillTimetableArrivalDepartureTable(v, v->cur_real_order_index % v->GetNumOrders(), travelling, table, start_time);
    00194
    00195     return (travelling && v->lateness_counter < 0);
    00196   }
    00197
    00198   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
    00199   {
    00200     switch (widget) {
    00201       case WID_VT_ARRIVAL_DEPARTURE_PANEL:
    00202         SetDParam(0, MAX_YEAR * DAYS_IN_YEAR);
    00203         this->deparr_time_width = GetStringBoundingBox(STR_JUST_DATE_TINY).width;
    00204         this->deparr_abbr_width = max(GetStringBoundingBox(STR_TIMETABLE_ARRIVAL_ABBREVIATION).width, GetStringBoundingBox(STR_TIMETABLE_DEPARTURE_ABBREVIATION).width);
    00205         size->width = WD_FRAMERECT_LEFT + this->deparr_abbr_width + 10 + this->deparr_time_width + WD_FRAMERECT_RIGHT;
    00206         /* FALL THROUGH */
    00207       case WID_VT_ARRIVAL_DEPARTURE_SELECTION:
    00208       case WID_VT_TIMETABLE_PANEL:
    00209         resize->height = FONT_HEIGHT_NORMAL;
    00210         size->height = WD_FRAMERECT_TOP + 8 * resize->height + WD_FRAMERECT_BOTTOM;
    00211         break;
    00212
    00213       case WID_VT_SUMMARY_PANEL:
    00214         size->height = WD_FRAMERECT_TOP + 2 * FONT_HEIGHT_NORMAL + WD_FRAMERECT_BOTTOM;
    00215         break;
    00216     }
    00217   }
    00218
    00219   int GetOrderFromTimetableWndPt(int y, const Vehicle *v)
    00220   {
    00221     int sel = (y - this->GetWidget<NWidgetBase>(WID_VT_TIMETABLE_PANEL)->pos_y - WD_FRAMERECT_TOP) / FONT_HEIGHT_NORMAL;
    00222
    00223     if ((uint)sel >= this->vscroll->GetCapacity()) return INVALID_ORDER;
    00224
    00225     sel += this->vscroll->GetPosition();
    00226
    00227     return (sel < v->GetNumOrders() * 2 && sel >= 0) ? sel : INVALID_ORDER;
    00228   }
    00229
    00235   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
    00236   {
    00237     switch (data) {
    00238       case -666:
    00239         /* Autoreplace replaced the vehicle */
    00240         this->vehicle = Vehicle::Get(this->window_number);
    00241         break;
    00242
    00243       case -1:
    00244         /* Removed / replaced all orders (after deleting / sharing) */
    00245         if (this->sel_index == -1) break;
    00246
    00247         this->DeleteChildWindows();
    00248         this->sel_index = -1;
    00249         break;
    00250
    00251       case -2:
    00252         if (!gui_scope) break;
    00253         this->UpdateSelectionStates();
    00254         this->ReInit();
    00255         break;
    00256
    00257       default: {
    00258         if (gui_scope) break; // only do this once; from command scope
    00259
    00260         /* Moving an order. If one of these is INVALID_VEH_ORDER_ID, then
    00261          * the order is being created / removed */
    00262         if (this->sel_index == -1) break;
    00263
    00264         VehicleOrderID from = GB(data, 0, 8);
    00265         VehicleOrderID to   = GB(data, 8, 8);
    00266
    00267         if (from == to) break; // no need to change anything
    00268
    00269         /* if from == INVALID_VEH_ORDER_ID, one order was added; if to == INVALID_VEH_ORDER_ID, one order was removed */
    00270         uint old_num_orders = this->vehicle->GetNumOrders() - (uint)(from == INVALID_VEH_ORDER_ID) + (uint)(to == INVALID_VEH_ORDER_ID);
    00271
    00272         VehicleOrderID selected_order = (this->sel_index + 1) / 2;
    00273         if (selected_order == old_num_orders) selected_order = 0; // when last travel time is selected, it belongs to order 0
    00274
    00275         bool travel = HasBit(this->sel_index, 0);
    00276
    00277         if (from != selected_order) {
    00278           /* Moving from preceding order? */
    00279           selected_order -= (int)(from <= selected_order);
    00280           /* Moving to   preceding order? */
    00281           selected_order += (int)(to   <= selected_order);
    00282         } else {
    00283           /* Now we are modifying the selected order */
    00284           if (to == INVALID_VEH_ORDER_ID) {
    00285             /* Deleting selected order */
    00286             this->DeleteChildWindows();
    00287             this->sel_index = -1;
    00288             break;
    00289           } else {
    00290             /* Moving selected order */
    00291             selected_order = to;
    00292           }
    00293         }
    00294
    00295         /* recompute new sel_index */
    00296         this->sel_index = 2 * selected_order - (int)travel;
    00297         /* travel time of first order needs special handling */
    00298         if (this->sel_index == -1) this->sel_index = this->vehicle->GetNumOrders() * 2 - 1;
    00299         break;
    00300       }
    00301     }
    00302   }
    00303
    00304
    00305   virtual void OnPaint()
    00306   {
    00307     const Vehicle *v = this->vehicle;
    00308     int selected = this->sel_index;
    00309
    00310     this->vscroll->SetCount(v->GetNumOrders() * 2);
    00311
    00312     if (v->owner == _local_company) {
    00313       bool disable = true;
    00314       if (selected != -1) {
    00315         const Order *order = v->GetOrder(((selected + 1) / 2) % v->GetNumOrders());
    00316         if (selected % 2 == 1) {
    00317           disable = order != NULL && (order->IsType(OT_CONDITIONAL) || order->IsType(OT_IMPLICIT));
    00318         } else {
    00319           disable = order == NULL || ((!order->IsType(OT_GOTO_STATION) || (order->GetNonStopType() & ONSF_NO_STOP_AT_DESTINATION_STATION)) && !order->IsType(OT_CONDITIONAL));
    00320         }
    00321       }
    00322
    00323       this->SetWidgetDisabledState(WID_VT_CHANGE_TIME, disable);
    00324       this->SetWidgetDisabledState(WID_VT_CLEAR_TIME, disable);
    00325       this->SetWidgetDisabledState(WID_VT_SHARED_ORDER_LIST, !v->IsOrderListShared());
    00326
    00327       this->EnableWidget(WID_VT_START_DATE);
    00328       this->EnableWidget(WID_VT_RESET_LATENESS);
    00329       this->EnableWidget(WID_VT_AUTOFILL);
    00330     } else {
    00331       this->DisableWidget(WID_VT_START_DATE);
    00332       this->DisableWidget(WID_VT_CHANGE_TIME);
    00333       this->DisableWidget(WID_VT_CLEAR_TIME);
    00334       this->DisableWidget(WID_VT_RESET_LATENESS);
    00335       this->DisableWidget(WID_VT_AUTOFILL);
    00336       this->DisableWidget(WID_VT_SHARED_ORDER_LIST);
    00337     }
    00338
    00339     this->SetWidgetLoweredState(WID_VT_AUTOFILL, HasBit(v->vehicle_flags, VF_AUTOFILL_TIMETABLE));
    00340
    00341     this->DrawWidgets();
    00342   }
    00343
    00344   virtual void SetStringParameters(int widget) const
    00345   {
    00346     switch (widget) {
    00347       case WID_VT_CAPTION: SetDParam(0, this->vehicle->index); break;
    00348       case WID_VT_EXPECTED: SetDParam(0, this->show_expected ? STR_TIMETABLE_EXPECTED : STR_TIMETABLE_SCHEDULED); break;
    00349     }
    00350   }
    00351
    00352   virtual void DrawWidget(const Rect &r, int widget) const
    00353   {
    00354     const Vehicle *v = this->vehicle;
    00355     int selected = this->sel_index;
    00356
    00357     switch (widget) {
    00358       case WID_VT_TIMETABLE_PANEL: {
    00359         int y = r.top + WD_FRAMERECT_TOP;
    00360         int i = this->vscroll->GetPosition();
    00361         VehicleOrderID order_id = (i + 1) / 2;
    00362         bool final_order = false;
    00363
    00364         bool rtl = _current_text_dir == TD_RTL;
    00365         SetDParam(0, 99);
    00366         int index_column_width = GetStringBoundingBox(STR_ORDER_INDEX).width + GetSpriteSize(rtl ? SPR_ARROW_RIGHT : SPR_ARROW_LEFT).width + 3;
    00367         int middle = rtl ? r.right - WD_FRAMERECT_RIGHT - index_column_width : r.left + WD_FRAMERECT_LEFT + index_column_width;
    00368
    00369         const Order *order = v->GetOrder(order_id);
    00370         while (order != NULL) {
    00371           /* Don't draw anything if it extends past the end of the window. */
    00372           if (!this->vscroll->IsVisible(i)) break;
    00373
    00374           if (i % 2 == 0) {
    00375             DrawOrderString(v, order, order_id, y, i == selected, true, r.left + WD_FRAMERECT_LEFT, middle, r.right - WD_FRAMERECT_RIGHT);
    00376
    00377             order_id++;
    00378
    00379             if (order_id >= v->GetNumOrders()) {
    00380               order = v->GetOrder(0);
    00381               final_order = true;
    00382             } else {
    00383               order = order->next;
    00384             }
    00385           } else {
    00386             StringID string;
    00387             TextColour colour = (i == selected) ? TC_WHITE : TC_BLACK;
    00388             if (order->IsType(OT_CONDITIONAL)) {
    00389               string = STR_TIMETABLE_NO_TRAVEL;
    00390             } else if (order->IsType(OT_IMPLICIT)) {
    00391               string = STR_TIMETABLE_NOT_TIMETABLEABLE;
    00392               colour = ((i == selected) ? TC_SILVER : TC_GREY) | TC_NO_SHADE;
    00393             } else if (order->travel_time == 0) {
    00394               string = STR_TIMETABLE_TRAVEL_NOT_TIMETABLED;
    00395             } else {
    00396               SetTimetableParams(0, 1, order->travel_time);
    00397               string = STR_TIMETABLE_TRAVEL_FOR;
    00398             }
    00399
    00400             DrawString(rtl ? r.left + WD_FRAMERECT_LEFT : middle, rtl ? middle : r.right - WD_FRAMERECT_LEFT, y, string, colour);
    00401
    00402             if (final_order) break;
    00403           }
    00404
    00405           i++;
    00406           y += FONT_HEIGHT_NORMAL;
    00407         }
    00408         break;
    00409       }
    00410
    00411       case WID_VT_ARRIVAL_DEPARTURE_PANEL: {
    00412         /* Arrival and departure times are handled in an all-or-nothing approach,
    00413          * i.e. are only shown if we can calculate all times.
    00414          * Excluding order lists with only one order makes some things easier.
    00415          */
    00416         Ticks total_time = v->orders.list != NULL ? v->orders.list->GetTimetableDurationIncomplete() : 0;
    00417         if (total_time <= 0 || v->GetNumOrders() <= 1 || !HasBit(v->vehicle_flags, VF_TIMETABLE_STARTED)) break;
    00418
    00419         TimetableArrivalDeparture *arr_dep = AllocaM(TimetableArrivalDeparture, v->GetNumOrders());
    00420         const VehicleOrderID cur_order = v->cur_real_order_index % v->GetNumOrders();
    00421
    00422         VehicleOrderID earlyID = BuildArrivalDepartureList(v, arr_dep) ? cur_order : (VehicleOrderID)INVALID_VEH_ORDER_ID;
    00423
    00424         int y = r.top + WD_FRAMERECT_TOP;
    00425
    00426         bool show_late = this->show_expected && v->lateness_counter > DAY_TICKS;
    00427         Ticks offset = show_late ? 0 : -v->lateness_counter;
    00428
    00429         bool rtl = _current_text_dir == TD_RTL;
    00430         int abbr_left  = rtl ? r.right - WD_FRAMERECT_RIGHT - this->deparr_abbr_width : r.left + WD_FRAMERECT_LEFT;
    00431         int abbr_right = rtl ? r.right - WD_FRAMERECT_RIGHT : r.left + WD_FRAMERECT_LEFT + this->deparr_abbr_width;
    00432         int time_left  = rtl ? r.left + WD_FRAMERECT_LEFT : r.right - WD_FRAMERECT_RIGHT - this->deparr_time_width;
    00433         int time_right = rtl ? r.left + WD_FRAMERECT_LEFT + this->deparr_time_width : r.right - WD_FRAMERECT_RIGHT;
    00434
    00435         for (int i = this->vscroll->GetPosition(); i / 2 < v->GetNumOrders(); ++i) { // note: i is also incremented in the loop
    00436           /* Don't draw anything if it extends past the end of the window. */
    00437           if (!this->vscroll->IsVisible(i)) break;
    00438
    00439           if (i % 2 == 0) {
    00440             if (arr_dep[i / 2].arrival != INVALID_TICKS) {
    00441               DrawString(abbr_left, abbr_right, y, STR_TIMETABLE_ARRIVAL_ABBREVIATION, i == selected ? TC_WHITE : TC_BLACK);
    00442               if (this->show_expected && i / 2 == earlyID) {
    00443                 SetArrivalDepartParams(0, 1, arr_dep[i / 2].arrival);
    00444                 DrawString(time_left, time_right, y, STR_GREEN_STRING, i == selected ? TC_WHITE : TC_BLACK);
    00445               } else {
    00446                 SetArrivalDepartParams(0, 1, arr_dep[i / 2].arrival + offset);
    00447                 DrawString(time_left, time_right, y, show_late ? STR_RED_STRING : STR_JUST_STRING, i == selected ? TC_WHITE : TC_BLACK);
    00448               }
    00449             }
    00450           } else {
    00451             if (arr_dep[i / 2].departure != INVALID_TICKS) {
    00452               DrawString(abbr_left, abbr_right, y, STR_TIMETABLE_DEPARTURE_ABBREVIATION, i == selected ? TC_WHITE : TC_BLACK);
    00453               SetArrivalDepartParams(0, 1, arr_dep[i/2].departure + offset);
    00454               DrawString(time_left, time_right, y, show_late ? STR_RED_STRING : STR_JUST_STRING, i == selected ? TC_WHITE : TC_BLACK);
    00455             }
    00456           }
    00457           y += FONT_HEIGHT_NORMAL;
    00458         }
    00459         break;
    00460       }
    00461
    00462       case WID_VT_SUMMARY_PANEL: {
    00463         int y = r.top + WD_FRAMERECT_TOP;
    00464
    00465         Ticks total_time = v->orders.list != NULL ? v->orders.list->GetTimetableDurationIncomplete() : 0;
    00466         if (total_time != 0) {
    00467           SetTimetableParams(0, 1, total_time);
    00468           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);
    00469         }
    00470         y += FONT_HEIGHT_NORMAL;
    00471
    00472         if (v->timetable_start != 0) {
    00473           /* We are running towards the first station so we can start the
    00474            * timetable at the given time. */
    00475           SetDParam(0, STR_JUST_DATE_TINY);
    00476           SetDParam(1, v->timetable_start);
    00477           DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_TIMETABLE_STATUS_START_AT);
    00478         } else if (!HasBit(v->vehicle_flags, VF_TIMETABLE_STARTED)) {
    00479           /* We aren't running on a timetable yet, so how can we be "on time"
    00480            * when we aren't even "on service"/"on duty"? */
    00481           DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_TIMETABLE_STATUS_NOT_STARTED);
    00482         } else if (v->lateness_counter == 0 || (!_settings_client.gui.timetable_in_ticks && v->lateness_counter / DAY_TICKS == 0)) {
    00483           DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_TIMETABLE_STATUS_ON_TIME);
    00484         } else {
    00485           SetTimetableParams(0, 1, abs(v->lateness_counter));
    00486           DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, v->lateness_counter < 0 ? STR_TIMETABLE_STATUS_EARLY : STR_TIMETABLE_STATUS_LATE);
    00487         }
    00488         break;
    00489       }
    00490     }
    00491   }
    00492
    00493   static inline uint32 PackTimetableArgs(const Vehicle *v, uint selected)
    00494   {
    00495     uint order_number = (selected + 1) / 2;
    00496     uint is_journey   = (selected % 2 == 1) ? 1 : 0;
    00497
    00498     if (order_number >= v->GetNumOrders()) order_number = 0;
    00499
    00500     return v->index | (order_number << 20) | (is_journey << 28);
    00501   }
    00502
    00503   virtual void OnClick(Point pt, int widget, int click_count)
    00504   {
    00505     const Vehicle *v = this->vehicle;
    00506
    00507     switch (widget) {
    00508       case WID_VT_ORDER_VIEW: // Order view button
    00509         ShowOrdersWindow(v);
    00510         break;
    00511
    00512       case WID_VT_TIMETABLE_PANEL: { // Main panel.
    00513         int selected = GetOrderFromTimetableWndPt(pt.y, v);
    00514
    00515         this->DeleteChildWindows();
    00516         this->sel_index = (selected == INVALID_ORDER || selected == this->sel_index) ? -1 : selected;
    00517         break;
    00518       }
    00519
    00520       case WID_VT_START_DATE: // Change the date that the timetable starts.
    00521         ShowSetDateWindow(this, v->index, _date, _cur_year, _cur_year + 15, ChangeTimetableStartCallback);
    00522         break;
    00523
    00524       case WID_VT_CHANGE_TIME: { // "Wait For" button.
    00525         int selected = this->sel_index;
    00526         VehicleOrderID real = (selected + 1) / 2;
    00527
    00528         if (real >= v->GetNumOrders()) real = 0;
    00529
    00530         const Order *order = v->GetOrder(real);
    00531         StringID current = STR_EMPTY;
    00532
    00533         if (order != NULL) {
    00534           uint time = (selected % 2 == 1) ? order->travel_time : order->wait_time;
    00535           if (!_settings_client.gui.timetable_in_ticks) time /= DAY_TICKS;
    00536
    00537           if (time != 0) {
    00538             SetDParam(0, time);
    00539             current = STR_JUST_INT;
    00540           }
    00541         }
    00542
    00543         ShowQueryString(current, STR_TIMETABLE_CHANGE_TIME, 31, this, CS_NUMERAL, QSF_NONE);
    00544         break;
    00545       }
    00546
    00547       case WID_VT_CLEAR_TIME: { // Clear waiting time button.
    00548         uint32 p1 = PackTimetableArgs(v, this->sel_index);
    00549         DoCommandP(0, p1, 0, CMD_CHANGE_TIMETABLE | CMD_MSG(STR_ERROR_CAN_T_TIMETABLE_VEHICLE));
    00550         break;
    00551       }
    00552
    00553       case WID_VT_RESET_LATENESS: // Reset the vehicle's late counter.
    00554         DoCommandP(0, v->index, 0, CMD_SET_VEHICLE_ON_TIME | CMD_MSG(STR_ERROR_CAN_T_TIMETABLE_VEHICLE));
    00555         break;
    00556
    00557       case WID_VT_AUTOFILL: { // Autofill the timetable.
    00558         uint32 p2 = 0;
    00559         if (!HasBit(v->vehicle_flags, VF_AUTOFILL_TIMETABLE)) SetBit(p2, 0);
    00560         if (_ctrl_pressed) SetBit(p2, 1);
    00561         DoCommandP(0, v->index, p2, CMD_AUTOFILL_TIMETABLE | CMD_MSG(STR_ERROR_CAN_T_TIMETABLE_VEHICLE));
    00562         break;
    00563       }
    00564
    00565       case WID_VT_EXPECTED:
    00566         this->show_expected = !this->show_expected;
    00567         break;
    00568
    00569       case WID_VT_SHARED_ORDER_LIST:
    00570         ShowVehicleListWindow(v);
    00571         break;
    00572     }
    00573
    00574     this->SetDirty();
    00575   }
    00576
    00577   virtual void OnQueryTextFinished(char *str)
    00578   {
    00579     if (str == NULL) return;
    00580
    00581     const Vehicle *v = this->vehicle;
    00582
    00583     uint32 p1 = PackTimetableArgs(v, this->sel_index);
    00584
    00585     uint64 time = StrEmpty(str) ? 0 : strtoul(str, NULL, 10);
    00586     if (!_settings_client.gui.timetable_in_ticks) time *= DAY_TICKS;
    00587
    00588     uint32 p2 = minu(time, UINT16_MAX);
    00589
    00590     DoCommandP(0, p1, p2, CMD_CHANGE_TIMETABLE | CMD_MSG(STR_ERROR_CAN_T_TIMETABLE_VEHICLE));
    00591   }
    00592
    00593   virtual void OnResize()
    00594   {
    00595     /* Update the scroll bar */
    00596     this->vscroll->SetCapacityFromWidget(this, WID_VT_TIMETABLE_PANEL, WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM);
    00597   }
    00598
    00602   void UpdateSelectionStates()
    00603   {
    00604     this->GetWidget<NWidgetStacked>(WID_VT_ARRIVAL_DEPARTURE_SELECTION)->SetDisplayedPlane(_settings_client.gui.timetable_arrival_departure ? 0 : SZSP_NONE);
    00605     this->GetWidget<NWidgetStacked>(WID_VT_EXPECTED_SELECTION)->SetDisplayedPlane(_settings_client.gui.timetable_arrival_departure ? 0 : 1);
    00606   }
    00607 };
    00608
    00609 static const NWidgetPart _nested_timetable_widgets[] = {
    00610   NWidget(NWID_HORIZONTAL),
    00611     NWidget(WWT_CLOSEBOX, COLOUR_GREY),
    00612     NWidget(WWT_CAPTION, COLOUR_GREY, WID_VT_CAPTION), SetDataTip(STR_TIMETABLE_TITLE, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    00613     NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_VT_ORDER_VIEW), SetMinimalSize(61, 14), SetDataTip( STR_TIMETABLE_ORDER_VIEW, STR_TIMETABLE_ORDER_VIEW_TOOLTIP),
    00614     NWidget(WWT_SHADEBOX, COLOUR_GREY),
    00615     NWidget(WWT_STICKYBOX, COLOUR_GREY),
    00616   EndContainer(),
    00617   NWidget(NWID_HORIZONTAL),
    00618     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(),
    00619     NWidget(NWID_SELECTION, INVALID_COLOUR, WID_VT_ARRIVAL_DEPARTURE_SELECTION),
    00620       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(),
    00621     EndContainer(),
    00622     NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_VT_SCROLLBAR),
    00623   EndContainer(),
    00624   NWidget(WWT_PANEL, COLOUR_GREY, WID_VT_SUMMARY_PANEL), SetMinimalSize(400, 22), SetResize(1, 0), EndContainer(),
    00625   NWidget(NWID_HORIZONTAL),
    00626     NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
    00627       NWidget(NWID_VERTICAL, NC_EQUALSIZE),
    00628         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),
    00629         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),
    00630       EndContainer(),
    00631       NWidget(NWID_VERTICAL, NC_EQUALSIZE),
    00632         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),
    00633         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),
    00634       EndContainer(),
    00635       NWidget(NWID_VERTICAL, NC_EQUALSIZE),
    00636         NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_VT_AUTOFILL), SetResize(1, 0), SetFill(1, 1), SetDataTip(STR_TIMETABLE_AUTOFILL, STR_TIMETABLE_AUTOFILL_TOOLTIP),
    00637         NWidget(NWID_SELECTION, INVALID_COLOUR, WID_VT_EXPECTED_SELECTION),
    00638           NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_VT_EXPECTED), SetResize(1, 0), SetFill(1, 1), SetDataTip(STR_BLACK_STRING, STR_TIMETABLE_EXPECTED_TOOLTIP),
    00639           NWidget(WWT_PANEL, COLOUR_GREY), SetResize(1, 0), SetFill(1, 1), EndContainer(),
    00640         EndContainer(),
    00641       EndContainer(),
    00642     EndContainer(),
    00643     NWidget(NWID_VERTICAL, NC_EQUALSIZE),
    00644       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),
    00645       NWidget(WWT_RESIZEBOX, COLOUR_GREY), SetFill(0, 1),
    00646     EndContainer(),
    00647   EndContainer(),
    00648 };
    00649
    00650 static const WindowDesc _timetable_desc(
    00651   WDP_AUTO, 400, 130,
    00652   WC_VEHICLE_TIMETABLE, WC_VEHICLE_VIEW,
    00653   WDF_UNCLICK_BUTTONS | WDF_CONSTRUCTION,
    00654   _nested_timetable_widgets, lengthof(_nested_timetable_widgets)
    00655 );
    00656
    00661 void ShowTimetableWindow(const Vehicle *v)
    00662 {
    00663   DeleteWindowById(WC_VEHICLE_DETAILS, v->index, false);
    00664   DeleteWindowById(WC_VEHICLE_ORDERS, v->index, false);
    00665   AllocateWindowDescFront<TimetableWindow>(&_timetable_desc, v->index);
    00666 }