# autoreplace_gui.cpp

Go to the documentation of this file.

    00001 /* $Id$ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
    00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
    00008  */
    00009
    00012 #include "stdafx.h"
    00013 #include "command_func.h"
    00014 #include "vehicle_gui.h"
    00015 #include "newgrf_engine.h"
    00016 #include "rail.h"
    00017 #include "strings_func.h"
    00018 #include "window_func.h"
    00019 #include "autoreplace_func.h"
    00020 #include "company_func.h"
    00021 #include "engine_base.h"
    00022 #include "window_gui.h"
    00023 #include "engine_gui.h"
    00024 #include "settings_func.h"
    00025 #include "core/geometry_func.hpp"
    00026 #include "rail_gui.h"
    00027 #include "widgets/dropdown_func.h"
    00028
    00029 #include "widgets/autoreplace_widget.h"
    00030
    00031
    00032 uint GetEngineListHeight(VehicleType type);
    00033 void DrawEngineList(VehicleType type, int x, int r, int y, const GUIEngineList *eng_list, uint16 min, uint16 max, EngineID selected_id, bool show_count, GroupID selected_group);
    00034
    00035 static int CDECL EngineNumberSorter(const EngineID *a, const EngineID *b)
    00036 {
    00037   int r = Engine::Get(*a)->list_position - Engine::Get(*b)->list_position;
    00038
    00039   return r;
    00040 }
    00041
    00051 void InvalidateAutoreplaceWindow(EngineID e, GroupID id_g)
    00052 {
    00053   if (GetGroupNumEngines(_local_company, id_g, e) == 0 || GetGroupNumEngines(_local_company, ALL_GROUP, e) == 0) {
    00054     /* We don't have any of this engine type.
    00055      * Either we just sold the last one, we build a new one or we stopped replacing it.
    00056      * In all cases, we need to update the left list */
    00057     InvalidateWindowData(WC_REPLACE_VEHICLE, Engine::Get(e)->type, 1);
    00058   }
    00059 }
    00060
    00065 void AddRemoveEngineFromAutoreplaceAndBuildWindows(VehicleType type)
    00066 {
    00067   InvalidateWindowData(WC_REPLACE_VEHICLE, type, 0); // Update the autoreplace window
    00068   InvalidateWindowClassesData(WC_BUILD_VEHICLE); // The build windows needs updating as well
    00069 }
    00070
    00071 static const StringID _start_replace_dropdown[] = {
    00072   STR_REPLACE_VEHICLES_NOW,
    00073   STR_REPLACE_VEHICLES_WHEN_OLD,
    00074   INVALID_STRING_ID
    00075 };
    00076
    00080 class ReplaceVehicleWindow : public Window {
    00081   EngineID sel_engine[2];
    00082   GUIEngineList engines[2];
    00083   bool replace_engines;
    00084   bool reset_sel_engine;
    00085   GroupID sel_group;
    00086   int details_height;
    00087   RailType sel_railtype;
    00088   Scrollbar *vscroll[2];
    00089
    00097   bool GenerateReplaceRailList(EngineID e, bool draw_left, bool show_engines)
    00098   {
    00099     const RailVehicleInfo *rvi = RailVehInfo(e);
    00100
    00101     /* Ensure that the wagon/engine selection fits the engine. */
    00102     if ((rvi->railveh_type == RAILVEH_WAGON) == show_engines) return false;
    00103
    00104     if (draw_left && show_engines) {
    00105       /* Ensure that the railtype is specific to the selected one */
    00106       if (rvi->railtype != this->sel_railtype) return false;
    00107     }
    00108     return true;
    00109   }
    00110
    00111
    00116   void GenerateReplaceVehList(bool draw_left)
    00117   {
    00118     EngineID selected_engine = INVALID_ENGINE;
    00119     VehicleType type = (VehicleType)this->window_number;
    00120     byte side = draw_left ? 0 : 1;
    00121
    00122     GUIEngineList *list = &this->engines[side];
    00123     list->Clear();
    00124
    00125     const Engine *e;
    00126     FOR_ALL_ENGINES_OF_TYPE(e, type) {
    00127       EngineID eid = e->index;
    00128       if (type == VEH_TRAIN && !this->GenerateReplaceRailList(eid, draw_left, this->replace_engines)) continue; // special rules for trains
    00129
    00130       if (draw_left) {
    00131         const uint num_engines = GetGroupNumEngines(_local_company, this->sel_group, eid);
    00132
    00133         /* Skip drawing the engines we don't have any of and haven't set for replacement */
    00134         if (num_engines == 0 && EngineReplacementForCompany(Company::Get(_local_company), eid, this->sel_group) == INVALID_ENGINE) continue;
    00135       } else {
    00136         if (!CheckAutoreplaceValidity(this->sel_engine[0], eid, _local_company)) continue;
    00137       }
    00138
    00139       *list->Append() = eid;
    00140       if (eid == this->sel_engine[side]) selected_engine = eid; // The selected engine is still in the list
    00141     }
    00142     this->sel_engine[side] = selected_engine; // update which engine we selected (the same or none, if it's not in the list anymore)
    00143     EngList_Sort(list, &EngineNumberSorter);
    00144   }
    00145
    00147   void GenerateLists()
    00148   {
    00149     EngineID e = this->sel_engine[0];
    00150
    00151     if (this->engines[0].NeedRebuild()) {
    00152       /* We need to rebuild the left engines list */
    00153       this->GenerateReplaceVehList(true);
    00154       this->vscroll[0]->SetCount(this->engines[0].Length());
    00155       if (this->reset_sel_engine && this->sel_engine[0] == INVALID_ENGINE && this->engines[0].Length() != 0) {
    00156         this->sel_engine[0] = this->engines[0][0];
    00157       }
    00158     }
    00159
    00160     if (this->engines[1].NeedRebuild() || e != this->sel_engine[0]) {
    00161       /* Either we got a request to rebuild the right engines list, or the left engines list selected a different engine */
    00162       if (this->sel_engine[0] == INVALID_ENGINE) {
    00163         /* Always empty the right engines list when nothing is selected in the left engines list */
    00164         this->engines[1].Clear();
    00165         this->sel_engine[1] = INVALID_ENGINE;
    00166       } else {
    00167         this->GenerateReplaceVehList(false);
    00168         this->vscroll[1]->SetCount(this->engines[1].Length());
    00169         if (this->reset_sel_engine && this->sel_engine[1] == INVALID_ENGINE && this->engines[1].Length() != 0) {
    00170           this->sel_engine[1] = this->engines[1][0];
    00171         }
    00172       }
    00173     }
    00174     /* Reset the flags about needed updates */
    00175     this->engines[0].RebuildDone();
    00176     this->engines[1].RebuildDone();
    00177     this->reset_sel_engine = false;
    00178   }
    00179
    00184   void ReplaceClick_StartReplace(bool replace_when_old)
    00185   {
    00186     EngineID veh_from = this->sel_engine[0];
    00187     EngineID veh_to = this->sel_engine[1];
    00188     DoCommandP(0, (replace_when_old ? 1 : 0) | (this->sel_group << 16), veh_from + (veh_to << 16), CMD_SET_AUTOREPLACE);
    00189     this->SetDirty();
    00190   }
    00191
    00192 public:
    00193   ReplaceVehicleWindow(const WindowDesc *desc, VehicleType vehicletype, GroupID id_g) : Window()
    00194   {
    00195     if (vehicletype == VEH_TRAIN) {
    00196       /* For rail vehicles find the most used vehicle type, which is usually
    00197        * better than 'just' the first/previous vehicle type. */
    00198       uint type_count[RAILTYPE_END];
    00199       memset(type_count, 0, sizeof(type_count));
    00200
    00201       const Engine *e;
    00202       FOR_ALL_ENGINES_OF_TYPE(e, VEH_TRAIN) {
    00203         if (e->u.rail.railveh_type == RAILVEH_WAGON) continue;
    00204         type_count[e->u.rail.railtype] += GetGroupNumEngines(_local_company, id_g, e->index);
    00205       }
    00206
    00207       this->sel_railtype = RAILTYPE_BEGIN;
    00208       for (RailType rt = RAILTYPE_BEGIN; rt < RAILTYPE_END; rt++) {
    00209         if (type_count[this->sel_railtype] < type_count[rt]) this->sel_railtype = rt;
    00210       }
    00211     }
    00212
    00213     this->replace_engines  = true; // start with locomotives (all other vehicles will not read this bool)
    00214     this->engines[0].ForceRebuild();
    00215     this->engines[1].ForceRebuild();
    00216     this->reset_sel_engine = true;
    00217     this->details_height   = ((vehicletype == VEH_TRAIN) ? 10 : 9) * FONT_HEIGHT_NORMAL + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
    00218     this->sel_engine[0] = INVALID_ENGINE;
    00219     this->sel_engine[1] = INVALID_ENGINE;
    00220
    00221     this->CreateNestedTree(desc);
    00222     this->vscroll[0] = this->GetScrollbar(WID_RV_LEFT_SCROLLBAR);
    00223     this->vscroll[1] = this->GetScrollbar(WID_RV_RIGHT_SCROLLBAR);
    00224     this->FinishInitNested(desc, vehicletype);
    00225
    00226     this->owner = _local_company;
    00227     this->sel_group = id_g;
    00228   }
    00229
    00230   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
    00231   {
    00232     switch (widget) {
    00233       case WID_RV_LEFT_MATRIX:
    00234       case WID_RV_RIGHT_MATRIX:
    00235         resize->height = GetEngineListHeight((VehicleType)this->window_number);
    00236         size->height = (this->window_number <= VEH_ROAD ? 8 : 4) * resize->height;
    00237         break;
    00238
    00239       case WID_RV_LEFT_DETAILS:
    00240       case WID_RV_RIGHT_DETAILS:
    00241         size->height = this->details_height;
    00242         break;
    00243
    00244       case WID_RV_TRAIN_WAGONREMOVE_TOGGLE: {
    00245         StringID str = this->GetWidget<NWidgetCore>(widget)->widget_data;
    00246         SetDParam(0, STR_CONFIG_SETTING_ON);
    00247         Dimension d = GetStringBoundingBox(str);
    00248         SetDParam(0, STR_CONFIG_SETTING_OFF);
    00249         d = maxdim(d, GetStringBoundingBox(str));
    00250         d.width += padding.width;
    00251         d.height += padding.height;
    00252         *size = maxdim(*size, d);
    00253         break;
    00254       }
    00255
    00256       case WID_RV_TRAIN_ENGINEWAGON_TOGGLE: {
    00257         StringID str = this->GetWidget<NWidgetCore>(widget)->widget_data;
    00258         SetDParam(0, STR_REPLACE_ENGINES);
    00259         Dimension d = GetStringBoundingBox(str);
    00260         SetDParam(0, STR_REPLACE_WAGONS);
    00261         d = maxdim(d, GetStringBoundingBox(str));
    00262         d.width += padding.width;
    00263         d.height += padding.height;
    00264         *size = maxdim(*size, d);
    00265         break;
    00266       }
    00267
    00268       case WID_RV_INFO_TAB: {
    00269         SetDParam(0, STR_REPLACE_NOT_REPLACING);
    00270         Dimension d = GetStringBoundingBox(STR_BLACK_STRING);
    00271         SetDParam(0, STR_REPLACE_NOT_REPLACING_VEHICLE_SELECTED);
    00272         d = maxdim(d, GetStringBoundingBox(STR_BLACK_STRING));
    00273         d.width += WD_FRAMETEXT_LEFT +  WD_FRAMETEXT_RIGHT;
    00274         d.height += WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
    00275         *size = maxdim(*size, d);
    00276         break;
    00277       }
    00278
    00279       case WID_RV_TRAIN_RAILTYPE_DROPDOWN: {
    00280         Dimension d = {0, 0};
    00281         for (RailType rt = RAILTYPE_BEGIN; rt != RAILTYPE_END; rt++) {
    00282           const RailtypeInfo *rti = GetRailTypeInfo(rt);
    00283           /* Skip rail type if it has no label */
    00284           if (rti->label == 0) continue;
    00285           d = maxdim(d, GetStringBoundingBox(rti->strings.replace_text));
    00286         }
    00287         d.width += padding.width;
    00288         d.height += padding.height;
    00289         *size = maxdim(*size, d);
    00290         break;
    00291       }
    00292
    00293       case WID_RV_START_REPLACE: {
    00294         Dimension d = GetStringBoundingBox(STR_REPLACE_VEHICLES_START);
    00295         for (int i = 0; _start_replace_dropdown[i] != INVALID_STRING_ID; i++) {
    00296           d = maxdim(d, GetStringBoundingBox(_start_replace_dropdown[i]));
    00297         }
    00298         d.width += padding.width;
    00299         d.height += padding.height;
    00300         *size = maxdim(*size, d);
    00301         break;
    00302       }
    00303     }
    00304   }
    00305
    00306   virtual void SetStringParameters(int widget) const
    00307   {
    00308     switch (widget) {
    00309       case WID_RV_CAPTION:
    00310         SetDParam(0, STR_REPLACE_VEHICLE_TRAIN + this->window_number);
    00311         switch (this->sel_group) {
    00312           case ALL_GROUP:
    00313             SetDParam(1, STR_GROUP_ALL_TRAINS + this->window_number);
    00314             break;
    00315
    00316           case DEFAULT_GROUP:
    00317             SetDParam(1, STR_GROUP_DEFAULT_TRAINS + this->window_number);
    00318             break;
    00319
    00320           default:
    00321             SetDParam(1, STR_GROUP_NAME);
    00322             SetDParam(2, sel_group);
    00323             break;
    00324         }
    00325         break;
    00326
    00327       case WID_RV_TRAIN_WAGONREMOVE_TOGGLE: {
    00328         const Company *c = Company::Get(_local_company);
    00329         SetDParam(0, c->settings.renew_keep_length ? STR_CONFIG_SETTING_ON : STR_CONFIG_SETTING_OFF);
    00330         break;
    00331       }
    00332
    00333       case WID_RV_TRAIN_ENGINEWAGON_TOGGLE:
    00334         SetDParam(0, this->replace_engines ? STR_REPLACE_ENGINES : STR_REPLACE_WAGONS);
    00335         break;
    00336     }
    00337   }
    00338
    00339   virtual void DrawWidget(const Rect &r, int widget) const
    00340   {
    00341     switch (widget) {
    00342       case WID_RV_INFO_TAB: {
    00343         const Company *c = Company::Get(_local_company);
    00344         if (this->sel_engine[0] != INVALID_ENGINE) {
    00345           if (!EngineHasReplacementForCompany(c, this->sel_engine[0], this->sel_group)) {
    00346             SetDParam(0, STR_REPLACE_NOT_REPLACING);
    00347           } else {
    00348             bool when_old = false;
    00349             EngineID e = EngineReplacementForCompany(c, this->sel_engine[0], this->sel_group, &when_old);
    00350             SetDParam(0, when_old ? STR_REPLACE_REPLACING_WHEN_OLD : STR_ENGINE_NAME);
    00351             SetDParam(1, e);
    00352           }
    00353         } else {
    00354           SetDParam(0, STR_REPLACE_NOT_REPLACING_VEHICLE_SELECTED);
    00355         }
    00356
    00357         DrawString(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, r.top + WD_FRAMERECT_TOP, STR_BLACK_STRING, TC_FROMSTRING, SA_HOR_CENTER);
    00358         break;
    00359       }
    00360
    00361       case WID_RV_LEFT_MATRIX:
    00362       case WID_RV_RIGHT_MATRIX: {
    00363         int side = (widget == WID_RV_LEFT_MATRIX) ? 0 : 1;
    00364         EngineID start  = this->vscroll[side]->GetPosition(); // what is the offset for the start (scrolling)
    00365         EngineID end    = min(this->vscroll[side]->GetCapacity() + start, this->engines[side].Length());
    00366
    00367         /* Do the actual drawing */
    00368         DrawEngineList((VehicleType)this->window_number, r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + WD_FRAMERECT_TOP,
    00369             &this->engines[side], start, end, this->sel_engine[side], side == 0, this->sel_group);
    00370         break;
    00371       }
    00372     }
    00373   }
    00374
    00375   virtual void OnPaint()
    00376   {
    00377     if (this->engines[0].NeedRebuild() || this->engines[1].NeedRebuild()) this->GenerateLists();
    00378
    00379     Company *c = Company::Get(_local_company);
    00380
    00381     /* Disable the "Start Replacing" button if:
    00382      *    Either engines list is empty
    00383      * or The selected replacement engine has a replacement (to prevent loops). */
    00384     this->SetWidgetDisabledState(WID_RV_START_REPLACE,
    00385                     this->sel_engine[0] == INVALID_ENGINE ||
    00386                     this->sel_engine[1] == INVALID_ENGINE ||
    00387                     EngineReplacementForCompany(c, this->sel_engine[1], this->sel_group) != INVALID_ENGINE);
    00388
    00389     /* Disable the "Stop Replacing" button if:
    00390      *   The left engines list (existing vehicle) is empty
    00391      *   or The selected vehicle has no replacement set up */
    00392     this->SetWidgetDisabledState(WID_RV_STOP_REPLACE,
    00393                     this->sel_engine[0] == INVALID_ENGINE ||
    00394                     !EngineHasReplacementForCompany(c, this->sel_engine[0], this->sel_group));
    00395
    00396     if (this->window_number == VEH_TRAIN) {
    00397       /* sets the colour of that art thing */
    00398       this->GetWidget<NWidgetCore>(WID_RV_TRAIN_FLUFF_LEFT)->colour  = _company_colours[_local_company];
    00399       this->GetWidget<NWidgetCore>(WID_RV_TRAIN_FLUFF_RIGHT)->colour = _company_colours[_local_company];
    00400
    00401       /* Show the selected railtype in the pulldown menu */
    00402       this->GetWidget<NWidgetCore>(WID_RV_TRAIN_RAILTYPE_DROPDOWN)->widget_data = GetRailTypeInfo(sel_railtype)->strings.replace_text;
    00403     }
    00404
    00405     this->DrawWidgets();
    00406
    00407     if (!this->IsShaded()) {
    00408       int needed_height = this->details_height;
    00409       /* Draw details panels. */
    00410       for (int side = 0; side < 2; side++) {
    00411         if (this->sel_engine[side] != INVALID_ENGINE) {
    00412           NWidgetBase *nwi = this->GetWidget<NWidgetBase>(side == 0 ? WID_RV_LEFT_DETAILS : WID_RV_RIGHT_DETAILS);
    00413           int text_end = DrawVehiclePurchaseInfo(nwi->pos_x + WD_FRAMETEXT_LEFT, nwi->pos_x + nwi->current_x - WD_FRAMETEXT_RIGHT,
    00414               nwi->pos_y + WD_FRAMERECT_TOP, this->sel_engine[side]);
    00415           needed_height = max(needed_height, text_end - (int)nwi->pos_y + WD_FRAMERECT_BOTTOM);
    00416         }
    00417       }
    00418       if (needed_height != this->details_height) { // Details window are not high enough, enlarge them.
    00419         this->details_height = needed_height;
    00420         this->ReInit();
    00421         return;
    00422       }
    00423     }
    00424   }
    00425
    00426   virtual void OnClick(Point pt, int widget, int click_count)
    00427   {
    00428     switch (widget) {
    00429       case WID_RV_TRAIN_ENGINEWAGON_TOGGLE:
    00430         this->replace_engines  = !(this->replace_engines);
    00431         this->engines[0].ForceRebuild();
    00432         this->reset_sel_engine = true;
    00433         this->SetDirty();
    00434         break;
    00435
    00436       case WID_RV_TRAIN_RAILTYPE_DROPDOWN: // Railtype selection dropdown menu
    00437         ShowDropDownList(this, GetRailTypeDropDownList(true), sel_railtype, WID_RV_TRAIN_RAILTYPE_DROPDOWN);
    00438         break;
    00439
    00440       case WID_RV_TRAIN_WAGONREMOVE_TOGGLE: // toggle renew_keep_length
    00441         DoCommandP(0, GetCompanySettingIndex("company.renew_keep_length"), Company::Get(_local_company)->settings.renew_keep_length ? 0 : 1, CMD_CHANGE_COMPANY_SETTING);
    00442         break;
    00443
    00444       case WID_RV_START_REPLACE: { // Start replacing
    00445         if (this->GetWidget<NWidgetLeaf>(widget)->ButtonHit(pt)) {
    00446           this->HandleButtonClick(WID_RV_START_REPLACE);
    00447           ReplaceClick_StartReplace(false);
    00448         } else {
    00449           bool replacment_when_old = EngineHasReplacementWhenOldForCompany(Company::Get(_local_company), this->sel_engine[0], this->sel_group);
    00450           ShowDropDownMenu(this, _start_replace_dropdown, replacment_when_old ? 1 : 0, WID_RV_START_REPLACE, !this->replace_engines ? 1 << 1 : 0, 0);
    00451         }
    00452         break;
    00453       }
    00454
    00455       case WID_RV_STOP_REPLACE: { // Stop replacing
    00456         EngineID veh_from = this->sel_engine[0];
    00457         DoCommandP(0, this->sel_group << 16, veh_from + (INVALID_ENGINE << 16), CMD_SET_AUTOREPLACE);
    00458         this->SetDirty();
    00459         break;
    00460       }
    00461
    00462       case WID_RV_LEFT_MATRIX:
    00463       case WID_RV_RIGHT_MATRIX: {
    00464         byte click_side;
    00465         if (widget == WID_RV_LEFT_MATRIX) {
    00466           click_side = 0;
    00467         } else {
    00468           click_side = 1;
    00469         }
    00470         uint i = this->vscroll[click_side]->GetScrolledRowFromWidget(pt.y, this, widget);
    00471         size_t engine_count = this->engines[click_side].Length();
    00472
    00473         EngineID e = engine_count > i ? this->engines[click_side][i] : INVALID_ENGINE;
    00474         if (e == this->sel_engine[click_side]) break; // we clicked the one we already selected
    00475         this->sel_engine[click_side] = e;
    00476         if (click_side == 0) {
    00477           this->engines[1].ForceRebuild();
    00478           this->reset_sel_engine = true;
    00479         }
    00480         this->SetDirty();
    00481         break;
    00482       }
    00483     }
    00484   }
    00485
    00486   virtual void OnDropdownSelect(int widget, int index)
    00487   {
    00488     switch (widget) {
    00489       case WID_RV_TRAIN_RAILTYPE_DROPDOWN: {
    00490         RailType temp = (RailType)index;
    00491         if (temp == sel_railtype) return; // we didn't select a new one. No need to change anything
    00492         sel_railtype = temp;
    00493         /* Reset scrollbar positions */
    00494         this->vscroll[0]->SetPosition(0);
    00495         this->vscroll[1]->SetPosition(0);
    00496         /* Rebuild the lists */
    00497         this->engines[0].ForceRebuild();
    00498         this->engines[1].ForceRebuild();
    00499         this->reset_sel_engine = true;
    00500         this->SetDirty();
    00501         break;
    00502       }
    00503
    00504       case WID_RV_START_REPLACE:
    00505         this->ReplaceClick_StartReplace(index != 0);
    00506         break;
    00507     }
    00508   }
    00509
    00510   virtual void OnResize()
    00511   {
    00512     this->vscroll[0]->SetCapacityFromWidget(this, WID_RV_LEFT_MATRIX);
    00513     this->vscroll[1]->SetCapacityFromWidget(this, WID_RV_RIGHT_MATRIX);
    00514
    00515     this->GetWidget<NWidgetCore>(WID_RV_LEFT_MATRIX)->widget_data =
    00516         this->GetWidget<NWidgetCore>(WID_RV_RIGHT_MATRIX)->widget_data = (this->vscroll[0]->GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
    00517   }
    00518
    00524   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
    00525   {
    00526     if (data != 0) {
    00527       /* This needs to be done in command-scope to enforce rebuilding before resorting invalid data */
    00528       this->engines[0].ForceRebuild();
    00529     } else {
    00530       this->engines[1].ForceRebuild();
    00531     }
    00532   }
    00533 };
    00534
    00535 static const NWidgetPart _nested_replace_rail_vehicle_widgets[] = {
    00536   NWidget(NWID_HORIZONTAL),
    00537     NWidget(WWT_CLOSEBOX, COLOUR_GREY),
    00538     NWidget(WWT_CAPTION, COLOUR_GREY, WID_RV_CAPTION), SetDataTip(STR_REPLACE_VEHICLES_WHITE, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    00539     NWidget(WWT_SHADEBOX, COLOUR_GREY),
    00540     NWidget(WWT_STICKYBOX, COLOUR_GREY),
    00541   EndContainer(),
    00542   NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
    00543     NWidget(WWT_MATRIX, COLOUR_GREY, WID_RV_LEFT_MATRIX), SetMinimalSize(216, 0), SetFill(1, 1), SetDataTip(0x1, STR_REPLACE_HELP_LEFT_ARRAY), SetResize(1, 1), SetScrollbar(WID_RV_LEFT_SCROLLBAR),
    00544     NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_RV_LEFT_SCROLLBAR),
    00545     NWidget(WWT_MATRIX, COLOUR_GREY, WID_RV_RIGHT_MATRIX), SetMinimalSize(216, 0), SetFill(1, 1), SetDataTip(0x1, STR_REPLACE_HELP_RIGHT_ARRAY), SetResize(1, 1), SetScrollbar(WID_RV_RIGHT_SCROLLBAR),
    00546     NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_RV_RIGHT_SCROLLBAR),
    00547   EndContainer(),
    00548   NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
    00549     NWidget(WWT_PANEL, COLOUR_GREY, WID_RV_LEFT_DETAILS), SetMinimalSize(240, 122), SetResize(1, 0), EndContainer(),
    00550     NWidget(WWT_PANEL, COLOUR_GREY, WID_RV_RIGHT_DETAILS), SetMinimalSize(240, 122), SetResize(1, 0), EndContainer(),
    00551   EndContainer(),
    00552   NWidget(NWID_HORIZONTAL),
    00553     NWidget(NWID_PUSHBUTTON_DROPDOWN, COLOUR_GREY, WID_RV_START_REPLACE), SetMinimalSize(139, 12), SetDataTip(STR_REPLACE_VEHICLES_START, STR_REPLACE_HELP_START_BUTTON),
    00554     NWidget(WWT_PANEL, COLOUR_GREY, WID_RV_INFO_TAB), SetMinimalSize(167, 12), SetDataTip(0x0, STR_REPLACE_HELP_REPLACE_INFO_TAB), SetResize(1, 0),
    00555     EndContainer(),
    00556     NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_RV_STOP_REPLACE), SetMinimalSize(150, 12), SetDataTip(STR_REPLACE_VEHICLES_STOP, STR_REPLACE_HELP_STOP_BUTTON),
    00557   EndContainer(),
    00558   NWidget(NWID_HORIZONTAL),
    00559     NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_RV_TRAIN_ENGINEWAGON_TOGGLE), SetMinimalSize(139, 12), SetDataTip(STR_REPLACE_ENGINE_WAGON_SELECT, STR_REPLACE_ENGINE_WAGON_SELECT_HELP),
    00560     NWidget(WWT_PANEL, COLOUR_GREY, WID_RV_TRAIN_FLUFF_LEFT), SetMinimalSize(15, 12), EndContainer(),
    00561     NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_RV_TRAIN_RAILTYPE_DROPDOWN), SetMinimalSize(136, 12), SetDataTip(0x0, STR_REPLACE_HELP_RAILTYPE), SetResize(1, 0),
    00562     NWidget(WWT_PANEL, COLOUR_GREY, WID_RV_TRAIN_FLUFF_RIGHT), SetMinimalSize(16, 12), EndContainer(),
    00563     NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_RV_TRAIN_WAGONREMOVE_TOGGLE), SetMinimalSize(138, 12), SetDataTip(STR_REPLACE_REMOVE_WAGON, STR_REPLACE_REMOVE_WAGON_HELP),
    00564     NWidget(WWT_RESIZEBOX, COLOUR_GREY),
    00565   EndContainer(),
    00566 };
    00567
    00568 static const WindowDesc _replace_rail_vehicle_desc(
    00569   WDP_AUTO, 500, 140,
    00570   WC_REPLACE_VEHICLE, WC_NONE,
    00571   WDF_CONSTRUCTION,
    00572   _nested_replace_rail_vehicle_widgets, lengthof(_nested_replace_rail_vehicle_widgets)
    00573 );
    00574
    00575 static const NWidgetPart _nested_replace_vehicle_widgets[] = {
    00576   NWidget(NWID_HORIZONTAL),
    00577     NWidget(WWT_CLOSEBOX, COLOUR_GREY),
    00578     NWidget(WWT_CAPTION, COLOUR_GREY, WID_RV_CAPTION), SetMinimalSize(433, 14), SetDataTip(STR_REPLACE_VEHICLES_WHITE, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    00579     NWidget(WWT_SHADEBOX, COLOUR_GREY),
    00580     NWidget(WWT_STICKYBOX, COLOUR_GREY),
    00581   EndContainer(),
    00582   NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
    00583     NWidget(WWT_MATRIX, COLOUR_GREY, WID_RV_LEFT_MATRIX), SetMinimalSize(216, 0), SetFill(1, 1), SetDataTip(0x1, STR_REPLACE_HELP_LEFT_ARRAY), SetResize(1, 1), SetScrollbar(WID_RV_LEFT_SCROLLBAR),
    00584     NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_RV_LEFT_SCROLLBAR),
    00585     NWidget(WWT_MATRIX, COLOUR_GREY, WID_RV_RIGHT_MATRIX), SetMinimalSize(216, 0), SetFill(1, 1), SetDataTip(0x1, STR_REPLACE_HELP_RIGHT_ARRAY), SetResize(1, 1), SetScrollbar(WID_RV_RIGHT_SCROLLBAR),
    00586     NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_RV_RIGHT_SCROLLBAR),
    00587   EndContainer(),
    00588   NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
    00589     NWidget(WWT_PANEL, COLOUR_GREY, WID_RV_LEFT_DETAILS), SetMinimalSize(228, 92), SetResize(1, 0), EndContainer(),
    00590     NWidget(WWT_PANEL, COLOUR_GREY, WID_RV_RIGHT_DETAILS), SetMinimalSize(228, 92), SetResize(1, 0), EndContainer(),
    00591   EndContainer(),
    00592   NWidget(NWID_HORIZONTAL),
    00593     NWidget(NWID_PUSHBUTTON_DROPDOWN, COLOUR_GREY, WID_RV_START_REPLACE), SetMinimalSize(139, 12), SetDataTip(STR_REPLACE_VEHICLES_START, STR_REPLACE_HELP_START_BUTTON),
    00594     NWidget(WWT_PANEL, COLOUR_GREY, WID_RV_INFO_TAB), SetMinimalSize(167, 12), SetDataTip(0x0, STR_REPLACE_HELP_REPLACE_INFO_TAB), SetResize(1, 0), EndContainer(),
    00595     NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_RV_STOP_REPLACE), SetMinimalSize(138, 12), SetDataTip(STR_REPLACE_VEHICLES_STOP, STR_REPLACE_HELP_STOP_BUTTON),
    00596     NWidget(WWT_RESIZEBOX, COLOUR_GREY),
    00597   EndContainer(),
    00598 };
    00599
    00600 static const WindowDesc _replace_vehicle_desc(
    00601   WDP_AUTO, 456, 118,
    00602   WC_REPLACE_VEHICLE, WC_NONE,
    00603   WDF_CONSTRUCTION,
    00604   _nested_replace_vehicle_widgets, lengthof(_nested_replace_vehicle_widgets)
    00605 );
    00606
    00612 void ShowReplaceGroupVehicleWindow(GroupID id_g, VehicleType vehicletype)
    00613 {
    00614   DeleteWindowById(WC_REPLACE_VEHICLE, vehicletype);
    00615   new ReplaceVehicleWindow(vehicletype == VEH_TRAIN ? &_replace_rail_vehicle_desc : &_replace_vehicle_desc, vehicletype, id_g);
    00616 }