]> Pileus Git - ~andy/gtk/blob - gtk/gtkplug.c
Change FSF Address
[~andy/gtk] / gtk / gtkplug.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, see <http://www.gnu.org/licenses/>.Free
16  */
17
18 /* By Owen Taylor <otaylor@gtk.org>              98/4/4 */
19
20 /*
21  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
22  * file for a list of people on the GTK+ Team.  See the ChangeLog
23  * files for a list of changes.  These files are distributed with
24  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
25  */
26
27 #include "config.h"
28
29 #include "gtkdebug.h"
30 #include "gtkmain.h"
31 #include "gtkmarshalers.h"
32 #include "gtkplug.h"
33 #include "gtkintl.h"
34 #include "gtkprivate.h"
35 #include "gtksocketprivate.h"
36 #include "gtkwidgetprivate.h"
37 #include "gtkwindowprivate.h"
38 #include "gtkxembed.h"
39
40 #include <gdk/gdkx.h>
41
42 /**
43  * SECTION:gtkplug
44  * @Short_description: Toplevel for embedding into other processes
45  * @Title: GtkPlug
46  * @include: gtk/gtkx.h
47  * @See_also: #GtkSocket
48  *
49  * Together with #GtkSocket, #GtkPlug provides the ability to embed
50  * widgets from one process into another process in a fashion that is
51  * transparent to the user. One process creates a #GtkSocket widget
52  * and passes the ID of that widget's window to the other process,
53  * which then creates a #GtkPlug with that window ID. Any widgets
54  * contained in the #GtkPlug then will appear inside the first
55  * application's window.
56  *
57  * The communication between a #GtkSocket and a #GtkPlug follows the
58  * <ulink url="http://www.freedesktop.org/Standards/xembed-spec">XEmbed</ulink>
59  * protocol. This protocol has also been implemented in other toolkits,
60  * e.g. <application>Qt</application>, allowing the same level of
61  * integration when embedding a <application>Qt</application> widget
62  * in GTK+ or vice versa.
63  *
64  * <note>
65  * The #GtkPlug and #GtkSocket widgets are only available when GTK+
66  * is compiled for the X11 platform and %GDK_WINDOWING_X11 is defined.
67  * They can only be used on a #GdkX11Display. To use #GtkPlug and
68  * #GtkSocket, you need to include the <filename>gtk/gtkx.h</filename>
69  * header.
70  * </note>
71  */
72
73 struct _GtkPlugPrivate
74 {
75   GtkWidget      *modality_window;
76   GtkWindowGroup *modality_group;
77
78   GdkWindow      *socket_window;
79
80   GHashTable     *grabbed_keys;
81
82   guint  same_app : 1;
83 };
84
85 static void            gtk_plug_get_property          (GObject     *object,
86                                                        guint        prop_id,
87                                                        GValue      *value,
88                                                        GParamSpec  *pspec);
89 static void            gtk_plug_finalize              (GObject          *object);
90 static void            gtk_plug_realize               (GtkWidget        *widget);
91 static void            gtk_plug_unrealize             (GtkWidget        *widget);
92 static void            gtk_plug_show                  (GtkWidget        *widget);
93 static void            gtk_plug_hide                  (GtkWidget        *widget);
94 static void            gtk_plug_map                   (GtkWidget        *widget);
95 static void            gtk_plug_unmap                 (GtkWidget        *widget);
96 static void            gtk_plug_size_allocate         (GtkWidget        *widget,
97                                                        GtkAllocation    *allocation);
98 static gboolean        gtk_plug_key_press_event       (GtkWidget        *widget,
99                                                        GdkEventKey      *event);
100 static gboolean        gtk_plug_focus_event           (GtkWidget        *widget,
101                                                        GdkEventFocus    *event);
102 static void            gtk_plug_set_focus             (GtkWindow        *window,
103                                                        GtkWidget        *focus);
104 static gboolean        gtk_plug_focus                 (GtkWidget        *widget,
105                                                        GtkDirectionType  direction);
106 static void            gtk_plug_check_resize          (GtkContainer     *container);
107 static void            gtk_plug_keys_changed          (GtkWindow        *window);
108
109 static void            xembed_set_info                (GdkWindow        *window,
110                                                        unsigned long     flags);
111
112 static GtkBinClass *bin_class = NULL;
113
114 typedef struct
115 {
116   guint                  accelerator_key;
117   GdkModifierType        accelerator_mods;
118 } GrabbedKey;
119
120 enum {
121   PROP_0,
122   PROP_EMBEDDED,
123   PROP_SOCKET_WINDOW
124 };
125
126 enum {
127   EMBEDDED,
128   LAST_SIGNAL
129 }; 
130
131 static guint plug_signals[LAST_SIGNAL] = { 0 };
132
133 G_DEFINE_TYPE (GtkPlug, gtk_plug, GTK_TYPE_WINDOW)
134
135 static void
136 gtk_plug_get_property (GObject    *object,
137                        guint       prop_id,
138                        GValue     *value,
139                        GParamSpec *pspec)
140 {
141   GtkPlug *plug = GTK_PLUG (object);
142   GtkPlugPrivate *priv = plug->priv;
143
144   switch (prop_id)
145     {
146     case PROP_EMBEDDED:
147       g_value_set_boolean (value, priv->socket_window != NULL);
148       break;
149     case PROP_SOCKET_WINDOW:
150       g_value_set_object (value, priv->socket_window);
151       break;
152     default:
153       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
154       break;
155     }
156 }
157
158 static void
159 gtk_plug_class_init (GtkPlugClass *class)
160 {
161   GObjectClass *gobject_class = (GObjectClass *)class;
162   GtkWidgetClass *widget_class = (GtkWidgetClass *)class;
163   GtkWindowClass *window_class = (GtkWindowClass *)class;
164   GtkContainerClass *container_class = (GtkContainerClass *)class;
165
166   bin_class = g_type_class_peek (GTK_TYPE_BIN);
167
168   gobject_class->get_property = gtk_plug_get_property;
169   gobject_class->finalize = gtk_plug_finalize;
170   
171   widget_class->realize = gtk_plug_realize;
172   widget_class->unrealize = gtk_plug_unrealize;
173   widget_class->key_press_event = gtk_plug_key_press_event;
174   widget_class->focus_in_event = gtk_plug_focus_event;
175   widget_class->focus_out_event = gtk_plug_focus_event;
176
177   widget_class->show = gtk_plug_show;
178   widget_class->hide = gtk_plug_hide;
179   widget_class->map = gtk_plug_map;
180   widget_class->unmap = gtk_plug_unmap;
181   widget_class->size_allocate = gtk_plug_size_allocate;
182
183   widget_class->focus = gtk_plug_focus;
184
185   gtk_widget_class_set_accessible_role (widget_class, ATK_ROLE_PANEL);
186
187   container_class->check_resize = gtk_plug_check_resize;
188
189   window_class->set_focus = gtk_plug_set_focus;
190   window_class->keys_changed = gtk_plug_keys_changed;
191
192   /**
193    * GtkPlug:embedded:
194    *
195    * %TRUE if the plug is embedded in a socket.
196    *
197    * Since: 2.12
198    */
199   g_object_class_install_property (gobject_class,
200                                    PROP_EMBEDDED,
201                                    g_param_spec_boolean ("embedded",
202                                                          P_("Embedded"),
203                                                          P_("Whether the plug is embedded"),
204                                                          FALSE,
205                                                          GTK_PARAM_READABLE));
206
207   /**
208    * GtkPlug:socket-window:
209    *
210    * The window of the socket the plug is embedded in.
211    *
212    * Since: 2.14
213    */
214   g_object_class_install_property (gobject_class,
215                                    PROP_SOCKET_WINDOW,
216                                    g_param_spec_object ("socket-window",
217                                                         P_("Socket Window"),
218                                                         P_("The window of the socket the plug is embedded in"),
219                                                         GDK_TYPE_WINDOW,
220                                                         GTK_PARAM_READABLE));
221
222   /**
223    * GtkPlug::embedded:
224    * @plug: the object on which the signal was emitted
225    *
226    * Gets emitted when the plug becomes embedded in a socket.
227    */ 
228   plug_signals[EMBEDDED] =
229     g_signal_new (I_("embedded"),
230                   G_OBJECT_CLASS_TYPE (class),
231                   G_SIGNAL_RUN_LAST,
232                   G_STRUCT_OFFSET (GtkPlugClass, embedded),
233                   NULL, NULL,
234                   _gtk_marshal_VOID__VOID,
235                   G_TYPE_NONE, 0);
236
237   g_type_class_add_private (class, sizeof (GtkPlugPrivate));
238 }
239
240 static void
241 gtk_plug_init (GtkPlug *plug)
242 {
243   plug->priv = G_TYPE_INSTANCE_GET_PRIVATE (plug,
244                                             GTK_TYPE_PLUG,
245                                             GtkPlugPrivate);
246 }
247
248 /**
249  * gtk_plug_handle_modality_on:
250  *
251  * @plug: a #GtkPlug
252  *
253  * Called from the GtkPlug backend when the corresponding socket has
254  * told the plug that it modality has toggled on.
255  */
256 static void
257 gtk_plug_handle_modality_on (GtkPlug *plug)
258 {
259   GtkPlugPrivate *priv = plug->priv;
260
261   if (!priv->modality_window)
262     {
263       priv->modality_window = gtk_window_new (GTK_WINDOW_POPUP);
264       gtk_window_set_screen (GTK_WINDOW (priv->modality_window),
265                              gtk_widget_get_screen (GTK_WIDGET (plug)));
266       gtk_widget_realize (priv->modality_window);
267       gtk_window_group_add_window (priv->modality_group, GTK_WINDOW (priv->modality_window));
268       gtk_grab_add (priv->modality_window);
269     }
270 }
271
272 /**
273  * gtk_plug_handle_modality_off:
274  *
275  * @plug: a #GtkPlug
276  *
277  * Called from the GtkPlug backend when the corresponding socket has
278  * told the plug that it modality has toggled off.
279  */
280 static void
281 gtk_plug_handle_modality_off (GtkPlug *plug)
282 {
283   GtkPlugPrivate *priv = plug->priv;
284
285   if (priv->modality_window)
286     {
287       gtk_widget_destroy (priv->modality_window);
288       priv->modality_window = NULL;
289     }
290 }
291
292 static void
293 gtk_plug_set_is_child (GtkPlug  *plug,
294                        gboolean  is_child)
295 {
296   GtkPlugPrivate *priv = plug->priv;
297   GtkWidget *widget = GTK_WIDGET (plug);
298
299   g_assert (!gtk_widget_get_parent (widget));
300
301   if (is_child)
302     {
303       if (priv->modality_window)
304         gtk_plug_handle_modality_off (plug);
305
306       if (priv->modality_group)
307         {
308           gtk_window_group_remove_window (priv->modality_group, GTK_WINDOW (plug));
309           g_object_unref (priv->modality_group);
310           priv->modality_group = NULL;
311         }
312       
313       /* As a toplevel, the MAPPED flag doesn't correspond
314        * to whether the widget->window is mapped; we unmap
315        * here, but don't bother remapping -- we will get mapped
316        * by gtk_widget_set_parent ().
317        */
318       if (gtk_widget_get_mapped (widget))
319         gtk_widget_unmap (widget);
320
321       _gtk_window_set_is_toplevel (GTK_WINDOW (plug), FALSE);
322       gtk_container_set_resize_mode (GTK_CONTAINER (plug), GTK_RESIZE_PARENT);
323
324       _gtk_widget_propagate_hierarchy_changed (widget, widget);
325     }
326   else
327     {
328       if (gtk_window_get_focus (GTK_WINDOW (plug)))
329         gtk_window_set_focus (GTK_WINDOW (plug), NULL);
330       if (gtk_window_get_default_widget (GTK_WINDOW (plug)))
331         gtk_window_set_default (GTK_WINDOW (plug), NULL);
332
333       priv->modality_group = gtk_window_group_new ();
334       gtk_window_group_add_window (priv->modality_group, GTK_WINDOW (plug));
335
336       _gtk_window_set_is_toplevel (GTK_WINDOW (plug), TRUE);
337       gtk_container_set_resize_mode (GTK_CONTAINER (plug), GTK_RESIZE_QUEUE);
338
339       _gtk_widget_propagate_hierarchy_changed (GTK_WIDGET (plug), NULL);
340     }
341 }
342
343 /**
344  * gtk_plug_get_id:
345  * @plug: a #GtkPlug.
346  * 
347  * Gets the window ID of a #GtkPlug widget, which can then
348  * be used to embed this window inside another window, for
349  * instance with gtk_socket_add_id().
350  * 
351  * Return value: the window ID for the plug
352  **/
353 Window
354 gtk_plug_get_id (GtkPlug *plug)
355 {
356   g_return_val_if_fail (GTK_IS_PLUG (plug), 0);
357
358   if (!gtk_widget_get_realized (GTK_WIDGET (plug)))
359     gtk_widget_realize (GTK_WIDGET (plug));
360
361   return GDK_WINDOW_XID (gtk_widget_get_window (GTK_WIDGET (plug)));
362 }
363
364 /**
365  * gtk_plug_get_embedded:
366  * @plug: a #GtkPlug
367  *
368  * Determines whether the plug is embedded in a socket.
369  *
370  * Return value: %TRUE if the plug is embedded in a socket
371  *
372  * Since: 2.14
373  **/
374 gboolean
375 gtk_plug_get_embedded (GtkPlug *plug)
376 {
377   g_return_val_if_fail (GTK_IS_PLUG (plug), FALSE);
378
379   return plug->priv->socket_window != NULL;
380 }
381
382 /**
383  * gtk_plug_get_socket_window:
384  * @plug: a #GtkPlug
385  *
386  * Retrieves the socket the plug is embedded in.
387  *
388  * Return value: (transfer none): the window of the socket, or %NULL
389  *
390  * Since: 2.14
391  **/
392 GdkWindow *
393 gtk_plug_get_socket_window (GtkPlug *plug)
394 {
395   g_return_val_if_fail (GTK_IS_PLUG (plug), NULL);
396
397   return plug->priv->socket_window;
398 }
399
400 /**
401  * _gtk_plug_add_to_socket:
402  * @plug: a #GtkPlug
403  * @socket_: a #GtkSocket
404  * 
405  * Adds a plug to a socket within the same application.
406  **/
407 void
408 _gtk_plug_add_to_socket (GtkPlug   *plug,
409                          GtkSocket *socket_)
410 {
411   GtkPlugPrivate *priv;
412   GtkWidget *widget;
413   
414   g_return_if_fail (GTK_IS_PLUG (plug));
415   g_return_if_fail (GTK_IS_SOCKET (socket_));
416   g_return_if_fail (gtk_widget_get_realized (GTK_WIDGET (socket_)));
417
418   priv = plug->priv;
419   widget = GTK_WIDGET (plug);
420
421   gtk_plug_set_is_child (plug, TRUE);
422   priv->same_app = TRUE;
423   socket_->priv->same_app = TRUE;
424   socket_->priv->plug_widget = widget;
425
426   priv->socket_window = gtk_widget_get_window (GTK_WIDGET (socket_));
427   g_object_ref (priv->socket_window);
428   g_signal_emit (plug, plug_signals[EMBEDDED], 0);
429   g_object_notify (G_OBJECT (plug), "embedded");
430
431   if (gtk_widget_get_realized (widget))
432     {
433       GdkWindow *window;
434
435       window = gtk_widget_get_window (widget);
436       gdk_window_reparent (window, priv->socket_window,
437                            -gdk_window_get_width (window),
438                            -gdk_window_get_height (window));
439     }
440
441   gtk_widget_set_parent (widget, GTK_WIDGET (socket_));
442
443   g_signal_emit_by_name (socket_, "plug-added");
444 }
445
446 /**
447  * _gtk_plug_send_delete_event:
448  * @widget: a #GtkWidget
449  *
450  * Send a GDK_DELETE event to the @widget and destroy it if
451  * necessary. Internal GTK function, called from this file or the
452  * backend-specific GtkPlug implementation.
453  */
454 void
455 _gtk_plug_send_delete_event (GtkWidget *widget)
456 {
457   GdkEvent *event = gdk_event_new (GDK_DELETE);
458
459   event->any.window = g_object_ref (gtk_widget_get_window (widget));
460   event->any.send_event = FALSE;
461
462   g_object_ref (widget);
463
464   if (!gtk_widget_event (widget, event))
465     gtk_widget_destroy (widget);
466
467   g_object_unref (widget);
468
469   gdk_event_free (event);
470 }
471
472 /**
473  * _gtk_plug_remove_from_socket:
474  * @plug: a #GtkPlug
475  * @socket_: a #GtkSocket
476  * 
477  * Removes a plug from a socket within the same application.
478  **/
479 void
480 _gtk_plug_remove_from_socket (GtkPlug   *plug,
481                               GtkSocket *socket_)
482 {
483   GtkPlugPrivate *priv;
484   GtkWidget *widget;
485   GdkWindow *window;
486   gboolean result;
487   gboolean widget_was_visible;
488
489   g_return_if_fail (GTK_IS_PLUG (plug));
490   g_return_if_fail (GTK_IS_SOCKET (socket_));
491   g_return_if_fail (gtk_widget_get_realized (GTK_WIDGET (plug)));
492
493   priv = plug->priv;
494   widget = GTK_WIDGET (plug);
495
496   if (_gtk_widget_get_in_reparent (widget))
497     return;
498
499   g_object_ref (plug);
500   g_object_ref (socket_);
501
502   widget_was_visible = gtk_widget_get_visible (widget);
503   window = gtk_widget_get_window (widget);
504
505   gdk_window_hide (window);
506   _gtk_widget_set_in_reparent (widget, TRUE);
507   gdk_window_reparent (window,
508                        gtk_widget_get_root_window (widget),
509                        0, 0);
510   gtk_widget_unparent (GTK_WIDGET (plug));
511   _gtk_widget_set_in_reparent (widget, FALSE);
512   
513   socket_->priv->plug_widget = NULL;
514   if (socket_->priv->plug_window != NULL)
515     {
516       g_object_unref (socket_->priv->plug_window);
517       socket_->priv->plug_window = NULL;
518     }
519   
520   socket_->priv->same_app = FALSE;
521
522   priv->same_app = FALSE;
523   if (priv->socket_window != NULL)
524     {
525       g_object_unref (priv->socket_window);
526       priv->socket_window = NULL;
527     }
528   gtk_plug_set_is_child (plug, FALSE);
529
530   g_signal_emit_by_name (socket_, "plug-removed", &result);
531   if (!result)
532     gtk_widget_destroy (GTK_WIDGET (socket_));
533
534   if (window)
535     _gtk_plug_send_delete_event (widget);
536
537   g_object_unref (plug);
538
539   if (widget_was_visible && gtk_widget_get_visible (GTK_WIDGET (socket_)))
540     gtk_widget_queue_resize (GTK_WIDGET (socket_));
541
542   g_object_unref (socket_);
543 }
544
545 /**
546  * gtk_plug_construct:
547  * @plug: a #GtkPlug.
548  * @socket_id: the XID of the socket's window.
549  *
550  * Finish the initialization of @plug for a given #GtkSocket identified by
551  * @socket_id. This function will generally only be used by classes deriving from #GtkPlug.
552  **/
553 void
554 gtk_plug_construct (GtkPlug *plug,
555                     Window   socket_id)
556 {
557   gtk_plug_construct_for_display (plug, gdk_display_get_default (), socket_id);
558 }
559
560 /**
561  * gtk_plug_construct_for_display:
562  * @plug: a #GtkPlug.
563  * @display: the #GdkDisplay associated with @socket_id's 
564  *           #GtkSocket.
565  * @socket_id: the XID of the socket's window.
566  *
567  * Finish the initialization of @plug for a given #GtkSocket identified by
568  * @socket_id which is currently displayed on @display.
569  * This function will generally only be used by classes deriving from #GtkPlug.
570  *
571  * Since: 2.2
572  **/
573 void
574 gtk_plug_construct_for_display (GtkPlug    *plug,
575                                 GdkDisplay *display,
576                                 Window      socket_id)
577 {
578   GtkPlugPrivate *priv;
579
580   g_return_if_fail (GTK_IS_PLUG (plug));
581   g_return_if_fail (GDK_IS_DISPLAY (display));
582
583   priv = plug->priv;
584
585   if (socket_id)
586     {
587       gpointer user_data = NULL;
588
589       if (GDK_IS_X11_DISPLAY (display))
590         priv->socket_window = gdk_x11_window_lookup_for_display (display, socket_id);
591       else
592         priv->socket_window = NULL;
593
594       if (priv->socket_window)
595         {
596           gdk_window_get_user_data (priv->socket_window, &user_data);
597
598           if (user_data)
599             {
600               if (GTK_IS_SOCKET (user_data))
601                 _gtk_plug_add_to_socket (plug, user_data);
602               else
603                 {
604                   g_warning (G_STRLOC "Can't create GtkPlug as child of non-GtkSocket");
605                   priv->socket_window = NULL;
606                 }
607             }
608           else
609             g_object_ref (priv->socket_window);
610         }
611       else if (GDK_IS_X11_DISPLAY (display))
612         priv->socket_window = gdk_x11_window_foreign_new_for_display (display, socket_id);
613
614       if (priv->socket_window) {
615         g_signal_emit (plug, plug_signals[EMBEDDED], 0);
616
617         g_object_notify (G_OBJECT (plug), "embedded");
618       }
619     }
620 }
621
622 /**
623  * gtk_plug_new:
624  * @socket_id:  the window ID of the socket, or 0.
625  * 
626  * Creates a new plug widget inside the #GtkSocket identified
627  * by @socket_id. If @socket_id is 0, the plug is left "unplugged" and
628  * can later be plugged into a #GtkSocket by  gtk_socket_add_id().
629  * 
630  * Return value: the new #GtkPlug widget.
631  **/
632 GtkWidget*
633 gtk_plug_new (Window socket_id)
634 {
635   return gtk_plug_new_for_display (gdk_display_get_default (), socket_id);
636 }
637
638 /**
639  * gtk_plug_new_for_display:
640  * @display: the #GdkDisplay on which @socket_id is displayed
641  * @socket_id: the XID of the socket's window.
642  * 
643  * Create a new plug widget inside the #GtkSocket identified by socket_id.
644  *
645  * Return value: the new #GtkPlug widget.
646  *
647  * Since: 2.2
648  */
649 GtkWidget*
650 gtk_plug_new_for_display (GdkDisplay *display,
651                           Window      socket_id)
652 {
653   GtkPlug *plug;
654
655   g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
656
657   plug = g_object_new (GTK_TYPE_PLUG, NULL);
658   gtk_plug_construct_for_display (plug, display, socket_id);
659   return GTK_WIDGET (plug);
660 }
661
662 static void
663 gtk_plug_finalize (GObject *object)
664 {
665   GtkPlug *plug = GTK_PLUG (object);
666   GtkPlugPrivate *priv = plug->priv;
667
668   if (priv->grabbed_keys)
669     g_hash_table_destroy (priv->grabbed_keys);
670
671   G_OBJECT_CLASS (gtk_plug_parent_class)->finalize (object);
672 }
673
674 static void
675 gtk_plug_unrealize (GtkWidget *widget)
676 {
677   GtkPlug *plug = GTK_PLUG (widget);
678   GtkPlugPrivate *priv = plug->priv;
679
680   if (priv->socket_window != NULL)
681     {
682       gdk_window_set_user_data (priv->socket_window, NULL);
683       g_object_unref (priv->socket_window);
684       priv->socket_window = NULL;
685
686       g_object_notify (G_OBJECT (widget), "embedded");
687     }
688
689   if (!priv->same_app)
690     {
691       if (priv->modality_window)
692         gtk_plug_handle_modality_off (plug);
693
694       gtk_window_group_remove_window (priv->modality_group, GTK_WINDOW (plug));
695       g_object_unref (priv->modality_group);
696     }
697
698   GTK_WIDGET_CLASS (gtk_plug_parent_class)->unrealize (widget);
699 }
700
701 static void
702 xembed_set_info (GdkWindow     *window,
703                  unsigned long  flags)
704 {
705   GdkDisplay *display = gdk_window_get_display (window);
706   unsigned long buffer[2];
707
708   Atom xembed_info_atom = gdk_x11_get_xatom_by_name_for_display (display, "_XEMBED_INFO");
709
710   buffer[0] = GTK_XEMBED_PROTOCOL_VERSION;
711   buffer[1] = flags;
712
713   XChangeProperty (GDK_DISPLAY_XDISPLAY (display),
714                    GDK_WINDOW_XID (window),
715                    xembed_info_atom, xembed_info_atom, 32,
716                    PropModeReplace,
717                    (unsigned char *)buffer, 2);
718 }
719
720 /**
721  * gtk_plug_focus_first_last:
722  *
723  * @plug: a #GtkPlug
724  * @direction: a direction
725  *
726  * Called from the GtkPlug backend when the corresponding socket has
727  * told the plug that it has received the focus.
728  */
729 static void
730 gtk_plug_focus_first_last (GtkPlug          *plug,
731                            GtkDirectionType  direction)
732 {
733   GtkWindow *window = GTK_WINDOW (plug);
734   GtkWidget *focus_widget;
735   GtkWidget *parent;
736
737   focus_widget = gtk_window_get_focus (window);
738   if (focus_widget)
739     {
740       parent = gtk_widget_get_parent (focus_widget);
741       while (parent)
742         {
743           gtk_container_set_focus_child (GTK_CONTAINER (parent), NULL);
744           parent = gtk_widget_get_parent (parent);
745         }
746       
747       gtk_window_set_focus (GTK_WINDOW (plug), NULL);
748     }
749
750   gtk_widget_child_focus (GTK_WIDGET (plug), direction);
751 }
752
753 static void
754 handle_xembed_message (GtkPlug           *plug,
755                        XEmbedMessageType  message,
756                        glong              detail,
757                        glong              data1,
758                        glong              data2,
759                        guint32            time)
760 {
761   GtkWindow *window = GTK_WINDOW (plug);
762
763   GTK_NOTE (PLUGSOCKET,
764             g_message ("GtkPlug: %s received", _gtk_xembed_message_name (message)));
765   
766   switch (message)
767     {
768     case XEMBED_EMBEDDED_NOTIFY:
769       break;
770     case XEMBED_WINDOW_ACTIVATE:
771       _gtk_window_set_is_active (window, TRUE);
772       break;
773     case XEMBED_WINDOW_DEACTIVATE:
774       _gtk_window_set_is_active (window, FALSE);
775       break;
776       
777     case XEMBED_MODALITY_ON:
778       gtk_plug_handle_modality_on (plug);
779       break;
780     case XEMBED_MODALITY_OFF:
781       gtk_plug_handle_modality_off (plug);
782       break;
783
784     case XEMBED_FOCUS_IN:
785       _gtk_window_set_has_toplevel_focus (window, TRUE);
786       switch (detail)
787         {
788         case XEMBED_FOCUS_FIRST:
789           gtk_plug_focus_first_last (plug, GTK_DIR_TAB_FORWARD);
790           break;
791         case XEMBED_FOCUS_LAST:
792           gtk_plug_focus_first_last (plug, GTK_DIR_TAB_BACKWARD);
793           break;
794         case XEMBED_FOCUS_CURRENT:
795           break;
796         }
797       break;
798
799     case XEMBED_FOCUS_OUT:
800       _gtk_window_set_has_toplevel_focus (window, FALSE);
801       break;
802       
803     case XEMBED_GRAB_KEY:
804     case XEMBED_UNGRAB_KEY:
805     case XEMBED_GTK_GRAB_KEY:
806     case XEMBED_GTK_UNGRAB_KEY:
807     case XEMBED_REQUEST_FOCUS:
808     case XEMBED_FOCUS_NEXT:
809     case XEMBED_FOCUS_PREV:
810       g_warning ("GtkPlug: Invalid _XEMBED message %s received", _gtk_xembed_message_name (message));
811       break;
812       
813     default:
814       GTK_NOTE(PLUGSOCKET,
815                g_message ("GtkPlug: Ignoring unknown _XEMBED message of type %d", message));
816       break;
817     }
818 }
819 static GdkFilterReturn
820 gtk_plug_filter_func (GdkXEvent *gdk_xevent,
821                       GdkEvent  *event,
822                       gpointer   data)
823 {
824   GdkScreen *screen = gdk_window_get_screen (event->any.window);
825   GdkDisplay *display = gdk_screen_get_display (screen);
826   GtkPlug *plug = GTK_PLUG (data);
827   GtkPlugPrivate *priv = plug->priv;
828   XEvent *xevent = (XEvent *)gdk_xevent;
829   GHashTableIter iter;
830   gpointer key;
831   GdkFilterReturn return_val;
832
833   return_val = GDK_FILTER_CONTINUE;
834
835   switch (xevent->type)
836     {
837     case ClientMessage:
838       if (xevent->xclient.message_type == gdk_x11_get_xatom_by_name_for_display (display, "_XEMBED"))
839         {
840           _gtk_xembed_push_message (xevent);
841           handle_xembed_message (plug,
842                                  xevent->xclient.data.l[1],
843                                  xevent->xclient.data.l[2],
844                                  xevent->xclient.data.l[3],
845                                  xevent->xclient.data.l[4],
846                                  xevent->xclient.data.l[0]);
847           _gtk_xembed_pop_message ();
848
849           return_val = GDK_FILTER_REMOVE;
850         }
851       else if (xevent->xclient.message_type == gdk_x11_get_xatom_by_name_for_display (display, "WM_DELETE_WINDOW"))
852         {
853           /* We filter these out because we take being reparented back to the
854            * root window as the reliable end of the embedding protocol
855            */
856
857           return_val = GDK_FILTER_REMOVE;
858         }
859       break;
860     case ReparentNotify:
861       {
862         XReparentEvent *xre = &xevent->xreparent;
863         gboolean was_embedded = priv->socket_window != NULL;
864
865         GTK_NOTE (PLUGSOCKET, g_message("GtkPlug: ReparentNotify received"));
866
867         return_val = GDK_FILTER_REMOVE;
868         
869         g_object_ref (plug);
870         
871         if (was_embedded)
872           {
873             /* End of embedding protocol for previous socket */
874             
875             GTK_NOTE (PLUGSOCKET, g_message ("GtkPlug: end of embedding"));
876             /* FIXME: race if we remove from another socket and
877              * then add to a local window before we get notification
878              * Probably need check in _gtk_plug_add_to_socket
879              */
880
881             if (xre->parent != GDK_WINDOW_XID (priv->socket_window))
882               {
883                 GtkWidget *widget = GTK_WIDGET (plug);
884
885                 gdk_window_set_user_data (priv->socket_window, NULL);
886                 g_object_unref (priv->socket_window);
887                 priv->socket_window = NULL;
888
889                 /* Emit a delete window, as if the user attempted
890                  * to close the toplevel. Simple as to how we
891                  * handle WM_DELETE_WINDOW, if it isn't handled
892                  * we destroy the widget. BUt only do this if
893                  * we are being reparented to the root window.
894                  * Moving from one embedder to another should
895                  * be invisible to the app.
896                  */
897
898                 if (xre->parent == GDK_WINDOW_XID (gdk_screen_get_root_window (screen)))
899                   {
900                     GTK_NOTE (PLUGSOCKET, g_message ("GtkPlug: calling gtk_plug_send_delete_event()"));
901                     _gtk_plug_send_delete_event (widget);
902
903                     g_object_notify (G_OBJECT (plug), "embedded");
904                   }
905               }
906             else
907               goto done;
908           }
909
910         if (xre->parent != GDK_WINDOW_XID (gdk_screen_get_root_window (screen)))
911           {
912             /* Start of embedding protocol */
913
914             GTK_NOTE (PLUGSOCKET, g_message ("GtkPlug: start of embedding"));
915
916             priv->socket_window = gdk_x11_window_lookup_for_display (display, xre->parent);
917             if (priv->socket_window)
918               {
919                 gpointer user_data = NULL;
920                 gdk_window_get_user_data (priv->socket_window, &user_data);
921
922                 if (user_data)
923                   {
924                     g_warning (G_STRLOC "Plug reparented unexpectedly into window in the same process");
925                     priv->socket_window = NULL;
926                     break; /* FIXME: shouldn't this unref the plug? i.e. "goto done;" instead */
927                   }
928
929                 g_object_ref (priv->socket_window);
930               }
931             else
932               {
933                 priv->socket_window = gdk_x11_window_foreign_new_for_display (display, xre->parent);
934                 if (!priv->socket_window) /* Already gone */
935                   break; /* FIXME: shouldn't this unref the plug? i.e. "goto done;" instead */
936               }
937
938             if (priv->grabbed_keys)
939               {
940                 g_hash_table_iter_init (&iter, priv->grabbed_keys);
941                 while (g_hash_table_iter_next (&iter, &key, NULL))
942                   {
943                     GrabbedKey *grabbed_key = key;
944
945                     _gtk_xembed_send_message (priv->socket_window, XEMBED_GTK_GRAB_KEY, 0,
946                                               grabbed_key->accelerator_key,
947                                               grabbed_key->accelerator_mods);
948                   }
949               }
950
951             if (!was_embedded)
952               g_signal_emit_by_name (plug, "embedded");
953
954             g_object_notify (G_OBJECT (plug), "embedded");
955           }
956
957       done:
958         g_object_unref (plug);
959         
960         break;
961       }
962     case KeyPress:
963     case KeyRelease:
964       {
965         GdkModifierType state, consumed;
966         GdkDeviceManager *device_manager;
967         GdkDevice *pointer, *keyboard;
968         GdkKeymap *keymap;
969
970         if (xevent->type == KeyPress)
971           event->key.type = GDK_KEY_PRESS;
972         else
973           event->key.type = GDK_KEY_RELEASE;
974
975         event->key.window = gdk_x11_window_lookup_for_display (display, xevent->xany.window);
976         event->key.send_event = TRUE;
977         event->key.time = xevent->xkey.time;
978         event->key.state = (GdkModifierType) xevent->xkey.state;
979         event->key.hardware_keycode = xevent->xkey.keycode;
980         event->key.keyval = GDK_KEY_VoidSymbol;
981
982         device_manager = gdk_display_get_device_manager (display);
983         pointer = gdk_device_manager_get_client_pointer (device_manager);
984         keyboard = gdk_device_get_associated_device (pointer);
985         gdk_event_set_device (event, keyboard);
986
987         keymap = gdk_keymap_get_for_display (display);
988         gdk_keymap_translate_keyboard_state (keymap,
989                                              event->key.hardware_keycode,
990                                              event->key.state,
991                                              event->key.group,
992                                              &event->key.keyval,
993                                              NULL, NULL, &consumed);
994
995         state = event->key.state & ~consumed;
996         gdk_keymap_add_virtual_modifiers (keymap, &state);
997         event->key.state |= state;
998
999         event->key.length = 0;
1000         event->key.string = g_strdup ("");
1001
1002         /* FIXME: These should be filled in properly */
1003         event->key.group = 0;
1004         event->key.is_modifier = FALSE;
1005
1006         return_val = GDK_FILTER_TRANSLATE;
1007       }
1008     }
1009
1010   return return_val;
1011 }
1012 static void
1013 gtk_plug_realize (GtkWidget *widget)
1014 {
1015   GtkAllocation allocation;
1016   GtkPlug *plug = GTK_PLUG (widget);
1017   GtkPlugPrivate *priv = plug->priv;
1018   GtkWindow *window = GTK_WINDOW (widget);
1019   GdkWindow *gdk_window;
1020   GdkWindowAttr attributes;
1021   const gchar *title;
1022   gchar *wmclass_name, *wmclass_class;
1023   gint attributes_mask;
1024
1025   gtk_widget_set_realized (widget, TRUE);
1026
1027   title = gtk_window_get_title (window);
1028   _gtk_window_get_wmclass (window, &wmclass_name, &wmclass_class);
1029   gtk_widget_get_allocation (widget, &allocation);
1030
1031   attributes.window_type = GDK_WINDOW_CHILD;    /* XXX GDK_WINDOW_PLUG ? */
1032   attributes.title = (gchar *) title;
1033   attributes.wmclass_name = wmclass_name;
1034   attributes.wmclass_class = wmclass_class;
1035   attributes.width = allocation.width;
1036   attributes.height = allocation.height;
1037   attributes.wclass = GDK_INPUT_OUTPUT;
1038
1039   /* this isn't right - we should match our parent's visual/colormap.
1040    * though that will require handling "foreign" colormaps */
1041   attributes.visual = gtk_widget_get_visual (widget);
1042   attributes.event_mask = gtk_widget_get_events (widget);
1043   attributes.event_mask |= (GDK_EXPOSURE_MASK |
1044                             GDK_KEY_PRESS_MASK |
1045                             GDK_KEY_RELEASE_MASK |
1046                             GDK_ENTER_NOTIFY_MASK |
1047                             GDK_LEAVE_NOTIFY_MASK |
1048                             GDK_STRUCTURE_MASK);
1049
1050   attributes_mask = GDK_WA_VISUAL;
1051   attributes_mask |= (title ? GDK_WA_TITLE : 0);
1052   attributes_mask |= (wmclass_name ? GDK_WA_WMCLASS : 0);
1053
1054   if (gtk_widget_is_toplevel (widget))
1055     {
1056       attributes.window_type = GDK_WINDOW_TOPLEVEL;
1057
1058       gdk_error_trap_push ();
1059       if (priv->socket_window)
1060         gdk_window = gdk_window_new (priv->socket_window,
1061                                      &attributes, attributes_mask);
1062       else /* If it's a passive plug, we use the root window */
1063         gdk_window = gdk_window_new (gtk_widget_get_root_window (widget),
1064                                      &attributes, attributes_mask);
1065       gtk_widget_set_window (widget, gdk_window);
1066
1067       gdk_display_sync (gtk_widget_get_display (widget));
1068       if (gdk_error_trap_pop ()) /* Uh-oh */
1069         {
1070           gdk_error_trap_push ();
1071           gdk_window_destroy (gdk_window);
1072           gdk_error_trap_pop_ignored ();
1073           gdk_window = gdk_window_new (gtk_widget_get_root_window (widget),
1074                                    &attributes, attributes_mask);
1075           gtk_widget_set_window (widget, gdk_window);
1076         }
1077
1078       gdk_window_add_filter (gdk_window,
1079                              gtk_plug_filter_func,
1080                              widget);
1081
1082       priv->modality_group = gtk_window_group_new ();
1083       gtk_window_group_add_window (priv->modality_group, window);
1084
1085       xembed_set_info (gtk_widget_get_window (GTK_WIDGET (plug)), 0);
1086     }
1087   else
1088     {
1089       gdk_window = gdk_window_new (gtk_widget_get_parent_window (widget),
1090                                    &attributes, attributes_mask);
1091       gtk_widget_set_window (widget, gdk_window);
1092     }
1093
1094   gdk_window_set_user_data (gdk_window, window);
1095
1096   gtk_style_context_set_background (gtk_widget_get_style_context (widget),
1097                                     gdk_window);
1098
1099   gdk_window_enable_synchronized_configure (gdk_window);
1100 }
1101
1102 static void
1103 gtk_plug_show (GtkWidget *widget)
1104 {
1105   if (gtk_widget_is_toplevel (widget))
1106     GTK_WIDGET_CLASS (gtk_plug_parent_class)->show (widget);
1107   else
1108     GTK_WIDGET_CLASS (bin_class)->show (widget);
1109 }
1110
1111 static void
1112 gtk_plug_hide (GtkWidget *widget)
1113 {
1114   if (gtk_widget_is_toplevel (widget))
1115     GTK_WIDGET_CLASS (gtk_plug_parent_class)->hide (widget);
1116   else
1117     GTK_WIDGET_CLASS (bin_class)->hide (widget);
1118 }
1119
1120 /* From gdkinternals.h */
1121 void gdk_synthesize_window_state (GdkWindow     *window,
1122                                   GdkWindowState unset_flags,
1123                                   GdkWindowState set_flags);
1124
1125 static void
1126 gtk_plug_map (GtkWidget *widget)
1127 {
1128   if (gtk_widget_is_toplevel (widget))
1129     {
1130       GtkBin *bin = GTK_BIN (widget);
1131       GtkPlug *plug = GTK_PLUG (widget);
1132       GtkWidget *child;
1133
1134       gtk_widget_set_mapped (widget, TRUE);
1135
1136       child = gtk_bin_get_child (bin);
1137       if (child != NULL &&
1138           gtk_widget_get_visible (child) &&
1139           !gtk_widget_get_mapped (child))
1140         gtk_widget_map (child);
1141
1142       xembed_set_info (gtk_widget_get_window (GTK_WIDGET (plug)), XEMBED_MAPPED);
1143
1144       gdk_synthesize_window_state (gtk_widget_get_window (widget),
1145                                    GDK_WINDOW_STATE_WITHDRAWN,
1146                                    0);
1147     }
1148   else
1149     GTK_WIDGET_CLASS (bin_class)->map (widget);
1150 }
1151
1152 static void
1153 gtk_plug_unmap (GtkWidget *widget)
1154 {
1155   if (gtk_widget_is_toplevel (widget))
1156     {
1157       GtkPlug *plug = GTK_PLUG (widget);
1158       GdkWindow *window;
1159       GtkWidget *child;
1160
1161       window = gtk_widget_get_window (widget);
1162
1163       gtk_widget_set_mapped (widget, FALSE);
1164
1165       gdk_window_hide (window);
1166
1167       child = gtk_bin_get_child (GTK_BIN (widget));
1168       if (child != NULL)
1169         gtk_widget_unmap (child);
1170
1171       xembed_set_info (gtk_widget_get_window (GTK_WIDGET (plug)), 0);
1172
1173       gdk_synthesize_window_state (window,
1174                                    0,
1175                                    GDK_WINDOW_STATE_WITHDRAWN);
1176     }
1177   else
1178     GTK_WIDGET_CLASS (bin_class)->unmap (widget);
1179 }
1180
1181 static void
1182 gtk_plug_size_allocate (GtkWidget     *widget,
1183                         GtkAllocation *allocation)
1184 {
1185   GtkWidget *child;
1186
1187   if (gtk_widget_is_toplevel (widget))
1188     GTK_WIDGET_CLASS (gtk_plug_parent_class)->size_allocate (widget, allocation);
1189   else
1190     {
1191       GtkBin *bin = GTK_BIN (widget);
1192
1193       gtk_widget_set_allocation (widget, allocation);
1194
1195       if (gtk_widget_get_realized (widget))
1196         gdk_window_move_resize (gtk_widget_get_window (widget),
1197                                 allocation->x, allocation->y,
1198                                 allocation->width, allocation->height);
1199
1200       child = gtk_bin_get_child (bin);
1201
1202       if (child != NULL && gtk_widget_get_visible (child))
1203         {
1204           GtkAllocation child_allocation;
1205           
1206           child_allocation.x = child_allocation.y = gtk_container_get_border_width (GTK_CONTAINER (widget));
1207           child_allocation.width =
1208             MAX (1, (gint)allocation->width - child_allocation.x * 2);
1209           child_allocation.height =
1210             MAX (1, (gint)allocation->height - child_allocation.y * 2);
1211           
1212           gtk_widget_size_allocate (child, &child_allocation);
1213         }
1214       
1215     }
1216 }
1217
1218 static gboolean
1219 gtk_plug_key_press_event (GtkWidget   *widget,
1220                           GdkEventKey *event)
1221 {
1222   if (gtk_widget_is_toplevel (widget))
1223     return GTK_WIDGET_CLASS (gtk_plug_parent_class)->key_press_event (widget, event);
1224   else
1225     return FALSE;
1226 }
1227
1228 static gboolean
1229 gtk_plug_focus_event (GtkWidget      *widget,
1230                       GdkEventFocus  *event)
1231 {
1232   /* We eat focus-in events and focus-out events, since they
1233    * can be generated by something like a keyboard grab on
1234    * a child of the plug.
1235    */
1236   return FALSE;
1237 }
1238
1239 static void
1240 gtk_plug_set_focus (GtkWindow *window,
1241                     GtkWidget *focus)
1242 {
1243   GtkPlug *plug = GTK_PLUG (window);
1244   GtkPlugPrivate *priv = plug->priv;
1245
1246   GTK_WINDOW_CLASS (gtk_plug_parent_class)->set_focus (window, focus);
1247   
1248   /* Ask for focus from embedder
1249    */
1250
1251   if (focus && !gtk_window_has_toplevel_focus (window))
1252     _gtk_xembed_send_message (priv->socket_window,
1253                               XEMBED_REQUEST_FOCUS, 0, 0, 0);
1254 }
1255
1256 static guint
1257 grabbed_key_hash (gconstpointer a)
1258 {
1259   const GrabbedKey *key = a;
1260   guint h;
1261   
1262   h = key->accelerator_key << 16;
1263   h ^= key->accelerator_key >> 16;
1264   h ^= key->accelerator_mods;
1265
1266   return h;
1267 }
1268
1269 static gboolean
1270 grabbed_key_equal (gconstpointer a, gconstpointer b)
1271 {
1272   const GrabbedKey *keya = a;
1273   const GrabbedKey *keyb = b;
1274
1275   return (keya->accelerator_key == keyb->accelerator_key &&
1276           keya->accelerator_mods == keyb->accelerator_mods);
1277 }
1278
1279 static void
1280 add_grabbed_key (gpointer key, gpointer val, gpointer data)
1281 {
1282   GrabbedKey *grabbed_key = key;
1283   GtkPlug *plug = data;
1284   GtkPlugPrivate *priv = plug->priv;
1285
1286   if (!priv->grabbed_keys ||
1287       !g_hash_table_lookup (priv->grabbed_keys, grabbed_key))
1288     {
1289       _gtk_xembed_send_message (priv->socket_window, XEMBED_GTK_GRAB_KEY, 0,
1290                                 grabbed_key->accelerator_key,
1291                                 grabbed_key->accelerator_mods);
1292     }
1293 }
1294
1295 static void
1296 remove_grabbed_key (gpointer key, gpointer val, gpointer data)
1297 {
1298   GrabbedKey *grabbed_key = key;
1299   GtkPlug *plug = data;
1300   GtkPlugPrivate *priv = plug->priv;
1301
1302   if (!priv->grabbed_keys ||
1303       !g_hash_table_lookup (priv->grabbed_keys, grabbed_key))
1304     {
1305       _gtk_xembed_send_message (priv->socket_window, XEMBED_GTK_UNGRAB_KEY, 0,
1306                                 grabbed_key->accelerator_key,
1307                                 grabbed_key->accelerator_mods);
1308     }
1309 }
1310
1311 static void
1312 keys_foreach (GtkWindow      *window,
1313               guint           keyval,
1314               GdkModifierType modifiers,
1315               gboolean        is_mnemonic,
1316               gpointer        data)
1317 {
1318   GHashTable *new_grabbed_keys = data;
1319   GrabbedKey *key = g_slice_new (GrabbedKey);
1320
1321   key->accelerator_key = keyval;
1322   key->accelerator_mods = modifiers;
1323   
1324   g_hash_table_replace (new_grabbed_keys, key, key);
1325 }
1326
1327 static void
1328 grabbed_key_free (gpointer data)
1329 {
1330   g_slice_free (GrabbedKey, data);
1331 }
1332
1333 static void
1334 gtk_plug_keys_changed (GtkWindow *window)
1335 {
1336   GHashTable *new_grabbed_keys, *old_grabbed_keys;
1337   GtkPlug *plug = GTK_PLUG (window);
1338   GtkPlugPrivate *priv = plug->priv;
1339
1340   new_grabbed_keys = g_hash_table_new_full (grabbed_key_hash, grabbed_key_equal, (GDestroyNotify)grabbed_key_free, NULL);
1341   _gtk_window_keys_foreach (window, keys_foreach, new_grabbed_keys);
1342
1343   if (priv->socket_window)
1344     g_hash_table_foreach (new_grabbed_keys, add_grabbed_key, plug);
1345
1346   old_grabbed_keys = priv->grabbed_keys;
1347   priv->grabbed_keys = new_grabbed_keys;
1348
1349   if (old_grabbed_keys)
1350     {
1351       if (priv->socket_window)
1352         g_hash_table_foreach (old_grabbed_keys, remove_grabbed_key, plug);
1353       g_hash_table_destroy (old_grabbed_keys);
1354     }
1355 }
1356
1357 static void
1358 gtk_plug_focus_to_parent (GtkPlug         *plug,
1359                           GtkDirectionType direction)
1360 {
1361   GtkPlugPrivate *priv = plug->priv;
1362   XEmbedMessageType message;
1363   
1364   switch (direction)
1365     {
1366     case GTK_DIR_UP:
1367     case GTK_DIR_LEFT:
1368     case GTK_DIR_TAB_BACKWARD:
1369       message = XEMBED_FOCUS_PREV;
1370       break;
1371     case GTK_DIR_DOWN:
1372     case GTK_DIR_RIGHT:
1373     case GTK_DIR_TAB_FORWARD:
1374       message = XEMBED_FOCUS_NEXT;
1375       break;
1376     default:
1377       g_assert_not_reached ();
1378       message = XEMBED_FOCUS_PREV;
1379       break;
1380     }
1381
1382   _gtk_xembed_send_focus_message (priv->socket_window, message, 0);
1383 }
1384
1385 static gboolean
1386 gtk_plug_focus (GtkWidget        *widget,
1387                 GtkDirectionType  direction)
1388 {
1389   GtkBin *bin = GTK_BIN (widget);
1390   GtkPlug *plug = GTK_PLUG (widget);
1391   GtkWindow *window = GTK_WINDOW (widget);
1392   GtkContainer *container = GTK_CONTAINER (widget);
1393   GtkWidget *child;
1394   GtkWidget *old_focus_child;
1395   GtkWidget *parent;
1396
1397   old_focus_child = gtk_container_get_focus_child (container);
1398   /* We override GtkWindow's behavior, since we don't want wrapping here.
1399    */
1400   if (old_focus_child)
1401     {
1402       GtkWidget *focus_widget;
1403
1404       if (gtk_widget_child_focus (old_focus_child, direction))
1405         return TRUE;
1406
1407       focus_widget = gtk_window_get_focus (window);
1408       if (focus_widget)
1409         {
1410           /* Wrapped off the end, clear the focus setting for the toplevel */
1411           parent = gtk_widget_get_parent (focus_widget);
1412           while (parent)
1413             {
1414               gtk_container_set_focus_child (GTK_CONTAINER (parent), NULL);
1415               parent = gtk_widget_get_parent (parent);
1416             }
1417           
1418           gtk_window_set_focus (GTK_WINDOW (container), NULL);
1419         }
1420     }
1421   else
1422     {
1423       /* Try to focus the first widget in the window */
1424       child = gtk_bin_get_child (bin);
1425       if (child && gtk_widget_child_focus (child, direction))
1426         return TRUE;
1427     }
1428
1429   if (!gtk_container_get_focus_child (GTK_CONTAINER (window)))
1430     gtk_plug_focus_to_parent (plug, direction);
1431
1432   return FALSE;
1433 }
1434
1435 static void
1436 gtk_plug_check_resize (GtkContainer *container)
1437 {
1438   if (gtk_widget_is_toplevel (GTK_WIDGET (container)))
1439     GTK_CONTAINER_CLASS (gtk_plug_parent_class)->check_resize (container);
1440   else
1441     GTK_CONTAINER_CLASS (bin_class)->check_resize (container);
1442 }
1443