]> Pileus Git - ~andy/gtk/blob - gtk/gtksocket.c
Merge branch 'notebooks-without-mouse-scrolling'
[~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.colormap = gtk_widget_get_colormap (widget);
385   attributes.event_mask = GDK_FOCUS_CHANGE_MASK;
386
387   attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
388
389   window = gdk_window_new (gtk_widget_get_parent_window (widget),
390                            &attributes, attributes_mask);
391   gtk_widget_set_window (widget, window);
392   gdk_window_set_user_data (window, socket);
393
394   gtk_widget_style_attach (widget);
395   gtk_style_set_background (gtk_widget_get_style (widget),
396                             window, GTK_STATE_NORMAL);
397
398   _gtk_socket_windowing_realize_window (socket);
399
400   gdk_window_add_filter (window,
401                          _gtk_socket_windowing_filter_func,
402                          widget);
403
404   /* We sync here so that we make sure that if the XID for
405    * our window is passed to another application, SubstructureRedirectMask
406    * will be set by the time the other app creates its window.
407    */
408   gdk_display_sync (gtk_widget_get_display (widget));
409 }
410
411 /**
412  * _gtk_socket_end_embedding:
413  *
414  * @socket: a #GtkSocket
415  *
416  * Called to end the embedding of a plug in the socket.
417  */
418 void
419 _gtk_socket_end_embedding (GtkSocket *socket)
420 {
421   GtkSocketPrivate *private = _gtk_socket_get_private (socket);
422   GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (socket));
423   
424   if (GTK_IS_WINDOW (toplevel))
425     _gtk_socket_windowing_end_embedding_toplevel (socket);
426
427   g_object_unref (socket->plug_window);
428   socket->plug_window = NULL;
429   socket->current_width = 0;
430   socket->current_height = 0;
431   private->resize_count = 0;
432
433   gtk_accel_group_disconnect (socket->accel_group, NULL);
434 }
435
436 static void
437 gtk_socket_unrealize (GtkWidget *widget)
438 {
439   GtkSocket *socket = GTK_SOCKET (widget);
440
441   gtk_widget_set_realized (widget, FALSE);
442
443   if (socket->plug_widget)
444     {
445       _gtk_plug_remove_from_socket (GTK_PLUG (socket->plug_widget), socket);
446     }
447   else if (socket->plug_window)
448     {
449       _gtk_socket_end_embedding (socket);
450     }
451
452   GTK_WIDGET_CLASS (gtk_socket_parent_class)->unrealize (widget);
453 }
454
455 static void
456 gtk_socket_size_request (GtkWidget      *widget,
457                          GtkRequisition *requisition)
458 {
459   GtkSocket *socket = GTK_SOCKET (widget);
460
461   if (socket->plug_widget)
462     {
463       gtk_size_request_get_size (GTK_SIZE_REQUEST (socket->plug_widget),
464                                  requisition, NULL);
465     }
466   else
467     {
468       if (socket->is_mapped && !socket->have_size && socket->plug_window)
469         _gtk_socket_windowing_size_request (socket);
470
471       if (socket->is_mapped && socket->have_size)
472         {
473           requisition->width = MAX (socket->request_width, 1);
474           requisition->height = MAX (socket->request_height, 1);
475         }
476       else
477         {
478           requisition->width = 1;
479           requisition->height = 1;
480         }
481     }
482 }
483
484 static void
485 gtk_socket_size_allocate (GtkWidget     *widget,
486                           GtkAllocation *allocation)
487 {
488   GtkSocket *socket = GTK_SOCKET (widget);
489
490   gtk_widget_set_allocation (widget, allocation);
491   if (gtk_widget_get_realized (widget))
492     {
493       gdk_window_move_resize (gtk_widget_get_window (widget),
494                               allocation->x, allocation->y,
495                               allocation->width, allocation->height);
496
497       if (socket->plug_widget)
498         {
499           GtkAllocation child_allocation;
500
501           child_allocation.x = 0;
502           child_allocation.y = 0;
503           child_allocation.width = allocation->width;
504           child_allocation.height = allocation->height;
505
506           gtk_widget_size_allocate (socket->plug_widget, &child_allocation);
507         }
508       else if (socket->plug_window)
509         {
510           GtkSocketPrivate *private = _gtk_socket_get_private (socket);
511           
512           gdk_error_trap_push ();
513           
514           if (allocation->width != socket->current_width ||
515               allocation->height != socket->current_height)
516             {
517               gdk_window_move_resize (socket->plug_window,
518                                       0, 0,
519                                       allocation->width, allocation->height);
520               if (private->resize_count)
521                 private->resize_count--;
522               
523               GTK_NOTE (PLUGSOCKET,
524                         g_message ("GtkSocket - allocated: %d %d",
525                                    allocation->width, allocation->height));
526               socket->current_width = allocation->width;
527               socket->current_height = allocation->height;
528             }
529
530           if (socket->need_map)
531             {
532               gdk_window_show (socket->plug_window);
533               socket->need_map = FALSE;
534             }
535
536           while (private->resize_count)
537             {
538               _gtk_socket_windowing_send_configure_event (socket);
539               private->resize_count--;
540               GTK_NOTE (PLUGSOCKET,
541                         g_message ("GtkSocket - sending synthetic configure: %d %d",
542                                    allocation->width, allocation->height));
543             }
544
545           gdk_error_trap_pop_ignored ();
546         }
547     }
548 }
549
550 static gboolean
551 activate_key (GtkAccelGroup  *accel_group,
552               GObject        *acceleratable,
553               guint           accel_key,
554               GdkModifierType accel_mods,
555               GrabbedKey     *grabbed_key)
556 {
557   GdkEvent *gdk_event = gtk_get_current_event ();
558   
559   GtkSocket *socket = g_object_get_data (G_OBJECT (accel_group), "gtk-socket");
560   gboolean retval = FALSE;
561
562   if (gdk_event && gdk_event->type == GDK_KEY_PRESS && socket->plug_window)
563     {
564       _gtk_socket_windowing_send_key_event (socket, gdk_event, FALSE);
565       retval = TRUE;
566     }
567
568   if (gdk_event)
569     gdk_event_free (gdk_event);
570
571   return retval;
572 }
573
574 static gboolean
575 find_accel_key (GtkAccelKey *key,
576                 GClosure    *closure,
577                 gpointer     data)
578 {
579   GrabbedKey *grabbed_key = data;
580   
581   return (key->accel_key == grabbed_key->accel_key &&
582           key->accel_mods == grabbed_key->accel_mods);
583 }
584
585 /**
586  * _gtk_socket_add_grabbed_key:
587  *
588  * @socket: a #GtkSocket
589  * @keyval: a key
590  * @modifiers: modifiers for the key
591  *
592  * Called from the GtkSocket platform-specific backend when the
593  * corresponding plug has told the socket to grab a key.
594  */
595 void
596 _gtk_socket_add_grabbed_key (GtkSocket       *socket,
597                              guint            keyval,
598                              GdkModifierType  modifiers)
599 {
600   GClosure *closure;
601   GrabbedKey *grabbed_key;
602
603   grabbed_key = g_new (GrabbedKey, 1);
604   
605   grabbed_key->accel_key = keyval;
606   grabbed_key->accel_mods = modifiers;
607
608   if (gtk_accel_group_find (socket->accel_group,
609                             find_accel_key,
610                             &grabbed_key))
611     {
612       g_warning ("GtkSocket: request to add already present grabbed key %u,%#x\n",
613                  keyval, modifiers);
614       g_free (grabbed_key);
615       return;
616     }
617
618   closure = g_cclosure_new (G_CALLBACK (activate_key), grabbed_key, (GClosureNotify)g_free);
619
620   gtk_accel_group_connect (socket->accel_group, keyval, modifiers, GTK_ACCEL_LOCKED,
621                            closure);
622 }
623
624 /**
625  * _gtk_socket_remove_grabbed_key:
626  *
627  * @socket: a #GtkSocket
628  * @keyval: a key
629  * @modifiers: modifiers for the key
630  *
631  * Called from the GtkSocket backend when the corresponding plug has
632  * told the socket to remove a key grab.
633  */
634 void
635 _gtk_socket_remove_grabbed_key (GtkSocket      *socket,
636                                 guint           keyval,
637                                 GdkModifierType modifiers)
638 {
639   if (!gtk_accel_group_disconnect_key (socket->accel_group, keyval, modifiers))
640     g_warning ("GtkSocket: request to remove non-present grabbed key %u,%#x\n",
641                keyval, modifiers);
642 }
643
644 static void
645 socket_update_focus_in (GtkSocket *socket)
646 {
647   gboolean focus_in = FALSE;
648
649   if (socket->plug_window)
650     {
651       GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (socket));
652
653       if (gtk_widget_is_toplevel (toplevel) &&
654           gtk_window_has_toplevel_focus (GTK_WINDOW (toplevel)) &&
655           gtk_widget_is_focus (GTK_WIDGET (socket)))
656         focus_in = TRUE;
657     }
658
659   if (focus_in != socket->focus_in)
660     {
661       socket->focus_in = focus_in;
662
663       _gtk_socket_windowing_focus_change (socket, focus_in);
664     }
665 }
666
667 static void
668 socket_update_active (GtkSocket *socket)
669 {
670   gboolean active = FALSE;
671
672   if (socket->plug_window)
673     {
674       GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (socket));
675
676       if (gtk_widget_is_toplevel (toplevel) &&
677           gtk_window_is_active  (GTK_WINDOW (toplevel)))
678         active = TRUE;
679     }
680
681   if (active != socket->active)
682     {
683       socket->active = active;
684
685       _gtk_socket_windowing_update_active (socket, active);
686     }
687 }
688
689 static void
690 gtk_socket_hierarchy_changed (GtkWidget *widget,
691                               GtkWidget *old_toplevel)
692 {
693   GtkSocket *socket = GTK_SOCKET (widget);
694   GtkWidget *toplevel = gtk_widget_get_toplevel (widget);
695
696   if (toplevel && !GTK_IS_WINDOW (toplevel))
697     toplevel = NULL;
698
699   if (toplevel != socket->toplevel)
700     {
701       if (socket->toplevel)
702         {
703           gtk_window_remove_accel_group (GTK_WINDOW (socket->toplevel), socket->accel_group);
704           g_signal_handlers_disconnect_by_func (socket->toplevel,
705                                                 socket_update_focus_in,
706                                                 socket);
707           g_signal_handlers_disconnect_by_func (socket->toplevel,
708                                                 socket_update_active,
709                                                 socket);
710         }
711
712       socket->toplevel = toplevel;
713
714       if (toplevel)
715         {
716           gtk_window_add_accel_group (GTK_WINDOW (socket->toplevel), socket->accel_group);
717           g_signal_connect_swapped (socket->toplevel, "notify::has-toplevel-focus",
718                                     G_CALLBACK (socket_update_focus_in), socket);
719           g_signal_connect_swapped (socket->toplevel, "notify::is-active",
720                                     G_CALLBACK (socket_update_active), socket);
721         }
722
723       socket_update_focus_in (socket);
724       socket_update_active (socket);
725     }
726 }
727
728 static void
729 gtk_socket_grab_notify (GtkWidget *widget,
730                         gboolean   was_grabbed)
731 {
732   GtkSocket *socket = GTK_SOCKET (widget);
733
734   if (!socket->same_app)
735     _gtk_socket_windowing_update_modality (socket, !was_grabbed);
736 }
737
738 static gboolean
739 gtk_socket_key_event (GtkWidget   *widget,
740                       GdkEventKey *event)
741 {
742   GtkSocket *socket = GTK_SOCKET (widget);
743   
744   if (gtk_widget_has_focus (widget) && socket->plug_window && !socket->plug_widget)
745     {
746       _gtk_socket_windowing_send_key_event (socket, (GdkEvent *) event, FALSE);
747
748       return TRUE;
749     }
750   else
751     return FALSE;
752 }
753
754 static void
755 gtk_socket_notify (GObject    *object,
756                    GParamSpec *pspec)
757 {
758   if (!strcmp (pspec->name, "is-focus"))
759     return;
760   socket_update_focus_in (GTK_SOCKET (object));
761 }
762
763 /**
764  * _gtk_socket_claim_focus:
765  *
766  * @socket: a #GtkSocket
767  * @send_event: huh?
768  *
769  * Claims focus for the socket. XXX send_event?
770  */
771 void
772 _gtk_socket_claim_focus (GtkSocket *socket,
773                          gboolean   send_event)
774 {
775   GtkWidget *widget = GTK_WIDGET (socket);
776
777   if (!send_event)
778     socket->focus_in = TRUE;    /* Otherwise, our notify handler will send FOCUS_IN  */
779       
780   /* Oh, the trickery... */
781   
782   gtk_widget_set_can_focus (widget, TRUE);
783   gtk_widget_grab_focus (widget);
784   gtk_widget_set_can_focus (widget, FALSE);
785 }
786
787 static gboolean
788 gtk_socket_focus (GtkWidget       *widget,
789                   GtkDirectionType direction)
790 {
791   GtkSocket *socket = GTK_SOCKET (widget);
792
793   if (socket->plug_widget)
794     return gtk_widget_child_focus (socket->plug_widget, direction);
795
796   if (!gtk_widget_is_focus (widget))
797     {
798       _gtk_socket_windowing_focus (socket, direction);
799       _gtk_socket_claim_focus (socket, FALSE);
800  
801       return TRUE;
802     }
803   else
804     return FALSE;
805 }
806
807 static void
808 gtk_socket_remove (GtkContainer *container,
809                    GtkWidget    *child)
810 {
811   GtkSocket *socket = GTK_SOCKET (container);
812
813   g_return_if_fail (child == socket->plug_widget);
814
815   _gtk_plug_remove_from_socket (GTK_PLUG (socket->plug_widget), socket);
816 }
817
818 static void
819 gtk_socket_forall (GtkContainer *container,
820                    gboolean      include_internals,
821                    GtkCallback   callback,
822                    gpointer      callback_data)
823 {
824   GtkSocket *socket = GTK_SOCKET (container);
825
826   if (socket->plug_widget)
827     (* callback) (socket->plug_widget, callback_data);
828 }
829
830 /**
831  * _gtk_socket_add_window:
832  *
833  * @socket: a #GtkSocket
834  * @xid: the native identifier for a window
835  * @need_reparent: whether the socket's plug's window needs to be
836  *                 reparented to the socket
837  *
838  * Adds a window to a GtkSocket.
839  */
840 void
841 _gtk_socket_add_window (GtkSocket       *socket,
842                         GdkNativeWindow  xid,
843                         gboolean         need_reparent)
844 {
845   GtkWidget *widget = GTK_WIDGET (socket);
846   GdkDisplay *display = gtk_widget_get_display (widget);
847   gpointer user_data = NULL;
848   
849   socket->plug_window = gdk_window_lookup_for_display (display, xid);
850
851   if (socket->plug_window)
852     {
853       g_object_ref (socket->plug_window);
854       gdk_window_get_user_data (socket->plug_window, &user_data);
855     }
856
857   if (user_data)                /* A widget's window in this process */
858     {
859       GtkWidget *child_widget = user_data;
860
861       if (!GTK_IS_PLUG (child_widget))
862         {
863           g_warning (G_STRLOC ": Can't add non-GtkPlug to GtkSocket");
864           socket->plug_window = NULL;
865           gdk_error_trap_pop_ignored ();
866           
867           return;
868         }
869
870       _gtk_plug_add_to_socket (GTK_PLUG (child_widget), socket);
871     }
872   else                          /* A foreign window */
873     {
874       GtkWidget *toplevel;
875       GdkDragProtocol protocol;
876
877       gdk_error_trap_push ();
878
879       if (!socket->plug_window)
880         {  
881           socket->plug_window = gdk_window_foreign_new_for_display (display, xid);
882           if (!socket->plug_window) /* was deleted before we could get it */
883             {
884               gdk_error_trap_pop_ignored ();
885               return;
886             }
887         }
888         
889       _gtk_socket_windowing_select_plug_window_input (socket);
890
891       if (gdk_error_trap_pop ())
892         {
893           g_object_unref (socket->plug_window);
894           socket->plug_window = NULL;
895           return;
896         }
897       
898       /* OK, we now will reliably get destroy notification on socket->plug_window */
899
900       gdk_error_trap_push ();
901
902       if (need_reparent)
903         {
904           gdk_window_hide (socket->plug_window); /* Shouldn't actually be necessary for XEMBED, but just in case */
905           gdk_window_reparent (socket->plug_window,
906                                gtk_widget_get_window (widget),
907                                0, 0);
908         }
909
910       socket->have_size = FALSE;
911
912       _gtk_socket_windowing_embed_get_info (socket);
913
914       socket->need_map = socket->is_mapped;
915
916       if (gdk_drag_get_protocol_for_display (display, xid, &protocol))
917         gtk_drag_dest_set_proxy (GTK_WIDGET (socket), socket->plug_window, 
918                                  protocol, TRUE);
919
920       gdk_error_trap_pop_ignored ();
921
922       gdk_window_add_filter (socket->plug_window,
923                              _gtk_socket_windowing_filter_func,
924                              socket);
925
926       /* Add a pointer to the socket on our toplevel window */
927
928       toplevel = gtk_widget_get_toplevel (GTK_WIDGET (socket));
929       if (GTK_IS_WINDOW (toplevel))
930         gtk_window_add_embedded_xid (GTK_WINDOW (toplevel), xid);
931
932       _gtk_socket_windowing_embed_notify (socket);
933
934       socket_update_active (socket);
935       socket_update_focus_in (socket);
936
937       gtk_widget_queue_resize (GTK_WIDGET (socket));
938     }
939
940   if (socket->plug_window)
941     g_signal_emit (socket, socket_signals[PLUG_ADDED], 0);
942 }
943
944 /**
945  * _gtk_socket_handle_map_request:
946  *
947  * @socket: a #GtkSocket
948  *
949  * Called from the GtkSocket backend when the plug has been mapped.
950  */
951 void
952 _gtk_socket_handle_map_request (GtkSocket *socket)
953 {
954   if (!socket->is_mapped)
955     {
956       socket->is_mapped = TRUE;
957       socket->need_map = TRUE;
958
959       gtk_widget_queue_resize (GTK_WIDGET (socket));
960     }
961 }
962
963 /**
964  * _gtk_socket_unmap_notify:
965  *
966  * @socket: a #GtkSocket
967  *
968  * Called from the GtkSocket backend when the plug has been unmapped ???
969  */
970 void
971 _gtk_socket_unmap_notify (GtkSocket *socket)
972 {
973   if (socket->is_mapped)
974     {
975       socket->is_mapped = FALSE;
976       gtk_widget_queue_resize (GTK_WIDGET (socket));
977     }
978 }
979
980 /**
981  * _gtk_socket_advance_toplevel_focus:
982  *
983  * @socket: a #GtkSocket
984  * @direction: a direction
985  *
986  * Called from the GtkSocket backend when the corresponding plug
987  * has told the socket to move the focus.
988  */
989 void
990 _gtk_socket_advance_toplevel_focus (GtkSocket        *socket,
991                                     GtkDirectionType  direction)
992 {
993   GtkBin *bin;
994   GtkWindow *window;
995   GtkContainer *container;
996   GtkWidget *child;
997   GtkWidget *focus_widget;
998   GtkWidget *toplevel;
999   GtkWidget *old_focus_child;
1000   GtkWidget *parent;
1001
1002   toplevel = gtk_widget_get_toplevel (GTK_WIDGET (socket));
1003   if (!toplevel)
1004     return;
1005
1006   if (!gtk_widget_is_toplevel (toplevel) || GTK_IS_PLUG (toplevel))
1007     {
1008       gtk_widget_child_focus (toplevel,direction);
1009       return;
1010     }
1011
1012   container = GTK_CONTAINER (toplevel);
1013   window = GTK_WINDOW (toplevel);
1014   bin = GTK_BIN (toplevel);
1015
1016   /* This is a copy of gtk_window_focus(), modified so that we
1017    * can detect wrap-around.
1018    */
1019   old_focus_child = gtk_container_get_focus_child (container);
1020   
1021   if (old_focus_child)
1022     {
1023       if (gtk_widget_child_focus (old_focus_child, direction))
1024         return;
1025
1026       /* We are allowed exactly one wrap-around per sequence of focus
1027        * events
1028        */
1029       if (_gtk_socket_windowing_embed_get_focus_wrapped ())
1030         return;
1031       else
1032         _gtk_socket_windowing_embed_set_focus_wrapped ();
1033     }
1034
1035   focus_widget = gtk_window_get_focus (window);
1036   if (window)
1037     {
1038       /* Wrapped off the end, clear the focus setting for the toplevel */
1039       parent = gtk_widget_get_parent (focus_widget);
1040       while (parent)
1041         {
1042           gtk_container_set_focus_child (GTK_CONTAINER (parent), NULL);
1043           parent = gtk_widget_get_parent (parent);
1044         }
1045       
1046       gtk_window_set_focus (GTK_WINDOW (container), NULL);
1047     }
1048
1049   /* Now try to focus the first widget in the window */
1050   child = gtk_bin_get_child (bin);
1051   if (child)
1052     {
1053       if (gtk_widget_child_focus (child, direction))
1054         return;
1055     }
1056 }