Mention Hochleitner's correlation.
[emacs.git] / lwlib / dispatch.c
blobb6bec2967ddf16aada26356645a10ea0a84f6170
1 /* Defines a function to find the Widget that XtDispatchEvent() would use.
2 Copyright (C) 1992 Lucid, Inc.
4 This file is part of the Lucid Widget Library.
6 The Lucid Widget Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 1, or (at your option)
9 any later version.
11 The Lucid Widget Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
20 /*
21 * The function XtWidgetToDispatchTo(), given an XEvent, returns the
22 * widget that XtDispatchEvent() would send that event to if called now.
23 * This file copies much code from the X11r4 Xt source, and is thus a
24 * portability problem. It also requires data structures defined in
25 * IntrinsicI.h, which is a non-exported Xt header file, so you can't
26 * compile this file unless you have the Xt sources online.
29 #include <IntrinsicI.h> /* Don't change this: see comments in Imakefile. */
30 #include <X11/Xatom.h>
31 #include "dispatch.h"
33 #include <X11/Xlib.h>
34 #include <X11/cursorfont.h>
35 #include <X11/Xutil.h>
37 #ifdef XlibSpecificationRelease
38 #if XlibSpecificationRelease >= 5
39 #define HAVE_X11R5
40 #endif
41 #endif
43 /* ## All of the code on this page was copied from the X11R5 lib/Xt/Event.c,
44 ## but is compatible with X11R4; the code in Event.c is different, but
45 ## functionally equivalent for our purposes.
48 #if __STDC__
49 #define Const const
50 #else
51 #define Const /**/
52 #endif
54 #define NonMaskableMask ((EventMask)0x80000000L)
56 #define COMP_EXPOSE (widget->core.widget_class->core_class.compress_exposure)
57 #define COMP_EXPOSE_TYPE (COMP_EXPOSE & 0x0f)
58 #define GRAPHICS_EXPOSE ((XtExposeGraphicsExpose & COMP_EXPOSE) || \
59 (XtExposeGraphicsExposeMerged & COMP_EXPOSE))
60 #define NO_EXPOSE (XtExposeNoExpose & COMP_EXPOSE)
63 /* -- lots of stuff we don't need to copy, omitted -- */
66 static EventMask Const masks[] = {
67 0, /* Error, should never see */
68 0, /* Reply, should never see */
69 KeyPressMask, /* KeyPress */
70 KeyReleaseMask, /* KeyRelease */
71 ButtonPressMask, /* ButtonPress */
72 ButtonReleaseMask, /* ButtonRelease */
73 PointerMotionMask /* MotionNotify */
74 | ButtonMotionMask,
75 EnterWindowMask, /* EnterNotify */
76 LeaveWindowMask, /* LeaveNotify */
77 FocusChangeMask, /* FocusIn */
78 FocusChangeMask, /* FocusOut */
79 KeymapStateMask, /* KeymapNotify */
80 ExposureMask, /* Expose */
81 NonMaskableMask, /* GraphicsExpose, in GC */
82 NonMaskableMask, /* NoExpose, in GC */
83 VisibilityChangeMask, /* VisibilityNotify */
84 SubstructureNotifyMask, /* CreateNotify */
85 StructureNotifyMask /* DestroyNotify */
86 | SubstructureNotifyMask,
87 StructureNotifyMask /* UnmapNotify */
88 | SubstructureNotifyMask,
89 StructureNotifyMask /* MapNotify */
90 | SubstructureNotifyMask,
91 SubstructureRedirectMask, /* MapRequest */
92 StructureNotifyMask /* ReparentNotify */
93 | SubstructureNotifyMask,
94 StructureNotifyMask /* ConfigureNotify */
95 | SubstructureNotifyMask,
96 SubstructureRedirectMask, /* ConfigureRequest */
97 StructureNotifyMask /* GravityNotify */
98 | SubstructureNotifyMask,
99 ResizeRedirectMask, /* ResizeRequest */
100 StructureNotifyMask /* CirculateNotify */
101 | SubstructureNotifyMask,
102 SubstructureRedirectMask, /* CirculateRequest */
103 PropertyChangeMask, /* PropertyNotify */
104 NonMaskableMask, /* SelectionClear */
105 NonMaskableMask, /* SelectionRequest */
106 NonMaskableMask, /* SelectionNotify */
107 ColormapChangeMask, /* ColormapNotify */
108 NonMaskableMask, /* ClientMessage */
109 NonMaskableMask /* MappingNotify */
112 #ifndef HAVE_X11R5
114 static /* in R5, this is not static, so we don't need to define it at all */
115 EventMask _XtConvertTypeToMask (eventType)
116 int eventType;
118 eventType &= 0x7f; /* Events sent with XSendEvent have high bit set. */
119 if (eventType < XtNumber(masks))
120 return masks[eventType];
121 else
122 return 0;
125 #endif /* not HAVE_X11R5 */
127 /* -- _XtOnGrabList() omitted -- */
130 static Widget LookupSpringLoaded(grabList)
131 XtGrabList grabList;
133 XtGrabList gl;
135 for (gl = grabList; gl != NULL; gl = gl->next) {
136 if (gl->spring_loaded)
137 if (XtIsSensitive(gl->widget))
138 return gl->widget;
139 else
140 return NULL;
141 if (gl->exclusive) break;
143 return NULL;
148 /* This function is new. */
150 static Boolean WouldDispatchEvent(event, widget, mask, pd)
151 register XEvent *event;
152 Widget widget;
153 EventMask mask;
154 XtPerDisplay pd;
156 XtEventRec *p;
157 Boolean would_dispatched = False;
159 if ((mask == ExposureMask) ||
160 ((event->type == NoExpose) && NO_EXPOSE) ||
161 ((event->type == GraphicsExpose) && GRAPHICS_EXPOSE) )
162 if (widget->core.widget_class->core_class.expose != NULL )
163 return True;
166 if ((mask == VisibilityChangeMask) &&
167 XtClass(widget)->core_class.visible_interest)
168 return True;
170 for (p=widget->core.event_table; p != NULL; p = p->next)
171 if ((mask & p->mask) != 0
172 #ifndef HAVE_X11R5
173 || (mask == 0 && p->non_filter)
174 #endif
176 return True;
178 return False;
182 /* #### This function is mostly copied from DecideToDispatch().
185 typedef enum _GrabType {pass, ignore, remap} GrabType;
187 Widget
188 XtWidgetToDispatchTo (XEvent* event)
190 register Widget widget;
191 EventMask mask;
192 GrabType grabType;
193 Widget dspWidget;
194 Time time = 0;
195 XtPerDisplay pd;
196 XtPerDisplayInput pdi;
197 XtGrabList grabList;
199 widget = XtWindowToWidget (event->xany.display, event->xany.window);
200 pd = _XtGetPerDisplay(event->xany.display);
201 pdi = _XtGetPerDisplayInput(event->xany.display);
202 grabList = *_XtGetGrabList(pdi);
204 mask = _XtConvertTypeToMask(event->xany.type);
205 grabType = pass;
206 switch (event->xany.type & 0x7f) {
207 case KeyPress:
208 case KeyRelease: grabType = remap; break;
209 case ButtonPress:
210 case ButtonRelease: grabType = remap; break;
211 case MotionNotify: grabType = ignore;
212 #define XKnownButtons (Button1MotionMask|Button2MotionMask|Button3MotionMask|\
213 Button4MotionMask|Button5MotionMask)
214 mask |= (event->xmotion.state & XKnownButtons);
215 #undef XKnownButtons
216 break;
217 case EnterNotify: grabType = ignore; break;
220 if (widget == NULL) {
221 if (grabType != remap) return False;
222 /* event occurred in a non-widget window, but we've promised also
223 to dispatch it to the nearest accessible spring_loaded widget */
224 else if ((widget = LookupSpringLoaded(grabList)) != NULL)
225 return widget;
226 return False;
229 switch(grabType) {
230 case pass:
231 return widget;
233 case ignore:
234 if ((grabList == NULL || _XtOnGrabList(widget,grabList))
235 && XtIsSensitive(widget)) {
236 return widget;
238 return NULL;
240 case remap:
243 Widget was_dispatched_to= NULL;
244 extern Widget _XtFindRemapWidget();
245 extern void _XtUngrabBadGrabs();
247 dspWidget = _XtFindRemapWidget(event, widget, mask, pdi);
249 if ((grabList == NULL ||
250 _XtOnGrabList(dspWidget, grabList)) &&
251 XtIsSensitive(dspWidget)) {
252 if (WouldDispatchEvent (event, dspWidget, mask, pd))
253 was_dispatched_to = dspWidget;
256 /* Also dispatch to nearest accessible spring_loaded. */
257 /* Fetch this afterward to reflect modal list changes */
258 grabList = *_XtGetGrabList(pdi);
259 widget = LookupSpringLoaded(grabList);
260 if (widget != NULL && widget != dspWidget) {
261 if (!was_dispatched_to)
262 was_dispatched_to = widget;
265 return was_dispatched_to;
268 /* should never reach here */
269 return NULL;