]> Pileus Git - ~andy/gtk/blob - gtk/gtkplug-x11.c
Rename GdkQuartzWindow.h and .c to GdkQuartzNSWindow.h and .c
[~andy/gtk] / gtk / gtkplug-x11.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free
16  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  */
18
19 /* By Owen Taylor <otaylor@gtk.org>              98/4/4 */
20
21 /*
22  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
23  * file for a list of people on the GTK+ Team.  See the ChangeLog
24  * files for a list of changes.  These files are distributed with
25  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
26  */
27
28 #include "config.h"
29
30 #if 0
31 #ifdef XINPUT_2
32
33 /* Hack to have keyboard events interpreted
34  * regardless of the default device manager
35  */
36 #define GDK_COMPILATION
37 #include "x11/gdkdevicemanager-core.h"
38 #include "x11/gdkdevicemanager-xi2.h"
39 #include "x11/gdkeventtranslator.h"
40 #undef GDK_COMPILATION
41
42 #endif /* XINPUT_2 */
43 #endif
44
45 #include "gtkmain.h"
46 #include "gtkmarshalers.h"
47 #include "gtkplug.h"
48 #include "gtkprivate.h"
49 #include "gtkplugprivate.h"
50 #include "gtkwindowprivate.h"
51 #include "gtkdebug.h"
52
53 #include "x11/gdkx.h"
54
55 #include "gtkxembed.h"
56
57 static void xembed_set_info            (GdkWindow     *window,
58                                         unsigned long  flags);
59
60 GdkNativeWindow
61 _gtk_plug_windowing_get_id (GtkPlug *plug)
62 {
63   return GDK_WINDOW_XID (gtk_widget_get_window (GTK_WIDGET (plug)));
64 }
65
66 void
67 _gtk_plug_windowing_realize_toplevel (GtkPlug *plug)
68 {
69   xembed_set_info (gtk_widget_get_window (GTK_WIDGET (plug)), 0);
70 }
71
72 void
73 _gtk_plug_windowing_map_toplevel (GtkPlug *plug)
74 {
75   xembed_set_info (gtk_widget_get_window (GTK_WIDGET (plug)), XEMBED_MAPPED);
76 }
77
78 void
79 _gtk_plug_windowing_unmap_toplevel (GtkPlug *plug)
80 {
81   xembed_set_info (gtk_widget_get_window (GTK_WIDGET (plug)), 0);
82 }
83
84 void
85 _gtk_plug_windowing_set_focus (GtkPlug *plug)
86 {
87   GtkPlugPrivate *priv = plug->priv;
88
89   _gtk_xembed_send_message (priv->socket_window,
90                             XEMBED_REQUEST_FOCUS, 0, 0, 0);
91 }
92
93 void
94 _gtk_plug_windowing_add_grabbed_key (GtkPlug        *plug,
95                                      guint           accelerator_key,
96                                      GdkModifierType accelerator_mods)
97 {
98   GtkPlugPrivate *priv = plug->priv;
99
100   _gtk_xembed_send_message (priv->socket_window, XEMBED_GTK_GRAB_KEY, 0,
101                             accelerator_key, accelerator_mods);
102 }
103
104 void
105 _gtk_plug_windowing_remove_grabbed_key (GtkPlug        *plug,
106                                         guint           accelerator_key,
107                                         GdkModifierType accelerator_mods)
108 {
109   GtkPlugPrivate *priv = plug->priv;
110
111   _gtk_xembed_send_message (priv->socket_window, XEMBED_GTK_UNGRAB_KEY, 0,
112                             accelerator_key, accelerator_mods);
113 }
114
115 void
116 _gtk_plug_windowing_focus_to_parent (GtkPlug         *plug,
117                                      GtkDirectionType direction)
118 {
119   GtkPlugPrivate *priv = plug->priv;
120   XEmbedMessageType message = XEMBED_FOCUS_PREV; /* Quiet GCC */
121   
122   switch (direction)
123     {
124     case GTK_DIR_UP:
125     case GTK_DIR_LEFT:
126     case GTK_DIR_TAB_BACKWARD:
127       message = XEMBED_FOCUS_PREV;
128       break;
129     case GTK_DIR_DOWN:
130     case GTK_DIR_RIGHT:
131     case GTK_DIR_TAB_FORWARD:
132       message = XEMBED_FOCUS_NEXT;
133       break;
134     }
135
136   _gtk_xembed_send_focus_message (priv->socket_window, message, 0);
137 }
138
139 static void
140 xembed_set_info (GdkWindow     *window,
141                  unsigned long  flags)
142 {
143   GdkDisplay *display = gdk_window_get_display (window);
144   unsigned long buffer[2];
145
146   Atom xembed_info_atom = gdk_x11_get_xatom_by_name_for_display (display, "_XEMBED_INFO");
147
148   buffer[0] = GTK_XEMBED_PROTOCOL_VERSION;
149   buffer[1] = flags;
150
151   XChangeProperty (GDK_DISPLAY_XDISPLAY (display),
152                    GDK_WINDOW_XID (window),
153                    xembed_info_atom, xembed_info_atom, 32,
154                    PropModeReplace,
155                    (unsigned char *)buffer, 2);
156 }
157
158 static void
159 handle_xembed_message (GtkPlug           *plug,
160                        XEmbedMessageType  message,
161                        glong              detail,
162                        glong              data1,
163                        glong              data2,
164                        guint32            time)
165 {
166   GtkWindow *window = GTK_WINDOW (plug);
167
168   GTK_NOTE (PLUGSOCKET,
169             g_message ("GtkPlug: %s received", _gtk_xembed_message_name (message)));
170   
171   switch (message)
172     {
173     case XEMBED_EMBEDDED_NOTIFY:
174       break;
175     case XEMBED_WINDOW_ACTIVATE:
176       _gtk_window_set_is_active (window, TRUE);
177       break;
178     case XEMBED_WINDOW_DEACTIVATE:
179       _gtk_window_set_is_active (window, FALSE);
180       break;
181       
182     case XEMBED_MODALITY_ON:
183       _gtk_plug_handle_modality_on (plug);
184       break;
185     case XEMBED_MODALITY_OFF:
186       _gtk_plug_handle_modality_off (plug);
187       break;
188
189     case XEMBED_FOCUS_IN:
190       _gtk_window_set_has_toplevel_focus (window, TRUE);
191       switch (detail)
192         {
193         case XEMBED_FOCUS_FIRST:
194           _gtk_plug_focus_first_last (plug, GTK_DIR_TAB_FORWARD);
195           break;
196         case XEMBED_FOCUS_LAST:
197           _gtk_plug_focus_first_last (plug, GTK_DIR_TAB_BACKWARD);
198           break;
199         case XEMBED_FOCUS_CURRENT:
200           break;
201         }
202       break;
203
204     case XEMBED_FOCUS_OUT:
205       _gtk_window_set_has_toplevel_focus (window, FALSE);
206       break;
207       
208     case XEMBED_GRAB_KEY:
209     case XEMBED_UNGRAB_KEY:
210     case XEMBED_GTK_GRAB_KEY:
211     case XEMBED_GTK_UNGRAB_KEY:
212     case XEMBED_REQUEST_FOCUS:
213     case XEMBED_FOCUS_NEXT:
214     case XEMBED_FOCUS_PREV:
215       g_warning ("GtkPlug: Invalid _XEMBED message %s received", _gtk_xembed_message_name (message));
216       break;
217       
218     default:
219       GTK_NOTE(PLUGSOCKET,
220                g_message ("GtkPlug: Ignoring unknown _XEMBED message of type %d", message));
221       break;
222     }
223 }
224
225 GdkFilterReturn
226 _gtk_plug_windowing_filter_func (GdkXEvent *gdk_xevent,
227                                  GdkEvent  *event,
228                                  gpointer   data)
229 {
230   GdkScreen *screen = gdk_window_get_screen (event->any.window);
231   GdkDisplay *display = gdk_screen_get_display (screen);
232   GtkPlug *plug = GTK_PLUG (data);
233   GtkPlugPrivate *priv = plug->priv;
234   XEvent *xevent = (XEvent *)gdk_xevent;
235
236   GdkFilterReturn return_val;
237
238   return_val = GDK_FILTER_CONTINUE;
239
240   switch (xevent->type)
241     {
242     case ClientMessage:
243       if (xevent->xclient.message_type == gdk_x11_get_xatom_by_name_for_display (display, "_XEMBED"))
244         {
245           _gtk_xembed_push_message (xevent);
246           handle_xembed_message (plug,
247                                  xevent->xclient.data.l[1],
248                                  xevent->xclient.data.l[2],
249                                  xevent->xclient.data.l[3],
250                                  xevent->xclient.data.l[4],
251                                  xevent->xclient.data.l[0]);
252           _gtk_xembed_pop_message ();
253
254           return_val = GDK_FILTER_REMOVE;
255         }
256       else if (xevent->xclient.message_type == gdk_x11_get_xatom_by_name_for_display (display, "WM_DELETE_WINDOW"))
257         {
258           /* We filter these out because we take being reparented back to the
259            * root window as the reliable end of the embedding protocol
260            */
261
262           return_val = GDK_FILTER_REMOVE;
263         }
264       break;
265     case ReparentNotify:
266       {
267         XReparentEvent *xre = &xevent->xreparent;
268         gboolean was_embedded = priv->socket_window != NULL;
269
270         GTK_NOTE (PLUGSOCKET, g_message("GtkPlug: ReparentNotify received"));
271
272         return_val = GDK_FILTER_REMOVE;
273         
274         g_object_ref (plug);
275         
276         if (was_embedded)
277           {
278             /* End of embedding protocol for previous socket */
279             
280             GTK_NOTE (PLUGSOCKET, g_message ("GtkPlug: end of embedding"));
281             /* FIXME: race if we remove from another socket and
282              * then add to a local window before we get notification
283              * Probably need check in _gtk_plug_add_to_socket
284              */
285
286             if (xre->parent != GDK_WINDOW_XID (priv->socket_window))
287               {
288                 GtkWidget *widget = GTK_WIDGET (plug);
289
290                 gdk_window_set_user_data (priv->socket_window, NULL);
291                 g_object_unref (priv->socket_window);
292                 priv->socket_window = NULL;
293
294                 /* Emit a delete window, as if the user attempted
295                  * to close the toplevel. Simple as to how we
296                  * handle WM_DELETE_WINDOW, if it isn't handled
297                  * we destroy the widget. BUt only do this if
298                  * we are being reparented to the root window.
299                  * Moving from one embedder to another should
300                  * be invisible to the app.
301                  */
302
303                 if (xre->parent == GDK_WINDOW_XID (gdk_screen_get_root_window (screen)))
304                   {
305                     GTK_NOTE (PLUGSOCKET, g_message ("GtkPlug: calling gtk_plug_send_delete_event()"));
306                     _gtk_plug_send_delete_event (widget);
307
308                     g_object_notify (G_OBJECT (plug), "embedded");
309                   }
310               }
311             else
312               goto done;
313           }
314
315         if (xre->parent != GDK_WINDOW_XID (gdk_screen_get_root_window (screen)))
316           {
317             /* Start of embedding protocol */
318
319             GTK_NOTE (PLUGSOCKET, g_message ("GtkPlug: start of embedding"));
320
321             priv->socket_window = gdk_x11_window_lookup_for_display (display, xre->parent);
322             if (priv->socket_window)
323               {
324                 gpointer user_data = NULL;
325                 gdk_window_get_user_data (priv->socket_window, &user_data);
326
327                 if (user_data)
328                   {
329                     g_warning (G_STRLOC "Plug reparented unexpectedly into window in the same process");
330                     priv->socket_window = NULL;
331                     break; /* FIXME: shouldn't this unref the plug? i.e. "goto done;" instead */
332                   }
333
334                 g_object_ref (priv->socket_window);
335               }
336             else
337               {
338                 priv->socket_window = gdk_x11_window_foreign_new_for_display (display, xre->parent);
339                 if (!priv->socket_window) /* Already gone */
340                   break; /* FIXME: shouldn't this unref the plug? i.e. "goto done;" instead */
341               }
342
343             _gtk_plug_add_all_grabbed_keys (plug);
344
345             if (!was_embedded)
346               g_signal_emit_by_name (plug, "embedded");
347
348             g_object_notify (G_OBJECT (plug), "embedded");
349           }
350
351       done:
352         g_object_unref (plug);
353         
354         break;
355       }
356
357 #if 0
358  /* FIXME: this needs some X11 backend api to do things
359   * in a saner way
360   */
361     case KeyPress:
362     case KeyRelease:
363       {
364         static GdkDeviceManager *core_device_manager = NULL;
365         GdkDeviceManager *device_manager;
366         GdkEvent *translated_event;
367         GList *devices, *d;
368         GdkDevice *keyboard = NULL;
369
370         device_manager = gdk_display_get_device_manager (display);
371
372         /* bail out if the device manager already
373          * interprets core keyboard events.
374          */
375         if (!GDK_IS_DEVICE_MANAGER_XI2 (device_manager))
376           return GDK_FILTER_CONTINUE;
377
378         /* Find out the first keyboard device, the
379          * generated event will be assigned to it.
380          */
381         devices = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_MASTER);
382
383         for (d = devices; d; d = d->next)
384           {
385             GdkDevice *device = d->data;
386
387             if (gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
388               keyboard = device;
389           }
390
391         g_list_free (devices);
392
393         if (!keyboard)
394           return GDK_FILTER_CONTINUE;
395
396         /* This is a crude hack so key events
397          * are interpreted as if there was a
398          * GdkDeviceManagerCore available.
399          */
400         if (G_UNLIKELY (!core_device_manager))
401           core_device_manager = g_object_new (GDK_TYPE_DEVICE_MANAGER_CORE,
402                                               "display", display,
403                                               NULL);
404
405         translated_event = gdk_event_translator_translate (GDK_EVENT_TRANSLATOR (core_device_manager), display, xevent);
406         gdk_event_set_device (translated_event, keyboard);
407
408         gtk_main_do_event (translated_event);
409         gdk_event_free (translated_event);
410
411         return_val = GDK_FILTER_REMOVE;
412       }
413 #endif
414     }
415
416   return return_val;
417 }