# timetable_gui.cpp

Go to the documentation of this file.

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