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