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/.
34 #define GTK_TEXT_USE_INTERNAL_UNSUPPORTED_API
35 #include "gtkclipboard.h"
36 #include "gtkinvisible.h"
37 #include "gtkmarshalers.h"
38 #include "gtktextbuffer.h"
39 #include "gtktextbtree.h"
40 #include "gtktextiterprivate.h"
43 typedef struct _ClipboardRequest ClipboardRequest;
45 struct _ClipboardRequest
47 GtkTextBuffer *buffer;
49 gboolean default_editable;
50 gboolean is_clipboard;
51 gboolean replace_selection;
82 TARGET_TEXT_BUFFER_CONTENTS
85 static void gtk_text_buffer_init (GtkTextBuffer *tkxt_buffer);
86 static void gtk_text_buffer_class_init (GtkTextBufferClass *klass);
87 static void gtk_text_buffer_finalize (GObject *object);
90 static void gtk_text_buffer_real_insert_text (GtkTextBuffer *buffer,
94 static void gtk_text_buffer_real_insert_pixbuf (GtkTextBuffer *buffer,
97 static void gtk_text_buffer_real_insert_anchor (GtkTextBuffer *buffer,
99 GtkTextChildAnchor *anchor);
100 static void gtk_text_buffer_real_delete_range (GtkTextBuffer *buffer,
103 static void gtk_text_buffer_real_apply_tag (GtkTextBuffer *buffer,
105 const GtkTextIter *start_char,
106 const GtkTextIter *end_char);
107 static void gtk_text_buffer_real_remove_tag (GtkTextBuffer *buffer,
109 const GtkTextIter *start_char,
110 const GtkTextIter *end_char);
111 static void gtk_text_buffer_real_changed (GtkTextBuffer *buffer);
113 static GtkTextBTree* get_btree (GtkTextBuffer *buffer);
114 static void free_log_attr_cache (GtkTextLogAttrCache *cache);
116 static void remove_all_clipboard_contents_buffers (GtkTextBuffer *buffer);
117 static void remove_all_selection_clipboards (GtkTextBuffer *buffer);
118 static void update_selection_clipboards (GtkTextBuffer *buffer);
120 static GObjectClass *parent_class = NULL;
121 static guint signals[LAST_SIGNAL] = { 0 };
123 static void gtk_text_buffer_set_property (GObject *object,
127 static void gtk_text_buffer_get_property (GObject *object,
134 gtk_text_buffer_get_type (void)
136 static GType our_type = 0;
140 static const GTypeInfo our_info =
142 sizeof (GtkTextBufferClass),
143 (GBaseInitFunc) NULL,
144 (GBaseFinalizeFunc) NULL,
145 (GClassInitFunc) gtk_text_buffer_class_init,
146 NULL, /* class_finalize */
147 NULL, /* class_data */
148 sizeof (GtkTextBuffer),
150 (GInstanceInitFunc) gtk_text_buffer_init
153 our_type = g_type_register_static (G_TYPE_OBJECT, "GtkTextBuffer",
161 gtk_text_buffer_class_init (GtkTextBufferClass *klass)
163 GObjectClass *object_class = G_OBJECT_CLASS (klass);
165 parent_class = g_type_class_peek_parent (klass);
167 object_class->finalize = gtk_text_buffer_finalize;
168 object_class->set_property = gtk_text_buffer_set_property;
169 object_class->get_property = gtk_text_buffer_get_property;
171 klass->insert_text = gtk_text_buffer_real_insert_text;
172 klass->insert_pixbuf = gtk_text_buffer_real_insert_pixbuf;
173 klass->insert_child_anchor = gtk_text_buffer_real_insert_anchor;
174 klass->delete_range = gtk_text_buffer_real_delete_range;
175 klass->apply_tag = gtk_text_buffer_real_apply_tag;
176 klass->remove_tag = gtk_text_buffer_real_remove_tag;
177 klass->changed = gtk_text_buffer_real_changed;
180 g_object_class_install_property (object_class,
182 g_param_spec_object ("tag_table",
184 P_("Text Tag Table"),
185 GTK_TYPE_TEXT_TAG_TABLE,
186 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
188 signals[INSERT_TEXT] =
189 g_signal_new ("insert_text",
190 G_OBJECT_CLASS_TYPE (object_class),
192 G_STRUCT_OFFSET (GtkTextBufferClass, insert_text),
194 _gtk_marshal_VOID__BOXED_STRING_INT,
197 GTK_TYPE_TEXT_ITER | G_SIGNAL_TYPE_STATIC_SCOPE,
198 G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE,
201 signals[INSERT_PIXBUF] =
202 g_signal_new ("insert_pixbuf",
203 G_OBJECT_CLASS_TYPE (object_class),
205 G_STRUCT_OFFSET (GtkTextBufferClass, insert_pixbuf),
207 _gtk_marshal_VOID__BOXED_OBJECT,
210 GTK_TYPE_TEXT_ITER | G_SIGNAL_TYPE_STATIC_SCOPE,
213 signals[INSERT_CHILD_ANCHOR] =
214 g_signal_new ("insert_child_anchor",
215 G_OBJECT_CLASS_TYPE (object_class),
217 G_STRUCT_OFFSET (GtkTextBufferClass, insert_child_anchor),
219 _gtk_marshal_VOID__BOXED_OBJECT,
222 GTK_TYPE_TEXT_ITER | G_SIGNAL_TYPE_STATIC_SCOPE,
223 GTK_TYPE_TEXT_CHILD_ANCHOR);
226 * GtkTextBuffer::delete_range:
227 * @buffer: the object which received the signal.
228 * @start: the start of the range to be deleted
229 * @end: the end of the range to be deleted
231 * The ::delete_range signal is emitted to delete a range from
232 * a #GtkTextBuffer. Note that your handler must not invalidate the
233 * @start and @end iters (or has to revalidate them), if it runs before the
234 * default handler. There is no need to keep the iters valid in handlers
235 * which run after the default handler (see g_signal_connect_after()), but
236 * those don't have access to the deleted text.
238 signals[DELETE_RANGE] =
239 g_signal_new ("delete_range",
240 G_OBJECT_CLASS_TYPE (object_class),
242 G_STRUCT_OFFSET (GtkTextBufferClass, delete_range),
244 _gtk_marshal_VOID__BOXED_BOXED,
247 GTK_TYPE_TEXT_ITER | G_SIGNAL_TYPE_STATIC_SCOPE,
248 GTK_TYPE_TEXT_ITER | G_SIGNAL_TYPE_STATIC_SCOPE);
251 g_signal_new ("changed",
252 G_OBJECT_CLASS_TYPE (object_class),
254 G_STRUCT_OFFSET (GtkTextBufferClass, changed),
256 _gtk_marshal_VOID__VOID,
260 signals[MODIFIED_CHANGED] =
261 g_signal_new ("modified_changed",
262 G_OBJECT_CLASS_TYPE (object_class),
264 G_STRUCT_OFFSET (GtkTextBufferClass, modified_changed),
266 _gtk_marshal_VOID__VOID,
271 g_signal_new ("mark_set",
272 G_OBJECT_CLASS_TYPE (object_class),
274 G_STRUCT_OFFSET (GtkTextBufferClass, mark_set),
276 _gtk_marshal_VOID__BOXED_OBJECT,
282 signals[MARK_DELETED] =
283 g_signal_new ("mark_deleted",
284 G_OBJECT_CLASS_TYPE (object_class),
286 G_STRUCT_OFFSET (GtkTextBufferClass, mark_deleted),
288 _gtk_marshal_VOID__OBJECT,
294 g_signal_new ("apply_tag",
295 G_OBJECT_CLASS_TYPE (object_class),
297 G_STRUCT_OFFSET (GtkTextBufferClass, apply_tag),
299 _gtk_marshal_VOID__OBJECT_BOXED_BOXED,
306 signals[REMOVE_TAG] =
307 g_signal_new ("remove_tag",
308 G_OBJECT_CLASS_TYPE (object_class),
310 G_STRUCT_OFFSET (GtkTextBufferClass, remove_tag),
312 _gtk_marshal_VOID__OBJECT_BOXED_BOXED,
319 signals[BEGIN_USER_ACTION] =
320 g_signal_new ("begin_user_action",
321 G_OBJECT_CLASS_TYPE (object_class),
323 G_STRUCT_OFFSET (GtkTextBufferClass, begin_user_action),
325 _gtk_marshal_VOID__VOID,
329 signals[END_USER_ACTION] =
330 g_signal_new ("end_user_action",
331 G_OBJECT_CLASS_TYPE (object_class),
333 G_STRUCT_OFFSET (GtkTextBufferClass, end_user_action),
335 _gtk_marshal_VOID__VOID,
341 gtk_text_buffer_init (GtkTextBuffer *buffer)
343 buffer->clipboard_contents_buffers = NULL;
344 buffer->tag_table = NULL;
348 set_table (GtkTextBuffer *buffer, GtkTextTagTable *table)
350 g_return_if_fail (buffer->tag_table == NULL);
354 buffer->tag_table = table;
355 g_object_ref (buffer->tag_table);
356 _gtk_text_tag_table_add_buffer (table, buffer);
360 static GtkTextTagTable*
361 get_table (GtkTextBuffer *buffer)
363 if (buffer->tag_table == NULL)
365 buffer->tag_table = gtk_text_tag_table_new ();
366 _gtk_text_tag_table_add_buffer (buffer->tag_table, buffer);
369 return buffer->tag_table;
373 gtk_text_buffer_set_property (GObject *object,
378 GtkTextBuffer *text_buffer;
380 text_buffer = GTK_TEXT_BUFFER (object);
385 set_table (text_buffer, g_value_get_object (value));
394 gtk_text_buffer_get_property (GObject *object,
399 GtkTextBuffer *text_buffer;
401 text_buffer = GTK_TEXT_BUFFER (object);
406 g_value_set_object (value, get_table (text_buffer));
415 * gtk_text_buffer_new:
416 * @table: a tag table, or NULL to create a new one
418 * Creates a new text buffer.
420 * Return value: a new text buffer
423 gtk_text_buffer_new (GtkTextTagTable *table)
425 GtkTextBuffer *text_buffer;
427 text_buffer = g_object_new (GTK_TYPE_TEXT_BUFFER, "tag_table", table, NULL);
433 gtk_text_buffer_finalize (GObject *object)
435 GtkTextBuffer *buffer;
437 buffer = GTK_TEXT_BUFFER (object);
439 remove_all_clipboard_contents_buffers (buffer);
440 remove_all_selection_clipboards (buffer);
442 if (buffer->tag_table)
444 _gtk_text_tag_table_remove_buffer (buffer->tag_table, buffer);
445 g_object_unref (buffer->tag_table);
446 buffer->tag_table = NULL;
451 _gtk_text_btree_unref (buffer->btree);
452 buffer->btree = NULL;
455 if (buffer->log_attr_cache)
456 free_log_attr_cache (buffer->log_attr_cache);
458 buffer->log_attr_cache = NULL;
460 G_OBJECT_CLASS (parent_class)->finalize (object);
464 get_btree (GtkTextBuffer *buffer)
466 if (buffer->btree == NULL)
467 buffer->btree = _gtk_text_btree_new (gtk_text_buffer_get_tag_table (buffer),
470 return buffer->btree;
474 _gtk_text_buffer_get_btree (GtkTextBuffer *buffer)
476 return get_btree (buffer);
480 * gtk_text_buffer_get_tag_table:
481 * @buffer: a #GtkTextBuffer
483 * Get the #GtkTextTagTable associated with this buffer.
485 * Return value: the buffer's tag table
488 gtk_text_buffer_get_tag_table (GtkTextBuffer *buffer)
490 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
492 return get_table (buffer);
496 * gtk_text_buffer_set_text:
497 * @buffer: a #GtkTextBuffer
498 * @text: UTF-8 text to insert
499 * @len: length of @text in bytes
501 * Deletes current contents of @buffer, and inserts @text instead. If
502 * @len is -1, @text must be nul-terminated. @text must be valid UTF-8.
505 gtk_text_buffer_set_text (GtkTextBuffer *buffer,
509 GtkTextIter start, end;
511 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
512 g_return_if_fail (text != NULL);
517 gtk_text_buffer_get_bounds (buffer, &start, &end);
519 gtk_text_buffer_delete (buffer, &start, &end);
523 gtk_text_buffer_get_iter_at_offset (buffer, &start, 0);
524 gtk_text_buffer_insert (buffer, &start, text, len);
533 gtk_text_buffer_real_insert_text (GtkTextBuffer *buffer,
538 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
539 g_return_if_fail (iter != NULL);
541 _gtk_text_btree_insert (iter, text, len);
543 g_signal_emit (buffer, signals[CHANGED], 0);
547 gtk_text_buffer_emit_insert (GtkTextBuffer *buffer,
552 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
553 g_return_if_fail (iter != NULL);
554 g_return_if_fail (text != NULL);
559 g_return_if_fail (g_utf8_validate (text, len, NULL));
563 g_signal_emit (buffer, signals[INSERT_TEXT], 0,
569 * gtk_text_buffer_insert:
570 * @buffer: a #GtkTextBuffer
571 * @iter: a position in the buffer
572 * @text: UTF-8 format text to insert
573 * @len: length of text in bytes, or -1
575 * Inserts @len bytes of @text at position @iter. If @len is -1,
576 * @text must be nul-terminated and will be inserted in its
577 * entirety. Emits the "insert_text" signal; insertion actually occurs
578 * in the default handler for the signal. @iter is invalidated when
579 * insertion occurs (because the buffer contents change), but the
580 * default signal handler revalidates it to point to the end of the
585 gtk_text_buffer_insert (GtkTextBuffer *buffer,
590 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
591 g_return_if_fail (iter != NULL);
592 g_return_if_fail (text != NULL);
593 g_return_if_fail (gtk_text_iter_get_buffer (iter) == buffer);
595 gtk_text_buffer_emit_insert (buffer, iter, text, len);
599 * gtk_text_buffer_insert_at_cursor:
600 * @buffer: a #GtkTextBuffer
601 * @text: some text in UTF-8 format
602 * @len: length of text, in bytes
604 * Simply calls gtk_text_buffer_insert (), using the current
605 * cursor position as the insertion point.
608 gtk_text_buffer_insert_at_cursor (GtkTextBuffer *buffer,
614 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
615 g_return_if_fail (text != NULL);
617 gtk_text_buffer_get_iter_at_mark (buffer, &iter,
618 gtk_text_buffer_get_mark (buffer,
621 gtk_text_buffer_insert (buffer, &iter, text, len);
625 * gtk_text_buffer_insert_interactive:
626 * @buffer: a #GtkTextBuffer
627 * @iter: a position in @buffer
628 * @text: some UTF-8 text
629 * @len: length of text in bytes, or -1
630 * @default_editable: default editability of buffer
632 * Like gtk_text_buffer_insert (), but the insertion will not occur if
633 * @iter is at a non-editable location in the buffer. Usually you
634 * want to prevent insertions at ineditable locations if the insertion
635 * results from a user action (is interactive).
637 * @default_editable indicates the editability of text that doesn't
638 * have a tag affecting editability applied to it. Typically the
639 * result of gtk_text_view_get_editable() is appropriate here.
641 * Return value: whether text was actually inserted
644 gtk_text_buffer_insert_interactive (GtkTextBuffer *buffer,
648 gboolean default_editable)
650 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
651 g_return_val_if_fail (text != NULL, FALSE);
652 g_return_val_if_fail (gtk_text_iter_get_buffer (iter) == buffer, FALSE);
654 if (gtk_text_iter_can_insert (iter, default_editable))
656 gtk_text_buffer_begin_user_action (buffer);
657 gtk_text_buffer_emit_insert (buffer, iter, text, len);
658 gtk_text_buffer_end_user_action (buffer);
666 * gtk_text_buffer_insert_interactive_at_cursor:
667 * @buffer: a #GtkTextBuffer
668 * @text: text in UTF-8 format
669 * @len: length of text in bytes, or -1
670 * @default_editable: default editability of buffer
672 * Calls gtk_text_buffer_insert_interactive () at the cursor
675 * @default_editable indicates the editability of text that doesn't
676 * have a tag affecting editability applied to it. Typically the
677 * result of gtk_text_view_get_editable() is appropriate here.
679 * Return value: whether text was actually inserted
682 gtk_text_buffer_insert_interactive_at_cursor (GtkTextBuffer *buffer,
685 gboolean default_editable)
689 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
690 g_return_val_if_fail (text != NULL, FALSE);
692 gtk_text_buffer_get_iter_at_mark (buffer, &iter,
693 gtk_text_buffer_get_mark (buffer,
696 return gtk_text_buffer_insert_interactive (buffer, &iter, text, len,
701 possibly_not_text (gunichar ch,
704 return ch == GTK_TEXT_UNKNOWN_CHAR;
708 insert_text_range (GtkTextBuffer *buffer,
710 const GtkTextIter *orig_start,
711 const GtkTextIter *orig_end,
712 gboolean interactive)
716 text = gtk_text_iter_get_text (orig_start, orig_end);
718 gtk_text_buffer_emit_insert (buffer, iter, text, -1);
723 typedef struct _Range Range;
726 GtkTextBuffer *buffer;
727 GtkTextMark *start_mark;
728 GtkTextMark *end_mark;
729 GtkTextMark *whole_end_mark;
730 GtkTextIter *range_start;
731 GtkTextIter *range_end;
732 GtkTextIter *whole_end;
736 save_range (GtkTextIter *range_start,
737 GtkTextIter *range_end,
738 GtkTextIter *whole_end)
742 r = g_new (Range, 1);
744 r->buffer = gtk_text_iter_get_buffer (range_start);
745 g_object_ref (r->buffer);
748 gtk_text_buffer_create_mark (gtk_text_iter_get_buffer (range_start),
753 gtk_text_buffer_create_mark (gtk_text_iter_get_buffer (range_start),
759 gtk_text_buffer_create_mark (gtk_text_iter_get_buffer (range_start),
764 r->range_start = range_start;
765 r->range_end = range_end;
766 r->whole_end = whole_end;
772 restore_range (Range *r)
774 gtk_text_buffer_get_iter_at_mark (r->buffer,
778 gtk_text_buffer_get_iter_at_mark (r->buffer,
782 gtk_text_buffer_get_iter_at_mark (r->buffer,
786 gtk_text_buffer_delete_mark (r->buffer, r->start_mark);
787 gtk_text_buffer_delete_mark (r->buffer, r->end_mark);
788 gtk_text_buffer_delete_mark (r->buffer, r->whole_end_mark);
790 /* Due to the gravities on the marks, the ordering could have
791 * gotten mangled; we switch to an empty range in that
795 if (gtk_text_iter_compare (r->range_start, r->range_end) > 0)
796 *r->range_start = *r->range_end;
798 if (gtk_text_iter_compare (r->range_end, r->whole_end) > 0)
799 *r->range_end = *r->whole_end;
801 g_object_unref (r->buffer);
806 insert_range_untagged (GtkTextBuffer *buffer,
808 const GtkTextIter *orig_start,
809 const GtkTextIter *orig_end,
810 gboolean interactive)
812 GtkTextIter range_start;
813 GtkTextIter range_end;
814 GtkTextIter start, end;
815 GtkTextBuffer *src_buffer;
818 if (gtk_text_iter_equal (orig_start, orig_end))
824 src_buffer = gtk_text_iter_get_buffer (&start);
831 if (gtk_text_iter_equal (&range_start, &range_end))
833 /* Figure out how to move forward */
835 g_assert (gtk_text_iter_compare (&range_end, &end) <= 0);
837 if (gtk_text_iter_equal (&range_end, &end))
839 /* nothing left to do */
842 else if (gtk_text_iter_get_char (&range_end) == GTK_TEXT_UNKNOWN_CHAR)
844 GdkPixbuf *pixbuf = NULL;
845 GtkTextChildAnchor *anchor = NULL;
846 pixbuf = gtk_text_iter_get_pixbuf (&range_end);
847 anchor = gtk_text_iter_get_child_anchor (&range_end);
851 r = save_range (&range_start,
855 gtk_text_buffer_insert_pixbuf (buffer,
862 gtk_text_iter_forward_char (&range_end);
864 range_start = range_end;
868 /* Just skip anchors */
870 gtk_text_iter_forward_char (&range_end);
871 range_start = range_end;
875 /* The GTK_TEXT_UNKNOWN_CHAR was in a text segment, so
878 gtk_text_iter_forward_find_char (&range_end,
879 possibly_not_text, NULL,
882 g_assert (gtk_text_iter_compare (&range_end, &end) <= 0);
887 /* Text segment starts here, so forward search to
888 * find its possible endpoint
890 gtk_text_iter_forward_find_char (&range_end,
891 possibly_not_text, NULL,
894 g_assert (gtk_text_iter_compare (&range_end, &end) <= 0);
899 r = save_range (&range_start,
903 insert_text_range (buffer,
912 range_start = range_end;
918 insert_range_not_inside_self (GtkTextBuffer *buffer,
920 const GtkTextIter *orig_start,
921 const GtkTextIter *orig_end,
922 gboolean interactive)
924 /* Find each range of uniformly-tagged text, insert it,
925 * then apply the tags.
927 GtkTextIter start = *orig_start;
928 GtkTextIter end = *orig_end;
929 GtkTextIter range_start;
930 GtkTextIter range_end;
931 GtkTextBuffer *src_buffer;
933 if (gtk_text_iter_equal (orig_start, orig_end))
936 src_buffer = gtk_text_iter_get_buffer (orig_start);
938 gtk_text_iter_order (&start, &end);
946 GtkTextIter start_iter;
951 if (gtk_text_iter_equal (&range_start, &end))
952 break; /* All done */
954 g_assert (gtk_text_iter_compare (&range_start, &end) < 0);
956 gtk_text_iter_forward_to_tag_toggle (&range_end, NULL);
958 g_assert (!gtk_text_iter_equal (&range_start, &range_end));
960 /* Clamp to the end iterator */
961 if (gtk_text_iter_compare (&range_end, &end) > 0)
964 /* We have a range with unique tags; insert it, and
967 start_offset = gtk_text_iter_get_offset (iter);
969 r = save_range (&range_start, &range_end, &end);
971 insert_range_untagged (buffer, iter, &range_start, &range_end, interactive);
976 gtk_text_buffer_get_iter_at_offset (buffer, &start_iter, start_offset);
978 tags = gtk_text_iter_get_tags (&range_start);
980 while (tmp_list != NULL)
982 gtk_text_buffer_apply_tag (buffer,
987 tmp_list = g_slist_next (tmp_list);
991 range_start = range_end;
996 gtk_text_buffer_real_insert_range (GtkTextBuffer *buffer,
998 const GtkTextIter *orig_start,
999 const GtkTextIter *orig_end,
1000 gboolean interactive)
1002 GtkTextBuffer *src_buffer;
1004 /* Find each range of uniformly-tagged text, insert it,
1005 * then apply the tags.
1007 if (gtk_text_iter_equal (orig_start, orig_end))
1011 gtk_text_buffer_begin_user_action (buffer);
1013 src_buffer = gtk_text_iter_get_buffer (orig_start);
1015 if (gtk_text_iter_get_buffer (iter) != src_buffer ||
1016 !gtk_text_iter_in_range (iter, orig_start, orig_end))
1018 insert_range_not_inside_self (buffer, iter, orig_start, orig_end, interactive);
1022 /* If you insert a range into itself, it could loop infinitely
1023 * because the region being copied keeps growing as we insert. So
1024 * we have to separately copy the range before and after
1025 * the insertion point.
1027 GtkTextIter start = *orig_start;
1028 GtkTextIter end = *orig_end;
1029 GtkTextIter range_start;
1030 GtkTextIter range_end;
1034 gtk_text_iter_order (&start, &end);
1036 range_start = start;
1038 first_half = save_range (&range_start, &range_end, &end);
1040 range_start = *iter;
1042 second_half = save_range (&range_start, &range_end, &end);
1044 restore_range (first_half);
1045 insert_range_not_inside_self (buffer, iter, &range_start, &range_end, interactive);
1047 restore_range (second_half);
1048 insert_range_not_inside_self (buffer, iter, &range_start, &range_end, interactive);
1052 gtk_text_buffer_end_user_action (buffer);
1056 * gtk_text_buffer_insert_range:
1057 * @buffer: a #GtkTextBuffer
1058 * @iter: a position in @buffer
1059 * @start: a position in a #GtkTextBuffer
1060 * @end: another position in the same buffer as @start
1062 * Copies text, tags, and pixbufs between @start and @end (the order
1063 * of @start and @end doesn't matter) and inserts the copy at @iter.
1064 * Used instead of simply getting/inserting text because it preserves
1065 * images and tags. If @start and @end are in a different buffer from
1066 * @buffer, the two buffers must share the same tag table.
1068 * Implemented via emissions of the insert_text and apply_tag signals,
1072 gtk_text_buffer_insert_range (GtkTextBuffer *buffer,
1074 const GtkTextIter *start,
1075 const GtkTextIter *end)
1077 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1078 g_return_if_fail (iter != NULL);
1079 g_return_if_fail (start != NULL);
1080 g_return_if_fail (end != NULL);
1081 g_return_if_fail (gtk_text_iter_get_buffer (start) ==
1082 gtk_text_iter_get_buffer (end));
1083 g_return_if_fail (gtk_text_iter_get_buffer (start)->tag_table ==
1085 g_return_if_fail (gtk_text_iter_get_buffer (iter) == buffer);
1087 gtk_text_buffer_real_insert_range (buffer, iter, start, end, FALSE);
1091 * gtk_text_buffer_insert_range_interactive:
1092 * @buffer: a #GtkTextBuffer
1093 * @iter: a position in @buffer
1094 * @start: a position in a #GtkTextBuffer
1095 * @end: another position in the same buffer as @start
1096 * @default_editable: default editability of the buffer
1098 * Same as gtk_text_buffer_insert_range(), but does nothing if the
1099 * insertion point isn't editable. The @default_editable parameter
1100 * indicates whether the text is editable at @iter if no tags
1101 * enclosing @iter affect editability. Typically the result of
1102 * gtk_text_view_get_editable() is appropriate here.
1104 * Returns: whether an insertion was possible at @iter
1107 gtk_text_buffer_insert_range_interactive (GtkTextBuffer *buffer,
1109 const GtkTextIter *start,
1110 const GtkTextIter *end,
1111 gboolean default_editable)
1113 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
1114 g_return_val_if_fail (iter != NULL, FALSE);
1115 g_return_val_if_fail (start != NULL, FALSE);
1116 g_return_val_if_fail (end != NULL, FALSE);
1117 g_return_val_if_fail (gtk_text_iter_get_buffer (start) ==
1118 gtk_text_iter_get_buffer (end), FALSE);
1119 g_return_val_if_fail (gtk_text_iter_get_buffer (start)->tag_table ==
1120 buffer->tag_table, FALSE);
1123 if (gtk_text_iter_can_insert (iter, default_editable))
1125 gtk_text_buffer_real_insert_range (buffer, iter, start, end, TRUE);
1133 * gtk_text_buffer_insert_with_tags:
1134 * @buffer: a #GtkTextBuffer
1135 * @iter: an iterator in @buffer
1137 * @len: length of @text, or -1
1138 * @first_tag: first tag to apply to @text
1139 * @Varargs: NULL-terminated list of tags to apply
1141 * Inserts @text into @buffer at @iter, applying the list of tags to
1142 * the newly-inserted text. The last tag specified must be NULL to
1143 * terminate the list. Equivalent to calling gtk_text_buffer_insert (),
1144 * then gtk_text_buffer_apply_tag () on the inserted text;
1145 * gtk_text_buffer_insert_with_tags () is just a convenience function.
1148 gtk_text_buffer_insert_with_tags (GtkTextBuffer *buffer,
1152 GtkTextTag *first_tag,
1160 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1161 g_return_if_fail (iter != NULL);
1162 g_return_if_fail (text != NULL);
1163 g_return_if_fail (gtk_text_iter_get_buffer (iter) == buffer);
1165 start_offset = gtk_text_iter_get_offset (iter);
1167 gtk_text_buffer_insert (buffer, iter, text, len);
1169 if (first_tag == NULL)
1172 gtk_text_buffer_get_iter_at_offset (buffer, &start, start_offset);
1174 va_start (args, first_tag);
1178 gtk_text_buffer_apply_tag (buffer, tag, &start, iter);
1180 tag = va_arg (args, GtkTextTag*);
1187 * gtk_text_buffer_insert_with_tags_by_name:
1188 * @buffer: a #GtkTextBuffer
1189 * @iter: position in @buffer
1191 * @len: length of @text, or -1
1192 * @first_tag_name: name of a tag to apply to @text
1193 * @Varargs: more tag names
1195 * Same as gtk_text_buffer_insert_with_tags (), but allows you
1196 * to pass in tag names instead of tag objects.
1199 gtk_text_buffer_insert_with_tags_by_name (GtkTextBuffer *buffer,
1203 const gchar *first_tag_name,
1209 const gchar *tag_name;
1211 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1212 g_return_if_fail (iter != NULL);
1213 g_return_if_fail (text != NULL);
1214 g_return_if_fail (gtk_text_iter_get_buffer (iter) == buffer);
1216 start_offset = gtk_text_iter_get_offset (iter);
1218 gtk_text_buffer_insert (buffer, iter, text, len);
1220 if (first_tag_name == NULL)
1223 gtk_text_buffer_get_iter_at_offset (buffer, &start, start_offset);
1225 va_start (args, first_tag_name);
1226 tag_name = first_tag_name;
1231 tag = gtk_text_tag_table_lookup (buffer->tag_table,
1236 g_warning ("%s: no tag with name '%s'!", G_STRLOC, tag_name);
1240 gtk_text_buffer_apply_tag (buffer, tag, &start, iter);
1242 tag_name = va_arg (args, const gchar*);
1254 gtk_text_buffer_real_delete_range (GtkTextBuffer *buffer,
1258 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1259 g_return_if_fail (start != NULL);
1260 g_return_if_fail (end != NULL);
1262 _gtk_text_btree_delete (start, end);
1264 /* may have deleted the selection... */
1265 update_selection_clipboards (buffer);
1267 g_signal_emit (buffer, signals[CHANGED], 0);
1271 gtk_text_buffer_emit_delete (GtkTextBuffer *buffer,
1275 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1276 g_return_if_fail (start != NULL);
1277 g_return_if_fail (end != NULL);
1279 if (gtk_text_iter_equal (start, end))
1282 gtk_text_iter_order (start, end);
1284 g_signal_emit (buffer,
1285 signals[DELETE_RANGE],
1291 * gtk_text_buffer_delete:
1292 * @buffer: a #GtkTextBuffer
1293 * @start: a position in @buffer
1294 * @end: another position in @buffer
1296 * Deletes text between @start and @end. The order of @start and @end
1297 * is not actually relevant; gtk_text_buffer_delete () will reorder
1298 * them. This function actually emits the "delete_range" signal, and
1299 * the default handler of that signal deletes the text. Because the
1300 * buffer is modified, all outstanding iterators become invalid after
1301 * calling this function; however, the @start and @end will be
1302 * re-initialized to point to the location where text was deleted.
1306 gtk_text_buffer_delete (GtkTextBuffer *buffer,
1310 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1311 g_return_if_fail (start != NULL);
1312 g_return_if_fail (end != NULL);
1313 g_return_if_fail (gtk_text_iter_get_buffer (start) == buffer);
1314 g_return_if_fail (gtk_text_iter_get_buffer (end) == buffer);
1316 gtk_text_buffer_emit_delete (buffer, start, end);
1320 * gtk_text_buffer_delete_interactive:
1321 * @buffer: a #GtkTextBuffer
1322 * @start_iter: start of range to delete
1323 * @end_iter: end of range
1324 * @default_editable: whether the buffer is editable by default
1326 * Deletes all <emphasis>editable</emphasis> text in the given range.
1327 * Calls gtk_text_buffer_delete () for each editable sub-range of
1328 * [@start,@end). @start and @end are revalidated to point to
1329 * the location of the last deleted range, or left untouched if
1330 * no text was deleted.
1332 * Return value: whether some text was actually deleted
1335 gtk_text_buffer_delete_interactive (GtkTextBuffer *buffer,
1336 GtkTextIter *start_iter,
1337 GtkTextIter *end_iter,
1338 gboolean default_editable)
1340 GtkTextMark *end_mark;
1341 GtkTextMark *start_mark;
1343 gboolean current_state;
1344 gboolean deleted_stuff = FALSE;
1346 /* Delete all editable text in the range start_iter, end_iter */
1348 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
1349 g_return_val_if_fail (start_iter != NULL, FALSE);
1350 g_return_val_if_fail (end_iter != NULL, FALSE);
1351 g_return_val_if_fail (gtk_text_iter_get_buffer (start_iter) == buffer, FALSE);
1352 g_return_val_if_fail (gtk_text_iter_get_buffer (end_iter) == buffer, FALSE);
1355 gtk_text_buffer_begin_user_action (buffer);
1357 gtk_text_iter_order (start_iter, end_iter);
1359 start_mark = gtk_text_buffer_create_mark (buffer, NULL,
1361 end_mark = gtk_text_buffer_create_mark (buffer, NULL,
1364 gtk_text_buffer_get_iter_at_mark (buffer, &iter, start_mark);
1366 current_state = gtk_text_iter_editable (&iter, default_editable);
1371 gboolean done = FALSE;
1374 gtk_text_iter_forward_to_tag_toggle (&iter, NULL);
1376 gtk_text_buffer_get_iter_at_mark (buffer, &end, end_mark);
1378 if (gtk_text_iter_compare (&iter, &end) >= 0)
1381 iter = end; /* clamp to the last boundary */
1384 new_state = gtk_text_iter_editable (&iter, default_editable);
1386 if (current_state == new_state)
1392 /* We're ending an editable region. Delete said region. */
1395 gtk_text_buffer_get_iter_at_mark (buffer, &start, start_mark);
1397 gtk_text_buffer_emit_delete (buffer, &start, &iter);
1399 deleted_stuff = TRUE;
1401 /* revalidate user's iterators. */
1402 *start_iter = start;
1412 if (current_state && !new_state)
1414 /* End of an editable region. Delete it. */
1417 gtk_text_buffer_get_iter_at_mark (buffer, &start, start_mark);
1419 gtk_text_buffer_emit_delete (buffer, &start, &iter);
1421 current_state = FALSE;
1422 deleted_stuff = TRUE;
1424 /* revalidate user's iterators. */
1425 *start_iter = start;
1430 /* We are at the start of an editable region. We won't be deleting
1431 * the previous region. Move start mark to start of this region.
1434 g_assert (!current_state && new_state);
1436 gtk_text_buffer_move_mark (buffer, start_mark,
1440 current_state = TRUE;
1448 gtk_text_buffer_delete_mark (buffer, start_mark);
1449 gtk_text_buffer_delete_mark (buffer, end_mark);
1451 gtk_text_buffer_end_user_action (buffer);
1453 return deleted_stuff;
1457 * Extracting textual buffer contents
1461 * gtk_text_buffer_get_text:
1462 * @buffer: a #GtkTextBuffer
1463 * @start: start of a range
1464 * @end: end of a range
1465 * @include_hidden_chars: whether to include invisible text
1467 * Returns the text in the range [@start,@end). Excludes undisplayed
1468 * text (text marked with tags that set the invisibility attribute) if
1469 * @include_hidden_chars is FALSE. Does not include characters
1470 * representing embedded images, so byte and character indexes into
1471 * the returned string do <emphasis>not</emphasis> correspond to byte
1472 * and character indexes into the buffer. Contrast with
1473 * gtk_text_buffer_get_slice ().
1475 * Return value: an allocated UTF-8 string
1478 gtk_text_buffer_get_text (GtkTextBuffer *buffer,
1479 const GtkTextIter *start,
1480 const GtkTextIter *end,
1481 gboolean include_hidden_chars)
1483 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
1484 g_return_val_if_fail (start != NULL, NULL);
1485 g_return_val_if_fail (end != NULL, NULL);
1486 g_return_val_if_fail (gtk_text_iter_get_buffer (start) == buffer, NULL);
1487 g_return_val_if_fail (gtk_text_iter_get_buffer (end) == buffer, NULL);
1489 if (include_hidden_chars)
1490 return gtk_text_iter_get_text (start, end);
1492 return gtk_text_iter_get_visible_text (start, end);
1496 * gtk_text_buffer_get_slice:
1497 * @buffer: a #GtkTextBuffer
1498 * @start: start of a range
1499 * @end: end of a range
1500 * @include_hidden_chars: whether to include invisible text
1502 * Returns the text in the range [@start,@end). Excludes undisplayed
1503 * text (text marked with tags that set the invisibility attribute) if
1504 * @include_hidden_chars is FALSE. The returned string includes a
1505 * 0xFFFC character whenever the buffer contains
1506 * embedded images, so byte and character indexes into
1507 * the returned string <emphasis>do</emphasis> correspond to byte
1508 * and character indexes into the buffer. Contrast with
1509 * gtk_text_buffer_get_text (). Note that 0xFFFC can occur in normal
1510 * text as well, so it is not a reliable indicator that a pixbuf or
1511 * widget is in the buffer.
1513 * Return value: an allocated UTF-8 string
1516 gtk_text_buffer_get_slice (GtkTextBuffer *buffer,
1517 const GtkTextIter *start,
1518 const GtkTextIter *end,
1519 gboolean include_hidden_chars)
1521 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
1522 g_return_val_if_fail (start != NULL, NULL);
1523 g_return_val_if_fail (end != NULL, NULL);
1524 g_return_val_if_fail (gtk_text_iter_get_buffer (start) == buffer, NULL);
1525 g_return_val_if_fail (gtk_text_iter_get_buffer (end) == buffer, NULL);
1527 if (include_hidden_chars)
1528 return gtk_text_iter_get_slice (start, end);
1530 return gtk_text_iter_get_visible_slice (start, end);
1538 gtk_text_buffer_real_insert_pixbuf (GtkTextBuffer *buffer,
1542 _gtk_text_btree_insert_pixbuf (iter, pixbuf);
1544 g_signal_emit (buffer, signals[CHANGED], 0);
1548 * gtk_text_buffer_insert_pixbuf:
1549 * @buffer: a #GtkTextBuffer
1550 * @iter: location to insert the pixbuf
1551 * @pixbuf: a #GdkPixbuf
1553 * Inserts an image into the text buffer at @iter. The image will be
1554 * counted as one character in character counts, and when obtaining
1555 * the buffer contents as a string, will be represented by the Unicode
1556 * "object replacement character" 0xFFFC. Note that the "slice"
1557 * variants for obtaining portions of the buffer as a string include
1558 * this character for pixbufs, but the "text" variants do
1559 * not. e.g. see gtk_text_buffer_get_slice() and
1560 * gtk_text_buffer_get_text().
1564 gtk_text_buffer_insert_pixbuf (GtkTextBuffer *buffer,
1568 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1569 g_return_if_fail (iter != NULL);
1570 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
1571 g_return_if_fail (gtk_text_iter_get_buffer (iter) == buffer);
1573 g_signal_emit (buffer, signals[INSERT_PIXBUF], 0,
1583 gtk_text_buffer_real_insert_anchor (GtkTextBuffer *buffer,
1585 GtkTextChildAnchor *anchor)
1587 _gtk_text_btree_insert_child_anchor (iter, anchor);
1589 g_signal_emit (buffer, signals[CHANGED], 0);
1593 * gtk_text_buffer_insert_child_anchor:
1594 * @buffer: a #GtkTextBuffer
1595 * @iter: location to insert the anchor
1596 * @anchor: a #GtkTextChildAnchor
1598 * Inserts a child widget anchor into the text buffer at @iter. The
1599 * anchor will be counted as one character in character counts, and
1600 * when obtaining the buffer contents as a string, will be represented
1601 * by the Unicode "object replacement character" 0xFFFC. Note that the
1602 * "slice" variants for obtaining portions of the buffer as a string
1603 * include this character for child anchors, but the "text" variants do
1604 * not. e.g. see gtk_text_buffer_get_slice() and
1605 * gtk_text_buffer_get_text(). Consider
1606 * gtk_text_buffer_create_child_anchor() as a more convenient
1607 * alternative to this function. The buffer will add a reference to
1608 * the anchor, so you can unref it after insertion.
1612 gtk_text_buffer_insert_child_anchor (GtkTextBuffer *buffer,
1614 GtkTextChildAnchor *anchor)
1616 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1617 g_return_if_fail (iter != NULL);
1618 g_return_if_fail (GTK_IS_TEXT_CHILD_ANCHOR (anchor));
1619 g_return_if_fail (gtk_text_iter_get_buffer (iter) == buffer);
1621 g_signal_emit (buffer, signals[INSERT_CHILD_ANCHOR], 0,
1627 * gtk_text_buffer_create_child_anchor:
1628 * @buffer: a #GtkTextBuffer
1629 * @iter: location in the buffer
1631 * This is a convenience function which simply creates a child anchor
1632 * with gtk_text_child_anchor_new() and inserts it into the buffer
1633 * with gtk_text_buffer_insert_child_anchor(). The new anchor is
1634 * owned by the buffer; no reference count is returned to
1635 * the caller of gtk_text_buffer_create_child_anchor().
1637 * Return value: the created child anchor
1640 gtk_text_buffer_create_child_anchor (GtkTextBuffer *buffer,
1643 GtkTextChildAnchor *anchor;
1645 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
1646 g_return_val_if_fail (iter != NULL, NULL);
1647 g_return_val_if_fail (gtk_text_iter_get_buffer (iter) == buffer, NULL);
1649 anchor = gtk_text_child_anchor_new ();
1651 gtk_text_buffer_insert_child_anchor (buffer, iter, anchor);
1653 g_object_unref (anchor);
1663 gtk_text_buffer_mark_set (GtkTextBuffer *buffer,
1664 const GtkTextIter *location,
1667 /* IMO this should NOT work like insert_text and delete_range,
1668 * where the real action happens in the default handler.
1670 * The reason is that the default handler would be _required_,
1671 * i.e. the whole widget would start breaking and segfaulting if the
1672 * default handler didn't get run. So you can't really override the
1673 * default handler or stop the emission; that is, this signal is
1674 * purely for notification, and not to allow users to modify the
1678 g_object_ref (mark);
1680 g_signal_emit (buffer,
1686 g_object_unref (mark);
1690 * gtk_text_buffer_set_mark:
1691 * @buffer: a #GtkTextBuffer
1692 * @mark_name: name of the mark
1693 * @iter: location for the mark.
1694 * @left_gravity: if the mark is created by this function, gravity for the new
1696 * @should_exist: if %TRUE, warn if the mark does not exist, and return
1699 * Move the mark to the given position, if not @should_exist, create the mark.
1701 * Return value: mark
1704 gtk_text_buffer_set_mark (GtkTextBuffer *buffer,
1705 GtkTextMark *existing_mark,
1706 const gchar *mark_name,
1707 const GtkTextIter *iter,
1708 gboolean left_gravity,
1709 gboolean should_exist)
1711 GtkTextIter location;
1714 g_return_val_if_fail (gtk_text_iter_get_buffer (iter) == buffer, NULL);
1716 mark = _gtk_text_btree_set_mark (get_btree (buffer),
1723 if (_gtk_text_btree_mark_is_insert (get_btree (buffer), mark) ||
1724 _gtk_text_btree_mark_is_selection_bound (get_btree (buffer), mark))
1726 update_selection_clipboards (buffer);
1729 _gtk_text_btree_get_iter_at_mark (get_btree (buffer),
1733 gtk_text_buffer_mark_set (buffer, &location, mark);
1739 * gtk_text_buffer_create_mark:
1740 * @buffer: a #GtkTextBuffer
1741 * @mark_name: name for mark, or %NULL
1742 * @where: location to place mark
1743 * @left_gravity: whether the mark has left gravity
1745 * Creates a mark at position @where. If @mark_name is %NULL, the mark
1746 * is anonymous; otherwise, the mark can be retrieved by name using
1747 * gtk_text_buffer_get_mark (). If a mark has left gravity, and text is
1748 * inserted at the mark's current location, the mark will be moved to
1749 * the left of the newly-inserted text. If the mark has right gravity
1750 * (@left_gravity = %FALSE), the mark will end up on the right of
1751 * newly-inserted text. The standard left-to-right cursor is a mark
1752 * with right gravity (when you type, the cursor stays on the right
1753 * side of the text you're typing).
1755 * The caller of this function does <emphasis>not</emphasis> own a reference
1756 * to the returned #GtkTextMark, so you can ignore the return value
1757 * if you like. Marks are owned by the buffer and go away when the
1760 * Emits the "mark_set" signal as notification of the mark's initial
1763 * Return value: the new #GtkTextMark object
1766 gtk_text_buffer_create_mark (GtkTextBuffer *buffer,
1767 const gchar *mark_name,
1768 const GtkTextIter *where,
1769 gboolean left_gravity)
1771 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
1773 return gtk_text_buffer_set_mark (buffer, NULL, mark_name, where,
1774 left_gravity, FALSE);
1778 * gtk_text_buffer_move_mark:
1779 * @buffer: a #GtkTextBuffer
1780 * @mark: a #GtkTextMark
1781 * @where: new location for @mark in @buffer
1783 * Moves @mark to the new location @where. Emits the "mark_set" signal
1784 * as notification of the move.
1787 gtk_text_buffer_move_mark (GtkTextBuffer *buffer,
1789 const GtkTextIter *where)
1791 g_return_if_fail (GTK_IS_TEXT_MARK (mark));
1792 g_return_if_fail (!gtk_text_mark_get_deleted (mark));
1793 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1795 gtk_text_buffer_set_mark (buffer, mark, NULL, where, FALSE, TRUE);
1799 * gtk_text_buffer_get_iter_at_mark:
1800 * @buffer: a #GtkTextBuffer
1801 * @iter: iterator to initialize
1802 * @mark: a #GtkTextMark in @buffer
1804 * Initializes @iter with the current position of @mark.
1807 gtk_text_buffer_get_iter_at_mark (GtkTextBuffer *buffer,
1811 g_return_if_fail (GTK_IS_TEXT_MARK (mark));
1812 g_return_if_fail (!gtk_text_mark_get_deleted (mark));
1813 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1815 _gtk_text_btree_get_iter_at_mark (get_btree (buffer),
1821 * gtk_text_buffer_delete_mark:
1822 * @buffer: a #GtkTextBuffer
1823 * @mark: a #GtkTextMark in @buffer
1825 * Deletes @mark, so that it's no longer located anywhere in the
1826 * buffer. Removes the reference the buffer holds to the mark, so if
1827 * you haven't called g_object_ref () on the mark, it will be freed. Even
1828 * if the mark isn't freed, most operations on @mark become
1829 * invalid. There is no way to undelete a
1830 * mark. gtk_text_mark_get_deleted () will return TRUE after this
1831 * function has been called on a mark; gtk_text_mark_get_deleted ()
1832 * indicates that a mark no longer belongs to a buffer. The "mark_deleted"
1833 * signal will be emitted as notification after the mark is deleted.
1836 gtk_text_buffer_delete_mark (GtkTextBuffer *buffer,
1839 g_return_if_fail (GTK_IS_TEXT_MARK (mark));
1840 g_return_if_fail (!gtk_text_mark_get_deleted (mark));
1841 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1843 g_object_ref (mark);
1845 _gtk_text_btree_remove_mark (get_btree (buffer), mark);
1847 /* See rationale above for MARK_SET on why we emit this after
1848 * removing the mark, rather than removing the mark in a default
1851 g_signal_emit (buffer, signals[MARK_DELETED],
1855 g_object_unref (mark);
1859 * gtk_text_buffer_get_mark:
1860 * @buffer: a #GtkTextBuffer
1861 * @name: a mark name
1863 * Returns the mark named @name in buffer @buffer, or NULL if no such
1864 * mark exists in the buffer.
1866 * Return value: a #GtkTextMark, or NULL
1869 gtk_text_buffer_get_mark (GtkTextBuffer *buffer,
1874 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
1875 g_return_val_if_fail (name != NULL, NULL);
1877 mark = _gtk_text_btree_get_mark_by_name (get_btree (buffer), name);
1884 * gtk_text_buffer_move_mark_by_name:
1885 * @buffer: a #GtkTextBuffer
1886 * @name: name of a mark
1887 * @where: new location for mark
1889 * Moves the mark named @name (which must exist) to location @where.
1890 * See gtk_text_buffer_move_mark () for details.
1893 gtk_text_buffer_move_mark_by_name (GtkTextBuffer *buffer,
1895 const GtkTextIter *where)
1899 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1900 g_return_if_fail (name != NULL);
1902 mark = _gtk_text_btree_get_mark_by_name (get_btree (buffer), name);
1906 g_warning ("%s: no mark named '%s'", G_STRLOC, name);
1910 gtk_text_buffer_move_mark (buffer, mark, where);
1914 * gtk_text_buffer_delete_mark_by_name:
1915 * @buffer: a #GtkTextBuffer
1916 * @name: name of a mark in @buffer
1918 * Deletes the mark named @name; the mark must exist. See
1919 * gtk_text_buffer_delete_mark () for details.
1922 gtk_text_buffer_delete_mark_by_name (GtkTextBuffer *buffer,
1927 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1928 g_return_if_fail (name != NULL);
1930 mark = _gtk_text_btree_get_mark_by_name (get_btree (buffer), name);
1934 g_warning ("%s: no mark named '%s'", G_STRLOC, name);
1938 gtk_text_buffer_delete_mark (buffer, mark);
1942 * gtk_text_buffer_get_insert:
1943 * @buffer: a #GtkTextBuffer
1945 * Returns the mark that represents the cursor (insertion point).
1946 * Equivalent to calling gtk_text_buffer_get_mark () to get the mark
1947 * named "insert", but very slightly more efficient, and involves less
1950 * Return value: insertion point mark
1953 gtk_text_buffer_get_insert (GtkTextBuffer *buffer)
1955 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
1957 /* FIXME use struct member in btree */
1958 return gtk_text_buffer_get_mark (buffer, "insert");
1962 * gtk_text_buffer_get_selection_bound:
1963 * @buffer: a #GtkTextBuffer
1965 * Returns the mark that represents the selection bound. Equivalent
1966 * to calling gtk_text_buffer_get_mark () to get the mark named
1967 * "selection_bound", but very slightly more efficient, and involves
1970 * The currently-selected text in @buffer is the region between the
1971 * "selection_bound" and "insert" marks. If "selection_bound" and
1972 * "insert" are in the same place, then there is no current selection.
1973 * gtk_text_buffer_get_selection_bounds() is another convenient function
1974 * for handling the selection, if you just want to know whether there's a
1975 * selection and what its bounds are.
1977 * Return value: selection bound mark
1980 gtk_text_buffer_get_selection_bound (GtkTextBuffer *buffer)
1982 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
1984 /* FIXME use struct member in btree */
1985 return gtk_text_buffer_get_mark (buffer, "selection_bound");
1989 * gtk_text_buffer_get_iter_at_child_anchor:
1990 * @buffer: a #GtkTextBuffer
1991 * @iter: an iterator to be initialized
1992 * @anchor: a child anchor that appears in @buffer
1994 * Obtains the location of @anchor within @buffer.
1998 gtk_text_buffer_get_iter_at_child_anchor (GtkTextBuffer *buffer,
2000 GtkTextChildAnchor *anchor)
2002 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2003 g_return_if_fail (iter != NULL);
2004 g_return_if_fail (GTK_IS_TEXT_CHILD_ANCHOR (anchor));
2005 g_return_if_fail (!gtk_text_child_anchor_get_deleted (anchor));
2007 _gtk_text_btree_get_iter_at_child_anchor (get_btree (buffer),
2013 * gtk_text_buffer_place_cursor:
2014 * @buffer: a #GtkTextBuffer
2015 * @where: where to put the cursor
2017 * This function moves the "insert" and "selection_bound" marks
2018 * simultaneously. If you move them to the same place in two steps
2019 * with gtk_text_buffer_move_mark(), you will temporarily select a
2020 * region in between their old and new locations, which can be pretty
2021 * inefficient since the temporarily-selected region will force stuff
2022 * to be recalculated. This function moves them as a unit, which can
2026 gtk_text_buffer_place_cursor (GtkTextBuffer *buffer,
2027 const GtkTextIter *where)
2029 gtk_text_buffer_select_range (buffer, where, where);
2034 * gtk_text_buffer_select_range:
2035 * @buffer: a #GtkTextBuffer
2036 * @ins: where to put the "insert" mark
2037 * @bound: where to put the "selection_bound" mark
2039 * This function moves the "insert" and "selection_bound" marks
2040 * simultaneously. If you move them in two steps
2041 * with gtk_text_buffer_move_mark(), you will temporarily select a
2042 * region in between their old and new locations, which can be pretty
2043 * inefficient since the temporarily-selected region will force stuff
2044 * to be recalculated. This function moves them as a unit, which can
2050 gtk_text_buffer_select_range (GtkTextBuffer *buffer,
2051 const GtkTextIter *ins,
2052 const GtkTextIter *bound)
2054 GtkTextIter real_ins;
2055 GtkTextIter real_bound;
2057 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2060 real_bound = *bound;
2062 _gtk_text_btree_select_range (get_btree (buffer), &real_ins, &real_bound);
2063 gtk_text_buffer_mark_set (buffer, &real_ins,
2064 gtk_text_buffer_get_mark (buffer,
2066 gtk_text_buffer_mark_set (buffer, &real_bound,
2067 gtk_text_buffer_get_mark (buffer,
2068 "selection_bound"));
2069 update_selection_clipboards (buffer);
2077 * gtk_text_buffer_create_tag:
2078 * @buffer: a #GtkTextBuffer
2079 * @tag_name: name of the new tag, or %NULL
2080 * @first_property_name: name of first property to set, or %NULL
2081 * @Varargs: %NULL-terminated list of property names and values
2084 * Creates a tag and adds it to the tag table for @buffer.
2085 * Equivalent to calling gtk_text_tag_new () and then adding the
2086 * tag to the buffer's tag table. The returned tag is owned by
2087 * the buffer's tag table, so the ref count will be equal to one.
2089 * If @tag_name is %NULL, the tag is anonymous.
2091 * If @tag_name is non-%NULL, a tag called @tag_name must not already
2092 * exist in the tag table for this buffer.
2094 * The @first_property_name argument and subsequent arguments are a list
2095 * of properties to set on the tag, as with g_object_set().
2097 * Return value: a new tag
2100 gtk_text_buffer_create_tag (GtkTextBuffer *buffer,
2101 const gchar *tag_name,
2102 const gchar *first_property_name,
2108 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
2110 tag = gtk_text_tag_new (tag_name);
2112 gtk_text_tag_table_add (get_table (buffer), tag);
2114 if (first_property_name)
2116 va_start (list, first_property_name);
2117 g_object_set_valist (G_OBJECT (tag), first_property_name, list);
2121 g_object_unref (tag);
2127 gtk_text_buffer_real_apply_tag (GtkTextBuffer *buffer,
2129 const GtkTextIter *start,
2130 const GtkTextIter *end)
2132 if (tag->table != buffer->tag_table)
2134 g_warning ("Can only apply tags that are in the tag table for the buffer");
2138 _gtk_text_btree_tag (start, end, tag, TRUE);
2142 gtk_text_buffer_real_remove_tag (GtkTextBuffer *buffer,
2144 const GtkTextIter *start,
2145 const GtkTextIter *end)
2147 if (tag->table != buffer->tag_table)
2149 g_warning ("Can only remove tags that are in the tag table for the buffer");
2153 _gtk_text_btree_tag (start, end, tag, FALSE);
2157 gtk_text_buffer_real_changed (GtkTextBuffer *buffer)
2159 gtk_text_buffer_set_modified (buffer, TRUE);
2163 gtk_text_buffer_emit_tag (GtkTextBuffer *buffer,
2166 const GtkTextIter *start,
2167 const GtkTextIter *end)
2169 GtkTextIter start_tmp = *start;
2170 GtkTextIter end_tmp = *end;
2172 g_return_if_fail (tag != NULL);
2174 gtk_text_iter_order (&start_tmp, &end_tmp);
2177 g_signal_emit (buffer, signals[APPLY_TAG],
2179 tag, &start_tmp, &end_tmp);
2181 g_signal_emit (buffer, signals[REMOVE_TAG],
2183 tag, &start_tmp, &end_tmp);
2188 * gtk_text_buffer_apply_tag:
2189 * @buffer: a #GtkTextBuffer
2190 * @tag: a #GtkTextTag
2191 * @start: one bound of range to be tagged
2192 * @end: other bound of range to be tagged
2194 * Emits the "apply_tag" signal on @buffer. The default
2195 * handler for the signal applies @tag to the given range.
2196 * @start and @end do not have to be in order.
2200 gtk_text_buffer_apply_tag (GtkTextBuffer *buffer,
2202 const GtkTextIter *start,
2203 const GtkTextIter *end)
2205 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2206 g_return_if_fail (GTK_IS_TEXT_TAG (tag));
2207 g_return_if_fail (start != NULL);
2208 g_return_if_fail (end != NULL);
2209 g_return_if_fail (gtk_text_iter_get_buffer (start) == buffer);
2210 g_return_if_fail (gtk_text_iter_get_buffer (end) == buffer);
2211 g_return_if_fail (tag->table == buffer->tag_table);
2213 gtk_text_buffer_emit_tag (buffer, tag, TRUE, start, end);
2217 * gtk_text_buffer_remove_tag:
2218 * @buffer: a #GtkTextBuffer
2219 * @tag: a #GtkTextTag
2220 * @start: one bound of range to be untagged
2221 * @end: other bound of range to be untagged
2223 * Emits the "remove_tag" signal. The default handler for the signal
2224 * removes all occurrences of @tag from the given range. @start and
2225 * @end don't have to be in order.
2229 gtk_text_buffer_remove_tag (GtkTextBuffer *buffer,
2231 const GtkTextIter *start,
2232 const GtkTextIter *end)
2235 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2236 g_return_if_fail (GTK_IS_TEXT_TAG (tag));
2237 g_return_if_fail (start != NULL);
2238 g_return_if_fail (end != NULL);
2239 g_return_if_fail (gtk_text_iter_get_buffer (start) == buffer);
2240 g_return_if_fail (gtk_text_iter_get_buffer (end) == buffer);
2241 g_return_if_fail (tag->table == buffer->tag_table);
2243 gtk_text_buffer_emit_tag (buffer, tag, FALSE, start, end);
2248 * gtk_text_buffer_apply_tag_by_name:
2249 * @buffer: a #GtkTextBuffer
2250 * @name: name of a named #GtkTextTag
2251 * @start: one bound of range to be tagged
2252 * @end: other bound of range to be tagged
2254 * Calls gtk_text_tag_table_lookup() on the buffer's tag table to
2255 * get a #GtkTextTag, then calls gtk_text_buffer_apply_tag().
2259 gtk_text_buffer_apply_tag_by_name (GtkTextBuffer *buffer,
2261 const GtkTextIter *start,
2262 const GtkTextIter *end)
2266 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2267 g_return_if_fail (name != NULL);
2268 g_return_if_fail (start != NULL);
2269 g_return_if_fail (end != NULL);
2270 g_return_if_fail (gtk_text_iter_get_buffer (start) == buffer);
2271 g_return_if_fail (gtk_text_iter_get_buffer (end) == buffer);
2273 tag = gtk_text_tag_table_lookup (get_table (buffer),
2278 g_warning ("Unknown tag `%s'", name);
2282 gtk_text_buffer_emit_tag (buffer, tag, TRUE, start, end);
2286 * gtk_text_buffer_remove_tag_by_name:
2287 * @buffer: a #GtkTextBuffer
2288 * @name: name of a #GtkTextTag
2289 * @start: one bound of range to be untagged
2290 * @end: other bound of range to be untagged
2292 * Calls gtk_text_tag_table_lookup() on the buffer's tag table to
2293 * get a #GtkTextTag, then calls gtk_text_buffer_remove_tag().
2298 gtk_text_buffer_remove_tag_by_name (GtkTextBuffer *buffer,
2300 const GtkTextIter *start,
2301 const GtkTextIter *end)
2305 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2306 g_return_if_fail (name != NULL);
2307 g_return_if_fail (start != NULL);
2308 g_return_if_fail (end != NULL);
2309 g_return_if_fail (gtk_text_iter_get_buffer (start) == buffer);
2310 g_return_if_fail (gtk_text_iter_get_buffer (end) == buffer);
2312 tag = gtk_text_tag_table_lookup (get_table (buffer),
2317 g_warning ("Unknown tag `%s'", name);
2321 gtk_text_buffer_emit_tag (buffer, tag, FALSE, start, end);
2325 pointer_cmp (gconstpointer a,
2337 * gtk_text_buffer_remove_all_tags:
2338 * @buffer: a #GtkTextBuffer
2339 * @start: one bound of range to be untagged
2340 * @end: other bound of range to be untagged
2342 * Removes all tags in the range between @start and @end. Be careful
2343 * with this function; it could remove tags added in code unrelated to
2344 * the code you're currently writing. That is, using this function is
2345 * probably a bad idea if you have two or more unrelated code sections
2349 gtk_text_buffer_remove_all_tags (GtkTextBuffer *buffer,
2350 const GtkTextIter *start,
2351 const GtkTextIter *end)
2353 GtkTextIter first, second, tmp;
2359 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2360 g_return_if_fail (start != NULL);
2361 g_return_if_fail (end != NULL);
2362 g_return_if_fail (gtk_text_iter_get_buffer (start) == buffer);
2363 g_return_if_fail (gtk_text_iter_get_buffer (end) == buffer);
2368 gtk_text_iter_order (&first, &second);
2370 /* Get all tags turned on at the start */
2371 tags = gtk_text_iter_get_tags (&first);
2373 /* Find any that are toggled on within the range */
2375 while (gtk_text_iter_forward_to_tag_toggle (&tmp, NULL))
2380 if (gtk_text_iter_compare (&tmp, &second) >= 0)
2381 break; /* past the end of the range */
2383 toggled = gtk_text_iter_get_toggled_tags (&tmp, TRUE);
2385 /* We could end up with a really big-ass list here.
2388 tmp_list2 = toggled;
2389 while (tmp_list2 != NULL)
2391 tags = g_slist_prepend (tags, tmp_list2->data);
2393 tmp_list2 = g_slist_next (tmp_list2);
2396 g_slist_free (toggled);
2400 tags = g_slist_sort (tags, pointer_cmp);
2402 /* Strip duplicates */
2406 while (tmp_list != NULL)
2408 if (tag == tmp_list->data)
2412 prev->next = tmp_list->next;
2414 tmp_list->next = NULL;
2416 g_slist_free (tmp_list);
2418 tmp_list = prev->next;
2419 /* prev is unchanged */
2423 /* not a duplicate */
2424 tag = GTK_TEXT_TAG (tmp_list->data);
2426 tmp_list = tmp_list->next;
2430 g_slist_foreach (tags, (GFunc) g_object_ref, NULL);
2433 while (tmp_list != NULL)
2435 tag = GTK_TEXT_TAG (tmp_list->data);
2437 gtk_text_buffer_remove_tag (buffer, tag, &first, &second);
2439 tmp_list = tmp_list->next;
2442 g_slist_foreach (tags, (GFunc) g_object_unref, NULL);
2444 g_slist_free (tags);
2449 * Obtain various iterators
2453 * gtk_text_buffer_get_iter_at_line_offset:
2454 * @buffer: a #GtkTextBuffer
2455 * @iter: iterator to initialize
2456 * @line_number: line number counting from 0
2457 * @char_offset: char offset from start of line
2459 * Obtains an iterator pointing to @char_offset within the given
2460 * line. The @char_offset must exist, offsets off the end of the line
2461 * are not allowed. Note <emphasis>characters</emphasis>, not bytes;
2462 * UTF-8 may encode one character as multiple bytes.
2466 gtk_text_buffer_get_iter_at_line_offset (GtkTextBuffer *buffer,
2471 g_return_if_fail (iter != NULL);
2472 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2474 _gtk_text_btree_get_iter_at_line_char (get_btree (buffer),
2475 iter, line_number, char_offset);
2479 * gtk_text_buffer_get_iter_at_line_index:
2480 * @buffer: a #GtkTextBuffer
2481 * @iter: iterator to initialize
2482 * @line_number: line number counting from 0
2483 * @byte_index: byte index from start of line
2485 * Obtains an iterator pointing to @byte_index within the given line.
2486 * @byte_index must be the start of a UTF-8 character, and must not be
2487 * beyond the end of the line. Note <emphasis>bytes</emphasis>, not
2488 * characters; UTF-8 may encode one character as multiple bytes.
2492 gtk_text_buffer_get_iter_at_line_index (GtkTextBuffer *buffer,
2497 g_return_if_fail (iter != NULL);
2498 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2500 _gtk_text_btree_get_iter_at_line_byte (get_btree (buffer),
2501 iter, line_number, byte_index);
2505 * gtk_text_buffer_get_iter_at_line:
2506 * @buffer: a #GtkTextBuffer
2507 * @iter: iterator to initialize
2508 * @line_number: line number counting from 0
2510 * Initializes @iter to the start of the given line.
2513 gtk_text_buffer_get_iter_at_line (GtkTextBuffer *buffer,
2517 g_return_if_fail (iter != NULL);
2518 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2520 gtk_text_buffer_get_iter_at_line_offset (buffer, iter, line_number, 0);
2524 * gtk_text_buffer_get_iter_at_offset:
2525 * @buffer: a #GtkTextBuffer
2526 * @iter: iterator to initialize
2527 * @char_offset: char offset from start of buffer, counting from 0, or -1
2529 * Initializes @iter to a position @char_offset chars from the start
2530 * of the entire buffer. If @char_offset is -1 or greater than the number
2531 * of characters in the buffer, @iter is initialized to the end iterator,
2532 * the iterator one past the last valid character in the buffer.
2535 gtk_text_buffer_get_iter_at_offset (GtkTextBuffer *buffer,
2539 g_return_if_fail (iter != NULL);
2540 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2542 _gtk_text_btree_get_iter_at_char (get_btree (buffer), iter, char_offset);
2546 * gtk_text_buffer_get_start_iter:
2547 * @buffer: a #GtkTextBuffer
2548 * @iter: iterator to initialize
2550 * Initialized @iter with the first position in the text buffer. This
2551 * is the same as using gtk_text_buffer_get_iter_at_offset() to get
2552 * the iter at character offset 0.
2555 gtk_text_buffer_get_start_iter (GtkTextBuffer *buffer,
2558 g_return_if_fail (iter != NULL);
2559 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2561 _gtk_text_btree_get_iter_at_char (get_btree (buffer), iter, 0);
2565 * gtk_text_buffer_get_end_iter:
2566 * @buffer: a #GtkTextBuffer
2567 * @iter: iterator to initialize
2569 * Initializes @iter with the "end iterator," one past the last valid
2570 * character in the text buffer. If dereferenced with
2571 * gtk_text_iter_get_char(), the end iterator has a character value of
2572 * 0. The entire buffer lies in the range from the first position in
2573 * the buffer (call gtk_text_buffer_get_start_iter() to get
2574 * character position 0) to the end iterator.
2578 gtk_text_buffer_get_end_iter (GtkTextBuffer *buffer,
2581 g_return_if_fail (iter != NULL);
2582 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2584 _gtk_text_btree_get_end_iter (get_btree (buffer), iter);
2588 * gtk_text_buffer_get_bounds:
2589 * @buffer: a #GtkTextBuffer
2590 * @start: iterator to initialize with first position in the buffer
2591 * @end: iterator to initialize with the end iterator
2593 * Retrieves the first and last iterators in the buffer, i.e. the
2594 * entire buffer lies within the range [@start,@end).
2598 gtk_text_buffer_get_bounds (GtkTextBuffer *buffer,
2602 g_return_if_fail (start != NULL);
2603 g_return_if_fail (end != NULL);
2604 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2606 _gtk_text_btree_get_iter_at_char (get_btree (buffer), start, 0);
2607 _gtk_text_btree_get_end_iter (get_btree (buffer), end);
2615 * gtk_text_buffer_get_modified:
2616 * @buffer: a #GtkTextBuffer
2618 * Indicates whether the buffer has been modified since the last call
2619 * to gtk_text_buffer_set_modified() set the modification flag to
2620 * %FALSE. Used for example to enable a "save" function in a text
2623 * Return value: %TRUE if the buffer has been modified
2626 gtk_text_buffer_get_modified (GtkTextBuffer *buffer)
2628 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
2630 return buffer->modified;
2634 * gtk_text_buffer_set_modified:
2635 * @buffer: a #GtkTextBuffer
2636 * @setting: modification flag setting
2638 * Used to keep track of whether the buffer has been modified since the
2639 * last time it was saved. Whenever the buffer is saved to disk, call
2640 * gtk_text_buffer_set_modified (@buffer, FALSE). When the buffer is modified,
2641 * it will automatically toggled on the modified bit again. When the modified
2642 * bit flips, the buffer emits a "modified_changed" signal.
2646 gtk_text_buffer_set_modified (GtkTextBuffer *buffer,
2649 gboolean fixed_setting;
2651 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2653 fixed_setting = setting != FALSE;
2655 if (buffer->modified == fixed_setting)
2659 buffer->modified = fixed_setting;
2660 g_signal_emit (buffer, signals[MODIFIED_CHANGED], 0);
2666 * Assorted other stuff
2670 * gtk_text_buffer_get_line_count:
2671 * @buffer: a #GtkTextBuffer
2673 * Obtains the number of lines in the buffer. This value is cached, so
2674 * the function is very fast.
2676 * Return value: number of lines in the buffer
2679 gtk_text_buffer_get_line_count (GtkTextBuffer *buffer)
2681 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), 0);
2683 return _gtk_text_btree_line_count (get_btree (buffer));
2687 * gtk_text_buffer_get_char_count:
2688 * @buffer: a #GtkTextBuffer
2690 * Gets the number of characters in the buffer; note that characters
2691 * and bytes are not the same, you can't e.g. expect the contents of
2692 * the buffer in string form to be this many bytes long. The character
2693 * count is cached, so this function is very fast.
2695 * Return value: number of characters in the buffer
2698 gtk_text_buffer_get_char_count (GtkTextBuffer *buffer)
2700 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), 0);
2702 return _gtk_text_btree_char_count (get_btree (buffer));
2705 /* Called when we lose the primary selection.
2708 clipboard_clear_selection_cb (GtkClipboard *clipboard,
2711 /* Move selection_bound to the insertion point */
2713 GtkTextIter selection_bound;
2714 GtkTextBuffer *buffer = GTK_TEXT_BUFFER (data);
2716 gtk_text_buffer_get_iter_at_mark (buffer, &insert,
2717 gtk_text_buffer_get_mark (buffer, "insert"));
2718 gtk_text_buffer_get_iter_at_mark (buffer, &selection_bound,
2719 gtk_text_buffer_get_mark (buffer, "selection_bound"));
2721 if (!gtk_text_iter_equal (&insert, &selection_bound))
2722 gtk_text_buffer_move_mark (buffer,
2723 gtk_text_buffer_get_mark (buffer, "selection_bound"),
2727 /* Called when we have the primary selection and someone else wants our
2728 * data in order to paste it.
2731 clipboard_get_selection_cb (GtkClipboard *clipboard,
2732 GtkSelectionData *selection_data,
2736 GtkTextBuffer *buffer = GTK_TEXT_BUFFER (data);
2737 GtkTextIter start, end;
2739 if (gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
2741 if (selection_data->target ==
2742 gdk_atom_intern ("GTK_TEXT_BUFFER_CONTENTS", FALSE))
2744 /* Provide the address of the buffer; this will only be
2745 * used within-process
2747 gtk_selection_data_set (selection_data,
2748 gdk_atom_intern ("GTK_TEXT_BUFFER_CONTENTS", FALSE),
2757 str = gtk_text_iter_get_visible_text (&start, &end);
2758 gtk_selection_data_set_text (selection_data, str, -1);
2766 GtkClipboard *clipboard;
2767 GtkTextBuffer *buffer;
2771 remove_all_clipboard_contents_buffers (GtkTextBuffer *buffer)
2773 GSList *tmp_list = buffer->clipboard_contents_buffers;
2776 ContentsBuffer *contents_buffer = tmp_list->data;
2778 g_object_unref (contents_buffer->buffer);
2779 g_free (contents_buffer);
2781 tmp_list = tmp_list->next;
2784 g_slist_free (buffer->clipboard_contents_buffers);
2785 buffer->clipboard_contents_buffers = NULL;
2789 remove_clipboard_contents_buffer (GtkTextBuffer *buffer,
2790 GtkClipboard *clipboard)
2792 GSList *tmp_list = buffer->clipboard_contents_buffers;
2795 ContentsBuffer *contents_buffer = tmp_list->data;
2797 if (contents_buffer->clipboard == clipboard)
2799 buffer->clipboard_contents_buffers = g_slist_remove (buffer->clipboard_contents_buffers, contents_buffer);
2801 g_object_unref (contents_buffer->buffer);
2802 g_free (contents_buffer);
2807 tmp_list = tmp_list->next;
2811 static GtkTextBuffer *
2812 get_clipboard_contents_buffer (GtkTextBuffer *buffer,
2813 GtkClipboard *clipboard,
2816 ContentsBuffer *contents_buffer;
2819 tmp_list = buffer->clipboard_contents_buffers;
2822 contents_buffer = tmp_list->data;
2823 if (contents_buffer->clipboard == clipboard)
2824 return contents_buffer->buffer;
2829 contents_buffer = g_new (ContentsBuffer, 1);
2830 contents_buffer->clipboard = clipboard;
2831 contents_buffer->buffer = gtk_text_buffer_new (gtk_text_buffer_get_tag_table (buffer));
2833 g_object_set_data (G_OBJECT (contents_buffer->buffer), "gtk-text-buffer-clipboard",
2834 GUINT_TO_POINTER (1));
2836 buffer->clipboard_contents_buffers = g_slist_prepend (buffer->clipboard_contents_buffers, contents_buffer);
2839 return contents_buffer->buffer;
2845 /* Provide cut/copied data */
2847 clipboard_get_contents_cb (GtkClipboard *clipboard,
2848 GtkSelectionData *selection_data,
2852 GtkTextBuffer *buffer;
2853 GtkTextBuffer *contents;
2855 buffer = GTK_TEXT_BUFFER (data);
2856 contents = get_clipboard_contents_buffer (buffer, clipboard, FALSE);
2858 g_assert (contents); /* This should never be called unless we own the clipboard */
2860 if (selection_data->target ==
2861 gdk_atom_intern ("GTK_TEXT_BUFFER_CONTENTS", FALSE))
2863 /* Provide the address of the clipboard buffer; this will only
2864 * be used within-process. OK to supply a NULL value for contents.
2866 gtk_selection_data_set (selection_data,
2867 gdk_atom_intern ("GTK_TEXT_BUFFER_CONTENTS", FALSE),
2875 GtkTextIter start, end;
2877 gtk_text_buffer_get_bounds (contents, &start, &end);
2879 str = gtk_text_iter_get_visible_text (&start, &end);
2880 gtk_selection_data_set_text (selection_data, str, -1);
2886 clipboard_clear_contents_cb (GtkClipboard *clipboard,
2889 GtkTextBuffer *buffer = GTK_TEXT_BUFFER (data);
2891 remove_clipboard_contents_buffer (buffer, clipboard);
2895 get_paste_point (GtkTextBuffer *buffer,
2897 gboolean clear_afterward)
2899 GtkTextIter insert_point;
2900 GtkTextMark *paste_point_override;
2902 paste_point_override = gtk_text_buffer_get_mark (buffer,
2903 "gtk_paste_point_override");
2905 if (paste_point_override != NULL)
2907 gtk_text_buffer_get_iter_at_mark (buffer, &insert_point,
2908 paste_point_override);
2909 if (clear_afterward)
2910 gtk_text_buffer_delete_mark (buffer,
2911 gtk_text_buffer_get_mark (buffer,
2912 "gtk_paste_point_override"));
2916 gtk_text_buffer_get_iter_at_mark (buffer, &insert_point,
2917 gtk_text_buffer_get_mark (buffer,
2921 *iter = insert_point;
2925 pre_paste_prep (ClipboardRequest *request_data,
2926 GtkTextIter *insert_point)
2928 GtkTextBuffer *buffer = request_data->buffer;
2930 get_paste_point (buffer, insert_point, TRUE);
2932 /* If we're going to replace the selection, we insert before it to
2933 * avoid messing it up, then we delete the selection after inserting.
2935 if (request_data->replace_selection)
2937 GtkTextIter start, end;
2939 if (gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
2940 *insert_point = start;
2945 post_paste_cleanup (ClipboardRequest *request_data)
2947 if (request_data->replace_selection)
2949 GtkTextIter start, end;
2951 if (gtk_text_buffer_get_selection_bounds (request_data->buffer,
2954 if (request_data->interactive)
2955 gtk_text_buffer_delete_interactive (request_data->buffer,
2958 request_data->default_editable);
2960 gtk_text_buffer_delete (request_data->buffer, &start, &end);
2965 /* Called when we request a paste and receive the text data
2968 clipboard_text_received (GtkClipboard *clipboard,
2972 ClipboardRequest *request_data = data;
2973 GtkTextBuffer *buffer = request_data->buffer;
2977 GtkTextIter insert_point;
2979 if (request_data->interactive)
2980 gtk_text_buffer_begin_user_action (buffer);
2982 pre_paste_prep (request_data, &insert_point);
2984 if (request_data->interactive)
2985 gtk_text_buffer_insert_interactive (buffer, &insert_point,
2986 str, -1, request_data->default_editable);
2988 gtk_text_buffer_insert (buffer, &insert_point,
2991 post_paste_cleanup (request_data);
2993 if (request_data->interactive)
2994 gtk_text_buffer_end_user_action (buffer);
2997 g_object_unref (buffer);
2998 g_free (request_data);
3001 static GtkTextBuffer*
3002 selection_data_get_buffer (GtkSelectionData *selection_data,
3003 ClipboardRequest *request_data)
3006 GtkTextBuffer *src_buffer = NULL;
3008 /* If we can get the owner, the selection is in-process */
3009 owner = gdk_selection_owner_get_for_display (selection_data->display,
3010 selection_data->selection);
3015 if (gdk_window_get_window_type (owner) == GDK_WINDOW_FOREIGN)
3018 if (selection_data->type != gdk_atom_intern ("GTK_TEXT_BUFFER_CONTENTS", FALSE))
3021 if (selection_data->length != sizeof (src_buffer))
3024 memcpy (&src_buffer, selection_data->data, sizeof (src_buffer));
3026 if (src_buffer == NULL)
3029 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (src_buffer), NULL);
3031 if (gtk_text_buffer_get_tag_table (src_buffer) !=
3032 gtk_text_buffer_get_tag_table (request_data->buffer))
3039 /* These are pretty handy functions; maybe something like them
3040 * should be in the public API. Also, there are other places in this
3041 * file where they could be used.
3044 save_iter (const GtkTextIter *iter,
3045 gboolean left_gravity)
3047 return gtk_text_buffer_create_mark (gtk_text_iter_get_buffer (iter),
3054 restore_iter (const GtkTextIter *iter,
3057 gtk_text_buffer_get_iter_at_mark (gtk_text_mark_get_buffer (save_id),
3058 (GtkTextIter*) iter,
3060 gtk_text_buffer_delete_mark (gtk_text_mark_get_buffer (save_id),
3066 paste_from_buffer (ClipboardRequest *request_data,
3067 GtkTextBuffer *src_buffer,
3068 const GtkTextIter *start,
3069 const GtkTextIter *end)
3071 GtkTextIter insert_point;
3072 GtkTextBuffer *buffer = request_data->buffer;
3074 /* We're about to emit a bunch of signals, so be safe */
3075 g_object_ref (src_buffer);
3077 pre_paste_prep (request_data, &insert_point);
3079 if (request_data->interactive)
3080 gtk_text_buffer_begin_user_action (buffer);
3082 if (!gtk_text_iter_equal (start, end))
3084 if (!request_data->interactive ||
3085 (gtk_text_iter_can_insert (&insert_point,
3086 request_data->default_editable)))
3087 gtk_text_buffer_real_insert_range (buffer,
3091 request_data->interactive);
3094 post_paste_cleanup (request_data);
3096 if (request_data->interactive)
3097 gtk_text_buffer_end_user_action (buffer);
3099 g_object_unref (src_buffer);
3100 g_free (request_data);
3104 clipboard_clipboard_buffer_received (GtkClipboard *clipboard,
3105 GtkSelectionData *selection_data,
3108 ClipboardRequest *request_data = data;
3109 GtkTextBuffer *src_buffer;
3111 src_buffer = selection_data_get_buffer (selection_data, request_data);
3115 GtkTextIter start, end;
3117 if (g_object_get_data (G_OBJECT (src_buffer), "gtk-text-buffer-clipboard"))
3119 gtk_text_buffer_get_bounds (src_buffer, &start, &end);
3121 paste_from_buffer (request_data, src_buffer,
3126 if (gtk_text_buffer_get_selection_bounds (src_buffer, &start, &end))
3127 paste_from_buffer (request_data, src_buffer,
3133 /* Request the text selection instead */
3134 gtk_clipboard_request_text (clipboard,
3135 clipboard_text_received,
3140 static const GtkTargetEntry targets[] = {
3141 { "STRING", 0, TARGET_STRING },
3142 { "TEXT", 0, TARGET_TEXT },
3143 { "COMPOUND_TEXT", 0, TARGET_COMPOUND_TEXT },
3144 { "UTF8_STRING", 0, TARGET_UTF8_STRING },
3145 { "GTK_TEXT_BUFFER_CONTENTS", 0, TARGET_TEXT_BUFFER_CONTENTS }
3150 GtkClipboard *clipboard;
3152 } SelectionClipboard;
3155 update_selection_clipboards (GtkTextBuffer *buffer)
3157 GSList *tmp_list = buffer->selection_clipboards;
3163 SelectionClipboard *selection_clipboard = tmp_list->data;
3164 GtkClipboard *clipboard = selection_clipboard->clipboard;
3166 /* Determine whether we have a selection and adjust X selection
3169 if (!gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
3171 if (gtk_clipboard_get_owner (clipboard) == G_OBJECT (buffer))
3172 gtk_clipboard_clear (clipboard);
3176 /* Even if we already have the selection, we need to update our
3179 if (!gtk_clipboard_set_with_owner (clipboard, targets, G_N_ELEMENTS (targets),
3180 clipboard_get_selection_cb,
3181 clipboard_clear_selection_cb,
3183 clipboard_clear_selection_cb (clipboard, buffer);
3186 tmp_list = tmp_list->next;
3190 static SelectionClipboard *
3191 find_selection_clipboard (GtkTextBuffer *buffer,
3192 GtkClipboard *clipboard)
3194 GSList *tmp_list = buffer->selection_clipboards;
3197 SelectionClipboard *selection_clipboard = tmp_list->data;
3198 if (selection_clipboard->clipboard == clipboard)
3199 return selection_clipboard;
3201 tmp_list = tmp_list->next;
3208 * gtk_text_buffer_add_selection_clipboard:
3209 * @buffer: a #GtkTextBuffer
3210 * @clipboard: a #GtkClipboard
3212 * Adds @clipboard to the list of clipboards in which the selection contents
3213 * of @buffer are available. In most cases, @clipboard will be the #GtkClipboard
3214 * of type %GDK_SELECTION_PRIMARY for a view of @buffer.
3217 gtk_text_buffer_add_selection_clipboard (GtkTextBuffer *buffer,
3218 GtkClipboard *clipboard)
3220 SelectionClipboard *selection_clipboard;
3222 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
3223 g_return_if_fail (clipboard != NULL);
3225 selection_clipboard = find_selection_clipboard (buffer, clipboard);
3226 if (selection_clipboard)
3228 selection_clipboard->ref_count++;
3232 selection_clipboard = g_new (SelectionClipboard, 1);
3234 selection_clipboard->clipboard = clipboard;
3235 selection_clipboard->ref_count = 1;
3237 buffer->selection_clipboards = g_slist_prepend (buffer->selection_clipboards, selection_clipboard);
3242 * gtk_text_buffer_remove_selection_clipboard:
3243 * @buffer: a #GtkTextBuffer
3244 * @clipboard: a #GtkClipboard added to @buffer by gtk_text_buffer_add_selection_clipboard().
3246 * Removes a #GtkClipboard added with gtk_text_buffer_add_selection_clipboard()
3249 gtk_text_buffer_remove_selection_clipboard (GtkTextBuffer *buffer,
3250 GtkClipboard *clipboard)
3252 SelectionClipboard *selection_clipboard;
3254 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
3255 g_return_if_fail (clipboard != NULL);
3257 selection_clipboard = find_selection_clipboard (buffer, clipboard);
3258 g_return_if_fail (selection_clipboard != NULL);
3260 selection_clipboard->ref_count--;
3261 if (selection_clipboard->ref_count == 0)
3263 if (gtk_clipboard_get_owner (selection_clipboard->clipboard) == G_OBJECT (buffer))
3264 gtk_clipboard_clear (selection_clipboard->clipboard);
3266 buffer->selection_clipboards = g_slist_remove (buffer->selection_clipboards,
3267 selection_clipboard);
3269 g_free (selection_clipboard);
3274 remove_all_selection_clipboards (GtkTextBuffer *buffer)
3276 GSList *tmp_list = buffer->selection_clipboards;
3279 SelectionClipboard *selection_clipboard = tmp_list->data;
3281 if (gtk_clipboard_get_owner (selection_clipboard->clipboard) == G_OBJECT (buffer))
3282 gtk_clipboard_clear (selection_clipboard->clipboard);
3284 g_free (selection_clipboard);
3286 tmp_list = tmp_list->next;
3289 g_slist_free (buffer->selection_clipboards);
3290 buffer->selection_clipboards = NULL;
3294 * gtk_text_buffer_paste_clipboard:
3295 * @buffer: a #GtkTextBuffer
3296 * @clipboard: the #GtkClipboard to paste from
3297 * @override_location: location to insert pasted text, or %NULL for at the cursor
3298 * @default_editable: whether the buffer is editable by default
3300 * Pastes the contents of a clipboard at the insertion point, or at @override_location.
3301 * (Note: pasting is asynchronous, that is, we'll ask for the paste data and
3302 * return, and at some point later after the main loop runs, the paste
3303 * data will be inserted.)
3307 gtk_text_buffer_paste_clipboard (GtkTextBuffer *buffer,
3308 GtkClipboard *clipboard,
3309 GtkTextIter *override_location,
3310 gboolean default_editable)
3312 ClipboardRequest *data = g_new (ClipboardRequest, 1);
3313 GtkTextIter paste_point;
3314 GtkTextIter start, end;
3316 if (override_location != NULL)
3317 gtk_text_buffer_create_mark (buffer,
3318 "gtk_paste_point_override",
3319 override_location, FALSE);
3321 data->buffer = buffer;
3322 g_object_ref (buffer);
3323 data->interactive = TRUE;
3324 data->default_editable = default_editable;
3326 /* When pasting with the cursor inside the selection area, you
3327 * replace the selection with the new text, otherwise, you
3328 * simply insert the new text at the point where the click
3329 * occured, unselecting any selected text. The replace_selection
3330 * flag toggles this behavior.
3332 data->replace_selection = FALSE;
3334 get_paste_point (buffer, &paste_point, FALSE);
3335 if (gtk_text_buffer_get_selection_bounds (buffer, &start, &end) &&
3336 (gtk_text_iter_in_range (&paste_point, &start, &end) ||
3337 gtk_text_iter_equal (&paste_point, &end)))
3338 data->replace_selection = TRUE;
3340 gtk_clipboard_request_contents (clipboard,
3341 gdk_atom_intern ("GTK_TEXT_BUFFER_CONTENTS", FALSE),
3342 clipboard_clipboard_buffer_received, data);
3346 * gtk_text_buffer_delete_selection:
3347 * @buffer: a #GtkTextBuffer
3348 * @interactive: whether the deletion is caused by user interaction
3349 * @default_editable: whether the buffer is editable by default
3351 * Deletes the range between the "insert" and "selection_bound" marks,
3352 * that is, the currently-selected text. If @interactive is %TRUE,
3353 * the editability of the selection will be considered (users can't delete
3356 * Return value: whether there was a non-empty selection to delete
3359 gtk_text_buffer_delete_selection (GtkTextBuffer *buffer,
3360 gboolean interactive,
3361 gboolean default_editable)
3366 if (!gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
3368 return FALSE; /* No selection */
3374 gtk_text_buffer_begin_user_action (buffer);
3375 gtk_text_buffer_delete_interactive (buffer, &start, &end, default_editable);
3376 gtk_text_buffer_end_user_action (buffer);
3379 gtk_text_buffer_delete (buffer, &start, &end);
3381 return TRUE; /* We deleted stuff */
3386 * gtk_text_buffer_backspace:
3387 * @buffer: a #GtkTextBuffer
3388 * @iter: a position in @buffer
3389 * @interactive: whether the deletion is caused by user interaction
3390 * @default_editable: whether the buffer is editable by default
3392 * Performs the appropriate action as if the user hit the delete
3393 * key with the cursor at the position specified by @iter. In the
3394 * normal case a single character will be deleted, but when
3395 * combining accents are involved, more than one character can
3396 * be deleted, and when precomposed character and accent combinations,
3397 * less than one character will be deleted.
3399 * @iter must be at a cursor position.
3401 * Return value: %TRUE if tbe buffer was modified
3406 gtk_text_buffer_backspace (GtkTextBuffer *buffer,
3408 gboolean interactive,
3409 gboolean default_editable)
3411 gchar *cluster_text;
3414 gboolean retval = FALSE;
3415 const PangoLogAttr *attrs;
3417 gboolean backspace_deletes_character;
3419 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
3420 g_return_val_if_fail (iter != NULL, FALSE);
3425 attrs = _gtk_text_buffer_get_line_log_attrs (buffer, &start, NULL);
3427 /* For no good reason, attrs is NULL for the empty last line in
3428 * a buffer. Special case that here. (#156164)
3432 offset = gtk_text_iter_get_line_offset (&start);
3433 backspace_deletes_character = attrs[offset].backspace_deletes_character;
3436 backspace_deletes_character = FALSE;
3438 gtk_text_iter_backward_cursor_position (&start);
3440 if (gtk_text_iter_equal (&start, &end))
3443 cluster_text = gtk_text_iter_get_text (&start, &end);
3446 gtk_text_buffer_begin_user_action (buffer);
3448 if (gtk_text_buffer_delete_interactive (buffer, &start, &end,
3451 if (backspace_deletes_character)
3453 gchar *normalized_text = g_utf8_normalize (cluster_text,
3454 strlen (cluster_text),
3456 glong len = g_utf8_strlen (normalized_text, -1);
3459 gtk_text_buffer_insert_interactive (buffer,
3462 g_utf8_offset_to_pointer (normalized_text, len - 1) - normalized_text,
3465 g_free (normalized_text);
3472 gtk_text_buffer_end_user_action (buffer);
3474 g_free (cluster_text);
3480 cut_or_copy (GtkTextBuffer *buffer,
3481 GtkClipboard *clipboard,
3482 gboolean delete_region_after,
3483 gboolean interactive,
3484 gboolean default_editable)
3486 /* We prefer to cut the selected region between selection_bound and
3487 * insertion point. If that region is empty, then we cut the region
3488 * between the "anchor" and the insertion point (this is for
3489 * C-space and M-w and other Emacs-style copy/yank keys). Note that
3490 * insert and selection_bound are guaranteed to exist, but the
3491 * anchor only exists sometimes.
3496 if (!gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
3498 /* Let's try the anchor thing */
3499 GtkTextMark * anchor = gtk_text_buffer_get_mark (buffer, "anchor");
3505 gtk_text_buffer_get_iter_at_mark (buffer, &end, anchor);
3506 gtk_text_iter_order (&start, &end);
3510 if (!gtk_text_iter_equal (&start, &end))
3513 GtkTextBuffer *contents;
3515 remove_clipboard_contents_buffer (buffer, clipboard);
3516 contents = get_clipboard_contents_buffer (buffer, clipboard, TRUE);
3518 gtk_text_buffer_get_iter_at_offset (contents, &ins, 0);
3520 gtk_text_buffer_insert_range (contents, &ins, &start, &end);
3522 if (!gtk_clipboard_set_with_owner (clipboard, targets, G_N_ELEMENTS (targets),
3523 clipboard_get_contents_cb,
3524 clipboard_clear_contents_cb,
3526 clipboard_clear_contents_cb (clipboard, buffer);
3528 if (delete_region_after)
3531 gtk_text_buffer_delete_interactive (buffer, &start, &end,
3534 gtk_text_buffer_delete (buffer, &start, &end);
3540 * gtk_text_buffer_cut_clipboard:
3541 * @buffer: a #GtkTextBuffer
3542 * @clipboard: the #GtkClipboard object to cut to.
3543 * @default_editable: default editability of the buffer
3545 * Copies the currently-selected text to a clipboard, then deletes
3546 * said text if it's editable.
3550 gtk_text_buffer_cut_clipboard (GtkTextBuffer *buffer,
3551 GtkClipboard *clipboard,
3552 gboolean default_editable)
3554 gtk_text_buffer_begin_user_action (buffer);
3555 cut_or_copy (buffer, clipboard, TRUE, TRUE, default_editable);
3556 gtk_text_buffer_end_user_action (buffer);
3560 * gtk_text_buffer_copy_clipboard:
3561 * @buffer: a #GtkTextBuffer
3562 * @clipboard: the #GtkClipboard object to copy to.
3564 * Copies the currently-selected text to a clipboard.
3568 gtk_text_buffer_copy_clipboard (GtkTextBuffer *buffer,
3569 GtkClipboard *clipboard)
3571 gtk_text_buffer_begin_user_action (buffer);
3572 cut_or_copy (buffer, clipboard, FALSE, TRUE, TRUE);
3573 gtk_text_buffer_end_user_action (buffer);
3578 * gtk_text_buffer_get_selection_bounds:
3579 * @buffer: a #GtkTextBuffer a #GtkTextBuffer
3580 * @start: iterator to initialize with selection start
3581 * @end: iterator to initialize with selection end
3583 * Returns %TRUE if some text is selected; places the bounds
3584 * of the selection in @start and @end (if the selection has length 0,
3585 * then @start and @end are filled in with the same value).
3586 * @start and @end will be in ascending order. If @start and @end are
3587 * NULL, then they are not filled in, but the return value still indicates
3588 * whether text is selected.
3590 * Return value: whether the selection has nonzero length
3593 gtk_text_buffer_get_selection_bounds (GtkTextBuffer *buffer,
3597 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
3599 return _gtk_text_btree_get_selection_bounds (get_btree (buffer), start, end);
3603 * gtk_text_buffer_begin_user_action:
3604 * @buffer: a #GtkTextBuffer
3606 * Called to indicate that the buffer operations between here and a
3607 * call to gtk_text_buffer_end_user_action() are part of a single
3608 * user-visible operation. The operations between
3609 * gtk_text_buffer_begin_user_action() and
3610 * gtk_text_buffer_end_user_action() can then be grouped when creating
3611 * an undo stack. #GtkTextBuffer maintains a count of calls to
3612 * gtk_text_buffer_begin_user_action() that have not been closed with
3613 * a call to gtk_text_buffer_end_user_action(), and emits the "begin_user_action"
3614 * and "end_user_action" signals only for the outermost pair of calls.
3615 * This allows you to build user actions from other user actions.
3617 * The "interactive" buffer mutation functions, such as
3618 * gtk_text_buffer_insert_interactive(), automatically call begin/end
3619 * user action around the buffer operations they perform, so there's
3620 * no need to add extra calls if you user action consists solely of a
3621 * single call to one of those functions.
3624 gtk_text_buffer_begin_user_action (GtkTextBuffer *buffer)
3626 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
3628 buffer->user_action_count += 1;
3630 if (buffer->user_action_count == 1)
3632 /* Outermost nested user action begin emits the signal */
3633 g_signal_emit (buffer, signals[BEGIN_USER_ACTION], 0);
3638 * gtk_text_buffer_end_user_action:
3639 * @buffer: a #GtkTextBuffer
3641 * Should be paired with a call to gtk_text_buffer_begin_user_action().
3642 * See that function for a full explanation.
3645 gtk_text_buffer_end_user_action (GtkTextBuffer *buffer)
3647 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
3648 g_return_if_fail (buffer->user_action_count > 0);
3650 buffer->user_action_count -= 1;
3652 if (buffer->user_action_count == 0)
3654 /* Ended the outermost-nested user action end, so emit the signal */
3655 g_signal_emit (buffer, signals[END_USER_ACTION], 0);
3660 * Logical attribute cache
3663 #define ATTR_CACHE_SIZE 2
3665 typedef struct _CacheEntry CacheEntry;
3670 PangoLogAttr *attrs;
3674 struct _GtkTextLogAttrCache
3676 gint chars_changed_stamp;
3677 CacheEntry entries[ATTR_CACHE_SIZE];
3681 free_log_attr_cache (GtkTextLogAttrCache *cache)
3684 while (i < ATTR_CACHE_SIZE)
3686 g_free (cache->entries[i].attrs);
3693 clear_log_attr_cache (GtkTextLogAttrCache *cache)
3696 while (i < ATTR_CACHE_SIZE)
3698 g_free (cache->entries[i].attrs);
3699 cache->entries[i].attrs = NULL;
3704 static PangoLogAttr*
3705 compute_log_attrs (const GtkTextIter *iter,
3711 gint char_len, byte_len;
3712 PangoLogAttr *attrs = NULL;
3717 gtk_text_iter_set_line_offset (&start, 0);
3718 gtk_text_iter_forward_line (&end);
3720 paragraph = gtk_text_iter_get_slice (&start, &end);
3721 char_len = g_utf8_strlen (paragraph, -1);
3722 byte_len = strlen (paragraph);
3724 g_assert (char_len > 0);
3727 *char_lenp = char_len;
3729 attrs = g_new (PangoLogAttr, char_len + 1);
3731 /* FIXME we need to follow PangoLayout and allow different language
3732 * tags within the paragraph
3734 pango_get_log_attrs (paragraph, byte_len, -1,
3735 gtk_text_iter_get_language (&start),
3744 /* The return value from this is valid until you call this a second time.
3747 _gtk_text_buffer_get_line_log_attrs (GtkTextBuffer *buffer,
3748 const GtkTextIter *anywhere_in_line,
3752 GtkTextLogAttrCache *cache;
3755 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
3756 g_return_val_if_fail (anywhere_in_line != NULL, NULL);
3758 /* special-case for empty last line in buffer */
3759 if (gtk_text_iter_is_end (anywhere_in_line) &&
3760 gtk_text_iter_get_line_offset (anywhere_in_line) == 0)
3767 /* FIXME we also need to recompute log attrs if the language tag at
3768 * the start of a paragraph changes
3771 if (buffer->log_attr_cache == NULL)
3773 buffer->log_attr_cache = g_new0 (GtkTextLogAttrCache, 1);
3774 buffer->log_attr_cache->chars_changed_stamp =
3775 _gtk_text_btree_get_chars_changed_stamp (get_btree (buffer));
3777 else if (buffer->log_attr_cache->chars_changed_stamp !=
3778 _gtk_text_btree_get_chars_changed_stamp (get_btree (buffer)))
3780 clear_log_attr_cache (buffer->log_attr_cache);
3783 cache = buffer->log_attr_cache;
3784 line = gtk_text_iter_get_line (anywhere_in_line);
3787 while (i < ATTR_CACHE_SIZE)
3789 if (cache->entries[i].attrs &&
3790 cache->entries[i].line == line)
3793 *char_len = cache->entries[i].char_len;
3794 return cache->entries[i].attrs;
3799 /* Not in cache; open up the first cache entry */
3800 if (cache->entries[ATTR_CACHE_SIZE-1].attrs)
3801 g_free (cache->entries[ATTR_CACHE_SIZE-1].attrs);
3803 g_memmove (cache->entries + 1, cache->entries,
3804 sizeof (CacheEntry) * (ATTR_CACHE_SIZE - 1));
3806 cache->entries[0].line = line;
3807 cache->entries[0].attrs = compute_log_attrs (anywhere_in_line,
3808 &cache->entries[0].char_len);
3811 *char_len = cache->entries[0].char_len;
3813 return cache->entries[0].attrs;
3817 _gtk_text_buffer_notify_will_remove_tag (GtkTextBuffer *buffer,
3820 /* This removes tag from the buffer, but DOESN'T emit the
3821 * remove_tag signal, because we can't afford to have user
3822 * code messing things up at this point; the tag MUST be removed
3826 _gtk_text_btree_notify_will_remove_tag (buffer->btree, tag);
3834 _gtk_text_buffer_spew (GtkTextBuffer *buffer)
3836 _gtk_text_btree_spew (get_btree (buffer));