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, see <http://www.gnu.org/licenses/>.Free
18 /* By Owen Taylor <otaylor@gtk.org> 98/4/4 */
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
31 #include "gtkmarshalers.h"
34 #include "gtkprivate.h"
35 #include "gtksocketprivate.h"
36 #include "gtkwidgetprivate.h"
37 #include "gtkwindowprivate.h"
38 #include "gtkxembed.h"
44 * @Short_description: Toplevel for embedding into other processes
46 * @include: gtk/gtkx.h
47 * @See_also: #GtkSocket
49 * Together with #GtkSocket, #GtkPlug provides the ability to embed
50 * widgets from one process into another process in a fashion that is
51 * transparent to the user. One process creates a #GtkSocket widget
52 * and passes the ID of that widget's window to the other process,
53 * which then creates a #GtkPlug with that window ID. Any widgets
54 * contained in the #GtkPlug then will appear inside the first
55 * application's window.
57 * The communication between a #GtkSocket and a #GtkPlug follows the
58 * <ulink url="http://www.freedesktop.org/Standards/xembed-spec">XEmbed</ulink>
59 * protocol. This protocol has also been implemented in other toolkits,
60 * e.g. <application>Qt</application>, allowing the same level of
61 * integration when embedding a <application>Qt</application> widget
62 * in GTK+ or vice versa.
65 * The #GtkPlug and #GtkSocket widgets are only available when GTK+
66 * is compiled for the X11 platform and %GDK_WINDOWING_X11 is defined.
67 * They can only be used on a #GdkX11Display. To use #GtkPlug and
68 * #GtkSocket, you need to include the <filename>gtk/gtkx.h</filename>
73 struct _GtkPlugPrivate
75 GtkWidget *modality_window;
76 GtkWindowGroup *modality_group;
78 GdkWindow *socket_window;
80 GHashTable *grabbed_keys;
85 static void gtk_plug_get_property (GObject *object,
89 static void gtk_plug_finalize (GObject *object);
90 static void gtk_plug_realize (GtkWidget *widget);
91 static void gtk_plug_unrealize (GtkWidget *widget);
92 static void gtk_plug_show (GtkWidget *widget);
93 static void gtk_plug_hide (GtkWidget *widget);
94 static void gtk_plug_map (GtkWidget *widget);
95 static void gtk_plug_unmap (GtkWidget *widget);
96 static void gtk_plug_size_allocate (GtkWidget *widget,
97 GtkAllocation *allocation);
98 static gboolean gtk_plug_key_press_event (GtkWidget *widget,
100 static gboolean gtk_plug_focus_event (GtkWidget *widget,
101 GdkEventFocus *event);
102 static void gtk_plug_set_focus (GtkWindow *window,
104 static gboolean gtk_plug_focus (GtkWidget *widget,
105 GtkDirectionType direction);
106 static void gtk_plug_check_resize (GtkContainer *container);
107 static void gtk_plug_keys_changed (GtkWindow *window);
109 static void xembed_set_info (GdkWindow *window,
110 unsigned long flags);
112 static GtkBinClass *bin_class = NULL;
116 guint accelerator_key;
117 GdkModifierType accelerator_mods;
131 static guint plug_signals[LAST_SIGNAL] = { 0 };
133 G_DEFINE_TYPE (GtkPlug, gtk_plug, GTK_TYPE_WINDOW)
136 gtk_plug_get_property (GObject *object,
141 GtkPlug *plug = GTK_PLUG (object);
142 GtkPlugPrivate *priv = plug->priv;
147 g_value_set_boolean (value, priv->socket_window != NULL);
149 case PROP_SOCKET_WINDOW:
150 g_value_set_object (value, priv->socket_window);
153 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
159 gtk_plug_class_init (GtkPlugClass *class)
161 GObjectClass *gobject_class = (GObjectClass *)class;
162 GtkWidgetClass *widget_class = (GtkWidgetClass *)class;
163 GtkWindowClass *window_class = (GtkWindowClass *)class;
164 GtkContainerClass *container_class = (GtkContainerClass *)class;
166 bin_class = g_type_class_peek (GTK_TYPE_BIN);
168 gobject_class->get_property = gtk_plug_get_property;
169 gobject_class->finalize = gtk_plug_finalize;
171 widget_class->realize = gtk_plug_realize;
172 widget_class->unrealize = gtk_plug_unrealize;
173 widget_class->key_press_event = gtk_plug_key_press_event;
174 widget_class->focus_in_event = gtk_plug_focus_event;
175 widget_class->focus_out_event = gtk_plug_focus_event;
177 widget_class->show = gtk_plug_show;
178 widget_class->hide = gtk_plug_hide;
179 widget_class->map = gtk_plug_map;
180 widget_class->unmap = gtk_plug_unmap;
181 widget_class->size_allocate = gtk_plug_size_allocate;
183 widget_class->focus = gtk_plug_focus;
185 gtk_widget_class_set_accessible_role (widget_class, ATK_ROLE_PANEL);
187 container_class->check_resize = gtk_plug_check_resize;
189 window_class->set_focus = gtk_plug_set_focus;
190 window_class->keys_changed = gtk_plug_keys_changed;
195 * %TRUE if the plug is embedded in a socket.
199 g_object_class_install_property (gobject_class,
201 g_param_spec_boolean ("embedded",
203 P_("Whether the plug is embedded"),
205 GTK_PARAM_READABLE));
208 * GtkPlug:socket-window:
210 * The window of the socket the plug is embedded in.
214 g_object_class_install_property (gobject_class,
216 g_param_spec_object ("socket-window",
218 P_("The window of the socket the plug is embedded in"),
220 GTK_PARAM_READABLE));
224 * @plug: the object on which the signal was emitted
226 * Gets emitted when the plug becomes embedded in a socket.
228 plug_signals[EMBEDDED] =
229 g_signal_new (I_("embedded"),
230 G_OBJECT_CLASS_TYPE (class),
232 G_STRUCT_OFFSET (GtkPlugClass, embedded),
234 _gtk_marshal_VOID__VOID,
237 g_type_class_add_private (class, sizeof (GtkPlugPrivate));
241 gtk_plug_init (GtkPlug *plug)
243 plug->priv = G_TYPE_INSTANCE_GET_PRIVATE (plug,
249 * gtk_plug_handle_modality_on:
253 * Called from the GtkPlug backend when the corresponding socket has
254 * told the plug that it modality has toggled on.
257 gtk_plug_handle_modality_on (GtkPlug *plug)
259 GtkPlugPrivate *priv = plug->priv;
261 if (!priv->modality_window)
263 priv->modality_window = gtk_window_new (GTK_WINDOW_POPUP);
264 gtk_window_set_screen (GTK_WINDOW (priv->modality_window),
265 gtk_widget_get_screen (GTK_WIDGET (plug)));
266 gtk_widget_realize (priv->modality_window);
267 gtk_window_group_add_window (priv->modality_group, GTK_WINDOW (priv->modality_window));
268 gtk_grab_add (priv->modality_window);
273 * gtk_plug_handle_modality_off:
277 * Called from the GtkPlug backend when the corresponding socket has
278 * told the plug that it modality has toggled off.
281 gtk_plug_handle_modality_off (GtkPlug *plug)
283 GtkPlugPrivate *priv = plug->priv;
285 if (priv->modality_window)
287 gtk_widget_destroy (priv->modality_window);
288 priv->modality_window = NULL;
293 gtk_plug_set_is_child (GtkPlug *plug,
296 GtkPlugPrivate *priv = plug->priv;
297 GtkWidget *widget = GTK_WIDGET (plug);
299 g_assert (!gtk_widget_get_parent (widget));
303 if (priv->modality_window)
304 gtk_plug_handle_modality_off (plug);
306 if (priv->modality_group)
308 gtk_window_group_remove_window (priv->modality_group, GTK_WINDOW (plug));
309 g_object_unref (priv->modality_group);
310 priv->modality_group = NULL;
313 /* As a toplevel, the MAPPED flag doesn't correspond
314 * to whether the widget->window is mapped; we unmap
315 * here, but don't bother remapping -- we will get mapped
316 * by gtk_widget_set_parent ().
318 if (gtk_widget_get_mapped (widget))
319 gtk_widget_unmap (widget);
321 _gtk_window_set_is_toplevel (GTK_WINDOW (plug), FALSE);
322 gtk_container_set_resize_mode (GTK_CONTAINER (plug), GTK_RESIZE_PARENT);
324 _gtk_widget_propagate_hierarchy_changed (widget, widget);
328 if (gtk_window_get_focus (GTK_WINDOW (plug)))
329 gtk_window_set_focus (GTK_WINDOW (plug), NULL);
330 if (gtk_window_get_default_widget (GTK_WINDOW (plug)))
331 gtk_window_set_default (GTK_WINDOW (plug), NULL);
333 priv->modality_group = gtk_window_group_new ();
334 gtk_window_group_add_window (priv->modality_group, GTK_WINDOW (plug));
336 _gtk_window_set_is_toplevel (GTK_WINDOW (plug), TRUE);
337 gtk_container_set_resize_mode (GTK_CONTAINER (plug), GTK_RESIZE_QUEUE);
339 _gtk_widget_propagate_hierarchy_changed (GTK_WIDGET (plug), NULL);
347 * Gets the window ID of a #GtkPlug widget, which can then
348 * be used to embed this window inside another window, for
349 * instance with gtk_socket_add_id().
351 * Return value: the window ID for the plug
354 gtk_plug_get_id (GtkPlug *plug)
356 g_return_val_if_fail (GTK_IS_PLUG (plug), 0);
358 if (!gtk_widget_get_realized (GTK_WIDGET (plug)))
359 gtk_widget_realize (GTK_WIDGET (plug));
361 return GDK_WINDOW_XID (gtk_widget_get_window (GTK_WIDGET (plug)));
365 * gtk_plug_get_embedded:
368 * Determines whether the plug is embedded in a socket.
370 * Return value: %TRUE if the plug is embedded in a socket
375 gtk_plug_get_embedded (GtkPlug *plug)
377 g_return_val_if_fail (GTK_IS_PLUG (plug), FALSE);
379 return plug->priv->socket_window != NULL;
383 * gtk_plug_get_socket_window:
386 * Retrieves the socket the plug is embedded in.
388 * Return value: (transfer none): the window of the socket, or %NULL
393 gtk_plug_get_socket_window (GtkPlug *plug)
395 g_return_val_if_fail (GTK_IS_PLUG (plug), NULL);
397 return plug->priv->socket_window;
401 * _gtk_plug_add_to_socket:
403 * @socket_: a #GtkSocket
405 * Adds a plug to a socket within the same application.
408 _gtk_plug_add_to_socket (GtkPlug *plug,
411 GtkPlugPrivate *priv;
414 g_return_if_fail (GTK_IS_PLUG (plug));
415 g_return_if_fail (GTK_IS_SOCKET (socket_));
416 g_return_if_fail (gtk_widget_get_realized (GTK_WIDGET (socket_)));
419 widget = GTK_WIDGET (plug);
421 gtk_plug_set_is_child (plug, TRUE);
422 priv->same_app = TRUE;
423 socket_->priv->same_app = TRUE;
424 socket_->priv->plug_widget = widget;
426 priv->socket_window = gtk_widget_get_window (GTK_WIDGET (socket_));
427 g_object_ref (priv->socket_window);
428 g_signal_emit (plug, plug_signals[EMBEDDED], 0);
429 g_object_notify (G_OBJECT (plug), "embedded");
431 if (gtk_widget_get_realized (widget))
435 window = gtk_widget_get_window (widget);
436 gdk_window_reparent (window, priv->socket_window,
437 -gdk_window_get_width (window),
438 -gdk_window_get_height (window));
441 gtk_widget_set_parent (widget, GTK_WIDGET (socket_));
443 g_signal_emit_by_name (socket_, "plug-added");
447 * _gtk_plug_send_delete_event:
448 * @widget: a #GtkWidget
450 * Send a GDK_DELETE event to the @widget and destroy it if
451 * necessary. Internal GTK function, called from this file or the
452 * backend-specific GtkPlug implementation.
455 _gtk_plug_send_delete_event (GtkWidget *widget)
457 GdkEvent *event = gdk_event_new (GDK_DELETE);
459 event->any.window = g_object_ref (gtk_widget_get_window (widget));
460 event->any.send_event = FALSE;
462 g_object_ref (widget);
464 if (!gtk_widget_event (widget, event))
465 gtk_widget_destroy (widget);
467 g_object_unref (widget);
469 gdk_event_free (event);
473 * _gtk_plug_remove_from_socket:
475 * @socket_: a #GtkSocket
477 * Removes a plug from a socket within the same application.
480 _gtk_plug_remove_from_socket (GtkPlug *plug,
483 GtkPlugPrivate *priv;
487 gboolean widget_was_visible;
489 g_return_if_fail (GTK_IS_PLUG (plug));
490 g_return_if_fail (GTK_IS_SOCKET (socket_));
491 g_return_if_fail (gtk_widget_get_realized (GTK_WIDGET (plug)));
494 widget = GTK_WIDGET (plug);
496 if (_gtk_widget_get_in_reparent (widget))
500 g_object_ref (socket_);
502 widget_was_visible = gtk_widget_get_visible (widget);
503 window = gtk_widget_get_window (widget);
505 gdk_window_hide (window);
506 _gtk_widget_set_in_reparent (widget, TRUE);
507 gdk_window_reparent (window,
508 gtk_widget_get_root_window (widget),
510 gtk_widget_unparent (GTK_WIDGET (plug));
511 _gtk_widget_set_in_reparent (widget, FALSE);
513 socket_->priv->plug_widget = NULL;
514 if (socket_->priv->plug_window != NULL)
516 g_object_unref (socket_->priv->plug_window);
517 socket_->priv->plug_window = NULL;
520 socket_->priv->same_app = FALSE;
522 priv->same_app = FALSE;
523 if (priv->socket_window != NULL)
525 g_object_unref (priv->socket_window);
526 priv->socket_window = NULL;
528 gtk_plug_set_is_child (plug, FALSE);
530 g_signal_emit_by_name (socket_, "plug-removed", &result);
532 gtk_widget_destroy (GTK_WIDGET (socket_));
535 _gtk_plug_send_delete_event (widget);
537 g_object_unref (plug);
539 if (widget_was_visible && gtk_widget_get_visible (GTK_WIDGET (socket_)))
540 gtk_widget_queue_resize (GTK_WIDGET (socket_));
542 g_object_unref (socket_);
546 * gtk_plug_construct:
548 * @socket_id: the XID of the socket's window.
550 * Finish the initialization of @plug for a given #GtkSocket identified by
551 * @socket_id. This function will generally only be used by classes deriving from #GtkPlug.
554 gtk_plug_construct (GtkPlug *plug,
557 gtk_plug_construct_for_display (plug, gdk_display_get_default (), socket_id);
561 * gtk_plug_construct_for_display:
563 * @display: the #GdkDisplay associated with @socket_id's
565 * @socket_id: the XID of the socket's window.
567 * Finish the initialization of @plug for a given #GtkSocket identified by
568 * @socket_id which is currently displayed on @display.
569 * This function will generally only be used by classes deriving from #GtkPlug.
574 gtk_plug_construct_for_display (GtkPlug *plug,
578 GtkPlugPrivate *priv;
580 g_return_if_fail (GTK_IS_PLUG (plug));
581 g_return_if_fail (GDK_IS_DISPLAY (display));
587 gpointer user_data = NULL;
589 if (GDK_IS_X11_DISPLAY (display))
590 priv->socket_window = gdk_x11_window_lookup_for_display (display, socket_id);
592 priv->socket_window = NULL;
594 if (priv->socket_window)
596 gdk_window_get_user_data (priv->socket_window, &user_data);
600 if (GTK_IS_SOCKET (user_data))
601 _gtk_plug_add_to_socket (plug, user_data);
604 g_warning (G_STRLOC "Can't create GtkPlug as child of non-GtkSocket");
605 priv->socket_window = NULL;
609 g_object_ref (priv->socket_window);
611 else if (GDK_IS_X11_DISPLAY (display))
612 priv->socket_window = gdk_x11_window_foreign_new_for_display (display, socket_id);
614 if (priv->socket_window) {
615 g_signal_emit (plug, plug_signals[EMBEDDED], 0);
617 g_object_notify (G_OBJECT (plug), "embedded");
624 * @socket_id: the window ID of the socket, or 0.
626 * Creates a new plug widget inside the #GtkSocket identified
627 * by @socket_id. If @socket_id is 0, the plug is left "unplugged" and
628 * can later be plugged into a #GtkSocket by gtk_socket_add_id().
630 * Return value: the new #GtkPlug widget.
633 gtk_plug_new (Window socket_id)
635 return gtk_plug_new_for_display (gdk_display_get_default (), socket_id);
639 * gtk_plug_new_for_display:
640 * @display: the #GdkDisplay on which @socket_id is displayed
641 * @socket_id: the XID of the socket's window.
643 * Create a new plug widget inside the #GtkSocket identified by socket_id.
645 * Return value: the new #GtkPlug widget.
650 gtk_plug_new_for_display (GdkDisplay *display,
655 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
657 plug = g_object_new (GTK_TYPE_PLUG, NULL);
658 gtk_plug_construct_for_display (plug, display, socket_id);
659 return GTK_WIDGET (plug);
663 gtk_plug_finalize (GObject *object)
665 GtkPlug *plug = GTK_PLUG (object);
666 GtkPlugPrivate *priv = plug->priv;
668 if (priv->grabbed_keys)
669 g_hash_table_destroy (priv->grabbed_keys);
671 G_OBJECT_CLASS (gtk_plug_parent_class)->finalize (object);
675 gtk_plug_unrealize (GtkWidget *widget)
677 GtkPlug *plug = GTK_PLUG (widget);
678 GtkPlugPrivate *priv = plug->priv;
680 if (priv->socket_window != NULL)
682 gdk_window_set_user_data (priv->socket_window, NULL);
683 g_object_unref (priv->socket_window);
684 priv->socket_window = NULL;
686 g_object_notify (G_OBJECT (widget), "embedded");
691 if (priv->modality_window)
692 gtk_plug_handle_modality_off (plug);
694 gtk_window_group_remove_window (priv->modality_group, GTK_WINDOW (plug));
695 g_object_unref (priv->modality_group);
698 GTK_WIDGET_CLASS (gtk_plug_parent_class)->unrealize (widget);
702 xembed_set_info (GdkWindow *window,
705 GdkDisplay *display = gdk_window_get_display (window);
706 unsigned long buffer[2];
708 Atom xembed_info_atom = gdk_x11_get_xatom_by_name_for_display (display, "_XEMBED_INFO");
710 buffer[0] = GTK_XEMBED_PROTOCOL_VERSION;
713 XChangeProperty (GDK_DISPLAY_XDISPLAY (display),
714 GDK_WINDOW_XID (window),
715 xembed_info_atom, xembed_info_atom, 32,
717 (unsigned char *)buffer, 2);
721 * gtk_plug_focus_first_last:
724 * @direction: a direction
726 * Called from the GtkPlug backend when the corresponding socket has
727 * told the plug that it has received the focus.
730 gtk_plug_focus_first_last (GtkPlug *plug,
731 GtkDirectionType direction)
733 GtkWindow *window = GTK_WINDOW (plug);
734 GtkWidget *focus_widget;
737 focus_widget = gtk_window_get_focus (window);
740 parent = gtk_widget_get_parent (focus_widget);
743 gtk_container_set_focus_child (GTK_CONTAINER (parent), NULL);
744 parent = gtk_widget_get_parent (parent);
747 gtk_window_set_focus (GTK_WINDOW (plug), NULL);
750 gtk_widget_child_focus (GTK_WIDGET (plug), direction);
754 handle_xembed_message (GtkPlug *plug,
755 XEmbedMessageType message,
761 GtkWindow *window = GTK_WINDOW (plug);
763 GTK_NOTE (PLUGSOCKET,
764 g_message ("GtkPlug: %s received", _gtk_xembed_message_name (message)));
768 case XEMBED_EMBEDDED_NOTIFY:
770 case XEMBED_WINDOW_ACTIVATE:
771 _gtk_window_set_is_active (window, TRUE);
773 case XEMBED_WINDOW_DEACTIVATE:
774 _gtk_window_set_is_active (window, FALSE);
777 case XEMBED_MODALITY_ON:
778 gtk_plug_handle_modality_on (plug);
780 case XEMBED_MODALITY_OFF:
781 gtk_plug_handle_modality_off (plug);
784 case XEMBED_FOCUS_IN:
785 _gtk_window_set_has_toplevel_focus (window, TRUE);
788 case XEMBED_FOCUS_FIRST:
789 gtk_plug_focus_first_last (plug, GTK_DIR_TAB_FORWARD);
791 case XEMBED_FOCUS_LAST:
792 gtk_plug_focus_first_last (plug, GTK_DIR_TAB_BACKWARD);
794 case XEMBED_FOCUS_CURRENT:
799 case XEMBED_FOCUS_OUT:
800 _gtk_window_set_has_toplevel_focus (window, FALSE);
803 case XEMBED_GRAB_KEY:
804 case XEMBED_UNGRAB_KEY:
805 case XEMBED_GTK_GRAB_KEY:
806 case XEMBED_GTK_UNGRAB_KEY:
807 case XEMBED_REQUEST_FOCUS:
808 case XEMBED_FOCUS_NEXT:
809 case XEMBED_FOCUS_PREV:
810 g_warning ("GtkPlug: Invalid _XEMBED message %s received", _gtk_xembed_message_name (message));
815 g_message ("GtkPlug: Ignoring unknown _XEMBED message of type %d", message));
819 static GdkFilterReturn
820 gtk_plug_filter_func (GdkXEvent *gdk_xevent,
824 GdkScreen *screen = gdk_window_get_screen (event->any.window);
825 GdkDisplay *display = gdk_screen_get_display (screen);
826 GtkPlug *plug = GTK_PLUG (data);
827 GtkPlugPrivate *priv = plug->priv;
828 XEvent *xevent = (XEvent *)gdk_xevent;
831 GdkFilterReturn return_val;
833 return_val = GDK_FILTER_CONTINUE;
835 switch (xevent->type)
838 if (xevent->xclient.message_type == gdk_x11_get_xatom_by_name_for_display (display, "_XEMBED"))
840 _gtk_xembed_push_message (xevent);
841 handle_xembed_message (plug,
842 xevent->xclient.data.l[1],
843 xevent->xclient.data.l[2],
844 xevent->xclient.data.l[3],
845 xevent->xclient.data.l[4],
846 xevent->xclient.data.l[0]);
847 _gtk_xembed_pop_message ();
849 return_val = GDK_FILTER_REMOVE;
851 else if (xevent->xclient.message_type == gdk_x11_get_xatom_by_name_for_display (display, "WM_DELETE_WINDOW"))
853 /* We filter these out because we take being reparented back to the
854 * root window as the reliable end of the embedding protocol
857 return_val = GDK_FILTER_REMOVE;
862 XReparentEvent *xre = &xevent->xreparent;
863 gboolean was_embedded = priv->socket_window != NULL;
865 GTK_NOTE (PLUGSOCKET, g_message("GtkPlug: ReparentNotify received"));
867 return_val = GDK_FILTER_REMOVE;
873 /* End of embedding protocol for previous socket */
875 GTK_NOTE (PLUGSOCKET, g_message ("GtkPlug: end of embedding"));
876 /* FIXME: race if we remove from another socket and
877 * then add to a local window before we get notification
878 * Probably need check in _gtk_plug_add_to_socket
881 if (xre->parent != GDK_WINDOW_XID (priv->socket_window))
883 GtkWidget *widget = GTK_WIDGET (plug);
885 gdk_window_set_user_data (priv->socket_window, NULL);
886 g_object_unref (priv->socket_window);
887 priv->socket_window = NULL;
889 /* Emit a delete window, as if the user attempted
890 * to close the toplevel. Simple as to how we
891 * handle WM_DELETE_WINDOW, if it isn't handled
892 * we destroy the widget. BUt only do this if
893 * we are being reparented to the root window.
894 * Moving from one embedder to another should
895 * be invisible to the app.
898 if (xre->parent == GDK_WINDOW_XID (gdk_screen_get_root_window (screen)))
900 GTK_NOTE (PLUGSOCKET, g_message ("GtkPlug: calling gtk_plug_send_delete_event()"));
901 _gtk_plug_send_delete_event (widget);
903 g_object_notify (G_OBJECT (plug), "embedded");
910 if (xre->parent != GDK_WINDOW_XID (gdk_screen_get_root_window (screen)))
912 /* Start of embedding protocol */
914 GTK_NOTE (PLUGSOCKET, g_message ("GtkPlug: start of embedding"));
916 priv->socket_window = gdk_x11_window_lookup_for_display (display, xre->parent);
917 if (priv->socket_window)
919 gpointer user_data = NULL;
920 gdk_window_get_user_data (priv->socket_window, &user_data);
924 g_warning (G_STRLOC "Plug reparented unexpectedly into window in the same process");
925 priv->socket_window = NULL;
926 break; /* FIXME: shouldn't this unref the plug? i.e. "goto done;" instead */
929 g_object_ref (priv->socket_window);
933 priv->socket_window = gdk_x11_window_foreign_new_for_display (display, xre->parent);
934 if (!priv->socket_window) /* Already gone */
935 break; /* FIXME: shouldn't this unref the plug? i.e. "goto done;" instead */
938 if (priv->grabbed_keys)
940 g_hash_table_iter_init (&iter, priv->grabbed_keys);
941 while (g_hash_table_iter_next (&iter, &key, NULL))
943 GrabbedKey *grabbed_key = key;
945 _gtk_xembed_send_message (priv->socket_window, XEMBED_GTK_GRAB_KEY, 0,
946 grabbed_key->accelerator_key,
947 grabbed_key->accelerator_mods);
952 g_signal_emit_by_name (plug, "embedded");
954 g_object_notify (G_OBJECT (plug), "embedded");
958 g_object_unref (plug);
965 GdkModifierType state, consumed;
966 GdkDeviceManager *device_manager;
967 GdkDevice *pointer, *keyboard;
970 if (xevent->type == KeyPress)
971 event->key.type = GDK_KEY_PRESS;
973 event->key.type = GDK_KEY_RELEASE;
975 event->key.window = gdk_x11_window_lookup_for_display (display, xevent->xany.window);
976 event->key.send_event = TRUE;
977 event->key.time = xevent->xkey.time;
978 event->key.state = (GdkModifierType) xevent->xkey.state;
979 event->key.hardware_keycode = xevent->xkey.keycode;
980 event->key.keyval = GDK_KEY_VoidSymbol;
982 device_manager = gdk_display_get_device_manager (display);
983 pointer = gdk_device_manager_get_client_pointer (device_manager);
984 keyboard = gdk_device_get_associated_device (pointer);
985 gdk_event_set_device (event, keyboard);
987 keymap = gdk_keymap_get_for_display (display);
988 gdk_keymap_translate_keyboard_state (keymap,
989 event->key.hardware_keycode,
993 NULL, NULL, &consumed);
995 state = event->key.state & ~consumed;
996 gdk_keymap_add_virtual_modifiers (keymap, &state);
997 event->key.state |= state;
999 event->key.length = 0;
1000 event->key.string = g_strdup ("");
1002 /* FIXME: These should be filled in properly */
1003 event->key.group = 0;
1004 event->key.is_modifier = FALSE;
1006 return_val = GDK_FILTER_TRANSLATE;
1013 gtk_plug_realize (GtkWidget *widget)
1015 GtkAllocation allocation;
1016 GtkPlug *plug = GTK_PLUG (widget);
1017 GtkPlugPrivate *priv = plug->priv;
1018 GtkWindow *window = GTK_WINDOW (widget);
1019 GdkWindow *gdk_window;
1020 GdkWindowAttr attributes;
1022 gchar *wmclass_name, *wmclass_class;
1023 gint attributes_mask;
1025 gtk_widget_set_realized (widget, TRUE);
1027 title = gtk_window_get_title (window);
1028 _gtk_window_get_wmclass (window, &wmclass_name, &wmclass_class);
1029 gtk_widget_get_allocation (widget, &allocation);
1031 attributes.window_type = GDK_WINDOW_CHILD; /* XXX GDK_WINDOW_PLUG ? */
1032 attributes.title = (gchar *) title;
1033 attributes.wmclass_name = wmclass_name;
1034 attributes.wmclass_class = wmclass_class;
1035 attributes.width = allocation.width;
1036 attributes.height = allocation.height;
1037 attributes.wclass = GDK_INPUT_OUTPUT;
1039 /* this isn't right - we should match our parent's visual/colormap.
1040 * though that will require handling "foreign" colormaps */
1041 attributes.visual = gtk_widget_get_visual (widget);
1042 attributes.event_mask = gtk_widget_get_events (widget);
1043 attributes.event_mask |= (GDK_EXPOSURE_MASK |
1044 GDK_KEY_PRESS_MASK |
1045 GDK_KEY_RELEASE_MASK |
1046 GDK_ENTER_NOTIFY_MASK |
1047 GDK_LEAVE_NOTIFY_MASK |
1048 GDK_STRUCTURE_MASK);
1050 attributes_mask = GDK_WA_VISUAL;
1051 attributes_mask |= (title ? GDK_WA_TITLE : 0);
1052 attributes_mask |= (wmclass_name ? GDK_WA_WMCLASS : 0);
1054 if (gtk_widget_is_toplevel (widget))
1056 attributes.window_type = GDK_WINDOW_TOPLEVEL;
1058 gdk_error_trap_push ();
1059 if (priv->socket_window)
1060 gdk_window = gdk_window_new (priv->socket_window,
1061 &attributes, attributes_mask);
1062 else /* If it's a passive plug, we use the root window */
1063 gdk_window = gdk_window_new (gtk_widget_get_root_window (widget),
1064 &attributes, attributes_mask);
1065 gtk_widget_set_window (widget, gdk_window);
1067 gdk_display_sync (gtk_widget_get_display (widget));
1068 if (gdk_error_trap_pop ()) /* Uh-oh */
1070 gdk_error_trap_push ();
1071 gdk_window_destroy (gdk_window);
1072 gdk_error_trap_pop_ignored ();
1073 gdk_window = gdk_window_new (gtk_widget_get_root_window (widget),
1074 &attributes, attributes_mask);
1075 gtk_widget_set_window (widget, gdk_window);
1078 gdk_window_add_filter (gdk_window,
1079 gtk_plug_filter_func,
1082 priv->modality_group = gtk_window_group_new ();
1083 gtk_window_group_add_window (priv->modality_group, window);
1085 xembed_set_info (gtk_widget_get_window (GTK_WIDGET (plug)), 0);
1089 gdk_window = gdk_window_new (gtk_widget_get_parent_window (widget),
1090 &attributes, attributes_mask);
1091 gtk_widget_set_window (widget, gdk_window);
1094 gdk_window_set_user_data (gdk_window, window);
1096 gtk_style_context_set_background (gtk_widget_get_style_context (widget),
1099 gdk_window_enable_synchronized_configure (gdk_window);
1103 gtk_plug_show (GtkWidget *widget)
1105 if (gtk_widget_is_toplevel (widget))
1106 GTK_WIDGET_CLASS (gtk_plug_parent_class)->show (widget);
1108 GTK_WIDGET_CLASS (bin_class)->show (widget);
1112 gtk_plug_hide (GtkWidget *widget)
1114 if (gtk_widget_is_toplevel (widget))
1115 GTK_WIDGET_CLASS (gtk_plug_parent_class)->hide (widget);
1117 GTK_WIDGET_CLASS (bin_class)->hide (widget);
1120 /* From gdkinternals.h */
1121 void gdk_synthesize_window_state (GdkWindow *window,
1122 GdkWindowState unset_flags,
1123 GdkWindowState set_flags);
1126 gtk_plug_map (GtkWidget *widget)
1128 if (gtk_widget_is_toplevel (widget))
1130 GtkBin *bin = GTK_BIN (widget);
1131 GtkPlug *plug = GTK_PLUG (widget);
1134 gtk_widget_set_mapped (widget, TRUE);
1136 child = gtk_bin_get_child (bin);
1137 if (child != NULL &&
1138 gtk_widget_get_visible (child) &&
1139 !gtk_widget_get_mapped (child))
1140 gtk_widget_map (child);
1142 xembed_set_info (gtk_widget_get_window (GTK_WIDGET (plug)), XEMBED_MAPPED);
1144 gdk_synthesize_window_state (gtk_widget_get_window (widget),
1145 GDK_WINDOW_STATE_WITHDRAWN,
1149 GTK_WIDGET_CLASS (bin_class)->map (widget);
1153 gtk_plug_unmap (GtkWidget *widget)
1155 if (gtk_widget_is_toplevel (widget))
1157 GtkPlug *plug = GTK_PLUG (widget);
1161 window = gtk_widget_get_window (widget);
1163 gtk_widget_set_mapped (widget, FALSE);
1165 gdk_window_hide (window);
1167 child = gtk_bin_get_child (GTK_BIN (widget));
1169 gtk_widget_unmap (child);
1171 xembed_set_info (gtk_widget_get_window (GTK_WIDGET (plug)), 0);
1173 gdk_synthesize_window_state (window,
1175 GDK_WINDOW_STATE_WITHDRAWN);
1178 GTK_WIDGET_CLASS (bin_class)->unmap (widget);
1182 gtk_plug_size_allocate (GtkWidget *widget,
1183 GtkAllocation *allocation)
1187 if (gtk_widget_is_toplevel (widget))
1188 GTK_WIDGET_CLASS (gtk_plug_parent_class)->size_allocate (widget, allocation);
1191 GtkBin *bin = GTK_BIN (widget);
1193 gtk_widget_set_allocation (widget, allocation);
1195 if (gtk_widget_get_realized (widget))
1196 gdk_window_move_resize (gtk_widget_get_window (widget),
1197 allocation->x, allocation->y,
1198 allocation->width, allocation->height);
1200 child = gtk_bin_get_child (bin);
1202 if (child != NULL && gtk_widget_get_visible (child))
1204 GtkAllocation child_allocation;
1206 child_allocation.x = child_allocation.y = gtk_container_get_border_width (GTK_CONTAINER (widget));
1207 child_allocation.width =
1208 MAX (1, (gint)allocation->width - child_allocation.x * 2);
1209 child_allocation.height =
1210 MAX (1, (gint)allocation->height - child_allocation.y * 2);
1212 gtk_widget_size_allocate (child, &child_allocation);
1219 gtk_plug_key_press_event (GtkWidget *widget,
1222 if (gtk_widget_is_toplevel (widget))
1223 return GTK_WIDGET_CLASS (gtk_plug_parent_class)->key_press_event (widget, event);
1229 gtk_plug_focus_event (GtkWidget *widget,
1230 GdkEventFocus *event)
1232 /* We eat focus-in events and focus-out events, since they
1233 * can be generated by something like a keyboard grab on
1234 * a child of the plug.
1240 gtk_plug_set_focus (GtkWindow *window,
1243 GtkPlug *plug = GTK_PLUG (window);
1244 GtkPlugPrivate *priv = plug->priv;
1246 GTK_WINDOW_CLASS (gtk_plug_parent_class)->set_focus (window, focus);
1248 /* Ask for focus from embedder
1251 if (focus && !gtk_window_has_toplevel_focus (window))
1252 _gtk_xembed_send_message (priv->socket_window,
1253 XEMBED_REQUEST_FOCUS, 0, 0, 0);
1257 grabbed_key_hash (gconstpointer a)
1259 const GrabbedKey *key = a;
1262 h = key->accelerator_key << 16;
1263 h ^= key->accelerator_key >> 16;
1264 h ^= key->accelerator_mods;
1270 grabbed_key_equal (gconstpointer a, gconstpointer b)
1272 const GrabbedKey *keya = a;
1273 const GrabbedKey *keyb = b;
1275 return (keya->accelerator_key == keyb->accelerator_key &&
1276 keya->accelerator_mods == keyb->accelerator_mods);
1280 add_grabbed_key (gpointer key, gpointer val, gpointer data)
1282 GrabbedKey *grabbed_key = key;
1283 GtkPlug *plug = data;
1284 GtkPlugPrivate *priv = plug->priv;
1286 if (!priv->grabbed_keys ||
1287 !g_hash_table_lookup (priv->grabbed_keys, grabbed_key))
1289 _gtk_xembed_send_message (priv->socket_window, XEMBED_GTK_GRAB_KEY, 0,
1290 grabbed_key->accelerator_key,
1291 grabbed_key->accelerator_mods);
1296 remove_grabbed_key (gpointer key, gpointer val, gpointer data)
1298 GrabbedKey *grabbed_key = key;
1299 GtkPlug *plug = data;
1300 GtkPlugPrivate *priv = plug->priv;
1302 if (!priv->grabbed_keys ||
1303 !g_hash_table_lookup (priv->grabbed_keys, grabbed_key))
1305 _gtk_xembed_send_message (priv->socket_window, XEMBED_GTK_UNGRAB_KEY, 0,
1306 grabbed_key->accelerator_key,
1307 grabbed_key->accelerator_mods);
1312 keys_foreach (GtkWindow *window,
1314 GdkModifierType modifiers,
1315 gboolean is_mnemonic,
1318 GHashTable *new_grabbed_keys = data;
1319 GrabbedKey *key = g_slice_new (GrabbedKey);
1321 key->accelerator_key = keyval;
1322 key->accelerator_mods = modifiers;
1324 g_hash_table_replace (new_grabbed_keys, key, key);
1328 grabbed_key_free (gpointer data)
1330 g_slice_free (GrabbedKey, data);
1334 gtk_plug_keys_changed (GtkWindow *window)
1336 GHashTable *new_grabbed_keys, *old_grabbed_keys;
1337 GtkPlug *plug = GTK_PLUG (window);
1338 GtkPlugPrivate *priv = plug->priv;
1340 new_grabbed_keys = g_hash_table_new_full (grabbed_key_hash, grabbed_key_equal, (GDestroyNotify)grabbed_key_free, NULL);
1341 _gtk_window_keys_foreach (window, keys_foreach, new_grabbed_keys);
1343 if (priv->socket_window)
1344 g_hash_table_foreach (new_grabbed_keys, add_grabbed_key, plug);
1346 old_grabbed_keys = priv->grabbed_keys;
1347 priv->grabbed_keys = new_grabbed_keys;
1349 if (old_grabbed_keys)
1351 if (priv->socket_window)
1352 g_hash_table_foreach (old_grabbed_keys, remove_grabbed_key, plug);
1353 g_hash_table_destroy (old_grabbed_keys);
1358 gtk_plug_focus_to_parent (GtkPlug *plug,
1359 GtkDirectionType direction)
1361 GtkPlugPrivate *priv = plug->priv;
1362 XEmbedMessageType message;
1368 case GTK_DIR_TAB_BACKWARD:
1369 message = XEMBED_FOCUS_PREV;
1373 case GTK_DIR_TAB_FORWARD:
1374 message = XEMBED_FOCUS_NEXT;
1377 g_assert_not_reached ();
1378 message = XEMBED_FOCUS_PREV;
1382 _gtk_xembed_send_focus_message (priv->socket_window, message, 0);
1386 gtk_plug_focus (GtkWidget *widget,
1387 GtkDirectionType direction)
1389 GtkBin *bin = GTK_BIN (widget);
1390 GtkPlug *plug = GTK_PLUG (widget);
1391 GtkWindow *window = GTK_WINDOW (widget);
1392 GtkContainer *container = GTK_CONTAINER (widget);
1394 GtkWidget *old_focus_child;
1397 old_focus_child = gtk_container_get_focus_child (container);
1398 /* We override GtkWindow's behavior, since we don't want wrapping here.
1400 if (old_focus_child)
1402 GtkWidget *focus_widget;
1404 if (gtk_widget_child_focus (old_focus_child, direction))
1407 focus_widget = gtk_window_get_focus (window);
1410 /* Wrapped off the end, clear the focus setting for the toplevel */
1411 parent = gtk_widget_get_parent (focus_widget);
1414 gtk_container_set_focus_child (GTK_CONTAINER (parent), NULL);
1415 parent = gtk_widget_get_parent (parent);
1418 gtk_window_set_focus (GTK_WINDOW (container), NULL);
1423 /* Try to focus the first widget in the window */
1424 child = gtk_bin_get_child (bin);
1425 if (child && gtk_widget_child_focus (child, direction))
1429 if (!gtk_container_get_focus_child (GTK_CONTAINER (window)))
1430 gtk_plug_focus_to_parent (plug, direction);
1436 gtk_plug_check_resize (GtkContainer *container)
1438 if (gtk_widget_is_toplevel (GTK_WIDGET (container)))
1439 GTK_CONTAINER_CLASS (gtk_plug_parent_class)->check_resize (container);
1441 GTK_CONTAINER_CLASS (bin_class)->check_resize (container);