depot_gui.cpp

Go to the documentation of this file.
00001 /* $Id$ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
00008  */
00009 
00012 #include "stdafx.h"
00013 #include "train.h"
00014 #include "ship.h"
00015 #include "aircraft.h"
00016 #include "gui.h"
00017 #include "textbuf_gui.h"
00018 #include "viewport_func.h"
00019 #include "command_func.h"
00020 #include "depot_base.h"
00021 #include "vehicle_gui.h"
00022 #include "spritecache.h"
00023 #include "strings_func.h"
00024 #include "window_func.h"
00025 #include "vehicle_func.h"
00026 #include "company_func.h"
00027 #include "tilehighlight_func.h"
00028 #include "window_gui.h"
00029 #include "vehiclelist.h"
00030 #include "order_backup.h"
00031 #include "zoom_func.h"
00032 
00033 #include "table/strings.h"
00034 
00035 /*
00036  * Since all depot window sizes aren't the same, we need to modify sizes a little.
00037  * It's done with the following arrays of widget indexes. Each of them tells if a widget side should be moved and in what direction.
00038  * How long they should be moved and for what window types are controlled in ShowDepotWindow()
00039  */
00040 
00041 /* Names of the widgets. Keep them in the same order as in the widget array */
00042 enum DepotWindowWidgets {
00043   DEPOT_WIDGET_CAPTION,
00044   DEPOT_WIDGET_SELL,
00045   DEPOT_WIDGET_SHOW_SELL_CHAIN,
00046   DEPOT_WIDGET_SELL_CHAIN,
00047   DEPOT_WIDGET_SELL_ALL,
00048   DEPOT_WIDGET_AUTOREPLACE,
00049   DEPOT_WIDGET_MATRIX,
00050   DEPOT_WIDGET_V_SCROLL, 
00051   DEPOT_WIDGET_SHOW_H_SCROLL,
00052   DEPOT_WIDGET_H_SCROLL, 
00053   DEPOT_WIDGET_BUILD,
00054   DEPOT_WIDGET_CLONE,
00055   DEPOT_WIDGET_LOCATION,
00056   DEPOT_WIDGET_SHOW_RENAME,
00057   DEPOT_WIDGET_RENAME,
00058   DEPOT_WIDGET_VEHICLE_LIST,
00059   DEPOT_WIDGET_STOP_ALL,
00060   DEPOT_WIDGET_START_ALL,
00061 };
00062 
00064 static const NWidgetPart _nested_train_depot_widgets[] = {
00065   NWidget(NWID_HORIZONTAL),
00066     NWidget(WWT_CLOSEBOX, COLOUR_GREY),
00067     NWidget(WWT_CAPTION, COLOUR_GREY, DEPOT_WIDGET_CAPTION), SetDataTip(STR_DEPOT_CAPTION, STR_NULL),
00068     NWidget(WWT_SHADEBOX, COLOUR_GREY),
00069     NWidget(WWT_STICKYBOX, COLOUR_GREY),
00070   EndContainer(),
00071   NWidget(NWID_HORIZONTAL),
00072     NWidget(NWID_VERTICAL),
00073       NWidget(WWT_MATRIX, COLOUR_GREY, DEPOT_WIDGET_MATRIX), SetDataTip(0x0, STR_NULL), SetResize(1, 1), SetScrollbar(DEPOT_WIDGET_V_SCROLL),
00074       NWidget(NWID_SELECTION, INVALID_COLOUR, DEPOT_WIDGET_SHOW_H_SCROLL),
00075         NWidget(NWID_HSCROLLBAR, COLOUR_GREY, DEPOT_WIDGET_H_SCROLL),
00076       EndContainer(),
00077     EndContainer(),
00078     NWidget(NWID_VERTICAL),
00079       NWidget(WWT_IMGBTN, COLOUR_GREY, DEPOT_WIDGET_SELL), SetDataTip(0x0, STR_NULL), SetResize(0, 1), SetFill(0, 1),
00080       NWidget(NWID_SELECTION, INVALID_COLOUR, DEPOT_WIDGET_SHOW_SELL_CHAIN),
00081         NWidget(WWT_IMGBTN, COLOUR_GREY, DEPOT_WIDGET_SELL_CHAIN), SetDataTip(SPR_SELL_CHAIN_TRAIN, STR_DEPOT_DRAG_WHOLE_TRAIN_TO_SELL_TOOLTIP), SetResize(0, 1), SetFill(0, 1),
00082       EndContainer(),
00083       NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, DEPOT_WIDGET_SELL_ALL), SetDataTip(0x0, STR_NULL),
00084       NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, DEPOT_WIDGET_AUTOREPLACE), SetDataTip(0x0, STR_NULL),
00085     EndContainer(),
00086     NWidget(NWID_VSCROLLBAR, COLOUR_GREY, DEPOT_WIDGET_V_SCROLL),
00087   EndContainer(),
00088   NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
00089     NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, DEPOT_WIDGET_BUILD), SetDataTip(0x0, STR_NULL), SetFill(1, 1), SetResize(1, 0),
00090     NWidget(WWT_TEXTBTN, COLOUR_GREY, DEPOT_WIDGET_CLONE), SetDataTip(0x0, STR_NULL), SetFill(1, 1), SetResize(1, 0),
00091     NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, DEPOT_WIDGET_LOCATION), SetDataTip(STR_BUTTON_LOCATION, STR_NULL), SetFill(1, 1), SetResize(1, 0),
00092     NWidget(NWID_SELECTION, INVALID_COLOUR, DEPOT_WIDGET_SHOW_RENAME), // rename button
00093       NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, DEPOT_WIDGET_RENAME), SetDataTip(STR_BUTTON_RENAME, STR_DEPOT_RENAME_TOOLTIP), SetFill(1, 1), SetResize(1, 0),
00094     EndContainer(),
00095     NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, DEPOT_WIDGET_VEHICLE_LIST), SetDataTip(0x0, STR_NULL), SetFill(0, 1),
00096     NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, DEPOT_WIDGET_STOP_ALL), SetDataTip(SPR_FLAG_VEH_STOPPED, STR_NULL), SetFill(0, 1),
00097     NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, DEPOT_WIDGET_START_ALL), SetDataTip(SPR_FLAG_VEH_RUNNING, STR_NULL), SetFill(0, 1),
00098     NWidget(WWT_RESIZEBOX, COLOUR_GREY),
00099   EndContainer(),
00100 };
00101 
00102 static const WindowDesc _train_depot_desc(
00103   WDP_AUTO, 362, 123,
00104   WC_VEHICLE_DEPOT, WC_NONE,
00105   WDF_UNCLICK_BUTTONS,
00106   _nested_train_depot_widgets, lengthof(_nested_train_depot_widgets)
00107 );
00108 
00109 static const WindowDesc _road_depot_desc(
00110   WDP_AUTO, 316, 97,
00111   WC_VEHICLE_DEPOT, WC_NONE,
00112   WDF_UNCLICK_BUTTONS,
00113   _nested_train_depot_widgets, lengthof(_nested_train_depot_widgets)
00114 );
00115 
00116 static const WindowDesc _ship_depot_desc(
00117   WDP_AUTO, 306, 99,
00118   WC_VEHICLE_DEPOT, WC_NONE,
00119   WDF_UNCLICK_BUTTONS,
00120   _nested_train_depot_widgets, lengthof(_nested_train_depot_widgets)
00121 );
00122 
00123 static const WindowDesc _aircraft_depot_desc(
00124   WDP_AUTO, 332, 99,
00125   WC_VEHICLE_DEPOT, WC_NONE,
00126   WDF_UNCLICK_BUTTONS,
00127   _nested_train_depot_widgets, lengthof(_nested_train_depot_widgets)
00128 );
00129 
00130 extern void DepotSortList(VehicleList *list);
00131 
00139 void CcCloneVehicle(const CommandCost &result, TileIndex tile, uint32 p1, uint32 p2)
00140 {
00141   if (result.Failed()) return;
00142 
00143   const Vehicle *v = Vehicle::Get(_new_vehicle_id);
00144 
00145   ShowVehicleViewWindow(v);
00146 }
00147 
00148 static void TrainDepotMoveVehicle(const Vehicle *wagon, VehicleID sel, const Vehicle *head)
00149 {
00150   const Vehicle *v = Vehicle::Get(sel);
00151 
00152   if (v == wagon) return;
00153 
00154   if (wagon == NULL) {
00155     if (head != NULL) wagon = head->Last();
00156   } else {
00157     wagon = wagon->Previous();
00158     if (wagon == NULL) return;
00159   }
00160 
00161   if (wagon == v) return;
00162 
00163   DoCommandP(v->tile, v->index | (_ctrl_pressed ? 1 : 0) << 20, wagon == NULL ? INVALID_VEHICLE : wagon->index, CMD_MOVE_RAIL_VEHICLE | CMD_MSG(STR_ERROR_CAN_T_MOVE_VEHICLE));
00164 }
00165 
00170 static Dimension _base_block_sizes[4];
00171 
00172 static void InitBlocksizeForShipAircraft(VehicleType type)
00173 {
00174   uint max_width  = 0;
00175   uint max_height = 0;
00176 
00177   const Engine *e;
00178   FOR_ALL_ENGINES_OF_TYPE(e, type) {
00179     EngineID eid = e->index;
00180     uint x, y;
00181 
00182     switch (type) {
00183       default: NOT_REACHED();
00184       case VEH_SHIP:     GetShipSpriteSize(    eid, x, y, EIT_IN_DEPOT); break;
00185       case VEH_AIRCRAFT: GetAircraftSpriteSize(eid, x, y, EIT_IN_DEPOT); break;
00186     }
00187     if (x > max_width)  max_width  = x;
00188     if (y > max_height) max_height = y;
00189   }
00190 
00191   switch (type) {
00192     default: NOT_REACHED();
00193     case VEH_SHIP:
00194       _base_block_sizes[VEH_SHIP].width = max(76U, max_width);
00195       break;
00196     case VEH_AIRCRAFT:
00197       _base_block_sizes[VEH_AIRCRAFT].width = max(67U, max_width);
00198       break;
00199   }
00200   _base_block_sizes[type].height = max(GetVehicleHeight(type), max_height);
00201 }
00202 
00207 void InitDepotWindowBlockSizes()
00208 {
00209   _base_block_sizes[VEH_TRAIN].width = 0;
00210   _base_block_sizes[VEH_TRAIN].height = GetVehicleHeight(VEH_TRAIN);
00211 
00212   _base_block_sizes[VEH_ROAD].width = 32;
00213   _base_block_sizes[VEH_ROAD].height = GetVehicleHeight(VEH_ROAD);
00214 
00215   InitBlocksizeForShipAircraft(VEH_SHIP);
00216   InitBlocksizeForShipAircraft(VEH_AIRCRAFT);
00217 }
00218 
00219 static void DepotSellAllConfirmationCallback(Window *w, bool confirmed);
00220 const Sprite *GetAircraftSprite(EngineID engine);
00221 
00222 struct DepotWindow : Window {
00223   VehicleID sel;
00224   VehicleID vehicle_over; 
00225   VehicleType type;
00226   bool generate_list;
00227   VehicleList vehicle_list;
00228   VehicleList wagon_list;
00229   uint num_columns;       
00230   Scrollbar *hscroll;     
00231   Scrollbar *vscroll;
00232 
00233   DepotWindow(const WindowDesc *desc, TileIndex tile, VehicleType type) : Window()
00234   {
00235     assert(IsCompanyBuildableVehicleType(type)); // ensure that we make the call with a valid type
00236 
00237     this->sel = INVALID_VEHICLE;
00238     this->vehicle_over = INVALID_VEHICLE;
00239     this->generate_list = true;
00240     this->type = type;
00241     this->num_columns = 1; // for non-trains this gets set in FinishInitNested()
00242 
00243     this->CreateNestedTree(desc);
00244     this->hscroll = (this->type == VEH_TRAIN ? this->GetScrollbar(DEPOT_WIDGET_H_SCROLL) : NULL);
00245     this->vscroll = this->GetScrollbar(DEPOT_WIDGET_V_SCROLL);
00246     /* Don't show 'rename button' of aircraft hangar */
00247     this->GetWidget<NWidgetStacked>(DEPOT_WIDGET_SHOW_RENAME)->SetDisplayedPlane(type == VEH_AIRCRAFT ? SZSP_NONE : 0);
00248     /* Only train depots have a horizontal scrollbar and a 'sell chain' button */
00249     this->GetWidget<NWidgetStacked>(DEPOT_WIDGET_SHOW_H_SCROLL)->SetDisplayedPlane(type == VEH_TRAIN ? 0 : SZSP_HORIZONTAL);
00250     this->GetWidget<NWidgetStacked>(DEPOT_WIDGET_SHOW_SELL_CHAIN)->SetDisplayedPlane(type == VEH_TRAIN ? 0 : SZSP_NONE);
00251     this->SetupWidgetData(type);
00252     this->FinishInitNested(desc, tile);
00253 
00254     this->owner = GetTileOwner(tile);
00255     OrderBackup::Reset();
00256   }
00257 
00258   ~DepotWindow()
00259   {
00260     DeleteWindowById(WC_BUILD_VEHICLE, this->window_number);
00261     OrderBackup::Reset(this->window_number);
00262   }
00263 
00271   void DrawVehicleInDepot(const Vehicle *v, int left, int right, int y) const
00272   {
00273     bool free_wagon = false;
00274     int sprite_y = y + (this->resize.step_height - GetVehicleHeight(v->type)) / 2;
00275 
00276     bool rtl = _current_text_dir == TD_RTL;
00277     int image_left  = rtl ? left  + this->count_width  : left  + this->header_width;
00278     int image_right = rtl ? right - this->header_width : right - this->count_width;
00279 
00280     switch (v->type) {
00281       case VEH_TRAIN: {
00282         const Train *u = Train::From(v);
00283         free_wagon = u->IsFreeWagon();
00284 
00285         uint x_space = free_wagon ? TRAININFO_DEFAULT_VEHICLE_WIDTH : 0;
00286         DrawTrainImage(u, image_left + (rtl ? 0 : x_space), image_right - (rtl ? x_space : 0), sprite_y - 1,
00287             this->sel, EIT_IN_DEPOT, free_wagon ? 0 : this->hscroll->GetPosition(), this->vehicle_over);
00288 
00289         /* Length of consist in tiles with 1 fractional digit (rounded up) */
00290         SetDParam(0, CeilDiv(u->gcache.cached_total_length * 10, TILE_SIZE));
00291         SetDParam(1, 1);
00292         DrawString(rtl ? left + WD_FRAMERECT_LEFT : right - this->count_width, rtl ? left + this->count_width : right - WD_FRAMERECT_RIGHT, y + (this->resize.step_height - FONT_HEIGHT_SMALL) / 2, STR_TINY_BLACK_DECIMAL, TC_FROMSTRING, SA_RIGHT); // Draw the counter
00293         break;
00294       }
00295 
00296       case VEH_ROAD:     DrawRoadVehImage( v, image_left, image_right, sprite_y, this->sel, EIT_IN_DEPOT); break;
00297       case VEH_SHIP:     DrawShipImage(    v, image_left, image_right, sprite_y, this->sel, EIT_IN_DEPOT); break;
00298       case VEH_AIRCRAFT: {
00299         const Sprite *spr = GetSprite(v->GetImage(DIR_W, EIT_IN_DEPOT), ST_NORMAL);
00300         DrawAircraftImage(v, image_left, image_right,
00301                   y + max(UnScaleByZoom(spr->height, ZOOM_LVL_GUI) + UnScaleByZoom(spr->y_offs, ZOOM_LVL_GUI) - 14, 0), // tall sprites needs an y offset
00302                   this->sel, EIT_IN_DEPOT);
00303         break;
00304       }
00305       default: NOT_REACHED();
00306     }
00307 
00308     uint diff_x, diff_y;
00309     if (v->IsGroundVehicle()) {
00310       /* Arrange unitnumber and flag horizontally */
00311       diff_x = this->flag_width + WD_FRAMERECT_LEFT;
00312       diff_y = (this->resize.step_height - this->flag_height) / 2 - 2;
00313     } else {
00314       /* Arrange unitnumber and flag vertically */
00315       diff_x = WD_FRAMERECT_LEFT;
00316       diff_y = FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL;
00317     }
00318     int text_left  = rtl ? right - this->header_width - 1 : left + diff_x;
00319     int text_right = rtl ? right - diff_x : left + this->header_width - 1;
00320 
00321     if (free_wagon) {
00322       DrawString(text_left, text_right, y + 2, STR_DEPOT_NO_ENGINE);
00323     } else {
00324       DrawSprite((v->vehstatus & VS_STOPPED) ? SPR_FLAG_VEH_STOPPED : SPR_FLAG_VEH_RUNNING, PAL_NONE, rtl ? right - this->flag_width : left + WD_FRAMERECT_LEFT, y + diff_y);
00325 
00326       SetDParam(0, v->unitnumber);
00327       DrawString(text_left, text_right, y + 2, (uint16)(v->max_age - DAYS_IN_LEAP_YEAR) >= v->age ? STR_BLACK_COMMA : STR_RED_COMMA);
00328     }
00329   }
00330 
00331   void DrawWidget(const Rect &r, int widget) const
00332   {
00333     if (widget != DEPOT_WIDGET_MATRIX) return;
00334 
00335     bool rtl = _current_text_dir == TD_RTL;
00336 
00337     /* Set the row and number of boxes in each row based on the number of boxes drawn in the matrix */
00338     uint16 mat_data = this->GetWidget<NWidgetCore>(DEPOT_WIDGET_MATRIX)->widget_data;
00339     uint16 rows_in_display   = GB(mat_data, MAT_ROW_START, MAT_ROW_BITS);
00340     uint16 boxes_in_each_row = GB(mat_data, MAT_COL_START, MAT_COL_BITS);
00341 
00342     uint16 num = this->vscroll->GetPosition() * boxes_in_each_row;
00343     int maxval = min(this->vehicle_list.Length(), num + (rows_in_display * boxes_in_each_row));
00344     int y;
00345     for (y = r.top + 1; num < maxval; y += this->resize.step_height) { // Draw the rows
00346       for (byte i = 0; i < boxes_in_each_row && num < maxval; i++, num++) {
00347         /* Draw all vehicles in the current row */
00348         const Vehicle *v = this->vehicle_list[num];
00349         if (boxes_in_each_row == 1) {
00350           this->DrawVehicleInDepot(v, r.left, r.right, y);
00351         } else {
00352           int x = r.left + (rtl ? (boxes_in_each_row - i - 1) : i) * this->resize.step_width;
00353           this->DrawVehicleInDepot(v, x, x + this->resize.step_width - 1, y);
00354         }
00355       }
00356     }
00357 
00358     maxval = min(this->vehicle_list.Length() + this->wagon_list.Length(), (this->vscroll->GetPosition() * boxes_in_each_row) + (rows_in_display * boxes_in_each_row));
00359 
00360     /* Draw the train wagons without an engine in front. */
00361     for (; num < maxval; num++, y += this->resize.step_height) {
00362       const Vehicle *v = this->wagon_list[num - this->vehicle_list.Length()];
00363       this->DrawVehicleInDepot(v, r.left, r.right, y);
00364     }
00365   }
00366 
00367   void SetStringParameters(int widget) const
00368   {
00369     if (widget != DEPOT_WIDGET_CAPTION) return;
00370 
00371     /* locate the depot struct */
00372     TileIndex tile = this->window_number;
00373     SetDParam(0, this->type);
00374     SetDParam(1, (this->type == VEH_AIRCRAFT) ? GetStationIndex(tile) : GetDepotIndex(tile));
00375   }
00376 
00377   struct GetDepotVehiclePtData {
00378     const Vehicle *head;
00379     const Vehicle *wagon;
00380   };
00381 
00382   enum DepotGUIAction {
00383     MODE_ERROR,
00384     MODE_DRAG_VEHICLE,
00385     MODE_SHOW_VEHICLE,
00386     MODE_START_STOP,
00387   };
00388 
00389   DepotGUIAction GetVehicleFromDepotWndPt(int x, int y, const Vehicle **veh, GetDepotVehiclePtData *d) const
00390   {
00391     const NWidgetCore *matrix_widget = this->GetWidget<NWidgetCore>(DEPOT_WIDGET_MATRIX);
00392     /* In case of RTL the widgets are swapped as a whole */
00393     if (_current_text_dir == TD_RTL) x = matrix_widget->current_x - x;
00394 
00395     uint xt = 0, xm = 0, ym = 0;
00396     if (this->type == VEH_TRAIN) {
00397       xm = x;
00398     } else {
00399       xt = x / this->resize.step_width;
00400       xm = x % this->resize.step_width;
00401       if (xt >= this->num_columns) return MODE_ERROR;
00402     }
00403     ym = y % this->resize.step_height;
00404 
00405     uint row = y / this->resize.step_height;
00406     if (row >= this->vscroll->GetCapacity()) return MODE_ERROR;
00407 
00408     uint boxes_in_each_row = GB(matrix_widget->widget_data, MAT_COL_START, MAT_COL_BITS);
00409     uint pos = ((row + this->vscroll->GetPosition()) * boxes_in_each_row) + xt;
00410 
00411     if (this->vehicle_list.Length() + this->wagon_list.Length() <= pos) {
00412       /* Clicking on 'line' / 'block' without a vehicle */
00413       if (this->type == VEH_TRAIN) {
00414         /* End the dragging */
00415         d->head  = NULL;
00416         d->wagon = NULL;
00417         return MODE_DRAG_VEHICLE;
00418       } else {
00419         return MODE_ERROR; // empty block, so no vehicle is selected
00420       }
00421     }
00422 
00423     bool wagon = false;
00424     if (this->vehicle_list.Length() > pos) {
00425       *veh = this->vehicle_list[pos];
00426       /* Skip vehicles that are scrolled off the list */
00427       if (this->type == VEH_TRAIN) x += this->hscroll->GetPosition();
00428     } else {
00429       pos -= this->vehicle_list.Length();
00430       *veh = this->wagon_list[pos];
00431       /* free wagons don't have an initial loco. */
00432       x -= VEHICLEINFO_FULL_VEHICLE_WIDTH;
00433       wagon = true;
00434     }
00435 
00436     const Train *v = NULL;
00437     if (this->type == VEH_TRAIN) {
00438       v = Train::From(*veh);
00439       d->head = d->wagon = v;
00440     }
00441 
00442     if (xm <= this->header_width) {
00443       switch (this->type) {
00444         case VEH_TRAIN:
00445           if (wagon) return MODE_ERROR;
00446         case VEH_ROAD:
00447           if (xm <= this->flag_width) return MODE_START_STOP;
00448           break;
00449 
00450         case VEH_SHIP:
00451         case VEH_AIRCRAFT:
00452           if (xm <= this->flag_width && ym >= (uint)(FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL)) return MODE_START_STOP;
00453           break;
00454 
00455         default: NOT_REACHED();
00456       }
00457       return MODE_SHOW_VEHICLE;
00458     }
00459 
00460     if (this->type != VEH_TRAIN) return MODE_DRAG_VEHICLE;
00461 
00462     /* Clicking on the counter */
00463     if (xm >= matrix_widget->current_x - this->count_width) return wagon ? MODE_ERROR : MODE_SHOW_VEHICLE;
00464 
00465     /* Account for the header */
00466     x -= this->header_width;
00467 
00468     /* find the vehicle in this row that was clicked */
00469     for (; v != NULL; v = v->Next()) {
00470       x -= v->GetDisplayImageWidth();
00471       if (x < 0) break;
00472     }
00473 
00474     d->wagon = (v != NULL ? v->GetFirstEnginePart() : NULL);
00475 
00476     return MODE_DRAG_VEHICLE;
00477   }
00478 
00484   void DepotClick(int x, int y)
00485   {
00486     GetDepotVehiclePtData gdvp = { NULL, NULL };
00487     const Vehicle *v = NULL;
00488     DepotGUIAction mode = this->GetVehicleFromDepotWndPt(x, y, &v, &gdvp);
00489 
00490     if (this->type == VEH_TRAIN) v = gdvp.wagon;
00491 
00492     switch (mode) {
00493       case MODE_ERROR: // invalid
00494         return;
00495 
00496       case MODE_DRAG_VEHICLE: { // start dragging of vehicle
00497         if (v != NULL && VehicleClicked(v)) return;
00498 
00499         VehicleID sel = this->sel;
00500 
00501         if (this->type == VEH_TRAIN && sel != INVALID_VEHICLE) {
00502           this->sel = INVALID_VEHICLE;
00503           TrainDepotMoveVehicle(v, sel, gdvp.head);
00504         } else if (v != NULL) {
00505           int image = v->GetImage(_current_text_dir == TD_RTL ? DIR_E : DIR_W, EIT_IN_DEPOT);
00506           SetObjectToPlaceWnd(image, GetVehiclePalette(v), HT_DRAG, this);
00507 
00508           this->sel = v->index;
00509           this->SetDirty();
00510 
00511           _cursor.short_vehicle_offset = v->IsGroundVehicle() ? 16 - v->GetGroundVehicleCache()->cached_veh_length * 2 : 0;
00512           _cursor.vehchain = _ctrl_pressed;
00513         }
00514         break;
00515       }
00516 
00517       case MODE_SHOW_VEHICLE: // show info window
00518         ShowVehicleViewWindow(v);
00519         break;
00520 
00521       case MODE_START_STOP: // click start/stop flag
00522         StartStopVehicle(v, false);
00523         break;
00524 
00525       default: NOT_REACHED();
00526     }
00527   }
00528 
00535   void SetupWidgetData(VehicleType type)
00536   {
00537     this->GetWidget<NWidgetCore>(DEPOT_WIDGET_STOP_ALL)->tool_tip     = STR_DEPOT_MASS_STOP_DEPOT_TRAIN_TOOLTIP + type;
00538     this->GetWidget<NWidgetCore>(DEPOT_WIDGET_START_ALL)->tool_tip    = STR_DEPOT_MASS_START_DEPOT_TRAIN_TOOLTIP + type;
00539     this->GetWidget<NWidgetCore>(DEPOT_WIDGET_SELL)->tool_tip         = STR_DEPOT_TRAIN_SELL_TOOLTIP + type;
00540     this->GetWidget<NWidgetCore>(DEPOT_WIDGET_SELL_ALL)->tool_tip     = STR_DEPOT_SELL_ALL_BUTTON_TRAIN_TOOLTIP + type;
00541 
00542     this->GetWidget<NWidgetCore>(DEPOT_WIDGET_BUILD)->SetDataTip(STR_DEPOT_TRAIN_NEW_VEHICLES_BUTTON + type, STR_DEPOT_TRAIN_NEW_VEHICLES_TOOLTIP + type);
00543     this->GetWidget<NWidgetCore>(DEPOT_WIDGET_CLONE)->SetDataTip(STR_DEPOT_CLONE_TRAIN + type, STR_DEPOT_CLONE_TRAIN_DEPOT_INFO + type);
00544 
00545     this->GetWidget<NWidgetCore>(DEPOT_WIDGET_LOCATION)->tool_tip     = STR_DEPOT_TRAIN_LOCATION_TOOLTIP + type;
00546     this->GetWidget<NWidgetCore>(DEPOT_WIDGET_VEHICLE_LIST)->tool_tip = STR_DEPOT_VEHICLE_ORDER_LIST_TRAIN_TOOLTIP + type;
00547     this->GetWidget<NWidgetCore>(DEPOT_WIDGET_AUTOREPLACE)->tool_tip  = STR_DEPOT_AUTOREPLACE_TRAIN_TOOLTIP + type;
00548     this->GetWidget<NWidgetCore>(DEPOT_WIDGET_MATRIX)->tool_tip       = STR_DEPOT_TRAIN_LIST_TOOLTIP + this->type;
00549 
00550     switch (type) {
00551       default: NOT_REACHED();
00552 
00553       case VEH_TRAIN:
00554         this->GetWidget<NWidgetCore>(DEPOT_WIDGET_VEHICLE_LIST)->widget_data = STR_TRAIN;
00555 
00556         /* Sprites */
00557         this->GetWidget<NWidgetCore>(DEPOT_WIDGET_SELL)->widget_data        = SPR_SELL_TRAIN;
00558         this->GetWidget<NWidgetCore>(DEPOT_WIDGET_SELL_ALL)->widget_data    = SPR_SELL_ALL_TRAIN;
00559         this->GetWidget<NWidgetCore>(DEPOT_WIDGET_AUTOREPLACE)->widget_data = SPR_REPLACE_TRAIN;
00560         break;
00561 
00562       case VEH_ROAD:
00563         this->GetWidget<NWidgetCore>(DEPOT_WIDGET_VEHICLE_LIST)->widget_data = STR_LORRY;
00564 
00565         /* Sprites */
00566         this->GetWidget<NWidgetCore>(DEPOT_WIDGET_SELL)->widget_data        = SPR_SELL_ROADVEH;
00567         this->GetWidget<NWidgetCore>(DEPOT_WIDGET_SELL_ALL)->widget_data    = SPR_SELL_ALL_ROADVEH;
00568         this->GetWidget<NWidgetCore>(DEPOT_WIDGET_AUTOREPLACE)->widget_data = SPR_REPLACE_ROADVEH;
00569         break;
00570 
00571       case VEH_SHIP:
00572         this->GetWidget<NWidgetCore>(DEPOT_WIDGET_VEHICLE_LIST)->widget_data = STR_SHIP;
00573 
00574         /* Sprites */
00575         this->GetWidget<NWidgetCore>(DEPOT_WIDGET_SELL)->widget_data        = SPR_SELL_SHIP;
00576         this->GetWidget<NWidgetCore>(DEPOT_WIDGET_SELL_ALL)->widget_data    = SPR_SELL_ALL_SHIP;
00577         this->GetWidget<NWidgetCore>(DEPOT_WIDGET_AUTOREPLACE)->widget_data = SPR_REPLACE_SHIP;
00578         break;
00579 
00580       case VEH_AIRCRAFT:
00581         this->GetWidget<NWidgetCore>(DEPOT_WIDGET_VEHICLE_LIST)->widget_data = STR_PLANE;
00582 
00583         /* Sprites */
00584         this->GetWidget<NWidgetCore>(DEPOT_WIDGET_SELL)->widget_data        = SPR_SELL_AIRCRAFT;
00585         this->GetWidget<NWidgetCore>(DEPOT_WIDGET_SELL_ALL)->widget_data    = SPR_SELL_ALL_AIRCRAFT;
00586         this->GetWidget<NWidgetCore>(DEPOT_WIDGET_AUTOREPLACE)->widget_data = SPR_REPLACE_AIRCRAFT;
00587         break;
00588     }
00589   }
00590 
00591   uint count_width;
00592   uint header_width;
00593   uint flag_width;
00594   uint flag_height;
00595 
00596   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00597   {
00598     switch (widget) {
00599       case DEPOT_WIDGET_MATRIX: {
00600         uint min_height = 0;
00601 
00602         if (this->type == VEH_TRAIN) {
00603           SetDParam(0, 1000);
00604           SetDParam(1, 1);
00605           this->count_width = GetStringBoundingBox(STR_TINY_BLACK_DECIMAL).width + WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
00606         } else {
00607           this->count_width = 0;
00608         }
00609 
00610         Dimension unumber = { GetDigitWidth() * 4, FONT_HEIGHT_NORMAL };
00611         const Sprite *spr = GetSprite(SPR_FLAG_VEH_STOPPED, ST_NORMAL);
00612         this->flag_width  = UnScaleByZoom(spr->width, ZOOM_LVL_GUI) + WD_FRAMERECT_RIGHT;
00613         this->flag_height = UnScaleByZoom(spr->height, ZOOM_LVL_GUI);
00614 
00615         if (this->type == VEH_TRAIN || this->type == VEH_ROAD) {
00616           min_height = max<uint>(unumber.height + WD_MATRIX_TOP, UnScaleByZoom(spr->height, ZOOM_LVL_GUI));
00617           this->header_width = unumber.width + this->flag_width + WD_FRAMERECT_LEFT;
00618         } else {
00619           min_height = unumber.height + UnScaleByZoom(spr->height, ZOOM_LVL_GUI) + WD_MATRIX_TOP + WD_PAR_VSEP_NORMAL + WD_MATRIX_BOTTOM;
00620           this->header_width = max<uint>(unumber.width, this->flag_width) + WD_FRAMERECT_RIGHT;
00621         }
00622         int base_width = this->count_width + this->header_width;
00623 
00624         resize->height = max(_base_block_sizes[this->type].height, min_height);
00625         if (this->type == VEH_TRAIN) {
00626           resize->width = 1;
00627           size->width = base_width + 2 * 29; // about 2 parts
00628           size->height = resize->height * 6;
00629         } else {
00630           resize->width = base_width + _base_block_sizes[this->type].width;
00631           size->width = resize->width * (this->type == VEH_ROAD ? 5 : 3);
00632           size->height = resize->height * (this->type == VEH_ROAD ? 5 : 3);
00633         }
00634         fill->width = resize->width;
00635         fill->height = resize->height;
00636         break;
00637       }
00638     }
00639   }
00640 
00646   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
00647   {
00648     this->generate_list = true;
00649   }
00650 
00651   virtual void OnPaint()
00652   {
00653     if (this->generate_list) {
00654       /* Generate the vehicle list
00655        * It's ok to use the wagon pointers for non-trains as they will be ignored */
00656       BuildDepotVehicleList(this->type, this->window_number, &this->vehicle_list, &this->wagon_list);
00657       this->generate_list = false;
00658       DepotSortList(&this->vehicle_list);
00659     }
00660 
00661     /* determine amount of items for scroller */
00662     if (this->type == VEH_TRAIN) {
00663       uint max_width = VEHICLEINFO_FULL_VEHICLE_WIDTH;
00664       for (uint num = 0; num < this->vehicle_list.Length(); num++) {
00665         uint width = 0;
00666         for (const Train *v = Train::From(this->vehicle_list[num]); v != NULL; v = v->Next()) {
00667           width += v->GetDisplayImageWidth();
00668         }
00669         max_width = max(max_width, width);
00670       }
00671       /* Always have 1 empty row, so people can change the setting of the train */
00672       this->vscroll->SetCount(this->vehicle_list.Length() + this->wagon_list.Length() + 1);
00673       this->hscroll->SetCount(max_width);
00674     } else {
00675       this->vscroll->SetCount(CeilDiv(this->vehicle_list.Length(), this->num_columns));
00676     }
00677 
00678     /* Setup disabled buttons. */
00679     TileIndex tile = this->window_number;
00680     this->SetWidgetsDisabledState(!IsTileOwner(tile, _local_company),
00681       DEPOT_WIDGET_STOP_ALL,
00682       DEPOT_WIDGET_START_ALL,
00683       DEPOT_WIDGET_SELL,
00684       DEPOT_WIDGET_SELL_CHAIN,
00685       DEPOT_WIDGET_SELL_ALL,
00686       DEPOT_WIDGET_BUILD,
00687       DEPOT_WIDGET_CLONE,
00688       DEPOT_WIDGET_RENAME,
00689       DEPOT_WIDGET_AUTOREPLACE,
00690       WIDGET_LIST_END);
00691 
00692     this->DrawWidgets();
00693   }
00694 
00695   virtual void OnClick(Point pt, int widget, int click_count)
00696   {
00697     switch (widget) {
00698       case DEPOT_WIDGET_MATRIX: { // List
00699         NWidgetBase *nwi = this->GetWidget<NWidgetBase>(DEPOT_WIDGET_MATRIX);
00700         this->DepotClick(pt.x - nwi->pos_x, pt.y - nwi->pos_y);
00701         break;
00702       }
00703 
00704       case DEPOT_WIDGET_BUILD: // Build vehicle
00705         ResetObjectToPlace();
00706         ShowBuildVehicleWindow(this->window_number, this->type);
00707         break;
00708 
00709       case DEPOT_WIDGET_CLONE: // Clone button
00710         this->SetWidgetDirty(DEPOT_WIDGET_CLONE);
00711         this->ToggleWidgetLoweredState(DEPOT_WIDGET_CLONE);
00712 
00713         if (this->IsWidgetLowered(DEPOT_WIDGET_CLONE)) {
00714           static const CursorID clone_icons[] = {
00715             SPR_CURSOR_CLONE_TRAIN, SPR_CURSOR_CLONE_ROADVEH,
00716             SPR_CURSOR_CLONE_SHIP, SPR_CURSOR_CLONE_AIRPLANE
00717           };
00718 
00719           SetObjectToPlaceWnd(clone_icons[this->type], PAL_NONE, HT_VEHICLE, this);
00720         } else {
00721           ResetObjectToPlace();
00722         }
00723         break;
00724 
00725       case DEPOT_WIDGET_LOCATION:
00726         if (_ctrl_pressed) {
00727           ShowExtraViewPortWindow(this->window_number);
00728         } else {
00729           ScrollMainWindowToTile(this->window_number);
00730         }
00731         break;
00732 
00733       case DEPOT_WIDGET_RENAME: // Rename button
00734         SetDParam(0, this->type);
00735         SetDParam(1, Depot::GetByTile((TileIndex)this->window_number)->index);
00736         ShowQueryString(STR_DEPOT_NAME, STR_DEPOT_RENAME_DEPOT_CAPTION, MAX_LENGTH_DEPOT_NAME_CHARS, this, CS_ALPHANUMERAL, QSF_ENABLE_DEFAULT | QSF_LEN_IN_CHARS);
00737         break;
00738 
00739       case DEPOT_WIDGET_STOP_ALL:
00740       case DEPOT_WIDGET_START_ALL: {
00741         VehicleListIdentifier vli(VL_DEPOT_LIST, this->type, this->owner);
00742         DoCommandP(this->window_number, (widget == DEPOT_WIDGET_START_ALL ? (1 << 0) : 0), vli.Pack(), CMD_MASS_START_STOP);
00743         break;
00744       }
00745 
00746       case DEPOT_WIDGET_SELL_ALL:
00747         /* Only open the confimation window if there are anything to sell */
00748         if (this->vehicle_list.Length() != 0 || this->wagon_list.Length() != 0) {
00749           TileIndex tile = this->window_number;
00750           byte vehtype = this->type;
00751 
00752           SetDParam(0, vehtype);
00753           SetDParam(1, (vehtype == VEH_AIRCRAFT) ? GetStationIndex(tile) : GetDepotIndex(tile));
00754           ShowQuery(
00755             STR_DEPOT_CAPTION,
00756             STR_DEPOT_SELL_CONFIRMATION_TEXT,
00757             this,
00758             DepotSellAllConfirmationCallback
00759           );
00760         }
00761         break;
00762 
00763       case DEPOT_WIDGET_VEHICLE_LIST:
00764         ShowVehicleListWindow(GetTileOwner(this->window_number), this->type, (TileIndex)this->window_number);
00765         break;
00766 
00767       case DEPOT_WIDGET_AUTOREPLACE:
00768         DoCommandP(this->window_number, this->type, 0, CMD_DEPOT_MASS_AUTOREPLACE);
00769         break;
00770 
00771     }
00772   }
00773 
00774   virtual void OnQueryTextFinished(char *str)
00775   {
00776     if (str == NULL) return;
00777 
00778     /* Do depot renaming */
00779     DoCommandP(0, GetDepotIndex(this->window_number), 0, CMD_RENAME_DEPOT | CMD_MSG(STR_ERROR_CAN_T_RENAME_DEPOT), NULL, str);
00780   }
00781 
00782   virtual bool OnRightClick(Point pt, int widget)
00783   {
00784     if (widget != DEPOT_WIDGET_MATRIX) return false;
00785 
00786     GetDepotVehiclePtData gdvp = { NULL, NULL };
00787     const Vehicle *v = NULL;
00788     NWidgetBase *nwi = this->GetWidget<NWidgetBase>(DEPOT_WIDGET_MATRIX);
00789     DepotGUIAction mode = this->GetVehicleFromDepotWndPt(pt.x - nwi->pos_x, pt.y - nwi->pos_y, &v, &gdvp);
00790 
00791     if (this->type == VEH_TRAIN) v = gdvp.wagon;
00792 
00793     if (v == NULL || mode != MODE_DRAG_VEHICLE) return false;
00794 
00795     CargoArray capacity, loaded;
00796 
00797     /* Display info for single (articulated) vehicle, or for whole chain starting with selected vehicle */
00798     bool whole_chain = (this->type == VEH_TRAIN && _ctrl_pressed);
00799 
00800     /* loop through vehicle chain and collect cargos */
00801     uint num = 0;
00802     for (const Vehicle *w = v; w != NULL; w = w->Next()) {
00803       if (w->cargo_cap > 0 && w->cargo_type < NUM_CARGO) {
00804         capacity[w->cargo_type] += w->cargo_cap;
00805         loaded  [w->cargo_type] += w->cargo.Count();
00806       }
00807 
00808       if (w->type == VEH_TRAIN && !w->HasArticulatedPart()) {
00809         num++;
00810         if (!whole_chain) break;
00811       }
00812     }
00813 
00814     /* Build tooltipstring */
00815     static char details[1024];
00816     details[0] = '\0';
00817     char *pos = details;
00818 
00819     for (CargoID cargo_type = 0; cargo_type < NUM_CARGO; cargo_type++) {
00820       if (capacity[cargo_type] == 0) continue;
00821 
00822       SetDParam(0, cargo_type);           // {CARGO} #1
00823       SetDParam(1, loaded[cargo_type]);   // {CARGO} #2
00824       SetDParam(2, cargo_type);           // {SHORTCARGO} #1
00825       SetDParam(3, capacity[cargo_type]); // {SHORTCARGO} #2
00826       pos = GetString(pos, STR_DEPOT_VEHICLE_TOOLTIP_CARGO, lastof(details));
00827     }
00828 
00829     /* Show tooltip window */
00830     uint64 args[2];
00831     args[0] = (whole_chain ? num : v->engine_type);
00832     args[1] = (uint64)(size_t)details;
00833     GuiShowTooltips(this, whole_chain ? STR_DEPOT_VEHICLE_TOOLTIP_CHAIN : STR_DEPOT_VEHICLE_TOOLTIP, 2, args, TCC_RIGHT_CLICK);
00834 
00835     return true;
00836   }
00837 
00842   virtual void OnVehicleSelect(const Vehicle *v)
00843   {
00844     if (DoCommandP(this->window_number, v->index, _ctrl_pressed ? 1 : 0, CMD_CLONE_VEHICLE | CMD_MSG(STR_ERROR_CAN_T_BUY_TRAIN + v->type), CcCloneVehicle)) {
00845       ResetObjectToPlace();
00846     }
00847   }
00848 
00849   virtual void OnPlaceObjectAbort()
00850   {
00851     /* abort clone */
00852     this->RaiseWidget(DEPOT_WIDGET_CLONE);
00853     this->SetWidgetDirty(DEPOT_WIDGET_CLONE);
00854 
00855     /* abort drag & drop */
00856     this->sel = INVALID_VEHICLE;
00857     this->vehicle_over = INVALID_VEHICLE;
00858     this->SetWidgetDirty(DEPOT_WIDGET_MATRIX);
00859   }
00860 
00861   virtual void OnMouseDrag(Point pt, int widget)
00862   {
00863     if (this->type != VEH_TRAIN || this->sel == INVALID_VEHICLE) return;
00864 
00865     /* A rail vehicle is dragged.. */
00866     if (widget != DEPOT_WIDGET_MATRIX) { // ..outside of the depot matrix.
00867       if (this->vehicle_over != INVALID_VEHICLE) {
00868         this->vehicle_over = INVALID_VEHICLE;
00869         this->SetWidgetDirty(DEPOT_WIDGET_MATRIX);
00870       }
00871       return;
00872     }
00873 
00874     NWidgetBase *matrix = this->GetWidget<NWidgetBase>(widget);
00875     const Vehicle *v = NULL;
00876     GetDepotVehiclePtData gdvp = {NULL, NULL};
00877 
00878     if (this->GetVehicleFromDepotWndPt(pt.x - matrix->pos_x, pt.y - matrix->pos_y, &v, &gdvp) != MODE_DRAG_VEHICLE) return;
00879 
00880     VehicleID new_vehicle_over = INVALID_VEHICLE;
00881     if (gdvp.head != NULL) {
00882       if (gdvp.wagon == NULL && gdvp.head->Last()->index != this->sel) { // ..at the end of the train.
00883         /* NOTE: As a wagon can't be moved at the begin of a train, head index isn't used to mark a drag-and-drop
00884          * destination inside a train. This head index is then used to indicate that a wagon is inserted at
00885          * the end of the train.
00886          */
00887         new_vehicle_over = gdvp.head->index;
00888       } else if (gdvp.wagon != NULL && gdvp.head != gdvp.wagon &&
00889           gdvp.wagon->index != this->sel &&
00890           gdvp.wagon->Previous()->index != this->sel) { // ..over an existing wagon.
00891         new_vehicle_over = gdvp.wagon->index;
00892       }
00893     }
00894 
00895     if (this->vehicle_over == new_vehicle_over) return;
00896 
00897     this->vehicle_over = new_vehicle_over;
00898     this->SetWidgetDirty(widget);
00899   }
00900 
00901   virtual void OnDragDrop(Point pt, int widget)
00902   {
00903     switch (widget) {
00904       case DEPOT_WIDGET_MATRIX: {
00905         const Vehicle *v = NULL;
00906         VehicleID sel = this->sel;
00907 
00908         this->sel = INVALID_VEHICLE;
00909         this->SetDirty();
00910 
00911         NWidgetBase *nwi = this->GetWidget<NWidgetBase>(DEPOT_WIDGET_MATRIX);
00912         if (this->type == VEH_TRAIN) {
00913           GetDepotVehiclePtData gdvp = { NULL, NULL };
00914 
00915           if (this->GetVehicleFromDepotWndPt(pt.x - nwi->pos_x, pt.y - nwi->pos_y, &v, &gdvp) == MODE_DRAG_VEHICLE && sel != INVALID_VEHICLE) {
00916             if (gdvp.wagon != NULL && gdvp.wagon->index == sel && _ctrl_pressed) {
00917               DoCommandP(Vehicle::Get(sel)->tile, Vehicle::Get(sel)->index, true,
00918                   CMD_REVERSE_TRAIN_DIRECTION | CMD_MSG(STR_ERROR_CAN_T_REVERSE_DIRECTION_RAIL_VEHICLE));
00919             } else if (gdvp.wagon == NULL || gdvp.wagon->index != sel) {
00920               this->vehicle_over = INVALID_VEHICLE;
00921               TrainDepotMoveVehicle(gdvp.wagon, sel, gdvp.head);
00922             } else if (gdvp.head != NULL && gdvp.head->IsFrontEngine()) {
00923               ShowVehicleViewWindow(gdvp.head);
00924             }
00925           }
00926         } else if (this->GetVehicleFromDepotWndPt(pt.x - nwi->pos_x, pt.y - nwi->pos_y, &v, NULL) == MODE_DRAG_VEHICLE && v != NULL && sel == v->index) {
00927           ShowVehicleViewWindow(v);
00928         }
00929         break;
00930       }
00931 
00932       case DEPOT_WIDGET_SELL: case DEPOT_WIDGET_SELL_CHAIN: {
00933         if (this->IsWidgetDisabled(widget)) return;
00934         if (this->sel == INVALID_VEHICLE) return;
00935 
00936         this->HandleButtonClick(widget);
00937 
00938         const Vehicle *v = Vehicle::Get(this->sel);
00939         this->sel = INVALID_VEHICLE;
00940         this->SetDirty();
00941 
00942         int sell_cmd = (v->type == VEH_TRAIN && (widget == DEPOT_WIDGET_SELL_CHAIN || _ctrl_pressed)) ? 1 : 0;
00943         DoCommandP(v->tile, v->index | sell_cmd << 20 | MAKE_ORDER_BACKUP_FLAG, 0, GetCmdSellVeh(v->type));
00944         break;
00945       }
00946 
00947       default:
00948         this->sel = INVALID_VEHICLE;
00949         this->SetDirty();
00950     }
00951     _cursor.vehchain = false;
00952   }
00953 
00954   virtual void OnTimeout()
00955   {
00956     if (!this->IsWidgetDisabled(DEPOT_WIDGET_SELL)) {
00957       this->RaiseWidget(DEPOT_WIDGET_SELL);
00958       this->SetWidgetDirty(DEPOT_WIDGET_SELL);
00959     }
00960     if (this->nested_array[DEPOT_WIDGET_SELL] != NULL && !this->IsWidgetDisabled(DEPOT_WIDGET_SELL_CHAIN)) {
00961       this->RaiseWidget(DEPOT_WIDGET_SELL_CHAIN);
00962       this->SetWidgetDirty(DEPOT_WIDGET_SELL_CHAIN);
00963     }
00964   }
00965 
00966   virtual void OnResize()
00967   {
00968     NWidgetCore *nwi = this->GetWidget<NWidgetCore>(DEPOT_WIDGET_MATRIX);
00969     this->vscroll->SetCapacityFromWidget(this, DEPOT_WIDGET_MATRIX);
00970     if (this->type == VEH_TRAIN) {
00971       this->hscroll->SetCapacity(nwi->current_x - this->header_width - this->count_width);
00972       nwi->widget_data = (this->vscroll->GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
00973     } else {
00974       this->num_columns = nwi->current_x / nwi->resize_x;
00975       nwi->widget_data = (this->vscroll->GetCapacity() << MAT_ROW_START) + (this->num_columns << MAT_COL_START);
00976     }
00977   }
00978 
00979   virtual EventState OnCTRLStateChange()
00980   {
00981     if (this->sel != INVALID_VEHICLE) {
00982       _cursor.vehchain = _ctrl_pressed;
00983       this->SetWidgetDirty(DEPOT_WIDGET_MATRIX);
00984       return ES_HANDLED;
00985     }
00986 
00987     return ES_NOT_HANDLED;
00988   }
00989 };
00990 
00991 static void DepotSellAllConfirmationCallback(Window *win, bool confirmed)
00992 {
00993   if (confirmed) {
00994     DepotWindow *w = (DepotWindow*)win;
00995     TileIndex tile = w->window_number;
00996     byte vehtype = w->type;
00997     DoCommandP(tile, vehtype, 0, CMD_DEPOT_SELL_ALL_VEHICLES);
00998   }
00999 }
01000 
01006 void ShowDepotWindow(TileIndex tile, VehicleType type)
01007 {
01008   if (BringWindowToFrontById(WC_VEHICLE_DEPOT, tile) != NULL) return;
01009 
01010   const WindowDesc *desc;
01011   switch (type) {
01012     default: NOT_REACHED();
01013     case VEH_TRAIN:    desc = &_train_depot_desc;    break;
01014     case VEH_ROAD:     desc = &_road_depot_desc;     break;
01015     case VEH_SHIP:     desc = &_ship_depot_desc;     break;
01016     case VEH_AIRCRAFT: desc = &_aircraft_depot_desc; break;
01017   }
01018 
01019   new DepotWindow(desc, tile, type);
01020 }
01021 
01026 void DeleteDepotHighlightOfVehicle(const Vehicle *v)
01027 {
01028   DepotWindow *w;
01029 
01030   /* If we haven't got any vehicles on the mouse pointer, we haven't got any highlighted in any depots either
01031    * If that is the case, we can skip looping though the windows and save time
01032    */
01033   if (_special_mouse_mode != WSM_DRAGDROP) return;
01034 
01035   w = dynamic_cast<DepotWindow*>(FindWindowById(WC_VEHICLE_DEPOT, v->tile));
01036   if (w != NULL) {
01037     if (w->sel == v->index) ResetObjectToPlace();
01038   }
01039 }