home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / lib / Xt / TMgrab.c.orig < prev    next >
Encoding:
Text File  |  1991-04-12  |  7.9 KB  |  268 lines

  1. /* $XConsortium: TMgrab.c,v 1.4 91/04/12 14:02:15 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.     XtKeysymToKeycodeList(
  50.         dpy,
  51.         (KeySym)typeMatch->eventCode,
  52.         &keycodes,
  53.         &keycount
  54.              );
  55.     if (keycount == 0) return;
  56.     for (keycodeP = keycodes; keycount--; keycodeP++) {
  57.     if (modMatch->standard) {
  58.         /* find standard modifiers that produce this keysym */
  59.         KeySym keysym;
  60.         int std_mods, least_mod = 1;
  61.         Modifiers modifiers_return;
  62.         XtTranslateKeycode( dpy, *keycodeP, (Modifiers)0,
  63.                     &modifiers_return, &keysym );
  64.         if (keysym == typeMatch->eventCode) {
  65.         XtGrabKey(widget, *keycodeP,
  66.               (unsigned)modMatch->modifiers,
  67.               grabP->owner_events,
  68.               grabP->pointer_mode,
  69.               grabP->keyboard_mode
  70.             );
  71.         /* continue; */        /* grab all modifier combinations */
  72.         }
  73.         while ((least_mod & modifiers_return)==0) least_mod <<= 1;        
  74.         for (std_mods = modifiers_return;
  75.          std_mods >= least_mod; std_mods--) {
  76.          /* check all useful combinations of modifier bits */
  77.         if (modifiers_return & std_mods) {
  78.             XtTranslateKeycode( dpy, *keycodeP,
  79.                     (Modifiers)std_mods,
  80.                     &modifiers_return, &keysym );
  81.             if (keysym == typeMatch->eventCode) {
  82.             XtGrabKey(widget, *keycodeP,
  83.                   (unsigned)modMatch->modifiers | std_mods,
  84.                   grabP->owner_events,
  85.                   grabP->pointer_mode,
  86.                   grabP->keyboard_mode
  87.                 );
  88.             /* break; */    /* grab all modifier combinations */
  89.             }
  90.         }
  91.         }
  92.     } else /* !event->standard */ {
  93.         XtGrabKey(widget, *keycodeP,
  94.               (unsigned)modMatch->modifiers,
  95.               grabP->owner_events,
  96.               grabP->pointer_mode,
  97.               grabP->keyboard_mode
  98.             );
  99.     }
  100.     }
  101.     XtFree((char *)keycodes);
  102. }
  103.  
  104. typedef struct {
  105.     TMShortCard count;
  106.     Widget    widget;
  107.     GrabActionRec *grabP;
  108. }DoGrabRec;
  109.  
  110. static Boolean DoGrab(state, data)
  111.     StatePtr        state;
  112.     XtPointer        data;
  113. {
  114.     DoGrabRec        *doGrabP = (DoGrabRec *)data;
  115.     GrabActionRec*     grabP = doGrabP->grabP;
  116.     Widget        widget = doGrabP->widget;
  117.     TMShortCard        count = doGrabP->count;
  118.     TMShortCard        typeIndex = state->typeIndex;
  119.     TMShortCard        modIndex = state->modIndex;
  120.     ActionRec        *action;
  121.     TMTypeMatch        typeMatch = TMGetTypeMatch(typeIndex);
  122.     TMModifierMatch    modMatch = TMGetModifierMatch(modIndex);
  123.  
  124.     for (action = state->actions; action; action = action->next)
  125.       if (count == action->idx) break;
  126.     if (!action) return False;
  127.     
  128.     switch (typeMatch->eventType) {
  129.       case ButtonPress:
  130.       case ButtonRelease:
  131.     XtGrabButton(
  132.              widget,
  133.              (unsigned) typeMatch->eventCode,
  134.              (unsigned) modMatch->modifiers,
  135.              grabP->owner_events,
  136.              grabP->event_mask,
  137.              grabP->pointer_mode,
  138.              grabP->keyboard_mode,
  139.              None,
  140.              None
  141.              );
  142.     break;
  143.     
  144.       case KeyPress:
  145.       case KeyRelease:
  146.     GrabAllCorrectKeys(widget, typeMatch, modMatch, grabP);
  147.     break;
  148.     
  149.       case EnterNotify:
  150.     break;
  151.     
  152.       default:
  153.     XtAppWarningMsg(XtWidgetToApplicationContext(widget),
  154.             "invalidPopup","unsupportedOperation",XtCXtToolkitError,
  155.             "Pop-up menu creation is only supported on Button, Key or EnterNotify events.",
  156.             (String *)NULL, (Cardinal *)NULL);
  157.     break;
  158.     }
  159.     return False;
  160. }
  161.  
  162. void _XtRegisterGrabs(widget)
  163.     Widget widget;
  164. {
  165.     XtTranslations     xlations = widget->core.tm.translations;
  166.     TMComplexStateTree     *stateTreePtr;
  167.     unsigned int     count;
  168.     TMShortCard        i;
  169.     TMBindData       bindData = (TMBindData) widget->core.tm.proc_table;
  170.     XtActionProc    *procs;
  171.  
  172.     if (! XtIsRealized(widget) || widget->core.being_destroyed)
  173.     return;
  174.  
  175.     /* walk the widget instance action bindings table looking for */
  176.     /* actions registered as grab actions. */
  177.     /* when you find one, do a grab on the triggering event */
  178.     
  179.     if (xlations == NULL) return;
  180.     stateTreePtr = (TMComplexStateTree *) xlations->stateTreeTbl;
  181.     if (*stateTreePtr == NULL) return;
  182.     for (i = 0; i < xlations->numStateTrees; i++, stateTreePtr++) {
  183.     if (bindData->simple.isComplex) 
  184.       procs = TMGetComplexBindEntry(bindData, i)->procs;
  185.     else 
  186.       procs = TMGetSimpleBindEntry(bindData, i)->procs;
  187.     for (count=0; count < (*stateTreePtr)->numQuarks; count++) {
  188.         GrabActionRec* grabP;
  189.         DoGrabRec      doGrab;
  190.         for (grabP = grabActionList; grabP != NULL; grabP = grabP->next) {
  191.         if (grabP->action_proc == procs[count]) {
  192.             /* we've found a "grabber" in the action table. Find the 
  193.              * states that call this action.  Note that if there is 
  194.              * more than one "grabber" in the action table, we end 
  195.              * up searching all of the states multiple times.
  196.              */
  197.             doGrab.widget = widget;
  198.             doGrab.grabP = grabP;
  199.             doGrab.count = count;
  200.             _XtTraverseStateTree((TMStateTree)*stateTreePtr,
  201.                      DoGrab,
  202.                      (XtPointer)&doGrab);
  203.         }
  204.         }
  205.     }
  206.     }
  207. }
  208.  
  209. #if NeedFunctionPrototypes
  210. void XtRegisterGrabAction(
  211.     XtActionProc action_proc, 
  212.     _XtBoolean owner_events,
  213.     unsigned int event_mask,
  214.     int pointer_mode,
  215.     int keyboard_mode
  216.     )
  217. #else
  218. void XtRegisterGrabAction(action_proc, owner_events, event_mask,
  219.               pointer_mode, keyboard_mode)
  220.     XtActionProc action_proc;
  221.     Boolean owner_events;
  222.     unsigned int event_mask;
  223.     int pointer_mode, keyboard_mode;
  224. #endif
  225. {
  226.     GrabActionRec* actionP;
  227.  
  228.     for (actionP = grabActionList; actionP != NULL; actionP = actionP->next) {
  229.     if (actionP->action_proc == action_proc) break;
  230.     }
  231.     if (actionP == NULL) {
  232.     actionP = XtNew(GrabActionRec);
  233.     actionP->action_proc = action_proc;
  234.     actionP->next = grabActionList;
  235.     grabActionList = actionP;
  236.     }
  237. #ifdef DEBUG
  238.     else
  239.     if (   actionP->owner_events != owner_events
  240.         || actionP->event_mask != event_mask
  241.         || actionP->pointer_mode != pointer_mode
  242.         || actionP->keyboard_mode != keyboard_mode) {
  243.         XtWarningMsg(
  244.         "argsReplaced", "xtRegisterGrabAction", XtCXtToolkitError,
  245.         "XtRegisterGrabAction called on same proc with different args"
  246.             );
  247.     }
  248. #endif /*DEBUG*/
  249.  
  250.     actionP->owner_events = owner_events;
  251.     actionP->event_mask = event_mask;
  252.     actionP->pointer_mode = pointer_mode;
  253.     actionP->keyboard_mode = keyboard_mode;
  254. }
  255.  
  256. /*ARGSUSED*/
  257. void _XtGrabInitialize(app)
  258.     XtAppContext    app;
  259. {
  260.     if (grabActionList == NULL)
  261.     XtRegisterGrabAction( XtMenuPopupAction, True,
  262.                   (unsigned)(ButtonPressMask | ButtonReleaseMask),
  263.                   GrabModeAsync,
  264.                   GrabModeAsync
  265.                 );
  266.  
  267. }
  268.