changeset 57:1ed95c8ec2ff

Added a mutex lock and unlock in the pointer calls. And fixed a warning on OS/2.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Wed, 21 Nov 2001 23:30:24 +0000
parents b0f94956c981
children 5c66a108aa47
files gtk/dw.c win/dw.c
diffstat 2 files changed, 3673 insertions(+), 3362 deletions(-) [+]
line wrap: on
line diff
--- a/gtk/dw.c	Wed Nov 21 22:33:58 2001 +0000
+++ b/gtk/dw.c	Wed Nov 21 23:30:24 2001 +0000
@@ -1705,10 +1705,13 @@
 {
 	GdkModifierType state;
 	int gx, gy;
-
+	int _locked_by_me = FALSE;
+
+	DW_MUTEX_LOCK;
 	gdk_window_get_pointer (GDK_ROOT_PARENT(), &gx, &gy, &state);
 	*x = gx;
 	*y = gy;
+	DW_MUTEX_UNLOCK;
 }
 
 /*
@@ -1719,7 +1722,11 @@
  */
 void dw_pointer_set_pos(long x, long y)
 {
+	int _locked_by_me = FALSE;
+
+	DW_MUTEX_LOCK;
 	XWarpPointer(GDK_DISPLAY(), None, GDK_ROOT_WINDOW(), 0,0,0,0, x, y);
+	DW_MUTEX_UNLOCK;
 }
 
 /*
--- a/win/dw.c	Wed Nov 21 22:33:58 2001 +0000
+++ b/win/dw.c	Wed Nov 21 23:30:24 2001 +0000
@@ -1,50 +1,57 @@
 /*
  * Dynamic Windows:
- *          A GTK like implementation of the Win32 GUI
+ *          A GTK like implementation of the PM GUI
  *
  * (C) 2000,2001 Brian Smith <dbsoft@technologist.com>
+ * (C) 2000 Achim Hasenmueller <achimha@innotek.de>
+ * (C) 2000 Peter Nielsen <peter@pmview.com>
+ * (C) 1998 Sergey I. Yevtushenko (some code borrowed from cell toolkit)
  *
  */
-#define _WIN32_IE 0x0500
-#define WINVER 0x400
-#include <windows.h>
-#include <windowsx.h>
-#include <commctrl.h>
+#define INCL_DOS
+#define INCL_DOSERRORS
+#define INCL_WIN
+#define INCL_GPI
+
+#include <os2.h>
 #include <stdlib.h>
 #include <string.h>
 #include <stdio.h>
+#include <stdarg.h>
+#include <stddef.h>
+#include <ctype.h>
 #include <process.h>
 #include <time.h>
 #include "dw.h"
 
-/* this is the callback handle for the window procedure */
-/* make sure you always match the calling convention! */
-int (*filterfunc)(HWND, UINT, WPARAM, LPARAM) = 0L;
-
-HWND hwndBubble = (HWND)NULL, hwndBubbleLast, DW_HWND_OBJECT = (HWND)NULL;
-
-HINSTANCE DWInstance = NULL;
-
-DWORD dwVersion = 0;
-
-/* I should probably check the actual file version, but this will do for now */
-#define IS_WIN98PLUS (LOBYTE(LOWORD(dwVersion)) > 4 || \
-	(LOBYTE(LOWORD(dwVersion)) == 4 && HIBYTE(LOWORD(dwVersion)) > 0))
-
-char monthlist[][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug",
-                        "Sep", "Oct", "Nov", "Dec" };
-
-int main(int argc, char *argv[]);
-
-#define ICON_INDEX_LIMIT 200
-HICON lookup[200];
-HIMAGELIST hSmall  = 0, hLarge = 0;
-
-#define THREAD_LIMIT 128
-COLORREF _foreground[THREAD_LIMIT];
-COLORREF _background[THREAD_LIMIT];
-HPEN _hPen[THREAD_LIMIT];
-HBRUSH _hBrush[THREAD_LIMIT];
+#define QWP_USER 0
+
+MRESULT EXPENTRY _run_event(HWND hWnd, ULONG msg, MPARAM mp1, MPARAM mp2);
+
+char ClassName[] = "dynamicwindows";
+char SplitbarClassName[] = "dwsplitbar";
+char DefaultFont[] = "9.WarpSans";
+
+/* this is the callback handle for the window procedure
+ * make sure you always match the calling convention!
+ */
+int (* EXPENTRY filterfunc)(HWND, ULONG, MPARAM, MPARAM) = 0L;
+
+HAB dwhab = 0;
+HMQ dwhmq = 0;
+DWTID _dwtid = 0;
+LONG _foreground = 0xAAAAAA, _background = 0;
+
+HWND hwndBubble = NULLHANDLE, hwndBubbleLast = NULLHANDLE;
+PRECORDCORE pCore = NULL;
+ULONG aulBuffer[4];
+HWND lasthcnr = 0, lastitem = 0;
+
+#define IS_WARP4() (aulBuffer[0] == 20 && aulBuffer[1] >= 40)
+
+#ifndef min
+#define min(a, b) (((a < b) ? a : b))
+#endif
 
 #ifdef DWDEBUG
 FILE *f;
@@ -52,19 +59,10 @@
 void reopen(void)
 {
 	fclose(f);
-	f = fopen("dw.log", "at");
+	f = fopen("dw.log", "a+");
 }
 #endif
 
-BYTE _red[] = { 	0x00, 0xbb, 0x00, 0xaa, 0x00, 0xbb, 0x00, 0xaa, 0x77,
-			  0xff, 0x00, 0xee, 0x00, 0xff, 0x00, 0xff, 0xaa, 0x00 };
-BYTE _green[] = {	0x00, 0x00, 0xbb, 0xaa, 0x00, 0x00, 0xbb, 0xaa, 0x77,
-			  0x00, 0xff, 0xee, 0x00, 0x00, 0xee, 0xff, 0xaa, 0x00 };
-BYTE _blue[] = { 	0x00, 0x00, 0x00, 0x00, 0xcc, 0xbb, 0xbb, 0xaa, 0x77,
-		      0x00, 0x00, 0x00, 0xff, 0xff, 0xee, 0xff, 0xaa, 0x00};
-
-HBRUSH _colors[18];
-
 static LONG lColor[SPLITBAR_WIDTH] =
 {
     DW_CLR_BLACK,
@@ -72,9 +70,6 @@
     DW_CLR_WHITE
 };
 
-void _resize_notebook_page(HWND handle, int pageid);
-int _lookup_icon(HWND handle, HICON hicon, int type);
-
 #ifdef NO_SIGNALS
 #define USE_FILTER
 #else
@@ -89,7 +84,6 @@
 } SignalHandler;
 
 SignalHandler *Root = NULL;
-int _index;
 
 typedef struct
 {
@@ -98,145 +92,26 @@
 
 } SignalList;
 
-/* List of signals and their equivilent Win32 message */
+/* List of signals and their equivilent OS/2 message */
 #define SIGNALMAX 14
 
 SignalList SignalTranslate[SIGNALMAX] = {
 	{ WM_SIZE, "configure_event" },
 	{ WM_CHAR, "key_press_event" },
-	{ WM_LBUTTONDOWN, "button_press_event" },
-	{ WM_LBUTTONUP, "button_release_event" },
+	{ WM_BUTTON1DOWN, "button_press_event" },
+	{ WM_BUTTON1UP, "button_release_event"},
 	{ WM_MOUSEMOVE, "motion_notify_event" },
 	{ WM_CLOSE, "delete_event" },
 	{ WM_PAINT, "expose_event" },
 	{ WM_COMMAND, "clicked" },
-	{ NM_DBLCLK, "container-select" },
-	{ NM_RCLICK, "container-context" },
-	{ LBN_SELCHANGE, "item-select" },
-	{ TVN_SELCHANGED, "tree-select" },
+	{ CN_ENTER, "container-select" },
+	{ CN_CONTEXTMENU, "container-context" },
+	{ LN_SELECT, "item-select" },
+	{ CN_EMPHASIS, "tree-select" },
 	{ WM_SETFOCUS, "set-focus" },
 	{ WM_USER+1, "lose-focus" }
 };
 
-#ifdef BUILD_DLL
-void Win32_Set_Instance(HINSTANCE hInstance)
-{
-	DWInstance = hInstance;
-}
-#else
-char **_convertargs(int *count, char *start)
-{
-	char *tmp, *argstart, **argv;
-	int loc = 0, inquotes = 0;
-
-	(*count) = 1;
-
-	tmp = start;
-
-	/* Count the number of entries */
-	if(*start)
-	{
-		(*count)++;
-
-		while(*tmp)
-		{
-			if(*tmp == '"' && inquotes)
-				inquotes = 0;
-			else if(*tmp == '"' && !inquotes)
-				inquotes = 1;
-			else if(*tmp == ' ' && !inquotes)
-			{
-				/* Push past any white space */
-				while(*(tmp+1) == ' ')
-					tmp++;
-				/* If we aren't at the end of the command
-				 * line increment the count.
-				 */
-				if(*(tmp+1))
-					(*count)++;
-			}
-			tmp++;
-		}
-	}
-
-	argv = (char **)malloc(sizeof(char *) * ((*count)+1));
-	argv[0] = malloc(260);
-	GetModuleFileName(DWInstance, argv[0], 260);
-
-	argstart = tmp = start;
-
-	if(*start)
-	{
-		loc = 1;
-
-		while(*tmp)
-		{
-			if(*tmp == '"' && inquotes)
-			{
-				*tmp = 0;
-				inquotes = 0;
-			}
-			else if(*tmp == '"' && !inquotes)
-			{
-				argstart = tmp+1;
-				inquotes = 1;
-			}
-			else if(*tmp == ' ' && !inquotes)
-			{
-				*tmp = 0;
-				argv[loc] = strdup(argstart);
-
-				/* Push past any white space */
-				while(*(tmp+1) == ' ')
-					tmp++;
-
-				/* Move the start pointer */
-				argstart = tmp+1;
-
-				/* If we aren't at the end of the command
-				 * line increment the count.
-				 */
-				if(*(tmp+1))
-					loc++;
-			}
-			tmp++;
-		}
-		if(*argstart)
-			argv[loc] = strdup(argstart);
-	}
-	argv[loc+1] = NULL;
-	return argv;
-}
-
-/* Ok this is a really big hack but what the hell ;) */
-int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
-{
-	char **argv;
-    int argc;
-
-	DWInstance = hInstance;
-
-	argv = _convertargs(&argc, lpCmdLine);
-
-	return main(argc, argv);
-}
-#endif
-
-/* This should return true for WinNT/2K/XP and false on Win9x */
-int IsWinNT(void)
-{
-	static int isnt = -1;
-
-	if(isnt == -1)
-	{
-		if (GetVersion() < 0x80000000)
-			isnt = 1;
-		else
-			isnt = 0;
-	}
-	return isnt;
-}
-
 /* This function adds a signal handler callback into the linked list.
  */
 void _new_signal(ULONG message, HWND window, void *signalfunction, void *data)
@@ -285,12 +160,65 @@
 }
 #endif
 
+typedef struct _CNRITEM
+{
+	MINIRECORDCORE rc;
+	HPOINTER       hptrIcon;
+	PVOID          user;
+
+} CNRITEM, *PCNRITEM;
+
+
+/* This function changes the owner of buttons in to the
+ * dynamicwindows handle to fix a problem in notebooks.
+ */
+void _fix_button_owner(HWND handle, HWND dw)
+{
+	HENUM henum;
+	HWND child;
+
+	henum = WinBeginEnumWindows(handle);
+	while((child = WinGetNextWindow(henum)) != NULLHANDLE)
+	{
+		char tmpbuf[100];
+
+		WinQueryClassName(child, 99, tmpbuf);
+
+		if(strncmp(tmpbuf, "#3", 3)==0 && dw)  /* Button */
+			WinSetOwner(child, dw);
+		else if(strncmp(tmpbuf, "dynamicwindows", 14) == 0)
+			dw = child;
+
+		_fix_button_owner(child, dw);
+	}
+	WinEndEnumWindows(henum);
+	return;
+}
+
+void _disconnect_windows(HWND handle)
+{
+	HENUM henum;
+	HWND child;
+
+#ifndef NO_SIGNALS
+	dw_signal_disconnect_by_window(handle);
+#endif
+
+	henum = WinBeginEnumWindows(handle);
+	while((child = WinGetNextWindow(henum)) != NULLHANDLE)
+		_disconnect_windows(child);
+
+	WinEndEnumWindows(henum);
+}
+
 /* This function removes and handlers on windows and frees
  * the user memory allocated to it.
  */
-BOOL CALLBACK _free_window_memory(HWND handle, LPARAM lParam)
-{
-	void *ptr = (void *)GetWindowLong(handle, GWL_USERDATA);
+void _free_window_memory(HWND handle)
+{
+	HENUM henum;
+	HWND child;
+	void *ptr = (void *)WinQueryWindowPtr(handle, QWP_USER);
 
 #ifndef NO_SIGNALS
 	dw_signal_disconnect_by_window(handle);
@@ -298,10 +226,16 @@
 
 	if(ptr)
 	{
-		SetWindowLong(handle, GWL_USERDATA, 0);
+		WinSetWindowPtr(handle, QWP_USER, 0);
 		free(ptr);
 	}
-	return TRUE;
+
+	henum = WinBeginEnumWindows(handle);
+	while((child = WinGetNextWindow(henum)) != NULLHANDLE)
+		_free_window_memory(child);
+
+	WinEndEnumWindows(henum);
+	return;
 }
 
 /* This function returns 1 if the window (widget) handle
@@ -314,46 +248,25 @@
 	if(!handle)
 		return 0;
 
-	if(!IsWindowEnabled(handle))
+	if(!WinIsWindowEnabled(handle))
 		return 0;
 
-	GetClassName(handle, tmpbuf, 99);
+	WinQueryClassName(handle, 99, tmpbuf);
 
 	/* These are the window classes which can
 	 * obtain input focus.
 	 */
-	if(strnicmp(tmpbuf, EDITCLASSNAME, strlen(EDITCLASSNAME))==0 ||  /* Entryfield */
-	   strnicmp(tmpbuf, BUTTONCLASSNAME, strlen(BUTTONCLASSNAME))==0 ||  /* Button */
-	   strnicmp(tmpbuf, COMBOBOXCLASSNAME, strlen(COMBOBOXCLASSNAME))==0 ||  /* Combobox */
-	   strnicmp(tmpbuf, LISTBOXCLASSNAME, strlen(LISTBOXCLASSNAME))==0 ||  /* List box */
-	   strnicmp(tmpbuf, UPDOWN_CLASS, strlen(UPDOWN_CLASS))==0 || /* Spinbutton */
-	   strnicmp(tmpbuf, WC_LISTVIEW, strlen(WC_LISTVIEW))== 0)  /* Container */
+	if(strncmp(tmpbuf, "#2", 3)==0 ||  /* Combobox */
+	   strncmp(tmpbuf, "#3", 3)==0 ||  /* Button */
+	   strncmp(tmpbuf, "#6", 3)==0 ||  /* Entryfield */
+	   strncmp(tmpbuf, "#7", 3)==0 ||  /* List box */
+	   strncmp(tmpbuf, "#10", 3)==0 || /* MLE */
+	   strncmp(tmpbuf, "#32", 3)==0 || /* Spinbutton */
+	   strncmp(tmpbuf, "#37", 3)== 0)  /* Container */
 		return 1;
 	return 0;
 }
 
-HWND _normalize_handle(HWND handle)
-{
-	char tmpbuf[100] = "";
-
-	GetClassName(handle, tmpbuf, 99);
-	if(strnicmp(tmpbuf, UPDOWN_CLASS, strlen(UPDOWN_CLASS))==0) /* Spinner */
-	{
-		ColorInfo *cinfo = (ColorInfo *)GetWindowLong(handle, GWL_USERDATA);
-
-		if(cinfo && cinfo->buddy)
-			return cinfo->buddy;
-	}
-	if(strnicmp(tmpbuf, COMBOBOXCLASSNAME, strlen(COMBOBOXCLASSNAME))==0) /* Combobox */
-	{
-		ColorInfo *cinfo = (ColorInfo *)GetWindowLong(handle, GWL_USERDATA);
-
-		if(cinfo && cinfo->buddy)
-			return cinfo->buddy;
-	}
-	return handle;
-}
-
 int _focus_check_box(Box *box, HWND handle, int start, HWND defaultitem)
 {
 	int z;
@@ -367,7 +280,7 @@
 	if(start == 2)
 	{
 		if(lasthwnd)
-			SetFocus(lasthwnd);
+			WinSetFocus(HWND_DESKTOP, lasthwnd);
 		return 0;
 	}
 
@@ -382,69 +295,138 @@
 		firsthwnd = 0;
 	}
 
-	for(z=box->count-1;z>-1;z--)
+	/* Vertical boxes are inverted on OS/2 */
+	if(box->type == BOXVERT)
 	{
-		if(box->items[z].type == TYPEBOX)
+		for(z=0;z<box->count;z++)
 		{
-			Box *thisbox = (Box *)GetWindowLong(box->items[z].hwnd, GWL_USERDATA);
-
-			if(thisbox && _focus_check_box(thisbox, handle, start == 3 ? 3 : 0, defaultitem))
-				return 1;
-		}
-		else
-		{
-			if(box->items[z].hwnd == handle)
+			if(box->items[z].type == TYPEBOX)
 			{
-				if(lasthwnd == handle && firsthwnd)
-					SetFocus(firsthwnd);
-				else if(lasthwnd == handle && !firsthwnd)
-					finish_searching = 1;
-				else
-					SetFocus(lasthwnd);
-
-				/* If we aren't looking for the last handle,
-				 * return immediately.
-				 */
-				if(!finish_searching)
+				Box *thisbox = WinQueryWindowPtr(box->items[z].hwnd, QWP_USER);
+
+				if(thisbox && _focus_check_box(thisbox, handle, start == 3 ? 3 : 0, defaultitem))
 					return 1;
 			}
-			if(_validate_focus(box->items[z].hwnd))
-			{
-				/* Start is 3 when we are looking for the
-				 * first valid item in the layout.
-				 */
-				if(start == 3)
-				{
-					if(!defaultitem || (defaultitem && box->items[z].hwnd == defaultitem))
-					{
-						SetFocus(_normalize_handle(box->items[z].hwnd));
-						return 1;
-					}
-				}
-
-				if(!firsthwnd)
-					firsthwnd = _normalize_handle(box->items[z].hwnd);
-
-				lasthwnd = _normalize_handle(box->items[z].hwnd);
-			}
 			else
 			{
-				char tmpbuf[100] = "";
-
-				GetClassName(box->items[z].hwnd, tmpbuf, 99);
-
-				if(strnicmp(tmpbuf, WC_TABCONTROL, strlen(WC_TABCONTROL))==0) /* Notebook */
+				if(box->items[z].hwnd == handle)
+				{
+					if(lasthwnd == handle && firsthwnd)
+						WinSetFocus(HWND_DESKTOP, firsthwnd);
+					else if(lasthwnd == handle && !firsthwnd)
+						finish_searching = 1;
+					else
+						WinSetFocus(HWND_DESKTOP, lasthwnd);
+
+					/* If we aren't looking for the last handle,
+					 * return immediately.
+					 */
+					if(!finish_searching)
+						return 1;
+				}
+				if(_validate_focus(box->items[z].hwnd))
 				{
-					NotebookPage **array = (NotebookPage **)GetWindowLong(box->items[z].hwnd, GWL_USERDATA);
-					int pageid = TabCtrl_GetCurSel(box->items[z].hwnd);
-
-					if(pageid > -1 && array && array[pageid])
+					/* Start is 3 when we are looking for the
+					 * first valid item in the layout.
+					 */
+					if(start == 3)
+					{
+						if(!defaultitem || (defaultitem && defaultitem == box->items[z].hwnd))
+						{
+							WinSetFocus(HWND_DESKTOP, box->items[z].hwnd);
+							return 1;
+						}
+					}
+
+					if(!firsthwnd)
+						firsthwnd = box->items[z].hwnd;
+
+					lasthwnd = box->items[z].hwnd;
+				}
+				else
+				{
+					char tmpbuf[100] = "";
+
+					WinQueryClassName(box->items[z].hwnd, 99, tmpbuf);
+					if(strncmp(tmpbuf, "#40", 3)==0) /* Notebook */
 					{
 						Box *notebox;
-
-						if(array[pageid]->hwnd)
+						HWND page = (HWND)WinSendMsg(box->items[z].hwnd, BKM_QUERYPAGEWINDOWHWND,
+													 (MPARAM)dw_notebook_page_query(box->items[z].hwnd), 0);
+
+						if(page)
 						{
-							notebox = (Box *)GetWindowLong(array[pageid]->hwnd, GWL_USERDATA);
+							notebox = (Box *)WinQueryWindowPtr(page, QWP_USER);
+
+							if(notebox && _focus_check_box(notebox, handle, start == 3 ? 3 : 0, defaultitem))
+								return 1;
+						}
+					}
+				}
+			}
+		}
+	}
+	else
+	{
+		for(z=box->count-1;z>-1;z--)
+		{
+			if(box->items[z].type == TYPEBOX)
+			{
+				Box *thisbox = WinQueryWindowPtr(box->items[z].hwnd, QWP_USER);
+
+				if(thisbox && _focus_check_box(thisbox, handle, start == 3 ? 3 : 0, defaultitem))
+					return 1;
+			}
+			else
+			{
+				if(box->items[z].hwnd == handle)
+				{
+					if(lasthwnd == handle && firsthwnd)
+						WinSetFocus(HWND_DESKTOP, firsthwnd);
+					else if(lasthwnd == handle && !firsthwnd)
+						finish_searching = 1;
+					else
+						WinSetFocus(HWND_DESKTOP, lasthwnd);
+
+					/* If we aren't looking for the last handle,
+					 * return immediately.
+					 */
+					if(!finish_searching)
+						return 1;
+				}
+				if(_validate_focus(box->items[z].hwnd))
+				{
+					/* Start is 3 when we are looking for the
+					 * first valid item in the layout.
+					 */
+					if(start == 3)
+					{
+						if(!defaultitem || (defaultitem && defaultitem == box->items[z].hwnd))
+						{
+							WinSetFocus(HWND_DESKTOP, box->items[z].hwnd);
+							return 1;
+						}
+					}
+
+					if(!firsthwnd)
+						firsthwnd = box->items[z].hwnd;
+
+					lasthwnd = box->items[z].hwnd;
+				}
+				else
+				{
+					char tmpbuf[100] = "";
+
+					WinQueryClassName(box->items[z].hwnd, 99, tmpbuf);
+					if(strncmp(tmpbuf, "#40", 3)==0) /* Notebook */
+					{
+						Box *notebox;
+						HWND page = (HWND)WinSendMsg(box->items[z].hwnd, BKM_QUERYPAGEWINDOWHWND,
+													 (MPARAM)dw_notebook_page_query(box->items[z].hwnd), 0);
+
+						if(page)
+						{
+							notebox = (Box *)WinQueryWindowPtr(page, QWP_USER);
 
 							if(notebox && _focus_check_box(notebox, handle, start == 3 ? 3 : 0, defaultitem))
 								return 1;
@@ -460,27 +442,20 @@
 /* This function finds the first widget in the
  * layout and moves the current focus to it.
  */
-void _initial_focus(HWND handle)
-{
-	Box *thisbox;
-	char tmpbuf[100];
-
-	if(!handle)
-		return;
-
-	GetClassName(handle, tmpbuf, 99);
-
-	if(strnicmp(tmpbuf, ClassName, strlen(ClassName))!=0)
-		return;
-
-
-	if(handle)
-		thisbox = (Box *)GetWindowLong(handle, GWL_USERDATA);
+int _initial_focus(HWND handle)
+{
+	Box *thisbox = NULL;
+	HWND box;
+
+	box = WinWindowFromID(handle, FID_CLIENT);
+	if(box)
+		thisbox = WinQueryWindowPtr(box, QWP_USER);
+	else
+		return 1;
 
 	if(thisbox)
-	{
 		_focus_check_box(thisbox, handle, 3, thisbox->defaultitem);
-	}
+	return 0;
 }
 
 /* This function finds the current widget in the
@@ -489,16 +464,20 @@
 void _shift_focus(HWND handle)
 {
 	Box *thisbox;
-
-	HWND box, lastbox = GetParent(handle);
+	HWND box, lastbox = WinQueryWindow(handle, QW_PARENT);
 
 	/* Find the toplevel window */
-	while((box = GetParent(lastbox)))
+	while((box = WinQueryWindow(lastbox, QW_PARENT)) > 0x80000001)
 	{
 		lastbox = box;
 	}
 
-	thisbox = (Box *)GetWindowLong(lastbox, GWL_USERDATA);
+	box = WinWindowFromID(lastbox, FID_CLIENT);
+	if(box)
+		thisbox = WinQueryWindowPtr(box, QWP_USER);
+    else
+		thisbox = WinQueryWindowPtr(lastbox, QWP_USER);
+
 	if(thisbox)
 	{
 		if(_focus_check_box(thisbox, handle, 1, 0)  == 0)
@@ -512,14 +491,73 @@
  */
 void _ResetWindow(HWND hwndFrame)
 {
-	RECT rcl;
-
-	GetWindowRect(hwndFrame, &rcl);
-	SetWindowPos(hwndFrame, HWND_TOP, 0, 0, rcl.right - rcl.left,
-				 rcl.bottom - rcl.top - 1, SWP_NOMOVE | SWP_NOZORDER);
-	SetWindowPos(hwndFrame, HWND_TOP, 0, 0, rcl.right - rcl.left,
-				 rcl.bottom - rcl.top, SWP_NOMOVE | SWP_NOZORDER);
-}
+	SWP swp;
+
+	WinQueryWindowPos(hwndFrame, &swp);
+	WinSetWindowPos(hwndFrame, HWND_TOP, 0, 0, swp.cx, swp.cy-1, SWP_SIZE);
+	WinSetWindowPos(hwndFrame, HWND_TOP, 0, 0, swp.cx, swp.cy, SWP_SIZE);
+}
+
+/* This function will recursively search a box and add up the total height of it */
+void _count_size(HWND box, int type, int *xsize, int *xorigsize)
+{
+	int size = 0, origsize = 0, z;
+	Box *tmp = WinQueryWindowPtr(box, QWP_USER);
+
+	if(!tmp)
+	{
+		*xsize = *xorigsize = 0;
+		return;
+	}
+
+	if(type == tmp->type)
+	{
+		/* If the box is going in the direction we want, then we
+		 * return the entire sum of the items.
+		 */
+		for(z=0;z<tmp->count;z++)
+		{
+			if(tmp->items[z].type == TYPEBOX)
+			{
+				int s, os;
+
+				_count_size(tmp->items[z].hwnd, type, &s, &os);
+				size += s;
+				origsize += os;
+			}
+			else
+			{
+				size += (type == BOXHORZ ? tmp->items[z].width : tmp->items[z].height);
+				origsize += (type == BOXHORZ ? tmp->items[z].origwidth : tmp->items[z].origheight);
+			}
+		}
+	}
+	else
+	{
+		/* If the box is not going in the direction we want, then we only
+		 * want to return the maximum value.
+		 */
+		int tmpsize = 0, tmporigsize = 0;
+
+		for(z=0;z<tmp->count;z++)
+		{
+			if(tmp->items[z].type == TYPEBOX)
+				_count_size(tmp->items[z].hwnd, type, &tmpsize, &tmporigsize);
+			else
+			{
+				tmpsize = (type == BOXHORZ ? tmp->items[z].width : tmp->items[z].height);
+				tmporigsize = (type == BOXHORZ ? tmp->items[z].origwidth : tmp->items[z].origheight);
+			}
+
+			if(tmpsize > size)
+				size = tmpsize;
+		}
+	}
+
+	*xsize = size;
+	*xorigsize = origsize;
+}
+
 
 /* Function: TrackRectangle
  * Abstract: Tracks given rectangle.
@@ -531,9 +569,8 @@
 
 BOOL _TrackRectangle(HWND hwndBase, RECTL* rclTrack, RECTL* rclBounds)
 {
-	ULONG rc = 0;
-#if 0
 	TRACKINFO track;
+	APIRET rc;
 
 	track.cxBorder = 1;
 	track.cyBorder = 1;
@@ -559,10 +596,10 @@
 
 	track.rclTrack = *rclTrack;
 
-	MapWindowPoints(hwndBase,
-					HWND_DESKTOP,
-					(PPOINT)&track.rclTrack,
-					2);
+	WinMapWindowPoints(hwndBase,
+					   HWND_DESKTOP,
+					   (PPOINTL)&track.rclTrack,
+					   2);
 
 	track.ptlMinTrackSize.x = track.rclTrack.xRight
 		- track.rclTrack.xLeft;
@@ -580,7 +617,6 @@
 	if(rc)
 		*rclTrack = track.rclTrack;
 
-#endif
 	return rc;
 }
 
@@ -605,7 +641,7 @@
 		if(thisbox->items[z].type == TYPEBOX)
 		{
 			int initialx, initialy;
-			Box *tmp = (Box *)GetWindowLong(thisbox->items[z].hwnd, GWL_USERDATA);
+			Box *tmp = WinQueryWindowPtr(thisbox->items[z].hwnd, QWP_USER);
 
 			initialx = x - (*usedx);
 			initialy = y - (*usedy);
@@ -651,6 +687,7 @@
 						reopen();
 					}
 #endif
+
 					if(thisbox->type == BOXVERT)
 					{
 						if((thisbox->items[z].width-((thisbox->items[z].pad*2)+(tmp->pad*2)))!=0)
@@ -722,7 +759,7 @@
 
 			if(thisbox->items[z].type == TYPEBOX)
 			{
-				Box *tmp = (Box *)GetWindowLong(thisbox->items[z].hwnd, GWL_USERDATA);
+				Box *tmp = WinQueryWindowPtr(thisbox->items[z].hwnd, QWP_USER);
 
 				if(tmp)
 				{
@@ -834,7 +871,7 @@
 			/* Run this code segment again to finalize the sized after setting uxmax/uymax values. */
 			if(thisbox->items[z].type == TYPEBOX)
 			{
-				Box *tmp = (Box *)GetWindowLong(thisbox->items[z].hwnd, GWL_USERDATA);
+				Box *tmp = WinQueryWindowPtr(thisbox->items[z].hwnd, QWP_USER);
 
 				if(tmp)
 				{
@@ -896,58 +933,34 @@
 				if(thisbox->items[z].hsize != SIZEEXPAND)
 					vectorx = 0;
 
-				GetClassName(handle, tmpbuf, 99);
-
-				if(strnicmp(tmpbuf, COMBOBOXCLASSNAME, strlen(COMBOBOXCLASSNAME))==0)
-				{
-					/* Handle special case Combobox */
-					MoveWindow(handle, currentx + pad, currenty + pad,
-							   width + vectorx, (height + vectory) + 400, TRUE);
-				}
-				else if(strnicmp(tmpbuf, UPDOWN_CLASS, strlen(UPDOWN_CLASS))==0)
+				WinQueryClassName(handle, 99, tmpbuf);
+
+				if(strncmp(tmpbuf, "#2", 3)==0)
 				{
-					/* Handle special case Spinbutton */
-					ColorInfo *cinfo = (ColorInfo *)GetWindowLong(handle, GWL_USERDATA);
-
-					MoveWindow(handle, currentx + pad + ((width + vectorx) - 20), currenty + pad,
-							   20, height + vectory, TRUE);
-
-					if(cinfo)
-					{
-						MoveWindow(cinfo->buddy, currentx + pad, currenty + pad,
-								   (width + vectorx) - 20, height + vectory, TRUE);
-					}
+					/* Make the combobox big enough to drop down. :) */
+					WinSetWindowPos(handle, HWND_TOP, currentx + pad, (currenty + pad) - 100,
+									width + vectorx, (height + vectory) + 100, SWP_MOVE | SWP_SIZE | SWP_ZORDER);
+				}
+				else if(strncmp(tmpbuf, "#6", 3)==0)
+				{
+					/* Entryfields on OS/2 have a thick border that isn't on Windows and GTK */
+					WinSetWindowPos(handle, HWND_TOP, (currentx + pad) + 3, (currenty + pad) + 3,
+									(width + vectorx) - 6, (height + vectory) - 6, SWP_MOVE | SWP_SIZE | SWP_ZORDER);
 				}
 				else
 				{
-					/* Everything else */
-					MoveWindow(handle, currentx + pad, currenty + pad,
-							   width + vectorx, height + vectory, TRUE);
+					WinSetWindowPos(handle, HWND_TOP, currentx + pad, currenty + pad,
+									width + vectorx, height + vectory, SWP_MOVE | SWP_SIZE | SWP_ZORDER);
 					if(thisbox->items[z].type == TYPEBOX)
 					{
-						Box *boxinfo = (Box *)GetWindowLong(handle, GWL_USERDATA);
+						Box *boxinfo = WinQueryWindowPtr(handle, QWP_USER);
 
 						if(boxinfo && boxinfo->grouphwnd)
-							MoveWindow(boxinfo->grouphwnd, 0, 0,
-									   width + vectorx, height + vectory, TRUE);
+							WinSetWindowPos(boxinfo->grouphwnd, HWND_TOP, 0, 0,
+											width + vectorx, height + vectory, SWP_MOVE | SWP_SIZE);
 
 					}
-				}
-
-				/* Notebook dialog requires additional processing */
-				if(strncmp(tmpbuf, WC_TABCONTROL, strlen(WC_TABCONTROL))==0)
-				{
-					RECT rect;
-					NotebookPage **array = (NotebookPage **)GetWindowLong(handle, GWL_USERDATA);
-					int pageid = TabCtrl_GetCurSel(handle);
-
-					if(pageid > -1 && array && array[pageid])
-					{
-						GetClientRect(handle,&rect);
-						TabCtrl_AdjustRect(handle,FALSE,&rect);
-						MoveWindow(array[pageid]->hwnd, rect.left, rect.top,
-								   rect.right - rect.left, rect.bottom-rect.top, TRUE);
-					}
+
 				}
 
 #ifdef DWDEBUG
@@ -972,7 +985,7 @@
 	{
 		if(thisbox)
 		{
-			int usedx = 0, usedy = 0, depth = 0, usedpadx = 0, usedpady = 0;
+			int usedx = 0, usedy = 0, usedpadx = 0, usedpady = 0, depth = 0;
 
 			_resize_box(thisbox, &depth, x, y, &usedx, &usedy, 1, &usedpadx, &usedpady);
 
@@ -988,10 +1001,9 @@
 			reopen();
 #endif
 
-			usedpadx = usedpady = usedx = usedy = depth = 0;
+			usedx = usedy = usedpadx = usedpady = depth = 0;
 
 			_resize_box(thisbox, &depth, x, y, &usedx, &usedy, 2, &usedpadx, &usedpady);
-
 #ifdef DWDEBUG
 			fprintf(f, "WM_SIZE Resize Box Pass 2\r\nx = %d, y = %d, usedx = %d, usedy = %d, usedpadx = %d, usedpady = %d\r\n",
 					x, y, usedx, usedy, usedpadx, usedpady);
@@ -1001,583 +1013,229 @@
 	}
 }
 
-/* The main window procedure for Dynamic Windows, all the resizing code is done here. */
-BOOL CALLBACK _wndproc(HWND hWnd, UINT msg, WPARAM mp1, LPARAM mp2)
-{
-	int result = -1;
-	static int command_active = 0;
-#ifndef NO_SIGNALS
-	SignalHandler *tmp = Root;
-#endif
-	void (* windowfunc)(PVOID);
-	ULONG origmsg = msg;
-
-	if(msg == WM_RBUTTONDOWN || msg == WM_MBUTTONDOWN)
-		msg = WM_LBUTTONDOWN;
-	if(msg == WM_RBUTTONUP || msg == WM_MBUTTONUP)
-		msg = WM_LBUTTONUP;
-
-	if(filterfunc)
-		result = filterfunc(hWnd, msg, mp1, mp2);
-
-#ifndef NO_SIGNALS
-	if(result == -1)
+/* This procedure handles WM_QUERYTRACKINFO requests from the frame */
+MRESULT EXPENTRY _sizeproc(HWND hWnd, ULONG msg, MPARAM mp1, MPARAM mp2)
+{
+	PFNWP *blah = WinQueryWindowPtr(hWnd, QWP_USER);
+
+	switch(msg)
 	{
-		/* Avoid infinite recursion */
-		command_active = 1;
-
-		/* Find any callbacks for this function */
-		while(tmp)
+	case WM_QUERYTRACKINFO:
 		{
-			if(tmp->message == msg || msg == WM_COMMAND || msg == WM_NOTIFY || tmp->message == WM_USER+1)
+			if(blah && *blah)
 			{
-				switch(msg)
-				{
-				case WM_SETFOCUS:
-					{
-						int (*setfocusfunc)(HWND, void *) = (int (*)(HWND, void *))tmp->signalfunction;
-
-						if(hWnd == tmp->window)
-						{
-							result = setfocusfunc(tmp->window, tmp->data);
-							tmp = NULL;
-						}
-					}
-					break;
-				case WM_SIZE:
-					{
-						int (*sizefunc)(HWND, int, int, void *) = tmp->signalfunction;
-
-						if(hWnd == tmp->window)
-						{
-							result = sizefunc(tmp->window, LOWORD(mp2), HIWORD(mp2), tmp->data);
-							tmp = NULL;
-						}
-					}
-					break;
-				case WM_LBUTTONDOWN:
-					{
-						POINTS pts = MAKEPOINTS(mp2);
-						int (*buttonfunc)(HWND, int, int, int, void *) = (int (*)(HWND, int, int, int, void *))tmp->signalfunction;
-
-						if(hWnd == tmp->window)
-						{
-							int button;
-
-							switch(origmsg)
-							{
-							case WM_LBUTTONDOWN:
-								button = 1;
-								break;
-							case WM_RBUTTONDOWN:
-								button = 2;
-								break;
-							case WM_MBUTTONDOWN:
-								button = 3;
-								break;
-							}
-							result = buttonfunc(tmp->window, pts.x, pts.y, button, tmp->data);
-							tmp = NULL;
-						}
-					}
-					break;
-				case WM_LBUTTONUP:
-					{
-						POINTS pts = MAKEPOINTS(mp2);
-						int (*buttonfunc)(HWND, int, int, int, void *) = (int (*)(HWND, int, int, int, void *))tmp->signalfunction;
-
-						if(hWnd == tmp->window)
-						{
-							int button;
-
-							switch(origmsg)
-							{
-							case WM_LBUTTONUP:
-								button = 1;
-								break;
-							case WM_RBUTTONUP:
-								button = 2;
-								break;
-							case WM_MBUTTONUP:
-								button = 3;
-								break;
-							}
-							result = buttonfunc(tmp->window, pts.x, pts.y, button, tmp->data);
-							tmp = NULL;
-						}
-					}
-					break;
-				case WM_MOUSEMOVE:
-					{
-						POINTS pts = MAKEPOINTS(mp2);
-						int (*motionfunc)(HWND, int, int, int, void *) = (int (*)(HWND, int, int, int, void *))tmp->signalfunction;
-
-						if(hWnd == tmp->window)
-						{
-							int keys = 0;
-
-							if (mp1 & MK_LBUTTON)
-								keys = DW_BUTTON1_MASK;
-							if (mp1 & MK_RBUTTON)
-								keys |= DW_BUTTON2_MASK;
-							if (mp1 & MK_MBUTTON)
-								keys |= DW_BUTTON3_MASK;
-
-							result = motionfunc(tmp->window, pts.x, pts.y, keys, tmp->data);
-							tmp = NULL;
-						}
-					}
-					break;
-				case WM_CHAR:
-					{
-						int (*keypressfunc)(HWND, int, void *) = tmp->signalfunction;
-
-						if(hWnd == tmp->window)
-						{
-							result = keypressfunc(tmp->window, LOWORD(mp2), tmp->data);
-							tmp = NULL;
-						}
-					}
-					break;
-				case WM_CLOSE:
-					{
-						int (*closefunc)(HWND, void *) = tmp->signalfunction;
-
-						if(hWnd == tmp->window)
-						{
-							result = closefunc(tmp->window, tmp->data);
-							tmp = NULL;
-						}
-					}
-					break;
-				case WM_PAINT:
-					{
-						PAINTSTRUCT ps;
-						DWExpose exp;
-						int (*exposefunc)(HWND, DWExpose *, void *) = tmp->signalfunction;
-
-						if(hWnd == tmp->window)
-						{
-							BeginPaint(hWnd, &ps);
-							exp.x = ps.rcPaint.left;
-							exp.y = ps.rcPaint.top;
-							exp.width = ps.rcPaint.right - ps.rcPaint.left;
-							exp.height = ps.rcPaint.bottom - ps.rcPaint.top;
-							result = exposefunc(hWnd, &exp, tmp->data);
-							EndPaint(hWnd, &ps);
-						}
-					}
-					break;
-				case WM_NOTIFY:
-					{
-						if(tmp->message == TVN_SELCHANGED || tmp->message == NM_RCLICK)
-						{
-							NMTREEVIEW FAR *tem=(NMTREEVIEW FAR *)mp2;
-							char tmpbuf[100];
-
-							GetClassName(tem->hdr.hwndFrom, tmpbuf, 99);
-
-							if(strnicmp(tmpbuf, WC_TREEVIEW, strlen(WC_TREEVIEW))==0)
-							{
-								if(tem->hdr.code == TVN_SELCHANGED && tmp->message == TVN_SELCHANGED)
-								{
-									if(tmp->window == tem->hdr.hwndFrom)
-									{
-										int (*treeselectfunc)(HWND, HWND, char *, void *, void *) = tmp->signalfunction;
-										TVITEM tvi;
-										void **ptrs;
-
-										tvi.mask = TVIF_HANDLE;
-										tvi.hItem = tem->itemNew.hItem;
-
-										TreeView_GetItem(tmp->window, &tvi);
-
-										ptrs = (void **)tvi.lParam;
-										if(ptrs)
-											result = treeselectfunc(tmp->window, (HWND)tem->itemNew.hItem, (char *)ptrs[0], (void *)ptrs[1], tmp->data);
-
-										tmp = NULL;
-									}
-								}
-								else if(tem->hdr.code == NM_RCLICK && tmp->message == NM_RCLICK)
-								{
-									if(tmp->window == tem->hdr.hwndFrom)
-									{
-										int (*containercontextfunc)(HWND, char *, int, int, void *, void *) = tmp->signalfunction;
-										HTREEITEM hti;
-										TVITEM tvi;
-										TVHITTESTINFO thi;
-										void **ptrs = NULL;
-										LONG x, y;
-
-										dw_pointer_query_pos(&x, &y);
-
-										thi.pt.x = x;
-										thi.pt.y = y;
-
-										MapWindowPoints(HWND_DESKTOP, tmp->window, &thi.pt, 1);
-
-										hti = TreeView_HitTest(tmp->window, &thi);
-
-										if(hti)
-										{
-											tvi.mask = TVIF_HANDLE;
-											tvi.hItem = hti;
-
-											TreeView_GetItem(tmp->window, &tvi);
-											dw_tree_item_select(tmp->window, (HWND)hti);
-
-											ptrs = (void **)tvi.lParam;
-
-										}
-										containercontextfunc(tmp->window, ptrs ? (char *)ptrs[0] : NULL, x, y, tmp->data, ptrs ? ptrs[1] : NULL);
-										tmp = NULL;
-									}
-								}
-							}
-						}
-					}
-					break;
-				case WM_COMMAND:
-					{
-						int (*clickfunc)(HWND, void *) = tmp->signalfunction;
-						HWND command;
-						ULONG passthru = (ULONG)LOWORD(mp1);
-						ULONG message = HIWORD(mp1);
-
-						command = (HWND)passthru;
-
-						if(message == LBN_SELCHANGE || message == CBN_SELCHANGE)
-						{
-							int (*listboxselectfunc)(HWND, int, void *) = tmp->signalfunction;
-
-							if(tmp->message == LBN_SELCHANGE && tmp->window == (HWND)mp2)
-							{
-								result = listboxselectfunc(tmp->window, dw_listbox_selected(tmp->window), tmp->data);
-								tmp = NULL;
-							}
-						} /* Make sure it's the right window, and the right ID */
-						else if(tmp->window < (HWND)65536 && command == tmp->window)
-						{
-							result = clickfunc(tmp->window, tmp->data);
-							tmp = NULL;
-						}
-					}
-					break;
-				}
-			}
-			if(tmp)
-				tmp = tmp->next;
-		}
-		command_active = 0;
-	}
-#endif
-
-	/* Now that any handlers are done... do normal processing */
-	switch( msg )
-	{
-	case WM_PAINT:
-		{
-			PAINTSTRUCT ps;
-
-			BeginPaint(hWnd, &ps);
-			EndPaint(hWnd, &ps);
-		}
-		break;
-	case WM_SIZE:
-		{
-			static int lastx = -1, lasty = -1;
-			static HWND lasthwnd = 0;
-
-			if(lastx != LOWORD(mp2) || lasty != HIWORD(mp2) || lasthwnd != hWnd)
-			{
-				Box *mybox = (Box *)GetWindowLong(hWnd, GWL_USERDATA);
-
-				if(mybox && mybox->count)
-				{
-					lastx = LOWORD(mp2);
-					lasty = HIWORD(mp2);
-					lasthwnd = hWnd;
-
-					ShowWindow(mybox->items[0].hwnd, SW_HIDE);
-					_do_resize(mybox,LOWORD(mp2),HIWORD(mp2));
-					ShowWindow(mybox->items[0].hwnd, SW_SHOW);
-					return 0;
-				}
+				PTRACKINFO ptInfo;
+				int res;
+				PFNWP myfunc = *blah;
+				res = (int)myfunc(hWnd, msg, mp1, mp2);
+
+				ptInfo = (PTRACKINFO)(mp2);
+
+				ptInfo->ptlMinTrackSize.y = 8;
+				ptInfo->ptlMinTrackSize.x = 8;
+
+				return (MRESULT)res;
 			}
 		}
-		break;
-	case WM_CHAR:
-		if(LOWORD(mp1) == '\t')
-		{
-			_shift_focus(hWnd);
-			return TRUE;
-		}
-		break;
-	case WM_USER:
-		windowfunc = (void *)mp1;
-
-		if(windowfunc)
-			windowfunc((void *)mp2);
-		break;
-	case WM_NOTIFY:
+	}
+	if(blah && *blah)
+	{
+		PFNWP myfunc = *blah;
+		return myfunc(hWnd, msg, mp1, mp2);
+	}
+
+	return WinDefWindowProc(hWnd, msg, mp1, mp2);
+}
+
+void _Top(HPS hpsPaint, RECTL rclPaint)
+{
+	POINTL ptl1, ptl2;
+
+	ptl1.x = rclPaint.xLeft;
+	ptl2.y = ptl1.y = rclPaint.yTop - 1;
+	ptl2.x = rclPaint.xRight - 1;
+	GpiMove(hpsPaint, &ptl1);
+	GpiLine(hpsPaint, &ptl2);
+}
+
+/* Left hits the bottom */
+void _Left(HPS hpsPaint, RECTL rclPaint)
+{
+	POINTL ptl1, ptl2;
+
+	ptl2.x = ptl1.x = rclPaint.xLeft;
+	ptl1.y = rclPaint.yTop - 1;
+	ptl2.y = rclPaint.yBottom;
+	GpiMove(hpsPaint, &ptl1);
+	GpiLine(hpsPaint, &ptl2);
+}
+
+void _Bottom(HPS hpsPaint, RECTL rclPaint)
+{
+	POINTL ptl1, ptl2;
+
+	ptl1.x = rclPaint.xRight - 1;
+	ptl1.y = ptl2.y = rclPaint.yBottom;
+	ptl2.x = rclPaint.xLeft;
+	GpiMove(hpsPaint, &ptl1);
+	GpiLine(hpsPaint, &ptl2);
+}
+
+/* Right hits the top */
+void _Right(HPS hpsPaint, RECTL rclPaint)
+{
+	POINTL ptl1, ptl2;
+
+	ptl2.x = ptl1.x = rclPaint.xRight - 1;
+	ptl1.y = rclPaint.yBottom + 1;
+	ptl2.y = rclPaint.yTop - 1;
+	GpiMove(hpsPaint, &ptl1);
+	GpiLine(hpsPaint, &ptl2);
+}
+
+/* This procedure handles drawing of a status border */
+MRESULT EXPENTRY _statusproc(HWND hWnd, ULONG msg, MPARAM mp1, MPARAM mp2)
+{
+	PFNWP *blah = WinQueryWindowPtr(hWnd, QWP_USER);
+
+	if(blah && *blah)
+	{
+		PFNWP myfunc = *blah;
+
+		switch(msg)
 		{
-			NMHDR FAR *tem=(NMHDR FAR *)mp2;
-
-			if(tem->code == TCN_SELCHANGING)
+		case WM_PAINT:
 			{
-				int num=TabCtrl_GetCurSel(tem->hwndFrom);
-				NotebookPage **array = (NotebookPage **)GetWindowLong(tem->hwndFrom, GWL_USERDATA);
-
-				if(num > -1 && array && array[num])
-					SetParent(array[num]->hwnd, DW_HWND_OBJECT);
-
-			}
-			else if(tem->code == TCN_SELCHANGE)
-			{
-				int num=TabCtrl_GetCurSel(tem->hwndFrom);
-				NotebookPage **array = (NotebookPage **)GetWindowLong(tem->hwndFrom, GWL_USERDATA);
-
-				if(num > -1 && array && array[num])
-					SetParent(array[num]->hwnd, tem->hwndFrom);
-
-				_resize_notebook_page(tem->hwndFrom, num);
+				HPS hpsPaint;
+				RECTL rclPaint;
+				char buf[1024];
+
+				hpsPaint = WinBeginPaint(hWnd, 0, 0);
+				WinQueryWindowRect(hWnd, &rclPaint);
+				WinFillRect(hpsPaint, &rclPaint, CLR_PALEGRAY);
+
+				GpiSetColor(hpsPaint, CLR_DARKGRAY);
+				_Top(hpsPaint, rclPaint);
+				_Left(hpsPaint, rclPaint);
+
+				GpiSetColor(hpsPaint, CLR_WHITE);
+				_Right(hpsPaint, rclPaint);
+				_Bottom(hpsPaint, rclPaint);
+
+				WinQueryWindowText(hWnd, 1024, buf);
+				rclPaint.xLeft += 3;
+				rclPaint.xRight--;
+				rclPaint.yTop--;
+				rclPaint.yBottom++;
+
+				GpiSetColor(hpsPaint, CLR_BLACK);
+				WinDrawText(hpsPaint, -1, buf, &rclPaint, DT_TEXTATTRS, DT_TEXTATTRS, DT_VCENTER | DT_LEFT | DT_TEXTATTRS);
+				WinEndPaint(hpsPaint);
+
+				return (MRESULT)TRUE;
 			}
 		}
-		break;
-	case WM_GETMINMAXINFO:
+		return myfunc(hWnd, msg, mp1, mp2);
+	}
+
+	return WinDefWindowProc(hWnd, msg, mp1, mp2);
+}
+
+/* This procedure handles drawing of a percent bar */
+MRESULT EXPENTRY _percentproc(HWND hWnd, ULONG msg, MPARAM mp1, MPARAM mp2)
+{
+	PercentBar *blah = (PercentBar *)WinQueryWindowPtr(hWnd, QWP_USER);
+
+	if(blah)
+	{
+		PFNWP myfunc = blah->oldproc;
+
+		switch(msg)
 		{
-			MINMAXINFO *info = (MINMAXINFO *)mp2;
-			info->ptMinTrackSize.x = 8;
-			info->ptMinTrackSize.y = 8;
-			return 0;
-		}
-		break;
-	case WM_DESTROY:
-		/* Free memory before destroying */
-		_free_window_memory(hWnd, 0);
-		EnumChildWindows(hWnd, _free_window_memory, 0);
-		break;
-	case WM_CTLCOLORSTATIC:
-	case WM_CTLCOLORLISTBOX:
-	case WM_CTLCOLORBTN:
-	case WM_CTLCOLOREDIT:
-	case WM_CTLCOLORMSGBOX:
-	case WM_CTLCOLORSCROLLBAR:
-	case WM_CTLCOLORDLG:
-		{
-			ColorInfo *thiscinfo = (ColorInfo *)GetWindowLong((HWND)mp2, GWL_USERDATA);
-			if(thiscinfo && thiscinfo->fore != -1 && thiscinfo->back != -1)
+		case WM_PAINT:
 			{
-				if(thiscinfo->fore > -1 && thiscinfo->back > -1 &&
-				   thiscinfo->fore < 18 && thiscinfo->back < 18)
-				{
-					SetTextColor((HDC)mp1, RGB(_red[thiscinfo->fore],
-											   _green[thiscinfo->fore],
-											   _blue[thiscinfo->fore]));
-					SetBkColor((HDC)mp1, RGB(_red[thiscinfo->back],
-											 _green[thiscinfo->back],
-											 _blue[thiscinfo->back]));
-					DeleteObject(thiscinfo->hbrush);
-					thiscinfo->hbrush = CreateSolidBrush(RGB(_red[thiscinfo->back],
-															 _green[thiscinfo->back],
-															 _blue[thiscinfo->back]));
-					SelectObject((HDC)mp1, thiscinfo->hbrush);
-					return (LONG)thiscinfo->hbrush;
-				}
-				if((thiscinfo->fore & DW_RGB_COLOR) == DW_RGB_COLOR && (thiscinfo->back & DW_RGB_COLOR) == DW_RGB_COLOR)
-				{
-					SetTextColor((HDC)mp1, RGB(DW_RED_VALUE(thiscinfo->fore),
-											   DW_GREEN_VALUE(thiscinfo->fore),
-											   DW_BLUE_VALUE(thiscinfo->fore)));
-					SetBkColor((HDC)mp1, RGB(DW_RED_VALUE(thiscinfo->back),
-												 DW_GREEN_VALUE(thiscinfo->back),
-												 DW_BLUE_VALUE(thiscinfo->back)));
-					DeleteObject(thiscinfo->hbrush);
-					thiscinfo->hbrush = CreateSolidBrush(RGB(DW_RED_VALUE(thiscinfo->back),
-															 DW_GREEN_VALUE(thiscinfo->back),
-															 DW_BLUE_VALUE(thiscinfo->back)));
-					SelectObject((HDC)mp1, thiscinfo->hbrush);
-					return (LONG)thiscinfo->hbrush;
-				}
-			}
-
-		}
-		break;
-	}
-	if(result != -1)
-		return result;
-	else
-		return DefWindowProc(hWnd, msg, mp1, mp2);
-}
-
-BOOL CALLBACK _framewndproc(HWND hWnd, UINT msg, WPARAM mp1, LPARAM mp2)
-{
-	switch( msg )
-	{
-	case WM_LBUTTONDOWN:
-	case WM_MBUTTONDOWN:
-	case WM_RBUTTONDOWN:
-		SetActiveWindow(hWnd);
-		break;
-	case WM_COMMAND:
-	case WM_NOTIFY:
-		_wndproc(hWnd, msg, mp1, mp2);
-		break;
-#if 0
-	case WM_ERASEBKGND:
-		{
-			ColorInfo *thiscinfo = (ColorInfo *)GetWindowLong(hWnd, GWL_USERDATA);
-
-			if(thiscinfo && thiscinfo->fore != -1 && thiscinfo->back != -1)
-				return FALSE;
-		}
-		break;
-#endif
-	case WM_PAINT:
-		{
-			ColorInfo *thiscinfo = (ColorInfo *)GetWindowLong(hWnd, GWL_USERDATA);
-
-			if(thiscinfo && thiscinfo->fore != -1 && thiscinfo->back != -1)
-			{
-				PAINTSTRUCT ps;
-				HDC hdcPaint = BeginPaint(hWnd, &ps);
-				int success = FALSE;
-
-				if(thiscinfo->fore > -1 && thiscinfo->back > -1 &&
-				   thiscinfo->fore < 18 && thiscinfo->back < 18)
+				HPS hpsPaint;
+				RECTL rclPaint, rclBar;
+
+				hpsPaint = WinBeginPaint(hWnd, 0, 0);
+				WinQueryWindowRect(hWnd, &rclPaint);
+
+				/* Draw outer border */
+				rclBar = rclPaint;
+				GpiSetColor(hpsPaint, CLR_PALEGRAY);
+				_Top(hpsPaint, rclBar);
+				_Bottom(hpsPaint, rclBar);
+				rclBar.yTop--;
+				GpiSetColor(hpsPaint, CLR_WHITE);
+				_Right(hpsPaint, rclBar);
+				rclBar.yBottom++;
+				GpiSetColor(hpsPaint, CLR_DARKGRAY);
+				_Left(hpsPaint, rclBar);
+
+				/* Draw inner border */
+				rclBar.xLeft++;
+				rclBar.xRight--;
+				GpiSetColor(hpsPaint, CLR_DARKGRAY);
+				_Left(hpsPaint, rclBar);
+				_Top(hpsPaint, rclBar);
+				GpiSetColor(hpsPaint, CLR_WHITE);
+				_Bottom(hpsPaint, rclBar);
+				_Right(hpsPaint, rclBar);
+
+				/* Draw bar border */
+				rclBar.xLeft++;
+				rclBar.xRight--;
+				rclBar.yBottom++;
+				rclBar.yTop--;
+				GpiSetColor(hpsPaint, CLR_DARKGRAY);
+				_Left(hpsPaint, rclBar);
+				_Top(hpsPaint, rclBar);
+				_Bottom(hpsPaint, rclBar);
+				_Right(hpsPaint, rclBar);
+
+				if(blah->pos)
 				{
-					SetTextColor((HDC)mp1, RGB(_red[thiscinfo->fore],
-											   _green[thiscinfo->fore],
-											   _blue[thiscinfo->fore]));
-					SetBkColor((HDC)mp1, RGB(_red[thiscinfo->back],
-											 _green[thiscinfo->back],
-											 _blue[thiscinfo->back]));
-					DeleteObject(thiscinfo->hbrush);
-					thiscinfo->hbrush = CreateSolidBrush(RGB(_red[thiscinfo->back],
-															 _green[thiscinfo->back],
-															 _blue[thiscinfo->back]));
-					SelectObject(hdcPaint, thiscinfo->hbrush);
-					Rectangle(hdcPaint, ps.rcPaint.left - 1, ps.rcPaint.top - 1, ps.rcPaint.right + 1, ps.rcPaint.bottom + 1);
-					success = TRUE;
+					rclBar.xRight = 3 + blah->pos;
+					_Right(hpsPaint, rclBar);
+
+					/* Draw Bar itself */
+					rclBar.xLeft = rclPaint.xLeft + 3;
+					rclBar.xRight = rclPaint.xLeft +  2 + blah->pos;
+					rclBar.yTop = rclPaint.yTop - 3;
+					rclBar.yBottom = rclPaint.yBottom + 3;
+
+					WinFillRect(hpsPaint, &rclBar, CLR_DARKBLUE);
 				}
-				if((thiscinfo->fore & DW_RGB_COLOR) == DW_RGB_COLOR && (thiscinfo->back & DW_RGB_COLOR) == DW_RGB_COLOR)
-				{
-					SetTextColor((HDC)mp1, RGB(DW_RED_VALUE(thiscinfo->fore),
-											   DW_GREEN_VALUE(thiscinfo->fore),
-											   DW_BLUE_VALUE(thiscinfo->fore)));
-					SetBkColor((HDC)mp1, RGB(DW_RED_VALUE(thiscinfo->back),
-											 DW_GREEN_VALUE(thiscinfo->back),
-											 DW_BLUE_VALUE(thiscinfo->back)));
-					DeleteObject(thiscinfo->hbrush);
-					thiscinfo->hbrush = CreateSolidBrush(RGB(DW_RED_VALUE(thiscinfo->back),
-															 DW_GREEN_VALUE(thiscinfo->back),
-															 DW_BLUE_VALUE(thiscinfo->back)));
-					SelectObject(hdcPaint, thiscinfo->hbrush);
-					Rectangle(hdcPaint, ps.rcPaint.left, ps.rcPaint.top, ps.rcPaint.right, ps.rcPaint.bottom);
-					success = TRUE;
-				}
-
-				EndPaint(hWnd, &ps);
-				if(success)
-					return FALSE;
+
+				/* Draw the background */
+				rclBar.xLeft = rclPaint.xLeft + 3 + blah->pos;
+				rclBar.xRight = rclPaint.xRight - 3;
+				rclBar.yTop = rclPaint.yTop - 3;
+				rclBar.yBottom = rclPaint.yBottom + 3;
+
+				WinFillRect(hpsPaint, &rclBar, CLR_PALEGRAY);
+
+				WinEndPaint(hpsPaint);
+
+				return (MRESULT)TRUE;
 			}
-
 		}
-		break;
+		return myfunc(hWnd, msg, mp1, mp2);
 	}
-	return DefWindowProc(hWnd, msg, mp1, mp2);
-}
-
-BOOL CALLBACK _rendwndproc(HWND hWnd, UINT msg, WPARAM mp1, LPARAM mp2)
-{
-	switch( msg )
-	{
-	case WM_LBUTTONDOWN:
-	case WM_MBUTTONDOWN:
-	case WM_RBUTTONDOWN:
-		SetActiveWindow(hWnd);
-		_wndproc(hWnd, msg, mp1, mp2);
-		break;
-	case WM_LBUTTONUP:
-	case WM_MBUTTONUP:
-	case WM_RBUTTONUP:
-	case WM_MOUSEMOVE:
-	case WM_PAINT:
-	case WM_SIZE:
-	case WM_COMMAND:
-		_wndproc(hWnd, msg, mp1, mp2);
-		break;
-	}
-	return DefWindowProc(hWnd, msg, mp1, mp2);
-}
-
-BOOL CALLBACK _spinnerwndproc(HWND hWnd, UINT msg, WPARAM mp1, LPARAM mp2)
-{
-	ColorInfo *cinfo;
-
-	cinfo = (ColorInfo *)GetWindowLong(hWnd, GWL_USERDATA);
-
-	if(cinfo)
-	{
-		switch( msg )
-		{
-		case WM_LBUTTONDOWN:
-		case WM_MBUTTONDOWN:
-		case WM_RBUTTONDOWN:
-		case WM_CHAR:
-			{
-				BOOL ret;
-
-				if(!cinfo || !cinfo->pOldProc)
-					ret = DefWindowProc(hWnd, msg, mp1, mp2);
-				ret = CallWindowProc(cinfo->pOldProc, hWnd, msg, mp1, mp2);
-
-				/* Tell the edit control that a buttonpress has
-				 * occured and to update it's window title.
-				 */
-				if(cinfo->buddy)
-					SendMessage(cinfo->buddy, WM_USER+10, 0, 0);
-
-				return ret;
-			}
-			break;
-		case WM_USER+10:
-			{
-				if(cinfo->buddy)
-				{
-					char tempbuf[100] = "";
-					long position;
-
-					GetWindowText(cinfo->buddy, tempbuf, 99);
-
-					position = atol(tempbuf);
-
-					if(IS_WIN98PLUS)
-						SendMessage(hWnd, UDM_SETPOS32, 0, (LPARAM)position);
-					else
-						SendMessage(hWnd, UDM_SETPOS, 0, (LPARAM)MAKELONG((short)position, 0));
-				}
-			}
-			break;
-		}
-	}
-
-	if(!cinfo || !cinfo->pOldProc)
-		return DefWindowProc(hWnd, msg, mp1, mp2);
-	return CallWindowProc(cinfo->pOldProc, hWnd, msg, mp1, mp2);
+
+	return WinDefWindowProc(hWnd, msg, mp1, mp2);
 }
 
 void _click_default(HWND handle)
 {
 	char tmpbuf[100];
 
-	GetClassName(handle, tmpbuf, 99);
+	WinQueryClassName(handle, 99, tmpbuf);
 
 	/* These are the window classes which can
 	 * obtain input focus.
 	 */
-	if(strnicmp(tmpbuf, BUTTONCLASSNAME, strlen(BUTTONCLASSNAME))==0)
+	if(strncmp(tmpbuf, "#3", 3)==0)
 	{
 		/* Generate click on default item */
 		SignalHandler *tmp = Root;
@@ -1587,7 +1245,7 @@
 		{
 			if(tmp->message == WM_COMMAND)
 			{
-				int (*clickfunc)(HWND, void *) = tmp->signalfunction;
+				int (*clickfunc)(HWND, void *) = (int (*)(HWND, void *))tmp->signalfunction;
 
 				/* Make sure it's the right window, and the right ID */
 				if(tmp->window == handle)
@@ -1601,271 +1259,726 @@
 		}
 	}
 	else
-		SetFocus(handle);
-}
-
-BOOL CALLBACK _colorwndproc(HWND hWnd, UINT msg, WPARAM mp1, LPARAM mp2)
-{
-	ColorInfo *cinfo;
-	char tmpbuf[100];
-	WNDPROC pOldProc = 0;
-
-	cinfo = (ColorInfo *)GetWindowLong(hWnd, GWL_USERDATA);
-
-	GetClassName(hWnd, tmpbuf, 99);
-	if(strcmp(tmpbuf, FRAMECLASSNAME) == 0)
-		cinfo = &(((Box *)cinfo)->cinfo);
-
-	if(cinfo)
+		WinSetFocus(HWND_DESKTOP, handle);
+}
+
+MRESULT EXPENTRY _comboentryproc(HWND hWnd, ULONG msg, MPARAM mp1, MPARAM mp2)
+{
+	WindowData *blah = (WindowData *)WinQueryWindowPtr(hWnd, QWP_USER);
+
+	switch(msg)
+	{
+	case WM_SETFOCUS:
+		_run_event(hWnd, msg, mp1, mp2);
+		break;
+	}
+
+	if(blah && blah->oldproc)
+		return blah->oldproc(hWnd, msg, mp1, mp2);
+
+	return WinDefWindowProc(hWnd, msg, mp1, mp2);
+}
+
+/* Originally just intended for entryfields, it now serves as a generic
+ * procedure for handling TAB presses to change input focus on controls.
+ */
+MRESULT EXPENTRY _entryproc(HWND hWnd, ULONG msg, MPARAM mp1, MPARAM mp2)
+{
+	WindowData *blah = (WindowData *)WinQueryWindowPtr(hWnd, QWP_USER);
+	PFNWP oldproc = 0;
+
+	if(blah)
+		oldproc = blah->oldproc;
+
+	switch(msg)
+	{
+	case WM_BUTTON1DOWN:
+	case WM_BUTTON2DOWN:
+	case WM_BUTTON3DOWN:
+		{
+			char tmpbuf[100];
+
+			WinQueryClassName(hWnd, 99, tmpbuf);
+
+			if(strncmp(tmpbuf, "#32", 3)==0)
+				_run_event(hWnd, WM_SETFOCUS, (MPARAM)FALSE, (MPARAM)TRUE);
+		}
+		break;
+	case WM_SETFOCUS:
+		_run_event(hWnd, msg, mp1, mp2);
+		break;
+	case WM_CHAR:
+		if(SHORT1FROMMP(mp2) == '\t')
+		{
+			_shift_focus(hWnd);
+			return FALSE;
+		}
+		else if(SHORT1FROMMP(mp2) == '\r' && blah && blah->clickdefault)
+			_click_default(blah->clickdefault);
+
+		break;
+	}
+
+	if(oldproc)
+		return oldproc(hWnd, msg, mp1, mp2);
+
+	return WinDefWindowProc(hWnd, msg, mp1, mp2);
+}
+
+/* Handle correct painting of a combobox with the WS_CLIPCHILDREN
+ * flag enabled, and also handle TABs to switch input focus.
+ */
+MRESULT EXPENTRY _comboproc(HWND hWnd, ULONG msg, MPARAM mp1, MPARAM mp2)
+{
+	WindowData *blah = WinQueryWindowPtr(hWnd, QWP_USER);
+	PFNWP oldproc = 0;
+
+	if(blah)
+		oldproc = blah->oldproc;
+
+	switch(msg)
 	{
-		pOldProc = cinfo->pOldProc;
-
-		switch( msg )
+	case WM_CHAR:
+		if(SHORT1FROMMP(mp2) == '\t')
+		{
+			_shift_focus(hWnd);
+			return FALSE;
+		}
+		else if(SHORT1FROMMP(mp2) == '\r' && blah && blah->clickdefault)
+			_click_default(blah->clickdefault);
+		break;
+	case WM_BUTTON1DOWN:
+	case WM_BUTTON2DOWN:
+	case WM_BUTTON3DOWN:
+		_run_event(hWnd, WM_SETFOCUS, (MPARAM)FALSE, (MPARAM)TRUE);
+		break;
+	case WM_SETFOCUS:
+		_run_event(hWnd, msg, mp1, mp2);
+		break;
+	case WM_PAINT:
 		{
-		case WM_SETFOCUS:
-            if(cinfo->combo)
-				_wndproc(cinfo->combo, msg, mp1, mp2);
-			else
-				_wndproc(hWnd, msg, mp1, mp2);
-			break;
-		case WM_CHAR:
-			if(LOWORD(mp1) == '\t')
+			HWND parent = WinQueryWindow(hWnd, QW_PARENT);
+			ULONG bcol, av[32];
+			HPS hpsPaint;
+			POINTL ptl;                  /* Add 6 because it has a thick border like the entryfield */
+			unsigned long width, height, thumbheight = WinQuerySysValue(HWND_DESKTOP, SV_CYVSCROLLARROW) + 6;
+
+			WinQueryPresParam(parent, PP_BACKGROUNDCOLORINDEX, 0, &bcol, sizeof(ULONG), &av, QPF_ID1COLORINDEX | QPF_NOINHERIT);
+            dw_window_get_pos_size(hWnd, 0, 0, &width, &height);
+
+			hpsPaint = WinGetPS(hWnd);
+			GpiSetColor(hpsPaint, CLR_PALEGRAY);
+
+			ptl.x = 0;
+			ptl.y = 0;
+			GpiMove(hpsPaint, &ptl);
+
+			ptl.x = width;
+			ptl.y = height - thumbheight;
+			GpiBox(hpsPaint, DRO_FILL, &ptl, 0, 0);
+
+			WinReleasePS(hpsPaint);
+		}
+		break;
+	}
+	if(oldproc)
+		return oldproc(hWnd, msg, mp1, mp2);
+
+	return WinDefWindowProc(hWnd, msg, mp1, mp2);
+}
+
+void _GetPPFont(HWND hwnd, char *buff)
+{
+    ULONG AttrFound;
+    BYTE  AttrValue[128];
+    ULONG cbRetLen;
+
+    cbRetLen = WinQueryPresParam(hwnd,
+                                 PP_FONTNAMESIZE,
+                                 0,
+                                 &AttrFound,
+                                 sizeof(AttrValue),
+                                 &AttrValue,
+                                 QPF_NOINHERIT);
+
+    if(PP_FONTNAMESIZE == AttrFound && cbRetLen)
+    {
+        memcpy(buff, AttrValue, cbRetLen);
+    }
+}
+
+/* Returns height of specified window. */
+int _get_height(HWND handle)
+{
+	unsigned long height;
+	dw_window_get_pos_size(handle, NULL, NULL, NULL, &height);
+	return (int)height;
+}
+
+/* Find the height of the frame a desktop style window is sitting on */
+int _get_frame_height(HWND handle)
+{
+	while(handle)
+	{
+		HWND client;
+		if((client = WinWindowFromID(handle, FID_CLIENT)) != NULLHANDLE)
+		{
+            return _get_height(WinQueryWindow(handle, QW_PARENT));
+		}
+        handle = WinQueryWindow(handle, QW_PARENT);
+	}
+	return dw_screen_height();
+}
+
+#ifndef NO_SIGNALS
+MRESULT EXPENTRY _run_event(HWND hWnd, ULONG msg, MPARAM mp1, MPARAM mp2)
+{
+	int result = -1;
+	SignalHandler *tmp = Root;
+	ULONG origmsg = msg;
+
+	if(msg == WM_BUTTON2DOWN || msg == WM_BUTTON3DOWN)
+		msg = WM_BUTTON1DOWN;
+	if(msg == WM_BUTTON2UP || msg == WM_BUTTON3UP)
+		msg = WM_BUTTON1UP;
+
+	/* Find any callbacks for this function */
+	while(tmp)
+	{
+		if(tmp->message == msg || msg == WM_CONTROL || tmp->message == WM_USER+1)
+		{
+			switch(msg)
 			{
-				if(cinfo->combo)
-					_shift_focus(cinfo->combo);
-				else if(cinfo->buddy)
-					_shift_focus(cinfo->buddy);
-				else
-					_shift_focus(hWnd);
-				return FALSE;
-			}
-			else if(LOWORD(mp1) == '\r')
-			{
-				if(cinfo->clickdefault)
-					_click_default(cinfo->clickdefault);
-
-			}
-
-			/* Tell the spinner control that a keypress has
-			 * occured and to update it's internal value.
-			 */
-			if(cinfo->buddy && !cinfo->combo)
-			{
-				if(IsWinNT())
-					PostMessage(cinfo->buddy, WM_USER+10, 0, 0);
-				else
-					SendMessage(cinfo->buddy, WM_USER+10, 0, 0);
-			}
-			break;
-		case WM_USER+10:
-			{
-				if(cinfo->buddy)
+			case WM_SETFOCUS:
+				{
+					if((mp2 && tmp->message == WM_SETFOCUS) || (!mp2 && tmp->message == WM_USER+1))
+					{
+						int (*setfocusfunc)(HWND, void *) = (int (*)(HWND, void *))tmp->signalfunction;
+
+						if(hWnd == tmp->window || WinWindowFromID(tmp->window, FID_CLIENT) == hWnd)
+						{
+							result = setfocusfunc(tmp->window, tmp->data);
+							tmp = NULL;
+						}
+					}
+				}
+				break;
+			case WM_SIZE:
+				{
+					int (*sizefunc)(HWND, int, int, void *) = (int (*)(HWND, int, int, void *))tmp->signalfunction;
+
+					if(hWnd == tmp->window || WinWindowFromID(tmp->window, FID_CLIENT) == hWnd)
+					{
+						result = sizefunc(tmp->window, SHORT1FROMMP(mp2), SHORT2FROMMP(mp2), tmp->data);
+						tmp = NULL;
+					}
+				}
+				break;
+			case WM_BUTTON1DOWN:
+				{
+					POINTS pts = (*((POINTS*)&mp1));
+					int (*buttonfunc)(HWND, int, int, int, void *) = (int (*)(HWND, int, int, int, void *))tmp->signalfunction;
+
+					if(hWnd == tmp->window || WinWindowFromID(tmp->window, FID_CLIENT) == hWnd || WinQueryCapture(HWND_DESKTOP) == tmp->window)
+					{
+						int button = 0;
+
+						switch(origmsg)
+						{
+						case WM_BUTTON1DOWN:
+							button = 1;
+							break;
+						case WM_BUTTON2DOWN:
+							button = 2;
+							break;
+						case WM_BUTTON3DOWN:
+							button = 3;
+							break;
+						}
+
+						result = buttonfunc(tmp->window, pts.x, _get_frame_height(tmp->window) - pts.y, button, tmp->data);
+						tmp = NULL;
+					}
+				}
+				break;
+			case WM_BUTTON1UP:
 				{
-					long val, position;
-					char tmpbuf[100] = "";
-
-					GetWindowText(cinfo->buddy, tmpbuf, 99);
-
-					position = atol(tmpbuf);
-
-					if(IS_WIN98PLUS)
-						val = (long)SendMessage(cinfo->buddy, UDM_GETPOS32, 0, 0);
-					else
-						val = (long)SendMessage(cinfo->buddy, UDM_GETPOS, 0, 0);
-
-					if(val != position)
+					POINTS pts = (*((POINTS*)&mp1));
+					int (*buttonfunc)(HWND, int, int, int, void *) = (int (*)(HWND, int, int, int, void *))tmp->signalfunction;
+
+					if(hWnd == tmp->window || WinWindowFromID(tmp->window, FID_CLIENT) == hWnd || WinQueryCapture(HWND_DESKTOP) == tmp->window)
+					{
+						int button = 0;
+
+						switch(origmsg)
+						{
+						case WM_BUTTON1UP:
+							button = 1;
+							break;
+						case WM_BUTTON2UP:
+							button = 2;
+							break;
+						case WM_BUTTON3UP:
+							button = 3;
+							break;
+						}
+
+						result = buttonfunc(tmp->window, pts.x, WinQueryWindow(tmp->window, QW_PARENT) == HWND_DESKTOP ? dw_screen_height() - pts.y : _get_height(tmp->window) - pts.y, button, tmp->data);
+						tmp = NULL;
+					}
+				}
+				break;
+			case WM_MOUSEMOVE:
+				{
+					int (*motionfunc)(HWND, int, int, int, void *) = (int (*)(HWND, int, int, int, void *))tmp->signalfunction;
+
+					if(hWnd == tmp->window || WinWindowFromID(tmp->window, FID_CLIENT) == hWnd || WinQueryCapture(HWND_DESKTOP) == tmp->window)
 					{
-						sprintf(tmpbuf, "%d", val);
-						SetWindowText(hWnd, tmpbuf);
+						int keys = 0;
+						SHORT x = SHORT1FROMMP(mp1), y = SHORT2FROMMP(mp1);
+
+						if (WinGetKeyState(HWND_DESKTOP, VK_BUTTON1) & 0x8000)
+							keys = DW_BUTTON1_MASK;
+						if (WinGetKeyState(HWND_DESKTOP, VK_BUTTON2) & 0x8000)
+							keys |= DW_BUTTON2_MASK;
+						if (WinGetKeyState(HWND_DESKTOP, VK_BUTTON3) & 0x8000)
+							keys |= DW_BUTTON3_MASK;
+
+						result = motionfunc(tmp->window, x, _get_frame_height(tmp->window) - y, keys, tmp->data);
+						tmp = NULL;
+					}
+				}
+				break;
+			case WM_CHAR:
+				{
+					int (*keypressfunc)(HWND, int, void *) = (int (*)(HWND, int, void *))tmp->signalfunction;
+
+					if(hWnd == tmp->window)
+					{
+						result = keypressfunc(tmp->window, SHORT1FROMMP(mp2), tmp->data);
+						tmp = NULL;
+					}
+				}
+				break;
+			case WM_CLOSE:
+				{
+					int (*closefunc)(HWND, void *) = (int (*)(HWND, void *))tmp->signalfunction;
+
+					if(hWnd == tmp->window || hWnd == WinWindowFromID(tmp->window, FID_CLIENT))
+					{
+						result = closefunc(tmp->window, tmp->data);
+						if(result)
+							result = FALSE;
+						tmp = NULL;
+					}
+				}
+				break;
+			case WM_PAINT:
+				{
+					HPS hps;
+					DWExpose exp;
+					int (*exposefunc)(HWND, DWExpose *, void *) = (int (*)(HWND, DWExpose *, void *))tmp->signalfunction;
+					RECTL  rc;
+
+					if(hWnd == tmp->window)
+					{
+						int height = _get_height(hWnd);
+
+						hps = WinBeginPaint(hWnd, 0L, &rc);
+						exp.x = rc.xLeft;
+						exp.y = height - rc.yTop - 1;
+						exp.width = rc.xRight - rc. xLeft;
+						exp.height = rc.yTop - rc.yBottom;
+						result = exposefunc(hWnd, &exp, tmp->data);
+						WinEndPaint(hps);
 					}
 				}
+				break;
+			case WM_COMMAND:
+				{
+					int (*clickfunc)(HWND, void *) = (int (*)(HWND, void *))tmp->signalfunction;
+					ULONG command = COMMANDMSG(&msg)->cmd;
+
+					if(tmp->window < 65536 && command == tmp->window)
+					{
+						result = clickfunc(tmp->window, tmp->data);
+						tmp = NULL;
+					}
+				}
+				break;
+			case WM_CONTROL:
+				if(tmp->message == SHORT2FROMMP(mp1))
+				{
+					switch(SHORT2FROMMP(mp1))
+					{
+					case CN_ENTER:
+						{
+							int (*containerselectfunc)(HWND, char *, void *) = (int (*)(HWND, char *, void *))tmp->signalfunction;
+							int id = SHORT1FROMMP(mp1);
+							HWND conthwnd = dw_window_from_id(hWnd, id);
+							char *text = NULL;
+
+							if(mp2)
+							{
+								PRECORDCORE pre;
+
+								pre = ((PNOTIFYRECORDENTER)mp2)->pRecord;
+								if(pre)
+									text = pre->pszIcon;
+							}
+
+							if(tmp->window == conthwnd)
+							{
+								result = containerselectfunc(tmp->window, text, tmp->data);
+								tmp = NULL;
+							}
+						}
+						break;
+					case CN_CONTEXTMENU:
+						{
+							int (*containercontextfunc)(HWND, char *, int, int, void *, void *) = (int (*)(HWND, char *, int, int, void *, void *))tmp->signalfunction;
+							int id = SHORT1FROMMP(mp1);
+							HWND conthwnd = dw_window_from_id(hWnd, id);
+							char *text = NULL;
+							void *user = NULL;
+							LONG x,y;
+
+							if(mp2)
+							{
+								PCNRITEM pci;
+
+								pci = (PCNRITEM)mp2;
+
+								text = pci->rc.pszIcon;
+								user = pci->user;
+							}
+
+							dw_pointer_query_pos(&x, &y);
+
+							if(tmp->window == conthwnd)
+							{
+								if(mp2)
+								{
+									NOTIFYRECORDEMPHASIS pre;
+
+									dw_tree_item_select(tmp->window, (HWND)mp2);
+									pre.pRecord = mp2;
+									pre.fEmphasisMask = CRA_CURSORED;
+									pre.hwndCnr = tmp->window;
+									_run_event(hWnd, WM_CONTROL, MPFROM2SHORT(0, CN_EMPHASIS), (MPARAM)&pre);
+									pre.pRecord->flRecordAttr |= CRA_CURSORED;
+								}
+								result = containercontextfunc(tmp->window, text, x, y, tmp->data, user);
+								tmp = NULL;
+							}
+						}
+						break;
+					case CN_EMPHASIS:
+						{
+							PNOTIFYRECORDEMPHASIS pre = (PNOTIFYRECORDEMPHASIS)mp2;
+							static int emph_recurse = 0;
+
+							if(!emph_recurse)
+							{
+								emph_recurse = 1;
+
+								if(mp2)
+								{
+									if(tmp->window == pre->hwndCnr)
+									{
+										PCNRITEM pci = (PCNRITEM)pre->pRecord;
+
+										if(pci && pre->fEmphasisMask & CRA_CURSORED && (pci->rc.flRecordAttr & CRA_CURSORED))
+										{
+											int (*treeselectfunc)(HWND, HWND, char *, void *, void *) = (int (*)(HWND, HWND, char *, void *, void *))tmp->signalfunction;
+
+											if(lasthcnr == tmp->window && lastitem == (HWND)pci)
+											{
+												lasthcnr = 0;
+												lastitem = 0;
+											}
+											else
+											{
+												lasthcnr = tmp->window;
+												lastitem = (HWND)pci;
+												result = treeselectfunc(tmp->window, (HWND)pci, pci->rc.pszIcon, pci->user, tmp->data);
+											}
+											tmp = NULL;
+										}
+									}
+								}
+								emph_recurse = 0;
+							}
+						}
+						break;
+					case LN_SELECT:
+						{
+							int (*listboxselectfunc)(HWND, int, void *) = (int (*)(HWND, int, void *))tmp->signalfunction;
+							int id = SHORT1FROMMP(mp1);
+							HWND conthwnd = dw_window_from_id(hWnd, id);
+							static int _recursing = 0;
+
+							if(_recursing == 0 && (tmp->window == conthwnd || (!id && tmp->window == (HWND)mp2)))
+							{
+								char buf1[500], classbuf[100];
+								unsigned int index = dw_listbox_selected(tmp->window);
+
+								dw_listbox_query_text(tmp->window, index, buf1, 500);
+
+								WinQueryClassName(tmp->window, 99, classbuf);
+
+								_recursing = 1;
+
+								if(id && strncmp(classbuf, "#2", 3)==0)
+								{
+									char *buf2;
+
+									buf2 = dw_window_get_text(tmp->window);
+
+									/* This is to make sure the listboxselect function doesn't
+									 * get called if the user is modifying the entry text.
+									 */
+									if(buf2 && *buf2 && *buf1 && strncmp(buf1, buf2, 500) == 0)
+										result = listboxselectfunc(tmp->window, index, tmp->data);
+
+									if(buf2)
+										free(buf2);
+								}
+								else
+									result = listboxselectfunc(tmp->window, index, tmp->data);
+
+								_recursing = 0;
+								tmp = NULL;
+							}
+						}
+						break;
+					}
+				}
+				break;
 			}
-			break;
-		case WM_KEYUP:
+		}
+
+		if(tmp)
+			tmp = tmp->next;
+
+	}
+
+	return (MRESULT)result;
+}
+#endif
+
+/* Handles control messages sent to the box (owner). */
+MRESULT EXPENTRY _controlproc(HWND hWnd, ULONG msg, MPARAM mp1, MPARAM mp2)
+{
+	Box *blah = WinQueryWindowPtr(hWnd, QWP_USER);
+
+#ifndef NO_SIGNALS
+	switch(msg)
+	{
+	case WM_CONTROL:
+		_run_event(hWnd, msg, mp1, mp2);
+		break;
+	}
+#endif
+	if(blah && blah->oldproc)
+	{
+		return blah->oldproc(hWnd, msg, mp1, mp2);
+	}
+
+	return WinDefWindowProc(hWnd, msg, mp1, mp2);
+}
+
+/* The main window procedure for Dynamic Windows, all the resizing code is done here. */
+MRESULT EXPENTRY _wndproc(HWND hWnd, ULONG msg, MPARAM mp1, MPARAM mp2)
+{
+	int result = -1;
+	static int command_active = 0;
+	void (* windowfunc)(PVOID) = 0L;
+
+	if(filterfunc)
+		result = filterfunc(hWnd, msg, mp1, mp2);
+
+#ifndef NO_SIGNALS
+	if(result == -1 && !command_active)
+	{
+        /* Make sure we don't end up in infinite recursion */
+		command_active = 1;
+
+		result = (int)_run_event(hWnd, msg, mp1, mp2);
+
+		command_active = 0;
+	}
+#endif
+
+	/* Now that any handlers are done... do normal processing */
+	switch( msg )
+	{
+   case WM_ERASEBACKGROUND:
+      return 0;
+
+	case WM_PAINT:
+		{
+		HPS    hps;
+		RECTL  rc;
+
+		hps = WinBeginPaint( hWnd, 0L, &rc );
+		WinEndPaint( hps );
+		break;
+		}
+
+	case WM_SIZE:
+		{
+			Box *mybox = (Box *)WinQueryWindowPtr(hWnd, QWP_USER);
+
+			if(!SHORT1FROMMP(mp2) && !SHORT2FROMMP(mp2))
+				return (MPARAM)TRUE;
+
+			if(mybox && mybox->flags != DW_MINIMIZED)
 			{
-				if(mp1 == VK_UP || mp1 == VK_DOWN)
+				/* Hide the window when recalculating to reduce
+				 * CPU load.
+				 */
+				WinShowWindow(hWnd, FALSE);
+
+				_do_resize(mybox, SHORT1FROMMP(mp2), SHORT2FROMMP(mp2));
+
+				WinShowWindow(hWnd, TRUE);
+			}
+		}
+		break;
+	case WM_MINMAXFRAME:
+		{
+			Box *mybox = (Box *)WinQueryWindowPtr(hWnd, QWP_USER);
+			SWP *swp = (SWP *)mp1;
+
+			if(mybox && (swp->fl & SWP_MINIMIZE))
+				mybox->flags = DW_MINIMIZED;
+
+			if(mybox && (swp->fl & SWP_RESTORE))
+			{
+				if(!mybox->titlebar && mybox->hwndtitle)
+					WinSetParent(mybox->hwndtitle, HWND_OBJECT, FALSE);
+				mybox->flags = 0;
+			}
+
+			if(mybox && (swp->fl & SWP_MAXIMIZE))
+			{
+				int z;
+				SWP swp2;
+
+				WinQueryWindowPos(swp->hwnd, &swp2);
+
+				if(swp2.cx == swp->cx && swp2.cy == swp->cy)
+					return FALSE;
+
+				mybox->flags = 0;
+
+				/* Hide the window when recalculating to reduce
+				 * CPU load.
+				 */
+				WinShowWindow(hWnd, FALSE);
+
+				_do_resize(mybox, swp->cx, swp->cy);
+
+				if(mybox->count == 1 && mybox->items[0].type == TYPEBOX)
 				{
-					if(cinfo->buddy)
-						PostMessage(hWnd, WM_USER+10, 0, 0);
+					mybox = (Box *)WinQueryWindowPtr(mybox->items[0].hwnd, QWP_USER);
+
+					for(z=0;z<mybox->count;z++)
+					{
+						char tmpbuf[100];
+
+						WinQueryClassName(mybox->items[z].hwnd, 99, tmpbuf);
+
+						/* If we have a notebook we resize the page again. */
+						if(strncmp(tmpbuf, "#40", 3)==0)
+						{
+							unsigned long x, y, width, height;
+							int page = dw_notebook_page_query(mybox->items[z].hwnd);
+							HWND pagehwnd = (HWND)WinSendMsg(mybox->items[z].hwnd, BKM_QUERYPAGEWINDOWHWND, MPFROMLONG(page), 0);
+							RECTL rc;
+
+							Box *pagebox = (Box *)WinQueryWindowPtr(pagehwnd, QWP_USER);
+							if(pagebox)
+							{
+								dw_window_get_pos_size(mybox->items[z].hwnd, &x, &y, &width, &height);
+
+								rc.xLeft = x;
+								rc.yBottom = y;
+								rc.xRight = x + width;
+								rc.yTop = y + height;
+
+								WinSendMsg(mybox->items[z].hwnd, BKM_CALCPAGERECT, (MPARAM)&rc, (MPARAM)TRUE);
+
+								_do_resize(pagebox, rc.xRight - rc.xLeft, rc.yTop - rc.yBottom);
+							}
+
+						}
+					}
+
+				}
+
+				WinShowWindow(hWnd, TRUE);
+			}
+		}
+		break;
+	case WM_CONTROL:
+		switch(SHORT2FROMMP(mp1))
+		{
+		case BKN_PAGESELECTEDPENDING:
+			{
+				PAGESELECTNOTIFY *psn = (PAGESELECTNOTIFY *)mp2;
+				HWND pagehwnd = (HWND)WinSendMsg(psn->hwndBook, BKM_QUERYPAGEWINDOWHWND, MPFROMLONG(psn->ulPageIdNew), 0);
+				Box *pagebox = (Box *)WinQueryWindowPtr(pagehwnd, QWP_USER);
+				unsigned long x, y, width, height;
+				RECTL rc;
+
+				if(pagebox && psn->ulPageIdNew != psn->ulPageIdCur)
+				{
+					dw_window_get_pos_size(psn->hwndBook, &x, &y, &width, &height);
+
+					rc.xLeft = x;
+					rc.yBottom = y;
+					rc.xRight = x + width;
+					rc.yTop = y + height;
+
+					WinSendMsg(psn->hwndBook, BKM_CALCPAGERECT, (MPARAM)&rc, (MPARAM)TRUE);
+
+					_do_resize(pagebox, rc.xRight - rc.xLeft, rc.yTop - rc.yBottom);
 				}
 			}
 			break;
-		case WM_CTLCOLORSTATIC:
-		case WM_CTLCOLORLISTBOX:
-		case WM_CTLCOLORBTN:
-		case WM_CTLCOLOREDIT:
-		case WM_CTLCOLORMSGBOX:
-		case WM_CTLCOLORSCROLLBAR:
-		case WM_CTLCOLORDLG:
-			{
-				ColorInfo *thiscinfo = (ColorInfo *)GetWindowLong((HWND)mp2, GWL_USERDATA);
-				if(thiscinfo && thiscinfo->fore != -1 && thiscinfo->back != -1)
-				{
-					if(thiscinfo->fore > -1 && thiscinfo->back > -1 &&
-					   thiscinfo->fore < 18 && thiscinfo->back < 18)
-					{
-						SetTextColor((HDC)mp1, RGB(_red[thiscinfo->fore],
-												   _green[thiscinfo->fore],
-											   _blue[thiscinfo->fore]));
-						SetBkColor((HDC)mp1, RGB(_red[thiscinfo->back],
-												 _green[thiscinfo->back],
-												 _blue[thiscinfo->back]));
-						DeleteObject(thiscinfo->hbrush);
-						thiscinfo->hbrush = CreateSolidBrush(RGB(_red[thiscinfo->back],
-																 _green[thiscinfo->back],
-																 _blue[thiscinfo->back]));
-						SelectObject((HDC)mp1, thiscinfo->hbrush);
-						return (LONG)thiscinfo->hbrush;
-					}
-					if((thiscinfo->fore & DW_RGB_COLOR) == DW_RGB_COLOR && (thiscinfo->back & DW_RGB_COLOR) == DW_RGB_COLOR)
-					{
-						SetTextColor((HDC)mp1, RGB(DW_RED_VALUE(thiscinfo->fore),
-												   DW_GREEN_VALUE(thiscinfo->fore),
-												   DW_BLUE_VALUE(thiscinfo->fore)));
-						SetBkColor((HDC)mp1, RGB(DW_RED_VALUE(thiscinfo->back),
-												 DW_GREEN_VALUE(thiscinfo->back),
-												 DW_BLUE_VALUE(thiscinfo->back)));
-						DeleteObject(thiscinfo->hbrush);
-						thiscinfo->hbrush = CreateSolidBrush(RGB(DW_RED_VALUE(thiscinfo->back),
-																 DW_GREEN_VALUE(thiscinfo->back),
-																 DW_BLUE_VALUE(thiscinfo->back)));
-						SelectObject((HDC)mp1, thiscinfo->hbrush);
-						return (LONG)thiscinfo->hbrush;
-					}
-				}
-
-			}
-			break;
 		}
-	}
-
-	if(!pOldProc)
-		return DefWindowProc(hWnd, msg, mp1, mp2);
-	return CallWindowProc(pOldProc, hWnd, msg, mp1, mp2);
-}
-
-BOOL CALLBACK _containerwndproc(HWND hWnd, UINT msg, WPARAM mp1, LPARAM mp2)
-{
-	ContainerInfo *cinfo;
-
-	cinfo = (ContainerInfo *)GetWindowLong(hWnd, GWL_USERDATA);
-
-	switch( msg )
-	{
-	case WM_COMMAND:
-	case WM_NOTIFY:
-		_wndproc(hWnd, msg, mp1, mp2);
 		break;
-#ifndef NO_SIGNALS
-	case WM_LBUTTONDBLCLK:
-	case WM_CHAR:
+	case WM_CLOSE:
+		if(result == -1)
 		{
-			LV_ITEM lvi;
-			int iItem;
-
-			if(LOWORD(mp1) == '\t')
-			{
-				_shift_focus(hWnd);
-				return FALSE;
-			}
-
-			if(msg == WM_CHAR && (char)mp1 != '\r')
-				break;
-
-			iItem = ListView_GetNextItem(hWnd, -1, LVNI_FOCUSED);
-
-			if(iItem > -1)
-			{
-				lvi.iItem = iItem;
-				lvi.mask = LVIF_PARAM;
-
-				ListView_GetItem(hWnd, &lvi);
-			}
-			else
-				lvi.lParam = (LPARAM)NULL;
-
-			{
-				SignalHandler *tmp = Root;
-
-				while(tmp)
-				{
-					if(tmp->message == NM_DBLCLK && tmp->window == hWnd)
-					{
-						int (*containerselectfunc)(HWND, char *, void *) = tmp->signalfunction;
-
-						/* Seems to be having lParam as 1 which really sucks */
-						if(lvi.lParam < 100)
-							lvi.lParam = 0;
-
-						containerselectfunc(tmp->window, (char *)lvi.lParam, tmp->data);
-						tmp = NULL;
-					}
-					if(tmp)
-						tmp = tmp->next;
-				}
-			}
+			dw_window_destroy(WinQueryWindow(hWnd, QW_PARENT));
+			return (MRESULT)TRUE;
 		}
 		break;
-	case WM_CONTEXTMENU:
-		{
-			SignalHandler *tmp = Root;
-
-			while(tmp)
-			{
-				if(tmp->message == NM_RCLICK && tmp->window == hWnd)
-				{
-					int (*containercontextfunc)(HWND, char *, int, int, void *) = tmp->signalfunction;
-					LONG x,y;
-					LV_ITEM lvi;
-					int iItem;
-					LVHITTESTINFO lhi;
-
-					dw_pointer_query_pos(&x, &y);
-
-					lhi.pt.x = x;
-					lhi.pt.y = y;
-
-					MapWindowPoints(HWND_DESKTOP, tmp->window, &lhi.pt, 1);
-
-					iItem = ListView_HitTest(tmp->window, &lhi);
-
-					if(iItem > -1)
-					{
-						lvi.iItem = iItem;
-						lvi.mask = LVIF_PARAM;
-
-						ListView_GetItem(tmp->window, &lvi);
-						ListView_SetSelectionMark(tmp->window, iItem);
-					}
-					else
-						lvi.lParam = (LPARAM)NULL;
-
-					/* Seems to be having lParam as 1 which really sucks */
-					if(lvi.lParam < 100)
-						lvi.lParam = 0;
-
-					containercontextfunc(tmp->window, (char *)lvi.lParam, x, y, tmp->data);
-					tmp = NULL;
-				}
-				if(tmp)
-					tmp = tmp->next;
-			}
-		}
+	case WM_USER:
+		windowfunc = (void (*)(void *))mp1;
+
+		if(windowfunc)
+			windowfunc((void *)mp2);
 		break;
-#else
 	case WM_CHAR:
-		if(LOWORD(mp1) == '\t')
+		if(SHORT1FROMMP(mp2) == '\t')
 		{
 			_shift_focus(hWnd);
 			return FALSE;
 		}
 		break;
-#endif
+	case WM_DESTROY:
+		/* Free memory before destroying */
+		_free_window_memory(hWnd);
+		break;
 	}
-
-	if(!cinfo || !cinfo->pOldProc)
-		return DefWindowProc(hWnd, msg, mp1, mp2);
-	return CallWindowProc(cinfo->pOldProc, hWnd, msg, mp1, mp2);
+	if(result != -1)
+		return (MRESULT)result;
+	else
+		return WinDefWindowProc(hWnd, msg, mp1, mp2);
 }
 
 void _changebox(Box *thisbox, int percent, int type)
@@ -1876,7 +1989,7 @@
 	{
 		if(thisbox->items[z].type == TYPEBOX)
 		{
-			Box *tmp = (Box*)GetWindowLong(thisbox->items[z].hwnd, GWL_USERDATA);
+			Box *tmp = WinQueryWindowPtr(thisbox->items[z].hwnd, QWP_USER);
 			_changebox(tmp, percent, type);
 		}
 		else
@@ -1896,113 +2009,170 @@
 }
 
 /* This handles any activity on the splitbars (sizers) */
-BOOL CALLBACK _splitwndproc(HWND hwnd, UINT msg, WPARAM mp1, LPARAM mp2)
+MRESULT EXPENTRY _splitwndproc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
 {
 	HWND hwndFrame = 0;
 	Box *thisbox = 0;
 
-	hwndFrame = GetParent(hwnd);
+	hwndFrame = WinQueryWindow(hwnd, QW_PARENT);
 	if(hwndFrame)
-		thisbox = (Box *)GetWindowLong(hwndFrame, GWL_USERDATA);
+		thisbox = WinQueryWindowPtr(hwndFrame, QWL_USER);
 
 	switch (msg)
 	{
 	case WM_ACTIVATE:
 	case WM_SETFOCUS:
-		return FALSE;
+		return (MRESULT)(FALSE);
 
 	case WM_PAINT:
 		{
-			HDC hdcPaint;
-			PAINTSTRUCT ps;
-			POINT ptlStart[SPLITBAR_WIDTH];
-			POINT ptlEnd[SPLITBAR_WIDTH];
-			RECT rcPaint;
+			HPS hpsPaint;
+			RECTL rclPaint;
+			POINTL ptlStart[SPLITBAR_WIDTH];
+			POINTL ptlEnd[SPLITBAR_WIDTH];
 			USHORT i;
 
-			hdcPaint = BeginPaint(hwnd, &ps);
-			GetWindowRect(hwnd, &rcPaint);
+			hpsPaint = WinBeginPaint(hwnd, 0, 0);
+			WinQueryWindowRect(hwnd, &rclPaint);
 
 			if(thisbox->type == BOXHORZ)
 			{
 				for(i = 0; i < SPLITBAR_WIDTH; i++)
 				{
-					ptlStart[i].x = i;
-					ptlStart[i].y = 0;
-
-					ptlEnd[i].x = i;
-					ptlEnd[i].y = rcPaint.bottom - rcPaint.top;
+					ptlStart[i].x = rclPaint.xLeft + i;
+					ptlStart[i].y = rclPaint.yTop;
+
+					ptlEnd[i].x = rclPaint.xLeft + i;
+					ptlEnd[i].y = rclPaint.yBottom;
 				}
 			}
 			else
 			{
 				for(i = 0; i < SPLITBAR_WIDTH; i++)
 				{
-					ptlStart[i].x = 0;
-					ptlStart[i].y = i;
-
-					ptlEnd[i].x = rcPaint.right - rcPaint.left;
-					ptlEnd[i].y = i;
+					ptlStart[i].x = rclPaint.xLeft;
+					ptlStart[i].y = rclPaint.yBottom + i;
+
+					ptlEnd[i].x = rclPaint.xRight;
+					ptlEnd[i].y = rclPaint.yBottom + i;
 				}
 			}
 
 			for(i = 0; i < SPLITBAR_WIDTH; i++)
 			{
-				HPEN hPen;
-				HPEN hOldPen;
-	 
-				hPen = CreatePen(PS_SOLID, 1, RGB (_red[lColor[i]], _green[lColor[i]], _blue[lColor[i]]));
-				hOldPen = (HPEN)SelectObject(hdcPaint, hPen);
-				MoveToEx(hdcPaint, ptlStart[i].x, ptlStart[i].y, NULL);
-				LineTo(hdcPaint, ptlEnd[i].x, ptlEnd[i].y);
-				SelectObject(hdcPaint, hOldPen);
-				DeleteObject(hPen);
+				GpiSetColor( hpsPaint, lColor[i]);
+				GpiMove(hpsPaint, &ptlStart[i]);
+				GpiLine(hpsPaint, &ptlEnd[i]);
 			}
-			EndPaint(hwnd, &ps);
+			WinEndPaint(hpsPaint);
 		}
-		return FALSE;
+		return MRFROMSHORT(FALSE);
+
 	case WM_MOUSEMOVE:
 		{
 			if(thisbox->type == BOXHORZ)
-				SetCursor(LoadCursor(NULL, IDC_SIZEWE));
+				WinSetPointer(HWND_DESKTOP,
+							  WinQuerySysPointer(HWND_DESKTOP,
+												 SPTR_SIZEWE,
+												 FALSE));
 			else
-				SetCursor(LoadCursor(NULL, IDC_SIZENS));
+				WinSetPointer(HWND_DESKTOP,
+							  WinQuerySysPointer(HWND_DESKTOP,
+												 SPTR_SIZENS,
+												 FALSE));
 		}
-		return FALSE;
-#if 0
+		return MRFROMSHORT(FALSE);
 	case WM_BUTTON1DOWN:
 		{
-			ULONG rc;
+			APIRET rc;
 			RECTL  rclFrame;
 			RECTL  rclBounds;
 			RECTL  rclStart;
 			USHORT startSize, orig, actual;
 
-			GetWindowRect(hwnd, &rclFrame);
-			GetWindowRect(hwnd, &rclStart);
-
-			GetWindowRect(hwndFrame, &rclBounds);
+			WinQueryWindowRect(hwnd, &rclFrame);
+			WinQueryWindowRect(hwnd, &rclStart);
+
+			WinQueryWindowRect(hwndFrame, &rclBounds);
 
 			WinMapWindowPoints(hwndFrame, HWND_DESKTOP,
 							   (PPOINTL)&rclBounds, 2);
 			WinMapWindowPoints(hwnd, HWND_DESKTOP,
 							   (PPOINTL)&rclStart, 2);
 
-			if(thisbox->type == BOXHORZ)
 			{
-				orig =  thisbox->items[0].origwidth;
-				actual = thisbox->items[0].width;
-
-				startSize = (rclStart.xLeft - rclBounds.xLeft)
-					* (((float)orig)/((float)actual));
-			}
-			else
-			{
-				orig = thisbox->items[0].origheight;
-				actual = thisbox->items[0].height;
-
-				startSize  = (rclStart.yBottom - rclBounds.yBottom)
-					* (((float)actual)/((float)orig));
+				int z, pastsplitbar = FALSE, found = FALSE;
+				orig = actual = 0;
+
+				for(z=0;z<thisbox->count;z++)
+				{
+					if(thisbox->items[z].hwnd == hwnd)
+						pastsplitbar = TRUE;
+					else
+					{
+						if(thisbox->type == BOXHORZ)
+						{
+							int tmpwidth, tmporigwidth;
+
+							if(thisbox->items[z].type == TYPEBOX)
+								_count_size(thisbox->items[z].hwnd, BOXHORZ, &tmpwidth, &tmporigwidth);
+							else
+							{
+								tmpwidth = thisbox->items[z].width;
+								tmporigwidth = thisbox->items[z].origwidth;
+							}
+
+							if(thisbox->items[z].hsize != SIZESTATIC && tmpwidth > actual && tmporigwidth)
+							{
+								found = pastsplitbar;
+								orig = tmporigwidth;
+								actual = tmpwidth;
+							}
+						}
+						else
+						{
+							int tmpheight, tmporigheight;
+
+							if(thisbox->items[z].type == TYPEBOX)
+								_count_size(thisbox->items[z].hwnd, BOXVERT, &tmpheight, &tmporigheight);
+							else
+							{
+								tmpheight = thisbox->items[z].height;
+								tmporigheight = thisbox->items[z].origheight;
+							}
+
+							if(thisbox->items[z].vsize != SIZESTATIC && tmpheight > actual && tmporigheight)
+							{
+								found = pastsplitbar;
+								orig = tmporigheight;
+								actual = tmpheight;
+							}
+						}
+					}
+				}
+
+				/* If we couldn't determine a valid scale... then abort */
+				if(!orig || !actual)
+					return MRFROMSHORT(FALSE);
+
+				if(thisbox->type == BOXHORZ)
+				{
+					if(found)
+						startSize = (rclStart.xLeft - rclBounds.xLeft)
+							* (((float)actual)/((float)orig));
+					else
+						startSize = (rclStart.xLeft - rclBounds.xLeft)
+							* (((float)orig)/((float)actual));
+				}
+				else
+				{
+					if(found)
+						startSize  = (rclStart.yBottom - rclBounds.yBottom)
+							* (((float)actual)/((float)orig));
+					else
+						startSize  = (rclStart.yBottom - rclBounds.yBottom)
+							* (((float)orig)/((float)actual));
+				}
 			}
 
 			rc = _TrackRectangle(hwnd, &rclFrame, &rclBounds);
@@ -2010,7 +2180,6 @@
 			if(rc == TRUE)
 			{
 				USHORT usNewRB;
-				USHORT usSize;
 				USHORT percent;
 				int z;
 
@@ -2018,25 +2187,30 @@
 				{
 					usNewRB = rclFrame.xLeft
 						- rclBounds.xLeft;
-					usSize  = rclBounds.xRight
-						- rclBounds.xLeft;
 				}
 				else
 				{
 					usNewRB = rclFrame.yBottom
 						- rclBounds.yBottom;
-					usSize  = rclBounds.yTop
-						- rclBounds.yBottom;
 				}
 
+				/* We don't want the item to disappear completely */
+				if(!usNewRB)
+					usNewRB++;
+
+				if(!startSize)
+					startSize++;
+
 				percent = (usNewRB*100)/startSize;
 
 				for(z=0;z<thisbox->count;z++)
 				{
 					if(thisbox->items[z].type == TYPEBOX)
 					{
-						Box *tmp = (Box *)GetWindowLong(thisbox->items[z].hwnd, GWL_USERDATA);
-						_changebox(tmp, percent, thisbox->type);
+						Box *tmp = WinQueryWindowPtr(thisbox->items[z].hwnd, QWP_USER);
+
+						if(tmp)
+							_changebox(tmp, percent, thisbox->type);
 					}
 					else
 					{
@@ -2056,151 +2230,136 @@
 					}
 				}
 
-				_ResetWindow(GetWindow(hwnd, GW_OWNER));
+				_ResetWindow(WinQueryWindow(hwnd, QW_OWNER));
 			}
 		}
 		return MRFROMSHORT(FALSE);
-#endif
 	}
-	return DefWindowProc(hwnd, msg, mp1, mp2);
-}
-
-/* This handles drawing the status text areas */
-BOOL CALLBACK _statuswndproc(HWND hwnd, UINT msg, WPARAM mp1, LPARAM mp2)
-{
-	switch (msg)
+	return WinDefWindowProc(hwnd, msg, mp1, mp2);
+}
+
+/* Function: BubbleProc
+ * Abstract: Subclass procedure for bubble help
+ */
+MRESULT EXPENTRY _BubbleProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
+{
+	MRESULT res;
+	PFNWP proc = (PFNWP)WinQueryWindowPtr(hwnd, QWL_USER);
+
+	if(proc)
+		res = proc(hwnd, msg, mp1, mp2);
+	else
+		res = WinDefWindowProc(hwnd, msg, mp1, mp2);
+
+	if(msg == WM_PAINT)
 	{
-	case WM_SETTEXT:
-		{
-			/* Make sure the control redraws when there is a text change */
-			int ret = (int)DefWindowProc(hwnd, msg, mp1, mp2);
-
-			InvalidateRgn(hwnd, NULL, TRUE);
-			return ret;
-		}
-	case WM_PAINT:
-		{
-			HDC hdcPaint;
-			PAINTSTRUCT ps;
-			RECT rc;
-			HFONT hFont;
-			HBRUSH oldBrush;
-			HPEN oldPen;
-			unsigned long cx, cy;
-			int threadid = dw_thread_id();
-			char tempbuf[1024] = "";
-
-			if(threadid < 0 || threadid >= THREAD_LIMIT)
-				threadid = 0;
-
-			hdcPaint = BeginPaint(hwnd, &ps);
-			EndPaint(hwnd, &ps);
-
-			hdcPaint = GetDC(hwnd);
-
-			oldBrush = _hBrush[threadid];
-			oldPen = _hPen[threadid];
-
-			dw_window_get_pos_size(hwnd, NULL, NULL, &cx, &cy);
-
- 
-			_hBrush[threadid] = CreateSolidBrush(GetSysColor(COLOR_3DFACE));
-
-			dw_draw_rect(hwnd, 0, TRUE, 0, 0, cx, cy);
-
-			_hPen[threadid] = CreatePen(PS_SOLID, 1, RGB(_red[DW_CLR_DARKGRAY],
-														 _green[DW_CLR_DARKGRAY],
-														 _blue[DW_CLR_DARKGRAY]));
-
-			dw_draw_line(hwnd, 0, 0, 0, cx, 0);
-			dw_draw_line(hwnd, 0, 0, 0, 0, cy);
-
-			DeleteObject(_hPen[threadid]);
-
-			_hPen[threadid] = GetStockObject(WHITE_PEN);
-
-			dw_draw_line(hwnd, 0, cx - 1, cy - 1, cx - 1, 0);
-			dw_draw_line(hwnd, 0, cx - 1, cy - 1, 0, cy - 1);
-
-			rc.left = 3;
-			rc.top = 1;
-			rc.bottom = cy - 1;
-			rc.right = cx - 1;
-
-			GetWindowText(hwnd, tempbuf, 1024);
-
-			hFont = (HFONT)SelectObject(hdcPaint, GetStockObject(DEFAULT_GUI_FONT));
-
-			SetTextColor(hdcPaint, RGB(0,0,0));
-			SetBkMode(hdcPaint, TRANSPARENT);
-
-			ExtTextOut(hdcPaint, 3, 1, ETO_CLIPPED, &rc, tempbuf, strlen(tempbuf), NULL);
-
-			SelectObject(hdcPaint, hFont);
-
-			DeleteObject(_hBrush[threadid]);
-			_hBrush[threadid] = oldBrush;
-			_hPen[threadid] = oldPen;
-			ReleaseDC(hwnd, hdcPaint);
-		}
-		return FALSE;
+		POINTL ptl;
+		HPS hpsTemp;
+		RECTL rcl;
+		int height, width;
+
+		WinQueryWindowRect(hwnd, &rcl);
+		height = rcl.yTop - rcl.yBottom - 1;
+		width = rcl.xRight - rcl.xLeft - 1;
+
+		/* Draw a border around the bubble help */
+		hpsTemp = WinGetPS(hwnd);
+		GpiSetColor(hpsTemp, DW_CLR_BLACK);
+		ptl.x = ptl.y = 0;
+		GpiMove(hpsTemp, &ptl);
+		ptl.x = 0;
+		ptl.y = height;
+		GpiLine(hpsTemp, &ptl);
+		ptl.x = ptl.y = 0;
+		GpiMove(hpsTemp, &ptl);
+		ptl.y = 0;
+		ptl.x = width;
+		GpiLine(hpsTemp, &ptl);
+		ptl.x = width;
+		ptl.y = height;
+		GpiMove(hpsTemp, &ptl);
+		ptl.x = 0;
+		ptl.y = height;
+		GpiLine(hpsTemp, &ptl);
+		ptl.x = width;
+		ptl.y = height;
+		GpiMove(hpsTemp, &ptl);
+		ptl.y = 0;
+		ptl.x = width;
+		GpiLine(hpsTemp, &ptl);
+		WinReleasePS(hpsTemp);
 	}
-	return DefWindowProc(hwnd, msg, mp1, mp2);
-}
-
-/* Function: _BtProc
+	return res;
+}
+
+/* Function: BtProc
  * Abstract: Subclass procedure for buttons
  */
 
-BOOL CALLBACK _BtProc(HWND hwnd, ULONG msg, WPARAM mp1, LPARAM mp2)
+MRESULT EXPENTRY _BtProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
 {
 	BubbleButton *bubble;
-	static int bMouseOver = 0;
-	POINT point;
-	RECT rect;
-	WNDPROC pOldProc;
-
-	bubble = (BubbleButton *)GetWindowLong(hwnd, GWL_USERDATA);
+	PFNWP oldproc;
+
+	bubble = (BubbleButton *)WinQueryWindowPtr(hwnd, QWL_USER);
 
 	if(!bubble)
-		return DefWindowProc(hwnd, msg, mp1, mp2);
-
-	/* We must save a pointer to the old
-	 * window procedure because if a signal
-	 * handler attached here destroys this
-	 * window it will then be invalid.
-	 */
-	pOldProc = bubble->pOldProc;
+		return WinDefWindowProc(hwnd, msg, mp1, mp2);
+
+	oldproc = bubble->pOldProc;
 
 	switch(msg)
 	{
 #ifndef NO_SIGNALS
 	case WM_SETFOCUS:
-		_wndproc(hwnd, msg, mp1, mp2);
+		if(mp2)
+			_run_event(hwnd, msg, mp1, mp2);
+		else
+			WinSendMsg(hwnd, BM_SETDEFAULT, 0, 0);
 		break;
-	case WM_LBUTTONUP:
+	case WM_BUTTON1UP:
 		{
 			SignalHandler *tmp = Root;
 
-			/* Find any callbacks for this function */
-			while(tmp)
+			if(WinIsWindowEnabled(hwnd))
 			{
-				if(tmp->message == WM_COMMAND)
+				/* Find any callbacks for this function */
+				while(tmp)
 				{
-					int (*clickfunc)(HWND, void *) = tmp->signalfunction;
-
-					/* Make sure it's the right window, and the right ID */
-					if(tmp->window == hwnd)
+					if(tmp->message == WM_COMMAND)
 					{
-						clickfunc(tmp->window, tmp->data);
-						tmp = NULL;
+						/* Make sure it's the right window, and the right ID */
+						if(tmp->window == hwnd)
+						{
+							/* Due to the fact that if we run the function
+							 * here, finishing actions on the button will occur
+							 * after we run the signal handler.  So we post the
+							 * message so the button can finish what it needs to
+							 * do before we run our handler.
+							 */
+							WinPostMsg(hwnd, WM_USER, (MPARAM)tmp, 0);
+							tmp = NULL;
+						}
 					}
+					if(tmp)
+						tmp= tmp->next;
 				}
-				if(tmp)
-					tmp= tmp->next;
 			}
 		}
 		break;
+	case WM_USER:
+		{
+            SignalHandler *tmp = (SignalHandler *)mp1;
+			int (*clickfunc)(HWND, void *) = NULL;
+
+			if(tmp)
+			{
+				clickfunc = (int (*)(HWND, void *))tmp->signalfunction;
+
+				clickfunc(tmp->window, tmp->data);
+			}
+		}
+        break;
 #endif
 	case WM_CHAR:
 		{
@@ -2208,7 +2367,7 @@
 			/* A button press should also occur for an ENTER or SPACE press
 			 * while the button has the active input focus.
 			 */
-			if(LOWORD(mp1) == '\r' || LOWORD(mp1) == ' ')
+			if(SHORT1FROMMP(mp2) == '\r' || SHORT1FROMMP(mp2) == ' ')
 			{
 				SignalHandler *tmp = Root;
 
@@ -2217,12 +2376,10 @@
 				{
 					if(tmp->message == WM_COMMAND)
 					{
-						int (*clickfunc)(HWND, void *) = tmp->signalfunction;
-
 						/* Make sure it's the right window, and the right ID */
 						if(tmp->window == hwnd)
 						{
-							clickfunc(tmp->window, tmp->data);
+							WinPostMsg(hwnd, WM_USER, (MPARAM)tmp, 0);
 							tmp = NULL;
 						}
 					}
@@ -2231,157 +2388,139 @@
 				}
 			}
 #endif
-			if(LOWORD(mp1) == '\t')
+			if(SHORT1FROMMP(mp2) == '\t')
 			{
 				_shift_focus(hwnd);
+				WinSendMsg(hwnd, BM_SETDEFAULT, 0, 0);
 				return FALSE;
 			}
 		}
 		break;
-	case WM_TIMER:
+	case 0x041f:
 		if (hwndBubble)
 		{
-			DestroyWindow(hwndBubble);
+			WinDestroyWindow(hwndBubble);
 			hwndBubble = 0;
-			KillTimer(hwnd, 1);
 		}
 		break;
 
-	case WM_MOUSEMOVE:
-		GetCursorPos(&point);
-		GetWindowRect(hwnd, &rect);
-
-		if(PtInRect(&rect, point))
+	case 0x041e:
+
+		if(!*bubble->bubbletext)
+			break;
+
+
+		if(hwndBubble)
+		{
+			WinDestroyWindow(hwndBubble);
+			hwndBubble = 0;
+		}
+
+		if(!hwndBubble)
 		{
-			if(hwnd != GetCapture())
-			{
-				SetCapture(hwnd);
-			}
-			if(!bMouseOver)
-			{
-				bMouseOver = 1;
-				if(!*bubble->bubbletext)
-					break;
-
-				if(hwndBubble)
-				{
-					DestroyWindow(hwndBubble);
-					hwndBubble = 0;
-					KillTimer(hwndBubbleLast, 1);
-				}
-
-				if(!hwndBubble)
-				{
-					POINTL ptlWork = {0,0};
-					ULONG ulColor = DW_CLR_YELLOW;
-					SIZE size;
-					HDC hdc;
-					RECT rect;
-					void *oldproc;
-
-					/* Use the WS_EX_TOOLWINDOW extended style
-					 * so the window doesn't get listed in the
-					 * taskbar.
-					 */
-					hwndBubble = CreateWindowEx(WS_EX_TOOLWINDOW,
-												STATICCLASSNAME,
-												bubble->bubbletext,
-												BS_TEXT | WS_POPUP |
-												WS_BORDER |
-												SS_CENTER,
-												0,0,50,20,
-												HWND_DESKTOP,
-												NULL,
-												NULL,
-												NULL);
-
-					dw_window_set_font(hwndBubble, DefaultFont);
-					dw_window_set_color(hwndBubble, DW_CLR_BLACK, DW_CLR_YELLOW);
-
-					hwndBubbleLast = hwnd;
-
-					SetTimer(hwnd, 1, 3000, NULL);
-
-					hdc = GetDC(hwndBubble);
-
-					GetTextExtentPoint(hdc, bubble->bubbletext, strlen(bubble->bubbletext), &size);
-
-					MapWindowPoints(hwnd, HWND_DESKTOP, (LPPOINT)&ptlWork, 1);
-
-					GetWindowRect(hwnd, &rect);
-
-					SetWindowPos(hwndBubble,
-								 HWND_TOP,
-								 ptlWork.x,
-								 ptlWork.y + (rect.bottom-rect.top) + 1,
-								 size.cx + 2,
-								 size.cy + 2,
-								 SWP_NOACTIVATE | SWP_SHOWWINDOW);
-
-					ReleaseDC(hwndBubble, hdc);
-				}
-			}
-		}
-		else{
-			/* Calling ReleaseCapture in Win95 also causes WM_CAPTURECHANGED
-			 * to be sent.  Be sure to account for that.
-			 */
-			ReleaseCapture();
-
-			if(bMouseOver)
-			{
-				bMouseOver = 0;
-				DestroyWindow(hwndBubble);
-				hwndBubble = 0;
-				KillTimer(hwndBubbleLast, 1);
-			}
-		}
-		break;
-	case WM_CAPTURECHANGED:
-		/* This message means we are losing the capture for some reason
-		 * Either because we intentionally lost it or another window
-		 * stole it
-		 */
-		if(bMouseOver)
-		{
-			bMouseOver = 0;
-			DestroyWindow(hwndBubble);
-			hwndBubble = 0;
-			KillTimer(hwndBubbleLast, 1);
+			HPS   hpsTemp = 0;
+			LONG  lHight;
+			LONG  lWidth;
+			POINTL txtPointl[TXTBOX_COUNT];
+			POINTL ptlWork = {0,0};
+			ULONG ulColor = DW_CLR_YELLOW;
+			void *blah;
+
+			hwndBubbleLast   = hwnd;
+			hwndBubble = WinCreateWindow(HWND_DESKTOP,
+										 WC_STATIC,
+										 "",
+										 SS_TEXT |
+										 DT_CENTER |
+										 DT_VCENTER,
+                                         0,0,0,0,
+										 HWND_DESKTOP,
+										 HWND_TOP,
+										 0,
+										 NULL,
+										 NULL);
+
+			WinSetPresParam(hwndBubble,
+							PP_FONTNAMESIZE,
+							sizeof(DefaultFont),
+							DefaultFont);
+
+
+			WinSetPresParam(hwndBubble,
+							PP_BACKGROUNDCOLORINDEX,
+							sizeof(ulColor),
+							&ulColor);
+
+			WinSetWindowText(hwndBubble,
+							 bubble->bubbletext);
+
+			WinMapWindowPoints(hwnd, HWND_DESKTOP, &ptlWork, 1);
+
+			hpsTemp = WinGetPS(hwndBubble);
+			GpiQueryTextBox(hpsTemp,
+							strlen(bubble->bubbletext),
+							bubble->bubbletext,
+							TXTBOX_COUNT,
+							txtPointl);
+			WinReleasePS(hpsTemp);
+
+			lWidth = txtPointl[TXTBOX_TOPRIGHT].x -
+				txtPointl[TXTBOX_TOPLEFT ].x + 8;
+
+			lHight = txtPointl[TXTBOX_TOPLEFT].y -
+				txtPointl[TXTBOX_BOTTOMLEFT].y + 8;
+
+			ptlWork.y -= lHight;
+
+			blah = (void *)WinSubclassWindow(hwndBubble, _BubbleProc);
+
+			if(blah)
+				WinSetWindowPtr(hwndBubble, QWP_USER, blah);
+
+			WinSetWindowPos(hwndBubble,
+							HWND_TOP,
+							ptlWork.x,
+							ptlWork.y,
+							lWidth,
+							lHight,
+							SWP_SIZE | SWP_MOVE | SWP_SHOW);
 		}
 		break;
 	}
 
-	if(!pOldProc)
-		return DefWindowProc(hwnd, msg, mp1, mp2);
-	return CallWindowProc(pOldProc, hwnd, msg, mp1, mp2);
-}
-
-/* This function recalculates a notebook page for example
- * during switching of notebook pages.
- */
-void _resize_notebook_page(HWND handle, int pageid)
-{
-	RECT rect;
-	NotebookPage **array = (NotebookPage **)GetWindowLong(handle, GWL_USERDATA);
-
-	if(array && array[pageid])
+	if(!oldproc)
+		return WinDefWindowProc(hwnd, msg, mp1, mp2);
+	return oldproc(hwnd, msg, mp1, mp2);
+}
+
+MRESULT EXPENTRY _RendProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
+{
+	int res = 0;
+#ifndef NO_SIGNALS
+	res = (int)_run_event(hwnd, msg, mp1, mp2);
+#endif
+	switch(msg)
 	{
-		Box *box = (Box *)GetWindowLong(array[pageid]->hwnd, GWL_USERDATA);
-
-		GetClientRect(handle,&rect);
-		TabCtrl_AdjustRect(handle,FALSE,&rect);
-		MoveWindow(array[pageid]->hwnd, rect.left, rect.top,
-				   rect.right - rect.left, rect.bottom-rect.top, TRUE);
-		if(box && box->count)
-		{
-			ShowWindow(box->items[0].hwnd, SW_HIDE);
-			_do_resize(box, rect.right - rect.left, rect.bottom - rect.top);
-			ShowWindow(box->items[0].hwnd, SW_SHOW);
-		}
-
-		ShowWindow(array[pageid]->hwnd, SW_SHOWNORMAL);
+	case WM_BUTTON1DOWN:
+	case WM_BUTTON2DOWN:
+	case WM_BUTTON3DOWN:
+		if(!res)
+			WinSetFocus(HWND_DESKTOP, hwnd);
+		return (MPARAM)TRUE;
 	}
+	return WinDefWindowProc(hwnd, msg, mp1, mp2);
+}
+
+MRESULT EXPENTRY _TreeProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
+{
+	Box *blah = WinQueryWindowPtr(hwnd, QWP_USER);
+
+#ifndef NO_SIGNALS
+	_run_event(hwnd, msg, mp1, mp2);
+#endif
+	if(blah && blah->oldproc)
+		return blah->oldproc(hwnd, msg, mp1, mp2);
+	return WinDefWindowProc(hwnd, msg, mp1, mp2);
 }
 
 /*
@@ -2392,116 +2531,24 @@
  */
 int dw_init(int newthread, int argc, char *argv[])
 {
-	WNDCLASS wc;
-	int z;
-	INITCOMMONCONTROLSEX icc;
-
-	icc.dwSize = sizeof(INITCOMMONCONTROLSEX);
-	icc.dwICC = ICC_WIN95_CLASSES;
-
-	InitCommonControlsEx(&icc);
-
-	memset(lookup, 0, sizeof(HICON) * ICON_INDEX_LIMIT);
-
-	/* Register the generic Dynamic Windows class */
-	memset(&wc, 0, sizeof(WNDCLASS));
-	wc.style = CS_DBLCLKS;
-	wc.lpfnWndProc = (WNDPROC)_wndproc;
-	wc.cbClsExtra = 0;
-	wc.cbWndExtra = 32;
-	wc.hbrBackground = NULL;
-	wc.lpszMenuName = NULL;
-	wc.lpszClassName = ClassName;
-
-	RegisterClass(&wc);
-
-	/* Register the splitbar control */
-	memset(&wc, 0, sizeof(WNDCLASS));
-	wc.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;
-	wc.lpfnWndProc = (WNDPROC)_splitwndproc;
-	wc.cbClsExtra = 0;
-	wc.cbWndExtra = 0;
-	wc.hbrBackground = NULL;
-	wc.lpszMenuName = NULL;
-	wc.lpszClassName = SplitbarClassName;
-
-	RegisterClass(&wc);
-
-	/* Register a frame control like on OS/2 */
-	memset(&wc, 0, sizeof(WNDCLASS));
-	wc.style = CS_DBLCLKS;
-	wc.lpfnWndProc = (WNDPROC)_framewndproc;
-	wc.cbClsExtra = 0;
-	wc.cbWndExtra = 32;
-	wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
-	wc.hCursor = LoadCursor(NULL, IDC_ARROW);
-	wc.lpszMenuName = NULL;
-	wc.lpszClassName = FRAMECLASSNAME;
-
-	RegisterClass(&wc);
-
-	/* Create a set of brushes using the default OS/2 and DOS colors */
-	for(z=0;z<18;z++)
-		_colors[z] = CreateSolidBrush(RGB(_red[z],_green[z],_blue[z]));
-
-	/* Register an Object Windows class like OS/2 and Win2k+
-	 * so similar functionality can be used on earlier releases
-	 * of Windows.
-	 */
-	memset(&wc, 0, sizeof(WNDCLASS));
-	wc.style = 0;
-	wc.lpfnWndProc = (WNDPROC)_wndproc;
-	wc.cbClsExtra = 0;
-	wc.cbWndExtra = 0;
-	wc.hbrBackground = NULL;
-	wc.hCursor = LoadCursor(NULL, IDC_ARROW);
-	wc.lpszMenuName = NULL;
-	wc.lpszClassName = ObjectClassName;
-
-	RegisterClass(&wc);
-
-	/* Since Windows 95/98/NT don't have a HWND_OBJECT class
-	 * also known as a input only window, I will create a
-	 * temporary window that isn't visible and really does nothing
-	 * except temporarily hold the child windows before they are
-	 * packed into their correct parent.
-	 */
-
-	DW_HWND_OBJECT = CreateWindow(ObjectClassName, "", 0, 0, 0,
-								  0, 0, HWND_DESKTOP, NULL, NULL, NULL);
-
-	if(!DW_HWND_OBJECT)
+	APIRET rc;
+
+	if(newthread)
 	{
-		dw_messagebox("Dynamic Windows", "Could not initialize the object window. error code %d", GetLastError());
-		exit(1);
+		dwhab = WinInitialize(0);
+		dwhmq = WinCreateMsgQueue(dwhab, 0);
 	}
 
+	rc = WinRegisterClass(dwhab, ClassName, _wndproc, CS_SIZEREDRAW | CS_CLIPCHILDREN, 32);
+	rc = WinRegisterClass(dwhab, SplitbarClassName, _splitwndproc, 0L, 32);
+
+	/* Get the OS/2 version. */
+	DosQuerySysInfo(QSV_VERSION_MAJOR, QSV_MS_COUNT,(void *)aulBuffer, 4*sizeof(ULONG));
+
 #ifdef DWDEBUG
-	f = fopen("dw.log", "wt");
+	f = fopen("dw.log", "w");
 #endif
-	/* We need the version to check capability like up-down controls */
-	dwVersion = GetVersion();
-
-	for(z=0;z<THREAD_LIMIT;z++)
-	{
-		_foreground[z] = RGB(128,128,128);
-		_background[z] = 0;
-		_hPen[z] = CreatePen(PS_SOLID, 1, _foreground[z]);
-		_hBrush[z] = CreateSolidBrush(_foreground[z]);
-	}
-
-#if 0
-	{
-		DWORD dwResult = GetSysColor(COLOR_3DFACE);
- 
-		dw_messagebox("DW",
-					  "Window color: {%x, %x, %x}",
-					  GetRValue(dwResult),
-					  GetGValue(dwResult),
-					  GetBValue(dwResult));
-	}
-#endif
-	return 0;
+	return rc;
 }
 
 /*
@@ -2513,20 +2560,31 @@
  */
 void dw_main(HAB currenthab, void *func)
 {
-	MSG msg;
+	QMSG qmsg;
+	HAB habtouse;
+
+	if(!currenthab)
+		habtouse = dwhab;
+	else
+		habtouse = currenthab;
 
 	/* Setup the filter function */
-	filterfunc = func;
-
-	while (GetMessage(&msg, NULL, 0, 0))
-	{
-		TranslateMessage(&msg);
-		DispatchMessage(&msg);
-	}
+	filterfunc = (int (* EXPENTRY)(HWND, ULONG, MPARAM, MPARAM))func;
+
+	_dwtid = dw_thread_id();
+
+	while (WinGetMsg(habtouse, &qmsg, 0, 0, 0))
+		WinDispatchMsg(habtouse, &qmsg);
 
 #ifdef DWDEBUG
 	fclose(f);
 #endif
+
+	if(!currenthab)
+	{
+		WinDestroyMsgQueue(dwhmq);
+		WinTerminate(dwhab);
+	}
 }
 
 /*
@@ -2536,19 +2594,18 @@
  */
 void dw_main_sleep(int seconds)
 {
-	MSG msg;
+	QMSG qmsg;
 	time_t start = time(NULL);
 
 	while(time(NULL) - start <= seconds)
 	{
-		if(PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))
+		if(WinPeekMsg(dwhab, &qmsg, 0, 0, 0, PM_NOREMOVE))
 		{
-			GetMessage(&msg, NULL, 0, 0);
-			TranslateMessage(&msg);
-			DispatchMessage(&msg);
+			WinGetMsg(dwhab, &qmsg, 0, 0, 0);
+			WinDispatchMsg(dwhab, &qmsg);
 		}
 		else
-			Sleep(1);
+			DosSleep(1);
 	}
 }
 
@@ -2604,13 +2661,12 @@
  */
 void *dw_dialog_wait(DWDialog *dialog)
 {
-	MSG msg;
+	QMSG qmsg;
 	void *tmp;
 
-	while (GetMessage(&msg,NULL,0,0))
+	while (WinGetMsg(dwhab, &qmsg, 0, 0, 0))
 	{
-		TranslateMessage(&msg);
-		DispatchMessage(&msg);
+		WinDispatchMsg(dwhab, &qmsg);
 		if(dialog->done)
 			break;
 	}
@@ -2620,6 +2676,7 @@
 	return tmp;
 }
 
+
 /*
  * Displays a Message Box with given text and title..
  * Parameters:
@@ -2630,13 +2687,13 @@
 int dw_messagebox(char *title, char *format, ...)
 {
 	va_list args;
-	char outbuf[256];
+	char outbuf[1024];
 
 	va_start(args, format);
 	vsprintf(outbuf, format, args);
 	va_end(args);
 
-	MessageBox(HWND_DESKTOP, outbuf, title, MB_OK);
+	WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, outbuf, title, 0, MB_OK | MB_INFORMATION | MB_MOVEABLE);
 
 	return strlen(outbuf);
 }
@@ -2651,29 +2708,19 @@
  */
 int dw_yesno(char *title, char *text)
 {
-	if(MessageBox(HWND_DESKTOP, text, title, MB_YESNO) == IDYES)
+	if(WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, text, title, 0, MB_YESNO | MB_INFORMATION | MB_MOVEABLE | MB_SYSTEMMODAL)==MBID_YES)
 		return TRUE;
 	return FALSE;
 }
 
 /*
- * Minimizes or Iconifies a top-level window.
- * Parameters:
- *           handle: The window handle to minimize.
- */
-int dw_window_minimize(HWND handle)
-{
-	return ShowWindow(handle, SW_MINIMIZE);
-}
-
-/*
  * Makes the window topmost.
  * Parameters:
  *           handle: The window handle to make topmost.
  */
 int dw_window_raise(HWND handle)
 {
-	return SetWindowPos(handle, HWND_TOP, 0, 0, 0, 0, SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE);
+	return WinSetWindowPos(handle, HWND_TOP, 0, 0, 0, 0, SWP_ZORDER);
 }
 
 /*
@@ -2683,7 +2730,7 @@
  */
 int dw_window_lower(HWND handle)
 {
-	return SetWindowPos(handle, HWND_BOTTOM, 0, 0, 0, 0, SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE);
+	return WinSetWindowPos(handle, HWND_BOTTOM, 0, 0, 0, 0, SWP_ZORDER);
 }
 
 /*
@@ -2693,10 +2740,47 @@
  */
 int dw_window_show(HWND handle)
 {
-	int rc = ShowWindow(handle, SW_SHOW);
-	SetFocus(handle);
+	int rc = WinSetWindowPos(handle, NULLHANDLE, 0, 0, 0, 0, SWP_SHOW);
+	HSWITCH hswitch;
+	SWCNTRL swcntrl;
+
+	_fix_button_owner(handle, 0);
+	WinSetFocus(HWND_DESKTOP, handle);
 	_initial_focus(handle);
+
+	/* If this window has a  switch list entry make sure it is visible */
+	hswitch = WinQuerySwitchHandle(handle, 0);
+	if(hswitch)
+	{
+		WinQuerySwitchEntry(hswitch, &swcntrl);
+		swcntrl.uchVisibility = SWL_VISIBLE;
+		WinChangeSwitchEntry(hswitch, &swcntrl);
+	}
 	return rc;
+
+}
+
+/*
+ * Minimizes or Iconifies a top-level window.
+ * Parameters:
+ *           handle: The window handle to minimize.
+ */
+int dw_window_minimize(HWND handle)
+{
+	HWND hwndclient = WinWindowFromID(handle, FID_CLIENT);
+
+	if(hwndclient)
+	{
+		Box *box = (Box *)WinQueryWindowPtr(hwndclient, QWP_USER);
+
+		if(box)
+		{
+			if(!box->titlebar && box->hwndtitle)
+				WinSetParent(box->hwndtitle, handle, FALSE);
+		}
+	}
+
+	return WinSetWindowPos(handle, NULLHANDLE, 0, 0, 0, 0, SWP_MINIMIZE);
 }
 
 /*
@@ -2706,7 +2790,18 @@
  */
 int dw_window_hide(HWND handle)
 {
-	return ShowWindow(handle, SW_HIDE);
+	HSWITCH hswitch;
+	SWCNTRL swcntrl;
+
+	/* If this window has a  switch list entry make sure it is invisible */
+	hswitch = WinQuerySwitchHandle(handle, 0);
+	if(hswitch)
+	{
+		WinQuerySwitchEntry(hswitch, &swcntrl);
+		swcntrl.uchVisibility = SWL_INVISIBLE;
+		WinChangeSwitchEntry(hswitch, &swcntrl);
+	}
+	return WinShowWindow(handle, FALSE);
 }
 
 /*
@@ -2716,8 +2811,11 @@
  */
 int dw_window_destroy(HWND handle)
 {
-	HWND parent = GetParent(handle);
-	Box *thisbox = (Box *)GetWindowLong(parent, GWL_USERDATA);
+	HWND parent = WinQueryWindow(handle, QW_PARENT);
+	Box *thisbox = WinQueryWindowPtr(parent, QWP_USER);
+
+	if(!handle)
+		return -1;
 
 	if(parent != HWND_DESKTOP && thisbox && thisbox->count)
 	{
@@ -2749,7 +2847,8 @@
 		free(thisitem);
 		thisbox->count--;
 	}
-	return DestroyWindow(handle);
+	_disconnect_windows(handle);
+	return WinDestroyWindow(handle);
 }
 
 /* Causes entire window to be invalidated and redrawn.
@@ -2758,17 +2857,18 @@
  */
 void dw_window_redraw(HWND handle)
 {
-	Box *mybox = (Box *)GetWindowLong(handle, GWL_USERDATA);
-
-	if(mybox)
+	HWND window = WinWindowFromID(handle, FID_CLIENT);
+	Box *mybox = (Box *)WinQueryWindowPtr(window, QWP_USER);
+
+	if(window && mybox)
 	{
-		RECT rect;
-
-		GetClientRect(handle, &rect);
-
-		ShowWindow(mybox->items[0].hwnd, SW_HIDE);
-		_do_resize(mybox, rect.right - rect.left, rect.bottom - rect.top);
-		ShowWindow(mybox->items[0].hwnd, SW_SHOW);
+		unsigned long width, height;
+
+		dw_window_get_pos_size(window, NULL, NULL, &width, &height);
+
+		WinShowWindow(mybox->items[0].hwnd, FALSE);
+		_do_resize(mybox, width, height);
+		WinShowWindow(mybox->items[0].hwnd, TRUE);
 	}
 }
 
@@ -2780,44 +2880,8 @@
  */
 void dw_window_reparent(HWND handle, HWND newparent)
 {
-	SetParent(handle, newparent);
-}
-
-HFONT _aquire_font(char *fontname)
-{
-	HFONT hfont;
-	int z, size = 9;
-	LOGFONT lf;
-
-	if(fontname == DefaultFont)
-		hfont = GetStockObject(DEFAULT_GUI_FONT);
-	else
-	{
-		for(z=0;z<strlen(fontname);z++)
-		{
-			if(fontname[z]=='.')
-				break;
-		}
-		size = atoi(fontname) + 5;
-
-		lf.lfHeight = size;
-		lf.lfWidth = 0;
-		lf.lfEscapement = 0;
-		lf.lfOrientation = 0;
-		lf.lfItalic = 0;
-		lf.lfUnderline = 0;
-		lf.lfStrikeOut = 0;
-		lf.lfWeight = FW_NORMAL;
-		lf.lfCharSet = DEFAULT_CHARSET;
-		lf.lfOutPrecision = 0;
-		lf.lfClipPrecision = 0;
-		lf.lfQuality = DEFAULT_QUALITY;
-		lf.lfPitchAndFamily = DEFAULT_PITCH | FW_DONTCARE;
-		strcpy(lf.lfFaceName, &fontname[z+1]);
-
-		hfont = CreateFontIndirect(&lf);
-	}
-	return hfont;
+	HWND blah = WinWindowFromID(newparent, FID_CLIENT);
+	WinSetParent(handle, blah ? blah : newparent, TRUE);
 }
 
 /*
@@ -2828,82 +2892,58 @@
  */
 int dw_window_set_font(HWND handle, char *fontname)
 {
-	HFONT hfont = _aquire_font(fontname);
-	ColorInfo *cinfo;
-
-	cinfo = (ColorInfo *)GetWindowLong(handle, GWL_USERDATA);
-
-	if(fontname)
-	{
-		if(cinfo)
-		{
-			strcpy(cinfo->fontname, fontname);
-		}
-		else
-		{
-			cinfo = calloc(1, sizeof(ColorInfo));
-			cinfo->fore = cinfo->back = -1;
-			cinfo->buddy = 0;
-
-			strcpy(cinfo->fontname, fontname);
-
-			cinfo->pOldProc = SubclassWindow(handle, _colorwndproc);
-			SetWindowLong(handle, GWL_USERDATA, (ULONG)cinfo);
-		}
-	}
-	SendMessage(handle, WM_SETFONT, (WPARAM)hfont, FALSE);
-	return 0;
+	return WinSetPresParam(handle, PP_FONTNAMESIZE, strlen(fontname)+1, fontname);
 }
 
 /*
  * Sets the colors used by a specified window (widget) handle.
  * Parameters:
  *          handle: The window (widget) handle.
- *          fore: Foreground color in RGB format.
- *          back: Background color in RGB format.
+ *          fore: Foreground color in DW_RGB format or a default color index.
+ *          back: Background color in DW_RGB format or a default color index.
  */
 int dw_window_set_color(HWND handle, ULONG fore, ULONG back)
 {
-	ColorInfo *cinfo;
-	char tmpbuf[100];
-
-	cinfo = (ColorInfo *)GetWindowLong(handle, GWL_USERDATA);
-
-	GetClassName(handle, tmpbuf, 99);
-
-	if(strnicmp(tmpbuf, WC_LISTVIEW, strlen(WC_LISTVIEW))==0)
+	if((fore & DW_RGB_COLOR) == DW_RGB_COLOR)
+	{
+		RGB2 rgb2;
+
+		rgb2.bBlue = DW_BLUE_VALUE(fore);
+		rgb2.bGreen = DW_GREEN_VALUE(fore);
+		rgb2.bRed = DW_RED_VALUE(fore);
+		rgb2.fcOptions = 0;
+
+		WinSetPresParam(handle, PP_FOREGROUNDCOLOR, sizeof(RGB2), &rgb2);
+
+	}
+	if((back & DW_RGB_COLOR) == DW_RGB_COLOR)
 	{
-		ListView_SetTextColor(handle, RGB(DW_RED_VALUE(fore),
-										  DW_GREEN_VALUE(fore),
-										  DW_BLUE_VALUE(fore)));
-		ListView_SetTextBkColor(handle, RGB(DW_RED_VALUE(back),
-											DW_GREEN_VALUE(back),
-											DW_BLUE_VALUE(back)));
-		ListView_SetBkColor(handle, RGB(DW_RED_VALUE(back),
-										DW_GREEN_VALUE(back),
-										DW_BLUE_VALUE(back)));
-		InvalidateRgn(handle, NULL, TRUE);
-		return TRUE;
+		RGB2 rgb2;
+
+		rgb2.bBlue = DW_BLUE_VALUE(back);
+		rgb2.bGreen = DW_GREEN_VALUE(back);
+		rgb2.bRed = DW_RED_VALUE(back);
+		rgb2.fcOptions = 0;
+
+		WinSetPresParam(handle, PP_BACKGROUNDCOLOR, sizeof(RGB2), &rgb2);
+		return 0;
 	}
-
-	if(cinfo)
-	{
-		cinfo->fore = fore;
-		cinfo->back = back;
-	}
-	else
-	{
-		cinfo = calloc(1, sizeof(ColorInfo));
-
-		cinfo->fore = fore;
-		cinfo->back = back;
-		cinfo->buddy = 0;
-
-		cinfo->pOldProc = SubclassWindow(handle, _colorwndproc);
-		SetWindowLong(handle, GWL_USERDATA, (ULONG)cinfo);
-	}
-	InvalidateRgn(handle, NULL, TRUE);
-	return TRUE;
+	if((fore & DW_RGB_COLOR) == DW_RGB_COLOR)
+		return 0;
+
+	/* Slight conversion */
+	if(fore == DW_CLR_BLACK)
+		fore = CLR_BLACK;
+	if(fore == DW_CLR_WHITE)
+		fore = CLR_WHITE;
+
+	if(back == DW_CLR_BLACK)
+		back = CLR_BLACK;
+	if(back == DW_CLR_WHITE)
+		back = CLR_WHITE;
+
+	return (WinSetPresParam(handle, PP_FOREGROUNDCOLORINDEX, sizeof(ULONG), &fore) |
+			WinSetPresParam(handle, PP_BACKGROUNDCOLORINDEX, sizeof(ULONG), &back));
 }
 
 /*
@@ -2914,6 +2954,7 @@
  */
 int dw_window_set_border(HWND handle, int border)
 {
+	WinSendMsg(handle, WM_SETBORDERSIZE, MPFROMSHORT(border), MPFROMSHORT(border));
 	return 0;
 }
 
@@ -2924,7 +2965,7 @@
  */
 void dw_window_capture(HWND handle)
 {
-	SetCapture(handle);
+	WinSetCapture(HWND_DESKTOP, handle);
 }
 
 /*
@@ -2932,7 +2973,17 @@
  */
 void dw_window_release(void)
 {
-	ReleaseCapture();
+	WinSetCapture(HWND_DESKTOP, NULLHANDLE);
+}
+
+/*
+ * Tracks this window movement.
+ * Parameters:
+ *       handle: Handle to frame to be tracked.
+ */
+void dw_window_track(HWND handle)
+{
+	WinSendMsg(handle, WM_TRACKFRAME, MPFROMSHORT(TF_MOVE), 0);
 }
 
 /*
@@ -2943,7 +2994,10 @@
  */
 void dw_window_pointer(HWND handle, int pointertype)
 {
-	SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(pointertype)));
+	WinSetPointer(handle,
+				  WinQuerySysPointer(HWND_DESKTOP,
+									 pointertype,
+									 FALSE));
 }
 
 /*
@@ -2951,44 +3005,32 @@
  * Parameters:
  *       owner: The Owner's window handle or HWND_DESKTOP.
  *       title: The Window title.
- *       flStyle: Style flags, see the DW reference.
+ *       flStyle: Style flags, see the PM reference.
  */
 HWND dw_window_new(HWND hwndOwner, char *title, ULONG flStyle)
 {
-	HWND hwndframe;
-	Box *newbox = malloc(sizeof(Box));
-	ULONG flStyleEx = 0;
+	HWND hwndclient = 0, hwndframe;
+	Box *newbox = calloc(1, sizeof(Box));
+	PFNWP *blah = malloc(sizeof(PFNWP));
 
 	newbox->pad = 0;
 	newbox->type = BOXVERT;
 	newbox->count = 0;
 
-	if(hwndOwner)
-		flStyleEx |= WS_EX_MDICHILD;
-
-	if(!(flStyle & WS_CAPTION))
-		flStyle |= WS_POPUPWINDOW;
-
-	if(flStyle & DW_FCF_TASKLIST)
-	{
-		ULONG newflags = (flStyle | WS_CLIPCHILDREN) & ~DW_FCF_TASKLIST;
-
-		hwndframe = CreateWindowEx(flStyleEx, ClassName, title, newflags, CW_USEDEFAULT, CW_USEDEFAULT,
-								   CW_USEDEFAULT, CW_USEDEFAULT, hwndOwner, NULL, NULL, NULL);
-	}
+	flStyle |= FCF_NOBYTEALIGN;
+
+	if(flStyle & DW_FCF_TITLEBAR)
+		newbox->titlebar = 1;
 	else
-	{
-		flStyleEx |= WS_EX_TOOLWINDOW;
-
-		hwndframe = CreateWindowEx(flStyleEx, ClassName, title, flStyle | WS_CLIPCHILDREN, CW_USEDEFAULT, CW_USEDEFAULT,
-								   CW_USEDEFAULT, CW_USEDEFAULT, hwndOwner, NULL, NULL, NULL);
-	}
-	SetWindowLong(hwndframe, GWL_USERDATA, (ULONG)newbox);
-
-#if 0
-	if(hwndOwner)
-		SetParent(hwndframe, hwndOwner);
-#endif
+		flStyle |= FCF_TITLEBAR;
+
+	hwndframe = WinCreateStdWindow(hwndOwner, 0L, &flStyle, ClassName, title, 0L, NULLHANDLE, 0L, &hwndclient);
+	newbox->hwndtitle = WinWindowFromID(hwndframe, FID_TITLEBAR);
+	if(!newbox->titlebar && newbox->hwndtitle)
+		WinSetParent(newbox->hwndtitle, HWND_OBJECT, FALSE);
+	*blah = WinSubclassWindow(hwndframe, _sizeproc);
+	WinSetWindowPtr(hwndframe, QWP_USER, blah);
+	WinSetWindowPtr(hwndclient, QWP_USER, newbox);
 
 	return hwndframe;
 }
@@ -3001,27 +3043,29 @@
  */
 HWND dw_box_new(int type, int pad)
 {
-	Box *newbox = malloc(sizeof(Box));
+	Box *newbox = calloc(1, sizeof(Box));
 	HWND hwndframe;
 
 	newbox->pad = pad;
 	newbox->type = type;
 	newbox->count = 0;
-	newbox->grouphwnd = (HWND)NULL;
-
-	hwndframe = CreateWindow(FRAMECLASSNAME,
-							 "",
-							 WS_CHILD | WS_CLIPCHILDREN,
-							 0,0,2000,1000,
-							 DW_HWND_OBJECT,
-							 NULL,
-							 NULL,
-							 NULL);
-
-	newbox->cinfo.pOldProc = SubclassWindow(hwndframe, _colorwndproc);
-	newbox->cinfo.fore = newbox->cinfo.back = -1;
-
-	SetWindowLong(hwndframe, GWL_USERDATA, (ULONG)newbox);
+    newbox->grouphwnd = NULLHANDLE;
+
+	hwndframe = WinCreateWindow(HWND_OBJECT,
+								WC_FRAME,
+								NULL,
+								WS_VISIBLE | WS_CLIPCHILDREN |
+								FS_NOBYTEALIGN,
+								0,0,2000,1000,
+								NULLHANDLE,
+								HWND_TOP,
+								0L,
+								NULL,
+								NULL);
+
+	newbox->oldproc = WinSubclassWindow(hwndframe, _controlproc);
+	WinSetWindowPtr(hwndframe, QWP_USER, newbox);
+	dw_window_set_color(hwndframe, DW_CLR_PALEGRAY, DW_CLR_PALEGRAY);
 	return hwndframe;
 }
 
@@ -3034,33 +3078,40 @@
  */
 HWND dw_groupbox_new(int type, int pad, char *title)
 {
-	Box *newbox = malloc(sizeof(Box));
+	Box *newbox = calloc(1, sizeof(Box));
 	HWND hwndframe;
 
 	newbox->pad = pad;
 	newbox->type = type;
 	newbox->count = 0;
 
-	hwndframe = CreateWindow(FRAMECLASSNAME,
-							 "",
-							 WS_CHILD,
-							 0,0,2000,1000,
-							 DW_HWND_OBJECT,
-							 NULL,
-							 NULL,
-							 NULL);
-
-	newbox->grouphwnd = CreateWindow(BUTTONCLASSNAME,
-									 title,
-									 WS_CHILD | BS_GROUPBOX |
-									 WS_VISIBLE | WS_CLIPCHILDREN,
-									 0,0,2000,1000,
-									 hwndframe,
-									 NULL,
-									 NULL,
-									 NULL);
-
-	SetWindowLong(hwndframe, GWL_USERDATA, (ULONG)newbox);
+	hwndframe = WinCreateWindow(HWND_OBJECT,
+								WC_FRAME,
+								NULL,
+								WS_VISIBLE |
+								FS_NOBYTEALIGN,
+								0,0,2000,1000,
+								NULLHANDLE,
+								HWND_TOP,
+								0L,
+								NULL,
+								NULL);
+
+	newbox->grouphwnd = WinCreateWindow(hwndframe,
+										WC_STATIC,
+										title,
+										WS_VISIBLE | SS_GROUPBOX |
+										WS_GROUP,
+										0,0,2000,1000,
+										NULLHANDLE,
+										HWND_TOP,
+										0L,
+										NULL,
+										NULL);
+
+	WinSetWindowPtr(hwndframe, QWP_USER, newbox);
+	dw_window_set_color(hwndframe, DW_CLR_PALEGRAY, DW_CLR_PALEGRAY);
+	dw_window_set_color(newbox->grouphwnd, DW_CLR_BLACK, DW_CLR_PALEGRAY);
 	dw_window_set_font(newbox->grouphwnd, DefaultFont);
 	return hwndframe;
 }
@@ -3072,38 +3123,39 @@
  */
 HWND dw_mdi_new(unsigned long id)
 {
-	CLIENTCREATESTRUCT ccs;
 	HWND hwndframe;
 
-	ccs.hWindowMenu = NULL;
-	ccs.idFirstChild = 0;
-
-	hwndframe = CreateWindow("MDICLIENT",
-							 "",
-							 WS_CHILD | WS_CLIPSIBLINGS,
-							 0,0,2000,1000,
-							 DW_HWND_OBJECT,
-							 NULL,
-							 DWInstance,
-							 &ccs);
+	hwndframe = WinCreateWindow(HWND_OBJECT,
+								WC_FRAME,
+								NULL,
+								WS_VISIBLE | WS_CLIPCHILDREN |
+								FS_NOBYTEALIGN,
+								0,0,2000,1000,
+								NULLHANDLE,
+								HWND_TOP,
+								0L,
+								NULL,
+								NULL);
 	return hwndframe;
 }
 
 /*
  * Create a bitmap object to be packed.
  * Parameters:
- *       id: An ID to be used with dw_window_from_id or 0L.
+ *       id: An ID to be used with WinWindowFromID() or 0L.
  */
 HWND dw_bitmap_new(ULONG id)
 {
-	return CreateWindow(STATICCLASSNAME,
-						"",
-						SS_BITMAP | WS_CHILD | WS_CLIPCHILDREN,
-						0,0,2000,1000,
-						DW_HWND_OBJECT,
-						NULL,
-						NULL,
-						NULL);
+	return WinCreateWindow(HWND_OBJECT,
+						   WC_STATIC,
+						   NULL,
+						   WS_VISIBLE | SS_TEXT,
+						   0,0,2000,1000,
+						   NULLHANDLE,
+						   HWND_TOP,
+						   id,
+						   NULL,
+						   NULL);
 }
 
 /*
@@ -3114,22 +3166,34 @@
  */
 HWND dw_notebook_new(ULONG id, int top)
 {
-	ULONG flags = 0;
+	ULONG flags;
 	HWND tmp;
-	NotebookPage **array = calloc(256, sizeof(NotebookPage *));
-
-	if(!top)
-		flags = TCS_BOTTOM;
-
-	tmp = CreateWindow(WC_TABCONTROL,
-					   "",
-					   WS_CHILD | WS_CLIPCHILDREN,
-					   0,0,2000,1000,
-					   DW_HWND_OBJECT,
-					   NULL,
-					   NULL,
-					   NULL);
-	SetWindowLong(tmp, GWL_USERDATA, (ULONG)array);
+
+	if(top)
+		flags = BKS_MAJORTABTOP;
+	else
+		flags = BKS_MAJORTABBOTTOM;
+
+	tmp = WinCreateWindow(HWND_OBJECT,
+						  WC_NOTEBOOK,
+						  NULL,
+						  WS_VISIBLE |
+						  BKS_TABBEDDIALOG |
+						  flags,
+						  0,0,2000,1000,
+						  NULLHANDLE,
+						  HWND_TOP,
+						  id,
+						  NULL,
+						  NULL);
+
+	/* Fix tab sizes on Warp 3 */
+	if(!IS_WARP4())
+	{
+		/* best sizes to be determined by trial and error */
+		WinSendMsg(tmp, BKM_SETDIMENSIONS,MPFROM2SHORT(102, 28), MPFROMSHORT( BKA_MAJORTAB));
+	}
+
 	dw_window_set_font(tmp, DefaultFont);
 	return tmp;
 }
@@ -3147,8 +3211,16 @@
 	if(!tmp)
 		return NULL;
 
-	tmp->menu = CreatePopupMenu();
-	tmp->hwnd = NULL;
+	tmp->menu = WinCreateWindow(HWND_OBJECT,
+								WC_MENU,
+								NULL,
+								WS_VISIBLE,
+								0,0,2000,1000,
+								NULLHANDLE,
+								HWND_TOP,
+								id,
+								NULL,
+								NULL);
 	return tmp;
 }
 
@@ -3164,10 +3236,16 @@
 	if(!tmp)
 		return NULL;
 
-	tmp->menu = CreateMenu();
-	tmp->hwnd = location;
-
-	SetMenu(location, tmp->menu);
+	tmp->menu = WinCreateWindow(location,
+								WC_MENU,
+								NULL,
+								WS_VISIBLE | MS_ACTIONBAR,
+								0,0,2000,1000,
+								location,
+								HWND_TOP,
+								FID_MENU,
+								NULL,
+								NULL);
 	return tmp;
 }
 
@@ -3180,7 +3258,7 @@
 {
 	if(menu && *menu)
 	{
-		DestroyMenu((*menu)->menu);
+		WinDestroyWindow((*menu)->menu);
 		free(*menu);
 		*menu = NULL;
 	}
@@ -3192,50 +3270,39 @@
  *       menu: The handle the the existing menu.
  *       title: The title text on the menu item to be added.
  *       id: An ID to be used for message passing.
+ *       flags: Extended attributes to set on the menu.
  *       end: If TRUE memu is positioned at the end of the menu.
  *       check: If TRUE menu is "check"able.
- *       flags: Extended attributes to set on the menu.
  *       submenu: Handle to an existing menu to be a submenu or NULL.
  */
 HWND dw_menu_append_item(HMENUI menux, char *title, ULONG id, ULONG flags, int end, int check, HMENUI submenu)
 {
-	MENUITEMINFO mii;
-	HMENU menu;
+	MENUITEM miSubMenu;
+	HWND menu;
 
 	if(!menux)
-		return NULL;
+		return NULLHANDLE;
 
 	menu = menux->menu;
 
-	mii.cbSize = sizeof(MENUITEMINFO);
-	mii.fMask = MIIM_ID | MIIM_SUBMENU | MIIM_TYPE;
-
-	/* Convert from OS/2 style accellerators to Win32 style */
-	if(title)
-	{
-		char *tmp = title;
-
-		while(*tmp)
-		{
-			if(*tmp == '~')
-				*tmp = '&';
-			tmp++;
-		}
-	}
-
-	if(title && *title)
-		mii.fType = MFT_STRING;
+	if(end)
+		miSubMenu.iPosition=MIT_END;
+	else
+		miSubMenu.iPosition=0;
+
+	if(strlen(title) == 0)
+		miSubMenu.afStyle=MIS_SEPARATOR | flags;
 	else
-		mii.fType = MFT_SEPARATOR;
-
-	mii.wID = id;
-	mii.hSubMenu = submenu ? submenu->menu : 0;
-	mii.dwTypeData = title;
-	mii.cch = strlen(title);
-
-	InsertMenuItem(menu, 65535, TRUE, &mii);
-	if(menux->hwnd)
-		DrawMenuBar(menux->hwnd);
+		miSubMenu.afStyle=MIS_TEXT | flags;
+	miSubMenu.afAttribute=0;
+	miSubMenu.id=id;
+	miSubMenu.hwndSubMenu = submenu ? submenu->menu : 0;
+	miSubMenu.hItem=NULLHANDLE;
+
+	WinSendMsg(menu,
+			   MM_INSERTITEM,
+			   MPFROMP(&miSubMenu),
+			   MPFROMP(title));
 	return (HWND)id;
 }
 
@@ -3248,21 +3315,19 @@
  */
 void dw_menu_item_set_check(HMENUI menux, unsigned long id, int check)
 {
-	MENUITEMINFO mii;
-	HMENU menu;
+	HWND menu;
 
 	if(!menux)
 		return;
 
 	menu = menux->menu;
 
-	mii.cbSize = sizeof(MENUITEMINFO);
-	mii.fMask = MIIM_STATE;
 	if(check)
-		mii.fState = MFS_CHECKED;
+		WinSendMsg(menu, MM_SETITEMATTR, MPFROM2SHORT(id, TRUE),
+				   MPFROM2SHORT(MIA_CHECKED, MIA_CHECKED));
 	else
-		mii.fState = MFS_UNCHECKED;
-	SetMenuItemInfo(menu, id, FALSE, &mii);
+		WinSendMsg(menu, MM_SETITEMATTR, MPFROM2SHORT(id, TRUE),
+				   MPFROM2SHORT(MIA_CHECKED, 0));
 }
 
 /*
@@ -3277,12 +3342,40 @@
 {
 	if(menu && *menu)
 	{
-		TrackPopupMenu((*menu)->menu, 0, x, y, 0, parent, NULL);
+		WinPopupMenu(HWND_DESKTOP, parent, (*menu)->menu, x, dw_screen_height() - y, 0, PU_KEYBOARD | PU_MOUSEBUTTON1 | PU_VCONSTRAIN | PU_HCONSTRAIN);
 		free(*menu);
 		*menu = NULL;
 	}
 }
 
+/*
+ * Returns the current X and Y coordinates of the mouse pointer.
+ * Parameters:
+ *       x: Pointer to variable to store X coordinate.
+ *       y: Pointer to variable to store Y coordinate.
+ */
+void dw_pointer_query_pos(long *x, long *y)
+{
+	POINTL ptl;
+
+	WinQueryPointerPos(HWND_DESKTOP, &ptl);
+	if(x && y)
+	{
+		*x = ptl.x;
+		*y = dw_screen_height() - ptl.y;
+	}
+}
+
+/*
+ * Sets the X and Y coordinates of the mouse pointer.
+ * Parameters:
+ *       x: X coordinate.
+ *       y: Y coordinate.
+ */
+void dw_pointer_set_pos(long x, long y)
+{
+	WinSetPointerPos(HWND_DESKTOP, x, dw_screen_height() - y);
+}
 
 /*
  * Create a container object to be packed.
@@ -3292,27 +3385,17 @@
  */
 HWND dw_container_new(ULONG id)
 {
-	HWND tmp = CreateWindow(WC_LISTVIEW,
-							"",
-							WS_CHILD | LVS_REPORT |
-							LVS_SHAREIMAGELISTS | WS_BORDER |
-							WS_CLIPCHILDREN,
-							0,0,2000,1000,
-							DW_HWND_OBJECT,
-							(HMENU)id,
-							NULL,
-							NULL);
-	ContainerInfo *cinfo = (ContainerInfo *)calloc(1, sizeof(ContainerInfo));
-
-	if(!cinfo)
-	{
-		DestroyWindow(tmp);
-		return NULL;
-	}
-
-	cinfo->pOldProc = (WNDPROC)SubclassWindow(tmp, _containerwndproc);
-
-	SetWindowLong(tmp, GWL_USERDATA, (ULONG)cinfo);
+	HWND tmp = WinCreateWindow(HWND_OBJECT,
+							   WC_CONTAINER,
+							   NULL,
+							   WS_VISIBLE | CCS_READONLY |
+							   CCS_SINGLESEL | CCS_AUTOPOSITION,
+							   0,0,2000,1000,
+							   NULLHANDLE,
+							   HWND_TOP,
+							   id,
+							   NULL,
+							   NULL);
 	dw_window_set_font(tmp, DefaultFont);
 	return tmp;
 }
@@ -3325,51 +3408,32 @@
  */
 HWND dw_tree_new(ULONG id)
 {
-	HWND tmp = CreateWindow(WC_TREEVIEW,
-							"",
-							WS_CHILD | TVS_HASLINES |
-							TVS_HASBUTTONS | TVS_LINESATROOT |
-							WS_BORDER | WS_CLIPCHILDREN,
-							0,0,2000,1000,
-							DW_HWND_OBJECT,
-							(HMENU)id,
-							NULL,
-							NULL);
-	TreeView_SetItemHeight(tmp, 16);
+	CNRINFO cnrinfo;
+	Box *newbox = calloc(1, sizeof(Box));
+	HWND tmp = WinCreateWindow(HWND_OBJECT,
+							   WC_CONTAINER,
+							   NULL,
+							   WS_VISIBLE | CCS_READONLY |
+							   CCS_SINGLESEL | CCS_AUTOPOSITION,
+							   0,0,2000,1000,
+							   NULLHANDLE,
+							   HWND_TOP,
+							   id,
+							   NULL,
+							   NULL);
+
+	cnrinfo.flWindowAttr = CV_TREE | CA_TREELINE;
+	cnrinfo.slBitmapOrIcon.cx = 16;
+	cnrinfo.slBitmapOrIcon.cy = 16;
+
+	WinSendMsg(tmp, CM_SETCNRINFO, &cnrinfo, MPFROMLONG(CMA_FLWINDOWATTR | CMA_SLBITMAPORICON));
+	newbox->oldproc = WinSubclassWindow(tmp, _TreeProc);
+	WinSetWindowPtr(tmp, QWP_USER, newbox);
 	dw_window_set_font(tmp, DefaultFont);
 	return tmp;
 }
 
 /*
- * Returns the current X and Y coordinates of the mouse pointer.
- * Parameters:
- *       x: Pointer to variable to store X coordinate.
- *       y: Pointer to variable to store Y coordinate.
- */
-void dw_pointer_query_pos(long *x, long *y)
-{
-	POINT ptl;
-
-	GetCursorPos(&ptl);
-	if(x && y)
-	{
-		*x = ptl.x;
-		*y = ptl.y;
-	}
-}
-
-/*
- * Sets the X and Y coordinates of the mouse pointer.
- * Parameters:
- *       x: X coordinate.
- *       y: Y coordinate.
- */
-void dw_pointer_set_pos(long x, long y)
-{
-	SetCursorPos(x, y);
-}
-
-/*
  * Create a new static text window (widget) to be packed.
  * Parameters:
  *       text: The text to be display by the static text widget.
@@ -3377,15 +3441,18 @@
  */
 HWND dw_text_new(char *text, ULONG id)
 {
-	HWND tmp = CreateWindow(STATICCLASSNAME,
-							text,
-							BS_TEXT | WS_CHILD | WS_CLIPCHILDREN,
-							0,0,2000,1000,
-							DW_HWND_OBJECT,
-							(HMENU)id,
-							NULL,
-							NULL);
+	HWND tmp = WinCreateWindow(HWND_OBJECT,
+							   WC_STATIC,
+							   text,
+							   WS_VISIBLE | SS_TEXT,
+							   0,0,2000,1000,
+							   NULLHANDLE,
+							   HWND_TOP,
+							   id,
+							   NULL,
+							   NULL);
 	dw_window_set_font(tmp, DefaultFont);
+	dw_window_set_color(tmp, DW_CLR_BLACK, DW_CLR_PALEGRAY);
 	return tmp;
 }
 
@@ -3397,16 +3464,22 @@
  */
 HWND dw_status_text_new(char *text, ULONG id)
 {
-	HWND tmp = CreateWindow(STATICCLASSNAME,
-							text,
-							BS_TEXT | WS_CHILD | WS_CLIPCHILDREN,
-							0,0,2000,1000,
-							DW_HWND_OBJECT,
-							(HMENU)id,
-							NULL,
-							NULL);
+	PFNWP *blah = malloc(sizeof(PFNWP));
+	HWND tmp = WinCreateWindow(HWND_OBJECT,
+							   WC_STATIC,
+							   text,
+							   WS_VISIBLE | SS_TEXT,
+							   0,0,2000,1000,
+							   NULLHANDLE,
+							   HWND_TOP,
+							   id,
+							   NULL,
+							   NULL);
 	dw_window_set_font(tmp, DefaultFont);
-	SubclassWindow(tmp, _statuswndproc);
+	dw_window_set_color(tmp, DW_CLR_BLACK, DW_CLR_PALEGRAY);
+
+	*blah = WinSubclassWindow(tmp, _statusproc);
+	WinSetWindowPtr(tmp, QWP_USER, blah);
 	return tmp;
 }
 
@@ -3417,18 +3490,19 @@
  */
 HWND dw_mle_new(ULONG id)
 {
-    
-	HWND tmp = CreateWindow(EDITCLASSNAME,
-							"",
-							WS_BORDER |
-							WS_VSCROLL | ES_MULTILINE |
-							ES_WANTRETURN | WS_CHILD |
-							WS_CLIPCHILDREN,
-							0,0,2000,1000,
-							DW_HWND_OBJECT,
-							(HMENU)id,
-							NULL,
-							NULL);
+	HWND tmp = WinCreateWindow(HWND_OBJECT,
+							   WC_MLE,
+							   "",
+							   WS_VISIBLE |
+							   MLS_BORDER | MLS_IGNORETAB |
+							   MLS_READONLY | MLS_VSCROLL,
+							   0,0,2000,1000,
+							   NULLHANDLE,
+							   HWND_TOP,
+							   id,
+							   NULL,
+							   NULL);
+	dw_window_set_font(tmp, DefaultFont);
 	dw_window_set_font(tmp, DefaultFont);
 	return tmp;
 }
@@ -3441,69 +3515,51 @@
  */
 HWND dw_entryfield_new(char *text, ULONG id)
 {
-	HWND tmp = CreateWindow(EDITCLASSNAME,
-							text,
-							ES_WANTRETURN | WS_CHILD |
-							WS_BORDER | ES_AUTOHSCROLL |
-							WS_CLIPCHILDREN,
-							0,0,2000,1000,
-							DW_HWND_OBJECT,
-							(HMENU)id,
-							NULL,
-							NULL);
-	ColorInfo *cinfo = calloc(1, sizeof(ColorInfo));
-
-	cinfo->back = cinfo->fore = -1;
-	cinfo->buddy = 0;
-
-	cinfo->pOldProc = SubclassWindow(tmp, _colorwndproc);
-	SetWindowLong(tmp, GWL_USERDATA, (ULONG)cinfo);
+
+	WindowData *blah = calloc(1, sizeof(WindowData));
+	HWND tmp = WinCreateWindow(HWND_OBJECT,
+							   WC_ENTRYFIELD,
+							   text,
+							   WS_VISIBLE | ES_MARGIN |
+							   ES_AUTOSCROLL | WS_TABSTOP,
+							   0,0,2000,1000,
+							   NULLHANDLE,
+							   HWND_TOP,
+							   id,
+							   NULL,
+							   NULL);
 	dw_window_set_font(tmp, DefaultFont);
+	blah->oldproc = WinSubclassWindow(tmp, _entryproc);
+	WinSetWindowPtr(tmp, QWP_USER, blah);
 	return tmp;
 }
 
 /*
- * Create a new Entryfield passwird window (widget) to be packed.
+ * Create a new Entryfield (password) window (widget) to be packed.
  * Parameters:
  *       text: The default text to be in the entryfield widget.
  *       id: An ID to be used with WinWindowFromID() or 0L.
  */
 HWND dw_entryfield_password_new(char *text, ULONG id)
 {
-	HWND tmp = CreateWindow(EDITCLASSNAME,
-							text,
-							ES_WANTRETURN | WS_CHILD |
-							ES_PASSWORD | WS_BORDER |
-							ES_AUTOHSCROLL | WS_CLIPCHILDREN,
-							0,0,2000,1000,
-							DW_HWND_OBJECT,
-							(HMENU)id,
-							NULL,
-							NULL);
-	ColorInfo *cinfo = calloc(1, sizeof(ColorInfo));
-
-	cinfo->back = cinfo->fore = -1;
-	cinfo->buddy = 0;
-
-	cinfo->pOldProc = SubclassWindow(tmp, _colorwndproc);
-	SetWindowLong(tmp, GWL_USERDATA, (ULONG)cinfo);
+	WindowData *blah = calloc(1, sizeof(WindowData));
+	HWND tmp = WinCreateWindow(HWND_OBJECT,
+							   WC_ENTRYFIELD,
+							   text,
+							   WS_VISIBLE | ES_MARGIN | ES_UNREADABLE |
+							   ES_AUTOSCROLL | WS_TABSTOP,
+							   0,0,2000,1000,
+							   NULLHANDLE,
+							   HWND_TOP,
+							   id,
+							   NULL,
+							   NULL);
 	dw_window_set_font(tmp, DefaultFont);
+	blah->oldproc = WinSubclassWindow(tmp, _entryproc);
+	WinSetWindowPtr(tmp, QWP_USER, blah);
 	return tmp;
 }
 
-BOOL CALLBACK _subclass_child(HWND handle, LPARAM lp)
-{
-	ColorInfo *cinfo = (ColorInfo *)lp;
-
-	if(cinfo)
-	{
-		cinfo->buddy = handle;
-		cinfo->pOldProc = (WNDPROC)SubclassWindow(handle, _colorwndproc);
-		SetWindowLong(handle, GWL_USERDATA, (ULONG)cinfo);
-	}
-	return FALSE;
-}
-
 /*
  * Create a new Combobox window (widget) to be packed.
  * Parameters:
@@ -3512,30 +3568,31 @@
  */
 HWND dw_combobox_new(char *text, ULONG id)
 {
-	HWND tmp = CreateWindow(COMBOBOXCLASSNAME,
-							"",
-                            WS_CHILD | CBS_DROPDOWN | WS_CLIPCHILDREN,
-							0,0,2000,1000,
-							DW_HWND_OBJECT,
-							(HMENU)id,
-							NULL,
-							NULL);
-	ColorInfo *cinfo = (ColorInfo *)calloc(1, sizeof(ColorInfo));
-	ColorInfo *cinfo2 = (ColorInfo *)calloc(1, sizeof(ColorInfo));
-
-	if(!cinfo)
+	WindowData *blah = calloc(1, sizeof(WindowData));
+	HWND tmp = WinCreateWindow(HWND_OBJECT,
+							   WC_COMBOBOX,
+							   text,
+							   WS_VISIBLE | CBS_DROPDOWN | WS_GROUP,
+							   0,0,2000,1000,
+							   NULLHANDLE,
+							   HWND_TOP,
+							   id,
+							   NULL,
+							   NULL);
+	HENUM henum = WinBeginEnumWindows(tmp);
+	HWND child;
+	
+	while((child = WinGetNextWindow(henum)) != NULLHANDLE)
 	{
-		DestroyWindow(tmp);
-		return NULL;
+		WindowData *moreblah = calloc(1, sizeof(WindowData));
+		moreblah->oldproc = WinSubclassWindow(child, _comboentryproc);
+		WinSetWindowPtr(child, QWP_USER, moreblah);
 	}
-
-	cinfo2->fore = cinfo->fore = -1;
-	cinfo2->back = cinfo->back = -1;
-	cinfo2->combo = cinfo->combo = tmp;
-	EnumChildWindows(tmp, _subclass_child, (LPARAM)cinfo2);
-
-	SetWindowLong(tmp, GWL_USERDATA, (ULONG)cinfo);
+	WinEndEnumWindows(henum);
 	dw_window_set_font(tmp, DefaultFont);
+	dw_window_set_color(tmp, DW_CLR_BLACK, DW_CLR_WHITE);
+	blah->oldproc = WinSubclassWindow(tmp, _comboproc);
+	WinSetWindowPtr(tmp, QWP_USER, blah);
 	return tmp;
 }
 
@@ -3549,24 +3606,59 @@
 {
 	BubbleButton *bubble = malloc(sizeof(BubbleButton));
 
-	HWND tmp = CreateWindow(BUTTONCLASSNAME,
-							text,
-							WS_CHILD | BS_PUSHBUTTON | WS_CLIPCHILDREN,
-							0,0,2000,1000,
-							DW_HWND_OBJECT,
-							(HMENU)id,
-							NULL,
-							NULL);
+	HWND tmp = WinCreateWindow(HWND_OBJECT,
+							   WC_BUTTON,
+							   text,
+							   WS_VISIBLE,
+							   0,0,2000,1000,
+							   NULLHANDLE,
+							   HWND_TOP,
+							   id,
+							   NULL,
+							   NULL);
 
 	bubble->id = id;
 	bubble->bubbletext[0] = '\0';
-	bubble->pOldProc = (WNDPROC)SubclassWindow(tmp, _BtProc);
-
-	SetWindowLong(tmp, GWL_USERDATA, (ULONG)bubble);
+	bubble->pOldProc = WinSubclassWindow(tmp, _BtProc);
+
+	WinSetWindowPtr(tmp, QWP_USER, bubble);
 	dw_window_set_font(tmp, DefaultFont);
 	return tmp;
 }
 
+/* Function: GenResIDStr
+** Abstract: Generate string '#nnnn' for a given ID for using with Button
+**           controls
+*/
+
+void _GenResIDStr(CHAR *buff, ULONG ulID)
+{
+	char *str;
+	int  slen = 0;
+
+	*buff++ = '#';
+
+	str = buff;
+
+	do
+	{
+		*str++ = (ulID % 10) + '0';
+		ulID /= 10;
+		slen++;
+	}
+	while(ulID);
+
+	*str-- = 0;
+
+	for(; str > buff; str--, buff++)
+	{
+		*buff ^= *str;
+		*str  ^= *buff;
+		*buff ^= *str;
+	}
+}
+
+
 /*
  * Create a new bitmap button window (widget) to be packed.
  * Parameters:
@@ -3575,30 +3667,31 @@
  */
 HWND dw_bitmapbutton_new(char *text, ULONG id)
 {
+	char idbuf[256];
 	HWND tmp;
 	BubbleButton *bubble = malloc(sizeof(BubbleButton));
-	HBITMAP hbitmap = LoadBitmap(DWInstance, MAKEINTRESOURCE(id));
-
-	tmp = CreateWindow(BUTTONCLASSNAME,
-					   "",
-					   WS_CHILD | BS_PUSHBUTTON |
-					   BS_BITMAP | WS_CLIPCHILDREN,
-					   0,0,2000,1000,
-					   DW_HWND_OBJECT,
-					   (HMENU)id,
-					   NULL,
-					   NULL);
+
+	_GenResIDStr(idbuf, id);
+
+	tmp = WinCreateWindow(HWND_OBJECT,
+						  WC_BUTTON,
+						  idbuf,
+						  WS_VISIBLE | BS_PUSHBUTTON |
+						  BS_BITMAP | BS_AUTOSIZE |
+						  BS_NOPOINTERFOCUS,
+						  0,0,2000,1000,
+						  NULLHANDLE,
+						  HWND_TOP,
+						  id,
+						  NULL,
+						  NULL);
 
 	bubble->id = id;
 	strncpy(bubble->bubbletext, text, BUBBLE_HELP_MAX - 1);
 	bubble->bubbletext[BUBBLE_HELP_MAX - 1] = '\0';
-	bubble->pOldProc = (WNDPROC)SubclassWindow(tmp, _BtProc);
-
-	SetWindowLong(tmp, GWL_USERDATA, (ULONG)bubble);
-
-	SendMessage(tmp, BM_SETIMAGE,
-				(WPARAM) IMAGE_BITMAP,
-				(LPARAM) hbitmap);
+	bubble->pOldProc = WinSubclassWindow(tmp, _BtProc);
+
+	WinSetWindowPtr(tmp, QWP_USER, bubble);
 	return tmp;
 }
 
@@ -3610,45 +3703,20 @@
  */
 HWND dw_spinbutton_new(char *text, ULONG id)
 {
-	ULONG *data = malloc(sizeof(ULONG));
-	HWND buddy = CreateWindow(EDITCLASSNAME,
-							  text,
-							  WS_CHILD | WS_BORDER |
-							  ES_NUMBER | WS_CLIPCHILDREN,
-							  0,0,2000,1000,
-							  DW_HWND_OBJECT,
-							  NULL,
-							  NULL,
-							  NULL);
-	HWND tmp = CreateUpDownControl(
-								   WS_CHILD | UDS_ALIGNRIGHT |
-								   UDS_ARROWKEYS | UDS_SETBUDDYINT |
-								   UDS_WRAP | UDS_NOTHOUSANDS,
-								   0,
-								   0,
-								   2000,
-								   1000,
-								   DW_HWND_OBJECT,
-								   id,
-								   DWInstance,
-								   buddy,
-								   0,
-								   100,
-								   0);
-	ColorInfo *cinfo = calloc(1, sizeof(ColorInfo));
-
-	cinfo->back = cinfo->fore = -1;
-	cinfo->buddy = tmp;
-
-	cinfo->pOldProc = SubclassWindow(buddy, _colorwndproc);
-	SetWindowLong(buddy, GWL_USERDATA, (ULONG)cinfo);
-
-	cinfo = calloc(1, sizeof(ColorInfo));
-	cinfo->buddy = buddy;
-	cinfo->pOldProc = SubclassWindow(tmp, _spinnerwndproc);
-
-	SetWindowLong(tmp, GWL_USERDATA, (ULONG)cinfo);
-	dw_window_set_font(buddy, DefaultFont);
+	PFNWP *blah = malloc(sizeof(PFNWP));
+	HWND tmp = WinCreateWindow(HWND_OBJECT,
+							   WC_SPINBUTTON,
+							   text,
+							   WS_VISIBLE | SPBS_MASTER,
+							   0,0,2000,1000,
+							   NULLHANDLE,
+							   HWND_TOP,
+							   id,
+							   NULL,
+							   NULL);
+	dw_window_set_font(tmp, DefaultFont);
+	*blah = WinSubclassWindow(tmp, _entryproc);
+	WinSetWindowPtr(tmp, QWP_USER, blah);
 	return tmp;
 }
 
@@ -3660,29 +3728,25 @@
  */
 HWND dw_radiobutton_new(char *text, ULONG id)
 {
-	HWND tmp = CreateWindow(BUTTONCLASSNAME,
-							text,
-							WS_CHILD | BS_AUTORADIOBUTTON |
-							WS_CLIPCHILDREN,
-							0,0,2000,1000,
-							DW_HWND_OBJECT,
-							(HMENU)id,
-							NULL,
-							NULL);
-
-	ColorInfo *cinfo = calloc(1, sizeof(ColorInfo));
-
-	cinfo->back = cinfo->fore = -1;
-	cinfo->buddy = 0;
-	cinfo->user = 0;
-
-	cinfo->pOldProc = SubclassWindow(tmp, _colorwndproc);
-	SetWindowLong(tmp, GWL_USERDATA, (ULONG)cinfo);
+	PFNWP *blah = malloc(sizeof(PFNWP));
+	HWND tmp = WinCreateWindow(HWND_OBJECT,
+							   WC_BUTTON,
+							   text,
+							   WS_VISIBLE |
+							   BS_AUTORADIOBUTTON,
+							   0,0,2000,1000,
+							   NULLHANDLE,
+							   HWND_TOP,
+							   id,
+							   NULL,
+							   NULL);
 	dw_window_set_font(tmp, DefaultFont);
+	dw_window_set_color(tmp, DW_CLR_BLACK, DW_CLR_PALEGRAY);
+	*blah = WinSubclassWindow(tmp, _entryproc);
+	WinSetWindowPtr(tmp, QWP_USER, blah);
 	return tmp;
 }
 
-
 /*
  * Create a new percent bar window (widget) to be packed.
  * Parameters:
@@ -3690,14 +3754,24 @@
  */
 HWND dw_percent_new(ULONG id)
 {
-	return CreateWindow(PROGRESS_CLASS,
-						"",
-						WS_CHILD | WS_CLIPCHILDREN,
-						0,0,2000,1000,
-						DW_HWND_OBJECT,
-						NULL,
-						NULL,
-						NULL);
+	PercentBar *blah = malloc(sizeof(PercentBar));
+	HWND tmp = WinCreateWindow(HWND_OBJECT,
+							   WC_STATIC,
+							   "",
+							   WS_VISIBLE | SS_TEXT,
+							   0,0,2000,1000,
+							   NULLHANDLE,
+							   HWND_TOP,
+							   id,
+							   NULL,
+							   NULL);
+	dw_window_set_font(tmp, DefaultFont);
+	dw_window_set_color(tmp, DW_CLR_BLUE, DW_CLR_PALEGRAY);
+
+	blah->oldproc = WinSubclassWindow(tmp, _percentproc);
+	blah->pos = 0;
+	WinSetWindowPtr(tmp, QWP_USER, blah);
+	return tmp;
 }
 
 /*
@@ -3708,24 +3782,21 @@
  */
 HWND dw_checkbox_new(char *text, ULONG id)
 {
-	HWND tmp = CreateWindow(BUTTONCLASSNAME,
-							text,
-							WS_CHILD | BS_AUTOCHECKBOX |
-							BS_TEXT | WS_CLIPCHILDREN,
-							0,0,2000,1000,
-							DW_HWND_OBJECT,
-							NULL,
-							NULL,
-							NULL);
-	ColorInfo *cinfo = calloc(1, sizeof(ColorInfo));
-
-	cinfo->back = cinfo->fore = -1;
-	cinfo->buddy = 0;
-	cinfo->user = 1;
-
-	cinfo->pOldProc = SubclassWindow(tmp, _colorwndproc);
-	SetWindowLong(tmp, GWL_USERDATA, (ULONG)cinfo);
+	PFNWP *blah = malloc(sizeof(PFNWP));
+	HWND tmp = WinCreateWindow(HWND_OBJECT,
+							   WC_BUTTON,
+							   text,
+							   WS_VISIBLE | BS_AUTOCHECKBOX,
+							   0,0,2000,1000,
+							   NULLHANDLE,
+							   HWND_TOP,
+							   id,
+							   NULL,
+							   NULL);
 	dw_window_set_font(tmp, DefaultFont);
+	dw_window_set_color(tmp, DW_CLR_BLACK, DW_CLR_PALEGRAY);
+	*blah = WinSubclassWindow(tmp, _entryproc);
+	WinSetWindowPtr(tmp, QWP_USER, blah);
 	return tmp;
 }
 
@@ -3737,30 +3808,21 @@
  */
 HWND dw_listbox_new(ULONG id, int multi)
 {
-	HWND tmp = CreateWindow(LISTBOXCLASSNAME,
-							"",
-							LBS_NOINTEGRALHEIGHT | WS_CHILD | LBS_HASSTRINGS |
-							LBS_NOTIFY | WS_BORDER  | WS_CLIPCHILDREN |
-							WS_VSCROLL | (multi ? LBS_EXTENDEDSEL : 0) ,
-							0,0,2000,1000,
-							DW_HWND_OBJECT,
-							NULL,
-							NULL,
-							NULL);
-	ContainerInfo *cinfo = (ContainerInfo *)calloc(1, sizeof(ContainerInfo));
-
-	if(!cinfo)
-	{
-		DestroyWindow(tmp);
-		return NULL;
-	}
-
-	cinfo->cinfo.fore = -1;
-	cinfo->cinfo.back = -1;
-	cinfo->pOldProc = (WNDPROC)SubclassWindow(tmp, _containerwndproc);
-
-	SetWindowLong(tmp, GWL_USERDATA, (ULONG)cinfo);
+	PFNWP *blah = malloc(sizeof(PFNWP));
+	HWND tmp = WinCreateWindow(HWND_OBJECT,
+							   WC_LISTBOX,
+							   NULL,
+							   WS_VISIBLE | LS_NOADJUSTPOS |
+							   (multi ? LS_MULTIPLESEL : 0),
+							   0,0,2000,1000,
+							   NULLHANDLE,
+							   HWND_TOP,
+							   id,
+							   NULL,
+							   NULL);
 	dw_window_set_font(tmp, DefaultFont);
+	*blah = WinSubclassWindow(tmp, _entryproc);
+	WinSetWindowPtr(tmp, QWP_USER, blah);
 	return tmp;
 }
 
@@ -3772,11 +3834,10 @@
  */
 void dw_window_set_icon(HWND handle, ULONG id)
 {
-	HICON hicon = LoadIcon(DWInstance, MAKEINTRESOURCE(id));
-
-	SendMessage(handle, WM_SETICON,
-				(WPARAM) IMAGE_ICON,
-				(LPARAM) hicon);
+	HPOINTER icon;
+
+	icon = WinLoadPointer(HWND_DESKTOP,NULLHANDLE,id);
+	WinSendMsg(handle, WM_SETICON, (MPARAM)icon, 0);
 }
 
 /*
@@ -3787,11 +3848,14 @@
  */
 void dw_window_set_bitmap(HWND handle, ULONG id)
 {
-	HBITMAP hbitmap = LoadBitmap(DWInstance, MAKEINTRESOURCE(id));
-
-	SendMessage(handle, STM_SETIMAGE,
-				(WPARAM) IMAGE_BITMAP,
-				(LPARAM) hbitmap);
+	HBITMAP hbm;
+	HPS     hps = WinGetPS(handle);
+
+	hbm = GpiLoadBitmap(hps, NULLHANDLE, id, 0, 0);
+	WinSetWindowBits(handle,QWL_STYLE,SS_BITMAP,SS_BITMAP | 0x7f);
+	WinSendMsg( handle, SM_SETHANDLE, MPFROMP(hbm), NULL );
+	/*WinSetWindowULong( hwndDlg, QWL_USER, (ULONG) hbm );*/
+	WinReleasePS(hps);
 }
 
 /*
@@ -3802,7 +3866,7 @@
  */
 void dw_window_set_text(HWND handle, char *text)
 {
-	SetWindowText(handle, text);
+	WinSetWindowText(handle, text);
 }
 
 /*
@@ -3816,7 +3880,7 @@
 {
 	char tempbuf[4096] = "";
 
-	GetWindowText(handle, tempbuf, 4095);
+	WinQueryWindowText(handle, 4095, tempbuf);
 	tempbuf[4095] = 0;
 
 	return strdup(tempbuf);
@@ -3829,7 +3893,7 @@
  */
 void dw_window_disable(HWND handle)
 {
-	EnableWindow(handle, FALSE);
+	WinEnableWindow(handle, FALSE);
 }
 
 /*
@@ -3839,7 +3903,7 @@
  */
 void dw_window_enable(HWND handle)
 {
-	EnableWindow(handle, TRUE);
+	WinEnableWindow(handle, TRUE);
 }
 
 /*
@@ -3850,10 +3914,35 @@
  */
 HWND dw_window_from_id(HWND handle, int id)
 {
-    return 0L;
-}
-/*
- * Pack windows (widgets) into a box from the start (or top).
+	HENUM henum;
+	HWND child;
+	char tmpbuf[100];
+
+	henum = WinBeginEnumWindows(handle);
+	while((child = WinGetNextWindow(henum)) != NULLHANDLE)
+	{
+		int windowid = WinQueryWindowUShort(child, QWS_ID);
+		HWND found;
+
+		WinQueryClassName(child, 99, tmpbuf);
+
+		/* If the child is a box (frame) then recurse into it */
+		if(strncmp(tmpbuf, "#1", 3)==0)
+			if((found = dw_window_from_id(child, id)) != NULLHANDLE)
+				return found;
+
+		if(windowid && windowid == id)
+		{
+			WinEndEnumWindows(henum);
+			return child;
+		}
+	}
+	WinEndEnumWindows(henum);
+	return NULLHANDLE;
+}
+
+/*
+ * Pack windows (widgets) into a box from the end (or bottom).
  * Parameters:
  *       box: Window handle of the box to be packed into.
  *       item: Window handle of the item to be back.
@@ -3863,11 +3952,50 @@
  *       vsize: TRUE if the window (widget) should expand vertically to fill space given.
  *       pad: Number of pixels of padding around the item.
  */
-void dw_box_pack_start(HWND box, HWND item, int width, int height, int hsize, int vsize, int pad)
+void dw_box_pack_end(HWND box, HWND item, int width, int height, int hsize, int vsize, int pad)
 {
 	Box *thisbox;
 
-	thisbox = (Box *)GetWindowLong(box, GWL_USERDATA);
+	if(WinWindowFromID(box, FID_CLIENT))
+	{
+		box = WinWindowFromID(box, FID_CLIENT);
+		thisbox = WinQueryWindowPtr(box, QWP_USER);
+	}
+	else
+		thisbox = WinQueryWindowPtr(box, QWP_USER);
+	if(thisbox)
+	{
+		if(thisbox->type == BOXHORZ)
+			dw_box_pack_start_stub(box, item, width, height, hsize, vsize, pad);
+		else
+			dw_box_pack_end_stub(box, item, width, height, hsize, vsize, pad);
+	}
+}
+
+void dw_box_pack_end_stub(HWND box, HWND item, int width, int height, int hsize, int vsize, int pad)
+{
+    HWND boxowner = NULLHANDLE;
+	Box *thisbox;
+
+	if(WinWindowFromID(box, FID_CLIENT))
+	{
+		box = WinWindowFromID(box, FID_CLIENT);
+		thisbox = WinQueryWindowPtr(box, QWP_USER);
+		hsize = TRUE;
+		vsize = TRUE;
+	}
+	else
+		thisbox = WinQueryWindowPtr(box, QWP_USER);
+	if(!thisbox)
+	{
+		box = WinWindowFromID(box, FID_CLIENT);
+		if(box)
+		{
+			thisbox = WinQueryWindowPtr(box, QWP_USER);
+			hsize = TRUE;
+			vsize = TRUE;
+		}
+	}
 	if(thisbox)
 	{
 		int z;
@@ -3881,9 +4009,9 @@
 			tmpitem[z] = thisitem[z];
 		}
 
-		GetClassName(item, tmpbuf, 99);
-
-		if(strnicmp(tmpbuf, FRAMECLASSNAME, 2)==0)
+		WinQueryClassName(item, 99, tmpbuf);
+
+		if(strncmp(tmpbuf, "#1", 3)==0)
 			tmpitem[thisbox->count].type = TYPEBOX;
 		else
 			tmpitem[thisbox->count].type = TYPEITEM;
@@ -3909,19 +4037,16 @@
 
 		thisbox->count++;
 
-		SetParent(item, box);
-		ShowWindow(item, SW_SHOW);
-		if(strncmp(tmpbuf, UPDOWN_CLASS, strlen(UPDOWN_CLASS))==0)
+        /* Don't set the ownership if it's an entryfield  or spinbutton */
+		WinQueryClassName(item, 99, tmpbuf);
+		if(strncmp(tmpbuf, "#6", 3)!=0 && strncmp(tmpbuf, "#32", 3)!=0)
 		{
-			ColorInfo *cinfo = (ColorInfo *)GetWindowLong(item, GWL_USERDATA);
-
-			if(cinfo)
-			{
-				SetParent(cinfo->buddy, box);
-				ShowWindow(cinfo->buddy, SW_SHOW);
-				SendMessage(item, UDM_SETBUDDY, (WPARAM)cinfo->buddy, 0);
-			}
+			if((boxowner = WinQueryWindow(box, QW_OWNER)) != 0)
+				WinSetOwner(item, boxowner);
+			else
+				WinSetOwner(item, box);
 		}
+		WinSetParent(item, box, FALSE);
 	}
 }
 
@@ -3934,7 +4059,7 @@
  */
 void dw_window_set_usize(HWND handle, ULONG width, ULONG height)
 {
-	SetWindowPos(handle, (HWND)NULL, 0, 0, width, height, SWP_SHOWWINDOW | SWP_NOZORDER | SWP_NOMOVE);
+	WinSetWindowPos(handle, NULLHANDLE, 0, 0, width, height, SWP_SHOW | SWP_SIZE);
 }
 
 /*
@@ -3942,7 +4067,7 @@
  */
 int dw_screen_width(void)
 {
-	return GetSystemMetrics(SM_CXSCREEN);
+	return WinQuerySysValue(HWND_DESKTOP,SV_CXSCREEN);
 }
 
 /*
@@ -3950,20 +4075,18 @@
  */
 int dw_screen_height(void)
 {
-	return GetSystemMetrics(SM_CYSCREEN);
+	return WinQuerySysValue(HWND_DESKTOP,SV_CYSCREEN);
 }
 
 /* This should return the current color depth */
 unsigned long dw_color_depth(void)
 {
-	int bpp;
-	HDC hdc = GetDC(HWND_DESKTOP);
-    
-	bpp = GetDeviceCaps(hdc, BITSPIXEL);
-
-	ReleaseDC(HWND_DESKTOP, hdc);
-
-	return bpp;
+	HDC hdc = WinOpenWindowDC(HWND_DESKTOP);
+	long colors;
+
+	DevQueryCaps(hdc, CAPS_COLOR_BITCOUNT, 1, &colors);
+	DevCloseDC(hdc);
+	return colors;
 }
 
 
@@ -3976,7 +4099,9 @@
  */
 void dw_window_set_pos(HWND handle, ULONG x, ULONG y)
 {
-	SetWindowPos(handle, (HWND)NULL, x, y, 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
+	int myy = _get_frame_height(handle) - (y + _get_height(handle));
+
+	WinSetWindowPos(handle, NULLHANDLE, x, myy, 0, 0, SWP_MOVE);
 }
 
 /*
@@ -3990,7 +4115,9 @@
  */
 void dw_window_set_pos_size(HWND handle, ULONG x, ULONG y, ULONG width, ULONG height)
 {
-	SetWindowPos(handle, (HWND)NULL, x, y, width, height, SWP_NOZORDER | SWP_SHOWWINDOW | SWP_NOACTIVATE);
+	int myy = _get_frame_height(handle) - (y + height);
+
+	WinSetWindowPos(handle, NULLHANDLE, x, myy, width, height, SWP_MOVE | SWP_SIZE | SWP_SHOW);
 }
 
 /*
@@ -4004,20 +4131,16 @@
  */
 void dw_window_get_pos_size(HWND handle, ULONG *x, ULONG *y, ULONG *width, ULONG *height)
 {
-	WINDOWPLACEMENT wp;
-
-	wp.length = sizeof(WINDOWPLACEMENT);
-
-	GetWindowPlacement(handle, &wp);
+	SWP swp;
+	WinQueryWindowPos(handle, &swp);
 	if(x)
-		*x = wp.rcNormalPosition.left;
+		*x = swp.x;
 	if(y)
-		*y = wp.rcNormalPosition.top;
+		*y = _get_frame_height(handle) - (swp.y + swp.cy);
 	if(width)
-		*width = wp.rcNormalPosition.right - wp.rcNormalPosition.left;
+		*width = swp.cx;
 	if(height)
-		*height = wp.rcNormalPosition.bottom - wp.rcNormalPosition.top;
-
+		*height = swp.cy;
 }
 
 /*
@@ -4029,26 +4152,7 @@
  */
 void dw_window_set_style(HWND handle, ULONG style, ULONG mask)
 {
-	ULONG tmp, currentstyle = GetWindowLong(handle, GWL_STYLE);
-
-	tmp = currentstyle | mask;
-	tmp ^= mask;
-	tmp |= style;
-
-	SetWindowLong(handle, GWL_STYLE, tmp);
-}
-
-/* Finds the physical ID from the reference ID */
-int _findnotebookid(NotebookPage **array, int pageid)
-{
-	int z;
-
-	for(z=0;z<256;z++)
-	{
-		if(array[z] && array[z]->realid == pageid)
-			return z;
-	}
-	return -1;
+	WinSetWindowBits(handle, QWL_STYLE, style, mask);
 }
 
 /*
@@ -4060,46 +4164,44 @@
  */
 ULONG dw_notebook_page_new(HWND handle, ULONG flags, int front)
 {
-	NotebookPage **array = (NotebookPage **)GetWindowLong(handle, GWL_USERDATA);
-
-	if(array)
-	{
-		int z, refid = -1;
-
-		for(z=0;z<256;z++)
-		{
-			if(_findnotebookid(array, z) == -1)
-			{
-				refid = z;
-				break;
-			}
-		}
-
-		if(refid == -1)
-			return -1;
-
-		for(z=0;z<256;z++)
-		{
-			if(!array[z])
-			{
-				int oldpage = TabCtrl_GetCurSel(handle);
-
-				array[z] = calloc(1, sizeof(NotebookPage));
-				array[z]->realid = refid;
-				array[z]->item.mask = TCIF_TEXT;
-				array[z]->item.iImage = -1;
-				array[z]->item.pszText = "";
-				TabCtrl_InsertItem(handle, z, &(array[z]->item));
-
-				if(oldpage > -1 && array[oldpage])
-					SetParent(array[oldpage]->hwnd, DW_HWND_OBJECT);
-
-				TabCtrl_SetCurSel(handle, z);
-				return refid;
-			}
-		}
-	}
-	return -1;
+	if(front)
+		return (ULONG)WinSendMsg(handle, BKM_INSERTPAGE, 0L,
+						  MPFROM2SHORT((BKA_STATUSTEXTON | BKA_AUTOPAGESIZE | BKA_MAJOR | flags), BKA_FIRST));
+	return (ULONG)WinSendMsg(handle, BKM_INSERTPAGE, 0L,
+					  MPFROM2SHORT((BKA_STATUSTEXTON | BKA_AUTOPAGESIZE | BKA_MAJOR | flags), BKA_LAST));
+}
+
+/*
+ * Remove a page from a notebook.
+ * Parameters:
+ *          handle: Handle to the notebook widget.
+ *          pageid: ID of the page to be destroyed.
+ */
+void dw_notebook_page_destroy(HWND handle, unsigned int pageid)
+{
+	WinSendMsg(handle, BKM_DELETEPAGE,
+			   MPFROMLONG(pageid),	(MPARAM)BKA_SINGLE);
+}
+
+/*
+ * Queries the currently visible page ID.
+ * Parameters:
+ *          handle: Handle to the notebook widget.
+ */
+unsigned int dw_notebook_page_query(HWND handle)
+{
+	return (int)WinSendMsg(handle, BKM_QUERYPAGEID,0L, MPFROM2SHORT(BKA_TOP, BKA_MAJOR));
+}
+
+/*
+ * Sets the currently visibale page ID.
+ * Parameters:
+ *          handle: Handle to the notebook widget.
+ *          pageid: ID of the page to be made visible.
+ */
+void dw_notebook_page_set(HWND handle, unsigned int pageid)
+{
+	WinSendMsg(handle, BKM_TURNTOPAGE, MPFROMLONG(pageid), 0L);
 }
 
 /*
@@ -4109,24 +4211,10 @@
  *          pageid: Page ID of the tab to set.
  *          text: Pointer to the text to set.
  */
-void dw_notebook_page_set_text(HWND handle, ULONG pageidx, char *text)
-{
-
-	NotebookPage **array = (NotebookPage **)GetWindowLong(handle, GWL_USERDATA);
-	int pageid;
-
-	if(!array)
-		return;
-
-	pageid = _findnotebookid(array, pageidx);
-
-	if(pageid > -1 && array[pageid])
-	{
-		array[pageid]->item.mask = TCIF_TEXT;
-		array[pageid]->item.pszText = text;
-		TabCtrl_SetItem(handle, pageid, &(array[pageid]->item));
-		_resize_notebook_page(handle, pageid);
-	}
+void dw_notebook_page_set_text(HWND handle, ULONG pageid, char *text)
+{
+	WinSendMsg(handle, BKM_SETTABTEXT,
+			   MPFROMLONG(pageid),	MPFROMP(text));
 }
 
 /*
@@ -4138,6 +4226,8 @@
  */
 void dw_notebook_page_set_status_text(HWND handle, ULONG pageid, char *text)
 {
+	WinSendMsg(handle, BKM_SETSTATUSLINETEXT,
+			   MPFROMLONG(pageid),	MPFROMP(text));
 }
 
 /*
@@ -4147,125 +4237,14 @@
  *          pageid: Page ID in the notebook which is being packed.
  *          page: Box handle to be packed.
  */
-void dw_notebook_pack(HWND handle, ULONG pageidx, HWND page)
-{
-	NotebookPage **array = (NotebookPage **)GetWindowLong(handle, GWL_USERDATA);
-	int pageid;
-
-	if(!array)
-		return;
-
-	pageid = _findnotebookid(array, pageidx);
-
-	if(pageid > -1 && array[pageid])
-	{
-		HWND tmpbox = dw_box_new(BOXVERT, 0);
-
-		dw_box_pack_start(tmpbox, page, 0, 0, TRUE, TRUE, 0);
-		SubclassWindow(tmpbox, _wndproc);
-		if(array[pageid]->hwnd)
-			dw_window_destroy(array[pageid]->hwnd);
-		array[pageid]->hwnd = tmpbox;
-		if(pageidx == dw_notebook_page_query(handle))
-		{
-			SetParent(tmpbox, handle);
-			_resize_notebook_page(handle, pageid);
-		}
-	}
-}
-
-/*
- * Remove a page from a notebook.
- * Parameters:
- *          handle: Handle to the notebook widget.
- *          pageid: ID of the page to be destroyed.
- */
-void dw_notebook_page_destroy(HWND handle, unsigned int pageidx)
-{
-	NotebookPage **array = (NotebookPage **)GetWindowLong(handle, GWL_USERDATA);
-	int newid = -1, z, pageid;
-
-	if(!array)
-		return;
-
-	pageid = _findnotebookid(array, pageidx);
-
-	if(pageid < 0)
-		return;
-
-	if(array[pageid])
-	{
-		SetParent(array[pageid]->hwnd, DW_HWND_OBJECT);
-		free(array[pageid]);
-		array[pageid] = NULL;
-	}
-
-	TabCtrl_DeleteItem(handle, pageid);
-
-	/* Shift the pages over 1 */
-	for(z=pageid;z<255;z++)
-		array[z] = array[z+1];
-	array[255] = NULL;
-
-	for(z=0;z<256;z++)
-	{
-		if(array[z])
-		{
-			newid = z;
-			break;
-		}
-	}
-	if(newid > -1)
-	{
-		SetParent(array[newid]->hwnd, handle);
-		_resize_notebook_page(handle, newid);
-		dw_notebook_page_set(handle, array[newid]->realid);
-	}
-}
-
-/*
- * Queries the currently visible page ID.
- * Parameters:
- *          handle: Handle to the notebook widget.
- */
-unsigned int dw_notebook_page_query(HWND handle)
-{
-	NotebookPage **array = (NotebookPage **)GetWindowLong(handle, GWL_USERDATA);
-	int physid = TabCtrl_GetCurSel(handle);
-
-	if(physid > -1 && physid < 256 && array && array[physid])
-		return array[physid]->realid;
-	return -1;
-}
-
-/*
- * Sets the currently visible page ID.
- * Parameters:
- *          handle: Handle to the notebook widget.
- *          pageid: ID of the page to be made visible.
- */
-void dw_notebook_page_set(HWND handle, unsigned int pageidx)
-{
-	NotebookPage **array = (NotebookPage **)GetWindowLong(handle, GWL_USERDATA);
-	int pageid;
-
-	if(!array)
-		return;
-
-	pageid = _findnotebookid(array, pageidx);
-
-	if(pageid > -1 && pageid < 256)
-	{
-		int oldpage = TabCtrl_GetCurSel(handle);
-
-		if(oldpage > -1 && array && array[oldpage])
-			SetParent(array[oldpage]->hwnd, DW_HWND_OBJECT);
-
-		TabCtrl_SetCurSel(handle, pageid);
-
-		SetParent(array[pageid]->hwnd, handle);
-		_resize_notebook_page(handle, pageid);
-	}
+void dw_notebook_pack(HWND handle, ULONG pageid, HWND page)
+{
+	HWND tmpbox = dw_box_new(BOXVERT, 0);
+
+	dw_box_pack_start(tmpbox, page, 0, 0, TRUE, TRUE, 0);
+	WinSubclassWindow(tmpbox, _wndproc);
+	WinSendMsg(handle, BKM_SETPAGEWINDOWHWND,
+			   MPFROMLONG(pageid),	MPFROMHWND(tmpbox));
 }
 
 /*
@@ -4276,18 +4255,10 @@
  */
 void dw_listbox_append(HWND handle, char *text)
 {
-	char tmpbuf[100];
-
-	GetClassName(handle, tmpbuf, 99);
-
-	if(strnicmp(tmpbuf, COMBOBOXCLASSNAME, strlen(COMBOBOXCLASSNAME))==0)
-		SendMessage(handle,
-					CB_ADDSTRING,
-					0, (LPARAM)text);
-	else
-		SendMessage(handle,
-					LB_ADDSTRING,
-					0, (LPARAM)text);
+	WinSendMsg(handle,
+			   LM_INSERTITEM,
+			   MPFROMSHORT(LIT_END),
+			   MPFROMP(text));
 }
 
 /*
@@ -4297,42 +4268,33 @@
  */
 void dw_listbox_clear(HWND handle)
 {
-	char tmpbuf[100];
-
-	GetClassName(handle, tmpbuf, 99);
-
-	if(strnicmp(tmpbuf, COMBOBOXCLASSNAME, strlen(COMBOBOXCLASSNAME))==0)
-	{
-		char *buf = dw_window_get_text(handle);
-
-		SendMessage(handle,
-					CB_RESETCONTENT, 0L, 0L);
-
-		if(buf)
-		{
-			dw_window_set_text(handle, buf);
-			free(buf);
-		}
-	}
-	else
-		SendMessage(handle,
-					LB_RESETCONTENT, 0L, 0L);
-}
-
-/*
- * Sets the text of a given listbox entry.
- * Parameters:
- *          handle: Handle to the listbox to be queried.
- *          index: Index into the list to be queried.
- *          buffer: Buffer where text will be copied.
- */
-void dw_listbox_set_text(HWND handle, unsigned int index, char *buffer)
-{
-	unsigned int sel = (unsigned int)SendMessage(handle, LB_GETCURSEL, 0, 0);
-	SendMessage(handle,	LB_DELETESTRING, (WPARAM)index, 0);
-	SendMessage(handle, LB_INSERTSTRING, (WPARAM)index, (LPARAM)buffer);
-	SendMessage(handle, LB_SETCURSEL, (WPARAM)sel, 0);
-	SendMessage(handle, LB_SETSEL, (WPARAM)TRUE, (LPARAM)sel);
+	WinSendMsg(handle,
+			   LM_DELETEALL, 0L, 0L);
+}
+
+/*
+ * Returns the listbox's item count.
+ * Parameters:
+ *          handle: Handle to the listbox to be cleared.
+ */
+int dw_listbox_count(HWND handle)
+{
+	return (int)WinSendMsg(handle,
+						   LM_QUERYITEMCOUNT,0L, 0L);
+}
+
+/*
+ * Sets the topmost item in the viewport.
+ * Parameters:
+ *          handle: Handle to the listbox to be cleared.
+ *          top: Index to the top item.
+ */
+void dw_listbox_set_top(HWND handle, int top)
+{
+	WinSendMsg(handle,
+			   LM_SETTOPINDEX,
+			   MPFROMSHORT(top),
+			   0L);
 }
 
 /*
@@ -4345,8 +4307,19 @@
  */
 void dw_listbox_query_text(HWND handle, unsigned int index, char *buffer, unsigned int length)
 {
-	SendMessage(handle,
-				LB_GETTEXT, (WPARAM)index, (LPARAM)buffer);
+	WinSendMsg(handle, LM_QUERYITEMTEXT, MPFROM2SHORT(index, length), (MPARAM)buffer);
+}
+
+/*
+ * Sets the text of a given listbox entry.
+ * Parameters:
+ *          handle: Handle to the listbox to be queried.
+ *          index: Index into the list to be queried.
+ *          buffer: Buffer where text will be copied.
+ */
+void dw_listbox_set_text(HWND handle, unsigned int index, char *buffer)
+{
+	WinSendMsg(handle, LM_SETITEMTEXT, MPFROMSHORT(index), (MPARAM)buffer);
 }
 
 /*
@@ -4356,18 +4329,10 @@
  */
 unsigned int dw_listbox_selected(HWND handle)
 {
-	char tmpbuf[100];
-
-	GetClassName(handle, tmpbuf, 99);
-
-	if(strnicmp(tmpbuf, COMBOBOXCLASSNAME, strlen(COMBOBOXCLASSNAME))==0)
-		return (unsigned int)SendMessage(handle,
-										 CB_GETCURSEL,
-										 0, 0);
-
-	return (unsigned int)SendMessage(handle,
-									 LB_GETCURSEL,
-									 0, 0);
+		return (unsigned int)WinSendMsg(handle,
+										LM_QUERYSELECTION,
+										MPFROMSHORT(LIT_CURSOR),
+										0);
 }
 
 /*
@@ -4378,32 +4343,17 @@
  */
 int dw_listbox_selected_multi(HWND handle, int where)
 {
-	int *array, count, z;
-
-	count = (int)SendMessage(handle, LB_GETSELCOUNT, 0, 0);
-	if(count > 0)
-	{
-		array = malloc(sizeof(int)*count);
-		SendMessage(handle, LB_GETSELITEMS, (WPARAM)count, (LPARAM)array);
-
-		if(where == -1)
-		{
-			int ret = array[0];
-			free(array);
-			return ret;
-		}
-		for(z=0;z<count;z++)
-		{
-			if(array[z] == where && (z+1) < count)
-			{
-				int ret = array[z+1];
-				free(array);
-				return ret;
-			}
-		}
-		free(array);
-	}
-	return -1;
+	int place = where;
+
+	if(where == -1)
+		place = LIT_FIRST;
+
+	place = (int)WinSendMsg(handle,
+							LM_QUERYSELECTION,
+							MPFROMSHORT(place),0L);
+	if(place == LIT_NONE)
+		return -1;
+	return place;
 }
 
 /*
@@ -4417,16 +4367,13 @@
 {
 	char tmpbuf[100];
 
-	GetClassName(handle, tmpbuf, 99);
-
-	if(strnicmp(tmpbuf, COMBOBOXCLASSNAME, strlen(COMBOBOXCLASSNAME))==0)
-		SendMessage(handle, CB_SETCURSEL, (WPARAM)index, 0);
-	else
-	{
-		SendMessage(handle, LB_SETCURSEL, (WPARAM)index, 0);
-		SendMessage(handle, LB_SETSEL, (WPARAM)state, (LPARAM)index);
-	}
-	_wndproc(handle, WM_COMMAND, (WPARAM)(LBN_SELCHANGE << 16), (LPARAM)handle);
+	WinSendMsg(handle, LM_SELECTITEM, MPFROMSHORT(index), (MPARAM)state);
+
+	WinQueryClassName(handle, 99, tmpbuf);
+
+	/* If we are setting a combobox call the event handler manually */
+	if(strncmp(tmpbuf, "#6", 3)==0)
+		_run_event(handle, WM_CONTROL, MPFROM2SHORT(0, LN_SELECT), (MPARAM)handle);
 }
 
 /*
@@ -4437,40 +4384,9 @@
  */
 void dw_listbox_delete(HWND handle, int index)
 {
-	SendMessage(handle, LB_DELETESTRING, (WPARAM)index, 0);
-}
-
-/*
- * Returns the listbox's item count.
- * Parameters:
- *          handle: Handle to the listbox to be cleared.
- */
-int dw_listbox_count(HWND handle)
-{
-	char tmpbuf[100];
-
-	GetClassName(handle, tmpbuf, 99);
-
-	if(strnicmp(tmpbuf, COMBOBOXCLASSNAME, strlen(COMBOBOXCLASSNAME))==0)
-		return (int)SendMessage(handle,
-								CB_GETCOUNT,0L, 0L);
-
-	return (int)SendMessage(handle,
-							LB_GETCOUNT,0L, 0L);
-}
-
-/*
- * Sets the topmost item in the viewport.
- * Parameters:
- *          handle: Handle to the listbox to be cleared.
- *          top: Index to the top item.
- */
-void dw_listbox_set_top(HWND handle, int top)
-{
-	SendMessage(handle, LB_SETTOPINDEX, (WPARAM)top, 0);
-}
-
-#define MLE_MAX 200000
+	WinSendMsg(handle, LM_DELETEITEM, MPFROMSHORT(index), 0);
+}
+
 /*
  * Adds text to an MLE box and returns the current point.
  * Parameters:
@@ -4480,34 +4396,33 @@
  */
 unsigned int dw_mle_import(HWND handle, char *buffer, int startpoint)
 {
-	char *tmpbuf = calloc(1, MLE_MAX+1);
-	int len;
-
-	if(strlen(buffer) < 1)
-		return startpoint;
-
-	startpoint++;
-
-	if(startpoint < 0)
-		startpoint = 0;
-
-	GetWindowText(handle, tmpbuf, MLE_MAX);
-
-	len = strlen(tmpbuf);
-	if(len)
+	unsigned long point = startpoint;
+	PBYTE mlebuf;
+
+	/* Work around 64K limit */
+	if(!DosAllocMem((PPVOID) &mlebuf, 65536, PAG_COMMIT | PAG_READ | PAG_WRITE | OBJ_TILE))
 	{
-		char *dest = &tmpbuf[startpoint+strlen(buffer)-1], *start = &tmpbuf[startpoint];
-		int copylen = len - startpoint;
-
-		if(copylen > 0)
-			memcpy(dest, start, copylen);
+		int amount, len = strlen(buffer), written = 0;
+
+		while(written < len)
+		{
+			if((len - written) > 65535)
+				amount = 65535;
+			else
+				amount = len - written;
+
+			memcpy(mlebuf, &buffer[written], amount);
+			mlebuf[amount] = '\0';
+
+			WinSendMsg(handle, MLM_SETIMPORTEXPORT, MPFROMP(mlebuf), MPFROMLONG(amount+1));
+			WinSendMsg(handle, MLM_IMPORT, MPFROMP(&point), MPFROMLONG(amount + 1));
+			dw_mle_delete(handle, point, 1);
+
+			written += amount;
+		}
+		DosFreeMem(mlebuf);
 	}
-	memcpy(&tmpbuf[startpoint], buffer, strlen(buffer));
-
-	SetWindowText(handle, tmpbuf);
-
-	free(tmpbuf);
-	return startpoint+strlen(buffer) - 1;
+	return point - 1;
 }
 
 /*
@@ -4520,14 +4435,34 @@
  */
 void dw_mle_export(HWND handle, char *buffer, int startpoint, int length)
 {
-	char *tmpbuf = malloc(MLE_MAX+1);
-
-	GetWindowText(handle, tmpbuf, MLE_MAX);
-	tmpbuf[MLE_MAX] = 0;
-
-	memcpy(buffer, &tmpbuf[startpoint], length);
-
-	free(tmpbuf);
+	PBYTE mlebuf;
+
+	/* Work around 64K limit */
+	if(!DosAllocMem((PPVOID) &mlebuf, 65535, PAG_COMMIT | PAG_READ | PAG_WRITE | OBJ_TILE))
+	{
+		int amount, copied, written = 0;
+
+		while(written < length)
+		{
+			if((length - written) > 65535)
+				amount = 65535;
+			else
+				amount = length - written;
+
+			WinSendMsg(handle, MLM_SETIMPORTEXPORT, MPFROMP(mlebuf), MPFROMLONG(amount));
+			copied = (int)WinSendMsg(handle, MLM_EXPORT, MPFROMP(&startpoint), MPFROMLONG(&amount));
+
+			if(copied)
+			{
+				memcpy(&buffer[written], mlebuf, copied);
+
+				written += copied;
+			}
+			else
+				break;
+		}
+		DosFreeMem(mlebuf);
+	}
 }
 
 /*
@@ -4539,17 +4474,10 @@
  */
 void dw_mle_query(HWND handle, unsigned long *bytes, unsigned long *lines)
 {
-	char *tmpbuf = malloc(MLE_MAX+1);
-
-	GetWindowText(handle, tmpbuf, MLE_MAX);
-	tmpbuf[MLE_MAX] = 0;
-
 	if(bytes)
-		*bytes = strlen(tmpbuf);
+		*bytes = (unsigned long)WinSendMsg(handle, MLM_QUERYTEXTLENGTH, 0, 0);
 	if(lines)
-		*lines = (unsigned long)SendMessage(handle, EM_GETLINECOUNT, 0, 0);
-
-	free(tmpbuf);
+		*lines = (unsigned long)WinSendMsg(handle, MLM_QUERYLINECOUNT, 0, 0);
 }
 
 /*
@@ -4561,19 +4489,18 @@
  */
 void dw_mle_delete(HWND handle, int startpoint, int length)
 {
-	char *tmpbuf = malloc(MLE_MAX+1);
-	int len;
-
-	GetWindowText(handle, tmpbuf, MLE_MAX);
-	tmpbuf[MLE_MAX] = 0;
-
-	len = strlen(tmpbuf);
-
-	strcpy(&tmpbuf[startpoint], &tmpbuf[startpoint+length]);
-
-	SetWindowText(handle, tmpbuf);
-
-	free(tmpbuf);
+	char *buf = malloc(length+1);
+	int z, dellen = length;
+
+	dw_mle_export(handle, buf, startpoint, length);
+
+	for(z=0;z<length-1;z++)
+	{
+		if(strncmp(&buf[z], "\r\n", 2) == 0)
+			dellen--;
+	}
+	WinSendMsg(handle, MLM_DELETE, MPFROMLONG(startpoint), MPFROMLONG(dellen));
+	free(buf);
 }
 
 /*
@@ -4583,19 +4510,28 @@
  */
 void dw_mle_clear(HWND handle)
 {
-	SetWindowText(handle, "");
+	unsigned long bytes;
+
+	dw_mle_query(handle, &bytes, NULL);
+
+	WinSendMsg(handle, MLM_DELETE, MPFROMLONG(0), MPFROMLONG(bytes));
 }
 
 /*
  * Sets the visible line of an MLE box.
  * Parameters:
- *          handle: Handle to the MLE.
+ *          handle: Handle to the MLE to be positioned.
  *          line: Line to be visible.
  */
 void dw_mle_set_visible(HWND handle, int line)
 {
-	int point = (int)SendMessage(handle, EM_LINEINDEX, (WPARAM)line, 0);
-	dw_mle_set(handle, point);
+	int tmppnt;
+
+	if(line > 10)
+	{
+		tmppnt = (int)WinSendMsg(handle, MLM_CHARFROMLINE, MPFROMLONG(line - 10), 0);
+		WinSendMsg(handle, MLM_SETFIRSTCHAR, MPFROMLONG(tmppnt), 0);
+	}
 }
 
 /*
@@ -4606,7 +4542,7 @@
  */
 void dw_mle_set_editable(HWND handle, int state)
 {
-	SendMessage(handle, EM_SETREADONLY, (WPARAM)(state ? FALSE : TRUE), 0);
+	WinSendMsg(handle, MLM_SETREADONLY, MPFROMLONG(state ? FALSE : TRUE), 0);
 }
 
 /*
@@ -4617,13 +4553,7 @@
  */
 void dw_mle_set_word_wrap(HWND handle, int state)
 {
-	/* If ES_AUTOHSCROLL is not set and there is not
-	 * horizontal scrollbar it word wraps.
-	 */
-	if(state)
-		dw_window_set_style(handle, 0, ES_AUTOHSCROLL);
-	else
-		dw_window_set_style(handle, ES_AUTOHSCROLL, ES_AUTOHSCROLL);
+	WinSendMsg(handle, MLM_SETWRAP, MPFROMLONG(state), 0);
 }
 
 /*
@@ -4634,8 +4564,7 @@
  */
 void dw_mle_set(HWND handle, int point)
 {
-	SendMessage(handle, EM_SETSEL, (WPARAM)point, (LPARAM)point);
-	SendMessage(handle, EM_SCROLLCARET, 0, 0);
+	WinSendMsg(handle, MLM_SETSEL, MPFROMLONG(point), MPFROMLONG(point));
 }
 
 /*
@@ -4648,41 +4577,22 @@
  */
 int dw_mle_search(HWND handle, char *text, int point, unsigned long flags)
 {
-	char *tmpbuf = malloc(MLE_MAX+1);
-	int z, len, textlen, retval = 0;
-
-	GetWindowText(handle, tmpbuf, MLE_MAX);
-	tmpbuf[MLE_MAX] = 0;
-
-	len = strlen(tmpbuf);
-	textlen = strlen(text);
-
-	if(flags & DW_MLE_CASESENSITIVE)
-	{
-		for(z=point;z<(len-textlen) && !retval;z++)
-		{
-			if(strncmp(&tmpbuf[z], text, textlen) == 0)
-				retval = z + textlen;
-		}
-	}
-	else
-	{
-		for(z=point;z<(len-textlen) && !retval;z++)
-		{
-			if(strnicmp(&tmpbuf[z], text, textlen) == 0)
-				retval = z + textlen;
-		}
-	}
-
-	if(retval)
-	{
-		SendMessage(handle, EM_SETSEL, (WPARAM)retval - textlen, (LPARAM)retval);
-		SendMessage(handle, EM_SCROLLCARET, 0, 0);
-	}
-
-	free(tmpbuf);
-
-	return retval;
+	MLE_SEARCHDATA msd;
+
+	/* This code breaks with structure packing set to 1 (/Sp1 in VAC)
+	 * if this is needed we need to add a pragma here.
+	 */
+	msd.cb = sizeof(msd);
+	msd.pchFind = text;
+	msd.pchReplace = NULL;
+	msd.cchFind = strlen(text);
+	msd.cchReplace = 0;
+	msd.iptStart = point;
+	msd.iptStop = -1;
+
+	if(WinSendMsg(handle, MLM_SEARCH, MPFROMLONG(MLFSEARCH_SELECTMATCH | flags), (MPARAM)&msd))
+		return (int)WinSendMsg(handle, MLM_QUERYSEL,(MPARAM)MLFQS_MAXSEL, 0);
+	return 0;
 }
 
 /*
@@ -4692,6 +4602,7 @@
  */
 void dw_mle_freeze(HWND handle)
 {
+	WinSendMsg(handle, MLM_DISABLEREFRESH, 0, 0);
 }
 
 /*
@@ -4701,6 +4612,7 @@
  */
 void dw_mle_thaw(HWND handle)
 {
+	WinSendMsg(handle, MLM_ENABLEREFRESH, 0, 0);
 }
 
 /*
@@ -4710,7 +4622,13 @@
  */
 unsigned int dw_percent_query_range(HWND handle)
 {
-	return (unsigned int)SendMessage(handle, PBM_GETRANGE, (WPARAM)FALSE, 0);
+	unsigned long width;
+
+	dw_window_get_pos_size(handle, 0, 0, &width, 0);
+
+	if(width - 6 < 1)
+		return 1;
+	return width - 6;
 }
 
 /*
@@ -4721,7 +4639,16 @@
  */
 void dw_percent_set_pos(HWND handle, unsigned int position)
 {
-	SendMessage(handle, PBM_SETPOS, (WPARAM)position, 0);
+	PercentBar *pb = (PercentBar *)WinQueryWindowPtr(handle, 0);
+
+	if(pb)
+	{
+		RECTL rcl;
+
+		pb->pos = position;
+		WinQueryWindowRect(handle, &rcl);
+		WinInvalidateRect(handle, &rcl, FALSE);
+	}
 }
 
 /*
@@ -4732,34 +4659,19 @@
  */
 void dw_spinbutton_set_pos(HWND handle, long position)
 {
-	char tmpbuf[100];
-	ColorInfo *cinfo = (ColorInfo *)GetWindowLong(handle, GWL_USERDATA);
-
-	sprintf(tmpbuf, "%d", position);
-
-	if(cinfo && cinfo->buddy)
-		SetWindowText(cinfo->buddy, tmpbuf);
-
-	if(IS_WIN98PLUS)
-		SendMessage(handle, UDM_SETPOS32, 0, (LPARAM)position);
-	else
-		SendMessage(handle, UDM_SETPOS, 0, (LPARAM)MAKELONG((short)position, 0));
+	WinSendMsg(handle, SPBM_SETCURRENTVALUE, MPFROMLONG((long)position), 0L);
 }
 
 /*
  * Sets the spinbutton limits.
  * Parameters:
  *          handle: Handle to the spinbutton to be set.
- *          position: Current value of the spinbutton.
- *          position: Current value of the spinbutton.
+ *          upper: Upper limit.
+ *          lower: Lower limit.
  */
 void dw_spinbutton_set_limits(HWND handle, long upper, long lower)
 {
-	if(IS_WIN98PLUS)
-		SendMessage(handle, UDM_SETRANGE32, (WPARAM)lower,(LPARAM)upper);
-	else
-		SendMessage(handle, UDM_SETRANGE32, (WPARAM)((short)lower),
-					(LPARAM)((short)upper));
+	WinSendMsg(handle, SPBM_SETLIMITS, MPFROMLONG(upper), MPFROMLONG(lower));
 }
 
 /*
@@ -4770,8 +4682,9 @@
  */
 void dw_entryfield_set_limit(HWND handle, ULONG limit)
 {
-	SendMessage(handle, EM_SETLIMITTEXT, (WPARAM)limit, 0);
-}
+	WinSendMsg(handle, EM_SETTEXTLIMIT, (MPARAM)limit, (MPARAM)0);
+}
+
 
 /*
  * Returns the current value of the spinbutton.
@@ -4780,10 +4693,10 @@
  */
 long dw_spinbutton_query(HWND handle)
 {
-	if(IS_WIN98PLUS)
-		return (long)SendMessage(handle, UDM_GETPOS32, 0, 0);
-	else
-		return (long)SendMessage(handle, UDM_GETPOS, 0, 0);
+	long tmpval = 0L;
+
+	WinSendMsg(handle, SPBM_QUERYVALUE, (MPARAM)&tmpval,0L);
+    return tmpval;
 }
 
 /*
@@ -4793,9 +4706,7 @@
  */
 int dw_checkbox_query(HWND handle)
 {
-	if(SendMessage(handle, BM_GETCHECK, 0, 0) == BST_CHECKED)
-		return TRUE;
-	return FALSE;
+	return (int)WinSendMsg(handle,BM_QUERYCHECK,0,0);
 }
 
 /*
@@ -4806,11 +4717,7 @@
  */
 void dw_checkbox_set(HWND handle, int value)
 {
-	ColorInfo *cinfo = (ColorInfo *)GetWindowLong(handle, GWL_USERDATA);
-
-	if(cinfo && !cinfo->user)
-		SendMessage(handle, BM_CLICK, 0, 0);
-	SendMessage(handle, BM_SETCHECK, (WPARAM)value, 0);
+	WinSendMsg(handle,BM_SETCHECK,MPFROMSHORT(value),0);
 }
 
 /*
@@ -4824,27 +4731,47 @@
  */
 HWND dw_tree_insert(HWND handle, char *title, unsigned long icon, HWND parent, void *itemdata)
 {
-	TVITEM tvi;
-	TVINSERTSTRUCT tvins;
-	HTREEITEM hti;
-	void **ptrs= malloc(sizeof(void *) * 2);
-
-	ptrs[0] = title;
-	ptrs[1] = itemdata;
-
-	tvi.mask = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_PARAM;
-	tvi.pszText = title;
-	tvi.lParam = (LONG)ptrs;
-	tvi.cchTextMax = strlen(title);
-	tvi.iSelectedImage = tvi.iImage = _lookup_icon(handle, (HICON)icon, 1);
-
-	tvins.item = tvi;
-	tvins.hParent = (HTREEITEM)parent;
-	tvins.hInsertAfter = TVI_LAST;
-
-	hti = TreeView_InsertItem(handle, &tvins);
-
-	return (HWND)hti;
+	ULONG        cbExtra;
+	PCNRITEM     pci;
+	RECORDINSERT ri;
+
+	/* Calculate extra bytes needed for each record besides that needed for the
+	 * MINIRECORDCORE structure
+	 */
+
+	cbExtra = sizeof(CNRITEM) - sizeof(MINIRECORDCORE);
+
+	/* Allocate memory for the parent record */
+
+	pci = WinSendMsg(handle, CM_ALLOCRECORD, MPFROMLONG(cbExtra), MPFROMSHORT(1));
+
+	/* Fill in the parent record data */
+
+	pci->rc.cb          = sizeof(MINIRECORDCORE);
+	pci->rc.pszIcon     = strdup(title);
+	pci->rc.hptrIcon    = icon;
+
+	pci->hptrIcon       = icon;
+	pci->user           = itemdata;
+
+	memset(&ri, 0, sizeof(RECORDINSERT));
+
+	ri.cb                 = sizeof(RECORDINSERT);
+	ri.pRecordOrder       = (PRECORDCORE)CMA_END;
+	ri.pRecordParent      = (PRECORDCORE)NULL;
+	ri.zOrder             = (USHORT)CMA_TOP;
+	ri.cRecordsInsert     = 1;
+	ri.fInvalidateRecord  = TRUE;
+
+	/* We are about to insert the child records. Set the parent record to be
+	 * the one we just inserted.
+	 */
+	ri.pRecordParent = (PRECORDCORE)parent;
+
+	/* Insert the record */
+	WinSendMsg(handle, CM_INSERTRECORD, MPFROMP(pci), MPFROMP(&ri));
+
+	return (HWND)pci;
 }
 
 /*
@@ -4857,24 +4784,20 @@
  */
 void dw_tree_set(HWND handle, HWND item, char *title, unsigned long icon)
 {
-	TVITEM tvi;
-	void **ptrs;
-
-	tvi.mask = TVIF_HANDLE;
-	tvi.hItem = (HTREEITEM)item;
-
-	TreeView_GetItem(handle, &tvi);
-
-	ptrs = (void **)tvi.lParam;
-	ptrs[0] = title;
-
-	tvi.mask = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE;
-	tvi.pszText = title;
-	tvi.cchTextMax = strlen(title);
-	tvi.iSelectedImage = tvi.iImage = _lookup_icon(handle, (HICON)icon, 1);
-	tvi.hItem = (HTREEITEM)item;
-
-	TreeView_SetItem(handle, &tvi);
+	PCNRITEM pci = (PCNRITEM)item;
+
+	if(!pci)
+		return;
+
+	if(pci->rc.pszIcon)
+		free(pci->rc.pszIcon);
+
+	pci->rc.pszIcon     = strdup(title);
+	pci->rc.hptrIcon    = icon;
+
+	pci->hptrIcon       = icon;
+
+	WinSendMsg(handle, CM_INVALIDATERECORD, (MPARAM)&pci, MPFROM2SHORT(1, CMA_TEXTCHANGED));
 }
 
 /*
@@ -4886,16 +4809,12 @@
  */
 void dw_tree_set_data(HWND handle, HWND item, void *itemdata)
 {
-	TVITEM tvi;
-	void **ptrs;
-
-	tvi.mask = TVIF_HANDLE;
-	tvi.hItem = (HTREEITEM)item;
-
-	TreeView_GetItem(handle, &tvi);
-
-	ptrs = (void **)tvi.lParam;
-	ptrs[1] = itemdata;
+	PCNRITEM pci = (PCNRITEM)item;
+
+	if(!pci)
+		return;
+
+	pci->user = itemdata;
 }
 
 /*
@@ -4906,8 +4825,17 @@
  */
 void dw_tree_item_select(HWND handle, HWND item)
 {
-	TreeView_SelectItem(handle, (HTREEITEM)item);
-	SetFocus(handle);
+	PRECORDCORE pCore = WinSendMsg(handle, CM_QUERYRECORD, (MPARAM)0L, MPFROM2SHORT(CMA_FIRST, CMA_ITEMORDER));
+
+	while(pCore)
+	{
+		if(pCore->flRecordAttr & CRA_SELECTED)
+			WinSendMsg(handle, CM_SETRECORDEMPHASIS, (MPARAM)pCore, MPFROM2SHORT(FALSE, CRA_SELECTED | CRA_CURSORED));
+		pCore = WinSendMsg(handle, CM_QUERYRECORD, (MPARAM)pCore, MPFROM2SHORT(CMA_NEXT, CMA_ITEMORDER));
+	}
+	WinSendMsg(handle, CM_SETRECORDEMPHASIS, (MPARAM)item, MPFROM2SHORT(TRUE, CRA_SELECTED | CRA_CURSORED));
+	lastitem = 0;
+	lasthcnr = 0;
 }
 
 /*
@@ -4917,7 +4845,7 @@
  */
 void dw_tree_clear(HWND handle)
 {
-	TreeView_DeleteAllItems(handle);
+	WinSendMsg(handle, CM_REMOVERECORD, (MPARAM)0L, MPFROM2SHORT(0, CMA_INVALIDATE | CMA_FREE));
 }
 
 /*
@@ -4928,7 +4856,7 @@
  */
 void dw_tree_expand(HWND handle, HWND item)
 {
-	TreeView_Expand(handle, (HTREEITEM)item, TVE_EXPAND);
+	WinSendMsg(handle, CM_EXPANDTREE, MPFROMP(item), 0);
 }
 
 /*
@@ -4939,7 +4867,7 @@
  */
 void dw_tree_collapse(HWND handle, HWND item)
 {
-	TreeView_Expand(handle, (HTREEITEM)item, TVE_COLLAPSE);
+	WinSendMsg(handle, CM_COLLAPSETREE, MPFROMP(item), 0);
 }
 
 /*
@@ -4950,8 +4878,23 @@
  */
 void dw_tree_delete(HWND handle, HWND item)
 {
-	TreeView_DeleteItem(handle, (HTREEITEM)item);
-}
+	PCNRITEM     pci = (PCNRITEM)item;
+
+	if(!item)
+		return;
+
+	if(pci->rc.pszIcon)
+		free(pci->rc.pszIcon);
+
+	WinSendMsg(handle, CM_REMOVERECORD, (MPARAM)&pci, MPFROM2SHORT(1, CMA_INVALIDATE | CMA_FREE));
+}
+
+/* Some OS/2 specific container structs */
+typedef struct _containerinfo {
+	int count;
+	void *data;
+	HWND handle;
+} ContainerInfo;
 
 /*
  * Sets up the container columns.
@@ -4961,37 +4904,88 @@
  *          titles: An array of strings with column text titles.
  *          count: The number of columns (this should match the arrays).
  *          separator: The column number that contains the main separator.
- *                     (only used on OS/2 but must be >= 0 on all)
+ *                     (this item may only be used in OS/2)
  */
 int dw_container_setup(HWND handle, unsigned long *flags, char **titles, int count, int separator)
 {
-	ContainerInfo *cinfo = (ContainerInfo *)GetWindowLong(handle, GWL_USERDATA);
-	int z, l = 0;
-	unsigned long *tempflags = malloc(sizeof(unsigned long) * (count + 2));
-	LV_COLUMN lvc;
-
-	if(separator == -1)
-		l = 1;
-
-	memcpy(&tempflags[l], flags, sizeof(unsigned long) * count);
-	tempflags[count + l] = 0;
-	cinfo->flags = tempflags;
-
+	PFIELDINFO details, first, left = NULL;
+	FIELDINFOINSERT detin;
+	CNRINFO cnri;
+	int z;
+	ULONG size = sizeof(RECORDCORE);
+	ULONG *offStruct = malloc(count * sizeof(ULONG));
+	ULONG *tempflags = malloc((count+1) * sizeof(ULONG));
+	ULONG *oldflags = (ULONG *)WinQueryWindowPtr(handle, 0);
+
+	if(!offStruct || !tempflags)
+		return FALSE;
+
+	memcpy(tempflags, flags, count * sizeof(ULONG));
+	tempflags[count] = 0;
+
+	WinSetWindowPtr(handle, 0, tempflags);
+
+	if(oldflags)
+		free(oldflags);
+
+	while((first = (PFIELDINFO)WinSendMsg(handle, CM_QUERYDETAILFIELDINFO,  0, MPFROMSHORT(CMA_FIRST))) != NULL)
+	{
+		WinSendMsg(handle, CM_REMOVEDETAILFIELDINFO, (MPARAM)&first, MPFROM2SHORT(1, CMA_FREE));
+	}
+
+	/* Figure out the offsets to the items in the struct */
+	for(z=0;z<count;z++)
+	{
+		offStruct[z] = size;
+		if(flags[z] & DW_CFA_BITMAPORICON)
+			size += sizeof(HPOINTER);
+		else if(flags[z] & DW_CFA_STRING)
+			size += sizeof(char *);
+		else if(flags[z] & DW_CFA_ULONG)
+			size += sizeof(ULONG);
+		else if(flags[z] & DW_CFA_DATE)
+			size += sizeof(CDATE);
+		else if(flags[z] & DW_CFA_TIME)
+			size += sizeof(CTIME);
+	}
+
+	first = details = (PFIELDINFO)WinSendMsg(handle, CM_ALLOCDETAILFIELDINFO, MPFROMLONG(count), 0L);
+
+	if(!first)
+	{
+		free(offStruct);
+		return FALSE;
+	}
 
 	for(z=0;z<count;z++)
 	{
-		if(titles[z])
-		{
-			lvc.mask = LVCF_TEXT | LVCF_WIDTH | LVCF_SUBITEM /*| LVCF_FORMAT*/;
-			lvc.pszText = titles[z];
-			lvc.cchTextMax = strlen(titles[z]);
-			lvc.fmt = flags[z];
-			lvc.cx = 75;
-			lvc.iSubItem = count;
-			SendMessage(handle, LVM_INSERTCOLUMN, (WPARAM)z + l, (LPARAM)&lvc);
-		}
+		if(z==separator-1)
+			left=details;
+		details->cb = sizeof(FIELDINFO);
+		details->flData = flags[z];
+		details->flTitle = CFA_FITITLEREADONLY;
+		details->pTitleData = titles[z];
+		details->offStruct = offStruct[z];
+		details = details->pNextFieldInfo;
 	}
-	ListView_SetExtendedListViewStyle(handle, LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);
+
+	detin.cb = sizeof(FIELDINFOINSERT);
+	detin.fInvalidateFieldInfo = FALSE;
+	detin.pFieldInfoOrder = (PFIELDINFO) CMA_FIRST;
+	detin.cFieldInfoInsert = (ULONG)count;
+
+	WinSendMsg(handle, CM_INSERTDETAILFIELDINFO, MPFROMP(first), MPFROMP(&detin));
+
+	if(count > separator && separator > 0)
+	{
+		cnri.cb = sizeof(CNRINFO);
+		cnri.pFieldInfoLast = left;
+		cnri.xVertSplitbar  = 150;
+
+		WinSendMsg(handle, CM_SETCNRINFO, MPFROMP(&cnri),  MPFROMLONG(CMA_PFIELDINFOLAST | CMA_XVERTSPLITBAR));
+	}
+
+	free(offStruct);
 	return TRUE;
 }
 
@@ -5005,19 +4999,22 @@
  */
 int dw_filesystem_setup(HWND handle, unsigned long *flags, char **titles, int count)
 {
-	LV_COLUMN lvc;
-
-	lvc.mask = LVCF_TEXT | LVCF_WIDTH | LVCF_SUBITEM;
-	lvc.pszText = "Filename";
-	lvc.cchTextMax = 8;
-	lvc.fmt = 0;
-	if(!count)
-		lvc.cx = 300;
-	else
-		lvc.cx = 150;
-	lvc.iSubItem = count;
-	SendMessage(handle, LVM_INSERTCOLUMN, (WPARAM)0, (LPARAM)&lvc);
-	dw_container_setup(handle, flags, titles, count, -1);
+	char **newtitles = malloc(sizeof(char *) * (count + 2));
+	unsigned long *newflags = malloc(sizeof(unsigned long) * (count + 2));
+
+	newtitles[0] = "Icon";
+	newtitles[1] = "Filename";
+
+	newflags[0] = DW_CFA_BITMAPORICON | DW_CFA_CENTER | DW_CFA_HORZSEPARATOR | DW_CFA_SEPARATOR;
+	newflags[1] = DW_CFA_STRING | DW_CFA_LEFT | DW_CFA_HORZSEPARATOR;
+
+	memcpy(&newtitles[2], titles, sizeof(char *) * count);
+	memcpy(&newflags[2], flags, sizeof(unsigned long) * count);
+
+	dw_container_setup(handle, newflags, newtitles, count + 2, 2);
+
+	free(newtitles);
+	free(newflags);
 	return TRUE;
 }
 
@@ -5031,7 +5028,7 @@
  */
 unsigned long dw_icon_load(unsigned long module, unsigned long id)
 {
-	return (unsigned long)LoadIcon(DWInstance, MAKEINTRESOURCE(id));
+	return WinLoadPointer(HWND_DESKTOP,module,id);
 }
 
 /*
@@ -5041,7 +5038,7 @@
  */
 void dw_icon_free(unsigned long handle)
 {
-	DestroyIcon((HICON)handle);
+	WinDestroyPointer(handle);
 }
 
 /*
@@ -5052,111 +5049,63 @@
  */
 void *dw_container_alloc(HWND handle, int rowcount)
 {
-	LV_ITEM lvi;
-	int z;
-
-	lvi.mask = LVIF_DI_SETITEM | LVIF_TEXT | LVIF_IMAGE;
-	lvi.iSubItem = 0;
-	/* Insert at the end */
-	lvi.iItem = 1000000;
-	lvi.pszText = "";
-	lvi.cchTextMax = 1;
-	lvi.iImage = -1;
+	ULONG *flags = (ULONG *)WinQueryWindowPtr(handle, 0);
+	int z, size = 0, totalsize, count = 0;
+	PRECORDCORE temp;
+	ContainerInfo *ci;
+	void *blah = NULL;
+
+	if(!flags || rowcount < 1)
+		return NULL;
+
+	while(flags[count])
+		count++;
+
+	/* Figure out the offsets to the items in the struct */
+	for(z=0;z<count;z++)
+	{
+		if(flags[z] & DW_CFA_BITMAPORICON)
+			size += sizeof(HPOINTER);
+		else if(flags[z] & DW_CFA_STRING)
+			size += sizeof(char *);
+		else if(flags[z] & DW_CFA_ULONG)
+			size += sizeof(ULONG);
+		else if(flags[z] & DW_CFA_DATE)
+			size += sizeof(CDATE);
+		else if(flags[z] & DW_CFA_TIME)
+			size += sizeof(CTIME);
+	}
+
+	totalsize = size + sizeof(RECORDCORE);
+
+	z = 0;
+
+	while((blah = (void *)WinSendMsg(handle, CM_ALLOCRECORD, MPFROMLONG(size), MPFROMLONG(rowcount))) == NULL)
+	{
+		z++;
+		if(z > 5000000)
+			break;
+		DosSleep(1);
+	}
+
+	if(!blah)
+		return NULL;
+
+	temp = (PRECORDCORE)blah;
 
 	for(z=0;z<rowcount;z++)
-		ListView_InsertItem(handle, &lvi);
-	return (void *)handle;
-}
-
-/* Finds a icon in the table, otherwise it adds it to the table
- * and returns the index in the table.
- */
-int _lookup_icon(HWND handle, HICON hicon, int type)
-{
-	int z;
-	static HWND lasthwnd = NULL;
-
-	if(!hSmall || !hLarge)
 	{
-		hSmall = ImageList_Create(16, 16, ILC_COLOR16, ICON_INDEX_LIMIT, 0);
-		hLarge = ImageList_Create(32, 32, ILC_COLOR16, ICON_INDEX_LIMIT, 0);
+		temp->cb = totalsize;
+		temp = temp->preccNextRecord;
 	}
-	for(z=0;z<ICON_INDEX_LIMIT;z++)
-	{
-		if(!lookup[z])
-		{
-			lookup[z] = hicon;
-			ImageList_AddIcon(hSmall, hicon);
-			ImageList_AddIcon(hLarge, hicon);
-			if(type)
-			{
-				TreeView_SetImageList(handle, hSmall, TVSIL_NORMAL);
-			}
-			else
-			{
-				ListView_SetImageList(handle, hSmall, LVSIL_SMALL);
-				ListView_SetImageList(handle, hLarge, LVSIL_NORMAL);
-			}
-			lasthwnd = handle;
-			return z;
-		}
-
-		if(hicon == lookup[z])
-		{
-			if(lasthwnd != handle)
-			{
-				if(type)
-				{
-					TreeView_SetImageList(handle, hSmall, TVSIL_NORMAL);
-				}
-				else
-				{
-					ListView_SetImageList(handle, hSmall, LVSIL_SMALL);
-					ListView_SetImageList(handle, hLarge, LVSIL_NORMAL);
-				}
-                lasthwnd = handle;
-			}
-			return z;
-		}
-	}
-	return -1;
-}
-
-/*
- * Sets an item in specified row and column to the given data.
- * Parameters:
- *          handle: Handle to the container window (widget).
- *          pointer: Pointer to the allocated memory in dw_container_alloc().
- *          column: Zero based column of data being set.
- *          row: Zero based row of data being set.
- *          data: Pointer to the data to be added.
- */
-void dw_filesystem_set_file(HWND handle, void *pointer, int row, char *filename, unsigned long icon)
-{
-	LV_ITEM lvi;
-
-	lvi.iItem = row;
-	lvi.iSubItem = 0;
-	lvi.mask = LVIF_DI_SETITEM | LVIF_IMAGE | LVIF_TEXT;
-	lvi.pszText = filename;
-	lvi.cchTextMax = strlen(filename);
-	lvi.iImage = _lookup_icon(handle, (HICON)icon, 0);
-
-	ListView_SetItem(handle, &lvi);
-}
-
-/*
- * Sets an item in specified row and column to the given data.
- * Parameters:
- *          handle: Handle to the container window (widget).
- *          pointer: Pointer to the allocated memory in dw_container_alloc().
- *          column: Zero based column of data being set.
- *          row: Zero based row of data being set.
- *          data: Pointer to the data to be added.
- */
-void dw_filesystem_set_item(HWND handle, void *pointer, int column, int row, void *data)
-{
-	dw_container_set_item(handle, pointer, column + 1, row, data);
+
+	ci = malloc(sizeof(struct _containerinfo));
+
+	ci->count = rowcount;
+	ci->data = blah;
+	ci->handle = handle;
+
+	return (void *)ci;
 }
 
 /*
@@ -5170,74 +5119,85 @@
  */
 void dw_container_set_item(HWND handle, void *pointer, int column, int row, void *data)
 {
-	ContainerInfo *cinfo = (ContainerInfo *)GetWindowLong(handle, GWL_USERDATA);
-	ULONG *flags;
-	LV_ITEM lvi;
-	char textbuffer[100], *destptr = textbuffer;
-
-	if(!cinfo || !cinfo->flags || !data)
+	ULONG totalsize, size = 0, *flags = (ULONG *)WinQueryWindowPtr(handle, 0);
+	int z, currentcount;
+	ContainerInfo *ci = (ContainerInfo *)pointer;
+	PRECORDCORE temp;
+	CNRINFO cnr;
+    void *dest;
+
+	if(!ci)
+		return;
+
+	if(!flags)
 		return;
 
-	flags = cinfo->flags;
-
-	lvi.mask = LVIF_DI_SETITEM | LVIF_TEXT;
-	lvi.iItem = row;
-	lvi.iSubItem = column;
+	temp = (PRECORDCORE)ci->data;
+
+	WinSendMsg(handle, CM_QUERYCNRINFO, (MPARAM)&cnr, MPFROMSHORT(sizeof(CNRINFO)));
+	currentcount = cnr.cRecords;
+
+	/* Figure out the offsets to the items in the struct */
+	for(z=0;z<column;z++)
+	{
+		if(flags[z] & DW_CFA_BITMAPORICON)
+			size += sizeof(HPOINTER);
+		else if(flags[z] & DW_CFA_STRING)
+			size += sizeof(char *);
+		else if(flags[z] & DW_CFA_ULONG)
+			size += sizeof(ULONG);
+		else if(flags[z] & DW_CFA_DATE)
+			size += sizeof(CDATE);
+		else if(flags[z] & DW_CFA_TIME)
+			size += sizeof(CTIME);
+	}
+
+	totalsize = size + sizeof(RECORDCORE);
+
+	for(z=0;z<(row-currentcount);z++)
+		temp = temp->preccNextRecord;
+
+	dest = (void *)(((ULONG)temp)+((ULONG)totalsize));
 
 	if(flags[column] & DW_CFA_BITMAPORICON)
-	{
-		HICON hicon = *((HICON *)data);
-
-		lvi.mask = LVIF_DI_SETITEM | LVIF_IMAGE;
-		lvi.pszText = NULL;
-		lvi.cchTextMax = 0;
-
-		lvi.iImage = _lookup_icon(handle, hicon, 0);
-	}
+        memcpy(dest, data, sizeof(HPOINTER));
 	else if(flags[column] & DW_CFA_STRING)
-	{
-		char *tmp = *((char **)data);
-
-		if(!tmp)
-			tmp = "";
-
-		lvi.pszText = tmp;
-		lvi.cchTextMax = strlen(tmp);
-		destptr = tmp;
-	}
+        memcpy(dest, data, sizeof(char *));
 	else if(flags[column] & DW_CFA_ULONG)
-	{
-		ULONG tmp = *((ULONG *)data);
-
-		sprintf(textbuffer, "%lu", tmp);
-
-		lvi.pszText = textbuffer;
-		lvi.cchTextMax = strlen(textbuffer);
-	}
+        memcpy(dest, data, sizeof(ULONG));
 	else if(flags[column] & DW_CFA_DATE)
-	{
-		CDATE fdate = *((CDATE *)data);
-
-		if(fdate.month > 0)
-			sprintf(textbuffer, "%s %d, %d", monthlist[fdate.month-1], fdate.day, fdate.year);
-        else
-			strcpy(textbuffer, "");
-		lvi.pszText = textbuffer;
-		lvi.cchTextMax = strlen(textbuffer);
-	}
+        memcpy(dest, data, sizeof(CDATE));
 	else if(flags[column] & DW_CFA_TIME)
-	{
-		CTIME ftime = *((CTIME *)data);
-
-		if(ftime.hours > 12)
-			sprintf(textbuffer, "%d:%s%dpm", ftime.hours - 12, (ftime.minutes < 10) ? "0" : "", ftime.minutes);
-		else
-			sprintf(textbuffer, "%d:%s%dam", ftime.hours ? ftime.hours : 12, (ftime.minutes < 10) ? "0" : "", ftime.minutes);
-		lvi.pszText = textbuffer;
-		lvi.cchTextMax = strlen(textbuffer);
-	}
-
-	ListView_SetItem(handle, &lvi);
+        memcpy(dest, data, sizeof(CTIME));
+}
+
+/*
+ * Sets an item in specified row and column to the given data.
+ * Parameters:
+ *          handle: Handle to the container window (widget).
+ *          pointer: Pointer to the allocated memory in dw_container_alloc().
+ *          column: Zero based column of data being set.
+ *          row: Zero based row of data being set.
+ *          data: Pointer to the data to be added.
+ */
+void dw_filesystem_set_file(HWND handle, void *pointer, int row, char *filename, unsigned long icon)
+{
+	dw_container_set_item(handle, pointer, 0, row, (void *)&icon);
+	dw_container_set_item(handle, pointer, 1, row, (void *)&filename);
+}
+
+/*
+ * Sets an item in specified row and column to the given data.
+ * Parameters:
+ *          handle: Handle to the container window (widget).
+ *          pointer: Pointer to the allocated memory in dw_container_alloc().
+ *          column: Zero based column of data being set.
+ *          row: Zero based row of data being set.
+ *          data: Pointer to the data to be added.
+ */
+void dw_filesystem_set_item(HWND handle, void *pointer, int column, int row, void *data)
+{
+	dw_container_set_item(handle, pointer, column + 2, row, data);
 }
 
 /*
@@ -5249,7 +5209,6 @@
  */
 void dw_container_set_column_width(HWND handle, int column, int width)
 {
-	ListView_SetColumnWidth(handle, column, width);
 }
 
 /*
@@ -5261,17 +5220,25 @@
  */
 void dw_container_set_row_title(void *pointer, int row, char *title)
 {
-	LV_ITEM lvi;
-	HWND container = (HWND)pointer;
-
-	lvi.iItem = row;
-	lvi.iSubItem = 0;
-	lvi.mask = LVIF_PARAM;
-	lvi.lParam = (LPARAM)title;
-
-	if(!ListView_SetItem(container, &lvi) && lvi.lParam)
-		lvi.lParam = 0;
-
+	ContainerInfo *ci = (ContainerInfo *)pointer;
+	PRECORDCORE temp;
+	int z, currentcount;
+	CNRINFO cnr;
+
+	if(!ci)
+		return;
+
+	temp = (PRECORDCORE)ci->data;
+
+	WinSendMsg(ci->handle, CM_QUERYCNRINFO, (MPARAM)&cnr, MPFROMSHORT(sizeof(CNRINFO)));
+	currentcount = cnr.cRecords;
+
+	for(z=0;z<(row-currentcount);z++)
+		temp = temp->preccNextRecord;
+
+	temp->pszIcon = title;
+	temp->pszName = title;
+	temp->pszText = title;
 }
 
 /*
@@ -5283,7 +5250,29 @@
  */
 void dw_container_insert(HWND handle, void *pointer, int rowcount)
 {
-	/* This isn't a separate step in windows. */
+	RECORDINSERT recin;
+	ContainerInfo *ci = (ContainerInfo *)pointer;
+	int z;
+
+	if(!ci)
+		return;
+
+	recin.cb = sizeof(RECORDINSERT);
+	recin.pRecordOrder = (PRECORDCORE)CMA_END;
+	recin.pRecordParent = NULL;
+	recin.zOrder = CMA_TOP;
+	recin.fInvalidateRecord = TRUE;
+	recin.cRecordsInsert = rowcount;
+
+	z = 0;
+
+	while(WinSendMsg(handle, CM_INSERTRECORD, MPFROMP(ci->data), MPFROMP(&recin)) == 0)
+	{
+		z++;
+		if(z > 5000000)
+			break;
+		DosSleep(1);
+	}
 }
 
 /*
@@ -5294,7 +5283,15 @@
  */
 void dw_container_clear(HWND handle, int redraw)
 {
-	ListView_DeleteAllItems(handle);
+	int z = 0;
+
+	while((int)WinSendMsg(handle, CM_REMOVERECORD, (MPARAM)0L, MPFROM2SHORT(0, (redraw ? CMA_INVALIDATE : 0) | CMA_FREE)) == -1)
+	{
+		z++;
+		if(z > 5000000)
+			break;
+		DosSleep(1);
+	}
 }
 
 /*
@@ -5305,12 +5302,28 @@
  */
 void dw_container_delete(HWND handle, int rowcount)
 {
-	int z;
-
-	for(z=0;z<rowcount;z++)
+	RECORDCORE *last, **prc = malloc(sizeof(RECORDCORE *) * rowcount);
+	int current = 1, z;
+
+	prc[0] = last = (RECORDCORE *)WinSendMsg(handle, CM_QUERYRECORD, (MPARAM)0L, MPFROM2SHORT(CMA_FIRST, CMA_ITEMORDER));
+
+	while(last && current < rowcount)
 	{
-		ListView_DeleteItem(handle, 0);
+		prc[current] = last = (RECORDCORE *)WinSendMsg(handle, CM_QUERYRECORD, (MPARAM)last, MPFROM2SHORT(CMA_NEXT, CMA_ITEMORDER));
+		current++;
 	}
+
+	z = 0;
+
+	while((int)WinSendMsg(handle, CM_REMOVERECORD, (MPARAM)prc, MPFROM2SHORT(current, CMA_INVALIDATE | CMA_FREE)) == -1)
+	{
+		z++;
+		if(z > 5000000)
+			break;
+		DosSleep(1);
+	}
+	
+	free(prc);
 }
 
 /*
@@ -5326,10 +5339,10 @@
 	switch(direction)
 	{
 	case DW_SCROLL_TOP:
-		ListView_Scroll(handle, 0, -10000000);
+		WinSendMsg(handle, CM_SCROLLWINDOW, MPFROMSHORT(CMA_VERTICAL), MPFROMLONG(-10000000));
         break;
 	case DW_SCROLL_BOTTOM:
-		ListView_Scroll(handle, 0, 10000000);
+		WinSendMsg(handle, CM_SCROLLWINDOW, MPFROMSHORT(CMA_VERTICAL), MPFROMLONG(10000000));
 		break;
 	}
 }
@@ -5341,6 +5354,13 @@
  */
 void dw_container_set_view(HWND handle, unsigned long flags, int iconwidth, int iconheight)
 {
+	CNRINFO cnrinfo;
+
+	cnrinfo.flWindowAttr = flags;
+	cnrinfo.slBitmapOrIcon.cx = iconwidth;
+	cnrinfo.slBitmapOrIcon.cy = iconheight;
+
+	WinSendMsg(handle, CM_SETCNRINFO, &cnrinfo, MPFROMLONG(CMA_FLWINDOWATTR | CMA_SLBITMAPORICON));
 }
 
 /*
@@ -5353,20 +5373,22 @@
  */
 char *dw_container_query_start(HWND handle, unsigned long flags)
 {
-	LV_ITEM lvi;
-
-    if(flags)
-		_index = ListView_GetNextItem(handle, -1, LVNI_SELECTED);
-	else
-		_index = ListView_GetNextItem(handle, -1, LVNI_ALL);
-
-
-	lvi.iItem = _index;
-	lvi.mask = LVIF_PARAM;
-
-	ListView_GetItem(handle, &lvi);
-
-	return (char *)lvi.lParam;
+	pCore = WinSendMsg(handle, CM_QUERYRECORD, (MPARAM)0L, MPFROM2SHORT(CMA_FIRST, CMA_ITEMORDER));
+	if(pCore)
+	{
+		if(flags)
+		{
+			while(pCore)
+			{
+				if(pCore->flRecordAttr & CRA_SELECTED)
+					return pCore->pszIcon;
+				pCore = WinSendMsg(handle, CM_QUERYRECORD, (MPARAM)pCore, MPFROM2SHORT(CMA_NEXT, CMA_ITEMORDER));
+			}
+		}
+		else
+			return pCore->pszIcon;
+	}
+    return NULL;
 }
 
 /*
@@ -5379,22 +5401,23 @@
  */
 char *dw_container_query_next(HWND handle, unsigned long flags)
 {
-	LV_ITEM lvi;
-
-	if(flags)
-		_index = ListView_GetNextItem(handle, _index, LVNI_SELECTED);
-	else
-		_index = ListView_GetNextItem(handle, _index, LVNI_ALL);
-
-	if(_index == -1)
-		return NULL;
-
-	lvi.iItem = _index;
-	lvi.mask = LVIF_PARAM;
-
-	ListView_GetItem(handle, &lvi);
-
-	return (char *)lvi.lParam;
+	pCore = WinSendMsg(handle, CM_QUERYRECORD, (MPARAM)pCore, MPFROM2SHORT(CMA_NEXT, CMA_ITEMORDER));
+	if(pCore)
+	{
+		if(flags)
+		{
+			while(pCore)
+			{
+				if(pCore->flRecordAttr & CRA_SELECTED)
+					return pCore->pszIcon;
+
+				pCore = WinSendMsg(handle, CM_QUERYRECORD, (MPARAM)pCore, MPFROM2SHORT(CMA_NEXT, CMA_ITEMORDER));
+			}
+		}
+		else
+			return pCore->pszIcon;
+	}
+    return NULL;
 }
 
 /*
@@ -5406,24 +5429,19 @@
  */
 HWND dw_render_new(unsigned long id)
 {
-	Box *newbox = malloc(sizeof(Box));
-	HWND tmp = CreateWindow(ObjectClassName,
-							"",
-							WS_CHILD | WS_CLIPCHILDREN,
-							0,0,2000,1000,
-							DW_HWND_OBJECT,
-							NULL,
-							NULL,
-							NULL);
-	newbox->pad = 0;
-	newbox->type = 0;
-	newbox->count = 0;
-	newbox->grouphwnd = (HWND)NULL;
-	newbox->cinfo.pOldProc = SubclassWindow(tmp, _rendwndproc);
-	newbox->cinfo.fore = newbox->cinfo.back = -1;
-
-	SetWindowLong(tmp, GWL_USERDATA, (ULONG)newbox);
-	return tmp;
+	HWND hwndframe = WinCreateWindow(HWND_OBJECT,
+									 WC_FRAME,
+									 NULL,
+									 WS_VISIBLE |
+									 FS_NOBYTEALIGN,
+									 0,0,2000,1000,
+									 NULLHANDLE,
+									 HWND_TOP,
+									 id,
+									 NULL,
+									 NULL);
+	WinSubclassWindow(hwndframe, _RendProc);
+	return hwndframe;
 }
 
 /* Sets the current foreground drawing color.
@@ -5434,16 +5452,7 @@
  */
 void dw_color_foreground_set(unsigned long value)
 {
-	int threadid = dw_thread_id();
-
-	if(threadid < 0 || threadid >= THREAD_LIMIT)
-		threadid = 0;
-
-	DeleteObject(_hPen[threadid]);
-	DeleteObject(_hBrush[threadid]);
-	_foreground[threadid] = RGB(DW_RED_VALUE(value), DW_GREEN_VALUE(value), DW_BLUE_VALUE(value));
-	_hPen[threadid] = CreatePen(PS_SOLID, 1, _foreground[threadid]);
-	_hBrush[threadid] = CreateSolidBrush(_foreground[threadid]);
+	_foreground = DW_RED_VALUE(value) << 16 | DW_GREEN_VALUE(value) << 8 | DW_BLUE_VALUE(value);
 }
 
 /* Sets the current background drawing color.
@@ -5454,12 +5463,35 @@
  */
 void dw_color_background_set(unsigned long value)
 {
-	int threadid = dw_thread_id();
-
-	if(threadid < 0 || threadid >= THREAD_LIMIT)
-		threadid = 0;
-
-	_background[threadid] = RGB(DW_RED_VALUE(value), DW_GREEN_VALUE(value), DW_BLUE_VALUE(value));
+	_background = DW_RED_VALUE(value) << 16 | DW_GREEN_VALUE(value) << 8 | DW_BLUE_VALUE(value);
+}
+
+HPS _set_hps(HPS hps)
+{
+	LONG alTable[18];
+
+	GpiQueryLogColorTable(hps, 0L, 0L, 18L, alTable);
+
+	alTable[16] = _foreground;
+	alTable[17] = _background;
+
+	GpiCreateLogColorTable(hps,
+						   0L,
+						   LCOLF_CONSECRGB,
+						   0L,
+						   18,
+						   alTable);
+	GpiSetColor(hps, 16);
+	GpiSetBackColor(hps, 17);
+	return hps;
+}
+
+HPS _set_colors(HWND handle)
+{
+	HPS hps = WinGetPS(handle);
+
+	_set_hps(hps);
+	return hps;
 }
 
 /* Draw a point on a window (preferably a render window).
@@ -5471,22 +5503,29 @@
  */
 void dw_draw_point(HWND handle, HPIXMAP pixmap, int x, int y)
 {
-	HDC hdcPaint;
-	int threadid = dw_thread_id();
-
-	if(threadid < 0 || threadid >= THREAD_LIMIT)
-		threadid = 0;
+	HPS hps;
+	int height;
+	POINTL ptl;
 
 	if(handle)
-		hdcPaint = GetDC(handle);
+	{
+		hps = _set_colors(handle);
+        height = _get_height(handle);
+	}
 	else if(pixmap)
-		hdcPaint = pixmap->hdc;
+	{
+		hps = _set_hps(pixmap->hps);
+		height = pixmap->height;
+	}
 	else
 		return;
 
-	SetPixel(hdcPaint, x, y, _foreground[threadid]);
+	ptl.x = x;
+	ptl.y = height - y - 1;
+
+	GpiSetPel(hps, &ptl);
 	if(!pixmap)
-		ReleaseDC(handle, hdcPaint);
+		WinReleasePS(hps);
 }
 
 /* Draw a line on a window (preferably a render window).
@@ -5500,65 +5539,56 @@
  */
 void dw_draw_line(HWND handle, HPIXMAP pixmap, int x1, int y1, int x2, int y2)
 {
-	HDC hdcPaint;
-	HPEN oldPen;
-	int threadid = dw_thread_id();
-
-	if(threadid < 0 || threadid >= THREAD_LIMIT)
-		threadid = 0;
+	HPS hps;
+	int height;
+	POINTL ptl[2];
 
 	if(handle)
-		hdcPaint = GetDC(handle);
+	{
+		hps = _set_colors(handle);
+        height = _get_height(handle);
+	}
 	else if(pixmap)
-		hdcPaint = pixmap->hdc;
+	{
+		hps = _set_hps(pixmap->hps);
+		height = pixmap->height;
+	}
 	else
 		return;
 
-	oldPen = SelectObject(hdcPaint, _hPen[threadid]);
-	MoveToEx(hdcPaint, x1, y1, NULL);
-	LineTo(hdcPaint, x2, y2);
-	SelectObject(hdcPaint, oldPen);
-	/* For some reason Win98 (at least) fails
-	 * to draw the last pixel.  So I do it myself.
-	 */
-	SetPixel(hdcPaint, x2, y2, _foreground[threadid]);
+	ptl[0].x = x1;
+	ptl[0].y = height - y1 - 1;
+	ptl[1].x = x2;
+	ptl[1].y = height - y2 - 1;
+
+	GpiMove(hps, &ptl[0]);
+	GpiLine(hps, &ptl[1]);
+	
 	if(!pixmap)
-		ReleaseDC(handle, hdcPaint);
-}
-
-/* Draw a rectangle on a window (preferably a render window).
- * Parameters:
- *       handle: Handle to the window.
- *       pixmap: Handle to the pixmap. (choose only one of these)
- *       x: X coordinate.
- *       y: Y coordinate.
- *       width: Width of rectangle.
- *       height: Height of rectangle.
- */
-void dw_draw_rect(HWND handle, HPIXMAP pixmap, int fill, int x, int y, int width, int height)
-{
-	HDC hdcPaint;
-	HPEN oldPen;
-	HBRUSH oldBrush;
-	int threadid = dw_thread_id();
-
-	if(threadid < 0 || threadid >= THREAD_LIMIT)
-		threadid = 0;
-
-	if(handle)
-		hdcPaint = GetDC(handle);
-	else if(pixmap)
-		hdcPaint = pixmap->hdc;
-	else
-		return;
-
-	oldPen = SelectObject(hdcPaint, _hPen[threadid]);
-	oldBrush = SelectObject(hdcPaint, _hBrush[threadid]);
-	Rectangle(hdcPaint, x, y, x + width, y + height);
-	SelectObject(hdcPaint, oldPen);
-	SelectObject(hdcPaint, oldBrush);
-	if(!pixmap)
-		ReleaseDC(handle, hdcPaint);
+		WinReleasePS(hps);
+}
+
+
+void _CopyFontSettings(HPS hpsSrc, HPS hpsDst)
+{
+	FONTMETRICS fm;
+	FATTRS fat;
+	SIZEF sizf;
+
+	GpiQueryFontMetrics(hpsSrc, sizeof(FONTMETRICS), &fm);
+
+    memset(&fat, 0, sizeof(fat));
+
+	fat.usRecordLength  = sizeof(FATTRS);
+	fat.lMatch          = fm.lMatch;
+	strcpy(fat.szFacename, fm.szFacename);
+
+	GpiCreateLogFont(hpsDst, 0, 1L, &fat);
+	GpiSetCharSet(hpsDst, 1L);
+
+	sizf.cx = MAKEFIXED(fm.lEmInc,0);
+	sizf.cy = MAKEFIXED(fm.lMaxBaselineExt,0);
+	GpiSetCharBox(hpsDst, &sizf );
 }
 
 /* Draw text on a window (preferably a render window).
@@ -5571,44 +5601,46 @@
  */
 void dw_draw_text(HWND handle, HPIXMAP pixmap, int x, int y, char *text)
 {
-	HDC hdc;
-	int size = 9, z, mustdelete = 0;
-	HFONT hFont, oldFont;
-	int threadid = dw_thread_id();
-
-	if(threadid < 0 || threadid >= THREAD_LIMIT)
-		threadid = 0;
+	HPS hps;
+	int size = 9, z, height;
+	RECTL rcl;
+	char fontname[128];
 
 	if(handle)
-		hdc = GetDC(handle);
+	{
+		hps = _set_colors(handle);
+		height = _get_height(handle);
+		_GetPPFont(handle, fontname);
+	}
 	else if(pixmap)
-		hdc = pixmap->hdc;
+	{
+		HPS pixmaphps = WinGetPS(pixmap->handle);
+
+		hps = _set_hps(pixmap->hps);
+		height = pixmap->height;
+		_GetPPFont(pixmap->handle, fontname);
+		_CopyFontSettings(pixmaphps, hps);
+		WinReleasePS(pixmaphps);
+	}
 	else
 		return;
 
+	for(z=0;z<strlen(fontname);z++)
 	{
-		ColorInfo *cinfo;
-
-		if(handle)
-			cinfo = (ColorInfo *)GetWindowLong(handle, GWL_USERDATA);
-		else
-			cinfo = (ColorInfo *)GetWindowLong(pixmap->handle, GWL_USERDATA);
-
-		if(cinfo)
-		{
-			hFont = _aquire_font(cinfo->fontname);
-			mustdelete = 1;
-		}
+		if(fontname[z]=='.')
+			break;
 	}
-	oldFont = SelectObject(hdc, hFont);
-	SetTextColor(hdc, _foreground[threadid]);
-	SetBkMode(hdc, TRANSPARENT);
-	TextOut(hdc, x, y, text, strlen(text));
-	SelectObject(hdc, oldFont);
-	if(mustdelete)
-		DeleteObject(hFont);
+	size = atoi(fontname);
+
+	rcl.xLeft = x;
+	rcl.yTop = height - y;
+	rcl.yBottom = rcl.yTop - (size*2);
+	rcl.xRight = rcl.xLeft + (size * strlen(text));
+
+	WinDrawText(hps, -1, text, &rcl, DT_TEXTATTRS, DT_TEXTATTRS, DT_VCENTER | DT_LEFT | DT_TEXTATTRS);
+
 	if(!pixmap)
-		ReleaseDC(handle, hdc);
+		WinReleasePS(hps);
 }
 
 /* Query the width and height of a text string.
@@ -5621,47 +5653,75 @@
  */
 void dw_font_text_extents(HWND handle, HPIXMAP pixmap, char *text, int *width, int *height)
 {
-	HDC hdc;
-	int mustdelete = 0;
-	HFONT hFont, oldFont;
-	SIZE sz;
+	HPS hps;
+	POINTL aptl[TXTBOX_COUNT];
 
 	if(handle)
-		hdc = GetDC(handle);
+	{
+		hps = _set_colors(handle);
+	}
 	else if(pixmap)
-		hdc = pixmap->hdc;
+	{
+		HPS pixmaphps = WinGetPS(pixmap->handle);
+
+		hps = _set_hps(pixmap->hps);
+		_CopyFontSettings(pixmaphps, hps);
+		WinReleasePS(pixmaphps);
+	}
 	else
 		return;
 
-	{
-		ColorInfo *cinfo;
-
-		if(handle)
-			cinfo = (ColorInfo *)GetWindowLong(handle, GWL_USERDATA);
-		else
-			cinfo = (ColorInfo *)GetWindowLong(pixmap->handle, GWL_USERDATA);
-
-		if(cinfo)
-		{
-			hFont = _aquire_font(cinfo->fontname);
-			mustdelete = 1;
-		}
-	}
-	oldFont = SelectObject(hdc, hFont);
-
-	GetTextExtentPoint32(hdc, text, strlen(text), &sz);
+	GpiQueryTextBox(hps, strlen(text), text, TXTBOX_COUNT, aptl);
 
 	if(width)
-		*width = sz.cx;
+		*width = aptl[TXTBOX_TOPRIGHT].x - aptl[TXTBOX_TOPLEFT].x;
 
 	if(height)
-		*height = sz.cy;
-
-	SelectObject(hdc, oldFont);
-	if(mustdelete)
-		DeleteObject(hFont);
+		*height = aptl[TXTBOX_TOPLEFT].y - aptl[TXTBOX_BOTTOMLEFT].y;
+
 	if(!pixmap)
-		ReleaseDC(handle, hdc);
+		WinReleasePS(hps);
+}
+
+/* Draw a rectangle on a window (preferably a render window).
+ * Parameters:
+ *       handle: Handle to the window.
+ *       pixmap: Handle to the pixmap. (choose only one of these)
+ *       fill: Fill box TRUE or FALSE.
+ *       x: X coordinate.
+ *       y: Y coordinate.
+ *       width: Width of rectangle.
+ *       height: Height of rectangle.
+ */
+void dw_draw_rect(HWND handle, HPIXMAP pixmap, int fill, int x, int y, int width, int height)
+{
+	HPS hps;
+	int thisheight;
+	POINTL ptl[2];
+
+	if(handle)
+	{
+		hps = _set_colors(handle);
+        thisheight = _get_height(handle);
+	}
+	else if(pixmap)
+	{
+		hps = _set_hps(pixmap->hps);
+		thisheight = pixmap->height;
+	}
+	else
+		return;
+
+	ptl[0].x = x;
+	ptl[0].y = thisheight - y - 1;
+	ptl[1].x = x + width - 1;
+	ptl[1].y = thisheight - y - height;
+
+	GpiMove(hps, &ptl[0]);
+	GpiBox(hps, fill ? DRO_OUTLINEFILL : DRO_OUTLINE, &ptl[1], 0, 0);
+	
+	if(!pixmap)
+		WinReleasePS(hps);
 }
 
 /* Call this after drawing to the screen to make sure
@@ -5683,24 +5743,50 @@
  */
 HPIXMAP dw_pixmap_new(HWND handle, unsigned long width, unsigned long height, int depth)
 {
+	BITMAPINFOHEADER bmih;
+	SIZEL sizl = { 0, 0 };
 	HPIXMAP pixmap;
-	BITMAP bm;
 	HDC hdc;
+	HPS hps;
+	ULONG ulFlags;
+    LONG cPlanes, cBitCount;
 
 	if (!(pixmap = calloc(1,sizeof(struct _hpixmap))))
 		return NULL;
 
-	hdc = GetDC(handle);
-
-	pixmap->width = width; pixmap->height = height;
+	hps = WinGetPS(handle);
+
+	hdc     = GpiQueryDevice(hps);
+	ulFlags = GpiQueryPS(hps, &sizl);
 
 	pixmap->handle = handle;
-	pixmap->hbm = CreateCompatibleBitmap(hdc, width, height);
-	pixmap->hdc = CreateCompatibleDC(hdc);
-
-	SelectObject(pixmap->hdc, pixmap->hbm);
-
-	ReleaseDC(handle, hdc);
+	pixmap->hdc = DevOpenDC(dwhab, OD_MEMORY, "*", 0L, NULL, hdc);
+	pixmap->hps = GpiCreatePS (dwhab, pixmap->hdc, &sizl, ulFlags | GPIA_ASSOC);
+
+	DevQueryCaps(hdc, CAPS_COLOR_PLANES  , 1L, &cPlanes);
+	if (!depth)
+	{
+		DevQueryCaps(hdc, CAPS_COLOR_BITCOUNT, 1L, &cBitCount);
+		depth = cBitCount;
+	}
+
+	memset(&bmih, 0, sizeof(BITMAPINFOHEADER));
+	bmih.cbFix     = sizeof(BITMAPINFOHEADER);
+	bmih.cx        = (SHORT)width;
+	bmih.cy        = (SHORT)height;
+	bmih.cPlanes   = (SHORT)cPlanes;
+	bmih.cBitCount = (SHORT)depth;
+
+	pixmap->width = width; pixmap->height = height;
+
+	pixmap->hbm = GpiCreateBitmap(pixmap->hps, (PBITMAPINFOHEADER2)&bmih, 0L, NULL, NULL);
+
+	GpiSetBitmap(pixmap->hps, pixmap->hbm);
+
+	if (depth>8)
+		GpiCreateLogColorTable(pixmap->hps, LCOL_PURECOLOR, LCOLF_RGB, 0, 0, NULL );
+
+	WinReleasePS(hps);
 
 	return pixmap;
 }
@@ -5715,26 +5801,33 @@
  */
 HPIXMAP dw_pixmap_grab(HWND handle, ULONG id)
 {
+	BITMAPINFOHEADER bmih;
+	SIZEL sizl = { 0, 0 };
 	HPIXMAP pixmap;
-	BITMAP bm;
 	HDC hdc;
+	HPS hps;
+	ULONG ulFlags;
 
 	if (!(pixmap = calloc(1,sizeof(struct _hpixmap))))
 		return NULL;
 
-	hdc = GetDC(handle);
-
-
-	pixmap->hbm = LoadBitmap(DWInstance, MAKEINTRESOURCE(id));
-	pixmap->hdc = CreateCompatibleDC(hdc);
-
-	GetObject(pixmap->hbm, sizeof(BITMAP), (void *)&bm);
-
-	pixmap->width = bm.bmWidth; pixmap->height = bm.bmHeight;
-
-	SelectObject(pixmap->hdc, pixmap->hbm);
-
-	ReleaseDC(handle, hdc);
+	hps = WinGetPS(handle);
+
+	hdc     = GpiQueryDevice(hps);
+	ulFlags = GpiQueryPS(hps, &sizl);
+
+	pixmap->hdc = DevOpenDC(dwhab, OD_MEMORY, "*", 0L, NULL, hdc);
+	pixmap->hps = GpiCreatePS (dwhab, pixmap->hdc, &sizl, ulFlags | GPIA_ASSOC);
+
+	pixmap->hbm = GpiLoadBitmap(pixmap->hps, NULLHANDLE, id, 0, 0);
+
+	GpiQueryBitmapParameters(pixmap->hbm, &bmih);
+
+	GpiSetBitmap(pixmap->hps, pixmap->hbm);
+
+	pixmap->width = bmih.cx; pixmap->height = bmih.cy;
+
+	WinReleasePS(hps);
 
 	return pixmap;
 }
@@ -5747,12 +5840,12 @@
  */
 void dw_pixmap_destroy(HPIXMAP pixmap)
 {
-	if(pixmap)
-	{
-		DeleteDC(pixmap->hdc);
-		DeleteObject(pixmap->hbm);
-		free(pixmap);
-	}
+	GpiSetBitmap(pixmap->hps, NULLHANDLE);
+	GpiDeleteBitmap(pixmap->hbm);
+	GpiAssociate(pixmap->hps, NULLHANDLE);
+	GpiDestroyPS(pixmap->hps);
+	DevCloseDC(pixmap->hdc);
+	free(pixmap);
 }
 
 /*
@@ -5771,29 +5864,56 @@
  */
 void dw_pixmap_bitblt(HWND dest, HPIXMAP destp, int xdest, int ydest, int width, int height, HWND src, HPIXMAP srcp, int xsrc, int ysrc)
 {
-	HDC hdcdest;
-	HDC hdcsrc;
+	HPS hpsdest;
+	HPS hpssrc;
+	POINTL ptl[4];
+    int destheight, srcheight;
 
 	if(dest)
-		hdcdest = GetDC(dest);
+	{
+		hpsdest = WinGetPS(dest);
+		destheight = _get_height(dest);
+	}
 	else if(destp)
-		hdcdest = destp->hdc;
+	{
+		hpsdest = destp->hps;
+		destheight = destp->height;
+	}
 	else
 		return;
 
 	if(src)
-		hdcsrc = GetDC(src);
+	{
+		hpssrc = WinGetPS(src);
+		srcheight = _get_height(src);
+	}
 	else if(srcp)
-		hdcsrc = srcp->hdc;
+	{
+		hpssrc = srcp->hps;
+		srcheight = srcp->height;
+	}
 	else
+	{
+		if(!destp)
+			WinReleasePS(hpsdest);
 		return;
-
-	BitBlt(hdcdest, xdest, ydest, width, height, hdcsrc, xsrc, ysrc, SRCCOPY);
+	}
+
+	ptl[0].x = xdest;
+	ptl[0].y = (destheight - ydest) - height;
+	ptl[1].x = ptl[0].x + width;
+	ptl[1].y = destheight - ydest;
+	ptl[2].x = xsrc;
+	ptl[2].y = srcheight - (ysrc + height);
+	ptl[3].x = ptl[2].x + width;
+	ptl[3].y = ptl[2].y + height;
+
+	GpiBitBlt(hpsdest, hpssrc, 4, ptl, ROP_SRCCOPY, BBO_IGNORE);
 
 	if(!destp)
-		ReleaseDC(dest, hdcdest);
+		WinReleasePS(hpsdest);
 	if(!srcp)
-		ReleaseDC(src, hdcsrc);
+		WinReleasePS(hpssrc);
 }
 
 /*
@@ -5804,7 +5924,7 @@
  */
 void dw_beep(int freq, int dur)
 {
-	Beep(freq, dur);
+	DosBeep(freq, dur);
 }
 
 /*
@@ -5812,7 +5932,10 @@
  */
 HMTX dw_mutex_new(void)
 {
-	return (HMTX)CreateMutex(NULL, FALSE, NULL);
+	HMTX mutex;
+
+	DosCreateMutexSem(NULL, &mutex, 0, FALSE);
+	return mutex;
 }
 
 /*
@@ -5822,7 +5945,7 @@
  */
 void dw_mutex_close(HMTX mutex)
 {
-	CloseHandle((HANDLE)mutex);
+	DosCloseMutexSem(mutex);
 }
 
 /*
@@ -5832,7 +5955,7 @@
  */
 void dw_mutex_lock(HMTX mutex)
 {
-	WaitForSingleObject((HANDLE)mutex, INFINITE);
+	DosRequestMutexSem(mutex, SEM_INDEFINITE_WAIT);
 }
 
 /*
@@ -5842,7 +5965,7 @@
  */
 void dw_mutex_unlock(HMTX mutex)
 {
-	ReleaseMutex((HANDLE)mutex);
+	DosReleaseMutexSem(mutex);
 }
 
 /*
@@ -5850,7 +5973,12 @@
  */
 HEV dw_event_new(void)
 {
-    return CreateEvent(NULL, TRUE, FALSE, NULL);
+	HEV blah;
+
+	if(DosCreateEventSem (NULL, &blah, 0L, FALSE))
+		return 0;
+
+	return blah;
 }
 
 /*
@@ -5860,7 +5988,11 @@
  */
 int dw_event_reset(HEV eve)
 {
-	return ResetEvent(eve);
+	ULONG count;
+
+	if(DosResetEventSem(eve, &count))
+		return FALSE;
+	return TRUE;
 }
 
 /*
@@ -5871,8 +6003,11 @@
  */
 int dw_event_post(HEV eve)
 {
-	return SetEvent(eve);
-}
+	if(DosPostEventSem(eve))
+		return FALSE;
+	return TRUE;
+}
+
 
 /*
  * Waits on a semaphore created by dw_event_new(), until the
@@ -5882,12 +6017,10 @@
  */
 int dw_event_wait(HEV eve, unsigned long timeout)
 {
-	int rc;
-
-	rc = WaitForSingleObject(eve, timeout);
-	if(rc == WAIT_OBJECT_0)
+	int rc = DosWaitEventSem(eve, timeout);
+	if(!rc)
 		return 1;
-	if(rc == WAIT_ABANDONED)
+	if(rc == ERROR_TIMEOUT)
 		return -1;
 	return 0;
 }
@@ -5899,9 +6032,28 @@
  */
 int dw_event_close(HEV *eve)
 {
-	if(eve)
-		return CloseHandle(*eve);
-	return FALSE;
+	if(!eve || ~DosCloseEventSem(*eve))
+		return FALSE;
+	return TRUE;
+}
+
+/*
+ * Encapsulate the message queues on OS/2.
+ */
+void _dwthreadstart(void *data)
+{
+	HAB thishab = WinInitialize(0);
+	HMQ thishmq = WinCreateMsgQueue(dwhab, 0);
+	void (*threadfunc)(void *) = NULL;
+	void **tmp = (void **)data;
+
+	threadfunc = (void (*)(void *))tmp[0];
+	threadfunc(tmp[1]);
+
+	free(tmp);
+
+	WinDestroyMsgQueue(thishmq);
+	WinTerminate(thishab);
 }
 
 /*
@@ -5913,11 +6065,12 @@
  */
 DWTID dw_thread_new(void *func, void *data, int stack)
 {
-#if defined(__CYGWIN__)
-	return 0;
-#else
-	return (DWTID)_beginthread((void(*)(void *))func, stack, data);
-#endif
+	void **tmp = malloc(sizeof(void *) * 2);
+
+	tmp[0] = func;
+	tmp[1] = data;
+
+	return (DWTID)_beginthread((void (*)(void *))_dwthreadstart, NULL, stack, (void *)tmp);
 }
 
 /*
@@ -5925,9 +6078,7 @@
  */
 void dw_thread_end(void)
 {
-#if !defined(__CYGWIN__)
 	_endthread();
-#endif
 }
 
 /*
@@ -5935,11 +6086,7 @@
  */
 DWTID dw_thread_id(void)
 {
-#if defined(__CYGWIN__)
-	return 0;
-#else
-	return (DWTID)GetCurrentThreadId();
-#endif
+	return (DWTID)_threadid;
 }
 
 /*
@@ -5949,6 +6096,13 @@
  */
 void dw_exit(int exitcode)
 {
+	/* In case we are in a signal handler, don't
+	 * try to free memory that could possibly be
+	 * free()'d by the runtime already.
+	 */
+#ifndef NO_SIGNALS
+	Root = NULL;
+#endif
 	exit(exitcode);
 }
 
@@ -5959,18 +6113,27 @@
  */
 void dw_box_pack_splitbar_start(HWND box)
 {
-	Box *thisbox = (Box *)GetWindowLong(box, GWL_USERDATA);
-
+	Box *thisbox;
+
+	if(WinWindowFromID(box, FID_CLIENT))
+	{
+		box = WinWindowFromID(box, FID_CLIENT);
+		thisbox = WinQueryWindowPtr(box, QWP_USER);
+	}
+	else
+		thisbox = WinQueryWindowPtr(box, QWP_USER);
 	if(thisbox)
 	{
-		HWND tmp = CreateWindow(SplitbarClassName,
-								"",
-								WS_CHILD | WS_CLIPCHILDREN,
-								0,0,2000,1000,
-								DW_HWND_OBJECT,
-								NULL,
-								NULL,
-								NULL);
+		HWND tmp = WinCreateWindow(HWND_OBJECT,
+								   SplitbarClassName,
+								   NULL,
+								   WS_VISIBLE,
+								   0,0,2000,1000,
+								   NULLHANDLE,
+								   HWND_TOP,
+								   0L,
+								   NULL,
+								   NULL);
 		if(thisbox->type == BOXVERT)
 			dw_box_pack_start(box, tmp, 1, SPLITBAR_WIDTH, TRUE, FALSE, 0);
 		else
@@ -5986,18 +6149,27 @@
  */
 void dw_box_pack_splitbar_end(HWND box)
 {
-	Box *thisbox = (Box *)GetWindowLong(box, GWL_USERDATA);
-
+	Box *thisbox;
+
+	if(WinWindowFromID(box, FID_CLIENT))
+	{
+		box = WinWindowFromID(box, FID_CLIENT);
+		thisbox = WinQueryWindowPtr(box, QWP_USER);
+	}
+	else
+		thisbox = WinQueryWindowPtr(box, QWP_USER);
 	if(thisbox)
 	{
-		HWND tmp = CreateWindow(SplitbarClassName,
-								"",
-								WS_CHILD | WS_CLIPCHILDREN,
-								0,0,2000,1000,
-								DW_HWND_OBJECT,
-								NULL,
-								NULL,
-								NULL);
+		HWND tmp = WinCreateWindow(HWND_OBJECT,
+								   SplitbarClassName,
+								   NULL,
+								   WS_VISIBLE,
+								   0,0,2000,1000,
+								   NULLHANDLE,
+								   HWND_TOP,
+								   0L,
+								   NULL,
+								   NULL);
 		if(thisbox->type == BOXVERT)
 			dw_box_pack_end(box, tmp, 1, SPLITBAR_WIDTH, TRUE, FALSE, 0);
 		else
@@ -6007,7 +6179,7 @@
 }
 
 /*
- * Pack windows (widgets) into a box from the end (or bottom).
+ * Pack windows (widgets) into a box from the start (or top).
  * Parameters:
  *       box: Window handle of the box to be packed into.
  *       item: Window handle of the item to be back.
@@ -6017,11 +6189,40 @@
  *       vsize: TRUE if the window (widget) should expand vertically to fill space given.
  *       pad: Number of pixels of padding around the item.
  */
-void dw_box_pack_end(HWND box, HWND item, int width, int height, int hsize, int vsize, int pad)
+void dw_box_pack_start(HWND box, HWND item, int width, int height, int hsize, int vsize, int pad)
 {
 	Box *thisbox;
 
-	thisbox = (Box *)GetWindowLong(box, GWL_USERDATA);
+	if(WinWindowFromID(box, FID_CLIENT))
+	{
+		box = WinWindowFromID(box, FID_CLIENT);
+		thisbox = WinQueryWindowPtr(box, QWP_USER);
+	}
+	else
+		thisbox = WinQueryWindowPtr(box, QWP_USER);
+	if(thisbox)
+	{
+		if(thisbox->type == BOXHORZ)
+			dw_box_pack_end_stub(box, item, width, height, hsize, vsize, pad);
+		else
+			dw_box_pack_start_stub(box, item, width, height, hsize, vsize, pad);
+	}
+}
+
+void dw_box_pack_start_stub(HWND box, HWND item, int width, int height, int hsize, int vsize, int pad)
+{
+	HWND boxowner = NULLHANDLE;
+	Box *thisbox;
+
+	if(WinWindowFromID(box, FID_CLIENT))
+	{
+		box = WinWindowFromID(box, FID_CLIENT);
+		thisbox = WinQueryWindowPtr(box, QWP_USER);
+		hsize = TRUE;
+		vsize = TRUE;
+	}
+	else
+		thisbox = WinQueryWindowPtr(box, QWP_USER);
 	if(thisbox)
 	{
 		int z;
@@ -6035,9 +6236,9 @@
 			tmpitem[z+1] = thisitem[z];
 		}
 
-		GetClassName(item, tmpbuf, 99);
-
-		if(strnicmp(tmpbuf, FRAMECLASSNAME, 2)==0)
+		WinQueryClassName(item, 99, tmpbuf);
+
+		if(strncmp(tmpbuf, "#1", 3)==0)
 			tmpitem[0].type = TYPEBOX;
 		else
 			tmpitem[0].type = TYPEITEM;
@@ -6063,20 +6264,100 @@
 
 		thisbox->count++;
 
-		SetParent(item, box);
-		ShowWindow(item, SW_SHOW);
-		if(strncmp(tmpbuf, UPDOWN_CLASS, strlen(UPDOWN_CLASS))==0)
+		WinQueryClassName(item, 99, tmpbuf);
+		/* Don't set the ownership if it's an entryfield or spinbutton */
+		if(strncmp(tmpbuf, "#6", 3)!=0 && strncmp(tmpbuf, "#32", 3)!=0)
+		{
+			if((boxowner = WinQueryWindow(box, QW_OWNER)) != 0)
+				WinSetOwner(item, boxowner);
+			else
+				WinSetOwner(item, box);
+		}
+		WinSetParent(item, box, FALSE);
+	}
+}
+
+/* The following two functions graciously contributed by Peter Nielsen. */
+static ULONG _ParseBuildLevel (char* pchBuffer, ULONG ulSize) {
+	char* pchStart = pchBuffer;
+	char* pchEnd = pchStart + ulSize - 2;
+
+	while (pchEnd >= pchStart)
+	{
+		if ((pchEnd[0] == '#') && (pchEnd[1] == '@'))
 		{
-			ColorInfo *cinfo = (ColorInfo *)GetWindowLong(item, GWL_USERDATA);
-
-			if(cinfo)
+			*pchEnd-- = '\0';
+			while (pchEnd >= pchStart)
 			{
-				SetParent(cinfo->buddy, box);
-				ShowWindow(cinfo->buddy, SW_SHOW);
-				SendMessage(item, UDM_SETBUDDY, (WPARAM)cinfo->buddy, 0);
+				if ((pchEnd[0] == '@') && (pchEnd[1] == '#'))
+				{
+					ULONG ulMajor = 0;
+					ULONG ulMinor = 0;
+
+					char* pch = pchEnd + 2;
+					while (!isdigit (*pch) && *pch)
+						pch++;
+
+					while (isdigit (*pch))
+						ulMajor = ulMajor * 10 + *pch++ - '0';
+
+					if (*pch == '.')
+					{
+						while (isdigit (*++pch))
+							ulMinor = ulMinor * 10 + *pch - '0';
+					}
+					return ((ulMajor << 16) | ulMinor);
+				}
+				pchEnd--;
 			}
 		}
+		pchEnd--;
 	}
+	return (0);
+}
+
+ULONG _GetSystemBuildLevel(void) {
+	/* The build level info is normally available in the end of the OS2KRNL file. However, this is not the case in some beta versions of OS/2.
+	 * We first try to find the info in the 256 last bytes of the file. If that fails, we load the entire file and search it completely.
+	 */
+	ULONG ulBootDrive = 0;
+	ULONG ulBuild = 0;
+	if (DosQuerySysInfo (QSV_BOOT_DRIVE, QSV_BOOT_DRIVE, &ulBootDrive, sizeof (ulBootDrive)) == NO_ERROR)
+	{
+		char achFileName[11] = { (char)('A'+ulBootDrive-1),':','\\','O','S','2','K','R','N','L','\0' };
+		HFILE hfile;
+		ULONG ulResult;
+		if (DosOpen (achFileName, &hfile, &ulResult, 0, 0, OPEN_ACTION_FAIL_IF_NEW | OPEN_ACTION_OPEN_IF_EXISTS, OPEN_FLAGS_FAIL_ON_ERROR | OPEN_FLAGS_NO_CACHE | OPEN_FLAGS_SEQUENTIAL | OPEN_SHARE_DENYNONE | OPEN_ACCESS_READONLY, NULL) == NO_ERROR)
+		{
+			ULONG ulFileSize = 0;
+			if (DosSetFilePtr (hfile, 0, FILE_END, &ulFileSize) == NO_ERROR)
+			{
+				const ULONG ulFirstTry = min (256, ulFileSize);
+				if (DosSetFilePtr (hfile, -(LONG)ulFirstTry, FILE_END, &ulResult) == NO_ERROR)
+				{
+					char *pchBuffer = malloc(ulFirstTry);
+					if (DosRead (hfile, pchBuffer, ulFirstTry, &ulResult) == NO_ERROR)
+					{
+						ulBuild = _ParseBuildLevel (pchBuffer, ulFirstTry);
+						if (ulBuild == 0)
+						{
+							if (DosSetFilePtr (hfile, 0, FILE_BEGIN, &ulResult) == NO_ERROR)
+							{
+								free(pchBuffer);
+								pchBuffer = malloc(ulFileSize);
+
+								if (DosRead (hfile, pchBuffer, ulFileSize, &ulResult) == NO_ERROR)
+									ulBuild = _ParseBuildLevel (pchBuffer, ulFileSize);
+							}
+						}
+					}
+					free(pchBuffer);
+				}
+			}
+			DosClose (hfile);
+		}
+	}
+	return (ulBuild);
 }
 
 /*
@@ -6087,7 +6368,12 @@
  */
 void dw_window_default(HWND window, HWND defaultitem)
 {
-	Box *thisbox = (Box *)GetWindowLong(window, GWL_USERDATA);
+	Box *thisbox = NULL;
+	HWND box;
+
+	box = WinWindowFromID(window, FID_CLIENT);
+	if(box)
+		thisbox = WinQueryWindowPtr(box, QWP_USER);
 
 	if(thisbox)
 		thisbox->defaultitem = defaultitem;
@@ -6101,10 +6387,16 @@
  */
 void dw_window_click_default(HWND window, HWND next)
 {
-	ColorInfo *cinfo = (ColorInfo *)GetWindowLong(window, GWL_USERDATA);
-
-	if(cinfo)
-		cinfo->clickdefault = next;
+	WindowData *blah = (WindowData *)WinQueryWindowPtr(window, QWP_USER);
+	char tmpbuf[100];
+
+	WinQueryClassName(window, 99, tmpbuf);
+
+	/* These are the window classes which can
+	 * obtain input focus.
+	 */
+	if(strncmp(tmpbuf, "#6", 3) == 0 && blah)
+		blah->clickdefault = next;
 }
 
 /*
@@ -6114,35 +6406,32 @@
  */
 void dw_environment_query(DWEnv *env)
 {
+	ULONG Build;
+
 	if(!env)
 		return;
 
-	/* Get the Windows version. */
-
-	env->MajorVersion =  (DWORD)(LOBYTE(LOWORD(dwVersion)));
-	env->MinorVersion =  (DWORD)(HIBYTE(LOWORD(dwVersion)));
-
-	/* Get the build number for Windows NT/Windows 2000. */
-
-	env->MinorBuild =  0;
-
-	if (dwVersion < 0x80000000)
+	/* The default is OS/2 2.0 */
+	strcpy(env->osName,"OS/2");
+	env->MajorVersion = 2;
+	env->MinorVersion = 0;
+
+	Build = _GetSystemBuildLevel();
+	env->MinorBuild =  Build & 0xFFFF;
+	env->MajorBuild =  Build >> 16;
+
+	if (aulBuffer[0] == 20)
 	{
-		if(env->MajorVersion == 5 && env->MinorVersion == 1)
-			strcpy(env->osName, "Windows XP");
-		else if(env->MajorVersion == 5 && env->MinorVersion == 0)
-			strcpy(env->osName, "Windows 2000");
-		else
-			strcpy(env->osName, "Windows NT");
-
-		env->MajorBuild = (DWORD)(HIWORD(dwVersion));
+		int i = (unsigned int)aulBuffer[1];
+		if (i > 20)
+		{
+			strcpy(env->osName,"Warp");
+			env->MajorVersion = (int)i/10;
+			env->MinorVersion = i-(((int)i/10)*10);
+		}
+		else if (i == 10)
+			env->MinorVersion = 1;
 	}
-	else
-	{
-		strcpy(env->osName, "Windows 95/98/ME");
-		env->MajorBuild =  0;
-	}
-
 	strcpy(env->buildDate, __DATE__);
 	strcpy(env->buildTime, __TIME__);
 	env->DWMajorVersion = DW_MAJOR_VERSION;
@@ -6164,36 +6453,60 @@
  */
 char *dw_file_browse(char *title, char *defpath, char *ext, int flags)
 {
-	OPENFILENAME of;
-	char filenamebuf[1001] = "";
-	int rc;
+	FILEDLG fild;
+	HWND hwndFile;
+	int len;
+
+	if(defpath)
+		strcpy(fild.szFullFile, defpath);
+	else
+		strcpy(fild.szFullFile, "");
+
+	len = strlen(fild.szFullFile);
+
+	if(len)
+	{
+		if(fild.szFullFile[len-1] != '\\')
+			strcat(fild.szFullFile, "\\");
+	}
+	strcat(fild.szFullFile, "*");
 
 	if(ext)
 	{
-		strcpy(filenamebuf, "*.");
-		strcat(filenamebuf, ext);
+		strcat(fild.szFullFile, ".");
+		strcat(fild.szFullFile, ext);
 	}
 
-	memset(&of, 0, sizeof(OPENFILENAME));
-
-	of.lStructSize = sizeof(OPENFILENAME);
-	of.hwndOwner = HWND_DESKTOP;
-	of.hInstance = DWInstance;
-	of.lpstrInitialDir = defpath;
-	of.lpstrTitle = title;
-	of.lpstrFile = filenamebuf;
-	of.nMaxFile = 1000;
-	of.lpstrDefExt = ext;
-	of.Flags = 0;
-
-	if(flags & DW_FILE_SAVE)
-		rc = GetSaveFileName(&of);
-	else
-		rc = GetOpenFileName(&of);
-
-	if(rc)
-		return strdup(of.lpstrFile);
-
+	fild.cbSize = sizeof(FILEDLG);
+	fild.fl = /*FDS_HELPBUTTON |*/ FDS_CENTER | FDS_OPEN_DIALOG;
+	fild.pszTitle = title;
+	fild.pszOKButton = ((flags & DW_FILE_SAVE) ? "Save" : "Open");
+	fild.ulUser = 0L;
+	fild.pfnDlgProc = (PFNWP)WinDefFileDlgProc;
+	fild.lReturn = 0L;
+	fild.lSRC = 0L;
+	fild.hMod = 0;
+	fild.x = 0;
+	fild.y = 0;
+	fild.pszIType       = (PSZ)NULL;
+	fild.papszITypeList = (PAPSZ)NULL;
+	fild.pszIDrive      = (PSZ)NULL;
+	fild.papszIDriveList= (PAPSZ)NULL;
+	fild.sEAType        = (SHORT)0;
+	fild.papszFQFilename= (PAPSZ)NULL;
+	fild.ulFQFCount     = 0L;
+
+	hwndFile = WinFileDlg(HWND_DESKTOP, HWND_DESKTOP, &fild);
+	if(hwndFile)
+	{
+		switch(fild.lReturn)
+		{
+		case DID_OK:
+            return strdup(fild.szFullFile);
+		case DID_CANCEL:
+			return NULL;
+		}
+	}
 	return NULL;
 }
 
@@ -6208,34 +6521,7 @@
  */
 int dw_exec(char *program, int type, char **params)
 {
-	char **newparams;
-	int retcode, count = 0, z;
-
-	while(params[count])
-	{
-		count++;
-	}
-
-	newparams = (char **)malloc(sizeof(char *) * (count+1));
-
-	for(z=0;z<count;z++)
-	{
-		newparams[z] = malloc(strlen(params[z])+3);
-		strcpy(newparams[z], "\"");
-		strcat(newparams[z], params[z]);
-		strcat(newparams[z], "\"");
-	}
-	newparams[count] = NULL;
-
-	retcode = spawnvp(P_NOWAIT, program, newparams);
-
-	for(z=0;z<count;z++)
-	{
-		free(newparams[z]);
-	}
-	free(newparams);
-
-	return retcode;
+	return spawnvp(P_NOWAIT, program, (const char **)params);
 }
 
 /*
@@ -6245,29 +6531,42 @@
  */
 int dw_browse(char *url)
 {
-	char *browseurl = url;
-	int retcode;
-
-	if(strlen(url) > 7 && strncmp(url, "file://", 7) == 0)
+	/* Is there a way to find the webbrowser in Unix? */
+	char *execargs[3], browser[1024], *newurl = NULL;
+	int len;
+
+	PrfQueryProfileString(HINI_USERPROFILE, "WPURLDEFAULTSETTINGS",
+						  "DefaultBrowserExe", NULL, browser, 1024);
+
+	len = strlen(browser) - strlen("explore.exe");
+
+	execargs[0] = browser;
+	execargs[1] = url;
+	execargs[2] = NULL;
+
+	/* Special case for Web Explorer, it requires file:/// instead
+	 * of file:// so I am handling it here.
+	 */
+	if(len > 0)
 	{
-		int len, z;
-
-		browseurl = &url[7];
-		len = strlen(browseurl);
-
-		for(z=0;z<len;z++)
+		if(stricmp(&browser[len], "explore.exe") == 0)
 		{
-			if(browseurl[z] == '|')
-				browseurl[z] = ':';
-			if(browseurl[z] == '/')
-				browseurl[z] = '\\';
+			int newlen, z;
+			newurl = alloca(strlen(url) + 2);
+			sprintf(newurl, "file:///%s", &url[7]);
+			newlen = strlen(newurl);
+			for(z=8;z<(newlen-8);z++)
+			{
+				if(newurl[z] == '|')
+					newurl[z] = ':';
+				if(newurl[z] == '/')
+					newurl[z] = '\\';
+			}
+			execargs[1] = newurl;
 		}
 	}
 
-	retcode = (int)ShellExecute(NULL, "open", browseurl, NULL, NULL, SW_SHOWNORMAL);
-	if(retcode<33 && retcode != 2)
-		return -1;
-	return 1;
+	return dw_exec(browser, DW_EXEC_GUI, execargs);
 }
 
 /*
@@ -6281,7 +6580,6 @@
 
 	if(!_user_dir[0])
 	{
-		/* Figure out how to do this the "Windows way" */
 		char *home = getenv("HOME");
 
 		if(home)
@@ -6301,7 +6599,7 @@
  */
 void dw_window_function(HWND handle, void *function, void *data)
 {
-	SendMessage(handle, WM_USER, (WPARAM)function, (LPARAM)data);
+	WinSendMsg(handle, WM_USER, (MPARAM)function, (MPARAM)data);
 }
 
 #ifndef NO_SIGNALS
@@ -6317,11 +6615,23 @@
 {
 	ULONG message = 0L;
 
+	if(strcmp(signame, "lose-focus") == 0)
+	{
+		char tmpbuf[100];
+
+		WinQueryClassName(window, 99, tmpbuf);
+
+		if(strncmp(tmpbuf, "#2", 3) == 0)
+		{
+			HENUM henum = WinBeginEnumWindows(window);
+			HWND child = WinGetNextWindow(henum);
+			WinEndEnumWindows(henum);
+			if(child)
+				window = child;
+		}
+	}
 	if(window && signame && sigfunc)
 	{
-		if(stricmp(signame, "set-focus") == 0)
-			window = _normalize_handle(window);
-
 		if((message = _findsigmessage(signame)) != 0)
 			_new_signal(message, window, sigfunc, data);
 	}
@@ -6470,7 +6780,7 @@
 		case 1003L:
 		case 1004L:
 			dw_window_destroy(testwindow);;
-            count--;
+			count--;
 			break;
 		}
 		if(!count)
@@ -6487,7 +6797,7 @@
 	/* Return -1 to allow the default handlers to return. */
 	count--;
 	if(!count)
-        exit(0);
+		exit(0);
 	return -1;
 }
 #endif
@@ -6495,12 +6805,7 @@
 /*
  * Let's demonstrate the functionality of this library. :)
  */
-int WINAPI WinMain(
-  HINSTANCE hInstance,
-  HINSTANCE hPrevInstance,
-  LPSTR lpCmdLine,
-  int nCmdShow
-)
+int main(void)
 {
 	ULONG flStyle = DW_FCF_SYSMENU | DW_FCF_TITLEBAR |
 		DW_FCF_SHELLPOSITION | DW_FCF_TASKLIST | DW_FCF_DLGBORDER;
@@ -6551,7 +6856,7 @@
 	dw_window_set_usize(mainwindow, 170, 340);
 
 	/* Another small example */
-	flStyle |= DW_FCF_MINMAX | DW_FCF_SIZEBORDER;
+	flStyle |= FCF_MINMAX | FCF_SIZEBORDER;
 
 	testwindow = dw_window_new(HWND_DESKTOP, "Wow a test dialog! :) yay!", flStyle);
 
@@ -6570,7 +6875,7 @@
 	dw_notebook_page_set_text(notebook, pageid, "Test page");
 	dw_notebook_page_set_status_text(notebook, pageid, "Test page");
 
-    dw_notebook_pack(notebook, pageid, testbox);
+	dw_notebook_pack(notebook, pageid, testbox);
 
 	testok = dw_button_new("Ok", 1003L);
 
@@ -6605,7 +6910,6 @@
 	dw_window_show(testwindow);
 
 #ifdef USE_FILTER
-
 	dw_main(0L, (void *)testfilter);
 #else
 	/* Setup the function callbacks */