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