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