1 /* GTK - The GIMP Toolkit
2 * gtktextbuffer.c Copyright (C) 2000 Red Hat, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
31 #define GTK_TEXT_USE_INTERNAL_UNSUPPORTED_API
32 #include "gtkclipboard.h"
33 #include "gtkinvisible.h"
34 #include "gtkmarshalers.h"
35 #include "gtktextbuffer.h"
36 #include "gtktextbtree.h"
37 #include "gtktextiterprivate.h"
40 typedef struct _ClipboardRequest ClipboardRequest;
42 struct _ClipboardRequest
44 GtkTextBuffer *buffer;
46 gboolean default_editable;
47 gboolean is_clipboard;
48 gboolean replace_selection;
79 TARGET_TEXT_BUFFER_CONTENTS
82 static void gtk_text_buffer_init (GtkTextBuffer *tkxt_buffer);
83 static void gtk_text_buffer_class_init (GtkTextBufferClass *klass);
84 static void gtk_text_buffer_finalize (GObject *object);
87 static void gtk_text_buffer_real_insert_text (GtkTextBuffer *buffer,
91 static void gtk_text_buffer_real_insert_pixbuf (GtkTextBuffer *buffer,
94 static void gtk_text_buffer_real_insert_anchor (GtkTextBuffer *buffer,
96 GtkTextChildAnchor *anchor);
97 static void gtk_text_buffer_real_delete_range (GtkTextBuffer *buffer,
100 static void gtk_text_buffer_real_apply_tag (GtkTextBuffer *buffer,
102 const GtkTextIter *start_char,
103 const GtkTextIter *end_char);
104 static void gtk_text_buffer_real_remove_tag (GtkTextBuffer *buffer,
106 const GtkTextIter *start_char,
107 const GtkTextIter *end_char);
108 static void gtk_text_buffer_real_changed (GtkTextBuffer *buffer);
110 static GtkTextBTree* get_btree (GtkTextBuffer *buffer);
111 static void free_log_attr_cache (GtkTextLogAttrCache *cache);
113 static void remove_all_clipboard_contents_buffers (GtkTextBuffer *buffer);
114 static void remove_all_selection_clipboards (GtkTextBuffer *buffer);
115 static void update_selection_clipboards (GtkTextBuffer *buffer);
117 static GObjectClass *parent_class = NULL;
118 static guint signals[LAST_SIGNAL] = { 0 };
120 static void gtk_text_buffer_set_property (GObject *object,
124 static void gtk_text_buffer_get_property (GObject *object,
131 gtk_text_buffer_get_type (void)
133 static GType our_type = 0;
137 static const GTypeInfo our_info =
139 sizeof (GtkTextBufferClass),
140 (GBaseInitFunc) NULL,
141 (GBaseFinalizeFunc) NULL,
142 (GClassInitFunc) gtk_text_buffer_class_init,
143 NULL, /* class_finalize */
144 NULL, /* class_data */
145 sizeof (GtkTextBuffer),
147 (GInstanceInitFunc) gtk_text_buffer_init
150 our_type = g_type_register_static (G_TYPE_OBJECT, "GtkTextBuffer",
158 gtk_text_buffer_class_init (GtkTextBufferClass *klass)
160 GObjectClass *object_class = G_OBJECT_CLASS (klass);
162 parent_class = g_type_class_peek_parent (klass);
164 object_class->finalize = gtk_text_buffer_finalize;
165 object_class->set_property = gtk_text_buffer_set_property;
166 object_class->get_property = gtk_text_buffer_get_property;
168 klass->insert_text = gtk_text_buffer_real_insert_text;
169 klass->insert_pixbuf = gtk_text_buffer_real_insert_pixbuf;
170 klass->insert_child_anchor = gtk_text_buffer_real_insert_anchor;
171 klass->delete_range = gtk_text_buffer_real_delete_range;
172 klass->apply_tag = gtk_text_buffer_real_apply_tag;
173 klass->remove_tag = gtk_text_buffer_real_remove_tag;
174 klass->changed = gtk_text_buffer_real_changed;
177 g_object_class_install_property (object_class,
179 g_param_spec_object ("tag_table",
182 GTK_TYPE_TEXT_TAG_TABLE,
183 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
185 signals[INSERT_TEXT] =
186 g_signal_new ("insert_text",
187 G_OBJECT_CLASS_TYPE (object_class),
189 G_STRUCT_OFFSET (GtkTextBufferClass, insert_text),
191 _gtk_marshal_VOID__BOXED_STRING_INT,
194 GTK_TYPE_TEXT_ITER | G_SIGNAL_TYPE_STATIC_SCOPE,
195 G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE,
198 signals[INSERT_PIXBUF] =
199 g_signal_new ("insert_pixbuf",
200 G_OBJECT_CLASS_TYPE (object_class),
202 G_STRUCT_OFFSET (GtkTextBufferClass, insert_pixbuf),
204 _gtk_marshal_VOID__BOXED_OBJECT,
207 GTK_TYPE_TEXT_ITER | G_SIGNAL_TYPE_STATIC_SCOPE,
210 signals[INSERT_CHILD_ANCHOR] =
211 g_signal_new ("insert_child_anchor",
212 G_OBJECT_CLASS_TYPE (object_class),
214 G_STRUCT_OFFSET (GtkTextBufferClass, insert_child_anchor),
216 _gtk_marshal_VOID__BOXED_OBJECT,
219 GTK_TYPE_TEXT_ITER | G_SIGNAL_TYPE_STATIC_SCOPE,
220 GTK_TYPE_TEXT_CHILD_ANCHOR);
222 signals[DELETE_RANGE] =
223 g_signal_new ("delete_range",
224 G_OBJECT_CLASS_TYPE (object_class),
226 G_STRUCT_OFFSET (GtkTextBufferClass, delete_range),
228 _gtk_marshal_VOID__BOXED_BOXED,
231 GTK_TYPE_TEXT_ITER | G_SIGNAL_TYPE_STATIC_SCOPE,
232 GTK_TYPE_TEXT_ITER | G_SIGNAL_TYPE_STATIC_SCOPE);
235 g_signal_new ("changed",
236 G_OBJECT_CLASS_TYPE (object_class),
238 G_STRUCT_OFFSET (GtkTextBufferClass, changed),
240 _gtk_marshal_VOID__VOID,
244 signals[MODIFIED_CHANGED] =
245 g_signal_new ("modified_changed",
246 G_OBJECT_CLASS_TYPE (object_class),
248 G_STRUCT_OFFSET (GtkTextBufferClass, modified_changed),
250 _gtk_marshal_VOID__VOID,
255 g_signal_new ("mark_set",
256 G_OBJECT_CLASS_TYPE (object_class),
258 G_STRUCT_OFFSET (GtkTextBufferClass, mark_set),
260 _gtk_marshal_VOID__BOXED_OBJECT,
266 signals[MARK_DELETED] =
267 g_signal_new ("mark_deleted",
268 G_OBJECT_CLASS_TYPE (object_class),
270 G_STRUCT_OFFSET (GtkTextBufferClass, mark_deleted),
272 _gtk_marshal_VOID__OBJECT,
278 g_signal_new ("apply_tag",
279 G_OBJECT_CLASS_TYPE (object_class),
281 G_STRUCT_OFFSET (GtkTextBufferClass, apply_tag),
283 _gtk_marshal_VOID__OBJECT_BOXED_BOXED,
290 signals[REMOVE_TAG] =
291 g_signal_new ("remove_tag",
292 G_OBJECT_CLASS_TYPE (object_class),
294 G_STRUCT_OFFSET (GtkTextBufferClass, remove_tag),
296 _gtk_marshal_VOID__OBJECT_BOXED_BOXED,
303 signals[BEGIN_USER_ACTION] =
304 g_signal_new ("begin_user_action",
305 G_OBJECT_CLASS_TYPE (object_class),
307 G_STRUCT_OFFSET (GtkTextBufferClass, begin_user_action),
309 _gtk_marshal_VOID__VOID,
313 signals[END_USER_ACTION] =
314 g_signal_new ("end_user_action",
315 G_OBJECT_CLASS_TYPE (object_class),
317 G_STRUCT_OFFSET (GtkTextBufferClass, end_user_action),
319 _gtk_marshal_VOID__VOID,
325 gtk_text_buffer_init (GtkTextBuffer *buffer)
327 buffer->clipboard_contents_buffers = NULL;
328 buffer->tag_table = NULL;
332 set_table (GtkTextBuffer *buffer, GtkTextTagTable *table)
334 g_return_if_fail (buffer->tag_table == NULL);
338 buffer->tag_table = table;
339 g_object_ref (G_OBJECT (buffer->tag_table));
340 _gtk_text_tag_table_add_buffer (table, buffer);
344 static GtkTextTagTable*
345 get_table (GtkTextBuffer *buffer)
347 if (buffer->tag_table == NULL)
349 buffer->tag_table = gtk_text_tag_table_new ();
350 _gtk_text_tag_table_add_buffer (buffer->tag_table, buffer);
353 return buffer->tag_table;
357 gtk_text_buffer_set_property (GObject *object,
362 GtkTextBuffer *text_buffer;
364 text_buffer = GTK_TEXT_BUFFER (object);
369 set_table (text_buffer, g_value_get_object (value));
378 gtk_text_buffer_get_property (GObject *object,
383 GtkTextBuffer *text_buffer;
385 text_buffer = GTK_TEXT_BUFFER (object);
390 g_value_set_object (value, get_table (text_buffer));
399 * gtk_text_buffer_new:
400 * @table: a tag table, or NULL to create a new one
402 * Creates a new text buffer.
404 * Return value: a new text buffer
407 gtk_text_buffer_new (GtkTextTagTable *table)
409 GtkTextBuffer *text_buffer;
411 text_buffer = g_object_new (GTK_TYPE_TEXT_BUFFER, "tag_table", table, NULL);
417 gtk_text_buffer_finalize (GObject *object)
419 GtkTextBuffer *buffer;
421 buffer = GTK_TEXT_BUFFER (object);
423 remove_all_clipboard_contents_buffers (buffer);
424 remove_all_selection_clipboards (buffer);
426 if (buffer->tag_table)
428 _gtk_text_tag_table_remove_buffer (buffer->tag_table, buffer);
429 g_object_unref (buffer->tag_table);
430 buffer->tag_table = NULL;
435 _gtk_text_btree_unref (buffer->btree);
436 buffer->btree = NULL;
439 if (buffer->log_attr_cache)
440 free_log_attr_cache (buffer->log_attr_cache);
442 buffer->log_attr_cache = NULL;
444 G_OBJECT_CLASS (parent_class)->finalize (object);
448 get_btree (GtkTextBuffer *buffer)
450 if (buffer->btree == NULL)
451 buffer->btree = _gtk_text_btree_new (gtk_text_buffer_get_tag_table (buffer),
454 return buffer->btree;
458 _gtk_text_buffer_get_btree (GtkTextBuffer *buffer)
460 return get_btree (buffer);
464 * gtk_text_buffer_get_tag_table:
465 * @buffer: a #GtkTextBuffer
467 * Get the #GtkTextTagTable associated with this buffer.
469 * Return value: the buffer's tag table
472 gtk_text_buffer_get_tag_table (GtkTextBuffer *buffer)
474 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
476 return get_table (buffer);
480 * gtk_text_buffer_set_text:
481 * @buffer: a #GtkTextBuffer
482 * @text: UTF-8 text to insert
483 * @len: length of @text in bytes
485 * Deletes current contents of @buffer, and inserts @text instead. If
486 * @len is -1, @text must be nul-terminated. @text must be valid UTF-8.
489 gtk_text_buffer_set_text (GtkTextBuffer *buffer,
493 GtkTextIter start, end;
495 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
496 g_return_if_fail (text != NULL);
501 gtk_text_buffer_get_bounds (buffer, &start, &end);
503 gtk_text_buffer_delete (buffer, &start, &end);
507 gtk_text_buffer_get_iter_at_offset (buffer, &start, 0);
508 gtk_text_buffer_insert (buffer, &start, text, len);
517 gtk_text_buffer_real_insert_text (GtkTextBuffer *buffer,
522 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
523 g_return_if_fail (iter != NULL);
525 _gtk_text_btree_insert (iter, text, len);
527 g_signal_emit (buffer, signals[CHANGED], 0);
531 gtk_text_buffer_emit_insert (GtkTextBuffer *buffer,
536 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
537 g_return_if_fail (iter != NULL);
538 g_return_if_fail (text != NULL);
543 g_return_if_fail (g_utf8_validate (text, len, NULL));
547 g_signal_emit (buffer, signals[INSERT_TEXT], 0,
553 * gtk_text_buffer_insert:
554 * @buffer: a #GtkTextBuffer
555 * @iter: a position in the buffer
556 * @text: UTF-8 format text to insert
557 * @len: length of text in bytes, or -1
559 * Inserts @len bytes of @text at position @iter. If @len is -1,
560 * @text must be nul-terminated and will be inserted in its
561 * entirety. Emits the "insert_text" signal; insertion actually occurs
562 * in the default handler for the signal. @iter is invalidated when
563 * insertion occurs (because the buffer contents change), but the
564 * default signal handler revalidates it to point to the end of the
569 gtk_text_buffer_insert (GtkTextBuffer *buffer,
574 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
575 g_return_if_fail (iter != NULL);
576 g_return_if_fail (text != NULL);
577 g_return_if_fail (gtk_text_iter_get_buffer (iter) == buffer);
579 gtk_text_buffer_emit_insert (buffer, iter, text, len);
583 * gtk_text_buffer_insert_at_cursor:
584 * @buffer: a #GtkTextBuffer
585 * @text: some text in UTF-8 format
586 * @len: length of text, in bytes
588 * Simply calls gtk_text_buffer_insert (), using the current
589 * cursor position as the insertion point.
592 gtk_text_buffer_insert_at_cursor (GtkTextBuffer *buffer,
598 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
599 g_return_if_fail (text != NULL);
601 gtk_text_buffer_get_iter_at_mark (buffer, &iter,
602 gtk_text_buffer_get_mark (buffer,
605 gtk_text_buffer_insert (buffer, &iter, text, len);
609 * gtk_text_buffer_insert_interactive:
610 * @buffer: a #GtkTextBuffer
611 * @iter: a position in @buffer
612 * @text: some UTF-8 text
613 * @len: length of text in bytes, or -1
614 * @default_editable: default editability of buffer
616 * Like gtk_text_buffer_insert (), but the insertion will not occur if
617 * @iter is at a non-editable location in the buffer. Usually you
618 * want to prevent insertions at ineditable locations if the insertion
619 * results from a user action (is interactive).
621 * @default_editable indicates the editability of text that doesn't
622 * have a tag affecting editability applied to it. Typically the
623 * result of gtk_text_view_get_editable() is appropriate here.
625 * Return value: whether text was actually inserted
628 gtk_text_buffer_insert_interactive (GtkTextBuffer *buffer,
632 gboolean default_editable)
634 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
635 g_return_val_if_fail (text != NULL, FALSE);
636 g_return_val_if_fail (gtk_text_iter_get_buffer (iter) == buffer, FALSE);
638 if (gtk_text_iter_can_insert (iter, default_editable))
640 gtk_text_buffer_begin_user_action (buffer);
641 gtk_text_buffer_emit_insert (buffer, iter, text, len);
642 gtk_text_buffer_end_user_action (buffer);
650 * gtk_text_buffer_insert_interactive_at_cursor:
651 * @buffer: a #GtkTextBuffer
652 * @text: text in UTF-8 format
653 * @len: length of text in bytes, or -1
654 * @default_editable: default editability of buffer
656 * Calls gtk_text_buffer_insert_interactive () at the cursor
659 * @default_editable indicates the editability of text that doesn't
660 * have a tag affecting editability applied to it. Typically the
661 * result of gtk_text_view_get_editable() is appropriate here.
663 * Return value: whether text was actually inserted
666 gtk_text_buffer_insert_interactive_at_cursor (GtkTextBuffer *buffer,
669 gboolean default_editable)
673 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
674 g_return_val_if_fail (text != NULL, FALSE);
676 gtk_text_buffer_get_iter_at_mark (buffer, &iter,
677 gtk_text_buffer_get_mark (buffer,
680 return gtk_text_buffer_insert_interactive (buffer, &iter, text, len,
685 possibly_not_text (gunichar ch,
688 return ch == GTK_TEXT_UNKNOWN_CHAR;
692 insert_text_range (GtkTextBuffer *buffer,
694 const GtkTextIter *orig_start,
695 const GtkTextIter *orig_end,
696 gboolean interactive)
700 text = gtk_text_iter_get_text (orig_start, orig_end);
702 gtk_text_buffer_emit_insert (buffer, iter, text, -1);
707 typedef struct _Range Range;
710 GtkTextBuffer *buffer;
711 GtkTextMark *start_mark;
712 GtkTextMark *end_mark;
713 GtkTextMark *whole_end_mark;
714 GtkTextIter *range_start;
715 GtkTextIter *range_end;
716 GtkTextIter *whole_end;
720 save_range (GtkTextIter *range_start,
721 GtkTextIter *range_end,
722 GtkTextIter *whole_end)
726 r = g_new (Range, 1);
728 r->buffer = gtk_text_iter_get_buffer (range_start);
729 g_object_ref (r->buffer);
732 gtk_text_buffer_create_mark (gtk_text_iter_get_buffer (range_start),
737 gtk_text_buffer_create_mark (gtk_text_iter_get_buffer (range_start),
743 gtk_text_buffer_create_mark (gtk_text_iter_get_buffer (range_start),
748 r->range_start = range_start;
749 r->range_end = range_end;
750 r->whole_end = whole_end;
756 restore_range (Range *r)
758 gtk_text_buffer_get_iter_at_mark (r->buffer,
762 gtk_text_buffer_get_iter_at_mark (r->buffer,
766 gtk_text_buffer_get_iter_at_mark (r->buffer,
770 gtk_text_buffer_delete_mark (r->buffer, r->start_mark);
771 gtk_text_buffer_delete_mark (r->buffer, r->end_mark);
772 gtk_text_buffer_delete_mark (r->buffer, r->whole_end_mark);
774 /* Due to the gravities on the marks, the ordering could have
775 * gotten mangled; we switch to an empty range in that
779 if (gtk_text_iter_compare (r->range_start, r->range_end) > 0)
780 *r->range_start = *r->range_end;
782 if (gtk_text_iter_compare (r->range_end, r->whole_end) > 0)
783 *r->range_end = *r->whole_end;
785 g_object_unref (r->buffer);
790 insert_range_untagged (GtkTextBuffer *buffer,
792 const GtkTextIter *orig_start,
793 const GtkTextIter *orig_end,
794 gboolean interactive)
796 GtkTextIter range_start;
797 GtkTextIter range_end;
798 GtkTextIter start, end;
799 GtkTextBuffer *src_buffer;
802 if (gtk_text_iter_equal (orig_start, orig_end))
808 src_buffer = gtk_text_iter_get_buffer (&start);
815 if (gtk_text_iter_equal (&range_start, &range_end))
817 /* Figure out how to move forward */
819 g_assert (gtk_text_iter_compare (&range_end, &end) <= 0);
821 if (gtk_text_iter_equal (&range_end, &end))
823 /* nothing left to do */
826 else if (gtk_text_iter_get_char (&range_end) == GTK_TEXT_UNKNOWN_CHAR)
828 GdkPixbuf *pixbuf = NULL;
829 GtkTextChildAnchor *anchor = NULL;
830 pixbuf = gtk_text_iter_get_pixbuf (&range_end);
831 anchor = gtk_text_iter_get_child_anchor (&range_end);
835 r = save_range (&range_start,
839 gtk_text_buffer_insert_pixbuf (buffer,
846 gtk_text_iter_forward_char (&range_end);
848 range_start = range_end;
852 /* Just skip anchors */
854 gtk_text_iter_forward_char (&range_end);
855 range_start = range_end;
859 /* The GTK_TEXT_UNKNOWN_CHAR was in a text segment, so
862 gtk_text_iter_forward_find_char (&range_end,
863 possibly_not_text, NULL,
866 g_assert (gtk_text_iter_compare (&range_end, &end) <= 0);
871 /* Text segment starts here, so forward search to
872 * find its possible endpoint
874 gtk_text_iter_forward_find_char (&range_end,
875 possibly_not_text, NULL,
878 g_assert (gtk_text_iter_compare (&range_end, &end) <= 0);
883 r = save_range (&range_start,
887 insert_text_range (buffer,
896 range_start = range_end;
902 insert_range_not_inside_self (GtkTextBuffer *buffer,
904 const GtkTextIter *orig_start,
905 const GtkTextIter *orig_end,
906 gboolean interactive)
908 /* Find each range of uniformly-tagged text, insert it,
909 * then apply the tags.
911 GtkTextIter start = *orig_start;
912 GtkTextIter end = *orig_end;
913 GtkTextIter range_start;
914 GtkTextIter range_end;
915 GtkTextBuffer *src_buffer;
917 if (gtk_text_iter_equal (orig_start, orig_end))
920 src_buffer = gtk_text_iter_get_buffer (orig_start);
922 gtk_text_iter_order (&start, &end);
930 GtkTextIter start_iter;
935 if (gtk_text_iter_equal (&range_start, &end))
936 break; /* All done */
938 g_assert (gtk_text_iter_compare (&range_start, &end) < 0);
940 gtk_text_iter_forward_to_tag_toggle (&range_end, NULL);
942 g_assert (!gtk_text_iter_equal (&range_start, &range_end));
944 /* Clamp to the end iterator */
945 if (gtk_text_iter_compare (&range_end, &end) > 0)
948 /* We have a range with unique tags; insert it, and
951 start_offset = gtk_text_iter_get_offset (iter);
953 r = save_range (&range_start, &range_end, &end);
955 insert_range_untagged (buffer, iter, &range_start, &range_end, interactive);
960 gtk_text_buffer_get_iter_at_offset (buffer, &start_iter, start_offset);
962 tags = gtk_text_iter_get_tags (&range_start);
964 while (tmp_list != NULL)
966 gtk_text_buffer_apply_tag (buffer,
971 tmp_list = g_slist_next (tmp_list);
975 range_start = range_end;
980 gtk_text_buffer_real_insert_range (GtkTextBuffer *buffer,
982 const GtkTextIter *orig_start,
983 const GtkTextIter *orig_end,
984 gboolean interactive)
986 GtkTextBuffer *src_buffer;
988 /* Find each range of uniformly-tagged text, insert it,
989 * then apply the tags.
991 if (gtk_text_iter_equal (orig_start, orig_end))
995 gtk_text_buffer_begin_user_action (buffer);
997 src_buffer = gtk_text_iter_get_buffer (orig_start);
999 if (gtk_text_iter_get_buffer (iter) != src_buffer ||
1000 !gtk_text_iter_in_range (iter, orig_start, orig_end))
1002 insert_range_not_inside_self (buffer, iter, orig_start, orig_end, interactive);
1006 /* If you insert a range into itself, it could loop infinitely
1007 * because the region being copied keeps growing as we insert. So
1008 * we have to separately copy the range before and after
1009 * the insertion point.
1011 GtkTextIter start = *orig_start;
1012 GtkTextIter end = *orig_end;
1013 GtkTextIter range_start;
1014 GtkTextIter range_end;
1018 gtk_text_iter_order (&start, &end);
1020 range_start = start;
1022 first_half = save_range (&range_start, &range_end, &end);
1024 range_start = *iter;
1026 second_half = save_range (&range_start, &range_end, &end);
1028 restore_range (first_half);
1029 insert_range_not_inside_self (buffer, iter, &range_start, &range_end, interactive);
1031 restore_range (second_half);
1032 insert_range_not_inside_self (buffer, iter, &range_start, &range_end, interactive);
1036 gtk_text_buffer_end_user_action (buffer);
1040 * gtk_text_buffer_insert_range:
1041 * @buffer: a #GtkTextBuffer
1042 * @iter: a position in @buffer
1043 * @start: a position in a #GtkTextBuffer
1044 * @end: another position in the same buffer as @start
1046 * Copies text, tags, and pixbufs between @start and @end (the order
1047 * of @start and @end doesn't matter) and inserts the copy at @iter.
1048 * Used instead of simply getting/inserting text because it preserves
1049 * images and tags. If @start and @end are in a different buffer from
1050 * @buffer, the two buffers must share the same tag table.
1052 * Implemented via emissions of the insert_text and apply_tag signals,
1056 gtk_text_buffer_insert_range (GtkTextBuffer *buffer,
1058 const GtkTextIter *start,
1059 const GtkTextIter *end)
1061 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1062 g_return_if_fail (iter != NULL);
1063 g_return_if_fail (start != NULL);
1064 g_return_if_fail (end != NULL);
1065 g_return_if_fail (gtk_text_iter_get_buffer (start) ==
1066 gtk_text_iter_get_buffer (end));
1067 g_return_if_fail (gtk_text_iter_get_buffer (start)->tag_table ==
1069 g_return_if_fail (gtk_text_iter_get_buffer (iter) == buffer);
1071 gtk_text_buffer_real_insert_range (buffer, iter, start, end, FALSE);
1075 * gtk_text_buffer_insert_range_interactive:
1076 * @buffer: a #GtkTextBuffer
1077 * @iter: a position in @buffer
1078 * @start: a position in a #GtkTextBuffer
1079 * @end: another position in the same buffer as @start
1080 * @default_editable: default editability of the buffer
1082 * Same as gtk_text_buffer_insert_range(), but does nothing if the
1083 * insertion point isn't editable. The @default_editable parameter
1084 * indicates whether the text is editable at @iter if no tags
1085 * enclosing @iter affect editability. Typically the result of
1086 * gtk_text_view_get_editable() is appropriate here.
1088 * Returns: whether an insertion was possible at @iter
1091 gtk_text_buffer_insert_range_interactive (GtkTextBuffer *buffer,
1093 const GtkTextIter *start,
1094 const GtkTextIter *end,
1095 gboolean default_editable)
1097 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
1098 g_return_val_if_fail (iter != NULL, FALSE);
1099 g_return_val_if_fail (start != NULL, FALSE);
1100 g_return_val_if_fail (end != NULL, FALSE);
1101 g_return_val_if_fail (gtk_text_iter_get_buffer (start) ==
1102 gtk_text_iter_get_buffer (end), FALSE);
1103 g_return_val_if_fail (gtk_text_iter_get_buffer (start)->tag_table ==
1104 buffer->tag_table, FALSE);
1107 if (gtk_text_iter_can_insert (iter, default_editable))
1109 gtk_text_buffer_real_insert_range (buffer, iter, start, end, TRUE);
1117 * gtk_text_buffer_insert_with_tags:
1118 * @buffer: a #GtkTextBuffer
1119 * @iter: an iterator in @buffer
1121 * @len: length of @text, or -1
1122 * @first_tag: first tag to apply to @text
1123 * @Varargs: NULL-terminated list of tags to apply
1125 * Inserts @text into @buffer at @iter, applying the list of tags to
1126 * the newly-inserted text. The last tag specified must be NULL to
1127 * terminate the list. Equivalent to calling gtk_text_buffer_insert (),
1128 * then gtk_text_buffer_apply_tag () on the inserted text;
1129 * gtk_text_buffer_insert_with_tags () is just a convenience function.
1132 gtk_text_buffer_insert_with_tags (GtkTextBuffer *buffer,
1136 GtkTextTag *first_tag,
1144 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1145 g_return_if_fail (iter != NULL);
1146 g_return_if_fail (text != NULL);
1147 g_return_if_fail (gtk_text_iter_get_buffer (iter) == buffer);
1149 start_offset = gtk_text_iter_get_offset (iter);
1151 gtk_text_buffer_insert (buffer, iter, text, len);
1153 if (first_tag == NULL)
1156 gtk_text_buffer_get_iter_at_offset (buffer, &start, start_offset);
1158 va_start (args, first_tag);
1162 gtk_text_buffer_apply_tag (buffer, tag, &start, iter);
1164 tag = va_arg (args, GtkTextTag*);
1171 * gtk_text_buffer_insert_with_tags_by_name:
1172 * @buffer: a #GtkTextBuffer
1173 * @iter: position in @buffer
1175 * @len: length of @text, or -1
1176 * @first_tag_name: name of a tag to apply to @text
1177 * @Varargs: more tag names
1179 * Same as gtk_text_buffer_insert_with_tags (), but allows you
1180 * to pass in tag names instead of tag objects.
1183 gtk_text_buffer_insert_with_tags_by_name (GtkTextBuffer *buffer,
1187 const gchar *first_tag_name,
1193 const gchar *tag_name;
1195 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1196 g_return_if_fail (iter != NULL);
1197 g_return_if_fail (text != NULL);
1198 g_return_if_fail (gtk_text_iter_get_buffer (iter) == buffer);
1200 start_offset = gtk_text_iter_get_offset (iter);
1202 gtk_text_buffer_insert (buffer, iter, text, len);
1204 if (first_tag_name == NULL)
1207 gtk_text_buffer_get_iter_at_offset (buffer, &start, start_offset);
1209 va_start (args, first_tag_name);
1210 tag_name = first_tag_name;
1215 tag = gtk_text_tag_table_lookup (buffer->tag_table,
1220 g_warning ("%s: no tag with name '%s'!", G_STRLOC, tag_name);
1224 gtk_text_buffer_apply_tag (buffer, tag, &start, iter);
1226 tag_name = va_arg (args, const gchar*);
1238 gtk_text_buffer_real_delete_range (GtkTextBuffer *buffer,
1242 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1243 g_return_if_fail (start != NULL);
1244 g_return_if_fail (end != NULL);
1246 _gtk_text_btree_delete (start, end);
1248 /* may have deleted the selection... */
1249 update_selection_clipboards (buffer);
1251 g_signal_emit (buffer, signals[CHANGED], 0);
1255 gtk_text_buffer_emit_delete (GtkTextBuffer *buffer,
1259 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1260 g_return_if_fail (start != NULL);
1261 g_return_if_fail (end != NULL);
1263 if (gtk_text_iter_equal (start, end))
1266 gtk_text_iter_order (start, end);
1268 g_signal_emit (buffer,
1269 signals[DELETE_RANGE],
1275 * gtk_text_buffer_delete:
1276 * @buffer: a #GtkTextBuffer
1277 * @start: a position in @buffer
1278 * @end: another position in @buffer
1280 * Deletes text between @start and @end. The order of @start and @end
1281 * is not actually relevant; gtk_text_buffer_delete () will reorder
1282 * them. This function actually emits the "delete_range" signal, and
1283 * the default handler of that signal deletes the text. Because the
1284 * buffer is modified, all outstanding iterators become invalid after
1285 * calling this function; however, the @start and @end will be
1286 * re-initialized to point to the location where text was deleted.
1290 gtk_text_buffer_delete (GtkTextBuffer *buffer,
1294 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1295 g_return_if_fail (start != NULL);
1296 g_return_if_fail (end != NULL);
1297 g_return_if_fail (gtk_text_iter_get_buffer (start) == buffer);
1298 g_return_if_fail (gtk_text_iter_get_buffer (end) == buffer);
1300 gtk_text_buffer_emit_delete (buffer, start, end);
1304 * gtk_text_buffer_delete_interactive:
1305 * @buffer: a #GtkTextBuffer
1306 * @start_iter: start of range to delete
1307 * @end_iter: end of range
1308 * @default_editable: whether the buffer is editable by default
1310 * Deletes all <emphasis>editable</emphasis> text in the given range.
1311 * Calls gtk_text_buffer_delete () for each editable sub-range of
1312 * [@start,@end). @start and @end are revalidated to point to
1313 * the location of the last deleted range, or left untouched if
1314 * no text was deleted.
1316 * Return value: whether some text was actually deleted
1319 gtk_text_buffer_delete_interactive (GtkTextBuffer *buffer,
1320 GtkTextIter *start_iter,
1321 GtkTextIter *end_iter,
1322 gboolean default_editable)
1324 GtkTextMark *end_mark;
1325 GtkTextMark *start_mark;
1327 gboolean current_state;
1328 gboolean deleted_stuff = FALSE;
1330 /* Delete all editable text in the range start_iter, end_iter */
1332 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
1333 g_return_val_if_fail (start_iter != NULL, FALSE);
1334 g_return_val_if_fail (end_iter != NULL, FALSE);
1335 g_return_val_if_fail (gtk_text_iter_get_buffer (start_iter) == buffer, FALSE);
1336 g_return_val_if_fail (gtk_text_iter_get_buffer (end_iter) == buffer, FALSE);
1339 gtk_text_buffer_begin_user_action (buffer);
1341 gtk_text_iter_order (start_iter, end_iter);
1343 start_mark = gtk_text_buffer_create_mark (buffer, NULL,
1345 end_mark = gtk_text_buffer_create_mark (buffer, NULL,
1349 current_state = gtk_text_iter_editable (&iter, default_editable);
1354 gboolean done = FALSE;
1357 gtk_text_iter_forward_to_tag_toggle (&iter, NULL);
1359 gtk_text_buffer_get_iter_at_mark (buffer, &end, end_mark);
1361 if (gtk_text_iter_compare (&iter, &end) >= 0)
1364 iter = end; /* clamp to the last boundary */
1367 new_state = gtk_text_iter_editable (&iter, default_editable);
1369 if (current_state == new_state)
1375 /* We're ending an editable region. Delete said region. */
1378 gtk_text_buffer_get_iter_at_mark (buffer, &start, start_mark);
1380 gtk_text_buffer_emit_delete (buffer, &start, &iter);
1382 deleted_stuff = TRUE;
1384 /* revalidate user's iterators. */
1385 *start_iter = start;
1395 if (current_state && !new_state)
1397 /* End of an editable region. Delete it. */
1400 gtk_text_buffer_get_iter_at_mark (buffer, &start, start_mark);
1402 gtk_text_buffer_emit_delete (buffer, &start, &iter);
1404 current_state = FALSE;
1405 deleted_stuff = TRUE;
1407 /* revalidate user's iterators. */
1408 *start_iter = start;
1413 /* We are at the start of an editable region. We won't be deleting
1414 * the previous region. Move start mark to start of this region.
1417 g_assert (!current_state && new_state);
1419 gtk_text_buffer_move_mark (buffer, start_mark,
1423 current_state = TRUE;
1431 gtk_text_buffer_delete_mark (buffer, start_mark);
1432 gtk_text_buffer_delete_mark (buffer, end_mark);
1434 gtk_text_buffer_end_user_action (buffer);
1436 return deleted_stuff;
1440 * Extracting textual buffer contents
1444 * gtk_text_buffer_get_text:
1445 * @buffer: a #GtkTextBuffer
1446 * @start: start of a range
1447 * @end: end of a range
1448 * @include_hidden_chars: whether to include invisible text
1450 * Returns the text in the range [@start,@end). Excludes undisplayed
1451 * text (text marked with tags that set the invisibility attribute) if
1452 * @include_hidden_chars is FALSE. Does not include characters
1453 * representing embedded images, so byte and character indexes into
1454 * the returned string do <emphasis>not</emphasis> correspond to byte
1455 * and character indexes into the buffer. Contrast with
1456 * gtk_text_buffer_get_slice ().
1458 * Return value: an allocated UTF-8 string
1461 gtk_text_buffer_get_text (GtkTextBuffer *buffer,
1462 const GtkTextIter *start,
1463 const GtkTextIter *end,
1464 gboolean include_hidden_chars)
1466 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
1467 g_return_val_if_fail (start != NULL, NULL);
1468 g_return_val_if_fail (end != NULL, NULL);
1469 g_return_val_if_fail (gtk_text_iter_get_buffer (start) == buffer, NULL);
1470 g_return_val_if_fail (gtk_text_iter_get_buffer (end) == buffer, NULL);
1472 if (include_hidden_chars)
1473 return gtk_text_iter_get_text (start, end);
1475 return gtk_text_iter_get_visible_text (start, end);
1479 * gtk_text_buffer_get_slice:
1480 * @buffer: a #GtkTextBuffer
1481 * @start: start of a range
1482 * @end: end of a range
1483 * @include_hidden_chars: whether to include invisible text
1485 * Returns the text in the range [@start,@end). Excludes undisplayed
1486 * text (text marked with tags that set the invisibility attribute) if
1487 * @include_hidden_chars is FALSE. The returned string includes a
1488 * 0xFFFC character whenever the buffer contains
1489 * embedded images, so byte and character indexes into
1490 * the returned string <emphasis>do</emphasis> correspond to byte
1491 * and character indexes into the buffer. Contrast with
1492 * gtk_text_buffer_get_text (). Note that 0xFFFC can occur in normal
1493 * text as well, so it is not a reliable indicator that a pixbuf or
1494 * widget is in the buffer.
1496 * Return value: an allocated UTF-8 string
1499 gtk_text_buffer_get_slice (GtkTextBuffer *buffer,
1500 const GtkTextIter *start,
1501 const GtkTextIter *end,
1502 gboolean include_hidden_chars)
1504 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
1505 g_return_val_if_fail (start != NULL, NULL);
1506 g_return_val_if_fail (end != NULL, NULL);
1507 g_return_val_if_fail (gtk_text_iter_get_buffer (start) == buffer, NULL);
1508 g_return_val_if_fail (gtk_text_iter_get_buffer (end) == buffer, NULL);
1510 if (include_hidden_chars)
1511 return gtk_text_iter_get_slice (start, end);
1513 return gtk_text_iter_get_visible_slice (start, end);
1521 gtk_text_buffer_real_insert_pixbuf (GtkTextBuffer *buffer,
1525 _gtk_text_btree_insert_pixbuf (iter, pixbuf);
1527 g_signal_emit (buffer, signals[CHANGED], 0);
1531 * gtk_text_buffer_insert_pixbuf:
1532 * @buffer: a #GtkTextBuffer
1533 * @iter: location to insert the pixbuf
1534 * @pixbuf: a #GdkPixbuf
1536 * Inserts an image into the text buffer at @iter. The image will be
1537 * counted as one character in character counts, and when obtaining
1538 * the buffer contents as a string, will be represented by the Unicode
1539 * "object replacement character" 0xFFFC. Note that the "slice"
1540 * variants for obtaining portions of the buffer as a string include
1541 * this character for pixbufs, but the "text" variants do
1542 * not. e.g. see gtk_text_buffer_get_slice() and
1543 * gtk_text_buffer_get_text().
1547 gtk_text_buffer_insert_pixbuf (GtkTextBuffer *buffer,
1551 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1552 g_return_if_fail (iter != NULL);
1553 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
1554 g_return_if_fail (gtk_text_iter_get_buffer (iter) == buffer);
1556 g_signal_emit (buffer, signals[INSERT_PIXBUF], 0,
1566 gtk_text_buffer_real_insert_anchor (GtkTextBuffer *buffer,
1568 GtkTextChildAnchor *anchor)
1570 _gtk_text_btree_insert_child_anchor (iter, anchor);
1572 g_signal_emit (buffer, signals[CHANGED], 0);
1576 * gtk_text_buffer_insert_child_anchor:
1577 * @buffer: a #GtkTextBuffer
1578 * @iter: location to insert the anchor
1579 * @anchor: a #GtkTextChildAnchor
1581 * Inserts a child widget anchor into the text buffer at @iter. The
1582 * anchor will be counted as one character in character counts, and
1583 * when obtaining the buffer contents as a string, will be represented
1584 * by the Unicode "object replacement character" 0xFFFC. Note that the
1585 * "slice" variants for obtaining portions of the buffer as a string
1586 * include this character for child anchors, but the "text" variants do
1587 * not. e.g. see gtk_text_buffer_get_slice() and
1588 * gtk_text_buffer_get_text(). Consider
1589 * gtk_text_buffer_create_child_anchor() as a more convenient
1590 * alternative to this function. The buffer will add a reference to
1591 * the anchor, so you can unref it after insertion.
1595 gtk_text_buffer_insert_child_anchor (GtkTextBuffer *buffer,
1597 GtkTextChildAnchor *anchor)
1599 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1600 g_return_if_fail (iter != NULL);
1601 g_return_if_fail (GTK_IS_TEXT_CHILD_ANCHOR (anchor));
1602 g_return_if_fail (gtk_text_iter_get_buffer (iter) == buffer);
1604 g_signal_emit (buffer, signals[INSERT_CHILD_ANCHOR], 0,
1610 * gtk_text_buffer_create_child_anchor:
1611 * @buffer: a #GtkTextBuffer
1612 * @iter: location in the buffer
1614 * This is a convenience function which simply creates a child anchor
1615 * with gtk_text_child_anchor_new() and inserts it into the buffer
1616 * with gtk_text_buffer_insert_child_anchor(). The new anchor is
1617 * owned by the buffer; no reference count is returned to
1618 * the caller of gtk_text_buffer_create_child_anchor().
1620 * Return value: the created child anchor
1623 gtk_text_buffer_create_child_anchor (GtkTextBuffer *buffer,
1626 GtkTextChildAnchor *anchor;
1628 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
1629 g_return_val_if_fail (iter != NULL, NULL);
1630 g_return_val_if_fail (gtk_text_iter_get_buffer (iter) == buffer, NULL);
1632 anchor = gtk_text_child_anchor_new ();
1634 gtk_text_buffer_insert_child_anchor (buffer, iter, anchor);
1636 g_object_unref (anchor);
1646 gtk_text_buffer_mark_set (GtkTextBuffer *buffer,
1647 const GtkTextIter *location,
1650 /* IMO this should NOT work like insert_text and delete_range,
1651 * where the real action happens in the default handler.
1653 * The reason is that the default handler would be _required_,
1654 * i.e. the whole widget would start breaking and segfaulting if the
1655 * default handler didn't get run. So you can't really override the
1656 * default handler or stop the emission; that is, this signal is
1657 * purely for notification, and not to allow users to modify the
1661 g_object_ref (mark);
1663 g_signal_emit (buffer,
1669 g_object_unref (mark);
1673 * gtk_text_buffer_set_mark:
1674 * @buffer: a #GtkTextBuffer
1675 * @mark_name: name of the mark
1676 * @iter: location for the mark.
1677 * @left_gravity: if the mark is created by this function, gravity for the new
1679 * @should_exist: if %TRUE, warn if the mark does not exist, and return
1682 * Move the mark to the given position, if not @should_exist, create the mark.
1684 * Return value: mark
1687 gtk_text_buffer_set_mark (GtkTextBuffer *buffer,
1688 GtkTextMark *existing_mark,
1689 const gchar *mark_name,
1690 const GtkTextIter *iter,
1691 gboolean left_gravity,
1692 gboolean should_exist)
1694 GtkTextIter location;
1697 g_return_val_if_fail (gtk_text_iter_get_buffer (iter) == buffer, NULL);
1699 mark = _gtk_text_btree_set_mark (get_btree (buffer),
1706 if (_gtk_text_btree_mark_is_insert (get_btree (buffer), mark) ||
1707 _gtk_text_btree_mark_is_selection_bound (get_btree (buffer), mark))
1709 update_selection_clipboards (buffer);
1712 _gtk_text_btree_get_iter_at_mark (get_btree (buffer),
1716 gtk_text_buffer_mark_set (buffer, &location, mark);
1722 * gtk_text_buffer_create_mark:
1723 * @buffer: a #GtkTextBuffer
1724 * @mark_name: name for mark, or %NULL
1725 * @where: location to place mark
1726 * @left_gravity: whether the mark has left gravity
1728 * Creates a mark at position @where. If @mark_name is %NULL, the mark
1729 * is anonymous; otherwise, the mark can be retrieved by name using
1730 * gtk_text_buffer_get_mark (). If a mark has left gravity, and text is
1731 * inserted at the mark's current location, the mark will be moved to
1732 * the left of the newly-inserted text. If the mark has right gravity
1733 * (@left_gravity = %FALSE), the mark will end up on the right of
1734 * newly-inserted text. The standard left-to-right cursor is a mark
1735 * with right gravity (when you type, the cursor stays on the right
1736 * side of the text you're typing).
1738 * The caller of this function does <emphasis>not</emphasis> own a reference
1739 * to the returned #GtkTextMark, so you can ignore the return value
1740 * if you like. Marks are owned by the buffer and go away when the
1743 * Emits the "mark_set" signal as notification of the mark's initial
1746 * Return value: the new #GtkTextMark object
1749 gtk_text_buffer_create_mark (GtkTextBuffer *buffer,
1750 const gchar *mark_name,
1751 const GtkTextIter *where,
1752 gboolean left_gravity)
1754 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
1756 return gtk_text_buffer_set_mark (buffer, NULL, mark_name, where,
1757 left_gravity, FALSE);
1761 * gtk_text_buffer_move_mark:
1762 * @buffer: a #GtkTextBuffer
1763 * @mark: a #GtkTextMark
1764 * @where: new location for @mark in @buffer
1766 * Moves @mark to the new location @where. Emits the "mark_set" signal
1767 * as notification of the move.
1770 gtk_text_buffer_move_mark (GtkTextBuffer *buffer,
1772 const GtkTextIter *where)
1774 g_return_if_fail (GTK_IS_TEXT_MARK (mark));
1775 g_return_if_fail (!gtk_text_mark_get_deleted (mark));
1776 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1778 gtk_text_buffer_set_mark (buffer, mark, NULL, where, FALSE, TRUE);
1782 * gtk_text_buffer_get_iter_at_mark:
1783 * @buffer: a #GtkTextBuffer
1784 * @iter: iterator to initialize
1785 * @mark: a #GtkTextMark in @buffer
1787 * Initializes @iter with the current position of @mark.
1790 gtk_text_buffer_get_iter_at_mark (GtkTextBuffer *buffer,
1794 g_return_if_fail (GTK_IS_TEXT_MARK (mark));
1795 g_return_if_fail (!gtk_text_mark_get_deleted (mark));
1796 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1798 _gtk_text_btree_get_iter_at_mark (get_btree (buffer),
1804 * gtk_text_buffer_delete_mark:
1805 * @buffer: a #GtkTextBuffer
1806 * @mark: a #GtkTextMark in @buffer
1808 * Deletes @mark, so that it's no longer located anywhere in the
1809 * buffer. Removes the reference the buffer holds to the mark, so if
1810 * you haven't called g_object_ref () on the mark, it will be freed. Even
1811 * if the mark isn't freed, most operations on @mark become
1812 * invalid. There is no way to undelete a
1813 * mark. gtk_text_mark_get_deleted () will return TRUE after this
1814 * function has been called on a mark; gtk_text_mark_get_deleted ()
1815 * indicates that a mark no longer belongs to a buffer. The "mark_deleted"
1816 * signal will be emitted as notification after the mark is deleted.
1819 gtk_text_buffer_delete_mark (GtkTextBuffer *buffer,
1822 g_return_if_fail (GTK_IS_TEXT_MARK (mark));
1823 g_return_if_fail (!gtk_text_mark_get_deleted (mark));
1824 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1826 g_object_ref (mark);
1828 _gtk_text_btree_remove_mark (get_btree (buffer), mark);
1830 /* See rationale above for MARK_SET on why we emit this after
1831 * removing the mark, rather than removing the mark in a default
1834 g_signal_emit (buffer, signals[MARK_DELETED],
1838 g_object_unref (mark);
1842 * gtk_text_buffer_get_mark:
1843 * @buffer: a #GtkTextBuffer
1844 * @name: a mark name
1846 * Returns the mark named @name in buffer @buffer, or NULL if no such
1847 * mark exists in the buffer.
1849 * Return value: a #GtkTextMark, or NULL
1852 gtk_text_buffer_get_mark (GtkTextBuffer *buffer,
1857 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
1858 g_return_val_if_fail (name != NULL, NULL);
1860 mark = _gtk_text_btree_get_mark_by_name (get_btree (buffer), name);
1867 * gtk_text_buffer_move_mark_by_name:
1868 * @buffer: a #GtkTextBuffer
1869 * @name: name of a mark
1870 * @where: new location for mark
1872 * Moves the mark named @name (which must exist) to location @where.
1873 * See gtk_text_buffer_move_mark () for details.
1876 gtk_text_buffer_move_mark_by_name (GtkTextBuffer *buffer,
1878 const GtkTextIter *where)
1882 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1883 g_return_if_fail (name != NULL);
1885 mark = _gtk_text_btree_get_mark_by_name (get_btree (buffer), name);
1889 g_warning ("%s: no mark named '%s'", G_STRLOC, name);
1893 gtk_text_buffer_move_mark (buffer, mark, where);
1897 * gtk_text_buffer_delete_mark_by_name:
1898 * @buffer: a #GtkTextBuffer
1899 * @name: name of a mark in @buffer
1901 * Deletes the mark named @name; the mark must exist. See
1902 * gtk_text_buffer_delete_mark () for details.
1905 gtk_text_buffer_delete_mark_by_name (GtkTextBuffer *buffer,
1910 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1911 g_return_if_fail (name != NULL);
1913 mark = _gtk_text_btree_get_mark_by_name (get_btree (buffer), name);
1917 g_warning ("%s: no mark named '%s'", G_STRLOC, name);
1921 gtk_text_buffer_delete_mark (buffer, mark);
1925 * gtk_text_buffer_get_insert:
1926 * @buffer: a #GtkTextBuffer
1928 * Returns the mark that represents the cursor (insertion point).
1929 * Equivalent to calling gtk_text_buffer_get_mark () to get the mark
1930 * named "insert", but very slightly more efficient, and involves less
1933 * Return value: insertion point mark
1936 gtk_text_buffer_get_insert (GtkTextBuffer *buffer)
1938 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
1940 /* FIXME use struct member in btree */
1941 return gtk_text_buffer_get_mark (buffer, "insert");
1945 * gtk_text_buffer_get_selection_bound:
1946 * @buffer: a #GtkTextBuffer
1948 * Returns the mark that represents the selection bound. Equivalent
1949 * to calling gtk_text_buffer_get_mark () to get the mark named
1950 * "selection_bound", but very slightly more efficient, and involves
1953 * The currently-selected text in @buffer is the region between the
1954 * "selection_bound" and "insert" marks. If "selection_bound" and
1955 * "insert" are in the same place, then there is no current selection.
1956 * gtk_text_buffer_get_selection_bounds() is another convenient function
1957 * for handling the selection, if you just want to know whether there's a
1958 * selection and what its bounds are.
1960 * Return value: selection bound mark
1963 gtk_text_buffer_get_selection_bound (GtkTextBuffer *buffer)
1965 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
1967 /* FIXME use struct member in btree */
1968 return gtk_text_buffer_get_mark (buffer, "selection_bound");
1972 * gtk_text_buffer_get_iter_at_child_anchor:
1973 * @buffer: a #GtkTextBuffer
1974 * @iter: an iterator to be initialized
1975 * @anchor: a child anchor that appears in @buffer
1977 * Obtains the location of @anchor within @buffer.
1981 gtk_text_buffer_get_iter_at_child_anchor (GtkTextBuffer *buffer,
1983 GtkTextChildAnchor *anchor)
1985 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1986 g_return_if_fail (iter != NULL);
1987 g_return_if_fail (GTK_IS_TEXT_CHILD_ANCHOR (anchor));
1988 g_return_if_fail (!gtk_text_child_anchor_get_deleted (anchor));
1990 _gtk_text_btree_get_iter_at_child_anchor (get_btree (buffer),
1996 * gtk_text_buffer_place_cursor:
1997 * @buffer: a #GtkTextBuffer
1998 * @where: where to put the cursor
2000 * This function moves the "insert" and "selection_bound" marks
2001 * simultaneously. If you move them to the same place in two steps
2002 * with gtk_text_buffer_move_mark(), you will temporarily select a
2003 * region in between their old and new locations, which can be pretty
2004 * inefficient since the temporarily-selected region will force stuff
2005 * to be recalculated. This function moves them as a unit, which can
2009 gtk_text_buffer_place_cursor (GtkTextBuffer *buffer,
2010 const GtkTextIter *where)
2014 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2018 _gtk_text_btree_place_cursor (get_btree (buffer), &real);
2019 gtk_text_buffer_mark_set (buffer, &real,
2020 gtk_text_buffer_get_mark (buffer,
2022 gtk_text_buffer_mark_set (buffer, &real,
2023 gtk_text_buffer_get_mark (buffer,
2024 "selection_bound"));
2032 * gtk_text_buffer_create_tag:
2033 * @buffer: a #GtkTextBuffer
2034 * @tag_name: name of the new tag, or %NULL
2035 * @first_property_name: name of first property to set, or %NULL
2036 * @Varargs: %NULL-terminated list of property names and values
2039 * Creates a tag and adds it to the tag table for @buffer.
2040 * Equivalent to calling gtk_text_tag_new () and then adding the
2041 * tag to the buffer's tag table. The returned tag is owned by
2042 * the buffer's tag table, so the ref count will be equal to one.
2044 * If @tag_name is %NULL, the tag is anonymous.
2046 * If @tag_name is non-%NULL, a tag called @tag_name must not already
2047 * exist in the tag table for this buffer.
2049 * The @first_property_name argument and subsequent arguments are a list
2050 * of properties to set on the tag, as with g_object_set().
2052 * Return value: a new tag
2055 gtk_text_buffer_create_tag (GtkTextBuffer *buffer,
2056 const gchar *tag_name,
2057 const gchar *first_property_name,
2063 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
2065 tag = gtk_text_tag_new (tag_name);
2067 gtk_text_tag_table_add (get_table (buffer), tag);
2069 if (first_property_name)
2071 va_start (list, first_property_name);
2072 g_object_set_valist (G_OBJECT (tag), first_property_name, list);
2076 g_object_unref (tag);
2082 gtk_text_buffer_real_apply_tag (GtkTextBuffer *buffer,
2084 const GtkTextIter *start,
2085 const GtkTextIter *end)
2087 if (tag->table != buffer->tag_table)
2089 g_warning ("Can only apply tags that are in the tag table for the buffer");
2093 _gtk_text_btree_tag (start, end, tag, TRUE);
2097 gtk_text_buffer_real_remove_tag (GtkTextBuffer *buffer,
2099 const GtkTextIter *start,
2100 const GtkTextIter *end)
2102 if (tag->table != buffer->tag_table)
2104 g_warning ("Can only remove tags that are in the tag table for the buffer");
2108 _gtk_text_btree_tag (start, end, tag, FALSE);
2112 gtk_text_buffer_real_changed (GtkTextBuffer *buffer)
2114 gtk_text_buffer_set_modified (buffer, TRUE);
2118 gtk_text_buffer_emit_tag (GtkTextBuffer *buffer,
2121 const GtkTextIter *start,
2122 const GtkTextIter *end)
2124 GtkTextIter start_tmp = *start;
2125 GtkTextIter end_tmp = *end;
2127 g_return_if_fail (tag != NULL);
2129 gtk_text_iter_order (&start_tmp, &end_tmp);
2132 g_signal_emit (buffer, signals[APPLY_TAG],
2134 tag, &start_tmp, &end_tmp);
2136 g_signal_emit (buffer, signals[REMOVE_TAG],
2138 tag, &start_tmp, &end_tmp);
2143 * gtk_text_buffer_apply_tag:
2144 * @buffer: a #GtkTextBuffer
2145 * @tag: a #GtkTextTag
2146 * @start: one bound of range to be tagged
2147 * @end: other bound of range to be tagged
2149 * Emits the "apply_tag" signal on @buffer. The default
2150 * handler for the signal applies @tag to the given range.
2151 * @start and @end do not have to be in order.
2155 gtk_text_buffer_apply_tag (GtkTextBuffer *buffer,
2157 const GtkTextIter *start,
2158 const GtkTextIter *end)
2160 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2161 g_return_if_fail (GTK_IS_TEXT_TAG (tag));
2162 g_return_if_fail (start != NULL);
2163 g_return_if_fail (end != NULL);
2164 g_return_if_fail (gtk_text_iter_get_buffer (start) == buffer);
2165 g_return_if_fail (gtk_text_iter_get_buffer (end) == buffer);
2166 g_return_if_fail (tag->table == buffer->tag_table);
2168 gtk_text_buffer_emit_tag (buffer, tag, TRUE, start, end);
2172 * gtk_text_buffer_remove_tag:
2173 * @buffer: a #GtkTextBuffer
2174 * @tag: a #GtkTextTag
2175 * @start: one bound of range to be untagged
2176 * @end: other bound of range to be untagged
2178 * Emits the "remove_tag" signal. The default handler for the signal
2179 * removes all occurrences of @tag from the given range. @start and
2180 * @end don't have to be in order.
2184 gtk_text_buffer_remove_tag (GtkTextBuffer *buffer,
2186 const GtkTextIter *start,
2187 const GtkTextIter *end)
2190 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2191 g_return_if_fail (GTK_IS_TEXT_TAG (tag));
2192 g_return_if_fail (start != NULL);
2193 g_return_if_fail (end != NULL);
2194 g_return_if_fail (gtk_text_iter_get_buffer (start) == buffer);
2195 g_return_if_fail (gtk_text_iter_get_buffer (end) == buffer);
2196 g_return_if_fail (tag->table == buffer->tag_table);
2198 gtk_text_buffer_emit_tag (buffer, tag, FALSE, start, end);
2203 * gtk_text_buffer_apply_tag_by_name:
2204 * @buffer: a #GtkTextBuffer
2205 * @name: name of a named #GtkTextTag
2206 * @start: one bound of range to be tagged
2207 * @end: other bound of range to be tagged
2209 * Calls gtk_text_tag_table_lookup() on the buffer's tag table to
2210 * get a #GtkTextTag, then calls gtk_text_buffer_apply_tag().
2214 gtk_text_buffer_apply_tag_by_name (GtkTextBuffer *buffer,
2216 const GtkTextIter *start,
2217 const GtkTextIter *end)
2221 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2222 g_return_if_fail (name != NULL);
2223 g_return_if_fail (start != NULL);
2224 g_return_if_fail (end != NULL);
2225 g_return_if_fail (gtk_text_iter_get_buffer (start) == buffer);
2226 g_return_if_fail (gtk_text_iter_get_buffer (end) == buffer);
2228 tag = gtk_text_tag_table_lookup (get_table (buffer),
2233 g_warning ("Unknown tag `%s'", name);
2237 gtk_text_buffer_emit_tag (buffer, tag, TRUE, start, end);
2241 * gtk_text_buffer_remove_tag_by_name:
2242 * @buffer: a #GtkTextBuffer
2243 * @name: name of a #GtkTextTag
2244 * @start: one bound of range to be untagged
2245 * @end: other bound of range to be untagged
2247 * Calls gtk_text_tag_table_lookup() on the buffer's tag table to
2248 * get a #GtkTextTag, then calls gtk_text_buffer_remove_tag().
2253 gtk_text_buffer_remove_tag_by_name (GtkTextBuffer *buffer,
2255 const GtkTextIter *start,
2256 const GtkTextIter *end)
2260 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2261 g_return_if_fail (name != NULL);
2262 g_return_if_fail (start != NULL);
2263 g_return_if_fail (end != NULL);
2264 g_return_if_fail (gtk_text_iter_get_buffer (start) == buffer);
2265 g_return_if_fail (gtk_text_iter_get_buffer (end) == buffer);
2267 tag = gtk_text_tag_table_lookup (get_table (buffer),
2272 g_warning ("Unknown tag `%s'", name);
2276 gtk_text_buffer_emit_tag (buffer, tag, FALSE, start, end);
2280 pointer_cmp (gconstpointer a,
2292 * gtk_text_buffer_remove_all_tags:
2293 * @buffer: a #GtkTextBuffer
2294 * @start: one bound of range to be untagged
2295 * @end: other bound of range to be untagged
2297 * Removes all tags in the range between @start and @end. Be careful
2298 * with this function; it could remove tags added in code unrelated to
2299 * the code you're currently writing. That is, using this function is
2300 * probably a bad idea if you have two or more unrelated code sections
2304 gtk_text_buffer_remove_all_tags (GtkTextBuffer *buffer,
2305 const GtkTextIter *start,
2306 const GtkTextIter *end)
2308 GtkTextIter first, second, tmp;
2314 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2315 g_return_if_fail (start != NULL);
2316 g_return_if_fail (end != NULL);
2317 g_return_if_fail (gtk_text_iter_get_buffer (start) == buffer);
2318 g_return_if_fail (gtk_text_iter_get_buffer (end) == buffer);
2323 gtk_text_iter_order (&first, &second);
2325 /* Get all tags turned on at the start */
2326 tags = gtk_text_iter_get_tags (&first);
2328 /* Find any that are toggled on within the range */
2330 while (gtk_text_iter_forward_to_tag_toggle (&tmp, NULL))
2335 if (gtk_text_iter_compare (&tmp, &second) >= 0)
2336 break; /* past the end of the range */
2338 toggled = gtk_text_iter_get_toggled_tags (&tmp, TRUE);
2340 /* We could end up with a really big-ass list here.
2343 tmp_list2 = toggled;
2344 while (tmp_list2 != NULL)
2346 tags = g_slist_prepend (tags, tmp_list2->data);
2348 tmp_list2 = g_slist_next (tmp_list2);
2351 g_slist_free (toggled);
2355 tags = g_slist_sort (tags, pointer_cmp);
2357 /* Strip duplicates */
2361 while (tmp_list != NULL)
2363 if (tag == tmp_list->data)
2367 prev->next = tmp_list->next;
2369 tmp_list->next = NULL;
2371 g_slist_free (tmp_list);
2373 tmp_list = prev->next;
2374 /* prev is unchanged */
2378 /* not a duplicate */
2379 tag = GTK_TEXT_TAG (tmp_list->data);
2381 tmp_list = tmp_list->next;
2385 g_slist_foreach (tags, (GFunc) g_object_ref, NULL);
2388 while (tmp_list != NULL)
2390 tag = GTK_TEXT_TAG (tmp_list->data);
2392 gtk_text_buffer_remove_tag (buffer, tag, &first, &second);
2394 tmp_list = tmp_list->next;
2397 g_slist_foreach (tags, (GFunc) g_object_unref, NULL);
2399 g_slist_free (tags);
2404 * Obtain various iterators
2408 * gtk_text_buffer_get_iter_at_line_offset:
2409 * @buffer: a #GtkTextBuffer
2410 * @iter: iterator to initialize
2411 * @line_number: line number counting from 0
2412 * @char_offset: char offset from start of line
2414 * Obtains an iterator pointing to @char_offset within the given
2415 * line. The @char_offset must exist, offsets off the end of the line
2416 * are not allowed. Note <emphasis>characters</emphasis>, not bytes;
2417 * UTF-8 may encode one character as multiple bytes.
2421 gtk_text_buffer_get_iter_at_line_offset (GtkTextBuffer *buffer,
2426 g_return_if_fail (iter != NULL);
2427 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2429 _gtk_text_btree_get_iter_at_line_char (get_btree (buffer),
2430 iter, line_number, char_offset);
2434 * gtk_text_buffer_get_iter_at_line_index:
2435 * @buffer: a #GtkTextBuffer
2436 * @iter: iterator to initialize
2437 * @line_number: line number counting from 0
2438 * @byte_index: byte index from start of line
2440 * Obtains an iterator pointing to @byte_index within the given line.
2441 * @byte_index must be the start of a UTF-8 character, and must not be
2442 * beyond the end of the line. Note <emphasis>bytes</emphasis>, not
2443 * characters; UTF-8 may encode one character as multiple bytes.
2447 gtk_text_buffer_get_iter_at_line_index (GtkTextBuffer *buffer,
2452 g_return_if_fail (iter != NULL);
2453 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2455 _gtk_text_btree_get_iter_at_line_byte (get_btree (buffer),
2456 iter, line_number, byte_index);
2460 * gtk_text_buffer_get_iter_at_line:
2461 * @buffer: a #GtkTextBuffer
2462 * @iter: iterator to initialize
2463 * @line_number: line number counting from 0
2465 * Initializes @iter to the start of the given line.
2468 gtk_text_buffer_get_iter_at_line (GtkTextBuffer *buffer,
2472 g_return_if_fail (iter != NULL);
2473 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2475 gtk_text_buffer_get_iter_at_line_offset (buffer, iter, line_number, 0);
2479 * gtk_text_buffer_get_iter_at_offset:
2480 * @buffer: a #GtkTextBuffer
2481 * @iter: iterator to initialize
2482 * @char_offset: char offset from start of buffer, counting from 0, or -1
2484 * Initializes @iter to a position @char_offset chars from the start
2485 * of the entire buffer. If @char_offset is -1 or greater than the number
2486 * of characters in the buffer, @iter is initialized to the end iterator,
2487 * the iterator one past the last valid character in the buffer.
2490 gtk_text_buffer_get_iter_at_offset (GtkTextBuffer *buffer,
2494 g_return_if_fail (iter != NULL);
2495 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2497 _gtk_text_btree_get_iter_at_char (get_btree (buffer), iter, char_offset);
2501 * gtk_text_buffer_get_start_iter:
2502 * @buffer: a #GtkTextBuffer
2503 * @iter: iterator to initialize
2505 * Initialized @iter with the first position in the text buffer. This
2506 * is the same as using gtk_text_buffer_get_iter_at_offset() to get
2507 * the iter at character offset 0.
2510 gtk_text_buffer_get_start_iter (GtkTextBuffer *buffer,
2513 g_return_if_fail (iter != NULL);
2514 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2516 _gtk_text_btree_get_iter_at_char (get_btree (buffer), iter, 0);
2520 * gtk_text_buffer_get_end_iter:
2521 * @buffer: a #GtkTextBuffer
2522 * @iter: iterator to initialize
2524 * Initializes @iter with the "end iterator," one past the last valid
2525 * character in the text buffer. If dereferenced with
2526 * gtk_text_iter_get_char(), the end iterator has a character value of
2527 * 0. The entire buffer lies in the range from the first position in
2528 * the buffer (call gtk_text_buffer_get_start_iter() to get
2529 * character position 0) to the end iterator.
2533 gtk_text_buffer_get_end_iter (GtkTextBuffer *buffer,
2536 g_return_if_fail (iter != NULL);
2537 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2539 _gtk_text_btree_get_end_iter (get_btree (buffer), iter);
2543 * gtk_text_buffer_get_bounds:
2544 * @buffer: a #GtkTextBuffer
2545 * @start: iterator to initialize with first position in the buffer
2546 * @end: iterator to initialize with the end iterator
2548 * Retrieves the first and last iterators in the buffer, i.e. the
2549 * entire buffer lies within the range [@start,@end).
2553 gtk_text_buffer_get_bounds (GtkTextBuffer *buffer,
2557 g_return_if_fail (start != NULL);
2558 g_return_if_fail (end != NULL);
2559 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2561 _gtk_text_btree_get_iter_at_char (get_btree (buffer), start, 0);
2562 _gtk_text_btree_get_end_iter (get_btree (buffer), end);
2570 * gtk_text_buffer_get_modified:
2571 * @buffer: a #GtkTextBuffer
2573 * Indicates whether the buffer has been modified since the last call
2574 * to gtk_text_buffer_set_modified() set the modification flag to
2575 * %FALSE. Used for example to enable a "save" function in a text
2578 * Return value: %TRUE if the buffer has been modified
2581 gtk_text_buffer_get_modified (GtkTextBuffer *buffer)
2583 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
2585 return buffer->modified;
2589 * gtk_text_buffer_set_modified:
2590 * @buffer: a #GtkTextBuffer
2591 * @setting: modification flag setting
2593 * Used to keep track of whether the buffer has been modified since the
2594 * last time it was saved. Whenever the buffer is saved to disk, call
2595 * gtk_text_buffer_set_modified (@buffer, FALSE). When the buffer is modified,
2596 * it will automatically toggled on the modified bit again. When the modified
2597 * bit flips, the buffer emits a "modified_changed" signal.
2601 gtk_text_buffer_set_modified (GtkTextBuffer *buffer,
2604 gboolean fixed_setting;
2606 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2608 fixed_setting = setting != FALSE;
2610 if (buffer->modified == fixed_setting)
2614 buffer->modified = fixed_setting;
2615 g_signal_emit (buffer, signals[MODIFIED_CHANGED], 0);
2621 * Assorted other stuff
2625 * gtk_text_buffer_get_line_count:
2626 * @buffer: a #GtkTextBuffer
2628 * Obtains the number of lines in the buffer. This value is cached, so
2629 * the function is very fast.
2631 * Return value: number of lines in the buffer
2634 gtk_text_buffer_get_line_count (GtkTextBuffer *buffer)
2636 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), 0);
2638 return _gtk_text_btree_line_count (get_btree (buffer));
2642 * gtk_text_buffer_get_char_count:
2643 * @buffer: a #GtkTextBuffer
2645 * Gets the number of characters in the buffer; note that characters
2646 * and bytes are not the same, you can't e.g. expect the contents of
2647 * the buffer in string form to be this many bytes long. The character
2648 * count is cached, so this function is very fast.
2650 * Return value: number of characters in the buffer
2653 gtk_text_buffer_get_char_count (GtkTextBuffer *buffer)
2655 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), 0);
2657 return _gtk_text_btree_char_count (get_btree (buffer));
2660 /* Called when we lose the primary selection.
2663 clipboard_clear_selection_cb (GtkClipboard *clipboard,
2666 /* Move selection_bound to the insertion point */
2668 GtkTextIter selection_bound;
2669 GtkTextBuffer *buffer = GTK_TEXT_BUFFER (data);
2671 gtk_text_buffer_get_iter_at_mark (buffer, &insert,
2672 gtk_text_buffer_get_mark (buffer, "insert"));
2673 gtk_text_buffer_get_iter_at_mark (buffer, &selection_bound,
2674 gtk_text_buffer_get_mark (buffer, "selection_bound"));
2676 if (!gtk_text_iter_equal (&insert, &selection_bound))
2677 gtk_text_buffer_move_mark (buffer,
2678 gtk_text_buffer_get_mark (buffer, "selection_bound"),
2682 /* Called when we have the primary selection and someone else wants our
2683 * data in order to paste it.
2686 clipboard_get_selection_cb (GtkClipboard *clipboard,
2687 GtkSelectionData *selection_data,
2691 GtkTextBuffer *buffer = GTK_TEXT_BUFFER (data);
2692 GtkTextIter start, end;
2694 if (gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
2696 if (selection_data->target ==
2697 gdk_atom_intern ("GTK_TEXT_BUFFER_CONTENTS", FALSE))
2699 /* Provide the address of the buffer; this will only be
2700 * used within-process
2702 gtk_selection_data_set (selection_data,
2703 gdk_atom_intern ("GTK_TEXT_BUFFER_CONTENTS", FALSE),
2712 str = gtk_text_iter_get_visible_text (&start, &end);
2713 gtk_selection_data_set_text (selection_data, str, -1);
2721 GtkClipboard *clipboard;
2722 GtkTextBuffer *buffer;
2726 remove_all_clipboard_contents_buffers (GtkTextBuffer *buffer)
2728 GSList *tmp_list = buffer->clipboard_contents_buffers;
2731 ContentsBuffer *contents_buffer = tmp_list->data;
2733 g_object_unref (contents_buffer->buffer);
2734 g_free (contents_buffer);
2736 tmp_list = tmp_list->next;
2739 g_slist_free (buffer->clipboard_contents_buffers);
2740 buffer->clipboard_contents_buffers = NULL;
2744 remove_clipboard_contents_buffer (GtkTextBuffer *buffer,
2745 GtkClipboard *clipboard)
2747 GSList *tmp_list = buffer->clipboard_contents_buffers;
2750 ContentsBuffer *contents_buffer = tmp_list->data;
2752 if (contents_buffer->clipboard == clipboard)
2754 buffer->clipboard_contents_buffers = g_slist_remove (buffer->clipboard_contents_buffers, contents_buffer);
2756 g_object_unref (contents_buffer->buffer);
2757 g_free (contents_buffer);
2762 tmp_list = tmp_list->next;
2766 static GtkTextBuffer *
2767 get_clipboard_contents_buffer (GtkTextBuffer *buffer,
2768 GtkClipboard *clipboard,
2771 ContentsBuffer *contents_buffer;
2774 tmp_list = buffer->clipboard_contents_buffers;
2777 contents_buffer = tmp_list->data;
2778 if (contents_buffer->clipboard == clipboard)
2779 return contents_buffer->buffer;
2784 contents_buffer = g_new (ContentsBuffer, 1);
2785 contents_buffer->clipboard = clipboard;
2786 contents_buffer->buffer = gtk_text_buffer_new (gtk_text_buffer_get_tag_table (buffer));
2788 g_object_set_data (G_OBJECT (contents_buffer->buffer), "gtk-text-buffer-clipboard",
2789 GUINT_TO_POINTER (1));
2791 buffer->clipboard_contents_buffers = g_slist_prepend (buffer->clipboard_contents_buffers, contents_buffer);
2794 return contents_buffer->buffer;
2800 /* Provide cut/copied data */
2802 clipboard_get_contents_cb (GtkClipboard *clipboard,
2803 GtkSelectionData *selection_data,
2807 GtkTextBuffer *buffer;
2808 GtkTextBuffer *contents;
2810 buffer = GTK_TEXT_BUFFER (data);
2811 contents = get_clipboard_contents_buffer (buffer, clipboard, FALSE);
2813 g_assert (contents); /* This should never be called unless we own the clipboard */
2815 if (selection_data->target ==
2816 gdk_atom_intern ("GTK_TEXT_BUFFER_CONTENTS", FALSE))
2818 /* Provide the address of the clipboard buffer; this will only
2819 * be used within-process. OK to supply a NULL value for contents.
2821 gtk_selection_data_set (selection_data,
2822 gdk_atom_intern ("GTK_TEXT_BUFFER_CONTENTS", FALSE),
2830 GtkTextIter start, end;
2832 gtk_text_buffer_get_bounds (contents, &start, &end);
2834 str = gtk_text_iter_get_visible_text (&start, &end);
2835 gtk_selection_data_set_text (selection_data, str, -1);
2841 clipboard_clear_contents_cb (GtkClipboard *clipboard,
2844 GtkTextBuffer *buffer = GTK_TEXT_BUFFER (data);
2846 remove_clipboard_contents_buffer (buffer, clipboard);
2850 get_paste_point (GtkTextBuffer *buffer,
2852 gboolean clear_afterward)
2854 GtkTextIter insert_point;
2855 GtkTextMark *paste_point_override;
2857 paste_point_override = gtk_text_buffer_get_mark (buffer,
2858 "gtk_paste_point_override");
2860 if (paste_point_override != NULL)
2862 gtk_text_buffer_get_iter_at_mark (buffer, &insert_point,
2863 paste_point_override);
2864 if (clear_afterward)
2865 gtk_text_buffer_delete_mark (buffer,
2866 gtk_text_buffer_get_mark (buffer,
2867 "gtk_paste_point_override"));
2871 gtk_text_buffer_get_iter_at_mark (buffer, &insert_point,
2872 gtk_text_buffer_get_mark (buffer,
2876 *iter = insert_point;
2880 pre_paste_prep (ClipboardRequest *request_data,
2881 GtkTextIter *insert_point)
2883 GtkTextBuffer *buffer = request_data->buffer;
2885 get_paste_point (buffer, insert_point, TRUE);
2887 /* If we're going to replace the selection, we insert before it to
2888 * avoid messing it up, then we delete the selection after inserting.
2890 if (request_data->replace_selection)
2892 GtkTextIter start, end;
2894 if (gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
2895 *insert_point = start;
2900 post_paste_cleanup (ClipboardRequest *request_data)
2902 if (request_data->replace_selection)
2904 GtkTextIter start, end;
2906 if (gtk_text_buffer_get_selection_bounds (request_data->buffer,
2909 if (request_data->interactive)
2910 gtk_text_buffer_delete_interactive (request_data->buffer,
2913 request_data->default_editable);
2915 gtk_text_buffer_delete (request_data->buffer, &start, &end);
2920 /* Called when we request a paste and receive the text data
2923 clipboard_text_received (GtkClipboard *clipboard,
2927 ClipboardRequest *request_data = data;
2928 GtkTextBuffer *buffer = request_data->buffer;
2932 GtkTextIter insert_point;
2934 pre_paste_prep (request_data, &insert_point);
2936 if (request_data->interactive)
2937 gtk_text_buffer_insert_interactive (buffer, &insert_point,
2938 str, -1, request_data->default_editable);
2940 gtk_text_buffer_insert (buffer, &insert_point,
2943 post_paste_cleanup (request_data);
2946 g_object_unref (buffer);
2947 g_free (request_data);
2950 static GtkTextBuffer*
2951 selection_data_get_buffer (GtkSelectionData *selection_data,
2952 ClipboardRequest *request_data)
2955 GtkTextBuffer *src_buffer = NULL;
2957 /* If we can get the owner, the selection is in-process */
2958 owner = gdk_selection_owner_get_for_display (selection_data->display,
2959 selection_data->selection);
2964 if (gdk_window_get_window_type (owner) == GDK_WINDOW_FOREIGN)
2967 if (selection_data->type != gdk_atom_intern ("GTK_TEXT_BUFFER_CONTENTS", FALSE))
2970 if (selection_data->length != sizeof (src_buffer))
2973 memcpy (&src_buffer, selection_data->data, sizeof (src_buffer));
2975 if (src_buffer == NULL)
2978 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (src_buffer), NULL);
2980 if (gtk_text_buffer_get_tag_table (src_buffer) !=
2981 gtk_text_buffer_get_tag_table (request_data->buffer))
2988 /* These are pretty handy functions; maybe something like them
2989 * should be in the public API. Also, there are other places in this
2990 * file where they could be used.
2993 save_iter (const GtkTextIter *iter,
2994 gboolean left_gravity)
2996 return gtk_text_buffer_create_mark (gtk_text_iter_get_buffer (iter),
3003 restore_iter (const GtkTextIter *iter,
3006 gtk_text_buffer_get_iter_at_mark (gtk_text_mark_get_buffer (save_id),
3007 (GtkTextIter*) iter,
3009 gtk_text_buffer_delete_mark (gtk_text_mark_get_buffer (save_id),
3015 paste_from_buffer (ClipboardRequest *request_data,
3016 GtkTextBuffer *src_buffer,
3017 const GtkTextIter *start,
3018 const GtkTextIter *end)
3020 GtkTextIter insert_point;
3022 /* We're about to emit a bunch of signals, so be safe */
3023 g_object_ref (src_buffer);
3025 pre_paste_prep (request_data, &insert_point);
3027 if (!gtk_text_iter_equal (start, end))
3029 if (!request_data->interactive ||
3030 (gtk_text_iter_can_insert (&insert_point,
3031 request_data->default_editable)))
3032 gtk_text_buffer_real_insert_range (request_data->buffer,
3036 request_data->interactive);
3039 post_paste_cleanup (request_data);
3041 g_object_unref (src_buffer);
3042 g_free (request_data);
3046 clipboard_clipboard_buffer_received (GtkClipboard *clipboard,
3047 GtkSelectionData *selection_data,
3050 ClipboardRequest *request_data = data;
3051 GtkTextBuffer *src_buffer;
3053 src_buffer = selection_data_get_buffer (selection_data, request_data);
3057 GtkTextIter start, end;
3059 if (g_object_get_data (G_OBJECT (src_buffer), "gtk-text-buffer-clipboard"))
3061 gtk_text_buffer_get_bounds (src_buffer, &start, &end);
3063 paste_from_buffer (request_data, src_buffer,
3068 if (gtk_text_buffer_get_selection_bounds (src_buffer, &start, &end))
3069 paste_from_buffer (request_data, src_buffer,
3075 /* Request the text selection instead */
3076 gtk_clipboard_request_text (clipboard,
3077 clipboard_text_received,
3082 static const GtkTargetEntry targets[] = {
3083 { "STRING", 0, TARGET_STRING },
3084 { "TEXT", 0, TARGET_TEXT },
3085 { "COMPOUND_TEXT", 0, TARGET_COMPOUND_TEXT },
3086 { "UTF8_STRING", 0, TARGET_UTF8_STRING },
3087 { "GTK_TEXT_BUFFER_CONTENTS", 0, TARGET_TEXT_BUFFER_CONTENTS }
3092 GtkClipboard *clipboard;
3094 } SelectionClipboard;
3097 update_selection_clipboards (GtkTextBuffer *buffer)
3099 GSList *tmp_list = buffer->selection_clipboards;
3105 SelectionClipboard *selection_clipboard = tmp_list->data;
3106 GtkClipboard *clipboard = selection_clipboard->clipboard;
3108 /* Determine whether we have a selection and adjust X selection
3111 if (!gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
3113 if (gtk_clipboard_get_owner (clipboard) == G_OBJECT (buffer))
3114 gtk_clipboard_clear (clipboard);
3118 /* Even if we already have the selection, we need to update our
3121 if (!gtk_clipboard_set_with_owner (clipboard, targets, G_N_ELEMENTS (targets),
3122 clipboard_get_selection_cb,
3123 clipboard_clear_selection_cb,
3125 clipboard_clear_selection_cb (clipboard, buffer);
3128 tmp_list = tmp_list->next;
3132 static SelectionClipboard *
3133 find_selection_clipboard (GtkTextBuffer *buffer,
3134 GtkClipboard *clipboard)
3136 GSList *tmp_list = buffer->selection_clipboards;
3139 SelectionClipboard *selection_clipboard = tmp_list->data;
3140 if (selection_clipboard->clipboard == clipboard)
3141 return selection_clipboard;
3143 tmp_list = tmp_list->next;
3150 * gtk_text_buffer_add_selection_clipboard:
3151 * @buffer: a #GtkTextBuffer
3152 * @clipboard: a #GtkClipboard
3154 * Adds @clipboard to the list of clipboards in which the selection contents
3155 * of @buffer are available. In most cases, @clipboard will be the #GtkClipboard
3156 * of type %GDK_SELECTION_PRIMARY for a view of @buffer.
3159 gtk_text_buffer_add_selection_clipboard (GtkTextBuffer *buffer,
3160 GtkClipboard *clipboard)
3162 SelectionClipboard *selection_clipboard;
3164 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
3165 g_return_if_fail (clipboard != NULL);
3167 selection_clipboard = find_selection_clipboard (buffer, clipboard);
3168 if (selection_clipboard)
3170 selection_clipboard->ref_count++;
3174 selection_clipboard = g_new (SelectionClipboard, 1);
3176 selection_clipboard->clipboard = clipboard;
3177 selection_clipboard->ref_count = 1;
3179 buffer->selection_clipboards = g_slist_prepend (buffer->selection_clipboards, selection_clipboard);
3184 * gtk_text_buffer_remove_selection_clipboard:
3185 * @buffer: a #GtkTextBuffer
3186 * @clipboard: a #GtkClipboard added to @buffer by gtk_text_buffer_add_selection_clipboard().
3188 * Removes a #GtkClipboard added with gtk_text_buffer_add_selection_clipboard()
3191 gtk_text_buffer_remove_selection_clipboard (GtkTextBuffer *buffer,
3192 GtkClipboard *clipboard)
3194 SelectionClipboard *selection_clipboard;
3196 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
3197 g_return_if_fail (clipboard != NULL);
3199 selection_clipboard = find_selection_clipboard (buffer, clipboard);
3200 g_return_if_fail (selection_clipboard != NULL);
3202 selection_clipboard->ref_count--;
3203 if (selection_clipboard->ref_count == 0)
3205 if (gtk_clipboard_get_owner (selection_clipboard->clipboard) == G_OBJECT (buffer))
3206 gtk_clipboard_clear (selection_clipboard->clipboard);
3208 buffer->selection_clipboards = g_slist_remove (buffer->selection_clipboards,
3209 selection_clipboard);
3211 g_free (selection_clipboard);
3216 remove_all_selection_clipboards (GtkTextBuffer *buffer)
3218 GSList *tmp_list = buffer->selection_clipboards;
3221 SelectionClipboard *selection_clipboard = tmp_list->data;
3223 if (gtk_clipboard_get_owner (selection_clipboard->clipboard) == G_OBJECT (buffer))
3224 gtk_clipboard_clear (selection_clipboard->clipboard);
3226 g_free (selection_clipboard);
3228 tmp_list = tmp_list->next;
3231 g_slist_free (buffer->selection_clipboards);
3232 buffer->selection_clipboards = NULL;
3236 * gtk_text_buffer_paste_clipboard:
3237 * @buffer: a #GtkTextBuffer
3238 * @clipboard: the #GtkClipboard to paste from
3239 * @override_location: location to insert pasted text, or %NULL for at the cursor
3240 * @default_editable: whether the buffer is editable by default
3242 * Pastes the contents of a clipboard at the insertion point, or at @override_location.
3243 * (Note: pasting is asynchronous, that is, we'll ask for the paste data and
3244 * return, and at some point later after the main loop runs, the paste
3245 * data will be inserted.)
3249 gtk_text_buffer_paste_clipboard (GtkTextBuffer *buffer,
3250 GtkClipboard *clipboard,
3251 GtkTextIter *override_location,
3252 gboolean default_editable)
3254 ClipboardRequest *data = g_new (ClipboardRequest, 1);
3255 GtkTextIter paste_point;
3256 GtkTextIter start, end;
3258 if (override_location != NULL)
3259 gtk_text_buffer_create_mark (buffer,
3260 "gtk_paste_point_override",
3261 override_location, FALSE);
3263 data->buffer = buffer;
3264 g_object_ref (buffer);
3265 data->interactive = TRUE;
3266 data->default_editable = default_editable;
3268 /* When pasting with the cursor inside the selection area, you
3269 * replace the selection with the new text, otherwise, you
3270 * simply insert the new text at the point where the click
3271 * occured, unselecting any selected text. The replace_selection
3272 * flag toggles this behavior.
3274 data->replace_selection = FALSE;
3276 get_paste_point (buffer, &paste_point, FALSE);
3277 if (gtk_text_buffer_get_selection_bounds (buffer, &start, &end) &&
3278 (gtk_text_iter_in_range (&paste_point, &start, &end) ||
3279 gtk_text_iter_equal (&paste_point, &end)))
3280 data->replace_selection = TRUE;
3282 gtk_clipboard_request_contents (clipboard,
3283 gdk_atom_intern ("GTK_TEXT_BUFFER_CONTENTS", FALSE),
3284 clipboard_clipboard_buffer_received, data);
3288 * gtk_text_buffer_delete_selection:
3289 * @buffer: a #GtkTextBuffer
3290 * @interactive: whether the deletion is caused by user interaction
3291 * @default_editable: whether the buffer is editable by default
3293 * Deletes the range between the "insert" and "selection_bound" marks,
3294 * that is, the currently-selected text. If @interactive is %TRUE,
3295 * the editability of the selection will be considered (users can't delete
3298 * Return value: whether there was a non-empty selection to delete
3301 gtk_text_buffer_delete_selection (GtkTextBuffer *buffer,
3302 gboolean interactive,
3303 gboolean default_editable)
3308 if (!gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
3310 return FALSE; /* No selection */
3316 gtk_text_buffer_begin_user_action (buffer);
3317 gtk_text_buffer_delete_interactive (buffer, &start, &end, default_editable);
3318 gtk_text_buffer_end_user_action (buffer);
3321 gtk_text_buffer_delete (buffer, &start, &end);
3323 return TRUE; /* We deleted stuff */
3328 cut_or_copy (GtkTextBuffer *buffer,
3329 GtkClipboard *clipboard,
3330 gboolean delete_region_after,
3331 gboolean interactive,
3332 gboolean default_editable)
3334 /* We prefer to cut the selected region between selection_bound and
3335 * insertion point. If that region is empty, then we cut the region
3336 * between the "anchor" and the insertion point (this is for
3337 * C-space and M-w and other Emacs-style copy/yank keys). Note that
3338 * insert and selection_bound are guaranteed to exist, but the
3339 * anchor only exists sometimes.
3344 if (!gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
3346 /* Let's try the anchor thing */
3347 GtkTextMark * anchor = gtk_text_buffer_get_mark (buffer, "anchor");
3353 gtk_text_buffer_get_iter_at_mark (buffer, &end, anchor);
3354 gtk_text_iter_order (&start, &end);
3358 if (!gtk_text_iter_equal (&start, &end))
3361 GtkTextBuffer *contents;
3363 remove_clipboard_contents_buffer (buffer, clipboard);
3364 contents = get_clipboard_contents_buffer (buffer, clipboard, TRUE);
3366 gtk_text_buffer_get_iter_at_offset (contents, &ins, 0);
3368 gtk_text_buffer_insert_range (contents, &ins, &start, &end);
3370 if (!gtk_clipboard_set_with_owner (clipboard, targets, G_N_ELEMENTS (targets),
3371 clipboard_get_contents_cb,
3372 clipboard_clear_contents_cb,
3374 clipboard_clear_contents_cb (clipboard, buffer);
3376 if (delete_region_after)
3379 gtk_text_buffer_delete_interactive (buffer, &start, &end,
3382 gtk_text_buffer_delete (buffer, &start, &end);
3388 * gtk_text_buffer_cut_clipboard:
3389 * @buffer: a #GtkTextBuffer
3390 * @clipboard: the #GtkClipboard object to cut to.
3391 * @default_editable: default editability of the buffer
3393 * Copies the currently-selected text to a clipboard, then deletes
3394 * said text if it's editable.
3398 gtk_text_buffer_cut_clipboard (GtkTextBuffer *buffer,
3399 GtkClipboard *clipboard,
3400 gboolean default_editable)
3402 gtk_text_buffer_begin_user_action (buffer);
3403 cut_or_copy (buffer, clipboard, TRUE, TRUE, default_editable);
3404 gtk_text_buffer_end_user_action (buffer);
3408 * gtk_text_buffer_copy_clipboard:
3409 * @buffer: a #GtkTextBuffer
3410 * @clipboard: the #GtkClipboard object to copy to.
3412 * Copies the currently-selected text to a clipboard.
3416 gtk_text_buffer_copy_clipboard (GtkTextBuffer *buffer,
3417 GtkClipboard *clipboard)
3419 gtk_text_buffer_begin_user_action (buffer);
3420 cut_or_copy (buffer, clipboard, FALSE, TRUE, TRUE);
3421 gtk_text_buffer_end_user_action (buffer);
3426 * gtk_text_buffer_get_selection_bounds:
3427 * @buffer: a #GtkTextBuffer a #GtkTextBuffer
3428 * @start: iterator to initialize with selection start
3429 * @end: iterator to initialize with selection end
3431 * Returns %TRUE if some text is selected; places the bounds
3432 * of the selection in @start and @end (if the selection has length 0,
3433 * then @start and @end are filled in with the same value).
3434 * @start and @end will be in ascending order. If @start and @end are
3435 * NULL, then they are not filled in, but the return value still indicates
3436 * whether text is selected.
3438 * Return value: whether the selection has nonzero length
3441 gtk_text_buffer_get_selection_bounds (GtkTextBuffer *buffer,
3445 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
3447 return _gtk_text_btree_get_selection_bounds (get_btree (buffer), start, end);
3451 * gtk_text_buffer_begin_user_action:
3452 * @buffer: a #GtkTextBuffer
3454 * Called to indicate that the buffer operations between here and a
3455 * call to gtk_text_buffer_end_user_action() are part of a single
3456 * user-visible operation. The operations between
3457 * gtk_text_buffer_begin_user_action() and
3458 * gtk_text_buffer_end_user_action() can then be grouped when creating
3459 * an undo stack. #GtkTextBuffer maintains a count of calls to
3460 * gtk_text_buffer_begin_user_action() that have not been closed with
3461 * a call to gtk_text_buffer_end_user_action(), and emits the "begin_user_action"
3462 * and "end_user_action" signals only for the outermost pair of calls.
3463 * This allows you to build user actions from other user actions.
3465 * The "interactive" buffer mutation functions, such as
3466 * gtk_text_buffer_insert_interactive(), automatically call begin/end
3467 * user action around the buffer operations they perform, so there's
3468 * no need to add extra calls if you user action consists solely of a
3469 * single call to one of those functions.
3472 gtk_text_buffer_begin_user_action (GtkTextBuffer *buffer)
3474 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
3476 buffer->user_action_count += 1;
3478 if (buffer->user_action_count == 1)
3480 /* Outermost nested user action begin emits the signal */
3481 g_signal_emit (buffer, signals[BEGIN_USER_ACTION], 0);
3486 * gtk_text_buffer_end_user_action:
3487 * @buffer: a #GtkTextBuffer
3489 * Should be paired with a call to gtk_text_buffer_begin_user_action().
3490 * See that function for a full explanation.
3493 gtk_text_buffer_end_user_action (GtkTextBuffer *buffer)
3495 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
3496 g_return_if_fail (buffer->user_action_count > 0);
3498 buffer->user_action_count -= 1;
3500 if (buffer->user_action_count == 0)
3502 /* Ended the outermost-nested user action end, so emit the signal */
3503 g_signal_emit (buffer, signals[END_USER_ACTION], 0);
3508 * Logical attribute cache
3511 #define ATTR_CACHE_SIZE 2
3513 typedef struct _CacheEntry CacheEntry;
3518 PangoLogAttr *attrs;
3522 struct _GtkTextLogAttrCache
3524 gint chars_changed_stamp;
3525 CacheEntry entries[ATTR_CACHE_SIZE];
3529 free_log_attr_cache (GtkTextLogAttrCache *cache)
3532 while (i < ATTR_CACHE_SIZE)
3534 g_free (cache->entries[i].attrs);
3541 clear_log_attr_cache (GtkTextLogAttrCache *cache)
3544 while (i < ATTR_CACHE_SIZE)
3546 g_free (cache->entries[i].attrs);
3547 cache->entries[i].attrs = NULL;
3552 static PangoLogAttr*
3553 compute_log_attrs (const GtkTextIter *iter,
3559 gint char_len, byte_len;
3560 PangoLogAttr *attrs = NULL;
3565 gtk_text_iter_set_line_offset (&start, 0);
3566 gtk_text_iter_forward_line (&end);
3568 paragraph = gtk_text_iter_get_slice (&start, &end);
3569 char_len = g_utf8_strlen (paragraph, -1);
3570 byte_len = strlen (paragraph);
3572 g_assert (char_len > 0);
3575 *char_lenp = char_len;
3577 attrs = g_new (PangoLogAttr, char_len + 1);
3579 /* FIXME we need to follow PangoLayout and allow different language
3580 * tags within the paragraph
3582 pango_get_log_attrs (paragraph, byte_len, -1,
3583 gtk_text_iter_get_language (&start),
3592 /* The return value from this is valid until you call this a second time.
3595 _gtk_text_buffer_get_line_log_attrs (GtkTextBuffer *buffer,
3596 const GtkTextIter *anywhere_in_line,
3600 GtkTextLogAttrCache *cache;
3603 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
3604 g_return_val_if_fail (anywhere_in_line != NULL, NULL);
3606 /* special-case for empty last line in buffer */
3607 if (gtk_text_iter_is_end (anywhere_in_line) &&
3608 gtk_text_iter_get_line_offset (anywhere_in_line) == 0)
3615 /* FIXME we also need to recompute log attrs if the language tag at
3616 * the start of a paragraph changes
3619 if (buffer->log_attr_cache == NULL)
3621 buffer->log_attr_cache = g_new0 (GtkTextLogAttrCache, 1);
3622 buffer->log_attr_cache->chars_changed_stamp =
3623 _gtk_text_btree_get_chars_changed_stamp (get_btree (buffer));
3625 else if (buffer->log_attr_cache->chars_changed_stamp !=
3626 _gtk_text_btree_get_chars_changed_stamp (get_btree (buffer)))
3628 clear_log_attr_cache (buffer->log_attr_cache);
3631 cache = buffer->log_attr_cache;
3632 line = gtk_text_iter_get_line (anywhere_in_line);
3635 while (i < ATTR_CACHE_SIZE)
3637 if (cache->entries[i].attrs &&
3638 cache->entries[i].line == line)
3641 *char_len = cache->entries[i].char_len;
3642 return cache->entries[i].attrs;
3647 /* Not in cache; open up the first cache entry */
3648 if (cache->entries[ATTR_CACHE_SIZE-1].attrs)
3649 g_free (cache->entries[ATTR_CACHE_SIZE-1].attrs);
3651 g_memmove (cache->entries + 1, cache->entries,
3652 sizeof (CacheEntry) * (ATTR_CACHE_SIZE - 1));
3654 cache->entries[0].line = line;
3655 cache->entries[0].attrs = compute_log_attrs (anywhere_in_line,
3656 &cache->entries[0].char_len);
3659 *char_len = cache->entries[0].char_len;
3661 return cache->entries[0].attrs;
3665 _gtk_text_buffer_notify_will_remove_tag (GtkTextBuffer *buffer,
3668 /* This removes tag from the buffer, but DOESN'T emit the
3669 * remove_tag signal, because we can't afford to have user
3670 * code messing things up at this point; the tag MUST be removed
3674 _gtk_text_btree_notify_will_remove_tag (buffer->btree, tag);
3682 _gtk_text_buffer_spew (GtkTextBuffer *buffer)
3684 _gtk_text_btree_spew (get_btree (buffer));