# timetable_gui.cpp

Go to the documentation of this file.

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