]> Pileus Git - ~andy/gtk/blob - gtk/gtkplug.c
fbf25e0f3abf52185c26f30e9dc2aa441ad1ece2
[~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 void
356 gtk_plug_construct_for_display (GtkPlug         *plug,
357                                 GdkDisplay      *display,
358                                 GdkNativeWindow  socket_id)
359 {
360   if (socket_id)
361     {
362       gpointer user_data = NULL;
363
364       plug->socket_window = gdk_window_lookup_for_display (display, socket_id);
365       
366       if (plug->socket_window)
367         gdk_window_get_user_data (plug->socket_window, &user_data);
368       else
369         plug->socket_window = gdk_window_foreign_new_for_display (display, socket_id);
370           
371       if (user_data)
372         {
373           if (GTK_IS_SOCKET (user_data))
374             _gtk_plug_add_to_socket (plug, user_data);
375           else
376             {
377               g_warning (G_STRLOC "Can't create GtkPlug as child of non-GtkSocket");
378               plug->socket_window = NULL;
379             }
380         }
381
382       if (plug->socket_window)
383         g_signal_emit (plug, plug_signals[EMBEDDED], 0);
384     }
385 }
386
387 /**
388  * gtk_plug_new:
389  * @socket_id:  the window ID of the socket, or 0.
390  * 
391  * Creates a new plug widget inside the #GtkSocket identified
392  * by @socket_id. If @socket_id is 0, the plug is left "unplugged" and
393  * can later be plugged into a #GtkSocket by  gtk_socket_add_id().
394  * 
395  * Return value: the new #GtkPlug widget.
396  **/
397 GtkWidget*
398 gtk_plug_new (GdkNativeWindow socket_id)
399 {
400   return gtk_plug_new_for_display (gdk_display_get_default (), socket_id);
401 }
402
403 /**
404  * gtk_plug_new_for_display:
405  * @display : the #GdkDisplay on which @socket_id is displayed
406  * @socket_id: the XID of the socket's window.
407  * 
408  * Create a new plug widget inside the #GtkSocket identified by socket_id.
409  *
410  * Return value: the new #GtkPlug widget.
411  */
412 GtkWidget*
413 gtk_plug_new_for_display (GdkDisplay      *display,
414                           GdkNativeWindow  socket_id)
415 {
416   GtkPlug *plug;
417
418   plug = g_object_new (GTK_TYPE_PLUG, NULL);
419   gtk_plug_construct_for_display (plug, display, socket_id);
420   return GTK_WIDGET (plug);
421 }
422
423 /**
424  * gtk_plug_get_id:
425  * @plug: a #GtkPlug.
426  * 
427  * Gets the window ID of a #GtkPlug widget, which can then
428  * be used to embed this window inside another window, for
429  * instance with gtk_socket_add_id().
430  * 
431  * Return value: the window ID for the plug
432  **/
433 GdkNativeWindow
434 gtk_plug_get_id (GtkPlug *plug)
435 {
436   g_return_val_if_fail (GTK_IS_PLUG (plug), 0);
437
438   if (!GTK_WIDGET_REALIZED (plug))
439     gtk_widget_realize (GTK_WIDGET (plug));
440
441   return GDK_WINDOW_XWINDOW (GTK_WIDGET (plug)->window);
442 }
443
444 static void
445 gtk_plug_finalize (GObject *object)
446 {
447   GtkPlug *plug = GTK_PLUG (object);
448
449   if (plug->grabbed_keys)
450     {
451       g_hash_table_destroy (plug->grabbed_keys);
452       plug->grabbed_keys = NULL;
453     }
454   
455   G_OBJECT_CLASS (parent_class)->finalize (object);
456 }
457
458 static void
459 gtk_plug_unrealize (GtkWidget *widget)
460 {
461   GtkPlug *plug;
462
463   g_return_if_fail (GTK_IS_PLUG (widget));
464
465   plug = GTK_PLUG (widget);
466
467   if (plug->socket_window != NULL)
468     {
469       gdk_window_set_user_data (plug->socket_window, NULL);
470       g_object_unref (plug->socket_window);
471       plug->socket_window = NULL;
472     }
473
474   if (!plug->same_app)
475     {
476       if (plug->modality_window)
477         handle_modality_off (plug);
478
479       gtk_window_group_remove_window (plug->modality_group, GTK_WINDOW (plug));
480       g_object_unref (plug->modality_group);
481     }
482   
483   if (GTK_WIDGET_CLASS (parent_class)->unrealize)
484     (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
485 }
486
487 static void
488 gtk_plug_realize (GtkWidget *widget)
489 {
490   GtkWindow *window;
491   GtkPlug *plug;
492   GdkWindowAttr attributes;
493   gint attributes_mask;
494
495   g_return_if_fail (GTK_IS_PLUG (widget));
496
497   GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
498   window = GTK_WINDOW (widget);
499   plug = GTK_PLUG (widget);
500
501   attributes.window_type = GDK_WINDOW_CHILD;    /* XXX GDK_WINDOW_PLUG ? */
502   attributes.title = window->title;
503   attributes.wmclass_name = window->wmclass_name;
504   attributes.wmclass_class = window->wmclass_class;
505   attributes.width = widget->allocation.width;
506   attributes.height = widget->allocation.height;
507   attributes.wclass = GDK_INPUT_OUTPUT;
508
509   /* this isn't right - we should match our parent's visual/colormap.
510    * though that will require handling "foreign" colormaps */
511   attributes.visual = gtk_widget_get_visual (widget);
512   attributes.colormap = gtk_widget_get_colormap (widget);
513   attributes.event_mask = gtk_widget_get_events (widget);
514   attributes.event_mask |= (GDK_EXPOSURE_MASK |
515                             GDK_KEY_PRESS_MASK |
516                             GDK_KEY_RELEASE_MASK |
517                             GDK_ENTER_NOTIFY_MASK |
518                             GDK_LEAVE_NOTIFY_MASK |
519                             GDK_STRUCTURE_MASK);
520
521   attributes_mask = GDK_WA_VISUAL | GDK_WA_COLORMAP;
522   attributes_mask |= (window->title ? GDK_WA_TITLE : 0);
523   attributes_mask |= (window->wmclass_name ? GDK_WA_WMCLASS : 0);
524
525   if (GTK_WIDGET_TOPLEVEL (widget))
526     {
527       attributes.window_type = GDK_WINDOW_TOPLEVEL;
528
529       gdk_error_trap_push ();
530       if (plug->socket_window)
531         widget->window = gdk_window_new (plug->socket_window, 
532                                          &attributes, attributes_mask);
533       else /* If it's a passive plug, we use the root window */
534         widget->window = gdk_window_new (gtk_widget_get_root_window (widget),
535                                          &attributes, attributes_mask);
536
537       gdk_display_sync (gtk_widget_get_display (widget));
538       if (gdk_error_trap_pop ()) /* Uh-oh */
539         {
540           gdk_error_trap_push ();
541           gdk_window_destroy (widget->window);
542           gdk_flush ();
543           gdk_error_trap_pop ();
544           widget->window = gdk_window_new (gtk_widget_get_root_window (widget),
545                                            &attributes, attributes_mask);
546         }
547       
548       gdk_window_add_filter (widget->window, gtk_plug_filter_func, widget);
549
550       plug->modality_group = gtk_window_group_new ();
551       gtk_window_group_add_window (plug->modality_group, window);
552       
553       xembed_set_info (widget->window, 0);
554     }
555   else
556     widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), 
557                                      &attributes, attributes_mask);      
558   
559   gdk_window_set_user_data (widget->window, window);
560
561   widget->style = gtk_style_attach (widget->style, widget->window);
562   gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
563 }
564
565 static void
566 gtk_plug_show (GtkWidget *widget)
567 {
568   if (GTK_WIDGET_TOPLEVEL (widget))
569     GTK_WIDGET_CLASS (parent_class)->show (widget);
570   else
571     GTK_WIDGET_CLASS (bin_class)->show (widget);
572 }
573
574 static void
575 gtk_plug_hide (GtkWidget *widget)
576 {
577   if (GTK_WIDGET_TOPLEVEL (widget))
578     GTK_WIDGET_CLASS (parent_class)->hide (widget);
579   else
580     GTK_WIDGET_CLASS (bin_class)->hide (widget);
581 }
582
583 /* From gdkinternals.h */
584 void gdk_synthesize_window_state (GdkWindow     *window,
585                                   GdkWindowState unset_flags,
586                                   GdkWindowState set_flags);
587
588 static void
589 gtk_plug_map (GtkWidget *widget)
590 {
591   if (GTK_WIDGET_TOPLEVEL (widget))
592     {
593       GtkBin *bin = GTK_BIN (widget);
594       
595       GTK_WIDGET_SET_FLAGS (widget, GTK_MAPPED);
596
597       if (bin->child &&
598           GTK_WIDGET_VISIBLE (bin->child) &&
599           !GTK_WIDGET_MAPPED (bin->child))
600         gtk_widget_map (bin->child);
601
602       xembed_set_info (widget->window, XEMBED_MAPPED);
603       
604       gdk_synthesize_window_state (widget->window,
605                                    GDK_WINDOW_STATE_WITHDRAWN,
606                                    0);
607     }
608   else
609     GTK_WIDGET_CLASS (bin_class)->map (widget);
610 }
611
612 static void
613 gtk_plug_unmap (GtkWidget *widget)
614 {
615   if (GTK_WIDGET_TOPLEVEL (widget))
616     {
617       GTK_WIDGET_UNSET_FLAGS (widget, GTK_MAPPED);
618
619       gdk_window_hide (widget->window);
620       xembed_set_info (widget->window, 0);
621       
622       gdk_synthesize_window_state (widget->window,
623                                    0,
624                                    GDK_WINDOW_STATE_WITHDRAWN);
625     }
626   else
627     GTK_WIDGET_CLASS (bin_class)->unmap (widget);
628 }
629
630 static void
631 gtk_plug_size_allocate (GtkWidget     *widget,
632                         GtkAllocation *allocation)
633 {
634   if (GTK_WIDGET_TOPLEVEL (widget))
635     GTK_WIDGET_CLASS (parent_class)->size_allocate (widget, allocation);
636   else
637     {
638       GtkBin *bin = GTK_BIN (widget);
639
640       widget->allocation = *allocation;
641
642       if (GTK_WIDGET_REALIZED (widget))
643         gdk_window_move_resize (widget->window,
644                                 allocation->x, allocation->y,
645                                 allocation->width, allocation->height);
646
647       if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
648         {
649           GtkAllocation child_allocation;
650           
651           child_allocation.x = child_allocation.y = GTK_CONTAINER (widget)->border_width;
652           child_allocation.width =
653             MAX (1, (gint)allocation->width - child_allocation.x * 2);
654           child_allocation.height =
655             MAX (1, (gint)allocation->height - child_allocation.y * 2);
656           
657           gtk_widget_size_allocate (bin->child, &child_allocation);
658         }
659       
660     }
661 }
662
663 static gboolean
664 gtk_plug_key_press_event (GtkWidget   *widget,
665                           GdkEventKey *event)
666 {
667   if (GTK_WIDGET_TOPLEVEL (widget))
668     return GTK_WIDGET_CLASS (parent_class)->key_press_event (widget, event);
669   else
670     return FALSE;
671 }
672
673 static gboolean
674 gtk_plug_focus_event (GtkWidget      *widget,
675                       GdkEventFocus  *event)
676 {
677   /* We eat focus-in events and focus-out events, since they
678    * can be generated by something like a keyboard grab on
679    * a child of the plug.
680    */
681   return FALSE;
682 }
683
684 static void
685 gtk_plug_set_focus (GtkWindow *window,
686                     GtkWidget *focus)
687 {
688   GtkPlug *plug = GTK_PLUG (window);
689
690   GTK_WINDOW_CLASS (parent_class)->set_focus (window, focus);
691   
692   /* Ask for focus from embedder
693    */
694
695   if (focus && !window->has_toplevel_focus)
696     {
697       send_xembed_message (plug, XEMBED_REQUEST_FOCUS, 0, 0, 0,
698                            gtk_get_current_event_time ());
699     }
700 }
701
702 typedef struct
703 {
704   guint                  accelerator_key;
705   GdkModifierType        accelerator_mods;
706 } GrabbedKey;
707
708 static guint
709 grabbed_key_hash (gconstpointer a)
710 {
711   const GrabbedKey *key = a;
712   guint h;
713   
714   h = key->accelerator_key << 16;
715   h ^= key->accelerator_key >> 16;
716   h ^= key->accelerator_mods;
717
718   return h;
719 }
720
721 static gboolean
722 grabbed_key_equal (gconstpointer a, gconstpointer b)
723 {
724   const GrabbedKey *keya = a;
725   const GrabbedKey *keyb = b;
726
727   return (keya->accelerator_key == keyb->accelerator_key &&
728           keya->accelerator_mods == keyb->accelerator_mods);
729 }
730
731 static void
732 add_grabbed_key (gpointer key, gpointer val, gpointer data)
733 {
734   GrabbedKey *grabbed_key = key;
735   GtkPlug *plug = data;
736
737   if (!plug->grabbed_keys ||
738       !g_hash_table_lookup (plug->grabbed_keys, grabbed_key))
739     {
740       send_xembed_message (plug, XEMBED_GTK_GRAB_KEY, 0, 
741                            grabbed_key->accelerator_key, grabbed_key->accelerator_mods,
742                            gtk_get_current_event_time ());
743     }
744 }
745
746 static void
747 add_grabbed_key_always (gpointer key, gpointer val, gpointer data)
748 {
749   GrabbedKey *grabbed_key = key;
750   GtkPlug *plug = data;
751
752   send_xembed_message (plug, XEMBED_GTK_GRAB_KEY, 0, 
753                        grabbed_key->accelerator_key, grabbed_key->accelerator_mods,
754                        gtk_get_current_event_time ());
755 }
756
757 static void
758 remove_grabbed_key (gpointer key, gpointer val, gpointer data)
759 {
760   GrabbedKey *grabbed_key = key;
761   GtkPlug *plug = data;
762
763   if (!plug->grabbed_keys ||
764       !g_hash_table_lookup (plug->grabbed_keys, grabbed_key))
765     {
766       send_xembed_message (plug, XEMBED_GTK_UNGRAB_KEY, 0, 
767                            grabbed_key->accelerator_key, grabbed_key->accelerator_mods,
768                            gtk_get_current_event_time ());
769     }
770 }
771
772 static void
773 keys_foreach (GtkWindow      *window,
774               guint           keyval,
775               GdkModifierType modifiers,
776               gboolean        is_mnemonic,
777               gpointer        data)
778 {
779   GHashTable *new_grabbed_keys = data;
780   GrabbedKey *key = g_new (GrabbedKey, 1);
781
782   key->accelerator_key = keyval;
783   key->accelerator_mods = modifiers;
784   
785   g_hash_table_replace (new_grabbed_keys, key, key);
786 }
787
788 static void
789 gtk_plug_keys_changed (GtkWindow *window)
790 {
791   GHashTable *new_grabbed_keys, *old_grabbed_keys;
792   GtkPlug *plug = GTK_PLUG (window);
793
794   new_grabbed_keys = g_hash_table_new_full (grabbed_key_hash, grabbed_key_equal, (GDestroyNotify)g_free, NULL);
795   _gtk_window_keys_foreach (window, keys_foreach, new_grabbed_keys);
796
797   if (plug->socket_window)
798     g_hash_table_foreach (new_grabbed_keys, add_grabbed_key, plug);
799
800   old_grabbed_keys = plug->grabbed_keys;
801   plug->grabbed_keys = new_grabbed_keys;
802
803   if (old_grabbed_keys)
804     {
805       if (plug->socket_window)
806         g_hash_table_foreach (old_grabbed_keys, remove_grabbed_key, plug);
807       g_hash_table_destroy (old_grabbed_keys);
808     }
809 }
810
811 static gboolean
812 gtk_plug_focus (GtkWidget        *widget,
813                 GtkDirectionType  direction)
814 {
815   GtkBin *bin = GTK_BIN (widget);
816   GtkPlug *plug = GTK_PLUG (widget);
817   GtkWindow *window = GTK_WINDOW (widget);
818   GtkContainer *container = GTK_CONTAINER (widget);
819   GtkWidget *old_focus_child = container->focus_child;
820   GtkWidget *parent;
821   
822   /* We override GtkWindow's behavior, since we don't want wrapping here.
823    */
824   if (old_focus_child)
825     {
826       if (gtk_widget_child_focus (old_focus_child, direction))
827         return TRUE;
828
829       if (window->focus_widget)
830         {
831           /* Wrapped off the end, clear the focus setting for the toplevel */
832           parent = window->focus_widget->parent;
833           while (parent)
834             {
835               gtk_container_set_focus_child (GTK_CONTAINER (parent), NULL);
836               parent = GTK_WIDGET (parent)->parent;
837             }
838           
839           gtk_window_set_focus (GTK_WINDOW (container), NULL);
840
841           if (!GTK_CONTAINER (window)->focus_child)
842             {
843               gint message = -1;
844
845               switch (direction)
846                 {
847                 case GTK_DIR_UP:
848                 case GTK_DIR_LEFT:
849                 case GTK_DIR_TAB_BACKWARD:
850                   message = XEMBED_FOCUS_PREV;
851                   break;
852                 case GTK_DIR_DOWN:
853                 case GTK_DIR_RIGHT:
854                 case GTK_DIR_TAB_FORWARD:
855                   message = XEMBED_FOCUS_NEXT;
856                   break;
857                 }
858               
859               send_xembed_message (plug, message, 0, 0, 0,
860                                    gtk_get_current_event_time ());
861             }
862         }
863
864       return FALSE;
865     }
866   else
867     {
868       /* Try to focus the first widget in the window */
869       
870       if (bin->child && gtk_widget_child_focus (bin->child, direction))
871         return TRUE;
872     }
873
874   return FALSE;
875 }
876
877 static void
878 gtk_plug_check_resize (GtkContainer *container)
879 {
880   if (GTK_WIDGET_TOPLEVEL (container))
881     GTK_CONTAINER_CLASS (parent_class)->check_resize (container);
882   else
883     GTK_CONTAINER_CLASS (bin_class)->check_resize (container);
884 }
885
886 static void
887 send_xembed_message (GtkPlug *plug,
888                      glong      message,
889                      glong      detail,
890                      glong      data1,
891                      glong      data2,
892                      guint32    time)
893 {
894   if (plug->socket_window)
895     {
896       GdkDisplay *display = gdk_drawable_get_display (plug->socket_window);
897       XEvent xevent;
898
899       GTK_NOTE(PLUGSOCKET,
900                g_message ("GtkPlug: Sending XEMBED message of type %ld", message));
901
902       xevent.xclient.window = GDK_WINDOW_XWINDOW (plug->socket_window);
903       xevent.xclient.type = ClientMessage;
904       xevent.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "_XEMBED");
905       xevent.xclient.format = 32;
906       xevent.xclient.data.l[0] = time;
907       xevent.xclient.data.l[1] = message;
908       xevent.xclient.data.l[2] = detail;
909       xevent.xclient.data.l[3] = data1;
910       xevent.xclient.data.l[4] = data2;
911
912       gdk_error_trap_push ();
913       XSendEvent (GDK_WINDOW_XDISPLAY(plug->socket_window),
914                   GDK_WINDOW_XWINDOW (plug->socket_window),
915                   False, NoEventMask, &xevent);
916       gdk_display_sync (display);
917       gdk_error_trap_pop ();
918     }
919 }
920
921 static void
922 focus_first_last (GtkPlug          *plug,
923                   GtkDirectionType  direction)
924 {
925   GtkWindow *window = GTK_WINDOW (plug);
926   GtkWidget *parent;
927   
928   if (window->focus_widget)
929     {
930       parent = window->focus_widget->parent;
931       while (parent)
932         {
933           gtk_container_set_focus_child (GTK_CONTAINER (parent), NULL);
934           parent = GTK_WIDGET (parent)->parent;
935         }
936       
937       gtk_window_set_focus (GTK_WINDOW (plug), NULL);
938     }
939
940   gtk_widget_child_focus (GTK_WIDGET (plug), direction);
941 }
942
943 static void
944 handle_modality_on (GtkPlug *plug)
945 {
946   if (!plug->modality_window)
947     {
948       plug->modality_window = gtk_window_new (GTK_WINDOW_POPUP);
949       gtk_window_set_screen (GTK_WINDOW (plug->modality_window),
950                              gtk_widget_get_screen (GTK_WIDGET (plug)));
951       gtk_widget_realize (plug->modality_window);
952       gtk_window_group_add_window (plug->modality_group, GTK_WINDOW (plug->modality_window));
953       gtk_grab_add (plug->modality_window);
954     }
955 }
956
957 static void
958 handle_modality_off (GtkPlug *plug)
959 {
960   if (plug->modality_window)
961     {
962       gtk_widget_destroy (plug->modality_window);
963       plug->modality_window = NULL;
964     }
965 }
966
967 static void
968 xembed_set_info (GdkWindow     *window,
969                  unsigned long  flags)
970 {
971   GdkDisplay *display = gdk_drawable_get_display (window);
972   unsigned long buffer[2];
973
974   Atom xembed_info_atom = gdk_x11_get_xatom_by_name_for_display (display, "_XEMBED_INFO");
975
976   buffer[1] = 0;                /* Protocol version */
977   buffer[1] = flags;
978
979   XChangeProperty (GDK_DISPLAY_XDISPLAY (display),
980                    GDK_WINDOW_XWINDOW (window),
981                    xembed_info_atom, xembed_info_atom, 32,
982                    PropModeReplace,
983                    (unsigned char *)buffer, 2);
984 }
985
986 static void
987 handle_xembed_message (GtkPlug   *plug,
988                        glong      message,
989                        glong      detail,
990                        glong      data1,
991                        glong      data2,
992                        guint32    time)
993 {
994   GtkWindow *window = GTK_WINDOW (plug);
995   
996   GTK_NOTE (PLUGSOCKET,
997             g_message ("GtkPlug: Message of type %ld received", message));
998   
999   switch (message)
1000     {
1001     case XEMBED_EMBEDDED_NOTIFY:
1002       break;
1003     case XEMBED_WINDOW_ACTIVATE:
1004       _gtk_window_set_is_active (window, TRUE);
1005       break;
1006     case XEMBED_WINDOW_DEACTIVATE:
1007       _gtk_window_set_is_active (window, FALSE);
1008       break;
1009       
1010     case XEMBED_MODALITY_ON:
1011       handle_modality_on (plug);
1012       break;
1013     case XEMBED_MODALITY_OFF:
1014       handle_modality_off (plug);
1015       break;
1016
1017     case XEMBED_FOCUS_IN:
1018       _gtk_window_set_has_toplevel_focus (window, TRUE);
1019       switch (detail)
1020         {
1021         case XEMBED_FOCUS_FIRST:
1022           focus_first_last (plug, GTK_DIR_TAB_FORWARD);
1023           break;
1024         case XEMBED_FOCUS_LAST:
1025           focus_first_last (plug, GTK_DIR_TAB_BACKWARD);
1026           break;
1027         case XEMBED_FOCUS_CURRENT:
1028           break;
1029         }
1030       break;
1031
1032     case XEMBED_FOCUS_OUT:
1033       _gtk_window_set_has_toplevel_focus (window, FALSE);
1034       break;
1035       
1036     case XEMBED_GRAB_KEY:
1037     case XEMBED_UNGRAB_KEY:
1038     case XEMBED_GTK_GRAB_KEY:
1039     case XEMBED_GTK_UNGRAB_KEY:
1040     case XEMBED_REQUEST_FOCUS:
1041     case XEMBED_FOCUS_NEXT:
1042     case XEMBED_FOCUS_PREV:
1043       g_warning ("GtkPlug: Invalid _XEMBED message of type %ld received", message);
1044       break;
1045       
1046     default:
1047       GTK_NOTE(PLUGSOCKET,
1048                g_message ("GtkPlug: Ignoring unknown _XEMBED message of type %ld", message));
1049       break;
1050     }
1051 }
1052
1053 static GdkFilterReturn
1054 gtk_plug_filter_func (GdkXEvent *gdk_xevent, GdkEvent *event, gpointer data)
1055 {
1056   GdkScreen *screen = gdk_drawable_get_screen (event->any.window);
1057   GdkDisplay *display = gdk_screen_get_display (screen);
1058   GtkPlug *plug = GTK_PLUG (data);
1059   XEvent *xevent = (XEvent *)gdk_xevent;
1060
1061   GdkFilterReturn return_val;
1062   
1063   return_val = GDK_FILTER_CONTINUE;
1064
1065   switch (xevent->type)
1066     {
1067     case ClientMessage:
1068       if (xevent->xclient.message_type == gdk_x11_get_xatom_by_name_for_display (display, "_XEMBED"))
1069         {
1070           handle_xembed_message (plug,
1071                                  xevent->xclient.data.l[1],
1072                                  xevent->xclient.data.l[2],
1073                                  xevent->xclient.data.l[3],
1074                                  xevent->xclient.data.l[4],
1075                                  xevent->xclient.data.l[0]);
1076                                  
1077
1078           return GDK_FILTER_REMOVE;
1079         }
1080       else if (xevent->xclient.message_type == gdk_x11_get_xatom_by_name_for_display (display, "WM_DELETE_WINDOW"))
1081         {
1082           /* We filter these out because we take being reparented back to the
1083            * root window as the reliable end of the embedding protocol
1084            */
1085
1086           return GDK_FILTER_REMOVE;
1087         }
1088       break;
1089     case ReparentNotify:
1090       {
1091         XReparentEvent *xre = &xevent->xreparent;
1092         gboolean was_embedded = plug->socket_window != NULL;
1093
1094         return_val = GDK_FILTER_REMOVE;
1095         
1096         g_object_ref (plug);
1097         
1098         if (was_embedded)
1099           {
1100             /* End of embedding protocol for previous socket */
1101             
1102             /* FIXME: race if we remove from another socket and
1103              * then add to a local window before we get notification
1104              * Probably need check in _gtk_plug_add_to_socket
1105              */
1106             
1107             if (xre->parent != GDK_WINDOW_XWINDOW (plug->socket_window))
1108               {
1109                 GtkWidget *widget = GTK_WIDGET (plug);
1110
1111                 gdk_window_set_user_data (plug->socket_window, NULL);
1112                 g_object_unref (plug->socket_window);
1113                 plug->socket_window = NULL;
1114
1115                 /* Emit a delete window, as if the user attempted
1116                  * to close the toplevel. Simple as to how we
1117                  * handle WM_DELETE_WINDOW, if it isn't handled
1118                  * we destroy the widget. BUt only do this if
1119                  * we are being reparented to the root window.
1120                  * Moving from one embedder to another should
1121                  * be invisible to the app.
1122                  */
1123
1124                 if (xre->parent == GDK_WINDOW_XWINDOW (gdk_screen_get_root_window (screen)))
1125                   send_delete_event (widget);
1126               }
1127             else
1128               goto done;
1129           }
1130
1131         if (xre->parent != GDK_WINDOW_XWINDOW (gdk_screen_get_root_window (screen)))
1132           {
1133             /* Start of embedding protocol */
1134
1135             plug->socket_window = gdk_window_lookup_for_display (display, xre->parent);
1136             if (plug->socket_window)
1137               {
1138                 gpointer user_data = NULL;
1139                 gdk_window_get_user_data (plug->socket_window, &user_data);
1140
1141                 if (user_data)
1142                   {
1143                     g_warning (G_STRLOC "Plug reparented unexpectedly into window in the same process");
1144                     plug->socket_window = NULL;
1145                     break;
1146                   }
1147
1148                 g_object_ref (plug->socket_window);
1149               }
1150             else
1151               {
1152                 plug->socket_window = gdk_window_foreign_new_for_display (display, xre->parent);
1153                 if (!plug->socket_window) /* Already gone */
1154                   break;
1155               }
1156
1157             if (plug->grabbed_keys)
1158               g_hash_table_foreach (plug->grabbed_keys, add_grabbed_key_always, plug);
1159
1160             if (!was_embedded)
1161               g_signal_emit (plug, plug_signals[EMBEDDED], 0);
1162           }
1163
1164       done:
1165         g_object_unref (plug);
1166         
1167         break;
1168       }
1169     }
1170
1171   return GDK_FILTER_CONTINUE;
1172 }