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/.
30 #include "gtksocket.h"
34 #include "gdk/gdkkeysyms.h"
36 #include "gtkmarshalers.h"
37 #include "gtksizerequest.h"
38 #include "gtkwindow.h"
40 #include "gtkprivate.h"
41 #include "gtksocketprivate.h"
45 #include "gtkwidgetprivate.h"
50 * @Short_description: Container for widgets from other processes
52 * @See_also: #GtkPlug, <ulink url="http://www.freedesktop.org/Standards/xembed-spec">XEmbed</ulink>
54 * Together with #GtkPlug, #GtkSocket provides the ability
55 * to embed widgets from one process into another process
56 * in a fashion that is transparent to the user. One
57 * process creates a #GtkSocket widget and passes
58 * that widget's window ID to the other process,
59 * which then creates a #GtkPlug with that window ID.
60 * Any widgets contained in the #GtkPlug then will appear
61 * inside the first application's window.
63 * The socket's window ID is obtained by using
64 * gtk_socket_get_id(). Before using this function,
65 * the socket must have been realized, and for hence,
66 * have been added to its parent.
69 * <title>Obtaining the window ID of a socket.</title>
71 * GtkWidget *socket = gtk_socket_new (<!-- -->);
72 * gtk_widget_show (socket);
73 * gtk_container_add (GTK_CONTAINER (parent), socket);
75 * /<!---->* The following call is only necessary if one of
76 * * the ancestors of the socket is not yet visible.
78 * gtk_widget_realize (socket);
79 * g_print ("The ID of the sockets window is %#x\n",
80 * gtk_socket_get_id (socket));
84 * Note that if you pass the window ID of the socket to another
85 * process that will create a plug in the socket, you
86 * must make sure that the socket widget is not destroyed
87 * until that plug is created. Violating this rule will
88 * cause unpredictable consequences, the most likely
89 * consequence being that the plug will appear as a
90 * separate toplevel window. You can check if the plug
91 * has been created by using gtk_socket_get_plug_window(). If
92 * it returns a non-%NULL value, then the plug has been
93 * successfully created inside of the socket.
95 * When GTK+ is notified that the embedded window has been
96 * destroyed, then it will destroy the socket as well. You
97 * should always, therefore, be prepared for your sockets
98 * to be destroyed at any time when the main event loop
99 * is running. To prevent this from happening, you can
100 * connect to the #GtkSocket::plug-removed signal.
102 * The communication between a #GtkSocket and a #GtkPlug follows the
103 * <ulink url="http://www.freedesktop.org/Standards/xembed-spec">XEmbed</ulink>
104 * protocol. This protocol has also been implemented in other toolkits, e.g.
105 * <application>Qt</application>, allowing the same level of integration
106 * when embedding a <application>Qt</application> widget in GTK or vice versa.
109 * The #GtkPlug and #GtkSocket widgets are currently not available
110 * on all platforms supported by GTK+.
114 /* Forward declararations */
116 static void gtk_socket_finalize (GObject *object);
117 static void gtk_socket_notify (GObject *object,
119 static void gtk_socket_realize (GtkWidget *widget);
120 static void gtk_socket_unrealize (GtkWidget *widget);
121 static void gtk_socket_get_preferred_width (GtkWidget *widget,
124 static void gtk_socket_get_preferred_height (GtkWidget *widget,
127 static void gtk_socket_size_allocate (GtkWidget *widget,
128 GtkAllocation *allocation);
129 static void gtk_socket_hierarchy_changed (GtkWidget *widget,
130 GtkWidget *old_toplevel);
131 static void gtk_socket_grab_notify (GtkWidget *widget,
132 gboolean was_grabbed);
133 static gboolean gtk_socket_key_event (GtkWidget *widget,
135 static gboolean gtk_socket_focus (GtkWidget *widget,
136 GtkDirectionType direction);
137 static void gtk_socket_remove (GtkContainer *container,
139 static void gtk_socket_forall (GtkContainer *container,
140 gboolean include_internals,
141 GtkCallback callback,
142 gpointer callback_data);
150 GdkModifierType accel_mods;
159 static guint socket_signals[LAST_SIGNAL] = { 0 };
162 * _gtk_socket_get_private:
164 * @socket: a #GtkSocket
166 * Returns the private data associated with a GtkSocket, creating it
167 * first if necessary.
170 _gtk_socket_get_private (GtkSocket *socket)
172 return G_TYPE_INSTANCE_GET_PRIVATE (socket, GTK_TYPE_SOCKET, GtkSocketPrivate);
175 G_DEFINE_TYPE (GtkSocket, gtk_socket, GTK_TYPE_CONTAINER)
178 gtk_socket_finalize (GObject *object)
180 GtkSocket *socket = GTK_SOCKET (object);
182 g_object_unref (socket->accel_group);
183 socket->accel_group = NULL;
185 G_OBJECT_CLASS (gtk_socket_parent_class)->finalize (object);
189 gtk_socket_class_init (GtkSocketClass *class)
191 GtkWidgetClass *widget_class;
192 GtkContainerClass *container_class;
193 GObjectClass *gobject_class;
195 gobject_class = (GObjectClass *) class;
196 widget_class = (GtkWidgetClass*) class;
197 container_class = (GtkContainerClass*) class;
199 gobject_class->finalize = gtk_socket_finalize;
200 gobject_class->notify = gtk_socket_notify;
202 widget_class->realize = gtk_socket_realize;
203 widget_class->unrealize = gtk_socket_unrealize;
204 widget_class->get_preferred_width = gtk_socket_get_preferred_width;
205 widget_class->get_preferred_height = gtk_socket_get_preferred_height;
206 widget_class->size_allocate = gtk_socket_size_allocate;
207 widget_class->hierarchy_changed = gtk_socket_hierarchy_changed;
208 widget_class->grab_notify = gtk_socket_grab_notify;
209 widget_class->key_press_event = gtk_socket_key_event;
210 widget_class->key_release_event = gtk_socket_key_event;
211 widget_class->focus = gtk_socket_focus;
213 /* We don't want to show_all the in-process plug, if any.
215 widget_class->show_all = gtk_widget_show;
217 container_class->remove = gtk_socket_remove;
218 container_class->forall = gtk_socket_forall;
221 * GtkSocket::plug-added:
222 * @socket_: the object which received the signal
224 * This signal is emitted when a client is successfully
225 * added to the socket.
227 socket_signals[PLUG_ADDED] =
228 g_signal_new (I_("plug-added"),
229 G_OBJECT_CLASS_TYPE (class),
231 G_STRUCT_OFFSET (GtkSocketClass, plug_added),
233 _gtk_marshal_VOID__VOID,
237 * GtkSocket::plug-removed:
238 * @socket_: the object which received the signal
240 * This signal is emitted when a client is removed from the socket.
241 * The default action is to destroy the #GtkSocket widget, so if you
242 * want to reuse it you must add a signal handler that returns %TRUE.
244 * Return value: %TRUE to stop other handlers from being invoked.
246 socket_signals[PLUG_REMOVED] =
247 g_signal_new (I_("plug-removed"),
248 G_OBJECT_CLASS_TYPE (class),
250 G_STRUCT_OFFSET (GtkSocketClass, plug_removed),
251 _gtk_boolean_handled_accumulator, NULL,
252 _gtk_marshal_BOOLEAN__VOID,
255 g_type_class_add_private (gobject_class, sizeof (GtkSocketPrivate));
259 gtk_socket_init (GtkSocket *socket)
261 socket->request_width = 0;
262 socket->request_height = 0;
263 socket->current_width = 0;
264 socket->current_height = 0;
266 socket->plug_window = NULL;
267 socket->plug_widget = NULL;
268 socket->focus_in = FALSE;
269 socket->have_size = FALSE;
270 socket->need_map = FALSE;
271 socket->active = FALSE;
273 socket->accel_group = gtk_accel_group_new ();
274 g_object_set_data (G_OBJECT (socket->accel_group), I_("gtk-socket"), socket);
280 * Create a new empty #GtkSocket.
282 * Return value: the new #GtkSocket.
285 gtk_socket_new (void)
289 socket = g_object_new (GTK_TYPE_SOCKET, NULL);
291 return GTK_WIDGET (socket);
296 * @socket_: a #GtkSocket
297 * @window_id: the window ID of a client participating in the XEMBED protocol.
299 * Adds an XEMBED client, such as a #GtkPlug, to the #GtkSocket. The
300 * client may be in the same process or in a different process.
302 * To embed a #GtkPlug in a #GtkSocket, you can either create the
303 * #GtkPlug with <literal>gtk_plug_new (0)</literal>, call
304 * gtk_plug_get_id() to get the window ID of the plug, and then pass that to the
305 * gtk_socket_add_id(), or you can call gtk_socket_get_id() to get the
306 * window ID for the socket, and call gtk_plug_new() passing in that
309 * The #GtkSocket must have already be added into a toplevel window
310 * before you can make this call.
313 gtk_socket_add_id (GtkSocket *socket,
314 GdkNativeWindow window_id)
316 g_return_if_fail (GTK_IS_SOCKET (socket));
317 g_return_if_fail (_gtk_widget_get_anchored (GTK_WIDGET (socket)));
319 if (!gtk_widget_get_realized (GTK_WIDGET (socket)))
320 gtk_widget_realize (GTK_WIDGET (socket));
322 _gtk_socket_add_window (socket, window_id, TRUE);
327 * @socket_: a #GtkSocket.
329 * Gets the window ID of a #GtkSocket widget, which can then
330 * be used to create a client embedded inside the socket, for
331 * instance with gtk_plug_new().
333 * The #GtkSocket must have already be added into a toplevel window
334 * before you can make this call.
336 * Return value: the window ID for the socket
339 gtk_socket_get_id (GtkSocket *socket)
341 g_return_val_if_fail (GTK_IS_SOCKET (socket), 0);
342 g_return_val_if_fail (_gtk_widget_get_anchored (GTK_WIDGET (socket)), 0);
344 if (!gtk_widget_get_realized (GTK_WIDGET (socket)))
345 gtk_widget_realize (GTK_WIDGET (socket));
347 return _gtk_socket_windowing_get_id (socket);
351 * gtk_socket_get_plug_window:
352 * @socket_: a #GtkSocket.
354 * Retrieves the window of the plug. Use this to check if the plug has
355 * been created inside of the socket.
357 * Return value: (transfer none): the window of the plug if available, or %NULL
362 gtk_socket_get_plug_window (GtkSocket *socket)
364 g_return_val_if_fail (GTK_IS_SOCKET (socket), NULL);
366 return socket->plug_window;
370 gtk_socket_realize (GtkWidget *widget)
372 GtkAllocation allocation;
373 GtkSocket *socket = GTK_SOCKET (widget);
375 GdkWindowAttr attributes;
376 gint attributes_mask;
378 gtk_widget_set_realized (widget, TRUE);
380 gtk_widget_get_allocation (widget, &allocation);
382 attributes.window_type = GDK_WINDOW_CHILD;
383 attributes.x = allocation.x;
384 attributes.y = allocation.y;
385 attributes.width = allocation.width;
386 attributes.height = allocation.height;
387 attributes.wclass = GDK_INPUT_OUTPUT;
388 attributes.visual = gtk_widget_get_visual (widget);
389 attributes.event_mask = GDK_FOCUS_CHANGE_MASK;
391 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
393 window = gdk_window_new (gtk_widget_get_parent_window (widget),
394 &attributes, attributes_mask);
395 gtk_widget_set_window (widget, window);
396 gdk_window_set_user_data (window, socket);
398 gtk_widget_style_attach (widget);
399 gtk_style_set_background (gtk_widget_get_style (widget),
400 window, GTK_STATE_NORMAL);
402 _gtk_socket_windowing_realize_window (socket);
404 gdk_window_add_filter (window,
405 _gtk_socket_windowing_filter_func,
408 /* We sync here so that we make sure that if the XID for
409 * our window is passed to another application, SubstructureRedirectMask
410 * will be set by the time the other app creates its window.
412 gdk_display_sync (gtk_widget_get_display (widget));
416 * _gtk_socket_end_embedding:
418 * @socket: a #GtkSocket
420 * Called to end the embedding of a plug in the socket.
423 _gtk_socket_end_embedding (GtkSocket *socket)
425 GtkSocketPrivate *private = _gtk_socket_get_private (socket);
426 GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (socket));
428 if (GTK_IS_WINDOW (toplevel))
429 _gtk_socket_windowing_end_embedding_toplevel (socket);
431 g_object_unref (socket->plug_window);
432 socket->plug_window = NULL;
433 socket->current_width = 0;
434 socket->current_height = 0;
435 private->resize_count = 0;
437 gtk_accel_group_disconnect (socket->accel_group, NULL);
441 gtk_socket_unrealize (GtkWidget *widget)
443 GtkSocket *socket = GTK_SOCKET (widget);
445 gtk_widget_set_realized (widget, FALSE);
447 if (socket->plug_widget)
449 _gtk_plug_remove_from_socket (GTK_PLUG (socket->plug_widget), socket);
451 else if (socket->plug_window)
453 _gtk_socket_end_embedding (socket);
456 GTK_WIDGET_CLASS (gtk_socket_parent_class)->unrealize (widget);
460 gtk_socket_get_preferred_width (GtkWidget *widget,
464 GtkSocket *socket = GTK_SOCKET (widget);
466 if (socket->plug_widget)
468 gtk_widget_get_preferred_width (socket->plug_widget, minimum, natural);
472 if (socket->is_mapped && !socket->have_size && socket->plug_window)
473 _gtk_socket_windowing_size_request (socket);
475 if (socket->is_mapped && socket->have_size)
476 *minimum = *natural = MAX (socket->request_width, 1);
478 *minimum = *natural = 1;
483 gtk_socket_get_preferred_height (GtkWidget *widget,
487 GtkSocket *socket = GTK_SOCKET (widget);
489 if (socket->plug_widget)
491 gtk_widget_get_preferred_height (socket->plug_widget, minimum, natural);
495 if (socket->is_mapped && !socket->have_size && socket->plug_window)
496 _gtk_socket_windowing_size_request (socket);
498 if (socket->is_mapped && socket->have_size)
499 *minimum = *natural = MAX (socket->request_height, 1);
501 *minimum = *natural = 1;
506 gtk_socket_size_allocate (GtkWidget *widget,
507 GtkAllocation *allocation)
509 GtkSocket *socket = GTK_SOCKET (widget);
511 gtk_widget_set_allocation (widget, allocation);
512 if (gtk_widget_get_realized (widget))
514 gdk_window_move_resize (gtk_widget_get_window (widget),
515 allocation->x, allocation->y,
516 allocation->width, allocation->height);
518 if (socket->plug_widget)
520 GtkAllocation child_allocation;
522 child_allocation.x = 0;
523 child_allocation.y = 0;
524 child_allocation.width = allocation->width;
525 child_allocation.height = allocation->height;
527 gtk_widget_size_allocate (socket->plug_widget, &child_allocation);
529 else if (socket->plug_window)
531 GtkSocketPrivate *private = _gtk_socket_get_private (socket);
533 gdk_error_trap_push ();
535 if (allocation->width != socket->current_width ||
536 allocation->height != socket->current_height)
538 gdk_window_move_resize (socket->plug_window,
540 allocation->width, allocation->height);
541 if (private->resize_count)
542 private->resize_count--;
544 GTK_NOTE (PLUGSOCKET,
545 g_message ("GtkSocket - allocated: %d %d",
546 allocation->width, allocation->height));
547 socket->current_width = allocation->width;
548 socket->current_height = allocation->height;
551 if (socket->need_map)
553 gdk_window_show (socket->plug_window);
554 socket->need_map = FALSE;
557 while (private->resize_count)
559 _gtk_socket_windowing_send_configure_event (socket);
560 private->resize_count--;
561 GTK_NOTE (PLUGSOCKET,
562 g_message ("GtkSocket - sending synthetic configure: %d %d",
563 allocation->width, allocation->height));
566 gdk_error_trap_pop_ignored ();
572 activate_key (GtkAccelGroup *accel_group,
573 GObject *acceleratable,
575 GdkModifierType accel_mods,
576 GrabbedKey *grabbed_key)
578 GdkEvent *gdk_event = gtk_get_current_event ();
580 GtkSocket *socket = g_object_get_data (G_OBJECT (accel_group), "gtk-socket");
581 gboolean retval = FALSE;
583 if (gdk_event && gdk_event->type == GDK_KEY_PRESS && socket->plug_window)
585 _gtk_socket_windowing_send_key_event (socket, gdk_event, FALSE);
590 gdk_event_free (gdk_event);
596 find_accel_key (GtkAccelKey *key,
600 GrabbedKey *grabbed_key = data;
602 return (key->accel_key == grabbed_key->accel_key &&
603 key->accel_mods == grabbed_key->accel_mods);
607 * _gtk_socket_add_grabbed_key:
609 * @socket: a #GtkSocket
611 * @modifiers: modifiers for the key
613 * Called from the GtkSocket platform-specific backend when the
614 * corresponding plug has told the socket to grab a key.
617 _gtk_socket_add_grabbed_key (GtkSocket *socket,
619 GdkModifierType modifiers)
622 GrabbedKey *grabbed_key;
624 grabbed_key = g_new (GrabbedKey, 1);
626 grabbed_key->accel_key = keyval;
627 grabbed_key->accel_mods = modifiers;
629 if (gtk_accel_group_find (socket->accel_group,
633 g_warning ("GtkSocket: request to add already present grabbed key %u,%#x\n",
635 g_free (grabbed_key);
639 closure = g_cclosure_new (G_CALLBACK (activate_key), grabbed_key, (GClosureNotify)g_free);
641 gtk_accel_group_connect (socket->accel_group, keyval, modifiers, GTK_ACCEL_LOCKED,
646 * _gtk_socket_remove_grabbed_key:
648 * @socket: a #GtkSocket
650 * @modifiers: modifiers for the key
652 * Called from the GtkSocket backend when the corresponding plug has
653 * told the socket to remove a key grab.
656 _gtk_socket_remove_grabbed_key (GtkSocket *socket,
658 GdkModifierType modifiers)
660 if (!gtk_accel_group_disconnect_key (socket->accel_group, keyval, modifiers))
661 g_warning ("GtkSocket: request to remove non-present grabbed key %u,%#x\n",
666 socket_update_focus_in (GtkSocket *socket)
668 gboolean focus_in = FALSE;
670 if (socket->plug_window)
672 GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (socket));
674 if (gtk_widget_is_toplevel (toplevel) &&
675 gtk_window_has_toplevel_focus (GTK_WINDOW (toplevel)) &&
676 gtk_widget_is_focus (GTK_WIDGET (socket)))
680 if (focus_in != socket->focus_in)
682 socket->focus_in = focus_in;
684 _gtk_socket_windowing_focus_change (socket, focus_in);
689 socket_update_active (GtkSocket *socket)
691 gboolean active = FALSE;
693 if (socket->plug_window)
695 GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (socket));
697 if (gtk_widget_is_toplevel (toplevel) &&
698 gtk_window_is_active (GTK_WINDOW (toplevel)))
702 if (active != socket->active)
704 socket->active = active;
706 _gtk_socket_windowing_update_active (socket, active);
711 gtk_socket_hierarchy_changed (GtkWidget *widget,
712 GtkWidget *old_toplevel)
714 GtkSocket *socket = GTK_SOCKET (widget);
715 GtkWidget *toplevel = gtk_widget_get_toplevel (widget);
717 if (toplevel && !GTK_IS_WINDOW (toplevel))
720 if (toplevel != socket->toplevel)
722 if (socket->toplevel)
724 gtk_window_remove_accel_group (GTK_WINDOW (socket->toplevel), socket->accel_group);
725 g_signal_handlers_disconnect_by_func (socket->toplevel,
726 socket_update_focus_in,
728 g_signal_handlers_disconnect_by_func (socket->toplevel,
729 socket_update_active,
733 socket->toplevel = toplevel;
737 gtk_window_add_accel_group (GTK_WINDOW (socket->toplevel), socket->accel_group);
738 g_signal_connect_swapped (socket->toplevel, "notify::has-toplevel-focus",
739 G_CALLBACK (socket_update_focus_in), socket);
740 g_signal_connect_swapped (socket->toplevel, "notify::is-active",
741 G_CALLBACK (socket_update_active), socket);
744 socket_update_focus_in (socket);
745 socket_update_active (socket);
750 gtk_socket_grab_notify (GtkWidget *widget,
751 gboolean was_grabbed)
753 GtkSocket *socket = GTK_SOCKET (widget);
755 if (!socket->same_app)
756 _gtk_socket_windowing_update_modality (socket, !was_grabbed);
760 gtk_socket_key_event (GtkWidget *widget,
763 GtkSocket *socket = GTK_SOCKET (widget);
765 if (gtk_widget_has_focus (widget) && socket->plug_window && !socket->plug_widget)
767 _gtk_socket_windowing_send_key_event (socket, (GdkEvent *) event, FALSE);
776 gtk_socket_notify (GObject *object,
779 if (!strcmp (pspec->name, "is-focus"))
781 socket_update_focus_in (GTK_SOCKET (object));
785 * _gtk_socket_claim_focus:
787 * @socket: a #GtkSocket
790 * Claims focus for the socket. XXX send_event?
793 _gtk_socket_claim_focus (GtkSocket *socket,
796 GtkWidget *widget = GTK_WIDGET (socket);
799 socket->focus_in = TRUE; /* Otherwise, our notify handler will send FOCUS_IN */
801 /* Oh, the trickery... */
803 gtk_widget_set_can_focus (widget, TRUE);
804 gtk_widget_grab_focus (widget);
805 gtk_widget_set_can_focus (widget, FALSE);
809 gtk_socket_focus (GtkWidget *widget,
810 GtkDirectionType direction)
812 GtkSocket *socket = GTK_SOCKET (widget);
814 if (socket->plug_widget)
815 return gtk_widget_child_focus (socket->plug_widget, direction);
817 if (!gtk_widget_is_focus (widget))
819 _gtk_socket_windowing_focus (socket, direction);
820 _gtk_socket_claim_focus (socket, FALSE);
829 gtk_socket_remove (GtkContainer *container,
832 GtkSocket *socket = GTK_SOCKET (container);
834 g_return_if_fail (child == socket->plug_widget);
836 _gtk_plug_remove_from_socket (GTK_PLUG (socket->plug_widget), socket);
840 gtk_socket_forall (GtkContainer *container,
841 gboolean include_internals,
842 GtkCallback callback,
843 gpointer callback_data)
845 GtkSocket *socket = GTK_SOCKET (container);
847 if (socket->plug_widget)
848 (* callback) (socket->plug_widget, callback_data);
852 * _gtk_socket_add_window:
854 * @socket: a #GtkSocket
855 * @xid: the native identifier for a window
856 * @need_reparent: whether the socket's plug's window needs to be
857 * reparented to the socket
859 * Adds a window to a GtkSocket.
862 _gtk_socket_add_window (GtkSocket *socket,
864 gboolean need_reparent)
866 GtkWidget *widget = GTK_WIDGET (socket);
867 GdkDisplay *display = gtk_widget_get_display (widget);
868 gpointer user_data = NULL;
870 socket->plug_window = gdk_window_lookup_for_display (display, xid);
872 if (socket->plug_window)
874 g_object_ref (socket->plug_window);
875 gdk_window_get_user_data (socket->plug_window, &user_data);
878 if (user_data) /* A widget's window in this process */
880 GtkWidget *child_widget = user_data;
882 if (!GTK_IS_PLUG (child_widget))
884 g_warning (G_STRLOC ": Can't add non-GtkPlug to GtkSocket");
885 socket->plug_window = NULL;
886 gdk_error_trap_pop_ignored ();
891 _gtk_plug_add_to_socket (GTK_PLUG (child_widget), socket);
893 else /* A foreign window */
896 GdkDragProtocol protocol;
898 gdk_error_trap_push ();
900 if (!socket->plug_window)
902 socket->plug_window = gdk_window_foreign_new_for_display (display, xid);
903 if (!socket->plug_window) /* was deleted before we could get it */
905 gdk_error_trap_pop_ignored ();
910 _gtk_socket_windowing_select_plug_window_input (socket);
912 if (gdk_error_trap_pop ())
914 g_object_unref (socket->plug_window);
915 socket->plug_window = NULL;
919 /* OK, we now will reliably get destroy notification on socket->plug_window */
921 gdk_error_trap_push ();
925 gdk_window_hide (socket->plug_window); /* Shouldn't actually be necessary for XEMBED, but just in case */
926 gdk_window_reparent (socket->plug_window,
927 gtk_widget_get_window (widget),
931 socket->have_size = FALSE;
933 _gtk_socket_windowing_embed_get_info (socket);
935 socket->need_map = socket->is_mapped;
937 if (gdk_drag_get_protocol_for_display (display, xid, &protocol))
938 gtk_drag_dest_set_proxy (GTK_WIDGET (socket), socket->plug_window,
941 gdk_error_trap_pop_ignored ();
943 gdk_window_add_filter (socket->plug_window,
944 _gtk_socket_windowing_filter_func,
947 /* Add a pointer to the socket on our toplevel window */
949 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (socket));
950 if (GTK_IS_WINDOW (toplevel))
951 gtk_window_add_embedded_xid (GTK_WINDOW (toplevel), xid);
953 _gtk_socket_windowing_embed_notify (socket);
955 socket_update_active (socket);
956 socket_update_focus_in (socket);
958 gtk_widget_queue_resize (GTK_WIDGET (socket));
961 if (socket->plug_window)
962 g_signal_emit (socket, socket_signals[PLUG_ADDED], 0);
966 * _gtk_socket_handle_map_request:
968 * @socket: a #GtkSocket
970 * Called from the GtkSocket backend when the plug has been mapped.
973 _gtk_socket_handle_map_request (GtkSocket *socket)
975 if (!socket->is_mapped)
977 socket->is_mapped = TRUE;
978 socket->need_map = TRUE;
980 gtk_widget_queue_resize (GTK_WIDGET (socket));
985 * _gtk_socket_unmap_notify:
987 * @socket: a #GtkSocket
989 * Called from the GtkSocket backend when the plug has been unmapped ???
992 _gtk_socket_unmap_notify (GtkSocket *socket)
994 if (socket->is_mapped)
996 socket->is_mapped = FALSE;
997 gtk_widget_queue_resize (GTK_WIDGET (socket));
1002 * _gtk_socket_advance_toplevel_focus:
1004 * @socket: a #GtkSocket
1005 * @direction: a direction
1007 * Called from the GtkSocket backend when the corresponding plug
1008 * has told the socket to move the focus.
1011 _gtk_socket_advance_toplevel_focus (GtkSocket *socket,
1012 GtkDirectionType direction)
1016 GtkContainer *container;
1018 GtkWidget *focus_widget;
1019 GtkWidget *toplevel;
1020 GtkWidget *old_focus_child;
1023 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (socket));
1027 if (!gtk_widget_is_toplevel (toplevel) || GTK_IS_PLUG (toplevel))
1029 gtk_widget_child_focus (toplevel,direction);
1033 container = GTK_CONTAINER (toplevel);
1034 window = GTK_WINDOW (toplevel);
1035 bin = GTK_BIN (toplevel);
1037 /* This is a copy of gtk_window_focus(), modified so that we
1038 * can detect wrap-around.
1040 old_focus_child = gtk_container_get_focus_child (container);
1042 if (old_focus_child)
1044 if (gtk_widget_child_focus (old_focus_child, direction))
1047 /* We are allowed exactly one wrap-around per sequence of focus
1050 if (_gtk_socket_windowing_embed_get_focus_wrapped ())
1053 _gtk_socket_windowing_embed_set_focus_wrapped ();
1056 focus_widget = gtk_window_get_focus (window);
1059 /* Wrapped off the end, clear the focus setting for the toplevel */
1060 parent = gtk_widget_get_parent (focus_widget);
1063 gtk_container_set_focus_child (GTK_CONTAINER (parent), NULL);
1064 parent = gtk_widget_get_parent (parent);
1067 gtk_window_set_focus (GTK_WINDOW (container), NULL);
1070 /* Now try to focus the first widget in the window */
1071 child = gtk_bin_get_child (bin);
1074 if (gtk_widget_child_focus (child, direction))