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