]> Pileus Git - ~andy/gtk/blob - gtk/gtksocket.c
new default color scheme based on the GNOME stock icon palette. (#80691,
[~andy/gtk] / gtk / gtksocket.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 <string.h>
29
30 #include "gdk/gdkkeysyms.h"
31 #include "gtkmain.h"
32 #include "gtkmarshalers.h"
33 #include "gtkwindow.h"
34 #include "gtkplug.h"
35 #include "gtkprivate.h"
36 #include "gtksocket.h"
37 #include "gtkdnd.h"
38
39 #include "x11/gdkx.h"
40
41 #include "xembed.h"
42
43 typedef struct _GtkSocketPrivate GtkSocketPrivate;
44
45 struct _GtkSocketPrivate
46 {
47   gint resize_count;
48 };
49
50 /* Forward declararations */
51
52 static void     gtk_socket_class_init           (GtkSocketClass   *klass);
53 static void     gtk_socket_init                 (GtkSocket        *socket);
54 static void     gtk_socket_finalize             (GObject          *object);
55 static void     gtk_socket_notify               (GObject          *object,
56                                                  GParamSpec       *pspec);
57 static void     gtk_socket_realize              (GtkWidget        *widget);
58 static void     gtk_socket_unrealize            (GtkWidget        *widget);
59 static void     gtk_socket_size_request         (GtkWidget        *widget,
60                                                  GtkRequisition   *requisition);
61 static void     gtk_socket_size_allocate        (GtkWidget        *widget,
62                                                  GtkAllocation    *allocation);
63 static void     gtk_socket_hierarchy_changed    (GtkWidget        *widget,
64                                                  GtkWidget        *old_toplevel);
65 static void     gtk_socket_grab_notify          (GtkWidget        *widget,
66                                                  gboolean          was_grabbed);
67 static gboolean gtk_socket_key_press_event      (GtkWidget        *widget,
68                                                  GdkEventKey      *event);
69 static void     gtk_socket_claim_focus          (GtkSocket        *socket,
70                                                  gboolean          send_event);
71 static void     gtk_socket_send_configure_event (GtkSocket        *socket);
72 static gboolean gtk_socket_focus                (GtkWidget        *widget,
73                                                  GtkDirectionType  direction);
74 static void     gtk_socket_remove               (GtkContainer     *container,
75                                                  GtkWidget        *widget);
76 static void     gtk_socket_forall               (GtkContainer     *container,
77                                                  gboolean          include_internals,
78                                                  GtkCallback       callback,
79                                                  gpointer          callback_data);
80
81
82 static void            gtk_socket_add_window  (GtkSocket       *socket,
83                                                GdkNativeWindow  xid,
84                                                gboolean         need_reparent);
85 static GdkFilterReturn gtk_socket_filter_func (GdkXEvent       *gdk_xevent,
86                                                GdkEvent        *event,
87                                                gpointer         data);
88
89 static void     send_xembed_message (GtkSocket     *socket,
90                                      glong          message,
91                                      glong          detail,
92                                      glong          data1,
93                                      glong          data2,
94                                      guint32        time);
95 static gboolean xembed_get_info     (GdkWindow     *gdk_window,
96                                      unsigned long *version,
97                                      unsigned long *flags);
98
99 /* From Tk */
100 #define EMBEDDED_APP_WANTS_FOCUS NotifyNormal+20
101
102 /* Local data */
103
104 enum {
105   PLUG_ADDED,
106   PLUG_REMOVED,
107   LAST_SIGNAL
108 }; 
109
110 static guint socket_signals[LAST_SIGNAL] = { 0 };
111
112 static GtkWidgetClass *parent_class = NULL;
113
114 GtkSocketPrivate *
115 gtk_socket_get_private (GtkSocket *socket)
116 {
117   GtkSocketPrivate *private;
118   static GQuark private_quark = 0;
119
120   if (!private_quark)
121     private_quark = g_quark_from_static_string ("gtk-socket-private");
122
123   private = g_object_get_qdata (G_OBJECT (socket), private_quark);
124
125   if (!private)
126     {
127       private = g_new0 (GtkSocketPrivate, 1);
128       private->resize_count = 0;
129       
130       g_object_set_qdata_full (G_OBJECT (socket), private_quark,
131                                private, (GDestroyNotify) g_free);
132     }
133
134   return private;
135 }
136
137 GType
138 gtk_socket_get_type (void)
139 {
140   static GType socket_type = 0;
141
142   if (!socket_type)
143     {
144       static const GTypeInfo socket_info =
145       {
146         sizeof (GtkSocketClass),
147         NULL,           /* base_init */
148         NULL,           /* base_finalize */
149         (GClassInitFunc) gtk_socket_class_init,
150         NULL,           /* class_finalize */
151         NULL,           /* class_data */
152         sizeof (GtkSocket),
153         16,             /* n_preallocs */
154         (GInstanceInitFunc) gtk_socket_init,
155       };
156
157       socket_type = g_type_register_static (GTK_TYPE_CONTAINER, "GtkSocket",
158                                             &socket_info, 0);
159     }
160
161   return socket_type;
162 }
163
164 static void
165 gtk_socket_finalize (GObject *object)
166 {
167   GtkSocket *socket = GTK_SOCKET (object);
168   
169   g_object_unref (socket->accel_group);
170   socket->accel_group = NULL;
171
172   G_OBJECT_CLASS (parent_class)->finalize (object);
173 }
174
175 static void
176 gtk_socket_class_init (GtkSocketClass *class)
177 {
178   GtkWidgetClass *widget_class;
179   GtkContainerClass *container_class;
180   GObjectClass *gobject_class;
181
182   gobject_class = (GObjectClass *) class;
183   widget_class = (GtkWidgetClass*) class;
184   container_class = (GtkContainerClass*) class;
185
186   parent_class = g_type_class_peek_parent (class);
187
188   gobject_class->finalize = gtk_socket_finalize;
189   gobject_class->notify = gtk_socket_notify;
190
191   widget_class->realize = gtk_socket_realize;
192   widget_class->unrealize = gtk_socket_unrealize;
193   widget_class->size_request = gtk_socket_size_request;
194   widget_class->size_allocate = gtk_socket_size_allocate;
195   widget_class->hierarchy_changed = gtk_socket_hierarchy_changed;
196   widget_class->grab_notify = gtk_socket_grab_notify;
197   widget_class->key_press_event = gtk_socket_key_press_event;
198   widget_class->focus = gtk_socket_focus;
199   
200   container_class->remove = gtk_socket_remove;
201   container_class->forall = gtk_socket_forall;
202
203   socket_signals[PLUG_ADDED] =
204     g_signal_new ("plug_added",
205                   G_OBJECT_CLASS_TYPE (class),
206                   G_SIGNAL_RUN_LAST,
207                   G_STRUCT_OFFSET (GtkSocketClass, plug_added),
208                   NULL, NULL,
209                   _gtk_marshal_VOID__VOID,
210                   G_TYPE_NONE, 0);
211   socket_signals[PLUG_REMOVED] =
212     g_signal_new ("plug_removed",
213                   G_OBJECT_CLASS_TYPE (class),
214                   G_SIGNAL_RUN_LAST,
215                   G_STRUCT_OFFSET (GtkSocketClass, plug_removed),
216                   _gtk_boolean_handled_accumulator, NULL,
217                   _gtk_marshal_BOOLEAN__VOID,
218                   G_TYPE_BOOLEAN, 0);
219 }
220
221 static void
222 gtk_socket_init (GtkSocket *socket)
223 {
224   socket->request_width = 0;
225   socket->request_height = 0;
226   socket->current_width = 0;
227   socket->current_height = 0;
228   
229   socket->plug_window = NULL;
230   socket->plug_widget = NULL;
231   socket->focus_in = FALSE;
232   socket->have_size = FALSE;
233   socket->need_map = FALSE;
234   socket->active = FALSE;
235
236   socket->accel_group = gtk_accel_group_new ();
237   g_object_set_data (G_OBJECT (socket->accel_group), "gtk-socket", socket);
238 }
239
240 /**
241  * gtk_socket_new:
242  * 
243  * Create a new empty #GtkSocket.
244  * 
245  * Return value:  the new #GtkSocket.
246  **/
247 GtkWidget*
248 gtk_socket_new (void)
249 {
250   GtkSocket *socket;
251
252   socket = g_object_new (GTK_TYPE_SOCKET, NULL);
253
254   return GTK_WIDGET (socket);
255 }
256
257 /**
258  * gtk_socket_steal:
259  * @socket_: a #GtkSocket
260  * @wid: the window ID of an existing toplevel window.
261  * 
262  * Reparents a pre-existing toplevel window into a #GtkSocket. This is
263  * meant to embed clients that do not know about embedding into a
264  * #GtkSocket, however doing so is inherently unreliable, and using
265  * this function is not recommended.
266  *
267  * The #GtkSocket must have already be added into a toplevel window
268  *  before you can make this call.
269  **/
270 void           
271 gtk_socket_steal (GtkSocket *socket, GdkNativeWindow wid)
272 {
273   g_return_if_fail (GTK_IS_SOCKET (socket));
274   g_return_if_fail (GTK_WIDGET_ANCHORED (socket));
275
276   if (!GTK_WIDGET_REALIZED (socket))
277     gtk_widget_realize (GTK_WIDGET (socket));
278
279   gtk_socket_add_window (socket, wid, TRUE);
280 }
281
282 /**
283  * gtk_socket_add_id:
284  * @socket_: a #GtkSocket
285  * @window_id: the window ID of a client participating in the XEMBED protocol.
286  *
287  * Adds an XEMBED client, such as a #GtkPlug, to the #GtkSocket.  The
288  * client may be in the same process or in a different process. 
289  * 
290  * To embed a #GtkPlug in a #GtkSocket, you can either create the
291  * #GtkPlug with <literal>gtk_plug_new (0)</literal>, call 
292  * gtk_plug_get_id() to get the window ID of the plug, and then pass that to the
293  * gtk_socket_add_id(), or you can call gtk_socket_get_id() to get the
294  * window ID for the socket, and call gtk_plug_new() passing in that
295  * ID.
296  *
297  * The #GtkSocket must have already be added into a toplevel window
298  *  before you can make this call.
299  **/
300 void           
301 gtk_socket_add_id (GtkSocket *socket, GdkNativeWindow window_id)
302 {
303   g_return_if_fail (GTK_IS_SOCKET (socket));
304   g_return_if_fail (GTK_WIDGET_ANCHORED (socket));
305
306   if (!GTK_WIDGET_REALIZED (socket))
307     gtk_widget_realize (GTK_WIDGET (socket));
308
309   gtk_socket_add_window (socket, window_id, TRUE);
310 }
311
312 /**
313  * gtk_socket_get_id:
314  * @socket_: a #GtkSocket.
315  * 
316  * Gets the window ID of a #GtkSocket widget, which can then
317  * be used to create a client embedded inside the socket, for
318  * instance with gtk_plug_new(). 
319  *
320  * The #GtkSocket must have already be added into a toplevel window 
321  * before you can make this call.
322  * 
323  * Return value: the window ID for the socket
324  **/
325 GdkNativeWindow
326 gtk_socket_get_id (GtkSocket *socket)
327 {
328   g_return_val_if_fail (GTK_IS_SOCKET (socket), 0);
329   g_return_val_if_fail (GTK_WIDGET_ANCHORED (socket), 0);
330
331   if (!GTK_WIDGET_REALIZED (socket))
332     gtk_widget_realize (GTK_WIDGET (socket));
333
334   return GDK_WINDOW_XWINDOW (GTK_WIDGET (socket)->window);
335 }
336
337 static void
338 gtk_socket_realize (GtkWidget *widget)
339 {
340   GtkSocket *socket;
341   GdkWindowAttr attributes;
342   gint attributes_mask;
343   XWindowAttributes xattrs;
344
345   g_return_if_fail (GTK_IS_SOCKET (widget));
346
347   socket = GTK_SOCKET (widget);
348   GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
349
350   attributes.window_type = GDK_WINDOW_CHILD;
351   attributes.x = widget->allocation.x;
352   attributes.y = widget->allocation.y;
353   attributes.width = widget->allocation.width;
354   attributes.height = widget->allocation.height;
355   attributes.wclass = GDK_INPUT_OUTPUT;
356   attributes.visual = gtk_widget_get_visual (widget);
357   attributes.colormap = gtk_widget_get_colormap (widget);
358   attributes.event_mask = GDK_FOCUS_CHANGE_MASK;
359
360   attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
361
362   widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), 
363                                    &attributes, attributes_mask);
364   gdk_window_set_user_data (widget->window, socket);
365
366   widget->style = gtk_style_attach (widget->style, widget->window);
367   gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
368
369   XGetWindowAttributes (GDK_WINDOW_XDISPLAY (widget->window),
370                         GDK_WINDOW_XWINDOW (widget->window),
371                         &xattrs);
372
373   XSelectInput (GDK_WINDOW_XDISPLAY (widget->window),
374                 GDK_WINDOW_XWINDOW (widget->window), 
375                 xattrs.your_event_mask | 
376                 SubstructureNotifyMask | SubstructureRedirectMask);
377
378   gdk_window_add_filter (widget->window, gtk_socket_filter_func, widget);
379
380   GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
381
382   /* We sync here so that we make sure that if the XID for
383    * our window is passed to another application, SubstructureRedirectMask
384    * will be set by the time the other app creates its window.
385    */
386   gdk_display_sync (gtk_widget_get_display (widget));
387 }
388
389 static void
390 gtk_socket_end_embedding (GtkSocket *socket)
391 {
392   GtkSocketPrivate *private = gtk_socket_get_private (socket);
393   GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (socket));
394   gint i;
395   
396   if (toplevel && GTK_IS_WINDOW (toplevel))
397     gtk_window_remove_embedded_xid (GTK_WINDOW (toplevel), 
398                                     GDK_WINDOW_XWINDOW (socket->plug_window));
399
400   g_object_unref (socket->plug_window);
401   socket->plug_window = NULL;
402   private->resize_count = 0;
403
404   /* Remove from end to avoid indexes shifting. This is evil */
405   for (i = socket->accel_group->n_accels - 1; i >= 0; i--)
406     {
407       GtkAccelGroupEntry *accel_entry = &socket->accel_group->priv_accels[i];
408       gtk_accel_group_disconnect (socket->accel_group, accel_entry->closure);
409     }
410 }
411
412 static void
413 gtk_socket_unrealize (GtkWidget *widget)
414 {
415   GtkSocket *socket = GTK_SOCKET (widget);
416
417   GTK_WIDGET_UNSET_FLAGS (widget, GTK_REALIZED);
418
419   if (socket->plug_widget)
420     {
421       _gtk_plug_remove_from_socket (GTK_PLUG (socket->plug_widget), socket);
422     }
423   else if (socket->plug_window)
424     {
425       gtk_socket_end_embedding (socket);
426     }
427
428   if (GTK_WIDGET_CLASS (parent_class)->unrealize)
429     (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
430 }
431   
432 static void 
433 gtk_socket_size_request (GtkWidget      *widget,
434                          GtkRequisition *requisition)
435 {
436   GtkSocket *socket = GTK_SOCKET (widget);
437
438   if (socket->plug_widget)
439     {
440       gtk_widget_size_request (socket->plug_widget, requisition);
441     }
442   else
443     {
444       if (socket->is_mapped && !socket->have_size && socket->plug_window)
445         {
446           XSizeHints hints;
447           long supplied;
448           
449           gdk_error_trap_push ();
450
451           socket->request_width = 1;
452           socket->request_height = 1;
453           
454           if (XGetWMNormalHints (GDK_WINDOW_XDISPLAY (socket->plug_window),
455                                  GDK_WINDOW_XWINDOW (socket->plug_window),
456                                  &hints, &supplied))
457             {
458               if (hints.flags & PMinSize)
459                 {
460                   socket->request_width = hints.min_width;
461                   socket->request_height = hints.min_height;
462                 }
463               else if (hints.flags & PBaseSize)
464                 {
465                   socket->request_width = hints.base_width;
466                   socket->request_height = hints.base_height;
467                 }
468             }
469           socket->have_size = TRUE;
470           
471           gdk_error_trap_pop ();
472         }
473
474       if (socket->is_mapped && socket->have_size)
475         {
476           requisition->width = MAX (socket->request_width, 1);
477           requisition->height = MAX (socket->request_height, 1);
478         }
479       else
480         {
481           requisition->width = 1;
482           requisition->height = 1;
483         }
484     }
485 }
486
487 static void
488 gtk_socket_size_allocate (GtkWidget     *widget,
489                           GtkAllocation *allocation)
490 {
491   GtkSocket *socket;
492
493   g_return_if_fail (GTK_IS_SOCKET (widget));
494   g_return_if_fail (allocation != NULL);
495
496   socket = GTK_SOCKET (widget);
497
498   widget->allocation = *allocation;
499   if (GTK_WIDGET_REALIZED (widget))
500     {
501       gdk_window_move_resize (widget->window,
502                               allocation->x, allocation->y,
503                               allocation->width, allocation->height);
504
505       if (socket->plug_widget)
506         {
507           GtkAllocation child_allocation;
508
509           child_allocation.x = 0;
510           child_allocation.y = 0;
511           child_allocation.width = allocation->width;
512           child_allocation.height = allocation->height;
513
514           gtk_widget_size_allocate (socket->plug_widget, &child_allocation);
515         }
516       else if (socket->plug_window)
517         {
518           GtkSocketPrivate *private = gtk_socket_get_private (socket);
519           
520           gdk_error_trap_push ();
521           
522           if (allocation->width != socket->current_width ||
523               allocation->height != socket->current_height)
524             {
525               gdk_window_move_resize (socket->plug_window,
526                                       0, 0,
527                                       allocation->width, allocation->height);
528               if (private->resize_count)
529                 private->resize_count--;
530               
531               GTK_NOTE(PLUGSOCKET,
532                        g_message ("GtkSocket - allocated: %d %d",
533                                   allocation->width, allocation->height));
534               socket->current_width = allocation->width;
535               socket->current_height = allocation->height;
536             }
537
538           if (socket->need_map)
539             {
540               gdk_window_show (socket->plug_window);
541               socket->need_map = FALSE;
542             }
543
544           while (private->resize_count)
545             {
546               gtk_socket_send_configure_event (socket);
547               private->resize_count--;
548               GTK_NOTE(PLUGSOCKET,
549                        g_message ("GtkSocket - sending synthetic configure: %d %d",
550                                   allocation->width, allocation->height));
551             }
552           
553           gdk_display_sync (gtk_widget_get_display (widget));
554           gdk_error_trap_pop ();
555         }
556     }
557 }
558
559 typedef struct
560 {
561   guint                  accel_key;
562   GdkModifierType        accel_mods;
563 } GrabbedKey;
564
565 static void
566 activate_key (GtkAccelGroup *accel_group,
567               GrabbedKey    *grabbed_key)
568 {
569   XEvent xevent;
570   GdkEvent *gdk_event = gtk_get_current_event ();
571   
572   GtkSocket *socket = g_object_get_data (G_OBJECT (accel_group), "gtk-socket");
573   GdkScreen *screen = gdk_drawable_get_screen (socket->plug_window);
574
575   if (gdk_event && gdk_event->type == GDK_KEY_PRESS && socket->plug_window)
576     {
577       xevent.xkey.type = KeyPress;
578       xevent.xkey.window = GDK_WINDOW_XWINDOW (socket->plug_window);
579       xevent.xkey.root = GDK_WINDOW_XWINDOW (gdk_screen_get_root_window (screen));
580       xevent.xkey.subwindow = None;
581       xevent.xkey.time = gdk_event->key.time;
582       xevent.xkey.x = 0;
583       xevent.xkey.y = 0;
584       xevent.xkey.x_root = 0;
585       xevent.xkey.y_root = 0;
586       xevent.xkey.state = gdk_event->key.state;
587       xevent.xkey.keycode = gdk_event->key.hardware_keycode;
588       xevent.xkey.same_screen = True;
589
590       gdk_error_trap_push ();
591       XSendEvent (GDK_WINDOW_XDISPLAY (socket->plug_window),
592                   GDK_WINDOW_XWINDOW (socket->plug_window),
593                   False, KeyPressMask, &xevent);
594       gdk_display_sync (gdk_screen_get_display (screen));
595       gdk_error_trap_pop ();
596     }
597
598   if (gdk_event)
599     gdk_event_free (gdk_event);
600 }
601
602 static gboolean
603 find_accel_key (GtkAccelKey *key,
604                 GClosure    *closure,
605                 gpointer     data)
606 {
607   GrabbedKey *grabbed_key = data;
608   
609   return (key->accel_key == grabbed_key->accel_key &&
610           key->accel_mods == grabbed_key->accel_mods);
611 }
612
613 static void
614 add_grabbed_key (GtkSocket       *socket,
615                  guint            keyval,
616                  GdkModifierType  modifiers)
617 {
618   GClosure *closure;
619   GrabbedKey *grabbed_key;
620
621   grabbed_key = g_new (GrabbedKey, 1);
622   
623   grabbed_key->accel_key = keyval;
624   grabbed_key->accel_mods = modifiers;
625
626   if (gtk_accel_group_find (socket->accel_group,
627                             find_accel_key,
628                             &grabbed_key))
629     {
630       g_warning ("GtkSocket: request to add already present grabbed key %u,%#x\n",
631                  keyval, modifiers);
632       g_free (grabbed_key);
633       return;
634     }
635
636   closure = g_cclosure_new (G_CALLBACK (activate_key), grabbed_key, (GClosureNotify)g_free);
637
638   gtk_accel_group_connect (socket->accel_group, keyval, modifiers, GTK_ACCEL_LOCKED,
639                            closure);
640 }
641
642 static void
643 remove_grabbed_key (GtkSocket      *socket,
644                     guint           keyval,
645                     GdkModifierType modifiers)
646 {
647   gint i;
648
649   for (i = 0; i < socket->accel_group->n_accels; i++)
650     {
651       GtkAccelGroupEntry *accel_entry = &socket->accel_group->priv_accels[i];
652       if (accel_entry->key.accel_key == keyval &&
653           accel_entry->key.accel_mods == modifiers)
654         {
655           gtk_accel_group_disconnect (socket->accel_group,
656                                       accel_entry->closure);
657           return;
658         }
659     }
660
661   g_warning ("GtkSocket: request to remove non-present grabbed key %u,%#x\n",
662              keyval, modifiers);
663 }
664
665 static void
666 socket_update_focus_in (GtkSocket *socket)
667 {
668   gboolean focus_in = FALSE;
669
670   if (socket->plug_window)
671     {
672       GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (socket));
673       if (GTK_WIDGET_TOPLEVEL (toplevel) &&
674           GTK_WINDOW (toplevel)->has_toplevel_focus &&
675           gtk_widget_is_focus (GTK_WIDGET (socket)))
676         focus_in = TRUE;
677     }
678
679   if (focus_in != socket->focus_in)
680     {
681       socket->focus_in = focus_in;
682
683       if (focus_in)
684         {
685           send_xembed_message (socket, XEMBED_FOCUS_IN, XEMBED_FOCUS_CURRENT, 0, 0,
686                                gtk_get_current_event_time ());
687         }
688       else
689         {
690           send_xembed_message (socket, XEMBED_FOCUS_OUT, 0, 0, 0,
691                                gtk_get_current_event_time ());
692       
693         }
694     }
695 }
696
697 static void
698 socket_update_active (GtkSocket *socket)
699 {
700   gboolean active = FALSE;
701
702   if (socket->plug_window)
703     {
704       GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (socket));
705       if (GTK_WIDGET_TOPLEVEL (toplevel) &&
706           GTK_WINDOW (toplevel)->is_active)
707         active = TRUE;
708     }
709
710   if (active != socket->active)
711     {
712       socket->active = active;
713
714       send_xembed_message (socket,
715                            active ? XEMBED_WINDOW_ACTIVATE : XEMBED_WINDOW_DEACTIVATE,
716                            0, 0, 0,
717                            gtk_get_current_event_time ());
718     }
719 }
720
721 static void
722 gtk_socket_hierarchy_changed (GtkWidget *widget,
723                               GtkWidget *old_toplevel)
724 {
725   GtkSocket *socket = GTK_SOCKET (widget);
726   GtkWidget *toplevel = gtk_widget_get_toplevel (widget);
727
728   if (toplevel && !GTK_IS_WINDOW (toplevel))
729     toplevel = NULL;
730
731   if (toplevel != socket->toplevel)
732     {
733       if (socket->toplevel)
734         {
735           gtk_window_remove_accel_group (GTK_WINDOW (socket->toplevel), socket->accel_group);
736           g_signal_handlers_disconnect_by_func (socket->toplevel,
737                                                 socket_update_focus_in,
738                                                 socket);
739           g_signal_handlers_disconnect_by_func (socket->toplevel,
740                                                 socket_update_active,
741                                                 socket);
742         }
743
744       socket->toplevel = toplevel;
745
746       if (toplevel)
747         {
748           gtk_window_add_accel_group (GTK_WINDOW (socket->toplevel), socket->accel_group);
749           g_signal_connect_swapped (socket->toplevel, "notify::has-toplevel-focus",
750                                     G_CALLBACK (socket_update_focus_in), socket);
751           g_signal_connect_swapped (socket->toplevel, "notify::is-active",
752                                     G_CALLBACK (socket_update_active), socket);
753         }
754
755       socket_update_focus_in (socket);
756       socket_update_active (socket);
757     }
758 }
759
760 static void
761 gtk_socket_grab_notify (GtkWidget *widget,
762                         gboolean   was_grabbed)
763 {
764   GtkSocket *socket = GTK_SOCKET (widget);
765
766   if (!socket->same_app)
767     send_xembed_message (GTK_SOCKET (widget),
768                          was_grabbed ? XEMBED_MODALITY_OFF : XEMBED_MODALITY_ON,
769                          0, 0, 0, gtk_get_current_event_time ());
770 }
771
772 static gboolean
773 gtk_socket_key_press_event (GtkWidget   *widget,
774                             GdkEventKey *event)
775 {
776   GtkSocket *socket = GTK_SOCKET (widget);
777   
778   if (GTK_WIDGET_HAS_FOCUS (socket) && socket->plug_window && !socket->plug_widget)
779     {
780       GdkScreen *screen = gdk_drawable_get_screen (socket->plug_window);
781       XEvent xevent;
782       
783       xevent.xkey.type = KeyPress;
784       xevent.xkey.window = GDK_WINDOW_XWINDOW (socket->plug_window);
785       xevent.xkey.root = GDK_WINDOW_XWINDOW (gdk_screen_get_root_window (screen));
786       xevent.xkey.time = event->time;
787       /* FIXME, the following might cause problems for non-GTK apps */
788       xevent.xkey.x = 0;
789       xevent.xkey.y = 0;
790       xevent.xkey.x_root = 0;
791       xevent.xkey.y_root = 0;
792       xevent.xkey.state = event->state;
793       xevent.xkey.keycode = event->hardware_keycode;
794       xevent.xkey.same_screen = TRUE; /* FIXME ? */
795       
796       gdk_error_trap_push ();
797       XSendEvent (GDK_WINDOW_XDISPLAY (socket->plug_window),
798                   GDK_WINDOW_XWINDOW (socket->plug_window),
799                   False, NoEventMask, &xevent);
800       gdk_display_sync (gtk_widget_get_display (widget));
801       gdk_error_trap_pop ();
802       
803       return TRUE;
804     }
805   else
806     return FALSE;
807 }
808
809 static void
810 gtk_socket_notify (GObject    *object,
811                    GParamSpec *pspec)
812 {
813   if (!strcmp (pspec->name, "is_focus"))
814     return;
815
816   socket_update_focus_in (GTK_SOCKET (object));
817 }
818
819 static void
820 gtk_socket_claim_focus (GtkSocket *socket,
821                         gboolean   send_event)
822 {
823   if (!send_event)
824     socket->focus_in = TRUE;    /* Otherwise, our notify handler will send FOCUS_IN  */
825       
826   /* Oh, the trickery... */
827   
828   GTK_WIDGET_SET_FLAGS (socket, GTK_CAN_FOCUS);
829   gtk_widget_grab_focus (GTK_WIDGET (socket));
830   GTK_WIDGET_UNSET_FLAGS (socket, GTK_CAN_FOCUS);
831 }
832
833 static gboolean
834 gtk_socket_focus (GtkWidget *widget, GtkDirectionType direction)
835 {
836   GtkSocket *socket;
837   gint detail = -1;
838
839   g_return_val_if_fail (GTK_IS_SOCKET (widget), FALSE);
840   
841   socket = GTK_SOCKET (widget);
842
843   if (socket->plug_widget)
844     return gtk_widget_child_focus (socket->plug_widget, direction);
845
846   if (!GTK_WIDGET_HAS_FOCUS (widget))
847     {
848       switch (direction)
849         {
850         case GTK_DIR_UP:
851         case GTK_DIR_LEFT:
852         case GTK_DIR_TAB_BACKWARD:
853           detail = XEMBED_FOCUS_LAST;
854           break;
855         case GTK_DIR_DOWN:
856         case GTK_DIR_RIGHT:
857         case GTK_DIR_TAB_FORWARD:
858           detail = XEMBED_FOCUS_FIRST;
859           break;
860         }
861       
862       send_xembed_message (socket, XEMBED_FOCUS_IN, detail, 0, 0,
863                            gtk_get_current_event_time ());
864
865       gtk_socket_claim_focus (socket, FALSE);
866  
867       return TRUE;
868     }
869   else
870     return FALSE;
871 }
872
873 static void
874 gtk_socket_remove (GtkContainer *container,
875                    GtkWidget    *child)
876 {
877   GtkSocket *socket = GTK_SOCKET (container);
878
879   g_return_if_fail (child == socket->plug_widget);
880
881   _gtk_plug_remove_from_socket (GTK_PLUG (socket->plug_widget), socket);
882 }
883
884 static void
885 gtk_socket_forall (GtkContainer *container,
886                    gboolean      include_internals,
887                    GtkCallback   callback,
888                    gpointer      callback_data)
889 {
890   GtkSocket *socket = GTK_SOCKET (container);
891
892   if (socket->plug_widget)
893     (* callback) (socket->plug_widget, callback_data);
894 }
895
896 static void
897 gtk_socket_send_configure_event (GtkSocket *socket)
898 {
899   XEvent event;
900   gint x, y;
901
902   g_return_if_fail (socket->plug_window != NULL);
903
904   event.xconfigure.type = ConfigureNotify;
905
906   event.xconfigure.event = GDK_WINDOW_XWINDOW (socket->plug_window);
907   event.xconfigure.window = GDK_WINDOW_XWINDOW (socket->plug_window);
908
909   /* The ICCCM says that synthetic events should have root relative
910    * coordinates. We still aren't really ICCCM compliant, since
911    * we don't send events when the real toplevel is moved.
912    */
913   gdk_error_trap_push ();
914   gdk_window_get_origin (socket->plug_window, &x, &y);
915   gdk_error_trap_pop ();
916                          
917   event.xconfigure.x = x;
918   event.xconfigure.y = y;
919   event.xconfigure.width = GTK_WIDGET(socket)->allocation.width;
920   event.xconfigure.height = GTK_WIDGET(socket)->allocation.height;
921
922   event.xconfigure.border_width = 0;
923   event.xconfigure.above = None;
924   event.xconfigure.override_redirect = False;
925
926   gdk_error_trap_push ();
927   XSendEvent (GDK_WINDOW_XDISPLAY (socket->plug_window),
928               GDK_WINDOW_XWINDOW (socket->plug_window),
929               False, NoEventMask, &event);
930   gdk_display_sync (gtk_widget_get_display (GTK_WIDGET (socket)));
931   gdk_error_trap_pop ();
932 }
933
934 static void
935 gtk_socket_add_window (GtkSocket        *socket,
936                        GdkNativeWindow   xid,
937                        gboolean          need_reparent)
938 {
939   GtkWidget *widget = GTK_WIDGET (socket);
940   GdkDisplay *display = gtk_widget_get_display (widget);
941   gpointer user_data = NULL;
942   
943   socket->plug_window = gdk_window_lookup_for_display (display, xid);
944
945   if (socket->plug_window)
946     {
947       g_object_ref (socket->plug_window);
948       gdk_window_get_user_data (socket->plug_window, &user_data);
949     }
950
951   if (user_data)                /* A widget's window in this process */
952     {
953       GtkWidget *child_widget = user_data;
954
955       if (!GTK_IS_PLUG (child_widget))
956         {
957           g_warning (G_STRLOC "Can't add non-GtkPlug to GtkSocket");
958           socket->plug_window = NULL;
959           gdk_error_trap_pop ();
960           
961           return;
962         }
963
964       _gtk_plug_add_to_socket (GTK_PLUG (child_widget), socket);
965     }
966   else                          /* A foreign window */
967     {
968       GtkWidget *toplevel;
969       GdkDragProtocol protocol;
970       unsigned long version;
971       unsigned long flags;
972
973       gdk_error_trap_push ();
974
975       if (!socket->plug_window)
976         {  
977           socket->plug_window = gdk_window_foreign_new_for_display (display, xid);
978           if (!socket->plug_window) /* was deleted before we could get it */
979             {
980               gdk_error_trap_pop ();
981               return;
982             }
983         }
984         
985       XSelectInput (GDK_DISPLAY_XDISPLAY (display),
986                     GDK_WINDOW_XWINDOW (socket->plug_window),
987                     StructureNotifyMask | PropertyChangeMask);
988       
989       if (gdk_error_trap_pop ())
990         {
991           g_object_unref (socket->plug_window);
992           socket->plug_window = NULL;
993           return;
994         }
995       
996       /* OK, we now will reliably get destroy notification on socket->plug_window */
997
998       gdk_error_trap_push ();
999
1000       if (need_reparent)
1001         {
1002           gdk_window_hide (socket->plug_window); /* Shouldn't actually be necessary for XEMBED, but just in case */
1003           gdk_window_reparent (socket->plug_window, widget->window, 0, 0);
1004         }
1005
1006       socket->have_size = FALSE;
1007
1008       socket->xembed_version = -1;
1009       if (xembed_get_info (socket->plug_window, &version, &flags))
1010         {
1011           socket->xembed_version = version;
1012           socket->is_mapped = (flags & XEMBED_MAPPED) != 0;
1013         }
1014       else
1015         {
1016           /* FIXME, we should probably actually check the state before we started */
1017           
1018           socket->is_mapped = TRUE;
1019         }
1020       
1021       socket->need_map = socket->is_mapped;
1022
1023       if (gdk_drag_get_protocol_for_display (display, xid, &protocol))
1024         gtk_drag_dest_set_proxy (GTK_WIDGET (socket), socket->plug_window, 
1025                                  protocol, TRUE);
1026
1027       gdk_display_sync (display);
1028       gdk_error_trap_pop ();
1029
1030       gdk_window_add_filter (socket->plug_window, 
1031                              gtk_socket_filter_func, socket);
1032
1033       /* Add a pointer to the socket on our toplevel window */
1034
1035       toplevel = gtk_widget_get_toplevel (GTK_WIDGET (socket));
1036       if (toplevel && GTK_IS_WINDOW (toplevel))
1037         gtk_window_add_embedded_xid (GTK_WINDOW (toplevel), xid);
1038
1039       send_xembed_message (socket, XEMBED_EMBEDDED_NOTIFY, 0, 0, 0,
1040                            gtk_get_current_event_time ());
1041       socket_update_active (socket);
1042       socket_update_focus_in (socket);
1043
1044       gtk_widget_queue_resize (GTK_WIDGET (socket));
1045     }
1046
1047   if (socket->plug_window)
1048     g_signal_emit (socket, socket_signals[PLUG_ADDED], 0);
1049 }
1050
1051
1052 static void
1053 send_xembed_message (GtkSocket *socket,
1054                      glong      message,
1055                      glong      detail,
1056                      glong      data1,
1057                      glong      data2,
1058                      guint32    time)
1059 {
1060   GTK_NOTE(PLUGSOCKET,
1061          g_message ("GtkSocket: Sending XEMBED message of type %ld", message));
1062   
1063   if (socket->plug_window)
1064     {
1065       GdkDisplay *display = gtk_widget_get_display (GTK_WIDGET (socket));
1066       XEvent xevent;
1067
1068       xevent.xclient.window = GDK_WINDOW_XWINDOW (socket->plug_window);
1069       xevent.xclient.type = ClientMessage;
1070       xevent.xclient.message_type = gdk_x11_get_xatom_by_name_for_display (display, "_XEMBED");
1071       xevent.xclient.format = 32;
1072       xevent.xclient.data.l[0] = time;
1073       xevent.xclient.data.l[1] = message;
1074       xevent.xclient.data.l[2] = detail;
1075       xevent.xclient.data.l[3] = data1;
1076       xevent.xclient.data.l[4] = data2;
1077
1078       gdk_error_trap_push ();
1079       XSendEvent (GDK_DISPLAY_XDISPLAY (display),
1080                   GDK_WINDOW_XWINDOW (socket->plug_window),
1081                   False, NoEventMask, &xevent);
1082       gdk_display_sync (display);
1083       gdk_error_trap_pop ();
1084     }
1085 }
1086
1087 static gboolean
1088 xembed_get_info (GdkWindow     *window,
1089                  unsigned long *version,
1090                  unsigned long *flags)
1091 {
1092   GdkDisplay *display = gdk_drawable_get_display (window);
1093   Atom xembed_info_atom = gdk_x11_get_xatom_by_name_for_display (display, "_XEMBED_INFO");
1094   Atom type;
1095   int format;
1096   unsigned long nitems, bytes_after;
1097   unsigned char *data;
1098   unsigned long *data_long;
1099   int status;
1100   
1101   gdk_error_trap_push();
1102   status = XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display),
1103                                GDK_WINDOW_XWINDOW (window),
1104                                xembed_info_atom,
1105                                0, 2, False,
1106                                xembed_info_atom, &type, &format,
1107                                &nitems, &bytes_after, &data);
1108   gdk_error_trap_pop();
1109
1110   if (status != Success)
1111     return FALSE;               /* Window vanished? */
1112
1113   if (type == None)             /* No info property */
1114     return FALSE;
1115
1116   if (type != xembed_info_atom)
1117     {
1118       g_warning ("_XEMBED_INFO property has wrong type\n");
1119       return FALSE;
1120     }
1121   
1122   if (nitems < 2)
1123     {
1124       g_warning ("_XEMBED_INFO too short\n");
1125       XFree (data);
1126       return FALSE;
1127     }
1128   
1129   data_long = (unsigned long *)data;
1130   if (version)
1131     *version = data_long[0];
1132   if (flags)
1133     *flags = data_long[1] & XEMBED_MAPPED;
1134   
1135   XFree (data);
1136   return TRUE;
1137 }
1138
1139 static void
1140 handle_xembed_message (GtkSocket *socket,
1141                        glong      message,
1142                        glong      detail,
1143                        glong      data1,
1144                        glong      data2,
1145                        guint32    time)
1146 {
1147   GTK_NOTE (PLUGSOCKET,
1148             g_message ("GtkSocket: Message of type %ld received", message));
1149   
1150   switch (message)
1151     {
1152     case XEMBED_EMBEDDED_NOTIFY:
1153     case XEMBED_WINDOW_ACTIVATE:
1154     case XEMBED_WINDOW_DEACTIVATE:
1155     case XEMBED_MODALITY_ON:
1156     case XEMBED_MODALITY_OFF:
1157     case XEMBED_FOCUS_IN:
1158     case XEMBED_FOCUS_OUT:
1159       g_warning ("GtkSocket: Invalid _XEMBED message of type %ld received", message);
1160       break;
1161       
1162     case XEMBED_REQUEST_FOCUS:
1163       gtk_socket_claim_focus (socket, TRUE);
1164       break;
1165
1166     case XEMBED_FOCUS_NEXT:
1167     case XEMBED_FOCUS_PREV:
1168       {
1169         GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (socket));
1170         if (toplevel)
1171           {
1172             gtk_widget_child_focus (toplevel,
1173                                     (message == XEMBED_FOCUS_NEXT ?
1174                                      GTK_DIR_TAB_FORWARD : GTK_DIR_TAB_BACKWARD));
1175           }
1176         break;
1177       }
1178       
1179     case XEMBED_GTK_GRAB_KEY:
1180       add_grabbed_key (socket, data1, data2);
1181       break; 
1182     case XEMBED_GTK_UNGRAB_KEY:
1183       remove_grabbed_key (socket, data1, data2);
1184       break;
1185
1186     case XEMBED_GRAB_KEY:
1187     case XEMBED_UNGRAB_KEY:
1188       break;
1189       
1190     default:
1191       GTK_NOTE (PLUGSOCKET,
1192                 g_message ("GtkSocket: Ignoring unknown _XEMBED message of type %ld", message));
1193       break;
1194     }
1195 }
1196
1197 static void
1198 map_request (GtkSocket *socket)
1199 {
1200   if (!socket->is_mapped)
1201     {
1202       socket->is_mapped = TRUE;
1203       socket->need_map = TRUE;
1204
1205       gtk_widget_queue_resize (GTK_WIDGET (socket));
1206     }
1207 }
1208
1209 static void
1210 unmap_notify (GtkSocket *socket)
1211 {
1212   if (socket->is_mapped)
1213     {
1214       socket->is_mapped = FALSE;
1215       gtk_widget_queue_resize (GTK_WIDGET (socket));
1216     }
1217 }
1218
1219 static GdkFilterReturn
1220 gtk_socket_filter_func (GdkXEvent *gdk_xevent, GdkEvent *event, gpointer data)
1221 {
1222   GtkSocket *socket;
1223   GtkWidget *widget;
1224   GdkDisplay *display;
1225   XEvent *xevent;
1226
1227   GdkFilterReturn return_val;
1228   
1229   socket = GTK_SOCKET (data);
1230   widget = GTK_WIDGET (socket);
1231   xevent = (XEvent *)gdk_xevent;
1232   display = gdk_drawable_get_display (event->any.window);
1233
1234   return_val = GDK_FILTER_CONTINUE;
1235
1236   if (socket->plug_widget)
1237     return return_val;
1238
1239   switch (xevent->type)
1240     {
1241     case ClientMessage:
1242       if (xevent->xclient.message_type == gdk_x11_get_xatom_by_name_for_display (display, "_XEMBED"))
1243         {
1244           handle_xembed_message (socket,
1245                                  xevent->xclient.data.l[1],
1246                                  xevent->xclient.data.l[2],
1247                                  xevent->xclient.data.l[3],
1248                                  xevent->xclient.data.l[4],
1249                                  xevent->xclient.data.l[0]);
1250           
1251           
1252           return_val = GDK_FILTER_REMOVE;
1253         }
1254       break;
1255
1256     case CreateNotify:
1257       {
1258         XCreateWindowEvent *xcwe = &xevent->xcreatewindow;
1259
1260         if (!socket->plug_window)
1261           {
1262             gtk_socket_add_window (socket, xcwe->window, FALSE);
1263
1264             if (socket->plug_window)
1265               {
1266                 GTK_NOTE(PLUGSOCKET,
1267                          g_message ("GtkSocket - window created"));
1268               }
1269           }
1270         
1271         return_val = GDK_FILTER_REMOVE;
1272         
1273         break;
1274       }
1275
1276     case ConfigureRequest:
1277       {
1278         XConfigureRequestEvent *xcre = &xevent->xconfigurerequest;
1279         
1280         if (!socket->plug_window)
1281           gtk_socket_add_window (socket, xcre->window, FALSE);
1282         
1283         if (socket->plug_window)
1284           {
1285             GtkSocketPrivate *private = gtk_socket_get_private (socket);
1286             
1287             if (xcre->value_mask & (CWWidth | CWHeight))
1288               {
1289                 GTK_NOTE(PLUGSOCKET,
1290                          g_message ("GtkSocket - configure request: %d %d",
1291                                     socket->request_width,
1292                                     socket->request_height));
1293
1294                 private->resize_count++;
1295                 gtk_widget_queue_resize (widget);
1296               }
1297             else if (xcre->value_mask & (CWX | CWY))
1298               {
1299                 gtk_socket_send_configure_event (socket);
1300               }
1301             /* Ignore stacking requests. */
1302             
1303             return_val = GDK_FILTER_REMOVE;
1304           }
1305         break;
1306       }
1307
1308     case DestroyNotify:
1309       {
1310         XDestroyWindowEvent *xdwe = &xevent->xdestroywindow;
1311
1312         /* Note that we get destroy notifies both from SubstructureNotify on
1313          * our window and StructureNotify on socket->plug_window
1314          */
1315         if (socket->plug_window && (xdwe->window == GDK_WINDOW_XWINDOW (socket->plug_window)))
1316           {
1317             gboolean result;
1318             
1319             GTK_NOTE(PLUGSOCKET,
1320                      g_message ("GtkSocket - destroy notify"));
1321             
1322             gdk_window_destroy_notify (socket->plug_window);
1323             gtk_socket_end_embedding (socket);
1324
1325             g_object_ref (widget);
1326             g_signal_emit (widget, socket_signals[PLUG_REMOVED], 0, &result);
1327             if (!result)
1328               gtk_widget_destroy (widget);
1329             g_object_unref (widget);
1330             
1331             return_val = GDK_FILTER_REMOVE;
1332           }
1333         break;
1334       }
1335
1336     case FocusIn:
1337       if (xevent->xfocus.mode == EMBEDDED_APP_WANTS_FOCUS)
1338         {
1339           gtk_socket_claim_focus (socket, TRUE);
1340         }
1341       return_val = GDK_FILTER_REMOVE;
1342       break;
1343     case FocusOut:
1344       return_val = GDK_FILTER_REMOVE;
1345       break;
1346     case MapRequest:
1347       if (!socket->plug_window)
1348         gtk_socket_add_window (socket, xevent->xmaprequest.window, FALSE);
1349         
1350       if (socket->plug_window)
1351         {
1352           GTK_NOTE(PLUGSOCKET,
1353                    g_message ("GtkSocket - Map Request"));
1354
1355           map_request (socket);
1356           return_val = GDK_FILTER_REMOVE;
1357         }
1358       break;
1359     case PropertyNotify:
1360       if (socket->plug_window &&
1361           xevent->xproperty.window == GDK_WINDOW_XWINDOW (socket->plug_window))
1362         {
1363           GdkDragProtocol protocol;
1364
1365           if (xevent->xproperty.atom == gdk_x11_get_xatom_by_name_for_display (display, "WM_NORMAL_HINTS"))
1366             {
1367               socket->have_size = FALSE;
1368               gtk_widget_queue_resize (widget);
1369             }
1370           else if ((xevent->xproperty.atom == gdk_x11_get_xatom_by_name_for_display (display, "XdndAware")) ||
1371               (xevent->xproperty.atom == gdk_x11_get_xatom_by_name_for_display (display, "_MOTIF_DRAG_RECEIVER_INFO")))
1372             {
1373               gdk_error_trap_push ();
1374               if (gdk_drag_get_protocol_for_display (display,
1375                                                      xevent->xproperty.window,
1376                                                      &protocol))
1377                 gtk_drag_dest_set_proxy (GTK_WIDGET (socket),
1378                                          socket->plug_window,
1379                                          protocol, TRUE);
1380
1381               gdk_display_sync (display);
1382               gdk_error_trap_pop ();
1383             }
1384           else if (xevent->xproperty.atom == gdk_x11_get_xatom_by_name_for_display (display, "_XEMBED_INFO"))
1385             {
1386               unsigned long flags;
1387               
1388               if (xembed_get_info (socket->plug_window, NULL, &flags))
1389                 {
1390                   gboolean was_mapped = socket->is_mapped;
1391                   gboolean is_mapped = (flags & XEMBED_MAPPED) != 0;
1392
1393                   if (was_mapped != is_mapped)
1394                     {
1395                       if (is_mapped)
1396                         map_request (socket);
1397                       else
1398                         {
1399                           gdk_error_trap_push ();
1400                           gdk_window_show (socket->plug_window);
1401                           gdk_flush ();
1402                           gdk_error_trap_pop ();
1403                           
1404                           unmap_notify (socket);
1405                         }
1406                     }
1407                 }
1408             }
1409                    
1410           return_val = GDK_FILTER_REMOVE;
1411         }
1412       break;
1413     case ReparentNotify:
1414       {
1415         XReparentEvent *xre = &xevent->xreparent;
1416
1417         if (!socket->plug_window && xre->parent == GDK_WINDOW_XWINDOW (widget->window))
1418           {
1419             gtk_socket_add_window (socket, xre->window, FALSE);
1420             
1421             if (socket->plug_window)
1422               {
1423                 GTK_NOTE(PLUGSOCKET,
1424                          g_message ("GtkSocket - window reparented"));
1425               }
1426             
1427             return_val = GDK_FILTER_REMOVE;
1428           }
1429         
1430         break;
1431       }
1432     case UnmapNotify:
1433       if (socket->plug_window &&
1434           xevent->xunmap.window == GDK_WINDOW_XWINDOW (socket->plug_window))
1435         {
1436           GTK_NOTE(PLUGSOCKET,
1437                    g_message ("GtkSocket - Unmap notify"));
1438
1439           unmap_notify (socket);
1440           return_val = GDK_FILTER_REMOVE;
1441         }
1442       break;
1443       
1444     }
1445   
1446   return return_val;
1447 }