src/vehicle_gui.cpp
branchNewGRF_ports
changeset 10242 52b4a9006029
parent 10210 a2131f7a315d
child 10274 b3c58f3df92b
--- a/src/vehicle_gui.cpp	Wed Apr 16 22:34:14 2008 +0000
+++ b/src/vehicle_gui.cpp	Fri Apr 18 19:55:13 2008 +0000
@@ -21,7 +21,7 @@
 #include "ship.h"
 #include "aircraft.h"
 #include "roadveh.h"
-#include "depot.h"
+#include "depot_base.h"
 #include "cargotype.h"
 #include "group.h"
 #include "group_gui.h"
@@ -35,6 +35,7 @@
 #include "settings_type.h"
 #include "widgets/dropdown_func.h"
 #include "order_func.h"
+#include "depot_base.h"
 
 #include "table/sprites.h"
 #include "table/strings.h"
@@ -71,6 +72,7 @@
 static VehicleSortListingTypeFunction VehicleMaxSpeedSorter;
 static VehicleSortListingTypeFunction VehicleModelSorter;
 static VehicleSortListingTypeFunction VehicleValueSorter;
+static VehicleSortListingTypeFunction VehicleLengthSorter;
 
 static VehicleSortListingTypeFunction* const _vehicle_sorter[] = {
 	&VehicleNumberSorter,
@@ -83,6 +85,7 @@
 	&VehicleMaxSpeedSorter,
 	&VehicleModelSorter,
 	&VehicleValueSorter,
+	&VehicleLengthSorter,
 };
 
 const StringID _vehicle_sort_listing[] = {
@@ -96,6 +99,7 @@
 	STR_SORT_BY_MAX_SPEED,
 	STR_SORT_BY_MODEL,
 	STR_SORT_BY_VALUE,
+	STR_SORT_BY_LENGTH,
 	INVALID_STRING_ID
 };
 
@@ -697,6 +701,30 @@
 	return (_internal_sort_order & 1) ? -r : r;
 }
 
+static int CDECL VehicleLengthSorter(const void *a, const void *b)
+{
+	const Vehicle *va = *(const Vehicle**)a;
+	const Vehicle *vb = *(const Vehicle**)b;
+	int r = 0;
+
+	switch (va->type) {
+		case VEH_TRAIN:
+			r = va->u.rail.cached_total_length - vb->u.rail.cached_total_length;
+			break;
+
+		case VEH_ROAD:
+			for (const Vehicle *u = va; u != NULL; u = u->Next()) r += u->u.road.cached_veh_length;
+			for (const Vehicle *u = vb; u != NULL; u = u->Next()) r -= u->u.road.cached_veh_length;
+			break;
+
+		default: NOT_REACHED();
+	}
+
+	VEHICLEUNITNUMBERSORTER(r, va, vb);
+
+	return (_internal_sort_order & 1) ? -r : r;
+}
+
 void InitializeGUI()
 {
 	memset(&_sorting, 0, sizeof(_sorting));
@@ -1060,7 +1088,7 @@
 					SetWindowDirty(w);
 					break;
 				case VLW_WIDGET_SORT_BY_PULLDOWN:/* Select sorting criteria dropdown menu */
-					ShowDropDownMenu(w, _vehicle_sort_listing, vl->l.sort_type, VLW_WIDGET_SORT_BY_PULLDOWN, 0, 0);
+					ShowDropDownMenu(w, _vehicle_sort_listing, vl->l.sort_type, VLW_WIDGET_SORT_BY_PULLDOWN, 0, (vl->vehicle_type == VEH_TRAIN || vl->vehicle_type == VEH_ROAD) ? 0 : (1 << 10));
 					return;
 				case VLW_WIDGET_LIST: { /* Matrix to show vehicles */
 					uint32 id_v = (e->we.click.pt.y - PLY_WND_PRC__OFFSET_TOP_WIDGET) / w->resize.step_height;
@@ -1436,7 +1464,7 @@
 extern void DrawAircraftDetails(const Vehicle *v, int x, int y);
 
 /**
-* Draw the details for the given vehicle at the position (x,y) of the Details windows
+* Draw the details for the given vehicle at the position (x, y) of the Details windows
 *
 * @param v current vehicle
 * @param x The x coordinate