]> Pileus Git - ~andy/gtk/blob - gtk/gtkplug.c
x11: Rename GdkDisplayX11 to GdkX11Display
[~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, 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 "gtkmain.h"
31 #include "gtkmarshalers.h"
32 #include "gtkplug.h"
33 #include "gtkintl.h"
34 #include "gtkprivate.h"
35 #include "gtkplugprivate.h"
36 #include "gtksocketprivate.h"
37 #include "gtkwidgetprivate.h"
38 #include "gtkwindowprivate.h"
39
40 #ifdef GDK_WINDOWING_X11
41 #include "x11/gdkx.h"
42 #endif
43
44 /**
45  * SECTION:gtkplug
46  * @Short_description: Toplevel for embedding into other processes
47  * @Title: GtkPlug
48  * @See_also: #GtkSocket
49  *
50  * Together with #GtkSocket, #GtkPlug 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 the
54  * ID of that widget's window 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  * <note>
60  * The #GtkPlug and #GtkSocket widgets are currently not available
61  * on all platforms supported by GTK+.
62  * </note>
63  */
64
65 static void            gtk_plug_get_property          (GObject     *object,
66                                                        guint        prop_id,
67                                                        GValue      *value,
68                                                        GParamSpec  *pspec);
69 static void            gtk_plug_finalize              (GObject          *object);
70 static void            gtk_plug_realize               (GtkWidget        *widget);
71 static void            gtk_plug_unrealize             (GtkWidget        *widget);
72 static void            gtk_plug_show                  (GtkWidget        *widget);
73 static void            gtk_plug_hide                  (GtkWidget        *widget);
74 static void            gtk_plug_map                   (GtkWidget        *widget);
75 static void            gtk_plug_unmap                 (GtkWidget        *widget);
76 static void            gtk_plug_size_allocate         (GtkWidget        *widget,
77                                                        GtkAllocation    *allocation);
78 static gboolean        gtk_plug_key_press_event       (GtkWidget        *widget,
79                                                        GdkEventKey      *event);
80 static gboolean        gtk_plug_focus_event           (GtkWidget        *widget,
81                                                        GdkEventFocus    *event);
82 static void            gtk_plug_set_focus             (GtkWindow        *window,
83                                                        GtkWidget        *focus);
84 static gboolean        gtk_plug_focus                 (GtkWidget        *widget,
85                                                        GtkDirectionType  direction);
86 static void            gtk_plug_check_resize          (GtkContainer     *container);
87 static void            gtk_plug_keys_changed          (GtkWindow        *window);
88
89 static GtkBinClass *bin_class = NULL;
90
91 typedef struct
92 {
93   guint                  accelerator_key;
94   GdkModifierType        accelerator_mods;
95 } GrabbedKey;
96
97 enum {
98   PROP_0,
99   PROP_EMBEDDED,
100   PROP_SOCKET_WINDOW
101 };
102
103 enum {
104   EMBEDDED,
105   LAST_SIGNAL
106 }; 
107
108 static guint plug_signals[LAST_SIGNAL] = { 0 };
109
110 G_DEFINE_TYPE (GtkPlug, gtk_plug, GTK_TYPE_WINDOW)
111
112 static void
113 gtk_plug_get_property (GObject    *object,
114                        guint       prop_id,
115                        GValue     *value,
116                        GParamSpec *pspec)
117 {
118   GtkPlug *plug = GTK_PLUG (object);
119   GtkPlugPrivate *priv = plug->priv;
120
121   switch (prop_id)
122     {
123     case PROP_EMBEDDED:
124       g_value_set_boolean (value, priv->socket_window != NULL);
125       break;
126     case PROP_SOCKET_WINDOW:
127       g_value_set_object (value, priv->socket_window);
128       break;
129     default:
130       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
131       break;
132     }
133 }
134
135 static void
136 gtk_plug_class_init (GtkPlugClass *class)
137 {
138   GObjectClass *gobject_class = (GObjectClass *)class;
139   GtkWidgetClass *widget_class = (GtkWidgetClass *)class;
140   GtkWindowClass *window_class = (GtkWindowClass *)class;
141   GtkContainerClass *container_class = (GtkContainerClass *)class;
142
143   bin_class = g_type_class_peek (GTK_TYPE_BIN);
144
145   gobject_class->get_property = gtk_plug_get_property;
146   gobject_class->finalize = gtk_plug_finalize;
147   
148   widget_class->realize = gtk_plug_realize;
149   widget_class->unrealize = gtk_plug_unrealize;
150   widget_class->key_press_event = gtk_plug_key_press_event;
151   widget_class->focus_in_event = gtk_plug_focus_event;
152   widget_class->focus_out_event = gtk_plug_focus_event;
153
154   widget_class->show = gtk_plug_show;
155   widget_class->hide = gtk_plug_hide;
156   widget_class->map = gtk_plug_map;
157   widget_class->unmap = gtk_plug_unmap;
158   widget_class->size_allocate = gtk_plug_size_allocate;
159
160   widget_class->focus = gtk_plug_focus;
161
162   container_class->check_resize = gtk_plug_check_resize;
163
164   window_class->set_focus = gtk_plug_set_focus;
165   window_class->keys_changed = gtk_plug_keys_changed;
166
167   /**
168    * GtkPlug:embedded:
169    *
170    * %TRUE if the plug is embedded in a socket.
171    *
172    * Since: 2.12
173    */
174   g_object_class_install_property (gobject_class,
175                                    PROP_EMBEDDED,
176                                    g_param_spec_boolean ("embedded",
177                                                          P_("Embedded"),
178                                                          P_("Whether the plug is embedded"),
179                                                          FALSE,
180                                                          GTK_PARAM_READABLE));
181
182   /**
183    * GtkPlug:socket-window:
184    *
185    * The window of the socket the plug is embedded in.
186    *
187    * Since: 2.14
188    */
189   g_object_class_install_property (gobject_class,
190                                    PROP_SOCKET_WINDOW,
191                                    g_param_spec_object ("socket-window",
192                                                         P_("Socket Window"),
193                                                         P_("The window of the socket the plug is embedded in"),
194                                                         GDK_TYPE_WINDOW,
195                                                         GTK_PARAM_READABLE));
196
197   /**
198    * GtkPlug::embedded:
199    * @plug: the object on which the signal was emitted
200    *
201    * Gets emitted when the plug becomes embedded in a socket.
202    */ 
203   plug_signals[EMBEDDED] =
204     g_signal_new (I_("embedded"),
205                   G_OBJECT_CLASS_TYPE (class),
206                   G_SIGNAL_RUN_LAST,
207                   G_STRUCT_OFFSET (GtkPlugClass, embedded),
208                   NULL, NULL,
209                   _gtk_marshal_VOID__VOID,
210                   G_TYPE_NONE, 0);
211
212   g_type_class_add_private (class, sizeof (GtkPlugPrivate));
213 }
214
215 static void
216 gtk_plug_init (GtkPlug *plug)
217 {
218   plug->priv = G_TYPE_INSTANCE_GET_PRIVATE (plug,
219                                             GTK_TYPE_PLUG,
220                                             GtkPlugPrivate);
221 }
222
223 static void
224 gtk_plug_set_is_child (GtkPlug  *plug,
225                        gboolean  is_child)
226 {
227   GtkPlugPrivate *priv = plug->priv;
228   GtkWidget *widget = GTK_WIDGET (plug);
229
230   g_assert (!gtk_widget_get_parent (widget));
231
232   if (is_child)
233     {
234       if (priv->modality_window)
235         _gtk_plug_handle_modality_off (plug);
236
237       if (priv->modality_group)
238         {
239           gtk_window_group_remove_window (priv->modality_group, GTK_WINDOW (plug));
240           g_object_unref (priv->modality_group);
241           priv->modality_group = NULL;
242         }
243       
244       /* As a toplevel, the MAPPED flag doesn't correspond
245        * to whether the widget->window is mapped; we unmap
246        * here, but don't bother remapping -- we will get mapped
247        * by gtk_widget_set_parent ().
248        */
249       if (gtk_widget_get_mapped (widget))
250         gtk_widget_unmap (widget);
251
252       _gtk_window_set_is_toplevel (GTK_WINDOW (plug), FALSE);
253       gtk_container_set_resize_mode (GTK_CONTAINER (plug), GTK_RESIZE_PARENT);
254
255       _gtk_widget_propagate_hierarchy_changed (widget, widget);
256     }
257   else
258     {
259       if (gtk_window_get_focus (GTK_WINDOW (plug)))
260         gtk_window_set_focus (GTK_WINDOW (plug), NULL);
261       if (gtk_window_get_default_widget (GTK_WINDOW (plug)))
262         gtk_window_set_default (GTK_WINDOW (plug), NULL);
263
264       priv->modality_group = gtk_window_group_new ();
265       gtk_window_group_add_window (priv->modality_group, GTK_WINDOW (plug));
266
267       _gtk_window_set_is_toplevel (GTK_WINDOW (plug), TRUE);
268       gtk_container_set_resize_mode (GTK_CONTAINER (plug), GTK_RESIZE_QUEUE);
269
270       _gtk_widget_propagate_hierarchy_changed (GTK_WIDGET (plug), NULL);
271     }
272 }
273
274 /**
275  * gtk_plug_get_id:
276  * @plug: a #GtkPlug.
277  * 
278  * Gets the window ID of a #GtkPlug widget, which can then
279  * be used to embed this window inside another window, for
280  * instance with gtk_socket_add_id().
281  * 
282  * Return value: the window ID for the plug
283  **/
284 GdkNativeWindow
285 gtk_plug_get_id (GtkPlug *plug)
286 {
287   g_return_val_if_fail (GTK_IS_PLUG (plug), 0);
288
289   if (!gtk_widget_get_realized (GTK_WIDGET (plug)))
290     gtk_widget_realize (GTK_WIDGET (plug));
291
292   return _gtk_plug_windowing_get_id (plug);
293 }
294
295 /**
296  * gtk_plug_get_embedded:
297  * @plug: a #GtkPlug
298  *
299  * Determines whether the plug is embedded in a socket.
300  *
301  * Return value: %TRUE if the plug is embedded in a socket
302  *
303  * Since: 2.14
304  **/
305 gboolean
306 gtk_plug_get_embedded (GtkPlug *plug)
307 {
308   g_return_val_if_fail (GTK_IS_PLUG (plug), FALSE);
309
310   return plug->priv->socket_window != NULL;
311 }
312
313 /**
314  * gtk_plug_get_socket_window:
315  * @plug: a #GtkPlug
316  *
317  * Retrieves the socket the plug is embedded in.
318  *
319  * Return value: (transfer none): the window of the socket, or %NULL
320  *
321  * Since: 2.14
322  **/
323 GdkWindow *
324 gtk_plug_get_socket_window (GtkPlug *plug)
325 {
326   g_return_val_if_fail (GTK_IS_PLUG (plug), NULL);
327
328   return plug->priv->socket_window;
329 }
330
331 /**
332  * _gtk_plug_add_to_socket:
333  * @plug: a #GtkPlug
334  * @socket_: a #GtkSocket
335  * 
336  * Adds a plug to a socket within the same application.
337  **/
338 void
339 _gtk_plug_add_to_socket (GtkPlug   *plug,
340                          GtkSocket *socket_)
341 {
342   GtkPlugPrivate *priv;
343   GtkWidget *widget;
344   
345   g_return_if_fail (GTK_IS_PLUG (plug));
346   g_return_if_fail (GTK_IS_SOCKET (socket_));
347   g_return_if_fail (gtk_widget_get_realized (GTK_WIDGET (socket_)));
348
349   priv = plug->priv;
350   widget = GTK_WIDGET (plug);
351
352   gtk_plug_set_is_child (plug, TRUE);
353   priv->same_app = TRUE;
354   socket_->priv->same_app = TRUE;
355   socket_->priv->plug_widget = widget;
356
357   priv->socket_window = gtk_widget_get_window (GTK_WIDGET (socket_));
358   g_object_ref (priv->socket_window);
359   g_signal_emit (plug, plug_signals[EMBEDDED], 0);
360   g_object_notify (G_OBJECT (plug), "embedded");
361
362   if (gtk_widget_get_realized (widget))
363     {
364       GdkWindow *window;
365
366       window = gtk_widget_get_window (widget);
367       gdk_window_reparent (window, priv->socket_window,
368                            -gdk_window_get_width (window),
369                            -gdk_window_get_height (window));
370     }
371
372   gtk_widget_set_parent (widget, GTK_WIDGET (socket_));
373
374   g_signal_emit_by_name (socket_, "plug-added");
375 }
376
377 /**
378  * _gtk_plug_send_delete_event:
379  * @widget: a #GtkWidget
380  *
381  * Send a GDK_DELETE event to the @widget and destroy it if
382  * necessary. Internal GTK function, called from this file or the
383  * backend-specific GtkPlug implementation.
384  */
385 void
386 _gtk_plug_send_delete_event (GtkWidget *widget)
387 {
388   GdkEvent *event = gdk_event_new (GDK_DELETE);
389
390   event->any.window = g_object_ref (gtk_widget_get_window (widget));
391   event->any.send_event = FALSE;
392
393   g_object_ref (widget);
394
395   if (!gtk_widget_event (widget, event))
396     gtk_widget_destroy (widget);
397
398   g_object_unref (widget);
399
400   gdk_event_free (event);
401 }
402
403 /**
404  * _gtk_plug_remove_from_socket:
405  * @plug: a #GtkPlug
406  * @socket_: a #GtkSocket
407  * 
408  * Removes a plug from a socket within the same application.
409  **/
410 void
411 _gtk_plug_remove_from_socket (GtkPlug   *plug,
412                               GtkSocket *socket_)
413 {
414   GtkPlugPrivate *priv;
415   GtkWidget *widget;
416   GdkWindow *window;
417   gboolean result;
418   gboolean widget_was_visible;
419
420   g_return_if_fail (GTK_IS_PLUG (plug));
421   g_return_if_fail (GTK_IS_SOCKET (socket_));
422   g_return_if_fail (gtk_widget_get_realized (GTK_WIDGET (plug)));
423
424   priv = plug->priv;
425   widget = GTK_WIDGET (plug);
426
427   if (_gtk_widget_get_in_reparent (widget))
428     return;
429
430   g_object_ref (plug);
431   g_object_ref (socket_);
432
433   widget_was_visible = gtk_widget_get_visible (widget);
434   window = gtk_widget_get_window (widget);
435
436   gdk_window_hide (window);
437   _gtk_widget_set_in_reparent (widget, TRUE);
438   gdk_window_reparent (window,
439                        gtk_widget_get_root_window (widget),
440                        0, 0);
441   gtk_widget_unparent (GTK_WIDGET (plug));
442   _gtk_widget_set_in_reparent (widget, FALSE);
443   
444   socket_->priv->plug_widget = NULL;
445   if (socket_->priv->plug_window != NULL)
446     {
447       g_object_unref (socket_->priv->plug_window);
448       socket_->priv->plug_window = NULL;
449     }
450   
451   socket_->priv->same_app = FALSE;
452
453   priv->same_app = FALSE;
454   if (priv->socket_window != NULL)
455     {
456       g_object_unref (priv->socket_window);
457       priv->socket_window = NULL;
458     }
459   gtk_plug_set_is_child (plug, FALSE);
460
461   g_signal_emit_by_name (socket_, "plug-removed", &result);
462   if (!result)
463     gtk_widget_destroy (GTK_WIDGET (socket_));
464
465   if (window)
466     _gtk_plug_send_delete_event (widget);
467
468   g_object_unref (plug);
469
470   if (widget_was_visible && gtk_widget_get_visible (GTK_WIDGET (socket_)))
471     gtk_widget_queue_resize (GTK_WIDGET (socket_));
472
473   g_object_unref (socket_);
474 }
475
476 /**
477  * gtk_plug_construct:
478  * @plug: a #GtkPlug.
479  * @socket_id: the XID of the socket's window.
480  *
481  * Finish the initialization of @plug for a given #GtkSocket identified by
482  * @socket_id. This function will generally only be used by classes deriving from #GtkPlug.
483  **/
484 void
485 gtk_plug_construct (GtkPlug         *plug,
486                     GdkNativeWindow  socket_id)
487 {
488   gtk_plug_construct_for_display (plug, gdk_display_get_default (), socket_id);
489 }
490
491 /**
492  * gtk_plug_construct_for_display:
493  * @plug: a #GtkPlug.
494  * @display: the #GdkDisplay associated with @socket_id's 
495  *           #GtkSocket.
496  * @socket_id: the XID of the socket's window.
497  *
498  * Finish the initialization of @plug for a given #GtkSocket identified by
499  * @socket_id which is currently displayed on @display.
500  * This function will generally only be used by classes deriving from #GtkPlug.
501  *
502  * Since: 2.2
503  **/
504 void
505 gtk_plug_construct_for_display (GtkPlug         *plug,
506                                 GdkDisplay      *display,
507                                 GdkNativeWindow  socket_id)
508 {
509   GtkPlugPrivate *priv;
510
511   g_return_if_fail (GTK_IS_PLUG (plug));
512   g_return_if_fail (GDK_IS_DISPLAY (display));
513
514   priv = plug->priv;
515
516   if (socket_id)
517     {
518       gpointer user_data = NULL;
519
520 #ifdef GDK_WINDOWING_X11
521       if (GDK_IS_X11_DISPLAY (display))
522         priv->socket_window = gdk_x11_window_lookup_for_display (display, socket_id);
523       else
524 #endif
525         priv->socket_window = NULL;
526
527       if (priv->socket_window)
528         {
529           gdk_window_get_user_data (priv->socket_window, &user_data);
530
531           if (user_data)
532             {
533               if (GTK_IS_SOCKET (user_data))
534                 _gtk_plug_add_to_socket (plug, user_data);
535               else
536                 {
537                   g_warning (G_STRLOC "Can't create GtkPlug as child of non-GtkSocket");
538                   priv->socket_window = NULL;
539                 }
540             }
541           else
542             g_object_ref (priv->socket_window);
543         }
544       else
545 #ifdef GDK_WINDOWING_X11
546       if (GDK_IS_X11_DISPLAY (display))
547         priv->socket_window = gdk_x11_window_foreign_new_for_display (display, socket_id);
548 #endif
549
550       if (priv->socket_window) {
551         g_signal_emit (plug, plug_signals[EMBEDDED], 0);
552
553         g_object_notify (G_OBJECT (plug), "embedded");
554       }
555     }
556 }
557
558 /**
559  * gtk_plug_new:
560  * @socket_id:  the window ID of the socket, or 0.
561  * 
562  * Creates a new plug widget inside the #GtkSocket identified
563  * by @socket_id. If @socket_id is 0, the plug is left "unplugged" and
564  * can later be plugged into a #GtkSocket by  gtk_socket_add_id().
565  * 
566  * Return value: the new #GtkPlug widget.
567  **/
568 GtkWidget*
569 gtk_plug_new (GdkNativeWindow socket_id)
570 {
571   return gtk_plug_new_for_display (gdk_display_get_default (), socket_id);
572 }
573
574 /**
575  * gtk_plug_new_for_display:
576  * @display: the #GdkDisplay on which @socket_id is displayed
577  * @socket_id: the XID of the socket's window.
578  * 
579  * Create a new plug widget inside the #GtkSocket identified by socket_id.
580  *
581  * Return value: the new #GtkPlug widget.
582  *
583  * Since: 2.2
584  */
585 GtkWidget*
586 gtk_plug_new_for_display (GdkDisplay      *display,
587                           GdkNativeWindow  socket_id)
588 {
589   GtkPlug *plug;
590
591   g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
592
593   plug = g_object_new (GTK_TYPE_PLUG, NULL);
594   gtk_plug_construct_for_display (plug, display, socket_id);
595   return GTK_WIDGET (plug);
596 }
597
598 static void
599 gtk_plug_finalize (GObject *object)
600 {
601   GtkPlug *plug = GTK_PLUG (object);
602   GtkPlugPrivate *priv = plug->priv;
603
604   if (priv->grabbed_keys)
605     {
606       g_hash_table_destroy (priv->grabbed_keys);
607       priv->grabbed_keys = NULL;
608     }
609   
610   G_OBJECT_CLASS (gtk_plug_parent_class)->finalize (object);
611 }
612
613 static void
614 gtk_plug_unrealize (GtkWidget *widget)
615 {
616   GtkPlug *plug = GTK_PLUG (widget);
617   GtkPlugPrivate *priv = plug->priv;
618
619   if (priv->socket_window != NULL)
620     {
621       gdk_window_set_user_data (priv->socket_window, NULL);
622       g_object_unref (priv->socket_window);
623       priv->socket_window = NULL;
624
625       g_object_notify (G_OBJECT (widget), "embedded");
626     }
627
628   if (!priv->same_app)
629     {
630       if (priv->modality_window)
631         _gtk_plug_handle_modality_off (plug);
632
633       gtk_window_group_remove_window (priv->modality_group, GTK_WINDOW (plug));
634       g_object_unref (priv->modality_group);
635     }
636
637   GTK_WIDGET_CLASS (gtk_plug_parent_class)->unrealize (widget);
638 }
639
640 static void
641 gtk_plug_realize (GtkWidget *widget)
642 {
643   GtkAllocation allocation;
644   GtkPlug *plug = GTK_PLUG (widget);
645   GtkPlugPrivate *priv = plug->priv;
646   GtkWindow *window = GTK_WINDOW (widget);
647   GdkWindow *gdk_window;
648   GdkWindowAttr attributes;
649   const gchar *title;
650   gchar *wmclass_name, *wmclass_class;
651   gint attributes_mask;
652
653   gtk_widget_set_realized (widget, TRUE);
654
655   title = gtk_window_get_title (window);
656   _gtk_window_get_wmclass (window, &wmclass_name, &wmclass_class);
657   gtk_widget_get_allocation (widget, &allocation);
658
659   attributes.window_type = GDK_WINDOW_CHILD;    /* XXX GDK_WINDOW_PLUG ? */
660   attributes.title = (gchar *) title;
661   attributes.wmclass_name = wmclass_name;
662   attributes.wmclass_class = wmclass_class;
663   attributes.width = allocation.width;
664   attributes.height = allocation.height;
665   attributes.wclass = GDK_INPUT_OUTPUT;
666
667   /* this isn't right - we should match our parent's visual/colormap.
668    * though that will require handling "foreign" colormaps */
669   attributes.visual = gtk_widget_get_visual (widget);
670   attributes.event_mask = gtk_widget_get_events (widget);
671   attributes.event_mask |= (GDK_EXPOSURE_MASK |
672                             GDK_KEY_PRESS_MASK |
673                             GDK_KEY_RELEASE_MASK |
674                             GDK_ENTER_NOTIFY_MASK |
675                             GDK_LEAVE_NOTIFY_MASK |
676                             GDK_STRUCTURE_MASK);
677
678   attributes_mask = GDK_WA_VISUAL;
679   attributes_mask |= (title ? GDK_WA_TITLE : 0);
680   attributes_mask |= (wmclass_name ? GDK_WA_WMCLASS : 0);
681
682   if (gtk_widget_is_toplevel (widget))
683     {
684       attributes.window_type = GDK_WINDOW_TOPLEVEL;
685
686       gdk_error_trap_push ();
687       if (priv->socket_window)
688         gdk_window = gdk_window_new (priv->socket_window,
689                                      &attributes, attributes_mask);
690       else /* If it's a passive plug, we use the root window */
691         gdk_window = gdk_window_new (gtk_widget_get_root_window (widget),
692                                      &attributes, attributes_mask);
693       gtk_widget_set_window (widget, gdk_window);
694
695       gdk_display_sync (gtk_widget_get_display (widget));
696       if (gdk_error_trap_pop ()) /* Uh-oh */
697         {
698           gdk_error_trap_push ();
699           gdk_window_destroy (gdk_window);
700           gdk_error_trap_pop_ignored ();
701           gdk_window = gdk_window_new (gtk_widget_get_root_window (widget),
702                                    &attributes, attributes_mask);
703           gtk_widget_set_window (widget, gdk_window);
704         }
705
706       gdk_window_add_filter (gdk_window,
707                              _gtk_plug_windowing_filter_func,
708                              widget);
709
710       priv->modality_group = gtk_window_group_new ();
711       gtk_window_group_add_window (priv->modality_group, window);
712
713       _gtk_plug_windowing_realize_toplevel (plug);
714     }
715   else
716     {
717       gdk_window = gdk_window_new (gtk_widget_get_parent_window (widget),
718                                    &attributes, attributes_mask);
719       gtk_widget_set_window (widget, gdk_window);
720     }
721
722   gdk_window_set_user_data (gdk_window, window);
723
724   gtk_widget_style_attach (widget);
725   gtk_style_set_background (gtk_widget_get_style (widget),
726                             gdk_window, GTK_STATE_NORMAL);
727
728   gdk_window_enable_synchronized_configure (gdk_window);
729 }
730
731 static void
732 gtk_plug_show (GtkWidget *widget)
733 {
734   if (gtk_widget_is_toplevel (widget))
735     GTK_WIDGET_CLASS (gtk_plug_parent_class)->show (widget);
736   else
737     GTK_WIDGET_CLASS (bin_class)->show (widget);
738 }
739
740 static void
741 gtk_plug_hide (GtkWidget *widget)
742 {
743   if (gtk_widget_is_toplevel (widget))
744     GTK_WIDGET_CLASS (gtk_plug_parent_class)->hide (widget);
745   else
746     GTK_WIDGET_CLASS (bin_class)->hide (widget);
747 }
748
749 /* From gdkinternals.h */
750 void gdk_synthesize_window_state (GdkWindow     *window,
751                                   GdkWindowState unset_flags,
752                                   GdkWindowState set_flags);
753
754 static void
755 gtk_plug_map (GtkWidget *widget)
756 {
757   if (gtk_widget_is_toplevel (widget))
758     {
759       GtkBin *bin = GTK_BIN (widget);
760       GtkPlug *plug = GTK_PLUG (widget);
761       GtkWidget *child;
762       
763       gtk_widget_set_mapped (widget, TRUE);
764
765       child = gtk_bin_get_child (bin);
766       if (child != NULL &&
767           gtk_widget_get_visible (child) &&
768           !gtk_widget_get_mapped (child))
769         gtk_widget_map (child);
770
771       _gtk_plug_windowing_map_toplevel (plug);
772
773       gdk_synthesize_window_state (gtk_widget_get_window (widget),
774                                    GDK_WINDOW_STATE_WITHDRAWN,
775                                    0);
776     }
777   else
778     GTK_WIDGET_CLASS (bin_class)->map (widget);
779 }
780
781 static void
782 gtk_plug_unmap (GtkWidget *widget)
783 {
784   if (gtk_widget_is_toplevel (widget))
785     {
786       GtkPlug *plug = GTK_PLUG (widget);
787       GdkWindow *window;
788
789       window = gtk_widget_get_window (widget);
790
791       gtk_widget_set_mapped (widget, FALSE);
792
793       gdk_window_hide (window);
794
795       _gtk_plug_windowing_unmap_toplevel (plug);
796
797       gdk_synthesize_window_state (window,
798                                    0,
799                                    GDK_WINDOW_STATE_WITHDRAWN);
800     }
801   else
802     GTK_WIDGET_CLASS (bin_class)->unmap (widget);
803 }
804
805 static void
806 gtk_plug_size_allocate (GtkWidget     *widget,
807                         GtkAllocation *allocation)
808 {
809   GtkWidget *child;
810
811   if (gtk_widget_is_toplevel (widget))
812     GTK_WIDGET_CLASS (gtk_plug_parent_class)->size_allocate (widget, allocation);
813   else
814     {
815       GtkBin *bin = GTK_BIN (widget);
816
817       gtk_widget_set_allocation (widget, allocation);
818
819       if (gtk_widget_get_realized (widget))
820         gdk_window_move_resize (gtk_widget_get_window (widget),
821                                 allocation->x, allocation->y,
822                                 allocation->width, allocation->height);
823
824       child = gtk_bin_get_child (bin);
825
826       if (child != NULL && gtk_widget_get_visible (child))
827         {
828           GtkAllocation child_allocation;
829           
830           child_allocation.x = child_allocation.y = gtk_container_get_border_width (GTK_CONTAINER (widget));
831           child_allocation.width =
832             MAX (1, (gint)allocation->width - child_allocation.x * 2);
833           child_allocation.height =
834             MAX (1, (gint)allocation->height - child_allocation.y * 2);
835           
836           gtk_widget_size_allocate (child, &child_allocation);
837         }
838       
839     }
840 }
841
842 static gboolean
843 gtk_plug_key_press_event (GtkWidget   *widget,
844                           GdkEventKey *event)
845 {
846   if (gtk_widget_is_toplevel (widget))
847     return GTK_WIDGET_CLASS (gtk_plug_parent_class)->key_press_event (widget, event);
848   else
849     return FALSE;
850 }
851
852 static gboolean
853 gtk_plug_focus_event (GtkWidget      *widget,
854                       GdkEventFocus  *event)
855 {
856   /* We eat focus-in events and focus-out events, since they
857    * can be generated by something like a keyboard grab on
858    * a child of the plug.
859    */
860   return FALSE;
861 }
862
863 static void
864 gtk_plug_set_focus (GtkWindow *window,
865                     GtkWidget *focus)
866 {
867   GtkPlug *plug = GTK_PLUG (window);
868
869   GTK_WINDOW_CLASS (gtk_plug_parent_class)->set_focus (window, focus);
870   
871   /* Ask for focus from embedder
872    */
873
874   if (focus && !gtk_window_has_toplevel_focus (window))
875     _gtk_plug_windowing_set_focus (plug);
876 }
877
878 static guint
879 grabbed_key_hash (gconstpointer a)
880 {
881   const GrabbedKey *key = a;
882   guint h;
883   
884   h = key->accelerator_key << 16;
885   h ^= key->accelerator_key >> 16;
886   h ^= key->accelerator_mods;
887
888   return h;
889 }
890
891 static gboolean
892 grabbed_key_equal (gconstpointer a, gconstpointer b)
893 {
894   const GrabbedKey *keya = a;
895   const GrabbedKey *keyb = b;
896
897   return (keya->accelerator_key == keyb->accelerator_key &&
898           keya->accelerator_mods == keyb->accelerator_mods);
899 }
900
901 static void
902 add_grabbed_key (gpointer key, gpointer val, gpointer data)
903 {
904   GrabbedKey *grabbed_key = key;
905   GtkPlug *plug = data;
906   GtkPlugPrivate *priv = plug->priv;
907
908   if (!priv->grabbed_keys ||
909       !g_hash_table_lookup (priv->grabbed_keys, grabbed_key))
910     {
911       _gtk_plug_windowing_add_grabbed_key (plug,
912                                            grabbed_key->accelerator_key,
913                                            grabbed_key->accelerator_mods);
914     }
915 }
916
917 static void
918 add_grabbed_key_always (gpointer key,
919                         gpointer val,
920                         gpointer data)
921 {
922   GrabbedKey *grabbed_key = key;
923   GtkPlug *plug = data;
924
925   _gtk_plug_windowing_add_grabbed_key (plug,
926                                        grabbed_key->accelerator_key,
927                                        grabbed_key->accelerator_mods);
928 }
929
930 /**
931  * _gtk_plug_add_all_grabbed_keys:
932  *
933  * @plug: a #GtkPlug
934  *
935  * Calls _gtk_plug_windowing_add_grabbed_key() on all the grabbed keys
936  * in the @plug.
937  */
938 void
939 _gtk_plug_add_all_grabbed_keys (GtkPlug *plug)
940 {
941   GtkPlugPrivate *priv = plug->priv;
942
943   if (priv->grabbed_keys)
944     g_hash_table_foreach (priv->grabbed_keys, add_grabbed_key_always, plug);
945 }
946
947 static void
948 remove_grabbed_key (gpointer key, gpointer val, gpointer data)
949 {
950   GrabbedKey *grabbed_key = key;
951   GtkPlug *plug = data;
952   GtkPlugPrivate *priv = plug->priv;
953
954   if (!priv->grabbed_keys ||
955       !g_hash_table_lookup (priv->grabbed_keys, grabbed_key))
956     {
957       _gtk_plug_windowing_remove_grabbed_key (plug, 
958                                               grabbed_key->accelerator_key,
959                                               grabbed_key->accelerator_mods);
960     }
961 }
962
963 static void
964 keys_foreach (GtkWindow      *window,
965               guint           keyval,
966               GdkModifierType modifiers,
967               gboolean        is_mnemonic,
968               gpointer        data)
969 {
970   GHashTable *new_grabbed_keys = data;
971   GrabbedKey *key = g_slice_new (GrabbedKey);
972
973   key->accelerator_key = keyval;
974   key->accelerator_mods = modifiers;
975   
976   g_hash_table_replace (new_grabbed_keys, key, key);
977 }
978
979 static void
980 grabbed_key_free (gpointer data)
981 {
982   g_slice_free (GrabbedKey, data);
983 }
984
985 static void
986 gtk_plug_keys_changed (GtkWindow *window)
987 {
988   GHashTable *new_grabbed_keys, *old_grabbed_keys;
989   GtkPlug *plug = GTK_PLUG (window);
990   GtkPlugPrivate *priv = plug->priv;
991
992   new_grabbed_keys = g_hash_table_new_full (grabbed_key_hash, grabbed_key_equal, (GDestroyNotify)grabbed_key_free, NULL);
993   _gtk_window_keys_foreach (window, keys_foreach, new_grabbed_keys);
994
995   if (priv->socket_window)
996     g_hash_table_foreach (new_grabbed_keys, add_grabbed_key, plug);
997
998   old_grabbed_keys = priv->grabbed_keys;
999   priv->grabbed_keys = new_grabbed_keys;
1000
1001   if (old_grabbed_keys)
1002     {
1003       if (priv->socket_window)
1004         g_hash_table_foreach (old_grabbed_keys, remove_grabbed_key, plug);
1005       g_hash_table_destroy (old_grabbed_keys);
1006     }
1007 }
1008
1009 static gboolean
1010 gtk_plug_focus (GtkWidget        *widget,
1011                 GtkDirectionType  direction)
1012 {
1013   GtkBin *bin = GTK_BIN (widget);
1014   GtkPlug *plug = GTK_PLUG (widget);
1015   GtkWindow *window = GTK_WINDOW (widget);
1016   GtkContainer *container = GTK_CONTAINER (widget);
1017   GtkWidget *child;
1018   GtkWidget *old_focus_child;
1019   GtkWidget *parent;
1020
1021   old_focus_child = gtk_container_get_focus_child (container);
1022   /* We override GtkWindow's behavior, since we don't want wrapping here.
1023    */
1024   if (old_focus_child)
1025     {
1026       GtkWidget *focus_widget;
1027
1028       if (gtk_widget_child_focus (old_focus_child, direction))
1029         return TRUE;
1030
1031       focus_widget = gtk_window_get_focus (window);
1032       if (focus_widget)
1033         {
1034           /* Wrapped off the end, clear the focus setting for the toplevel */
1035           parent = gtk_widget_get_parent (focus_widget);
1036           while (parent)
1037             {
1038               gtk_container_set_focus_child (GTK_CONTAINER (parent), NULL);
1039               parent = gtk_widget_get_parent (parent);
1040             }
1041           
1042           gtk_window_set_focus (GTK_WINDOW (container), NULL);
1043         }
1044     }
1045   else
1046     {
1047       /* Try to focus the first widget in the window */
1048       child = gtk_bin_get_child (bin);
1049       if (child && gtk_widget_child_focus (child, direction))
1050         return TRUE;
1051     }
1052
1053   if (!gtk_container_get_focus_child (GTK_CONTAINER (window)))
1054     _gtk_plug_windowing_focus_to_parent (plug, direction);
1055
1056   return FALSE;
1057 }
1058
1059 static void
1060 gtk_plug_check_resize (GtkContainer *container)
1061 {
1062   if (gtk_widget_is_toplevel (GTK_WIDGET (container)))
1063     GTK_CONTAINER_CLASS (gtk_plug_parent_class)->check_resize (container);
1064   else
1065     GTK_CONTAINER_CLASS (bin_class)->check_resize (container);
1066 }
1067
1068 /**
1069  * _gtk_plug_handle_modality_on:
1070  *
1071  * @plug: a #GtkPlug
1072  *
1073  * Called from the GtkPlug backend when the corresponding socket has
1074  * told the plug that it modality has toggled on.
1075  */
1076 void
1077 _gtk_plug_handle_modality_on (GtkPlug *plug)
1078 {
1079   GtkPlugPrivate *priv = plug->priv;
1080
1081   if (!priv->modality_window)
1082     {
1083       priv->modality_window = gtk_window_new (GTK_WINDOW_POPUP);
1084       gtk_window_set_screen (GTK_WINDOW (priv->modality_window),
1085                              gtk_widget_get_screen (GTK_WIDGET (plug)));
1086       gtk_widget_realize (priv->modality_window);
1087       gtk_window_group_add_window (priv->modality_group, GTK_WINDOW (priv->modality_window));
1088       gtk_grab_add (priv->modality_window);
1089     }
1090 }
1091
1092 /**
1093  * _gtk_plug_handle_modality_off:
1094  *
1095  * @plug: a #GtkPlug
1096  *
1097  * Called from the GtkPlug backend when the corresponding socket has
1098  * told the plug that it modality has toggled off.
1099  */
1100 void
1101 _gtk_plug_handle_modality_off (GtkPlug *plug)
1102 {
1103   GtkPlugPrivate *priv = plug->priv;
1104
1105   if (priv->modality_window)
1106     {
1107       gtk_widget_destroy (priv->modality_window);
1108       priv->modality_window = NULL;
1109     }
1110 }
1111
1112 /**
1113  * _gtk_plug_focus_first_last:
1114  *
1115  * @plug: a #GtkPlug
1116  * @direction: a direction
1117  *
1118  * Called from the GtkPlug backend when the corresponding socket has
1119  * told the plug that it has received the focus.
1120  */
1121 void
1122 _gtk_plug_focus_first_last (GtkPlug          *plug,
1123                             GtkDirectionType  direction)
1124 {
1125   GtkWindow *window = GTK_WINDOW (plug);
1126   GtkWidget *focus_widget;
1127   GtkWidget *parent;
1128
1129   focus_widget = gtk_window_get_focus (window);
1130   if (focus_widget)
1131     {
1132       parent = gtk_widget_get_parent (focus_widget);
1133       while (parent)
1134         {
1135           gtk_container_set_focus_child (GTK_CONTAINER (parent), NULL);
1136           parent = gtk_widget_get_parent (parent);
1137         }
1138       
1139       gtk_window_set_focus (GTK_WINDOW (plug), NULL);
1140     }
1141
1142   gtk_widget_child_focus (GTK_WIDGET (plug), direction);
1143 }