timetable_gui.cpp

Go to the documentation of this file.
00001 /* $Id: timetable_gui.cpp 21157 2010-11-13 09:56:25Z rubidium $ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
00008  */
00009 
00012 #include "stdafx.h"
00013 #include "command_func.h"
00014 #include "gui.h"
00015 #include "window_gui.h"
00016 #include "window_func.h"
00017 #include "textbuf_gui.h"
00018 #include "strings_func.h"
00019 #include "vehicle_base.h"
00020 #include "string_func.h"
00021 #include "gfx_func.h"
00022 #include "company_func.h"
00023 #include "date_func.h"
00024 #include "date_gui.h"
00025 #include "vehicle_gui.h"
00026 #include "settings_type.h"
00027 
00028 #include "table/sprites.h"
00029 #include "table/strings.h"
00030 
00031 enum TimetableViewWindowWidgets {
00032   TTV_CAPTION,
00033   TTV_ORDER_VIEW,
00034   TTV_TIMETABLE_PANEL,
00035   TTV_ARRIVAL_DEPARTURE_PANEL,      
00036   TTV_SCROLLBAR,
00037   TTV_SUMMARY_PANEL,
00038   TTV_START_DATE,
00039   TTV_CHANGE_TIME,
00040   TTV_CLEAR_TIME,
00041   TTV_RESET_LATENESS,
00042   TTV_AUTOFILL,
00043   TTV_EXPECTED,                    
00044   TTV_SHARED_ORDER_LIST,           
00045   TTV_ARRIVAL_DEPARTURE_SELECTION, 
00046   TTV_EXPECTED_SELECTION,          
00047 };
00048 
00050 struct TimetableArrivalDeparture {
00051   Ticks arrival;   
00052   Ticks departure; 
00053 };
00054 
00061 void SetTimetableParams(int param1, int param2, Ticks ticks)
00062 {
00063   if (_settings_client.gui.timetable_in_ticks) {
00064     SetDParam(param1, STR_TIMETABLE_TICKS);
00065     SetDParam(param2, ticks);
00066   } else {
00067     SetDParam(param1, STR_TIMETABLE_DAYS);
00068     SetDParam(param2, ticks / DAY_TICKS);
00069   }
00070 }
00071 
00078 static void SetArrivalDepartParams(int param1, int param2, Ticks ticks)
00079 {
00080   SetDParam(param1, STR_JUST_DATE_TINY);
00081   SetDParam(param2, _date + (ticks / DAY_TICKS));
00082 }
00083 
00090 static bool CanDetermineTimeTaken(const Order *order, bool travelling)
00091 {
00092   /* Current order is conditional */
00093   if (order->IsType(OT_CONDITIONAL)) return false;
00094   /* No travel time and we have not already finished travelling */
00095   if (travelling && order->travel_time == 0) return false;
00096   /* No wait time but we are loading at this timetabled station */
00097   if (!travelling && order->wait_time == 0 && order->IsType(OT_GOTO_STATION) && !(order->GetNonStopType() & ONSF_NO_STOP_AT_DESTINATION_STATION)) return false;
00098 
00099   return true;
00100 }
00101 
00102 
00111 static void FillTimetableArrivalDepartureTable(const Vehicle *v, VehicleOrderID start, bool travelling, TimetableArrivalDeparture *table, Ticks offset)
00112 {
00113   assert(table != NULL);
00114   assert(v->GetNumOrders() >= 2);
00115   assert(start < v->GetNumOrders());
00116 
00117   Ticks sum = offset;
00118   VehicleOrderID i = start;
00119   const Order *order = v->GetOrder(i);
00120 
00121   /* Pre-initialize with unknown time */
00122   for (int i = 0; i < v->GetNumOrders(); ++i) {
00123     table[i].arrival = table[i].departure = INVALID_TICKS;
00124   }
00125 
00126   /* Cyclically loop over all orders until we reach the current one again.
00127    * As we may start at the current order, do a post-checking loop */
00128   do {
00129     if (travelling || i != start) {
00130       if (!CanDetermineTimeTaken(order, true)) return;
00131       sum += order->travel_time;
00132       table[i].arrival = sum;
00133     }
00134 
00135     if (!CanDetermineTimeTaken(order, false)) return;
00136     sum += order->wait_time;
00137     table[i].departure = sum;
00138 
00139     ++i;
00140     order = order->next;
00141     if (i >= v->GetNumOrders()) {
00142       i = 0;
00143       assert(order == NULL);
00144       order = v->orders.list->GetFirstOrder();
00145     }
00146   } while (i != start);
00147 
00148   /* When loading at a scheduled station we still have to treat the
00149    * travelling part of the first order. */
00150   if (!travelling) {
00151     if (!CanDetermineTimeTaken(order, true)) return;
00152     sum += order->travel_time;
00153     table[i].arrival = sum;
00154   }
00155 }
00156 
00157 
00163 static void ChangeTimetableStartCallback(const Window *w, Date date)
00164 {
00165   DoCommandP(0, w->window_number, date, CMD_SET_TIMETABLE_START | CMD_MSG(STR_ERROR_CAN_T_TIMETABLE_VEHICLE));
00166 }
00167 
00168 
00169 struct TimetableWindow : Window {
00170   int sel_index;
00171   const Vehicle *vehicle; 
00172   bool show_expected;     
00173   uint deparr_time_width; 
00174   uint deparr_abbr_width; 
00175   Scrollbar *vscroll;
00176 
00177   TimetableWindow(const WindowDesc *desc, WindowNumber window_number) :
00178       Window(),
00179       sel_index(-1),
00180       vehicle(Vehicle::Get(window_number)),
00181       show_expected(true)
00182   {
00183     this->CreateNestedTree(desc);
00184     this->vscroll = this->GetScrollbar(TTV_SCROLLBAR);
00185     this->UpdateSelectionStates();
00186     this->FinishInitNested(desc, window_number);
00187 
00188     this->owner = this->vehicle->owner;
00189   }
00190 
00197   static bool BuildArrivalDepartureList(const Vehicle *v, TimetableArrivalDeparture *table)
00198   {
00199     assert(HasBit(v->vehicle_flags, VF_TIMETABLE_STARTED));
00200 
00201     bool travelling = (!v->current_order.IsType(OT_LOADING) || v->current_order.GetNonStopType() == ONSF_STOP_EVERYWHERE);
00202     Ticks start_time = _date_fract - v->current_order_time;
00203 
00204     FillTimetableArrivalDepartureTable(v, v->cur_order_index % v->GetNumOrders(), travelling, table, start_time);
00205 
00206     return (travelling && v->lateness_counter < 0);
00207   }
00208 
00209   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00210   {
00211     switch (widget) {
00212       case TTV_ARRIVAL_DEPARTURE_PANEL:
00213         SetDParam(0, MAX_YEAR * DAYS_IN_YEAR);
00214         this->deparr_time_width = GetStringBoundingBox(STR_JUST_DATE_TINY).width;
00215         this->deparr_abbr_width = max(GetStringBoundingBox(STR_TIMETABLE_ARRIVAL_ABBREVIATION).width, GetStringBoundingBox(STR_TIMETABLE_DEPARTURE_ABBREVIATION).width);
00216         size->width = WD_FRAMERECT_LEFT + this->deparr_abbr_width + 10 + this->deparr_time_width + WD_FRAMERECT_RIGHT;
00217         /* FALL THROUGH */
00218       case TTV_ARRIVAL_DEPARTURE_SELECTION:
00219       case TTV_TIMETABLE_PANEL:
00220         resize->height = FONT_HEIGHT_NORMAL;
00221         size->height = WD_FRAMERECT_TOP + 8 * resize->height + WD_FRAMERECT_BOTTOM;
00222         break;
00223 
00224       case TTV_SUMMARY_PANEL:
00225         size->height = WD_FRAMERECT_TOP + 2 * FONT_HEIGHT_NORMAL + WD_FRAMERECT_BOTTOM;
00226         break;
00227     }
00228   }
00229 
00230   int GetOrderFromTimetableWndPt(int y, const Vehicle *v)
00231   {
00232     int sel = (y - this->GetWidget<NWidgetBase>(TTV_TIMETABLE_PANEL)->pos_y - WD_FRAMERECT_TOP) / FONT_HEIGHT_NORMAL;
00233 
00234     if ((uint)sel >= this->vscroll->GetCapacity()) return INVALID_ORDER;
00235 
00236     sel += this->vscroll->GetPosition();
00237 
00238     return (sel < v->GetNumOrders() * 2 && sel >= 0) ? sel : INVALID_ORDER;
00239   }
00240 
00241   virtual void OnInvalidateData(int data)
00242   {
00243     switch (data) {
00244       case 0:
00245         /* Autoreplace replaced the vehicle */
00246         this->vehicle = Vehicle::Get(this->window_number);
00247         break;
00248 
00249       case -1:
00250         /* Removed / replaced all orders (after deleting / sharing) */
00251         if (this->sel_index == -1) break;
00252 
00253         this->DeleteChildWindows();
00254         this->sel_index = -1;
00255         break;
00256 
00257       case -2:
00258         this->UpdateSelectionStates();
00259         this->ReInit();
00260         break;
00261 
00262       default: {
00263         /* Moving an order. If one of these is INVALID_VEH_ORDER_ID, then
00264          * the order is being created / removed */
00265         if (this->sel_index == -1) break;
00266 
00267         VehicleOrderID from = GB(data, 0, 8);
00268         VehicleOrderID to   = GB(data, 8, 8);
00269 
00270         if (from == to) break; // no need to change anything
00271 
00272         /* if from == INVALID_VEH_ORDER_ID, one order was added; if to == INVALID_VEH_ORDER_ID, one order was removed */
00273         uint old_num_orders = this->vehicle->GetNumOrders() - (uint)(from == INVALID_VEH_ORDER_ID) + (uint)(to == INVALID_VEH_ORDER_ID);
00274 
00275         VehicleOrderID selected_order = (this->sel_index + 1) / 2;
00276         if (selected_order == old_num_orders) selected_order = 0; // when last travel time is selected, it belongs to order 0
00277 
00278         bool travel = HasBit(this->sel_index, 0);
00279 
00280         if (from != selected_order) {
00281           /* Moving from preceding order? */
00282           selected_order -= (int)(from <= selected_order);
00283           /* Moving to   preceding order? */
00284           selected_order += (int)(to   <= selected_order);
00285         } else {
00286           /* Now we are modifying the selected order */
00287           if (to == INVALID_VEH_ORDER_ID) {
00288             /* Deleting selected order */
00289             this->DeleteChildWindows();
00290             this->sel_index = -1;
00291             break;
00292           } else {
00293             /* Moving selected order */
00294             selected_order = to;
00295           }
00296         }
00297 
00298         /* recompute new sel_index */
00299         this->sel_index = 2 * selected_order - (int)travel;
00300         /* travel time of first order needs special handling */
00301         if (this->sel_index == -1) this->sel_index = this->vehicle->GetNumOrders() * 2 - 1;
00302         break;
00303       }
00304     }
00305   }
00306 
00307 
00308   virtual void OnPaint()
00309   {
00310     const Vehicle *v = this->vehicle;
00311     int selected = this->sel_index;
00312 
00313     this->vscroll->SetCount(v->GetNumOrders() * 2);
00314 
00315     if (v->owner == _local_company) {
00316       bool disable = true;
00317       if (selected != -1) {
00318         const Order *order = v->GetOrder(((selected + 1) / 2) % v->GetNumOrders());
00319         if (selected % 2 == 1) {
00320           disable = order != NULL && order->IsType(OT_CONDITIONAL);
00321         } else {
00322           disable = order == NULL || ((!order->IsType(OT_GOTO_STATION) || (order->GetNonStopType() & ONSF_NO_STOP_AT_DESTINATION_STATION)) && !order->IsType(OT_CONDITIONAL));
00323         }
00324       }
00325 
00326       this->SetWidgetDisabledState(TTV_CHANGE_TIME, disable);
00327       this->SetWidgetDisabledState(TTV_CLEAR_TIME, disable);
00328       this->SetWidgetDisabledState(TTV_SHARED_ORDER_LIST, !v->IsOrderListShared());
00329 
00330       this->EnableWidget(TTV_START_DATE);
00331       this->EnableWidget(TTV_RESET_LATENESS);
00332       this->EnableWidget(TTV_AUTOFILL);
00333     } else {
00334       this->DisableWidget(TTV_START_DATE);
00335       this->DisableWidget(TTV_CHANGE_TIME);
00336       this->DisableWidget(TTV_CLEAR_TIME);
00337       this->DisableWidget(TTV_RESET_LATENESS);
00338       this->DisableWidget(TTV_AUTOFILL);
00339       this->DisableWidget(TTV_SHARED_ORDER_LIST);
00340     }
00341 
00342     this->SetWidgetLoweredState(TTV_AUTOFILL, HasBit(v->vehicle_flags, VF_AUTOFILL_TIMETABLE));
00343 
00344     this->DrawWidgets();
00345   }
00346 
00347   virtual void SetStringParameters(int widget) const
00348   {
00349     switch (widget) {
00350       case TTV_CAPTION: SetDParam(0, this->vehicle->index); break;
00351       case TTV_EXPECTED: SetDParam(0, this->show_expected ? STR_TIMETABLE_EXPECTED : STR_TIMETABLE_SCHEDULED); break;
00352     }
00353   }
00354 
00355   virtual void DrawWidget(const Rect &r, int widget) const
00356   {
00357     const Vehicle *v = this->vehicle;
00358     int selected = this->sel_index;
00359 
00360     switch (widget) {
00361       case TTV_TIMETABLE_PANEL: {
00362         int y = r.top + WD_FRAMERECT_TOP;
00363         int i = this->vscroll->GetPosition();
00364         VehicleOrderID order_id = (i + 1) / 2;
00365         bool final_order = false;
00366 
00367         bool rtl = _current_text_dir == TD_RTL;
00368         SetDParam(0, 99);
00369         int index_column_width = GetStringBoundingBox(STR_ORDER_INDEX).width + GetSpriteSize(rtl ? SPR_ARROW_RIGHT : SPR_ARROW_LEFT).width + 3;
00370         int middle = rtl ? r.right - WD_FRAMERECT_RIGHT - index_column_width : r.left + WD_FRAMERECT_LEFT + index_column_width;
00371 
00372         const Order *order = v->GetOrder(order_id);
00373         while (order != NULL) {
00374           /* Don't draw anything if it extends past the end of the window. */
00375           if (!this->vscroll->IsVisible(i)) break;
00376 
00377           if (i % 2 == 0) {
00378             DrawOrderString(v, order, order_id, y, i == selected, true, r.left + WD_FRAMERECT_LEFT, middle, r.right - WD_FRAMERECT_RIGHT);
00379 
00380             order_id++;
00381 
00382             if (order_id >= v->GetNumOrders()) {
00383               order = v->GetOrder(0);
00384               final_order = true;
00385             } else {
00386               order = order->next;
00387             }
00388           } else {
00389             StringID string;
00390 
00391             if (order->IsType(OT_CONDITIONAL)) {
00392               string = STR_TIMETABLE_NO_TRAVEL;
00393             } else if (order->travel_time == 0) {
00394               string = STR_TIMETABLE_TRAVEL_NOT_TIMETABLED;
00395             } else {
00396               SetTimetableParams(0, 1, order->travel_time);
00397               string = STR_TIMETABLE_TRAVEL_FOR;
00398             }
00399 
00400             DrawString(rtl ? r.left + WD_FRAMERECT_LEFT : middle, rtl ? middle : r.right - WD_FRAMERECT_LEFT, y, string, (i == selected) ? TC_WHITE : TC_BLACK);
00401 
00402             if (final_order) break;
00403           }
00404 
00405           i++;
00406           y += FONT_HEIGHT_NORMAL;
00407         }
00408         break;
00409       }
00410 
00411       case TTV_ARRIVAL_DEPARTURE_PANEL: {
00412         /* Arrival and departure times are handled in an all-or-nothing approach,
00413          * i.e. are only shown if we can calculate all times.
00414          * Excluding order lists with only one order makes some things easier.
00415          */
00416         Ticks total_time = v->orders.list != NULL ? v->orders.list->GetTimetableDurationIncomplete() : 0;
00417         if (total_time <= 0 || v->GetNumOrders() <= 1 || !HasBit(v->vehicle_flags, VF_TIMETABLE_STARTED)) break;
00418 
00419         TimetableArrivalDeparture *arr_dep = AllocaM(TimetableArrivalDeparture, v->GetNumOrders());
00420         const VehicleOrderID cur_order = v->cur_order_index % v->GetNumOrders();
00421 
00422         VehicleOrderID earlyID = BuildArrivalDepartureList(v, arr_dep) ? cur_order : (VehicleOrderID)INVALID_VEH_ORDER_ID;
00423 
00424         int y = r.top + WD_FRAMERECT_TOP;
00425 
00426         bool show_late = this->show_expected && v->lateness_counter > DAY_TICKS;
00427         Ticks offset = show_late ? 0 : -v->lateness_counter;
00428 
00429         bool rtl = _current_text_dir == TD_RTL;
00430         int abbr_left  = rtl ? r.right - WD_FRAMERECT_RIGHT - this->deparr_abbr_width : r.left + WD_FRAMERECT_LEFT;
00431         int abbr_right = rtl ? r.right - WD_FRAMERECT_RIGHT : r.left + WD_FRAMERECT_LEFT + this->deparr_abbr_width;
00432         int time_left  = rtl ? r.left + WD_FRAMERECT_LEFT : r.right - WD_FRAMERECT_RIGHT - this->deparr_time_width;
00433         int time_right = rtl ? r.left + WD_FRAMERECT_LEFT + this->deparr_time_width : r.right - WD_FRAMERECT_RIGHT;
00434 
00435         for (int i = this->vscroll->GetPosition(); i / 2 < v->GetNumOrders(); ++i) { // note: i is also incremented in the loop
00436           /* Don't draw anything if it extends past the end of the window. */
00437           if (!this->vscroll->IsVisible(i)) break;
00438 
00439           if (i % 2 == 0) {
00440             if (arr_dep[i / 2].arrival != INVALID_TICKS) {
00441               DrawString(abbr_left, abbr_right, y, STR_TIMETABLE_ARRIVAL_ABBREVIATION, i == selected ? TC_WHITE : TC_BLACK);
00442               if (this->show_expected && i / 2 == earlyID) {
00443                 SetArrivalDepartParams(0, 1, arr_dep[i / 2].arrival);
00444                 DrawString(time_left, time_right, y, STR_GREEN_STRING, i == selected ? TC_WHITE : TC_BLACK);
00445               } else {
00446                 SetArrivalDepartParams(0, 1, arr_dep[i / 2].arrival + offset);
00447                 DrawString(time_left, time_right, y, show_late ? STR_RED_STRING : STR_JUST_STRING, i == selected ? TC_WHITE : TC_BLACK);
00448               }
00449             }
00450           } else {
00451             if (arr_dep[i / 2].departure != INVALID_TICKS) {
00452               DrawString(abbr_left, abbr_right, y, STR_TIMETABLE_DEPARTURE_ABBREVIATION, i == selected ? TC_WHITE : TC_BLACK);
00453               SetArrivalDepartParams(0, 1, arr_dep[i/2].departure + 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           y += FONT_HEIGHT_NORMAL;
00458         }
00459         break;
00460       }
00461 
00462       case TTV_SUMMARY_PANEL: {
00463         int y = r.top + WD_FRAMERECT_TOP;
00464 
00465         Ticks total_time = v->orders.list != NULL ? v->orders.list->GetTimetableDurationIncomplete() : 0;
00466         if (total_time != 0) {
00467           SetTimetableParams(0, 1, total_time);
00468           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);
00469         }
00470         y += FONT_HEIGHT_NORMAL;
00471 
00472         if (v->timetable_start != 0) {
00473           /* We are running towards the first station so we can start the
00474            * timetable at the given time. */
00475           SetDParam(0, STR_JUST_DATE_TINY);
00476           SetDParam(1, v->timetable_start);
00477           DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_TIMETABLE_STATUS_START_AT);
00478         } else if (!HasBit(v->vehicle_flags, VF_TIMETABLE_STARTED)) {
00479           /* We aren't running on a timetable yet, so how can we be "on time"
00480            * when we aren't even "on service"/"on duty"? */
00481           DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_TIMETABLE_STATUS_NOT_STARTED);
00482         } else if (v->lateness_counter == 0 || (!_settings_client.gui.timetable_in_ticks && v->lateness_counter / DAY_TICKS == 0)) {
00483           DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_TIMETABLE_STATUS_ON_TIME);
00484         } else {
00485           SetTimetableParams(0, 1, abs(v->lateness_counter));
00486           DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, v->lateness_counter < 0 ? STR_TIMETABLE_STATUS_EARLY : STR_TIMETABLE_STATUS_LATE);
00487         }
00488         break;
00489       }
00490     }
00491   }
00492 
00493   static inline uint32 PackTimetableArgs(const Vehicle *v, uint selected)
00494   {
00495     uint order_number = (selected + 1) / 2;
00496     uint is_journey   = (selected % 2 == 1) ? 1 : 0;
00497 
00498     if (order_number >= v->GetNumOrders()) order_number = 0;
00499 
00500     return v->index | (order_number << 20) | (is_journey << 28);
00501   }
00502 
00503   virtual void OnClick(Point pt, int widget, int click_count)
00504   {
00505     const Vehicle *v = this->vehicle;
00506 
00507     switch (widget) {
00508       case TTV_ORDER_VIEW: // Order view button
00509         ShowOrdersWindow(v);
00510         break;
00511 
00512       case TTV_TIMETABLE_PANEL: { // Main panel.
00513         int selected = GetOrderFromTimetableWndPt(pt.y, v);
00514 
00515         this->DeleteChildWindows();
00516         this->sel_index = (selected == INVALID_ORDER || selected == this->sel_index) ? -1 : selected;
00517         break;
00518       }
00519 
00520       case TTV_START_DATE: // Change the date that the timetable starts.
00521         ShowSetDateWindow(this, v->index, _date, _cur_year, _cur_year + 15, ChangeTimetableStartCallback);
00522         break;
00523 
00524       case TTV_CHANGE_TIME: { // "Wait For" button.
00525         int selected = this->sel_index;
00526         VehicleOrderID real = (selected + 1) / 2;
00527 
00528         if (real >= v->GetNumOrders()) real = 0;
00529 
00530         const Order *order = v->GetOrder(real);
00531         StringID current = STR_EMPTY;
00532 
00533         if (order != NULL) {
00534           uint time = (selected % 2 == 1) ? order->travel_time : order->wait_time;
00535           if (!_settings_client.gui.timetable_in_ticks) time /= DAY_TICKS;
00536 
00537           if (time != 0) {
00538             SetDParam(0, time);
00539             current = STR_JUST_INT;
00540           }
00541         }
00542 
00543         ShowQueryString(current, STR_TIMETABLE_CHANGE_TIME, 31, 150, this, CS_NUMERAL, QSF_NONE);
00544         break;
00545       }
00546 
00547       case TTV_CLEAR_TIME: { // Clear waiting time button.
00548         uint32 p1 = PackTimetableArgs(v, this->sel_index);
00549         DoCommandP(0, p1, 0, CMD_CHANGE_TIMETABLE | CMD_MSG(STR_ERROR_CAN_T_TIMETABLE_VEHICLE));
00550         break;
00551       }
00552 
00553       case TTV_RESET_LATENESS: // Reset the vehicle's late counter.
00554         DoCommandP(0, v->index, 0, CMD_SET_VEHICLE_ON_TIME | CMD_MSG(STR_ERROR_CAN_T_TIMETABLE_VEHICLE));
00555         break;
00556 
00557       case TTV_AUTOFILL: { // Autofill the timetable.
00558         uint32 p2 = 0;
00559         if (!HasBit(v->vehicle_flags, VF_AUTOFILL_TIMETABLE)) SetBit(p2, 0);
00560         if (_ctrl_pressed) SetBit(p2, 1);
00561         DoCommandP(0, v->index, p2, CMD_AUTOFILL_TIMETABLE | CMD_MSG(STR_ERROR_CAN_T_TIMETABLE_VEHICLE));
00562         break;
00563       }
00564 
00565       case TTV_EXPECTED:
00566         this->show_expected = !this->show_expected;
00567         break;
00568 
00569       case TTV_SHARED_ORDER_LIST:
00570         ShowVehicleListWindow(v);
00571         break;
00572     }
00573 
00574     this->SetDirty();
00575   }
00576 
00577   virtual void OnQueryTextFinished(char *str)
00578   {
00579     if (str == NULL) return;
00580 
00581     const Vehicle *v = this->vehicle;
00582 
00583     uint32 p1 = PackTimetableArgs(v, this->sel_index);
00584 
00585     uint64 time = StrEmpty(str) ? 0 : strtoul(str, NULL, 10);
00586     if (!_settings_client.gui.timetable_in_ticks) time *= DAY_TICKS;
00587 
00588     uint32 p2 = minu(time, UINT16_MAX);
00589 
00590     DoCommandP(0, p1, p2, CMD_CHANGE_TIMETABLE | CMD_MSG(STR_ERROR_CAN_T_TIMETABLE_VEHICLE));
00591   }
00592 
00593   virtual void OnResize()
00594   {
00595     /* Update the scroll bar */
00596     this->vscroll->SetCapacityFromWidget(this, TTV_TIMETABLE_PANEL, WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM);
00597   }
00598 
00602   void UpdateSelectionStates()
00603   {
00604     this->GetWidget<NWidgetStacked>(TTV_ARRIVAL_DEPARTURE_SELECTION)->SetDisplayedPlane(_settings_client.gui.timetable_arrival_departure ? 0 : SZSP_NONE);
00605     this->GetWidget<NWidgetStacked>(TTV_EXPECTED_SELECTION)->SetDisplayedPlane(_settings_client.gui.timetable_arrival_departure ? 0 : 1);
00606   }
00607 };
00608 
00609 static const NWidgetPart _nested_timetable_widgets[] = {
00610   NWidget(NWID_HORIZONTAL),
00611     NWidget(WWT_CLOSEBOX, COLOUR_GREY),
00612     NWidget(WWT_CAPTION, COLOUR_GREY, TTV_CAPTION), SetDataTip(STR_TIMETABLE_TITLE, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
00613     NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, TTV_ORDER_VIEW), SetMinimalSize(61, 14), SetDataTip( STR_TIMETABLE_ORDER_VIEW, STR_TIMETABLE_ORDER_VIEW_TOOLTIP),
00614     NWidget(WWT_SHADEBOX, COLOUR_GREY),
00615     NWidget(WWT_STICKYBOX, COLOUR_GREY),
00616   EndContainer(),
00617   NWidget(NWID_HORIZONTAL),
00618     NWidget(WWT_PANEL, COLOUR_GREY, TTV_TIMETABLE_PANEL), SetMinimalSize(388, 82), SetResize(1, 10), SetDataTip(STR_NULL, STR_TIMETABLE_TOOLTIP), SetScrollbar(TTV_SCROLLBAR), EndContainer(),
00619     NWidget(NWID_SELECTION, INVALID_COLOUR, TTV_ARRIVAL_DEPARTURE_SELECTION),
00620       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(),
00621     EndContainer(),
00622     NWidget(NWID_VSCROLLBAR, COLOUR_GREY, TTV_SCROLLBAR),
00623   EndContainer(),
00624   NWidget(WWT_PANEL, COLOUR_GREY, TTV_SUMMARY_PANEL), SetMinimalSize(400, 22), SetResize(1, 0), EndContainer(),
00625   NWidget(NWID_HORIZONTAL),
00626     NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
00627       NWidget(NWID_VERTICAL, NC_EQUALSIZE),
00628         NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, TTV_CHANGE_TIME), SetResize(1, 0), SetFill(1, 1), SetDataTip(STR_TIMETABLE_CHANGE_TIME, STR_TIMETABLE_WAIT_TIME_TOOLTIP),
00629         NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, TTV_CLEAR_TIME), SetResize(1, 0), SetFill(1, 1), SetDataTip(STR_TIMETABLE_CLEAR_TIME, STR_TIMETABLE_CLEAR_TIME_TOOLTIP),
00630       EndContainer(),
00631       NWidget(NWID_VERTICAL, NC_EQUALSIZE),
00632         NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, TTV_START_DATE), SetResize(1, 0), SetFill(1, 1), SetDataTip(STR_TIMETABLE_STARTING_DATE, STR_TIMETABLE_STARTING_DATE_TOOLTIP),
00633         NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, TTV_RESET_LATENESS), SetResize(1, 0), SetFill(1, 1), SetDataTip(STR_TIMETABLE_RESET_LATENESS, STR_TIMETABLE_RESET_LATENESS_TOOLTIP),
00634       EndContainer(),
00635       NWidget(NWID_VERTICAL, NC_EQUALSIZE),
00636         NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, TTV_AUTOFILL), SetResize(1, 0), SetFill(1, 1), SetDataTip(STR_TIMETABLE_AUTOFILL, STR_TIMETABLE_AUTOFILL_TOOLTIP),
00637         NWidget(NWID_SELECTION, INVALID_COLOUR, TTV_EXPECTED_SELECTION),
00638           NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, TTV_EXPECTED), SetResize(1, 0), SetFill(1, 1), SetDataTip(STR_BLACK_STRING, STR_TIMETABLE_EXPECTED_TOOLTIP),
00639           NWidget(WWT_PANEL, COLOUR_GREY), SetResize(1, 0), SetFill(1, 1), EndContainer(),
00640         EndContainer(),
00641       EndContainer(),
00642     EndContainer(),
00643     NWidget(NWID_VERTICAL, NC_EQUALSIZE),
00644       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),
00645       NWidget(WWT_RESIZEBOX, COLOUR_GREY), SetFill(0, 1),
00646     EndContainer(),
00647   EndContainer(),
00648 };
00649 
00650 static const WindowDesc _timetable_desc(
00651   WDP_AUTO, 400, 130,
00652   WC_VEHICLE_TIMETABLE, WC_VEHICLE_VIEW,
00653   WDF_UNCLICK_BUTTONS | WDF_CONSTRUCTION,
00654   _nested_timetable_widgets, lengthof(_nested_timetable_widgets)
00655 );
00656 
00657 void ShowTimetableWindow(const Vehicle *v)
00658 {
00659   DeleteWindowById(WC_VEHICLE_DETAILS, v->index, false);
00660   DeleteWindowById(WC_VEHICLE_ORDERS, v->index, false);
00661   AllocateWindowDescFront<TimetableWindow>(&_timetable_desc, v->index);
00662 }

Generated on Thu Dec 23 23:41:33 2010 for OpenTTD by  doxygen 1.6.1