# autoreplace_gui.cpp

Go to the documentation of this file.

    00001 /* $Id: autoreplace_gui.cpp 26653 2014-06-17 19:14:59Z frosch $ */
    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         if (this->reset_sel_engine && this->sel_engine[0] != INVALID_ENGINE) {
    00168           /* Select the current replacement for sel_engine[0]. */
    00169           const Company *c = Company::Get(_local_company);
    00170           this->sel_engine[1] = EngineReplacementForCompany(c, this->sel_engine[0], this->sel_group);
    00171         }
    00172         /* Regenerate the list on the right. Note: This resets sel_engine[1] to INVALID_ENGINE, if it is no longer available. */
    00173         this->GenerateReplaceVehList(false);
    00174         this->vscroll[1]->SetCount(this->engines[1].Length());
    00175         if (this->reset_sel_engine && this->sel_engine[1] != INVALID_ENGINE) {
    00176           int position = 0;
    00177           for (EngineID *it = this->engines[1].Begin(); it != this->engines[1].End(); ++it) {
    00178             if (*it == this->sel_engine[1]) break;
    00179             ++position;
    00180           }
    00181           this->vscroll[1]->ScrollTowards(position);
    00182         }
    00183       }
    00184     }
    00185     /* Reset the flags about needed updates */
    00186     this->engines[0].RebuildDone();
    00187     this->engines[1].RebuildDone();
    00188     this->reset_sel_engine = false;
    00189   }
    00190
    00195   void ReplaceClick_StartReplace(bool replace_when_old)
    00196   {
    00197     EngineID veh_from = this->sel_engine[0];
    00198     EngineID veh_to = this->sel_engine[1];
    00199     DoCommandP(0, (replace_when_old ? 1 : 0) | (this->sel_group << 16), veh_from + (veh_to << 16), CMD_SET_AUTOREPLACE);
    00200   }
    00201
    00202 public:
    00203   ReplaceVehicleWindow(WindowDesc *desc, VehicleType vehicletype, GroupID id_g) : Window(desc)
    00204   {
    00205     if (vehicletype == VEH_TRAIN) {
    00206       /* For rail vehicles find the most used vehicle type, which is usually
    00207        * better than 'just' the first/previous vehicle type. */
    00208       uint type_count[RAILTYPE_END];
    00209       memset(type_count, 0, sizeof(type_count));
    00210
    00211       const Engine *e;
    00212       FOR_ALL_ENGINES_OF_TYPE(e, VEH_TRAIN) {
    00213         if (e->u.rail.railveh_type == RAILVEH_WAGON) continue;
    00214         type_count[e->u.rail.railtype] += GetGroupNumEngines(_local_company, id_g, e->index);
    00215       }
    00216
    00217       this->sel_railtype = RAILTYPE_BEGIN;
    00218       for (RailType rt = RAILTYPE_BEGIN; rt < RAILTYPE_END; rt++) {
    00219         if (type_count[this->sel_railtype] < type_count[rt]) this->sel_railtype = rt;
    00220       }
    00221     }
    00222
    00223     this->replace_engines  = true; // start with locomotives (all other vehicles will not read this bool)
    00224     this->engines[0].ForceRebuild();
    00225     this->engines[1].ForceRebuild();
    00226     this->reset_sel_engine = true;
    00227     this->details_height   = ((vehicletype == VEH_TRAIN) ? 10 : 9) * FONT_HEIGHT_NORMAL + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
    00228     this->sel_engine[0] = INVALID_ENGINE;
    00229     this->sel_engine[1] = INVALID_ENGINE;
    00230
    00231     this->CreateNestedTree();
    00232     this->vscroll[0] = this->GetScrollbar(WID_RV_LEFT_SCROLLBAR);
    00233     this->vscroll[1] = this->GetScrollbar(WID_RV_RIGHT_SCROLLBAR);
    00234     this->FinishInitNested(vehicletype);
    00235
    00236     this->owner = _local_company;
    00237     this->sel_group = id_g;
    00238   }
    00239
    00240   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
    00241   {
    00242     switch (widget) {
    00243       case WID_RV_LEFT_MATRIX:
    00244       case WID_RV_RIGHT_MATRIX:
    00245         resize->height = GetEngineListHeight((VehicleType)this->window_number);
    00246         size->height = (this->window_number <= VEH_ROAD ? 8 : 4) * resize->height;
    00247         break;
    00248
    00249       case WID_RV_LEFT_DETAILS:
    00250       case WID_RV_RIGHT_DETAILS:
    00251         size->height = this->details_height;
    00252         break;
    00253
    00254       case WID_RV_TRAIN_WAGONREMOVE_TOGGLE: {
    00255         StringID str = this->GetWidget<NWidgetCore>(widget)->widget_data;
    00256         SetDParam(0, STR_CONFIG_SETTING_ON);
    00257         Dimension d = GetStringBoundingBox(str);
    00258         SetDParam(0, STR_CONFIG_SETTING_OFF);
    00259         d = maxdim(d, GetStringBoundingBox(str));
    00260         d.width += padding.width;
    00261         d.height += padding.height;
    00262         *size = maxdim(*size, d);
    00263         break;
    00264       }
    00265
    00266       case WID_RV_TRAIN_ENGINEWAGON_TOGGLE: {
    00267         StringID str = this->GetWidget<NWidgetCore>(widget)->widget_data;
    00268         SetDParam(0, STR_REPLACE_ENGINES);
    00269         Dimension d = GetStringBoundingBox(str);
    00270         SetDParam(0, STR_REPLACE_WAGONS);
    00271         d = maxdim(d, GetStringBoundingBox(str));
    00272         d.width += padding.width;
    00273         d.height += padding.height;
    00274         *size = maxdim(*size, d);
    00275         break;
    00276       }
    00277
    00278       case WID_RV_INFO_TAB: {
    00279         Dimension d = GetStringBoundingBox(STR_REPLACE_NOT_REPLACING);
    00280         d = maxdim(d, GetStringBoundingBox(STR_REPLACE_NOT_REPLACING_VEHICLE_SELECTED));
    00281         d.width += WD_FRAMETEXT_LEFT +  WD_FRAMETEXT_RIGHT;
    00282         d.height += WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
    00283         *size = maxdim(*size, d);
    00284         break;
    00285       }
    00286
    00287       case WID_RV_TRAIN_RAILTYPE_DROPDOWN: {
    00288         Dimension d = {0, 0};
    00289         for (RailType rt = RAILTYPE_BEGIN; rt != RAILTYPE_END; rt++) {
    00290           const RailtypeInfo *rti = GetRailTypeInfo(rt);
    00291           /* Skip rail type if it has no label */
    00292           if (rti->label == 0) continue;
    00293           d = maxdim(d, GetStringBoundingBox(rti->strings.replace_text));
    00294         }
    00295         d.width += padding.width;
    00296         d.height += padding.height;
    00297         *size = maxdim(*size, d);
    00298         break;
    00299       }
    00300
    00301       case WID_RV_START_REPLACE: {
    00302         Dimension d = GetStringBoundingBox(STR_REPLACE_VEHICLES_START);
    00303         for (int i = 0; _start_replace_dropdown[i] != INVALID_STRING_ID; i++) {
    00304           d = maxdim(d, GetStringBoundingBox(_start_replace_dropdown[i]));
    00305         }
    00306         d.width += padding.width;
    00307         d.height += padding.height;
    00308         *size = maxdim(*size, d);
    00309         break;
    00310       }
    00311     }
    00312   }
    00313
    00314   virtual void SetStringParameters(int widget) const
    00315   {
    00316     switch (widget) {
    00317       case WID_RV_CAPTION:
    00318         SetDParam(0, STR_REPLACE_VEHICLE_TRAIN + this->window_number);
    00319         switch (this->sel_group) {
    00320           case ALL_GROUP:
    00321             SetDParam(1, STR_GROUP_ALL_TRAINS + this->window_number);
    00322             break;
    00323
    00324           case DEFAULT_GROUP:
    00325             SetDParam(1, STR_GROUP_DEFAULT_TRAINS + this->window_number);
    00326             break;
    00327
    00328           default:
    00329             SetDParam(1, STR_GROUP_NAME);
    00330             SetDParam(2, sel_group);
    00331             break;
    00332         }
    00333         break;
    00334
    00335       case WID_RV_TRAIN_WAGONREMOVE_TOGGLE: {
    00336         const Company *c = Company::Get(_local_company);
    00337         SetDParam(0, c->settings.renew_keep_length ? STR_CONFIG_SETTING_ON : STR_CONFIG_SETTING_OFF);
    00338         break;
    00339       }
    00340
    00341       case WID_RV_TRAIN_ENGINEWAGON_TOGGLE:
    00342         SetDParam(0, this->replace_engines ? STR_REPLACE_ENGINES : STR_REPLACE_WAGONS);
    00343         break;
    00344     }
    00345   }
    00346
    00347   virtual void DrawWidget(const Rect &r, int widget) const
    00348   {
    00349     switch (widget) {
    00350       case WID_RV_INFO_TAB: {
    00351         const Company *c = Company::Get(_local_company);
    00352         StringID str;
    00353         if (this->sel_engine[0] != INVALID_ENGINE) {
    00354           if (!EngineHasReplacementForCompany(c, this->sel_engine[0], this->sel_group)) {
    00355             str = STR_REPLACE_NOT_REPLACING;
    00356           } else {
    00357             bool when_old = false;
    00358             EngineID e = EngineReplacementForCompany(c, this->sel_engine[0], this->sel_group, &when_old);
    00359             str = when_old ? STR_REPLACE_REPLACING_WHEN_OLD : STR_ENGINE_NAME;
    00360             SetDParam(0, e);
    00361           }
    00362         } else {
    00363           str = STR_REPLACE_NOT_REPLACING_VEHICLE_SELECTED;
    00364         }
    00365
    00366         DrawString(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, r.top + WD_FRAMERECT_TOP, str, TC_BLACK, SA_HOR_CENTER);
    00367         break;
    00368       }
    00369
    00370       case WID_RV_LEFT_MATRIX:
    00371       case WID_RV_RIGHT_MATRIX: {
    00372         int side = (widget == WID_RV_LEFT_MATRIX) ? 0 : 1;
    00373         EngineID start  = this->vscroll[side]->GetPosition(); // what is the offset for the start (scrolling)
    00374         EngineID end    = min(this->vscroll[side]->GetCapacity() + start, this->engines[side].Length());
    00375
    00376         /* Do the actual drawing */
    00377         DrawEngineList((VehicleType)this->window_number, r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + WD_FRAMERECT_TOP,
    00378             &this->engines[side], start, end, this->sel_engine[side], side == 0, this->sel_group);
    00379         break;
    00380       }
    00381     }
    00382   }
    00383
    00384   virtual void OnPaint()
    00385   {
    00386     if (this->engines[0].NeedRebuild() || this->engines[1].NeedRebuild()) this->GenerateLists();
    00387
    00388     Company *c = Company::Get(_local_company);
    00389
    00390     /* Disable the "Start Replacing" button if:
    00391      *    Either engines list is empty
    00392      * or The selected replacement engine has a replacement (to prevent loops). */
    00393     this->SetWidgetDisabledState(WID_RV_START_REPLACE,
    00394                     this->sel_engine[0] == INVALID_ENGINE ||
    00395                     this->sel_engine[1] == INVALID_ENGINE ||
    00396                     EngineReplacementForCompany(c, this->sel_engine[1], this->sel_group) != INVALID_ENGINE);
    00397
    00398     /* Disable the "Stop Replacing" button if:
    00399      *   The left engines list (existing vehicle) is empty
    00400      *   or The selected vehicle has no replacement set up */
    00401     this->SetWidgetDisabledState(WID_RV_STOP_REPLACE,
    00402                     this->sel_engine[0] == INVALID_ENGINE ||
    00403                     !EngineHasReplacementForCompany(c, this->sel_engine[0], this->sel_group));
    00404
    00405     if (this->window_number == VEH_TRAIN) {
    00406       /* sets the colour of that art thing */
    00407       this->GetWidget<NWidgetCore>(WID_RV_TRAIN_FLUFF_LEFT)->colour  = _company_colours[_local_company];
    00408       this->GetWidget<NWidgetCore>(WID_RV_TRAIN_FLUFF_RIGHT)->colour = _company_colours[_local_company];
    00409
    00410       /* Show the selected railtype in the pulldown menu */
    00411       this->GetWidget<NWidgetCore>(WID_RV_TRAIN_RAILTYPE_DROPDOWN)->widget_data = GetRailTypeInfo(sel_railtype)->strings.replace_text;
    00412     }
    00413
    00414     this->DrawWidgets();
    00415
    00416     if (!this->IsShaded()) {
    00417       int needed_height = this->details_height;
    00418       /* Draw details panels. */
    00419       for (int side = 0; side < 2; side++) {
    00420         if (this->sel_engine[side] != INVALID_ENGINE) {
    00421           NWidgetBase *nwi = this->GetWidget<NWidgetBase>(side == 0 ? WID_RV_LEFT_DETAILS : WID_RV_RIGHT_DETAILS);
    00422           int text_end = DrawVehiclePurchaseInfo(nwi->pos_x + WD_FRAMETEXT_LEFT, nwi->pos_x + nwi->current_x - WD_FRAMETEXT_RIGHT,
    00423               nwi->pos_y + WD_FRAMERECT_TOP, this->sel_engine[side]);
    00424           needed_height = max(needed_height, text_end - (int)nwi->pos_y + WD_FRAMERECT_BOTTOM);
    00425         }
    00426       }
    00427       if (needed_height != this->details_height) { // Details window are not high enough, enlarge them.
    00428         this->details_height = needed_height;
    00429         this->ReInit();
    00430         return;
    00431       }
    00432     }
    00433   }
    00434
    00435   virtual void OnClick(Point pt, int widget, int click_count)
    00436   {
    00437     switch (widget) {
    00438       case WID_RV_TRAIN_ENGINEWAGON_TOGGLE:
    00439         this->replace_engines  = !(this->replace_engines);
    00440         this->engines[0].ForceRebuild();
    00441         this->reset_sel_engine = true;
    00442         this->SetDirty();
    00443         break;
    00444
    00445       case WID_RV_TRAIN_RAILTYPE_DROPDOWN: // Railtype selection dropdown menu
    00446         ShowDropDownList(this, GetRailTypeDropDownList(true), sel_railtype, WID_RV_TRAIN_RAILTYPE_DROPDOWN);
    00447         break;
    00448
    00449       case WID_RV_TRAIN_WAGONREMOVE_TOGGLE: // toggle renew_keep_length
    00450         DoCommandP(0, GetCompanySettingIndex("company.renew_keep_length"), Company::Get(_local_company)->settings.renew_keep_length ? 0 : 1, CMD_CHANGE_COMPANY_SETTING);
    00451         break;
    00452
    00453       case WID_RV_START_REPLACE: { // Start replacing
    00454         if (this->GetWidget<NWidgetLeaf>(widget)->ButtonHit(pt)) {
    00455           this->HandleButtonClick(WID_RV_START_REPLACE);
    00456           ReplaceClick_StartReplace(false);
    00457         } else {
    00458           bool replacment_when_old = EngineHasReplacementWhenOldForCompany(Company::Get(_local_company), this->sel_engine[0], this->sel_group);
    00459           ShowDropDownMenu(this, _start_replace_dropdown, replacment_when_old ? 1 : 0, WID_RV_START_REPLACE, !this->replace_engines ? 1 << 1 : 0, 0);
    00460         }
    00461         break;
    00462       }
    00463
    00464       case WID_RV_STOP_REPLACE: { // Stop replacing
    00465         EngineID veh_from = this->sel_engine[0];
    00466         DoCommandP(0, this->sel_group << 16, veh_from + (INVALID_ENGINE << 16), CMD_SET_AUTOREPLACE);
    00467         break;
    00468       }
    00469
    00470       case WID_RV_LEFT_MATRIX:
    00471       case WID_RV_RIGHT_MATRIX: {
    00472         byte click_side;
    00473         if (widget == WID_RV_LEFT_MATRIX) {
    00474           click_side = 0;
    00475         } else {
    00476           click_side = 1;
    00477         }
    00478         uint i = this->vscroll[click_side]->GetScrolledRowFromWidget(pt.y, this, widget);
    00479         size_t engine_count = this->engines[click_side].Length();
    00480
    00481         EngineID e = engine_count > i ? this->engines[click_side][i] : INVALID_ENGINE;
    00482         if (e == this->sel_engine[click_side]) break; // we clicked the one we already selected
    00483         this->sel_engine[click_side] = e;
    00484         if (click_side == 0) {
    00485           this->engines[1].ForceRebuild();
    00486           this->reset_sel_engine = true;
    00487         }
    00488         this->SetDirty();
    00489         break;
    00490       }
    00491     }
    00492   }
    00493
    00494   virtual void OnDropdownSelect(int widget, int index)
    00495   {
    00496     switch (widget) {
    00497       case WID_RV_TRAIN_RAILTYPE_DROPDOWN: {
    00498         RailType temp = (RailType)index;
    00499         if (temp == sel_railtype) return; // we didn't select a new one. No need to change anything
    00500         sel_railtype = temp;
    00501         /* Reset scrollbar positions */
    00502         this->vscroll[0]->SetPosition(0);
    00503         this->vscroll[1]->SetPosition(0);
    00504         /* Rebuild the lists */
    00505         this->engines[0].ForceRebuild();
    00506         this->engines[1].ForceRebuild();
    00507         this->reset_sel_engine = true;
    00508         this->SetDirty();
    00509         break;
    00510       }
    00511
    00512       case WID_RV_START_REPLACE:
    00513         this->ReplaceClick_StartReplace(index != 0);
    00514         break;
    00515     }
    00516   }
    00517
    00518   virtual void OnResize()
    00519   {
    00520     this->vscroll[0]->SetCapacityFromWidget(this, WID_RV_LEFT_MATRIX);
    00521     this->vscroll[1]->SetCapacityFromWidget(this, WID_RV_RIGHT_MATRIX);
    00522   }
    00523
    00529   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
    00530   {
    00531     if (data != 0) {
    00532       /* This needs to be done in command-scope to enforce rebuilding before resorting invalid data */
    00533       this->engines[0].ForceRebuild();
    00534     } else {
    00535       this->engines[1].ForceRebuild();
    00536     }
    00537   }
    00538 };
    00539
    00540 static const NWidgetPart _nested_replace_rail_vehicle_widgets[] = {
    00541   NWidget(NWID_HORIZONTAL),
    00542     NWidget(WWT_CLOSEBOX, COLOUR_GREY),
    00543     NWidget(WWT_CAPTION, COLOUR_GREY, WID_RV_CAPTION), SetDataTip(STR_REPLACE_VEHICLES_WHITE, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    00544     NWidget(WWT_SHADEBOX, COLOUR_GREY),
    00545     NWidget(WWT_DEFSIZEBOX, COLOUR_GREY),
    00546     NWidget(WWT_STICKYBOX, COLOUR_GREY),
    00547   EndContainer(),
    00548   NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
    00549     NWidget(WWT_MATRIX, COLOUR_GREY, WID_RV_LEFT_MATRIX), SetMinimalSize(216, 0), SetFill(1, 1), SetMatrixDataTip(1, 0, STR_REPLACE_HELP_LEFT_ARRAY), SetResize(1, 1), SetScrollbar(WID_RV_LEFT_SCROLLBAR),
    00550     NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_RV_LEFT_SCROLLBAR),
    00551     NWidget(WWT_MATRIX, COLOUR_GREY, WID_RV_RIGHT_MATRIX), SetMinimalSize(216, 0), SetFill(1, 1), SetMatrixDataTip(1, 0, STR_REPLACE_HELP_RIGHT_ARRAY), SetResize(1, 1), SetScrollbar(WID_RV_RIGHT_SCROLLBAR),
    00552     NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_RV_RIGHT_SCROLLBAR),
    00553   EndContainer(),
    00554   NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
    00555     NWidget(WWT_PANEL, COLOUR_GREY, WID_RV_LEFT_DETAILS), SetMinimalSize(240, 122), SetResize(1, 0), EndContainer(),
    00556     NWidget(WWT_PANEL, COLOUR_GREY, WID_RV_RIGHT_DETAILS), SetMinimalSize(240, 122), SetResize(1, 0), EndContainer(),
    00557   EndContainer(),
    00558   NWidget(NWID_HORIZONTAL),
    00559     NWidget(NWID_PUSHBUTTON_DROPDOWN, COLOUR_GREY, WID_RV_START_REPLACE), SetMinimalSize(139, 12), SetDataTip(STR_REPLACE_VEHICLES_START, STR_REPLACE_HELP_START_BUTTON),
    00560     NWidget(WWT_PANEL, COLOUR_GREY, WID_RV_INFO_TAB), SetMinimalSize(167, 12), SetDataTip(0x0, STR_REPLACE_HELP_REPLACE_INFO_TAB), SetResize(1, 0),
    00561     EndContainer(),
    00562     NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_RV_STOP_REPLACE), SetMinimalSize(150, 12), SetDataTip(STR_REPLACE_VEHICLES_STOP, STR_REPLACE_HELP_STOP_BUTTON),
    00563   EndContainer(),
    00564   NWidget(NWID_HORIZONTAL),
    00565     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),
    00566     NWidget(WWT_PANEL, COLOUR_GREY, WID_RV_TRAIN_FLUFF_LEFT), SetMinimalSize(15, 12), EndContainer(),
    00567     NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_RV_TRAIN_RAILTYPE_DROPDOWN), SetMinimalSize(136, 12), SetDataTip(0x0, STR_REPLACE_HELP_RAILTYPE), SetResize(1, 0),
    00568     NWidget(WWT_PANEL, COLOUR_GREY, WID_RV_TRAIN_FLUFF_RIGHT), SetMinimalSize(16, 12), EndContainer(),
    00569     NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_RV_TRAIN_WAGONREMOVE_TOGGLE), SetMinimalSize(138, 12), SetDataTip(STR_REPLACE_REMOVE_WAGON, STR_REPLACE_REMOVE_WAGON_HELP),
    00570     NWidget(WWT_RESIZEBOX, COLOUR_GREY),
    00571   EndContainer(),
    00572 };
    00573
    00574 static WindowDesc _replace_rail_vehicle_desc(
    00575   WDP_AUTO, "replace_vehicle_train", 500, 140,
    00576   WC_REPLACE_VEHICLE, WC_NONE,
    00577   WDF_CONSTRUCTION,
    00578   _nested_replace_rail_vehicle_widgets, lengthof(_nested_replace_rail_vehicle_widgets)
    00579 );
    00580
    00581 static const NWidgetPart _nested_replace_vehicle_widgets[] = {
    00582   NWidget(NWID_HORIZONTAL),
    00583     NWidget(WWT_CLOSEBOX, COLOUR_GREY),
    00584     NWidget(WWT_CAPTION, COLOUR_GREY, WID_RV_CAPTION), SetMinimalSize(433, 14), SetDataTip(STR_REPLACE_VEHICLES_WHITE, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    00585     NWidget(WWT_SHADEBOX, COLOUR_GREY),
    00586     NWidget(WWT_DEFSIZEBOX, COLOUR_GREY),
    00587     NWidget(WWT_STICKYBOX, COLOUR_GREY),
    00588   EndContainer(),
    00589   NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
    00590     NWidget(WWT_MATRIX, COLOUR_GREY, WID_RV_LEFT_MATRIX), SetMinimalSize(216, 0), SetFill(1, 1), SetMatrixDataTip(1, 0, STR_REPLACE_HELP_LEFT_ARRAY), SetResize(1, 1), SetScrollbar(WID_RV_LEFT_SCROLLBAR),
    00591     NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_RV_LEFT_SCROLLBAR),
    00592     NWidget(WWT_MATRIX, COLOUR_GREY, WID_RV_RIGHT_MATRIX), SetMinimalSize(216, 0), SetFill(1, 1), SetMatrixDataTip(1, 0, STR_REPLACE_HELP_RIGHT_ARRAY), SetResize(1, 1), SetScrollbar(WID_RV_RIGHT_SCROLLBAR),
    00593     NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_RV_RIGHT_SCROLLBAR),
    00594   EndContainer(),
    00595   NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
    00596     NWidget(WWT_PANEL, COLOUR_GREY, WID_RV_LEFT_DETAILS), SetMinimalSize(228, 92), SetResize(1, 0), EndContainer(),
    00597     NWidget(WWT_PANEL, COLOUR_GREY, WID_RV_RIGHT_DETAILS), SetMinimalSize(228, 92), SetResize(1, 0), EndContainer(),
    00598   EndContainer(),
    00599   NWidget(NWID_HORIZONTAL),
    00600     NWidget(NWID_PUSHBUTTON_DROPDOWN, COLOUR_GREY, WID_RV_START_REPLACE), SetMinimalSize(139, 12), SetDataTip(STR_REPLACE_VEHICLES_START, STR_REPLACE_HELP_START_BUTTON),
    00601     NWidget(WWT_PANEL, COLOUR_GREY, WID_RV_INFO_TAB), SetMinimalSize(167, 12), SetDataTip(0x0, STR_REPLACE_HELP_REPLACE_INFO_TAB), SetResize(1, 0), EndContainer(),
    00602     NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_RV_STOP_REPLACE), SetMinimalSize(138, 12), SetDataTip(STR_REPLACE_VEHICLES_STOP, STR_REPLACE_HELP_STOP_BUTTON),
    00603     NWidget(WWT_RESIZEBOX, COLOUR_GREY),
    00604   EndContainer(),
    00605 };
    00606
    00607 static WindowDesc _replace_vehicle_desc(
    00608   WDP_AUTO, "replace_vehicle", 456, 118,
    00609   WC_REPLACE_VEHICLE, WC_NONE,
    00610   WDF_CONSTRUCTION,
    00611   _nested_replace_vehicle_widgets, lengthof(_nested_replace_vehicle_widgets)
    00612 );
    00613
    00619 void ShowReplaceGroupVehicleWindow(GroupID id_g, VehicleType vehicletype)
    00620 {
    00621   DeleteWindowById(WC_REPLACE_VEHICLE, vehicletype);
    00622   new ReplaceVehicleWindow(vehicletype == VEH_TRAIN ? &_replace_rail_vehicle_desc : &_replace_vehicle_desc, vehicletype, id_g);
    00623 }