# timetable_gui.cpp

Go to the documentation of this file.

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