1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
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.
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.
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.
19 /* By Owen Taylor <otaylor@gtk.org> 98/4/4 */
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/.
31 #include "gtkmarshalers.h"
34 #include "gtkprivate.h"
35 #include "gtkplugprivate.h"
36 #include "gtksocketprivate.h"
37 #include "gtkwidgetprivate.h"
38 #include "gtkwindowprivate.h"
40 #ifdef GDK_WINDOWING_X11
43 #ifdef GDK_WINDOWING_WIN32
44 #include "win32/gdkwin32.h"
49 * @Short_description: Toplevel for embedding into other processes
51 * @See_also: #GtkSocket
53 * Together with #GtkSocket, #GtkPlug provides the ability
54 * to embed widgets from one process into another process
55 * in a fashion that is transparent to the user. One
56 * process creates a #GtkSocket widget and passes the
57 * ID of that widget's window to the other process,
58 * which then creates a #GtkPlug with that window ID.
59 * Any widgets contained in the #GtkPlug then will appear
60 * inside the first application's window.
62 * The communication between a #GtkSocket and a #GtkPlug follows the
63 * <ulink url="http://www.freedesktop.org/Standards/xembed-spec">XEmbed</ulink>
64 * protocol. This protocol has also been implemented in other toolkits, e.g.
65 * <application>Qt</application>, allowing the same level of integration
66 * when embedding a <application>Qt</application> widget in GTK or vice versa.
69 * The #GtkPlug and #GtkSocket widgets are only available when GTK is
70 * compiled for the X11 platform and %GDK_WINDOWING_X11 is defined.
71 * They can only be used on a #GdkX11Display.
75 static void gtk_plug_get_property (GObject *object,
79 static void gtk_plug_finalize (GObject *object);
80 static void gtk_plug_realize (GtkWidget *widget);
81 static void gtk_plug_unrealize (GtkWidget *widget);
82 static void gtk_plug_show (GtkWidget *widget);
83 static void gtk_plug_hide (GtkWidget *widget);
84 static void gtk_plug_map (GtkWidget *widget);
85 static void gtk_plug_unmap (GtkWidget *widget);
86 static void gtk_plug_size_allocate (GtkWidget *widget,
87 GtkAllocation *allocation);
88 static gboolean gtk_plug_key_press_event (GtkWidget *widget,
90 static gboolean gtk_plug_focus_event (GtkWidget *widget,
91 GdkEventFocus *event);
92 static void gtk_plug_set_focus (GtkWindow *window,
94 static gboolean gtk_plug_focus (GtkWidget *widget,
95 GtkDirectionType direction);
96 static void gtk_plug_check_resize (GtkContainer *container);
97 static void gtk_plug_keys_changed (GtkWindow *window);
99 static GtkBinClass *bin_class = NULL;
103 guint accelerator_key;
104 GdkModifierType accelerator_mods;
118 static guint plug_signals[LAST_SIGNAL] = { 0 };
120 G_DEFINE_TYPE (GtkPlug, gtk_plug, GTK_TYPE_WINDOW)
123 gtk_plug_get_property (GObject *object,
128 GtkPlug *plug = GTK_PLUG (object);
129 GtkPlugPrivate *priv = plug->priv;
134 g_value_set_boolean (value, priv->socket_window != NULL);
136 case PROP_SOCKET_WINDOW:
137 g_value_set_object (value, priv->socket_window);
140 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
146 gtk_plug_class_init (GtkPlugClass *class)
148 GObjectClass *gobject_class = (GObjectClass *)class;
149 GtkWidgetClass *widget_class = (GtkWidgetClass *)class;
150 GtkWindowClass *window_class = (GtkWindowClass *)class;
151 GtkContainerClass *container_class = (GtkContainerClass *)class;
153 bin_class = g_type_class_peek (GTK_TYPE_BIN);
155 gobject_class->get_property = gtk_plug_get_property;
156 gobject_class->finalize = gtk_plug_finalize;
158 widget_class->realize = gtk_plug_realize;
159 widget_class->unrealize = gtk_plug_unrealize;
160 widget_class->key_press_event = gtk_plug_key_press_event;
161 widget_class->focus_in_event = gtk_plug_focus_event;
162 widget_class->focus_out_event = gtk_plug_focus_event;
164 widget_class->show = gtk_plug_show;
165 widget_class->hide = gtk_plug_hide;
166 widget_class->map = gtk_plug_map;
167 widget_class->unmap = gtk_plug_unmap;
168 widget_class->size_allocate = gtk_plug_size_allocate;
170 widget_class->focus = gtk_plug_focus;
172 container_class->check_resize = gtk_plug_check_resize;
174 window_class->set_focus = gtk_plug_set_focus;
175 window_class->keys_changed = gtk_plug_keys_changed;
180 * %TRUE if the plug is embedded in a socket.
184 g_object_class_install_property (gobject_class,
186 g_param_spec_boolean ("embedded",
188 P_("Whether the plug is embedded"),
190 GTK_PARAM_READABLE));
193 * GtkPlug:socket-window:
195 * The window of the socket the plug is embedded in.
199 g_object_class_install_property (gobject_class,
201 g_param_spec_object ("socket-window",
203 P_("The window of the socket the plug is embedded in"),
205 GTK_PARAM_READABLE));
209 * @plug: the object on which the signal was emitted
211 * Gets emitted when the plug becomes embedded in a socket.
213 plug_signals[EMBEDDED] =
214 g_signal_new (I_("embedded"),
215 G_OBJECT_CLASS_TYPE (class),
217 G_STRUCT_OFFSET (GtkPlugClass, embedded),
219 _gtk_marshal_VOID__VOID,
222 g_type_class_add_private (class, sizeof (GtkPlugPrivate));
226 gtk_plug_init (GtkPlug *plug)
228 plug->priv = G_TYPE_INSTANCE_GET_PRIVATE (plug,
234 gtk_plug_set_is_child (GtkPlug *plug,
237 GtkPlugPrivate *priv = plug->priv;
238 GtkWidget *widget = GTK_WIDGET (plug);
240 g_assert (!gtk_widget_get_parent (widget));
244 if (priv->modality_window)
245 _gtk_plug_handle_modality_off (plug);
247 if (priv->modality_group)
249 gtk_window_group_remove_window (priv->modality_group, GTK_WINDOW (plug));
250 g_object_unref (priv->modality_group);
251 priv->modality_group = NULL;
254 /* As a toplevel, the MAPPED flag doesn't correspond
255 * to whether the widget->window is mapped; we unmap
256 * here, but don't bother remapping -- we will get mapped
257 * by gtk_widget_set_parent ().
259 if (gtk_widget_get_mapped (widget))
260 gtk_widget_unmap (widget);
262 _gtk_window_set_is_toplevel (GTK_WINDOW (plug), FALSE);
263 gtk_container_set_resize_mode (GTK_CONTAINER (plug), GTK_RESIZE_PARENT);
265 _gtk_widget_propagate_hierarchy_changed (widget, widget);
269 if (gtk_window_get_focus (GTK_WINDOW (plug)))
270 gtk_window_set_focus (GTK_WINDOW (plug), NULL);
271 if (gtk_window_get_default_widget (GTK_WINDOW (plug)))
272 gtk_window_set_default (GTK_WINDOW (plug), NULL);
274 priv->modality_group = gtk_window_group_new ();
275 gtk_window_group_add_window (priv->modality_group, GTK_WINDOW (plug));
277 _gtk_window_set_is_toplevel (GTK_WINDOW (plug), TRUE);
278 gtk_container_set_resize_mode (GTK_CONTAINER (plug), GTK_RESIZE_QUEUE);
280 _gtk_widget_propagate_hierarchy_changed (GTK_WIDGET (plug), NULL);
288 * Gets the window ID of a #GtkPlug widget, which can then
289 * be used to embed this window inside another window, for
290 * instance with gtk_socket_add_id().
292 * Return value: the window ID for the plug
295 gtk_plug_get_id (GtkPlug *plug)
297 g_return_val_if_fail (GTK_IS_PLUG (plug), 0);
299 if (!gtk_widget_get_realized (GTK_WIDGET (plug)))
300 gtk_widget_realize (GTK_WIDGET (plug));
302 return _gtk_plug_windowing_get_id (plug);
306 * gtk_plug_get_embedded:
309 * Determines whether the plug is embedded in a socket.
311 * Return value: %TRUE if the plug is embedded in a socket
316 gtk_plug_get_embedded (GtkPlug *plug)
318 g_return_val_if_fail (GTK_IS_PLUG (plug), FALSE);
320 return plug->priv->socket_window != NULL;
324 * gtk_plug_get_socket_window:
327 * Retrieves the socket the plug is embedded in.
329 * Return value: (transfer none): the window of the socket, or %NULL
334 gtk_plug_get_socket_window (GtkPlug *plug)
336 g_return_val_if_fail (GTK_IS_PLUG (plug), NULL);
338 return plug->priv->socket_window;
342 * _gtk_plug_add_to_socket:
344 * @socket_: a #GtkSocket
346 * Adds a plug to a socket within the same application.
349 _gtk_plug_add_to_socket (GtkPlug *plug,
352 GtkPlugPrivate *priv;
355 g_return_if_fail (GTK_IS_PLUG (plug));
356 g_return_if_fail (GTK_IS_SOCKET (socket_));
357 g_return_if_fail (gtk_widget_get_realized (GTK_WIDGET (socket_)));
360 widget = GTK_WIDGET (plug);
362 gtk_plug_set_is_child (plug, TRUE);
363 priv->same_app = TRUE;
364 socket_->priv->same_app = TRUE;
365 socket_->priv->plug_widget = widget;
367 priv->socket_window = gtk_widget_get_window (GTK_WIDGET (socket_));
368 g_object_ref (priv->socket_window);
369 g_signal_emit (plug, plug_signals[EMBEDDED], 0);
370 g_object_notify (G_OBJECT (plug), "embedded");
372 if (gtk_widget_get_realized (widget))
376 window = gtk_widget_get_window (widget);
377 gdk_window_reparent (window, priv->socket_window,
378 -gdk_window_get_width (window),
379 -gdk_window_get_height (window));
382 gtk_widget_set_parent (widget, GTK_WIDGET (socket_));
384 g_signal_emit_by_name (socket_, "plug-added");
388 * _gtk_plug_send_delete_event:
389 * @widget: a #GtkWidget
391 * Send a GDK_DELETE event to the @widget and destroy it if
392 * necessary. Internal GTK function, called from this file or the
393 * backend-specific GtkPlug implementation.
396 _gtk_plug_send_delete_event (GtkWidget *widget)
398 GdkEvent *event = gdk_event_new (GDK_DELETE);
400 event->any.window = g_object_ref (gtk_widget_get_window (widget));
401 event->any.send_event = FALSE;
403 g_object_ref (widget);
405 if (!gtk_widget_event (widget, event))
406 gtk_widget_destroy (widget);
408 g_object_unref (widget);
410 gdk_event_free (event);
414 * _gtk_plug_remove_from_socket:
416 * @socket_: a #GtkSocket
418 * Removes a plug from a socket within the same application.
421 _gtk_plug_remove_from_socket (GtkPlug *plug,
424 GtkPlugPrivate *priv;
428 gboolean widget_was_visible;
430 g_return_if_fail (GTK_IS_PLUG (plug));
431 g_return_if_fail (GTK_IS_SOCKET (socket_));
432 g_return_if_fail (gtk_widget_get_realized (GTK_WIDGET (plug)));
435 widget = GTK_WIDGET (plug);
437 if (_gtk_widget_get_in_reparent (widget))
441 g_object_ref (socket_);
443 widget_was_visible = gtk_widget_get_visible (widget);
444 window = gtk_widget_get_window (widget);
446 gdk_window_hide (window);
447 _gtk_widget_set_in_reparent (widget, TRUE);
448 gdk_window_reparent (window,
449 gtk_widget_get_root_window (widget),
451 gtk_widget_unparent (GTK_WIDGET (plug));
452 _gtk_widget_set_in_reparent (widget, FALSE);
454 socket_->priv->plug_widget = NULL;
455 if (socket_->priv->plug_window != NULL)
457 g_object_unref (socket_->priv->plug_window);
458 socket_->priv->plug_window = NULL;
461 socket_->priv->same_app = FALSE;
463 priv->same_app = FALSE;
464 if (priv->socket_window != NULL)
466 g_object_unref (priv->socket_window);
467 priv->socket_window = NULL;
469 gtk_plug_set_is_child (plug, FALSE);
471 g_signal_emit_by_name (socket_, "plug-removed", &result);
473 gtk_widget_destroy (GTK_WIDGET (socket_));
476 _gtk_plug_send_delete_event (widget);
478 g_object_unref (plug);
480 if (widget_was_visible && gtk_widget_get_visible (GTK_WIDGET (socket_)))
481 gtk_widget_queue_resize (GTK_WIDGET (socket_));
483 g_object_unref (socket_);
487 * gtk_plug_construct:
489 * @socket_id: the XID of the socket's window.
491 * Finish the initialization of @plug for a given #GtkSocket identified by
492 * @socket_id. This function will generally only be used by classes deriving from #GtkPlug.
495 gtk_plug_construct (GtkPlug *plug,
496 GdkNativeWindow socket_id)
498 gtk_plug_construct_for_display (plug, gdk_display_get_default (), socket_id);
502 * gtk_plug_construct_for_display:
504 * @display: the #GdkDisplay associated with @socket_id's
506 * @socket_id: the XID of the socket's window.
508 * Finish the initialization of @plug for a given #GtkSocket identified by
509 * @socket_id which is currently displayed on @display.
510 * This function will generally only be used by classes deriving from #GtkPlug.
515 gtk_plug_construct_for_display (GtkPlug *plug,
517 GdkNativeWindow socket_id)
519 GtkPlugPrivate *priv;
521 g_return_if_fail (GTK_IS_PLUG (plug));
522 g_return_if_fail (GDK_IS_DISPLAY (display));
528 gpointer user_data = NULL;
530 #ifdef GDK_WINDOWING_X11
531 if (GDK_IS_X11_DISPLAY (display))
532 priv->socket_window = gdk_x11_window_lookup_for_display (display, socket_id);
535 priv->socket_window = NULL;
537 if (priv->socket_window)
539 gdk_window_get_user_data (priv->socket_window, &user_data);
543 if (GTK_IS_SOCKET (user_data))
544 _gtk_plug_add_to_socket (plug, user_data);
547 g_warning (G_STRLOC "Can't create GtkPlug as child of non-GtkSocket");
548 priv->socket_window = NULL;
552 g_object_ref (priv->socket_window);
555 #ifdef GDK_WINDOWING_X11
556 if (GDK_IS_X11_DISPLAY (display))
557 priv->socket_window = gdk_x11_window_foreign_new_for_display (display, socket_id);
559 #ifdef GDK_WINDOWING_WIN32
560 if (GDK_IS_WIN32_DISPLAY (display))
561 priv->socket_window = gdk_win32_window_foreign_new_for_display (display, socket_id);
564 if (priv->socket_window) {
565 g_signal_emit (plug, plug_signals[EMBEDDED], 0);
567 g_object_notify (G_OBJECT (plug), "embedded");
574 * @socket_id: the window ID of the socket, or 0.
576 * Creates a new plug widget inside the #GtkSocket identified
577 * by @socket_id. If @socket_id is 0, the plug is left "unplugged" and
578 * can later be plugged into a #GtkSocket by gtk_socket_add_id().
580 * Return value: the new #GtkPlug widget.
583 gtk_plug_new (GdkNativeWindow socket_id)
585 return gtk_plug_new_for_display (gdk_display_get_default (), socket_id);
589 * gtk_plug_new_for_display:
590 * @display: the #GdkDisplay on which @socket_id is displayed
591 * @socket_id: the XID of the socket's window.
593 * Create a new plug widget inside the #GtkSocket identified by socket_id.
595 * Return value: the new #GtkPlug widget.
600 gtk_plug_new_for_display (GdkDisplay *display,
601 GdkNativeWindow socket_id)
605 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
607 plug = g_object_new (GTK_TYPE_PLUG, NULL);
608 gtk_plug_construct_for_display (plug, display, socket_id);
609 return GTK_WIDGET (plug);
613 gtk_plug_finalize (GObject *object)
615 GtkPlug *plug = GTK_PLUG (object);
616 GtkPlugPrivate *priv = plug->priv;
618 if (priv->grabbed_keys)
620 g_hash_table_destroy (priv->grabbed_keys);
621 priv->grabbed_keys = NULL;
624 G_OBJECT_CLASS (gtk_plug_parent_class)->finalize (object);
628 gtk_plug_unrealize (GtkWidget *widget)
630 GtkPlug *plug = GTK_PLUG (widget);
631 GtkPlugPrivate *priv = plug->priv;
633 if (priv->socket_window != NULL)
635 gdk_window_set_user_data (priv->socket_window, NULL);
636 g_object_unref (priv->socket_window);
637 priv->socket_window = NULL;
639 g_object_notify (G_OBJECT (widget), "embedded");
644 if (priv->modality_window)
645 _gtk_plug_handle_modality_off (plug);
647 gtk_window_group_remove_window (priv->modality_group, GTK_WINDOW (plug));
648 g_object_unref (priv->modality_group);
651 GTK_WIDGET_CLASS (gtk_plug_parent_class)->unrealize (widget);
655 gtk_plug_realize (GtkWidget *widget)
657 GtkAllocation allocation;
658 GtkPlug *plug = GTK_PLUG (widget);
659 GtkPlugPrivate *priv = plug->priv;
660 GtkWindow *window = GTK_WINDOW (widget);
661 GdkWindow *gdk_window;
662 GdkWindowAttr attributes;
664 gchar *wmclass_name, *wmclass_class;
665 gint attributes_mask;
667 gtk_widget_set_realized (widget, TRUE);
669 title = gtk_window_get_title (window);
670 _gtk_window_get_wmclass (window, &wmclass_name, &wmclass_class);
671 gtk_widget_get_allocation (widget, &allocation);
673 attributes.window_type = GDK_WINDOW_CHILD; /* XXX GDK_WINDOW_PLUG ? */
674 attributes.title = (gchar *) title;
675 attributes.wmclass_name = wmclass_name;
676 attributes.wmclass_class = wmclass_class;
677 attributes.width = allocation.width;
678 attributes.height = allocation.height;
679 attributes.wclass = GDK_INPUT_OUTPUT;
681 /* this isn't right - we should match our parent's visual/colormap.
682 * though that will require handling "foreign" colormaps */
683 attributes.visual = gtk_widget_get_visual (widget);
684 attributes.event_mask = gtk_widget_get_events (widget);
685 attributes.event_mask |= (GDK_EXPOSURE_MASK |
687 GDK_KEY_RELEASE_MASK |
688 GDK_ENTER_NOTIFY_MASK |
689 GDK_LEAVE_NOTIFY_MASK |
692 attributes_mask = GDK_WA_VISUAL;
693 attributes_mask |= (title ? GDK_WA_TITLE : 0);
694 attributes_mask |= (wmclass_name ? GDK_WA_WMCLASS : 0);
696 if (gtk_widget_is_toplevel (widget))
698 attributes.window_type = GDK_WINDOW_TOPLEVEL;
700 gdk_error_trap_push ();
701 if (priv->socket_window)
702 gdk_window = gdk_window_new (priv->socket_window,
703 &attributes, attributes_mask);
704 else /* If it's a passive plug, we use the root window */
705 gdk_window = gdk_window_new (gtk_widget_get_root_window (widget),
706 &attributes, attributes_mask);
707 gtk_widget_set_window (widget, gdk_window);
709 gdk_display_sync (gtk_widget_get_display (widget));
710 if (gdk_error_trap_pop ()) /* Uh-oh */
712 gdk_error_trap_push ();
713 gdk_window_destroy (gdk_window);
714 gdk_error_trap_pop_ignored ();
715 gdk_window = gdk_window_new (gtk_widget_get_root_window (widget),
716 &attributes, attributes_mask);
717 gtk_widget_set_window (widget, gdk_window);
720 gdk_window_add_filter (gdk_window,
721 _gtk_plug_windowing_filter_func,
724 priv->modality_group = gtk_window_group_new ();
725 gtk_window_group_add_window (priv->modality_group, window);
727 _gtk_plug_windowing_realize_toplevel (plug);
731 gdk_window = gdk_window_new (gtk_widget_get_parent_window (widget),
732 &attributes, attributes_mask);
733 gtk_widget_set_window (widget, gdk_window);
736 gdk_window_set_user_data (gdk_window, window);
738 gtk_style_context_set_background (gtk_widget_get_style_context (widget),
741 gdk_window_enable_synchronized_configure (gdk_window);
745 gtk_plug_show (GtkWidget *widget)
747 if (gtk_widget_is_toplevel (widget))
748 GTK_WIDGET_CLASS (gtk_plug_parent_class)->show (widget);
750 GTK_WIDGET_CLASS (bin_class)->show (widget);
754 gtk_plug_hide (GtkWidget *widget)
756 if (gtk_widget_is_toplevel (widget))
757 GTK_WIDGET_CLASS (gtk_plug_parent_class)->hide (widget);
759 GTK_WIDGET_CLASS (bin_class)->hide (widget);
762 /* From gdkinternals.h */
763 void gdk_synthesize_window_state (GdkWindow *window,
764 GdkWindowState unset_flags,
765 GdkWindowState set_flags);
768 gtk_plug_map (GtkWidget *widget)
770 if (gtk_widget_is_toplevel (widget))
772 GtkBin *bin = GTK_BIN (widget);
773 GtkPlug *plug = GTK_PLUG (widget);
776 gtk_widget_set_mapped (widget, TRUE);
778 child = gtk_bin_get_child (bin);
780 gtk_widget_get_visible (child) &&
781 !gtk_widget_get_mapped (child))
782 gtk_widget_map (child);
784 _gtk_plug_windowing_map_toplevel (plug);
786 gdk_synthesize_window_state (gtk_widget_get_window (widget),
787 GDK_WINDOW_STATE_WITHDRAWN,
791 GTK_WIDGET_CLASS (bin_class)->map (widget);
795 gtk_plug_unmap (GtkWidget *widget)
797 if (gtk_widget_is_toplevel (widget))
799 GtkPlug *plug = GTK_PLUG (widget);
802 window = gtk_widget_get_window (widget);
804 gtk_widget_set_mapped (widget, FALSE);
806 gdk_window_hide (window);
808 _gtk_plug_windowing_unmap_toplevel (plug);
810 gdk_synthesize_window_state (window,
812 GDK_WINDOW_STATE_WITHDRAWN);
815 GTK_WIDGET_CLASS (bin_class)->unmap (widget);
819 gtk_plug_size_allocate (GtkWidget *widget,
820 GtkAllocation *allocation)
824 if (gtk_widget_is_toplevel (widget))
825 GTK_WIDGET_CLASS (gtk_plug_parent_class)->size_allocate (widget, allocation);
828 GtkBin *bin = GTK_BIN (widget);
830 gtk_widget_set_allocation (widget, allocation);
832 if (gtk_widget_get_realized (widget))
833 gdk_window_move_resize (gtk_widget_get_window (widget),
834 allocation->x, allocation->y,
835 allocation->width, allocation->height);
837 child = gtk_bin_get_child (bin);
839 if (child != NULL && gtk_widget_get_visible (child))
841 GtkAllocation child_allocation;
843 child_allocation.x = child_allocation.y = gtk_container_get_border_width (GTK_CONTAINER (widget));
844 child_allocation.width =
845 MAX (1, (gint)allocation->width - child_allocation.x * 2);
846 child_allocation.height =
847 MAX (1, (gint)allocation->height - child_allocation.y * 2);
849 gtk_widget_size_allocate (child, &child_allocation);
856 gtk_plug_key_press_event (GtkWidget *widget,
859 if (gtk_widget_is_toplevel (widget))
860 return GTK_WIDGET_CLASS (gtk_plug_parent_class)->key_press_event (widget, event);
866 gtk_plug_focus_event (GtkWidget *widget,
867 GdkEventFocus *event)
869 /* We eat focus-in events and focus-out events, since they
870 * can be generated by something like a keyboard grab on
871 * a child of the plug.
877 gtk_plug_set_focus (GtkWindow *window,
880 GtkPlug *plug = GTK_PLUG (window);
882 GTK_WINDOW_CLASS (gtk_plug_parent_class)->set_focus (window, focus);
884 /* Ask for focus from embedder
887 if (focus && !gtk_window_has_toplevel_focus (window))
888 _gtk_plug_windowing_set_focus (plug);
892 grabbed_key_hash (gconstpointer a)
894 const GrabbedKey *key = a;
897 h = key->accelerator_key << 16;
898 h ^= key->accelerator_key >> 16;
899 h ^= key->accelerator_mods;
905 grabbed_key_equal (gconstpointer a, gconstpointer b)
907 const GrabbedKey *keya = a;
908 const GrabbedKey *keyb = b;
910 return (keya->accelerator_key == keyb->accelerator_key &&
911 keya->accelerator_mods == keyb->accelerator_mods);
915 add_grabbed_key (gpointer key, gpointer val, gpointer data)
917 GrabbedKey *grabbed_key = key;
918 GtkPlug *plug = data;
919 GtkPlugPrivate *priv = plug->priv;
921 if (!priv->grabbed_keys ||
922 !g_hash_table_lookup (priv->grabbed_keys, grabbed_key))
924 _gtk_plug_windowing_add_grabbed_key (plug,
925 grabbed_key->accelerator_key,
926 grabbed_key->accelerator_mods);
931 add_grabbed_key_always (gpointer key,
935 GrabbedKey *grabbed_key = key;
936 GtkPlug *plug = data;
938 _gtk_plug_windowing_add_grabbed_key (plug,
939 grabbed_key->accelerator_key,
940 grabbed_key->accelerator_mods);
944 * _gtk_plug_add_all_grabbed_keys:
948 * Calls _gtk_plug_windowing_add_grabbed_key() on all the grabbed keys
952 _gtk_plug_add_all_grabbed_keys (GtkPlug *plug)
954 GtkPlugPrivate *priv = plug->priv;
956 if (priv->grabbed_keys)
957 g_hash_table_foreach (priv->grabbed_keys, add_grabbed_key_always, plug);
961 remove_grabbed_key (gpointer key, gpointer val, gpointer data)
963 GrabbedKey *grabbed_key = key;
964 GtkPlug *plug = data;
965 GtkPlugPrivate *priv = plug->priv;
967 if (!priv->grabbed_keys ||
968 !g_hash_table_lookup (priv->grabbed_keys, grabbed_key))
970 _gtk_plug_windowing_remove_grabbed_key (plug,
971 grabbed_key->accelerator_key,
972 grabbed_key->accelerator_mods);
977 keys_foreach (GtkWindow *window,
979 GdkModifierType modifiers,
980 gboolean is_mnemonic,
983 GHashTable *new_grabbed_keys = data;
984 GrabbedKey *key = g_slice_new (GrabbedKey);
986 key->accelerator_key = keyval;
987 key->accelerator_mods = modifiers;
989 g_hash_table_replace (new_grabbed_keys, key, key);
993 grabbed_key_free (gpointer data)
995 g_slice_free (GrabbedKey, data);
999 gtk_plug_keys_changed (GtkWindow *window)
1001 GHashTable *new_grabbed_keys, *old_grabbed_keys;
1002 GtkPlug *plug = GTK_PLUG (window);
1003 GtkPlugPrivate *priv = plug->priv;
1005 new_grabbed_keys = g_hash_table_new_full (grabbed_key_hash, grabbed_key_equal, (GDestroyNotify)grabbed_key_free, NULL);
1006 _gtk_window_keys_foreach (window, keys_foreach, new_grabbed_keys);
1008 if (priv->socket_window)
1009 g_hash_table_foreach (new_grabbed_keys, add_grabbed_key, plug);
1011 old_grabbed_keys = priv->grabbed_keys;
1012 priv->grabbed_keys = new_grabbed_keys;
1014 if (old_grabbed_keys)
1016 if (priv->socket_window)
1017 g_hash_table_foreach (old_grabbed_keys, remove_grabbed_key, plug);
1018 g_hash_table_destroy (old_grabbed_keys);
1023 gtk_plug_focus (GtkWidget *widget,
1024 GtkDirectionType direction)
1026 GtkBin *bin = GTK_BIN (widget);
1027 GtkPlug *plug = GTK_PLUG (widget);
1028 GtkWindow *window = GTK_WINDOW (widget);
1029 GtkContainer *container = GTK_CONTAINER (widget);
1031 GtkWidget *old_focus_child;
1034 old_focus_child = gtk_container_get_focus_child (container);
1035 /* We override GtkWindow's behavior, since we don't want wrapping here.
1037 if (old_focus_child)
1039 GtkWidget *focus_widget;
1041 if (gtk_widget_child_focus (old_focus_child, direction))
1044 focus_widget = gtk_window_get_focus (window);
1047 /* Wrapped off the end, clear the focus setting for the toplevel */
1048 parent = gtk_widget_get_parent (focus_widget);
1051 gtk_container_set_focus_child (GTK_CONTAINER (parent), NULL);
1052 parent = gtk_widget_get_parent (parent);
1055 gtk_window_set_focus (GTK_WINDOW (container), NULL);
1060 /* Try to focus the first widget in the window */
1061 child = gtk_bin_get_child (bin);
1062 if (child && gtk_widget_child_focus (child, direction))
1066 if (!gtk_container_get_focus_child (GTK_CONTAINER (window)))
1067 _gtk_plug_windowing_focus_to_parent (plug, direction);
1073 gtk_plug_check_resize (GtkContainer *container)
1075 if (gtk_widget_is_toplevel (GTK_WIDGET (container)))
1076 GTK_CONTAINER_CLASS (gtk_plug_parent_class)->check_resize (container);
1078 GTK_CONTAINER_CLASS (bin_class)->check_resize (container);
1082 * _gtk_plug_handle_modality_on:
1086 * Called from the GtkPlug backend when the corresponding socket has
1087 * told the plug that it modality has toggled on.
1090 _gtk_plug_handle_modality_on (GtkPlug *plug)
1092 GtkPlugPrivate *priv = plug->priv;
1094 if (!priv->modality_window)
1096 priv->modality_window = gtk_window_new (GTK_WINDOW_POPUP);
1097 gtk_window_set_screen (GTK_WINDOW (priv->modality_window),
1098 gtk_widget_get_screen (GTK_WIDGET (plug)));
1099 gtk_widget_realize (priv->modality_window);
1100 gtk_window_group_add_window (priv->modality_group, GTK_WINDOW (priv->modality_window));
1101 gtk_grab_add (priv->modality_window);
1106 * _gtk_plug_handle_modality_off:
1110 * Called from the GtkPlug backend when the corresponding socket has
1111 * told the plug that it modality has toggled off.
1114 _gtk_plug_handle_modality_off (GtkPlug *plug)
1116 GtkPlugPrivate *priv = plug->priv;
1118 if (priv->modality_window)
1120 gtk_widget_destroy (priv->modality_window);
1121 priv->modality_window = NULL;
1126 * _gtk_plug_focus_first_last:
1129 * @direction: a direction
1131 * Called from the GtkPlug backend when the corresponding socket has
1132 * told the plug that it has received the focus.
1135 _gtk_plug_focus_first_last (GtkPlug *plug,
1136 GtkDirectionType direction)
1138 GtkWindow *window = GTK_WINDOW (plug);
1139 GtkWidget *focus_widget;
1142 focus_widget = gtk_window_get_focus (window);
1145 parent = gtk_widget_get_parent (focus_widget);
1148 gtk_container_set_focus_child (GTK_CONTAINER (parent), NULL);
1149 parent = gtk_widget_get_parent (parent);
1152 gtk_window_set_focus (GTK_WINDOW (plug), NULL);
1155 gtk_widget_child_focus (GTK_WIDGET (plug), direction);