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