From:	CRDGW2::CRDGW2::MRGATE::"SMTP::CRVAX.SRI.COM::RELAY-INFO-VAX" 24-MAY-1989 05:58
To:	MRGATE::"ARISIA::EVERHART"
Subj:	X Windows Select_Window() routine

Received: From KL.SRI.COM by CRVAX.SRI.COM with TCP; Tue, 23 MAY 89 15:08:10 PDT
Received: from ucbvax.Berkeley.EDU by KL.SRI.COM with TCP; Tue, 23 May 89 14:43:33 PDT
Received: by ucbvax.Berkeley.EDU (5.61/1.36)
	id AA10789; Tue, 23 May 89 14:08:57 -0700
Received: from USENET by ucbvax.Berkeley.EDU with netnews
	for info-vax@kl.sri.com (info-vax@kl.sri.com)
	(contact usenet@ucbvax.Berkeley.EDU if you have questions)
Date: 23 May 89 04:23:29 GMT
From: munnari!otc!metro!basser!usage!csdvx0!decus!taylort@uunet.uu.net  (Trevor Taylor)
Organization: DECUS, South Pacific Chapter
Subject: X Windows Select_Window() routine
Message-Id: <137@decus.com.au>
Sender: info-vax-request@kl.sri.com
To: info-vax@kl.sri.com

Selecting a Window using the Pointer

Following on from my previous item about the pseudo-root
window, here is a modified version of the Select_Window
routine which appears in a lot of the MIT code, e.g.,
xwininfo, xprop, etc.

NOTE: This is not a direct replacement for the code in
DSIMPLE.C (in the MIT sources) because it has an extra
parameter: subwindow_flag. The purpose of this flag is
to allow/prevent the translation of coordinates into a
subwindow ID. Depending on the application, you may
want to get the ID of the parent window or of the subwindow
that you specifically pointed to. Try it and see. With
this routine xwininfo will work (once you sort out the
location of the include files).


	Trevor Taylor


/*------------ Cut here -----------*/

/*
 * Routine to let user select a window using the mouse
 *
 * Modified to work with DECwindows
 *
 */

#include <X11/Xlib.h>
#include <X11/cursorfont.h>

/*
	NOTE: As with almost all applications,
	this code does not work on displays with
	more than one screen because it uses
	DefaultScreen(dpy).
*/

Window Select_Window(dpy, subwindow_flag)
Display *dpy;
int subwindow_flag;
{
  int status;
  Cursor cursor;
  XEvent event;
  Window target_win = None;
  int buttons = 0;

#ifdef VMS
  Window root;
  Window child;
  int child_x, child_y;
  extern Window pseudo_root();
  /* 
    For VMS we have to use the "pseudoroot" because 
    of the window manager 
  */
  root = pseudo_root(dpy, DefaultScreen(dpy));
#else
  Window root;
  root = RootWindow(dpy, DefaultScreen(dpy));
#endif


  /* Make the target cursor */
  cursor = XCreateFontCursor(dpy, XC_crosshair);

  /* Grab the pointer using target cursor, letting it room all over */
  status = XGrabPointer(dpy, root, False,
			ButtonPressMask|ButtonReleaseMask, GrabModeSync,
			GrabModeAsync, None, cursor, CurrentTime);
  if (status != GrabSuccess) Fatal_Error("Can't grab the mouse.");

  /* Let the user select a window... */
  while ((target_win == None) || (buttons != 0)) {
    /* allow one more event */
    XAllowEvents(dpy, SyncPointer, CurrentTime);

    XWindowEvent(dpy, root,
		 ButtonPressMask|ButtonReleaseMask, &event);

    switch (event.type) {
    case ButtonPress:
      if (target_win == None) {
	target_win = event.xbutton.subwindow; /* window selected */

#ifdef VMS
	if (target_win == None)
	    target_win = event.xbutton.window;
	else
	{
	    /* Try to determine if the pointer is inside a child */

	    /*
		Not everybody wants to select a subwindow.
		For example, if the user wants to see what
		the window ID is of a top-level shell widget,
		then this routine must NOT look for a child.
	    */
	    if (subwindow_flag)
	    {
		child = None;
		if (XTranslateCoordinates(dpy, root, target_win,
			event.xbutton.x, event.xbutton.y,
			&child_x, &child_y, &child))
		{
		    if (child != None)
			target_win = child;
		}
	    }
	}
#else
	if (target_win == None)
	    target_win = root;
#endif

      }
      buttons++;
      break;
    case ButtonRelease:
      if (buttons > 0) /* there may have been some down before we started */
	buttons--;
       break;
    }
  } 

  XUngrabPointer(dpy, CurrentTime);      /* Done with pointer */

  return(target_win);
}




Received: from usage.csd by csdvx0.accu.csd.unsw.oz; Tue, 23 May 89 00:16 EST
Date: Tue, 23 May 89 00:15:06 EST
From: news@usage.csd
Subject: network news article
To: newsmgr@csdvx0.accu.csd.unsw.oz
Message-Id:  <8905221415.AA26676@usage.csd>

