Change: Improve "local authority rating" setting name and helptext
[openttd-github.git] / src / group_gui.cpp
blob503ddddd9e5551d95ad8a8e774c720d4fa3af48e
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),
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;
390 /* Minimum height is the height of the list widget minus all and default vehicles... */
391 size->height = 4 * GetVehicleListHeight(this->vli.vtype, this->tiny_step_height);
393 /* ... minus the buttons at the bottom ... */
394 uint max_icon_height = GetSpriteSize(this->GetWidget<NWidgetCore>(WID_GL_CREATE_GROUP)->widget_data).height;
395 max_icon_height = std::max(max_icon_height, GetSpriteSize(this->GetWidget<NWidgetCore>(WID_GL_RENAME_GROUP)->widget_data).height);
396 max_icon_height = std::max(max_icon_height, GetSpriteSize(this->GetWidget<NWidgetCore>(WID_GL_DELETE_GROUP)->widget_data).height);
397 max_icon_height = std::max(max_icon_height, GetSpriteSize(this->GetWidget<NWidgetCore>(WID_GL_REPLACE_PROTECTION)->widget_data).height);
399 /* ... minus the height of the group info ... */
400 max_icon_height += (FONT_HEIGHT_NORMAL * 3) + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
402 /* Get a multiple of tiny_step_height of that amount */
403 size->height = Ceil(size->height - max_icon_height, tiny_step_height);
404 break;
407 case WID_GL_ALL_VEHICLES:
408 case WID_GL_DEFAULT_VEHICLES:
409 size->width = this->ComputeGroupInfoSize();
410 size->height = this->tiny_step_height;
411 break;
413 case WID_GL_SORT_BY_ORDER: {
414 Dimension d = GetStringBoundingBox(this->GetWidget<NWidgetCore>(widget)->widget_data);
415 d.width += padding.width + Window::SortButtonWidth() * 2; // Doubled since the string is centred and it also looks better.
416 d.height += padding.height;
417 *size = maxdim(*size, d);
418 break;
421 case WID_GL_LIST_VEHICLE:
422 this->ComputeGroupInfoSize();
423 resize->height = GetVehicleListHeight(this->vli.vtype, this->tiny_step_height);
424 size->height = 4 * resize->height;
425 break;
427 case WID_GL_MANAGE_VEHICLES_DROPDOWN: {
428 Dimension d = this->GetActionDropdownSize(true, true);
429 d.height += padding.height;
430 d.width += padding.width;
431 *size = maxdim(*size, d);
432 break;
438 * Some data on this window has become invalid.
439 * @param data Information about the changed data.
440 * @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.
442 void OnInvalidateData(int data = 0, bool gui_scope = true) override
444 if (data == 0) {
445 /* This needs to be done in command-scope to enforce rebuilding before resorting invalid data */
446 this->vehgroups.ForceRebuild();
447 this->groups.ForceRebuild();
448 } else {
449 this->vehgroups.ForceResort();
450 this->groups.ForceResort();
453 /* Process ID-invalidation in command-scope as well */
454 if (this->group_rename != INVALID_GROUP && !Group::IsValidID(this->group_rename)) {
455 CloseWindowByClass(WC_QUERY_STRING);
456 this->group_rename = INVALID_GROUP;
459 if (!(IsAllGroupID(this->vli.index) || IsDefaultGroupID(this->vli.index) || Group::IsValidID(this->vli.index))) {
460 this->vli.index = ALL_GROUP;
461 HideDropDownMenu(this);
463 this->SetDirty();
466 void SetStringParameters(int widget) const override
468 switch (widget) {
469 case WID_GL_AVAILABLE_VEHICLES:
470 SetDParam(0, STR_VEHICLE_LIST_AVAILABLE_TRAINS + this->vli.vtype);
471 break;
473 case WID_GL_CAPTION:
474 /* If selected_group == DEFAULT_GROUP || ALL_GROUP, draw the standard caption
475 * We list all vehicles or ungrouped vehicles */
476 if (IsDefaultGroupID(this->vli.index) || IsAllGroupID(this->vli.index)) {
477 SetDParam(0, STR_COMPANY_NAME);
478 SetDParam(1, this->vli.company);
479 SetDParam(2, this->vehicles.size());
480 SetDParam(3, this->vehicles.size());
481 } else {
482 uint num_vehicle = GetGroupNumVehicle(this->vli.company, this->vli.index, this->vli.vtype);
484 SetDParam(0, STR_GROUP_NAME);
485 SetDParam(1, this->vli.index);
486 SetDParam(2, num_vehicle);
487 SetDParam(3, num_vehicle);
489 break;
493 void OnPaint() override
495 /* If we select the all vehicles, this->list will contain all vehicles of the owner
496 * else this->list will contain all vehicles which belong to the selected group */
497 this->BuildVehicleList();
498 this->SortVehicleList();
500 this->BuildGroupList(this->owner);
502 this->group_sb->SetCount(static_cast<int>(this->groups.size()));
503 this->vscroll->SetCount(static_cast<int>(this->vehgroups.size()));
505 /* The drop down menu is out, *but* it may not be used, retract it. */
506 if (this->vehicles.size() == 0 && this->IsWidgetLowered(WID_GL_MANAGE_VEHICLES_DROPDOWN)) {
507 this->RaiseWidget(WID_GL_MANAGE_VEHICLES_DROPDOWN);
508 HideDropDownMenu(this);
511 /* Disable all lists management button when the list is empty */
512 this->SetWidgetsDisabledState(this->vehicles.size() == 0 || _local_company != this->vli.company,
513 WID_GL_STOP_ALL,
514 WID_GL_START_ALL,
515 WID_GL_MANAGE_VEHICLES_DROPDOWN,
516 WIDGET_LIST_END);
518 /* Disable the group specific function when we select the default group or all vehicles */
519 this->SetWidgetsDisabledState(IsDefaultGroupID(this->vli.index) || IsAllGroupID(this->vli.index) || _local_company != this->vli.company,
520 WID_GL_DELETE_GROUP,
521 WID_GL_RENAME_GROUP,
522 WID_GL_LIVERY_GROUP,
523 WID_GL_REPLACE_PROTECTION,
524 WIDGET_LIST_END);
526 /* Disable remaining buttons for non-local companies
527 * Needed while changing _local_company, eg. by cheats
528 * All procedures (eg. move vehicle to another group)
529 * verify, whether you are the owner of the vehicle,
530 * so it doesn't have to be disabled
532 this->SetWidgetsDisabledState(_local_company != this->vli.company,
533 WID_GL_CREATE_GROUP,
534 WID_GL_AVAILABLE_VEHICLES,
535 WIDGET_LIST_END);
537 /* If not a default group and the group has replace protection, show an enabled replace sprite. */
538 uint16 protect_sprite = SPR_GROUP_REPLACE_OFF_TRAIN;
539 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;
540 this->GetWidget<NWidgetCore>(WID_GL_REPLACE_PROTECTION)->widget_data = protect_sprite + this->vli.vtype;
542 /* Set text of "group by" dropdown widget. */
543 this->GetWidget<NWidgetCore>(WID_GL_GROUP_BY_DROPDOWN)->widget_data = this->vehicle_group_by_names[this->grouping];
545 /* Set text of "sort by" dropdown widget. */
546 this->GetWidget<NWidgetCore>(WID_GL_SORT_BY_DROPDOWN)->widget_data = this->GetVehicleSorterNames()[this->vehgroups.SortType()];
548 this->DrawWidgets();
551 void DrawWidget(const Rect &r, int widget) const override
553 switch (widget) {
554 case WID_GL_ALL_VEHICLES:
555 DrawGroupInfo(r.top, r.left, r.right, ALL_GROUP);
556 break;
558 case WID_GL_DEFAULT_VEHICLES:
559 DrawGroupInfo(r.top, r.left, r.right, DEFAULT_GROUP);
560 break;
562 case WID_GL_INFO: {
563 Money this_year = 0;
564 Money last_year = 0;
565 uint64 occupancy = 0;
567 for (const Vehicle * const v : this->vehicles) {
568 assert(v->owner == this->owner);
570 this_year += v->GetDisplayProfitThisYear();
571 last_year += v->GetDisplayProfitLastYear();
572 occupancy += v->trip_occupancy;
575 const int left = r.left + WD_FRAMERECT_LEFT + 8;
576 const int right = r.right - WD_FRAMERECT_RIGHT - 8;
578 int y = r.top + WD_FRAMERECT_TOP;
579 DrawString(left, right, y, STR_GROUP_PROFIT_THIS_YEAR, TC_BLACK);
580 SetDParam(0, this_year);
581 DrawString(left, right, y, STR_JUST_CURRENCY_LONG, TC_BLACK, SA_RIGHT);
583 y += FONT_HEIGHT_NORMAL;
584 DrawString(left, right, y, STR_GROUP_PROFIT_LAST_YEAR, TC_BLACK);
585 SetDParam(0, last_year);
586 DrawString(left, right, y, STR_JUST_CURRENCY_LONG, TC_BLACK, SA_RIGHT);
588 y += FONT_HEIGHT_NORMAL;
589 DrawString(left, right, y, STR_GROUP_OCCUPANCY, TC_BLACK);
590 const size_t vehicle_count = this->vehicles.size();
591 if (vehicle_count > 0) {
592 SetDParam(0, occupancy / vehicle_count);
593 DrawString(left, right, y, STR_GROUP_OCCUPANCY_VALUE, TC_BLACK, SA_RIGHT);
596 break;
599 case WID_GL_LIST_GROUP: {
600 int y1 = r.top;
601 int max = std::min<size_t>(this->group_sb->GetPosition() + this->group_sb->GetCapacity(), this->groups.size());
602 for (int i = this->group_sb->GetPosition(); i < max; ++i) {
603 const Group *g = this->groups[i];
605 assert(g->owner == this->owner);
607 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]));
609 y1 += this->tiny_step_height;
611 if ((uint)this->group_sb->GetPosition() + this->group_sb->GetCapacity() > this->groups.size()) {
612 DrawGroupInfo(y1, r.left, r.right, NEW_GROUP);
614 break;
617 case WID_GL_SORT_BY_ORDER:
618 this->DrawSortButtonState(WID_GL_SORT_BY_ORDER, this->vehgroups.IsDescSortOrder() ? SBS_DOWN : SBS_UP);
619 break;
621 case WID_GL_LIST_VEHICLE:
622 if (this->vli.index != ALL_GROUP && this->grouping == GB_NONE) {
623 /* Mark vehicles which are in sub-groups (only if we are not using shared order coalescing) */
624 int y = r.top;
625 uint max = static_cast<uint>(std::min<size_t>(this->vscroll->GetPosition() + this->vscroll->GetCapacity(), this->vehgroups.size()));
626 for (uint i = this->vscroll->GetPosition(); i < max; ++i) {
627 const Vehicle *v = this->vehgroups[i].GetSingleVehicle();
628 if (v->group_id != this->vli.index) {
629 GfxFillRect(r.left + 1, y + 1, r.right - 1, y + this->resize.step_height - 2, _colour_gradient[COLOUR_GREY][3], FILLRECT_CHECKER);
631 y += this->resize.step_height;
635 this->DrawVehicleListItems(this->vehicle_sel, this->resize.step_height, r);
636 break;
640 static void DeleteGroupCallback(Window *win, bool confirmed)
642 if (confirmed) {
643 VehicleGroupWindow *w = (VehicleGroupWindow*)win;
644 w->vli.index = ALL_GROUP;
645 Command<CMD_DELETE_GROUP>::Post(STR_ERROR_GROUP_CAN_T_DELETE, w->group_confirm);
649 void OnClick(Point pt, int widget, int click_count) override
651 switch (widget) {
652 case WID_GL_SORT_BY_ORDER: // Flip sorting method ascending/descending
653 this->vehgroups.ToggleSortOrder();
654 this->SetDirty();
655 break;
657 case WID_GL_GROUP_BY_DROPDOWN: // Select grouping option dropdown menu
658 ShowDropDownMenu(this, this->vehicle_group_by_names, this->grouping, WID_GL_GROUP_BY_DROPDOWN, 0, 0);
659 return;
661 case WID_GL_SORT_BY_DROPDOWN: // Select sorting criteria dropdown menu
662 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));
663 return;
665 case WID_GL_ALL_VEHICLES: // All vehicles button
666 if (!IsAllGroupID(this->vli.index)) {
667 this->vli.index = ALL_GROUP;
668 this->vehgroups.ForceRebuild();
669 this->SetDirty();
671 break;
673 case WID_GL_DEFAULT_VEHICLES: // Ungrouped vehicles button
674 if (!IsDefaultGroupID(this->vli.index)) {
675 this->vli.index = DEFAULT_GROUP;
676 this->vehgroups.ForceRebuild();
677 this->SetDirty();
679 break;
681 case WID_GL_LIST_GROUP: { // Matrix Group
682 uint id_g = this->group_sb->GetScrolledRowFromWidget(pt.y, this, WID_GL_LIST_GROUP);
683 if (id_g >= this->groups.size()) return;
685 if (groups[id_g]->folded || (id_g + 1 < this->groups.size() && this->indents[id_g + 1] > this->indents[id_g])) {
686 /* The group has children, check if the user clicked the fold / unfold button. */
687 NWidgetCore *group_display = this->GetWidget<NWidgetCore>(widget);
688 int x = _current_text_dir == TD_RTL ?
689 group_display->pos_x + group_display->current_x - WD_FRAMERECT_RIGHT - 8 - this->indents[id_g] * LEVEL_WIDTH - this->column_size[VGC_FOLD].width :
690 group_display->pos_x + WD_FRAMERECT_LEFT + 8 + this->indents[id_g] * LEVEL_WIDTH;
691 if (click_count > 1 || (pt.x >= x && pt.x < (int)(x + this->column_size[VGC_FOLD].width))) {
693 GroupID g = this->vli.index;
694 if (!IsAllGroupID(g) && !IsDefaultGroupID(g)) {
695 do {
696 g = Group::Get(g)->parent;
697 if (g == groups[id_g]->index) {
698 this->vli.index = g;
699 break;
701 } while (g != INVALID_GROUP);
704 Group::Get(groups[id_g]->index)->folded = !groups[id_g]->folded;
705 this->groups.ForceRebuild();
707 this->SetDirty();
708 break;
712 this->group_sel = this->vli.index = this->groups[id_g]->index;
714 SetObjectToPlaceWnd(SPR_CURSOR_MOUSE, PAL_NONE, HT_DRAG, this);
716 this->vehgroups.ForceRebuild();
717 this->SetDirty();
718 break;
721 case WID_GL_LIST_VEHICLE: { // Matrix Vehicle
722 uint id_v = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_GL_LIST_VEHICLE);
723 if (id_v >= this->vehgroups.size()) return; // click out of list bound
725 const GUIVehicleGroup &vehgroup = this->vehgroups[id_v];
727 const Vehicle *v = nullptr;
729 switch (this->grouping) {
730 case GB_NONE: {
731 const Vehicle *v2 = vehgroup.GetSingleVehicle();
732 if (VehicleClicked(v2)) break;
733 v = v2;
734 break;
737 case GB_SHARED_ORDERS: {
738 assert(vehgroup.NumVehicles() > 0);
739 v = vehgroup.vehicles_begin[0];
741 * No VehicleClicked(v) support for now, because don't want
742 * to enable any contextual actions except perhaps clicking/ctrl-clicking to clone orders.
744 break;
747 default:
748 NOT_REACHED();
750 if (v) {
751 if (_ctrl_pressed && this->grouping == GB_SHARED_ORDERS) {
752 ShowOrdersWindow(v);
753 } else {
754 this->vehicle_sel = v->index;
756 if (_ctrl_pressed && this->grouping == GB_NONE) {
758 * It only makes sense to select a group if not using shared orders
759 * since two vehicles sharing orders can be from different groups.
761 this->SelectGroup(v->group_id);
764 SetObjectToPlaceWnd(SPR_CURSOR_MOUSE, PAL_NONE, HT_DRAG, this);
765 SetMouseCursorVehicle(v, EIT_IN_LIST);
766 _cursor.vehchain = true;
768 this->SetDirty();
772 break;
775 case WID_GL_CREATE_GROUP: { // Create a new group
776 Command<CMD_CREATE_GROUP>::Post(STR_ERROR_GROUP_CAN_T_CREATE, CcCreateGroup, this->vli.vtype, this->vli.index);
777 break;
780 case WID_GL_DELETE_GROUP: { // Delete the selected group
781 this->group_confirm = this->vli.index;
782 ShowQuery(STR_QUERY_GROUP_DELETE_CAPTION, STR_GROUP_DELETE_QUERY_TEXT, this, DeleteGroupCallback);
783 break;
786 case WID_GL_RENAME_GROUP: // Rename the selected roup
787 this->ShowRenameGroupWindow(this->vli.index, false);
788 break;
790 case WID_GL_LIVERY_GROUP: // Set group livery
791 ShowCompanyLiveryWindow(this->owner, this->vli.index);
792 break;
794 case WID_GL_AVAILABLE_VEHICLES:
795 ShowBuildVehicleWindow(INVALID_TILE, this->vli.vtype);
796 break;
798 case WID_GL_MANAGE_VEHICLES_DROPDOWN: {
799 ShowDropDownList(this, this->BuildActionDropdownList(true, Group::IsValidID(this->vli.index)), 0, WID_GL_MANAGE_VEHICLES_DROPDOWN);
800 break;
803 case WID_GL_START_ALL:
804 case WID_GL_STOP_ALL: { // Start/stop all vehicles of the list
805 Command<CMD_MASS_START_STOP>::Post(0, widget == WID_GL_START_ALL, true, this->vli);
806 break;
809 case WID_GL_REPLACE_PROTECTION: {
810 const Group *g = Group::GetIfValid(this->vli.index);
811 if (g != nullptr) {
812 Command<CMD_SET_GROUP_FLAG>::Post(this->vli.index, GroupFlags::GF_REPLACE_PROTECTION, !HasBit(g->flags, GroupFlags::GF_REPLACE_PROTECTION), _ctrl_pressed);
814 break;
819 void OnDragDrop_Group(Point pt, int widget)
821 const Group *g = Group::Get(this->group_sel);
823 switch (widget) {
824 case WID_GL_ALL_VEHICLES: // All vehicles
825 case WID_GL_DEFAULT_VEHICLES: // Ungrouped vehicles
826 if (g->parent != INVALID_GROUP) {
827 Command<CMD_ALTER_GROUP>::Post(STR_ERROR_GROUP_CAN_T_SET_PARENT, AlterGroupMode::SetParent, this->group_sel, INVALID_GROUP, {});
830 this->group_sel = INVALID_GROUP;
831 this->group_over = INVALID_GROUP;
832 this->SetDirty();
833 break;
835 case WID_GL_LIST_GROUP: { // Matrix group
836 uint id_g = this->group_sb->GetScrolledRowFromWidget(pt.y, this, WID_GL_LIST_GROUP);
837 GroupID new_g = id_g >= this->groups.size() ? INVALID_GROUP : this->groups[id_g]->index;
839 if (this->group_sel != new_g && g->parent != new_g) {
840 Command<CMD_ALTER_GROUP>::Post(STR_ERROR_GROUP_CAN_T_SET_PARENT, AlterGroupMode::SetParent, this->group_sel, new_g, {});
843 this->group_sel = INVALID_GROUP;
844 this->group_over = INVALID_GROUP;
845 this->SetDirty();
846 break;
851 void OnDragDrop_Vehicle(Point pt, int widget)
853 switch (widget) {
854 case WID_GL_DEFAULT_VEHICLES: // Ungrouped vehicles
855 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);
857 this->vehicle_sel = INVALID_VEHICLE;
858 this->group_over = INVALID_GROUP;
860 this->SetDirty();
861 break;
863 case WID_GL_LIST_GROUP: { // Matrix group
864 const VehicleID vindex = this->vehicle_sel;
865 this->vehicle_sel = INVALID_VEHICLE;
866 this->group_over = INVALID_GROUP;
867 this->SetDirty();
869 uint id_g = this->group_sb->GetScrolledRowFromWidget(pt.y, this, WID_GL_LIST_GROUP);
870 GroupID new_g = id_g >= this->groups.size() ? NEW_GROUP : this->groups[id_g]->index;
872 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);
873 break;
876 case WID_GL_LIST_VEHICLE: { // Matrix vehicle
877 const VehicleID vindex = this->vehicle_sel;
878 this->vehicle_sel = INVALID_VEHICLE;
879 this->group_over = INVALID_GROUP;
880 this->SetDirty();
882 uint id_v = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_GL_LIST_VEHICLE);
883 if (id_v >= this->vehgroups.size()) return; // click out of list bound
885 const GUIVehicleGroup &vehgroup = this->vehgroups[id_v];
886 switch (this->grouping) {
887 case GB_NONE: {
888 const Vehicle *v = vehgroup.GetSingleVehicle();
889 if (!VehicleClicked(v) && vindex == v->index) {
890 ShowVehicleViewWindow(v);
892 break;
895 case GB_SHARED_ORDERS: {
896 const Vehicle *v = vehgroup.vehicles_begin[0];
897 /* We do not support VehicleClicked() here since the contextual action may only make sense for individual vehicles */
899 if (vindex == v->index) {
900 if (vehgroup.NumVehicles() == 1) {
901 ShowVehicleViewWindow(v);
902 } else {
903 ShowVehicleListWindow(v);
906 break;
909 default:
910 NOT_REACHED();
912 break;
917 void OnDragDrop(Point pt, int widget) override
919 if (this->vehicle_sel != INVALID_VEHICLE) OnDragDrop_Vehicle(pt, widget);
920 if (this->group_sel != INVALID_GROUP) OnDragDrop_Group(pt, widget);
922 _cursor.vehchain = false;
925 void OnQueryTextFinished(char *str) override
927 if (str != nullptr) Command<CMD_ALTER_GROUP>::Post(STR_ERROR_GROUP_CAN_T_RENAME, AlterGroupMode::Rename, this->group_rename, 0, str);
928 this->group_rename = INVALID_GROUP;
931 void OnResize() override
933 this->group_sb->SetCapacityFromWidget(this, WID_GL_LIST_GROUP);
934 this->vscroll->SetCapacityFromWidget(this, WID_GL_LIST_VEHICLE);
937 void OnDropdownSelect(int widget, int index) override
939 switch (widget) {
940 case WID_GL_GROUP_BY_DROPDOWN:
941 this->UpdateVehicleGroupBy(static_cast<GroupBy>(index));
942 break;
944 case WID_GL_SORT_BY_DROPDOWN:
945 this->vehgroups.SetSortType(index);
946 break;
948 case WID_GL_MANAGE_VEHICLES_DROPDOWN:
949 assert(this->vehicles.size() != 0);
951 switch (index) {
952 case ADI_REPLACE: // Replace window
953 ShowReplaceGroupVehicleWindow(this->vli.index, this->vli.vtype);
954 break;
955 case ADI_SERVICE: // Send for servicing
956 case ADI_DEPOT: { // Send to Depots
957 Command<CMD_SEND_VEHICLE_TO_DEPOT>::Post(GetCmdSendToDepotMsg(this->vli.vtype), 0, DepotCommand::MassSend | (index == ADI_SERVICE ? DepotCommand::Service : DepotCommand::None), this->vli);
958 break;
961 case ADI_ADD_SHARED: // Add shared Vehicles
962 assert(Group::IsValidID(this->vli.index));
964 Command<CMD_ADD_SHARED_VEHICLE_GROUP>::Post(STR_ERROR_GROUP_CAN_T_ADD_SHARED_VEHICLE, this->vli.index, this->vli.vtype);
965 break;
966 case ADI_REMOVE_ALL: // Remove all Vehicles from the selected group
967 assert(Group::IsValidID(this->vli.index));
969 Command<CMD_REMOVE_ALL_VEHICLES_GROUP>::Post(STR_ERROR_GROUP_CAN_T_REMOVE_ALL_VEHICLES, this->vli.index);
970 break;
971 default: NOT_REACHED();
973 break;
975 default: NOT_REACHED();
978 this->SetDirty();
981 void OnGameTick() override
983 if (this->groups.NeedResort() || this->vehgroups.NeedResort()) {
984 this->SetDirty();
988 void OnPlaceObjectAbort() override
990 /* abort drag & drop */
991 this->vehicle_sel = INVALID_VEHICLE;
992 this->DirtyHighlightedGroupWidget();
993 this->group_sel = INVALID_GROUP;
994 this->group_over = INVALID_GROUP;
995 this->SetWidgetDirty(WID_GL_LIST_VEHICLE);
998 void OnMouseDrag(Point pt, int widget) override
1000 if (this->vehicle_sel == INVALID_VEHICLE && this->group_sel == INVALID_GROUP) return;
1002 /* A vehicle is dragged over... */
1003 GroupID new_group_over = INVALID_GROUP;
1004 switch (widget) {
1005 case WID_GL_DEFAULT_VEHICLES: // ... the 'default' group.
1006 new_group_over = DEFAULT_GROUP;
1007 break;
1009 case WID_GL_LIST_GROUP: { // ... the list of custom groups.
1010 uint id_g = this->group_sb->GetScrolledRowFromWidget(pt.y, this, WID_GL_LIST_GROUP);
1011 new_group_over = id_g >= this->groups.size() ? NEW_GROUP : this->groups[id_g]->index;
1012 break;
1016 /* Do not highlight when dragging over the current group */
1017 if (this->vehicle_sel != INVALID_VEHICLE) {
1018 if (Vehicle::Get(vehicle_sel)->group_id == new_group_over) new_group_over = INVALID_GROUP;
1019 } else if (this->group_sel != INVALID_GROUP) {
1020 if (this->group_sel == new_group_over || Group::Get(this->group_sel)->parent == new_group_over) new_group_over = INVALID_GROUP;
1023 /* Mark widgets as dirty if the group changed. */
1024 if (new_group_over != this->group_over) {
1025 this->DirtyHighlightedGroupWidget();
1026 this->group_over = new_group_over;
1027 this->DirtyHighlightedGroupWidget();
1031 void ShowRenameGroupWindow(GroupID group, bool empty)
1033 assert(Group::IsValidID(group));
1034 this->group_rename = group;
1035 /* Show empty query for new groups */
1036 StringID str = STR_EMPTY;
1037 if (!empty) {
1038 SetDParam(0, group);
1039 str = STR_GROUP_NAME;
1041 ShowQueryString(str, STR_GROUP_RENAME_CAPTION, MAX_LENGTH_GROUP_NAME_CHARS, this, CS_ALPHANUMERAL, QSF_ENABLE_DEFAULT | QSF_LEN_IN_CHARS);
1045 * Tests whether a given vehicle is selected in the window, and unselects it if necessary.
1046 * Called when the vehicle is deleted.
1047 * @param vehicle Vehicle that is going to be deleted
1049 void UnselectVehicle(VehicleID vehicle)
1051 if (this->vehicle_sel == vehicle) ResetObjectToPlace();
1055 * Selects the specified group in the list
1057 * @param g_id The ID of the group to be selected
1059 void SelectGroup(const GroupID g_id)
1061 if (g_id == INVALID_GROUP || g_id == this->vli.index) return;
1063 this->vli.index = g_id;
1064 if (g_id != ALL_GROUP && g_id != DEFAULT_GROUP) {
1065 const Group *g = Group::Get(g_id);
1066 int id_g = find_index(this->groups, g);
1067 // The group's branch is maybe collapsed, so try to expand it
1068 if (id_g == -1) {
1069 for (auto pg = Group::GetIfValid(g->parent); pg != nullptr; pg = Group::GetIfValid(pg->parent)) {
1070 pg->folded = false;
1072 this->groups.ForceRebuild();
1073 this->BuildGroupList(this->owner);
1074 this->group_sb->SetCount((uint)this->groups.size());
1075 id_g = find_index(this->groups, g);
1077 this->group_sb->ScrollTowards(id_g);
1079 this->vehgroups.ForceRebuild();
1080 this->SetDirty();
1086 static WindowDesc _other_group_desc(
1087 WDP_AUTO, "list_groups", 460, 246,
1088 WC_INVALID, WC_NONE,
1090 _nested_group_widgets, lengthof(_nested_group_widgets)
1093 static WindowDesc _train_group_desc(
1094 WDP_AUTO, "list_groups_train", 525, 246,
1095 WC_TRAINS_LIST, WC_NONE,
1097 _nested_group_widgets, lengthof(_nested_group_widgets)
1101 * Show the group window for the given company and vehicle type.
1102 * @param company The company to show the window for.
1103 * @param vehicle_type The type of vehicle to show it for.
1104 * @param group The group to be selected. Defaults to INVALID_GROUP.
1105 * @param need_existing_window Whether the existing window is needed. Defaults to false.
1107 void ShowCompanyGroup(CompanyID company, VehicleType vehicle_type, GroupID group = INVALID_GROUP, bool need_existing_window = false)
1109 if (!Company::IsValidID(company)) return;
1111 const WindowNumber num = VehicleListIdentifier(VL_GROUP_LIST, vehicle_type, company).Pack();
1112 VehicleGroupWindow *w;
1113 if (vehicle_type == VEH_TRAIN) {
1114 w = AllocateWindowDescFront<VehicleGroupWindow>(&_train_group_desc, num, need_existing_window);
1115 } else {
1116 _other_group_desc.cls = GetWindowClassForVehicleType(vehicle_type);
1117 w = AllocateWindowDescFront<VehicleGroupWindow>(&_other_group_desc, num, need_existing_window);
1119 if (w != nullptr) w->SelectGroup(group);
1123 * Show the group window for the given vehicle.
1124 * @param v The vehicle to show the window for.
1126 void ShowCompanyGroupForVehicle(const Vehicle *v)
1128 ShowCompanyGroup(v->owner, v->type, v->group_id, true);
1132 * Finds a group list window determined by vehicle type and owner
1133 * @param vt vehicle type
1134 * @param owner owner of groups
1135 * @return pointer to VehicleGroupWindow, nullptr if not found
1137 static inline VehicleGroupWindow *FindVehicleGroupWindow(VehicleType vt, Owner owner)
1139 return (VehicleGroupWindow *)FindWindowById(GetWindowClassForVehicleType(vt), VehicleListIdentifier(VL_GROUP_LIST, vt, owner).Pack());
1143 * Opens a 'Rename group' window for newly created group.
1144 * @param veh_type Vehicle type.
1146 static void CcCreateGroup(GroupID gid, VehicleType veh_type)
1148 VehicleGroupWindow *w = FindVehicleGroupWindow(veh_type, _current_company);
1149 if (w != nullptr) w->ShowRenameGroupWindow(gid, true);
1153 * Opens a 'Rename group' window for newly created group.
1154 * @param cmd Unused.
1155 * @param result Did command succeed?
1156 * @param new_group ID of the created group.
1157 * @param vt Vehicle type.
1158 * @param parent_group Parent group of the new group.
1159 * @see CmdCreateGroup
1161 void CcCreateGroup(Commands cmd, const CommandCost &result, GroupID new_group, VehicleType vt, GroupID parent_group)
1163 if (result.Failed()) return;
1165 assert(vt <= VEH_AIRCRAFT);
1166 CcCreateGroup(new_group, vt);
1170 * Open rename window after adding a vehicle to a new group via drag and drop.
1171 * @param cmd Unused.
1172 * @param result Did command succeed?
1173 * @param new_group ID of the created group.
1174 * @param veh_id vehicle to add to a group
1176 void CcAddVehicleNewGroup(Commands cmd, const CommandCost &result, GroupID new_group, GroupID, VehicleID veh_id, bool)
1178 if (result.Failed()) return;
1180 assert(Vehicle::IsValidID(veh_id));
1181 CcCreateGroup(new_group, Vehicle::Get(veh_id)->type);
1185 * Removes the highlight of a vehicle in a group window
1186 * @param *v Vehicle to remove all highlights from
1188 void DeleteGroupHighlightOfVehicle(const Vehicle *v)
1190 /* If we haven't got any vehicles on the mouse pointer, we haven't got any highlighted in any group windows either
1191 * If that is the case, we can skip looping though the windows and save time
1193 if (_special_mouse_mode != WSM_DRAGDROP) return;
1195 VehicleGroupWindow *w = FindVehicleGroupWindow(v->type, v->owner);
1196 if (w != nullptr) w->UnselectVehicle(v->index);