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 "gtkwindow.h"
40 * @Short_description: Toplevel for embedding into other processes
42 * @See_also: #GtkSocket
44 * Together with #GtkSocket, #GtkPlug provides the ability
45 * to embed widgets from one process into another process
46 * in a fashion that is transparent to the user. One
47 * process creates a #GtkSocket widget and passes the
48 * ID of that widget's window to the other process,
49 * which then creates a #GtkPlug with that window ID.
50 * Any widgets contained in the #GtkPlug then will appear
51 * inside the first application's window.
54 * The #GtkPlug and #GtkSocket widgets are currently not available
55 * on all platforms supported by GTK+.
59 static void gtk_plug_get_property (GObject *object,
63 static void gtk_plug_finalize (GObject *object);
64 static void gtk_plug_realize (GtkWidget *widget);
65 static void gtk_plug_unrealize (GtkWidget *widget);
66 static void gtk_plug_show (GtkWidget *widget);
67 static void gtk_plug_hide (GtkWidget *widget);
68 static void gtk_plug_map (GtkWidget *widget);
69 static void gtk_plug_unmap (GtkWidget *widget);
70 static void gtk_plug_size_allocate (GtkWidget *widget,
71 GtkAllocation *allocation);
72 static gboolean gtk_plug_key_press_event (GtkWidget *widget,
74 static gboolean gtk_plug_focus_event (GtkWidget *widget,
75 GdkEventFocus *event);
76 static void gtk_plug_set_focus (GtkWindow *window,
78 static gboolean gtk_plug_focus (GtkWidget *widget,
79 GtkDirectionType direction);
80 static void gtk_plug_check_resize (GtkContainer *container);
81 static void gtk_plug_keys_changed (GtkWindow *window);
83 static GtkBinClass *bin_class = NULL;
87 guint accelerator_key;
88 GdkModifierType accelerator_mods;
102 static guint plug_signals[LAST_SIGNAL] = { 0 };
104 G_DEFINE_TYPE (GtkPlug, gtk_plug, GTK_TYPE_WINDOW)
107 gtk_plug_get_property (GObject *object,
112 GtkPlug *plug = GTK_PLUG (object);
113 GtkPlugPrivate *priv = plug->priv;
118 g_value_set_boolean (value, priv->socket_window != NULL);
120 case PROP_SOCKET_WINDOW:
121 g_value_set_object (value, priv->socket_window);
124 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
130 gtk_plug_class_init (GtkPlugClass *class)
132 GObjectClass *gobject_class = (GObjectClass *)class;
133 GtkWidgetClass *widget_class = (GtkWidgetClass *)class;
134 GtkWindowClass *window_class = (GtkWindowClass *)class;
135 GtkContainerClass *container_class = (GtkContainerClass *)class;
137 bin_class = g_type_class_peek (GTK_TYPE_BIN);
139 gobject_class->get_property = gtk_plug_get_property;
140 gobject_class->finalize = gtk_plug_finalize;
142 widget_class->realize = gtk_plug_realize;
143 widget_class->unrealize = gtk_plug_unrealize;
144 widget_class->key_press_event = gtk_plug_key_press_event;
145 widget_class->focus_in_event = gtk_plug_focus_event;
146 widget_class->focus_out_event = gtk_plug_focus_event;
148 widget_class->show = gtk_plug_show;
149 widget_class->hide = gtk_plug_hide;
150 widget_class->map = gtk_plug_map;
151 widget_class->unmap = gtk_plug_unmap;
152 widget_class->size_allocate = gtk_plug_size_allocate;
154 widget_class->focus = gtk_plug_focus;
156 container_class->check_resize = gtk_plug_check_resize;
158 window_class->set_focus = gtk_plug_set_focus;
159 window_class->keys_changed = gtk_plug_keys_changed;
164 * %TRUE if the plug is embedded in a socket.
168 g_object_class_install_property (gobject_class,
170 g_param_spec_boolean ("embedded",
172 P_("Whether the plug is embedded"),
174 GTK_PARAM_READABLE));
177 * GtkPlug:socket-window:
179 * The window of the socket the plug is embedded in.
183 g_object_class_install_property (gobject_class,
185 g_param_spec_object ("socket-window",
187 P_("The window of the socket the plug is embedded in"),
189 GTK_PARAM_READABLE));
193 * @plug: the object on which the signal was emitted
195 * Gets emitted when the plug becomes embedded in a socket.
197 plug_signals[EMBEDDED] =
198 g_signal_new (I_("embedded"),
199 G_OBJECT_CLASS_TYPE (class),
201 G_STRUCT_OFFSET (GtkPlugClass, embedded),
203 _gtk_marshal_VOID__VOID,
206 g_type_class_add_private (class, sizeof (GtkPlugPrivate));
210 gtk_plug_init (GtkPlug *plug)
212 plug->priv = G_TYPE_INSTANCE_GET_PRIVATE (plug,
218 gtk_plug_set_is_child (GtkPlug *plug,
221 GtkPlugPrivate *priv = plug->priv;
222 GtkWidget *widget = GTK_WIDGET (plug);
224 g_assert (!gtk_widget_get_parent (widget));
228 if (priv->modality_window)
229 _gtk_plug_handle_modality_off (plug);
231 if (priv->modality_group)
233 gtk_window_group_remove_window (priv->modality_group, GTK_WINDOW (plug));
234 g_object_unref (priv->modality_group);
235 priv->modality_group = NULL;
238 /* As a toplevel, the MAPPED flag doesn't correspond
239 * to whether the widget->window is mapped; we unmap
240 * here, but don't bother remapping -- we will get mapped
241 * by gtk_widget_set_parent ().
243 if (gtk_widget_get_mapped (widget))
244 gtk_widget_unmap (widget);
246 _gtk_window_set_is_toplevel (GTK_WINDOW (plug), FALSE);
247 gtk_container_set_resize_mode (GTK_CONTAINER (plug), GTK_RESIZE_PARENT);
249 _gtk_widget_propagate_hierarchy_changed (widget, widget);
253 if (gtk_window_get_focus (GTK_WINDOW (plug)))
254 gtk_window_set_focus (GTK_WINDOW (plug), NULL);
255 if (gtk_window_get_default_widget (GTK_WINDOW (plug)))
256 gtk_window_set_default (GTK_WINDOW (plug), NULL);
258 priv->modality_group = gtk_window_group_new ();
259 gtk_window_group_add_window (priv->modality_group, GTK_WINDOW (plug));
261 _gtk_window_set_is_toplevel (GTK_WINDOW (plug), TRUE);
262 gtk_container_set_resize_mode (GTK_CONTAINER (plug), GTK_RESIZE_QUEUE);
264 _gtk_widget_propagate_hierarchy_changed (GTK_WIDGET (plug), NULL);
272 * Gets the window ID of a #GtkPlug widget, which can then
273 * be used to embed this window inside another window, for
274 * instance with gtk_socket_add_id().
276 * Return value: the window ID for the plug
279 gtk_plug_get_id (GtkPlug *plug)
281 g_return_val_if_fail (GTK_IS_PLUG (plug), 0);
283 if (!gtk_widget_get_realized (GTK_WIDGET (plug)))
284 gtk_widget_realize (GTK_WIDGET (plug));
286 return _gtk_plug_windowing_get_id (plug);
290 * gtk_plug_get_embedded:
293 * Determines whether the plug is embedded in a socket.
295 * Return value: %TRUE if the plug is embedded in a socket
300 gtk_plug_get_embedded (GtkPlug *plug)
302 g_return_val_if_fail (GTK_IS_PLUG (plug), FALSE);
304 return plug->priv->socket_window != NULL;
308 * gtk_plug_get_socket_window:
311 * Retrieves the socket the plug is embedded in.
313 * Return value: (transfer none): the window of the socket, or %NULL
318 gtk_plug_get_socket_window (GtkPlug *plug)
320 g_return_val_if_fail (GTK_IS_PLUG (plug), NULL);
322 return plug->priv->socket_window;
326 * _gtk_plug_add_to_socket:
328 * @socket_: a #GtkSocket
330 * Adds a plug to a socket within the same application.
333 _gtk_plug_add_to_socket (GtkPlug *plug,
336 GtkPlugPrivate *priv;
339 g_return_if_fail (GTK_IS_PLUG (plug));
340 g_return_if_fail (GTK_IS_SOCKET (socket_));
341 g_return_if_fail (gtk_widget_get_realized (GTK_WIDGET (socket_)));
344 widget = GTK_WIDGET (plug);
346 gtk_plug_set_is_child (plug, TRUE);
347 priv->same_app = TRUE;
348 socket_->same_app = TRUE;
349 socket_->plug_widget = widget;
351 priv->socket_window = gtk_widget_get_window (GTK_WIDGET (socket_));
352 g_object_ref (priv->socket_window);
353 g_signal_emit (plug, plug_signals[EMBEDDED], 0);
354 g_object_notify (G_OBJECT (plug), "embedded");
356 if (gtk_widget_get_realized (widget))
360 window = gtk_widget_get_window (widget);
361 gdk_window_reparent (window, priv->socket_window,
362 -gdk_window_get_width (window),
363 -gdk_window_get_height (window));
366 gtk_widget_set_parent (widget, GTK_WIDGET (socket_));
368 g_signal_emit_by_name (socket_, "plug-added");
372 * _gtk_plug_send_delete_event:
373 * @widget: a #GtkWidget
375 * Send a GDK_DELETE event to the @widget and destroy it if
376 * necessary. Internal GTK function, called from this file or the
377 * backend-specific GtkPlug implementation.
380 _gtk_plug_send_delete_event (GtkWidget *widget)
382 GdkEvent *event = gdk_event_new (GDK_DELETE);
384 event->any.window = g_object_ref (gtk_widget_get_window (widget));
385 event->any.send_event = FALSE;
387 g_object_ref (widget);
389 if (!gtk_widget_event (widget, event))
390 gtk_widget_destroy (widget);
392 g_object_unref (widget);
394 gdk_event_free (event);
398 * _gtk_plug_remove_from_socket:
400 * @socket_: a #GtkSocket
402 * Removes a plug from a socket within the same application.
405 _gtk_plug_remove_from_socket (GtkPlug *plug,
408 GtkPlugPrivate *priv;
412 gboolean widget_was_visible;
414 g_return_if_fail (GTK_IS_PLUG (plug));
415 g_return_if_fail (GTK_IS_SOCKET (socket_));
416 g_return_if_fail (gtk_widget_get_realized (GTK_WIDGET (plug)));
419 widget = GTK_WIDGET (plug);
421 if (_gtk_widget_get_in_reparent (widget))
425 g_object_ref (socket_);
427 widget_was_visible = gtk_widget_get_visible (widget);
428 window = gtk_widget_get_window (widget);
430 gdk_window_hide (window);
431 _gtk_widget_set_in_reparent (widget, TRUE);
432 gdk_window_reparent (window,
433 gtk_widget_get_root_window (widget),
435 gtk_widget_unparent (GTK_WIDGET (plug));
436 _gtk_widget_set_in_reparent (widget, FALSE);
438 socket_->plug_widget = NULL;
439 if (socket_->plug_window != NULL)
441 g_object_unref (socket_->plug_window);
442 socket_->plug_window = NULL;
445 socket_->same_app = FALSE;
447 priv->same_app = FALSE;
448 if (priv->socket_window != NULL)
450 g_object_unref (priv->socket_window);
451 priv->socket_window = NULL;
453 gtk_plug_set_is_child (plug, FALSE);
455 g_signal_emit_by_name (socket_, "plug-removed", &result);
457 gtk_widget_destroy (GTK_WIDGET (socket_));
460 _gtk_plug_send_delete_event (widget);
462 g_object_unref (plug);
464 if (widget_was_visible && gtk_widget_get_visible (GTK_WIDGET (socket_)))
465 gtk_widget_queue_resize (GTK_WIDGET (socket_));
467 g_object_unref (socket_);
471 * gtk_plug_construct:
473 * @socket_id: the XID of the socket's window.
475 * Finish the initialization of @plug for a given #GtkSocket identified by
476 * @socket_id. This function will generally only be used by classes deriving from #GtkPlug.
479 gtk_plug_construct (GtkPlug *plug,
480 GdkNativeWindow socket_id)
482 gtk_plug_construct_for_display (plug, gdk_display_get_default (), socket_id);
486 * gtk_plug_construct_for_display:
488 * @display: the #GdkDisplay associated with @socket_id's
490 * @socket_id: the XID of the socket's window.
492 * Finish the initialization of @plug for a given #GtkSocket identified by
493 * @socket_id which is currently displayed on @display.
494 * This function will generally only be used by classes deriving from #GtkPlug.
499 gtk_plug_construct_for_display (GtkPlug *plug,
501 GdkNativeWindow socket_id)
503 GtkPlugPrivate *priv;
505 g_return_if_fail (GTK_IS_PLUG (plug));
506 g_return_if_fail (GDK_IS_DISPLAY (display));
512 gpointer user_data = NULL;
514 priv->socket_window = gdk_window_lookup_for_display (display, socket_id);
515 if (priv->socket_window)
517 gdk_window_get_user_data (priv->socket_window, &user_data);
521 if (GTK_IS_SOCKET (user_data))
522 _gtk_plug_add_to_socket (plug, user_data);
525 g_warning (G_STRLOC "Can't create GtkPlug as child of non-GtkSocket");
526 priv->socket_window = NULL;
530 g_object_ref (priv->socket_window);
533 priv->socket_window = gdk_window_foreign_new_for_display (display, socket_id);
535 if (priv->socket_window) {
536 g_signal_emit (plug, plug_signals[EMBEDDED], 0);
538 g_object_notify (G_OBJECT (plug), "embedded");
545 * @socket_id: the window ID of the socket, or 0.
547 * Creates a new plug widget inside the #GtkSocket identified
548 * by @socket_id. If @socket_id is 0, the plug is left "unplugged" and
549 * can later be plugged into a #GtkSocket by gtk_socket_add_id().
551 * Return value: the new #GtkPlug widget.
554 gtk_plug_new (GdkNativeWindow socket_id)
556 return gtk_plug_new_for_display (gdk_display_get_default (), socket_id);
560 * gtk_plug_new_for_display:
561 * @display: the #GdkDisplay on which @socket_id is displayed
562 * @socket_id: the XID of the socket's window.
564 * Create a new plug widget inside the #GtkSocket identified by socket_id.
566 * Return value: the new #GtkPlug widget.
571 gtk_plug_new_for_display (GdkDisplay *display,
572 GdkNativeWindow socket_id)
576 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
578 plug = g_object_new (GTK_TYPE_PLUG, NULL);
579 gtk_plug_construct_for_display (plug, display, socket_id);
580 return GTK_WIDGET (plug);
584 gtk_plug_finalize (GObject *object)
586 GtkPlug *plug = GTK_PLUG (object);
587 GtkPlugPrivate *priv = plug->priv;
589 if (priv->grabbed_keys)
591 g_hash_table_destroy (priv->grabbed_keys);
592 priv->grabbed_keys = NULL;
595 G_OBJECT_CLASS (gtk_plug_parent_class)->finalize (object);
599 gtk_plug_unrealize (GtkWidget *widget)
601 GtkPlug *plug = GTK_PLUG (widget);
602 GtkPlugPrivate *priv = plug->priv;
604 if (priv->socket_window != NULL)
606 gdk_window_set_user_data (priv->socket_window, NULL);
607 g_object_unref (priv->socket_window);
608 priv->socket_window = NULL;
610 g_object_notify (G_OBJECT (widget), "embedded");
615 if (priv->modality_window)
616 _gtk_plug_handle_modality_off (plug);
618 gtk_window_group_remove_window (priv->modality_group, GTK_WINDOW (plug));
619 g_object_unref (priv->modality_group);
622 GTK_WIDGET_CLASS (gtk_plug_parent_class)->unrealize (widget);
626 gtk_plug_realize (GtkWidget *widget)
628 GtkAllocation allocation;
629 GtkPlug *plug = GTK_PLUG (widget);
630 GtkPlugPrivate *priv = plug->priv;
631 GtkWindow *window = GTK_WINDOW (widget);
632 GdkWindow *gdk_window;
633 GdkWindowAttr attributes;
635 gchar *wmclass_name, *wmclass_class;
636 gint attributes_mask;
638 gtk_widget_set_realized (widget, TRUE);
640 title = gtk_window_get_title (window);
641 _gtk_window_get_wmclass (window, &wmclass_name, &wmclass_class);
642 gtk_widget_get_allocation (widget, &allocation);
644 attributes.window_type = GDK_WINDOW_CHILD; /* XXX GDK_WINDOW_PLUG ? */
645 attributes.title = (gchar *) title;
646 attributes.wmclass_name = wmclass_name;
647 attributes.wmclass_class = wmclass_class;
648 attributes.width = allocation.width;
649 attributes.height = allocation.height;
650 attributes.wclass = GDK_INPUT_OUTPUT;
652 /* this isn't right - we should match our parent's visual/colormap.
653 * though that will require handling "foreign" colormaps */
654 attributes.visual = gtk_widget_get_visual (widget);
655 attributes.event_mask = gtk_widget_get_events (widget);
656 attributes.event_mask |= (GDK_EXPOSURE_MASK |
658 GDK_KEY_RELEASE_MASK |
659 GDK_ENTER_NOTIFY_MASK |
660 GDK_LEAVE_NOTIFY_MASK |
663 attributes_mask = GDK_WA_VISUAL;
664 attributes_mask |= (title ? GDK_WA_TITLE : 0);
665 attributes_mask |= (g_strdup (g_get_prgname ()) ? GDK_WA_WMCLASS : 0);
667 if (gtk_widget_is_toplevel (widget))
669 attributes.window_type = GDK_WINDOW_TOPLEVEL;
671 gdk_error_trap_push ();
672 if (priv->socket_window)
673 gdk_window = gdk_window_new (priv->socket_window,
674 &attributes, attributes_mask);
675 else /* If it's a passive plug, we use the root window */
676 gdk_window = gdk_window_new (gtk_widget_get_root_window (widget),
677 &attributes, attributes_mask);
678 gtk_widget_set_window (widget, gdk_window);
680 gdk_display_sync (gtk_widget_get_display (widget));
681 if (gdk_error_trap_pop ()) /* Uh-oh */
683 gdk_error_trap_push ();
684 gdk_window_destroy (gdk_window);
685 gdk_error_trap_pop_ignored ();
686 gdk_window = gdk_window_new (gtk_widget_get_root_window (widget),
687 &attributes, attributes_mask);
688 gtk_widget_set_window (widget, gdk_window);
691 gdk_window_add_filter (gdk_window,
692 _gtk_plug_windowing_filter_func,
695 priv->modality_group = gtk_window_group_new ();
696 gtk_window_group_add_window (priv->modality_group, window);
698 _gtk_plug_windowing_realize_toplevel (plug);
702 gdk_window = gdk_window_new (gtk_widget_get_parent_window (widget),
703 &attributes, attributes_mask);
704 gtk_widget_set_window (widget, gdk_window);
707 gdk_window_set_user_data (gdk_window, window);
709 gtk_widget_style_attach (widget);
710 gtk_style_set_background (gtk_widget_get_style (widget),
711 gdk_window, GTK_STATE_NORMAL);
713 gdk_window_enable_synchronized_configure (gdk_window);
717 gtk_plug_show (GtkWidget *widget)
719 if (gtk_widget_is_toplevel (widget))
720 GTK_WIDGET_CLASS (gtk_plug_parent_class)->show (widget);
722 GTK_WIDGET_CLASS (bin_class)->show (widget);
726 gtk_plug_hide (GtkWidget *widget)
728 if (gtk_widget_is_toplevel (widget))
729 GTK_WIDGET_CLASS (gtk_plug_parent_class)->hide (widget);
731 GTK_WIDGET_CLASS (bin_class)->hide (widget);
734 /* From gdkinternals.h */
735 void gdk_synthesize_window_state (GdkWindow *window,
736 GdkWindowState unset_flags,
737 GdkWindowState set_flags);
740 gtk_plug_map (GtkWidget *widget)
742 if (gtk_widget_is_toplevel (widget))
744 GtkBin *bin = GTK_BIN (widget);
745 GtkPlug *plug = GTK_PLUG (widget);
748 gtk_widget_set_mapped (widget, TRUE);
750 child = gtk_bin_get_child (bin);
752 gtk_widget_get_visible (child) &&
753 !gtk_widget_get_mapped (child))
754 gtk_widget_map (child);
756 _gtk_plug_windowing_map_toplevel (plug);
758 gdk_synthesize_window_state (gtk_widget_get_window (widget),
759 GDK_WINDOW_STATE_WITHDRAWN,
763 GTK_WIDGET_CLASS (bin_class)->map (widget);
767 gtk_plug_unmap (GtkWidget *widget)
769 if (gtk_widget_is_toplevel (widget))
771 GtkPlug *plug = GTK_PLUG (widget);
774 window = gtk_widget_get_window (widget);
776 gtk_widget_set_mapped (widget, FALSE);
778 gdk_window_hide (window);
780 _gtk_plug_windowing_unmap_toplevel (plug);
782 gdk_synthesize_window_state (window,
784 GDK_WINDOW_STATE_WITHDRAWN);
787 GTK_WIDGET_CLASS (bin_class)->unmap (widget);
791 gtk_plug_size_allocate (GtkWidget *widget,
792 GtkAllocation *allocation)
796 if (gtk_widget_is_toplevel (widget))
797 GTK_WIDGET_CLASS (gtk_plug_parent_class)->size_allocate (widget, allocation);
800 GtkBin *bin = GTK_BIN (widget);
802 gtk_widget_set_allocation (widget, allocation);
804 if (gtk_widget_get_realized (widget))
805 gdk_window_move_resize (gtk_widget_get_window (widget),
806 allocation->x, allocation->y,
807 allocation->width, allocation->height);
809 child = gtk_bin_get_child (bin);
811 if (child != NULL && gtk_widget_get_visible (child))
813 GtkAllocation child_allocation;
815 child_allocation.x = child_allocation.y = gtk_container_get_border_width (GTK_CONTAINER (widget));
816 child_allocation.width =
817 MAX (1, (gint)allocation->width - child_allocation.x * 2);
818 child_allocation.height =
819 MAX (1, (gint)allocation->height - child_allocation.y * 2);
821 gtk_widget_size_allocate (child, &child_allocation);
828 gtk_plug_key_press_event (GtkWidget *widget,
831 if (gtk_widget_is_toplevel (widget))
832 return GTK_WIDGET_CLASS (gtk_plug_parent_class)->key_press_event (widget, event);
838 gtk_plug_focus_event (GtkWidget *widget,
839 GdkEventFocus *event)
841 /* We eat focus-in events and focus-out events, since they
842 * can be generated by something like a keyboard grab on
843 * a child of the plug.
849 gtk_plug_set_focus (GtkWindow *window,
852 GtkPlug *plug = GTK_PLUG (window);
854 GTK_WINDOW_CLASS (gtk_plug_parent_class)->set_focus (window, focus);
856 /* Ask for focus from embedder
859 if (focus && !gtk_window_has_toplevel_focus (window))
860 _gtk_plug_windowing_set_focus (plug);
864 grabbed_key_hash (gconstpointer a)
866 const GrabbedKey *key = a;
869 h = key->accelerator_key << 16;
870 h ^= key->accelerator_key >> 16;
871 h ^= key->accelerator_mods;
877 grabbed_key_equal (gconstpointer a, gconstpointer b)
879 const GrabbedKey *keya = a;
880 const GrabbedKey *keyb = b;
882 return (keya->accelerator_key == keyb->accelerator_key &&
883 keya->accelerator_mods == keyb->accelerator_mods);
887 add_grabbed_key (gpointer key, gpointer val, gpointer data)
889 GrabbedKey *grabbed_key = key;
890 GtkPlug *plug = data;
891 GtkPlugPrivate *priv = plug->priv;
893 if (!priv->grabbed_keys ||
894 !g_hash_table_lookup (priv->grabbed_keys, grabbed_key))
896 _gtk_plug_windowing_add_grabbed_key (plug,
897 grabbed_key->accelerator_key,
898 grabbed_key->accelerator_mods);
903 add_grabbed_key_always (gpointer key,
907 GrabbedKey *grabbed_key = key;
908 GtkPlug *plug = data;
910 _gtk_plug_windowing_add_grabbed_key (plug,
911 grabbed_key->accelerator_key,
912 grabbed_key->accelerator_mods);
916 * _gtk_plug_add_all_grabbed_keys:
920 * Calls _gtk_plug_windowing_add_grabbed_key() on all the grabbed keys
924 _gtk_plug_add_all_grabbed_keys (GtkPlug *plug)
926 GtkPlugPrivate *priv = plug->priv;
928 if (priv->grabbed_keys)
929 g_hash_table_foreach (priv->grabbed_keys, add_grabbed_key_always, plug);
933 remove_grabbed_key (gpointer key, gpointer val, gpointer data)
935 GrabbedKey *grabbed_key = key;
936 GtkPlug *plug = data;
937 GtkPlugPrivate *priv = plug->priv;
939 if (!priv->grabbed_keys ||
940 !g_hash_table_lookup (priv->grabbed_keys, grabbed_key))
942 _gtk_plug_windowing_remove_grabbed_key (plug,
943 grabbed_key->accelerator_key,
944 grabbed_key->accelerator_mods);
949 keys_foreach (GtkWindow *window,
951 GdkModifierType modifiers,
952 gboolean is_mnemonic,
955 GHashTable *new_grabbed_keys = data;
956 GrabbedKey *key = g_slice_new (GrabbedKey);
958 key->accelerator_key = keyval;
959 key->accelerator_mods = modifiers;
961 g_hash_table_replace (new_grabbed_keys, key, key);
965 grabbed_key_free (gpointer data)
967 g_slice_free (GrabbedKey, data);
971 gtk_plug_keys_changed (GtkWindow *window)
973 GHashTable *new_grabbed_keys, *old_grabbed_keys;
974 GtkPlug *plug = GTK_PLUG (window);
975 GtkPlugPrivate *priv = plug->priv;
977 new_grabbed_keys = g_hash_table_new_full (grabbed_key_hash, grabbed_key_equal, (GDestroyNotify)grabbed_key_free, NULL);
978 _gtk_window_keys_foreach (window, keys_foreach, new_grabbed_keys);
980 if (priv->socket_window)
981 g_hash_table_foreach (new_grabbed_keys, add_grabbed_key, plug);
983 old_grabbed_keys = priv->grabbed_keys;
984 priv->grabbed_keys = new_grabbed_keys;
986 if (old_grabbed_keys)
988 if (priv->socket_window)
989 g_hash_table_foreach (old_grabbed_keys, remove_grabbed_key, plug);
990 g_hash_table_destroy (old_grabbed_keys);
995 gtk_plug_focus (GtkWidget *widget,
996 GtkDirectionType direction)
998 GtkBin *bin = GTK_BIN (widget);
999 GtkPlug *plug = GTK_PLUG (widget);
1000 GtkWindow *window = GTK_WINDOW (widget);
1001 GtkContainer *container = GTK_CONTAINER (widget);
1003 GtkWidget *old_focus_child;
1006 old_focus_child = gtk_container_get_focus_child (container);
1007 /* We override GtkWindow's behavior, since we don't want wrapping here.
1009 if (old_focus_child)
1011 GtkWidget *focus_widget;
1013 if (gtk_widget_child_focus (old_focus_child, direction))
1016 focus_widget = gtk_window_get_focus (window);
1019 /* Wrapped off the end, clear the focus setting for the toplevel */
1020 parent = gtk_widget_get_parent (focus_widget);
1023 gtk_container_set_focus_child (GTK_CONTAINER (parent), NULL);
1024 parent = gtk_widget_get_parent (parent);
1027 gtk_window_set_focus (GTK_WINDOW (container), NULL);
1032 /* Try to focus the first widget in the window */
1033 child = gtk_bin_get_child (bin);
1034 if (child && gtk_widget_child_focus (child, direction))
1038 if (!gtk_container_get_focus_child (GTK_CONTAINER (window)))
1039 _gtk_plug_windowing_focus_to_parent (plug, direction);
1045 gtk_plug_check_resize (GtkContainer *container)
1047 if (gtk_widget_is_toplevel (GTK_WIDGET (container)))
1048 GTK_CONTAINER_CLASS (gtk_plug_parent_class)->check_resize (container);
1050 GTK_CONTAINER_CLASS (bin_class)->check_resize (container);
1054 * _gtk_plug_handle_modality_on:
1058 * Called from the GtkPlug backend when the corresponding socket has
1059 * told the plug that it modality has toggled on.
1062 _gtk_plug_handle_modality_on (GtkPlug *plug)
1064 GtkPlugPrivate *priv = plug->priv;
1066 if (!priv->modality_window)
1068 priv->modality_window = gtk_window_new (GTK_WINDOW_POPUP);
1069 gtk_window_set_screen (GTK_WINDOW (priv->modality_window),
1070 gtk_widget_get_screen (GTK_WIDGET (plug)));
1071 gtk_widget_realize (priv->modality_window);
1072 gtk_window_group_add_window (priv->modality_group, GTK_WINDOW (priv->modality_window));
1073 gtk_grab_add (priv->modality_window);
1078 * _gtk_plug_handle_modality_off:
1082 * Called from the GtkPlug backend when the corresponding socket has
1083 * told the plug that it modality has toggled off.
1086 _gtk_plug_handle_modality_off (GtkPlug *plug)
1088 GtkPlugPrivate *priv = plug->priv;
1090 if (priv->modality_window)
1092 gtk_widget_destroy (priv->modality_window);
1093 priv->modality_window = NULL;
1098 * _gtk_plug_focus_first_last:
1101 * @direction: a direction
1103 * Called from the GtkPlug backend when the corresponding socket has
1104 * told the plug that it has received the focus.
1107 _gtk_plug_focus_first_last (GtkPlug *plug,
1108 GtkDirectionType direction)
1110 GtkWindow *window = GTK_WINDOW (plug);
1111 GtkWidget *focus_widget;
1114 focus_widget = gtk_window_get_focus (window);
1117 parent = gtk_widget_get_parent (focus_widget);
1120 gtk_container_set_focus_child (GTK_CONTAINER (parent), NULL);
1121 parent = gtk_widget_get_parent (parent);
1124 gtk_window_set_focus (GTK_WINDOW (plug), NULL);
1127 gtk_widget_child_focus (GTK_WIDGET (plug), direction);