# vehicle_gui.cpp

Go to the documentation of this file.

    00001 /* $Id: vehicle_gui.cpp 22152 2011-02-26 20:13:14Z rubidium $ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
    00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
    00008  */
    00009
    00012 #include "stdafx.h"
    00013 #include "debug.h"
    00014 #include "company_func.h"
    00015 #include "gui.h"
    00016 #include "textbuf_gui.h"
    00017 #include "command_func.h"
    00018 #include "vehicle_gui.h"
    00019 #include "vehicle_gui_base.h"
    00020 #include "viewport_func.h"
    00021 #include "newgrf_text.h"
    00022 #include "newgrf_debug.h"
    00023 #include "roadveh.h"
    00024 #include "train.h"
    00025 #include "aircraft.h"
    00026 #include "depot_map.h"
    00027 #include "group_gui.h"
    00028 #include "strings_func.h"
    00029 #include "window_func.h"
    00030 #include "vehicle_func.h"
    00031 #include "autoreplace_gui.h"
    00032 #include "string_func.h"
    00033 #include "widgets/dropdown_func.h"
    00034 #include "timetable.h"
    00035 #include "articulated_vehicles.h"
    00036 #include "spritecache.h"
    00037 #include "core/geometry_func.hpp"
    00038 #include "company_base.h"
    00039 #include "engine_func.h"
    00040 #include "station_base.h"
    00041 #include "tilehighlight_func.h"
    00042
    00043 #include "table/strings.h"
    00044
    00045 Sorting _sorting;
    00046
    00047 static GUIVehicleList::SortFunction VehicleNumberSorter;
    00048 static GUIVehicleList::SortFunction VehicleNameSorter;
    00049 static GUIVehicleList::SortFunction VehicleAgeSorter;
    00050 static GUIVehicleList::SortFunction VehicleProfitThisYearSorter;
    00051 static GUIVehicleList::SortFunction VehicleProfitLastYearSorter;
    00052 static GUIVehicleList::SortFunction VehicleCargoSorter;
    00053 static GUIVehicleList::SortFunction VehicleReliabilitySorter;
    00054 static GUIVehicleList::SortFunction VehicleMaxSpeedSorter;
    00055 static GUIVehicleList::SortFunction VehicleModelSorter;
    00056 static GUIVehicleList::SortFunction VehicleValueSorter;
    00057 static GUIVehicleList::SortFunction VehicleLengthSorter;
    00058 static GUIVehicleList::SortFunction VehicleTimeToLiveSorter;
    00059 static GUIVehicleList::SortFunction VehicleTimetableDelaySorter;
    00060
    00061 GUIVehicleList::SortFunction * const BaseVehicleListWindow::vehicle_sorter_funcs[] = {
    00062   &VehicleNumberSorter,
    00063   &VehicleNameSorter,
    00064   &VehicleAgeSorter,
    00065   &VehicleProfitThisYearSorter,
    00066   &VehicleProfitLastYearSorter,
    00067   &VehicleCargoSorter,
    00068   &VehicleReliabilitySorter,
    00069   &VehicleMaxSpeedSorter,
    00070   &VehicleModelSorter,
    00071   &VehicleValueSorter,
    00072   &VehicleLengthSorter,
    00073   &VehicleTimeToLiveSorter,
    00074   &VehicleTimetableDelaySorter,
    00075 };
    00076
    00077 const StringID BaseVehicleListWindow::vehicle_sorter_names[] = {
    00078   STR_SORT_BY_NUMBER,
    00079   STR_SORT_BY_NAME,
    00080   STR_SORT_BY_AGE,
    00081   STR_SORT_BY_PROFIT_THIS_YEAR,
    00082   STR_SORT_BY_PROFIT_LAST_YEAR,
    00083   STR_SORT_BY_TOTAL_CAPACITY_PER_CARGOTYPE,
    00084   STR_SORT_BY_RELIABILITY,
    00085   STR_SORT_BY_MAX_SPEED,
    00086   STR_SORT_BY_MODEL,
    00087   STR_SORT_BY_VALUE,
    00088   STR_SORT_BY_LENGTH,
    00089   STR_SORT_BY_LIFE_TIME,
    00090   STR_SORT_BY_TIMETABLE_DELAY,
    00091   INVALID_STRING_ID
    00092 };
    00093
    00094 const StringID BaseVehicleListWindow::vehicle_depot_name[] = {
    00095   STR_VEHICLE_LIST_SEND_TRAIN_TO_DEPOT,
    00096   STR_VEHICLE_LIST_SEND_ROAD_VEHICLE_TO_DEPOT,
    00097   STR_VEHICLE_LIST_SEND_SHIP_TO_DEPOT,
    00098   STR_VEHICLE_LIST_SEND_AIRCRAFT_TO_HANGAR
    00099 };
    00100
    00101 void BaseVehicleListWindow::BuildVehicleList()
    00102 {
    00103   if (!this->vehicles.NeedRebuild()) return;
    00104
    00105   DEBUG(misc, 3, "Building vehicle list type %d for company %d given index %d", this->vli.type, this->vli.company, this->vli.index);
    00106
    00107   GenerateVehicleSortList(&this->vehicles, this->vli);
    00108
    00109   uint unitnumber = 0;
    00110   for (const Vehicle **v = this->vehicles.Begin(); v != this->vehicles.End(); v++) {
    00111     unitnumber = max<uint>(unitnumber, (*v)->unitnumber);
    00112   }
    00113
    00114   /* Because 111 is much less wide than e.g. 999 we use the
    00115    * wider numbers to determine the width instead of just
    00116    * the random number that it seems to be. */
    00117   if (unitnumber >= 1000) {
    00118     this->unitnumber_digits = 4;
    00119   } else if (unitnumber >= 100) {
    00120     this->unitnumber_digits = 3;
    00121   } else {
    00122     this->unitnumber_digits = 2;
    00123   }
    00124
    00125   this->vehicles.RebuildDone();
    00126   this->vscroll->SetCount(this->vehicles.Length());
    00127 }
    00128
    00135 Dimension BaseVehicleListWindow::GetActionDropdownSize(bool show_autoreplace, bool show_group)
    00136 {
    00137   Dimension d = {0, 0};
    00138
    00139   if (show_autoreplace) d = maxdim(d, GetStringBoundingBox(STR_VEHICLE_LIST_REPLACE_VEHICLES));
    00140   d = maxdim(d, GetStringBoundingBox(STR_VEHICLE_LIST_SEND_FOR_SERVICING));
    00141   d = maxdim(d, GetStringBoundingBox(this->vehicle_depot_name[this->vli.vtype]));
    00142
    00143   if (show_group) {
    00144     d = maxdim(d, GetStringBoundingBox(STR_GROUP_ADD_SHARED_VEHICLE));
    00145     d = maxdim(d, GetStringBoundingBox(STR_GROUP_REMOVE_ALL_VEHICLES));
    00146   }
    00147
    00148   return d;
    00149 }
    00150
    00157 DropDownList *BaseVehicleListWindow::BuildActionDropdownList(bool show_autoreplace, bool show_group)
    00158 {
    00159   DropDownList *list = new DropDownList();
    00160
    00161   if (show_autoreplace) list->push_back(new DropDownListStringItem(STR_VEHICLE_LIST_REPLACE_VEHICLES, ADI_REPLACE, false));
    00162   list->push_back(new DropDownListStringItem(STR_VEHICLE_LIST_SEND_FOR_SERVICING, ADI_SERVICE, false));
    00163   list->push_back(new DropDownListStringItem(this->vehicle_depot_name[this->vli.vtype], ADI_DEPOT, false));
    00164
    00165   if (show_group) {
    00166     list->push_back(new DropDownListStringItem(STR_GROUP_ADD_SHARED_VEHICLE, ADI_ADD_SHARED, false));
    00167     list->push_back(new DropDownListStringItem(STR_GROUP_REMOVE_ALL_VEHICLES, ADI_REMOVE_ALL, false));
    00168   }
    00169
    00170   return list;
    00171 }
    00172
    00173 /* cached values for VehicleNameSorter to spare many GetString() calls */
    00174 static const Vehicle *_last_vehicle[2] = { NULL, NULL };
    00175
    00176 void BaseVehicleListWindow::SortVehicleList()
    00177 {
    00178   if (this->vehicles.Sort()) return;
    00179
    00180   /* invalidate cached values for name sorter - vehicle names could change */
    00181   _last_vehicle[0] = _last_vehicle[1] = NULL;
    00182 }
    00183
    00184 void DepotSortList(VehicleList *list)
    00185 {
    00186   if (list->Length() < 2) return;
    00187   QSortT(list->Begin(), list->Length(), &VehicleNumberSorter);
    00188 }
    00189
    00191 static void DrawVehicleProfitButton(const Vehicle *v, int x, int y)
    00192 {
    00193   SpriteID spr;
    00194
    00195   /* draw profit-based coloured icons */
    00196   if (v->age <= DAYS_IN_YEAR * 2) {
    00197     spr = SPR_PROFIT_NA;
    00198   } else if (v->GetDisplayProfitLastYear() < 0) {
    00199     spr = SPR_PROFIT_NEGATIVE;
    00200   } else if (v->GetDisplayProfitLastYear() < 10000) {
    00201     spr = SPR_PROFIT_SOME;
    00202   } else {
    00203     spr = SPR_PROFIT_LOT;
    00204   }
    00205   DrawSprite(spr, PAL_NONE, x, y);
    00206 }
    00207
    00209 static const uint MAX_REFIT_CYCLE = 256;
    00210
    00218 byte GetBestFittingSubType(Vehicle *v_from, Vehicle *v_for)
    00219 {
    00220   const Engine *e_from = Engine::Get(v_from->engine_type);
    00221   const Engine *e_for  = Engine::Get(v_for->engine_type);
    00222
    00223   /* If one them doesn't carry cargo, there's no need to find a sub type */
    00224   if (!e_from->CanCarryCargo() || !e_for->CanCarryCargo()) return 0;
    00225
    00226   if (!HasBit(e_from->info.callback_mask, CBM_VEHICLE_CARGO_SUFFIX) ||
    00227       !HasBit(e_for->info.callback_mask,  CBM_VEHICLE_CARGO_SUFFIX)) {
    00228     /* One of the engines doesn't have cargo suffixes, i.e. sub types. */
    00229     return 0;
    00230   }
    00231
    00232   /* It has to be possible for v_for to carry the cargo of v_from. */
    00233   if (!HasBit(e_for->info.refit_mask, v_from->cargo_type)) return 0;
    00234
    00235   StringID expected_string = GetCargoSubtypeText(v_from);
    00236
    00237   CargoID old_cargo_type = v_for->cargo_type;
    00238   byte old_cargo_subtype = v_for->cargo_subtype;
    00239   byte ret_refit_cyc = 0;
    00240
    00241   /* Set the 'destination' cargo */
    00242   v_for->cargo_type = v_from->cargo_type;
    00243
    00244   /* Cycle through the refits */
    00245   for (uint refit_cyc = 0; refit_cyc < MAX_REFIT_CYCLE; refit_cyc++) {
    00246     v_for->cargo_subtype = refit_cyc;
    00247
    00248     /* Make sure we don't pick up anything cached. */
    00249     v_for->First()->InvalidateNewGRFCache();
    00250     v_for->InvalidateNewGRFCache();
    00251     uint16 callback = GetVehicleCallback(CBID_VEHICLE_CARGO_SUFFIX, 0, 0, v_for->engine_type, v_for);
    00252
    00253     if (callback == 0xFF) callback = CALLBACK_FAILED;
    00254     if (callback == CALLBACK_FAILED) break;
    00255
    00256     if (GetCargoSubtypeText(v_for) != expected_string) continue;
    00257
    00258     /* We found something matching. */
    00259     ret_refit_cyc = refit_cyc;
    00260     break;
    00261   }
    00262
    00263   /* Reset the vehicle's cargo type */
    00264   v_for->cargo_type    = old_cargo_type;
    00265   v_for->cargo_subtype = old_cargo_subtype;
    00266
    00267   /* Make sure we don't taint the vehicle. */
    00268   v_for->First()->InvalidateNewGRFCache();
    00269   v_for->InvalidateNewGRFCache();
    00270
    00271   return ret_refit_cyc;
    00272 }
    00273
    00275 struct RefitOption {
    00276   CargoID cargo;
    00277   byte subtype;
    00278   uint16 value;
    00279   EngineID engine;
    00280
    00286   FORCEINLINE bool operator != (const RefitOption &other) const
    00287   {
    00288     return other.cargo != this->cargo || other.value != this->value;
    00289   }
    00290
    00296   FORCEINLINE bool operator == (const RefitOption &other) const
    00297   {
    00298     return other.cargo == this->cargo && other.value == this->value;
    00299   }
    00300 };
    00301
    00302 typedef SmallVector<RefitOption, 32> SubtypeList;
    00303
    00313 static void DrawVehicleRefitWindow(const SubtypeList list[NUM_CARGO], int sel, uint pos, uint rows, uint delta, const Rect &r)
    00314 {
    00315   uint y = r.top + WD_MATRIX_TOP;
    00316   uint current = 0;
    00317
    00318   /* Draw the list of subtypes for each cargo, and find the selected refit option (by its position). */
    00319   for (uint i = 0; current < pos + rows && i < NUM_CARGO; i++) {
    00320     for (uint j = 0; current < pos + rows && j < list[i].Length(); j++) {
    00321       /* Refit options with a position smaller than pos don't have to be drawn. */
    00322       if (current < pos) {
    00323         current++;
    00324         continue;
    00325       }
    00326
    00327       TextColour colour = (sel == (int)current) ? TC_WHITE : TC_BLACK;
    00328       const RefitOption refit = list[i][j];
    00329       /* Get the cargo name. */
    00330       SetDParam(0, CargoSpec::Get(refit.cargo)->name);
    00331       /* If the callback succeeded, draw the cargo suffix. */
    00332       if (refit.value != CALLBACK_FAILED) {
    00333         SetDParam(1, GetGRFStringID(GetEngineGRFID(refit.engine), 0xD000 + refit.value));
    00334         DrawString(r.left + WD_MATRIX_LEFT, r.right - WD_MATRIX_RIGHT, y, STR_JUST_STRING_SPACE_STRING, colour);
    00335       } else {
    00336         DrawString(r.left + WD_MATRIX_LEFT, r.right - WD_MATRIX_RIGHT, y, STR_JUST_STRING, colour);
    00337       }
    00338
    00339       y += delta;
    00340       current++;
    00341     }
    00342   }
    00343 }
    00344
    00346 enum VehicleRefitWidgets {
    00347   VRW_CAPTION,
    00348   VRW_VEHICLE_PANEL_DISPLAY,
    00349   VRW_SHOW_HSCROLLBAR,
    00350   VRW_HSCROLLBAR,
    00351   VRW_SELECTHEADER,
    00352   VRW_MATRIX,
    00353   VRW_SCROLLBAR,
    00354   VRW_INFOPANEL,
    00355   VRW_REFITBUTTON,
    00356 };
    00357
    00359 struct RefitWindow : public Window {
    00360   int sel;
    00361   RefitOption *cargo;
    00362   SubtypeList list[NUM_CARGO];
    00363   VehicleOrderID order;
    00364   uint information_width;
    00365   Scrollbar *vscroll;
    00366   Scrollbar *hscroll;
    00367   int vehicle_width;
    00368   int sprite_left;
    00369   int sprite_right;
    00370   uint vehicle_margin;
    00371   int click_x;
    00372   VehicleID selected_vehicle;
    00373   uint8 num_vehicles;
    00374
    00378   void BuildRefitList()
    00379   {
    00380     for (uint i = 0; i < NUM_CARGO; i++) this->list[i].Clear();
    00381     Vehicle *v = Vehicle::Get(this->window_number);
    00382
    00383     /* Check only the selected vehicles. */
    00384     VehicleSet vehicles_to_refit;
    00385     GetVehicleSet(vehicles_to_refit, Vehicle::Get(this->selected_vehicle), this->num_vehicles);
    00386
    00387     do {
    00388       if (v->type == VEH_TRAIN && !vehicles_to_refit.Contains(v->index)) continue;
    00389       const Engine *e = Engine::Get(v->engine_type);
    00390       uint32 cmask = e->info.refit_mask;
    00391       byte callback_mask = e->info.callback_mask;
    00392
    00393       /* Skip this engine if it does not carry anything */
    00394       if (!e->CanCarryCargo()) continue;
    00395
    00396       /* Loop through all cargos in the refit mask */
    00397       int current_index = 0;
    00398       const CargoSpec *cs;
    00399       FOR_ALL_SORTED_CARGOSPECS(cs) {
    00400         CargoID cid = cs->Index();
    00401         /* Skip cargo type if it's not listed */
    00402         if (!HasBit(cmask, cid)) {
    00403           current_index++;
    00404           continue;
    00405         }
    00406
    00407         /* Check the vehicle's callback mask for cargo suffixes */
    00408         if (HasBit(callback_mask, CBM_VEHICLE_CARGO_SUFFIX)) {
    00409           /* Make a note of the original cargo type. It has to be
    00410            * changed to test the cargo & subtype... */
    00411           CargoID temp_cargo = v->cargo_type;
    00412           byte temp_subtype  = v->cargo_subtype;
    00413
    00414           v->cargo_type = cid;
    00415
    00416           for (uint refit_cyc = 0; refit_cyc < MAX_REFIT_CYCLE; refit_cyc++) {
    00417             v->cargo_subtype = refit_cyc;
    00418
    00419             /* Make sure we don't pick up anything cached. */
    00420             v->First()->InvalidateNewGRFCache();
    00421             v->InvalidateNewGRFCache();
    00422             uint16 callback = GetVehicleCallback(CBID_VEHICLE_CARGO_SUFFIX, 0, 0, v->engine_type, v);
    00423
    00424             if (callback == 0xFF) callback = CALLBACK_FAILED;
    00425             if (refit_cyc != 0 && callback == CALLBACK_FAILED) break;
    00426
    00427             RefitOption option;
    00428             option.cargo   = cid;
    00429             option.subtype = refit_cyc;
    00430             option.value   = callback;
    00431             option.engine  = v->engine_type;
    00432             this->list[current_index].Include(option);
    00433           }
    00434
    00435           /* Reset the vehicle's cargo type */
    00436           v->cargo_type    = temp_cargo;
    00437           v->cargo_subtype = temp_subtype;
    00438
    00439           /* And make sure we haven't tainted the cache */
    00440           v->First()->InvalidateNewGRFCache();
    00441           v->InvalidateNewGRFCache();
    00442         } else {
    00443           /* No cargo suffix callback -- use no subtype */
    00444           RefitOption option;
    00445           option.cargo   = cid;
    00446           option.subtype = 0;
    00447           option.value   = CALLBACK_FAILED;
    00448           option.engine  = INVALID_ENGINE;
    00449           this->list[current_index].Include(option);
    00450         }
    00451         current_index++;
    00452       }
    00453     } while (v->IsGroundVehicle() && (v = v->Next()) != NULL);
    00454
    00455     int scroll_size = 0;
    00456     for (uint i = 0; i < NUM_CARGO; i++) {
    00457       scroll_size += (this->list[i].Length());
    00458     }
    00459     this->vscroll->SetCount(scroll_size);
    00460   }
    00461
    00466   RefitOption *GetRefitOption()
    00467   {
    00468     if (this->sel < 0) return NULL;
    00469     int subtype = 0;
    00470     for (uint i = 0; subtype <= this->sel && i < NUM_CARGO; i++) {
    00471       for (uint j = 0; subtype <= this->sel && j < this->list[i].Length(); j++) {
    00472         if (subtype == this->sel) {
    00473           return &this->list[i][j];
    00474         }
    00475         subtype++;
    00476       }
    00477     }
    00478
    00479     return NULL;
    00480   }
    00481
    00482   RefitWindow(const WindowDesc *desc, const Vehicle *v, VehicleOrderID order) : Window()
    00483   {
    00484     this->sel = -1;
    00485     this->CreateNestedTree(desc);
    00486
    00487     this->vscroll = this->GetScrollbar(VRW_SCROLLBAR);
    00488     this->hscroll = (v->IsGroundVehicle() ? this->GetScrollbar(VRW_HSCROLLBAR) : NULL);
    00489     this->GetWidget<NWidgetCore>(VRW_SELECTHEADER)->tool_tip = STR_REFIT_TRAIN_LIST_TOOLTIP + v->type;
    00490     this->GetWidget<NWidgetCore>(VRW_MATRIX)->tool_tip       = STR_REFIT_TRAIN_LIST_TOOLTIP + v->type;
    00491     NWidgetCore *nwi = this->GetWidget<NWidgetCore>(VRW_REFITBUTTON);
    00492     nwi->widget_data = STR_REFIT_TRAIN_REFIT_BUTTON + v->type;
    00493     nwi->tool_tip    = STR_REFIT_TRAIN_REFIT_TOOLTIP + v->type;
    00494     this->GetWidget<NWidgetStacked>(VRW_SHOW_HSCROLLBAR)->SetDisplayedPlane(v->IsGroundVehicle() ? 0 : SZSP_HORIZONTAL);
    00495     this->GetWidget<NWidgetCore>(VRW_VEHICLE_PANEL_DISPLAY)->tool_tip = (v->type == VEH_TRAIN) ? STR_REFIT_SELECT_VEHICLES_TOOLTIP : STR_NULL;
    00496
    00497     this->FinishInitNested(desc, v->index);
    00498     this->owner = v->owner;
    00499
    00500     this->order = order;
    00501     this->SetWidgetDisabledState(VRW_REFITBUTTON, this->sel == -1);
    00502   }
    00503
    00504   virtual void OnInit()
    00505   {
    00506     if (this->cargo != NULL) {
    00507       /* Store the RefitOption currently in use. */
    00508       RefitOption current_refit_option = *(this->cargo);
    00509
    00510       /* Rebuild the refit list */
    00511       this->BuildRefitList();
    00512       this->sel = -1;
    00513       this->cargo = NULL;
    00514       int current = 0;
    00515       for (uint i = 0; this->cargo == NULL && i < NUM_CARGO; i++) {
    00516         for (uint j = 0; j < list[i].Length(); j++) {
    00517           if (list[i][j] == current_refit_option) {
    00518             this->sel = current;
    00519             this->cargo = &list[i][j];
    00520             this->vscroll->ScrollTowards(current);
    00521             break;
    00522           }
    00523           current++;
    00524         }
    00525       }
    00526
    00527       this->SetWidgetDisabledState(VRW_REFITBUTTON, this->sel == -1);
    00528       /* If the selected refit option was not found, scroll the window to the initial position. */
    00529       if (this->sel == -1) this->vscroll->ScrollTowards(0);
    00530     } else {
    00531       /* Rebuild the refit list */
    00532       this->OnInvalidateData(0);
    00533     }
    00534   }
    00535
    00536   virtual void OnPaint()
    00537   {
    00538     /* Determine amount of items for scroller. */
    00539     if (this->hscroll != NULL) this->hscroll->SetCount(this->vehicle_width);
    00540
    00541     /* Calculate sprite position. */
    00542     NWidgetCore *vehicle_panel_display = this->GetWidget<NWidgetCore>(VRW_VEHICLE_PANEL_DISPLAY);
    00543     int sprite_width = max(0, ((int)vehicle_panel_display->current_x - this->vehicle_width) / 2);
    00544     this->sprite_left = vehicle_panel_display->pos_x;
    00545     this->sprite_right = vehicle_panel_display->pos_x + vehicle_panel_display->current_x - 1;
    00546     if (_current_text_dir == TD_RTL) {
    00547       this->sprite_right -= sprite_width;
    00548       this->vehicle_margin = vehicle_panel_display->current_x - sprite_right;
    00549     } else {
    00550       this->sprite_left += sprite_width;
    00551       this->vehicle_margin = sprite_left;
    00552     }
    00553
    00554     this->DrawWidgets();
    00555   }
    00556
    00557   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
    00558   {
    00559     switch (widget) {
    00560       case VRW_MATRIX:
    00561         resize->height = WD_MATRIX_TOP + FONT_HEIGHT_NORMAL + WD_MATRIX_BOTTOM;
    00562         size->height = resize->height * 8;
    00563         break;
    00564
    00565       case VRW_VEHICLE_PANEL_DISPLAY:
    00566         size->height = GetVehicleHeight(Vehicle::Get(this->window_number)->type);
    00567         break;
    00568
    00569       case VRW_INFOPANEL:
    00570         size->width = WD_FRAMERECT_LEFT + this->information_width + WD_FRAMERECT_RIGHT;
    00571         break;
    00572     }
    00573   }
    00574
    00575   virtual void SetStringParameters(int widget) const
    00576   {
    00577     if (widget == VRW_CAPTION) SetDParam(0, Vehicle::Get(this->window_number)->index);
    00578   }
    00579
    00586   StringID GetCapacityString(RefitOption *option) const
    00587   {
    00588     assert(_current_company == _local_company);
    00589     Vehicle *v = Vehicle::Get(this->window_number);
    00590     CommandCost cost = DoCommand(v->tile, this->selected_vehicle, option->cargo | option->subtype << 8 |
    00591         this->num_vehicles << 16, DC_QUERY_COST, GetCmdRefitVeh(v->type));
    00592
    00593     if (cost.Failed()) return INVALID_STRING_ID;
    00594
    00595     SetDParam(0, option->cargo);
    00596     SetDParam(1, _returned_refit_capacity);
    00597
    00598     if (_returned_mail_refit_capacity > 0) {
    00599       SetDParam(2, CT_MAIL);
    00600       SetDParam(3, _returned_mail_refit_capacity);
    00601       SetDParam(4, cost.GetCost());
    00602       return STR_REFIT_NEW_CAPACITY_COST_OF_AIRCRAFT_REFIT;
    00603     } else {
    00604       SetDParam(2, cost.GetCost());
    00605       return STR_REFIT_NEW_CAPACITY_COST_OF_REFIT;
    00606     }
    00607   }
    00608
    00609   virtual void DrawWidget(const Rect &r, int widget) const
    00610   {
    00611     switch (widget) {
    00612       case VRW_VEHICLE_PANEL_DISPLAY: {
    00613         Vehicle *v = Vehicle::Get(this->window_number);
    00614         DrawVehicleImage(v, this->sprite_left + WD_FRAMERECT_LEFT, this->sprite_right - WD_FRAMERECT_RIGHT,
    00615           r.top + WD_FRAMERECT_TOP, INVALID_VEHICLE, this->hscroll != NULL ? this->hscroll->GetPosition() : 0);
    00616
    00617         /* Highlight selected vehicles. */
    00618         if (this->order != INVALID_VEH_ORDER_ID) break;
    00619         int x = 0;
    00620         switch (v->type) {
    00621           case VEH_TRAIN: {
    00622             VehicleSet vehicles_to_refit;
    00623             GetVehicleSet(vehicles_to_refit, Vehicle::Get(this->selected_vehicle), this->num_vehicles);
    00624
    00625             int left = INT32_MIN;
    00626             int width = 0;
    00627
    00628             for (Train *u = Train::From(v); u != NULL; u = u->Next()) {
    00629               /* Start checking. */
    00630               if (vehicles_to_refit.Contains(u->index) && left == INT32_MIN) {
    00631                 left = x - this->hscroll->GetPosition() + r.left + this->vehicle_margin;
    00632                 width = 0;
    00633               }
    00634
    00635               /* Draw a selection. */
    00636               if ((!vehicles_to_refit.Contains(u->index) || u->Next() == NULL) && left != INT32_MIN) {
    00637                 if (u->Next() == NULL && vehicles_to_refit.Contains(u->index)) {
    00638                   int current_width = u->GetDisplayImageWidth();
    00639                   width += current_width;
    00640                   x += current_width;
    00641                 }
    00642
    00643                 int right = Clamp(left + width, 0, r.right);
    00644                 left = max(0, left);
    00645
    00646                 if (_current_text_dir == TD_RTL) {
    00647                   right = this->GetWidget<NWidgetCore>(VRW_VEHICLE_PANEL_DISPLAY)->current_x - left;
    00648                   left = right - width;
    00649                 }
    00650
    00651                 if (left != right) {
    00652                   DrawFrameRect(left, r.top + WD_FRAMERECT_TOP, right, r.top + WD_FRAMERECT_TOP + 13, COLOUR_WHITE, FR_BORDERONLY);
    00653                 }
    00654
    00655                 left = INT32_MIN;
    00656               }
    00657
    00658               int current_width = u->GetDisplayImageWidth();
    00659               width += current_width;
    00660               x += current_width;
    00661             }
    00662             break;
    00663           }
    00664
    00665           default: break;
    00666         }
    00667         break;
    00668       }
    00669
    00670       case VRW_MATRIX:
    00671         DrawVehicleRefitWindow(this->list, this->sel, this->vscroll->GetPosition(), this->vscroll->GetCapacity(), this->resize.step_height, r);
    00672         break;
    00673
    00674       case VRW_INFOPANEL:
    00675         if (this->cargo != NULL) {
    00676           StringID string = this->GetCapacityString(this->cargo);
    00677           if (string != INVALID_STRING_ID) {
    00678             DrawStringMultiLine(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT,
    00679                 r.top + WD_FRAMERECT_TOP, r.bottom - WD_FRAMERECT_BOTTOM, string);
    00680           }
    00681         }
    00682         break;
    00683     }
    00684   }
    00685
    00686   virtual void OnInvalidateData(int data)
    00687   {
    00688     switch (data) {
    00689       case 0: { // The consist has changed; rebuild the entire list.
    00690         /* Clear the selection. */
    00691         Vehicle *v = Vehicle::Get(this->window_number);
    00692         this->selected_vehicle = v->index;
    00693         this->num_vehicles = UINT8_MAX;
    00694         /* FALL THROUGH */
    00695       }
    00696
    00697       case 2: { // The vehicle selection has changed; rebuild the entire list.
    00698         this->BuildRefitList();
    00699
    00700         /* The vehicle width has changed too. */
    00701         this->vehicle_width = GetVehicleWidth(Vehicle::Get(this->window_number));
    00702         uint max_width = 0;
    00703
    00704         /* Check the width of all cargo information strings. */
    00705         for (uint i = 0; i < NUM_CARGO; i++) {
    00706           for (uint j = 0; j < this->list[i].Length(); j++) {
    00707             StringID string = this->GetCapacityString(&list[i][j]);
    00708             if (string != INVALID_STRING_ID) {
    00709               Dimension dim = GetStringBoundingBox(string);
    00710               max_width = max(dim.width, max_width);
    00711             }
    00712           }
    00713         }
    00714
    00715         if (this->information_width < max_width) {
    00716           this->information_width = max_width;
    00717           this->ReInit();
    00718         }
    00719         /* FALL THROUGH */
    00720       }
    00721
    00722       case 1: // A new cargo has been selected.
    00723         this->cargo = GetRefitOption();
    00724         break;
    00725     }
    00726   }
    00727
    00728   int GetClickPosition(int click_x)
    00729   {
    00730     const NWidgetCore *matrix_widget = this->GetWidget<NWidgetCore>(VRW_VEHICLE_PANEL_DISPLAY);
    00731     if (_current_text_dir == TD_RTL) click_x = matrix_widget->current_x - click_x;
    00732     click_x -= this->vehicle_margin;
    00733     if (this->hscroll != NULL) click_x += this->hscroll->GetPosition();
    00734
    00735     return click_x;
    00736   }
    00737
    00738   void SetSelectedVehicles(int drag_x)
    00739   {
    00740     drag_x = GetClickPosition(drag_x);
    00741
    00742     int left_x  = min(this->click_x, drag_x);
    00743     int right_x = max(this->click_x, drag_x);
    00744     this->num_vehicles = 0;
    00745
    00746     Vehicle *v = Vehicle::Get(this->window_number);
    00747     /* Find the vehicle part that was clicked. */
    00748     switch (v->type) {
    00749       case VEH_TRAIN: {
    00750         /* Don't select anything if we are not clicking in the vehicle. */
    00751         if (left_x >= 0) {
    00752           const Train *u = Train::From(v);
    00753           bool start_counting = false;
    00754           for (; u != NULL; u = u->Next()) {
    00755             int current_width = u->GetDisplayImageWidth();
    00756             left_x  -= current_width;
    00757             right_x -= current_width;
    00758
    00759             if (left_x < 0 && !start_counting) {
    00760               this->selected_vehicle = u->index;
    00761               start_counting = true;
    00762
    00763               /* Count the first vehicle, even if articulated part */
    00764               this->num_vehicles++;
    00765             } else if (start_counting && !u->IsArticulatedPart()) {
    00766               /* Do not count articulated parts */
    00767               this->num_vehicles++;
    00768             }
    00769
    00770             if (right_x < 0) break;
    00771           }
    00772         }
    00773
    00774         /* If the selection is not correct, clear it. */
    00775         if (this->num_vehicles != 0) {
    00776           if (_ctrl_pressed) this->num_vehicles = UINT8_MAX;
    00777           break;
    00778         }
    00779         /* FALL THROUGH */
    00780       }
    00781
    00782       default:
    00783         /* Clear the selection. */
    00784         this->selected_vehicle = v->index;
    00785         this->num_vehicles = UINT8_MAX;
    00786         break;
    00787     }
    00788   }
    00789
    00790   virtual void OnClick(Point pt, int widget, int click_count)
    00791   {
    00792     switch (widget) {
    00793       case VRW_VEHICLE_PANEL_DISPLAY: { // Vehicle image.
    00794         if (this->order != INVALID_VEH_ORDER_ID) break;
    00795         NWidgetBase *nwi = this->GetWidget<NWidgetBase>(VRW_VEHICLE_PANEL_DISPLAY);
    00796         this->click_x = GetClickPosition(pt.x - nwi->pos_x);
    00797         this->SetSelectedVehicles(pt.x - nwi->pos_x);
    00798         this->SetWidgetDirty(VRW_VEHICLE_PANEL_DISPLAY);
    00799         if (!_ctrl_pressed) {
    00800           SetObjectToPlaceWnd(SPR_CURSOR_MOUSE, PAL_NONE, HT_DRAG, this);
    00801         } else {
    00802           /* The vehicle selection has changed. */
    00803           this->InvalidateData(2);
    00804         }
    00805         break;
    00806       }
    00807
    00808       case VRW_MATRIX: { // listbox
    00809         this->sel = this->vscroll->GetScrolledRowFromWidget(pt.y, this, VRW_MATRIX);
    00810         if (this->sel == INT_MAX) this->sel = -1;
    00811         this->SetWidgetDisabledState(VRW_REFITBUTTON, this->sel == -1);
    00812         this->InvalidateData(1);
    00813
    00814         if (click_count == 1) break;
    00815         /* FALL THROUGH */
    00816       }
    00817
    00818       case VRW_REFITBUTTON: // refit button
    00819         if (this->cargo != NULL) {
    00820           const Vehicle *v = Vehicle::Get(this->window_number);
    00821
    00822           if (this->order == INVALID_VEH_ORDER_ID) {
    00823             bool delete_window = this->selected_vehicle == v->index && this->num_vehicles == UINT8_MAX;
    00824             if (DoCommandP(v->tile, this->selected_vehicle, this->cargo->cargo | this->cargo->subtype << 8 | this->num_vehicles << 16, GetCmdRefitVeh(v)) && delete_window) delete this;
    00825           } else {
    00826             if (DoCommandP(v->tile, v->index, this->cargo->cargo | this->cargo->subtype << 8 | this->order << 16, CMD_ORDER_REFIT)) delete this;
    00827           }
    00828         }
    00829         break;
    00830     }
    00831   }
    00832
    00833   virtual void OnMouseDrag(Point pt, int widget)
    00834   {
    00835     switch (widget) {
    00836       case VRW_VEHICLE_PANEL_DISPLAY: { // Vehicle image.
    00837         if (this->order != INVALID_VEH_ORDER_ID) break;
    00838         NWidgetBase *nwi = this->GetWidget<NWidgetBase>(VRW_VEHICLE_PANEL_DISPLAY);
    00839         this->SetSelectedVehicles(pt.x - nwi->pos_x);
    00840         this->SetWidgetDirty(VRW_VEHICLE_PANEL_DISPLAY);
    00841         break;
    00842       }
    00843     }
    00844   }
    00845
    00846   virtual void OnDragDrop(Point pt, int widget)
    00847   {
    00848     switch (widget) {
    00849       case VRW_VEHICLE_PANEL_DISPLAY: { // Vehicle image.
    00850         if (this->order != INVALID_VEH_ORDER_ID) break;
    00851         NWidgetBase *nwi = this->GetWidget<NWidgetBase>(VRW_VEHICLE_PANEL_DISPLAY);
    00852         this->SetSelectedVehicles(pt.x - nwi->pos_x);
    00853         this->InvalidateData(2);
    00854         break;
    00855       }
    00856     }
    00857   }
    00858
    00859   virtual void OnResize()
    00860   {
    00861     this->vehicle_width = GetVehicleWidth(Vehicle::Get(this->window_number));
    00862     this->vscroll->SetCapacityFromWidget(this, VRW_MATRIX);
    00863     if (this->hscroll != NULL) this->hscroll->SetCapacityFromWidget(this, VRW_VEHICLE_PANEL_DISPLAY);
    00864     this->GetWidget<NWidgetCore>(VRW_MATRIX)->widget_data = (this->vscroll->GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
    00865   }
    00866 };
    00867
    00868 static const NWidgetPart _nested_vehicle_refit_widgets[] = {
    00869   NWidget(NWID_HORIZONTAL),
    00870     NWidget(WWT_CLOSEBOX, COLOUR_GREY),
    00871     NWidget(WWT_CAPTION, COLOUR_GREY, VRW_CAPTION), SetDataTip(STR_REFIT_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    00872   EndContainer(),
    00873   /* Vehicle display + scrollbar. */
    00874   NWidget(NWID_VERTICAL),
    00875     NWidget(WWT_PANEL, COLOUR_GREY, VRW_VEHICLE_PANEL_DISPLAY), SetMinimalSize(228, 14), SetResize(1, 0), SetScrollbar(VRW_HSCROLLBAR), EndContainer(),
    00876     NWidget(NWID_SELECTION, INVALID_COLOUR, VRW_SHOW_HSCROLLBAR),
    00877       NWidget(NWID_HSCROLLBAR, COLOUR_GREY, VRW_HSCROLLBAR),
    00878     EndContainer(),
    00879   EndContainer(),
    00880   NWidget(WWT_TEXTBTN, COLOUR_GREY, VRW_SELECTHEADER), SetDataTip(STR_REFIT_TITLE, STR_NULL), SetResize(1, 0),
    00881   /* Matrix + scrollbar. */
    00882   NWidget(NWID_HORIZONTAL),
    00883     NWidget(WWT_MATRIX, COLOUR_GREY, VRW_MATRIX), SetMinimalSize(228, 112), SetResize(1, 14), SetFill(1, 1), SetDataTip(0x801, STR_NULL), SetScrollbar(VRW_SCROLLBAR),
    00884     NWidget(NWID_VSCROLLBAR, COLOUR_GREY, VRW_SCROLLBAR),
    00885   EndContainer(),
    00886   NWidget(WWT_PANEL, COLOUR_GREY, VRW_INFOPANEL), SetMinimalTextLines(2, WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM), SetResize(1, 0), EndContainer(),
    00887   NWidget(NWID_HORIZONTAL),
    00888     NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, VRW_REFITBUTTON), SetFill(1, 0), SetResize(1, 0),
    00889     NWidget(WWT_RESIZEBOX, COLOUR_GREY),
    00890   EndContainer(),
    00891 };
    00892
    00893 static const WindowDesc _vehicle_refit_desc(
    00894   WDP_AUTO, 240, 174,
    00895   WC_VEHICLE_REFIT, WC_VEHICLE_VIEW,
    00896   WDF_UNCLICK_BUTTONS | WDF_CONSTRUCTION,
    00897   _nested_vehicle_refit_widgets, lengthof(_nested_vehicle_refit_widgets)
    00898 );
    00899
    00906 void ShowVehicleRefitWindow(const Vehicle *v, VehicleOrderID order, Window *parent)
    00907 {
    00908   DeleteWindowById(WC_VEHICLE_REFIT, v->index);
    00909   RefitWindow *w = new RefitWindow(&_vehicle_refit_desc, v, order);
    00910   w->parent = parent;
    00911 }
    00912
    00914 uint ShowRefitOptionsList(int left, int right, int y, EngineID engine)
    00915 {
    00916   /* List of cargo types of this engine */
    00917   uint32 cmask = GetUnionOfArticulatedRefitMasks(engine, false);
    00918   /* List of cargo types available in this climate */
    00919   uint32 lmask = _cargo_mask;
    00920   char string[512];
    00921   char *b = string;
    00922
    00923   /* Draw nothing if the engine is not refittable */
    00924   if (HasAtMostOneBit(cmask)) return y;
    00925
    00926   b = InlineString(b, STR_PURCHASE_INFO_REFITTABLE_TO);
    00927
    00928   if (cmask == lmask) {
    00929     /* Engine can be refitted to all types in this climate */
    00930     b = InlineString(b, STR_PURCHASE_INFO_ALL_TYPES);
    00931   } else {
    00932     /* Check if we are able to refit to more cargo types and unable to. If
    00933      * so, invert the cargo types to list those that we can't refit to. */
    00934     if (CountBits(cmask ^ lmask) < CountBits(cmask) && CountBits(cmask ^ lmask) <= 7) {
    00935       cmask ^= lmask;
    00936       b = InlineString(b, STR_PURCHASE_INFO_ALL_BUT);
    00937     }
    00938
    00939     bool first = true;
    00940
    00941     /* Add each cargo type to the list */
    00942     const CargoSpec *cs;
    00943     FOR_ALL_SORTED_CARGOSPECS(cs) {
    00944       if (!HasBit(cmask, cs->Index())) continue;
    00945
    00946       if (b >= lastof(string) - (2 + 2 * 4)) break; // ", " and two calls to Utf8Encode()
    00947
    00948       if (!first) b = strecpy(b, ", ", lastof(string));
    00949       first = false;
    00950
    00951       b = InlineString(b, cs->name);
    00952     }
    00953   }
    00954
    00955   /* Terminate and display the completed string */
    00956   *b = '\0';
    00957
    00958   /* Make sure we detect any buffer overflow */
    00959   assert(b < endof(string));
    00960
    00961   SetDParamStr(0, string);
    00962   return DrawStringMultiLine(left, right, y, INT32_MAX, STR_JUST_RAW_STRING);
    00963 }
    00964
    00966 StringID GetCargoSubtypeText(const Vehicle *v)
    00967 {
    00968   if (HasBit(EngInfo(v->engine_type)->callback_mask, CBM_VEHICLE_CARGO_SUFFIX)) {
    00969     uint16 cb = GetVehicleCallback(CBID_VEHICLE_CARGO_SUFFIX, 0, 0, v->engine_type, v);
    00970     if (cb != CALLBACK_FAILED) {
    00971       return GetGRFStringID(GetEngineGRFID(v->engine_type), 0xD000 + cb);
    00972     }
    00973   }
    00974   return STR_EMPTY;
    00975 }
    00976
    00978 static int CDECL VehicleNumberSorter(const Vehicle * const *a, const Vehicle * const *b)
    00979 {
    00980   return (*a)->unitnumber - (*b)->unitnumber;
    00981 }
    00982
    00984 static int CDECL VehicleNameSorter(const Vehicle * const *a, const Vehicle * const *b)
    00985 {
    00986   static char last_name[2][64];
    00987
    00988   if (*a != _last_vehicle[0]) {
    00989     _last_vehicle[0] = *a;
    00990     SetDParam(0, (*a)->index);
    00991     GetString(last_name[0], STR_VEHICLE_NAME, lastof(last_name[0]));
    00992   }
    00993
    00994   if (*b != _last_vehicle[1]) {
    00995     _last_vehicle[1] = *b;
    00996     SetDParam(0, (*b)->index);
    00997     GetString(last_name[1], STR_VEHICLE_NAME, lastof(last_name[1]));
    00998   }
    00999
    01000   int r = strnatcmp(last_name[0], last_name[1]); // Sort by name (natural sorting).
    01001   return (r != 0) ? r : VehicleNumberSorter(a, b);
    01002 }
    01003
    01005 static int CDECL VehicleAgeSorter(const Vehicle * const *a, const Vehicle * const *b)
    01006 {
    01007   int r = (*a)->age - (*b)->age;
    01008   return (r != 0) ? r : VehicleNumberSorter(a, b);
    01009 }
    01010
    01012 static int CDECL VehicleProfitThisYearSorter(const Vehicle * const *a, const Vehicle * const *b)
    01013 {
    01014   int r = ClampToI32((*a)->GetDisplayProfitThisYear() - (*b)->GetDisplayProfitThisYear());
    01015   return (r != 0) ? r : VehicleNumberSorter(a, b);
    01016 }
    01017
    01019 static int CDECL VehicleProfitLastYearSorter(const Vehicle * const *a, const Vehicle * const *b)
    01020 {
    01021   int r = ClampToI32((*a)->GetDisplayProfitLastYear() - (*b)->GetDisplayProfitLastYear());
    01022   return (r != 0) ? r : VehicleNumberSorter(a, b);
    01023 }
    01024
    01026 static int CDECL VehicleCargoSorter(const Vehicle * const *a, const Vehicle * const *b)
    01027 {
    01028   const Vehicle *v;
    01029   CargoArray diff;
    01030
    01031   /* Append the cargo of the connected weagons */
    01032   for (v = *a; v != NULL; v = v->Next()) diff[v->cargo_type] += v->cargo_cap;
    01033   for (v = *b; v != NULL; v = v->Next()) diff[v->cargo_type] -= v->cargo_cap;
    01034
    01035   int r = 0;
    01036   for (CargoID i = 0; i < NUM_CARGO; i++) {
    01037     r = diff[i];
    01038     if (r != 0) break;
    01039   }
    01040
    01041   return (r != 0) ? r : VehicleNumberSorter(a, b);
    01042 }
    01043
    01045 static int CDECL VehicleReliabilitySorter(const Vehicle * const *a, const Vehicle * const *b)
    01046 {
    01047   int r = (*a)->reliability - (*b)->reliability;
    01048   return (r != 0) ? r : VehicleNumberSorter(a, b);
    01049 }
    01050
    01052 static int CDECL VehicleMaxSpeedSorter(const Vehicle * const *a, const Vehicle * const *b)
    01053 {
    01054   int r = (*a)->vcache.cached_max_speed - (*b)->vcache.cached_max_speed;
    01055   return (r != 0) ? r : VehicleNumberSorter(a, b);
    01056 }
    01057
    01059 static int CDECL VehicleModelSorter(const Vehicle * const *a, const Vehicle * const *b)
    01060 {
    01061   int r = (*a)->engine_type - (*b)->engine_type;
    01062   return (r != 0) ? r : VehicleNumberSorter(a, b);
    01063 }
    01064
    01066 static int CDECL VehicleValueSorter(const Vehicle * const *a, const Vehicle * const *b)
    01067 {
    01068   const Vehicle *u;
    01069   Money diff = 0;
    01070
    01071   for (u = *a; u != NULL; u = u->Next()) diff += u->value;
    01072   for (u = *b; u != NULL; u = u->Next()) diff -= u->value;
    01073
    01074   int r = ClampToI32(diff);
    01075   return (r != 0) ? r : VehicleNumberSorter(a, b);
    01076 }
    01077
    01079 static int CDECL VehicleLengthSorter(const Vehicle * const *a, const Vehicle * const *b)
    01080 {
    01081   int r = (*a)->GetGroundVehicleCache()->cached_total_length - (*b)->GetGroundVehicleCache()->cached_total_length;
    01082   return (r != 0) ? r : VehicleNumberSorter(a, b);
    01083 }
    01084
    01086 static int CDECL VehicleTimeToLiveSorter(const Vehicle * const *a, const Vehicle * const *b)
    01087 {
    01088   int r = ClampToI32(((*a)->max_age - (*a)->age) - ((*b)->max_age - (*b)->age));
    01089   return (r != 0) ? r : VehicleNumberSorter(a, b);
    01090 }
    01091
    01093 static int CDECL VehicleTimetableDelaySorter(const Vehicle * const *a, const Vehicle * const *b)
    01094 {
    01095   int r = (*a)->lateness_counter - (*b)->lateness_counter;
    01096   return (r != 0) ? r : VehicleNumberSorter(a, b);
    01097 }
    01098
    01099 void InitializeGUI()
    01100 {
    01101   MemSetT(&_sorting, 0);
    01102 }
    01103
    01110 static inline void ChangeVehicleWindow(WindowClass window_class, VehicleID from_index, VehicleID to_index)
    01111 {
    01112   Window *w = FindWindowById(window_class, from_index);
    01113   if (w != NULL) {
    01114     /* Update window_number */
    01115     w->window_number = to_index;
    01116     if (w->viewport != NULL) w->viewport->follow_vehicle = to_index;
    01117
    01118     /* Update vehicle drag data */
    01119     if (_thd.window_class == window_class && _thd.window_number == (WindowNumber)from_index) {
    01120       _thd.window_number = to_index;
    01121     }
    01122
    01123     /* Notify the window immediately, without scheduling. */
    01124     w->InvalidateData();
    01125   }
    01126 }
    01127
    01133 void ChangeVehicleViewWindow(VehicleID from_index, VehicleID to_index)
    01134 {
    01135   ChangeVehicleWindow(WC_VEHICLE_VIEW,      from_index, to_index);
    01136   ChangeVehicleWindow(WC_VEHICLE_ORDERS,    from_index, to_index);
    01137   ChangeVehicleWindow(WC_VEHICLE_REFIT,     from_index, to_index);
    01138   ChangeVehicleWindow(WC_VEHICLE_DETAILS,   from_index, to_index);
    01139   ChangeVehicleWindow(WC_VEHICLE_TIMETABLE, from_index, to_index);
    01140 }
    01141
    01142 enum VehicleListWindowWidgets {
    01143   VLW_WIDGET_CAPTION,
    01144   VLW_WIDGET_SORT_ORDER,
    01145   VLW_WIDGET_SORT_BY_PULLDOWN,
    01146   VLW_WIDGET_LIST,
    01147   VLW_WIDGET_SCROLLBAR,
    01148   VLW_WIDGET_HIDE_BUTTONS,
    01149   VLW_WIDGET_AVAILABLE_VEHICLES,
    01150   VLW_WIDGET_MANAGE_VEHICLES_DROPDOWN,
    01151   VLW_WIDGET_STOP_ALL,
    01152   VLW_WIDGET_START_ALL,
    01153 };
    01154
    01155 static const NWidgetPart _nested_vehicle_list[] = {
    01156   NWidget(NWID_HORIZONTAL),
    01157     NWidget(WWT_CLOSEBOX, COLOUR_GREY),
    01158     NWidget(WWT_CAPTION, COLOUR_GREY, VLW_WIDGET_CAPTION),
    01159     NWidget(WWT_SHADEBOX, COLOUR_GREY),
    01160     NWidget(WWT_STICKYBOX, COLOUR_GREY),
    01161   EndContainer(),
    01162
    01163   NWidget(NWID_HORIZONTAL),
    01164     NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, VLW_WIDGET_SORT_ORDER), SetMinimalSize(81, 12), SetFill(0, 1), SetDataTip(STR_BUTTON_SORT_BY, STR_TOOLTIP_SORT_ORDER),
    01165     NWidget(WWT_DROPDOWN, COLOUR_GREY, VLW_WIDGET_SORT_BY_PULLDOWN), SetMinimalSize(167, 12), SetFill(0, 1), SetDataTip(0x0, STR_TOOLTIP_SORT_CRITERIA),
    01166     NWidget(WWT_PANEL, COLOUR_GREY), SetMinimalSize(12, 12), SetFill(1, 1), SetResize(1, 0),
    01167     EndContainer(),
    01168   EndContainer(),
    01169
    01170   NWidget(NWID_HORIZONTAL),
    01171     NWidget(WWT_MATRIX, COLOUR_GREY, VLW_WIDGET_LIST), SetMinimalSize(248, 0), SetFill(1, 0), SetResize(1, 1), SetScrollbar(VLW_WIDGET_SCROLLBAR),
    01172     NWidget(NWID_VSCROLLBAR, COLOUR_GREY, VLW_WIDGET_SCROLLBAR),
    01173   EndContainer(),
    01174
    01175   NWidget(NWID_HORIZONTAL),
    01176     NWidget(NWID_SELECTION, INVALID_COLOUR, VLW_WIDGET_HIDE_BUTTONS),
    01177       NWidget(NWID_HORIZONTAL),
    01178         NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, VLW_WIDGET_AVAILABLE_VEHICLES), SetMinimalSize(106, 12), SetFill(0, 1),
    01179                 SetDataTip(STR_BLACK_STRING, STR_VEHICLE_LIST_AVAILABLE_ENGINES_TOOLTIP),
    01180         NWidget(WWT_DROPDOWN, COLOUR_GREY, VLW_WIDGET_MANAGE_VEHICLES_DROPDOWN), SetMinimalSize(118, 12), SetFill(0, 1),
    01181                 SetDataTip(STR_VEHICLE_LIST_MANAGE_LIST, STR_VEHICLE_LIST_MANAGE_LIST_TOOLTIP),
    01182         NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, VLW_WIDGET_STOP_ALL), SetMinimalSize(12, 12), SetFill(0, 1),
    01183                 SetDataTip(SPR_FLAG_VEH_STOPPED, STR_VEHICLE_LIST_MASS_STOP_LIST_TOOLTIP),
    01184         NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, VLW_WIDGET_START_ALL), SetMinimalSize(12, 12), SetFill(0, 1),
    01185                 SetDataTip(SPR_FLAG_VEH_RUNNING, STR_VEHICLE_LIST_MASS_START_LIST_TOOLTIP),
    01186         NWidget(WWT_PANEL, COLOUR_GREY), SetMinimalSize(0, 12), SetResize(1, 0), SetFill(1, 1), EndContainer(),
    01187       EndContainer(),
    01188       /* Widget to be shown for other companies hiding the previous 5 widgets. */
    01189       NWidget(WWT_PANEL, COLOUR_GREY), SetFill(1, 1), SetResize(1, 0), EndContainer(),
    01190     EndContainer(),
    01191     NWidget(WWT_RESIZEBOX, COLOUR_GREY),
    01192   EndContainer(),
    01193 };
    01194
    01195 static void DrawSmallOrderList(const Vehicle *v, int left, int right, int y, VehicleOrderID start = 0)
    01196 {
    01197   const Order *order = v->GetOrder(start);
    01198   if (order == NULL) return;
    01199
    01200   int i = 0;
    01201   VehicleOrderID oid = start;
    01202
    01203   do {
    01204     if (oid == v->cur_real_order_index) DrawString(left, right, y, STR_TINY_RIGHT_ARROW, TC_BLACK);
    01205
    01206     if (order->IsType(OT_GOTO_STATION)) {
    01207       SetDParam(0, order->GetDestination());
    01208       DrawString(left + 6, right - 6, y, STR_TINY_BLACK_STATION);
    01209
    01210       y += FONT_HEIGHT_SMALL;
    01211       if (++i == 4) break;
    01212     }
    01213
    01214     oid++;
    01215     order = order->next;
    01216     if (order == NULL) {
    01217       order = v->orders.list->GetFirstOrder();
    01218       oid = 0;
    01219     }
    01220   } while (oid != start);
    01221 }
    01222
    01232 void DrawVehicleImage(const Vehicle *v, int left, int right, int y, VehicleID selection, int skip)
    01233 {
    01234   switch (v->type) {
    01235     case VEH_TRAIN:    DrawTrainImage(Train::From(v), left, right, y, selection, skip); break;
    01236     case VEH_ROAD:     DrawRoadVehImage(v, left, right, y, selection, skip);  break;
    01237     case VEH_SHIP:     DrawShipImage(v, left, right, y, selection);     break;
    01238     case VEH_AIRCRAFT: DrawAircraftImage(v, left, right, y, selection); break;
    01239     default: NOT_REACHED();
    01240   }
    01241 }
    01242
    01249 uint GetVehicleListHeight(VehicleType type, uint divisor)
    01250 {
    01251   /* Name + vehicle + profit */
    01252   uint base = GetVehicleHeight(type) + 2 * FONT_HEIGHT_SMALL;
    01253   /* Drawing of the 4 small orders + profit*/
    01254   if (type >= VEH_SHIP) base = max(base, 5U * FONT_HEIGHT_SMALL);
    01255
    01256   if (divisor == 1) return base;
    01257
    01258   /* Make sure the height is dividable by divisor */
    01259   uint rem = base % divisor;
    01260   return base + (rem == 0 ? 0 : divisor - rem);
    01261 }
    01262
    01269 void BaseVehicleListWindow::DrawVehicleListItems(VehicleID selected_vehicle, int line_height, const Rect &r) const
    01270 {
    01271   int left = r.left + WD_MATRIX_LEFT;
    01272   int right = r.right - WD_MATRIX_RIGHT;
    01273   int width = right - left;
    01274   bool rtl = _current_text_dir == TD_RTL;
    01275
    01276   int text_offset = GetDigitWidth() * this->unitnumber_digits + WD_FRAMERECT_RIGHT;
    01277   int text_left  = left  + (rtl ?           0 : text_offset);
    01278   int text_right = right - (rtl ? text_offset :           0);
    01279
    01280   bool show_orderlist = this->vli.vtype >= VEH_SHIP;
    01281   int orderlist_left  = left  + (rtl ? 0 : max(100 + text_offset, width / 2));
    01282   int orderlist_right = right - (rtl ? max(100 + text_offset, width / 2) : 0);
    01283
    01284   int image_left  = (rtl && show_orderlist) ? orderlist_right : text_left;
    01285   int image_right = (!rtl && show_orderlist) ? orderlist_left : text_right;
    01286
    01287   int vehicle_button_x = rtl ? right - GetSpriteSize(SPR_PROFIT_LOT).width : left;
    01288
    01289   int y = r.top;
    01290   uint max = min(this->vscroll->GetPosition() + this->vscroll->GetCapacity(), this->vehicles.Length());
    01291   for (uint i = this->vscroll->GetPosition(); i < max; ++i) {
    01292     const Vehicle *v = this->vehicles[i];
    01293     StringID str;
    01294
    01295     SetDParam(0, v->GetDisplayProfitThisYear());
    01296     SetDParam(1, v->GetDisplayProfitLastYear());
    01297
    01298     DrawVehicleImage(v, image_left, image_right, y + FONT_HEIGHT_SMALL - 1, selected_vehicle, 0);
    01299     DrawString(text_left, text_right, y + line_height - FONT_HEIGHT_SMALL - WD_FRAMERECT_BOTTOM - 1, STR_VEHICLE_LIST_PROFIT_THIS_YEAR_LAST_YEAR);
    01300
    01301     if (v->name != NULL) {
    01302       /* The vehicle got a name so we will print it */
    01303       SetDParam(0, v->index);
    01304       DrawString(text_left, text_right, y, STR_TINY_BLACK_VEHICLE);
    01305     } else if (v->group_id != DEFAULT_GROUP) {
    01306       /* The vehicle has no name, but is member of a group, so print group name */
    01307       SetDParam(0, v->group_id);
    01308       DrawString(text_left, text_right, y, STR_TINY_GROUP, TC_BLACK);
    01309     }
    01310
    01311     if (show_orderlist) DrawSmallOrderList(v, orderlist_left, orderlist_right, y, v->cur_real_order_index);
    01312
    01313     if (v->IsInDepot()) {
    01314       str = STR_BLUE_COMMA;
    01315     } else {
    01316       str = (v->age > v->max_age - DAYS_IN_LEAP_YEAR) ? STR_RED_COMMA : STR_BLACK_COMMA;
    01317     }
    01318
    01319     SetDParam(0, v->unitnumber);
    01320     DrawString(left, right, y + 2, str);
    01321
    01322     DrawVehicleProfitButton(v, vehicle_button_x, y + FONT_HEIGHT_NORMAL + 3);
    01323
    01324     y += line_height;
    01325   }
    01326 }
    01327
    01337 struct VehicleListWindow : public BaseVehicleListWindow {
    01338 private:
    01340   enum ButtonPlanes {
    01341     BP_SHOW_BUTTONS,
    01342     BP_HIDE_BUTTONS,
    01343   };
    01344
    01345 public:
    01346   VehicleListWindow(const WindowDesc *desc, WindowNumber window_number) : BaseVehicleListWindow(window_number)
    01347   {
    01348     /* Set up sorting. Make the window-specific _sorting variable
    01349      * point to the correct global _sorting struct so we are freed
    01350      * from having conditionals during window operation */
    01351     switch (this->vli.vtype) {
    01352       case VEH_TRAIN:    this->sorting = &_sorting.train; break;
    01353       case VEH_ROAD:     this->sorting = &_sorting.roadveh; break;
    01354       case VEH_SHIP:     this->sorting = &_sorting.ship; break;
    01355       case VEH_AIRCRAFT: this->sorting = &_sorting.aircraft; break;
    01356       default: NOT_REACHED();
    01357     }
    01358
    01359     this->CreateNestedTree(desc);
    01360
    01361     this->vscroll = this->GetScrollbar(VLW_WIDGET_SCROLLBAR);
    01362
    01363     this->vehicles.SetListing(*this->sorting);
    01364     this->vehicles.ForceRebuild();
    01365     this->vehicles.NeedResort();
    01366     this->BuildVehicleList();
    01367     this->SortVehicleList();
    01368
    01369     /* Set up the window widgets */
    01370     this->GetWidget<NWidgetCore>(VLW_WIDGET_LIST)->tool_tip = STR_VEHICLE_LIST_TRAIN_LIST_TOOLTIP + this->vli.vtype;
    01371
    01372     if (this->vli.type == VL_SHARED_ORDERS) {
    01373       this->GetWidget<NWidgetCore>(VLW_WIDGET_CAPTION)->widget_data = STR_VEHICLE_LIST_SHARED_ORDERS_LIST_CAPTION;
    01374     } else {
    01375       this->GetWidget<NWidgetCore>(VLW_WIDGET_CAPTION)->widget_data = STR_VEHICLE_LIST_TRAIN_CAPTION + this->vli.vtype;
    01376     }
    01377
    01378     this->FinishInitNested(desc, window_number);
    01379     this->owner = this->vli.company;
    01380
    01381     if (this->vli.vtype == VEH_TRAIN) ResizeWindow(this, 65, 0);
    01382   }
    01383
    01384   ~VehicleListWindow()
    01385   {
    01386     *this->sorting = this->vehicles.GetListing();
    01387   }
    01388
    01389   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
    01390   {
    01391     switch (widget) {
    01392       case VLW_WIDGET_LIST:
    01393         resize->height = GetVehicleListHeight(this->vli.vtype, 1);
    01394
    01395         switch (this->vli.vtype) {
    01396           case VEH_TRAIN:
    01397           case VEH_ROAD:
    01398             size->height = 6 * resize->height;
    01399             break;
    01400           case VEH_SHIP:
    01401           case VEH_AIRCRAFT:
    01402             size->height = 4 * resize->height;
    01403             break;
    01404           default: NOT_REACHED();
    01405         }
    01406         break;
    01407
    01408       case VLW_WIDGET_SORT_ORDER: {
    01409         Dimension d = GetStringBoundingBox(this->GetWidget<NWidgetCore>(widget)->widget_data);
    01410         d.width += padding.width + WD_SORTBUTTON_ARROW_WIDTH * 2; // Doubled since the string is centred and it also looks better.
    01411         d.height += padding.height;
    01412         *size = maxdim(*size, d);
    01413         break;
    01414       }
    01415
    01416       case VLW_WIDGET_MANAGE_VEHICLES_DROPDOWN: {
    01417         Dimension d = this->GetActionDropdownSize(this->vli.type == VL_STANDARD, false);
    01418         d.height += padding.height;
    01419         d.width  += padding.width;
    01420         *size = maxdim(*size, d);
    01421         break;
    01422       }
    01423     }
    01424   }
    01425
    01426   virtual void SetStringParameters(int widget) const
    01427   {
    01428     switch (widget) {
    01429       case VLW_WIDGET_AVAILABLE_VEHICLES:
    01430         SetDParam(0, STR_VEHICLE_LIST_AVAILABLE_TRAINS + this->vli.vtype);
    01431         break;
    01432
    01433       case VLW_WIDGET_CAPTION: {
    01434         switch (this->vli.type) {
    01435           case VL_SHARED_ORDERS: // Shared Orders
    01436             if (this->vehicles.Length() == 0) {
    01437               /* We can't open this window without vehicles using this order
    01438                * and we should close the window when deleting the order. */
    01439               NOT_REACHED();
    01440             }
    01441             SetDParam(0, this->vscroll->GetCount());
    01442             break;
    01443
    01444           case VL_STANDARD: // Company Name
    01445             SetDParam(0, STR_COMPANY_NAME);
    01446             SetDParam(1, this->vli.index);
    01447             SetDParam(3, this->vscroll->GetCount());
    01448             break;
    01449
    01450           case VL_STATION_LIST: // Station/Waypoint Name
    01451             SetDParam(0, Station::IsExpected(BaseStation::Get(this->vli.index)) ? STR_STATION_NAME : STR_WAYPOINT_NAME);
    01452             SetDParam(1, this->vli.index);
    01453             SetDParam(3, this->vscroll->GetCount());
    01454             break;
    01455
    01456           case VL_DEPOT_LIST:
    01457             SetDParam(0, STR_DEPOT_CAPTION);
    01458             SetDParam(1, this->vli.vtype);
    01459             SetDParam(2, this->vli.index);
    01460             SetDParam(3, this->vscroll->GetCount());
    01461             break;
    01462           default: NOT_REACHED();
    01463         }
    01464         break;
    01465       }
    01466     }
    01467   }
    01468
    01469   virtual void DrawWidget(const Rect &r, int widget) const
    01470   {
    01471     switch (widget) {
    01472       case VLW_WIDGET_SORT_ORDER:
    01473         /* draw arrow pointing up/down for ascending/descending sorting */
    01474         this->DrawSortButtonState(widget, this->vehicles.IsDescSortOrder() ? SBS_DOWN : SBS_UP);
    01475         break;
    01476
    01477       case VLW_WIDGET_LIST:
    01478         this->DrawVehicleListItems(INVALID_VEHICLE, this->resize.step_height, r);
    01479         break;
    01480     }
    01481   }
    01482
    01483   virtual void OnPaint()
    01484   {
    01485     this->BuildVehicleList();
    01486     this->SortVehicleList();
    01487
    01488     if (this->vehicles.Length() == 0 && this->IsWidgetLowered(VLW_WIDGET_MANAGE_VEHICLES_DROPDOWN)) {
    01489       HideDropDownMenu(this);
    01490     }
    01491
    01492     /* Hide the widgets that we will not use in this window
    01493      * Some windows contains actions only fit for the owner */
    01494     int plane_to_show = (this->owner == _local_company) ? BP_SHOW_BUTTONS : BP_HIDE_BUTTONS;
    01495     NWidgetStacked *nwi = this->GetWidget<NWidgetStacked>(VLW_WIDGET_HIDE_BUTTONS);
    01496     if (plane_to_show != nwi->shown_plane) {
    01497       nwi->SetDisplayedPlane(plane_to_show);
    01498       nwi->SetDirty(this);
    01499     }
    01500     if (this->owner == _local_company) {
    01501       this->SetWidgetDisabledState(VLW_WIDGET_AVAILABLE_VEHICLES, this->vli.type != VL_STANDARD);
    01502       this->SetWidgetsDisabledState(this->vehicles.Length() == 0,
    01503         VLW_WIDGET_MANAGE_VEHICLES_DROPDOWN,
    01504         VLW_WIDGET_STOP_ALL,
    01505         VLW_WIDGET_START_ALL,
    01506         WIDGET_LIST_END);
    01507     }
    01508
    01509     /* Set text of sort by dropdown widget. */
    01510     this->GetWidget<NWidgetCore>(VLW_WIDGET_SORT_BY_PULLDOWN)->widget_data = this->vehicle_sorter_names[this->vehicles.SortType()];
    01511
    01512     this->DrawWidgets();
    01513   }
    01514
    01515   virtual void OnClick(Point pt, int widget, int click_count)
    01516   {
    01517     switch (widget) {
    01518       case VLW_WIDGET_SORT_ORDER: // Flip sorting method ascending/descending
    01519         this->vehicles.ToggleSortOrder();
    01520         this->SetDirty();
    01521         break;
    01522
    01523       case VLW_WIDGET_SORT_BY_PULLDOWN:// Select sorting criteria dropdown menu
    01524         ShowDropDownMenu(this, this->vehicle_sorter_names, this->vehicles.SortType(), VLW_WIDGET_SORT_BY_PULLDOWN, 0,
    01525             (this->vli.vtype == VEH_TRAIN || this->vli.vtype == VEH_ROAD) ? 0 : (1 << 10));
    01526         return;
    01527
    01528       case VLW_WIDGET_LIST: { // Matrix to show vehicles
    01529         uint id_v = this->vscroll->GetScrolledRowFromWidget(pt.y, this, VLW_WIDGET_LIST);
    01530         if (id_v >= this->vehicles.Length()) return; // click out of list bound
    01531
    01532         const Vehicle *v = this->vehicles[id_v];
    01533         if (!VehicleClicked(v)) ShowVehicleViewWindow(v);
    01534         break;
    01535       }
    01536
    01537       case VLW_WIDGET_AVAILABLE_VEHICLES:
    01538         ShowBuildVehicleWindow(INVALID_TILE, this->vli.vtype);
    01539         break;
    01540
    01541       case VLW_WIDGET_MANAGE_VEHICLES_DROPDOWN: {
    01542         DropDownList *list = this->BuildActionDropdownList(VehicleListIdentifier(this->window_number).type == VL_STANDARD, false);
    01543         ShowDropDownList(this, list, 0, VLW_WIDGET_MANAGE_VEHICLES_DROPDOWN);
    01544         break;
    01545       }
    01546
    01547       case VLW_WIDGET_STOP_ALL:
    01548       case VLW_WIDGET_START_ALL:
    01549         DoCommandP(0, (1 << 1) | (widget == VLW_WIDGET_START_ALL ? (1 << 0) : 0), this->window_number, CMD_MASS_START_STOP);
    01550         break;
    01551     }
    01552   }
    01553
    01554   virtual void OnDropdownSelect(int widget, int index)
    01555   {
    01556     switch (widget) {
    01557       case VLW_WIDGET_SORT_BY_PULLDOWN:
    01558         this->vehicles.SetSortType(index);
    01559         break;
    01560       case VLW_WIDGET_MANAGE_VEHICLES_DROPDOWN:
    01561         assert(this->vehicles.Length() != 0);
    01562
    01563         switch (index) {
    01564           case ADI_REPLACE: // Replace window
    01565             ShowReplaceGroupVehicleWindow(DEFAULT_GROUP, this->vli.vtype);
    01566             break;
    01567           case ADI_SERVICE: // Send for servicing
    01568           case ADI_DEPOT: // Send to Depots
    01569             DoCommandP(0, DEPOT_MASS_SEND | (index == ADI_SERVICE ? DEPOT_SERVICE : (DepotCommand)0), this->window_number, GetCmdSendToDepot(this->vli.vtype));
    01570             break;
    01571
    01572           default: NOT_REACHED();
    01573         }
    01574         break;
    01575       default: NOT_REACHED();
    01576     }
    01577     this->SetDirty();
    01578   }
    01579
    01580   virtual void OnTick()
    01581   {
    01582     if (_pause_mode != PM_UNPAUSED) return;
    01583     if (this->vehicles.NeedResort()) {
    01584       StationID station = (this->vli.type == VL_STATION_LIST) ? this->vli.index : INVALID_STATION;
    01585
    01586       DEBUG(misc, 3, "Periodic resort %d list company %d at station %d", this->vli.vtype, this->owner, station);
    01587       this->SetDirty();
    01588     }
    01589   }
    01590
    01591   virtual void OnResize()
    01592   {
    01593     this->vscroll->SetCapacityFromWidget(this, VLW_WIDGET_LIST);
    01594     this->GetWidget<NWidgetCore>(VLW_WIDGET_LIST)->widget_data = (this->vscroll->GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
    01595   }
    01596
    01597   virtual void OnInvalidateData(int data)
    01598   {
    01599     if (HasBit(data, 31) && this->vli.type == VL_SHARED_ORDERS) {
    01600       this->vli.index = GB(data, 0, 20);
    01601       this->window_number = this->vli.Pack();
    01602       this->vehicles.ForceRebuild();
    01603       return;
    01604     }
    01605
    01606     if (data == 0) {
    01607       this->vehicles.ForceRebuild();
    01608     } else {
    01609       this->vehicles.ForceResort();
    01610     }
    01611   }
    01612 };
    01613
    01614 static WindowDesc _vehicle_list_desc(
    01615   WDP_AUTO, 260, 246,
    01616   WC_INVALID, WC_NONE,
    01617   WDF_UNCLICK_BUTTONS,
    01618   _nested_vehicle_list, lengthof(_nested_vehicle_list)
    01619 );
    01620
    01621 static void ShowVehicleListWindowLocal(CompanyID company, VehicleListType vlt, VehicleType vehicle_type, uint16 unique_number)
    01622 {
    01623   if (!Company::IsValidID(company)) {
    01624     company = _local_company;
    01625     /* This can happen when opening the vehicle list as a spectator.
    01626      * While it would be cleaner to check this somewhere else, having
    01627      * it here reduces code duplication */
    01628     if (!Company::IsValidID(company)) return;
    01629     _vehicle_list_desc.flags |= WDF_CONSTRUCTION;
    01630   } else {
    01631     _vehicle_list_desc.flags &= ~WDF_CONSTRUCTION;
    01632   }
    01633
    01634   _vehicle_list_desc.cls = GetWindowClassForVehicleType(vehicle_type);
    01635   AllocateWindowDescFront<VehicleListWindow>(&_vehicle_list_desc, VehicleListIdentifier(vlt, vehicle_type, company, unique_number).Pack());
    01636 }
    01637
    01638 void ShowVehicleListWindow(CompanyID company, VehicleType vehicle_type)
    01639 {
    01640   /* If _settings_client.gui.advanced_vehicle_list > 1, display the Advanced list
    01641    * if _settings_client.gui.advanced_vehicle_list == 1, display Advanced list only for local company
    01642    * if _ctrl_pressed, do the opposite action (Advanced list x Normal list)
    01643    */
    01644
    01645   if ((_settings_client.gui.advanced_vehicle_list > (uint)(company != _local_company)) != _ctrl_pressed) {
    01646     ShowCompanyGroup(company, vehicle_type);
    01647   } else {
    01648     ShowVehicleListWindowLocal(company, VL_STANDARD, vehicle_type, company);
    01649   }
    01650 }
    01651
    01652 void ShowVehicleListWindow(const Vehicle *v)
    01653 {
    01654   ShowVehicleListWindowLocal(v->owner, VL_SHARED_ORDERS, v->type, v->FirstShared()->index);
    01655 }
    01656
    01657 void ShowVehicleListWindow(CompanyID company, VehicleType vehicle_type, StationID station)
    01658 {
    01659   ShowVehicleListWindowLocal(company, VL_STATION_LIST, vehicle_type, station);
    01660 }
    01661
    01662 void ShowVehicleListWindow(CompanyID company, VehicleType vehicle_type, TileIndex depot_tile)
    01663 {
    01664   uint16 depot_airport_index;
    01665
    01666   if (vehicle_type == VEH_AIRCRAFT) {
    01667     depot_airport_index = GetStationIndex(depot_tile);
    01668   } else {
    01669     depot_airport_index = GetDepotIndex(depot_tile);
    01670   }
    01671   ShowVehicleListWindowLocal(company, VL_DEPOT_LIST, vehicle_type, depot_airport_index);
    01672 }
    01673
    01674
    01675 /* Unified vehicle GUI - Vehicle Details Window */
    01676
    01678 enum VehicleDetailsWindowWidgets {
    01679   VLD_WIDGET_CAPTION,
    01680   VLD_WIDGET_RENAME_VEHICLE,
    01681   VLD_WIDGET_TOP_DETAILS,
    01682   VLD_WIDGET_INCREASE_SERVICING_INTERVAL,
    01683   VLD_WIDGET_DECREASE_SERVICING_INTERVAL,
    01684   VLD_WIDGET_SERVICING_INTERVAL,
    01685   VLD_WIDGET_MIDDLE_DETAILS,
    01686   VLD_WIDGET_MATRIX,
    01687   VLD_WIDGET_SCROLLBAR,
    01688   VLD_WIDGET_DETAILS_CARGO_CARRIED,
    01689   VLD_WIDGET_DETAILS_TRAIN_VEHICLES,
    01690   VLD_WIDGET_DETAILS_CAPACITY_OF_EACH,
    01691   VLD_WIDGET_DETAILS_TOTAL_CARGO,
    01692 };
    01693
    01694 assert_compile(VLD_WIDGET_DETAILS_CARGO_CARRIED    == VLD_WIDGET_DETAILS_CARGO_CARRIED + TDW_TAB_CARGO   );
    01695 assert_compile(VLD_WIDGET_DETAILS_TRAIN_VEHICLES   == VLD_WIDGET_DETAILS_CARGO_CARRIED + TDW_TAB_INFO    );
    01696 assert_compile(VLD_WIDGET_DETAILS_CAPACITY_OF_EACH == VLD_WIDGET_DETAILS_CARGO_CARRIED + TDW_TAB_CAPACITY);
    01697 assert_compile(VLD_WIDGET_DETAILS_TOTAL_CARGO      == VLD_WIDGET_DETAILS_CARGO_CARRIED + TDW_TAB_TOTALS  );
    01698
    01700 static const NWidgetPart _nested_nontrain_vehicle_details_widgets[] = {
    01701   NWidget(NWID_HORIZONTAL),
    01702     NWidget(WWT_CLOSEBOX, COLOUR_GREY),
    01703     NWidget(WWT_CAPTION, COLOUR_GREY, VLD_WIDGET_CAPTION), SetDataTip(STR_VEHICLE_DETAILS_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    01704     NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, VLD_WIDGET_RENAME_VEHICLE), SetMinimalSize(40, 0), SetMinimalTextLines(1, WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM + 2), SetDataTip(STR_VEHICLE_NAME_BUTTON, STR_NULL /* filled in later */),
    01705     NWidget(WWT_SHADEBOX, COLOUR_GREY),
    01706     NWidget(WWT_STICKYBOX, COLOUR_GREY),
    01707   EndContainer(),
    01708   NWidget(WWT_PANEL, COLOUR_GREY, VLD_WIDGET_TOP_DETAILS), SetMinimalSize(405, 42), SetResize(1, 0), EndContainer(),
    01709   NWidget(WWT_PANEL, COLOUR_GREY, VLD_WIDGET_MIDDLE_DETAILS), SetMinimalSize(405, 45), SetResize(1, 0), EndContainer(),
    01710   NWidget(NWID_HORIZONTAL),
    01711     NWidget(WWT_PUSHARROWBTN, COLOUR_GREY, VLD_WIDGET_DECREASE_SERVICING_INTERVAL), SetFill(0, 1),
    01712         SetDataTip(AWV_DECREASE, STR_VEHICLE_DETAILS_DECREASE_SERVICING_INTERVAL_TOOLTIP),
    01713     NWidget(WWT_PUSHARROWBTN, COLOUR_GREY, VLD_WIDGET_INCREASE_SERVICING_INTERVAL), SetFill(0, 1),
    01714         SetDataTip(AWV_INCREASE, STR_VEHICLE_DETAILS_INCREASE_SERVICING_INTERVAL_TOOLTIP),
    01715     NWidget(WWT_PANEL, COLOUR_GREY, VLD_WIDGET_SERVICING_INTERVAL), SetFill(1, 1), SetResize(1, 0), EndContainer(),
    01716     NWidget(WWT_RESIZEBOX, COLOUR_GREY),
    01717   EndContainer(),
    01718 };
    01719
    01721 static const NWidgetPart _nested_train_vehicle_details_widgets[] = {
    01722   NWidget(NWID_HORIZONTAL),
    01723     NWidget(WWT_CLOSEBOX, COLOUR_GREY),
    01724     NWidget(WWT_CAPTION, COLOUR_GREY, VLD_WIDGET_CAPTION), SetDataTip(STR_VEHICLE_DETAILS_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    01725     NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, VLD_WIDGET_RENAME_VEHICLE), SetMinimalSize(40, 0), SetMinimalTextLines(1, WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM + 2), SetDataTip(STR_VEHICLE_NAME_BUTTON, STR_NULL /* filled in later */),
    01726     NWidget(WWT_SHADEBOX, COLOUR_GREY),
    01727     NWidget(WWT_STICKYBOX, COLOUR_GREY),
    01728   EndContainer(),
    01729   NWidget(WWT_PANEL, COLOUR_GREY, VLD_WIDGET_TOP_DETAILS), SetResize(1, 0), SetMinimalSize(405, 42), EndContainer(),
    01730   NWidget(NWID_HORIZONTAL),
    01731     NWidget(WWT_MATRIX, COLOUR_GREY, VLD_WIDGET_MATRIX), SetResize(1, 1), SetMinimalSize(393, 45), SetDataTip(0x701, STR_NULL), SetFill(1, 0), SetScrollbar(VLD_WIDGET_SCROLLBAR),
    01732     NWidget(NWID_VSCROLLBAR, COLOUR_GREY, VLD_WIDGET_SCROLLBAR),
    01733   EndContainer(),
    01734   NWidget(NWID_HORIZONTAL),
    01735     NWidget(WWT_PUSHARROWBTN, COLOUR_GREY, VLD_WIDGET_DECREASE_SERVICING_INTERVAL), SetFill(0, 1),
    01736         SetDataTip(AWV_DECREASE, STR_VEHICLE_DETAILS_DECREASE_SERVICING_INTERVAL_TOOLTIP),
    01737     NWidget(WWT_PUSHARROWBTN, COLOUR_GREY, VLD_WIDGET_INCREASE_SERVICING_INTERVAL), SetFill(0, 1),
    01738         SetDataTip(AWV_INCREASE, STR_VEHICLE_DETAILS_DECREASE_SERVICING_INTERVAL_TOOLTIP),
    01739     NWidget(WWT_PANEL, COLOUR_GREY, VLD_WIDGET_SERVICING_INTERVAL), SetFill(1, 1), SetResize(1, 0), EndContainer(),
    01740   EndContainer(),
    01741   NWidget(NWID_HORIZONTAL),
    01742     NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, VLD_WIDGET_DETAILS_CARGO_CARRIED), SetMinimalSize(96, 12),
    01743         SetDataTip(STR_VEHICLE_DETAIL_TAB_CARGO, STR_VEHICLE_DETAILS_TRAIN_CARGO_TOOLTIP), SetFill(1, 0), SetResize(1, 0),
    01744     NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, VLD_WIDGET_DETAILS_TRAIN_VEHICLES), SetMinimalSize(99, 12),
    01745         SetDataTip(STR_VEHICLE_DETAIL_TAB_INFORMATION, STR_VEHICLE_DETAILS_TRAIN_INFORMATION_TOOLTIP), SetFill(1, 0), SetResize(1, 0),
    01746     NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, VLD_WIDGET_DETAILS_CAPACITY_OF_EACH), SetMinimalSize(99, 12),
    01747         SetDataTip(STR_VEHICLE_DETAIL_TAB_CAPACITIES, STR_VEHICLE_DETAILS_TRAIN_CAPACITIES_TOOLTIP), SetFill(1, 0), SetResize(1, 0),
    01748     NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, VLD_WIDGET_DETAILS_TOTAL_CARGO), SetMinimalSize(99, 12),
    01749         SetDataTip(STR_VEHICLE_DETAIL_TAB_TOTAL_CARGO, STR_VEHICLE_DETAILS_TRAIN_TOTAL_CARGO_TOOLTIP), SetFill(1, 0), SetResize(1, 0),
    01750     NWidget(WWT_RESIZEBOX, COLOUR_GREY),
    01751   EndContainer(),
    01752 };
    01753
    01754
    01755 extern int GetTrainDetailsWndVScroll(VehicleID veh_id, TrainDetailsWindowTabs det_tab);
    01756 extern void DrawTrainDetails(const Train *v, int left, int right, int y, int vscroll_pos, uint16 vscroll_cap, TrainDetailsWindowTabs det_tab);
    01757 extern void DrawRoadVehDetails(const Vehicle *v, int left, int right, int y);
    01758 extern void DrawShipDetails(const Vehicle *v, int left, int right, int y);
    01759 extern void DrawAircraftDetails(const Aircraft *v, int left, int right, int y);
    01760
    01762 struct VehicleDetailsWindow : Window {
    01763   TrainDetailsWindowTabs tab;
    01764   Scrollbar *vscroll;
    01765
    01767   VehicleDetailsWindow(const WindowDesc *desc, WindowNumber window_number) : Window()
    01768   {
    01769     const Vehicle *v = Vehicle::Get(window_number);
    01770
    01771     this->CreateNestedTree(desc);
    01772     this->vscroll = (v->type == VEH_TRAIN ? this->GetScrollbar(VLD_WIDGET_SCROLLBAR) : NULL);
    01773     this->FinishInitNested(desc, window_number);
    01774
    01775     this->GetWidget<NWidgetCore>(VLD_WIDGET_RENAME_VEHICLE)->tool_tip = STR_VEHICLE_DETAILS_TRAIN_RENAME + v->type;
    01776
    01777     this->owner = v->owner;
    01778     this->tab = TDW_TAB_CARGO;
    01779   }
    01780
    01781   virtual void OnInvalidateData(int data)
    01782   {
    01783     const Vehicle *v = Vehicle::Get(this->window_number);
    01784     if (v->type == VEH_ROAD) {
    01785       const NWidgetBase *nwid_info = this->GetWidget<NWidgetBase>(VLD_WIDGET_MIDDLE_DETAILS);
    01786       uint aimed_height = this->GetRoadVehDetailsHeight(v);
    01787       /* If the number of articulated parts changes, the size of the window must change too. */
    01788       if (aimed_height != nwid_info->current_y) {
    01789         this->ReInit();
    01790       }
    01791     }
    01792   }
    01793
    01799   uint GetRoadVehDetailsHeight(const Vehicle *v)
    01800   {
    01801     uint desired_height;
    01802     if (v->HasArticulatedPart()) {
    01803       /* An articulated RV has its text drawn under the sprite instead of after it, hence 15 pixels extra. */
    01804       desired_height = WD_FRAMERECT_TOP + 15 + 3 * FONT_HEIGHT_NORMAL + 2 + WD_FRAMERECT_BOTTOM;
    01805       /* Add space for the cargo amount for each part. */
    01806       for (const Vehicle *u = v; u != NULL; u = u->Next()) {
    01807         if (u->cargo_cap != 0) desired_height += FONT_HEIGHT_NORMAL + 1;
    01808       }
    01809     } else {
    01810       desired_height = WD_FRAMERECT_TOP + 4 * FONT_HEIGHT_NORMAL + 3 + WD_FRAMERECT_BOTTOM;
    01811     }
    01812     return desired_height;
    01813   }
    01814
    01815   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
    01816   {
    01817     switch (widget) {
    01818       case VLD_WIDGET_TOP_DETAILS: {
    01819         Dimension dim = { 0, 0 };
    01820         size->height = WD_FRAMERECT_TOP + 4 * FONT_HEIGHT_NORMAL + WD_FRAMERECT_BOTTOM;
    01821
    01822         for (uint i = 0; i < 4; i++) SetDParam(i, INT16_MAX);
    01823         static const StringID info_strings[] = {
    01824           STR_VEHICLE_INFO_MAX_SPEED,
    01825           STR_VEHICLE_INFO_WEIGHT_POWER_MAX_SPEED,
    01826           STR_VEHICLE_INFO_WEIGHT_POWER_MAX_SPEED_MAX_TE,
    01827           STR_VEHICLE_INFO_PROFIT_THIS_YEAR_LAST_YEAR,
    01828           STR_VEHICLE_INFO_RELIABILITY_BREAKDOWNS
    01829         };
    01830         for (uint i = 0; i < lengthof(info_strings); i++) {
    01831           dim = maxdim(dim, GetStringBoundingBox(info_strings[i]));
    01832         }
    01833         SetDParam(0, STR_VEHICLE_INFO_AGE);
    01834         dim = maxdim(dim, GetStringBoundingBox(STR_VEHICLE_INFO_AGE_RUNNING_COST_YR));
    01835         size->width = dim.width + WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
    01836         break;
    01837       }
    01838
    01839       case VLD_WIDGET_MIDDLE_DETAILS: {
    01840         const Vehicle *v = Vehicle::Get(this->window_number);
    01841         switch (v->type) {
    01842           case VEH_ROAD:
    01843             size->height = this->GetRoadVehDetailsHeight(v);
    01844             break;
    01845
    01846           case VEH_SHIP:
    01847             size->height = WD_FRAMERECT_TOP + 4 * FONT_HEIGHT_NORMAL + 3 + WD_FRAMERECT_BOTTOM;
    01848             break;
    01849
    01850           case VEH_AIRCRAFT:
    01851             size->height = WD_FRAMERECT_TOP + 5 * FONT_HEIGHT_NORMAL + 4 + WD_FRAMERECT_BOTTOM;
    01852             break;
    01853
    01854           default:
    01855             NOT_REACHED(); // Train uses VLD_WIDGET_MATRIX instead.
    01856         }
    01857         break;
    01858       }
    01859
    01860       case VLD_WIDGET_MATRIX:
    01861         resize->height = WD_MATRIX_TOP + FONT_HEIGHT_NORMAL + WD_MATRIX_BOTTOM;
    01862         size->height = 4 * resize->height;
    01863         break;
    01864
    01865       case VLD_WIDGET_SERVICING_INTERVAL:
    01866         SetDParam(0, 9999); // Roughly the maximum interval
    01867         SetDParam(1, MAX_YEAR * DAYS_IN_YEAR); // Roughly the maximum year
    01868         size->width = max(GetStringBoundingBox(STR_VEHICLE_DETAILS_SERVICING_INTERVAL_PERCENT).width, GetStringBoundingBox(STR_VEHICLE_DETAILS_SERVICING_INTERVAL_DAYS).width) + WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
    01869         size->height = WD_FRAMERECT_TOP + FONT_HEIGHT_NORMAL + WD_FRAMERECT_BOTTOM;
    01870         break;
    01871     }
    01872   }
    01873
    01875   static bool IsVehicleServiceIntervalEnabled(const VehicleType vehicle_type, CompanyID company_id)
    01876   {
    01877     const VehicleDefaultSettings *vds = &Company::Get(company_id)->settings.vehicle;
    01878     switch (vehicle_type) {
    01879       default: NOT_REACHED();
    01880       case VEH_TRAIN:    return vds->servint_trains   != 0;
    01881       case VEH_ROAD:     return vds->servint_roadveh  != 0;
    01882       case VEH_SHIP:     return vds->servint_ships    != 0;
    01883       case VEH_AIRCRAFT: return vds->servint_aircraft != 0;
    01884     }
    01885   }
    01886
    01898   static void DrawVehicleDetails(const Vehicle *v, int left, int right, int y, int vscroll_pos, uint vscroll_cap, TrainDetailsWindowTabs det_tab)
    01899   {
    01900     switch (v->type) {
    01901       case VEH_TRAIN:    DrawTrainDetails(Train::From(v), left, right, y, vscroll_pos, vscroll_cap, det_tab);  break;
    01902       case VEH_ROAD:     DrawRoadVehDetails(v, left, right, y);  break;
    01903       case VEH_SHIP:     DrawShipDetails(v, left, right, y);     break;
    01904       case VEH_AIRCRAFT: DrawAircraftDetails(Aircraft::From(v), left, right, y); break;
    01905       default: NOT_REACHED();
    01906     }
    01907   }
    01908
    01909   virtual void SetStringParameters(int widget) const
    01910   {
    01911     if (widget == VLD_WIDGET_CAPTION) SetDParam(0, Vehicle::Get(this->window_number)->index);
    01912   }
    01913
    01914   virtual void DrawWidget(const Rect &r, int widget) const
    01915   {
    01916     const Vehicle *v = Vehicle::Get(this->window_number);
    01917
    01918     switch (widget) {
    01919       case VLD_WIDGET_TOP_DETAILS: {
    01920         int y = r.top + WD_FRAMERECT_TOP;
    01921
    01922         /* Draw running cost */
    01923         SetDParam(1, v->age / DAYS_IN_LEAP_YEAR);
    01924         SetDParam(0, (v->age + DAYS_IN_YEAR < v->max_age) ? STR_VEHICLE_INFO_AGE : STR_VEHICLE_INFO_AGE_RED);
    01925         SetDParam(2, v->max_age / DAYS_IN_LEAP_YEAR);
    01926         SetDParam(3, v->GetDisplayRunningCost());
    01927         DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_VEHICLE_INFO_AGE_RUNNING_COST_YR);
    01928         y += FONT_HEIGHT_NORMAL;
    01929
    01930         /* Draw max speed */
    01931         StringID string;
    01932         if (v->type == VEH_TRAIN ||
    01933             (v->type == VEH_ROAD && _settings_game.vehicle.roadveh_acceleration_model != AM_ORIGINAL)) {
    01934           const GroundVehicleCache *gcache = v->GetGroundVehicleCache();
    01935           SetDParam(2, v->GetDisplayMaxSpeed());
    01936           SetDParam(1, gcache->cached_power);
    01937           SetDParam(0, gcache->cached_weight);
    01938           SetDParam(3, gcache->cached_max_te / 1000);
    01939           if (v->type == VEH_TRAIN && (_settings_game.vehicle.train_acceleration_model == AM_ORIGINAL ||
    01940               GetRailTypeInfo(Train::From(v)->railtype)->acceleration_type == 2)) {
    01941             string = STR_VEHICLE_INFO_WEIGHT_POWER_MAX_SPEED;
    01942           } else {
    01943             string = STR_VEHICLE_INFO_WEIGHT_POWER_MAX_SPEED_MAX_TE;
    01944           }
    01945         } else {
    01946           SetDParam(0, v->GetDisplayMaxSpeed());
    01947           string = STR_VEHICLE_INFO_MAX_SPEED;
    01948         }
    01949         DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, string);
    01950         y += FONT_HEIGHT_NORMAL;
    01951
    01952         /* Draw profit */
    01953         SetDParam(0, v->GetDisplayProfitThisYear());
    01954         SetDParam(1, v->GetDisplayProfitLastYear());
    01955         DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_VEHICLE_INFO_PROFIT_THIS_YEAR_LAST_YEAR);
    01956         y += FONT_HEIGHT_NORMAL;
    01957
    01958         /* Draw breakdown & reliability */
    01959         SetDParam(0, ToPercent16(v->reliability));
    01960         SetDParam(1, v->breakdowns_since_last_service);
    01961         DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_VEHICLE_INFO_RELIABILITY_BREAKDOWNS);
    01962         break;
    01963       }
    01964
    01965       case VLD_WIDGET_MATRIX:
    01966         /* For trains only. */
    01967         DrawVehicleDetails(v, r.left + WD_MATRIX_LEFT, r.right - WD_MATRIX_RIGHT, r.top + WD_MATRIX_TOP, this->vscroll->GetPosition(), this->vscroll->GetCapacity(), this->tab);
    01968         break;
    01969
    01970       case VLD_WIDGET_MIDDLE_DETAILS: {
    01971         /* For other vehicles, at the place of the matrix. */
    01972         bool rtl = _current_text_dir == TD_RTL;
    01973         uint sprite_width = max<uint>(GetSprite(v->GetImage(rtl ? DIR_E : DIR_W), ST_NORMAL)->width, 70U) + WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
    01974
    01975         uint text_left  = r.left  + (rtl ? 0 : sprite_width);
    01976         uint text_right = r.right - (rtl ? sprite_width : 0);
    01977
    01978         /* Articulated road vehicles use a complete line. */
    01979         if (v->type == VEH_ROAD && v->HasArticulatedPart()) {
    01980           DrawVehicleImage(v, r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + WD_FRAMERECT_TOP, INVALID_VEHICLE, 0);
    01981         } else {
    01982           uint sprite_left  = rtl ? text_right : r.left;
    01983           uint sprite_right = rtl ? r.right : text_left;
    01984
    01985           DrawVehicleImage(v, sprite_left + WD_FRAMERECT_LEFT, sprite_right - WD_FRAMERECT_RIGHT, r.top + WD_FRAMERECT_TOP, INVALID_VEHICLE, 0);
    01986         }
    01987         DrawVehicleDetails(v, text_left + WD_FRAMERECT_LEFT, text_right - WD_FRAMERECT_RIGHT, r.top + WD_FRAMERECT_TOP, 0, 0, this->tab);
    01988         break;
    01989       }
    01990
    01991       case VLD_WIDGET_SERVICING_INTERVAL:
    01992         /* Draw service interval text */
    01993         SetDParam(0, v->service_interval);
    01994         SetDParam(1, v->date_of_last_service);
    01995         DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + (r.bottom - r.top + 1 - FONT_HEIGHT_NORMAL) / 2,
    01996             Company::Get(v->owner)->settings.vehicle.servint_ispercent ? STR_VEHICLE_DETAILS_SERVICING_INTERVAL_PERCENT : STR_VEHICLE_DETAILS_SERVICING_INTERVAL_DAYS);
    01997         break;
    01998     }
    01999   }
    02000
    02002   virtual void OnPaint()
    02003   {
    02004     const Vehicle *v = Vehicle::Get(this->window_number);
    02005
    02006     this->SetWidgetDisabledState(VLD_WIDGET_RENAME_VEHICLE, v->owner != _local_company);
    02007
    02008     if (v->type == VEH_TRAIN) {
    02009       this->DisableWidget(this->tab + VLD_WIDGET_DETAILS_CARGO_CARRIED);
    02010       this->vscroll->SetCount(GetTrainDetailsWndVScroll(v->index, this->tab));
    02011     }
    02012
    02013     /* Disable service-scroller when interval is set to disabled */
    02014     this->SetWidgetsDisabledState(!IsVehicleServiceIntervalEnabled(v->type, v->owner),
    02015       VLD_WIDGET_INCREASE_SERVICING_INTERVAL,
    02016       VLD_WIDGET_DECREASE_SERVICING_INTERVAL,
    02017       WIDGET_LIST_END);
    02018
    02019     this->DrawWidgets();
    02020   }
    02021
    02022   virtual void OnClick(Point pt, int widget, int click_count)
    02023   {
    02024     switch (widget) {
    02025       case VLD_WIDGET_RENAME_VEHICLE: { // rename
    02026         const Vehicle *v = Vehicle::Get(this->window_number);
    02027         SetDParam(0, v->index);
    02028         ShowQueryString(STR_VEHICLE_NAME, STR_QUERY_RENAME_TRAIN_CAPTION + v->type,
    02029             MAX_LENGTH_VEHICLE_NAME_CHARS, MAX_LENGTH_VEHICLE_NAME_PIXELS, this, CS_ALPHANUMERAL, QSF_ENABLE_DEFAULT | QSF_LEN_IN_CHARS);
    02030         break;
    02031       }
    02032
    02033       case VLD_WIDGET_INCREASE_SERVICING_INTERVAL:   // increase int
    02034       case VLD_WIDGET_DECREASE_SERVICING_INTERVAL: { // decrease int
    02035         int mod = _ctrl_pressed ? 5 : 10;
    02036         const Vehicle *v = Vehicle::Get(this->window_number);
    02037
    02038         mod = (widget == VLD_WIDGET_DECREASE_SERVICING_INTERVAL) ? -mod : mod;
    02039         mod = GetServiceIntervalClamped(mod + v->service_interval, v->owner);
    02040         if (mod == v->service_interval) return;
    02041
    02042         DoCommandP(v->tile, v->index, mod, CMD_CHANGE_SERVICE_INT | CMD_MSG(STR_ERROR_CAN_T_CHANGE_SERVICING));
    02043         break;
    02044       }
    02045
    02046       case VLD_WIDGET_DETAILS_CARGO_CARRIED:
    02047       case VLD_WIDGET_DETAILS_TRAIN_VEHICLES:
    02048       case VLD_WIDGET_DETAILS_CAPACITY_OF_EACH:
    02049       case VLD_WIDGET_DETAILS_TOTAL_CARGO:
    02050         this->SetWidgetsDisabledState(false,
    02051           VLD_WIDGET_DETAILS_CARGO_CARRIED,
    02052           VLD_WIDGET_DETAILS_TRAIN_VEHICLES,
    02053           VLD_WIDGET_DETAILS_CAPACITY_OF_EACH,
    02054           VLD_WIDGET_DETAILS_TOTAL_CARGO,
    02055           widget,
    02056           WIDGET_LIST_END);
    02057
    02058         this->tab = (TrainDetailsWindowTabs)(widget - VLD_WIDGET_DETAILS_CARGO_CARRIED);
    02059         this->SetDirty();
    02060         break;
    02061     }
    02062   }
    02063
    02064   virtual void OnQueryTextFinished(char *str)
    02065   {
    02066     if (str == NULL) return;
    02067
    02068     DoCommandP(0, this->window_number, 0, CMD_RENAME_VEHICLE | CMD_MSG(STR_ERROR_CAN_T_RENAME_TRAIN + Vehicle::Get(this->window_number)->type), NULL, str);
    02069   }
    02070
    02071   virtual void OnResize()
    02072   {
    02073     NWidgetCore *nwi = this->GetWidget<NWidgetCore>(VLD_WIDGET_MATRIX);
    02074     if (nwi != NULL) {
    02075       this->vscroll->SetCapacityFromWidget(this, VLD_WIDGET_MATRIX);
    02076       nwi->widget_data = (this->vscroll->GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
    02077     }
    02078   }
    02079 };
    02080
    02082 static const WindowDesc _train_vehicle_details_desc(
    02083   WDP_AUTO, 405, 178,
    02084   WC_VEHICLE_DETAILS, WC_VEHICLE_VIEW,
    02085   WDF_UNCLICK_BUTTONS,
    02086   _nested_train_vehicle_details_widgets, lengthof(_nested_train_vehicle_details_widgets)
    02087 );
    02088
    02090 static const WindowDesc _nontrain_vehicle_details_desc(
    02091   WDP_AUTO, 405, 113,
    02092   WC_VEHICLE_DETAILS, WC_VEHICLE_VIEW,
    02093   WDF_UNCLICK_BUTTONS,
    02094   _nested_nontrain_vehicle_details_widgets, lengthof(_nested_nontrain_vehicle_details_widgets)
    02095 );
    02096
    02098 static void ShowVehicleDetailsWindow(const Vehicle *v)
    02099 {
    02100   DeleteWindowById(WC_VEHICLE_ORDERS, v->index, false);
    02101   DeleteWindowById(WC_VEHICLE_TIMETABLE, v->index, false);
    02102   AllocateWindowDescFront<VehicleDetailsWindow>((v->type == VEH_TRAIN) ? &_train_vehicle_details_desc : &_nontrain_vehicle_details_desc, v->index);
    02103 }
    02104
    02105
    02106 /* Unified vehicle GUI - Vehicle View Window */
    02107
    02109 static const NWidgetPart _nested_vehicle_view_widgets[] = {
    02110   NWidget(NWID_HORIZONTAL),
    02111     NWidget(WWT_CLOSEBOX, COLOUR_GREY),
    02112     NWidget(WWT_CAPTION, COLOUR_GREY, VVW_WIDGET_CAPTION), SetDataTip(STR_VEHICLE_VIEW_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    02113     NWidget(WWT_DEBUGBOX, COLOUR_GREY),
    02114     NWidget(WWT_SHADEBOX, COLOUR_GREY),
    02115     NWidget(WWT_STICKYBOX, COLOUR_GREY),
    02116   EndContainer(),
    02117   NWidget(NWID_HORIZONTAL),
    02118     NWidget(WWT_PANEL, COLOUR_GREY),
    02119       NWidget(WWT_INSET, COLOUR_GREY), SetPadding(2, 2, 2, 2),
    02120         NWidget(NWID_VIEWPORT, INVALID_COLOUR, VVW_WIDGET_VIEWPORT), SetMinimalSize(226, 84), SetResize(1, 1), SetPadding(1, 1, 1, 1),
    02121       EndContainer(),
    02122     EndContainer(),
    02123     NWidget(NWID_VERTICAL),
    02124       NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, VVW_WIDGET_CENTER_MAIN_VIEH), SetMinimalSize(18, 18), SetFill(1, 1), SetDataTip(SPR_CENTRE_VIEW_VEHICLE, 0x0 /* filled later */),
    02125       NWidget(NWID_SELECTION, INVALID_COLOUR, VVW_WIDGET_SELECT_DEPOT_CLONE),
    02126         NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, VVW_WIDGET_GOTO_DEPOT), SetMinimalSize(18, 18), SetFill(1, 1), SetDataTip(0x0 /* filled later */, 0x0 /* filled later */),
    02127         NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, VVW_WIDGET_CLONE_VEH), SetMinimalSize(18, 18), SetFill(1, 1), SetDataTip(0x0 /* filled later */, 0x0 /* filled later */),
    02128       EndContainer(),
    02129       /* For trains only, 'ignore signal' button. */
    02130       NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, VVW_WIDGET_FORCE_PROCEED), SetMinimalSize(18, 18), SetFill(1, 1),
    02131                       SetDataTip(SPR_IGNORE_SIGNALS, STR_VEHICLE_VIEW_TRAIN_IGNORE_SIGNAL_TOOLTIP),
    02132       NWidget(NWID_SELECTION, INVALID_COLOUR, VVW_WIDGET_SELECT_REFIT_TURN),
    02133         NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, VVW_WIDGET_REFIT_VEH), SetMinimalSize(18, 18), SetFill(1, 1), SetDataTip(SPR_REFIT_VEHICLE, 0x0 /* filled later */),
    02134         NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, VVW_WIDGET_TURN_AROUND), SetMinimalSize(18, 18), SetFill(1, 1),
    02135                         SetDataTip(SPR_FORCE_VEHICLE_TURN, STR_VEHICLE_VIEW_ROAD_VEHICLE_REVERSE_TOOLTIP),
    02136       EndContainer(),
    02137       NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, VVW_WIDGET_SHOW_ORDERS), SetFill(1, 1), SetMinimalSize(18, 18), SetDataTip(SPR_SHOW_ORDERS, 0x0 /* filled later */),
    02138       NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, VVW_WIDGET_SHOW_DETAILS), SetFill(1, 1), SetMinimalSize(18, 18), SetDataTip(SPR_SHOW_VEHICLE_DETAILS, 0x0 /* filled later */),
    02139       NWidget(WWT_PANEL, COLOUR_GREY), SetFill(1, 1), SetMinimalSize(18, 0), SetResize(0, 1), EndContainer(),
    02140     EndContainer(),
    02141   EndContainer(),
    02142   NWidget(NWID_HORIZONTAL),
    02143     NWidget(WWT_PUSHBTN, COLOUR_GREY, VVW_WIDGET_START_STOP_VEH), SetMinimalTextLines(1, WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM), SetResize(1, 0), SetFill(1, 0),
    02144     NWidget(WWT_RESIZEBOX, COLOUR_GREY),
    02145   EndContainer(),
    02146 };
    02147
    02149 static const WindowDesc _vehicle_view_desc(
    02150   WDP_AUTO, 250, 116,
    02151   WC_VEHICLE_VIEW, WC_NONE,
    02152   WDF_UNCLICK_BUTTONS,
    02153   _nested_vehicle_view_widgets, lengthof(_nested_vehicle_view_widgets)
    02154 );
    02155
    02160 static const WindowDesc _train_view_desc(
    02161   WDP_AUTO, 250, 134,
    02162   WC_VEHICLE_VIEW, WC_NONE,
    02163   WDF_UNCLICK_BUTTONS,
    02164   _nested_vehicle_view_widgets, lengthof(_nested_vehicle_view_widgets)
    02165 );
    02166
    02167
    02168 /* Just to make sure, nobody has changed the vehicle type constants, as we are
    02169    using them for array indexing in a number of places here. */
    02170 assert_compile(VEH_TRAIN == 0);
    02171 assert_compile(VEH_ROAD == 1);
    02172 assert_compile(VEH_SHIP == 2);
    02173 assert_compile(VEH_AIRCRAFT == 3);
    02174
    02176 static const ZoomLevel _vehicle_view_zoom_levels[] = {
    02177   ZOOM_LVL_TRAIN,
    02178   ZOOM_LVL_ROADVEH,
    02179   ZOOM_LVL_SHIP,
    02180   ZOOM_LVL_AIRCRAFT,
    02181 };
    02182
    02183 /* Constants for geometry of vehicle view viewport */
    02184 static const int VV_INITIAL_VIEWPORT_WIDTH = 226;
    02185 static const int VV_INITIAL_VIEWPORT_HEIGHT = 84;
    02186 static const int VV_INITIAL_VIEWPORT_HEIGHT_TRAIN = 102;
    02187
    02189 enum VehicleCommandTranslation {
    02190   VCT_CMD_START_STOP = 0,
    02191   VCT_CMD_CLONE_VEH,
    02192   VCT_CMD_TURN_AROUND,
    02193 };
    02194
    02196 static const uint32 _vehicle_command_translation_table[][4] = {
    02197   { // VCT_CMD_START_STOP
    02198     CMD_START_STOP_VEHICLE | CMD_MSG(STR_ERROR_CAN_T_STOP_START_TRAIN),
    02199     CMD_START_STOP_VEHICLE | CMD_MSG(STR_ERROR_CAN_T_STOP_START_ROAD_VEHICLE),
    02200     CMD_START_STOP_VEHICLE | CMD_MSG(STR_ERROR_CAN_T_STOP_START_SHIP),
    02201     CMD_START_STOP_VEHICLE | CMD_MSG(STR_ERROR_CAN_T_STOP_START_AIRCRAFT)
    02202   },
    02203   { // VCT_CMD_CLONE_VEH
    02204     CMD_CLONE_VEHICLE | CMD_MSG(STR_ERROR_CAN_T_BUY_TRAIN),
    02205     CMD_CLONE_VEHICLE | CMD_MSG(STR_ERROR_CAN_T_BUY_ROAD_VEHICLE),
    02206     CMD_CLONE_VEHICLE | CMD_MSG(STR_ERROR_CAN_T_BUY_SHIP),
    02207     CMD_CLONE_VEHICLE | CMD_MSG(STR_ERROR_CAN_T_BUY_AIRCRAFT)
    02208   },
    02209   { // VCT_CMD_TURN_AROUND
    02210     CMD_REVERSE_TRAIN_DIRECTION | CMD_MSG(STR_ERROR_CAN_T_REVERSE_DIRECTION_TRAIN),
    02211     CMD_TURN_ROADVEH            | CMD_MSG(STR_ERROR_CAN_T_MAKE_ROAD_VEHICLE_TURN),
    02212     0xffffffff, // invalid for ships
    02213     0xffffffff  // invalid for aircrafts
    02214   },
    02215 };
    02216
    02224 void CcStartStopVehicle(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
    02225 {
    02226   if (result.Failed()) return;
    02227
    02228   const Vehicle *v = Vehicle::GetIfValid(p1);
    02229   if (v == NULL || !v->IsPrimaryVehicle() || v->owner != _local_company) return;
    02230
    02231   StringID msg = (v->vehstatus & VS_STOPPED) ? STR_VEHICLE_COMMAND_STOPPED : STR_VEHICLE_COMMAND_STARTED;
    02232   Point pt = RemapCoords(v->x_pos, v->y_pos, v->z_pos);
    02233   AddTextEffect(msg, pt.x, pt.y, DAY_TICKS, TE_RISING);
    02234 }
    02235
    02241 void StartStopVehicle(const Vehicle *v, bool texteffect)
    02242 {
    02243   assert(v->IsPrimaryVehicle());
    02244   DoCommandP(v->tile, v->index, 0, _vehicle_command_translation_table[VCT_CMD_START_STOP][v->type], texteffect ? CcStartStopVehicle : NULL);
    02245 }
    02246
    02248 static bool IsVehicleRefitable(const Vehicle *v)
    02249 {
    02250   if (!v->IsStoppedInDepot()) return false;
    02251
    02252   do {
    02253     if (IsEngineRefittable(v->engine_type)) return true;
    02254   } while (v->IsGroundVehicle() && (v = v->Next()) != NULL);
    02255
    02256   return false;
    02257 }
    02258
    02260 struct VehicleViewWindow : Window {
    02261 private:
    02263   enum PlaneSelections {
    02264     SEL_DC_GOTO_DEPOT,
    02265     SEL_DC_CLONE,
    02266
    02267     SEL_RT_REFIT,
    02268     SEL_RT_TURN_AROUND,
    02269
    02270     SEL_DC_BASEPLANE = SEL_DC_GOTO_DEPOT,
    02271     SEL_RT_BASEPLANE = SEL_RT_REFIT,
    02272   };
    02273
    02278   void SelectPlane(PlaneSelections plane)
    02279   {
    02280     switch (plane) {
    02281       case SEL_DC_GOTO_DEPOT:
    02282       case SEL_DC_CLONE:
    02283         this->GetWidget<NWidgetStacked>(VVW_WIDGET_SELECT_DEPOT_CLONE)->SetDisplayedPlane(plane - SEL_DC_BASEPLANE);
    02284         break;
    02285
    02286       case SEL_RT_REFIT:
    02287       case SEL_RT_TURN_AROUND:
    02288         this->GetWidget<NWidgetStacked>(VVW_WIDGET_SELECT_REFIT_TURN)->SetDisplayedPlane(plane - SEL_RT_BASEPLANE);
    02289         break;
    02290
    02291       default:
    02292         NOT_REACHED();
    02293     }
    02294   }
    02295
    02296 public:
    02297   VehicleViewWindow(const WindowDesc *desc, WindowNumber window_number) : Window()
    02298   {
    02299     this->CreateNestedTree(desc);
    02300
    02301     /* Sprites for the 'send to depot' button indexed by vehicle type. */
    02302     static const SpriteID vehicle_view_goto_depot_sprites[] = {
    02303       SPR_SEND_TRAIN_TODEPOT,
    02304       SPR_SEND_ROADVEH_TODEPOT,
    02305       SPR_SEND_SHIP_TODEPOT,
    02306       SPR_SEND_AIRCRAFT_TODEPOT,
    02307     };
    02308     const Vehicle *v = Vehicle::Get(window_number);
    02309     this->GetWidget<NWidgetCore>(VVW_WIDGET_GOTO_DEPOT)->widget_data = vehicle_view_goto_depot_sprites[v->type];
    02310
    02311     /* Sprites for the 'clone vehicle' button indexed by vehicle type. */
    02312     static const SpriteID vehicle_view_clone_sprites[] = {
    02313       SPR_CLONE_TRAIN,
    02314       SPR_CLONE_ROADVEH,
    02315       SPR_CLONE_SHIP,
    02316       SPR_CLONE_AIRCRAFT,
    02317     };
    02318     this->GetWidget<NWidgetCore>(VVW_WIDGET_CLONE_VEH)->widget_data = vehicle_view_clone_sprites[v->type];
    02319
    02320     switch (v->type) {
    02321       case VEH_TRAIN:
    02322         this->GetWidget<NWidgetCore>(VVW_WIDGET_TURN_AROUND)->tool_tip = STR_VEHICLE_VIEW_TRAIN_REVERSE_TOOLTIP;
    02323         break;
    02324
    02325       case VEH_ROAD:
    02326         break;
    02327
    02328       case VEH_SHIP:
    02329       case VEH_AIRCRAFT:
    02330         this->SelectPlane(SEL_RT_REFIT);
    02331         break;
    02332
    02333       default: NOT_REACHED();
    02334     }
    02335     this->FinishInitNested(desc, window_number);
    02336     this->owner = v->owner;
    02337     this->GetWidget<NWidgetViewport>(VVW_WIDGET_VIEWPORT)->InitializeViewport(this, this->window_number | (1 << 31), _vehicle_view_zoom_levels[v->type]);
    02338
    02339     this->GetWidget<NWidgetCore>(VVW_WIDGET_START_STOP_VEH)->tool_tip   = STR_VEHICLE_VIEW_TRAIN_STATE_START_STOP_TOOLTIP + v->type;
    02340     this->GetWidget<NWidgetCore>(VVW_WIDGET_CENTER_MAIN_VIEH)->tool_tip = STR_VEHICLE_VIEW_TRAIN_LOCATION_TOOLTIP + v->type;
    02341     this->GetWidget<NWidgetCore>(VVW_WIDGET_REFIT_VEH)->tool_tip        = STR_VEHICLE_VIEW_TRAIN_REFIT_TOOLTIP + v->type;
    02342     this->GetWidget<NWidgetCore>(VVW_WIDGET_GOTO_DEPOT)->tool_tip       = STR_VEHICLE_VIEW_TRAIN_SEND_TO_DEPOT_TOOLTIP + v->type;
    02343     this->GetWidget<NWidgetCore>(VVW_WIDGET_SHOW_ORDERS)->tool_tip      = STR_VEHICLE_VIEW_TRAIN_ORDERS_TOOLTIP + v->type;
    02344     this->GetWidget<NWidgetCore>(VVW_WIDGET_SHOW_DETAILS)->tool_tip     = STR_VEHICLE_VIEW_TRAIN_SHOW_DETAILS_TOOLTIP + v->type;
    02345     this->GetWidget<NWidgetCore>(VVW_WIDGET_CLONE_VEH)->tool_tip        = STR_VEHICLE_VIEW_CLONE_TRAIN_INFO + v->type;
    02346   }
    02347
    02348   ~VehicleViewWindow()
    02349   {
    02350     DeleteWindowById(WC_VEHICLE_ORDERS, this->window_number, false);
    02351     DeleteWindowById(WC_VEHICLE_REFIT, this->window_number, false);
    02352     DeleteWindowById(WC_VEHICLE_DETAILS, this->window_number, false);
    02353     DeleteWindowById(WC_VEHICLE_TIMETABLE, this->window_number, false);
    02354   }
    02355
    02356   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
    02357   {
    02358     const Vehicle *v = Vehicle::Get(this->window_number);
    02359     switch (widget) {
    02360       case VVW_WIDGET_FORCE_PROCEED:
    02361         if (v->type != VEH_TRAIN) {
    02362           size->height = 0;
    02363           size->width = 0;
    02364         }
    02365         break;
    02366
    02367       case VVW_WIDGET_VIEWPORT:
    02368         size->width = VV_INITIAL_VIEWPORT_WIDTH;
    02369         size->height = (v->type == VEH_TRAIN) ? VV_INITIAL_VIEWPORT_HEIGHT_TRAIN : VV_INITIAL_VIEWPORT_HEIGHT;
    02370         break;
    02371     }
    02372   }
    02373
    02374   virtual void OnPaint()
    02375   {
    02376     const Vehicle *v = Vehicle::Get(this->window_number);
    02377     bool is_localcompany = v->owner == _local_company;
    02378     bool refitable_and_stopped_in_depot = IsVehicleRefitable(v);
    02379
    02380     this->SetWidgetDisabledState(VVW_WIDGET_GOTO_DEPOT, !is_localcompany);
    02381     this->SetWidgetDisabledState(VVW_WIDGET_REFIT_VEH, !refitable_and_stopped_in_depot || !is_localcompany);
    02382     this->SetWidgetDisabledState(VVW_WIDGET_CLONE_VEH, !is_localcompany);
    02383
    02384     if (v->type == VEH_TRAIN) {
    02385       this->SetWidgetLoweredState(VVW_WIDGET_FORCE_PROCEED, Train::From(v)->force_proceed == TFP_SIGNAL);
    02386       this->SetWidgetDisabledState(VVW_WIDGET_FORCE_PROCEED, !is_localcompany);
    02387       this->SetWidgetDisabledState(VVW_WIDGET_TURN_AROUND, !is_localcompany);
    02388     }
    02389
    02390     this->DrawWidgets();
    02391   }
    02392
    02393   virtual void SetStringParameters(int widget) const
    02394   {
    02395     if (widget != VVW_WIDGET_CAPTION) return;
    02396
    02397     const Vehicle *v = Vehicle::Get(this->window_number);
    02398     SetDParam(0, v->index);
    02399   }
    02400
    02401   virtual void DrawWidget(const Rect &r, int widget) const
    02402   {
    02403     if (widget != VVW_WIDGET_START_STOP_VEH) return;
    02404
    02405     const Vehicle *v = Vehicle::Get(this->window_number);
    02406     StringID str;
    02407     if (v->vehstatus & VS_CRASHED) {
    02408       str = STR_VEHICLE_STATUS_CRASHED;
    02409     } else if (v->type != VEH_AIRCRAFT && v->breakdown_ctr == 1) { // check for aircraft necessary?
    02410       str = STR_VEHICLE_STATUS_BROKEN_DOWN;
    02411     } else if (v->vehstatus & VS_STOPPED) {
    02412       if (v->type == VEH_TRAIN) {
    02413         if (v->cur_speed == 0) {
    02414           if (Train::From(v)->gcache.cached_power == 0) {
    02415             str = STR_VEHICLE_STATUS_TRAIN_NO_POWER;
    02416           } else {
    02417             str = STR_VEHICLE_STATUS_STOPPED;
    02418           }
    02419         } else {
    02420           SetDParam(0, v->GetDisplaySpeed());
    02421           str = STR_VEHICLE_STATUS_TRAIN_STOPPING_VEL;
    02422         }
    02423       } else { // no train
    02424         str = STR_VEHICLE_STATUS_STOPPED;
    02425       }
    02426     } else if (v->type == VEH_TRAIN && HasBit(Train::From(v)->flags, VRF_TRAIN_STUCK) && !v->current_order.IsType(OT_LOADING)) {
    02427       str = STR_VEHICLE_STATUS_TRAIN_STUCK;
    02428     } else { // vehicle is in a "normal" state, show current order
    02429       switch (v->current_order.GetType()) {
    02430         case OT_GOTO_STATION: {
    02431           SetDParam(0, v->current_order.GetDestination());
    02432           SetDParam(1, v->GetDisplaySpeed());
    02433           str = STR_VEHICLE_STATUS_HEADING_FOR_STATION_VEL;
    02434           break;
    02435         }
    02436
    02437         case OT_GOTO_DEPOT: {
    02438           SetDParam(0, v->type);
    02439           SetDParam(1, v->current_order.GetDestination());
    02440           SetDParam(2, v->GetDisplaySpeed());
    02441           if (v->current_order.GetDepotActionType() & ODATFB_NEAREST_DEPOT) {
    02442             /* This case *only* happens when multiple nearest depot orders
    02443              * follow eachother (including an order list only one order: a
    02444              * nearest depot order) and there are no reachable depots.
    02445              * It is primarily to guard for the case that there is no
    02446              * depot with index 0, which would be used as fallback for
    02447              * evaluating the string in the status bar. */
    02448             str = STR_EMPTY;
    02449           } else if (v->current_order.GetDepotActionType() & ODATFB_HALT) {
    02450             str = STR_VEHICLE_STATUS_HEADING_FOR_DEPOT_VEL;
    02451           } else {
    02452             str = STR_VEHICLE_STATUS_HEADING_FOR_DEPOT_SERVICE_VEL;
    02453           }
    02454           break;
    02455         }
    02456
    02457         case OT_LOADING:
    02458           str = STR_VEHICLE_STATUS_LOADING_UNLOADING;
    02459           break;
    02460
    02461         case OT_GOTO_WAYPOINT: {
    02462           assert(v->type == VEH_TRAIN || v->type == VEH_SHIP);
    02463           SetDParam(0, v->current_order.GetDestination());
    02464           str = STR_VEHICLE_STATUS_HEADING_FOR_WAYPOINT_VEL;
    02465           SetDParam(1, v->GetDisplaySpeed());
    02466           break;
    02467         }
    02468
    02469         case OT_LEAVESTATION:
    02470           if (v->type != VEH_AIRCRAFT) {
    02471             str = STR_VEHICLE_STATUS_LEAVING;
    02472             break;
    02473           }
    02474           /* FALL THROUGH, if aircraft. Does this even happen? */
    02475
    02476         default:
    02477           if (v->GetNumManualOrders() == 0) {
    02478             str = STR_VEHICLE_STATUS_NO_ORDERS_VEL;
    02479             SetDParam(0, v->GetDisplaySpeed());
    02480           } else {
    02481             str = STR_EMPTY;
    02482           }
    02483           break;
    02484       }
    02485     }
    02486
    02487     /* draw the flag plus orders */
    02488     DrawSprite(v->vehstatus & VS_STOPPED ? SPR_FLAG_VEH_STOPPED : SPR_FLAG_VEH_RUNNING, PAL_NONE, WD_FRAMERECT_LEFT, r.top + WD_FRAMERECT_TOP);
    02489     DrawString(r.left + WD_FRAMERECT_LEFT + 6, r.right - WD_FRAMERECT_RIGHT, r.top + WD_FRAMERECT_TOP, str, TC_FROMSTRING, SA_HOR_CENTER);
    02490   }
    02491
    02492   virtual void OnClick(Point pt, int widget, int click_count)
    02493   {
    02494     const Vehicle *v = Vehicle::Get(this->window_number);
    02495
    02496     switch (widget) {
    02497       case VVW_WIDGET_START_STOP_VEH: // start stop
    02498         if (_ctrl_pressed) {
    02499           /* Scroll to current order destination */
    02500           TileIndex tile = v->current_order.GetLocation(v);
    02501           if (tile != INVALID_TILE) ScrollMainWindowToTile(tile);
    02502         } else {
    02503           /* Start/Stop */
    02504           StartStopVehicle(v, false);
    02505         }
    02506         break;
    02507       case VVW_WIDGET_CENTER_MAIN_VIEH: {// center main view
    02508         const Window *mainwindow = FindWindowById(WC_MAIN_WINDOW, 0);
    02509         /* code to allow the main window to 'follow' the vehicle if the ctrl key is pressed */
    02510         if (_ctrl_pressed && mainwindow->viewport->zoom == ZOOM_LVL_NORMAL) {
    02511           mainwindow->viewport->follow_vehicle = v->index;
    02512         } else {
    02513           ScrollMainWindowTo(v->x_pos, v->y_pos, v->z_pos);
    02514         }
    02515         break;
    02516       }
    02517
    02518       case VVW_WIDGET_GOTO_DEPOT: // goto hangar
    02519         DoCommandP(v->tile, v->index | (_ctrl_pressed ? DEPOT_SERVICE : 0U), 0, GetCmdSendToDepot(v));
    02520         break;
    02521       case VVW_WIDGET_REFIT_VEH: // refit
    02522         ShowVehicleRefitWindow(v, INVALID_VEH_ORDER_ID, this);
    02523         break;
    02524       case VVW_WIDGET_SHOW_ORDERS: // show orders
    02525         if (_ctrl_pressed) {
    02526           ShowTimetableWindow(v);
    02527         } else {
    02528           ShowOrdersWindow(v);
    02529         }
    02530         break;
    02531       case VVW_WIDGET_SHOW_DETAILS: // show details
    02532         ShowVehicleDetailsWindow(v);
    02533         break;
    02534       case VVW_WIDGET_CLONE_VEH: // clone vehicle
    02535         DoCommandP(v->tile, v->index, _ctrl_pressed ? 1 : 0,
    02536                     _vehicle_command_translation_table[VCT_CMD_CLONE_VEH][v->type],
    02537                     CcCloneVehicle);
    02538         break;
    02539       case VVW_WIDGET_TURN_AROUND: // turn around
    02540         assert(v->IsGroundVehicle());
    02541         DoCommandP(v->tile, v->index, 0,
    02542                     _vehicle_command_translation_table[VCT_CMD_TURN_AROUND][v->type]);
    02543         break;
    02544       case VVW_WIDGET_FORCE_PROCEED: // force proceed
    02545         assert(v->type == VEH_TRAIN);
    02546         DoCommandP(v->tile, v->index, 0, CMD_FORCE_TRAIN_PROCEED | CMD_MSG(STR_ERROR_CAN_T_MAKE_TRAIN_PASS_SIGNAL));
    02547         break;
    02548     }
    02549   }
    02550
    02551   virtual void OnResize()
    02552   {
    02553     if (this->viewport != NULL) {
    02554       NWidgetViewport *nvp = this->GetWidget<NWidgetViewport>(VVW_WIDGET_VIEWPORT);
    02555       nvp->UpdateViewportCoordinates(this);
    02556     }
    02557   }
    02558
    02559   virtual void OnTick()
    02560   {
    02561     const Vehicle *v = Vehicle::Get(this->window_number);
    02562     bool veh_stopped = v->IsStoppedInDepot();
    02563
    02564     /* Widget VVW_WIDGET_GOTO_DEPOT must be hidden if the vehicle is already stopped in depot.
    02565      * Widget VVW_WIDGET_CLONE_VEH should then be shown, since cloning is allowed only while in depot and stopped.
    02566      */
    02567     PlaneSelections plane = veh_stopped ? SEL_DC_CLONE : SEL_DC_GOTO_DEPOT;
    02568     NWidgetStacked *nwi = this->GetWidget<NWidgetStacked>(VVW_WIDGET_SELECT_DEPOT_CLONE); // Selection widget 'send to depot' / 'clone'.
    02569     if (nwi->shown_plane + SEL_DC_BASEPLANE != plane) {
    02570       this->SelectPlane(plane);
    02571       this->SetWidgetDirty(VVW_WIDGET_SELECT_DEPOT_CLONE);
    02572     }
    02573     /* The same system applies to widget VVW_WIDGET_REFIT_VEH and VVW_WIDGET_TURN_AROUND.*/
    02574     if (v->IsGroundVehicle()) {
    02575       PlaneSelections plane = veh_stopped ? SEL_RT_REFIT : SEL_RT_TURN_AROUND;
    02576       NWidgetStacked *nwi = this->GetWidget<NWidgetStacked>(VVW_WIDGET_SELECT_REFIT_TURN);
    02577       if (nwi->shown_plane + SEL_RT_BASEPLANE != plane) {
    02578         this->SelectPlane(plane);
    02579         this->SetWidgetDirty(VVW_WIDGET_SELECT_REFIT_TURN);
    02580       }
    02581     }
    02582   }
    02583
    02584   virtual bool IsNewGRFInspectable() const
    02585   {
    02586     return ::IsNewGRFInspectable(GetGrfSpecFeature(Vehicle::Get(this->window_number)->type), this->window_number);
    02587   }
    02588
    02589   virtual void ShowNewGRFInspectWindow() const
    02590   {
    02591 		::ShowNewGRFInspectWindow(GetGrfSpecFeature(Vehicle::Get(this->window_number)->type), this->window_number);
    02592   }
    02593 };
    02594
    02595
    02597 void ShowVehicleViewWindow(const Vehicle *v)
    02598 {
    02599   AllocateWindowDescFront<VehicleViewWindow>((v->type == VEH_TRAIN) ? &_train_view_desc : &_vehicle_view_desc, v->index);
    02600 }
    02601
    02607 bool VehicleClicked(const Vehicle *v)
    02608 {
    02609   assert(v != NULL);
    02610   if (!(_thd.place_mode & HT_VEHICLE)) return false;
    02611
    02612   v = v->First();
    02613   if (!v->IsPrimaryVehicle()) return false;
    02614
    02615   _thd.GetCallbackWnd()->OnVehicleSelect(v);
    02616   return true;
    02617 }
    02618
    02619 void StopGlobalFollowVehicle(const Vehicle *v)
    02620 {
    02621   Window *w = FindWindowById(WC_MAIN_WINDOW, 0);
    02622   if (w != NULL && w->viewport->follow_vehicle == v->index) {
    02623     ScrollMainWindowTo(v->x_pos, v->y_pos, v->z_pos, true); // lock the main view on the vehicle's last position
    02624     w->viewport->follow_vehicle = INVALID_VEHICLE;
    02625   }
    02626 }
    02627
    02628
    02636 void CcBuildPrimaryVehicle(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
    02637 {
    02638   if (result.Failed()) return;
    02639
    02640   const Vehicle *v = Vehicle::Get(_new_vehicle_id);
    02641   ShowVehicleViewWindow(v);
    02642 }
    02643
    02649 int GetVehicleWidth(Vehicle *v)
    02650 {
    02651   int vehicle_width = 0;
    02652
    02653   switch (v->type) {
    02654     case VEH_TRAIN:
    02655       for (const Train *u = Train::From(v); u != NULL; u = u->Next()) {
    02656         vehicle_width += u->GetDisplayImageWidth();
    02657       }
    02658       break;
    02659
    02660     case VEH_ROAD:
    02661       for (const RoadVehicle *u = RoadVehicle::From(v); u != NULL; u = u->Next()) {
    02662         vehicle_width += u->GetDisplayImageWidth();
    02663       }
    02664       break;
    02665
    02666     default:
    02667       bool rtl = _current_text_dir == TD_RTL;
    02668       SpriteID sprite = v->GetImage(rtl ? DIR_E : DIR_W);
    02669       const Sprite *real_sprite = GetSprite(sprite, ST_NORMAL);
    02670       vehicle_width = real_sprite->width;
    02671
    02672       break;
    02673   }
    02674
    02675   return vehicle_width;
    02676 }