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