depot_gui.cpp

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

Generated on Sun May 15 19:20:07 2011 for OpenTTD by  doxygen 1.6.1