# build_vehicle_gui.cpp

Go to the documentation of this file.

    00001 /* $Id: build_vehicle_gui.cpp 15592 2009-02-27 20:40:39Z frosch $ */
    00002
    00005 #include "train.h"
    00006 #include "roadveh.h"
    00007 #include "ship.h"
    00008 #include "aircraft.h"
    00009 #include "articulated_vehicles.h"
    00010 #include "textbuf_gui.h"
    00011 #include "command_func.h"
    00012 #include "company_func.h"
    00013 #include "vehicle_gui.h"
    00014 #include "newgrf_engine.h"
    00015 #include "group.h"
    00016 #include "strings_func.h"
    00017 #include "window_func.h"
    00018 #include "date_func.h"
    00019 #include "vehicle_func.h"
    00020 #include "gfx_func.h"
    00021 #include "widgets/dropdown_func.h"
    00022 #include "window_gui.h"
    00023 #include "engine_gui.h"
    00024 #include "settings_type.h"
    00025
    00026 #include "table/sprites.h"
    00027 #include "table/strings.h"
    00028
    00029 enum BuildVehicleWidgets {
    00030   BUILD_VEHICLE_WIDGET_CLOSEBOX = 0,
    00031   BUILD_VEHICLE_WIDGET_CAPTION,
    00032   BUILD_VEHICLE_WIDGET_SORT_ASSENDING_DESCENDING,
    00033   BUILD_VEHICLE_WIDGET_SORT_DROPDOWN,
    00034   BUILD_VEHICLE_WIDGET_LIST,
    00035   BUILD_VEHICLE_WIDGET_SCROLLBAR,
    00036   BUILD_VEHICLE_WIDGET_PANEL,
    00037   BUILD_VEHICLE_WIDGET_BUILD,
    00038   BUILD_VEHICLE_WIDGET_RENAME,
    00039   BUILD_VEHICLE_WIDGET_RESIZE,
    00040   BUILD_VEHICLE_WIDGET_END
    00041 };
    00042
    00043 static const Widget _build_vehicle_widgets[] = {
    00044   {   WWT_CLOSEBOX,   RESIZE_NONE,  COLOUR_GREY,     0,    10,     0,    13, STR_00C5,                STR_018B_CLOSE_WINDOW },
    00045   {    WWT_CAPTION,  RESIZE_RIGHT,  COLOUR_GREY,    11,   239,     0,    13, 0x0,                     STR_018C_WINDOW_TITLE_DRAG_THIS },
    00046   { WWT_PUSHTXTBTN,   RESIZE_NONE,  COLOUR_GREY,     0,    80,    14,    25, STR_SORT_BY,             STR_SORT_ORDER_TIP},
    00047   {   WWT_DROPDOWN,  RESIZE_RIGHT,  COLOUR_GREY,    81,   239,    14,    25, 0x0,                     STR_SORT_CRITERIA_TIP},
    00048   {     WWT_MATRIX,     RESIZE_RB,  COLOUR_GREY,     0,   227,    26,    39, 0x101,                   STR_NULL },
    00049   {  WWT_SCROLLBAR,    RESIZE_LRB,  COLOUR_GREY,   228,   239,    26,    39, 0x0,                     STR_0190_SCROLL_BAR_SCROLLS_LIST },
    00050   {      WWT_PANEL,    RESIZE_RTB,  COLOUR_GREY,     0,   239,    40,   161, 0x0,                     STR_NULL },
    00051
    00052   { WWT_PUSHTXTBTN,     RESIZE_TB,  COLOUR_GREY,     0,   114,   162,   173, 0x0,                     STR_NULL },
    00053   { WWT_PUSHTXTBTN,    RESIZE_RTB,  COLOUR_GREY,   115,   227,   162,   173, 0x0,                     STR_NULL },
    00054   {  WWT_RESIZEBOX,   RESIZE_LRTB,  COLOUR_GREY,   228,   239,   162,   173, 0x0,                     STR_RESIZE_BUTTON },
    00055   {   WIDGETS_END},
    00056 };
    00057
    00058 static bool _internal_sort_order; // descending/ascending
    00059 static byte _last_sort_criteria[]    = {0, 0, 0, 0};
    00060 static bool _last_sort_order[]       = {false, false, false, false};
    00061
    00062 static int CDECL EngineNumberSorter(const void *a, const void *b)
    00063 {
    00064   const EngineID va = *(const EngineID*)a;
    00065   const EngineID vb = *(const EngineID*)b;
    00066   int r = ListPositionOfEngine(va) - ListPositionOfEngine(vb);
    00067
    00068   return _internal_sort_order ? -r : r;
    00069 }
    00070
    00071 static int CDECL EngineIntroDateSorter(const void *a, const void *b)
    00072 {
    00073   const int va = GetEngine(*(const EngineID*)a)->intro_date;
    00074   const int vb = GetEngine(*(const EngineID*)b)->intro_date;
    00075   const int r = va - vb;
    00076
    00077   if (r == 0) {
    00078     /* Use EngineID to sort instead since we want consistent sorting */
    00079     return EngineNumberSorter(a, b);
    00080   }
    00081   return _internal_sort_order ? -r : r;
    00082 }
    00083
    00084 static int CDECL EngineNameSorter(const void *a, const void *b)
    00085 {
    00086   static EngineID last_engine[2] = { INVALID_ENGINE, INVALID_ENGINE };
    00087   static char     last_name[2][64] = { "\0", "\0" };
    00088
    00089   const EngineID va = *(const EngineID*)a;
    00090   const EngineID vb = *(const EngineID*)b;
    00091   int r;
    00092
    00093   if (va != last_engine[0]) {
    00094     last_engine[0] = va;
    00095     SetDParam(0, va);
    00096     GetString(last_name[0], STR_ENGINE_NAME, lastof(last_name[0]));
    00097   }
    00098
    00099   if (vb != last_engine[1]) {
    00100     last_engine[1] = vb;
    00101     SetDParam(0, vb);
    00102     GetString(last_name[1], STR_ENGINE_NAME, lastof(last_name[1]));
    00103   }
    00104
    00105   r = strcmp(last_name[0], last_name[1]); // sort by name
    00106
    00107   if (r == 0) {
    00108     /* Use EngineID to sort instead since we want consistent sorting */
    00109     return EngineNumberSorter(a, b);
    00110   }
    00111   return _internal_sort_order ? -r : r;
    00112 }
    00113
    00114 static int CDECL EngineReliabilitySorter(const void *a, const void *b)
    00115 {
    00116   const int va = GetEngine(*(const EngineID*)a)->reliability;
    00117   const int vb = GetEngine(*(const EngineID*)b)->reliability;
    00118   const int r = va - vb;
    00119
    00120   if (r == 0) {
    00121     /* Use EngineID to sort instead since we want consistent sorting */
    00122     return EngineNumberSorter(a, b);
    00123   }
    00124   return _internal_sort_order ? -r : r;
    00125 }
    00126
    00127 /* Train sorting functions */
    00128 static int CDECL TrainEngineCostSorter(const void *a, const void *b)
    00129 {
    00130   int va = RailVehInfo(*(const EngineID*)a)->cost_factor;
    00131   int vb = RailVehInfo(*(const EngineID*)b)->cost_factor;
    00132   int r = va - vb;
    00133
    00134   return _internal_sort_order ? -r : r;
    00135 }
    00136
    00137 static int CDECL TrainEngineSpeedSorter(const void *a, const void *b)
    00138 {
    00139   int va = RailVehInfo(*(const EngineID*)a)->max_speed;
    00140   int vb = RailVehInfo(*(const EngineID*)b)->max_speed;
    00141   int r = va - vb;
    00142
    00143   return _internal_sort_order ? -r : r;
    00144 }
    00145
    00146 static int CDECL TrainEnginePowerSorter(const void *a, const void *b)
    00147 {
    00148   const RailVehicleInfo *rvi_a = RailVehInfo(*(const EngineID*)a);
    00149   const RailVehicleInfo *rvi_b = RailVehInfo(*(const EngineID*)b);
    00150
    00151   int va = rvi_a->power;
    00152   int vb = rvi_b->power;
    00153   int r = va - vb;
    00154
    00155   return _internal_sort_order ? -r : r;
    00156 }
    00157
    00158 static int CDECL TrainEngineRunningCostSorter(const void *a, const void *b)
    00159 {
    00160   Money va = GetEngine(*(const EngineID*)a)->GetRunningCost();
    00161   Money vb = GetEngine(*(const EngineID*)b)->GetRunningCost();
    00162   int r = ClampToI32(va - vb);
    00163
    00164   return _internal_sort_order ? -r : r;
    00165 }
    00166
    00167 static int CDECL TrainEnginePowerVsRunningCostSorter(const void *a, const void *b)
    00168 {
    00169   const RailVehicleInfo *rvi_a = RailVehInfo(*(const EngineID*)a);
    00170   const RailVehicleInfo *rvi_b = RailVehInfo(*(const EngineID*)b);
    00171
    00172   /* Here we are using a few tricks to get the right sort.
    00173     * We want power/running cost, but since we usually got higher running cost than power and we store the result in an int,
    00174     * we will actually calculate cunning cost/power (to make it more than 1).
    00175     * Because of this, the return value have to be reversed as well and we return b - a instead of a - b.
    00176     * Another thing is that both power and running costs should be doubled for multiheaded engines.
    00177     * Since it would be multipling with 2 in both numerator and denumerator, it will even themselves out and we skip checking for multiheaded. */
    00178   Money va = (GetEngine(*(const EngineID*)a)->GetRunningCost()) / max(1U, (uint)rvi_a->power);
    00179   Money vb = (GetEngine(*(const EngineID*)b)->GetRunningCost()) / max(1U, (uint)rvi_b->power);
    00180   int r = ClampToI32(vb - va);
    00181
    00182   return _internal_sort_order ? -r : r;
    00183 }
    00184
    00185 static int CDECL TrainEngineCapacitySorter(const void *a, const void *b)
    00186 {
    00187   const RailVehicleInfo *rvi_a = RailVehInfo(*(const EngineID*)a);
    00188   const RailVehicleInfo *rvi_b = RailVehInfo(*(const EngineID*)b);
    00189
    00190   int va = rvi_a->capacity * (rvi_a->railveh_type == RAILVEH_MULTIHEAD ? 2 : 1);
    00191   int vb = rvi_b->capacity * (rvi_b->railveh_type == RAILVEH_MULTIHEAD ? 2 : 1);
    00192   int r = va - vb;
    00193
    00194   if (r == 0) {
    00195     /* Use EngineID to sort instead since we want consistent sorting */
    00196     return EngineNumberSorter(a, b);
    00197   }
    00198   return _internal_sort_order ? -r : r;
    00199 }
    00200
    00201 static int CDECL TrainEnginesThenWagonsSorter(const void *a, const void *b)
    00202 {
    00203   EngineID va = *(const EngineID*)a;
    00204   EngineID vb = *(const EngineID*)b;
    00205   int val_a = (RailVehInfo(va)->railveh_type == RAILVEH_WAGON ? 1 : 0);
    00206   int val_b = (RailVehInfo(vb)->railveh_type == RAILVEH_WAGON ? 1 : 0);
    00207   int r = val_a - val_b;
    00208
    00209   /* Use EngineID to sort instead since we want consistent sorting */
    00210   if (r == 0) return EngineNumberSorter(a, b);
    00211
    00212   return _internal_sort_order ? -r : r;
    00213 }
    00214
    00215 /* Road vehicle sorting functions */
    00216 static int CDECL RoadVehEngineCostSorter(const void *a, const void *b)
    00217 {
    00218   int va = RoadVehInfo(*(const EngineID*)a)->cost_factor;
    00219   int vb = RoadVehInfo(*(const EngineID*)b)->cost_factor;
    00220   int r = va - vb;
    00221
    00222   return _internal_sort_order ? -r : r;
    00223 }
    00224
    00225 static int CDECL RoadVehEngineSpeedSorter(const void *a, const void *b)
    00226 {
    00227   int va = RoadVehInfo(*(const EngineID*)a)->max_speed;
    00228   int vb = RoadVehInfo(*(const EngineID*)b)->max_speed;
    00229   int r = va - vb;
    00230
    00231   return _internal_sort_order ? -r : r;
    00232 }
    00233
    00234 static int CDECL RoadVehEngineRunningCostSorter(const void *a, const void *b)
    00235 {
    00236   Money va = GetEngine(*(const EngineID*)a)->GetRunningCost();
    00237   Money vb = GetEngine(*(const EngineID*)b)->GetRunningCost();
    00238   int r = ClampToI32(va - vb);
    00239
    00240   if (r == 0) {
    00241     /* Use EngineID to sort instead since we want consistent sorting */
    00242     return EngineNumberSorter(a, b);
    00243   }
    00244   return _internal_sort_order ? -r : r;
    00245 }
    00246
    00247 static int CDECL RoadVehEngineCapacitySorter(const void *a, const void *b)
    00248 {
    00249   int va = RoadVehInfo(*(const EngineID*)a)->capacity;
    00250   int vb = RoadVehInfo(*(const EngineID*)b)->capacity;
    00251   int r = va - vb;
    00252
    00253   if (r == 0) {
    00254     /* Use EngineID to sort instead since we want consistent sorting */
    00255     return EngineNumberSorter(a, b);
    00256   }
    00257   return _internal_sort_order ? -r : r;
    00258 }
    00259
    00260 /* Road vehicle sorting functions */
    00261 static int CDECL ShipEngineCostSorter(const void *a, const void *b)
    00262 {
    00263   int va = ShipVehInfo(*(const EngineID*)a)->cost_factor;
    00264   int vb = ShipVehInfo(*(const EngineID*)b)->cost_factor;
    00265   int r = va - vb;
    00266
    00267   return _internal_sort_order ? -r : r;
    00268 }
    00269
    00270 static int CDECL ShipEngineSpeedSorter(const void *a, const void *b)
    00271 {
    00272   int va = ShipVehInfo(*(const EngineID*)a)->max_speed;
    00273   int vb = ShipVehInfo(*(const EngineID*)b)->max_speed;
    00274   int r = va - vb;
    00275
    00276   return _internal_sort_order ? -r : r;
    00277 }
    00278
    00279 static int CDECL ShipEngineRunningCostSorter(const void *a, const void *b)
    00280 {
    00281   const int va = GetEngine(*(const EngineID*)a)->GetRunningCost();
    00282   const int vb = GetEngine(*(const EngineID*)b)->GetRunningCost();
    00283   const int r = va - vb;
    00284
    00285   if (r == 0) {
    00286     /* Use EngineID to sort instead since we want consistent sorting */
    00287     return EngineNumberSorter(a, b);
    00288   }
    00289   return _internal_sort_order ? -r : r;
    00290 }
    00291
    00292 static int CDECL ShipEngineCapacitySorter(const void *a, const void *b)
    00293 {
    00294   int va = ShipVehInfo(*(const EngineID*)a)->capacity;
    00295   int vb = ShipVehInfo(*(const EngineID*)b)->capacity;
    00296   int r = va - vb;
    00297
    00298   if (r == 0) {
    00299     /* Use EngineID to sort instead since we want consistent sorting */
    00300     return EngineNumberSorter(a, b);
    00301   }
    00302   return _internal_sort_order ? -r : r;
    00303 }
    00304
    00305 /* Aircraft sorting functions */
    00306
    00307 static int CDECL AircraftEngineCostSorter(const void *a, const void *b)
    00308 {
    00309   const int va = AircraftVehInfo(*(const EngineID*)a)->cost_factor;
    00310   const int vb = AircraftVehInfo(*(const EngineID*)b)->cost_factor;
    00311   int r = va - vb;
    00312
    00313   return _internal_sort_order ? -r : r;
    00314 }
    00315
    00316 static int CDECL AircraftEngineSpeedSorter(const void *a, const void *b)
    00317 {
    00318   const int va = AircraftVehInfo(*(const EngineID*)a)->max_speed;
    00319   const int vb = AircraftVehInfo(*(const EngineID*)b)->max_speed;
    00320   const int r = va - vb;
    00321
    00322   if (r == 0) {
    00323     /* Use EngineID to sort instead since we want consistent sorting */
    00324     return EngineNumberSorter(a, b);
    00325   }
    00326   return _internal_sort_order ? -r : r;
    00327 }
    00328
    00329 static int CDECL AircraftEngineRunningCostSorter(const void *a, const void *b)
    00330 {
    00331   const int va = GetEngine(*(const EngineID*)a)->GetRunningCost();
    00332   const int vb = GetEngine(*(const EngineID*)b)->GetRunningCost();
    00333   const int r = va - vb;
    00334
    00335   if (r == 0) {
    00336     /* Use EngineID to sort instead since we want consistent sorting */
    00337     return EngineNumberSorter(a, b);
    00338   }
    00339   return _internal_sort_order ? -r : r;
    00340 }
    00341
    00342 static int CDECL AircraftEngineCargoSorter(const void *a, const void *b)
    00343 {
    00344   int va = AircraftVehInfo(*(const EngineID*)a)->passenger_capacity;
    00345   int vb = AircraftVehInfo(*(const EngineID*)b)->passenger_capacity;
    00346   int r = va - vb;
    00347
    00348   if (r == 0) {
    00349     /* The planes has the same passenger capacity. Check mail capacity instead */
    00350     va = AircraftVehInfo(*(const EngineID*)a)->mail_capacity;
    00351     vb = AircraftVehInfo(*(const EngineID*)b)->mail_capacity;
    00352     r = va - vb;
    00353
    00354     if (r == 0) {
    00355       /* Use EngineID to sort instead since we want consistent sorting */
    00356       return EngineNumberSorter(a, b);
    00357     }
    00358   }
    00359   return _internal_sort_order ? -r : r;
    00360 }
    00361
    00362 static EngList_SortTypeFunction * const _sorter[][10] = {{
    00363   /* Trains */
    00364   &EngineNumberSorter,
    00365   &TrainEngineCostSorter,
    00366   &TrainEngineSpeedSorter,
    00367   &TrainEnginePowerSorter,
    00368   &EngineIntroDateSorter,
    00369   &EngineNameSorter,
    00370   &TrainEngineRunningCostSorter,
    00371   &TrainEnginePowerVsRunningCostSorter,
    00372   &EngineReliabilitySorter,
    00373   &TrainEngineCapacitySorter,
    00374 }, {
    00375   /* Road vehicles */
    00376   &EngineNumberSorter,
    00377   &RoadVehEngineCostSorter,
    00378   &RoadVehEngineSpeedSorter,
    00379   &EngineIntroDateSorter,
    00380   &EngineNameSorter,
    00381   &RoadVehEngineRunningCostSorter,
    00382   &EngineReliabilitySorter,
    00383   &RoadVehEngineCapacitySorter,
    00384 }, {
    00385   /* Ships */
    00386   &EngineNumberSorter,
    00387   &ShipEngineCostSorter,
    00388   &ShipEngineSpeedSorter,
    00389   &EngineIntroDateSorter,
    00390   &EngineNameSorter,
    00391   &ShipEngineRunningCostSorter,
    00392   &EngineReliabilitySorter,
    00393   &ShipEngineCapacitySorter,
    00394 }, {
    00395   /* Aircraft */
    00396   &EngineNumberSorter,
    00397   &AircraftEngineCostSorter,
    00398   &AircraftEngineSpeedSorter,
    00399   &EngineIntroDateSorter,
    00400   &EngineNameSorter,
    00401   &AircraftEngineRunningCostSorter,
    00402   &EngineReliabilitySorter,
    00403   &AircraftEngineCargoSorter,
    00404 }};
    00405
    00406 static const StringID _sort_listing[][11] = {{
    00407   /* Trains */
    00408   STR_ENGINE_SORT_ENGINE_ID,
    00409   STR_ENGINE_SORT_COST,
    00410   STR_SORT_BY_MAX_SPEED,
    00411   STR_ENGINE_SORT_POWER,
    00412   STR_ENGINE_SORT_INTRO_DATE,
    00413   STR_SORT_BY_DROPDOWN_NAME,
    00414   STR_ENGINE_SORT_RUNNING_COST,
    00415   STR_ENGINE_SORT_POWER_VS_RUNNING_COST,
    00416   STR_SORT_BY_RELIABILITY,
    00417   STR_ENGINE_SORT_CARGO_CAPACITY,
    00418   INVALID_STRING_ID
    00419 }, {
    00420   /* Road vehicles */
    00421   STR_ENGINE_SORT_ENGINE_ID,
    00422   STR_ENGINE_SORT_COST,
    00423   STR_SORT_BY_MAX_SPEED,
    00424   STR_ENGINE_SORT_INTRO_DATE,
    00425   STR_SORT_BY_DROPDOWN_NAME,
    00426   STR_ENGINE_SORT_RUNNING_COST,
    00427   STR_SORT_BY_RELIABILITY,
    00428   STR_ENGINE_SORT_CARGO_CAPACITY,
    00429   INVALID_STRING_ID
    00430 }, {
    00431   /* Ships */
    00432   STR_ENGINE_SORT_ENGINE_ID,
    00433   STR_ENGINE_SORT_COST,
    00434   STR_SORT_BY_MAX_SPEED,
    00435   STR_ENGINE_SORT_INTRO_DATE,
    00436   STR_SORT_BY_DROPDOWN_NAME,
    00437   STR_ENGINE_SORT_RUNNING_COST,
    00438   STR_SORT_BY_RELIABILITY,
    00439   STR_ENGINE_SORT_CARGO_CAPACITY,
    00440   INVALID_STRING_ID
    00441 }, {
    00442   /* Aircraft */
    00443   STR_ENGINE_SORT_ENGINE_ID,
    00444   STR_ENGINE_SORT_COST,
    00445   STR_SORT_BY_MAX_SPEED,
    00446   STR_ENGINE_SORT_INTRO_DATE,
    00447   STR_SORT_BY_DROPDOWN_NAME,
    00448   STR_ENGINE_SORT_RUNNING_COST,
    00449   STR_SORT_BY_RELIABILITY,
    00450   STR_ENGINE_SORT_CARGO_CAPACITY,
    00451   INVALID_STRING_ID
    00452 }};
    00453
    00454 static int DrawCargoCapacityInfo(int x, int y, EngineID engine, VehicleType type, bool refittable)
    00455 {
    00456   uint16 *cap = GetCapacityOfArticulatedParts(engine, type);
    00457
    00458   for (uint c = 0; c < NUM_CARGO; c++) {
    00459     if (cap[c] == 0) continue;
    00460
    00461     SetDParam(0, c);
    00462     SetDParam(1, cap[c]);
    00463     SetDParam(2, refittable ? STR_9842_REFITTABLE : STR_EMPTY);
    00464     DrawString(x, y, STR_PURCHASE_INFO_CAPACITY, TC_FROMSTRING);
    00465     y += 10;
    00466
    00467     /* Only show as refittable once */
    00468     refittable = false;
    00469   }
    00470
    00471   return y;
    00472 }
    00473
    00474 /* Draw rail wagon specific details */
    00475 static int DrawRailWagonPurchaseInfo(int x, int y, EngineID engine_number, const RailVehicleInfo *rvi)
    00476 {
    00477   const Engine *e = GetEngine(engine_number);
    00478
    00479   /* Purchase cost */
    00480   SetDParam(0, e->GetCost());
    00481   DrawString(x, y, STR_PURCHASE_INFO_COST, TC_FROMSTRING);
    00482   y += 10;
    00483
    00484   /* Wagon weight - (including cargo) */
    00485   uint weight = e->GetDisplayWeight();
    00486   SetDParam(0, weight);
    00487   uint cargo_weight = (e->CanCarryCargo() ? GetCargo(e->GetDefaultCargoType())->weight * GetEngineProperty(engine_number, 0x14, rvi->capacity) >> 4 : 0);
    00488   SetDParam(1, cargo_weight + weight);
    00489   DrawString(x, y, STR_PURCHASE_INFO_WEIGHT_CWEIGHT, TC_FROMSTRING);
    00490   y += 10;
    00491
    00492   /* Wagon speed limit, displayed if above zero */
    00493   if (_settings_game.vehicle.wagon_speed_limits) {
    00494     uint max_speed = e->GetDisplayMaxSpeed();
    00495     if (max_speed > 0) {
    00496       SetDParam(0, max_speed);
    00497       DrawString(x, y, STR_PURCHASE_INFO_SPEED, TC_FROMSTRING);
    00498       y += 10;
    00499     }
    00500   }
    00501
    00502   /* Running cost */
    00503   if (rvi->running_cost_class != 0xFF) {
    00504     SetDParam(0, e->GetRunningCost());
    00505     DrawString(x, y, STR_PURCHASE_INFO_RUNNINGCOST, TC_FROMSTRING);
    00506     y += 10;
    00507   }
    00508
    00509   return y;
    00510 }
    00511
    00512 /* Draw locomotive specific details */
    00513 static int DrawRailEnginePurchaseInfo(int x, int y, EngineID engine_number, const RailVehicleInfo *rvi)
    00514 {
    00515   const Engine *e = GetEngine(engine_number);
    00516
    00517   /* Purchase Cost - Engine weight */
    00518   SetDParam(0, e->GetCost());
    00519   SetDParam(1, e->GetDisplayWeight());
    00520   DrawString(x, y, STR_PURCHASE_INFO_COST_WEIGHT, TC_FROMSTRING);
    00521   y += 10;
    00522
    00523   /* Max speed - Engine power */
    00524   SetDParam(0, e->GetDisplayMaxSpeed());
    00525   SetDParam(1, e->GetPower());
    00526   DrawString(x, y, STR_PURCHASE_INFO_SPEED_POWER, TC_FROMSTRING);
    00527   y += 10;
    00528
    00529   /* Max tractive effort - not applicable if old acceleration or maglev */
    00530   if (_settings_game.vehicle.train_acceleration_model != TAM_ORIGINAL && rvi->railtype != RAILTYPE_MAGLEV) {
    00531     SetDParam(0, e->GetDisplayMaxTractiveEffort());
    00532     DrawString(x, y, STR_PURCHASE_INFO_MAX_TE, TC_FROMSTRING);
    00533     y += 10;
    00534   }
    00535
    00536   /* Running cost */
    00537   if (rvi->running_cost_class != 0xFF) {
    00538     SetDParam(0, e->GetRunningCost());
    00539     DrawString(x, y, STR_PURCHASE_INFO_RUNNINGCOST, TC_FROMSTRING);
    00540     y += 10;
    00541   }
    00542
    00543   /* Powered wagons power - Powered wagons extra weight */
    00544   if (rvi->pow_wag_power != 0) {
    00545     SetDParam(0, rvi->pow_wag_power);
    00546     SetDParam(1, rvi->pow_wag_weight);
    00547     DrawString(x, y, STR_PURCHASE_INFO_PWAGPOWER_PWAGWEIGHT, TC_FROMSTRING);
    00548     y += 10;
    00549   };
    00550
    00551   return y;
    00552 }
    00553
    00554 /* Draw road vehicle specific details */
    00555 static int DrawRoadVehPurchaseInfo(int x, int y, EngineID engine_number)
    00556 {
    00557   const Engine *e = GetEngine(engine_number);
    00558
    00559   /* Purchase cost - Max speed */
    00560   SetDParam(0, e->GetCost());
    00561   SetDParam(1, e->GetDisplayMaxSpeed());
    00562   DrawString(x, y, STR_PURCHASE_INFO_COST_SPEED, TC_FROMSTRING);
    00563   y += 10;
    00564
    00565   /* Running cost */
    00566   SetDParam(0, e->GetRunningCost());
    00567   DrawString(x, y, STR_PURCHASE_INFO_RUNNINGCOST, TC_FROMSTRING);
    00568   y += 10;
    00569
    00570   return y;
    00571 }
    00572
    00573 /* Draw ship specific details */
    00574 static int DrawShipPurchaseInfo(int x, int y, EngineID engine_number, const ShipVehicleInfo *svi, bool refittable)
    00575 {
    00576   const Engine *e = GetEngine(engine_number);
    00577
    00578   /* Purchase cost - Max speed */
    00579   SetDParam(0, e->GetCost());
    00580   SetDParam(1, e->GetDisplayMaxSpeed());
    00581   DrawString(x, y, STR_PURCHASE_INFO_COST_SPEED, TC_FROMSTRING);
    00582   y += 10;
    00583
    00584   /* Cargo type + capacity */
    00585   SetDParam(0, e->GetDefaultCargoType());
    00586   SetDParam(1, GetEngineProperty(engine_number, 0x0D, svi->capacity));
    00587   SetDParam(2, refittable ? STR_9842_REFITTABLE : STR_EMPTY);
    00588   DrawString(x, y, STR_PURCHASE_INFO_CAPACITY, TC_FROMSTRING);
    00589   y += 10;
    00590
    00591   /* Running cost */
    00592   SetDParam(0, e->GetRunningCost());
    00593   DrawString(x, y, STR_PURCHASE_INFO_RUNNINGCOST, TC_FROMSTRING);
    00594   y += 10;
    00595
    00596   return y;
    00597 }
    00598
    00599 /* Draw aircraft specific details */
    00600 static int DrawAircraftPurchaseInfo(int x, int y, EngineID engine_number, const AircraftVehicleInfo *avi, bool refittable)
    00601 {
    00602   const Engine *e = GetEngine(engine_number);
    00603   CargoID cargo = e->GetDefaultCargoType();
    00604
    00605   /* Purchase cost - Max speed */
    00606   SetDParam(0, e->GetCost());
    00607   SetDParam(1, e->GetDisplayMaxSpeed());
    00608   DrawString(x, y, STR_PURCHASE_INFO_COST_SPEED, TC_FROMSTRING);
    00609   y += 10;
    00610
    00611   /* Cargo capacity */
    00612   if (cargo == CT_INVALID || cargo == CT_PASSENGERS) {
    00613     SetDParam(0, avi->passenger_capacity);
    00614     SetDParam(1, avi->mail_capacity);
    00615     DrawString(x, y, STR_PURCHASE_INFO_AIRCRAFT_CAPACITY, TC_FROMSTRING);
    00616   } else {
    00617     /* Note, if the default capacity is selected by the refit capacity
    00618     * callback, then the capacity shown is likely to be incorrect. */
    00619     SetDParam(0, cargo);
    00620     SetDParam(1, AircraftDefaultCargoCapacity(cargo, avi));
    00621     SetDParam(2, refittable ? STR_9842_REFITTABLE : STR_EMPTY);
    00622     DrawString(x, y, STR_PURCHASE_INFO_CAPACITY, TC_FROMSTRING);
    00623   }
    00624   y += 10;
    00625
    00626   /* Running cost */
    00627   SetDParam(0, e->GetRunningCost());
    00628   DrawString(x, y, STR_PURCHASE_INFO_RUNNINGCOST, TC_FROMSTRING);
    00629   y += 10;
    00630
    00631   return y;
    00632 }
    00633
    00641 int DrawVehiclePurchaseInfo(int x, int y, uint w, EngineID engine_number)
    00642 {
    00643   const Engine *e = GetEngine(engine_number);
    00644   YearMonthDay ymd;
    00645   ConvertDateToYMD(e->intro_date, &ymd);
    00646   bool refittable = IsArticulatedVehicleRefittable(engine_number);
    00647
    00648   switch (e->type) {
    00649     default: NOT_REACHED();
    00650     case VEH_TRAIN: {
    00651       const RailVehicleInfo *rvi = RailVehInfo(engine_number);
    00652       if (rvi->railveh_type == RAILVEH_WAGON) {
    00653         y = DrawRailWagonPurchaseInfo(x, y, engine_number, rvi);
    00654       } else {
    00655         y = DrawRailEnginePurchaseInfo(x, y, engine_number, rvi);
    00656       }
    00657
    00658       /* Cargo type + capacity, or N/A */
    00659       int new_y = DrawCargoCapacityInfo(x, y, engine_number, VEH_TRAIN, refittable);
    00660
    00661       if (new_y == y) {
    00662         SetDParam(0, CT_INVALID);
    00663         SetDParam(2, STR_EMPTY);
    00664         DrawString(x, y, STR_PURCHASE_INFO_CAPACITY, TC_FROMSTRING);
    00665         y += 10;
    00666       } else {
    00667         y = new_y;
    00668       }
    00669       break;
    00670     }
    00671     case VEH_ROAD: {
    00672       y = DrawRoadVehPurchaseInfo(x, y, engine_number);
    00673
    00674       /* Cargo type + capacity, or N/A */
    00675       int new_y = DrawCargoCapacityInfo(x, y, engine_number, VEH_ROAD, refittable);
    00676
    00677       if (new_y == y) {
    00678         SetDParam(0, CT_INVALID);
    00679         SetDParam(2, STR_EMPTY);
    00680         DrawString(x, y, STR_PURCHASE_INFO_CAPACITY, TC_FROMSTRING);
    00681         y += 10;
    00682       } else {
    00683         y = new_y;
    00684       }
    00685       break;
    00686     }
    00687     case VEH_SHIP:
    00688       y = DrawShipPurchaseInfo(x, y, engine_number, ShipVehInfo(engine_number), refittable);
    00689       break;
    00690     case VEH_AIRCRAFT:
    00691       y = DrawAircraftPurchaseInfo(x, y, engine_number, AircraftVehInfo(engine_number), refittable);
    00692       break;
    00693   }
    00694
    00695   /* Draw details, that applies to all types except rail wagons */
    00696   if (e->type != VEH_TRAIN || RailVehInfo(engine_number)->railveh_type != RAILVEH_WAGON) {
    00697     /* Design date - Life length */
    00698     SetDParam(0, ymd.year);
    00699     SetDParam(1, e->lifelength);
    00700     DrawString(x, y, STR_PURCHASE_INFO_DESIGNED_LIFE, TC_FROMSTRING);
    00701     y += 10;
    00702
    00703     /* Reliability */
    00704     SetDParam(0, e->reliability * 100 >> 16);
    00705     DrawString(x, y, STR_PURCHASE_INFO_RELIABILITY, TC_FROMSTRING);
    00706     y += 10;
    00707   }
    00708
    00709   /* Additional text from NewGRF */
    00710   y += ShowAdditionalText(x, y, w, engine_number);
    00711   if (refittable) y += ShowRefitOptionsList(x, y, w, engine_number);
    00712
    00713   return y;
    00714 }
    00715
    00716 static void DrawVehicleEngine(VehicleType type, int x, int y, EngineID engine, SpriteID pal)
    00717 {
    00718   switch (type) {
    00719     case VEH_TRAIN:    DrawTrainEngine(   x, y, engine, pal); break;
    00720     case VEH_ROAD:     DrawRoadVehEngine( x, y, engine, pal); break;
    00721     case VEH_SHIP:     DrawShipEngine(    x, y, engine, pal); break;
    00722     case VEH_AIRCRAFT: DrawAircraftEngine(x, y, engine, pal); break;
    00723     default: NOT_REACHED();
    00724   }
    00725 }
    00726
    00736 void DrawEngineList(VehicleType type, int x, int r, int y, const GUIEngineList *eng_list, uint16 min, uint16 max, EngineID selected_id, int count_location, GroupID selected_group)
    00737 {
    00738   byte step_size = GetVehicleListHeight(type);
    00739   byte x_offset = 0;
    00740   byte y_offset = 0;
    00741
    00742   assert(max <= eng_list->Length());
    00743
    00744   switch (type) {
    00745     case VEH_TRAIN:
    00746       x++; // train and road vehicles use the same offset, except trains are one more pixel to the right
    00747       /* Fallthough */
    00748     case VEH_ROAD:
    00749       x += 26;
    00750       x_offset = 30;
    00751       y += 2;
    00752       y_offset = 4;
    00753       break;
    00754     case VEH_SHIP:
    00755       x += 35;
    00756       x_offset = 40;
    00757       y += 7;
    00758       y_offset = 3;
    00759       break;
    00760     case VEH_AIRCRAFT:
    00761       x += 27;
    00762       x_offset = 33;
    00763       y += 7;
    00764       y_offset = 3;
    00765       break;
    00766     default: NOT_REACHED();
    00767   }
    00768
    00769   uint maxw = r - x - x_offset;
    00770
    00771   for (; min < max; min++, y += step_size) {
    00772     const EngineID engine = (*eng_list)[min];
    00773     /* Note: num_engines is only used in the autoreplace GUI, so it is correct to use _local_company here. */
    00774     const uint num_engines = GetGroupNumEngines(_local_company, selected_group, engine);
    00775
    00776     SetDParam(0, engine);
    00777     DrawStringTruncated(x + x_offset, y, STR_ENGINE_NAME, engine == selected_id ? TC_WHITE : TC_BLACK, maxw);
    00778     DrawVehicleEngine(type, x, y + y_offset, engine, (count_location != 0 && num_engines == 0) ? PALETTE_CRASH : GetEnginePalette(engine, _local_company));
    00779     if (count_location != 0) {
    00780       SetDParam(0, num_engines);
    00781       DrawStringRightAligned(count_location, y + (GetVehicleListHeight(type) == 14 ? 3 : 8), STR_TINY_BLACK, TC_FROMSTRING);
    00782     }
    00783   }
    00784 }
    00785
    00786
    00787 struct BuildVehicleWindow : Window {
    00788   VehicleType vehicle_type;
    00789   union {
    00790     RailTypeByte railtype;
    00791     AirportFTAClass::Flags flags;
    00792     RoadTypes roadtypes;
    00793   } filter;
    00794   bool descending_sort_order;
    00795   byte sort_criteria;
    00796   bool regenerate_list;
    00797   bool listview_mode;
    00798   EngineID sel_engine;
    00799   EngineID rename_engine;
    00800   GUIEngineList eng_list;
    00801
    00802   BuildVehicleWindow(const WindowDesc *desc, TileIndex tile, VehicleType type) : Window(desc, tile == INVALID_TILE ? (int)type : tile)
    00803   {
    00804     this->vehicle_type = type;
    00805     int vlh = GetVehicleListHeight(this->vehicle_type);
    00806
    00807     ResizeWindow(this, 0, vlh - 14);
    00808     this->resize.step_height = vlh;
    00809     this->vscroll.cap = 1;
    00810     this->widget[BUILD_VEHICLE_WIDGET_LIST].data = 0x101;
    00811
    00812     this->resize.width  = this->width;
    00813     this->resize.height = this->height;
    00814
    00815     this->owner = (tile != INVALID_TILE) ? GetTileOwner(tile) : _local_company;
    00816
    00817     this->sel_engine      = INVALID_ENGINE;
    00818     this->regenerate_list = false;
    00819
    00820     this->sort_criteria         = _last_sort_criteria[type];
    00821     this->descending_sort_order = _last_sort_order[type];
    00822
    00823     switch (type) {
    00824       default: NOT_REACHED();
    00825       case VEH_TRAIN:
    00826         this->filter.railtype = (tile == INVALID_TILE) ? RAILTYPE_END : GetRailType(tile);
    00827         break;
    00828       case VEH_ROAD:
    00829         this->filter.roadtypes = (tile == INVALID_TILE) ? ROADTYPES_ALL : GetRoadTypes(tile);
    00830       case VEH_SHIP:
    00831         break;
    00832       case VEH_AIRCRAFT:
    00833         this->filter.flags =
    00834           tile == INVALID_TILE ? AirportFTAClass::ALL : GetStationByTile(tile)->Airport()->flags;
    00835         break;
    00836     }
    00837     this->SetupWindowStrings(type);
    00838
    00839     this->listview_mode = (this->window_number <= VEH_END);
    00840     /* If we are just viewing the list of vehicles, we do not need the Build button.
    00841      * So we just hide it, and enlarge the Rename buton by the now vacant place. */
    00842     if (this->listview_mode) {
    00843       this->HideWidget(BUILD_VEHICLE_WIDGET_BUILD);
    00844       this->widget[BUILD_VEHICLE_WIDGET_RENAME].left = this->widget[BUILD_VEHICLE_WIDGET_BUILD].left;
    00845     } else {
    00846       /* Both are visible, adjust the size of each */
    00847       ResizeButtons(this, BUILD_VEHICLE_WIDGET_BUILD, BUILD_VEHICLE_WIDGET_RENAME);
    00848     }
    00849
    00850     this->GenerateBuildList(); // generate the list, since we need it in the next line
    00851     /* Select the first engine in the list as default when opening the window */
    00852     if (this->eng_list.Length() > 0) this->sel_engine = this->eng_list[0];
    00853
    00854     this->FindWindowPlacementAndResize(desc);
    00855   }
    00856
    00857   /* Setup widget strings to fit the different types of vehicles */
    00858   void SetupWindowStrings(VehicleType type)
    00859   {
    00860     switch (type) {
    00861       default: NOT_REACHED();
    00862
    00863       case VEH_TRAIN:
    00864         this->widget[BUILD_VEHICLE_WIDGET_CAPTION].data    = this->listview_mode ? STR_AVAILABLE_TRAINS : STR_JUST_STRING;
    00865         this->widget[BUILD_VEHICLE_WIDGET_LIST].tooltips   = STR_8843_TRAIN_VEHICLE_SELECTION;
    00866         this->widget[BUILD_VEHICLE_WIDGET_BUILD].data      = STR_881F_BUILD_VEHICLE;
    00867         this->widget[BUILD_VEHICLE_WIDGET_BUILD].tooltips  = STR_8844_BUILD_THE_HIGHLIGHTED_TRAIN;
    00868         this->widget[BUILD_VEHICLE_WIDGET_RENAME].data     = STR_8820_RENAME;
    00869         this->widget[BUILD_VEHICLE_WIDGET_RENAME].tooltips = STR_8845_RENAME_TRAIN_VEHICLE_TYPE;
    00870         break;
    00871
    00872       case VEH_ROAD:
    00873         this->widget[BUILD_VEHICLE_WIDGET_CAPTION].data    = this->listview_mode ? STR_AVAILABLE_ROAD_VEHICLES : STR_9006_NEW_ROAD_VEHICLES;
    00874         this->widget[BUILD_VEHICLE_WIDGET_LIST].tooltips   = STR_9026_ROAD_VEHICLE_SELECTION;
    00875         this->widget[BUILD_VEHICLE_WIDGET_BUILD].data      = STR_9007_BUILD_VEHICLE;
    00876         this->widget[BUILD_VEHICLE_WIDGET_BUILD].tooltips  = STR_9027_BUILD_THE_HIGHLIGHTED_ROAD;
    00877         this->widget[BUILD_VEHICLE_WIDGET_RENAME].data     = STR_9034_RENAME;
    00878         this->widget[BUILD_VEHICLE_WIDGET_RENAME].tooltips = STR_9035_RENAME_ROAD_VEHICLE_TYPE;
    00879         break;
    00880
    00881       case VEH_SHIP:
    00882         this->widget[BUILD_VEHICLE_WIDGET_CAPTION].data    = this->listview_mode ? STR_AVAILABLE_SHIPS : STR_9808_NEW_SHIPS;
    00883         this->widget[BUILD_VEHICLE_WIDGET_LIST].tooltips   = STR_9825_SHIP_SELECTION_LIST_CLICK;
    00884         this->widget[BUILD_VEHICLE_WIDGET_BUILD].data      = STR_9809_BUILD_SHIP;
    00885         this->widget[BUILD_VEHICLE_WIDGET_BUILD].tooltips  = STR_9826_BUILD_THE_HIGHLIGHTED_SHIP;
    00886         this->widget[BUILD_VEHICLE_WIDGET_RENAME].data     = STR_9836_RENAME;
    00887         this->widget[BUILD_VEHICLE_WIDGET_RENAME].tooltips = STR_9837_RENAME_SHIP_TYPE;
    00888         break;
    00889
    00890       case VEH_AIRCRAFT:
    00891         this->widget[BUILD_VEHICLE_WIDGET_CAPTION].data    = this->listview_mode ? STR_AVAILABLE_AIRCRAFT : STR_A005_NEW_AIRCRAFT;
    00892         this->widget[BUILD_VEHICLE_WIDGET_LIST].tooltips   = STR_A025_AIRCRAFT_SELECTION_LIST;
    00893         this->widget[BUILD_VEHICLE_WIDGET_BUILD].data      = STR_A006_BUILD_AIRCRAFT;
    00894         this->widget[BUILD_VEHICLE_WIDGET_BUILD].tooltips  = STR_A026_BUILD_THE_HIGHLIGHTED_AIRCRAFT;
    00895         this->widget[BUILD_VEHICLE_WIDGET_RENAME].data     = STR_A037_RENAME;
    00896         this->widget[BUILD_VEHICLE_WIDGET_RENAME].tooltips = STR_A038_RENAME_AIRCRAFT_TYPE;
    00897         break;
    00898     }
    00899   }
    00900
    00901   /* Figure out what train EngineIDs to put in the list */
    00902   void GenerateBuildTrainList()
    00903   {
    00904     EngineID sel_id = INVALID_ENGINE;
    00905     int num_engines = 0;
    00906     int num_wagons  = 0;
    00907
    00908     this->filter.railtype = (this->listview_mode) ? RAILTYPE_END : GetRailType(this->window_number);
    00909
    00910     this->eng_list.Clear();
    00911
    00912     /* Make list of all available train engines and wagons.
    00913     * Also check to see if the previously selected engine is still available,
    00914     * and if not, reset selection to INVALID_ENGINE. This could be the case
    00915     * when engines become obsolete and are removed */
    00916     const Engine *e;
    00917     FOR_ALL_ENGINES_OF_TYPE(e, VEH_TRAIN) {
    00918       EngineID eid = e->index;
    00919       const RailVehicleInfo *rvi = &e->u.rail;
    00920
    00921       if (this->filter.railtype != RAILTYPE_END && !HasPowerOnRail(rvi->railtype, this->filter.railtype)) continue;
    00922       if (!IsEngineBuildable(eid, VEH_TRAIN, _local_company)) continue;
    00923
    00924       *this->eng_list.Append() = eid;
    00925
    00926       if (rvi->railveh_type != RAILVEH_WAGON) {
    00927         num_engines++;
    00928       } else {
    00929         num_wagons++;
    00930       }
    00931
    00932       if (eid == this->sel_engine) sel_id = eid;
    00933     }
    00934
    00935     this->sel_engine = sel_id;
    00936
    00937     /* make engines first, and then wagons, sorted by ListPositionOfEngine() */
    00938     _internal_sort_order = false;
    00939     EngList_Sort(&this->eng_list, TrainEnginesThenWagonsSorter);
    00940
    00941     /* and then sort engines */
    00942     _internal_sort_order = this->descending_sort_order;
    00943     EngList_SortPartial(&this->eng_list, _sorter[0][this->sort_criteria], 0, num_engines);
    00944
    00945     /* and finally sort wagons */
    00946     EngList_SortPartial(&this->eng_list, _sorter[0][this->sort_criteria], num_engines, num_wagons);
    00947   }
    00948
    00949   /* Figure out what road vehicle EngineIDs to put in the list */
    00950   void GenerateBuildRoadVehList()
    00951   {
    00952     EngineID sel_id = INVALID_ENGINE;
    00953
    00954     this->eng_list.Clear();
    00955
    00956     const Engine *e;
    00957     FOR_ALL_ENGINES_OF_TYPE(e, VEH_ROAD) {
    00958       EngineID eid = e->index;
    00959       if (!IsEngineBuildable(eid, VEH_ROAD, _local_company)) continue;
    00960       if (!HasBit(this->filter.roadtypes, HasBit(EngInfo(eid)->misc_flags, EF_ROAD_TRAM) ? ROADTYPE_TRAM : ROADTYPE_ROAD)) continue;
    00961       *this->eng_list.Append() = eid;
    00962
    00963       if (eid == this->sel_engine) sel_id = eid;
    00964     }
    00965     this->sel_engine = sel_id;
    00966   }
    00967
    00968   /* Figure out what ship EngineIDs to put in the list */
    00969   void GenerateBuildShipList()
    00970   {
    00971     EngineID sel_id = INVALID_ENGINE;
    00972     this->eng_list.Clear();
    00973
    00974     const Engine *e;
    00975     FOR_ALL_ENGINES_OF_TYPE(e, VEH_SHIP) {
    00976       EngineID eid = e->index;
    00977       if (!IsEngineBuildable(eid, VEH_SHIP, _local_company)) continue;
    00978       *this->eng_list.Append() = eid;
    00979
    00980       if (eid == this->sel_engine) sel_id = eid;
    00981     }
    00982     this->sel_engine = sel_id;
    00983   }
    00984
    00985   /* Figure out what aircraft EngineIDs to put in the list */
    00986   void GenerateBuildAircraftList()
    00987   {
    00988     EngineID sel_id = INVALID_ENGINE;
    00989
    00990     this->eng_list.Clear();
    00991
    00992     const Station *st = this->listview_mode ? NULL : GetStationByTile(this->window_number);
    00993
    00994     /* Make list of all available planes.
    00995     * Also check to see if the previously selected plane is still available,
    00996     * and if not, reset selection to INVALID_ENGINE. This could be the case
    00997     * when planes become obsolete and are removed */
    00998     const Engine *e;
    00999     FOR_ALL_ENGINES_OF_TYPE(e, VEH_AIRCRAFT) {
    01000       EngineID eid = e->index;
    01001       if (!IsEngineBuildable(eid, VEH_AIRCRAFT, _local_company)) continue;
    01002       /* First VEH_END window_numbers are fake to allow a window open for all different types at once */
    01003       if (!this->listview_mode && !CanVehicleUseStation(eid, st)) continue;
    01004
    01005       *this->eng_list.Append() = eid;
    01006       if (eid == this->sel_engine) sel_id = eid;
    01007     }
    01008
    01009     this->sel_engine = sel_id;
    01010   }
    01011
    01012   /* Generate the list of vehicles */
    01013   void GenerateBuildList()
    01014   {
    01015     switch (this->vehicle_type) {
    01016       default: NOT_REACHED();
    01017       case VEH_TRAIN:
    01018         this->GenerateBuildTrainList();
    01019         return; // trains should not reach the last sorting
    01020       case VEH_ROAD:
    01021         this->GenerateBuildRoadVehList();
    01022         break;
    01023       case VEH_SHIP:
    01024         this->GenerateBuildShipList();
    01025         break;
    01026       case VEH_AIRCRAFT:
    01027         this->GenerateBuildAircraftList();
    01028         break;
    01029     }
    01030     _internal_sort_order = this->descending_sort_order;
    01031     EngList_Sort(&this->eng_list, _sorter[this->vehicle_type][this->sort_criteria]);
    01032   }
    01033
    01034   void OnClick(Point pt, int widget)
    01035   {
    01036     switch (widget) {
    01037       case BUILD_VEHICLE_WIDGET_SORT_ASSENDING_DESCENDING:
    01038         this->descending_sort_order ^= true;
    01039         _last_sort_order[this->vehicle_type] = this->descending_sort_order;
    01040         this->regenerate_list = true;
    01041         this->SetDirty();
    01042         break;
    01043
    01044       case BUILD_VEHICLE_WIDGET_LIST: {
    01045         uint i = (pt.y - this->widget[BUILD_VEHICLE_WIDGET_LIST].top) / GetVehicleListHeight(this->vehicle_type) + this->vscroll.pos;
    01046         size_t num_items = this->eng_list.Length();
    01047         this->sel_engine = (i < num_items) ? this->eng_list[i] : INVALID_ENGINE;
    01048         this->SetDirty();
    01049         break;
    01050       }
    01051
    01052       case BUILD_VEHICLE_WIDGET_SORT_DROPDOWN: // Select sorting criteria dropdown menu
    01053         ShowDropDownMenu(this, _sort_listing[this->vehicle_type], this->sort_criteria, BUILD_VEHICLE_WIDGET_SORT_DROPDOWN, 0, 0);
    01054         break;
    01055
    01056       case BUILD_VEHICLE_WIDGET_BUILD: {
    01057         EngineID sel_eng = this->sel_engine;
    01058         if (sel_eng != INVALID_ENGINE) {
    01059           switch (this->vehicle_type) {
    01060             default: NOT_REACHED();
    01061             case VEH_TRAIN:
    01062               DoCommandP(this->window_number, sel_eng, 0,
    01063                   CMD_BUILD_RAIL_VEHICLE | CMD_MSG(STR_882B_CAN_T_BUILD_RAILROAD_VEHICLE),
    01064                   (RailVehInfo(sel_eng)->railveh_type == RAILVEH_WAGON) ? CcBuildWagon : CcBuildLoco);
    01065               break;
    01066             case VEH_ROAD:
    01067               DoCommandP(this->window_number, sel_eng, 0, CMD_BUILD_ROAD_VEH | CMD_MSG(STR_9009_CAN_T_BUILD_ROAD_VEHICLE), CcBuildRoadVeh);
    01068               break;
    01069             case VEH_SHIP:
    01070               DoCommandP(this->window_number, sel_eng, 0, CMD_BUILD_SHIP | CMD_MSG(STR_980D_CAN_T_BUILD_SHIP), CcBuildShip);
    01071               break;
    01072             case VEH_AIRCRAFT:
    01073               DoCommandP(this->window_number, sel_eng, 0, CMD_BUILD_AIRCRAFT | CMD_MSG(STR_A008_CAN_T_BUILD_AIRCRAFT), CcBuildAircraft);
    01074               break;
    01075           }
    01076         }
    01077         break;
    01078       }
    01079
    01080       case BUILD_VEHICLE_WIDGET_RENAME: {
    01081         EngineID sel_eng = this->sel_engine;
    01082         if (sel_eng != INVALID_ENGINE) {
    01083           StringID str = STR_NULL;
    01084
    01085           this->rename_engine = sel_eng;
    01086           switch (this->vehicle_type) {
    01087             default: NOT_REACHED();
    01088             case VEH_TRAIN:    str = STR_886A_RENAME_TRAIN_VEHICLE_TYPE; break;
    01089             case VEH_ROAD:     str = STR_9036_RENAME_ROAD_VEHICLE_TYPE;  break;
    01090             case VEH_SHIP:     str = STR_9838_RENAME_SHIP_TYPE;          break;
    01091             case VEH_AIRCRAFT: str = STR_A039_RENAME_AIRCRAFT_TYPE;      break;
    01092           }
    01093           SetDParam(0, sel_eng);
    01094           ShowQueryString(STR_ENGINE_NAME, str, MAX_LENGTH_ENGINE_NAME_BYTES, MAX_LENGTH_ENGINE_NAME_PIXELS, this, CS_ALPHANUMERAL, QSF_ENABLE_DEFAULT);
    01095         }
    01096         break;
    01097       }
    01098     }
    01099   }
    01100
    01101   virtual void OnInvalidateData(int data)
    01102   {
    01103     this->regenerate_list = true;
    01104   }
    01105
    01106   virtual void OnPaint()
    01107   {
    01108     if (this->regenerate_list) {
    01109       this->regenerate_list = false;
    01110       this->GenerateBuildList();
    01111     }
    01112
    01113     uint max = min(this->vscroll.pos + this->vscroll.cap, this->eng_list.Length());
    01114
    01115     SetVScrollCount(this, this->eng_list.Length());
    01116     if (this->vehicle_type == VEH_TRAIN) {
    01117       if (this->filter.railtype == RAILTYPE_END) {
    01118         SetDParam(0, STR_ALL_AVAIL_RAIL_VEHICLES);
    01119       } else {
    01120         const RailtypeInfo *rti = GetRailTypeInfo(this->filter.railtype);
    01121         SetDParam(0, rti->strings.build_caption);
    01122       }
    01123     }
    01124
    01125     /* Set text of sort by dropdown */
    01126     this->widget[BUILD_VEHICLE_WIDGET_SORT_DROPDOWN].data = _sort_listing[this->vehicle_type][this->sort_criteria];
    01127
    01128     this->DrawWidgets();
    01129
    01130     DrawEngineList(this->vehicle_type, this->widget[BUILD_VEHICLE_WIDGET_LIST].left + 2, this->widget[BUILD_VEHICLE_WIDGET_LIST].right, this->widget[BUILD_VEHICLE_WIDGET_LIST].top + 1, &this->eng_list, this->vscroll.pos, max, this->sel_engine, 0, DEFAULT_GROUP);
    01131
    01132     if (this->sel_engine != INVALID_ENGINE) {
    01133       const Widget *wi = &this->widget[BUILD_VEHICLE_WIDGET_PANEL];
    01134       int text_end = DrawVehiclePurchaseInfo(2, wi->top + 1, wi->right - wi->left - 2, this->sel_engine);
    01135
    01136       if (text_end > wi->bottom) {
    01137         this->SetDirty();
    01138         ResizeWindowForWidget(this, BUILD_VEHICLE_WIDGET_PANEL, 0, text_end - wi->bottom);
    01139         this->SetDirty();
    01140       }
    01141     }
    01142
    01143     this->DrawSortButtonState(BUILD_VEHICLE_WIDGET_SORT_ASSENDING_DESCENDING, this->descending_sort_order ? SBS_DOWN : SBS_UP);
    01144   }
    01145
    01146   virtual void OnDoubleClick(Point pt, int widget)
    01147   {
    01148     if (widget == BUILD_VEHICLE_WIDGET_LIST) {
    01149       /* When double clicking, we want to buy a vehicle */
    01150       this->OnClick(pt, BUILD_VEHICLE_WIDGET_BUILD);
    01151     }
    01152   }
    01153
    01154   virtual void OnQueryTextFinished(char *str)
    01155   {
    01156     if (str == NULL) return;
    01157
    01158     StringID err_str = STR_NULL;
    01159     switch (this->vehicle_type) {
    01160       default: NOT_REACHED();
    01161       case VEH_TRAIN:    err_str = STR_886B_CAN_T_RENAME_TRAIN_VEHICLE; break;
    01162       case VEH_ROAD:     err_str = STR_9037_CAN_T_RENAME_ROAD_VEHICLE;  break;
    01163       case VEH_SHIP:     err_str = STR_9839_CAN_T_RENAME_SHIP_TYPE;     break;
    01164       case VEH_AIRCRAFT: err_str = STR_A03A_CAN_T_RENAME_AIRCRAFT_TYPE; break;
    01165     }
    01166     DoCommandP(0, this->rename_engine, 0, CMD_RENAME_ENGINE | CMD_MSG(err_str), NULL, str);
    01167   }
    01168
    01169   virtual void OnDropdownSelect(int widget, int index)
    01170   {
    01171     if (this->sort_criteria != index) {
    01172       this->sort_criteria = index;
    01173       _last_sort_criteria[this->vehicle_type] = this->sort_criteria;
    01174       this->regenerate_list = true;
    01175     }
    01176     this->SetDirty();
    01177   }
    01178
    01179   virtual void OnResize(Point new_size, Point delta)
    01180   {
    01181     if (delta.x != 0 && !this->listview_mode) {
    01182       ResizeButtons(this, BUILD_VEHICLE_WIDGET_BUILD, BUILD_VEHICLE_WIDGET_RENAME);
    01183     }
    01184     if (delta.y == 0) return;
    01185
    01186     this->vscroll.cap += delta.y / (int)GetVehicleListHeight(this->vehicle_type);
    01187     this->widget[BUILD_VEHICLE_WIDGET_LIST].data = (this->vscroll.cap << 8) + 1;
    01188   }
    01189 };
    01190
    01191 static const WindowDesc _build_vehicle_desc = {
    01192   WDP_AUTO, WDP_AUTO, 240, 174, 240, 256,
    01193   WC_BUILD_VEHICLE, WC_NONE,
    01194   WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS | WDF_RESIZABLE | WDF_CONSTRUCTION,
    01195   _build_vehicle_widgets,
    01196 };
    01197
    01198 void ShowBuildVehicleWindow(TileIndex tile, VehicleType type)
    01199 {
    01200   /* We want to be able to open both Available Train as Available Ships,
    01201    *  so if tile == INVALID_TILE (Available XXX Window), use 'type' as unique number.
    01202    *  As it always is a low value, it won't collide with any real tile
    01203    *  number. */
    01204   uint num = (tile == INVALID_TILE) ? (int)type : tile;
    01205
    01206   assert(IsCompanyBuildableVehicleType(type));
    01207
    01208   DeleteWindowById(WC_BUILD_VEHICLE, num);
    01209
    01210   new BuildVehicleWindow(&_build_vehicle_desc, tile, type);
    01211 }