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