# vehicle_gui.cpp

Go to the documentation of this file.

    00001 /* $Id: vehicle_gui.cpp 16061 2009-04-14 21:20:03Z rubidium $ */
    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     /* Set up the window widgets */
    00819     switch (this->vehicle_type) {
    00820       case VEH_TRAIN:
    00821         this->widget[VLW_WIDGET_LIST].tooltips          = STR_883D_TRAINS_CLICK_ON_TRAIN_FOR;
    00822         this->widget[VLW_WIDGET_AVAILABLE_VEHICLES].data = STR_AVAILABLE_TRAINS;
    00823         break;
    00824
    00825       case VEH_ROAD:
    00826         this->widget[VLW_WIDGET_LIST].tooltips          = STR_901A_ROAD_VEHICLES_CLICK_ON;
    00827         this->widget[VLW_WIDGET_AVAILABLE_VEHICLES].data = STR_AVAILABLE_ROAD_VEHICLES;
    00828         break;
    00829
    00830       case VEH_SHIP:
    00831         this->widget[VLW_WIDGET_LIST].tooltips          = STR_9823_SHIPS_CLICK_ON_SHIP_FOR;
    00832         this->widget[VLW_WIDGET_AVAILABLE_VEHICLES].data = STR_AVAILABLE_SHIPS;
    00833         break;
    00834
    00835       case VEH_AIRCRAFT:
    00836         this->widget[VLW_WIDGET_LIST].tooltips          = STR_A01F_AIRCRAFT_CLICK_ON_AIRCRAFT;
    00837         this->widget[VLW_WIDGET_AVAILABLE_VEHICLES].data = STR_AVAILABLE_AIRCRAFT;
    00838         break;
    00839
    00840       default: NOT_REACHED();
    00841     }
    00842
    00843     switch (window_type) {
    00844       case VLW_SHARED_ORDERS:
    00845         this->widget[VLW_WIDGET_CAPTION].data  = STR_VEH_WITH_SHARED_ORDERS_LIST;
    00846         break;
    00847
    00848       case VLW_STANDARD: // Company Name - standard widget setup
    00849         switch (this->vehicle_type) {
    00850           case VEH_TRAIN:    this->widget[VLW_WIDGET_CAPTION].data = STR_881B_TRAINS;        break;
    00851           case VEH_ROAD:     this->widget[VLW_WIDGET_CAPTION].data = STR_9001_ROAD_VEHICLES; break;
    00852           case VEH_SHIP:     this->widget[VLW_WIDGET_CAPTION].data = STR_9805_SHIPS;         break;
    00853           case VEH_AIRCRAFT: this->widget[VLW_WIDGET_CAPTION].data = STR_A009_AIRCRAFT;      break;
    00854           default: NOT_REACHED(); break;
    00855         }
    00856         break;
    00857
    00858       case VLW_WAYPOINT_LIST:
    00859         this->widget[VLW_WIDGET_CAPTION].data = STR_WAYPOINT_VIEWPORT_LIST;
    00860         break;
    00861
    00862       case VLW_STATION_LIST: // Station Name
    00863         switch (this->vehicle_type) {
    00864           case VEH_TRAIN:    this->widget[VLW_WIDGET_CAPTION].data = STR_SCHEDULED_TRAINS;        break;
    00865           case VEH_ROAD:     this->widget[VLW_WIDGET_CAPTION].data = STR_SCHEDULED_ROAD_VEHICLES; break;
    00866           case VEH_SHIP:     this->widget[VLW_WIDGET_CAPTION].data = STR_SCHEDULED_SHIPS;         break;
    00867           case VEH_AIRCRAFT: this->widget[VLW_WIDGET_CAPTION].data = STR_SCHEDULED_AIRCRAFT;      break;
    00868           default: NOT_REACHED(); break;
    00869         }
    00870         break;
    00871
    00872       case VLW_DEPOT_LIST:
    00873         switch (this->vehicle_type) {
    00874           case VEH_TRAIN:    this->widget[VLW_WIDGET_CAPTION].data = STR_VEHICLE_LIST_TRAIN_DEPOT;    break;
    00875           case VEH_ROAD:     this->widget[VLW_WIDGET_CAPTION].data = STR_VEHICLE_LIST_ROADVEH_DEPOT;  break;
    00876           case VEH_SHIP:     this->widget[VLW_WIDGET_CAPTION].data = STR_VEHICLE_LIST_SHIP_DEPOT;     break;
    00877           case VEH_AIRCRAFT: this->widget[VLW_WIDGET_CAPTION].data = STR_VEHICLE_LIST_AIRCRAFT_DEPOT; break;
    00878           default: NOT_REACHED(); break;
    00879         }
    00880         break;
    00881       default: NOT_REACHED(); break;
    00882     }
    00883
    00884     switch (this->vehicle_type) {
    00885       case VEH_TRAIN:
    00886         this->resize.step_width = 1;
    00887         /* Fallthrough */
    00888       case VEH_ROAD:
    00889         this->vscroll.cap = 6;
    00890         this->resize.step_height = PLY_WND_PRC__SIZE_OF_ROW_SMALL;
    00891         break;
    00892       case VEH_SHIP:
    00893       case VEH_AIRCRAFT:
    00894         this->vscroll.cap = 4;
    00895         this->resize.step_height = PLY_WND_PRC__SIZE_OF_ROW_BIG;
    00896         break;
    00897       default: NOT_REACHED();
    00898     }
    00899
    00900
    00901     this->widget[VLW_WIDGET_LIST].data = (this->vscroll.cap << 8) + 1;
    00902
    00903     /* Set up sorting. Make the window-specific _sorting variable
    00904      * point to the correct global _sorting struct so we are freed
    00905      * from having conditionals during window operation */
    00906     switch (this->vehicle_type) {
    00907       case VEH_TRAIN:    this->sorting = &_sorting.train; break;
    00908       case VEH_ROAD:     this->sorting = &_sorting.roadveh; break;
    00909       case VEH_SHIP:     this->sorting = &_sorting.ship; break;
    00910       case VEH_AIRCRAFT: this->sorting = &_sorting.aircraft; break;
    00911       default: NOT_REACHED(); break;
    00912     }
    00913
    00914     this->vehicles.SetListing(*this->sorting);
    00915     this->vehicles.ForceRebuild();
    00916     this->vehicles.NeedResort();
    00917
    00918     this->FindWindowPlacementAndResize(desc);
    00919     if (this->vehicle_type == VEH_TRAIN) ResizeWindow(this, 65, 0);
    00920   }
    00921
    00922   ~VehicleListWindow()
    00923   {
    00924     *this->sorting = this->vehicles.GetListing();
    00925   }
    00926
    00927   virtual void OnPaint()
    00928   {
    00929     int x = 2;
    00930     const Owner owner = this->owner;
    00931     const uint16 window_type = this->window_number & VLW_MASK;
    00932     const uint16 index = GB(this->window_number, 16, 16);
    00933
    00934     this->BuildVehicleList(owner, index, window_type);
    00935     this->SortVehicleList();
    00936     SetVScrollCount(this, this->vehicles.Length());
    00937
    00938     if (this->vehicles.Length() == 0) HideDropDownMenu(this);
    00939
    00940     /* draw the widgets */
    00941     switch (window_type) {
    00942       case VLW_SHARED_ORDERS: // Shared Orders
    00943         if (this->vehicles.Length() == 0) {
    00944           /* We can't open this window without vehicles using this order
    00945            * and we should close the window when deleting the order      */
    00946           NOT_REACHED();
    00947         }
    00948         SetDParam(0, this->vscroll.count);
    00949         break;
    00950
    00951       case VLW_STANDARD: // Company Name
    00952         SetDParam(0, owner);
    00953         SetDParam(1, this->vscroll.count);
    00954         break;
    00955
    00956       case VLW_WAYPOINT_LIST:
    00957         SetDParam(0, index);
    00958         SetDParam(1, this->vscroll.count);
    00959         break;
    00960
    00961       case VLW_STATION_LIST: // Station Name
    00962         SetDParam(0, index);
    00963         SetDParam(1, this->vscroll.count);
    00964         break;
    00965
    00966       case VLW_DEPOT_LIST:
    00967         switch (this->vehicle_type) {
    00968           case VEH_TRAIN:    SetDParam(0, STR_8800_TRAIN_DEPOT);        break;
    00969           case VEH_ROAD:     SetDParam(0, STR_9003_ROAD_VEHICLE_DEPOT); break;
    00970           case VEH_SHIP:     SetDParam(0, STR_9803_SHIP_DEPOT);         break;
    00971           case VEH_AIRCRAFT: SetDParam(0, STR_A002_AIRCRAFT_HANGAR);    break;
    00972           default: NOT_REACHED(); break;
    00973         }
    00974         if (this->vehicle_type == VEH_AIRCRAFT) {
    00975           SetDParam(1, index); // Airport name
    00976         } else {
    00977           SetDParam(1, GetDepot(index)->town_index);
    00978         }
    00979         SetDParam(2, this->vscroll.count);
    00980         break;
    00981       default: NOT_REACHED(); break;
    00982     }
    00983
    00984     /* Hide the widgets that we will not use in this window
    00985      * Some windows contains actions only fit for the owner */
    00986     this->SetWidgetsHiddenState(this->owner != _local_company,
    00987       VLW_WIDGET_AVAILABLE_VEHICLES,
    00988       VLW_WIDGET_MANAGE_VEHICLES_DROPDOWN,
    00989       VLW_WIDGET_STOP_ALL,
    00990       VLW_WIDGET_START_ALL,
    00991       VLW_WIDGET_EMPTY_BOTTOM_RIGHT,
    00992       WIDGET_LIST_END);
    00993     this->SetWidgetHiddenState(VLW_WIDGET_OTHER_COMPANY_FILLER, this->owner == _local_company);
    00994     if (this->owner == _local_company) {
    00995       this->SetWidgetDisabledState(VLW_WIDGET_AVAILABLE_VEHICLES, window_type != VLW_STANDARD);
    00996       this->SetWidgetsDisabledState(this->vehicles.Length() == 0,
    00997         VLW_WIDGET_MANAGE_VEHICLES_DROPDOWN,
    00998         VLW_WIDGET_STOP_ALL,
    00999         VLW_WIDGET_START_ALL,
    01000         WIDGET_LIST_END);
    01001     }
    01002
    01003     this->DrawWidgets();
    01004
    01005     /* draw sorting criteria string */
    01006     DrawString(85, 15, this->vehicle_sorter_names[this->vehicles.SortType()], TC_BLACK);
    01007     /* draw arrow pointing up/down for ascending/descending sorting */
    01008     this->DrawSortButtonState(VLW_WIDGET_SORT_ORDER, this->vehicles.IsDescSortOrder() ? SBS_DOWN : SBS_UP);
    01009
    01010     this->DrawVehicleListItems(x,  INVALID_VEHICLE);
    01011   }
    01012
    01013   virtual void OnClick(Point pt, int widget)
    01014   {
    01015     switch (widget) {
    01016       case VLW_WIDGET_SORT_ORDER: // Flip sorting method ascending/descending
    01017         this->vehicles.ToggleSortOrder();
    01018         this->SetDirty();
    01019         break;
    01020       case VLW_WIDGET_SORT_BY_PULLDOWN:// Select sorting criteria dropdown menu
    01021         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));
    01022         return;
    01023       case VLW_WIDGET_LIST: { // Matrix to show vehicles
    01024         uint32 id_v = (pt.y - PLY_WND_PRC__OFFSET_TOP_WIDGET) / this->resize.step_height;
    01025         const Vehicle *v;
    01026
    01027         if (id_v >= this->vscroll.cap) return; // click out of bounds
    01028
    01029         id_v += this->vscroll.pos;
    01030
    01031         if (id_v >= this->vehicles.Length()) return; // click out of list bound
    01032
    01033         v = this->vehicles[id_v];
    01034
    01035         ShowVehicleViewWindow(v);
    01036       } break;
    01037
    01038       case VLW_WIDGET_AVAILABLE_VEHICLES:
    01039         ShowBuildVehicleWindow(INVALID_TILE, this->vehicle_type);
    01040         break;
    01041
    01042       case VLW_WIDGET_MANAGE_VEHICLES_DROPDOWN: {
    01043         static StringID action_str[] = {
    01044           STR_REPLACE_VEHICLES,
    01045           STR_SEND_FOR_SERVICING,
    01046           STR_NULL,
    01047           INVALID_STRING_ID
    01048         };
    01049
    01050         static const StringID depot_name[] = {
    01051           STR_SEND_TRAIN_TO_DEPOT,
    01052           STR_SEND_ROAD_VEHICLE_TO_DEPOT,
    01053           STR_SEND_SHIP_TO_DEPOT,
    01054           STR_SEND_AIRCRAFT_TO_HANGAR
    01055         };
    01056
    01057         /* XXX - Substite string since the dropdown cannot handle dynamic strings */
    01058         action_str[2] = depot_name[this->vehicle_type];
    01059         ShowDropDownMenu(this, action_str, 0, VLW_WIDGET_MANAGE_VEHICLES_DROPDOWN, 0, (this->window_number & VLW_MASK) == VLW_STANDARD ? 0 : 1);
    01060         break;
    01061       }
    01062
    01063       case VLW_WIDGET_STOP_ALL:
    01064       case VLW_WIDGET_START_ALL:
    01065         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);
    01066         break;
    01067     }
    01068   }
    01069
    01070   virtual void OnDropdownSelect(int widget, int index)
    01071   {
    01072     switch (widget) {
    01073       case VLW_WIDGET_SORT_BY_PULLDOWN:
    01074         this->vehicles.SetSortType(index);
    01075         break;
    01076       case VLW_WIDGET_MANAGE_VEHICLES_DROPDOWN:
    01077         assert(this->vehicles.Length() != 0);
    01078
    01079         switch (index) {
    01080           case 0: // Replace window
    01081             ShowReplaceGroupVehicleWindow(DEFAULT_GROUP, this->vehicle_type);
    01082             break;
    01083           case 1: // Send for servicing
    01084             DoCommandP(0, GB(this->window_number, 16, 16) /* StationID or OrderID (depending on VLW) */,
    01085               (this->window_number & VLW_MASK) | DEPOT_MASS_SEND | DEPOT_SERVICE,
    01086               GetCmdSendToDepot(this->vehicle_type));
    01087             break;
    01088           case 2: // Send to Depots
    01089             DoCommandP(0, GB(this->window_number, 16, 16) /* StationID or OrderID (depending on VLW) */,
    01090               (this->window_number & VLW_MASK) | DEPOT_MASS_SEND,
    01091               GetCmdSendToDepot(this->vehicle_type));
    01092             break;
    01093
    01094           default: NOT_REACHED();
    01095         }
    01096         break;
    01097       default: NOT_REACHED();
    01098     }
    01099     this->SetDirty();
    01100   }
    01101
    01102   virtual void OnTick()
    01103   {
    01104     if (_pause_game != 0) return;
    01105     if (this->vehicles.NeedResort()) {
    01106       StationID station = ((this->window_number & VLW_MASK) == VLW_STATION_LIST) ? GB(this->window_number, 16, 16) : INVALID_STATION;
    01107
    01108       DEBUG(misc, 3, "Periodic resort %d list company %d at station %d", this->vehicle_type, this->owner, station);
    01109       this->SetDirty();
    01110     }
    01111   }
    01112
    01113   virtual void OnResize(Point new_size, Point delta)
    01114   {
    01115     this->vscroll.cap += delta.y / (int)this->resize.step_height;
    01116     this->widget[VLW_WIDGET_LIST].data = (this->vscroll.cap << 8) + 1;
    01117   }
    01118
    01119   virtual void OnInvalidateData(int data)
    01120   {
    01121     if (HasBit(data, 15) && (this->window_number & VLW_MASK) == VLW_SHARED_ORDERS) {
    01122       SB(this->window_number, 16, 16, GB(data, 16, 16));
    01123       this->vehicles.ForceRebuild();
    01124       return;
    01125     }
    01126
    01127     if (data == 0) {
    01128       this->vehicles.ForceRebuild();
    01129     } else {
    01130       this->vehicles.ForceResort();
    01131     }
    01132   }
    01133 };
    01134
    01135 static WindowDesc _vehicle_list_desc(
    01136   WDP_AUTO, WDP_AUTO, 260, 194, 260, 246,
    01137   WC_INVALID, WC_NONE,
    01138   WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS | WDF_STICKY_BUTTON | WDF_RESIZABLE,
    01139   _vehicle_list_widgets
    01140 );
    01141
    01142 static void ShowVehicleListWindowLocal(CompanyID company, uint16 VLW_flag, VehicleType vehicle_type, uint16 unique_number)
    01143 {
    01144   if (!IsValidCompanyID(company)) return;
    01145
    01146   _vehicle_list_desc.cls = GetWindowClassForVehicleType(vehicle_type);
    01147   WindowNumber num = (unique_number << 16) | (vehicle_type << 11) | VLW_flag | company;
    01148   AllocateWindowDescFront<VehicleListWindow>(&_vehicle_list_desc, num);
    01149 }
    01150
    01151 void ShowVehicleListWindow(CompanyID company, VehicleType vehicle_type)
    01152 {
    01153   /* If _settings_client.gui.advanced_vehicle_list > 1, display the Advanced list
    01154    * if _settings_client.gui.advanced_vehicle_list == 1, display Advanced list only for local company
    01155    * if _ctrl_pressed, do the opposite action (Advanced list x Normal list)
    01156    */
    01157
    01158   if ((_settings_client.gui.advanced_vehicle_list > (uint)(company != _local_company)) != _ctrl_pressed) {
    01159     ShowCompanyGroup(company, vehicle_type);
    01160   } else {
    01161     ShowVehicleListWindowLocal(company, VLW_STANDARD, vehicle_type, 0);
    01162   }
    01163 }
    01164
    01165 void ShowVehicleListWindow(const Waypoint *wp)
    01166 {
    01167   if (wp == NULL) return;
    01168   ShowVehicleListWindowLocal(wp->owner, VLW_WAYPOINT_LIST, VEH_TRAIN, wp->index);
    01169 }
    01170
    01171 void ShowVehicleListWindow(const Vehicle *v)
    01172 {
    01173   ShowVehicleListWindowLocal(v->owner, VLW_SHARED_ORDERS, v->type, v->FirstShared()->index);
    01174 }
    01175
    01176 void ShowVehicleListWindow(CompanyID company, VehicleType vehicle_type, StationID station)
    01177 {
    01178   ShowVehicleListWindowLocal(company, VLW_STATION_LIST, vehicle_type, station);
    01179 }
    01180
    01181 void ShowVehicleListWindow(CompanyID company, VehicleType vehicle_type, TileIndex depot_tile)
    01182 {
    01183   uint16 depot_airport_index;
    01184
    01185   if (vehicle_type == VEH_AIRCRAFT) {
    01186     depot_airport_index = GetStationIndex(depot_tile);
    01187   } else {
    01188     Depot *depot = GetDepotByTile(depot_tile);
    01189     if (depot == NULL) return; // no depot to show
    01190     depot_airport_index = depot->index;
    01191   }
    01192   ShowVehicleListWindowLocal(company, VLW_DEPOT_LIST, vehicle_type, depot_airport_index);
    01193 }
    01194
    01195
    01196 /* Unified vehicle GUI - Vehicle Details Window */
    01197
    01199 enum VehicleDetailsWindowWidgets {
    01200   VLD_WIDGET_CLOSEBOX = 0,
    01201   VLD_WIDGET_CAPTION,
    01202   VLD_WIDGET_RENAME_VEHICLE,
    01203   VLD_WIDGET_STICKY,
    01204   VLD_WIDGET_TOP_DETAILS,
    01205   VLD_WIDGET_INCREASE_SERVICING_INTERVAL,
    01206   VLD_WIDGET_DECREASE_SERVICING_INTERVAL,
    01207   VLD_WIDGET_BOTTOM_RIGHT,
    01208   VLD_WIDGET_MIDDLE_DETAILS,
    01209   VLD_WIDGET_SCROLLBAR,
    01210   VLD_WIDGET_DETAILS_CARGO_CARRIED,
    01211   VLD_WIDGET_DETAILS_TRAIN_VEHICLES,
    01212   VLD_WIDGET_DETAILS_CAPACITY_OF_EACH,
    01213   VLD_WIDGET_DETAILS_TOTAL_CARGO,
    01214   VLD_WIDGET_RESIZE,
    01215 };
    01216
    01218 static const Widget _vehicle_details_widgets[] = {
    01219   {   WWT_CLOSEBOX,   RESIZE_NONE,  COLOUR_GREY,   0,  10,   0,  13, STR_00C5,             STR_018B_CLOSE_WINDOW},                  // VLD_WIDGET_CLOSEBOX
    01220   {    WWT_CAPTION,  RESIZE_RIGHT,  COLOUR_GREY,  11, 352,   0,  13, 0x0,                  STR_018C_WINDOW_TITLE_DRAG_THIS},        // VLD_WIDGET_CAPTION
    01221   { WWT_PUSHTXTBTN,     RESIZE_LR,  COLOUR_GREY, 353, 392,   0,  13, STR_01AA_NAME,        STR_NULL /* filled in later */},         // VLD_WIDGET_RENAME_VEHICLE
    01222   {  WWT_STICKYBOX,     RESIZE_LR,  COLOUR_GREY, 393, 404,   0,  13, STR_NULL,             STR_STICKY_BUTTON},                      // VLD_WIDGET_STICKY
    01223   {      WWT_PANEL,  RESIZE_RIGHT,  COLOUR_GREY,   0, 404,  14,  55, 0x0,                  STR_NULL},                               // VLD_WIDGET_TOP_DETAILS
    01224   { WWT_PUSHTXTBTN,     RESIZE_TB,  COLOUR_GREY,   0,  10, 101, 106, STR_0188,             STR_884D_INCREASE_SERVICING_INTERVAL},   // VLD_WIDGET_INCREASE_SERVICING_INTERVAL
    01225   { WWT_PUSHTXTBTN,     RESIZE_TB,  COLOUR_GREY,   0,  10, 107, 112, STR_0189,             STR_884E_DECREASE_SERVICING_INTERVAL},   // VLD_WIDGET_DECREASE_SERVICING_INTERVAL
    01226   {      WWT_PANEL,    RESIZE_RTB,  COLOUR_GREY,  11, 404, 101, 112, 0x0,                  STR_NULL},                               // VLD_WIDGET_BOTTOM_RIGHT
    01227   {     WWT_MATRIX,     RESIZE_RB,  COLOUR_GREY,   0, 392,  56, 100, 0x701,                STR_NULL},                               // VLD_WIDGET_MIDDLE_DETAILS
    01228   {  WWT_SCROLLBAR,    RESIZE_LRB,  COLOUR_GREY, 393, 404,  56, 100, 0x0,                  STR_0190_SCROLL_BAR_SCROLLS_LIST},       // VLD_WIDGET_SCROLLBAR
    01229   { 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
    01230   { 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
    01231   { 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
    01232   { WWT_PUSHTXTBTN,    RESIZE_RTB,  COLOUR_GREY, 294, 392, 113, 124, STR_TOTAL_CARGO,      STR_SHOW_TOTAL_CARGO},                   // VLD_WIDGET_DETAILS_TOTAL_CARGO
    01233   {  WWT_RESIZEBOX,   RESIZE_LRTB,  COLOUR_GREY, 393, 404, 113, 124, 0x0,                  STR_RESIZE_BUTTON},                      // VLD_RESIZE
    01234   {   WIDGETS_END},
    01235 };
    01236
    01237
    01239 enum VehicleStringTranslation {
    01240   VST_VEHICLE_AGE_RUNNING_COST_YR,
    01241   VST_VEHICLE_MAX_SPEED,
    01242   VST_VEHICLE_PROFIT_THIS_YEAR_LAST_YEAR,
    01243   VST_VEHICLE_RELIABILITY_BREAKDOWNS,
    01244 };
    01245
    01247 static const StringID _vehicle_translation_table[][4] = {
    01248   { // VST_VEHICLE_AGE_RUNNING_COST_YR
    01249     STR_885D_AGE_RUNNING_COST_YR,
    01250     STR_900D_AGE_RUNNING_COST_YR,
    01251     STR_9812_AGE_RUNNING_COST_YR,
    01252     STR_A00D_AGE_RUNNING_COST_YR,
    01253   },
    01254   { // VST_VEHICLE_MAX_SPEED
    01255     STR_NULL,
    01256     STR_900E_MAX_SPEED,
    01257     STR_9813_MAX_SPEED,
    01258     STR_A00E_MAX_SPEED,
    01259   },
    01260   { // VST_VEHICLE_PROFIT_THIS_YEAR_LAST_YEAR
    01261     STR_885F_PROFIT_THIS_YEAR_LAST_YEAR,
    01262     STR_900F_PROFIT_THIS_YEAR_LAST_YEAR,
    01263     STR_9814_PROFIT_THIS_YEAR_LAST_YEAR,
    01264     STR_A00F_PROFIT_THIS_YEAR_LAST_YEAR,
    01265   },
    01266   { // VST_VEHICLE_RELIABILITY_BREAKDOWNS
    01267     STR_8860_RELIABILITY_BREAKDOWNS,
    01268     STR_9010_RELIABILITY_BREAKDOWNS,
    01269     STR_9815_RELIABILITY_BREAKDOWNS,
    01270     STR_A010_RELIABILITY_BREAKDOWNS,
    01271   },
    01272 };
    01273
    01274
    01275 extern int GetTrainDetailsWndVScroll(VehicleID veh_id, byte det_tab);
    01276 extern void DrawTrainDetails(const Vehicle *v, int x, int y, int vscroll_pos, uint16 vscroll_cap, byte det_tab);
    01277 extern void DrawRoadVehDetails(const Vehicle *v, int x, int y);
    01278 extern void DrawShipDetails(const Vehicle *v, int x, int y);
    01279 extern void DrawAircraftDetails(const Vehicle *v, int x, int y);
    01280
    01281 struct VehicleDetailsWindow : Window {
    01282   int tab;
    01283
    01285   VehicleDetailsWindow(const WindowDesc *desc, WindowNumber window_number) : Window(desc, window_number)
    01286   {
    01287     const Vehicle *v = GetVehicle(this->window_number);
    01288
    01289     switch (v->type) {
    01290       case VEH_TRAIN:
    01291         ResizeWindow(this, 0, 39);
    01292
    01293         this->vscroll.cap = 6;
    01294         this->height += 12;
    01295         this->resize.step_height = 14;
    01296         this->resize.height = this->height - 14 * 2; // Minimum of 4 wagons in the display
    01297
    01298         this->widget[VLD_WIDGET_RENAME_VEHICLE].tooltips = STR_8867_NAME_TRAIN;
    01299         this->widget[VLD_WIDGET_CAPTION].data = STR_8802_DETAILS;
    01300         break;
    01301
    01302       case VEH_ROAD: {
    01303         this->widget[VLD_WIDGET_CAPTION].data = STR_900C_DETAILS;
    01304         this->widget[VLD_WIDGET_RENAME_VEHICLE].tooltips = STR_902E_NAME_ROAD_VEHICLE;
    01305
    01306         if (!RoadVehHasArticPart(v)) break;
    01307
    01308         /* Draw the text under the vehicle instead of next to it, minus the
    01309          * height already allocated for the cargo of the first vehicle. */
    01310         uint height_extension = 15 - 11;
    01311
    01312         /* Add space for the cargo amount for each part. */
    01313         for (const Vehicle *u = v; u != NULL; u = u->Next()) {
    01314           if (u->cargo_cap != 0) height_extension += 11;
    01315         }
    01316
    01317         ResizeWindow(this, 0, height_extension);
    01318       } break;
    01319
    01320       case VEH_SHIP:
    01321         this->widget[VLD_WIDGET_RENAME_VEHICLE].tooltips = STR_982F_NAME_SHIP;
    01322         this->widget[VLD_WIDGET_CAPTION].data = STR_9811_DETAILS;
    01323         break;
    01324
    01325       case VEH_AIRCRAFT:
    01326         ResizeWindow(this, 0, 11);
    01327         this->widget[VLD_WIDGET_RENAME_VEHICLE].tooltips = STR_A032_NAME_AIRCRAFT;
    01328         this->widget[VLD_WIDGET_CAPTION].data = STR_A00C_DETAILS;
    01329         break;
    01330       default: NOT_REACHED();
    01331     }
    01332
    01333     if (v->type != VEH_TRAIN) {
    01334       this->vscroll.cap = 1;
    01335       this->widget[VLD_WIDGET_MIDDLE_DETAILS].right += 12;
    01336     }
    01337
    01338     this->widget[VLD_WIDGET_MIDDLE_DETAILS].data = (this->vscroll.cap << 8) + 1;
    01339     this->owner = v->owner;
    01340
    01341     this->tab = 0;
    01342
    01343     this->FindWindowPlacementAndResize(desc);
    01344   }
    01345
    01347   static bool IsVehicleServiceIntervalEnabled(const VehicleType vehicle_type)
    01348   {
    01349     switch (vehicle_type) {
    01350       default: NOT_REACHED();
    01351       case VEH_TRAIN:    return _settings_game.vehicle.servint_trains   != 0; break;
    01352       case VEH_ROAD:     return _settings_game.vehicle.servint_roadveh  != 0; break;
    01353       case VEH_SHIP:     return _settings_game.vehicle.servint_ships    != 0; break;
    01354       case VEH_AIRCRAFT: return _settings_game.vehicle.servint_aircraft != 0; break;
    01355     }
    01356     return false; // kill a compiler warning
    01357   }
    01358
    01369   static void DrawVehicleDetails(const Vehicle *v, int x, int y, int vscroll_pos, uint vscroll_cap, byte det_tab)
    01370   {
    01371     switch (v->type) {
    01372       case VEH_TRAIN:    DrawTrainDetails(v, x, y, vscroll_pos, vscroll_cap, det_tab);  break;
    01373       case VEH_ROAD:     DrawRoadVehDetails(v, x, y);  break;
    01374       case VEH_SHIP:     DrawShipDetails(v, x, y);     break;
    01375       case VEH_AIRCRAFT: DrawAircraftDetails(v, x, y); break;
    01376       default: NOT_REACHED();
    01377     }
    01378   }
    01379
    01381   virtual void OnPaint()
    01382   {
    01383     const Vehicle *v = GetVehicle(this->window_number);
    01384     byte det_tab = this->tab;
    01385
    01386     this->SetWidgetDisabledState(VLD_WIDGET_RENAME_VEHICLE, v->owner != _local_company);
    01387
    01388     if (v->type == VEH_TRAIN) {
    01389       this->DisableWidget(det_tab + VLD_WIDGET_DETAILS_CARGO_CARRIED);
    01390       SetVScrollCount(this, GetTrainDetailsWndVScroll(v->index, det_tab));
    01391     }
    01392
    01393     this->SetWidgetsHiddenState(v->type != VEH_TRAIN,
    01394       VLD_WIDGET_SCROLLBAR,
    01395       VLD_WIDGET_DETAILS_CARGO_CARRIED,
    01396       VLD_WIDGET_DETAILS_TRAIN_VEHICLES,
    01397       VLD_WIDGET_DETAILS_CAPACITY_OF_EACH,
    01398       VLD_WIDGET_DETAILS_TOTAL_CARGO,
    01399       VLD_WIDGET_RESIZE,
    01400       WIDGET_LIST_END);
    01401
    01402     /* Disable service-scroller when interval is set to disabled */
    01403     this->SetWidgetsDisabledState(!IsVehicleServiceIntervalEnabled(v->type),
    01404       VLD_WIDGET_INCREASE_SERVICING_INTERVAL,
    01405       VLD_WIDGET_DECREASE_SERVICING_INTERVAL,
    01406       WIDGET_LIST_END);
    01407
    01408
    01409     SetDParam(0, v->index);
    01410     this->DrawWidgets();
    01411
    01412     /* Draw running cost */
    01413     SetDParam(1, v->age / DAYS_IN_LEAP_YEAR);
    01414     SetDParam(0, (v->age + DAYS_IN_YEAR < v->max_age) ? STR_AGE : STR_AGE_RED);
    01415     SetDParam(2, v->max_age / DAYS_IN_LEAP_YEAR);
    01416     SetDParam(3, v->GetDisplayRunningCost());
    01417     DrawString(2, 15, _vehicle_translation_table[VST_VEHICLE_AGE_RUNNING_COST_YR][v->type], TC_FROMSTRING);
    01418
    01419     /* Draw max speed */
    01420     switch (v->type) {
    01421       case VEH_TRAIN:
    01422         SetDParam(2, v->GetDisplayMaxSpeed());
    01423         SetDParam(1, v->u.rail.cached_power);
    01424         SetDParam(0, v->u.rail.cached_weight);
    01425         SetDParam(3, v->u.rail.cached_max_te / 1000);
    01426         DrawString(2, 25, (_settings_game.vehicle.train_acceleration_model != TAM_ORIGINAL && v->u.rail.railtype != RAILTYPE_MAGLEV) ?
    01427           STR_VEHICLE_INFO_WEIGHT_POWER_MAX_SPEED_MAX_TE :
    01428           STR_VEHICLE_INFO_WEIGHT_POWER_MAX_SPEED, TC_FROMSTRING);
    01429         break;
    01430
    01431       case VEH_ROAD:
    01432       case VEH_SHIP:
    01433       case VEH_AIRCRAFT:
    01434         SetDParam(0, v->GetDisplayMaxSpeed());
    01435         DrawString(2, 25, _vehicle_translation_table[VST_VEHICLE_MAX_SPEED][v->type], TC_FROMSTRING);
    01436         break;
    01437
    01438       default: NOT_REACHED();
    01439     }
    01440
    01441     /* Draw profit */
    01442     SetDParam(0, v->GetDisplayProfitThisYear());
    01443     SetDParam(1, v->GetDisplayProfitLastYear());
    01444     DrawString(2, 35, _vehicle_translation_table[VST_VEHICLE_PROFIT_THIS_YEAR_LAST_YEAR][v->type], TC_FROMSTRING);
    01445
    01446     /* Draw breakdown & reliability */
    01447     SetDParam(0, v->reliability * 100 >> 16);
    01448     SetDParam(1, v->breakdowns_since_last_service);
    01449     DrawString(2, 45, _vehicle_translation_table[VST_VEHICLE_RELIABILITY_BREAKDOWNS][v->type], TC_FROMSTRING);
    01450
    01451     /* Draw service interval text */
    01452     SetDParam(0, v->service_interval);
    01453     SetDParam(1, v->date_of_last_service);
    01454     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);
    01455
    01456     switch (v->type) {
    01457       case VEH_TRAIN:
    01458         DrawVehicleDetails(v, 2, 57, this->vscroll.pos, this->vscroll.cap, det_tab);
    01459         break;
    01460
    01461       case VEH_ROAD:
    01462       case VEH_SHIP:
    01463       case VEH_AIRCRAFT:
    01464         DrawVehicleImage(v, 3, 57, INVALID_VEHICLE, 0, 0);
    01465         DrawVehicleDetails(v, 75, 57, this->vscroll.pos, this->vscroll.cap, det_tab);
    01466         break;
    01467
    01468       default: NOT_REACHED();
    01469     }
    01470   }
    01471
    01472   virtual void OnClick(Point pt, int widget)
    01473   {
    01475     static const StringID _name_vehicle_title[] = {
    01476       STR_8865_NAME_TRAIN,
    01477       STR_902C_NAME_ROAD_VEHICLE,
    01478       STR_9831_NAME_SHIP,
    01479       STR_A030_NAME_AIRCRAFT
    01480     };
    01481
    01482     switch (widget) {
    01483       case VLD_WIDGET_RENAME_VEHICLE: {// rename
    01484         const Vehicle *v = GetVehicle(this->window_number);
    01485         SetDParam(0, v->index);
    01486         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);
    01487       } break;
    01488
    01489       case VLD_WIDGET_INCREASE_SERVICING_INTERVAL:   // increase int
    01490       case VLD_WIDGET_DECREASE_SERVICING_INTERVAL: { // decrease int
    01491         int mod = _ctrl_pressed ? 5 : 10;
    01492         const Vehicle *v = GetVehicle(this->window_number);
    01493
    01494         mod = (widget == VLD_WIDGET_DECREASE_SERVICING_INTERVAL) ? -mod : mod;
    01495         mod = GetServiceIntervalClamped(mod + v->service_interval);
    01496         if (mod == v->service_interval) return;
    01497
    01498         DoCommandP(v->tile, v->index, mod, CMD_CHANGE_SERVICE_INT | CMD_MSG(STR_018A_CAN_T_CHANGE_SERVICING));
    01499       } break;
    01500
    01501       case VLD_WIDGET_DETAILS_CARGO_CARRIED:
    01502       case VLD_WIDGET_DETAILS_TRAIN_VEHICLES:
    01503       case VLD_WIDGET_DETAILS_CAPACITY_OF_EACH:
    01504       case VLD_WIDGET_DETAILS_TOTAL_CARGO:
    01505         this->SetWidgetsDisabledState(false,
    01506           VLD_WIDGET_DETAILS_CARGO_CARRIED,
    01507           VLD_WIDGET_DETAILS_TRAIN_VEHICLES,
    01508           VLD_WIDGET_DETAILS_CAPACITY_OF_EACH,
    01509           VLD_WIDGET_DETAILS_TOTAL_CARGO,
    01510           widget,
    01511           WIDGET_LIST_END);
    01512
    01513         this->tab = widget - VLD_WIDGET_DETAILS_CARGO_CARRIED;
    01514         this->SetDirty();
    01515         break;
    01516     }
    01517   }
    01518
    01519   virtual void OnQueryTextFinished(char *str)
    01520   {
    01522     static const StringID _name_vehicle_error[] = {
    01523       STR_8866_CAN_T_NAME_TRAIN,
    01524       STR_902D_CAN_T_NAME_ROAD_VEHICLE,
    01525       STR_9832_CAN_T_NAME_SHIP,
    01526       STR_A031_CAN_T_NAME_AIRCRAFT
    01527     };
    01528
    01529     if (str == NULL) return;
    01530
    01531     DoCommandP(0, this->window_number, 0, CMD_RENAME_VEHICLE | CMD_MSG(_name_vehicle_error[GetVehicle(this->window_number)->type]), NULL, str);
    01532   }
    01533
    01534   virtual void OnResize(Point new_size, Point delta)
    01535   {
    01536     if (delta.x != 0) ResizeButtons(this, VLD_WIDGET_DETAILS_CARGO_CARRIED, VLD_WIDGET_DETAILS_TOTAL_CARGO);
    01537     if (delta.y == 0) return;
    01538
    01539     this->vscroll.cap += delta.y / 14;
    01540     this->widget[VLD_WIDGET_MIDDLE_DETAILS].data = (this->vscroll.cap << 8) + 1;
    01541   }
    01542 };
    01543
    01545 static const WindowDesc _vehicle_details_desc(
    01546   WDP_AUTO, WDP_AUTO, 405, 113, 405, 113,
    01547   WC_VEHICLE_DETAILS, WC_VEHICLE_VIEW,
    01548   WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS | WDF_STICKY_BUTTON | WDF_RESIZABLE,
    01549   _vehicle_details_widgets
    01550 );
    01551
    01553 static void ShowVehicleDetailsWindow(const Vehicle *v)
    01554 {
    01555   DeleteWindowById(WC_VEHICLE_ORDERS, v->index, false);
    01556   DeleteWindowById(WC_VEHICLE_TIMETABLE, v->index, false);
    01557   AllocateWindowDescFront<VehicleDetailsWindow>(&_vehicle_details_desc, v->index);
    01558 }
    01559
    01560
    01561 /* Unified vehicle GUI - Vehicle View Window */
    01562
    01564 static const Widget _vehicle_view_widgets[] = {
    01565   {   WWT_CLOSEBOX,  RESIZE_NONE,  COLOUR_GREY,   0,  10,   0,  13, STR_00C5,                 STR_018B_CLOSE_WINDOW },           // VVW_WIDGET_CLOSEBOX
    01566   {    WWT_CAPTION, RESIZE_RIGHT,  COLOUR_GREY,  11, 237,   0,  13, 0x0 /* filled later */,   STR_018C_WINDOW_TITLE_DRAG_THIS }, // VVW_WIDGET_CAPTION
    01567   {  WWT_STICKYBOX,    RESIZE_LR,  COLOUR_GREY, 238, 249,   0,  13, 0x0,                      STR_STICKY_BUTTON },               // VVW_WIDGET_STICKY
    01568   {      WWT_PANEL,    RESIZE_RB,  COLOUR_GREY,   0, 231,  14, 103, 0x0,                      STR_NULL },                        // VVW_WIDGET_PANEL
    01569   {      WWT_INSET,    RESIZE_RB,  COLOUR_GREY,   2, 229,  16, 101, 0x0,                      STR_NULL },                        // VVW_WIDGET_VIEWPORT
    01570   {    WWT_PUSHBTN,   RESIZE_RTB,  COLOUR_GREY,   0, 237, 104, 115, 0x0,                      0x0 /* filled later */ },          // VVW_WIDGET_START_STOP_VEH
    01571   { WWT_PUSHIMGBTN,    RESIZE_LR,  COLOUR_GREY, 232, 249,  14,  31, SPR_CENTRE_VIEW_VEHICLE,  0x0 /* filled later */ },          // VVW_WIDGET_CENTER_MAIN_VIEH
    01572   { WWT_PUSHIMGBTN,    RESIZE_LR,  COLOUR_GREY, 232, 249,  32,  49, 0x0 /* filled later */,   0x0 /* filled later */ },          // VVW_WIDGET_GOTO_DEPOT
    01573   { WWT_PUSHIMGBTN,    RESIZE_LR,  COLOUR_GREY, 232, 249,  50,  67, SPR_REFIT_VEHICLE,        0x0 /* filled later */ },          // VVW_WIDGET_REFIT_VEH
    01574   { WWT_PUSHIMGBTN,    RESIZE_LR,  COLOUR_GREY, 232, 249,  68,  85, SPR_SHOW_ORDERS,          0x0 /* filled later */ },          // VVW_WIDGET_SHOW_ORDERS
    01575   { WWT_PUSHIMGBTN,    RESIZE_LR,  COLOUR_GREY, 232, 249,  86, 103, SPR_SHOW_VEHICLE_DETAILS, 0x0 /* filled later */ },          // VVW_WIDGET_SHOW_DETAILS
    01576   { WWT_PUSHIMGBTN,    RESIZE_LR,  COLOUR_GREY, 232, 249,  32,  49, 0x0 /* filled later */,   0x0 /* filled later */ },          // VVW_WIDGET_CLONE_VEH
    01577   {      WWT_PANEL,   RESIZE_LRB,  COLOUR_GREY, 232, 249, 104, 103, 0x0,                      STR_NULL },                        // VVW_WIDGET_EMPTY_BOTTOM_RIGHT
    01578   {  WWT_RESIZEBOX,  RESIZE_LRTB,  COLOUR_GREY, 238, 249, 104, 115, 0x0,                      STR_NULL },                        // VVW_WIDGET_RESIZE
    01579   { 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
    01580   { WWT_PUSHIMGBTN,    RESIZE_LR,  COLOUR_GREY, 232, 249,  50,  67, SPR_IGNORE_SIGNALS,       STR_884A_FORCE_TRAIN_TO_PROCEED },       // VVW_WIDGET_FORCE_PROCEED
    01581 {   WIDGETS_END},
    01582 };
    01583
    01584
    01586 static const WindowDesc _vehicle_view_desc(
    01587   WDP_AUTO, WDP_AUTO, 250, 116, 250, 116,
    01588   WC_VEHICLE_VIEW, WC_NONE,
    01589   WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS | WDF_STICKY_BUTTON | WDF_RESIZABLE,
    01590   _vehicle_view_widgets
    01591 );
    01592
    01596 static const WindowDesc _train_view_desc(
    01597   WDP_AUTO, WDP_AUTO, 250, 134, 250, 134,
    01598   WC_VEHICLE_VIEW, WC_NONE,
    01599   WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS | WDF_STICKY_BUTTON | WDF_RESIZABLE,
    01600   _vehicle_view_widgets
    01601 );
    01602
    01603
    01604 /* Just to make sure, nobody has changed the vehicle type constants, as we are
    01605    using them for array indexing in a number of places here. */
    01606 assert_compile(VEH_TRAIN == 0);
    01607 assert_compile(VEH_ROAD == 1);
    01608 assert_compile(VEH_SHIP == 2);
    01609 assert_compile(VEH_AIRCRAFT == 3);
    01610
    01612 static const ZoomLevel _vehicle_view_zoom_levels[] = {
    01613   ZOOM_LVL_TRAIN,
    01614   ZOOM_LVL_ROADVEH,
    01615   ZOOM_LVL_SHIP,
    01616   ZOOM_LVL_AIRCRAFT,
    01617 };
    01618
    01619 /* Constants for geometry of vehicle view viewport */
    01620 static const int VV_VIEWPORT_X = 3;
    01621 static const int VV_VIEWPORT_Y = 17;
    01622 static const int VV_INITIAL_VIEWPORT_WIDTH = 226;
    01623 static const int VV_INITIAL_VIEWPORT_HEIGHT = 84;
    01624 static const int VV_INITIAL_VIEWPORT_HEIGHT_TRAIN = 102;
    01625
    01627 enum VehicleCommandTranslation {
    01628   VCT_CMD_START_STOP = 0,
    01629   VCT_CMD_GOTO_DEPOT,
    01630   VCT_CMD_CLONE_VEH,
    01631   VCT_CMD_TURN_AROUND,
    01632 };
    01633
    01635 static const uint32 _vehicle_command_translation_table[][4] = {
    01636   { // VCT_CMD_START_STOP
    01637     CMD_START_STOP_VEHICLE | CMD_MSG(STR_883B_CAN_T_STOP_START_TRAIN),
    01638     CMD_START_STOP_VEHICLE | CMD_MSG(STR_9015_CAN_T_STOP_START_ROAD_VEHICLE),
    01639     CMD_START_STOP_VEHICLE | CMD_MSG(STR_9818_CAN_T_STOP_START_SHIP),
    01640     CMD_START_STOP_VEHICLE | CMD_MSG(STR_A016_CAN_T_STOP_START_AIRCRAFT)
    01641   },
    01642   { // VCT_CMD_GOTO_DEPOT
    01643     /* TrainGotoDepot has a nice randomizer in the pathfinder, which causes desyncs... */
    01644     CMD_SEND_TRAIN_TO_DEPOT | CMD_NO_TEST_IF_IN_NETWORK | CMD_MSG(STR_8830_CAN_T_SEND_TRAIN_TO_DEPOT),
    01645     CMD_SEND_ROADVEH_TO_DEPOT | CMD_MSG(STR_9018_CAN_T_SEND_VEHICLE_TO_DEPOT),
    01646     CMD_SEND_SHIP_TO_DEPOT | CMD_MSG(STR_9819_CAN_T_SEND_SHIP_TO_DEPOT),
    01647     CMD_SEND_AIRCRAFT_TO_HANGAR | CMD_MSG(STR_A012_CAN_T_SEND_AIRCRAFT_TO)
    01648   },
    01649   { // VCT_CMD_CLONE_VEH
    01650     CMD_CLONE_VEHICLE | CMD_MSG(STR_882B_CAN_T_BUILD_RAILROAD_VEHICLE),
    01651     CMD_CLONE_VEHICLE | CMD_MSG(STR_9009_CAN_T_BUILD_ROAD_VEHICLE),
    01652     CMD_CLONE_VEHICLE | CMD_MSG(STR_980D_CAN_T_BUILD_SHIP),
    01653     CMD_CLONE_VEHICLE | CMD_MSG(STR_A008_CAN_T_BUILD_AIRCRAFT)
    01654   },
    01655   { // VCT_CMD_TURN_AROUND
    01656     CMD_REVERSE_TRAIN_DIRECTION | CMD_MSG(STR_8869_CAN_T_REVERSE_DIRECTION),
    01657     CMD_TURN_ROADVEH | CMD_MSG(STR_9033_CAN_T_MAKE_VEHICLE_TURN),
    01658     0xffffffff, // invalid for ships
    01659     0xffffffff  // invalid for aircrafts
    01660   },
    01661 };
    01662
    01664 static bool IsVehicleRefitable(const Vehicle *v)
    01665 {
    01666   if (!v->IsStoppedInDepot()) return false;
    01667
    01668   do {
    01669     if (IsEngineRefittable(v->engine_type)) return true;
    01670   } while ((v->type == VEH_TRAIN || v->type == VEH_ROAD) && (v = v->Next()) != NULL);
    01671
    01672   return false;
    01673 }
    01674
    01675 struct VehicleViewWindow : Window {
    01676   VehicleViewWindow(const WindowDesc *desc, WindowNumber window_number) : Window(desc, window_number)
    01677   {
    01678     const Vehicle *v = GetVehicle(this->window_number);
    01679
    01680     this->owner = v->owner;
    01681     InitializeWindowViewport(this, VV_VIEWPORT_X, VV_VIEWPORT_Y, VV_INITIAL_VIEWPORT_WIDTH,
    01682                          (v->type == VEH_TRAIN) ? VV_INITIAL_VIEWPORT_HEIGHT_TRAIN : VV_INITIAL_VIEWPORT_HEIGHT,
    01683                          this->window_number | (1 << 31), _vehicle_view_zoom_levels[v->type]);
    01684
    01685     /*
    01686      * fill in data and tooltip codes for the widgets and
    01687      * move some of the buttons for trains
    01688      */
    01689     switch (v->type) {
    01690       case VEH_TRAIN:
    01691         this->widget[VVW_WIDGET_CAPTION].data = STR_882E;
    01692
    01693         this->widget[VVW_WIDGET_START_STOP_VEH].tooltips = STR_8846_CURRENT_TRAIN_ACTION_CLICK;
    01694
    01695         this->widget[VVW_WIDGET_CENTER_MAIN_VIEH].tooltips = STR_8848_CENTER_MAIN_VIEW_ON_TRAIN;
    01696
    01697         this->widget[VVW_WIDGET_GOTO_DEPOT].data = SPR_SEND_TRAIN_TODEPOT;
    01698         this->widget[VVW_WIDGET_GOTO_DEPOT].tooltips = STR_8849_SEND_TRAIN_TO_DEPOT;
    01699
    01700         this->widget[VVW_WIDGET_REFIT_VEH].tooltips = STR_RAIL_REFIT_VEHICLE_TO_CARRY;
    01701
    01702         this->widget[VVW_WIDGET_SHOW_ORDERS].tooltips = STR_8847_SHOW_TRAIN_S_ORDERS;
    01703
    01704         this->widget[VVW_WIDGET_SHOW_DETAILS].tooltips = STR_884C_SHOW_TRAIN_DETAILS;
    01705
    01706         this->widget[VVW_WIDGET_CLONE_VEH].data = SPR_CLONE_TRAIN;
    01707         this->widget[VVW_WIDGET_CLONE_VEH].tooltips = STR_CLONE_TRAIN_INFO;
    01708
    01709         this->widget[VVW_WIDGET_TURN_AROUND].tooltips = STR_884B_REVERSE_DIRECTION_OF_TRAIN;
    01710
    01711
    01712         /* due to more buttons we must modify the layout a bit for trains */
    01713         this->widget[VVW_WIDGET_PANEL].bottom = 121;
    01714         this->widget[VVW_WIDGET_VIEWPORT].bottom = 119;
    01715
    01716         this->widget[VVW_WIDGET_START_STOP_VEH].top = 122;
    01717         this->widget[VVW_WIDGET_START_STOP_VEH].bottom = 133;
    01718
    01719         this->widget[VVW_WIDGET_REFIT_VEH].top = 68;
    01720         this->widget[VVW_WIDGET_REFIT_VEH].bottom = 85;
    01721
    01722         this->widget[VVW_WIDGET_SHOW_ORDERS].top = 86;
    01723         this->widget[VVW_WIDGET_SHOW_ORDERS].bottom = 103;
    01724
    01725         this->widget[VVW_WIDGET_SHOW_DETAILS].top = 104;
    01726         this->widget[VVW_WIDGET_SHOW_DETAILS].bottom = 121;
    01727
    01728         this->widget[VVW_WIDGET_EMPTY_BOTTOM_RIGHT].top = 122;
    01729         this->widget[VVW_WIDGET_EMPTY_BOTTOM_RIGHT].bottom = 121;
    01730
    01731         this->widget[VVW_WIDGET_RESIZE].top = 122;
    01732         this->widget[VVW_WIDGET_RESIZE].bottom = 133;
    01733
    01734         this->widget[VVW_WIDGET_TURN_AROUND].top = 68;
    01735         this->widget[VVW_WIDGET_TURN_AROUND].bottom = 85;
    01736         break;
    01737
    01738       case VEH_ROAD:
    01739         this->widget[VVW_WIDGET_CAPTION].data = STR_9002;
    01740
    01741         this->widget[VVW_WIDGET_START_STOP_VEH].tooltips = STR_901C_CURRENT_VEHICLE_ACTION;
    01742
    01743         this->widget[VVW_WIDGET_CENTER_MAIN_VIEH].tooltips = STR_901E_CENTER_MAIN_VIEW_ON_VEHICLE;
    01744
    01745         this->widget[VVW_WIDGET_GOTO_DEPOT].data = SPR_SEND_ROADVEH_TODEPOT;
    01746         this->widget[VVW_WIDGET_GOTO_DEPOT].tooltips = STR_901F_SEND_VEHICLE_TO_DEPOT;
    01747
    01748         this->widget[VVW_WIDGET_REFIT_VEH].tooltips = STR_REFIT_ROAD_VEHICLE_TO_CARRY;
    01749
    01750         this->widget[VVW_WIDGET_SHOW_ORDERS].tooltips = STR_901D_SHOW_VEHICLE_S_ORDERS;
    01751
    01752         this->widget[VVW_WIDGET_SHOW_DETAILS].tooltips = STR_9021_SHOW_ROAD_VEHICLE_DETAILS;
    01753
    01754         this->widget[VVW_WIDGET_CLONE_VEH].data = SPR_CLONE_ROADVEH;
    01755         this->widget[VVW_WIDGET_CLONE_VEH].tooltips = STR_CLONE_ROAD_VEHICLE_INFO;
    01756
    01757         this->SetWidgetHiddenState(VVW_WIDGET_FORCE_PROCEED, true);
    01758         break;
    01759
    01760       case VEH_SHIP:
    01761         this->widget[VVW_WIDGET_CAPTION].data = STR_980F;
    01762
    01763         this->widget[VVW_WIDGET_START_STOP_VEH].tooltips = STR_9827_CURRENT_SHIP_ACTION_CLICK;
    01764
    01765         this->widget[VVW_WIDGET_CENTER_MAIN_VIEH].tooltips = STR_9829_CENTER_MAIN_VIEW_ON_SHIP;
    01766
    01767         this->widget[VVW_WIDGET_GOTO_DEPOT].data = SPR_SEND_SHIP_TODEPOT;
    01768         this->widget[VVW_WIDGET_GOTO_DEPOT].tooltips = STR_982A_SEND_SHIP_TO_DEPOT;
    01769
    01770         this->widget[VVW_WIDGET_REFIT_VEH].tooltips = STR_983A_REFIT_CARGO_SHIP_TO_CARRY;
    01771
    01772         this->widget[VVW_WIDGET_SHOW_ORDERS].tooltips = STR_9828_SHOW_SHIP_S_ORDERS;
    01773
    01774         this->widget[VVW_WIDGET_SHOW_DETAILS].tooltips = STR_982B_SHOW_SHIP_DETAILS;
    01775
    01776         this->widget[VVW_WIDGET_CLONE_VEH].data = SPR_CLONE_SHIP;
    01777         this->widget[VVW_WIDGET_CLONE_VEH].tooltips = STR_CLONE_SHIP_INFO;
    01778
    01779         this->SetWidgetsHiddenState(true,
    01780                                     VVW_WIDGET_TURN_AROUND,
    01781                                     VVW_WIDGET_FORCE_PROCEED,
    01782                                     WIDGET_LIST_END);
    01783         break;
    01784
    01785       case VEH_AIRCRAFT:
    01786         this->widget[VVW_WIDGET_CAPTION].data = STR_A00A;
    01787
    01788         this->widget[VVW_WIDGET_START_STOP_VEH].tooltips = STR_A027_CURRENT_AIRCRAFT_ACTION;
    01789
    01790         this->widget[VVW_WIDGET_CENTER_MAIN_VIEH].tooltips = STR_A029_CENTER_MAIN_VIEW_ON_AIRCRAFT;
    01791
    01792         this->widget[VVW_WIDGET_GOTO_DEPOT].data = SPR_SEND_AIRCRAFT_TODEPOT;
    01793         this->widget[VVW_WIDGET_GOTO_DEPOT].tooltips = STR_A02A_SEND_AIRCRAFT_TO_HANGAR;
    01794
    01795         this->widget[VVW_WIDGET_REFIT_VEH].tooltips = STR_A03B_REFIT_AIRCRAFT_TO_CARRY;
    01796
    01797         this->widget[VVW_WIDGET_SHOW_ORDERS].tooltips = STR_A028_SHOW_AIRCRAFT_S_ORDERS;
    01798
    01799         this->widget[VVW_WIDGET_SHOW_DETAILS].tooltips = STR_A02B_SHOW_AIRCRAFT_DETAILS;
    01800
    01801         this->widget[VVW_WIDGET_CLONE_VEH].data = SPR_CLONE_AIRCRAFT;
    01802         this->widget[VVW_WIDGET_CLONE_VEH].tooltips = STR_CLONE_AIRCRAFT_INFO;
    01803
    01804         this->SetWidgetsHiddenState(true,
    01805                                     VVW_WIDGET_TURN_AROUND,
    01806                                     VVW_WIDGET_FORCE_PROCEED,
    01807                                     WIDGET_LIST_END);
    01808         break;
    01809
    01810         default: NOT_REACHED();
    01811     }
    01812
    01813     this->FindWindowPlacementAndResize(desc);
    01814   }
    01815
    01816   ~VehicleViewWindow()
    01817   {
    01818     DeleteWindowById(WC_VEHICLE_ORDERS, this->window_number, false);
    01819     DeleteWindowById(WC_VEHICLE_REFIT, this->window_number, false);
    01820     DeleteWindowById(WC_VEHICLE_DETAILS, this->window_number, false);
    01821     DeleteWindowById(WC_VEHICLE_TIMETABLE, this->window_number, false);
    01822   }
    01823
    01824   virtual void OnPaint()
    01825   {
    01827     static const StringID _heading_for_depot_strings[] = {
    01828       STR_HEADING_FOR_TRAIN_DEPOT,
    01829       STR_HEADING_FOR_ROAD_DEPOT,
    01830       STR_HEADING_FOR_SHIP_DEPOT,
    01831       STR_HEADING_FOR_HANGAR,
    01832     };
    01833
    01835     static const StringID _heading_for_depot_service_strings[] = {
    01836       STR_HEADING_FOR_TRAIN_DEPOT_SERVICE,
    01837       STR_HEADING_FOR_ROAD_DEPOT_SERVICE,
    01838       STR_HEADING_FOR_SHIP_DEPOT_SERVICE,
    01839       STR_HEADING_FOR_HANGAR_SERVICE,
    01840     };
    01841
    01842     const Vehicle *v = GetVehicle(this->window_number);
    01843     StringID str;
    01844     bool is_localcompany = v->owner == _local_company;
    01845     bool refitable_and_stopped_in_depot = IsVehicleRefitable(v);
    01846
    01847     this->SetWidgetDisabledState(VVW_WIDGET_GOTO_DEPOT, !is_localcompany);
    01848     this->SetWidgetDisabledState(VVW_WIDGET_REFIT_VEH,
    01849                                 !refitable_and_stopped_in_depot || !is_localcompany);
    01850     this->SetWidgetDisabledState(VVW_WIDGET_CLONE_VEH, !is_localcompany);
    01851
    01852     if (v->type == VEH_TRAIN) {
    01853       this->SetWidgetDisabledState(VVW_WIDGET_FORCE_PROCEED, !is_localcompany);
    01854       this->SetWidgetDisabledState(VVW_WIDGET_TURN_AROUND, !is_localcompany);
    01855     }
    01856
    01857     /* draw widgets & caption */
    01858     SetDParam(0, v->index);
    01859     this->DrawWidgets();
    01860
    01861     if (v->vehstatus & VS_CRASHED) {
    01862       str = STR_8863_CRASHED;
    01863     } else if (v->type != VEH_AIRCRAFT && v->breakdown_ctr == 1) { // check for aircraft necessary?
    01864       str = STR_885C_BROKEN_DOWN;
    01865     } else if (v->vehstatus & VS_STOPPED) {
    01866       if (v->type == VEH_TRAIN) {
    01867         if (v->cur_speed == 0) {
    01868           if (v->u.rail.cached_power == 0) {
    01869             str = STR_TRAIN_NO_POWER;
    01870           } else {
    01871             str = STR_8861_STOPPED;
    01872           }
    01873         } else {
    01874           SetDParam(0, v->GetDisplaySpeed());
    01875           str = STR_TRAIN_STOPPING + _settings_client.gui.vehicle_speed;
    01876         }
    01877       } else { // no train
    01878         str = STR_8861_STOPPED;
    01879       }
    01880     } else if (v->type == VEH_TRAIN && HasBit(v->u.rail.flags, VRF_TRAIN_STUCK)) {
    01881       str = STR_TRAIN_STUCK;
    01882     } else { // vehicle is in a "normal" state, show current order
    01883       switch (v->current_order.GetType()) {
    01884         case OT_GOTO_STATION: {
    01885           SetDParam(0, v->current_order.GetDestination());
    01886           SetDParam(1, v->GetDisplaySpeed());
    01887           str = STR_HEADING_FOR_STATION + _settings_client.gui.vehicle_speed;
    01888         } break;
    01889
    01890         case OT_GOTO_DEPOT: {
    01891           if (v->type == VEH_AIRCRAFT) {
    01892             /* Aircrafts always go to a station, even if you say depot */
    01893             SetDParam(0, v->current_order.GetDestination());
    01894             SetDParam(1, v->GetDisplaySpeed());
    01895           } else {
    01896             Depot *depot = GetDepot(v->current_order.GetDestination());
    01897             SetDParam(0, depot->town_index);
    01898             SetDParam(1, v->GetDisplaySpeed());
    01899           }
    01900           if (v->current_order.GetDepotActionType() & ODATFB_HALT) {
    01901             str = _heading_for_depot_strings[v->type] + _settings_client.gui.vehicle_speed;
    01902           } else {
    01903             str = _heading_for_depot_service_strings[v->type] + _settings_client.gui.vehicle_speed;
    01904           }
    01905         } break;
    01906
    01907         case OT_LOADING:
    01908           str = STR_882F_LOADING_UNLOADING;
    01909           break;
    01910
    01911         case OT_GOTO_WAYPOINT: {
    01912           assert(v->type == VEH_TRAIN);
    01913           SetDParam(0, v->current_order.GetDestination());
    01914           str = STR_HEADING_FOR_WAYPOINT + _settings_client.gui.vehicle_speed;
    01915           SetDParam(1, v->GetDisplaySpeed());
    01916           break;
    01917         }
    01918
    01919         case OT_LEAVESTATION:
    01920           if (v->type != VEH_AIRCRAFT) {
    01921             str = STR_LEAVING;
    01922             break;
    01923           }
    01924           /* fall-through if aircraft. Does this even happen? */
    01925
    01926         default:
    01927           if (v->GetNumOrders() == 0) {
    01928             str = STR_NO_ORDERS + _settings_client.gui.vehicle_speed;
    01929             SetDParam(0, v->GetDisplaySpeed());
    01930           } else {
    01931             str = STR_EMPTY;
    01932           }
    01933           break;
    01934       }
    01935     }
    01936
    01937     /* draw the flag plus orders */
    01938     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);
    01939     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);
    01940     this->DrawViewport();
    01941   }
    01942
    01943   virtual void OnClick(Point pt, int widget)
    01944   {
    01945     const Vehicle *v = GetVehicle(this->window_number);
    01946
    01947     switch (widget) {
    01948       case VVW_WIDGET_START_STOP_VEH: // start stop
    01949         DoCommandP(v->tile, v->index, 0,
    01950                     _vehicle_command_translation_table[VCT_CMD_START_STOP][v->type]);
    01951         break;
    01952       case VVW_WIDGET_CENTER_MAIN_VIEH: {// center main view
    01953         const Window *mainwindow = FindWindowById(WC_MAIN_WINDOW, 0);
    01954         /* code to allow the main window to 'follow' the vehicle if the ctrl key is pressed */
    01955         if (_ctrl_pressed && mainwindow->viewport->zoom == ZOOM_LVL_NORMAL) {
    01956           mainwindow->viewport->follow_vehicle = v->index;
    01957         } else {
    01958           ScrollMainWindowTo(v->x_pos, v->y_pos, v->z_pos);
    01959         }
    01960       } break;
    01961
    01962       case VVW_WIDGET_GOTO_DEPOT: // goto hangar
    01963         DoCommandP(v->tile, v->index, _ctrl_pressed ? DEPOT_SERVICE : 0,
    01964           _vehicle_command_translation_table[VCT_CMD_GOTO_DEPOT][v->type]);
    01965         break;
    01966       case VVW_WIDGET_REFIT_VEH: // refit
    01967         ShowVehicleRefitWindow(v, INVALID_VEH_ORDER_ID, this);
    01968         break;
    01969       case VVW_WIDGET_SHOW_ORDERS: // show orders
    01970         if (_ctrl_pressed) {
    01971           ShowTimetableWindow(v);
    01972         } else {
    01973           ShowOrdersWindow(v);
    01974         }
    01975         break;
    01976       case VVW_WIDGET_SHOW_DETAILS: // show details
    01977         ShowVehicleDetailsWindow(v);
    01978         break;
    01979       case VVW_WIDGET_CLONE_VEH: // clone vehicle
    01980         DoCommandP(v->tile, v->index, _ctrl_pressed ? 1 : 0,
    01981                     _vehicle_command_translation_table[VCT_CMD_CLONE_VEH][v->type],
    01982                     CcCloneVehicle);
    01983         break;
    01984       case VVW_WIDGET_TURN_AROUND: // turn around
    01985         assert(v->type == VEH_TRAIN || v->type == VEH_ROAD);
    01986         DoCommandP(v->tile, v->index, 0,
    01987                     _vehicle_command_translation_table[VCT_CMD_TURN_AROUND][v->type]);
    01988         break;
    01989       case VVW_WIDGET_FORCE_PROCEED: // force proceed
    01990         assert(v->type == VEH_TRAIN);
    01991         DoCommandP(v->tile, v->index, 0, CMD_FORCE_TRAIN_PROCEED | CMD_MSG(STR_8862_CAN_T_MAKE_TRAIN_PASS_SIGNAL));
    01992         break;
    01993     }
    01994   }
    01995
    01996   virtual void OnResize(Point new_size, Point delta)
    01997   {
    01998     this->viewport->width          += delta.x;
    01999     this->viewport->height         += delta.y;
    02000     this->viewport->virtual_width  += delta.x;
    02001     this->viewport->virtual_height += delta.y;
    02002   }
    02003
    02004   virtual void OnTick()
    02005   {
    02006     const Vehicle *v = GetVehicle(this->window_number);
    02007     bool veh_stopped = v->IsStoppedInDepot();
    02008
    02009     /* Widget VVW_WIDGET_GOTO_DEPOT must be hidden if the vehicle is already
    02010      * stopped in depot.
    02011      * Widget VVW_WIDGET_CLONE_VEH should then be shown, since cloning is
    02012      * allowed only while in depot and stopped.
    02013      * This sytem allows to have two buttons, on top of each other.
    02014      * The same system applies to widget VVW_WIDGET_REFIT_VEH and VVW_WIDGET_TURN_AROUND.*/
    02015     if (veh_stopped != this->IsWidgetHidden(VVW_WIDGET_GOTO_DEPOT) || veh_stopped == this->IsWidgetHidden(VVW_WIDGET_CLONE_VEH)) {
    02016       this->SetWidgetHiddenState( VVW_WIDGET_GOTO_DEPOT, veh_stopped);  // send to depot
    02017       this->SetWidgetHiddenState(VVW_WIDGET_CLONE_VEH, !veh_stopped); // clone
    02018       if (v->type == VEH_ROAD || v->type == VEH_TRAIN) {
    02019         this->SetWidgetHiddenState( VVW_WIDGET_REFIT_VEH, !veh_stopped); // refit
    02020         this->SetWidgetHiddenState(VVW_WIDGET_TURN_AROUND, veh_stopped);  // force turn around
    02021       }
    02022       this->SetDirty();
    02023     }
    02024   }
    02025 };
    02026
    02027
    02029 void ShowVehicleViewWindow(const Vehicle *v)
    02030 {
    02031   AllocateWindowDescFront<VehicleViewWindow>((v->type == VEH_TRAIN) ? &_train_view_desc : &_vehicle_view_desc, v->index);
    02032 }
    02033
    02034 void StopGlobalFollowVehicle(const Vehicle *v)
    02035 {
    02036   Window *w = FindWindowById(WC_MAIN_WINDOW, 0);
    02037   if (w != NULL && w->viewport->follow_vehicle == v->index) {
    02038     ScrollMainWindowTo(v->x_pos, v->y_pos, v->z_pos, true); // lock the main view on the vehicle's last position
    02039     w->viewport->follow_vehicle = INVALID_VEHICLE;
    02040   }
    02041 }