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