]> Pileus Git - ~andy/gtk/blob - gtk/gtkplug.c
Document 2.2 API additions.
[~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 "gtkmain.h"
29 #include "gtkmarshalers.h"
30 #include "gtkplug.h"
31 #include "gtkprivate.h"
32
33 #include "gdk/gdkkeysyms.h"
34 #include "x11/gdkx.h"
35
36 #include "xembed.h"
37
38 static void            gtk_plug_class_init            (GtkPlugClass     *klass);
39 static void            gtk_plug_init                  (GtkPlug          *plug);
40 static void            gtk_plug_finalize              (GObject          *object);
41 static void            gtk_plug_realize               (GtkWidget        *widget);
42 static void            gtk_plug_unrealize             (GtkWidget        *widget);
43 static void            gtk_plug_show                  (GtkWidget        *widget);
44 static void            gtk_plug_hide                  (GtkWidget        *widget);
45 static void            gtk_plug_map                   (GtkWidget        *widget);
46 static void            gtk_plug_unmap                 (GtkWidget        *widget);
47 static void            gtk_plug_size_allocate         (GtkWidget        *widget,
48                                                        GtkAllocation    *allocation);
49 static gboolean        gtk_plug_key_press_event       (GtkWidget        *widget,
50                                                        GdkEventKey      *event);
51 static gboolean        gtk_plug_focus_event           (GtkWidget        *widget,
52                                                        GdkEventFocus    *event);
53 static void            gtk_plug_set_focus             (GtkWindow        *window,
54                                                        GtkWidget        *focus);
55 static gboolean        gtk_plug_focus                 (GtkWidget        *widget,
56                                                        GtkDirectionType  direction);
57 static void            gtk_plug_check_resize          (GtkContainer     *container);
58 static void            gtk_plug_keys_changed          (GtkWindow        *window);
59 static GdkFilterReturn gtk_plug_filter_func           (GdkXEvent        *gdk_xevent,
60                                                        GdkEvent         *event,
61                                                        gpointer          data);
62
63 static void handle_modality_off        (GtkPlug       *plug);
64 static void send_xembed_message        (GtkPlug       *plug,
65                                         glong          message,
66                                         glong          detail,
67                                         glong          data1,
68                                         glong          data2,
69                                         guint32        time);
70 static void xembed_set_info            (GdkWindow     *window,
71                                         unsigned long  flags);
72
73 /* From Tk */
74 #define EMBEDDED_APP_WANTS_FOCUS NotifyNormal+20
75   
76 static GtkWindowClass *parent_class = NULL;
77 static GtkBinClass *bin_class = NULL;
78
79 enum {
80   EMBEDDED,
81   LAST_SIGNAL
82 }; 
83
84 static guint plug_signals[LAST_SIGNAL] = { 0 };
85
86 GType
87 gtk_plug_get_type ()
88 {
89   static GType plug_type = 0;
90
91   if (!plug_type)
92     {
93       static const GTypeInfo plug_info =
94       {
95         sizeof (GtkPlugClass),
96         NULL,           /* base_init */
97         NULL,           /* base_finalize */
98         (GClassInitFunc) gtk_plug_class_init,
99         NULL,           /* class_finalize */
100         NULL,           /* class_data */
101         sizeof (GtkPlug),
102         16,             /* n_preallocs */
103         (GInstanceInitFunc) gtk_plug_init,
104       };
105
106       plug_type = g_type_register_static (GTK_TYPE_WINDOW, "GtkPlug",
107                                           &plug_info, 0);
108     }
109
110   return plug_type;
111 }
112
113 static void
114 gtk_plug_class_init (GtkPlugClass *class)
115 {
116   GObjectClass *gobject_class = (GObjectClass *)class;
117   GtkWidgetClass *widget_class = (GtkWidgetClass *)class;
118   GtkWindowClass *window_class = (GtkWindowClass *)class;
119   GtkContainerClass *container_class = (GtkContainerClass *)class;
120
121   parent_class = g_type_class_peek_parent (class);
122   bin_class = g_type_class_peek (GTK_TYPE_BIN);
123
124   gobject_class->finalize = gtk_plug_finalize;
125   
126   widget_class->realize = gtk_plug_realize;
127   widget_class->unrealize = gtk_plug_unrealize;
128   widget_class->key_press_event = gtk_plug_key_press_event;
129   widget_class->focus_in_event = gtk_plug_focus_event;
130   widget_class->focus_out_event = gtk_plug_focus_event;
131
132   widget_class->show = gtk_plug_show;
133   widget_class->hide = gtk_plug_hide;
134   widget_class->map = gtk_plug_map;
135   widget_class->unmap = gtk_plug_unmap;
136   widget_class->size_allocate = gtk_plug_size_allocate;
137
138   widget_class->focus = gtk_plug_focus;
139
140   container_class->check_resize = gtk_plug_check_resize;
141
142   window_class->set_focus = gtk_plug_set_focus;
143   window_class->keys_changed = gtk_plug_keys_changed;
144
145   plug_signals[EMBEDDED] =
146     g_signal_new ("embedded",
147                   G_OBJECT_CLASS_TYPE (class),
148                   G_SIGNAL_RUN_LAST,
149                   G_STRUCT_OFFSET (GtkPlugClass, embedded),
150                   NULL, NULL,
151                   _gtk_marshal_VOID__VOID,
152                   G_TYPE_NONE, 0);
153 }
154
155 static void
156 gtk_plug_init (GtkPlug *plug)
157 {
158   GtkWindow *window;
159
160   window = GTK_WINDOW (plug);
161
162   window->type = GTK_WINDOW_TOPLEVEL;
163 }
164
165 static void
166 gtk_plug_set_is_child (GtkPlug  *plug,
167                        gboolean  is_child)
168 {
169   g_assert (!GTK_WIDGET (plug)->parent);
170       
171   if (is_child)
172     {
173       if (plug->modality_window)
174         handle_modality_off (plug);
175
176       if (plug->modality_group)
177         {
178           gtk_window_group_remove_window (plug->modality_group, GTK_WINDOW (plug));
179           g_object_unref (plug->modality_group);
180           plug->modality_group = NULL;
181         }
182       
183       /* As a toplevel, the MAPPED flag doesn't correspond
184        * to whether the widget->window is mapped; we unmap
185        * here, but don't bother remapping -- we will get mapped
186        * by gtk_widget_set_parent ().
187        */
188       if (GTK_WIDGET_MAPPED (plug))
189         gtk_widget_unmap (GTK_WIDGET (plug));
190       
191       GTK_WIDGET_UNSET_FLAGS (plug, GTK_TOPLEVEL);
192       gtk_container_set_resize_mode (GTK_CONTAINER (plug), GTK_RESIZE_PARENT);
193
194       _gtk_widget_propagate_hierarchy_changed (GTK_WIDGET (plug), GTK_WIDGET (plug));
195     }
196   else
197     {
198       if (GTK_WINDOW (plug)->focus_widget)
199         gtk_window_set_focus (GTK_WINDOW (plug), NULL);
200       if (GTK_WINDOW (plug)->default_widget)
201         gtk_window_set_default (GTK_WINDOW (plug), NULL);
202           
203       plug->modality_group = gtk_window_group_new ();
204       gtk_window_group_add_window (plug->modality_group, GTK_WINDOW (plug));
205       
206       GTK_WIDGET_SET_FLAGS (plug, GTK_TOPLEVEL);
207       gtk_container_set_resize_mode (GTK_CONTAINER (plug), GTK_RESIZE_QUEUE);
208
209       _gtk_widget_propagate_hierarchy_changed (GTK_WIDGET (plug), NULL);
210     }
211 }
212
213 /**
214  * _gtk_plug_add_to_socket:
215  * @plug: a #GtkPlug
216  * @socket_: a #GtkSocket
217  * 
218  * Adds a plug to a socket within the same application.
219  **/
220 void
221 _gtk_plug_add_to_socket (GtkPlug   *plug,
222                          GtkSocket *socket)
223 {
224   GtkWidget *widget;
225   gint w, h;
226   
227   g_return_if_fail (GTK_IS_PLUG (plug));
228   g_return_if_fail (GTK_IS_SOCKET (socket));
229   g_return_if_fail (GTK_WIDGET_REALIZED (socket));
230
231   widget = GTK_WIDGET (plug);
232
233   gtk_plug_set_is_child (plug, TRUE);
234   plug->same_app = TRUE;
235   socket->same_app = TRUE;
236   socket->plug_widget = widget;
237
238   plug->socket_window = GTK_WIDGET (socket)->window;
239
240   if (GTK_WIDGET_REALIZED (widget))
241     {
242       gdk_drawable_get_size (GDK_DRAWABLE (widget->window), &w, &h);
243       gdk_window_reparent (widget->window, plug->socket_window, -w, -h);
244     }
245
246   gtk_widget_set_parent (widget, GTK_WIDGET (socket));
247
248   g_signal_emit_by_name (socket, "plug_added", 0);
249 }
250
251 static void
252 send_delete_event (GtkWidget *widget)
253 {
254   GdkEvent *event = gdk_event_new (GDK_DELETE);
255   
256   event->any.window = g_object_ref (widget->window);
257   event->any.send_event = FALSE;
258
259   gtk_widget_ref (widget);
260   
261   if (!gtk_widget_event (widget, event))
262     gtk_widget_destroy (widget);
263   
264   gtk_widget_unref (widget);
265
266   gdk_event_free (event);
267 }
268
269 /**
270  * _gtk_plug_remove_from_socket:
271  * @plug: a #GtkPlug
272  * @socket_: a #GtkSocket
273  * 
274  * Removes a plug from a socket within the same application.
275  **/
276 void
277 _gtk_plug_remove_from_socket (GtkPlug   *plug,
278                               GtkSocket *socket)
279 {
280   GtkWidget *widget;
281   gboolean result;
282   gboolean widget_was_visible;
283
284   g_return_if_fail (GTK_IS_PLUG (plug));
285   g_return_if_fail (GTK_IS_SOCKET (socket));
286   g_return_if_fail (GTK_WIDGET_REALIZED (plug));
287
288   widget = GTK_WIDGET (plug);
289
290   g_object_ref (plug);
291   g_object_ref (socket);
292
293   widget_was_visible = GTK_WIDGET_VISIBLE (plug);
294   
295   gdk_window_hide (widget->window);
296   gdk_window_reparent (widget->window,
297                        gtk_widget_get_root_window (widget),
298                        0, 0);
299
300   GTK_PRIVATE_SET_FLAG (plug, GTK_IN_REPARENT);
301   gtk_widget_unparent (GTK_WIDGET (plug));
302   GTK_PRIVATE_UNSET_FLAG (plug, GTK_IN_REPARENT);
303   
304   socket->plug_widget = NULL;
305   g_object_unref (socket->plug_window);
306   socket->plug_window = NULL;
307   
308   socket->same_app = FALSE;
309
310   plug->same_app = FALSE;
311   plug->socket_window = NULL;
312
313   gtk_plug_set_is_child (plug, FALSE);
314                     
315   g_signal_emit_by_name (socket, "plug_removed", &result);
316   if (!result)
317     gtk_widget_destroy (GTK_WIDGET (socket));
318
319   send_delete_event (widget);
320
321   g_object_unref (plug);
322
323   if (widget_was_visible && GTK_WIDGET_VISIBLE (socket))
324     gtk_widget_queue_resize (GTK_WIDGET (socket));
325
326   g_object_unref (socket);
327 }
328
329 /**
330  * gtk_plug_construct:
331  * @plug: a #GtkPlug.
332  * @socket_id: the XID of the socket's window.
333  *
334  * Finish the initialization of @plug for a given #GtkSocket identified by
335  * @socket_id. This function will generally only be used by classes deriving from #GtkPlug.
336  **/
337 void
338 gtk_plug_construct (GtkPlug         *plug,
339                     GdkNativeWindow  socket_id)
340 {
341   gtk_plug_construct_for_display (plug, gdk_display_get_default (), socket_id);
342 }
343
344 /**
345  * gtk_plug_construct_for_display:
346  * @plug: a #GtkPlug.
347  * @display: the #GdkDisplay associated with @socket_id's 
348  *           #GtkSocket.
349  * @socket_id: the XID of the socket's window.
350  *
351  * Finish the initialization of @plug for a given #GtkSocket identified by
352  * @socket_id which is currently displayed on @display.
353  * This function will generally only be used by classes deriving from #GtkPlug.
354  *
355  * Since: 2.2
356  **/
357 void
358 gtk_plug_construct_for_display (GtkPlug         *plug,
359                                 GdkDisplay      *display,
360                                 GdkNativeWindow  socket_id)
361 {
362   if (socket_id)
363     {
364       gpointer user_data = NULL;
365
366       plug->socket_window = gdk_window_lookup_for_display (display, socket_id);
367       
368       if (plug->socket_window)
369         gdk_window_get_user_data (plug->socket_window, &user_data);
370       else
371         plug->socket_window = gdk_window_foreign_new_for_display (display, socket_id);
372           
373       if (user_data)
374         {
375           if (GTK_IS_SOCKET (user_data))
376             _gtk_plug_add_to_socket (plug, user_data);
377           else
378             {
379               g_warning (G_STRLOC "Can't create GtkPlug as child of non-GtkSocket");
380               plug->socket_window = NULL;
381             }
382         }
383
384       if (plug->socket_window)
385         g_signal_emit (plug, plug_signals[EMBEDDED], 0);
386     }
387 }
388
389 /**
390  * gtk_plug_new:
391  * @socket_id:  the window ID of the socket, or 0.
392  * 
393  * Creates a new plug widget inside the #GtkSocket identified
394  * by @socket_id. If @socket_id is 0, the plug is left "unplugged" and
395  * can later be plugged into a #GtkSocket by  gtk_socket_add_id().
396  * 
397  * Return value: the new #GtkPlug widget.
398  **/
399 GtkWidget*
400 gtk_plug_new (GdkNativeWindow socket_id)
401 {
402   return gtk_plug_new_for_display (gdk_display_get_default (), socket_id);
403 }
404
405 /**
406  * gtk_plug_new_for_display:
407  * @display : the #GdkDisplay on which @socket_id is displayed
408  * @socket_id: the XID of the socket's window.
409  * 
410  * Create a new plug widget inside the #GtkSocket identified by socket_id.
411  *
412  * Return value: the new #GtkPlug widget.
413  *
414  * Since: 2.2
415  */
416 GtkWidget*
417 gtk_plug_new_for_display (GdkDisplay      *display,
418                           GdkNativeWindow  socket_id)
419 {
420   GtkPlug *plug;
421
422   plug = g_object_new (GTK_TYPE_PLUG, NULL);
423   gtk_plug_construct_for_display (plug, display, socket_id);
424   return GTK_WIDGET (plug);
425 }
426
427 /**
428  * gtk_plug_get_id:
429  * @plug: a #GtkPlug.
430  * 
431  * Gets the window ID of a #GtkPlug widget, which can then
432  * be used to embed this window inside another window, for
433  * instance with gtk_socket_add_id().
434  * 
435  * Return value: the window ID for the plug
436  **/
437 GdkNativeWindow
438 gtk_plug_get_id (GtkPlug *plug)
439 {
440   g_return_val_if_fail (GTK_IS_PLUG (plug), 0);
441
442   if (!GTK_WIDGET_REALIZED (plug))
443     gtk_widget_realize (GTK_WIDGET (plug));
444
445   return GDK_WINDOW_XWINDOW (GTK_WIDGET (plug)->window);
446 }
447
448 static void
449 gtk_plug_finalize (GObject *object)
450 {
451   GtkPlug *plug = GTK_PLUG (object);
452
453   if (plug->grabbed_keys)
454     {
455       g_hash_table_destroy (plug->grabbed_keys);
456       plug->grabbed_keys = NULL;
457     }
458   
459   G_OBJECT_CLASS (parent_class)->finalize (object);
460 }
461
462 static void
463 gtk_plug_unrealize (GtkWidget *widget)
464 {
465   GtkPlug *plug;
466
467   g_return_if_fail (GTK_IS_PLUG (widget));
468
469   plug = GTK_PLUG (widget);
470
471   if (plug->socket_window != NULL)
472     {
473       gdk_window_set_user_data (plug->socket_window, NULL);
474       g_object_unref (plug->socket_window);
475       plug->socket_window = NULL;
476     }
477
478   if (!plug->same_app)
479     {
480       if (plug->modality_window)
481         handle_modality_off (plug);
482
483       gtk_window_group_remove_window (plug->modality_group, GTK_WINDOW (plug));
484       g_object_unref (plug->modality_group);
485     }
486   
487   if (GTK_WIDGET_CLASS (parent_class)->unrealize)
488     (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
489 }
490
491 static void
492 gtk_plug_realize (GtkWidget *widget)
493 {
494   GtkWindow *window;
495   GtkPlug *plug;
496   GdkWindowAttr attributes;
497   gint attributes_mask;
498
499   g_return_if_fail (GTK_IS_PLUG (widget));
500
501   GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
502   window = GTK_WINDOW (widget);
503   plug = GTK_PLUG (widget);
504
505   attributes.window_type = GDK_WINDOW_CHILD;    /* XXX GDK_WINDOW_PLUG ? */
506   attributes.title = window->title;
507   attributes.wmclass_name = window->wmclass_name;
508   attributes.wmclass_class = window->wmclass_class;
509   attributes.width = widget->allocation.width;
510   attributes.height = widget->allocation.height;
511   attributes.wclass = GDK_INPUT_OUTPUT;
512
513   /* this isn't right - we should match our parent's visual/colormap.
514    * though that will require handling "foreign" colormaps */
515   attributes.visual = gtk_widget_get_visual (widget);
516   attributes.colormap = gtk_widget_get_colormap (widget);
517   attributes.event_mask = gtk_widget_get_events (widget);
518   attributes.event_mask |= (GDK_EXPOSURE_MASK |
519                             GDK_KEY_PRESS_MASK |
520                             GDK_KEY_RELEASE_MASK |
521                             GDK_ENTER_NOTIFY_MASK |
522                             GDK_LEAVE_NOTIFY_MASK |
523                             GDK_STRUCTURE_MASK);
524
525   attributes_mask = GDK_WA_VISUAL | GDK_WA_COLORMAP;
526   attributes_mask |= (window->title ? GDK_WA_TITLE : 0);
527   attributes_mask |= (window->wmclass_name ? GDK_WA_WMCLASS : 0);
528
529   if (GTK_WIDGET_TOPLEVEL (widget))
530     {
531       attributes.window_type = GDK_WINDOW_TOPLEVEL;
532
533       gdk_error_trap_push ();
534       if (plug->socket_window)
535         widget->window = gdk_window_new (plug->socket_window, 
536                                          &attributes, attributes_mask);
537       else /* If it's a passive plug, we use the root window */
538         widget->window = gdk_window_new (gtk_widget_get_root_window (widget),
539                                          &attributes, attributes_mask);
540
541       gdk_display_sync (gtk_widget_get_display (widget));
542       if (gdk_error_trap_pop ()) /* Uh-oh */
543         {
544           gdk_error_trap_push ();
545           gdk_window_destroy (widget->window);
546           gdk_flush ();
547           gdk_error_trap_pop ();
548           widget->window = gdk_window_new (gtk_widget_get_root_window (widget),
549                                            &attributes, attributes_mask);
550         }
551       
552       gdk_window_add_filter (widget->window, gtk_plug_filter_func, widget);
553
554       plug->modality_group = gtk_window_group_new ();
555       gtk_window_group_add_window (plug->modality_group, window);
556       
557       xembed_set_info (widget->window, 0);
558     }
559   else
560     widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), 
561                                      &attributes, attributes_mask);      
562   
563   gdk_window_set_user_data (widget->window, window);
564
565   widget->style = gtk_style_attach (widget->style, widget->window);
566   gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
567 }
568
569 static void
570 gtk_plug_show (GtkWidget *widget)
571 {
572   if (GTK_WIDGET_TOPLEVEL (widget))
573     GTK_WIDGET_CLASS (parent_class)->show (widget);
574   else
575     GTK_WIDGET_CLASS (bin_class)->show (widget);
576 }
577
578 static void
579 gtk_plug_hide (GtkWidget *widget)
580 {
581   if (GTK_WIDGET_TOPLEVEL (widget))
582     GTK_WIDGET_CLASS (parent_class)->hide (widget);
583   else
584     GTK_WIDGET_CLASS (bin_class)->hide (widget);
585 }
586
587 /* From gdkinternals.h */
588 void gdk_synthesize_window_state (GdkWindow     *window,
589                                   GdkWindowState unset_flags,
590                                   GdkWindowState set_flags);
591
592 static void
593 gtk_plug_map (GtkWidget *widget)
594 {
595   if (GTK_WIDGET_TOPLEVEL (widget))
596     {
597       GtkBin *bin = GTK_BIN (widget);
598       
599       GTK_WIDGET_SET_FLAGS (widget, GTK_MAPPED);
600
601       if (bin->child &&
602           GTK_WIDGET_VISIBLE (bin->child) &&
603           !GTK_WIDGET_MAPPED (bin->child))
604         gtk_widget_map (bin->child);
605
606       xembed_set_info (widget->window, XEMBED_MAPPED);
607       
608       gdk_synthesize_window_state (widget->window,
609                                    GDK_WINDOW_STATE_WITHDRAWN,
610                                    0);
611     }
612   else
613     GTK_WIDGET_CLASS (bin_class)->map (widget);
614 }
615
616 static void
617 gtk_plug_unmap (GtkWidget *widget)
618 {
619   if (GTK_WIDGET_TOPLEVEL (widget))
620     {
621       GTK_WIDGET_UNSET_FLAGS (widget, GTK_MAPPED);
622
623       gdk_window_hide (widget->window);
624       xembed_set_info (widget->window, 0);
625       
626       gdk_synthesize_window_state (widget->window,
627                                    0,
628                                    GDK_WINDOW_STATE_WITHDRAWN);
629     }
630   else
631     GTK_WIDGET_CLASS (bin_class)->unmap (widget);
632 }
633
634 static void
635 gtk_plug_size_allocate (GtkWidget     *widget,
636                         GtkAllocation *allocation)
637 {
638   if (GTK_WIDGET_TOPLEVEL (widget))
639     GTK_WIDGET_CLASS (parent_class)->size_allocate (widget, allocation);
640   else
641     {
642       GtkBin *bin = GTK_BIN (widget);
643
644       widget->allocation = *allocation;
645
646       if (GTK_WIDGET_REALIZED (widget))
647         gdk_window_move_resize (widget->window,
648                                 allocation->x, allocation->y,
649                                 allocation->width, allocation->height);
650
651       if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
652         {
653           GtkAllocation child_allocation;
654           
655           child_allocation.x = child_allocation.y = GTK_CONTAINER (widget)->border_width;
656           child_allocation.width =
657             MAX (1, (gint)allocation->width - child_allocation.x * 2);
658           child_allocation.height =
659             MAX (1, (gint)allocation->height - child_allocation.y * 2);
660           
661           gtk_widget_size_allocate (bin->child, &child_allocation);
662         }
663       
664     }
665 }
666
667 static gboolean
668 gtk_plug_key_press_event (GtkWidget   *widget,
669                           GdkEventKey *event)
670 {
671   if (GTK_WIDGET_TOPLEVEL (widget))
672     return GTK_WIDGET_CLASS (parent_class)->key_press_event (widget, event);
673   else
674     return FALSE;
675 }
676
677 static gboolean
678 gtk_plug_focus_event (GtkWidget      *widget,
679                       GdkEventFocus  *event)
680 {
681   /* We eat focus-in events and focus-out events, since they
682    * can be generated by something like a keyboard grab on
683    * a child of the plug.
684    */
685   return FALSE;
686 }
687
688 static void
689 gtk_plug_set_focus (GtkWindow *window,
690                     GtkWidget *focus)
691 {
692   GtkPlug *plug = GTK_PLUG (window);
693
694   GTK_WINDOW_CLASS (parent_class)->set_focus (window, focus);
695   
696   /* Ask for focus from embedder
697    */
698
699   if (focus && !window->has_toplevel_focus)
700     {
701       send_xembed_message (plug, XEMBED_REQUEST_FOCUS, 0, 0, 0,
702                            gtk_get_current_event_time ());
703     }
704 }
705
706 typedef struct
707 {
708   guint                  accelerator_key;
709   GdkModifierType        accelerator_mods;
710 } GrabbedKey;
711
712 static guint
713 grabbed_key_hash (gconstpointer a)
714 {
715   const GrabbedKey *key = a;
716   guint h;
717   
718   h = key->accelerator_key << 16;
719   h ^= key->accelerator_key >> 16;
720   h ^= key->accelerator_mods;
721
722   return h;
723 }
724
725 static gboolean
726 grabbed_key_equal (gconstpointer a, gconstpointer b)
727 {
728   const GrabbedKey *keya = a;
729   const GrabbedKey *keyb = b;
730
731   return (keya->accelerator_key == keyb->accelerator_key &&
732           keya->accelerator_mods == keyb->accelerator_mods);
733 }
734
735 static void
736 add_grabbed_key (gpointer key, gpointer val, gpointer data)
737 {
738   GrabbedKey *grabbed_key = key;
739   GtkPlug *plug = data;
740
741   if (!plug->grabbed_keys ||
742       !g_hash_table_lookup (plug->grabbed_keys, grabbed_key))
743     {
744       send_xembed_message (plug, XEMBED_GTK_GRAB_KEY, 0, 
745                            grabbed_key->accelerator_key, grabbed_key->accelerator_mods,
746                            gtk_get_current_event_time ());
747     }
748 }
749
750 static void
751 add_grabbed_key_always (gpointer key, gpointer val, gpointer data)
752 {
753   GrabbedKey *grabbed_key = key;
754   GtkPlug *plug = data;
755
756   send_xembed_message (plug, XEMBED_GTK_GRAB_KEY, 0, 
757                        grabbed_key->accelerator_key, grabbed_key->accelerator_mods,
758                        gtk_get_current_event_time ());
759 }
760
761 static void
762 remove_grabbed_key (gpointer key, gpointer val, gpointer data)
763 {
764   GrabbedKey *grabbed_key = key;
765   GtkPlug *plug = data;
766
767   if (!plug->grabbed_keys ||
768       !g_hash_table_lookup (plug->grabbed_keys, grabbed_key))
769     {
770       send_xembed_message (plug, XEMBED_GTK_UNGRAB_KEY, 0, 
771                            grabbed_key->accelerator_key, grabbed_key->accelerator_mods,
772                            gtk_get_current_event_time ());
773     }
774 }
775
776 static void
777 keys_foreach (GtkWindow      *window,
778               guint           keyval,
779               GdkModifierType modifiers,
780               gboolean        is_mnemonic,
781               gpointer        data)
782 {
783   GHashTable *new_grabbed_keys = data;
784   GrabbedKey *key = g_new (GrabbedKey, 1);
785
786   key->accelerator_key = keyval;
787   key->accelerator_mods = modifiers;
788   
789   g_hash_table_replace (new_grabbed_keys, key, key);
790 }
791
792 static void
793 gtk_plug_keys_changed (GtkWindow *window)
794 {
795   GHashTable *new_grabbed_keys, *old_grabbed_keys;
796   GtkPlug *plug = GTK_PLUG (window);
797
798   new_grabbed_keys = g_hash_table_new_full (grabbed_key_hash, grabbed_key_equal, (GDestroyNotify)g_free, NULL);
799   _gtk_window_keys_foreach (window, keys_foreach, new_grabbed_keys);
800
801   if (plug->socket_window)
802     g_hash_table_foreach (new_grabbed_keys, add_grabbed_key, plug);
803
804   old_grabbed_keys = plug->grabbed_keys;
805   plug->grabbed_keys = new_grabbed_keys;
806
807   if (old_grabbed_keys)
808     {
809       if (plug->socket_window)
810         g_hash_table_foreach (old_grabbed_keys, remove_grabbed_key, plug);
811       g_hash_table_destroy (old_grabbed_keys);
812     }
813 }
814
815 static gboolean
816 gtk_plug_focus (GtkWidget        *widget,
817                 GtkDirectionType  direction)
818 {
819   GtkBin *bin = GTK_BIN (widget);
820   GtkPlug *plug = GTK_PLUG (widget);
821   GtkWindow *window = GTK_WINDOW (widget);
822   GtkContainer *container = GTK_CONTAINER (widget);
823   GtkWidget *old_focus_child = container->focus_child;
824   GtkWidget *parent;
825   
826   /* We override GtkWindow's behavior, since we don't want wrapping here.
827    */
828   if (old_focus_child)
829     {
830       if (gtk_widget_child_focus (old_focus_child, direction))
831         return TRUE;
832
833       if (window->focus_widget)
834         {
835           /* Wrapped off the end, clear the focus setting for the toplevel */
836           parent = window->focus_widget->parent;
837           while (parent)
838             {
839               gtk_container_set_focus_child (GTK_CONTAINER (parent), NULL);
840               parent = GTK_WIDGET (parent)->parent;
841             }
842           
843           gtk_window_set_focus (GTK_WINDOW (container), NULL);
844
845           if (!GTK_CONTAINER (window)->focus_child)
846             {
847               gint message = -1;
848
849               switch (direction)
850                 {
851                 case GTK_DIR_UP:
852                 case GTK_DIR_LEFT:
853                 case GTK_DIR_TAB_BACKWARD:
854                   message = XEMBED_FOCUS_PREV;
855                   break;
856                 case GTK_DIR_DOWN:
857                 case GTK_DIR_RIGHT:
858                 case GTK_DIR_TAB_FORWARD:
859                   message = XEMBED_FOCUS_NEXT;
860                   break;
861                 }
862               
863               send_xembed_message (plug, message, 0, 0, 0,
864                                    gtk_get_current_event_time ());
865             }
866         }
867
868       return FALSE;
869     }
870   else
871     {
872       /* Try to focus the first widget in the window */
873       
874       if (bin->child && gtk_widget_child_focus (bin->child, direction))
875         return TRUE;
876     }
877
878   return FALSE;
879 }
880
881 static void
882 gtk_plug_check_resize (GtkContainer *container)
883 {
884   if (GTK_WIDGET_TOPLEVEL (container))
885     GTK_CONTAINER_CLASS (parent_class)->check_resize (container);
886   else
887     GTK_CONTAINER_CLASS (bin_class)->check_resize (container);
888 }
889
890 static void
891 send_xembed_message (GtkPlug *plug,
892                      glong      message,
893                      glong      detail,
894                      glong      data1,
895                      glong      data2,
896                      guint32    time)
897 {
898   if (plug->socket_window)
899     {
900       GdkDisplay *display = gdk_drawable_get_display (plug->socket_window);
901       XEvent xevent;
902
903       GTK_NOTE(PLUGSOCKET,
904                g_message ("GtkPlug: Sending XEMBED message of type %ld", message));
905
906       xevent.xclient.window = GDK_WINDOW_XWINDOW (plug->socket_window);
907       xevent.xclient.type = ClientMessage;
908       xevent.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "_XEMBED");
909       xevent.xclient.format = 32;
910       xevent.xclient.data.l[0] = time;
911       xevent.xclient.data.l[1] = message;
912       xevent.xclient.data.l[2] = detail;
913       xevent.xclient.data.l[3] = data1;
914       xevent.xclient.data.l[4] = data2;
915
916       gdk_error_trap_push ();
917       XSendEvent (GDK_WINDOW_XDISPLAY(plug->socket_window),
918                   GDK_WINDOW_XWINDOW (plug->socket_window),
919                   False, NoEventMask, &xevent);
920       gdk_display_sync (display);
921       gdk_error_trap_pop ();
922     }
923 }
924
925 static void
926 focus_first_last (GtkPlug          *plug,
927                   GtkDirectionType  direction)
928 {
929   GtkWindow *window = GTK_WINDOW (plug);
930   GtkWidget *parent;
931   
932   if (window->focus_widget)
933     {
934       parent = window->focus_widget->parent;
935       while (parent)
936         {
937           gtk_container_set_focus_child (GTK_CONTAINER (parent), NULL);
938           parent = GTK_WIDGET (parent)->parent;
939         }
940       
941       gtk_window_set_focus (GTK_WINDOW (plug), NULL);
942     }
943
944   gtk_widget_child_focus (GTK_WIDGET (plug), direction);
945 }
946
947 static void
948 handle_modality_on (GtkPlug *plug)
949 {
950   if (!plug->modality_window)
951     {
952       plug->modality_window = gtk_window_new (GTK_WINDOW_POPUP);
953       gtk_window_set_screen (GTK_WINDOW (plug->modality_window),
954                              gtk_widget_get_screen (GTK_WIDGET (plug)));
955       gtk_widget_realize (plug->modality_window);
956       gtk_window_group_add_window (plug->modality_group, GTK_WINDOW (plug->modality_window));
957       gtk_grab_add (plug->modality_window);
958     }
959 }
960
961 static void
962 handle_modality_off (GtkPlug *plug)
963 {
964   if (plug->modality_window)
965     {
966       gtk_widget_destroy (plug->modality_window);
967       plug->modality_window = NULL;
968     }
969 }
970
971 static void
972 xembed_set_info (GdkWindow     *window,
973                  unsigned long  flags)
974 {
975   GdkDisplay *display = gdk_drawable_get_display (window);
976   unsigned long buffer[2];
977
978   Atom xembed_info_atom = gdk_x11_get_xatom_by_name_for_display (display, "_XEMBED_INFO");
979
980   buffer[1] = 0;                /* Protocol version */
981   buffer[1] = flags;
982
983   XChangeProperty (GDK_DISPLAY_XDISPLAY (display),
984                    GDK_WINDOW_XWINDOW (window),
985                    xembed_info_atom, xembed_info_atom, 32,
986                    PropModeReplace,
987                    (unsigned char *)buffer, 2);
988 }
989
990 static void
991 handle_xembed_message (GtkPlug   *plug,
992                        glong      message,
993                        glong      detail,
994                        glong      data1,
995                        glong      data2,
996                        guint32    time)
997 {
998   GtkWindow *window = GTK_WINDOW (plug);
999   
1000   GTK_NOTE (PLUGSOCKET,
1001             g_message ("GtkPlug: Message of type %ld received", message));
1002   
1003   switch (message)
1004     {
1005     case XEMBED_EMBEDDED_NOTIFY:
1006       break;
1007     case XEMBED_WINDOW_ACTIVATE:
1008       _gtk_window_set_is_active (window, TRUE);
1009       break;
1010     case XEMBED_WINDOW_DEACTIVATE:
1011       _gtk_window_set_is_active (window, FALSE);
1012       break;
1013       
1014     case XEMBED_MODALITY_ON:
1015       handle_modality_on (plug);
1016       break;
1017     case XEMBED_MODALITY_OFF:
1018       handle_modality_off (plug);
1019       break;
1020
1021     case XEMBED_FOCUS_IN:
1022       _gtk_window_set_has_toplevel_focus (window, TRUE);
1023       switch (detail)
1024         {
1025         case XEMBED_FOCUS_FIRST:
1026           focus_first_last (plug, GTK_DIR_TAB_FORWARD);
1027           break;
1028         case XEMBED_FOCUS_LAST:
1029           focus_first_last (plug, GTK_DIR_TAB_BACKWARD);
1030           break;
1031         case XEMBED_FOCUS_CURRENT:
1032           break;
1033         }
1034       break;
1035
1036     case XEMBED_FOCUS_OUT:
1037       _gtk_window_set_has_toplevel_focus (window, FALSE);
1038       break;
1039       
1040     case XEMBED_GRAB_KEY:
1041     case XEMBED_UNGRAB_KEY:
1042     case XEMBED_GTK_GRAB_KEY:
1043     case XEMBED_GTK_UNGRAB_KEY:
1044     case XEMBED_REQUEST_FOCUS:
1045     case XEMBED_FOCUS_NEXT:
1046     case XEMBED_FOCUS_PREV:
1047       g_warning ("GtkPlug: Invalid _XEMBED message of type %ld received", message);
1048       break;
1049       
1050     default:
1051       GTK_NOTE(PLUGSOCKET,
1052                g_message ("GtkPlug: Ignoring unknown _XEMBED message of type %ld", message));
1053       break;
1054     }
1055 }
1056
1057 static GdkFilterReturn
1058 gtk_plug_filter_func (GdkXEvent *gdk_xevent, GdkEvent *event, gpointer data)
1059 {
1060   GdkScreen *screen = gdk_drawable_get_screen (event->any.window);
1061   GdkDisplay *display = gdk_screen_get_display (screen);
1062   GtkPlug *plug = GTK_PLUG (data);
1063   XEvent *xevent = (XEvent *)gdk_xevent;
1064
1065   GdkFilterReturn return_val;
1066   
1067   return_val = GDK_FILTER_CONTINUE;
1068
1069   switch (xevent->type)
1070     {
1071     case ClientMessage:
1072       if (xevent->xclient.message_type == gdk_x11_get_xatom_by_name_for_display (display, "_XEMBED"))
1073         {
1074           handle_xembed_message (plug,
1075                                  xevent->xclient.data.l[1],
1076                                  xevent->xclient.data.l[2],
1077                                  xevent->xclient.data.l[3],
1078                                  xevent->xclient.data.l[4],
1079                                  xevent->xclient.data.l[0]);
1080                                  
1081
1082           return GDK_FILTER_REMOVE;
1083         }
1084       else if (xevent->xclient.message_type == gdk_x11_get_xatom_by_name_for_display (display, "WM_DELETE_WINDOW"))
1085         {
1086           /* We filter these out because we take being reparented back to the
1087            * root window as the reliable end of the embedding protocol
1088            */
1089
1090           return GDK_FILTER_REMOVE;
1091         }
1092       break;
1093     case ReparentNotify:
1094       {
1095         XReparentEvent *xre = &xevent->xreparent;
1096         gboolean was_embedded = plug->socket_window != NULL;
1097
1098         return_val = GDK_FILTER_REMOVE;
1099         
1100         g_object_ref (plug);
1101         
1102         if (was_embedded)
1103           {
1104             /* End of embedding protocol for previous socket */
1105             
1106             /* FIXME: race if we remove from another socket and
1107              * then add to a local window before we get notification
1108              * Probably need check in _gtk_plug_add_to_socket
1109              */
1110             
1111             if (xre->parent != GDK_WINDOW_XWINDOW (plug->socket_window))
1112               {
1113                 GtkWidget *widget = GTK_WIDGET (plug);
1114
1115                 gdk_window_set_user_data (plug->socket_window, NULL);
1116                 g_object_unref (plug->socket_window);
1117                 plug->socket_window = NULL;
1118
1119                 /* Emit a delete window, as if the user attempted
1120                  * to close the toplevel. Simple as to how we
1121                  * handle WM_DELETE_WINDOW, if it isn't handled
1122                  * we destroy the widget. BUt only do this if
1123                  * we are being reparented to the root window.
1124                  * Moving from one embedder to another should
1125                  * be invisible to the app.
1126                  */
1127
1128                 if (xre->parent == GDK_WINDOW_XWINDOW (gdk_screen_get_root_window (screen)))
1129                   send_delete_event (widget);
1130               }
1131             else
1132               goto done;
1133           }
1134
1135         if (xre->parent != GDK_WINDOW_XWINDOW (gdk_screen_get_root_window (screen)))
1136           {
1137             /* Start of embedding protocol */
1138
1139             plug->socket_window = gdk_window_lookup_for_display (display, xre->parent);
1140             if (plug->socket_window)
1141               {
1142                 gpointer user_data = NULL;
1143                 gdk_window_get_user_data (plug->socket_window, &user_data);
1144
1145                 if (user_data)
1146                   {
1147                     g_warning (G_STRLOC "Plug reparented unexpectedly into window in the same process");
1148                     plug->socket_window = NULL;
1149                     break;
1150                   }
1151
1152                 g_object_ref (plug->socket_window);
1153               }
1154             else
1155               {
1156                 plug->socket_window = gdk_window_foreign_new_for_display (display, xre->parent);
1157                 if (!plug->socket_window) /* Already gone */
1158                   break;
1159               }
1160
1161             if (plug->grabbed_keys)
1162               g_hash_table_foreach (plug->grabbed_keys, add_grabbed_key_always, plug);
1163
1164             if (!was_embedded)
1165               g_signal_emit (plug, plug_signals[EMBEDDED], 0);
1166           }
1167
1168       done:
1169         g_object_unref (plug);
1170         
1171         break;
1172       }
1173     }
1174
1175   return GDK_FILTER_CONTINUE;
1176 }