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