Add templated versions of CeilDiv and Ceil maths functions
[openttd-joker.git] / src / autoreplace_gui.cpp
blob6e17bb1afafacc2a59fc936cf64492d0213dc8e7
1 /* $Id: autoreplace_gui.cpp 25919 2013-10-28 10:28:24Z frosch $ */
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 #include "safeguards.h"
34 static int CDECL EngineNumberSorter(const EngineID *a, const EngineID *b)
36 int r = Engine::Get(*a)->list_position - Engine::Get(*b)->list_position;
38 return r;
41 /**
42 * Rebuild the left autoreplace list if an engine is removed or added
43 * @param e Engine to check if it is removed or added
44 * @param id_g The group the engine belongs to
45 * Note: this function only works if it is called either
46 * - when a new vehicle is build, but before it's counted in num_engines
47 * - when a vehicle is deleted and after it's subtracted from num_engines
48 * - when not changing the count (used when changing replace orders)
50 void InvalidateAutoreplaceWindow(EngineID e, GroupID id_g)
52 if (GetGroupNumEngines(_local_company, id_g, e) == 0 || GetGroupNumEngines(_local_company, ALL_GROUP, e) == 0) {
53 /* We don't have any of this engine type.
54 * Either we just sold the last one, we build a new one or we stopped replacing it.
55 * In all cases, we need to update the left list */
56 InvalidateWindowData(WC_REPLACE_VEHICLE, Engine::Get(e)->type, 1);
60 /**
61 * When an engine is made buildable or is removed from being buildable, add/remove it from the build/autoreplace lists
62 * @param type The type of engine
64 void AddRemoveEngineFromAutoreplaceAndBuildWindows(VehicleType type)
66 InvalidateWindowData(WC_REPLACE_VEHICLE, type, 0); // Update the autoreplace window
67 InvalidateWindowClassesData(WC_BUILD_VEHICLE); // The build windows needs updating as well
68 InvalidateWindowClassesData(WC_BUILD_VIRTUAL_TRAIN); // The build windows for virtual trains 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 static const StringID _start_replace_only_when_old_dropdown[] = {
78 STR_REPLACE_VEHICLES_WHEN_OLD,
79 INVALID_STRING_ID
82 /**
83 * Window for the autoreplacing of vehicles.
85 class ReplaceVehicleWindow : public Window {
86 EngineID sel_engine[2]; ///< Selected engine left and right.
87 GUIEngineList engines[2]; ///< Left and right list of engines.
88 bool replace_engines; ///< If \c true, engines are replaced, if \c false, wagons are replaced (only for trains).
89 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.
90 GroupID sel_group; ///< Group selected to replace.
91 int details_height; ///< Minimal needed height of the details panels (found so far).
92 byte sort_criteria; ///< Criteria of sorting vehicles.
93 bool descending_sort_order; ///< Order of sorting vehicles.
94 bool show_hidden_engines; ///< Whether to show the hidden engines.
95 RailType sel_railtype; ///< Type of rail tracks selected. #INVALID_RAILTYPE to show all.
96 Scrollbar *vscroll[2];
98 /**
99 * Figure out if an engine should be added to a list.
100 * @param e The EngineID.
101 * @param draw_left If \c true, the left list is drawn (the engines specific to the railtype you selected).
102 * @param show_engines If \c true, the locomotives are drawn, else the wagons are drawn (never both).
103 * @return \c true if the engine should be in the list (based on this check), else \c false.
105 bool GenerateReplaceRailList(EngineID e, bool draw_left, bool show_engines)
107 const RailVehicleInfo *rvi = RailVehInfo(e);
109 /* Ensure that the wagon/engine selection fits the engine. */
110 if ((rvi->railveh_type == RAILVEH_WAGON) == show_engines) return false;
112 if (draw_left && this->sel_railtype != INVALID_RAILTYPE) {
113 /* Ensure that the railtype is specific to the selected one */
114 if (rvi->railtype != this->sel_railtype) return false;
116 return true;
121 * Generate an engines list
122 * @param draw_left true if generating the left list, otherwise false
124 void GenerateReplaceVehList(bool draw_left)
126 EngineID selected_engine = INVALID_ENGINE;
127 VehicleType type = (VehicleType)this->window_number;
128 byte side = draw_left ? 0 : 1;
130 GUIEngineList *list = &this->engines[side];
131 list->Clear();
133 const Engine *e;
134 FOR_ALL_ENGINES_OF_TYPE(e, type) {
135 if (!draw_left && !this->show_hidden_engines && e->IsHidden(_local_company)) continue;
136 EngineID eid = e->index;
137 if (type == VEH_TRAIN && !this->GenerateReplaceRailList(eid, draw_left, this->replace_engines)) continue; // special rules for trains
139 if (draw_left) {
140 const uint num_engines = GetGroupNumEngines(_local_company, this->sel_group, eid);
142 /* Skip drawing the engines we don't have any of and haven't set for replacement */
143 if (num_engines == 0 && EngineReplacementForCompany(Company::Get(_local_company), eid, this->sel_group) == INVALID_ENGINE) continue;
144 } else {
145 if (!CheckAutoreplaceValidity(this->sel_engine[0], eid, _local_company)) continue;
148 *list->Append() = eid;
149 if (eid == this->sel_engine[side]) selected_engine = eid; // The selected engine is still in the list
151 this->sel_engine[side] = selected_engine; // update which engine we selected (the same or none, if it's not in the list anymore)
152 if (draw_left) {
153 EngList_Sort(list, &EngineNumberSorter);
154 } else {
155 _engine_sort_direction = this->descending_sort_order;
156 EngList_Sort(list, _engine_sort_functions[this->window_number][this->sort_criteria]);
160 /** Generate the lists */
161 void GenerateLists()
163 EngineID e = this->sel_engine[0];
165 if (this->engines[0].NeedRebuild()) {
166 /* We need to rebuild the left engines list */
167 this->GenerateReplaceVehList(true);
168 this->vscroll[0]->SetCount(this->engines[0].Length());
169 if (this->reset_sel_engine && this->sel_engine[0] == INVALID_ENGINE && this->engines[0].Length() != 0) {
170 this->sel_engine[0] = this->engines[0][0];
174 if (this->engines[1].NeedRebuild() || e != this->sel_engine[0]) {
175 /* Either we got a request to rebuild the right engines list, or the left engines list selected a different engine */
176 if (this->sel_engine[0] == INVALID_ENGINE) {
177 /* Always empty the right engines list when nothing is selected in the left engines list */
178 this->engines[1].Clear();
179 this->sel_engine[1] = INVALID_ENGINE;
180 } else {
181 if (this->reset_sel_engine && this->sel_engine[0] != INVALID_ENGINE) {
182 /* Select the current replacement for sel_engine[0]. */
183 const Company *c = Company::Get(_local_company);
184 this->sel_engine[1] = EngineReplacementForCompany(c, this->sel_engine[0], this->sel_group);
186 /* Regenerate the list on the right. Note: This resets sel_engine[1] to INVALID_ENGINE, if it is no longer available. */
187 this->GenerateReplaceVehList(false);
188 this->vscroll[1]->SetCount(this->engines[1].Length());
189 if (this->reset_sel_engine && this->sel_engine[1] != INVALID_ENGINE) {
190 int position = 0;
191 for (EngineID *it = this->engines[1].Begin(); it != this->engines[1].End(); ++it) {
192 if (*it == this->sel_engine[1]) break;
193 ++position;
195 this->vscroll[1]->ScrollTowards(position);
199 /* Reset the flags about needed updates */
200 this->engines[0].RebuildDone();
201 this->engines[1].RebuildDone();
202 this->reset_sel_engine = false;
206 * Handle click on the start replace button.
207 * @param replace_when_old Replace now or only when old?
209 void ReplaceClick_StartReplace(bool replace_when_old)
211 EngineID veh_from = this->sel_engine[0];
212 EngineID veh_to = this->sel_engine[1];
213 DoCommandP(0, (replace_when_old ? 1 : 0) | (this->sel_group << 16), veh_from + (veh_to << 16), CMD_SET_AUTOREPLACE);
216 public:
217 ReplaceVehicleWindow(WindowDesc *desc, VehicleType vehicletype, GroupID id_g) : Window(desc)
219 this->sel_railtype = INVALID_RAILTYPE;
220 this->replace_engines = true; // start with locomotives (all other vehicles will not read this bool)
221 this->engines[0].ForceRebuild();
222 this->engines[1].ForceRebuild();
223 this->reset_sel_engine = true;
224 this->details_height = ((vehicletype == VEH_TRAIN) ? 10 : 9) * FONT_HEIGHT_NORMAL + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
225 this->sel_engine[0] = INVALID_ENGINE;
226 this->sel_engine[1] = INVALID_ENGINE;
227 this->show_hidden_engines = _engine_sort_show_hidden_engines[vehicletype];
229 this->CreateNestedTree();
230 this->vscroll[0] = this->GetScrollbar(WID_RV_LEFT_SCROLLBAR);
231 this->vscroll[1] = this->GetScrollbar(WID_RV_RIGHT_SCROLLBAR);
233 NWidgetCore *widget = this->GetWidget<NWidgetCore>(WID_RV_SHOW_HIDDEN_ENGINES);
234 widget->widget_data = STR_SHOW_HIDDEN_ENGINES_VEHICLE_TRAIN + vehicletype;
235 widget->tool_tip = STR_SHOW_HIDDEN_ENGINES_VEHICLE_TRAIN_TOOLTIP + vehicletype;
236 widget->SetLowered(this->show_hidden_engines);
237 this->FinishInitNested(vehicletype);
239 this->sort_criteria = _engine_sort_last_criteria[vehicletype];
240 this->descending_sort_order = _engine_sort_last_order[vehicletype];
241 this->owner = _local_company;
242 this->sel_group = id_g;
245 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
247 switch (widget) {
248 case WID_RV_SORT_ASCENDING_DESCENDING: {
249 Dimension d = GetStringBoundingBox(this->GetWidget<NWidgetCore>(widget)->widget_data);
250 d.width += padding.width + Window::SortButtonWidth() * 2; // Doubled since the string is centred and it also looks better.
251 d.height += padding.height;
252 *size = maxdim(*size, d);
253 break;
256 case WID_RV_LEFT_MATRIX:
257 case WID_RV_RIGHT_MATRIX:
258 resize->height = GetEngineListHeight((VehicleType)this->window_number);
259 size->height = (this->window_number <= VEH_ROAD ? 8 : 4) * resize->height;
260 break;
262 case WID_RV_LEFT_DETAILS:
263 case WID_RV_RIGHT_DETAILS:
264 size->height = this->details_height;
265 break;
267 case WID_RV_TRAIN_WAGONREMOVE_TOGGLE: {
268 StringID str = this->GetWidget<NWidgetCore>(widget)->widget_data;
269 SetDParam(0, STR_CONFIG_SETTING_ON);
270 Dimension d = GetStringBoundingBox(str);
271 SetDParam(0, STR_CONFIG_SETTING_OFF);
272 d = maxdim(d, GetStringBoundingBox(str));
273 d.width += padding.width;
274 d.height += padding.height;
275 *size = maxdim(*size, d);
276 break;
279 case WID_RV_TRAIN_ENGINEWAGON_DROPDOWN: {
280 Dimension d = GetStringBoundingBox(STR_REPLACE_ENGINES);
281 d = maxdim(d, GetStringBoundingBox(STR_REPLACE_WAGONS));
282 d.width += padding.width;
283 d.height += padding.height;
284 *size = maxdim(*size, d);
285 break;
288 case WID_RV_INFO_TAB: {
289 Dimension d = GetStringBoundingBox(STR_REPLACE_NOT_REPLACING);
290 d = maxdim(d, GetStringBoundingBox(STR_REPLACE_NOT_REPLACING_VEHICLE_SELECTED));
291 d.width += WD_FRAMETEXT_LEFT + WD_FRAMETEXT_RIGHT;
292 d.height += WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
293 *size = maxdim(*size, d);
294 break;
297 case WID_RV_TRAIN_RAILTYPE_DROPDOWN: {
298 Dimension d = {0, 0};
299 for (RailType rt = RAILTYPE_BEGIN; rt != RAILTYPE_END; rt++) {
300 const RailtypeInfo *rti = GetRailTypeInfo(rt);
301 /* Skip rail type if it has no label */
302 if (rti->label == 0) continue;
303 d = maxdim(d, GetStringBoundingBox(rti->strings.replace_text));
305 d.width += padding.width;
306 d.height += padding.height;
307 *size = maxdim(*size, d);
308 break;
311 case WID_RV_START_REPLACE: {
312 Dimension d = GetStringBoundingBox(STR_REPLACE_VEHICLES_START);
313 for (int i = 0; _start_replace_dropdown[i] != INVALID_STRING_ID; i++) {
314 d = maxdim(d, GetStringBoundingBox(_start_replace_dropdown[i]));
316 d.width += padding.width;
317 d.height += padding.height;
318 *size = maxdim(*size, d);
319 break;
324 virtual void SetStringParameters(int widget) const
326 switch (widget) {
327 case WID_RV_CAPTION:
328 SetDParam(0, STR_REPLACE_VEHICLE_TRAIN + this->window_number);
329 switch (this->sel_group) {
330 case ALL_GROUP:
331 SetDParam(1, STR_GROUP_ALL_TRAINS + this->window_number);
332 break;
334 case DEFAULT_GROUP:
335 SetDParam(1, STR_GROUP_DEFAULT_TRAINS + this->window_number);
336 break;
338 default:
339 SetDParam(1, STR_GROUP_NAME);
340 SetDParam(2, sel_group);
341 break;
343 break;
345 case WID_RV_SORT_DROPDOWN:
346 SetDParam(0, _engine_sort_listing[this->window_number][this->sort_criteria]);
347 break;
349 case WID_RV_TRAIN_WAGONREMOVE_TOGGLE: {
350 const Company *c = Company::Get(_local_company);
351 SetDParam(0, c->settings.renew_keep_length ? STR_CONFIG_SETTING_ON : STR_CONFIG_SETTING_OFF);
352 break;
355 case WID_RV_TRAIN_ENGINEWAGON_DROPDOWN:
356 SetDParam(0, this->replace_engines ? STR_REPLACE_ENGINES : STR_REPLACE_WAGONS);
357 break;
361 virtual void DrawWidget(const Rect &r, int widget) const
363 switch (widget) {
364 case WID_RV_SORT_ASCENDING_DESCENDING:
365 this->DrawSortButtonState(WID_RV_SORT_ASCENDING_DESCENDING, this->descending_sort_order ? SBS_DOWN : SBS_UP);
366 break;
368 case WID_RV_INFO_TAB: {
369 const Company *c = Company::Get(_local_company);
370 StringID str;
371 if (this->sel_engine[0] != INVALID_ENGINE) {
372 if (!EngineHasReplacementForCompany(c, this->sel_engine[0], this->sel_group)) {
373 str = STR_REPLACE_NOT_REPLACING;
374 } else {
375 bool when_old = false;
376 EngineID e = EngineReplacementForCompany(c, this->sel_engine[0], this->sel_group, &when_old);
377 str = when_old ? STR_REPLACE_REPLACING_WHEN_OLD : STR_ENGINE_NAME;
378 SetDParam(0, e);
380 } else {
381 str = STR_REPLACE_NOT_REPLACING_VEHICLE_SELECTED;
384 DrawString(r.left + WD_FRAMETEXT_LEFT, r.right - WD_FRAMETEXT_RIGHT, r.top + WD_FRAMERECT_TOP, str, TC_BLACK, SA_HOR_CENTER);
385 break;
388 case WID_RV_LEFT_MATRIX:
389 case WID_RV_RIGHT_MATRIX: {
390 int side = (widget == WID_RV_LEFT_MATRIX) ? 0 : 1;
391 EngineID start = this->vscroll[side]->GetPosition(); // what is the offset for the start (scrolling)
392 EngineID end = min(this->vscroll[side]->GetCapacity() + start, this->engines[side].Length());
394 /* Do the actual drawing */
395 DrawEngineList((VehicleType)this->window_number, r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, r.top + WD_FRAMERECT_TOP,
396 &this->engines[side], start, end, this->sel_engine[side], side == 0, this->sel_group);
397 break;
402 virtual void OnPaint()
404 if (this->engines[0].NeedRebuild() || this->engines[1].NeedRebuild()) this->GenerateLists();
406 Company *c = Company::Get(_local_company);
408 /* Disable the "Start Replacing" button if:
409 * Either engines list is empty
410 * or The selected replacement engine has a replacement (to prevent loops). */
411 this->SetWidgetDisabledState(WID_RV_START_REPLACE,
412 this->sel_engine[0] == INVALID_ENGINE || this->sel_engine[1] == INVALID_ENGINE || EngineReplacementForCompany(c, this->sel_engine[1], this->sel_group) != INVALID_ENGINE);
414 this->GetWidget<NWidgetCore>(WID_RV_START_REPLACE)->SetDataTip(_start_replace_dropdown[this->sel_engine[0] == this->sel_engine[1] ? 1 : 0], STR_NULL);
416 /* Disable the "Stop Replacing" button if:
417 * The left engines list (existing vehicle) is empty
418 * or The selected vehicle has no replacement set up */
419 this->SetWidgetDisabledState(WID_RV_STOP_REPLACE, this->sel_engine[0] == INVALID_ENGINE || !EngineHasReplacementForCompany(c, this->sel_engine[0], this->sel_group));
421 if (this->window_number == VEH_TRAIN) {
422 /* Show the selected railtype in the pulldown menu */
423 this->GetWidget<NWidgetCore>(WID_RV_TRAIN_RAILTYPE_DROPDOWN)->widget_data = sel_railtype == INVALID_RAILTYPE ? STR_REPLACE_ALL_RAILTYPE : GetRailTypeInfo(sel_railtype)->strings.replace_text;
426 this->DrawWidgets();
428 if (!this->IsShaded()) {
429 int needed_height = this->details_height;
430 /* Draw details panels. */
431 for (int side = 0; side < 2; side++) {
432 if (this->sel_engine[side] != INVALID_ENGINE) {
433 NWidgetBase *nwi = this->GetWidget<NWidgetBase>(side == 0 ? WID_RV_LEFT_DETAILS : WID_RV_RIGHT_DETAILS);
434 int text_end = DrawVehiclePurchaseInfo(nwi->pos_x + WD_FRAMETEXT_LEFT, nwi->pos_x + nwi->current_x - WD_FRAMETEXT_RIGHT,
435 nwi->pos_y + WD_FRAMERECT_TOP, this->sel_engine[side]);
436 needed_height = max(needed_height, text_end - (int)nwi->pos_y + WD_FRAMERECT_BOTTOM);
439 if (needed_height != this->details_height) { // Details window are not high enough, enlarge them.
440 this->details_height = needed_height;
441 this->ReInit();
442 return;
447 virtual void OnClick(Point pt, int widget, int click_count)
449 switch (widget) {
450 case WID_RV_SORT_ASCENDING_DESCENDING:
451 this->descending_sort_order ^= true;
452 _engine_sort_last_order[this->window_number] = this->descending_sort_order;
453 this->engines[1].ForceRebuild();
454 this->SetDirty();
455 break;
457 case WID_RV_SHOW_HIDDEN_ENGINES:
458 this->show_hidden_engines ^= true;
459 _engine_sort_show_hidden_engines[this->window_number] = this->show_hidden_engines;
460 this->engines[1].ForceRebuild();
461 this->SetWidgetLoweredState(widget, this->show_hidden_engines);
462 this->SetDirty();
463 break;
465 case WID_RV_SORT_DROPDOWN:
466 DisplayVehicleSortDropDown(this, static_cast<VehicleType>(this->window_number), this->sort_criteria, WID_RV_SORT_DROPDOWN);
467 break;
469 case WID_RV_TRAIN_ENGINEWAGON_DROPDOWN: {
470 DropDownList *list = new DropDownList();
471 *list->Append() = new DropDownListStringItem(STR_REPLACE_ENGINES, 1, false);
472 *list->Append() = new DropDownListStringItem(STR_REPLACE_WAGONS, 0, false);
473 ShowDropDownList(this, list, this->replace_engines ? 1 : 0, WID_RV_TRAIN_ENGINEWAGON_DROPDOWN);
474 break;
477 case WID_RV_TRAIN_RAILTYPE_DROPDOWN: // Railtype selection dropdown menu
478 ShowDropDownList(this, GetRailTypeDropDownList(true, true), sel_railtype, WID_RV_TRAIN_RAILTYPE_DROPDOWN);
479 break;
481 case WID_RV_TRAIN_WAGONREMOVE_TOGGLE: // toggle renew_keep_length
482 DoCommandP(0, GetCompanySettingIndex("company.renew_keep_length"), Company::Get(_local_company)->settings.renew_keep_length ? 0 : 1, CMD_CHANGE_COMPANY_SETTING);
483 break;
485 case WID_RV_START_REPLACE: { // Start replacing
486 if (this->GetWidget<NWidgetLeaf>(widget)->ButtonHit(pt)) {
487 this->HandleButtonClick(WID_RV_START_REPLACE);
488 ReplaceClick_StartReplace((this->sel_engine[0] == this->sel_engine[1]));
489 } else {
490 bool replacment_when_old = EngineHasReplacementWhenOldForCompany(Company::Get(_local_company), this->sel_engine[0], this->sel_group);
491 ShowDropDownMenu(
492 this,
493 (this->sel_engine[0] == this->sel_engine[1]) ? _start_replace_only_when_old_dropdown : _start_replace_dropdown,
494 (this->sel_engine[0] == this->sel_engine[1]) ? 0 : (replacment_when_old ? 1 : 0),
495 WID_RV_START_REPLACE,
496 !this->replace_engines ? 1 << 1 : 0,
499 break;
502 case WID_RV_STOP_REPLACE: { // Stop replacing
503 EngineID veh_from = this->sel_engine[0];
504 DoCommandP(0, this->sel_group << 16, veh_from + (INVALID_ENGINE << 16), CMD_SET_AUTOREPLACE);
505 break;
508 case WID_RV_LEFT_MATRIX:
509 case WID_RV_RIGHT_MATRIX: {
510 byte click_side;
511 if (widget == WID_RV_LEFT_MATRIX) {
512 click_side = 0;
513 } else {
514 click_side = 1;
516 uint i = this->vscroll[click_side]->GetScrolledRowFromWidget(pt.y, this, widget);
517 size_t engine_count = this->engines[click_side].Length();
519 EngineID e = engine_count > i ? this->engines[click_side][i] : INVALID_ENGINE;
520 if (e == this->sel_engine[click_side]) break; // we clicked the one we already selected
521 this->sel_engine[click_side] = e;
522 if (click_side == 0) {
523 this->engines[1].ForceRebuild();
524 this->reset_sel_engine = true;
526 this->SetDirty();
527 break;
532 virtual void OnDropdownSelect(int widget, int index)
534 switch (widget) {
535 case WID_RV_SORT_DROPDOWN:
536 if (this->sort_criteria != index) {
537 this->sort_criteria = index;
538 _engine_sort_last_criteria[this->window_number] = this->sort_criteria;
539 this->engines[1].ForceRebuild();
540 this->SetDirty();
542 break;
544 case WID_RV_TRAIN_RAILTYPE_DROPDOWN: {
545 RailType temp = (RailType)index;
546 if (temp == sel_railtype) return; // we didn't select a new one. No need to change anything
547 sel_railtype = temp;
548 /* Reset scrollbar positions */
549 this->vscroll[0]->SetPosition(0);
550 this->vscroll[1]->SetPosition(0);
551 /* Rebuild the lists */
552 this->engines[0].ForceRebuild();
553 this->engines[1].ForceRebuild();
554 this->reset_sel_engine = true;
555 this->SetDirty();
556 break;
559 case WID_RV_TRAIN_ENGINEWAGON_DROPDOWN: {
560 this->replace_engines = index != 0;
561 this->engines[0].ForceRebuild();
562 this->reset_sel_engine = true;
563 this->SetDirty();
564 break;
567 case WID_RV_START_REPLACE:
568 this->ReplaceClick_StartReplace((this->sel_engine[0] == this->sel_engine[1]) ? (index == 0) : (index != 0));
569 break;
573 virtual void OnResize()
575 this->vscroll[0]->SetCapacityFromWidget(this, WID_RV_LEFT_MATRIX);
576 this->vscroll[1]->SetCapacityFromWidget(this, WID_RV_RIGHT_MATRIX);
580 * Some data on this window has become invalid.
581 * @param data Information about the changed data.
582 * @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.
584 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
586 if (data != 0) {
587 /* This needs to be done in command-scope to enforce rebuilding before resorting invalid data */
588 this->engines[0].ForceRebuild();
589 } else {
590 this->engines[1].ForceRebuild();
595 static const NWidgetPart _nested_replace_rail_vehicle_widgets[] = {
596 NWidget(NWID_HORIZONTAL),
597 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
598 NWidget(WWT_CAPTION, COLOUR_GREY, WID_RV_CAPTION), SetDataTip(STR_REPLACE_VEHICLES_WHITE, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
599 NWidget(WWT_SHADEBOX, COLOUR_GREY),
600 NWidget(WWT_DEFSIZEBOX, COLOUR_GREY),
601 NWidget(WWT_STICKYBOX, COLOUR_GREY),
602 EndContainer(),
603 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
604 NWidget(WWT_PANEL, COLOUR_GREY),
605 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),
606 EndContainer(),
607 NWidget(WWT_PANEL, COLOUR_GREY),
608 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),
609 EndContainer(),
610 EndContainer(),
611 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
612 NWidget(NWID_VERTICAL),
613 NWidget(NWID_HORIZONTAL),
614 NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_RV_TRAIN_RAILTYPE_DROPDOWN), SetMinimalSize(136, 12), SetDataTip(0x0, STR_REPLACE_HELP_RAILTYPE), SetFill(1, 0), SetResize(1, 0),
615 NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_RV_TRAIN_ENGINEWAGON_DROPDOWN), SetDataTip(STR_BLACK_STRING, STR_REPLACE_ENGINE_WAGON_SELECT_HELP),
616 EndContainer(),
617 NWidget(WWT_PANEL, COLOUR_GREY), SetResize(1, 0), EndContainer(),
618 EndContainer(),
619 NWidget(NWID_VERTICAL),
620 NWidget(NWID_HORIZONTAL),
621 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_RV_SORT_ASCENDING_DESCENDING), SetDataTip(STR_BUTTON_SORT_BY, STR_TOOLTIP_SORT_ORDER), SetFill(1, 1),
622 NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_RV_SORT_DROPDOWN), SetResize(1, 0), SetFill(1, 1), SetDataTip(STR_JUST_STRING, STR_TOOLTIP_SORT_CRITERIA),
623 EndContainer(),
624 NWidget(NWID_HORIZONTAL),
625 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_RV_SHOW_HIDDEN_ENGINES), SetDataTip(STR_SHOW_HIDDEN_ENGINES_VEHICLE_TRAIN, STR_SHOW_HIDDEN_ENGINES_VEHICLE_TRAIN_TOOLTIP),
626 NWidget(WWT_PANEL, COLOUR_GREY), SetResize(1, 0), SetFill(1, 1), EndContainer(),
627 EndContainer(),
628 EndContainer(),
629 EndContainer(),
630 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
631 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),
632 NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_RV_LEFT_SCROLLBAR),
633 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),
634 NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_RV_RIGHT_SCROLLBAR),
635 EndContainer(),
636 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
637 NWidget(WWT_PANEL, COLOUR_GREY, WID_RV_LEFT_DETAILS), SetMinimalSize(240, 122), SetResize(1, 0), EndContainer(),
638 NWidget(NWID_VERTICAL),
639 NWidget(WWT_PANEL, COLOUR_GREY, WID_RV_RIGHT_DETAILS), SetMinimalSize(240, 122), SetResize(1, 0), EndContainer(),
640 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_RV_TRAIN_WAGONREMOVE_TOGGLE), SetMinimalSize(138, 12), SetDataTip(STR_REPLACE_REMOVE_WAGON, STR_REPLACE_REMOVE_WAGON_HELP), SetFill(1, 0), SetResize(1, 0),
641 EndContainer(),
642 EndContainer(),
643 NWidget(NWID_HORIZONTAL),
644 NWidget(NWID_PUSHBUTTON_DROPDOWN, COLOUR_GREY, WID_RV_START_REPLACE), SetMinimalSize(139, 12), SetDataTip(STR_REPLACE_VEHICLES_START, STR_REPLACE_HELP_START_BUTTON),
645 NWidget(WWT_PANEL, COLOUR_GREY, WID_RV_INFO_TAB), SetMinimalSize(167, 12), SetDataTip(0x0, STR_REPLACE_HELP_REPLACE_INFO_TAB), SetResize(1, 0),
646 EndContainer(),
647 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_RV_STOP_REPLACE), SetMinimalSize(150, 12), SetDataTip(STR_REPLACE_VEHICLES_STOP, STR_REPLACE_HELP_STOP_BUTTON),
648 NWidget(WWT_RESIZEBOX, COLOUR_GREY),
649 EndContainer(),
652 static WindowDesc _replace_rail_vehicle_desc(
653 WDP_AUTO, "replace_vehicle_train", 500, 140,
654 WC_REPLACE_VEHICLE, WC_NONE,
655 WDF_CONSTRUCTION,
656 _nested_replace_rail_vehicle_widgets, lengthof(_nested_replace_rail_vehicle_widgets)
659 static const NWidgetPart _nested_replace_vehicle_widgets[] = {
660 NWidget(NWID_HORIZONTAL),
661 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
662 NWidget(WWT_CAPTION, COLOUR_GREY, WID_RV_CAPTION), SetMinimalSize(433, 14), SetDataTip(STR_REPLACE_VEHICLES_WHITE, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
663 NWidget(WWT_SHADEBOX, COLOUR_GREY),
664 NWidget(WWT_DEFSIZEBOX, COLOUR_GREY),
665 NWidget(WWT_STICKYBOX, COLOUR_GREY),
666 EndContainer(),
667 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
668 NWidget(WWT_PANEL, COLOUR_GREY),
669 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),
670 EndContainer(),
671 NWidget(WWT_PANEL, COLOUR_GREY),
672 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),
673 EndContainer(),
674 EndContainer(),
675 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
676 NWidget(WWT_PANEL, COLOUR_GREY), SetResize(1, 0), EndContainer(),
677 NWidget(NWID_VERTICAL),
678 NWidget(NWID_HORIZONTAL),
679 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_RV_SORT_ASCENDING_DESCENDING), SetDataTip(STR_BUTTON_SORT_BY, STR_TOOLTIP_SORT_ORDER),
680 NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_RV_SORT_DROPDOWN), SetResize(1, 0), SetFill(1, 1), SetDataTip(STR_JUST_STRING, STR_TOOLTIP_SORT_CRITERIA),
681 EndContainer(),
682 NWidget(NWID_HORIZONTAL),
683 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_RV_SHOW_HIDDEN_ENGINES), SetDataTip(STR_SHOW_HIDDEN_ENGINES_VEHICLE_TRAIN, STR_SHOW_HIDDEN_ENGINES_VEHICLE_TRAIN_TOOLTIP),
684 NWidget(WWT_PANEL, COLOUR_GREY), SetResize(1, 0), SetFill(1, 1), EndContainer(),
685 EndContainer(),
686 EndContainer(),
687 EndContainer(),
688 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
689 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),
690 NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_RV_LEFT_SCROLLBAR),
691 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),
692 NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_RV_RIGHT_SCROLLBAR),
693 EndContainer(),
694 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
695 NWidget(WWT_PANEL, COLOUR_GREY, WID_RV_LEFT_DETAILS), SetMinimalSize(228, 92), SetResize(1, 0), EndContainer(),
696 NWidget(WWT_PANEL, COLOUR_GREY, WID_RV_RIGHT_DETAILS), SetMinimalSize(228, 92), SetResize(1, 0), EndContainer(),
697 EndContainer(),
698 NWidget(NWID_HORIZONTAL),
699 NWidget(NWID_PUSHBUTTON_DROPDOWN, COLOUR_GREY, WID_RV_START_REPLACE), SetMinimalSize(139, 12), SetDataTip(STR_REPLACE_VEHICLES_START, STR_REPLACE_HELP_START_BUTTON),
700 NWidget(WWT_PANEL, COLOUR_GREY, WID_RV_INFO_TAB), SetMinimalSize(167, 12), SetDataTip(0x0, STR_REPLACE_HELP_REPLACE_INFO_TAB), SetResize(1, 0), EndContainer(),
701 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_RV_STOP_REPLACE), SetMinimalSize(138, 12), SetDataTip(STR_REPLACE_VEHICLES_STOP, STR_REPLACE_HELP_STOP_BUTTON),
702 NWidget(WWT_RESIZEBOX, COLOUR_GREY),
703 EndContainer(),
706 static WindowDesc _replace_vehicle_desc(
707 WDP_AUTO, "replace_vehicle", 456, 118,
708 WC_REPLACE_VEHICLE, WC_NONE,
709 WDF_CONSTRUCTION,
710 _nested_replace_vehicle_widgets, lengthof(_nested_replace_vehicle_widgets)
714 * Show the autoreplace configuration window for a particular group.
715 * @param id_g The group to replace the vehicles for.
716 * @param vehicletype The type of vehicles in the group.
718 void ShowReplaceGroupVehicleWindow(GroupID id_g, VehicleType vehicletype)
720 DeleteWindowById(WC_REPLACE_VEHICLE, vehicletype);
721 new ReplaceVehicleWindow(vehicletype == VEH_TRAIN ? &_replace_rail_vehicle_desc : &_replace_vehicle_desc, vehicletype, id_g);