# build_vehicle_gui.cpp

Go to the documentation of this file.

    00001 /* $Id: build_vehicle_gui.cpp 15427 2009-02-09 02:33:10Z rubidium $ */
    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   SetDParam(1, (GetCargo(rvi->cargo_type)->weight * GetEngineProperty(engine_number, 0x14, rvi->capacity) >> 4) + weight);
    00488   DrawString(x, y, STR_PURCHASE_INFO_WEIGHT_CWEIGHT, TC_FROMSTRING);
    00489   y += 10;
    00490
    00491   /* Wagon speed limit, displayed if above zero */
    00492   if (_settings_game.vehicle.wagon_speed_limits) {
    00493     uint max_speed = e->GetDisplayMaxSpeed();
    00494     if (max_speed > 0) {
    00495       SetDParam(0, max_speed);
    00496       DrawString(x, y, STR_PURCHASE_INFO_SPEED, TC_FROMSTRING);
    00497       y += 10;
    00498     }
    00499   }
    00500
    00501   /* Running cost */
    00502   if (rvi->running_cost_class != 0xFF) {
    00503     SetDParam(0, e->GetRunningCost());
    00504     DrawString(x, y, STR_PURCHASE_INFO_RUNNINGCOST, TC_FROMSTRING);
    00505     y += 10;
    00506   }
    00507
    00508   return y;
    00509 }
    00510
    00511 /* Draw locomotive specific details */
    00512 static int DrawRailEnginePurchaseInfo(int x, int y, EngineID engine_number, const RailVehicleInfo *rvi)
    00513 {
    00514   const Engine *e = GetEngine(engine_number);
    00515   uint weight = e->GetDisplayWeight();
    00516
    00517   /* Purchase Cost - Engine weight */
    00518   SetDParam(0, e->GetCost());
    00519   SetDParam(1, weight);
    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, (weight * 10 * GetEngineProperty(engine_number, 0x1F, rvi->tractive_effort)) / 256);
    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   /* Cargo type + capacity */
    00571   return DrawCargoCapacityInfo(x, y, engine_number, VEH_ROAD, IsEngineRefittable(engine_number));
    00572 }
    00573
    00574 /* Draw ship specific details */
    00575 static int DrawShipPurchaseInfo(int x, int y, EngineID engine_number, const ShipVehicleInfo *svi)
    00576 {
    00577   const Engine *e = GetEngine(engine_number);
    00578
    00579   /* Purchase cost - Max speed */
    00580   SetDParam(0, e->GetCost());
    00581   SetDParam(1, e->GetDisplayMaxSpeed());
    00582   DrawString(x, y, STR_PURCHASE_INFO_COST_SPEED, TC_FROMSTRING);
    00583   y += 10;
    00584
    00585   /* Cargo type + capacity */
    00586   SetDParam(0, svi->cargo_type);
    00587   SetDParam(1, GetEngineProperty(engine_number, 0x0D, svi->capacity));
    00588   SetDParam(2, IsEngineRefittable(engine_number) ? STR_9842_REFITTABLE : STR_EMPTY);
    00589   DrawString(x, y, STR_PURCHASE_INFO_CAPACITY, TC_FROMSTRING);
    00590   y += 10;
    00591
    00592   /* Running cost */
    00593   SetDParam(0, e->GetRunningCost());
    00594   DrawString(x, y, STR_PURCHASE_INFO_RUNNINGCOST, TC_FROMSTRING);
    00595   y += 10;
    00596
    00597   return y;
    00598 }
    00599
    00600 /* Draw aircraft specific details */
    00601 static int DrawAircraftPurchaseInfo(int x, int y, EngineID engine_number, const AircraftVehicleInfo *avi)
    00602 {
    00603   CargoID cargo;
    00604   const Engine *e = GetEngine(engine_number);
    00605
    00606   /* Purchase cost - Max speed */
    00607   SetDParam(0, e->GetCost());
    00608   SetDParam(1, e->GetDisplayMaxSpeed());
    00609   DrawString(x, y, STR_PURCHASE_INFO_COST_SPEED, TC_FROMSTRING);
    00610   y += 10;
    00611
    00612   /* Cargo capacity */
    00613   cargo = FindFirstRefittableCargo(engine_number);
    00614   if (cargo == CT_INVALID || cargo == CT_PASSENGERS) {
    00615     SetDParam(0, avi->passenger_capacity);
    00616     SetDParam(1, avi->mail_capacity);
    00617     DrawString(x, y, STR_PURCHASE_INFO_AIRCRAFT_CAPACITY, TC_FROMSTRING);
    00618   } else {
    00619     /* Note, if the default capacity is selected by the refit capacity
    00620     * callback, then the capacity shown is likely to be incorrect. */
    00621     SetDParam(0, cargo);
    00622     SetDParam(1, AircraftDefaultCargoCapacity(cargo, avi));
    00623     SetDParam(2, STR_9842_REFITTABLE);
    00624     DrawString(x, y, STR_PURCHASE_INFO_CAPACITY, TC_FROMSTRING);
    00625   }
    00626   y += 10;
    00627
    00628   /* Running cost */
    00629   SetDParam(0, e->GetRunningCost());
    00630   DrawString(x, y, STR_PURCHASE_INFO_RUNNINGCOST, TC_FROMSTRING);
    00631   y += 10;
    00632
    00633   return y;
    00634 }
    00635
    00643 int DrawVehiclePurchaseInfo(int x, int y, uint w, EngineID engine_number)
    00644 {
    00645   const Engine *e = GetEngine(engine_number);
    00646   YearMonthDay ymd;
    00647   ConvertDateToYMD(e->intro_date, &ymd);
    00648   bool refittable = IsEngineRefittable(engine_number);
    00649
    00650   switch (e->type) {
    00651     default: NOT_REACHED();
    00652     case VEH_TRAIN: {
    00653       const RailVehicleInfo *rvi = RailVehInfo(engine_number);
    00654       refittable &= GetEngineProperty(engine_number, 0x14, rvi->capacity) > 0;
    00655
    00656       if (rvi->railveh_type == RAILVEH_WAGON) {
    00657         y = DrawRailWagonPurchaseInfo(x, y, engine_number, rvi);
    00658       } else {
    00659         y = DrawRailEnginePurchaseInfo(x, y, engine_number, rvi);
    00660       }
    00661
    00662       /* Cargo type + capacity, or N/A */
    00663       int new_y = DrawCargoCapacityInfo(x, y, engine_number, VEH_TRAIN, refittable);
    00664
    00665       if (new_y == y) {
    00666         SetDParam(0, CT_INVALID);
    00667         SetDParam(2, STR_EMPTY);
    00668         DrawString(x, y, STR_PURCHASE_INFO_CAPACITY, TC_FROMSTRING);
    00669         y += 10;
    00670       } else {
    00671         y = new_y;
    00672       }
    00673       break;
    00674     }
    00675     case VEH_ROAD:
    00676       y = DrawRoadVehPurchaseInfo(x, y, engine_number);
    00677       break;
    00678     case VEH_SHIP:
    00679       y = DrawShipPurchaseInfo(x, y, engine_number, ShipVehInfo(engine_number));
    00680       break;
    00681     case VEH_AIRCRAFT:
    00682       y = DrawAircraftPurchaseInfo(x, y, engine_number, AircraftVehInfo(engine_number));
    00683       break;
    00684   }
    00685
    00686   /* Draw details, that applies to all types except rail wagons */
    00687   if (e->type != VEH_TRAIN || RailVehInfo(engine_number)->railveh_type != RAILVEH_WAGON) {
    00688     /* Design date - Life length */
    00689     SetDParam(0, ymd.year);
    00690     SetDParam(1, e->lifelength);
    00691     DrawString(x, y, STR_PURCHASE_INFO_DESIGNED_LIFE, TC_FROMSTRING);
    00692     y += 10;
    00693
    00694     /* Reliability */
    00695     SetDParam(0, e->reliability * 100 >> 16);
    00696     DrawString(x, y, STR_PURCHASE_INFO_RELIABILITY, TC_FROMSTRING);
    00697     y += 10;
    00698   }
    00699
    00700   /* Additional text from NewGRF */
    00701   y += ShowAdditionalText(x, y, w, engine_number);
    00702   if (refittable) y += ShowRefitOptionsList(x, y, w, engine_number);
    00703
    00704   return y;
    00705 }
    00706
    00707 static void DrawVehicleEngine(VehicleType type, int x, int y, EngineID engine, SpriteID pal)
    00708 {
    00709   switch (type) {
    00710     case VEH_TRAIN:    DrawTrainEngine(   x, y, engine, pal); break;
    00711     case VEH_ROAD:     DrawRoadVehEngine( x, y, engine, pal); break;
    00712     case VEH_SHIP:     DrawShipEngine(    x, y, engine, pal); break;
    00713     case VEH_AIRCRAFT: DrawAircraftEngine(x, y, engine, pal); break;
    00714     default: NOT_REACHED();
    00715   }
    00716 }
    00717
    00727 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)
    00728 {
    00729   byte step_size = GetVehicleListHeight(type);
    00730   byte x_offset = 0;
    00731   byte y_offset = 0;
    00732
    00733   assert(max <= eng_list->Length());
    00734
    00735   switch (type) {
    00736     case VEH_TRAIN:
    00737       x++; // train and road vehicles use the same offset, except trains are one more pixel to the right
    00738       /* Fallthough */
    00739     case VEH_ROAD:
    00740       x += 26;
    00741       x_offset = 30;
    00742       y += 2;
    00743       y_offset = 4;
    00744       break;
    00745     case VEH_SHIP:
    00746       x += 35;
    00747       x_offset = 40;
    00748       y += 7;
    00749       y_offset = 3;
    00750       break;
    00751     case VEH_AIRCRAFT:
    00752       x += 27;
    00753       x_offset = 33;
    00754       y += 7;
    00755       y_offset = 3;
    00756       break;
    00757     default: NOT_REACHED();
    00758   }
    00759
    00760   uint maxw = r - x - x_offset;
    00761
    00762   for (; min < max; min++, y += step_size) {
    00763     const EngineID engine = (*eng_list)[min];
    00764     /* Note: num_engines is only used in the autoreplace GUI, so it is correct to use _local_company here. */
    00765     const uint num_engines = GetGroupNumEngines(_local_company, selected_group, engine);
    00766
    00767     SetDParam(0, engine);
    00768     DrawStringTruncated(x + x_offset, y, STR_ENGINE_NAME, engine == selected_id ? TC_WHITE : TC_BLACK, maxw);
    00769     DrawVehicleEngine(type, x, y + y_offset, engine, (count_location != 0 && num_engines == 0) ? PALETTE_CRASH : GetEnginePalette(engine, _local_company));
    00770     if (count_location != 0) {
    00771       SetDParam(0, num_engines);
    00772       DrawStringRightAligned(count_location, y + (GetVehicleListHeight(type) == 14 ? 3 : 8), STR_TINY_BLACK, TC_FROMSTRING);
    00773     }
    00774   }
    00775 }
    00776
    00777
    00778 struct BuildVehicleWindow : Window {
    00779   VehicleType vehicle_type;
    00780   union {
    00781     RailTypeByte railtype;
    00782     AirportFTAClass::Flags flags;
    00783     RoadTypes roadtypes;
    00784   } filter;
    00785   bool descending_sort_order;
    00786   byte sort_criteria;
    00787   bool regenerate_list;
    00788   bool listview_mode;
    00789   EngineID sel_engine;
    00790   EngineID rename_engine;
    00791   GUIEngineList eng_list;
    00792
    00793   BuildVehicleWindow(const WindowDesc *desc, TileIndex tile, VehicleType type) : Window(desc, tile == INVALID_TILE ? (int)type : tile)
    00794   {
    00795     this->vehicle_type = type;
    00796     int vlh = GetVehicleListHeight(this->vehicle_type);
    00797
    00798     ResizeWindow(this, 0, vlh - 14);
    00799     this->resize.step_height = vlh;
    00800     this->vscroll.cap = 1;
    00801     this->widget[BUILD_VEHICLE_WIDGET_LIST].data = 0x101;
    00802
    00803     this->resize.width  = this->width;
    00804     this->resize.height = this->height;
    00805
    00806     this->owner = (tile != INVALID_TILE) ? GetTileOwner(tile) : _local_company;
    00807
    00808     this->sel_engine      = INVALID_ENGINE;
    00809     this->regenerate_list = false;
    00810
    00811     this->sort_criteria         = _last_sort_criteria[type];
    00812     this->descending_sort_order = _last_sort_order[type];
    00813
    00814     switch (type) {
    00815       default: NOT_REACHED();
    00816       case VEH_TRAIN:
    00817         this->filter.railtype = (tile == INVALID_TILE) ? RAILTYPE_END : GetRailType(tile);
    00818         break;
    00819       case VEH_ROAD:
    00820         this->filter.roadtypes = (tile == INVALID_TILE) ? ROADTYPES_ALL : GetRoadTypes(tile);
    00821       case VEH_SHIP:
    00822         break;
    00823       case VEH_AIRCRAFT:
    00824         this->filter.flags =
    00825           tile == INVALID_TILE ? AirportFTAClass::ALL : GetStationByTile(tile)->Airport()->flags;
    00826         break;
    00827     }
    00828     this->SetupWindowStrings(type);
    00829
    00830     this->listview_mode = (this->window_number <= VEH_END);
    00831     /* If we are just viewing the list of vehicles, we do not need the Build button.
    00832      * So we just hide it, and enlarge the Rename buton by the now vacant place. */
    00833     if (this->listview_mode) {
    00834       this->HideWidget(BUILD_VEHICLE_WIDGET_BUILD);
    00835       this->widget[BUILD_VEHICLE_WIDGET_RENAME].left = this->widget[BUILD_VEHICLE_WIDGET_BUILD].left;
    00836     } else {
    00837       /* Both are visible, adjust the size of each */
    00838       ResizeButtons(this, BUILD_VEHICLE_WIDGET_BUILD, BUILD_VEHICLE_WIDGET_RENAME);
    00839     }
    00840
    00841     this->GenerateBuildList(); // generate the list, since we need it in the next line
    00842     /* Select the first engine in the list as default when opening the window */
    00843     if (this->eng_list.Length() > 0) this->sel_engine = this->eng_list[0];
    00844
    00845     this->FindWindowPlacementAndResize(desc);
    00846   }
    00847
    00848   /* Setup widget strings to fit the different types of vehicles */
    00849   void SetupWindowStrings(VehicleType type)
    00850   {
    00851     switch (type) {
    00852       default: NOT_REACHED();
    00853
    00854       case VEH_TRAIN:
    00855         this->widget[BUILD_VEHICLE_WIDGET_CAPTION].data    = this->listview_mode ? STR_AVAILABLE_TRAINS : STR_JUST_STRING;
    00856         this->widget[BUILD_VEHICLE_WIDGET_LIST].tooltips   = STR_8843_TRAIN_VEHICLE_SELECTION;
    00857         this->widget[BUILD_VEHICLE_WIDGET_BUILD].data      = STR_881F_BUILD_VEHICLE;
    00858         this->widget[BUILD_VEHICLE_WIDGET_BUILD].tooltips  = STR_8844_BUILD_THE_HIGHLIGHTED_TRAIN;
    00859         this->widget[BUILD_VEHICLE_WIDGET_RENAME].data     = STR_8820_RENAME;
    00860         this->widget[BUILD_VEHICLE_WIDGET_RENAME].tooltips = STR_8845_RENAME_TRAIN_VEHICLE_TYPE;
    00861         break;
    00862
    00863       case VEH_ROAD:
    00864         this->widget[BUILD_VEHICLE_WIDGET_CAPTION].data    = this->listview_mode ? STR_AVAILABLE_ROAD_VEHICLES : STR_9006_NEW_ROAD_VEHICLES;
    00865         this->widget[BUILD_VEHICLE_WIDGET_LIST].tooltips   = STR_9026_ROAD_VEHICLE_SELECTION;
    00866         this->widget[BUILD_VEHICLE_WIDGET_BUILD].data      = STR_9007_BUILD_VEHICLE;
    00867         this->widget[BUILD_VEHICLE_WIDGET_BUILD].tooltips  = STR_9027_BUILD_THE_HIGHLIGHTED_ROAD;
    00868         this->widget[BUILD_VEHICLE_WIDGET_RENAME].data     = STR_9034_RENAME;
    00869         this->widget[BUILD_VEHICLE_WIDGET_RENAME].tooltips = STR_9035_RENAME_ROAD_VEHICLE_TYPE;
    00870         break;
    00871
    00872       case VEH_SHIP:
    00873         this->widget[BUILD_VEHICLE_WIDGET_CAPTION].data    = this->listview_mode ? STR_AVAILABLE_SHIPS : STR_9808_NEW_SHIPS;
    00874         this->widget[BUILD_VEHICLE_WIDGET_LIST].tooltips   = STR_9825_SHIP_SELECTION_LIST_CLICK;
    00875         this->widget[BUILD_VEHICLE_WIDGET_BUILD].data      = STR_9809_BUILD_SHIP;
    00876         this->widget[BUILD_VEHICLE_WIDGET_BUILD].tooltips  = STR_9826_BUILD_THE_HIGHLIGHTED_SHIP;
    00877         this->widget[BUILD_VEHICLE_WIDGET_RENAME].data     = STR_9836_RENAME;
    00878         this->widget[BUILD_VEHICLE_WIDGET_RENAME].tooltips = STR_9837_RENAME_SHIP_TYPE;
    00879         break;
    00880
    00881       case VEH_AIRCRAFT:
    00882         this->widget[BUILD_VEHICLE_WIDGET_CAPTION].data    = this->listview_mode ? STR_AVAILABLE_AIRCRAFT : STR_A005_NEW_AIRCRAFT;
    00883         this->widget[BUILD_VEHICLE_WIDGET_LIST].tooltips   = STR_A025_AIRCRAFT_SELECTION_LIST;
    00884         this->widget[BUILD_VEHICLE_WIDGET_BUILD].data      = STR_A006_BUILD_AIRCRAFT;
    00885         this->widget[BUILD_VEHICLE_WIDGET_BUILD].tooltips  = STR_A026_BUILD_THE_HIGHLIGHTED_AIRCRAFT;
    00886         this->widget[BUILD_VEHICLE_WIDGET_RENAME].data     = STR_A037_RENAME;
    00887         this->widget[BUILD_VEHICLE_WIDGET_RENAME].tooltips = STR_A038_RENAME_AIRCRAFT_TYPE;
    00888         break;
    00889     }
    00890   }
    00891
    00892   /* Figure out what train EngineIDs to put in the list */
    00893   void GenerateBuildTrainList()
    00894   {
    00895     EngineID sel_id = INVALID_ENGINE;
    00896     int num_engines = 0;
    00897     int num_wagons  = 0;
    00898
    00899     this->filter.railtype = (this->listview_mode) ? RAILTYPE_END : GetRailType(this->window_number);
    00900
    00901     this->eng_list.Clear();
    00902
    00903     /* Make list of all available train engines and wagons.
    00904     * Also check to see if the previously selected engine is still available,
    00905     * and if not, reset selection to INVALID_ENGINE. This could be the case
    00906     * when engines become obsolete and are removed */
    00907     const Engine *e;
    00908     FOR_ALL_ENGINES_OF_TYPE(e, VEH_TRAIN) {
    00909       EngineID eid = e->index;
    00910       const RailVehicleInfo *rvi = &e->u.rail;
    00911
    00912       if (this->filter.railtype != RAILTYPE_END && !HasPowerOnRail(rvi->railtype, this->filter.railtype)) continue;
    00913       if (!IsEngineBuildable(eid, VEH_TRAIN, _local_company)) continue;
    00914
    00915       *this->eng_list.Append() = eid;
    00916
    00917       if (rvi->railveh_type != RAILVEH_WAGON) {
    00918         num_engines++;
    00919       } else {
    00920         num_wagons++;
    00921       }
    00922
    00923       if (eid == this->sel_engine) sel_id = eid;
    00924     }
    00925
    00926     this->sel_engine = sel_id;
    00927
    00928     /* make engines first, and then wagons, sorted by ListPositionOfEngine() */
    00929     _internal_sort_order = false;
    00930     EngList_Sort(&this->eng_list, TrainEnginesThenWagonsSorter);
    00931
    00932     /* and then sort engines */
    00933     _internal_sort_order = this->descending_sort_order;
    00934     EngList_SortPartial(&this->eng_list, _sorter[0][this->sort_criteria], 0, num_engines);
    00935
    00936     /* and finally sort wagons */
    00937     EngList_SortPartial(&this->eng_list, _sorter[0][this->sort_criteria], num_engines, num_wagons);
    00938   }
    00939
    00940   /* Figure out what road vehicle EngineIDs to put in the list */
    00941   void GenerateBuildRoadVehList()
    00942   {
    00943     EngineID sel_id = INVALID_ENGINE;
    00944
    00945     this->eng_list.Clear();
    00946
    00947     const Engine *e;
    00948     FOR_ALL_ENGINES_OF_TYPE(e, VEH_ROAD) {
    00949       EngineID eid = e->index;
    00950       if (!IsEngineBuildable(eid, VEH_ROAD, _local_company)) continue;
    00951       if (!HasBit(this->filter.roadtypes, HasBit(EngInfo(eid)->misc_flags, EF_ROAD_TRAM) ? ROADTYPE_TRAM : ROADTYPE_ROAD)) continue;
    00952       *this->eng_list.Append() = eid;
    00953
    00954       if (eid == this->sel_engine) sel_id = eid;
    00955     }
    00956     this->sel_engine = sel_id;
    00957   }
    00958
    00959   /* Figure out what ship EngineIDs to put in the list */
    00960   void GenerateBuildShipList()
    00961   {
    00962     EngineID sel_id = INVALID_ENGINE;
    00963     this->eng_list.Clear();
    00964
    00965     const Engine *e;
    00966     FOR_ALL_ENGINES_OF_TYPE(e, VEH_SHIP) {
    00967       EngineID eid = e->index;
    00968       if (!IsEngineBuildable(eid, VEH_SHIP, _local_company)) continue;
    00969       *this->eng_list.Append() = eid;
    00970
    00971       if (eid == this->sel_engine) sel_id = eid;
    00972     }
    00973     this->sel_engine = sel_id;
    00974   }
    00975
    00976   /* Figure out what aircraft EngineIDs to put in the list */
    00977   void GenerateBuildAircraftList()
    00978   {
    00979     EngineID sel_id = INVALID_ENGINE;
    00980
    00981     this->eng_list.Clear();
    00982
    00983     const Station *st = this->listview_mode ? NULL : GetStationByTile(this->window_number);
    00984
    00985     /* Make list of all available planes.
    00986     * Also check to see if the previously selected plane is still available,
    00987     * and if not, reset selection to INVALID_ENGINE. This could be the case
    00988     * when planes become obsolete and are removed */
    00989     const Engine *e;
    00990     FOR_ALL_ENGINES_OF_TYPE(e, VEH_AIRCRAFT) {
    00991       EngineID eid = e->index;
    00992       if (!IsEngineBuildable(eid, VEH_AIRCRAFT, _local_company)) continue;
    00993       /* First VEH_END window_numbers are fake to allow a window open for all different types at once */
    00994       if (!this->listview_mode && !CanVehicleUseStation(eid, st)) continue;
    00995
    00996       *this->eng_list.Append() = eid;
    00997       if (eid == this->sel_engine) sel_id = eid;
    00998     }
    00999
    01000     this->sel_engine = sel_id;
    01001   }
    01002
    01003   /* Generate the list of vehicles */
    01004   void GenerateBuildList()
    01005   {
    01006     switch (this->vehicle_type) {
    01007       default: NOT_REACHED();
    01008       case VEH_TRAIN:
    01009         this->GenerateBuildTrainList();
    01010         return; // trains should not reach the last sorting
    01011       case VEH_ROAD:
    01012         this->GenerateBuildRoadVehList();
    01013         break;
    01014       case VEH_SHIP:
    01015         this->GenerateBuildShipList();
    01016         break;
    01017       case VEH_AIRCRAFT:
    01018         this->GenerateBuildAircraftList();
    01019         break;
    01020     }
    01021     _internal_sort_order = this->descending_sort_order;
    01022     EngList_Sort(&this->eng_list, _sorter[this->vehicle_type][this->sort_criteria]);
    01023   }
    01024
    01025   void OnClick(Point pt, int widget)
    01026   {
    01027     switch (widget) {
    01028       case BUILD_VEHICLE_WIDGET_SORT_ASSENDING_DESCENDING:
    01029         this->descending_sort_order ^= true;
    01030         _last_sort_order[this->vehicle_type] = this->descending_sort_order;
    01031         this->regenerate_list = true;
    01032         this->SetDirty();
    01033         break;
    01034
    01035       case BUILD_VEHICLE_WIDGET_LIST: {
    01036         uint i = (pt.y - this->widget[BUILD_VEHICLE_WIDGET_LIST].top) / GetVehicleListHeight(this->vehicle_type) + this->vscroll.pos;
    01037         size_t num_items = this->eng_list.Length();
    01038         this->sel_engine = (i < num_items) ? this->eng_list[i] : INVALID_ENGINE;
    01039         this->SetDirty();
    01040         break;
    01041       }
    01042
    01043       case BUILD_VEHICLE_WIDGET_SORT_DROPDOWN: // Select sorting criteria dropdown menu
    01044         ShowDropDownMenu(this, _sort_listing[this->vehicle_type], this->sort_criteria, BUILD_VEHICLE_WIDGET_SORT_DROPDOWN, 0, 0);
    01045         break;
    01046
    01047       case BUILD_VEHICLE_WIDGET_BUILD: {
    01048         EngineID sel_eng = this->sel_engine;
    01049         if (sel_eng != INVALID_ENGINE) {
    01050           switch (this->vehicle_type) {
    01051             default: NOT_REACHED();
    01052             case VEH_TRAIN:
    01053               DoCommandP(this->window_number, sel_eng, 0,
    01054                   CMD_BUILD_RAIL_VEHICLE | CMD_MSG(STR_882B_CAN_T_BUILD_RAILROAD_VEHICLE),
    01055                   (RailVehInfo(sel_eng)->railveh_type == RAILVEH_WAGON) ? CcBuildWagon : CcBuildLoco);
    01056               break;
    01057             case VEH_ROAD:
    01058               DoCommandP(this->window_number, sel_eng, 0, CMD_BUILD_ROAD_VEH | CMD_MSG(STR_9009_CAN_T_BUILD_ROAD_VEHICLE), CcBuildRoadVeh);
    01059               break;
    01060             case VEH_SHIP:
    01061               DoCommandP(this->window_number, sel_eng, 0, CMD_BUILD_SHIP | CMD_MSG(STR_980D_CAN_T_BUILD_SHIP), CcBuildShip);
    01062               break;
    01063             case VEH_AIRCRAFT:
    01064               DoCommandP(this->window_number, sel_eng, 0, CMD_BUILD_AIRCRAFT | CMD_MSG(STR_A008_CAN_T_BUILD_AIRCRAFT), CcBuildAircraft);
    01065               break;
    01066           }
    01067         }
    01068         break;
    01069       }
    01070
    01071       case BUILD_VEHICLE_WIDGET_RENAME: {
    01072         EngineID sel_eng = this->sel_engine;
    01073         if (sel_eng != INVALID_ENGINE) {
    01074           StringID str = STR_NULL;
    01075
    01076           this->rename_engine = sel_eng;
    01077           switch (this->vehicle_type) {
    01078             default: NOT_REACHED();
    01079             case VEH_TRAIN:    str = STR_886A_RENAME_TRAIN_VEHICLE_TYPE; break;
    01080             case VEH_ROAD:     str = STR_9036_RENAME_ROAD_VEHICLE_TYPE;  break;
    01081             case VEH_SHIP:     str = STR_9838_RENAME_SHIP_TYPE;          break;
    01082             case VEH_AIRCRAFT: str = STR_A039_RENAME_AIRCRAFT_TYPE;      break;
    01083           }
    01084           SetDParam(0, sel_eng);
    01085           ShowQueryString(STR_ENGINE_NAME, str, MAX_LENGTH_ENGINE_NAME_BYTES, MAX_LENGTH_ENGINE_NAME_PIXELS, this, CS_ALPHANUMERAL, QSF_ENABLE_DEFAULT);
    01086         }
    01087         break;
    01088       }
    01089     }
    01090   }
    01091
    01092   virtual void OnInvalidateData(int data)
    01093   {
    01094     this->regenerate_list = true;
    01095   }
    01096
    01097   virtual void OnPaint()
    01098   {
    01099     if (this->regenerate_list) {
    01100       this->regenerate_list = false;
    01101       this->GenerateBuildList();
    01102     }
    01103
    01104     uint max = min(this->vscroll.pos + this->vscroll.cap, this->eng_list.Length());
    01105
    01106     SetVScrollCount(this, this->eng_list.Length());
    01107     if (this->vehicle_type == VEH_TRAIN) {
    01108       if (this->filter.railtype == RAILTYPE_END) {
    01109         SetDParam(0, STR_ALL_AVAIL_RAIL_VEHICLES);
    01110       } else {
    01111         const RailtypeInfo *rti = GetRailTypeInfo(this->filter.railtype);
    01112         SetDParam(0, rti->strings.build_caption);
    01113       }
    01114     }
    01115
    01116     /* Set text of sort by dropdown */
    01117     this->widget[BUILD_VEHICLE_WIDGET_SORT_DROPDOWN].data = _sort_listing[this->vehicle_type][this->sort_criteria];
    01118
    01119     this->DrawWidgets();
    01120
    01121     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);
    01122
    01123     if (this->sel_engine != INVALID_ENGINE) {
    01124       const Widget *wi = &this->widget[BUILD_VEHICLE_WIDGET_PANEL];
    01125       int text_end = DrawVehiclePurchaseInfo(2, wi->top + 1, wi->right - wi->left - 2, this->sel_engine);
    01126
    01127       if (text_end > wi->bottom) {
    01128         this->SetDirty();
    01129         ResizeWindowForWidget(this, BUILD_VEHICLE_WIDGET_PANEL, 0, text_end - wi->bottom);
    01130         this->SetDirty();
    01131       }
    01132     }
    01133
    01134     this->DrawSortButtonState(BUILD_VEHICLE_WIDGET_SORT_ASSENDING_DESCENDING, this->descending_sort_order ? SBS_DOWN : SBS_UP);
    01135   }
    01136
    01137   virtual void OnDoubleClick(Point pt, int widget)
    01138   {
    01139     if (widget == BUILD_VEHICLE_WIDGET_LIST) {
    01140       /* When double clicking, we want to buy a vehicle */
    01141       this->OnClick(pt, BUILD_VEHICLE_WIDGET_BUILD);
    01142     }
    01143   }
    01144
    01145   virtual void OnQueryTextFinished(char *str)
    01146   {
    01147     if (str == NULL) return;
    01148
    01149     StringID err_str = STR_NULL;
    01150     switch (this->vehicle_type) {
    01151       default: NOT_REACHED();
    01152       case VEH_TRAIN:    err_str = STR_886B_CAN_T_RENAME_TRAIN_VEHICLE; break;
    01153       case VEH_ROAD:     err_str = STR_9037_CAN_T_RENAME_ROAD_VEHICLE;  break;
    01154       case VEH_SHIP:     err_str = STR_9839_CAN_T_RENAME_SHIP_TYPE;     break;
    01155       case VEH_AIRCRAFT: err_str = STR_A03A_CAN_T_RENAME_AIRCRAFT_TYPE; break;
    01156     }
    01157     DoCommandP(0, this->rename_engine, 0, CMD_RENAME_ENGINE | CMD_MSG(err_str), NULL, str);
    01158   }
    01159
    01160   virtual void OnDropdownSelect(int widget, int index)
    01161   {
    01162     if (this->sort_criteria != index) {
    01163       this->sort_criteria = index;
    01164       _last_sort_criteria[this->vehicle_type] = this->sort_criteria;
    01165       this->regenerate_list = true;
    01166     }
    01167     this->SetDirty();
    01168   }
    01169
    01170   virtual void OnResize(Point new_size, Point delta)
    01171   {
    01172     if (delta.x != 0 && !this->listview_mode) {
    01173       ResizeButtons(this, BUILD_VEHICLE_WIDGET_BUILD, BUILD_VEHICLE_WIDGET_RENAME);
    01174     }
    01175     if (delta.y == 0) return;
    01176
    01177     this->vscroll.cap += delta.y / (int)GetVehicleListHeight(this->vehicle_type);
    01178     this->widget[BUILD_VEHICLE_WIDGET_LIST].data = (this->vscroll.cap << 8) + 1;
    01179   }
    01180 };
    01181
    01182 static const WindowDesc _build_vehicle_desc = {
    01183   WDP_AUTO, WDP_AUTO, 240, 174, 240, 256,
    01184   WC_BUILD_VEHICLE, WC_NONE,
    01185   WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS | WDF_RESIZABLE | WDF_CONSTRUCTION,
    01186   _build_vehicle_widgets,
    01187 };
    01188
    01189 void ShowBuildVehicleWindow(TileIndex tile, VehicleType type)
    01190 {
    01191   /* We want to be able to open both Available Train as Available Ships,
    01192    *  so if tile == INVALID_TILE (Available XXX Window), use 'type' as unique number.
    01193    *  As it always is a low value, it won't collide with any real tile
    01194    *  number. */
    01195   uint num = (tile == INVALID_TILE) ? (int)type : tile;
    01196
    01197   assert(IsCompanyBuildableVehicleType(type));
    01198
    01199   DeleteWindowById(WC_BUILD_VEHICLE, num);
    01200
    01201   new BuildVehicleWindow(&_build_vehicle_desc, tile, type);
    01202 }