Fix 03cc0d6: Mark level crossings dirty when removing road from them, not from bridge...
[openttd-github.git] / src / group_gui.cpp
blob197be9e2e7934da891a784831d4d1b3312477665
1 /*
2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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/>.
6 */
8 /** @file group_gui.cpp GUI for the group window. */
10 #include "stdafx.h"
11 #include "textbuf_gui.h"
12 #include "command_func.h"
13 #include "vehicle_gui.h"
14 #include "vehicle_base.h"
15 #include "string_func.h"
16 #include "strings_func.h"
17 #include "window_func.h"
18 #include "vehicle_func.h"
19 #include "autoreplace_gui.h"
20 #include "company_func.h"
21 #include "widgets/dropdown_func.h"
22 #include "tilehighlight_func.h"
23 #include "vehicle_gui_base.h"
24 #include "core/geometry_func.hpp"
25 #include "company_base.h"
26 #include "company_gui.h"
27 #include "gui.h"
28 #include "group_cmd.h"
29 #include "vehicle_cmd.h"
31 #include "widgets/group_widget.h"
33 #include "table/sprites.h"
35 #include "safeguards.h"
37 static const int LEVEL_WIDTH = 10; ///< Indenting width of a sub-group in pixels
39 typedef GUIList<const Group*> GUIGroupList;
41 static const NWidgetPart _nested_group_widgets[] = {
42 NWidget(NWID_HORIZONTAL), // Window header
43 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
44 NWidget(WWT_CAPTION, COLOUR_GREY, WID_GL_CAPTION),
45 NWidget(WWT_SHADEBOX, COLOUR_GREY),
46 NWidget(WWT_DEFSIZEBOX, COLOUR_GREY),
47 NWidget(WWT_STICKYBOX, COLOUR_GREY),
48 EndContainer(),
49 NWidget(NWID_HORIZONTAL),
50 /* left part */
51 NWidget(NWID_VERTICAL, NC_BIGFIRST),
52 NWidget(WWT_PANEL, COLOUR_GREY, WID_GL_ALL_VEHICLES), SetFill(1, 0), EndContainer(),
53 NWidget(WWT_PANEL, COLOUR_GREY, WID_GL_DEFAULT_VEHICLES), SetFill(1, 0), EndContainer(),
54 NWidget(NWID_HORIZONTAL),
55 NWidget(WWT_MATRIX, COLOUR_GREY, WID_GL_LIST_GROUP), SetMatrixDataTip(1, 0, STR_GROUPS_CLICK_ON_GROUP_FOR_TOOLTIP),
56 SetFill(1, 0), SetResize(0, 1), SetScrollbar(WID_GL_LIST_GROUP_SCROLLBAR),
57 NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_GL_LIST_GROUP_SCROLLBAR),
58 EndContainer(),
59 NWidget(WWT_PANEL, COLOUR_GREY, WID_GL_INFO), SetFill(1, 1), SetMinimalTextLines(3, WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM), EndContainer(),
60 NWidget(NWID_HORIZONTAL),
61 NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_GL_CREATE_GROUP),
62 SetDataTip(SPR_GROUP_CREATE_TRAIN, STR_GROUP_CREATE_TOOLTIP),
63 NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_GL_DELETE_GROUP),
64 SetDataTip(SPR_GROUP_DELETE_TRAIN, STR_GROUP_DELETE_TOOLTIP),
65 NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_GL_RENAME_GROUP),
66 SetDataTip(SPR_GROUP_RENAME_TRAIN, STR_GROUP_RENAME_TOOLTIP),
67 NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_GL_LIVERY_GROUP),
68 SetDataTip(SPR_GROUP_LIVERY_TRAIN, STR_GROUP_LIVERY_TOOLTIP),
69 NWidget(WWT_PANEL, COLOUR_GREY), SetFill(1, 0), EndContainer(),
70 NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_GL_REPLACE_PROTECTION),
71 SetDataTip(SPR_GROUP_REPLACE_OFF_TRAIN, STR_GROUP_REPLACE_PROTECTION_TOOLTIP),
72 EndContainer(),
73 EndContainer(),
74 /* right part */
75 NWidget(NWID_VERTICAL),
76 NWidget(NWID_HORIZONTAL),
77 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_GL_GROUP_BY_ORDER), SetMinimalSize(81, 12), SetDataTip(STR_STATION_VIEW_GROUP, STR_TOOLTIP_GROUP_ORDER),
78 NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_GL_GROUP_BY_DROPDOWN), SetMinimalSize(167, 12), SetDataTip(0x0, STR_TOOLTIP_GROUP_ORDER),
79 NWidget(WWT_PANEL, COLOUR_GREY), SetMinimalSize(12, 12), SetResize(1, 0), EndContainer(),
80 EndContainer(),
81 NWidget(NWID_HORIZONTAL),
82 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_GL_SORT_BY_ORDER), SetMinimalSize(81, 12), SetDataTip(STR_BUTTON_SORT_BY, STR_TOOLTIP_SORT_ORDER),
83 NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_GL_SORT_BY_DROPDOWN), SetMinimalSize(167, 12), SetDataTip(0x0, STR_TOOLTIP_SORT_CRITERIA),
84 NWidget(WWT_PANEL, COLOUR_GREY), SetMinimalSize(12, 12), SetResize(1, 0), EndContainer(),
85 EndContainer(),
86 NWidget(NWID_HORIZONTAL),
87 NWidget(WWT_MATRIX, COLOUR_GREY, WID_GL_LIST_VEHICLE), SetMinimalSize(248, 0), SetMatrixDataTip(1, 0, STR_NULL), SetResize(1, 1), SetFill(1, 0), SetScrollbar(WID_GL_LIST_VEHICLE_SCROLLBAR),
88 NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_GL_LIST_VEHICLE_SCROLLBAR),
89 EndContainer(),
90 NWidget(WWT_PANEL, COLOUR_GREY), SetMinimalSize(1, 0), SetFill(1, 1), SetResize(1, 0), EndContainer(),
91 NWidget(NWID_HORIZONTAL),
92 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_GL_AVAILABLE_VEHICLES), SetMinimalSize(106, 12),
93 SetDataTip(STR_BLACK_STRING, STR_VEHICLE_LIST_AVAILABLE_ENGINES_TOOLTIP),
94 NWidget(WWT_PANEL, COLOUR_GREY), SetMinimalSize(0, 12), SetFill(1, 0), SetResize(1, 0), EndContainer(),
95 NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_GL_MANAGE_VEHICLES_DROPDOWN), SetMinimalSize(118, 12),
96 SetDataTip(STR_VEHICLE_LIST_MANAGE_LIST, STR_VEHICLE_LIST_MANAGE_LIST_TOOLTIP),
97 NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_GL_STOP_ALL), SetMinimalSize(12, 12),
98 SetDataTip(SPR_FLAG_VEH_STOPPED, STR_VEHICLE_LIST_MASS_STOP_LIST_TOOLTIP),
99 NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_GL_START_ALL), SetMinimalSize(12, 12),
100 SetDataTip(SPR_FLAG_VEH_RUNNING, STR_VEHICLE_LIST_MASS_START_LIST_TOOLTIP),
101 NWidget(WWT_RESIZEBOX, COLOUR_GREY),
102 EndContainer(),
103 EndContainer(),
104 EndContainer(),
107 class VehicleGroupWindow : public BaseVehicleListWindow {
108 private:
109 /* Columns in the group list */
110 enum ListColumns {
111 VGC_FOLD, ///< Fold / Unfold button.
112 VGC_NAME, ///< Group name.
113 VGC_PROTECT, ///< Autoreplace protect icon.
114 VGC_AUTOREPLACE, ///< Autoreplace active icon.
115 VGC_PROFIT, ///< Profit icon.
116 VGC_NUMBER, ///< Number of vehicles in the group.
118 VGC_END
121 VehicleID vehicle_sel; ///< Selected vehicle
122 GroupID group_sel; ///< Selected group (for drag/drop)
123 GroupID group_rename; ///< Group being renamed, INVALID_GROUP if none
124 GroupID group_over; ///< Group over which a vehicle is dragged, INVALID_GROUP if none
125 GroupID group_confirm; ///< Group awaiting delete confirmation
126 GUIGroupList groups; ///< List of groups
127 uint tiny_step_height; ///< Step height for the group list
128 Scrollbar *group_sb;
130 std::vector<int> indents; ///< Indentation levels
132 Dimension column_size[VGC_END]; ///< Size of the columns in the group list.
134 void AddChildren(GUIGroupList *source, GroupID parent, int indent)
136 for (const Group *g : *source) {
137 if (g->parent != parent) continue;
138 this->groups.push_back(g);
139 this->indents.push_back(indent);
140 if (g->folded) {
141 /* Test if this group has children at all. If not, the folded flag should be cleared to avoid lingering unfold buttons in the list. */
142 auto child = std::find_if(source->begin(), source->end(), [g](const Group *child){ return child->parent == g->index; });
143 bool has_children = child != source->end();
144 Group::Get(g->index)->folded = has_children;
145 } else {
146 AddChildren(source, g->index, indent + 1);
152 * (Re)Build the group list.
154 * @param owner The owner of the window
156 void BuildGroupList(Owner owner)
158 if (!this->groups.NeedRebuild()) return;
160 this->groups.clear();
161 this->indents.clear();
163 GUIGroupList list;
165 for (const Group *g : Group::Iterate()) {
166 if (g->owner == owner && g->vehicle_type == this->vli.vtype) {
167 list.push_back(g);
171 list.ForceResort();
173 /* Sort the groups by their name */
174 const Group *last_group[2] = { nullptr, nullptr };
175 char last_name[2][64] = { "", "" };
176 list.Sort([&](const Group * const &a, const Group * const &b) {
177 if (a != last_group[0]) {
178 last_group[0] = a;
179 SetDParam(0, a->index);
180 GetString(last_name[0], STR_GROUP_NAME, lastof(last_name[0]));
183 if (b != last_group[1]) {
184 last_group[1] = b;
185 SetDParam(0, b->index);
186 GetString(last_name[1], STR_GROUP_NAME, lastof(last_name[1]));
189 int r = strnatcmp(last_name[0], last_name[1]); // Sort by name (natural sorting).
190 if (r == 0) return a->index < b->index;
191 return r < 0;
194 AddChildren(&list, INVALID_GROUP, 0);
196 this->groups.shrink_to_fit();
197 this->groups.RebuildDone();
201 * Compute tiny_step_height and column_size
202 * @return Total width required for the group list.
204 uint ComputeGroupInfoSize()
206 this->column_size[VGC_FOLD] = maxdim(GetSpriteSize(SPR_CIRCLE_FOLDED), GetSpriteSize(SPR_CIRCLE_UNFOLDED));
207 this->tiny_step_height = this->column_size[VGC_FOLD].height;
209 this->column_size[VGC_NAME] = maxdim(GetStringBoundingBox(STR_GROUP_DEFAULT_TRAINS + this->vli.vtype), GetStringBoundingBox(STR_GROUP_ALL_TRAINS + this->vli.vtype));
210 this->column_size[VGC_NAME].width = std::max(170u, this->column_size[VGC_NAME].width);
211 this->tiny_step_height = std::max(this->tiny_step_height, this->column_size[VGC_NAME].height);
213 this->column_size[VGC_PROTECT] = GetSpriteSize(SPR_GROUP_REPLACE_PROTECT);
214 this->tiny_step_height = std::max(this->tiny_step_height, this->column_size[VGC_PROTECT].height);
216 this->column_size[VGC_AUTOREPLACE] = GetSpriteSize(SPR_GROUP_REPLACE_ACTIVE);
217 this->tiny_step_height = std::max(this->tiny_step_height, this->column_size[VGC_AUTOREPLACE].height);
219 this->column_size[VGC_PROFIT].width = 0;
220 this->column_size[VGC_PROFIT].height = 0;
221 static const SpriteID profit_sprites[] = {SPR_PROFIT_NA, SPR_PROFIT_NEGATIVE, SPR_PROFIT_SOME, SPR_PROFIT_LOT};
222 for (uint i = 0; i < lengthof(profit_sprites); i++) {
223 Dimension d = GetSpriteSize(profit_sprites[i]);
224 this->column_size[VGC_PROFIT] = maxdim(this->column_size[VGC_PROFIT], d);
226 this->tiny_step_height = std::max(this->tiny_step_height, this->column_size[VGC_PROFIT].height);
228 int num_vehicle = GetGroupNumVehicle(this->vli.company, ALL_GROUP, this->vli.vtype);
229 SetDParamMaxValue(0, num_vehicle, 3, FS_SMALL);
230 SetDParamMaxValue(1, num_vehicle, 3, FS_SMALL);
231 this->column_size[VGC_NUMBER] = GetStringBoundingBox(STR_GROUP_COUNT_WITH_SUBGROUP);
232 this->tiny_step_height = std::max(this->tiny_step_height, this->column_size[VGC_NUMBER].height);
234 this->tiny_step_height += WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
236 return WD_FRAMERECT_LEFT + 8 +
237 this->column_size[VGC_FOLD].width + 2 +
238 this->column_size[VGC_NAME].width + 8 +
239 this->column_size[VGC_PROTECT].width + 2 +
240 this->column_size[VGC_AUTOREPLACE].width + 2 +
241 this->column_size[VGC_PROFIT].width + 2 +
242 this->column_size[VGC_NUMBER].width + 2 +
243 WD_FRAMERECT_RIGHT;
247 * Draw a row in the group list.
248 * @param y Top of the row.
249 * @param left Left of the row.
250 * @param right Right of the row.
251 * @param g_id Group to list.
252 * @param indent Indentation level.
253 * @param protection Whether autoreplace protection is set.
254 * @param has_children Whether the group has children and should have a fold / unfold button.
256 void DrawGroupInfo(int y, int left, int right, GroupID g_id, int indent = 0, bool protection = false, bool has_children = false) const
258 /* Highlight the group if a vehicle is dragged over it */
259 if (g_id == this->group_over) {
260 GfxFillRect(left + WD_FRAMERECT_LEFT, y + WD_FRAMERECT_TOP + 1, right - WD_FRAMERECT_RIGHT, y + this->tiny_step_height - WD_FRAMERECT_BOTTOM - 1, _colour_gradient[COLOUR_GREY][7]);
263 if (g_id == NEW_GROUP) return;
265 /* draw the selected group in white, else we draw it in black */
266 TextColour colour = g_id == this->vli.index ? TC_WHITE : TC_BLACK;
267 const GroupStatistics &stats = GroupStatistics::Get(this->vli.company, g_id, this->vli.vtype);
268 bool rtl = _current_text_dir == TD_RTL;
270 /* draw fold / unfold button */
271 int x = rtl ? right - WD_FRAMERECT_RIGHT - 8 - this->column_size[VGC_FOLD].width + 1 : left + WD_FRAMERECT_LEFT + 8;
272 if (has_children) {
273 DrawSprite(Group::Get(g_id)->folded ? SPR_CIRCLE_FOLDED : SPR_CIRCLE_UNFOLDED, PAL_NONE, rtl ? x - indent : x + indent, y + (this->tiny_step_height - this->column_size[VGC_FOLD].height) / 2);
276 /* draw group name */
277 StringID str;
278 if (IsAllGroupID(g_id)) {
279 str = STR_GROUP_ALL_TRAINS + this->vli.vtype;
280 } else if (IsDefaultGroupID(g_id)) {
281 str = STR_GROUP_DEFAULT_TRAINS + this->vli.vtype;
282 } else {
283 SetDParam(0, g_id);
284 str = STR_GROUP_NAME;
286 x = rtl ? x - 2 - this->column_size[VGC_NAME].width : x + 2 + this->column_size[VGC_FOLD].width;
287 DrawString(x + (rtl ? 0 : indent), x + this->column_size[VGC_NAME].width - 1 - (rtl ? indent : 0), y + (this->tiny_step_height - this->column_size[VGC_NAME].height) / 2, str, colour);
289 /* draw autoreplace protection */
290 x = rtl ? x - 8 - this->column_size[VGC_PROTECT].width : x + 8 + this->column_size[VGC_NAME].width;
291 if (protection) DrawSprite(SPR_GROUP_REPLACE_PROTECT, PAL_NONE, x, y + (this->tiny_step_height - this->column_size[VGC_PROTECT].height) / 2);
293 /* draw autoreplace status */
294 x = rtl ? x - 2 - this->column_size[VGC_AUTOREPLACE].width : x + 2 + this->column_size[VGC_PROTECT].width;
295 if (stats.autoreplace_defined) DrawSprite(SPR_GROUP_REPLACE_ACTIVE, stats.autoreplace_finished ? PALETTE_CRASH : PAL_NONE, x, y + (this->tiny_step_height - this->column_size[VGC_AUTOREPLACE].height) / 2);
297 /* draw the profit icon */
298 x = rtl ? x - 2 - this->column_size[VGC_PROFIT].width : x + 2 + this->column_size[VGC_AUTOREPLACE].width;
299 SpriteID spr;
300 uint num_profit_vehicle = GetGroupNumProfitVehicle(this->vli.company, g_id, this->vli.vtype);
301 Money profit_last_year = GetGroupProfitLastYear(this->vli.company, g_id, this->vli.vtype);
302 if (num_profit_vehicle == 0) {
303 spr = SPR_PROFIT_NA;
304 } else if (profit_last_year < 0) {
305 spr = SPR_PROFIT_NEGATIVE;
306 } else if (profit_last_year < VEHICLE_PROFIT_THRESHOLD * num_profit_vehicle) {
307 spr = SPR_PROFIT_SOME;
308 } else {
309 spr = SPR_PROFIT_LOT;
311 DrawSprite(spr, PAL_NONE, x, y + (this->tiny_step_height - this->column_size[VGC_PROFIT].height) / 2);
313 /* draw the number of vehicles of the group */
314 x = rtl ? x - 2 - this->column_size[VGC_NUMBER].width : x + 2 + this->column_size[VGC_PROFIT].width;
315 int num_vehicle_with_subgroups = GetGroupNumVehicle(this->vli.company, g_id, this->vli.vtype);
316 int num_vehicle = GroupStatistics::Get(this->vli.company, g_id, this->vli.vtype).num_vehicle;
317 if (IsAllGroupID(g_id) || IsDefaultGroupID(g_id) || num_vehicle_with_subgroups == num_vehicle) {
318 SetDParam(0, num_vehicle);
319 DrawString(x, x + this->column_size[VGC_NUMBER].width - 1, y + (this->tiny_step_height - this->column_size[VGC_NUMBER].height) / 2, STR_TINY_COMMA, colour, SA_RIGHT | SA_FORCE);
320 } else {
321 SetDParam(0, num_vehicle);
322 SetDParam(1, num_vehicle_with_subgroups - num_vehicle);
323 DrawString(x, x + this->column_size[VGC_NUMBER].width - 1, y + (this->tiny_step_height - this->column_size[VGC_NUMBER].height) / 2, STR_GROUP_COUNT_WITH_SUBGROUP, colour, SA_RIGHT | SA_FORCE);
328 * Mark the widget containing the currently highlighted group as dirty.
330 void DirtyHighlightedGroupWidget()
332 if (this->group_over == INVALID_GROUP) return;
334 if (IsAllGroupID(this->group_over)) {
335 this->SetWidgetDirty(WID_GL_ALL_VEHICLES);
336 } else if (IsDefaultGroupID(this->group_over)) {
337 this->SetWidgetDirty(WID_GL_DEFAULT_VEHICLES);
338 } else {
339 this->SetWidgetDirty(WID_GL_LIST_GROUP);
343 public:
344 VehicleGroupWindow(WindowDesc *desc, WindowNumber window_number) : BaseVehicleListWindow(desc, window_number)
346 this->CreateNestedTree();
348 this->vscroll = this->GetScrollbar(WID_GL_LIST_VEHICLE_SCROLLBAR);
349 this->group_sb = this->GetScrollbar(WID_GL_LIST_GROUP_SCROLLBAR);
351 this->vli.index = ALL_GROUP;
352 this->vehicle_sel = INVALID_VEHICLE;
353 this->group_sel = INVALID_GROUP;
354 this->group_rename = INVALID_GROUP;
355 this->group_over = INVALID_GROUP;
357 this->BuildVehicleList();
358 this->SortVehicleList();
360 this->groups.ForceRebuild();
361 this->groups.NeedResort();
362 this->BuildGroupList(vli.company);
363 this->group_sb->SetCount((uint)this->groups.size());
365 this->GetWidget<NWidgetCore>(WID_GL_CAPTION)->widget_data = STR_VEHICLE_LIST_TRAIN_CAPTION + this->vli.vtype;
366 this->GetWidget<NWidgetCore>(WID_GL_LIST_VEHICLE)->tool_tip = STR_VEHICLE_LIST_TRAIN_LIST_TOOLTIP + this->vli.vtype;
368 this->GetWidget<NWidgetCore>(WID_GL_CREATE_GROUP)->widget_data += this->vli.vtype;
369 this->GetWidget<NWidgetCore>(WID_GL_RENAME_GROUP)->widget_data += this->vli.vtype;
370 this->GetWidget<NWidgetCore>(WID_GL_DELETE_GROUP)->widget_data += this->vli.vtype;
371 this->GetWidget<NWidgetCore>(WID_GL_LIVERY_GROUP)->widget_data += this->vli.vtype;
372 this->GetWidget<NWidgetCore>(WID_GL_REPLACE_PROTECTION)->widget_data += this->vli.vtype;
374 this->FinishInitNested(window_number);
375 this->owner = vli.company;
378 ~VehicleGroupWindow()
380 *this->sorting = this->vehgroups.GetListing();
383 void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
385 switch (widget) {
386 case WID_GL_LIST_GROUP:
387 size->width = this->ComputeGroupInfoSize();
388 resize->height = this->tiny_step_height;
389 fill->height = this->tiny_step_height;
390 break;
392 case WID_GL_ALL_VEHICLES:
393 case WID_GL_DEFAULT_VEHICLES:
394 size->width = this->ComputeGroupInfoSize();
395 size->height = this->tiny_step_height;
396 break;
398 case WID_GL_SORT_BY_ORDER: {
399 Dimension d = GetStringBoundingBox(this->GetWidget<NWidgetCore>(widget)->widget_data);
400 d.width += padding.width + Window::SortButtonWidth() * 2; // Doubled since the string is centred and it also looks better.
401 d.height += padding.height;
402 *size = maxdim(*size, d);
403 break;
406 case WID_GL_LIST_VEHICLE:
407 this->ComputeGroupInfoSize();
408 resize->height = GetVehicleListHeight(this->vli.vtype, this->tiny_step_height);
409 size->height = 4 * resize->height;
410 break;
412 case WID_GL_MANAGE_VEHICLES_DROPDOWN: {
413 Dimension d = this->GetActionDropdownSize(true, true);
414 d.height += padding.height;
415 d.width += padding.width;
416 *size = maxdim(*size, d);
417 break;
423 * Some data on this window has become invalid.
424 * @param data Information about the changed data.
425 * @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.
427 void OnInvalidateData(int data = 0, bool gui_scope = true) override
429 if (data == 0) {
430 /* This needs to be done in command-scope to enforce rebuilding before resorting invalid data */
431 this->vehgroups.ForceRebuild();
432 this->groups.ForceRebuild();
433 } else {
434 this->vehgroups.ForceResort();
435 this->groups.ForceResort();
438 /* Process ID-invalidation in command-scope as well */
439 if (this->group_rename != INVALID_GROUP && !Group::IsValidID(this->group_rename)) {
440 CloseWindowByClass(WC_QUERY_STRING);
441 this->group_rename = INVALID_GROUP;
444 if (!(IsAllGroupID(this->vli.index) || IsDefaultGroupID(this->vli.index) || Group::IsValidID(this->vli.index))) {
445 this->vli.index = ALL_GROUP;
446 HideDropDownMenu(this);
448 this->SetDirty();
451 void SetStringParameters(int widget) const override
453 switch (widget) {
454 case WID_GL_AVAILABLE_VEHICLES:
455 SetDParam(0, STR_VEHICLE_LIST_AVAILABLE_TRAINS + this->vli.vtype);
456 break;
458 case WID_GL_CAPTION:
459 /* If selected_group == DEFAULT_GROUP || ALL_GROUP, draw the standard caption
460 * We list all vehicles or ungrouped vehicles */
461 if (IsDefaultGroupID(this->vli.index) || IsAllGroupID(this->vli.index)) {
462 SetDParam(0, STR_COMPANY_NAME);
463 SetDParam(1, this->vli.company);
464 SetDParam(2, this->vehicles.size());
465 SetDParam(3, this->vehicles.size());
466 } else {
467 uint num_vehicle = GetGroupNumVehicle(this->vli.company, this->vli.index, this->vli.vtype);
469 SetDParam(0, STR_GROUP_NAME);
470 SetDParam(1, this->vli.index);
471 SetDParam(2, num_vehicle);
472 SetDParam(3, num_vehicle);
474 break;
478 void OnPaint() override
480 /* If we select the all vehicles, this->list will contain all vehicles of the owner
481 * else this->list will contain all vehicles which belong to the selected group */
482 this->BuildVehicleList();
483 this->SortVehicleList();
485 this->BuildGroupList(this->owner);
487 this->group_sb->SetCount(static_cast<int>(this->groups.size()));
488 this->vscroll->SetCount(static_cast<int>(this->vehgroups.size()));
490 /* The drop down menu is out, *but* it may not be used, retract it. */
491 if (this->vehicles.size() == 0 && this->IsWidgetLowered(WID_GL_MANAGE_VEHICLES_DROPDOWN)) {
492 this->RaiseWidget(WID_GL_MANAGE_VEHICLES_DROPDOWN);
493 HideDropDownMenu(this);
496 /* Disable all lists management button when the list is empty */
497 this->SetWidgetsDisabledState(this->vehicles.size() == 0 || _local_company != this->vli.company,
498 WID_GL_STOP_ALL,
499 WID_GL_START_ALL,
500 WID_GL_MANAGE_VEHICLES_DROPDOWN,
501 WIDGET_LIST_END);
503 /* Disable the group specific function when we select the default group or all vehicles */
504 this->SetWidgetsDisabledState(IsDefaultGroupID(this->vli.index) || IsAllGroupID(this->vli.index) || _local_company != this->vli.company,
505 WID_GL_DELETE_GROUP,
506 WID_GL_RENAME_GROUP,
507 WID_GL_LIVERY_GROUP,
508 WID_GL_REPLACE_PROTECTION,
509 WIDGET_LIST_END);
511 /* Disable remaining buttons for non-local companies
512 * Needed while changing _local_company, eg. by cheats
513 * All procedures (eg. move vehicle to another group)
514 * verify, whether you are the owner of the vehicle,
515 * so it doesn't have to be disabled
517 this->SetWidgetsDisabledState(_local_company != this->vli.company,
518 WID_GL_CREATE_GROUP,
519 WID_GL_AVAILABLE_VEHICLES,
520 WIDGET_LIST_END);
522 /* If not a default group and the group has replace protection, show an enabled replace sprite. */
523 uint16 protect_sprite = SPR_GROUP_REPLACE_OFF_TRAIN;
524 if (!IsDefaultGroupID(this->vli.index) && !IsAllGroupID(this->vli.index) && HasBit(Group::Get(this->vli.index)->flags, GroupFlags::GF_REPLACE_PROTECTION)) protect_sprite = SPR_GROUP_REPLACE_ON_TRAIN;
525 this->GetWidget<NWidgetCore>(WID_GL_REPLACE_PROTECTION)->widget_data = protect_sprite + this->vli.vtype;
527 /* Set text of "group by" dropdown widget. */
528 this->GetWidget<NWidgetCore>(WID_GL_GROUP_BY_DROPDOWN)->widget_data = this->vehicle_group_by_names[this->grouping];
530 /* Set text of "sort by" dropdown widget. */
531 this->GetWidget<NWidgetCore>(WID_GL_SORT_BY_DROPDOWN)->widget_data = this->GetVehicleSorterNames()[this->vehgroups.SortType()];
533 this->DrawWidgets();
536 void DrawWidget(const Rect &r, int widget) const override
538 switch (widget) {
539 case WID_GL_ALL_VEHICLES:
540 DrawGroupInfo(r.top, r.left, r.right, ALL_GROUP);
541 break;
543 case WID_GL_DEFAULT_VEHICLES:
544 DrawGroupInfo(r.top, r.left, r.right, DEFAULT_GROUP);
545 break;
547 case WID_GL_INFO: {
548 Money this_year = 0;
549 Money last_year = 0;
550 uint64 occupancy = 0;
552 for (const Vehicle * const v : this->vehicles) {
553 assert(v->owner == this->owner);
555 this_year += v->GetDisplayProfitThisYear();
556 last_year += v->GetDisplayProfitLastYear();
557 occupancy += v->trip_occupancy;
560 const int left = r.left + WD_FRAMERECT_LEFT + 8;
561 const int right = r.right - WD_FRAMERECT_RIGHT - 8;
563 int y = r.top + WD_FRAMERECT_TOP;
564 DrawString(left, right, y, STR_GROUP_PROFIT_THIS_YEAR, TC_BLACK);
565 SetDParam(0, this_year);
566 DrawString(left, right, y, STR_JUST_CURRENCY_LONG, TC_BLACK, SA_RIGHT);
568 y += FONT_HEIGHT_NORMAL;
569 DrawString(left, right, y, STR_GROUP_PROFIT_LAST_YEAR, TC_BLACK);
570 SetDParam(0, last_year);
571 DrawString(left, right, y, STR_JUST_CURRENCY_LONG, TC_BLACK, SA_RIGHT);
573 y += FONT_HEIGHT_NORMAL;
574 DrawString(left, right, y, STR_GROUP_OCCUPANCY, TC_BLACK);
575 const size_t vehicle_count = this->vehicles.size();
576 if (vehicle_count > 0) {
577 SetDParam(0, occupancy / vehicle_count);
578 DrawString(left, right, y, STR_GROUP_OCCUPANCY_VALUE, TC_BLACK, SA_RIGHT);
581 break;
584 case WID_GL_LIST_GROUP: {
585 int y1 = r.top;
586 int max = std::min<size_t>(this->group_sb->GetPosition() + this->group_sb->GetCapacity(), this->groups.size());
587 for (int i = this->group_sb->GetPosition(); i < max; ++i) {
588 const Group *g = this->groups[i];
590 assert(g->owner == this->owner);
592 DrawGroupInfo(y1, r.left, r.right, g->index, this->indents[i] * LEVEL_WIDTH, HasBit(g->flags, GroupFlags::GF_REPLACE_PROTECTION), g->folded || (i + 1 < (int)this->groups.size() && indents[i + 1] > this->indents[i]));
594 y1 += this->tiny_step_height;
596 if ((uint)this->group_sb->GetPosition() + this->group_sb->GetCapacity() > this->groups.size()) {
597 DrawGroupInfo(y1, r.left, r.right, NEW_GROUP);
599 break;
602 case WID_GL_SORT_BY_ORDER:
603 this->DrawSortButtonState(WID_GL_SORT_BY_ORDER, this->vehgroups.IsDescSortOrder() ? SBS_DOWN : SBS_UP);
604 break;
606 case WID_GL_LIST_VEHICLE:
607 if (this->vli.index != ALL_GROUP && this->grouping == GB_NONE) {
608 /* Mark vehicles which are in sub-groups (only if we are not using shared order coalescing) */
609 int y = r.top;
610 uint max = static_cast<uint>(std::min<size_t>(this->vscroll->GetPosition() + this->vscroll->GetCapacity(), this->vehgroups.size()));
611 for (uint i = this->vscroll->GetPosition(); i < max; ++i) {
612 const Vehicle *v = this->vehgroups[i].GetSingleVehicle();
613 if (v->group_id != this->vli.index) {
614 GfxFillRect(r.left + 1, y + 1, r.right - 1, y + this->resize.step_height - 2, _colour_gradient[COLOUR_GREY][3], FILLRECT_CHECKER);
616 y += this->resize.step_height;
620 this->DrawVehicleListItems(this->vehicle_sel, this->resize.step_height, r);
621 break;
625 static void DeleteGroupCallback(Window *win, bool confirmed)
627 if (confirmed) {
628 VehicleGroupWindow *w = (VehicleGroupWindow*)win;
629 w->vli.index = ALL_GROUP;
630 Command<CMD_DELETE_GROUP>::Post(STR_ERROR_GROUP_CAN_T_DELETE, w->group_confirm);
634 void OnClick(Point pt, int widget, int click_count) override
636 switch (widget) {
637 case WID_GL_SORT_BY_ORDER: // Flip sorting method ascending/descending
638 this->vehgroups.ToggleSortOrder();
639 this->SetDirty();
640 break;
642 case WID_GL_GROUP_BY_DROPDOWN: // Select grouping option dropdown menu
643 ShowDropDownMenu(this, this->vehicle_group_by_names, this->grouping, WID_GL_GROUP_BY_DROPDOWN, 0, 0);
644 return;
646 case WID_GL_SORT_BY_DROPDOWN: // Select sorting criteria dropdown menu
647 ShowDropDownMenu(this, this->GetVehicleSorterNames(), this->vehgroups.SortType(), WID_GL_SORT_BY_DROPDOWN, 0, (this->vli.vtype == VEH_TRAIN || this->vli.vtype == VEH_ROAD) ? 0 : (1 << 10));
648 return;
650 case WID_GL_ALL_VEHICLES: // All vehicles button
651 if (!IsAllGroupID(this->vli.index)) {
652 this->vli.index = ALL_GROUP;
653 this->vehgroups.ForceRebuild();
654 this->SetDirty();
656 break;
658 case WID_GL_DEFAULT_VEHICLES: // Ungrouped vehicles button
659 if (!IsDefaultGroupID(this->vli.index)) {
660 this->vli.index = DEFAULT_GROUP;
661 this->vehgroups.ForceRebuild();
662 this->SetDirty();
664 break;
666 case WID_GL_LIST_GROUP: { // Matrix Group
667 uint id_g = this->group_sb->GetScrolledRowFromWidget(pt.y, this, WID_GL_LIST_GROUP);
668 if (id_g >= this->groups.size()) return;
670 if (groups[id_g]->folded || (id_g + 1 < this->groups.size() && this->indents[id_g + 1] > this->indents[id_g])) {
671 /* The group has children, check if the user clicked the fold / unfold button. */
672 NWidgetCore *group_display = this->GetWidget<NWidgetCore>(widget);
673 int x = _current_text_dir == TD_RTL ?
674 group_display->pos_x + group_display->current_x - WD_FRAMERECT_RIGHT - 8 - this->indents[id_g] * LEVEL_WIDTH - this->column_size[VGC_FOLD].width :
675 group_display->pos_x + WD_FRAMERECT_LEFT + 8 + this->indents[id_g] * LEVEL_WIDTH;
676 if (click_count > 1 || (pt.x >= x && pt.x < (int)(x + this->column_size[VGC_FOLD].width))) {
678 GroupID g = this->vli.index;
679 if (!IsAllGroupID(g) && !IsDefaultGroupID(g)) {
680 do {
681 g = Group::Get(g)->parent;
682 if (g == groups[id_g]->index) {
683 this->vli.index = g;
684 break;
686 } while (g != INVALID_GROUP);
689 Group::Get(groups[id_g]->index)->folded = !groups[id_g]->folded;
690 this->groups.ForceRebuild();
692 this->SetDirty();
693 break;
697 this->group_sel = this->vli.index = this->groups[id_g]->index;
699 SetObjectToPlaceWnd(SPR_CURSOR_MOUSE, PAL_NONE, HT_DRAG, this);
701 this->vehgroups.ForceRebuild();
702 this->SetDirty();
703 break;
706 case WID_GL_LIST_VEHICLE: { // Matrix Vehicle
707 uint id_v = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_GL_LIST_VEHICLE);
708 if (id_v >= this->vehgroups.size()) return; // click out of list bound
710 const GUIVehicleGroup &vehgroup = this->vehgroups[id_v];
712 const Vehicle *v = nullptr;
714 switch (this->grouping) {
715 case GB_NONE: {
716 const Vehicle *v2 = vehgroup.GetSingleVehicle();
717 if (VehicleClicked(v2)) break;
718 v = v2;
719 break;
722 case GB_SHARED_ORDERS: {
723 assert(vehgroup.NumVehicles() > 0);
724 v = vehgroup.vehicles_begin[0];
726 * No VehicleClicked(v) support for now, because don't want
727 * to enable any contextual actions except perhaps clicking/ctrl-clicking to clone orders.
729 break;
732 default:
733 NOT_REACHED();
735 if (v) {
736 if (_ctrl_pressed && this->grouping == GB_SHARED_ORDERS) {
737 ShowOrdersWindow(v);
738 } else {
739 this->vehicle_sel = v->index;
741 if (_ctrl_pressed && this->grouping == GB_NONE) {
743 * It only makes sense to select a group if not using shared orders
744 * since two vehicles sharing orders can be from different groups.
746 this->SelectGroup(v->group_id);
749 SetObjectToPlaceWnd(SPR_CURSOR_MOUSE, PAL_NONE, HT_DRAG, this);
750 SetMouseCursorVehicle(v, EIT_IN_LIST);
751 _cursor.vehchain = true;
753 this->SetDirty();
757 break;
760 case WID_GL_CREATE_GROUP: { // Create a new group
761 Command<CMD_CREATE_GROUP>::Post(STR_ERROR_GROUP_CAN_T_CREATE, CcCreateGroup, this->vli.vtype, this->vli.index);
762 break;
765 case WID_GL_DELETE_GROUP: { // Delete the selected group
766 this->group_confirm = this->vli.index;
767 ShowQuery(STR_QUERY_GROUP_DELETE_CAPTION, STR_GROUP_DELETE_QUERY_TEXT, this, DeleteGroupCallback);
768 break;
771 case WID_GL_RENAME_GROUP: // Rename the selected roup
772 this->ShowRenameGroupWindow(this->vli.index, false);
773 break;
775 case WID_GL_LIVERY_GROUP: // Set group livery
776 ShowCompanyLiveryWindow(this->owner, this->vli.index);
777 break;
779 case WID_GL_AVAILABLE_VEHICLES:
780 ShowBuildVehicleWindow(INVALID_TILE, this->vli.vtype);
781 break;
783 case WID_GL_MANAGE_VEHICLES_DROPDOWN: {
784 ShowDropDownList(this, this->BuildActionDropdownList(true, Group::IsValidID(this->vli.index)), 0, WID_GL_MANAGE_VEHICLES_DROPDOWN);
785 break;
788 case WID_GL_START_ALL:
789 case WID_GL_STOP_ALL: { // Start/stop all vehicles of the list
790 Command<CMD_MASS_START_STOP>::Post(0, widget == WID_GL_START_ALL, true, this->vli);
791 break;
794 case WID_GL_REPLACE_PROTECTION: {
795 const Group *g = Group::GetIfValid(this->vli.index);
796 if (g != nullptr) {
797 Command<CMD_SET_GROUP_FLAG>::Post(this->vli.index, GroupFlags::GF_REPLACE_PROTECTION, !HasBit(g->flags, GroupFlags::GF_REPLACE_PROTECTION), _ctrl_pressed);
799 break;
804 void OnDragDrop_Group(Point pt, int widget)
806 const Group *g = Group::Get(this->group_sel);
808 switch (widget) {
809 case WID_GL_ALL_VEHICLES: // All vehicles
810 case WID_GL_DEFAULT_VEHICLES: // Ungrouped vehicles
811 if (g->parent != INVALID_GROUP) {
812 Command<CMD_ALTER_GROUP>::Post(STR_ERROR_GROUP_CAN_T_SET_PARENT, AlterGroupMode::SetParent, this->group_sel, INVALID_GROUP, {});
815 this->group_sel = INVALID_GROUP;
816 this->group_over = INVALID_GROUP;
817 this->SetDirty();
818 break;
820 case WID_GL_LIST_GROUP: { // Matrix group
821 uint id_g = this->group_sb->GetScrolledRowFromWidget(pt.y, this, WID_GL_LIST_GROUP);
822 GroupID new_g = id_g >= this->groups.size() ? INVALID_GROUP : this->groups[id_g]->index;
824 if (this->group_sel != new_g && g->parent != new_g) {
825 Command<CMD_ALTER_GROUP>::Post(STR_ERROR_GROUP_CAN_T_SET_PARENT, AlterGroupMode::SetParent, this->group_sel, new_g, {});
828 this->group_sel = INVALID_GROUP;
829 this->group_over = INVALID_GROUP;
830 this->SetDirty();
831 break;
836 void OnDragDrop_Vehicle(Point pt, int widget)
838 switch (widget) {
839 case WID_GL_DEFAULT_VEHICLES: // Ungrouped vehicles
840 Command<CMD_ADD_VEHICLE_GROUP>::Post(STR_ERROR_GROUP_CAN_T_ADD_VEHICLE, DEFAULT_GROUP, this->vehicle_sel, _ctrl_pressed || this->grouping == GB_SHARED_ORDERS);
842 this->vehicle_sel = INVALID_VEHICLE;
843 this->group_over = INVALID_GROUP;
845 this->SetDirty();
846 break;
848 case WID_GL_LIST_GROUP: { // Matrix group
849 const VehicleID vindex = this->vehicle_sel;
850 this->vehicle_sel = INVALID_VEHICLE;
851 this->group_over = INVALID_GROUP;
852 this->SetDirty();
854 uint id_g = this->group_sb->GetScrolledRowFromWidget(pt.y, this, WID_GL_LIST_GROUP);
855 GroupID new_g = id_g >= this->groups.size() ? NEW_GROUP : this->groups[id_g]->index;
857 Command<CMD_ADD_VEHICLE_GROUP>::Post(STR_ERROR_GROUP_CAN_T_ADD_VEHICLE, new_g == NEW_GROUP ? CcAddVehicleNewGroup : nullptr, 0, new_g, vindex, _ctrl_pressed || this->grouping == GB_SHARED_ORDERS);
858 break;
861 case WID_GL_LIST_VEHICLE: { // Matrix vehicle
862 const VehicleID vindex = this->vehicle_sel;
863 this->vehicle_sel = INVALID_VEHICLE;
864 this->group_over = INVALID_GROUP;
865 this->SetDirty();
867 uint id_v = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_GL_LIST_VEHICLE);
868 if (id_v >= this->vehgroups.size()) return; // click out of list bound
870 const GUIVehicleGroup &vehgroup = this->vehgroups[id_v];
871 switch (this->grouping) {
872 case GB_NONE: {
873 const Vehicle *v = vehgroup.GetSingleVehicle();
874 if (!VehicleClicked(v) && vindex == v->index) {
875 ShowVehicleViewWindow(v);
877 break;
880 case GB_SHARED_ORDERS: {
881 const Vehicle *v = vehgroup.vehicles_begin[0];
882 /* We do not support VehicleClicked() here since the contextual action may only make sense for individual vehicles */
884 if (vindex == v->index) {
885 if (vehgroup.NumVehicles() == 1) {
886 ShowVehicleViewWindow(v);
887 } else {
888 ShowVehicleListWindow(v);
891 break;
894 default:
895 NOT_REACHED();
897 break;
902 void OnDragDrop(Point pt, int widget) override
904 if (this->vehicle_sel != INVALID_VEHICLE) OnDragDrop_Vehicle(pt, widget);
905 if (this->group_sel != INVALID_GROUP) OnDragDrop_Group(pt, widget);
907 _cursor.vehchain = false;
910 void OnQueryTextFinished(char *str) override
912 if (str != nullptr) Command<CMD_ALTER_GROUP>::Post(STR_ERROR_GROUP_CAN_T_RENAME, AlterGroupMode::Rename, this->group_rename, 0, str);
913 this->group_rename = INVALID_GROUP;
916 void OnResize() override
918 this->group_sb->SetCapacityFromWidget(this, WID_GL_LIST_GROUP);
919 this->vscroll->SetCapacityFromWidget(this, WID_GL_LIST_VEHICLE);
922 void OnDropdownSelect(int widget, int index) override
924 switch (widget) {
925 case WID_GL_GROUP_BY_DROPDOWN:
926 this->UpdateVehicleGroupBy(static_cast<GroupBy>(index));
927 break;
929 case WID_GL_SORT_BY_DROPDOWN:
930 this->vehgroups.SetSortType(index);
931 break;
933 case WID_GL_MANAGE_VEHICLES_DROPDOWN:
934 assert(this->vehicles.size() != 0);
936 switch (index) {
937 case ADI_REPLACE: // Replace window
938 ShowReplaceGroupVehicleWindow(this->vli.index, this->vli.vtype);
939 break;
940 case ADI_SERVICE: // Send for servicing
941 case ADI_DEPOT: { // Send to Depots
942 Command<CMD_SEND_VEHICLE_TO_DEPOT>::Post(GetCmdSendToDepotMsg(this->vli.vtype), 0, DepotCommand::MassSend | (index == ADI_SERVICE ? DepotCommand::Service : DepotCommand::None), this->vli);
943 break;
946 case ADI_ADD_SHARED: // Add shared Vehicles
947 assert(Group::IsValidID(this->vli.index));
949 Command<CMD_ADD_SHARED_VEHICLE_GROUP>::Post(STR_ERROR_GROUP_CAN_T_ADD_SHARED_VEHICLE, this->vli.index, this->vli.vtype);
950 break;
951 case ADI_REMOVE_ALL: // Remove all Vehicles from the selected group
952 assert(Group::IsValidID(this->vli.index));
954 Command<CMD_REMOVE_ALL_VEHICLES_GROUP>::Post(STR_ERROR_GROUP_CAN_T_REMOVE_ALL_VEHICLES, this->vli.index);
955 break;
956 default: NOT_REACHED();
958 break;
960 default: NOT_REACHED();
963 this->SetDirty();
966 void OnGameTick() override
968 if (this->groups.NeedResort() || this->vehgroups.NeedResort()) {
969 this->SetDirty();
973 void OnPlaceObjectAbort() override
975 /* abort drag & drop */
976 this->vehicle_sel = INVALID_VEHICLE;
977 this->DirtyHighlightedGroupWidget();
978 this->group_sel = INVALID_GROUP;
979 this->group_over = INVALID_GROUP;
980 this->SetWidgetDirty(WID_GL_LIST_VEHICLE);
983 void OnMouseDrag(Point pt, int widget) override
985 if (this->vehicle_sel == INVALID_VEHICLE && this->group_sel == INVALID_GROUP) return;
987 /* A vehicle is dragged over... */
988 GroupID new_group_over = INVALID_GROUP;
989 switch (widget) {
990 case WID_GL_DEFAULT_VEHICLES: // ... the 'default' group.
991 new_group_over = DEFAULT_GROUP;
992 break;
994 case WID_GL_LIST_GROUP: { // ... the list of custom groups.
995 uint id_g = this->group_sb->GetScrolledRowFromWidget(pt.y, this, WID_GL_LIST_GROUP);
996 new_group_over = id_g >= this->groups.size() ? NEW_GROUP : this->groups[id_g]->index;
997 break;
1001 /* Do not highlight when dragging over the current group */
1002 if (this->vehicle_sel != INVALID_VEHICLE) {
1003 if (Vehicle::Get(vehicle_sel)->group_id == new_group_over) new_group_over = INVALID_GROUP;
1004 } else if (this->group_sel != INVALID_GROUP) {
1005 if (this->group_sel == new_group_over || Group::Get(this->group_sel)->parent == new_group_over) new_group_over = INVALID_GROUP;
1008 /* Mark widgets as dirty if the group changed. */
1009 if (new_group_over != this->group_over) {
1010 this->DirtyHighlightedGroupWidget();
1011 this->group_over = new_group_over;
1012 this->DirtyHighlightedGroupWidget();
1016 void ShowRenameGroupWindow(GroupID group, bool empty)
1018 assert(Group::IsValidID(group));
1019 this->group_rename = group;
1020 /* Show empty query for new groups */
1021 StringID str = STR_EMPTY;
1022 if (!empty) {
1023 SetDParam(0, group);
1024 str = STR_GROUP_NAME;
1026 ShowQueryString(str, STR_GROUP_RENAME_CAPTION, MAX_LENGTH_GROUP_NAME_CHARS, this, CS_ALPHANUMERAL, QSF_ENABLE_DEFAULT | QSF_LEN_IN_CHARS);
1030 * Tests whether a given vehicle is selected in the window, and unselects it if necessary.
1031 * Called when the vehicle is deleted.
1032 * @param vehicle Vehicle that is going to be deleted
1034 void UnselectVehicle(VehicleID vehicle)
1036 if (this->vehicle_sel == vehicle) ResetObjectToPlace();
1040 * Selects the specified group in the list
1042 * @param g_id The ID of the group to be selected
1044 void SelectGroup(const GroupID g_id)
1046 if (g_id == INVALID_GROUP || g_id == this->vli.index) return;
1048 this->vli.index = g_id;
1049 if (g_id != ALL_GROUP && g_id != DEFAULT_GROUP) {
1050 const Group *g = Group::Get(g_id);
1051 int id_g = find_index(this->groups, g);
1052 // The group's branch is maybe collapsed, so try to expand it
1053 if (id_g == -1) {
1054 for (auto pg = Group::GetIfValid(g->parent); pg != nullptr; pg = Group::GetIfValid(pg->parent)) {
1055 pg->folded = false;
1057 this->groups.ForceRebuild();
1058 this->BuildGroupList(this->owner);
1059 this->group_sb->SetCount((uint)this->groups.size());
1060 id_g = find_index(this->groups, g);
1062 this->group_sb->ScrollTowards(id_g);
1064 this->vehgroups.ForceRebuild();
1065 this->SetDirty();
1071 static WindowDesc _other_group_desc(
1072 WDP_AUTO, "list_groups", 460, 246,
1073 WC_INVALID, WC_NONE,
1075 _nested_group_widgets, lengthof(_nested_group_widgets)
1078 static WindowDesc _train_group_desc(
1079 WDP_AUTO, "list_groups_train", 525, 246,
1080 WC_TRAINS_LIST, WC_NONE,
1082 _nested_group_widgets, lengthof(_nested_group_widgets)
1086 * Show the group window for the given company and vehicle type.
1087 * @param company The company to show the window for.
1088 * @param vehicle_type The type of vehicle to show it for.
1089 * @param group The group to be selected. Defaults to INVALID_GROUP.
1090 * @param need_existing_window Whether the existing window is needed. Defaults to false.
1092 void ShowCompanyGroup(CompanyID company, VehicleType vehicle_type, GroupID group = INVALID_GROUP, bool need_existing_window = false)
1094 if (!Company::IsValidID(company)) return;
1096 const WindowNumber num = VehicleListIdentifier(VL_GROUP_LIST, vehicle_type, company).Pack();
1097 VehicleGroupWindow *w;
1098 if (vehicle_type == VEH_TRAIN) {
1099 w = AllocateWindowDescFront<VehicleGroupWindow>(&_train_group_desc, num, need_existing_window);
1100 } else {
1101 _other_group_desc.cls = GetWindowClassForVehicleType(vehicle_type);
1102 w = AllocateWindowDescFront<VehicleGroupWindow>(&_other_group_desc, num, need_existing_window);
1104 if (w != nullptr) w->SelectGroup(group);
1108 * Show the group window for the given vehicle.
1109 * @param v The vehicle to show the window for.
1111 void ShowCompanyGroupForVehicle(const Vehicle *v)
1113 ShowCompanyGroup(v->owner, v->type, v->group_id, true);
1117 * Finds a group list window determined by vehicle type and owner
1118 * @param vt vehicle type
1119 * @param owner owner of groups
1120 * @return pointer to VehicleGroupWindow, nullptr if not found
1122 static inline VehicleGroupWindow *FindVehicleGroupWindow(VehicleType vt, Owner owner)
1124 return (VehicleGroupWindow *)FindWindowById(GetWindowClassForVehicleType(vt), VehicleListIdentifier(VL_GROUP_LIST, vt, owner).Pack());
1128 * Opens a 'Rename group' window for newly created group.
1129 * @param veh_type Vehicle type.
1131 static void CcCreateGroup(GroupID gid, VehicleType veh_type)
1133 VehicleGroupWindow *w = FindVehicleGroupWindow(veh_type, _current_company);
1134 if (w != nullptr) w->ShowRenameGroupWindow(gid, true);
1138 * Opens a 'Rename group' window for newly created group.
1139 * @param cmd Unused.
1140 * @param result Did command succeed?
1141 * @param new_group ID of the created group.
1142 * @param vt Vehicle type.
1143 * @param parent_group Parent group of the new group.
1144 * @see CmdCreateGroup
1146 void CcCreateGroup(Commands cmd, const CommandCost &result, GroupID new_group, VehicleType vt, GroupID parent_group)
1148 if (result.Failed()) return;
1150 assert(vt <= VEH_AIRCRAFT);
1151 CcCreateGroup(new_group, vt);
1155 * Open rename window after adding a vehicle to a new group via drag and drop.
1156 * @param cmd Unused.
1157 * @param result Did command succeed?
1158 * @param new_group ID of the created group.
1159 * @param veh_id vehicle to add to a group
1161 void CcAddVehicleNewGroup(Commands cmd, const CommandCost &result, GroupID new_group, GroupID, VehicleID veh_id, bool)
1163 if (result.Failed()) return;
1165 assert(Vehicle::IsValidID(veh_id));
1166 CcCreateGroup(new_group, Vehicle::Get(veh_id)->type);
1170 * Removes the highlight of a vehicle in a group window
1171 * @param *v Vehicle to remove all highlights from
1173 void DeleteGroupHighlightOfVehicle(const Vehicle *v)
1175 /* If we haven't got any vehicles on the mouse pointer, we haven't got any highlighted in any group windows either
1176 * If that is the case, we can skip looping though the windows and save time
1178 if (_special_mouse_mode != WSM_DRAGDROP) return;
1180 VehicleGroupWindow *w = FindVehicleGroupWindow(v->type, v->owner);
1181 if (w != nullptr) w->UnselectVehicle(v->index);