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:
252 * Called from the GtkPlug backend when the corresponding socket has
253 * told the plug that it modality has toggled on.
256 gtk_plug_handle_modality_on (GtkPlug *plug)
258 GtkPlugPrivate *priv = plug->priv;
260 if (!priv->modality_window)
262 priv->modality_window = gtk_window_new (GTK_WINDOW_POPUP);
263 gtk_window_set_screen (GTK_WINDOW (priv->modality_window),
264 gtk_widget_get_screen (GTK_WIDGET (plug)));
265 gtk_widget_realize (priv->modality_window);
266 gtk_window_group_add_window (priv->modality_group, GTK_WINDOW (priv->modality_window));
267 gtk_grab_add (priv->modality_window);
272 * gtk_plug_handle_modality_off:
275 * Called from the GtkPlug backend when the corresponding socket has
276 * told the plug that it modality has toggled off.
279 gtk_plug_handle_modality_off (GtkPlug *plug)
281 GtkPlugPrivate *priv = plug->priv;
283 if (priv->modality_window)
285 gtk_widget_destroy (priv->modality_window);
286 priv->modality_window = NULL;
291 gtk_plug_set_is_child (GtkPlug *plug,
294 GtkPlugPrivate *priv = plug->priv;
295 GtkWidget *widget = GTK_WIDGET (plug);
297 g_assert (!gtk_widget_get_parent (widget));
301 if (priv->modality_window)
302 gtk_plug_handle_modality_off (plug);
304 if (priv->modality_group)
306 gtk_window_group_remove_window (priv->modality_group, GTK_WINDOW (plug));
307 g_object_unref (priv->modality_group);
308 priv->modality_group = NULL;
311 /* As a toplevel, the MAPPED flag doesn't correspond
312 * to whether the widget->window is mapped; we unmap
313 * here, but don't bother remapping -- we will get mapped
314 * by gtk_widget_set_parent ().
316 if (gtk_widget_get_mapped (widget))
317 gtk_widget_unmap (widget);
319 _gtk_window_set_is_toplevel (GTK_WINDOW (plug), FALSE);
320 gtk_container_set_resize_mode (GTK_CONTAINER (plug), GTK_RESIZE_PARENT);
322 _gtk_widget_propagate_hierarchy_changed (widget, widget);
326 if (gtk_window_get_focus (GTK_WINDOW (plug)))
327 gtk_window_set_focus (GTK_WINDOW (plug), NULL);
328 if (gtk_window_get_default_widget (GTK_WINDOW (plug)))
329 gtk_window_set_default (GTK_WINDOW (plug), NULL);
331 priv->modality_group = gtk_window_group_new ();
332 gtk_window_group_add_window (priv->modality_group, GTK_WINDOW (plug));
334 _gtk_window_set_is_toplevel (GTK_WINDOW (plug), TRUE);
335 gtk_container_set_resize_mode (GTK_CONTAINER (plug), GTK_RESIZE_QUEUE);
337 _gtk_widget_propagate_hierarchy_changed (GTK_WIDGET (plug), NULL);
345 * Gets the window ID of a #GtkPlug widget, which can then
346 * be used to embed this window inside another window, for
347 * instance with gtk_socket_add_id().
349 * Return value: the window ID for the plug
352 gtk_plug_get_id (GtkPlug *plug)
354 g_return_val_if_fail (GTK_IS_PLUG (plug), 0);
356 if (!gtk_widget_get_realized (GTK_WIDGET (plug)))
357 gtk_widget_realize (GTK_WIDGET (plug));
359 return GDK_WINDOW_XID (gtk_widget_get_window (GTK_WIDGET (plug)));
363 * gtk_plug_get_embedded:
366 * Determines whether the plug is embedded in a socket.
368 * Return value: %TRUE if the plug is embedded in a socket
373 gtk_plug_get_embedded (GtkPlug *plug)
375 g_return_val_if_fail (GTK_IS_PLUG (plug), FALSE);
377 return plug->priv->socket_window != NULL;
381 * gtk_plug_get_socket_window:
384 * Retrieves the socket the plug is embedded in.
386 * Return value: (transfer none): the window of the socket, or %NULL
391 gtk_plug_get_socket_window (GtkPlug *plug)
393 g_return_val_if_fail (GTK_IS_PLUG (plug), NULL);
395 return plug->priv->socket_window;
399 * _gtk_plug_add_to_socket:
401 * @socket_: a #GtkSocket
403 * Adds a plug to a socket within the same application.
406 _gtk_plug_add_to_socket (GtkPlug *plug,
409 GtkPlugPrivate *priv;
412 g_return_if_fail (GTK_IS_PLUG (plug));
413 g_return_if_fail (GTK_IS_SOCKET (socket_));
414 g_return_if_fail (gtk_widget_get_realized (GTK_WIDGET (socket_)));
417 widget = GTK_WIDGET (plug);
419 gtk_plug_set_is_child (plug, TRUE);
420 priv->same_app = TRUE;
421 socket_->priv->same_app = TRUE;
422 socket_->priv->plug_widget = widget;
424 priv->socket_window = gtk_widget_get_window (GTK_WIDGET (socket_));
425 g_object_ref (priv->socket_window);
426 g_signal_emit (plug, plug_signals[EMBEDDED], 0);
427 g_object_notify (G_OBJECT (plug), "embedded");
429 if (gtk_widget_get_realized (widget))
433 window = gtk_widget_get_window (widget);
434 gdk_window_reparent (window, priv->socket_window,
435 -gdk_window_get_width (window),
436 -gdk_window_get_height (window));
439 gtk_widget_set_parent (widget, GTK_WIDGET (socket_));
441 g_signal_emit_by_name (socket_, "plug-added");
445 * gtk_plug_send_delete_event:
446 * @widget: a #GtkWidget
448 * Send a GDK_DELETE event to the @widget and destroy it if
449 * necessary. Internal GTK function, called from this file or the
450 * backend-specific GtkPlug implementation.
453 gtk_plug_send_delete_event (GtkWidget *widget)
455 GdkEvent *event = gdk_event_new (GDK_DELETE);
457 event->any.window = g_object_ref (gtk_widget_get_window (widget));
458 event->any.send_event = FALSE;
460 g_object_ref (widget);
462 if (!gtk_widget_event (widget, event))
463 gtk_widget_destroy (widget);
465 g_object_unref (widget);
467 gdk_event_free (event);
471 * _gtk_plug_remove_from_socket:
473 * @socket_: a #GtkSocket
475 * Removes a plug from a socket within the same application.
478 _gtk_plug_remove_from_socket (GtkPlug *plug,
481 GtkPlugPrivate *priv;
485 gboolean widget_was_visible;
487 g_return_if_fail (GTK_IS_PLUG (plug));
488 g_return_if_fail (GTK_IS_SOCKET (socket_));
489 g_return_if_fail (gtk_widget_get_realized (GTK_WIDGET (plug)));
492 widget = GTK_WIDGET (plug);
494 if (_gtk_widget_get_in_reparent (widget))
498 g_object_ref (socket_);
500 widget_was_visible = gtk_widget_get_visible (widget);
501 window = gtk_widget_get_window (widget);
503 gdk_window_hide (window);
504 _gtk_widget_set_in_reparent (widget, TRUE);
505 gdk_window_reparent (window,
506 gtk_widget_get_root_window (widget),
508 gtk_widget_unparent (GTK_WIDGET (plug));
509 _gtk_widget_set_in_reparent (widget, FALSE);
511 socket_->priv->plug_widget = NULL;
512 if (socket_->priv->plug_window != NULL)
514 g_object_unref (socket_->priv->plug_window);
515 socket_->priv->plug_window = NULL;
518 socket_->priv->same_app = FALSE;
520 priv->same_app = FALSE;
521 if (priv->socket_window != NULL)
523 g_object_unref (priv->socket_window);
524 priv->socket_window = NULL;
526 gtk_plug_set_is_child (plug, FALSE);
528 g_signal_emit_by_name (socket_, "plug-removed", &result);
530 gtk_widget_destroy (GTK_WIDGET (socket_));
533 gtk_plug_send_delete_event (widget);
535 g_object_unref (plug);
537 if (widget_was_visible && gtk_widget_get_visible (GTK_WIDGET (socket_)))
538 gtk_widget_queue_resize (GTK_WIDGET (socket_));
540 g_object_unref (socket_);
544 * gtk_plug_construct:
546 * @socket_id: the XID of the socket's window.
548 * Finish the initialization of @plug for a given #GtkSocket identified by
549 * @socket_id. This function will generally only be used by classes deriving from #GtkPlug.
552 gtk_plug_construct (GtkPlug *plug,
555 gtk_plug_construct_for_display (plug, gdk_display_get_default (), socket_id);
559 * gtk_plug_construct_for_display:
561 * @display: the #GdkDisplay associated with @socket_id's
563 * @socket_id: the XID of the socket's window.
565 * Finish the initialization of @plug for a given #GtkSocket identified by
566 * @socket_id which is currently displayed on @display.
567 * This function will generally only be used by classes deriving from #GtkPlug.
572 gtk_plug_construct_for_display (GtkPlug *plug,
576 GtkPlugPrivate *priv;
578 g_return_if_fail (GTK_IS_PLUG (plug));
579 g_return_if_fail (GDK_IS_DISPLAY (display));
585 gpointer user_data = NULL;
587 if (GDK_IS_X11_DISPLAY (display))
588 priv->socket_window = gdk_x11_window_lookup_for_display (display, socket_id);
590 priv->socket_window = NULL;
592 if (priv->socket_window)
594 gdk_window_get_user_data (priv->socket_window, &user_data);
598 if (GTK_IS_SOCKET (user_data))
599 _gtk_plug_add_to_socket (plug, user_data);
602 g_warning (G_STRLOC "Can't create GtkPlug as child of non-GtkSocket");
603 priv->socket_window = NULL;
607 g_object_ref (priv->socket_window);
609 else if (GDK_IS_X11_DISPLAY (display))
610 priv->socket_window = gdk_x11_window_foreign_new_for_display (display, socket_id);
612 if (priv->socket_window) {
613 g_signal_emit (plug, plug_signals[EMBEDDED], 0);
615 g_object_notify (G_OBJECT (plug), "embedded");
622 * @socket_id: the window ID of the socket, or 0.
624 * Creates a new plug widget inside the #GtkSocket identified
625 * by @socket_id. If @socket_id is 0, the plug is left "unplugged" and
626 * can later be plugged into a #GtkSocket by gtk_socket_add_id().
628 * Return value: the new #GtkPlug widget.
631 gtk_plug_new (Window socket_id)
633 return gtk_plug_new_for_display (gdk_display_get_default (), socket_id);
637 * gtk_plug_new_for_display:
638 * @display: the #GdkDisplay on which @socket_id is displayed
639 * @socket_id: the XID of the socket's window.
641 * Create a new plug widget inside the #GtkSocket identified by socket_id.
643 * Return value: the new #GtkPlug widget.
648 gtk_plug_new_for_display (GdkDisplay *display,
653 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
655 plug = g_object_new (GTK_TYPE_PLUG, NULL);
656 gtk_plug_construct_for_display (plug, display, socket_id);
657 return GTK_WIDGET (plug);
661 gtk_plug_finalize (GObject *object)
663 GtkPlug *plug = GTK_PLUG (object);
664 GtkPlugPrivate *priv = plug->priv;
666 if (priv->grabbed_keys)
667 g_hash_table_destroy (priv->grabbed_keys);
669 G_OBJECT_CLASS (gtk_plug_parent_class)->finalize (object);
673 gtk_plug_unrealize (GtkWidget *widget)
675 GtkPlug *plug = GTK_PLUG (widget);
676 GtkPlugPrivate *priv = plug->priv;
678 if (priv->socket_window != NULL)
680 gdk_window_set_user_data (priv->socket_window, NULL);
681 g_object_unref (priv->socket_window);
682 priv->socket_window = NULL;
684 g_object_notify (G_OBJECT (widget), "embedded");
689 if (priv->modality_window)
690 gtk_plug_handle_modality_off (plug);
692 gtk_window_group_remove_window (priv->modality_group, GTK_WINDOW (plug));
693 g_object_unref (priv->modality_group);
696 GTK_WIDGET_CLASS (gtk_plug_parent_class)->unrealize (widget);
700 xembed_set_info (GdkWindow *window,
703 GdkDisplay *display = gdk_window_get_display (window);
704 unsigned long buffer[2];
706 Atom xembed_info_atom = gdk_x11_get_xatom_by_name_for_display (display, "_XEMBED_INFO");
708 buffer[0] = GTK_XEMBED_PROTOCOL_VERSION;
711 XChangeProperty (GDK_DISPLAY_XDISPLAY (display),
712 GDK_WINDOW_XID (window),
713 xembed_info_atom, xembed_info_atom, 32,
715 (unsigned char *)buffer, 2);
719 * gtk_plug_focus_first_last:
721 * @direction: a direction
723 * Called from the GtkPlug backend when the corresponding socket has
724 * told the plug that it has received the focus.
727 gtk_plug_focus_first_last (GtkPlug *plug,
728 GtkDirectionType direction)
730 GtkWindow *window = GTK_WINDOW (plug);
731 GtkWidget *focus_widget;
734 focus_widget = gtk_window_get_focus (window);
737 parent = gtk_widget_get_parent (focus_widget);
740 gtk_container_set_focus_child (GTK_CONTAINER (parent), NULL);
741 parent = gtk_widget_get_parent (parent);
744 gtk_window_set_focus (GTK_WINDOW (plug), NULL);
747 gtk_widget_child_focus (GTK_WIDGET (plug), direction);
751 handle_xembed_message (GtkPlug *plug,
752 XEmbedMessageType message,
758 GtkWindow *window = GTK_WINDOW (plug);
760 GTK_NOTE (PLUGSOCKET,
761 g_message ("GtkPlug: %s received", _gtk_xembed_message_name (message)));
765 case XEMBED_EMBEDDED_NOTIFY:
767 case XEMBED_WINDOW_ACTIVATE:
768 _gtk_window_set_is_active (window, TRUE);
770 case XEMBED_WINDOW_DEACTIVATE:
771 _gtk_window_set_is_active (window, FALSE);
774 case XEMBED_MODALITY_ON:
775 gtk_plug_handle_modality_on (plug);
777 case XEMBED_MODALITY_OFF:
778 gtk_plug_handle_modality_off (plug);
781 case XEMBED_FOCUS_IN:
782 _gtk_window_set_has_toplevel_focus (window, TRUE);
785 case XEMBED_FOCUS_FIRST:
786 gtk_plug_focus_first_last (plug, GTK_DIR_TAB_FORWARD);
788 case XEMBED_FOCUS_LAST:
789 gtk_plug_focus_first_last (plug, GTK_DIR_TAB_BACKWARD);
791 case XEMBED_FOCUS_CURRENT:
796 case XEMBED_FOCUS_OUT:
797 _gtk_window_set_has_toplevel_focus (window, FALSE);
800 case XEMBED_GRAB_KEY:
801 case XEMBED_UNGRAB_KEY:
802 case XEMBED_GTK_GRAB_KEY:
803 case XEMBED_GTK_UNGRAB_KEY:
804 case XEMBED_REQUEST_FOCUS:
805 case XEMBED_FOCUS_NEXT:
806 case XEMBED_FOCUS_PREV:
807 g_warning ("GtkPlug: Invalid _XEMBED message %s received", _gtk_xembed_message_name (message));
812 g_message ("GtkPlug: Ignoring unknown _XEMBED message of type %d", message));
816 static GdkFilterReturn
817 gtk_plug_filter_func (GdkXEvent *gdk_xevent,
821 GdkScreen *screen = gdk_window_get_screen (event->any.window);
822 GdkDisplay *display = gdk_screen_get_display (screen);
823 GtkPlug *plug = GTK_PLUG (data);
824 GtkPlugPrivate *priv = plug->priv;
825 XEvent *xevent = (XEvent *)gdk_xevent;
828 GdkFilterReturn return_val;
830 return_val = GDK_FILTER_CONTINUE;
832 switch (xevent->type)
835 if (xevent->xclient.message_type == gdk_x11_get_xatom_by_name_for_display (display, "_XEMBED"))
837 _gtk_xembed_push_message (xevent);
838 handle_xembed_message (plug,
839 xevent->xclient.data.l[1],
840 xevent->xclient.data.l[2],
841 xevent->xclient.data.l[3],
842 xevent->xclient.data.l[4],
843 xevent->xclient.data.l[0]);
844 _gtk_xembed_pop_message ();
846 return_val = GDK_FILTER_REMOVE;
848 else if (xevent->xclient.message_type == gdk_x11_get_xatom_by_name_for_display (display, "WM_DELETE_WINDOW"))
850 /* We filter these out because we take being reparented back to the
851 * root window as the reliable end of the embedding protocol
854 return_val = GDK_FILTER_REMOVE;
859 XReparentEvent *xre = &xevent->xreparent;
860 gboolean was_embedded = priv->socket_window != NULL;
862 GTK_NOTE (PLUGSOCKET, g_message("GtkPlug: ReparentNotify received"));
864 return_val = GDK_FILTER_REMOVE;
870 /* End of embedding protocol for previous socket */
872 GTK_NOTE (PLUGSOCKET, g_message ("GtkPlug: end of embedding"));
873 /* FIXME: race if we remove from another socket and
874 * then add to a local window before we get notification
875 * Probably need check in _gtk_plug_add_to_socket
878 if (xre->parent != GDK_WINDOW_XID (priv->socket_window))
880 GtkWidget *widget = GTK_WIDGET (plug);
882 gdk_window_set_user_data (priv->socket_window, NULL);
883 g_object_unref (priv->socket_window);
884 priv->socket_window = NULL;
886 /* Emit a delete window, as if the user attempted
887 * to close the toplevel. Simple as to how we
888 * handle WM_DELETE_WINDOW, if it isn't handled
889 * we destroy the widget. BUt only do this if
890 * we are being reparented to the root window.
891 * Moving from one embedder to another should
892 * be invisible to the app.
895 if (xre->parent == GDK_WINDOW_XID (gdk_screen_get_root_window (screen)))
897 GTK_NOTE (PLUGSOCKET, g_message ("GtkPlug: calling gtk_plug_send_delete_event()"));
898 gtk_plug_send_delete_event (widget);
900 g_object_notify (G_OBJECT (plug), "embedded");
907 if (xre->parent != GDK_WINDOW_XID (gdk_screen_get_root_window (screen)))
909 /* Start of embedding protocol */
911 GTK_NOTE (PLUGSOCKET, g_message ("GtkPlug: start of embedding"));
913 priv->socket_window = gdk_x11_window_lookup_for_display (display, xre->parent);
914 if (priv->socket_window)
916 gpointer user_data = NULL;
917 gdk_window_get_user_data (priv->socket_window, &user_data);
921 g_warning (G_STRLOC "Plug reparented unexpectedly into window in the same process");
922 priv->socket_window = NULL;
923 break; /* FIXME: shouldn't this unref the plug? i.e. "goto done;" instead */
926 g_object_ref (priv->socket_window);
930 priv->socket_window = gdk_x11_window_foreign_new_for_display (display, xre->parent);
931 if (!priv->socket_window) /* Already gone */
932 break; /* FIXME: shouldn't this unref the plug? i.e. "goto done;" instead */
935 if (priv->grabbed_keys)
937 g_hash_table_iter_init (&iter, priv->grabbed_keys);
938 while (g_hash_table_iter_next (&iter, &key, NULL))
940 GrabbedKey *grabbed_key = key;
942 _gtk_xembed_send_message (priv->socket_window, XEMBED_GTK_GRAB_KEY, 0,
943 grabbed_key->accelerator_key,
944 grabbed_key->accelerator_mods);
949 g_signal_emit_by_name (plug, "embedded");
951 g_object_notify (G_OBJECT (plug), "embedded");
955 g_object_unref (plug);
962 GdkModifierType state, consumed;
963 GdkDeviceManager *device_manager;
964 GdkDevice *pointer, *keyboard;
967 if (xevent->type == KeyPress)
968 event->key.type = GDK_KEY_PRESS;
970 event->key.type = GDK_KEY_RELEASE;
972 event->key.window = gdk_x11_window_lookup_for_display (display, xevent->xany.window);
973 event->key.send_event = TRUE;
974 event->key.time = xevent->xkey.time;
975 event->key.state = (GdkModifierType) xevent->xkey.state;
976 event->key.hardware_keycode = xevent->xkey.keycode;
977 event->key.keyval = GDK_KEY_VoidSymbol;
979 device_manager = gdk_display_get_device_manager (display);
980 pointer = gdk_device_manager_get_client_pointer (device_manager);
981 keyboard = gdk_device_get_associated_device (pointer);
982 gdk_event_set_device (event, keyboard);
984 keymap = gdk_keymap_get_for_display (display);
986 event->key.group = gdk_x11_keymap_get_group_for_state (keymap, xevent->xkey.state);
987 event->key.is_modifier = gdk_x11_keymap_key_is_modifier (keymap, event->key.hardware_keycode);
989 gdk_keymap_translate_keyboard_state (keymap,
990 event->key.hardware_keycode,
994 NULL, NULL, &consumed);
996 state = event->key.state & ~consumed;
997 gdk_keymap_add_virtual_modifiers (keymap, &state);
998 event->key.state |= state;
1000 event->key.length = 0;
1001 event->key.string = g_strdup ("");
1003 return_val = GDK_FILTER_TRANSLATE;
1010 gtk_plug_realize (GtkWidget *widget)
1012 GtkAllocation allocation;
1013 GtkPlug *plug = GTK_PLUG (widget);
1014 GtkPlugPrivate *priv = plug->priv;
1015 GtkWindow *window = GTK_WINDOW (widget);
1016 GdkWindow *gdk_window;
1017 GdkWindowAttr attributes;
1019 gchar *wmclass_name, *wmclass_class;
1020 gint attributes_mask;
1022 gtk_widget_set_realized (widget, TRUE);
1024 title = gtk_window_get_title (window);
1025 _gtk_window_get_wmclass (window, &wmclass_name, &wmclass_class);
1026 gtk_widget_get_allocation (widget, &allocation);
1028 attributes.window_type = GDK_WINDOW_CHILD; /* XXX GDK_WINDOW_PLUG ? */
1029 attributes.title = (gchar *) title;
1030 attributes.wmclass_name = wmclass_name;
1031 attributes.wmclass_class = wmclass_class;
1032 attributes.width = allocation.width;
1033 attributes.height = allocation.height;
1034 attributes.wclass = GDK_INPUT_OUTPUT;
1036 /* this isn't right - we should match our parent's visual/colormap.
1037 * though that will require handling "foreign" colormaps */
1038 attributes.visual = gtk_widget_get_visual (widget);
1039 attributes.event_mask = gtk_widget_get_events (widget);
1040 attributes.event_mask |= (GDK_EXPOSURE_MASK |
1041 GDK_KEY_PRESS_MASK |
1042 GDK_KEY_RELEASE_MASK |
1043 GDK_ENTER_NOTIFY_MASK |
1044 GDK_LEAVE_NOTIFY_MASK |
1045 GDK_STRUCTURE_MASK);
1047 attributes_mask = GDK_WA_VISUAL;
1048 attributes_mask |= (title ? GDK_WA_TITLE : 0);
1049 attributes_mask |= (wmclass_name ? GDK_WA_WMCLASS : 0);
1051 if (gtk_widget_is_toplevel (widget))
1053 attributes.window_type = GDK_WINDOW_TOPLEVEL;
1055 gdk_error_trap_push ();
1056 if (priv->socket_window)
1057 gdk_window = gdk_window_new (priv->socket_window,
1058 &attributes, attributes_mask);
1059 else /* If it's a passive plug, we use the root window */
1060 gdk_window = gdk_window_new (gtk_widget_get_root_window (widget),
1061 &attributes, attributes_mask);
1062 gtk_widget_set_window (widget, gdk_window);
1064 gdk_display_sync (gtk_widget_get_display (widget));
1065 if (gdk_error_trap_pop ()) /* Uh-oh */
1067 gdk_error_trap_push ();
1068 gdk_window_destroy (gdk_window);
1069 gdk_error_trap_pop_ignored ();
1070 gdk_window = gdk_window_new (gtk_widget_get_root_window (widget),
1071 &attributes, attributes_mask);
1072 gtk_widget_set_window (widget, gdk_window);
1075 gdk_window_add_filter (gdk_window,
1076 gtk_plug_filter_func,
1079 priv->modality_group = gtk_window_group_new ();
1080 gtk_window_group_add_window (priv->modality_group, window);
1082 xembed_set_info (gtk_widget_get_window (GTK_WIDGET (plug)), 0);
1086 gdk_window = gdk_window_new (gtk_widget_get_parent_window (widget),
1087 &attributes, attributes_mask);
1088 gtk_widget_set_window (widget, gdk_window);
1091 gdk_window_set_user_data (gdk_window, window);
1093 gtk_style_context_set_background (gtk_widget_get_style_context (widget),
1096 gdk_window_enable_synchronized_configure (gdk_window);
1100 gtk_plug_show (GtkWidget *widget)
1102 if (gtk_widget_is_toplevel (widget))
1103 GTK_WIDGET_CLASS (gtk_plug_parent_class)->show (widget);
1105 GTK_WIDGET_CLASS (bin_class)->show (widget);
1109 gtk_plug_hide (GtkWidget *widget)
1111 if (gtk_widget_is_toplevel (widget))
1112 GTK_WIDGET_CLASS (gtk_plug_parent_class)->hide (widget);
1114 GTK_WIDGET_CLASS (bin_class)->hide (widget);
1117 /* From gdkinternals.h */
1118 void gdk_synthesize_window_state (GdkWindow *window,
1119 GdkWindowState unset_flags,
1120 GdkWindowState set_flags);
1123 gtk_plug_map (GtkWidget *widget)
1125 if (gtk_widget_is_toplevel (widget))
1127 GtkBin *bin = GTK_BIN (widget);
1128 GtkPlug *plug = GTK_PLUG (widget);
1131 gtk_widget_set_mapped (widget, TRUE);
1133 child = gtk_bin_get_child (bin);
1134 if (child != NULL &&
1135 gtk_widget_get_visible (child) &&
1136 !gtk_widget_get_mapped (child))
1137 gtk_widget_map (child);
1139 xembed_set_info (gtk_widget_get_window (GTK_WIDGET (plug)), XEMBED_MAPPED);
1141 gdk_synthesize_window_state (gtk_widget_get_window (widget),
1142 GDK_WINDOW_STATE_WITHDRAWN,
1146 GTK_WIDGET_CLASS (bin_class)->map (widget);
1150 gtk_plug_unmap (GtkWidget *widget)
1152 if (gtk_widget_is_toplevel (widget))
1154 GtkPlug *plug = GTK_PLUG (widget);
1158 window = gtk_widget_get_window (widget);
1160 gtk_widget_set_mapped (widget, FALSE);
1162 gdk_window_hide (window);
1164 child = gtk_bin_get_child (GTK_BIN (widget));
1166 gtk_widget_unmap (child);
1168 xembed_set_info (gtk_widget_get_window (GTK_WIDGET (plug)), 0);
1170 gdk_synthesize_window_state (window,
1172 GDK_WINDOW_STATE_WITHDRAWN);
1175 GTK_WIDGET_CLASS (bin_class)->unmap (widget);
1179 gtk_plug_size_allocate (GtkWidget *widget,
1180 GtkAllocation *allocation)
1184 if (gtk_widget_is_toplevel (widget))
1185 GTK_WIDGET_CLASS (gtk_plug_parent_class)->size_allocate (widget, allocation);
1188 GtkBin *bin = GTK_BIN (widget);
1190 gtk_widget_set_allocation (widget, allocation);
1192 if (gtk_widget_get_realized (widget))
1193 gdk_window_move_resize (gtk_widget_get_window (widget),
1194 allocation->x, allocation->y,
1195 allocation->width, allocation->height);
1197 child = gtk_bin_get_child (bin);
1199 if (child != NULL && gtk_widget_get_visible (child))
1201 GtkAllocation child_allocation;
1203 child_allocation.x = child_allocation.y = gtk_container_get_border_width (GTK_CONTAINER (widget));
1204 child_allocation.width =
1205 MAX (1, (gint)allocation->width - child_allocation.x * 2);
1206 child_allocation.height =
1207 MAX (1, (gint)allocation->height - child_allocation.y * 2);
1209 gtk_widget_size_allocate (child, &child_allocation);
1216 gtk_plug_key_press_event (GtkWidget *widget,
1219 if (gtk_widget_is_toplevel (widget))
1220 return GTK_WIDGET_CLASS (gtk_plug_parent_class)->key_press_event (widget, event);
1226 gtk_plug_focus_event (GtkWidget *widget,
1227 GdkEventFocus *event)
1229 /* We eat focus-in events and focus-out events, since they
1230 * can be generated by something like a keyboard grab on
1231 * a child of the plug.
1237 gtk_plug_set_focus (GtkWindow *window,
1240 GtkPlug *plug = GTK_PLUG (window);
1241 GtkPlugPrivate *priv = plug->priv;
1243 GTK_WINDOW_CLASS (gtk_plug_parent_class)->set_focus (window, focus);
1245 /* Ask for focus from embedder
1248 if (focus && !gtk_window_has_toplevel_focus (window))
1249 _gtk_xembed_send_message (priv->socket_window,
1250 XEMBED_REQUEST_FOCUS, 0, 0, 0);
1254 grabbed_key_hash (gconstpointer a)
1256 const GrabbedKey *key = a;
1259 h = key->accelerator_key << 16;
1260 h ^= key->accelerator_key >> 16;
1261 h ^= key->accelerator_mods;
1267 grabbed_key_equal (gconstpointer a, gconstpointer b)
1269 const GrabbedKey *keya = a;
1270 const GrabbedKey *keyb = b;
1272 return (keya->accelerator_key == keyb->accelerator_key &&
1273 keya->accelerator_mods == keyb->accelerator_mods);
1277 add_grabbed_key (gpointer key, gpointer val, gpointer data)
1279 GrabbedKey *grabbed_key = key;
1280 GtkPlug *plug = data;
1281 GtkPlugPrivate *priv = plug->priv;
1283 if (!priv->grabbed_keys ||
1284 !g_hash_table_lookup (priv->grabbed_keys, grabbed_key))
1286 _gtk_xembed_send_message (priv->socket_window, XEMBED_GTK_GRAB_KEY, 0,
1287 grabbed_key->accelerator_key,
1288 grabbed_key->accelerator_mods);
1293 remove_grabbed_key (gpointer key, gpointer val, gpointer data)
1295 GrabbedKey *grabbed_key = key;
1296 GtkPlug *plug = data;
1297 GtkPlugPrivate *priv = plug->priv;
1299 if (!priv->grabbed_keys ||
1300 !g_hash_table_lookup (priv->grabbed_keys, grabbed_key))
1302 _gtk_xembed_send_message (priv->socket_window, XEMBED_GTK_UNGRAB_KEY, 0,
1303 grabbed_key->accelerator_key,
1304 grabbed_key->accelerator_mods);
1309 keys_foreach (GtkWindow *window,
1311 GdkModifierType modifiers,
1312 gboolean is_mnemonic,
1315 GHashTable *new_grabbed_keys = data;
1316 GrabbedKey *key = g_slice_new (GrabbedKey);
1318 key->accelerator_key = keyval;
1319 key->accelerator_mods = modifiers;
1321 g_hash_table_replace (new_grabbed_keys, key, key);
1325 grabbed_key_free (gpointer data)
1327 g_slice_free (GrabbedKey, data);
1331 gtk_plug_keys_changed (GtkWindow *window)
1333 GHashTable *new_grabbed_keys, *old_grabbed_keys;
1334 GtkPlug *plug = GTK_PLUG (window);
1335 GtkPlugPrivate *priv = plug->priv;
1337 new_grabbed_keys = g_hash_table_new_full (grabbed_key_hash, grabbed_key_equal, (GDestroyNotify)grabbed_key_free, NULL);
1338 _gtk_window_keys_foreach (window, keys_foreach, new_grabbed_keys);
1340 if (priv->socket_window)
1341 g_hash_table_foreach (new_grabbed_keys, add_grabbed_key, plug);
1343 old_grabbed_keys = priv->grabbed_keys;
1344 priv->grabbed_keys = new_grabbed_keys;
1346 if (old_grabbed_keys)
1348 if (priv->socket_window)
1349 g_hash_table_foreach (old_grabbed_keys, remove_grabbed_key, plug);
1350 g_hash_table_destroy (old_grabbed_keys);
1355 gtk_plug_focus_to_parent (GtkPlug *plug,
1356 GtkDirectionType direction)
1358 GtkPlugPrivate *priv = plug->priv;
1359 XEmbedMessageType message;
1365 case GTK_DIR_TAB_BACKWARD:
1366 message = XEMBED_FOCUS_PREV;
1370 case GTK_DIR_TAB_FORWARD:
1371 message = XEMBED_FOCUS_NEXT;
1374 g_assert_not_reached ();
1375 message = XEMBED_FOCUS_PREV;
1379 _gtk_xembed_send_focus_message (priv->socket_window, message, 0);
1383 gtk_plug_focus (GtkWidget *widget,
1384 GtkDirectionType direction)
1386 GtkBin *bin = GTK_BIN (widget);
1387 GtkPlug *plug = GTK_PLUG (widget);
1388 GtkWindow *window = GTK_WINDOW (widget);
1389 GtkContainer *container = GTK_CONTAINER (widget);
1391 GtkWidget *old_focus_child;
1394 old_focus_child = gtk_container_get_focus_child (container);
1395 /* We override GtkWindow's behavior, since we don't want wrapping here.
1397 if (old_focus_child)
1399 GtkWidget *focus_widget;
1401 if (gtk_widget_child_focus (old_focus_child, direction))
1404 focus_widget = gtk_window_get_focus (window);
1407 /* Wrapped off the end, clear the focus setting for the toplevel */
1408 parent = gtk_widget_get_parent (focus_widget);
1411 gtk_container_set_focus_child (GTK_CONTAINER (parent), NULL);
1412 parent = gtk_widget_get_parent (parent);
1415 gtk_window_set_focus (GTK_WINDOW (container), NULL);
1420 /* Try to focus the first widget in the window */
1421 child = gtk_bin_get_child (bin);
1422 if (child && gtk_widget_child_focus (child, direction))
1426 if (!gtk_container_get_focus_child (GTK_CONTAINER (window)))
1427 gtk_plug_focus_to_parent (plug, direction);
1433 gtk_plug_check_resize (GtkContainer *container)
1435 if (gtk_widget_is_toplevel (GTK_WIDGET (container)))
1436 GTK_CONTAINER_CLASS (gtk_plug_parent_class)->check_resize (container);
1438 GTK_CONTAINER_CLASS (bin_class)->check_resize (container);