Index: Source/x11/XGGLContext.m
===================================================================
--- Source/x11/XGGLContext.m	(revision 28046)
+++ Source/x11/XGGLContext.m	(working copy)
@@ -33,6 +33,7 @@
 #include <AppKit/NSView.h>
 #include <AppKit/NSWindow.h>
 #include "x11/XGServerWindow.h"
+#include "x11/XGServer.h"
 #include "x11/XGOpenGL.h"
 #include <X11/Xlib.h>
 
@@ -58,7 +59,7 @@
 @implementation XGXSubWindow
 
 //We assume that the current context is the same and is an XGServer
-- initWithView: (NSView *)view visualinfo: (XVisualInfo *)xVisualInfo
+- initWithContext:(XGGLContext *)context visualinfo: (XVisualInfo *)xVisualInfo
 {
   NSRect rect;
   gswindow_device_t *win_info;
@@ -67,11 +68,13 @@
   int x, y, width, height;
   int mask;
   XSetWindowAttributes window_attributes;
+  Window defaultWindow;
+  Display *dpy;
 
   self = [super init];
   if (!self)
     return nil;
-
+#if 0
   window = [view window];
   NSAssert(window, @"request of an X window attachment on a view that is not on a NSWindow");
 
@@ -80,16 +83,16 @@
       [NSException raise: NSInvalidArgumentException
                    format: @"Cannot attach an Xwindow to a view that is rotated or scaled"];
     }
+#endif
   
   server = (XGServer *)GSServerForWindow(window);
   NSAssert(server != nil, NSInternalInconsistencyException);
 
   NSAssert([server isKindOfClass: [XGServer class]], 
            NSInternalInconsistencyException);
-
+#if 0
   win_info = [XGServer _windowWithTag: [window windowNumber]];
   NSAssert(win_info, NSInternalInconsistencyException);
-
   if ([server handlesWindowDecorations] == YES)
     {
       /* The window manager handles window decorations, so the
@@ -112,26 +115,31 @@
   y = NSHeight(win_info->xframe) - NSMaxY(rect);
   width = NSWidth(rect);
   height = NSHeight(rect);
+#endif
 
+  dpy = [server xDisplay]; 
+  defaultWindow = [server xDisplayRootWindowForScreen:0]; //[context currentVirtualScreen]];
+
   window_attributes.border_pixel = 255;
   window_attributes.background_pixel = 0;
-  window_attributes.colormap = XCreateColormap(win_info->display, 
-                                               win_info->ident,
-                                               xVisualInfo->visual, AllocNone);
   window_attributes.event_mask = StructureNotifyMask
                                | VisibilityChangeMask
                                | ExposureMask;
-
+  window_attributes.colormap = XCreateColormap(dpy, 
+                                               defaultWindow,
+                                               xVisualInfo->visual, AllocNone);
+  
   mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
 
-  xwindowid = XCreateWindow(win_info->display, win_info->ident,
-                            x, y, width, height, 0, 
+  xwindowid = XCreateWindow(dpy, defaultWindow,
+                            0, 0, 1, 1, 0, 
                             xVisualInfo->depth, InputOutput, xVisualInfo->visual, 
                             mask, &window_attributes);
 
-  XMapWindow(win_info->display, xwindowid);
+  //XMapWindow(win_info->display, xwindowid);
 
-  attached = view;
+  //attached = view;
+  //XSync(dpy, False);
 
   return self;
 }
@@ -213,12 +221,73 @@
   [super dealloc];
 }
 
-+ subwindowOnView:(NSView *)view visualinfo:(XVisualInfo *)xVisualInfo
++ subwindowOnContext:(XGGLContext *)context visualinfo:(XVisualInfo *)xVisualInfo
 {
-  XGXSubWindow *win = [[self alloc] initWithView: view visualinfo: xVisualInfo];
+  XGXSubWindow *win = [[self alloc] initWithContext: context visualinfo: xVisualInfo];
 
   return AUTORELEASE(win);
 }
+
+- (void) reparentOnView:(NSView *)view
+{
+  NSRect rect;
+  gswindow_device_t *win_info;
+  XGServer *server;
+  NSWindow *window;
+  int x, y, width, height;
+  int mask;
+  XSetWindowAttributes window_attributes;
+  Window defaultWindow;
+  Display *dpy;
+
+  window = [view window];
+  NSAssert(window, @"request of an X window attachment on a view that is not on a NSWindow");
+
+  if ([view isRotatedOrScaledFromBase])
+    {
+      [NSException raise: NSInvalidArgumentException
+                   format: @"Cannot attach an Xwindow to a view that is rotated or scaled"];
+    }
+  
+  server = (XGServer *)GSServerForWindow(window);
+  NSAssert(server != nil, NSInternalInconsistencyException);
+
+  NSAssert([server isKindOfClass: [XGServer class]], 
+           NSInternalInconsistencyException);
+
+  win_info = [XGServer _windowWithTag: [window windowNumber]];
+  NSAssert(win_info, NSInternalInconsistencyException);
+  if ([server handlesWindowDecorations] == YES)
+    {
+      /* The window manager handles window decorations, so the
+       * the parent X window is equal to the content view and
+       * we must therefore use content view coordinates.
+       */
+      rect = [view convertRect: [view bounds]
+                        toView: [window contentView]];
+    }
+  else
+    {
+      /* The GUI library handles window decorations, so the
+       * the parent X window is equal to the NSWindow frame
+       * and we can use window base coordinates.
+       */
+      rect = [view convertRect: [view bounds] toView: nil];
+    }
+
+  x = NSMinX(rect);
+  y = NSHeight(win_info->xframe) - NSMaxY(rect);
+  width = NSWidth(rect);
+  height = NSHeight(rect);
+
+  XReparentWindow(win_info->display, xwindowid, win_info->ident, x, y);
+  ASSIGN(attached,view);
+  [self update];
+  [self map];
+  //XMoveResizeWindow(win_info->display, xwindowid, x, y, width, height);
+  //XMapWindow(win_info->display, xwindowid);
+}
+
 @end
 
 //FIXME:
@@ -405,10 +474,14 @@
     }
 
   ASSIGN(pixelFormat, (XGGLPixelFormat *)_format);
-  
+
+  xSubWindow = RETAIN([XGXSubWindow subwindowOnContext:self
+		 visualinfo:[pixelFormat xvinfo]]);
+  glx_drawable = [pixelFormat drawableForWindow: xSubWindow->xwindowid];
+
   //FIXME: allow index mode and sharing
-  glx_context = [pixelFormat createGLXContext: (XGGLContext *)share];
-  
+  glx_context = [pixelFormat createGLXContext: (XGGLContext *)share]; 
+ 
   return self;
 }
 
@@ -435,7 +508,7 @@
   if (xSubWindow == nil)
     [NSException raise: NSGenericException
 		 format: @"GL Context is not bind, cannot be made current"];
-  
+ 
   NSAssert(glx_context != None && glx_drawable != None,
 	   NSInternalInconsistencyException);
 
@@ -496,9 +569,12 @@
     {
       [current_view _setIgnoresBacking: saved_ignores_backing];
     }
-  ASSIGN(xSubWindow, [XGXSubWindow subwindowOnView: view 
+#if 0
+  ASSIGN(xSubWindow, [XGXSubWindow subwindowOnContext: self 
                                    visualinfo: [pixelFormat xvinfo]]);
-  glx_drawable = [pixelFormat drawableForWindow: xSubWindow->xwindowid];
+#endif
+  [xSubWindow reparentOnView:view];
+
   saved_ignores_backing = [view _ignoresBacking];
   [view _setIgnoresBacking: YES];
 
