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