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