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,
352 g_param_spec_string ("text",
354 P_("The contents of the buffer"),
356 GTK_PARAM_READWRITE));
359 * GtkEntryBuffer:length:
361 * The length (in characters) of the text in buffer.
365 g_object_class_install_property (gobject_class,
367 g_param_spec_uint ("length",
369 P_("Length of the text currently in the buffer"),
370 0, GTK_ENTRY_BUFFER_MAX_SIZE, 0,
371 GTK_PARAM_READABLE));
374 * GtkEntryBuffer:max-length:
376 * The maximum length (in characters) of the text in the buffer.
380 g_object_class_install_property (gobject_class,
382 g_param_spec_int ("max-length",
383 P_("Maximum length"),
384 P_("Maximum number of characters for this entry. Zero if no maximum"),
385 0, GTK_ENTRY_BUFFER_MAX_SIZE, 0,
386 GTK_PARAM_READWRITE));
389 * GtkEntry::inserted-text:
390 * @buffer: a #GtkEntryBuffer
391 * @position: the position the text was inserted at.
392 * @chars: The text that was inserted.
393 * @n_chars: The number of characters that were inserted.
395 * This signal is emitted after text is inserted into the buffer.
399 signals[INSERTED_TEXT] = g_signal_new (I_("inserted-text"),
400 GTK_TYPE_ENTRY_BUFFER,
402 G_STRUCT_OFFSET (GtkEntryBufferClass, inserted_text),
404 _gtk_marshal_VOID__UINT_STRING_UINT,
411 * GtkEntry::deleted-text:
412 * @buffer: a #GtkEntryBuffer
413 * @position: the position the text was deleted at.
414 * @n_chars: The number of characters that were deleted.
416 * This signal is emitted after text is deleted from the buffer.
420 signals[DELETED_TEXT] = g_signal_new (I_("deleted-text"),
421 GTK_TYPE_ENTRY_BUFFER,
423 G_STRUCT_OFFSET (GtkEntryBufferClass, deleted_text),
425 _gtk_marshal_VOID__UINT_UINT,
431 /* --------------------------------------------------------------------------------
436 * gtk_entry_buffer_new:
437 * @initial_chars: initial buffer text, or %NULL
438 * @n_initial_chars: number of characters in @initial_chars, or -1
440 * Create a new GtkEntryBuffer object.
442 * Optionally, specify initial text to set in the buffer.
444 * Return value: A new GtkEntryBuffer object.
449 gtk_entry_buffer_new (const gchar *initial_chars,
450 gint n_initial_chars)
452 GtkEntryBuffer *buffer = g_object_new (GTK_TYPE_ENTRY_BUFFER, NULL);
454 gtk_entry_buffer_set_text (buffer, initial_chars, n_initial_chars);
459 * gtk_entry_buffer_get_length:
460 * @buffer: a #GtkEntryBuffer
462 * Retrieves the length in characters of the buffer.
464 * Return value: The number of characters in the buffer.
469 gtk_entry_buffer_get_length (GtkEntryBuffer *buffer)
471 GtkEntryBufferClass *klass;
473 g_return_val_if_fail (GTK_IS_ENTRY_BUFFER (buffer), 0);
475 klass = GTK_ENTRY_BUFFER_GET_CLASS (buffer);
476 g_return_val_if_fail (klass->get_length != NULL, 0);
478 return (*klass->get_length) (buffer);
482 * gtk_entry_buffer_get_bytes:
483 * @buffer: a #GtkEntryBuffer
485 * Retrieves the length in bytes of the buffer.
486 * See gtk_entry_buffer_get_length().
488 * Return value: The byte length of the buffer.
493 gtk_entry_buffer_get_bytes (GtkEntryBuffer *buffer)
495 GtkEntryBufferClass *klass;
498 g_return_val_if_fail (GTK_IS_ENTRY_BUFFER (buffer), 0);
500 klass = GTK_ENTRY_BUFFER_GET_CLASS (buffer);
501 g_return_val_if_fail (klass->get_text != NULL, 0);
503 (*klass->get_text) (buffer, &bytes);
508 * gtk_entry_buffer_get_text:
509 * @buffer: a #GtkEntryBuffer
511 * Retrieves the contents of the buffer.
513 * The memory pointer returned by this call will not change
514 * unless this object emits a signal, or is finalized.
516 * Return value: a pointer to the contents of the widget as a
517 * string. This string points to internally allocated
518 * storage in the buffer and must not be freed, modified or
523 G_CONST_RETURN gchar*
524 gtk_entry_buffer_get_text (GtkEntryBuffer *buffer)
526 GtkEntryBufferClass *klass;
528 g_return_val_if_fail (GTK_IS_ENTRY_BUFFER (buffer), NULL);
530 klass = GTK_ENTRY_BUFFER_GET_CLASS (buffer);
531 g_return_val_if_fail (klass->get_text != NULL, NULL);
533 return (*klass->get_text) (buffer, NULL);
537 * gtk_entry_buffer_set_text:
538 * @buffer: a #GtkEntryBuffer
539 * @chars: the new text
540 * @n_chars: the number of characters in @text, or -1
542 * Sets the text in the buffer.
544 * This is roughly equivalent to calling gtk_entry_buffer_delete_text()
545 * and gtk_entry_buffer_insert_text().
547 * Note that @n_chars is in characters, not in bytes.
552 gtk_entry_buffer_set_text (GtkEntryBuffer *buffer,
556 g_return_if_fail (GTK_IS_ENTRY_BUFFER (buffer));
557 g_return_if_fail (chars != NULL);
559 g_object_freeze_notify (G_OBJECT (buffer));
560 gtk_entry_buffer_delete_text (buffer, 0, -1);
561 gtk_entry_buffer_insert_text (buffer, 0, chars, n_chars);
562 g_object_thaw_notify (G_OBJECT (buffer));
566 * gtk_entry_buffer_set_max_length:
567 * @buffer: a #GtkEntryBuffer
568 * @max_length: the maximum length of the entry buffer, or 0 for no maximum.
569 * (other than the maximum length of entries.) The value passed in will
570 * be clamped to the range 0-65536.
572 * Sets the maximum allowed length of the contents of the buffer. If
573 * the current contents are longer than the given length, then they
574 * will be truncated to fit.
579 gtk_entry_buffer_set_max_length (GtkEntryBuffer *buffer,
582 g_return_if_fail (GTK_IS_ENTRY_BUFFER (buffer));
584 max_length = CLAMP (max_length, 0, GTK_ENTRY_BUFFER_MAX_SIZE);
586 if (max_length > 0 && gtk_entry_buffer_get_length (buffer) > max_length)
587 gtk_entry_buffer_delete_text (buffer, max_length, -1);
589 buffer->priv->max_length = max_length;
590 g_object_notify (G_OBJECT (buffer), "max-length");
594 * gtk_entry_buffer_get_max_length:
595 * @buffer: a #GtkEntryBuffer
597 * Retrieves the maximum allowed length of the text in
598 * @buffer. See gtk_entry_buffer_set_max_length().
600 * Return value: the maximum allowed number of characters
601 * in #GtkEntryBuffer, or 0 if there is no maximum.
606 gtk_entry_buffer_get_max_length (GtkEntryBuffer *buffer)
608 g_return_val_if_fail (GTK_IS_ENTRY_BUFFER (buffer), 0);
609 return buffer->priv->max_length;
613 * gtk_entry_buffer_insert_text:
614 * @buffer: a #GtkEntryBuffer
615 * @position: the position at which to insert text.
616 * @chars: the text to insert into the buffer.
617 * @n_chars: the length of the text in characters, or -1
619 * Inserts @n_chars characters of @chars into the contents of the
620 * buffer, at position @position.
622 * If @n_chars is negative, then characters from chars will be inserted
623 * until a null-terminator is found. If @position or @n_chars are out of
624 * bounds, or the maximum buffer text length is exceeded, then they are
625 * coerced to sane values.
627 * Note that the position and length are in characters, not in bytes.
629 * Returns: The number of characters actually inserted.
634 gtk_entry_buffer_insert_text (GtkEntryBuffer *buffer,
639 GtkEntryBufferClass *klass;
640 GtkEntryBufferPrivate *pv;
643 g_return_val_if_fail (GTK_IS_ENTRY_BUFFER (buffer), 0);
645 length = gtk_entry_buffer_get_length (buffer);
649 n_chars = g_utf8_strlen (chars, -1);
651 /* Bring position into bounds */
652 if (position > length)
655 /* Make sure not entering too much data */
656 if (pv->max_length > 0)
658 if (length >= pv->max_length)
660 else if (length + n_chars > pv->max_length)
661 n_chars -= (length + n_chars) - pv->max_length;
664 klass = GTK_ENTRY_BUFFER_GET_CLASS (buffer);
665 g_return_val_if_fail (klass->insert_text != NULL, 0);
667 return (*klass->insert_text) (buffer, position, chars, n_chars);
671 * gtk_entry_buffer_delete_text:
672 * @buffer: a #GtkEntryBuffer
673 * @position: position at which to delete text
674 * @n_chars: number of characters to delete
676 * Deletes a sequence of characters from the buffer. @n_chars characters are
677 * deleted starting at @position. If @n_chars is negative, then all characters
678 * until the end of the text are deleted.
680 * If @position or @n_chars are out of bounds, then they are coerced to sane
683 * Note that the positions are specified in characters, not bytes.
685 * Returns: The number of characters deleted.
690 gtk_entry_buffer_delete_text (GtkEntryBuffer *buffer,
694 GtkEntryBufferClass *klass;
697 g_return_val_if_fail (GTK_IS_ENTRY_BUFFER (buffer), 0);
699 length = gtk_entry_buffer_get_length (buffer);
702 if (position > length)
704 if (position + n_chars > length)
705 n_chars = length - position;
707 klass = GTK_ENTRY_BUFFER_GET_CLASS (buffer);
708 g_return_val_if_fail (klass->delete_text != NULL, 0);
710 return (*klass->delete_text) (buffer, position, n_chars);
714 * gtk_entry_buffer_emit_inserted_text:
715 * @buffer: a #GtkEntryBuffer
716 * @position: position at which text was inserted
717 * @chars: text that was inserted
718 * @n_chars: number of characters inserted
720 * Used when subclassing #GtkEntryBuffer
725 gtk_entry_buffer_emit_inserted_text (GtkEntryBuffer *buffer,
730 g_return_if_fail (GTK_IS_ENTRY_BUFFER (buffer));
731 g_signal_emit (buffer, signals[INSERTED_TEXT], 0, position, chars, n_chars);
735 * gtk_entry_buffer_emit_deleted_text:
736 * @buffer: a #GtkEntryBuffer
737 * @position: position at which text was deleted
738 * @n_chars: number of characters deleted
740 * Used when subclassing #GtkEntryBuffer
745 gtk_entry_buffer_emit_deleted_text (GtkEntryBuffer *buffer,
749 g_return_if_fail (GTK_IS_ENTRY_BUFFER (buffer));
750 g_signal_emit (buffer, signals[DELETED_TEXT], 0, position, n_chars);
753 #define __GTK_ENTRY_BUFFER_C__
754 #include "gtkaliasdef.c"