# build_vehicle_gui.cpp

Go to the documentation of this file.

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