timetable_gui.cpp

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