]> Pileus Git - ~andy/gtk/blob - gtk/gtksocket.c
9d679d11d186eff9173c26c13d0339be9d77b5a0
[~andy/gtk] / gtk / gtksocket.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free
16  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  */
18
19 /* By Owen Taylor <otaylor@gtk.org>              98/4/4 */
20
21 /*
22  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
23  * file for a list of people on the GTK+ Team.  See the ChangeLog
24  * files for a list of changes.  These files are distributed with
25  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
26  */
27
28 #include "config.h"
29 #include <string.h>
30
31 #include "gdk/gdkkeysyms.h"
32 #include "gtkmain.h"
33 #include "gtkmarshalers.h"
34 #include "gtkwindow.h"
35 #include "gtkplug.h"
36 #include "gtkprivate.h"
37 #include "gtksocket.h"
38 #include "gtksocketprivate.h"
39 #include "gtkdnd.h"
40 #include "gtkintl.h"
41
42
43 /**
44  * SECTION:gtksocket
45  * @Short_description: Container for widgets from other processes
46  * @Title: GtkSocket
47  * @See_also: #GtkPlug, <ulink url="http://www.freedesktop.org/Standards/xembed-spec">XEmbed</ulink>
48  *
49  * Together with #GtkPlug, #GtkSocket provides the ability
50  * to embed widgets from one process into another process
51  * in a fashion that is transparent to the user. One
52  * process creates a #GtkSocket widget and passes
53  * that widget's window ID to the other process,
54  * which then creates a #GtkPlug with that window ID.
55  * Any widgets contained in the #GtkPlug then will appear
56  * inside the first application's window.
57  *
58  * The socket's window ID is obtained by using
59  * gtk_socket_get_id(). Before using this function,
60  * the socket must have been realized, and for hence,
61  * have been added to its parent.
62  *
63  * <example>
64  * <title>Obtaining the window ID of a socket.</title>
65  * <programlisting>
66  * GtkWidget *socket = gtk_socket_new (<!-- -->);
67  * gtk_widget_show (socket);
68  * gtk_container_add (GTK_CONTAINER (parent), socket);
69  *
70  * /<!---->* The following call is only necessary if one of
71  *  * the ancestors of the socket is not yet visible.
72  *  *<!---->/
73  * gtk_widget_realize (socket);
74  * g_print ("The ID of the sockets window is %#x\n",
75  *          gtk_socket_get_id (socket));
76  * </programlisting>
77  * </example>
78  *
79  * Note that if you pass the window ID of the socket to another
80  * process that will create a plug in the socket, you
81  * must make sure that the socket widget is not destroyed
82  * until that plug is created. Violating this rule will
83  * cause unpredictable consequences, the most likely
84  * consequence being that the plug will appear as a
85  * separate toplevel window. You can check if the plug
86  * has been created by using gtk_socket_get_plug_window(). If
87  * it returns a non-%NULL value, then the plug has been
88  * successfully created inside of the socket.
89  *
90  * When GTK+ is notified that the embedded window has been
91  * destroyed, then it will destroy the socket as well. You
92  * should always, therefore, be prepared for your sockets
93  * to be destroyed at any time when the main event loop
94  * is running. To prevent this from happening, you can
95  * connect to the #GtkSocket::plug-removed signal.
96  *
97  * The communication between a #GtkSocket and a #GtkPlug follows the
98  * <ulink url="http://www.freedesktop.org/Standards/xembed-spec">XEmbed</ulink>
99  * protocol. This protocol has also been implemented in other toolkits, e.g.
100  * <application>Qt</application>, allowing the same level of integration
101  * when embedding a <application>Qt</application> widget in GTK or vice versa.
102  *
103  * A socket can also be used to swallow arbitrary
104  * pre-existing top-level windows using gtk_socket_steal(),
105  * though the integration when this is done will not be as close
106  * as between a #GtkPlug and a #GtkSocket.
107  *
108  * <note>
109  * The #GtkPlug and #GtkSocket widgets are currently not available
110  * on all platforms supported by GTK+.
111  * </note>
112  */
113
114 /* Forward declararations */
115
116 static void     gtk_socket_finalize             (GObject          *object);
117 static void     gtk_socket_notify               (GObject          *object,
118                                                  GParamSpec       *pspec);
119 static void     gtk_socket_realize              (GtkWidget        *widget);
120 static void     gtk_socket_unrealize            (GtkWidget        *widget);
121 static void     gtk_socket_size_request         (GtkWidget        *widget,
122                                                  GtkRequisition   *requisition);
123 static void     gtk_socket_size_allocate        (GtkWidget        *widget,
124                                                  GtkAllocation    *allocation);
125 static void     gtk_socket_hierarchy_changed    (GtkWidget        *widget,
126                                                  GtkWidget        *old_toplevel);
127 static void     gtk_socket_grab_notify          (GtkWidget        *widget,
128                                                  gboolean          was_grabbed);
129 static gboolean gtk_socket_key_event            (GtkWidget        *widget,
130                                                  GdkEventKey      *event);
131 static gboolean gtk_socket_focus                (GtkWidget        *widget,
132                                                  GtkDirectionType  direction);
133 static void     gtk_socket_remove               (GtkContainer     *container,
134                                                  GtkWidget        *widget);
135 static void     gtk_socket_forall               (GtkContainer     *container,
136                                                  gboolean          include_internals,
137                                                  GtkCallback       callback,
138                                                  gpointer          callback_data);
139
140
141 /* Local data */
142
143 typedef struct
144 {
145   guint                  accel_key;
146   GdkModifierType        accel_mods;
147 } GrabbedKey;
148
149 enum {
150   PLUG_ADDED,
151   PLUG_REMOVED,
152   LAST_SIGNAL
153 }; 
154
155 static guint socket_signals[LAST_SIGNAL] = { 0 };
156
157 /*
158  * _gtk_socket_get_private:
159  *
160  * @socket: a #GtkSocket
161  *
162  * Returns the private data associated with a GtkSocket, creating it
163  * first if necessary.
164  */
165 GtkSocketPrivate *
166 _gtk_socket_get_private (GtkSocket *socket)
167 {
168   return G_TYPE_INSTANCE_GET_PRIVATE (socket, GTK_TYPE_SOCKET, GtkSocketPrivate);
169 }
170
171 G_DEFINE_TYPE (GtkSocket, gtk_socket, GTK_TYPE_CONTAINER)
172
173 static void
174 gtk_socket_finalize (GObject *object)
175 {
176   GtkSocket *socket = GTK_SOCKET (object);
177   
178   g_object_unref (socket->accel_group);
179   socket->accel_group = NULL;
180
181   G_OBJECT_CLASS (gtk_socket_parent_class)->finalize (object);
182 }
183
184 static void
185 gtk_socket_class_init (GtkSocketClass *class)
186 {
187   GtkWidgetClass *widget_class;
188   GtkContainerClass *container_class;
189   GObjectClass *gobject_class;
190
191   gobject_class = (GObjectClass *) class;
192   widget_class = (GtkWidgetClass*) class;
193   container_class = (GtkContainerClass*) class;
194
195   gobject_class->finalize = gtk_socket_finalize;
196   gobject_class->notify = gtk_socket_notify;
197
198   widget_class->realize = gtk_socket_realize;
199   widget_class->unrealize = gtk_socket_unrealize;
200   widget_class->size_request = gtk_socket_size_request;
201   widget_class->size_allocate = gtk_socket_size_allocate;
202   widget_class->hierarchy_changed = gtk_socket_hierarchy_changed;
203   widget_class->grab_notify = gtk_socket_grab_notify;
204   widget_class->key_press_event = gtk_socket_key_event;
205   widget_class->key_release_event = gtk_socket_key_event;
206   widget_class->focus = gtk_socket_focus;
207
208   /* We don't want to show_all/hide_all the in-process
209    * plug, if any.
210    */
211   widget_class->show_all = gtk_widget_show;
212   widget_class->hide_all = gtk_widget_hide;
213   
214   container_class->remove = gtk_socket_remove;
215   container_class->forall = gtk_socket_forall;
216
217   /**
218    * GtkSocket::plug-added:
219    * @socket_: the object which received the signal
220    *
221    * This signal is emitted when a client is successfully
222    * added to the socket. 
223    */
224   socket_signals[PLUG_ADDED] =
225     g_signal_new (I_("plug-added"),
226                   G_OBJECT_CLASS_TYPE (class),
227                   G_SIGNAL_RUN_LAST,
228                   G_STRUCT_OFFSET (GtkSocketClass, plug_added),
229                   NULL, NULL,
230                   _gtk_marshal_VOID__VOID,
231                   G_TYPE_NONE, 0);
232
233   /**
234    * GtkSocket::plug-removed:
235    * @socket_: the object which received the signal
236    *
237    * This signal is emitted when a client is removed from the socket. 
238    * The default action is to destroy the #GtkSocket widget, so if you 
239    * want to reuse it you must add a signal handler that returns %TRUE. 
240    *
241    * Return value: %TRUE to stop other handlers from being invoked.
242    */
243   socket_signals[PLUG_REMOVED] =
244     g_signal_new (I_("plug-removed"),
245                   G_OBJECT_CLASS_TYPE (class),
246                   G_SIGNAL_RUN_LAST,
247                   G_STRUCT_OFFSET (GtkSocketClass, plug_removed),
248                   _gtk_boolean_handled_accumulator, NULL,
249                   _gtk_marshal_BOOLEAN__VOID,
250                   G_TYPE_BOOLEAN, 0);
251
252   g_type_class_add_private (gobject_class, sizeof (GtkSocketPrivate));
253 }
254
255 static void
256 gtk_socket_init (GtkSocket *socket)
257 {
258   socket->request_width = 0;
259   socket->request_height = 0;
260   socket->current_width = 0;
261   socket->current_height = 0;
262   
263   socket->plug_window = NULL;
264   socket->plug_widget = NULL;
265   socket->focus_in = FALSE;
266   socket->have_size = FALSE;
267   socket->need_map = FALSE;
268   socket->active = FALSE;
269
270   socket->accel_group = gtk_accel_group_new ();
271   g_object_set_data (G_OBJECT (socket->accel_group), I_("gtk-socket"), socket);
272 }
273
274 /**
275  * gtk_socket_new:
276  * 
277  * Create a new empty #GtkSocket.
278  * 
279  * Return value:  the new #GtkSocket.
280  **/
281 GtkWidget*
282 gtk_socket_new (void)
283 {
284   GtkSocket *socket;
285
286   socket = g_object_new (GTK_TYPE_SOCKET, NULL);
287
288   return GTK_WIDGET (socket);
289 }
290
291 /**
292  * gtk_socket_add_id:
293  * @socket_: a #GtkSocket
294  * @window_id: the window ID of a client participating in the XEMBED protocol.
295  *
296  * Adds an XEMBED client, such as a #GtkPlug, to the #GtkSocket.  The
297  * client may be in the same process or in a different process. 
298  * 
299  * To embed a #GtkPlug in a #GtkSocket, you can either create the
300  * #GtkPlug with <literal>gtk_plug_new (0)</literal>, call 
301  * gtk_plug_get_id() to get the window ID of the plug, and then pass that to the
302  * gtk_socket_add_id(), or you can call gtk_socket_get_id() to get the
303  * window ID for the socket, and call gtk_plug_new() passing in that
304  * ID.
305  *
306  * The #GtkSocket must have already be added into a toplevel window
307  *  before you can make this call.
308  **/
309 void           
310 gtk_socket_add_id (GtkSocket      *socket,
311                    GdkNativeWindow window_id)
312 {
313   g_return_if_fail (GTK_IS_SOCKET (socket));
314   g_return_if_fail (GTK_WIDGET_ANCHORED (socket));
315
316   if (!gtk_widget_get_realized (GTK_WIDGET (socket)))
317     gtk_widget_realize (GTK_WIDGET (socket));
318
319   _gtk_socket_add_window (socket, window_id, TRUE);
320 }
321
322 /**
323  * gtk_socket_get_id:
324  * @socket_: a #GtkSocket.
325  * 
326  * Gets the window ID of a #GtkSocket widget, which can then
327  * be used to create a client embedded inside the socket, for
328  * instance with gtk_plug_new(). 
329  *
330  * The #GtkSocket must have already be added into a toplevel window 
331  * before you can make this call.
332  * 
333  * Return value: the window ID for the socket
334  **/
335 GdkNativeWindow
336 gtk_socket_get_id (GtkSocket *socket)
337 {
338   g_return_val_if_fail (GTK_IS_SOCKET (socket), 0);
339   g_return_val_if_fail (GTK_WIDGET_ANCHORED (socket), 0);
340
341   if (!gtk_widget_get_realized (GTK_WIDGET (socket)))
342     gtk_widget_realize (GTK_WIDGET (socket));
343
344   return _gtk_socket_windowing_get_id (socket);
345 }
346
347 /**
348  * gtk_socket_get_plug_window:
349  * @socket_: a #GtkSocket.
350  *
351  * Retrieves the window of the plug. Use this to check if the plug has
352  * been created inside of the socket.
353  *
354  * Return value: the window of the plug if available, or %NULL
355  *
356  * Since:  2.14
357  **/
358 GdkWindow*
359 gtk_socket_get_plug_window (GtkSocket *socket)
360 {
361   g_return_val_if_fail (GTK_IS_SOCKET (socket), NULL);
362
363   return socket->plug_window;
364 }
365
366 static void
367 gtk_socket_realize (GtkWidget *widget)
368 {
369   GtkSocket *socket = GTK_SOCKET (widget);
370   GdkWindowAttr attributes;
371   gint attributes_mask;
372
373   gtk_widget_set_realized (widget, TRUE);
374
375   attributes.window_type = GDK_WINDOW_CHILD;
376   attributes.x = widget->allocation.x;
377   attributes.y = widget->allocation.y;
378   attributes.width = widget->allocation.width;
379   attributes.height = widget->allocation.height;
380   attributes.wclass = GDK_INPUT_OUTPUT;
381   attributes.visual = gtk_widget_get_visual (widget);
382   attributes.colormap = gtk_widget_get_colormap (widget);
383   attributes.event_mask = GDK_FOCUS_CHANGE_MASK;
384
385   attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
386
387   widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), 
388                                    &attributes, attributes_mask);
389   gdk_window_set_user_data (widget->window, socket);
390
391   widget->style = gtk_style_attach (widget->style, widget->window);
392   gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
393
394   _gtk_socket_windowing_realize_window (socket);
395
396   gdk_window_add_filter (widget->window,
397                          _gtk_socket_windowing_filter_func,
398                          widget);
399
400   /* We sync here so that we make sure that if the XID for
401    * our window is passed to another application, SubstructureRedirectMask
402    * will be set by the time the other app creates its window.
403    */
404   gdk_display_sync (gtk_widget_get_display (widget));
405 }
406
407 /**
408  * _gtk_socket_end_embedding:
409  *
410  * @socket: a #GtkSocket
411  *
412  * Called to end the embedding of a plug in the socket.
413  */
414 void
415 _gtk_socket_end_embedding (GtkSocket *socket)
416 {
417   GtkSocketPrivate *private = _gtk_socket_get_private (socket);
418   GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (socket));
419   
420   if (GTK_IS_WINDOW (toplevel))
421     _gtk_socket_windowing_end_embedding_toplevel (socket);
422
423   g_object_unref (socket->plug_window);
424   socket->plug_window = NULL;
425   socket->current_width = 0;
426   socket->current_height = 0;
427   private->resize_count = 0;
428
429   gtk_accel_group_disconnect (socket->accel_group, NULL);
430 }
431
432 static void
433 gtk_socket_unrealize (GtkWidget *widget)
434 {
435   GtkSocket *socket = GTK_SOCKET (widget);
436
437   gtk_widget_set_realized (widget, FALSE);
438
439   if (socket->plug_widget)
440     {
441       _gtk_plug_remove_from_socket (GTK_PLUG (socket->plug_widget), socket);
442     }
443   else if (socket->plug_window)
444     {
445       _gtk_socket_end_embedding (socket);
446     }
447
448   GTK_WIDGET_CLASS (gtk_socket_parent_class)->unrealize (widget);
449 }
450
451 static void
452 gtk_socket_size_request (GtkWidget      *widget,
453                          GtkRequisition *requisition)
454 {
455   GtkSocket *socket = GTK_SOCKET (widget);
456
457   if (socket->plug_widget)
458     {
459       gtk_widget_size_request (socket->plug_widget, requisition);
460     }
461   else
462     {
463       if (socket->is_mapped && !socket->have_size && socket->plug_window)
464         _gtk_socket_windowing_size_request (socket);
465
466       if (socket->is_mapped && socket->have_size)
467         {
468           requisition->width = MAX (socket->request_width, 1);
469           requisition->height = MAX (socket->request_height, 1);
470         }
471       else
472         {
473           requisition->width = 1;
474           requisition->height = 1;
475         }
476     }
477 }
478
479 static void
480 gtk_socket_size_allocate (GtkWidget     *widget,
481                           GtkAllocation *allocation)
482 {
483   GtkSocket *socket = GTK_SOCKET (widget);
484
485   widget->allocation = *allocation;
486   if (gtk_widget_get_realized (widget))
487     {
488       gdk_window_move_resize (widget->window,
489                               allocation->x, allocation->y,
490                               allocation->width, allocation->height);
491
492       if (socket->plug_widget)
493         {
494           GtkAllocation child_allocation;
495
496           child_allocation.x = 0;
497           child_allocation.y = 0;
498           child_allocation.width = allocation->width;
499           child_allocation.height = allocation->height;
500
501           gtk_widget_size_allocate (socket->plug_widget, &child_allocation);
502         }
503       else if (socket->plug_window)
504         {
505           GtkSocketPrivate *private = _gtk_socket_get_private (socket);
506           
507           gdk_error_trap_push ();
508           
509           if (allocation->width != socket->current_width ||
510               allocation->height != socket->current_height)
511             {
512               gdk_window_move_resize (socket->plug_window,
513                                       0, 0,
514                                       allocation->width, allocation->height);
515               if (private->resize_count)
516                 private->resize_count--;
517               
518               GTK_NOTE (PLUGSOCKET,
519                         g_message ("GtkSocket - allocated: %d %d",
520                                    allocation->width, allocation->height));
521               socket->current_width = allocation->width;
522               socket->current_height = allocation->height;
523             }
524
525           if (socket->need_map)
526             {
527               gdk_window_show (socket->plug_window);
528               socket->need_map = FALSE;
529             }
530
531           while (private->resize_count)
532             {
533               _gtk_socket_windowing_send_configure_event (socket);
534               private->resize_count--;
535               GTK_NOTE (PLUGSOCKET,
536                         g_message ("GtkSocket - sending synthetic configure: %d %d",
537                                    allocation->width, allocation->height));
538             }
539           
540           gdk_display_sync (gtk_widget_get_display (widget));
541           gdk_error_trap_pop ();
542         }
543     }
544 }
545
546 static gboolean
547 activate_key (GtkAccelGroup  *accel_group,
548               GObject        *acceleratable,
549               guint           accel_key,
550               GdkModifierType accel_mods,
551               GrabbedKey     *grabbed_key)
552 {
553   GdkEvent *gdk_event = gtk_get_current_event ();
554   
555   GtkSocket *socket = g_object_get_data (G_OBJECT (accel_group), "gtk-socket");
556   gboolean retval = FALSE;
557
558   if (gdk_event && gdk_event->type == GDK_KEY_PRESS && socket->plug_window)
559     {
560       _gtk_socket_windowing_send_key_event (socket, gdk_event, FALSE);
561       retval = TRUE;
562     }
563
564   if (gdk_event)
565     gdk_event_free (gdk_event);
566
567   return retval;
568 }
569
570 static gboolean
571 find_accel_key (GtkAccelKey *key,
572                 GClosure    *closure,
573                 gpointer     data)
574 {
575   GrabbedKey *grabbed_key = data;
576   
577   return (key->accel_key == grabbed_key->accel_key &&
578           key->accel_mods == grabbed_key->accel_mods);
579 }
580
581 /**
582  * _gtk_socket_add_grabbed_key:
583  *
584  * @socket: a #GtkSocket
585  * @keyval: a key
586  * @modifiers: modifiers for the key
587  *
588  * Called from the GtkSocket platform-specific backend when the
589  * corresponding plug has told the socket to grab a key.
590  */
591 void
592 _gtk_socket_add_grabbed_key (GtkSocket       *socket,
593                              guint            keyval,
594                              GdkModifierType  modifiers)
595 {
596   GClosure *closure;
597   GrabbedKey *grabbed_key;
598
599   grabbed_key = g_new (GrabbedKey, 1);
600   
601   grabbed_key->accel_key = keyval;
602   grabbed_key->accel_mods = modifiers;
603
604   if (gtk_accel_group_find (socket->accel_group,
605                             find_accel_key,
606                             &grabbed_key))
607     {
608       g_warning ("GtkSocket: request to add already present grabbed key %u,%#x\n",
609                  keyval, modifiers);
610       g_free (grabbed_key);
611       return;
612     }
613
614   closure = g_cclosure_new (G_CALLBACK (activate_key), grabbed_key, (GClosureNotify)g_free);
615
616   gtk_accel_group_connect (socket->accel_group, keyval, modifiers, GTK_ACCEL_LOCKED,
617                            closure);
618 }
619
620 /**
621  * _gtk_socket_remove_grabbed_key:
622  *
623  * @socket: a #GtkSocket
624  * @keyval: a key
625  * @modifiers: modifiers for the key
626  *
627  * Called from the GtkSocket backend when the corresponding plug has
628  * told the socket to remove a key grab.
629  */
630 void
631 _gtk_socket_remove_grabbed_key (GtkSocket      *socket,
632                                 guint           keyval,
633                                 GdkModifierType modifiers)
634 {
635   if (!gtk_accel_group_disconnect_key (socket->accel_group, keyval, modifiers))
636     g_warning ("GtkSocket: request to remove non-present grabbed key %u,%#x\n",
637                keyval, modifiers);
638 }
639
640 static void
641 socket_update_focus_in (GtkSocket *socket)
642 {
643   gboolean focus_in = FALSE;
644
645   if (socket->plug_window)
646     {
647       GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (socket));
648
649       if (gtk_widget_is_toplevel (toplevel) &&
650           GTK_WINDOW (toplevel)->has_toplevel_focus &&
651           gtk_widget_is_focus (GTK_WIDGET (socket)))
652         focus_in = TRUE;
653     }
654
655   if (focus_in != socket->focus_in)
656     {
657       socket->focus_in = focus_in;
658
659       _gtk_socket_windowing_focus_change (socket, focus_in);
660     }
661 }
662
663 static void
664 socket_update_active (GtkSocket *socket)
665 {
666   gboolean active = FALSE;
667
668   if (socket->plug_window)
669     {
670       GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (socket));
671
672       if (gtk_widget_is_toplevel (toplevel) &&
673           GTK_WINDOW (toplevel)->is_active)
674         active = TRUE;
675     }
676
677   if (active != socket->active)
678     {
679       socket->active = active;
680
681       _gtk_socket_windowing_update_active (socket, active);
682     }
683 }
684
685 static void
686 gtk_socket_hierarchy_changed (GtkWidget *widget,
687                               GtkWidget *old_toplevel)
688 {
689   GtkSocket *socket = GTK_SOCKET (widget);
690   GtkWidget *toplevel = gtk_widget_get_toplevel (widget);
691
692   if (toplevel && !GTK_IS_WINDOW (toplevel))
693     toplevel = NULL;
694
695   if (toplevel != socket->toplevel)
696     {
697       if (socket->toplevel)
698         {
699           gtk_window_remove_accel_group (GTK_WINDOW (socket->toplevel), socket->accel_group);
700           g_signal_handlers_disconnect_by_func (socket->toplevel,
701                                                 socket_update_focus_in,
702                                                 socket);
703           g_signal_handlers_disconnect_by_func (socket->toplevel,
704                                                 socket_update_active,
705                                                 socket);
706         }
707
708       socket->toplevel = toplevel;
709
710       if (toplevel)
711         {
712           gtk_window_add_accel_group (GTK_WINDOW (socket->toplevel), socket->accel_group);
713           g_signal_connect_swapped (socket->toplevel, "notify::has-toplevel-focus",
714                                     G_CALLBACK (socket_update_focus_in), socket);
715           g_signal_connect_swapped (socket->toplevel, "notify::is-active",
716                                     G_CALLBACK (socket_update_active), socket);
717         }
718
719       socket_update_focus_in (socket);
720       socket_update_active (socket);
721     }
722 }
723
724 static void
725 gtk_socket_grab_notify (GtkWidget *widget,
726                         gboolean   was_grabbed)
727 {
728   GtkSocket *socket = GTK_SOCKET (widget);
729
730   if (!socket->same_app)
731     _gtk_socket_windowing_update_modality (socket, !was_grabbed);
732 }
733
734 static gboolean
735 gtk_socket_key_event (GtkWidget   *widget,
736                       GdkEventKey *event)
737 {
738   GtkSocket *socket = GTK_SOCKET (widget);
739   
740   if (gtk_widget_has_focus (widget) && socket->plug_window && !socket->plug_widget)
741     {
742       _gtk_socket_windowing_send_key_event (socket, (GdkEvent *) event, FALSE);
743
744       return TRUE;
745     }
746   else
747     return FALSE;
748 }
749
750 static void
751 gtk_socket_notify (GObject    *object,
752                    GParamSpec *pspec)
753 {
754   if (!strcmp (pspec->name, "is-focus"))
755     return;
756   socket_update_focus_in (GTK_SOCKET (object));
757 }
758
759 /**
760  * _gtk_socket_claim_focus:
761  *
762  * @socket: a #GtkSocket
763  * @send_event: huh?
764  *
765  * Claims focus for the socket. XXX send_event?
766  */
767 void
768 _gtk_socket_claim_focus (GtkSocket *socket,
769                          gboolean   send_event)
770 {
771   GtkWidget *widget = GTK_WIDGET (socket);
772
773   if (!send_event)
774     socket->focus_in = TRUE;    /* Otherwise, our notify handler will send FOCUS_IN  */
775       
776   /* Oh, the trickery... */
777   
778   gtk_widget_set_can_focus (widget, TRUE);
779   gtk_widget_grab_focus (widget);
780   gtk_widget_set_can_focus (widget, FALSE);
781 }
782
783 static gboolean
784 gtk_socket_focus (GtkWidget       *widget,
785                   GtkDirectionType direction)
786 {
787   GtkSocket *socket = GTK_SOCKET (widget);
788
789   if (socket->plug_widget)
790     return gtk_widget_child_focus (socket->plug_widget, direction);
791
792   if (!gtk_widget_is_focus (widget))
793     {
794       _gtk_socket_windowing_focus (socket, direction);
795       _gtk_socket_claim_focus (socket, FALSE);
796  
797       return TRUE;
798     }
799   else
800     return FALSE;
801 }
802
803 static void
804 gtk_socket_remove (GtkContainer *container,
805                    GtkWidget    *child)
806 {
807   GtkSocket *socket = GTK_SOCKET (container);
808
809   g_return_if_fail (child == socket->plug_widget);
810
811   _gtk_plug_remove_from_socket (GTK_PLUG (socket->plug_widget), socket);
812 }
813
814 static void
815 gtk_socket_forall (GtkContainer *container,
816                    gboolean      include_internals,
817                    GtkCallback   callback,
818                    gpointer      callback_data)
819 {
820   GtkSocket *socket = GTK_SOCKET (container);
821
822   if (socket->plug_widget)
823     (* callback) (socket->plug_widget, callback_data);
824 }
825
826 /**
827  * _gtk_socket_add_window:
828  *
829  * @socket: a #GtkSocket
830  * @xid: the native identifier for a window
831  * @need_reparent: whether the socket's plug's window needs to be
832  *                 reparented to the socket
833  *
834  * Adds a window to a GtkSocket.
835  */
836 void
837 _gtk_socket_add_window (GtkSocket       *socket,
838                         GdkNativeWindow  xid,
839                         gboolean         need_reparent)
840 {
841   GtkWidget *widget = GTK_WIDGET (socket);
842   GdkDisplay *display = gtk_widget_get_display (widget);
843   gpointer user_data = NULL;
844   
845   socket->plug_window = gdk_window_lookup_for_display (display, xid);
846
847   if (socket->plug_window)
848     {
849       g_object_ref (socket->plug_window);
850       gdk_window_get_user_data (socket->plug_window, &user_data);
851     }
852
853   if (user_data)                /* A widget's window in this process */
854     {
855       GtkWidget *child_widget = user_data;
856
857       if (!GTK_IS_PLUG (child_widget))
858         {
859           g_warning (G_STRLOC ": Can't add non-GtkPlug to GtkSocket");
860           socket->plug_window = NULL;
861           gdk_error_trap_pop ();
862           
863           return;
864         }
865
866       _gtk_plug_add_to_socket (GTK_PLUG (child_widget), socket);
867     }
868   else                          /* A foreign window */
869     {
870       GtkWidget *toplevel;
871       GdkDragProtocol protocol;
872
873       gdk_error_trap_push ();
874
875       if (!socket->plug_window)
876         {  
877           socket->plug_window = gdk_window_foreign_new_for_display (display, xid);
878           if (!socket->plug_window) /* was deleted before we could get it */
879             {
880               gdk_error_trap_pop ();
881               return;
882             }
883         }
884         
885       _gtk_socket_windowing_select_plug_window_input (socket);
886
887       if (gdk_error_trap_pop ())
888         {
889           g_object_unref (socket->plug_window);
890           socket->plug_window = NULL;
891           return;
892         }
893       
894       /* OK, we now will reliably get destroy notification on socket->plug_window */
895
896       gdk_error_trap_push ();
897
898       if (need_reparent)
899         {
900           gdk_window_hide (socket->plug_window); /* Shouldn't actually be necessary for XEMBED, but just in case */
901           gdk_window_reparent (socket->plug_window, widget->window, 0, 0);
902         }
903
904       socket->have_size = FALSE;
905
906       _gtk_socket_windowing_embed_get_info (socket);
907
908       socket->need_map = socket->is_mapped;
909
910       if (gdk_drag_get_protocol_for_display (display, xid, &protocol))
911         gtk_drag_dest_set_proxy (GTK_WIDGET (socket), socket->plug_window, 
912                                  protocol, TRUE);
913
914       gdk_display_sync (display);
915       gdk_error_trap_pop ();
916
917       gdk_window_add_filter (socket->plug_window,
918                              _gtk_socket_windowing_filter_func,
919                              socket);
920
921       /* Add a pointer to the socket on our toplevel window */
922
923       toplevel = gtk_widget_get_toplevel (GTK_WIDGET (socket));
924       if (GTK_IS_WINDOW (toplevel))
925         gtk_window_add_embedded_xid (GTK_WINDOW (toplevel), xid);
926
927       _gtk_socket_windowing_embed_notify (socket);
928
929       socket_update_active (socket);
930       socket_update_focus_in (socket);
931
932       gtk_widget_queue_resize (GTK_WIDGET (socket));
933     }
934
935   if (socket->plug_window)
936     g_signal_emit (socket, socket_signals[PLUG_ADDED], 0);
937 }
938
939 /**
940  * _gtk_socket_handle_map_request:
941  *
942  * @socket: a #GtkSocket
943  *
944  * Called from the GtkSocket backend when the plug has been mapped.
945  */
946 void
947 _gtk_socket_handle_map_request (GtkSocket *socket)
948 {
949   if (!socket->is_mapped)
950     {
951       socket->is_mapped = TRUE;
952       socket->need_map = TRUE;
953
954       gtk_widget_queue_resize (GTK_WIDGET (socket));
955     }
956 }
957
958 /**
959  * _gtk_socket_unmap_notify:
960  *
961  * @socket: a #GtkSocket
962  *
963  * Called from the GtkSocket backend when the plug has been unmapped ???
964  */
965 void
966 _gtk_socket_unmap_notify (GtkSocket *socket)
967 {
968   if (socket->is_mapped)
969     {
970       socket->is_mapped = FALSE;
971       gtk_widget_queue_resize (GTK_WIDGET (socket));
972     }
973 }
974
975 /**
976  * _gtk_socket_advance_toplevel_focus:
977  *
978  * @socket: a #GtkSocket
979  * @direction: a direction
980  *
981  * Called from the GtkSocket backend when the corresponding plug
982  * has told the socket to move the focus.
983  */
984 void
985 _gtk_socket_advance_toplevel_focus (GtkSocket        *socket,
986                                     GtkDirectionType  direction)
987 {
988   GtkBin *bin;
989   GtkWindow *window;
990   GtkContainer *container;
991   GtkWidget *toplevel;
992   GtkWidget *old_focus_child;
993   GtkWidget *parent;
994
995   toplevel = gtk_widget_get_toplevel (GTK_WIDGET (socket));
996   if (!toplevel)
997     return;
998
999   if (!gtk_widget_is_toplevel (toplevel) || GTK_IS_PLUG (toplevel))
1000     {
1001       gtk_widget_child_focus (toplevel,direction);
1002       return;
1003     }
1004
1005   container = GTK_CONTAINER (toplevel);
1006   window = GTK_WINDOW (toplevel);
1007   bin = GTK_BIN (toplevel);
1008
1009   /* This is a copy of gtk_window_focus(), modified so that we
1010    * can detect wrap-around.
1011    */
1012   old_focus_child = container->focus_child;
1013   
1014   if (old_focus_child)
1015     {
1016       if (gtk_widget_child_focus (old_focus_child, direction))
1017         return;
1018
1019       /* We are allowed exactly one wrap-around per sequence of focus
1020        * events
1021        */
1022       if (_gtk_socket_windowing_embed_get_focus_wrapped ())
1023         return;
1024       else
1025         _gtk_socket_windowing_embed_set_focus_wrapped ();
1026     }
1027
1028   if (window->focus_widget)
1029     {
1030       /* Wrapped off the end, clear the focus setting for the toplevel */
1031       parent = window->focus_widget->parent;
1032       while (parent)
1033         {
1034           gtk_container_set_focus_child (GTK_CONTAINER (parent), NULL);
1035           parent = GTK_WIDGET (parent)->parent;
1036         }
1037       
1038       gtk_window_set_focus (GTK_WINDOW (container), NULL);
1039     }
1040
1041   /* Now try to focus the first widget in the window */
1042   if (bin->child)
1043     {
1044       if (gtk_widget_child_focus (bin->child, direction))
1045         return;
1046     }
1047 }