1444c061aSmrg/***********************************************************
2fdf6a26fSmrgCopyright (c) 1993, Oracle and/or its affiliates.
31477040fSmrg
41477040fSmrgPermission is hereby granted, free of charge, to any person obtaining a
51477040fSmrgcopy of this software and associated documentation files (the "Software"),
61477040fSmrgto deal in the Software without restriction, including without limitation
71477040fSmrgthe rights to use, copy, modify, merge, publish, distribute, sublicense,
81477040fSmrgand/or sell copies of the Software, and to permit persons to whom the
91477040fSmrgSoftware is furnished to do so, subject to the following conditions:
101477040fSmrg
111477040fSmrgThe above copyright notice and this permission notice (including the next
121477040fSmrgparagraph) shall be included in all copies or substantial portions of the
131477040fSmrgSoftware.
141477040fSmrg
151477040fSmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
161477040fSmrgIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
171477040fSmrgFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
181477040fSmrgTHE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
191477040fSmrgLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
201477040fSmrgFROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
211477040fSmrgDEALINGS IN THE SOFTWARE.
221477040fSmrg
231477040fSmrgCopyright 1987, 1988 by Digital Equipment Corporation, Maynard, Massachusetts.
24444c061aSmrg
25444c061aSmrg                        All Rights Reserved
26444c061aSmrg
27444c061aSmrgPermission to use, copy, modify, and distribute this software and its
28444c061aSmrgdocumentation for any purpose and without fee is hereby granted,
29444c061aSmrgprovided that the above copyright notice appear in all copies and that
30444c061aSmrgboth that copyright notice and this permission notice appear in
311477040fSmrgsupporting documentation, and that the name of Digital not be
32444c061aSmrgused in advertising or publicity pertaining to distribution of the
33444c061aSmrgsoftware without specific, written prior permission.
34444c061aSmrg
35444c061aSmrgDIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
36444c061aSmrgALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
37444c061aSmrgDIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
38444c061aSmrgANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
39444c061aSmrgWHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
40444c061aSmrgARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
41444c061aSmrgSOFTWARE.
42444c061aSmrg
43444c061aSmrg******************************************************************/
44444c061aSmrg
45444c061aSmrg/*
46444c061aSmrg
47444c061aSmrgCopyright 1987, 1988, 1998  The Open Group
48444c061aSmrg
49444c061aSmrgPermission to use, copy, modify, distribute, and sell this software and its
50444c061aSmrgdocumentation for any purpose is hereby granted without fee, provided that
51444c061aSmrgthe above copyright notice appear in all copies and that both that
52444c061aSmrgcopyright notice and this permission notice appear in supporting
53444c061aSmrgdocumentation.
54444c061aSmrg
55444c061aSmrgThe above copyright notice and this permission notice shall be included in
56444c061aSmrgall copies or substantial portions of the Software.
57444c061aSmrg
58444c061aSmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
59444c061aSmrgIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
60444c061aSmrgFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
61444c061aSmrgOPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
62444c061aSmrgAN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
63444c061aSmrgCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
64444c061aSmrg
65444c061aSmrgExcept as contained in this notice, the name of The Open Group shall not be
66444c061aSmrgused in advertising or otherwise to promote the sale, use or other dealings
67444c061aSmrgin this Software without prior written authorization from The Open Group.
68444c061aSmrg
69444c061aSmrg*/
70444c061aSmrg
71444c061aSmrg#ifdef HAVE_CONFIG_H
72444c061aSmrg#include <config.h>
73444c061aSmrg#endif
74444c061aSmrg#include "IntrinsicI.h"
75444c061aSmrg
76444c061aSmrgtypedef struct _GrabActionRec {
77a3bd7f05Smrg    struct _GrabActionRec *next;
78444c061aSmrg    XtActionProc action_proc;
79444c061aSmrg    Boolean owner_events;
80444c061aSmrg    unsigned int event_mask;
81444c061aSmrg    int pointer_mode, keyboard_mode;
82444c061aSmrg} GrabActionRec;
83444c061aSmrg
84444c061aSmrgstatic GrabActionRec *grabActionList = NULL;
85444c061aSmrg
86a3bd7f05Smrgstatic void
87a3bd7f05SmrgGrabAllCorrectKeys(Widget widget,
88a3bd7f05Smrg                   TMTypeMatch typeMatch,
89a3bd7f05Smrg                   TMModifierMatch modMatch, GrabActionRec * grabP)
90444c061aSmrg{
91444c061aSmrg    Display *dpy = XtDisplay(widget);
92444c061aSmrg    KeyCode *keycodes, *keycodeP;
93444c061aSmrg    Cardinal keycount;
94444c061aSmrg    Modifiers careOn = 0;
95444c061aSmrg    Modifiers careMask = 0;
96444c061aSmrg
97444c061aSmrg    if (modMatch->lateModifiers) {
98a3bd7f05Smrg        Boolean resolved;
99a3bd7f05Smrg
100a3bd7f05Smrg        resolved = _XtComputeLateBindings(dpy, modMatch->lateModifiers,
101a3bd7f05Smrg                                          &careOn, &careMask);
102a3bd7f05Smrg        if (!resolved)
103a3bd7f05Smrg            return;
104444c061aSmrg    }
105a3bd7f05Smrg    careOn = (careOn | (Modifiers) modMatch->modifiers);
106a3bd7f05Smrg    careMask = (careMask | (Modifiers) modMatch->modifierMask);
107444c061aSmrg
1080568f49bSmrg    keycodes = NULL;
109a3bd7f05Smrg    XtKeysymToKeycodeList(dpy,
110a3bd7f05Smrg                          (KeySym) typeMatch->eventCode, &keycodes, &keycount);
1110568f49bSmrg    if (keycount == 0) {
112a3bd7f05Smrg        XtFree((char *) keycodes);
113a3bd7f05Smrg        return;
1140568f49bSmrg    }
115444c061aSmrg    for (keycodeP = keycodes; keycount--; keycodeP++) {
116a3bd7f05Smrg        if (modMatch->standard) {
117a3bd7f05Smrg            /* find standard modifiers that produce this keysym */
118a3bd7f05Smrg            KeySym keysym;
119a3bd7f05Smrg            int std_mods, least_mod;
120a3bd7f05Smrg            Modifiers modifiers_return;
121a3bd7f05Smrg
122a3bd7f05Smrg            XtTranslateKeycode(dpy, *keycodeP, (Modifiers) 0,
123a3bd7f05Smrg                               &modifiers_return, &keysym);
124a3bd7f05Smrg            if (careOn & modifiers_return) {
125a3bd7f05Smrg                XtFree((char *) keycodes);
126a3bd7f05Smrg                return;
127a3bd7f05Smrg            }
128a3bd7f05Smrg            if (keysym == typeMatch->eventCode) {
129a3bd7f05Smrg                XtGrabKey(widget, *keycodeP, careOn,
130a3bd7f05Smrg                          grabP->owner_events,
131a3bd7f05Smrg                          grabP->pointer_mode, grabP->keyboard_mode);
132a3bd7f05Smrg                /* continue; *//* grab all modifier combinations */
133a3bd7f05Smrg            }
134a3bd7f05Smrg            least_mod = (int) (modifiers_return & (~modifiers_return + 1));
135a3bd7f05Smrg            for (std_mods = (int) modifiers_return;
136a3bd7f05Smrg                 std_mods >= least_mod; std_mods--) {
137a3bd7f05Smrg                Modifiers dummy;
138a3bd7f05Smrg
139a3bd7f05Smrg                /* check all useful combinations of modifier bits */
140a3bd7f05Smrg                if ((modifiers_return & (Modifiers) std_mods) &&
141a3bd7f05Smrg                    !(~modifiers_return & (Modifiers) std_mods)) {
142a3bd7f05Smrg                    XtTranslateKeycode(dpy, *keycodeP,
143a3bd7f05Smrg                                       (Modifiers) std_mods, &dummy, &keysym);
144a3bd7f05Smrg                    if (keysym == typeMatch->eventCode) {
145a3bd7f05Smrg                        XtGrabKey(widget, *keycodeP,
146a3bd7f05Smrg                                  careOn | (Modifiers) std_mods,
147a3bd7f05Smrg                                  grabP->owner_events,
148a3bd7f05Smrg                                  grabP->pointer_mode, grabP->keyboard_mode);
149a3bd7f05Smrg                        /* break; *//* grab all modifier combinations */
150a3bd7f05Smrg                    }
151a3bd7f05Smrg                }
152a3bd7f05Smrg            }
153a3bd7f05Smrg        }
154a3bd7f05Smrg        else {                  /* !event->standard */
155a3bd7f05Smrg
156a3bd7f05Smrg            XtGrabKey(widget, *keycodeP, careOn,
157a3bd7f05Smrg                      grabP->owner_events,
158a3bd7f05Smrg                      grabP->pointer_mode, grabP->keyboard_mode);
159a3bd7f05Smrg        }
160444c061aSmrg    }
161a3bd7f05Smrg    XtFree((char *) keycodes);
162444c061aSmrg}
163444c061aSmrg
164444c061aSmrgtypedef struct {
165444c061aSmrg    TMShortCard count;
166a3bd7f05Smrg    Widget widget;
167444c061aSmrg    GrabActionRec *grabP;
168a3bd7f05Smrg} DoGrabRec;
169444c061aSmrg
170a3bd7f05Smrgstatic Boolean
171a3bd7f05SmrgDoGrab(StatePtr state, XtPointer data)
172444c061aSmrg{
173a3bd7f05Smrg    /* *INDENT-EQLS* */
174a3bd7f05Smrg    DoGrabRec *doGrabP     = (DoGrabRec *) data;
175a3bd7f05Smrg    GrabActionRec *grabP   = doGrabP->grabP;
176a3bd7f05Smrg    Widget widget          = doGrabP->widget;
177a3bd7f05Smrg    TMShortCard count      = doGrabP->count;
178a3bd7f05Smrg    TMShortCard typeIndex  = state->typeIndex;
179a3bd7f05Smrg    TMShortCard modIndex   = state->modIndex;
180a3bd7f05Smrg    ActionRec *action;
181a3bd7f05Smrg    TMTypeMatch typeMatch;
182a3bd7f05Smrg    TMModifierMatch modMatch;
183a3bd7f05Smrg    Modifiers careOn = 0;
184a3bd7f05Smrg    Modifiers careMask = 0;
185444c061aSmrg
186444c061aSmrg    LOCK_PROCESS;
187444c061aSmrg    typeMatch = TMGetTypeMatch(typeIndex);
188444c061aSmrg    modMatch = TMGetModifierMatch(modIndex);
189444c061aSmrg
190444c061aSmrg    for (action = state->actions; action; action = action->next)
191a3bd7f05Smrg        if (count == action->idx)
192a3bd7f05Smrg            break;
193444c061aSmrg    if (!action) {
194a3bd7f05Smrg        UNLOCK_PROCESS;
195a3bd7f05Smrg        return False;
196444c061aSmrg    }
197444c061aSmrg
198444c061aSmrg    switch (typeMatch->eventType) {
199a3bd7f05Smrg    case ButtonPress:
200a3bd7f05Smrg    case ButtonRelease:
201a3bd7f05Smrg        if (modMatch->lateModifiers) {
202a3bd7f05Smrg            Boolean resolved = _XtComputeLateBindings(XtDisplay(widget),
203a3bd7f05Smrg                                                      modMatch->lateModifiers,
204a3bd7f05Smrg                                                      &careOn, &careMask);
205a3bd7f05Smrg
206a3bd7f05Smrg            if (!resolved)
207a3bd7f05Smrg                break;
208a3bd7f05Smrg        }
209a3bd7f05Smrg        careOn = (careOn | (Modifiers) modMatch->modifiers);
210a3bd7f05Smrg        XtGrabButton(widget,
211a3bd7f05Smrg                     (int) typeMatch->eventCode,
212a3bd7f05Smrg                     careOn,
213a3bd7f05Smrg                     grabP->owner_events,
214a3bd7f05Smrg                     grabP->event_mask,
215a3bd7f05Smrg                     grabP->pointer_mode,
216a3bd7f05Smrg                     grabP->keyboard_mode,
217a3bd7f05Smrg                     None,
218a3bd7f05Smrg                     None);
219a3bd7f05Smrg        break;
220a3bd7f05Smrg
221a3bd7f05Smrg    case KeyPress:
222a3bd7f05Smrg    case KeyRelease:
223a3bd7f05Smrg        GrabAllCorrectKeys(widget, typeMatch, modMatch, grabP);
224a3bd7f05Smrg        break;
225a3bd7f05Smrg
226a3bd7f05Smrg    case EnterNotify:
227a3bd7f05Smrg        break;
228a3bd7f05Smrg
229a3bd7f05Smrg    default:
230a3bd7f05Smrg        XtAppWarningMsg(XtWidgetToApplicationContext(widget),
231a3bd7f05Smrg                        "invalidPopup", "unsupportedOperation",
232a3bd7f05Smrg                        XtCXtToolkitError,
233a3bd7f05Smrg                        "Pop-up menu creation is only supported on Button, Key or EnterNotify events.",
234a3bd7f05Smrg                        NULL, NULL);
235a3bd7f05Smrg        break;
236444c061aSmrg    }
237444c061aSmrg    UNLOCK_PROCESS;
238444c061aSmrg    return False;
239444c061aSmrg}
240444c061aSmrg
241a3bd7f05Smrgvoid
242a3bd7f05Smrg_XtRegisterGrabs(Widget widget)
243444c061aSmrg{
244a3bd7f05Smrg    XtTranslations xlations = widget->core.tm.translations;
245a3bd7f05Smrg    TMComplexStateTree *stateTreePtr;
246a3bd7f05Smrg    unsigned int count;
247a3bd7f05Smrg    TMShortCard i;
248a3bd7f05Smrg    TMBindData bindData = (TMBindData) widget->core.tm.proc_table;
249a3bd7f05Smrg    XtActionProc *procs;
250444c061aSmrg
251a3bd7f05Smrg    if (!XtIsRealized(widget) || widget->core.being_destroyed)
252a3bd7f05Smrg        return;
253444c061aSmrg
254444c061aSmrg    /* walk the widget instance action bindings table looking for */
255444c061aSmrg    /* actions registered as grab actions. */
256444c061aSmrg    /* when you find one, do a grab on the triggering event */
257444c061aSmrg
258a3bd7f05Smrg    if (xlations == NULL)
259a3bd7f05Smrg        return;
260444c061aSmrg    stateTreePtr = (TMComplexStateTree *) xlations->stateTreeTbl;
261a3bd7f05Smrg    if (*stateTreePtr == NULL)
262a3bd7f05Smrg        return;
263444c061aSmrg    for (i = 0; i < xlations->numStateTrees; i++, stateTreePtr++) {
264a3bd7f05Smrg        if (bindData->simple.isComplex)
265a3bd7f05Smrg            procs = TMGetComplexBindEntry(bindData, i)->procs;
266a3bd7f05Smrg        else
267a3bd7f05Smrg            procs = TMGetSimpleBindEntry(bindData, i)->procs;
268a3bd7f05Smrg        for (count = 0; count < (*stateTreePtr)->numQuarks; count++) {
269a3bd7f05Smrg            GrabActionRec *grabP;
270a3bd7f05Smrg            DoGrabRec doGrab;
271a3bd7f05Smrg
272a3bd7f05Smrg            LOCK_PROCESS;
273a3bd7f05Smrg            for (grabP = grabActionList; grabP != NULL; grabP = grabP->next) {
274a3bd7f05Smrg                if (grabP->action_proc == procs[count]) {
275a3bd7f05Smrg                    /* we've found a "grabber" in the action table. Find the
276a3bd7f05Smrg                     * states that call this action.  Note that if there is
277a3bd7f05Smrg                     * more than one "grabber" in the action table, we end
278a3bd7f05Smrg                     * up searching all of the states multiple times.
279a3bd7f05Smrg                     */
280a3bd7f05Smrg                    doGrab.widget = widget;
281a3bd7f05Smrg                    doGrab.grabP = grabP;
282a3bd7f05Smrg                    doGrab.count = (TMShortCard) count;
283a3bd7f05Smrg                    _XtTraverseStateTree((TMStateTree) *stateTreePtr,
284a3bd7f05Smrg                                         DoGrab, (XtPointer) &doGrab);
285a3bd7f05Smrg                }
286a3bd7f05Smrg            }
287a3bd7f05Smrg            UNLOCK_PROCESS;
288a3bd7f05Smrg        }
289444c061aSmrg    }
290444c061aSmrg}
291444c061aSmrg
292a3bd7f05Smrgvoid
293a3bd7f05SmrgXtRegisterGrabAction(XtActionProc action_proc,
294a3bd7f05Smrg                     _XtBoolean owner_events,
295a3bd7f05Smrg                     unsigned int event_mask,
296a3bd7f05Smrg                     int pointer_mode,
297a3bd7f05Smrg                     int keyboard_mode)
298444c061aSmrg{
299a3bd7f05Smrg    GrabActionRec *actionP;
300444c061aSmrg
301444c061aSmrg    LOCK_PROCESS;
302444c061aSmrg    for (actionP = grabActionList; actionP != NULL; actionP = actionP->next) {
303a3bd7f05Smrg        if (actionP->action_proc == action_proc)
304a3bd7f05Smrg            break;
305444c061aSmrg    }
306444c061aSmrg    if (actionP == NULL) {
307a3bd7f05Smrg        actionP = XtNew(GrabActionRec);
308a3bd7f05Smrg        actionP->action_proc = action_proc;
309a3bd7f05Smrg        actionP->next = grabActionList;
310a3bd7f05Smrg        grabActionList = actionP;
311444c061aSmrg    }
312444c061aSmrg#ifdef DEBUG
313a3bd7f05Smrg    else if (actionP->owner_events != owner_events
314a3bd7f05Smrg             || actionP->event_mask != event_mask
315a3bd7f05Smrg             || actionP->pointer_mode != pointer_mode
316a3bd7f05Smrg             || actionP->keyboard_mode != keyboard_mode) {
317a3bd7f05Smrg        Cardinal n = 0;
318a3bd7f05Smrg
319a3bd7f05Smrg        XtWarningMsg("argsReplaced", "xtRegisterGrabAction", XtCXtToolkitError,
320a3bd7f05Smrg                     "XtRegisterGrabAction called on same proc with different args",
321a3bd7f05Smrg                     NULL, &n);
322a3bd7f05Smrg    }
323444c061aSmrg#endif /*DEBUG*/
324a3bd7f05Smrg        actionP->owner_events = (Boolean) owner_events;
325444c061aSmrg    actionP->event_mask = event_mask;
326444c061aSmrg    actionP->pointer_mode = pointer_mode;
327444c061aSmrg    actionP->keyboard_mode = keyboard_mode;
328444c061aSmrg    UNLOCK_PROCESS;
329444c061aSmrg}
330444c061aSmrg
331a3bd7f05Smrgvoid
332a3bd7f05Smrg_XtGrabInitialize(XtAppContext app _X_UNUSED)
333444c061aSmrg{
334444c061aSmrg    LOCK_PROCESS;
335444c061aSmrg    if (grabActionList == NULL)
336a3bd7f05Smrg        XtRegisterGrabAction(XtMenuPopupAction, True,
337a3bd7f05Smrg                             (unsigned) (ButtonPressMask | ButtonReleaseMask),
338a3bd7f05Smrg                             GrabModeAsync, GrabModeAsync);
339444c061aSmrg    UNLOCK_PROCESS;
340444c061aSmrg
341444c061aSmrg}
342