Release 0.6

Tue Jan  4 13:01:33 1994  David Metcalfe <david@prism.demon.co.uk>

        * [window/caret.c]
        Modified code to use system timer.

Jan 9, 94 martin2@trgcorp.solucorp.qc.ca (Martin Ayotte)

	* [windows/win.c]
	Windows create if required new XLIB MenuBar & CaptionBar.

	* [windows/defwnd.c]
	WM_CALCSIZE Move & Resize caption, menubar & scrollbars.
	(I'm not sure it's the good place for it, but it work...)

	* [loader/resource.c]
	optimize in FindResourceByNumber, make lseek() if next type ...

	* [controls/scroll.c]
	scrollbar buttons are now using system resources bitmaps.

	* [controls/caption.c] - new file ...
	captionbar showing title, close button with SysMenu,
	and other buttons using system resources bitmaps.

	* [controls/menu.c]
	New functions: SetMenuItemBitmaps() with 'glues',
	Make new version of LoadMenu() & ParseMenu(),
	( put #define USE_POPUPMENU ).
	Implementation of MenuBar functions.
	
	* [sysres.dll]
	New bitmaps for system such OBM_CLOSE, OBM_MINIMIZE, OBM_UPARROWI.
	New SYSMENU menu, it don't work yet ! :-((

Tue Jan 11 05:27:45 1994  julliard@di.epfl.ch (Alexandre Julliard

	* [memory/atom.c]
	Fixed a bug that could cause atoms to be case-sensitive.

	* [misc/rect.c]
	Bug fix in SubtractRect().

	* [objects/clipping.c]
	Bug fix when setting the clip mask to an empty region.

	* [windows/dce.c]
	Bug fix in ReleaseDC().

	* [windows/dialog.c]
	Call AdjustWindowRectEx() before creating the dialog window.
	Added support for DS_MODALFRAME style.

	* [windows/event.c]
	Cleaned up event handling and removed old Xt stuff.
	Moved double-click handling to windows/message.c

	* [windows/focus.c]
	Bug fix: only set the X focus when the window is viewable.

	* [windows/graphics.c]
	Rewritten DrawReliefRect() to use brush instead of pen, and
	to use the system colors.

	* [windows/message.c]
	Implemented WM_NCHITTEST message sending, and non-client
	mouse messages.
	Cleaned up double-click handling, and removed the Xt code.

	* [windows/nonclient.c]  (New file)
	Implemented AdjustWindowRect().
	Implemented WM_NCCALCSIZE, WM_NCHITTEST and WM_NCPAINT handling.

	* [windows/painting.c]
	Added sending of the WM_NCPAINT message in BeginPaint().

	* [windows/sysmetrics.c] [include/sysmetrics.h]  (New files)
	Implemented system metrics.

	* [windows/win.c]
	Bug fix in setting the parent and owner in CreateWindow().
	Removed the Xt code.

	* [windows/winpos.c]
	Added sending of the WM_NCPAINT message in SetWindowPos().
	Removed the Xt code.
diff --git a/windows/Makefile b/windows/Makefile
index 30570a1..8ac8efa 100644
--- a/windows/Makefile
+++ b/windows/Makefile
@@ -2,7 +2,8 @@
 
 OBJS=class.o dc.o dce.o event.o message.o win.o timer.o graphics.o \
     	clipping.o mapping.o painting.o keyboard.o utility.o syscolor.o \
-	defwnd.o defdlg.o dialog.o focus.o scroll.o caret.o winpos.o
+	defwnd.o defdlg.o dialog.o focus.o scroll.o caret.o winpos.o \
+	sysmetrics.o nonclient.o
 
 default: windows.o
 
diff --git a/windows/caret.c b/windows/caret.c
index eb78dc8..566e321 100644
--- a/windows/caret.c
+++ b/windows/caret.c
@@ -6,12 +6,8 @@
 
 static char Copyright[] = "Copyright  David Metcalfe, 1993";
 
-#include <X11/Intrinsic.h>
-
 #include "windows.h"
 
-extern XtAppContext XT_app_context;
-
 typedef struct
 {
     HWND          hwnd;
@@ -23,49 +19,46 @@
     short         height;
     COLORREF      color;
     WORD          timeout;
-    XtIntervalId  xtid;
+    WORD          timerid;
 } CARET;
 
 static CARET Caret;
 static BOOL LockCaret;
 
-static void CARET_Callback(XtPointer data, XtIntervalId *xtid);
-static void CARET_HideCaret(CARET *pCaret);
+static WORD CARET_Callback(HWND hwnd, WORD msg, WORD timerid, LONG ctime);
+static void CARET_HideCaret();
 
 
 /*****************************************************************
  *               CARET_Callback
  */
 
-static void CARET_Callback(XtPointer data, XtIntervalId *xtid)
+static WORD CARET_Callback(HWND hwnd, WORD msg, WORD timerid, LONG ctime)
 {
-    CARET *pCaret = (CARET *)data;
     HDC hdc;
     HBRUSH hBrush;
     HRGN rgn;
 
 #ifdef DEBUG_CARET
-    printf("CARET_Callback: LockCaret=%d, hidden=%d, on=%d\n",
-	   LockCaret, pCaret->hidden, pCaret->on);
+    printf("CARET_Callback: id=%d: LockCaret=%d, hidden=%d, on=%d\n",
+	   timerid, LockCaret, Caret.hidden, Caret.on);
 #endif
-    if (!LockCaret && (!pCaret->hidden || pCaret->on))
+    if (!LockCaret && (!Caret.hidden || Caret.on))
     {
-	pCaret->on = (pCaret->on ? FALSE : TRUE);
-	hdc = GetDC(pCaret->hwnd);
-	hBrush = CreateSolidBrush(pCaret->color);
+	Caret.on = (Caret.on ? FALSE : TRUE);
+	hdc = GetDC(Caret.hwnd);
+	hBrush = CreateSolidBrush(Caret.color);
 	SelectObject(hdc, (HANDLE)hBrush);
 	SetROP2(hdc, R2_XORPEN);
-	rgn = CreateRectRgn(pCaret->x, pCaret->y, 
-			    pCaret->x + pCaret->width,
-			    pCaret->y + pCaret->height);
+	rgn = CreateRectRgn(Caret.x, Caret.y, 
+			    Caret.x + Caret.width,
+			    Caret.y + Caret.height);
 	FillRgn(hdc, rgn, hBrush);
 	DeleteObject((HANDLE)rgn);
 	DeleteObject((HANDLE)hBrush);
-	ReleaseDC(pCaret->hwnd, hdc);
+	ReleaseDC(Caret.hwnd, hdc);
     }
-
-    pCaret->xtid = XtAppAddTimeOut(XT_app_context, pCaret->timeout,
-				   CARET_Callback, pCaret);
+    return 0;
 }
 
 
@@ -73,24 +66,24 @@
  *               CARET_HideCaret
  */
 
-static void CARET_HideCaret(CARET *pCaret)
+static void CARET_HideCaret()
 {
     HDC hdc;
     HBRUSH hBrush;
     HRGN rgn;
 
-    pCaret->on = FALSE;
-    hdc = GetDC(pCaret->hwnd);
-    hBrush = CreateSolidBrush(pCaret->color);
+    Caret.on = FALSE;
+    hdc = GetDC(Caret.hwnd);
+    hBrush = CreateSolidBrush(Caret.color);
     SelectObject(hdc, (HANDLE)hBrush);
     SetROP2(hdc, R2_XORPEN);
-    rgn = CreateRectRgn(pCaret->x, pCaret->y, 
-			pCaret->x + pCaret->width,
-			pCaret->y + pCaret->height);
+    rgn = CreateRectRgn(Caret.x, Caret.y, 
+			Caret.x + Caret.width,
+			Caret.y + Caret.height);
     FillRgn(hdc, rgn, hBrush);
     DeleteObject((HANDLE)rgn);
     DeleteObject((HANDLE)hBrush);
-    ReleaseDC(pCaret->hwnd, hdc);
+    ReleaseDC(Caret.hwnd, hdc);
 }
 
 
@@ -102,6 +95,7 @@
 {
     if (!hwnd) return;
 
+
     /* if cursor already exists, destroy it */
 /*    if (Caret.hwnd)
 	DestroyCaret();
@@ -131,8 +125,11 @@
     Caret.timeout = 750;
     LockCaret = FALSE;
 
-    Caret.xtid = XtAppAddTimeOut(XT_app_context, Caret.timeout,
-				 CARET_Callback, &Caret);
+    Caret.timerid = SetSystemTimer(NULL, 0, Caret.timeout, CARET_Callback);
+
+#ifdef DEBUG_CARET
+    printf("CreateCaret: hwnd=%d, timerid=%d\n", hwnd, Caret.timerid);
+#endif
 }
    
 
@@ -144,10 +141,14 @@
 {
 /*    if (!Caret.hwnd) return;
 */
-    XtRemoveTimeOut(Caret.xtid);
+#ifdef DEBUG_CARET
+    printf("DestroyCaret: timerid\n", Caret.timerid);
+#endif
+
+    KillSystemTimer(NULL, Caret.timerid);
 
     if (Caret.on)
-	CARET_HideCaret(&Caret);
+	CARET_HideCaret();
 
     Caret.hwnd = 0;          /* cursor marked as not existing */
 }
@@ -171,7 +172,7 @@
 
     LockCaret = TRUE;
     if (Caret.on)
-	CARET_HideCaret(&Caret);
+	CARET_HideCaret();
 
     Caret.x = x;
     Caret.y = y;
@@ -189,7 +190,7 @@
 
     LockCaret = TRUE;
     if (Caret.on)
-	CARET_HideCaret(&Caret);
+	CARET_HideCaret();
 
     ++Caret.hidden;
     LockCaret = FALSE;
@@ -221,7 +222,9 @@
 {
     if (!Caret.hwnd) return;
 
+    KillSystemTimer(NULL, Caret.timerid);
     Caret.timeout = msecs;
+    Caret.timerid = SetSystemTimer(NULL, 0, Caret.timeout, CARET_Callback);
 }
 
 
diff --git a/windows/dce.c b/windows/dce.c
index 7b24f1d..5aef283 100644
--- a/windows/dce.c
+++ b/windows/dce.c
@@ -15,8 +15,7 @@
 
 #define NB_DCE    5  /* Number of DCEs created at startup */
 
-extern Display * XT_display;
-extern Screen * XT_screen;
+extern Display * display;
 
 static HANDLE firstDCE = 0;
 static HDC defaultDCstate = 0;
@@ -159,11 +158,11 @@
 	    IntersectVisRect( hdc, 0, 0, dc->w.DCSizeX, dc->w.DCSizeY );
 	}	
     }
-    else dc->u.x.drawable = DefaultRootWindow( XT_display );
+    else dc->u.x.drawable = DefaultRootWindow( display );
 
     if (flags & DCX_CLIPCHILDREN)
-	XSetSubwindowMode( XT_display, dc->u.x.gc, ClipByChildren );
-    else XSetSubwindowMode( XT_display, dc->u.x.gc, IncludeInferiors);
+	XSetSubwindowMode( display, dc->u.x.gc, ClipByChildren );
+    else XSetSubwindowMode( display, dc->u.x.gc, IncludeInferiors);
 
     if ((flags & DCX_INTERSECTRGN) || (flags & DCX_EXCLUDERGN))
     {
@@ -216,7 +215,7 @@
 int ReleaseDC( HWND hwnd, HDC hdc )
 {
     HANDLE hdce;
-    DCE * dce;
+    DCE * dce = NULL;
     
 #ifdef DEBUG_DC
     printf( "ReleaseDC: %d %d\n", hwnd, hdc );
@@ -227,11 +226,12 @@
 	if (!(dce = (DCE *) USER_HEAP_ADDR( hdce ))) return 0;
 	if (dce->inUse && (dce->hdc == hdc)) break;
     }
+    if (!hdce) return 0;
 
     if (dce->type == DCE_CACHE_DC)
     {
 	SetDCState( dce->hdc, defaultDCstate );
 	dce->inUse = FALSE;
     }
-    return (hdce != 0);
+    return 1;
 }
diff --git a/windows/defwnd.c b/windows/defwnd.c
index e2f9a39..dc75bc1 100644
--- a/windows/defwnd.c
+++ b/windows/defwnd.c
@@ -6,17 +6,19 @@
 
 static char Copyright[] = "Copyright  Alexandre Julliard, 1993";
 
-#ifndef USE_XLIB
-#include <X11/Intrinsic.h>
-#include <X11/Shell.h>
-#endif
 
 #include "windows.h"
 #include "win.h"
 #include "class.h"
 #include "user.h"
 
-extern Display * XT_display;
+extern Display * display;
+
+extern LONG NC_HandleNCPaint( HWND hwnd, HRGN hrgn );
+extern LONG NC_HandleNCCalcSize( HWND hwnd, NCCALCSIZE_PARAMS *params );
+extern LONG NC_HandleNCHitTest( HWND hwnd, POINT pt );
+extern LONG NC_HandleNCMouseMsg(HWND hwnd, WORD msg, WORD wParam, LONG lParam);
+
 
 /***********************************************************************
  *           DefWindowProc   (USER.107)
@@ -26,6 +28,7 @@
     CLASS * classPtr;
     LPSTR textPtr;
     int len;
+    int tempwidth, tempheight;
     WND * wndPtr = WIN_FindWndPtr( hwnd );
     
 #ifdef DEBUG_MESSAGE
@@ -51,40 +54,19 @@
 	}
 
     case WM_NCCALCSIZE:
-	{
-#ifdef USE_XLIB
-	    NCCALCSIZE_PARAMS *params = (NCCALCSIZE_PARAMS *)lParam;
-	    if (wndPtr->dwStyle & WS_CHILD)
-	    {
-		if (wndPtr->dwStyle & WS_BORDER)
-		{
-		    params->rgrc[0].left   += 1; /* SM_CXBORDER */
-		    params->rgrc[0].top    += 1; /* SM_CYBORDER */
-		    params->rgrc[0].right  -= 1; /* SM_CXBORDER */
-		    params->rgrc[0].bottom -= 1; /* SM_CYBORDER */
-		}
-	    }
-	    else
-	    {
-		params->rgrc[0].left   += 4; /* SM_CXFRAME */
-		params->rgrc[0].top    += 30; /* SM_CYFRAME+SM_CYCAPTION */
-		params->rgrc[0].right  -= 4; /* SM_CXFRAME */
-		params->rgrc[0].bottom -= 4; /* SM_CYFRAME */
-		if (wndPtr->dwStyle & WS_VSCROLL)
-		{
-		    params->rgrc[0].right -= 16; /* SM_CXVSCROLL */
-		}
-		if (wndPtr->dwStyle & WS_HSCROLL)
-		{
-		    params->rgrc[0].bottom += 16;  /* SM_CYHSCROLL */
-		}
-	    }
-#endif
-	    return 0;
-	}
-	
-    case WM_CREATE:
-	return 0;
+	return NC_HandleNCCalcSize( hwnd, (NCCALCSIZE_PARAMS *)lParam );
+
+    case WM_NCPAINT:
+	return NC_HandleNCPaint( hwnd, (HRGN)wParam );
+
+    case WM_NCHITTEST:
+	return NC_HandleNCHitTest( hwnd, MAKEPOINT(lParam) );
+
+    case WM_NCLBUTTONDOWN:
+    case WM_NCLBUTTONUP:
+    case WM_NCLBUTTONDBLCLK:
+    case WM_NCMOUSEMOVE:
+	return NC_HandleNCMouseMsg( hwnd, msg, wParam, lParam );
 
     case WM_NCDESTROY:
 	{
@@ -188,18 +170,42 @@
 					    strlen((LPSTR)lParam) + 1);
 	    textPtr = (LPSTR)USER_HEAP_ADDR(wndPtr->hText);
 	    strcpy(textPtr, (LPSTR)lParam);
-#ifdef USE_XLIB
-	    XStoreName( XT_display, wndPtr->window, textPtr );
-#else
-	    if (wndPtr->shellWidget)
-		XtVaSetValues( wndPtr->shellWidget, XtNtitle, textPtr, NULL );
-#endif
+	    XStoreName( display, wndPtr->window, textPtr );
 	    return (0L);
 	}
     case WM_SETCURSOR:
 	if (wndPtr->hCursor != (HCURSOR)NULL)
 	    SetCursor(wndPtr->hCursor);
 	return 0L;
+    case WM_SYSCOMMAND:
+	switch (wParam)
+	    {
+	    case SC_CLOSE:
+		ShowWindow(hwnd, SW_MINIMIZE);
+		printf("defdwndproc WM_SYSCOMMAND SC_CLOSE !\n");
+	        return SendMessage( hwnd, WM_CLOSE, 0, 0 );
+	    case SC_RESTORE:
+		ShowWindow(hwnd, SW_RESTORE);
+		break;
+	    case SC_MINIMIZE:
+		ShowWindow(hwnd, SW_MINIMIZE);
+		printf("defdwndproc WM_SYSCOMMAND SC_MINIMIZE !\n");
+		break;
+	    case SC_MAXIMIZE:
+		ShowWindow(hwnd, SW_MAXIMIZE);
+		break;
+	    }
+    	break;    	
+    case WM_SYSKEYDOWN:
+    	if (wParam == VK_MENU) {
+    	    printf("VK_MENU Pressed // hMenu=%04X !\n", GetMenu(hwnd));
+    	    }
+    	break;    	
+    case WM_SYSKEYUP:
+    	if (wParam == VK_MENU) {
+    	    printf("VK_MENU Released // hMenu=%04X !\n", GetMenu(hwnd));
+    	    }
+    	break;    	
     }
     return 0;
 }
diff --git a/windows/dialog.c b/windows/dialog.c
index 83a0389..6316e34 100644
--- a/windows/dialog.c
+++ b/windows/dialog.c
@@ -184,11 +184,13 @@
     HMENU hMenu;
     HFONT hFont = 0;
     HWND hwnd;
+    RECT rect;
     WND * wndPtr;
     int i;
     DLGTEMPLATE template;
     DLGCONTROLHEADER * header;
     DIALOGINFO * dlgInfo;
+    DWORD exStyle = 0;
     WORD xUnit = xBaseUnit;
     WORD yUnit = yBaseUnit;
 
@@ -229,7 +231,7 @@
 	    HFONT oldFont;
 	    HDC hdc;
 
-	    hdc = GetDC(GetDesktopWindow());
+	    hdc = GetDC(0);
 	    oldFont = SelectObject( hdc, hFont );
 	    GetTextMetrics( hdc, &tm );
 	    SelectObject( hdc, oldFont );
@@ -241,14 +243,19 @@
     
       /* Create dialog main window */
 
-    hwnd = CreateWindow( template.className, template.caption,
-			 template.header->style,
-			 template.header->x * xUnit / 4,
-			 template.header->y * yUnit / 8,
-			 template.header->cx * xUnit / 4,
-			 template.header->cy * yUnit / 8,
-			 owner, hMenu, hInst,
-			 NULL );
+    rect.left = rect.top = 0;
+    rect.right = template.header->cx * xUnit / 4;
+    rect.bottom = template.header->cy * yUnit / 8;
+    if (template.header->style & DS_MODALFRAME) exStyle |= WS_EX_DLGMODALFRAME;
+    AdjustWindowRectEx( &rect, template.header->style, hMenu, exStyle );
+
+    hwnd = CreateWindowEx( exStyle, template.className, template.caption,
+			   template.header->style,
+			   rect.left + template.header->x * xUnit / 4,
+			   rect.top + template.header->y * yUnit / 8,
+			   rect.right - rect.left, rect.bottom - rect.top,
+			   owner, hMenu, hInst,
+			   NULL );
     if (!hwnd)
     {
 	if (hFont) DeleteObject( hFont );
diff --git a/windows/event.c b/windows/event.c
index 5f3aa3d..b2de49f 100644
--- a/windows/event.c
+++ b/windows/event.c
@@ -6,33 +6,23 @@
 
 static char Copyright[] = "Copyright  Alexandre Julliard, 1993";
 
-#include <X11/Intrinsic.h>
-#include <X11/StringDefs.h>
-#include <X11/Core.h>
+#include <X11/Xlib.h>
 
 #include "windows.h"
 #include "win.h"
 #include "class.h"
+#include "message.h"
 
 
 #define NB_BUTTONS      3     /* Windows can handle 3 buttons */
-static WORD dblclick_time = 300; /* Max. time for a double click (milliseconds) */
 
-extern Display * XT_display;
-
-  /* Event handlers */
-static void EVENT_expose();
-static void EVENT_key();
-static void EVENT_mouse_motion();
-static void EVENT_mouse_button();
-static void EVENT_structure();
-static void EVENT_focus_change();
-static void EVENT_enter_notify();
+extern Display * display;
 
   /* X context to associate a hwnd to an X window */
 static XContext winContext = 0;
 
   /* State variables */
+static WORD ALTKeyState;
 static HWND captureWnd = 0;
 Window winHasCursor = 0;
 extern HWND hWndFocus;
@@ -118,6 +108,17 @@
 };
 #endif
 
+  /* Event handlers */
+static void EVENT_key( HWND hwnd, XKeyEvent *event );
+static void EVENT_ButtonPress( HWND hwnd, XButtonEvent *event );
+static void EVENT_ButtonRelease( HWND hwnd, XButtonEvent *event );
+static void EVENT_MotionNotify( HWND hwnd, XMotionEvent *event );
+static void EVENT_EnterNotify( HWND hwnd, XCrossingEvent *event );
+static void EVENT_FocusIn( HWND hwnd, XFocusChangeEvent *event );
+static void EVENT_FocusOut( HWND hwnd, XFocusChangeEvent *event );
+static void EVENT_Expose( HWND hwnd, XExposeEvent *event );
+static void EVENT_ConfigureNotify( HWND hwnd, XConfigureEvent *event );
+
 
 /***********************************************************************
  *           EVENT_ProcessEvent
@@ -128,117 +129,72 @@
 {
     HWND hwnd;
     XPointer ptr;
-    Boolean cont_dispatch = TRUE;
     
-    XFindContext( XT_display, ((XAnyEvent *)event)->window, winContext, &ptr );
-    hwnd = (HWND)ptr & 0xffff;
+    XFindContext( display, ((XAnyEvent *)event)->window, winContext, &ptr );
+    hwnd = (HWND) (int)ptr;
 
 #ifdef DEBUG_EVENT
-    printf( "Got event %s for hwnd %d\n", 
-	    event_names[event->type], hwnd );
+    printf( "Got event %s for hwnd %d\n", event_names[event->type], hwnd );
 #endif
 
     switch(event->type)
     {
-        case Expose:
-	    EVENT_expose( 0, hwnd, event, &cont_dispatch );
-	    break;
+    case KeyPress:
+    case KeyRelease:
+	EVENT_key( hwnd, (XKeyEvent*)event );
+	break;
+	
+    case ButtonPress:
+	EVENT_ButtonPress( hwnd, (XButtonEvent*)event );
+	break;
 
-	case KeyPress:
-	case KeyRelease:
-	    EVENT_key( 0, hwnd, event, &cont_dispatch );
-	    break;
+    case ButtonRelease:
+	EVENT_ButtonRelease( hwnd, (XButtonEvent*)event );
+	break;
 
-	case MotionNotify:
-	    EVENT_mouse_motion( 0, hwnd, event, &cont_dispatch );
-	    break;
+    case MotionNotify:
+	EVENT_MotionNotify( hwnd, (XMotionEvent*)event );
+	break;
 
-	case ButtonPress:
-	case ButtonRelease:
-	    EVENT_mouse_button( 0, hwnd, event, &cont_dispatch );
-	    break;
+    case EnterNotify:
+	EVENT_EnterNotify( hwnd, (XCrossingEvent*)event );
+	break;
 
-	case CirculateNotify:
-	case ConfigureNotify:
-	case MapNotify:
-	case UnmapNotify:
-	    EVENT_structure( 0, hwnd, event, &cont_dispatch );
-	    break;
+    case FocusIn:
+	EVENT_FocusIn( hwnd, (XFocusChangeEvent*)event );
+	break;
 
-	case FocusIn:
-	case FocusOut:
-	    EVENT_focus_change( 0, hwnd, event, &cont_dispatch );
-	    break;
+    case FocusOut:
+	EVENT_FocusOut( hwnd, (XFocusChangeEvent*)event );
+	break;
 
-	case EnterNotify:
-	    EVENT_enter_notify( 0, hwnd, event, &cont_dispatch );
-	    break;
+    case Expose:
+	EVENT_Expose( hwnd, (XExposeEvent*)event );
+	break;
+
+    case ConfigureNotify:
+	EVENT_ConfigureNotify( hwnd, (XConfigureEvent*)event );
+	break;
 
 #ifdef DEBUG_EVENT
-	default:
-	    printf( "Unprocessed event %s for hwnd %d\n", 
-		    event_names[event->type], hwnd );
-	    break;
-#endif
+    default:    
+	printf( "Unprocessed event %s for hwnd %d\n",
+	        event_names[event->type], hwnd );
+	break;
+#endif	
     }
 }
 
 
 /***********************************************************************
- *           EVENT_AddHandlers
+ *           EVENT_RegisterWindow
  *
- * Add the event handlers to the given window
+ * Associate an X window to a HWND.
  */
-#ifdef USE_XLIB
-void EVENT_AddHandlers( Window w, int hwnd )
+void EVENT_RegisterWindow( Window w, HWND hwnd )
 {
     if (!winContext) winContext = XUniqueContext();
-    XSaveContext( XT_display, w, winContext, (XPointer)hwnd );
-}
-#else
-void EVENT_AddHandlers( Widget w, int hwnd )
-{
-    XtAddEventHandler(w, ExposureMask, FALSE,
-		      EVENT_expose, (XtPointer)hwnd );
-    XtAddEventHandler(w, KeyPressMask | KeyReleaseMask, FALSE, 
-		      EVENT_key, (XtPointer)hwnd );
-    XtAddEventHandler(w, PointerMotionMask, FALSE,
-		      EVENT_mouse_motion, (XtPointer)hwnd );
-    XtAddEventHandler(w, ButtonPressMask | ButtonReleaseMask, FALSE,
-		      EVENT_mouse_button, (XtPointer)hwnd );
-    XtAddEventHandler(w, StructureNotifyMask, FALSE,
-		      EVENT_structure, (XtPointer)hwnd );
-    XtAddEventHandler(w, FocusChangeMask, FALSE,
-		      EVENT_focus_change, (XtPointer)hwnd );
-    XtAddEventHandler(w, EnterWindowMask, FALSE,
-		      EVENT_enter_notify, (XtPointer)hwnd );
-}
-#endif
-
-
-/***********************************************************************
- *           EVENT_RemoveHandlers
- *
- * Remove the event handlers of the given window
- */
-void EVENT_RemoveHandlers( Widget w, int hwnd )
-{
-#ifndef USE_XLIB
-    XtRemoveEventHandler(w, ExposureMask, FALSE,
-			 EVENT_expose, (XtPointer)hwnd );
-    XtRemoveEventHandler(w, KeyPressMask | KeyReleaseMask, FALSE, 
-			 EVENT_key, (XtPointer)hwnd );
-    XtRemoveEventHandler(w, PointerMotionMask, FALSE,
-			 EVENT_mouse_motion, (XtPointer)hwnd );
-    XtRemoveEventHandler(w, ButtonPressMask | ButtonReleaseMask, FALSE,
-			 EVENT_mouse_button, (XtPointer)hwnd );
-    XtRemoveEventHandler(w, StructureNotifyMask, FALSE,
-			 EVENT_structure, (XtPointer)hwnd );
-    XtRemoveEventHandler(w, FocusChangeMask, FALSE,
-			 EVENT_focus_change, (XtPointer)hwnd );
-    XtRemoveEventHandler(w, EnterWindowMask, FALSE,
-			 EVENT_enter_notify, (XtPointer)hwnd );
-#endif
+    XSaveContext( display, w, winContext, (XPointer)(int)hwnd );
 }
 
 
@@ -262,12 +218,9 @@
 
 
 /***********************************************************************
- *           EVENT_expose
- *
- * Handle a X expose event
+ *           EVENT_Expose
  */
-static void EVENT_expose( Widget w, int hwnd, XExposeEvent *event,
-			  Boolean *cont_dispatch )
+static void EVENT_Expose( HWND hwnd, XExposeEvent *event )
 {
     RECT rect;
     WND * wndPtr = WIN_FindWndPtr( hwnd );
@@ -288,10 +241,8 @@
  *
  * Handle a X key event
  */
-static void EVENT_key( Widget w, int hwnd, XKeyEvent *event,
-		       Boolean *cont_dispatch )
+static void EVENT_key( HWND hwnd, XKeyEvent *event )
 {
-    MSG msg;
     char Str[24]; 
     XComposeStatus cs; 
     KeySym keysym;
@@ -350,25 +301,20 @@
 
     if (event->type == KeyPress)
     {
-	msg.hwnd    = hwnd;
-	msg.message = WM_KEYDOWN;
-	msg.wParam  = vkey;
+	if (vkey == VK_MENU) ALTKeyState = TRUE;
 	keylp.lp1.count = 1;
 	keylp.lp1.code = LOBYTE(event->keycode);
 	keylp.lp1.extended = (extended ? 1 : 0);
 	keylp.lp1.context = (event->state & Mod1Mask ? 1 : 0);
 	keylp.lp1.previous = (KeyDown ? 0 : 1);
 	keylp.lp1.transition = 0;
-	msg.lParam  = keylp.lp2;
 #ifdef DEBUG_KEY
-	printf("            wParam=%X, lParam=%lX\n", msg.wParam, msg.lParam);
+	printf("            wParam=%X, lParam=%lX\n", vkey, keylp.lp2 );
 #endif
-	msg.time = event->time;
-	msg.pt.x = event->x & 0xffff;
-	msg.pt.y = event->y & 0xffff;
-    
-	hardware_event( hwnd, WM_KEYDOWN, vkey, keylp.lp2,
-		        event->x & 0xffff, event->y & 0xffff, event->time, 0 );
+	hardware_event( hwnd, ALTKeyState ? WM_SYSKEYDOWN : WM_KEYDOWN, 
+		        vkey, keylp.lp2,
+		        event->x_root & 0xffff, event->y_root & 0xffff,
+		        event->time, 0 );
 	KeyDown = TRUE;
 
 	/* The key translation ought to take place in TranslateMessage().
@@ -378,218 +324,145 @@
 	 */
 	if (count == 1)                /* key has an ASCII representation */
 	{
-	    msg.hwnd    = hwnd;
-	    msg.message = WM_CHAR;
-	    msg.wParam  = (WORD)Str[0];
-	    msg.lParam  = keylp.lp2;
 #ifdef DEBUG_KEY
-	printf("WM_CHAR :   wParam=%X\n", msg.wParam);
+	    printf("WM_CHAR :   wParam=%X\n", (WORD)Str[0] );
 #endif
-	    msg.time = event->time;
-	    msg.pt.x = event->x & 0xffff;
-	    msg.pt.y = event->y & 0xffff;
 	    PostMessage( hwnd, WM_CHAR, (WORD)Str[0], keylp.lp2 );
 	}
     }
     else
     {
-	msg.hwnd    = hwnd;
-	msg.message = WM_KEYUP;
-	msg.wParam  = vkey;
+	if (vkey == VK_MENU) ALTKeyState = FALSE;
 	keylp.lp1.count = 1;
 	keylp.lp1.code = LOBYTE(event->keycode);
 	keylp.lp1.extended = (extended ? 1 : 0);
 	keylp.lp1.context = (event->state & Mod1Mask ? 1 : 0);
 	keylp.lp1.previous = 1;
 	keylp.lp1.transition = 1;
-	msg.lParam  = keylp.lp2;
 #ifdef DEBUG_KEY
-	printf("            wParam=%X, lParam=%lX\n", msg.wParam, msg.lParam);
+	printf("            wParam=%X, lParam=%lX\n", vkey, keylp.lp2 );
 #endif
-	msg.time = event->time;
-	msg.pt.x = event->x & 0xffff;
-	msg.pt.y = event->y & 0xffff;
-    
-	hardware_event( hwnd, WM_KEYUP, vkey, keylp.lp2,
-		        event->x & 0xffff, event->y & 0xffff, event->time, 0 );
+	hardware_event( hwnd, 
+		        ((ALTKeyState || vkey == VK_MENU) ? 
+			 WM_SYSKEYUP : WM_KEYUP), 
+		        vkey, keylp.lp2,
+		        event->x_root & 0xffff, event->y_root & 0xffff,
+		        event->time, 0 );
 	KeyDown = FALSE;
     }
 }
 
 
 /***********************************************************************
- *           EVENT_mouse_motion
- *
- * Handle a X mouse motion event
+ *           EVENT_MotionNotify
  */
-static void EVENT_mouse_motion( Widget w, int hwnd, XMotionEvent *event, 
-			        Boolean *cont_dispatch )
+static void EVENT_MotionNotify( HWND hwnd, XMotionEvent *event )
 {
-    WND * wndPtr = WIN_FindWndPtr( hwnd );
-    if (!wndPtr) return;
-
-      /* Position must be relative to client area */
-    event->x -= wndPtr->rectClient.left - wndPtr->rectWindow.left;
-    event->y -= wndPtr->rectClient.top - wndPtr->rectWindow.top;
-
-    hardware_event( hwnd, WM_MOUSEMOVE, EVENT_XStateToKeyState( event->state ),
-		    (event->x & 0xffff) | (event->y << 16),
-		    event->x & 0xffff, event->y & 0xffff,
+    hardware_event( hwnd, WM_MOUSEMOVE,
+		    EVENT_XStateToKeyState( event->state ), 0L,
+		    event->x_root & 0xffff, event->y_root & 0xffff,
 		    event->time, 0 );		    
 }
 
 
 /***********************************************************************
- *           EVENT_mouse_button
- *
- * Handle a X mouse button event
+ *           EVENT_ButtonPress
  */
-static void EVENT_mouse_button( Widget w, int hwnd, XButtonEvent *event,
-			        Boolean *cont_dispatch )
+static void EVENT_ButtonPress( HWND hwnd, XButtonEvent *event )
 {
-    static WORD messages[3][NB_BUTTONS] = 
-    {
-	{ WM_LBUTTONDOWN, WM_MBUTTONDOWN, WM_RBUTTONDOWN },
-	{ WM_LBUTTONUP, WM_MBUTTONUP, WM_RBUTTONUP },
-        { WM_LBUTTONDBLCLK, WM_MBUTTONDBLCLK, WM_RBUTTONDBLCLK }
-    };
-    static unsigned long lastClickTime[NB_BUTTONS] = { 0, 0, 0 };
-        
-    int buttonNum, prevTime, type;
+    static WORD messages[NB_BUTTONS] = 
+        { WM_LBUTTONDOWN, WM_MBUTTONDOWN, WM_RBUTTONDOWN };
+    int buttonNum = event->button - 1;
 
-    WND * wndPtr = WIN_FindWndPtr( hwnd );
-    if (!wndPtr) return;
-
-      /* Position must be relative to client area */
-    event->x -= wndPtr->rectClient.left - wndPtr->rectWindow.left;
-    event->y -= wndPtr->rectClient.top - wndPtr->rectWindow.top;
-
-    buttonNum = event->button-1;
-    if (buttonNum >= NB_BUTTONS) return;
-    if (event->type == ButtonRelease) type = 1;
-    else
-    {  /* Check if double-click */
-	prevTime = lastClickTime[buttonNum];
-	lastClickTime[buttonNum] = event->time;
-	if (event->time - prevTime < dblclick_time)
-	{
-	    WND * wndPtr;
-	    CLASS * classPtr;
-	    if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return;
-	    if (!(classPtr = CLASS_FindClassPtr( wndPtr->hClass ))) return;
-	    type = (classPtr->wc.style & CS_DBLCLKS) ? 2 : 0;
-	}
-	else type = 0;
-    }	
-    
+    if (buttonNum >= NB_BUTTONS) return;    
     winHasCursor = event->window;
-
-    hardware_event( hwnd, messages[type][buttonNum],
-		    EVENT_XStateToKeyState( event->state ),
-		    (event->x & 0xffff) | (event->y << 16),
-		    event->x & 0xffff, event->y & 0xffff,
+    hardware_event( hwnd, messages[buttonNum],
+		    EVENT_XStateToKeyState( event->state ), 0L,
+		    event->x_root & 0xffff, event->y_root & 0xffff,
 		    event->time, 0 );		    
 }
 
 
 /***********************************************************************
- *           EVENT_structure
- *
- * Handle a X StructureNotify event
+ *           EVENT_ButtonRelease
  */
-static void EVENT_structure( Widget w, int hwnd, XEvent *event, 
- 			     Boolean *cont_dispatch )
+static void EVENT_ButtonRelease( HWND hwnd, XButtonEvent *event )
 {
-    MSG msg;
-    
-    msg.hwnd = hwnd;
-    msg.time = GetTickCount();
-    msg.pt.x = 0;
-    msg.pt.y = 0;
+    static const WORD messages[NB_BUTTONS] = 
+        { WM_LBUTTONUP, WM_MBUTTONUP, WM_RBUTTONUP };
+    int buttonNum = event->button - 1;
 
-    switch(event->type)
-    {
-      case ConfigureNotify:
-	{
-	    HANDLE handle;
-	    NCCALCSIZE_PARAMS *params;	    
-	    XConfigureEvent * evt = (XConfigureEvent *)event;
-	    WND * wndPtr = WIN_FindWndPtr( hwnd );
-	    if (!wndPtr) return;
-	    wndPtr->rectWindow.left   = evt->x;
-	    wndPtr->rectWindow.top    = evt->y;
-	    wndPtr->rectWindow.right  = evt->x + evt->width;
-	    wndPtr->rectWindow.bottom = evt->y + evt->height;
+    if (buttonNum >= NB_BUTTONS) return;    
+    winHasCursor = event->window;
+    hardware_event( hwnd, messages[buttonNum],
+		    EVENT_XStateToKeyState( event->state ), 0L,
+		    event->x_root & 0xffff, event->y_root & 0xffff,
+		    event->time, 0 );		    
+}
 
-	      /* Send WM_NCCALCSIZE message */
-	    handle = GlobalAlloc( GMEM_MOVEABLE, sizeof(*params) );
-	    params = (NCCALCSIZE_PARAMS *)GlobalLock( handle );
-	    params->rgrc[0] = wndPtr->rectWindow;
-	    params->lppos   = NULL;  /* Should be WINDOWPOS struct */
-	    SendMessage( hwnd, WM_NCCALCSIZE, FALSE, params );
-	    wndPtr->rectClient = params->rgrc[0];
-	    PostMessage( hwnd, WM_MOVE, 0,
-		             MAKELONG( wndPtr->rectClient.left,
-				       wndPtr->rectClient.top ));
-	    PostMessage( hwnd, WM_SIZE, SIZE_RESTORED,
-		   MAKELONG(wndPtr->rectClient.right-wndPtr->rectClient.left,
-			    wndPtr->rectClient.bottom-wndPtr->rectClient.top));
-	    GlobalUnlock( handle );
-	    GlobalFree( handle );
-	}
-	break;
-	
-    }    
+
+/***********************************************************************
+ *           EVENT_ConfigureNotify
+ */
+static void EVENT_ConfigureNotify( HWND hwnd, XConfigureEvent *event )
+{
+    HANDLE handle;
+    NCCALCSIZE_PARAMS *params;	    
+    WND * wndPtr = WIN_FindWndPtr( hwnd );
+    if (!wndPtr) return;
+    wndPtr->rectWindow.left   = event->x;
+    wndPtr->rectWindow.top    = event->y;
+    wndPtr->rectWindow.right  = event->x + event->width;
+    wndPtr->rectWindow.bottom = event->y + event->height;
+
+      /* Send WM_NCCALCSIZE message */
+    handle = GlobalAlloc( GMEM_MOVEABLE, sizeof(*params) );
+    params = (NCCALCSIZE_PARAMS *)GlobalLock( handle );
+    params->rgrc[0] = wndPtr->rectWindow;
+    params->lppos   = NULL;  /* Should be WINDOWPOS struct */
+    SendMessage( hwnd, WM_NCCALCSIZE, FALSE, (LPARAM)params );
+    wndPtr->rectClient = params->rgrc[0];
+    PostMessage( hwnd, WM_MOVE, 0,
+		 MAKELONG( wndPtr->rectClient.left, wndPtr->rectClient.top ));
+    PostMessage( hwnd, WM_SIZE, SIZE_RESTORED,
+		 MAKELONG( wndPtr->rectClient.right-wndPtr->rectClient.left,
+			   wndPtr->rectClient.bottom-wndPtr->rectClient.top) );
+    GlobalUnlock( handle );
+    GlobalFree( handle );
 }
 
 
 /**********************************************************************
- *              EVENT_focus_change
- *
- * Handle an X FocusChange event
+ *              EVENT_FocusIn
  */
-static void EVENT_focus_change( Widget w, int hwnd, XEvent *event, 
-			       Boolean *cont_dispatch )
+static void EVENT_FocusIn( HWND hwnd, XFocusChangeEvent *event )
 {
-    switch(event->type)
-    {
-      case FocusIn:
-	{
-	    PostMessage( hwnd, WM_SETFOCUS, hwnd, 0 );
-	    hWndFocus = hwnd;
-	}
-	break;
-	
-      case FocusOut:
-	{
-	    if (hWndFocus)
-	    {
-		PostMessage( hwnd, WM_KILLFOCUS, hwnd, 0 );
-		hWndFocus = 0;
-	    }
-	}
-    }    
+    PostMessage( hwnd, WM_SETFOCUS, hwnd, 0 );
+    hWndFocus = hwnd;
 }
 
 
 /**********************************************************************
- *              EVENT_enter_notify
- *
- * Handle an X EnterNotify event
+ *              EVENT_FocusOut
  */
-static void EVENT_enter_notify( Widget w, int hwnd, XCrossingEvent *event, 
-			       Boolean *cont_dispatch )
+static void EVENT_FocusOut( HWND hwnd, XFocusChangeEvent *event )
+{
+    if (hWndFocus)
+    {
+	PostMessage( hwnd, WM_KILLFOCUS, hwnd, 0 );
+	hWndFocus = 0;
+    }
+}
+
+
+/**********************************************************************
+ *              EVENT_EnterNotify
+ */
+static void EVENT_EnterNotify( HWND hwnd, XCrossingEvent *event )
 {
     if (captureWnd != 0) return;
-
     winHasCursor = event->window;
-
-    switch(event->type)
-    {
-      case EnterNotify:
-	PostMessage( hwnd, WM_SETCURSOR, hwnd, 0 );
-	break;
-    }    
+    PostMessage( hwnd, WM_SETCURSOR, hwnd, 0 );
 }
 
 
@@ -604,15 +477,9 @@
     if (wnd_p == NULL)
 	return 0;
     
-#ifdef USE_XLIB
-    rv = XGrabPointer(XT_display, wnd_p->window, False, 
+    rv = XGrabPointer(display, wnd_p->window, False, 
 		      ButtonPressMask | ButtonReleaseMask | ButtonMotionMask,
 		      GrabModeAsync, GrabModeSync, None, None, CurrentTime);
-#else
-    rv = XtGrabPointer(wnd_p->winWidget, False, 
-		       ButtonPressMask | ButtonReleaseMask | ButtonMotionMask,
-		       GrabModeAsync, GrabModeSync, None, None, CurrentTime);
-#endif
 
     if (rv == GrabSuccess)
     {
@@ -637,12 +504,7 @@
     if (wnd_p == NULL)
 	return;
     
-#ifdef USE_XLIB
-    XUngrabPointer( XT_display, CurrentTime );
-#else
-    XtUngrabPointer(wnd_p->winWidget, CurrentTime);
-#endif
-
+    XUngrabPointer( display, CurrentTime );
     captureWnd = 0;
 }
 
@@ -653,24 +515,3 @@
 {
     return captureWnd;
 }
- 
-/**********************************************************************
- *		SetDoubleClickTime  (USER.20)
- */
-void SetDoubleClickTime (WORD interval)
-{
-	if (interval == 0)
-		dblclick_time = 500;
-	else
-		dblclick_time = interval;
-}		
-
-/**********************************************************************
- *		GetDoubleClickTime  (USER.21)
- */
-WORD GetDoubleClickTime ()
-{
-	return ((WORD)dblclick_time);
-}		
-
-
diff --git a/windows/focus.c b/windows/focus.c
index 639b2df..a2d2b8d 100644
--- a/windows/focus.c
+++ b/windows/focus.c
@@ -27,13 +27,19 @@
 
     if (hwnd == 0)
     {
-	XSetInputFocus(XT_display, None, RevertToPointerRoot, CurrentTime);
+	XSetInputFocus(display, None, RevertToPointerRoot, CurrentTime);
     }
     else
     {
+	XWindowAttributes win_attr;
 	wndPtr = WIN_FindWndPtr(hwnd);
-	XSetInputFocus(XT_display, wndPtr->window,
-		       RevertToParent, CurrentTime);
+	
+	if (XGetWindowAttributes( display, wndPtr->window, &win_attr ))
+	{
+	    if (win_attr.map_state == IsViewable)
+		XSetInputFocus(display, wndPtr->window,
+			       RevertToParent, CurrentTime);
+	}
     }
 
     return hWndPrevFocus;
diff --git a/windows/graphics.c b/windows/graphics.c
index 9fd5d27..c5e41b5 100644
--- a/windows/graphics.c
+++ b/windows/graphics.c
@@ -7,7 +7,9 @@
 static char Copyright[] = "Copyright  Alexandre Julliard, 1993";
 
 #include <math.h>
+#include <stdlib.h>
 #include <X11/Xlib.h>
+#include <X11/Xutil.h>
 #ifndef PI
 #define PI M_PI
 #endif
@@ -413,48 +415,44 @@
 
 
 /**********************************************************************
- *          Line  (Not a MSWin Call)
- */
-void Line(HDC hDC, int X1, int Y1, int X2, int Y2)
-{
-MoveTo(hDC, X1, Y1);
-LineTo(hDC, X2, Y2);
-}
-
-
-/**********************************************************************
  *          DrawReliefRect  (Not a MSWin Call)
  */
- void DrawReliefRect(HDC hDC, RECT rect, int ThickNess, int Mode)
+void DrawReliefRect( HDC hdc, RECT rect, int thickness, BOOL pressed )
 {
-HPEN   hWHITEPen;
-HPEN   hDKGRAYPen;
-HPEN   hOldPen;
-int    OldColor;
-rect.right--;  rect.bottom--;
-hDKGRAYPen = CreatePen(PS_SOLID, 1, 0x00808080L);
-hWHITEPen = GetStockObject(WHITE_PEN);
-hOldPen = SelectObject(hDC, hWHITEPen);
-while(ThickNess > 0) {
-    if (Mode == 0)
-	SelectObject(hDC, hWHITEPen);
-    else
-	SelectObject(hDC, hDKGRAYPen);
-    Line(hDC, rect.left, rect.top, rect.right, rect.top);
-    Line(hDC, rect.left, rect.top, rect.left, rect.bottom + 1);
-    if (Mode == 0)
-	SelectObject(hDC, hDKGRAYPen);
-    else
-	SelectObject(hDC, hWHITEPen);
-    Line(hDC, rect.right, rect.bottom, rect.left, rect.bottom);
-    Line(hDC, rect.right, rect.bottom, rect.right, rect.top - 1);
-    InflateRect(&rect, -1, -1);
-    ThickNess--;
+    HBRUSH hbrushOld, hbrushShadow, hbrushHighlight;
+    int i;
+
+    hbrushShadow = CreateSolidBrush( GetSysColor(COLOR_BTNSHADOW) );
+    hbrushHighlight = CreateSolidBrush( GetSysColor(COLOR_BTNHIGHLIGHT) );
+
+    if (pressed) hbrushOld = SelectObject( hdc, hbrushShadow );
+    else hbrushOld = SelectObject( hdc, hbrushHighlight );
+
+    for (i = 0; i < thickness; i++)
+    {
+	PatBlt( hdc, rect.left + i, rect.top,
+	        1, rect.bottom - rect.top - i, PATCOPY );
+	PatBlt( hdc, rect.left, rect.top + i,
+	        rect.right - rect.left - i, 1, PATCOPY );
     }
-SelectObject(hDC, hOldPen);
-DeleteObject(hDKGRAYPen);
+
+    if (pressed) hbrushOld = SelectObject( hdc, hbrushHighlight );
+    else hbrushOld = SelectObject( hdc, hbrushShadow );
+
+    for (i = 0; i < thickness; i++)
+    {
+	PatBlt( hdc, rect.right - i - 1, rect.top + i,
+	        1, rect.bottom - rect.top - i, PATCOPY );
+	PatBlt( hdc, rect.left + i, rect.bottom - i - 1,
+	        rect.right - rect.left - i, 1, PATCOPY );
+    }
+
+    SelectObject( hdc, hbrushOld );
+    DeleteObject( hbrushShadow );
+    DeleteObject( hbrushHighlight );
 }
 
+
 /**********************************************************************
  *          Polyline  (GDI.37)
  */
diff --git a/windows/message.c b/windows/message.c
index 09b49e7..3782a27 100644
--- a/windows/message.c
+++ b/windows/message.c
@@ -17,15 +17,15 @@
 
 #include "message.h"
 #include "win.h"
-
+#include "wineopts.h"
+#include "sysmetrics.h"
 
 #define MAX_QUEUE_SIZE   120  /* Max. size of a message queue */
 
-extern BOOL TIMER_CheckTimer( DWORD *next );    /* timer.c */
-
-extern Display * XT_display;
-extern Screen * XT_screen;
-extern XtAppContext XT_app_context;
+extern BOOL TIMER_CheckTimer( DWORD *next );      /* timer.c */
+extern void EVENT_ProcessEvent( XEvent *event );  /* event.c */
+  
+extern Display * display;
 
   /* System message queue (for hardware events) */
 static HANDLE hmemSysMsgQueue = 0;
@@ -35,6 +35,10 @@
 static HANDLE hmemAppMsgQueue = 0;
 static MESSAGEQUEUE * appMsgQueue = NULL;
 
+  /* Double-click time */
+static int doubleClickSpeed = 452;
+
+
 /***********************************************************************
  *           MSG_CreateMsgQueue
  *
@@ -78,7 +82,8 @@
 /***********************************************************************
  *           MSG_CreateSysMsgQueue
  *
- * Create the system message queue. Must be called only once.
+ * Create the system message queue, and set the double-click speed.
+ * Must be called only once.
  */
 BOOL MSG_CreateSysMsgQueue( int size )
 {
@@ -86,6 +91,7 @@
     else if (size <= 0) size = 1;
     if (!(hmemSysMsgQueue = MSG_CreateMsgQueue( size ))) return FALSE;
     sysMsgQueue = (MESSAGEQUEUE *) GlobalLock( hmemSysMsgQueue );
+    doubleClickSpeed = GetProfileInt( "windows", "DoubleClickSpeed", 452 );
     return TRUE;
 }
 
@@ -99,6 +105,8 @@
 {
     int pos;
   
+    SpyMessage(msg->hwnd, msg->message, msg->wParam, msg->lParam);
+    
     if (!msgQueue) return FALSE;
     pos = msgQueue->nextFreeMessage;
 
@@ -184,6 +192,93 @@
 
 
 /***********************************************************************
+ *           MSG_TranslateMouseMsg
+ *
+ * Translate an mouse hardware event into a real mouse message.
+ * Actions performed:
+ * - Translate button down messages in double-clicks.
+ * - Send the WM_NCHITTEST message to find where the cursor is.
+ * - Translate the message into a non-client message, or translate
+ *   the coordinates to client coordinates.
+ */
+static void MSG_TranslateMouseMsg( MSG *msg )
+{
+    static DWORD lastClickTime = 0;
+    static WORD  lastClickMsg = 0;
+    static POINT lastClickPos = { 0, 0 };
+
+    LONG hittest_result = SendMessage( msg->hwnd, WM_NCHITTEST, 0,
+				       MAKELONG( msg->pt.x, msg->pt.y ) );
+
+    if ((msg->message == WM_LBUTTONDOWN) ||
+        (msg->message == WM_RBUTTONDOWN) ||
+        (msg->message == WM_MBUTTONDOWN))
+    {
+	BOOL dbl_click = FALSE;
+
+	  /* Check for double-click */
+
+	if ((msg->message == lastClickMsg) &&
+	    (msg->time - lastClickTime < doubleClickSpeed) &&
+	    (abs(msg->pt.x - lastClickPos.x) < SYSMETRICS_CXDOUBLECLK/2) &&
+	    (abs(msg->pt.y - lastClickPos.y) < SYSMETRICS_CYDOUBLECLK/2))
+	    dbl_click = TRUE;
+
+	if (dbl_click && (hittest_result == HTCLIENT))
+	{
+	    /* Check whether window wants the double click message. */
+	    WND * wndPtr = WIN_FindWndPtr( msg->hwnd );
+	    if (!wndPtr || !(wndPtr->flags & WIN_DOUBLE_CLICKS))
+		dbl_click = FALSE;
+	}
+
+	if (dbl_click) switch(msg->message)
+	{
+	    case WM_LBUTTONDOWN: msg->message = WM_LBUTTONDBLCLK; break;
+	    case WM_RBUTTONDOWN: msg->message = WM_RBUTTONDBLCLK; break;
+	    case WM_MBUTTONDOWN: msg->message = WM_MBUTTONDBLCLK; break;
+	}
+
+	lastClickTime = msg->time;
+	lastClickMsg  = msg->message;
+	lastClickPos  = msg->pt;
+    }
+
+    msg->lParam = MAKELONG( msg->pt.x, msg->pt.y );
+    if (hittest_result == HTCLIENT)
+    {
+	ScreenToClient( msg->hwnd, (LPPOINT)&msg->lParam );
+    }
+    else
+    {
+	msg->wParam = hittest_result;
+	msg->message += WM_NCLBUTTONDOWN - WM_LBUTTONDOWN;
+    }
+}
+
+
+/**********************************************************************
+ *		SetDoubleClickTime  (USER.20)
+ */
+void SetDoubleClickTime( WORD interval )
+{
+    if (interval == 0)
+	doubleClickSpeed = 500;
+    else
+	doubleClickSpeed = interval;
+}		
+
+
+/**********************************************************************
+ *		GetDoubleClickTime  (USER.21)
+ */
+WORD GetDoubleClickTime()
+{
+	return (WORD)doubleClickSpeed;
+}		
+
+
+/***********************************************************************
  *           MSG_IncPaintCount
  */
 void MSG_IncPaintCount( HANDLE hQueue )
@@ -233,6 +328,7 @@
  *           hardware_event
  *
  * Add an event to the system message queue.
+ * Note: the position is in screen coordinates.
  */
 void hardware_event( HWND hwnd, WORD message, WORD wParam, LONG lParam,
 		     WORD xPos, WORD yPos, DWORD time, DWORD extraInfo )
@@ -325,24 +421,6 @@
 }
 
 
-#ifndef USE_XLIB
-static XtIntervalId xt_timer = 0;
-
-/***********************************************************************
- *           MSG_TimerCallback
- */
-static void MSG_TimerCallback( XtPointer data, XtIntervalId * xtid )
-{
-    DWORD nextExp;
-    TIMER_CheckTimer( &nextExp );
-    if (nextExp != (DWORD)-1)
-	xt_timer = XtAppAddTimeOut( XT_app_context, nextExp,
-				    MSG_TimerCallback, NULL );
-    else xt_timer = 0;
-}
-#endif  /* USE_XLIB */
-
-
 /***********************************************************************
  *           MSG_PeekMessage
  */
@@ -351,9 +429,7 @@
 {
     int pos, mask;
     DWORD nextExp;  /* Next timer expiration time */
-#ifdef USE_XLIB
     XEvent event;
-#endif
 
     if (first || last)
     {
@@ -366,16 +442,11 @@
     }
     else mask = QS_MOUSE | QS_KEY | QS_POSTMESSAGE | QS_TIMER | QS_PAINT;
 
-#ifdef USE_XLIB
-    while (XPending( XT_display ))
+    while (XPending( display ))
     {
-	XNextEvent( XT_display, &event );
+	XNextEvent( display, &event );
 	EVENT_ProcessEvent( &event );
     }    
-#else
-    while (XtAppPending( XT_app_context ))
-	XtAppProcessEvent( XT_app_context, XtIMAll );
-#endif
 
     while(1)
     {    
@@ -432,6 +503,8 @@
 	    msgQueue->GetMessageExtraInfoVal = qmsg->extraInfo;
 
 	    if (flags & PM_REMOVE) MSG_RemoveMsg( sysMsgQueue, pos );
+	    if ((msg->message >= WM_MOUSEFIRST) &&
+		(msg->message <= WM_MOUSELAST)) MSG_TranslateMouseMsg( msg );
 	    break;
 	}
 
@@ -447,26 +520,16 @@
 
 	  /* Finally handle WM_TIMER messages */
 	if ((msgQueue->status & QS_TIMER) && (mask & QS_TIMER))
-	{
-	    BOOL posted = TIMER_CheckTimer( &nextExp );
-#ifndef USE_XLIB
-	    if (xt_timer) XtRemoveTimeOut( xt_timer );
-	    if (nextExp != (DWORD)-1)
-		xt_timer = XtAppAddTimeOut( XT_app_context, nextExp,
-					    MSG_TimerCallback, NULL );
-	    else xt_timer = 0;
-#endif
-	    if (posted) continue;  /* Restart the whole thing */
-	}
+	    if (TIMER_CheckTimer( &nextExp ))
+		continue;  /* Restart the whole search */
 
 	  /* Wait until something happens */
 	if (peek) return FALSE;
-#ifdef USE_XLIB
-	if (!XPending( XT_display ) && (nextExp != -1))
+	if (!XPending( display ) && (nextExp != -1))
 	{
 	    fd_set read_set;
 	    struct timeval timeout;
-	    int fd = ConnectionNumber(XT_display);
+	    int fd = ConnectionNumber(display);
 	    FD_ZERO( &read_set );
 	    FD_SET( fd, &read_set );
 	    timeout.tv_sec = nextExp / 1000;
@@ -474,11 +537,8 @@
 	    if (select( fd+1, &read_set, NULL, NULL, &timeout ) != 1)
 		continue;  /* On timeout or error, restart from the start */
 	}
-	XNextEvent( XT_display, &event );
+	XNextEvent( display, &event );
 	EVENT_ProcessEvent( &event );
-#else       
-	XtAppProcessEvent( XT_app_context, XtIMAll );
-#endif	
     }
 
       /* We got a message */
@@ -529,7 +589,11 @@
  */
 LONG SendMessage( HWND hwnd, WORD msg, WORD wParam, LONG lParam )
 {
-    WND * wndPtr = WIN_FindWndPtr( hwnd );
+    WND * wndPtr;
+
+    SpyMessage(hwnd, msg, wParam, lParam);
+    
+    wndPtr = WIN_FindWndPtr( hwnd );
     if (!wndPtr) return 0;
     return CallWindowProc( wndPtr->lpfnWndProc, hwnd, msg, wParam, lParam );
 }
@@ -564,7 +628,7 @@
     int painting;
     
 #ifdef DEBUG_MSG
-    printf( "Dispatch message hwnd=%08x msg=%d w=%d l=%d time=%u pt=%d,%d\n",
+    printf( "Dispatch message hwnd=%08x msg=0x%x w=%d l=%d time=%u pt=%d,%d\n",
 	    msg->hwnd, msg->message, msg->wParam, msg->lParam, 
 	    msg->time, msg->pt.x, msg->pt.y );
 #endif
@@ -587,7 +651,7 @@
     if (painting && (wndPtr->flags & WIN_NEEDS_BEGINPAINT))
     {
 #ifdef DEBUG_WIN
-	printf( "BeginPaint not called on WM_PAINT!\n" );
+	printf( "BeginPaint not called on WM_PAINT for hwnd %d!\n", msg->hwnd);
 #endif
 	wndPtr->flags &= ~WIN_NEEDS_BEGINPAINT;
     }
diff --git a/windows/nonclient.c b/windows/nonclient.c
new file mode 100644
index 0000000..79f5893
--- /dev/null
+++ b/windows/nonclient.c
@@ -0,0 +1,461 @@
+/*
+ * Non-client area window functions
+ *
+ * Copyright 1994 Alexandre Julliard
+ */
+
+static char Copyright[] = "Copyright  Alexandre Julliard, 1994";
+
+#include "win.h"
+#include "sysmetrics.h"
+
+
+/***********************************************************************
+ *           NC_AdjustRect
+ *
+ * Compute the size of the window rectangle from the size of the
+ * client rectangle.
+ */
+static void NC_AdjustRect( LPRECT rect, DWORD style, BOOL menu, DWORD exStyle )
+{
+    if ((style & WS_DLGFRAME) && 
+	((exStyle & WS_EX_DLGMODALFRAME) || !(style & WS_BORDER)))
+	InflateRect( rect, SYSMETRICS_CXDLGFRAME, SYSMETRICS_CYDLGFRAME );
+    else if (style & WS_THICKFRAME)
+	InflateRect( rect, SYSMETRICS_CXFRAME, SYSMETRICS_CYFRAME );
+
+    if (style & WS_BORDER)
+	InflateRect( rect, SYSMETRICS_CXBORDER, SYSMETRICS_CYBORDER );
+
+    if ((style & WS_CAPTION) == WS_CAPTION)
+	rect->top -= SYSMETRICS_CYCAPTION - 1;
+
+    if (menu) rect->top -= SYSMETRICS_CYMENU + 1;
+
+    if (style & WS_VSCROLL) rect->right  += SYSMETRICS_CXVSCROLL;
+    if (style & WS_HSCROLL) rect->bottom += SYSMETRICS_CYHSCROLL;
+}
+
+
+/***********************************************************************
+ *           AdjustWindowRect    (USER.102)
+ */
+void AdjustWindowRect( LPRECT rect, DWORD style, BOOL menu )
+{
+    AdjustWindowRectEx( rect, style, menu, 0 );
+}
+
+
+/***********************************************************************
+ *           AdjustWindowRectEx    (USER.454)
+ */
+void AdjustWindowRectEx( LPRECT rect, DWORD style, BOOL menu, DWORD exStyle )
+{
+      /* Correct the window style */
+
+    if (!(style & (WS_POPUP | WS_CHILD)))  /* Overlapped window */
+	style |= WS_CAPTION;
+    if (exStyle & WS_EX_DLGMODALFRAME) style &= ~WS_THICKFRAME;
+
+#ifdef DEBUG_NONCLIENT
+    printf( "AdjustWindowRectEx: (%d,%d)-(%d,%d) %08x %d %08x\n",
+      rect->left, rect->top, rect->right, rect->bottom, style, menu, exStyle );
+#endif
+
+    NC_AdjustRect( rect, style, menu, exStyle );
+}
+
+
+/***********************************************************************
+ *           NC_HandleNCCalcSize
+ *
+ * Handle a WM_NCCALCSIZE message. Called from DefWindowProc().
+ */
+LONG NC_HandleNCCalcSize( HWND hwnd, NCCALCSIZE_PARAMS *params )
+{
+    RECT tmpRect = { 0, 0, 0, 0 };
+    BOOL hasMenu;
+    WND *wndPtr = WIN_FindWndPtr( hwnd );
+    
+    if (!wndPtr) return 0;
+
+    hasMenu = (!(wndPtr->dwStyle & WS_CHILD)) && (wndPtr->wIDmenu != 0);
+
+    NC_AdjustRect( &tmpRect, wndPtr->dwStyle, hasMenu, wndPtr->dwExStyle );
+    
+    params->rgrc[0].left   -= tmpRect.left;
+    params->rgrc[0].top    -= tmpRect.top;
+    params->rgrc[0].right  -= tmpRect.right;
+    params->rgrc[0].bottom -= tmpRect.bottom;
+    return 0;
+}
+
+
+/***********************************************************************
+ *           NC_HandleNCHitTest
+ *
+ * Handle a WM_NCHITTEST message. Called from DefWindowProc().
+ */
+LONG NC_HandleNCHitTest( HWND hwnd, POINT pt )
+{
+    RECT rect;
+    WND *wndPtr = WIN_FindWndPtr( hwnd );
+    if (!wndPtr) return HTERROR;
+
+#ifdef DEBUG_NONCLIENT
+    printf( "NC_HandleNCHitTest: hwnd=%x pt=%d,%d\n", hwnd, pt.x, pt.y );
+#endif
+
+    if (hwnd == GetCapture()) return HTCLIENT;
+    GetWindowRect( hwnd, &rect );
+    if (!PtInRect( &rect, pt )) return HTNOWHERE;
+    ScreenToClient( hwnd, &pt );
+    GetClientRect( hwnd, &rect );
+    
+    if (PtInRect( &rect, pt )) return HTCLIENT;
+
+      /* Check vertical scroll bar */
+    if (wndPtr->dwStyle & WS_VSCROLL)
+    {
+	rect.right += SYSMETRICS_CXVSCROLL;
+	if (PtInRect( &rect, pt )) return HTVSCROLL;
+    }
+
+      /* Check horizontal scroll bar */
+    if (wndPtr->dwStyle & WS_HSCROLL)
+    {
+	rect.bottom += SYSMETRICS_CYHSCROLL;
+	if (PtInRect( &rect, pt ))
+	{
+	      /* Check size box */
+	    if ((wndPtr->dwStyle & WS_VSCROLL) &&
+		(pt.x >= rect.right - SYSMETRICS_CXVSCROLL)) return HTSIZE;
+	    return HTHSCROLL;
+	}
+    }
+
+      /* Check menu */
+    if ((!(wndPtr->dwStyle & WS_CHILD)) && (wndPtr->wIDmenu != 0))
+    {
+	rect.top -= SYSMETRICS_CYMENU + 1;
+	if (PtInRect( &rect, pt )) return HTMENU;
+    }
+
+      /* Check caption */
+    if ((wndPtr->dwStyle & WS_CAPTION) == WS_CAPTION)
+    {
+	rect.top -= SYSMETRICS_CYCAPTION - 1;
+	if (PtInRect( &rect, pt ))
+	{
+	      /* Check system menu */
+	    if ((wndPtr->dwStyle & WS_SYSMENU) && (pt.x <= SYSMETRICS_CXSIZE))
+		return HTSYSMENU;
+	      /* Check maximize box */
+	    if (wndPtr->dwStyle & WS_MAXIMIZEBOX)
+		rect.right -= SYSMETRICS_CXSIZE + 1;
+	    if (pt.x >= rect.right) return HTMAXBUTTON;
+	      /* Check minimize box */
+	    if (wndPtr->dwStyle & WS_MINIMIZEBOX)
+		rect.right -= SYSMETRICS_CXSIZE + 1;
+	    if (pt.x >= rect.right) return HTMINBUTTON;
+	    return HTCAPTION;
+	}
+    }
+    
+      /* Check non-sizing border */
+    if (!(wndPtr->dwStyle & WS_THICKFRAME) ||
+	((wndPtr->dwStyle & (WS_DLGFRAME | WS_BORDER)) == WS_DLGFRAME))
+	return HTBORDER;
+
+      /* Check top sizing border */
+    if (pt.y < rect.top)
+    {
+	if (pt.x < rect.left+SYSMETRICS_CXSIZE) return HTTOPLEFT;
+	if (pt.x >= rect.right-SYSMETRICS_CXSIZE) return HTTOPRIGHT;
+	return HTTOP;
+    }
+
+      /* Check bottom sizing border */
+    if (pt.y >= rect.bottom)
+    {
+	if (pt.x < rect.left+SYSMETRICS_CXSIZE) return HTBOTTOMLEFT;
+	if (pt.x >= rect.right-SYSMETRICS_CXSIZE) return HTBOTTOMRIGHT;
+	return HTBOTTOM;
+    }
+    
+      /* Check left sizing border */
+    if (pt.x < rect.left)
+    {
+	if (pt.y < rect.top+SYSMETRICS_CYSIZE) return HTTOPLEFT;
+	if (pt.y >= rect.bottom-SYSMETRICS_CYSIZE) return HTBOTTOMLEFT;
+	return HTLEFT;
+    }
+
+      /* Check right sizing border */
+    if (pt.x >= rect.right)
+    {
+	if (pt.y < rect.top+SYSMETRICS_CYSIZE) return HTTOPRIGHT;
+	if (pt.y >= rect.bottom-SYSMETRICS_CYSIZE) return HTBOTTOMRIGHT;
+	return HTRIGHT;
+    }
+
+      /* Should never get here */
+    return HTERROR;
+}
+
+
+/***********************************************************************
+ *           NC_DrawFrame
+ *
+ * Draw a window frame inside the given rectangle, and update the rectangle.
+ * The correct pen and brush must be selected in the DC.
+ */
+static void NC_DrawFrame( HDC hdc, RECT *rect, BOOL dlgFrame )
+{
+    short width, height, tmp;
+
+    if (dlgFrame)
+    {
+	width = SYSMETRICS_CXDLGFRAME;
+	height = SYSMETRICS_CYDLGFRAME;
+    }
+    else
+    {
+	width = SYSMETRICS_CXFRAME - 1;
+	height = SYSMETRICS_CYFRAME - 1;
+    }
+
+      /* Draw frame */
+    PatBlt( hdc, rect->left, rect->top,
+	    rect->right - rect->left, height, PATCOPY );
+    PatBlt( hdc, rect->left, rect->top,
+	    width, rect->bottom - rect->top, PATCOPY );
+    PatBlt( hdc, rect->left, rect->bottom,
+	    rect->right - rect->left, -height, PATCOPY );
+    PatBlt( hdc, rect->right, rect->top,
+	    -width, rect->bottom - rect->top, PATCOPY );
+
+    if (dlgFrame)
+    {
+	InflateRect( rect, -width, -height );
+	return;
+    }
+    
+      /* Draw inner rectangle */
+    MoveTo( hdc, rect->left+width, rect->top+height );
+    LineTo( hdc, rect->right-width-1, rect->top+height );
+    LineTo( hdc, rect->right-width-1, rect->bottom-height-1 );
+    LineTo( hdc, rect->left+width, rect->bottom-height-1 );
+    LineTo( hdc, rect->left+width, rect->top+height );
+
+      /* Draw the decorations */
+    tmp = rect->top + SYSMETRICS_CYFRAME + SYSMETRICS_CYSIZE;
+    MoveTo( hdc, rect->left, tmp);
+    LineTo( hdc, rect->left+width, tmp );
+    MoveTo( hdc, rect->right-width-1, tmp );
+    LineTo( hdc, rect->right-1, tmp );
+
+    tmp = rect->bottom - 1 - SYSMETRICS_CYFRAME - SYSMETRICS_CYSIZE;
+    MoveTo( hdc, rect->left, tmp );
+    LineTo( hdc, rect->left+width, tmp );
+    MoveTo( hdc, rect->right-width-1, tmp );
+    LineTo( hdc, rect->right-1, tmp );
+
+    tmp = rect->left + SYSMETRICS_CXFRAME + SYSMETRICS_CXSIZE;
+    MoveTo( hdc, tmp, rect->top );
+    LineTo( hdc, tmp, rect->top+height );
+    MoveTo( hdc, tmp, rect->bottom-height-1 );
+    LineTo( hdc, tmp, rect->bottom-1 );
+
+    tmp = rect->right - 1 - SYSMETRICS_CXFRAME - SYSMETRICS_CYSIZE;
+    MoveTo( hdc, tmp, rect->top );
+    LineTo( hdc, tmp, rect->top+height );
+    MoveTo( hdc, tmp, rect->bottom-height-1 );
+    LineTo( hdc, tmp, rect->bottom-1 );
+
+    InflateRect( rect, -width-1, -height-1 );
+}
+
+
+/***********************************************************************
+ *           NC_DrawCaption
+ *
+ * Draw the window caption.
+ * The correct pen for the window frame must be selected in the DC.
+ */
+static void NC_DrawCaption( HDC hdc, RECT *rect, HWND hwnd, DWORD style )
+{
+    RECT r;
+    HBRUSH hbrushCaption, hbrushButtons;
+    char buffer[256];
+
+    hbrushButtons = CreateSolidBrush( GetSysColor( COLOR_BTNFACE ) );
+    hbrushCaption = CreateSolidBrush( GetSysColor( COLOR_ACTIVECAPTION ) );
+
+    MoveTo( hdc, rect->left, rect->bottom );
+    LineTo( hdc, rect->right-1, rect->bottom );
+
+    /* We should probably use OEM bitmaps (OBM_*) here */
+    if (style & WS_SYSMENU)
+    {
+	r = *rect;
+	r.right = r.left + SYSMETRICS_CYSIZE;
+	FillRect( hdc, &r, hbrushButtons );
+	MoveTo( hdc, r.right, r.top );
+	LineTo( hdc, r.right, r.bottom );
+	rect->left += SYSMETRICS_CXSIZE + 1;
+    }
+    if (style & WS_MAXIMIZEBOX)
+    {
+	r = *rect;
+	r.left = r.right - SYSMETRICS_CXSIZE;
+	FillRect( hdc, &r, hbrushButtons );
+	MoveTo( hdc, r.left-1, r.top );
+	LineTo( hdc, r.left-1, r.bottom );
+	DrawReliefRect( hdc, r, 2, 0 );
+	rect->right -= SYSMETRICS_CXSIZE + 1;
+    }
+    if (style & WS_MINIMIZEBOX)
+    {
+	r = *rect;
+	r.left = r.right - SYSMETRICS_CXSIZE;
+	FillRect( hdc, &r, hbrushButtons );
+	MoveTo( hdc, r.left-1, r.top );
+	LineTo( hdc, r.left-1, r.bottom );
+	DrawReliefRect( hdc, r, 2, 0 );
+	rect->right -= SYSMETRICS_CXSIZE + 1;
+    }
+
+    FillRect( hdc, rect, hbrushCaption );
+
+    if (GetWindowText( hwnd, buffer, 256 ))
+    {
+	SetTextColor( hdc, GetSysColor( COLOR_CAPTIONTEXT ) );
+	SetBkMode( hdc, TRANSPARENT );
+	DrawText( hdc, buffer, -1, rect,
+		 DT_SINGLELINE | DT_CENTER | DT_VCENTER );
+    }
+
+    DeleteObject( hbrushButtons );
+    DeleteObject( hbrushCaption );
+}
+
+
+/***********************************************************************
+ *           NC_HandleNCPaint
+ *
+ * Handle a WM_NCPAINT message. Called from DefWindowProc().
+ */
+LONG NC_HandleNCPaint( HWND hwnd, HRGN hrgn )
+{
+    HDC hdc;
+    RECT rect;
+    HBRUSH hbrushBorder = 0;
+    HPEN hpenFrame = 0;
+
+    WND *wndPtr = WIN_FindWndPtr( hwnd );
+
+#ifdef DEBUG_NONCLIENT
+    printf( "NC_HandleNCPaint: %d %d\n", hwnd, hrgn );
+#endif
+
+    if (!wndPtr || !hrgn) return 0;
+    if (!(wndPtr->dwStyle & (WS_BORDER | WS_DLGFRAME | WS_THICKFRAME)))
+	return 0;  /* Nothing to do! */
+
+    if (hrgn == 1) hdc = GetDCEx( hwnd, 0, DCX_CACHE | DCX_WINDOW );
+    else hdc = GetDCEx( hwnd, hrgn, DCX_CACHE | DCX_WINDOW | DCX_INTERSECTRGN);
+    if (!hdc) return 0;
+    if (ExcludeVisRect( hdc, wndPtr->rectClient.left-wndPtr->rectWindow.left,
+		        wndPtr->rectClient.top-wndPtr->rectWindow.top,
+		        wndPtr->rectClient.right-wndPtr->rectWindow.left,
+		        wndPtr->rectClient.bottom-wndPtr->rectWindow.top )
+	== NULLREGION)
+    {
+	ReleaseDC( hwnd, hdc );
+	return 0;
+    }
+
+    rect.top = rect.left = 0;
+    rect.right  = wndPtr->rectWindow.right - wndPtr->rectWindow.left;
+    rect.bottom = wndPtr->rectWindow.bottom - wndPtr->rectWindow.top;
+
+    hpenFrame = CreatePen( PS_SOLID, 1, GetSysColor(COLOR_WINDOWFRAME) );
+    SelectObject( hdc, hpenFrame );
+    hbrushBorder = CreateSolidBrush( GetSysColor(COLOR_ACTIVEBORDER) );
+    SelectObject( hdc, hbrushBorder );
+
+    if ((wndPtr->dwStyle & WS_BORDER) || (wndPtr->dwStyle & WS_DLGFRAME))
+    {
+	MoveTo( hdc, 0, 0 );
+	LineTo( hdc, rect.right-1, 0 );
+	LineTo( hdc, rect.right-1, rect.bottom-1 );
+	LineTo( hdc, 0, rect.bottom-1 );
+	LineTo( hdc, 0, 0 );
+	InflateRect( &rect, -1, -1 );
+    }
+
+    if ((wndPtr->dwStyle & WS_DLGFRAME) &&
+	((wndPtr->dwExStyle & WS_EX_DLGMODALFRAME) || 
+	 !(wndPtr->dwStyle & WS_BORDER))) NC_DrawFrame( hdc, &rect, TRUE );
+    else if (wndPtr->dwStyle & WS_THICKFRAME) NC_DrawFrame(hdc, &rect, FALSE);
+
+    if ((wndPtr->dwStyle & WS_CAPTION) == WS_CAPTION)
+    {
+	RECT r = rect;
+	rect.top += SYSMETRICS_CYSIZE + 1;
+	r.bottom = rect.top - 1;
+	if (wndPtr->dwExStyle & WS_EX_DLGMODALFRAME)
+	{
+	    HBRUSH hbrushWindow = CreateSolidBrush(GetSysColor(COLOR_WINDOW));
+	    HBRUSH hbrushOld = SelectObject( hdc, hbrushWindow );
+	    PatBlt( hdc, r.left, r.top, 1, r.bottom-r.top+1, PATCOPY );
+	    PatBlt( hdc, r.right-1, r.top, 1, r.bottom-r.top+1, PATCOPY );
+	    PatBlt( hdc, r.left, r.top, r.right-r.left, 1, PATCOPY );
+	    r.left++;
+	    r.right--;
+	    r.top++;
+	    SelectObject( hdc, hbrushOld );
+	    DeleteObject( hbrushWindow );
+	}
+	NC_DrawCaption( hdc, &r, hwnd, wndPtr->dwStyle );
+    }
+
+    if (wndPtr->dwStyle & (WS_VSCROLL | WS_HSCROLL))
+    {
+	HBRUSH hbrushScroll = CreateSolidBrush( GetSysColor(COLOR_SCROLLBAR) );
+	HBRUSH hbrushOld = SelectObject( hdc, hbrushScroll );
+	if (wndPtr->dwStyle & WS_VSCROLL)
+	    PatBlt( hdc, rect.right - SYSMETRICS_CXVSCROLL, rect.top,
+		    SYSMETRICS_CXVSCROLL, rect.bottom-rect.top, PATCOPY );
+	if (wndPtr->dwStyle & WS_HSCROLL)
+	    PatBlt( hdc, rect.left, rect.bottom - SYSMETRICS_CYHSCROLL,
+		    rect.right-rect.left, SYSMETRICS_CYHSCROLL, PATCOPY );
+	SelectObject( hdc, hbrushOld );
+	DeleteObject( hbrushScroll );
+    }    
+
+    ReleaseDC( hwnd, hdc );
+    if (hbrushBorder) DeleteObject( hbrushBorder );
+    if (hpenFrame) DeleteObject( hpenFrame );    
+    return 0;
+}
+
+
+/***********************************************************************
+ *           NC_HandleNCMouseMsg
+ *
+ * Handle a non-client mouse message. Called from DefWindowProc().
+ */
+LONG NC_HandleNCMouseMsg( HWND hwnd, WORD msg, WORD wParam, LONG lParam )
+{
+    switch(wParam)  /* Hit test code */
+    {
+    case HTSYSMENU:
+	if (msg == WM_NCLBUTTONDBLCLK)
+	    return SendMessage( hwnd, WM_SYSCOMMAND, SC_CLOSE, lParam );
+	break;
+    }
+    return 0;
+}
+
diff --git a/windows/painting.c b/windows/painting.c
index 5a8e60a..2aeaba2 100644
--- a/windows/painting.c
+++ b/windows/painting.c
@@ -21,9 +21,12 @@
  */
 HDC BeginPaint( HWND hwnd, LPPAINTSTRUCT lps ) 
 {
+    HRGN hrgnUpdate;
     WND * wndPtr = WIN_FindWndPtr( hwnd );
     if (!wndPtr) return 0;
 
+    hrgnUpdate = wndPtr->hrgnUpdate;  /* Save update region */
+
     if (!(lps->hdc = GetDCEx( hwnd, wndPtr->hrgnUpdate,
 			      DCX_INTERSECTRGN | DCX_USESTYLE ))) return 0;
     GetRgnBox( InquireVisRgn(lps->hdc), &lps->rcPaint );
@@ -35,7 +38,8 @@
 	MSG_DecPaintCount( wndPtr->hmemTaskQ );
     }
     wndPtr->flags &= ~WIN_NEEDS_BEGINPAINT;
-    
+
+    SendMessage( hwnd, WM_NCPAINT, hrgnUpdate, 0 );
     if (!(wndPtr->flags & WIN_ERASE_UPDATERGN)) lps->fErase = TRUE;
     else lps->fErase = !SendMessage( hwnd, WM_ERASEBKGND, lps->hdc, 0 );
     
diff --git a/windows/sysmetrics.c b/windows/sysmetrics.c
new file mode 100644
index 0000000..bea4561
--- /dev/null
+++ b/windows/sysmetrics.c
@@ -0,0 +1,81 @@
+/*
+ * System metrics functions
+ *
+ * Copyright 1994 Alexandre Julliard
+ */
+
+static char Copyright[] = "Copyright  Alexandre Julliard, 1994";
+
+#include <X11/Xlib.h>
+
+#include "sysmetrics.h"
+
+
+short sysMetrics[SM_CMETRICS];
+
+extern Display * display;
+
+/***********************************************************************
+ *           SYSMETRICS_Init
+ *
+ * Initialisation of the system metrics array.
+ */
+void SYSMETRICS_Init()
+{
+    sysMetrics[SM_CXSCREEN] = DisplayWidth( display, DefaultScreen(display) );
+    sysMetrics[SM_CYSCREEN] = DisplayHeight( display, DefaultScreen(display) );
+    sysMetrics[SM_CXVSCROLL] = SYSMETRICS_CXVSCROLL;
+    sysMetrics[SM_CYHSCROLL] = SYSMETRICS_CYHSCROLL;
+    sysMetrics[SM_CYCAPTION] = SYSMETRICS_CYCAPTION;
+    sysMetrics[SM_CXBORDER] = SYSMETRICS_CXBORDER;
+    sysMetrics[SM_CYBORDER] = SYSMETRICS_CYBORDER;
+    sysMetrics[SM_CXDLGFRAME] = SYSMETRICS_CXDLGFRAME;
+    sysMetrics[SM_CYDLGFRAME] = SYSMETRICS_CYDLGFRAME;
+    sysMetrics[SM_CYVTHUMB] = SYSMETRICS_CYVTHUMB;
+    sysMetrics[SM_CXHTHUMB] = SYSMETRICS_CXHTHUMB;
+    sysMetrics[SM_CXICON] = SYSMETRICS_CXICON;
+    sysMetrics[SM_CYICON] = SYSMETRICS_CYICON;
+    sysMetrics[SM_CXCURSOR] = SYSMETRICS_CXCURSOR;
+    sysMetrics[SM_CYCURSOR] = SYSMETRICS_CYCURSOR;
+    sysMetrics[SM_CYMENU] = SYSMETRICS_CYMENU;
+    sysMetrics[SM_CXFULLSCREEN] = sysMetrics[SM_CXSCREEN];
+    sysMetrics[SM_CYFULLSCREEN] = sysMetrics[SM_CYSCREEN] - sysMetrics[SM_CYCAPTION];
+    sysMetrics[SM_CYKANJIWINDOW] = 0;
+    sysMetrics[SM_MOUSEPRESENT] = 1;
+    sysMetrics[SM_CYVSCROLL] = SYSMETRICS_CYVSCROLL;
+    sysMetrics[SM_CXHSCROLL] = SYSMETRICS_CXHSCROLL;
+    sysMetrics[SM_DEBUG] = 0;
+    sysMetrics[SM_SWAPBUTTON] = 0;
+    sysMetrics[SM_RESERVED1] = 0;
+    sysMetrics[SM_RESERVED2] = 0;
+    sysMetrics[SM_RESERVED3] = 0;
+    sysMetrics[SM_RESERVED4] = 0;
+    sysMetrics[SM_CXMIN] = SYSMETRICS_CXMIN;
+    sysMetrics[SM_CYMIN] = SYSMETRICS_CYMIN;
+    sysMetrics[SM_CXSIZE] = SYSMETRICS_CXSIZE;
+    sysMetrics[SM_CYSIZE] = SYSMETRICS_CYSIZE;
+    sysMetrics[SM_CXFRAME] = GetProfileInt( "windows", "BorderWidth", 4 );
+    sysMetrics[SM_CYFRAME] = sysMetrics[SM_CXFRAME];
+    sysMetrics[SM_CXMINTRACK] = SYSMETRICS_CXMINTRACK;
+    sysMetrics[SM_CYMINTRACK] = SYSMETRICS_CYMINTRACK;
+    sysMetrics[SM_CXDOUBLECLK] = (GetProfileInt( "windows","DoubleClickWidth", 4) + 1) & ~1;
+    sysMetrics[SM_CYDOUBLECLK] = (GetProfileInt( "windows","DoubleClickHeight", 4) + 1) & ~1;
+    sysMetrics[SM_CXICONSPACING] = GetProfileInt( "desktop","IconSpacing", 75);
+    sysMetrics[SM_CYICONSPACING] = GetProfileInt( "desktop","IconVerticalSpacing", 72);
+    sysMetrics[SM_MENUDROPALIGNMENT] = GetProfileInt( "windows","MenuDropAlignment", 0 );
+    sysMetrics[SM_PENWINDOWS] = 0;
+    sysMetrics[SM_DBCSENABLED] = 0;
+}
+
+
+/***********************************************************************
+ *           GetSystemMetrics    (USER.179)
+ */
+int GetSystemMetrics( WORD index )
+{
+    if (index >= SM_CMETRICS) return 0;
+    else return sysMetrics[index];    
+}
+
+
+
diff --git a/windows/win.c b/windows/win.c
index 8411e48..810503c 100644
--- a/windows/win.c
+++ b/windows/win.c
@@ -15,11 +15,13 @@
 #include "win.h"
 #include "user.h"
 #include "dce.h"
+#include "sysmetrics.h"
 
-extern Display * XT_display;
-extern Screen * XT_screen;
+extern Display * display;
 extern Colormap COLOR_WinColormap;
 
+extern void EVENT_RegisterWindow( Window w, HWND hwnd );  /* event.c */
+
 static HWND firstWindow = 0;
 
 /***********************************************************************
@@ -178,17 +180,22 @@
     CREATESTRUCT *createStruct;
     HANDLE hcreateStruct;
     int wmcreate;
-    short newwidth, newheight;
+    XSetWindowAttributes win_attr;
+    Window parentWindow;
+    int x_rel, y_rel;
+    LPPOPUPMENU lpbar;
 
 #ifdef DEBUG_WIN
-    printf( "CreateWindowEx: %s %s %d,%d %dx%d %08x\n",
-	    className, windowName, x, y, width, height, style );
+    printf( "CreateWindowEx: %d '%s' '%s' %d,%d %dx%d %08x %x\n",
+	   exStyle, className, windowName, x, y, width, height, style, parent);
 #endif
 
-    if (x == CW_USEDEFAULT) x = 0;
-    if (y == CW_USEDEFAULT) y = 0;
-    if (width == CW_USEDEFAULT) width = 600;
-    if (height == CW_USEDEFAULT) height = 400;
+    if (x == CW_USEDEFAULT) x = y = 0;
+    if (width == CW_USEDEFAULT)
+    {
+	width = 600;
+	height = 400;
+    }
     if (width == 0) width = 1;
     if (height == 0) height = 1;
 
@@ -201,11 +208,18 @@
 	if (!parent) return 0;
     }
     else if (style & WS_CHILD) return 0;  /* WS_CHILD needs a parent */
-
+    
     if (!(class = CLASS_FindClassByName( className, &classPtr ))) {
 	printf("CreateWindow BAD CLASSNAME '%s' !\n", className);
 	return 0;
 	}    
+
+      /* Correct the window style */
+
+    if (!(style & (WS_POPUP | WS_CHILD)))  /* Overlapped window */
+	style |= WS_CAPTION | WS_CLIPSIBLINGS;
+    if (exStyle & WS_EX_DLGMODALFRAME) style &= ~WS_THICKFRAME;
+
       /* Create the window structure */
 
     hwnd = USER_HEAP_ALLOC(GMEM_MOVEABLE, sizeof(WND)+classPtr->wc.cbWndExtra);
@@ -217,8 +231,8 @@
     wndPtr->hwndNext   = 0;
     wndPtr->hwndChild  = 0;
     wndPtr->dwMagic    = WND_MAGIC;
-    wndPtr->hwndParent = parent;
-    wndPtr->hwndOwner  = parent;  /* What else? */
+    wndPtr->hwndParent = (style & WS_CHILD) ? parent : 0;
+    wndPtr->hwndOwner  = (style & WS_CHILD) ? 0 : parent;
     wndPtr->hClass     = class;
     wndPtr->hInstance  = instance;
     wndPtr->rectWindow.left   = x;
@@ -239,9 +253,12 @@
     wndPtr->hCursor           = 0;
     wndPtr->hWndVScroll       = 0;
     wndPtr->hWndHScroll       = 0;
+    wndPtr->hWndMenuBar       = 0;
+    wndPtr->hWndCaption       = 0;
 
     if (classPtr->wc.cbWndExtra)
 	memset( wndPtr->wExtra, 0, classPtr->wc.cbWndExtra );
+    if (classPtr->wc.style & CS_DBLCLKS) wndPtr->flags |= WIN_DOUBLE_CLICKS;
     classPtr->cWindows++;
 
       /* Get class or window DC if needed */
@@ -261,156 +278,32 @@
 
     WIN_LinkWindow( hwnd, HWND_TOP );
 
-    if (!strcasecmp(className, "COMBOBOX"))
-    {
-	height = 16;
-    }
+      /* Create the X window */
 
-#ifdef USE_XLIB
-    {
-	XSetWindowAttributes win_attr;
-	Window parentWindow;
-	int x_rel, y_rel;
-	
-	win_attr.event_mask = ExposureMask | KeyPressMask | KeyReleaseMask |
-	                      PointerMotionMask | ButtonPressMask |
-			      ButtonReleaseMask | StructureNotifyMask |
-			      FocusChangeMask | EnterWindowMask;
-	win_attr.override_redirect = /*True*/ False;
-	win_attr.colormap = COLOR_WinColormap;
-	if (style & WS_CHILD)
-	{
-	    parentWindow = parentPtr->window;
-	    x_rel = x + parentPtr->rectClient.left-parentPtr->rectWindow.left;
-	    y_rel = y + parentPtr->rectClient.top-parentPtr->rectWindow.top;
-	}
-	else
-	{
-	    parentWindow = DefaultRootWindow( XT_display );
-	    x_rel = x;
-	    y_rel = y;
-	}
-	wndPtr->window = XCreateWindow( XT_display, parentWindow,
-				        x_rel, y_rel, width, height, 0,
-				        CopyFromParent, InputOutput,
-				        CopyFromParent,
-				        CWEventMask | CWOverrideRedirect |
-				        CWColormap, &win_attr );
-	XStoreName( XT_display, wndPtr->window, windowName );
-    }
-#else
-    /* Create the widgets */
-
+    win_attr.event_mask = ExposureMask | KeyPressMask | KeyReleaseMask |
+	                  PointerMotionMask | ButtonPressMask |
+			  ButtonReleaseMask | StructureNotifyMask |
+			  FocusChangeMask | EnterWindowMask;
+    win_attr.override_redirect = /*True*/ False;
+    win_attr.colormap = COLOR_WinColormap;
     if (style & WS_CHILD)
     {
-	wndPtr->shellWidget = 0;
-	if (style & (WS_BORDER | WS_DLGFRAME | WS_THICKFRAME))
-	{ 
-	    int borderCol = 0;
-	    if (COLOR_WinColormap == DefaultColormapOfScreen(XT_screen))
-		borderCol = BlackPixelOfScreen(XT_screen);
-	    wndPtr->winWidget = XtVaCreateManagedWidget(className,
-						    compositeWidgetClass,
-						    parentPtr->winWidget,
-						    XtNx, x,
-						    XtNy, y,
-						    XtNwidth, width,
-						    XtNheight, height,
-						    XtNborderColor, borderCol,
-						    XtNmappedWhenManaged, FALSE,
-						    NULL );
-	}
-	else
-	{
-	    wndPtr->winWidget = XtVaCreateManagedWidget(className,
-						    compositeWidgetClass,
-						    parentPtr->winWidget,
-						    XtNx, x,
-						    XtNy, y,
-						    XtNwidth, width,
-						    XtNheight, height,
-						    XtNborderWidth, 0,
-						    XtNmappedWhenManaged, FALSE,
-						    NULL );
-	}
+	parentWindow = parentPtr->window;
+	x_rel = x + parentPtr->rectClient.left - parentPtr->rectWindow.left;
+	y_rel = y + parentPtr->rectClient.top - parentPtr->rectWindow.top;
     }
     else
     {
-	wndPtr->shellWidget = XtVaAppCreateShell(windowName, 
-						 className,
-						 topLevelShellWidgetClass,
-						 XT_display,
-						 XtNx, x,
-						 XtNy, y,
-						 XtNcolormap, COLOR_WinColormap,
-						 XtNmappedWhenManaged, FALSE,
-						 NULL );
-	wndPtr->compositeWidget = XtVaCreateManagedWidget(className,
-						    formWidgetClass,
-						    wndPtr->shellWidget,
-						    NULL );
-/*	wndPtr->winWidget = wndPtr->compositeWidget; */
-	wndPtr->winWidget = wndPtr->shellWidget;
-	if (wndPtr->wIDmenu == 0)
-	{
-	    wndPtr->menuBarPtr = 
-		    MENU_CreateMenuBar(wndPtr->compositeWidget,
-				       instance, hwnd,
-				       classPtr->wc.lpszMenuName,
-				       width);
-	    if (wndPtr->menuBarPtr)
-		    wndPtr->wIDmenu = 
-			GlobalHandleFromPointer(wndPtr->menuBarPtr->firstItem);
-	}
-	else
-	{
-	    wndPtr->menuBarPtr = MENU_UseMenu(wndPtr->compositeWidget,
-						  instance, hwnd,
-						  wndPtr->wIDmenu, width);
-	}
-
-	if (wndPtr->menuBarPtr != NULL)
-	{
-	    wndPtr->winWidget = 
-		    XtVaCreateManagedWidget(className,
-					    compositeWidgetClass,
-					    wndPtr->compositeWidget,
-					    XtNwidth, width,
-					    XtNheight, height,
-					    XtNfromVert,
-					    wndPtr->menuBarPtr->menuBarWidget,
-					    XtNvertDistance, 4,
-					    NULL );
-	}
-	else
-	{
-	    wndPtr->winWidget = 
-		    XtVaCreateManagedWidget(className,
-					compositeWidgetClass,
-					wndPtr->compositeWidget,
-					XtNwidth, width,
-					XtNheight, height,
-					NULL );
-	}
+	parentWindow = DefaultRootWindow( display );
+	x_rel = x;
+	y_rel = y;
     }
-    if (wndPtr->shellWidget) XtRealizeWidget( wndPtr->shellWidget );
-    if (wndPtr->compositeWidget) XtRealizeWidget( wndPtr->compositeWidget );
-    XtRealizeWidget( wndPtr->winWidget );
-    wndPtr->window = XtWindow( wndPtr->winWidget );
-#endif  /* USE_XLIB */
-
-    if ((style & WS_VSCROLL) == WS_VSCROLL) {
-    	newheight = height - (((style & WS_HSCROLL) == WS_HSCROLL) ? 16 : 0);
-	wndPtr->hWndVScroll = CreateWindow("SCROLLBAR", "",
-		WS_CHILD | WS_CLIPSIBLINGS | WS_VISIBLE | SBS_VERT,
-		width - 16, 0, 16, newheight, hwnd, 2, instance, 0L);
-	}
-    if ((style & WS_HSCROLL) == WS_HSCROLL) {
-    	newwidth = width - (((style & WS_VSCROLL) == WS_VSCROLL) ? 16 : 0);
-	wndPtr->hWndHScroll = CreateWindow("SCROLLBAR", "",
-		WS_CHILD | WS_CLIPSIBLINGS | WS_VISIBLE | SBS_HORZ,
-		0, height - 16, newwidth, 16, hwnd, 3, instance, 0L);
-	}
+    wndPtr->window = XCreateWindow(display, parentWindow,
+				   x_rel, y_rel, width, height, 0,
+				   CopyFromParent, InputOutput, CopyFromParent,
+				   CWEventMask | CWOverrideRedirect |
+				   CWColormap, &win_attr );
+    XStoreName( display, wndPtr->window, windowName );
 
       /* Send the WM_CREATE message */
 	
@@ -452,30 +345,56 @@
 
     GlobalUnlock( hcreateStruct );
     GlobalFree( hcreateStruct );
-    
+
     if (wmcreate == -1)
     {
 	  /* Abort window creation */
 
-	if (parent) parentPtr->hwndChild = wndPtr->hwndNext;
-	else firstWindow = wndPtr->hwndNext;
-#ifdef USE_XLIB
-	XDestroyWindow( XT_display, wndPtr->window );
-#else	
-	if (wndPtr->shellWidget) XtDestroyWidget( wndPtr->shellWidget );
-	else XtDestroyWidget( wndPtr->winWidget );
-#endif
+	WIN_UnlinkWindow( hwnd );
+	XDestroyWindow( display, wndPtr->window );
 	if (wndPtr->flags & WIN_OWN_DC) DCE_FreeDCE( wndPtr->hdce );
 	classPtr->cWindows--;
 	USER_HEAP_FREE( hwnd );
 	return 0;
     }
 
-#ifdef USE_XLIB    
-    EVENT_AddHandlers( wndPtr->window, hwnd );
-#else
-    EVENT_AddHandlers( wndPtr->winWidget, hwnd );
-#endif
+      /* Create scrollbars */
+
+    if (windowName != NULL) SetWindowText(hwnd, windowName);
+    if ((style & WS_CAPTION) == WS_CAPTION) {
+	wndPtr->hWndCaption = CreateWindow("CAPTION", "",
+		WS_CHILD | WS_CLIPSIBLINGS | WS_VISIBLE,
+		0, -20, width, 20, hwnd, 1, instance, 0L);
+	}
+    if (((style & WS_CHILD) != WS_CHILD) && (wndPtr->wIDmenu != 0)) {
+	lpbar = (LPPOPUPMENU) GlobalLock(wndPtr->wIDmenu);
+	if (lpbar != NULL) {
+	    lpbar->ownerWnd = hwnd;
+	    wndPtr->hWndMenuBar = CreateWindow("POPUPMENU", "",
+		WS_CHILD | WS_CLIPSIBLINGS | WS_VISIBLE,
+		0, 0, width, 20, hwnd, 2, instance, (LPSTR)lpbar);
+	    }
+	}
+    if ((style & WS_VSCROLL) == WS_VSCROLL)
+    {
+	wndPtr->hWndVScroll = CreateWindow("SCROLLBAR", "",
+		WS_CHILD | WS_CLIPSIBLINGS | WS_VISIBLE | SBS_VERT,
+		wndPtr->rectClient.right-wndPtr->rectClient.left, 0,
+                SYSMETRICS_CXVSCROLL,
+		wndPtr->rectClient.bottom-wndPtr->rectClient.top,
+                hwnd, 3, instance, 0L);
+    }
+    if ((style & WS_HSCROLL) == WS_HSCROLL)
+    {
+	wndPtr->hWndHScroll = CreateWindow("SCROLLBAR", "",
+		WS_CHILD | WS_CLIPSIBLINGS | WS_VISIBLE | SBS_HORZ,
+		0, wndPtr->rectClient.bottom-wndPtr->rectClient.top,
+                wndPtr->rectClient.right-wndPtr->rectClient.left,
+		SYSMETRICS_CYHSCROLL,
+                hwnd, 4, instance, 0L);
+    }
+
+    EVENT_RegisterWindow( wndPtr->window, hwnd );
 
     WIN_SendParentNotify( hwnd, wndPtr, WM_CREATE );
     
@@ -515,12 +434,7 @@
 
       /* Destroy the window */
 
-#ifdef USE_XLIB
-    XDestroyWindow( XT_display, wndPtr->window );
-#else
-    if (wndPtr->shellWidget) XtDestroyWidget( wndPtr->shellWidget );
-    else XtDestroyWidget( wndPtr->winWidget );
-#endif
+    XDestroyWindow( display, wndPtr->window );
     if (wndPtr->flags & WIN_OWN_DC) DCE_FreeDCE( wndPtr->hdce );
     classPtr->cWindows--;
     USER_HEAP_FREE( hwnd );
@@ -588,49 +502,7 @@
  */
 BOOL SetMenu(HWND hwnd, HMENU hmenu)
 {
-#ifdef USE_XLIB
     return FALSE;
-#else
-    WND *wndPtr;
-    wndPtr = WIN_FindWndPtr(hwnd);
-    if (wndPtr == NULL)
-	return FALSE;
-
-    if (wndPtr->dwStyle & WS_CHILD) return FALSE;
-
-    if (wndPtr->menuBarPtr != NULL)
-    {
-	XtVaSetValues(wndPtr->winWidget, XtNfromVert, NULL, NULL);
-	MENU_CollapseMenu(wndPtr->menuBarPtr);
-    }
-    
-    wndPtr->menuBarPtr = MENU_UseMenu(wndPtr->compositeWidget, 
-				      wndPtr->hInstance, hwnd, hmenu, 
-				      wndPtr->rectClient.right -
-				      wndPtr->rectClient.left);
-
-    if (wndPtr->menuBarPtr != NULL)
-    {
-	XtVaSetValues(wndPtr->winWidget, 
-		      XtNfromVert, wndPtr->menuBarPtr->menuBarWidget, 
-		      XtNvertDistance, 4,
-		      NULL);
-    }
-    else
-    {
-	if (wndPtr->wIDmenu != 0)
-	{
-	    wndPtr->menuBarPtr = MENU_UseMenu(wndPtr->compositeWidget, 
-					      wndPtr->hInstance, hwnd, 
-					      wndPtr->wIDmenu, 
-					      wndPtr->rectClient.right -
-					      wndPtr->rectClient.left);
-	}
-	return FALSE;
-    }
-
-    return TRUE;
-#endif  /* USE_XLIB */
 }
 
 
diff --git a/windows/winpos.c b/windows/winpos.c
index 0b474e2..884f411 100644
--- a/windows/winpos.c
+++ b/windows/winpos.c
@@ -8,8 +8,7 @@
 
 #include "win.h"
 
-extern Display * XT_display;
-extern Screen * XT_screen;
+extern Display * display;
 
 
 /***********************************************************************
@@ -213,10 +212,8 @@
     RECT newWindowRect, newClientRect;
     WND *wndPtr;
     int calcsize_result = 0;
-#ifdef USE_XLIB
     XWindowChanges winChanges;
     int changeMask = 0;
-#endif
 
 #ifdef DEBUG_WIN
     printf( "SetWindowPos: %d %d %d,%d %dx%d 0x%x\n",
@@ -311,7 +308,7 @@
     }
     
       /* Perform the moving and resizing */
-#ifdef USE_XLIB
+
     if (!(winPos->flags & SWP_NOMOVE))
     {
 	WND * parentPtr;
@@ -343,31 +340,28 @@
 	}
 	changeMask |= CWStackMode;
     }
-    if (changeMask) XConfigureWindow( XT_display, wndPtr->window,
+    if (changeMask) XConfigureWindow( display, wndPtr->window,
 				      changeMask, &winChanges );
-#endif
 
     if (winPos->flags & SWP_SHOWWINDOW)
     {
 	wndPtr->dwStyle |= WS_VISIBLE;
-#ifdef USE_XLIB
-	XMapWindow( XT_display, wndPtr->window );
-#else		
-	if (wndPtr->shellWidget) XtMapWidget( wndPtr->shellWidget );
-	else XtMapWidget( wndPtr->winWidget );
-#endif
+	XMapWindow( display, wndPtr->window );
     }
     else if (winPos->flags & SWP_HIDEWINDOW)
     {
 	wndPtr->dwStyle &= ~WS_VISIBLE;
-#ifdef USE_XLIB
-	XUnmapWindow( XT_display, wndPtr->window );
-#else		
-	if (wndPtr->shellWidget) XtUnmapWidget( wndPtr->shellWidget );
-	else XtUnmapWidget( wndPtr->winWidget );
-#endif	
+	XUnmapWindow( display, wndPtr->window );
     }
 
+      /* Send WM_NCPAINT message if needed */
+    if ((winPos->flags & (SWP_FRAMECHANGED | SWP_SHOWWINDOW)) ||
+	(!(winPos->flags & SWP_NOSIZE)) ||
+	(!(winPos->flags & SWP_NOMOVE)) ||
+	(!(winPos->flags & SWP_NOACTIVATE)) ||
+	(!(winPos->flags & SWP_NOZORDER)))
+	    SendMessage( hwnd, WM_NCPAINT, 1, 0L );
+
       /* Finally send the WM_WINDOWPOSCHANGED message */
     wndPtr->rectWindow = newWindowRect;
     wndPtr->rectClient = newClientRect;