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