1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
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.
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.
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.
19 /* By Owen Taylor <otaylor@gtk.org> 98/4/4 */
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/.
29 #include "gtkmarshalers.h"
31 #include "gtkprivate.h"
32 #include "gtkplugprivate.h"
36 #include "gtkxembed.h"
39 static void xembed_set_info (GdkWindow *window,
43 _gtk_plug_windowing_get_id (GtkPlug *plug)
45 return GDK_WINDOW_XWINDOW (GTK_WIDGET (plug)->window);
49 _gtk_plug_windowing_realize_toplevel (GtkPlug *plug)
51 xembed_set_info (GTK_WIDGET (plug)->window, 0);
55 _gtk_plug_windowing_map_toplevel (GtkPlug *plug)
57 xembed_set_info (GTK_WIDGET (plug)->window, XEMBED_MAPPED);
61 _gtk_plug_windowing_unmap_toplevel (GtkPlug *plug)
63 xembed_set_info (GTK_WIDGET (plug)->window, 0);
67 _gtk_plug_windowing_set_focus (GtkPlug *plug)
69 _gtk_xembed_send_message (plug->socket_window,
70 XEMBED_REQUEST_FOCUS, 0, 0, 0);
74 _gtk_plug_windowing_add_grabbed_key (GtkPlug *plug,
75 guint accelerator_key,
76 GdkModifierType accelerator_mods)
78 _gtk_xembed_send_message (plug->socket_window, XEMBED_GTK_GRAB_KEY, 0,
79 accelerator_key, accelerator_mods);
83 _gtk_plug_windowing_remove_grabbed_key (GtkPlug *plug,
84 guint accelerator_key,
85 GdkModifierType accelerator_mods)
87 _gtk_xembed_send_message (plug->socket_window, XEMBED_GTK_UNGRAB_KEY, 0,
88 accelerator_key, accelerator_mods);
92 _gtk_plug_windowing_focus_to_parent (GtkPlug *plug,
93 GtkDirectionType direction)
95 XEmbedMessageType message = XEMBED_FOCUS_PREV; /* Quiet GCC */
101 case GTK_DIR_TAB_BACKWARD:
102 message = XEMBED_FOCUS_PREV;
106 case GTK_DIR_TAB_FORWARD:
107 message = XEMBED_FOCUS_NEXT;
111 _gtk_xembed_send_focus_message (plug->socket_window, message, 0);
115 xembed_set_info (GdkWindow *window,
118 GdkDisplay *display = gdk_drawable_get_display (window);
119 unsigned long buffer[2];
121 Atom xembed_info_atom = gdk_x11_get_xatom_by_name_for_display (display, "_XEMBED_INFO");
123 buffer[0] = GTK_XEMBED_PROTOCOL_VERSION;
126 XChangeProperty (GDK_DISPLAY_XDISPLAY (display),
127 GDK_WINDOW_XWINDOW (window),
128 xembed_info_atom, xembed_info_atom, 32,
130 (unsigned char *)buffer, 2);
134 handle_xembed_message (GtkPlug *plug,
135 XEmbedMessageType message,
141 GtkWindow *window = GTK_WINDOW (plug);
143 GTK_NOTE (PLUGSOCKET,
144 g_message ("GtkPlug: %s received", _gtk_xembed_message_name (message)));
148 case XEMBED_EMBEDDED_NOTIFY:
150 case XEMBED_WINDOW_ACTIVATE:
151 _gtk_window_set_is_active (window, TRUE);
153 case XEMBED_WINDOW_DEACTIVATE:
154 _gtk_window_set_is_active (window, FALSE);
157 case XEMBED_MODALITY_ON:
158 _gtk_plug_handle_modality_on (plug);
160 case XEMBED_MODALITY_OFF:
161 _gtk_plug_handle_modality_off (plug);
164 case XEMBED_FOCUS_IN:
165 _gtk_window_set_has_toplevel_focus (window, TRUE);
168 case XEMBED_FOCUS_FIRST:
169 _gtk_plug_focus_first_last (plug, GTK_DIR_TAB_FORWARD);
171 case XEMBED_FOCUS_LAST:
172 _gtk_plug_focus_first_last (plug, GTK_DIR_TAB_BACKWARD);
174 case XEMBED_FOCUS_CURRENT:
179 case XEMBED_FOCUS_OUT:
180 _gtk_window_set_has_toplevel_focus (window, FALSE);
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));
195 g_message ("GtkPlug: Ignoring unknown _XEMBED message of type %d", message));
201 _gtk_plug_windowing_filter_func (GdkXEvent *gdk_xevent,
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;
210 GdkFilterReturn return_val;
212 return_val = GDK_FILTER_CONTINUE;
214 switch (xevent->type)
217 if (xevent->xclient.message_type == gdk_x11_get_xatom_by_name_for_display (display, "_XEMBED"))
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 ();
228 return_val = GDK_FILTER_REMOVE;
230 else if (xevent->xclient.message_type == gdk_x11_get_xatom_by_name_for_display (display, "WM_DELETE_WINDOW"))
232 /* We filter these out because we take being reparented back to the
233 * root window as the reliable end of the embedding protocol
236 return_val = GDK_FILTER_REMOVE;
241 XReparentEvent *xre = &xevent->xreparent;
242 gboolean was_embedded = plug->socket_window != NULL;
244 GTK_NOTE (PLUGSOCKET, g_message("GtkPlug: ReparentNotify received"));
246 return_val = GDK_FILTER_REMOVE;
252 /* End of embedding protocol for previous socket */
254 GTK_NOTE (PLUGSOCKET, g_message ("GtkPlug: end of embedding"));
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
260 if (xre->parent != GDK_WINDOW_XWINDOW (plug->socket_window))
262 GtkWidget *widget = GTK_WIDGET (plug);
264 gdk_window_set_user_data (plug->socket_window, NULL);
265 g_object_unref (plug->socket_window);
266 plug->socket_window = NULL;
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.
277 if (xre->parent == GDK_WINDOW_XWINDOW (gdk_screen_get_root_window (screen)))
279 GTK_NOTE (PLUGSOCKET, g_message ("GtkPlug: calling gtk_plug_send_delete_event()"));
280 _gtk_plug_send_delete_event (widget);
282 g_object_notify (G_OBJECT (plug), "embedded");
289 if (xre->parent != GDK_WINDOW_XWINDOW (gdk_screen_get_root_window (screen)))
291 /* Start of embedding protocol */
293 GTK_NOTE (PLUGSOCKET, g_message ("GtkPlug: start of embedding"));
294 plug->socket_window = gdk_window_lookup_for_display (display, xre->parent);
295 if (plug->socket_window)
297 gpointer user_data = NULL;
298 gdk_window_get_user_data (plug->socket_window, &user_data);
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 */
307 g_object_ref (plug->socket_window);
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 */
316 _gtk_plug_add_all_grabbed_keys (plug);
319 g_signal_emit_by_name (plug, "embedded");
321 g_object_notify (G_OBJECT (plug), "embedded");
325 g_object_unref (plug);