2 * Copyright (C) 2009 Stefan Walter <stef@memberwebs.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library 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 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
22 #include "gtkentrybuffer.h"
24 #include "gtkmarshalers.h"
25 #include "gtkprivate.h"
26 #include "gtkwidget.h"
33 * SECTION:gtkentrybuffer
34 * @title: GtkEntryBuffer
35 * @short_description: Text buffer for GtkEntry
37 * The #GtkEntryBuffer class contains the actual text displayed in a
40 * A single #GtkEntryBuffer object can be shared by multiple #GtkEntry
41 * widgets which will then share the same text content, but not the cursor
42 * position, visibility attributes, icon etc.
44 * #GtkEntryBuffer may be derived from. Such a derived class might allow
45 * text to be stored in an alternate location, such as non-pageable memory,
46 * useful in the case of important passwords. Or a derived class could
47 * integrate with an application's concept of undo/redo.
52 /* Initial size of buffer, in bytes */
68 static guint signals[LAST_SIGNAL] = { 0 };
70 struct _GtkEntryBufferPrivate
72 /* Only valid if this class is not derived */
74 gsize normal_text_size;
75 gsize normal_text_bytes;
76 guint normal_text_chars;
81 G_DEFINE_TYPE (GtkEntryBuffer, gtk_entry_buffer, G_TYPE_OBJECT);
83 /* --------------------------------------------------------------------------------
84 * DEFAULT IMPLEMENTATIONS OF TEXT BUFFER
86 * These may be overridden by a derived class, behavior may be changed etc...
87 * The normal_text and normal_text_xxxx fields may not be valid when
88 * this class is derived from.
91 /* Overwrite a memory that might contain sensitive information. */
93 trash_area (gchar *area,
96 volatile gchar *varea = (volatile gchar *)area;
102 gtk_entry_buffer_normal_get_text (GtkEntryBuffer *buffer,
106 *n_bytes = buffer->priv->normal_text_bytes;
107 if (!buffer->priv->normal_text)
109 return buffer->priv->normal_text;
113 gtk_entry_buffer_normal_get_length (GtkEntryBuffer *buffer)
115 return buffer->priv->normal_text_chars;
119 gtk_entry_buffer_normal_insert_text (GtkEntryBuffer *buffer,
124 GtkEntryBufferPrivate *pv = buffer->priv;
129 n_bytes = g_utf8_offset_to_pointer (chars, n_chars) - chars;
131 /* Need more memory */
132 if (n_bytes + pv->normal_text_bytes + 1 > pv->normal_text_size)
136 prev_size = pv->normal_text_size;
138 /* Calculate our new buffer size */
139 while (n_bytes + pv->normal_text_bytes + 1 > pv->normal_text_size)
141 if (pv->normal_text_size == 0)
142 pv->normal_text_size = MIN_SIZE;
145 if (2 * pv->normal_text_size < GTK_ENTRY_BUFFER_MAX_SIZE)
146 pv->normal_text_size *= 2;
149 pv->normal_text_size = GTK_ENTRY_BUFFER_MAX_SIZE;
150 if (n_bytes > pv->normal_text_size - pv->normal_text_bytes - 1)
152 n_bytes = pv->normal_text_size - pv->normal_text_bytes - 1;
153 n_bytes = g_utf8_find_prev_char (chars, chars + n_bytes + 1) - chars;
154 n_chars = g_utf8_strlen (chars, n_bytes);
161 /* Could be a password, so can't leave stuff in memory. */
162 et_new = g_malloc (pv->normal_text_size);
163 memcpy (et_new, pv->normal_text, MIN (prev_size, pv->normal_text_size));
164 trash_area (pv->normal_text, prev_size);
165 g_free (pv->normal_text);
166 pv->normal_text = et_new;
169 /* Actual text insertion */
170 at = g_utf8_offset_to_pointer (pv->normal_text, position) - pv->normal_text;
171 g_memmove (pv->normal_text + at + n_bytes, pv->normal_text + at, pv->normal_text_bytes - at);
172 memcpy (pv->normal_text + at, chars, n_bytes);
175 pv->normal_text_bytes += n_bytes;
176 pv->normal_text_chars += n_chars;
177 pv->normal_text[pv->normal_text_bytes] = '\0';
179 gtk_entry_buffer_emit_inserted_text (buffer, position, chars, n_chars);
184 gtk_entry_buffer_normal_delete_text (GtkEntryBuffer *buffer,
188 GtkEntryBufferPrivate *pv = buffer->priv;
191 if (position > pv->normal_text_chars)
192 position = pv->normal_text_chars;
193 if (position + n_chars > pv->normal_text_chars)
194 n_chars = pv->normal_text_chars - position;
198 start = g_utf8_offset_to_pointer (pv->normal_text, position) - pv->normal_text;
199 end = g_utf8_offset_to_pointer (pv->normal_text, position + n_chars) - pv->normal_text;
201 g_memmove (pv->normal_text + start, pv->normal_text + end, pv->normal_text_bytes + 1 - end);
202 pv->normal_text_chars -= n_chars;
203 pv->normal_text_bytes -= (end - start);
206 * Could be a password, make sure we don't leave anything sensitive after
207 * the terminating zero. Note, that the terminating zero already trashed
210 trash_area (pv->normal_text + pv->normal_text_bytes + 1, end - start - 1);
212 gtk_entry_buffer_emit_deleted_text (buffer, position, n_chars);
218 /* --------------------------------------------------------------------------------
223 gtk_entry_buffer_real_inserted_text (GtkEntryBuffer *buffer,
228 g_object_notify (G_OBJECT (buffer), "text");
229 g_object_notify (G_OBJECT (buffer), "length");
233 gtk_entry_buffer_real_deleted_text (GtkEntryBuffer *buffer,
237 g_object_notify (G_OBJECT (buffer), "text");
238 g_object_notify (G_OBJECT (buffer), "length");
241 /* --------------------------------------------------------------------------------
246 gtk_entry_buffer_init (GtkEntryBuffer *buffer)
248 GtkEntryBufferPrivate *pv;
250 pv = buffer->priv = G_TYPE_INSTANCE_GET_PRIVATE (buffer, GTK_TYPE_ENTRY_BUFFER, GtkEntryBufferPrivate);
252 pv->normal_text = NULL;
253 pv->normal_text_chars = 0;
254 pv->normal_text_bytes = 0;
255 pv->normal_text_size = 0;
259 gtk_entry_buffer_finalize (GObject *obj)
261 GtkEntryBuffer *buffer = GTK_ENTRY_BUFFER (obj);
262 GtkEntryBufferPrivate *pv = buffer->priv;
266 trash_area (pv->normal_text, pv->normal_text_size);
267 g_free (pv->normal_text);
268 pv->normal_text = NULL;
269 pv->normal_text_bytes = pv->normal_text_size = 0;
270 pv->normal_text_chars = 0;
273 G_OBJECT_CLASS (gtk_entry_buffer_parent_class)->finalize (obj);
277 gtk_entry_buffer_set_property (GObject *obj,
282 GtkEntryBuffer *buffer = GTK_ENTRY_BUFFER (obj);
287 gtk_entry_buffer_set_text (buffer, g_value_get_string (value), -1);
289 case PROP_MAX_LENGTH:
290 gtk_entry_buffer_set_max_length (buffer, g_value_get_int (value));
293 G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, prop_id, pspec);
299 gtk_entry_buffer_get_property (GObject *obj,
304 GtkEntryBuffer *buffer = GTK_ENTRY_BUFFER (obj);
309 g_value_set_string (value, gtk_entry_buffer_get_text (buffer));
312 g_value_set_uint (value, gtk_entry_buffer_get_length (buffer));
314 case PROP_MAX_LENGTH:
315 g_value_set_int (value, gtk_entry_buffer_get_max_length (buffer));
318 G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, prop_id, pspec);
324 gtk_entry_buffer_class_init (GtkEntryBufferClass *klass)
326 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
328 gobject_class->finalize = gtk_entry_buffer_finalize;
329 gobject_class->set_property = gtk_entry_buffer_set_property;
330 gobject_class->get_property = gtk_entry_buffer_get_property;
332 klass->get_text = gtk_entry_buffer_normal_get_text;
333 klass->get_length = gtk_entry_buffer_normal_get_length;
334 klass->insert_text = gtk_entry_buffer_normal_insert_text;
335 klass->delete_text = gtk_entry_buffer_normal_delete_text;
337 klass->inserted_text = gtk_entry_buffer_real_inserted_text;
338 klass->deleted_text = gtk_entry_buffer_real_deleted_text;
340 g_type_class_add_private (gobject_class, sizeof (GtkEntryBufferPrivate));
343 * GtkEntryBuffer:text:
345 * The contents of the buffer.
349 g_object_class_install_property (gobject_class,
351 g_param_spec_string ("text",
353 P_("The contents of the buffer"),
355 GTK_PARAM_READWRITE));
358 * GtkEntryBuffer:length:
360 * The length (in characters) of the text in buffer.
364 g_object_class_install_property (gobject_class,
366 g_param_spec_uint ("length",
368 P_("Length of the text currently in the buffer"),
369 0, GTK_ENTRY_BUFFER_MAX_SIZE, 0,
370 GTK_PARAM_READABLE));
373 * GtkEntryBuffer:max-length:
375 * The maximum length (in characters) of the text in the buffer.
379 g_object_class_install_property (gobject_class,
381 g_param_spec_int ("max-length",
382 P_("Maximum length"),
383 P_("Maximum number of characters for this entry. Zero if no maximum"),
384 0, GTK_ENTRY_BUFFER_MAX_SIZE, 0,
385 GTK_PARAM_READWRITE));
388 * GtkEntryBuffer::inserted-text:
389 * @buffer: a #GtkEntryBuffer
390 * @position: the position the text was inserted at.
391 * @chars: The text that was inserted.
392 * @n_chars: The number of characters that were inserted.
394 * This signal is emitted after text is inserted into the buffer.
398 signals[INSERTED_TEXT] = g_signal_new (I_("inserted-text"),
399 GTK_TYPE_ENTRY_BUFFER,
401 G_STRUCT_OFFSET (GtkEntryBufferClass, inserted_text),
403 _gtk_marshal_VOID__UINT_STRING_UINT,
410 * GtkEntryBuffer::deleted-text:
411 * @buffer: a #GtkEntryBuffer
412 * @position: the position the text was deleted at.
413 * @n_chars: The number of characters that were deleted.
415 * This signal is emitted after text is deleted from the buffer.
419 signals[DELETED_TEXT] = g_signal_new (I_("deleted-text"),
420 GTK_TYPE_ENTRY_BUFFER,
422 G_STRUCT_OFFSET (GtkEntryBufferClass, deleted_text),
424 _gtk_marshal_VOID__UINT_UINT,
430 /* --------------------------------------------------------------------------------
435 * gtk_entry_buffer_new:
436 * @initial_chars: (allow-none): initial buffer text, or %NULL
437 * @n_initial_chars: number of characters in @initial_chars, or -1
439 * Create a new GtkEntryBuffer object.
441 * Optionally, specify initial text to set in the buffer.
443 * Return value: A new GtkEntryBuffer object.
448 gtk_entry_buffer_new (const gchar *initial_chars,
449 gint n_initial_chars)
451 GtkEntryBuffer *buffer = g_object_new (GTK_TYPE_ENTRY_BUFFER, NULL);
453 gtk_entry_buffer_set_text (buffer, initial_chars, n_initial_chars);
458 * gtk_entry_buffer_get_length:
459 * @buffer: a #GtkEntryBuffer
461 * Retrieves the length in characters of the buffer.
463 * Return value: The number of characters in the buffer.
468 gtk_entry_buffer_get_length (GtkEntryBuffer *buffer)
470 GtkEntryBufferClass *klass;
472 g_return_val_if_fail (GTK_IS_ENTRY_BUFFER (buffer), 0);
474 klass = GTK_ENTRY_BUFFER_GET_CLASS (buffer);
475 g_return_val_if_fail (klass->get_length != NULL, 0);
477 return (*klass->get_length) (buffer);
481 * gtk_entry_buffer_get_bytes:
482 * @buffer: a #GtkEntryBuffer
484 * Retrieves the length in bytes of the buffer.
485 * See gtk_entry_buffer_get_length().
487 * Return value: The byte length of the buffer.
492 gtk_entry_buffer_get_bytes (GtkEntryBuffer *buffer)
494 GtkEntryBufferClass *klass;
497 g_return_val_if_fail (GTK_IS_ENTRY_BUFFER (buffer), 0);
499 klass = GTK_ENTRY_BUFFER_GET_CLASS (buffer);
500 g_return_val_if_fail (klass->get_text != NULL, 0);
502 (*klass->get_text) (buffer, &bytes);
507 * gtk_entry_buffer_get_text:
508 * @buffer: a #GtkEntryBuffer
510 * Retrieves the contents of the buffer.
512 * The memory pointer returned by this call will not change
513 * unless this object emits a signal, or is finalized.
515 * Return value: a pointer to the contents of the widget as a
516 * string. This string points to internally allocated
517 * storage in the buffer and must not be freed, modified or
523 gtk_entry_buffer_get_text (GtkEntryBuffer *buffer)
525 GtkEntryBufferClass *klass;
527 g_return_val_if_fail (GTK_IS_ENTRY_BUFFER (buffer), NULL);
529 klass = GTK_ENTRY_BUFFER_GET_CLASS (buffer);
530 g_return_val_if_fail (klass->get_text != NULL, NULL);
532 return (*klass->get_text) (buffer, NULL);
536 * gtk_entry_buffer_set_text:
537 * @buffer: a #GtkEntryBuffer
538 * @chars: the new text
539 * @n_chars: the number of characters in @text, or -1
541 * Sets the text in the buffer.
543 * This is roughly equivalent to calling gtk_entry_buffer_delete_text()
544 * and gtk_entry_buffer_insert_text().
546 * Note that @n_chars is in characters, not in bytes.
551 gtk_entry_buffer_set_text (GtkEntryBuffer *buffer,
555 g_return_if_fail (GTK_IS_ENTRY_BUFFER (buffer));
556 g_return_if_fail (chars != NULL);
558 g_object_freeze_notify (G_OBJECT (buffer));
559 gtk_entry_buffer_delete_text (buffer, 0, -1);
560 gtk_entry_buffer_insert_text (buffer, 0, chars, n_chars);
561 g_object_thaw_notify (G_OBJECT (buffer));
565 * gtk_entry_buffer_set_max_length:
566 * @buffer: a #GtkEntryBuffer
567 * @max_length: the maximum length of the entry buffer, or 0 for no maximum.
568 * (other than the maximum length of entries.) The value passed in will
569 * be clamped to the range 0-65536.
571 * Sets the maximum allowed length of the contents of the buffer. If
572 * the current contents are longer than the given length, then they
573 * will be truncated to fit.
578 gtk_entry_buffer_set_max_length (GtkEntryBuffer *buffer,
581 g_return_if_fail (GTK_IS_ENTRY_BUFFER (buffer));
583 max_length = CLAMP (max_length, 0, GTK_ENTRY_BUFFER_MAX_SIZE);
585 if (max_length > 0 && gtk_entry_buffer_get_length (buffer) > max_length)
586 gtk_entry_buffer_delete_text (buffer, max_length, -1);
588 buffer->priv->max_length = max_length;
589 g_object_notify (G_OBJECT (buffer), "max-length");
593 * gtk_entry_buffer_get_max_length:
594 * @buffer: a #GtkEntryBuffer
596 * Retrieves the maximum allowed length of the text in
597 * @buffer. See gtk_entry_buffer_set_max_length().
599 * Return value: the maximum allowed number of characters
600 * in #GtkEntryBuffer, or 0 if there is no maximum.
605 gtk_entry_buffer_get_max_length (GtkEntryBuffer *buffer)
607 g_return_val_if_fail (GTK_IS_ENTRY_BUFFER (buffer), 0);
608 return buffer->priv->max_length;
612 * gtk_entry_buffer_insert_text:
613 * @buffer: a #GtkEntryBuffer
614 * @position: the position at which to insert text.
615 * @chars: the text to insert into the buffer.
616 * @n_chars: the length of the text in characters, or -1
618 * Inserts @n_chars characters of @chars into the contents of the
619 * buffer, at position @position.
621 * If @n_chars is negative, then characters from chars will be inserted
622 * until a null-terminator is found. If @position or @n_chars are out of
623 * bounds, or the maximum buffer text length is exceeded, then they are
624 * coerced to sane values.
626 * Note that the position and length are in characters, not in bytes.
628 * Returns: The number of characters actually inserted.
633 gtk_entry_buffer_insert_text (GtkEntryBuffer *buffer,
638 GtkEntryBufferClass *klass;
639 GtkEntryBufferPrivate *pv;
642 g_return_val_if_fail (GTK_IS_ENTRY_BUFFER (buffer), 0);
644 length = gtk_entry_buffer_get_length (buffer);
648 n_chars = g_utf8_strlen (chars, -1);
650 /* Bring position into bounds */
651 if (position > length)
654 /* Make sure not entering too much data */
655 if (pv->max_length > 0)
657 if (length >= pv->max_length)
659 else if (length + n_chars > pv->max_length)
660 n_chars -= (length + n_chars) - pv->max_length;
663 klass = GTK_ENTRY_BUFFER_GET_CLASS (buffer);
664 g_return_val_if_fail (klass->insert_text != NULL, 0);
666 return (*klass->insert_text) (buffer, position, chars, n_chars);
670 * gtk_entry_buffer_delete_text:
671 * @buffer: a #GtkEntryBuffer
672 * @position: position at which to delete text
673 * @n_chars: number of characters to delete
675 * Deletes a sequence of characters from the buffer. @n_chars characters are
676 * deleted starting at @position. If @n_chars is negative, then all characters
677 * until the end of the text are deleted.
679 * If @position or @n_chars are out of bounds, then they are coerced to sane
682 * Note that the positions are specified in characters, not bytes.
684 * Returns: The number of characters deleted.
689 gtk_entry_buffer_delete_text (GtkEntryBuffer *buffer,
693 GtkEntryBufferClass *klass;
696 g_return_val_if_fail (GTK_IS_ENTRY_BUFFER (buffer), 0);
698 length = gtk_entry_buffer_get_length (buffer);
701 if (position > length)
703 if (position + n_chars > length)
704 n_chars = length - position;
706 klass = GTK_ENTRY_BUFFER_GET_CLASS (buffer);
707 g_return_val_if_fail (klass->delete_text != NULL, 0);
709 return (*klass->delete_text) (buffer, position, n_chars);
713 * gtk_entry_buffer_emit_inserted_text:
714 * @buffer: a #GtkEntryBuffer
715 * @position: position at which text was inserted
716 * @chars: text that was inserted
717 * @n_chars: number of characters inserted
719 * Used when subclassing #GtkEntryBuffer
724 gtk_entry_buffer_emit_inserted_text (GtkEntryBuffer *buffer,
729 g_return_if_fail (GTK_IS_ENTRY_BUFFER (buffer));
730 g_signal_emit (buffer, signals[INSERTED_TEXT], 0, position, chars, n_chars);
734 * gtk_entry_buffer_emit_deleted_text:
735 * @buffer: a #GtkEntryBuffer
736 * @position: position at which text was deleted
737 * @n_chars: number of characters deleted
739 * Used when subclassing #GtkEntryBuffer
744 gtk_entry_buffer_emit_deleted_text (GtkEntryBuffer *buffer,
748 g_return_if_fail (GTK_IS_ENTRY_BUFFER (buffer));
749 g_signal_emit (buffer, signals[DELETED_TEXT], 0, position, n_chars);