Merge branch 'development' into feature/no_multiplayer_grf_limit
[openttd-joker.git] / src / group_gui.cpp
blob921cf2875af60194f55b87c29a7acfac5e2785c4
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_PANEL, COLOUR_GREY), SetMinimalSize(12, 12), SetResize(1, 0), EndContainer(),
86 EndContainer(),
87 NWidget(NWID_HORIZONTAL),
88 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),
89 NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_GL_LIST_VEHICLE_SCROLLBAR),
90 EndContainer(),
91 NWidget(WWT_PANEL, COLOUR_GREY), SetMinimalSize(1, 0), SetFill(1, 1), SetResize(1, 0), EndContainer(),
92 NWidget(NWID_HORIZONTAL),
93 NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_GL_AVAILABLE_VEHICLES), SetMinimalSize(106, 12), SetFill(0, 1),
94 SetDataTip(STR_BLACK_STRING, STR_VEHICLE_LIST_AVAILABLE_ENGINES_TOOLTIP),
95 NWidget(WWT_PANEL, COLOUR_GREY), SetMinimalSize(0, 12), SetFill(1, 1), SetResize(1, 0), EndContainer(),
96 NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_GL_MANAGE_VEHICLES_DROPDOWN), SetMinimalSize(118, 12), SetFill(0, 1),
97 SetDataTip(STR_VEHICLE_LIST_MANAGE_LIST, STR_VEHICLE_LIST_MANAGE_LIST_TOOLTIP),
98 NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_GL_STOP_ALL), SetMinimalSize(12, 12), SetFill(0, 1),
99 SetDataTip(SPR_FLAG_VEH_STOPPED, STR_VEHICLE_LIST_MASS_STOP_LIST_TOOLTIP),
100 NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_GL_START_ALL), SetMinimalSize(12, 12), SetFill(0, 1),
101 SetDataTip(SPR_FLAG_VEH_RUNNING, STR_VEHICLE_LIST_MASS_START_LIST_TOOLTIP),
102 NWidget(WWT_RESIZEBOX, COLOUR_GREY),
103 EndContainer(),
104 EndContainer(),
105 EndContainer(),
108 /** Sort the groups by their name */
109 int CDECL GroupNameSorter(const Group * const *a, const Group * const *b)
111 static const Group *last_group[2] = { NULL, NULL };
112 static char last_name[2][64] = { "", "" };
114 if (*a != last_group[0]) {
115 last_group[0] = *a;
116 SetDParam(0, (*a)->index);
117 GetString(last_name[0], STR_GROUP_NAME, lastof(last_name[0]));
120 if (*b != last_group[1]) {
121 last_group[1] = *b;
122 SetDParam(0, (*b)->index);
123 GetString(last_name[1], STR_GROUP_NAME, lastof(last_name[1]));
126 int r = strnatcmp(last_name[0], last_name[1]); // Sort by name (natural sorting).
127 if (r == 0) return (*a)->index - (*b)->index;
128 return r;
132 class VehicleGroupWindow : public BaseVehicleListWindow {
133 private:
134 /* Columns in the group list */
135 enum ListColumns {
136 VGC_NAME, ///< Group name.
137 VGC_COLLAPSED, ///< Collapsed icon
138 VGC_PROTECT, ///< Autoreplace protect icon.
139 VGC_AUTOREPLACE, ///< Autoreplace active icon.
140 VGC_PROFIT, ///< Profit icon.
141 VGC_NUMBER, ///< Number of vehicles in the group.
143 VGC_END
146 GroupID group_sel; ///< Selected group (for drag/drop)
147 GroupID group_rename; ///< Group being renamed, INVALID_GROUP if none
148 GroupID group_over; ///< Group over which a vehicle is dragged, INVALID_GROUP if none
149 GroupID group_confirm; ///< Group awaiting delete confirmation
150 GUIGroupList groups; ///< List of groups
151 uint tiny_step_height; ///< Step height for the group list
152 Scrollbar *group_sb;
154 SmallVector<GroupID, 16> all_groups; ///< List of all groups, including hidden ones
155 SmallVector<GroupID, 16> collapsed_groups; ///< List of collapsed groups
156 SmallVector<GroupID, 16> collapsable_groups; ///< List of collapsable groups
158 SmallVector<int, 16> indents; ///< Indentation levels
160 Dimension column_size[VGC_END]; ///< Size of the columns in the group list.
162 /** return true if group has children */
163 bool AddParents(GUIGroupList *source, GroupID parent, int indent, bool parent_collapsed)
165 bool is_collapsed = this->collapsed_groups.Contains(parent);
166 bool overall_collapsed = is_collapsed || parent_collapsed;
167 bool has_children = false;
169 for (const Group **g = source->Begin(); g != source->End(); g++) {
170 if ((*g)->parent == parent) {
171 has_children = true;
172 *this->all_groups.Append() = (*g)->index;
173 if (!overall_collapsed) {
174 *this->groups.Append() = *g;
175 *this->indents.Append() = indent;
177 bool child_has_children = AddParents(source, (*g)->index, indent + 1, overall_collapsed);
178 if (child_has_children) *this->collapsable_groups.Append() = (*g)->index;
182 return has_children;
185 void ToogleGroupCollapse(GroupID group)
187 GroupID *item = this->collapsed_groups.Find(group);
188 if (item == this->collapsed_groups.End()) {
189 if (this->collapsable_groups.Find(group) != this->collapsable_groups.End()) {
190 *(this->collapsed_groups.Append()) = group;
192 } else {
193 this->collapsed_groups.Erase(item);
198 * (Re)Build the group list.
200 * @param owner The owner of the window
202 void BuildGroupList(Owner owner)
204 if (!this->groups.NeedRebuild()) return;
206 this->all_groups.Clear();
207 this->groups.Clear();
208 this->indents.Clear();
209 this->collapsable_groups.Clear();
211 GUIGroupList list;
213 const Group *g;
214 FOR_ALL_GROUPS(g) {
215 if (g->owner == owner && g->vehicle_type == this->vli.vtype) {
216 *list.Append() = g;
220 list.ForceResort();
221 list.Sort(&GroupNameSorter);
223 AddParents(&list, INVALID_GROUP, 0, false);
225 this->groups.Compact();
226 this->groups.RebuildDone();
230 * Compute tiny_step_height and column_size
231 * @return Total width required for the group list.
233 uint ComputeGroupInfoSize()
235 this->column_size[VGC_COLLAPSED] = GetSpriteSize(SPR_WINDOW_UNSHADE);
236 this->tiny_step_height = this->column_size[VGC_COLLAPSED].height;
238 this->column_size[VGC_NAME] = maxdim(GetStringBoundingBox(STR_GROUP_DEFAULT_TRAINS + this->vli.vtype), GetStringBoundingBox(STR_GROUP_ALL_TRAINS + this->vli.vtype));
239 this->column_size[VGC_NAME].width = max((170u * FONT_HEIGHT_NORMAL) / 10u, this->column_size[VGC_NAME].width);
240 this->tiny_step_height = max(this->tiny_step_height, this->column_size[VGC_NAME].height);
242 this->column_size[VGC_PROTECT] = GetSpriteSize(SPR_GROUP_REPLACE_PROTECT);
243 this->tiny_step_height = max(this->tiny_step_height, this->column_size[VGC_PROTECT].height);
245 this->column_size[VGC_AUTOREPLACE] = GetSpriteSize(SPR_GROUP_REPLACE_ACTIVE);
246 this->tiny_step_height = max(this->tiny_step_height, this->column_size[VGC_AUTOREPLACE].height);
248 this->column_size[VGC_PROFIT].width = 0;
249 this->column_size[VGC_PROFIT].height = 0;
250 static const SpriteID profit_sprites[] = {SPR_PROFIT_NA, SPR_PROFIT_NEGATIVE, SPR_PROFIT_SOME, SPR_PROFIT_LOT};
251 for (uint i = 0; i < lengthof(profit_sprites); i++) {
252 Dimension d = GetSpriteSize(profit_sprites[i]);
253 this->column_size[VGC_PROFIT] = maxdim(this->column_size[VGC_PROFIT], d);
255 this->tiny_step_height = max(this->tiny_step_height, this->column_size[VGC_PROFIT].height);
257 SetDParamMaxValue(0, GroupStatistics::Get(this->vli.company, ALL_GROUP, this->vli.vtype).num_vehicle, 3, FS_SMALL);
258 this->column_size[VGC_NUMBER] = GetStringBoundingBox(STR_TINY_COMMA);
259 this->tiny_step_height = max(this->tiny_step_height, this->column_size[VGC_NUMBER].height);
261 this->tiny_step_height += WD_MATRIX_TOP;
263 return WD_FRAMERECT_LEFT + 8 +
264 this->column_size[VGC_COLLAPSED].width + 2 +
265 this->column_size[VGC_NAME].width + 8 +
266 this->column_size[VGC_PROTECT].width + 2 +
267 this->column_size[VGC_AUTOREPLACE].width + 2 +
268 this->column_size[VGC_PROFIT].width + 2 +
269 this->column_size[VGC_NUMBER].width + 2 +
270 WD_FRAMERECT_RIGHT;
274 * Draw a row in the group list.
275 * @param y Top of the row.
276 * @param left Left of the row.
277 * @param right Right of the row.
278 * @param g_id Group to list.
279 * @param indent Indentation level.
280 * @param protection Whether autoreplac/// e protection is set.
282 void DrawGroupInfo(int y, int left, int right, GroupID g_id, int indent = 0, bool protection = false) const
284 /* Highlight the group if a vehicle is dragged over it */
285 if (g_id == this->group_over) {
286 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]);
289 if (g_id == NEW_GROUP) return;
291 /* draw the selected group in white, else we draw it in black */
292 TextColour colour = g_id == this->vli.index ? TC_WHITE : TC_BLACK;
293 const GroupStatistics &stats = GroupStatistics::Get(this->vli.company, g_id, this->vli.vtype);
294 bool rtl = _current_text_dir == TD_RTL;
296 /* draw group name */
297 StringID str;
298 if (IsAllGroupID(g_id)) {
299 str = STR_GROUP_ALL_TRAINS + this->vli.vtype;
300 } else if (IsDefaultGroupID(g_id)) {
301 str = STR_GROUP_DEFAULT_TRAINS + this->vli.vtype;
302 } else {
303 SetDParam(0, g_id);
304 str = STR_GROUP_NAME;
307 /* draw collapse state */
308 int x = rtl ? right - WD_FRAMERECT_RIGHT - 8 - this->column_size[VGC_COLLAPSED].width + 1 : left + WD_FRAMERECT_LEFT + 8;
309 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);
311 x = rtl ? right - WD_FRAMERECT_RIGHT - 8 - this->column_size[VGC_NAME].width + 1 : x + 8 + this->column_size[VGC_COLLAPSED].width;
312 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);
314 /* draw autoreplace protection */
315 x = rtl ? x - 2 - this->column_size[VGC_PROTECT].width : x + 2 + this->column_size[VGC_NAME].width;
316 if (protection) DrawSprite(SPR_GROUP_REPLACE_PROTECT, PAL_NONE, x, y + (this->tiny_step_height - this->column_size[VGC_PROTECT].height) / 2);
318 /* draw autoreplace status */
319 x = rtl ? x - 2 - this->column_size[VGC_AUTOREPLACE].width : x + 2 + this->column_size[VGC_PROTECT].width;
320 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);
322 /* draw the profit icon */
323 x = rtl ? x - 2 - this->column_size[VGC_PROFIT].width : x + 2 + this->column_size[VGC_AUTOREPLACE].width;
324 SpriteID spr;
325 if (stats.num_profit_vehicle == 0) {
326 spr = SPR_PROFIT_NA;
327 } else if (stats.profit_last_year < 0) {
328 spr = SPR_PROFIT_NEGATIVE;
329 } else if (stats.profit_last_year < 10000 * stats.num_profit_vehicle) { // TODO magic number
330 spr = SPR_PROFIT_SOME;
331 } else {
332 spr = SPR_PROFIT_LOT;
334 DrawSprite(spr, PAL_NONE, x, y + (this->tiny_step_height - this->column_size[VGC_PROFIT].height) / 2);
336 /* draw the number of vehicles of the group */
337 x = rtl ? x - 2 - this->column_size[VGC_NUMBER].width : x + 2 + this->column_size[VGC_PROFIT].width;
338 SetDParam(0, stats.num_vehicle);
339 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);
343 * Mark the widget containing the currently highlighted group as dirty.
345 void DirtyHighlightedGroupWidget()
347 if (this->group_over == INVALID_GROUP) return;
349 if (IsAllGroupID(this->group_over)) {
350 this->SetWidgetDirty(WID_GL_ALL_VEHICLES);
351 } else if (IsDefaultGroupID(this->group_over)) {
352 this->SetWidgetDirty(WID_GL_DEFAULT_VEHICLES);
353 } else {
354 this->SetWidgetDirty(WID_GL_LIST_GROUP);
358 public:
359 VehicleGroupWindow(WindowDesc *desc, WindowNumber window_number) : BaseVehicleListWindow(desc, window_number)
361 this->CreateNestedTree();
363 this->vscroll = this->GetScrollbar(WID_GL_LIST_VEHICLE_SCROLLBAR);
364 this->group_sb = this->GetScrollbar(WID_GL_LIST_GROUP_SCROLLBAR);
366 switch (this->vli.vtype) {
367 default: NOT_REACHED();
368 case VEH_TRAIN: this->sorting = &_sorting.train; break;
369 case VEH_ROAD: this->sorting = &_sorting.roadveh; break;
370 case VEH_SHIP: this->sorting = &_sorting.ship; break;
371 case VEH_AIRCRAFT: this->sorting = &_sorting.aircraft; break;
374 this->vli.index = ALL_GROUP;
375 this->group_sel = INVALID_GROUP;
376 this->group_rename = INVALID_GROUP;
377 this->group_over = INVALID_GROUP;
379 this->vehicles.SetListing(*this->sorting);
380 this->vehicles.ForceRebuild();
381 this->vehicles.NeedResort();
383 this->BuildVehicleList();
384 this->SortVehicleList();
386 this->groups.ForceRebuild();
387 this->groups.NeedResort();
388 this->BuildGroupList(vli.company);
390 this->GetWidget<NWidgetCore>(WID_GL_CAPTION)->widget_data = STR_VEHICLE_LIST_TRAIN_CAPTION + this->vli.vtype;
391 this->GetWidget<NWidgetCore>(WID_GL_LIST_VEHICLE)->tool_tip = STR_VEHICLE_LIST_TRAIN_LIST_TOOLTIP + this->vli.vtype;
393 this->GetWidget<NWidgetCore>(WID_GL_COLLAPSE_EXPAND_GROUP)->widget_data = STR_GROUP_COLLAPSE;
394 this->GetWidget<NWidgetCore>(WID_GL_COLLAPSE_EXPAND_GROUP)->tool_tip = STR_GROUP_COLLAPSE_TOOLTIP;
396 this->GetWidget<NWidgetCore>(WID_GL_CREATE_GROUP)->widget_data += this->vli.vtype;
397 this->GetWidget<NWidgetCore>(WID_GL_RENAME_GROUP)->widget_data += this->vli.vtype;
398 this->GetWidget<NWidgetCore>(WID_GL_DELETE_GROUP)->widget_data += this->vli.vtype;
399 this->GetWidget<NWidgetCore>(WID_GL_REPLACE_PROTECTION)->widget_data += this->vli.vtype;
401 this->FinishInitNested(window_number);
402 this->owner = vli.company;
405 ~VehicleGroupWindow()
407 *this->sorting = this->vehicles.GetListing();
410 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
412 switch (widget) {
413 case WID_GL_LIST_GROUP: {
414 size->width = this->ComputeGroupInfoSize();
415 resize->height = this->tiny_step_height;
417 /* Minimum height is the height of the list widget minus all and default vehicles... */
418 size->height = 4 * GetVehicleListHeight(this->vli.vtype, this->tiny_step_height) - 2 * this->tiny_step_height;
420 /* ... minus the buttons at the bottom ... */
421 uint max_icon_height = GetSpriteSize(this->GetWidget<NWidgetCore>(WID_GL_CREATE_GROUP)->widget_data).height;
422 max_icon_height = max(max_icon_height, GetSpriteSize(this->GetWidget<NWidgetCore>(WID_GL_RENAME_GROUP)->widget_data).height);
423 max_icon_height = max(max_icon_height, GetSpriteSize(this->GetWidget<NWidgetCore>(WID_GL_DELETE_GROUP)->widget_data).height);
424 max_icon_height = max(max_icon_height, GetSpriteSize(this->GetWidget<NWidgetCore>(WID_GL_REPLACE_PROTECTION)->widget_data).height);
426 /* ... minus the height of the group info ... */
427 max_icon_height += (FONT_HEIGHT_NORMAL * 3) + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
429 /* Get a multiple of tiny_step_height of that amount */
430 size->height = Ceil(size->height - max_icon_height, tiny_step_height);
431 break;
434 case WID_GL_ALL_VEHICLES:
435 case WID_GL_DEFAULT_VEHICLES:
436 size->width = this->ComputeGroupInfoSize();
437 size->height = this->tiny_step_height;
438 break;
440 case WID_GL_SORT_BY_ORDER: {
441 Dimension d = GetStringBoundingBox(this->GetWidget<NWidgetCore>(widget)->widget_data);
442 d.width += padding.width + Window::SortButtonWidth() * 2; // Doubled since the string is centred and it also looks better.
443 d.height += padding.height;
444 *size = maxdim(*size, d);
445 break;
448 case WID_GL_LIST_VEHICLE:
449 this->ComputeGroupInfoSize();
450 resize->height = GetVehicleListHeight(this->vli.vtype, this->tiny_step_height);
451 size->height = 4 * resize->height;
452 break;
454 case WID_GL_MANAGE_VEHICLES_DROPDOWN: {
455 Dimension d = this->GetActionDropdownSize(true, true, this->vli.vtype == VEH_TRAIN);
456 d.height += padding.height;
457 d.width += padding.width;
458 *size = maxdim(*size, d);
459 break;
462 case WID_GL_INFO: {
463 size->height = (FONT_HEIGHT_NORMAL * 3) + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM + 5;
464 break;
470 * Some data on this window has become invalid.
471 * @param data Information about the changed data.
472 * @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.
474 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
476 if (data == 0) {
477 /* This needs to be done in command-scope to enforce rebuilding before resorting invalid data */
478 this->vehicles.ForceRebuild();
479 this->groups.ForceRebuild();
480 } else {
481 this->vehicles.ForceResort();
482 this->groups.ForceResort();
485 /* Process ID-invalidation in command-scope as well */
486 if (this->group_rename != INVALID_GROUP && !Group::IsValidID(this->group_rename)) {
487 DeleteWindowByClass(WC_QUERY_STRING);
488 this->group_rename = INVALID_GROUP;
491 if (!(IsAllGroupID(this->vli.index) || IsDefaultGroupID(this->vli.index) || Group::IsValidID(this->vli.index))) {
492 this->vli.index = ALL_GROUP;
493 HideDropDownMenu(this);
495 this->SetDirty();
498 virtual void SetStringParameters(int widget) const
500 switch (widget) {
501 case WID_GL_FILTER_BY_CARGO:
502 SetDParam(0, this->cargo_filter_texts[this->cargo_filter_criteria]);
503 break;
505 case WID_GL_AVAILABLE_VEHICLES:
506 SetDParam(0, STR_VEHICLE_LIST_AVAILABLE_TRAINS + this->vli.vtype);
507 break;
509 case WID_GL_CAPTION:
510 /* If selected_group == DEFAULT_GROUP || ALL_GROUP, draw the standard caption
511 * We list all vehicles or ungrouped vehicles */
512 if (IsDefaultGroupID(this->vli.index) || IsAllGroupID(this->vli.index)) {
513 SetDParam(0, STR_COMPANY_NAME);
514 SetDParam(1, this->vli.company);
515 SetDParam(2, this->vehicles.Length());
516 SetDParam(3, this->vehicles.Length());
517 } else {
518 const Group *g = Group::Get(this->vli.index);
520 SetDParam(0, STR_GROUP_NAME);
521 SetDParam(1, g->index);
522 SetDParam(2, g->statistics.num_vehicle);
523 SetDParam(3, g->statistics.num_vehicle);
525 break;
529 virtual void OnPaint()
531 /* If we select the all vehicles, this->list will contain all vehicles of the owner
532 * else this->list will contain all vehicles which belong to the selected group */
533 this->BuildVehicleList();
534 this->SortVehicleList();
536 this->BuildGroupList(this->owner);
538 this->group_sb->SetCount(this->groups.Length());
539 this->vscroll->SetCount(this->vehicles.Length());
541 /* The drop down menu is out, *but* it may not be used, retract it. */
542 if (!this->ShouldShowActionDropdownList() && this->IsWidgetLowered(WID_GL_MANAGE_VEHICLES_DROPDOWN)) {
543 this->RaiseWidget(WID_GL_MANAGE_VEHICLES_DROPDOWN);
544 HideDropDownMenu(this);
547 /* Disable all lists management button when the list is empty */
548 this->SetWidgetDisabledState(WID_GL_MANAGE_VEHICLES_DROPDOWN, !this->ShouldShowActionDropdownList() || _local_company != this->vli.company);
549 this->SetWidgetsDisabledState(this->vehicles.Length() == 0 || _local_company != this->vli.company,
550 WID_GL_STOP_ALL,
551 WID_GL_START_ALL,
552 WIDGET_LIST_END);
554 /* Disable the group specific function when we select the default group or all vehicles */
555 this->SetWidgetsDisabledState(IsDefaultGroupID(this->vli.index) || IsAllGroupID(this->vli.index) || _local_company != this->vli.company,
556 WID_GL_DELETE_GROUP,
557 WID_GL_RENAME_GROUP,
558 WID_GL_REPLACE_PROTECTION,
559 WIDGET_LIST_END);
561 /* Disable remaining buttons for non-local companies
562 * Needed while changing _local_company, eg. by cheats
563 * All procedures (eg. move vehicle to another group)
564 * verify, whether you are the owner of the vehicle,
565 * so it doesn't have to be disabled
567 this->SetWidgetsDisabledState(_local_company != this->vli.company,
568 WID_GL_CREATE_GROUP,
569 WID_GL_AVAILABLE_VEHICLES,
570 WIDGET_LIST_END);
572 /* If not a default group and the group has replace protection, show an enabled replace sprite. */
573 uint16 protect_sprite = SPR_GROUP_REPLACE_OFF_TRAIN;
574 if (!IsDefaultGroupID(this->vli.index) && !IsAllGroupID(this->vli.index) && Group::Get(this->vli.index)->replace_protection) protect_sprite = SPR_GROUP_REPLACE_ON_TRAIN;
575 this->GetWidget<NWidgetCore>(WID_GL_REPLACE_PROTECTION)->widget_data = protect_sprite + this->vli.vtype;
577 /* Set text of sort by dropdown */
578 this->GetWidget<NWidgetCore>(WID_GL_SORT_BY_DROPDOWN)->widget_data = this->vehicle_sorter_names[this->vehicles.SortType()];
580 this->GetWidget<NWidgetCore>(WID_GL_FILTER_BY_CARGO)->widget_data = this->cargo_filter_texts[this->cargo_filter_criteria];
582 bool is_non_collapsable_group = (this->vli.index == ALL_GROUP) || (this->vli.index == DEFAULT_GROUP)
583 || (this->vli.index == INVALID_GROUP) || !this->collapsable_groups.Contains(this->vli.index);
585 this->SetWidgetDisabledState(WID_GL_COLLAPSE_EXPAND_GROUP, is_non_collapsable_group);
587 NWidgetCore *collapse_widget = this->GetWidget<NWidgetCore>(WID_GL_COLLAPSE_EXPAND_GROUP);
589 if (is_non_collapsable_group || !this->collapsed_groups.Contains(this->vli.index)) {
590 collapse_widget->widget_data = STR_GROUP_COLLAPSE;
591 collapse_widget->tool_tip = STR_GROUP_COLLAPSE_TOOLTIP;
592 } else {
593 collapse_widget->widget_data = STR_GROUP_EXPAND;
594 collapse_widget->tool_tip = STR_GROUP_EXPAND_TOOLTIP;
597 this->SetWidgetDisabledState(WID_GL_EXPAND_ALL_GROUPS, this->collapsed_groups.Length() == 0);
598 this->SetWidgetDisabledState(WID_GL_COLLAPSE_ALL_GROUPS, this->collapsable_groups.Length() == 0 || this->collapsed_groups.Length() == this->collapsable_groups.Length());
600 this->DrawWidgets();
603 virtual void DrawWidget(const Rect &r, int widget) const
605 switch (widget) {
606 case WID_GL_ALL_VEHICLES:
607 DrawGroupInfo(r.top + WD_FRAMERECT_TOP, r.left, r.right, ALL_GROUP);
608 break;
610 case WID_GL_DEFAULT_VEHICLES:
611 DrawGroupInfo(r.top + WD_FRAMERECT_TOP, r.left, r.right, DEFAULT_GROUP);
612 break;
614 case WID_GL_INFO: {
615 Money this_year = 0;
616 Money last_year = 0;
617 Money lifetime = 0;
618 uint32 occupancy = 0;
619 uint32 group_vehicles = 0;
621 for (uint i = 0, vehicle_count = this->vehicles.Length(); i < vehicle_count; i++) {
622 const Vehicle *v = this->vehicles[i];
624 assert(v->owner == this->owner);
626 this_year += v->GetDisplayProfitThisYear();
627 last_year += v->GetDisplayProfitLastYear();
628 lifetime += v->GetDisplayProfitLifetime();
631 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);
632 SetDParam(0, this_year);
633 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);
635 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);
636 SetDParam(0, last_year);
637 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);
639 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);
640 SetDParam(0, lifetime);
641 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);
643 break;
646 case WID_GL_LIST_GROUP: {
647 int y1 = r.top + WD_FRAMERECT_TOP;
648 int max = min(this->group_sb->GetPosition() + this->group_sb->GetCapacity(), this->groups.Length());
649 for (int i = this->group_sb->GetPosition(); i < max; ++i) {
650 const Group *g = this->groups[i];
652 assert(g->owner == this->owner);
654 DrawGroupInfo(y1, r.left, r.right, g->index, this->indents[i], g->replace_protection);
656 y1 += this->tiny_step_height;
658 if ((uint)this->group_sb->GetPosition() + this->group_sb->GetCapacity() > this->groups.Length()) {
659 DrawGroupInfo(y1, r.left, r.right, NEW_GROUP);
661 break;
664 case WID_GL_SORT_BY_ORDER:
665 this->DrawSortButtonState(WID_GL_SORT_BY_ORDER, this->vehicles.IsDescSortOrder() ? SBS_DOWN : SBS_UP);
666 break;
668 case WID_GL_LIST_VEHICLE:
669 if (this->vli.index != ALL_GROUP) {
670 /* Mark vehicles which are in sub-groups */
671 int y = r.top;
672 uint max = min(this->vscroll->GetPosition() + this->vscroll->GetCapacity(), this->vehicles.Length());
673 for (uint i = this->vscroll->GetPosition(); i < max; ++i) {
674 const Vehicle *v = this->vehicles[i];
675 if (v->group_id != this->vli.index) {
676 GfxFillRect(r.left + 1, y + 1, r.right - 1, y + this->resize.step_height - 2, _colour_gradient[COLOUR_GREY][3], FILLRECT_CHECKER);
678 y += this->resize.step_height;
682 this->DrawVehicleListItems(this->vehicle_sel, this->resize.step_height, r);
683 break;
687 static void DeleteGroupCallback(Window *win, bool confirmed)
689 if (confirmed) {
690 VehicleGroupWindow *w = (VehicleGroupWindow*)win;
691 w->vli.index = ALL_GROUP;
692 DoCommandP(0, w->group_confirm, 0, CMD_DELETE_GROUP | CMD_MSG(STR_ERROR_GROUP_CAN_T_DELETE));
696 virtual void OnClick(Point pt, int widget, int click_count)
698 switch (widget) {
699 case WID_GL_SORT_BY_ORDER: // Flip sorting method ascending/descending
700 this->vehicles.ToggleSortOrder();
701 this->SetDirty();
702 break;
704 case WID_GL_SORT_BY_DROPDOWN: // Select sorting criteria dropdown menu
705 ShowDropDownMenu(this, this->vehicle_sorter_names, this->vehicles.SortType(), WID_GL_SORT_BY_DROPDOWN, 0,
706 (this->vli.vtype == VEH_TRAIN || this->vli.vtype == VEH_ROAD) ? 0 : this->vehicle_sorter_non_ground_veh_disable_mask);
707 return;
709 case WID_GL_FILTER_BY_CARGO: // Select filtering criteria dropdown menu
710 ShowDropDownMenu(this, this->cargo_filter_texts, this->cargo_filter_criteria, WID_GL_FILTER_BY_CARGO, 0, 0);
711 break;
713 case WID_GL_ALL_VEHICLES: // All vehicles button
714 if (!IsAllGroupID(this->vli.index)) {
715 this->vli.index = ALL_GROUP;
716 this->vehicles.ForceRebuild();
717 this->SetDirty();
719 break;
721 case WID_GL_DEFAULT_VEHICLES: // Ungrouped vehicles button
722 if (!IsDefaultGroupID(this->vli.index)) {
723 this->vli.index = DEFAULT_GROUP;
724 this->vehicles.ForceRebuild();
725 this->SetDirty();
727 break;
729 case WID_GL_LIST_GROUP: { // Matrix Group
730 uint id_g = this->group_sb->GetScrolledRowFromWidget(pt.y, this, WID_GL_LIST_GROUP, 0, this->tiny_step_height);
731 if (id_g >= this->groups.Length()) return;
733 this->group_sel = this->vli.index = this->groups[id_g]->index;
735 if (click_count % 2 == 0) {
736 this->ToogleGroupCollapse(this->vli.index);
737 OnInvalidateData();
740 SetObjectToPlaceWnd(SPR_CURSOR_MOUSE, PAL_NONE, HT_DRAG, this);
742 this->vehicles.ForceRebuild();
743 this->SetDirty();
744 break;
747 case WID_GL_LIST_VEHICLE: { // Matrix Vehicle
748 uint id_v = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_GL_LIST_VEHICLE);
749 if (id_v >= this->vehicles.Length()) return; // click out of list bound
751 const Vehicle *v = this->vehicles[id_v];
752 if (VehicleClicked(v)) break;
754 this->vehicle_sel = v->index;
756 SetObjectToPlaceWnd(SPR_CURSOR_MOUSE, PAL_NONE, HT_DRAG, this);
757 SetMouseCursorVehicle(v, EIT_IN_LIST);
758 _cursor.vehchain = true;
760 this->SetDirty();
761 break;
764 case WID_GL_CREATE_GROUP: { // Create a new group
765 DoCommandP(0, this->vli.vtype, 0, CMD_CREATE_GROUP | CMD_MSG(STR_ERROR_GROUP_CAN_T_CREATE), CcCreateGroup);
766 break;
769 case WID_GL_DELETE_GROUP: { // Delete the selected group
770 this->group_confirm = this->vli.index;
771 ShowQuery(STR_QUERY_GROUP_DELETE_CAPTION, STR_GROUP_DELETE_QUERY_TEXT, this, DeleteGroupCallback);
772 InvalidateWindowData(WC_TEMPLATEGUI_MAIN, 0, 0, 0);
773 break;
776 case WID_GL_RENAME_GROUP: // Rename the selected roup
777 this->ShowRenameGroupWindow(this->vli.index, false);
778 break;
780 case WID_GL_COLLAPSE_EXPAND_GROUP: // Toggle collapse/expand
781 this->ToogleGroupCollapse(this->vli.index);
782 OnInvalidateData();
783 this->SetDirty();
784 break;
786 case WID_GL_COLLAPSE_ALL_GROUPS: {
787 this->collapsed_groups.Clear();
788 for (const GroupID *gid = this->all_groups.Begin(); gid != this->all_groups.End(); ++gid) {
789 GroupID id = *gid;
790 if (id != ALL_GROUP && id != DEFAULT_GROUP && id != INVALID_GROUP && this->collapsable_groups.Contains(id)) {
791 *this->collapsed_groups.Append() = id;
794 OnInvalidateData();
795 this->SetDirty();
796 break;
799 case WID_GL_EXPAND_ALL_GROUPS: {
800 this->collapsed_groups.Clear();
801 OnInvalidateData();
802 this->SetDirty();
803 break;
805 case WID_GL_AUTO_GROUP: {
806 DoCommandP(0, vli.company, vli.vtype, CMD_AUTO_GROUP_VEHICLES, NULL);
808 this->vehicle_sel = INVALID_VEHICLE;
809 this->group_over = INVALID_GROUP;
810 this->SetDirty();
811 break;
814 case WID_GL_AVAILABLE_VEHICLES:
815 ShowBuildVehicleWindow(INVALID_TILE, this->vli.vtype);
816 break;
818 case WID_GL_MANAGE_VEHICLES_DROPDOWN: {
819 DropDownList *list = this->BuildActionDropdownList(true, Group::IsValidID(this->vli.index), this->vli.vtype == VEH_TRAIN, true);
820 ShowDropDownList(this, list, -1, WID_GL_MANAGE_VEHICLES_DROPDOWN);
821 break;
824 case WID_GL_START_ALL:
825 case WID_GL_STOP_ALL: { // Start/stop all vehicles of the list
826 DoCommandP(0, (1 << 1) | (widget == WID_GL_START_ALL ? (1 << 0) : 0), this->vli.Pack(), CMD_MASS_START_STOP);
827 break;
830 case WID_GL_REPLACE_PROTECTION: {
831 const Group *g = Group::GetIfValid(this->vli.index);
832 if (g != NULL) {
833 DoCommandP(0, this->vli.index, (g->replace_protection ? 0 : 1) | (_ctrl_pressed << 1), CMD_SET_GROUP_REPLACE_PROTECTION);
835 break;
840 void OnDragDrop_Group(Point pt, int widget)
842 const Group *g = Group::Get(this->group_sel);
844 switch (widget) {
845 case WID_GL_ALL_VEHICLES: // All vehicles
846 case WID_GL_DEFAULT_VEHICLES: // Ungroupd vehicles
847 if (g->parent != INVALID_GROUP) {
848 DoCommandP(0, this->group_sel | (1 << 16), INVALID_GROUP, CMD_ALTER_GROUP | CMD_MSG(STR_ERROR_GROUP_CAN_T_SET_PARENT));
851 this->group_sel = INVALID_GROUP;
852 this->group_over = INVALID_GROUP;
853 this->SetDirty();
854 break;
856 case WID_GL_LIST_GROUP: { // Matrix group
857 uint id_g = this->group_sb->GetScrolledRowFromWidget(pt.y, this, WID_GL_LIST_GROUP, 0, this->tiny_step_height);
858 GroupID new_g = id_g >= this->groups.Length() ? INVALID_GROUP : this->groups[id_g]->index;
860 if (this->group_sel != new_g && g->parent != new_g) {
861 DoCommandP(0, this->group_sel | (1 << 16), new_g, CMD_ALTER_GROUP | CMD_MSG(STR_ERROR_GROUP_CAN_T_SET_PARENT));
863 if (!_ctrl_pressed) {
864 GroupID *group = this->collapsed_groups.Find(new_g);
865 if (group != this->collapsed_groups.End()) this->collapsed_groups.Erase(group);
869 this->group_sel = INVALID_GROUP;
870 this->group_over = INVALID_GROUP;
871 this->SetDirty();
872 break;
877 void OnDragDrop_Vehicle(Point pt, int widget)
879 switch (widget) {
880 case WID_GL_DEFAULT_VEHICLES: // Ungrouped vehicles
881 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));
883 this->vehicle_sel = INVALID_VEHICLE;
884 this->group_over = INVALID_GROUP;
886 this->SetDirty();
887 break;
889 case WID_GL_LIST_GROUP: { // Matrix group
890 const VehicleID vindex = this->vehicle_sel;
891 this->vehicle_sel = INVALID_VEHICLE;
892 this->group_over = INVALID_GROUP;
893 this->SetDirty();
895 uint id_g = this->group_sb->GetScrolledRowFromWidget(pt.y, this, WID_GL_LIST_GROUP, 0, this->tiny_step_height);
896 GroupID new_g = id_g >= this->groups.Length() ? NEW_GROUP : this->groups[id_g]->index;
898 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);
899 break;
902 case WID_GL_LIST_VEHICLE: { // Matrix vehicle
903 const VehicleID vindex = this->vehicle_sel;
904 this->vehicle_sel = INVALID_VEHICLE;
905 this->group_over = INVALID_GROUP;
906 this->SetDirty();
908 uint id_v = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_GL_LIST_VEHICLE);
909 if (id_v >= this->vehicles.Length()) return; // click out of list bound
911 const Vehicle *v = this->vehicles[id_v];
912 if (!VehicleClicked(v) && vindex == v->index) {
913 ShowVehicleViewWindow(v);
915 break;
917 case WID_GL_CREATE_GROUP: { // make new group with vehicle specific name and add vehicle
918 const VehicleID vindex = this->vehicle_sel;
919 this->vehicle_sel = INVALID_VEHICLE;
920 this->group_over = INVALID_GROUP;
921 this->SetDirty();
923 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);
925 break;
930 virtual void OnDragDrop(Point pt, int widget)
932 if (this->vehicle_sel != INVALID_VEHICLE) OnDragDrop_Vehicle(pt, widget);
933 if (this->group_sel != INVALID_GROUP) OnDragDrop_Group(pt, widget);
935 _cursor.vehchain = false;
938 virtual void OnQueryTextFinished(char *str)
940 if (str != NULL) DoCommandP(0, this->group_rename, 0, CMD_ALTER_GROUP | CMD_MSG(STR_ERROR_GROUP_CAN_T_RENAME), NULL, str);
941 InvalidateWindowData(WC_TEMPLATEGUI_MAIN, 0, 0, 0);
942 this->group_rename = INVALID_GROUP;
945 virtual void OnResize()
947 this->group_sb->SetCapacityFromWidget(this, WID_GL_LIST_GROUP);
948 this->vscroll->SetCapacityFromWidget(this, WID_GL_LIST_VEHICLE);
951 virtual void OnDropdownSelect(int widget, int index)
953 switch (widget) {
954 case WID_GL_SORT_BY_DROPDOWN:
955 this->vehicles.SetSortType(index);
956 break;
958 case WID_GL_FILTER_BY_CARGO: // Select a cargo filter criteria
959 this->SetCargoFilterIndex(index);
960 break;
962 case WID_GL_MANAGE_VEHICLES_DROPDOWN:
963 assert(this->ShouldShowActionDropdownList());
965 switch (index) {
966 case ADI_SET_ALL_ON_TIME: { // Reset all late timers
967 int num_vehicles = this->vehicles.Length();
969 for (int v_index = 0; v_index < num_vehicles; ++v_index) {
970 DoCommandP(0, this->vehicles[v_index]->index, 0, CMD_SET_VEHICLE_ON_TIME | CMD_MSG(STR_ERROR_CAN_T_TIMETABLE_VEHICLE));
973 break;
975 case ADI_TEMPLATE_REPLACE: // TemplateReplace Window
976 if ( vli.vtype == VEH_TRAIN )
977 ShowTemplateReplaceWindow(this->unitnumber_digits, this->resize.step_height);
978 break;
979 case ADI_REPLACE: // Replace window
980 ShowReplaceGroupVehicleWindow(this->vli.index, this->vli.vtype);
981 break;
982 case ADI_SERVICE: // Send for servicing
983 case ADI_DEPOT: { // Send to Depots
984 DoCommandP(0, DEPOT_MASS_SEND | (index == ADI_SERVICE ? DEPOT_SERVICE : 0U), this->vli.Pack(), GetCmdSendToDepot(this->vli.vtype));
985 break;
988 case ADI_ADD_SHARED: // Add shared Vehicles
989 assert(Group::IsValidID(this->vli.index));
991 DoCommandP(0, this->vli.index, this->vli.vtype, CMD_ADD_SHARED_VEHICLE_GROUP | CMD_MSG(STR_ERROR_GROUP_CAN_T_ADD_SHARED_VEHICLE));
992 break;
993 case ADI_REMOVE_ALL: // Remove all Vehicles from the selected group
994 assert(Group::IsValidID(this->vli.index));
996 DoCommandP(0, this->vli.index, 0, CMD_REMOVE_ALL_VEHICLES_GROUP | CMD_MSG(STR_ERROR_GROUP_CAN_T_REMOVE_ALL_VEHICLES));
997 break;
998 case ADI_SELL_ALL: { // Sell all stopped Vehicles from the selected group
999 /* Only open the confirmation window if there are anything to sell */
1000 bool is_all_group = (this->vli.index == ALL_GROUP);
1001 bool is_default_group = (this->vli.index == DEFAULT_GROUP);
1002 if (is_all_group || is_default_group || Group::Get(this->vli.index)->statistics.num_vehicle != 0) {
1003 SetDParam(0, this->vli.index);
1004 ShowQuery(
1005 STR_GROUP_NAME,
1006 STR_GROUP_SELL_CONFIRMATION_TEXT,
1007 this,
1008 SellAllConfirmationCallback
1011 break;
1014 case ADI_TRACERESTRICT_SLOT_MGMT: {
1015 extern void ShowTraceRestrictSlotWindow(CompanyID company);
1016 ShowTraceRestrictSlotWindow(this->owner);
1017 break;
1019 default: NOT_REACHED();
1021 break;
1023 default: NOT_REACHED();
1026 this->SetDirty();
1029 virtual void OnTick()
1031 if (_pause_mode != PM_UNPAUSED) return;
1032 if (this->groups.NeedResort() || this->vehicles.NeedResort()) {
1033 this->SetDirty();
1037 virtual void OnPlaceObjectAbort()
1039 /* abort drag & drop */
1040 this->vehicle_sel = INVALID_VEHICLE;
1041 this->DirtyHighlightedGroupWidget();
1042 this->group_over = INVALID_GROUP;
1043 this->SetWidgetDirty(WID_GL_LIST_VEHICLE);
1046 virtual void OnMouseDrag(Point pt, int widget)
1048 if (this->vehicle_sel == INVALID_VEHICLE && this->group_sel == INVALID_GROUP) return;
1050 /* A vehicle is dragged over... */
1051 GroupID new_group_over = INVALID_GROUP;
1052 switch (widget) {
1053 case WID_GL_DEFAULT_VEHICLES: // ... the 'default' group.
1054 new_group_over = DEFAULT_GROUP;
1055 break;
1057 case WID_GL_LIST_GROUP: { // ... the list of custom groups.
1058 uint id_g = this->group_sb->GetScrolledRowFromWidget(pt.y, this, WID_GL_LIST_GROUP, 0, this->tiny_step_height);
1059 new_group_over = id_g >= this->groups.Length() ? NEW_GROUP : this->groups[id_g]->index;
1060 break;
1064 /* Do not highlight when dragging over the current group */
1065 if (this->vehicle_sel != INVALID_VEHICLE) {
1066 if (Vehicle::Get(vehicle_sel)->group_id == new_group_over) new_group_over = INVALID_GROUP;
1067 } else if (this->group_sel != INVALID_GROUP) {
1068 if (this->group_sel == new_group_over || Group::Get(this->group_sel)->parent == new_group_over) new_group_over = INVALID_GROUP;
1071 /* Mark widgets as dirty if the group changed. */
1072 if (new_group_over != this->group_over) {
1073 this->DirtyHighlightedGroupWidget();
1074 this->group_over = new_group_over;
1075 this->DirtyHighlightedGroupWidget();
1079 void ShowRenameGroupWindow(GroupID group, bool empty)
1081 assert(Group::IsValidID(group));
1082 this->group_rename = group;
1083 /* Show empty query for new groups */
1084 StringID str = STR_EMPTY;
1085 if (!empty) {
1086 SetDParam(0, group);
1087 str = STR_GROUP_NAME;
1089 ShowQueryString(str, STR_GROUP_RENAME_CAPTION, MAX_LENGTH_GROUP_NAME_CHARS, this, CS_ALPHANUMERAL, QSF_ENABLE_DEFAULT | QSF_LEN_IN_CHARS);
1093 * Tests whether a given vehicle is selected in the window, and unselects it if necessary.
1094 * Called when the vehicle is deleted.
1095 * @param vehicle Vehicle that is going to be deleted
1097 void UnselectVehicle(VehicleID vehicle)
1099 if (this->vehicle_sel == vehicle) ResetObjectToPlace();
1103 static void SellAllConfirmationCallback(Window *win, bool confirmed)
1105 if (confirmed) {
1106 VehicleGroupWindow *w = (VehicleGroupWindow*)win;
1107 DoCommandP(0, w->vli.vtype, w->vli.index, CMD_GROUP_SELL_ALL_VEHICLES);
1111 static WindowDesc _other_group_desc(
1112 WDP_AUTO, "list_groups", 460, 246,
1113 WC_INVALID, WC_NONE,
1115 _nested_group_widgets, lengthof(_nested_group_widgets)
1118 static WindowDesc _train_group_desc(
1119 WDP_AUTO, "list_groups_train", 525, 246,
1120 WC_TRAINS_LIST, WC_NONE,
1122 _nested_group_widgets, lengthof(_nested_group_widgets)
1126 * Show the group window for the given company and vehicle type.
1127 * @param company The company to show the window for.
1128 * @param vehicle_type The type of vehicle to show it for.
1130 void ShowCompanyGroup(CompanyID company, VehicleType vehicle_type)
1132 if (!Company::IsValidID(company)) return;
1134 WindowNumber num = VehicleListIdentifier(VL_GROUP_LIST, vehicle_type, company).Pack();
1135 if (vehicle_type == VEH_TRAIN) {
1136 AllocateWindowDescFront<VehicleGroupWindow>(&_train_group_desc, num);
1137 } else {
1138 _other_group_desc.cls = GetWindowClassForVehicleType(vehicle_type);
1139 AllocateWindowDescFront<VehicleGroupWindow>(&_other_group_desc, num);
1144 * Finds a group list window determined by vehicle type and owner
1145 * @param vt vehicle type
1146 * @param owner owner of groups
1147 * @return pointer to VehicleGroupWindow, NULL if not found
1149 static inline VehicleGroupWindow *FindVehicleGroupWindow(VehicleType vt, Owner owner)
1151 return (VehicleGroupWindow *)FindWindowById(GetWindowClassForVehicleType(vt), VehicleListIdentifier(VL_GROUP_LIST, vt, owner).Pack());
1155 * Opens a 'Rename group' window for newly created group
1156 * @param success did command succeed?
1157 * @param tile unused
1158 * @param p1 vehicle type
1159 * @param p2 unused
1160 * @see CmdCreateGroup
1162 void CcCreateGroup(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
1164 if (result.Failed()) return;
1165 assert(p1 <= VEH_AIRCRAFT);
1167 VehicleGroupWindow *w = FindVehicleGroupWindow((VehicleType)p1, _current_company);
1168 if (w != NULL) w->ShowRenameGroupWindow(_new_group_id, true);
1172 * Open rename window after adding a vehicle to a new group via drag and drop.
1173 * @param success Did command succeed?
1174 * @param tile Unused.
1175 * @param p1 Unused.
1176 * @param p2 Bit 0-19: Vehicle ID.
1178 void CcAddVehicleNewGroup(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
1180 if (result.Failed()) return;
1181 assert(Vehicle::IsValidID(GB(p2, 0, 20)));
1183 CcCreateGroup(result, 0, Vehicle::Get(GB(p2, 0, 20))->type, 0);
1187 * Removes the highlight of a vehicle in a group window
1188 * @param *v Vehicle to remove all highlights from
1190 void DeleteGroupHighlightOfVehicle(const Vehicle *v)
1192 /* If we haven't got any vehicles on the mouse pointer, we haven't got any highlighted in any group windows either
1193 * If that is the case, we can skip looping though the windows and save time
1195 if (_special_mouse_mode != WSM_DRAGDROP) return;
1197 VehicleGroupWindow *w = FindVehicleGroupWindow(v->type, v->owner);
1198 if (w != NULL) w->UnselectVehicle(v->index);