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