group_gui.cpp

Go to the documentation of this file.
00001 /* $Id$ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * 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.
00006  * 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.
00007  * 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/>.
00008  */
00009 
00012 #include "stdafx.h"
00013 #include "textbuf_gui.h"
00014 #include "command_func.h"
00015 #include "vehicle_gui.h"
00016 #include "vehicle_base.h"
00017 #include "group.h"
00018 #include "string_func.h"
00019 #include "strings_func.h"
00020 #include "window_func.h"
00021 #include "vehicle_func.h"
00022 #include "autoreplace_gui.h"
00023 #include "company_func.h"
00024 #include "widgets/dropdown_func.h"
00025 #include "tilehighlight_func.h"
00026 #include "vehicle_gui_base.h"
00027 #include "core/geometry_func.hpp"
00028 #include "company_base.h"
00029 
00030 #include "table/strings.h"
00031 #include "table/sprites.h"
00032 
00033 typedef GUIList<const Group*> GUIGroupList;
00034 
00035 enum GroupListWidgets {
00036   GRP_WIDGET_CAPTION,
00037   GRP_WIDGET_SORT_BY_ORDER,
00038   GRP_WIDGET_SORT_BY_DROPDOWN,
00039   GRP_WIDGET_LIST_VEHICLE,
00040   GRP_WIDGET_LIST_VEHICLE_SCROLLBAR,
00041   GRP_WIDGET_AVAILABLE_VEHICLES,
00042   GRP_WIDGET_MANAGE_VEHICLES_DROPDOWN,
00043   GRP_WIDGET_STOP_ALL,
00044   GRP_WIDGET_START_ALL,
00045 
00046   GRP_WIDGET_ALL_VEHICLES,
00047   GRP_WIDGET_DEFAULT_VEHICLES,
00048   GRP_WIDGET_LIST_GROUP,
00049   GRP_WIDGET_LIST_GROUP_SCROLLBAR,
00050   GRP_WIDGET_CREATE_GROUP,
00051   GRP_WIDGET_DELETE_GROUP,
00052   GRP_WIDGET_RENAME_GROUP,
00053   GRP_WIDGET_REPLACE_PROTECTION,
00054 };
00055 
00056 static const NWidgetPart _nested_group_widgets[] = {
00057   NWidget(NWID_HORIZONTAL), // Window header
00058     NWidget(WWT_CLOSEBOX, COLOUR_GREY),
00059     NWidget(WWT_CAPTION, COLOUR_GREY, GRP_WIDGET_CAPTION),
00060     NWidget(WWT_SHADEBOX, COLOUR_GREY),
00061     NWidget(WWT_STICKYBOX, COLOUR_GREY),
00062   EndContainer(),
00063   NWidget(NWID_HORIZONTAL),
00064     /* left part */
00065     NWidget(NWID_VERTICAL),
00066       NWidget(WWT_PANEL, COLOUR_GREY), SetMinimalTextLines(1, WD_DROPDOWNTEXT_TOP + WD_DROPDOWNTEXT_BOTTOM), SetFill(1, 0), EndContainer(),
00067       NWidget(WWT_PANEL, COLOUR_GREY, GRP_WIDGET_ALL_VEHICLES), SetFill(1, 0), EndContainer(),
00068       NWidget(WWT_PANEL, COLOUR_GREY, GRP_WIDGET_DEFAULT_VEHICLES), SetFill(1, 0), EndContainer(),
00069       NWidget(NWID_HORIZONTAL),
00070         NWidget(WWT_MATRIX, COLOUR_GREY, GRP_WIDGET_LIST_GROUP), SetDataTip(0x701, STR_GROUPS_CLICK_ON_GROUP_FOR_TOOLTIP),
00071             SetFill(1, 0), SetResize(0, 1), SetScrollbar(GRP_WIDGET_LIST_GROUP_SCROLLBAR),
00072         NWidget(NWID_VSCROLLBAR, COLOUR_GREY, GRP_WIDGET_LIST_GROUP_SCROLLBAR),
00073       EndContainer(),
00074       NWidget(NWID_HORIZONTAL),
00075         NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, GRP_WIDGET_CREATE_GROUP), SetMinimalSize(24, 25), SetFill(0, 1),
00076             SetDataTip(SPR_GROUP_CREATE_TRAIN, STR_GROUP_CREATE_TOOLTIP),
00077         NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, GRP_WIDGET_DELETE_GROUP), SetMinimalSize(24, 25), SetFill(0, 1),
00078             SetDataTip(SPR_GROUP_DELETE_TRAIN, STR_GROUP_DELETE_TOOLTIP),
00079         NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, GRP_WIDGET_RENAME_GROUP), SetMinimalSize(24, 25), SetFill(0, 1),
00080             SetDataTip(SPR_GROUP_RENAME_TRAIN, STR_GROUP_RENAME_TOOLTIP),
00081         NWidget(WWT_PANEL, COLOUR_GREY), SetFill(1, 1), EndContainer(),
00082         NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, GRP_WIDGET_REPLACE_PROTECTION), SetMinimalSize(24, 25), SetFill(0, 1),
00083             SetDataTip(SPR_GROUP_REPLACE_OFF_TRAIN, STR_GROUP_REPLACE_PROTECTION_TOOLTIP),
00084         NWidget(WWT_PANEL, COLOUR_GREY), SetFill(0, 1), EndContainer(),
00085       EndContainer(),
00086     EndContainer(),
00087     /* right part */
00088     NWidget(NWID_VERTICAL),
00089       NWidget(NWID_HORIZONTAL),
00090         NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, GRP_WIDGET_SORT_BY_ORDER), SetMinimalSize(81, 12), SetDataTip(STR_BUTTON_SORT_BY, STR_TOOLTIP_SORT_ORDER),
00091         NWidget(WWT_DROPDOWN, COLOUR_GREY, GRP_WIDGET_SORT_BY_DROPDOWN), SetMinimalSize(167, 12), SetDataTip(0x0, STR_TOOLTIP_SORT_CRITERIA),
00092         NWidget(WWT_PANEL, COLOUR_GREY), SetMinimalSize(12, 12), SetResize(1, 0), EndContainer(),
00093       EndContainer(),
00094       NWidget(NWID_HORIZONTAL),
00095         NWidget(WWT_MATRIX, COLOUR_GREY, GRP_WIDGET_LIST_VEHICLE), SetMinimalSize(248, 0), SetDataTip(0x701, STR_NULL), SetResize(1, 1), SetFill(1, 0), SetScrollbar(GRP_WIDGET_LIST_VEHICLE_SCROLLBAR),
00096         NWidget(NWID_VSCROLLBAR, COLOUR_GREY, GRP_WIDGET_LIST_VEHICLE_SCROLLBAR),
00097       EndContainer(),
00098       NWidget(WWT_PANEL, COLOUR_GREY), SetMinimalSize(1, 0), SetFill(1, 1), SetResize(1, 0), EndContainer(),
00099       NWidget(NWID_HORIZONTAL),
00100         NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, GRP_WIDGET_AVAILABLE_VEHICLES), SetMinimalSize(106, 12), SetFill(0, 1),
00101             SetDataTip(STR_BLACK_STRING, STR_VEHICLE_LIST_AVAILABLE_ENGINES_TOOLTIP),
00102         NWidget(WWT_DROPDOWN, COLOUR_GREY, GRP_WIDGET_MANAGE_VEHICLES_DROPDOWN), SetMinimalSize(118, 12), SetFill(0, 1),
00103             SetDataTip(STR_VEHICLE_LIST_MANAGE_LIST, STR_VEHICLE_LIST_MANAGE_LIST_TOOLTIP),
00104         NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, GRP_WIDGET_STOP_ALL), SetMinimalSize(12, 12), SetFill(0, 1),
00105             SetDataTip(SPR_FLAG_VEH_STOPPED, STR_VEHICLE_LIST_MASS_STOP_LIST_TOOLTIP),
00106         NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, GRP_WIDGET_START_ALL), SetMinimalSize(12, 12), SetFill(0, 1),
00107             SetDataTip(SPR_FLAG_VEH_RUNNING, STR_VEHICLE_LIST_MASS_START_LIST_TOOLTIP),
00108         NWidget(WWT_PANEL, COLOUR_GREY), SetMinimalSize(0, 12), SetFill(1, 1), SetResize(1, 0), EndContainer(),
00109         NWidget(WWT_RESIZEBOX, COLOUR_GREY),
00110       EndContainer(),
00111     EndContainer(),
00112   EndContainer(),
00113 };
00114 
00115 class VehicleGroupWindow : public BaseVehicleListWindow {
00116 private:
00117   /* Columns in the group list */
00118   enum ListColumns {
00119     VGC_NAME,          
00120     VGC_PROTECT,       
00121     VGC_AUTOREPLACE,   
00122     VGC_PROFIT,        
00123     VGC_NUMBER,        
00124 
00125     VGC_END
00126   };
00127 
00128   VehicleID vehicle_sel; 
00129   GroupID group_rename;  
00130   GUIGroupList groups;   
00131   uint tiny_step_height; 
00132   Scrollbar *group_sb;
00133 
00134   Dimension column_size[VGC_END]; 
00135 
00141   void BuildGroupList(Owner owner)
00142   {
00143     if (!this->groups.NeedRebuild()) return;
00144 
00145     this->groups.Clear();
00146 
00147     const Group *g;
00148     FOR_ALL_GROUPS(g) {
00149       if (g->owner == owner && g->vehicle_type == this->vli.vtype) {
00150         *this->groups.Append() = g;
00151       }
00152     }
00153 
00154     this->groups.Compact();
00155     this->groups.RebuildDone();
00156   }
00157 
00159   static int CDECL GroupNameSorter(const Group * const *a, const Group * const *b)
00160   {
00161     static const Group *last_group[2] = { NULL, NULL };
00162     static char         last_name[2][64] = { "", "" };
00163 
00164     if (*a != last_group[0]) {
00165       last_group[0] = *a;
00166       SetDParam(0, (*a)->index);
00167       GetString(last_name[0], STR_GROUP_NAME, lastof(last_name[0]));
00168     }
00169 
00170     if (*b != last_group[1]) {
00171       last_group[1] = *b;
00172       SetDParam(0, (*b)->index);
00173       GetString(last_name[1], STR_GROUP_NAME, lastof(last_name[1]));
00174     }
00175 
00176     int r = strnatcmp(last_name[0], last_name[1]); // Sort by name (natural sorting).
00177     if (r == 0) return (*a)->index - (*b)->index;
00178     return r;
00179   }
00180 
00185   uint ComputeGroupInfoSize()
00186   {
00187     this->column_size[VGC_NAME] = maxdim(GetStringBoundingBox(STR_GROUP_DEFAULT_TRAINS + this->vli.vtype), GetStringBoundingBox(STR_GROUP_ALL_TRAINS + this->vli.vtype));
00188     this->column_size[VGC_NAME].width = max(170u, this->column_size[VGC_NAME].width);
00189     this->tiny_step_height = this->column_size[VGC_NAME].height;
00190 
00191     this->column_size[VGC_PROTECT] = GetSpriteSize(SPR_GROUP_REPLACE_PROTECT);
00192     this->tiny_step_height = max(this->tiny_step_height, this->column_size[VGC_PROTECT].height);
00193 
00194     this->column_size[VGC_AUTOREPLACE] = GetSpriteSize(SPR_GROUP_REPLACE_ACTIVE);
00195     this->tiny_step_height = max(this->tiny_step_height, this->column_size[VGC_AUTOREPLACE].height);
00196 
00197     this->column_size[VGC_PROFIT].width = 0;
00198     this->column_size[VGC_PROFIT].height = 0;
00199     static const SpriteID profit_sprites[] = {SPR_PROFIT_NA, SPR_PROFIT_NEGATIVE, SPR_PROFIT_SOME, SPR_PROFIT_LOT};
00200     for (uint i = 0; i < lengthof(profit_sprites); i++) {
00201       Dimension d = GetSpriteSize(profit_sprites[i]);
00202       this->column_size[VGC_PROFIT] = maxdim(this->column_size[VGC_PROFIT], d);
00203     }
00204     this->tiny_step_height = max(this->tiny_step_height, this->column_size[VGC_PROFIT].height);
00205 
00206     SetDParam(0, GroupStatistics::Get(this->vli.company, ALL_GROUP, this->vli.vtype).num_vehicle > 900 ? 9999 : 999);
00207     this->column_size[VGC_NUMBER] = GetStringBoundingBox(STR_TINY_COMMA);
00208     this->tiny_step_height = max(this->tiny_step_height, this->column_size[VGC_NUMBER].height);
00209 
00210     this->tiny_step_height += WD_MATRIX_TOP;
00211 
00212     return WD_FRAMERECT_LEFT + 8 +
00213       this->column_size[VGC_NAME].width + 8 +
00214       this->column_size[VGC_PROTECT].width + 2 +
00215       this->column_size[VGC_AUTOREPLACE].width + 2 +
00216       this->column_size[VGC_PROFIT].width + 2 +
00217       this->column_size[VGC_NUMBER].width + 2 +
00218       WD_FRAMERECT_RIGHT;
00219   }
00220 
00229   void DrawGroupInfo(int y, int left, int right, GroupID g_id, bool protection = false) const
00230   {
00231     /* draw the selected group in white, else we draw it in black */
00232     TextColour colour = g_id == this->vli.index ? TC_WHITE : TC_BLACK;
00233     const GroupStatistics &stats = GroupStatistics::Get(this->vli.company, g_id, this->vli.vtype);
00234     bool rtl = _current_text_dir == TD_RTL;
00235 
00236     /* draw group name */
00237     StringID str;
00238     if (IsAllGroupID(g_id)) {
00239       str = STR_GROUP_ALL_TRAINS + this->vli.vtype;
00240     } else if (IsDefaultGroupID(g_id)) {
00241       str = STR_GROUP_DEFAULT_TRAINS + this->vli.vtype;
00242     } else {
00243       SetDParam(0, g_id);
00244       str = STR_GROUP_NAME;
00245     }
00246     int x = rtl ? right - WD_FRAMERECT_RIGHT - 8 - this->column_size[VGC_NAME].width + 1 : left + WD_FRAMERECT_LEFT + 8;
00247     DrawString(x, x + this->column_size[VGC_NAME].width - 1, y + (this->tiny_step_height - this->column_size[VGC_NAME].height) / 2, str, colour);
00248 
00249     /* draw autoreplace protection */
00250     x = rtl ? x - 8 - this->column_size[VGC_PROTECT].width : x + 8 + this->column_size[VGC_NAME].width;
00251     if (protection) DrawSprite(SPR_GROUP_REPLACE_PROTECT, PAL_NONE, x, y + (this->tiny_step_height - this->column_size[VGC_PROTECT].height) / 2);
00252 
00253     /* draw autoreplace status */
00254     x = rtl ? x - 2 - this->column_size[VGC_AUTOREPLACE].width : x + 2 + this->column_size[VGC_PROTECT].width;
00255     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);
00256 
00257     /* draw the profit icon */
00258     x = rtl ? x - 2 - this->column_size[VGC_PROFIT].width : x + 2 + this->column_size[VGC_AUTOREPLACE].width;
00259     SpriteID spr;
00260     if (stats.num_profit_vehicle == 0) {
00261       spr = SPR_PROFIT_NA;
00262     } else if (stats.profit_last_year < 0) {
00263       spr = SPR_PROFIT_NEGATIVE;
00264     } else if (stats.profit_last_year < 10000 * stats.num_profit_vehicle) { // TODO magic number
00265       spr = SPR_PROFIT_SOME;
00266     } else {
00267       spr = SPR_PROFIT_LOT;
00268     }
00269     DrawSprite(spr, PAL_NONE, x, y + (this->tiny_step_height - this->column_size[VGC_PROFIT].height) / 2);
00270 
00271     /* draw the number of vehicles of the group */
00272     x = rtl ? x - 2 - this->column_size[VGC_NUMBER].width : x + 2 + this->column_size[VGC_PROFIT].width;
00273     SetDParam(0, stats.num_vehicle);
00274     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);
00275   }
00276 
00277 public:
00278   VehicleGroupWindow(const WindowDesc *desc, WindowNumber window_number) : BaseVehicleListWindow(window_number)
00279   {
00280     this->CreateNestedTree(desc);
00281 
00282     this->vscroll = this->GetScrollbar(GRP_WIDGET_LIST_VEHICLE_SCROLLBAR);
00283     this->group_sb = this->GetScrollbar(GRP_WIDGET_LIST_GROUP_SCROLLBAR);
00284 
00285     switch (this->vli.vtype) {
00286       default: NOT_REACHED();
00287       case VEH_TRAIN:    this->sorting = &_sorting.train;    break;
00288       case VEH_ROAD:     this->sorting = &_sorting.roadveh;  break;
00289       case VEH_SHIP:     this->sorting = &_sorting.ship;     break;
00290       case VEH_AIRCRAFT: this->sorting = &_sorting.aircraft; break;
00291     }
00292 
00293     this->vli.index = ALL_GROUP;
00294     this->vehicle_sel = INVALID_VEHICLE;
00295     this->group_rename = INVALID_GROUP;
00296 
00297     this->vehicles.SetListing(*this->sorting);
00298     this->vehicles.ForceRebuild();
00299     this->vehicles.NeedResort();
00300 
00301     this->BuildVehicleList();
00302     this->SortVehicleList();
00303 
00304     this->groups.ForceRebuild();
00305     this->groups.NeedResort();
00306     this->BuildGroupList(vli.company);
00307     this->groups.Sort(&GroupNameSorter);
00308 
00309     this->GetWidget<NWidgetCore>(GRP_WIDGET_CAPTION)->widget_data = STR_VEHICLE_LIST_TRAIN_CAPTION + this->vli.vtype;
00310     this->GetWidget<NWidgetCore>(GRP_WIDGET_LIST_VEHICLE)->tool_tip = STR_VEHICLE_LIST_TRAIN_LIST_TOOLTIP + this->vli.vtype;
00311 
00312     this->GetWidget<NWidgetCore>(GRP_WIDGET_CREATE_GROUP)->widget_data += this->vli.vtype;
00313     this->GetWidget<NWidgetCore>(GRP_WIDGET_RENAME_GROUP)->widget_data += this->vli.vtype;
00314     this->GetWidget<NWidgetCore>(GRP_WIDGET_DELETE_GROUP)->widget_data += this->vli.vtype;
00315     this->GetWidget<NWidgetCore>(GRP_WIDGET_REPLACE_PROTECTION)->widget_data += this->vli.vtype;
00316 
00317     this->FinishInitNested(desc, window_number);
00318     this->owner = vli.company;
00319   }
00320 
00321   ~VehicleGroupWindow()
00322   {
00323     *this->sorting = this->vehicles.GetListing();
00324   }
00325 
00326   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00327   {
00328     switch (widget) {
00329       case GRP_WIDGET_LIST_GROUP: {
00330         size->width = this->ComputeGroupInfoSize();
00331         resize->height = this->tiny_step_height;
00332 
00333         /* Minimum height is the height of the list widget minus all and default vehicles... */
00334         size->height =  4 * GetVehicleListHeight(this->vli.vtype, this->tiny_step_height) - 2 * this->tiny_step_height;
00335 
00336         /* ... minus the buttons at the bottom ... */
00337         uint max_icon_height = 25;
00338         max_icon_height = max(max_icon_height, GetSpriteSize(this->GetWidget<NWidgetCore>(GRP_WIDGET_CREATE_GROUP)->widget_data).height);
00339         max_icon_height = max(max_icon_height, GetSpriteSize(this->GetWidget<NWidgetCore>(GRP_WIDGET_RENAME_GROUP)->widget_data).height);
00340         max_icon_height = max(max_icon_height, GetSpriteSize(this->GetWidget<NWidgetCore>(GRP_WIDGET_DELETE_GROUP)->widget_data).height);
00341         max_icon_height = max(max_icon_height, GetSpriteSize(this->GetWidget<NWidgetCore>(GRP_WIDGET_REPLACE_PROTECTION)->widget_data).height);
00342 
00343         /* ... plus the statusbar below the vehicle list */
00344         if (max_icon_height > FONT_HEIGHT_NORMAL) max_icon_height -= FONT_HEIGHT_NORMAL;
00345 
00346         /* The size must be a multiple of tiny_step_height for the resizing to work */
00347         size->height -= this->tiny_step_height * CeilDiv(max_icon_height, this->tiny_step_height);
00348         break;
00349       }
00350 
00351       case GRP_WIDGET_ALL_VEHICLES:
00352       case GRP_WIDGET_DEFAULT_VEHICLES:
00353         size->width = this->ComputeGroupInfoSize();
00354         size->height = this->tiny_step_height;
00355         break;
00356 
00357       case GRP_WIDGET_SORT_BY_ORDER: {
00358         Dimension d = GetStringBoundingBox(this->GetWidget<NWidgetCore>(widget)->widget_data);
00359         d.width += padding.width + WD_SORTBUTTON_ARROW_WIDTH * 2; // Doubled since the string is centred and it also looks better.
00360         d.height += padding.height;
00361         *size = maxdim(*size, d);
00362         break;
00363       }
00364 
00365       case GRP_WIDGET_LIST_VEHICLE:
00366         this->ComputeGroupInfoSize();
00367         resize->height = GetVehicleListHeight(this->vli.vtype, this->tiny_step_height);
00368         size->height = 4 * resize->height;
00369         break;
00370 
00371       case GRP_WIDGET_MANAGE_VEHICLES_DROPDOWN: {
00372         Dimension d = this->GetActionDropdownSize(true, true);
00373         d.height += padding.height;
00374         d.width  += padding.width;
00375         *size = maxdim(*size, d);
00376         break;
00377       }
00378     }
00379   }
00380 
00386   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
00387   {
00388     if (data == 0) {
00389       /* This needs to be done in command-scope to enforce rebuilding before resorting invalid data */
00390       this->vehicles.ForceRebuild();
00391       this->groups.ForceRebuild();
00392     } else {
00393       this->vehicles.ForceResort();
00394       this->groups.ForceResort();
00395     }
00396 
00397     /* Process ID-invalidation in command-scope as well */
00398     if (this->group_rename != INVALID_GROUP && !Group::IsValidID(this->group_rename)) {
00399       DeleteWindowByClass(WC_QUERY_STRING);
00400       this->group_rename = INVALID_GROUP;
00401     }
00402 
00403     if (!(IsAllGroupID(this->vli.index) || IsDefaultGroupID(this->vli.index) || Group::IsValidID(this->vli.index))) {
00404       this->vli.index = ALL_GROUP;
00405       HideDropDownMenu(this);
00406     }
00407     this->SetDirty();
00408   }
00409 
00410   virtual void SetStringParameters(int widget) const
00411   {
00412     switch (widget) {
00413       case GRP_WIDGET_AVAILABLE_VEHICLES:
00414         SetDParam(0, STR_VEHICLE_LIST_AVAILABLE_TRAINS + this->vli.vtype);
00415         break;
00416 
00417       case GRP_WIDGET_CAPTION:
00418         /* If selected_group == DEFAULT_GROUP || ALL_GROUP, draw the standard caption
00419          * We list all vehicles or ungrouped vehicles */
00420         if (IsDefaultGroupID(this->vli.index) || IsAllGroupID(this->vli.index)) {
00421           SetDParam(0, STR_COMPANY_NAME);
00422           SetDParam(1, this->vli.company);
00423           SetDParam(2, this->vehicles.Length());
00424           SetDParam(3, this->vehicles.Length());
00425         } else {
00426           const Group *g = Group::Get(this->vli.index);
00427 
00428           SetDParam(0, STR_GROUP_NAME);
00429           SetDParam(1, g->index);
00430           SetDParam(2, g->statistics.num_vehicle);
00431           SetDParam(3, g->statistics.num_vehicle);
00432         }
00433         break;
00434     }
00435   }
00436 
00437   virtual void OnPaint()
00438   {
00439     /* If we select the all vehicles, this->list will contain all vehicles of the owner
00440      * else this->list will contain all vehicles which belong to the selected group */
00441     this->BuildVehicleList();
00442     this->SortVehicleList();
00443 
00444     this->BuildGroupList(this->owner);
00445     this->groups.Sort(&GroupNameSorter);
00446 
00447     this->group_sb->SetCount(this->groups.Length());
00448     this->vscroll->SetCount(this->vehicles.Length());
00449 
00450     /* The drop down menu is out, *but* it may not be used, retract it. */
00451     if (this->vehicles.Length() == 0 && this->IsWidgetLowered(GRP_WIDGET_MANAGE_VEHICLES_DROPDOWN)) {
00452       this->RaiseWidget(GRP_WIDGET_MANAGE_VEHICLES_DROPDOWN);
00453       HideDropDownMenu(this);
00454     }
00455 
00456     /* Disable all lists management button when the list is empty */
00457     this->SetWidgetsDisabledState(this->vehicles.Length() == 0 || _local_company != this->vli.company,
00458         GRP_WIDGET_STOP_ALL,
00459         GRP_WIDGET_START_ALL,
00460         GRP_WIDGET_MANAGE_VEHICLES_DROPDOWN,
00461         WIDGET_LIST_END);
00462 
00463     /* Disable the group specific function when we select the default group or all vehicles */
00464     this->SetWidgetsDisabledState(IsDefaultGroupID(this->vli.index) || IsAllGroupID(this->vli.index) || _local_company != this->vli.company,
00465         GRP_WIDGET_DELETE_GROUP,
00466         GRP_WIDGET_RENAME_GROUP,
00467         GRP_WIDGET_REPLACE_PROTECTION,
00468         WIDGET_LIST_END);
00469 
00470     /* Disable remaining buttons for non-local companies
00471      * Needed while changing _local_company, eg. by cheats
00472      * All procedures (eg. move vehicle to another group)
00473      *  verify, whether you are the owner of the vehicle,
00474      *  so it doesn't have to be disabled
00475      */
00476     this->SetWidgetsDisabledState(_local_company != this->vli.company,
00477         GRP_WIDGET_CREATE_GROUP,
00478         GRP_WIDGET_AVAILABLE_VEHICLES,
00479         WIDGET_LIST_END);
00480 
00481     /* If not a default group and the group has replace protection, show an enabled replace sprite. */
00482     uint16 protect_sprite = SPR_GROUP_REPLACE_OFF_TRAIN;
00483     if (!IsDefaultGroupID(this->vli.index) && !IsAllGroupID(this->vli.index) && Group::Get(this->vli.index)->replace_protection) protect_sprite = SPR_GROUP_REPLACE_ON_TRAIN;
00484     this->GetWidget<NWidgetCore>(GRP_WIDGET_REPLACE_PROTECTION)->widget_data = protect_sprite + this->vli.vtype;
00485 
00486     /* Set text of sort by dropdown */
00487     this->GetWidget<NWidgetCore>(GRP_WIDGET_SORT_BY_DROPDOWN)->widget_data = this->vehicle_sorter_names[this->vehicles.SortType()];
00488 
00489     this->DrawWidgets();
00490   }
00491 
00492   virtual void DrawWidget(const Rect &r, int widget) const
00493   {
00494     switch (widget) {
00495       case GRP_WIDGET_ALL_VEHICLES:
00496         DrawGroupInfo(r.top + WD_FRAMERECT_TOP, r.left, r.right, ALL_GROUP);
00497         break;
00498 
00499       case GRP_WIDGET_DEFAULT_VEHICLES:
00500         DrawGroupInfo(r.top + WD_FRAMERECT_TOP, r.left, r.right, DEFAULT_GROUP);
00501         break;
00502 
00503       case GRP_WIDGET_LIST_GROUP: {
00504         int y1 = r.top + WD_FRAMERECT_TOP;
00505         int max = min(this->group_sb->GetPosition() + this->group_sb->GetCapacity(), this->groups.Length());
00506         for (int i = this->group_sb->GetPosition(); i < max; ++i) {
00507           const Group *g = this->groups[i];
00508 
00509           assert(g->owner == this->owner);
00510 
00511           DrawGroupInfo(y1, r.left, r.right, g->index, g->replace_protection);
00512 
00513           y1 += this->tiny_step_height;
00514         }
00515         break;
00516       }
00517 
00518       case GRP_WIDGET_SORT_BY_ORDER:
00519         this->DrawSortButtonState(GRP_WIDGET_SORT_BY_ORDER, this->vehicles.IsDescSortOrder() ? SBS_DOWN : SBS_UP);
00520         break;
00521 
00522       case GRP_WIDGET_LIST_VEHICLE:
00523         this->DrawVehicleListItems(this->vehicle_sel, this->resize.step_height, r);
00524         break;
00525     }
00526   }
00527 
00528   virtual void OnClick(Point pt, int widget, int click_count)
00529   {
00530     switch (widget) {
00531       case GRP_WIDGET_SORT_BY_ORDER: // Flip sorting method ascending/descending
00532         this->vehicles.ToggleSortOrder();
00533         this->SetDirty();
00534         break;
00535 
00536       case GRP_WIDGET_SORT_BY_DROPDOWN: // Select sorting criteria dropdown menu
00537         ShowDropDownMenu(this, this->vehicle_sorter_names, this->vehicles.SortType(),  GRP_WIDGET_SORT_BY_DROPDOWN, 0, (this->vli.vtype == VEH_TRAIN || this->vli.vtype == VEH_ROAD) ? 0 : (1 << 10));
00538         return;
00539 
00540       case GRP_WIDGET_ALL_VEHICLES: // All vehicles button
00541         if (!IsAllGroupID(this->vli.index)) {
00542           this->vli.index = ALL_GROUP;
00543           this->vehicles.ForceRebuild();
00544           this->SetDirty();
00545         }
00546         break;
00547 
00548       case GRP_WIDGET_DEFAULT_VEHICLES: // Ungrouped vehicles button
00549         if (!IsDefaultGroupID(this->vli.index)) {
00550           this->vli.index = DEFAULT_GROUP;
00551           this->vehicles.ForceRebuild();
00552           this->SetDirty();
00553         }
00554         break;
00555 
00556       case GRP_WIDGET_LIST_GROUP: { // Matrix Group
00557         uint id_g = this->group_sb->GetScrolledRowFromWidget(pt.y, this, GRP_WIDGET_LIST_GROUP, 0, this->tiny_step_height);
00558         if (id_g >= this->groups.Length()) return;
00559 
00560         this->vli.index = this->groups[id_g]->index;
00561 
00562         this->vehicles.ForceRebuild();
00563         this->SetDirty();
00564         break;
00565       }
00566 
00567       case GRP_WIDGET_LIST_VEHICLE: { // Matrix Vehicle
00568         uint id_v = this->vscroll->GetScrolledRowFromWidget(pt.y, this, GRP_WIDGET_LIST_VEHICLE);
00569         if (id_v >= this->vehicles.Length()) return; // click out of list bound
00570 
00571         const Vehicle *v = this->vehicles[id_v];
00572         if (VehicleClicked(v)) break;
00573 
00574         this->vehicle_sel = v->index;
00575 
00576         int image = v->GetImage(_current_text_dir == TD_RTL ? DIR_E : DIR_W, EIT_IN_LIST);
00577         SetObjectToPlaceWnd(image, GetVehiclePalette(v), HT_DRAG, this);
00578         _cursor.vehchain = true;
00579 
00580         this->SetDirty();
00581         break;
00582       }
00583 
00584       case GRP_WIDGET_CREATE_GROUP: { // Create a new group
00585         DoCommandP(0, this->vli.vtype, 0, CMD_CREATE_GROUP | CMD_MSG(STR_ERROR_GROUP_CAN_T_CREATE), CcCreateGroup);
00586         break;
00587       }
00588 
00589       case GRP_WIDGET_DELETE_GROUP: { // Delete the selected group
00590         GroupID group = this->vli.index;
00591         this->vli.index = ALL_GROUP;
00592 
00593         DoCommandP(0, group, 0, CMD_DELETE_GROUP | CMD_MSG(STR_ERROR_GROUP_CAN_T_DELETE));
00594         break;
00595       }
00596 
00597       case GRP_WIDGET_RENAME_GROUP: // Rename the selected roup
00598         this->ShowRenameGroupWindow(this->vli.index, false);
00599         break;
00600 
00601       case GRP_WIDGET_AVAILABLE_VEHICLES:
00602         ShowBuildVehicleWindow(INVALID_TILE, this->vli.vtype);
00603         break;
00604 
00605       case GRP_WIDGET_MANAGE_VEHICLES_DROPDOWN: {
00606         DropDownList *list = this->BuildActionDropdownList(true, Group::IsValidID(this->vli.index));
00607         ShowDropDownList(this, list, 0, GRP_WIDGET_MANAGE_VEHICLES_DROPDOWN);
00608         break;
00609       }
00610 
00611       case GRP_WIDGET_START_ALL:
00612       case GRP_WIDGET_STOP_ALL: { // Start/stop all vehicles of the list
00613         DoCommandP(0, (1 << 1) | (widget == GRP_WIDGET_START_ALL ? (1 << 0) : 0), this->vli.Pack(), CMD_MASS_START_STOP);
00614         break;
00615       }
00616 
00617       case GRP_WIDGET_REPLACE_PROTECTION: {
00618         const Group *g = Group::GetIfValid(this->vli.index);
00619         if (g != NULL) {
00620           DoCommandP(0, this->vli.index, !g->replace_protection, CMD_SET_GROUP_REPLACE_PROTECTION);
00621         }
00622         break;
00623       }
00624     }
00625   }
00626 
00627   virtual void OnDragDrop(Point pt, int widget)
00628   {
00629     switch (widget) {
00630       case GRP_WIDGET_ALL_VEHICLES: // All vehicles
00631       case GRP_WIDGET_DEFAULT_VEHICLES: // Ungrouped vehicles
00632         DoCommandP(0, DEFAULT_GROUP, this->vehicle_sel, CMD_ADD_VEHICLE_GROUP | CMD_MSG(STR_ERROR_GROUP_CAN_T_ADD_VEHICLE));
00633 
00634         this->vehicle_sel = INVALID_VEHICLE;
00635 
00636         this->SetDirty();
00637         break;
00638 
00639       case GRP_WIDGET_LIST_GROUP: { // Matrix group
00640         const VehicleID vindex = this->vehicle_sel;
00641         this->vehicle_sel = INVALID_VEHICLE;
00642         this->SetDirty();
00643 
00644         uint id_g = this->group_sb->GetScrolledRowFromWidget(pt.y, this, GRP_WIDGET_LIST_GROUP, 0, this->tiny_step_height);
00645         if (id_g >= this->groups.Length()) return;
00646 
00647         DoCommandP(0, this->groups[id_g]->index, vindex, CMD_ADD_VEHICLE_GROUP | CMD_MSG(STR_ERROR_GROUP_CAN_T_ADD_VEHICLE));
00648         break;
00649       }
00650 
00651       case GRP_WIDGET_LIST_VEHICLE: { // Matrix vehicle
00652         const VehicleID vindex = this->vehicle_sel;
00653         this->vehicle_sel = INVALID_VEHICLE;
00654         this->SetDirty();
00655 
00656         uint id_v = this->vscroll->GetScrolledRowFromWidget(pt.y, this, GRP_WIDGET_LIST_VEHICLE);
00657         if (id_v >= this->vehicles.Length()) return; // click out of list bound
00658 
00659         const Vehicle *v = this->vehicles[id_v];
00660         if (!VehicleClicked(v) && vindex == v->index) {
00661           ShowVehicleViewWindow(v);
00662         }
00663         break;
00664       }
00665     }
00666     _cursor.vehchain = false;
00667   }
00668 
00669   virtual void OnQueryTextFinished(char *str)
00670   {
00671     if (str != NULL) DoCommandP(0, this->group_rename, 0, CMD_RENAME_GROUP | CMD_MSG(STR_ERROR_GROUP_CAN_T_RENAME), NULL, str);
00672     this->group_rename = INVALID_GROUP;
00673   }
00674 
00675   virtual void OnResize()
00676   {
00677     NWidgetCore *nwi = this->GetWidget<NWidgetCore>(GRP_WIDGET_LIST_GROUP);
00678     this->group_sb->SetCapacity(nwi->current_y / this->tiny_step_height);
00679     nwi->widget_data = (this->group_sb->GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
00680 
00681     nwi = this->GetWidget<NWidgetCore>(GRP_WIDGET_LIST_VEHICLE);
00682     this->vscroll->SetCapacityFromWidget(this, GRP_WIDGET_LIST_VEHICLE);
00683     nwi->widget_data = (this->vscroll->GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
00684   }
00685 
00686   virtual void OnDropdownSelect(int widget, int index)
00687   {
00688     switch (widget) {
00689       case GRP_WIDGET_SORT_BY_DROPDOWN:
00690         this->vehicles.SetSortType(index);
00691         break;
00692 
00693       case GRP_WIDGET_MANAGE_VEHICLES_DROPDOWN:
00694         assert(this->vehicles.Length() != 0);
00695 
00696         switch (index) {
00697           case ADI_REPLACE: // Replace window
00698             ShowReplaceGroupVehicleWindow(this->vli.index, this->vli.vtype);
00699             break;
00700           case ADI_SERVICE: // Send for servicing
00701           case ADI_DEPOT: { // Send to Depots
00702             DoCommandP(0, DEPOT_MASS_SEND | (index == ADI_SERVICE ? DEPOT_SERVICE : 0U), this->vli.Pack(), GetCmdSendToDepot(this->vli.vtype));
00703             break;
00704           }
00705 
00706           case ADI_ADD_SHARED: // Add shared Vehicles
00707             assert(Group::IsValidID(this->vli.index));
00708 
00709             DoCommandP(0, this->vli.index, this->vli.vtype, CMD_ADD_SHARED_VEHICLE_GROUP | CMD_MSG(STR_ERROR_GROUP_CAN_T_ADD_SHARED_VEHICLE));
00710             break;
00711           case ADI_REMOVE_ALL: // Remove all Vehicles from the selected group
00712             assert(Group::IsValidID(this->vli.index));
00713 
00714             DoCommandP(0, this->vli.index, 0, CMD_REMOVE_ALL_VEHICLES_GROUP | CMD_MSG(STR_ERROR_GROUP_CAN_T_REMOVE_ALL_VEHICLES));
00715             break;
00716           default: NOT_REACHED();
00717         }
00718         break;
00719 
00720       default: NOT_REACHED();
00721     }
00722 
00723     this->SetDirty();
00724   }
00725 
00726   virtual void OnTick()
00727   {
00728     if (_pause_mode != PM_UNPAUSED) return;
00729     if (this->groups.NeedResort() || this->vehicles.NeedResort()) {
00730       this->SetDirty();
00731     }
00732   }
00733 
00734   virtual void OnPlaceObjectAbort()
00735   {
00736     /* abort drag & drop */
00737     this->vehicle_sel = INVALID_VEHICLE;
00738     this->SetWidgetDirty(GRP_WIDGET_LIST_VEHICLE);
00739   }
00740 
00741   void ShowRenameGroupWindow(GroupID group, bool empty)
00742   {
00743     assert(Group::IsValidID(group));
00744     this->group_rename = group;
00745     /* Show empty query for new groups */
00746     StringID str = STR_EMPTY;
00747     if (!empty) {
00748       SetDParam(0, group);
00749       str = STR_GROUP_NAME;
00750     }
00751     ShowQueryString(str, STR_GROUP_RENAME_CAPTION, MAX_LENGTH_GROUP_NAME_CHARS, this, CS_ALPHANUMERAL, QSF_ENABLE_DEFAULT | QSF_LEN_IN_CHARS);
00752   }
00753 
00759   void UnselectVehicle(VehicleID vehicle)
00760   {
00761     if (this->vehicle_sel == vehicle) ResetObjectToPlace();
00762   }
00763 };
00764 
00765 
00766 static WindowDesc _other_group_desc(
00767   WDP_AUTO, 460, 246,
00768   WC_INVALID, WC_NONE,
00769   WDF_UNCLICK_BUTTONS,
00770   _nested_group_widgets, lengthof(_nested_group_widgets)
00771 );
00772 
00773 static const WindowDesc _train_group_desc(
00774   WDP_AUTO, 525, 246,
00775   WC_TRAINS_LIST, WC_NONE,
00776   WDF_UNCLICK_BUTTONS,
00777   _nested_group_widgets, lengthof(_nested_group_widgets)
00778 );
00779 
00785 void ShowCompanyGroup(CompanyID company, VehicleType vehicle_type)
00786 {
00787   if (!Company::IsValidID(company)) return;
00788 
00789   WindowNumber num = VehicleListIdentifier(VL_GROUP_LIST, vehicle_type, company).Pack();
00790   if (vehicle_type == VEH_TRAIN) {
00791     AllocateWindowDescFront<VehicleGroupWindow>(&_train_group_desc, num);
00792   } else {
00793     _other_group_desc.cls = GetWindowClassForVehicleType(vehicle_type);
00794     AllocateWindowDescFront<VehicleGroupWindow>(&_other_group_desc, num);
00795   }
00796 }
00797 
00804 static inline VehicleGroupWindow *FindVehicleGroupWindow(VehicleType vt, Owner owner)
00805 {
00806   return (VehicleGroupWindow *)FindWindowById(GetWindowClassForVehicleType(vt), VehicleListIdentifier(VL_GROUP_LIST, vt, owner).Pack());
00807 }
00808 
00817 void CcCreateGroup(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
00818 {
00819   if (result.Failed()) return;
00820   assert(p1 <= VEH_AIRCRAFT);
00821 
00822   VehicleGroupWindow *w = FindVehicleGroupWindow((VehicleType)p1, _current_company);
00823   if (w != NULL) w->ShowRenameGroupWindow(_new_group_id, true);
00824 }
00825 
00830 void DeleteGroupHighlightOfVehicle(const Vehicle *v)
00831 {
00832   /* If we haven't got any vehicles on the mouse pointer, we haven't got any highlighted in any group windows either
00833    * If that is the case, we can skip looping though the windows and save time
00834    */
00835   if (_special_mouse_mode != WSM_DRAGDROP) return;
00836 
00837   VehicleGroupWindow *w = FindVehicleGroupWindow(v->type, v->owner);
00838   if (w != NULL) w->UnselectVehicle(v->index);
00839 }