(svn r8384) -Codechange: [GUI] instead of writing a resize button function for each window, a global ResizeButtons() is added
authorbjarni
Wed, 24 Jan 2007 02:36:55 +0000
changeset 6073 d8dae377c879
parent 6072 0088b58f1ce6
child 6074 e70d63ef4d62
(svn r8384) -Codechange: [GUI] instead of writing a resize button function for each window, a global ResizeButtons() is added
src/build_vehicle_gui.cpp
src/depot_gui.cpp
src/train_gui.cpp
src/widget.cpp
src/window.h
--- a/src/build_vehicle_gui.cpp	Wed Jan 24 01:37:04 2007 +0000
+++ b/src/build_vehicle_gui.cpp	Wed Jan 24 02:36:55 2007 +0000
@@ -56,14 +56,6 @@
 	{   WIDGETS_END},
 };
 
-static void ResizeButtons(Window *w)
-{
-	/* Make the buttons in the bottom equal in size */
-	w->widget[BUILD_VEHICLE_WIDGET_RENAME].right = w->widget[BUILD_VEHICLE_WIDGET_RESIZE].left - 1;
-	w->widget[BUILD_VEHICLE_WIDGET_RENAME].left  = w->widget[BUILD_VEHICLE_WIDGET_RENAME].right / 2;
-	w->widget[BUILD_VEHICLE_WIDGET_BUILD].right  = w->widget[BUILD_VEHICLE_WIDGET_RENAME].left - 1;
-}
-
 /* Setup widget strings to fit the different types of vehicles */
 static void SetupWindowStrings(Window *w, byte type)
 {
@@ -922,7 +914,7 @@
 			break;
 
 		case WE_RESIZE:
-			if (e->we.sizing.diff.x != 0) ResizeButtons(w);
+			if (e->we.sizing.diff.x != 0) ResizeButtons(w, BUILD_VEHICLE_WIDGET_BUILD, BUILD_VEHICLE_WIDGET_RESIZE);
 			if (e->we.sizing.diff.y == 0) break;
 
 			w->vscroll.cap += e->we.sizing.diff.y / GetVehicleListHeight(bv->vehicle_type);
@@ -984,7 +976,7 @@
 			break;
 	}
 	SetupWindowStrings(w, type);
-	ResizeButtons(w);
+	ResizeButtons(w, BUILD_VEHICLE_WIDGET_BUILD, BUILD_VEHICLE_WIDGET_RESIZE);
 
 	w->resize.width  = w->width;
 	w->resize.height = w->height;
--- a/src/depot_gui.cpp	Wed Jan 24 01:37:04 2007 +0000
+++ b/src/depot_gui.cpp	Wed Jan 24 02:36:55 2007 +0000
@@ -497,13 +497,7 @@
 
 static void ResizeDepotButtons(Window *w)
 {
-	/* We got the widget moved around. Now we will make some widgets to fill the gap between some widgets in equal sizes */
-
-	/* Make the buttons in the bottom equal in size */
-	w->widget[DEPOT_WIDGET_BUILD].right    = w->widget[DEPOT_WIDGET_LOCATION].right / 3;
-	w->widget[DEPOT_WIDGET_LOCATION].left  = w->widget[DEPOT_WIDGET_BUILD].right * 2;
-	w->widget[DEPOT_WIDGET_CLONE].left     = w->widget[DEPOT_WIDGET_BUILD].right + 1;
-	w->widget[DEPOT_WIDGET_CLONE].right    = w->widget[DEPOT_WIDGET_LOCATION].left - 1;
+	ResizeButtons(w, DEPOT_WIDGET_BUILD, DEPOT_WIDGET_VEHICLE_LIST);
 
 	if (WP(w, depot_d).type == VEH_Train) {
 		/* Divide the size of DEPOT_WIDGET_SELL into two equally big buttons so DEPOT_WIDGET_SELL and DEPOT_WIDGET_SELL_CHAIN will get the same size.
--- a/src/train_gui.cpp	Wed Jan 24 01:37:04 2007 +0000
+++ b/src/train_gui.cpp	Wed Jan 24 02:36:55 2007 +0000
@@ -506,19 +506,6 @@
 	}
 }
 
-static void TrainDetailButtonResize(Window *w)
-{
-	/* Make the buttons in the bottom equal in size */
-	w->widget[12].right = w->widget[13].left - 1; // right point of the buttons (4/4)
-	w->widget[10].right = w->widget[12].right / 2; // the middle of the buttons (2/4)
-	w->widget[ 9].right = w->widget[10].right / 2; // 1/4 of the buttons
-	w->widget[11].right = w->widget[10].right + w->widget[ 9].right; // (2+1)/4 = 3/4 of the buttons
-	/* Now the right side of the buttons are set. We will now set the left sides next to them */
-	w->widget[10].left  = w->widget[ 9].right + 1;
-	w->widget[11].left  = w->widget[10].right + 1;
-	w->widget[12].left  = w->widget[11].right + 1;
-}
-
 static void TrainDetailsWndProc(Window *w, WindowEvent *e)
 {
 	switch (e->event) {
@@ -573,7 +560,7 @@
 		break;
 
 	case WE_RESIZE:
-		if (e->we.sizing.diff.x != 0) TrainDetailButtonResize(w);
+		if (e->we.sizing.diff.x != 0) ResizeButtons(w, 9, 13);
 		if (e->we.sizing.diff.y == 0) break;
 
 		w->vscroll.cap += e->we.sizing.diff.y / 14;
--- a/src/widget.cpp	Wed Jan 24 01:37:04 2007 +0000
+++ b/src/widget.cpp	Wed Jan 24 02:36:55 2007 +0000
@@ -698,3 +698,50 @@
 	WP(w2,dropdown_d).click_delay = 0;
 	WP(w2,dropdown_d).drag_mode = true;
 }
+
+/* Make the buttons in the bottom equal in size */
+void ResizeButtons(Window *w, byte a, byte b, byte c, byte d, byte right)
+{
+	w->widget[d].right = w->widget[right].left - 1; // now we set the right of the widgets
+
+	/* Now we will find the middle, then the middle of each of the two blocks on each side of the middle.
+	 * This way, if we got leftover pixels from the division, they will be somewhat evenly distributed */
+	w->widget[b].right = w->widget[d].right / 2;
+	w->widget[a].right = w->widget[b].right / 2;
+	w->widget[c].right = (w->widget[b].right + w->widget[d].right)/2;
+	/* Now the right side of the buttons are set. We will now set the left sides next to them */
+	w->widget[b].left  = w->widget[a].right + 1;
+	w->widget[c].left  = w->widget[b].right + 1;
+	w->widget[d].left  = w->widget[c].right + 1;
+}
+
+void ResizeButtons(Window *w, byte a, byte b, byte c, byte right)
+{
+	w->widget[c].right = w->widget[right].left - 1; // now we set the right of the widgets
+
+	w->widget[a].right = w->widget[c].right / 3;
+	w->widget[b].right = w->widget[a].right * 2;
+
+	/* Now the right side of the buttons are set. We will now set the left sides next to them */
+	w->widget[b].left  = w->widget[a].right + 1;
+	w->widget[c].left  = w->widget[b].right + 1;
+}
+
+void ResizeButtons(Window *w, byte a, byte b, byte right)
+{
+	w->widget[b].right = w->widget[right].left - 1; // now we set the right of the widgets
+
+	w->widget[a].right  = w->widget[b].right / 2;
+
+	w->widget[b].left  = w->widget[a].right + 1;
+}
+
+void ResizeButtons(Window *w, byte left, byte right)
+{
+	switch (right - left) {
+		case 2: ResizeButtons(w, left, left + 1, left + 2); break;
+		case 3: ResizeButtons(w, left, left + 1, left + 2, left + 3); break;
+		case 4: ResizeButtons(w, left, left + 1, left + 2, left + 3, left + 4); break;
+		default: NOT_REACHED();
+	}
+}
--- a/src/window.h	Wed Jan 24 01:37:04 2007 +0000
+++ b/src/window.h	Wed Jan 24 02:36:55 2007 +0000
@@ -767,4 +767,21 @@
 
 void ScrollbarClickHandler(Window *w, const Widget *wi, int x, int y);
 
+/** Evenly distribute some widgets when resizing horizontally (often a button row)
+ * @param w widow to modify
+ * @param a,b,c,d the widgets to resize (left to right, order matters)
+ * @param right the widget right of the buttons, that needs resizing
+ */
+void ResizeButtons(Window *w, byte a, byte b, byte right);
+void ResizeButtons(Window *w, byte a, byte b, byte c, byte right);
+void ResizeButtons(Window *w, byte a, byte b, byte c, byte d, byte right);
+
+/** Evenly distribute some widgets when resizing horizontally (often a button row)
+ *  When only two arguments are given, the widgets are presumed to be on a line and only the ends are given
+ * @param w widow to modify
+ * @param left The leftmost widget to resize
+ * @param right The widget just right of the widgets to resize
+ */
+void ResizeButtons(Window *w, byte left, byte right);
+
 #endif /* WINDOW_H */