# order_gui.cpp

Go to the documentation of this file.

    00001 /* $Id$ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
    00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
    00008  */
    00009
    00012 #include "stdafx.h"
    00013 #include "command_func.h"
    00014 #include "viewport_func.h"
    00015 #include "depot_map.h"
    00016 #include "vehicle_gui.h"
    00017 #include "roadveh.h"
    00018 #include "timetable.h"
    00019 #include "strings_func.h"
    00020 #include "window_func.h"
    00021 #include "company_func.h"
    00022 #include "widgets/dropdown_func.h"
    00023 #include "textbuf_gui.h"
    00024 #include "string_func.h"
    00025 #include "tilehighlight_func.h"
    00026 #include "network/network.h"
    00027 #include "station_base.h"
    00028 #include "waypoint_base.h"
    00029 #include "core/geometry_func.hpp"
    00030 #include "hotkeys.h"
    00031
    00032 #include "table/strings.h"
    00033
    00035 enum OrderWindowWidgets {
    00036   ORDER_WIDGET_CAPTION,
    00037   ORDER_WIDGET_TIMETABLE_VIEW,
    00038   ORDER_WIDGET_ORDER_LIST,
    00039   ORDER_WIDGET_SCROLLBAR,
    00040   ORDER_WIDGET_SKIP,
    00041   ORDER_WIDGET_DELETE,
    00042   ORDER_WIDGET_STOP_SHARING,
    00043   ORDER_WIDGET_NON_STOP,
    00044   ORDER_WIDGET_GOTO,
    00045   ORDER_WIDGET_FULL_LOAD,
    00046   ORDER_WIDGET_UNLOAD,
    00047   ORDER_WIDGET_REFIT,
    00048   ORDER_WIDGET_SERVICE,
    00049   ORDER_WIDGET_COND_VARIABLE,
    00050   ORDER_WIDGET_COND_COMPARATOR,
    00051   ORDER_WIDGET_COND_VALUE,
    00052   ORDER_WIDGET_SEL_TOP_LEFT,
    00053   ORDER_WIDGET_SEL_TOP_MIDDLE,
    00054   ORDER_WIDGET_SEL_TOP_RIGHT,
    00055   ORDER_WIDGET_SEL_TOP_ROW,
    00056   ORDER_WIDGET_SEL_BOTTOM_MIDDLE,
    00057   ORDER_WIDGET_SHARED_ORDER_LIST,
    00058 };
    00059
    00061 static const StringID _station_load_types[][5] = {
    00062   {
    00063     STR_EMPTY,
    00064     INVALID_STRING_ID,
    00065     STR_ORDER_FULL_LOAD,
    00066     STR_ORDER_FULL_LOAD_ANY,
    00067     STR_ORDER_NO_LOAD,
    00068   }, {
    00069     STR_ORDER_UNLOAD,
    00070     INVALID_STRING_ID,
    00071     STR_ORDER_UNLOAD_FULL_LOAD,
    00072     STR_ORDER_UNLOAD_FULL_LOAD_ANY,
    00073     STR_ORDER_UNLOAD_NO_LOAD,
    00074   }, {
    00075     STR_ORDER_TRANSFER,
    00076     INVALID_STRING_ID,
    00077     STR_ORDER_TRANSFER_FULL_LOAD,
    00078     STR_ORDER_TRANSFER_FULL_LOAD_ANY,
    00079     STR_ORDER_TRANSFER_NO_LOAD,
    00080   }, {
    00081     /* Unload and transfer do not work together. */
    00082     INVALID_STRING_ID,
    00083     INVALID_STRING_ID,
    00084     INVALID_STRING_ID,
    00085     INVALID_STRING_ID,
    00086   }, {
    00087     STR_ORDER_NO_UNLOAD,
    00088     INVALID_STRING_ID,
    00089     STR_ORDER_NO_UNLOAD_FULL_LOAD,
    00090     STR_ORDER_NO_UNLOAD_FULL_LOAD_ANY,
    00091     STR_ORDER_NO_UNLOAD_NO_LOAD,
    00092   }
    00093 };
    00094
    00095 static const StringID _order_non_stop_drowdown[] = {
    00096   STR_ORDER_GO_TO,
    00097   STR_ORDER_GO_NON_STOP_TO,
    00098   STR_ORDER_GO_VIA,
    00099   STR_ORDER_GO_NON_STOP_VIA,
    00100   INVALID_STRING_ID
    00101 };
    00102
    00103 static const StringID _order_full_load_drowdown[] = {
    00104   STR_ORDER_DROP_LOAD_IF_POSSIBLE,
    00105   STR_EMPTY,
    00106   STR_ORDER_DROP_FULL_LOAD_ALL,
    00107   STR_ORDER_DROP_FULL_LOAD_ANY,
    00108   STR_ORDER_DROP_NO_LOADING,
    00109   INVALID_STRING_ID
    00110 };
    00111
    00112 static const StringID _order_unload_drowdown[] = {
    00113   STR_ORDER_DROP_UNLOAD_IF_ACCEPTED,
    00114   STR_ORDER_DROP_UNLOAD,
    00115   STR_ORDER_DROP_TRANSFER,
    00116   STR_EMPTY,
    00117   STR_ORDER_DROP_NO_UNLOADING,
    00118   INVALID_STRING_ID
    00119 };
    00120
    00121 static const StringID _order_goto_dropdown[] = {
    00122   STR_ORDER_GO_TO,
    00123   STR_ORDER_GO_TO_NEAREST_DEPOT,
    00124   STR_ORDER_CONDITIONAL,
    00125   STR_ORDER_SHARE,
    00126   INVALID_STRING_ID
    00127 };
    00128
    00129 static const StringID _order_goto_dropdown_aircraft[] = {
    00130   STR_ORDER_GO_TO,
    00131   STR_ORDER_GO_TO_NEAREST_HANGAR,
    00132   STR_ORDER_CONDITIONAL,
    00133   STR_ORDER_SHARE,
    00134   INVALID_STRING_ID
    00135 };
    00136
    00137 static const StringID _order_conditional_variable[] = {
    00138   STR_ORDER_CONDITIONAL_LOAD_PERCENTAGE,
    00139   STR_ORDER_CONDITIONAL_RELIABILITY,
    00140   STR_ORDER_CONDITIONAL_MAX_SPEED,
    00141   STR_ORDER_CONDITIONAL_AGE,
    00142   STR_ORDER_CONDITIONAL_REQUIRES_SERVICE,
    00143   STR_ORDER_CONDITIONAL_UNCONDITIONALLY,
    00144   INVALID_STRING_ID,
    00145 };
    00146
    00147 static const StringID _order_conditional_condition[] = {
    00148   STR_ORDER_CONDITIONAL_COMPARATOR_EQUALS,
    00149   STR_ORDER_CONDITIONAL_COMPARATOR_NOT_EQUALS,
    00150   STR_ORDER_CONDITIONAL_COMPARATOR_LESS_THAN,
    00151   STR_ORDER_CONDITIONAL_COMPARATOR_LESS_EQUALS,
    00152   STR_ORDER_CONDITIONAL_COMPARATOR_MORE_THAN,
    00153   STR_ORDER_CONDITIONAL_COMPARATOR_MORE_EQUALS,
    00154   STR_ORDER_CONDITIONAL_COMPARATOR_IS_TRUE,
    00155   STR_ORDER_CONDITIONAL_COMPARATOR_IS_FALSE,
    00156   INVALID_STRING_ID,
    00157 };
    00158
    00159 extern uint ConvertSpeedToDisplaySpeed(uint speed);
    00160 extern uint ConvertDisplaySpeedToSpeed(uint speed);
    00161
    00162 static const StringID _order_depot_action_dropdown[] = {
    00163   STR_ORDER_DROP_GO_ALWAYS_DEPOT,
    00164   STR_ORDER_DROP_SERVICE_DEPOT,
    00165   STR_ORDER_DROP_HALT_DEPOT,
    00166   INVALID_STRING_ID
    00167 };
    00168
    00169 static int DepotActionStringIndex(const Order *order)
    00170 {
    00171   if (order->GetDepotActionType() & ODATFB_HALT) {
    00172     return DA_STOP;
    00173   } else if (order->GetDepotOrderType() & ODTFB_SERVICE) {
    00174     return DA_SERVICE;
    00175   } else {
    00176     return DA_ALWAYS_GO;
    00177   }
    00178 }
    00179
    00192 void DrawOrderString(const Vehicle *v, const Order *order, int order_index, int y, bool selected, bool timetable, int left, int middle, int right)
    00193 {
    00194   bool rtl = _current_text_dir == TD_RTL;
    00195
    00196   SpriteID sprite = rtl ? SPR_ARROW_LEFT : SPR_ARROW_RIGHT;
    00197   Dimension sprite_size = GetSpriteSize(sprite);
    00198   if (v->cur_real_order_index == order_index) {
    00199     DrawSprite(sprite, PAL_NONE, rtl ? right -     sprite_size.width : left,                     y + ((int)FONT_HEIGHT_NORMAL - (int)sprite_size.height) / 2);
    00200     DrawSprite(sprite, PAL_NONE, rtl ? right - 2 * sprite_size.width : left + sprite_size.width, y + ((int)FONT_HEIGHT_NORMAL - (int)sprite_size.height) / 2);
    00201   } else if (v->cur_auto_order_index == order_index) {
    00202     DrawSprite(sprite, PAL_NONE, rtl ? right -     sprite_size.width : left,                     y + ((int)FONT_HEIGHT_NORMAL - (int)sprite_size.height) / 2);
    00203   }
    00204
    00205   TextColour colour = TC_BLACK;
    00206   if (order->IsType(OT_AUTOMATIC)) {
    00207     colour = (selected ? TC_SILVER : TC_GREY) | TC_NO_SHADE;
    00208   } else if (selected) {
    00209     colour = TC_WHITE;
    00210   }
    00211
    00212   SetDParam(0, order_index + 1);
    00213   DrawString(left, rtl ? right - 2 * sprite_size.width - 3 : middle, y, STR_ORDER_INDEX, colour, SA_RIGHT | SA_FORCE);
    00214
    00215   SetDParam(5, STR_EMPTY);
    00216
    00217   switch (order->GetType()) {
    00218     case OT_DUMMY:
    00219       SetDParam(0, STR_INVALID_ORDER);
    00220       SetDParam(1, order->GetDestination());
    00221       break;
    00222
    00223     case OT_AUTOMATIC:
    00224       SetDParam(0, STR_ORDER_GO_TO_STATION);
    00225       SetDParam(1, STR_ORDER_GO_TO);
    00226       SetDParam(2, order->GetDestination());
    00227       SetDParam(3, timetable ? STR_EMPTY : STR_ORDER_AUTOMATIC);
    00228       break;
    00229
    00230     case OT_GOTO_STATION: {
    00231       OrderLoadFlags load = order->GetLoadType();
    00232       OrderUnloadFlags unload = order->GetUnloadType();
    00233
    00234       SetDParam(0, STR_ORDER_GO_TO_STATION);
    00235       SetDParam(1, STR_ORDER_GO_TO + (v->IsGroundVehicle() ? order->GetNonStopType() : 0));
    00236       SetDParam(2, order->GetDestination());
    00237
    00238       if (timetable) {
    00239         SetDParam(3, STR_EMPTY);
    00240
    00241         if (order->wait_time > 0) {
    00242           SetDParam(5, STR_TIMETABLE_STAY_FOR);
    00243           SetTimetableParams(6, 7, order->wait_time);
    00244         }
    00245       } else {
    00246         SetDParam(3, (order->GetNonStopType() & ONSF_NO_STOP_AT_DESTINATION_STATION) ? STR_EMPTY : _station_load_types[unload][load]);
    00247         if (v->type == VEH_TRAIN && (order->GetNonStopType() & ONSF_NO_STOP_AT_DESTINATION_STATION) == 0) {
    00248           SetDParam(5, order->GetStopLocation() + STR_ORDER_STOP_LOCATION_NEAR_END);
    00249         }
    00250       }
    00251       break;
    00252     }
    00253
    00254     case OT_GOTO_DEPOT:
    00255       if (order->GetDepotActionType() & ODATFB_NEAREST_DEPOT) {
    00256         SetDParam(0, STR_ORDER_GO_TO_NEAREST_DEPOT_FORMAT);
    00257         if (v->type == VEH_AIRCRAFT) {
    00258           SetDParam(2, STR_ORDER_NEAREST_HANGAR);
    00259           SetDParam(3, STR_EMPTY);
    00260         } else {
    00261           SetDParam(2, STR_ORDER_NEAREST_DEPOT);
    00262           SetDParam(3, STR_ORDER_TRAIN_DEPOT + v->type);
    00263         }
    00264       } else {
    00265         SetDParam(0, STR_ORDER_GO_TO_DEPOT_FORMAT);
    00266         SetDParam(2, v->type);
    00267         SetDParam(3, order->GetDestination());
    00268       }
    00269
    00270       if (order->GetDepotOrderType() & ODTFB_SERVICE) {
    00271         SetDParam(1, (order->GetNonStopType() & ONSF_NO_STOP_AT_INTERMEDIATE_STATIONS) ? STR_ORDER_SERVICE_NON_STOP_AT : STR_ORDER_SERVICE_AT);
    00272       } else {
    00273         SetDParam(1, (order->GetNonStopType() & ONSF_NO_STOP_AT_INTERMEDIATE_STATIONS) ? STR_ORDER_GO_NON_STOP_TO : STR_ORDER_GO_TO);
    00274       }
    00275
    00276       if (!timetable && (order->GetDepotActionType() & ODATFB_HALT)) {
    00277         SetDParam(5, STR_ORDER_STOP_ORDER);
    00278       }
    00279
    00280       if (!timetable && order->IsRefit()) {
    00281         SetDParam(5, (order->GetDepotActionType() & ODATFB_HALT) ? STR_ORDER_REFIT_STOP_ORDER : STR_ORDER_REFIT_ORDER);
    00282         SetDParam(6, CargoSpec::Get(order->GetRefitCargo())->name);
    00283       }
    00284       break;
    00285
    00286     case OT_GOTO_WAYPOINT:
    00287       SetDParam(0, (order->GetNonStopType() & ONSF_NO_STOP_AT_INTERMEDIATE_STATIONS) ? STR_ORDER_GO_NON_STOP_TO_WAYPOINT : STR_ORDER_GO_TO_WAYPOINT);
    00288       SetDParam(1, order->GetDestination());
    00289       break;
    00290
    00291     case OT_CONDITIONAL:
    00292       SetDParam(1, order->GetConditionSkipToOrder() + 1);
    00293       if (order->GetConditionVariable() == OCV_UNCONDITIONALLY) {
    00294         SetDParam(0, STR_ORDER_CONDITIONAL_UNCONDITIONAL);
    00295       } else {
    00296         OrderConditionComparator occ = order->GetConditionComparator();
    00297         SetDParam(0, (occ == OCC_IS_TRUE || occ == OCC_IS_FALSE) ? STR_ORDER_CONDITIONAL_TRUE_FALSE : STR_ORDER_CONDITIONAL_NUM);
    00298         SetDParam(2, STR_ORDER_CONDITIONAL_LOAD_PERCENTAGE + order->GetConditionVariable());
    00299         SetDParam(3, STR_ORDER_CONDITIONAL_COMPARATOR_EQUALS + occ);
    00300
    00301         uint value = order->GetConditionValue();
    00302         if (order->GetConditionVariable() == OCV_MAX_SPEED) value = ConvertSpeedToDisplaySpeed(value);
    00303         SetDParam(4, value);
    00304       }
    00305
    00306       if (timetable && order->wait_time > 0) {
    00307         SetDParam(5, STR_TIMETABLE_AND_TRAVEL_FOR);
    00308         SetTimetableParams(6, 7, order->wait_time);
    00309       } else {
    00310         SetDParam(5, STR_EMPTY);
    00311       }
    00312       break;
    00313
    00314     default: NOT_REACHED();
    00315   }
    00316
    00317   DrawString(rtl ? left : middle, rtl ? middle : right, y, STR_ORDER_TEXT, colour);
    00318 }
    00319
    00320
    00321 static Order GetOrderCmdFromTile(const Vehicle *v, TileIndex tile)
    00322 {
    00323   Order order;
    00324   order.next  = NULL;
    00325   order.index = 0;
    00326
    00327   /* check depot first */
    00328   switch (GetTileType(tile)) {
    00329     case MP_RAILWAY:
    00330       if (v->type == VEH_TRAIN && IsTileOwner(tile, _local_company)) {
    00331         if (IsRailDepot(tile)) {
    00332           order.MakeGoToDepot(GetDepotIndex(tile), ODTFB_PART_OF_ORDERS,
    00333               _settings_client.gui.new_nonstop ? ONSF_NO_STOP_AT_INTERMEDIATE_STATIONS : ONSF_STOP_EVERYWHERE);
    00334           if (_ctrl_pressed) order.SetDepotOrderType((OrderDepotTypeFlags)(order.GetDepotOrderType() ^ ODTFB_SERVICE));
    00335           return order;
    00336         }
    00337       }
    00338       break;
    00339
    00340     case MP_ROAD:
    00341       if (IsRoadDepot(tile) && v->type == VEH_ROAD && IsTileOwner(tile, _local_company)) {
    00342         order.MakeGoToDepot(GetDepotIndex(tile), ODTFB_PART_OF_ORDERS,
    00343             _settings_client.gui.new_nonstop ? ONSF_NO_STOP_AT_INTERMEDIATE_STATIONS : ONSF_STOP_EVERYWHERE);
    00344         if (_ctrl_pressed) order.SetDepotOrderType((OrderDepotTypeFlags)(order.GetDepotOrderType() ^ ODTFB_SERVICE));
    00345         return order;
    00346       }
    00347       break;
    00348
    00349     case MP_STATION:
    00350       if (v->type != VEH_AIRCRAFT) break;
    00351       if (IsHangar(tile) && IsTileOwner(tile, _local_company)) {
    00352         order.MakeGoToDepot(GetStationIndex(tile), ODTFB_PART_OF_ORDERS, ONSF_STOP_EVERYWHERE);
    00353         if (_ctrl_pressed) order.SetDepotOrderType((OrderDepotTypeFlags)(order.GetDepotOrderType() ^ ODTFB_SERVICE));
    00354         return order;
    00355       }
    00356       break;
    00357
    00358     case MP_WATER:
    00359       if (v->type != VEH_SHIP) break;
    00360       if (IsShipDepot(tile) && IsTileOwner(tile, _local_company)) {
    00361         order.MakeGoToDepot(GetDepotIndex(tile), ODTFB_PART_OF_ORDERS, ONSF_STOP_EVERYWHERE);
    00362         if (_ctrl_pressed) order.SetDepotOrderType((OrderDepotTypeFlags)(order.GetDepotOrderType() ^ ODTFB_SERVICE));
    00363         return order;
    00364       }
    00365       break;
    00366
    00367     default:
    00368       break;
    00369   }
    00370
    00371   /* check waypoint */
    00372   if (IsRailWaypointTile(tile) &&
    00373       v->type == VEH_TRAIN &&
    00374       IsTileOwner(tile, _local_company)) {
    00375     order.MakeGoToWaypoint(Waypoint::GetByTile(tile)->index);
    00376     if (_settings_client.gui.new_nonstop != _ctrl_pressed) order.SetNonStopType(ONSF_NO_STOP_AT_ANY_STATION);
    00377     return order;
    00378   }
    00379
    00380   if ((IsBuoyTile(tile) && v->type == VEH_SHIP) || (IsRailWaypointTile(tile) && v->type == VEH_TRAIN)) {
    00381     order.MakeGoToWaypoint(GetStationIndex(tile));
    00382     return order;
    00383   }
    00384
    00385   if (IsTileType(tile, MP_STATION)) {
    00386     StationID st_index = GetStationIndex(tile);
    00387     const Station *st = Station::Get(st_index);
    00388
    00389     if (st->owner == _local_company || st->owner == OWNER_NONE) {
    00390       byte facil;
    00391       (facil = FACIL_DOCK, v->type == VEH_SHIP) ||
    00392       (facil = FACIL_TRAIN, v->type == VEH_TRAIN) ||
    00393       (facil = FACIL_AIRPORT, v->type == VEH_AIRCRAFT) ||
    00394       (facil = FACIL_BUS_STOP, v->type == VEH_ROAD && RoadVehicle::From(v)->IsBus()) ||
    00395       (facil = FACIL_TRUCK_STOP, 1);
    00396       if (st->facilities & facil) {
    00397         order.MakeGoToStation(st_index);
    00398         if (_ctrl_pressed) order.SetLoadType(OLF_FULL_LOAD_ANY);
    00399         if (_settings_client.gui.new_nonstop && v->IsGroundVehicle()) order.SetNonStopType(ONSF_NO_STOP_AT_INTERMEDIATE_STATIONS);
    00400         order.SetStopLocation(v->type == VEH_TRAIN ? (OrderStopLocation)(_settings_client.gui.stop_location) : OSL_PLATFORM_FAR_END);
    00401         return order;
    00402       }
    00403     }
    00404   }
    00405
    00406   /* not found */
    00407   order.Free();
    00408   return order;
    00409 }
    00410
    00451 struct OrdersWindow : public Window {
    00452 private:
    00454   enum OrderPlaceObjectState {
    00455     OPOS_GOTO,
    00456     OPOS_CONDITIONAL,
    00457     OPOS_SHARE,
    00458   };
    00459
    00461   enum DisplayPane {
    00462     /* ORDER_WIDGET_SEL_TOP_LEFT */
    00463     DP_LEFT_NONSTOP    = 0,
    00464     DP_LEFT_CONDVAR    = 1,
    00465
    00466     /* ORDER_WIDGET_SEL_TOP_MIDDLE */
    00467     DP_MIDDLE_LOAD     = 0,
    00468     DP_MIDDLE_REFIT    = 1,
    00469     DP_MIDDLE_COMPARE  = 2,
    00470
    00471     /* ORDER_WIDGET_SEL_TOP_RIGHT */
    00472     DP_RIGHT_UNLOAD    = 0,
    00473     DP_RIGHT_SERVICE   = 1,
    00474     DP_RIGHT_CONDVAL   = 2,
    00475
    00476     /* ORDER_WIDGET_SEL_TOP_ROW */
    00477     DP_ROW_LOAD        = 0,
    00478     DP_ROW_DEPOT       = 1,
    00479     DP_ROW_CONDITIONAL = 2,
    00480
    00481     /* ORDER_WIDGET_SEL_BOTTOM_MIDDLE */
    00482     DP_BOTTOM_MIDDLE_DELETE       = 0,
    00483     DP_BOTTOM_MIDDLE_STOP_SHARING = 1,
    00484   };
    00485
    00486   int selected_order;
    00487   VehicleOrderID order_over;
    00488   OrderPlaceObjectState goto_type;
    00489   const Vehicle *vehicle;
    00490   Scrollbar *vscroll;
    00491
    00497   VehicleOrderID OrderGetSel() const
    00498   {
    00499     int num = this->selected_order;
    00500     return (num >= 0 && num < vehicle->GetNumOrders()) ? num : vehicle->GetNumOrders();
    00501   }
    00502
    00511   VehicleOrderID GetOrderFromPt(int y)
    00512   {
    00513     NWidgetBase *nwid = this->GetWidget<NWidgetBase>(ORDER_WIDGET_ORDER_LIST);
    00514     int sel = (y - nwid->pos_y - WD_FRAMERECT_TOP) / nwid->resize_y; // Selected line in the ORDER_WIDGET_ORDER_LIST panel.
    00515
    00516     if ((uint)sel >= this->vscroll->GetCapacity()) return INVALID_VEH_ORDER_ID;
    00517
    00518     sel += this->vscroll->GetPosition();
    00519
    00520     return (sel <= vehicle->GetNumOrders() && sel >= 0) ? sel : INVALID_VEH_ORDER_ID;
    00521   }
    00522
    00527   void OrderClick_Goto(int i)
    00528   {
    00529     this->SetWidgetDirty(ORDER_WIDGET_GOTO);
    00530     this->ToggleWidgetLoweredState(ORDER_WIDGET_GOTO);
    00531     if (this->IsWidgetLowered(ORDER_WIDGET_GOTO)) {
    00532       SetObjectToPlaceWnd(ANIMCURSOR_PICKSTATION, PAL_NONE, HT_RECT | HT_VEHICLE, this);
    00533       this->goto_type = OPOS_GOTO;
    00534     } else {
    00535       ResetObjectToPlace();
    00536     }
    00537   }
    00538
    00543   void OrderClick_FullLoad(int load_type)
    00544   {
    00545     VehicleOrderID sel_ord = this->OrderGetSel();
    00546     const Order *order = this->vehicle->GetOrder(sel_ord);
    00547
    00548     if (order == NULL || order->GetLoadType() == load_type) return;
    00549
    00550     if (load_type < 0) {
    00551       load_type = order->GetLoadType() == OLF_LOAD_IF_POSSIBLE ? OLF_FULL_LOAD_ANY : OLF_LOAD_IF_POSSIBLE;
    00552     }
    00553     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));
    00554   }
    00555
    00559   void OrderHotkey_NoLoad(int i)
    00560   {
    00561     this->OrderClick_FullLoad(OLFB_NO_LOAD);
    00562   }
    00563
    00567   void OrderClick_Service(int i)
    00568   {
    00569     VehicleOrderID sel_ord = this->OrderGetSel();
    00570
    00571     if (i < 0) {
    00572       const Order *order = this->vehicle->GetOrder(sel_ord);
    00573       if (order == NULL) return;
    00574       i = (order->GetDepotOrderType() & ODTFB_SERVICE) ? DA_ALWAYS_GO : DA_SERVICE;
    00575     }
    00576     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));
    00577   }
    00578
    00583   void OrderClick_NearestDepot(int i)
    00584   {
    00585     Order order;
    00586     order.next = NULL;
    00587     order.index = 0;
    00588     order.MakeGoToDepot(0, ODTFB_PART_OF_ORDERS,
    00589         _settings_client.gui.new_nonstop && this->vehicle->IsGroundVehicle() ? ONSF_NO_STOP_AT_INTERMEDIATE_STATIONS : ONSF_STOP_EVERYWHERE);
    00590     order.SetDepotActionType(ODATFB_NEAREST_DEPOT);
    00591
    00592     DoCommandP(this->vehicle->tile, this->vehicle->index + (this->OrderGetSel() << 20), order.Pack(), CMD_INSERT_ORDER | CMD_MSG(STR_ERROR_CAN_T_INSERT_NEW_ORDER));
    00593   }
    00594
    00599   void OrderClick_Conditional(int i)
    00600   {
    00601     this->LowerWidget(ORDER_WIDGET_GOTO);
    00602     this->SetWidgetDirty(ORDER_WIDGET_GOTO);
    00603     SetObjectToPlaceWnd(ANIMCURSOR_PICKSTATION, PAL_NONE, HT_NONE, this);
    00604     this->goto_type = OPOS_CONDITIONAL;
    00605   }
    00606
    00611   void OrderClick_Share(int i)
    00612   {
    00613     this->LowerWidget(ORDER_WIDGET_GOTO);
    00614     this->SetWidgetDirty(ORDER_WIDGET_GOTO);
    00615     SetObjectToPlaceWnd(ANIMCURSOR_PICKSTATION, PAL_NONE, HT_VEHICLE, this);
    00616     this->goto_type = OPOS_SHARE;
    00617   }
    00618
    00622   void OrderClick_Unload(int unload_type)
    00623   {
    00624     VehicleOrderID sel_ord = this->OrderGetSel();
    00625     const Order *order = this->vehicle->GetOrder(sel_ord);
    00626
    00627     if (order == NULL || order->GetUnloadType() == unload_type) return;
    00628
    00629     if (unload_type < 0) {
    00630       unload_type = order->GetUnloadType() == OUF_UNLOAD_IF_POSSIBLE ? OUFB_UNLOAD : OUF_UNLOAD_IF_POSSIBLE;
    00631     }
    00632
    00633     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));
    00634
    00635     /* Transfer orders with leave empty as default */
    00636     if (unload_type == OUFB_TRANSFER) {
    00637       DoCommandP(this->vehicle->tile, this->vehicle->index + (sel_ord << 20), MOF_LOAD | (OLFB_NO_LOAD << 4), CMD_MODIFY_ORDER);
    00638       this->SetWidgetDirty(ORDER_WIDGET_FULL_LOAD);
    00639     }
    00640   }
    00641
    00645   void OrderHotkey_Transfer(int i)
    00646   {
    00647     this->OrderClick_Unload(OUFB_TRANSFER);
    00648   }
    00649
    00653   void OrderHotkey_NoUnload(int i)
    00654   {
    00655     this->OrderClick_Unload(OUFB_NO_UNLOAD);
    00656   }
    00657
    00662   void OrderClick_Nonstop(int non_stop)
    00663   {
    00664     if (!this->vehicle->IsGroundVehicle()) return;
    00665
    00666     VehicleOrderID sel_ord = this->OrderGetSel();
    00667     const Order *order = this->vehicle->GetOrder(sel_ord);
    00668
    00669     if (order == NULL || order->GetNonStopType() == non_stop) return;
    00670
    00671     /* Keypress if negative, so 'toggle' to the next */
    00672     if (non_stop < 0) {
    00673       non_stop = order->GetNonStopType() ^ ONSF_NO_STOP_AT_INTERMEDIATE_STATIONS;
    00674     }
    00675
    00676     this->SetWidgetDirty(ORDER_WIDGET_NON_STOP);
    00677     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));
    00678   }
    00679
    00685   void OrderClick_Skip(int i)
    00686   {
    00687     /* Don't skip when there's nothing to skip */
    00688     if (_ctrl_pressed && this->vehicle->cur_auto_order_index == this->OrderGetSel()) return;
    00689     if (this->vehicle->GetNumOrders() <= 1) return;
    00690
    00691     DoCommandP(this->vehicle->tile, this->vehicle->index, _ctrl_pressed ? this->OrderGetSel() : ((this->vehicle->cur_auto_order_index + 1) % this->vehicle->GetNumOrders()),
    00692         CMD_SKIP_TO_ORDER | CMD_MSG(_ctrl_pressed ? STR_ERROR_CAN_T_SKIP_TO_ORDER : STR_ERROR_CAN_T_SKIP_ORDER));
    00693   }
    00694
    00699   void OrderClick_Delete(int i)
    00700   {
    00701     /* When networking, move one order lower */
    00702     int selected = this->selected_order + (int)_networking;
    00703
    00704     if (DoCommandP(this->vehicle->tile, this->vehicle->index, this->OrderGetSel(), CMD_DELETE_ORDER | CMD_MSG(STR_ERROR_CAN_T_DELETE_THIS_ORDER))) {
    00705       this->selected_order = selected >= this->vehicle->GetNumOrders() ? -1 : selected;
    00706       this->UpdateButtonState();
    00707     }
    00708   }
    00709
    00717   void OrderClick_StopSharing(int i)
    00718   {
    00719     /* Don't try to stop sharing orders if 'End of Shared Orders' isn't selected. */
    00720     if (!this->vehicle->IsOrderListShared() || this->selected_order != this->vehicle->GetNumOrders()) return;
    00721     /* If Ctrl is pressed, delete the order list as if we clicked the 'Delete' button. */
    00722     if (_ctrl_pressed) {
    00723       this->OrderClick_Delete(0);
    00724       return;
    00725     }
    00726
    00727     /* Get another vehicle that share orders with this vehicle. */
    00728     Vehicle *other_shared = (this->vehicle->FirstShared() == this->vehicle) ? this->vehicle->NextShared() : this->vehicle->PreviousShared();
    00729     /* Copy the order list of the other vehicle. */
    00730     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))) {
    00731       this->UpdateButtonState();
    00732     }
    00733   }
    00734
    00740   void OrderClick_Refit(int i)
    00741   {
    00742     if (_ctrl_pressed) {
    00743       /* Cancel refitting */
    00744       DoCommandP(this->vehicle->tile, this->vehicle->index, (this->OrderGetSel() << 16) | (CT_NO_REFIT << 8) | CT_NO_REFIT, CMD_ORDER_REFIT);
    00745     } else {
    00746       ShowVehicleRefitWindow(this->vehicle, this->OrderGetSel(), this);
    00747     }
    00748   }
    00749
    00750 public:
    00751   OrdersWindow(const WindowDesc *desc, const Vehicle *v) : Window()
    00752   {
    00753     this->vehicle = v;
    00754
    00755     this->CreateNestedTree(desc);
    00756     this->vscroll = this->GetScrollbar(ORDER_WIDGET_SCROLLBAR);
    00757     this->FinishInitNested(desc, v->index);
    00758
    00759     this->selected_order = -1;
    00760     this->order_over = INVALID_VEH_ORDER_ID;
    00761     this->owner = v->owner;
    00762
    00763     if (_settings_client.gui.quick_goto && v->owner == _local_company) {
    00764       /* If there are less than 2 station, make Go To active. */
    00765       int station_orders = 0;
    00766       const Order *order;
    00767       FOR_VEHICLE_ORDERS(v, order) {
    00768         if (order->IsType(OT_GOTO_STATION)) station_orders++;
    00769       }
    00770
    00771       if (station_orders < 2) this->OrderClick_Goto(0);
    00772     }
    00773     this->OnInvalidateData(-2);
    00774   }
    00775
    00776   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
    00777   {
    00778     switch (widget) {
    00779       case ORDER_WIDGET_ORDER_LIST:
    00780         resize->height = FONT_HEIGHT_NORMAL;
    00781         size->height = 6 * resize->height + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
    00782         break;
    00783
    00784       case ORDER_WIDGET_COND_VARIABLE: {
    00785         Dimension d = {0, 0};
    00786         for (int i = 0; _order_conditional_variable[i] != INVALID_STRING_ID; i++) {
    00787           d = maxdim(d, GetStringBoundingBox(_order_conditional_variable[i]));
    00788         }
    00789         d.width += padding.width;
    00790         d.height += padding.height;
    00791         *size = maxdim(*size, d);
    00792         break;
    00793       }
    00794
    00795       case ORDER_WIDGET_COND_COMPARATOR: {
    00796         Dimension d = {0, 0};
    00797         for (int i = 0; _order_conditional_condition[i] != INVALID_STRING_ID; i++) {
    00798           d = maxdim(d, GetStringBoundingBox(_order_conditional_condition[i]));
    00799         }
    00800         d.width += padding.width;
    00801         d.height += padding.height;
    00802         *size = maxdim(*size, d);
    00803         break;
    00804       }
    00805     }
    00806   }
    00807
    00813   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
    00814   {
    00815     VehicleOrderID from = INVALID_VEH_ORDER_ID;
    00816     VehicleOrderID to   = INVALID_VEH_ORDER_ID;
    00817
    00818     switch (data) {
    00819       case -666:
    00820         /* Autoreplace replaced the vehicle */
    00821         this->vehicle = Vehicle::Get(this->window_number);
    00822         break;
    00823
    00824       case -1:
    00825         /* Removed / replaced all orders (after deleting / sharing) */
    00826         if (this->selected_order == -1) break;
    00827
    00828         this->DeleteChildWindows();
    00829         HideDropDownMenu(this);
    00830         this->selected_order = -1;
    00831         break;
    00832
    00833       case -2:
    00834         /* Some other order changes */
    00835         break;
    00836
    00837       default:
    00838         if (data < 0) break;
    00839
    00840         if (gui_scope) break; // only do this once; from command scope
    00841         from = GB(data, 0, 8);
    00842         to   = GB(data, 8, 8);
    00843         /* Moving an order. If one of these is INVALID_VEH_ORDER_ID, then
    00844          * the order is being created / removed */
    00845         if (this->selected_order == -1) break;
    00846
    00847         if (from == to) break; // no need to change anything
    00848
    00849         if (from != this->selected_order) {
    00850           /* Moving from preceding order? */
    00851           this->selected_order -= (int)(from <= this->selected_order);
    00852           /* Moving to   preceding order? */
    00853           this->selected_order += (int)(to   <= this->selected_order);
    00854           break;
    00855         }
    00856
    00857         /* Now we are modifying the selected order */
    00858         if (to == INVALID_VEH_ORDER_ID) {
    00859           /* Deleting selected order */
    00860           this->DeleteChildWindows();
    00861           HideDropDownMenu(this);
    00862           this->selected_order = -1;
    00863           break;
    00864         }
    00865
    00866         /* Moving selected order */
    00867         this->selected_order = to;
    00868         break;
    00869     }
    00870
    00871     this->vscroll->SetCount(this->vehicle->GetNumOrders() + 1);
    00872     if (gui_scope) this->UpdateButtonState();
    00873
    00874     /* Scroll to the new order. */
    00875     if (from == INVALID_VEH_ORDER_ID && to != INVALID_VEH_ORDER_ID && !this->vscroll->IsVisible(to)) {
    00876       this->vscroll->ScrollTowards(to);
    00877     }
    00878   }
    00879
    00880   void UpdateButtonState()
    00881   {
    00882     if (this->vehicle->owner != _local_company) return; // No buttons are displayed with competitor order windows.
    00883
    00884     bool shared_orders = this->vehicle->IsOrderListShared();
    00885     VehicleOrderID sel = this->OrderGetSel();
    00886     const Order *order = this->vehicle->GetOrder(sel);
    00887
    00888     /* Second row. */
    00889     /* skip */
    00890     this->SetWidgetDisabledState(ORDER_WIDGET_SKIP, this->vehicle->GetNumOrders() <= 1);
    00891
    00892     /* delete / stop sharing */
    00893     NWidgetStacked *delete_sel = this->GetWidget<NWidgetStacked>(ORDER_WIDGET_SEL_BOTTOM_MIDDLE);
    00894     if (shared_orders && this->selected_order == this->vehicle->GetNumOrders()) {
    00895       /* The 'End of Shared Orders' order is selected, show the 'stop sharing' button. */
    00896       delete_sel->SetDisplayedPlane(DP_BOTTOM_MIDDLE_STOP_SHARING);
    00897     } else {
    00898       /* The 'End of Shared Orders' order isn't selected, show the 'delete' button. */
    00899       delete_sel->SetDisplayedPlane(DP_BOTTOM_MIDDLE_DELETE);
    00900       this->SetWidgetDisabledState(ORDER_WIDGET_DELETE,
    00901         (uint)this->vehicle->GetNumOrders() + ((shared_orders || this->vehicle->GetNumOrders() != 0) ? 1 : 0) <= (uint)this->selected_order);
    00902
    00903       /* Set the tooltip of the 'delete' button depending on whether the
    00904        * 'End of Orders' order or a regular order is selected. */
    00905       NWidgetCore *nwi = this->GetWidget<NWidgetCore>(ORDER_WIDGET_DELETE);
    00906       if (this->selected_order == this->vehicle->GetNumOrders()) {
    00907         nwi->SetDataTip(STR_ORDERS_DELETE_BUTTON, STR_ORDERS_DELETE_ALL_TOOLTIP);
    00908       } else {
    00909         nwi->SetDataTip(STR_ORDERS_DELETE_BUTTON, STR_ORDERS_DELETE_TOOLTIP);
    00910       }
    00911     }
    00912
    00913     /* First row. */
    00914     this->RaiseWidget(ORDER_WIDGET_FULL_LOAD);
    00915     this->RaiseWidget(ORDER_WIDGET_UNLOAD);
    00916     this->RaiseWidget(ORDER_WIDGET_SERVICE);
    00917
    00918     /* Selection widgets. */
    00919     /* Train or road vehicle. */
    00920     NWidgetStacked *left_sel   = this->GetWidget<NWidgetStacked>(ORDER_WIDGET_SEL_TOP_LEFT);
    00921     NWidgetStacked *middle_sel = this->GetWidget<NWidgetStacked>(ORDER_WIDGET_SEL_TOP_MIDDLE);
    00922     NWidgetStacked *right_sel  = this->GetWidget<NWidgetStacked>(ORDER_WIDGET_SEL_TOP_RIGHT);
    00923     /* Ship or airplane. */
    00924     NWidgetStacked *row_sel = this->GetWidget<NWidgetStacked>(ORDER_WIDGET_SEL_TOP_ROW);
    00925     assert(row_sel != NULL || (left_sel != NULL && middle_sel != NULL && right_sel != NULL));
    00926
    00927
    00928     if (order == NULL) {
    00929       if (row_sel != NULL) {
    00930         row_sel->SetDisplayedPlane(DP_ROW_LOAD);
    00931       } else {
    00932         left_sel->SetDisplayedPlane(DP_LEFT_NONSTOP);
    00933         middle_sel->SetDisplayedPlane(DP_MIDDLE_LOAD);
    00934         right_sel->SetDisplayedPlane(DP_RIGHT_UNLOAD);
    00935         this->DisableWidget(ORDER_WIDGET_NON_STOP);
    00936         this->RaiseWidget(ORDER_WIDGET_NON_STOP);
    00937       }
    00938       this->DisableWidget(ORDER_WIDGET_FULL_LOAD);
    00939       this->DisableWidget(ORDER_WIDGET_UNLOAD);
    00940     } else {
    00941       this->SetWidgetDisabledState(ORDER_WIDGET_FULL_LOAD, (order->GetNonStopType() & ONSF_NO_STOP_AT_DESTINATION_STATION) != 0); // full load
    00942       this->SetWidgetDisabledState(ORDER_WIDGET_UNLOAD,    (order->GetNonStopType() & ONSF_NO_STOP_AT_DESTINATION_STATION) != 0); // unload
    00943
    00944       switch (order->GetType()) {
    00945         case OT_GOTO_STATION:
    00946           if (row_sel != NULL) {
    00947             row_sel->SetDisplayedPlane(DP_ROW_LOAD);
    00948           } else {
    00949             left_sel->SetDisplayedPlane(DP_LEFT_NONSTOP);
    00950             middle_sel->SetDisplayedPlane(DP_MIDDLE_LOAD);
    00951             right_sel->SetDisplayedPlane(DP_RIGHT_UNLOAD);
    00952             this->EnableWidget(ORDER_WIDGET_NON_STOP);
    00953             this->SetWidgetLoweredState(ORDER_WIDGET_NON_STOP, order->GetNonStopType() & ONSF_NO_STOP_AT_INTERMEDIATE_STATIONS);
    00954           }
    00955           this->SetWidgetLoweredState(ORDER_WIDGET_FULL_LOAD, order->GetLoadType() == OLF_FULL_LOAD_ANY);
    00956           this->SetWidgetLoweredState(ORDER_WIDGET_UNLOAD, order->GetUnloadType() == OUFB_UNLOAD);
    00957           break;
    00958
    00959         case OT_GOTO_WAYPOINT:
    00960           if (row_sel != NULL) {
    00961             row_sel->SetDisplayedPlane(DP_ROW_LOAD);
    00962           } else {
    00963             left_sel->SetDisplayedPlane(DP_LEFT_NONSTOP);
    00964             middle_sel->SetDisplayedPlane(DP_MIDDLE_LOAD);
    00965             right_sel->SetDisplayedPlane(DP_RIGHT_UNLOAD);
    00966             this->EnableWidget(ORDER_WIDGET_NON_STOP);
    00967             this->SetWidgetLoweredState(ORDER_WIDGET_NON_STOP, order->GetNonStopType() & ONSF_NO_STOP_AT_INTERMEDIATE_STATIONS);
    00968           }
    00969           this->DisableWidget(ORDER_WIDGET_FULL_LOAD);
    00970           this->DisableWidget(ORDER_WIDGET_UNLOAD);
    00971           break;
    00972
    00973         case OT_GOTO_DEPOT:
    00974           if (row_sel != NULL) {
    00975             row_sel->SetDisplayedPlane(DP_ROW_DEPOT);
    00976           } else {
    00977             left_sel->SetDisplayedPlane(DP_LEFT_NONSTOP);
    00978             middle_sel->SetDisplayedPlane(DP_MIDDLE_REFIT);
    00979             right_sel->SetDisplayedPlane(DP_RIGHT_SERVICE);
    00980             this->EnableWidget(ORDER_WIDGET_NON_STOP);
    00981             this->SetWidgetLoweredState(ORDER_WIDGET_NON_STOP, order->GetNonStopType() & ONSF_NO_STOP_AT_INTERMEDIATE_STATIONS);
    00982           }
    00983           this->SetWidgetLoweredState(ORDER_WIDGET_SERVICE, order->GetDepotOrderType() & ODTFB_SERVICE);
    00984           break;
    00985
    00986         case OT_CONDITIONAL: {
    00987           if (row_sel != NULL) {
    00988             row_sel->SetDisplayedPlane(DP_ROW_CONDITIONAL);
    00989           } else {
    00990             left_sel->SetDisplayedPlane(DP_LEFT_CONDVAR);
    00991             middle_sel->SetDisplayedPlane(DP_MIDDLE_COMPARE);
    00992             right_sel->SetDisplayedPlane(DP_RIGHT_CONDVAL);
    00993           }
    00994           OrderConditionVariable ocv = order->GetConditionVariable();
    00995           /* Set the strings for the dropdown boxes. */
    00996           this->GetWidget<NWidgetCore>(ORDER_WIDGET_COND_VARIABLE)->widget_data   = _order_conditional_variable[order == NULL ? 0 : ocv];
    00997           this->GetWidget<NWidgetCore>(ORDER_WIDGET_COND_COMPARATOR)->widget_data = _order_conditional_condition[order == NULL ? 0 : order->GetConditionComparator()];
    00998           this->SetWidgetDisabledState(ORDER_WIDGET_COND_COMPARATOR, ocv == OCV_UNCONDITIONALLY);
    00999           this->SetWidgetDisabledState(ORDER_WIDGET_COND_VALUE, ocv == OCV_REQUIRES_SERVICE || ocv == OCV_UNCONDITIONALLY);
    01000           break;
    01001         }
    01002
    01003         default: // every other order
    01004           if (row_sel != NULL) {
    01005             row_sel->SetDisplayedPlane(DP_ROW_LOAD);
    01006           } else {
    01007             left_sel->SetDisplayedPlane(DP_LEFT_NONSTOP);
    01008             middle_sel->SetDisplayedPlane(DP_MIDDLE_LOAD);
    01009             right_sel->SetDisplayedPlane(DP_RIGHT_UNLOAD);
    01010             this->DisableWidget(ORDER_WIDGET_NON_STOP);
    01011           }
    01012           this->DisableWidget(ORDER_WIDGET_FULL_LOAD);
    01013           this->DisableWidget(ORDER_WIDGET_UNLOAD);
    01014           break;
    01015       }
    01016     }
    01017
    01018     /* Disable list of vehicles with the same shared orders if there is no list */
    01019     this->SetWidgetDisabledState(ORDER_WIDGET_SHARED_ORDER_LIST, !shared_orders);
    01020
    01021     this->SetDirty();
    01022   }
    01023
    01024   virtual void OnPaint()
    01025   {
    01026     if (this->vehicle->owner != _local_company) this->selected_order = -1; // Disable selection any selected row at a competitor order window.
    01027     this->DrawWidgets();
    01028   }
    01029
    01030   virtual void DrawWidget(const Rect &r, int widget) const
    01031   {
    01032     if (widget != ORDER_WIDGET_ORDER_LIST) return;
    01033
    01034     bool rtl = _current_text_dir == TD_RTL;
    01035     SetDParam(0, 99);
    01036     int index_column_width = GetStringBoundingBox(STR_ORDER_INDEX).width + 2 * GetSpriteSize(rtl ? SPR_ARROW_RIGHT : SPR_ARROW_LEFT).width + 3;
    01037     int middle = rtl ? r.right - WD_FRAMETEXT_RIGHT - index_column_width : r.left + WD_FRAMETEXT_LEFT + index_column_width;
    01038
    01039     int y = r.top + WD_FRAMERECT_TOP;
    01040     int line_height = this->GetWidget<NWidgetBase>(ORDER_WIDGET_ORDER_LIST)->resize_y;
    01041
    01042     int i = this->vscroll->GetPosition();
    01043     const Order *order = this->vehicle->GetOrder(i);
    01044     /* First draw the highlighting underground if it exists. */
    01045     if (this->order_over != INVALID_VEH_ORDER_ID) {
    01046       while (order != NULL) {
    01047         /* Don't draw anything if it extends past the end of the window. */
    01048         if (!this->vscroll->IsVisible(i)) break;
    01049
    01050         if (i != this->selected_order && i == this->order_over) {
    01051           /* Highlight dragged order destination. */
    01052           int top = (this->order_over < this->selected_order ? y : y + line_height) - WD_FRAMERECT_TOP;
    01053           int bottom = min(top + 2, r.bottom - WD_FRAMERECT_BOTTOM);
    01054           top = max(top - 3, r.top + WD_FRAMERECT_TOP);
    01055           GfxFillRect(r.left + WD_FRAMETEXT_LEFT, top, r.right - WD_FRAMETEXT_RIGHT, bottom, _colour_gradient[COLOUR_GREY][7]);
    01056           break;
    01057         }
    01058         y += line_height;
    01059
    01060         i++;
    01061         order = order->next;
    01062       }
    01063
    01064       /* Reset counters for drawing the orders. */
    01065       y = r.top + WD_FRAMERECT_TOP;
    01066       i = this->vscroll->GetPosition();
    01067       order = this->vehicle->GetOrder(i);
    01068     }
    01069
    01070     /* Draw the orders. */
    01071     while (order != NULL) {
    01072       /* Don't draw anything if it extends past the end of the window. */
    01073       if (!this->vscroll->IsVisible(i)) break;
    01074
    01075       DrawOrderString(this->vehicle, order, i, y, i == this->selected_order, false, r.left + WD_FRAMETEXT_LEFT, middle, r.right - WD_FRAMETEXT_RIGHT);
    01076       y += line_height;
    01077
    01078       i++;
    01079       order = order->next;
    01080     }
    01081
    01082     if (this->vscroll->IsVisible(i)) {
    01083       StringID str = this->vehicle->IsOrderListShared() ? STR_ORDERS_END_OF_SHARED_ORDERS : STR_ORDERS_END_OF_ORDERS;
    01084       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);
    01085     }
    01086   }
    01087
    01088   virtual void SetStringParameters(int widget) const
    01089   {
    01090     switch (widget) {
    01091       case ORDER_WIDGET_COND_VALUE: {
    01092         VehicleOrderID sel = this->OrderGetSel();
    01093         const Order *order = this->vehicle->GetOrder(sel);
    01094
    01095         if (order != NULL && order->IsType(OT_CONDITIONAL)) {
    01096           uint value = order->GetConditionValue();
    01097           if (order->GetConditionVariable() == OCV_MAX_SPEED) value = ConvertSpeedToDisplaySpeed(value);
    01098           SetDParam(0, value);
    01099         }
    01100         break;
    01101       }
    01102
    01103       case ORDER_WIDGET_CAPTION:
    01104         SetDParam(0, this->vehicle->index);
    01105         break;
    01106     }
    01107   }
    01108
    01109   virtual void OnClick(Point pt, int widget, int click_count)
    01110   {
    01111     switch (widget) {
    01112       case ORDER_WIDGET_ORDER_LIST: {
    01113         if (this->goto_type == OPOS_CONDITIONAL) {
    01114           this->goto_type = OPOS_GOTO;
    01115           VehicleOrderID order_id = this->GetOrderFromPt(_cursor.pos.y - this->top);
    01116           if (order_id != INVALID_VEH_ORDER_ID) {
    01117             Order order;
    01118             order.next = NULL;
    01119             order.index = 0;
    01120             order.MakeConditional(order_id);
    01121
    01122             DoCommandP(this->vehicle->tile, this->vehicle->index + (this->OrderGetSel() << 20), order.Pack(), CMD_INSERT_ORDER | CMD_MSG(STR_ERROR_CAN_T_INSERT_NEW_ORDER));
    01123           }
    01124           ResetObjectToPlace();
    01125           break;
    01126         }
    01127
    01128         VehicleOrderID sel = this->GetOrderFromPt(pt.y);
    01129
    01130         if (_ctrl_pressed && sel < this->vehicle->GetNumOrders()) {
    01131           TileIndex xy = this->vehicle->GetOrder(sel)->GetLocation(this->vehicle);
    01132           if (xy != INVALID_TILE) ScrollMainWindowToTile(xy);
    01133           return;
    01134         }
    01135
    01136         /* This order won't be selected any more, close all child windows and dropdowns */
    01137         this->DeleteChildWindows();
    01138         HideDropDownMenu(this);
    01139
    01140         if (sel == INVALID_VEH_ORDER_ID || this->vehicle->owner != _local_company) {
    01141           /* Deselect clicked order */
    01142           this->selected_order = -1;
    01143         } else if (sel == this->selected_order) {
    01144           if (this->vehicle->type == VEH_TRAIN && sel < this->vehicle->GetNumOrders()) {
    01145             DoCommandP(this->vehicle->tile, this->vehicle->index + (sel << 20),
    01146                 MOF_STOP_LOCATION | ((this->vehicle->GetOrder(sel)->GetStopLocation() + 1) % OSL_END) << 4,
    01147                 CMD_MODIFY_ORDER | CMD_MSG(STR_ERROR_CAN_T_MODIFY_THIS_ORDER));
    01148           }
    01149         } else {
    01150           /* Select clicked order */
    01151           this->selected_order = sel;
    01152
    01153           if (this->vehicle->owner == _local_company) {
    01154             /* Activate drag and drop */
    01155             SetObjectToPlaceWnd(SPR_CURSOR_MOUSE, PAL_NONE, HT_DRAG, this);
    01156           }
    01157         }
    01158
    01159         this->UpdateButtonState();
    01160         break;
    01161       }
    01162
    01163       case ORDER_WIDGET_SKIP:
    01164         this->OrderClick_Skip(0);
    01165         break;
    01166
    01167       case ORDER_WIDGET_DELETE:
    01168         this->OrderClick_Delete(0);
    01169         break;
    01170
    01171       case ORDER_WIDGET_STOP_SHARING:
    01172         this->OrderClick_StopSharing(0);
    01173         break;
    01174
    01175       case ORDER_WIDGET_NON_STOP:
    01176         if (this->GetWidget<NWidgetLeaf>(widget)->ButtonHit(pt)) {
    01177           this->OrderClick_Nonstop(-1);
    01178         } else {
    01179           const Order *o = this->vehicle->GetOrder(this->OrderGetSel());
    01180           ShowDropDownMenu(this, _order_non_stop_drowdown, o->GetNonStopType(), ORDER_WIDGET_NON_STOP, 0,
    01181                           o->IsType(OT_GOTO_STATION) ? 0 : (o->IsType(OT_GOTO_WAYPOINT) ? 3 : 12));
    01182         }
    01183         break;
    01184
    01185       case ORDER_WIDGET_GOTO:
    01186         if (this->GetWidget<NWidgetLeaf>(widget)->ButtonHit(pt)) {
    01187           this->OrderClick_Goto(0);
    01188         } else {
    01189           ShowDropDownMenu(this, this->vehicle->type == VEH_AIRCRAFT ? _order_goto_dropdown_aircraft : _order_goto_dropdown, 0, ORDER_WIDGET_GOTO, 0, 0);
    01190         }
    01191         break;
    01192
    01193       case ORDER_WIDGET_FULL_LOAD:
    01194         if (this->GetWidget<NWidgetLeaf>(widget)->ButtonHit(pt)) {
    01195           this->OrderClick_FullLoad(-1);
    01196         } else {
    01197           ShowDropDownMenu(this, _order_full_load_drowdown, this->vehicle->GetOrder(this->OrderGetSel())->GetLoadType(), ORDER_WIDGET_FULL_LOAD, 0, 2);
    01198         }
    01199         break;
    01200
    01201       case ORDER_WIDGET_UNLOAD:
    01202         if (this->GetWidget<NWidgetLeaf>(widget)->ButtonHit(pt)) {
    01203           this->OrderClick_Unload(-1);
    01204         } else {
    01205           ShowDropDownMenu(this, _order_unload_drowdown, this->vehicle->GetOrder(this->OrderGetSel())->GetUnloadType(), ORDER_WIDGET_UNLOAD, 0, 8);
    01206         }
    01207         break;
    01208
    01209       case ORDER_WIDGET_REFIT:
    01210         this->OrderClick_Refit(0);
    01211         break;
    01212
    01213       case ORDER_WIDGET_SERVICE:
    01214         if (this->GetWidget<NWidgetLeaf>(widget)->ButtonHit(pt)) {
    01215           this->OrderClick_Service(-1);
    01216         } else {
    01217           ShowDropDownMenu(this, _order_depot_action_dropdown, DepotActionStringIndex(this->vehicle->GetOrder(this->OrderGetSel())), ORDER_WIDGET_SERVICE, 0, 0);
    01218         }
    01219         break;
    01220
    01221       case ORDER_WIDGET_TIMETABLE_VIEW:
    01222         ShowTimetableWindow(this->vehicle);
    01223         break;
    01224
    01225       case ORDER_WIDGET_COND_VARIABLE:
    01226         ShowDropDownMenu(this, _order_conditional_variable, this->vehicle->GetOrder(this->OrderGetSel())->GetConditionVariable(), ORDER_WIDGET_COND_VARIABLE, 0, 0);
    01227         break;
    01228
    01229       case ORDER_WIDGET_COND_COMPARATOR: {
    01230         const Order *o = this->vehicle->GetOrder(this->OrderGetSel());
    01231         ShowDropDownMenu(this, _order_conditional_condition, o->GetConditionComparator(), ORDER_WIDGET_COND_COMPARATOR, 0, (o->GetConditionVariable() == OCV_REQUIRES_SERVICE) ? 0x3F : 0xC0);
    01232         break;
    01233       }
    01234
    01235       case ORDER_WIDGET_COND_VALUE: {
    01236         const Order *order = this->vehicle->GetOrder(this->OrderGetSel());
    01237         uint value = order->GetConditionValue();
    01238         if (order->GetConditionVariable() == OCV_MAX_SPEED) value = ConvertSpeedToDisplaySpeed(value);
    01239         SetDParam(0, value);
    01240         ShowQueryString(STR_JUST_INT, STR_ORDER_CONDITIONAL_VALUE_CAPT, 5, 100, this, CS_NUMERAL, QSF_NONE);
    01241         break;
    01242       }
    01243
    01244       case ORDER_WIDGET_SHARED_ORDER_LIST:
    01245         ShowVehicleListWindow(this->vehicle);
    01246         break;
    01247     }
    01248   }
    01249
    01250   virtual void OnQueryTextFinished(char *str)
    01251   {
    01252     if (!StrEmpty(str)) {
    01253       VehicleOrderID sel = this->OrderGetSel();
    01254       uint value = atoi(str);
    01255
    01256       switch (this->vehicle->GetOrder(sel)->GetConditionVariable()) {
    01257         case OCV_MAX_SPEED:
    01258           value = ConvertDisplaySpeedToSpeed(value);
    01259           break;
    01260
    01261         case OCV_RELIABILITY:
    01262         case OCV_LOAD_PERCENTAGE:
    01263           value = Clamp(value, 0, 100);
    01264
    01265         default:
    01266           break;
    01267       }
    01268       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));
    01269     }
    01270   }
    01271
    01272   virtual void OnDropdownSelect(int widget, int index)
    01273   {
    01274     switch (widget) {
    01275       case ORDER_WIDGET_NON_STOP:
    01276         this->OrderClick_Nonstop(index);
    01277         break;
    01278
    01279       case ORDER_WIDGET_FULL_LOAD:
    01280         this->OrderClick_FullLoad(index);
    01281         break;
    01282
    01283       case ORDER_WIDGET_UNLOAD:
    01284         this->OrderClick_Unload(index);
    01285         break;
    01286
    01287       case ORDER_WIDGET_GOTO:
    01288         switch (index) {
    01289           case 0: this->OrderClick_Goto(0); break;
    01290           case 1: this->OrderClick_NearestDepot(0); break;
    01291           case 2: this->OrderClick_Conditional(0); break;
    01292           case 3: this->OrderClick_Share(0); break;
    01293           default: NOT_REACHED();
    01294         }
    01295         break;
    01296
    01297       case ORDER_WIDGET_SERVICE:
    01298         this->OrderClick_Service(index);
    01299         break;
    01300
    01301       case ORDER_WIDGET_COND_VARIABLE:
    01302         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));
    01303         break;
    01304
    01305       case ORDER_WIDGET_COND_COMPARATOR:
    01306         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));
    01307         break;
    01308     }
    01309   }
    01310
    01311   virtual void OnDragDrop(Point pt, int widget)
    01312   {
    01313     switch (widget) {
    01314       case ORDER_WIDGET_ORDER_LIST: {
    01315         VehicleOrderID from_order = this->OrderGetSel();
    01316         VehicleOrderID to_order = this->GetOrderFromPt(pt.y);
    01317
    01318         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()) &&
    01319             DoCommandP(this->vehicle->tile, this->vehicle->index, from_order | (to_order << 16), CMD_MOVE_ORDER | CMD_MSG(STR_ERROR_CAN_T_MOVE_THIS_ORDER))) {
    01320           this->selected_order = -1;
    01321           this->UpdateButtonState();
    01322         }
    01323         break;
    01324       }
    01325
    01326       case ORDER_WIDGET_DELETE:
    01327         this->OrderClick_Delete(0);
    01328         break;
    01329
    01330       case ORDER_WIDGET_STOP_SHARING:
    01331         this->OrderClick_StopSharing(0);
    01332         break;
    01333     }
    01334
    01335     ResetObjectToPlace();
    01336
    01337     if (this->order_over != INVALID_VEH_ORDER_ID) {
    01338       /* End of drag-and-drop, hide dragged order destination highlight. */
    01339       this->order_over = INVALID_VEH_ORDER_ID;
    01340       this->SetWidgetDirty(ORDER_WIDGET_ORDER_LIST);
    01341     }
    01342   }
    01343
    01344   virtual EventState OnKeyPress(uint16 key, uint16 keycode)
    01345   {
    01346     if (this->vehicle->owner != _local_company) return ES_NOT_HANDLED;
    01347
    01348     return CheckHotkeyMatch<OrdersWindow>(order_hotkeys, keycode, this) != -1 ? ES_HANDLED : ES_NOT_HANDLED;
    01349   }
    01350
    01351   virtual void OnPlaceObject(Point pt, TileIndex tile)
    01352   {
    01353     if (this->goto_type == OPOS_GOTO) {
    01354       const Order cmd = GetOrderCmdFromTile(this->vehicle, tile);
    01355       if (cmd.IsType(OT_NOTHING)) return;
    01356
    01357       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))) {
    01358         /* With quick goto the Go To button stays active */
    01359         if (!_settings_client.gui.quick_goto) ResetObjectToPlace();
    01360       }
    01361     }
    01362   }
    01363
    01364   virtual void OnVehicleSelect(const Vehicle *v)
    01365   {
    01366     /* v is vehicle getting orders. Only copy/clone orders if vehicle doesn't have any orders yet.
    01367      * We disallow copying orders of other vehicles if we already have at least one order entry
    01368      * ourself as it easily copies orders of vehicles within a station when we mean the station.
    01369      * Obviously if you press CTRL on a non-empty orders vehicle you know what you are doing
    01370      * TODO: give a warning message */
    01371     bool share_order = _ctrl_pressed || this->goto_type == OPOS_SHARE;
    01372     if (this->vehicle->GetNumOrders() != 0 && !share_order) return;
    01373
    01374     if (DoCommandP(this->vehicle->tile, this->vehicle->index | (share_order ? CO_SHARE : CO_COPY) << 30, v->index,
    01375         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))) {
    01376       this->selected_order = -1;
    01377       ResetObjectToPlace();
    01378     }
    01379   }
    01380
    01381   virtual void OnPlaceObjectAbort()
    01382   {
    01383     this->RaiseWidget(ORDER_WIDGET_GOTO);
    01384     this->SetWidgetDirty(ORDER_WIDGET_GOTO);
    01385
    01386     /* Remove drag highlighting if it exists. */
    01387     if (this->order_over != INVALID_VEH_ORDER_ID) {
    01388       this->order_over = INVALID_VEH_ORDER_ID;
    01389       this->SetWidgetDirty(ORDER_WIDGET_ORDER_LIST);
    01390     }
    01391   }
    01392
    01393   virtual void OnMouseDrag(Point pt, int widget)
    01394   {
    01395     if (this->selected_order != -1 && widget == ORDER_WIDGET_ORDER_LIST) {
    01396       /* An order is dragged.. */
    01397       VehicleOrderID from_order = this->OrderGetSel();
    01398       VehicleOrderID to_order = this->GetOrderFromPt(pt.y);
    01399       uint num_orders = this->vehicle->GetNumOrders();
    01400
    01401       if (from_order != INVALID_VEH_ORDER_ID && from_order <= num_orders) {
    01402         if (to_order != INVALID_VEH_ORDER_ID && to_order <= num_orders) { // ..over an existing order.
    01403           this->order_over = to_order;
    01404           this->SetWidgetDirty(widget);
    01405         } else if (from_order != to_order && this->order_over != INVALID_VEH_ORDER_ID) { // ..outside of the order list.
    01406           this->order_over = INVALID_VEH_ORDER_ID;
    01407           this->SetWidgetDirty(widget);
    01408         }
    01409       }
    01410     }
    01411   }
    01412
    01413   virtual void OnResize()
    01414   {
    01415     /* Update the scroll bar */
    01416     this->vscroll->SetCapacityFromWidget(this, ORDER_WIDGET_ORDER_LIST);
    01417   }
    01418
    01419   virtual void OnTimeout()
    01420   {
    01421     static const int raise_widgets[] = {
    01422       ORDER_WIDGET_TIMETABLE_VIEW, ORDER_WIDGET_SKIP, ORDER_WIDGET_DELETE, ORDER_WIDGET_STOP_SHARING, ORDER_WIDGET_REFIT, ORDER_WIDGET_SHARED_ORDER_LIST, WIDGET_LIST_END,
    01423     };
    01424
    01425     /* Unclick all buttons in raise_widgets[]. */
    01426     for (const int *widnum = raise_widgets; *widnum != WIDGET_LIST_END; widnum++) {
    01427       if (this->IsWidgetLowered(*widnum)) {
    01428         this->RaiseWidget(*widnum);
    01429         this->SetWidgetDirty(*widnum);
    01430       }
    01431     }
    01432   }
    01433
    01434   static Hotkey<OrdersWindow> order_hotkeys[];
    01435 };
    01436
    01437 Hotkey<OrdersWindow> OrdersWindow::order_hotkeys[] = {
    01438   Hotkey<OrdersWindow>('D', "skip", 0, &OrdersWindow::OrderClick_Skip),
    01439   Hotkey<OrdersWindow>('F', "delete", 0, &OrdersWindow::OrderClick_Delete),
    01440   Hotkey<OrdersWindow>('G', "goto", 0, &OrdersWindow::OrderClick_Goto),
    01441   Hotkey<OrdersWindow>('H', "nonstop", 0, &OrdersWindow::OrderClick_Nonstop),
    01442   Hotkey<OrdersWindow>('J', "fullload", 0, &OrdersWindow::OrderClick_FullLoad),
    01443   Hotkey<OrdersWindow>('K', "unload", 0, &OrdersWindow::OrderClick_Unload),
    01444   Hotkey<OrdersWindow>((uint16)0, "nearest_depot", 0, &OrdersWindow::OrderClick_NearestDepot),
    01445   Hotkey<OrdersWindow>((uint16)0, "always_service", 0, &OrdersWindow::OrderClick_Service),
    01446   Hotkey<OrdersWindow>((uint16)0, "force_unload", 0, &OrdersWindow::OrderClick_Unload),
    01447   Hotkey<OrdersWindow>((uint16)0, "transfer", 0, &OrdersWindow::OrderHotkey_Transfer),
    01448   Hotkey<OrdersWindow>((uint16)0, "no_unload", 0, &OrdersWindow::OrderHotkey_NoUnload),
    01449   Hotkey<OrdersWindow>((uint16)0, "no_load", 0, &OrdersWindow::OrderHotkey_NoLoad),
    01450   HOTKEY_LIST_END(OrdersWindow)
    01451 };
    01452 Hotkey<OrdersWindow> *_order_hotkeys = OrdersWindow::order_hotkeys;
    01453
    01455 static const NWidgetPart _nested_orders_train_widgets[] = {
    01456   NWidget(NWID_HORIZONTAL),
    01457     NWidget(WWT_CLOSEBOX, COLOUR_GREY),
    01458     NWidget(WWT_CAPTION, COLOUR_GREY, ORDER_WIDGET_CAPTION), SetDataTip(STR_ORDERS_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    01459     NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, ORDER_WIDGET_TIMETABLE_VIEW), SetMinimalSize(61, 14), SetDataTip(STR_ORDERS_TIMETABLE_VIEW, STR_ORDERS_TIMETABLE_VIEW_TOOLTIP),
    01460     NWidget(WWT_SHADEBOX, COLOUR_GREY),
    01461     NWidget(WWT_STICKYBOX, COLOUR_GREY),
    01462   EndContainer(),
    01463   NWidget(NWID_HORIZONTAL),
    01464     NWidget(WWT_PANEL, COLOUR_GREY, ORDER_WIDGET_ORDER_LIST), SetMinimalSize(372, 62), SetDataTip(0x0, STR_ORDERS_LIST_TOOLTIP), SetResize(1, 1), SetScrollbar(ORDER_WIDGET_SCROLLBAR), EndContainer(),
    01465     NWidget(NWID_VSCROLLBAR, COLOUR_GREY, ORDER_WIDGET_SCROLLBAR),
    01466   EndContainer(),
    01467
    01468   /* First button row. */
    01469   NWidget(NWID_HORIZONTAL),
    01470     NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
    01471       NWidget(NWID_SELECTION, INVALID_COLOUR, ORDER_WIDGET_SEL_TOP_LEFT),
    01472         NWidget(NWID_BUTTON_DROPDOWN, COLOUR_GREY, ORDER_WIDGET_NON_STOP), SetMinimalSize(124, 12), SetFill(1, 0),
    01473                             SetDataTip(STR_ORDER_NON_STOP, STR_ORDER_TOOLTIP_NON_STOP), SetResize(1, 0),
    01474         NWidget(WWT_DROPDOWN, COLOUR_GREY, ORDER_WIDGET_COND_VARIABLE), SetMinimalSize(124, 12), SetFill(1, 0),
    01475                             SetDataTip(STR_NULL, STR_ORDER_CONDITIONAL_VARIABLE_TOOLTIP), SetResize(1, 0),
    01476       EndContainer(),
    01477       NWidget(NWID_SELECTION, INVALID_COLOUR, ORDER_WIDGET_SEL_TOP_MIDDLE),
    01478         NWidget(NWID_BUTTON_DROPDOWN, COLOUR_GREY, ORDER_WIDGET_FULL_LOAD), SetMinimalSize(124, 12), SetFill(1, 0),
    01479                             SetDataTip(STR_ORDER_TOGGLE_FULL_LOAD, STR_ORDER_TOOLTIP_FULL_LOAD), SetResize(1, 0),
    01480         NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, ORDER_WIDGET_REFIT), SetMinimalSize(124, 12), SetFill(1, 0),
    01481                             SetDataTip(STR_ORDER_REFIT, STR_ORDER_REFIT_TOOLTIP), SetResize(1, 0),
    01482         NWidget(WWT_DROPDOWN, COLOUR_GREY, ORDER_WIDGET_COND_COMPARATOR), SetMinimalSize(124, 12), SetFill(1, 0),
    01483                             SetDataTip(STR_NULL, STR_ORDER_CONDITIONAL_COMPARATOR_TOOLTIP), SetResize(1, 0),
    01484       EndContainer(),
    01485       NWidget(NWID_SELECTION, INVALID_COLOUR, ORDER_WIDGET_SEL_TOP_RIGHT),
    01486         NWidget(NWID_BUTTON_DROPDOWN, COLOUR_GREY, ORDER_WIDGET_UNLOAD), SetMinimalSize(124, 12), SetFill(1, 0),
    01487                             SetDataTip(STR_ORDER_TOGGLE_UNLOAD, STR_ORDER_TOOLTIP_UNLOAD), SetResize(1, 0),
    01488         NWidget(NWID_BUTTON_DROPDOWN, COLOUR_GREY, ORDER_WIDGET_SERVICE), SetMinimalSize(124, 12), SetFill(1, 0),
    01489                             SetDataTip(STR_ORDER_SERVICE, STR_ORDER_SERVICE_TOOLTIP), SetResize(1, 0),
    01490         NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, ORDER_WIDGET_COND_VALUE), SetMinimalSize(124, 12), SetFill(1, 0),
    01491                             SetDataTip(STR_BLACK_COMMA, STR_ORDER_CONDITIONAL_VALUE_TOOLTIP), SetResize(1, 0),
    01492       EndContainer(),
    01493     EndContainer(),
    01494     NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, ORDER_WIDGET_SHARED_ORDER_LIST), SetMinimalSize(12, 12), SetDataTip(SPR_SHARED_ORDERS_ICON, STR_ORDERS_VEH_WITH_SHARED_ORDERS_LIST_TOOLTIP),
    01495   EndContainer(),
    01496
    01497   /* Second button row. */
    01498   NWidget(NWID_HORIZONTAL),
    01499     NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
    01500       NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, ORDER_WIDGET_SKIP), SetMinimalSize(124, 12), SetFill(1, 0),
    01501                           SetDataTip(STR_ORDERS_SKIP_BUTTON, STR_ORDERS_SKIP_TOOLTIP), SetResize(1, 0),
    01502       NWidget(NWID_SELECTION, INVALID_COLOUR, ORDER_WIDGET_SEL_BOTTOM_MIDDLE),
    01503         NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, ORDER_WIDGET_DELETE), SetMinimalSize(124, 12), SetFill(1, 0),
    01504                             SetDataTip(STR_ORDERS_DELETE_BUTTON, STR_ORDERS_DELETE_TOOLTIP), SetResize(1, 0),
    01505         NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, ORDER_WIDGET_STOP_SHARING), SetMinimalSize(124, 12), SetFill(1, 0),
    01506                             SetDataTip(STR_ORDERS_STOP_SHARING_BUTTON, STR_ORDERS_STOP_SHARING_TOOLTIP), SetResize(1, 0),
    01507       EndContainer(),
    01508       NWidget(NWID_BUTTON_DROPDOWN, COLOUR_GREY, ORDER_WIDGET_GOTO), SetMinimalSize(124, 12), SetFill(1, 0),
    01509                           SetDataTip(STR_ORDERS_GO_TO_BUTTON, STR_ORDERS_GO_TO_TOOLTIP), SetResize(1, 0),
    01510     EndContainer(),
    01511     NWidget(WWT_RESIZEBOX, COLOUR_GREY),
    01512   EndContainer(),
    01513 };
    01514
    01515 static const WindowDesc _orders_train_desc(
    01516   WDP_AUTO, 384, 100,
    01517   WC_VEHICLE_ORDERS, WC_VEHICLE_VIEW,
    01518   WDF_CONSTRUCTION,
    01519   _nested_orders_train_widgets, lengthof(_nested_orders_train_widgets)
    01520 );
    01521
    01523 static const NWidgetPart _nested_orders_widgets[] = {
    01524   NWidget(NWID_HORIZONTAL),
    01525     NWidget(WWT_CLOSEBOX, COLOUR_GREY),
    01526     NWidget(WWT_CAPTION, COLOUR_GREY, ORDER_WIDGET_CAPTION), SetDataTip(STR_ORDERS_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    01527     NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, ORDER_WIDGET_TIMETABLE_VIEW), SetMinimalSize(61, 14), SetDataTip(STR_ORDERS_TIMETABLE_VIEW, STR_ORDERS_TIMETABLE_VIEW_TOOLTIP),
    01528     NWidget(WWT_SHADEBOX, COLOUR_GREY),
    01529     NWidget(WWT_STICKYBOX, COLOUR_GREY),
    01530   EndContainer(),
    01531   NWidget(NWID_HORIZONTAL),
    01532     NWidget(WWT_PANEL, COLOUR_GREY, ORDER_WIDGET_ORDER_LIST), SetMinimalSize(372, 62), SetDataTip(0x0, STR_ORDERS_LIST_TOOLTIP), SetResize(1, 1), SetScrollbar(ORDER_WIDGET_SCROLLBAR), EndContainer(),
    01533     NWidget(NWID_VSCROLLBAR, COLOUR_GREY, ORDER_WIDGET_SCROLLBAR),
    01534   EndContainer(),
    01535
    01536   /* First button row. */
    01537   NWidget(NWID_HORIZONTAL),
    01538     NWidget(NWID_SELECTION, INVALID_COLOUR, ORDER_WIDGET_SEL_TOP_ROW),
    01539       /* load + unload buttons. */
    01540       NWidget(NWID_HORIZONTAL),
    01541         NWidget(NWID_BUTTON_DROPDOWN, COLOUR_GREY, ORDER_WIDGET_FULL_LOAD), SetMinimalSize(186, 12), SetFill(1, 0),
    01542                           SetDataTip(STR_ORDER_TOGGLE_FULL_LOAD, STR_ORDER_TOOLTIP_FULL_LOAD), SetResize(1, 0),
    01543         NWidget(NWID_BUTTON_DROPDOWN, COLOUR_GREY, ORDER_WIDGET_UNLOAD), SetMinimalSize(186, 12), SetFill(1, 0),
    01544                           SetDataTip(STR_ORDER_TOGGLE_UNLOAD, STR_ORDER_TOOLTIP_UNLOAD), SetResize(1, 0),
    01545       EndContainer(),
    01546       /* Refit + service buttons. */
    01547       NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
    01548         NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, ORDER_WIDGET_REFIT), SetMinimalSize(186, 12), SetFill(1, 0),
    01549                           SetDataTip(STR_ORDER_REFIT, STR_ORDER_REFIT_TOOLTIP), SetResize(1, 0),
    01550         NWidget(NWID_BUTTON_DROPDOWN, COLOUR_GREY, ORDER_WIDGET_SERVICE), SetMinimalSize(124, 12), SetFill(1, 0),
    01551                           SetDataTip(STR_ORDER_SERVICE, STR_ORDER_SERVICE_TOOLTIP), SetResize(1, 0),
    01552       EndContainer(),
    01553
    01554       /* Buttons for setting a condition. */
    01555       NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
    01556         NWidget(WWT_DROPDOWN, COLOUR_GREY, ORDER_WIDGET_COND_VARIABLE), SetMinimalSize(124, 12), SetFill(1, 0),
    01557                           SetDataTip(STR_NULL, STR_ORDER_CONDITIONAL_VARIABLE_TOOLTIP), SetResize(1, 0),
    01558         NWidget(WWT_DROPDOWN, COLOUR_GREY, ORDER_WIDGET_COND_COMPARATOR), SetMinimalSize(124, 12), SetFill(1, 0),
    01559                           SetDataTip(STR_NULL, STR_ORDER_CONDITIONAL_COMPARATOR_TOOLTIP), SetResize(1, 0),
    01560         NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, ORDER_WIDGET_COND_VALUE), SetMinimalSize(124, 12), SetFill(1, 0),
    01561                           SetDataTip(STR_BLACK_COMMA, STR_ORDER_CONDITIONAL_VALUE_TOOLTIP), SetResize(1, 0),
    01562       EndContainer(),
    01563     EndContainer(),
    01564
    01565     NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, ORDER_WIDGET_SHARED_ORDER_LIST), SetMinimalSize(12, 12), SetDataTip(SPR_SHARED_ORDERS_ICON, STR_ORDERS_VEH_WITH_SHARED_ORDERS_LIST_TOOLTIP),
    01566   EndContainer(),
    01567
    01568   /* Second button row. */
    01569   NWidget(NWID_HORIZONTAL),
    01570     NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, ORDER_WIDGET_SKIP), SetMinimalSize(124, 12), SetFill(1, 0),
    01571                       SetDataTip(STR_ORDERS_SKIP_BUTTON, STR_ORDERS_SKIP_TOOLTIP), SetResize(1, 0),
    01572     NWidget(NWID_SELECTION, INVALID_COLOUR, ORDER_WIDGET_SEL_BOTTOM_MIDDLE),
    01573       NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, ORDER_WIDGET_DELETE), SetMinimalSize(124, 12), SetFill(1, 0),
    01574                           SetDataTip(STR_ORDERS_DELETE_BUTTON, STR_ORDERS_DELETE_TOOLTIP), SetResize(1, 0),
    01575       NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, ORDER_WIDGET_STOP_SHARING), SetMinimalSize(124, 12), SetFill(1, 0),
    01576                           SetDataTip(STR_ORDERS_STOP_SHARING_BUTTON, STR_ORDERS_STOP_SHARING_TOOLTIP), SetResize(1, 0),
    01577     EndContainer(),
    01578     NWidget(NWID_BUTTON_DROPDOWN, COLOUR_GREY, ORDER_WIDGET_GOTO), SetMinimalSize(124, 12), SetFill(1, 0),
    01579                       SetDataTip(STR_ORDERS_GO_TO_BUTTON, STR_ORDERS_GO_TO_TOOLTIP), SetResize(1, 0),
    01580     NWidget(WWT_RESIZEBOX, COLOUR_GREY),
    01581   EndContainer(),
    01582 };
    01583
    01584 static const WindowDesc _orders_desc(
    01585   WDP_AUTO, 384, 100,
    01586   WC_VEHICLE_ORDERS, WC_VEHICLE_VIEW,
    01587   WDF_CONSTRUCTION,
    01588   _nested_orders_widgets, lengthof(_nested_orders_widgets)
    01589 );
    01590
    01592 static const NWidgetPart _nested_other_orders_widgets[] = {
    01593   NWidget(NWID_HORIZONTAL),
    01594     NWidget(WWT_CLOSEBOX, COLOUR_GREY),
    01595     NWidget(WWT_CAPTION, COLOUR_GREY, ORDER_WIDGET_CAPTION), SetDataTip(STR_ORDERS_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    01596     NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, ORDER_WIDGET_TIMETABLE_VIEW), SetMinimalSize(61, 14), SetDataTip(STR_ORDERS_TIMETABLE_VIEW, STR_ORDERS_TIMETABLE_VIEW_TOOLTIP),
    01597     NWidget(WWT_SHADEBOX, COLOUR_GREY),
    01598     NWidget(WWT_STICKYBOX, COLOUR_GREY),
    01599   EndContainer(),
    01600   NWidget(NWID_HORIZONTAL),
    01601     NWidget(WWT_PANEL, COLOUR_GREY, ORDER_WIDGET_ORDER_LIST), SetMinimalSize(372, 72), SetDataTip(0x0, STR_ORDERS_LIST_TOOLTIP), SetResize(1, 1), SetScrollbar(ORDER_WIDGET_SCROLLBAR), EndContainer(),
    01602     NWidget(NWID_VERTICAL),
    01603       NWidget(NWID_VSCROLLBAR, COLOUR_GREY, ORDER_WIDGET_SCROLLBAR),
    01604       NWidget(WWT_RESIZEBOX, COLOUR_GREY),
    01605     EndContainer(),
    01606   EndContainer(),
    01607 };
    01608
    01609 static const WindowDesc _other_orders_desc(
    01610   WDP_AUTO, 384, 86,
    01611   WC_VEHICLE_ORDERS, WC_VEHICLE_VIEW,
    01612   WDF_UNCLICK_BUTTONS | WDF_CONSTRUCTION,
    01613   _nested_other_orders_widgets, lengthof(_nested_other_orders_widgets)
    01614 );
    01615
    01616 void ShowOrdersWindow(const Vehicle *v)
    01617 {
    01618   DeleteWindowById(WC_VEHICLE_DETAILS, v->index, false);
    01619   DeleteWindowById(WC_VEHICLE_TIMETABLE, v->index, false);
    01620   if (BringWindowToFrontById(WC_VEHICLE_ORDERS, v->index) != NULL) return;
    01621
    01622   if (v->owner != _local_company) {
    01623     new OrdersWindow(&_other_orders_desc, v);
    01624   } else {
    01625     new OrdersWindow(v->IsGroundVehicle() ? &_orders_train_desc : &_orders_desc, v);
    01626   }
    01627 }