home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / lib / Xt / TMgrab.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-21  |  8.5 KB  |  293 lines

  1. /* $XConsortium: TMgrab.c,v 1.9 92/12/24 10:41:47 converse Exp $ */
  2. /*LINTLIBRARY*/
  3.  
  4. /***********************************************************
  5. Copyright 1987, 1988 by Digital Equipment Corporation, Maynard, Massachusetts,
  6. and the Massachusetts Institute of Technology, Cambridge, Massachusetts.
  7.  
  8.                         All Rights Reserved
  9.  
  10. Permission to use, copy, modify, and distribute this software and its 
  11. documentation for any purpose and without fee is hereby granted, 
  12. provided that the above copyright notice appear in all copies and that
  13. both that copyright notice and this permission notice appear in 
  14. supporting documentation, and that the names of Digital or MIT not be
  15. used in advertising or publicity pertaining to distribution of the
  16. software without specific, written prior permission.  
  17.  
  18. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  19. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  20. DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  21. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  22. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  23. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  24. SOFTWARE.
  25.  
  26. ******************************************************************/
  27.  
  28. #include "IntrinsicI.h"
  29.  
  30. typedef struct _GrabActionRec {
  31.     struct _GrabActionRec* next;
  32.     XtActionProc action_proc;
  33.     Boolean owner_events;
  34.     unsigned int event_mask;
  35.     int pointer_mode, keyboard_mode;
  36. } GrabActionRec;
  37.  
  38. static GrabActionRec *grabActionList = NULL;
  39.  
  40. static void GrabAllCorrectKeys(widget, typeMatch, modMatch, grabP)
  41.     Widget widget;
  42.     TMTypeMatch typeMatch;
  43.     TMModifierMatch modMatch;
  44.     GrabActionRec* grabP;
  45. {
  46.     Display *dpy = XtDisplay(widget);
  47.     KeyCode *keycodes, *keycodeP;
  48.     Cardinal keycount;
  49.     Modifiers careOn = 0;
  50.     Modifiers careMask = 0;
  51.  
  52.     if (modMatch->lateModifiers) {
  53.     Boolean resolved;
  54.     resolved = _XtComputeLateBindings(dpy, modMatch->lateModifiers,
  55.                       &careOn, &careMask);
  56.     if (!resolved) return;
  57.     }
  58.     careOn |= modMatch->modifiers;
  59.     careMask |= modMatch->modifierMask;
  60.  
  61.     XtKeysymToKeycodeList(
  62.         dpy,
  63.         (KeySym)typeMatch->eventCode,
  64.         &keycodes,
  65.         &keycount
  66.              );
  67.     if (keycount == 0) return;
  68.     for (keycodeP = keycodes; keycount--; keycodeP++) {
  69.     if (modMatch->standard) {
  70.         /* find standard modifiers that produce this keysym */
  71.         KeySym keysym;
  72.         int std_mods, least_mod;
  73.         Modifiers modifiers_return;
  74.         XtTranslateKeycode( dpy, *keycodeP, (Modifiers)0,
  75.                     &modifiers_return, &keysym );
  76.         if (careOn & modifiers_return)
  77.         return;
  78.         if (keysym == typeMatch->eventCode) {
  79.         XtGrabKey(widget, *keycodeP, careOn,
  80.               grabP->owner_events,
  81.               grabP->pointer_mode,
  82.               grabP->keyboard_mode
  83.             );
  84.         /* continue; */        /* grab all modifier combinations */
  85.         }
  86.         least_mod = modifiers_return & (~modifiers_return + 1);
  87.         for (std_mods = modifiers_return;
  88.          std_mods >= least_mod; std_mods--) {
  89.         Modifiers dummy;
  90.          /* check all useful combinations of modifier bits */
  91.         if (modifiers_return & std_mods &&
  92.             !(~modifiers_return & std_mods)) {
  93.             XtTranslateKeycode( dpy, *keycodeP,
  94.                     (Modifiers)std_mods,
  95.                     &dummy, &keysym );
  96.             if (keysym == typeMatch->eventCode) {
  97.             XtGrabKey(widget, *keycodeP,
  98.                   careOn | (Modifiers) std_mods,
  99.                   grabP->owner_events,
  100.                   grabP->pointer_mode,
  101.                   grabP->keyboard_mode
  102.                 );
  103.             /* break; */    /* grab all modifier combinations */
  104.             }
  105.         }
  106.         }
  107.     } else /* !event->standard */ {
  108.         XtGrabKey(widget, *keycodeP, careOn,
  109.               grabP->owner_events,
  110.               grabP->pointer_mode,
  111.               grabP->keyboard_mode
  112.             );
  113.     }
  114.     }
  115.     XtFree((char *)keycodes);
  116. }
  117.  
  118. typedef struct {
  119.     TMShortCard count;
  120.     Widget    widget;
  121.     GrabActionRec *grabP;
  122. }DoGrabRec;
  123.  
  124. static Boolean DoGrab(state, data)
  125.     StatePtr        state;
  126.     XtPointer        data;
  127. {
  128.     DoGrabRec        *doGrabP = (DoGrabRec *)data;
  129.     GrabActionRec*     grabP = doGrabP->grabP;
  130.     Widget        widget = doGrabP->widget;
  131.     TMShortCard        count = doGrabP->count;
  132.     TMShortCard        typeIndex = state->typeIndex;
  133.     TMShortCard        modIndex = state->modIndex;
  134.     ActionRec        *action;
  135.     TMTypeMatch        typeMatch = TMGetTypeMatch(typeIndex);
  136.     TMModifierMatch    modMatch = TMGetModifierMatch(modIndex);
  137.     Modifiers        careOn = 0;
  138.     Modifiers        careMask = 0;
  139.     Boolean        resolved;
  140.  
  141.     for (action = state->actions; action; action = action->next)
  142.       if (count == action->idx) break;
  143.     if (!action) return False;
  144.     
  145.     switch (typeMatch->eventType) {
  146.       case ButtonPress:
  147.       case ButtonRelease:
  148.     if (modMatch->lateModifiers) {
  149.         resolved = _XtComputeLateBindings(XtDisplay(widget),
  150.                           modMatch->lateModifiers,
  151.                           &careOn, &careMask);
  152.         if (!resolved) break;
  153.     }
  154.     careOn |= modMatch->modifiers;
  155.     XtGrabButton(
  156.              widget,
  157.              (unsigned) typeMatch->eventCode,
  158.              careOn,
  159.              grabP->owner_events,
  160.              grabP->event_mask,
  161.              grabP->pointer_mode,
  162.              grabP->keyboard_mode,
  163.              None,
  164.              None
  165.              );
  166.     break;
  167.     
  168.       case KeyPress:
  169.       case KeyRelease:
  170.     GrabAllCorrectKeys(widget, typeMatch, modMatch, grabP);
  171.     break;
  172.     
  173.       case EnterNotify:
  174.     break;
  175.     
  176.       default:
  177.     XtAppWarningMsg(XtWidgetToApplicationContext(widget),
  178.             "invalidPopup","unsupportedOperation",XtCXtToolkitError,
  179.             "Pop-up menu creation is only supported on Button, Key or EnterNotify events.",
  180.             (String *)NULL, (Cardinal *)NULL);
  181.     break;
  182.     }
  183.     return False;
  184. }
  185.  
  186. void _XtRegisterGrabs(widget)
  187.     Widget widget;
  188. {
  189.     XtTranslations     xlations = widget->core.tm.translations;
  190.     TMComplexStateTree     *stateTreePtr;
  191.     unsigned int     count;
  192.     TMShortCard        i;
  193.     TMBindData       bindData = (TMBindData) widget->core.tm.proc_table;
  194.     XtActionProc    *procs;
  195.  
  196.     if (! XtIsRealized(widget) || widget->core.being_destroyed)
  197.     return;
  198.  
  199.     /* walk the widget instance action bindings table looking for */
  200.     /* actions registered as grab actions. */
  201.     /* when you find one, do a grab on the triggering event */
  202.     
  203.     if (xlations == NULL) return;
  204.     stateTreePtr = (TMComplexStateTree *) xlations->stateTreeTbl;
  205.     if (*stateTreePtr == NULL) return;
  206.     for (i = 0; i < xlations->numStateTrees; i++, stateTreePtr++) {
  207.     if (bindData->simple.isComplex) 
  208.       procs = TMGetComplexBindEntry(bindData, i)->procs;
  209.     else 
  210.       procs = TMGetSimpleBindEntry(bindData, i)->procs;
  211.     for (count=0; count < (*stateTreePtr)->numQuarks; count++) {
  212.         GrabActionRec* grabP;
  213.         DoGrabRec      doGrab;
  214.         for (grabP = grabActionList; grabP != NULL; grabP = grabP->next) {
  215.         if (grabP->action_proc == procs[count]) {
  216.             /* we've found a "grabber" in the action table. Find the 
  217.              * states that call this action.  Note that if there is 
  218.              * more than one "grabber" in the action table, we end 
  219.              * up searching all of the states multiple times.
  220.              */
  221.             doGrab.widget = widget;
  222.             doGrab.grabP = grabP;
  223.             doGrab.count = count;
  224.             _XtTraverseStateTree((TMStateTree)*stateTreePtr,
  225.                      DoGrab,
  226.                      (XtPointer)&doGrab);
  227.         }
  228.         }
  229.     }
  230.     }
  231. }
  232.  
  233. #if NeedFunctionPrototypes
  234. void XtRegisterGrabAction(
  235.     XtActionProc action_proc, 
  236.     _XtBoolean owner_events,
  237.     unsigned int event_mask,
  238.     int pointer_mode,
  239.     int keyboard_mode
  240.     )
  241. #else
  242. void XtRegisterGrabAction(action_proc, owner_events, event_mask,
  243.               pointer_mode, keyboard_mode)
  244.     XtActionProc action_proc;
  245.     Boolean owner_events;
  246.     unsigned int event_mask;
  247.     int pointer_mode, keyboard_mode;
  248. #endif
  249. {
  250.     GrabActionRec* actionP;
  251.  
  252.     for (actionP = grabActionList; actionP != NULL; actionP = actionP->next) {
  253.     if (actionP->action_proc == action_proc) break;
  254.     }
  255.     if (actionP == NULL) {
  256.     actionP = XtNew(GrabActionRec);
  257.     actionP->action_proc = action_proc;
  258.     actionP->next = grabActionList;
  259.     grabActionList = actionP;
  260.     }
  261. #ifdef DEBUG
  262.     else
  263.     if (   actionP->owner_events != owner_events
  264.         || actionP->event_mask != event_mask
  265.         || actionP->pointer_mode != pointer_mode
  266.         || actionP->keyboard_mode != keyboard_mode) {
  267.         Cardinal n = 0;
  268.         XtWarningMsg(
  269.         "argsReplaced", "xtRegisterGrabAction", XtCXtToolkitError,
  270.         "XtRegisterGrabAction called on same proc with different args",
  271.              NULL, &n);
  272.     }
  273. #endif /*DEBUG*/
  274.  
  275.     actionP->owner_events = owner_events;
  276.     actionP->event_mask = event_mask;
  277.     actionP->pointer_mode = pointer_mode;
  278.     actionP->keyboard_mode = keyboard_mode;
  279. }
  280.  
  281. /*ARGSUSED*/
  282. void _XtGrabInitialize(app)
  283.     XtAppContext    app;
  284. {
  285.     if (grabActionList == NULL)
  286.     XtRegisterGrabAction( XtMenuPopupAction, True,
  287.                   (unsigned)(ButtonPressMask | ButtonReleaseMask),
  288.                   GrabModeAsync,
  289.                   GrabModeAsync
  290.                 );
  291.  
  292. }
  293.