# group_gui.cpp

Go to the documentation of this file.

    00001 /* $Id: group_gui.cpp 19132 2010-02-14 16:31:35Z alberth $ */
    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(0x0, 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     this->GetWidget<NWidgetCore>(GRP_WIDGET_AVAILABLE_VEHICLES)->widget_data = STR_VEHICLE_LIST_AVAILABLE_TRAINS + this->vehicle_type;
    00230
    00231     this->GetWidget<NWidgetCore>(GRP_WIDGET_CREATE_GROUP)->widget_data += this->vehicle_type;
    00232     this->GetWidget<NWidgetCore>(GRP_WIDGET_RENAME_GROUP)->widget_data += this->vehicle_type;
    00233     this->GetWidget<NWidgetCore>(GRP_WIDGET_DELETE_GROUP)->widget_data += this->vehicle_type;
    00234     this->GetWidget<NWidgetCore>(GRP_WIDGET_REPLACE_PROTECTION)->widget_data += this->vehicle_type;
    00235
    00236     this->FinishInitNested(desc, window_number);
    00237     this->owner = owner;
    00238   }
    00239
    00240   ~VehicleGroupWindow()
    00241   {
    00242     *this->sorting = this->vehicles.GetListing();
    00243   }
    00244
    00245   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
    00246   {
    00247     switch (widget) {
    00248       case GRP_WIDGET_LIST_GROUP:
    00249         this->tiny_step_height = FONT_HEIGHT_NORMAL + WD_MATRIX_TOP;
    00250         resize->height = this->tiny_step_height;
    00251         /* Minimum height is the height of the list widget minus all and default vehicles and a bit for the bottom bar */
    00252         size->height =  4 * GetVehicleListHeight(this->vehicle_type, this->tiny_step_height) - (this->tiny_step_height > 25 ? 2 : 3) * this->tiny_step_height;
    00253         break;
    00254
    00255       case GRP_WIDGET_ALL_VEHICLES:
    00256       case GRP_WIDGET_DEFAULT_VEHICLES:
    00257         size->height = FONT_HEIGHT_NORMAL + WD_MATRIX_TOP;
    00258         size->width = max(GetStringBoundingBox(STR_GROUP_DEFAULT_TRAINS + this->vehicle_type).width, GetStringBoundingBox(STR_GROUP_ALL_TRAINS + this->vehicle_type).width);
    00259         size->width += WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT + 8 + 8;
    00260         break;
    00261
    00262       case GRP_WIDGET_LIST_VEHICLE:
    00263         resize->height = GetVehicleListHeight(this->vehicle_type, FONT_HEIGHT_NORMAL + WD_MATRIX_TOP);
    00264         size->height = 4 * resize->height;
    00265         break;
    00266
    00267       case GRP_WIDGET_MANAGE_VEHICLES_DROPDOWN: {
    00268         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};
    00269         Dimension d = {0, 0};
    00270         for (const StringID *sid = _dropdown_text; sid != endof(_dropdown_text); sid++) {
    00271           d = maxdim(d, GetStringBoundingBox(*sid));
    00272         }
    00273         d.height += padding.height;
    00274         d.width  += padding.width;
    00275         *size = maxdim(*size, d);
    00276       } break;
    00277     }
    00278   }
    00279
    00280   virtual void OnInvalidateData(int data)
    00281   {
    00282     if (data == 0) {
    00283       this->vehicles.ForceRebuild();
    00284       this->groups.ForceRebuild();
    00285     } else {
    00286       this->vehicles.ForceResort();
    00287       this->groups.ForceResort();
    00288     }
    00289
    00290     if (this->group_rename != INVALID_GROUP && !Group::IsValidID(this->group_rename)) {
    00291       DeleteWindowByClass(WC_QUERY_STRING);
    00292       this->group_rename = INVALID_GROUP;
    00293     }
    00294
    00295     if (!(IsAllGroupID(this->group_sel) || IsDefaultGroupID(this->group_sel) || Group::IsValidID(this->group_sel))) {
    00296       this->group_sel = ALL_GROUP;
    00297       HideDropDownMenu(this);
    00298     }
    00299     this->SetDirty();
    00300   }
    00301
    00302   virtual void SetStringParameters(int widget) const
    00303   {
    00304     if (widget != GRP_WIDGET_CAPTION) return;
    00305
    00306     /* If selected_group == DEFAULT_GROUP || ALL_GROUP, draw the standard caption
    00307      * We list all vehicles or ungrouped vehicles */
    00308     if (IsDefaultGroupID(this->group_sel) || IsAllGroupID(this->group_sel)) {
    00309       SetDParam(0, STR_COMPANY_NAME);
    00310       SetDParam(1, GB(this->window_number, 0, 8));
    00311       SetDParam(2, this->vehicles.Length());
    00312       SetDParam(3, this->vehicles.Length());
    00313     } else {
    00314       const Group *g = Group::Get(this->group_sel);
    00315
    00316       SetDParam(0, STR_GROUP_NAME);
    00317       SetDParam(1, g->index);
    00318       SetDParam(2, g->num_vehicle);
    00319       SetDParam(3, g->num_vehicle);
    00320     }
    00321   }
    00322
    00323   virtual void OnPaint()
    00324   {
    00325     const Owner owner = (Owner)GB(this->window_number, 0, 8);
    00326
    00327     /* If we select the all vehicles, this->list will contain all vehicles of the owner
    00328      * else this->list will contain all vehicles which belong to the selected group */
    00329     this->BuildVehicleList(owner, this->group_sel, IsAllGroupID(this->group_sel) ? VLW_STANDARD : VLW_GROUP_LIST);
    00330     this->SortVehicleList();
    00331
    00332     this->BuildGroupList(owner);
    00333     this->groups.Sort(&GroupNameSorter);
    00334
    00335     this->vscroll2.SetCount(this->groups.Length());
    00336     this->vscroll.SetCount(this->vehicles.Length());
    00337
    00338     /* The drop down menu is out, *but* it may not be used, retract it. */
    00339     if (this->vehicles.Length() == 0 && this->IsWidgetLowered(GRP_WIDGET_MANAGE_VEHICLES_DROPDOWN)) {
    00340       this->RaiseWidget(GRP_WIDGET_MANAGE_VEHICLES_DROPDOWN);
    00341       HideDropDownMenu(this);
    00342     }
    00343
    00344     /* Disable all lists management button when the list is empty */
    00345     this->SetWidgetsDisabledState(this->vehicles.Length() == 0 || _local_company != owner,
    00346         GRP_WIDGET_STOP_ALL,
    00347         GRP_WIDGET_START_ALL,
    00348         GRP_WIDGET_MANAGE_VEHICLES_DROPDOWN,
    00349         WIDGET_LIST_END);
    00350
    00351     /* Disable the group specific function when we select the default group or all vehicles */
    00352     this->SetWidgetsDisabledState(IsDefaultGroupID(this->group_sel) || IsAllGroupID(this->group_sel) || _local_company != owner,
    00353         GRP_WIDGET_DELETE_GROUP,
    00354         GRP_WIDGET_RENAME_GROUP,
    00355         GRP_WIDGET_REPLACE_PROTECTION,
    00356         WIDGET_LIST_END);
    00357
    00358     /* Disable remaining buttons for non-local companies
    00359      * Needed while changing _local_company, eg. by cheats
    00360      * All procedures (eg. move vehicle to another group)
    00361      *  verify, whether you are the owner of the vehicle,
    00362      *  so it doesn't have to be disabled
    00363      */
    00364     this->SetWidgetsDisabledState(_local_company != owner,
    00365         GRP_WIDGET_CREATE_GROUP,
    00366         GRP_WIDGET_AVAILABLE_VEHICLES,
    00367         WIDGET_LIST_END);
    00368
    00369     /* If not a default group and the group has replace protection, show an enabled replace sprite. */
    00370     uint16 protect_sprite = SPR_GROUP_REPLACE_OFF_TRAIN;
    00371     if (!IsDefaultGroupID(this->group_sel) && !IsAllGroupID(this->group_sel) && Group::Get(this->group_sel)->replace_protection) protect_sprite = SPR_GROUP_REPLACE_ON_TRAIN;
    00372     this->GetWidget<NWidgetCore>(GRP_WIDGET_REPLACE_PROTECTION)->widget_data = protect_sprite + this->vehicle_type;
    00373
    00374     /* Set text of sort by dropdown */
    00375     this->GetWidget<NWidgetCore>(GRP_WIDGET_SORT_BY_DROPDOWN)->widget_data = this->vehicle_sorter_names[this->vehicles.SortType()];
    00376
    00377     this->DrawWidgets();
    00378   }
    00379
    00380   virtual void DrawWidget(const Rect &r, int widget) const
    00381   {
    00382     switch (widget) {
    00383       case GRP_WIDGET_ALL_VEHICLES:
    00384         DrawString(r.left + WD_FRAMERECT_LEFT + 8, r.right - WD_FRAMERECT_RIGHT - 8, r.top + WD_MATRIX_TOP,
    00385             STR_GROUP_ALL_TRAINS + this->vehicle_type, IsAllGroupID(this->group_sel) ? TC_WHITE : TC_BLACK);
    00386         break;
    00387
    00388       case GRP_WIDGET_DEFAULT_VEHICLES:
    00389         DrawString(r.left + WD_FRAMERECT_LEFT + 8, r.right - WD_FRAMERECT_RIGHT - 8, r.top + WD_MATRIX_TOP,
    00390             STR_GROUP_DEFAULT_TRAINS + this->vehicle_type, IsDefaultGroupID(this->group_sel) ? TC_WHITE : TC_BLACK);
    00391         break;
    00392
    00393       case GRP_WIDGET_LIST_GROUP: {
    00394         int y1 = r.top + WD_FRAMERECT_TOP + 1;
    00395         int max = min(this->vscroll2.GetPosition() + this->vscroll2.GetCapacity(), this->groups.Length());
    00396         for (int i = this->vscroll2.GetPosition(); i < max; ++i) {
    00397           const Group *g = this->groups[i];
    00398
    00399           assert(g->owner == this->owner);
    00400
    00401           /* draw the selected group in white, else we draw it in black */
    00402           SetDParam(0, g->index);
    00403           DrawString(r.left + WD_FRAMERECT_LEFT + 8, r.right - WD_FRAMERECT_RIGHT, y1, STR_GROUP_NAME, (this->group_sel == g->index) ? TC_WHITE : TC_BLACK);
    00404
    00405           /* draw the number of vehicles of the group */
    00406           SetDParam(0, g->num_vehicle);
    00407           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);
    00408
    00409           y1 += this->tiny_step_height;
    00410         }
    00411         break;
    00412       }
    00413
    00414       case GRP_WIDGET_SORT_BY_ORDER:
    00415         this->DrawSortButtonState(GRP_WIDGET_SORT_BY_ORDER, this->vehicles.IsDescSortOrder() ? SBS_DOWN : SBS_UP);
    00416         break;
    00417
    00418       case GRP_WIDGET_LIST_VEHICLE:
    00419         this->DrawVehicleListItems(this->vehicle_sel, this->resize.step_height, r);
    00420         break;
    00421     }
    00422   }
    00423
    00424   virtual void OnClick(Point pt, int widget, int click_count)
    00425   {
    00426     switch (widget) {
    00427       case GRP_WIDGET_SORT_BY_ORDER: // Flip sorting method ascending/descending
    00428         this->vehicles.ToggleSortOrder();
    00429         this->SetDirty();
    00430         break;
    00431
    00432       case GRP_WIDGET_SORT_BY_DROPDOWN: // Select sorting criteria dropdown menu
    00433         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));
    00434         return;
    00435
    00436       case GRP_WIDGET_ALL_VEHICLES: // All vehicles button
    00437         if (!IsAllGroupID(this->group_sel)) {
    00438           this->group_sel = ALL_GROUP;
    00439           this->vehicles.ForceRebuild();
    00440           this->SetDirty();
    00441         }
    00442         break;
    00443
    00444       case GRP_WIDGET_DEFAULT_VEHICLES: // Ungrouped vehicles button
    00445         if (!IsDefaultGroupID(this->group_sel)) {
    00446           this->group_sel = DEFAULT_GROUP;
    00447           this->vehicles.ForceRebuild();
    00448           this->SetDirty();
    00449         }
    00450         break;
    00451
    00452       case GRP_WIDGET_LIST_GROUP: { // Matrix Group
    00453         uint16 id_g = (pt.y - this->GetWidget<NWidgetBase>(GRP_WIDGET_LIST_GROUP)->pos_y) / (int)this->tiny_step_height;
    00454
    00455         if (id_g >= this->vscroll2.GetCapacity()) return;
    00456
    00457         id_g += this->vscroll2.GetPosition();
    00458
    00459         if (id_g >= this->groups.Length()) return;
    00460
    00461         this->group_sel = this->groups[id_g]->index;
    00462
    00463         this->vehicles.ForceRebuild();
    00464         this->SetDirty();
    00465         break;
    00466       }
    00467
    00468       case GRP_WIDGET_LIST_VEHICLE: { // Matrix Vehicle
    00469         uint32 id_v = (pt.y - this->GetWidget<NWidgetBase>(GRP_WIDGET_LIST_VEHICLE)->pos_y) / (int)this->resize.step_height;
    00470         if (id_v >= this->vscroll.GetCapacity()) return; // click out of bounds
    00471
    00472         id_v += this->vscroll.GetPosition();
    00473
    00474         if (id_v >= this->vehicles.Length()) return; // click out of list bound
    00475
    00476         const Vehicle *v = this->vehicles[id_v];
    00477
    00478         this->vehicle_sel = v->index;
    00479
    00480         SetObjectToPlaceWnd(v->GetImage(DIR_W), GetVehiclePalette(v), HT_DRAG, this);
    00481         _cursor.vehchain = true;
    00482
    00483         this->SetDirty();
    00484         break;
    00485       }
    00486
    00487       case GRP_WIDGET_CREATE_GROUP: { // Create a new group
    00488         DoCommandP(0, this->vehicle_type, 0, CMD_CREATE_GROUP | CMD_MSG(STR_ERROR_GROUP_CAN_T_CREATE), CcCreateGroup);
    00489         break;
    00490       }
    00491
    00492       case GRP_WIDGET_DELETE_GROUP: { // Delete the selected group
    00493         GroupID group = this->group_sel;
    00494         this->group_sel = ALL_GROUP;
    00495
    00496         DoCommandP(0, group, 0, CMD_DELETE_GROUP | CMD_MSG(STR_ERROR_GROUP_CAN_T_DELETE));
    00497         break;
    00498       }
    00499
    00500       case GRP_WIDGET_RENAME_GROUP: // Rename the selected roup
    00501         this->ShowRenameGroupWindow(this->group_sel, false);
    00502         break;
    00503
    00504       case GRP_WIDGET_AVAILABLE_VEHICLES:
    00505         ShowBuildVehicleWindow(INVALID_TILE, this->vehicle_type);
    00506         break;
    00507
    00508       case GRP_WIDGET_MANAGE_VEHICLES_DROPDOWN:
    00509         ShowGroupActionDropdown(this, this->group_sel);
    00510         break;
    00511
    00512       case GRP_WIDGET_START_ALL:
    00513       case GRP_WIDGET_STOP_ALL: { // Start/stop all vehicles of the list
    00514         DoCommandP(0, this->group_sel, ((IsAllGroupID(this->group_sel) ? VLW_STANDARD : VLW_GROUP_LIST) & VLW_MASK)
    00515                           | (1 << 6)
    00516                           | (widget == GRP_WIDGET_START_ALL ? (1 << 5) : 0)
    00517                           | this->vehicle_type, CMD_MASS_START_STOP);
    00518
    00519         break;
    00520       }
    00521
    00522       case GRP_WIDGET_REPLACE_PROTECTION: {
    00523         const Group *g = Group::GetIfValid(this->group_sel);
    00524         if (g != NULL) {
    00525           DoCommandP(0, this->group_sel, !g->replace_protection, CMD_SET_GROUP_REPLACE_PROTECTION);
    00526         }
    00527         break;
    00528       }
    00529     }
    00530   }
    00531
    00532   virtual void OnDragDrop(Point pt, int widget)
    00533   {
    00534     switch (widget) {
    00535       case GRP_WIDGET_ALL_VEHICLES: // All vehicles
    00536       case GRP_WIDGET_DEFAULT_VEHICLES: // Ungrouped vehicles
    00537         DoCommandP(0, DEFAULT_GROUP, this->vehicle_sel, CMD_ADD_VEHICLE_GROUP | CMD_MSG(STR_ERROR_GROUP_CAN_T_ADD_VEHICLE));
    00538
    00539         this->vehicle_sel = INVALID_VEHICLE;
    00540
    00541         this->SetDirty();
    00542         break;
    00543
    00544       case GRP_WIDGET_LIST_GROUP: { // Maxtrix group
    00545         uint16 id_g = (pt.y - this->GetWidget<NWidgetBase>(GRP_WIDGET_LIST_GROUP)->pos_y) / (int)this->tiny_step_height;
    00546         const VehicleID vindex = this->vehicle_sel;
    00547
    00548         this->vehicle_sel = INVALID_VEHICLE;
    00549
    00550         this->SetDirty();
    00551
    00552         if (id_g >= this->vscroll2.GetCapacity()) return;
    00553
    00554         id_g += this->vscroll2.GetPosition();
    00555
    00556         if (id_g >= this->groups.Length()) return;
    00557
    00558         DoCommandP(0, this->groups[id_g]->index, vindex, CMD_ADD_VEHICLE_GROUP | CMD_MSG(STR_ERROR_GROUP_CAN_T_ADD_VEHICLE));
    00559         break;
    00560       }
    00561
    00562       case GRP_WIDGET_LIST_VEHICLE: { // Maxtrix vehicle
    00563         uint32 id_v = (pt.y - this->GetWidget<NWidgetBase>(GRP_WIDGET_LIST_VEHICLE)->pos_y) / (int)this->resize.step_height;
    00564         const VehicleID vindex = this->vehicle_sel;
    00565
    00566         this->vehicle_sel = INVALID_VEHICLE;
    00567
    00568         this->SetDirty();
    00569
    00570         if (id_v >= this->vscroll.GetCapacity()) return; // click out of bounds
    00571
    00572         id_v += this->vscroll.GetPosition();
    00573
    00574         if (id_v >= this->vehicles.Length()) return; // click out of list bound
    00575
    00576         const Vehicle *v = this->vehicles[id_v];
    00577         if (vindex == v->index) {
    00578           ShowVehicleViewWindow(v);
    00579         }
    00580         break;
    00581       }
    00582     }
    00583     _cursor.vehchain = false;
    00584   }
    00585
    00586   virtual void OnQueryTextFinished(char *str)
    00587   {
    00588     if (str != NULL) DoCommandP(0, this->group_rename, 0, CMD_RENAME_GROUP | CMD_MSG(STR_ERROR_GROUP_CAN_T_RENAME), NULL, str);
    00589     this->group_rename = INVALID_GROUP;
    00590   }
    00591
    00592   virtual void OnResize()
    00593   {
    00594     NWidgetCore *nwi = this->GetWidget<NWidgetCore>(GRP_WIDGET_LIST_GROUP);
    00595     this->vscroll2.SetCapacity(nwi->current_y / this->tiny_step_height);
    00596     nwi->widget_data = (this->vscroll2.GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
    00597
    00598     nwi = this->GetWidget<NWidgetCore>(GRP_WIDGET_LIST_VEHICLE);
    00599     this->vscroll.SetCapacityFromWidget(this, GRP_WIDGET_LIST_VEHICLE);
    00600     nwi->widget_data = (this->vscroll.GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
    00601   }
    00602
    00603   virtual void OnDropdownSelect(int widget, int index)
    00604   {
    00605     switch (widget) {
    00606       case GRP_WIDGET_SORT_BY_DROPDOWN:
    00607         this->vehicles.SetSortType(index);
    00608         break;
    00609
    00610       case GRP_WIDGET_MANAGE_VEHICLES_DROPDOWN:
    00611         assert(this->vehicles.Length() != 0);
    00612
    00613         switch (index) {
    00614           case GALF_REPLACE: // Replace window
    00615             ShowReplaceGroupVehicleWindow(this->group_sel, this->vehicle_type);
    00616             break;
    00617           case GALF_SERVICE: // Send for servicing
    00618             DoCommandP(0, this->group_sel, ((IsAllGroupID(this->group_sel) ? VLW_STANDARD : VLW_GROUP_LIST) & VLW_MASK)
    00619                   | DEPOT_MASS_SEND
    00620                   | DEPOT_SERVICE, GetCmdSendToDepot(this->vehicle_type));
    00621             break;
    00622           case GALF_DEPOT: // Send to Depots
    00623             DoCommandP(0, this->group_sel, ((IsAllGroupID(this->group_sel) ? VLW_STANDARD : VLW_GROUP_LIST) & VLW_MASK)
    00624                   | DEPOT_MASS_SEND, GetCmdSendToDepot(this->vehicle_type));
    00625             break;
    00626           case GALF_ADD_SHARED: // Add shared Vehicles
    00627             assert(Group::IsValidID(this->group_sel));
    00628
    00629             DoCommandP(0, this->group_sel, this->vehicle_type, CMD_ADD_SHARED_VEHICLE_GROUP | CMD_MSG(STR_ERROR_GROUP_CAN_T_ADD_SHARED_VEHICLE));
    00630             break;
    00631           case GALF_REMOVE_ALL: // Remove all Vehicles from the selected group
    00632             assert(Group::IsValidID(this->group_sel));
    00633
    00634             DoCommandP(0, this->group_sel, this->vehicle_type, CMD_REMOVE_ALL_VEHICLES_GROUP | CMD_MSG(STR_ERROR_GROUP_CAN_T_REMOVE_ALL_VEHICLES));
    00635             break;
    00636           default: NOT_REACHED();
    00637         }
    00638         break;
    00639
    00640       default: NOT_REACHED();
    00641     }
    00642
    00643     this->SetDirty();
    00644   }
    00645
    00646   virtual void OnTick()
    00647   {
    00648     if (_pause_mode != PM_UNPAUSED) return;
    00649     if (this->groups.NeedResort() || this->vehicles.NeedResort()) {
    00650       this->SetDirty();
    00651     }
    00652   }
    00653
    00654   virtual void OnPlaceObjectAbort()
    00655   {
    00656     /* abort drag & drop */
    00657     this->vehicle_sel = INVALID_VEHICLE;
    00658     this->SetWidgetDirty(GRP_WIDGET_LIST_VEHICLE);
    00659   }
    00660
    00661   void ShowRenameGroupWindow(GroupID group, bool empty)
    00662   {
    00663     assert(Group::IsValidID(group));
    00664     this->group_rename = group;
    00665     /* Show empty query for new groups */
    00666     StringID str = STR_EMPTY;
    00667     if (!empty) {
    00668       SetDParam(0, group);
    00669       str = STR_GROUP_NAME;
    00670     }
    00671     ShowQueryString(str, STR_GROUP_RENAME_CAPTION, MAX_LENGTH_GROUP_NAME_BYTES, MAX_LENGTH_GROUP_NAME_PIXELS, this, CS_ALPHANUMERAL, QSF_ENABLE_DEFAULT);
    00672   }
    00673
    00679   void UnselectVehicle(VehicleID vehicle)
    00680   {
    00681     if (this->vehicle_sel == vehicle) ResetObjectToPlace();
    00682   }
    00683 };
    00684
    00685
    00686 static WindowDesc _other_group_desc(
    00687   WDP_AUTO, 460, 246,
    00688   WC_INVALID, WC_NONE,
    00689   WDF_UNCLICK_BUTTONS,
    00690   _nested_group_widgets, lengthof(_nested_group_widgets)
    00691 );
    00692
    00693 static const WindowDesc _train_group_desc(
    00694   WDP_AUTO, 525, 246,
    00695   WC_TRAINS_LIST, WC_NONE,
    00696   WDF_UNCLICK_BUTTONS,
    00697   _nested_group_widgets, lengthof(_nested_group_widgets)
    00698 );
    00699
    00700 void ShowCompanyGroup(CompanyID company, VehicleType vehicle_type)
    00701 {
    00702   if (!Company::IsValidID(company)) return;
    00703
    00704   WindowNumber num = (vehicle_type << 11) | VLW_GROUP_LIST | company;
    00705   if (vehicle_type == VEH_TRAIN) {
    00706     AllocateWindowDescFront<VehicleGroupWindow>(&_train_group_desc, num);
    00707   } else {
    00708     _other_group_desc.cls = GetWindowClassForVehicleType(vehicle_type);
    00709     AllocateWindowDescFront<VehicleGroupWindow>(&_other_group_desc, num);
    00710   }
    00711 }
    00712
    00719 static inline VehicleGroupWindow *FindVehicleGroupWindow(VehicleType vt, Owner owner)
    00720 {
    00721   return (VehicleGroupWindow *)FindWindowById(GetWindowClassForVehicleType(vt), (vt << 11) | VLW_GROUP_LIST | owner);
    00722 }
    00723
    00732 void CcCreateGroup(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
    00733 {
    00734   if (result.Failed()) return;
    00735   assert(p1 <= VEH_AIRCRAFT);
    00736
    00737   VehicleGroupWindow *w = FindVehicleGroupWindow((VehicleType)p1, _current_company);
    00738   if (w != NULL) w->ShowRenameGroupWindow(_new_group_id, true);
    00739 }
    00740
    00745 void DeleteGroupHighlightOfVehicle(const Vehicle *v)
    00746 {
    00747   /* If we haven't got any vehicles on the mouse pointer, we haven't got any highlighted in any group windows either
    00748    * If that is the case, we can skip looping though the windows and save time
    00749    */
    00750   if (_special_mouse_mode != WSM_DRAGDROP) return;
    00751
    00752   VehicleGroupWindow *w = FindVehicleGroupWindow(v->type, v->owner);
    00753   if (w != NULL) w->UnselectVehicle(v->index);
    00754 }