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