# group_gui.cpp

Go to the documentation of this file.

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