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