Index: Source/x11/XGServerWindow.m
===================================================================
--- Source/x11/XGServerWindow.m	(revision 26941)
+++ Source/x11/XGServerWindow.m	(working copy)
@@ -996,16 +996,9 @@
           XGetWindowAttributes(dpy, parent, &wattr);
           NSDebugLLog(@"Offset", @"Parent border,width,height %d,%d,%d\n",
                       wattr.border_width, wattr.width, wattr.height);
-          l = wattr.border_width;
-          t = wattr.border_width;
+          l = repx + wattr.border_width;
+          t = repy + wattr.border_width;
           
-          /* Find total parent size and subtract window size and
-           * top-left-corner offset to determine bottom-right-corner
-           * offset.
-           */
-          r = wattr.width + wattr.border_width * 2;
-          b = wattr.height + wattr.border_width * 2;
-          
           // Some window manager e.g. KDE2 put in multiple windows,
           // so we have to find the right parent, closest to root
           /* FIXME: This section of code has caused problems with
@@ -1019,14 +1012,16 @@
               || (repx == 0 && repy == 0))
             {
               Window new_parent = parent;
+              Window root = window->root;
 
               while (new_parent && (new_parent != window->root))
                 {
-                  Window root;
                   Window *children = 0;
                   unsigned int nchildren;
                   
                   parent = new_parent;
+                  repx = wattr.x;
+                  repy = wattr.y;
                   NSLog(@"QueryTree window is %d (root %d cwin root %d)", 
                         parent, root, window->root);
                   if (!XQueryTree(dpy, parent, &root, &new_parent, 
@@ -1045,21 +1040,22 @@
                     }
                   if (new_parent && new_parent != window->root)
                     {
-                      XGetWindowAttributes(dpy, parent, &wattr);
+                      XGetWindowAttributes(dpy, new_parent, &wattr);
                       l += repx + wattr.border_width;
                       t += repy + wattr.border_width;
-                      r = wattr.width + wattr.border_width * 2;
-                      b = wattr.height + wattr.border_width * 2;
-                      repx = wattr.x;
-                      repy = wattr.y;
                     }
                 } /* while */
             } /* generic.flags.doubleParentWindow */
+          
+          /* Find total parent size and subtract window size and
+           * top-left-corner offset to determine bottom-right-corner
+           * offset.
+           */
+          r = wattr.width + wattr.border_width * 2;
           r -= (window->xframe.size.width + l);
+          b = wattr.height + wattr.border_width * 2;
           b -= (window->xframe.size.height + t);
-          l += repx;
-          t += repy;
-
+          
           o->l = l;
           o->r = r;
           o->t = t;
