Make it possible to stop road vehicles from slowing down in curves so "diagonal"...
[openttd-joker.git] / src / group_gui.cpp
blob3c51d96a391d0de8e84a90685727364e7e4e76ae
1 /* $Id$ */
3 /*
4 * This file is part of OpenTTD.
5 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
6 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 */
10 /** @file group_gui.cpp GUI for the group window. */
12 #include "stdafx.h"
13 #include "textbuf_gui.h"
14 #include "command_func.h"
15 #include "vehicle_gui.h"
16 #include "vehicle_base.h"
17 #include "string_func.h"
18 #include "strings_func.h"
19 #include "window_func.h"
20 #include "vehicle_func.h"
21 #include "autoreplace_gui.h"
22 #include "company_func.h"
23 #include "widgets/dropdown_func.h"
24 #include "tilehighlight_func.h"
25 #include "vehicle_gui_base.h"
26 #include "core/geometry_func.hpp"
27 #include "company_base.h"
28 #include "tbtr_template_gui_main.h"
29 #include "widgets/group_widget.h"
30 #include "table/sprites.h"
32 #include "safeguards.h"
34 static const int LEVEL_WIDTH = 10; ///< Indenting width of a sub-group in pixels
36 typedef GUIList<const Group*> GUIGroupList;
38 static void SellAllConfirmationCallback(Window *win, bool confirmed);
40 static const NWidgetPart _nested_group_widgets[] = {
41 NWidget(NWID_HORIZONTAL), // Window header
42 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
43 NWidget(WWT_CAPTION, COLOUR_GREY, WID_GL_CAPTION),
44 NWidget(WWT_SHADEBOX, COLOUR_GREY),
45 NWidget(WWT_DEFSIZEBOX, COLOUR_GREY),
46 NWidget(WWT_STICKYBOX, COLOUR_GREY),
47 EndContainer(),
48 NWidget(NWID_HORIZONTAL),
49 /* left part */
50 NWidget(NWID_VERTICAL),
51 NWidget(WWT_PANEL, COLOUR_GREY), SetMinimalTextLines(1, WD_DROPDOWNTEXT_TOP + WD_DROPDOWNTEXT_BOTTOM), SetFill(1, 0), EndContainer(),
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), SetMinimalSize(200, 80), SetFill(1, 0), EndContainer(),
60 NWidget(NWID_HORIZONTAL),
61 NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_GL_CREATE_GROUP), SetFill(0, 1),
62 SetDataTip(SPR_GROUP_CREATE_TRAIN, STR_GROUP_CREATE_TOOLTIP),
63 NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_GL_DELETE_GROUP), SetFill(0, 1),
64 SetDataTip(SPR_GROUP_DELETE_TRAIN, STR_GROUP_DELETE_TOOLTIP),
65 NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_GL_RENAME_GROUP), SetFill(0, 1),
66 SetDataTip(SPR_GROUP_RENAME_TRAIN, STR_GROUP_RENAME_TOOLTIP),
67 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_GL_COLLAPSE_EXPAND_GROUP), SetFill(0, 1),
68 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_GL_COLLAPSE_ALL_GROUPS), SetFill(0, 1),
69 SetDataTip(STR_GROUP_COLLAPSE_ALL, STR_GROUP_COLLAPSE_ALL),
70 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_GL_EXPAND_ALL_GROUPS), SetFill(0, 1),
71 SetDataTip(STR_GROUP_EXPAND_ALL, STR_GROUP_EXPAND_ALL),
72 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_GL_AUTO_GROUP), SetFill(0, 1),
73 SetDataTip(STR_AUTO_GROUP, STR_AUTO_GROUP_TIP),
74 NWidget(WWT_PANEL, COLOUR_GREY), SetFill(1, 1), EndContainer(),
75 NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_GL_REPLACE_PROTECTION), SetFill(0, 1),
76 SetDataTip(SPR_GROUP_REPLACE_OFF_TRAIN, STR_GROUP_REPLACE_PROTECTION_TOOLTIP),
77 EndContainer(),
78 EndContainer(),
79 /* right part */
80 NWidget(NWID_VERTICAL),
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_DROPDOWN, COLOUR_GREY, WID_GL_FILTER_BY_CARGO), SetMinimalSize(167, 12), SetDataTip(STR_JUST_STRING, STR_TOOLTIP_FILTER_CRITERIA),
85 NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_GL_REFRESH_SORTING), SetMinimalSize(167, 12), SetFill(0, 1), SetDataTip(STR_BUTTON_REFRESH_SORTING, STR_TOOLTIP_REFRESH_SORTING),
86 NWidget(WWT_PANEL, COLOUR_GREY), SetMinimalSize(12, 12), SetResize(1, 0), EndContainer(),
87 EndContainer(),
88 NWidget(NWID_HORIZONTAL),
89 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),
90 NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_GL_LIST_VEHICLE_SCROLLBAR),
91 EndContainer(),
92 NWidget(WWT_PANEL, COLOUR_GREY), SetMinimalSize(1, 0), SetFill(1, 1), SetResize(1, 0), EndContainer(),
93 NWidget(NWID_HORIZONTAL),
94 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_GL_AVAILABLE_VEHICLES), SetMinimalSize(106, 12), SetFill(0, 1),
95 SetDataTip(STR_BLACK_STRING, STR_VEHICLE_LIST_AVAILABLE_ENGINES_TOOLTIP),
96 NWidget(WWT_PANEL, COLOUR_GREY), SetMinimalSize(0, 12), SetFill(1, 1), SetResize(1, 0), EndContainer(),
97 NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_GL_MANAGE_VEHICLES_DROPDOWN), SetMinimalSize(118, 12), SetFill(0, 1),
98 SetDataTip(STR_VEHICLE_LIST_MANAGE_LIST, STR_VEHICLE_LIST_MANAGE_LIST_TOOLTIP),
99 NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_GL_STOP_ALL), SetMinimalSize(12, 12), SetFill(0, 1),
100 SetDataTip(SPR_FLAG_VEH_STOPPED, STR_VEHICLE_LIST_MASS_STOP_LIST_TOOLTIP),
101 NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_GL_START_ALL), SetMinimalSize(12, 12), SetFill(0, 1),
102 SetDataTip(SPR_FLAG_VEH_RUNNING, STR_VEHICLE_LIST_MASS_START_LIST_TOOLTIP),
103 NWidget(WWT_RESIZEBOX, COLOUR_GREY),
104 EndContainer(),
105 EndContainer(),
106 EndContainer(),
109 /** Sort the groups by their name */
110 int CDECL GroupNameSorter(const Group * const *a, const Group * const *b)
112 static const Group *last_group[2] = { nullptr, nullptr };
113 static char last_name[2][64] = { "", "" };
115 if (*a != last_group[0]) {
116 last_group[0] = *a;
117 SetDParam(0, (*a)->index);
118 GetString(last_name[0], STR_GROUP_NAME, lastof(last_name[0]));
121 if (*b != last_group[1]) {
122 last_group[1] = *b;
123 SetDParam(0, (*b)->index);
124 GetString(last_name[1], STR_GROUP_NAME, lastof(last_name[1]));
127 int r = strnatcmp(last_name[0], last_name[1]); // Sort by name (natural sorting).
128 if (r == 0) return (*a)->index - (*b)->index;
129 return r;
133 class VehicleGroupWindow : public BaseVehicleListWindow {
134 private:
135 /* Columns in the group list */
136 enum ListColumns {
137 VGC_NAME, ///< Group name.
138 VGC_COLLAPSED, ///< Collapsed icon
139 VGC_PROTECT, ///< Autoreplace protect icon.
140 VGC_AUTOREPLACE, ///< Autoreplace active icon.
141 VGC_PROFIT, ///< Profit icon.
142 VGC_NUMBER, ///< Number of vehicles in the group.
144 VGC_END
147 GroupID group_sel; ///< Selected group (for drag/drop)
148 GroupID group_rename; ///< Group being renamed, INVALID_GROUP if none
149 GroupID group_over; ///< Group over which a vehicle is dragged, INVALID_GROUP if none
150 GroupID group_confirm; ///< Group awaiting delete confirmation
151 GUIGroupList groups; ///< List of groups
152 uint tiny_step_height; ///< Step height for the group list
153 Scrollbar *group_sb;
155 SmallVector<GroupID, 16> all_groups; ///< List of all groups, including hidden ones
156 SmallVector<GroupID, 16> collapsed_groups; ///< List of collapsed groups
157 SmallVector<GroupID, 16> collapsable_groups; ///< List of collapsable groups
159 SmallVector<int, 16> indents; ///< Indentation levels
161 Dimension column_size[VGC_END]; ///< Size of the columns in the group list.
163 bool live_refresh_sorting; ///< Refresh the sorting regularly
165 /** return true if group has children */
166 bool AddParents(GUIGroupList *source, GroupID parent, int indent, bool parent_collapsed)
168 bool is_collapsed = this->collapsed_groups.Contains(parent);
169 bool overall_collapsed = is_collapsed || parent_collapsed;
170 bool has_children = false;
172 for (const Group **g = source->Begin(); g != source->End(); g++) {
173 if ((*g)->parent == parent) {
174 has_children = true;
175 *this->all_groups.Append() = (*g)->index;
176 if (!overall_collapsed) {
177 *this->groups.Append() = *g;
178 *this->indents.Append() = indent;
180 bool child_has_children = AddParents(source, (*g)->index, indent + 1, overall_collapsed);
181 if (child_has_children) *this->collapsable_groups.Append() = (*g)->index;
185 return has_children;
188 void ToogleGroupCollapse(GroupID group)
190 GroupID *item = this->collapsed_groups.Find(group);
191 if (item == this->collapsed_groups.End()) {
192 if (this->collapsable_groups.Find(group) != this->collapsable_groups.End()) {
193 *(this->collapsed_groups.Append()) = group;
195 } else {
196 this->collapsed_groups.Erase(item);
201 * (Re)Build the group list.
203 * @param owner The owner of the window
205 void BuildGroupList(Owner owner)
207 if (!this->groups.NeedRebuild()) return;
209 this->all_groups.Clear();
210 this->groups.Clear();
211 this->indents.Clear();
212 this->collapsable_groups.Clear();
214 GUIGroupList list;
216 const Group *g;
217 FOR_ALL_GROUPS(g) {
218 if (g->owner == owner && g->vehicle_type == this->vli.vtype) {
219 *list.Append() = g;
223 list.ForceResort();
224 list.Sort(&GroupNameSorter);
226 AddParents(&list, INVALID_GROUP, 0, false);
228 this->groups.Compact();
229 this->groups.RebuildDone();
233 * Compute tiny_step_height and column_size
234 * @return Total width required for the group list.
236 uint ComputeGroupInfoSize()
238 this->column_size[VGC_COLLAPSED] = GetSpriteSize(SPR_WINDOW_UNSHADE);
239 this->tiny_step_height = this->column_size[VGC_COLLAPSED].height;
241 this->column_size[VGC_NAME] = maxdim(GetStringBoundingBox(STR_GROUP_DEFAULT_TRAINS + this->vli.vtype), GetStringBoundingBox(STR_GROUP_ALL_TRAINS + this->vli.vtype));
242 this->column_size[VGC_NAME].width = max((170u * FONT_HEIGHT_NORMAL) / 10u, this->column_size[VGC_NAME].width);
243 this->tiny_step_height = max(this->tiny_step_height, this->column_size[VGC_NAME].height);
245 this->column_size[VGC_PROTECT] = GetSpriteSize(SPR_GROUP_REPLACE_PROTECT);
246 this->tiny_step_height = max(this->tiny_step_height, this->column_size[VGC_PROTECT].height);
248 this->column_size[VGC_AUTOREPLACE] = GetSpriteSize(SPR_GROUP_REPLACE_ACTIVE);
249 this->tiny_step_height = max(this->tiny_step_height, this->column_size[VGC_AUTOREPLACE].height);
251 this->column_size[VGC_PROFIT].width = 0;
252 this->column_size[VGC_PROFIT].height = 0;
253 static const SpriteID profit_sprites[] = {SPR_PROFIT_NA, SPR_PROFIT_NEGATIVE, SPR_PROFIT_SOME, SPR_PROFIT_LOT};
254 for (uint i = 0; i < lengthof(profit_sprites); i++) {
255 Dimension d = GetSpriteSize(profit_sprites[i]);
256 this->column_size[VGC_PROFIT] = maxdim(this->column_size[VGC_PROFIT], d);
258 this->tiny_step_height = max(this->tiny_step_height, this->column_size[VGC_PROFIT].height);
260 SetDParamMaxValue(0, GroupStatistics::Get(this->vli.company, ALL_GROUP, this->vli.vtype).num_vehicle, 3, FS_SMALL);
261 this->column_size[VGC_NUMBER] = GetStringBoundingBox(STR_TINY_COMMA);
262 this->tiny_step_height = max(this->tiny_step_height, this->column_size[VGC_NUMBER].height);
264 this->tiny_step_height += WD_MATRIX_TOP;
266 return WD_FRAMERECT_LEFT + 8 +
267 this->column_size[VGC_COLLAPSED].width + 2 +
268 this->column_size[VGC_NAME].width + 8 +
269 this->column_size[VGC_PROTECT].width + 2 +
270 this->column_size[VGC_AUTOREPLACE].width + 2 +
271 this->column_size[VGC_PROFIT].width + 2 +
272 this->column_size[VGC_NUMBER].width + 2 +
273 WD_FRAMERECT_RIGHT;
277 * Draw a row in the group list.
278 * @param y Top of the row.
279 * @param left Left of the row.
280 * @param right Right of the row.
281 * @param g_id Group to list.
282 * @param indent Indentation level.
283 * @param protection Whether autoreplac/// e protection is set.
285 void DrawGroupInfo(int y, int left, int right, GroupID g_id, int indent = 0, bool protection = false) const
287 /* Highlight the group if a vehicle is dragged over it */
288 if (g_id == this->group_over) {
289 GfxFillRect(left + WD_FRAMERECT_LEFT, y + WD_FRAMERECT_TOP, right - WD_FRAMERECT_RIGHT, y + this->tiny_step_height - WD_FRAMERECT_BOTTOM - WD_MATRIX_TOP, _colour_gradient[COLOUR_GREY][7]);
292 if (g_id == NEW_GROUP) return;
294 /* draw the selected group in white, else we draw it in black */
295 TextColour colour = g_id == this->vli.index ? TC_WHITE : TC_BLACK;
296 const GroupStatistics &stats = GroupStatistics::Get(this->vli.company, g_id, this->vli.vtype);
297 bool rtl = _current_text_dir == TD_RTL;
299 /* draw group name */
300 StringID str;
301 if (IsAllGroupID(g_id)) {
302 str = STR_GROUP_ALL_TRAINS + this->vli.vtype;
303 } else if (IsDefaultGroupID(g_id)) {
304 str = STR_GROUP_DEFAULT_TRAINS + this->vli.vtype;
305 } else {
306 SetDParam(0, g_id);
307 str = STR_GROUP_NAME;
310 /* draw collapse state */
311 int x = rtl ? right - WD_FRAMERECT_RIGHT - 8 - this->column_size[VGC_COLLAPSED].width + 1 : left + WD_FRAMERECT_LEFT + 8;
312 if (this->collapsed_groups.Contains(g_id)) DrawSprite(SPR_WINDOW_UNSHADE, PAL_NONE, x, y + (this->tiny_step_height - this->column_size[VGC_COLLAPSED].height) / 2);
314 x = rtl ? right - WD_FRAMERECT_RIGHT - 8 - this->column_size[VGC_NAME].width + 1 : x + 8 + this->column_size[VGC_COLLAPSED].width;
315 DrawString(x + indent * LEVEL_WIDTH, x + this->column_size[VGC_NAME].width - 1, y + (this->tiny_step_height - this->column_size[VGC_NAME].height) / 2, str, colour);
317 /* draw autoreplace protection */
318 x = rtl ? x - 2 - this->column_size[VGC_PROTECT].width : x + 2 + this->column_size[VGC_NAME].width;
319 if (protection) DrawSprite(SPR_GROUP_REPLACE_PROTECT, PAL_NONE, x, y + (this->tiny_step_height - this->column_size[VGC_PROTECT].height) / 2);
321 /* draw autoreplace status */
322 x = rtl ? x - 2 - this->column_size[VGC_AUTOREPLACE].width : x + 2 + this->column_size[VGC_PROTECT].width;
323 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);
325 /* draw the profit icon */
326 x = rtl ? x - 2 - this->column_size[VGC_PROFIT].width : x + 2 + this->column_size[VGC_AUTOREPLACE].width;
327 SpriteID spr;
328 if (stats.num_profit_vehicle == 0) {
329 spr = SPR_PROFIT_NA;
330 } else if (stats.profit_last_year < 0) {
331 spr = SPR_PROFIT_NEGATIVE;
332 } else if (stats.profit_last_year < 10000 * stats.num_profit_vehicle) { // TODO magic number
333 spr = SPR_PROFIT_SOME;
334 } else {
335 spr = SPR_PROFIT_LOT;
337 DrawSprite(spr, PAL_NONE, x, y + (this->tiny_step_height - this->column_size[VGC_PROFIT].height) / 2);
339 /* draw the number of vehicles of the group */
340 x = rtl ? x - 2 - this->column_size[VGC_NUMBER].width : x + 2 + this->column_size[VGC_PROFIT].width;
341 SetDParam(0, stats.num_vehicle);
342 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);
346 * Mark the widget containing the currently highlighted group as dirty.
348 void DirtyHighlightedGroupWidget()
350 if (this->group_over == INVALID_GROUP) return;
352 if (IsAllGroupID(this->group_over)) {
353 this->SetWidgetDirty(WID_GL_ALL_VEHICLES);
354 } else if (IsDefaultGroupID(this->group_over)) {
355 this->SetWidgetDirty(WID_GL_DEFAULT_VEHICLES);
356 } else {
357 this->SetWidgetDirty(WID_GL_LIST_GROUP);
361 public:
362 VehicleGroupWindow(WindowDesc *desc, WindowNumber window_number) : BaseVehicleListWindow(desc, window_number)
364 this->CreateNestedTree();
366 this->vscroll = this->GetScrollbar(WID_GL_LIST_VEHICLE_SCROLLBAR);
367 this->group_sb = this->GetScrollbar(WID_GL_LIST_GROUP_SCROLLBAR);
369 switch (this->vli.vtype) {
370 default: NOT_REACHED();
371 case VEH_TRAIN: this->sorting = &_sorting.train; break;
372 case VEH_ROAD: this->sorting = &_sorting.roadveh; break;
373 case VEH_SHIP: this->sorting = &_sorting.ship; break;
374 case VEH_AIRCRAFT: this->sorting = &_sorting.aircraft; break;
377 this->vli.index = ALL_GROUP;
378 this->group_sel = INVALID_GROUP;
379 this->group_rename = INVALID_GROUP;
380 this->group_over = INVALID_GROUP;
382 this->vehicles.SetListing(*this->sorting);
383 this->vehicles.ForceRebuild();
384 this->vehicles.NeedResort();
386 this->BuildVehicleList();
387 this->SortVehicleList();
389 this->groups.ForceRebuild();
390 this->groups.NeedResort();
391 this->BuildGroupList(vli.company);
393 this->GetWidget<NWidgetCore>(WID_GL_CAPTION)->widget_data = STR_VEHICLE_LIST_TRAIN_CAPTION + this->vli.vtype;
394 this->GetWidget<NWidgetCore>(WID_GL_LIST_VEHICLE)->tool_tip = STR_VEHICLE_LIST_TRAIN_LIST_TOOLTIP + this->vli.vtype;
396 this->GetWidget<NWidgetCore>(WID_GL_COLLAPSE_EXPAND_GROUP)->widget_data = STR_GROUP_COLLAPSE;
397 this->GetWidget<NWidgetCore>(WID_GL_COLLAPSE_EXPAND_GROUP)->tool_tip = STR_GROUP_COLLAPSE_TOOLTIP;
399 this->GetWidget<NWidgetCore>(WID_GL_CREATE_GROUP)->widget_data += this->vli.vtype;
400 this->GetWidget<NWidgetCore>(WID_GL_RENAME_GROUP)->widget_data += this->vli.vtype;
401 this->GetWidget<NWidgetCore>(WID_GL_DELETE_GROUP)->widget_data += this->vli.vtype;
402 this->GetWidget<NWidgetCore>(WID_GL_REPLACE_PROTECTION)->widget_data += this->vli.vtype;
404 this->FinishInitNested(window_number);
405 this->owner = vli.company;
408 ~VehicleGroupWindow()
410 *this->sorting = this->vehicles.GetListing();
413 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
415 switch (widget) {
416 case WID_GL_LIST_GROUP: {
417 size->width = this->ComputeGroupInfoSize();
418 resize->height = this->tiny_step_height;
420 /* Minimum height is the height of the list widget minus all and default vehicles... */
421 size->height = 4 * GetVehicleListHeight(this->vli.vtype, this->tiny_step_height) - 2 * this->tiny_step_height;
423 /* ... minus the buttons at the bottom ... */
424 uint max_icon_height = GetSpriteSize(this->GetWidget<NWidgetCore>(WID_GL_CREATE_GROUP)->widget_data).height;
425 max_icon_height = max(max_icon_height, GetSpriteSize(this->GetWidget<NWidgetCore>(WID_GL_RENAME_GROUP)->widget_data).height);
426 max_icon_height = max(max_icon_height, GetSpriteSize(this->GetWidget<NWidgetCore>(WID_GL_DELETE_GROUP)->widget_data).height);
427 max_icon_height = max(max_icon_height, GetSpriteSize(this->GetWidget<NWidgetCore>(WID_GL_REPLACE_PROTECTION)->widget_data).height);
429 /* ... minus the height of the group info ... */
430 max_icon_height += (FONT_HEIGHT_NORMAL * 3) + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
432 /* Get a multiple of tiny_step_height of that amount */
433 size->height = Ceil(size->height - max_icon_height, tiny_step_height);
434 break;
437 case WID_GL_ALL_VEHICLES:
438 case WID_GL_DEFAULT_VEHICLES:
439 size->width = this->ComputeGroupInfoSize();
440 size->height = this->tiny_step_height;
441 break;
443 case WID_GL_SORT_BY_ORDER: {
444 Dimension d = GetStringBoundingBox(this->GetWidget<NWidgetCore>(widget)->widget_data);
445 d.width += padding.width + Window::SortButtonWidth() * 2; // Doubled since the string is centred and it also looks better.
446 d.height += padding.height;
447 *size = maxdim(*size, d);
448 break;
451 case WID_GL_LIST_VEHICLE:
452 this->ComputeGroupInfoSize();
453 resize->height = GetVehicleListHeight(this->vli.vtype, this->tiny_step_height);
454 size->height = 4 * resize->height;
455 break;
457 case WID_GL_MANAGE_VEHICLES_DROPDOWN: {
458 Dimension d = this->GetActionDropdownSize(true, true, this->vli.vtype == VEH_TRAIN);
459 d.height += padding.height;
460 d.width += padding.width;
461 *size = maxdim(*size, d);
462 break;
465 case WID_GL_INFO: {
466 size->height = (FONT_HEIGHT_NORMAL * 3) + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM + 5;
467 break;
473 * Some data on this window has become invalid.
474 * @param data Information about the changed data.
475 * @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.
477 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
479 if (data == 0) {
480 /* This needs to be done in command-scope to enforce rebuilding before resorting invalid data */
481 this->vehicles.ForceRebuild();
482 this->groups.ForceRebuild();
483 } else {
484 this->vehicles.ForceResort();
485 this->groups.ForceResort();
488 /* Process ID-invalidation in command-scope as well */
489 if (this->group_rename != INVALID_GROUP && !Group::IsValidID(this->group_rename)) {
490 DeleteWindowByClass(WC_QUERY_STRING);
491 this->group_rename = INVALID_GROUP;
494 if (!(IsAllGroupID(this->vli.index) || IsDefaultGroupID(this->vli.index) || Group::IsValidID(this->vli.index))) {
495 this->vli.index = ALL_GROUP;
496 HideDropDownMenu(this);
498 this->SetDirty();
501 virtual void SetStringParameters(int widget) const
503 switch (widget) {
504 case WID_GL_FILTER_BY_CARGO:
505 SetDParam(0, this->cargo_filter_texts[this->cargo_filter_criteria]);
506 break;
508 case WID_GL_AVAILABLE_VEHICLES:
509 SetDParam(0, STR_VEHICLE_LIST_AVAILABLE_TRAINS + this->vli.vtype);
510 break;
512 case WID_GL_CAPTION:
513 /* If selected_group == DEFAULT_GROUP || ALL_GROUP, draw the standard caption
514 * We list all vehicles or ungrouped vehicles */
515 if (IsDefaultGroupID(this->vli.index) || IsAllGroupID(this->vli.index)) {
516 SetDParam(0, STR_COMPANY_NAME);
517 SetDParam(1, this->vli.company);
518 SetDParam(2, this->vehicles.Length());
519 SetDParam(3, this->vehicles.Length());
520 } else {
521 const Group *g = Group::Get(this->vli.index);
523 SetDParam(0, STR_GROUP_NAME);
524 SetDParam(1, g->index);
525 SetDParam(2, g->statistics.num_vehicle);
526 SetDParam(3, g->statistics.num_vehicle);
528 break;
532 virtual void OnPaint()
534 /* If we select the all vehicles, this->list will contain all vehicles of the owner
535 * else this->list will contain all vehicles which belong to the selected group */
536 this->BuildVehicleList();
537 this->SortVehicleList();
539 this->BuildGroupList(this->owner);
541 this->group_sb->SetCount(this->groups.Length());
542 this->vscroll->SetCount(this->vehicles.Length());
544 /* The drop down menu is out, *but* it may not be used, retract it. */
545 if (!this->ShouldShowActionDropdownList() && this->IsWidgetLowered(WID_GL_MANAGE_VEHICLES_DROPDOWN)) {
546 this->RaiseWidget(WID_GL_MANAGE_VEHICLES_DROPDOWN);
547 HideDropDownMenu(this);
550 /* Disable all lists management button when the list is empty */
551 this->SetWidgetDisabledState(WID_GL_MANAGE_VEHICLES_DROPDOWN, !this->ShouldShowActionDropdownList() || _local_company != this->vli.company);
552 this->SetWidgetsDisabledState(this->vehicles.Length() == 0 || _local_company != this->vli.company,
553 WID_GL_STOP_ALL,
554 WID_GL_START_ALL,
555 WIDGET_LIST_END);
557 /* Disable the group specific function when we select the default group or all vehicles */
558 this->SetWidgetsDisabledState(IsDefaultGroupID(this->vli.index) || IsAllGroupID(this->vli.index) || _local_company != this->vli.company,
559 WID_GL_DELETE_GROUP,
560 WID_GL_RENAME_GROUP,
561 WID_GL_REPLACE_PROTECTION,
562 WIDGET_LIST_END);
564 /* Disable remaining buttons for non-local companies
565 * Needed while changing _local_company, eg. by cheats
566 * All procedures (eg. move vehicle to another group)
567 * verify, whether you are the owner of the vehicle,
568 * so it doesn't have to be disabled
570 this->SetWidgetsDisabledState(_local_company != this->vli.company,
571 WID_GL_CREATE_GROUP,
572 WID_GL_AVAILABLE_VEHICLES,
573 WIDGET_LIST_END);
575 /* If not a default group and the group has replace protection, show an enabled replace sprite. */
576 uint16 protect_sprite = SPR_GROUP_REPLACE_OFF_TRAIN;
577 if (!IsDefaultGroupID(this->vli.index) && !IsAllGroupID(this->vli.index) && Group::Get(this->vli.index)->replace_protection) protect_sprite = SPR_GROUP_REPLACE_ON_TRAIN;
578 this->GetWidget<NWidgetCore>(WID_GL_REPLACE_PROTECTION)->widget_data = protect_sprite + this->vli.vtype;
580 /* Set text of sort by dropdown */
581 this->GetWidget<NWidgetCore>(WID_GL_SORT_BY_DROPDOWN)->widget_data = this->vehicle_sorter_names[this->vehicles.SortType()];
583 this->GetWidget<NWidgetCore>(WID_GL_FILTER_BY_CARGO)->widget_data = this->cargo_filter_texts[this->cargo_filter_criteria];
585 bool is_non_collapsable_group = (this->vli.index == ALL_GROUP) || (this->vli.index == DEFAULT_GROUP)
586 || (this->vli.index == INVALID_GROUP) || !this->collapsable_groups.Contains(this->vli.index);
588 this->SetWidgetDisabledState(WID_GL_COLLAPSE_EXPAND_GROUP, is_non_collapsable_group);
590 NWidgetCore *collapse_widget = this->GetWidget<NWidgetCore>(WID_GL_COLLAPSE_EXPAND_GROUP);
592 if (is_non_collapsable_group || !this->collapsed_groups.Contains(this->vli.index)) {
593 collapse_widget->widget_data = STR_GROUP_COLLAPSE;
594 collapse_widget->tool_tip = STR_GROUP_COLLAPSE_TOOLTIP;
595 } else {
596 collapse_widget->widget_data = STR_GROUP_EXPAND;
597 collapse_widget->tool_tip = STR_GROUP_EXPAND_TOOLTIP;
600 this->SetWidgetDisabledState(WID_GL_EXPAND_ALL_GROUPS, this->collapsed_groups.Length() == 0);
601 this->SetWidgetDisabledState(WID_GL_COLLAPSE_ALL_GROUPS, this->collapsable_groups.Length() == 0 || this->collapsed_groups.Length() == this->collapsable_groups.Length());
603 this->SetWidgetLoweredState(WID_GL_REFRESH_SORTING, this->live_refresh_sorting);
605 this->DrawWidgets();
608 virtual void DrawWidget(const Rect &r, int widget) const
610 switch (widget) {
611 case WID_GL_ALL_VEHICLES:
612 DrawGroupInfo(r.top + WD_FRAMERECT_TOP, r.left, r.right, ALL_GROUP);
613 break;
615 case WID_GL_DEFAULT_VEHICLES:
616 DrawGroupInfo(r.top + WD_FRAMERECT_TOP, r.left, r.right, DEFAULT_GROUP);
617 break;
619 case WID_GL_INFO: {
620 Money this_year = 0;
621 Money last_year = 0;
622 Money lifetime = 0;
623 uint32 occupancy = 0;
624 uint32 group_vehicles = 0;
626 for (uint i = 0, vehicle_count = this->vehicles.Length(); i < vehicle_count; i++) {
627 const Vehicle *v = this->vehicles[i];
629 assert(v->owner == this->owner);
631 this_year += v->GetDisplayProfitThisYear();
632 last_year += v->GetDisplayProfitLastYear();
633 lifetime += v->GetDisplayProfitLifetime();
636 DrawString(r.left + WD_FRAMERECT_LEFT + 8, r.right - WD_FRAMERECT_RIGHT - 16, r.top + WD_FRAMERECT_TOP + 1, STR_GROUP_PROFIT_THIS_YEAR, TC_BLACK);
637 SetDParam(0, this_year);
638 DrawString(r.left + WD_FRAMERECT_LEFT + 8, r.right - WD_FRAMERECT_RIGHT - 16, r.top + WD_FRAMERECT_TOP + 1, STR_JUST_CURRENCY_LONG, TC_BLACK, SA_RIGHT);
640 DrawString(r.left + WD_FRAMERECT_LEFT + 8, r.right - WD_FRAMERECT_RIGHT - 16, r.top + WD_FRAMERECT_TOP + FONT_HEIGHT_NORMAL + 2, STR_GROUP_PROFIT_LAST_YEAR, TC_BLACK);
641 SetDParam(0, last_year);
642 DrawString(r.left + WD_FRAMERECT_LEFT + 8, r.right - WD_FRAMERECT_RIGHT - 16, r.top + WD_FRAMERECT_TOP + FONT_HEIGHT_NORMAL + 2, STR_JUST_CURRENCY_LONG, TC_BLACK, SA_RIGHT);
644 DrawString(r.left + WD_FRAMERECT_LEFT + 8, r.right - WD_FRAMERECT_RIGHT - 16, r.top + WD_FRAMERECT_TOP + 2 * FONT_HEIGHT_NORMAL + 3, STR_GROUP_LIFETIME_PROFIT, TC_BLACK);
645 SetDParam(0, lifetime);
646 DrawString(r.left + WD_FRAMERECT_LEFT + 8, r.right - WD_FRAMERECT_RIGHT - 16, r.top + WD_FRAMERECT_TOP + 2 * FONT_HEIGHT_NORMAL + 3, STR_JUST_CURRENCY_LONG, TC_BLACK, SA_RIGHT);
648 break;
651 case WID_GL_LIST_GROUP: {
652 int y1 = r.top + WD_FRAMERECT_TOP;
653 int max = min(this->group_sb->GetPosition() + this->group_sb->GetCapacity(), this->groups.Length());
654 for (int i = this->group_sb->GetPosition(); i < max; ++i) {
655 const Group *g = this->groups[i];
657 assert(g->owner == this->owner);
659 DrawGroupInfo(y1, r.left, r.right, g->index, this->indents[i], g->replace_protection);
661 y1 += this->tiny_step_height;
663 if ((uint)this->group_sb->GetPosition() + this->group_sb->GetCapacity() > this->groups.Length()) {
664 DrawGroupInfo(y1, r.left, r.right, NEW_GROUP);
666 break;
669 case WID_GL_SORT_BY_ORDER:
670 this->DrawSortButtonState(WID_GL_SORT_BY_ORDER, this->vehicles.IsDescSortOrder() ? SBS_DOWN : SBS_UP);
671 break;
673 case WID_GL_LIST_VEHICLE:
674 if (this->vli.index != ALL_GROUP) {
675 /* Mark vehicles which are in sub-groups */
676 int y = r.top;
677 uint max = min(this->vscroll->GetPosition() + this->vscroll->GetCapacity(), this->vehicles.Length());
678 for (uint i = this->vscroll->GetPosition(); i < max; ++i) {
679 const Vehicle *v = this->vehicles[i];
680 if (v->group_id != this->vli.index) {
681 GfxFillRect(r.left + 1, y + 1, r.right - 1, y + this->resize.step_height - 2, _colour_gradient[COLOUR_GREY][3], FILLRECT_CHECKER);
683 y += this->resize.step_height;
687 this->DrawVehicleListItems(this->vehicle_sel, this->resize.step_height, r);
688 break;
692 static void DeleteGroupCallback(Window *win, bool confirmed)
694 if (confirmed) {
695 VehicleGroupWindow *w = (VehicleGroupWindow*)win;
696 w->vli.index = ALL_GROUP;
697 DoCommandP(0, w->group_confirm, 0, CMD_DELETE_GROUP | CMD_MSG(STR_ERROR_GROUP_CAN_T_DELETE));
701 virtual void OnClick(Point pt, int widget, int click_count)
703 switch (widget) {
704 case WID_GL_SORT_BY_ORDER: // Flip sorting method ascending/descending
705 this->vehicles.ToggleSortOrder();
706 this->vehicles.ForceResort();
707 this->SetDirty();
708 break;
710 case WID_GL_SORT_BY_DROPDOWN: // Select sorting criteria dropdown menu
711 ShowDropDownMenu(this, this->vehicle_sorter_names, this->vehicles.SortType(), WID_GL_SORT_BY_DROPDOWN, 0,
712 (this->vli.vtype == VEH_TRAIN || this->vli.vtype == VEH_ROAD) ? 0 : this->vehicle_sorter_non_ground_veh_disable_mask);
713 return;
715 case WID_GL_FILTER_BY_CARGO: // Select filtering criteria dropdown menu
716 ShowDropDownMenu(this, this->cargo_filter_texts, this->cargo_filter_criteria, WID_GL_FILTER_BY_CARGO, 0, 0);
717 break;
719 case WID_GL_ALL_VEHICLES: // All vehicles button
720 if (!IsAllGroupID(this->vli.index)) {
721 this->vli.index = ALL_GROUP;
722 this->vehicles.ForceRebuild();
723 this->SetDirty();
725 break;
727 case WID_GL_DEFAULT_VEHICLES: // Ungrouped vehicles button
728 if (!IsDefaultGroupID(this->vli.index)) {
729 this->vli.index = DEFAULT_GROUP;
730 this->vehicles.ForceRebuild();
731 this->SetDirty();
733 break;
735 case WID_GL_LIST_GROUP: { // Matrix Group
736 uint id_g = this->group_sb->GetScrolledRowFromWidget(pt.y, this, WID_GL_LIST_GROUP, 0, this->tiny_step_height);
737 if (id_g >= this->groups.Length()) return;
739 this->group_sel = this->vli.index = this->groups[id_g]->index;
741 if (click_count % 2 == 0) {
742 this->ToogleGroupCollapse(this->vli.index);
743 OnInvalidateData();
746 SetObjectToPlaceWnd(SPR_CURSOR_MOUSE, PAL_NONE, HT_DRAG, this);
748 this->vehicles.ForceRebuild();
749 this->SetDirty();
750 break;
753 case WID_GL_LIST_VEHICLE: { // Matrix Vehicle
754 uint id_v = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_GL_LIST_VEHICLE);
755 if (id_v >= this->vehicles.Length()) return; // click out of list bound
757 const Vehicle *v = this->vehicles[id_v];
758 if (VehicleClicked(v)) break;
760 this->vehicle_sel = v->index;
762 SetObjectToPlaceWnd(SPR_CURSOR_MOUSE, PAL_NONE, HT_DRAG, this);
763 SetMouseCursorVehicle(v, EIT_IN_LIST);
764 _cursor.vehchain = true;
766 this->SetDirty();
767 break;
770 case WID_GL_CREATE_GROUP: { // Create a new group
771 DoCommandP(0, this->vli.vtype, 0, CMD_CREATE_GROUP | CMD_MSG(STR_ERROR_GROUP_CAN_T_CREATE), CcCreateGroup);
772 break;
775 case WID_GL_DELETE_GROUP: { // Delete the selected group
776 this->group_confirm = this->vli.index;
777 ShowQuery(STR_QUERY_GROUP_DELETE_CAPTION, STR_GROUP_DELETE_QUERY_TEXT, this, DeleteGroupCallback);
778 InvalidateWindowData(WC_TEMPLATEGUI_MAIN, 0, 0, 0);
779 break;
782 case WID_GL_RENAME_GROUP: // Rename the selected roup
783 this->ShowRenameGroupWindow(this->vli.index, false);
784 break;
786 case WID_GL_COLLAPSE_EXPAND_GROUP: // Toggle collapse/expand
787 this->ToogleGroupCollapse(this->vli.index);
788 OnInvalidateData();
789 this->SetDirty();
790 break;
792 case WID_GL_COLLAPSE_ALL_GROUPS: {
793 this->collapsed_groups.Clear();
794 for (const GroupID *gid = this->all_groups.Begin(); gid != this->all_groups.End(); ++gid) {
795 GroupID id = *gid;
796 if (id != ALL_GROUP && id != DEFAULT_GROUP && id != INVALID_GROUP && this->collapsable_groups.Contains(id)) {
797 *this->collapsed_groups.Append() = id;
800 OnInvalidateData();
801 this->SetDirty();
802 break;
805 case WID_GL_EXPAND_ALL_GROUPS: {
806 this->collapsed_groups.Clear();
807 OnInvalidateData();
808 this->SetDirty();
809 break;
811 case WID_GL_AUTO_GROUP: {
812 DoCommandP(0, vli.company, vli.vtype, CMD_AUTO_GROUP_VEHICLES, nullptr);
814 this->vehicle_sel = INVALID_VEHICLE;
815 this->group_over = INVALID_GROUP;
816 this->SetDirty();
817 break;
820 case WID_GL_AVAILABLE_VEHICLES:
821 ShowBuildVehicleWindow(INVALID_TILE, this->vli.vtype);
822 break;
824 case WID_GL_MANAGE_VEHICLES_DROPDOWN: {
825 DropDownList *list = this->BuildActionDropdownList(true, Group::IsValidID(this->vli.index), this->vli.vtype == VEH_TRAIN, true);
826 ShowDropDownList(this, list, -1, WID_GL_MANAGE_VEHICLES_DROPDOWN);
827 break;
830 case WID_GL_START_ALL:
831 case WID_GL_STOP_ALL: { // Start/stop all vehicles of the list
832 DoCommandP(0, (1 << 1) | (widget == WID_GL_START_ALL ? (1 << 0) : 0), this->vli.Pack(), CMD_MASS_START_STOP);
833 break;
836 case WID_GL_REPLACE_PROTECTION: {
837 const Group *g = Group::GetIfValid(this->vli.index);
838 if (g != nullptr) {
839 DoCommandP(0, this->vli.index, (g->replace_protection ? 0 : 1) | (_ctrl_pressed << 1), CMD_SET_GROUP_REPLACE_PROTECTION);
841 break;
844 case WID_GL_REFRESH_SORTING: {
845 this->live_refresh_sorting = !this->live_refresh_sorting;
846 this->SetWidgetDirty(WID_GL_REFRESH_SORTING);
847 break;
852 void OnDragDrop_Group(Point pt, int widget)
854 const Group *g = Group::Get(this->group_sel);
856 switch (widget) {
857 case WID_GL_ALL_VEHICLES: // All vehicles
858 case WID_GL_DEFAULT_VEHICLES: // Ungroupd vehicles
859 if (g->parent != INVALID_GROUP) {
860 DoCommandP(0, this->group_sel | (1 << 16), INVALID_GROUP, CMD_ALTER_GROUP | CMD_MSG(STR_ERROR_GROUP_CAN_T_SET_PARENT));
863 this->group_sel = INVALID_GROUP;
864 this->group_over = INVALID_GROUP;
865 this->SetDirty();
866 break;
868 case WID_GL_LIST_GROUP: { // Matrix group
869 uint id_g = this->group_sb->GetScrolledRowFromWidget(pt.y, this, WID_GL_LIST_GROUP, 0, this->tiny_step_height);
870 GroupID new_g = id_g >= this->groups.Length() ? INVALID_GROUP : this->groups[id_g]->index;
872 if (this->group_sel != new_g && g->parent != new_g) {
873 DoCommandP(0, this->group_sel | (1 << 16), new_g, CMD_ALTER_GROUP | CMD_MSG(STR_ERROR_GROUP_CAN_T_SET_PARENT));
875 if (!_ctrl_pressed) {
876 GroupID *group = this->collapsed_groups.Find(new_g);
877 if (group != this->collapsed_groups.End()) this->collapsed_groups.Erase(group);
881 this->group_sel = INVALID_GROUP;
882 this->group_over = INVALID_GROUP;
883 this->SetDirty();
884 break;
889 void OnDragDrop_Vehicle(Point pt, int widget)
891 switch (widget) {
892 case WID_GL_DEFAULT_VEHICLES: // Ungrouped vehicles
893 DoCommandP(0, DEFAULT_GROUP, this->vehicle_sel | (_ctrl_pressed ? 1 << 31 : 0), CMD_ADD_VEHICLE_GROUP | CMD_MSG(STR_ERROR_GROUP_CAN_T_ADD_VEHICLE));
895 this->vehicle_sel = INVALID_VEHICLE;
896 this->group_over = INVALID_GROUP;
898 this->SetDirty();
899 break;
901 case WID_GL_LIST_GROUP: { // Matrix group
902 const VehicleID vindex = this->vehicle_sel;
903 this->vehicle_sel = INVALID_VEHICLE;
904 this->group_over = INVALID_GROUP;
905 this->SetDirty();
907 uint id_g = this->group_sb->GetScrolledRowFromWidget(pt.y, this, WID_GL_LIST_GROUP, 0, this->tiny_step_height);
908 GroupID new_g = id_g >= this->groups.Length() ? NEW_GROUP : this->groups[id_g]->index;
910 DoCommandP(0, new_g, vindex | (_ctrl_pressed ? 1 << 31 : 0), CMD_ADD_VEHICLE_GROUP | CMD_MSG(STR_ERROR_GROUP_CAN_T_ADD_VEHICLE), new_g == NEW_GROUP ? CcAddVehicleNewGroup : nullptr);
911 break;
914 case WID_GL_LIST_VEHICLE: { // Matrix vehicle
915 const VehicleID vindex = this->vehicle_sel;
916 this->vehicle_sel = INVALID_VEHICLE;
917 this->group_over = INVALID_GROUP;
918 this->SetDirty();
920 uint id_v = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_GL_LIST_VEHICLE);
921 if (id_v >= this->vehicles.Length()) return; // click out of list bound
923 const Vehicle *v = this->vehicles[id_v];
924 if (!VehicleClicked(v) && vindex == v->index) {
925 ShowVehicleViewWindow(v);
927 break;
929 case WID_GL_CREATE_GROUP: { // make new group with vehicle specific name and add vehicle
930 const VehicleID vindex = this->vehicle_sel;
931 this->vehicle_sel = INVALID_VEHICLE;
932 this->group_over = INVALID_GROUP;
933 this->SetDirty();
935 DoCommandP(0, vindex | (_ctrl_pressed ? 1 << 31 : 0),0 , CMD_CREATE_GROUP_SPECIFIC_NAME | CMD_MSG(STR_ERROR_GROUP_CAN_T_CREATE_SPECIFIC_NAME), nullptr);
937 break;
942 virtual void OnDragDrop(Point pt, int widget)
944 if (this->vehicle_sel != INVALID_VEHICLE) OnDragDrop_Vehicle(pt, widget);
945 if (this->group_sel != INVALID_GROUP) OnDragDrop_Group(pt, widget);
947 _cursor.vehchain = false;
950 virtual void OnQueryTextFinished(char *str)
952 if (str != nullptr) DoCommandP(0, this->group_rename, 0, CMD_ALTER_GROUP | CMD_MSG(STR_ERROR_GROUP_CAN_T_RENAME), nullptr, str);
953 InvalidateWindowData(WC_TEMPLATEGUI_MAIN, 0, 0, 0);
954 this->group_rename = INVALID_GROUP;
957 virtual void OnResize()
959 this->group_sb->SetCapacityFromWidget(this, WID_GL_LIST_GROUP);
960 this->vscroll->SetCapacityFromWidget(this, WID_GL_LIST_VEHICLE);
963 virtual void OnDropdownSelect(int widget, int index)
965 switch (widget) {
966 case WID_GL_SORT_BY_DROPDOWN:
967 this->vehicles.SetSortType(index);
968 break;
970 case WID_GL_FILTER_BY_CARGO: // Select a cargo filter criteria
971 this->SetCargoFilterIndex(index);
972 break;
974 case WID_GL_MANAGE_VEHICLES_DROPDOWN:
975 assert(this->ShouldShowActionDropdownList());
977 switch (index) {
978 case ADI_SET_ALL_ON_TIME: { // Reset all late timers
979 int num_vehicles = this->vehicles.Length();
981 for (int v_index = 0; v_index < num_vehicles; ++v_index) {
982 DoCommandP(0, this->vehicles[v_index]->index, 0, CMD_SET_VEHICLE_ON_TIME | CMD_MSG(STR_ERROR_CAN_T_TIMETABLE_VEHICLE));
985 break;
987 case ADI_TEMPLATE_REPLACE: // TemplateReplace Window
988 if ( vli.vtype == VEH_TRAIN )
989 ShowTemplateReplaceWindow(this->unitnumber_digits, this->resize.step_height);
990 break;
991 case ADI_REPLACE: // Replace window
992 ShowReplaceGroupVehicleWindow(this->vli.index, this->vli.vtype);
993 break;
994 case ADI_SERVICE: // Send for servicing
995 case ADI_DEPOT: { // Send to Depots
996 DoCommandP(0, DEPOT_MASS_SEND | (index == ADI_SERVICE ? DEPOT_SERVICE : 0U), this->vli.Pack(), GetCmdSendToDepot(this->vli.vtype));
997 break;
1000 case ADI_ADD_SHARED: // Add shared Vehicles
1001 assert(Group::IsValidID(this->vli.index));
1003 DoCommandP(0, this->vli.index, this->vli.vtype, CMD_ADD_SHARED_VEHICLE_GROUP | CMD_MSG(STR_ERROR_GROUP_CAN_T_ADD_SHARED_VEHICLE));
1004 break;
1005 case ADI_REMOVE_ALL: // Remove all Vehicles from the selected group
1006 assert(Group::IsValidID(this->vli.index));
1008 DoCommandP(0, this->vli.index, 0, CMD_REMOVE_ALL_VEHICLES_GROUP | CMD_MSG(STR_ERROR_GROUP_CAN_T_REMOVE_ALL_VEHICLES));
1009 break;
1010 case ADI_SELL_ALL: { // Sell all stopped Vehicles from the selected group
1011 /* Only open the confirmation window if there are anything to sell */
1012 bool is_all_group = (this->vli.index == ALL_GROUP);
1013 bool is_default_group = (this->vli.index == DEFAULT_GROUP);
1014 if (is_all_group || is_default_group || Group::Get(this->vli.index)->statistics.num_vehicle != 0) {
1015 SetDParam(0, this->vli.index);
1016 ShowQuery(
1017 STR_GROUP_NAME,
1018 STR_GROUP_SELL_CONFIRMATION_TEXT,
1019 this,
1020 SellAllConfirmationCallback
1023 break;
1026 case ADI_TRACERESTRICT_SLOT_MGMT: {
1027 extern void ShowTraceRestrictSlotWindow(CompanyID company);
1028 ShowTraceRestrictSlotWindow(this->owner);
1029 break;
1031 default: NOT_REACHED();
1033 break;
1035 default: NOT_REACHED();
1038 this->SetDirty();
1041 virtual void OnTick()
1043 if (_pause_mode != PM_UNPAUSED) return;
1045 if (live_refresh_sorting) {
1046 this->vehicles.ForceResort();
1049 if (this->groups.NeedResort() || this->vehicles.NeedResort()) {
1050 this->SetDirty();
1054 virtual void OnPlaceObjectAbort()
1056 /* abort drag & drop */
1057 this->vehicle_sel = INVALID_VEHICLE;
1058 this->DirtyHighlightedGroupWidget();
1059 this->group_over = INVALID_GROUP;
1060 this->SetWidgetDirty(WID_GL_LIST_VEHICLE);
1063 virtual void OnMouseDrag(Point pt, int widget)
1065 if (this->vehicle_sel == INVALID_VEHICLE && this->group_sel == INVALID_GROUP) return;
1067 /* A vehicle is dragged over... */
1068 GroupID new_group_over = INVALID_GROUP;
1069 switch (widget) {
1070 case WID_GL_DEFAULT_VEHICLES: // ... the 'default' group.
1071 new_group_over = DEFAULT_GROUP;
1072 break;
1074 case WID_GL_LIST_GROUP: { // ... the list of custom groups.
1075 uint id_g = this->group_sb->GetScrolledRowFromWidget(pt.y, this, WID_GL_LIST_GROUP, 0, this->tiny_step_height);
1076 new_group_over = id_g >= this->groups.Length() ? NEW_GROUP : this->groups[id_g]->index;
1077 break;
1081 /* Do not highlight when dragging over the current group */
1082 if (this->vehicle_sel != INVALID_VEHICLE) {
1083 if (Vehicle::Get(vehicle_sel)->group_id == new_group_over) new_group_over = INVALID_GROUP;
1084 } else if (this->group_sel != INVALID_GROUP) {
1085 if (this->group_sel == new_group_over || Group::Get(this->group_sel)->parent == new_group_over) new_group_over = INVALID_GROUP;
1088 /* Mark widgets as dirty if the group changed. */
1089 if (new_group_over != this->group_over) {
1090 this->DirtyHighlightedGroupWidget();
1091 this->group_over = new_group_over;
1092 this->DirtyHighlightedGroupWidget();
1096 void ShowRenameGroupWindow(GroupID group, bool empty)
1098 assert(Group::IsValidID(group));
1099 this->group_rename = group;
1100 /* Show empty query for new groups */
1101 StringID str = STR_EMPTY;
1102 if (!empty) {
1103 SetDParam(0, group);
1104 str = STR_GROUP_NAME;
1106 ShowQueryString(str, STR_GROUP_RENAME_CAPTION, MAX_LENGTH_GROUP_NAME_CHARS, this, CS_ALPHANUMERAL, QSF_ENABLE_DEFAULT | QSF_LEN_IN_CHARS);
1110 * Tests whether a given vehicle is selected in the window, and unselects it if necessary.
1111 * Called when the vehicle is deleted.
1112 * @param vehicle Vehicle that is going to be deleted
1114 void UnselectVehicle(VehicleID vehicle)
1116 if (this->vehicle_sel == vehicle) ResetObjectToPlace();
1120 static void SellAllConfirmationCallback(Window *win, bool confirmed)
1122 if (confirmed) {
1123 VehicleGroupWindow *w = (VehicleGroupWindow*)win;
1124 DoCommandP(0, w->vli.vtype, w->vli.index, CMD_GROUP_SELL_ALL_VEHICLES);
1128 static WindowDesc _other_group_desc(
1129 WDP_AUTO, "list_groups", 460, 246,
1130 WC_INVALID, WC_NONE,
1132 _nested_group_widgets, lengthof(_nested_group_widgets)
1135 static WindowDesc _train_group_desc(
1136 WDP_AUTO, "list_groups_train", 525, 246,
1137 WC_TRAINS_LIST, WC_NONE,
1139 _nested_group_widgets, lengthof(_nested_group_widgets)
1143 * Show the group window for the given company and vehicle type.
1144 * @param company The company to show the window for.
1145 * @param vehicle_type The type of vehicle to show it for.
1147 void ShowCompanyGroup(CompanyID company, VehicleType vehicle_type)
1149 if (!Company::IsValidID(company)) return;
1151 WindowNumber num = VehicleListIdentifier(VL_GROUP_LIST, vehicle_type, company).Pack();
1152 if (vehicle_type == VEH_TRAIN) {
1153 AllocateWindowDescFront<VehicleGroupWindow>(&_train_group_desc, num);
1154 } else {
1155 _other_group_desc.cls = GetWindowClassForVehicleType(vehicle_type);
1156 AllocateWindowDescFront<VehicleGroupWindow>(&_other_group_desc, num);
1161 * Finds a group list window determined by vehicle type and owner
1162 * @param vt vehicle type
1163 * @param owner owner of groups
1164 * @return pointer to VehicleGroupWindow, nullptr if not found
1166 static inline VehicleGroupWindow *FindVehicleGroupWindow(VehicleType vt, Owner owner)
1168 return (VehicleGroupWindow *)FindWindowById(GetWindowClassForVehicleType(vt), VehicleListIdentifier(VL_GROUP_LIST, vt, owner).Pack());
1172 * Opens a 'Rename group' window for newly created group
1173 * @param success did command succeed?
1174 * @param tile unused
1175 * @param p1 vehicle type
1176 * @param p2 unused
1177 * @see CmdCreateGroup
1179 void CcCreateGroup(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
1181 if (result.Failed()) return;
1182 assert(p1 <= VEH_AIRCRAFT);
1184 VehicleGroupWindow *w = FindVehicleGroupWindow((VehicleType)p1, _current_company);
1185 if (w != nullptr) w->ShowRenameGroupWindow(_new_group_id, true);
1189 * Open rename window after adding a vehicle to a new group via drag and drop.
1190 * @param success Did command succeed?
1191 * @param tile Unused.
1192 * @param p1 Unused.
1193 * @param p2 Bit 0-19: Vehicle ID.
1195 void CcAddVehicleNewGroup(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
1197 if (result.Failed()) return;
1198 assert(Vehicle::IsValidID(GB(p2, 0, 20)));
1200 CcCreateGroup(result, 0, Vehicle::Get(GB(p2, 0, 20))->type, 0);
1204 * Removes the highlight of a vehicle in a group window
1205 * @param *v Vehicle to remove all highlights from
1207 void DeleteGroupHighlightOfVehicle(const Vehicle *v)
1209 /* If we haven't got any vehicles on the mouse pointer, we haven't got any highlighted in any group windows either
1210 * If that is the case, we can skip looping though the windows and save time
1212 if (_special_mouse_mode != WSM_DRAGDROP) return;
1214 VehicleGroupWindow *w = FindVehicleGroupWindow(v->type, v->owner);
1215 if (w != nullptr) w->UnselectVehicle(v->index);