# vehicle_gui.cpp

Go to the documentation of this file.

    00001 /* $Id: vehicle_gui.cpp 15592 2009-02-27 20:40:39Z frosch $ */
    00002
    00005 #include "stdafx.h"
    00006 #include "openttd.h"
    00007 #include "debug.h"
    00008 #include "company_func.h"
    00009 #include "gui.h"
    00010 #include "window_gui.h"
    00011 #include "textbuf_gui.h"
    00012 #include "command_func.h"
    00013 #include "vehicle_gui.h"
    00014 #include "vehicle_gui_base.h"
    00015 #include "viewport_func.h"
    00016 #include "gfx_func.h"
    00017 #include "newgrf_engine.h"
    00018 #include "newgrf_text.h"
    00019 #include "station_map.h"
    00020 #include "roadveh.h"
    00021 #include "depot_base.h"
    00022 #include "group_gui.h"
    00023 #include "strings_func.h"
    00024 #include "window_func.h"
    00025 #include "vehicle_func.h"
    00026 #include "autoreplace_gui.h"
    00027 #include "string_func.h"
    00028 #include "widgets/dropdown_func.h"
    00029 #include "timetable.h"
    00030 #include "vehiclelist.h"
    00031 #include "settings_type.h"
    00032 #include "articulated_vehicles.h"
    00033
    00034 #include "table/sprites.h"
    00035 #include "table/strings.h"
    00036
    00037 Sorting _sorting;
    00038
    00039 static GUIVehicleList::SortFunction VehicleNumberSorter;
    00040 static GUIVehicleList::SortFunction VehicleNameSorter;
    00041 static GUIVehicleList::SortFunction VehicleAgeSorter;
    00042 static GUIVehicleList::SortFunction VehicleProfitThisYearSorter;
    00043 static GUIVehicleList::SortFunction VehicleProfitLastYearSorter;
    00044 static GUIVehicleList::SortFunction VehicleCargoSorter;
    00045 static GUIVehicleList::SortFunction VehicleReliabilitySorter;
    00046 static GUIVehicleList::SortFunction VehicleMaxSpeedSorter;
    00047 static GUIVehicleList::SortFunction VehicleModelSorter;
    00048 static GUIVehicleList::SortFunction VehicleValueSorter;
    00049 static GUIVehicleList::SortFunction VehicleLengthSorter;
    00050 static GUIVehicleList::SortFunction VehicleTimeToLiveSorter;
    00051
    00052 GUIVehicleList::SortFunction * const BaseVehicleListWindow::vehicle_sorter_funcs[] = {
    00053   &VehicleNumberSorter,
    00054   &VehicleNameSorter,
    00055   &VehicleAgeSorter,
    00056   &VehicleProfitThisYearSorter,
    00057   &VehicleProfitLastYearSorter,
    00058   &VehicleCargoSorter,
    00059   &VehicleReliabilitySorter,
    00060   &VehicleMaxSpeedSorter,
    00061   &VehicleModelSorter,
    00062   &VehicleValueSorter,
    00063   &VehicleLengthSorter,
    00064   &VehicleTimeToLiveSorter,
    00065 };
    00066
    00067 const StringID BaseVehicleListWindow::vehicle_sorter_names[] = {
    00068   STR_SORT_BY_NUMBER,
    00069   STR_SORT_BY_DROPDOWN_NAME,
    00070   STR_SORT_BY_AGE,
    00071   STR_SORT_BY_PROFIT_THIS_YEAR,
    00072   STR_SORT_BY_PROFIT_LAST_YEAR,
    00073   STR_SORT_BY_TOTAL_CAPACITY_PER_CARGOTYPE,
    00074   STR_SORT_BY_RELIABILITY,
    00075   STR_SORT_BY_MAX_SPEED,
    00076   STR_SORT_BY_MODEL,
    00077   STR_SORT_BY_VALUE,
    00078   STR_SORT_BY_LENGTH,
    00079   STR_SORT_BY_LIFE_TIME,
    00080   INVALID_STRING_ID
    00081 };
    00082
    00083 void BaseVehicleListWindow::BuildVehicleList(Owner owner, uint16 index, uint16 window_type)
    00084 {
    00085   if (!this->vehicles.NeedRebuild()) return;
    00086
    00087   DEBUG(misc, 3, "Building vehicle list for company %d at station %d", owner, index);
    00088
    00089   GenerateVehicleSortList(&this->vehicles, this->vehicle_type, owner, index, window_type);
    00090
    00091   this->vehicles.RebuildDone();
    00092 }
    00093
    00094 /* cached values for VehicleNameSorter to spare many GetString() calls */
    00095 static const Vehicle *_last_vehicle[2] = { NULL, NULL };
    00096
    00097 void BaseVehicleListWindow::SortVehicleList()
    00098 {
    00099   if (this->vehicles.Sort()) return;
    00100
    00101   /* invalidate cached values for name sorter - vehicle names could change */
    00102   _last_vehicle[0] = _last_vehicle[1] = NULL;
    00103 }
    00104
    00105 void DepotSortList(VehicleList *list)
    00106 {
    00107   if (list->Length() < 2) return;
    00108   QSortT(list->Begin(), list->Length(), &VehicleNumberSorter);
    00109 }
    00110
    00112 void DrawVehicleProfitButton(const Vehicle *v, int x, int y)
    00113 {
    00114   SpriteID pal;
    00115
    00116   /* draw profit-based coloured icons */
    00117   if (v->age <= DAYS_IN_YEAR * 2) {
    00118     pal = PALETTE_TO_GREY;
    00119   } else if (v->GetDisplayProfitLastYear() < 0) {
    00120     pal = PALETTE_TO_RED;
    00121   } else if (v->GetDisplayProfitLastYear() < 10000) {
    00122     pal = PALETTE_TO_YELLOW;
    00123   } else {
    00124     pal = PALETTE_TO_GREEN;
    00125   }
    00126   DrawSprite(SPR_BLOT, pal, x, y);
    00127 }
    00128
    00129 struct RefitOption {
    00130   CargoID cargo;
    00131   byte subtype;
    00132   uint16 value;
    00133   EngineID engine;
    00134 };
    00135
    00136 struct RefitList {
    00137   uint num_lines;
    00138   RefitOption *items;
    00139 };
    00140
    00141 static RefitList *BuildRefitList(const Vehicle *v)
    00142 {
    00143   uint max_lines = 256;
    00144   RefitOption *refit = CallocT<RefitOption>(max_lines);
    00145   RefitList *list = CallocT<RefitList>(1);
    00146   Vehicle *u = (Vehicle*)v;
    00147   uint num_lines = 0;
    00148   uint i;
    00149
    00150   do {
    00151     uint32 cmask = EngInfo(u->engine_type)->refit_mask;
    00152     byte callbackmask = EngInfo(u->engine_type)->callbackmask;
    00153
    00154     /* Skip this engine if it has no capacity */
    00155     if (u->cargo_cap == 0) continue;
    00156
    00157     /* Loop through all cargos in the refit mask */
    00158     for (CargoID cid = 0; cid < NUM_CARGO && num_lines < max_lines; cid++) {
    00159       /* Skip cargo type if it's not listed */
    00160       if (!HasBit(cmask, cid)) continue;
    00161
    00162       /* Check the vehicle's callback mask for cargo suffixes */
    00163       if (HasBit(callbackmask, CBM_VEHICLE_CARGO_SUFFIX)) {
    00164         /* Make a note of the original cargo type. It has to be
    00165          * changed to test the cargo & subtype... */
    00166         CargoID temp_cargo = u->cargo_type;
    00167         byte temp_subtype  = u->cargo_subtype;
    00168         byte refit_cyc;
    00169
    00170         u->cargo_type = cid;
    00171
    00172         for (refit_cyc = 0; refit_cyc < 16 && num_lines < max_lines; refit_cyc++) {
    00173           bool duplicate = false;
    00174           uint16 callback;
    00175
    00176           u->cargo_subtype = refit_cyc;
    00177           callback = GetVehicleCallback(CBID_VEHICLE_CARGO_SUFFIX, 0, 0, u->engine_type, u);
    00178
    00179           if (callback == 0xFF) callback = CALLBACK_FAILED;
    00180           if (refit_cyc != 0 && callback == CALLBACK_FAILED) break;
    00181
    00182           /* Check if this cargo and subtype combination are listed */
    00183           for (i = 0; i < num_lines && !duplicate; i++) {
    00184             if (refit[i].cargo == cid && refit[i].value == callback) duplicate = true;
    00185           }
    00186
    00187           if (duplicate) continue;
    00188
    00189           refit[num_lines].cargo   = cid;
    00190           refit[num_lines].subtype = refit_cyc;
    00191           refit[num_lines].value   = callback;
    00192           refit[num_lines].engine  = u->engine_type;
    00193           num_lines++;
    00194         }
    00195
    00196         /* Reset the vehicle's cargo type */
    00197         u->cargo_type    = temp_cargo;
    00198         u->cargo_subtype = temp_subtype;
    00199       } else {
    00200         /* No cargo suffix callback -- use no subtype */
    00201         bool duplicate = false;
    00202
    00203         for (i = 0; i < num_lines && !duplicate; i++) {
    00204           if (refit[i].cargo == cid && refit[i].value == CALLBACK_FAILED) duplicate = true;
    00205         }
    00206
    00207         if (!duplicate) {
    00208           refit[num_lines].cargo   = cid;
    00209           refit[num_lines].subtype = 0;
    00210           refit[num_lines].value   = CALLBACK_FAILED;
    00211           refit[num_lines].engine  = INVALID_ENGINE;
    00212           num_lines++;
    00213         }
    00214       }
    00215     }
    00216   } while ((v->type == VEH_TRAIN || v->type == VEH_ROAD) && (u = u->Next()) != NULL && num_lines < max_lines);
    00217
    00218   list->num_lines = num_lines;
    00219   list->items = refit;
    00220
    00221   return list;
    00222 }
    00223
    00233 static RefitOption *DrawVehicleRefitWindow(const RefitList *list, int sel, uint pos, uint rows, uint delta)
    00234 {
    00235   RefitOption *refit = list->items;
    00236   RefitOption *selected = NULL;
    00237   uint num_lines = list->num_lines;
    00238   uint y = 31;
    00239   uint i;
    00240
    00241   /* Draw the list, and find the selected cargo (by its position in list) */
    00242   for (i = 0; i < num_lines; i++) {
    00243     TextColour colour = TC_BLACK;
    00244     if (sel == 0) {
    00245       selected = &refit[i];
    00246       colour = TC_WHITE;
    00247     }
    00248
    00249     if (i >= pos && i < pos + rows) {
    00250       /* Draw the cargo name */
    00251       int last_x = DrawString(2, y, GetCargo(refit[i].cargo)->name, colour);
    00252
    00253       /* If the callback succeeded, draw the cargo suffix */
    00254       if (refit[i].value != CALLBACK_FAILED) {
    00255         DrawString(last_x + 1, y, GetGRFStringID(GetEngineGRFID(refit[i].engine), 0xD000 + refit[i].value), colour);
    00256       }
    00257       y += delta;
    00258     }
    00259
    00260     sel--;
    00261   }
    00262
    00263   return selected;
    00264 }
    00265
    00266 struct RefitWindow : public Window {
    00267   int sel;
    00268   RefitOption *cargo;
    00269   RefitList *list;
    00270   uint length;
    00271   VehicleOrderID order;
    00272
    00273   RefitWindow(const WindowDesc *desc, const Vehicle *v, VehicleOrderID order) : Window(desc, v->index)
    00274   {
    00275     this->owner = v->owner;
    00276     this->vscroll.cap = 8;
    00277     this->resize.step_height = 14;
    00278
    00279     this->order = order;
    00280     this->sel  = -1;
    00281     this->list = BuildRefitList(v);
    00282     if (v->type == VEH_TRAIN) this->length = CountVehiclesInChain(v);
    00283     SetVScrollCount(this, this->list->num_lines);
    00284
    00285     switch (v->type) {
    00286       case VEH_TRAIN:
    00287         this->widget[3].tooltips = STR_RAIL_SELECT_TYPE_OF_CARGO_FOR;
    00288         this->widget[6].data     = STR_RAIL_REFIT_VEHICLE;
    00289         this->widget[6].tooltips = STR_RAIL_REFIT_TO_CARRY_HIGHLIGHTED;
    00290         break;
    00291
    00292       case VEH_ROAD:
    00293         this->widget[3].tooltips = STR_ROAD_SELECT_TYPE_OF_CARGO_FOR;
    00294         this->widget[6].data     = STR_REFIT_ROAD_VEHICLE;
    00295         this->widget[6].tooltips = STR_REFIT_ROAD_VEHICLE_TO_CARRY_HIGHLIGHTED;
    00296         break;
    00297
    00298       case VEH_SHIP:
    00299         this->widget[3].tooltips = STR_983D_SELECT_TYPE_OF_CARGO_FOR;
    00300         this->widget[6].data     = STR_983C_REFIT_SHIP;
    00301         this->widget[6].tooltips = STR_983E_REFIT_SHIP_TO_CARRY_HIGHLIGHTED;
    00302         break;
    00303
    00304       case VEH_AIRCRAFT:
    00305         this->widget[3].tooltips = STR_A03E_SELECT_TYPE_OF_CARGO_FOR;
    00306         this->widget[6].data     = STR_A03D_REFIT_AIRCRAFT;
    00307         this->widget[6].tooltips = STR_A03F_REFIT_AIRCRAFT_TO_CARRY;
    00308         break;
    00309
    00310       default: NOT_REACHED();
    00311     }
    00312
    00313     this->FindWindowPlacementAndResize(desc);
    00314   }
    00315
    00316   ~RefitWindow()
    00317   {
    00318     free(this->list->items);
    00319     free(this->list);
    00320   }
    00321
    00322   virtual void OnPaint()
    00323   {
    00324     Vehicle *v = GetVehicle(this->window_number);
    00325
    00326     if (v->type == VEH_TRAIN) {
    00327       uint length = CountVehiclesInChain(v);
    00328
    00329       if (length != this->length) {
    00330         /* Consist length has changed, so rebuild the refit list */
    00331         free(this->list->items);
    00332         free(this->list);
    00333         this->list = BuildRefitList(v);
    00334         this->length = length;
    00335       }
    00336     }
    00337
    00338     SetVScrollCount(this, this->list->num_lines);
    00339
    00340     SetDParam(0, v->index);
    00341     this->DrawWidgets();
    00342
    00343     this->cargo = DrawVehicleRefitWindow(this->list, this->sel, this->vscroll.pos, this->vscroll.cap, this->resize.step_height);
    00344
    00345     if (this->cargo != NULL) {
    00346       CommandCost cost;
    00347
    00348       cost = DoCommand(v->tile, v->index, this->cargo->cargo | this->cargo->subtype << 8,
    00349                DC_QUERY_COST, GetCmdRefitVeh(v->type));
    00350
    00351       if (CmdSucceeded(cost)) {
    00352         SetDParam(0, this->cargo->cargo);
    00353         SetDParam(1, _returned_refit_capacity);
    00354         SetDParam(2, cost.GetCost());
    00355         DrawString(2, this->widget[5].top + 1, STR_9840_NEW_CAPACITY_COST_OF_REFIT, TC_FROMSTRING);
    00356       }
    00357     }
    00358   }
    00359
    00360   virtual void OnClick(Point pt, int widget)
    00361   {
    00362     switch (widget) {
    00363       case 3: { // listbox
    00364         int y = pt.y - this->widget[3].top;
    00365         if (y >= 0) {
    00366           this->sel = (y / (int)this->resize.step_height) + this->vscroll.pos;
    00367           this->SetDirty();
    00368         }
    00369         break;
    00370       }
    00371
    00372       case 6: // refit button
    00373         if (this->cargo != NULL) {
    00374           const Vehicle *v = GetVehicle(this->window_number);
    00375
    00376           if (this->order == INVALID_VEH_ORDER_ID) {
    00377             int command = 0;
    00378
    00379             switch (v->type) {
    00380               default: NOT_REACHED();
    00381               case VEH_TRAIN:    command = CMD_REFIT_RAIL_VEHICLE | CMD_MSG(STR_RAIL_CAN_T_REFIT_VEHICLE);  break;
    00382               case VEH_ROAD:     command = CMD_REFIT_ROAD_VEH     | CMD_MSG(STR_REFIT_ROAD_VEHICLE_CAN_T);  break;
    00383               case VEH_SHIP:     command = CMD_REFIT_SHIP         | CMD_MSG(STR_9841_CAN_T_REFIT_SHIP);     break;
    00384               case VEH_AIRCRAFT: command = CMD_REFIT_AIRCRAFT     | CMD_MSG(STR_A042_CAN_T_REFIT_AIRCRAFT); break;
    00385             }
    00386             if (DoCommandP(v->tile, v->index, this->cargo->cargo | this->cargo->subtype << 8, command)) delete this;
    00387           } else {
    00388             if (DoCommandP(v->tile, v->index, this->cargo->cargo | this->cargo->subtype << 8 | this->order << 16, CMD_ORDER_REFIT)) delete this;
    00389           }
    00390         }
    00391         break;
    00392     }
    00393   }
    00394
    00395   virtual void OnResize(Point new_size, Point delta)
    00396   {
    00397     this->vscroll.cap += delta.y / (int)this->resize.step_height;
    00398     this->widget[3].data = (this->vscroll.cap << 8) + 1;
    00399   }
    00400 };
    00401
    00402
    00403 static const Widget _vehicle_refit_widgets[] = {
    00404   {   WWT_CLOSEBOX,   RESIZE_NONE,  COLOUR_GREY,     0,    10,     0,    13, STR_00C5,                            STR_018B_CLOSE_WINDOW},
    00405   {    WWT_CAPTION,   RESIZE_NONE,  COLOUR_GREY,    11,   239,     0,    13, STR_983B_REFIT,                      STR_018C_WINDOW_TITLE_DRAG_THIS},
    00406   {    WWT_TEXTBTN,   RESIZE_NONE,  COLOUR_GREY,     0,   239,    14,    27, STR_983F_SELECT_CARGO_TYPE_TO_CARRY, STR_983D_SELECT_TYPE_OF_CARGO_FOR},
    00407   {     WWT_MATRIX, RESIZE_BOTTOM,  COLOUR_GREY,     0,   227,    28,   139, 0x801,                               STR_EMPTY},
    00408   {  WWT_SCROLLBAR, RESIZE_BOTTOM,  COLOUR_GREY,   228,   239,    28,   139, 0x0,                                 STR_0190_SCROLL_BAR_SCROLLS_LIST},
    00409   {      WWT_PANEL,     RESIZE_TB,  COLOUR_GREY,     0,   239,   140,   161, 0x0,                                 STR_NULL},
    00410   { WWT_PUSHTXTBTN,     RESIZE_TB,  COLOUR_GREY,     0,   227,   162,   173, 0x0,                                 STR_NULL},
    00411   {  WWT_RESIZEBOX,     RESIZE_TB,  COLOUR_GREY,   228,   239,   162,   173, 0x0,                                 STR_RESIZE_BUTTON},
    00412   {   WIDGETS_END},
    00413 };
    00414
    00415 static const WindowDesc _vehicle_refit_desc = {
    00416   WDP_AUTO, WDP_AUTO, 240, 174, 240, 174,
    00417   WC_VEHICLE_REFIT, WC_VEHICLE_VIEW,
    00418   WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS | WDF_RESIZABLE | WDF_CONSTRUCTION,
    00419   _vehicle_refit_widgets,
    00420 };
    00421
    00426 void ShowVehicleRefitWindow(const Vehicle *v, VehicleOrderID order, Window *parent)
    00427 {
    00428   DeleteWindowById(WC_VEHICLE_REFIT, v->index);
    00429   RefitWindow *w = new RefitWindow(&_vehicle_refit_desc, v, order);
    00430   w->parent = parent;
    00431 }
    00432
    00434 uint ShowAdditionalText(int x, int y, uint w, EngineID engine)
    00435 {
    00436   uint16 callback = GetVehicleCallback(CBID_VEHICLE_ADDITIONAL_TEXT, 0, 0, engine, NULL);
    00437   if (callback == CALLBACK_FAILED) return 0;
    00438
    00439   /* STR_02BD is used to start the string with {BLACK} */
    00440   SetDParam(0, GetGRFStringID(GetEngineGRFID(engine), 0xD000 + callback));
    00441   PrepareTextRefStackUsage(0);
    00442   uint result = DrawStringMultiLine(x, y, STR_02BD, w);
    00443   StopTextRefStackUsage();
    00444   return result;
    00445 }
    00446
    00448 uint ShowRefitOptionsList(int x, int y, uint w, EngineID engine)
    00449 {
    00450   /* List of cargo types of this engine */
    00451   uint32 cmask = GetUnionOfArticulatedRefitMasks(engine, GetEngine(engine)->type, false);
    00452   /* List of cargo types available in this climate */
    00453   uint32 lmask = _cargo_mask;
    00454   char string[512];
    00455   char *b = string;
    00456
    00457   /* Draw nothing if the engine is not refittable */
    00458   if (CountBits(cmask) <= 1) return 0;
    00459
    00460   b = InlineString(b, STR_PURCHASE_INFO_REFITTABLE_TO);
    00461
    00462   if (cmask == lmask) {
    00463     /* Engine can be refitted to all types in this climate */
    00464     b = InlineString(b, STR_PURCHASE_INFO_ALL_TYPES);
    00465   } else {
    00466     /* Check if we are able to refit to more cargo types and unable to. If
    00467      * so, invert the cargo types to list those that we can't refit to. */
    00468     if (CountBits(cmask ^ lmask) < CountBits(cmask)) {
    00469       cmask ^= lmask;
    00470       b = InlineString(b, STR_PURCHASE_INFO_ALL_BUT);
    00471     }
    00472
    00473     bool first = true;
    00474
    00475     /* Add each cargo type to the list */
    00476     for (CargoID cid = 0; cid < NUM_CARGO; cid++) {
    00477       if (!HasBit(cmask, cid)) continue;
    00478
    00479       if (b >= lastof(string) - (2 + 2 * 4)) break; // ", " and two calls to Utf8Encode()
    00480
    00481       if (!first) b = strecpy(b, ", ", lastof(string));
    00482       first = false;
    00483
    00484       b = InlineString(b, GetCargo(cid)->name);
    00485     }
    00486   }
    00487
    00488   /* Terminate and display the completed string */
    00489   *b = '\0';
    00490
    00491   /* Make sure we detect any buffer overflow */
    00492   assert(b < endof(string));
    00493
    00494   SetDParamStr(0, string);
    00495   return DrawStringMultiLine(x, y, STR_JUST_RAW_STRING, w);
    00496 }
    00497
    00499 StringID GetCargoSubtypeText(const Vehicle *v)
    00500 {
    00501   if (HasBit(EngInfo(v->engine_type)->callbackmask, CBM_VEHICLE_CARGO_SUFFIX)) {
    00502     uint16 cb = GetVehicleCallback(CBID_VEHICLE_CARGO_SUFFIX, 0, 0, v->engine_type, v);
    00503     if (cb != CALLBACK_FAILED) {
    00504       return GetGRFStringID(GetEngineGRFID(v->engine_type), 0xD000 + cb);
    00505     }
    00506   }
    00507   return STR_EMPTY;
    00508 }
    00509
    00511 static int CDECL VehicleNumberSorter(const Vehicle * const *a, const Vehicle * const *b)
    00512 {
    00513   return (*a)->unitnumber - (*b)->unitnumber;
    00514 }
    00515
    00517 static int CDECL VehicleNameSorter(const Vehicle * const *a, const Vehicle * const *b)
    00518 {
    00519   static char last_name[2][64];
    00520
    00521   if (*a != _last_vehicle[0]) {
    00522     _last_vehicle[0] = *a;
    00523     SetDParam(0, (*a)->index);
    00524     GetString(last_name[0], STR_VEHICLE_NAME, lastof(last_name[0]));
    00525   }
    00526
    00527   if (*b != _last_vehicle[1]) {
    00528     _last_vehicle[1] = *b;
    00529     SetDParam(0, (*b)->index);
    00530     GetString(last_name[1], STR_VEHICLE_NAME, lastof(last_name[1]));
    00531   }
    00532
    00533   int r = strcmp(last_name[0], last_name[1]);
    00534   return (r != 0) ? r : VehicleNumberSorter(a, b);
    00535 }
    00536
    00538 static int CDECL VehicleAgeSorter(const Vehicle * const *a, const Vehicle * const *b)
    00539 {
    00540   int r = (*a)->age - (*b)->age;
    00541   return (r != 0) ? r : VehicleNumberSorter(a, b);
    00542 }
    00543
    00545 static int CDECL VehicleProfitThisYearSorter(const Vehicle * const *a, const Vehicle * const *b)
    00546 {
    00547   int r = ClampToI32((*a)->GetDisplayProfitThisYear() - (*b)->GetDisplayProfitThisYear());
    00548   return (r != 0) ? r : VehicleNumberSorter(a, b);
    00549 }
    00550
    00552 static int CDECL VehicleProfitLastYearSorter(const Vehicle * const *a, const Vehicle * const *b)
    00553 {
    00554   int r = ClampToI32((*a)->GetDisplayProfitLastYear() - (*b)->GetDisplayProfitLastYear());
    00555   return (r != 0) ? r : VehicleNumberSorter(a, b);
    00556 }
    00557
    00559 static int CDECL VehicleCargoSorter(const Vehicle * const *a, const Vehicle * const *b)
    00560 {
    00561   const Vehicle *v;
    00562   AcceptedCargo diff;
    00563   memset(diff, 0, sizeof(diff));
    00564
    00565   /* Append the cargo of the connected weagons */
    00566   for (v = *a; v != NULL; v = v->Next()) diff[v->cargo_type] += v->cargo_cap;
    00567   for (v = *b; v != NULL; v = v->Next()) diff[v->cargo_type] -= v->cargo_cap;
    00568
    00569   int r = 0;
    00570   for (CargoID i = 0; i < NUM_CARGO; i++) {
    00571     r = diff[i];
    00572     if (r != 0) break;
    00573   }
    00574
    00575   return (r != 0) ? r : VehicleNumberSorter(a, b);
    00576 }
    00577
    00579 static int CDECL VehicleReliabilitySorter(const Vehicle * const *a, const Vehicle * const *b)
    00580 {
    00581   int r = (*a)->reliability - (*b)->reliability;
    00582   return (r != 0) ? r : VehicleNumberSorter(a, b);
    00583 }
    00584
    00586 static int CDECL VehicleMaxSpeedSorter(const Vehicle * const *a, const Vehicle * const *b)
    00587 {
    00588   int r = 0;
    00589   if ((*a)->type == VEH_TRAIN && (*b)->type == VEH_TRAIN) {
    00590     r = (*a)->u.rail.cached_max_speed - (*b)->u.rail.cached_max_speed;
    00591   } else {
    00592     r = (*a)->max_speed - (*b)->max_speed;
    00593   }
    00594   return (r != 0) ? r : VehicleNumberSorter(a, b);
    00595 }
    00596
    00598 static int CDECL VehicleModelSorter(const Vehicle * const *a, const Vehicle * const *b)
    00599 {
    00600   int r = (*a)->engine_type - (*b)->engine_type;
    00601   return (r != 0) ? r : VehicleNumberSorter(a, b);
    00602 }
    00603
    00605 static int CDECL VehicleValueSorter(const Vehicle * const *a, const Vehicle * const *b)
    00606 {
    00607   const Vehicle *u;
    00608   Money diff = 0;
    00609
    00610   for (u = *a; u != NULL; u = u->Next()) diff += u->value;
    00611   for (u = *b; u != NULL; u = u->Next()) diff -= u->value;
    00612
    00613   int r = ClampToI32(diff);
    00614   return (r != 0) ? r : VehicleNumberSorter(a, b);
    00615 }
    00616
    00618 static int CDECL VehicleLengthSorter(const Vehicle * const *a, const Vehicle * const *b)
    00619 {
    00620   int r = 0;
    00621   switch ((*a)->type) {
    00622     case VEH_TRAIN:
    00623       r = (*a)->u.rail.cached_total_length - (*b)->u.rail.cached_total_length;
    00624       break;
    00625
    00626     case VEH_ROAD: {
    00627       const Vehicle *u;
    00628       for (u = *a; u != NULL; u = u->Next()) r += u->u.road.cached_veh_length;
    00629       for (u = *b; u != NULL; u = u->Next()) r -= u->u.road.cached_veh_length;
    00630     } break;
    00631
    00632     default: NOT_REACHED();
    00633   }
    00634   return (r != 0) ? r : VehicleNumberSorter(a, b);
    00635 }
    00636
    00638 static int CDECL VehicleTimeToLiveSorter(const Vehicle * const *a, const Vehicle * const *b)
    00639 {
    00640   int r = ClampToI32(((*a)->max_age - (*a)->age) - ((*b)->max_age - (*b)->age));
    00641   return (r != 0) ? r : VehicleNumberSorter(a, b);
    00642 }
    00643
    00644 void InitializeGUI()
    00645 {
    00646   MemSetT(&_sorting, 0);
    00647 }
    00648
    00655 static inline void ChangeVehicleWindow(WindowClass window_class, VehicleID from_index, VehicleID to_index)
    00656 {
    00657   Window *w = FindWindowById(window_class, from_index);
    00658   if (w != NULL) {
    00659     w->window_number = to_index;
    00660     if (w->viewport != NULL) w->viewport->follow_vehicle = to_index;
    00661     if (to_index != INVALID_VEHICLE) InvalidateThisWindowData(w, 0);
    00662   }
    00663 }
    00664
    00670 void ChangeVehicleViewWindow(VehicleID from_index, VehicleID to_index)
    00671 {
    00672   ChangeVehicleWindow(WC_VEHICLE_VIEW,      from_index, to_index);
    00673   ChangeVehicleWindow(WC_VEHICLE_ORDERS,    from_index, to_index);
    00674   ChangeVehicleWindow(WC_VEHICLE_REFIT,     from_index, to_index);
    00675   ChangeVehicleWindow(WC_VEHICLE_DETAILS,   from_index, to_index);
    00676   ChangeVehicleWindow(WC_VEHICLE_TIMETABLE, from_index, to_index);
    00677 }
    00678
    00679 enum VehicleListWindowWidgets {
    00680   VLW_WIDGET_CLOSEBOX = 0,
    00681   VLW_WIDGET_CAPTION,
    00682   VLW_WIDGET_STICKY,
    00683   VLW_WIDGET_SORT_ORDER,
    00684   VLW_WIDGET_SORT_BY_PULLDOWN,
    00685   VLW_WIDGET_EMPTY_TOP_RIGHT,
    00686   VLW_WIDGET_LIST,
    00687   VLW_WIDGET_SCROLLBAR,
    00688   VLW_WIDGET_OTHER_COMPANY_FILLER,
    00689   VLW_WIDGET_AVAILABLE_VEHICLES,
    00690   VLW_WIDGET_MANAGE_VEHICLES_DROPDOWN,
    00691   VLW_WIDGET_STOP_ALL,
    00692   VLW_WIDGET_START_ALL,
    00693   VLW_WIDGET_EMPTY_BOTTOM_RIGHT,
    00694   VLW_WIDGET_RESIZE,
    00695 };
    00696
    00697 static const Widget _vehicle_list_widgets[] = {
    00698   {   WWT_CLOSEBOX,   RESIZE_NONE,  COLOUR_GREY,     0,    10,     0,    13, STR_00C5,             STR_018B_CLOSE_WINDOW},
    00699   {    WWT_CAPTION,  RESIZE_RIGHT,  COLOUR_GREY,    11,   247,     0,    13, 0x0,                  STR_018C_WINDOW_TITLE_DRAG_THIS},
    00700   {  WWT_STICKYBOX,     RESIZE_LR,  COLOUR_GREY,   248,   259,     0,    13, 0x0,                  STR_STICKY_BUTTON},
    00701   { WWT_PUSHTXTBTN,   RESIZE_NONE,  COLOUR_GREY,     0,    80,    14,    25, STR_SORT_BY,          STR_SORT_ORDER_TIP},
    00702   {   WWT_DROPDOWN,   RESIZE_NONE,  COLOUR_GREY,    81,   247,    14,    25, 0x0,                  STR_SORT_CRITERIA_TIP},
    00703   {      WWT_PANEL,  RESIZE_RIGHT,  COLOUR_GREY,   248,   259,    14,    25, 0x0,                  STR_NULL},
    00704   {     WWT_MATRIX,     RESIZE_RB,  COLOUR_GREY,     0,   247,    26,   181, 0x0,                  STR_NULL},
    00705   {  WWT_SCROLLBAR,    RESIZE_LRB,  COLOUR_GREY,   248,   259,    26,   181, 0x0,                  STR_0190_SCROLL_BAR_SCROLLS_LIST},
    00706   /* Widget to be shown for other companies hiding the following 6 widgets */
    00707   {      WWT_PANEL,    RESIZE_RTB,  COLOUR_GREY,     0,   247,   182,   193, 0x0,                  STR_NULL},
    00708
    00709   { WWT_PUSHTXTBTN,     RESIZE_TB,  COLOUR_GREY,     0,   105,   182,   193, 0x0,                  STR_AVAILABLE_ENGINES_TIP},
    00710   {   WWT_DROPDOWN,     RESIZE_TB,  COLOUR_GREY,   106,   223,   182,   193, STR_MANAGE_LIST,      STR_MANAGE_LIST_TIP},
    00711
    00712   { WWT_PUSHIMGBTN,     RESIZE_TB,  COLOUR_GREY,   224,   235,   182,   193, SPR_FLAG_VEH_STOPPED, STR_MASS_STOP_LIST_TIP},
    00713   { WWT_PUSHIMGBTN,     RESIZE_TB,  COLOUR_GREY,   236,   247,   182,   193, SPR_FLAG_VEH_RUNNING, STR_MASS_START_LIST_TIP},
    00714   {      WWT_PANEL,    RESIZE_RTB,  COLOUR_GREY,   248,   247,   182,   193, 0x0,                  STR_NULL},
    00715   {  WWT_RESIZEBOX,   RESIZE_LRTB,  COLOUR_GREY,   248,   259,   182,   193, 0x0,                  STR_RESIZE_BUTTON},
    00716   {   WIDGETS_END},
    00717 };
    00718
    00719 static void DrawSmallOrderList(const Vehicle *v, int x, int y)
    00720 {
    00721   const Order *order;
    00722   int sel, i = 0;
    00723
    00724   sel = v->cur_order_index;
    00725
    00726   FOR_VEHICLE_ORDERS(v, order) {
    00727     if (sel == 0) DrawString(x - 6, y, STR_SMALL_RIGHT_ARROW, TC_BLACK);
    00728     sel--;
    00729
    00730     if (order->IsType(OT_GOTO_STATION)) {
    00731       if (v->type == VEH_SHIP && GetStation(order->GetDestination())->IsBuoy()) continue;
    00732
    00733       SetDParam(0, order->GetDestination());
    00734       DrawString(x, y, STR_A036, TC_FROMSTRING);
    00735
    00736       y += 6;
    00737       if (++i == 4) break;
    00738     }
    00739   }
    00740 }
    00741
    00742 static void DrawVehicleImage(const Vehicle *v, int x, int y, VehicleID selection, int count, int skip)
    00743 {
    00744   switch (v->type) {
    00745     case VEH_TRAIN:    DrawTrainImage(v, x, y, selection, count, skip); break;
    00746     case VEH_ROAD:     DrawRoadVehImage(v, x, y, selection, count);     break;
    00747     case VEH_SHIP:     DrawShipImage(v, x, y, selection);               break;
    00748     case VEH_AIRCRAFT: DrawAircraftImage(v, x, y, selection);           break;
    00749     default: NOT_REACHED();
    00750   }
    00751 }
    00752
    00758 void BaseVehicleListWindow::DrawVehicleListItems(int x, VehicleID selected_vehicle)
    00759 {
    00760   int y = PLY_WND_PRC__OFFSET_TOP_WIDGET;
    00761   uint max = min(this->vscroll.pos + this->vscroll.cap, this->vehicles.Length());
    00762   for (uint i = this->vscroll.pos; i < max; ++i) {
    00763     const Vehicle *v = this->vehicles[i];
    00764     StringID str;
    00765
    00766     SetDParam(0, v->GetDisplayProfitThisYear());
    00767     SetDParam(1, v->GetDisplayProfitLastYear());
    00768
    00769     DrawVehicleImage(v, x + 19, y + 6, selected_vehicle, this->widget[VLW_WIDGET_LIST].right - this->widget[VLW_WIDGET_LIST].left - 20, 0);
    00770     DrawString(x + 19, y + this->resize.step_height - 8, STR_0198_PROFIT_THIS_YEAR_LAST_YEAR, TC_FROMSTRING);
    00771
    00772     if (v->name != NULL) {
    00773       /* The vehicle got a name so we will print it */
    00774       SetDParam(0, v->index);
    00775       DrawString(x + 19, y, STR_01AB, TC_FROMSTRING);
    00776     } else if (v->group_id != DEFAULT_GROUP) {
    00777       /* The vehicle has no name, but is member of a group, so print group name */
    00778       SetDParam(0, v->group_id);
    00779       DrawString(x + 19, y, STR_GROUP_TINY_NAME, TC_BLACK);
    00780     }
    00781
    00782     if (this->resize.step_height == PLY_WND_PRC__SIZE_OF_ROW_BIG) DrawSmallOrderList(v, x + 138, y);
    00783
    00784     if (v->IsInDepot()) {
    00785       str = STR_021F;
    00786     } else {
    00787       str = (v->age > v->max_age - DAYS_IN_LEAP_YEAR) ? STR_00E3 : STR_00E2;
    00788     }
    00789
    00790     SetDParam(0, v->unitnumber);
    00791     DrawString(x, y + 2, str, TC_FROMSTRING);
    00792
    00793     DrawVehicleProfitButton(v, x, y + 13);
    00794
    00795     y += this->resize.step_height;
    00796   }
    00797 }
    00798
    00808 struct VehicleListWindow : public BaseVehicleListWindow {
    00809
    00810   VehicleListWindow(const WindowDesc *desc, WindowNumber window_number) : BaseVehicleListWindow(desc, window_number)
    00811   {
    00812     uint16 window_type = this->window_number & VLW_MASK;
    00813     CompanyID company = (CompanyID)GB(this->window_number, 0, 8);
    00814
    00815     this->vehicle_type = (VehicleType)GB(this->window_number, 11, 5);
    00816     this->owner = company;
    00817
    00818     /* Hide the widgets that we will not use in this window
    00819     * Some windows contains actions only fit for the owner */
    00820     if (company == _local_company) {
    00821       this->HideWidget(VLW_WIDGET_OTHER_COMPANY_FILLER);
    00822       this->SetWidgetDisabledState(VLW_WIDGET_AVAILABLE_VEHICLES, window_type != VLW_STANDARD);
    00823     } else {
    00824       this->SetWidgetsHiddenState(true,
    00825         VLW_WIDGET_AVAILABLE_VEHICLES,
    00826         VLW_WIDGET_MANAGE_VEHICLES_DROPDOWN,
    00827         VLW_WIDGET_STOP_ALL,
    00828         VLW_WIDGET_START_ALL,
    00829         VLW_WIDGET_EMPTY_BOTTOM_RIGHT,
    00830         WIDGET_LIST_END);
    00831     }
    00832
    00833     /* Set up the window widgets */
    00834     switch (this->vehicle_type) {
    00835       case VEH_TRAIN:
    00836         this->widget[VLW_WIDGET_LIST].tooltips          = STR_883D_TRAINS_CLICK_ON_TRAIN_FOR;
    00837         this->widget[VLW_WIDGET_AVAILABLE_VEHICLES].data = STR_AVAILABLE_TRAINS;
    00838         break;
    00839
    00840       case VEH_ROAD:
    00841         this->widget[VLW_WIDGET_LIST].tooltips          = STR_901A_ROAD_VEHICLES_CLICK_ON;
    00842         this->widget[VLW_WIDGET_AVAILABLE_VEHICLES].data = STR_AVAILABLE_ROAD_VEHICLES;
    00843         break;
    00844
    00845       case VEH_SHIP:
    00846         this->widget[VLW_WIDGET_LIST].tooltips          = STR_9823_SHIPS_CLICK_ON_SHIP_FOR;
    00847         this->widget[VLW_WIDGET_AVAILABLE_VEHICLES].data = STR_AVAILABLE_SHIPS;
    00848         break;
    00849
    00850       case VEH_AIRCRAFT:
    00851         this->widget[VLW_WIDGET_LIST].tooltips          = STR_A01F_AIRCRAFT_CLICK_ON_AIRCRAFT;
    00852         this->widget[VLW_WIDGET_AVAILABLE_VEHICLES].data = STR_AVAILABLE_AIRCRAFT;
    00853         break;
    00854
    00855       default: NOT_REACHED();
    00856     }
    00857
    00858     switch (window_type) {
    00859       case VLW_SHARED_ORDERS:
    00860         this->widget[VLW_WIDGET_CAPTION].data  = STR_VEH_WITH_SHARED_ORDERS_LIST;
    00861         break;
    00862
    00863       case VLW_STANDARD: /* Company Name - standard widget setup */
    00864         switch (this->vehicle_type) {
    00865           case VEH_TRAIN:    this->widget[VLW_WIDGET_CAPTION].data = STR_881B_TRAINS;        break;
    00866           case VEH_ROAD:     this->widget[VLW_WIDGET_CAPTION].data = STR_9001_ROAD_VEHICLES; break;
    00867           case VEH_SHIP:     this->widget[VLW_WIDGET_CAPTION].data = STR_9805_SHIPS;         break;
    00868           case VEH_AIRCRAFT: this->widget[VLW_WIDGET_CAPTION].data = STR_A009_AIRCRAFT;      break;
    00869           default: NOT_REACHED(); break;
    00870         }
    00871         break;
    00872
    00873       case VLW_WAYPOINT_LIST:
    00874         this->widget[VLW_WIDGET_CAPTION].data = STR_WAYPOINT_VIEWPORT_LIST;
    00875         break;
    00876
    00877       case VLW_STATION_LIST: /* Station Name */
    00878         switch (this->vehicle_type) {
    00879           case VEH_TRAIN:    this->widget[VLW_WIDGET_CAPTION].data = STR_SCHEDULED_TRAINS;        break;
    00880           case VEH_ROAD:     this->widget[VLW_WIDGET_CAPTION].data = STR_SCHEDULED_ROAD_VEHICLES; break;
    00881           case VEH_SHIP:     this->widget[VLW_WIDGET_CAPTION].data = STR_SCHEDULED_SHIPS;         break;
    00882           case VEH_AIRCRAFT: this->widget[VLW_WIDGET_CAPTION].data = STR_SCHEDULED_AIRCRAFT;      break;
    00883           default: NOT_REACHED(); break;
    00884         }
    00885         break;
    00886
    00887       case VLW_DEPOT_LIST:
    00888         switch (this->vehicle_type) {
    00889           case VEH_TRAIN:    this->widget[VLW_WIDGET_CAPTION].data = STR_VEHICLE_LIST_TRAIN_DEPOT;    break;
    00890           case VEH_ROAD:     this->widget[VLW_WIDGET_CAPTION].data = STR_VEHICLE_LIST_ROADVEH_DEPOT;  break;
    00891           case VEH_SHIP:     this->widget[VLW_WIDGET_CAPTION].data = STR_VEHICLE_LIST_SHIP_DEPOT;     break;
    00892           case VEH_AIRCRAFT: this->widget[VLW_WIDGET_CAPTION].data = STR_VEHICLE_LIST_AIRCRAFT_DEPOT; break;
    00893           default: NOT_REACHED(); break;
    00894         }
    00895         break;
    00896       default: NOT_REACHED(); break;
    00897     }
    00898
    00899     switch (this->vehicle_type) {
    00900       case VEH_TRAIN:
    00901         this->resize.step_width = 1;
    00902         /* Fallthrough */
    00903       case VEH_ROAD:
    00904         this->vscroll.cap = 6;
    00905         this->resize.step_height = PLY_WND_PRC__SIZE_OF_ROW_SMALL;
    00906         break;
    00907       case VEH_SHIP:
    00908       case VEH_AIRCRAFT:
    00909         this->vscroll.cap = 4;
    00910         this->resize.step_height = PLY_WND_PRC__SIZE_OF_ROW_BIG;
    00911         break;
    00912       default: NOT_REACHED();
    00913     }
    00914
    00915
    00916     this->widget[VLW_WIDGET_LIST].data = (this->vscroll.cap << 8) + 1;
    00917
    00918     /* Set up sorting. Make the window-specific _sorting variable
    00919       * point to the correct global _sorting struct so we are freed
    00920       * from having conditionals during window operation */
    00921     switch (this->vehicle_type) {
    00922       case VEH_TRAIN:    this->sorting = &_sorting.train; break;
    00923       case VEH_ROAD:     this->sorting = &_sorting.roadveh; break;
    00924       case VEH_SHIP:     this->sorting = &_sorting.ship; break;
    00925       case VEH_AIRCRAFT: this->sorting = &_sorting.aircraft; break;
    00926       default: NOT_REACHED(); break;
    00927     }
    00928
    00929     this->vehicles.SetListing(*this->sorting);
    00930     this->vehicles.ForceRebuild();
    00931     this->vehicles.NeedResort();
    00932
    00933     this->FindWindowPlacementAndResize(desc);
    00934     if (this->vehicle_type == VEH_TRAIN) ResizeWindow(this, 65, 0);
    00935   }
    00936
    00937   ~VehicleListWindow()
    00938   {
    00939     *this->sorting = this->vehicles.GetListing();
    00940   }
    00941
    00942   virtual void OnPaint()
    00943   {
    00944     int x = 2;
    00945     const Owner owner = this->owner;
    00946     const uint16 window_type = this->window_number & VLW_MASK;
    00947     const uint16 index = GB(this->window_number, 16, 16);
    00948
    00949     this->BuildVehicleList(owner, index, window_type);
    00950     this->SortVehicleList();
    00951     SetVScrollCount(this, this->vehicles.Length());
    00952
    00953     if (this->vehicles.Length() == 0) HideDropDownMenu(this);
    00954
    00955     /* draw the widgets */
    00956     switch (window_type) {
    00957       case VLW_SHARED_ORDERS: /* Shared Orders */
    00958         if (this->vehicles.Length() == 0) {
    00959           /* We can't open this window without vehicles using this order
    00960           * and we should close the window when deleting the order      */
    00961           NOT_REACHED();
    00962         }
    00963         SetDParam(0, this->vscroll.count);
    00964         break;
    00965
    00966       case VLW_STANDARD: /* Company Name */
    00967         SetDParam(0, owner);
    00968         SetDParam(1, this->vscroll.count);
    00969         break;
    00970
    00971       case VLW_WAYPOINT_LIST:
    00972         SetDParam(0, index);
    00973         SetDParam(1, this->vscroll.count);
    00974         break;
    00975
    00976       case VLW_STATION_LIST: /* Station Name */
    00977         SetDParam(0, index);
    00978         SetDParam(1, this->vscroll.count);
    00979         break;
    00980
    00981       case VLW_DEPOT_LIST:
    00982         switch (this->vehicle_type) {
    00983           case VEH_TRAIN:    SetDParam(0, STR_8800_TRAIN_DEPOT);        break;
    00984           case VEH_ROAD:     SetDParam(0, STR_9003_ROAD_VEHICLE_DEPOT); break;
    00985           case VEH_SHIP:     SetDParam(0, STR_9803_SHIP_DEPOT);         break;
    00986           case VEH_AIRCRAFT: SetDParam(0, STR_A002_AIRCRAFT_HANGAR);    break;
    00987           default: NOT_REACHED(); break;
    00988         }
    00989         if (this->vehicle_type == VEH_AIRCRAFT) {
    00990           SetDParam(1, index); // Airport name
    00991         } else {
    00992           SetDParam(1, GetDepot(index)->town_index);
    00993         }
    00994         SetDParam(2, this->vscroll.count);
    00995         break;
    00996       default: NOT_REACHED(); break;
    00997     }
    00998
    00999     this->SetWidgetsDisabledState(this->vehicles.Length() == 0,
    01000       VLW_WIDGET_MANAGE_VEHICLES_DROPDOWN,
    01001       VLW_WIDGET_STOP_ALL,
    01002       VLW_WIDGET_START_ALL,
    01003       WIDGET_LIST_END);
    01004
    01005     this->DrawWidgets();
    01006
    01007     /* draw sorting criteria string */
    01008     DrawString(85, 15, this->vehicle_sorter_names[this->vehicles.SortType()], TC_BLACK);
    01009     /* draw arrow pointing up/down for ascending/descending sorting */
    01010     this->DrawSortButtonState(VLW_WIDGET_SORT_ORDER, this->vehicles.IsDescSortOrder() ? SBS_DOWN : SBS_UP);
    01011
    01012     this->DrawVehicleListItems(x,  INVALID_VEHICLE);
    01013   }
    01014
    01015   virtual void OnClick(Point pt, int widget)
    01016   {
    01017     switch (widget) {
    01018       case VLW_WIDGET_SORT_ORDER: /* Flip sorting method ascending/descending */
    01019         this->vehicles.ToggleSortOrder();
    01020         this->SetDirty();
    01021         break;
    01022       case VLW_WIDGET_SORT_BY_PULLDOWN:/* Select sorting criteria dropdown menu */
    01023         ShowDropDownMenu(this, this->vehicle_sorter_names, this->vehicles.SortType(), VLW_WIDGET_SORT_BY_PULLDOWN, 0, (this->vehicle_type == VEH_TRAIN || this->vehicle_type == VEH_ROAD) ? 0 : (1 << 10));
    01024         return;
    01025       case VLW_WIDGET_LIST: { /* Matrix to show vehicles */
    01026         uint32 id_v = (pt.y - PLY_WND_PRC__OFFSET_TOP_WIDGET) / this->resize.step_height;
    01027         const Vehicle *v;
    01028
    01029         if (id_v >= this->vscroll.cap) return; // click out of bounds
    01030
    01031         id_v += this->vscroll.pos;
    01032
    01033         if (id_v >= this->vehicles.Length()) return; // click out of list bound
    01034
    01035         v = this->vehicles[id_v];
    01036
    01037         ShowVehicleViewWindow(v);
    01038       } break;
    01039
    01040       case VLW_WIDGET_AVAILABLE_VEHICLES:
    01041         ShowBuildVehicleWindow(INVALID_TILE, this->vehicle_type);
    01042         break;
    01043
    01044       case VLW_WIDGET_MANAGE_VEHICLES_DROPDOWN: {
    01045         static StringID action_str[] = {
    01046           STR_REPLACE_VEHICLES,
    01047           STR_SEND_FOR_SERVICING,
    01048           STR_NULL,
    01049           INVALID_STRING_ID
    01050         };
    01051
    01052         static const StringID depot_name[] = {
    01053           STR_SEND_TRAIN_TO_DEPOT,
    01054           STR_SEND_ROAD_VEHICLE_TO_DEPOT,
    01055           STR_SEND_SHIP_TO_DEPOT,
    01056           STR_SEND_AIRCRAFT_TO_HANGAR
    01057         };
    01058
    01059         /* XXX - Substite string since the dropdown cannot handle dynamic strings */
    01060         action_str[2] = depot_name[this->vehicle_type];
    01061         ShowDropDownMenu(this, action_str, 0, VLW_WIDGET_MANAGE_VEHICLES_DROPDOWN, 0, (this->window_number & VLW_MASK) == VLW_STANDARD ? 0 : 1);
    01062         break;
    01063       }
    01064
    01065       case VLW_WIDGET_STOP_ALL:
    01066       case VLW_WIDGET_START_ALL:
    01067         DoCommandP(0, GB(this->window_number, 16, 16), (this->window_number & VLW_MASK) | (1 << 6) | (widget == VLW_WIDGET_START_ALL ? (1 << 5) : 0) | this->vehicle_type, CMD_MASS_START_STOP);
    01068         break;
    01069     }
    01070   }
    01071
    01072   virtual void OnDropdownSelect(int widget, int index)
    01073   {
    01074     switch (widget) {
    01075       case VLW_WIDGET_SORT_BY_PULLDOWN:
    01076         this->vehicles.SetSortType(index);
    01077         break;
    01078       case VLW_WIDGET_MANAGE_VEHICLES_DROPDOWN:
    01079         assert(this->vehicles.Length() != 0);
    01080
    01081         switch (index) {
    01082           case 0: /* Replace window */
    01083             ShowReplaceGroupVehicleWindow(DEFAULT_GROUP, this->vehicle_type);
    01084             break;
    01085           case 1: /* Send for servicing */
    01086             DoCommandP(0, GB(this->window_number, 16, 16) /* StationID or OrderID (depending on VLW) */,
    01087               (this->window_number & VLW_MASK) | DEPOT_MASS_SEND | DEPOT_SERVICE,
    01088               GetCmdSendToDepot(this->vehicle_type));
    01089             break;
    01090           case 2: /* Send to Depots */
    01091             DoCommandP(0, GB(this->window_number, 16, 16) /* StationID or OrderID (depending on VLW) */,
    01092               (this->window_number & VLW_MASK) | DEPOT_MASS_SEND,
    01093               GetCmdSendToDepot(this->vehicle_type));
    01094             break;
    01095
    01096           default: NOT_REACHED();
    01097         }
    01098         break;
    01099       default: NOT_REACHED();
    01100     }
    01101     this->SetDirty();
    01102   }
    01103
    01104   virtual void OnTick()
    01105   {
    01106     if (_pause_game != 0) return;
    01107     if (this->vehicles.NeedResort()) {
    01108       StationID station = ((this->window_number & VLW_MASK) == VLW_STATION_LIST) ? GB(this->window_number, 16, 16) : INVALID_STATION;
    01109
    01110       DEBUG(misc, 3, "Periodic resort %d list company %d at station %d", this->vehicle_type, this->owner, station);
    01111       this->SetDirty();
    01112     }
    01113   }
    01114
    01115   virtual void OnResize(Point new_size, Point delta)
    01116   {
    01117     this->vscroll.cap += delta.y / (int)this->resize.step_height;
    01118     this->widget[VLW_WIDGET_LIST].data = (this->vscroll.cap << 8) + 1;
    01119   }
    01120
    01121   virtual void OnInvalidateData(int data)
    01122   {
    01123     if (HasBit(data, 15) && (this->window_number & VLW_MASK) == VLW_SHARED_ORDERS) {
    01124       SB(this->window_number, 16, 16, GB(data, 16, 16));
    01125       this->vehicles.ForceRebuild();
    01126       return;
    01127     }
    01128
    01129     if (data == 0) {
    01130       this->vehicles.ForceRebuild();
    01131     } else {
    01132       this->vehicles.ForceResort();
    01133     }
    01134   }
    01135 };
    01136
    01137 static WindowDesc _vehicle_list_desc = {
    01138   WDP_AUTO, WDP_AUTO, 260, 194, 260, 246,
    01139   WC_INVALID, WC_NONE,
    01140   WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS | WDF_STICKY_BUTTON | WDF_RESIZABLE,
    01141   _vehicle_list_widgets,
    01142 };
    01143
    01144 static void ShowVehicleListWindowLocal(CompanyID company, uint16 VLW_flag, VehicleType vehicle_type, uint16 unique_number)
    01145 {
    01146   if (!IsValidCompanyID(company)) return;
    01147
    01148   _vehicle_list_desc.cls = GetWindowClassForVehicleType(vehicle_type);
    01149   WindowNumber num = (unique_number << 16) | (vehicle_type << 11) | VLW_flag | company;
    01150   AllocateWindowDescFront<VehicleListWindow>(&_vehicle_list_desc, num);
    01151 }
    01152
    01153 void ShowVehicleListWindow(CompanyID company, VehicleType vehicle_type)
    01154 {
    01155   /* If _settings_client.gui.advanced_vehicle_list > 1, display the Advanced list
    01156    * if _settings_client.gui.advanced_vehicle_list == 1, display Advanced list only for local company
    01157    * if _ctrl_pressed, do the opposite action (Advanced list x Normal list)
    01158    */
    01159
    01160   if ((_settings_client.gui.advanced_vehicle_list > (uint)(company != _local_company)) != _ctrl_pressed) {
    01161     ShowCompanyGroup(company, vehicle_type);
    01162   } else {
    01163     ShowVehicleListWindowLocal(company, VLW_STANDARD, vehicle_type, 0);
    01164   }
    01165 }
    01166
    01167 void ShowVehicleListWindow(const Waypoint *wp)
    01168 {
    01169   if (wp == NULL) return;
    01170   ShowVehicleListWindowLocal(wp->owner, VLW_WAYPOINT_LIST, VEH_TRAIN, wp->index);
    01171 }
    01172
    01173 void ShowVehicleListWindow(const Vehicle *v)
    01174 {
    01175   ShowVehicleListWindowLocal(v->owner, VLW_SHARED_ORDERS, v->type, v->FirstShared()->index);
    01176 }
    01177
    01178 void ShowVehicleListWindow(CompanyID company, VehicleType vehicle_type, StationID station)
    01179 {
    01180   ShowVehicleListWindowLocal(company, VLW_STATION_LIST, vehicle_type, station);
    01181 }
    01182
    01183 void ShowVehicleListWindow(CompanyID company, VehicleType vehicle_type, TileIndex depot_tile)
    01184 {
    01185   uint16 depot_airport_index;
    01186
    01187   if (vehicle_type == VEH_AIRCRAFT) {
    01188     depot_airport_index = GetStationIndex(depot_tile);
    01189   } else {
    01190     Depot *depot = GetDepotByTile(depot_tile);
    01191     if (depot == NULL) return; // no depot to show
    01192     depot_airport_index = depot->index;
    01193   }
    01194   ShowVehicleListWindowLocal(company, VLW_DEPOT_LIST, vehicle_type, depot_airport_index);
    01195 }
    01196
    01197
    01198 /* Unified vehicle GUI - Vehicle Details Window */
    01199
    01201 enum VehicleDetailsWindowWidgets {
    01202   VLD_WIDGET_CLOSEBOX = 0,
    01203   VLD_WIDGET_CAPTION,
    01204   VLD_WIDGET_RENAME_VEHICLE,
    01205   VLD_WIDGET_STICKY,
    01206   VLD_WIDGET_TOP_DETAILS,
    01207   VLD_WIDGET_INCREASE_SERVICING_INTERVAL,
    01208   VLD_WIDGET_DECREASE_SERVICING_INTERVAL,
    01209   VLD_WIDGET_BOTTOM_RIGHT,
    01210   VLD_WIDGET_MIDDLE_DETAILS,
    01211   VLD_WIDGET_SCROLLBAR,
    01212   VLD_WIDGET_DETAILS_CARGO_CARRIED,
    01213   VLD_WIDGET_DETAILS_TRAIN_VEHICLES,
    01214   VLD_WIDGET_DETAILS_CAPACITY_OF_EACH,
    01215   VLD_WIDGET_DETAILS_TOTAL_CARGO,
    01216   VLD_WIDGET_RESIZE,
    01217 };
    01218
    01220 static const Widget _vehicle_details_widgets[] = {
    01221   {   WWT_CLOSEBOX,   RESIZE_NONE,  COLOUR_GREY,   0,  10,   0,  13, STR_00C5,             STR_018B_CLOSE_WINDOW},                  // VLD_WIDGET_CLOSEBOX
    01222   {    WWT_CAPTION,  RESIZE_RIGHT,  COLOUR_GREY,  11, 352,   0,  13, 0x0,                  STR_018C_WINDOW_TITLE_DRAG_THIS},        // VLD_WIDGET_CAPTION
    01223   { WWT_PUSHTXTBTN,     RESIZE_LR,  COLOUR_GREY, 353, 392,   0,  13, STR_01AA_NAME,        STR_NULL /* filled in later */},         // VLD_WIDGET_RENAME_VEHICLE
    01224   {  WWT_STICKYBOX,     RESIZE_LR,  COLOUR_GREY, 393, 404,   0,  13, STR_NULL,             STR_STICKY_BUTTON},                      // VLD_WIDGET_STICKY
    01225   {      WWT_PANEL,  RESIZE_RIGHT,  COLOUR_GREY,   0, 404,  14,  55, 0x0,                  STR_NULL},                               // VLD_WIDGET_TOP_DETAILS
    01226   { WWT_PUSHTXTBTN,     RESIZE_TB,  COLOUR_GREY,   0,  10, 101, 106, STR_0188,             STR_884D_INCREASE_SERVICING_INTERVAL},   // VLD_WIDGET_INCREASE_SERVICING_INTERVAL
    01227   { WWT_PUSHTXTBTN,     RESIZE_TB,  COLOUR_GREY,   0,  10, 107, 112, STR_0189,             STR_884E_DECREASE_SERVICING_INTERVAL},   // VLD_WIDGET_DECREASE_SERVICING_INTERVAL
    01228   {      WWT_PANEL,    RESIZE_RTB,  COLOUR_GREY,  11, 404, 101, 112, 0x0,                  STR_NULL},                               // VLD_WIDGET_BOTTOM_RIGHT
    01229   {     WWT_MATRIX,     RESIZE_RB,  COLOUR_GREY,   0, 392,  56, 100, 0x701,                STR_NULL},                               // VLD_WIDGET_MIDDLE_DETAILS
    01230   {  WWT_SCROLLBAR,    RESIZE_LRB,  COLOUR_GREY, 393, 404,  56, 100, 0x0,                  STR_0190_SCROLL_BAR_SCROLLS_LIST},       // VLD_WIDGET_SCROLLBAR
    01231   { WWT_PUSHTXTBTN,     RESIZE_TB,  COLOUR_GREY,   0,  95, 113, 124, STR_013C_CARGO,       STR_884F_SHOW_DETAILS_OF_CARGO_CARRIED}, // VLD_WIDGET_DETAILS_CARGO_CARRIED
    01232   { WWT_PUSHTXTBTN,     RESIZE_TB,  COLOUR_GREY,  96, 194, 113, 124, STR_013D_INFORMATION, STR_8850_SHOW_DETAILS_OF_TRAIN_VEHICLES},// VLD_WIDGET_DETAILS_TRAIN_VEHICLES
    01233   { WWT_PUSHTXTBTN,     RESIZE_TB,  COLOUR_GREY, 195, 293, 113, 124, STR_013E_CAPACITIES,  STR_8851_SHOW_CAPACITIES_OF_EACH},       // VLD_WIDGET_DETAILS_CAPACITY_OF_EACH
    01234   { WWT_PUSHTXTBTN,    RESIZE_RTB,  COLOUR_GREY, 294, 392, 113, 124, STR_TOTAL_CARGO,      STR_SHOW_TOTAL_CARGO},                   // VLD_WIDGET_DETAILS_TOTAL_CARGO
    01235   {  WWT_RESIZEBOX,   RESIZE_LRTB,  COLOUR_GREY, 393, 404, 113, 124, 0x0,                  STR_RESIZE_BUTTON},                      // VLD_RESIZE
    01236   {   WIDGETS_END},
    01237 };
    01238
    01239
    01241 enum VehicleStringTranslation {
    01242   VST_VEHICLE_AGE_RUNNING_COST_YR,
    01243   VST_VEHICLE_MAX_SPEED,
    01244   VST_VEHICLE_PROFIT_THIS_YEAR_LAST_YEAR,
    01245   VST_VEHICLE_RELIABILITY_BREAKDOWNS,
    01246 };
    01247
    01249 static const StringID _vehicle_translation_table[][4] = {
    01250   { // VST_VEHICLE_AGE_RUNNING_COST_YR
    01251     STR_885D_AGE_RUNNING_COST_YR,
    01252     STR_900D_AGE_RUNNING_COST_YR,
    01253     STR_9812_AGE_RUNNING_COST_YR,
    01254     STR_A00D_AGE_RUNNING_COST_YR,
    01255   },
    01256   { // VST_VEHICLE_MAX_SPEED
    01257     STR_NULL,
    01258     STR_900E_MAX_SPEED,
    01259     STR_9813_MAX_SPEED,
    01260     STR_A00E_MAX_SPEED,
    01261   },
    01262   { // VST_VEHICLE_PROFIT_THIS_YEAR_LAST_YEAR
    01263     STR_885F_PROFIT_THIS_YEAR_LAST_YEAR,
    01264     STR_900F_PROFIT_THIS_YEAR_LAST_YEAR,
    01265     STR_9814_PROFIT_THIS_YEAR_LAST_YEAR,
    01266     STR_A00F_PROFIT_THIS_YEAR_LAST_YEAR,
    01267   },
    01268   { // VST_VEHICLE_RELIABILITY_BREAKDOWNS
    01269     STR_8860_RELIABILITY_BREAKDOWNS,
    01270     STR_9010_RELIABILITY_BREAKDOWNS,
    01271     STR_9815_RELIABILITY_BREAKDOWNS,
    01272     STR_A010_RELIABILITY_BREAKDOWNS,
    01273   },
    01274 };
    01275
    01276
    01277 extern int GetTrainDetailsWndVScroll(VehicleID veh_id, byte det_tab);
    01278 extern void DrawTrainDetails(const Vehicle *v, int x, int y, int vscroll_pos, uint16 vscroll_cap, byte det_tab);
    01279 extern void DrawRoadVehDetails(const Vehicle *v, int x, int y);
    01280 extern void DrawShipDetails(const Vehicle *v, int x, int y);
    01281 extern void DrawAircraftDetails(const Vehicle *v, int x, int y);
    01282
    01283 struct VehicleDetailsWindow : Window {
    01284   int tab;
    01285
    01287   VehicleDetailsWindow(const WindowDesc *desc, WindowNumber window_number) : Window(desc, window_number)
    01288   {
    01289     const Vehicle *v = GetVehicle(this->window_number);
    01290
    01291     switch (v->type) {
    01292       case VEH_TRAIN:
    01293         ResizeWindow(this, 0, 39);
    01294
    01295         this->vscroll.cap = 6;
    01296         this->height += 12;
    01297         this->resize.step_height = 14;
    01298         this->resize.height = this->height - 14 * 2; // Minimum of 4 wagons in the display
    01299
    01300         this->widget[VLD_WIDGET_RENAME_VEHICLE].tooltips = STR_8867_NAME_TRAIN;
    01301         this->widget[VLD_WIDGET_CAPTION].data = STR_8802_DETAILS;
    01302         break;
    01303
    01304       case VEH_ROAD: {
    01305         this->widget[VLD_WIDGET_CAPTION].data = STR_900C_DETAILS;
    01306         this->widget[VLD_WIDGET_RENAME_VEHICLE].tooltips = STR_902E_NAME_ROAD_VEHICLE;
    01307
    01308         if (!RoadVehHasArticPart(v)) break;
    01309
    01310         /* Draw the text under the vehicle instead of next to it, minus the
    01311          * height already allocated for the cargo of the first vehicle. */
    01312         uint height_extension = 15 - 11;
    01313
    01314         /* Add space for the cargo amount for each part. */
    01315         for (const Vehicle *u = v; u != NULL; u = u->Next()) {
    01316           if (u->cargo_cap != 0) height_extension += 11;
    01317         }
    01318
    01319         ResizeWindow(this, 0, height_extension);
    01320       } break;
    01321
    01322       case VEH_SHIP:
    01323         this->widget[VLD_WIDGET_RENAME_VEHICLE].tooltips = STR_982F_NAME_SHIP;
    01324         this->widget[VLD_WIDGET_CAPTION].data = STR_9811_DETAILS;
    01325         break;
    01326
    01327       case VEH_AIRCRAFT:
    01328         ResizeWindow(this, 0, 11);
    01329         this->widget[VLD_WIDGET_RENAME_VEHICLE].tooltips = STR_A032_NAME_AIRCRAFT;
    01330         this->widget[VLD_WIDGET_CAPTION].data = STR_A00C_DETAILS;
    01331         break;
    01332       default: NOT_REACHED();
    01333     }
    01334
    01335     if (v->type != VEH_TRAIN) {
    01336       this->vscroll.cap = 1;
    01337       this->widget[VLD_WIDGET_MIDDLE_DETAILS].right += 12;
    01338     }
    01339
    01340     this->widget[VLD_WIDGET_MIDDLE_DETAILS].data = (this->vscroll.cap << 8) + 1;
    01341     this->owner = v->owner;
    01342
    01343     this->tab = 0;
    01344
    01345     this->FindWindowPlacementAndResize(desc);
    01346   }
    01347
    01349   static bool IsVehicleServiceIntervalEnabled(const VehicleType vehicle_type)
    01350   {
    01351     switch (vehicle_type) {
    01352       default: NOT_REACHED();
    01353       case VEH_TRAIN:    return _settings_game.vehicle.servint_trains   != 0; break;
    01354       case VEH_ROAD:     return _settings_game.vehicle.servint_roadveh  != 0; break;
    01355       case VEH_SHIP:     return _settings_game.vehicle.servint_ships    != 0; break;
    01356       case VEH_AIRCRAFT: return _settings_game.vehicle.servint_aircraft != 0; break;
    01357     }
    01358     return false; // kill a compiler warning
    01359   }
    01360
    01371   static void DrawVehicleDetails(const Vehicle *v, int x, int y, int vscroll_pos, uint vscroll_cap, byte det_tab)
    01372   {
    01373     switch (v->type) {
    01374       case VEH_TRAIN:    DrawTrainDetails(v, x, y, vscroll_pos, vscroll_cap, det_tab);  break;
    01375       case VEH_ROAD:     DrawRoadVehDetails(v, x, y);  break;
    01376       case VEH_SHIP:     DrawShipDetails(v, x, y);     break;
    01377       case VEH_AIRCRAFT: DrawAircraftDetails(v, x, y); break;
    01378       default: NOT_REACHED();
    01379     }
    01380   }
    01381
    01383   virtual void OnPaint()
    01384   {
    01385     const Vehicle *v = GetVehicle(this->window_number);
    01386     byte det_tab = this->tab;
    01387
    01388     this->SetWidgetDisabledState(VLD_WIDGET_RENAME_VEHICLE, v->owner != _local_company);
    01389
    01390     if (v->type == VEH_TRAIN) {
    01391       this->DisableWidget(det_tab + VLD_WIDGET_DETAILS_CARGO_CARRIED);
    01392       SetVScrollCount(this, GetTrainDetailsWndVScroll(v->index, det_tab));
    01393     }
    01394
    01395     this->SetWidgetsHiddenState(v->type != VEH_TRAIN,
    01396       VLD_WIDGET_SCROLLBAR,
    01397       VLD_WIDGET_DETAILS_CARGO_CARRIED,
    01398       VLD_WIDGET_DETAILS_TRAIN_VEHICLES,
    01399       VLD_WIDGET_DETAILS_CAPACITY_OF_EACH,
    01400       VLD_WIDGET_DETAILS_TOTAL_CARGO,
    01401       VLD_WIDGET_RESIZE,
    01402       WIDGET_LIST_END);
    01403
    01404     /* Disable service-scroller when interval is set to disabled */
    01405     this->SetWidgetsDisabledState(!IsVehicleServiceIntervalEnabled(v->type),
    01406       VLD_WIDGET_INCREASE_SERVICING_INTERVAL,
    01407       VLD_WIDGET_DECREASE_SERVICING_INTERVAL,
    01408       WIDGET_LIST_END);
    01409
    01410
    01411     SetDParam(0, v->index);
    01412     this->DrawWidgets();
    01413
    01414     /* Draw running cost */
    01415     SetDParam(1, v->age / DAYS_IN_LEAP_YEAR);
    01416     SetDParam(0, (v->age + DAYS_IN_YEAR < v->max_age) ? STR_AGE : STR_AGE_RED);
    01417     SetDParam(2, v->max_age / DAYS_IN_LEAP_YEAR);
    01418     SetDParam(3, v->GetDisplayRunningCost());
    01419     DrawString(2, 15, _vehicle_translation_table[VST_VEHICLE_AGE_RUNNING_COST_YR][v->type], TC_FROMSTRING);
    01420
    01421     /* Draw max speed */
    01422     switch (v->type) {
    01423       case VEH_TRAIN:
    01424         SetDParam(2, v->GetDisplayMaxSpeed());
    01425         SetDParam(1, v->u.rail.cached_power);
    01426         SetDParam(0, v->u.rail.cached_weight);
    01427         SetDParam(3, v->u.rail.cached_max_te / 1000);
    01428         DrawString(2, 25, (_settings_game.vehicle.train_acceleration_model != TAM_ORIGINAL && v->u.rail.railtype != RAILTYPE_MAGLEV) ?
    01429           STR_VEHICLE_INFO_WEIGHT_POWER_MAX_SPEED_MAX_TE :
    01430           STR_VEHICLE_INFO_WEIGHT_POWER_MAX_SPEED, TC_FROMSTRING);
    01431         break;
    01432
    01433       case VEH_ROAD:
    01434       case VEH_SHIP:
    01435       case VEH_AIRCRAFT:
    01436         SetDParam(0, v->GetDisplayMaxSpeed());
    01437         DrawString(2, 25, _vehicle_translation_table[VST_VEHICLE_MAX_SPEED][v->type], TC_FROMSTRING);
    01438         break;
    01439
    01440       default: NOT_REACHED();
    01441     }
    01442
    01443     /* Draw profit */
    01444     SetDParam(0, v->GetDisplayProfitThisYear());
    01445     SetDParam(1, v->GetDisplayProfitLastYear());
    01446     DrawString(2, 35, _vehicle_translation_table[VST_VEHICLE_PROFIT_THIS_YEAR_LAST_YEAR][v->type], TC_FROMSTRING);
    01447
    01448     /* Draw breakdown & reliability */
    01449     SetDParam(0, v->reliability * 100 >> 16);
    01450     SetDParam(1, v->breakdowns_since_last_service);
    01451     DrawString(2, 45, _vehicle_translation_table[VST_VEHICLE_RELIABILITY_BREAKDOWNS][v->type], TC_FROMSTRING);
    01452
    01453     /* Draw service interval text */
    01454     SetDParam(0, v->service_interval);
    01455     SetDParam(1, v->date_of_last_service);
    01456     DrawString(13, this->height - (v->type != VEH_TRAIN ? 11 : 23), _settings_game.vehicle.servint_ispercent ? STR_SERVICING_INTERVAL_PERCENT : STR_883C_SERVICING_INTERVAL_DAYS, TC_FROMSTRING);
    01457
    01458     switch (v->type) {
    01459       case VEH_TRAIN:
    01460         DrawVehicleDetails(v, 2, 57, this->vscroll.pos, this->vscroll.cap, det_tab);
    01461         break;
    01462
    01463       case VEH_ROAD:
    01464       case VEH_SHIP:
    01465       case VEH_AIRCRAFT:
    01466         DrawVehicleImage(v, 3, 57, INVALID_VEHICLE, 0, 0);
    01467         DrawVehicleDetails(v, 75, 57, this->vscroll.pos, this->vscroll.cap, det_tab);
    01468         break;
    01469
    01470       default: NOT_REACHED();
    01471     }
    01472   }
    01473
    01474   virtual void OnClick(Point pt, int widget)
    01475   {
    01477     static const StringID _name_vehicle_title[] = {
    01478       STR_8865_NAME_TRAIN,
    01479       STR_902C_NAME_ROAD_VEHICLE,
    01480       STR_9831_NAME_SHIP,
    01481       STR_A030_NAME_AIRCRAFT
    01482     };
    01483
    01484     switch (widget) {
    01485       case VLD_WIDGET_RENAME_VEHICLE: {// rename
    01486         const Vehicle *v = GetVehicle(this->window_number);
    01487         SetDParam(0, v->index);
    01488         ShowQueryString(STR_VEHICLE_NAME, _name_vehicle_title[v->type], MAX_LENGTH_VEHICLE_NAME_BYTES, MAX_LENGTH_VEHICLE_NAME_PIXELS, this, CS_ALPHANUMERAL, QSF_ENABLE_DEFAULT);
    01489       } break;
    01490
    01491       case VLD_WIDGET_INCREASE_SERVICING_INTERVAL:   // increase int
    01492       case VLD_WIDGET_DECREASE_SERVICING_INTERVAL: { // decrease int
    01493         int mod = _ctrl_pressed ? 5 : 10;
    01494         const Vehicle *v = GetVehicle(this->window_number);
    01495
    01496         mod = (widget == VLD_WIDGET_DECREASE_SERVICING_INTERVAL) ? -mod : mod;
    01497         mod = GetServiceIntervalClamped(mod + v->service_interval);
    01498         if (mod == v->service_interval) return;
    01499
    01500         DoCommandP(v->tile, v->index, mod, CMD_CHANGE_SERVICE_INT | CMD_MSG(STR_018A_CAN_T_CHANGE_SERVICING));
    01501       } break;
    01502
    01503       case VLD_WIDGET_DETAILS_CARGO_CARRIED:
    01504       case VLD_WIDGET_DETAILS_TRAIN_VEHICLES:
    01505       case VLD_WIDGET_DETAILS_CAPACITY_OF_EACH:
    01506       case VLD_WIDGET_DETAILS_TOTAL_CARGO:
    01507         this->SetWidgetsDisabledState(false,
    01508           VLD_WIDGET_DETAILS_CARGO_CARRIED,
    01509           VLD_WIDGET_DETAILS_TRAIN_VEHICLES,
    01510           VLD_WIDGET_DETAILS_CAPACITY_OF_EACH,
    01511           VLD_WIDGET_DETAILS_TOTAL_CARGO,
    01512           widget,
    01513           WIDGET_LIST_END);
    01514
    01515         this->tab = widget - VLD_WIDGET_DETAILS_CARGO_CARRIED;
    01516         this->SetDirty();
    01517         break;
    01518     }
    01519   }
    01520
    01521   virtual void OnQueryTextFinished(char *str)
    01522   {
    01524     static const StringID _name_vehicle_error[] = {
    01525       STR_8866_CAN_T_NAME_TRAIN,
    01526       STR_902D_CAN_T_NAME_ROAD_VEHICLE,
    01527       STR_9832_CAN_T_NAME_SHIP,
    01528       STR_A031_CAN_T_NAME_AIRCRAFT
    01529     };
    01530
    01531     if (str == NULL) return;
    01532
    01533     DoCommandP(0, this->window_number, 0, CMD_RENAME_VEHICLE | CMD_MSG(_name_vehicle_error[GetVehicle(this->window_number)->type]), NULL, str);
    01534   }
    01535
    01536   virtual void OnResize(Point new_size, Point delta)
    01537   {
    01538     if (delta.x != 0) ResizeButtons(this, VLD_WIDGET_DETAILS_CARGO_CARRIED, VLD_WIDGET_DETAILS_TOTAL_CARGO);
    01539     if (delta.y == 0) return;
    01540
    01541     this->vscroll.cap += delta.y / 14;
    01542     this->widget[VLD_WIDGET_MIDDLE_DETAILS].data = (this->vscroll.cap << 8) + 1;
    01543   }
    01544 };
    01545
    01547 static const WindowDesc _vehicle_details_desc = {
    01548   WDP_AUTO, WDP_AUTO, 405, 113, 405, 113,
    01549   WC_VEHICLE_DETAILS, WC_VEHICLE_VIEW,
    01550   WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS | WDF_STICKY_BUTTON | WDF_RESIZABLE,
    01551   _vehicle_details_widgets,
    01552 };
    01553
    01555 static void ShowVehicleDetailsWindow(const Vehicle *v)
    01556 {
    01557   DeleteWindowById(WC_VEHICLE_ORDERS, v->index, false);
    01558   DeleteWindowById(WC_VEHICLE_TIMETABLE, v->index, false);
    01559   AllocateWindowDescFront<VehicleDetailsWindow>(&_vehicle_details_desc, v->index);
    01560 }
    01561
    01562
    01563 /* Unified vehicle GUI - Vehicle View Window */
    01564
    01566 static const Widget _vehicle_view_widgets[] = {
    01567   {   WWT_CLOSEBOX,  RESIZE_NONE,  COLOUR_GREY,   0,  10,   0,  13, STR_00C5,                 STR_018B_CLOSE_WINDOW },           // VVW_WIDGET_CLOSEBOX
    01568   {    WWT_CAPTION, RESIZE_RIGHT,  COLOUR_GREY,  11, 237,   0,  13, 0x0 /* filled later */,   STR_018C_WINDOW_TITLE_DRAG_THIS }, // VVW_WIDGET_CAPTION
    01569   {  WWT_STICKYBOX,    RESIZE_LR,  COLOUR_GREY, 238, 249,   0,  13, 0x0,                      STR_STICKY_BUTTON },               // VVW_WIDGET_STICKY
    01570   {      WWT_PANEL,    RESIZE_RB,  COLOUR_GREY,   0, 231,  14, 103, 0x0,                      STR_NULL },                        // VVW_WIDGET_PANEL
    01571   {      WWT_INSET,    RESIZE_RB,  COLOUR_GREY,   2, 229,  16, 101, 0x0,                      STR_NULL },                        // VVW_WIDGET_VIEWPORT
    01572   {    WWT_PUSHBTN,   RESIZE_RTB,  COLOUR_GREY,   0, 237, 104, 115, 0x0,                      0x0 /* filled later */ },          // VVW_WIDGET_START_STOP_VEH
    01573   { WWT_PUSHIMGBTN,    RESIZE_LR,  COLOUR_GREY, 232, 249,  14,  31, SPR_CENTRE_VIEW_VEHICLE,  0x0 /* filled later */ },          // VVW_WIDGET_CENTER_MAIN_VIEH
    01574   { WWT_PUSHIMGBTN,    RESIZE_LR,  COLOUR_GREY, 232, 249,  32,  49, 0x0 /* filled later */,   0x0 /* filled later */ },          // VVW_WIDGET_GOTO_DEPOT
    01575   { WWT_PUSHIMGBTN,    RESIZE_LR,  COLOUR_GREY, 232, 249,  50,  67, SPR_REFIT_VEHICLE,        0x0 /* filled later */ },          // VVW_WIDGET_REFIT_VEH
    01576   { WWT_PUSHIMGBTN,    RESIZE_LR,  COLOUR_GREY, 232, 249,  68,  85, SPR_SHOW_ORDERS,          0x0 /* filled later */ },          // VVW_WIDGET_SHOW_ORDERS
    01577   { WWT_PUSHIMGBTN,    RESIZE_LR,  COLOUR_GREY, 232, 249,  86, 103, SPR_SHOW_VEHICLE_DETAILS, 0x0 /* filled later */ },          // VVW_WIDGET_SHOW_DETAILS
    01578   { WWT_PUSHIMGBTN,    RESIZE_LR,  COLOUR_GREY, 232, 249,  32,  49, 0x0 /* filled later */,   0x0 /* filled later */ },          // VVW_WIDGET_CLONE_VEH
    01579   {      WWT_PANEL,   RESIZE_LRB,  COLOUR_GREY, 232, 249, 104, 103, 0x0,                      STR_NULL },                        // VVW_WIDGET_EMPTY_BOTTOM_RIGHT
    01580   {  WWT_RESIZEBOX,  RESIZE_LRTB,  COLOUR_GREY, 238, 249, 104, 115, 0x0,                      STR_NULL },                        // VVW_WIDGET_RESIZE
    01581   { WWT_PUSHIMGBTN,    RESIZE_LR,  COLOUR_GREY, 232, 249,  50,  67, SPR_FORCE_VEHICLE_TURN,   STR_9020_FORCE_VEHICLE_TO_TURN_AROUND }, // VVW_WIDGET_TURN_AROUND
    01582   { WWT_PUSHIMGBTN,    RESIZE_LR,  COLOUR_GREY, 232, 249,  50,  67, SPR_IGNORE_SIGNALS,       STR_884A_FORCE_TRAIN_TO_PROCEED },       // VVW_WIDGET_FORCE_PROCEED
    01583 {   WIDGETS_END},
    01584 };
    01585
    01586
    01588 static const WindowDesc _vehicle_view_desc = {
    01589   WDP_AUTO, WDP_AUTO, 250, 116, 250, 116,
    01590   WC_VEHICLE_VIEW, WC_NONE,
    01591   WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS | WDF_STICKY_BUTTON | WDF_RESIZABLE,
    01592   _vehicle_view_widgets,
    01593 };
    01594
    01598 static const WindowDesc _train_view_desc = {
    01599   WDP_AUTO, WDP_AUTO, 250, 134, 250, 134,
    01600   WC_VEHICLE_VIEW, WC_NONE,
    01601   WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS | WDF_STICKY_BUTTON | WDF_RESIZABLE,
    01602   _vehicle_view_widgets,
    01603 };
    01604
    01605
    01606 /* Just to make sure, nobody has changed the vehicle type constants, as we are
    01607    using them for array indexing in a number of places here. */
    01608 assert_compile(VEH_TRAIN == 0);
    01609 assert_compile(VEH_ROAD == 1);
    01610 assert_compile(VEH_SHIP == 2);
    01611 assert_compile(VEH_AIRCRAFT == 3);
    01612
    01614 static const ZoomLevel _vehicle_view_zoom_levels[] = {
    01615   ZOOM_LVL_TRAIN,
    01616   ZOOM_LVL_ROADVEH,
    01617   ZOOM_LVL_SHIP,
    01618   ZOOM_LVL_AIRCRAFT,
    01619 };
    01620
    01621 /* Constants for geometry of vehicle view viewport */
    01622 static const int VV_VIEWPORT_X = 3;
    01623 static const int VV_VIEWPORT_Y = 17;
    01624 static const int VV_INITIAL_VIEWPORT_WIDTH = 226;
    01625 static const int VV_INITIAL_VIEWPORT_HEIGHT = 84;
    01626 static const int VV_INITIAL_VIEWPORT_HEIGHT_TRAIN = 102;
    01627
    01629 enum VehicleCommandTranslation {
    01630   VCT_CMD_START_STOP = 0,
    01631   VCT_CMD_GOTO_DEPOT,
    01632   VCT_CMD_CLONE_VEH,
    01633   VCT_CMD_TURN_AROUND,
    01634 };
    01635
    01637 static const uint32 _vehicle_command_translation_table[][4] = {
    01638   { // VCT_CMD_START_STOP
    01639     CMD_START_STOP_VEHICLE | CMD_MSG(STR_883B_CAN_T_STOP_START_TRAIN),
    01640     CMD_START_STOP_VEHICLE | CMD_MSG(STR_9015_CAN_T_STOP_START_ROAD_VEHICLE),
    01641     CMD_START_STOP_VEHICLE | CMD_MSG(STR_9818_CAN_T_STOP_START_SHIP),
    01642     CMD_START_STOP_VEHICLE | CMD_MSG(STR_A016_CAN_T_STOP_START_AIRCRAFT)
    01643   },
    01644   { // VCT_CMD_GOTO_DEPOT
    01645     /* TrainGotoDepot has a nice randomizer in the pathfinder, which causes desyncs... */
    01646     CMD_SEND_TRAIN_TO_DEPOT | CMD_NO_TEST_IF_IN_NETWORK | CMD_MSG(STR_8830_CAN_T_SEND_TRAIN_TO_DEPOT),
    01647     CMD_SEND_ROADVEH_TO_DEPOT | CMD_MSG(STR_9018_CAN_T_SEND_VEHICLE_TO_DEPOT),
    01648     CMD_SEND_SHIP_TO_DEPOT | CMD_MSG(STR_9819_CAN_T_SEND_SHIP_TO_DEPOT),
    01649     CMD_SEND_AIRCRAFT_TO_HANGAR | CMD_MSG(STR_A012_CAN_T_SEND_AIRCRAFT_TO)
    01650   },
    01651   { // VCT_CMD_CLONE_VEH
    01652     CMD_CLONE_VEHICLE | CMD_MSG(STR_882B_CAN_T_BUILD_RAILROAD_VEHICLE),
    01653     CMD_CLONE_VEHICLE | CMD_MSG(STR_9009_CAN_T_BUILD_ROAD_VEHICLE),
    01654     CMD_CLONE_VEHICLE | CMD_MSG(STR_980D_CAN_T_BUILD_SHIP),
    01655     CMD_CLONE_VEHICLE | CMD_MSG(STR_A008_CAN_T_BUILD_AIRCRAFT)
    01656   },
    01657   { // VCT_CMD_TURN_AROUND
    01658     CMD_REVERSE_TRAIN_DIRECTION | CMD_MSG(STR_8869_CAN_T_REVERSE_DIRECTION),
    01659     CMD_TURN_ROADVEH | CMD_MSG(STR_9033_CAN_T_MAKE_VEHICLE_TURN),
    01660     0xffffffff, // invalid for ships
    01661     0xffffffff  // invalid for aircrafts
    01662   },
    01663 };
    01664
    01666 static bool IsVehicleRefitable(const Vehicle *v)
    01667 {
    01668   if (!v->IsStoppedInDepot()) return false;
    01669
    01670   do {
    01671     if (IsEngineRefittable(v->engine_type)) return true;
    01672   } while ((v->type == VEH_TRAIN || v->type == VEH_ROAD) && (v = v->Next()) != NULL);
    01673
    01674   return false;
    01675 }
    01676
    01677 struct VehicleViewWindow : Window {
    01678   VehicleViewWindow(const WindowDesc *desc, WindowNumber window_number) : Window(desc, window_number)
    01679   {
    01680     const Vehicle *v = GetVehicle(this->window_number);
    01681
    01682     this->owner = v->owner;
    01683     InitializeWindowViewport(this, VV_VIEWPORT_X, VV_VIEWPORT_Y, VV_INITIAL_VIEWPORT_WIDTH,
    01684                          (v->type == VEH_TRAIN) ? VV_INITIAL_VIEWPORT_HEIGHT_TRAIN : VV_INITIAL_VIEWPORT_HEIGHT,
    01685                          this->window_number | (1 << 31), _vehicle_view_zoom_levels[v->type]);
    01686
    01687     /*
    01688      * fill in data and tooltip codes for the widgets and
    01689      * move some of the buttons for trains
    01690      */
    01691     switch (v->type) {
    01692       case VEH_TRAIN:
    01693         this->widget[VVW_WIDGET_CAPTION].data = STR_882E;
    01694
    01695         this->widget[VVW_WIDGET_START_STOP_VEH].tooltips = STR_8846_CURRENT_TRAIN_ACTION_CLICK;
    01696
    01697         this->widget[VVW_WIDGET_CENTER_MAIN_VIEH].tooltips = STR_8848_CENTER_MAIN_VIEW_ON_TRAIN;
    01698
    01699         this->widget[VVW_WIDGET_GOTO_DEPOT].data = SPR_SEND_TRAIN_TODEPOT;
    01700         this->widget[VVW_WIDGET_GOTO_DEPOT].tooltips = STR_8849_SEND_TRAIN_TO_DEPOT;
    01701
    01702         this->widget[VVW_WIDGET_REFIT_VEH].tooltips = STR_RAIL_REFIT_VEHICLE_TO_CARRY;
    01703
    01704         this->widget[VVW_WIDGET_SHOW_ORDERS].tooltips = STR_8847_SHOW_TRAIN_S_ORDERS;
    01705
    01706         this->widget[VVW_WIDGET_SHOW_DETAILS].tooltips = STR_884C_SHOW_TRAIN_DETAILS;
    01707
    01708         this->widget[VVW_WIDGET_CLONE_VEH].data = SPR_CLONE_TRAIN;
    01709         this->widget[VVW_WIDGET_CLONE_VEH].tooltips = STR_CLONE_TRAIN_INFO;
    01710
    01711         this->widget[VVW_WIDGET_TURN_AROUND].tooltips = STR_884B_REVERSE_DIRECTION_OF_TRAIN;
    01712
    01713
    01714         /* due to more buttons we must modify the layout a bit for trains */
    01715         this->widget[VVW_WIDGET_PANEL].bottom = 121;
    01716         this->widget[VVW_WIDGET_VIEWPORT].bottom = 119;
    01717
    01718         this->widget[VVW_WIDGET_START_STOP_VEH].top = 122;
    01719         this->widget[VVW_WIDGET_START_STOP_VEH].bottom = 133;
    01720
    01721         this->widget[VVW_WIDGET_REFIT_VEH].top = 68;
    01722         this->widget[VVW_WIDGET_REFIT_VEH].bottom = 85;
    01723
    01724         this->widget[VVW_WIDGET_SHOW_ORDERS].top = 86;
    01725         this->widget[VVW_WIDGET_SHOW_ORDERS].bottom = 103;
    01726
    01727         this->widget[VVW_WIDGET_SHOW_DETAILS].top = 104;
    01728         this->widget[VVW_WIDGET_SHOW_DETAILS].bottom = 121;
    01729
    01730         this->widget[VVW_WIDGET_EMPTY_BOTTOM_RIGHT].top = 122;
    01731         this->widget[VVW_WIDGET_EMPTY_BOTTOM_RIGHT].bottom = 121;
    01732
    01733         this->widget[VVW_WIDGET_RESIZE].top = 122;
    01734         this->widget[VVW_WIDGET_RESIZE].bottom = 133;
    01735
    01736         this->widget[VVW_WIDGET_TURN_AROUND].top = 68;
    01737         this->widget[VVW_WIDGET_TURN_AROUND].bottom = 85;
    01738         break;
    01739
    01740       case VEH_ROAD:
    01741         this->widget[VVW_WIDGET_CAPTION].data = STR_9002;
    01742
    01743         this->widget[VVW_WIDGET_START_STOP_VEH].tooltips = STR_901C_CURRENT_VEHICLE_ACTION;
    01744
    01745         this->widget[VVW_WIDGET_CENTER_MAIN_VIEH].tooltips = STR_901E_CENTER_MAIN_VIEW_ON_VEHICLE;
    01746
    01747         this->widget[VVW_WIDGET_GOTO_DEPOT].data = SPR_SEND_ROADVEH_TODEPOT;
    01748         this->widget[VVW_WIDGET_GOTO_DEPOT].tooltips = STR_901F_SEND_VEHICLE_TO_DEPOT;
    01749
    01750         this->widget[VVW_WIDGET_REFIT_VEH].tooltips = STR_REFIT_ROAD_VEHICLE_TO_CARRY;
    01751
    01752         this->widget[VVW_WIDGET_SHOW_ORDERS].tooltips = STR_901D_SHOW_VEHICLE_S_ORDERS;
    01753
    01754         this->widget[VVW_WIDGET_SHOW_DETAILS].tooltips = STR_9021_SHOW_ROAD_VEHICLE_DETAILS;
    01755
    01756         this->widget[VVW_WIDGET_CLONE_VEH].data = SPR_CLONE_ROADVEH;
    01757         this->widget[VVW_WIDGET_CLONE_VEH].tooltips = STR_CLONE_ROAD_VEHICLE_INFO;
    01758
    01759         this->SetWidgetHiddenState(VVW_WIDGET_FORCE_PROCEED, true);
    01760         break;
    01761
    01762       case VEH_SHIP:
    01763         this->widget[VVW_WIDGET_CAPTION].data = STR_980F;
    01764
    01765         this->widget[VVW_WIDGET_START_STOP_VEH].tooltips = STR_9827_CURRENT_SHIP_ACTION_CLICK;
    01766
    01767         this->widget[VVW_WIDGET_CENTER_MAIN_VIEH].tooltips = STR_9829_CENTER_MAIN_VIEW_ON_SHIP;
    01768
    01769         this->widget[VVW_WIDGET_GOTO_DEPOT].data = SPR_SEND_SHIP_TODEPOT;
    01770         this->widget[VVW_WIDGET_GOTO_DEPOT].tooltips = STR_982A_SEND_SHIP_TO_DEPOT;
    01771
    01772         this->widget[VVW_WIDGET_REFIT_VEH].tooltips = STR_983A_REFIT_CARGO_SHIP_TO_CARRY;
    01773
    01774         this->widget[VVW_WIDGET_SHOW_ORDERS].tooltips = STR_9828_SHOW_SHIP_S_ORDERS;
    01775
    01776         this->widget[VVW_WIDGET_SHOW_DETAILS].tooltips = STR_982B_SHOW_SHIP_DETAILS;
    01777
    01778         this->widget[VVW_WIDGET_CLONE_VEH].data = SPR_CLONE_SHIP;
    01779         this->widget[VVW_WIDGET_CLONE_VEH].tooltips = STR_CLONE_SHIP_INFO;
    01780
    01781         this->SetWidgetsHiddenState(true,
    01782                                     VVW_WIDGET_TURN_AROUND,
    01783                                     VVW_WIDGET_FORCE_PROCEED,
    01784                                     WIDGET_LIST_END);
    01785         break;
    01786
    01787       case VEH_AIRCRAFT:
    01788         this->widget[VVW_WIDGET_CAPTION].data = STR_A00A;
    01789
    01790         this->widget[VVW_WIDGET_START_STOP_VEH].tooltips = STR_A027_CURRENT_AIRCRAFT_ACTION;
    01791
    01792         this->widget[VVW_WIDGET_CENTER_MAIN_VIEH].tooltips = STR_A029_CENTER_MAIN_VIEW_ON_AIRCRAFT;
    01793
    01794         this->widget[VVW_WIDGET_GOTO_DEPOT].data = SPR_SEND_AIRCRAFT_TODEPOT;
    01795         this->widget[VVW_WIDGET_GOTO_DEPOT].tooltips = STR_A02A_SEND_AIRCRAFT_TO_HANGAR;
    01796
    01797         this->widget[VVW_WIDGET_REFIT_VEH].tooltips = STR_A03B_REFIT_AIRCRAFT_TO_CARRY;
    01798
    01799         this->widget[VVW_WIDGET_SHOW_ORDERS].tooltips = STR_A028_SHOW_AIRCRAFT_S_ORDERS;
    01800
    01801         this->widget[VVW_WIDGET_SHOW_DETAILS].tooltips = STR_A02B_SHOW_AIRCRAFT_DETAILS;
    01802
    01803         this->widget[VVW_WIDGET_CLONE_VEH].data = SPR_CLONE_AIRCRAFT;
    01804         this->widget[VVW_WIDGET_CLONE_VEH].tooltips = STR_CLONE_AIRCRAFT_INFO;
    01805
    01806         this->SetWidgetsHiddenState(true,
    01807                                     VVW_WIDGET_TURN_AROUND,
    01808                                     VVW_WIDGET_FORCE_PROCEED,
    01809                                     WIDGET_LIST_END);
    01810         break;
    01811
    01812         default: NOT_REACHED();
    01813     }
    01814
    01815     this->FindWindowPlacementAndResize(desc);
    01816   }
    01817
    01818   ~VehicleViewWindow()
    01819   {
    01820     DeleteWindowById(WC_VEHICLE_ORDERS, this->window_number, false);
    01821     DeleteWindowById(WC_VEHICLE_REFIT, this->window_number, false);
    01822     DeleteWindowById(WC_VEHICLE_DETAILS, this->window_number, false);
    01823     DeleteWindowById(WC_VEHICLE_TIMETABLE, this->window_number, false);
    01824   }
    01825
    01826   virtual void OnPaint()
    01827   {
    01829     static const StringID _heading_for_depot_strings[] = {
    01830       STR_HEADING_FOR_TRAIN_DEPOT,
    01831       STR_HEADING_FOR_ROAD_DEPOT,
    01832       STR_HEADING_FOR_SHIP_DEPOT,
    01833       STR_HEADING_FOR_HANGAR,
    01834     };
    01835
    01837     static const StringID _heading_for_depot_service_strings[] = {
    01838       STR_HEADING_FOR_TRAIN_DEPOT_SERVICE,
    01839       STR_HEADING_FOR_ROAD_DEPOT_SERVICE,
    01840       STR_HEADING_FOR_SHIP_DEPOT_SERVICE,
    01841       STR_HEADING_FOR_HANGAR_SERVICE,
    01842     };
    01843
    01844     const Vehicle *v = GetVehicle(this->window_number);
    01845     StringID str;
    01846     bool is_localcompany = v->owner == _local_company;
    01847     bool refitable_and_stopped_in_depot = IsVehicleRefitable(v);
    01848
    01849     this->SetWidgetDisabledState(VVW_WIDGET_GOTO_DEPOT, !is_localcompany);
    01850     this->SetWidgetDisabledState(VVW_WIDGET_REFIT_VEH,
    01851                                 !refitable_and_stopped_in_depot || !is_localcompany);
    01852     this->SetWidgetDisabledState(VVW_WIDGET_CLONE_VEH, !is_localcompany);
    01853
    01854     if (v->type == VEH_TRAIN) {
    01855       this->SetWidgetDisabledState(VVW_WIDGET_FORCE_PROCEED, !is_localcompany);
    01856       this->SetWidgetDisabledState(VVW_WIDGET_TURN_AROUND, !is_localcompany);
    01857     }
    01858
    01859     /* draw widgets & caption */
    01860     SetDParam(0, v->index);
    01861     this->DrawWidgets();
    01862
    01863     if (v->vehstatus & VS_CRASHED) {
    01864       str = STR_8863_CRASHED;
    01865     } else if (v->type != VEH_AIRCRAFT && v->breakdown_ctr == 1) { // check for aircraft necessary?
    01866       str = STR_885C_BROKEN_DOWN;
    01867     } else if (v->vehstatus & VS_STOPPED) {
    01868       if (v->type == VEH_TRAIN) {
    01869         if (v->cur_speed == 0) {
    01870           if (v->u.rail.cached_power == 0) {
    01871             str = STR_TRAIN_NO_POWER;
    01872           } else {
    01873             str = STR_8861_STOPPED;
    01874           }
    01875         } else {
    01876           SetDParam(0, v->GetDisplaySpeed());
    01877           str = STR_TRAIN_STOPPING + _settings_client.gui.vehicle_speed;
    01878         }
    01879       } else { // no train
    01880         str = STR_8861_STOPPED;
    01881       }
    01882     } else if (v->type == VEH_TRAIN && HasBit(v->u.rail.flags, VRF_TRAIN_STUCK)) {
    01883       str = STR_TRAIN_STUCK;
    01884     } else { // vehicle is in a "normal" state, show current order
    01885       switch (v->current_order.GetType()) {
    01886         case OT_GOTO_STATION: {
    01887           SetDParam(0, v->current_order.GetDestination());
    01888           SetDParam(1, v->GetDisplaySpeed());
    01889           str = STR_HEADING_FOR_STATION + _settings_client.gui.vehicle_speed;
    01890         } break;
    01891
    01892         case OT_GOTO_DEPOT: {
    01893           if (v->type == VEH_AIRCRAFT) {
    01894             /* Aircrafts always go to a station, even if you say depot */
    01895             SetDParam(0, v->current_order.GetDestination());
    01896             SetDParam(1, v->GetDisplaySpeed());
    01897           } else {
    01898             Depot *depot = GetDepot(v->current_order.GetDestination());
    01899             SetDParam(0, depot->town_index);
    01900             SetDParam(1, v->GetDisplaySpeed());
    01901           }
    01902           if (v->current_order.GetDepotActionType() & ODATFB_HALT) {
    01903             str = _heading_for_depot_strings[v->type] + _settings_client.gui.vehicle_speed;
    01904           } else {
    01905             str = _heading_for_depot_service_strings[v->type] + _settings_client.gui.vehicle_speed;
    01906           }
    01907         } break;
    01908
    01909         case OT_LOADING:
    01910           str = STR_882F_LOADING_UNLOADING;
    01911           break;
    01912
    01913         case OT_GOTO_WAYPOINT: {
    01914           assert(v->type == VEH_TRAIN);
    01915           SetDParam(0, v->current_order.GetDestination());
    01916           str = STR_HEADING_FOR_WAYPOINT + _settings_client.gui.vehicle_speed;
    01917           SetDParam(1, v->GetDisplaySpeed());
    01918           break;
    01919         }
    01920
    01921         case OT_LEAVESTATION:
    01922           if (v->type != VEH_AIRCRAFT) {
    01923             str = STR_LEAVING;
    01924             break;
    01925           }
    01926           /* fall-through if aircraft. Does this even happen? */
    01927
    01928         default:
    01929           if (v->GetNumOrders() == 0) {
    01930             str = STR_NO_ORDERS + _settings_client.gui.vehicle_speed;
    01931             SetDParam(0, v->GetDisplaySpeed());
    01932           } else {
    01933             str = STR_EMPTY;
    01934           }
    01935           break;
    01936       }
    01937     }
    01938
    01939     /* draw the flag plus orders */
    01940     DrawSprite(v->vehstatus & VS_STOPPED ? SPR_FLAG_VEH_STOPPED : SPR_FLAG_VEH_RUNNING, PAL_NONE, 2, this->widget[VVW_WIDGET_START_STOP_VEH].top + 1);
    01941     DrawStringCenteredTruncated(this->widget[VVW_WIDGET_START_STOP_VEH].left + 8, this->widget[VVW_WIDGET_START_STOP_VEH].right, this->widget[VVW_WIDGET_START_STOP_VEH].top + 1, str, TC_FROMSTRING);
    01942     this->DrawViewport();
    01943   }
    01944
    01945   virtual void OnClick(Point pt, int widget)
    01946   {
    01947     const Vehicle *v = GetVehicle(this->window_number);
    01948
    01949     switch (widget) {
    01950       case VVW_WIDGET_START_STOP_VEH: // start stop
    01951         DoCommandP(v->tile, v->index, 0,
    01952                     _vehicle_command_translation_table[VCT_CMD_START_STOP][v->type]);
    01953         break;
    01954       case VVW_WIDGET_CENTER_MAIN_VIEH: {/* center main view */
    01955         const Window *mainwindow = FindWindowById(WC_MAIN_WINDOW, 0);
    01956         /* code to allow the main window to 'follow' the vehicle if the ctrl key is pressed */
    01957         if (_ctrl_pressed && mainwindow->viewport->zoom == ZOOM_LVL_NORMAL) {
    01958           mainwindow->viewport->follow_vehicle = v->index;
    01959         } else {
    01960           ScrollMainWindowTo(v->x_pos, v->y_pos);
    01961         }
    01962       } break;
    01963
    01964       case VVW_WIDGET_GOTO_DEPOT: // goto hangar
    01965         DoCommandP(v->tile, v->index, _ctrl_pressed ? DEPOT_SERVICE : 0,
    01966           _vehicle_command_translation_table[VCT_CMD_GOTO_DEPOT][v->type]);
    01967         break;
    01968       case VVW_WIDGET_REFIT_VEH: // refit
    01969         ShowVehicleRefitWindow(v, INVALID_VEH_ORDER_ID, this);
    01970         break;
    01971       case VVW_WIDGET_SHOW_ORDERS: // show orders
    01972         if (_ctrl_pressed) {
    01973           ShowTimetableWindow(v);
    01974         } else {
    01975           ShowOrdersWindow(v);
    01976         }
    01977         break;
    01978       case VVW_WIDGET_SHOW_DETAILS: // show details
    01979         ShowVehicleDetailsWindow(v);
    01980         break;
    01981       case VVW_WIDGET_CLONE_VEH: // clone vehicle
    01982         DoCommandP(v->tile, v->index, _ctrl_pressed ? 1 : 0,
    01983                     _vehicle_command_translation_table[VCT_CMD_CLONE_VEH][v->type],
    01984                     CcCloneVehicle);
    01985         break;
    01986       case VVW_WIDGET_TURN_AROUND: // turn around
    01987         assert(v->type == VEH_TRAIN || v->type == VEH_ROAD);
    01988         DoCommandP(v->tile, v->index, 0,
    01989                     _vehicle_command_translation_table[VCT_CMD_TURN_AROUND][v->type]);
    01990         break;
    01991       case VVW_WIDGET_FORCE_PROCEED: // force proceed
    01992         assert(v->type == VEH_TRAIN);
    01993         DoCommandP(v->tile, v->index, 0, CMD_FORCE_TRAIN_PROCEED | CMD_MSG(STR_8862_CAN_T_MAKE_TRAIN_PASS_SIGNAL));
    01994         break;
    01995     }
    01996   }
    01997
    01998   virtual void OnResize(Point new_size, Point delta)
    01999   {
    02000     this->viewport->width          += delta.x;
    02001     this->viewport->height         += delta.y;
    02002     this->viewport->virtual_width  += delta.x;
    02003     this->viewport->virtual_height += delta.y;
    02004   }
    02005
    02006   virtual void OnTick()
    02007   {
    02008     const Vehicle *v = GetVehicle(this->window_number);
    02009     bool veh_stopped = v->IsStoppedInDepot();
    02010
    02011     /* Widget VVW_WIDGET_GOTO_DEPOT must be hidden if the vehicle is already
    02012      * stopped in depot.
    02013      * Widget VVW_WIDGET_CLONE_VEH should then be shown, since cloning is
    02014      * allowed only while in depot and stopped.
    02015      * This sytem allows to have two buttons, on top of each other.
    02016      * The same system applies to widget VVW_WIDGET_REFIT_VEH and VVW_WIDGET_TURN_AROUND.*/
    02017     if (veh_stopped != this->IsWidgetHidden(VVW_WIDGET_GOTO_DEPOT) || veh_stopped == this->IsWidgetHidden(VVW_WIDGET_CLONE_VEH)) {
    02018       this->SetWidgetHiddenState( VVW_WIDGET_GOTO_DEPOT, veh_stopped);  // send to depot
    02019       this->SetWidgetHiddenState(VVW_WIDGET_CLONE_VEH, !veh_stopped); // clone
    02020       if (v->type == VEH_ROAD || v->type == VEH_TRAIN) {
    02021         this->SetWidgetHiddenState( VVW_WIDGET_REFIT_VEH, !veh_stopped); // refit
    02022         this->SetWidgetHiddenState(VVW_WIDGET_TURN_AROUND, veh_stopped);  // force turn around
    02023       }
    02024       this->SetDirty();
    02025     }
    02026   }
    02027 };
    02028
    02029
    02031 void ShowVehicleViewWindow(const Vehicle *v)
    02032 {
    02033   AllocateWindowDescFront<VehicleViewWindow>((v->type == VEH_TRAIN) ? &_train_view_desc : &_vehicle_view_desc, v->index);
    02034 }
    02035
    02036 void StopGlobalFollowVehicle(const Vehicle *v)
    02037 {
    02038   Window *w = FindWindowById(WC_MAIN_WINDOW, 0);
    02039   if (w != NULL && w->viewport->follow_vehicle == v->index) {
    02040     ScrollMainWindowTo(v->x_pos, v->y_pos, true); // lock the main view on the vehicle's last position
    02041     w->viewport->follow_vehicle = INVALID_VEHICLE;
    02042   }
    02043 }