(svn r7315) -Fix: Have the first engine in the list be selected once again when the window is opened
authorDarkvater
Fri, 01 Dec 2006 00:26:42 +0000
changeset 5200 603e30641448
parent 5199 523e7025d979
child 5201 db204cf84e2d
(svn r7315) -Fix: Have the first engine in the list be selected once again when the window is opened
and remove a useless loop that tested if selected engine is still in list (moved to list
generation).
build_vehicle_gui.c
train_gui.c
--- a/build_vehicle_gui.c	Fri Dec 01 00:14:10 2006 +0000
+++ b/build_vehicle_gui.c	Fri Dec 01 00:26:42 2006 +0000
@@ -279,11 +279,16 @@
 
 static void GenerateBuildAircraftList(Window *w)
 {
-	EngineID eid;
+	EngineID eid, sel_id;
 	buildvehicle_d *bv = &WP(w, buildvehicle_d);
 
 	EngList_RemoveAll(&bv->eng_list);
 
+	/* Make list of all available planes.
+	 * Also check to see if the previously selected plane is still available,
+	 * and if not, reset selection to INVALID_ENGINE. This could be the case
+	 * when planes become obsolete and are removed */
+	sel_id = INVALID_ENGINE;
 	for (eid = AIRCRAFT_ENGINES_INDEX; eid < AIRCRAFT_ENGINES_INDEX + NUM_AIRCRAFT_ENGINES; eid++) {
 		if (IsEngineBuildable(eid, VEH_Aircraft)) {
 			const AircraftVehicleInfo *avi = AircraftVehInfo(eid);
@@ -299,8 +304,12 @@
 				case ALL: break;
 			}
 			EngList_Add(&bv->eng_list, eid);
+
+			if (eid == bv->sel_engine) sel_id = eid;
 		}
 	}
+
+	bv->sel_engine = sel_id;
 }
 
 static void GenerateBuildList(Window *w)
@@ -330,22 +339,10 @@
 
 static void DrawBuildAircraftWindow(Window *w)
 {
-	buildvehicle_d *bv = &WP(w, buildvehicle_d);
+	const buildvehicle_d *bv = &WP(w, buildvehicle_d);
 
 	SetWindowWidgetDisabledState(w, BUILD_VEHICLE_WIDGET_BUILD, w->window_number == 0);
 
-	if (bv->sel_engine != INVALID_ENGINE) {
-		int i;
-		bool found = false;
-		int num_planes = GetEngineArrayLength(w);
-		for (i = 0; i < num_planes; i++) {
-			if (bv->sel_engine != GetEngineArray(w)[i]) continue;
-			found = true;
-			break;
-		}
-		if (!found) bv->sel_engine = INVALID_ENGINE;
-	}
-
 	SetVScrollCount(w, GetEngineArrayLength(w));
 	DrawWindowWidgets(w);
 
@@ -504,4 +501,6 @@
 	}
 
 	GenerateBuildList(w);
+	/* Select the first plane in the list as default when opening the window */
+	if (EngList_Count(&bv->eng_list) > 0) bv->sel_engine = bv->eng_list[0];
 }
--- a/train_gui.c	Fri Dec 01 00:14:10 2006 +0000
+++ b/train_gui.c	Fri Dec 01 00:26:42 2006 +0000
@@ -375,7 +375,7 @@
 
 static void GenerateBuildList(Window *w)
 {
-	EngineID eid;
+	EngineID eid, sel_id;
 	int num_engines = 0;
 	int num_wagons  = 0;
 	buildvehicle_d *bv = &WP(w, buildvehicle_d);
@@ -384,8 +384,11 @@
 
 	EngList_RemoveAll(&bv->eng_list);
 
-	// make a list of all available cars
-	for (eid = 0; eid < NUM_TRAIN_ENGINES; eid++) {
+	/* Make list of all available train engines and wagons.
+	 * Also check to see if the previously selected engine is still available,
+	 * and if not, reset selection to INVALID_ENGINE. This could be the case
+	 * when engines become obsolete and are removed */
+	for (sel_id = INVALID_ENGINE, eid = 0; eid < NUM_TRAIN_ENGINES; eid++) {
 		const Engine *e = GetEngine(eid);
 		const RailVehicleInfo *rvi = RailVehInfo(eid);
 
@@ -398,8 +401,12 @@
 		} else {
 			num_wagons++;
 		}
+
+		if (eid == bv->sel_engine) sel_id = eid;
 	}
 
+	bv->sel_engine = sel_id;
+
 	// make engines first, and then wagons, sorted by ListPositionOfEngine()
 	_internal_sort_order = false;
 	EngList_Sort(&bv->eng_list, TrainEnginesThenWagonsSorter);
@@ -420,25 +427,10 @@
 	int y = 27;
 	EngineID selected_id = bv->sel_engine;
 	int max = w->vscroll.pos + w->vscroll.cap;
-	uint16 scrollcount = 0;
 
 	SetWindowWidgetDisabledState(w, BUILD_TRAIN_WIDGET_BUILD, w->window_number == 0); // Disable unless we got a depot to build in
 
-	/* Make sure that the selected engine is still in the list*/
-	if (bv->sel_engine != INVALID_ENGINE) {
-		int i;
-		bool found = false;
-		for (i = 0; i < num_engines; i++) {
-			if (bv->sel_engine != bv->eng_list[i]) continue;
-			found = true;
-			break;
-		}
-		if (!found) bv->sel_engine = INVALID_ENGINE;
-	}
-
-	scrollcount = EngList_Count(&bv->eng_list);
-
-	SetVScrollCount(w, scrollcount);
+	SetVScrollCount(w, EngList_Count(&bv->eng_list));
 	SetDParam(0, bv->filter.railtype + STR_881C_NEW_RAIL_VEHICLES);
 	DrawWindowWidgets(w);
 
@@ -469,6 +461,8 @@
 			bv->sort_criteria         = _last_sort_criteria;
 			bv->descending_sort_order = _last_sort_order;
 			GenerateBuildList(w);
+			/* Select the first engine in the list as default when opening the window */
+			if (EngList_Count(&bv->eng_list) > 0) bv->sel_engine = bv->eng_list[0];
 			break;
 
 		case WE_INVALIDATE_DATA: