# 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 "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
    00251   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
    00252   {
    00253     switch (data) {
    00254       case -666:
    00255         /* Autoreplace replaced the vehicle */
    00256         this->vehicle = Vehicle::Get(this->window_number);
    00257         break;
    00258
    00259       case -1:
    00260         /* Removed / replaced all orders (after deleting / sharing) */
    00261         if (this->sel_index == -1) break;
    00262
    00263         this->DeleteChildWindows();
    00264         this->sel_index = -1;
    00265         break;
    00266
    00267       case -2:
    00268         if (!gui_scope) break;
    00269         this->UpdateSelectionStates();
    00270         this->ReInit();
    00271         break;
    00272
    00273       default: {
    00274         if (gui_scope) break; // only do this once; from command scope
    00275
    00276         /* Moving an order. If one of these is INVALID_VEH_ORDER_ID, then
    00277          * the order is being created / removed */
    00278         if (this->sel_index == -1) break;
    00279
    00280         VehicleOrderID from = GB(data, 0, 8);
    00281         VehicleOrderID to   = GB(data, 8, 8);
    00282
    00283         if (from == to) break; // no need to change anything
    00284
    00285         /* if from == INVALID_VEH_ORDER_ID, one order was added; if to == INVALID_VEH_ORDER_ID, one order was removed */
    00286         uint old_num_orders = this->vehicle->GetNumOrders() - (uint)(from == INVALID_VEH_ORDER_ID) + (uint)(to == INVALID_VEH_ORDER_ID);
    00287
    00288         VehicleOrderID selected_order = (this->sel_index + 1) / 2;
    00289         if (selected_order == old_num_orders) selected_order = 0; // when last travel time is selected, it belongs to order 0
    00290
    00291         bool travel = HasBit(this->sel_index, 0);
    00292
    00293         if (from != selected_order) {
    00294           /* Moving from preceding order? */
    00295           selected_order -= (int)(from <= selected_order);
    00296           /* Moving to   preceding order? */
    00297           selected_order += (int)(to   <= selected_order);
    00298         } else {
    00299           /* Now we are modifying the selected order */
    00300           if (to == INVALID_VEH_ORDER_ID) {
    00301             /* Deleting selected order */
    00302             this->DeleteChildWindows();
    00303             this->sel_index = -1;
    00304             break;
    00305           } else {
    00306             /* Moving selected order */
    00307             selected_order = to;
    00308           }
    00309         }
    00310
    00311         /* recompute new sel_index */
    00312         this->sel_index = 2 * selected_order - (int)travel;
    00313         /* travel time of first order needs special handling */
    00314         if (this->sel_index == -1) this->sel_index = this->vehicle->GetNumOrders() * 2 - 1;
    00315         break;
    00316       }
    00317     }
    00318   }
    00319
    00320
    00321   virtual void OnPaint()
    00322   {
    00323     const Vehicle *v = this->vehicle;
    00324     int selected = this->sel_index;
    00325
    00326     this->vscroll->SetCount(v->GetNumOrders() * 2);
    00327
    00328     if (v->owner == _local_company) {
    00329       bool disable = true;
    00330       if (selected != -1) {
    00331         const Order *order = v->GetOrder(((selected + 1) / 2) % v->GetNumOrders());
    00332         if (selected % 2 == 1) {
    00333           disable = order != NULL && (order->IsType(OT_CONDITIONAL) || order->IsType(OT_AUTOMATIC));
    00334         } else {
    00335           disable = order == NULL || ((!order->IsType(OT_GOTO_STATION) || (order->GetNonStopType() & ONSF_NO_STOP_AT_DESTINATION_STATION)) && !order->IsType(OT_CONDITIONAL));
    00336         }
    00337       }
    00338
    00339       this->SetWidgetDisabledState(TTV_CHANGE_TIME, disable);
    00340       this->SetWidgetDisabledState(TTV_CLEAR_TIME, disable);
    00341       this->SetWidgetDisabledState(TTV_SHARED_ORDER_LIST, !v->IsOrderListShared());
    00342
    00343       this->EnableWidget(TTV_START_DATE);
    00344       this->EnableWidget(TTV_RESET_LATENESS);
    00345       this->EnableWidget(TTV_AUTOFILL);
    00346     } else {
    00347       this->DisableWidget(TTV_START_DATE);
    00348       this->DisableWidget(TTV_CHANGE_TIME);
    00349       this->DisableWidget(TTV_CLEAR_TIME);
    00350       this->DisableWidget(TTV_RESET_LATENESS);
    00351       this->DisableWidget(TTV_AUTOFILL);
    00352       this->DisableWidget(TTV_SHARED_ORDER_LIST);
    00353     }
    00354
    00355     this->SetWidgetLoweredState(TTV_AUTOFILL, HasBit(v->vehicle_flags, VF_AUTOFILL_TIMETABLE));
    00356
    00357     this->DrawWidgets();
    00358   }
    00359
    00360   virtual void SetStringParameters(int widget) const
    00361   {
    00362     switch (widget) {
    00363       case TTV_CAPTION: SetDParam(0, this->vehicle->index); break;
    00364       case TTV_EXPECTED: SetDParam(0, this->show_expected ? STR_TIMETABLE_EXPECTED : STR_TIMETABLE_SCHEDULED); break;
    00365     }
    00366   }
    00367
    00368   virtual void DrawWidget(const Rect &r, int widget) const
    00369   {
    00370     const Vehicle *v = this->vehicle;
    00371     int selected = this->sel_index;
    00372
    00373     switch (widget) {
    00374       case TTV_TIMETABLE_PANEL: {
    00375         int y = r.top + WD_FRAMERECT_TOP;
    00376         int i = this->vscroll->GetPosition();
    00377         VehicleOrderID order_id = (i + 1) / 2;
    00378         bool final_order = false;
    00379
    00380         bool rtl = _current_text_dir == TD_RTL;
    00381         SetDParam(0, 99);
    00382         int index_column_width = GetStringBoundingBox(STR_ORDER_INDEX).width + GetSpriteSize(rtl ? SPR_ARROW_RIGHT : SPR_ARROW_LEFT).width + 3;
    00383         int middle = rtl ? r.right - WD_FRAMERECT_RIGHT - index_column_width : r.left + WD_FRAMERECT_LEFT + index_column_width;
    00384
    00385         const Order *order = v->GetOrder(order_id);
    00386         while (order != NULL) {
    00387           /* Don't draw anything if it extends past the end of the window. */
    00388           if (!this->vscroll->IsVisible(i)) break;
    00389
    00390           if (i % 2 == 0) {
    00391             DrawOrderString(v, order, order_id, y, i == selected, true, r.left + WD_FRAMERECT_LEFT, middle, r.right - WD_FRAMERECT_RIGHT);
    00392
    00393             order_id++;
    00394
    00395             if (order_id >= v->GetNumOrders()) {
    00396               order = v->GetOrder(0);
    00397               final_order = true;
    00398             } else {
    00399               order = order->next;
    00400             }
    00401           } else {
    00402             StringID string;
    00403             TextColour colour = (i == selected) ? TC_WHITE : TC_BLACK;
    00404             if (order->IsType(OT_CONDITIONAL)) {
    00405               string = STR_TIMETABLE_NO_TRAVEL;
    00406             } else if (order->IsType(OT_AUTOMATIC)) {
    00407               string = STR_TIMETABLE_NOT_TIMETABLEABLE;
    00408               colour = ((i == selected) ? TC_SILVER : TC_GREY) | TC_NO_SHADE;
    00409             } else if (order->travel_time == 0) {
    00410               string = STR_TIMETABLE_TRAVEL_NOT_TIMETABLED;
    00411             } else {
    00412               SetTimetableParams(0, 1, order->travel_time);
    00413               string = STR_TIMETABLE_TRAVEL_FOR;
    00414             }
    00415
    00416             DrawString(rtl ? r.left + WD_FRAMERECT_LEFT : middle, rtl ? middle : r.right - WD_FRAMERECT_LEFT, y, string, colour);
    00417
    00418             if (final_order) break;
    00419           }
    00420
    00421           i++;
    00422           y += FONT_HEIGHT_NORMAL;
    00423         }
    00424         break;
    00425       }
    00426
    00427       case TTV_ARRIVAL_DEPARTURE_PANEL: {
    00428         /* Arrival and departure times are handled in an all-or-nothing approach,
    00429          * i.e. are only shown if we can calculate all times.
    00430          * Excluding order lists with only one order makes some things easier.
    00431          */
    00432         Ticks total_time = v->orders.list != NULL ? v->orders.list->GetTimetableDurationIncomplete() : 0;
    00433         if (total_time <= 0 || v->GetNumOrders() <= 1 || !HasBit(v->vehicle_flags, VF_TIMETABLE_STARTED)) break;
    00434
    00435         TimetableArrivalDeparture *arr_dep = AllocaM(TimetableArrivalDeparture, v->GetNumOrders());
    00436         const VehicleOrderID cur_order = v->cur_real_order_index % v->GetNumOrders();
    00437
    00438         VehicleOrderID earlyID = BuildArrivalDepartureList(v, arr_dep) ? cur_order : (VehicleOrderID)INVALID_VEH_ORDER_ID;
    00439
    00440         int y = r.top + WD_FRAMERECT_TOP;
    00441
    00442         bool show_late = this->show_expected && v->lateness_counter > DAY_TICKS;
    00443         Ticks offset = show_late ? 0 : -v->lateness_counter;
    00444
    00445         bool rtl = _current_text_dir == TD_RTL;
    00446         int abbr_left  = rtl ? r.right - WD_FRAMERECT_RIGHT - this->deparr_abbr_width : r.left + WD_FRAMERECT_LEFT;
    00447         int abbr_right = rtl ? r.right - WD_FRAMERECT_RIGHT : r.left + WD_FRAMERECT_LEFT + this->deparr_abbr_width;
    00448         int time_left  = rtl ? r.left + WD_FRAMERECT_LEFT : r.right - WD_FRAMERECT_RIGHT - this->deparr_time_width;
    00449         int time_right = rtl ? r.left + WD_FRAMERECT_LEFT + this->deparr_time_width : r.right - WD_FRAMERECT_RIGHT;
    00450
    00451         for (int i = this->vscroll->GetPosition(); i / 2 < v->GetNumOrders(); ++i) { // note: i is also incremented in the loop
    00452           /* Don't draw anything if it extends past the end of the window. */
    00453           if (!this->vscroll->IsVisible(i)) break;
    00454
    00455           if (i % 2 == 0) {
    00456             if (arr_dep[i / 2].arrival != INVALID_TICKS) {
    00457               DrawString(abbr_left, abbr_right, y, STR_TIMETABLE_ARRIVAL_ABBREVIATION, i == selected ? TC_WHITE : TC_BLACK);
    00458               if (this->show_expected && i / 2 == earlyID) {
    00459                 SetArrivalDepartParams(0, 1, arr_dep[i / 2].arrival);
    00460                 DrawString(time_left, time_right, y, STR_GREEN_STRING, i == selected ? TC_WHITE : TC_BLACK);
    00461               } else {
    00462                 SetArrivalDepartParams(0, 1, arr_dep[i / 2].arrival + offset);
    00463                 DrawString(time_left, time_right, y, show_late ? STR_RED_STRING : STR_JUST_STRING, i == selected ? TC_WHITE : TC_BLACK);
    00464               }
    00465             }
    00466           } else {
    00467             if (arr_dep[i / 2].departure != INVALID_TICKS) {
    00468               DrawString(abbr_left, abbr_right, y, STR_TIMETABLE_DEPARTURE_ABBREVIATION, i == selected ? TC_WHITE : TC_BLACK);
    00469               SetArrivalDepartParams(0, 1, arr_dep[i/2].departure + offset);
    00470               DrawString(time_left, time_right, y, show_late ? STR_RED_STRING : STR_JUST_STRING, i == selected ? TC_WHITE : TC_BLACK);
    00471             }
    00472           }
    00473           y += FONT_HEIGHT_NORMAL;
    00474         }
    00475         break;
    00476       }
    00477
    00478       case TTV_SUMMARY_PANEL: {
    00479         int y = r.top + WD_FRAMERECT_TOP;
    00480
    00481         Ticks total_time = v->orders.list != NULL ? v->orders.list->GetTimetableDurationIncomplete() : 0;
    00482         if (total_time != 0) {
    00483           SetTimetableParams(0, 1, total_time);
    00484           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);
    00485         }
    00486         y += FONT_HEIGHT_NORMAL;
    00487
    00488         if (v->timetable_start != 0) {
    00489           /* We are running towards the first station so we can start the
    00490            * timetable at the given time. */
    00491           SetDParam(0, STR_JUST_DATE_TINY);
    00492           SetDParam(1, v->timetable_start);
    00493           DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_TIMETABLE_STATUS_START_AT);
    00494         } else if (!HasBit(v->vehicle_flags, VF_TIMETABLE_STARTED)) {
    00495           /* We aren't running on a timetable yet, so how can we be "on time"
    00496            * when we aren't even "on service"/"on duty"? */
    00497           DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_TIMETABLE_STATUS_NOT_STARTED);
    00498         } else if (v->lateness_counter == 0 || (!_settings_client.gui.timetable_in_ticks && v->lateness_counter / DAY_TICKS == 0)) {
    00499           DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_TIMETABLE_STATUS_ON_TIME);
    00500         } else {
    00501           SetTimetableParams(0, 1, abs(v->lateness_counter));
    00502           DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, v->lateness_counter < 0 ? STR_TIMETABLE_STATUS_EARLY : STR_TIMETABLE_STATUS_LATE);
    00503         }
    00504         break;
    00505       }
    00506     }
    00507   }
    00508
    00509   static inline uint32 PackTimetableArgs(const Vehicle *v, uint selected)
    00510   {
    00511     uint order_number = (selected + 1) / 2;
    00512     uint is_journey   = (selected % 2 == 1) ? 1 : 0;
    00513
    00514     if (order_number >= v->GetNumOrders()) order_number = 0;
    00515
    00516     return v->index | (order_number << 20) | (is_journey << 28);
    00517   }
    00518
    00519   virtual void OnClick(Point pt, int widget, int click_count)
    00520   {
    00521     const Vehicle *v = this->vehicle;
    00522
    00523     switch (widget) {
    00524       case TTV_ORDER_VIEW: // Order view button
    00525         ShowOrdersWindow(v);
    00526         break;
    00527
    00528       case TTV_TIMETABLE_PANEL: { // Main panel.
    00529         int selected = GetOrderFromTimetableWndPt(pt.y, v);
    00530
    00531         this->DeleteChildWindows();
    00532         this->sel_index = (selected == INVALID_ORDER || selected == this->sel_index) ? -1 : selected;
    00533         break;
    00534       }
    00535
    00536       case TTV_START_DATE: // Change the date that the timetable starts.
    00537         ShowSetDateWindow(this, v->index, _date, _cur_year, _cur_year + 15, ChangeTimetableStartCallback);
    00538         break;
    00539
    00540       case TTV_CHANGE_TIME: { // "Wait For" button.
    00541         int selected = this->sel_index;
    00542         VehicleOrderID real = (selected + 1) / 2;
    00543
    00544         if (real >= v->GetNumOrders()) real = 0;
    00545
    00546         const Order *order = v->GetOrder(real);
    00547         StringID current = STR_EMPTY;
    00548
    00549         if (order != NULL) {
    00550           uint time = (selected % 2 == 1) ? order->travel_time : order->wait_time;
    00551           if (!_settings_client.gui.timetable_in_ticks) time /= DAY_TICKS;
    00552
    00553           if (time != 0) {
    00554             SetDParam(0, time);
    00555             current = STR_JUST_INT;
    00556           }
    00557         }
    00558
    00559         ShowQueryString(current, STR_TIMETABLE_CHANGE_TIME, 31, 150, this, CS_NUMERAL, QSF_NONE);
    00560         break;
    00561       }
    00562
    00563       case TTV_CLEAR_TIME: { // Clear waiting time button.
    00564         uint32 p1 = PackTimetableArgs(v, this->sel_index);
    00565         DoCommandP(0, p1, 0, CMD_CHANGE_TIMETABLE | CMD_MSG(STR_ERROR_CAN_T_TIMETABLE_VEHICLE));
    00566         break;
    00567       }
    00568
    00569       case TTV_RESET_LATENESS: // Reset the vehicle's late counter.
    00570         DoCommandP(0, v->index, 0, CMD_SET_VEHICLE_ON_TIME | CMD_MSG(STR_ERROR_CAN_T_TIMETABLE_VEHICLE));
    00571         break;
    00572
    00573       case TTV_AUTOFILL: { // Autofill the timetable.
    00574         uint32 p2 = 0;
    00575         if (!HasBit(v->vehicle_flags, VF_AUTOFILL_TIMETABLE)) SetBit(p2, 0);
    00576         if (_ctrl_pressed) SetBit(p2, 1);
    00577         DoCommandP(0, v->index, p2, CMD_AUTOFILL_TIMETABLE | CMD_MSG(STR_ERROR_CAN_T_TIMETABLE_VEHICLE));
    00578         break;
    00579       }
    00580
    00581       case TTV_EXPECTED:
    00582         this->show_expected = !this->show_expected;
    00583         break;
    00584
    00585       case TTV_SHARED_ORDER_LIST:
    00586         ShowVehicleListWindow(v);
    00587         break;
    00588     }
    00589
    00590     this->SetDirty();
    00591   }
    00592
    00593   virtual void OnQueryTextFinished(char *str)
    00594   {
    00595     if (str == NULL) return;
    00596
    00597     const Vehicle *v = this->vehicle;
    00598
    00599     uint32 p1 = PackTimetableArgs(v, this->sel_index);
    00600
    00601     uint64 time = StrEmpty(str) ? 0 : strtoul(str, NULL, 10);
    00602     if (!_settings_client.gui.timetable_in_ticks) time *= DAY_TICKS;
    00603
    00604     uint32 p2 = minu(time, UINT16_MAX);
    00605
    00606     DoCommandP(0, p1, p2, CMD_CHANGE_TIMETABLE | CMD_MSG(STR_ERROR_CAN_T_TIMETABLE_VEHICLE));
    00607   }
    00608
    00609   virtual void OnResize()
    00610   {
    00611     /* Update the scroll bar */
    00612     this->vscroll->SetCapacityFromWidget(this, TTV_TIMETABLE_PANEL, WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM);
    00613   }
    00614
    00618   void UpdateSelectionStates()
    00619   {
    00620     this->GetWidget<NWidgetStacked>(TTV_ARRIVAL_DEPARTURE_SELECTION)->SetDisplayedPlane(_settings_client.gui.timetable_arrival_departure ? 0 : SZSP_NONE);
    00621     this->GetWidget<NWidgetStacked>(TTV_EXPECTED_SELECTION)->SetDisplayedPlane(_settings_client.gui.timetable_arrival_departure ? 0 : 1);
    00622   }
    00623 };
    00624
    00625 static const NWidgetPart _nested_timetable_widgets[] = {
    00626   NWidget(NWID_HORIZONTAL),
    00627     NWidget(WWT_CLOSEBOX, COLOUR_GREY),
    00628     NWidget(WWT_CAPTION, COLOUR_GREY, TTV_CAPTION), SetDataTip(STR_TIMETABLE_TITLE, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    00629     NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, TTV_ORDER_VIEW), SetMinimalSize(61, 14), SetDataTip( STR_TIMETABLE_ORDER_VIEW, STR_TIMETABLE_ORDER_VIEW_TOOLTIP),
    00630     NWidget(WWT_SHADEBOX, COLOUR_GREY),
    00631     NWidget(WWT_STICKYBOX, COLOUR_GREY),
    00632   EndContainer(),
    00633   NWidget(NWID_HORIZONTAL),
    00634     NWidget(WWT_PANEL, COLOUR_GREY, TTV_TIMETABLE_PANEL), SetMinimalSize(388, 82), SetResize(1, 10), SetDataTip(STR_NULL, STR_TIMETABLE_TOOLTIP), SetScrollbar(TTV_SCROLLBAR), EndContainer(),
    00635     NWidget(NWID_SELECTION, INVALID_COLOUR, TTV_ARRIVAL_DEPARTURE_SELECTION),
    00636       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(),
    00637     EndContainer(),
    00638     NWidget(NWID_VSCROLLBAR, COLOUR_GREY, TTV_SCROLLBAR),
    00639   EndContainer(),
    00640   NWidget(WWT_PANEL, COLOUR_GREY, TTV_SUMMARY_PANEL), SetMinimalSize(400, 22), SetResize(1, 0), EndContainer(),
    00641   NWidget(NWID_HORIZONTAL),
    00642     NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
    00643       NWidget(NWID_VERTICAL, NC_EQUALSIZE),
    00644         NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, TTV_CHANGE_TIME), SetResize(1, 0), SetFill(1, 1), SetDataTip(STR_TIMETABLE_CHANGE_TIME, STR_TIMETABLE_WAIT_TIME_TOOLTIP),
    00645         NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, TTV_CLEAR_TIME), SetResize(1, 0), SetFill(1, 1), SetDataTip(STR_TIMETABLE_CLEAR_TIME, STR_TIMETABLE_CLEAR_TIME_TOOLTIP),
    00646       EndContainer(),
    00647       NWidget(NWID_VERTICAL, NC_EQUALSIZE),
    00648         NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, TTV_START_DATE), SetResize(1, 0), SetFill(1, 1), SetDataTip(STR_TIMETABLE_STARTING_DATE, STR_TIMETABLE_STARTING_DATE_TOOLTIP),
    00649         NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, TTV_RESET_LATENESS), SetResize(1, 0), SetFill(1, 1), SetDataTip(STR_TIMETABLE_RESET_LATENESS, STR_TIMETABLE_RESET_LATENESS_TOOLTIP),
    00650       EndContainer(),
    00651       NWidget(NWID_VERTICAL, NC_EQUALSIZE),
    00652         NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, TTV_AUTOFILL), SetResize(1, 0), SetFill(1, 1), SetDataTip(STR_TIMETABLE_AUTOFILL, STR_TIMETABLE_AUTOFILL_TOOLTIP),
    00653         NWidget(NWID_SELECTION, INVALID_COLOUR, TTV_EXPECTED_SELECTION),
    00654           NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, TTV_EXPECTED), SetResize(1, 0), SetFill(1, 1), SetDataTip(STR_BLACK_STRING, STR_TIMETABLE_EXPECTED_TOOLTIP),
    00655           NWidget(WWT_PANEL, COLOUR_GREY), SetResize(1, 0), SetFill(1, 1), EndContainer(),
    00656         EndContainer(),
    00657       EndContainer(),
    00658     EndContainer(),
    00659     NWidget(NWID_VERTICAL, NC_EQUALSIZE),
    00660       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),
    00661       NWidget(WWT_RESIZEBOX, COLOUR_GREY), SetFill(0, 1),
    00662     EndContainer(),
    00663   EndContainer(),
    00664 };
    00665
    00666 static const WindowDesc _timetable_desc(
    00667   WDP_AUTO, 400, 130,
    00668   WC_VEHICLE_TIMETABLE, WC_VEHICLE_VIEW,
    00669   WDF_UNCLICK_BUTTONS | WDF_CONSTRUCTION,
    00670   _nested_timetable_widgets, lengthof(_nested_timetable_widgets)
    00671 );
    00672
    00673 void ShowTimetableWindow(const Vehicle *v)
    00674 {
    00675   DeleteWindowById(WC_VEHICLE_DETAILS, v->index, false);
    00676   DeleteWindowById(WC_VEHICLE_ORDERS, v->index, false);
    00677   AllocateWindowDescFront<TimetableWindow>(&_timetable_desc, v->index);
    00678 }