# order_gui.cpp

Go to the documentation of this file.

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