group_gui.cpp

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

Generated on Sun Jan 9 16:01:54 2011 for OpenTTD by  doxygen 1.6.1