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"
34 * SECTION:gtkentrybuffer
35 * @title: GtkEntryBuffer
36 * @short_description: Text buffer for GtkEntry
38 * The #GtkEntryBuffer class contains the actual text displayed in a
41 * A single #GtkEntryBuffer object can be shared by multiple #GtkEntry
42 * widgets which will then share the same text content, but not the cursor
43 * position, visibility attributes, icon etc.
45 * #GtkEntryBuffer may be derived from. Such a derived class might allow
46 * text to be stored in an alternate location, such as non-pageable memory,
47 * useful in the case of important passwords. Or a derived class could
48 * integrate with an application's concept of undo/redo.
53 /* Initial size of buffer, in bytes */
69 static guint signals[LAST_SIGNAL] = { 0 };
71 struct _GtkEntryBufferPrivate
75 /* Only valid if this class is not derived */
77 gsize normal_text_size;
78 gsize normal_text_bytes;
79 guint normal_text_chars;
82 G_DEFINE_TYPE (GtkEntryBuffer, gtk_entry_buffer, G_TYPE_OBJECT);
84 /* --------------------------------------------------------------------------------
85 * DEFAULT IMPLEMENTATIONS OF TEXT BUFFER
87 * These may be overridden by a derived class, behavior may be changed etc...
88 * The normal_text and normal_text_xxxx fields may not be valid when
89 * this class is derived from.
92 /* Overwrite a memory that might contain sensitive information. */
94 trash_area (gchar *area,
97 volatile gchar *varea = (volatile gchar *)area;
103 gtk_entry_buffer_normal_get_text (GtkEntryBuffer *buffer,
107 *n_bytes = buffer->priv->normal_text_bytes;
108 if (!buffer->priv->normal_text)
110 return buffer->priv->normal_text;
114 gtk_entry_buffer_normal_get_length (GtkEntryBuffer *buffer)
116 return buffer->priv->normal_text_chars;
120 gtk_entry_buffer_normal_insert_text (GtkEntryBuffer *buffer,
125 GtkEntryBufferPrivate *pv = buffer->priv;
130 n_bytes = g_utf8_offset_to_pointer (chars, n_chars) - chars;
132 /* Need more memory */
133 if (n_bytes + pv->normal_text_bytes + 1 > pv->normal_text_size)
137 prev_size = pv->normal_text_size;
139 /* Calculate our new buffer size */
140 while (n_bytes + pv->normal_text_bytes + 1 > pv->normal_text_size)
142 if (pv->normal_text_size == 0)
143 pv->normal_text_size = MIN_SIZE;
146 if (2 * pv->normal_text_size < GTK_ENTRY_BUFFER_MAX_SIZE)
147 pv->normal_text_size *= 2;
150 pv->normal_text_size = GTK_ENTRY_BUFFER_MAX_SIZE;
151 if (n_bytes > pv->normal_text_size - pv->normal_text_bytes - 1)
153 n_bytes = pv->normal_text_size - pv->normal_text_bytes - 1;
154 n_bytes = g_utf8_find_prev_char (chars, chars + n_bytes + 1) - chars;
155 n_chars = g_utf8_strlen (chars, n_bytes);
162 /* Could be a password, so can't leave stuff in memory. */
163 et_new = g_malloc (pv->normal_text_size);
164 memcpy (et_new, pv->normal_text, MIN (prev_size, pv->normal_text_size));
165 trash_area (pv->normal_text, prev_size);
166 g_free (pv->normal_text);
167 pv->normal_text = et_new;
170 /* Actual text insertion */
171 at = g_utf8_offset_to_pointer (pv->normal_text, position) - pv->normal_text;
172 g_memmove (pv->normal_text + at + n_bytes, pv->normal_text + at, pv->normal_text_bytes - at);
173 memcpy (pv->normal_text + at, chars, n_bytes);
176 pv->normal_text_bytes += n_bytes;
177 pv->normal_text_chars += n_chars;
178 pv->normal_text[pv->normal_text_bytes] = '\0';
180 gtk_entry_buffer_emit_inserted_text (buffer, position, chars, n_chars);
185 gtk_entry_buffer_normal_delete_text (GtkEntryBuffer *buffer,
189 GtkEntryBufferPrivate *pv = buffer->priv;
192 if (position > pv->normal_text_chars)
193 position = pv->normal_text_chars;
194 if (position + n_chars > pv->normal_text_chars)
195 n_chars = pv->normal_text_chars - position;
199 start = g_utf8_offset_to_pointer (pv->normal_text, position) - pv->normal_text;
200 end = g_utf8_offset_to_pointer (pv->normal_text, position + n_chars) - pv->normal_text;
202 g_memmove (pv->normal_text + start, pv->normal_text + end, pv->normal_text_bytes + 1 - end);
203 pv->normal_text_chars -= n_chars;
204 pv->normal_text_bytes -= (end - start);
207 * Could be a password, make sure we don't leave anything sensitive after
208 * the terminating zero. Note, that the terminating zero already trashed
211 trash_area (pv->normal_text + pv->normal_text_bytes + 1, end - start - 1);
213 gtk_entry_buffer_emit_deleted_text (buffer, position, n_chars);
219 /* --------------------------------------------------------------------------------
224 gtk_entry_buffer_real_inserted_text (GtkEntryBuffer *buffer,
229 g_object_notify (G_OBJECT (buffer), "text");
230 g_object_notify (G_OBJECT (buffer), "length");
234 gtk_entry_buffer_real_deleted_text (GtkEntryBuffer *buffer,
238 g_object_notify (G_OBJECT (buffer), "text");
239 g_object_notify (G_OBJECT (buffer), "length");
242 /* --------------------------------------------------------------------------------
247 gtk_entry_buffer_init (GtkEntryBuffer *buffer)
249 GtkEntryBufferPrivate *pv;
251 pv = buffer->priv = G_TYPE_INSTANCE_GET_PRIVATE (buffer, GTK_TYPE_ENTRY_BUFFER, GtkEntryBufferPrivate);
253 pv->normal_text = NULL;
254 pv->normal_text_chars = 0;
255 pv->normal_text_bytes = 0;
256 pv->normal_text_size = 0;
260 gtk_entry_buffer_finalize (GObject *obj)
262 GtkEntryBuffer *buffer = GTK_ENTRY_BUFFER (obj);
263 GtkEntryBufferPrivate *pv = buffer->priv;
267 trash_area (pv->normal_text, pv->normal_text_size);
268 g_free (pv->normal_text);
269 pv->normal_text = NULL;
270 pv->normal_text_bytes = pv->normal_text_size = 0;
271 pv->normal_text_chars = 0;
274 G_OBJECT_CLASS (gtk_entry_buffer_parent_class)->finalize (obj);
278 gtk_entry_buffer_set_property (GObject *obj,
283 GtkEntryBuffer *buffer = GTK_ENTRY_BUFFER (obj);
288 gtk_entry_buffer_set_text (buffer, g_value_get_string (value), -1);
290 case PROP_MAX_LENGTH:
291 gtk_entry_buffer_set_max_length (buffer, g_value_get_int (value));
294 G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, prop_id, pspec);
300 gtk_entry_buffer_get_property (GObject *obj,
305 GtkEntryBuffer *buffer = GTK_ENTRY_BUFFER (obj);
310 g_value_set_string (value, gtk_entry_buffer_get_text (buffer));
313 g_value_set_uint (value, gtk_entry_buffer_get_length (buffer));
315 case PROP_MAX_LENGTH:
316 g_value_set_int (value, gtk_entry_buffer_get_max_length (buffer));
319 G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, prop_id, pspec);
325 gtk_entry_buffer_class_init (GtkEntryBufferClass *klass)
327 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
329 gobject_class->finalize = gtk_entry_buffer_finalize;
330 gobject_class->set_property = gtk_entry_buffer_set_property;
331 gobject_class->get_property = gtk_entry_buffer_get_property;
333 klass->get_text = gtk_entry_buffer_normal_get_text;
334 klass->get_length = gtk_entry_buffer_normal_get_length;
335 klass->insert_text = gtk_entry_buffer_normal_insert_text;
336 klass->delete_text = gtk_entry_buffer_normal_delete_text;
338 klass->inserted_text = gtk_entry_buffer_real_inserted_text;
339 klass->deleted_text = gtk_entry_buffer_real_deleted_text;
341 g_type_class_add_private (gobject_class, sizeof (GtkEntryBufferPrivate));
344 * GtkEntryBuffer:text:
346 * The contents of the buffer.
350 g_object_class_install_property (gobject_class, PROP_TEXT,
351 g_param_spec_string ("text", P_("Text"),
352 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, PROP_LENGTH,
363 g_param_spec_uint ("length", P_("Text length"),
364 P_("Length of the text currently in the buffer"),
365 0, GTK_ENTRY_BUFFER_MAX_SIZE, 0, GTK_PARAM_READABLE));
368 * GtkEntryBuffer:max-length:
370 * The maximum length (in characters) of the text in the buffer.
374 g_object_class_install_property (gobject_class, PROP_MAX_LENGTH,
375 g_param_spec_uint ("max-length", P_("Maximum length"),
376 P_("Maximum number of characters for this entry. Zero if no maximum"),
377 0, GTK_ENTRY_BUFFER_MAX_SIZE, 0, GTK_PARAM_READWRITE));
380 * GtkEntry::inserted-text:
381 * @buffer: a #GtkEntryBuffer
382 * @position: the position the text was inserted at.
383 * @chars: The text that was inserted.
384 * @n_chars: The number of characters that were inserted.
386 * This signal is emitted after text is inserted into the buffer.
390 signals[INSERTED_TEXT] = g_signal_new (I_("inserted-text"),
391 GTK_TYPE_ENTRY_BUFFER,
393 G_STRUCT_OFFSET (GtkEntryBufferClass, inserted_text),
395 _gtk_marshal_VOID__UINT_STRING_UINT,
402 * GtkEntry::deleted-text:
403 * @buffer: a #GtkEntryBuffer
404 * @position: the position the text was deleted at.
405 * @n_chars: The number of characters that were deleted.
407 * This signal is emitted after text is deleted from the buffer.
411 signals[DELETED_TEXT] = g_signal_new (I_("deleted-text"),
412 GTK_TYPE_ENTRY_BUFFER,
414 G_STRUCT_OFFSET (GtkEntryBufferClass, deleted_text),
416 _gtk_marshal_VOID__UINT_UINT,
422 /* --------------------------------------------------------------------------------
427 * gtk_entry_buffer_new:
428 * @initial_chars: initial buffer text, or %NULL
429 * @n_initial_chars: number of characters in @initial_chars, or -1
431 * Create a new GtkEntryBuffer object.
433 * Optionally, specify initial text to set in the buffer.
435 * Return value: A new GtkEntryBuffer object.
440 gtk_entry_buffer_new (const gchar *initial_chars,
441 gint n_initial_chars)
443 GtkEntryBuffer *buffer = g_object_new (GTK_TYPE_ENTRY_BUFFER, NULL);
445 gtk_entry_buffer_set_text (buffer, initial_chars, n_initial_chars);
450 * gtk_entry_buffer_get_length:
451 * @buffer: a #GtkEntryBuffer
453 * Retrieves the length in characters of the buffer.
455 * Return value: The number of characters in the buffer.
460 gtk_entry_buffer_get_length (GtkEntryBuffer *buffer)
462 GtkEntryBufferClass *klass;
464 g_return_val_if_fail (GTK_IS_ENTRY_BUFFER (buffer), 0);
466 klass = GTK_ENTRY_BUFFER_GET_CLASS (buffer);
467 g_return_val_if_fail (klass->get_length != NULL, 0);
469 return (*klass->get_length) (buffer);
473 * gtk_entry_buffer_get_bytes:
474 * @buffer: a #GtkEntryBuffer
476 * Retrieves the length in bytes of the buffer.
477 * See gtk_entry_buffer_get_length().
479 * Return value: The byte length of the buffer.
484 gtk_entry_buffer_get_bytes (GtkEntryBuffer *buffer)
486 GtkEntryBufferClass *klass;
489 g_return_val_if_fail (GTK_IS_ENTRY_BUFFER (buffer), 0);
491 klass = GTK_ENTRY_BUFFER_GET_CLASS (buffer);
492 g_return_val_if_fail (klass->get_text != NULL, 0);
494 (*klass->get_text) (buffer, &bytes);
499 * gtk_entry_buffer_get_text:
500 * @buffer: a #GtkEntryBuffer
502 * Retrieves the contents of the buffer.
504 * The memory pointer returned by this call will not change
505 * unless this object emits a signal, or is finalized.
507 * Return value: a pointer to the contents of the widget as a
508 * string. This string points to internally allocated
509 * storage in the buffer and must not be freed, modified or
514 G_CONST_RETURN gchar*
515 gtk_entry_buffer_get_text (GtkEntryBuffer *buffer)
517 GtkEntryBufferClass *klass;
519 g_return_val_if_fail (GTK_IS_ENTRY_BUFFER (buffer), NULL);
521 klass = GTK_ENTRY_BUFFER_GET_CLASS (buffer);
522 g_return_val_if_fail (klass->get_text != NULL, NULL);
524 return (*klass->get_text) (buffer, NULL);
528 * gtk_entry_buffer_set_text:
529 * @buffer: a #GtkEntryBuffer
530 * @chars: the new text
531 * @n_chars: the number of characters in @text, or -1
533 * Sets the text in the buffer.
535 * This is roughly equivalent to calling gtk_entry_buffer_delete_text()
536 * and gtk_entry_buffer_insert_text().
538 * Note that @n_chars is in characters, not in bytes.
543 gtk_entry_buffer_set_text (GtkEntryBuffer *buffer,
547 g_return_if_fail (GTK_IS_ENTRY_BUFFER (buffer));
548 g_return_if_fail (chars != NULL);
550 g_object_freeze_notify (G_OBJECT (buffer));
551 gtk_entry_buffer_delete_text (buffer, 0, -1);
552 gtk_entry_buffer_insert_text (buffer, 0, chars, n_chars);
553 g_object_thaw_notify (G_OBJECT (buffer));
557 * gtk_entry_buffer_set_max_length:
558 * @buffer: a #GtkEntryBuffer
559 * @max_length: the maximum length of the entry buffer, or 0 for no maximum.
560 * (other than the maximum length of entries.) The value passed in will
561 * be clamped to the range 0-65536.
563 * Sets the maximum allowed length of the contents of the buffer. If
564 * the current contents are longer than the given length, then they
565 * will be truncated to fit.
570 gtk_entry_buffer_set_max_length (GtkEntryBuffer *buffer,
573 g_return_if_fail (GTK_IS_ENTRY_BUFFER (buffer));
575 max_length = CLAMP (max_length, 0, GTK_ENTRY_BUFFER_MAX_SIZE);
577 if (max_length > 0 && gtk_entry_buffer_get_length (buffer) > max_length)
578 gtk_entry_buffer_delete_text (buffer, max_length, -1);
580 buffer->priv->max_length = max_length;
581 g_object_notify (G_OBJECT (buffer), "max-length");
585 * gtk_entry_buffer_get_max_length:
586 * @buffer: a #GtkEntryBuffer
588 * Retrieves the maximum allowed length of the text in
589 * @buffer. See gtk_entry_buffer_set_max_length().
591 * Return value: the maximum allowed number of characters
592 * in #GtkEntryBuffer, or 0 if there is no maximum.
597 gtk_entry_buffer_get_max_length (GtkEntryBuffer *buffer)
599 g_return_val_if_fail (GTK_IS_ENTRY_BUFFER (buffer), 0);
600 return buffer->priv->max_length;
604 * gtk_entry_buffer_insert_text:
605 * @buffer: a #GtkEntryBuffer
606 * @position: the position at which to insert text.
607 * @chars: the text to insert into the buffer.
608 * @n_chars: the length of the text in characters, or -1
610 * Inserts @n_chars characters of @chars into the contents of the
611 * buffer, at position @position.
613 * If @n_chars is negative, then characters from chars will be inserted
614 * until a null-terminator is found. If @position or @n_chars are out of
615 * bounds, or the maximum buffer text length is exceeded, then they are
616 * coerced to sane values.
618 * Note that the position and length are in characters, not in bytes.
620 * Returns: The number of characters actually inserted.
625 gtk_entry_buffer_insert_text (GtkEntryBuffer *buffer,
630 GtkEntryBufferClass *klass;
631 GtkEntryBufferPrivate *pv;
634 g_return_val_if_fail (GTK_IS_ENTRY_BUFFER (buffer), 0);
636 length = gtk_entry_buffer_get_length (buffer);
640 n_chars = g_utf8_strlen (chars, -1);
642 /* Bring position into bounds */
643 if (position > length)
646 /* Make sure not entering too much data */
647 if (pv->max_length > 0)
649 if (length >= pv->max_length)
651 else if (length + n_chars > pv->max_length)
652 n_chars -= (length + n_chars) - pv->max_length;
655 klass = GTK_ENTRY_BUFFER_GET_CLASS (buffer);
656 g_return_val_if_fail (klass->insert_text != NULL, 0);
658 return (*klass->insert_text) (buffer, position, chars, n_chars);
662 * gtk_entry_buffer_delete_text:
663 * @buffer: a #GtkEntryBuffer
664 * @position: position at which to delete text
665 * @n_chars: number of characters to delete
667 * Deletes a sequence of characters from the buffer. @n_chars characters are
668 * deleted starting at @position. If @n_chars is negative, then all characters
669 * until the end of the text are deleted.
671 * If @position or @n_chars are out of bounds, then they are coerced to sane
674 * Note that the positions are specified in characters, not bytes.
676 * Returns: The number of characters deleted.
681 gtk_entry_buffer_delete_text (GtkEntryBuffer *buffer,
685 GtkEntryBufferClass *klass;
688 g_return_val_if_fail (GTK_IS_ENTRY_BUFFER (buffer), 0);
690 length = gtk_entry_buffer_get_length (buffer);
693 if (position > length)
695 if (position + n_chars > length)
696 n_chars = length - position;
698 klass = GTK_ENTRY_BUFFER_GET_CLASS (buffer);
699 g_return_val_if_fail (klass->delete_text != NULL, 0);
701 return (*klass->delete_text) (buffer, position, n_chars);
705 * gtk_entry_buffer_emit_inserted_text:
706 * @buffer: a #GtkEntryBuffer
707 * @position: position at which text was inserted
708 * @chars: text that was inserted
709 * @n_chars: number of characters inserted
711 * Used when subclassing #GtkEntryBuffer
716 gtk_entry_buffer_emit_inserted_text (GtkEntryBuffer *buffer,
721 g_return_if_fail (GTK_IS_ENTRY_BUFFER (buffer));
722 g_signal_emit (buffer, signals[INSERTED_TEXT], 0, position, chars, n_chars);
726 * gtk_entry_buffer_emit_deleted_text:
727 * @buffer: a #GtkEntryBuffer
728 * @position: position at which text was deleted
729 * @n_chars: number of characters deleted
731 * Used when subclassing #GtkEntryBuffer
736 gtk_entry_buffer_emit_deleted_text (GtkEntryBuffer *buffer,
740 g_return_if_fail (GTK_IS_ENTRY_BUFFER (buffer));
741 g_signal_emit (buffer, signals[DELETED_TEXT], 0, position, n_chars);
744 #define __GTK_ENTRY_BUFFER_C__
745 #include "gtkaliasdef.c"