1 /* clutter-text-buffer.c
2 * Copyright (C) 2011 Collabora Ltd.
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.
19 * Author: Stef Walter <stefw@collabora.co.uk>
26 #include "clutter-text-buffer.h"
27 #include "clutter-marshal.h"
28 #include "clutter-private.h"
33 * SECTION:clutter-text-buffer
34 * @title: ClutterTextBuffer
35 * @short_description: Text buffer for ClutterText
37 * The #ClutterTextBuffer class contains the actual text displayed in a
38 * #ClutterText widget.
40 * A single #ClutterTextBuffer object can be shared by multiple #ClutterText
41 * widgets which will then share the same text content, but not the cursor
42 * position, visibility attributes, icon etc.
44 * #ClutterTextBuffer 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 */
63 static GParamSpec *obj_props[PROP_LAST] = { NULL, };
71 static guint signals[LAST_SIGNAL] = { 0 };
73 struct _ClutterTextBufferPrivate
77 /* Only valid if this class is not derived */
79 gsize normal_text_size;
80 gsize normal_text_bytes;
81 guint normal_text_chars;
84 G_DEFINE_TYPE (ClutterTextBuffer, clutter_text_buffer, G_TYPE_OBJECT);
86 /* --------------------------------------------------------------------------------
87 * DEFAULT IMPLEMENTATIONS OF TEXT BUFFER
89 * These may be overridden by a derived class, behavior may be changed etc...
90 * The normal_text and normal_text_xxxx fields may not be valid when
91 * this class is derived from.
94 /* Overwrite a memory that might contain sensitive information. */
96 trash_area (gchar *area,
99 volatile gchar *varea = (volatile gchar *)area;
105 clutter_text_buffer_normal_get_text (ClutterTextBuffer *buffer,
109 *n_bytes = buffer->priv->normal_text_bytes;
110 if (!buffer->priv->normal_text)
112 return buffer->priv->normal_text;
116 clutter_text_buffer_normal_get_length (ClutterTextBuffer *buffer)
118 return buffer->priv->normal_text_chars;
122 clutter_text_buffer_normal_insert_text (ClutterTextBuffer *buffer,
127 ClutterTextBufferPrivate *pv = buffer->priv;
132 n_bytes = g_utf8_offset_to_pointer (chars, n_chars) - chars;
134 /* Need more memory */
135 if (n_bytes + pv->normal_text_bytes + 1 > pv->normal_text_size)
139 prev_size = pv->normal_text_size;
141 /* Calculate our new buffer size */
142 while (n_bytes + pv->normal_text_bytes + 1 > pv->normal_text_size)
144 if (pv->normal_text_size == 0)
145 pv->normal_text_size = MIN_SIZE;
148 if (2 * pv->normal_text_size < CLUTTER_TEXT_BUFFER_MAX_SIZE)
149 pv->normal_text_size *= 2;
152 pv->normal_text_size = CLUTTER_TEXT_BUFFER_MAX_SIZE;
153 if (n_bytes > pv->normal_text_size - pv->normal_text_bytes - 1)
155 n_bytes = pv->normal_text_size - pv->normal_text_bytes - 1;
156 n_bytes = g_utf8_find_prev_char (chars, chars + n_bytes + 1) - chars;
157 n_chars = g_utf8_strlen (chars, n_bytes);
164 /* Could be a password, so can't leave stuff in memory. */
165 et_new = g_malloc (pv->normal_text_size);
166 memcpy (et_new, pv->normal_text, MIN (prev_size, pv->normal_text_size));
167 trash_area (pv->normal_text, prev_size);
168 g_free (pv->normal_text);
169 pv->normal_text = et_new;
172 /* Actual text insertion */
173 at = g_utf8_offset_to_pointer (pv->normal_text, position) - pv->normal_text;
174 g_memmove (pv->normal_text + at + n_bytes, pv->normal_text + at, pv->normal_text_bytes - at);
175 memcpy (pv->normal_text + at, chars, n_bytes);
178 pv->normal_text_bytes += n_bytes;
179 pv->normal_text_chars += n_chars;
180 pv->normal_text[pv->normal_text_bytes] = '\0';
182 clutter_text_buffer_emit_inserted_text (buffer, position, chars, n_chars);
187 clutter_text_buffer_normal_delete_text (ClutterTextBuffer *buffer,
191 ClutterTextBufferPrivate *pv = buffer->priv;
194 if (position > pv->normal_text_chars)
195 position = pv->normal_text_chars;
196 if (position + n_chars > pv->normal_text_chars)
197 n_chars = pv->normal_text_chars - position;
201 start = g_utf8_offset_to_pointer (pv->normal_text, position) - pv->normal_text;
202 end = g_utf8_offset_to_pointer (pv->normal_text, position + n_chars) - pv->normal_text;
204 g_memmove (pv->normal_text + start, pv->normal_text + end, pv->normal_text_bytes + 1 - end);
205 pv->normal_text_chars -= n_chars;
206 pv->normal_text_bytes -= (end - start);
209 * Could be a password, make sure we don't leave anything sensitive after
210 * the terminating zero. Note, that the terminating zero already trashed
213 trash_area (pv->normal_text + pv->normal_text_bytes + 1, end - start - 1);
215 clutter_text_buffer_emit_deleted_text (buffer, position, n_chars);
221 /* --------------------------------------------------------------------------------
226 clutter_text_buffer_real_inserted_text (ClutterTextBuffer *buffer,
231 g_object_notify (G_OBJECT (buffer), "text");
232 g_object_notify (G_OBJECT (buffer), "length");
236 clutter_text_buffer_real_deleted_text (ClutterTextBuffer *buffer,
240 g_object_notify (G_OBJECT (buffer), "text");
241 g_object_notify (G_OBJECT (buffer), "length");
244 /* --------------------------------------------------------------------------------
249 clutter_text_buffer_init (ClutterTextBuffer *buffer)
251 ClutterTextBufferPrivate *pv;
253 pv = buffer->priv = G_TYPE_INSTANCE_GET_PRIVATE (buffer, CLUTTER_TYPE_TEXT_BUFFER, ClutterTextBufferPrivate);
255 pv->normal_text = NULL;
256 pv->normal_text_chars = 0;
257 pv->normal_text_bytes = 0;
258 pv->normal_text_size = 0;
262 clutter_text_buffer_finalize (GObject *obj)
264 ClutterTextBuffer *buffer = CLUTTER_TEXT_BUFFER (obj);
265 ClutterTextBufferPrivate *pv = buffer->priv;
269 trash_area (pv->normal_text, pv->normal_text_size);
270 g_free (pv->normal_text);
271 pv->normal_text = NULL;
272 pv->normal_text_bytes = pv->normal_text_size = 0;
273 pv->normal_text_chars = 0;
276 G_OBJECT_CLASS (clutter_text_buffer_parent_class)->finalize (obj);
280 clutter_text_buffer_set_property (GObject *obj,
285 ClutterTextBuffer *buffer = CLUTTER_TEXT_BUFFER (obj);
289 case PROP_MAX_LENGTH:
290 clutter_text_buffer_set_max_length (buffer, g_value_get_int (value));
293 G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, prop_id, pspec);
299 clutter_text_buffer_get_property (GObject *obj,
304 ClutterTextBuffer *buffer = CLUTTER_TEXT_BUFFER (obj);
309 g_value_set_string (value, clutter_text_buffer_get_text (buffer));
312 g_value_set_uint (value, clutter_text_buffer_get_length (buffer));
314 case PROP_MAX_LENGTH:
315 g_value_set_int (value, clutter_text_buffer_get_max_length (buffer));
318 G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, prop_id, pspec);
324 clutter_text_buffer_class_init (ClutterTextBufferClass *klass)
326 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
328 gobject_class->finalize = clutter_text_buffer_finalize;
329 gobject_class->set_property = clutter_text_buffer_set_property;
330 gobject_class->get_property = clutter_text_buffer_get_property;
332 klass->get_text = clutter_text_buffer_normal_get_text;
333 klass->get_length = clutter_text_buffer_normal_get_length;
334 klass->insert_text = clutter_text_buffer_normal_insert_text;
335 klass->delete_text = clutter_text_buffer_normal_delete_text;
337 klass->inserted_text = clutter_text_buffer_real_inserted_text;
338 klass->deleted_text = clutter_text_buffer_real_deleted_text;
340 g_type_class_add_private (gobject_class, sizeof (ClutterTextBufferPrivate));
343 * ClutterTextBuffer:text:
345 * The contents of the buffer.
349 obj_props[PROP_TEXT] =
350 g_param_spec_string ("text",
352 P_("The contents of the buffer"),
354 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
357 * ClutterTextBuffer:length:
359 * The length (in characters) of the text in buffer.
363 obj_props[PROP_LENGTH] =
364 g_param_spec_uint ("length",
366 P_("Length of the text currently in the buffer"),
367 0, CLUTTER_TEXT_BUFFER_MAX_SIZE, 0,
368 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
371 * ClutterTextBuffer:max-length:
373 * The maximum length (in characters) of the text in the buffer.
377 obj_props[PROP_MAX_LENGTH] =
378 g_param_spec_int ("max-length",
379 P_("Maximum length"),
380 P_("Maximum number of characters for this entry. Zero if no maximum"),
381 0, CLUTTER_TEXT_BUFFER_MAX_SIZE, 0,
382 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
384 g_object_class_install_properties (gobject_class, PROP_LAST, obj_props);
387 * ClutterTextBuffer::inserted-text:
388 * @buffer: a #ClutterTextBuffer
389 * @position: the position the text was inserted at.
390 * @chars: The text that was inserted.
391 * @n_chars: The number of characters that were inserted.
393 * This signal is emitted after text is inserted into the buffer.
397 signals[INSERTED_TEXT] =
398 g_signal_new (I_("inserted-text"),
399 CLUTTER_TYPE_TEXT_BUFFER,
401 G_STRUCT_OFFSET (ClutterTextBufferClass, inserted_text),
403 _clutter_marshal_VOID__UINT_STRING_UINT,
410 * ClutterTextBuffer::deleted-text:
411 * @buffer: a #ClutterTextBuffer
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] =
420 g_signal_new (I_("deleted-text"),
421 CLUTTER_TYPE_TEXT_BUFFER,
423 G_STRUCT_OFFSET (ClutterTextBufferClass, deleted_text),
425 _clutter_marshal_VOID__UINT_UINT,
431 /* --------------------------------------------------------------------------------
436 * clutter_text_buffer_new:
438 * Create a new ClutterTextBuffer object.
440 * Return value: A new ClutterTextBuffer object.
445 clutter_text_buffer_new (void)
447 return g_object_new (CLUTTER_TYPE_TEXT_BUFFER, NULL);
452 * clutter_text_buffer_new_with_text:
453 * @text: (allow-none): initial buffer text
454 * @text_len: initial buffer text length, or -1 for null-terminated.
456 * Create a new ClutterTextBuffer object with some text.
458 * Return value: A new ClutterTextBuffer object.
463 clutter_text_buffer_new_with_text (const gchar *text,
466 ClutterTextBuffer *buffer;
467 buffer = clutter_text_buffer_new ();
468 clutter_text_buffer_set_text (buffer, text, text_len);
474 * clutter_text_buffer_get_length:
475 * @buffer: a #ClutterTextBuffer
477 * Retrieves the length in characters of the buffer.
479 * Return value: The number of characters in the buffer.
484 clutter_text_buffer_get_length (ClutterTextBuffer *buffer)
486 ClutterTextBufferClass *klass;
488 g_return_val_if_fail (CLUTTER_IS_TEXT_BUFFER (buffer), 0);
490 klass = CLUTTER_TEXT_BUFFER_GET_CLASS (buffer);
491 g_return_val_if_fail (klass->get_length != NULL, 0);
493 return (*klass->get_length) (buffer);
497 * clutter_text_buffer_get_bytes:
498 * @buffer: a #ClutterTextBuffer
500 * Retrieves the length in bytes of the buffer.
501 * See clutter_text_buffer_get_length().
503 * Return value: The byte length of the buffer.
508 clutter_text_buffer_get_bytes (ClutterTextBuffer *buffer)
510 ClutterTextBufferClass *klass;
513 g_return_val_if_fail (CLUTTER_IS_TEXT_BUFFER (buffer), 0);
515 klass = CLUTTER_TEXT_BUFFER_GET_CLASS (buffer);
516 g_return_val_if_fail (klass->get_text != NULL, 0);
518 (*klass->get_text) (buffer, &bytes);
523 * clutter_text_buffer_get_text:
524 * @buffer: a #ClutterTextBuffer
526 * Retrieves the contents of the buffer.
528 * The memory pointer returned by this call will not change
529 * unless this object emits a signal, or is finalized.
531 * Return value: a pointer to the contents of the widget as a
532 * string. This string points to internally allocated
533 * storage in the buffer and must not be freed, modified or
539 clutter_text_buffer_get_text (ClutterTextBuffer *buffer)
541 ClutterTextBufferClass *klass;
543 g_return_val_if_fail (CLUTTER_IS_TEXT_BUFFER (buffer), NULL);
545 klass = CLUTTER_TEXT_BUFFER_GET_CLASS (buffer);
546 g_return_val_if_fail (klass->get_text != NULL, NULL);
548 return (*klass->get_text) (buffer, NULL);
552 * clutter_text_buffer_set_text:
553 * @buffer: a #ClutterTextBuffer
554 * @chars: the new text
555 * @n_chars: the number of characters in @text, or -1
557 * Sets the text in the buffer.
559 * This is roughly equivalent to calling clutter_text_buffer_delete_text()
560 * and clutter_text_buffer_insert_text().
562 * Note that @n_chars is in characters, not in bytes.
567 clutter_text_buffer_set_text (ClutterTextBuffer *buffer,
571 g_return_if_fail (CLUTTER_IS_TEXT_BUFFER (buffer));
572 g_return_if_fail (chars != NULL);
574 g_object_freeze_notify (G_OBJECT (buffer));
575 clutter_text_buffer_delete_text (buffer, 0, -1);
576 clutter_text_buffer_insert_text (buffer, 0, chars, n_chars);
577 g_object_thaw_notify (G_OBJECT (buffer));
581 * clutter_text_buffer_set_max_length:
582 * @buffer: a #ClutterTextBuffer
583 * @max_length: the maximum length of the entry buffer, or 0 for no maximum.
584 * (other than the maximum length of entries.) The value passed in will
585 * be clamped to the range [ 0, %CLUTTER_TEXT_BUFFER_MAX_SIZE ].
587 * Sets the maximum allowed length of the contents of the buffer. If
588 * the current contents are longer than the given length, then they
589 * will be truncated to fit.
594 clutter_text_buffer_set_max_length (ClutterTextBuffer *buffer,
597 g_return_if_fail (CLUTTER_IS_TEXT_BUFFER (buffer));
599 max_length = CLAMP (max_length, 0, CLUTTER_TEXT_BUFFER_MAX_SIZE);
601 if (max_length > 0 && clutter_text_buffer_get_length (buffer) > max_length)
602 clutter_text_buffer_delete_text (buffer, max_length, -1);
604 buffer->priv->max_length = max_length;
605 g_object_notify (G_OBJECT (buffer), "max-length");
609 * clutter_text_buffer_get_max_length:
610 * @buffer: a #ClutterTextBuffer
612 * Retrieves the maximum allowed length of the text in
613 * @buffer. See clutter_text_buffer_set_max_length().
615 * Return value: the maximum allowed number of characters
616 * in #ClutterTextBuffer, or 0 if there is no maximum.
621 clutter_text_buffer_get_max_length (ClutterTextBuffer *buffer)
623 g_return_val_if_fail (CLUTTER_IS_TEXT_BUFFER (buffer), 0);
624 return buffer->priv->max_length;
628 * clutter_text_buffer_insert_text:
629 * @buffer: a #ClutterTextBuffer
630 * @position: the position at which to insert text.
631 * @chars: the text to insert into the buffer.
632 * @n_chars: the length of the text in characters, or -1
634 * Inserts @n_chars characters of @chars into the contents of the
635 * buffer, at position @position.
637 * If @n_chars is negative, then characters from chars will be inserted
638 * until a null-terminator is found. If @position or @n_chars are out of
639 * bounds, or the maximum buffer text length is exceeded, then they are
640 * coerced to sane values.
642 * Note that the position and length are in characters, not in bytes.
644 * Returns: The number of characters actually inserted.
649 clutter_text_buffer_insert_text (ClutterTextBuffer *buffer,
654 ClutterTextBufferClass *klass;
655 ClutterTextBufferPrivate *pv;
658 g_return_val_if_fail (CLUTTER_IS_TEXT_BUFFER (buffer), 0);
660 length = clutter_text_buffer_get_length (buffer);
664 n_chars = g_utf8_strlen (chars, -1);
666 /* Bring position into bounds */
667 if (position > length)
670 /* Make sure not entering too much data */
671 if (pv->max_length > 0)
673 if (length >= pv->max_length)
675 else if (length + n_chars > pv->max_length)
676 n_chars -= (length + n_chars) - pv->max_length;
679 klass = CLUTTER_TEXT_BUFFER_GET_CLASS (buffer);
680 g_return_val_if_fail (klass->insert_text != NULL, 0);
682 return (klass->insert_text) (buffer, position, chars, n_chars);
686 * clutter_text_buffer_delete_text:
687 * @buffer: a #ClutterTextBuffer
688 * @position: position at which to delete text
689 * @n_chars: number of characters to delete
691 * Deletes a sequence of characters from the buffer. @n_chars characters are
692 * deleted starting at @position. If @n_chars is negative, then all characters
693 * until the end of the text are deleted.
695 * If @position or @n_chars are out of bounds, then they are coerced to sane
698 * Note that the positions are specified in characters, not bytes.
700 * Returns: The number of characters deleted.
705 clutter_text_buffer_delete_text (ClutterTextBuffer *buffer,
709 ClutterTextBufferClass *klass;
712 g_return_val_if_fail (CLUTTER_IS_TEXT_BUFFER (buffer), 0);
714 length = clutter_text_buffer_get_length (buffer);
717 if (position > length)
719 if (position + n_chars > length)
720 n_chars = length - position;
722 klass = CLUTTER_TEXT_BUFFER_GET_CLASS (buffer);
723 g_return_val_if_fail (klass->delete_text != NULL, 0);
725 return (klass->delete_text) (buffer, position, n_chars);
729 * clutter_text_buffer_emit_inserted_text:
730 * @buffer: a #ClutterTextBuffer
731 * @position: position at which text was inserted
732 * @chars: text that was inserted
733 * @n_chars: number of characters inserted
735 * Emits the #ClutterTextBuffer::inserted-text signal on @buffer.
737 * Used when subclassing #ClutterTextBuffer
742 clutter_text_buffer_emit_inserted_text (ClutterTextBuffer *buffer,
747 g_return_if_fail (CLUTTER_IS_TEXT_BUFFER (buffer));
748 g_signal_emit (buffer, signals[INSERTED_TEXT], 0, position, chars, n_chars);
752 * clutter_text_buffer_emit_deleted_text:
753 * @buffer: a #ClutterTextBuffer
754 * @position: position at which text was deleted
755 * @n_chars: number of characters deleted
757 * Emits the #ClutterTextBuffer::deleted-text signal on @buffer.
759 * Used when subclassing #ClutterTextBuffer
764 clutter_text_buffer_emit_deleted_text (ClutterTextBuffer *buffer,
768 g_return_if_fail (CLUTTER_IS_TEXT_BUFFER (buffer));
769 g_signal_emit (buffer, signals[DELETED_TEXT], 0, position, n_chars);