# build_vehicle_gui.cpp

Go to the documentation of this file.

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