From 42f6d0f9747e3e6a12b1f918fbccc0a2ea55d334 Mon Sep 17 00:00:00 2001
From: Pietro Gagliardi <pietro10@mac.com>
Date: Tue, 1 Sep 2015 21:36:33 -0400
Subject: [PATCH] Set up the framework for handling Z-order and control IDs.
 Not actually used yet; we'll do that next.

---
 redo/ui_windows.h   | 14 +++++++++++++-
 redo/windows/util.c |  6 ++++++
 2 files changed, 19 insertions(+), 1 deletion(-)

diff --git a/redo/ui_windows.h b/redo/ui_windows.h
index 8e576366..0bc231f8 100644
--- a/redo/ui_windows.h
+++ b/redo/ui_windows.h
@@ -15,6 +15,7 @@ struct uiWindowsControl {
 	void (*CommitSetParent)(uiWindowsControl *, HWND);
 	void (*MinimumSize)(uiWindowsControl *, uiWindowsSizing *, intmax_t *, intmax_t *);
 	void (*Relayout)(uiWindowsControl *, intmax_t, intmax_t, intmax_t, intmax_t);
+	void (*AssignControlIDZOrder)(uiWindowsControl *, LONG_PTR *, HWND *);
 };
 _UI_EXTERN uintmax_t uiWindowsControlType(void);
 #define uiWindowsControl(this) ((uiWindowsControl *) uiIsA((this), uiWindowsControlType(), 1))
@@ -52,7 +53,13 @@ _UI_EXTERN void uiWindowsControlQueueRelayout(uiWindowsControl *);
 	{ \
 		uiWindowsEnsureMoveWindow(type(c)->hwnd, x, y, width, height); \
 	} \
-	static void minimumSize(uiWindowsControl *c, uiWindowsSizing *d, intmax_t *width, intmax_t *height);
+	static void minimumSize(uiWindowsControl *c, uiWindowsSizing *d, intmax_t *width, intmax_t *height); \
+	static void _ ## type ## AssignControlIDZOrder(uiWindowsControl *c, LONG_PTR *controlID, HWND *insertAfter) \
+	{ \
+		uiWindowsEnsureAssignControlIDZOrder(type(c)->hwnd, *controlID, *insertAfter); \
+		(*controlID)++; \
+		*insertAfter = type(c)->hwnd; \
+	}
 
 #define uiWindowsDefineControl(type, typefn) \
 	uiWindowsDefineControlWithOnDestroy(type, typefn, (void) this;)
@@ -64,6 +71,7 @@ _UI_EXTERN void uiWindowsControlQueueRelayout(uiWindowsControl *);
 	uiWindowsControl(variable)->CommitSetParent = _ ## type ## CommitSetParent; \
 	uiWindowsControl(variable)->MinimumSize = minimumSize; \
 	uiWindowsControl(variable)->Relayout = _ ## type ## Relayout; \
+	uiWindowsControl(variable)->AssignControlIDZOrder = _ ## type ## AssignControlIDZOrder; \
 	uiWindowsFinishControl(uiControl(variable));
 
 // This is a function used to set up a control.
@@ -81,6 +89,10 @@ _UI_EXTERN void uiWindowsEnsureSetParent(HWND hwnd, HWND parent);
 // Use this in your Relayout() implementation to move and resize HWNDs. libui handles errors for you.
 _UI_EXTERN void uiWindowsEnsureMoveWindow(HWND hwnd, intmax_t x, intmax_t y, intmax_t width, intmax_t height);
 
+// Use this in implementations of AssignControlIDZOrder().
+// libui handles errors for you.
+_UI_EXTERN void uiWindowsEnsureAssignControlIDZOrder(HWND hwnd, LONG_PTR controlID, HWND insertAfter);
+
 ////////////////////////////////////////////
 /////////////////// TODO ///////////////////
 ////////////////////////////////////////////
diff --git a/redo/windows/util.c b/redo/windows/util.c
index 51b0603d..6a3f02c2 100644
--- a/redo/windows/util.c
+++ b/redo/windows/util.c
@@ -92,3 +92,9 @@ void uiWindowsEnsureSetParent(HWND hwnd, HWND parent)
 	if (SetParent(hwnd, parent) == 0)
 		logLastError("error setting window parent in uiWindowsEnsureSetParent");
 }
+
+void uiWindowsEnsureAssignControlIDZOrder(HWND hwnd, LONG_PTR controlID, HWND insertAfter)
+{
+	SetWindowLongPtrW(hwnd, GWLP_ID, controlID);
+	setWindowInsertAfter(hwnd, insertAfter);
+}