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