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, see <http://www.gnu.org/licenses/>.
20 #include "gtkentrybuffer.h"
22 #include "gtkmarshalers.h"
23 #include "gtkprivate.h"
24 #include "gtkwidget.h"
31 * SECTION:gtkentrybuffer
32 * @title: GtkEntryBuffer
33 * @short_description: Text buffer for GtkEntry
35 * The #GtkEntryBuffer class contains the actual text displayed in a
38 * A single #GtkEntryBuffer object can be shared by multiple #GtkEntry
39 * widgets which will then share the same text content, but not the cursor
40 * position, visibility attributes, icon etc.
42 * #GtkEntryBuffer may be derived from. Such a derived class might allow
43 * text to be stored in an alternate location, such as non-pageable memory,
44 * useful in the case of important passwords. Or a derived class could
45 * integrate with an application's concept of undo/redo.
50 /* Initial size of buffer, in bytes */
66 static guint signals[LAST_SIGNAL] = { 0 };
68 struct _GtkEntryBufferPrivate
70 /* Only valid if this class is not derived */
72 gsize normal_text_size;
73 gsize normal_text_bytes;
74 guint normal_text_chars;
79 G_DEFINE_TYPE (GtkEntryBuffer, gtk_entry_buffer, G_TYPE_OBJECT);
81 /* --------------------------------------------------------------------------------
82 * DEFAULT IMPLEMENTATIONS OF TEXT BUFFER
84 * These may be overridden by a derived class, behavior may be changed etc...
85 * The normal_text and normal_text_xxxx fields may not be valid when
86 * this class is derived from.
89 /* Overwrite a memory that might contain sensitive information. */
91 trash_area (gchar *area,
94 volatile gchar *varea = (volatile gchar *)area;
100 gtk_entry_buffer_normal_get_text (GtkEntryBuffer *buffer,
104 *n_bytes = buffer->priv->normal_text_bytes;
105 if (!buffer->priv->normal_text)
107 return buffer->priv->normal_text;
111 gtk_entry_buffer_normal_get_length (GtkEntryBuffer *buffer)
113 return buffer->priv->normal_text_chars;
117 gtk_entry_buffer_normal_insert_text (GtkEntryBuffer *buffer,
122 GtkEntryBufferPrivate *pv = buffer->priv;
127 n_bytes = g_utf8_offset_to_pointer (chars, n_chars) - chars;
129 /* Need more memory */
130 if (n_bytes + pv->normal_text_bytes + 1 > pv->normal_text_size)
134 prev_size = pv->normal_text_size;
136 /* Calculate our new buffer size */
137 while (n_bytes + pv->normal_text_bytes + 1 > pv->normal_text_size)
139 if (pv->normal_text_size == 0)
140 pv->normal_text_size = MIN_SIZE;
143 if (2 * pv->normal_text_size < GTK_ENTRY_BUFFER_MAX_SIZE)
144 pv->normal_text_size *= 2;
147 pv->normal_text_size = GTK_ENTRY_BUFFER_MAX_SIZE;
148 if (n_bytes > pv->normal_text_size - pv->normal_text_bytes - 1)
150 n_bytes = pv->normal_text_size - pv->normal_text_bytes - 1;
151 n_bytes = g_utf8_find_prev_char (chars, chars + n_bytes + 1) - chars;
152 n_chars = g_utf8_strlen (chars, n_bytes);
159 /* Could be a password, so can't leave stuff in memory. */
160 et_new = g_malloc (pv->normal_text_size);
161 memcpy (et_new, pv->normal_text, MIN (prev_size, pv->normal_text_size));
162 trash_area (pv->normal_text, prev_size);
163 g_free (pv->normal_text);
164 pv->normal_text = et_new;
167 /* Actual text insertion */
168 at = g_utf8_offset_to_pointer (pv->normal_text, position) - pv->normal_text;
169 g_memmove (pv->normal_text + at + n_bytes, pv->normal_text + at, pv->normal_text_bytes - at);
170 memcpy (pv->normal_text + at, chars, n_bytes);
173 pv->normal_text_bytes += n_bytes;
174 pv->normal_text_chars += n_chars;
175 pv->normal_text[pv->normal_text_bytes] = '\0';
177 gtk_entry_buffer_emit_inserted_text (buffer, position, chars, n_chars);
182 gtk_entry_buffer_normal_delete_text (GtkEntryBuffer *buffer,
186 GtkEntryBufferPrivate *pv = buffer->priv;
189 if (position > pv->normal_text_chars)
190 position = pv->normal_text_chars;
191 if (position + n_chars > pv->normal_text_chars)
192 n_chars = pv->normal_text_chars - position;
196 start = g_utf8_offset_to_pointer (pv->normal_text, position) - pv->normal_text;
197 end = g_utf8_offset_to_pointer (pv->normal_text, position + n_chars) - pv->normal_text;
199 g_memmove (pv->normal_text + start, pv->normal_text + end, pv->normal_text_bytes + 1 - end);
200 pv->normal_text_chars -= n_chars;
201 pv->normal_text_bytes -= (end - start);
204 * Could be a password, make sure we don't leave anything sensitive after
205 * the terminating zero. Note, that the terminating zero already trashed
208 trash_area (pv->normal_text + pv->normal_text_bytes + 1, end - start - 1);
210 gtk_entry_buffer_emit_deleted_text (buffer, position, n_chars);
216 /* --------------------------------------------------------------------------------
221 gtk_entry_buffer_real_inserted_text (GtkEntryBuffer *buffer,
226 g_object_notify (G_OBJECT (buffer), "text");
227 g_object_notify (G_OBJECT (buffer), "length");
231 gtk_entry_buffer_real_deleted_text (GtkEntryBuffer *buffer,
235 g_object_notify (G_OBJECT (buffer), "text");
236 g_object_notify (G_OBJECT (buffer), "length");
239 /* --------------------------------------------------------------------------------
244 gtk_entry_buffer_init (GtkEntryBuffer *buffer)
246 GtkEntryBufferPrivate *pv;
248 pv = buffer->priv = G_TYPE_INSTANCE_GET_PRIVATE (buffer, GTK_TYPE_ENTRY_BUFFER, GtkEntryBufferPrivate);
250 pv->normal_text = NULL;
251 pv->normal_text_chars = 0;
252 pv->normal_text_bytes = 0;
253 pv->normal_text_size = 0;
257 gtk_entry_buffer_finalize (GObject *obj)
259 GtkEntryBuffer *buffer = GTK_ENTRY_BUFFER (obj);
260 GtkEntryBufferPrivate *pv = buffer->priv;
264 trash_area (pv->normal_text, pv->normal_text_size);
265 g_free (pv->normal_text);
266 pv->normal_text = NULL;
267 pv->normal_text_bytes = pv->normal_text_size = 0;
268 pv->normal_text_chars = 0;
271 G_OBJECT_CLASS (gtk_entry_buffer_parent_class)->finalize (obj);
275 gtk_entry_buffer_set_property (GObject *obj,
280 GtkEntryBuffer *buffer = GTK_ENTRY_BUFFER (obj);
285 gtk_entry_buffer_set_text (buffer, g_value_get_string (value), -1);
287 case PROP_MAX_LENGTH:
288 gtk_entry_buffer_set_max_length (buffer, g_value_get_int (value));
291 G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, prop_id, pspec);
297 gtk_entry_buffer_get_property (GObject *obj,
302 GtkEntryBuffer *buffer = GTK_ENTRY_BUFFER (obj);
307 g_value_set_string (value, gtk_entry_buffer_get_text (buffer));
310 g_value_set_uint (value, gtk_entry_buffer_get_length (buffer));
312 case PROP_MAX_LENGTH:
313 g_value_set_int (value, gtk_entry_buffer_get_max_length (buffer));
316 G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, prop_id, pspec);
322 gtk_entry_buffer_class_init (GtkEntryBufferClass *klass)
324 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
326 gobject_class->finalize = gtk_entry_buffer_finalize;
327 gobject_class->set_property = gtk_entry_buffer_set_property;
328 gobject_class->get_property = gtk_entry_buffer_get_property;
330 klass->get_text = gtk_entry_buffer_normal_get_text;
331 klass->get_length = gtk_entry_buffer_normal_get_length;
332 klass->insert_text = gtk_entry_buffer_normal_insert_text;
333 klass->delete_text = gtk_entry_buffer_normal_delete_text;
335 klass->inserted_text = gtk_entry_buffer_real_inserted_text;
336 klass->deleted_text = gtk_entry_buffer_real_deleted_text;
338 g_type_class_add_private (gobject_class, sizeof (GtkEntryBufferPrivate));
341 * GtkEntryBuffer:text:
343 * The contents of the buffer.
347 g_object_class_install_property (gobject_class,
349 g_param_spec_string ("text",
351 P_("The contents of the buffer"),
353 GTK_PARAM_READWRITE));
356 * GtkEntryBuffer:length:
358 * The length (in characters) of the text in buffer.
362 g_object_class_install_property (gobject_class,
364 g_param_spec_uint ("length",
366 P_("Length of the text currently in the buffer"),
367 0, GTK_ENTRY_BUFFER_MAX_SIZE, 0,
368 GTK_PARAM_READABLE));
371 * GtkEntryBuffer:max-length:
373 * The maximum length (in characters) of the text in the buffer.
377 g_object_class_install_property (gobject_class,
379 g_param_spec_int ("max-length",
380 P_("Maximum length"),
381 P_("Maximum number of characters for this entry. Zero if no maximum"),
382 0, GTK_ENTRY_BUFFER_MAX_SIZE, 0,
383 GTK_PARAM_READWRITE));
386 * GtkEntryBuffer::inserted-text:
387 * @buffer: a #GtkEntryBuffer
388 * @position: the position the text was inserted at.
389 * @chars: The text that was inserted.
390 * @n_chars: The number of characters that were inserted.
392 * This signal is emitted after text is inserted into the buffer.
396 signals[INSERTED_TEXT] = g_signal_new (I_("inserted-text"),
397 GTK_TYPE_ENTRY_BUFFER,
399 G_STRUCT_OFFSET (GtkEntryBufferClass, inserted_text),
401 _gtk_marshal_VOID__UINT_STRING_UINT,
408 * GtkEntryBuffer::deleted-text:
409 * @buffer: a #GtkEntryBuffer
410 * @position: the position the text was deleted at.
411 * @n_chars: The number of characters that were deleted.
413 * This signal is emitted after text is deleted from the buffer.
417 signals[DELETED_TEXT] = g_signal_new (I_("deleted-text"),
418 GTK_TYPE_ENTRY_BUFFER,
420 G_STRUCT_OFFSET (GtkEntryBufferClass, deleted_text),
422 _gtk_marshal_VOID__UINT_UINT,
428 /* --------------------------------------------------------------------------------
433 * gtk_entry_buffer_new:
434 * @initial_chars: (allow-none): initial buffer text, or %NULL
435 * @n_initial_chars: number of characters in @initial_chars, or -1
437 * Create a new GtkEntryBuffer object.
439 * Optionally, specify initial text to set in the buffer.
441 * Return value: A new GtkEntryBuffer object.
446 gtk_entry_buffer_new (const gchar *initial_chars,
447 gint n_initial_chars)
449 GtkEntryBuffer *buffer = g_object_new (GTK_TYPE_ENTRY_BUFFER, NULL);
451 gtk_entry_buffer_set_text (buffer, initial_chars, n_initial_chars);
456 * gtk_entry_buffer_get_length:
457 * @buffer: a #GtkEntryBuffer
459 * Retrieves the length in characters of the buffer.
461 * Return value: The number of characters in the buffer.
466 gtk_entry_buffer_get_length (GtkEntryBuffer *buffer)
468 GtkEntryBufferClass *klass;
470 g_return_val_if_fail (GTK_IS_ENTRY_BUFFER (buffer), 0);
472 klass = GTK_ENTRY_BUFFER_GET_CLASS (buffer);
473 g_return_val_if_fail (klass->get_length != NULL, 0);
475 return (*klass->get_length) (buffer);
479 * gtk_entry_buffer_get_bytes:
480 * @buffer: a #GtkEntryBuffer
482 * Retrieves the length in bytes of the buffer.
483 * See gtk_entry_buffer_get_length().
485 * Return value: The byte length of the buffer.
490 gtk_entry_buffer_get_bytes (GtkEntryBuffer *buffer)
492 GtkEntryBufferClass *klass;
495 g_return_val_if_fail (GTK_IS_ENTRY_BUFFER (buffer), 0);
497 klass = GTK_ENTRY_BUFFER_GET_CLASS (buffer);
498 g_return_val_if_fail (klass->get_text != NULL, 0);
500 (*klass->get_text) (buffer, &bytes);
505 * gtk_entry_buffer_get_text:
506 * @buffer: a #GtkEntryBuffer
508 * Retrieves the contents of the buffer.
510 * The memory pointer returned by this call will not change
511 * unless this object emits a signal, or is finalized.
513 * Return value: a pointer to the contents of the widget as a
514 * string. This string points to internally allocated
515 * storage in the buffer and must not be freed, modified or
521 gtk_entry_buffer_get_text (GtkEntryBuffer *buffer)
523 GtkEntryBufferClass *klass;
525 g_return_val_if_fail (GTK_IS_ENTRY_BUFFER (buffer), NULL);
527 klass = GTK_ENTRY_BUFFER_GET_CLASS (buffer);
528 g_return_val_if_fail (klass->get_text != NULL, NULL);
530 return (*klass->get_text) (buffer, NULL);
534 * gtk_entry_buffer_set_text:
535 * @buffer: a #GtkEntryBuffer
536 * @chars: the new text
537 * @n_chars: the number of characters in @text, or -1
539 * Sets the text in the buffer.
541 * This is roughly equivalent to calling gtk_entry_buffer_delete_text()
542 * and gtk_entry_buffer_insert_text().
544 * Note that @n_chars is in characters, not in bytes.
549 gtk_entry_buffer_set_text (GtkEntryBuffer *buffer,
553 g_return_if_fail (GTK_IS_ENTRY_BUFFER (buffer));
554 g_return_if_fail (chars != NULL);
556 g_object_freeze_notify (G_OBJECT (buffer));
557 gtk_entry_buffer_delete_text (buffer, 0, -1);
558 gtk_entry_buffer_insert_text (buffer, 0, chars, n_chars);
559 g_object_thaw_notify (G_OBJECT (buffer));
563 * gtk_entry_buffer_set_max_length:
564 * @buffer: a #GtkEntryBuffer
565 * @max_length: the maximum length of the entry buffer, or 0 for no maximum.
566 * (other than the maximum length of entries.) The value passed in will
567 * be clamped to the range 0-65536.
569 * Sets the maximum allowed length of the contents of the buffer. If
570 * the current contents are longer than the given length, then they
571 * will be truncated to fit.
576 gtk_entry_buffer_set_max_length (GtkEntryBuffer *buffer,
579 g_return_if_fail (GTK_IS_ENTRY_BUFFER (buffer));
581 max_length = CLAMP (max_length, 0, GTK_ENTRY_BUFFER_MAX_SIZE);
583 if (max_length > 0 && gtk_entry_buffer_get_length (buffer) > max_length)
584 gtk_entry_buffer_delete_text (buffer, max_length, -1);
586 buffer->priv->max_length = max_length;
587 g_object_notify (G_OBJECT (buffer), "max-length");
591 * gtk_entry_buffer_get_max_length:
592 * @buffer: a #GtkEntryBuffer
594 * Retrieves the maximum allowed length of the text in
595 * @buffer. See gtk_entry_buffer_set_max_length().
597 * Return value: the maximum allowed number of characters
598 * in #GtkEntryBuffer, or 0 if there is no maximum.
603 gtk_entry_buffer_get_max_length (GtkEntryBuffer *buffer)
605 g_return_val_if_fail (GTK_IS_ENTRY_BUFFER (buffer), 0);
606 return buffer->priv->max_length;
610 * gtk_entry_buffer_insert_text:
611 * @buffer: a #GtkEntryBuffer
612 * @position: the position at which to insert text.
613 * @chars: the text to insert into the buffer.
614 * @n_chars: the length of the text in characters, or -1
616 * Inserts @n_chars characters of @chars into the contents of the
617 * buffer, at position @position.
619 * If @n_chars is negative, then characters from chars will be inserted
620 * until a null-terminator is found. If @position or @n_chars are out of
621 * bounds, or the maximum buffer text length is exceeded, then they are
622 * coerced to sane values.
624 * Note that the position and length are in characters, not in bytes.
626 * Returns: The number of characters actually inserted.
631 gtk_entry_buffer_insert_text (GtkEntryBuffer *buffer,
636 GtkEntryBufferClass *klass;
637 GtkEntryBufferPrivate *pv;
640 g_return_val_if_fail (GTK_IS_ENTRY_BUFFER (buffer), 0);
642 length = gtk_entry_buffer_get_length (buffer);
646 n_chars = g_utf8_strlen (chars, -1);
648 /* Bring position into bounds */
649 if (position > length)
652 /* Make sure not entering too much data */
653 if (pv->max_length > 0)
655 if (length >= pv->max_length)
657 else if (length + n_chars > pv->max_length)
658 n_chars -= (length + n_chars) - pv->max_length;
661 klass = GTK_ENTRY_BUFFER_GET_CLASS (buffer);
662 g_return_val_if_fail (klass->insert_text != NULL, 0);
664 return (*klass->insert_text) (buffer, position, chars, n_chars);
668 * gtk_entry_buffer_delete_text:
669 * @buffer: a #GtkEntryBuffer
670 * @position: position at which to delete text
671 * @n_chars: number of characters to delete
673 * Deletes a sequence of characters from the buffer. @n_chars characters are
674 * deleted starting at @position. If @n_chars is negative, then all characters
675 * until the end of the text are deleted.
677 * If @position or @n_chars are out of bounds, then they are coerced to sane
680 * Note that the positions are specified in characters, not bytes.
682 * Returns: The number of characters deleted.
687 gtk_entry_buffer_delete_text (GtkEntryBuffer *buffer,
691 GtkEntryBufferClass *klass;
694 g_return_val_if_fail (GTK_IS_ENTRY_BUFFER (buffer), 0);
696 length = gtk_entry_buffer_get_length (buffer);
699 if (position > length)
701 if (position + n_chars > length)
702 n_chars = length - position;
704 klass = GTK_ENTRY_BUFFER_GET_CLASS (buffer);
705 g_return_val_if_fail (klass->delete_text != NULL, 0);
707 return (*klass->delete_text) (buffer, position, n_chars);
711 * gtk_entry_buffer_emit_inserted_text:
712 * @buffer: a #GtkEntryBuffer
713 * @position: position at which text was inserted
714 * @chars: text that was inserted
715 * @n_chars: number of characters inserted
717 * Used when subclassing #GtkEntryBuffer
722 gtk_entry_buffer_emit_inserted_text (GtkEntryBuffer *buffer,
727 g_return_if_fail (GTK_IS_ENTRY_BUFFER (buffer));
728 g_signal_emit (buffer, signals[INSERTED_TEXT], 0, position, chars, n_chars);
732 * gtk_entry_buffer_emit_deleted_text:
733 * @buffer: a #GtkEntryBuffer
734 * @position: position at which text was deleted
735 * @n_chars: number of characters deleted
737 * Used when subclassing #GtkEntryBuffer
742 gtk_entry_buffer_emit_deleted_text (GtkEntryBuffer *buffer,
746 g_return_if_fail (GTK_IS_ENTRY_BUFFER (buffer));
747 g_signal_emit (buffer, signals[DELETED_TEXT], 0, position, n_chars);