home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / windows / x / 14462 < prev    next >
Encoding:
Internet Message Format  |  1992-07-27  |  3.0 KB

  1. Path: sparky!uunet!olivea!hal.com!decwrl!sdd.hp.com!zaphod.mps.ohio-state.edu!cis.ohio-state.edu!organ!ware
  2. From: ware@organ.cis.ohio-state.edu (Peter Ware)
  3. Newsgroups: comp.windows.x
  4. Subject: Re: Trapping BadWindow Errors?
  5. Message-ID: <WARE.92Jul27181525@organ.cis.ohio-state.edu>
  6. Date: 27 Jul 92 23:15:25 GMT
  7. References: <brself.712270104@hal>
  8. Sender: news@cis.ohio-state.edu (NETnews        )
  9. Organization: Ohio State Computer Science
  10. Lines: 92
  11. In-Reply-To: brself@hal.gnu.ai.mit.edu's message of Mon, 27 Jul 1992 20: 48:24 GMT
  12. Originator: ware@organ.cis.ohio-state.edu
  13.  
  14. Here is how I deal with setting the input focus.  This temporarily installs a 
  15. handler that ignores any BaMatch errors generated by SetInputFocuse requests.
  16. A global flag is used to inform the caller if there was a problem.
  17. This could be generalized but I've not seen any reason to.
  18.  
  19. --pete
  20. ----------------------------------------------------------------------
  21.  
  22. static int      (*oldHandler) ();    /* the previous error handler */
  23. static Boolean  xoInputFailed;        /* set if SetInputFocus error */
  24.  
  25. /*
  26.  * An error handler that is briefly installed as we are changing the
  27.  * keyboard input focus.  It ignores BadMatch errors from SetInputFocus
  28.  * protocol requests.
  29.  */
  30.  
  31. static int
  32. xoIgnoreFocusError (display, err)
  33.     Display        *display;
  34.     XErrorEvent    *err;
  35. {
  36.     DBUG_ENTER ("xoIgnoreFocusError");
  37.     if (!err)
  38.         DBUG_RETURN (0);
  39.     if (err->error_code == BadMatch &&
  40.         err->request_code == X_SetInputFocus)
  41.     {
  42.         DBUG_PRINT ("focus", ("SetInputFocus error"));
  43.         xoInputFailed = True;
  44.     }
  45.     else
  46.     {
  47.         (*oldHandler) (display, err);
  48.     }
  49.     DBUG_RETURN (0);
  50. }
  51.  
  52. /*
  53.  * Determine if we'll accept the keyboard focus.  We accept it only if
  54.  * the window is visible.
  55.  */
  56.  
  57. Boolean
  58. XoFocusAccept (gw, t)
  59.     Widget          gw;
  60.     Time           *t;
  61. {
  62.     DBUG_ENTER ("xoAcceptFocus");
  63.     if (!gw || !t)
  64.         DBUG_RETURN (False);
  65.     if (!XtIsRealized (gw))
  66.     {
  67.         DBUG_PRINT ("focus", ("Widget %s not realized", XoName (gw)));
  68.         DBUG_RETURN (False);
  69.     }
  70.     /*
  71.      * We want to install a new error handler that ignores any
  72.      * SetInputFocus errors.  So we first make sure we've sent and
  73.      * received all messages -- just in case there is a lingering error.
  74.      * We install the handler, call the focus change, then Sync again to
  75.      * make sure this request has gone out and the reply come back.  We
  76.      * the restore the error handler.
  77.      */
  78.     XSync (XtDisplay (gw), False);
  79.     oldHandler = XSetErrorHandler (xoIgnoreFocusError);
  80.     xoInputFailed = False;
  81.     XSetInputFocus (XtDisplay (gw), XtWindow (gw),
  82.             RevertToPointerRoot, *t);
  83.     XSync (XtDisplay (gw), False);
  84.     (void) XSetErrorHandler (oldHandler);
  85.  
  86.     if (xoInputFailed)
  87.     {
  88.         DBUG_PRINT ("focus", ("Error setting focus to %s", XoName (gw)));
  89.         /*
  90.          * (void) XoFocusAccept (XoFocusWidgetGet (XoShellGet (gw)),
  91.          * t);
  92.          */
  93.         DBUG_RETURN (False);
  94.     }
  95.     else
  96.     {
  97.         XoFocusWidgetSet (XoShellGet (gw), gw);
  98.         DBUG_RETURN (True);
  99.     }
  100. }
  101. --
  102. Pete Ware / Ohio State University /
  103.            ware@cis.ohio-state.edu
  104.            (614) 538-0965
  105. Too bad the Nike slogan isn't "Just Do It Right" instead of "Just Do It"
  106.