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