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/>.
10 /** @file group_gui.cpp GUI for the group window. */
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"
29 #include "widgets/group_widget.h"
31 #include "table/sprites.h"
33 typedef GUIList
<const Group
*> GUIGroupList
;
35 static const NWidgetPart _nested_group_widgets
[] = {
36 NWidget(NWID_HORIZONTAL
), // Window header
37 NWidget(WWT_CLOSEBOX
, COLOUR_GREY
),
38 NWidget(WWT_CAPTION
, COLOUR_GREY
, WID_GL_CAPTION
),
39 NWidget(WWT_SHADEBOX
, COLOUR_GREY
),
40 NWidget(WWT_DEFSIZEBOX
, COLOUR_GREY
),
41 NWidget(WWT_STICKYBOX
, COLOUR_GREY
),
43 NWidget(NWID_HORIZONTAL
),
45 NWidget(NWID_VERTICAL
),
46 NWidget(WWT_PANEL
, COLOUR_GREY
), SetMinimalTextLines(1, WD_DROPDOWNTEXT_TOP
+ WD_DROPDOWNTEXT_BOTTOM
), SetFill(1, 0), EndContainer(),
47 NWidget(WWT_PANEL
, COLOUR_GREY
, WID_GL_ALL_VEHICLES
), SetFill(1, 0), EndContainer(),
48 NWidget(WWT_PANEL
, COLOUR_GREY
, WID_GL_DEFAULT_VEHICLES
), SetFill(1, 0), EndContainer(),
49 NWidget(NWID_HORIZONTAL
),
50 NWidget(WWT_MATRIX
, COLOUR_GREY
, WID_GL_LIST_GROUP
), SetMatrixDataTip(1, 0, STR_GROUPS_CLICK_ON_GROUP_FOR_TOOLTIP
),
51 SetFill(1, 0), SetResize(0, 1), SetScrollbar(WID_GL_LIST_GROUP_SCROLLBAR
),
52 NWidget(NWID_VSCROLLBAR
, COLOUR_GREY
, WID_GL_LIST_GROUP_SCROLLBAR
),
54 NWidget(NWID_HORIZONTAL
),
55 NWidget(WWT_PUSHIMGBTN
, COLOUR_GREY
, WID_GL_CREATE_GROUP
), SetFill(0, 1),
56 SetDataTip(SPR_GROUP_CREATE_TRAIN
, STR_GROUP_CREATE_TOOLTIP
),
57 NWidget(WWT_PUSHIMGBTN
, COLOUR_GREY
, WID_GL_DELETE_GROUP
), SetFill(0, 1),
58 SetDataTip(SPR_GROUP_DELETE_TRAIN
, STR_GROUP_DELETE_TOOLTIP
),
59 NWidget(WWT_PUSHIMGBTN
, COLOUR_GREY
, WID_GL_RENAME_GROUP
), SetFill(0, 1),
60 SetDataTip(SPR_GROUP_RENAME_TRAIN
, STR_GROUP_RENAME_TOOLTIP
),
61 NWidget(WWT_PANEL
, COLOUR_GREY
), SetFill(1, 1), EndContainer(),
62 NWidget(WWT_PUSHIMGBTN
, COLOUR_GREY
, WID_GL_REPLACE_PROTECTION
), SetFill(0, 1),
63 SetDataTip(SPR_GROUP_REPLACE_OFF_TRAIN
, STR_GROUP_REPLACE_PROTECTION_TOOLTIP
),
67 NWidget(NWID_VERTICAL
),
68 NWidget(NWID_HORIZONTAL
),
69 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_GL_SORT_BY_ORDER
), SetMinimalSize(81, 12), SetDataTip(STR_BUTTON_SORT_BY
, STR_TOOLTIP_SORT_ORDER
),
70 NWidget(WWT_DROPDOWN
, COLOUR_GREY
, WID_GL_SORT_BY_DROPDOWN
), SetMinimalSize(167, 12), SetDataTip(0x0, STR_TOOLTIP_SORT_CRITERIA
),
71 NWidget(WWT_PANEL
, COLOUR_GREY
), SetMinimalSize(12, 12), SetResize(1, 0), EndContainer(),
73 NWidget(NWID_HORIZONTAL
),
74 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
),
75 NWidget(NWID_VSCROLLBAR
, COLOUR_GREY
, WID_GL_LIST_VEHICLE_SCROLLBAR
),
77 NWidget(WWT_PANEL
, COLOUR_GREY
), SetMinimalSize(1, 0), SetFill(1, 1), SetResize(1, 0), EndContainer(),
78 NWidget(NWID_HORIZONTAL
),
79 NWidget(WWT_PUSHTXTBTN
, COLOUR_GREY
, WID_GL_AVAILABLE_VEHICLES
), SetMinimalSize(106, 12), SetFill(0, 1),
80 SetDataTip(STR_BLACK_STRING
, STR_VEHICLE_LIST_AVAILABLE_ENGINES_TOOLTIP
),
81 NWidget(WWT_PANEL
, COLOUR_GREY
), SetMinimalSize(0, 12), SetFill(1, 1), SetResize(1, 0), EndContainer(),
82 NWidget(WWT_DROPDOWN
, COLOUR_GREY
, WID_GL_MANAGE_VEHICLES_DROPDOWN
), SetMinimalSize(118, 12), SetFill(0, 1),
83 SetDataTip(STR_VEHICLE_LIST_MANAGE_LIST
, STR_VEHICLE_LIST_MANAGE_LIST_TOOLTIP
),
84 NWidget(WWT_PUSHIMGBTN
, COLOUR_GREY
, WID_GL_STOP_ALL
), SetMinimalSize(12, 12), SetFill(0, 1),
85 SetDataTip(SPR_FLAG_VEH_STOPPED
, STR_VEHICLE_LIST_MASS_STOP_LIST_TOOLTIP
),
86 NWidget(WWT_PUSHIMGBTN
, COLOUR_GREY
, WID_GL_START_ALL
), SetMinimalSize(12, 12), SetFill(0, 1),
87 SetDataTip(SPR_FLAG_VEH_RUNNING
, STR_VEHICLE_LIST_MASS_START_LIST_TOOLTIP
),
88 NWidget(WWT_RESIZEBOX
, COLOUR_GREY
),
94 class VehicleGroupWindow
: public BaseVehicleListWindow
{
96 /* Columns in the group list */
98 VGC_NAME
, ///< Group name.
99 VGC_PROTECT
, ///< Autoreplace protect icon.
100 VGC_AUTOREPLACE
, ///< Autoreplace active icon.
101 VGC_PROFIT
, ///< Profit icon.
102 VGC_NUMBER
, ///< Number of vehicles in the group.
107 VehicleID vehicle_sel
; ///< Selected vehicle
108 GroupID group_rename
; ///< Group being renamed, INVALID_GROUP if none
109 GroupID group_over
; ///< Group over which a vehicle is dragged, INVALID_GROUP if none
110 GUIGroupList groups
; ///< List of groups
111 uint tiny_step_height
; ///< Step height for the group list
114 Dimension column_size
[VGC_END
]; ///< Size of the columns in the group list.
117 * (Re)Build the group list.
119 * @param owner The owner of the window
121 void BuildGroupList(Owner owner
)
123 if (!this->groups
.NeedRebuild()) return;
125 this->groups
.Clear();
129 if (g
->owner
== owner
&& g
->vehicle_type
== this->vli
.vtype
) {
130 *this->groups
.Append() = g
;
134 this->groups
.Compact();
135 this->groups
.RebuildDone();
138 /** Sort the groups by their name */
139 static int CDECL
GroupNameSorter(const Group
* const *a
, const Group
* const *b
)
141 static const Group
*last_group
[2] = { NULL
, NULL
};
142 static char last_name
[2][64] = { "", "" };
144 if (*a
!= last_group
[0]) {
146 SetDParam(0, (*a
)->index
);
147 GetString(last_name
[0], STR_GROUP_NAME
, lastof(last_name
[0]));
150 if (*b
!= last_group
[1]) {
152 SetDParam(0, (*b
)->index
);
153 GetString(last_name
[1], STR_GROUP_NAME
, lastof(last_name
[1]));
156 int r
= strnatcmp(last_name
[0], last_name
[1]); // Sort by name (natural sorting).
157 if (r
== 0) return (*a
)->index
- (*b
)->index
;
162 * Compute tiny_step_height and column_size
163 * @return Total width required for the group list.
165 uint
ComputeGroupInfoSize()
167 this->column_size
[VGC_NAME
] = maxdim(GetStringBoundingBox(STR_GROUP_DEFAULT_TRAINS
+ this->vli
.vtype
), GetStringBoundingBox(STR_GROUP_ALL_TRAINS
+ this->vli
.vtype
));
168 this->column_size
[VGC_NAME
].width
= max(170u, this->column_size
[VGC_NAME
].width
);
169 this->tiny_step_height
= this->column_size
[VGC_NAME
].height
;
171 this->column_size
[VGC_PROTECT
] = GetSpriteSize(SPR_GROUP_REPLACE_PROTECT
);
172 this->tiny_step_height
= max(this->tiny_step_height
, this->column_size
[VGC_PROTECT
].height
);
174 this->column_size
[VGC_AUTOREPLACE
] = GetSpriteSize(SPR_GROUP_REPLACE_ACTIVE
);
175 this->tiny_step_height
= max(this->tiny_step_height
, this->column_size
[VGC_AUTOREPLACE
].height
);
177 this->column_size
[VGC_PROFIT
].width
= 0;
178 this->column_size
[VGC_PROFIT
].height
= 0;
179 static const SpriteID profit_sprites
[] = {SPR_PROFIT_NA
, SPR_PROFIT_NEGATIVE
, SPR_PROFIT_SOME
, SPR_PROFIT_LOT
};
180 for (uint i
= 0; i
< lengthof(profit_sprites
); i
++) {
181 Dimension d
= GetSpriteSize(profit_sprites
[i
]);
182 this->column_size
[VGC_PROFIT
] = maxdim(this->column_size
[VGC_PROFIT
], d
);
184 this->tiny_step_height
= max(this->tiny_step_height
, this->column_size
[VGC_PROFIT
].height
);
186 SetDParamMaxValue(0, GroupStatistics::Get(this->vli
.company
, ALL_GROUP
, this->vli
.vtype
).num_vehicle
, 3, FS_SMALL
);
187 this->column_size
[VGC_NUMBER
] = GetStringBoundingBox(STR_TINY_COMMA
);
188 this->tiny_step_height
= max(this->tiny_step_height
, this->column_size
[VGC_NUMBER
].height
);
190 this->tiny_step_height
+= WD_MATRIX_TOP
;
192 return WD_FRAMERECT_LEFT
+ 8 +
193 this->column_size
[VGC_NAME
].width
+ 8 +
194 this->column_size
[VGC_PROTECT
].width
+ 2 +
195 this->column_size
[VGC_AUTOREPLACE
].width
+ 2 +
196 this->column_size
[VGC_PROFIT
].width
+ 2 +
197 this->column_size
[VGC_NUMBER
].width
+ 2 +
202 * Draw a row in the group list.
203 * @param y Top of the row.
204 * @param left Left of the row.
205 * @param right Right of the row.
206 * @param g_id Group to list.
207 * @param protection Whether autoreplace protection is set.
209 void DrawGroupInfo(int y
, int left
, int right
, GroupID g_id
, bool protection
= false) const
211 /* Highlight the group if a vehicle is dragged over it */
212 if (g_id
== this->group_over
) {
213 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]);
216 if (g_id
== NEW_GROUP
) return;
218 /* draw the selected group in white, else we draw it in black */
219 TextColour colour
= g_id
== this->vli
.index
? TC_WHITE
: TC_BLACK
;
220 const GroupStatistics
&stats
= GroupStatistics::Get(this->vli
.company
, g_id
, this->vli
.vtype
);
221 bool rtl
= _current_text_dir
== TD_RTL
;
223 /* draw group name */
225 if (IsAllGroupID(g_id
)) {
226 str
= STR_GROUP_ALL_TRAINS
+ this->vli
.vtype
;
227 } else if (IsDefaultGroupID(g_id
)) {
228 str
= STR_GROUP_DEFAULT_TRAINS
+ this->vli
.vtype
;
231 str
= STR_GROUP_NAME
;
233 int x
= rtl
? right
- WD_FRAMERECT_RIGHT
- 8 - this->column_size
[VGC_NAME
].width
+ 1 : left
+ WD_FRAMERECT_LEFT
+ 8;
234 DrawString(x
, x
+ this->column_size
[VGC_NAME
].width
- 1, y
+ (this->tiny_step_height
- this->column_size
[VGC_NAME
].height
) / 2, str
, colour
);
236 /* draw autoreplace protection */
237 x
= rtl
? x
- 8 - this->column_size
[VGC_PROTECT
].width
: x
+ 8 + this->column_size
[VGC_NAME
].width
;
238 if (protection
) DrawSprite(SPR_GROUP_REPLACE_PROTECT
, PAL_NONE
, x
, y
+ (this->tiny_step_height
- this->column_size
[VGC_PROTECT
].height
) / 2);
240 /* draw autoreplace status */
241 x
= rtl
? x
- 2 - this->column_size
[VGC_AUTOREPLACE
].width
: x
+ 2 + this->column_size
[VGC_PROTECT
].width
;
242 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);
244 /* draw the profit icon */
245 x
= rtl
? x
- 2 - this->column_size
[VGC_PROFIT
].width
: x
+ 2 + this->column_size
[VGC_AUTOREPLACE
].width
;
247 if (stats
.num_profit_vehicle
== 0) {
249 } else if (stats
.profit_last_year
< 0) {
250 spr
= SPR_PROFIT_NEGATIVE
;
251 } else if (stats
.profit_last_year
< 10000 * stats
.num_profit_vehicle
) { // TODO magic number
252 spr
= SPR_PROFIT_SOME
;
254 spr
= SPR_PROFIT_LOT
;
256 DrawSprite(spr
, PAL_NONE
, x
, y
+ (this->tiny_step_height
- this->column_size
[VGC_PROFIT
].height
) / 2);
258 /* draw the number of vehicles of the group */
259 x
= rtl
? x
- 2 - this->column_size
[VGC_NUMBER
].width
: x
+ 2 + this->column_size
[VGC_PROFIT
].width
;
260 SetDParam(0, stats
.num_vehicle
);
261 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
);
265 * Mark the widget containing the currently highlighted group as dirty.
267 void DirtyHighlightedGroupWidget()
269 if (this->group_over
== INVALID_GROUP
) return;
271 if (IsAllGroupID(this->group_over
)) {
272 this->SetWidgetDirty(WID_GL_ALL_VEHICLES
);
273 } else if (IsDefaultGroupID(this->group_over
)) {
274 this->SetWidgetDirty(WID_GL_DEFAULT_VEHICLES
);
276 this->SetWidgetDirty(WID_GL_LIST_GROUP
);
281 VehicleGroupWindow(WindowDesc
*desc
, WindowNumber window_number
) : BaseVehicleListWindow(desc
, window_number
)
283 this->CreateNestedTree();
285 this->vscroll
= this->GetScrollbar(WID_GL_LIST_VEHICLE_SCROLLBAR
);
286 this->group_sb
= this->GetScrollbar(WID_GL_LIST_GROUP_SCROLLBAR
);
288 switch (this->vli
.vtype
) {
289 default: NOT_REACHED();
290 case VEH_TRAIN
: this->sorting
= &_sorting
.train
; break;
291 case VEH_ROAD
: this->sorting
= &_sorting
.roadveh
; break;
292 case VEH_SHIP
: this->sorting
= &_sorting
.ship
; break;
293 case VEH_AIRCRAFT
: this->sorting
= &_sorting
.aircraft
; break;
296 this->vli
.index
= ALL_GROUP
;
297 this->vehicle_sel
= INVALID_VEHICLE
;
298 this->group_rename
= INVALID_GROUP
;
299 this->group_over
= INVALID_GROUP
;
301 this->vehicles
.SetListing(*this->sorting
);
302 this->vehicles
.ForceRebuild();
303 this->vehicles
.NeedResort();
305 this->BuildVehicleList();
306 this->SortVehicleList();
308 this->groups
.ForceRebuild();
309 this->groups
.NeedResort();
310 this->BuildGroupList(vli
.company
);
311 this->groups
.Sort(&GroupNameSorter
);
313 this->GetWidget
<NWidgetCore
>(WID_GL_CAPTION
)->widget_data
= STR_VEHICLE_LIST_TRAIN_CAPTION
+ this->vli
.vtype
;
314 this->GetWidget
<NWidgetCore
>(WID_GL_LIST_VEHICLE
)->tool_tip
= STR_VEHICLE_LIST_TRAIN_LIST_TOOLTIP
+ this->vli
.vtype
;
316 this->GetWidget
<NWidgetCore
>(WID_GL_CREATE_GROUP
)->widget_data
+= this->vli
.vtype
;
317 this->GetWidget
<NWidgetCore
>(WID_GL_RENAME_GROUP
)->widget_data
+= this->vli
.vtype
;
318 this->GetWidget
<NWidgetCore
>(WID_GL_DELETE_GROUP
)->widget_data
+= this->vli
.vtype
;
319 this->GetWidget
<NWidgetCore
>(WID_GL_REPLACE_PROTECTION
)->widget_data
+= this->vli
.vtype
;
321 this->FinishInitNested(window_number
);
322 this->owner
= vli
.company
;
325 ~VehicleGroupWindow()
327 *this->sorting
= this->vehicles
.GetListing();
330 virtual void UpdateWidgetSize(int widget
, Dimension
*size
, const Dimension
&padding
, Dimension
*fill
, Dimension
*resize
)
333 case WID_GL_LIST_GROUP
: {
334 size
->width
= this->ComputeGroupInfoSize();
335 resize
->height
= this->tiny_step_height
;
337 /* Minimum height is the height of the list widget minus all and default vehicles... */
338 size
->height
= 4 * GetVehicleListHeight(this->vli
.vtype
, this->tiny_step_height
) - 2 * this->tiny_step_height
;
340 /* ... minus the buttons at the bottom ... */
341 uint max_icon_height
= GetSpriteSize(this->GetWidget
<NWidgetCore
>(WID_GL_CREATE_GROUP
)->widget_data
).height
;
342 max_icon_height
= max(max_icon_height
, GetSpriteSize(this->GetWidget
<NWidgetCore
>(WID_GL_RENAME_GROUP
)->widget_data
).height
);
343 max_icon_height
= max(max_icon_height
, GetSpriteSize(this->GetWidget
<NWidgetCore
>(WID_GL_DELETE_GROUP
)->widget_data
).height
);
344 max_icon_height
= max(max_icon_height
, GetSpriteSize(this->GetWidget
<NWidgetCore
>(WID_GL_REPLACE_PROTECTION
)->widget_data
).height
);
346 /* Get a multiple of tiny_step_height of that amount */
347 size
->height
= Ceil(size
->height
- max_icon_height
, tiny_step_height
);
351 case WID_GL_ALL_VEHICLES
:
352 case WID_GL_DEFAULT_VEHICLES
:
353 size
->width
= this->ComputeGroupInfoSize();
354 size
->height
= this->tiny_step_height
;
357 case WID_GL_SORT_BY_ORDER
: {
358 Dimension d
= GetStringBoundingBox(this->GetWidget
<NWidgetCore
>(widget
)->widget_data
);
359 d
.width
+= padding
.width
+ WD_SORTBUTTON_ARROW_WIDTH
* 2; // Doubled since the string is centred and it also looks better.
360 d
.height
+= padding
.height
;
361 *size
= maxdim(*size
, d
);
365 case WID_GL_LIST_VEHICLE
:
366 this->ComputeGroupInfoSize();
367 resize
->height
= GetVehicleListHeight(this->vli
.vtype
, this->tiny_step_height
);
368 size
->height
= 4 * resize
->height
;
371 case WID_GL_MANAGE_VEHICLES_DROPDOWN
: {
372 Dimension d
= this->GetActionDropdownSize(true, true);
373 d
.height
+= padding
.height
;
374 d
.width
+= padding
.width
;
375 *size
= maxdim(*size
, d
);
382 * Some data on this window has become invalid.
383 * @param data Information about the changed data.
384 * @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.
386 virtual void OnInvalidateData(int data
= 0, bool gui_scope
= true)
389 /* This needs to be done in command-scope to enforce rebuilding before resorting invalid data */
390 this->vehicles
.ForceRebuild();
391 this->groups
.ForceRebuild();
393 this->vehicles
.ForceResort();
394 this->groups
.ForceResort();
397 /* Process ID-invalidation in command-scope as well */
398 if (this->group_rename
!= INVALID_GROUP
&& !Group::IsValidID(this->group_rename
)) {
399 DeleteWindowByClass(WC_QUERY_STRING
);
400 this->group_rename
= INVALID_GROUP
;
403 if (!(IsAllGroupID(this->vli
.index
) || IsDefaultGroupID(this->vli
.index
) || Group::IsValidID(this->vli
.index
))) {
404 this->vli
.index
= ALL_GROUP
;
405 HideDropDownMenu(this);
410 virtual void SetStringParameters(int widget
) const
413 case WID_GL_AVAILABLE_VEHICLES
:
414 SetDParam(0, STR_VEHICLE_LIST_AVAILABLE_TRAINS
+ this->vli
.vtype
);
418 /* If selected_group == DEFAULT_GROUP || ALL_GROUP, draw the standard caption
419 * We list all vehicles or ungrouped vehicles */
420 if (IsDefaultGroupID(this->vli
.index
) || IsAllGroupID(this->vli
.index
)) {
421 SetDParam(0, STR_COMPANY_NAME
);
422 SetDParam(1, this->vli
.company
);
423 SetDParam(2, this->vehicles
.Length());
424 SetDParam(3, this->vehicles
.Length());
426 const Group
*g
= Group::Get(this->vli
.index
);
428 SetDParam(0, STR_GROUP_NAME
);
429 SetDParam(1, g
->index
);
430 SetDParam(2, g
->statistics
.num_vehicle
);
431 SetDParam(3, g
->statistics
.num_vehicle
);
437 virtual void OnPaint()
439 /* If we select the all vehicles, this->list will contain all vehicles of the owner
440 * else this->list will contain all vehicles which belong to the selected group */
441 this->BuildVehicleList();
442 this->SortVehicleList();
444 this->BuildGroupList(this->owner
);
445 this->groups
.Sort(&GroupNameSorter
);
447 this->group_sb
->SetCount(this->groups
.Length());
448 this->vscroll
->SetCount(this->vehicles
.Length());
450 /* The drop down menu is out, *but* it may not be used, retract it. */
451 if (this->vehicles
.Length() == 0 && this->IsWidgetLowered(WID_GL_MANAGE_VEHICLES_DROPDOWN
)) {
452 this->RaiseWidget(WID_GL_MANAGE_VEHICLES_DROPDOWN
);
453 HideDropDownMenu(this);
456 /* Disable all lists management button when the list is empty */
457 this->SetWidgetsDisabledState(this->vehicles
.Length() == 0 || _local_company
!= this->vli
.company
,
460 WID_GL_MANAGE_VEHICLES_DROPDOWN
,
463 /* Disable the group specific function when we select the default group or all vehicles */
464 this->SetWidgetsDisabledState(IsDefaultGroupID(this->vli
.index
) || IsAllGroupID(this->vli
.index
) || _local_company
!= this->vli
.company
,
467 WID_GL_REPLACE_PROTECTION
,
470 /* Disable remaining buttons for non-local companies
471 * Needed while changing _local_company, eg. by cheats
472 * All procedures (eg. move vehicle to another group)
473 * verify, whether you are the owner of the vehicle,
474 * so it doesn't have to be disabled
476 this->SetWidgetsDisabledState(_local_company
!= this->vli
.company
,
478 WID_GL_AVAILABLE_VEHICLES
,
481 /* If not a default group and the group has replace protection, show an enabled replace sprite. */
482 uint16 protect_sprite
= SPR_GROUP_REPLACE_OFF_TRAIN
;
483 if (!IsDefaultGroupID(this->vli
.index
) && !IsAllGroupID(this->vli
.index
) && Group::Get(this->vli
.index
)->replace_protection
) protect_sprite
= SPR_GROUP_REPLACE_ON_TRAIN
;
484 this->GetWidget
<NWidgetCore
>(WID_GL_REPLACE_PROTECTION
)->widget_data
= protect_sprite
+ this->vli
.vtype
;
486 /* Set text of sort by dropdown */
487 this->GetWidget
<NWidgetCore
>(WID_GL_SORT_BY_DROPDOWN
)->widget_data
= this->vehicle_sorter_names
[this->vehicles
.SortType()];
492 virtual void DrawWidget(const Rect
&r
, int widget
) const
495 case WID_GL_ALL_VEHICLES
:
496 DrawGroupInfo(r
.top
+ WD_FRAMERECT_TOP
, r
.left
, r
.right
, ALL_GROUP
);
499 case WID_GL_DEFAULT_VEHICLES
:
500 DrawGroupInfo(r
.top
+ WD_FRAMERECT_TOP
, r
.left
, r
.right
, DEFAULT_GROUP
);
503 case WID_GL_LIST_GROUP
: {
504 int y1
= r
.top
+ WD_FRAMERECT_TOP
;
505 int max
= min(this->group_sb
->GetPosition() + this->group_sb
->GetCapacity(), this->groups
.Length());
506 for (int i
= this->group_sb
->GetPosition(); i
< max
; ++i
) {
507 const Group
*g
= this->groups
[i
];
509 assert(g
->owner
== this->owner
);
511 DrawGroupInfo(y1
, r
.left
, r
.right
, g
->index
, g
->replace_protection
);
513 y1
+= this->tiny_step_height
;
515 if ((uint
)this->group_sb
->GetPosition() + this->group_sb
->GetCapacity() > this->groups
.Length()) {
516 DrawGroupInfo(y1
, r
.left
, r
.right
, NEW_GROUP
);
521 case WID_GL_SORT_BY_ORDER
:
522 this->DrawSortButtonState(WID_GL_SORT_BY_ORDER
, this->vehicles
.IsDescSortOrder() ? SBS_DOWN
: SBS_UP
);
525 case WID_GL_LIST_VEHICLE
:
526 this->DrawVehicleListItems(this->vehicle_sel
, this->resize
.step_height
, r
);
531 virtual void OnClick(Point pt
, int widget
, int click_count
)
534 case WID_GL_SORT_BY_ORDER
: // Flip sorting method ascending/descending
535 this->vehicles
.ToggleSortOrder();
539 case WID_GL_SORT_BY_DROPDOWN
: // Select sorting criteria dropdown menu
540 ShowDropDownMenu(this, this->vehicle_sorter_names
, this->vehicles
.SortType(), WID_GL_SORT_BY_DROPDOWN
, 0, (this->vli
.vtype
== VEH_TRAIN
|| this->vli
.vtype
== VEH_ROAD
) ? 0 : (1 << 10));
543 case WID_GL_ALL_VEHICLES
: // All vehicles button
544 if (!IsAllGroupID(this->vli
.index
)) {
545 this->vli
.index
= ALL_GROUP
;
546 this->vehicles
.ForceRebuild();
551 case WID_GL_DEFAULT_VEHICLES
: // Ungrouped vehicles button
552 if (!IsDefaultGroupID(this->vli
.index
)) {
553 this->vli
.index
= DEFAULT_GROUP
;
554 this->vehicles
.ForceRebuild();
559 case WID_GL_LIST_GROUP
: { // Matrix Group
560 uint id_g
= this->group_sb
->GetScrolledRowFromWidget(pt
.y
, this, WID_GL_LIST_GROUP
, 0, this->tiny_step_height
);
561 if (id_g
>= this->groups
.Length()) return;
563 this->vli
.index
= this->groups
[id_g
]->index
;
565 this->vehicles
.ForceRebuild();
570 case WID_GL_LIST_VEHICLE
: { // Matrix Vehicle
571 uint id_v
= this->vscroll
->GetScrolledRowFromWidget(pt
.y
, this, WID_GL_LIST_VEHICLE
);
572 if (id_v
>= this->vehicles
.Length()) return; // click out of list bound
574 const Vehicle
*v
= this->vehicles
[id_v
];
575 if (VehicleClicked(v
)) break;
577 this->vehicle_sel
= v
->index
;
579 int image
= v
->GetImage(_current_text_dir
== TD_RTL
? DIR_E
: DIR_W
, EIT_IN_LIST
);
580 SetObjectToPlaceWnd(image
, GetVehiclePalette(v
), HT_DRAG
, this);
581 _cursor
.vehchain
= true;
587 case WID_GL_CREATE_GROUP
: { // Create a new group
588 DoCommandP(0, this->vli
.vtype
, 0, CMD_CREATE_GROUP
| CMD_MSG(STR_ERROR_GROUP_CAN_T_CREATE
), CcCreateGroup
);
592 case WID_GL_DELETE_GROUP
: { // Delete the selected group
593 GroupID group
= this->vli
.index
;
594 this->vli
.index
= ALL_GROUP
;
596 DoCommandP(0, group
, 0, CMD_DELETE_GROUP
| CMD_MSG(STR_ERROR_GROUP_CAN_T_DELETE
));
600 case WID_GL_RENAME_GROUP
: // Rename the selected roup
601 this->ShowRenameGroupWindow(this->vli
.index
, false);
604 case WID_GL_AVAILABLE_VEHICLES
:
605 ShowBuildVehicleWindow(INVALID_TILE
, this->vli
.vtype
);
608 case WID_GL_MANAGE_VEHICLES_DROPDOWN
: {
609 DropDownList
*list
= this->BuildActionDropdownList(true, Group::IsValidID(this->vli
.index
));
610 ShowDropDownList(this, list
, 0, WID_GL_MANAGE_VEHICLES_DROPDOWN
);
614 case WID_GL_START_ALL
:
615 case WID_GL_STOP_ALL
: { // Start/stop all vehicles of the list
616 DoCommandP(0, (1 << 1) | (widget
== WID_GL_START_ALL
? (1 << 0) : 0), this->vli
.Pack(), CMD_MASS_START_STOP
);
620 case WID_GL_REPLACE_PROTECTION
: {
621 const Group
*g
= Group::GetIfValid(this->vli
.index
);
623 DoCommandP(0, this->vli
.index
, !g
->replace_protection
, CMD_SET_GROUP_REPLACE_PROTECTION
);
630 virtual void OnDragDrop(Point pt
, int widget
)
633 case WID_GL_DEFAULT_VEHICLES
: // Ungrouped vehicles
634 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
));
636 this->vehicle_sel
= INVALID_VEHICLE
;
637 this->group_over
= INVALID_GROUP
;
642 case WID_GL_LIST_GROUP
: { // Matrix group
643 const VehicleID vindex
= this->vehicle_sel
;
644 this->vehicle_sel
= INVALID_VEHICLE
;
645 this->group_over
= INVALID_GROUP
;
648 uint id_g
= this->group_sb
->GetScrolledRowFromWidget(pt
.y
, this, WID_GL_LIST_GROUP
, 0, this->tiny_step_height
);
649 GroupID new_g
= id_g
>= this->groups
.Length() ? NEW_GROUP
: this->groups
[id_g
]->index
;
651 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
);
655 case WID_GL_LIST_VEHICLE
: { // Matrix vehicle
656 const VehicleID vindex
= this->vehicle_sel
;
657 this->vehicle_sel
= INVALID_VEHICLE
;
658 this->group_over
= INVALID_GROUP
;
661 uint id_v
= this->vscroll
->GetScrolledRowFromWidget(pt
.y
, this, WID_GL_LIST_VEHICLE
);
662 if (id_v
>= this->vehicles
.Length()) return; // click out of list bound
664 const Vehicle
*v
= this->vehicles
[id_v
];
665 if (!VehicleClicked(v
) && vindex
== v
->index
) {
666 ShowVehicleViewWindow(v
);
671 _cursor
.vehchain
= false;
674 virtual void OnQueryTextFinished(char *str
)
676 if (str
!= NULL
) DoCommandP(0, this->group_rename
, 0, CMD_RENAME_GROUP
| CMD_MSG(STR_ERROR_GROUP_CAN_T_RENAME
), NULL
, str
);
677 this->group_rename
= INVALID_GROUP
;
680 virtual void OnResize()
682 this->group_sb
->SetCapacityFromWidget(this, WID_GL_LIST_GROUP
);
683 this->vscroll
->SetCapacityFromWidget(this, WID_GL_LIST_VEHICLE
);
686 virtual void OnDropdownSelect(int widget
, int index
)
689 case WID_GL_SORT_BY_DROPDOWN
:
690 this->vehicles
.SetSortType(index
);
693 case WID_GL_MANAGE_VEHICLES_DROPDOWN
:
694 assert(this->vehicles
.Length() != 0);
697 case ADI_REPLACE
: // Replace window
698 ShowReplaceGroupVehicleWindow(this->vli
.index
, this->vli
.vtype
);
700 case ADI_SERVICE
: // Send for servicing
701 case ADI_DEPOT
: { // Send to Depots
702 DoCommandP(0, DEPOT_MASS_SEND
| (index
== ADI_SERVICE
? DEPOT_SERVICE
: 0U), this->vli
.Pack(), GetCmdSendToDepot(this->vli
.vtype
));
706 case ADI_ADD_SHARED
: // Add shared Vehicles
707 assert(Group::IsValidID(this->vli
.index
));
709 DoCommandP(0, this->vli
.index
, this->vli
.vtype
, CMD_ADD_SHARED_VEHICLE_GROUP
| CMD_MSG(STR_ERROR_GROUP_CAN_T_ADD_SHARED_VEHICLE
));
711 case ADI_REMOVE_ALL
: // Remove all Vehicles from the selected group
712 assert(Group::IsValidID(this->vli
.index
));
714 DoCommandP(0, this->vli
.index
, 0, CMD_REMOVE_ALL_VEHICLES_GROUP
| CMD_MSG(STR_ERROR_GROUP_CAN_T_REMOVE_ALL_VEHICLES
));
716 default: NOT_REACHED();
720 default: NOT_REACHED();
726 virtual void OnTick()
728 if (_pause_mode
!= PM_UNPAUSED
) return;
729 if (this->groups
.NeedResort() || this->vehicles
.NeedResort()) {
734 virtual void OnPlaceObjectAbort()
736 /* abort drag & drop */
737 this->vehicle_sel
= INVALID_VEHICLE
;
738 this->DirtyHighlightedGroupWidget();
739 this->group_over
= INVALID_GROUP
;
740 this->SetWidgetDirty(WID_GL_LIST_VEHICLE
);
743 virtual void OnMouseDrag(Point pt
, int widget
)
745 if (this->vehicle_sel
== INVALID_VEHICLE
) return;
747 /* A vehicle is dragged over... */
748 GroupID new_group_over
= INVALID_GROUP
;
750 case WID_GL_DEFAULT_VEHICLES
: // ... the 'default' group.
751 new_group_over
= DEFAULT_GROUP
;
754 case WID_GL_LIST_GROUP
: { // ... the list of custom groups.
755 uint id_g
= this->group_sb
->GetScrolledRowFromWidget(pt
.y
, this, WID_GL_LIST_GROUP
, 0, this->tiny_step_height
);
756 new_group_over
= id_g
>= this->groups
.Length() ? NEW_GROUP
: this->groups
[id_g
]->index
;
761 /* Do not highlight when dragging over the current group */
762 if (Vehicle::Get(vehicle_sel
)->group_id
== new_group_over
) new_group_over
= INVALID_GROUP
;
764 /* Mark widgets as dirty if the group changed. */
765 if (new_group_over
!= this->group_over
) {
766 this->DirtyHighlightedGroupWidget();
767 this->group_over
= new_group_over
;
768 this->DirtyHighlightedGroupWidget();
772 void ShowRenameGroupWindow(GroupID group
, bool empty
)
774 assert(Group::IsValidID(group
));
775 this->group_rename
= group
;
776 /* Show empty query for new groups */
777 StringID str
= STR_EMPTY
;
780 str
= STR_GROUP_NAME
;
782 ShowQueryString(str
, STR_GROUP_RENAME_CAPTION
, MAX_LENGTH_GROUP_NAME_CHARS
, this, CS_ALPHANUMERAL
, QSF_ENABLE_DEFAULT
| QSF_LEN_IN_CHARS
);
786 * Tests whether a given vehicle is selected in the window, and unselects it if necessary.
787 * Called when the vehicle is deleted.
788 * @param vehicle Vehicle that is going to be deleted
790 void UnselectVehicle(VehicleID vehicle
)
792 if (this->vehicle_sel
== vehicle
) ResetObjectToPlace();
797 static WindowDesc
_other_group_desc(
798 WDP_AUTO
, "list_groups", 460, 246,
801 _nested_group_widgets
, lengthof(_nested_group_widgets
)
804 static WindowDesc
_train_group_desc(
805 WDP_AUTO
, "list_groups_train", 525, 246,
806 WC_TRAINS_LIST
, WC_NONE
,
808 _nested_group_widgets
, lengthof(_nested_group_widgets
)
812 * Show the group window for the given company and vehicle type.
813 * @param company The company to show the window for.
814 * @param vehicle_type The type of vehicle to show it for.
816 void ShowCompanyGroup(CompanyID company
, VehicleType vehicle_type
)
818 if (!Company::IsValidID(company
)) return;
820 WindowNumber num
= VehicleListIdentifier(VL_GROUP_LIST
, vehicle_type
, company
).Pack();
821 if (vehicle_type
== VEH_TRAIN
) {
822 AllocateWindowDescFront
<VehicleGroupWindow
>(&_train_group_desc
, num
);
824 _other_group_desc
.cls
= GetWindowClassForVehicleType(vehicle_type
);
825 AllocateWindowDescFront
<VehicleGroupWindow
>(&_other_group_desc
, num
);
830 * Finds a group list window determined by vehicle type and owner
831 * @param vt vehicle type
832 * @param owner owner of groups
833 * @return pointer to VehicleGroupWindow, NULL if not found
835 static inline VehicleGroupWindow
*FindVehicleGroupWindow(VehicleType vt
, Owner owner
)
837 return (VehicleGroupWindow
*)FindWindowById(GetWindowClassForVehicleType(vt
), VehicleListIdentifier(VL_GROUP_LIST
, vt
, owner
).Pack());
841 * Opens a 'Rename group' window for a newly created group
842 * @param type the vehicle type to open the window for
844 static void ShowRenameNewGroupWindow(VehicleType type
)
846 VehicleGroupWindow
*w
= FindVehicleGroupWindow(type
, _current_company
);
847 if (w
!= NULL
) w
->ShowRenameGroupWindow(_new_group_id
, true);
851 * Opens a 'Rename group' window for newly created group
852 * @param success did command succeed?
854 * @param p1 vehicle type
856 * @see CmdCreateGroup
858 void CcCreateGroup(const CommandCost
&result
, TileIndex tile
, uint32 p1
, uint32 p2
)
860 if (result
.Failed()) return;
861 assert(p1
<= VEH_AIRCRAFT
);
863 ShowRenameNewGroupWindow((VehicleType
)p1
);
867 * Open rename window after adding a vehicle to a new group via drag and drop.
868 * @param success Did command succeed?
869 * @param tile Unused.
871 * @param p2 Bit 0-19: Vehicle ID.
873 void CcAddVehicleNewGroup(const CommandCost
&result
, TileIndex tile
, uint32 p1
, uint32 p2
)
875 if (result
.Failed()) return;
876 assert(Vehicle::IsValidID(GB(p2
, 0, 20)));
878 ShowRenameNewGroupWindow(Vehicle::Get(GB(p2
, 0, 20))->type
);
882 * Removes the highlight of a vehicle in a group window
883 * @param *v Vehicle to remove all highlights from
885 void DeleteGroupHighlightOfVehicle(const Vehicle
*v
)
887 /* If we haven't got any vehicles on the mouse pointer, we haven't got any highlighted in any group windows either
888 * If that is the case, we can skip looping though the windows and save time
890 if (_special_mouse_mode
!= WSM_DRAGDROP
) return;
892 VehicleGroupWindow
*w
= FindVehicleGroupWindow(v
->type
, v
->owner
);
893 if (w
!= NULL
) w
->UnselectVehicle(v
->index
);