1 /*
2  * This file is part of OpenTTD.
3  * 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.
4  * 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.
5  * 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/>.
6  */
7 
8 /** @file order_gui.cpp GUI related to orders. */
9 
10 #include "stdafx.h"
11 #include "command_func.h"
12 #include "viewport_func.h"
13 #include "depot_map.h"
14 #include "roadveh.h"
15 #include "timetable.h"
16 #include "strings_func.h"
17 #include "company_func.h"
18 #include "widgets/dropdown_type.h"
19 #include "widgets/dropdown_func.h"
20 #include "textbuf_gui.h"
21 #include "string_func.h"
22 #include "tilehighlight_func.h"
23 #include "network/network.h"
24 #include "station_base.h"
25 #include "industry.h"
26 #include "waypoint_base.h"
27 #include "core/geometry_func.hpp"
28 #include "hotkeys.h"
29 #include "aircraft.h"
30 #include "engine_func.h"
31 #include "vehicle_func.h"
32 
33 #include "widgets/order_widget.h"
34 
35 #include "safeguards.h"
36 
37 
38 /** Order load types that could be given to station orders. */
39 static const StringID _station_load_types[][5][5] = {
40 	{
41 		/* No refitting. */
42 		{
43 			STR_EMPTY,
44 			INVALID_STRING_ID,
45 			STR_ORDER_FULL_LOAD,
46 			STR_ORDER_FULL_LOAD_ANY,
47 			STR_ORDER_NO_LOAD,
48 		}, {
49 			STR_ORDER_UNLOAD,
50 			INVALID_STRING_ID,
51 			STR_ORDER_UNLOAD_FULL_LOAD,
52 			STR_ORDER_UNLOAD_FULL_LOAD_ANY,
53 			STR_ORDER_UNLOAD_NO_LOAD,
54 		}, {
55 			STR_ORDER_TRANSFER,
56 			INVALID_STRING_ID,
57 			STR_ORDER_TRANSFER_FULL_LOAD,
58 			STR_ORDER_TRANSFER_FULL_LOAD_ANY,
59 			STR_ORDER_TRANSFER_NO_LOAD,
60 		}, {
61 			/* Unload and transfer do not work together. */
62 			INVALID_STRING_ID,
63 			INVALID_STRING_ID,
64 			INVALID_STRING_ID,
65 			INVALID_STRING_ID,
66 		}, {
67 			STR_ORDER_NO_UNLOAD,
68 			INVALID_STRING_ID,
69 			STR_ORDER_NO_UNLOAD_FULL_LOAD,
70 			STR_ORDER_NO_UNLOAD_FULL_LOAD_ANY,
71 			STR_ORDER_NO_UNLOAD_NO_LOAD,
72 		}
73 	}, {
74 		/* With auto-refitting. No loading and auto-refitting do not work together. */
75 		{
76 			STR_ORDER_AUTO_REFIT,
77 			INVALID_STRING_ID,
78 			STR_ORDER_FULL_LOAD_REFIT,
79 			STR_ORDER_FULL_LOAD_ANY_REFIT,
80 			INVALID_STRING_ID,
81 		}, {
82 			STR_ORDER_UNLOAD_REFIT,
83 			INVALID_STRING_ID,
84 			STR_ORDER_UNLOAD_FULL_LOAD_REFIT,
85 			STR_ORDER_UNLOAD_FULL_LOAD_ANY_REFIT,
86 			INVALID_STRING_ID,
87 		}, {
88 			STR_ORDER_TRANSFER_REFIT,
89 			INVALID_STRING_ID,
90 			STR_ORDER_TRANSFER_FULL_LOAD_REFIT,
91 			STR_ORDER_TRANSFER_FULL_LOAD_ANY_REFIT,
92 			INVALID_STRING_ID,
93 		}, {
94 			/* Unload and transfer do not work together. */
95 			INVALID_STRING_ID,
96 			INVALID_STRING_ID,
97 			INVALID_STRING_ID,
98 			INVALID_STRING_ID,
99 		}, {
100 			STR_ORDER_NO_UNLOAD_REFIT,
101 			INVALID_STRING_ID,
102 			STR_ORDER_NO_UNLOAD_FULL_LOAD_REFIT,
103 			STR_ORDER_NO_UNLOAD_FULL_LOAD_ANY_REFIT,
104 			INVALID_STRING_ID,
105 		}
106 	}
107 };
108 
109 static const StringID _order_non_stop_drowdown[] = {
110 	STR_ORDER_GO_TO,
111 	STR_ORDER_GO_NON_STOP_TO,
112 	STR_ORDER_GO_VIA,
113 	STR_ORDER_GO_NON_STOP_VIA,
114 	INVALID_STRING_ID
115 };
116 
117 static const StringID _order_full_load_drowdown[] = {
118 	STR_ORDER_DROP_LOAD_IF_POSSIBLE,
119 	STR_EMPTY,
120 	STR_ORDER_DROP_FULL_LOAD_ALL,
121 	STR_ORDER_DROP_FULL_LOAD_ANY,
122 	STR_ORDER_DROP_NO_LOADING,
123 	INVALID_STRING_ID
124 };
125 
126 static const StringID _order_unload_drowdown[] = {
127 	STR_ORDER_DROP_UNLOAD_IF_ACCEPTED,
128 	STR_ORDER_DROP_UNLOAD,
129 	STR_ORDER_DROP_TRANSFER,
130 	STR_EMPTY,
131 	STR_ORDER_DROP_NO_UNLOADING,
132 	INVALID_STRING_ID
133 };
134 
135 static const StringID _order_goto_dropdown[] = {
136 	STR_ORDER_GO_TO,
137 	STR_ORDER_GO_TO_NEAREST_DEPOT,
138 	STR_ORDER_CONDITIONAL,
139 	STR_ORDER_SHARE,
140 	INVALID_STRING_ID
141 };
142 
143 static const StringID _order_goto_dropdown_aircraft[] = {
144 	STR_ORDER_GO_TO,
145 	STR_ORDER_GO_TO_NEAREST_HANGAR,
146 	STR_ORDER_CONDITIONAL,
147 	STR_ORDER_SHARE,
148 	INVALID_STRING_ID
149 };
150 
151 /** Variables for conditional orders; this defines the order of appearance in the dropdown box */
152 static const OrderConditionVariable _order_conditional_variable[] = {
153 	OCV_LOAD_PERCENTAGE,
154 	OCV_RELIABILITY,
155 	OCV_MAX_RELIABILITY,
156 	OCV_MAX_SPEED,
157 	OCV_AGE,
158 	OCV_REMAINING_LIFETIME,
159 	OCV_REQUIRES_SERVICE,
160 	OCV_UNCONDITIONALLY,
161 };
162 
163 static const StringID _order_conditional_condition[] = {
164 	STR_ORDER_CONDITIONAL_COMPARATOR_EQUALS,
165 	STR_ORDER_CONDITIONAL_COMPARATOR_NOT_EQUALS,
166 	STR_ORDER_CONDITIONAL_COMPARATOR_LESS_THAN,
167 	STR_ORDER_CONDITIONAL_COMPARATOR_LESS_EQUALS,
168 	STR_ORDER_CONDITIONAL_COMPARATOR_MORE_THAN,
169 	STR_ORDER_CONDITIONAL_COMPARATOR_MORE_EQUALS,
170 	STR_ORDER_CONDITIONAL_COMPARATOR_IS_TRUE,
171 	STR_ORDER_CONDITIONAL_COMPARATOR_IS_FALSE,
172 	INVALID_STRING_ID,
173 };
174 
175 extern uint ConvertSpeedToDisplaySpeed(uint speed);
176 extern uint ConvertDisplaySpeedToSpeed(uint speed);
177 
178 static const StringID _order_depot_action_dropdown[] = {
179 	STR_ORDER_DROP_GO_ALWAYS_DEPOT,
180 	STR_ORDER_DROP_SERVICE_DEPOT,
181 	STR_ORDER_DROP_HALT_DEPOT,
182 	INVALID_STRING_ID
183 };
184 
DepotActionStringIndex(const Order * order)185 static int DepotActionStringIndex(const Order *order)
186 {
187 	if (order->GetDepotActionType() & ODATFB_HALT) {
188 		return DA_STOP;
189 	} else if (order->GetDepotOrderType() & ODTFB_SERVICE) {
190 		return DA_SERVICE;
191 	} else {
192 		return DA_ALWAYS_GO;
193 	}
194 }
195 
196 static const StringID _order_refit_action_dropdown[] = {
197 	STR_ORDER_DROP_REFIT_AUTO,
198 	STR_ORDER_DROP_REFIT_AUTO_ANY,
199 	INVALID_STRING_ID
200 };
201 
202 /**
203  * Draws an order in order or timetable GUI
204  * @param v Vehicle the order belongs to
205  * @param order The order to draw
206  * @param order_index Index of the order in the orders of the vehicle
207  * @param y Y position for drawing
208  * @param selected True, if the order is selected
209  * @param timetable True, when drawing in the timetable GUI
210  * @param left Left border for text drawing
211  * @param middle X position between order index and order text
212  * @param right Right border for text drawing
213  */
DrawOrderString(const Vehicle * v,const Order * order,int order_index,int y,bool selected,bool timetable,int left,int middle,int right)214 void DrawOrderString(const Vehicle *v, const Order *order, int order_index, int y, bool selected, bool timetable, int left, int middle, int right)
215 {
216 	bool rtl = _current_text_dir == TD_RTL;
217 
218 	SpriteID sprite = rtl ? SPR_ARROW_LEFT : SPR_ARROW_RIGHT;
219 	Dimension sprite_size = GetSpriteSize(sprite);
220 	if (v->cur_real_order_index == order_index) {
221 		DrawSprite(sprite, PAL_NONE, rtl ? right -     sprite_size.width : left,                     y + ((int)FONT_HEIGHT_NORMAL - (int)sprite_size.height) / 2);
222 		DrawSprite(sprite, PAL_NONE, rtl ? right - 2 * sprite_size.width : left + sprite_size.width, y + ((int)FONT_HEIGHT_NORMAL - (int)sprite_size.height) / 2);
223 	} else if (v->cur_implicit_order_index == order_index) {
224 		DrawSprite(sprite, PAL_NONE, rtl ? right -     sprite_size.width : left,                     y + ((int)FONT_HEIGHT_NORMAL - (int)sprite_size.height) / 2);
225 	}
226 
227 	TextColour colour = TC_BLACK;
228 	if (order->IsType(OT_IMPLICIT)) {
229 		colour = (selected ? TC_SILVER : TC_GREY) | TC_NO_SHADE;
230 	} else if (selected) {
231 		colour = TC_WHITE;
232 	}
233 
234 	SetDParam(0, order_index + 1);
235 	DrawString(left, rtl ? right - 2 * sprite_size.width - 3 : middle, y, STR_ORDER_INDEX, colour, SA_RIGHT | SA_FORCE);
236 
237 	SetDParam(5, STR_EMPTY);
238 	SetDParam(8, STR_EMPTY);
239 
240 	/* Check range for aircraft. */
241 	if (v->type == VEH_AIRCRAFT && Aircraft::From(v)->GetRange() > 0 && order->IsGotoOrder()) {
242 		const Order *next = order->next != nullptr ? order->next : v->GetFirstOrder();
243 		if (GetOrderDistance(order, next, v) > Aircraft::From(v)->acache.cached_max_range_sqr) SetDParam(8, STR_ORDER_OUT_OF_RANGE);
244 	}
245 
246 	switch (order->GetType()) {
247 		case OT_DUMMY:
248 			SetDParam(0, STR_INVALID_ORDER);
249 			SetDParam(1, order->GetDestination());
250 			break;
251 
252 		case OT_IMPLICIT:
253 			SetDParam(0, STR_ORDER_GO_TO_STATION);
254 			SetDParam(1, STR_ORDER_GO_TO);
255 			SetDParam(2, order->GetDestination());
256 			SetDParam(3, timetable ? STR_EMPTY : STR_ORDER_IMPLICIT);
257 			break;
258 
259 		case OT_GOTO_STATION: {
260 			OrderLoadFlags load = order->GetLoadType();
261 			OrderUnloadFlags unload = order->GetUnloadType();
262 			bool valid_station = CanVehicleUseStation(v, Station::Get(order->GetDestination()));
263 
264 			SetDParam(0, valid_station ? STR_ORDER_GO_TO_STATION : STR_ORDER_GO_TO_STATION_CAN_T_USE_STATION);
265 			SetDParam(1, STR_ORDER_GO_TO + (v->IsGroundVehicle() ? order->GetNonStopType() : 0));
266 			SetDParam(2, order->GetDestination());
267 
268 			if (timetable) {
269 				SetDParam(3, STR_EMPTY);
270 
271 				if (order->GetWaitTime() > 0) {
272 					SetDParam(5, order->IsWaitTimetabled() ? STR_TIMETABLE_STAY_FOR : STR_TIMETABLE_STAY_FOR_ESTIMATED);
273 					SetTimetableParams(6, 7, order->GetWaitTime());
274 				}
275 			} else {
276 				SetDParam(3, (order->GetNonStopType() & ONSF_NO_STOP_AT_DESTINATION_STATION) ? STR_EMPTY : _station_load_types[order->IsRefit()][unload][load]);
277 				if (order->IsRefit()) {
278 					SetDParam(4, order->IsAutoRefit() ? STR_ORDER_AUTO_REFIT_ANY : CargoSpec::Get(order->GetRefitCargo())->name);
279 				}
280 				if (v->type == VEH_TRAIN && (order->GetNonStopType() & ONSF_NO_STOP_AT_DESTINATION_STATION) == 0) {
281 					SetDParam(5, order->GetStopLocation() + STR_ORDER_STOP_LOCATION_NEAR_END);
282 				}
283 			}
284 			break;
285 		}
286 
287 		case OT_GOTO_DEPOT:
288 			if (order->GetDepotActionType() & ODATFB_NEAREST_DEPOT) {
289 				SetDParam(0, STR_ORDER_GO_TO_NEAREST_DEPOT_FORMAT);
290 				if (v->type == VEH_AIRCRAFT) {
291 					SetDParam(2, STR_ORDER_NEAREST_HANGAR);
292 					SetDParam(3, STR_EMPTY);
293 				} else {
294 					SetDParam(2, STR_ORDER_NEAREST_DEPOT);
295 					SetDParam(3, STR_ORDER_TRAIN_DEPOT + v->type);
296 				}
297 			} else {
298 				SetDParam(0, STR_ORDER_GO_TO_DEPOT_FORMAT);
299 				SetDParam(2, v->type);
300 				SetDParam(3, order->GetDestination());
301 			}
302 
303 			if (order->GetDepotOrderType() & ODTFB_SERVICE) {
304 				SetDParam(1, (order->GetNonStopType() & ONSF_NO_STOP_AT_INTERMEDIATE_STATIONS) ? STR_ORDER_SERVICE_NON_STOP_AT : STR_ORDER_SERVICE_AT);
305 			} else {
306 				SetDParam(1, (order->GetNonStopType() & ONSF_NO_STOP_AT_INTERMEDIATE_STATIONS) ? STR_ORDER_GO_NON_STOP_TO : STR_ORDER_GO_TO);
307 			}
308 
309 			if (!timetable && (order->GetDepotActionType() & ODATFB_HALT)) {
310 				SetDParam(5, STR_ORDER_STOP_ORDER);
311 			}
312 
313 			if (!timetable && order->IsRefit()) {
314 				SetDParam(5, (order->GetDepotActionType() & ODATFB_HALT) ? STR_ORDER_REFIT_STOP_ORDER : STR_ORDER_REFIT_ORDER);
315 				SetDParam(6, CargoSpec::Get(order->GetRefitCargo())->name);
316 			}
317 			break;
318 
319 		case OT_GOTO_WAYPOINT:
320 			SetDParam(0, (order->GetNonStopType() & ONSF_NO_STOP_AT_INTERMEDIATE_STATIONS) ? STR_ORDER_GO_NON_STOP_TO_WAYPOINT : STR_ORDER_GO_TO_WAYPOINT);
321 			SetDParam(1, order->GetDestination());
322 			break;
323 
324 		case OT_CONDITIONAL:
325 			SetDParam(1, order->GetConditionSkipToOrder() + 1);
326 			if (order->GetConditionVariable() == OCV_UNCONDITIONALLY) {
327 				SetDParam(0, STR_ORDER_CONDITIONAL_UNCONDITIONAL);
328 			} else {
329 				OrderConditionComparator occ = order->GetConditionComparator();
330 				SetDParam(0, (occ == OCC_IS_TRUE || occ == OCC_IS_FALSE) ? STR_ORDER_CONDITIONAL_TRUE_FALSE : STR_ORDER_CONDITIONAL_NUM);
331 				SetDParam(2, STR_ORDER_CONDITIONAL_LOAD_PERCENTAGE + order->GetConditionVariable());
332 				SetDParam(3, STR_ORDER_CONDITIONAL_COMPARATOR_EQUALS + occ);
333 
334 				uint value = order->GetConditionValue();
335 				if (order->GetConditionVariable() == OCV_MAX_SPEED) value = ConvertSpeedToDisplaySpeed(value);
336 				SetDParam(4, value);
337 			}
338 
339 			if (timetable && order->GetWaitTime() > 0) {
340 				SetDParam(5, order->IsWaitTimetabled() ? STR_TIMETABLE_AND_TRAVEL_FOR : STR_TIMETABLE_AND_TRAVEL_FOR_ESTIMATED);
341 				SetTimetableParams(6, 7, order->GetWaitTime());
342 			} else {
343 				SetDParam(5, STR_EMPTY);
344 			}
345 			break;
346 
347 		default: NOT_REACHED();
348 	}
349 
350 	DrawString(rtl ? left : middle, rtl ? middle : right, y, STR_ORDER_TEXT, colour);
351 }
352 
353 /**
354  * Get the order command a vehicle can do in a given tile.
355  * @param v Vehicle involved.
356  * @param tile Tile being queried.
357  * @return The order associated to vehicle v in given tile (or empty order if vehicle can do nothing in the tile).
358  */
GetOrderCmdFromTile(const Vehicle * v,TileIndex tile)359 static Order GetOrderCmdFromTile(const Vehicle *v, TileIndex tile)
360 {
361 	/* Hack-ish; unpack order 0, so everything gets initialised with either zero
362 	 * or a suitable default value for the variable. Then also override the index
363 	 * as it is not coming from a pool, so would be initialised. */
364 	Order order(0);
365 	order.index = 0;
366 
367 	/* check depot first */
368 	if (IsDepotTypeTile(tile, (TransportType)(uint)v->type) && IsTileOwner(tile, _local_company)) {
369 		order.MakeGoToDepot(v->type == VEH_AIRCRAFT ? GetStationIndex(tile) : GetDepotIndex(tile),
370 				ODTFB_PART_OF_ORDERS,
371 				(_settings_client.gui.new_nonstop && v->IsGroundVehicle()) ? ONSF_NO_STOP_AT_INTERMEDIATE_STATIONS : ONSF_STOP_EVERYWHERE);
372 
373 		if (_ctrl_pressed) order.SetDepotOrderType((OrderDepotTypeFlags)(order.GetDepotOrderType() ^ ODTFB_SERVICE));
374 
375 		return order;
376 	}
377 
378 	/* check rail waypoint */
379 	if (IsRailWaypointTile(tile) &&
380 			v->type == VEH_TRAIN &&
381 			IsTileOwner(tile, _local_company)) {
382 		order.MakeGoToWaypoint(GetStationIndex(tile));
383 		if (_settings_client.gui.new_nonstop != _ctrl_pressed) order.SetNonStopType(ONSF_NO_STOP_AT_ANY_STATION);
384 		return order;
385 	}
386 
387 	/* check buoy (no ownership) */
388 	if (IsBuoyTile(tile) && v->type == VEH_SHIP) {
389 		order.MakeGoToWaypoint(GetStationIndex(tile));
390 		return order;
391 	}
392 
393 	/* check for station or industry with neutral station */
394 	if (IsTileType(tile, MP_STATION) || IsTileType(tile, MP_INDUSTRY)) {
395 		const Station *st = nullptr;
396 
397 		if (IsTileType(tile, MP_STATION)) {
398 			st = Station::GetByTile(tile);
399 		} else {
400 			const Industry *in = Industry::GetByTile(tile);
401 			st = in->neutral_station;
402 		}
403 		if (st != nullptr && (st->owner == _local_company || st->owner == OWNER_NONE)) {
404 			byte facil;
405 			switch (v->type) {
406 				case VEH_SHIP:     facil = FACIL_DOCK;    break;
407 				case VEH_TRAIN:    facil = FACIL_TRAIN;   break;
408 				case VEH_AIRCRAFT: facil = FACIL_AIRPORT; break;
409 				case VEH_ROAD:     facil = FACIL_BUS_STOP | FACIL_TRUCK_STOP; break;
410 				default: NOT_REACHED();
411 			}
412 			if (st->facilities & facil) {
413 				order.MakeGoToStation(st->index);
414 				if (_ctrl_pressed) order.SetLoadType(OLF_FULL_LOAD_ANY);
415 				if (_settings_client.gui.new_nonstop && v->IsGroundVehicle()) order.SetNonStopType(ONSF_NO_STOP_AT_INTERMEDIATE_STATIONS);
416 				order.SetStopLocation(v->type == VEH_TRAIN ? (OrderStopLocation)(_settings_client.gui.stop_location) : OSL_PLATFORM_FAR_END);
417 				return order;
418 			}
419 		}
420 	}
421 
422 	/* not found */
423 	order.Free();
424 	return order;
425 }
426 
427 /** Hotkeys for order window. */
428 enum {
429 	OHK_SKIP,
430 	OHK_DELETE,
431 	OHK_GOTO,
432 	OHK_NONSTOP,
433 	OHK_FULLLOAD,
434 	OHK_UNLOAD,
435 	OHK_NEAREST_DEPOT,
436 	OHK_ALWAYS_SERVICE,
437 	OHK_TRANSFER,
438 	OHK_NO_UNLOAD,
439 	OHK_NO_LOAD,
440 };
441 
442 /**
443  * %Order window code for all vehicles.
444  *
445  * At the bottom of the window two button rows are located for changing the orders of the vehicle.
446  *
447  * \section top-row Top row
448  * The top-row is for manipulating an individual order. What row is displayed depends on the type of vehicle, and whether or not you are the owner of the vehicle.
449  *
450  * The top-row buttons of one of your trains or road vehicles is one of the following three cases:
451  * \verbatim
452  * +-----------------+-----------------+-----------------+-----------------+
453  * |    NON-STOP     |    FULL_LOAD    |     UNLOAD      |      REFIT      | (normal)
454  * +-----------------+-----+-----------+-----------+-----+-----------------+
455  * |       COND_VAR        |    COND_COMPARATOR    |      COND_VALUE       | (for conditional orders)
456  * +-----------------+-----+-----------+-----------+-----+-----------------+
457  * |    NON-STOP     |      REFIT      |     SERVICE     |     (empty)     | (for depot orders)
458  * +-----------------+-----------------+-----------------+-----------------+
459  * \endverbatim
460  *
461  * Airplanes and ships have one of the following three top-row button rows:
462  * \verbatim
463  * +-----------------+-----------------+-----------------+
464  * |    FULL_LOAD    |     UNLOAD      |      REFIT      | (normal)
465  * +-----------------+-----------------+-----------------+
466  * |    COND_VAR     | COND_COMPARATOR |   COND_VALUE    | (for conditional orders)
467  * +-----------------+--------+--------+-----------------+
468  * |            REFIT         |          SERVICE         | (for depot order)
469  * +--------------------------+--------------------------+
470  * \endverbatim
471  *
472  * \section bottom-row Bottom row
473  * The second row (the bottom row) is for manipulating the list of orders:
474  * \verbatim
475  * +-----------------+-----------------+-----------------+
476  * |      SKIP       |     DELETE      |      GOTO       |
477  * +-----------------+-----------------+-----------------+
478  * \endverbatim
479  *
480  * For vehicles of other companies, both button rows are not displayed.
481  */
482 struct OrdersWindow : public Window {
483 private:
484 	/** Under what reason are we using the PlaceObject functionality? */
485 	enum OrderPlaceObjectState {
486 		OPOS_NONE,
487 		OPOS_GOTO,
488 		OPOS_CONDITIONAL,
489 		OPOS_SHARE,
490 		OPOS_END,
491 	};
492 
493 	/** Displayed planes of the #NWID_SELECTION widgets. */
494 	enum DisplayPane {
495 		/* WID_O_SEL_TOP_ROW_GROUNDVEHICLE */
496 		DP_GROUNDVEHICLE_ROW_NORMAL      = 0, ///< Display the row for normal/depot orders in the top row of the train/rv order window.
497 		DP_GROUNDVEHICLE_ROW_CONDITIONAL = 1, ///< Display the row for conditional orders in the top row of the train/rv order window.
498 
499 		/* WID_O_SEL_TOP_LEFT */
500 		DP_LEFT_LOAD       = 0, ///< Display 'load' in the left button of the top row of the train/rv order window.
501 		DP_LEFT_REFIT      = 1, ///< Display 'refit' in the left button of the top row of the train/rv order window.
502 
503 		/* WID_O_SEL_TOP_MIDDLE */
504 		DP_MIDDLE_UNLOAD   = 0, ///< Display 'unload' in the middle button of the top row of the train/rv order window.
505 		DP_MIDDLE_SERVICE  = 1, ///< Display 'service' in the middle button of the top row of the train/rv order window.
506 
507 		/* WID_O_SEL_TOP_RIGHT */
508 		DP_RIGHT_EMPTY     = 0, ///< Display an empty panel in the right button of the top row of the train/rv order window.
509 		DP_RIGHT_REFIT     = 1, ///< Display 'refit' in the right button of the top  row of the train/rv order window.
510 
511 		/* WID_O_SEL_TOP_ROW */
512 		DP_ROW_LOAD        = 0, ///< Display 'load' / 'unload' / 'refit' buttons in the top row of the ship/airplane order window.
513 		DP_ROW_DEPOT       = 1, ///< Display 'refit' / 'service' buttons in the top row of the ship/airplane order window.
514 		DP_ROW_CONDITIONAL = 2, ///< Display the conditional order buttons in the top row of the ship/airplane order window.
515 
516 		/* WID_O_SEL_BOTTOM_MIDDLE */
517 		DP_BOTTOM_MIDDLE_DELETE       = 0, ///< Display 'delete' in the middle button of the bottom row of the vehicle order window.
518 		DP_BOTTOM_MIDDLE_STOP_SHARING = 1, ///< Display 'stop sharing' in the middle button of the bottom row of the vehicle order window.
519 	};
520 
521 	int selected_order;
522 	VehicleOrderID order_over;         ///< Order over which another order is dragged, \c INVALID_VEH_ORDER_ID if none.
523 	OrderPlaceObjectState goto_type;
524 	const Vehicle *vehicle; ///< Vehicle owning the orders being displayed and manipulated.
525 	Scrollbar *vscroll;
526 	bool can_do_refit;     ///< Vehicle chain can be refitted in depot.
527 	bool can_do_autorefit; ///< Vehicle chain can be auto-refitted.
528 
529 	/**
530 	 * Return the memorised selected order.
531 	 * @return the memorised order if it is a valid one
532 	 *  else return the number of orders
533 	 */
OrderGetSelOrdersWindow534 	VehicleOrderID OrderGetSel() const
535 	{
536 		int num = this->selected_order;
537 		return (num >= 0 && num < vehicle->GetNumOrders()) ? num : vehicle->GetNumOrders();
538 	}
539 
540 	/**
541 	 * Calculate the selected order.
542 	 * The calculation is based on the relative (to the window) y click position and
543 	 *  the position of the scrollbar.
544 	 *
545 	 * @param y Y-value of the click relative to the window origin
546 	 * @return The selected order if the order is valid, else return \c INVALID_VEH_ORDER_ID.
547 	 */
GetOrderFromPtOrdersWindow548 	VehicleOrderID GetOrderFromPt(int y)
549 	{
550 		NWidgetBase *nwid = this->GetWidget<NWidgetBase>(WID_O_ORDER_LIST);
551 		uint sel = (y - nwid->pos_y - WD_FRAMERECT_TOP) / nwid->resize_y; // Selected line in the WID_O_ORDER_LIST panel.
552 
553 		if (sel >= this->vscroll->GetCapacity()) return INVALID_VEH_ORDER_ID;
554 
555 		sel += this->vscroll->GetPosition();
556 
557 		return (sel <= vehicle->GetNumOrders()) ? sel : INVALID_VEH_ORDER_ID;
558 	}
559 
560 	/**
561 	 * Handle the click on the goto button.
562 	 */
OrderClick_GotoOrdersWindow563 	void OrderClick_Goto(OrderPlaceObjectState type)
564 	{
565 		assert(type > OPOS_NONE && type < OPOS_END);
566 
567 		static const HighLightStyle goto_place_style[OPOS_END - 1] = {
568 			HT_RECT | HT_VEHICLE, // OPOS_GOTO
569 			HT_NONE,              // OPOS_CONDITIONAL
570 			HT_VEHICLE,           // OPOS_SHARE
571 		};
572 		SetObjectToPlaceWnd(ANIMCURSOR_PICKSTATION, PAL_NONE, goto_place_style[type - 1], this);
573 		this->goto_type = type;
574 		this->SetWidgetDirty(WID_O_GOTO);
575 	}
576 
577 	/**
578 	 * Handle the click on the full load button.
579 	 * @param load_type Load flag to apply. If matches existing load type, toggles to default of 'load if possible'.
580 	 * @param toggle If we toggle or not (used for hotkey behavior)
581 	 */
OrderClick_FullLoadOrdersWindow582 	void OrderClick_FullLoad(OrderLoadFlags load_type, bool toggle = false)
583 	{
584 		VehicleOrderID sel_ord = this->OrderGetSel();
585 		const Order *order = this->vehicle->GetOrder(sel_ord);
586 
587 		if (order == nullptr) return;
588 
589 		if (toggle && order->GetLoadType() == load_type) {
590 			load_type = OLF_LOAD_IF_POSSIBLE; // reset to 'default'
591 		}
592 		if (order->GetLoadType() == load_type) return; // If we still match, do nothing
593 
594 		DoCommandP(this->vehicle->tile, this->vehicle->index + (sel_ord << 20), MOF_LOAD | (load_type << 4), CMD_MODIFY_ORDER | CMD_MSG(STR_ERROR_CAN_T_MODIFY_THIS_ORDER));
595 	}
596 
597 	/**
598 	 * Handle the click on the service.
599 	 */
OrderClick_ServiceOrdersWindow600 	void OrderClick_Service(int i)
601 	{
602 		VehicleOrderID sel_ord = this->OrderGetSel();
603 
604 		if (i < 0) {
605 			const Order *order = this->vehicle->GetOrder(sel_ord);
606 			if (order == nullptr) return;
607 			i = (order->GetDepotOrderType() & ODTFB_SERVICE) ? DA_ALWAYS_GO : DA_SERVICE;
608 		}
609 		DoCommandP(this->vehicle->tile, this->vehicle->index + (sel_ord << 20), MOF_DEPOT_ACTION | (i << 4), CMD_MODIFY_ORDER | CMD_MSG(STR_ERROR_CAN_T_MODIFY_THIS_ORDER));
610 	}
611 
612 	/**
613 	 * Handle the click on the service in nearest depot button.
614 	 */
OrderClick_NearestDepotOrdersWindow615 	void OrderClick_NearestDepot()
616 	{
617 		Order order;
618 		order.next = nullptr;
619 		order.index = 0;
620 		order.MakeGoToDepot(0, ODTFB_PART_OF_ORDERS,
621 				_settings_client.gui.new_nonstop && this->vehicle->IsGroundVehicle() ? ONSF_NO_STOP_AT_INTERMEDIATE_STATIONS : ONSF_STOP_EVERYWHERE);
622 		order.SetDepotActionType(ODATFB_NEAREST_DEPOT);
623 
624 		DoCommandP(this->vehicle->tile, this->vehicle->index + (this->OrderGetSel() << 20), order.Pack(), CMD_INSERT_ORDER | CMD_MSG(STR_ERROR_CAN_T_INSERT_NEW_ORDER));
625 	}
626 
627 	/**
628 	 * Handle the click on the unload button.
629 	 * @param unload_type Unload flag to apply. If matches existing unload type, toggles to default of 'unload if possible'.
630 	 * @param toggle If we toggle or not (used for hotkey behavior)
631 	 */
OrderClick_UnloadOrdersWindow632 	void OrderClick_Unload(OrderUnloadFlags unload_type, bool toggle = false)
633 	{
634 		VehicleOrderID sel_ord = this->OrderGetSel();
635 		const Order *order = this->vehicle->GetOrder(sel_ord);
636 
637 		if (order == nullptr) return;
638 
639 		if (toggle && order->GetUnloadType() == unload_type) {
640 			unload_type = OUF_UNLOAD_IF_POSSIBLE;
641 		}
642 		if (order->GetUnloadType() == unload_type) return; // If we still match, do nothing
643 
644 		DoCommandP(this->vehicle->tile, this->vehicle->index + (sel_ord << 20), MOF_UNLOAD | (unload_type << 4), CMD_MODIFY_ORDER | CMD_MSG(STR_ERROR_CAN_T_MODIFY_THIS_ORDER));
645 
646 		/* Transfer and unload orders with leave empty as default */
647 		if (unload_type == OUFB_TRANSFER || unload_type == OUFB_UNLOAD) {
648 			DoCommandP(this->vehicle->tile, this->vehicle->index + (sel_ord << 20), MOF_LOAD | (OLFB_NO_LOAD << 4), CMD_MODIFY_ORDER);
649 			this->SetWidgetDirty(WID_O_FULL_LOAD);
650 		}
651 	}
652 
653 	/**
654 	 * Handle the click on the nonstop button.
655 	 * @param non_stop what non-stop type to use; -1 to use the 'next' one.
656 	 */
OrderClick_NonstopOrdersWindow657 	void OrderClick_Nonstop(int non_stop)
658 	{
659 		if (!this->vehicle->IsGroundVehicle()) return;
660 
661 		VehicleOrderID sel_ord = this->OrderGetSel();
662 		const Order *order = this->vehicle->GetOrder(sel_ord);
663 
664 		if (order == nullptr || order->GetNonStopType() == non_stop) return;
665 
666 		/* Keypress if negative, so 'toggle' to the next */
667 		if (non_stop < 0) {
668 			non_stop = order->GetNonStopType() ^ ONSF_NO_STOP_AT_INTERMEDIATE_STATIONS;
669 		}
670 
671 		this->SetWidgetDirty(WID_O_NON_STOP);
672 		DoCommandP(this->vehicle->tile, this->vehicle->index + (sel_ord << 20), MOF_NON_STOP | non_stop << 4,  CMD_MODIFY_ORDER | CMD_MSG(STR_ERROR_CAN_T_MODIFY_THIS_ORDER));
673 	}
674 
675 	/**
676 	 * Handle the click on the skip button.
677 	 * If ctrl is pressed, skip to selected order, else skip to current order + 1
678 	 */
OrderClick_SkipOrdersWindow679 	void OrderClick_Skip()
680 	{
681 		/* Don't skip when there's nothing to skip */
682 		if (_ctrl_pressed && this->vehicle->cur_implicit_order_index == this->OrderGetSel()) return;
683 		if (this->vehicle->GetNumOrders() <= 1) return;
684 
685 		DoCommandP(this->vehicle->tile, this->vehicle->index, _ctrl_pressed ? this->OrderGetSel() : ((this->vehicle->cur_implicit_order_index + 1) % this->vehicle->GetNumOrders()),
686 				CMD_SKIP_TO_ORDER | CMD_MSG(_ctrl_pressed ? STR_ERROR_CAN_T_SKIP_TO_ORDER : STR_ERROR_CAN_T_SKIP_ORDER));
687 	}
688 
689 	/**
690 	 * Handle the click on the delete button.
691 	 */
OrderClick_DeleteOrdersWindow692 	void OrderClick_Delete()
693 	{
694 		/* When networking, move one order lower */
695 		int selected = this->selected_order + (int)_networking;
696 
697 		if (DoCommandP(this->vehicle->tile, this->vehicle->index, this->OrderGetSel(), CMD_DELETE_ORDER | CMD_MSG(STR_ERROR_CAN_T_DELETE_THIS_ORDER))) {
698 			this->selected_order = selected >= this->vehicle->GetNumOrders() ? -1 : selected;
699 			this->UpdateButtonState();
700 		}
701 	}
702 
703 	/**
704 	 * Handle the click on the 'stop sharing' button.
705 	 * If 'End of Shared Orders' isn't selected, do nothing. If Ctrl is pressed, call OrderClick_Delete and exit.
706 	 * To stop sharing this vehicle order list, we copy the orders of a vehicle that share this order list. That way we
707 	 * exit the group of shared vehicles while keeping the same order list.
708 	 */
OrderClick_StopSharingOrdersWindow709 	void OrderClick_StopSharing()
710 	{
711 		/* Don't try to stop sharing orders if 'End of Shared Orders' isn't selected. */
712 		if (!this->vehicle->IsOrderListShared() || this->selected_order != this->vehicle->GetNumOrders()) return;
713 		/* If Ctrl is pressed, delete the order list as if we clicked the 'Delete' button. */
714 		if (_ctrl_pressed) {
715 			this->OrderClick_Delete();
716 			return;
717 		}
718 
719 		/* Get another vehicle that share orders with this vehicle. */
720 		Vehicle *other_shared = (this->vehicle->FirstShared() == this->vehicle) ? this->vehicle->NextShared() : this->vehicle->PreviousShared();
721 		/* Copy the order list of the other vehicle. */
722 		if (DoCommandP(this->vehicle->tile, this->vehicle->index | CO_COPY << 30, other_shared->index, CMD_CLONE_ORDER | CMD_MSG(STR_ERROR_CAN_T_STOP_SHARING_ORDER_LIST))) {
723 			this->UpdateButtonState();
724 		}
725 	}
726 
727 	/**
728 	 * Handle the click on the refit button.
729 	 * If ctrl is pressed, cancel refitting, else show the refit window.
730 	 * @param i Selected refit command.
731 	 * @param auto_refit Select refit for auto-refitting.
732 	 */
OrderClick_RefitOrdersWindow733 	void OrderClick_Refit(int i, bool auto_refit)
734 	{
735 		if (_ctrl_pressed) {
736 			/* Cancel refitting */
737 			DoCommandP(this->vehicle->tile, this->vehicle->index, (this->OrderGetSel() << 16) | (CT_NO_REFIT << 8) | CT_NO_REFIT, CMD_ORDER_REFIT);
738 		} else {
739 			if (i == 1) { // Auto-refit to available cargo type.
740 				DoCommandP(this->vehicle->tile, this->vehicle->index, (this->OrderGetSel() << 16) | CT_AUTO_REFIT, CMD_ORDER_REFIT);
741 			} else {
742 				ShowVehicleRefitWindow(this->vehicle, this->OrderGetSel(), this, auto_refit);
743 			}
744 		}
745 	}
746 
747 	/** Cache auto-refittability of the vehicle chain. */
UpdateAutoRefitStateOrdersWindow748 	void UpdateAutoRefitState()
749 	{
750 		this->can_do_refit = false;
751 		this->can_do_autorefit = false;
752 		for (const Vehicle *w = this->vehicle; w != nullptr; w = w->IsGroundVehicle() ? w->Next() : nullptr) {
753 			if (IsEngineRefittable(w->engine_type)) this->can_do_refit = true;
754 			if (HasBit(Engine::Get(w->engine_type)->info.misc_flags, EF_AUTO_REFIT)) this->can_do_autorefit = true;
755 		}
756 	}
757 
758 public:
OrdersWindowOrdersWindow759 	OrdersWindow(WindowDesc *desc, const Vehicle *v) : Window(desc)
760 	{
761 		this->vehicle = v;
762 
763 		this->CreateNestedTree();
764 		this->vscroll = this->GetScrollbar(WID_O_SCROLLBAR);
765 		this->FinishInitNested(v->index);
766 		if (v->owner == _local_company) {
767 			this->DisableWidget(WID_O_EMPTY);
768 		}
769 
770 		this->selected_order = -1;
771 		this->order_over = INVALID_VEH_ORDER_ID;
772 		this->goto_type = OPOS_NONE;
773 		this->owner = v->owner;
774 
775 		this->UpdateAutoRefitState();
776 
777 		if (_settings_client.gui.quick_goto && v->owner == _local_company) {
778 			/* If there are less than 2 station, make Go To active. */
779 			int station_orders = 0;
780 			for(const Order *order : v->Orders()) {
781 				if (order->IsType(OT_GOTO_STATION)) station_orders++;
782 			}
783 
784 			if (station_orders < 2) this->OrderClick_Goto(OPOS_GOTO);
785 		}
786 		this->OnInvalidateData(VIWD_MODIFY_ORDERS);
787 	}
788 
UpdateWidgetSizeOrdersWindow789 	void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
790 	{
791 		switch (widget) {
792 			case WID_O_ORDER_LIST:
793 				resize->height = FONT_HEIGHT_NORMAL;
794 				size->height = 6 * resize->height + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
795 				break;
796 
797 			case WID_O_COND_VARIABLE: {
798 				Dimension d = {0, 0};
799 				for (uint i = 0; i < lengthof(_order_conditional_variable); i++) {
800 					d = maxdim(d, GetStringBoundingBox(STR_ORDER_CONDITIONAL_LOAD_PERCENTAGE + _order_conditional_variable[i]));
801 				}
802 				d.width += padding.width;
803 				d.height += padding.height;
804 				*size = maxdim(*size, d);
805 				break;
806 			}
807 
808 			case WID_O_COND_COMPARATOR: {
809 				Dimension d = {0, 0};
810 				for (int i = 0; _order_conditional_condition[i] != INVALID_STRING_ID; i++) {
811 					d = maxdim(d, GetStringBoundingBox(_order_conditional_condition[i]));
812 				}
813 				d.width += padding.width;
814 				d.height += padding.height;
815 				*size = maxdim(*size, d);
816 				break;
817 			}
818 		}
819 	}
820 
821 	/**
822 	 * Some data on this window has become invalid.
823 	 * @param data Information about the changed data.
824 	 * @param gui_scope Whether the call is done from GUI scope. You may not do everything when not in GUI scope. See #InvalidateWindowData() for details.
825 	 */
OnInvalidateDataOrdersWindow826 	void OnInvalidateData(int data = 0, bool gui_scope = true) override
827 	{
828 		VehicleOrderID from = INVALID_VEH_ORDER_ID;
829 		VehicleOrderID to   = INVALID_VEH_ORDER_ID;
830 
831 		switch (data) {
832 			case VIWD_AUTOREPLACE:
833 				/* Autoreplace replaced the vehicle */
834 				this->vehicle = Vehicle::Get(this->window_number);
835 				FALLTHROUGH;
836 
837 			case VIWD_CONSIST_CHANGED:
838 				/* Vehicle composition was changed. */
839 				this->UpdateAutoRefitState();
840 				break;
841 
842 			case VIWD_REMOVE_ALL_ORDERS:
843 				/* Removed / replaced all orders (after deleting / sharing) */
844 				if (this->selected_order == -1) break;
845 
846 				this->CloseChildWindows();
847 				HideDropDownMenu(this);
848 				this->selected_order = -1;
849 				break;
850 
851 			case VIWD_MODIFY_ORDERS:
852 				/* Some other order changes */
853 				break;
854 
855 			default:
856 				if (data < 0) break;
857 
858 				if (gui_scope) break; // only do this once; from command scope
859 				from = GB(data, 0, 8);
860 				to   = GB(data, 8, 8);
861 				/* Moving an order. If one of these is INVALID_VEH_ORDER_ID, then
862 				 * the order is being created / removed */
863 				if (this->selected_order == -1) break;
864 
865 				if (from == to) break; // no need to change anything
866 
867 				if (from != this->selected_order) {
868 					/* Moving from preceding order? */
869 					this->selected_order -= (int)(from <= this->selected_order);
870 					/* Moving to   preceding order? */
871 					this->selected_order += (int)(to   <= this->selected_order);
872 					break;
873 				}
874 
875 				/* Now we are modifying the selected order */
876 				if (to == INVALID_VEH_ORDER_ID) {
877 					/* Deleting selected order */
878 					this->CloseChildWindows();
879 					HideDropDownMenu(this);
880 					this->selected_order = -1;
881 					break;
882 				}
883 
884 				/* Moving selected order */
885 				this->selected_order = to;
886 				break;
887 		}
888 
889 		this->vscroll->SetCount(this->vehicle->GetNumOrders() + 1);
890 		if (gui_scope) this->UpdateButtonState();
891 
892 		/* Scroll to the new order. */
893 		if (from == INVALID_VEH_ORDER_ID && to != INVALID_VEH_ORDER_ID && !this->vscroll->IsVisible(to)) {
894 			this->vscroll->ScrollTowards(to);
895 		}
896 	}
897 
UpdateButtonStateOrdersWindow898 	void UpdateButtonState()
899 	{
900 		if (this->vehicle->owner != _local_company) return; // No buttons are displayed with competitor order windows.
901 
902 		bool shared_orders = this->vehicle->IsOrderListShared();
903 		VehicleOrderID sel = this->OrderGetSel();
904 		const Order *order = this->vehicle->GetOrder(sel);
905 
906 		/* Second row. */
907 		/* skip */
908 		this->SetWidgetDisabledState(WID_O_SKIP, this->vehicle->GetNumOrders() <= 1);
909 
910 		/* delete / stop sharing */
911 		NWidgetStacked *delete_sel = this->GetWidget<NWidgetStacked>(WID_O_SEL_BOTTOM_MIDDLE);
912 		if (shared_orders && this->selected_order == this->vehicle->GetNumOrders()) {
913 			/* The 'End of Shared Orders' order is selected, show the 'stop sharing' button. */
914 			delete_sel->SetDisplayedPlane(DP_BOTTOM_MIDDLE_STOP_SHARING);
915 		} else {
916 			/* The 'End of Shared Orders' order isn't selected, show the 'delete' button. */
917 			delete_sel->SetDisplayedPlane(DP_BOTTOM_MIDDLE_DELETE);
918 			this->SetWidgetDisabledState(WID_O_DELETE,
919 				(uint)this->vehicle->GetNumOrders() + ((shared_orders || this->vehicle->GetNumOrders() != 0) ? 1 : 0) <= (uint)this->selected_order);
920 
921 			/* Set the tooltip of the 'delete' button depending on whether the
922 			 * 'End of Orders' order or a regular order is selected. */
923 			NWidgetCore *nwi = this->GetWidget<NWidgetCore>(WID_O_DELETE);
924 			if (this->selected_order == this->vehicle->GetNumOrders()) {
925 				nwi->SetDataTip(STR_ORDERS_DELETE_BUTTON, STR_ORDERS_DELETE_ALL_TOOLTIP);
926 			} else {
927 				nwi->SetDataTip(STR_ORDERS_DELETE_BUTTON, STR_ORDERS_DELETE_TOOLTIP);
928 			}
929 		}
930 
931 		/* First row. */
932 		this->RaiseWidget(WID_O_FULL_LOAD);
933 		this->RaiseWidget(WID_O_UNLOAD);
934 		this->RaiseWidget(WID_O_SERVICE);
935 
936 		/* Selection widgets. */
937 		/* Train or road vehicle. */
938 		NWidgetStacked *train_row_sel = this->GetWidget<NWidgetStacked>(WID_O_SEL_TOP_ROW_GROUNDVEHICLE);
939 		NWidgetStacked *left_sel      = this->GetWidget<NWidgetStacked>(WID_O_SEL_TOP_LEFT);
940 		NWidgetStacked *middle_sel    = this->GetWidget<NWidgetStacked>(WID_O_SEL_TOP_MIDDLE);
941 		NWidgetStacked *right_sel     = this->GetWidget<NWidgetStacked>(WID_O_SEL_TOP_RIGHT);
942 		/* Ship or airplane. */
943 		NWidgetStacked *row_sel = this->GetWidget<NWidgetStacked>(WID_O_SEL_TOP_ROW);
944 		assert(row_sel != nullptr || (train_row_sel != nullptr && left_sel != nullptr && middle_sel != nullptr && right_sel != nullptr));
945 
946 
947 		if (order == nullptr) {
948 			if (row_sel != nullptr) {
949 				row_sel->SetDisplayedPlane(DP_ROW_LOAD);
950 			} else {
951 				train_row_sel->SetDisplayedPlane(DP_GROUNDVEHICLE_ROW_NORMAL);
952 				left_sel->SetDisplayedPlane(DP_LEFT_LOAD);
953 				middle_sel->SetDisplayedPlane(DP_MIDDLE_UNLOAD);
954 				right_sel->SetDisplayedPlane(DP_RIGHT_EMPTY);
955 				this->DisableWidget(WID_O_NON_STOP);
956 				this->RaiseWidget(WID_O_NON_STOP);
957 			}
958 			this->DisableWidget(WID_O_FULL_LOAD);
959 			this->DisableWidget(WID_O_UNLOAD);
960 			this->DisableWidget(WID_O_REFIT_DROPDOWN);
961 		} else {
962 			this->SetWidgetDisabledState(WID_O_FULL_LOAD, (order->GetNonStopType() & ONSF_NO_STOP_AT_DESTINATION_STATION) != 0); // full load
963 			this->SetWidgetDisabledState(WID_O_UNLOAD,    (order->GetNonStopType() & ONSF_NO_STOP_AT_DESTINATION_STATION) != 0); // unload
964 
965 			switch (order->GetType()) {
966 				case OT_GOTO_STATION:
967 					if (row_sel != nullptr) {
968 						row_sel->SetDisplayedPlane(DP_ROW_LOAD);
969 					} else {
970 						train_row_sel->SetDisplayedPlane(DP_GROUNDVEHICLE_ROW_NORMAL);
971 						left_sel->SetDisplayedPlane(DP_LEFT_LOAD);
972 						middle_sel->SetDisplayedPlane(DP_MIDDLE_UNLOAD);
973 						right_sel->SetDisplayedPlane(DP_RIGHT_REFIT);
974 						this->EnableWidget(WID_O_NON_STOP);
975 						this->SetWidgetLoweredState(WID_O_NON_STOP, order->GetNonStopType() & ONSF_NO_STOP_AT_INTERMEDIATE_STATIONS);
976 					}
977 					this->SetWidgetLoweredState(WID_O_FULL_LOAD, order->GetLoadType() == OLF_FULL_LOAD_ANY);
978 					this->SetWidgetLoweredState(WID_O_UNLOAD, order->GetUnloadType() == OUFB_UNLOAD);
979 
980 					/* Can only do refitting when stopping at the destination and loading cargo.
981 					 * Also enable the button if a refit is already set to allow clearing it. */
982 					this->SetWidgetDisabledState(WID_O_REFIT_DROPDOWN,
983 							order->GetLoadType() == OLFB_NO_LOAD || (order->GetNonStopType() & ONSF_NO_STOP_AT_DESTINATION_STATION) ||
984 							((!this->can_do_refit || !this->can_do_autorefit) && !order->IsRefit()));
985 
986 					break;
987 
988 				case OT_GOTO_WAYPOINT:
989 					if (row_sel != nullptr) {
990 						row_sel->SetDisplayedPlane(DP_ROW_LOAD);
991 					} else {
992 						train_row_sel->SetDisplayedPlane(DP_GROUNDVEHICLE_ROW_NORMAL);
993 						left_sel->SetDisplayedPlane(DP_LEFT_LOAD);
994 						middle_sel->SetDisplayedPlane(DP_MIDDLE_UNLOAD);
995 						right_sel->SetDisplayedPlane(DP_RIGHT_EMPTY);
996 						this->EnableWidget(WID_O_NON_STOP);
997 						this->SetWidgetLoweredState(WID_O_NON_STOP, order->GetNonStopType() & ONSF_NO_STOP_AT_INTERMEDIATE_STATIONS);
998 					}
999 					this->DisableWidget(WID_O_FULL_LOAD);
1000 					this->DisableWidget(WID_O_UNLOAD);
1001 					this->DisableWidget(WID_O_REFIT_DROPDOWN);
1002 					break;
1003 
1004 				case OT_GOTO_DEPOT:
1005 					if (row_sel != nullptr) {
1006 						row_sel->SetDisplayedPlane(DP_ROW_DEPOT);
1007 					} else {
1008 						train_row_sel->SetDisplayedPlane(DP_GROUNDVEHICLE_ROW_NORMAL);
1009 						left_sel->SetDisplayedPlane(DP_LEFT_REFIT);
1010 						middle_sel->SetDisplayedPlane(DP_MIDDLE_SERVICE);
1011 						right_sel->SetDisplayedPlane(DP_RIGHT_EMPTY);
1012 						this->EnableWidget(WID_O_NON_STOP);
1013 						this->SetWidgetLoweredState(WID_O_NON_STOP, order->GetNonStopType() & ONSF_NO_STOP_AT_INTERMEDIATE_STATIONS);
1014 					}
1015 					/* Disable refit button if the order is no 'always go' order.
1016 					 * However, keep the service button enabled for refit-orders to allow clearing refits (without knowing about ctrl). */
1017 					this->SetWidgetDisabledState(WID_O_REFIT,
1018 							(order->GetDepotOrderType() & ODTFB_SERVICE) || (order->GetDepotActionType() & ODATFB_HALT) ||
1019 							(!this->can_do_refit && !order->IsRefit()));
1020 					this->SetWidgetLoweredState(WID_O_SERVICE, order->GetDepotOrderType() & ODTFB_SERVICE);
1021 					break;
1022 
1023 				case OT_CONDITIONAL: {
1024 					if (row_sel != nullptr) {
1025 						row_sel->SetDisplayedPlane(DP_ROW_CONDITIONAL);
1026 					} else {
1027 						train_row_sel->SetDisplayedPlane(DP_GROUNDVEHICLE_ROW_CONDITIONAL);
1028 					}
1029 					OrderConditionVariable ocv = order->GetConditionVariable();
1030 					/* Set the strings for the dropdown boxes. */
1031 					this->GetWidget<NWidgetCore>(WID_O_COND_VARIABLE)->widget_data   = STR_ORDER_CONDITIONAL_LOAD_PERCENTAGE + ocv;
1032 					this->GetWidget<NWidgetCore>(WID_O_COND_COMPARATOR)->widget_data = _order_conditional_condition[order->GetConditionComparator()];
1033 					this->SetWidgetDisabledState(WID_O_COND_COMPARATOR, ocv == OCV_UNCONDITIONALLY);
1034 					this->SetWidgetDisabledState(WID_O_COND_VALUE, ocv == OCV_REQUIRES_SERVICE || ocv == OCV_UNCONDITIONALLY);
1035 					break;
1036 				}
1037 
1038 				default: // every other order
1039 					if (row_sel != nullptr) {
1040 						row_sel->SetDisplayedPlane(DP_ROW_LOAD);
1041 					} else {
1042 						train_row_sel->SetDisplayedPlane(DP_GROUNDVEHICLE_ROW_NORMAL);
1043 						left_sel->SetDisplayedPlane(DP_LEFT_LOAD);
1044 						middle_sel->SetDisplayedPlane(DP_MIDDLE_UNLOAD);
1045 						right_sel->SetDisplayedPlane(DP_RIGHT_EMPTY);
1046 						this->DisableWidget(WID_O_NON_STOP);
1047 					}
1048 					this->DisableWidget(WID_O_FULL_LOAD);
1049 					this->DisableWidget(WID_O_UNLOAD);
1050 					this->DisableWidget(WID_O_REFIT_DROPDOWN);
1051 					break;
1052 			}
1053 		}
1054 
1055 		/* Disable list of vehicles with the same shared orders if there is no list */
1056 		this->SetWidgetDisabledState(WID_O_SHARED_ORDER_LIST, !shared_orders);
1057 
1058 		this->SetDirty();
1059 	}
1060 
OnPaintOrdersWindow1061 	void OnPaint() override
1062 	{
1063 		if (this->vehicle->owner != _local_company) {
1064 			this->selected_order = -1; // Disable selection any selected row at a competitor order window.
1065 		} else {
1066 			this->SetWidgetLoweredState(WID_O_GOTO, this->goto_type != OPOS_NONE);
1067 		}
1068 		this->DrawWidgets();
1069 	}
1070 
DrawWidgetOrdersWindow1071 	void DrawWidget(const Rect &r, int widget) const override
1072 	{
1073 		if (widget != WID_O_ORDER_LIST) return;
1074 
1075 		bool rtl = _current_text_dir == TD_RTL;
1076 		SetDParamMaxValue(0, this->vehicle->GetNumOrders(), 2);
1077 		int index_column_width = GetStringBoundingBox(STR_ORDER_INDEX).width + 2 * GetSpriteSize(rtl ? SPR_ARROW_RIGHT : SPR_ARROW_LEFT).width + 3;
1078 		int middle = rtl ? r.right - WD_FRAMETEXT_RIGHT - index_column_width : r.left + WD_FRAMETEXT_LEFT + index_column_width;
1079 
1080 		int y = r.top + WD_FRAMERECT_TOP;
1081 		int line_height = this->GetWidget<NWidgetBase>(WID_O_ORDER_LIST)->resize_y;
1082 
1083 		int i = this->vscroll->GetPosition();
1084 		const Order *order = this->vehicle->GetOrder(i);
1085 		/* First draw the highlighting underground if it exists. */
1086 		if (this->order_over != INVALID_VEH_ORDER_ID) {
1087 			while (order != nullptr) {
1088 				/* Don't draw anything if it extends past the end of the window. */
1089 				if (!this->vscroll->IsVisible(i)) break;
1090 
1091 				if (i != this->selected_order && i == this->order_over) {
1092 					/* Highlight dragged order destination. */
1093 					int top = (this->order_over < this->selected_order ? y : y + line_height) - WD_FRAMERECT_TOP;
1094 					int bottom = std::min(top + 2, r.bottom - WD_FRAMERECT_BOTTOM);
1095 					top = std::max(top - 3, r.top + WD_FRAMERECT_TOP);
1096 					GfxFillRect(r.left + WD_FRAMETEXT_LEFT, top, r.right - WD_FRAMETEXT_RIGHT, bottom, _colour_gradient[COLOUR_GREY][7]);
1097 					break;
1098 				}
1099 				y += line_height;
1100 
1101 				i++;
1102 				order = order->next;
1103 			}
1104 
1105 			/* Reset counters for drawing the orders. */
1106 			y = r.top + WD_FRAMERECT_TOP;
1107 			i = this->vscroll->GetPosition();
1108 			order = this->vehicle->GetOrder(i);
1109 		}
1110 
1111 		/* Draw the orders. */
1112 		while (order != nullptr) {
1113 			/* Don't draw anything if it extends past the end of the window. */
1114 			if (!this->vscroll->IsVisible(i)) break;
1115 
1116 			DrawOrderString(this->vehicle, order, i, y, i == this->selected_order, false, r.left + WD_FRAMETEXT_LEFT, middle, r.right - WD_FRAMETEXT_RIGHT);
1117 			y += line_height;
1118 
1119 			i++;
1120 			order = order->next;
1121 		}
1122 
1123 		if (this->vscroll->IsVisible(i)) {
1124 			StringID str = this->vehicle->IsOrderListShared() ? STR_ORDERS_END_OF_SHARED_ORDERS : STR_ORDERS_END_OF_ORDERS;
1125 			DrawString(rtl ? r.left + WD_FRAMETEXT_LEFT : middle, rtl ? middle : r.right - WD_FRAMETEXT_RIGHT, y, str, (i == this->selected_order) ? TC_WHITE : TC_BLACK);
1126 		}
1127 	}
1128 
SetStringParametersOrdersWindow1129 	void SetStringParameters(int widget) const override
1130 	{
1131 		switch (widget) {
1132 			case WID_O_COND_VALUE: {
1133 				VehicleOrderID sel = this->OrderGetSel();
1134 				const Order *order = this->vehicle->GetOrder(sel);
1135 
1136 				if (order != nullptr && order->IsType(OT_CONDITIONAL)) {
1137 					uint value = order->GetConditionValue();
1138 					if (order->GetConditionVariable() == OCV_MAX_SPEED) value = ConvertSpeedToDisplaySpeed(value);
1139 					SetDParam(0, value);
1140 				}
1141 				break;
1142 			}
1143 
1144 			case WID_O_CAPTION:
1145 				SetDParam(0, this->vehicle->index);
1146 				break;
1147 		}
1148 	}
1149 
OnClickOrdersWindow1150 	void OnClick(Point pt, int widget, int click_count) override
1151 	{
1152 		switch (widget) {
1153 			case WID_O_ORDER_LIST: {
1154 				if (this->goto_type == OPOS_CONDITIONAL) {
1155 					VehicleOrderID order_id = this->GetOrderFromPt(_cursor.pos.y - this->top);
1156 					if (order_id != INVALID_VEH_ORDER_ID) {
1157 						Order order;
1158 						order.next = nullptr;
1159 						order.index = 0;
1160 						order.MakeConditional(order_id);
1161 
1162 						DoCommandP(this->vehicle->tile, this->vehicle->index + (this->OrderGetSel() << 20), order.Pack(), CMD_INSERT_ORDER | CMD_MSG(STR_ERROR_CAN_T_INSERT_NEW_ORDER));
1163 					}
1164 					ResetObjectToPlace();
1165 					break;
1166 				}
1167 
1168 				VehicleOrderID sel = this->GetOrderFromPt(pt.y);
1169 
1170 				if (_ctrl_pressed && sel < this->vehicle->GetNumOrders()) {
1171 					TileIndex xy = this->vehicle->GetOrder(sel)->GetLocation(this->vehicle);
1172 					if (xy != INVALID_TILE) ScrollMainWindowToTile(xy);
1173 					return;
1174 				}
1175 
1176 				/* This order won't be selected any more, close all child windows and dropdowns */
1177 				this->CloseChildWindows();
1178 				HideDropDownMenu(this);
1179 
1180 				if (sel == INVALID_VEH_ORDER_ID || this->vehicle->owner != _local_company) {
1181 					/* Deselect clicked order */
1182 					this->selected_order = -1;
1183 				} else if (sel == this->selected_order) {
1184 					if (this->vehicle->type == VEH_TRAIN && sel < this->vehicle->GetNumOrders()) {
1185 						DoCommandP(this->vehicle->tile, this->vehicle->index + (sel << 20),
1186 								MOF_STOP_LOCATION | ((this->vehicle->GetOrder(sel)->GetStopLocation() + 1) % OSL_END) << 4,
1187 								CMD_MODIFY_ORDER | CMD_MSG(STR_ERROR_CAN_T_MODIFY_THIS_ORDER));
1188 					}
1189 				} else {
1190 					/* Select clicked order */
1191 					this->selected_order = sel;
1192 
1193 					if (this->vehicle->owner == _local_company) {
1194 						/* Activate drag and drop */
1195 						SetObjectToPlaceWnd(SPR_CURSOR_MOUSE, PAL_NONE, HT_DRAG, this);
1196 					}
1197 				}
1198 
1199 				this->UpdateButtonState();
1200 				break;
1201 			}
1202 
1203 			case WID_O_SKIP:
1204 				this->OrderClick_Skip();
1205 				break;
1206 
1207 			case WID_O_DELETE:
1208 				this->OrderClick_Delete();
1209 				break;
1210 
1211 			case WID_O_STOP_SHARING:
1212 				this->OrderClick_StopSharing();
1213 				break;
1214 
1215 			case WID_O_NON_STOP:
1216 				if (this->GetWidget<NWidgetLeaf>(widget)->ButtonHit(pt)) {
1217 					this->OrderClick_Nonstop(-1);
1218 				} else {
1219 					const Order *o = this->vehicle->GetOrder(this->OrderGetSel());
1220 					ShowDropDownMenu(this, _order_non_stop_drowdown, o->GetNonStopType(), WID_O_NON_STOP, 0,
1221 													o->IsType(OT_GOTO_STATION) ? 0 : (o->IsType(OT_GOTO_WAYPOINT) ? 3 : 12));
1222 				}
1223 				break;
1224 
1225 			case WID_O_GOTO:
1226 				if (this->GetWidget<NWidgetLeaf>(widget)->ButtonHit(pt)) {
1227 					if (this->goto_type != OPOS_NONE) {
1228 						ResetObjectToPlace();
1229 					} else {
1230 						this->OrderClick_Goto(OPOS_GOTO);
1231 					}
1232 				} else {
1233 					int sel;
1234 					switch (this->goto_type) {
1235 						case OPOS_NONE:        sel = -1; break;
1236 						case OPOS_GOTO:        sel =  0; break;
1237 						case OPOS_CONDITIONAL: sel =  2; break;
1238 						case OPOS_SHARE:       sel =  3; break;
1239 						default: NOT_REACHED();
1240 					}
1241 					ShowDropDownMenu(this, this->vehicle->type == VEH_AIRCRAFT ? _order_goto_dropdown_aircraft : _order_goto_dropdown, sel, WID_O_GOTO, 0, 0);
1242 				}
1243 				break;
1244 
1245 			case WID_O_FULL_LOAD:
1246 				if (this->GetWidget<NWidgetLeaf>(widget)->ButtonHit(pt)) {
1247 					this->OrderClick_FullLoad(OLF_FULL_LOAD_ANY, true);
1248 				} else {
1249 					ShowDropDownMenu(this, _order_full_load_drowdown, this->vehicle->GetOrder(this->OrderGetSel())->GetLoadType(), WID_O_FULL_LOAD, 0, 2);
1250 				}
1251 				break;
1252 
1253 			case WID_O_UNLOAD:
1254 				if (this->GetWidget<NWidgetLeaf>(widget)->ButtonHit(pt)) {
1255 					this->OrderClick_Unload(OUFB_UNLOAD, true);
1256 				} else {
1257 					ShowDropDownMenu(this, _order_unload_drowdown, this->vehicle->GetOrder(this->OrderGetSel())->GetUnloadType(), WID_O_UNLOAD, 0, 8);
1258 				}
1259 				break;
1260 
1261 			case WID_O_REFIT:
1262 				this->OrderClick_Refit(0, false);
1263 				break;
1264 
1265 			case WID_O_SERVICE:
1266 				if (this->GetWidget<NWidgetLeaf>(widget)->ButtonHit(pt)) {
1267 					this->OrderClick_Service(-1);
1268 				} else {
1269 					ShowDropDownMenu(this, _order_depot_action_dropdown, DepotActionStringIndex(this->vehicle->GetOrder(this->OrderGetSel())), WID_O_SERVICE, 0, 0);
1270 				}
1271 				break;
1272 
1273 			case WID_O_REFIT_DROPDOWN:
1274 				if (this->GetWidget<NWidgetLeaf>(widget)->ButtonHit(pt)) {
1275 					this->OrderClick_Refit(0, true);
1276 				} else {
1277 					ShowDropDownMenu(this, _order_refit_action_dropdown, 0, WID_O_REFIT_DROPDOWN, 0, 0);
1278 				}
1279 				break;
1280 
1281 			case WID_O_TIMETABLE_VIEW:
1282 				ShowTimetableWindow(this->vehicle);
1283 				break;
1284 
1285 			case WID_O_COND_VARIABLE: {
1286 				DropDownList list;
1287 				for (uint i = 0; i < lengthof(_order_conditional_variable); i++) {
1288 					list.emplace_back(new DropDownListStringItem(STR_ORDER_CONDITIONAL_LOAD_PERCENTAGE + _order_conditional_variable[i], _order_conditional_variable[i], false));
1289 				}
1290 				ShowDropDownList(this, std::move(list), this->vehicle->GetOrder(this->OrderGetSel())->GetConditionVariable(), WID_O_COND_VARIABLE);
1291 				break;
1292 			}
1293 
1294 			case WID_O_COND_COMPARATOR: {
1295 				const Order *o = this->vehicle->GetOrder(this->OrderGetSel());
1296 				ShowDropDownMenu(this, _order_conditional_condition, o->GetConditionComparator(), WID_O_COND_COMPARATOR, 0, (o->GetConditionVariable() == OCV_REQUIRES_SERVICE) ? 0x3F : 0xC0);
1297 				break;
1298 			}
1299 
1300 			case WID_O_COND_VALUE: {
1301 				const Order *order = this->vehicle->GetOrder(this->OrderGetSel());
1302 				uint value = order->GetConditionValue();
1303 				if (order->GetConditionVariable() == OCV_MAX_SPEED) value = ConvertSpeedToDisplaySpeed(value);
1304 				SetDParam(0, value);
1305 				ShowQueryString(STR_JUST_INT, STR_ORDER_CONDITIONAL_VALUE_CAPT, 5, this, CS_NUMERAL, QSF_NONE);
1306 				break;
1307 			}
1308 
1309 			case WID_O_SHARED_ORDER_LIST:
1310 				ShowVehicleListWindow(this->vehicle);
1311 				break;
1312 		}
1313 	}
1314 
OnQueryTextFinishedOrdersWindow1315 	void OnQueryTextFinished(char *str) override
1316 	{
1317 		if (!StrEmpty(str)) {
1318 			VehicleOrderID sel = this->OrderGetSel();
1319 			uint value = atoi(str);
1320 
1321 			switch (this->vehicle->GetOrder(sel)->GetConditionVariable()) {
1322 				case OCV_MAX_SPEED:
1323 					value = ConvertDisplaySpeedToSpeed(value);
1324 					break;
1325 
1326 				case OCV_RELIABILITY:
1327 				case OCV_LOAD_PERCENTAGE:
1328 					value = Clamp(value, 0, 100);
1329 					break;
1330 
1331 				default:
1332 					break;
1333 			}
1334 			DoCommandP(this->vehicle->tile, this->vehicle->index + (sel << 20), MOF_COND_VALUE | Clamp(value, 0, 2047) << 4, CMD_MODIFY_ORDER | CMD_MSG(STR_ERROR_CAN_T_MODIFY_THIS_ORDER));
1335 		}
1336 	}
1337 
OnDropdownSelectOrdersWindow1338 	void OnDropdownSelect(int widget, int index) override
1339 	{
1340 		switch (widget) {
1341 			case WID_O_NON_STOP:
1342 				this->OrderClick_Nonstop(index);
1343 				break;
1344 
1345 			case WID_O_FULL_LOAD:
1346 				this->OrderClick_FullLoad((OrderLoadFlags)index);
1347 				break;
1348 
1349 			case WID_O_UNLOAD:
1350 				this->OrderClick_Unload((OrderUnloadFlags)index);
1351 				break;
1352 
1353 			case WID_O_GOTO:
1354 				switch (index) {
1355 					case 0: this->OrderClick_Goto(OPOS_GOTO); break;
1356 					case 1: this->OrderClick_NearestDepot(); break;
1357 					case 2: this->OrderClick_Goto(OPOS_CONDITIONAL); break;
1358 					case 3: this->OrderClick_Goto(OPOS_SHARE); break;
1359 					default: NOT_REACHED();
1360 				}
1361 				break;
1362 
1363 			case WID_O_SERVICE:
1364 				this->OrderClick_Service(index);
1365 				break;
1366 
1367 			case WID_O_REFIT_DROPDOWN:
1368 				this->OrderClick_Refit(index, true);
1369 				break;
1370 
1371 			case WID_O_COND_VARIABLE:
1372 				DoCommandP(this->vehicle->tile, this->vehicle->index + (this->OrderGetSel() << 20), MOF_COND_VARIABLE | index << 4,  CMD_MODIFY_ORDER | CMD_MSG(STR_ERROR_CAN_T_MODIFY_THIS_ORDER));
1373 				break;
1374 
1375 			case WID_O_COND_COMPARATOR:
1376 				DoCommandP(this->vehicle->tile, this->vehicle->index + (this->OrderGetSel() << 20), MOF_COND_COMPARATOR | index << 4,  CMD_MODIFY_ORDER | CMD_MSG(STR_ERROR_CAN_T_MODIFY_THIS_ORDER));
1377 				break;
1378 		}
1379 	}
1380 
OnDragDropOrdersWindow1381 	void OnDragDrop(Point pt, int widget) override
1382 	{
1383 		switch (widget) {
1384 			case WID_O_ORDER_LIST: {
1385 				VehicleOrderID from_order = this->OrderGetSel();
1386 				VehicleOrderID to_order = this->GetOrderFromPt(pt.y);
1387 
1388 				if (!(from_order == to_order || from_order == INVALID_VEH_ORDER_ID || from_order > this->vehicle->GetNumOrders() || to_order == INVALID_VEH_ORDER_ID || to_order > this->vehicle->GetNumOrders()) &&
1389 						DoCommandP(this->vehicle->tile, this->vehicle->index, from_order | (to_order << 16), CMD_MOVE_ORDER | CMD_MSG(STR_ERROR_CAN_T_MOVE_THIS_ORDER))) {
1390 					this->selected_order = -1;
1391 					this->UpdateButtonState();
1392 				}
1393 				break;
1394 			}
1395 
1396 			case WID_O_DELETE:
1397 				this->OrderClick_Delete();
1398 				break;
1399 
1400 			case WID_O_STOP_SHARING:
1401 				this->OrderClick_StopSharing();
1402 				break;
1403 		}
1404 
1405 		ResetObjectToPlace();
1406 
1407 		if (this->order_over != INVALID_VEH_ORDER_ID) {
1408 			/* End of drag-and-drop, hide dragged order destination highlight. */
1409 			this->order_over = INVALID_VEH_ORDER_ID;
1410 			this->SetWidgetDirty(WID_O_ORDER_LIST);
1411 		}
1412 	}
1413 
OnHotkeyOrdersWindow1414 	EventState OnHotkey(int hotkey) override
1415 	{
1416 		if (this->vehicle->owner != _local_company) return ES_NOT_HANDLED;
1417 
1418 		switch (hotkey) {
1419 			case OHK_SKIP:           this->OrderClick_Skip(); break;
1420 			case OHK_DELETE:         this->OrderClick_Delete(); break;
1421 			case OHK_GOTO:           this->OrderClick_Goto(OPOS_GOTO); break;
1422 			case OHK_NONSTOP:        this->OrderClick_Nonstop(-1); break;
1423 			case OHK_FULLLOAD:       this->OrderClick_FullLoad(OLF_FULL_LOAD_ANY, true); break;
1424 			case OHK_UNLOAD:         this->OrderClick_Unload(OUFB_UNLOAD, true); break;
1425 			case OHK_NEAREST_DEPOT:  this->OrderClick_NearestDepot(); break;
1426 			case OHK_ALWAYS_SERVICE: this->OrderClick_Service(-1); break;
1427 			case OHK_TRANSFER:       this->OrderClick_Unload(OUFB_TRANSFER, true); break;
1428 			case OHK_NO_UNLOAD:      this->OrderClick_Unload(OUFB_NO_UNLOAD, true); break;
1429 			case OHK_NO_LOAD:        this->OrderClick_FullLoad(OLFB_NO_LOAD, true); break;
1430 			default: return ES_NOT_HANDLED;
1431 		}
1432 		return ES_HANDLED;
1433 	}
1434 
OnPlaceObjectOrdersWindow1435 	void OnPlaceObject(Point pt, TileIndex tile) override
1436 	{
1437 		if (this->goto_type == OPOS_GOTO) {
1438 			const Order cmd = GetOrderCmdFromTile(this->vehicle, tile);
1439 			if (cmd.IsType(OT_NOTHING)) return;
1440 
1441 			if (DoCommandP(this->vehicle->tile, this->vehicle->index + (this->OrderGetSel() << 20), cmd.Pack(), CMD_INSERT_ORDER | CMD_MSG(STR_ERROR_CAN_T_INSERT_NEW_ORDER))) {
1442 				/* With quick goto the Go To button stays active */
1443 				if (!_settings_client.gui.quick_goto) ResetObjectToPlace();
1444 			}
1445 		}
1446 	}
1447 
OnVehicleSelectOrdersWindow1448 	bool OnVehicleSelect(const Vehicle *v) override
1449 	{
1450 		/* v is vehicle getting orders. Only copy/clone orders if vehicle doesn't have any orders yet.
1451 		 * We disallow copying orders of other vehicles if we already have at least one order entry
1452 		 * ourself as it easily copies orders of vehicles within a station when we mean the station.
1453 		 * Obviously if you press CTRL on a non-empty orders vehicle you know what you are doing
1454 		 * TODO: give a warning message */
1455 		bool share_order = _ctrl_pressed || this->goto_type == OPOS_SHARE;
1456 		if (this->vehicle->GetNumOrders() != 0 && !share_order) return false;
1457 
1458 		if (DoCommandP(this->vehicle->tile, this->vehicle->index | (share_order ? CO_SHARE : CO_COPY) << 30, v->index,
1459 				share_order ? CMD_CLONE_ORDER | CMD_MSG(STR_ERROR_CAN_T_SHARE_ORDER_LIST) : CMD_CLONE_ORDER | CMD_MSG(STR_ERROR_CAN_T_COPY_ORDER_LIST))) {
1460 			this->selected_order = -1;
1461 			ResetObjectToPlace();
1462 		}
1463 		return true;
1464 	}
1465 
OnPlaceObjectAbortOrdersWindow1466 	void OnPlaceObjectAbort() override
1467 	{
1468 		this->goto_type = OPOS_NONE;
1469 		this->SetWidgetDirty(WID_O_GOTO);
1470 
1471 		/* Remove drag highlighting if it exists. */
1472 		if (this->order_over != INVALID_VEH_ORDER_ID) {
1473 			this->order_over = INVALID_VEH_ORDER_ID;
1474 			this->SetWidgetDirty(WID_O_ORDER_LIST);
1475 		}
1476 	}
1477 
OnMouseDragOrdersWindow1478 	void OnMouseDrag(Point pt, int widget) override
1479 	{
1480 		if (this->selected_order != -1 && widget == WID_O_ORDER_LIST) {
1481 			/* An order is dragged.. */
1482 			VehicleOrderID from_order = this->OrderGetSel();
1483 			VehicleOrderID to_order = this->GetOrderFromPt(pt.y);
1484 			uint num_orders = this->vehicle->GetNumOrders();
1485 
1486 			if (from_order != INVALID_VEH_ORDER_ID && from_order <= num_orders) {
1487 				if (to_order != INVALID_VEH_ORDER_ID && to_order <= num_orders) { // ..over an existing order.
1488 					this->order_over = to_order;
1489 					this->SetWidgetDirty(widget);
1490 				} else if (from_order != to_order && this->order_over != INVALID_VEH_ORDER_ID) { // ..outside of the order list.
1491 					this->order_over = INVALID_VEH_ORDER_ID;
1492 					this->SetWidgetDirty(widget);
1493 				}
1494 			}
1495 		}
1496 	}
1497 
OnResizeOrdersWindow1498 	void OnResize() override
1499 	{
1500 		/* Update the scroll bar */
1501 		this->vscroll->SetCapacityFromWidget(this, WID_O_ORDER_LIST);
1502 	}
1503 
1504 	static HotkeyList hotkeys;
1505 };
1506 
1507 static Hotkey order_hotkeys[] = {
1508 	Hotkey('D', "skip", OHK_SKIP),
1509 	Hotkey('F', "delete", OHK_DELETE),
1510 	Hotkey('G', "goto", OHK_GOTO),
1511 	Hotkey('H', "nonstop", OHK_NONSTOP),
1512 	Hotkey('J', "fullload", OHK_FULLLOAD),
1513 	Hotkey('K', "unload", OHK_UNLOAD),
1514 	Hotkey((uint16)0, "nearest_depot", OHK_NEAREST_DEPOT),
1515 	Hotkey((uint16)0, "always_service", OHK_ALWAYS_SERVICE),
1516 	Hotkey((uint16)0, "transfer", OHK_TRANSFER),
1517 	Hotkey((uint16)0, "no_unload", OHK_NO_UNLOAD),
1518 	Hotkey((uint16)0, "no_load", OHK_NO_LOAD),
1519 	HOTKEY_LIST_END
1520 };
1521 HotkeyList OrdersWindow::hotkeys("order", order_hotkeys);
1522 
1523 /** Nested widget definition for "your" train orders. */
1524 static const NWidgetPart _nested_orders_train_widgets[] = {
1525 	NWidget(NWID_HORIZONTAL),
1526 		NWidget(WWT_CLOSEBOX, COLOUR_GREY),
1527 		NWidget(WWT_CAPTION, COLOUR_GREY, WID_O_CAPTION), SetDataTip(STR_ORDERS_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
1528 		NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_O_TIMETABLE_VIEW), SetMinimalSize(61, 14), SetDataTip(STR_ORDERS_TIMETABLE_VIEW, STR_ORDERS_TIMETABLE_VIEW_TOOLTIP),
1529 		NWidget(WWT_SHADEBOX, COLOUR_GREY),
1530 		NWidget(WWT_DEFSIZEBOX, COLOUR_GREY),
1531 		NWidget(WWT_STICKYBOX, COLOUR_GREY),
1532 	EndContainer(),
1533 	NWidget(NWID_HORIZONTAL),
1534 		NWidget(WWT_PANEL, COLOUR_GREY, WID_O_ORDER_LIST), SetMinimalSize(372, 62), SetDataTip(0x0, STR_ORDERS_LIST_TOOLTIP), SetResize(1, 1), SetScrollbar(WID_O_SCROLLBAR), EndContainer(),
1535 		NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_O_SCROLLBAR),
1536 	EndContainer(),
1537 
1538 	/* First button row. */
1539 	NWidget(NWID_HORIZONTAL),
1540 		NWidget(NWID_SELECTION, INVALID_COLOUR, WID_O_SEL_TOP_ROW_GROUNDVEHICLE),
1541 			NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
1542 				NWidget(NWID_BUTTON_DROPDOWN, COLOUR_GREY, WID_O_NON_STOP), SetMinimalSize(93, 12), SetFill(1, 0),
1543 															SetDataTip(STR_ORDER_NON_STOP, STR_ORDER_TOOLTIP_NON_STOP), SetResize(1, 0),
1544 				NWidget(NWID_SELECTION, INVALID_COLOUR, WID_O_SEL_TOP_LEFT),
1545 					NWidget(NWID_BUTTON_DROPDOWN, COLOUR_GREY, WID_O_FULL_LOAD), SetMinimalSize(93, 12), SetFill(1, 0),
1546 															SetDataTip(STR_ORDER_TOGGLE_FULL_LOAD, STR_ORDER_TOOLTIP_FULL_LOAD), SetResize(1, 0),
1547 					NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_O_REFIT), SetMinimalSize(93, 12), SetFill(1, 0),
1548 															SetDataTip(STR_ORDER_REFIT, STR_ORDER_REFIT_TOOLTIP), SetResize(1, 0),
1549 				EndContainer(),
1550 				NWidget(NWID_SELECTION, INVALID_COLOUR, WID_O_SEL_TOP_MIDDLE),
1551 					NWidget(NWID_BUTTON_DROPDOWN, COLOUR_GREY, WID_O_UNLOAD), SetMinimalSize(93, 12), SetFill(1, 0),
1552 															SetDataTip(STR_ORDER_TOGGLE_UNLOAD, STR_ORDER_TOOLTIP_UNLOAD), SetResize(1, 0),
1553 					NWidget(NWID_BUTTON_DROPDOWN, COLOUR_GREY, WID_O_SERVICE), SetMinimalSize(93, 12), SetFill(1, 0),
1554 															SetDataTip(STR_ORDER_SERVICE, STR_ORDER_SERVICE_TOOLTIP), SetResize(1, 0),
1555 				EndContainer(),
1556 				NWidget(NWID_SELECTION, INVALID_COLOUR, WID_O_SEL_TOP_RIGHT),
1557 					NWidget(WWT_TEXTBTN, COLOUR_GREY, WID_O_EMPTY), SetMinimalSize(93, 12), SetFill(1, 0),
1558 															SetDataTip(STR_ORDER_REFIT, STR_ORDER_REFIT_TOOLTIP), SetResize(1, 0),
1559 					NWidget(NWID_BUTTON_DROPDOWN, COLOUR_GREY, WID_O_REFIT_DROPDOWN), SetMinimalSize(93, 12), SetFill(1, 0),
1560 															SetDataTip(STR_ORDER_REFIT_AUTO, STR_ORDER_REFIT_AUTO_TOOLTIP), SetResize(1, 0),
1561 				EndContainer(),
1562 			EndContainer(),
1563 			NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
1564 				NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_O_COND_VARIABLE), SetMinimalSize(124, 12), SetFill(1, 0),
1565 															SetDataTip(STR_NULL, STR_ORDER_CONDITIONAL_VARIABLE_TOOLTIP), SetResize(1, 0),
1566 				NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_O_COND_COMPARATOR), SetMinimalSize(124, 12), SetFill(1, 0),
1567 															SetDataTip(STR_NULL, STR_ORDER_CONDITIONAL_COMPARATOR_TOOLTIP), SetResize(1, 0),
1568 				NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_O_COND_VALUE), SetMinimalSize(124, 12), SetFill(1, 0),
1569 															SetDataTip(STR_BLACK_COMMA, STR_ORDER_CONDITIONAL_VALUE_TOOLTIP), SetResize(1, 0),
1570 			EndContainer(),
1571 		EndContainer(),
1572 		NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_O_SHARED_ORDER_LIST), SetMinimalSize(12, 12), SetDataTip(SPR_SHARED_ORDERS_ICON, STR_ORDERS_VEH_WITH_SHARED_ORDERS_LIST_TOOLTIP),
1573 	EndContainer(),
1574 
1575 	/* Second button row. */
1576 	NWidget(NWID_HORIZONTAL),
1577 		NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
1578 			NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_O_SKIP), SetMinimalSize(124, 12), SetFill(1, 0),
1579 													SetDataTip(STR_ORDERS_SKIP_BUTTON, STR_ORDERS_SKIP_TOOLTIP), SetResize(1, 0),
1580 			NWidget(NWID_SELECTION, INVALID_COLOUR, WID_O_SEL_BOTTOM_MIDDLE),
1581 				NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_O_DELETE), SetMinimalSize(124, 12), SetFill(1, 0),
1582 														SetDataTip(STR_ORDERS_DELETE_BUTTON, STR_ORDERS_DELETE_TOOLTIP), SetResize(1, 0),
1583 				NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_O_STOP_SHARING), SetMinimalSize(124, 12), SetFill(1, 0),
1584 														SetDataTip(STR_ORDERS_STOP_SHARING_BUTTON, STR_ORDERS_STOP_SHARING_TOOLTIP), SetResize(1, 0),
1585 			EndContainer(),
1586 			NWidget(NWID_BUTTON_DROPDOWN, COLOUR_GREY, WID_O_GOTO), SetMinimalSize(124, 12), SetFill(1, 0),
1587 													SetDataTip(STR_ORDERS_GO_TO_BUTTON, STR_ORDERS_GO_TO_TOOLTIP), SetResize(1, 0),
1588 		EndContainer(),
1589 		NWidget(WWT_RESIZEBOX, COLOUR_GREY),
1590 	EndContainer(),
1591 };
1592 
1593 static WindowDesc _orders_train_desc(
1594 	WDP_AUTO, "view_vehicle_orders_train", 384, 100,
1595 	WC_VEHICLE_ORDERS, WC_VEHICLE_VIEW,
1596 	WDF_CONSTRUCTION,
1597 	_nested_orders_train_widgets, lengthof(_nested_orders_train_widgets),
1598 	&OrdersWindow::hotkeys
1599 );
1600 
1601 /** Nested widget definition for "your" orders (non-train). */
1602 static const NWidgetPart _nested_orders_widgets[] = {
1603 	NWidget(NWID_HORIZONTAL),
1604 		NWidget(WWT_CLOSEBOX, COLOUR_GREY),
1605 		NWidget(WWT_CAPTION, COLOUR_GREY, WID_O_CAPTION), SetDataTip(STR_ORDERS_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
1606 		NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_O_TIMETABLE_VIEW), SetMinimalSize(61, 14), SetDataTip(STR_ORDERS_TIMETABLE_VIEW, STR_ORDERS_TIMETABLE_VIEW_TOOLTIP),
1607 		NWidget(WWT_SHADEBOX, COLOUR_GREY),
1608 		NWidget(WWT_DEFSIZEBOX, COLOUR_GREY),
1609 		NWidget(WWT_STICKYBOX, COLOUR_GREY),
1610 	EndContainer(),
1611 	NWidget(NWID_HORIZONTAL),
1612 		NWidget(WWT_PANEL, COLOUR_GREY, WID_O_ORDER_LIST), SetMinimalSize(372, 62), SetDataTip(0x0, STR_ORDERS_LIST_TOOLTIP), SetResize(1, 1), SetScrollbar(WID_O_SCROLLBAR), EndContainer(),
1613 		NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_O_SCROLLBAR),
1614 	EndContainer(),
1615 
1616 	/* First button row. */
1617 	NWidget(NWID_HORIZONTAL),
1618 		NWidget(NWID_SELECTION, INVALID_COLOUR, WID_O_SEL_TOP_ROW),
1619 			/* Load + unload + refit buttons. */
1620 			NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
1621 				NWidget(NWID_BUTTON_DROPDOWN, COLOUR_GREY, WID_O_FULL_LOAD), SetMinimalSize(124, 12), SetFill(1, 0),
1622 													SetDataTip(STR_ORDER_TOGGLE_FULL_LOAD, STR_ORDER_TOOLTIP_FULL_LOAD), SetResize(1, 0),
1623 				NWidget(NWID_BUTTON_DROPDOWN, COLOUR_GREY, WID_O_UNLOAD), SetMinimalSize(124, 12), SetFill(1, 0),
1624 													SetDataTip(STR_ORDER_TOGGLE_UNLOAD, STR_ORDER_TOOLTIP_UNLOAD), SetResize(1, 0),
1625 				NWidget(NWID_BUTTON_DROPDOWN, COLOUR_GREY, WID_O_REFIT_DROPDOWN), SetMinimalSize(124, 12), SetFill(1, 0),
1626 													SetDataTip(STR_ORDER_REFIT_AUTO, STR_ORDER_REFIT_AUTO_TOOLTIP), SetResize(1, 0),
1627 			EndContainer(),
1628 			/* Refit + service buttons. */
1629 			NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
1630 				NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_O_REFIT), SetMinimalSize(186, 12), SetFill(1, 0),
1631 													SetDataTip(STR_ORDER_REFIT, STR_ORDER_REFIT_TOOLTIP), SetResize(1, 0),
1632 				NWidget(NWID_BUTTON_DROPDOWN, COLOUR_GREY, WID_O_SERVICE), SetMinimalSize(124, 12), SetFill(1, 0),
1633 													SetDataTip(STR_ORDER_SERVICE, STR_ORDER_SERVICE_TOOLTIP), SetResize(1, 0),
1634 			EndContainer(),
1635 
1636 			/* Buttons for setting a condition. */
1637 			NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
1638 				NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_O_COND_VARIABLE), SetMinimalSize(124, 12), SetFill(1, 0),
1639 													SetDataTip(STR_NULL, STR_ORDER_CONDITIONAL_VARIABLE_TOOLTIP), SetResize(1, 0),
1640 				NWidget(WWT_DROPDOWN, COLOUR_GREY, WID_O_COND_COMPARATOR), SetMinimalSize(124, 12), SetFill(1, 0),
1641 													SetDataTip(STR_NULL, STR_ORDER_CONDITIONAL_COMPARATOR_TOOLTIP), SetResize(1, 0),
1642 				NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_O_COND_VALUE), SetMinimalSize(124, 12), SetFill(1, 0),
1643 													SetDataTip(STR_BLACK_COMMA, STR_ORDER_CONDITIONAL_VALUE_TOOLTIP), SetResize(1, 0),
1644 			EndContainer(),
1645 		EndContainer(),
1646 
1647 		NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, WID_O_SHARED_ORDER_LIST), SetMinimalSize(12, 12), SetDataTip(SPR_SHARED_ORDERS_ICON, STR_ORDERS_VEH_WITH_SHARED_ORDERS_LIST_TOOLTIP),
1648 	EndContainer(),
1649 
1650 	/* Second button row. */
1651 	NWidget(NWID_HORIZONTAL),
1652 		NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_O_SKIP), SetMinimalSize(124, 12), SetFill(1, 0),
1653 											SetDataTip(STR_ORDERS_SKIP_BUTTON, STR_ORDERS_SKIP_TOOLTIP), SetResize(1, 0),
1654 		NWidget(NWID_SELECTION, INVALID_COLOUR, WID_O_SEL_BOTTOM_MIDDLE),
1655 			NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_O_DELETE), SetMinimalSize(124, 12), SetFill(1, 0),
1656 													SetDataTip(STR_ORDERS_DELETE_BUTTON, STR_ORDERS_DELETE_TOOLTIP), SetResize(1, 0),
1657 			NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_O_STOP_SHARING), SetMinimalSize(124, 12), SetFill(1, 0),
1658 													SetDataTip(STR_ORDERS_STOP_SHARING_BUTTON, STR_ORDERS_STOP_SHARING_TOOLTIP), SetResize(1, 0),
1659 		EndContainer(),
1660 		NWidget(NWID_BUTTON_DROPDOWN, COLOUR_GREY, WID_O_GOTO), SetMinimalSize(124, 12), SetFill(1, 0),
1661 											SetDataTip(STR_ORDERS_GO_TO_BUTTON, STR_ORDERS_GO_TO_TOOLTIP), SetResize(1, 0),
1662 		NWidget(WWT_RESIZEBOX, COLOUR_GREY),
1663 	EndContainer(),
1664 };
1665 
1666 static WindowDesc _orders_desc(
1667 	WDP_AUTO, "view_vehicle_orders", 384, 100,
1668 	WC_VEHICLE_ORDERS, WC_VEHICLE_VIEW,
1669 	WDF_CONSTRUCTION,
1670 	_nested_orders_widgets, lengthof(_nested_orders_widgets),
1671 	&OrdersWindow::hotkeys
1672 );
1673 
1674 /** Nested widget definition for competitor orders. */
1675 static const NWidgetPart _nested_other_orders_widgets[] = {
1676 	NWidget(NWID_HORIZONTAL),
1677 		NWidget(WWT_CLOSEBOX, COLOUR_GREY),
1678 		NWidget(WWT_CAPTION, COLOUR_GREY, WID_O_CAPTION), SetDataTip(STR_ORDERS_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
1679 		NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_O_TIMETABLE_VIEW), SetMinimalSize(61, 14), SetDataTip(STR_ORDERS_TIMETABLE_VIEW, STR_ORDERS_TIMETABLE_VIEW_TOOLTIP),
1680 		NWidget(WWT_SHADEBOX, COLOUR_GREY),
1681 		NWidget(WWT_DEFSIZEBOX, COLOUR_GREY),
1682 		NWidget(WWT_STICKYBOX, COLOUR_GREY),
1683 	EndContainer(),
1684 	NWidget(NWID_HORIZONTAL),
1685 		NWidget(WWT_PANEL, COLOUR_GREY, WID_O_ORDER_LIST), SetMinimalSize(372, 72), SetDataTip(0x0, STR_ORDERS_LIST_TOOLTIP), SetResize(1, 1), SetScrollbar(WID_O_SCROLLBAR), EndContainer(),
1686 		NWidget(NWID_VERTICAL),
1687 			NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_O_SCROLLBAR),
1688 			NWidget(WWT_RESIZEBOX, COLOUR_GREY),
1689 		EndContainer(),
1690 	EndContainer(),
1691 };
1692 
1693 static WindowDesc _other_orders_desc(
1694 	WDP_AUTO, "view_vehicle_orders_competitor", 384, 86,
1695 	WC_VEHICLE_ORDERS, WC_VEHICLE_VIEW,
1696 	WDF_CONSTRUCTION,
1697 	_nested_other_orders_widgets, lengthof(_nested_other_orders_widgets),
1698 	&OrdersWindow::hotkeys
1699 );
1700 
ShowOrdersWindow(const Vehicle * v)1701 void ShowOrdersWindow(const Vehicle *v)
1702 {
1703 	CloseWindowById(WC_VEHICLE_DETAILS, v->index, false);
1704 	CloseWindowById(WC_VEHICLE_TIMETABLE, v->index, false);
1705 	if (BringWindowToFrontById(WC_VEHICLE_ORDERS, v->index) != nullptr) return;
1706 
1707 	/* Using a different WindowDescs for _local_company causes problems.
1708 	 * Due to this we have to close order windows in ChangeWindowOwner/CloseCompanyWindows,
1709 	 * because we cannot change switch the WindowDescs and keeping the old WindowDesc results
1710 	 * in crashed due to missing widges.
1711 	 * TODO Rewrite the order GUI to not use different WindowDescs.
1712 	 */
1713 	if (v->owner != _local_company) {
1714 		new OrdersWindow(&_other_orders_desc, v);
1715 	} else {
1716 		new OrdersWindow(v->IsGroundVehicle() ? &_orders_train_desc : &_orders_desc, v);
1717 	}
1718 }
1719