(svn r7130) -Codechange: Handle the positioning of windows through the desc->left/top settings with
authorDarkvater
Sat, 11 Nov 2006 10:12:00 +0000
changeset 5072 df5bde83a4bc
parent 5071 14f432b47034
child 5073 7983f98f3d4c
(svn r7130) -Codechange: Handle the positioning of windows through the desc->left/top settings with
some special values (WDP_) instead of checking window-class. This also fixes FS#172
now that we can position windows arbitrarily and are not restricted to window-classes.
airport_gui.c
dock_gui.c
rail_gui.c
road_gui.c
terraform_gui.c
window.c
window.h
--- a/airport_gui.c	Sat Nov 11 09:47:44 2006 +0000
+++ b/airport_gui.c	Sat Nov 11 10:12:00 2006 +0000
@@ -124,7 +124,7 @@
 
 
 static const WindowDesc _air_toolbar_desc = {
-	0, 22, 86, 36,
+	WDP_ALIGN_TBR, 22, 86, 36,
 	WC_BUILD_TOOLBAR, 0,
 	WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_STICKY_BUTTON,
 	_air_toolbar_widgets,
--- a/dock_gui.c	Sat Nov 11 09:47:44 2006 +0000
+++ b/dock_gui.c	Sat Nov 11 10:12:00 2006 +0000
@@ -205,7 +205,7 @@
 };
 
 static const WindowDesc _build_docks_toolbar_desc = {
-	0, 22, 158, 36,
+	WDP_ALIGN_TBR, 22, 158, 36,
 	WC_BUILD_TOOLBAR, 0,
 	WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_STICKY_BUTTON,
 	_build_docks_toolb_widgets,
--- a/rail_gui.c	Sat Nov 11 09:47:44 2006 +0000
+++ b/rail_gui.c	Sat Nov 11 10:12:00 2006 +0000
@@ -544,7 +544,7 @@
 };
 
 static const WindowDesc _build_rail_desc = {
-	0, 22, 372, 36,
+	WDP_ALIGN_TBR, 22, 372, 36,
 	WC_BUILD_TOOLBAR, 0,
 	WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_STICKY_BUTTON,
 	_build_rail_widgets,
--- a/road_gui.c	Sat Nov 11 09:47:44 2006 +0000
+++ b/road_gui.c	Sat Nov 11 10:12:00 2006 +0000
@@ -309,7 +309,7 @@
 };
 
 static const WindowDesc _build_road_desc = {
-	0, 22, 240, 36,
+	WDP_ALIGN_TBR, 22, 240, 36,
 	WC_BUILD_TOOLBAR, 0,
 	WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_STICKY_BUTTON,
 	_build_road_widgets,
--- a/terraform_gui.c	Sat Nov 11 09:47:44 2006 +0000
+++ b/terraform_gui.c	Sat Nov 11 10:12:00 2006 +0000
@@ -265,7 +265,7 @@
 };
 
 static const WindowDesc _terraform_desc = {
-	640-157, 22+36, 158, 36,
+	WDP_ALIGN_TBR, 22+36, 158, 36,
 	WC_SCEN_LAND_GEN, 0,
 	WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_STICKY_BUTTON,
 	_terraform_widgets,
--- a/window.c	Sat Nov 11 09:47:44 2006 +0000
+++ b/window.c	Sat Nov 11 10:12:00 2006 +0000
@@ -694,30 +694,44 @@
 			pt.x = (_screen.width + 10 - desc->width) - 20;
 		}
 		pt.y = w->top + 10;
-	} else if (desc->cls == WC_BUILD_TOOLBAR) { // open Build Toolbars aligned
-		/* Override the position if a toolbar is opened according to the place of the maintoolbar
-		 * The main toolbar (WC_MAIN_TOOLBAR) is 640px in width */
-		switch (_patches.toolbar_pos) {
-			case 1:  pt.x = ((_screen.width + 640) >> 1) - desc->width; break;
-			case 2:  pt.x = _screen.width - desc->width; break;
-			default: pt.x = 640 - desc->width;
-		}
-		pt.y = desc->top;
 	} else {
-		pt.x = desc->left;
-		pt.y = desc->top;
-		if (pt.x == WDP_AUTO) {
-			pt = GetAutoPlacePosition(desc->width, desc->height);
-		} else {
-			if (pt.x == WDP_CENTER) pt.x = (_screen.width - desc->width) >> 1;
-			if (pt.y == WDP_CENTER) {
-				pt.y = (_screen.height - desc->height) >> 1;
-			} else if (pt.y < 0) {
-				pt.y = _screen.height + pt.y; // if y is negative, it's from the bottom of the screen
-			}
+		switch (desc->left) {
+			case WDP_ALIGN_TBR: { /* Align the right side with the top toolbar */
+				w = FindWindowById(WC_MAIN_TOOLBAR, 0);
+				pt.x = (w->left + w->width) - desc->width;
+			}	break;
+			case WDP_ALIGN_TBL: /* Align the left side with the top toolbar */
+				pt.x = FindWindowById(WC_MAIN_TOOLBAR, 0)->left;
+				break;
+			case WDP_AUTO: /* Find a good automatic position for the window */
+				pt = GetAutoPlacePosition(desc->width, desc->height);
+				goto allocate_window;
+			case WDP_CENTER: /* Centre the window horizontally */
+				pt.x = (_screen.width - desc->width) / 2;
+				break;
+			default:
+				pt.x = desc->left;
+				if (pt.x < 0) pt.x += _screen.width; // negative is from right of the screen
+		}
+
+		switch (desc->top) {
+			case WDP_CENTER: /* Centre the window vertically */
+				pt.y = (_screen.height - desc->height) / 2;
+				break;
+			/* WDP_AUTO sets the position at once and is controlled by desc->left.
+			 * Both left and top must be set to WDP_AUTO */
+			case WDP_AUTO:
+				NOT_REACHED();
+				assert(desc->left == WDP_AUTO && desc->top != WDP_AUTO);
+				/* fallthrough */
+			default:
+				pt.y = desc->top;
+				if (pt.y < 0) pt.y += _screen.height; // negative is from bottom of the screen
+				break;
 		}
 	}
 
+allocate_window:
 	w = LocalAllocateWindow(pt.x, pt.y, desc->width, desc->height, desc->proc, desc->cls, desc->widgets, window_number);
 	w->desc_flags = desc->flags;
 	return w;
--- a/window.h	Sat Nov 11 09:47:44 2006 +0000
+++ b/window.h	Sat Nov 11 10:12:00 2006 +0000
@@ -275,8 +275,10 @@
 
 /* can be used as x or y coordinates to cause a specific placement */
 enum {
-	WDP_AUTO   = -1,
-	WDP_CENTER = -2,
+	WDP_AUTO      = -1, ///< Find a place automatically
+	WDP_CENTER    = -2, ///< Center the window (left/right or top/bottom)
+	WDP_ALIGN_TBR = -3, ///< Align the right side of the window with the right side of the main toolbar
+	WDP_ALIGN_TBL = -4, ///< Align the left side of the window with the left side of the main toolbar
 };
 
 typedef struct Textbuf {