home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / lib / Xt / EventUtil.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-06-13  |  5.0 KB  |  184 lines

  1. /* $XConsortium: EventUtil.c,v 1.9 91/06/13 18:15:27 converse Exp $ */
  2.  
  3. /********************************************************
  4.  
  5. Copyright (c) 1988 by Hewlett-Packard Company
  6. Copyright (c) 1987, 1988, 1989 by Digital Equipment Corporation, Maynard, 
  7.               Massachusetts, and the Massachusetts Institute of Technology, 
  8.               Cambridge, Massachusetts
  9.  
  10. Permission to use, copy, modify, and distribute this software 
  11. and its documentation for any purpose and without fee is hereby 
  12. granted, provided that the above copyright notice appear in all 
  13. copies and that both that copyright notice and this permission 
  14. notice appear in supporting documentation, and that the names of 
  15. Hewlett-Packard, Digital or  M.I.T.  not be used in advertising or 
  16. publicity pertaining to distribution of the software without specific, 
  17. written prior permission.
  18.  
  19. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  20. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  21. DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  22. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  23. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  24. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  25. SOFTWARE.
  26.  
  27. ********************************************************/
  28.  
  29. #include "IntrinsicI.h"
  30. #include "PassivGraI.h"
  31. #include "StringDefs.h"
  32.  
  33. static XContext     perWidgetInputContext = 0;
  34.  
  35. void _XtFreePerWidgetInput(w, pwi)
  36.     Widget         w;
  37.     XtPerWidgetInput    pwi;
  38. {
  39.     XDeleteContext(XtDisplay(w), 
  40.            (Window)w,
  41.            perWidgetInputContext);
  42.     
  43.     XtFree((char *)pwi);
  44. }
  45.  
  46. /*
  47.  * This routine gets the passive list associated with the widget
  48.  * from the context manager.
  49.  */
  50. #if NeedFunctionPrototypes
  51. XtPerWidgetInput _XtGetPerWidgetInput(
  52.     Widget    widget,
  53.     _XtBoolean    create
  54.     )
  55. #else
  56. XtPerWidgetInput _XtGetPerWidgetInput(widget, create)
  57.     Widget    widget;
  58.     Boolean    create;
  59. #endif
  60. {
  61.     XtPerWidgetInput    pwi = NULL;
  62.     Display        *dpy = widget->core.screen->display;
  63.     
  64.     if (! perWidgetInputContext)
  65.       perWidgetInputContext = XUniqueContext();
  66.     
  67.     if (XFindContext(dpy, 
  68.              (Window)widget, 
  69.              perWidgetInputContext, 
  70.              (XPointer *)&pwi) &&
  71.     create) 
  72.       {
  73.       pwi = (XtPerWidgetInput) 
  74.         XtMalloc((unsigned) sizeof(XtPerWidgetInputRec));
  75.       
  76.       pwi->focusKid = NULL;
  77.       pwi->queryEventDescendant = NULL;
  78.       pwi->focalPoint = XtUnrelated;
  79.       pwi->keyList =
  80.         pwi->ptrList = NULL;
  81.  
  82.       pwi->haveFocus =
  83.           pwi->map_handler_added =
  84.           pwi->realize_handler_added = 
  85.               pwi->active_handler_added = FALSE;
  86.       
  87.       XtAddCallback(widget, XtNdestroyCallback, 
  88.             _XtDestroyServerGrabs, (XtPointer)pwi);
  89.  
  90.       (void) XSaveContext(dpy, 
  91.                   (Window)widget, 
  92.                   perWidgetInputContext, 
  93.                   (char *) pwi);
  94.       }
  95.     return pwi;
  96. }
  97.  
  98.  
  99. void _XtFillAncestorList(listPtr, maxElemsPtr, numElemsPtr, start, breakWidget)
  100.     Widget    **listPtr;
  101.     int        *maxElemsPtr, *numElemsPtr;
  102.     Widget    start, breakWidget;
  103. {
  104. #define CACHESIZE 16
  105.     Cardinal    i;
  106.     Widget    w;
  107.     Widget    *trace = *listPtr;
  108.  
  109.     /* First time in, allocate the ancestor list */
  110.     if (trace == NULL) 
  111.       {
  112.       trace = (Widget *) XtMalloc(CACHESIZE * sizeof(Widget));
  113.       *maxElemsPtr = CACHESIZE;
  114.       }    
  115.     /* First fill in the ancestor list */
  116.  
  117.     trace[0] = start;
  118.  
  119.     for (i = 1, w = XtParent(start); 
  120.      w != NULL && !XtIsShell(trace[i-1]) && trace[i-1] != breakWidget; 
  121.      w = XtParent(w), i++) {
  122.     if (i == *maxElemsPtr) {
  123.         /* This should rarely happen, but if it does it'll probably
  124.            happen again, so grow the ancestor list */
  125.         *maxElemsPtr += CACHESIZE;
  126.         trace = (Widget *) XtRealloc((char*)trace,
  127.                      sizeof(Widget) * (*maxElemsPtr));
  128.     }
  129.     trace[i] = w;
  130.     }
  131.     *listPtr = trace;
  132.     *numElemsPtr = i;
  133. #undef CACHESIZE
  134. }  
  135.  
  136.  
  137. Widget _XtFindRemapWidget(event, widget, mask, pdi)
  138.     XEvent    *event;
  139.     Widget    widget;
  140.     EventMask    mask;
  141.     XtPerDisplayInput pdi;
  142. {
  143.     Widget        dspWidget = widget;
  144.     
  145.     if (!pdi->traceDepth || !(widget == pdi->trace[0]))
  146.       {
  147.       _XtFillAncestorList(&pdi->trace, &pdi->traceMax, 
  148.                   &pdi->traceDepth, widget, NULL);
  149.       pdi->focusWidget = NULL; /* invalidate the focus
  150.                       cache */
  151.       }
  152.     if (mask & (KeyPressMask | KeyReleaseMask))
  153.       dspWidget = _XtProcessKeyboardEvent((XKeyEvent*)event, widget, pdi);
  154.     else if (mask &(ButtonPressMask | ButtonReleaseMask))
  155.       dspWidget = _XtProcessPointerEvent((XButtonEvent*)event, widget,pdi);
  156.  
  157.     return dspWidget;
  158. }
  159.  
  160. void _XtUngrabBadGrabs(event, widget, mask, pdi)
  161.     XEvent    *event;
  162.     Widget    widget;
  163.     EventMask    mask;
  164.     XtPerDisplayInput pdi;
  165. {
  166.     XKeyEvent    * ke = (XKeyEvent *) event;
  167.  
  168.     if (mask & (KeyPressMask | KeyReleaseMask))
  169.       {
  170.       if (IsServerGrab(pdi->keyboard.grabType) &&
  171.           !_XtOnGrabList(pdi->keyboard.grab.widget,
  172.                  pdi->grabList))
  173.         XtUngrabKeyboard(widget, ke->time);
  174.  
  175.       }
  176.     else
  177.       {
  178.       if (IsServerGrab(pdi->pointer.grabType) &&
  179.           !_XtOnGrabList(pdi->pointer.grab.widget,
  180.                  pdi->grabList))
  181.         XtUngrabPointer(widget, ke->time);
  182.       }
  183. }
  184.