Maintain a circular buffer of recent commands, add to crashlog.
[openttd-joker.git] / src / group_gui.cpp
blobac5f016fd84efd6c90c57bb54b84cf6aa421eb3d
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"
31 #include "safeguards.h"
33 static const int LEVEL_WIDTH = 10; ///< Indenting width of a sub-group in pixels
35 typedef GUIList<const Group*> GUIGroupList;
37 static void SellAllConfirmationCallback(Window *win, bool confirmed);
39 static const NWidgetPart _nested_group_widgets[] = {
40 NWidget(NWID_HORIZONTAL), // Window header
41 NWidget(WWT_CLOSEBOX, COLOUR_GREY),
42 NWidget(WWT_CAPTION, COLOUR_GREY, WID_GL_CAPTION),
43 NWidget(WWT_SHADEBOX, COLOUR_GREY),
44 NWidget(WWT_DEFSIZEBOX, COLOUR_GREY),
45 NWidget(WWT_STICKYBOX, COLOUR_GREY),
46 EndContainer(),
47 NWidget(NWID_HORIZONTAL),
48 /* left part */
49 NWidget(NWID_VERTICAL),
50 NWidget(WWT_PANEL, COLOUR_GREY), SetMinimalTextLines(1, WD_DROPDOWNTEXT_TOP + WD_DROPDOWNTEXT_BOTTOM), SetFill(1, 0), EndContainer(),
51 NWidget(WWT_PANEL, COLOUR_GREY, WID_GL_ALL_VEHICLES), SetFill(1, 0), EndContainer(),
52 NWidget(WWT_PANEL, COLOUR_GREY, WID_GL_DEFAULT_VEHICLES), SetFill(1, 0), EndContainer(),
53 NWidget(NWID_HORIZONTAL),
54 NWidget(WWT_MATRIX, COLOUR_GREY, WID_GL_LIST_GROUP), SetMatrixDataTip(1, 0, STR_GROUPS_CLICK_ON_GROUP_FOR_TOOLTIP),
55 SetFill(1, 0), SetResize(0, 1), SetScrollbar(WID_GL_LIST_GROUP_SCROLLBAR),
56 NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_GL_LIST_GROUP_SCROLLBAR),
57 EndContainer(),
58 NWidget(WWT_PANEL, COLOUR_GREY, WID_GL_INFO), SetMinimalSize(200, 80), SetFill(1, 0), EndContainer(),
59 NWidget(NWID_HORIZONTAL),
60 NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_GL_CREATE_GROUP), SetFill(0, 1),
61 SetDataTip(SPR_GROUP_CREATE_TRAIN, STR_GROUP_CREATE_TOOLTIP),
62 NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_GL_DELETE_GROUP), SetFill(0, 1),
63 SetDataTip(SPR_GROUP_DELETE_TRAIN, STR_GROUP_DELETE_TOOLTIP),
64 NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_GL_RENAME_GROUP), SetFill(0, 1),
65 SetDataTip(SPR_GROUP_RENAME_TRAIN, STR_GROUP_RENAME_TOOLTIP),
66 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_GL_COLLAPSE_EXPAND_GROUP), SetFill(0, 1),
67 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_GL_COLLAPSE_ALL_GROUPS), SetFill(0, 1),
68 SetDataTip(STR_GROUP_COLLAPSE_ALL, STR_GROUP_COLLAPSE_ALL),
69 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_GL_EXPAND_ALL_GROUPS), SetFill(0, 1),
70 SetDataTip(STR_GROUP_EXPAND_ALL, STR_GROUP_EXPAND_ALL),
71 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_GL_AUTO_GROUP), SetFill(0, 1),
72 SetDataTip(STR_AUTO_GROUP, STR_AUTO_GROUP_TIP),
73 NWidget(WWT_PANEL, COLOUR_GREY), SetFill(1, 1), EndContainer(),
74 NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_GL_REPLACE_PROTECTION), SetFill(0, 1),
75 SetDataTip(SPR_GROUP_REPLACE_OFF_TRAIN, STR_GROUP_REPLACE_PROTECTION_TOOLTIP),
76 EndContainer(),
77 EndContainer(),
78 /* right part */
79 NWidget(NWID_VERTICAL),
80 NWidget(NWID_HORIZONTAL),
81 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_GL_SORT_BY_ORDER), SetMinimalSize(81, 12), SetDataTip(STR_BUTTON_SORT_BY, STR_TOOLTIP_SORT_ORDER),
82 NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_GL_SORT_BY_DROPDOWN), SetMinimalSize(167, 12), SetDataTip(0x0, STR_TOOLTIP_SORT_CRITERIA),
83 NWidget(WWT_PANEL, COLOUR_GREY), SetMinimalSize(12, 12), SetResize(1, 0), EndContainer(),
84 EndContainer(),
85 NWidget(NWID_HORIZONTAL),
86 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),
87 NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_GL_LIST_VEHICLE_SCROLLBAR),
88 EndContainer(),
89 NWidget(WWT_PANEL, COLOUR_GREY), SetMinimalSize(1, 0), SetFill(1, 1), SetResize(1, 0), EndContainer(),
90 NWidget(NWID_HORIZONTAL),
91 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_GL_AVAILABLE_VEHICLES), SetMinimalSize(106, 12), SetFill(0, 1),
92 SetDataTip(STR_BLACK_STRING, STR_VEHICLE_LIST_AVAILABLE_ENGINES_TOOLTIP),
93 NWidget(WWT_PANEL, COLOUR_GREY), SetMinimalSize(0, 12), SetFill(1, 1), SetResize(1, 0), EndContainer(),
94 NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_GL_MANAGE_VEHICLES_DROPDOWN), SetMinimalSize(118, 12), SetFill(0, 1),
95 SetDataTip(STR_VEHICLE_LIST_MANAGE_LIST, STR_VEHICLE_LIST_MANAGE_LIST_TOOLTIP),
96 NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_GL_STOP_ALL), SetMinimalSize(12, 12), SetFill(0, 1),
97 SetDataTip(SPR_FLAG_VEH_STOPPED, STR_VEHICLE_LIST_MASS_STOP_LIST_TOOLTIP),
98 NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_GL_START_ALL), SetMinimalSize(12, 12), SetFill(0, 1),
99 SetDataTip(SPR_FLAG_VEH_RUNNING, STR_VEHICLE_LIST_MASS_START_LIST_TOOLTIP),
100 NWidget(WWT_RESIZEBOX, COLOUR_GREY),
101 EndContainer(),
102 EndContainer(),
103 EndContainer(),
106 /** Sort the groups by their name */
107 int CDECL GroupNameSorter(const Group * const *a, const Group * const *b)
109 static const Group *last_group[2] = { NULL, NULL };
110 static char last_name[2][64] = { "", "" };
112 if (*a != last_group[0]) {
113 last_group[0] = *a;
114 SetDParam(0, (*a)->index);
115 GetString(last_name[0], STR_GROUP_NAME, lastof(last_name[0]));
118 if (*b != last_group[1]) {
119 last_group[1] = *b;
120 SetDParam(0, (*b)->index);
121 GetString(last_name[1], STR_GROUP_NAME, lastof(last_name[1]));
124 int r = strnatcmp(last_name[0], last_name[1]); // Sort by name (natural sorting).
125 if (r == 0) return (*a)->index - (*b)->index;
126 return r;
130 class VehicleGroupWindow : public BaseVehicleListWindow {
131 private:
132 /* Columns in the group list */
133 enum ListColumns {
134 VGC_NAME, ///< Group name.
135 VGC_COLLAPSED, ///< Collapsed icon
136 VGC_PROTECT, ///< Autoreplace protect icon.
137 VGC_AUTOREPLACE, ///< Autoreplace active icon.
138 VGC_PROFIT, ///< Profit icon.
139 VGC_NUMBER, ///< Number of vehicles in the group.
141 VGC_END
144 VehicleID vehicle_sel; ///< Selected vehicle
145 GroupID group_sel; ///< Selected group (for drag/drop)
146 GroupID group_rename; ///< Group being renamed, INVALID_GROUP if none
147 GroupID group_over; ///< Group over which a vehicle is dragged, INVALID_GROUP if none
148 GroupID group_confirm; ///< Group awaiting delete confirmation
149 GUIGroupList groups; ///< List of groups
150 uint tiny_step_height; ///< Step height for the group list
151 Scrollbar *group_sb;
153 SmallVector<GroupID, 16> all_groups; ///< List of all groups, including hidden ones
154 SmallVector<GroupID, 16> collapsed_groups; ///< List of collapsed groups
155 SmallVector<GroupID, 16> collapsable_groups; ///< List of collapsable groups
156 SmallVector<int, 16> indents; ///< Indentation levels
158 Dimension column_size[VGC_END]; ///< Size of the columns in the group list.
160 /** return true if group has children */
161 bool AddParents(GUIGroupList *source, GroupID parent, int indent, bool parent_collapsed)
163 bool is_collapsed = this->collapsed_groups.Contains(parent);
164 bool overall_collapsed = is_collapsed || parent_collapsed;
165 bool has_children = false;
167 for (const Group **g = source->Begin(); g != source->End(); g++) {
168 if ((*g)->parent == parent) {
169 has_children = true;
170 *this->all_groups.Append() = (*g)->index;
171 if (!overall_collapsed) {
172 *this->groups.Append() = *g;
173 *this->indents.Append() = indent;
175 bool child_has_children = AddParents(source, (*g)->index, indent + 1, overall_collapsed);
176 if (child_has_children) *this->collapsable_groups.Append() = (*g)->index;
180 return has_children;
183 void ToogleGroupCollapse(GroupID group)
185 GroupID *item = this->collapsed_groups.Find(group);
186 if (item == this->collapsed_groups.End()) {
187 if (this->collapsable_groups.Find(group) != this->collapsable_groups.End()) {
188 *(this->collapsed_groups.Append()) = group;
190 } else {
191 this->collapsed_groups.Erase(item);
196 * (Re)Build the group list.
198 * @param owner The owner of the window
200 void BuildGroupList(Owner owner)
202 if (!this->groups.NeedRebuild()) return;
204 this->all_groups.Clear();
205 this->groups.Clear();
206 this->indents.Clear();
207 this->collapsable_groups.Clear();
209 GUIGroupList list;
211 const Group *g;
212 FOR_ALL_GROUPS(g) {
213 if (g->owner == owner && g->vehicle_type == this->vli.vtype) {
214 *list.Append() = g;
218 list.ForceResort();
219 list.Sort(&GroupNameSorter);
221 AddParents(&list, INVALID_GROUP, 0, false);
223 this->groups.Compact();
224 this->groups.RebuildDone();
228 * Compute tiny_step_height and column_size
229 * @return Total width required for the group list.
231 uint ComputeGroupInfoSize()
233 this->column_size[VGC_COLLAPSED] = GetSpriteSize(SPR_WINDOW_UNSHADE);
234 this->tiny_step_height = this->column_size[VGC_COLLAPSED].height;
236 this->column_size[VGC_NAME] = maxdim(GetStringBoundingBox(STR_GROUP_DEFAULT_TRAINS + this->vli.vtype), GetStringBoundingBox(STR_GROUP_ALL_TRAINS + this->vli.vtype));
237 this->column_size[VGC_NAME].width = max((170u * FONT_HEIGHT_NORMAL) / 10u, this->column_size[VGC_NAME].width);
238 this->tiny_step_height = max(this->tiny_step_height, this->column_size[VGC_NAME].height);
240 this->column_size[VGC_PROTECT] = GetSpriteSize(SPR_GROUP_REPLACE_PROTECT);
241 this->tiny_step_height = max(this->tiny_step_height, this->column_size[VGC_PROTECT].height);
243 this->column_size[VGC_AUTOREPLACE] = GetSpriteSize(SPR_GROUP_REPLACE_ACTIVE);
244 this->tiny_step_height = max(this->tiny_step_height, this->column_size[VGC_AUTOREPLACE].height);
246 this->column_size[VGC_PROFIT].width = 0;
247 this->column_size[VGC_PROFIT].height = 0;
248 static const SpriteID profit_sprites[] = {SPR_PROFIT_NA, SPR_PROFIT_NEGATIVE, SPR_PROFIT_SOME, SPR_PROFIT_LOT};
249 for (uint i = 0; i < lengthof(profit_sprites); i++) {
250 Dimension d = GetSpriteSize(profit_sprites[i]);
251 this->column_size[VGC_PROFIT] = maxdim(this->column_size[VGC_PROFIT], d);
253 this->tiny_step_height = max(this->tiny_step_height, this->column_size[VGC_PROFIT].height);
255 SetDParamMaxValue(0, GroupStatistics::Get(this->vli.company, ALL_GROUP, this->vli.vtype).num_vehicle, 3, FS_SMALL);
256 this->column_size[VGC_NUMBER] = GetStringBoundingBox(STR_TINY_COMMA);
257 this->tiny_step_height = max(this->tiny_step_height, this->column_size[VGC_NUMBER].height);
259 this->tiny_step_height += WD_MATRIX_TOP;
261 return WD_FRAMERECT_LEFT + 8 +
262 this->column_size[VGC_COLLAPSED].width + 2 +
263 this->column_size[VGC_NAME].width + 8 +
264 this->column_size[VGC_PROTECT].width + 2 +
265 this->column_size[VGC_AUTOREPLACE].width + 2 +
266 this->column_size[VGC_PROFIT].width + 2 +
267 this->column_size[VGC_NUMBER].width + 2 +
268 WD_FRAMERECT_RIGHT;
272 * Draw a row in the group list.
273 * @param y Top of the row.
274 * @param left Left of the row.
275 * @param right Right of the row.
276 * @param g_id Group to list.
277 * @param indent Indentation level.
278 * @param protection Whether autoreplac/// e protection is set.
280 void DrawGroupInfo(int y, int left, int right, GroupID g_id, int indent = 0, bool protection = false) const
282 /* Highlight the group if a vehicle is dragged over it */
283 if (g_id == this->group_over) {
284 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]);
287 if (g_id == NEW_GROUP) return;
289 /* draw the selected group in white, else we draw it in black */
290 TextColour colour = g_id == this->vli.index ? TC_WHITE : TC_BLACK;
291 const GroupStatistics &stats = GroupStatistics::Get(this->vli.company, g_id, this->vli.vtype);
292 bool rtl = _current_text_dir == TD_RTL;
294 /* draw group name */
295 StringID str;
296 if (IsAllGroupID(g_id)) {
297 str = STR_GROUP_ALL_TRAINS + this->vli.vtype;
298 } else if (IsDefaultGroupID(g_id)) {
299 str = STR_GROUP_DEFAULT_TRAINS + this->vli.vtype;
300 } else {
301 SetDParam(0, g_id);
302 str = STR_GROUP_NAME;
305 /* draw collapse state */
306 int x = rtl ? right - WD_FRAMERECT_RIGHT - 8 - this->column_size[VGC_COLLAPSED].width + 1 : left + WD_FRAMERECT_LEFT + 8;
307 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);
309 x = rtl ? right - WD_FRAMERECT_RIGHT - 8 - this->column_size[VGC_NAME].width + 1 : x + 8 + this->column_size[VGC_COLLAPSED].width;
310 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);
312 /* draw autoreplace protection */
313 x = rtl ? x - 2 - this->column_size[VGC_PROTECT].width : x + 2 + this->column_size[VGC_NAME].width;
314 if (protection) DrawSprite(SPR_GROUP_REPLACE_PROTECT, PAL_NONE, x, y + (this->tiny_step_height - this->column_size[VGC_PROTECT].height) / 2);
316 /* draw autoreplace status */
317 x = rtl ? x - 2 - this->column_size[VGC_AUTOREPLACE].width : x + 2 + this->column_size[VGC_PROTECT].width;
318 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);
320 /* draw the profit icon */
321 x = rtl ? x - 2 - this->column_size[VGC_PROFIT].width : x + 2 + this->column_size[VGC_AUTOREPLACE].width;
322 SpriteID spr;
323 if (stats.num_profit_vehicle == 0) {
324 spr = SPR_PROFIT_NA;
325 } else if (stats.profit_last_year < 0) {
326 spr = SPR_PROFIT_NEGATIVE;
327 } else if (stats.profit_last_year < 10000 * stats.num_profit_vehicle) { // TODO magic number
328 spr = SPR_PROFIT_SOME;
329 } else {
330 spr = SPR_PROFIT_LOT;
332 DrawSprite(spr, PAL_NONE, x, y + (this->tiny_step_height - this->column_size[VGC_PROFIT].height) / 2);
334 /* draw the number of vehicles of the group */
335 x = rtl ? x - 2 - this->column_size[VGC_NUMBER].width : x + 2 + this->column_size[VGC_PROFIT].width;
336 SetDParam(0, stats.num_vehicle);
337 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);
341 * Mark the widget containing the currently highlighted group as dirty.
343 void DirtyHighlightedGroupWidget()
345 if (this->group_over == INVALID_GROUP) return;
347 if (IsAllGroupID(this->group_over)) {
348 this->SetWidgetDirty(WID_GL_ALL_VEHICLES);
349 } else if (IsDefaultGroupID(this->group_over)) {
350 this->SetWidgetDirty(WID_GL_DEFAULT_VEHICLES);
351 } else {
352 this->SetWidgetDirty(WID_GL_LIST_GROUP);
356 public:
357 VehicleGroupWindow(WindowDesc *desc, WindowNumber window_number) : BaseVehicleListWindow(desc, window_number)
359 this->CreateNestedTree();
361 this->vscroll = this->GetScrollbar(WID_GL_LIST_VEHICLE_SCROLLBAR);
362 this->group_sb = this->GetScrollbar(WID_GL_LIST_GROUP_SCROLLBAR);
364 switch (this->vli.vtype) {
365 default: NOT_REACHED();
366 case VEH_TRAIN: this->sorting = &_sorting.train; break;
367 case VEH_ROAD: this->sorting = &_sorting.roadveh; break;
368 case VEH_SHIP: this->sorting = &_sorting.ship; break;
369 case VEH_AIRCRAFT: this->sorting = &_sorting.aircraft; break;
372 this->vli.index = ALL_GROUP;
373 this->vehicle_sel = INVALID_VEHICLE;
374 this->group_sel = INVALID_GROUP;
375 this->group_rename = INVALID_GROUP;
376 this->group_over = INVALID_GROUP;
378 this->vehicles.SetListing(*this->sorting);
379 this->vehicles.ForceRebuild();
380 this->vehicles.NeedResort();
382 this->BuildVehicleList();
383 this->SortVehicleList();
385 this->groups.ForceRebuild();
386 this->groups.NeedResort();
387 this->BuildGroupList(vli.company);
389 this->GetWidget<NWidgetCore>(WID_GL_CAPTION)->widget_data = STR_VEHICLE_LIST_TRAIN_CAPTION + this->vli.vtype;
390 this->GetWidget<NWidgetCore>(WID_GL_LIST_VEHICLE)->tool_tip = STR_VEHICLE_LIST_TRAIN_LIST_TOOLTIP + this->vli.vtype;
392 this->GetWidget<NWidgetCore>(WID_GL_COLLAPSE_EXPAND_GROUP)->widget_data = STR_GROUP_COLLAPSE;
393 this->GetWidget<NWidgetCore>(WID_GL_COLLAPSE_EXPAND_GROUP)->tool_tip = STR_GROUP_COLLAPSE_TOOLTIP;
395 this->GetWidget<NWidgetCore>(WID_GL_CREATE_GROUP)->widget_data += this->vli.vtype;
396 this->GetWidget<NWidgetCore>(WID_GL_RENAME_GROUP)->widget_data += this->vli.vtype;
397 this->GetWidget<NWidgetCore>(WID_GL_DELETE_GROUP)->widget_data += this->vli.vtype;
398 this->GetWidget<NWidgetCore>(WID_GL_REPLACE_PROTECTION)->widget_data += this->vli.vtype;
400 this->FinishInitNested(window_number);
401 this->owner = vli.company;
404 ~VehicleGroupWindow()
406 *this->sorting = this->vehicles.GetListing();
409 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
411 switch (widget) {
412 case WID_GL_LIST_GROUP: {
413 size->width = this->ComputeGroupInfoSize();
414 resize->height = this->tiny_step_height;
416 /* Minimum height is the height of the list widget minus all and default vehicles... */
417 size->height = 4 * GetVehicleListHeight(this->vli.vtype, this->tiny_step_height) - 2 * this->tiny_step_height;
419 /* ... minus the buttons at the bottom ... */
420 uint max_icon_height = GetSpriteSize(this->GetWidget<NWidgetCore>(WID_GL_CREATE_GROUP)->widget_data).height;
421 max_icon_height = max(max_icon_height, GetSpriteSize(this->GetWidget<NWidgetCore>(WID_GL_RENAME_GROUP)->widget_data).height);
422 max_icon_height = max(max_icon_height, GetSpriteSize(this->GetWidget<NWidgetCore>(WID_GL_DELETE_GROUP)->widget_data).height);
423 max_icon_height = max(max_icon_height, GetSpriteSize(this->GetWidget<NWidgetCore>(WID_GL_REPLACE_PROTECTION)->widget_data).height);
425 /* ... minus the height of the group info ... */
426 max_icon_height += (FONT_HEIGHT_NORMAL * 3) + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
428 /* Get a multiple of tiny_step_height of that amount */
429 size->height = Ceil(size->height - max_icon_height, tiny_step_height);
430 break;
433 case WID_GL_ALL_VEHICLES:
434 case WID_GL_DEFAULT_VEHICLES:
435 size->width = this->ComputeGroupInfoSize();
436 size->height = this->tiny_step_height;
437 break;
439 case WID_GL_SORT_BY_ORDER: {
440 Dimension d = GetStringBoundingBox(this->GetWidget<NWidgetCore>(widget)->widget_data);
441 d.width += padding.width + Window::SortButtonWidth() * 2; // Doubled since the string is centred and it also looks better.
442 d.height += padding.height;
443 *size = maxdim(*size, d);
444 break;
447 case WID_GL_LIST_VEHICLE:
448 this->ComputeGroupInfoSize();
449 resize->height = GetVehicleListHeight(this->vli.vtype, this->tiny_step_height);
450 size->height = 4 * resize->height;
451 break;
453 case WID_GL_MANAGE_VEHICLES_DROPDOWN: {
454 Dimension d = this->GetActionDropdownSize(true, true, this->vli.vtype == VEH_TRAIN);
455 d.height += padding.height;
456 d.width += padding.width;
457 *size = maxdim(*size, d);
458 break;
461 case WID_GL_INFO: {
462 size->height = (FONT_HEIGHT_NORMAL * 3) + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM + 5;
463 break;
469 * Some data on this window has become invalid.
470 * @param data Information about the changed data.
471 * @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.
473 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
475 if (data == 0) {
476 /* This needs to be done in command-scope to enforce rebuilding before resorting invalid data */
477 this->vehicles.ForceRebuild();
478 this->groups.ForceRebuild();
479 } else {
480 this->vehicles.ForceResort();
481 this->groups.ForceResort();
484 /* Process ID-invalidation in command-scope as well */
485 if (this->group_rename != INVALID_GROUP && !Group::IsValidID(this->group_rename)) {
486 DeleteWindowByClass(WC_QUERY_STRING);
487 this->group_rename = INVALID_GROUP;
490 if (!(IsAllGroupID(this->vli.index) || IsDefaultGroupID(this->vli.index) || Group::IsValidID(this->vli.index))) {
491 this->vli.index = ALL_GROUP;
492 HideDropDownMenu(this);
494 this->SetDirty();
497 virtual void SetStringParameters(int widget) const
499 switch (widget) {
500 case WID_GL_AVAILABLE_VEHICLES:
501 SetDParam(0, STR_VEHICLE_LIST_AVAILABLE_TRAINS + this->vli.vtype);
502 break;
504 case WID_GL_CAPTION:
505 /* If selected_group == DEFAULT_GROUP || ALL_GROUP, draw the standard caption
506 * We list all vehicles or ungrouped vehicles */
507 if (IsDefaultGroupID(this->vli.index) || IsAllGroupID(this->vli.index)) {
508 SetDParam(0, STR_COMPANY_NAME);
509 SetDParam(1, this->vli.company);
510 SetDParam(2, this->vehicles.Length());
511 SetDParam(3, this->vehicles.Length());
512 } else {
513 const Group *g = Group::Get(this->vli.index);
515 SetDParam(0, STR_GROUP_NAME);
516 SetDParam(1, g->index);
517 SetDParam(2, g->statistics.num_vehicle);
518 SetDParam(3, g->statistics.num_vehicle);
520 break;
524 virtual void OnPaint()
526 /* If we select the all vehicles, this->list will contain all vehicles of the owner
527 * else this->list will contain all vehicles which belong to the selected group */
528 this->BuildVehicleList();
529 this->SortVehicleList();
531 this->BuildGroupList(this->owner);
533 this->group_sb->SetCount(this->groups.Length());
534 this->vscroll->SetCount(this->vehicles.Length());
536 /* The drop down menu is out, *but* it may not be used, retract it. */
537 if (this->vehicles.Length() == 0 && this->IsWidgetLowered(WID_GL_MANAGE_VEHICLES_DROPDOWN)) {
538 this->RaiseWidget(WID_GL_MANAGE_VEHICLES_DROPDOWN);
539 HideDropDownMenu(this);
542 /* Disable all lists management button when the list is empty */
543 this->SetWidgetsDisabledState(this->vehicles.Length() == 0 || _local_company != this->vli.company,
544 WID_GL_STOP_ALL,
545 WID_GL_START_ALL,
546 WID_GL_MANAGE_VEHICLES_DROPDOWN,
547 WIDGET_LIST_END);
549 /* Disable the group specific function when we select the default group or all vehicles */
550 this->SetWidgetsDisabledState(IsDefaultGroupID(this->vli.index) || IsAllGroupID(this->vli.index) || _local_company != this->vli.company,
551 WID_GL_DELETE_GROUP,
552 WID_GL_RENAME_GROUP,
553 WID_GL_REPLACE_PROTECTION,
554 WIDGET_LIST_END);
556 /* Disable remaining buttons for non-local companies
557 * Needed while changing _local_company, eg. by cheats
558 * All procedures (eg. move vehicle to another group)
559 * verify, whether you are the owner of the vehicle,
560 * so it doesn't have to be disabled
562 this->SetWidgetsDisabledState(_local_company != this->vli.company,
563 WID_GL_CREATE_GROUP,
564 WID_GL_AVAILABLE_VEHICLES,
565 WIDGET_LIST_END);
567 /* If not a default group and the group has replace protection, show an enabled replace sprite. */
568 uint16 protect_sprite = SPR_GROUP_REPLACE_OFF_TRAIN;
569 if (!IsDefaultGroupID(this->vli.index) && !IsAllGroupID(this->vli.index) && Group::Get(this->vli.index)->replace_protection) protect_sprite = SPR_GROUP_REPLACE_ON_TRAIN;
570 this->GetWidget<NWidgetCore>(WID_GL_REPLACE_PROTECTION)->widget_data = protect_sprite + this->vli.vtype;
572 /* Set text of sort by dropdown */
573 this->GetWidget<NWidgetCore>(WID_GL_SORT_BY_DROPDOWN)->widget_data = this->vehicle_sorter_names[this->vehicles.SortType()];
576 bool is_non_collapsable_group = (this->vli.index == ALL_GROUP) || (this->vli.index == DEFAULT_GROUP)
577 || (this->vli.index == INVALID_GROUP) || !this->collapsable_groups.Contains(this->vli.index);
579 this->SetWidgetDisabledState(WID_GL_COLLAPSE_EXPAND_GROUP, is_non_collapsable_group);
581 NWidgetCore *collapse_widget = this->GetWidget<NWidgetCore>(WID_GL_COLLAPSE_EXPAND_GROUP);
583 if (is_non_collapsable_group || !this->collapsed_groups.Contains(this->vli.index)) {
584 collapse_widget->widget_data = STR_GROUP_COLLAPSE;
585 collapse_widget->tool_tip = STR_GROUP_COLLAPSE_TOOLTIP;
586 } else {
587 collapse_widget->widget_data = STR_GROUP_EXPAND;
588 collapse_widget->tool_tip = STR_GROUP_EXPAND_TOOLTIP;
591 this->SetWidgetDisabledState(WID_GL_EXPAND_ALL_GROUPS, this->collapsed_groups.Length() == 0);
592 this->SetWidgetDisabledState(WID_GL_COLLAPSE_ALL_GROUPS, this->collapsable_groups.Length() == 0 || this->collapsed_groups.Length() == this->collapsable_groups.Length());
594 this->DrawWidgets();
597 virtual void DrawWidget(const Rect &r, int widget) const
599 switch (widget) {
600 case WID_GL_ALL_VEHICLES:
601 DrawGroupInfo(r.top + WD_FRAMERECT_TOP, r.left, r.right, ALL_GROUP);
602 break;
604 case WID_GL_DEFAULT_VEHICLES:
605 DrawGroupInfo(r.top + WD_FRAMERECT_TOP, r.left, r.right, DEFAULT_GROUP);
606 break;
608 case WID_GL_INFO: {
609 Money this_year = 0;
610 Money last_year = 0;
611 Money lifetime = 0;
612 uint32 occupancy = 0;
613 uint32 group_vehicles = 0;
615 for (uint i = 0, vehicle_count = this->vehicles.Length(); i < vehicle_count; i++) {
616 const Vehicle *v = this->vehicles[i];
618 assert(v->owner == this->owner);
620 this_year += v->GetDisplayProfitThisYear();
621 last_year += v->GetDisplayProfitLastYear();
622 lifetime += v->GetDisplayProfitLifetime();
625 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);
626 SetDParam(0, this_year);
627 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);
629 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);
630 SetDParam(0, last_year);
631 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);
633 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);
634 SetDParam(0, lifetime);
635 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);
637 break;
640 case WID_GL_LIST_GROUP: {
641 int y1 = r.top + WD_FRAMERECT_TOP;
642 int max = min(this->group_sb->GetPosition() + this->group_sb->GetCapacity(), this->groups.Length());
643 for (int i = this->group_sb->GetPosition(); i < max; ++i) {
644 const Group *g = this->groups[i];
646 assert(g->owner == this->owner);
648 DrawGroupInfo(y1, r.left, r.right, g->index, this->indents[i], g->replace_protection);
650 y1 += this->tiny_step_height;
652 if ((uint)this->group_sb->GetPosition() + this->group_sb->GetCapacity() > this->groups.Length()) {
653 DrawGroupInfo(y1, r.left, r.right, NEW_GROUP);
655 break;
658 case WID_GL_SORT_BY_ORDER:
659 this->DrawSortButtonState(WID_GL_SORT_BY_ORDER, this->vehicles.IsDescSortOrder() ? SBS_DOWN : SBS_UP);
660 break;
662 case WID_GL_LIST_VEHICLE:
663 if (this->vli.index != ALL_GROUP) {
664 /* Mark vehicles which are in sub-groups */
665 int y = r.top;
666 uint max = min(this->vscroll->GetPosition() + this->vscroll->GetCapacity(), this->vehicles.Length());
667 for (uint i = this->vscroll->GetPosition(); i < max; ++i) {
668 const Vehicle *v = this->vehicles[i];
669 if (v->group_id != this->vli.index) {
670 GfxFillRect(r.left + 1, y + 1, r.right - 1, y + this->resize.step_height - 2, _colour_gradient[COLOUR_GREY][3], FILLRECT_CHECKER);
672 y += this->resize.step_height;
676 this->DrawVehicleListItems(this->vehicle_sel, this->resize.step_height, r);
677 break;
681 static void DeleteGroupCallback(Window *win, bool confirmed)
683 if (confirmed) {
684 VehicleGroupWindow *w = (VehicleGroupWindow*)win;
685 w->vli.index = ALL_GROUP;
686 DoCommandP(0, w->group_confirm, 0, CMD_DELETE_GROUP | CMD_MSG(STR_ERROR_GROUP_CAN_T_DELETE));
690 virtual void OnClick(Point pt, int widget, int click_count)
692 switch (widget) {
693 case WID_GL_SORT_BY_ORDER: // Flip sorting method ascending/descending
694 this->vehicles.ToggleSortOrder();
695 this->SetDirty();
696 break;
698 case WID_GL_SORT_BY_DROPDOWN: // Select sorting criteria dropdown menu
699 ShowDropDownMenu(this, this->vehicle_sorter_names, this->vehicles.SortType(), WID_GL_SORT_BY_DROPDOWN, 0,
700 (this->vli.vtype == VEH_TRAIN || this->vli.vtype == VEH_ROAD) ? 0 : this->vehicle_sorter_non_ground_veh_disable_mask);
701 return;
703 case WID_GL_ALL_VEHICLES: // All vehicles button
704 if (!IsAllGroupID(this->vli.index)) {
705 this->vli.index = ALL_GROUP;
706 this->vehicles.ForceRebuild();
707 this->SetDirty();
709 break;
711 case WID_GL_DEFAULT_VEHICLES: // Ungrouped vehicles button
712 if (!IsDefaultGroupID(this->vli.index)) {
713 this->vli.index = DEFAULT_GROUP;
714 this->vehicles.ForceRebuild();
715 this->SetDirty();
717 break;
719 case WID_GL_LIST_GROUP: { // Matrix Group
720 uint id_g = this->group_sb->GetScrolledRowFromWidget(pt.y, this, WID_GL_LIST_GROUP, 0, this->tiny_step_height);
721 if (id_g >= this->groups.Length()) return;
723 this->group_sel = this->vli.index = this->groups[id_g]->index;
725 if (click_count % 2 == 0) {
726 this->ToogleGroupCollapse(this->vli.index);
727 OnInvalidateData();
730 SetObjectToPlaceWnd(SPR_CURSOR_MOUSE, PAL_NONE, HT_DRAG, this);
732 this->vehicles.ForceRebuild();
733 this->SetDirty();
734 break;
737 case WID_GL_LIST_VEHICLE: { // Matrix Vehicle
738 uint id_v = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_GL_LIST_VEHICLE);
739 if (id_v >= this->vehicles.Length()) return; // click out of list bound
741 const Vehicle *v = this->vehicles[id_v];
742 if (VehicleClicked(v)) break;
744 this->vehicle_sel = v->index;
746 SetObjectToPlaceWnd(SPR_CURSOR_MOUSE, PAL_NONE, HT_DRAG, this);
747 SetMouseCursorVehicle(v, EIT_IN_LIST);
748 _cursor.vehchain = true;
750 this->SetDirty();
751 break;
754 case WID_GL_CREATE_GROUP: { // Create a new group
755 DoCommandP(0, this->vli.vtype, 0, CMD_CREATE_GROUP | CMD_MSG(STR_ERROR_GROUP_CAN_T_CREATE), CcCreateGroup);
756 break;
759 case WID_GL_DELETE_GROUP: { // Delete the selected group
760 this->group_confirm = this->vli.index;
761 ShowQuery(STR_QUERY_GROUP_DELETE_CAPTION, STR_GROUP_DELETE_QUERY_TEXT, this, DeleteGroupCallback);
762 InvalidateWindowData(WC_TEMPLATEGUI_MAIN, 0, 0, 0);
763 break;
766 case WID_GL_RENAME_GROUP: // Rename the selected roup
767 this->ShowRenameGroupWindow(this->vli.index, false);
768 break;
770 case WID_GL_COLLAPSE_EXPAND_GROUP: // Toggle collapse/expand
771 this->ToogleGroupCollapse(this->vli.index);
772 OnInvalidateData();
773 this->SetDirty();
774 break;
776 case WID_GL_COLLAPSE_ALL_GROUPS: {
777 this->collapsed_groups.Clear();
778 for (const GroupID *gid = this->all_groups.Begin(); gid != this->all_groups.End(); ++gid) {
779 GroupID id = *gid;
780 if (id != ALL_GROUP && id != DEFAULT_GROUP && id != INVALID_GROUP && this->collapsable_groups.Contains(id)) {
781 *this->collapsed_groups.Append() = id;
784 OnInvalidateData();
785 this->SetDirty();
786 break;
789 case WID_GL_EXPAND_ALL_GROUPS: {
790 this->collapsed_groups.Clear();
791 OnInvalidateData();
792 this->SetDirty();
793 break;
795 case WID_GL_AUTO_GROUP: {
796 DoCommandP(0, vli.company, vli.vtype, CMD_AUTO_GROUP_VEHICLES, NULL);
798 this->vehicle_sel = INVALID_VEHICLE;
799 this->group_over = INVALID_GROUP;
800 this->SetDirty();
801 break;
804 case WID_GL_AVAILABLE_VEHICLES:
805 ShowBuildVehicleWindow(INVALID_TILE, this->vli.vtype);
806 break;
808 case WID_GL_MANAGE_VEHICLES_DROPDOWN: {
809 DropDownList *list = this->BuildActionDropdownList(true, Group::IsValidID(this->vli.index), this->vli.vtype == VEH_TRAIN, true);
810 ShowDropDownList(this, list, 0, WID_GL_MANAGE_VEHICLES_DROPDOWN);
811 break;
814 case WID_GL_START_ALL:
815 case WID_GL_STOP_ALL: { // Start/stop all vehicles of the list
816 DoCommandP(0, (1 << 1) | (widget == WID_GL_START_ALL ? (1 << 0) : 0), this->vli.Pack(), CMD_MASS_START_STOP);
817 break;
820 case WID_GL_REPLACE_PROTECTION: {
821 const Group *g = Group::GetIfValid(this->vli.index);
822 if (g != NULL) {
823 DoCommandP(0, this->vli.index, (g->replace_protection ? 0 : 1) | (_ctrl_pressed << 1), CMD_SET_GROUP_REPLACE_PROTECTION);
825 break;
830 void OnDragDrop_Group(Point pt, int widget)
832 const Group *g = Group::Get(this->group_sel);
834 switch (widget) {
835 case WID_GL_ALL_VEHICLES: // All vehicles
836 case WID_GL_DEFAULT_VEHICLES: // Ungroupd vehicles
837 if (g->parent != INVALID_GROUP) {
838 DoCommandP(0, this->group_sel | (1 << 16), INVALID_GROUP, CMD_ALTER_GROUP | CMD_MSG(STR_ERROR_GROUP_CAN_T_SET_PARENT));
841 this->group_sel = INVALID_GROUP;
842 this->group_over = INVALID_GROUP;
843 this->SetDirty();
844 break;
846 case WID_GL_LIST_GROUP: { // Matrix group
847 uint id_g = this->group_sb->GetScrolledRowFromWidget(pt.y, this, WID_GL_LIST_GROUP, 0, this->tiny_step_height);
848 GroupID new_g = id_g >= this->groups.Length() ? INVALID_GROUP : this->groups[id_g]->index;
850 if (this->group_sel != new_g && g->parent != new_g) {
851 DoCommandP(0, this->group_sel | (1 << 16), new_g, CMD_ALTER_GROUP | CMD_MSG(STR_ERROR_GROUP_CAN_T_SET_PARENT));
853 if (!_ctrl_pressed) {
854 GroupID *group = this->collapsed_groups.Find(new_g);
855 if (group != this->collapsed_groups.End()) this->collapsed_groups.Erase(group);
859 this->group_sel = INVALID_GROUP;
860 this->group_over = INVALID_GROUP;
861 this->SetDirty();
862 break;
867 void OnDragDrop_Vehicle(Point pt, int widget)
869 switch (widget) {
870 case WID_GL_DEFAULT_VEHICLES: // Ungrouped vehicles
871 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));
873 this->vehicle_sel = INVALID_VEHICLE;
874 this->group_over = INVALID_GROUP;
876 this->SetDirty();
877 break;
879 case WID_GL_LIST_GROUP: { // Matrix group
880 const VehicleID vindex = this->vehicle_sel;
881 this->vehicle_sel = INVALID_VEHICLE;
882 this->group_over = INVALID_GROUP;
883 this->SetDirty();
885 uint id_g = this->group_sb->GetScrolledRowFromWidget(pt.y, this, WID_GL_LIST_GROUP, 0, this->tiny_step_height);
886 GroupID new_g = id_g >= this->groups.Length() ? NEW_GROUP : this->groups[id_g]->index;
888 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 : NULL);
889 break;
892 case WID_GL_LIST_VEHICLE: { // Matrix vehicle
893 const VehicleID vindex = this->vehicle_sel;
894 this->vehicle_sel = INVALID_VEHICLE;
895 this->group_over = INVALID_GROUP;
896 this->SetDirty();
898 uint id_v = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_GL_LIST_VEHICLE);
899 if (id_v >= this->vehicles.Length()) return; // click out of list bound
901 const Vehicle *v = this->vehicles[id_v];
902 if (!VehicleClicked(v) && vindex == v->index) {
903 ShowVehicleViewWindow(v);
905 break;
907 case WID_GL_CREATE_GROUP: { // make new group with vehicle specific name and add vehicle
908 const VehicleID vindex = this->vehicle_sel;
909 this->vehicle_sel = INVALID_VEHICLE;
910 this->group_over = INVALID_GROUP;
911 this->SetDirty();
913 DoCommandP(0, vindex | (_ctrl_pressed ? 1 << 31 : 0),0 , CMD_CREATE_GROUP_SPECIFIC_NAME | CMD_MSG(STR_ERROR_GROUP_CAN_T_CREATE_SPECIFIC_NAME), NULL);
915 break;
920 virtual void OnDragDrop(Point pt, int widget)
922 if (this->vehicle_sel != INVALID_VEHICLE) OnDragDrop_Vehicle(pt, widget);
923 if (this->group_sel != INVALID_GROUP) OnDragDrop_Group(pt, widget);
925 _cursor.vehchain = false;
928 virtual void OnQueryTextFinished(char *str)
930 if (str != NULL) DoCommandP(0, this->group_rename, 0, CMD_ALTER_GROUP | CMD_MSG(STR_ERROR_GROUP_CAN_T_RENAME), NULL, str);
931 InvalidateWindowData(WC_TEMPLATEGUI_MAIN, 0, 0, 0);
932 this->group_rename = INVALID_GROUP;
935 virtual void OnResize()
937 this->group_sb->SetCapacityFromWidget(this, WID_GL_LIST_GROUP);
938 this->vscroll->SetCapacityFromWidget(this, WID_GL_LIST_VEHICLE);
941 virtual void OnDropdownSelect(int widget, int index)
943 switch (widget) {
944 case WID_GL_SORT_BY_DROPDOWN:
945 this->vehicles.SetSortType(index);
946 break;
948 case WID_GL_MANAGE_VEHICLES_DROPDOWN:
949 assert(this->vehicles.Length() != 0);
951 switch (index) {
952 case ADI_SET_ALL_ON_TIME: { // Reset all late timers
953 int num_vehicles = this->vehicles.Length();
955 for (int v_index = 0; v_index < num_vehicles; ++v_index) {
956 DoCommandP(0, this->vehicles[v_index]->index, 0, CMD_SET_VEHICLE_ON_TIME | CMD_MSG(STR_ERROR_CAN_T_TIMETABLE_VEHICLE));
959 break;
961 case ADI_TEMPLATE_REPLACE: // TemplateReplace Window
962 if ( vli.vtype == VEH_TRAIN )
963 ShowTemplateReplaceWindow(this->unitnumber_digits, this->resize.step_height);
964 break;
965 case ADI_REPLACE: // Replace window
966 ShowReplaceGroupVehicleWindow(this->vli.index, this->vli.vtype);
967 break;
968 case ADI_SERVICE: // Send for servicing
969 case ADI_DEPOT: { // Send to Depots
970 DoCommandP(0, DEPOT_MASS_SEND | (index == ADI_SERVICE ? DEPOT_SERVICE : 0U), this->vli.Pack(), GetCmdSendToDepot(this->vli.vtype));
971 break;
974 case ADI_ADD_SHARED: // Add shared Vehicles
975 assert(Group::IsValidID(this->vli.index));
977 DoCommandP(0, this->vli.index, this->vli.vtype, CMD_ADD_SHARED_VEHICLE_GROUP | CMD_MSG(STR_ERROR_GROUP_CAN_T_ADD_SHARED_VEHICLE));
978 break;
979 case ADI_REMOVE_ALL: // Remove all Vehicles from the selected group
980 assert(Group::IsValidID(this->vli.index));
982 DoCommandP(0, this->vli.index, 0, CMD_REMOVE_ALL_VEHICLES_GROUP | CMD_MSG(STR_ERROR_GROUP_CAN_T_REMOVE_ALL_VEHICLES));
983 break;
984 case ADI_SELL_ALL: // Sell all stopped Vehicles from the selected group
986 /* Only open the confirmation window if there are anything to sell */
987 bool is_all_group = (this->vli.index == ALL_GROUP);
988 bool is_default_group = (this->vli.index == DEFAULT_GROUP);
989 if (is_all_group || is_default_group || Group::Get(this->vli.index)->statistics.num_vehicle != 0) {
990 SetDParam(0, this->vli.index);
991 ShowQuery(
992 STR_GROUP_NAME,
993 STR_GROUP_SELL_CONFIRMATION_TEXT,
994 this,
995 SellAllConfirmationCallback
999 break;
1000 default: NOT_REACHED();
1002 break;
1004 default: NOT_REACHED();
1007 this->SetDirty();
1010 virtual void OnTick()
1012 if (_pause_mode != PM_UNPAUSED) return;
1013 if (this->groups.NeedResort() || this->vehicles.NeedResort()) {
1014 this->SetDirty();
1018 virtual void OnPlaceObjectAbort()
1020 /* abort drag & drop */
1021 this->vehicle_sel = INVALID_VEHICLE;
1022 this->DirtyHighlightedGroupWidget();
1023 this->group_over = INVALID_GROUP;
1024 this->SetWidgetDirty(WID_GL_LIST_VEHICLE);
1027 virtual void OnMouseDrag(Point pt, int widget)
1029 if (this->vehicle_sel == INVALID_VEHICLE && this->group_sel == INVALID_GROUP) return;
1031 /* A vehicle is dragged over... */
1032 GroupID new_group_over = INVALID_GROUP;
1033 switch (widget) {
1034 case WID_GL_DEFAULT_VEHICLES: // ... the 'default' group.
1035 new_group_over = DEFAULT_GROUP;
1036 break;
1038 case WID_GL_LIST_GROUP: { // ... the list of custom groups.
1039 uint id_g = this->group_sb->GetScrolledRowFromWidget(pt.y, this, WID_GL_LIST_GROUP, 0, this->tiny_step_height);
1040 new_group_over = id_g >= this->groups.Length() ? NEW_GROUP : this->groups[id_g]->index;
1041 break;
1045 /* Do not highlight when dragging over the current group */
1046 if (this->vehicle_sel != INVALID_VEHICLE) {
1047 if (Vehicle::Get(vehicle_sel)->group_id == new_group_over) new_group_over = INVALID_GROUP;
1048 } else if (this->group_sel != INVALID_GROUP) {
1049 if (this->group_sel == new_group_over || Group::Get(this->group_sel)->parent == new_group_over) new_group_over = INVALID_GROUP;
1052 /* Mark widgets as dirty if the group changed. */
1053 if (new_group_over != this->group_over) {
1054 this->DirtyHighlightedGroupWidget();
1055 this->group_over = new_group_over;
1056 this->DirtyHighlightedGroupWidget();
1060 void ShowRenameGroupWindow(GroupID group, bool empty)
1062 assert(Group::IsValidID(group));
1063 this->group_rename = group;
1064 /* Show empty query for new groups */
1065 StringID str = STR_EMPTY;
1066 if (!empty) {
1067 SetDParam(0, group);
1068 str = STR_GROUP_NAME;
1070 ShowQueryString(str, STR_GROUP_RENAME_CAPTION, MAX_LENGTH_GROUP_NAME_CHARS, this, CS_ALPHANUMERAL, QSF_ENABLE_DEFAULT | QSF_LEN_IN_CHARS);
1074 * Tests whether a given vehicle is selected in the window, and unselects it if necessary.
1075 * Called when the vehicle is deleted.
1076 * @param vehicle Vehicle that is going to be deleted
1078 void UnselectVehicle(VehicleID vehicle)
1080 if (this->vehicle_sel == vehicle) ResetObjectToPlace();
1084 static void SellAllConfirmationCallback(Window *win, bool confirmed)
1086 if (confirmed) {
1087 VehicleGroupWindow *w = (VehicleGroupWindow*)win;
1088 DoCommandP(0, w->vli.vtype, w->vli.index, CMD_GROUP_SELL_ALL_VEHICLES);
1092 static WindowDesc _other_group_desc(
1093 WDP_AUTO, "list_groups", 460, 246,
1094 WC_INVALID, WC_NONE,
1096 _nested_group_widgets, lengthof(_nested_group_widgets)
1099 static WindowDesc _train_group_desc(
1100 WDP_AUTO, "list_groups_train", 525, 246,
1101 WC_TRAINS_LIST, WC_NONE,
1103 _nested_group_widgets, lengthof(_nested_group_widgets)
1107 * Show the group window for the given company and vehicle type.
1108 * @param company The company to show the window for.
1109 * @param vehicle_type The type of vehicle to show it for.
1111 void ShowCompanyGroup(CompanyID company, VehicleType vehicle_type)
1113 if (!Company::IsValidID(company)) return;
1115 WindowNumber num = VehicleListIdentifier(VL_GROUP_LIST, vehicle_type, company).Pack();
1116 if (vehicle_type == VEH_TRAIN) {
1117 AllocateWindowDescFront<VehicleGroupWindow>(&_train_group_desc, num);
1118 } else {
1119 _other_group_desc.cls = GetWindowClassForVehicleType(vehicle_type);
1120 AllocateWindowDescFront<VehicleGroupWindow>(&_other_group_desc, num);
1125 * Finds a group list window determined by vehicle type and owner
1126 * @param vt vehicle type
1127 * @param owner owner of groups
1128 * @return pointer to VehicleGroupWindow, NULL if not found
1130 static inline VehicleGroupWindow *FindVehicleGroupWindow(VehicleType vt, Owner owner)
1132 return (VehicleGroupWindow *)FindWindowById(GetWindowClassForVehicleType(vt), VehicleListIdentifier(VL_GROUP_LIST, vt, owner).Pack());
1136 * Opens a 'Rename group' window for newly created group
1137 * @param success did command succeed?
1138 * @param tile unused
1139 * @param p1 vehicle type
1140 * @param p2 unused
1141 * @see CmdCreateGroup
1143 void CcCreateGroup(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
1145 if (result.Failed()) return;
1146 assert(p1 <= VEH_AIRCRAFT);
1148 VehicleGroupWindow *w = FindVehicleGroupWindow((VehicleType)p1, _current_company);
1149 if (w != NULL) w->ShowRenameGroupWindow(_new_group_id, true);
1153 * Open rename window after adding a vehicle to a new group via drag and drop.
1154 * @param success Did command succeed?
1155 * @param tile Unused.
1156 * @param p1 Unused.
1157 * @param p2 Bit 0-19: Vehicle ID.
1159 void CcAddVehicleNewGroup(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
1161 if (result.Failed()) return;
1162 assert(Vehicle::IsValidID(GB(p2, 0, 20)));
1164 CcCreateGroup(result, 0, Vehicle::Get(GB(p2, 0, 20))->type, 0);
1168 * Removes the highlight of a vehicle in a group window
1169 * @param *v Vehicle to remove all highlights from
1171 void DeleteGroupHighlightOfVehicle(const Vehicle *v)
1173 /* If we haven't got any vehicles on the mouse pointer, we haven't got any highlighted in any group windows either
1174 * If that is the case, we can skip looping though the windows and save time
1176 if (_special_mouse_mode != WSM_DRAGDROP) return;
1178 VehicleGroupWindow *w = FindVehicleGroupWindow(v->type, v->owner);
1179 if (w != NULL) w->UnselectVehicle(v->index);