# build_vehicle_gui.cpp

Go to the documentation of this file.

    00001 /* $Id: build_vehicle_gui.cpp 21180 2010-11-14 09:27:20Z 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 "engine_base.h"
    00014 #include "engine_func.h"
    00015 #include "station_base.h"
    00016 #include "articulated_vehicles.h"
    00017 #include "textbuf_gui.h"
    00018 #include "command_func.h"
    00019 #include "company_func.h"
    00020 #include "vehicle_gui.h"
    00021 #include "newgrf_engine.h"
    00022 #include "newgrf_text.h"
    00023 #include "group.h"
    00024 #include "strings_func.h"
    00025 #include "window_func.h"
    00026 #include "date_func.h"
    00027 #include "vehicle_func.h"
    00028 #include "widgets/dropdown_func.h"
    00029 #include "engine_gui.h"
    00030 #include "cargotype.h"
    00031 #include "core/geometry_func.hpp"
    00032
    00033 #include "table/sprites.h"
    00034 #include "table/strings.h"
    00035
    00041 uint GetEngineListHeight(VehicleType type)
    00042 {
    00043   return max<uint>(FONT_HEIGHT_NORMAL + WD_MATRIX_TOP + WD_MATRIX_BOTTOM, GetVehicleHeight(type));
    00044 }
    00045
    00046 enum BuildVehicleWidgets {
    00047   BUILD_VEHICLE_WIDGET_CAPTION,
    00048   BUILD_VEHICLE_WIDGET_SORT_ASSENDING_DESCENDING,
    00049   BUILD_VEHICLE_WIDGET_SORT_DROPDOWN,
    00050   BUILD_VEHICLE_WIDGET_CARGO_FILTER_DROPDOWN,
    00051   BUILD_VEHICLE_WIDGET_LIST,
    00052   BUILD_VEHICLE_WIDGET_SCROLLBAR,
    00053   BUILD_VEHICLE_WIDGET_PANEL,
    00054   BUILD_VEHICLE_WIDGET_BUILD,
    00055   BUILD_VEHICLE_WIDGET_BUILD_SEL,
    00056   BUILD_VEHICLE_WIDGET_RENAME,
    00057   BUILD_VEHICLE_WIDGET_END
    00058 };
    00059
    00060 static const NWidgetPart _nested_build_vehicle_widgets[] = {
    00061   NWidget(NWID_HORIZONTAL),
    00062     NWidget(WWT_CLOSEBOX, COLOUR_GREY),
    00063     NWidget(WWT_CAPTION, COLOUR_GREY, BUILD_VEHICLE_WIDGET_CAPTION), SetDataTip(STR_WHITE_STRING, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    00064     NWidget(WWT_SHADEBOX, COLOUR_GREY),
    00065     NWidget(WWT_STICKYBOX, COLOUR_GREY),
    00066   EndContainer(),
    00067   NWidget(WWT_PANEL, COLOUR_GREY),
    00068     NWidget(NWID_HORIZONTAL),
    00069       NWidget(NWID_VERTICAL),
    00070         NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, BUILD_VEHICLE_WIDGET_SORT_ASSENDING_DESCENDING), SetDataTip(STR_BUTTON_SORT_BY, STR_TOOLTIP_SORT_ORDER), SetFill(1, 0),
    00071         NWidget(NWID_SPACER), SetFill(1, 1),
    00072       EndContainer(),
    00073       NWidget(NWID_VERTICAL),
    00074         NWidget(WWT_DROPDOWN, COLOUR_GREY, BUILD_VEHICLE_WIDGET_SORT_DROPDOWN), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_JUST_STRING, STR_TOOLTIP_SORT_CRITERIA),
    00075         NWidget(WWT_DROPDOWN, COLOUR_GREY, BUILD_VEHICLE_WIDGET_CARGO_FILTER_DROPDOWN), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_JUST_STRING, STR_TOOLTIP_FILTER_CRITERIA),
    00076       EndContainer(),
    00077     EndContainer(),
    00078   EndContainer(),
    00079   /* Vehicle list. */
    00080   NWidget(NWID_HORIZONTAL),
    00081     NWidget(WWT_MATRIX, COLOUR_GREY, BUILD_VEHICLE_WIDGET_LIST), SetResize(1, 1), SetFill(1, 0), SetDataTip(0x101, STR_NULL),
    00082     NWidget(WWT_SCROLLBAR, COLOUR_GREY, BUILD_VEHICLE_WIDGET_SCROLLBAR),
    00083   EndContainer(),
    00084   /* Panel with details. */
    00085   NWidget(WWT_PANEL, COLOUR_GREY, BUILD_VEHICLE_WIDGET_PANEL), SetMinimalSize(240, 122), SetResize(1, 0), EndContainer(),
    00086   /* Build/rename buttons, resize button. */
    00087   NWidget(NWID_HORIZONTAL),
    00088     NWidget(NWID_SELECTION, INVALID_COLOUR, BUILD_VEHICLE_WIDGET_BUILD_SEL),
    00089       NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, BUILD_VEHICLE_WIDGET_BUILD), SetResize(1, 0), SetFill(1, 0),
    00090     EndContainer(),
    00091     NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, BUILD_VEHICLE_WIDGET_RENAME), SetResize(1, 0), SetFill(1, 0),
    00092     NWidget(WWT_RESIZEBOX, COLOUR_GREY),
    00093   EndContainer(),
    00094 };
    00095
    00097 enum {
    00098   CF_ANY  = CT_NO_REFIT,
    00099   CF_NONE = CT_INVALID,
    00100 };
    00101
    00102 static bool _internal_sort_order; // descending/ascending
    00103 static byte _last_sort_criteria[]    = {0, 0, 0, 0};
    00104 static bool _last_sort_order[]       = {false, false, false, false};
    00105 static byte _last_filter_criteria[]  = {0, 0, 0, 0};
    00106
    00107 static int CDECL EngineNumberSorter(const EngineID *a, const EngineID *b)
    00108 {
    00109   int r = ListPositionOfEngine(*a) - ListPositionOfEngine(*b);
    00110
    00111   return _internal_sort_order ? -r : r;
    00112 }
    00113
    00114 static int CDECL EngineIntroDateSorter(const EngineID *a, const EngineID *b)
    00115 {
    00116   const int va = Engine::Get(*a)->intro_date;
    00117   const int vb = Engine::Get(*b)->intro_date;
    00118   const int r = va - vb;
    00119
    00120   /* Use EngineID to sort instead since we want consistent sorting */
    00121   if (r == 0) return EngineNumberSorter(a, b);
    00122   return _internal_sort_order ? -r : r;
    00123 }
    00124
    00125 static int CDECL EngineNameSorter(const EngineID *a, const EngineID *b)
    00126 {
    00127   static EngineID last_engine[2] = { INVALID_ENGINE, INVALID_ENGINE };
    00128   static char     last_name[2][64] = { "\0", "\0" };
    00129
    00130   const EngineID va = *a;
    00131   const EngineID vb = *b;
    00132
    00133   if (va != last_engine[0]) {
    00134     last_engine[0] = va;
    00135     SetDParam(0, va);
    00136     GetString(last_name[0], STR_ENGINE_NAME, lastof(last_name[0]));
    00137   }
    00138
    00139   if (vb != last_engine[1]) {
    00140     last_engine[1] = vb;
    00141     SetDParam(0, vb);
    00142     GetString(last_name[1], STR_ENGINE_NAME, lastof(last_name[1]));
    00143   }
    00144
    00145   int r = strcmp(last_name[0], last_name[1]); // sort by name
    00146
    00147   /* Use EngineID to sort instead since we want consistent sorting */
    00148   if (r == 0) return EngineNumberSorter(a, b);
    00149   return _internal_sort_order ? -r : r;
    00150 }
    00151
    00152 static int CDECL EngineReliabilitySorter(const EngineID *a, const EngineID *b)
    00153 {
    00154   const int va = Engine::Get(*a)->reliability;
    00155   const int vb = Engine::Get(*b)->reliability;
    00156   const int r = va - vb;
    00157
    00158   /* Use EngineID to sort instead since we want consistent sorting */
    00159   if (r == 0) return EngineNumberSorter(a, b);
    00160   return _internal_sort_order ? -r : r;
    00161 }
    00162
    00163 static int CDECL EngineCostSorter(const EngineID *a, const EngineID *b)
    00164 {
    00165   Money va = Engine::Get(*a)->GetCost();
    00166   Money vb = Engine::Get(*b)->GetCost();
    00167   int r = ClampToI32(va - vb);
    00168
    00169   /* Use EngineID to sort instead since we want consistent sorting */
    00170   if (r == 0) return EngineNumberSorter(a, b);
    00171   return _internal_sort_order ? -r : r;
    00172 }
    00173
    00174 static int CDECL EngineSpeedSorter(const EngineID *a, const EngineID *b)
    00175 {
    00176   int va = Engine::Get(*a)->GetDisplayMaxSpeed();
    00177   int vb = Engine::Get(*b)->GetDisplayMaxSpeed();
    00178   int r = va - vb;
    00179
    00180   /* Use EngineID to sort instead since we want consistent sorting */
    00181   if (r == 0) return EngineNumberSorter(a, b);
    00182   return _internal_sort_order ? -r : r;
    00183 }
    00184
    00185 static int CDECL EnginePowerSorter(const EngineID *a, const EngineID *b)
    00186 {
    00187   int va = Engine::Get(*a)->GetPower();
    00188   int vb = Engine::Get(*b)->GetPower();
    00189   int r = va - vb;
    00190
    00191   /* Use EngineID to sort instead since we want consistent sorting */
    00192   if (r == 0) return EngineNumberSorter(a, b);
    00193   return _internal_sort_order ? -r : r;
    00194 }
    00195
    00196 static int CDECL EngineRunningCostSorter(const EngineID *a, const EngineID *b)
    00197 {
    00198   Money va = Engine::Get(*a)->GetRunningCost();
    00199   Money vb = Engine::Get(*b)->GetRunningCost();
    00200   int r = ClampToI32(va - vb);
    00201
    00202   /* Use EngineID to sort instead since we want consistent sorting */
    00203   if (r == 0) return EngineNumberSorter(a, b);
    00204   return _internal_sort_order ? -r : r;
    00205 }
    00206
    00207 /* Train sorting functions */
    00208 static int CDECL TrainEnginePowerVsRunningCostSorter(const EngineID *a, const EngineID *b)
    00209 {
    00210   const Engine *e_a = Engine::Get(*a);
    00211   const Engine *e_b = Engine::Get(*b);
    00212
    00213   /* Here we are using a few tricks to get the right sort.
    00214    * We want power/running cost, but since we usually got higher running cost than power and we store the result in an int,
    00215    * we will actually calculate cunning cost/power (to make it more than 1).
    00216    * Because of this, the return value have to be reversed as well and we return b - a instead of a - b.
    00217    * Another thing is that both power and running costs should be doubled for multiheaded engines.
    00218    * Since it would be multipling with 2 in both numerator and denumerator, it will even themselves out and we skip checking for multiheaded. */
    00219   Money va = (e_a->GetRunningCost()) / max(1U, (uint)e_a->GetPower());
    00220   Money vb = (e_b->GetRunningCost()) / max(1U, (uint)e_b->GetPower());
    00221   int r = ClampToI32(vb - va);
    00222
    00223   /* Use EngineID to sort instead since we want consistent sorting */
    00224   if (r == 0) return EngineNumberSorter(a, b);
    00225   return _internal_sort_order ? -r : r;
    00226 }
    00227
    00228 static int CDECL TrainEngineCapacitySorter(const EngineID *a, const EngineID *b)
    00229 {
    00230   const RailVehicleInfo *rvi_a = RailVehInfo(*a);
    00231   const RailVehicleInfo *rvi_b = RailVehInfo(*b);
    00232
    00233   int va = GetTotalCapacityOfArticulatedParts(*a) * (rvi_a->railveh_type == RAILVEH_MULTIHEAD ? 2 : 1);
    00234   int vb = GetTotalCapacityOfArticulatedParts(*b) * (rvi_b->railveh_type == RAILVEH_MULTIHEAD ? 2 : 1);
    00235   int r = va - vb;
    00236
    00237   /* Use EngineID to sort instead since we want consistent sorting */
    00238   if (r == 0) return EngineNumberSorter(a, b);
    00239   return _internal_sort_order ? -r : r;
    00240 }
    00241
    00242 static int CDECL TrainEnginesThenWagonsSorter(const EngineID *a, const EngineID *b)
    00243 {
    00244   int val_a = (RailVehInfo(*a)->railveh_type == RAILVEH_WAGON ? 1 : 0);
    00245   int val_b = (RailVehInfo(*b)->railveh_type == RAILVEH_WAGON ? 1 : 0);
    00246   int r = val_a - val_b;
    00247
    00248   /* Use EngineID to sort instead since we want consistent sorting */
    00249   if (r == 0) return EngineNumberSorter(a, b);
    00250   return _internal_sort_order ? -r : r;
    00251 }
    00252
    00253 /* Road vehicle sorting functions */
    00254 static int CDECL RoadVehEngineCapacitySorter(const EngineID *a, const EngineID *b)
    00255 {
    00256   int va = GetTotalCapacityOfArticulatedParts(*a);
    00257   int vb = GetTotalCapacityOfArticulatedParts(*b);
    00258   int r = va - vb;
    00259
    00260   /* Use EngineID to sort instead since we want consistent sorting */
    00261   if (r == 0) return EngineNumberSorter(a, b);
    00262   return _internal_sort_order ? -r : r;
    00263 }
    00264
    00265 /* Ship vehicle sorting functions */
    00266 static int CDECL ShipEngineCapacitySorter(const EngineID *a, const EngineID *b)
    00267 {
    00268   const Engine *e_a = Engine::Get(*a);
    00269   const Engine *e_b = Engine::Get(*b);
    00270
    00271   int va = e_a->GetDisplayDefaultCapacity();
    00272   int vb = e_b->GetDisplayDefaultCapacity();
    00273   int r = va - vb;
    00274
    00275   /* Use EngineID to sort instead since we want consistent sorting */
    00276   if (r == 0) return EngineNumberSorter(a, b);
    00277   return _internal_sort_order ? -r : r;
    00278 }
    00279
    00280 /* Aircraft sorting functions */
    00281 static int CDECL AircraftEngineCargoSorter(const EngineID *a, const EngineID *b)
    00282 {
    00283   const Engine *e_a = Engine::Get(*a);
    00284   const Engine *e_b = Engine::Get(*b);
    00285
    00286   uint16 mail_a, mail_b;
    00287   int va = e_a->GetDisplayDefaultCapacity(&mail_a);
    00288   int vb = e_b->GetDisplayDefaultCapacity(&mail_b);
    00289   int r = va - vb;
    00290
    00291   if (r == 0) {
    00292     /* The planes have the same passenger capacity. Check mail capacity instead */
    00293     r = mail_a - mail_b;
    00294
    00295     if (r == 0) {
    00296       /* Use EngineID to sort instead since we want consistent sorting */
    00297       return EngineNumberSorter(a, b);
    00298     }
    00299   }
    00300   return _internal_sort_order ? -r : r;
    00301 }
    00302
    00303 static EngList_SortTypeFunction * const _sorter[][10] = {{
    00304   /* Trains */
    00305   &EngineNumberSorter,
    00306   &EngineCostSorter,
    00307   &EngineSpeedSorter,
    00308   &EnginePowerSorter,
    00309   &EngineIntroDateSorter,
    00310   &EngineNameSorter,
    00311   &EngineRunningCostSorter,
    00312   &TrainEnginePowerVsRunningCostSorter,
    00313   &EngineReliabilitySorter,
    00314   &TrainEngineCapacitySorter,
    00315 }, {
    00316   /* Road vehicles */
    00317   &EngineNumberSorter,
    00318   &EngineCostSorter,
    00319   &EngineSpeedSorter,
    00320   &EngineIntroDateSorter,
    00321   &EngineNameSorter,
    00322   &EngineRunningCostSorter,
    00323   &EngineReliabilitySorter,
    00324   &RoadVehEngineCapacitySorter,
    00325 }, {
    00326   /* Ships */
    00327   &EngineNumberSorter,
    00328   &EngineCostSorter,
    00329   &EngineSpeedSorter,
    00330   &EngineIntroDateSorter,
    00331   &EngineNameSorter,
    00332   &EngineRunningCostSorter,
    00333   &EngineReliabilitySorter,
    00334   &ShipEngineCapacitySorter,
    00335 }, {
    00336   /* Aircraft */
    00337   &EngineNumberSorter,
    00338   &EngineCostSorter,
    00339   &EngineSpeedSorter,
    00340   &EngineIntroDateSorter,
    00341   &EngineNameSorter,
    00342   &EngineRunningCostSorter,
    00343   &EngineReliabilitySorter,
    00344   &AircraftEngineCargoSorter,
    00345 }};
    00346
    00347 static const StringID _sort_listing[][11] = {{
    00348   /* Trains */
    00349   STR_SORT_BY_ENGINE_ID,
    00350   STR_SORT_BY_COST,
    00351   STR_SORT_BY_MAX_SPEED,
    00352   STR_SORT_BY_POWER,
    00353   STR_SORT_BY_INTRO_DATE,
    00354   STR_SORT_BY_NAME,
    00355   STR_SORT_BY_RUNNING_COST,
    00356   STR_SORT_BY_POWER_VS_RUNNING_COST,
    00357   STR_SORT_BY_RELIABILITY,
    00358   STR_SORT_BY_CARGO_CAPACITY,
    00359   INVALID_STRING_ID
    00360 }, {
    00361   /* Road vehicles */
    00362   STR_SORT_BY_ENGINE_ID,
    00363   STR_SORT_BY_COST,
    00364   STR_SORT_BY_MAX_SPEED,
    00365   STR_SORT_BY_INTRO_DATE,
    00366   STR_SORT_BY_NAME,
    00367   STR_SORT_BY_RUNNING_COST,
    00368   STR_SORT_BY_RELIABILITY,
    00369   STR_SORT_BY_CARGO_CAPACITY,
    00370   INVALID_STRING_ID
    00371 }, {
    00372   /* Ships */
    00373   STR_SORT_BY_ENGINE_ID,
    00374   STR_SORT_BY_COST,
    00375   STR_SORT_BY_MAX_SPEED,
    00376   STR_SORT_BY_INTRO_DATE,
    00377   STR_SORT_BY_NAME,
    00378   STR_SORT_BY_RUNNING_COST,
    00379   STR_SORT_BY_RELIABILITY,
    00380   STR_SORT_BY_CARGO_CAPACITY,
    00381   INVALID_STRING_ID
    00382 }, {
    00383   /* Aircraft */
    00384   STR_SORT_BY_ENGINE_ID,
    00385   STR_SORT_BY_COST,
    00386   STR_SORT_BY_MAX_SPEED,
    00387   STR_SORT_BY_INTRO_DATE,
    00388   STR_SORT_BY_NAME,
    00389   STR_SORT_BY_RUNNING_COST,
    00390   STR_SORT_BY_RELIABILITY,
    00391   STR_SORT_BY_CARGO_CAPACITY,
    00392   INVALID_STRING_ID
    00393 }};
    00394
    00396 static bool CDECL CargoFilter(const EngineID *eid, const CargoID cid)
    00397 {
    00398   if (cid == CF_ANY) return true;
    00399   uint32 refit_mask = GetUnionOfArticulatedRefitMasks(*eid, true);
    00400   return (cid == CF_NONE ? refit_mask == 0 : HasBit(refit_mask, cid));
    00401 }
    00402
    00403 static GUIEngineList::FilterFunction * const _filter_funcs[] = {
    00404   &CargoFilter,
    00405 };
    00406
    00407 static int DrawCargoCapacityInfo(int left, int right, int y, EngineID engine, bool refittable)
    00408 {
    00409   CargoArray cap = GetCapacityOfArticulatedParts(engine);
    00410
    00411   for (CargoID c = 0; c < NUM_CARGO; c++) {
    00412     if (cap[c] == 0) continue;
    00413
    00414     SetDParam(0, c);
    00415     SetDParam(1, cap[c]);
    00416     SetDParam(2, refittable ? STR_PURCHASE_INFO_REFITTABLE : STR_EMPTY);
    00417     DrawString(left, right, y, STR_PURCHASE_INFO_CAPACITY);
    00418     y += FONT_HEIGHT_NORMAL;
    00419
    00420     /* Only show as refittable once */
    00421     refittable = false;
    00422   }
    00423
    00424   return y;
    00425 }
    00426
    00427 /* Draw rail wagon specific details */
    00428 static int DrawRailWagonPurchaseInfo(int left, int right, int y, EngineID engine_number, const RailVehicleInfo *rvi)
    00429 {
    00430   const Engine *e = Engine::Get(engine_number);
    00431
    00432   /* Purchase cost */
    00433   SetDParam(0, e->GetCost());
    00434   DrawString(left, right, y, STR_PURCHASE_INFO_COST);
    00435   y += FONT_HEIGHT_NORMAL;
    00436
    00437   /* Wagon weight - (including cargo) */
    00438   uint weight = e->GetDisplayWeight();
    00439   SetDParam(0, weight);
    00440   uint cargo_weight = (e->CanCarryCargo() ? CargoSpec::Get(e->GetDefaultCargoType())->weight * e->GetDisplayDefaultCapacity() >> 4 : 0);
    00441   SetDParam(1, cargo_weight + weight);
    00442   DrawString(left, right, y, STR_PURCHASE_INFO_WEIGHT_CWEIGHT);
    00443   y += FONT_HEIGHT_NORMAL;
    00444
    00445   /* Wagon speed limit, displayed if above zero */
    00446   if (_settings_game.vehicle.wagon_speed_limits) {
    00447     uint max_speed = e->GetDisplayMaxSpeed();
    00448     if (max_speed > 0) {
    00449       SetDParam(0, max_speed);
    00450       DrawString(left, right, y, STR_PURCHASE_INFO_SPEED);
    00451       y += FONT_HEIGHT_NORMAL;
    00452     }
    00453   }
    00454
    00455   /* Running cost */
    00456   if (rvi->running_cost_class != INVALID_PRICE) {
    00457     SetDParam(0, e->GetRunningCost());
    00458     DrawString(left, right, y, STR_PURCHASE_INFO_RUNNINGCOST);
    00459     y += FONT_HEIGHT_NORMAL;
    00460   }
    00461
    00462   return y;
    00463 }
    00464
    00465 /* Draw locomotive specific details */
    00466 static int DrawRailEnginePurchaseInfo(int left, int right, int y, EngineID engine_number, const RailVehicleInfo *rvi)
    00467 {
    00468   const Engine *e = Engine::Get(engine_number);
    00469
    00470   /* Purchase Cost - Engine weight */
    00471   SetDParam(0, e->GetCost());
    00472   SetDParam(1, e->GetDisplayWeight());
    00473   DrawString(left, right, y, STR_PURCHASE_INFO_COST_WEIGHT);
    00474   y += FONT_HEIGHT_NORMAL;
    00475
    00476   /* Max speed - Engine power */
    00477   SetDParam(0, e->GetDisplayMaxSpeed());
    00478   SetDParam(1, e->GetPower());
    00479   DrawString(left, right, y, STR_PURCHASE_INFO_SPEED_POWER);
    00480   y += FONT_HEIGHT_NORMAL;
    00481
    00482   /* Max tractive effort - not applicable if old acceleration or maglev */
    00483   if (_settings_game.vehicle.train_acceleration_model != AM_ORIGINAL && GetRailTypeInfo(rvi->railtype)->acceleration_type != 2) {
    00484     SetDParam(0, e->GetDisplayMaxTractiveEffort());
    00485     DrawString(left, right, y, STR_PURCHASE_INFO_MAX_TE);
    00486     y += FONT_HEIGHT_NORMAL;
    00487   }
    00488
    00489   /* Running cost */
    00490   if (rvi->running_cost_class != INVALID_PRICE) {
    00491     SetDParam(0, e->GetRunningCost());
    00492     DrawString(left, right, y, STR_PURCHASE_INFO_RUNNINGCOST);
    00493     y += FONT_HEIGHT_NORMAL;
    00494   }
    00495
    00496   /* Powered wagons power - Powered wagons extra weight */
    00497   if (rvi->pow_wag_power != 0) {
    00498     SetDParam(0, rvi->pow_wag_power);
    00499     SetDParam(1, rvi->pow_wag_weight);
    00500     DrawString(left, right, y, STR_PURCHASE_INFO_PWAGPOWER_PWAGWEIGHT);
    00501     y += FONT_HEIGHT_NORMAL;
    00502   };
    00503
    00504   return y;
    00505 }
    00506
    00507 /* Draw road vehicle specific details */
    00508 static int DrawRoadVehPurchaseInfo(int left, int right, int y, EngineID engine_number)
    00509 {
    00510   const Engine *e = Engine::Get(engine_number);
    00511
    00512   /* Purchase cost - Max speed */
    00513   SetDParam(0, e->GetCost());
    00514   SetDParam(1, e->GetDisplayMaxSpeed());
    00515   DrawString(left, right, y, STR_PURCHASE_INFO_COST_SPEED);
    00516   y += FONT_HEIGHT_NORMAL;
    00517
    00518   /* Running cost */
    00519   SetDParam(0, e->GetRunningCost());
    00520   DrawString(left, right, y, STR_PURCHASE_INFO_RUNNINGCOST);
    00521   y += FONT_HEIGHT_NORMAL;
    00522
    00523   return y;
    00524 }
    00525
    00526 /* Draw ship specific details */
    00527 static int DrawShipPurchaseInfo(int left, int right, int y, EngineID engine_number, bool refittable)
    00528 {
    00529   const Engine *e = Engine::Get(engine_number);
    00530
    00531   /* Purchase cost - Max speed */
    00532   SetDParam(0, e->GetCost());
    00533   SetDParam(1, e->GetDisplayMaxSpeed());
    00534   DrawString(left, right, y, STR_PURCHASE_INFO_COST_SPEED);
    00535   y += FONT_HEIGHT_NORMAL;
    00536
    00537   /* Cargo type + capacity */
    00538   SetDParam(0, e->GetDefaultCargoType());
    00539   SetDParam(1, e->GetDisplayDefaultCapacity());
    00540   SetDParam(2, refittable ? STR_PURCHASE_INFO_REFITTABLE : STR_EMPTY);
    00541   DrawString(left, right, y, STR_PURCHASE_INFO_CAPACITY);
    00542   y += FONT_HEIGHT_NORMAL;
    00543
    00544   /* Running cost */
    00545   SetDParam(0, e->GetRunningCost());
    00546   DrawString(left, right, y, STR_PURCHASE_INFO_RUNNINGCOST);
    00547   y += FONT_HEIGHT_NORMAL;
    00548
    00549   return y;
    00550 }
    00551
    00552 /* Draw aircraft specific details */
    00553 static int DrawAircraftPurchaseInfo(int left, int right, int y, EngineID engine_number, bool refittable)
    00554 {
    00555   const Engine *e = Engine::Get(engine_number);
    00556   CargoID cargo = e->GetDefaultCargoType();
    00557
    00558   /* Purchase cost - Max speed */
    00559   SetDParam(0, e->GetCost());
    00560   SetDParam(1, e->GetDisplayMaxSpeed());
    00561   DrawString(left, right, y, STR_PURCHASE_INFO_COST_SPEED);
    00562   y += FONT_HEIGHT_NORMAL;
    00563
    00564   /* Cargo capacity */
    00565   uint16 mail_capacity;
    00566   uint capacity = e->GetDisplayDefaultCapacity(&mail_capacity);
    00567   if (mail_capacity > 0) {
    00568     SetDParam(0, cargo);
    00569     SetDParam(1, capacity);
    00570     SetDParam(2, CT_MAIL);
    00571     SetDParam(3, mail_capacity);
    00572     DrawString(left, right, y, STR_PURCHASE_INFO_AIRCRAFT_CAPACITY);
    00573   } else {
    00574     /* Note, if the default capacity is selected by the refit capacity
    00575      * callback, then the capacity shown is likely to be incorrect. */
    00576     SetDParam(0, cargo);
    00577     SetDParam(1, capacity);
    00578     SetDParam(2, refittable ? STR_PURCHASE_INFO_REFITTABLE : STR_EMPTY);
    00579     DrawString(left, right, y, STR_PURCHASE_INFO_CAPACITY);
    00580   }
    00581   y += FONT_HEIGHT_NORMAL;
    00582
    00583   /* Running cost */
    00584   SetDParam(0, e->GetRunningCost());
    00585   DrawString(left, right, y, STR_PURCHASE_INFO_RUNNINGCOST);
    00586   y += FONT_HEIGHT_NORMAL;
    00587
    00588   return y;
    00589 }
    00590
    00599 static uint ShowAdditionalText(int left, int right, int y, EngineID engine)
    00600 {
    00601   uint16 callback = GetVehicleCallback(CBID_VEHICLE_ADDITIONAL_TEXT, 0, 0, engine, NULL);
    00602   if (callback == CALLBACK_FAILED) return y;
    00603
    00604   /* STR_BLACK_STRING is used to start the string with {BLACK} */
    00605   SetDParam(0, GetGRFStringID(GetEngineGRFID(engine), 0xD000 + callback));
    00606   PrepareTextRefStackUsage(0);
    00607   uint result = DrawStringMultiLine(left, right, y, INT32_MAX, STR_BLACK_STRING);
    00608   StopTextRefStackUsage();
    00609   return result;
    00610 }
    00611
    00618 int DrawVehiclePurchaseInfo(int left, int right, int y, EngineID engine_number)
    00619 {
    00620   const Engine *e = Engine::Get(engine_number);
    00621   YearMonthDay ymd;
    00622   ConvertDateToYMD(e->intro_date, &ymd);
    00623   bool refittable = IsArticulatedVehicleRefittable(engine_number);
    00624   bool articulated_cargo = false;
    00625
    00626   switch (e->type) {
    00627     default: NOT_REACHED();
    00628     case VEH_TRAIN:
    00629       if (e->u.rail.railveh_type == RAILVEH_WAGON) {
    00630         y = DrawRailWagonPurchaseInfo(left, right, y, engine_number, &e->u.rail);
    00631       } else {
    00632         y = DrawRailEnginePurchaseInfo(left, right, y, engine_number, &e->u.rail);
    00633       }
    00634       articulated_cargo = true;
    00635       break;
    00636
    00637     case VEH_ROAD:
    00638       y = DrawRoadVehPurchaseInfo(left, right, y, engine_number);
    00639       articulated_cargo = true;
    00640       break;
    00641
    00642     case VEH_SHIP:
    00643       y = DrawShipPurchaseInfo(left, right, y, engine_number, refittable);
    00644       break;
    00645
    00646     case VEH_AIRCRAFT:
    00647       y = DrawAircraftPurchaseInfo(left, right, y, engine_number, refittable);
    00648       break;
    00649   }
    00650
    00651   if (articulated_cargo) {
    00652     /* Cargo type + capacity, or N/A */
    00653     int new_y = DrawCargoCapacityInfo(left, right, y, engine_number, refittable);
    00654
    00655     if (new_y == y) {
    00656       SetDParam(0, CT_INVALID);
    00657       SetDParam(2, STR_EMPTY);
    00658       DrawString(left, right, y, STR_PURCHASE_INFO_CAPACITY);
    00659       y += FONT_HEIGHT_NORMAL;
    00660     } else {
    00661       y = new_y;
    00662     }
    00663   }
    00664
    00665   /* Draw details, that applies to all types except rail wagons */
    00666   if (e->type != VEH_TRAIN || e->u.rail.railveh_type != RAILVEH_WAGON) {
    00667     /* Design date - Life length */
    00668     SetDParam(0, ymd.year);
    00669     SetDParam(1, e->GetLifeLengthInDays() / DAYS_IN_LEAP_YEAR);
    00670     DrawString(left, right, y, STR_PURCHASE_INFO_DESIGNED_LIFE);
    00671     y += FONT_HEIGHT_NORMAL;
    00672
    00673     /* Reliability */
    00674     SetDParam(0, ToPercent16(e->reliability));
    00675     DrawString(left, right, y, STR_PURCHASE_INFO_RELIABILITY);
    00676     y += FONT_HEIGHT_NORMAL;
    00677   }
    00678
    00679   /* Additional text from NewGRF */
    00680   y = ShowAdditionalText(left, right, y, engine_number);
    00681   if (refittable) y = ShowRefitOptionsList(left, right, y, engine_number);
    00682
    00683   return y;
    00684 }
    00685
    00698 void DrawEngineList(VehicleType type, int l, int r, int y, const GUIEngineList *eng_list, uint16 min, uint16 max, EngineID selected_id, bool show_count, GroupID selected_group)
    00699 {
    00700   static const int sprite_widths[]  = { 60, 60, 76, 67 };
    00701   static const int sprite_y_offsets[] = { -1, -1, -2, -2 };
    00702
    00703   /* Obligatory sanity checks! */
    00704   assert((uint)type < lengthof(sprite_widths));
    00705   assert_compile(lengthof(sprite_y_offsets) == lengthof(sprite_widths));
    00706   assert(max <= eng_list->Length());
    00707
    00708   bool rtl = _dynlang.text_dir == TD_RTL;
    00709   int step_size = GetEngineListHeight(type);
    00710   int sprite_width = sprite_widths[type];
    00711
    00712   int sprite_x        = (rtl ? r - sprite_width / 2 : l + sprite_width / 2) - 1;
    00713   int sprite_y_offset = sprite_y_offsets[type] + step_size / 2;
    00714
    00715   int text_left  = l + (rtl ? WD_FRAMERECT_LEFT : sprite_width);
    00716   int text_right = r - (rtl ? sprite_width : WD_FRAMERECT_RIGHT);
    00717
    00718   int normal_text_y_offset = (step_size - FONT_HEIGHT_NORMAL) / 2;
    00719   int small_text_y_offset  = step_size - FONT_HEIGHT_SMALL - WD_FRAMERECT_BOTTOM - 1;
    00720
    00721   for (; min < max; min++, y += step_size) {
    00722     const EngineID engine = (*eng_list)[min];
    00723     /* Note: num_engines is only used in the autoreplace GUI, so it is correct to use _local_company here. */
    00724     const uint num_engines = GetGroupNumEngines(_local_company, selected_group, engine);
    00725
    00726     SetDParam(0, engine);
    00727     DrawString(text_left, text_right, y + normal_text_y_offset, STR_ENGINE_NAME, engine == selected_id ? TC_WHITE : TC_BLACK);
    00728     DrawVehicleEngine(l, r, sprite_x, y + sprite_y_offset, engine, (show_count && num_engines == 0) ? PALETTE_CRASH : GetEnginePalette(engine, _local_company));
    00729     if (show_count) {
    00730       SetDParam(0, num_engines);
    00731       DrawString(text_left, text_right, y + small_text_y_offset, STR_TINY_BLACK_COMA, TC_FROMSTRING, SA_RIGHT);
    00732     }
    00733   }
    00734 }
    00735
    00736
    00737 struct BuildVehicleWindow : Window {
    00738   VehicleType vehicle_type;
    00739   union {
    00740     RailTypeByte railtype;
    00741     AirportFTAClass::Flags flags;
    00742     RoadTypes roadtypes;
    00743   } filter;
    00744   bool descending_sort_order;
    00745   byte sort_criteria;
    00746   bool listview_mode;
    00747   EngineID sel_engine;
    00748   EngineID rename_engine;
    00749   GUIEngineList eng_list;
    00750   CargoID cargo_filter[NUM_CARGO + 2];
    00751   StringID cargo_filter_texts[NUM_CARGO + 3];
    00752   byte cargo_filter_criteria;
    00753   int details_height;
    00754
    00755   BuildVehicleWindow(const WindowDesc *desc, TileIndex tile, VehicleType type) : Window()
    00756   {
    00757     this->vehicle_type = type;
    00758     this->window_number = tile == INVALID_TILE ? (int)type : tile;
    00759
    00760     this->sel_engine      = INVALID_ENGINE;
    00761
    00762     this->sort_criteria         = _last_sort_criteria[type];
    00763     this->descending_sort_order = _last_sort_order[type];
    00764     this->cargo_filter_criteria = _last_filter_criteria[type];
    00765
    00766     /* Populate filter list */
    00767     uint filter_items = 0;
    00768
    00769     /* Add item for disabling filtering */
    00770     this->cargo_filter[filter_items] = CF_ANY;
    00771     this->cargo_filter_texts[filter_items] = STR_PURCHASE_INFO_ALL_TYPES;
    00772     filter_items++;
    00773
    00774     /* Add item for vehicles not carrying anything, e.g. train engines.
    00775      * This could also be useful for eyecandy vehicles of other types, but is likely too confusing for joe, */
    00776     if (type == VEH_TRAIN) {
    00777       this->cargo_filter[filter_items] = CF_NONE;
    00778       this->cargo_filter_texts[filter_items] = STR_LAND_AREA_INFORMATION_LOCAL_AUTHORITY_NONE;
    00779       filter_items++;
    00780     }
    00781
    00782     /* Collect available cargo types for filtering */
    00783     const CargoSpec *cargo;
    00784     FOR_ALL_CARGOSPECS(cargo) {
    00785       if (IsCargoInClass(cargo->Index(), CC_SPECIAL)) continue; // exclude fake cargo types
    00786       this->cargo_filter[filter_items] = cargo->Index();
    00787       this->cargo_filter_texts[filter_items] = cargo->name;
    00788       filter_items++;
    00789     }
    00790
    00791     this->cargo_filter_texts[filter_items] = INVALID_STRING_ID;
    00792     if (this->cargo_filter_criteria >= filter_items) this->cargo_filter_criteria = 0;
    00793
    00794     this->eng_list.SetFilterFuncs(_filter_funcs);
    00795     this->eng_list.SetFilterState(this->cargo_filter[this->cargo_filter_criteria] != CF_ANY);
    00796
    00797     switch (type) {
    00798       default: NOT_REACHED();
    00799       case VEH_TRAIN:
    00800         this->filter.railtype = (tile == INVALID_TILE) ? RAILTYPE_END : GetRailType(tile);
    00801         break;
    00802       case VEH_ROAD:
    00803         this->filter.roadtypes = (tile == INVALID_TILE) ? ROADTYPES_ALL : GetRoadTypes(tile);
    00804       case VEH_SHIP:
    00805         break;
    00806       case VEH_AIRCRAFT:
    00807         this->filter.flags =
    00808           tile == INVALID_TILE ? AirportFTAClass::ALL : Station::GetByTile(tile)->Airport()->flags;
    00809         break;
    00810     }
    00811
    00812     this->listview_mode = (this->window_number <= VEH_END);
    00813
    00814     this->CreateNestedTree(desc);
    00815
    00816     /* If we are just viewing the list of vehicles, we do not need the Build button.
    00817      * So we just hide it, and enlarge the Rename buton by the now vacant place. */
    00818     if (this->listview_mode) this->GetWidget<NWidgetStacked>(BUILD_VEHICLE_WIDGET_BUILD_SEL)->SetDisplayedPlane(SZSP_NONE);
    00819
    00820     NWidgetCore *widget = this->GetWidget<NWidgetCore>(BUILD_VEHICLE_WIDGET_LIST);
    00821     widget->tool_tip = STR_BUY_VEHICLE_TRAIN_LIST_TOOLTIP + type;
    00822
    00823     widget = this->GetWidget<NWidgetCore>(BUILD_VEHICLE_WIDGET_BUILD);
    00824     widget->widget_data = STR_BUY_VEHICLE_TRAIN_BUY_VEHICLE_BUTTON + type;
    00825     widget->tool_tip    = STR_BUY_VEHICLE_TRAIN_BUY_VEHICLE_TOOLTIP + type;
    00826
    00827     widget = this->GetWidget<NWidgetCore>(BUILD_VEHICLE_WIDGET_RENAME);
    00828     widget->widget_data = STR_BUY_VEHICLE_TRAIN_RENAME_BUTTON + type;
    00829     widget->tool_tip    = STR_BUY_VEHICLE_TRAIN_RENAME_TOOLTIP + type;
    00830
    00831     this->details_height = ((this->vehicle_type == VEH_TRAIN) ? 10 : 9) * FONT_HEIGHT_NORMAL + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
    00832
    00833     this->FinishInitNested(desc, tile == INVALID_TILE ? (int)type : tile);
    00834
    00835     this->owner = (tile != INVALID_TILE) ? GetTileOwner(tile) : _local_company;
    00836
    00837     this->eng_list.ForceRebuild();
    00838     this->GenerateBuildList(); // generate the list, since we need it in the next line
    00839     /* Select the first engine in the list as default when opening the window */
    00840     if (this->eng_list.Length() > 0) this->sel_engine = this->eng_list[0];
    00841   }
    00842
    00844   void FilterEngineList()
    00845   {
    00846     this->eng_list.Filter(this->cargo_filter[this->cargo_filter_criteria]);
    00847     if (0 == this->eng_list.Length()) { // no engine passed through the filter, invalidate the previously selected engine
    00848       this->sel_engine = INVALID_ENGINE;
    00849     } else if (!this->eng_list.Contains(this->sel_engine)) { // previously selected engine didn't pass the filter, select the first engine of the list
    00850       this->sel_engine = this->eng_list[0];
    00851     }
    00852   }
    00853
    00855   bool FilterSingleEngine(EngineID eid)
    00856   {
    00857     CargoID filter_type = this->cargo_filter[this->cargo_filter_criteria];
    00858     return (filter_type == CF_ANY || CargoFilter(&eid, filter_type));
    00859   }
    00860
    00861   /* Figure out what train EngineIDs to put in the list */
    00862   void GenerateBuildTrainList()
    00863   {
    00864     EngineID sel_id = INVALID_ENGINE;
    00865     int num_engines = 0;
    00866     int num_wagons  = 0;
    00867
    00868     this->filter.railtype = (this->listview_mode) ? RAILTYPE_END : GetRailType(this->window_number);
    00869
    00870     this->eng_list.Clear();
    00871
    00872     /* Make list of all available train engines and wagons.
    00873      * Also check to see if the previously selected engine is still available,
    00874      * and if not, reset selection to INVALID_ENGINE. This could be the case
    00875      * when engines become obsolete and are removed */
    00876     const Engine *e;
    00877     FOR_ALL_ENGINES_OF_TYPE(e, VEH_TRAIN) {
    00878       EngineID eid = e->index;
    00879       const RailVehicleInfo *rvi = &e->u.rail;
    00880
    00881       if (this->filter.railtype != RAILTYPE_END && !HasPowerOnRail(rvi->railtype, this->filter.railtype)) continue;
    00882       if (!IsEngineBuildable(eid, VEH_TRAIN, _local_company)) continue;
    00883
    00884       /* Filter now! So num_engines and num_wagons is valid */
    00885       if (!FilterSingleEngine(eid)) continue;
    00886
    00887       *this->eng_list.Append() = eid;
    00888
    00889       if (rvi->railveh_type != RAILVEH_WAGON) {
    00890         num_engines++;
    00891       } else {
    00892         num_wagons++;
    00893       }
    00894
    00895       if (eid == this->sel_engine) sel_id = eid;
    00896     }
    00897
    00898     this->sel_engine = sel_id;
    00899
    00900     /* make engines first, and then wagons, sorted by ListPositionOfEngine() */
    00901     _internal_sort_order = false;
    00902     EngList_Sort(&this->eng_list, TrainEnginesThenWagonsSorter);
    00903
    00904     /* and then sort engines */
    00905     _internal_sort_order = this->descending_sort_order;
    00906     EngList_SortPartial(&this->eng_list, _sorter[0][this->sort_criteria], 0, num_engines);
    00907
    00908     /* and finally sort wagons */
    00909     EngList_SortPartial(&this->eng_list, _sorter[0][this->sort_criteria], num_engines, num_wagons);
    00910   }
    00911
    00912   /* Figure out what road vehicle EngineIDs to put in the list */
    00913   void GenerateBuildRoadVehList()
    00914   {
    00915     EngineID sel_id = INVALID_ENGINE;
    00916
    00917     this->eng_list.Clear();
    00918
    00919     const Engine *e;
    00920     FOR_ALL_ENGINES_OF_TYPE(e, VEH_ROAD) {
    00921       EngineID eid = e->index;
    00922       if (!IsEngineBuildable(eid, VEH_ROAD, _local_company)) continue;
    00923       if (!HasBit(this->filter.roadtypes, HasBit(EngInfo(eid)->misc_flags, EF_ROAD_TRAM) ? ROADTYPE_TRAM : ROADTYPE_ROAD)) continue;
    00924       *this->eng_list.Append() = eid;
    00925
    00926       if (eid == this->sel_engine) sel_id = eid;
    00927     }
    00928     this->sel_engine = sel_id;
    00929   }
    00930
    00931   /* Figure out what ship EngineIDs to put in the list */
    00932   void GenerateBuildShipList()
    00933   {
    00934     EngineID sel_id = INVALID_ENGINE;
    00935     this->eng_list.Clear();
    00936
    00937     const Engine *e;
    00938     FOR_ALL_ENGINES_OF_TYPE(e, VEH_SHIP) {
    00939       EngineID eid = e->index;
    00940       if (!IsEngineBuildable(eid, VEH_SHIP, _local_company)) continue;
    00941       *this->eng_list.Append() = eid;
    00942
    00943       if (eid == this->sel_engine) sel_id = eid;
    00944     }
    00945     this->sel_engine = sel_id;
    00946   }
    00947
    00948   /* Figure out what aircraft EngineIDs to put in the list */
    00949   void GenerateBuildAircraftList()
    00950   {
    00951     EngineID sel_id = INVALID_ENGINE;
    00952
    00953     this->eng_list.Clear();
    00954
    00955     const Station *st = this->listview_mode ? NULL : Station::GetByTile(this->window_number);
    00956
    00957     /* Make list of all available planes.
    00958      * Also check to see if the previously selected plane is still available,
    00959      * and if not, reset selection to INVALID_ENGINE. This could be the case
    00960      * when planes become obsolete and are removed */
    00961     const Engine *e;
    00962     FOR_ALL_ENGINES_OF_TYPE(e, VEH_AIRCRAFT) {
    00963       EngineID eid = e->index;
    00964       if (!IsEngineBuildable(eid, VEH_AIRCRAFT, _local_company)) continue;
    00965       /* First VEH_END window_numbers are fake to allow a window open for all different types at once */
    00966       if (!this->listview_mode && !CanVehicleUseStation(eid, st)) continue;
    00967
    00968       *this->eng_list.Append() = eid;
    00969       if (eid == this->sel_engine) sel_id = eid;
    00970     }
    00971
    00972     this->sel_engine = sel_id;
    00973   }
    00974
    00975   /* Generate the list of vehicles */
    00976   void GenerateBuildList()
    00977   {
    00978     if (!this->eng_list.NeedRebuild()) return;
    00979     switch (this->vehicle_type) {
    00980       default: NOT_REACHED();
    00981       case VEH_TRAIN:
    00982         this->GenerateBuildTrainList();
    00983         this->eng_list.Compact();
    00984         this->eng_list.RebuildDone();
    00985         return; // trains should not reach the last sorting
    00986       case VEH_ROAD:
    00987         this->GenerateBuildRoadVehList();
    00988         break;
    00989       case VEH_SHIP:
    00990         this->GenerateBuildShipList();
    00991         break;
    00992       case VEH_AIRCRAFT:
    00993         this->GenerateBuildAircraftList();
    00994         break;
    00995     }
    00996
    00997     this->FilterEngineList();
    00998
    00999     _internal_sort_order = this->descending_sort_order;
    01000     EngList_Sort(&this->eng_list, _sorter[this->vehicle_type][this->sort_criteria]);
    01001
    01002     this->eng_list.Compact();
    01003     this->eng_list.RebuildDone();
    01004   }
    01005
    01006   void OnClick(Point pt, int widget, int click_count)
    01007   {
    01008     switch (widget) {
    01009       case BUILD_VEHICLE_WIDGET_SORT_ASSENDING_DESCENDING:
    01010         this->descending_sort_order ^= true;
    01011         _last_sort_order[this->vehicle_type] = this->descending_sort_order;
    01012         this->eng_list.ForceRebuild();
    01013         this->SetDirty();
    01014         break;
    01015
    01016       case BUILD_VEHICLE_WIDGET_LIST: {
    01017         uint i = (pt.y - this->GetWidget<NWidgetBase>(BUILD_VEHICLE_WIDGET_LIST)->pos_y) / this->resize.step_height + this->vscroll.GetPosition();
    01018         size_t num_items = this->eng_list.Length();
    01019         this->sel_engine = (i < num_items) ? this->eng_list[i] : INVALID_ENGINE;
    01020         this->SetDirty();
    01021         if (click_count > 1 && !this->listview_mode) this->OnClick(pt, BUILD_VEHICLE_WIDGET_BUILD, 1);
    01022         break;
    01023       }
    01024
    01025       case BUILD_VEHICLE_WIDGET_SORT_DROPDOWN: // Select sorting criteria dropdown menu
    01026         ShowDropDownMenu(this, _sort_listing[this->vehicle_type], this->sort_criteria, BUILD_VEHICLE_WIDGET_SORT_DROPDOWN, 0, 0);
    01027         break;
    01028
    01029       case BUILD_VEHICLE_WIDGET_CARGO_FILTER_DROPDOWN: // Select cargo filtering criteria dropdown menu
    01030         ShowDropDownMenu(this, this->cargo_filter_texts, this->cargo_filter_criteria, BUILD_VEHICLE_WIDGET_CARGO_FILTER_DROPDOWN, 0, 0);
    01031         break;
    01032
    01033       case BUILD_VEHICLE_WIDGET_BUILD: {
    01034         EngineID sel_eng = this->sel_engine;
    01035         if (sel_eng != INVALID_ENGINE) {
    01036           CommandCallback *callback = (this->vehicle_type == VEH_TRAIN && RailVehInfo(sel_eng)->railveh_type == RAILVEH_WAGON) ? CcBuildWagon : CcBuildPrimaryVehicle;
    01037           DoCommandP(this->window_number, sel_eng, 0, GetCmdBuildVeh(this->vehicle_type), callback);
    01038         }
    01039         break;
    01040       }
    01041
    01042       case BUILD_VEHICLE_WIDGET_RENAME: {
    01043         EngineID sel_eng = this->sel_engine;
    01044         if (sel_eng != INVALID_ENGINE) {
    01045           this->rename_engine = sel_eng;
    01046           SetDParam(0, sel_eng);
    01047           ShowQueryString(STR_ENGINE_NAME, STR_QUERY_RENAME_TRAIN_TYPE_CAPTION + this->vehicle_type, MAX_LENGTH_ENGINE_NAME_BYTES, MAX_LENGTH_ENGINE_NAME_PIXELS, this, CS_ALPHANUMERAL, QSF_ENABLE_DEFAULT);
    01048         }
    01049         break;
    01050       }
    01051     }
    01052   }
    01053
    01054   virtual void OnInvalidateData(int data)
    01055   {
    01056     this->eng_list.ForceRebuild();
    01057   }
    01058
    01059   virtual void SetStringParameters(int widget) const
    01060   {
    01061     switch (widget) {
    01062       case BUILD_VEHICLE_WIDGET_CAPTION:
    01063         if (this->vehicle_type == VEH_TRAIN && !this->listview_mode) {
    01064           const RailtypeInfo *rti = GetRailTypeInfo(this->filter.railtype);
    01065           SetDParam(0, rti->strings.build_caption);
    01066         } else {
    01067           SetDParam(0, (this->listview_mode ? STR_VEHICLE_LIST_AVAILABLE_TRAINS : STR_BUY_VEHICLE_TRAIN_ALL_CAPTION) + this->vehicle_type);
    01068         }
    01069         break;
    01070
    01071       case BUILD_VEHICLE_WIDGET_SORT_DROPDOWN:
    01072         SetDParam(0, _sort_listing[this->vehicle_type][this->sort_criteria]);
    01073         break;
    01074
    01075       case BUILD_VEHICLE_WIDGET_CARGO_FILTER_DROPDOWN:
    01076         SetDParam(0, this->cargo_filter_texts[this->cargo_filter_criteria]);
    01077     }
    01078   }
    01079
    01080   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
    01081   {
    01082     switch (widget) {
    01083       case BUILD_VEHICLE_WIDGET_LIST:
    01084         resize->height = GetEngineListHeight(this->vehicle_type);
    01085         size->height = 3 * resize->height;
    01086         break;
    01087
    01088       case BUILD_VEHICLE_WIDGET_PANEL:
    01089         size->height = this->details_height;
    01090         break;
    01091
    01092       case BUILD_VEHICLE_WIDGET_SORT_ASSENDING_DESCENDING: {
    01093         Dimension d = GetStringBoundingBox(this->GetWidget<NWidgetCore>(widget)->widget_data);
    01094         d.width += padding.width + WD_SORTBUTTON_ARROW_WIDTH * 2; // Doubled since the string is centred and it also looks better.
    01095         d.height += padding.height;
    01096         *size = maxdim(*size, d);
    01097         break;
    01098       }
    01099     }
    01100   }
    01101
    01102   virtual void DrawWidget(const Rect &r, int widget) const
    01103   {
    01104     switch (widget) {
    01105       case BUILD_VEHICLE_WIDGET_LIST:
    01106         DrawEngineList(this->vehicle_type, r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + WD_FRAMERECT_TOP, &this->eng_list, this->vscroll.GetPosition(), min(this->vscroll.GetPosition() + this->vscroll.GetCapacity(), this->eng_list.Length()), this->sel_engine, false, DEFAULT_GROUP);
    01107         break;
    01108
    01109       case BUILD_VEHICLE_WIDGET_SORT_ASSENDING_DESCENDING:
    01110         this->DrawSortButtonState(BUILD_VEHICLE_WIDGET_SORT_ASSENDING_DESCENDING, this->descending_sort_order ? SBS_DOWN : SBS_UP);
    01111         break;
    01112     }
    01113   }
    01114
    01115   virtual void OnPaint()
    01116   {
    01117     this->GenerateBuildList();
    01118     this->vscroll.SetCount(this->eng_list.Length());
    01119
    01120     this->DrawWidgets();
    01121
    01122     if (!this->IsShaded()) {
    01123       int needed_height = this->details_height;
    01124       /* Draw details panels. */
    01125       for (int side = 0; side < 2; side++) {
    01126         if (this->sel_engine != INVALID_ENGINE) {
    01127           NWidgetBase *nwi = this->GetWidget<NWidgetBase>(BUILD_VEHICLE_WIDGET_PANEL);
    01128           int text_end = DrawVehiclePurchaseInfo(nwi->pos_x + WD_FRAMETEXT_LEFT, nwi->pos_x + nwi->current_x - WD_FRAMETEXT_RIGHT,
    01129               nwi->pos_y + WD_FRAMERECT_TOP, this->sel_engine);
    01130           needed_height = max(needed_height, text_end - (int)nwi->pos_y + WD_FRAMERECT_BOTTOM);
    01131         }
    01132       }
    01133       if (needed_height != this->details_height) { // Details window are not high enough, enlarge them.
    01134         int resize = needed_height - this->details_height;
    01135         this->details_height = needed_height;
    01136         this->ReInit(0, resize);
    01137         return;
    01138       }
    01139     }
    01140   }
    01141
    01142   virtual void OnQueryTextFinished(char *str)
    01143   {
    01144     if (str == NULL) return;
    01145
    01146     DoCommandP(0, this->rename_engine, 0, CMD_RENAME_ENGINE | CMD_MSG(STR_ERROR_CAN_T_RENAME_TRAIN_TYPE + this->vehicle_type), NULL, str);
    01147   }
    01148
    01149   virtual void OnDropdownSelect(int widget, int index)
    01150   {
    01151     switch (widget) {
    01152       case BUILD_VEHICLE_WIDGET_SORT_DROPDOWN:
    01153         if (this->sort_criteria != index) {
    01154           this->sort_criteria = index;
    01155           _last_sort_criteria[this->vehicle_type] = this->sort_criteria;
    01156           this->eng_list.ForceRebuild();
    01157         }
    01158         break;
    01159
    01160       case BUILD_VEHICLE_WIDGET_CARGO_FILTER_DROPDOWN: // Select a cargo filter criteria
    01161         if (this->cargo_filter_criteria != index) {
    01162           this->cargo_filter_criteria = index;
    01163           _last_filter_criteria[this->vehicle_type] = this->cargo_filter_criteria;
    01164           /* deactivate filter if criteria is 'Show All', activate it otherwise */
    01165           this->eng_list.SetFilterState(this->cargo_filter[this->cargo_filter_criteria] != CF_ANY);
    01166           this->eng_list.ForceRebuild();
    01167         }
    01168         break;
    01169     }
    01170     this->SetDirty();
    01171   }
    01172
    01173   virtual void OnResize()
    01174   {
    01175     this->vscroll.SetCapacityFromWidget(this, BUILD_VEHICLE_WIDGET_LIST);
    01176     this->GetWidget<NWidgetCore>(BUILD_VEHICLE_WIDGET_LIST)->widget_data = (this->vscroll.GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
    01177   }
    01178 };
    01179
    01180 static const WindowDesc _build_vehicle_desc(
    01181   WDP_AUTO, 240, 268,
    01182   WC_BUILD_VEHICLE, WC_NONE,
    01183   WDF_UNCLICK_BUTTONS | WDF_CONSTRUCTION,
    01184   _nested_build_vehicle_widgets, lengthof(_nested_build_vehicle_widgets)
    01185 );
    01186
    01187 void ShowBuildVehicleWindow(TileIndex tile, VehicleType type)
    01188 {
    01189   /* We want to be able to open both Available Train as Available Ships,
    01190    *  so if tile == INVALID_TILE (Available XXX Window), use 'type' as unique number.
    01191    *  As it always is a low value, it won't collide with any real tile
    01192    *  number. */
    01193   uint num = (tile == INVALID_TILE) ? (int)type : tile;
    01194
    01195   assert(IsCompanyBuildableVehicleType(type));
    01196
    01197   DeleteWindowById(WC_BUILD_VEHICLE, num);
    01198
    01199   new BuildVehicleWindow(&_build_vehicle_desc, tile, type);
    01200 }