]> Pileus Git - ~andy/gtk/blob - gtk/gtksocket.c
879406153a274f31bee51bfd4b8177b690233474
[~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 "config.h"
29 #include <string.h>
30
31 #include "gdk/gdkkeysyms.h"
32 #include "gtkmain.h"
33 #include "gtkmarshalers.h"
34 #include "gtkwindow.h"
35 #include "gtkplug.h"
36 #include "gtkprivate.h"
37 #include "gtksocket.h"
38 #include "gtksocketprivate.h"
39 #include "gtkdnd.h"
40 #include "gtkintl.h"
41
42
43 /**
44  * SECTION:gtksocket
45  * @Short_description: Container for widgets from other processes
46  * @Title: GtkSocket
47  * @See_also: #GtkPlug, <ulink url="http://www.freedesktop.org/Standards/xembed-spec">XEmbed</ulink>
48  *
49  * Together with #GtkPlug, #GtkSocket provides the ability
50  * to embed widgets from one process into another process
51  * in a fashion that is transparent to the user. One
52  * process creates a #GtkSocket widget and passes
53  * that widget's window ID to the other process,
54  * which then creates a #GtkPlug with that window ID.
55  * Any widgets contained in the #GtkPlug then will appear
56  * inside the first application's window.
57  *
58  * The socket's window ID is obtained by using
59  * gtk_socket_get_id(). Before using this function,
60  * the socket must have been realized, and for hence,
61  * have been added to its parent.
62  *
63  * <example>
64  * <title>Obtaining the window ID of a socket.</title>
65  * <programlisting>
66  * GtkWidget *socket = gtk_socket_new (<!-- -->);
67  * gtk_widget_show (socket);
68  * gtk_container_add (GTK_CONTAINER (parent), socket);
69  *
70  * /<!---->* The following call is only necessary if one of
71  *  * the ancestors of the socket is not yet visible.
72  *  *<!---->/
73  * gtk_widget_realize (socket);
74  * g_print ("The ID of the sockets window is %#x\n",
75  *          gtk_socket_get_id (socket));
76  * </programlisting>
77  * </example>
78  *
79  * Note that if you pass the window ID of the socket to another
80  * process that will create a plug in the socket, you
81  * must make sure that the socket widget is not destroyed
82  * until that plug is created. Violating this rule will
83  * cause unpredictable consequences, the most likely
84  * consequence being that the plug will appear as a
85  * separate toplevel window. You can check if the plug
86  * has been created by using gtk_socket_get_plug_window(). If
87  * it returns a non-%NULL value, then the plug has been
88  * successfully created inside of the socket.
89  *
90  * When GTK+ is notified that the embedded window has been
91  * destroyed, then it will destroy the socket as well. You
92  * should always, therefore, be prepared for your sockets
93  * to be destroyed at any time when the main event loop
94  * is running. To prevent this from happening, you can
95  * connect to the #GtkSocket::plug-removed signal.
96  *
97  * The communication between a #GtkSocket and a #GtkPlug follows the
98  * <ulink url="http://www.freedesktop.org/Standards/xembed-spec">XEmbed</ulink>
99  * protocol. This protocol has also been implemented in other toolkits, e.g.
100  * <application>Qt</application>, allowing the same level of integration
101  * when embedding a <application>Qt</application> widget in GTK or vice versa.
102  *
103  * A socket can also be used to swallow arbitrary
104  * pre-existing top-level windows using gtk_socket_steal(),
105  * though the integration when this is done will not be as close
106  * as between a #GtkPlug and a #GtkSocket.
107  *
108  * <note>
109  * The #GtkPlug and #GtkSocket widgets are currently not available
110  * on all platforms supported by GTK+.
111  * </note>
112  */
113
114 /* Forward declararations */
115
116 static void     gtk_socket_finalize             (GObject          *object);
117 static void     gtk_socket_notify               (GObject          *object,
118                                                  GParamSpec       *pspec);
119 static void     gtk_socket_realize              (GtkWidget        *widget);
120 static void     gtk_socket_unrealize            (GtkWidget        *widget);
121 static void     gtk_socket_size_request         (GtkWidget        *widget,
122                                                  GtkRequisition   *requisition);
123 static void     gtk_socket_size_allocate        (GtkWidget        *widget,
124                                                  GtkAllocation    *allocation);
125 static void     gtk_socket_hierarchy_changed    (GtkWidget        *widget,
126                                                  GtkWidget        *old_toplevel);
127 static void     gtk_socket_grab_notify          (GtkWidget        *widget,
128                                                  gboolean          was_grabbed);
129 static gboolean gtk_socket_key_event            (GtkWidget        *widget,
130                                                  GdkEventKey      *event);
131 static gboolean gtk_socket_focus                (GtkWidget        *widget,
132                                                  GtkDirectionType  direction);
133 static void     gtk_socket_remove               (GtkContainer     *container,
134                                                  GtkWidget        *widget);
135 static void     gtk_socket_forall               (GtkContainer     *container,
136                                                  gboolean          include_internals,
137                                                  GtkCallback       callback,
138                                                  gpointer          callback_data);
139
140
141 /* Local data */
142
143 typedef struct
144 {
145   guint                  accel_key;
146   GdkModifierType        accel_mods;
147 } GrabbedKey;
148
149 enum {
150   PLUG_ADDED,
151   PLUG_REMOVED,
152   LAST_SIGNAL
153 }; 
154
155 static guint socket_signals[LAST_SIGNAL] = { 0 };
156
157 /*
158  * _gtk_socket_get_private:
159  *
160  * @socket: a #GtkSocket
161  *
162  * Returns the private data associated with a GtkSocket, creating it
163  * first if necessary.
164  */
165 GtkSocketPrivate *
166 _gtk_socket_get_private (GtkSocket *socket)
167 {
168   return G_TYPE_INSTANCE_GET_PRIVATE (socket, GTK_TYPE_SOCKET, GtkSocketPrivate);
169 }
170
171 G_DEFINE_TYPE (GtkSocket, gtk_socket, GTK_TYPE_CONTAINER)
172
173 static void
174 gtk_socket_finalize (GObject *object)
175 {
176   GtkSocket *socket = GTK_SOCKET (object);
177   
178   g_object_unref (socket->accel_group);
179   socket->accel_group = NULL;
180
181   G_OBJECT_CLASS (gtk_socket_parent_class)->finalize (object);
182 }
183
184 static void
185 gtk_socket_class_init (GtkSocketClass *class)
186 {
187   GtkWidgetClass *widget_class;
188   GtkContainerClass *container_class;
189   GObjectClass *gobject_class;
190
191   gobject_class = (GObjectClass *) class;
192   widget_class = (GtkWidgetClass*) class;
193   container_class = (GtkContainerClass*) class;
194
195   gobject_class->finalize = gtk_socket_finalize;
196   gobject_class->notify = gtk_socket_notify;
197
198   widget_class->realize = gtk_socket_realize;
199   widget_class->unrealize = gtk_socket_unrealize;
200   widget_class->size_request = gtk_socket_size_request;
201   widget_class->size_allocate = gtk_socket_size_allocate;
202   widget_class->hierarchy_changed = gtk_socket_hierarchy_changed;
203   widget_class->grab_notify = gtk_socket_grab_notify;
204   widget_class->key_press_event = gtk_socket_key_event;
205   widget_class->key_release_event = gtk_socket_key_event;
206   widget_class->focus = gtk_socket_focus;
207
208   /* We don't want to show_all/hide_all the in-process
209    * plug, if any.
210    */
211   widget_class->show_all = gtk_widget_show;
212   widget_class->hide_all = gtk_widget_hide;
213   
214   container_class->remove = gtk_socket_remove;
215   container_class->forall = gtk_socket_forall;
216
217   /**
218    * GtkSocket::plug-added:
219    * @socket_: the object which received the signal
220    *
221    * This signal is emitted when a client is successfully
222    * added to the socket. 
223    */
224   socket_signals[PLUG_ADDED] =
225     g_signal_new (I_("plug-added"),
226                   G_OBJECT_CLASS_TYPE (class),
227                   G_SIGNAL_RUN_LAST,
228                   G_STRUCT_OFFSET (GtkSocketClass, plug_added),
229                   NULL, NULL,
230                   _gtk_marshal_VOID__VOID,
231                   G_TYPE_NONE, 0);
232
233   /**
234    * GtkSocket::plug-removed:
235    * @socket_: the object which received the signal
236    *
237    * This signal is emitted when a client is removed from the socket. 
238    * The default action is to destroy the #GtkSocket widget, so if you 
239    * want to reuse it you must add a signal handler that returns %TRUE. 
240    *
241    * Return value: %TRUE to stop other handlers from being invoked.
242    */
243   socket_signals[PLUG_REMOVED] =
244     g_signal_new (I_("plug-removed"),
245                   G_OBJECT_CLASS_TYPE (class),
246                   G_SIGNAL_RUN_LAST,
247                   G_STRUCT_OFFSET (GtkSocketClass, plug_removed),
248                   _gtk_boolean_handled_accumulator, NULL,
249                   _gtk_marshal_BOOLEAN__VOID,
250                   G_TYPE_BOOLEAN, 0);
251
252   g_type_class_add_private (gobject_class, sizeof (GtkSocketPrivate));
253 }
254
255 static void
256 gtk_socket_init (GtkSocket *socket)
257 {
258   socket->request_width = 0;
259   socket->request_height = 0;
260   socket->current_width = 0;
261   socket->current_height = 0;
262   
263   socket->plug_window = NULL;
264   socket->plug_widget = NULL;
265   socket->focus_in = FALSE;
266   socket->have_size = FALSE;
267   socket->need_map = FALSE;
268   socket->active = FALSE;
269
270   socket->accel_group = gtk_accel_group_new ();
271   g_object_set_data (G_OBJECT (socket->accel_group), I_("gtk-socket"), socket);
272 }
273
274 /**
275  * gtk_socket_new:
276  * 
277  * Create a new empty #GtkSocket.
278  * 
279  * Return value:  the new #GtkSocket.
280  **/
281 GtkWidget*
282 gtk_socket_new (void)
283 {
284   GtkSocket *socket;
285
286   socket = g_object_new (GTK_TYPE_SOCKET, NULL);
287
288   return GTK_WIDGET (socket);
289 }
290
291 /**
292  * gtk_socket_add_id:
293  * @socket_: a #GtkSocket
294  * @window_id: the window ID of a client participating in the XEMBED protocol.
295  *
296  * Adds an XEMBED client, such as a #GtkPlug, to the #GtkSocket.  The
297  * client may be in the same process or in a different process. 
298  * 
299  * To embed a #GtkPlug in a #GtkSocket, you can either create the
300  * #GtkPlug with <literal>gtk_plug_new (0)</literal>, call 
301  * gtk_plug_get_id() to get the window ID of the plug, and then pass that to the
302  * gtk_socket_add_id(), or you can call gtk_socket_get_id() to get the
303  * window ID for the socket, and call gtk_plug_new() passing in that
304  * ID.
305  *
306  * The #GtkSocket must have already be added into a toplevel window
307  *  before you can make this call.
308  **/
309 void           
310 gtk_socket_add_id (GtkSocket      *socket,
311                    GdkNativeWindow window_id)
312 {
313   g_return_if_fail (GTK_IS_SOCKET (socket));
314   g_return_if_fail (GTK_WIDGET_ANCHORED (socket));
315
316   if (!gtk_widget_get_realized (GTK_WIDGET (socket)))
317     gtk_widget_realize (GTK_WIDGET (socket));
318
319   _gtk_socket_add_window (socket, window_id, TRUE);
320 }
321
322 /**
323  * gtk_socket_get_id:
324  * @socket_: a #GtkSocket.
325  * 
326  * Gets the window ID of a #GtkSocket widget, which can then
327  * be used to create a client embedded inside the socket, for
328  * instance with gtk_plug_new(). 
329  *
330  * The #GtkSocket must have already be added into a toplevel window 
331  * before you can make this call.
332  * 
333  * Return value: the window ID for the socket
334  **/
335 GdkNativeWindow
336 gtk_socket_get_id (GtkSocket *socket)
337 {
338   g_return_val_if_fail (GTK_IS_SOCKET (socket), 0);
339   g_return_val_if_fail (GTK_WIDGET_ANCHORED (socket), 0);
340
341   if (!gtk_widget_get_realized (GTK_WIDGET (socket)))
342     gtk_widget_realize (GTK_WIDGET (socket));
343
344   return _gtk_socket_windowing_get_id (socket);
345 }
346
347 /**
348  * gtk_socket_get_plug_window:
349  * @socket_: a #GtkSocket.
350  *
351  * Retrieves the window of the plug. Use this to check if the plug has
352  * been created inside of the socket.
353  *
354  * Return value: the window of the plug if available, or %NULL
355  *
356  * Since:  2.14
357  **/
358 GdkWindow*
359 gtk_socket_get_plug_window (GtkSocket *socket)
360 {
361   g_return_val_if_fail (GTK_IS_SOCKET (socket), NULL);
362
363   return socket->plug_window;
364 }
365
366 static void
367 gtk_socket_realize (GtkWidget *widget)
368 {
369   GtkAllocation allocation;
370   GtkSocket *socket = GTK_SOCKET (widget);
371   GdkWindow *window;
372   GdkWindowAttr attributes;
373   gint attributes_mask;
374
375   gtk_widget_set_realized (widget, TRUE);
376
377   gtk_widget_get_allocation (widget, &allocation);
378
379   attributes.window_type = GDK_WINDOW_CHILD;
380   attributes.x = allocation.x;
381   attributes.y = allocation.y;
382   attributes.width = allocation.width;
383   attributes.height = allocation.height;
384   attributes.wclass = GDK_INPUT_OUTPUT;
385   attributes.visual = gtk_widget_get_visual (widget);
386   attributes.colormap = gtk_widget_get_colormap (widget);
387   attributes.event_mask = GDK_FOCUS_CHANGE_MASK;
388
389   attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
390
391   window = gdk_window_new (gtk_widget_get_parent_window (widget),
392                            &attributes, attributes_mask);
393   gtk_widget_set_window (widget, window);
394   gdk_window_set_user_data (window, socket);
395
396   gtk_widget_style_attach (widget);
397   gtk_style_set_background (gtk_widget_get_style (widget),
398                             window, GTK_STATE_NORMAL);
399
400   _gtk_socket_windowing_realize_window (socket);
401
402   gdk_window_add_filter (window,
403                          _gtk_socket_windowing_filter_func,
404                          widget);
405
406   /* We sync here so that we make sure that if the XID for
407    * our window is passed to another application, SubstructureRedirectMask
408    * will be set by the time the other app creates its window.
409    */
410   gdk_display_sync (gtk_widget_get_display (widget));
411 }
412
413 /**
414  * _gtk_socket_end_embedding:
415  *
416  * @socket: a #GtkSocket
417  *
418  * Called to end the embedding of a plug in the socket.
419  */
420 void
421 _gtk_socket_end_embedding (GtkSocket *socket)
422 {
423   GtkSocketPrivate *private = _gtk_socket_get_private (socket);
424   GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (socket));
425   
426   if (GTK_IS_WINDOW (toplevel))
427     _gtk_socket_windowing_end_embedding_toplevel (socket);
428
429   g_object_unref (socket->plug_window);
430   socket->plug_window = NULL;
431   socket->current_width = 0;
432   socket->current_height = 0;
433   private->resize_count = 0;
434
435   gtk_accel_group_disconnect (socket->accel_group, NULL);
436 }
437
438 static void
439 gtk_socket_unrealize (GtkWidget *widget)
440 {
441   GtkSocket *socket = GTK_SOCKET (widget);
442
443   gtk_widget_set_realized (widget, FALSE);
444
445   if (socket->plug_widget)
446     {
447       _gtk_plug_remove_from_socket (GTK_PLUG (socket->plug_widget), socket);
448     }
449   else if (socket->plug_window)
450     {
451       _gtk_socket_end_embedding (socket);
452     }
453
454   GTK_WIDGET_CLASS (gtk_socket_parent_class)->unrealize (widget);
455 }
456
457 static void
458 gtk_socket_size_request (GtkWidget      *widget,
459                          GtkRequisition *requisition)
460 {
461   GtkSocket *socket = GTK_SOCKET (widget);
462
463   if (socket->plug_widget)
464     {
465       gtk_widget_size_request (socket->plug_widget, requisition);
466     }
467   else
468     {
469       if (socket->is_mapped && !socket->have_size && socket->plug_window)
470         _gtk_socket_windowing_size_request (socket);
471
472       if (socket->is_mapped && socket->have_size)
473         {
474           requisition->width = MAX (socket->request_width, 1);
475           requisition->height = MAX (socket->request_height, 1);
476         }
477       else
478         {
479           requisition->width = 1;
480           requisition->height = 1;
481         }
482     }
483 }
484
485 static void
486 gtk_socket_size_allocate (GtkWidget     *widget,
487                           GtkAllocation *allocation)
488 {
489   GtkSocket *socket = GTK_SOCKET (widget);
490
491   gtk_widget_set_allocation (widget, allocation);
492   if (gtk_widget_get_realized (widget))
493     {
494       gdk_window_move_resize (gtk_widget_get_window (widget),
495                               allocation->x, allocation->y,
496                               allocation->width, allocation->height);
497
498       if (socket->plug_widget)
499         {
500           GtkAllocation child_allocation;
501
502           child_allocation.x = 0;
503           child_allocation.y = 0;
504           child_allocation.width = allocation->width;
505           child_allocation.height = allocation->height;
506
507           gtk_widget_size_allocate (socket->plug_widget, &child_allocation);
508         }
509       else if (socket->plug_window)
510         {
511           GtkSocketPrivate *private = _gtk_socket_get_private (socket);
512           
513           gdk_error_trap_push ();
514           
515           if (allocation->width != socket->current_width ||
516               allocation->height != socket->current_height)
517             {
518               gdk_window_move_resize (socket->plug_window,
519                                       0, 0,
520                                       allocation->width, allocation->height);
521               if (private->resize_count)
522                 private->resize_count--;
523               
524               GTK_NOTE (PLUGSOCKET,
525                         g_message ("GtkSocket - allocated: %d %d",
526                                    allocation->width, allocation->height));
527               socket->current_width = allocation->width;
528               socket->current_height = allocation->height;
529             }
530
531           if (socket->need_map)
532             {
533               gdk_window_show (socket->plug_window);
534               socket->need_map = FALSE;
535             }
536
537           while (private->resize_count)
538             {
539               _gtk_socket_windowing_send_configure_event (socket);
540               private->resize_count--;
541               GTK_NOTE (PLUGSOCKET,
542                         g_message ("GtkSocket - sending synthetic configure: %d %d",
543                                    allocation->width, allocation->height));
544             }
545           
546           gdk_display_sync (gtk_widget_get_display (widget));
547           gdk_error_trap_pop ();
548         }
549     }
550 }
551
552 static gboolean
553 activate_key (GtkAccelGroup  *accel_group,
554               GObject        *acceleratable,
555               guint           accel_key,
556               GdkModifierType accel_mods,
557               GrabbedKey     *grabbed_key)
558 {
559   GdkEvent *gdk_event = gtk_get_current_event ();
560   
561   GtkSocket *socket = g_object_get_data (G_OBJECT (accel_group), "gtk-socket");
562   gboolean retval = FALSE;
563
564   if (gdk_event && gdk_event->type == GDK_KEY_PRESS && socket->plug_window)
565     {
566       _gtk_socket_windowing_send_key_event (socket, gdk_event, FALSE);
567       retval = TRUE;
568     }
569
570   if (gdk_event)
571     gdk_event_free (gdk_event);
572
573   return retval;
574 }
575
576 static gboolean
577 find_accel_key (GtkAccelKey *key,
578                 GClosure    *closure,
579                 gpointer     data)
580 {
581   GrabbedKey *grabbed_key = data;
582   
583   return (key->accel_key == grabbed_key->accel_key &&
584           key->accel_mods == grabbed_key->accel_mods);
585 }
586
587 /**
588  * _gtk_socket_add_grabbed_key:
589  *
590  * @socket: a #GtkSocket
591  * @keyval: a key
592  * @modifiers: modifiers for the key
593  *
594  * Called from the GtkSocket platform-specific backend when the
595  * corresponding plug has told the socket to grab a key.
596  */
597 void
598 _gtk_socket_add_grabbed_key (GtkSocket       *socket,
599                              guint            keyval,
600                              GdkModifierType  modifiers)
601 {
602   GClosure *closure;
603   GrabbedKey *grabbed_key;
604
605   grabbed_key = g_new (GrabbedKey, 1);
606   
607   grabbed_key->accel_key = keyval;
608   grabbed_key->accel_mods = modifiers;
609
610   if (gtk_accel_group_find (socket->accel_group,
611                             find_accel_key,
612                             &grabbed_key))
613     {
614       g_warning ("GtkSocket: request to add already present grabbed key %u,%#x\n",
615                  keyval, modifiers);
616       g_free (grabbed_key);
617       return;
618     }
619
620   closure = g_cclosure_new (G_CALLBACK (activate_key), grabbed_key, (GClosureNotify)g_free);
621
622   gtk_accel_group_connect (socket->accel_group, keyval, modifiers, GTK_ACCEL_LOCKED,
623                            closure);
624 }
625
626 /**
627  * _gtk_socket_remove_grabbed_key:
628  *
629  * @socket: a #GtkSocket
630  * @keyval: a key
631  * @modifiers: modifiers for the key
632  *
633  * Called from the GtkSocket backend when the corresponding plug has
634  * told the socket to remove a key grab.
635  */
636 void
637 _gtk_socket_remove_grabbed_key (GtkSocket      *socket,
638                                 guint           keyval,
639                                 GdkModifierType modifiers)
640 {
641   if (!gtk_accel_group_disconnect_key (socket->accel_group, keyval, modifiers))
642     g_warning ("GtkSocket: request to remove non-present grabbed key %u,%#x\n",
643                keyval, modifiers);
644 }
645
646 static void
647 socket_update_focus_in (GtkSocket *socket)
648 {
649   gboolean focus_in = FALSE;
650
651   if (socket->plug_window)
652     {
653       GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (socket));
654
655       if (gtk_widget_is_toplevel (toplevel) &&
656           gtk_window_has_toplevel_focus (GTK_WINDOW (toplevel)) &&
657           gtk_widget_is_focus (GTK_WIDGET (socket)))
658         focus_in = TRUE;
659     }
660
661   if (focus_in != socket->focus_in)
662     {
663       socket->focus_in = focus_in;
664
665       _gtk_socket_windowing_focus_change (socket, focus_in);
666     }
667 }
668
669 static void
670 socket_update_active (GtkSocket *socket)
671 {
672   gboolean active = FALSE;
673
674   if (socket->plug_window)
675     {
676       GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (socket));
677
678       if (gtk_widget_is_toplevel (toplevel) &&
679           gtk_window_is_active  (GTK_WINDOW (toplevel)))
680         active = TRUE;
681     }
682
683   if (active != socket->active)
684     {
685       socket->active = active;
686
687       _gtk_socket_windowing_update_active (socket, active);
688     }
689 }
690
691 static void
692 gtk_socket_hierarchy_changed (GtkWidget *widget,
693                               GtkWidget *old_toplevel)
694 {
695   GtkSocket *socket = GTK_SOCKET (widget);
696   GtkWidget *toplevel = gtk_widget_get_toplevel (widget);
697
698   if (toplevel && !GTK_IS_WINDOW (toplevel))
699     toplevel = NULL;
700
701   if (toplevel != socket->toplevel)
702     {
703       if (socket->toplevel)
704         {
705           gtk_window_remove_accel_group (GTK_WINDOW (socket->toplevel), socket->accel_group);
706           g_signal_handlers_disconnect_by_func (socket->toplevel,
707                                                 socket_update_focus_in,
708                                                 socket);
709           g_signal_handlers_disconnect_by_func (socket->toplevel,
710                                                 socket_update_active,
711                                                 socket);
712         }
713
714       socket->toplevel = toplevel;
715
716       if (toplevel)
717         {
718           gtk_window_add_accel_group (GTK_WINDOW (socket->toplevel), socket->accel_group);
719           g_signal_connect_swapped (socket->toplevel, "notify::has-toplevel-focus",
720                                     G_CALLBACK (socket_update_focus_in), socket);
721           g_signal_connect_swapped (socket->toplevel, "notify::is-active",
722                                     G_CALLBACK (socket_update_active), socket);
723         }
724
725       socket_update_focus_in (socket);
726       socket_update_active (socket);
727     }
728 }
729
730 static void
731 gtk_socket_grab_notify (GtkWidget *widget,
732                         gboolean   was_grabbed)
733 {
734   GtkSocket *socket = GTK_SOCKET (widget);
735
736   if (!socket->same_app)
737     _gtk_socket_windowing_update_modality (socket, !was_grabbed);
738 }
739
740 static gboolean
741 gtk_socket_key_event (GtkWidget   *widget,
742                       GdkEventKey *event)
743 {
744   GtkSocket *socket = GTK_SOCKET (widget);
745   
746   if (gtk_widget_has_focus (widget) && socket->plug_window && !socket->plug_widget)
747     {
748       _gtk_socket_windowing_send_key_event (socket, (GdkEvent *) event, FALSE);
749
750       return TRUE;
751     }
752   else
753     return FALSE;
754 }
755
756 static void
757 gtk_socket_notify (GObject    *object,
758                    GParamSpec *pspec)
759 {
760   if (!strcmp (pspec->name, "is-focus"))
761     return;
762   socket_update_focus_in (GTK_SOCKET (object));
763 }
764
765 /**
766  * _gtk_socket_claim_focus:
767  *
768  * @socket: a #GtkSocket
769  * @send_event: huh?
770  *
771  * Claims focus for the socket. XXX send_event?
772  */
773 void
774 _gtk_socket_claim_focus (GtkSocket *socket,
775                          gboolean   send_event)
776 {
777   GtkWidget *widget = GTK_WIDGET (socket);
778
779   if (!send_event)
780     socket->focus_in = TRUE;    /* Otherwise, our notify handler will send FOCUS_IN  */
781       
782   /* Oh, the trickery... */
783   
784   gtk_widget_set_can_focus (widget, TRUE);
785   gtk_widget_grab_focus (widget);
786   gtk_widget_set_can_focus (widget, FALSE);
787 }
788
789 static gboolean
790 gtk_socket_focus (GtkWidget       *widget,
791                   GtkDirectionType direction)
792 {
793   GtkSocket *socket = GTK_SOCKET (widget);
794
795   if (socket->plug_widget)
796     return gtk_widget_child_focus (socket->plug_widget, direction);
797
798   if (!gtk_widget_is_focus (widget))
799     {
800       _gtk_socket_windowing_focus (socket, direction);
801       _gtk_socket_claim_focus (socket, FALSE);
802  
803       return TRUE;
804     }
805   else
806     return FALSE;
807 }
808
809 static void
810 gtk_socket_remove (GtkContainer *container,
811                    GtkWidget    *child)
812 {
813   GtkSocket *socket = GTK_SOCKET (container);
814
815   g_return_if_fail (child == socket->plug_widget);
816
817   _gtk_plug_remove_from_socket (GTK_PLUG (socket->plug_widget), socket);
818 }
819
820 static void
821 gtk_socket_forall (GtkContainer *container,
822                    gboolean      include_internals,
823                    GtkCallback   callback,
824                    gpointer      callback_data)
825 {
826   GtkSocket *socket = GTK_SOCKET (container);
827
828   if (socket->plug_widget)
829     (* callback) (socket->plug_widget, callback_data);
830 }
831
832 /**
833  * _gtk_socket_add_window:
834  *
835  * @socket: a #GtkSocket
836  * @xid: the native identifier for a window
837  * @need_reparent: whether the socket's plug's window needs to be
838  *                 reparented to the socket
839  *
840  * Adds a window to a GtkSocket.
841  */
842 void
843 _gtk_socket_add_window (GtkSocket       *socket,
844                         GdkNativeWindow  xid,
845                         gboolean         need_reparent)
846 {
847   GtkWidget *widget = GTK_WIDGET (socket);
848   GdkDisplay *display = gtk_widget_get_display (widget);
849   gpointer user_data = NULL;
850   
851   socket->plug_window = gdk_window_lookup_for_display (display, xid);
852
853   if (socket->plug_window)
854     {
855       g_object_ref (socket->plug_window);
856       gdk_window_get_user_data (socket->plug_window, &user_data);
857     }
858
859   if (user_data)                /* A widget's window in this process */
860     {
861       GtkWidget *child_widget = user_data;
862
863       if (!GTK_IS_PLUG (child_widget))
864         {
865           g_warning (G_STRLOC ": Can't add non-GtkPlug to GtkSocket");
866           socket->plug_window = NULL;
867           gdk_error_trap_pop ();
868           
869           return;
870         }
871
872       _gtk_plug_add_to_socket (GTK_PLUG (child_widget), socket);
873     }
874   else                          /* A foreign window */
875     {
876       GtkWidget *toplevel;
877       GdkDragProtocol protocol;
878
879       gdk_error_trap_push ();
880
881       if (!socket->plug_window)
882         {  
883           socket->plug_window = gdk_window_foreign_new_for_display (display, xid);
884           if (!socket->plug_window) /* was deleted before we could get it */
885             {
886               gdk_error_trap_pop ();
887               return;
888             }
889         }
890         
891       _gtk_socket_windowing_select_plug_window_input (socket);
892
893       if (gdk_error_trap_pop ())
894         {
895           g_object_unref (socket->plug_window);
896           socket->plug_window = NULL;
897           return;
898         }
899       
900       /* OK, we now will reliably get destroy notification on socket->plug_window */
901
902       gdk_error_trap_push ();
903
904       if (need_reparent)
905         {
906           gdk_window_hide (socket->plug_window); /* Shouldn't actually be necessary for XEMBED, but just in case */
907           gdk_window_reparent (socket->plug_window,
908                                gtk_widget_get_window (widget),
909                                0, 0);
910         }
911
912       socket->have_size = FALSE;
913
914       _gtk_socket_windowing_embed_get_info (socket);
915
916       socket->need_map = socket->is_mapped;
917
918       if (gdk_drag_get_protocol_for_display (display, xid, &protocol))
919         gtk_drag_dest_set_proxy (GTK_WIDGET (socket), socket->plug_window, 
920                                  protocol, TRUE);
921
922       gdk_display_sync (display);
923       gdk_error_trap_pop ();
924
925       gdk_window_add_filter (socket->plug_window,
926                              _gtk_socket_windowing_filter_func,
927                              socket);
928
929       /* Add a pointer to the socket on our toplevel window */
930
931       toplevel = gtk_widget_get_toplevel (GTK_WIDGET (socket));
932       if (GTK_IS_WINDOW (toplevel))
933         gtk_window_add_embedded_xid (GTK_WINDOW (toplevel), xid);
934
935       _gtk_socket_windowing_embed_notify (socket);
936
937       socket_update_active (socket);
938       socket_update_focus_in (socket);
939
940       gtk_widget_queue_resize (GTK_WIDGET (socket));
941     }
942
943   if (socket->plug_window)
944     g_signal_emit (socket, socket_signals[PLUG_ADDED], 0);
945 }
946
947 /**
948  * _gtk_socket_handle_map_request:
949  *
950  * @socket: a #GtkSocket
951  *
952  * Called from the GtkSocket backend when the plug has been mapped.
953  */
954 void
955 _gtk_socket_handle_map_request (GtkSocket *socket)
956 {
957   if (!socket->is_mapped)
958     {
959       socket->is_mapped = TRUE;
960       socket->need_map = TRUE;
961
962       gtk_widget_queue_resize (GTK_WIDGET (socket));
963     }
964 }
965
966 /**
967  * _gtk_socket_unmap_notify:
968  *
969  * @socket: a #GtkSocket
970  *
971  * Called from the GtkSocket backend when the plug has been unmapped ???
972  */
973 void
974 _gtk_socket_unmap_notify (GtkSocket *socket)
975 {
976   if (socket->is_mapped)
977     {
978       socket->is_mapped = FALSE;
979       gtk_widget_queue_resize (GTK_WIDGET (socket));
980     }
981 }
982
983 /**
984  * _gtk_socket_advance_toplevel_focus:
985  *
986  * @socket: a #GtkSocket
987  * @direction: a direction
988  *
989  * Called from the GtkSocket backend when the corresponding plug
990  * has told the socket to move the focus.
991  */
992 void
993 _gtk_socket_advance_toplevel_focus (GtkSocket        *socket,
994                                     GtkDirectionType  direction)
995 {
996   GtkBin *bin;
997   GtkWindow *window;
998   GtkContainer *container;
999   GtkWidget *child;
1000   GtkWidget *focus_widget;
1001   GtkWidget *toplevel;
1002   GtkWidget *old_focus_child;
1003   GtkWidget *parent;
1004
1005   toplevel = gtk_widget_get_toplevel (GTK_WIDGET (socket));
1006   if (!toplevel)
1007     return;
1008
1009   if (!gtk_widget_is_toplevel (toplevel) || GTK_IS_PLUG (toplevel))
1010     {
1011       gtk_widget_child_focus (toplevel,direction);
1012       return;
1013     }
1014
1015   container = GTK_CONTAINER (toplevel);
1016   window = GTK_WINDOW (toplevel);
1017   bin = GTK_BIN (toplevel);
1018
1019   /* This is a copy of gtk_window_focus(), modified so that we
1020    * can detect wrap-around.
1021    */
1022   old_focus_child = gtk_container_get_focus_child (container);
1023   
1024   if (old_focus_child)
1025     {
1026       if (gtk_widget_child_focus (old_focus_child, direction))
1027         return;
1028
1029       /* We are allowed exactly one wrap-around per sequence of focus
1030        * events
1031        */
1032       if (_gtk_socket_windowing_embed_get_focus_wrapped ())
1033         return;
1034       else
1035         _gtk_socket_windowing_embed_set_focus_wrapped ();
1036     }
1037
1038   focus_widget = gtk_window_get_focus (window);
1039   if (window)
1040     {
1041       /* Wrapped off the end, clear the focus setting for the toplevel */
1042       parent = gtk_widget_get_parent (focus_widget);
1043       while (parent)
1044         {
1045           gtk_container_set_focus_child (GTK_CONTAINER (parent), NULL);
1046           parent = gtk_widget_get_parent (parent);
1047         }
1048       
1049       gtk_window_set_focus (GTK_WINDOW (container), NULL);
1050     }
1051
1052   /* Now try to focus the first widget in the window */
1053   child = gtk_bin_get_child (bin);
1054   if (child)
1055     {
1056       if (gtk_widget_child_focus (child, direction))
1057         return;
1058     }
1059 }