]> Pileus Git - ~andy/gtk/blob - gtk/gtkplug-x11.c
c837cf0bd83ea634d8a4784685f56d6c006a21ce
[~andy/gtk] / gtk / gtkplug-x11.c
1 /* GTK - The GTK+ 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 "gtkmain.h"
29 #include "gtkmarshalers.h"
30 #include "gtkplug.h"
31 #include "gtkprivate.h"
32 #include "gtkplugprivate.h"
33
34 #include "x11/gdkx.h"
35
36 #include "gtkxembed.h"
37 #include "gtkalias.h"
38
39 static void xembed_set_info            (GdkWindow     *window,
40                                         unsigned long  flags);
41
42 GdkNativeWindow
43 _gtk_plug_windowing_get_id (GtkPlug *plug)
44 {
45   return GDK_WINDOW_XWINDOW (GTK_WIDGET (plug)->window);
46 }
47
48 void
49 _gtk_plug_windowing_realize_toplevel (GtkPlug *plug)
50 {
51   xembed_set_info (GTK_WIDGET (plug)->window, 0);
52 }
53
54 void
55 _gtk_plug_windowing_map_toplevel (GtkPlug *plug)
56 {
57   xembed_set_info (GTK_WIDGET (plug)->window, XEMBED_MAPPED);
58 }
59
60 void
61 _gtk_plug_windowing_unmap_toplevel (GtkPlug *plug)
62 {
63   xembed_set_info (GTK_WIDGET (plug)->window, 0);
64 }
65
66 void
67 _gtk_plug_windowing_set_focus (GtkPlug *plug)
68 {
69   _gtk_xembed_send_message (plug->socket_window,
70                             XEMBED_REQUEST_FOCUS, 0, 0, 0);
71 }
72
73 void
74 _gtk_plug_windowing_add_grabbed_key (GtkPlug        *plug,
75                                      guint           accelerator_key,
76                                      GdkModifierType accelerator_mods)
77 {
78   _gtk_xembed_send_message (plug->socket_window, XEMBED_GTK_GRAB_KEY, 0, 
79                             accelerator_key, accelerator_mods);
80 }
81
82 void
83 _gtk_plug_windowing_remove_grabbed_key (GtkPlug        *plug,
84                                         guint           accelerator_key,
85                                         GdkModifierType accelerator_mods)
86 {
87   _gtk_xembed_send_message (plug->socket_window, XEMBED_GTK_UNGRAB_KEY, 0, 
88                             accelerator_key, accelerator_mods);
89 }
90
91 void
92 _gtk_plug_windowing_focus_to_parent (GtkPlug         *plug,
93                                      GtkDirectionType direction)
94 {
95   XEmbedMessageType message = XEMBED_FOCUS_PREV; /* Quiet GCC */
96   
97   switch (direction)
98     {
99     case GTK_DIR_UP:
100     case GTK_DIR_LEFT:
101     case GTK_DIR_TAB_BACKWARD:
102       message = XEMBED_FOCUS_PREV;
103       break;
104     case GTK_DIR_DOWN:
105     case GTK_DIR_RIGHT:
106     case GTK_DIR_TAB_FORWARD:
107       message = XEMBED_FOCUS_NEXT;
108       break;
109     }
110   
111   _gtk_xembed_send_focus_message (plug->socket_window, message, 0);
112 }
113
114 static void
115 xembed_set_info (GdkWindow     *window,
116                  unsigned long  flags)
117 {
118   GdkDisplay *display = gdk_drawable_get_display (window);
119   unsigned long buffer[2];
120
121   Atom xembed_info_atom = gdk_x11_get_xatom_by_name_for_display (display, "_XEMBED_INFO");
122
123   buffer[0] = GTK_XEMBED_PROTOCOL_VERSION;
124   buffer[1] = flags;
125
126   XChangeProperty (GDK_DISPLAY_XDISPLAY (display),
127                    GDK_WINDOW_XWINDOW (window),
128                    xembed_info_atom, xembed_info_atom, 32,
129                    PropModeReplace,
130                    (unsigned char *)buffer, 2);
131 }
132
133 static void
134 handle_xembed_message (GtkPlug           *plug,
135                        XEmbedMessageType  message,
136                        glong              detail,
137                        glong              data1,
138                        glong              data2,
139                        guint32            time)
140 {
141   GtkWindow *window = GTK_WINDOW (plug);
142
143   GTK_NOTE (PLUGSOCKET,
144             g_message ("GtkPlug: %s received", _gtk_xembed_message_name (message)));
145   
146   switch (message)
147     {
148     case XEMBED_EMBEDDED_NOTIFY:
149       break;
150     case XEMBED_WINDOW_ACTIVATE:
151       _gtk_window_set_is_active (window, TRUE);
152       break;
153     case XEMBED_WINDOW_DEACTIVATE:
154       _gtk_window_set_is_active (window, FALSE);
155       break;
156       
157     case XEMBED_MODALITY_ON:
158       _gtk_plug_handle_modality_on (plug);
159       break;
160     case XEMBED_MODALITY_OFF:
161       _gtk_plug_handle_modality_off (plug);
162       break;
163
164     case XEMBED_FOCUS_IN:
165       _gtk_window_set_has_toplevel_focus (window, TRUE);
166       switch (detail)
167         {
168         case XEMBED_FOCUS_FIRST:
169           _gtk_plug_focus_first_last (plug, GTK_DIR_TAB_FORWARD);
170           break;
171         case XEMBED_FOCUS_LAST:
172           _gtk_plug_focus_first_last (plug, GTK_DIR_TAB_BACKWARD);
173           break;
174         case XEMBED_FOCUS_CURRENT:
175           break;
176         }
177       break;
178
179     case XEMBED_FOCUS_OUT:
180       _gtk_window_set_has_toplevel_focus (window, FALSE);
181       break;
182       
183     case XEMBED_GRAB_KEY:
184     case XEMBED_UNGRAB_KEY:
185     case XEMBED_GTK_GRAB_KEY:
186     case XEMBED_GTK_UNGRAB_KEY:
187     case XEMBED_REQUEST_FOCUS:
188     case XEMBED_FOCUS_NEXT:
189     case XEMBED_FOCUS_PREV:
190       g_warning ("GtkPlug: Invalid _XEMBED message %s received", _gtk_xembed_message_name (message));
191       break;
192       
193     default:
194       GTK_NOTE(PLUGSOCKET,
195                g_message ("GtkPlug: Ignoring unknown _XEMBED message of type %d", message));
196       break;
197     }
198 }
199
200 GdkFilterReturn
201 _gtk_plug_windowing_filter_func (GdkXEvent *gdk_xevent,
202                                  GdkEvent  *event,
203                                  gpointer   data)
204 {
205   GdkScreen *screen = gdk_drawable_get_screen (event->any.window);
206   GdkDisplay *display = gdk_screen_get_display (screen);
207   GtkPlug *plug = GTK_PLUG (data);
208   XEvent *xevent = (XEvent *)gdk_xevent;
209
210   GdkFilterReturn return_val;
211   
212   return_val = GDK_FILTER_CONTINUE;
213
214   switch (xevent->type)
215     {
216     case ClientMessage:
217       if (xevent->xclient.message_type == gdk_x11_get_xatom_by_name_for_display (display, "_XEMBED"))
218         {
219           _gtk_xembed_push_message (xevent);
220           handle_xembed_message (plug,
221                                  xevent->xclient.data.l[1],
222                                  xevent->xclient.data.l[2],
223                                  xevent->xclient.data.l[3],
224                                  xevent->xclient.data.l[4],
225                                  xevent->xclient.data.l[0]);
226           _gtk_xembed_pop_message ();
227
228           return_val = GDK_FILTER_REMOVE;
229         }
230       else if (xevent->xclient.message_type == gdk_x11_get_xatom_by_name_for_display (display, "WM_DELETE_WINDOW"))
231         {
232           /* We filter these out because we take being reparented back to the
233            * root window as the reliable end of the embedding protocol
234            */
235
236           return_val = GDK_FILTER_REMOVE;
237         }
238       break;
239     case ReparentNotify:
240       {
241         XReparentEvent *xre = &xevent->xreparent;
242         gboolean was_embedded = plug->socket_window != NULL;
243
244         GTK_NOTE (PLUGSOCKET, g_message("GtkPlug: ReparentNotify received\n"));
245
246         return_val = GDK_FILTER_REMOVE;
247         
248         g_object_ref (plug);
249         
250         if (was_embedded)
251           {
252             /* End of embedding protocol for previous socket */
253             
254             GTK_NOTE (PLUGSOCKET, g_message ("GtkPlug: end of embedding\n"));
255             /* FIXME: race if we remove from another socket and
256              * then add to a local window before we get notification
257              * Probably need check in _gtk_plug_add_to_socket
258              */
259             
260             if (xre->parent != GDK_WINDOW_XWINDOW (plug->socket_window))
261               {
262                 GtkWidget *widget = GTK_WIDGET (plug);
263
264                 gdk_window_set_user_data (plug->socket_window, NULL);
265                 g_object_unref (plug->socket_window);
266                 plug->socket_window = NULL;
267
268                 /* Emit a delete window, as if the user attempted
269                  * to close the toplevel. Simple as to how we
270                  * handle WM_DELETE_WINDOW, if it isn't handled
271                  * we destroy the widget. BUt only do this if
272                  * we are being reparented to the root window.
273                  * Moving from one embedder to another should
274                  * be invisible to the app.
275                  */
276
277                 if (xre->parent == GDK_WINDOW_XWINDOW (gdk_screen_get_root_window (screen)))
278                   {
279                     GTK_NOTE (PLUGSOCKET, g_message ("GtkPlug: calling gtk_plug_send_delete_event()\n"));
280                     _gtk_plug_send_delete_event (widget);
281
282                     g_object_notify (G_OBJECT (plug), "embedded");
283                   }
284               }
285             else
286               goto done;
287           }
288
289         if (xre->parent != GDK_WINDOW_XWINDOW (gdk_screen_get_root_window (screen)))
290           {
291             /* Start of embedding protocol */
292
293             GTK_NOTE (PLUGSOCKET, g_message ("GtkPlug: start of embedding\n"));
294             plug->socket_window = gdk_window_lookup_for_display (display, xre->parent);
295             if (plug->socket_window)
296               {
297                 gpointer user_data = NULL;
298                 gdk_window_get_user_data (plug->socket_window, &user_data);
299
300                 if (user_data)
301                   {
302                     g_warning (G_STRLOC "Plug reparented unexpectedly into window in the same process");
303                     plug->socket_window = NULL;
304                     break; /* FIXME: shouldn't this unref the plug? i.e. "goto done;" instead */
305                   }
306
307                 g_object_ref (plug->socket_window);
308               }
309             else
310               {
311                 plug->socket_window = gdk_window_foreign_new_for_display (display, xre->parent);
312                 if (!plug->socket_window) /* Already gone */
313                   break; /* FIXME: shouldn't this unref the plug? i.e. "goto done;" instead */
314               }
315
316             _gtk_plug_add_all_grabbed_keys (plug);
317
318             if (!was_embedded)
319               g_signal_emit_by_name (plug, "embedded");
320
321             g_object_notify (G_OBJECT (plug), "embedded");
322           }
323
324       done:
325         g_object_unref (plug);
326         
327         break;
328       }
329     }
330
331   return return_val;
332 }