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