[comp.windows.x] Getting window position

joe@etac632 (Joe Fulson-Woytek) (07/27/90)

I have written the standard "helloworld" program as described in the
"Introduction to the X Window System" by Oliver Jones. My goal is to
use X on an Iris, a Sun, and an HP. The basic program works on all 3.
My next step was to find the position and size of the X window after an 
expose event. The likely candidates seemed to be XGetGeometry or
XGetWindowAttributes. Either of these work fine on the Iris. BUt on
both the HP and the Sun, the size is right but the x and y position
come out as 0, 0 (I should note that I do an XGetGeometry before the
Event loop and it returns the correct x and y position - I only have
problems within the event loop). I next tried to check for a
ConfigureNotify event and use the XConfigureEvent structure to get
the x and y. This also returned a 0,0 on the Sun (I haven't tried the
ConfigureNotify on the other systems). One more note - the problem is
with the server - I can run my program on the HP or Sun and display
on the IRIS and get the window position.

Any suggestions would be greatly appreciated.


Joe FUlson-Woytek

spencer@eecs.umich.edu (Spencer W. Thomas) (07/28/90)

It appears that you are using a "reparenting" window manager on the
Sun and HP (twm and mwm are such, uwm is not).  The configurenotify
event (and XGetGeometry) always return the position *relative to the
parent window*.  Without a window manager, or with one that doesn't
reparent, the parent window is the root.  With twm, mwm, and the like,
the parent window is there to give you a title bar, drag handles, etc.
Most frequently, your window is indeed at 0,0 within its parent.  If
you want to know where it is in the root, you can use
XTranslateCoordinates. 

But, WHY do you care where the window is on the screen?  Unless you
are going to save the position information in a file so that you can
restart the program with exactly the same position and size, there is
almost no reason to need to know the window position.  (I might note,
also, that some window managers will not honor a placement request for
a new window, even if it is "user specified".)

--
=Spencer W. Thomas 		EECS Dept, U of Michigan, Ann Arbor, MI 48109
spencer@eecs.umich.edu		313-936-2616 (8-6 E[SD]T M-F)

west@gsrc.enet.dec.com (Jim West (Stealth Contractor)) (08/01/90)

In article <2919@dftsrv.gsfc.nasa.gov>, joe@etac632 (Joe Fulson-Woytek) writes...
>I have written the standard "helloworld" program as described in the
>"Introduction to the X Window System" by Oliver Jones. My goal is to
>use X on an Iris, a Sun, and an HP. The basic program works on all 3.
>My next step was to find the position and size of the X window after an 
>expose event. The likely candidates seemed to be XGetGeometry or
>XGetWindowAttributes. Either of these work fine on the Iris. BUt on
>both the HP and the Sun, the size is right but the x and y position
>come out as 0, 0 (I should note that I do an XGetGeometry before the
>Event loop and it returns the correct x and y position - I only have
>problems within the event loop). I next tried to check for a
>ConfigureNotify event and use the XConfigureEvent structure to get
>the x and y. This also returned a 0,0 on the Sun (I haven't tried the
>ConfigureNotify on the other systems). One more note - the problem is
>with the server - I can run my program on the HP or Sun and display
>on the IRIS and get the window position.
> 
>Any suggestions would be greatly appreciated.
> 
> 
>Joe FUlson-Woytek

  Have you tried XTranslateCoordinates relative to the root window?  The
x and y values are relative to the windows parent.  It looks like on the
IRIS that the root window is the parent whereas on the HP and SUN there is
a window between you and the root...probably window manager frame window.

----------------------------------------------------------------------
 Jim West                      |  The Schainker Converse
 west@gsrc.enet.dec.com        |  to Hoare's Law :
                               |
 These are my opinions.        |   Inside every small problem
 Digital has no idea           |     is a larger problem struggling
 what I'm  saying.             |       to get out.
----------------------------------------------------------------------


----------------------------------------------------------------------
 Jim West                      |  The Schainker Converse
 west@gsrc.enet.dec.com        |  to Hoare's Law :
                               |
 These are my opinions.        |   Inside every small problem
 Digital has no idea           |     is a larger problem struggling
 what I'm  saying.             |       to get out.
----------------------------------------------------------------------

ramakris@bloss.cs.vt.edu (S.Ramakrishnan) (08/01/90)

In reply to the query of joe@etac632 (Joe Fulson-Woytek) of NSESCC, 
------------------------------------------------------------------------
Goddard Space Flight Center, Greenbelt MD
------------------------------------------------------------------------

> My next step was to find the position and size of the X window after an 
> expose event. The likely candidates seemed to be XGetGeometry or


>Joe FUlson-Woytek


 Getting the window position
 ---------------------------
  
  I assume that you need the position of the top left of thw window with
respect to the root window coordinates. 

  Why not try this simple method :


   int  pos_x, pos_y ;

   XTranslateCoordinates(display , window , root_w , 0 , 0 , &pos_x , pos_y );

   where display = the current display of the window.
         window = the window in question.
         ( 0 , 0 ) == coordinates of the top left of the window.


  If someone thinks there could be problems with this, please do inform
me. I have used this method in some packages and am so far not having
any problems.


--

 -- S.Ramakrishnan, CS Dept, McBryde Hall, VaTech, Blacksburg, Virg. 24061-0106
   (email : ramakris@bloss.cs.vt.edu, ramakris@vtcc1.cc.vt.edu)

ramakris@bloss.cs.vt.edu (S.Ramakrishnan) (08/01/90)

 ( There was a small error in my earlier posting : am posting
   this again after correcting it. Apologize for the error ).

In reply to the query of joe@etac632 (Joe Fulson-Woytek) of NSESCC, Goddard Space Flight Center, Greenbelt MD
--------------------------------------------------------------------------------

> My next step was to find the position and size of the X window after an 
> expose event. The likely candidates seemed to be XGetGeometry or


>Joe FUlson-Woytek


 Getting the window position
 ---------------------------
  
  I assume that you need the position of the top left of thw window with
respect to the root window coordinates. 

  Why not try this simple method :


   int  pos_x, pos_y ;

   XTranslateCoordinates(display , window , root_w , 0 , 0 , &pos_x , &pos_y );

   where display = the current display of the window.
         window = the window in question.
         ( 0 , 0 ) == coordinates of the top left of the window.


  If someone thinks there could be problems with this, please do inform
me. I have used this method in some packages and am so far not having
any problems.


--

 -- S.Ramakrishnan, CS Dept, McBryde Hall, VaTech, Blacksburg, Virg. 24061-0106
   (email : ramakris@bloss.cs.vt.edu, ramakris@vtcc1.cc.vt.edu)

mouse@SHAMASH.MCRCIM.MCGILL.EDU (der Mouse) (08/02/90)

>> My next step was to find the position and size of the X window after
>> an expose event.

> I assume that you need the position of the top left of thw window
> with respect to the root window coordinates.

> Why not try [XTranslateCoordinates]

> If someone thinks there could be problems with this, please do inform
> me.  I have used this method in some packages and am so far not
> having any problems.

The potential problems are mostly race conditions.  The basic problem
is that there's essentially no synchronization.  Yes,
XTranslateCoordinates will give you the result, but it may be out of
date even before you get it.  The window manager can move the window at
any time.  (A correctly functioning window manager will ensure that you
get some event - a ConfigureNotify, I think - when this happens, but in
the presence of a correctly functioning window manager there is no
problem to start with.[%])

					der Mouse

			old: mcgill-vision!mouse
			new: mouse@larry.mcrcim.mcgill.edu

[%] Or more precisely will ensure that such an event is generated; if
    you don't select for it that's your lookout.

joe@etac632 (Joe Fulson-Woytek) (08/02/90)

In article <1515@shodha.dec.com> west@gsrc.enet.dec.com (Jim West (Stealth Contractor)) writes:
>
>  Have you tried XTranslateCoordinates relative to the root window?  The
>x and y values are relative to the windows parent.  It looks like on the
>IRIS that the root window is the parent whereas on the HP and SUN there is
>a window between you and the root...probably window manager frame window.
>
Yes, I gave this a try but still got the translated coordinates also
came back 0.,0.
While I still think it is important to be able to get the current position
of the window within the screen, I have ended up using a different technigue
for my problem which does not require this knowledge. Thanks to all who
responded.

Joe FUlson-Woytek

joe@etac632 (Joe Fulson-Woytek) (08/02/90)

In article <532@creatures.cs.vt.edu> ramakris@bloss.cs.vt.edu (S.Ramakrishnan) writes:
;
;
; Getting the window position
;  I assume that you need the position of the top left of thw window with
; respect to the root window coordinates. 
;  Why not try this simple method :
;
;   int  pos_x, pos_y ;
;
;   XTranslateCoordinates(display , window , root_w , 0 , 0 , &pos_x , pos_y );
;
;   where display = the current display of the window.
;         window = the window in question.
;         ( 0 , 0 ) == coordinates of the top left of the window.
OOps. I had justed posted a response to a previous suggestion to try
XTranslateCoordinate by saying it didn't work. When I saw this second posting
suggesting the same thing, I decided to recheck my code. I discovered I had
set the "window" argument wrong, and now XTranslateCoordinate works as
advertised. Thanks.
Joe Fulson-Woytek