GtkEntryBuffer holds text for GtkEntry

Adds a 'model' type buffer for GtkEntry in which the actual
textual data is stored. GtkEntryBuffer can be subclassed.

Among other things, this allows GtkEntry to be used for secrets
that need to be stored in non-pageable memory. It also allows
buffers to be shared by entries.

See bug #576801.
This commit is contained in:
Stef Walter 2009-07-08 20:41:53 -05:00
parent 9ef87ea11e
commit eab02f697a
15 changed files with 1764 additions and 447 deletions

View File

@ -17,6 +17,7 @@ demos = \
dialog.c \
drawingarea.c \
editable_cells.c \
entry_buffer.c \
entry_completion.c \
expander.c \
hypertext.c \

View File

@ -0,0 +1,65 @@
/* Entry/Entry Buffer
*
* GtkEntryBuffer provides the text content in a GtkEntry.
*
*/
#include <gtk/gtk.h>
static GtkWidget *window = NULL;
GtkWidget *
do_entry_buffer (GtkWidget *do_widget)
{
GtkWidget *vbox;
GtkWidget *label;
GtkWidget *entry;
GtkEntryBuffer *buffer;
if (!window)
{
window = gtk_dialog_new_with_buttons ("GtkEntryBuffer",
GTK_WINDOW (do_widget),
0,
GTK_STOCK_CLOSE,
GTK_RESPONSE_NONE,
NULL);
gtk_window_set_resizable (GTK_WINDOW (window), FALSE);
g_signal_connect (window, "response",
G_CALLBACK (gtk_widget_destroy), NULL);
g_signal_connect (window, "destroy",
G_CALLBACK (gtk_widget_destroyed), &window);
vbox = gtk_vbox_new (FALSE, 5);
gtk_box_pack_start (GTK_BOX (GTK_DIALOG (window)->vbox), vbox, TRUE, TRUE, 0);
gtk_container_set_border_width (GTK_CONTAINER (vbox), 5);
label = gtk_label_new (NULL);
gtk_label_set_markup (GTK_LABEL (label), "Entries share a buffer. Typing in one is reflected in the other.");
gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
/* Create a buffer */
buffer = gtk_entry_buffer_new (NULL, 0);
/* Create our first entry */
entry = gtk_entry_new_with_buffer (buffer);
gtk_box_pack_start (GTK_BOX (vbox), entry, FALSE, FALSE, 0);
/* Create the second entry */
entry = gtk_entry_new_with_buffer (buffer);
gtk_entry_set_visibility (GTK_ENTRY (entry), FALSE);
gtk_box_pack_start (GTK_BOX (vbox), entry, FALSE, FALSE, 0);
g_object_unref (buffer);
}
if (!GTK_WIDGET_VISIBLE (window))
gtk_widget_show_all (window);
else
gtk_widget_destroy (window);
return window;
}

View File

@ -177,6 +177,7 @@ that is, GUI components such as #GtkButton or #GtkTextView.
<chapter id="NumericEntry">
<title>Numeric/Text Data Entry</title>
<xi:include href="xml/gtkentry.xml" />
<xi:include href="xml/gtkentrybuffer.xml" />
<xi:include href="xml/gtkentrycompletion.xml" />
<xi:include href="xml/gtkhscale.xml" />
<xi:include href="xml/gtkvscale.xml" />

View File

@ -1268,7 +1268,10 @@ gtk_old_editable_get_type
<TITLE>GtkEntry</TITLE>
GtkEntry
gtk_entry_new
gtk_entry_new_with_buffer
gtk_entry_new_with_max_length
gtk_entry_get_buffer
gtk_entry_set_buffer
gtk_entry_set_text
gtk_entry_append_text
gtk_entry_prepend_text
@ -1342,6 +1345,34 @@ GTK_ENTRY_GET_CLASS
gtk_entry_get_type
</SECTION>
<SECTION>
<FILE>gtkentrybuffer</FILE>
<TITLE>GtkEntryBuffer</TITLE>
GtkEntryBuffer
gtk_entry_buffer_new
gtk_entry_buffer_get_text
gtk_entry_buffer_set_text
gtk_entry_buffer_get_bytes
gtk_entry_buffer_get_length
gtk_entry_buffer_get_max_length
gtk_entry_buffer_set_max_length
gtk_entry_buffer_insert_text
gtk_entry_buffer_delete_text
gtk_entry_buffer_emit_deleted_text
gtk_entry_buffer_emit_inserted_text
<SUBSECTION Standard>
GTK_ENTRY_BUFFER
GTK_IS_ENTRY_BUFFER
GTK_TYPE_ENTRY_BUFFER
GTK_ENTRY_BUFFER_CLASS
GTK_IS_ENTRY_BUFFER_CLASS
GTK_ENTRY_BUFFER_GET_CLASS
GTK_ENTRY_BUFFER_MAX_SIZE
<SUBSECTION Private>
gtk_entry_buffer_get_type
</SECTION>
<SECTION>
<FILE>gtkentrycompletion</FILE>
<TITLE>GtkEntryCompletion</TITLE>

View File

@ -48,6 +48,7 @@ gtk_curve_get_type
gtk_dialog_get_type
gtk_drawing_area_get_type
gtk_editable_get_type
gtk_entry_buffer_get_type
gtk_entry_completion_get_type
gtk_entry_get_type
gtk_event_box_get_type

View File

@ -1033,6 +1033,25 @@ You may not attach these to signals created with the
</para>
<!-- ##### STRUCT GtkEntryBufferClass ##### -->
<para>
</para>
@parent_class:
@inserted_text:
@deleted_text:
@get_text:
@get_length:
@insert_text:
@delete_text:
@_gtk_reserved0:
@_gtk_reserved1:
@_gtk_reserved2:
@_gtk_reserved3:
@_gtk_reserved4:
@_gtk_reserved5:
<!-- ##### ARG GtkFileChooser:file-system ##### -->
<para>

View File

@ -173,6 +173,11 @@ The #GtkEntry-struct struct contains only private data.
</para>
<!-- ##### ARG GtkEntry:buffer ##### -->
<para>
</para>
<!-- ##### ARG GtkEntry:caps-lock-warning ##### -->
<para>
@ -401,6 +406,15 @@ The #GtkEntry-struct struct contains only private data.
@Returns:
<!-- ##### FUNCTION gtk_entry_new_with_buffer ##### -->
<para>
</para>
@buffer:
@Returns:
<!-- ##### FUNCTION gtk_entry_new_with_max_length ##### -->
<para>
</para>
@ -409,6 +423,24 @@ The #GtkEntry-struct struct contains only private data.
@Returns:
<!-- ##### FUNCTION gtk_entry_get_buffer ##### -->
<para>
</para>
@entry:
@Returns:
<!-- ##### FUNCTION gtk_entry_set_buffer ##### -->
<para>
</para>
@entry:
@buffer:
<!-- ##### FUNCTION gtk_entry_set_text ##### -->
<para>

View File

@ -0,0 +1,168 @@
<!-- ##### SECTION Title ##### -->
GtkEntryBuffer
<!-- ##### SECTION Short_Description ##### -->
<!-- ##### SECTION Long_Description ##### -->
<para>
</para>
<!-- ##### SECTION See_Also ##### -->
<para>
</para>
<!-- ##### SECTION Stability_Level ##### -->
<!-- ##### STRUCT GtkEntryBuffer ##### -->
<para>
</para>
<!-- ##### SIGNAL GtkEntryBuffer::deleted-text ##### -->
<para>
</para>
@entrybuffer: the object which received the signal.
@arg1:
@arg2:
<!-- ##### SIGNAL GtkEntryBuffer::inserted-text ##### -->
<para>
</para>
@entrybuffer: the object which received the signal.
@arg1:
@arg2:
@arg3:
<!-- ##### ARG GtkEntryBuffer:length ##### -->
<para>
</para>
<!-- ##### ARG GtkEntryBuffer:max-length ##### -->
<para>
</para>
<!-- ##### ARG GtkEntryBuffer:text ##### -->
<para>
</para>
<!-- ##### FUNCTION gtk_entry_buffer_new ##### -->
<para>
</para>
@initial_chars:
@n_initial_chars:
@Returns:
<!-- ##### FUNCTION gtk_entry_buffer_get_text ##### -->
<para>
</para>
@buffer:
@Returns:
<!-- ##### FUNCTION gtk_entry_buffer_set_text ##### -->
<para>
</para>
@buffer:
@chars:
@n_chars:
<!-- ##### FUNCTION gtk_entry_buffer_get_bytes ##### -->
<para>
</para>
@buffer:
@Returns:
<!-- ##### FUNCTION gtk_entry_buffer_get_length ##### -->
<para>
</para>
@buffer:
@Returns:
<!-- ##### FUNCTION gtk_entry_buffer_get_max_length ##### -->
<para>
</para>
@buffer:
@Returns:
<!-- ##### FUNCTION gtk_entry_buffer_set_max_length ##### -->
<para>
</para>
@buffer:
@max_length:
<!-- ##### FUNCTION gtk_entry_buffer_insert_text ##### -->
<para>
</para>
@buffer:
@position:
@chars:
@n_chars:
@Returns:
<!-- ##### FUNCTION gtk_entry_buffer_delete_text ##### -->
<para>
</para>
@buffer:
@position:
@n_chars:
@Returns:
<!-- ##### FUNCTION gtk_entry_buffer_emit_deleted_text ##### -->
<para>
</para>
@buffer:
@position:
@n_chars:
<!-- ##### FUNCTION gtk_entry_buffer_emit_inserted_text ##### -->
<para>
</para>
@buffer:
@position:
@chars:
@n_chars:

View File

@ -197,6 +197,7 @@ gtk_public_h_sources = \
gtkdrawingarea.h \
gtkeditable.h \
gtkentry.h \
gtkentrybuffer.h \
gtkentrycompletion.h \
gtkenums.h \
gtkeventbox.h \
@ -447,6 +448,7 @@ gtk_base_c_sources = \
gtkdrawingarea.c \
gtkeditable.c \
gtkentry.c \
gtkentrybuffer.c \
gtkentrycompletion.c \
gtkeventbox.c \
gtkexpander.c \

View File

@ -78,6 +78,7 @@
#include <gtk/gtkdrawingarea.h>
#include <gtk/gtkeditable.h>
#include <gtk/gtkentry.h>
#include <gtk/gtkentrybuffer.h>
#include <gtk/gtkentrycompletion.h>
#include <gtk/gtkenums.h>
#include <gtk/gtkeventbox.h>

File diff suppressed because it is too large Load Diff

View File

@ -39,6 +39,7 @@
#include <gtk/gtkeditable.h>
#include <gtk/gtkimcontext.h>
#include <gtk/gtkmenu.h>
#include <gtk/gtkentrybuffer.h>
#include <gtk/gtkentrycompletion.h>
#include <gtk/gtkimage.h>
#include <gtk/gtkselection.h>
@ -66,7 +67,7 @@ struct _GtkEntry
{
GtkWidget widget;
gchar *GSEAL (text);
gchar *GSEAL (text); /* COMPAT: Deprecated, not used. Remove in GTK+ 3.x */
guint GSEAL (editable) : 1;
guint GSEAL (visible) : 1;
@ -74,8 +75,8 @@ struct _GtkEntry
guint GSEAL (in_drag) : 1; /* FIXME: Should be private?
Dragging within the selection */
guint16 GSEAL (text_length); /* length in use, in chars */
guint16 GSEAL (text_max_length);
guint16 GSEAL (text_length); /* COMPAT: Deprecated, not used. Remove in GTK+ 3.x */
guint16 GSEAL (text_max_length); /* COMPAT: Deprecated, not used. Remove in GTK+ 3.x */
/*< private >*/
GdkWindow *GSEAL (text_area);
@ -108,8 +109,8 @@ struct _GtkEntry
gint GSEAL (ascent); /* font ascent in pango units */
gint GSEAL (descent); /* font descent in pango units */
guint16 GSEAL (text_size); /* allocated size, in bytes */
guint16 GSEAL (n_bytes); /* length in use, in bytes */
guint16 GSEAL (x_text_size); /* allocated size, in bytes */
guint16 GSEAL (x_n_bytes); /* length in use, in bytes */
guint16 GSEAL (preedit_length); /* length of preedit string, in bytes */
guint16 GSEAL (preedit_cursor); /* offset of cursor within preedit string, in chars */
@ -164,6 +165,10 @@ struct _GtkEntryClass
GType gtk_entry_get_type (void) G_GNUC_CONST;
GtkWidget* gtk_entry_new (void);
GtkWidget* gtk_entry_new_with_buffer (GtkEntryBuffer *buffer);
GtkEntryBuffer* gtk_entry_get_buffer (GtkEntry *entry);
void gtk_entry_set_buffer (GtkEntry *entry,
GtkEntryBuffer *buffer);
void gtk_entry_set_visibility (GtkEntry *entry,
gboolean visible);
gboolean gtk_entry_get_visibility (GtkEntry *entry);

741
gtk/gtkentrybuffer.c Normal file
View File

@ -0,0 +1,741 @@
/* gtkentrybuffer.c
* Copyright (C) 2009 Stefan Walter <stef@memberwebs.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include "config.h"
#include "gtkentrybuffer.h"
#include "gtkintl.h"
#include "gtkmarshalers.h"
#include "gtkprivate.h"
#include "gtkwidget.h"
#include <gdk/gdk.h>
#include <string.h>
/**
* SECTION:gtkentrybuffer
* @title: GtkEntryBuffer
* @short_description: Text buffer for GtkEntry
*
* The #GtkEntryBuffer class contains the actual text displayed in a
* #GtkEntry widget.
*
* A single #GtkEntryBuffer object can be shared by multiple #GtkEntry
* widgets which will then share the same text content, but not the cursor
* position, visibility attributes, icon etc.
*
* #GtkEntryBuffer may be derived from. Such a derived class might allow
* text to be stored in an alternate location, such as non-pageable memory,
* useful in the case of important passwords. Or a derived class could
* integrate with an application's concept of undo/redo.
*
* Since: 2.18
*/
/* Initial size of buffer, in bytes */
#define MIN_SIZE 16
enum {
PROP_0,
PROP_TEXT,
PROP_LENGTH,
PROP_MAX_LENGTH,
};
enum {
INSERTED_TEXT,
DELETED_TEXT,
LAST_SIGNAL
};
static guint signals[LAST_SIGNAL] = { 0 };
struct _GtkEntryBufferPrivate
{
guint max_length;
/* Only valid if this class is not derived */
gchar *normal_text;
gsize normal_text_size;
gsize normal_text_bytes;
guint normal_text_chars;
};
G_DEFINE_TYPE (GtkEntryBuffer, gtk_entry_buffer, G_TYPE_OBJECT);
/* --------------------------------------------------------------------------------
* DEFAULT IMPLEMENTATIONS OF TEXT BUFFER
*
* These may be overridden by a derived class, behavior may be changed etc...
* The normal_text and normal_text_xxxx fields may not be valid when
* this class is derived from.
*/
/* Overwrite a memory that might contain sensitive information. */
static void
trash_area (gchar *area,
gsize len)
{
volatile gchar *varea = (volatile gchar *)area;
while (len-- > 0)
*varea++ = 0;
}
static const gchar*
gtk_entry_buffer_normal_get_text (GtkEntryBuffer *buffer,
gsize *n_bytes)
{
if (n_bytes)
*n_bytes = buffer->priv->normal_text_bytes;
if (!buffer->priv->normal_text)
return "";
return buffer->priv->normal_text;
}
static guint
gtk_entry_buffer_normal_get_length (GtkEntryBuffer *buffer)
{
return buffer->priv->normal_text_chars;
}
static guint
gtk_entry_buffer_normal_insert_text (GtkEntryBuffer *buffer,
guint position,
const gchar *chars,
guint n_chars)
{
GtkEntryBufferPrivate *pv = buffer->priv;
gsize prev_size;
gsize n_bytes;
gsize at;
n_bytes = g_utf8_offset_to_pointer (chars, n_chars) - chars;
/* Need more memory */
if (n_bytes + pv->normal_text_bytes + 1 > pv->normal_text_size)
{
prev_size = pv->normal_text_size;
/* Calculate our new buffer size */
while (n_bytes + pv->normal_text_bytes + 1 > pv->normal_text_size)
{
if (pv->normal_text_size == 0)
pv->normal_text_size = MIN_SIZE;
else
{
if (2 * pv->normal_text_size < GTK_ENTRY_BUFFER_MAX_SIZE)
pv->normal_text_size *= 2;
else
{
pv->normal_text_size = GTK_ENTRY_BUFFER_MAX_SIZE;
if (n_bytes > pv->normal_text_size - pv->normal_text_bytes - 1)
{
n_bytes = pv->normal_text_size - pv->normal_text_bytes - 1;
n_bytes = g_utf8_find_prev_char (chars, chars + n_bytes + 1) - chars;
n_chars = g_utf8_strlen (chars, n_bytes);
}
break;
}
}
}
/* Could be a password, so can't leave stuff in memory. */
gchar *et_new = g_malloc (pv->normal_text_size);
memcpy (et_new, pv->normal_text, MIN (prev_size, pv->normal_text_size));
trash_area (pv->normal_text, prev_size);
g_free (pv->normal_text);
pv->normal_text = et_new;
}
/* Actual text insertion */
at = g_utf8_offset_to_pointer (pv->normal_text, position) - pv->normal_text;
g_memmove (pv->normal_text + at + n_bytes, pv->normal_text + at, pv->normal_text_bytes - at);
memcpy (pv->normal_text + at, chars, n_bytes);
/* Book keeping */
pv->normal_text_bytes += n_bytes;
pv->normal_text_chars += n_chars;
pv->normal_text[pv->normal_text_bytes] = '\0';
gtk_entry_buffer_emit_inserted_text (buffer, position, chars, n_chars);
return n_chars;
}
static guint
gtk_entry_buffer_normal_delete_text (GtkEntryBuffer *buffer,
guint position,
guint n_chars)
{
GtkEntryBufferPrivate *pv = buffer->priv;
gsize start, end;
if (position > pv->normal_text_chars)
position = pv->normal_text_chars;
if (position + n_chars > pv->normal_text_chars)
n_chars = pv->normal_text_chars - position;
if (n_chars > 0)
{
start = g_utf8_offset_to_pointer (pv->normal_text, position) - pv->normal_text;
end = g_utf8_offset_to_pointer (pv->normal_text, position + n_chars) - pv->normal_text;
g_memmove (pv->normal_text + start, pv->normal_text + end, pv->normal_text_bytes + 1 - end);
pv->normal_text_chars -= n_chars;
pv->normal_text_bytes -= (end - start);
/*
* Could be a password, make sure we don't leave anything sensitive after
* the terminating zero. Note, that the terminating zero already trashed
* one byte.
*/
trash_area (pv->normal_text + pv->normal_text_bytes + 1, end - start - 1);
gtk_entry_buffer_emit_deleted_text (buffer, position, n_chars);
}
return n_chars;
}
/* --------------------------------------------------------------------------------
*
*/
static void
gtk_entry_buffer_real_inserted_text (GtkEntryBuffer *buffer,
guint position,
const gchar *chars,
guint n_chars)
{
g_object_notify (G_OBJECT (buffer), "text");
g_object_notify (G_OBJECT (buffer), "length");
}
static void
gtk_entry_buffer_real_deleted_text (GtkEntryBuffer *buffer,
guint position,
guint n_chars)
{
g_object_notify (G_OBJECT (buffer), "text");
g_object_notify (G_OBJECT (buffer), "length");
}
/* --------------------------------------------------------------------------------
*
*/
static void
gtk_entry_buffer_init (GtkEntryBuffer *buffer)
{
GtkEntryBufferPrivate *pv;
pv = buffer->priv = G_TYPE_INSTANCE_GET_PRIVATE (buffer, GTK_TYPE_ENTRY_BUFFER, GtkEntryBufferPrivate);
pv->normal_text = NULL;
pv->normal_text_chars = 0;
pv->normal_text_bytes = 0;
pv->normal_text_size = 0;
}
static void
gtk_entry_buffer_finalize (GObject *obj)
{
GtkEntryBuffer *buffer = GTK_ENTRY_BUFFER (obj);
GtkEntryBufferPrivate *pv = buffer->priv;
if (pv->normal_text)
{
trash_area (pv->normal_text, pv->normal_text_size);
g_free (pv->normal_text);
pv->normal_text = NULL;
pv->normal_text_bytes = pv->normal_text_size = 0;
pv->normal_text_chars = 0;
}
G_OBJECT_CLASS (gtk_entry_buffer_parent_class)->finalize (obj);
}
static void
gtk_entry_buffer_set_property (GObject *obj,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
GtkEntryBuffer *buffer = GTK_ENTRY_BUFFER (obj);
switch (prop_id)
{
case PROP_TEXT:
gtk_entry_buffer_set_text (buffer, g_value_get_string (value), -1);
break;
case PROP_MAX_LENGTH:
gtk_entry_buffer_set_max_length (buffer, g_value_get_uint (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, prop_id, pspec);
break;
}
}
static void
gtk_entry_buffer_get_property (GObject *obj,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
GtkEntryBuffer *buffer = GTK_ENTRY_BUFFER (obj);
switch (prop_id)
{
case PROP_TEXT:
g_value_set_string (value, gtk_entry_buffer_get_text (buffer));
break;
case PROP_LENGTH:
g_value_set_uint (value, gtk_entry_buffer_get_length (buffer));
break;
case PROP_MAX_LENGTH:
g_value_set_uint (value, gtk_entry_buffer_get_max_length (buffer));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, prop_id, pspec);
break;
}
}
static void
gtk_entry_buffer_class_init (GtkEntryBufferClass *klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
gobject_class->finalize = gtk_entry_buffer_finalize;
gobject_class->set_property = gtk_entry_buffer_set_property;
gobject_class->get_property = gtk_entry_buffer_get_property;
klass->get_text = gtk_entry_buffer_normal_get_text;
klass->get_length = gtk_entry_buffer_normal_get_length;
klass->insert_text = gtk_entry_buffer_normal_insert_text;
klass->delete_text = gtk_entry_buffer_normal_delete_text;
klass->inserted_text = gtk_entry_buffer_real_inserted_text;
klass->deleted_text = gtk_entry_buffer_real_deleted_text;
g_type_class_add_private (gobject_class, sizeof (GtkEntryBufferPrivate));
/**
* GtkEntryBuffer:text:
*
* The contents of the buffer.
*
* Since: 2.18
*/
g_object_class_install_property (gobject_class, PROP_TEXT,
g_param_spec_string ("text", P_("Text"),
P_("The contents of the buffer"),
"", GTK_PARAM_READWRITE));
/**
* GtkEntryBuffer:length:
*
* The length (in characters) of the text in buffer.
*
* Since: 2.18
*/
g_object_class_install_property (gobject_class, PROP_LENGTH,
g_param_spec_uint ("length", P_("Text length"),
P_("Length of the text currently in the buffer"),
0, GTK_ENTRY_BUFFER_MAX_SIZE, 0, GTK_PARAM_READABLE));
/**
* GtkEntryBuffer:max-length:
*
* The maximum length (in characters) of the text in the buffer.
*
* Since: 2.18
*/
g_object_class_install_property (gobject_class, PROP_MAX_LENGTH,
g_param_spec_uint ("max-length", P_("Maximum length"),
P_("Maximum number of characters for this entry. Zero if no maximum"),
0, GTK_ENTRY_BUFFER_MAX_SIZE, 0, GTK_PARAM_READWRITE));
/**
* GtkEntry::inserted-text:
* @buffer: a #GtkEntryBuffer
* @position: the position the text was inserted at.
* @chars: The text that was inserted.
* @n_chars: The number of characters that were inserted.
*
* This signal is emitted after text is inserted into the buffer.
*
* Since: 2.18
*/
signals[INSERTED_TEXT] = g_signal_new (I_("inserted-text"),
GTK_TYPE_ENTRY_BUFFER,
G_SIGNAL_RUN_FIRST,
G_STRUCT_OFFSET (GtkEntryBufferClass, inserted_text),
NULL, NULL,
_gtk_marshal_VOID__UINT_STRING_UINT,
G_TYPE_NONE, 3,
G_TYPE_UINT,
G_TYPE_STRING,
G_TYPE_UINT);
/**
* GtkEntry::deleted-text:
* @buffer: a #GtkEntryBuffer
* @position: the position the text was deleted at.
* @n_chars: The number of characters that were deleted.
*
* This signal is emitted after text is deleted from the buffer.
*
* Since: 2.18
*/
signals[DELETED_TEXT] = g_signal_new (I_("deleted-text"),
GTK_TYPE_ENTRY_BUFFER,
G_SIGNAL_RUN_FIRST,
G_STRUCT_OFFSET (GtkEntryBufferClass, deleted_text),
NULL, NULL,
_gtk_marshal_VOID__UINT_UINT,
G_TYPE_NONE, 2,
G_TYPE_UINT,
G_TYPE_UINT);
}
/* --------------------------------------------------------------------------------
*
*/
/**
* gtk_entry_buffer_new:
* @initial_chars: initial buffer text, or %NULL
* @n_initial_chars: number of characters in @initial_chars, or -1
*
* Create a new GtkEntryBuffer object.
*
* Optionally, specify initial text to set in the buffer.
*
* Return value: A new GtkEntryBuffer object.
*
* Since: 2.18
**/
GtkEntryBuffer*
gtk_entry_buffer_new (const gchar *initial_chars,
gint n_initial_chars)
{
GtkEntryBuffer *buffer = g_object_new (GTK_TYPE_ENTRY_BUFFER, NULL);
if (initial_chars)
gtk_entry_buffer_set_text (buffer, initial_chars, n_initial_chars);
return buffer;
}
/**
* gtk_entry_buffer_get_length:
* @buffer: a #GtkEntryBuffer
*
* Retrieves the length in characters of the buffer.
*
* Return value: The number of characters in the buffer.
*
* Since: 2.18
**/
guint
gtk_entry_buffer_get_length (GtkEntryBuffer *buffer)
{
GtkEntryBufferClass *klass;
g_return_val_if_fail (GTK_IS_ENTRY_BUFFER (buffer), 0);
klass = GTK_ENTRY_BUFFER_GET_CLASS (buffer);
g_return_val_if_fail (klass->get_length != NULL, 0);
return (*klass->get_length) (buffer);
}
/**
* gtk_entry_buffer_get_bytes:
* @buffer: a #GtkEntryBuffer
*
* Retrieves the length in bytes of the buffer.
* See gtk_entry_buffer_get_length().
*
* Return value: The byte length of the buffer.
*
* Since: 2.18
**/
gsize
gtk_entry_buffer_get_bytes (GtkEntryBuffer *buffer)
{
GtkEntryBufferClass *klass;
gsize bytes = 0;
g_return_val_if_fail (GTK_IS_ENTRY_BUFFER (buffer), 0);
klass = GTK_ENTRY_BUFFER_GET_CLASS (buffer);
g_return_val_if_fail (klass->get_text != NULL, 0);
(*klass->get_text) (buffer, &bytes);
return bytes;
}
/**
* gtk_entry_buffer_get_text:
* @buffer: a #GtkEntryBuffer
*
* Retrieves the contents of the buffer.
*
* The memory pointer returned by this call will not change
* unless this object emits a signal, or is finalized.
*
* Return value: a pointer to the contents of the widget as a
* string. This string points to internally allocated
* storage in the buffer and must not be freed, modified or
* stored.
*
* Since: 2.18
**/
const gchar*
gtk_entry_buffer_get_text (GtkEntryBuffer *buffer)
{
GtkEntryBufferClass *klass;
g_return_val_if_fail (GTK_IS_ENTRY_BUFFER (buffer), NULL);
klass = GTK_ENTRY_BUFFER_GET_CLASS (buffer);
g_return_val_if_fail (klass->get_text != NULL, NULL);
return (*klass->get_text) (buffer, NULL);
}
/**
* gtk_entry_buffer_set_text:
* @buffer: a #GtkEntryBuffer
* @chars: the new text
* @n_chars: the number of characters in @text, or -1
*
* Sets the text in the buffer.
*
* This is roughly equivalent to calling gtk_entry_buffer_delete_text()
* and gtk_entry_buffer_insert_text().
*
* Note that @n_chars is in characters, not in bytes.
*
* Since: 2.18
**/
void
gtk_entry_buffer_set_text (GtkEntryBuffer *buffer,
const gchar *chars,
gint n_chars)
{
g_return_if_fail (GTK_IS_ENTRY_BUFFER (buffer));
g_return_if_fail (chars != NULL);
g_object_freeze_notify (G_OBJECT (buffer));
gtk_entry_buffer_delete_text (buffer, 0, -1);
gtk_entry_buffer_insert_text (buffer, 0, chars, n_chars);
g_object_thaw_notify (G_OBJECT (buffer));
}
/**
* gtk_entry_buffer_set_max_length:
* @buffer: a #GtkEntryBuffer
* @max_length: the maximum length of the entry buffer, or 0 for no maximum.
* (other than the maximum length of entries.) The value passed in will
* be clamped to the range 0-65536.
*
* Sets the maximum allowed length of the contents of the buffer. If
* the current contents are longer than the given length, then they
* will be truncated to fit.
*
* Since: 2.18
**/
void
gtk_entry_buffer_set_max_length (GtkEntryBuffer *buffer,
guint max_length)
{
g_return_if_fail (GTK_IS_ENTRY_BUFFER (buffer));
if (max_length > GTK_ENTRY_BUFFER_MAX_SIZE)
max_length = GTK_ENTRY_BUFFER_MAX_SIZE;
if (max_length > 0 && gtk_entry_buffer_get_length (buffer) > max_length)
gtk_entry_buffer_delete_text (buffer, max_length, -1);
buffer->priv->max_length = max_length;
g_object_notify (G_OBJECT (buffer), "max-length");
}
/**
* gtk_entry_buffer_get_max_length:
* @buffer: a #GtkEntryBuffer
*
* Retrieves the maximum allowed length of the text in
* @buffer. See gtk_entry_buffer_set_max_length().
*
* Return value: the maximum allowed number of characters
* in #GtkEntryBuffer, or 0 if there is no maximum.
*
* Since: 2.18
**/
guint
gtk_entry_buffer_get_max_length (GtkEntryBuffer *buffer)
{
g_return_val_if_fail (GTK_IS_ENTRY_BUFFER (buffer), 0);
return buffer->priv->max_length;
}
/**
* gtk_entry_buffer_insert_text:
* @buffer: a #GtkEntryBuffer
* @position: the position at which to insert text.
* @chars: the text to insert into the buffer.
* @n_chars: the length of the text in characters, or -1
*
* Inserts @n_chars characters of @chars into the contents of the
* buffer, at position @position.
*
* If @n_chars is negative, then characters from chars will be inserted
* until a null-terminator is found. If @position or @n_chars are out of
* bounds, or the maximum buffer text length is exceeded, then they are
* coerced to sane values.
*
* Note that the position and length are in characters, not in bytes.
*
* Returns: The number of characters actually inserted.
*
* Since: 2.18
*/
guint
gtk_entry_buffer_insert_text (GtkEntryBuffer *buffer,
guint position,
const gchar *chars,
gint n_chars)
{
GtkEntryBufferClass *klass;
GtkEntryBufferPrivate *pv;
guint length;
g_return_val_if_fail (GTK_IS_ENTRY_BUFFER (buffer), 0);
length = gtk_entry_buffer_get_length (buffer);
pv = buffer->priv;
if (n_chars < 0)
n_chars = g_utf8_strlen (chars, -1);
/* Bring position into bounds */
if (position > length)
position = length;
/* Make sure not entering too much data */
if (pv->max_length > 0)
{
if (length >= pv->max_length)
n_chars = 0;
else if (length + n_chars > pv->max_length)
n_chars -= (length + n_chars) - pv->max_length;
}
klass = GTK_ENTRY_BUFFER_GET_CLASS (buffer);
g_return_val_if_fail (klass->insert_text != NULL, 0);
return (*klass->insert_text) (buffer, position, chars, n_chars);
}
/**
* gtk_entry_buffer_delete_text:
* @buffer: a #GtkEntryBuffer
* @position: position at which to delete text
* @n_chars: number of characters to delete
*
* Deletes a sequence of characters from the buffer. @n_chars characters are
* deleted starting at @position. If @n_chars is negative, then all characters
* until the end of the text are deleted.
*
* If @position or @n_chars are out of bounds, then they are coerced to sane
* values.
*
* Note that the positions are specified in characters, not bytes.
*
* Returns: The number of characters deleted.
*
* Since: 2.18
*/
guint
gtk_entry_buffer_delete_text (GtkEntryBuffer *buffer,
guint position,
gint n_chars)
{
GtkEntryBufferClass *klass;
guint length;
g_return_val_if_fail (GTK_IS_ENTRY_BUFFER (buffer), 0);
length = gtk_entry_buffer_get_length (buffer);
if (n_chars < 0)
n_chars = length;
if (position > length)
position = length;
if (position + n_chars > length)
n_chars = length - position;
klass = GTK_ENTRY_BUFFER_GET_CLASS (buffer);
g_return_val_if_fail (klass->delete_text != NULL, 0);
return (*klass->delete_text) (buffer, position, n_chars);
}
/**
* gtk_entry_buffer_emit_inserted_text:
* @buffer: a #GtkEntryBuffer
* @position: position at which text was inserted
* @chars: text that was inserted
* @n_chars: number of characters inserted
*
* Used when subclassing #GtkEntryBuffer
*
* Since: 2.18
*/
void
gtk_entry_buffer_emit_inserted_text (GtkEntryBuffer *buffer,
guint position,
const gchar *chars,
guint n_chars)
{
g_return_if_fail (GTK_IS_ENTRY_BUFFER (buffer));
g_signal_emit (buffer, signals[INSERTED_TEXT], 0, position, chars, n_chars);
}
/**
* gtk_entry_buffer_emit_deleted_text:
* @buffer: a #GtkEntryBuffer
* @position: position at which text was deleted
* @n_chars: number of characters deleted
*
* Used when subclassing #GtkEntryBuffer
*
* Since: 2.18
*/
void
gtk_entry_buffer_emit_deleted_text (GtkEntryBuffer *buffer,
guint position,
guint n_chars)
{
g_return_if_fail (GTK_IS_ENTRY_BUFFER (buffer));
g_signal_emit (buffer, signals[DELETED_TEXT], 0, position, n_chars);
}

133
gtk/gtkentrybuffer.h Normal file
View File

@ -0,0 +1,133 @@
/* gtkentrybuffer.h
* Copyright (C) 2009 Stefan Walter <stef@memberwebs.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#if defined(GTK_DISABLE_SINGLE_INCLUDES) && !defined (__GTK_H_INSIDE__) && !defined (GTK_COMPILATION)
#error "Only <gtk/gtk.h> can be included directly."
#endif
#ifndef __GTK_ENTRY_BUFFER_H__
#define __GTK_ENTRY_BUFFER_H__
#include <glib-object.h>
G_BEGIN_DECLS
/* Maximum size of text buffer, in bytes */
#define GTK_ENTRY_BUFFER_MAX_SIZE G_MAXUSHORT
#define GTK_TYPE_ENTRY_BUFFER (gtk_entry_buffer_get_type ())
#define GTK_ENTRY_BUFFER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_ENTRY_BUFFER, GtkEntryBuffer))
#define GTK_ENTRY_BUFFER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_ENTRY_BUFFER, GtkEntryBufferClass))
#define GTK_IS_ENTRY_BUFFER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_ENTRY_BUFFER))
#define GTK_IS_ENTRY_BUFFER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_ENTRY_BUFFER))
#define GTK_ENTRY_BUFFER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_ENTRY_BUFFER, GtkEntryBufferClass))
typedef struct _GtkEntryBuffer GtkEntryBuffer;
typedef struct _GtkEntryBufferClass GtkEntryBufferClass;
typedef struct _GtkEntryBufferPrivate GtkEntryBufferPrivate;
struct _GtkEntryBuffer
{
GObject parent_instance;
/*< private >*/
GtkEntryBufferPrivate *priv;
};
struct _GtkEntryBufferClass
{
GObjectClass parent_class;
/* Signals */
void (*inserted_text) (GtkEntryBuffer *buffer,
guint position,
const gchar *chars,
guint n_chars);
void (*deleted_text) (GtkEntryBuffer *buffer,
guint position,
guint n_chars);
/* Virtual Methods */
const gchar* (*get_text) (GtkEntryBuffer *buffer,
gsize *n_bytes);
guint (*get_length) (GtkEntryBuffer *buffer);
guint (*insert_text) (GtkEntryBuffer *buffer,
guint position,
const gchar *chars,
guint n_chars);
guint (*delete_text) (GtkEntryBuffer *buffer,
guint position,
guint n_chars);
/* Padding for future expansion */
void (*_gtk_reserved0) (void);
void (*_gtk_reserved1) (void);
void (*_gtk_reserved2) (void);
void (*_gtk_reserved3) (void);
void (*_gtk_reserved4) (void);
void (*_gtk_reserved5) (void);
};
GType gtk_entry_buffer_get_type (void) G_GNUC_CONST;
GtkEntryBuffer* gtk_entry_buffer_new (const gchar *initial_chars,
gint n_initial_chars);
gsize gtk_entry_buffer_get_bytes (GtkEntryBuffer *buffer);
guint gtk_entry_buffer_get_length (GtkEntryBuffer *buffer);
const gchar* gtk_entry_buffer_get_text (GtkEntryBuffer *buffer);
void gtk_entry_buffer_set_text (GtkEntryBuffer *buffer,
const gchar *chars,
gint n_chars);
void gtk_entry_buffer_set_max_length (GtkEntryBuffer *buffer,
guint max_length);
guint gtk_entry_buffer_get_max_length (GtkEntryBuffer *buffer);
guint gtk_entry_buffer_insert_text (GtkEntryBuffer *buffer,
guint position,
const gchar *chars,
gint n_chars);
guint gtk_entry_buffer_delete_text (GtkEntryBuffer *buffer,
guint position,
gint n_chars);
void gtk_entry_buffer_emit_inserted_text (GtkEntryBuffer *buffer,
guint position,
const gchar *chars,
guint n_chars);
void gtk_entry_buffer_emit_deleted_text (GtkEntryBuffer *buffer,
guint position,
guint n_chars);
G_END_DECLS
#endif /* __GTK_ENTRY_BUFFER_H__ */

View File

@ -107,5 +107,7 @@ VOID:UINT,UINT
VOID:UINT,STRING
VOID:UINT,BOXED,UINT,FLAGS,FLAGS
VOID:UINT,OBJECT,UINT,FLAGS,FLAGS
VOID:UINT,STRING,UINT
VOID:UINT,UINT
VOID:VOID
OBJECT:OBJECT,INT,INT