src/depot_gui.c
changeset 5475 2e6990a8c7c4
parent 5460 dd591800bf3f
equal deleted inserted replaced
5474:ac55aefc54f3 5475:2e6990a8c7c4
       
     1 /* $Id$ */
       
     2 
       
     3 #include "stdafx.h"
       
     4 #include "openttd.h"
       
     5 #include "functions.h"
       
     6 #include "train.h"
       
     7 #include "roadveh.h"
       
     8 #include "ship.h"
       
     9 #include "aircraft.h"
       
    10 #include "table/strings.h"
       
    11 #include "table/sprites.h"
       
    12 #include "gui.h"
       
    13 #include "gfx.h"
       
    14 #include "vehicle.h"
       
    15 #include "viewport.h"
       
    16 #include "command.h"
       
    17 #include "depot.h"
       
    18 #include "vehicle_gui.h"
       
    19 #include "station_map.h"
       
    20 #include "newgrf_engine.h"
       
    21 
       
    22 /*
       
    23  * Since all depot window sizes aren't the same, we need to modify sizes a little.
       
    24  * 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.
       
    25  * How long they should be moved and for what window types are controlled in ShowDepotWindow()
       
    26  */
       
    27 
       
    28 /* Names of the widgets. Keep them in the same order as in the widget array */
       
    29 enum DepotWindowWidgets {
       
    30 	DEPOT_WIDGET_CLOSEBOX = 0,
       
    31 	DEPOT_WIDGET_CAPTION,
       
    32 	DEPOT_WIDGET_STICKY,
       
    33 	DEPOT_WIDGET_SELL,
       
    34 	DEPOT_WIDGET_SELL_CHAIN,
       
    35 	DEPOT_WIDGET_SELL_ALL,
       
    36 	DEPOT_WIDGET_AUTOREPLACE,
       
    37 	DEPOT_WIDGET_MATRIX,
       
    38 	DEPOT_WIDGET_V_SCROLL, // Vertical scrollbar
       
    39 	DEPOT_WIDGET_H_SCROLL, // Horizontal scrollbar
       
    40 	DEPOT_WIDGET_BUILD,
       
    41 	DEPOT_WIDGET_CLONE,
       
    42 	DEPOT_WIDGET_LOCATION,
       
    43 	DEPOT_WIDGET_VEHICLE_LIST,
       
    44 	DEPOT_WIDGET_STOP_ALL,
       
    45 	DEPOT_WIDGET_START_ALL,
       
    46 	DEPOT_WIDGET_RESIZE,
       
    47 };
       
    48 
       
    49 /* Widget array for all depot windows.
       
    50  * If a widget is needed in some windows only (like train specific), add it for all windows
       
    51  * and use HideWindowWidget in ShowDepotWindow() to remove it in the windows where it should not be
       
    52  * Keep the widget numbers in sync with the enum or really bad stuff will happen!!! */
       
    53 
       
    54 /* When adding widgets, place them as you would place them for the ship depot and define how you want it to move in widget_moves[]
       
    55  * If you want a widget for one window only, set it to be hidden in ShowDepotWindow() for the windows where you don't want it
       
    56  * NOTE: the train only widgets are moved/resized in ShowDepotWindow() so they follow certain other widgets if they are moved to ensure that they stick together.
       
    57  *    Changing the size of those here will not have an effect at all. It should be done in ShowDepotWindow()
       
    58  */
       
    59 static const Widget _depot_widgets[] = {
       
    60 	{   WWT_CLOSEBOX,   RESIZE_NONE,    14,     0,    10,     0,    13, STR_00C5,            STR_018B_CLOSE_WINDOW},            // DEPOT_WIDGET_CLOSEBOX
       
    61 	{    WWT_CAPTION,  RESIZE_RIGHT,    14,    11,   292,     0,    13, 0x0,                 STR_018C_WINDOW_TITLE_DRAG_THIS},  // DEPOT_WIDGET_CAPTION
       
    62 	{  WWT_STICKYBOX,     RESIZE_LR,    14,   293,   304,     0,    13, 0x0,                 STR_STICKY_BUTTON},                // DEPOT_WIDGET_STICKY
       
    63 
       
    64 	/* Widgets are set up run-time */
       
    65 	{     WWT_IMGBTN,    RESIZE_LRB,    14,   270,   292,    14,    37, 0x0,                 STR_NULL},                         // DEPOT_WIDGET_SELL
       
    66 	{     WWT_IMGBTN,   RESIZE_LRTB,    14,   270,   292,    14,    37, SPR_SELL_CHAIN_TRAIN,STR_DRAG_WHOLE_TRAIN_TO_SELL_TIP}, // DEPOT_WIDGET_SELL_CHAIN, trains only
       
    67 	{ WWT_PUSHIMGBTN,   RESIZE_LRTB,    14,   270,   292,    38,    60, 0x0,                 STR_NULL},                         // DEPOT_WIDGET_SELL_ALL
       
    68 	{ WWT_PUSHIMGBTN,   RESIZE_LRTB,    14,   270,   292,    61,    83, 0x0,                 STR_NULL},                         // DEPOT_WIDGET_AUTOREPLACE
       
    69 
       
    70 	{     WWT_MATRIX,     RESIZE_RB,    14,     0,   269,    14,    83, 0x0,                 STR_NULL},                         // DEPOT_WIDGET_MATRIX
       
    71 	{  WWT_SCROLLBAR,    RESIZE_LRB,    14,   293,   304,    14,    83, 0x0,                 STR_0190_SCROLL_BAR_SCROLLS_LIST}, // DEPOT_WIDGET_V_SCROLL
       
    72 
       
    73 	{ WWT_HSCROLLBAR,    RESIZE_RTB,    14,     0,   269,    72,    83, 0x0,                 STR_HSCROLL_BAR_SCROLLS_LIST},     // DEPOT_WIDGET_H_SCROLL, trains only
       
    74 
       
    75 	/* The buttons in the bottom of the window. left and right is not important as they are later resized to be equal in size
       
    76 	 * This calculation is based on right in DEPOT_WIDGET_LOCATION and it presumes left of DEPOT_WIDGET_BUILD is 0            */
       
    77 	{ WWT_PUSHTXTBTN,     RESIZE_TB,    14,     0,    85,    84,    95, 0x0,                 STR_NULL},                         // DEPOT_WIDGET_BUILD
       
    78 	{    WWT_TEXTBTN,     RESIZE_TB,    14,    86,   170,    84,    95, 0x0,                 STR_NULL},                         // DEPOT_WIDGET_CLONE
       
    79 	{ WWT_PUSHTXTBTN,    RESIZE_RTB,    14,   171,   257,    84,    95, STR_00E4_LOCATION,   STR_NULL},                         // DEPOT_WIDGET_LOCATION
       
    80 	{ WWT_PUSHTXTBTN,   RESIZE_LRTB,    14,   258,   269,    84,    95, 0x0,                 STR_NULL},                         // DEPOT_WIDGET_VEHICLE_LIST
       
    81 	{ WWT_PUSHIMGBTN,   RESIZE_LRTB,    14,   270,   280,    84,    95, SPR_FLAG_VEH_STOPPED,STR_NULL},                         // DEPOT_WIDGET_STOP_ALL
       
    82 	{ WWT_PUSHIMGBTN,   RESIZE_LRTB,    14,   281,   292,    84,    95, SPR_FLAG_VEH_RUNNING,STR_NULL},                         // DEPOT_WIDGET_START_ALL
       
    83 	{  WWT_RESIZEBOX,   RESIZE_LRTB,    14,   293,   304,    84,    95, 0x0,                 STR_RESIZE_BUTTON},                // DEPOT_WIDGET_RESIZE
       
    84 	{   WIDGETS_END},
       
    85 };
       
    86 
       
    87 static void DepotWndProc(Window *w, WindowEvent *e);
       
    88 
       
    89 static const WindowDesc _train_depot_desc = {
       
    90 	WDP_AUTO, WDP_AUTO, 305, 96,
       
    91 	WC_VEHICLE_DEPOT,0,
       
    92 	WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS | WDF_STICKY_BUTTON | WDF_RESIZABLE,
       
    93 	_depot_widgets,
       
    94 	DepotWndProc
       
    95 };
       
    96 
       
    97 static const WindowDesc _road_depot_desc = {
       
    98 	WDP_AUTO, WDP_AUTO, 305, 96,
       
    99 	WC_VEHICLE_DEPOT,0,
       
   100 	WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS | WDF_STICKY_BUTTON | WDF_RESIZABLE,
       
   101 	_depot_widgets,
       
   102 	DepotWndProc
       
   103 };
       
   104 
       
   105 static const WindowDesc _ship_depot_desc = {
       
   106 	WDP_AUTO, WDP_AUTO, 305, 96,
       
   107 	WC_VEHICLE_DEPOT,0,
       
   108 	WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS | WDF_STICKY_BUTTON | WDF_RESIZABLE,
       
   109 	_depot_widgets,
       
   110 	DepotWndProc
       
   111 };
       
   112 
       
   113 static const WindowDesc _aircraft_depot_desc = {
       
   114 	WDP_AUTO, WDP_AUTO, 305, 96,
       
   115 	WC_VEHICLE_DEPOT,0,
       
   116 	WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS | WDF_STICKY_BUTTON | WDF_RESIZABLE,
       
   117 	_depot_widgets,
       
   118 	DepotWndProc
       
   119 };
       
   120 
       
   121 extern int WagonLengthToPixels(int len);
       
   122 
       
   123 void CcCloneVehicle(bool success, TileIndex tile, uint32 p1, uint32 p2)
       
   124 {
       
   125 	if (!success) return;
       
   126 	switch(GetVehicle(p1)->type) {
       
   127 		case VEH_Train:    CcCloneTrain(   true, tile, p1, p2); break;
       
   128 		case VEH_Road:     CcCloneRoadVeh( true, tile, p1, p2); break;
       
   129 		case VEH_Ship:     CcCloneShip(    true, tile, p1, p2); break;
       
   130 		case VEH_Aircraft: CcCloneAircraft(true, tile, p1, p2); break;
       
   131 	}
       
   132 }
       
   133 
       
   134 static inline void ShowVehicleViewWindow(const Vehicle *v)
       
   135 {
       
   136 	switch (v->type) {
       
   137 		case VEH_Train:    ShowTrainViewWindow(v);    break;
       
   138 		case VEH_Road:     ShowRoadVehViewWindow(v);  break;
       
   139 		case VEH_Ship:     ShowShipViewWindow(v);     break;
       
   140 		case VEH_Aircraft: ShowAircraftViewWindow(v); break;
       
   141 		default: NOT_REACHED();
       
   142 	}
       
   143 }
       
   144 
       
   145 static void DepotSellAllConfirmationCallback(Window *w, bool confirmed)
       
   146 {
       
   147 	if (confirmed) {
       
   148 		TileIndex tile = w->window_number;
       
   149 		byte vehtype = WP(w, depot_d).type;
       
   150 		DoCommandP(tile, vehtype, 0, NULL, CMD_DEPOT_SELL_ALL_VEHICLES);
       
   151 	}
       
   152 }
       
   153 
       
   154 /** Draw a vehicle in the depot window in the box with the top left corner at x,y
       
   155  * @param *w Window to draw in
       
   156  * @param *v Vehicle to draw
       
   157  * @param x Left side of the box to draw in
       
   158  * @param y Top of the box to draw in
       
   159  */
       
   160 static void DrawVehicleInDepot(Window *w, const Vehicle *v, int x, int y)
       
   161 {
       
   162 	byte diff_x = 0, diff_y = 0;
       
   163 
       
   164 	switch (v->type) {
       
   165 		case VEH_Train:
       
   166 			DrawTrainImage(v, x + 21, y, w->hscroll.cap + 4, w->hscroll.pos, WP(w,depot_d).sel);
       
   167 
       
   168 			/* Number of wagons relative to a standard length wagon (rounded up) */
       
   169 			SetDParam(0, (v->u.rail.cached_total_length + 7) / 8);
       
   170 			DrawStringRightAligned(w->widget[DEPOT_WIDGET_MATRIX].right - 1, y + 4, STR_TINY_BLACK, 0); // Draw the counter
       
   171 			break;
       
   172 
       
   173 		case VEH_Road:     DrawRoadVehImage( v, x + 24, y, WP(w, depot_d).sel); break;
       
   174 		case VEH_Ship:     DrawShipImage(    v, x + 19, y, WP(w, depot_d).sel); break;
       
   175 		case VEH_Aircraft: DrawAircraftImage(v, x + 12, y, WP(w, depot_d).sel); break;
       
   176 		default: NOT_REACHED();
       
   177 	}
       
   178 
       
   179 	if (w->resize.step_height == 14) {
       
   180 		/* VEH_Train and VEH_Road, which are low */
       
   181 		diff_x = 15;
       
   182 	} else {
       
   183 		/* VEH_Ship and VEH_Aircraft, which are tall */
       
   184 		diff_y = 12;
       
   185 	}
       
   186 
       
   187 	DrawSprite((v->vehstatus & VS_STOPPED) ? SPR_FLAG_VEH_STOPPED : SPR_FLAG_VEH_RUNNING, x + diff_x, y + diff_y);
       
   188 
       
   189 	SetDParam(0, v->unitnumber);
       
   190 	DrawString(x, y + 2, (uint16)(v->max_age-366) >= v->age ? STR_00E2 : STR_00E3, 0);
       
   191 }
       
   192 
       
   193 static void DrawDepotWindow(Window *w)
       
   194 {
       
   195 	Vehicle **vl = WP(w, depot_d).vehicle_list;
       
   196 	TileIndex tile = w->window_number;
       
   197 	int x, y, i, hnum, max;
       
   198 	uint16 num = WP(w, depot_d).engine_count;
       
   199 
       
   200 	/* Set the row and number of boxes in each row based on the number of boxes drawn in the matrix */
       
   201 	uint16 rows_in_display   = w->widget[DEPOT_WIDGET_MATRIX].data >> 8;
       
   202 	uint16 boxes_in_each_row = w->widget[DEPOT_WIDGET_MATRIX].data & 0xFF;
       
   203 
       
   204 	/* setup disabled buttons */
       
   205 	SetWindowWidgetsDisabledState(w, !IsTileOwner(tile, _local_player),
       
   206 		DEPOT_WIDGET_STOP_ALL,
       
   207 		DEPOT_WIDGET_START_ALL,
       
   208 		DEPOT_WIDGET_SELL,
       
   209 		DEPOT_WIDGET_SELL_CHAIN,
       
   210 		DEPOT_WIDGET_SELL_ALL,
       
   211 		DEPOT_WIDGET_BUILD,
       
   212 		DEPOT_WIDGET_CLONE,
       
   213 		DEPOT_WIDGET_AUTOREPLACE,
       
   214 		WIDGET_LIST_END);
       
   215 
       
   216 	/* determine amount of items for scroller */
       
   217 	if (WP(w, depot_d).type == VEH_Train) {
       
   218 		hnum = 8;
       
   219 		for (num = 0; num < WP(w, depot_d).engine_count; num++) {
       
   220 			const Vehicle *v = vl[num];
       
   221 			hnum = maxu(hnum, v->u.rail.cached_total_length);
       
   222 		}
       
   223 		/* Always have 1 empty row, so people can change the setting of the train */
       
   224 		SetVScrollCount(w, WP(w, depot_d).engine_count + WP(w, depot_d).wagon_count + 1);
       
   225 		SetHScrollCount(w, WagonLengthToPixels(hnum));
       
   226 	} else {
       
   227 		SetVScrollCount(w, (num + w->hscroll.cap - 1) / w->hscroll.cap);
       
   228 	}
       
   229 
       
   230 	/* locate the depot struct */
       
   231 	if (WP(w, depot_d).type == VEH_Aircraft) {
       
   232 		SetDParam(0, GetStationIndex(tile)); // Airport name
       
   233 	} else {
       
   234 		Depot *depot = GetDepotByTile(tile);
       
   235 		assert(depot != NULL);
       
   236 
       
   237 		SetDParam(0, depot->town_index);
       
   238 	}
       
   239 
       
   240 	DrawWindowWidgets(w);
       
   241 
       
   242 	num = w->vscroll.pos * boxes_in_each_row;
       
   243 	max = min(WP(w, depot_d).engine_count, num + (rows_in_display * boxes_in_each_row));
       
   244 
       
   245 	for (x = 2, y = 15; num < max; y += w->resize.step_height, x = 2) { // Draw the rows
       
   246 		byte i;
       
   247 
       
   248 		for (i = 0; i < boxes_in_each_row && num < max; i++, num++, x += w->resize.step_width) {
       
   249 			/* Draw all vehicles in the current row */
       
   250 			const Vehicle *v = vl[num];
       
   251 			DrawVehicleInDepot(w, v, x, y);
       
   252 		}
       
   253 	}
       
   254 
       
   255 	max = min(WP(w, depot_d).engine_count + WP(w, depot_d).wagon_count, (w->vscroll.pos * boxes_in_each_row) + (rows_in_display * boxes_in_each_row));
       
   256 
       
   257 	/* draw the train wagons, that do not have an engine in front */
       
   258 	for (; num < max; num++, y += 14) {
       
   259 		const Vehicle *v = WP(w, depot_d).wagon_list[num - WP(w, depot_d).engine_count];
       
   260 		const Vehicle *u;
       
   261 
       
   262 		DrawTrainImage(v, x + 50, y, w->hscroll.cap - 29, 0, WP(w,depot_d).sel);
       
   263 		DrawString(x, y + 2, STR_8816, 0);
       
   264 
       
   265 		/*Draw the train counter */
       
   266 		i = 0;
       
   267 		u = v;
       
   268 		do i++; while ( (u=u->next) != NULL); // Determine length of train
       
   269 		SetDParam(0, i);                      // Set the counter
       
   270 		DrawStringRightAligned(w->widget[DEPOT_WIDGET_MATRIX].right - 1, y + 4, STR_TINY_BLACK, 0); // Draw the counter
       
   271 	}
       
   272 }
       
   273 
       
   274 typedef struct GetDepotVehiclePtData {
       
   275 	Vehicle *head;
       
   276 	Vehicle *wagon;
       
   277 } GetDepotVehiclePtData;
       
   278 
       
   279 enum {
       
   280 	MODE_ERROR        =  1,
       
   281 	MODE_DRAG_VEHICLE =  0,
       
   282 	MODE_SHOW_VEHICLE = -1,
       
   283 	MODE_START_STOP   = -2,
       
   284 };
       
   285 
       
   286 static int GetVehicleFromDepotWndPt(const Window *w, int x, int y, Vehicle **veh, GetDepotVehiclePtData *d)
       
   287 {
       
   288 	Vehicle **vl = WP(w, depot_d).vehicle_list;
       
   289 	uint xt, row, xm = 0, ym = 0;
       
   290 	int pos, skip = 0;
       
   291 	uint16 boxes_in_each_row = w->widget[DEPOT_WIDGET_MATRIX].data & 0xFF;
       
   292 
       
   293 	if (WP(w, depot_d).type == VEH_Train) {
       
   294 		xt = 0;
       
   295 		x -= 23;
       
   296 	} else {
       
   297 		xt = x / w->resize.step_width;
       
   298 		xm = x % w->resize.step_width;
       
   299 		if (xt >= w->hscroll.cap) return MODE_ERROR;
       
   300 
       
   301 		ym = (y - 14) % w->resize.step_height;
       
   302 	}
       
   303 
       
   304 	row = (y - 14) / w->resize.step_height;
       
   305 	if (row >= w->vscroll.cap) return MODE_ERROR;
       
   306 
       
   307 	pos = ((row + w->vscroll.pos) * boxes_in_each_row) + xt;
       
   308 
       
   309 	if (WP(w, depot_d).engine_count + WP(w, depot_d).wagon_count <= pos) {
       
   310 		if (WP(w, depot_d).type == VEH_Train) {
       
   311 			d->head  = NULL;
       
   312 			d->wagon = NULL;
       
   313 			return MODE_DRAG_VEHICLE;
       
   314 		} else {
       
   315 			return MODE_ERROR; // empty block, so no vehicle is selected
       
   316 		}
       
   317 	}
       
   318 
       
   319 	if (WP(w, depot_d).engine_count > pos) {
       
   320 		*veh = vl[pos];
       
   321 		skip = w->hscroll.pos;
       
   322 	} else {
       
   323 		vl = WP(w, depot_d).wagon_list;
       
   324 		pos -= WP(w, depot_d).engine_count;
       
   325 		*veh = vl[pos];
       
   326 		/* free wagons don't have an initial loco. */
       
   327 		x -= _traininfo_vehicle_width;
       
   328 	}
       
   329 
       
   330 	switch (WP(w, depot_d).type) {
       
   331 		case VEH_Train: {
       
   332 			Vehicle *v = *veh;
       
   333 			d->head = d->wagon = v;
       
   334 
       
   335 			/* either pressed the flag or the number, but only when it's a loco */
       
   336 			if (x < 0 && IsFrontEngine(v)) return (x >= -10) ? MODE_START_STOP : MODE_SHOW_VEHICLE;
       
   337 
       
   338 			skip = (skip * 8) / _traininfo_vehicle_width;
       
   339 			x = (x * 8) / _traininfo_vehicle_width;
       
   340 
       
   341 			/* Skip vehicles that are scrolled off the list */
       
   342 			x += skip;
       
   343 
       
   344 			/* find the vehicle in this row that was clicked */
       
   345 			while (v != NULL && (x -= v->u.rail.cached_veh_length) >= 0) v = v->next;
       
   346 
       
   347 			/* if an articulated part was selected, find its parent */
       
   348 			while (v != NULL && IsArticulatedPart(v)) v = GetPrevVehicleInChain(v);
       
   349 
       
   350 			d->wagon = v;
       
   351 
       
   352 			return MODE_DRAG_VEHICLE;
       
   353 			}
       
   354 			break;
       
   355 
       
   356 		case VEH_Road:
       
   357 			if (xm >= 24) return MODE_DRAG_VEHICLE;
       
   358 			if (xm <= 16) return MODE_SHOW_VEHICLE;
       
   359 			break;
       
   360 
       
   361 		case VEH_Ship:
       
   362 			if (xm >= 19) return MODE_DRAG_VEHICLE;
       
   363 			if (ym <= 10) return MODE_SHOW_VEHICLE;
       
   364 			break;
       
   365 
       
   366 		case VEH_Aircraft:
       
   367 			if (xm >= 12) return MODE_DRAG_VEHICLE;
       
   368 			if (ym <= 12) return MODE_SHOW_VEHICLE;
       
   369 			break;
       
   370 
       
   371 		default: NOT_REACHED();
       
   372 	}
       
   373 	return MODE_START_STOP;
       
   374 }
       
   375 
       
   376 static void TrainDepotMoveVehicle(Vehicle *wagon, VehicleID sel, Vehicle *head)
       
   377 {
       
   378 	Vehicle *v;
       
   379 
       
   380 	v = GetVehicle(sel);
       
   381 
       
   382 	if (v == wagon) return;
       
   383 
       
   384 	if (wagon == NULL) {
       
   385 		if (head != NULL) wagon = GetLastVehicleInChain(head);
       
   386 	} else  {
       
   387 		wagon = GetPrevVehicleInChain(wagon);
       
   388 		if (wagon == NULL) return;
       
   389 	}
       
   390 
       
   391 	if (wagon == v) return;
       
   392 
       
   393 	DoCommandP(v->tile, v->index + ((wagon == NULL ? INVALID_VEHICLE : wagon->index) << 16), _ctrl_pressed ? 1 : 0, NULL, CMD_MOVE_RAIL_VEHICLE | CMD_MSG(STR_8837_CAN_T_MOVE_VEHICLE));
       
   394 }
       
   395 
       
   396 static void DepotClick(Window *w, int x, int y)
       
   397 {
       
   398 	GetDepotVehiclePtData gdvp;
       
   399 	Vehicle *v = NULL;
       
   400 	int mode = GetVehicleFromDepotWndPt(w, x, y, &v, &gdvp);
       
   401 
       
   402 	/* share / copy orders */
       
   403 	if (_thd.place_mode && mode <= 0) {
       
   404 		_place_clicked_vehicle = (WP(w, depot_d).type == VEH_Train ? gdvp.head : v);
       
   405 		return;
       
   406 	}
       
   407 
       
   408 	if (WP(w, depot_d).type == VEH_Train) v = gdvp.wagon;
       
   409 
       
   410 	switch (mode) {
       
   411 		case MODE_ERROR: // invalid
       
   412 			return;
       
   413 
       
   414 		case MODE_DRAG_VEHICLE: { // start dragging of vehicle
       
   415 			VehicleID sel = WP(w, depot_d).sel;
       
   416 
       
   417 			if (WP(w, depot_d).type == VEH_Train && sel != INVALID_VEHICLE) {
       
   418 				WP(w,depot_d).sel = INVALID_VEHICLE;
       
   419 				TrainDepotMoveVehicle(v, sel, gdvp.head);
       
   420 			} else if (v != NULL) {
       
   421 				int image;
       
   422 
       
   423 				switch (WP(w, depot_d).type) {
       
   424 					case VEH_Train:    image = GetTrainImage(v, DIR_W);    break;
       
   425 					case VEH_Road:     image = GetRoadVehImage(v, DIR_W);  break;
       
   426 					case VEH_Ship:     image = GetShipImage(v, DIR_W);     break;
       
   427 					case VEH_Aircraft: image = GetAircraftImage(v, DIR_W); break;
       
   428 					default: NOT_REACHED(); image = 0;
       
   429 				}
       
   430 
       
   431 				WP(w, depot_d).sel = v->index;
       
   432 				SetWindowDirty(w);
       
   433 				SetObjectToPlaceWnd(GetVehiclePalette(v) | image, 4, w);
       
   434 			}
       
   435 			}
       
   436 			break;
       
   437 
       
   438 		case MODE_SHOW_VEHICLE: // show info window
       
   439 			ShowVehicleViewWindow(v);
       
   440 			break;
       
   441 
       
   442 		case MODE_START_STOP: { // click start/stop flag
       
   443 			uint command;
       
   444 
       
   445 			switch (WP(w, depot_d).type) {
       
   446 				case VEH_Train:    command = CMD_START_STOP_TRAIN | CMD_MSG(STR_883B_CAN_T_STOP_START_TRAIN);          break;
       
   447 				case VEH_Road:     command = CMD_START_STOP_ROADVEH | CMD_MSG(STR_9015_CAN_T_STOP_START_ROAD_VEHICLE); break;
       
   448 				case VEH_Ship:     command = CMD_START_STOP_SHIP | CMD_MSG(STR_9818_CAN_T_STOP_START_SHIP);            break;
       
   449 				case VEH_Aircraft: command = CMD_START_STOP_AIRCRAFT | CMD_MSG(STR_A016_CAN_T_STOP_START_AIRCRAFT);    break;
       
   450 				default: NOT_REACHED(); command = 0;
       
   451 			}
       
   452 			DoCommandP(v->tile, v->index, 0, NULL, command);
       
   453 			}
       
   454 			break;
       
   455 
       
   456 		default: NOT_REACHED();
       
   457 	}
       
   458 }
       
   459 
       
   460 /**
       
   461  * Clones a vehicle
       
   462  * @param *v is the original vehicle to clone
       
   463  * @param *w is the window of the depot where the clone is build
       
   464  */
       
   465 static void HandleCloneVehClick(const Vehicle *v, const Window *w)
       
   466 {
       
   467 	uint error_str;
       
   468 
       
   469 	if (v == NULL) return;
       
   470 
       
   471 	if (v->type == VEH_Train && !IsFrontEngine(v)) {
       
   472 		v = GetFirstVehicleInChain(v);
       
   473 		/* Do nothing when clicking on a train in depot with no loc attached */
       
   474 		if (!IsFrontEngine(v)) return;
       
   475 	}
       
   476 
       
   477 	switch (v->type) {
       
   478 		case VEH_Train:    error_str = CMD_MSG(STR_882B_CAN_T_BUILD_RAILROAD_VEHICLE); break;
       
   479 		case VEH_Road:     error_str = CMD_MSG(STR_9009_CAN_T_BUILD_ROAD_VEHICLE);     break;
       
   480 		case VEH_Ship:     error_str = CMD_MSG(STR_980D_CAN_T_BUILD_SHIP);             break;
       
   481 		case VEH_Aircraft: error_str = CMD_MSG(STR_A008_CAN_T_BUILD_AIRCRAFT);         break;
       
   482 		default: return;
       
   483 	}
       
   484 
       
   485 	DoCommandP(w->window_number, v->index, _ctrl_pressed ? 1 : 0, CcCloneVehicle, CMD_CLONE_VEHICLE | error_str);
       
   486 
       
   487 	ResetObjectToPlace();
       
   488 }
       
   489 
       
   490 static void ClonePlaceObj(const Window *w)
       
   491 {
       
   492 	const Vehicle *v = CheckMouseOverVehicle();
       
   493 
       
   494 	if (v != NULL) HandleCloneVehClick(v, w);
       
   495 }
       
   496 
       
   497 static void ResizeDepotButtons(Window *w)
       
   498 {
       
   499 	/* We got the widget moved around. Now we will make some widgets to fill the gap between some widgets in equal sizes */
       
   500 
       
   501 	/* Make the buttons in the bottom equal in size */
       
   502 	w->widget[DEPOT_WIDGET_BUILD].right    = w->widget[DEPOT_WIDGET_LOCATION].right / 3;
       
   503 	w->widget[DEPOT_WIDGET_LOCATION].left  = w->widget[DEPOT_WIDGET_BUILD].right * 2;
       
   504 	w->widget[DEPOT_WIDGET_CLONE].left     = w->widget[DEPOT_WIDGET_BUILD].right + 1;
       
   505 	w->widget[DEPOT_WIDGET_CLONE].right    = w->widget[DEPOT_WIDGET_LOCATION].left - 1;
       
   506 
       
   507 	if (WP(w, depot_d).type == VEH_Train) {
       
   508 		/* Divide the size of DEPOT_WIDGET_SELL into two equally big buttons so DEPOT_WIDGET_SELL and DEPOT_WIDGET_SELL_CHAIN will get the same size.
       
   509 		* This way it will stay the same even if DEPOT_WIDGET_SELL_CHAIN is resized for some reason                                                  */
       
   510 		w->widget[DEPOT_WIDGET_SELL_CHAIN].top    = ((w->widget[DEPOT_WIDGET_SELL_CHAIN].bottom - w->widget[DEPOT_WIDGET_SELL].top) / 2) + w->widget[DEPOT_WIDGET_SELL].top;
       
   511 		w->widget[DEPOT_WIDGET_SELL].bottom     = w->widget[DEPOT_WIDGET_SELL_CHAIN].top - 1;
       
   512 	}
       
   513 }
       
   514 
       
   515 /* Function to set up vehicle specific sprites and strings
       
   516  * Only use this if it's the same widget, that's used for more than one vehicle type and it needs different text/sprites
       
   517  * Vehicle specific text/sprites, that's in a widget, that's only shown for one vehicle type (like sell whole train) is set in the widget array
       
   518  */
       
   519 static void SetupStringsForDepotWindow(Window *w, byte type)
       
   520 {
       
   521 	switch (type) {
       
   522 		case VEH_Train:
       
   523 			w->widget[DEPOT_WIDGET_CAPTION].data      = STR_8800_TRAIN_DEPOT;
       
   524 			w->widget[DEPOT_WIDGET_STOP_ALL].tooltips = STR_MASS_STOP_DEPOT_TRAIN_TIP;
       
   525 			w->widget[DEPOT_WIDGET_START_ALL].tooltips=	STR_MASS_START_DEPOT_TRAIN_TIP;
       
   526 			w->widget[DEPOT_WIDGET_SELL].tooltips     = STR_8841_DRAG_TRAIN_VEHICLE_TO_HERE;
       
   527 			w->widget[DEPOT_WIDGET_SELL_ALL].tooltips =	STR_DEPOT_SELL_ALL_BUTTON_TRAIN_TIP;
       
   528 			w->widget[DEPOT_WIDGET_MATRIX].tooltips   = STR_883F_TRAINS_CLICK_ON_TRAIN_FOR;
       
   529 
       
   530 			w->widget[DEPOT_WIDGET_BUILD].data        = STR_8815_NEW_VEHICLES;
       
   531 			w->widget[DEPOT_WIDGET_BUILD].tooltips    = STR_8840_BUILD_NEW_TRAIN_VEHICLE;
       
   532 			w->widget[DEPOT_WIDGET_CLONE].data        = STR_CLONE_TRAIN;
       
   533 			w->widget[DEPOT_WIDGET_CLONE].tooltips    = STR_CLONE_TRAIN_DEPOT_INFO;
       
   534 
       
   535 			w->widget[DEPOT_WIDGET_LOCATION].tooltips = STR_8842_CENTER_MAIN_VIEW_ON_TRAIN;
       
   536 			w->widget[DEPOT_WIDGET_VEHICLE_LIST].data = STR_TRAIN;
       
   537 			w->widget[DEPOT_WIDGET_VEHICLE_LIST].tooltips = STR_DEPOT_VEHICLE_ORDER_LIST_TRAIN_TIP;
       
   538 			w->widget[DEPOT_WIDGET_AUTOREPLACE].tooltips = STR_DEPOT_AUTOREPLACE_TRAIN_TIP;
       
   539 
       
   540 			/* Sprites */
       
   541 			w->widget[DEPOT_WIDGET_SELL].data        = SPR_SELL_TRAIN;
       
   542 			w->widget[DEPOT_WIDGET_SELL_ALL].data    = SPR_SELL_ALL_TRAIN;
       
   543 			w->widget[DEPOT_WIDGET_AUTOREPLACE].data = SPR_REPLACE_TRAIN;
       
   544 			break;
       
   545 
       
   546 		case VEH_Road:
       
   547 			w->widget[DEPOT_WIDGET_CAPTION].data      = STR_9003_ROAD_VEHICLE_DEPOT;
       
   548 			w->widget[DEPOT_WIDGET_STOP_ALL].tooltips = STR_MASS_STOP_DEPOT_ROADVEH_TIP;
       
   549 			w->widget[DEPOT_WIDGET_START_ALL].tooltips=	STR_MASS_START_DEPOT_ROADVEH_TIP;
       
   550 			w->widget[DEPOT_WIDGET_SELL].tooltips     = STR_9024_DRAG_ROAD_VEHICLE_TO_HERE;
       
   551 			w->widget[DEPOT_WIDGET_SELL_ALL].tooltips =	STR_DEPOT_SELL_ALL_BUTTON_ROADVEH_TIP;
       
   552 			w->widget[DEPOT_WIDGET_MATRIX].tooltips   = STR_9022_VEHICLES_CLICK_ON_VEHICLE;
       
   553 
       
   554 			w->widget[DEPOT_WIDGET_BUILD].data        = STR_9004_NEW_VEHICLES;
       
   555 			w->widget[DEPOT_WIDGET_BUILD].tooltips    = STR_9023_BUILD_NEW_ROAD_VEHICLE;
       
   556 			w->widget[DEPOT_WIDGET_CLONE].data        = STR_CLONE_ROAD_VEHICLE;
       
   557 			w->widget[DEPOT_WIDGET_CLONE].tooltips    = STR_CLONE_ROAD_VEHICLE_DEPOT_INFO;
       
   558 
       
   559 			w->widget[DEPOT_WIDGET_LOCATION].tooltips = STR_9025_CENTER_MAIN_VIEW_ON_ROAD;
       
   560 			w->widget[DEPOT_WIDGET_VEHICLE_LIST].data = STR_LORRY;
       
   561 			w->widget[DEPOT_WIDGET_VEHICLE_LIST].tooltips = STR_DEPOT_VEHICLE_ORDER_LIST_ROADVEH_TIP;
       
   562 			w->widget[DEPOT_WIDGET_AUTOREPLACE].tooltips = STR_DEPOT_AUTOREPLACE_ROADVEH_TIP;
       
   563 
       
   564 			/* Sprites */
       
   565 			w->widget[DEPOT_WIDGET_SELL].data        = SPR_SELL_ROADVEH;
       
   566 			w->widget[DEPOT_WIDGET_SELL_ALL].data    = SPR_SELL_ALL_ROADVEH;
       
   567 			w->widget[DEPOT_WIDGET_AUTOREPLACE].data = SPR_REPLACE_ROADVEH;
       
   568 			break;
       
   569 
       
   570 		case VEH_Ship:
       
   571 			w->widget[DEPOT_WIDGET_CAPTION].data      = STR_9803_SHIP_DEPOT;
       
   572 			w->widget[DEPOT_WIDGET_STOP_ALL].tooltips = STR_MASS_STOP_DEPOT_SHIP_TIP;
       
   573 			w->widget[DEPOT_WIDGET_START_ALL].tooltips=	STR_MASS_START_DEPOT_SHIP_TIP;
       
   574 			w->widget[DEPOT_WIDGET_SELL].tooltips     = STR_9821_DRAG_SHIP_TO_HERE_TO_SELL;
       
   575 			w->widget[DEPOT_WIDGET_SELL_ALL].tooltips =	STR_DEPOT_SELL_ALL_BUTTON_SHIP_TIP;
       
   576 			w->widget[DEPOT_WIDGET_MATRIX].tooltips   = STR_981F_SHIPS_CLICK_ON_SHIP_FOR;
       
   577 
       
   578 			w->widget[DEPOT_WIDGET_BUILD].data        = STR_9804_NEW_SHIPS;
       
   579 			w->widget[DEPOT_WIDGET_BUILD].tooltips    = STR_9820_BUILD_NEW_SHIP;
       
   580 			w->widget[DEPOT_WIDGET_CLONE].data        = STR_CLONE_SHIP;
       
   581 			w->widget[DEPOT_WIDGET_CLONE].tooltips    = STR_CLONE_SHIP_DEPOT_INFO;
       
   582 
       
   583 			w->widget[DEPOT_WIDGET_LOCATION].tooltips = STR_9822_CENTER_MAIN_VIEW_ON_SHIP;
       
   584 			w->widget[DEPOT_WIDGET_VEHICLE_LIST].data = STR_SHIP;
       
   585 			w->widget[DEPOT_WIDGET_VEHICLE_LIST].tooltips = STR_DEPOT_VEHICLE_ORDER_LIST_SHIP_TIP;
       
   586 			w->widget[DEPOT_WIDGET_AUTOREPLACE].tooltips = STR_DEPOT_AUTOREPLACE_SHIP_TIP;
       
   587 
       
   588 			/* Sprites */
       
   589 			w->widget[DEPOT_WIDGET_SELL].data        = SPR_SELL_SHIP;
       
   590 			w->widget[DEPOT_WIDGET_SELL_ALL].data    = SPR_SELL_ALL_SHIP;
       
   591 			w->widget[DEPOT_WIDGET_AUTOREPLACE].data = SPR_REPLACE_SHIP;
       
   592 			break;
       
   593 
       
   594 		case VEH_Aircraft:
       
   595 			w->widget[DEPOT_WIDGET_CAPTION].data      = STR_A002_AIRCRAFT_HANGAR;
       
   596 			w->widget[DEPOT_WIDGET_STOP_ALL].tooltips = STR_MASS_STOP_HANGAR_TIP;
       
   597 			w->widget[DEPOT_WIDGET_START_ALL].tooltips=	STR_MASS_START_HANGAR_TIP;
       
   598 			w->widget[DEPOT_WIDGET_SELL].tooltips     = STR_A023_DRAG_AIRCRAFT_TO_HERE_TO;
       
   599 			w->widget[DEPOT_WIDGET_SELL_ALL].tooltips =	STR_DEPOT_SELL_ALL_BUTTON_AIRCRAFT_TIP;
       
   600 			w->widget[DEPOT_WIDGET_MATRIX].tooltips   = STR_A021_AIRCRAFT_CLICK_ON_AIRCRAFT;
       
   601 
       
   602 			w->widget[DEPOT_WIDGET_BUILD].data        = STR_A003_NEW_AIRCRAFT;
       
   603 			w->widget[DEPOT_WIDGET_BUILD].tooltips    = STR_A022_BUILD_NEW_AIRCRAFT;
       
   604 			w->widget[DEPOT_WIDGET_CLONE].data        = STR_CLONE_AIRCRAFT;
       
   605 			w->widget[DEPOT_WIDGET_CLONE].tooltips    = STR_CLONE_AIRCRAFT_INFO_HANGAR_WINDOW;
       
   606 
       
   607 			w->widget[DEPOT_WIDGET_LOCATION].tooltips = STR_A024_CENTER_MAIN_VIEW_ON_HANGAR;
       
   608 			w->widget[DEPOT_WIDGET_VEHICLE_LIST].data = STR_PLANE;
       
   609 			w->widget[DEPOT_WIDGET_VEHICLE_LIST].tooltips = STR_DEPOT_VEHICLE_ORDER_LIST_AIRCRAFT_TIP;
       
   610 			w->widget[DEPOT_WIDGET_AUTOREPLACE].tooltips = STR_DEPOT_AUTOREPLACE_AIRCRAFT_TIP;
       
   611 
       
   612 			/* Sprites */
       
   613 			w->widget[DEPOT_WIDGET_SELL].data        = SPR_SELL_AIRCRAFT;
       
   614 			w->widget[DEPOT_WIDGET_SELL_ALL].data    = SPR_SELL_ALL_AIRCRAFT;
       
   615 			w->widget[DEPOT_WIDGET_AUTOREPLACE].data = SPR_REPLACE_AIRCRAFT;
       
   616 			break;
       
   617 	}
       
   618 }
       
   619 
       
   620 static void CreateDepotListWindow(Window *w, byte type)
       
   621 {
       
   622 	WP(w, depot_d).type = type;
       
   623 	_backup_orders_tile = 0;
       
   624 
       
   625 	/* Resize the window according to the vehicle type */
       
   626 	switch (type) {
       
   627 		default: NOT_REACHED();
       
   628 		case VEH_Train:
       
   629 			w->vscroll.cap = 6;
       
   630 			w->hscroll.cap = 10 * 29;
       
   631 			w->resize.step_width = 1;
       
   632 			ResizeWindow(w, 56, 26);
       
   633 			break;
       
   634 
       
   635 		case VEH_Road:
       
   636 			w->vscroll.cap = 5;
       
   637 			w->hscroll.cap = 5;
       
   638 			w->resize.step_width = 56;
       
   639 			ResizeWindow(w, 10, 0);
       
   640 			break;
       
   641 
       
   642 		case VEH_Ship:
       
   643 			w->vscroll.cap = 3;
       
   644 			w->hscroll.cap = 3;
       
   645 			w->resize.step_width = 90;
       
   646 			ResizeWindow(w, 0, 2);
       
   647 			break;
       
   648 
       
   649 		case VEH_Aircraft:
       
   650 			w->vscroll.cap = 3;
       
   651 			w->hscroll.cap = 4;
       
   652 			w->resize.step_width = 74;
       
   653 			ResizeWindow(w, 26, 2);
       
   654 			break;
       
   655 	}
       
   656 
       
   657 	/* Set the minimum window size to the current window size */
       
   658 	w->resize.width = w->width;
       
   659 	w->resize.height = w->height;
       
   660 	w->resize.step_height = GetVehicleListHeight(type);
       
   661 
       
   662 	SetupStringsForDepotWindow(w, type);
       
   663 
       
   664 	w->widget[DEPOT_WIDGET_MATRIX].data =
       
   665 		(w->vscroll.cap * 0x100) // number of rows to draw on the background
       
   666 		+ (type == VEH_Train ? 1 : w->hscroll.cap); // number of boxes in each row. Trains always have just one
       
   667 
       
   668 
       
   669 	SetWindowWidgetsHiddenState(w, type != VEH_Train,
       
   670 		DEPOT_WIDGET_H_SCROLL,
       
   671 		DEPOT_WIDGET_SELL_CHAIN,
       
   672 		WIDGET_LIST_END);
       
   673 
       
   674 	/* The train depot has a horizontal scroller, make the matrix that much shorter to fit */
       
   675 	if (type == VEH_Train) w->widget[DEPOT_WIDGET_MATRIX].bottom -= 12;
       
   676 	ResizeDepotButtons(w);
       
   677 }
       
   678 
       
   679 void DepotSortList(Vehicle **v, uint16 length);
       
   680 
       
   681 static void DepotWndProc(Window *w, WindowEvent *e)
       
   682 {
       
   683 	switch (e->event) {
       
   684 		case WE_CREATE:
       
   685 			WP(w, depot_d).sel = INVALID_VEHICLE;
       
   686 			WP(w, depot_d).vehicle_list  = NULL;
       
   687 			WP(w, depot_d).wagon_list    = NULL;
       
   688 			WP(w, depot_d).engine_count  = 0;
       
   689 			WP(w, depot_d).wagon_count   = 0;
       
   690 			WP(w, depot_d).generate_list = true;
       
   691 			break;
       
   692 
       
   693 		case WE_INVALIDATE_DATA:
       
   694 			WP(w, depot_d).generate_list = true;
       
   695 			break;
       
   696 
       
   697 		case WE_PAINT:
       
   698 			if (WP(w, depot_d).generate_list) {
       
   699 				/* Generate the vehicle list
       
   700 				 * It's ok to use the wagon pointers for non-trains as they will be ignored */
       
   701 				BuildDepotVehicleList(WP(w, depot_d).type, w->window_number,
       
   702 					&WP(w, depot_d).vehicle_list, &WP(w, depot_d).engine_list_length, &WP(w, depot_d).engine_count,
       
   703 					&WP(w, depot_d).wagon_list,   &WP(w, depot_d).wagon_list_length,  &WP(w, depot_d).wagon_count);
       
   704 				WP(w, depot_d).generate_list = false;
       
   705 				DepotSortList(WP(w, depot_d).vehicle_list, WP(w, depot_d).engine_count);
       
   706 //#ifndef NDEBUG
       
   707 #if 0
       
   708 /* We disabled this check for now, but will keep it to quickly make this test again later (if we change some code) */
       
   709 			} else {
       
   710 				/* Here we got a piece of code, that only checks if we got a different number of vehicles in the depot list and the number of vehicles actually being in the depot.
       
   711 				 * IF they aren't the same, then WE_INVALIDATE_DATA should have been called somewhere, but it wasn't and we got a bug
       
   712 				 * Since this is a time consuming check and not nice to memory fragmentation, it may not stay for long, but it's a good way to check this
       
   713 				 * We can turn it on/off by switching between #ifndef NDEBUG and #if 0 */
       
   714 				Vehicle **engines = NULL, **wagons = NULL;
       
   715 				uint16 engine_count = 0, engine_length = 0;
       
   716 				uint16 wagon_count  = 0, wagon_length  = 0;
       
   717 				BuildDepotVehicleList(WP(w, depot_d).type, w->window_number, &engines, &engine_length, &engine_count,
       
   718 									  &wagons,  &wagon_length,  &wagon_count);
       
   719 
       
   720 				assert(engine_count == WP(w, depot_d).engine_count);
       
   721 				assert(wagon_count == WP(w, depot_d).wagon_count);
       
   722 				free((void*)engines);
       
   723 				free((void*)wagons);
       
   724 #endif
       
   725 			}
       
   726 			DrawDepotWindow(w);
       
   727 			break;
       
   728 
       
   729 		case WE_CLICK:
       
   730 			switch (e->we.click.widget) {
       
   731 				case DEPOT_WIDGET_MATRIX: // List
       
   732 					DepotClick(w, e->we.click.pt.x, e->we.click.pt.y);
       
   733 					break;
       
   734 
       
   735 				case DEPOT_WIDGET_BUILD: // Build vehicle
       
   736 					ResetObjectToPlace();
       
   737 					switch (WP(w, depot_d).type) {
       
   738 						case VEH_Train:    ShowBuildTrainWindow(w->window_number);    break;
       
   739 						case VEH_Road:     ShowBuildRoadVehWindow(w->window_number);  break;
       
   740 						case VEH_Ship:     ShowBuildShipWindow(w->window_number);     break;
       
   741 						case VEH_Aircraft:
       
   742 							ShowBuildVehicleWindow(w->window_number, WP(w, depot_d).type);
       
   743 							break;
       
   744 					default: NOT_REACHED();
       
   745 					}
       
   746 					break;
       
   747 
       
   748 				case DEPOT_WIDGET_CLONE: // Clone button
       
   749 					InvalidateWidget(w, DEPOT_WIDGET_CLONE);
       
   750 					ToggleWidgetLoweredState(w, DEPOT_WIDGET_CLONE);
       
   751 
       
   752 					if (IsWindowWidgetLowered(w, DEPOT_WIDGET_CLONE)) {
       
   753 						static const CursorID clone_icons[] = {
       
   754 							SPR_CURSOR_CLONE_TRAIN, SPR_CURSOR_CLONE_ROADVEH,
       
   755 							SPR_CURSOR_CLONE_SHIP, SPR_CURSOR_CLONE_AIRPLANE
       
   756 						};
       
   757 
       
   758 						_place_clicked_vehicle = NULL;
       
   759 						SetObjectToPlaceWnd(clone_icons[WP(w, depot_d).type - VEH_Train], VHM_RECT, w);
       
   760 					} else {
       
   761 						ResetObjectToPlace();
       
   762 					}
       
   763 						break;
       
   764 
       
   765 				case DEPOT_WIDGET_LOCATION: ScrollMainWindowToTile(w->window_number); break;
       
   766 
       
   767 				case DEPOT_WIDGET_STOP_ALL:
       
   768 				case DEPOT_WIDGET_START_ALL:
       
   769 					DoCommandP(w->window_number, 0, WP(w, depot_d).type | (e->we.click.widget == DEPOT_WIDGET_START_ALL ? (1 << 5) : 0), NULL, CMD_MASS_START_STOP);
       
   770 					break;
       
   771 
       
   772 				case DEPOT_WIDGET_SELL_ALL:
       
   773 					/* Only open the confimation window if there are anything to sell */
       
   774 					if (WP(w, depot_d).engine_count != 0 || WP(w, depot_d).wagon_count != 0) {
       
   775 						static const StringID confirm_captions[] = {
       
   776 							STR_8800_TRAIN_DEPOT,
       
   777 							STR_9003_ROAD_VEHICLE_DEPOT,
       
   778 							STR_9803_SHIP_DEPOT,
       
   779 							STR_A002_AIRCRAFT_HANGAR
       
   780 						};
       
   781 						TileIndex tile = w->window_number;
       
   782 						byte vehtype = WP(w, depot_d).type;
       
   783 
       
   784 						SetDParam(0, (vehtype == VEH_Aircraft) ? GetStationIndex(tile) : GetDepotByTile(tile)->town_index);
       
   785 						ShowQuery(
       
   786 							confirm_captions[vehtype - VEH_Train],
       
   787 							STR_DEPOT_SELL_CONFIRMATION_TEXT,
       
   788 							w,
       
   789 							DepotSellAllConfirmationCallback
       
   790 						);
       
   791 					}
       
   792 					break;
       
   793 
       
   794 				case DEPOT_WIDGET_VEHICLE_LIST:
       
   795 					ShowVehDepotOrders(GetTileOwner(w->window_number), WP(w, depot_d).type, w->window_number);
       
   796 					break;
       
   797 
       
   798 				case DEPOT_WIDGET_AUTOREPLACE:
       
   799 					DoCommandP(w->window_number, WP(w, depot_d).type, 0, NULL, CMD_DEPOT_MASS_AUTOREPLACE);
       
   800 					break;
       
   801 
       
   802 			}
       
   803 			break;
       
   804 
       
   805 		case WE_PLACE_OBJ: {
       
   806 			ClonePlaceObj(w);
       
   807 		} break;
       
   808 
       
   809 		case WE_ABORT_PLACE_OBJ: {
       
   810 			RaiseWindowWidget(w, DEPOT_WIDGET_CLONE);
       
   811 			InvalidateWidget(w, DEPOT_WIDGET_CLONE);
       
   812 		} break;
       
   813 
       
   814 			/* check if a vehicle in a depot was clicked.. */
       
   815 		case WE_MOUSELOOP: {
       
   816 			const Vehicle *v = _place_clicked_vehicle;
       
   817 
       
   818 			/* since OTTD checks all open depot windows, we will make sure that it triggers the one with a clicked clone button */
       
   819 			if (v != NULL && IsWindowWidgetLowered(w, DEPOT_WIDGET_CLONE)) {
       
   820 				_place_clicked_vehicle = NULL;
       
   821 				HandleCloneVehClick(v, w);
       
   822 			}
       
   823 		} break;
       
   824 
       
   825 		case WE_DESTROY:
       
   826 			DeleteWindowById(WC_BUILD_VEHICLE, w->window_number);
       
   827 			free((void*)WP(w, depot_d).vehicle_list);
       
   828 			free((void*)WP(w, depot_d).wagon_list);
       
   829 			break;
       
   830 
       
   831 		case WE_DRAGDROP:
       
   832 			switch (e->we.click.widget) {
       
   833 				case DEPOT_WIDGET_MATRIX: {
       
   834 					Vehicle *v;
       
   835 					VehicleID sel = WP(w, depot_d).sel;
       
   836 
       
   837 					WP(w, depot_d).sel = INVALID_VEHICLE;
       
   838 					SetWindowDirty(w);
       
   839 
       
   840 					if (WP(w, depot_d).type == VEH_Train) {
       
   841 						GetDepotVehiclePtData gdvp;
       
   842 
       
   843 						if (GetVehicleFromDepotWndPt(w, e->we.dragdrop.pt.x, e->we.dragdrop.pt.y, &v, &gdvp) == MODE_DRAG_VEHICLE &&
       
   844 							sel != INVALID_VEHICLE) {
       
   845 							if (gdvp.wagon != NULL && gdvp.wagon->index == sel && _ctrl_pressed) {
       
   846 								DoCommandP(GetVehicle(sel)->tile, GetVehicle(sel)->index, true, NULL, CMD_REVERSE_TRAIN_DIRECTION | CMD_MSG(STR_9033_CAN_T_MAKE_VEHICLE_TURN));
       
   847 							} else if (gdvp.wagon == NULL || gdvp.wagon->index != sel) {
       
   848 								TrainDepotMoveVehicle(gdvp.wagon, sel, gdvp.head);
       
   849 							} else if (gdvp.head != NULL && IsFrontEngine(gdvp.head)) {
       
   850 								ShowTrainViewWindow(gdvp.head);
       
   851 							}
       
   852 						}
       
   853 					} else if (GetVehicleFromDepotWndPt(w, e->we.dragdrop.pt.x, e->we.dragdrop.pt.y, &v, NULL) == MODE_DRAG_VEHICLE &&
       
   854 						v != NULL &&
       
   855 						sel == v->index) {
       
   856 						ShowVehicleViewWindow(v);
       
   857 					}
       
   858 				} break;
       
   859 
       
   860 				case DEPOT_WIDGET_SELL: case DEPOT_WIDGET_SELL_CHAIN:
       
   861 					if (!IsWindowWidgetDisabled(w, DEPOT_WIDGET_SELL) &&
       
   862 						WP(w, depot_d).sel != INVALID_VEHICLE) {
       
   863 						Vehicle *v;
       
   864 						uint command;
       
   865 						int sell_cmd;
       
   866 						bool is_engine;
       
   867 
       
   868 						if (IsWindowWidgetDisabled(w, e->we.click.widget)) return;
       
   869 						if (WP(w, depot_d).sel == INVALID_VEHICLE) return;
       
   870 
       
   871 						HandleButtonClick(w, e->we.click.widget);
       
   872 
       
   873 						v = GetVehicle(WP(w, depot_d).sel);
       
   874 						WP(w, depot_d).sel = INVALID_VEHICLE;
       
   875 						SetWindowDirty(w);
       
   876 
       
   877 						sell_cmd = (v->type == VEH_Train && (e->we.click.widget == DEPOT_WIDGET_SELL_CHAIN || _ctrl_pressed)) ? 1 : 0;
       
   878 
       
   879 						is_engine = (!(v->type == VEH_Train && !IsFrontEngine(v)));
       
   880 
       
   881 						if (is_engine) {
       
   882 							_backup_orders_tile = v->tile;
       
   883 							BackupVehicleOrders(v, _backup_orders_data);
       
   884 						}
       
   885 
       
   886 						switch (v->type) {
       
   887 							case VEH_Train:    command = CMD_SELL_RAIL_WAGON | CMD_MSG(STR_8839_CAN_T_SELL_RAILROAD_VEHICLE); break;
       
   888 							case VEH_Road:     command = CMD_SELL_ROAD_VEH | CMD_MSG(STR_9014_CAN_T_SELL_ROAD_VEHICLE);       break;
       
   889 							case VEH_Ship:     command = CMD_SELL_SHIP | CMD_MSG(STR_980C_CAN_T_SELL_SHIP);                   break;
       
   890 							case VEH_Aircraft: command = CMD_SELL_AIRCRAFT | CMD_MSG(STR_A01C_CAN_T_SELL_AIRCRAFT);           break;
       
   891 							default: NOT_REACHED(); command = 0;
       
   892 						}
       
   893 
       
   894 						if (!DoCommandP(v->tile, v->index, sell_cmd, NULL, command) && is_engine) _backup_orders_tile = 0;
       
   895 					}
       
   896 					break;
       
   897 				default:
       
   898 					WP(w, depot_d).sel = INVALID_VEHICLE;
       
   899 					SetWindowDirty(w);
       
   900 			}
       
   901 			break;
       
   902 
       
   903 		case WE_RESIZE:
       
   904 			w->vscroll.cap += e->we.sizing.diff.y / (int)w->resize.step_height;
       
   905 			w->hscroll.cap += e->we.sizing.diff.x / (int)w->resize.step_width;
       
   906 			w->widget[DEPOT_WIDGET_MATRIX].data = (w->vscroll.cap << 8) + (WP(w, depot_d).type == VEH_Train ? 1 : w->hscroll.cap);
       
   907 			ResizeDepotButtons(w);
       
   908 			break;
       
   909 	}
       
   910 }
       
   911 
       
   912 /** Opens a depot window
       
   913  * @param tile The tile where the depot/hangar is located
       
   914  * @param type The type of vehicles in the depot
       
   915  */
       
   916 void ShowDepotWindow(TileIndex tile, byte type)
       
   917 {
       
   918 	Window *w;
       
   919 
       
   920 	switch (type) {
       
   921 		default: NOT_REACHED();
       
   922 		case VEH_Train:
       
   923 			w = AllocateWindowDescFront(&_train_depot_desc, tile); break;
       
   924 		case VEH_Road:
       
   925 			w = AllocateWindowDescFront(&_road_depot_desc, tile); break;
       
   926 		case VEH_Ship:
       
   927 			w = AllocateWindowDescFront(&_ship_depot_desc, tile); break;
       
   928 		case VEH_Aircraft:
       
   929 			w = AllocateWindowDescFront(&_aircraft_depot_desc, tile); break;
       
   930 	}
       
   931 
       
   932 	if (w != NULL) {
       
   933 		w->caption_color = GetTileOwner(tile);
       
   934 		CreateDepotListWindow(w, type);
       
   935 	}
       
   936 }
       
   937 
       
   938 /** Removes the highlight of a vehicle in a depot window
       
   939  * @param *v Vehicle to remove all highlights from
       
   940  */
       
   941 void DeleteDepotHighlightOfVehicle(const Vehicle *v)
       
   942 {
       
   943 	Window *w;
       
   944 
       
   945 	/* If we haven't got any vehicles on the mouse pointer, we haven't got any highlighted in any depots either
       
   946 	 * If that is the case, we can skip looping though the windows and save time                                */
       
   947 	if (_special_mouse_mode != WSM_DRAGDROP) return;
       
   948 
       
   949 	w = FindWindowById(WC_VEHICLE_DEPOT, v->tile);
       
   950 	if (w != NULL) {
       
   951 		WP(w, depot_d).sel = INVALID_VEHICLE;
       
   952 		ResetObjectToPlace();
       
   953 	}
       
   954 }