diff options
author | Matthias Clasen <mclasen@redhat.com> | 2019-04-11 14:46:55 -0400 |
---|---|---|
committer | Matthias Clasen <mclasen@redhat.com> | 2019-06-13 11:59:50 +0000 |
commit | 1b21d6ecba4aac1f787e18513dc54f75253f9c28 (patch) | |
tree | ea22c3b362c35c9b5af37a83ce0422cd86deeed8 /gtk/gtkpasswordentry.c | |
parent | 6030465cdf6967065eb7631aa953a49af430dd10 (diff) | |
download | gtk+-1b21d6ecba4aac1f787e18513dc54f75253f9c28.tar.gz |
password entry: Adapt to new context menu api
Diffstat (limited to 'gtk/gtkpasswordentry.c')
-rw-r--r-- | gtk/gtkpasswordentry.c | 112 |
1 files changed, 88 insertions, 24 deletions
diff --git a/gtk/gtkpasswordentry.c b/gtk/gtkpasswordentry.c index ddf31467ba..ef0651600c 100644 --- a/gtk/gtkpasswordentry.c +++ b/gtk/gtkpasswordentry.c @@ -58,6 +58,7 @@ typedef struct { GtkWidget *icon; GtkWidget *peek_icon; GdkKeymap *keymap; + GMenuModel *extra_menu; } GtkPasswordEntryPrivate; struct _GtkPasswordEntryClass @@ -69,6 +70,7 @@ enum { PROP_PLACEHOLDER_TEXT = 1, PROP_ACTIVATES_DEFAULT, PROP_SHOW_PEEK_ICON, + PROP_EXTRA_MENU, NUM_PROPERTIES }; @@ -105,7 +107,7 @@ focus_changed (GtkWidget *widget) if (priv->keymap) keymap_state_changed (priv->keymap, widget); } - + static void gtk_password_entry_toggle_peek (GtkPasswordEntry *entry) { @@ -126,27 +128,6 @@ gtk_password_entry_toggle_peek (GtkPasswordEntry *entry) } static void -populate_popup (GtkText *text, - GtkWidget *popup, - GtkPasswordEntry *entry) -{ - GtkPasswordEntryPrivate *priv = gtk_password_entry_get_instance_private (entry); - - if (priv->peek_icon != NULL) - { - GtkWidget *item; - - item = gtk_check_menu_item_new_with_mnemonic (_("_Show text")); - gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (item), - gtk_text_get_visibility (text)); - g_signal_connect_swapped (item, "activate", - G_CALLBACK (gtk_password_entry_toggle_peek), entry); - gtk_widget_show (item); - gtk_menu_shell_append (GTK_MENU_SHELL (popup), item); - } -} - -static void gtk_password_entry_init (GtkPasswordEntry *entry) { GtkPasswordEntryPrivate *priv = gtk_password_entry_get_instance_private (entry); @@ -156,7 +137,6 @@ gtk_password_entry_init (GtkPasswordEntry *entry) gtk_widget_set_parent (priv->entry, GTK_WIDGET (entry)); gtk_editable_init_delegate (GTK_EDITABLE (entry)); g_signal_connect_swapped (priv->entry, "notify::has-focus", G_CALLBACK (focus_changed), entry); - g_signal_connect (priv->entry, "populate-popup", G_CALLBACK (populate_popup), entry); priv->icon = gtk_image_new_from_icon_name ("caps-lock-symbolic"); gtk_widget_set_tooltip_text (priv->icon, _("Caps Lock is on")); @@ -165,6 +145,8 @@ gtk_password_entry_init (GtkPasswordEntry *entry) gtk_widget_set_parent (priv->icon, GTK_WIDGET (entry)); gtk_style_context_add_class (gtk_widget_get_style_context (GTK_WIDGET (entry)), I_("password")); + + gtk_password_entry_set_extra_menu (entry, NULL); } static void @@ -195,6 +177,7 @@ gtk_password_entry_dispose (GObject *object) g_clear_pointer (&priv->entry, gtk_widget_unparent); g_clear_pointer (&priv->icon, gtk_widget_unparent); g_clear_pointer (&priv->peek_icon, gtk_widget_unparent); + g_clear_object (&priv->extra_menu); G_OBJECT_CLASS (gtk_password_entry_parent_class)->dispose (object); } @@ -235,6 +218,10 @@ gtk_password_entry_set_property (GObject *object, gtk_password_entry_set_show_peek_icon (entry, g_value_get_boolean (value)); break; + case PROP_EXTRA_MENU: + gtk_password_entry_set_extra_menu (entry, g_value_get_object (value)); + break; + default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); break; @@ -267,6 +254,10 @@ gtk_password_entry_get_property (GObject *object, g_value_set_boolean (value, gtk_password_entry_get_show_peek_icon (entry)); break; + case PROP_EXTRA_MENU: + g_value_set_object (value, priv->extra_menu); + break; + default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); break; @@ -389,7 +380,6 @@ gtk_password_entry_class_init (GtkPasswordEntryClass *klass) widget_class->get_accessible = gtk_password_entry_get_accessible; widget_class->grab_focus = gtk_password_entry_grab_focus; widget_class->mnemonic_activate = gtk_password_entry_mnemonic_activate; - props[PROP_PLACEHOLDER_TEXT] = g_param_spec_string ("placeholder-text", P_("Placeholder text"), @@ -411,6 +401,19 @@ gtk_password_entry_class_init (GtkPasswordEntryClass *klass) FALSE, GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY); + /** + * GtkPasswordEntry:extra-menu: + * + * A menu model whose contents will be appended to + * the context menu. + */ + props[PROP_EXTRA_MENU] = + g_param_spec_object ("extra-menu", + P_("Extra menu"), + P_("Model menu to append to the context menu"), + G_TYPE_MENU_MODEL, + GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY); + g_object_class_install_properties (object_class, NUM_PROPERTIES, props); gtk_editable_install_properties (object_class, NUM_PROPERTIES); @@ -509,3 +512,64 @@ gtk_password_entry_get_show_peek_icon (GtkPasswordEntry *entry) return priv->peek_icon != NULL; } + +/** + * gtk_password_entry_set_extra_menu: + * @entry: a #GtkPasswordEntry + * @model: (allow-none): a #GMenuModel + * + * Sets a menu model to add when constructing + * the context menu for @entry. + */ +void +gtk_password_entry_set_extra_menu (GtkPasswordEntry *entry, + GMenuModel *model) +{ + GtkPasswordEntryPrivate *priv = gtk_password_entry_get_instance_private (entry); + GMenu *menu; + GMenu *section; + GMenuItem *item; + + g_return_if_fail (GTK_IS_PASSWORD_ENTRY (entry)); + + if (!g_set_object (&priv->extra_menu, model)) + return; + + menu = g_menu_new (); + + section = g_menu_new (); + item = g_menu_item_new (_("_Show Text"), "context.toggle-visibility"); + g_menu_item_set_attribute (item, "touch-icon", "s", "eye-not-looking-symbolic"); + g_menu_append_item (section, item); + g_object_unref (item); + + g_menu_append_section (menu, NULL, G_MENU_MODEL (section)); + g_object_unref (section); + + if (model) + g_menu_append_section (menu, NULL, model); + + gtk_text_set_extra_menu (GTK_TEXT (priv->entry), G_MENU_MODEL (menu)); + + g_object_unref (menu); + + g_object_notify_by_pspec (G_OBJECT (entry), props[PROP_EXTRA_MENU]); +} + +/** + * gtk_password_entry_get_extra_menu: + * @self: a #GtkText + * + * Gets the menu model set with gtk_password_entry_set_extra_menu(). + * + * Returns: (transfer none): (nullable): the menu model + */ +GMenuModel * +gtk_password_entry_get_extra_menu (GtkPasswordEntry *entry) +{ + GtkPasswordEntryPrivate *priv = gtk_password_entry_get_instance_private (entry); + + g_return_val_if_fail (GTK_IS_PASSWORD_ENTRY (entry), NULL); + + return priv->extra_menu; +} |