1 /* GTK - The GIMP Toolkit
2 * gtktextbuffer.c Copyright (C) 2000 Red Hat, Inc.
3 * Copyright (C) 2004 Nokia Corporation
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
20 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
21 * file for a list of people on the GTK+ Team. See the ChangeLog
22 * files for a list of changes. These files are distributed with
23 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
30 #define GTK_TEXT_USE_INTERNAL_UNSUPPORTED_API
31 #include "gtkclipboard.h"
33 #include "gtkinvisible.h"
34 #include "gtkmarshalers.h"
35 #include "gtktextbuffer.h"
36 #include "gtktextbufferrichtext.h"
37 #include "gtktextbtree.h"
38 #include "gtktextiterprivate.h"
39 #include "gtktexttagprivate.h"
40 #include "gtkprivate.h"
45 * SECTION:gtktextbuffer
46 * @Short_description: Stores attributed text for display in a GtkTextView
47 * @Title: GtkTextBuffer
48 * @See_also: #GtkTextView, #GtkTextIter, #GtkTextMark
50 * You may wish to begin by reading the <link linkend="TextWidget">text widget
51 * conceptual overview</link> which gives an overview of all the objects and data
52 * types related to the text widget and how they work together.
56 typedef struct _GtkTextLogAttrCache GtkTextLogAttrCache;
58 struct _GtkTextBufferPrivate
60 GtkTargetList *copy_target_list;
61 GtkTargetEntry *copy_target_entries;
62 GtkTargetList *paste_target_list;
63 GtkTargetEntry *paste_target_entries;
65 gint n_copy_target_entries;
66 gint n_paste_target_entries;
68 GtkTextTagTable *tag_table;
71 GSList *clipboard_contents_buffers;
72 GSList *selection_clipboards;
74 GtkTextLogAttrCache *log_attr_cache;
76 guint user_action_count;
78 /* Whether the buffer has been modified since last save */
80 guint has_selection : 1;
84 typedef struct _ClipboardRequest ClipboardRequest;
86 struct _ClipboardRequest
88 GtkTextBuffer *buffer;
89 guint interactive : 1;
90 guint default_editable : 1;
91 guint replace_selection : 1;
120 PROP_CURSOR_POSITION,
121 PROP_COPY_TARGET_LIST,
122 PROP_PASTE_TARGET_LIST
125 static void gtk_text_buffer_finalize (GObject *object);
127 static void gtk_text_buffer_real_insert_text (GtkTextBuffer *buffer,
131 static void gtk_text_buffer_real_insert_pixbuf (GtkTextBuffer *buffer,
134 static void gtk_text_buffer_real_insert_anchor (GtkTextBuffer *buffer,
136 GtkTextChildAnchor *anchor);
137 static void gtk_text_buffer_real_delete_range (GtkTextBuffer *buffer,
140 static void gtk_text_buffer_real_apply_tag (GtkTextBuffer *buffer,
142 const GtkTextIter *start_char,
143 const GtkTextIter *end_char);
144 static void gtk_text_buffer_real_remove_tag (GtkTextBuffer *buffer,
146 const GtkTextIter *start_char,
147 const GtkTextIter *end_char);
148 static void gtk_text_buffer_real_changed (GtkTextBuffer *buffer);
149 static void gtk_text_buffer_real_mark_set (GtkTextBuffer *buffer,
150 const GtkTextIter *iter,
153 static GtkTextBTree* get_btree (GtkTextBuffer *buffer);
154 static void free_log_attr_cache (GtkTextLogAttrCache *cache);
156 static void remove_all_selection_clipboards (GtkTextBuffer *buffer);
157 static void update_selection_clipboards (GtkTextBuffer *buffer);
159 static GtkTextBuffer *create_clipboard_contents_buffer (GtkTextBuffer *buffer);
161 static void gtk_text_buffer_free_target_lists (GtkTextBuffer *buffer);
163 static guint signals[LAST_SIGNAL] = { 0 };
165 static void gtk_text_buffer_set_property (GObject *object,
169 static void gtk_text_buffer_get_property (GObject *object,
173 static void gtk_text_buffer_notify (GObject *object,
176 G_DEFINE_TYPE (GtkTextBuffer, gtk_text_buffer, G_TYPE_OBJECT)
179 gtk_text_buffer_class_init (GtkTextBufferClass *klass)
181 GObjectClass *object_class = G_OBJECT_CLASS (klass);
183 object_class->finalize = gtk_text_buffer_finalize;
184 object_class->set_property = gtk_text_buffer_set_property;
185 object_class->get_property = gtk_text_buffer_get_property;
186 object_class->notify = gtk_text_buffer_notify;
188 klass->insert_text = gtk_text_buffer_real_insert_text;
189 klass->insert_pixbuf = gtk_text_buffer_real_insert_pixbuf;
190 klass->insert_child_anchor = gtk_text_buffer_real_insert_anchor;
191 klass->delete_range = gtk_text_buffer_real_delete_range;
192 klass->apply_tag = gtk_text_buffer_real_apply_tag;
193 klass->remove_tag = gtk_text_buffer_real_remove_tag;
194 klass->changed = gtk_text_buffer_real_changed;
195 klass->mark_set = gtk_text_buffer_real_mark_set;
198 g_object_class_install_property (object_class,
200 g_param_spec_object ("tag-table",
202 P_("Text Tag Table"),
203 GTK_TYPE_TEXT_TAG_TABLE,
204 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
206 /* Normal properties*/
209 * GtkTextBuffer:text:
211 * The text content of the buffer. Without child widgets and images,
212 * see gtk_text_buffer_get_text() for more information.
216 g_object_class_install_property (object_class,
218 g_param_spec_string ("text",
220 P_("Current text of the buffer"),
222 GTK_PARAM_READWRITE));
225 * GtkTextBuffer:has-selection:
227 * Whether the buffer has some text currently selected.
231 g_object_class_install_property (object_class,
233 g_param_spec_boolean ("has-selection",
235 P_("Whether the buffer has some text currently selected"),
237 GTK_PARAM_READABLE));
240 * GtkTextBuffer:cursor-position:
242 * The position of the insert mark (as offset from the beginning
243 * of the buffer). It is useful for getting notified when the
248 g_object_class_install_property (object_class,
249 PROP_CURSOR_POSITION,
250 g_param_spec_int ("cursor-position",
251 P_("Cursor position"),
252 P_("The position of the insert mark (as offset from the beginning of the buffer)"),
254 GTK_PARAM_READABLE));
257 * GtkTextBuffer:copy-target-list:
259 * The list of targets this buffer supports for clipboard copying
264 g_object_class_install_property (object_class,
265 PROP_COPY_TARGET_LIST,
266 g_param_spec_boxed ("copy-target-list",
267 P_("Copy target list"),
268 P_("The list of targets this buffer supports for clipboard copying and DND source"),
269 GTK_TYPE_TARGET_LIST,
270 GTK_PARAM_READABLE));
273 * GtkTextBuffer:paste-target-list:
275 * The list of targets this buffer supports for clipboard pasting
276 * and as DND destination.
280 g_object_class_install_property (object_class,
281 PROP_PASTE_TARGET_LIST,
282 g_param_spec_boxed ("paste-target-list",
283 P_("Paste target list"),
284 P_("The list of targets this buffer supports for clipboard pasting and DND destination"),
285 GTK_TYPE_TARGET_LIST,
286 GTK_PARAM_READABLE));
289 * GtkTextBuffer::insert-text:
290 * @textbuffer: the object which received the signal
291 * @location: position to insert @text in @textbuffer
292 * @text: the UTF-8 text to be inserted
293 * @len: length of the inserted text in bytes
295 * The ::insert-text signal is emitted to insert text in a #GtkTextBuffer.
296 * Insertion actually occurs in the default handler.
298 * Note that if your handler runs before the default handler it must not
299 * invalidate the @location iter (or has to revalidate it).
300 * The default signal handler revalidates it to point to the end of the
304 * gtk_text_buffer_insert(),
305 * gtk_text_buffer_insert_range().
307 signals[INSERT_TEXT] =
308 g_signal_new (I_("insert-text"),
309 G_OBJECT_CLASS_TYPE (object_class),
311 G_STRUCT_OFFSET (GtkTextBufferClass, insert_text),
313 _gtk_marshal_VOID__BOXED_STRING_INT,
316 GTK_TYPE_TEXT_ITER | G_SIGNAL_TYPE_STATIC_SCOPE,
317 G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE,
321 * GtkTextBuffer::insert-pixbuf:
322 * @textbuffer: the object which received the signal
323 * @location: position to insert @pixbuf in @textbuffer
324 * @pixbuf: the #GdkPixbuf to be inserted
326 * The ::insert-pixbuf signal is emitted to insert a #GdkPixbuf
327 * in a #GtkTextBuffer. Insertion actually occurs in the default handler.
329 * Note that if your handler runs before the default handler it must not
330 * invalidate the @location iter (or has to revalidate it).
331 * The default signal handler revalidates it to be placed after the
334 * See also: gtk_text_buffer_insert_pixbuf().
336 signals[INSERT_PIXBUF] =
337 g_signal_new (I_("insert-pixbuf"),
338 G_OBJECT_CLASS_TYPE (object_class),
340 G_STRUCT_OFFSET (GtkTextBufferClass, insert_pixbuf),
342 _gtk_marshal_VOID__BOXED_OBJECT,
345 GTK_TYPE_TEXT_ITER | G_SIGNAL_TYPE_STATIC_SCOPE,
350 * GtkTextBuffer::insert-child-anchor:
351 * @textbuffer: the object which received the signal
352 * @location: position to insert @anchor in @textbuffer
353 * @anchor: the #GtkTextChildAnchor to be inserted
355 * The ::insert-child-anchor signal is emitted to insert a
356 * #GtkTextChildAnchor in a #GtkTextBuffer.
357 * Insertion actually occurs in the default handler.
359 * Note that if your handler runs before the default handler it must
360 * not invalidate the @location iter (or has to revalidate it).
361 * The default signal handler revalidates it to be placed after the
364 * See also: gtk_text_buffer_insert_child_anchor().
366 signals[INSERT_CHILD_ANCHOR] =
367 g_signal_new (I_("insert-child-anchor"),
368 G_OBJECT_CLASS_TYPE (object_class),
370 G_STRUCT_OFFSET (GtkTextBufferClass, insert_child_anchor),
372 _gtk_marshal_VOID__BOXED_OBJECT,
375 GTK_TYPE_TEXT_ITER | G_SIGNAL_TYPE_STATIC_SCOPE,
376 GTK_TYPE_TEXT_CHILD_ANCHOR);
379 * GtkTextBuffer::delete-range:
380 * @textbuffer: the object which received the signal
381 * @start: the start of the range to be deleted
382 * @end: the end of the range to be deleted
384 * The ::delete-range signal is emitted to delete a range
385 * from a #GtkTextBuffer.
387 * Note that if your handler runs before the default handler it must not
388 * invalidate the @start and @end iters (or has to revalidate them).
389 * The default signal handler revalidates the @start and @end iters to
390 * both point point to the location where text was deleted. Handlers
391 * which run after the default handler (see g_signal_connect_after())
392 * do not have access to the deleted text.
394 * See also: gtk_text_buffer_delete().
396 signals[DELETE_RANGE] =
397 g_signal_new (I_("delete-range"),
398 G_OBJECT_CLASS_TYPE (object_class),
400 G_STRUCT_OFFSET (GtkTextBufferClass, delete_range),
402 _gtk_marshal_VOID__BOXED_BOXED,
405 GTK_TYPE_TEXT_ITER | G_SIGNAL_TYPE_STATIC_SCOPE,
406 GTK_TYPE_TEXT_ITER | G_SIGNAL_TYPE_STATIC_SCOPE);
409 * GtkTextBuffer::changed:
410 * @textbuffer: the object which received the signal
412 * The ::changed signal is emitted when the content of a #GtkTextBuffer
416 g_signal_new (I_("changed"),
417 G_OBJECT_CLASS_TYPE (object_class),
419 G_STRUCT_OFFSET (GtkTextBufferClass, changed),
421 _gtk_marshal_VOID__VOID,
426 * GtkTextBuffer::modified-changed:
427 * @textbuffer: the object which received the signal
429 * The ::modified-changed signal is emitted when the modified bit of a
430 * #GtkTextBuffer flips.
433 * gtk_text_buffer_set_modified().
435 signals[MODIFIED_CHANGED] =
436 g_signal_new (I_("modified-changed"),
437 G_OBJECT_CLASS_TYPE (object_class),
439 G_STRUCT_OFFSET (GtkTextBufferClass, modified_changed),
441 _gtk_marshal_VOID__VOID,
446 * GtkTextBuffer::mark-set:
447 * @textbuffer: the object which received the signal
448 * @location: The location of @mark in @textbuffer
449 * @mark: The mark that is set
451 * The ::mark-set signal is emitted as notification
452 * after a #GtkTextMark is set.
455 * gtk_text_buffer_create_mark(),
456 * gtk_text_buffer_move_mark().
459 g_signal_new (I_("mark-set"),
460 G_OBJECT_CLASS_TYPE (object_class),
462 G_STRUCT_OFFSET (GtkTextBufferClass, mark_set),
464 _gtk_marshal_VOID__BOXED_OBJECT,
471 * GtkTextBuffer::mark-deleted:
472 * @textbuffer: the object which received the signal
473 * @mark: The mark that was deleted
475 * The ::mark-deleted signal is emitted as notification
476 * after a #GtkTextMark is deleted.
479 * gtk_text_buffer_delete_mark().
481 signals[MARK_DELETED] =
482 g_signal_new (I_("mark-deleted"),
483 G_OBJECT_CLASS_TYPE (object_class),
485 G_STRUCT_OFFSET (GtkTextBufferClass, mark_deleted),
487 _gtk_marshal_VOID__OBJECT,
493 * GtkTextBuffer::apply-tag:
494 * @textbuffer: the object which received the signal
495 * @tag: the applied tag
496 * @start: the start of the range the tag is applied to
497 * @end: the end of the range the tag is applied to
499 * The ::apply-tag signal is emitted to apply a tag to a
500 * range of text in a #GtkTextBuffer.
501 * Applying actually occurs in the default handler.
503 * Note that if your handler runs before the default handler it must not
504 * invalidate the @start and @end iters (or has to revalidate them).
507 * gtk_text_buffer_apply_tag(),
508 * gtk_text_buffer_insert_with_tags(),
509 * gtk_text_buffer_insert_range().
512 g_signal_new (I_("apply-tag"),
513 G_OBJECT_CLASS_TYPE (object_class),
515 G_STRUCT_OFFSET (GtkTextBufferClass, apply_tag),
517 _gtk_marshal_VOID__OBJECT_BOXED_BOXED,
526 * GtkTextBuffer::remove-tag:
527 * @textbuffer: the object which received the signal
528 * @tag: the tag to be removed
529 * @start: the start of the range the tag is removed from
530 * @end: the end of the range the tag is removed from
532 * The ::remove-tag signal is emitted to remove all occurrences of @tag from
533 * a range of text in a #GtkTextBuffer.
534 * Removal actually occurs in the default handler.
536 * Note that if your handler runs before the default handler it must not
537 * invalidate the @start and @end iters (or has to revalidate them).
540 * gtk_text_buffer_remove_tag().
542 signals[REMOVE_TAG] =
543 g_signal_new (I_("remove-tag"),
544 G_OBJECT_CLASS_TYPE (object_class),
546 G_STRUCT_OFFSET (GtkTextBufferClass, remove_tag),
548 _gtk_marshal_VOID__OBJECT_BOXED_BOXED,
556 * GtkTextBuffer::begin-user-action:
557 * @textbuffer: the object which received the signal
559 * The ::begin-user-action signal is emitted at the beginning of a single
560 * user-visible operation on a #GtkTextBuffer.
563 * gtk_text_buffer_begin_user_action(),
564 * gtk_text_buffer_insert_interactive(),
565 * gtk_text_buffer_insert_range_interactive(),
566 * gtk_text_buffer_delete_interactive(),
567 * gtk_text_buffer_backspace(),
568 * gtk_text_buffer_delete_selection().
570 signals[BEGIN_USER_ACTION] =
571 g_signal_new (I_("begin-user-action"),
572 G_OBJECT_CLASS_TYPE (object_class),
574 G_STRUCT_OFFSET (GtkTextBufferClass, begin_user_action),
576 _gtk_marshal_VOID__VOID,
581 * GtkTextBuffer::end-user-action:
582 * @textbuffer: the object which received the signal
584 * The ::end-user-action signal is emitted at the end of a single
585 * user-visible operation on the #GtkTextBuffer.
588 * gtk_text_buffer_end_user_action(),
589 * gtk_text_buffer_insert_interactive(),
590 * gtk_text_buffer_insert_range_interactive(),
591 * gtk_text_buffer_delete_interactive(),
592 * gtk_text_buffer_backspace(),
593 * gtk_text_buffer_delete_selection(),
594 * gtk_text_buffer_backspace().
596 signals[END_USER_ACTION] =
597 g_signal_new (I_("end-user-action"),
598 G_OBJECT_CLASS_TYPE (object_class),
600 G_STRUCT_OFFSET (GtkTextBufferClass, end_user_action),
602 _gtk_marshal_VOID__VOID,
607 * GtkTextBuffer::paste-done:
608 * @textbuffer: the object which received the signal
610 * The paste-done signal is emitted after paste operation has been completed.
611 * This is useful to properly scroll the view to the end of the pasted text.
612 * See gtk_text_buffer_paste_clipboard() for more details.
616 signals[PASTE_DONE] =
617 g_signal_new (I_("paste-done"),
618 G_OBJECT_CLASS_TYPE (object_class),
620 G_STRUCT_OFFSET (GtkTextBufferClass, paste_done),
622 _gtk_marshal_VOID__OBJECT,
627 g_type_class_add_private (object_class, sizeof (GtkTextBufferPrivate));
631 gtk_text_buffer_init (GtkTextBuffer *buffer)
633 buffer->priv = G_TYPE_INSTANCE_GET_PRIVATE (buffer,
634 GTK_TYPE_TEXT_BUFFER,
635 GtkTextBufferPrivate);
637 buffer->priv->clipboard_contents_buffers = NULL;
638 buffer->priv->tag_table = NULL;
640 /* allow copying of arbiatray stuff in the internal rich text format */
641 gtk_text_buffer_register_serialize_tagset (buffer, NULL);
645 set_table (GtkTextBuffer *buffer, GtkTextTagTable *table)
647 GtkTextBufferPrivate *priv = buffer->priv;
649 g_return_if_fail (priv->tag_table == NULL);
653 priv->tag_table = table;
654 g_object_ref (priv->tag_table);
655 _gtk_text_tag_table_add_buffer (table, buffer);
659 static GtkTextTagTable*
660 get_table (GtkTextBuffer *buffer)
662 GtkTextBufferPrivate *priv = buffer->priv;
664 if (priv->tag_table == NULL)
666 priv->tag_table = gtk_text_tag_table_new ();
667 _gtk_text_tag_table_add_buffer (priv->tag_table, buffer);
670 return priv->tag_table;
674 gtk_text_buffer_set_property (GObject *object,
679 GtkTextBuffer *text_buffer;
681 text_buffer = GTK_TEXT_BUFFER (object);
686 set_table (text_buffer, g_value_get_object (value));
690 gtk_text_buffer_set_text (text_buffer,
691 g_value_get_string (value), -1);
695 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
701 gtk_text_buffer_get_property (GObject *object,
706 GtkTextBuffer *text_buffer;
709 text_buffer = GTK_TEXT_BUFFER (object);
714 g_value_set_object (value, get_table (text_buffer));
719 GtkTextIter start, end;
721 gtk_text_buffer_get_start_iter (text_buffer, &start);
722 gtk_text_buffer_get_end_iter (text_buffer, &end);
724 g_value_take_string (value,
725 gtk_text_buffer_get_text (text_buffer,
726 &start, &end, FALSE));
730 case PROP_HAS_SELECTION:
731 g_value_set_boolean (value, text_buffer->priv->has_selection);
734 case PROP_CURSOR_POSITION:
735 gtk_text_buffer_get_iter_at_mark (text_buffer, &iter,
736 gtk_text_buffer_get_insert (text_buffer));
737 g_value_set_int (value, gtk_text_iter_get_offset (&iter));
740 case PROP_COPY_TARGET_LIST:
741 g_value_set_boxed (value, gtk_text_buffer_get_copy_target_list (text_buffer));
744 case PROP_PASTE_TARGET_LIST:
745 g_value_set_boxed (value, gtk_text_buffer_get_paste_target_list (text_buffer));
749 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
755 gtk_text_buffer_notify (GObject *object,
758 if (!strcmp (pspec->name, "copy-target-list") ||
759 !strcmp (pspec->name, "paste-target-list"))
761 gtk_text_buffer_free_target_lists (GTK_TEXT_BUFFER (object));
766 * gtk_text_buffer_new:
767 * @table: (allow-none): a tag table, or %NULL to create a new one
769 * Creates a new text buffer.
771 * Return value: a new text buffer
774 gtk_text_buffer_new (GtkTextTagTable *table)
776 GtkTextBuffer *text_buffer;
778 text_buffer = g_object_new (GTK_TYPE_TEXT_BUFFER, "tag-table", table, NULL);
784 gtk_text_buffer_finalize (GObject *object)
786 GtkTextBuffer *buffer;
787 GtkTextBufferPrivate *priv;
789 buffer = GTK_TEXT_BUFFER (object);
792 remove_all_selection_clipboards (buffer);
796 _gtk_text_tag_table_remove_buffer (priv->tag_table, buffer);
797 g_object_unref (priv->tag_table);
798 priv->tag_table = NULL;
803 _gtk_text_btree_unref (priv->btree);
807 if (priv->log_attr_cache)
808 free_log_attr_cache (priv->log_attr_cache);
810 priv->log_attr_cache = NULL;
812 gtk_text_buffer_free_target_lists (buffer);
814 G_OBJECT_CLASS (gtk_text_buffer_parent_class)->finalize (object);
818 get_btree (GtkTextBuffer *buffer)
820 GtkTextBufferPrivate *priv = buffer->priv;
822 if (priv->btree == NULL)
823 priv->btree = _gtk_text_btree_new (gtk_text_buffer_get_tag_table (buffer),
830 _gtk_text_buffer_get_btree (GtkTextBuffer *buffer)
832 return get_btree (buffer);
836 * gtk_text_buffer_get_tag_table:
837 * @buffer: a #GtkTextBuffer
839 * Get the #GtkTextTagTable associated with this buffer.
841 * Return value: (transfer none): the buffer's tag table
844 gtk_text_buffer_get_tag_table (GtkTextBuffer *buffer)
846 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
848 return get_table (buffer);
852 * gtk_text_buffer_set_text:
853 * @buffer: a #GtkTextBuffer
854 * @text: UTF-8 text to insert
855 * @len: length of @text in bytes
857 * Deletes current contents of @buffer, and inserts @text instead. If
858 * @len is -1, @text must be nul-terminated. @text must be valid UTF-8.
861 gtk_text_buffer_set_text (GtkTextBuffer *buffer,
865 GtkTextIter start, end;
867 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
868 g_return_if_fail (text != NULL);
873 gtk_text_buffer_get_bounds (buffer, &start, &end);
875 gtk_text_buffer_delete (buffer, &start, &end);
879 gtk_text_buffer_get_iter_at_offset (buffer, &start, 0);
880 gtk_text_buffer_insert (buffer, &start, text, len);
883 g_object_notify (G_OBJECT (buffer), "text");
893 gtk_text_buffer_real_insert_text (GtkTextBuffer *buffer,
898 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
899 g_return_if_fail (iter != NULL);
901 _gtk_text_btree_insert (iter, text, len);
903 g_signal_emit (buffer, signals[CHANGED], 0);
904 g_object_notify (G_OBJECT (buffer), "cursor-position");
908 gtk_text_buffer_emit_insert (GtkTextBuffer *buffer,
913 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
914 g_return_if_fail (iter != NULL);
915 g_return_if_fail (text != NULL);
920 g_return_if_fail (g_utf8_validate (text, len, NULL));
924 g_signal_emit (buffer, signals[INSERT_TEXT], 0,
930 * gtk_text_buffer_insert:
931 * @buffer: a #GtkTextBuffer
932 * @iter: a position in the buffer
933 * @text: text in UTF-8 format
934 * @len: length of text in bytes, or -1
936 * Inserts @len bytes of @text at position @iter. If @len is -1,
937 * @text must be nul-terminated and will be inserted in its
938 * entirety. Emits the "insert-text" signal; insertion actually occurs
939 * in the default handler for the signal. @iter is invalidated when
940 * insertion occurs (because the buffer contents change), but the
941 * default signal handler revalidates it to point to the end of the
945 gtk_text_buffer_insert (GtkTextBuffer *buffer,
950 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
951 g_return_if_fail (iter != NULL);
952 g_return_if_fail (text != NULL);
953 g_return_if_fail (gtk_text_iter_get_buffer (iter) == buffer);
955 gtk_text_buffer_emit_insert (buffer, iter, text, len);
959 * gtk_text_buffer_insert_at_cursor:
960 * @buffer: a #GtkTextBuffer
961 * @text: text in UTF-8 format
962 * @len: length of text, in bytes
964 * Simply calls gtk_text_buffer_insert(), using the current
965 * cursor position as the insertion point.
968 gtk_text_buffer_insert_at_cursor (GtkTextBuffer *buffer,
974 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
975 g_return_if_fail (text != NULL);
977 gtk_text_buffer_get_iter_at_mark (buffer, &iter,
978 gtk_text_buffer_get_insert (buffer));
980 gtk_text_buffer_insert (buffer, &iter, text, len);
984 * gtk_text_buffer_insert_interactive:
985 * @buffer: a #GtkTextBuffer
986 * @iter: a position in @buffer
987 * @text: some UTF-8 text
988 * @len: length of text in bytes, or -1
989 * @default_editable: default editability of buffer
991 * Like gtk_text_buffer_insert(), but the insertion will not occur if
992 * @iter is at a non-editable location in the buffer. Usually you
993 * want to prevent insertions at ineditable locations if the insertion
994 * results from a user action (is interactive).
996 * @default_editable indicates the editability of text that doesn't
997 * have a tag affecting editability applied to it. Typically the
998 * result of gtk_text_view_get_editable() is appropriate here.
1000 * Return value: whether text was actually inserted
1003 gtk_text_buffer_insert_interactive (GtkTextBuffer *buffer,
1007 gboolean default_editable)
1009 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
1010 g_return_val_if_fail (text != NULL, FALSE);
1011 g_return_val_if_fail (gtk_text_iter_get_buffer (iter) == buffer, FALSE);
1013 if (gtk_text_iter_can_insert (iter, default_editable))
1015 gtk_text_buffer_begin_user_action (buffer);
1016 gtk_text_buffer_emit_insert (buffer, iter, text, len);
1017 gtk_text_buffer_end_user_action (buffer);
1025 * gtk_text_buffer_insert_interactive_at_cursor:
1026 * @buffer: a #GtkTextBuffer
1027 * @text: text in UTF-8 format
1028 * @len: length of text in bytes, or -1
1029 * @default_editable: default editability of buffer
1031 * Calls gtk_text_buffer_insert_interactive() at the cursor
1034 * @default_editable indicates the editability of text that doesn't
1035 * have a tag affecting editability applied to it. Typically the
1036 * result of gtk_text_view_get_editable() is appropriate here.
1038 * Return value: whether text was actually inserted
1041 gtk_text_buffer_insert_interactive_at_cursor (GtkTextBuffer *buffer,
1044 gboolean default_editable)
1048 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
1049 g_return_val_if_fail (text != NULL, FALSE);
1051 gtk_text_buffer_get_iter_at_mark (buffer, &iter,
1052 gtk_text_buffer_get_insert (buffer));
1054 return gtk_text_buffer_insert_interactive (buffer, &iter, text, len,
1059 possibly_not_text (gunichar ch,
1062 return ch == GTK_TEXT_UNKNOWN_CHAR;
1066 insert_text_range (GtkTextBuffer *buffer,
1068 const GtkTextIter *orig_start,
1069 const GtkTextIter *orig_end,
1070 gboolean interactive)
1074 text = gtk_text_iter_get_text (orig_start, orig_end);
1076 gtk_text_buffer_emit_insert (buffer, iter, text, -1);
1081 typedef struct _Range Range;
1084 GtkTextBuffer *buffer;
1085 GtkTextMark *start_mark;
1086 GtkTextMark *end_mark;
1087 GtkTextMark *whole_end_mark;
1088 GtkTextIter *range_start;
1089 GtkTextIter *range_end;
1090 GtkTextIter *whole_end;
1094 save_range (GtkTextIter *range_start,
1095 GtkTextIter *range_end,
1096 GtkTextIter *whole_end)
1100 r = g_new (Range, 1);
1102 r->buffer = gtk_text_iter_get_buffer (range_start);
1103 g_object_ref (r->buffer);
1106 gtk_text_buffer_create_mark (gtk_text_iter_get_buffer (range_start),
1111 gtk_text_buffer_create_mark (gtk_text_iter_get_buffer (range_start),
1117 gtk_text_buffer_create_mark (gtk_text_iter_get_buffer (range_start),
1122 r->range_start = range_start;
1123 r->range_end = range_end;
1124 r->whole_end = whole_end;
1130 restore_range (Range *r)
1132 gtk_text_buffer_get_iter_at_mark (r->buffer,
1136 gtk_text_buffer_get_iter_at_mark (r->buffer,
1140 gtk_text_buffer_get_iter_at_mark (r->buffer,
1144 gtk_text_buffer_delete_mark (r->buffer, r->start_mark);
1145 gtk_text_buffer_delete_mark (r->buffer, r->end_mark);
1146 gtk_text_buffer_delete_mark (r->buffer, r->whole_end_mark);
1148 /* Due to the gravities on the marks, the ordering could have
1149 * gotten mangled; we switch to an empty range in that
1153 if (gtk_text_iter_compare (r->range_start, r->range_end) > 0)
1154 *r->range_start = *r->range_end;
1156 if (gtk_text_iter_compare (r->range_end, r->whole_end) > 0)
1157 *r->range_end = *r->whole_end;
1159 g_object_unref (r->buffer);
1164 insert_range_untagged (GtkTextBuffer *buffer,
1166 const GtkTextIter *orig_start,
1167 const GtkTextIter *orig_end,
1168 gboolean interactive)
1170 GtkTextIter range_start;
1171 GtkTextIter range_end;
1172 GtkTextIter start, end;
1175 if (gtk_text_iter_equal (orig_start, orig_end))
1178 start = *orig_start;
1181 range_start = start;
1186 if (gtk_text_iter_equal (&range_start, &range_end))
1188 /* Figure out how to move forward */
1190 g_assert (gtk_text_iter_compare (&range_end, &end) <= 0);
1192 if (gtk_text_iter_equal (&range_end, &end))
1194 /* nothing left to do */
1197 else if (gtk_text_iter_get_char (&range_end) == GTK_TEXT_UNKNOWN_CHAR)
1199 GdkPixbuf *pixbuf = NULL;
1200 GtkTextChildAnchor *anchor = NULL;
1201 pixbuf = gtk_text_iter_get_pixbuf (&range_end);
1202 anchor = gtk_text_iter_get_child_anchor (&range_end);
1206 r = save_range (&range_start,
1210 gtk_text_buffer_insert_pixbuf (buffer,
1217 gtk_text_iter_forward_char (&range_end);
1219 range_start = range_end;
1223 /* Just skip anchors */
1225 gtk_text_iter_forward_char (&range_end);
1226 range_start = range_end;
1230 /* The GTK_TEXT_UNKNOWN_CHAR was in a text segment, so
1233 gtk_text_iter_forward_find_char (&range_end,
1234 possibly_not_text, NULL,
1237 g_assert (gtk_text_iter_compare (&range_end, &end) <= 0);
1242 /* Text segment starts here, so forward search to
1243 * find its possible endpoint
1245 gtk_text_iter_forward_find_char (&range_end,
1246 possibly_not_text, NULL,
1249 g_assert (gtk_text_iter_compare (&range_end, &end) <= 0);
1254 r = save_range (&range_start,
1258 insert_text_range (buffer,
1267 range_start = range_end;
1273 insert_range_not_inside_self (GtkTextBuffer *buffer,
1275 const GtkTextIter *orig_start,
1276 const GtkTextIter *orig_end,
1277 gboolean interactive)
1279 /* Find each range of uniformly-tagged text, insert it,
1280 * then apply the tags.
1282 GtkTextIter start = *orig_start;
1283 GtkTextIter end = *orig_end;
1284 GtkTextIter range_start;
1285 GtkTextIter range_end;
1287 if (gtk_text_iter_equal (orig_start, orig_end))
1290 gtk_text_iter_order (&start, &end);
1292 range_start = start;
1298 GtkTextIter start_iter;
1303 if (gtk_text_iter_equal (&range_start, &end))
1304 break; /* All done */
1306 g_assert (gtk_text_iter_compare (&range_start, &end) < 0);
1308 gtk_text_iter_forward_to_tag_toggle (&range_end, NULL);
1310 g_assert (!gtk_text_iter_equal (&range_start, &range_end));
1312 /* Clamp to the end iterator */
1313 if (gtk_text_iter_compare (&range_end, &end) > 0)
1316 /* We have a range with unique tags; insert it, and
1319 start_offset = gtk_text_iter_get_offset (iter);
1321 r = save_range (&range_start, &range_end, &end);
1323 insert_range_untagged (buffer, iter, &range_start, &range_end, interactive);
1328 gtk_text_buffer_get_iter_at_offset (buffer, &start_iter, start_offset);
1330 tags = gtk_text_iter_get_tags (&range_start);
1332 while (tmp_list != NULL)
1334 gtk_text_buffer_apply_tag (buffer,
1339 tmp_list = g_slist_next (tmp_list);
1341 g_slist_free (tags);
1343 range_start = range_end;
1348 gtk_text_buffer_real_insert_range (GtkTextBuffer *buffer,
1350 const GtkTextIter *orig_start,
1351 const GtkTextIter *orig_end,
1352 gboolean interactive)
1354 GtkTextBuffer *src_buffer;
1356 /* Find each range of uniformly-tagged text, insert it,
1357 * then apply the tags.
1359 if (gtk_text_iter_equal (orig_start, orig_end))
1363 gtk_text_buffer_begin_user_action (buffer);
1365 src_buffer = gtk_text_iter_get_buffer (orig_start);
1367 if (gtk_text_iter_get_buffer (iter) != src_buffer ||
1368 !gtk_text_iter_in_range (iter, orig_start, orig_end))
1370 insert_range_not_inside_self (buffer, iter, orig_start, orig_end, interactive);
1374 /* If you insert a range into itself, it could loop infinitely
1375 * because the region being copied keeps growing as we insert. So
1376 * we have to separately copy the range before and after
1377 * the insertion point.
1379 GtkTextIter start = *orig_start;
1380 GtkTextIter end = *orig_end;
1381 GtkTextIter range_start;
1382 GtkTextIter range_end;
1386 gtk_text_iter_order (&start, &end);
1388 range_start = start;
1390 first_half = save_range (&range_start, &range_end, &end);
1392 range_start = *iter;
1394 second_half = save_range (&range_start, &range_end, &end);
1396 restore_range (first_half);
1397 insert_range_not_inside_self (buffer, iter, &range_start, &range_end, interactive);
1399 restore_range (second_half);
1400 insert_range_not_inside_self (buffer, iter, &range_start, &range_end, interactive);
1404 gtk_text_buffer_end_user_action (buffer);
1408 * gtk_text_buffer_insert_range:
1409 * @buffer: a #GtkTextBuffer
1410 * @iter: a position in @buffer
1411 * @start: a position in a #GtkTextBuffer
1412 * @end: another position in the same buffer as @start
1414 * Copies text, tags, and pixbufs between @start and @end (the order
1415 * of @start and @end doesn't matter) and inserts the copy at @iter.
1416 * Used instead of simply getting/inserting text because it preserves
1417 * images and tags. If @start and @end are in a different buffer from
1418 * @buffer, the two buffers must share the same tag table.
1420 * Implemented via emissions of the insert_text and apply_tag signals,
1424 gtk_text_buffer_insert_range (GtkTextBuffer *buffer,
1426 const GtkTextIter *start,
1427 const GtkTextIter *end)
1429 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1430 g_return_if_fail (iter != NULL);
1431 g_return_if_fail (start != NULL);
1432 g_return_if_fail (end != NULL);
1433 g_return_if_fail (gtk_text_iter_get_buffer (start) ==
1434 gtk_text_iter_get_buffer (end));
1435 g_return_if_fail (gtk_text_iter_get_buffer (start)->priv->tag_table ==
1436 buffer->priv->tag_table);
1437 g_return_if_fail (gtk_text_iter_get_buffer (iter) == buffer);
1439 gtk_text_buffer_real_insert_range (buffer, iter, start, end, FALSE);
1443 * gtk_text_buffer_insert_range_interactive:
1444 * @buffer: a #GtkTextBuffer
1445 * @iter: a position in @buffer
1446 * @start: a position in a #GtkTextBuffer
1447 * @end: another position in the same buffer as @start
1448 * @default_editable: default editability of the buffer
1450 * Same as gtk_text_buffer_insert_range(), but does nothing if the
1451 * insertion point isn't editable. The @default_editable parameter
1452 * indicates whether the text is editable at @iter if no tags
1453 * enclosing @iter affect editability. Typically the result of
1454 * gtk_text_view_get_editable() is appropriate here.
1456 * Returns: whether an insertion was possible at @iter
1459 gtk_text_buffer_insert_range_interactive (GtkTextBuffer *buffer,
1461 const GtkTextIter *start,
1462 const GtkTextIter *end,
1463 gboolean default_editable)
1465 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
1466 g_return_val_if_fail (iter != NULL, FALSE);
1467 g_return_val_if_fail (start != NULL, FALSE);
1468 g_return_val_if_fail (end != NULL, FALSE);
1469 g_return_val_if_fail (gtk_text_iter_get_buffer (start) ==
1470 gtk_text_iter_get_buffer (end), FALSE);
1471 g_return_val_if_fail (gtk_text_iter_get_buffer (start)->priv->tag_table ==
1472 buffer->priv->tag_table, FALSE);
1474 if (gtk_text_iter_can_insert (iter, default_editable))
1476 gtk_text_buffer_real_insert_range (buffer, iter, start, end, TRUE);
1484 * gtk_text_buffer_insert_with_tags:
1485 * @buffer: a #GtkTextBuffer
1486 * @iter: an iterator in @buffer
1488 * @len: length of @text, or -1
1489 * @first_tag: first tag to apply to @text
1490 * @...: %NULL-terminated list of tags to apply
1492 * Inserts @text into @buffer at @iter, applying the list of tags to
1493 * the newly-inserted text. The last tag specified must be %NULL to
1494 * terminate the list. Equivalent to calling gtk_text_buffer_insert(),
1495 * then gtk_text_buffer_apply_tag() on the inserted text;
1496 * gtk_text_buffer_insert_with_tags() is just a convenience function.
1499 gtk_text_buffer_insert_with_tags (GtkTextBuffer *buffer,
1503 GtkTextTag *first_tag,
1511 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1512 g_return_if_fail (iter != NULL);
1513 g_return_if_fail (text != NULL);
1514 g_return_if_fail (gtk_text_iter_get_buffer (iter) == buffer);
1516 start_offset = gtk_text_iter_get_offset (iter);
1518 gtk_text_buffer_insert (buffer, iter, text, len);
1520 if (first_tag == NULL)
1523 gtk_text_buffer_get_iter_at_offset (buffer, &start, start_offset);
1525 va_start (args, first_tag);
1529 gtk_text_buffer_apply_tag (buffer, tag, &start, iter);
1531 tag = va_arg (args, GtkTextTag*);
1538 * gtk_text_buffer_insert_with_tags_by_name:
1539 * @buffer: a #GtkTextBuffer
1540 * @iter: position in @buffer
1542 * @len: length of @text, or -1
1543 * @first_tag_name: name of a tag to apply to @text
1544 * @...: more tag names
1546 * Same as gtk_text_buffer_insert_with_tags(), but allows you
1547 * to pass in tag names instead of tag objects.
1550 gtk_text_buffer_insert_with_tags_by_name (GtkTextBuffer *buffer,
1554 const gchar *first_tag_name,
1560 const gchar *tag_name;
1562 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1563 g_return_if_fail (iter != NULL);
1564 g_return_if_fail (text != NULL);
1565 g_return_if_fail (gtk_text_iter_get_buffer (iter) == buffer);
1567 start_offset = gtk_text_iter_get_offset (iter);
1569 gtk_text_buffer_insert (buffer, iter, text, len);
1571 if (first_tag_name == NULL)
1574 gtk_text_buffer_get_iter_at_offset (buffer, &start, start_offset);
1576 va_start (args, first_tag_name);
1577 tag_name = first_tag_name;
1582 tag = gtk_text_tag_table_lookup (buffer->priv->tag_table,
1587 g_warning ("%s: no tag with name '%s'!", G_STRLOC, tag_name);
1592 gtk_text_buffer_apply_tag (buffer, tag, &start, iter);
1594 tag_name = va_arg (args, const gchar*);
1606 gtk_text_buffer_real_delete_range (GtkTextBuffer *buffer,
1610 gboolean has_selection;
1612 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1613 g_return_if_fail (start != NULL);
1614 g_return_if_fail (end != NULL);
1616 _gtk_text_btree_delete (start, end);
1618 /* may have deleted the selection... */
1619 update_selection_clipboards (buffer);
1621 has_selection = gtk_text_buffer_get_selection_bounds (buffer, NULL, NULL);
1622 if (has_selection != buffer->priv->has_selection)
1624 buffer->priv->has_selection = has_selection;
1625 g_object_notify (G_OBJECT (buffer), "has-selection");
1628 g_signal_emit (buffer, signals[CHANGED], 0);
1629 g_object_notify (G_OBJECT (buffer), "cursor-position");
1633 gtk_text_buffer_emit_delete (GtkTextBuffer *buffer,
1637 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1638 g_return_if_fail (start != NULL);
1639 g_return_if_fail (end != NULL);
1641 if (gtk_text_iter_equal (start, end))
1644 gtk_text_iter_order (start, end);
1646 g_signal_emit (buffer,
1647 signals[DELETE_RANGE],
1653 * gtk_text_buffer_delete:
1654 * @buffer: a #GtkTextBuffer
1655 * @start: a position in @buffer
1656 * @end: another position in @buffer
1658 * Deletes text between @start and @end. The order of @start and @end
1659 * is not actually relevant; gtk_text_buffer_delete() will reorder
1660 * them. This function actually emits the "delete-range" signal, and
1661 * the default handler of that signal deletes the text. Because the
1662 * buffer is modified, all outstanding iterators become invalid after
1663 * calling this function; however, the @start and @end will be
1664 * re-initialized to point to the location where text was deleted.
1667 gtk_text_buffer_delete (GtkTextBuffer *buffer,
1671 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1672 g_return_if_fail (start != NULL);
1673 g_return_if_fail (end != NULL);
1674 g_return_if_fail (gtk_text_iter_get_buffer (start) == buffer);
1675 g_return_if_fail (gtk_text_iter_get_buffer (end) == buffer);
1677 gtk_text_buffer_emit_delete (buffer, start, end);
1681 * gtk_text_buffer_delete_interactive:
1682 * @buffer: a #GtkTextBuffer
1683 * @start_iter: start of range to delete
1684 * @end_iter: end of range
1685 * @default_editable: whether the buffer is editable by default
1687 * Deletes all <emphasis>editable</emphasis> text in the given range.
1688 * Calls gtk_text_buffer_delete() for each editable sub-range of
1689 * [@start,@end). @start and @end are revalidated to point to
1690 * the location of the last deleted range, or left untouched if
1691 * no text was deleted.
1693 * Return value: whether some text was actually deleted
1696 gtk_text_buffer_delete_interactive (GtkTextBuffer *buffer,
1697 GtkTextIter *start_iter,
1698 GtkTextIter *end_iter,
1699 gboolean default_editable)
1701 GtkTextMark *end_mark;
1702 GtkTextMark *start_mark;
1704 gboolean current_state;
1705 gboolean deleted_stuff = FALSE;
1707 /* Delete all editable text in the range start_iter, end_iter */
1709 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
1710 g_return_val_if_fail (start_iter != NULL, FALSE);
1711 g_return_val_if_fail (end_iter != NULL, FALSE);
1712 g_return_val_if_fail (gtk_text_iter_get_buffer (start_iter) == buffer, FALSE);
1713 g_return_val_if_fail (gtk_text_iter_get_buffer (end_iter) == buffer, FALSE);
1716 gtk_text_buffer_begin_user_action (buffer);
1718 gtk_text_iter_order (start_iter, end_iter);
1720 start_mark = gtk_text_buffer_create_mark (buffer, NULL,
1722 end_mark = gtk_text_buffer_create_mark (buffer, NULL,
1725 gtk_text_buffer_get_iter_at_mark (buffer, &iter, start_mark);
1727 current_state = gtk_text_iter_editable (&iter, default_editable);
1732 gboolean done = FALSE;
1735 gtk_text_iter_forward_to_tag_toggle (&iter, NULL);
1737 gtk_text_buffer_get_iter_at_mark (buffer, &end, end_mark);
1739 if (gtk_text_iter_compare (&iter, &end) >= 0)
1742 iter = end; /* clamp to the last boundary */
1745 new_state = gtk_text_iter_editable (&iter, default_editable);
1747 if (current_state == new_state)
1753 /* We're ending an editable region. Delete said region. */
1756 gtk_text_buffer_get_iter_at_mark (buffer, &start, start_mark);
1758 gtk_text_buffer_emit_delete (buffer, &start, &iter);
1760 deleted_stuff = TRUE;
1762 /* revalidate user's iterators. */
1763 *start_iter = start;
1773 if (current_state && !new_state)
1775 /* End of an editable region. Delete it. */
1778 gtk_text_buffer_get_iter_at_mark (buffer, &start, start_mark);
1780 gtk_text_buffer_emit_delete (buffer, &start, &iter);
1782 /* It's more robust to ask for the state again then to assume that
1783 * we're on the next not-editable segment. We don't know what the
1784 * ::delete-range handler did.... maybe it deleted the following
1785 * not-editable segment because it was associated with the editable
1788 current_state = gtk_text_iter_editable (&iter, default_editable);
1789 deleted_stuff = TRUE;
1791 /* revalidate user's iterators. */
1792 *start_iter = start;
1797 /* We are at the start of an editable region. We won't be deleting
1798 * the previous region. Move start mark to start of this region.
1801 g_assert (!current_state && new_state);
1803 gtk_text_buffer_move_mark (buffer, start_mark, &iter);
1805 current_state = TRUE;
1812 gtk_text_buffer_delete_mark (buffer, start_mark);
1813 gtk_text_buffer_delete_mark (buffer, end_mark);
1815 gtk_text_buffer_end_user_action (buffer);
1817 return deleted_stuff;
1821 * Extracting textual buffer contents
1825 * gtk_text_buffer_get_text:
1826 * @buffer: a #GtkTextBuffer
1827 * @start: start of a range
1828 * @end: end of a range
1829 * @include_hidden_chars: whether to include invisible text
1831 * Returns the text in the range [@start,@end). Excludes undisplayed
1832 * text (text marked with tags that set the invisibility attribute) if
1833 * @include_hidden_chars is %FALSE. Does not include characters
1834 * representing embedded images, so byte and character indexes into
1835 * the returned string do <emphasis>not</emphasis> correspond to byte
1836 * and character indexes into the buffer. Contrast with
1837 * gtk_text_buffer_get_slice().
1839 * Return value: an allocated UTF-8 string
1842 gtk_text_buffer_get_text (GtkTextBuffer *buffer,
1843 const GtkTextIter *start,
1844 const GtkTextIter *end,
1845 gboolean include_hidden_chars)
1847 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
1848 g_return_val_if_fail (start != NULL, NULL);
1849 g_return_val_if_fail (end != NULL, NULL);
1850 g_return_val_if_fail (gtk_text_iter_get_buffer (start) == buffer, NULL);
1851 g_return_val_if_fail (gtk_text_iter_get_buffer (end) == buffer, NULL);
1853 if (include_hidden_chars)
1854 return gtk_text_iter_get_text (start, end);
1856 return gtk_text_iter_get_visible_text (start, end);
1860 * gtk_text_buffer_get_slice:
1861 * @buffer: a #GtkTextBuffer
1862 * @start: start of a range
1863 * @end: end of a range
1864 * @include_hidden_chars: whether to include invisible text
1866 * Returns the text in the range [@start,@end). Excludes undisplayed
1867 * text (text marked with tags that set the invisibility attribute) if
1868 * @include_hidden_chars is %FALSE. The returned string includes a
1869 * 0xFFFC character whenever the buffer contains
1870 * embedded images, so byte and character indexes into
1871 * the returned string <emphasis>do</emphasis> correspond to byte
1872 * and character indexes into the buffer. Contrast with
1873 * gtk_text_buffer_get_text(). Note that 0xFFFC can occur in normal
1874 * text as well, so it is not a reliable indicator that a pixbuf or
1875 * widget is in the buffer.
1877 * Return value: an allocated UTF-8 string
1880 gtk_text_buffer_get_slice (GtkTextBuffer *buffer,
1881 const GtkTextIter *start,
1882 const GtkTextIter *end,
1883 gboolean include_hidden_chars)
1885 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
1886 g_return_val_if_fail (start != NULL, NULL);
1887 g_return_val_if_fail (end != NULL, NULL);
1888 g_return_val_if_fail (gtk_text_iter_get_buffer (start) == buffer, NULL);
1889 g_return_val_if_fail (gtk_text_iter_get_buffer (end) == buffer, NULL);
1891 if (include_hidden_chars)
1892 return gtk_text_iter_get_slice (start, end);
1894 return gtk_text_iter_get_visible_slice (start, end);
1902 gtk_text_buffer_real_insert_pixbuf (GtkTextBuffer *buffer,
1906 _gtk_text_btree_insert_pixbuf (iter, pixbuf);
1908 g_signal_emit (buffer, signals[CHANGED], 0);
1912 * gtk_text_buffer_insert_pixbuf:
1913 * @buffer: a #GtkTextBuffer
1914 * @iter: location to insert the pixbuf
1915 * @pixbuf: a #GdkPixbuf
1917 * Inserts an image into the text buffer at @iter. The image will be
1918 * counted as one character in character counts, and when obtaining
1919 * the buffer contents as a string, will be represented by the Unicode
1920 * "object replacement character" 0xFFFC. Note that the "slice"
1921 * variants for obtaining portions of the buffer as a string include
1922 * this character for pixbufs, but the "text" variants do
1923 * not. e.g. see gtk_text_buffer_get_slice() and
1924 * gtk_text_buffer_get_text().
1927 gtk_text_buffer_insert_pixbuf (GtkTextBuffer *buffer,
1931 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1932 g_return_if_fail (iter != NULL);
1933 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
1934 g_return_if_fail (gtk_text_iter_get_buffer (iter) == buffer);
1936 g_signal_emit (buffer, signals[INSERT_PIXBUF], 0,
1946 gtk_text_buffer_real_insert_anchor (GtkTextBuffer *buffer,
1948 GtkTextChildAnchor *anchor)
1950 _gtk_text_btree_insert_child_anchor (iter, anchor);
1952 g_signal_emit (buffer, signals[CHANGED], 0);
1956 * gtk_text_buffer_insert_child_anchor:
1957 * @buffer: a #GtkTextBuffer
1958 * @iter: location to insert the anchor
1959 * @anchor: a #GtkTextChildAnchor
1961 * Inserts a child widget anchor into the text buffer at @iter. The
1962 * anchor will be counted as one character in character counts, and
1963 * when obtaining the buffer contents as a string, will be represented
1964 * by the Unicode "object replacement character" 0xFFFC. Note that the
1965 * "slice" variants for obtaining portions of the buffer as a string
1966 * include this character for child anchors, but the "text" variants do
1967 * not. E.g. see gtk_text_buffer_get_slice() and
1968 * gtk_text_buffer_get_text(). Consider
1969 * gtk_text_buffer_create_child_anchor() as a more convenient
1970 * alternative to this function. The buffer will add a reference to
1971 * the anchor, so you can unref it after insertion.
1974 gtk_text_buffer_insert_child_anchor (GtkTextBuffer *buffer,
1976 GtkTextChildAnchor *anchor)
1978 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1979 g_return_if_fail (iter != NULL);
1980 g_return_if_fail (GTK_IS_TEXT_CHILD_ANCHOR (anchor));
1981 g_return_if_fail (gtk_text_iter_get_buffer (iter) == buffer);
1983 g_signal_emit (buffer, signals[INSERT_CHILD_ANCHOR], 0,
1988 * gtk_text_buffer_create_child_anchor:
1989 * @buffer: a #GtkTextBuffer
1990 * @iter: location in the buffer
1992 * This is a convenience function which simply creates a child anchor
1993 * with gtk_text_child_anchor_new() and inserts it into the buffer
1994 * with gtk_text_buffer_insert_child_anchor(). The new anchor is
1995 * owned by the buffer; no reference count is returned to
1996 * the caller of gtk_text_buffer_create_child_anchor().
1998 * Return value: (transfer none): the created child anchor
2001 gtk_text_buffer_create_child_anchor (GtkTextBuffer *buffer,
2004 GtkTextChildAnchor *anchor;
2006 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
2007 g_return_val_if_fail (iter != NULL, NULL);
2008 g_return_val_if_fail (gtk_text_iter_get_buffer (iter) == buffer, NULL);
2010 anchor = gtk_text_child_anchor_new ();
2012 gtk_text_buffer_insert_child_anchor (buffer, iter, anchor);
2014 g_object_unref (anchor);
2024 gtk_text_buffer_mark_set (GtkTextBuffer *buffer,
2025 const GtkTextIter *location,
2028 /* IMO this should NOT work like insert_text and delete_range,
2029 * where the real action happens in the default handler.
2031 * The reason is that the default handler would be _required_,
2032 * i.e. the whole widget would start breaking and segfaulting if the
2033 * default handler didn't get run. So you can't really override the
2034 * default handler or stop the emission; that is, this signal is
2035 * purely for notification, and not to allow users to modify the
2039 g_object_ref (mark);
2041 g_signal_emit (buffer,
2047 g_object_unref (mark);
2051 * gtk_text_buffer_set_mark:
2052 * @buffer: a #GtkTextBuffer
2053 * @mark_name: name of the mark
2054 * @iter: location for the mark
2055 * @left_gravity: if the mark is created by this function, gravity for
2057 * @should_exist: if %TRUE, warn if the mark does not exist, and return
2060 * Move the mark to the given position, if not @should_exist,
2063 * Return value: mark
2066 gtk_text_buffer_set_mark (GtkTextBuffer *buffer,
2067 GtkTextMark *existing_mark,
2068 const gchar *mark_name,
2069 const GtkTextIter *iter,
2070 gboolean left_gravity,
2071 gboolean should_exist)
2073 GtkTextIter location;
2076 g_return_val_if_fail (gtk_text_iter_get_buffer (iter) == buffer, NULL);
2078 mark = _gtk_text_btree_set_mark (get_btree (buffer),
2085 _gtk_text_btree_get_iter_at_mark (get_btree (buffer),
2089 gtk_text_buffer_mark_set (buffer, &location, mark);
2095 * gtk_text_buffer_create_mark:
2096 * @buffer: a #GtkTextBuffer
2097 * @mark_name: (allow-none): name for mark, or %NULL
2098 * @where: location to place mark
2099 * @left_gravity: whether the mark has left gravity
2101 * Creates a mark at position @where. If @mark_name is %NULL, the mark
2102 * is anonymous; otherwise, the mark can be retrieved by name using
2103 * gtk_text_buffer_get_mark(). If a mark has left gravity, and text is
2104 * inserted at the mark's current location, the mark will be moved to
2105 * the left of the newly-inserted text. If the mark has right gravity
2106 * (@left_gravity = %FALSE), the mark will end up on the right of
2107 * newly-inserted text. The standard left-to-right cursor is a mark
2108 * with right gravity (when you type, the cursor stays on the right
2109 * side of the text you're typing).
2111 * The caller of this function does <emphasis>not</emphasis> own a
2112 * reference to the returned #GtkTextMark, so you can ignore the
2113 * return value if you like. Marks are owned by the buffer and go
2114 * away when the buffer does.
2116 * Emits the "mark-set" signal as notification of the mark's initial
2119 * Return value: (transfer none): the new #GtkTextMark object
2122 gtk_text_buffer_create_mark (GtkTextBuffer *buffer,
2123 const gchar *mark_name,
2124 const GtkTextIter *where,
2125 gboolean left_gravity)
2127 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
2129 return gtk_text_buffer_set_mark (buffer, NULL, mark_name, where,
2130 left_gravity, FALSE);
2134 * gtk_text_buffer_add_mark:
2135 * @buffer: a #GtkTextBuffer
2136 * @mark: the mark to add
2137 * @where: location to place mark
2139 * Adds the mark at position @where. The mark must not be added to
2140 * another buffer, and if its name is not %NULL then there must not
2141 * be another mark in the buffer with the same name.
2143 * Emits the "mark-set" signal as notification of the mark's initial
2149 gtk_text_buffer_add_mark (GtkTextBuffer *buffer,
2151 const GtkTextIter *where)
2155 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2156 g_return_if_fail (GTK_IS_TEXT_MARK (mark));
2157 g_return_if_fail (where != NULL);
2158 g_return_if_fail (gtk_text_mark_get_buffer (mark) == NULL);
2160 name = gtk_text_mark_get_name (mark);
2162 if (name != NULL && gtk_text_buffer_get_mark (buffer, name) != NULL)
2164 g_critical ("Mark %s already exists in the buffer", name);
2168 gtk_text_buffer_set_mark (buffer, mark, NULL, where, FALSE, FALSE);
2172 * gtk_text_buffer_move_mark:
2173 * @buffer: a #GtkTextBuffer
2174 * @mark: a #GtkTextMark
2175 * @where: new location for @mark in @buffer
2177 * Moves @mark to the new location @where. Emits the "mark-set" signal
2178 * as notification of the move.
2181 gtk_text_buffer_move_mark (GtkTextBuffer *buffer,
2183 const GtkTextIter *where)
2185 g_return_if_fail (GTK_IS_TEXT_MARK (mark));
2186 g_return_if_fail (!gtk_text_mark_get_deleted (mark));
2187 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2189 gtk_text_buffer_set_mark (buffer, mark, NULL, where, FALSE, TRUE);
2193 * gtk_text_buffer_get_iter_at_mark:
2194 * @buffer: a #GtkTextBuffer
2195 * @iter: (out): iterator to initialize
2196 * @mark: a #GtkTextMark in @buffer
2198 * Initializes @iter with the current position of @mark.
2201 gtk_text_buffer_get_iter_at_mark (GtkTextBuffer *buffer,
2205 g_return_if_fail (GTK_IS_TEXT_MARK (mark));
2206 g_return_if_fail (!gtk_text_mark_get_deleted (mark));
2207 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2209 _gtk_text_btree_get_iter_at_mark (get_btree (buffer),
2215 * gtk_text_buffer_delete_mark:
2216 * @buffer: a #GtkTextBuffer
2217 * @mark: a #GtkTextMark in @buffer
2219 * Deletes @mark, so that it's no longer located anywhere in the
2220 * buffer. Removes the reference the buffer holds to the mark, so if
2221 * you haven't called g_object_ref() on the mark, it will be freed. Even
2222 * if the mark isn't freed, most operations on @mark become
2223 * invalid, until it gets added to a buffer again with
2224 * gtk_text_buffer_add_mark(). Use gtk_text_mark_get_deleted() to
2225 * find out if a mark has been removed from its buffer.
2226 * The "mark-deleted" signal will be emitted as notification after
2227 * the mark is deleted.
2230 gtk_text_buffer_delete_mark (GtkTextBuffer *buffer,
2233 g_return_if_fail (GTK_IS_TEXT_MARK (mark));
2234 g_return_if_fail (!gtk_text_mark_get_deleted (mark));
2235 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2237 g_object_ref (mark);
2239 _gtk_text_btree_remove_mark (get_btree (buffer), mark);
2241 /* See rationale above for MARK_SET on why we emit this after
2242 * removing the mark, rather than removing the mark in a default
2245 g_signal_emit (buffer, signals[MARK_DELETED],
2249 g_object_unref (mark);
2253 * gtk_text_buffer_get_mark:
2254 * @buffer: a #GtkTextBuffer
2255 * @name: a mark name
2257 * Returns the mark named @name in buffer @buffer, or %NULL if no such
2258 * mark exists in the buffer.
2260 * Return value: (transfer none): a #GtkTextMark, or %NULL
2263 gtk_text_buffer_get_mark (GtkTextBuffer *buffer,
2268 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
2269 g_return_val_if_fail (name != NULL, NULL);
2271 mark = _gtk_text_btree_get_mark_by_name (get_btree (buffer), name);
2277 * gtk_text_buffer_move_mark_by_name:
2278 * @buffer: a #GtkTextBuffer
2279 * @name: name of a mark
2280 * @where: new location for mark
2282 * Moves the mark named @name (which must exist) to location @where.
2283 * See gtk_text_buffer_move_mark() for details.
2286 gtk_text_buffer_move_mark_by_name (GtkTextBuffer *buffer,
2288 const GtkTextIter *where)
2292 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2293 g_return_if_fail (name != NULL);
2295 mark = _gtk_text_btree_get_mark_by_name (get_btree (buffer), name);
2299 g_warning ("%s: no mark named '%s'", G_STRLOC, name);
2303 gtk_text_buffer_move_mark (buffer, mark, where);
2307 * gtk_text_buffer_delete_mark_by_name:
2308 * @buffer: a #GtkTextBuffer
2309 * @name: name of a mark in @buffer
2311 * Deletes the mark named @name; the mark must exist. See
2312 * gtk_text_buffer_delete_mark() for details.
2315 gtk_text_buffer_delete_mark_by_name (GtkTextBuffer *buffer,
2320 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2321 g_return_if_fail (name != NULL);
2323 mark = _gtk_text_btree_get_mark_by_name (get_btree (buffer), name);
2327 g_warning ("%s: no mark named '%s'", G_STRLOC, name);
2331 gtk_text_buffer_delete_mark (buffer, mark);
2335 * gtk_text_buffer_get_insert:
2336 * @buffer: a #GtkTextBuffer
2338 * Returns the mark that represents the cursor (insertion point).
2339 * Equivalent to calling gtk_text_buffer_get_mark() to get the mark
2340 * named "insert", but very slightly more efficient, and involves less
2343 * Return value: (transfer none): insertion point mark
2346 gtk_text_buffer_get_insert (GtkTextBuffer *buffer)
2348 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
2350 return _gtk_text_btree_get_insert (get_btree (buffer));
2354 * gtk_text_buffer_get_selection_bound:
2355 * @buffer: a #GtkTextBuffer
2357 * Returns the mark that represents the selection bound. Equivalent
2358 * to calling gtk_text_buffer_get_mark() to get the mark named
2359 * "selection_bound", but very slightly more efficient, and involves
2362 * The currently-selected text in @buffer is the region between the
2363 * "selection_bound" and "insert" marks. If "selection_bound" and
2364 * "insert" are in the same place, then there is no current selection.
2365 * gtk_text_buffer_get_selection_bounds() is another convenient function
2366 * for handling the selection, if you just want to know whether there's a
2367 * selection and what its bounds are.
2369 * Return value: (transfer none): selection bound mark
2372 gtk_text_buffer_get_selection_bound (GtkTextBuffer *buffer)
2374 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
2376 return _gtk_text_btree_get_selection_bound (get_btree (buffer));
2380 * gtk_text_buffer_get_iter_at_child_anchor:
2381 * @buffer: a #GtkTextBuffer
2382 * @iter: (out): an iterator to be initialized
2383 * @anchor: a child anchor that appears in @buffer
2385 * Obtains the location of @anchor within @buffer.
2388 gtk_text_buffer_get_iter_at_child_anchor (GtkTextBuffer *buffer,
2390 GtkTextChildAnchor *anchor)
2392 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2393 g_return_if_fail (iter != NULL);
2394 g_return_if_fail (GTK_IS_TEXT_CHILD_ANCHOR (anchor));
2395 g_return_if_fail (!gtk_text_child_anchor_get_deleted (anchor));
2397 _gtk_text_btree_get_iter_at_child_anchor (get_btree (buffer),
2403 * gtk_text_buffer_place_cursor:
2404 * @buffer: a #GtkTextBuffer
2405 * @where: where to put the cursor
2407 * This function moves the "insert" and "selection_bound" marks
2408 * simultaneously. If you move them to the same place in two steps
2409 * with gtk_text_buffer_move_mark(), you will temporarily select a
2410 * region in between their old and new locations, which can be pretty
2411 * inefficient since the temporarily-selected region will force stuff
2412 * to be recalculated. This function moves them as a unit, which can
2416 gtk_text_buffer_place_cursor (GtkTextBuffer *buffer,
2417 const GtkTextIter *where)
2419 gtk_text_buffer_select_range (buffer, where, where);
2423 * gtk_text_buffer_select_range:
2424 * @buffer: a #GtkTextBuffer
2425 * @ins: where to put the "insert" mark
2426 * @bound: where to put the "selection_bound" mark
2428 * This function moves the "insert" and "selection_bound" marks
2429 * simultaneously. If you move them in two steps
2430 * with gtk_text_buffer_move_mark(), you will temporarily select a
2431 * region in between their old and new locations, which can be pretty
2432 * inefficient since the temporarily-selected region will force stuff
2433 * to be recalculated. This function moves them as a unit, which can
2439 gtk_text_buffer_select_range (GtkTextBuffer *buffer,
2440 const GtkTextIter *ins,
2441 const GtkTextIter *bound)
2443 GtkTextIter real_ins;
2444 GtkTextIter real_bound;
2446 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2449 real_bound = *bound;
2451 _gtk_text_btree_select_range (get_btree (buffer), &real_ins, &real_bound);
2452 gtk_text_buffer_mark_set (buffer, &real_ins,
2453 gtk_text_buffer_get_insert (buffer));
2454 gtk_text_buffer_mark_set (buffer, &real_bound,
2455 gtk_text_buffer_get_selection_bound (buffer));
2463 * gtk_text_buffer_create_tag:
2464 * @buffer: a #GtkTextBuffer
2465 * @tag_name: (allow-none): name of the new tag, or %NULL
2466 * @first_property_name: (allow-none): name of first property to set, or %NULL
2467 * @...: %NULL-terminated list of property names and values
2469 * Creates a tag and adds it to the tag table for @buffer.
2470 * Equivalent to calling gtk_text_tag_new() and then adding the
2471 * tag to the buffer's tag table. The returned tag is owned by
2472 * the buffer's tag table, so the ref count will be equal to one.
2474 * If @tag_name is %NULL, the tag is anonymous.
2476 * If @tag_name is non-%NULL, a tag called @tag_name must not already
2477 * exist in the tag table for this buffer.
2479 * The @first_property_name argument and subsequent arguments are a list
2480 * of properties to set on the tag, as with g_object_set().
2482 * Return value: (transfer none): a new tag
2485 gtk_text_buffer_create_tag (GtkTextBuffer *buffer,
2486 const gchar *tag_name,
2487 const gchar *first_property_name,
2493 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
2495 tag = gtk_text_tag_new (tag_name);
2497 gtk_text_tag_table_add (get_table (buffer), tag);
2499 if (first_property_name)
2501 va_start (list, first_property_name);
2502 g_object_set_valist (G_OBJECT (tag), first_property_name, list);
2506 g_object_unref (tag);
2512 gtk_text_buffer_real_apply_tag (GtkTextBuffer *buffer,
2514 const GtkTextIter *start,
2515 const GtkTextIter *end)
2517 if (tag->priv->table != buffer->priv->tag_table)
2519 g_warning ("Can only apply tags that are in the tag table for the buffer");
2523 _gtk_text_btree_tag (start, end, tag, TRUE);
2527 gtk_text_buffer_real_remove_tag (GtkTextBuffer *buffer,
2529 const GtkTextIter *start,
2530 const GtkTextIter *end)
2532 if (tag->priv->table != buffer->priv->tag_table)
2534 g_warning ("Can only remove tags that are in the tag table for the buffer");
2538 _gtk_text_btree_tag (start, end, tag, FALSE);
2542 gtk_text_buffer_real_changed (GtkTextBuffer *buffer)
2544 gtk_text_buffer_set_modified (buffer, TRUE);
2548 gtk_text_buffer_real_mark_set (GtkTextBuffer *buffer,
2549 const GtkTextIter *iter,
2552 GtkTextMark *insert;
2554 insert = gtk_text_buffer_get_insert (buffer);
2556 if (mark == insert || mark == gtk_text_buffer_get_selection_bound (buffer))
2558 gboolean has_selection;
2560 update_selection_clipboards (buffer);
2562 has_selection = gtk_text_buffer_get_selection_bounds (buffer,
2566 if (has_selection != buffer->priv->has_selection)
2568 buffer->priv->has_selection = has_selection;
2569 g_object_notify (G_OBJECT (buffer), "has-selection");
2574 g_object_notify (G_OBJECT (buffer), "cursor-position");
2578 gtk_text_buffer_emit_tag (GtkTextBuffer *buffer,
2581 const GtkTextIter *start,
2582 const GtkTextIter *end)
2584 GtkTextIter start_tmp = *start;
2585 GtkTextIter end_tmp = *end;
2587 g_return_if_fail (tag != NULL);
2589 gtk_text_iter_order (&start_tmp, &end_tmp);
2592 g_signal_emit (buffer, signals[APPLY_TAG],
2594 tag, &start_tmp, &end_tmp);
2596 g_signal_emit (buffer, signals[REMOVE_TAG],
2598 tag, &start_tmp, &end_tmp);
2602 * gtk_text_buffer_apply_tag:
2603 * @buffer: a #GtkTextBuffer
2604 * @tag: a #GtkTextTag
2605 * @start: one bound of range to be tagged
2606 * @end: other bound of range to be tagged
2608 * Emits the "apply-tag" signal on @buffer. The default
2609 * handler for the signal applies @tag to the given range.
2610 * @start and @end do not have to be in order.
2613 gtk_text_buffer_apply_tag (GtkTextBuffer *buffer,
2615 const GtkTextIter *start,
2616 const GtkTextIter *end)
2618 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2619 g_return_if_fail (GTK_IS_TEXT_TAG (tag));
2620 g_return_if_fail (start != NULL);
2621 g_return_if_fail (end != NULL);
2622 g_return_if_fail (gtk_text_iter_get_buffer (start) == buffer);
2623 g_return_if_fail (gtk_text_iter_get_buffer (end) == buffer);
2624 g_return_if_fail (tag->priv->table == buffer->priv->tag_table);
2626 gtk_text_buffer_emit_tag (buffer, tag, TRUE, start, end);
2630 * gtk_text_buffer_remove_tag:
2631 * @buffer: a #GtkTextBuffer
2632 * @tag: a #GtkTextTag
2633 * @start: one bound of range to be untagged
2634 * @end: other bound of range to be untagged
2636 * Emits the "remove-tag" signal. The default handler for the signal
2637 * removes all occurrences of @tag from the given range. @start and
2638 * @end don't have to be in order.
2641 gtk_text_buffer_remove_tag (GtkTextBuffer *buffer,
2643 const GtkTextIter *start,
2644 const GtkTextIter *end)
2647 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2648 g_return_if_fail (GTK_IS_TEXT_TAG (tag));
2649 g_return_if_fail (start != NULL);
2650 g_return_if_fail (end != NULL);
2651 g_return_if_fail (gtk_text_iter_get_buffer (start) == buffer);
2652 g_return_if_fail (gtk_text_iter_get_buffer (end) == buffer);
2653 g_return_if_fail (tag->priv->table == buffer->priv->tag_table);
2655 gtk_text_buffer_emit_tag (buffer, tag, FALSE, start, end);
2659 * gtk_text_buffer_apply_tag_by_name:
2660 * @buffer: a #GtkTextBuffer
2661 * @name: name of a named #GtkTextTag
2662 * @start: one bound of range to be tagged
2663 * @end: other bound of range to be tagged
2665 * Calls gtk_text_tag_table_lookup() on the buffer's tag table to
2666 * get a #GtkTextTag, then calls gtk_text_buffer_apply_tag().
2669 gtk_text_buffer_apply_tag_by_name (GtkTextBuffer *buffer,
2671 const GtkTextIter *start,
2672 const GtkTextIter *end)
2676 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2677 g_return_if_fail (name != NULL);
2678 g_return_if_fail (start != NULL);
2679 g_return_if_fail (end != NULL);
2680 g_return_if_fail (gtk_text_iter_get_buffer (start) == buffer);
2681 g_return_if_fail (gtk_text_iter_get_buffer (end) == buffer);
2683 tag = gtk_text_tag_table_lookup (get_table (buffer),
2688 g_warning ("Unknown tag `%s'", name);
2692 gtk_text_buffer_emit_tag (buffer, tag, TRUE, start, end);
2696 * gtk_text_buffer_remove_tag_by_name:
2697 * @buffer: a #GtkTextBuffer
2698 * @name: name of a #GtkTextTag
2699 * @start: one bound of range to be untagged
2700 * @end: other bound of range to be untagged
2702 * Calls gtk_text_tag_table_lookup() on the buffer's tag table to
2703 * get a #GtkTextTag, then calls gtk_text_buffer_remove_tag().
2706 gtk_text_buffer_remove_tag_by_name (GtkTextBuffer *buffer,
2708 const GtkTextIter *start,
2709 const GtkTextIter *end)
2713 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2714 g_return_if_fail (name != NULL);
2715 g_return_if_fail (start != NULL);
2716 g_return_if_fail (end != NULL);
2717 g_return_if_fail (gtk_text_iter_get_buffer (start) == buffer);
2718 g_return_if_fail (gtk_text_iter_get_buffer (end) == buffer);
2720 tag = gtk_text_tag_table_lookup (get_table (buffer),
2725 g_warning ("Unknown tag `%s'", name);
2729 gtk_text_buffer_emit_tag (buffer, tag, FALSE, start, end);
2733 pointer_cmp (gconstpointer a,
2745 * gtk_text_buffer_remove_all_tags:
2746 * @buffer: a #GtkTextBuffer
2747 * @start: one bound of range to be untagged
2748 * @end: other bound of range to be untagged
2750 * Removes all tags in the range between @start and @end. Be careful
2751 * with this function; it could remove tags added in code unrelated to
2752 * the code you're currently writing. That is, using this function is
2753 * probably a bad idea if you have two or more unrelated code sections
2757 gtk_text_buffer_remove_all_tags (GtkTextBuffer *buffer,
2758 const GtkTextIter *start,
2759 const GtkTextIter *end)
2761 GtkTextIter first, second, tmp;
2764 GSList *prev, *next;
2767 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2768 g_return_if_fail (start != NULL);
2769 g_return_if_fail (end != NULL);
2770 g_return_if_fail (gtk_text_iter_get_buffer (start) == buffer);
2771 g_return_if_fail (gtk_text_iter_get_buffer (end) == buffer);
2776 gtk_text_iter_order (&first, &second);
2778 /* Get all tags turned on at the start */
2779 tags = gtk_text_iter_get_tags (&first);
2781 /* Find any that are toggled on within the range */
2783 while (gtk_text_iter_forward_to_tag_toggle (&tmp, NULL))
2788 if (gtk_text_iter_compare (&tmp, &second) >= 0)
2789 break; /* past the end of the range */
2791 toggled = gtk_text_iter_get_toggled_tags (&tmp, TRUE);
2793 /* We could end up with a really big-ass list here.
2796 tmp_list2 = toggled;
2797 while (tmp_list2 != NULL)
2799 tags = g_slist_prepend (tags, tmp_list2->data);
2801 tmp_list2 = g_slist_next (tmp_list2);
2804 g_slist_free (toggled);
2808 tags = g_slist_sort (tags, pointer_cmp);
2810 /* Strip duplicates */
2814 while (tmp_list != NULL)
2816 if (tag == tmp_list->data)
2819 next = tmp_list->next;
2823 tmp_list->next = NULL;
2825 g_slist_free (tmp_list);
2828 /* prev is unchanged */
2832 /* not a duplicate */
2833 tag = GTK_TEXT_TAG (tmp_list->data);
2835 tmp_list = tmp_list->next;
2839 g_slist_foreach (tags, (GFunc) g_object_ref, NULL);
2842 while (tmp_list != NULL)
2844 tag = GTK_TEXT_TAG (tmp_list->data);
2846 gtk_text_buffer_remove_tag (buffer, tag, &first, &second);
2848 tmp_list = tmp_list->next;
2851 g_slist_foreach (tags, (GFunc) g_object_unref, NULL);
2853 g_slist_free (tags);
2858 * Obtain various iterators
2862 * gtk_text_buffer_get_iter_at_line_offset:
2863 * @buffer: a #GtkTextBuffer
2864 * @iter: (out): iterator to initialize
2865 * @line_number: line number counting from 0
2866 * @char_offset: char offset from start of line
2868 * Obtains an iterator pointing to @char_offset within the given
2869 * line. The @char_offset must exist, offsets off the end of the line
2870 * are not allowed. Note <emphasis>characters</emphasis>, not bytes;
2871 * UTF-8 may encode one character as multiple bytes.
2874 gtk_text_buffer_get_iter_at_line_offset (GtkTextBuffer *buffer,
2879 g_return_if_fail (iter != NULL);
2880 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2882 _gtk_text_btree_get_iter_at_line_char (get_btree (buffer),
2883 iter, line_number, char_offset);
2887 * gtk_text_buffer_get_iter_at_line_index:
2888 * @buffer: a #GtkTextBuffer
2889 * @iter: (out): iterator to initialize
2890 * @line_number: line number counting from 0
2891 * @byte_index: byte index from start of line
2893 * Obtains an iterator pointing to @byte_index within the given line.
2894 * @byte_index must be the start of a UTF-8 character, and must not be
2895 * beyond the end of the line. Note <emphasis>bytes</emphasis>, not
2896 * characters; UTF-8 may encode one character as multiple bytes.
2899 gtk_text_buffer_get_iter_at_line_index (GtkTextBuffer *buffer,
2904 g_return_if_fail (iter != NULL);
2905 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2907 _gtk_text_btree_get_iter_at_line_byte (get_btree (buffer),
2908 iter, line_number, byte_index);
2912 * gtk_text_buffer_get_iter_at_line:
2913 * @buffer: a #GtkTextBuffer
2914 * @iter: (out): iterator to initialize
2915 * @line_number: line number counting from 0
2917 * Initializes @iter to the start of the given line.
2920 gtk_text_buffer_get_iter_at_line (GtkTextBuffer *buffer,
2924 g_return_if_fail (iter != NULL);
2925 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2927 gtk_text_buffer_get_iter_at_line_offset (buffer, iter, line_number, 0);
2931 * gtk_text_buffer_get_iter_at_offset:
2932 * @buffer: a #GtkTextBuffer
2933 * @iter: (out): iterator to initialize
2934 * @char_offset: char offset from start of buffer, counting from 0, or -1
2936 * Initializes @iter to a position @char_offset chars from the start
2937 * of the entire buffer. If @char_offset is -1 or greater than the number
2938 * of characters in the buffer, @iter is initialized to the end iterator,
2939 * the iterator one past the last valid character in the buffer.
2942 gtk_text_buffer_get_iter_at_offset (GtkTextBuffer *buffer,
2946 g_return_if_fail (iter != NULL);
2947 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2949 _gtk_text_btree_get_iter_at_char (get_btree (buffer), iter, char_offset);
2953 * gtk_text_buffer_get_start_iter:
2954 * @buffer: a #GtkTextBuffer
2955 * @iter: (out): iterator to initialize
2957 * Initialized @iter with the first position in the text buffer. This
2958 * is the same as using gtk_text_buffer_get_iter_at_offset() to get
2959 * the iter at character offset 0.
2962 gtk_text_buffer_get_start_iter (GtkTextBuffer *buffer,
2965 g_return_if_fail (iter != NULL);
2966 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2968 _gtk_text_btree_get_iter_at_char (get_btree (buffer), iter, 0);
2972 * gtk_text_buffer_get_end_iter:
2973 * @buffer: a #GtkTextBuffer
2974 * @iter: (out): iterator to initialize
2976 * Initializes @iter with the "end iterator," one past the last valid
2977 * character in the text buffer. If dereferenced with
2978 * gtk_text_iter_get_char(), the end iterator has a character value of
2979 * 0. The entire buffer lies in the range from the first position in
2980 * the buffer (call gtk_text_buffer_get_start_iter() to get
2981 * character position 0) to the end iterator.
2984 gtk_text_buffer_get_end_iter (GtkTextBuffer *buffer,
2987 g_return_if_fail (iter != NULL);
2988 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2990 _gtk_text_btree_get_end_iter (get_btree (buffer), iter);
2994 * gtk_text_buffer_get_bounds:
2995 * @buffer: a #GtkTextBuffer
2996 * @start: (out): iterator to initialize with first position in the buffer
2997 * @end: (out): iterator to initialize with the end iterator
2999 * Retrieves the first and last iterators in the buffer, i.e. the
3000 * entire buffer lies within the range [@start,@end).
3003 gtk_text_buffer_get_bounds (GtkTextBuffer *buffer,
3007 g_return_if_fail (start != NULL);
3008 g_return_if_fail (end != NULL);
3009 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
3011 _gtk_text_btree_get_iter_at_char (get_btree (buffer), start, 0);
3012 _gtk_text_btree_get_end_iter (get_btree (buffer), end);
3020 * gtk_text_buffer_get_modified:
3021 * @buffer: a #GtkTextBuffer
3023 * Indicates whether the buffer has been modified since the last call
3024 * to gtk_text_buffer_set_modified() set the modification flag to
3025 * %FALSE. Used for example to enable a "save" function in a text
3028 * Return value: %TRUE if the buffer has been modified
3031 gtk_text_buffer_get_modified (GtkTextBuffer *buffer)
3033 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
3035 return buffer->priv->modified;
3039 * gtk_text_buffer_set_modified:
3040 * @buffer: a #GtkTextBuffer
3041 * @setting: modification flag setting
3043 * Used to keep track of whether the buffer has been modified since the
3044 * last time it was saved. Whenever the buffer is saved to disk, call
3045 * gtk_text_buffer_set_modified (@buffer, FALSE). When the buffer is modified,
3046 * it will automatically toggled on the modified bit again. When the modified
3047 * bit flips, the buffer emits a "modified-changed" signal.
3050 gtk_text_buffer_set_modified (GtkTextBuffer *buffer,
3053 gboolean fixed_setting;
3055 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
3057 fixed_setting = setting != FALSE;
3059 if (buffer->priv->modified == fixed_setting)
3063 buffer->priv->modified = fixed_setting;
3064 g_signal_emit (buffer, signals[MODIFIED_CHANGED], 0);
3069 * gtk_text_buffer_get_has_selection:
3070 * @buffer: a #GtkTextBuffer
3072 * Indicates whether the buffer has some text currently selected.
3074 * Return value: %TRUE if the there is text selected
3079 gtk_text_buffer_get_has_selection (GtkTextBuffer *buffer)
3081 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
3083 return buffer->priv->has_selection;
3088 * Assorted other stuff
3092 * gtk_text_buffer_get_line_count:
3093 * @buffer: a #GtkTextBuffer
3095 * Obtains the number of lines in the buffer. This value is cached, so
3096 * the function is very fast.
3098 * Return value: number of lines in the buffer
3101 gtk_text_buffer_get_line_count (GtkTextBuffer *buffer)
3103 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), 0);
3105 return _gtk_text_btree_line_count (get_btree (buffer));
3109 * gtk_text_buffer_get_char_count:
3110 * @buffer: a #GtkTextBuffer
3112 * Gets the number of characters in the buffer; note that characters
3113 * and bytes are not the same, you can't e.g. expect the contents of
3114 * the buffer in string form to be this many bytes long. The character
3115 * count is cached, so this function is very fast.
3117 * Return value: number of characters in the buffer
3120 gtk_text_buffer_get_char_count (GtkTextBuffer *buffer)
3122 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), 0);
3124 return _gtk_text_btree_char_count (get_btree (buffer));
3127 /* Called when we lose the primary selection.
3130 clipboard_clear_selection_cb (GtkClipboard *clipboard,
3133 /* Move selection_bound to the insertion point */
3135 GtkTextIter selection_bound;
3136 GtkTextBuffer *buffer = GTK_TEXT_BUFFER (data);
3138 gtk_text_buffer_get_iter_at_mark (buffer, &insert,
3139 gtk_text_buffer_get_insert (buffer));
3140 gtk_text_buffer_get_iter_at_mark (buffer, &selection_bound,
3141 gtk_text_buffer_get_selection_bound (buffer));
3143 if (!gtk_text_iter_equal (&insert, &selection_bound))
3144 gtk_text_buffer_move_mark (buffer,
3145 gtk_text_buffer_get_selection_bound (buffer),
3149 /* Called when we have the primary selection and someone else wants our
3150 * data in order to paste it.
3153 clipboard_get_selection_cb (GtkClipboard *clipboard,
3154 GtkSelectionData *selection_data,
3158 GtkTextBuffer *buffer = GTK_TEXT_BUFFER (data);
3159 GtkTextIter start, end;
3161 if (gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
3163 if (info == GTK_TEXT_BUFFER_TARGET_INFO_BUFFER_CONTENTS)
3165 /* Provide the address of the buffer; this will only be
3166 * used within-process
3168 gtk_selection_data_set (selection_data,
3169 gtk_selection_data_get_target (selection_data),
3174 else if (info == GTK_TEXT_BUFFER_TARGET_INFO_RICH_TEXT)
3179 str = gtk_text_buffer_serialize (buffer, buffer,
3180 gtk_selection_data_get_target (selection_data),
3181 &start, &end, &len);
3183 gtk_selection_data_set (selection_data,
3184 gtk_selection_data_get_target (selection_data),
3193 str = gtk_text_iter_get_visible_text (&start, &end);
3194 gtk_selection_data_set_text (selection_data, str, -1);
3200 static GtkTextBuffer *
3201 create_clipboard_contents_buffer (GtkTextBuffer *buffer)
3203 GtkTextBuffer *contents;
3205 contents = gtk_text_buffer_new (gtk_text_buffer_get_tag_table (buffer));
3207 g_object_set_data (G_OBJECT (contents), I_("gtk-text-buffer-clipboard-source"),
3209 g_object_set_data (G_OBJECT (contents), I_("gtk-text-buffer-clipboard"),
3210 GINT_TO_POINTER (1));
3212 /* Ref the source buffer as long as the clipboard contents buffer
3213 * exists, because it's needed for serializing the contents buffer.
3214 * See http://bugzilla.gnome.org/show_bug.cgi?id=339195
3216 g_object_ref (buffer);
3217 g_object_weak_ref (G_OBJECT (contents), (GWeakNotify) g_object_unref, buffer);
3222 /* Provide cut/copied data */
3224 clipboard_get_contents_cb (GtkClipboard *clipboard,
3225 GtkSelectionData *selection_data,
3229 GtkTextBuffer *contents = GTK_TEXT_BUFFER (data);
3231 g_assert (contents); /* This should never be called unless we own the clipboard */
3233 if (info == GTK_TEXT_BUFFER_TARGET_INFO_BUFFER_CONTENTS)
3235 /* Provide the address of the clipboard buffer; this will only
3236 * be used within-process. OK to supply a NULL value for contents.
3238 gtk_selection_data_set (selection_data,
3239 gtk_selection_data_get_target (selection_data),
3244 else if (info == GTK_TEXT_BUFFER_TARGET_INFO_RICH_TEXT)
3246 GtkTextBuffer *clipboard_source_buffer;
3247 GtkTextIter start, end;
3251 clipboard_source_buffer = g_object_get_data (G_OBJECT (contents),
3252 "gtk-text-buffer-clipboard-source");
3254 gtk_text_buffer_get_bounds (contents, &start, &end);
3256 str = gtk_text_buffer_serialize (clipboard_source_buffer, contents,
3257 gtk_selection_data_get_target (selection_data),
3258 &start, &end, &len);
3260 gtk_selection_data_set (selection_data,
3261 gtk_selection_data_get_target (selection_data),
3269 GtkTextIter start, end;
3271 gtk_text_buffer_get_bounds (contents, &start, &end);
3273 str = gtk_text_iter_get_visible_text (&start, &end);
3274 gtk_selection_data_set_text (selection_data, str, -1);
3280 clipboard_clear_contents_cb (GtkClipboard *clipboard,
3283 GtkTextBuffer *contents = GTK_TEXT_BUFFER (data);
3285 g_object_unref (contents);
3289 get_paste_point (GtkTextBuffer *buffer,
3291 gboolean clear_afterward)
3293 GtkTextIter insert_point;
3294 GtkTextMark *paste_point_override;
3296 paste_point_override = gtk_text_buffer_get_mark (buffer,
3297 "gtk_paste_point_override");
3299 if (paste_point_override != NULL)
3301 gtk_text_buffer_get_iter_at_mark (buffer, &insert_point,
3302 paste_point_override);
3303 if (clear_afterward)
3304 gtk_text_buffer_delete_mark (buffer, paste_point_override);
3308 gtk_text_buffer_get_iter_at_mark (buffer, &insert_point,
3309 gtk_text_buffer_get_insert (buffer));
3312 *iter = insert_point;
3316 pre_paste_prep (ClipboardRequest *request_data,
3317 GtkTextIter *insert_point)
3319 GtkTextBuffer *buffer = request_data->buffer;
3321 get_paste_point (buffer, insert_point, TRUE);
3323 /* If we're going to replace the selection, we insert before it to
3324 * avoid messing it up, then we delete the selection after inserting.
3326 if (request_data->replace_selection)
3328 GtkTextIter start, end;
3330 if (gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
3331 *insert_point = start;
3336 post_paste_cleanup (ClipboardRequest *request_data)
3338 if (request_data->replace_selection)
3340 GtkTextIter start, end;
3342 if (gtk_text_buffer_get_selection_bounds (request_data->buffer,
3345 if (request_data->interactive)
3346 gtk_text_buffer_delete_interactive (request_data->buffer,
3349 request_data->default_editable);
3351 gtk_text_buffer_delete (request_data->buffer, &start, &end);
3357 emit_paste_done (GtkTextBuffer *buffer,
3358 GtkClipboard *clipboard)
3360 g_signal_emit (buffer, signals[PASTE_DONE], 0, clipboard);
3364 free_clipboard_request (ClipboardRequest *request_data)
3366 g_object_unref (request_data->buffer);
3367 g_free (request_data);
3370 /* Called when we request a paste and receive the text data
3373 clipboard_text_received (GtkClipboard *clipboard,
3377 ClipboardRequest *request_data = data;
3378 GtkTextBuffer *buffer = request_data->buffer;
3382 GtkTextIter insert_point;
3384 if (request_data->interactive)
3385 gtk_text_buffer_begin_user_action (buffer);
3387 pre_paste_prep (request_data, &insert_point);
3389 if (request_data->interactive)
3390 gtk_text_buffer_insert_interactive (buffer, &insert_point,
3391 str, -1, request_data->default_editable);
3393 gtk_text_buffer_insert (buffer, &insert_point,
3396 post_paste_cleanup (request_data);
3398 if (request_data->interactive)
3399 gtk_text_buffer_end_user_action (buffer);
3401 emit_paste_done (buffer, clipboard);
3405 /* It may happen that we set a point override but we are not inserting
3406 any text, so we must remove it afterwards */
3407 GtkTextMark *paste_point_override;
3409 paste_point_override = gtk_text_buffer_get_mark (buffer,
3410 "gtk_paste_point_override");
3412 if (paste_point_override != NULL)
3413 gtk_text_buffer_delete_mark (buffer, paste_point_override);
3416 free_clipboard_request (request_data);
3419 static GtkTextBuffer*
3420 selection_data_get_buffer (GtkSelectionData *selection_data,
3421 ClipboardRequest *request_data)
3424 GtkTextBuffer *src_buffer = NULL;
3426 /* If we can get the owner, the selection is in-process */
3427 owner = gdk_selection_owner_get_for_display (gtk_selection_data_get_display (selection_data),
3428 gtk_selection_data_get_selection (selection_data));
3433 if (gdk_window_get_window_type (owner) == GDK_WINDOW_FOREIGN)
3436 if (gtk_selection_data_get_data_type (selection_data) != gdk_atom_intern_static_string ("GTK_TEXT_BUFFER_CONTENTS"))
3439 if (gtk_selection_data_get_length (selection_data) != sizeof (src_buffer))
3442 memcpy (&src_buffer, gtk_selection_data_get_data (selection_data), sizeof (src_buffer));
3444 if (src_buffer == NULL)
3447 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (src_buffer), NULL);
3449 if (gtk_text_buffer_get_tag_table (src_buffer) !=
3450 gtk_text_buffer_get_tag_table (request_data->buffer))
3457 /* These are pretty handy functions; maybe something like them
3458 * should be in the public API. Also, there are other places in this
3459 * file where they could be used.
3462 save_iter (const GtkTextIter *iter,
3463 gboolean left_gravity)
3465 return gtk_text_buffer_create_mark (gtk_text_iter_get_buffer (iter),
3472 restore_iter (const GtkTextIter *iter,
3475 gtk_text_buffer_get_iter_at_mark (gtk_text_mark_get_buffer (save_id),
3476 (GtkTextIter*) iter,
3478 gtk_text_buffer_delete_mark (gtk_text_mark_get_buffer (save_id),
3484 clipboard_rich_text_received (GtkClipboard *clipboard,
3490 ClipboardRequest *request_data = data;
3491 GtkTextIter insert_point;
3492 gboolean retval = TRUE;
3493 GError *error = NULL;
3495 if (text != NULL && length > 0)
3497 pre_paste_prep (request_data, &insert_point);
3499 if (request_data->interactive)
3500 gtk_text_buffer_begin_user_action (request_data->buffer);
3502 if (!request_data->interactive ||
3503 gtk_text_iter_can_insert (&insert_point,
3504 request_data->default_editable))
3506 retval = gtk_text_buffer_deserialize (request_data->buffer,
3507 request_data->buffer,
3516 g_warning ("error pasting: %s\n", error->message);
3517 g_clear_error (&error);
3520 if (request_data->interactive)
3521 gtk_text_buffer_end_user_action (request_data->buffer);
3523 emit_paste_done (request_data->buffer, clipboard);
3527 post_paste_cleanup (request_data);
3532 /* Request the text selection instead */
3533 gtk_clipboard_request_text (clipboard,
3534 clipboard_text_received,
3539 paste_from_buffer (GtkClipboard *clipboard,
3540 ClipboardRequest *request_data,
3541 GtkTextBuffer *src_buffer,
3542 const GtkTextIter *start,
3543 const GtkTextIter *end)
3545 GtkTextIter insert_point;
3546 GtkTextBuffer *buffer = request_data->buffer;
3548 /* We're about to emit a bunch of signals, so be safe */
3549 g_object_ref (src_buffer);
3551 pre_paste_prep (request_data, &insert_point);
3553 if (request_data->interactive)
3554 gtk_text_buffer_begin_user_action (buffer);
3556 if (!gtk_text_iter_equal (start, end))
3558 if (!request_data->interactive ||
3559 (gtk_text_iter_can_insert (&insert_point,
3560 request_data->default_editable)))
3561 gtk_text_buffer_real_insert_range (buffer,
3565 request_data->interactive);
3568 post_paste_cleanup (request_data);
3570 if (request_data->interactive)
3571 gtk_text_buffer_end_user_action (buffer);
3573 emit_paste_done (buffer, clipboard);
3575 g_object_unref (src_buffer);
3577 free_clipboard_request (request_data);
3581 clipboard_clipboard_buffer_received (GtkClipboard *clipboard,
3582 GtkSelectionData *selection_data,
3585 ClipboardRequest *request_data = data;
3586 GtkTextBuffer *src_buffer;
3588 src_buffer = selection_data_get_buffer (selection_data, request_data);
3592 GtkTextIter start, end;
3594 if (g_object_get_data (G_OBJECT (src_buffer), "gtk-text-buffer-clipboard"))
3596 gtk_text_buffer_get_bounds (src_buffer, &start, &end);
3598 paste_from_buffer (clipboard, request_data, src_buffer,
3603 if (gtk_text_buffer_get_selection_bounds (src_buffer, &start, &end))
3604 paste_from_buffer (clipboard, request_data, src_buffer,
3610 if (gtk_clipboard_wait_is_rich_text_available (clipboard,
3611 request_data->buffer))
3613 /* Request rich text */
3614 gtk_clipboard_request_rich_text (clipboard,
3615 request_data->buffer,
3616 clipboard_rich_text_received,
3621 /* Request the text selection instead */
3622 gtk_clipboard_request_text (clipboard,
3623 clipboard_text_received,
3631 GtkClipboard *clipboard;
3633 } SelectionClipboard;
3636 update_selection_clipboards (GtkTextBuffer *buffer)
3638 GtkTextBufferPrivate *priv;
3639 GSList *tmp_list = buffer->priv->selection_clipboards;
3641 priv = buffer->priv;
3643 gtk_text_buffer_get_copy_target_list (buffer);
3650 SelectionClipboard *selection_clipboard = tmp_list->data;
3651 GtkClipboard *clipboard = selection_clipboard->clipboard;
3653 /* Determine whether we have a selection and adjust X selection
3656 if (!gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
3658 if (gtk_clipboard_get_owner (clipboard) == G_OBJECT (buffer))
3659 gtk_clipboard_clear (clipboard);
3663 /* Even if we already have the selection, we need to update our
3666 if (!gtk_clipboard_set_with_owner (clipboard,
3667 priv->copy_target_entries,
3668 priv->n_copy_target_entries,
3669 clipboard_get_selection_cb,
3670 clipboard_clear_selection_cb,
3672 clipboard_clear_selection_cb (clipboard, buffer);
3675 tmp_list = tmp_list->next;
3679 static SelectionClipboard *
3680 find_selection_clipboard (GtkTextBuffer *buffer,
3681 GtkClipboard *clipboard)
3683 GSList *tmp_list = buffer->priv->selection_clipboards;
3686 SelectionClipboard *selection_clipboard = tmp_list->data;
3687 if (selection_clipboard->clipboard == clipboard)
3688 return selection_clipboard;
3690 tmp_list = tmp_list->next;
3697 * gtk_text_buffer_add_selection_clipboard:
3698 * @buffer: a #GtkTextBuffer
3699 * @clipboard: a #GtkClipboard
3701 * Adds @clipboard to the list of clipboards in which the selection
3702 * contents of @buffer are available. In most cases, @clipboard will be
3703 * the #GtkClipboard of type %GDK_SELECTION_PRIMARY for a view of @buffer.
3706 gtk_text_buffer_add_selection_clipboard (GtkTextBuffer *buffer,
3707 GtkClipboard *clipboard)
3709 SelectionClipboard *selection_clipboard;
3711 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
3712 g_return_if_fail (clipboard != NULL);
3714 selection_clipboard = find_selection_clipboard (buffer, clipboard);
3715 if (selection_clipboard)
3717 selection_clipboard->ref_count++;
3721 selection_clipboard = g_new (SelectionClipboard, 1);
3723 selection_clipboard->clipboard = clipboard;
3724 selection_clipboard->ref_count = 1;
3726 buffer->priv->selection_clipboards = g_slist_prepend (buffer->priv->selection_clipboards,
3727 selection_clipboard);
3732 * gtk_text_buffer_remove_selection_clipboard:
3733 * @buffer: a #GtkTextBuffer
3734 * @clipboard: a #GtkClipboard added to @buffer by
3735 * gtk_text_buffer_add_selection_clipboard()
3737 * Removes a #GtkClipboard added with
3738 * gtk_text_buffer_add_selection_clipboard().
3741 gtk_text_buffer_remove_selection_clipboard (GtkTextBuffer *buffer,
3742 GtkClipboard *clipboard)
3744 SelectionClipboard *selection_clipboard;
3746 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
3747 g_return_if_fail (clipboard != NULL);
3749 selection_clipboard = find_selection_clipboard (buffer, clipboard);
3750 g_return_if_fail (selection_clipboard != NULL);
3752 selection_clipboard->ref_count--;
3753 if (selection_clipboard->ref_count == 0)
3755 if (gtk_clipboard_get_owner (selection_clipboard->clipboard) == G_OBJECT (buffer))
3756 gtk_clipboard_clear (selection_clipboard->clipboard);
3758 buffer->priv->selection_clipboards = g_slist_remove (buffer->priv->selection_clipboards,
3759 selection_clipboard);
3761 g_free (selection_clipboard);
3766 remove_all_selection_clipboards (GtkTextBuffer *buffer)
3768 GtkTextBufferPrivate *priv = buffer->priv;
3770 g_slist_foreach (priv->selection_clipboards, (GFunc)g_free, NULL);
3771 g_slist_free (priv->selection_clipboards);
3772 priv->selection_clipboards = NULL;
3776 * gtk_text_buffer_paste_clipboard:
3777 * @buffer: a #GtkTextBuffer
3778 * @clipboard: the #GtkClipboard to paste from
3779 * @override_location: (allow-none): location to insert pasted text, or %NULL for
3781 * @default_editable: whether the buffer is editable by default
3783 * Pastes the contents of a clipboard at the insertion point, or
3784 * at @override_location. (Note: pasting is asynchronous, that is,
3785 * we'll ask for the paste data and return, and at some point later
3786 * after the main loop runs, the paste data will be inserted.)
3789 gtk_text_buffer_paste_clipboard (GtkTextBuffer *buffer,
3790 GtkClipboard *clipboard,
3791 GtkTextIter *override_location,
3792 gboolean default_editable)
3794 ClipboardRequest *data = g_new (ClipboardRequest, 1);
3795 GtkTextIter paste_point;
3796 GtkTextIter start, end;
3798 if (override_location != NULL)
3799 gtk_text_buffer_create_mark (buffer,
3800 "gtk_paste_point_override",
3801 override_location, FALSE);
3803 data->buffer = g_object_ref (buffer);
3804 data->interactive = TRUE;
3805 data->default_editable = !!default_editable;
3807 /* When pasting with the cursor inside the selection area, you
3808 * replace the selection with the new text, otherwise, you
3809 * simply insert the new text at the point where the click
3810 * occured, unselecting any selected text. The replace_selection
3811 * flag toggles this behavior.
3813 data->replace_selection = FALSE;
3815 get_paste_point (buffer, &paste_point, FALSE);
3816 if (gtk_text_buffer_get_selection_bounds (buffer, &start, &end) &&
3817 (gtk_text_iter_in_range (&paste_point, &start, &end) ||
3818 gtk_text_iter_equal (&paste_point, &end)))
3819 data->replace_selection = TRUE;
3821 gtk_clipboard_request_contents (clipboard,
3822 gdk_atom_intern_static_string ("GTK_TEXT_BUFFER_CONTENTS"),
3823 clipboard_clipboard_buffer_received, data);
3827 * gtk_text_buffer_delete_selection:
3828 * @buffer: a #GtkTextBuffer
3829 * @interactive: whether the deletion is caused by user interaction
3830 * @default_editable: whether the buffer is editable by default
3832 * Deletes the range between the "insert" and "selection_bound" marks,
3833 * that is, the currently-selected text. If @interactive is %TRUE,
3834 * the editability of the selection will be considered (users can't delete
3837 * Return value: whether there was a non-empty selection to delete
3840 gtk_text_buffer_delete_selection (GtkTextBuffer *buffer,
3841 gboolean interactive,
3842 gboolean default_editable)
3847 if (!gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
3849 return FALSE; /* No selection */
3854 gtk_text_buffer_delete_interactive (buffer, &start, &end, default_editable);
3856 gtk_text_buffer_delete (buffer, &start, &end);
3858 return TRUE; /* We deleted stuff */
3863 * gtk_text_buffer_backspace:
3864 * @buffer: a #GtkTextBuffer
3865 * @iter: a position in @buffer
3866 * @interactive: whether the deletion is caused by user interaction
3867 * @default_editable: whether the buffer is editable by default
3869 * Performs the appropriate action as if the user hit the delete
3870 * key with the cursor at the position specified by @iter. In the
3871 * normal case a single character will be deleted, but when
3872 * combining accents are involved, more than one character can
3873 * be deleted, and when precomposed character and accent combinations
3874 * are involved, less than one character will be deleted.
3876 * Because the buffer is modified, all outstanding iterators become
3877 * invalid after calling this function; however, the @iter will be
3878 * re-initialized to point to the location where text was deleted.
3880 * Return value: %TRUE if the buffer was modified
3885 gtk_text_buffer_backspace (GtkTextBuffer *buffer,
3887 gboolean interactive,
3888 gboolean default_editable)
3890 gchar *cluster_text;
3893 gboolean retval = FALSE;
3894 const PangoLogAttr *attrs;
3896 gboolean backspace_deletes_character;
3898 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
3899 g_return_val_if_fail (iter != NULL, FALSE);
3904 attrs = _gtk_text_buffer_get_line_log_attrs (buffer, &start, NULL);
3906 /* For no good reason, attrs is NULL for the empty last line in
3907 * a buffer. Special case that here. (#156164)
3911 offset = gtk_text_iter_get_line_offset (&start);
3912 backspace_deletes_character = attrs[offset].backspace_deletes_character;
3915 backspace_deletes_character = FALSE;
3917 gtk_text_iter_backward_cursor_position (&start);
3919 if (gtk_text_iter_equal (&start, &end))
3922 cluster_text = gtk_text_iter_get_text (&start, &end);
3925 gtk_text_buffer_begin_user_action (buffer);
3927 if (gtk_text_buffer_delete_interactive (buffer, &start, &end,
3930 /* special case \r\n, since we never want to reinsert \r */
3931 if (backspace_deletes_character && strcmp ("\r\n", cluster_text))
3933 gchar *normalized_text = g_utf8_normalize (cluster_text,
3934 strlen (cluster_text),
3936 glong len = g_utf8_strlen (normalized_text, -1);
3939 gtk_text_buffer_insert_interactive (buffer,
3942 g_utf8_offset_to_pointer (normalized_text, len - 1) - normalized_text,
3945 g_free (normalized_text);
3952 gtk_text_buffer_end_user_action (buffer);
3954 g_free (cluster_text);
3956 /* Revalidate the users iter */
3963 cut_or_copy (GtkTextBuffer *buffer,
3964 GtkClipboard *clipboard,
3965 gboolean delete_region_after,
3966 gboolean interactive,
3967 gboolean default_editable)
3969 GtkTextBufferPrivate *priv;
3971 /* We prefer to cut the selected region between selection_bound and
3972 * insertion point. If that region is empty, then we cut the region
3973 * between the "anchor" and the insertion point (this is for
3974 * C-space and M-w and other Emacs-style copy/yank keys). Note that
3975 * insert and selection_bound are guaranteed to exist, but the
3976 * anchor only exists sometimes.
3981 priv = buffer->priv;
3983 gtk_text_buffer_get_copy_target_list (buffer);
3985 if (!gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
3987 /* Let's try the anchor thing */
3988 GtkTextMark * anchor = gtk_text_buffer_get_mark (buffer, "anchor");
3994 gtk_text_buffer_get_iter_at_mark (buffer, &end, anchor);
3995 gtk_text_iter_order (&start, &end);
3999 if (!gtk_text_iter_equal (&start, &end))
4002 GtkTextBuffer *contents;
4004 contents = create_clipboard_contents_buffer (buffer);
4006 gtk_text_buffer_get_iter_at_offset (contents, &ins, 0);
4008 gtk_text_buffer_insert_range (contents, &ins, &start, &end);
4010 if (!gtk_clipboard_set_with_data (clipboard,
4011 priv->copy_target_entries,
4012 priv->n_copy_target_entries,
4013 clipboard_get_contents_cb,
4014 clipboard_clear_contents_cb,
4016 g_object_unref (contents);
4018 gtk_clipboard_set_can_store (clipboard,
4019 priv->copy_target_entries + 1,
4020 priv->n_copy_target_entries - 1);
4022 if (delete_region_after)
4025 gtk_text_buffer_delete_interactive (buffer, &start, &end,
4028 gtk_text_buffer_delete (buffer, &start, &end);
4034 * gtk_text_buffer_cut_clipboard:
4035 * @buffer: a #GtkTextBuffer
4036 * @clipboard: the #GtkClipboard object to cut to
4037 * @default_editable: default editability of the buffer
4039 * Copies the currently-selected text to a clipboard, then deletes
4040 * said text if it's editable.
4043 gtk_text_buffer_cut_clipboard (GtkTextBuffer *buffer,
4044 GtkClipboard *clipboard,
4045 gboolean default_editable)
4047 gtk_text_buffer_begin_user_action (buffer);
4048 cut_or_copy (buffer, clipboard, TRUE, TRUE, default_editable);
4049 gtk_text_buffer_end_user_action (buffer);
4053 * gtk_text_buffer_copy_clipboard:
4054 * @buffer: a #GtkTextBuffer
4055 * @clipboard: the #GtkClipboard object to copy to
4057 * Copies the currently-selected text to a clipboard.
4060 gtk_text_buffer_copy_clipboard (GtkTextBuffer *buffer,
4061 GtkClipboard *clipboard)
4063 cut_or_copy (buffer, clipboard, FALSE, TRUE, TRUE);
4067 * gtk_text_buffer_get_selection_bounds:
4068 * @buffer: a #GtkTextBuffer a #GtkTextBuffer
4069 * @start: (out): iterator to initialize with selection start
4070 * @end: (out): iterator to initialize with selection end
4072 * Returns %TRUE if some text is selected; places the bounds
4073 * of the selection in @start and @end (if the selection has length 0,
4074 * then @start and @end are filled in with the same value).
4075 * @start and @end will be in ascending order. If @start and @end are
4076 * NULL, then they are not filled in, but the return value still indicates
4077 * whether text is selected.
4079 * Return value: whether the selection has nonzero length
4082 gtk_text_buffer_get_selection_bounds (GtkTextBuffer *buffer,
4086 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
4088 return _gtk_text_btree_get_selection_bounds (get_btree (buffer), start, end);
4092 * gtk_text_buffer_begin_user_action:
4093 * @buffer: a #GtkTextBuffer
4095 * Called to indicate that the buffer operations between here and a
4096 * call to gtk_text_buffer_end_user_action() are part of a single
4097 * user-visible operation. The operations between
4098 * gtk_text_buffer_begin_user_action() and
4099 * gtk_text_buffer_end_user_action() can then be grouped when creating
4100 * an undo stack. #GtkTextBuffer maintains a count of calls to
4101 * gtk_text_buffer_begin_user_action() that have not been closed with
4102 * a call to gtk_text_buffer_end_user_action(), and emits the
4103 * "begin-user-action" and "end-user-action" signals only for the
4104 * outermost pair of calls. This allows you to build user actions
4105 * from other user actions.
4107 * The "interactive" buffer mutation functions, such as
4108 * gtk_text_buffer_insert_interactive(), automatically call begin/end
4109 * user action around the buffer operations they perform, so there's
4110 * no need to add extra calls if you user action consists solely of a
4111 * single call to one of those functions.
4114 gtk_text_buffer_begin_user_action (GtkTextBuffer *buffer)
4116 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
4118 buffer->priv->user_action_count += 1;
4120 if (buffer->priv->user_action_count == 1)
4122 /* Outermost nested user action begin emits the signal */
4123 g_signal_emit (buffer, signals[BEGIN_USER_ACTION], 0);
4128 * gtk_text_buffer_end_user_action:
4129 * @buffer: a #GtkTextBuffer
4131 * Should be paired with a call to gtk_text_buffer_begin_user_action().
4132 * See that function for a full explanation.
4135 gtk_text_buffer_end_user_action (GtkTextBuffer *buffer)
4137 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
4138 g_return_if_fail (buffer->priv->user_action_count > 0);
4140 buffer->priv->user_action_count -= 1;
4142 if (buffer->priv->user_action_count == 0)
4144 /* Ended the outermost-nested user action end, so emit the signal */
4145 g_signal_emit (buffer, signals[END_USER_ACTION], 0);
4150 gtk_text_buffer_free_target_lists (GtkTextBuffer *buffer)
4152 GtkTextBufferPrivate *priv = buffer->priv;
4154 if (priv->copy_target_list)
4156 gtk_target_list_unref (priv->copy_target_list);
4157 priv->copy_target_list = NULL;
4159 gtk_target_table_free (priv->copy_target_entries,
4160 priv->n_copy_target_entries);
4161 priv->copy_target_entries = NULL;
4162 priv->n_copy_target_entries = 0;
4165 if (priv->paste_target_list)
4167 gtk_target_list_unref (priv->paste_target_list);
4168 priv->paste_target_list = NULL;
4170 gtk_target_table_free (priv->paste_target_entries,
4171 priv->n_paste_target_entries);
4172 priv->paste_target_entries = NULL;
4173 priv->n_paste_target_entries = 0;
4177 static GtkTargetList *
4178 gtk_text_buffer_get_target_list (GtkTextBuffer *buffer,
4179 gboolean deserializable,
4180 GtkTargetEntry **entries,
4183 GtkTargetList *target_list;
4185 target_list = gtk_target_list_new (NULL, 0);
4187 gtk_target_list_add (target_list,
4188 gdk_atom_intern_static_string ("GTK_TEXT_BUFFER_CONTENTS"),
4189 GTK_TARGET_SAME_APP,
4190 GTK_TEXT_BUFFER_TARGET_INFO_BUFFER_CONTENTS);
4192 gtk_target_list_add_rich_text_targets (target_list,
4193 GTK_TEXT_BUFFER_TARGET_INFO_RICH_TEXT,
4197 gtk_target_list_add_text_targets (target_list,
4198 GTK_TEXT_BUFFER_TARGET_INFO_TEXT);
4200 *entries = gtk_target_table_new_from_list (target_list, n_entries);
4206 * gtk_text_buffer_get_copy_target_list:
4207 * @buffer: a #GtkTextBuffer
4209 * This function returns the list of targets this text buffer can
4210 * provide for copying and as DND source. The targets in the list are
4211 * added with %info values from the #GtkTextBufferTargetInfo enum,
4212 * using gtk_target_list_add_rich_text_targets() and
4213 * gtk_target_list_add_text_targets().
4215 * Return value: (transfer none): the #GtkTargetList
4220 gtk_text_buffer_get_copy_target_list (GtkTextBuffer *buffer)
4222 GtkTextBufferPrivate *priv;
4224 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
4226 priv = buffer->priv;
4228 if (! priv->copy_target_list)
4229 priv->copy_target_list =
4230 gtk_text_buffer_get_target_list (buffer, FALSE,
4231 &priv->copy_target_entries,
4232 &priv->n_copy_target_entries);
4234 return priv->copy_target_list;
4238 * gtk_text_buffer_get_paste_target_list:
4239 * @buffer: a #GtkTextBuffer
4241 * This function returns the list of targets this text buffer supports
4242 * for pasting and as DND destination. The targets in the list are
4243 * added with %info values from the #GtkTextBufferTargetInfo enum,
4244 * using gtk_target_list_add_rich_text_targets() and
4245 * gtk_target_list_add_text_targets().
4247 * Return value: (transfer none): the #GtkTargetList
4252 gtk_text_buffer_get_paste_target_list (GtkTextBuffer *buffer)
4254 GtkTextBufferPrivate *priv;
4256 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
4258 priv = buffer->priv;
4260 if (! priv->paste_target_list)
4261 priv->paste_target_list =
4262 gtk_text_buffer_get_target_list (buffer, TRUE,
4263 &priv->paste_target_entries,
4264 &priv->n_paste_target_entries);
4266 return priv->paste_target_list;
4270 * Logical attribute cache
4273 #define ATTR_CACHE_SIZE 2
4275 typedef struct _CacheEntry CacheEntry;
4280 PangoLogAttr *attrs;
4283 struct _GtkTextLogAttrCache
4285 gint chars_changed_stamp;
4286 CacheEntry entries[ATTR_CACHE_SIZE];
4290 free_log_attr_cache (GtkTextLogAttrCache *cache)
4293 while (i < ATTR_CACHE_SIZE)
4295 g_free (cache->entries[i].attrs);
4302 clear_log_attr_cache (GtkTextLogAttrCache *cache)
4305 while (i < ATTR_CACHE_SIZE)
4307 g_free (cache->entries[i].attrs);
4308 cache->entries[i].attrs = NULL;
4313 static PangoLogAttr*
4314 compute_log_attrs (const GtkTextIter *iter,
4320 gint char_len, byte_len;
4321 PangoLogAttr *attrs = NULL;
4326 gtk_text_iter_set_line_offset (&start, 0);
4327 gtk_text_iter_forward_line (&end);
4329 paragraph = gtk_text_iter_get_slice (&start, &end);
4330 char_len = g_utf8_strlen (paragraph, -1);
4331 byte_len = strlen (paragraph);
4333 g_assert (char_len > 0);
4336 *char_lenp = char_len;
4338 attrs = g_new (PangoLogAttr, char_len + 1);
4340 /* FIXME we need to follow PangoLayout and allow different language
4341 * tags within the paragraph
4343 pango_get_log_attrs (paragraph, byte_len, -1,
4344 gtk_text_iter_get_language (&start),
4353 /* The return value from this is valid until you call this a second time.
4356 _gtk_text_buffer_get_line_log_attrs (GtkTextBuffer *buffer,
4357 const GtkTextIter *anywhere_in_line,
4360 GtkTextBufferPrivate *priv;
4362 GtkTextLogAttrCache *cache;
4365 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
4366 g_return_val_if_fail (anywhere_in_line != NULL, NULL);
4368 priv = buffer->priv;
4370 /* special-case for empty last line in buffer */
4371 if (gtk_text_iter_is_end (anywhere_in_line) &&
4372 gtk_text_iter_get_line_offset (anywhere_in_line) == 0)
4379 /* FIXME we also need to recompute log attrs if the language tag at
4380 * the start of a paragraph changes
4383 if (priv->log_attr_cache == NULL)
4385 priv->log_attr_cache = g_new0 (GtkTextLogAttrCache, 1);
4386 priv->log_attr_cache->chars_changed_stamp =
4387 _gtk_text_btree_get_chars_changed_stamp (get_btree (buffer));
4389 else if (priv->log_attr_cache->chars_changed_stamp !=
4390 _gtk_text_btree_get_chars_changed_stamp (get_btree (buffer)))
4392 clear_log_attr_cache (priv->log_attr_cache);
4395 cache = priv->log_attr_cache;
4396 line = gtk_text_iter_get_line (anywhere_in_line);
4399 while (i < ATTR_CACHE_SIZE)
4401 if (cache->entries[i].attrs &&
4402 cache->entries[i].line == line)
4405 *char_len = cache->entries[i].char_len;
4406 return cache->entries[i].attrs;
4411 /* Not in cache; open up the first cache entry */
4412 g_free (cache->entries[ATTR_CACHE_SIZE-1].attrs);
4414 g_memmove (cache->entries + 1, cache->entries,
4415 sizeof (CacheEntry) * (ATTR_CACHE_SIZE - 1));
4417 cache->entries[0].line = line;
4418 cache->entries[0].attrs = compute_log_attrs (anywhere_in_line,
4419 &cache->entries[0].char_len);
4422 *char_len = cache->entries[0].char_len;
4424 return cache->entries[0].attrs;
4428 _gtk_text_buffer_notify_will_remove_tag (GtkTextBuffer *buffer,
4431 /* This removes tag from the buffer, but DOESN'T emit the
4432 * remove-tag signal, because we can't afford to have user
4433 * code messing things up at this point; the tag MUST be removed
4436 if (buffer->priv->btree)
4437 _gtk_text_btree_notify_will_remove_tag (buffer->priv->btree, tag);
4445 _gtk_text_buffer_spew (GtkTextBuffer *buffer)
4447 _gtk_text_btree_spew (get_btree (buffer));
4451 _gtk_text_buffer_get_text_before (GtkTextBuffer *buffer,
4452 AtkTextBoundary boundary_type,
4453 GtkTextIter *position,
4462 switch (boundary_type)
4464 case ATK_TEXT_BOUNDARY_CHAR:
4465 gtk_text_iter_backward_char (start);
4468 case ATK_TEXT_BOUNDARY_WORD_START:
4469 if (!gtk_text_iter_starts_word (start))
4470 gtk_text_iter_backward_word_start (start);
4472 gtk_text_iter_backward_word_start (start);
4475 case ATK_TEXT_BOUNDARY_WORD_END:
4476 if (gtk_text_iter_inside_word (start) &&
4477 !gtk_text_iter_starts_word (start))
4478 gtk_text_iter_backward_word_start (start);
4479 while (!gtk_text_iter_ends_word (start))
4481 if (!gtk_text_iter_backward_char (start))
4485 gtk_text_iter_backward_word_start (start);
4486 while (!gtk_text_iter_ends_word (start))
4488 if (!gtk_text_iter_backward_char (start))
4493 case ATK_TEXT_BOUNDARY_SENTENCE_START:
4494 if (!gtk_text_iter_starts_sentence (start))
4495 gtk_text_iter_backward_sentence_start (start);
4497 gtk_text_iter_backward_sentence_start (start);
4500 case ATK_TEXT_BOUNDARY_SENTENCE_END:
4501 if (gtk_text_iter_inside_sentence (start) &&
4502 !gtk_text_iter_starts_sentence (start))
4503 gtk_text_iter_backward_sentence_start (start);
4504 while (!gtk_text_iter_ends_sentence (start))
4506 if (!gtk_text_iter_backward_char (start))
4510 gtk_text_iter_backward_sentence_start (start);
4511 while (!gtk_text_iter_ends_sentence (start))
4513 if (!gtk_text_iter_backward_char (start))
4518 case ATK_TEXT_BOUNDARY_LINE_START:
4519 line_number = gtk_text_iter_get_line (start);
4520 if (line_number == 0)
4522 gtk_text_buffer_get_iter_at_offset (buffer, start, 0);
4526 gtk_text_iter_backward_line (start);
4527 gtk_text_iter_forward_line (start);
4530 gtk_text_iter_backward_line (start);
4533 case ATK_TEXT_BOUNDARY_LINE_END:
4534 line_number = gtk_text_iter_get_line (start);
4535 if (line_number == 0)
4537 gtk_text_buffer_get_iter_at_offset (buffer, start, 0);
4542 gtk_text_iter_backward_line (start);
4544 while (!gtk_text_iter_ends_line (start))
4546 if (!gtk_text_iter_backward_char (start))
4549 gtk_text_iter_forward_to_line_end (end);
4556 _gtk_text_buffer_get_text_at (GtkTextBuffer *buffer,
4557 AtkTextBoundary boundary_type,
4558 GtkTextIter *position,
4567 switch (boundary_type)
4569 case ATK_TEXT_BOUNDARY_CHAR:
4570 gtk_text_iter_forward_char (end);
4573 case ATK_TEXT_BOUNDARY_WORD_START:
4574 if (!gtk_text_iter_starts_word (start))
4575 gtk_text_iter_backward_word_start (start);
4576 if (gtk_text_iter_inside_word (end))
4577 gtk_text_iter_forward_word_end (end);
4578 while (!gtk_text_iter_starts_word (end))
4580 if (!gtk_text_iter_forward_char (end))
4585 case ATK_TEXT_BOUNDARY_WORD_END:
4586 if (gtk_text_iter_inside_word (start) &&
4587 !gtk_text_iter_starts_word (start))
4588 gtk_text_iter_backward_word_start (start);
4589 while (!gtk_text_iter_ends_word (start))
4591 if (!gtk_text_iter_backward_char (start))
4594 gtk_text_iter_forward_word_end (end);
4597 case ATK_TEXT_BOUNDARY_SENTENCE_START:
4598 if (!gtk_text_iter_starts_sentence (start))
4599 gtk_text_iter_backward_sentence_start (start);
4600 if (gtk_text_iter_inside_sentence (end))
4601 gtk_text_iter_forward_sentence_end (end);
4602 while (!gtk_text_iter_starts_sentence (end))
4604 if (!gtk_text_iter_forward_char (end))
4609 case ATK_TEXT_BOUNDARY_SENTENCE_END:
4610 if (gtk_text_iter_inside_sentence (start) &&
4611 !gtk_text_iter_starts_sentence (start))
4612 gtk_text_iter_backward_sentence_start (start);
4613 while (!gtk_text_iter_ends_sentence (start))
4615 if (!gtk_text_iter_backward_char (start))
4618 gtk_text_iter_forward_sentence_end (end);
4621 case ATK_TEXT_BOUNDARY_LINE_START:
4622 line_number = gtk_text_iter_get_line (start);
4623 if (line_number == 0)
4625 gtk_text_buffer_get_iter_at_offset (buffer, start, 0);
4629 gtk_text_iter_backward_line (start);
4630 gtk_text_iter_forward_line (start);
4632 gtk_text_iter_forward_line (end);
4635 case ATK_TEXT_BOUNDARY_LINE_END:
4636 line_number = gtk_text_iter_get_line (start);
4637 if (line_number == 0)
4639 gtk_text_buffer_get_iter_at_offset (buffer, start, 0);
4643 gtk_text_iter_backward_line (start);
4644 gtk_text_iter_forward_line (start);
4646 while (!gtk_text_iter_ends_line (start))
4648 if (!gtk_text_iter_backward_char (start))
4651 gtk_text_iter_forward_to_line_end (end);
4657 _gtk_text_buffer_get_text_after (GtkTextBuffer *buffer,
4658 AtkTextBoundary boundary_type,
4659 GtkTextIter *position,
4666 switch (boundary_type)
4668 case ATK_TEXT_BOUNDARY_CHAR:
4669 gtk_text_iter_forward_char (start);
4670 gtk_text_iter_forward_chars (end, 2);
4673 case ATK_TEXT_BOUNDARY_WORD_START:
4674 if (gtk_text_iter_inside_word (end))
4675 gtk_text_iter_forward_word_end (end);
4676 while (!gtk_text_iter_starts_word (end))
4678 if (!gtk_text_iter_forward_char (end))
4682 if (!gtk_text_iter_is_end (end))
4684 gtk_text_iter_forward_word_end (end);
4685 while (!gtk_text_iter_starts_word (end))
4687 if (!gtk_text_iter_forward_char (end))
4693 case ATK_TEXT_BOUNDARY_WORD_END:
4694 gtk_text_iter_forward_word_end (end);
4696 if (!gtk_text_iter_is_end (end))
4697 gtk_text_iter_forward_word_end (end);
4700 case ATK_TEXT_BOUNDARY_SENTENCE_START:
4701 if (gtk_text_iter_inside_sentence (end))
4702 gtk_text_iter_forward_sentence_end (end);
4703 while (!gtk_text_iter_starts_sentence (end))
4705 if (!gtk_text_iter_forward_char (end))
4709 if (!gtk_text_iter_is_end (end))
4711 gtk_text_iter_forward_sentence_end (end);
4712 while (!gtk_text_iter_starts_sentence (end))
4714 if (!gtk_text_iter_forward_char (end))
4720 case ATK_TEXT_BOUNDARY_SENTENCE_END:
4721 gtk_text_iter_forward_sentence_end (end);
4723 if (!gtk_text_iter_is_end (end))
4724 gtk_text_iter_forward_sentence_end (end);
4727 case ATK_TEXT_BOUNDARY_LINE_START:
4728 gtk_text_iter_forward_line (end);
4730 gtk_text_iter_forward_line (end);
4733 case ATK_TEXT_BOUNDARY_LINE_END:
4734 gtk_text_iter_forward_line (start);
4736 if (!gtk_text_iter_is_end (start))
4738 while (!gtk_text_iter_ends_line (start))
4740 if (!gtk_text_iter_backward_char (start))
4743 gtk_text_iter_forward_to_line_end (end);