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