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 "gtksignal.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;
77 TARGET_TEXT_BUFFER_CONTENTS
80 static void gtk_text_buffer_init (GtkTextBuffer *tkxt_buffer);
81 static void gtk_text_buffer_class_init (GtkTextBufferClass *klass);
82 static void gtk_text_buffer_finalize (GObject *object);
85 static void gtk_text_buffer_real_insert_text (GtkTextBuffer *buffer,
89 static void gtk_text_buffer_real_insert_pixbuf (GtkTextBuffer *buffer,
92 static void gtk_text_buffer_real_insert_anchor (GtkTextBuffer *buffer,
94 GtkTextChildAnchor *anchor);
95 static void gtk_text_buffer_real_delete_range (GtkTextBuffer *buffer,
98 static void gtk_text_buffer_real_apply_tag (GtkTextBuffer *buffer,
100 const GtkTextIter *start_char,
101 const GtkTextIter *end_char);
102 static void gtk_text_buffer_real_remove_tag (GtkTextBuffer *buffer,
104 const GtkTextIter *start_char,
105 const GtkTextIter *end_char);
106 static void gtk_text_buffer_real_changed (GtkTextBuffer *buffer);
108 static GtkTextBTree* get_btree (GtkTextBuffer *buffer);
109 static void free_log_attr_cache (GtkTextLogAttrCache *cache);
111 static void remove_all_clipboard_contents_buffers (GtkTextBuffer *buffer);
112 static void remove_all_selection_clipboards (GtkTextBuffer *buffer);
113 static void update_selection_clipboards (GtkTextBuffer *buffer);
115 static GtkObjectClass *parent_class = NULL;
116 static guint signals[LAST_SIGNAL] = { 0 };
119 gtk_text_buffer_get_type (void)
121 static GType our_type = 0;
125 static const GTypeInfo our_info =
127 sizeof (GtkTextBufferClass),
128 (GBaseInitFunc) NULL,
129 (GBaseFinalizeFunc) NULL,
130 (GClassInitFunc) gtk_text_buffer_class_init,
131 NULL, /* class_finalize */
132 NULL, /* class_data */
133 sizeof (GtkTextBuffer),
135 (GInstanceInitFunc) gtk_text_buffer_init
138 our_type = g_type_register_static (G_TYPE_OBJECT,
148 gtk_text_buffer_class_init (GtkTextBufferClass *klass)
150 GObjectClass *object_class = G_OBJECT_CLASS (klass);
152 parent_class = g_type_class_peek_parent (klass);
154 object_class->finalize = gtk_text_buffer_finalize;
156 klass->insert_text = gtk_text_buffer_real_insert_text;
157 klass->insert_pixbuf = gtk_text_buffer_real_insert_pixbuf;
158 klass->insert_child_anchor = gtk_text_buffer_real_insert_anchor;
159 klass->delete_range = gtk_text_buffer_real_delete_range;
160 klass->apply_tag = gtk_text_buffer_real_apply_tag;
161 klass->remove_tag = gtk_text_buffer_real_remove_tag;
162 klass->changed = gtk_text_buffer_real_changed;
164 signals[INSERT_TEXT] =
165 g_signal_new ("insert_text",
166 G_OBJECT_CLASS_TYPE (object_class),
168 G_STRUCT_OFFSET (GtkTextBufferClass, insert_text),
170 gtk_marshal_VOID__BOXED_STRING_INT,
173 GTK_TYPE_TEXT_ITER | G_SIGNAL_TYPE_STATIC_SCOPE,
174 GTK_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE,
177 signals[INSERT_PIXBUF] =
178 g_signal_new ("insert_pixbuf",
179 G_OBJECT_CLASS_TYPE (object_class),
181 G_STRUCT_OFFSET (GtkTextBufferClass, insert_pixbuf),
183 gtk_marshal_VOID__BOXED_OBJECT,
186 GTK_TYPE_TEXT_ITER | G_SIGNAL_TYPE_STATIC_SCOPE,
189 signals[INSERT_CHILD_ANCHOR] =
190 g_signal_new ("insert_child_anchor",
191 G_OBJECT_CLASS_TYPE (object_class),
193 G_STRUCT_OFFSET (GtkTextBufferClass, insert_child_anchor),
195 gtk_marshal_VOID__BOXED_OBJECT,
198 GTK_TYPE_TEXT_ITER | G_SIGNAL_TYPE_STATIC_SCOPE,
199 GTK_TYPE_TEXT_CHILD_ANCHOR);
201 signals[DELETE_RANGE] =
202 g_signal_new ("delete_range",
203 G_OBJECT_CLASS_TYPE (object_class),
205 G_STRUCT_OFFSET (GtkTextBufferClass, delete_range),
207 gtk_marshal_VOID__BOXED_BOXED,
210 GTK_TYPE_TEXT_ITER | G_SIGNAL_TYPE_STATIC_SCOPE,
211 GTK_TYPE_TEXT_ITER | G_SIGNAL_TYPE_STATIC_SCOPE);
214 g_signal_new ("changed",
215 G_OBJECT_CLASS_TYPE (object_class),
217 G_STRUCT_OFFSET (GtkTextBufferClass, changed),
219 gtk_marshal_VOID__VOID,
223 signals[MODIFIED_CHANGED] =
224 g_signal_new ("modified_changed",
225 G_OBJECT_CLASS_TYPE (object_class),
227 G_STRUCT_OFFSET (GtkTextBufferClass, modified_changed),
229 gtk_marshal_VOID__VOID,
234 g_signal_new ("mark_set",
235 G_OBJECT_CLASS_TYPE (object_class),
237 G_STRUCT_OFFSET (GtkTextBufferClass, mark_set),
239 gtk_marshal_VOID__BOXED_OBJECT,
245 signals[MARK_DELETED] =
246 g_signal_new ("mark_deleted",
247 G_OBJECT_CLASS_TYPE (object_class),
249 G_STRUCT_OFFSET (GtkTextBufferClass, mark_deleted),
251 gtk_marshal_VOID__OBJECT,
257 g_signal_new ("apply_tag",
258 G_OBJECT_CLASS_TYPE (object_class),
260 G_STRUCT_OFFSET (GtkTextBufferClass, apply_tag),
262 gtk_marshal_VOID__OBJECT_BOXED_BOXED,
269 signals[REMOVE_TAG] =
270 g_signal_new ("remove_tag",
271 G_OBJECT_CLASS_TYPE (object_class),
273 G_STRUCT_OFFSET (GtkTextBufferClass, remove_tag),
275 gtk_marshal_VOID__OBJECT_BOXED_BOXED,
282 signals[BEGIN_USER_ACTION] =
283 g_signal_new ("begin_user_action",
284 G_OBJECT_CLASS_TYPE (object_class),
286 G_STRUCT_OFFSET (GtkTextBufferClass, begin_user_action),
288 gtk_marshal_VOID__VOID,
292 signals[END_USER_ACTION] =
293 g_signal_new ("end_user_action",
294 G_OBJECT_CLASS_TYPE (object_class),
296 G_STRUCT_OFFSET (GtkTextBufferClass, end_user_action),
298 gtk_marshal_VOID__VOID,
304 gtk_text_buffer_init (GtkTextBuffer *buffer)
306 buffer->clipboard_contents_buffers = NULL;
310 * gtk_text_buffer_new:
311 * @table: a tag table, or NULL to create a new one
313 * Creates a new text buffer.
315 * Return value: a new text buffer
318 gtk_text_buffer_new (GtkTextTagTable *table)
320 GtkTextBuffer *text_buffer;
322 text_buffer = GTK_TEXT_BUFFER (g_object_new (gtk_text_buffer_get_type (), NULL));
326 text_buffer->tag_table = table;
328 g_object_ref (G_OBJECT (text_buffer->tag_table));
335 gtk_text_buffer_finalize (GObject *object)
337 GtkTextBuffer *buffer;
339 buffer = GTK_TEXT_BUFFER (object);
341 remove_all_clipboard_contents_buffers (buffer);
342 remove_all_selection_clipboards (buffer);
344 if (buffer->tag_table)
346 g_object_unref (G_OBJECT (buffer->tag_table));
347 buffer->tag_table = NULL;
352 _gtk_text_btree_unref (buffer->btree);
353 buffer->btree = NULL;
356 if (buffer->log_attr_cache)
357 free_log_attr_cache (buffer->log_attr_cache);
359 buffer->log_attr_cache = NULL;
361 G_OBJECT_CLASS (parent_class)->finalize (object);
364 static GtkTextTagTable*
365 get_table (GtkTextBuffer *buffer)
367 if (buffer->tag_table == NULL)
368 buffer->tag_table = gtk_text_tag_table_new ();
370 return buffer->tag_table;
374 get_btree (GtkTextBuffer *buffer)
376 if (buffer->btree == NULL)
377 buffer->btree = _gtk_text_btree_new (gtk_text_buffer_get_tag_table (buffer),
380 return buffer->btree;
384 _gtk_text_buffer_get_btree (GtkTextBuffer *buffer)
386 return get_btree (buffer);
390 * gtk_text_buffer_get_tag_table:
391 * @buffer: a #GtkTextBuffer
393 * Get the #GtkTextTagTable associated with this buffer.
395 * Return value: the buffer's tag table
398 gtk_text_buffer_get_tag_table (GtkTextBuffer *buffer)
400 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
402 return get_table (buffer);
406 * gtk_text_buffer_set_text:
407 * @buffer: a #GtkTextBuffer
408 * @text: UTF-8 text to insert
409 * @len: length of @text in bytes
411 * Deletes current contents of @buffer, and inserts @text instead. If
412 * @len is -1, @text must be nul-terminated. @text must be valid UTF-8.
415 gtk_text_buffer_set_text (GtkTextBuffer *buffer,
419 GtkTextIter start, end;
421 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
422 g_return_if_fail (text != NULL);
427 gtk_text_buffer_get_bounds (buffer, &start, &end);
429 gtk_text_buffer_delete (buffer, &start, &end);
433 gtk_text_buffer_get_iter_at_offset (buffer, &start, 0);
434 gtk_text_buffer_insert (buffer, &start, text, len);
443 gtk_text_buffer_real_insert_text (GtkTextBuffer *buffer,
448 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
449 g_return_if_fail (iter != NULL);
451 _gtk_text_btree_insert (iter, text, len);
453 g_signal_emit (G_OBJECT (buffer), signals[CHANGED], 0);
457 gtk_text_buffer_emit_insert (GtkTextBuffer *buffer,
462 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
463 g_return_if_fail (iter != NULL);
464 g_return_if_fail (text != NULL);
469 g_return_if_fail (g_utf8_validate (text, len, NULL));
473 g_signal_emit (G_OBJECT (buffer), signals[INSERT_TEXT], 0,
479 * gtk_text_buffer_insert:
480 * @buffer: a #GtkTextBuffer
481 * @iter: a position in the buffer
482 * @text: UTF-8 format text to insert
483 * @len: length of text in bytes, or -1
485 * Inserts @len bytes of @text at position @iter. If @len is -1,
486 * @text must be nul-terminated and will be inserted in its
487 * entirety. Emits the "insert_text" signal; insertion actually occurs
488 * in the default handler for the signal. @iter is invalidated when
489 * insertion occurs (because the buffer contents change), but the
490 * default signal handler revalidates it to point to the end of the
495 gtk_text_buffer_insert (GtkTextBuffer *buffer,
500 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
501 g_return_if_fail (iter != NULL);
502 g_return_if_fail (text != NULL);
503 g_return_if_fail (gtk_text_iter_get_buffer (iter) == buffer);
505 gtk_text_buffer_emit_insert (buffer, iter, text, len);
509 * gtk_text_buffer_insert_at_cursor:
510 * @buffer: a #GtkTextBuffer
511 * @text: some text in UTF-8 format
512 * @len: length of text, in bytes
514 * Simply calls gtk_text_buffer_insert (), using the current
515 * cursor position as the insertion point.
518 gtk_text_buffer_insert_at_cursor (GtkTextBuffer *buffer,
524 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
525 g_return_if_fail (text != NULL);
527 gtk_text_buffer_get_iter_at_mark (buffer, &iter,
528 gtk_text_buffer_get_mark (buffer,
531 gtk_text_buffer_insert (buffer, &iter, text, len);
535 * gtk_text_buffer_insert_interactive:
536 * @buffer: a #GtkTextBuffer
537 * @iter: a position in @buffer
538 * @text: some UTF-8 text
539 * @len: length of text in bytes, or -1
540 * @default_editable: default editability of buffer
542 * Like gtk_text_buffer_insert (), but the insertion will not occur if
543 * @iter is at a non-editable location in the buffer. Usually you
544 * want to prevent insertions at ineditable locations if the insertion
545 * results from a user action (is interactive).
547 * @default_editable indicates the editability of text that doesn't
548 * have a tag affecting editability applied to it. Typically the
549 * result of gtk_text_view_get_editable() is appropriate here.
551 * Return value: whether text was actually inserted
554 gtk_text_buffer_insert_interactive (GtkTextBuffer *buffer,
558 gboolean default_editable)
560 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
561 g_return_val_if_fail (text != NULL, FALSE);
562 g_return_val_if_fail (gtk_text_iter_get_buffer (iter) == buffer, FALSE);
564 if (gtk_text_iter_can_insert (iter, default_editable))
566 gtk_text_buffer_begin_user_action (buffer);
567 gtk_text_buffer_emit_insert (buffer, iter, text, len);
568 gtk_text_buffer_end_user_action (buffer);
576 * gtk_text_buffer_insert_interactive_at_cursor:
577 * @buffer: a #GtkTextBuffer
578 * @text: text in UTF-8 format
579 * @len: length of text in bytes, or -1
580 * @default_editable: default editability of buffer
582 * Calls gtk_text_buffer_insert_interactive () at the cursor
585 * @default_editable indicates the editability of text that doesn't
586 * have a tag affecting editability applied to it. Typically the
587 * result of gtk_text_view_get_editable() is appropriate here.
589 * Return value: whether text was actually inserted
592 gtk_text_buffer_insert_interactive_at_cursor (GtkTextBuffer *buffer,
595 gboolean default_editable)
599 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
600 g_return_val_if_fail (text != NULL, FALSE);
602 gtk_text_buffer_get_iter_at_mark (buffer, &iter,
603 gtk_text_buffer_get_mark (buffer,
606 return gtk_text_buffer_insert_interactive (buffer, &iter, text, len,
611 possibly_not_text (gunichar ch,
614 return ch == GTK_TEXT_UNKNOWN_CHAR;
618 insert_text_range (GtkTextBuffer *buffer,
620 const GtkTextIter *orig_start,
621 const GtkTextIter *orig_end,
622 gboolean interactive)
626 text = gtk_text_iter_get_text (orig_start, orig_end);
628 gtk_text_buffer_emit_insert (buffer, iter, text, -1);
633 typedef struct _Range Range;
636 GtkTextBuffer *buffer;
637 GtkTextMark *start_mark;
638 GtkTextMark *end_mark;
639 GtkTextMark *whole_end_mark;
640 GtkTextIter *range_start;
641 GtkTextIter *range_end;
642 GtkTextIter *whole_end;
646 save_range (GtkTextIter *range_start,
647 GtkTextIter *range_end,
648 GtkTextIter *whole_end)
652 r = g_new (Range, 1);
654 r->buffer = gtk_text_iter_get_buffer (range_start);
655 g_object_ref (G_OBJECT (r->buffer));
658 gtk_text_buffer_create_mark (gtk_text_iter_get_buffer (range_start),
663 gtk_text_buffer_create_mark (gtk_text_iter_get_buffer (range_start),
669 gtk_text_buffer_create_mark (gtk_text_iter_get_buffer (range_start),
674 r->range_start = range_start;
675 r->range_end = range_end;
676 r->whole_end = whole_end;
682 restore_range (Range *r)
684 gtk_text_buffer_get_iter_at_mark (r->buffer,
688 gtk_text_buffer_get_iter_at_mark (r->buffer,
692 gtk_text_buffer_get_iter_at_mark (r->buffer,
696 gtk_text_buffer_delete_mark (r->buffer, r->start_mark);
697 gtk_text_buffer_delete_mark (r->buffer, r->end_mark);
698 gtk_text_buffer_delete_mark (r->buffer, r->whole_end_mark);
700 g_object_unref (G_OBJECT (r->buffer));
705 insert_range_untagged (GtkTextBuffer *buffer,
707 const GtkTextIter *orig_start,
708 const GtkTextIter *orig_end,
709 gboolean interactive)
711 GtkTextIter range_start;
712 GtkTextIter range_end;
713 GtkTextIter start, end;
714 GtkTextBuffer *src_buffer;
717 if (gtk_text_iter_equal (orig_start, orig_end))
723 src_buffer = gtk_text_iter_get_buffer (&start);
730 if (gtk_text_iter_equal (&range_start, &range_end))
732 /* Figure out how to move forward */
734 g_assert (gtk_text_iter_compare (&range_end, &end) <= 0);
736 if (gtk_text_iter_equal (&range_end, &end))
738 /* nothing left to do */
741 else if (gtk_text_iter_get_char (&range_end) == GTK_TEXT_UNKNOWN_CHAR)
743 GdkPixbuf *pixbuf = NULL;
744 GtkTextChildAnchor *anchor = NULL;
745 pixbuf = gtk_text_iter_get_pixbuf (&range_end);
746 anchor = gtk_text_iter_get_child_anchor (&range_end);
750 r = save_range (&range_start,
754 gtk_text_buffer_insert_pixbuf (buffer,
761 gtk_text_iter_forward_char (&range_end);
763 range_start = range_end;
767 /* Just skip anchors */
769 gtk_text_iter_forward_char (&range_end);
770 range_start = range_end;
774 /* The GTK_TEXT_UNKNOWN_CHAR was in a text segment, so
777 gtk_text_iter_forward_find_char (&range_end,
778 possibly_not_text, NULL,
781 g_assert (gtk_text_iter_compare (&range_end, &end) <= 0);
786 /* Text segment starts here, so forward search to
787 * find its possible endpoint
789 gtk_text_iter_forward_find_char (&range_end,
790 possibly_not_text, NULL,
793 g_assert (gtk_text_iter_compare (&range_end, &end) <= 0);
798 r = save_range (&range_start,
802 insert_text_range (buffer,
811 range_start = range_end;
817 gtk_text_buffer_real_insert_range (GtkTextBuffer *buffer,
819 const GtkTextIter *orig_start,
820 const GtkTextIter *orig_end,
821 gboolean interactive)
823 /* Find each range of uniformly-tagged text, insert it,
824 * then apply the tags.
826 GtkTextIter start = *orig_start;
827 GtkTextIter end = *orig_end;
828 GtkTextIter range_start;
829 GtkTextIter range_end;
830 GtkTextBuffer *src_buffer;
833 if (gtk_text_iter_equal (orig_start, orig_end))
837 gtk_text_buffer_begin_user_action (buffer);
839 src_buffer = gtk_text_iter_get_buffer (orig_start);
841 gtk_text_iter_order (&start, &end);
849 GtkTextIter start_iter;
853 if (gtk_text_iter_equal (&range_start, &end))
854 break; /* All done */
856 g_assert (gtk_text_iter_compare (&range_start, &end) < 0);
858 gtk_text_iter_forward_to_tag_toggle (&range_end, NULL);
860 g_assert (!gtk_text_iter_equal (&range_start, &range_end));
862 /* Clamp to the end iterator */
863 if (gtk_text_iter_compare (&range_end, &end) > 0)
866 /* We have a range with unique tags; insert it, and
869 start_offset = gtk_text_iter_get_offset (iter);
871 r = save_range (&range_start, &range_end, &end);
873 insert_range_untagged (buffer, iter, &range_start, &range_end, interactive);
878 gtk_text_buffer_get_iter_at_offset (buffer, &start_iter, start_offset);
880 tags = gtk_text_iter_get_tags (&range_start);
882 while (tmp_list != NULL)
884 gtk_text_buffer_apply_tag (buffer,
889 tmp_list = g_slist_next (tmp_list);
893 range_start = range_end;
897 gtk_text_buffer_end_user_action (buffer);
901 * gtk_text_buffer_insert_range:
902 * @buffer: a #GtkTextBuffer
903 * @iter: a position in @buffer
904 * @start: a position in a #GtkTextBuffer
905 * @end: another position in the same buffer as @start
907 * Copies text, tags, and pixbufs between @start and @end (the order
908 * of @start and @end doesn't matter) and inserts the copy at @iter.
909 * Used instead of simply getting/inserting text because it preserves
910 * images and tags. If @start and @end are in a different buffer from
911 * @buffer, the two buffers must share the same tag table.
913 * Implemented via emissions of the insert_text and apply_tag signals,
917 gtk_text_buffer_insert_range (GtkTextBuffer *buffer,
919 const GtkTextIter *start,
920 const GtkTextIter *end)
922 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
923 g_return_if_fail (iter != NULL);
924 g_return_if_fail (start != NULL);
925 g_return_if_fail (end != NULL);
926 g_return_if_fail (gtk_text_iter_get_buffer (start) ==
927 gtk_text_iter_get_buffer (end));
928 g_return_if_fail (gtk_text_iter_get_buffer (start)->tag_table ==
930 g_return_if_fail (gtk_text_iter_get_buffer (iter) == buffer);
932 gtk_text_buffer_real_insert_range (buffer, iter, start, end, FALSE);
936 * gtk_text_buffer_insert_range_interactive:
937 * @buffer: a #GtkTextBuffer
938 * @iter: a position in @buffer
939 * @start: a position in a #GtkTextBuffer
940 * @end: another position in the same buffer as @start
941 * @default_editable: default editability of the buffer
943 * Same as gtk_text_buffer_insert_range(), but does nothing if the
944 * insertion point isn't editable. The @default_editable parameter
945 * indicates whether the text is editable at @iter if no tags
946 * enclosing @iter affect editability. Typically the result of
947 * gtk_text_view_get_editable() is appropriate here.
949 * Returns: whether an insertion was possible at @iter
952 gtk_text_buffer_insert_range_interactive (GtkTextBuffer *buffer,
954 const GtkTextIter *start,
955 const GtkTextIter *end,
956 gboolean default_editable)
958 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
959 g_return_val_if_fail (iter != NULL, FALSE);
960 g_return_val_if_fail (start != NULL, FALSE);
961 g_return_val_if_fail (end != NULL, FALSE);
962 g_return_val_if_fail (gtk_text_iter_get_buffer (start) ==
963 gtk_text_iter_get_buffer (end), FALSE);
964 g_return_val_if_fail (gtk_text_iter_get_buffer (start)->tag_table ==
965 buffer->tag_table, FALSE);
968 if (gtk_text_iter_can_insert (iter, default_editable))
970 gtk_text_buffer_real_insert_range (buffer, iter, start, end, TRUE);
978 * gtk_text_buffer_insert_with_tags:
979 * @buffer: a #GtkTextBuffer
980 * @iter: an iterator in @buffer
982 * @len: length of @text, or -1
983 * @first_tag: first tag to apply to @text
984 * @Varargs: NULL-terminated list of tags to apply
986 * Inserts @text into @buffer at @iter, applying the list of tags to
987 * the newly-inserted text. The last tag specified must be NULL to
988 * terminate the list. Equivalent to calling gtk_text_buffer_insert (),
989 * then gtk_text_buffer_apply_tag () on the inserted text;
990 * gtk_text_buffer_insert_with_tags () is just a convenience function.
993 gtk_text_buffer_insert_with_tags (GtkTextBuffer *buffer,
997 GtkTextTag *first_tag,
1005 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1006 g_return_if_fail (iter != NULL);
1007 g_return_if_fail (text != NULL);
1008 g_return_if_fail (gtk_text_iter_get_buffer (iter) == buffer);
1010 start_offset = gtk_text_iter_get_offset (iter);
1012 gtk_text_buffer_insert (buffer, iter, text, len);
1014 if (first_tag == NULL)
1017 gtk_text_buffer_get_iter_at_offset (buffer, &start, start_offset);
1019 va_start (args, first_tag);
1023 gtk_text_buffer_apply_tag (buffer, tag, &start, iter);
1025 tag = va_arg (args, GtkTextTag*);
1032 * gtk_text_buffer_insert_with_tags_by_name:
1033 * @buffer: a #GtkTextBuffer
1034 * @iter: position in @buffer
1036 * @len: length of @text, or -1
1037 * @first_tag_name: name of a tag to apply to @text
1038 * @Varargs: more tag names
1040 * Same as gtk_text_buffer_insert_with_tags (), but allows you
1041 * to pass in tag names instead of tag objects.
1044 gtk_text_buffer_insert_with_tags_by_name (GtkTextBuffer *buffer,
1048 const gchar *first_tag_name,
1054 const gchar *tag_name;
1056 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1057 g_return_if_fail (iter != NULL);
1058 g_return_if_fail (text != NULL);
1059 g_return_if_fail (gtk_text_iter_get_buffer (iter) == buffer);
1061 start_offset = gtk_text_iter_get_offset (iter);
1063 gtk_text_buffer_insert (buffer, iter, text, len);
1065 if (first_tag_name == NULL)
1068 gtk_text_buffer_get_iter_at_offset (buffer, &start, start_offset);
1070 va_start (args, first_tag_name);
1071 tag_name = first_tag_name;
1076 tag = gtk_text_tag_table_lookup (buffer->tag_table,
1081 g_warning ("%s: no tag with name '%s'!", G_STRLOC, tag_name);
1085 gtk_text_buffer_apply_tag (buffer, tag, &start, iter);
1087 tag_name = va_arg (args, const gchar*);
1099 gtk_text_buffer_real_delete_range (GtkTextBuffer *buffer,
1103 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1104 g_return_if_fail (start != NULL);
1105 g_return_if_fail (end != NULL);
1107 _gtk_text_btree_delete (start, end);
1109 /* may have deleted the selection... */
1110 update_selection_clipboards (buffer);
1112 g_signal_emit (G_OBJECT (buffer), signals[CHANGED], 0);
1116 gtk_text_buffer_emit_delete (GtkTextBuffer *buffer,
1120 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1121 g_return_if_fail (start != NULL);
1122 g_return_if_fail (end != NULL);
1124 if (gtk_text_iter_equal (start, end))
1127 gtk_text_iter_order (start, end);
1129 g_signal_emit (G_OBJECT (buffer),
1130 signals[DELETE_RANGE],
1136 * gtk_text_buffer_delete:
1137 * @buffer: a #GtkTextBuffer
1138 * @start: a position in @buffer
1139 * @end: another position in @buffer
1141 * Deletes text between @start and @end. The order of @start and @end
1142 * is not actually relevant; gtk_text_buffer_delete () will reorder
1143 * them. This function actually emits the "delete_range" signal, and
1144 * the default handler of that signal deletes the text. Because the
1145 * buffer is modified, all outstanding iterators become invalid after
1146 * calling this function; however, the @start and @end will be
1147 * re-initialized to point to the location where text was deleted.
1151 gtk_text_buffer_delete (GtkTextBuffer *buffer,
1155 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1156 g_return_if_fail (start != NULL);
1157 g_return_if_fail (end != NULL);
1158 g_return_if_fail (gtk_text_iter_get_buffer (start) == buffer);
1159 g_return_if_fail (gtk_text_iter_get_buffer (end) == buffer);
1161 gtk_text_buffer_emit_delete (buffer, start, end);
1165 * gtk_text_buffer_delete_interactive:
1166 * @buffer: a #GtkTextBuffer
1167 * @start_iter: start of range to delete
1168 * @end_iter: end of range
1169 * @default_editable: whether the buffer is editable by default
1171 * Deletes all <emphasis>editable</emphasis> text in the given range.
1172 * Calls gtk_text_buffer_delete () for each editable sub-range of
1173 * [@start,@end). @start and @end are revalidated to point to
1174 * the location of the last deleted range, or left untouched if
1175 * no text was deleted.
1177 * Return value: whether some text was actually deleted
1180 gtk_text_buffer_delete_interactive (GtkTextBuffer *buffer,
1181 GtkTextIter *start_iter,
1182 GtkTextIter *end_iter,
1183 gboolean default_editable)
1185 GtkTextMark *end_mark;
1186 GtkTextMark *start_mark;
1188 gboolean current_state;
1189 gboolean deleted_stuff = FALSE;
1191 /* Delete all editable text in the range start_iter, end_iter */
1193 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
1194 g_return_val_if_fail (start_iter != NULL, FALSE);
1195 g_return_val_if_fail (end_iter != NULL, FALSE);
1196 g_return_val_if_fail (gtk_text_iter_get_buffer (start_iter) == buffer, FALSE);
1197 g_return_val_if_fail (gtk_text_iter_get_buffer (end_iter) == buffer, FALSE);
1200 gtk_text_buffer_begin_user_action (buffer);
1202 gtk_text_iter_order (start_iter, end_iter);
1204 start_mark = gtk_text_buffer_create_mark (buffer, NULL,
1206 end_mark = gtk_text_buffer_create_mark (buffer, NULL,
1210 current_state = gtk_text_iter_editable (&iter, default_editable);
1215 gboolean done = FALSE;
1218 gtk_text_iter_forward_to_tag_toggle (&iter, NULL);
1220 gtk_text_buffer_get_iter_at_mark (buffer, &end, end_mark);
1222 if (gtk_text_iter_compare (&iter, &end) >= 0)
1225 iter = end; /* clamp to the last boundary */
1228 new_state = gtk_text_iter_editable (&iter, default_editable);
1230 if (current_state == new_state)
1236 /* We're ending an editable region. Delete said region. */
1239 gtk_text_buffer_get_iter_at_mark (buffer, &start, start_mark);
1241 gtk_text_buffer_emit_delete (buffer, &start, &iter);
1243 deleted_stuff = TRUE;
1245 /* revalidate user's iterators. */
1246 *start_iter = start;
1256 if (current_state && !new_state)
1258 /* End of an editable region. Delete it. */
1261 gtk_text_buffer_get_iter_at_mark (buffer, &start, start_mark);
1263 gtk_text_buffer_emit_delete (buffer, &start, &iter);
1265 current_state = FALSE;
1266 deleted_stuff = TRUE;
1268 /* revalidate user's iterators. */
1269 *start_iter = start;
1274 /* We are at the start of an editable region. We won't be deleting
1275 * the previous region. Move start mark to start of this region.
1278 g_assert (!current_state && new_state);
1280 gtk_text_buffer_move_mark (buffer, start_mark,
1284 current_state = TRUE;
1292 gtk_text_buffer_delete_mark (buffer, start_mark);
1293 gtk_text_buffer_delete_mark (buffer, end_mark);
1295 gtk_text_buffer_end_user_action (buffer);
1297 return deleted_stuff;
1301 * Extracting textual buffer contents
1305 * gtk_text_buffer_get_text:
1306 * @buffer: a #GtkTextBuffer
1307 * @start: start of a range
1308 * @end: end of a range
1309 * @include_hidden_chars: whether to include invisible text
1311 * Returns the text in the range [@start,@end). Excludes undisplayed
1312 * text (text marked with tags that set the invisibility attribute) if
1313 * @include_hidden_chars is FALSE. Does not include characters
1314 * representing embedded images, so byte and character indexes into
1315 * the returned string do <emphasis>not</emphasis> correspond to byte
1316 * and character indexes into the buffer. Contrast with
1317 * gtk_text_buffer_get_slice ().
1319 * Return value: an allocated UTF-8 string
1322 gtk_text_buffer_get_text (GtkTextBuffer *buffer,
1323 const GtkTextIter *start,
1324 const GtkTextIter *end,
1325 gboolean include_hidden_chars)
1327 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
1328 g_return_val_if_fail (start != NULL, NULL);
1329 g_return_val_if_fail (end != NULL, NULL);
1330 g_return_val_if_fail (gtk_text_iter_get_buffer (start) == buffer, NULL);
1331 g_return_val_if_fail (gtk_text_iter_get_buffer (end) == buffer, NULL);
1333 if (include_hidden_chars)
1334 return gtk_text_iter_get_text (start, end);
1336 return gtk_text_iter_get_visible_text (start, end);
1340 * gtk_text_buffer_get_slice:
1341 * @buffer: a #GtkTextBuffer
1342 * @start: start of a range
1343 * @end: end of a range
1344 * @include_hidden_chars: whether to include invisible text
1346 * Returns the text in the range [@start,@end). Excludes undisplayed
1347 * text (text marked with tags that set the invisibility attribute) if
1348 * @include_hidden_chars is FALSE. The returned string includes a
1349 * 0xFFFC character whenever the buffer contains
1350 * embedded images, so byte and character indexes into
1351 * the returned string <emphasis>do</emphasis> correspond to byte
1352 * and character indexes into the buffer. Contrast with
1353 * gtk_text_buffer_get_text (). Note that 0xFFFC can occur in normal
1354 * text as well, so it is not a reliable indicator that a pixbuf or
1355 * widget is in the buffer.
1357 * Return value: an allocated UTF-8 string
1360 gtk_text_buffer_get_slice (GtkTextBuffer *buffer,
1361 const GtkTextIter *start,
1362 const GtkTextIter *end,
1363 gboolean include_hidden_chars)
1365 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
1366 g_return_val_if_fail (start != NULL, NULL);
1367 g_return_val_if_fail (end != NULL, NULL);
1368 g_return_val_if_fail (gtk_text_iter_get_buffer (start) == buffer, NULL);
1369 g_return_val_if_fail (gtk_text_iter_get_buffer (end) == buffer, NULL);
1371 if (include_hidden_chars)
1372 return gtk_text_iter_get_slice (start, end);
1374 return gtk_text_iter_get_visible_slice (start, end);
1382 gtk_text_buffer_real_insert_pixbuf (GtkTextBuffer *buffer,
1386 _gtk_text_btree_insert_pixbuf (iter, pixbuf);
1388 g_signal_emit (G_OBJECT (buffer), signals[CHANGED], 0);
1392 * gtk_text_buffer_insert_pixbuf:
1393 * @buffer: a #GtkTextBuffer
1394 * @iter: location to insert the pixbuf
1395 * @pixbuf: a #GdkPixbuf
1397 * Inserts an image into the text buffer at @iter. The image will be
1398 * counted as one character in character counts, and when obtaining
1399 * the buffer contents as a string, will be represented by the Unicode
1400 * "object replacement character" 0xFFFC. Note that the "slice"
1401 * variants for obtaining portions of the buffer as a string include
1402 * this character for pixbufs, but the "text" variants do
1403 * not. e.g. see gtk_text_buffer_get_slice() and
1404 * gtk_text_buffer_get_text().
1408 gtk_text_buffer_insert_pixbuf (GtkTextBuffer *buffer,
1412 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1413 g_return_if_fail (iter != NULL);
1414 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
1415 g_return_if_fail (gtk_text_iter_get_buffer (iter) == buffer);
1417 g_signal_emit (G_OBJECT (buffer), signals[INSERT_PIXBUF], 0,
1427 gtk_text_buffer_real_insert_anchor (GtkTextBuffer *buffer,
1429 GtkTextChildAnchor *anchor)
1431 _gtk_text_btree_insert_child_anchor (iter, anchor);
1433 g_signal_emit (G_OBJECT (buffer), signals[CHANGED], 0);
1437 * gtk_text_buffer_insert_child_anchor:
1438 * @buffer: a #GtkTextBuffer
1439 * @iter: location to insert the anchor
1440 * @anchor: a #GtkTextChildAnchor
1442 * Inserts a child widget anchor into the text buffer at @iter. The
1443 * anchor will be counted as one character in character counts, and
1444 * when obtaining the buffer contents as a string, will be represented
1445 * by the Unicode "object replacement character" 0xFFFC. Note that the
1446 * "slice" variants for obtaining portions of the buffer as a string
1447 * include this character for pixbufs, but the "text" variants do
1448 * not. e.g. see gtk_text_buffer_get_slice() and
1449 * gtk_text_buffer_get_text(). Consider
1450 * gtk_text_buffer_create_child_anchor() as a more convenient
1451 * alternative to this function. The buffer will add a reference to
1452 * the anchor, so you can unref it after insertion.
1456 gtk_text_buffer_insert_child_anchor (GtkTextBuffer *buffer,
1458 GtkTextChildAnchor *anchor)
1460 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1461 g_return_if_fail (iter != NULL);
1462 g_return_if_fail (GTK_IS_TEXT_CHILD_ANCHOR (anchor));
1463 g_return_if_fail (gtk_text_iter_get_buffer (iter) == buffer);
1465 g_signal_emit (G_OBJECT (buffer), signals[INSERT_CHILD_ANCHOR], 0,
1471 * gtk_text_buffer_create_child_anchor:
1472 * @buffer: a #GtkTextBuffer
1473 * @iter: location in the buffer
1475 * This is a convenience function which simply creates a child anchor
1476 * with gtk_text_child_anchor_new() and inserts it into the buffer
1477 * with gtk_text_buffer_insert_child_anchor().
1479 * Return value: the created child anchor
1482 gtk_text_buffer_create_child_anchor (GtkTextBuffer *buffer,
1485 GtkTextChildAnchor *anchor;
1487 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
1488 g_return_val_if_fail (iter != NULL, NULL);
1489 g_return_val_if_fail (gtk_text_iter_get_buffer (iter) == buffer, NULL);
1491 anchor = gtk_text_child_anchor_new ();
1493 gtk_text_buffer_insert_child_anchor (buffer, iter, anchor);
1495 g_object_unref (G_OBJECT (anchor));
1505 gtk_text_buffer_mark_set (GtkTextBuffer *buffer,
1506 const GtkTextIter *location,
1509 /* IMO this should NOT work like insert_text and delete_range,
1510 * where the real action happens in the default handler.
1512 * The reason is that the default handler would be _required_,
1513 * i.e. the whole widget would start breaking and segfaulting if the
1514 * default handler didn't get run. So you can't really override the
1515 * default handler or stop the emission; that is, this signal is
1516 * purely for notification, and not to allow users to modify the
1520 g_object_ref (G_OBJECT (mark));
1522 g_signal_emit (G_OBJECT (buffer),
1528 g_object_unref (G_OBJECT (mark));
1532 * gtk_text_buffer_set_mark:
1533 * @buffer: a #GtkTextBuffer
1534 * @mark_name: name of the mark
1535 * @iter: location for the mark.
1536 * @left_gravity: if the mark is created by this function, gravity for the new
1538 * @should_exist: if %TRUE, warn if the mark does not exist, and return
1541 * Move the mark to the given position, if not @should_exist, create the mark.
1543 * Return value: mark
1546 gtk_text_buffer_set_mark (GtkTextBuffer *buffer,
1547 GtkTextMark *existing_mark,
1548 const gchar *mark_name,
1549 const GtkTextIter *iter,
1550 gboolean left_gravity,
1551 gboolean should_exist)
1553 GtkTextIter location;
1556 g_return_val_if_fail (gtk_text_iter_get_buffer (iter) == buffer, NULL);
1558 mark = _gtk_text_btree_set_mark (get_btree (buffer),
1565 if (_gtk_text_btree_mark_is_insert (get_btree (buffer), mark) ||
1566 _gtk_text_btree_mark_is_selection_bound (get_btree (buffer), mark))
1568 update_selection_clipboards (buffer);
1571 _gtk_text_btree_get_iter_at_mark (get_btree (buffer),
1575 gtk_text_buffer_mark_set (buffer, &location, mark);
1581 * gtk_text_buffer_create_mark:
1582 * @buffer: a #GtkTextBuffer
1583 * @mark_name: name for mark, or %NULL
1584 * @where: location to place mark
1585 * @left_gravity: whether the mark has left gravity
1587 * Creates a mark at position @where. If @mark_name is %NULL, the mark
1588 * is anonymous; otherwise, the mark can be retrieved by name using
1589 * gtk_text_buffer_get_mark (). If a mark has left gravity, and text is
1590 * inserted at the mark's current location, the mark will be moved to
1591 * the left of the newly-inserted text. If the mark has right gravity
1592 * (@left_gravity = %FALSE), the mark will end up on the right of
1593 * newly-inserted text. The standard left-to-right cursor is a mark
1594 * with right gravity (when you type, the cursor stays on the right
1595 * side of the text you're typing).
1597 * The caller of this function does <emphasis>not</emphasis> own a reference
1598 * to the returned #GtkTextMark, so you can ignore the return value
1599 * if you like. Marks are owned by the buffer and go away when the
1602 * Emits the "mark_set" signal as notification of the mark's initial
1605 * Return value: the new #GtkTextMark object
1608 gtk_text_buffer_create_mark (GtkTextBuffer *buffer,
1609 const gchar *mark_name,
1610 const GtkTextIter *where,
1611 gboolean left_gravity)
1613 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
1615 return gtk_text_buffer_set_mark (buffer, NULL, mark_name, where,
1616 left_gravity, FALSE);
1620 * gtk_text_buffer_move_mark:
1621 * @buffer: a #GtkTextBuffer
1622 * @mark: a #GtkTextMark
1623 * @where: new location for @mark in @buffer
1625 * Moves @mark to the new location @where. Emits the "mark_set" signal
1626 * as notification of the move.
1629 gtk_text_buffer_move_mark (GtkTextBuffer *buffer,
1631 const GtkTextIter *where)
1633 g_return_if_fail (GTK_IS_TEXT_MARK (mark));
1634 g_return_if_fail (!gtk_text_mark_get_deleted (mark));
1635 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1637 gtk_text_buffer_set_mark (buffer, mark, NULL, where, FALSE, TRUE);
1641 * gtk_text_buffer_get_iter_at_mark:
1642 * @buffer: a #GtkTextBuffer
1643 * @iter: iterator to initialize
1644 * @mark: a #GtkTextMark in @buffer
1646 * Initializes @iter with the current position of @mark.
1649 gtk_text_buffer_get_iter_at_mark (GtkTextBuffer *buffer,
1653 g_return_if_fail (GTK_IS_TEXT_MARK (mark));
1654 g_return_if_fail (!gtk_text_mark_get_deleted (mark));
1655 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1657 _gtk_text_btree_get_iter_at_mark (get_btree (buffer),
1663 * gtk_text_buffer_delete_mark:
1664 * @buffer: a #GtkTextBuffer
1665 * @mark: a #GtkTextMark in @buffer
1667 * Deletes @mark, so that it's no longer located anywhere in the
1668 * buffer. Removes the reference the buffer holds to the mark, so if
1669 * you haven't called g_object_ref () on the mark, it will be freed. Even
1670 * if the mark isn't freed, most operations on @mark become
1671 * invalid. There is no way to undelete a
1672 * mark. gtk_text_mark_get_deleted () will return TRUE after this
1673 * function has been called on a mark; gtk_text_mark_get_deleted ()
1674 * indicates that a mark no longer belongs to a buffer. The "mark_deleted"
1675 * signal will be emitted as notification after the mark is deleted.
1678 gtk_text_buffer_delete_mark (GtkTextBuffer *buffer,
1681 g_return_if_fail (GTK_IS_TEXT_MARK (mark));
1682 g_return_if_fail (!gtk_text_mark_get_deleted (mark));
1683 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1685 g_object_ref (G_OBJECT (mark));
1687 _gtk_text_btree_remove_mark (get_btree (buffer), mark);
1689 /* See rationale above for MARK_SET on why we emit this after
1690 * removing the mark, rather than removing the mark in a default
1693 g_signal_emit (G_OBJECT (buffer), signals[MARK_DELETED],
1697 g_object_unref (G_OBJECT (mark));
1701 * gtk_text_buffer_get_mark:
1702 * @buffer: a #GtkTextBuffer
1703 * @name: a mark name
1705 * Returns the mark named @name in buffer @buffer, or NULL if no such
1706 * mark exists in the buffer.
1708 * Return value: a #GtkTextMark, or NULL
1711 gtk_text_buffer_get_mark (GtkTextBuffer *buffer,
1716 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
1717 g_return_val_if_fail (name != NULL, NULL);
1719 mark = _gtk_text_btree_get_mark_by_name (get_btree (buffer), name);
1726 * gtk_text_buffer_move_mark_by_name:
1727 * @buffer: a #GtkTextBuffer
1728 * @name: name of a mark
1729 * @where: new location for mark
1731 * Moves the mark named @name (which must exist) to location @where.
1732 * See gtk_text_buffer_move_mark () for details.
1735 gtk_text_buffer_move_mark_by_name (GtkTextBuffer *buffer,
1737 const GtkTextIter *where)
1741 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1742 g_return_if_fail (name != NULL);
1744 mark = _gtk_text_btree_get_mark_by_name (get_btree (buffer), name);
1748 g_warning ("%s: no mark named '%s'", G_STRLOC, name);
1752 gtk_text_buffer_move_mark (buffer, mark, where);
1756 * gtk_text_buffer_delete_mark_by_name:
1757 * @buffer: a #GtkTextBuffer
1758 * @name: name of a mark in @buffer
1760 * Deletes the mark named @name; the mark must exist. See
1761 * gtk_text_buffer_delete_mark () for details.
1764 gtk_text_buffer_delete_mark_by_name (GtkTextBuffer *buffer,
1769 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1770 g_return_if_fail (name != NULL);
1772 mark = _gtk_text_btree_get_mark_by_name (get_btree (buffer), name);
1776 g_warning ("%s: no mark named '%s'", G_STRLOC, name);
1780 gtk_text_buffer_delete_mark (buffer, mark);
1784 * gtk_text_buffer_get_insert:
1785 * @buffer: a #GtkTextBuffer
1787 * Returns the mark that represents the cursor (insertion point).
1788 * Equivalent to calling gtk_text_buffer_get_mark () to get the mark
1789 * name "insert," but very slightly more efficient, and involves less
1792 * Return value: insertion point mark
1795 gtk_text_buffer_get_insert (GtkTextBuffer *buffer)
1797 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
1799 /* FIXME use struct member in btree */
1800 return gtk_text_buffer_get_mark (buffer, "insert");
1804 * gtk_text_buffer_get_selection_bound:
1805 * @buffer: a #GtkTextBuffer
1807 * Returns the mark that represents the selection bound. Equivalent
1808 * to calling gtk_text_buffer_get_mark () to get the mark name
1809 * "selection_bound," but very slightly more efficient, and involves
1812 * The currently-selected text in @buffer is the region between the
1813 * "selection_bound" and "insert" marks. If "selection_bound" and
1814 * "insert" are in the same place, then there is no current selection.
1815 * gtk_text_buffer_get_selection_bounds () is another convenient function
1816 * for handling the selection, if you just want to know whether there's a
1817 * selection and what its bounds are.
1819 * Return value: selection bound mark
1822 gtk_text_buffer_get_selection_bound (GtkTextBuffer *buffer)
1824 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
1826 /* FIXME use struct member in btree */
1827 return gtk_text_buffer_get_mark (buffer, "selection_bound");
1831 gtk_text_buffer_get_iter_at_child_anchor (GtkTextBuffer *buffer,
1833 GtkTextChildAnchor *anchor)
1835 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1836 g_return_if_fail (iter != NULL);
1837 g_return_if_fail (GTK_IS_TEXT_CHILD_ANCHOR (anchor));
1838 g_return_if_fail (!gtk_text_child_anchor_get_deleted (anchor));
1840 _gtk_text_btree_get_iter_at_child_anchor (get_btree (buffer),
1846 * gtk_text_buffer_place_cursor:
1847 * @buffer: a #GtkTextBuffer
1848 * @where: where to put the cursor
1850 * This function moves the "insert" and "selection_bound" marks
1851 * simultaneously. If you move them to the same place in two steps
1852 * with gtk_text_buffer_move_mark (), you will temporarily select a
1853 * region in between their old and new locations, which can be pretty
1854 * inefficient since the temporarily-selected region will force stuff
1855 * to be recalculated. This function moves them as a unit, which can
1859 gtk_text_buffer_place_cursor (GtkTextBuffer *buffer,
1860 const GtkTextIter *where)
1864 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1868 _gtk_text_btree_place_cursor (get_btree (buffer), &real);
1869 gtk_text_buffer_mark_set (buffer, &real,
1870 gtk_text_buffer_get_mark (buffer,
1872 gtk_text_buffer_mark_set (buffer, &real,
1873 gtk_text_buffer_get_mark (buffer,
1874 "selection_bound"));
1882 * gtk_text_buffer_create_tag:
1883 * @buffer: a #GtkTextBuffer
1884 * @tag_name: name of the new tag, or %NULL
1885 * @first_property_name: name of first property to set, or %NULL
1886 * @Varargs: %NULL-terminated list of property names and values
1889 * Creates a tag and adds it to the tag table for @buffer.
1890 * Equivalent to calling gtk_text_tag_new () and then adding the
1891 * tag to the buffer's tag table. The returned tag is owned by
1892 * the buffer's tag table, so the ref count will be equal to one.
1894 * If @tag_name is %NULL, the tag is anonymous.
1896 * If @tag_name is non-%NULL, a tag called @tag_name must not already
1897 * exist in the tag table for this buffer.
1899 * The @first_property_name argument and subsequent arguments are a list
1900 * of properties to set on the tag, as with g_object_set().
1902 * Return value: a new tag
1905 gtk_text_buffer_create_tag (GtkTextBuffer *buffer,
1906 const gchar *tag_name,
1907 const gchar *first_property_name,
1913 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
1915 tag = gtk_text_tag_new (tag_name);
1917 gtk_text_tag_table_add (get_table (buffer), tag);
1919 if (first_property_name)
1921 va_start (list, first_property_name);
1922 g_object_set_valist (G_OBJECT (tag), first_property_name, list);
1926 g_object_unref (tag);
1932 gtk_text_buffer_real_apply_tag (GtkTextBuffer *buffer,
1934 const GtkTextIter *start,
1935 const GtkTextIter *end)
1937 if (tag->table != buffer->tag_table)
1939 g_warning ("Can only apply tags that are in the tag table for the buffer");
1943 _gtk_text_btree_tag (start, end, tag, TRUE);
1947 gtk_text_buffer_real_remove_tag (GtkTextBuffer *buffer,
1949 const GtkTextIter *start,
1950 const GtkTextIter *end)
1952 if (tag->table != buffer->tag_table)
1954 g_warning ("Can only remove tags that are in the tag table for the buffer");
1958 _gtk_text_btree_tag (start, end, tag, FALSE);
1962 gtk_text_buffer_real_changed (GtkTextBuffer *buffer)
1964 gtk_text_buffer_set_modified (buffer, TRUE);
1968 gtk_text_buffer_emit_tag (GtkTextBuffer *buffer,
1971 const GtkTextIter *start,
1972 const GtkTextIter *end)
1974 GtkTextIter start_tmp = *start;
1975 GtkTextIter end_tmp = *end;
1977 g_return_if_fail (tag != NULL);
1979 gtk_text_iter_order (&start_tmp, &end_tmp);
1982 g_signal_emit (G_OBJECT (buffer), signals[APPLY_TAG],
1984 tag, &start_tmp, &end_tmp);
1986 g_signal_emit (G_OBJECT (buffer), signals[REMOVE_TAG],
1988 tag, &start_tmp, &end_tmp);
1993 * gtk_text_buffer_apply_tag:
1994 * @buffer: a #GtkTextBuffer
1995 * @tag: a #GtkTextTag
1996 * @start: one bound of range to be tagged
1997 * @end: other bound of range to be tagged
1999 * Emits the "apply_tag" signal on @buffer. The default
2000 * handler for the signal applies @tag to the given range.
2001 * @start and @end do not have to be in order.
2005 gtk_text_buffer_apply_tag (GtkTextBuffer *buffer,
2007 const GtkTextIter *start,
2008 const GtkTextIter *end)
2010 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2011 g_return_if_fail (GTK_IS_TEXT_TAG (tag));
2012 g_return_if_fail (start != NULL);
2013 g_return_if_fail (end != NULL);
2014 g_return_if_fail (gtk_text_iter_get_buffer (start) == buffer);
2015 g_return_if_fail (gtk_text_iter_get_buffer (end) == buffer);
2016 g_return_if_fail (tag->table == buffer->tag_table);
2018 gtk_text_buffer_emit_tag (buffer, tag, TRUE, start, end);
2022 * gtk_text_buffer_remove_tag:
2023 * @buffer: a #GtkTextBuffer
2024 * @tag: a #GtkTextTag
2025 * @start: one bound of range to be untagged
2026 * @end: other bound of range to be untagged
2028 * Emits the "remove_tag" signal. The default handler for the signal
2029 * removes all occurrences of @tag from the given range. @start and
2030 * @end don't have to be in order.
2034 gtk_text_buffer_remove_tag (GtkTextBuffer *buffer,
2036 const GtkTextIter *start,
2037 const GtkTextIter *end)
2040 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2041 g_return_if_fail (GTK_IS_TEXT_TAG (tag));
2042 g_return_if_fail (start != NULL);
2043 g_return_if_fail (end != NULL);
2044 g_return_if_fail (gtk_text_iter_get_buffer (start) == buffer);
2045 g_return_if_fail (gtk_text_iter_get_buffer (end) == buffer);
2046 g_return_if_fail (tag->table == buffer->tag_table);
2048 gtk_text_buffer_emit_tag (buffer, tag, FALSE, start, end);
2053 * gtk_text_buffer_apply_tag_by_name:
2054 * @buffer: a #GtkTextBuffer
2055 * @name: name of a named #GtkTextTag
2056 * @start: one bound of range to be tagged
2057 * @end: other bound of range to be tagged
2059 * Calls gtk_text_tag_table_lookup() on the buffer's tag table to
2060 * get a #GtkTextTag, then calls gtk_text_buffer_apply_tag().
2064 gtk_text_buffer_apply_tag_by_name (GtkTextBuffer *buffer,
2066 const GtkTextIter *start,
2067 const GtkTextIter *end)
2071 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2072 g_return_if_fail (name != NULL);
2073 g_return_if_fail (start != NULL);
2074 g_return_if_fail (end != NULL);
2075 g_return_if_fail (gtk_text_iter_get_buffer (start) == buffer);
2076 g_return_if_fail (gtk_text_iter_get_buffer (end) == buffer);
2078 tag = gtk_text_tag_table_lookup (get_table (buffer),
2083 g_warning ("Unknown tag `%s'", name);
2087 gtk_text_buffer_emit_tag (buffer, tag, TRUE, start, end);
2091 * gtk_text_buffer_remove_tag_by_name:
2092 * @buffer: a #GtkTextBuffer
2093 * @name: name of a #GtkTextTag
2094 * @start: one bound of range to be untagged
2095 * @end: other bound of range to be untagged
2097 * Calls gtk_text_tag_table_lookup() on the buffer's tag table to
2098 * get a #GtkTextTag, then calls gtk_text_buffer_remove_tag().
2103 gtk_text_buffer_remove_tag_by_name (GtkTextBuffer *buffer,
2105 const GtkTextIter *start,
2106 const GtkTextIter *end)
2110 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2111 g_return_if_fail (name != NULL);
2112 g_return_if_fail (start != NULL);
2113 g_return_if_fail (end != NULL);
2114 g_return_if_fail (gtk_text_iter_get_buffer (start) == buffer);
2115 g_return_if_fail (gtk_text_iter_get_buffer (end) == buffer);
2117 tag = gtk_text_tag_table_lookup (get_table (buffer),
2122 g_warning ("Unknown tag `%s'", name);
2126 gtk_text_buffer_emit_tag (buffer, tag, FALSE, start, end);
2130 pointer_cmp (gconstpointer a,
2142 * gtk_text_buffer_remove_all_tags:
2143 * @buffer: a #GtkTextBuffer
2144 * @start: one bound of range to be untagged
2145 * @end: other bound of range to be untagged
2147 * Removes all tags in the range between @start and @end. Be careful
2148 * with this function; it could remove tags added in code unrelated to
2149 * the code you're currently writing. That is, using this function is
2150 * probably a bad idea if you have two or more unrelated code sections
2154 gtk_text_buffer_remove_all_tags (GtkTextBuffer *buffer,
2155 const GtkTextIter *start,
2156 const GtkTextIter *end)
2158 GtkTextIter first, second, tmp;
2164 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2165 g_return_if_fail (start != NULL);
2166 g_return_if_fail (end != NULL);
2167 g_return_if_fail (gtk_text_iter_get_buffer (start) == buffer);
2168 g_return_if_fail (gtk_text_iter_get_buffer (end) == buffer);
2173 gtk_text_iter_order (&first, &second);
2175 /* Get all tags turned on at the start */
2176 tags = gtk_text_iter_get_tags (&first);
2178 /* Find any that are toggled on within the range */
2180 while (gtk_text_iter_forward_to_tag_toggle (&tmp, NULL))
2185 if (gtk_text_iter_compare (&tmp, &second) >= 0)
2186 break; /* past the end of the range */
2188 toggled = gtk_text_iter_get_toggled_tags (&tmp, TRUE);
2190 /* We could end up with a really big-ass list here.
2193 tmp_list2 = toggled;
2194 while (tmp_list2 != NULL)
2196 tags = g_slist_prepend (tags, tmp_list2->data);
2198 tmp_list2 = g_slist_next (tmp_list2);
2201 g_slist_free (toggled);
2205 tags = g_slist_sort (tags, pointer_cmp);
2207 /* Strip duplicates */
2211 while (tmp_list != NULL)
2213 if (tag == tmp_list->data)
2217 prev->next = tmp_list->next;
2219 tmp_list->next = NULL;
2221 g_slist_free (tmp_list);
2223 tmp_list = prev->next;
2224 /* prev is unchanged */
2228 /* not a duplicate */
2229 tag = GTK_TEXT_TAG (tmp_list->data);
2231 tmp_list = tmp_list->next;
2235 g_slist_foreach (tags, (GFunc) g_object_ref, NULL);
2238 while (tmp_list != NULL)
2240 tag = GTK_TEXT_TAG (tmp_list->data);
2242 gtk_text_buffer_remove_tag (buffer, tag, &first, &second);
2244 tmp_list = tmp_list->next;
2247 g_slist_foreach (tags, (GFunc) g_object_unref, NULL);
2249 g_slist_free (tags);
2254 * Obtain various iterators
2258 * gtk_text_buffer_get_iter_at_line_offset:
2259 * @buffer: a #GtkTextBuffer
2260 * @iter: iterator to initialize
2261 * @line_number: line number counting from 0
2262 * @char_offset: char offset from start of line
2264 * Obtains an iterator pointing to @char_offset within the given
2265 * line. The @char_offset must exist, offsets off the end of the line
2266 * are not allowed. Note <emphasis>characters</emphasis>, not bytes;
2267 * UTF-8 may encode one character as multiple bytes.
2271 gtk_text_buffer_get_iter_at_line_offset (GtkTextBuffer *buffer,
2276 g_return_if_fail (iter != NULL);
2277 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2279 _gtk_text_btree_get_iter_at_line_char (get_btree (buffer),
2280 iter, line_number, char_offset);
2284 * gtk_text_buffer_get_iter_at_line_index:
2285 * @buffer: a #GtkTextBuffer
2286 * @iter: iterator to initialize
2287 * @line_number: line number counting from 0
2288 * @byte_index: byte index from start of line
2290 * Obtains an iterator pointing to @byte_index within the given line.
2291 * @byte_index must be the start of a UTF-8 character, and must not be
2292 * beyond the end of the line. Note <emphasis>bytes</emphasis>, not
2293 * characters; UTF-8 may encode one character as multiple bytes.
2297 gtk_text_buffer_get_iter_at_line_index (GtkTextBuffer *buffer,
2302 g_return_if_fail (iter != NULL);
2303 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2305 _gtk_text_btree_get_iter_at_line_byte (get_btree (buffer),
2306 iter, line_number, byte_index);
2310 * gtk_text_buffer_get_iter_at_line:
2311 * @buffer: a #GtkTextBuffer
2312 * @iter: iterator to initialize
2313 * @line_number: line number counting from 0
2315 * Initializes @iter to the start of the given line.
2318 gtk_text_buffer_get_iter_at_line (GtkTextBuffer *buffer,
2322 g_return_if_fail (iter != NULL);
2323 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2325 gtk_text_buffer_get_iter_at_line_offset (buffer, iter, line_number, 0);
2329 * gtk_text_buffer_get_iter_at_offset:
2330 * @buffer: a #GtkTextBuffer
2331 * @iter: iterator to initialize
2332 * @char_offset: char offset from start of buffer, counting from 0
2334 * Initializes @iter to a position @char_offset chars from the start
2335 * of the entire buffer.
2339 gtk_text_buffer_get_iter_at_offset (GtkTextBuffer *buffer,
2343 g_return_if_fail (iter != NULL);
2344 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2346 _gtk_text_btree_get_iter_at_char (get_btree (buffer), iter, char_offset);
2350 * gtk_text_buffer_get_start_iter:
2351 * @buffer: a #GtkTextBuffer
2352 * @iter: iterator to initialize
2354 * Initialized @iter with the first position in the text buffer. This
2355 * is the same as using gtk_text_buffer_get_iter_at_offset() to get
2356 * the iter at character offset 0.
2360 gtk_text_buffer_get_start_iter (GtkTextBuffer *buffer,
2363 g_return_if_fail (iter != NULL);
2364 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2366 _gtk_text_btree_get_iter_at_char (get_btree (buffer), iter, 0);
2370 * gtk_text_buffer_get_end_iter:
2371 * @buffer: a #GtkTextBuffer
2372 * @iter: iterator to initialize
2374 * Initializes @iter with the "end iterator," one past the last valid
2375 * character in the text buffer. If dereferenced with
2376 * gtk_text_iter_get_char(), the end iterator has a character value of
2377 * 0. The entire buffer lies in the range from the first position in
2378 * the buffer (call gtk_text_buffer_get_start_iter() to get
2379 * character position 0) to the end iterator.
2383 gtk_text_buffer_get_end_iter (GtkTextBuffer *buffer,
2386 g_return_if_fail (iter != NULL);
2387 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2389 _gtk_text_btree_get_end_iter (get_btree (buffer), iter);
2393 * gtk_text_buffer_get_bounds:
2394 * @buffer: a #GtkTextBuffer
2395 * @start: iterator to initialize with first position in the buffer
2396 * @end: iterator to initialize with the end iterator
2398 * Retrieves the first and last iterators in the buffer, i.e. the
2399 * entire buffer lies within the range [@start,@end).
2403 gtk_text_buffer_get_bounds (GtkTextBuffer *buffer,
2407 g_return_if_fail (start != NULL);
2408 g_return_if_fail (end != NULL);
2409 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2411 _gtk_text_btree_get_iter_at_char (get_btree (buffer), start, 0);
2412 _gtk_text_btree_get_end_iter (get_btree (buffer), end);
2420 * gtk_text_buffer_get_modified:
2421 * @buffer: a #GtkTextBuffer
2423 * Indicates whether the buffer has been modified since the last call
2424 * to gtk_text_buffer_set_modified() set the modification flag to
2425 * %FALSE. Used for example to enable a "save" function in a text
2428 * Return value: %TRUE if the buffer has been modified
2431 gtk_text_buffer_get_modified (GtkTextBuffer *buffer)
2433 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
2435 return buffer->modified;
2439 * gtk_text_buffer_set_modified:
2440 * @buffer: a #GtkTextBuffer
2441 * @setting: modification flag setting
2443 * Used to keep track of whether the buffer has been modified since the
2444 * last time it was saved. Whenever the buffer is saved to disk, call
2445 * gtk_text_buffer_set_modified (@buffer, FALSE). When the buffer is modified,
2446 * it will automatically toggled on the modified bit again. When the modified
2447 * bit flips, the buffer emits a "modified_changed" signal.
2451 gtk_text_buffer_set_modified (GtkTextBuffer *buffer,
2454 gboolean fixed_setting;
2456 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2458 fixed_setting = setting != FALSE;
2460 if (buffer->modified == fixed_setting)
2464 buffer->modified = fixed_setting;
2465 g_signal_emit (G_OBJECT (buffer), signals[MODIFIED_CHANGED], 0);
2471 * Assorted other stuff
2475 * gtk_text_buffer_get_line_count:
2476 * @buffer: a #GtkTextBuffer
2478 * Obtains the number of lines in the buffer. This value is cached, so
2479 * the function is very fast.
2481 * Return value: number of lines in the buffer
2484 gtk_text_buffer_get_line_count (GtkTextBuffer *buffer)
2486 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), 0);
2488 return _gtk_text_btree_line_count (get_btree (buffer));
2492 * gtk_text_buffer_get_char_count:
2493 * @buffer: a #GtkTextBuffer
2495 * Gets the number of characters in the buffer; note that characters
2496 * and bytes are not the same, you can't e.g. expect the contents of
2497 * the buffer in string form to be this many bytes long. The character
2498 * count is cached, so this function is very fast.
2500 * Return value: number of characters in the buffer
2503 gtk_text_buffer_get_char_count (GtkTextBuffer *buffer)
2505 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), 0);
2507 return _gtk_text_btree_char_count (get_btree (buffer));
2510 /* Called when we lose the primary selection.
2513 clipboard_clear_selection_cb (GtkClipboard *clipboard,
2516 /* Move selection_bound to the insertion point */
2518 GtkTextIter selection_bound;
2519 GtkTextBuffer *buffer = GTK_TEXT_BUFFER (data);
2521 gtk_text_buffer_get_iter_at_mark (buffer, &insert,
2522 gtk_text_buffer_get_mark (buffer, "insert"));
2523 gtk_text_buffer_get_iter_at_mark (buffer, &selection_bound,
2524 gtk_text_buffer_get_mark (buffer, "selection_bound"));
2526 if (!gtk_text_iter_equal (&insert, &selection_bound))
2527 gtk_text_buffer_move_mark (buffer,
2528 gtk_text_buffer_get_mark (buffer, "selection_bound"),
2532 /* Called when we have the primary selection and someone else wants our
2533 * data in order to paste it.
2536 clipboard_get_selection_cb (GtkClipboard *clipboard,
2537 GtkSelectionData *selection_data,
2541 GtkTextBuffer *buffer = GTK_TEXT_BUFFER (data);
2542 GtkTextIter start, end;
2544 if (gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
2546 if (selection_data->target ==
2547 gdk_atom_intern ("GTK_TEXT_BUFFER_CONTENTS", FALSE))
2549 /* Provide the address of the buffer; this will only be
2550 * used within-process
2552 gtk_selection_data_set (selection_data,
2553 gdk_atom_intern ("GTK_TEXT_BUFFER_CONTENTS", FALSE),
2562 str = gtk_text_iter_get_visible_text (&start, &end);
2563 gtk_selection_data_set_text (selection_data, str, -1);
2571 GtkClipboard *clipboard;
2572 GtkTextBuffer *buffer;
2576 remove_all_clipboard_contents_buffers (GtkTextBuffer *buffer)
2578 GSList *tmp_list = buffer->clipboard_contents_buffers;
2581 ContentsBuffer *contents_buffer = tmp_list->data;
2583 g_object_unref (contents_buffer->buffer);
2584 g_free (contents_buffer);
2586 tmp_list = tmp_list->next;
2589 g_slist_free (buffer->clipboard_contents_buffers);
2590 buffer->clipboard_contents_buffers = NULL;
2594 remove_clipboard_contents_buffer (GtkTextBuffer *buffer,
2595 GtkClipboard *clipboard)
2597 GSList *tmp_list = buffer->clipboard_contents_buffers;
2600 ContentsBuffer *contents_buffer = tmp_list->data;
2602 if (contents_buffer->clipboard == clipboard)
2604 buffer->clipboard_contents_buffers = g_slist_remove (buffer->clipboard_contents_buffers, contents_buffer);
2606 g_object_unref (contents_buffer->buffer);
2607 g_free (contents_buffer);
2612 tmp_list = tmp_list->next;
2616 static GtkTextBuffer *
2617 get_clipboard_contents_buffer (GtkTextBuffer *buffer,
2618 GtkClipboard *clipboard,
2621 ContentsBuffer *contents_buffer;
2624 tmp_list = buffer->clipboard_contents_buffers;
2627 contents_buffer = tmp_list->data;
2628 if (contents_buffer->clipboard == clipboard)
2629 return contents_buffer->buffer;
2634 contents_buffer = g_new (ContentsBuffer, 1);
2635 contents_buffer->clipboard = clipboard;
2636 contents_buffer->buffer = gtk_text_buffer_new (gtk_text_buffer_get_tag_table (buffer));
2638 g_object_set_data (G_OBJECT (contents_buffer->buffer), "gtk-text-buffer-clipboard",
2639 GUINT_TO_POINTER (1));
2641 buffer->clipboard_contents_buffers = g_slist_prepend (buffer->clipboard_contents_buffers, contents_buffer);
2644 return contents_buffer->buffer;
2650 /* Provide cut/copied data */
2652 clipboard_get_contents_cb (GtkClipboard *clipboard,
2653 GtkSelectionData *selection_data,
2657 GtkTextBuffer *buffer = GTK_TEXT_BUFFER (data);
2658 GtkTextBuffer *contents = get_clipboard_contents_buffer (buffer, clipboard, FALSE);
2660 g_assert (contents); /* This should never be called unless we own the clipboard */
2662 if (selection_data->target ==
2663 gdk_atom_intern ("GTK_TEXT_BUFFER_CONTENTS", FALSE))
2665 /* Provide the address of the clipboard buffer; this will only
2666 * be used within-process. OK to supply a NULL value for contents.
2668 gtk_selection_data_set (selection_data,
2669 gdk_atom_intern ("GTK_TEXT_BUFFER_CONTENTS", FALSE),
2677 GtkTextIter start, end;
2679 gtk_text_buffer_get_bounds (contents, &start, &end);
2681 str = gtk_text_iter_get_visible_text (&start, &end);
2682 gtk_selection_data_set_text (selection_data, str, -1);
2688 clipboard_clear_contents_cb (GtkClipboard *clipboard,
2691 GtkTextBuffer *buffer = GTK_TEXT_BUFFER (data);
2693 remove_clipboard_contents_buffer (buffer, clipboard);
2697 get_paste_point (GtkTextBuffer *buffer,
2699 gboolean clear_afterward)
2701 GtkTextIter insert_point;
2702 GtkTextMark *paste_point_override;
2704 paste_point_override = gtk_text_buffer_get_mark (buffer,
2705 "gtk_paste_point_override");
2707 if (paste_point_override != NULL)
2709 gtk_text_buffer_get_iter_at_mark (buffer, &insert_point,
2710 paste_point_override);
2711 if (clear_afterward)
2712 gtk_text_buffer_delete_mark (buffer,
2713 gtk_text_buffer_get_mark (buffer,
2714 "gtk_paste_point_override"));
2718 gtk_text_buffer_get_iter_at_mark (buffer, &insert_point,
2719 gtk_text_buffer_get_mark (buffer,
2723 *iter = insert_point;
2727 pre_paste_prep (ClipboardRequest *request_data,
2728 GtkTextIter *insert_point)
2730 GtkTextBuffer *buffer = request_data->buffer;
2732 get_paste_point (buffer, insert_point, TRUE);
2734 /* If we're going to replace the selection, we insert before it to
2735 * avoid messing it up, then we delete the selection after inserting.
2737 if (request_data->replace_selection)
2739 GtkTextIter start, end;
2741 if (gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
2742 *insert_point = start;
2747 post_paste_cleanup (ClipboardRequest *request_data)
2749 if (request_data->replace_selection)
2751 GtkTextIter start, end;
2753 if (gtk_text_buffer_get_selection_bounds (request_data->buffer,
2756 if (request_data->interactive)
2757 gtk_text_buffer_delete_interactive (request_data->buffer,
2760 request_data->default_editable);
2762 gtk_text_buffer_delete (request_data->buffer, &start, &end);
2767 /* Called when we request a paste and receive the text data
2770 clipboard_text_received (GtkClipboard *clipboard,
2774 ClipboardRequest *request_data = data;
2775 GtkTextBuffer *buffer = request_data->buffer;
2779 GtkTextIter insert_point;
2781 pre_paste_prep (request_data, &insert_point);
2783 if (request_data->interactive)
2784 gtk_text_buffer_insert_interactive (buffer, &insert_point,
2785 str, -1, request_data->default_editable);
2787 gtk_text_buffer_insert (buffer, &insert_point,
2790 post_paste_cleanup (request_data);
2793 g_object_unref (G_OBJECT (buffer));
2794 g_free (request_data);
2797 static GtkTextBuffer*
2798 selection_data_get_buffer (GtkSelectionData *selection_data,
2799 ClipboardRequest *request_data)
2802 GtkTextBuffer *src_buffer = NULL;
2804 /* If we can get the owner, the selection is in-process */
2805 owner = gdk_selection_owner_get (selection_data->selection);
2810 if (selection_data->type != gdk_atom_intern ("GTK_TEXT_BUFFER_CONTENTS", FALSE))
2813 if (selection_data->length != sizeof (src_buffer))
2816 memcpy (&src_buffer, selection_data->data, sizeof (src_buffer));
2818 if (src_buffer == NULL)
2821 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (src_buffer), NULL);
2823 if (gtk_text_buffer_get_tag_table (src_buffer) !=
2824 gtk_text_buffer_get_tag_table (request_data->buffer))
2831 /* These are pretty handy functions; maybe something like them
2832 * should be in the public API. Also, there are other places in this
2833 * file where they could be used.
2836 save_iter (const GtkTextIter *iter,
2837 gboolean left_gravity)
2839 return gtk_text_buffer_create_mark (gtk_text_iter_get_buffer (iter),
2846 restore_iter (const GtkTextIter *iter,
2849 gtk_text_buffer_get_iter_at_mark (gtk_text_mark_get_buffer (save_id),
2850 (GtkTextIter*) iter,
2852 gtk_text_buffer_delete_mark (gtk_text_mark_get_buffer (save_id),
2858 paste_from_buffer (ClipboardRequest *request_data,
2859 GtkTextBuffer *src_buffer,
2860 const GtkTextIter *start,
2861 const GtkTextIter *end)
2863 GtkTextIter insert_point;
2865 /* We're about to emit a bunch of signals, so be safe */
2866 g_object_ref (G_OBJECT (src_buffer));
2868 pre_paste_prep (request_data, &insert_point);
2870 if (!gtk_text_iter_equal (start, end))
2872 gtk_text_buffer_real_insert_range (request_data->buffer,
2876 request_data->interactive);
2879 post_paste_cleanup (request_data);
2881 g_object_unref (G_OBJECT (src_buffer));
2885 clipboard_clipboard_buffer_received (GtkClipboard *clipboard,
2886 GtkSelectionData *selection_data,
2889 ClipboardRequest *request_data = data;
2890 GtkTextBuffer *src_buffer;
2892 src_buffer = selection_data_get_buffer (selection_data, request_data);
2896 GtkTextIter start, end;
2898 if (g_object_get_data (G_OBJECT (src_buffer), "gtk-text-buffer-clipboard"))
2900 gtk_text_buffer_get_bounds (src_buffer, &start, &end);
2902 paste_from_buffer (request_data, src_buffer,
2907 if (gtk_text_buffer_get_selection_bounds (src_buffer, &start, &end))
2908 paste_from_buffer (request_data, src_buffer,
2914 /* Request the text selection instead */
2915 gtk_clipboard_request_text (clipboard,
2916 clipboard_text_received,
2921 static const GtkTargetEntry targets[] = {
2922 { "STRING", 0, TARGET_STRING },
2923 { "TEXT", 0, TARGET_TEXT },
2924 { "COMPOUND_TEXT", 0, TARGET_COMPOUND_TEXT },
2925 { "UTF8_STRING", 0, TARGET_UTF8_STRING },
2926 { "GTK_TEXT_BUFFER_CONTENTS", 0, TARGET_TEXT_BUFFER_CONTENTS }
2931 GtkClipboard *clipboard;
2933 } SelectionClipboard;
2936 update_selection_clipboards (GtkTextBuffer *buffer)
2938 GSList *tmp_list = buffer->selection_clipboards;
2944 SelectionClipboard *selection_clipboard = tmp_list->data;
2945 GtkClipboard *clipboard = selection_clipboard->clipboard;
2947 /* Determine whether we have a selection and adjust X selection
2950 if (!gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
2952 if (gtk_clipboard_get_owner (clipboard) == G_OBJECT (buffer))
2953 gtk_clipboard_clear (clipboard);
2957 /* Even if we already have the selection, we need to update our
2960 if (!gtk_clipboard_set_with_owner (clipboard, targets, G_N_ELEMENTS (targets),
2961 clipboard_get_selection_cb,
2962 clipboard_clear_selection_cb,
2964 clipboard_clear_selection_cb (clipboard, buffer);
2967 tmp_list = tmp_list->next;
2971 static SelectionClipboard *
2972 find_selection_clipboard (GtkTextBuffer *buffer,
2973 GtkClipboard *clipboard)
2975 GSList *tmp_list = buffer->selection_clipboards;
2978 SelectionClipboard *selection_clipboard = tmp_list->data;
2979 if (selection_clipboard->clipboard == clipboard)
2980 return selection_clipboard;
2982 tmp_list = tmp_list->next;
2989 * gtk_text_buffer_add_selection_clipboard:
2990 * @buffer: a #GtkTextBuffer
2991 * @clipboard: a #GtkClipboard
2993 * Adds @clipboard to the list of clipboards in which the selection contents
2994 * of @buffer are available. In most cases, @clipboard will be the #GtkClipboard
2995 * of type %GDK_SELECTION_PRIMARY for a view of @buffer.
2998 gtk_text_buffer_add_selection_clipboard (GtkTextBuffer *buffer,
2999 GtkClipboard *clipboard)
3001 SelectionClipboard *selection_clipboard;
3003 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
3004 g_return_if_fail (clipboard != NULL);
3006 selection_clipboard = find_selection_clipboard (buffer, clipboard);
3007 if (selection_clipboard)
3009 selection_clipboard->ref_count++;
3013 selection_clipboard = g_new (SelectionClipboard, 1);
3015 selection_clipboard->clipboard = clipboard;
3016 selection_clipboard->ref_count = 1;
3018 buffer->selection_clipboards = g_slist_prepend (buffer->selection_clipboards, selection_clipboard);
3023 * gtk_text_buffer_remove_selection_clipboard:
3024 * @buffer: a #GtkTextBuffer
3025 * @clipboard: a #GtkClipboard added to @buffer by gtk_text_buffer_add_selection_clipboard().
3027 * Removes a #GtkClipboard added with gtk_text_buffer_add_selection_clipboard()
3030 gtk_text_buffer_remove_selection_clipboard (GtkTextBuffer *buffer,
3031 GtkClipboard *clipboard)
3033 SelectionClipboard *selection_clipboard;
3035 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
3036 g_return_if_fail (clipboard != NULL);
3038 selection_clipboard = find_selection_clipboard (buffer, clipboard);
3039 g_return_if_fail (selection_clipboard != NULL);
3041 selection_clipboard->ref_count--;
3042 if (selection_clipboard->ref_count == 0)
3044 if (gtk_clipboard_get_owner (selection_clipboard->clipboard) == G_OBJECT (buffer))
3045 gtk_clipboard_clear (selection_clipboard->clipboard);
3047 buffer->selection_clipboards = g_slist_remove (buffer->selection_clipboards,
3048 selection_clipboard);
3050 g_free (selection_clipboard);
3055 remove_all_selection_clipboards (GtkTextBuffer *buffer)
3057 GSList *tmp_list = buffer->selection_clipboards;
3060 SelectionClipboard *selection_clipboard = tmp_list->data;
3062 if (gtk_clipboard_get_owner (selection_clipboard->clipboard) == G_OBJECT (buffer))
3063 gtk_clipboard_clear (selection_clipboard->clipboard);
3065 g_free (selection_clipboard);
3067 tmp_list = tmp_list->next;
3070 g_slist_free (buffer->selection_clipboards);
3071 buffer->selection_clipboards = NULL;
3075 * gtk_text_buffer_paste_clipboard:
3076 * @buffer: a #GtkTextBuffer
3077 * @clipboard: the #GtkClipboard to paste from
3078 * @override_location: location to insert pasted text, or %NULL for at the cursor
3079 * @default_editable: whether the buffer is editable by default
3081 * Pastes the contents of a clipboard at the insertion point, or at @override_location.
3082 * (Note: pasting is asynchronous, that is, we'll ask for the paste data and
3083 * return, and at some point later after the main loop runs, the paste
3084 * data will be inserted.)
3088 gtk_text_buffer_paste_clipboard (GtkTextBuffer *buffer,
3089 GtkClipboard *clipboard,
3090 GtkTextIter *override_location,
3091 gboolean default_editable)
3093 ClipboardRequest *data = g_new (ClipboardRequest, 1);
3094 GtkTextIter paste_point;
3095 GtkTextIter start, end;
3097 if (override_location != NULL)
3098 gtk_text_buffer_create_mark (buffer,
3099 "gtk_paste_point_override",
3100 override_location, FALSE);
3102 data->buffer = buffer;
3103 g_object_ref (G_OBJECT (buffer));
3104 data->interactive = TRUE;
3105 data->default_editable = default_editable;
3107 /* When pasting with the cursor inside the selection area, you
3108 * replace the selection with the new text, otherwise, you
3109 * simply insert the new text at the point where the click
3110 * occured, unselecting any selected text. The replace_selection
3111 * flag toggles this behavior.
3113 data->replace_selection = FALSE;
3115 get_paste_point (buffer, &paste_point, FALSE);
3116 if (gtk_text_buffer_get_selection_bounds (buffer, &start, &end) &&
3117 (gtk_text_iter_in_range (&paste_point, &start, &end) ||
3118 gtk_text_iter_equal (&paste_point, &end)))
3119 data->replace_selection = TRUE;
3121 gtk_clipboard_request_contents (clipboard,
3122 gdk_atom_intern ("GTK_TEXT_BUFFER_CONTENTS", FALSE),
3123 clipboard_clipboard_buffer_received, data);
3127 * gtk_text_buffer_delete_selection:
3128 * @buffer: a #GtkTextBuffer
3129 * @interactive: whether the deletion is caused by user interaction
3130 * @default_editable: whether the buffer is editable by default
3132 * Deletes the range between the "insert" and "selection_bound" marks,
3133 * that is, the currently-selected text. If @interactive is %TRUE,
3134 * the editability of the selection will be considered (users can't delete
3137 * Return value: whether there was a non-empty selection to delete
3140 gtk_text_buffer_delete_selection (GtkTextBuffer *buffer,
3141 gboolean interactive,
3142 gboolean default_editable)
3147 if (!gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
3149 return FALSE; /* No selection */
3155 gtk_text_buffer_begin_user_action (buffer);
3156 gtk_text_buffer_delete_interactive (buffer, &start, &end, default_editable);
3157 gtk_text_buffer_end_user_action (buffer);
3160 gtk_text_buffer_delete (buffer, &start, &end);
3162 return TRUE; /* We deleted stuff */
3167 cut_or_copy (GtkTextBuffer *buffer,
3168 GtkClipboard *clipboard,
3169 gboolean delete_region_after,
3170 gboolean interactive,
3171 gboolean default_editable)
3173 /* We prefer to cut the selected region between selection_bound and
3174 * insertion point. If that region is empty, then we cut the region
3175 * between the "anchor" and the insertion point (this is for
3176 * C-space and M-w and other Emacs-style copy/yank keys). Note that
3177 * insert and selection_bound are guaranteed to exist, but the
3178 * anchor only exists sometimes.
3183 remove_clipboard_contents_buffer (buffer, clipboard);
3185 if (!gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
3187 /* Let's try the anchor thing */
3188 GtkTextMark * anchor = gtk_text_buffer_get_mark (buffer, "anchor");
3194 gtk_text_buffer_get_iter_at_mark (buffer, &end, anchor);
3195 gtk_text_iter_order (&start, &end);
3199 if (!gtk_text_iter_equal (&start, &end))
3202 GtkTextBuffer *contents;
3204 contents = get_clipboard_contents_buffer (buffer, clipboard, TRUE);
3206 gtk_text_buffer_get_iter_at_offset (contents, &ins, 0);
3208 gtk_text_buffer_insert_range (contents, &ins, &start, &end);
3210 if (!gtk_clipboard_set_with_owner (clipboard, targets, G_N_ELEMENTS (targets),
3211 clipboard_get_contents_cb,
3212 clipboard_clear_contents_cb,
3214 clipboard_clear_contents_cb (clipboard, buffer);
3216 if (delete_region_after)
3219 gtk_text_buffer_delete_interactive (buffer, &start, &end,
3222 gtk_text_buffer_delete (buffer, &start, &end);
3228 * gtk_text_buffer_cut_clipboard:
3229 * @buffer: a #GtkTextBuffer
3230 * @clipboard: the #GtkClipboard object to cut to.
3231 * @default_editable: default editability of the buffer
3233 * Copies the currently-selected text to a clipboard, then deletes
3234 * said text if it's editable.
3238 gtk_text_buffer_cut_clipboard (GtkTextBuffer *buffer,
3239 GtkClipboard *clipboard,
3240 gboolean default_editable)
3242 gtk_text_buffer_begin_user_action (buffer);
3243 cut_or_copy (buffer, clipboard, TRUE, TRUE, default_editable);
3244 gtk_text_buffer_end_user_action (buffer);
3248 * gtk_text_buffer_copy_clipboard:
3249 * @buffer: a #GtkTextBuffer
3250 * @clipboard: the #GtkClipboard object to copy to.
3252 * Copies the currently-selected text to a clipboard.
3256 gtk_text_buffer_copy_clipboard (GtkTextBuffer *buffer,
3257 GtkClipboard *clipboard)
3259 gtk_text_buffer_begin_user_action (buffer);
3260 cut_or_copy (buffer, clipboard, FALSE, TRUE, TRUE);
3261 gtk_text_buffer_end_user_action (buffer);
3266 * gtk_text_buffer_get_selection_bounds:
3267 * @buffer: a #GtkTextBuffer a #GtkTextBuffer
3268 * @start: iterator to initialize with selection start
3269 * @end: iterator to initialize with selection end
3271 * Returns %TRUE if some text is selected; places the bounds
3272 * of the selection in @start and @end (if the selection has length 0,
3273 * then @start and @end are filled in with the same value).
3274 * @start and @end will be in ascending order. If @start and @end are
3275 * NULL, then they are not filled in, but the return value still indicates
3276 * whether text is selected.
3278 * Return value: whether the selection has nonzero length
3281 gtk_text_buffer_get_selection_bounds (GtkTextBuffer *buffer,
3285 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
3287 return _gtk_text_btree_get_selection_bounds (get_btree (buffer), start, end);
3291 * gtk_text_buffer_begin_user_action:
3292 * @buffer: a #GtkTextBuffer
3294 * Called to indicate that the buffer operations between here and a
3295 * call to gtk_text_buffer_end_user_action() are part of a single
3296 * user-visible operation. The operations between
3297 * gtk_text_buffer_begin_user_action() and
3298 * gtk_text_buffer_end_user_action() can then be grouped when creating
3299 * an undo stack. #GtkTextBuffer maintains a count of calls to
3300 * gtk_text_buffer_begin_user_action() that have not been closed with
3301 * a call to gtk_text_buffer_end_user_action(), and emits the "begin_user_action"
3302 * and "end_user_action" signals only for the outermost pair of calls.
3303 * This allows you to build user actions from other user actions.
3305 * The "interactive" buffer mutation functions, such as
3306 * gtk_text_buffer_insert_interactive(), automatically call begin/end
3307 * user action around the buffer operations they perform, so there's
3308 * no need to add extra calls if you user action consists solely of a
3309 * single call to one of those functions.
3312 gtk_text_buffer_begin_user_action (GtkTextBuffer *buffer)
3314 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
3316 buffer->user_action_count += 1;
3318 if (buffer->user_action_count == 1)
3320 /* Outermost nested user action begin emits the signal */
3321 g_signal_emit (G_OBJECT (buffer), signals[BEGIN_USER_ACTION], 0);
3326 * gtk_text_buffer_end_user_action:
3327 * @buffer: a #GtkTextBuffer
3329 * Should be paired with a call to gtk_text_buffer_begin_user_action().
3330 * See that function for a full explanation.
3333 gtk_text_buffer_end_user_action (GtkTextBuffer *buffer)
3335 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
3336 g_return_if_fail (buffer->user_action_count > 0);
3338 buffer->user_action_count -= 1;
3340 if (buffer->user_action_count == 0)
3342 /* Ended the outermost-nested user action end, so emit the signal */
3343 g_signal_emit (G_OBJECT (buffer), signals[END_USER_ACTION], 0);
3348 * Logical attribute cache
3351 #define ATTR_CACHE_SIZE 2
3353 typedef struct _CacheEntry CacheEntry;
3358 PangoLogAttr *attrs;
3362 struct _GtkTextLogAttrCache
3364 gint chars_changed_stamp;
3365 CacheEntry entries[ATTR_CACHE_SIZE];
3369 free_log_attr_cache (GtkTextLogAttrCache *cache)
3372 while (i < ATTR_CACHE_SIZE)
3374 g_free (cache->entries[i].attrs);
3381 clear_log_attr_cache (GtkTextLogAttrCache *cache)
3384 while (i < ATTR_CACHE_SIZE)
3386 g_free (cache->entries[i].attrs);
3387 cache->entries[i].attrs = NULL;
3392 static PangoLogAttr*
3393 compute_log_attrs (const GtkTextIter *iter,
3399 gint char_len, byte_len;
3400 PangoLogAttr *attrs = NULL;
3405 gtk_text_iter_set_line_offset (&start, 0);
3406 gtk_text_iter_forward_line (&end);
3408 paragraph = gtk_text_iter_get_slice (&start, &end);
3409 char_len = g_utf8_strlen (paragraph, -1);
3410 byte_len = strlen (paragraph);
3412 g_assert (char_len > 0);
3415 *char_lenp = char_len;
3417 attrs = g_new (PangoLogAttr, char_len + 1);
3419 /* FIXME we need to follow PangoLayout and allow different language
3420 * tags within the paragraph
3422 pango_get_log_attrs (paragraph, byte_len, -1,
3423 gtk_text_iter_get_language (&start),
3432 /* The return value from this is valid until you call this a second time.
3435 _gtk_text_buffer_get_line_log_attrs (GtkTextBuffer *buffer,
3436 const GtkTextIter *anywhere_in_line,
3440 GtkTextLogAttrCache *cache;
3443 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
3444 g_return_val_if_fail (anywhere_in_line != NULL, NULL);
3446 /* special-case for empty last line in buffer */
3447 if (gtk_text_iter_is_end (anywhere_in_line) &&
3448 gtk_text_iter_get_line_offset (anywhere_in_line) == 0)
3455 /* FIXME we also need to recompute log attrs if the language tag at
3456 * the start of a paragraph changes
3459 if (buffer->log_attr_cache == NULL)
3461 buffer->log_attr_cache = g_new0 (GtkTextLogAttrCache, 1);
3462 buffer->log_attr_cache->chars_changed_stamp =
3463 _gtk_text_btree_get_chars_changed_stamp (get_btree (buffer));
3465 else if (buffer->log_attr_cache->chars_changed_stamp !=
3466 _gtk_text_btree_get_chars_changed_stamp (get_btree (buffer)))
3468 clear_log_attr_cache (buffer->log_attr_cache);
3471 cache = buffer->log_attr_cache;
3472 line = gtk_text_iter_get_line (anywhere_in_line);
3475 while (i < ATTR_CACHE_SIZE)
3477 if (cache->entries[i].attrs &&
3478 cache->entries[i].line == line)
3481 *char_len = cache->entries[i].char_len;
3482 return cache->entries[i].attrs;
3487 /* Not in cache; open up the first cache entry */
3488 if (cache->entries[ATTR_CACHE_SIZE-1].attrs)
3489 g_free (cache->entries[ATTR_CACHE_SIZE-1].attrs);
3491 g_memmove (cache->entries + 1, cache->entries,
3492 sizeof (CacheEntry) * (ATTR_CACHE_SIZE - 1));
3494 cache->entries[0].line = line;
3495 cache->entries[0].attrs = compute_log_attrs (anywhere_in_line,
3496 &cache->entries[0].char_len);
3499 *char_len = cache->entries[0].char_len;
3501 return cache->entries[0].attrs;
3509 _gtk_text_buffer_spew (GtkTextBuffer *buffer)
3511 _gtk_text_btree_spew (get_btree (buffer));