From 88623b98a7ca0afca08ac21bc3aa662c6ed3ab7f Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Wed, 6 Feb 2013 01:20:02 -0500 Subject: [PATCH] Allow to populate bubbles with extra content We do this by making the ::populate-popup signals a little more flexible. They used to just accept a GtkMenu as argument, now they can take a menu or a toolbar. To not break the expectations of existing callbacks, we only emit ::populate-popup with a toolbar if the :populate-toolbar property is TRUE. --- gtk/gtkentry.c | 47 ++++++++++++++++++++++++++++++++++++++++------- gtk/gtkentry.h | 2 +- gtk/gtktextview.c | 46 ++++++++++++++++++++++++++++++++++++++++------ gtk/gtktextview.h | 2 +- 4 files changed, 82 insertions(+), 15 deletions(-) diff --git a/gtk/gtkentry.c b/gtk/gtkentry.c index 27fceb1bcb..3354c90a50 100644 --- a/gtk/gtkentry.c +++ b/gtk/gtkentry.c @@ -67,6 +67,7 @@ #include "gtkstylecontextprivate.h" #include "gtktexthandleprivate.h" #include "gtkbubblewindowprivate.h" +#include "gtktoolbar.h" #include "a11y/gtkentryaccessible.h" @@ -222,6 +223,7 @@ struct _GtkEntryPrivate guint truncate_multiline : 1; guint cursor_handle_dragged : 1; guint selection_handle_dragged : 1; + guint populate_toolbar : 1; }; struct _EntryIconInfo @@ -318,7 +320,8 @@ enum { PROP_COMPLETION, PROP_INPUT_PURPOSE, PROP_INPUT_HINTS, - PROP_ATTRIBUTES + PROP_ATTRIBUTES, + PROP_POPULATE_TOOLBAR }; static guint signals[LAST_SIGNAL] = { 0 }; @@ -1422,6 +1425,21 @@ gtk_entry_class_init (GtkEntryClass *class) PANGO_TYPE_ATTR_LIST, GTK_PARAM_READWRITE)); + /** GtkEntry:populate-toolbar: + * + * If ::populate-toolbar is %TRUE, the #GtkEntry::populate-popup + * signal is also emitted for touch popups. + * + * Since: 3.8 + */ + g_object_class_install_property (gobject_class, + PROP_POPULATE_TOOLBAR, + g_param_spec_boolean ("populate-toolbar", + P_("Populate toolbar"), + P_("Whether to emit ::populate-popup for touch popups"), + FALSE, + GTK_PARAM_READWRITE)); + /** * GtkEntry:icon-prelight: * @@ -1480,13 +1498,17 @@ gtk_entry_class_init (GtkEntryClass *class) /** * GtkEntry::populate-popup: * @entry: The entry on which the signal is emitted - * @menu: the menu that is being populated + * @popup: the menu or toolbar that is being populated * - * The ::populate-popup signal gets emitted before showing the - * context menu of the entry. + * The ::populate-popup signal gets emitted before showing the + * context menu of the entry. * * If you need to add items to the context menu, connect - * to this signal and append your menuitems to the @menu. + * to this signal and append your items to the @widget. + * + * If #GtkEntry::populate-toolbar is %TRUE, this signal will + * also be emitted to populate touch popups. In this case, + * @widget will be a toolbar instead of a menu. */ signals[POPULATE_POPUP] = g_signal_new (I_("populate-popup"), @@ -1496,7 +1518,7 @@ gtk_entry_class_init (GtkEntryClass *class) NULL, NULL, _gtk_marshal_VOID__OBJECT, G_TYPE_NONE, 1, - GTK_TYPE_MENU); + GTK_TYPE_WIDGET); /* Action signals */ @@ -2238,6 +2260,10 @@ gtk_entry_set_property (GObject *object, gtk_entry_set_attributes (entry, g_value_get_boxed (value)); break; + case PROP_POPULATE_TOOLBAR: + entry->priv->populate_toolbar = g_value_get_boolean (value); + break; + case PROP_SCROLL_OFFSET: case PROP_CURSOR_POSITION: default: @@ -2474,6 +2500,10 @@ gtk_entry_get_property (GObject *object, g_value_set_boxed (value, priv->attrs); break; + case PROP_POPULATE_TOOLBAR: + g_value_set_boolean (value, priv->populate_toolbar); + break; + default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); break; @@ -9307,7 +9337,7 @@ bubble_targets_received (GtkClipboard *clipboard, gtk_widget_destroy (priv->selection_bubble); priv->selection_bubble = gtk_bubble_window_new (); - toolbar = gtk_toolbar_new (); + toolbar = GTK_WIDGET (gtk_toolbar_new ()); gtk_toolbar_set_style (GTK_TOOLBAR (toolbar), GTK_TOOLBAR_TEXT); gtk_toolbar_set_show_arrow (GTK_TOOLBAR (toolbar), FALSE); gtk_widget_show (toolbar); @@ -9325,6 +9355,9 @@ bubble_targets_received (GtkClipboard *clipboard, append_bubble_action (entry, toolbar, GTK_STOCK_PASTE, "paste-clipboard", priv->editable && has_clipboard); + if (priv->populate_toolbar) + g_signal_emit (entry, signals[POPULATE_POPUP], 0, toolbar); + gtk_widget_get_allocation (GTK_WIDGET (entry), &allocation); gtk_entry_get_cursor_locations (entry, CURSOR_STANDARD, &start_x, NULL); diff --git a/gtk/gtkentry.h b/gtk/gtkentry.h index 425547f9ea..db80b9e5a5 100644 --- a/gtk/gtkentry.h +++ b/gtk/gtkentry.h @@ -85,7 +85,7 @@ struct _GtkEntryClass /* Hook to customize right-click popup */ void (* populate_popup) (GtkEntry *entry, - GtkMenu *menu); + GtkWidget *popup); /* Action signals */ diff --git a/gtk/gtktextview.c b/gtk/gtktextview.c index 1510f1fb02..8efb5870ef 100644 --- a/gtk/gtktextview.c +++ b/gtk/gtktextview.c @@ -54,6 +54,7 @@ #include "gtkstylecontextprivate.h" #include "gtkcssstylepropertyprivate.h" #include "gtkbubblewindowprivate.h" +#include "gtktoolbar.h" #include "a11y/gtktextviewaccessibleprivate.h" @@ -238,6 +239,7 @@ struct _GtkTextViewPrivate guint vscroll_policy : 1; guint cursor_handle_dragged : 1; guint selection_handle_dragged : 1; + guint populate_toolbar : 1; }; struct _GtkTextPendingScroll @@ -292,7 +294,8 @@ enum PROP_HSCROLL_POLICY, PROP_VSCROLL_POLICY, PROP_INPUT_PURPOSE, - PROP_INPUT_HINTS + PROP_INPUT_HINTS, + PROP_POPULATE_TOOLBAR }; static void gtk_text_view_finalize (GObject *object); @@ -860,6 +863,22 @@ gtk_text_view_class_init (GtkTextViewClass *klass) GTK_INPUT_HINT_NONE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); + /** GtkTextView:populate-toolbar: + * + * If ::populate-toolbar is %TRUE, the #GtkTextView::populate-popup + * signal is also emitted for touch popups. + * + * Since: 3.8 + */ + g_object_class_install_property (gobject_class, + PROP_POPULATE_TOOLBAR, + g_param_spec_boolean ("populate-toolbar", + P_("Populate toolbar"), + P_("Whether to emit ::populate-popup for touch popups"), + FALSE, + GTK_PARAM_READWRITE)); + + /* GtkScrollable interface */ g_object_class_override_property (gobject_class, PROP_HADJUSTMENT, "hadjustment"); g_object_class_override_property (gobject_class, PROP_VADJUSTMENT, "vadjustment"); @@ -1122,13 +1141,17 @@ gtk_text_view_class_init (GtkTextViewClass *klass) /** * GtkTextView::populate-popup: * @text_view: The text view on which the signal is emitted - * @menu: the menu that is being populated + * @popup: the menu or toolbar that is being populated * * The ::populate-popup signal gets emitted before showing the * context menu of the text view. * * If you need to add items to the context menu, connect - * to this signal and append your menuitems to the @menu. + * to this signal and append your items to the @popup. + * + * If #GtkEntry::populate-toolbar is %TRUE, this signal will + * also be emitted to populate touch popups. In this case, + * @popup will be a toolbar instead of a menu. */ signals[POPULATE_POPUP] = g_signal_new (I_("populate-popup"), @@ -1138,7 +1161,7 @@ gtk_text_view_class_init (GtkTextViewClass *klass) NULL, NULL, _gtk_marshal_VOID__OBJECT, G_TYPE_NONE, 1, - GTK_TYPE_MENU); + GTK_TYPE_WIDGET); /** * GtkTextView::select-all: @@ -3263,6 +3286,10 @@ gtk_text_view_set_property (GObject *object, gtk_text_view_set_input_hints (text_view, g_value_get_flags (value)); break; + case PROP_POPULATE_TOOLBAR: + text_view->priv->populate_toolbar = g_value_get_boolean (value); + break; + default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); break; @@ -3367,6 +3394,10 @@ gtk_text_view_get_property (GObject *object, g_value_set_flags (value, gtk_text_view_get_input_hints (text_view)); break; + case PROP_POPULATE_TOOLBAR: + g_value_set_boolean (value, priv->populate_toolbar); + break; + default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); break; @@ -8800,7 +8831,7 @@ bubble_targets_received (GtkClipboard *clipboard, GtkTextIter iter; GtkTextIter sel_start, sel_end; GdkWindow *window; - GtkToolbar *toolbar; + GtkWidget *toolbar; has_selection = gtk_text_buffer_get_selection_bounds (get_buffer (text_view), &sel_start, &sel_end); @@ -8815,7 +8846,7 @@ bubble_targets_received (GtkClipboard *clipboard, window = gtk_widget_get_window (GTK_WIDGET (text_view)); priv->selection_bubble = gtk_bubble_window_new (); - toolbar = gtk_toolbar_new (); + toolbar = GTK_WIDGET (gtk_toolbar_new ()); gtk_toolbar_set_style (GTK_TOOLBAR (toolbar), GTK_TOOLBAR_TEXT); gtk_toolbar_set_show_arrow (GTK_TOOLBAR (toolbar), FALSE); gtk_widget_show (toolbar); @@ -8835,6 +8866,9 @@ bubble_targets_received (GtkClipboard *clipboard, append_bubble_action (text_view, toolbar, GTK_STOCK_PASTE, "paste-clipboard", can_insert && has_clipboard); + if (priv->populate_toolbar) + g_signal_emit (text_view, signals[POPULATE_POPUP], 0, toolbar); + gtk_text_view_get_selection_rect (text_view, &rect); rect.x -= priv->xoffset; rect.y -= priv->yoffset; diff --git a/gtk/gtktextview.h b/gtk/gtktextview.h index 2b81b4f6c5..650e6051c3 100644 --- a/gtk/gtktextview.h +++ b/gtk/gtktextview.h @@ -78,7 +78,7 @@ struct _GtkTextViewClass GtkContainerClass parent_class; void (* populate_popup) (GtkTextView *text_view, - GtkMenu *menu); + GtkWidget *popup); /* These are all RUN_ACTION signals for keybindings */