group_gui.cpp

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