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 "gtkwidgetprivate.h"
37 #include "gtkwindow.h"
41 * @Short_description: Toplevel for embedding into other processes
43 * @See_also: #GtkSocket
45 * Together with #GtkSocket, #GtkPlug provides the ability
46 * to embed widgets from one process into another process
47 * in a fashion that is transparent to the user. One
48 * process creates a #GtkSocket widget and passes the
49 * ID of that widget's window to the other process,
50 * which then creates a #GtkPlug with that window ID.
51 * Any widgets contained in the #GtkPlug then will appear
52 * inside the first application's window.
55 * The #GtkPlug and #GtkSocket widgets are currently not available
56 * on all platforms supported by GTK+.
60 static void gtk_plug_get_property (GObject *object,
64 static void gtk_plug_finalize (GObject *object);
65 static void gtk_plug_realize (GtkWidget *widget);
66 static void gtk_plug_unrealize (GtkWidget *widget);
67 static void gtk_plug_show (GtkWidget *widget);
68 static void gtk_plug_hide (GtkWidget *widget);
69 static void gtk_plug_map (GtkWidget *widget);
70 static void gtk_plug_unmap (GtkWidget *widget);
71 static void gtk_plug_size_allocate (GtkWidget *widget,
72 GtkAllocation *allocation);
73 static gboolean gtk_plug_key_press_event (GtkWidget *widget,
75 static gboolean gtk_plug_focus_event (GtkWidget *widget,
76 GdkEventFocus *event);
77 static void gtk_plug_set_focus (GtkWindow *window,
79 static gboolean gtk_plug_focus (GtkWidget *widget,
80 GtkDirectionType direction);
81 static void gtk_plug_check_resize (GtkContainer *container);
82 static void gtk_plug_keys_changed (GtkWindow *window);
84 static GtkBinClass *bin_class = NULL;
88 guint accelerator_key;
89 GdkModifierType accelerator_mods;
103 static guint plug_signals[LAST_SIGNAL] = { 0 };
105 G_DEFINE_TYPE (GtkPlug, gtk_plug, GTK_TYPE_WINDOW)
108 gtk_plug_get_property (GObject *object,
113 GtkPlug *plug = GTK_PLUG (object);
114 GtkPlugPrivate *priv = plug->priv;
119 g_value_set_boolean (value, priv->socket_window != NULL);
121 case PROP_SOCKET_WINDOW:
122 g_value_set_object (value, priv->socket_window);
125 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
131 gtk_plug_class_init (GtkPlugClass *class)
133 GObjectClass *gobject_class = (GObjectClass *)class;
134 GtkWidgetClass *widget_class = (GtkWidgetClass *)class;
135 GtkWindowClass *window_class = (GtkWindowClass *)class;
136 GtkContainerClass *container_class = (GtkContainerClass *)class;
138 bin_class = g_type_class_peek (GTK_TYPE_BIN);
140 gobject_class->get_property = gtk_plug_get_property;
141 gobject_class->finalize = gtk_plug_finalize;
143 widget_class->realize = gtk_plug_realize;
144 widget_class->unrealize = gtk_plug_unrealize;
145 widget_class->key_press_event = gtk_plug_key_press_event;
146 widget_class->focus_in_event = gtk_plug_focus_event;
147 widget_class->focus_out_event = gtk_plug_focus_event;
149 widget_class->show = gtk_plug_show;
150 widget_class->hide = gtk_plug_hide;
151 widget_class->map = gtk_plug_map;
152 widget_class->unmap = gtk_plug_unmap;
153 widget_class->size_allocate = gtk_plug_size_allocate;
155 widget_class->focus = gtk_plug_focus;
157 container_class->check_resize = gtk_plug_check_resize;
159 window_class->set_focus = gtk_plug_set_focus;
160 window_class->keys_changed = gtk_plug_keys_changed;
165 * %TRUE if the plug is embedded in a socket.
169 g_object_class_install_property (gobject_class,
171 g_param_spec_boolean ("embedded",
173 P_("Whether the plug is embedded"),
175 GTK_PARAM_READABLE));
178 * GtkPlug:socket-window:
180 * The window of the socket the plug is embedded in.
184 g_object_class_install_property (gobject_class,
186 g_param_spec_object ("socket-window",
188 P_("The window of the socket the plug is embedded in"),
190 GTK_PARAM_READABLE));
194 * @plug: the object on which the signal was emitted
196 * Gets emitted when the plug becomes embedded in a socket.
198 plug_signals[EMBEDDED] =
199 g_signal_new (I_("embedded"),
200 G_OBJECT_CLASS_TYPE (class),
202 G_STRUCT_OFFSET (GtkPlugClass, embedded),
204 _gtk_marshal_VOID__VOID,
207 g_type_class_add_private (class, sizeof (GtkPlugPrivate));
211 gtk_plug_init (GtkPlug *plug)
213 plug->priv = G_TYPE_INSTANCE_GET_PRIVATE (plug,
219 gtk_plug_set_is_child (GtkPlug *plug,
222 GtkPlugPrivate *priv = plug->priv;
223 GtkWidget *widget = GTK_WIDGET (plug);
225 g_assert (!gtk_widget_get_parent (widget));
229 if (priv->modality_window)
230 _gtk_plug_handle_modality_off (plug);
232 if (priv->modality_group)
234 gtk_window_group_remove_window (priv->modality_group, GTK_WINDOW (plug));
235 g_object_unref (priv->modality_group);
236 priv->modality_group = NULL;
239 /* As a toplevel, the MAPPED flag doesn't correspond
240 * to whether the widget->window is mapped; we unmap
241 * here, but don't bother remapping -- we will get mapped
242 * by gtk_widget_set_parent ().
244 if (gtk_widget_get_mapped (widget))
245 gtk_widget_unmap (widget);
247 _gtk_window_set_is_toplevel (GTK_WINDOW (plug), FALSE);
248 gtk_container_set_resize_mode (GTK_CONTAINER (plug), GTK_RESIZE_PARENT);
250 _gtk_widget_propagate_hierarchy_changed (widget, widget);
254 if (gtk_window_get_focus (GTK_WINDOW (plug)))
255 gtk_window_set_focus (GTK_WINDOW (plug), NULL);
256 if (gtk_window_get_default_widget (GTK_WINDOW (plug)))
257 gtk_window_set_default (GTK_WINDOW (plug), NULL);
259 priv->modality_group = gtk_window_group_new ();
260 gtk_window_group_add_window (priv->modality_group, GTK_WINDOW (plug));
262 _gtk_window_set_is_toplevel (GTK_WINDOW (plug), TRUE);
263 gtk_container_set_resize_mode (GTK_CONTAINER (plug), GTK_RESIZE_QUEUE);
265 _gtk_widget_propagate_hierarchy_changed (GTK_WIDGET (plug), NULL);
273 * Gets the window ID of a #GtkPlug widget, which can then
274 * be used to embed this window inside another window, for
275 * instance with gtk_socket_add_id().
277 * Return value: the window ID for the plug
280 gtk_plug_get_id (GtkPlug *plug)
282 g_return_val_if_fail (GTK_IS_PLUG (plug), 0);
284 if (!gtk_widget_get_realized (GTK_WIDGET (plug)))
285 gtk_widget_realize (GTK_WIDGET (plug));
287 return _gtk_plug_windowing_get_id (plug);
291 * gtk_plug_get_embedded:
294 * Determines whether the plug is embedded in a socket.
296 * Return value: %TRUE if the plug is embedded in a socket
301 gtk_plug_get_embedded (GtkPlug *plug)
303 g_return_val_if_fail (GTK_IS_PLUG (plug), FALSE);
305 return plug->priv->socket_window != NULL;
309 * gtk_plug_get_socket_window:
312 * Retrieves the socket the plug is embedded in.
314 * Return value: (transfer none): the window of the socket, or %NULL
319 gtk_plug_get_socket_window (GtkPlug *plug)
321 g_return_val_if_fail (GTK_IS_PLUG (plug), NULL);
323 return plug->priv->socket_window;
327 * _gtk_plug_add_to_socket:
329 * @socket_: a #GtkSocket
331 * Adds a plug to a socket within the same application.
334 _gtk_plug_add_to_socket (GtkPlug *plug,
337 GtkPlugPrivate *priv;
340 g_return_if_fail (GTK_IS_PLUG (plug));
341 g_return_if_fail (GTK_IS_SOCKET (socket_));
342 g_return_if_fail (gtk_widget_get_realized (GTK_WIDGET (socket_)));
345 widget = GTK_WIDGET (plug);
347 gtk_plug_set_is_child (plug, TRUE);
348 priv->same_app = TRUE;
349 socket_->same_app = TRUE;
350 socket_->plug_widget = widget;
352 priv->socket_window = gtk_widget_get_window (GTK_WIDGET (socket_));
353 g_object_ref (priv->socket_window);
354 g_signal_emit (plug, plug_signals[EMBEDDED], 0);
355 g_object_notify (G_OBJECT (plug), "embedded");
357 if (gtk_widget_get_realized (widget))
361 window = gtk_widget_get_window (widget);
362 gdk_window_reparent (window, priv->socket_window,
363 -gdk_window_get_width (window),
364 -gdk_window_get_height (window));
367 gtk_widget_set_parent (widget, GTK_WIDGET (socket_));
369 g_signal_emit_by_name (socket_, "plug-added");
373 * _gtk_plug_send_delete_event:
374 * @widget: a #GtkWidget
376 * Send a GDK_DELETE event to the @widget and destroy it if
377 * necessary. Internal GTK function, called from this file or the
378 * backend-specific GtkPlug implementation.
381 _gtk_plug_send_delete_event (GtkWidget *widget)
383 GdkEvent *event = gdk_event_new (GDK_DELETE);
385 event->any.window = g_object_ref (gtk_widget_get_window (widget));
386 event->any.send_event = FALSE;
388 g_object_ref (widget);
390 if (!gtk_widget_event (widget, event))
391 gtk_widget_destroy (widget);
393 g_object_unref (widget);
395 gdk_event_free (event);
399 * _gtk_plug_remove_from_socket:
401 * @socket_: a #GtkSocket
403 * Removes a plug from a socket within the same application.
406 _gtk_plug_remove_from_socket (GtkPlug *plug,
409 GtkPlugPrivate *priv;
413 gboolean widget_was_visible;
415 g_return_if_fail (GTK_IS_PLUG (plug));
416 g_return_if_fail (GTK_IS_SOCKET (socket_));
417 g_return_if_fail (gtk_widget_get_realized (GTK_WIDGET (plug)));
420 widget = GTK_WIDGET (plug);
422 if (_gtk_widget_get_in_reparent (widget))
426 g_object_ref (socket_);
428 widget_was_visible = gtk_widget_get_visible (widget);
429 window = gtk_widget_get_window (widget);
431 gdk_window_hide (window);
432 _gtk_widget_set_in_reparent (widget, TRUE);
433 gdk_window_reparent (window,
434 gtk_widget_get_root_window (widget),
436 gtk_widget_unparent (GTK_WIDGET (plug));
437 _gtk_widget_set_in_reparent (widget, FALSE);
439 socket_->plug_widget = NULL;
440 if (socket_->plug_window != NULL)
442 g_object_unref (socket_->plug_window);
443 socket_->plug_window = NULL;
446 socket_->same_app = FALSE;
448 priv->same_app = FALSE;
449 if (priv->socket_window != NULL)
451 g_object_unref (priv->socket_window);
452 priv->socket_window = NULL;
454 gtk_plug_set_is_child (plug, FALSE);
456 g_signal_emit_by_name (socket_, "plug-removed", &result);
458 gtk_widget_destroy (GTK_WIDGET (socket_));
461 _gtk_plug_send_delete_event (widget);
463 g_object_unref (plug);
465 if (widget_was_visible && gtk_widget_get_visible (GTK_WIDGET (socket_)))
466 gtk_widget_queue_resize (GTK_WIDGET (socket_));
468 g_object_unref (socket_);
472 * gtk_plug_construct:
474 * @socket_id: the XID of the socket's window.
476 * Finish the initialization of @plug for a given #GtkSocket identified by
477 * @socket_id. This function will generally only be used by classes deriving from #GtkPlug.
480 gtk_plug_construct (GtkPlug *plug,
481 GdkNativeWindow socket_id)
483 gtk_plug_construct_for_display (plug, gdk_display_get_default (), socket_id);
487 * gtk_plug_construct_for_display:
489 * @display: the #GdkDisplay associated with @socket_id's
491 * @socket_id: the XID of the socket's window.
493 * Finish the initialization of @plug for a given #GtkSocket identified by
494 * @socket_id which is currently displayed on @display.
495 * This function will generally only be used by classes deriving from #GtkPlug.
500 gtk_plug_construct_for_display (GtkPlug *plug,
502 GdkNativeWindow socket_id)
504 GtkPlugPrivate *priv;
506 g_return_if_fail (GTK_IS_PLUG (plug));
507 g_return_if_fail (GDK_IS_DISPLAY (display));
513 gpointer user_data = NULL;
515 priv->socket_window = gdk_window_lookup_for_display (display, socket_id);
516 if (priv->socket_window)
518 gdk_window_get_user_data (priv->socket_window, &user_data);
522 if (GTK_IS_SOCKET (user_data))
523 _gtk_plug_add_to_socket (plug, user_data);
526 g_warning (G_STRLOC "Can't create GtkPlug as child of non-GtkSocket");
527 priv->socket_window = NULL;
531 g_object_ref (priv->socket_window);
534 priv->socket_window = gdk_window_foreign_new_for_display (display, socket_id);
536 if (priv->socket_window) {
537 g_signal_emit (plug, plug_signals[EMBEDDED], 0);
539 g_object_notify (G_OBJECT (plug), "embedded");
546 * @socket_id: the window ID of the socket, or 0.
548 * Creates a new plug widget inside the #GtkSocket identified
549 * by @socket_id. If @socket_id is 0, the plug is left "unplugged" and
550 * can later be plugged into a #GtkSocket by gtk_socket_add_id().
552 * Return value: the new #GtkPlug widget.
555 gtk_plug_new (GdkNativeWindow socket_id)
557 return gtk_plug_new_for_display (gdk_display_get_default (), socket_id);
561 * gtk_plug_new_for_display:
562 * @display: the #GdkDisplay on which @socket_id is displayed
563 * @socket_id: the XID of the socket's window.
565 * Create a new plug widget inside the #GtkSocket identified by socket_id.
567 * Return value: the new #GtkPlug widget.
572 gtk_plug_new_for_display (GdkDisplay *display,
573 GdkNativeWindow socket_id)
577 g_return_val_if_fail (GDK_IS_DISPLAY (display), NULL);
579 plug = g_object_new (GTK_TYPE_PLUG, NULL);
580 gtk_plug_construct_for_display (plug, display, socket_id);
581 return GTK_WIDGET (plug);
585 gtk_plug_finalize (GObject *object)
587 GtkPlug *plug = GTK_PLUG (object);
588 GtkPlugPrivate *priv = plug->priv;
590 if (priv->grabbed_keys)
592 g_hash_table_destroy (priv->grabbed_keys);
593 priv->grabbed_keys = NULL;
596 G_OBJECT_CLASS (gtk_plug_parent_class)->finalize (object);
600 gtk_plug_unrealize (GtkWidget *widget)
602 GtkPlug *plug = GTK_PLUG (widget);
603 GtkPlugPrivate *priv = plug->priv;
605 if (priv->socket_window != NULL)
607 gdk_window_set_user_data (priv->socket_window, NULL);
608 g_object_unref (priv->socket_window);
609 priv->socket_window = NULL;
611 g_object_notify (G_OBJECT (widget), "embedded");
616 if (priv->modality_window)
617 _gtk_plug_handle_modality_off (plug);
619 gtk_window_group_remove_window (priv->modality_group, GTK_WINDOW (plug));
620 g_object_unref (priv->modality_group);
623 GTK_WIDGET_CLASS (gtk_plug_parent_class)->unrealize (widget);
627 gtk_plug_realize (GtkWidget *widget)
629 GtkAllocation allocation;
630 GtkPlug *plug = GTK_PLUG (widget);
631 GtkPlugPrivate *priv = plug->priv;
632 GtkWindow *window = GTK_WINDOW (widget);
633 GdkWindow *gdk_window;
634 GdkWindowAttr attributes;
636 gchar *wmclass_name, *wmclass_class;
637 gint attributes_mask;
639 gtk_widget_set_realized (widget, TRUE);
641 title = gtk_window_get_title (window);
642 _gtk_window_get_wmclass (window, &wmclass_name, &wmclass_class);
643 gtk_widget_get_allocation (widget, &allocation);
645 attributes.window_type = GDK_WINDOW_CHILD; /* XXX GDK_WINDOW_PLUG ? */
646 attributes.title = (gchar *) title;
647 attributes.wmclass_name = wmclass_name;
648 attributes.wmclass_class = wmclass_class;
649 attributes.width = allocation.width;
650 attributes.height = allocation.height;
651 attributes.wclass = GDK_INPUT_OUTPUT;
653 /* this isn't right - we should match our parent's visual/colormap.
654 * though that will require handling "foreign" colormaps */
655 attributes.visual = gtk_widget_get_visual (widget);
656 attributes.event_mask = gtk_widget_get_events (widget);
657 attributes.event_mask |= (GDK_EXPOSURE_MASK |
659 GDK_KEY_RELEASE_MASK |
660 GDK_ENTER_NOTIFY_MASK |
661 GDK_LEAVE_NOTIFY_MASK |
664 attributes_mask = GDK_WA_VISUAL;
665 attributes_mask |= (title ? GDK_WA_TITLE : 0);
666 attributes_mask |= (g_strdup (g_get_prgname ()) ? GDK_WA_WMCLASS : 0);
668 if (gtk_widget_is_toplevel (widget))
670 attributes.window_type = GDK_WINDOW_TOPLEVEL;
672 gdk_error_trap_push ();
673 if (priv->socket_window)
674 gdk_window = gdk_window_new (priv->socket_window,
675 &attributes, attributes_mask);
676 else /* If it's a passive plug, we use the root window */
677 gdk_window = gdk_window_new (gtk_widget_get_root_window (widget),
678 &attributes, attributes_mask);
679 gtk_widget_set_window (widget, gdk_window);
681 gdk_display_sync (gtk_widget_get_display (widget));
682 if (gdk_error_trap_pop ()) /* Uh-oh */
684 gdk_error_trap_push ();
685 gdk_window_destroy (gdk_window);
686 gdk_error_trap_pop_ignored ();
687 gdk_window = gdk_window_new (gtk_widget_get_root_window (widget),
688 &attributes, attributes_mask);
689 gtk_widget_set_window (widget, gdk_window);
692 gdk_window_add_filter (gdk_window,
693 _gtk_plug_windowing_filter_func,
696 priv->modality_group = gtk_window_group_new ();
697 gtk_window_group_add_window (priv->modality_group, window);
699 _gtk_plug_windowing_realize_toplevel (plug);
703 gdk_window = gdk_window_new (gtk_widget_get_parent_window (widget),
704 &attributes, attributes_mask);
705 gtk_widget_set_window (widget, gdk_window);
708 gdk_window_set_user_data (gdk_window, window);
710 gtk_widget_style_attach (widget);
711 gtk_style_set_background (gtk_widget_get_style (widget),
712 gdk_window, GTK_STATE_NORMAL);
714 gdk_window_enable_synchronized_configure (gdk_window);
718 gtk_plug_show (GtkWidget *widget)
720 if (gtk_widget_is_toplevel (widget))
721 GTK_WIDGET_CLASS (gtk_plug_parent_class)->show (widget);
723 GTK_WIDGET_CLASS (bin_class)->show (widget);
727 gtk_plug_hide (GtkWidget *widget)
729 if (gtk_widget_is_toplevel (widget))
730 GTK_WIDGET_CLASS (gtk_plug_parent_class)->hide (widget);
732 GTK_WIDGET_CLASS (bin_class)->hide (widget);
735 /* From gdkinternals.h */
736 void gdk_synthesize_window_state (GdkWindow *window,
737 GdkWindowState unset_flags,
738 GdkWindowState set_flags);
741 gtk_plug_map (GtkWidget *widget)
743 if (gtk_widget_is_toplevel (widget))
745 GtkBin *bin = GTK_BIN (widget);
746 GtkPlug *plug = GTK_PLUG (widget);
749 gtk_widget_set_mapped (widget, TRUE);
751 child = gtk_bin_get_child (bin);
753 gtk_widget_get_visible (child) &&
754 !gtk_widget_get_mapped (child))
755 gtk_widget_map (child);
757 _gtk_plug_windowing_map_toplevel (plug);
759 gdk_synthesize_window_state (gtk_widget_get_window (widget),
760 GDK_WINDOW_STATE_WITHDRAWN,
764 GTK_WIDGET_CLASS (bin_class)->map (widget);
768 gtk_plug_unmap (GtkWidget *widget)
770 if (gtk_widget_is_toplevel (widget))
772 GtkPlug *plug = GTK_PLUG (widget);
775 window = gtk_widget_get_window (widget);
777 gtk_widget_set_mapped (widget, FALSE);
779 gdk_window_hide (window);
781 _gtk_plug_windowing_unmap_toplevel (plug);
783 gdk_synthesize_window_state (window,
785 GDK_WINDOW_STATE_WITHDRAWN);
788 GTK_WIDGET_CLASS (bin_class)->unmap (widget);
792 gtk_plug_size_allocate (GtkWidget *widget,
793 GtkAllocation *allocation)
797 if (gtk_widget_is_toplevel (widget))
798 GTK_WIDGET_CLASS (gtk_plug_parent_class)->size_allocate (widget, allocation);
801 GtkBin *bin = GTK_BIN (widget);
803 gtk_widget_set_allocation (widget, allocation);
805 if (gtk_widget_get_realized (widget))
806 gdk_window_move_resize (gtk_widget_get_window (widget),
807 allocation->x, allocation->y,
808 allocation->width, allocation->height);
810 child = gtk_bin_get_child (bin);
812 if (child != NULL && gtk_widget_get_visible (child))
814 GtkAllocation child_allocation;
816 child_allocation.x = child_allocation.y = gtk_container_get_border_width (GTK_CONTAINER (widget));
817 child_allocation.width =
818 MAX (1, (gint)allocation->width - child_allocation.x * 2);
819 child_allocation.height =
820 MAX (1, (gint)allocation->height - child_allocation.y * 2);
822 gtk_widget_size_allocate (child, &child_allocation);
829 gtk_plug_key_press_event (GtkWidget *widget,
832 if (gtk_widget_is_toplevel (widget))
833 return GTK_WIDGET_CLASS (gtk_plug_parent_class)->key_press_event (widget, event);
839 gtk_plug_focus_event (GtkWidget *widget,
840 GdkEventFocus *event)
842 /* We eat focus-in events and focus-out events, since they
843 * can be generated by something like a keyboard grab on
844 * a child of the plug.
850 gtk_plug_set_focus (GtkWindow *window,
853 GtkPlug *plug = GTK_PLUG (window);
855 GTK_WINDOW_CLASS (gtk_plug_parent_class)->set_focus (window, focus);
857 /* Ask for focus from embedder
860 if (focus && !gtk_window_has_toplevel_focus (window))
861 _gtk_plug_windowing_set_focus (plug);
865 grabbed_key_hash (gconstpointer a)
867 const GrabbedKey *key = a;
870 h = key->accelerator_key << 16;
871 h ^= key->accelerator_key >> 16;
872 h ^= key->accelerator_mods;
878 grabbed_key_equal (gconstpointer a, gconstpointer b)
880 const GrabbedKey *keya = a;
881 const GrabbedKey *keyb = b;
883 return (keya->accelerator_key == keyb->accelerator_key &&
884 keya->accelerator_mods == keyb->accelerator_mods);
888 add_grabbed_key (gpointer key, gpointer val, gpointer data)
890 GrabbedKey *grabbed_key = key;
891 GtkPlug *plug = data;
892 GtkPlugPrivate *priv = plug->priv;
894 if (!priv->grabbed_keys ||
895 !g_hash_table_lookup (priv->grabbed_keys, grabbed_key))
897 _gtk_plug_windowing_add_grabbed_key (plug,
898 grabbed_key->accelerator_key,
899 grabbed_key->accelerator_mods);
904 add_grabbed_key_always (gpointer key,
908 GrabbedKey *grabbed_key = key;
909 GtkPlug *plug = data;
911 _gtk_plug_windowing_add_grabbed_key (plug,
912 grabbed_key->accelerator_key,
913 grabbed_key->accelerator_mods);
917 * _gtk_plug_add_all_grabbed_keys:
921 * Calls _gtk_plug_windowing_add_grabbed_key() on all the grabbed keys
925 _gtk_plug_add_all_grabbed_keys (GtkPlug *plug)
927 GtkPlugPrivate *priv = plug->priv;
929 if (priv->grabbed_keys)
930 g_hash_table_foreach (priv->grabbed_keys, add_grabbed_key_always, plug);
934 remove_grabbed_key (gpointer key, gpointer val, gpointer data)
936 GrabbedKey *grabbed_key = key;
937 GtkPlug *plug = data;
938 GtkPlugPrivate *priv = plug->priv;
940 if (!priv->grabbed_keys ||
941 !g_hash_table_lookup (priv->grabbed_keys, grabbed_key))
943 _gtk_plug_windowing_remove_grabbed_key (plug,
944 grabbed_key->accelerator_key,
945 grabbed_key->accelerator_mods);
950 keys_foreach (GtkWindow *window,
952 GdkModifierType modifiers,
953 gboolean is_mnemonic,
956 GHashTable *new_grabbed_keys = data;
957 GrabbedKey *key = g_slice_new (GrabbedKey);
959 key->accelerator_key = keyval;
960 key->accelerator_mods = modifiers;
962 g_hash_table_replace (new_grabbed_keys, key, key);
966 grabbed_key_free (gpointer data)
968 g_slice_free (GrabbedKey, data);
972 gtk_plug_keys_changed (GtkWindow *window)
974 GHashTable *new_grabbed_keys, *old_grabbed_keys;
975 GtkPlug *plug = GTK_PLUG (window);
976 GtkPlugPrivate *priv = plug->priv;
978 new_grabbed_keys = g_hash_table_new_full (grabbed_key_hash, grabbed_key_equal, (GDestroyNotify)grabbed_key_free, NULL);
979 _gtk_window_keys_foreach (window, keys_foreach, new_grabbed_keys);
981 if (priv->socket_window)
982 g_hash_table_foreach (new_grabbed_keys, add_grabbed_key, plug);
984 old_grabbed_keys = priv->grabbed_keys;
985 priv->grabbed_keys = new_grabbed_keys;
987 if (old_grabbed_keys)
989 if (priv->socket_window)
990 g_hash_table_foreach (old_grabbed_keys, remove_grabbed_key, plug);
991 g_hash_table_destroy (old_grabbed_keys);
996 gtk_plug_focus (GtkWidget *widget,
997 GtkDirectionType direction)
999 GtkBin *bin = GTK_BIN (widget);
1000 GtkPlug *plug = GTK_PLUG (widget);
1001 GtkWindow *window = GTK_WINDOW (widget);
1002 GtkContainer *container = GTK_CONTAINER (widget);
1004 GtkWidget *old_focus_child;
1007 old_focus_child = gtk_container_get_focus_child (container);
1008 /* We override GtkWindow's behavior, since we don't want wrapping here.
1010 if (old_focus_child)
1012 GtkWidget *focus_widget;
1014 if (gtk_widget_child_focus (old_focus_child, direction))
1017 focus_widget = gtk_window_get_focus (window);
1020 /* Wrapped off the end, clear the focus setting for the toplevel */
1021 parent = gtk_widget_get_parent (focus_widget);
1024 gtk_container_set_focus_child (GTK_CONTAINER (parent), NULL);
1025 parent = gtk_widget_get_parent (parent);
1028 gtk_window_set_focus (GTK_WINDOW (container), NULL);
1033 /* Try to focus the first widget in the window */
1034 child = gtk_bin_get_child (bin);
1035 if (child && gtk_widget_child_focus (child, direction))
1039 if (!gtk_container_get_focus_child (GTK_CONTAINER (window)))
1040 _gtk_plug_windowing_focus_to_parent (plug, direction);
1046 gtk_plug_check_resize (GtkContainer *container)
1048 if (gtk_widget_is_toplevel (GTK_WIDGET (container)))
1049 GTK_CONTAINER_CLASS (gtk_plug_parent_class)->check_resize (container);
1051 GTK_CONTAINER_CLASS (bin_class)->check_resize (container);
1055 * _gtk_plug_handle_modality_on:
1059 * Called from the GtkPlug backend when the corresponding socket has
1060 * told the plug that it modality has toggled on.
1063 _gtk_plug_handle_modality_on (GtkPlug *plug)
1065 GtkPlugPrivate *priv = plug->priv;
1067 if (!priv->modality_window)
1069 priv->modality_window = gtk_window_new (GTK_WINDOW_POPUP);
1070 gtk_window_set_screen (GTK_WINDOW (priv->modality_window),
1071 gtk_widget_get_screen (GTK_WIDGET (plug)));
1072 gtk_widget_realize (priv->modality_window);
1073 gtk_window_group_add_window (priv->modality_group, GTK_WINDOW (priv->modality_window));
1074 gtk_grab_add (priv->modality_window);
1079 * _gtk_plug_handle_modality_off:
1083 * Called from the GtkPlug backend when the corresponding socket has
1084 * told the plug that it modality has toggled off.
1087 _gtk_plug_handle_modality_off (GtkPlug *plug)
1089 GtkPlugPrivate *priv = plug->priv;
1091 if (priv->modality_window)
1093 gtk_widget_destroy (priv->modality_window);
1094 priv->modality_window = NULL;
1099 * _gtk_plug_focus_first_last:
1102 * @direction: a direction
1104 * Called from the GtkPlug backend when the corresponding socket has
1105 * told the plug that it has received the focus.
1108 _gtk_plug_focus_first_last (GtkPlug *plug,
1109 GtkDirectionType direction)
1111 GtkWindow *window = GTK_WINDOW (plug);
1112 GtkWidget *focus_widget;
1115 focus_widget = gtk_window_get_focus (window);
1118 parent = gtk_widget_get_parent (focus_widget);
1121 gtk_container_set_focus_child (GTK_CONTAINER (parent), NULL);
1122 parent = gtk_widget_get_parent (parent);
1125 gtk_window_set_focus (GTK_WINDOW (plug), NULL);
1128 gtk_widget_child_focus (GTK_WIDGET (plug), direction);