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