group_gui.cpp

Go to the documentation of this file.
00001 /* $Id: group_gui.cpp 18588 2009-12-21 16:24:29Z 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 "window_gui.h"
00015 #include "textbuf_gui.h"
00016 #include "command_func.h"
00017 #include "vehicle_gui.h"
00018 #include "vehicle_base.h"
00019 #include "group.h"
00020 #include "strings_func.h"
00021 #include "window_func.h"
00022 #include "vehicle_func.h"
00023 #include "autoreplace_gui.h"
00024 #include "gfx_func.h"
00025 #include "company_func.h"
00026 #include "widgets/dropdown_type.h"
00027 #include "widgets/dropdown_func.h"
00028 #include "tilehighlight_func.h"
00029 #include "vehicle_gui_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_CRITERIAP),
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)
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         extern void CcCreateGroup(bool success, TileIndex tile, uint32 p1, uint32 p2);
00489         DoCommandP(0, this->vehicle_type, 0, CMD_CREATE_GROUP | CMD_MSG(STR_ERROR_GROUP_CAN_T_CREATE), CcCreateGroup);
00490         break;
00491       }
00492 
00493       case GRP_WIDGET_DELETE_GROUP: { // Delete the selected group
00494         GroupID group = this->group_sel;
00495         this->group_sel = ALL_GROUP;
00496 
00497         DoCommandP(0, group, 0, CMD_DELETE_GROUP | CMD_MSG(STR_ERROR_GROUP_CAN_T_DELETE));
00498         break;
00499       }
00500 
00501       case GRP_WIDGET_RENAME_GROUP: // Rename the selected roup
00502         this->ShowRenameGroupWindow(this->group_sel);
00503         break;
00504 
00505       case GRP_WIDGET_AVAILABLE_VEHICLES:
00506         ShowBuildVehicleWindow(INVALID_TILE, this->vehicle_type);
00507         break;
00508 
00509       case GRP_WIDGET_MANAGE_VEHICLES_DROPDOWN:
00510         ShowGroupActionDropdown(this, this->group_sel);
00511         break;
00512 
00513       case GRP_WIDGET_START_ALL:
00514       case GRP_WIDGET_STOP_ALL: { // Start/stop all vehicles of the list
00515         DoCommandP(0, this->group_sel, ((IsAllGroupID(this->group_sel) ? VLW_STANDARD : VLW_GROUP_LIST) & VLW_MASK)
00516                           | (1 << 6)
00517                           | (widget == GRP_WIDGET_START_ALL ? (1 << 5) : 0)
00518                           | this->vehicle_type, CMD_MASS_START_STOP);
00519 
00520         break;
00521       }
00522 
00523       case GRP_WIDGET_REPLACE_PROTECTION: {
00524         const Group *g = Group::GetIfValid(this->group_sel);
00525         if (g != NULL) {
00526           DoCommandP(0, this->group_sel, !g->replace_protection, CMD_SET_GROUP_REPLACE_PROTECTION);
00527         }
00528         break;
00529       }
00530     }
00531   }
00532 
00533   virtual void OnDragDrop(Point pt, int widget)
00534   {
00535     switch (widget) {
00536       case GRP_WIDGET_ALL_VEHICLES: // All vehicles
00537       case GRP_WIDGET_DEFAULT_VEHICLES: // Ungrouped vehicles
00538         DoCommandP(0, DEFAULT_GROUP, this->vehicle_sel, CMD_ADD_VEHICLE_GROUP | CMD_MSG(STR_ERROR_GROUP_CAN_T_ADD_VEHICLE));
00539 
00540         this->vehicle_sel = INVALID_VEHICLE;
00541 
00542         this->SetDirty();
00543         break;
00544 
00545       case GRP_WIDGET_LIST_GROUP: { // Maxtrix group
00546         uint16 id_g = (pt.y - this->GetWidget<NWidgetBase>(GRP_WIDGET_LIST_GROUP)->pos_y) / (int)this->tiny_step_height;
00547         const VehicleID vindex = this->vehicle_sel;
00548 
00549         this->vehicle_sel = INVALID_VEHICLE;
00550 
00551         this->SetDirty();
00552 
00553         if (id_g >= this->vscroll2.GetCapacity()) return;
00554 
00555         id_g += this->vscroll2.GetPosition();
00556 
00557         if (id_g >= this->groups.Length()) return;
00558 
00559         DoCommandP(0, this->groups[id_g]->index, vindex, CMD_ADD_VEHICLE_GROUP | CMD_MSG(STR_ERROR_GROUP_CAN_T_ADD_VEHICLE));
00560         break;
00561       }
00562 
00563       case GRP_WIDGET_LIST_VEHICLE: { // Maxtrix vehicle
00564         uint32 id_v = (pt.y - this->GetWidget<NWidgetBase>(GRP_WIDGET_LIST_VEHICLE)->pos_y) / (int)this->resize.step_height;
00565         const VehicleID vindex = this->vehicle_sel;
00566 
00567         this->vehicle_sel = INVALID_VEHICLE;
00568 
00569         this->SetDirty();
00570 
00571         if (id_v >= this->vscroll.GetCapacity()) return; // click out of bounds
00572 
00573         id_v += this->vscroll.GetPosition();
00574 
00575         if (id_v >= this->vehicles.Length()) return; // click out of list bound
00576 
00577         const Vehicle *v = this->vehicles[id_v];
00578         if (vindex == v->index) {
00579           ShowVehicleViewWindow(v);
00580         }
00581         break;
00582       }
00583     }
00584     _cursor.vehchain = false;
00585   }
00586 
00587   virtual void OnQueryTextFinished(char *str)
00588   {
00589     if (str != NULL) DoCommandP(0, this->group_rename, 0, CMD_RENAME_GROUP | CMD_MSG(STR_ERROR_GROUP_CAN_T_RENAME), NULL, str);
00590     this->group_rename = INVALID_GROUP;
00591   }
00592 
00593   virtual void OnResize()
00594   {
00595     NWidgetCore *nwi = this->GetWidget<NWidgetCore>(GRP_WIDGET_LIST_GROUP);
00596     this->vscroll2.SetCapacity(nwi->current_y / this->tiny_step_height);
00597     nwi->widget_data = (this->vscroll2.GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
00598 
00599     nwi = this->GetWidget<NWidgetCore>(GRP_WIDGET_LIST_VEHICLE);
00600     this->vscroll.SetCapacityFromWidget(this, GRP_WIDGET_LIST_VEHICLE);
00601     nwi->widget_data = (this->vscroll.GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
00602   }
00603 
00604   virtual void OnDropdownSelect(int widget, int index)
00605   {
00606     switch (widget) {
00607       case GRP_WIDGET_SORT_BY_DROPDOWN:
00608         this->vehicles.SetSortType(index);
00609         break;
00610 
00611       case GRP_WIDGET_MANAGE_VEHICLES_DROPDOWN:
00612         assert(this->vehicles.Length() != 0);
00613 
00614         switch (index) {
00615           case GALF_REPLACE: // Replace window
00616             ShowReplaceGroupVehicleWindow(this->group_sel, this->vehicle_type);
00617             break;
00618           case GALF_SERVICE: // Send for servicing
00619             DoCommandP(0, this->group_sel, ((IsAllGroupID(this->group_sel) ? VLW_STANDARD : VLW_GROUP_LIST) & VLW_MASK)
00620                   | DEPOT_MASS_SEND
00621                   | DEPOT_SERVICE, GetCmdSendToDepot(this->vehicle_type));
00622             break;
00623           case GALF_DEPOT: // Send to Depots
00624             DoCommandP(0, this->group_sel, ((IsAllGroupID(this->group_sel) ? VLW_STANDARD : VLW_GROUP_LIST) & VLW_MASK)
00625                   | DEPOT_MASS_SEND, GetCmdSendToDepot(this->vehicle_type));
00626             break;
00627           case GALF_ADD_SHARED: // Add shared Vehicles
00628             assert(Group::IsValidID(this->group_sel));
00629 
00630             DoCommandP(0, this->group_sel, this->vehicle_type, CMD_ADD_SHARED_VEHICLE_GROUP | CMD_MSG(STR_ERROR_GROUP_CAN_T_ADD_SHARED_VEHICLE));
00631             break;
00632           case GALF_REMOVE_ALL: // Remove all Vehicles from the selected group
00633             assert(Group::IsValidID(this->group_sel));
00634 
00635             DoCommandP(0, this->group_sel, this->vehicle_type, CMD_REMOVE_ALL_VEHICLES_GROUP | CMD_MSG(STR_ERROR_GROUP_CAN_T_REMOVE_ALL_VEHICLES));
00636             break;
00637           default: NOT_REACHED();
00638         }
00639         break;
00640 
00641       default: NOT_REACHED();
00642     }
00643 
00644     this->SetDirty();
00645   }
00646 
00647   virtual void OnTick()
00648   {
00649     if (_pause_mode != PM_UNPAUSED) return;
00650     if (this->groups.NeedResort() || this->vehicles.NeedResort()) {
00651       this->SetDirty();
00652     }
00653   }
00654 
00655   virtual void OnPlaceObjectAbort()
00656   {
00657     /* abort drag & drop */
00658     this->vehicle_sel = INVALID_VEHICLE;
00659     this->SetWidgetDirty(GRP_WIDGET_LIST_VEHICLE);
00660   }
00661 
00662   void ShowRenameGroupWindow(GroupID group)
00663   {
00664     assert(Group::IsValidID(group));
00665     this->group_rename = group;
00666     SetDParam(0, group);
00667     ShowQueryString(STR_GROUP_NAME, STR_GROUP_RENAME_CAPTION, MAX_LENGTH_GROUP_NAME_BYTES, MAX_LENGTH_GROUP_NAME_PIXELS, this, CS_ALPHANUMERAL, QSF_ENABLE_DEFAULT);
00668   }
00669 
00675   void UnselectVehicle(VehicleID vehicle)
00676   {
00677     if (this->vehicle_sel == vehicle) ResetObjectToPlace();
00678   }
00679 };
00680 
00681 
00682 static WindowDesc _other_group_desc(
00683   WDP_AUTO, 460, 246,
00684   WC_INVALID, WC_NONE,
00685   WDF_UNCLICK_BUTTONS,
00686   _nested_group_widgets, lengthof(_nested_group_widgets)
00687 );
00688 
00689 const static WindowDesc _train_group_desc(
00690   WDP_AUTO, 525, 246,
00691   WC_TRAINS_LIST, WC_NONE,
00692   WDF_UNCLICK_BUTTONS,
00693   _nested_group_widgets, lengthof(_nested_group_widgets)
00694 );
00695 
00696 void ShowCompanyGroup(CompanyID company, VehicleType vehicle_type)
00697 {
00698   if (!Company::IsValidID(company)) return;
00699 
00700   WindowNumber num = (vehicle_type << 11) | VLW_GROUP_LIST | company;
00701   if (vehicle_type == VEH_TRAIN) {
00702     AllocateWindowDescFront<VehicleGroupWindow>(&_train_group_desc, num);
00703   } else {
00704     _other_group_desc.cls = GetWindowClassForVehicleType(vehicle_type);
00705     AllocateWindowDescFront<VehicleGroupWindow>(&_other_group_desc, num);
00706   }
00707 }
00708 
00715 static inline VehicleGroupWindow *FindVehicleGroupWindow(VehicleType vt, Owner owner)
00716 {
00717   return (VehicleGroupWindow *)FindWindowById(GetWindowClassForVehicleType(vt), (vt << 11) | VLW_GROUP_LIST | owner);
00718 }
00719 
00728 void CcCreateGroup(bool success, TileIndex tile, uint32 p1, uint32 p2)
00729 {
00730   if (!success) return;
00731   assert(p1 <= VEH_AIRCRAFT);
00732 
00733   VehicleGroupWindow *w = FindVehicleGroupWindow((VehicleType)p1, _current_company);
00734   if (w != NULL) w->ShowRenameGroupWindow(_new_group_id);
00735 }
00736 
00741 void DeleteGroupHighlightOfVehicle(const Vehicle *v)
00742 {
00743   /* If we haven't got any vehicles on the mouse pointer, we haven't got any highlighted in any group windows either
00744    * If that is the case, we can skip looping though the windows and save time
00745    */
00746   if (_special_mouse_mode != WSM_DRAGDROP) return;
00747 
00748   VehicleGroupWindow *w = FindVehicleGroupWindow(v->type, v->owner);
00749   if (w != NULL) w->UnselectVehicle(v->index);
00750 }

Generated on Tue Jan 5 21:02:54 2010 for OpenTTD by  doxygen 1.5.6