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