Add GtkPasswordEntry

This is a simple editable which hides the entered
text and shows a caps-lock warning.
This commit is contained in:
Matthias Clasen 2019-02-16 22:21:57 -05:00
parent 39d5f9e07b
commit c6e6fb19e7
7 changed files with 346 additions and 1 deletions

View File

@ -143,6 +143,7 @@
<xi:include href="xml/gtktext.xml" /> <xi:include href="xml/gtktext.xml" />
<xi:include href="xml/gtkentry.xml" /> <xi:include href="xml/gtkentry.xml" />
<xi:include href="xml/gtkentrycompletion.xml" /> <xi:include href="xml/gtkentrycompletion.xml" />
<xi:include href="xml/gtkpasswordentry.xml" />
<xi:include href="xml/gtkscale.xml" /> <xi:include href="xml/gtkscale.xml" />
<xi:include href="xml/gtkspinbutton.xml" /> <xi:include href="xml/gtkspinbutton.xml" />
<xi:include href="xml/gtksearchentry.xml" /> <xi:include href="xml/gtksearchentry.xml" />

View File

@ -996,6 +996,15 @@ GtkEntryPrivate
gtk_entry_get_type gtk_entry_get_type
</SECTION> </SECTION>
<SECTION>
<FILE>gtkpasswordentry</FILE>
<TITLE>GtkPasswordEntry</TITLE>
GtkPasswordEntry
gtk_password_entry_new
<SUBSECTION Private>
gtk_password_entry_get_type
</SECTION>
<SECTION> <SECTION>
<FILE>gtkentrybuffer</FILE> <FILE>gtkentrybuffer</FILE>
<TITLE>GtkEntryBuffer</TITLE> <TITLE>GtkEntryBuffer</TITLE>

View File

@ -118,6 +118,7 @@ gtk_page_setup_get_type
@DISABLE_ON_W32@gtk_page_setup_unix_dialog_get_type @DISABLE_ON_W32@gtk_page_setup_unix_dialog_get_type
gtk_paned_get_type gtk_paned_get_type
gtk_paper_size_get_type gtk_paper_size_get_type
gtk_password_entry_get_type
gtk_picture_get_type gtk_picture_get_type
gtk_popover_get_type gtk_popover_get_type
gtk_popover_menu_get_type gtk_popover_menu_get_type

View File

@ -159,8 +159,9 @@
#include <gtk/gtkoverlay.h> #include <gtk/gtkoverlay.h>
#include <gtk/gtkpadcontroller.h> #include <gtk/gtkpadcontroller.h>
#include <gtk/gtkpagesetup.h> #include <gtk/gtkpagesetup.h>
#include <gtk/gtkpapersize.h>
#include <gtk/gtkpaned.h> #include <gtk/gtkpaned.h>
#include <gtk/gtkpapersize.h>
#include <gtk/gtkpasswordentry.h>
#include <gtk/gtkpicture.h> #include <gtk/gtkpicture.h>
#include <gtk/gtkpopover.h> #include <gtk/gtkpopover.h>
#include <gtk/gtkpopovermenu.h> #include <gtk/gtkpopovermenu.h>

271
gtk/gtkpasswordentry.c Normal file
View File

@ -0,0 +1,271 @@
/* GTK - The GIMP Toolkit
* Copyright (C) 2019 Red Hat, Inc.
*
* Authors:
* - Matthias Clasen <mclasen@redhat.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include "gtkpasswordentry.h"
#include "gtkaccessible.h"
#include "gtkbindings.h"
#include "gtktextprivate.h"
#include "gtkeditable.h"
#include "gtkbox.h"
#include "gtkimage.h"
#include "gtkintl.h"
#include "gtkmarshalers.h"
#include "gtkstylecontext.h"
#include "gtkeventcontrollerkey.h"
#include "a11y/gtkentryaccessible.h"
/**
* SECTION:gtkpasswordhentry
* @Short_description: An entry for secrets
* @Title: GtkPasswordEntry
*
* #GtkPasswordEntry is entry that has been tailored for
* entering secrets.
*/
typedef struct {
GtkWidget *box;
GtkWidget *entry;
GtkWidget *icon;
GdkKeymap *keymap;
} GtkPasswordEntryPrivate;
static void gtk_password_entry_editable_init (GtkEditableInterface *iface);
G_DEFINE_TYPE_WITH_CODE (GtkPasswordEntry, gtk_password_entry, GTK_TYPE_WIDGET,
G_ADD_PRIVATE (GtkPasswordEntry)
G_IMPLEMENT_INTERFACE (GTK_TYPE_EDITABLE, gtk_password_entry_editable_init))
static void
keymap_state_changed (GdkKeymap *keymap,
GtkWidget *widget)
{
GtkPasswordEntry *entry = GTK_PASSWORD_ENTRY (widget);
GtkPasswordEntryPrivate *priv = gtk_password_entry_get_instance_private (entry);
if (gtk_editable_get_editable (GTK_EDITABLE (entry)) &&
gtk_widget_has_focus (priv->entry) &&
gdk_keymap_get_caps_lock_state (priv->keymap))
gtk_widget_show (priv->icon);
else
gtk_widget_hide (priv->icon);
}
static void
focus_changed (GtkWidget *widget)
{
GtkPasswordEntry *entry = GTK_PASSWORD_ENTRY (widget);
GtkPasswordEntryPrivate *priv = gtk_password_entry_get_instance_private (entry);
if (priv->keymap)
keymap_state_changed (priv->keymap, widget);
}
static void
gtk_password_entry_init (GtkPasswordEntry *entry)
{
GtkPasswordEntryPrivate *priv = gtk_password_entry_get_instance_private (entry);
gtk_widget_set_has_surface (GTK_WIDGET (entry), FALSE);
priv->box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
gtk_widget_set_parent (priv->box, GTK_WIDGET (entry));
priv->entry = gtk_text_new ();
gtk_text_set_visibility (GTK_TEXT (priv->entry), FALSE);
gtk_widget_set_hexpand (priv->entry, TRUE);
gtk_widget_set_vexpand (priv->entry, TRUE);
gtk_container_add (GTK_CONTAINER (priv->box), priv->entry);
gtk_editable_init_delegate (GTK_EDITABLE (entry));
g_signal_connect_swapped (priv->entry, "notify::has-focus", G_CALLBACK (focus_changed), entry);
priv->icon = gtk_image_new_from_icon_name ("dialog-warning-symbolic");
gtk_widget_set_tooltip_text (priv->icon, _("Caps Lock is on"));
gtk_container_add (GTK_CONTAINER (priv->box), priv->icon);
gtk_style_context_add_class (gtk_widget_get_style_context (GTK_WIDGET (entry)), I_("password"));
}
static void
gtk_password_entry_realize (GtkWidget *widget)
{
GtkPasswordEntry *entry = GTK_PASSWORD_ENTRY (widget);
GtkPasswordEntryPrivate *priv = gtk_password_entry_get_instance_private (entry);
GTK_WIDGET_CLASS (gtk_password_entry_parent_class)->realize (widget);
priv->keymap = gdk_display_get_keymap (gtk_widget_get_display (widget));
g_signal_connect (priv->keymap, "state-changed", G_CALLBACK (keymap_state_changed), entry);
}
static void
gtk_password_entry_dispose (GObject *object)
{
GtkPasswordEntry *entry = GTK_PASSWORD_ENTRY (object);
GtkPasswordEntryPrivate *priv = gtk_password_entry_get_instance_private (entry);
if (priv->keymap)
g_signal_handlers_disconnect_by_func (priv->keymap, keymap_state_changed, entry);
if (priv->entry)
gtk_editable_finish_delegate (GTK_EDITABLE (entry));
g_clear_pointer (&priv->entry, gtk_widget_unparent);
g_clear_pointer (&priv->icon, gtk_widget_unparent);
g_clear_pointer (&priv->box, gtk_widget_unparent);
G_OBJECT_CLASS (gtk_password_entry_parent_class)->dispose (object);
}
static void
gtk_password_entry_finalize (GObject *object)
{
G_OBJECT_CLASS (gtk_password_entry_parent_class)->finalize (object);
}
static void
gtk_password_entry_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
if (gtk_editable_delegate_set_property (object, prop_id, value, pspec))
return;
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
static void
gtk_password_entry_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
if (gtk_editable_delegate_get_property (object, prop_id, value, pspec))
return;
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
static void
gtk_password_entry_measure (GtkWidget *widget,
GtkOrientation orientation,
int for_size,
int *minimum,
int *natural,
int *minimum_baseline,
int *natural_baseline)
{
GtkPasswordEntry *entry = GTK_PASSWORD_ENTRY (widget);
GtkPasswordEntryPrivate *priv = gtk_password_entry_get_instance_private (entry);
gtk_widget_measure (priv->box, orientation, for_size,
minimum, natural,
minimum_baseline, natural_baseline);
}
static void
gtk_password_entry_size_allocate (GtkWidget *widget,
int width,
int height,
int baseline)
{
GtkPasswordEntry *entry = GTK_PASSWORD_ENTRY (widget);
GtkPasswordEntryPrivate *priv = gtk_password_entry_get_instance_private (entry);
gtk_widget_size_allocate (priv->box,
&(GtkAllocation) { 0, 0, width, height },
baseline);
}
static AtkObject *
gtk_password_entry_get_accessible (GtkWidget *widget)
{
AtkObject *atk_obj;
atk_obj = GTK_WIDGET_CLASS (gtk_password_entry_parent_class)->get_accessible (widget);
atk_object_set_name (atk_obj, _("Password"));
return atk_obj;
}
static void
gtk_password_entry_grab_focus (GtkWidget *widget)
{
GtkPasswordEntry *entry = GTK_PASSWORD_ENTRY (widget);
GtkPasswordEntryPrivate *priv = gtk_password_entry_get_instance_private (entry);
gtk_widget_grab_focus (priv->entry);
}
static void
gtk_password_entry_class_init (GtkPasswordEntryClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
object_class->dispose = gtk_password_entry_dispose;
object_class->finalize = gtk_password_entry_finalize;
object_class->get_property = gtk_password_entry_get_property;
object_class->set_property = gtk_password_entry_set_property;
widget_class->realize = gtk_password_entry_realize;
widget_class->measure = gtk_password_entry_measure;
widget_class->size_allocate = gtk_password_entry_size_allocate;
widget_class->get_accessible = gtk_password_entry_get_accessible;
widget_class->grab_focus = gtk_password_entry_grab_focus;
gtk_editable_install_properties (object_class, 1);
gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_ENTRY_ACCESSIBLE);
gtk_widget_class_set_css_name (widget_class, I_("entry"));
}
static GtkEditable *
gtk_password_entry_get_delegate (GtkEditable *editable)
{
GtkPasswordEntry *entry = GTK_PASSWORD_ENTRY (editable);
GtkPasswordEntryPrivate *priv = gtk_password_entry_get_instance_private (entry);
return GTK_EDITABLE (priv->entry);
}
static void
gtk_password_entry_editable_init (GtkEditableInterface *iface)
{
iface->get_delegate = gtk_password_entry_get_delegate;
}
/**
* gtk_password_entry_new:
*
* Creates a #GtkPasswordEntry.
*
* Returns: a new #GtkPasswordEntry
*/
GtkWidget *
gtk_password_entry_new (void)
{
return GTK_WIDGET (g_object_new (GTK_TYPE_PASSWORD_ENTRY, NULL));
}

60
gtk/gtkpasswordentry.h Normal file
View File

@ -0,0 +1,60 @@
/* GTK - The GIMP Toolkit
* Copyright (C) 2019 Red Hat, Inc.
*
* Authors:
* - MAtthias Clasen <mclasen@redhat.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __GTK_PASSWORD_ENTRY_H__
#define __GTK_PASSWORD_ENTRY_H__
#if !defined (__GTK_H_INSIDE__) && !defined (GTK_COMPILATION)
#error "Only <gtk/gtk.h> can be included directly."
#endif
#include <gtk/gtkentry.h>
G_BEGIN_DECLS
#define GTK_TYPE_PASSWORD_ENTRY (gtk_password_entry_get_type ())
#define GTK_PASSWORD_ENTRY(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_PASSWORD_ENTRY, GtkPasswordEntry))
#define GTK_PASSWORD_ENTRY_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_PASSWORD_ENTRY, GtkPasswordEntryClass))
#define GTK_IS_PASSWORD_ENTRY(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_PASSWORD_ENTRY))
#define GTK_IS_PASSWORD_ENTRY_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_PASSWORD_ENTRY))
#define GTK_PASSWORD_ENTRY_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_PASSWORD_ENTRY, GtkPasswordEntryClass))
typedef struct _GtkPasswordEntry GtkPasswordEntry;
typedef struct _GtkPasswordEntryClass GtkPasswordEntryClass;
struct _GtkPasswordEntry
{
GtkWidget parent;
};
struct _GtkPasswordEntryClass
{
GtkWidgetClass parent_class;
};
GDK_AVAILABLE_IN_ALL
GType gtk_password_entry_get_type (void) G_GNUC_CONST;
GDK_AVAILABLE_IN_ALL
GtkWidget * gtk_password_entry_new (void);
G_END_DECLS
#endif /* __GTK_PASSWORD_ENTRY_H__ */

View File

@ -293,6 +293,7 @@ gtk_public_sources = files([
'gtkpagesetup.c', 'gtkpagesetup.c',
'gtkpaned.c', 'gtkpaned.c',
'gtkpapersize.c', 'gtkpapersize.c',
'gtkpasswordentry.c',
'gtkpicture.c', 'gtkpicture.c',
'gtkpopover.c', 'gtkpopover.c',
'gtkpopovermenu.c', 'gtkpopovermenu.c',
@ -538,6 +539,7 @@ gtk_public_headers = files([
'gtkpagesetup.h', 'gtkpagesetup.h',
'gtkpaned.h', 'gtkpaned.h',
'gtkpapersize.h', 'gtkpapersize.h',
'gtkpasswordentry.h',
'gtkpicture.h', 'gtkpicture.h',
'gtkpopover.h', 'gtkpopover.h',
'gtkpopovermenu.h', 'gtkpopovermenu.h',