Translations update
[openttd/fttd.git] / src / autoreplace_gui.cpp
blob921bacb96528828783a953784c9c3e06fdfec77a
1 /* $Id$ */
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
8 */
10 /** @file autoreplace_gui.cpp GUI for autoreplace handling. */
12 #include "stdafx.h"
13 #include "command_func.h"
14 #include "vehicle_gui.h"
15 #include "newgrf_engine.h"
16 #include "rail.h"
17 #include "strings_func.h"
18 #include "window_func.h"
19 #include "autoreplace_func.h"
20 #include "company_func.h"
21 #include "engine_base.h"
22 #include "window_gui.h"
23 #include "engine_gui.h"
24 #include "settings_func.h"
25 #include "core/geometry_func.hpp"
26 #include "rail_gui.h"
27 #include "widgets/dropdown_func.h"
29 #include "widgets/autoreplace_widget.h"
31 void DrawEngineList (VehicleType type, BlitArea *dpi, int x, int r, int y,
32 const GUIEngineList *eng_list, uint16 min, uint16 max,
33 EngineID selected_id, bool show_count, GroupID selected_group);
35 static int CDECL EngineNumberSorter(const EngineID *a, const EngineID *b)
37 int r = Engine::Get(*a)->list_position - Engine::Get(*b)->list_position;
39 return r;
42 /**
43 * Rebuild the left autoreplace list if an engine is removed or added
44 * @param e Engine to check if it is removed or added
45 * @param id_g The group the engine belongs to
46 * Note: this function only works if it is called either
47 * - when a new vehicle is build, but before it's counted in num_engines
48 * - when a vehicle is deleted and after it's subtracted from num_engines
49 * - when not changing the count (used when changing replace orders)
51 void InvalidateAutoreplaceWindow(EngineID e, GroupID id_g)
53 if (GetGroupNumEngines(_local_company, id_g, e) == 0 || GetGroupNumEngines(_local_company, ALL_GROUP, e) == 0) {
54 /* We don't have any of this engine type.
55 * Either we just sold the last one, we build a new one or we stopped replacing it.
56 * In all cases, we need to update the left list */
57 InvalidateWindowData(WC_REPLACE_VEHICLE, Engine::Get(e)->type, 1);
61 /**
62 * When an engine is made buildable or is removed from being buildable, add/remove it from the build/autoreplace lists
63 * @param type The type of engine
65 void AddRemoveEngineFromAutoreplaceAndBuildWindows(VehicleType type)
67 InvalidateWindowData(WC_REPLACE_VEHICLE, type, 0); // Update the autoreplace window
68 InvalidateWindowClassesData(WC_BUILD_VEHICLE); // The build windows needs updating as well
71 static const StringID _start_replace_dropdown[] = {
72 STR_REPLACE_VEHICLES_NOW,
73 STR_REPLACE_VEHICLES_WHEN_OLD,
74 INVALID_STRING_ID
77 /**
78 * Window for the autoreplacing of vehicles.
80 class ReplaceVehicleWindow : public Window {
81 EngineID sel_engine[2]; ///< Selected engine left and right.
82 GUIEngineList engines[2]; ///< Left and right list of engines.
83 bool replace_engines; ///< If \c true, engines are replaced, if \c false, wagons are replaced (only for trains).
84 bool reset_sel_engine; ///< Also reset #sel_engine while updating left and/or right (#update_left and/or #update_right) and no valid engine selected.
85 GroupID sel_group; ///< Group selected to replace.
86 int details_height; ///< Minimal needed height of the details panels (found so far).
87 byte sort_criteria; ///< Criteria of sorting vehicles.
88 bool descending_sort_order; ///< Order of sorting vehicles.
89 bool show_hidden_engines; ///< Whether to show the hidden engines.
90 RailType sel_railtype; ///< Type of rail tracks selected. #INVALID_RAILTYPE to show all.
91 Scrollbar *vscroll[2];
93 /**
94 * Figure out if an engine should be added to a list.
95 * @param e The EngineID.
96 * @param draw_left If \c true, the left list is drawn (the engines specific to the railtype you selected).
97 * @param show_engines If \c true, the locomotives are drawn, else the wagons are drawn (never both).
98 * @return \c true if the engine should be in the list (based on this check), else \c false.
100 bool GenerateReplaceRailList(EngineID e, bool draw_left, bool show_engines)
102 const RailVehicleInfo *rvi = RailVehInfo(e);
104 /* Ensure that the wagon/engine selection fits the engine. */
105 if ((rvi->railveh_type == RAILVEH_WAGON) == show_engines) return false;
107 if (draw_left && this->sel_railtype != INVALID_RAILTYPE) {
108 /* Ensure that the railtype is specific to the selected one */
109 if (rvi->railtype != this->sel_railtype) return false;
111 return true;
116 * Generate an engines list
117 * @param draw_left true if generating the left list, otherwise false
119 void GenerateReplaceVehList(bool draw_left)
121 EngineID selected_engine = INVALID_ENGINE;
122 VehicleType type = (VehicleType)this->window_number;
123 byte side = draw_left ? 0 : 1;
125 GUIEngineList *list = &this->engines[side];
126 list->Clear();
128 const Engine *e;
129 FOR_ALL_ENGINES_OF_TYPE(e, type) {
130 if (!draw_left && !this->show_hidden_engines && e->IsHidden(_local_company)) continue;
131 EngineID eid = e->index;
132 if (type == VEH_TRAIN && !this->GenerateReplaceRailList(eid, draw_left, this->replace_engines)) continue; // special rules for trains
134 if (draw_left) {
135 const uint num_engines = GetGroupNumEngines(_local_company, this->sel_group, eid);
137 /* Skip drawing the engines we don't have any of and haven't set for replacement */
138 if (num_engines == 0 && EngineReplacementForCompany(Company::Get(_local_company), eid, this->sel_group) == INVALID_ENGINE) continue;
139 } else {
140 if (!CheckAutoreplaceValidity(this->sel_engine[0], eid, _local_company)) continue;
143 *list->Append() = eid;
144 if (eid == this->sel_engine[side]) selected_engine = eid; // The selected engine is still in the list
146 this->sel_engine[side] = selected_engine; // update which engine we selected (the same or none, if it's not in the list anymore)
147 if (draw_left) {
148 EngList_Sort(list, &EngineNumberSorter);
149 } else {
150 _engine_sort_direction = this->descending_sort_order;
151 EngList_Sort(list, _engine_sort_functions[this->window_number][this->sort_criteria]);
155 /** Generate the lists */
156 void GenerateLists()
158 EngineID e = this->sel_engine[0];
160 if (this->engines[0].NeedRebuild()) {
161 /* We need to rebuild the left engines list */
162 this->GenerateReplaceVehList(true);
163 this->vscroll[0]->SetCount(this->engines[0].Length());
164 if (this->reset_sel_engine && this->sel_engine[0] == INVALID_ENGINE && this->engines[0].Length() != 0) {
165 this->sel_engine[0] = this->engines[0][0];
169 if (this->engines[1].NeedRebuild() || e != this->sel_engine[0]) {
170 /* Either we got a request to rebuild the right engines list, or the left engines list selected a different engine */
171 if (this->sel_engine[0] == INVALID_ENGINE) {
172 /* Always empty the right engines list when nothing is selected in the left engines list */
173 this->engines[1].Clear();
174 this->sel_engine[1] = INVALID_ENGINE;
175 } else {
176 if (this->reset_sel_engine && this->sel_engine[0] != INVALID_ENGINE) {
177 /* Select the current replacement for sel_engine[0]. */
178 const Company *c = Company::Get(_local_company);
179 this->sel_engine[1] = EngineReplacementForCompany(c, this->sel_engine[0], this->sel_group);
181 /* Regenerate the list on the right. Note: This resets sel_engine[1] to INVALID_ENGINE, if it is no longer available. */
182 this->GenerateReplaceVehList(false);
183 this->vscroll[1]->SetCount(this->engines[1].Length());
184 if (this->reset_sel_engine && this->sel_engine[1] != INVALID_ENGINE) {
185 int position = 0;
186 for (EngineID *it = this->engines[1].Begin(); it != this->engines[1].End(); ++it) {
187 if (*it == this->sel_engine[1]) break;
188 ++position;
190 this->vscroll[1]->ScrollTowards(position);
194 /* Reset the flags about needed updates */
195 this->engines[0].RebuildDone();
196 this->engines[1].RebuildDone();
197 this->reset_sel_engine = false;
201 * Handle click on the start replace button.
202 * @param replace_when_old Replace now or only when old?
204 void ReplaceClick_StartReplace(bool replace_when_old)
206 EngineID veh_from = this->sel_engine[0];
207 EngineID veh_to = this->sel_engine[1];
208 DoCommandP(0, (replace_when_old ? 1 : 0) | (this->sel_group << 16), veh_from + (veh_to << 16), CMD_SET_AUTOREPLACE);
212 * Handle click on the engine/wagon toggle.
213 * @param engines Whether to select engines or wagons.
215 void ReplaceClick_EngineWagonToggle (bool engines)
217 this->replace_engines = engines;
218 this->engines[0].ForceRebuild();
219 this->reset_sel_engine = true;
220 this->SetDirty();
223 public:
224 ReplaceVehicleWindow (const WindowDesc *desc, VehicleType vehicletype, GroupID id_g)
225 : Window (desc), sel_engine(), engines(),
226 replace_engines (false), reset_sel_engine (false),
227 sel_group (0), details_height (0), sort_criteria (0),
228 descending_sort_order (false), show_hidden_engines (false),
229 sel_railtype (INVALID_RAILTYPE), vscroll()
231 this->sel_engine[0] = this->sel_engine[1] = (EngineID)0;
232 this->vscroll[0] = this->vscroll[1] = NULL;
234 this->replace_engines = true; // start with locomotives (all other vehicles will not read this bool)
235 this->engines[0].ForceRebuild();
236 this->engines[1].ForceRebuild();
237 this->reset_sel_engine = true;
238 this->details_height = ((vehicletype == VEH_TRAIN) ? 10 : 9) * FONT_HEIGHT_NORMAL + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
239 this->sel_engine[0] = INVALID_ENGINE;
240 this->sel_engine[1] = INVALID_ENGINE;
241 this->show_hidden_engines = _engine_sort_show_hidden_engines[vehicletype];
243 this->CreateNestedTree();
244 this->vscroll[0] = this->GetScrollbar(WID_RV_LEFT_SCROLLBAR);
245 this->vscroll[1] = this->GetScrollbar(WID_RV_RIGHT_SCROLLBAR);
247 NWidgetCore *widget = this->GetWidget<NWidgetCore>(WID_RV_SHOW_HIDDEN_ENGINES);
248 widget->widget_data = STR_SHOW_HIDDEN_ENGINES_VEHICLE_TRAIN + vehicletype;
249 widget->tool_tip = STR_SHOW_HIDDEN_ENGINES_VEHICLE_TRAIN_TOOLTIP + vehicletype;
250 widget->SetLowered(this->show_hidden_engines);
251 this->InitNested(vehicletype);
253 this->sort_criteria = _engine_sort_last_criteria[vehicletype];
254 this->descending_sort_order = _engine_sort_last_order[vehicletype];
255 this->owner = _local_company;
256 this->sel_group = id_g;
259 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
261 switch (widget) {
262 case WID_RV_SORT_ASCENDING_DESCENDING: {
263 Dimension d = GetStringBoundingBox(this->GetWidget<NWidgetCore>(widget)->widget_data);
264 d.width += padding.width + Window::SortButtonWidth() * 2; // Doubled since the string is centred and it also looks better.
265 d.height += padding.height;
266 *size = maxdim(*size, d);
267 break;
270 case WID_RV_LEFT_MATRIX:
271 case WID_RV_RIGHT_MATRIX:
272 resize->height = GetEngineListHeight((VehicleType)this->window_number);
273 size->height = (this->window_number <= VEH_ROAD ? 8 : 4) * resize->height;
274 break;
276 case WID_RV_LEFT_DETAILS:
277 case WID_RV_RIGHT_DETAILS:
278 size->height = this->details_height;
279 break;
281 case WID_RV_TRAIN_WAGONREMOVE_TOGGLE: {
282 StringID str = this->GetWidget<NWidgetCore>(widget)->widget_data;
283 SetDParam(0, STR_CONFIG_SETTING_ON);
284 Dimension d = GetStringBoundingBox(str);
285 SetDParam(0, STR_CONFIG_SETTING_OFF);
286 d = maxdim(d, GetStringBoundingBox(str));
287 d.width += padding.width;
288 d.height += padding.height;
289 *size = maxdim(*size, d);
290 break;
293 case WID_RV_TRAIN_ENGINEWAGON_DROPDOWN: {
294 StringID str = this->GetWidget<NWidgetCore>(widget)->widget_data;
295 SetDParam(0, STR_REPLACE_ENGINES);
296 Dimension d = GetStringBoundingBox(str);
297 SetDParam(0, STR_REPLACE_WAGONS);
298 d = maxdim(d, GetStringBoundingBox(str));
299 d.width += padding.width;
300 d.height += padding.height;
301 *size = maxdim(*size, d);
302 break;
305 case WID_RV_INFO_TAB: {
306 Dimension d = GetStringBoundingBox(STR_REPLACE_NOT_REPLACING);
307 d = maxdim(d, GetStringBoundingBox(STR_REPLACE_NOT_REPLACING_VEHICLE_SELECTED));
308 Dimension h = GetStringBoundingBox(STR_REPLACE_INFO_TAB);
309 d.width = max (d.width, h.width);
310 d.width += WD_FRAMETEXT_LEFT + WD_FRAMETEXT_RIGHT;
311 d.height += h.height + 2 * (WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM);
312 *size = maxdim(*size, d);
313 break;
316 case WID_RV_TRAIN_RAILTYPE_DROPDOWN: {
317 Dimension d = {0, 0};
318 for (RailType rt = RAILTYPE_BEGIN; rt != RAILTYPE_END; rt++) {
319 const RailtypeInfo *rti = GetRailTypeInfo(rt);
320 /* Skip rail type if it has no label */
321 if (rti->label == 0) continue;
322 d = maxdim(d, GetStringBoundingBox(rti->strings.replace_text));
324 d.width += padding.width;
325 d.height += padding.height;
326 *size = maxdim(*size, d);
327 break;
330 case WID_RV_START_REPLACE: {
331 Dimension d = GetStringBoundingBox(STR_REPLACE_VEHICLES_START);
332 for (int i = 0; _start_replace_dropdown[i] != INVALID_STRING_ID; i++) {
333 d = maxdim(d, GetStringBoundingBox(_start_replace_dropdown[i]));
335 d.width += padding.width;
336 d.height += padding.height;
337 *size = maxdim(*size, d);
338 break;
343 virtual void SetStringParameters(int widget) const
345 switch (widget) {
346 case WID_RV_CAPTION:
347 SetDParam(0, STR_REPLACE_VEHICLE_TRAIN + this->window_number);
348 switch (this->sel_group) {
349 case ALL_GROUP:
350 SetDParam(1, STR_GROUP_ALL_TRAINS + this->window_number);
351 break;
353 case DEFAULT_GROUP:
354 SetDParam(1, STR_GROUP_DEFAULT_TRAINS + this->window_number);
355 break;
357 default:
358 SetDParam(1, STR_GROUP_NAME);
359 SetDParam(2, sel_group);
360 break;
362 break;
364 case WID_RV_SORT_DROPDOWN:
365 SetDParam(0, _engine_sort_listing[this->window_number][this->sort_criteria]);
366 break;
368 case WID_RV_TRAIN_WAGONREMOVE_TOGGLE: {
369 const Company *c = Company::Get(_local_company);
370 SetDParam(0, c->settings.renew_keep_length ? STR_CONFIG_SETTING_ON : STR_CONFIG_SETTING_OFF);
371 break;
374 case WID_RV_TRAIN_ENGINEWAGON_DROPDOWN:
375 SetDParam(0, this->replace_engines ? STR_REPLACE_ENGINES : STR_REPLACE_WAGONS);
376 break;
380 void DrawWidget (BlitArea *dpi, const Rect &r, int widget) const OVERRIDE
382 switch (widget) {
383 case WID_RV_SORT_ASCENDING_DESCENDING:
384 this->DrawSortButtonState (dpi, WID_RV_SORT_ASCENDING_DESCENDING, this->descending_sort_order ? SBS_DOWN : SBS_UP);
385 break;
387 case WID_RV_INFO_TAB: {
388 DrawString (dpi, r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, r.top + WD_FRAMERECT_TOP, STR_REPLACE_INFO_TAB, TC_BLACK, SA_HOR_CENTER);
390 const Company *c = Company::Get(_local_company);
391 StringID str;
392 if (this->sel_engine[0] != INVALID_ENGINE) {
393 if (!EngineHasReplacementForCompany(c, this->sel_engine[0], this->sel_group)) {
394 str = STR_REPLACE_NOT_REPLACING;
395 } else {
396 bool when_old = false;
397 EngineID e = EngineReplacementForCompany(c, this->sel_engine[0], this->sel_group, &when_old);
398 str = when_old ? STR_REPLACE_REPLACING_WHEN_OLD : STR_ENGINE_NAME;
399 SetDParam(0, e);
401 } else {
402 str = STR_REPLACE_NOT_REPLACING_VEHICLE_SELECTED;
405 DrawString (dpi, r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, r.top + 2 * WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM + GetStringBoundingBox(STR_REPLACE_INFO_TAB).height, str, TC_BLACK, SA_HOR_CENTER);
406 break;
409 case WID_RV_LEFT_MATRIX:
410 case WID_RV_RIGHT_MATRIX: {
411 int side = (widget == WID_RV_LEFT_MATRIX) ? 0 : 1;
412 EngineID start = this->vscroll[side]->GetPosition(); // what is the offset for the start (scrolling)
413 EngineID end = min(this->vscroll[side]->GetCapacity() + start, this->engines[side].Length());
415 /* Do the actual drawing */
416 DrawEngineList ((VehicleType)this->window_number, dpi, r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + WD_FRAMERECT_TOP,
417 &this->engines[side], start, end, this->sel_engine[side], side == 0, this->sel_group);
418 break;
423 void OnPaint (BlitArea *dpi) OVERRIDE
425 if (this->engines[0].NeedRebuild() || this->engines[1].NeedRebuild()) this->GenerateLists();
427 Company *c = Company::Get(_local_company);
429 /* Disable the "Start Replacing" button if:
430 * Either engines list is empty
431 * or The selected replacement engine has a replacement (to prevent loops). */
432 this->SetWidgetDisabledState(WID_RV_START_REPLACE,
433 this->sel_engine[0] == INVALID_ENGINE ||
434 this->sel_engine[1] == INVALID_ENGINE ||
435 EngineReplacementForCompany(c, this->sel_engine[1], this->sel_group) != INVALID_ENGINE);
437 /* Disable the "Stop Replacing" button if:
438 * The left engines list (existing vehicle) is empty
439 * or The selected vehicle has no replacement set up */
440 this->SetWidgetDisabledState(WID_RV_STOP_REPLACE,
441 this->sel_engine[0] == INVALID_ENGINE ||
442 !EngineHasReplacementForCompany(c, this->sel_engine[0], this->sel_group));
444 if (this->window_number == VEH_TRAIN) {
445 /* Show the selected railtype in the pulldown menu */
446 this->GetWidget<NWidgetCore>(WID_RV_TRAIN_RAILTYPE_DROPDOWN)->widget_data = sel_railtype == INVALID_RAILTYPE ? STR_REPLACE_ALL_RAILTYPE : GetRailTypeInfo(sel_railtype)->strings.replace_text;
449 this->DrawWidgets (dpi);
451 if (!this->IsShaded()) {
452 int needed_height = this->details_height;
453 /* Draw details panels. */
454 for (int side = 0; side < 2; side++) {
455 if (this->sel_engine[side] != INVALID_ENGINE) {
456 NWidgetBase *nwi = this->GetWidget<NWidgetBase>(side == 0 ? WID_RV_LEFT_DETAILS : WID_RV_RIGHT_DETAILS);
457 int text_end = DrawVehiclePurchaseInfo (dpi, nwi->pos_x + WD_FRAMETEXT_LEFT, nwi->pos_x + nwi->current_x - WD_FRAMETEXT_RIGHT,
458 nwi->pos_y + WD_FRAMERECT_TOP, this->sel_engine[side]);
459 needed_height = max(needed_height, text_end - (int)nwi->pos_y + WD_FRAMERECT_BOTTOM);
462 if (needed_height != this->details_height) { // Details window are not high enough, enlarge them.
463 this->details_height = needed_height;
464 this->ReInit();
465 return;
470 virtual void OnClick(Point pt, int widget, int click_count)
472 switch (widget) {
473 case WID_RV_SORT_ASCENDING_DESCENDING:
474 this->descending_sort_order ^= true;
475 _engine_sort_last_order[this->window_number] = this->descending_sort_order;
476 this->engines[1].ForceRebuild();
477 this->SetDirty();
478 break;
480 case WID_RV_SHOW_HIDDEN_ENGINES:
481 this->show_hidden_engines ^= true;
482 _engine_sort_show_hidden_engines[this->window_number] = this->show_hidden_engines;
483 this->engines[1].ForceRebuild();
484 this->SetWidgetLoweredState(widget, this->show_hidden_engines);
485 this->SetDirty();
486 break;
488 case WID_RV_SORT_DROPDOWN:
489 DisplayVehicleSortDropDown(this, static_cast<VehicleType>(this->window_number), this->sort_criteria, WID_RV_SORT_DROPDOWN);
490 break;
492 case WID_RV_TRAIN_ENGINEWAGON_DROPDOWN: {
493 if (this->GetWidget<NWidgetLeaf>(widget)->ButtonHit(pt)) {
494 this->HandleButtonClick (WID_RV_TRAIN_ENGINEWAGON_DROPDOWN);
495 ReplaceClick_EngineWagonToggle (!this->replace_engines);
496 } else {
497 DropDownList *list = new DropDownList();
498 *list->Append() = new DropDownListStringItem (STR_REPLACE_ENGINES, 0, false);
499 *list->Append() = new DropDownListStringItem (STR_REPLACE_WAGONS, 1, false);
500 ShowDropDownList (this, list, this->replace_engines ? 0 : 1, WID_RV_TRAIN_ENGINEWAGON_DROPDOWN);
502 break;
505 case WID_RV_TRAIN_RAILTYPE_DROPDOWN: // Railtype selection dropdown menu
506 ShowDropDownList(this, GetRailTypeDropDownList(true, true), sel_railtype, WID_RV_TRAIN_RAILTYPE_DROPDOWN);
507 break;
509 case WID_RV_TRAIN_WAGONREMOVE_TOGGLE: // toggle renew_keep_length
510 DoCommandP(0, GetCompanySettingIndex("company.renew_keep_length"), Company::Get(_local_company)->settings.renew_keep_length ? 0 : 1, CMD_CHANGE_COMPANY_SETTING);
511 break;
513 case WID_RV_START_REPLACE: { // Start replacing
514 if (this->GetWidget<NWidgetLeaf>(widget)->ButtonHit(pt)) {
515 this->HandleButtonClick(WID_RV_START_REPLACE);
516 ReplaceClick_StartReplace(false);
517 } else {
518 bool replacment_when_old = EngineHasReplacementWhenOldForCompany(Company::Get(_local_company), this->sel_engine[0], this->sel_group);
519 ShowDropDownMenu(this, _start_replace_dropdown, replacment_when_old ? 1 : 0, WID_RV_START_REPLACE, !this->replace_engines ? 1 << 1 : 0, 0);
521 break;
524 case WID_RV_STOP_REPLACE: { // Stop replacing
525 EngineID veh_from = this->sel_engine[0];
526 DoCommandP(0, this->sel_group << 16, veh_from + (INVALID_ENGINE << 16), CMD_SET_AUTOREPLACE);
527 break;
530 case WID_RV_LEFT_MATRIX:
531 case WID_RV_RIGHT_MATRIX: {
532 byte click_side;
533 if (widget == WID_RV_LEFT_MATRIX) {
534 click_side = 0;
535 } else {
536 click_side = 1;
538 uint i = this->vscroll[click_side]->GetScrolledRowFromWidget(pt.y, this, widget);
539 size_t engine_count = this->engines[click_side].Length();
541 EngineID e = engine_count > i ? this->engines[click_side][i] : INVALID_ENGINE;
542 if (e == this->sel_engine[click_side]) break; // we clicked the one we already selected
543 this->sel_engine[click_side] = e;
544 if (click_side == 0) {
545 this->engines[1].ForceRebuild();
546 this->reset_sel_engine = true;
548 this->SetDirty();
549 break;
554 virtual void OnDropdownSelect(int widget, int index)
556 switch (widget) {
557 case WID_RV_SORT_DROPDOWN:
558 if (this->sort_criteria != index) {
559 this->sort_criteria = index;
560 _engine_sort_last_criteria[this->window_number] = this->sort_criteria;
561 this->engines[1].ForceRebuild();
562 this->SetDirty();
564 break;
566 case WID_RV_TRAIN_RAILTYPE_DROPDOWN: {
567 RailType temp = (RailType)index;
568 if (temp == sel_railtype) return; // we didn't select a new one. No need to change anything
569 sel_railtype = temp;
570 /* Reset scrollbar positions */
571 this->vscroll[0]->SetPosition(0);
572 this->vscroll[1]->SetPosition(0);
573 /* Rebuild the lists */
574 this->engines[0].ForceRebuild();
575 this->engines[1].ForceRebuild();
576 this->reset_sel_engine = true;
577 this->SetDirty();
578 break;
581 case WID_RV_TRAIN_ENGINEWAGON_DROPDOWN: {
582 bool engines = (index == 0);
583 if (engines == this->replace_engines) return;
584 this->ReplaceClick_EngineWagonToggle (engines);
585 break;
588 case WID_RV_START_REPLACE:
589 this->ReplaceClick_StartReplace(index != 0);
590 break;
594 virtual void OnResize()
596 this->vscroll[0]->SetCapacityFromWidget(this, WID_RV_LEFT_MATRIX);
597 this->vscroll[1]->SetCapacityFromWidget(this, WID_RV_RIGHT_MATRIX);
601 * Some data on this window has become invalid.
602 * @param data Information about the changed data.
603 * @param gui_scope Whether the call is done from GUI scope. You may not do everything when not in GUI scope. See #InvalidateWindowData() for details.
605 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
607 if (data != 0) {
608 /* This needs to be done in command-scope to enforce rebuilding before resorting invalid data */
609 this->engines[0].ForceRebuild();
610 } else {
611 this->engines[1].ForceRebuild();
616 static const NWidgetPart _nested_replace_rail_vehicle_widgets[] = {
617 NWidget(NWID_HORIZONTAL),
618 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
619 NWidget(WWT_CAPTION, COLOUR_GREY, WID_RV_CAPTION), SetDataTip(STR_REPLACE_VEHICLES_WHITE, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
620 NWidget(WWT_SHADEBOX, COLOUR_GREY),
621 NWidget(WWT_DEFSIZEBOX, COLOUR_GREY),
622 NWidget(WWT_STICKYBOX, COLOUR_GREY),
623 EndContainer(),
624 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
625 NWidget(WWT_PANEL, COLOUR_GREY),
626 NWidget(WWT_LABEL, COLOUR_GREY), SetDataTip(STR_REPLACE_VEHICLE_VEHICLES_IN_USE, STR_REPLACE_VEHICLE_VEHICLES_IN_USE_TOOLTIP), SetFill(1, 1), SetMinimalSize(0, 12), SetResize(1, 0),
627 EndContainer(),
628 NWidget(WWT_PANEL, COLOUR_GREY),
629 NWidget(WWT_LABEL, COLOUR_GREY), SetDataTip(STR_REPLACE_VEHICLE_AVAILABLE_VEHICLES, STR_REPLACE_VEHICLE_AVAILABLE_VEHICLES_TOOLTIP), SetFill(1, 1), SetMinimalSize(0, 12), SetResize(1, 0),
630 EndContainer(),
631 EndContainer(),
632 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
633 NWidget(NWID_HORIZONTAL),
634 NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_RV_TRAIN_RAILTYPE_DROPDOWN), SetDataTip(0x0, STR_REPLACE_HELP_RAILTYPE), SetResize(1, 0), SetFill(1, 1),
635 NWidget(NWID_PUSHBUTTON_DROPDOWN, COLOUR_GREY, WID_RV_TRAIN_ENGINEWAGON_DROPDOWN), SetDataTip(STR_JUST_STRING, STR_REPLACE_ENGINE_WAGON_SELECT_HELP), SetMinimalSize(80, 12),
636 EndContainer(),
637 NWidget(NWID_VERTICAL),
638 NWidget(NWID_HORIZONTAL),
639 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_RV_TRAIN_WAGONREMOVE_TOGGLE), SetMinimalSize(120, 12), SetDataTip(STR_REPLACE_REMOVE_WAGON, STR_REPLACE_REMOVE_WAGON_HELP), SetResize(1, 0), SetFill(1, 0),
640 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_RV_SHOW_HIDDEN_ENGINES), SetDataTip(STR_SHOW_HIDDEN_ENGINES_VEHICLE_TRAIN, STR_SHOW_HIDDEN_ENGINES_VEHICLE_TRAIN_TOOLTIP), SetResize(1, 0), SetFill(1, 0),
641 EndContainer(),
642 EndContainer(),
643 EndContainer(),
644 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
645 NWidget(WWT_PANEL, COLOUR_GREY), SetResize(1, 0), EndContainer(),
646 NWidget(NWID_HORIZONTAL),
647 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_RV_SORT_ASCENDING_DESCENDING), SetDataTip(STR_BUTTON_SORT_BY, STR_TOOLTIP_SORT_ORDER), SetFill(1, 0),
648 NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_RV_SORT_DROPDOWN), SetResize(1, 0), SetFill(1, 0), SetDataTip(STR_JUST_STRING, STR_TOOLTIP_SORT_CRITERIA),
649 EndContainer(),
650 EndContainer(),
651 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
652 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),
653 NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_RV_LEFT_SCROLLBAR),
654 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),
655 NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_RV_RIGHT_SCROLLBAR),
656 EndContainer(),
657 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
658 NWidget(WWT_PANEL, COLOUR_GREY, WID_RV_LEFT_DETAILS), SetMinimalSize(240, 122), SetResize(1, 0), EndContainer(),
659 NWidget(WWT_PANEL, COLOUR_GREY, WID_RV_RIGHT_DETAILS), SetMinimalSize(240, 122), SetResize(1, 0), EndContainer(),
660 EndContainer(),
661 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
662 NWidget(WWT_PANEL, COLOUR_GREY, WID_RV_INFO_TAB), SetMinimalSize(240, 24), SetDataTip(0x0, STR_REPLACE_HELP_REPLACE_INFO_TAB), SetResize(1, 0), EndContainer(),
663 NWidget(NWID_VERTICAL),
664 NWidget(NWID_PUSHBUTTON_DROPDOWN, COLOUR_GREY, WID_RV_START_REPLACE), SetMinimalSize(240, 12), SetDataTip(STR_REPLACE_VEHICLES_START, STR_REPLACE_HELP_START_BUTTON), SetResize(1, 0),
665 NWidget(NWID_HORIZONTAL),
666 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_RV_STOP_REPLACE), SetMinimalSize(228, 12), SetDataTip(STR_REPLACE_VEHICLES_STOP, STR_REPLACE_HELP_STOP_BUTTON), SetResize(1, 0),
667 NWidget(WWT_RESIZEBOX, COLOUR_GREY),
668 EndContainer(),
669 EndContainer(),
670 EndContainer(),
673 static WindowDesc::Prefs _replace_rail_vehicle_prefs ("replace_vehicle_train");
675 static const WindowDesc _replace_rail_vehicle_desc(
676 WDP_AUTO, 500, 140,
677 WC_REPLACE_VEHICLE, WC_NONE,
678 WDF_CONSTRUCTION,
679 _nested_replace_rail_vehicle_widgets, lengthof(_nested_replace_rail_vehicle_widgets),
680 &_replace_rail_vehicle_prefs
683 static const NWidgetPart _nested_replace_vehicle_widgets[] = {
684 NWidget(NWID_HORIZONTAL),
685 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
686 NWidget(WWT_CAPTION, COLOUR_GREY, WID_RV_CAPTION), SetDataTip(STR_REPLACE_VEHICLES_WHITE, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
687 NWidget(WWT_SHADEBOX, COLOUR_GREY),
688 NWidget(WWT_DEFSIZEBOX, COLOUR_GREY),
689 NWidget(WWT_STICKYBOX, COLOUR_GREY),
690 EndContainer(),
691 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
692 NWidget(WWT_PANEL, COLOUR_GREY),
693 NWidget(WWT_LABEL, COLOUR_GREY), SetDataTip(STR_REPLACE_VEHICLE_VEHICLES_IN_USE, STR_REPLACE_VEHICLE_VEHICLES_IN_USE_TOOLTIP), SetFill(1, 1), SetMinimalSize(0, 12), SetResize(1, 0),
694 EndContainer(),
695 NWidget(WWT_PANEL, COLOUR_GREY),
696 NWidget(WWT_LABEL, COLOUR_GREY), SetDataTip(STR_REPLACE_VEHICLE_AVAILABLE_VEHICLES, STR_REPLACE_VEHICLE_AVAILABLE_VEHICLES_TOOLTIP), SetFill(1, 1), SetMinimalSize(0, 12), SetResize(1, 0),
697 EndContainer(),
698 EndContainer(),
699 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
700 NWidget(WWT_PANEL, COLOUR_GREY), SetResize(1, 0), EndContainer(),
701 NWidget(NWID_VERTICAL),
702 NWidget(NWID_HORIZONTAL),
703 NWidget(WWT_PANEL, COLOUR_GREY), SetResize(1, 0), SetFill(1, 1), EndContainer(),
704 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_RV_SHOW_HIDDEN_ENGINES), SetDataTip(STR_SHOW_HIDDEN_ENGINES_VEHICLE_TRAIN, STR_SHOW_HIDDEN_ENGINES_VEHICLE_TRAIN_TOOLTIP), SetMinimalSize(80, 12),
705 EndContainer(),
706 NWidget(NWID_HORIZONTAL),
707 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_RV_SORT_ASCENDING_DESCENDING), SetDataTip(STR_BUTTON_SORT_BY, STR_TOOLTIP_SORT_ORDER),
708 NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_RV_SORT_DROPDOWN), SetResize(1, 0), SetFill(1, 1), SetDataTip(STR_JUST_STRING, STR_TOOLTIP_SORT_CRITERIA),
709 EndContainer(),
710 EndContainer(),
711 EndContainer(),
712 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
713 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),
714 NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_RV_LEFT_SCROLLBAR),
715 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),
716 NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_RV_RIGHT_SCROLLBAR),
717 EndContainer(),
718 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
719 NWidget(WWT_PANEL, COLOUR_GREY, WID_RV_LEFT_DETAILS), SetMinimalSize(228, 92), SetResize(1, 0), EndContainer(),
720 NWidget(WWT_PANEL, COLOUR_GREY, WID_RV_RIGHT_DETAILS), SetMinimalSize(228, 92), SetResize(1, 0), EndContainer(),
721 EndContainer(),
722 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
723 NWidget(WWT_PANEL, COLOUR_GREY, WID_RV_INFO_TAB), SetMinimalSize(228, 24), SetDataTip(0x0, STR_REPLACE_HELP_REPLACE_INFO_TAB), SetResize(1, 0), EndContainer(),
724 NWidget(NWID_VERTICAL),
725 NWidget(NWID_PUSHBUTTON_DROPDOWN, COLOUR_GREY, WID_RV_START_REPLACE), SetMinimalSize(228, 12), SetDataTip(STR_REPLACE_VEHICLES_START, STR_REPLACE_HELP_START_BUTTON), SetResize(1, 0),
726 NWidget(NWID_HORIZONTAL),
727 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_RV_STOP_REPLACE), SetMinimalSize(216, 12), SetDataTip(STR_REPLACE_VEHICLES_STOP, STR_REPLACE_HELP_STOP_BUTTON), SetResize(1, 0),
728 NWidget(WWT_RESIZEBOX, COLOUR_GREY),
729 EndContainer(),
730 EndContainer(),
731 EndContainer(),
734 static WindowDesc::Prefs _replace_vehicle_prefs ("replace_vehicle");
736 static const WindowDesc _replace_vehicle_desc(
737 WDP_AUTO, 456, 118,
738 WC_REPLACE_VEHICLE, WC_NONE,
739 WDF_CONSTRUCTION,
740 _nested_replace_vehicle_widgets, lengthof(_nested_replace_vehicle_widgets),
741 &_replace_vehicle_prefs
745 * Show the autoreplace configuration window for a particular group.
746 * @param id_g The group to replace the vehicles for.
747 * @param vehicletype The type of vehicles in the group.
749 void ShowReplaceGroupVehicleWindow(GroupID id_g, VehicleType vehicletype)
751 DeleteWindowById(WC_REPLACE_VEHICLE, vehicletype);
752 new ReplaceVehicleWindow(vehicletype == VEH_TRAIN ? &_replace_rail_vehicle_desc : &_replace_vehicle_desc, vehicletype, id_g);