1 /* GTK - The GIMP Toolkit
2 * gtktextbuffer.c Copyright (C) 2000 Red Hat, Inc.
3 * Copyright (C) 2004 Nokia Corporation
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
22 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
23 * file for a list of people on the GTK+ Team. See the ChangeLog
24 * files for a list of changes. These files are distributed with
25 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
32 #define GTK_TEXT_USE_INTERNAL_UNSUPPORTED_API
33 #include "gtkclipboard.h"
35 #include "gtkinvisible.h"
36 #include "gtkmarshalers.h"
37 #include "gtktextbuffer.h"
38 #include "gtktextbufferrichtext.h"
39 #include "gtktextbtree.h"
40 #include "gtktextiterprivate.h"
41 #include "gtkprivate.h"
46 #define GTK_TEXT_BUFFER_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_TEXT_BUFFER, GtkTextBufferPrivate))
48 typedef struct _GtkTextBufferPrivate GtkTextBufferPrivate;
50 struct _GtkTextBufferPrivate
52 GtkTargetList *copy_target_list;
53 GtkTargetEntry *copy_target_entries;
54 gint n_copy_target_entries;
56 GtkTargetList *paste_target_list;
57 GtkTargetEntry *paste_target_entries;
58 gint n_paste_target_entries;
62 typedef struct _ClipboardRequest ClipboardRequest;
64 struct _ClipboardRequest
66 GtkTextBuffer *buffer;
68 gboolean default_editable;
69 gboolean is_clipboard;
70 gboolean replace_selection;
99 PROP_COPY_TARGET_LIST,
100 PROP_PASTE_TARGET_LIST
103 static void gtk_text_buffer_finalize (GObject *object);
105 static void gtk_text_buffer_real_insert_text (GtkTextBuffer *buffer,
109 static void gtk_text_buffer_real_insert_pixbuf (GtkTextBuffer *buffer,
112 static void gtk_text_buffer_real_insert_anchor (GtkTextBuffer *buffer,
114 GtkTextChildAnchor *anchor);
115 static void gtk_text_buffer_real_delete_range (GtkTextBuffer *buffer,
118 static void gtk_text_buffer_real_apply_tag (GtkTextBuffer *buffer,
120 const GtkTextIter *start_char,
121 const GtkTextIter *end_char);
122 static void gtk_text_buffer_real_remove_tag (GtkTextBuffer *buffer,
124 const GtkTextIter *start_char,
125 const GtkTextIter *end_char);
126 static void gtk_text_buffer_real_changed (GtkTextBuffer *buffer);
127 static void gtk_text_buffer_real_mark_set (GtkTextBuffer *buffer,
128 const GtkTextIter *iter,
131 static GtkTextBTree* get_btree (GtkTextBuffer *buffer);
132 static void free_log_attr_cache (GtkTextLogAttrCache *cache);
134 static void remove_all_selection_clipboards (GtkTextBuffer *buffer);
135 static void update_selection_clipboards (GtkTextBuffer *buffer);
137 static GtkTextBuffer *create_clipboard_contents_buffer (GtkTextBuffer *buffer);
139 static void gtk_text_buffer_free_target_lists (GtkTextBuffer *buffer);
141 static guint signals[LAST_SIGNAL] = { 0 };
143 static void gtk_text_buffer_set_property (GObject *object,
147 static void gtk_text_buffer_get_property (GObject *object,
151 static void gtk_text_buffer_notify (GObject *object,
154 G_DEFINE_TYPE (GtkTextBuffer, gtk_text_buffer, G_TYPE_OBJECT)
157 gtk_text_buffer_class_init (GtkTextBufferClass *klass)
159 GObjectClass *object_class = G_OBJECT_CLASS (klass);
161 object_class->finalize = gtk_text_buffer_finalize;
162 object_class->set_property = gtk_text_buffer_set_property;
163 object_class->get_property = gtk_text_buffer_get_property;
164 object_class->notify = gtk_text_buffer_notify;
166 klass->insert_text = gtk_text_buffer_real_insert_text;
167 klass->insert_pixbuf = gtk_text_buffer_real_insert_pixbuf;
168 klass->insert_child_anchor = gtk_text_buffer_real_insert_anchor;
169 klass->delete_range = gtk_text_buffer_real_delete_range;
170 klass->apply_tag = gtk_text_buffer_real_apply_tag;
171 klass->remove_tag = gtk_text_buffer_real_remove_tag;
172 klass->changed = gtk_text_buffer_real_changed;
173 klass->mark_set = gtk_text_buffer_real_mark_set;
176 g_object_class_install_property (object_class,
178 g_param_spec_object ("tag-table",
180 P_("Text Tag Table"),
181 GTK_TYPE_TEXT_TAG_TABLE,
182 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
184 /* Normal properties*/
187 * GtkTextBuffer:text:
189 * The text content of the buffer. Without child widgets and images,
190 * see gtk_text_buffer_get_text () for more information.
194 g_object_class_install_property (object_class,
196 g_param_spec_string ("text",
198 P_("Current text of the buffer"),
200 GTK_PARAM_READWRITE));
203 * GtkTextBuffer:has-selection:
205 * Whether the buffer has some text currently selected.
209 g_object_class_install_property (object_class,
211 g_param_spec_boolean ("has-selection",
213 P_("Whether the buffer has some text currently selected"),
215 GTK_PARAM_READABLE));
218 * GtkTextBuffer:cursor-position:
220 * The position of the insert mark (as offset from the beginning of the buffer).
221 * It is useful for getting notified when the cursor moves.
225 g_object_class_install_property (object_class,
226 PROP_CURSOR_POSITION,
227 g_param_spec_int ("cursor-position",
228 P_("Cursor position"),
229 P_("The position of the insert mark (as offset from the beginning of the buffer)"),
231 GTK_PARAM_READABLE));
234 * GtkTextBuffer:copy-target-list:
236 * The list of targets this buffer supports for clipboard copying
241 g_object_class_install_property (object_class,
242 PROP_COPY_TARGET_LIST,
243 g_param_spec_boxed ("copy-target-list",
244 P_("Copy target list"),
245 P_("The list of targets this buffer supports for clipboard copying and DND source"),
246 GTK_TYPE_TARGET_LIST,
247 GTK_PARAM_READABLE));
250 * GtkTextBuffer:paste-target-list:
252 * The list of targets this buffer supports for clipboard pasting
253 * and as DND destination.
257 g_object_class_install_property (object_class,
258 PROP_PASTE_TARGET_LIST,
259 g_param_spec_boxed ("paste-target-list",
260 P_("Paste target list"),
261 P_("The list of targets this buffer supports for clipboard pasting and DND destination"),
262 GTK_TYPE_TARGET_LIST,
263 GTK_PARAM_READABLE));
265 signals[INSERT_TEXT] =
266 g_signal_new (I_("insert_text"),
267 G_OBJECT_CLASS_TYPE (object_class),
269 G_STRUCT_OFFSET (GtkTextBufferClass, insert_text),
271 _gtk_marshal_VOID__BOXED_STRING_INT,
274 GTK_TYPE_TEXT_ITER | G_SIGNAL_TYPE_STATIC_SCOPE,
275 G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE,
278 signals[INSERT_PIXBUF] =
279 g_signal_new (I_("insert_pixbuf"),
280 G_OBJECT_CLASS_TYPE (object_class),
282 G_STRUCT_OFFSET (GtkTextBufferClass, insert_pixbuf),
284 _gtk_marshal_VOID__BOXED_OBJECT,
287 GTK_TYPE_TEXT_ITER | G_SIGNAL_TYPE_STATIC_SCOPE,
290 signals[INSERT_CHILD_ANCHOR] =
291 g_signal_new (I_("insert_child_anchor"),
292 G_OBJECT_CLASS_TYPE (object_class),
294 G_STRUCT_OFFSET (GtkTextBufferClass, insert_child_anchor),
296 _gtk_marshal_VOID__BOXED_OBJECT,
299 GTK_TYPE_TEXT_ITER | G_SIGNAL_TYPE_STATIC_SCOPE,
300 GTK_TYPE_TEXT_CHILD_ANCHOR);
303 * GtkTextBuffer::delete_range:
304 * @buffer: the object which received the signal.
305 * @start: the start of the range to be deleted
306 * @end: the end of the range to be deleted
308 * The ::delete_range signal is emitted to delete a range from
309 * a #GtkTextBuffer. Note that your handler must not invalidate the
310 * @start and @end iters (or has to revalidate them), if it runs before the
311 * default handler. There is no need to keep the iters valid in handlers
312 * which run after the default handler (see g_signal_connect_after()), but
313 * those don't have access to the deleted text.
315 signals[DELETE_RANGE] =
316 g_signal_new (I_("delete_range"),
317 G_OBJECT_CLASS_TYPE (object_class),
319 G_STRUCT_OFFSET (GtkTextBufferClass, delete_range),
321 _gtk_marshal_VOID__BOXED_BOXED,
324 GTK_TYPE_TEXT_ITER | G_SIGNAL_TYPE_STATIC_SCOPE,
325 GTK_TYPE_TEXT_ITER | G_SIGNAL_TYPE_STATIC_SCOPE);
328 g_signal_new (I_("changed"),
329 G_OBJECT_CLASS_TYPE (object_class),
331 G_STRUCT_OFFSET (GtkTextBufferClass, changed),
333 _gtk_marshal_VOID__VOID,
337 signals[MODIFIED_CHANGED] =
338 g_signal_new (I_("modified_changed"),
339 G_OBJECT_CLASS_TYPE (object_class),
341 G_STRUCT_OFFSET (GtkTextBufferClass, modified_changed),
343 _gtk_marshal_VOID__VOID,
348 g_signal_new (I_("mark_set"),
349 G_OBJECT_CLASS_TYPE (object_class),
351 G_STRUCT_OFFSET (GtkTextBufferClass, mark_set),
353 _gtk_marshal_VOID__BOXED_OBJECT,
359 signals[MARK_DELETED] =
360 g_signal_new (I_("mark_deleted"),
361 G_OBJECT_CLASS_TYPE (object_class),
363 G_STRUCT_OFFSET (GtkTextBufferClass, mark_deleted),
365 _gtk_marshal_VOID__OBJECT,
371 g_signal_new (I_("apply_tag"),
372 G_OBJECT_CLASS_TYPE (object_class),
374 G_STRUCT_OFFSET (GtkTextBufferClass, apply_tag),
376 _gtk_marshal_VOID__OBJECT_BOXED_BOXED,
383 signals[REMOVE_TAG] =
384 g_signal_new (I_("remove_tag"),
385 G_OBJECT_CLASS_TYPE (object_class),
387 G_STRUCT_OFFSET (GtkTextBufferClass, remove_tag),
389 _gtk_marshal_VOID__OBJECT_BOXED_BOXED,
396 signals[BEGIN_USER_ACTION] =
397 g_signal_new (I_("begin_user_action"),
398 G_OBJECT_CLASS_TYPE (object_class),
400 G_STRUCT_OFFSET (GtkTextBufferClass, begin_user_action),
402 _gtk_marshal_VOID__VOID,
406 signals[END_USER_ACTION] =
407 g_signal_new (I_("end_user_action"),
408 G_OBJECT_CLASS_TYPE (object_class),
410 G_STRUCT_OFFSET (GtkTextBufferClass, end_user_action),
412 _gtk_marshal_VOID__VOID,
416 g_type_class_add_private (object_class, sizeof (GtkTextBufferPrivate));
420 gtk_text_buffer_init (GtkTextBuffer *buffer)
422 buffer->clipboard_contents_buffers = NULL;
423 buffer->tag_table = NULL;
425 /* allow copying of arbiatray stuff in the internal rich text format */
426 gtk_text_buffer_register_serialize_tagset (buffer, NULL);
430 set_table (GtkTextBuffer *buffer, GtkTextTagTable *table)
432 g_return_if_fail (buffer->tag_table == NULL);
436 buffer->tag_table = table;
437 g_object_ref (buffer->tag_table);
438 _gtk_text_tag_table_add_buffer (table, buffer);
442 static GtkTextTagTable*
443 get_table (GtkTextBuffer *buffer)
445 if (buffer->tag_table == NULL)
447 buffer->tag_table = gtk_text_tag_table_new ();
448 _gtk_text_tag_table_add_buffer (buffer->tag_table, buffer);
451 return buffer->tag_table;
455 gtk_text_buffer_set_property (GObject *object,
460 GtkTextBuffer *text_buffer;
462 text_buffer = GTK_TEXT_BUFFER (object);
467 set_table (text_buffer, g_value_get_object (value));
471 gtk_text_buffer_set_text (text_buffer,
472 g_value_get_string (value), -1);
481 gtk_text_buffer_get_property (GObject *object,
486 GtkTextBuffer *text_buffer;
489 text_buffer = GTK_TEXT_BUFFER (object);
494 g_value_set_object (value, get_table (text_buffer));
499 GtkTextIter start, end;
501 gtk_text_buffer_get_start_iter (text_buffer, &start);
502 gtk_text_buffer_get_end_iter (text_buffer, &end);
504 g_value_take_string (value,
505 gtk_text_buffer_get_text (text_buffer,
506 &start, &end, FALSE));
510 case PROP_HAS_SELECTION:
511 g_value_set_boolean (value, text_buffer->has_selection);
514 case PROP_CURSOR_POSITION:
515 gtk_text_buffer_get_iter_at_mark (text_buffer, &iter,
516 gtk_text_buffer_get_insert (text_buffer));
517 g_value_set_int (value, gtk_text_iter_get_offset (&iter));
520 case PROP_COPY_TARGET_LIST:
521 g_value_set_boxed (value, gtk_text_buffer_get_copy_target_list (text_buffer));
524 case PROP_PASTE_TARGET_LIST:
525 g_value_set_boxed (value, gtk_text_buffer_get_paste_target_list (text_buffer));
534 gtk_text_buffer_notify (GObject *object,
537 if (!strcmp (pspec->name, "copy-target-list") ||
538 !strcmp (pspec->name, "paste-target-list"))
540 gtk_text_buffer_free_target_lists (GTK_TEXT_BUFFER (object));
545 * gtk_text_buffer_new:
546 * @table: a tag table, or NULL to create a new one
548 * Creates a new text buffer.
550 * Return value: a new text buffer
553 gtk_text_buffer_new (GtkTextTagTable *table)
555 GtkTextBuffer *text_buffer;
557 text_buffer = g_object_new (GTK_TYPE_TEXT_BUFFER, "tag-table", table, NULL);
563 gtk_text_buffer_finalize (GObject *object)
565 GtkTextBuffer *buffer;
567 buffer = GTK_TEXT_BUFFER (object);
569 priv = GTK_TEXT_BUFFER_GET_PRIVATE (buffer);
571 remove_all_selection_clipboards (buffer);
573 if (buffer->tag_table)
575 _gtk_text_tag_table_remove_buffer (buffer->tag_table, buffer);
576 g_object_unref (buffer->tag_table);
577 buffer->tag_table = NULL;
582 _gtk_text_btree_unref (buffer->btree);
583 buffer->btree = NULL;
586 if (buffer->log_attr_cache)
587 free_log_attr_cache (buffer->log_attr_cache);
589 buffer->log_attr_cache = NULL;
591 gtk_text_buffer_free_target_lists (buffer);
593 G_OBJECT_CLASS (gtk_text_buffer_parent_class)->finalize (object);
597 get_btree (GtkTextBuffer *buffer)
599 if (buffer->btree == NULL)
600 buffer->btree = _gtk_text_btree_new (gtk_text_buffer_get_tag_table (buffer),
603 return buffer->btree;
607 _gtk_text_buffer_get_btree (GtkTextBuffer *buffer)
609 return get_btree (buffer);
613 * gtk_text_buffer_get_tag_table:
614 * @buffer: a #GtkTextBuffer
616 * Get the #GtkTextTagTable associated with this buffer.
618 * Return value: the buffer's tag table
621 gtk_text_buffer_get_tag_table (GtkTextBuffer *buffer)
623 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
625 return get_table (buffer);
629 * gtk_text_buffer_set_text:
630 * @buffer: a #GtkTextBuffer
631 * @text: UTF-8 text to insert
632 * @len: length of @text in bytes
634 * Deletes current contents of @buffer, and inserts @text instead. If
635 * @len is -1, @text must be nul-terminated. @text must be valid UTF-8.
638 gtk_text_buffer_set_text (GtkTextBuffer *buffer,
642 GtkTextIter start, end;
644 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
645 g_return_if_fail (text != NULL);
650 gtk_text_buffer_get_bounds (buffer, &start, &end);
652 gtk_text_buffer_delete (buffer, &start, &end);
656 gtk_text_buffer_get_iter_at_offset (buffer, &start, 0);
657 gtk_text_buffer_insert (buffer, &start, text, len);
660 g_object_notify (G_OBJECT (buffer), "text");
670 gtk_text_buffer_real_insert_text (GtkTextBuffer *buffer,
675 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
676 g_return_if_fail (iter != NULL);
678 _gtk_text_btree_insert (iter, text, len);
680 g_signal_emit (buffer, signals[CHANGED], 0);
681 g_object_notify (G_OBJECT (buffer), "cursor-position");
685 gtk_text_buffer_emit_insert (GtkTextBuffer *buffer,
690 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
691 g_return_if_fail (iter != NULL);
692 g_return_if_fail (text != NULL);
697 g_return_if_fail (g_utf8_validate (text, len, NULL));
701 g_signal_emit (buffer, signals[INSERT_TEXT], 0,
707 * gtk_text_buffer_insert:
708 * @buffer: a #GtkTextBuffer
709 * @iter: a position in the buffer
710 * @text: UTF-8 format text to insert
711 * @len: length of text in bytes, or -1
713 * Inserts @len bytes of @text at position @iter. If @len is -1,
714 * @text must be nul-terminated and will be inserted in its
715 * entirety. Emits the "insert_text" signal; insertion actually occurs
716 * in the default handler for the signal. @iter is invalidated when
717 * insertion occurs (because the buffer contents change), but the
718 * default signal handler revalidates it to point to the end of the
723 gtk_text_buffer_insert (GtkTextBuffer *buffer,
728 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
729 g_return_if_fail (iter != NULL);
730 g_return_if_fail (text != NULL);
731 g_return_if_fail (gtk_text_iter_get_buffer (iter) == buffer);
733 gtk_text_buffer_emit_insert (buffer, iter, text, len);
737 * gtk_text_buffer_insert_at_cursor:
738 * @buffer: a #GtkTextBuffer
739 * @text: some text in UTF-8 format
740 * @len: length of text, in bytes
742 * Simply calls gtk_text_buffer_insert (), using the current
743 * cursor position as the insertion point.
746 gtk_text_buffer_insert_at_cursor (GtkTextBuffer *buffer,
752 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
753 g_return_if_fail (text != NULL);
755 gtk_text_buffer_get_iter_at_mark (buffer, &iter,
756 gtk_text_buffer_get_mark (buffer,
759 gtk_text_buffer_insert (buffer, &iter, text, len);
763 * gtk_text_buffer_insert_interactive:
764 * @buffer: a #GtkTextBuffer
765 * @iter: a position in @buffer
766 * @text: some UTF-8 text
767 * @len: length of text in bytes, or -1
768 * @default_editable: default editability of buffer
770 * Like gtk_text_buffer_insert(), but the insertion will not occur if
771 * @iter is at a non-editable location in the buffer. Usually you
772 * want to prevent insertions at ineditable locations if the insertion
773 * results from a user action (is interactive).
775 * @default_editable indicates the editability of text that doesn't
776 * have a tag affecting editability applied to it. Typically the
777 * result of gtk_text_view_get_editable() is appropriate here.
779 * Return value: whether text was actually inserted
782 gtk_text_buffer_insert_interactive (GtkTextBuffer *buffer,
786 gboolean default_editable)
788 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
789 g_return_val_if_fail (text != NULL, FALSE);
790 g_return_val_if_fail (gtk_text_iter_get_buffer (iter) == buffer, FALSE);
792 if (gtk_text_iter_can_insert (iter, default_editable))
794 gtk_text_buffer_begin_user_action (buffer);
795 gtk_text_buffer_emit_insert (buffer, iter, text, len);
796 gtk_text_buffer_end_user_action (buffer);
804 * gtk_text_buffer_insert_interactive_at_cursor:
805 * @buffer: a #GtkTextBuffer
806 * @text: text in UTF-8 format
807 * @len: length of text in bytes, or -1
808 * @default_editable: default editability of buffer
810 * Calls gtk_text_buffer_insert_interactive () at the cursor
813 * @default_editable indicates the editability of text that doesn't
814 * have a tag affecting editability applied to it. Typically the
815 * result of gtk_text_view_get_editable() is appropriate here.
817 * Return value: whether text was actually inserted
820 gtk_text_buffer_insert_interactive_at_cursor (GtkTextBuffer *buffer,
823 gboolean default_editable)
827 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
828 g_return_val_if_fail (text != NULL, FALSE);
830 gtk_text_buffer_get_iter_at_mark (buffer, &iter,
831 gtk_text_buffer_get_mark (buffer,
834 return gtk_text_buffer_insert_interactive (buffer, &iter, text, len,
839 possibly_not_text (gunichar ch,
842 return ch == GTK_TEXT_UNKNOWN_CHAR;
846 insert_text_range (GtkTextBuffer *buffer,
848 const GtkTextIter *orig_start,
849 const GtkTextIter *orig_end,
850 gboolean interactive)
854 text = gtk_text_iter_get_text (orig_start, orig_end);
856 gtk_text_buffer_emit_insert (buffer, iter, text, -1);
861 typedef struct _Range Range;
864 GtkTextBuffer *buffer;
865 GtkTextMark *start_mark;
866 GtkTextMark *end_mark;
867 GtkTextMark *whole_end_mark;
868 GtkTextIter *range_start;
869 GtkTextIter *range_end;
870 GtkTextIter *whole_end;
874 save_range (GtkTextIter *range_start,
875 GtkTextIter *range_end,
876 GtkTextIter *whole_end)
880 r = g_new (Range, 1);
882 r->buffer = gtk_text_iter_get_buffer (range_start);
883 g_object_ref (r->buffer);
886 gtk_text_buffer_create_mark (gtk_text_iter_get_buffer (range_start),
891 gtk_text_buffer_create_mark (gtk_text_iter_get_buffer (range_start),
897 gtk_text_buffer_create_mark (gtk_text_iter_get_buffer (range_start),
902 r->range_start = range_start;
903 r->range_end = range_end;
904 r->whole_end = whole_end;
910 restore_range (Range *r)
912 gtk_text_buffer_get_iter_at_mark (r->buffer,
916 gtk_text_buffer_get_iter_at_mark (r->buffer,
920 gtk_text_buffer_get_iter_at_mark (r->buffer,
924 gtk_text_buffer_delete_mark (r->buffer, r->start_mark);
925 gtk_text_buffer_delete_mark (r->buffer, r->end_mark);
926 gtk_text_buffer_delete_mark (r->buffer, r->whole_end_mark);
928 /* Due to the gravities on the marks, the ordering could have
929 * gotten mangled; we switch to an empty range in that
933 if (gtk_text_iter_compare (r->range_start, r->range_end) > 0)
934 *r->range_start = *r->range_end;
936 if (gtk_text_iter_compare (r->range_end, r->whole_end) > 0)
937 *r->range_end = *r->whole_end;
939 g_object_unref (r->buffer);
944 insert_range_untagged (GtkTextBuffer *buffer,
946 const GtkTextIter *orig_start,
947 const GtkTextIter *orig_end,
948 gboolean interactive)
950 GtkTextIter range_start;
951 GtkTextIter range_end;
952 GtkTextIter start, end;
955 if (gtk_text_iter_equal (orig_start, orig_end))
966 if (gtk_text_iter_equal (&range_start, &range_end))
968 /* Figure out how to move forward */
970 g_assert (gtk_text_iter_compare (&range_end, &end) <= 0);
972 if (gtk_text_iter_equal (&range_end, &end))
974 /* nothing left to do */
977 else if (gtk_text_iter_get_char (&range_end) == GTK_TEXT_UNKNOWN_CHAR)
979 GdkPixbuf *pixbuf = NULL;
980 GtkTextChildAnchor *anchor = NULL;
981 pixbuf = gtk_text_iter_get_pixbuf (&range_end);
982 anchor = gtk_text_iter_get_child_anchor (&range_end);
986 r = save_range (&range_start,
990 gtk_text_buffer_insert_pixbuf (buffer,
997 gtk_text_iter_forward_char (&range_end);
999 range_start = range_end;
1003 /* Just skip anchors */
1005 gtk_text_iter_forward_char (&range_end);
1006 range_start = range_end;
1010 /* The GTK_TEXT_UNKNOWN_CHAR was in a text segment, so
1013 gtk_text_iter_forward_find_char (&range_end,
1014 possibly_not_text, NULL,
1017 g_assert (gtk_text_iter_compare (&range_end, &end) <= 0);
1022 /* Text segment starts here, so forward search to
1023 * find its possible endpoint
1025 gtk_text_iter_forward_find_char (&range_end,
1026 possibly_not_text, NULL,
1029 g_assert (gtk_text_iter_compare (&range_end, &end) <= 0);
1034 r = save_range (&range_start,
1038 insert_text_range (buffer,
1047 range_start = range_end;
1053 insert_range_not_inside_self (GtkTextBuffer *buffer,
1055 const GtkTextIter *orig_start,
1056 const GtkTextIter *orig_end,
1057 gboolean interactive)
1059 /* Find each range of uniformly-tagged text, insert it,
1060 * then apply the tags.
1062 GtkTextIter start = *orig_start;
1063 GtkTextIter end = *orig_end;
1064 GtkTextIter range_start;
1065 GtkTextIter range_end;
1067 if (gtk_text_iter_equal (orig_start, orig_end))
1070 gtk_text_iter_order (&start, &end);
1072 range_start = start;
1078 GtkTextIter start_iter;
1083 if (gtk_text_iter_equal (&range_start, &end))
1084 break; /* All done */
1086 g_assert (gtk_text_iter_compare (&range_start, &end) < 0);
1088 gtk_text_iter_forward_to_tag_toggle (&range_end, NULL);
1090 g_assert (!gtk_text_iter_equal (&range_start, &range_end));
1092 /* Clamp to the end iterator */
1093 if (gtk_text_iter_compare (&range_end, &end) > 0)
1096 /* We have a range with unique tags; insert it, and
1099 start_offset = gtk_text_iter_get_offset (iter);
1101 r = save_range (&range_start, &range_end, &end);
1103 insert_range_untagged (buffer, iter, &range_start, &range_end, interactive);
1108 gtk_text_buffer_get_iter_at_offset (buffer, &start_iter, start_offset);
1110 tags = gtk_text_iter_get_tags (&range_start);
1112 while (tmp_list != NULL)
1114 gtk_text_buffer_apply_tag (buffer,
1119 tmp_list = g_slist_next (tmp_list);
1121 g_slist_free (tags);
1123 range_start = range_end;
1128 gtk_text_buffer_real_insert_range (GtkTextBuffer *buffer,
1130 const GtkTextIter *orig_start,
1131 const GtkTextIter *orig_end,
1132 gboolean interactive)
1134 GtkTextBuffer *src_buffer;
1136 /* Find each range of uniformly-tagged text, insert it,
1137 * then apply the tags.
1139 if (gtk_text_iter_equal (orig_start, orig_end))
1143 gtk_text_buffer_begin_user_action (buffer);
1145 src_buffer = gtk_text_iter_get_buffer (orig_start);
1147 if (gtk_text_iter_get_buffer (iter) != src_buffer ||
1148 !gtk_text_iter_in_range (iter, orig_start, orig_end))
1150 insert_range_not_inside_self (buffer, iter, orig_start, orig_end, interactive);
1154 /* If you insert a range into itself, it could loop infinitely
1155 * because the region being copied keeps growing as we insert. So
1156 * we have to separately copy the range before and after
1157 * the insertion point.
1159 GtkTextIter start = *orig_start;
1160 GtkTextIter end = *orig_end;
1161 GtkTextIter range_start;
1162 GtkTextIter range_end;
1166 gtk_text_iter_order (&start, &end);
1168 range_start = start;
1170 first_half = save_range (&range_start, &range_end, &end);
1172 range_start = *iter;
1174 second_half = save_range (&range_start, &range_end, &end);
1176 restore_range (first_half);
1177 insert_range_not_inside_self (buffer, iter, &range_start, &range_end, interactive);
1179 restore_range (second_half);
1180 insert_range_not_inside_self (buffer, iter, &range_start, &range_end, interactive);
1184 gtk_text_buffer_end_user_action (buffer);
1188 * gtk_text_buffer_insert_range:
1189 * @buffer: a #GtkTextBuffer
1190 * @iter: a position in @buffer
1191 * @start: a position in a #GtkTextBuffer
1192 * @end: another position in the same buffer as @start
1194 * Copies text, tags, and pixbufs between @start and @end (the order
1195 * of @start and @end doesn't matter) and inserts the copy at @iter.
1196 * Used instead of simply getting/inserting text because it preserves
1197 * images and tags. If @start and @end are in a different buffer from
1198 * @buffer, the two buffers must share the same tag table.
1200 * Implemented via emissions of the insert_text and apply_tag signals,
1204 gtk_text_buffer_insert_range (GtkTextBuffer *buffer,
1206 const GtkTextIter *start,
1207 const GtkTextIter *end)
1209 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1210 g_return_if_fail (iter != NULL);
1211 g_return_if_fail (start != NULL);
1212 g_return_if_fail (end != NULL);
1213 g_return_if_fail (gtk_text_iter_get_buffer (start) ==
1214 gtk_text_iter_get_buffer (end));
1215 g_return_if_fail (gtk_text_iter_get_buffer (start)->tag_table ==
1217 g_return_if_fail (gtk_text_iter_get_buffer (iter) == buffer);
1219 gtk_text_buffer_real_insert_range (buffer, iter, start, end, FALSE);
1223 * gtk_text_buffer_insert_range_interactive:
1224 * @buffer: a #GtkTextBuffer
1225 * @iter: a position in @buffer
1226 * @start: a position in a #GtkTextBuffer
1227 * @end: another position in the same buffer as @start
1228 * @default_editable: default editability of the buffer
1230 * Same as gtk_text_buffer_insert_range(), but does nothing if the
1231 * insertion point isn't editable. The @default_editable parameter
1232 * indicates whether the text is editable at @iter if no tags
1233 * enclosing @iter affect editability. Typically the result of
1234 * gtk_text_view_get_editable() is appropriate here.
1236 * Returns: whether an insertion was possible at @iter
1239 gtk_text_buffer_insert_range_interactive (GtkTextBuffer *buffer,
1241 const GtkTextIter *start,
1242 const GtkTextIter *end,
1243 gboolean default_editable)
1245 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
1246 g_return_val_if_fail (iter != NULL, FALSE);
1247 g_return_val_if_fail (start != NULL, FALSE);
1248 g_return_val_if_fail (end != NULL, FALSE);
1249 g_return_val_if_fail (gtk_text_iter_get_buffer (start) ==
1250 gtk_text_iter_get_buffer (end), FALSE);
1251 g_return_val_if_fail (gtk_text_iter_get_buffer (start)->tag_table ==
1252 buffer->tag_table, FALSE);
1255 if (gtk_text_iter_can_insert (iter, default_editable))
1257 gtk_text_buffer_real_insert_range (buffer, iter, start, end, TRUE);
1265 * gtk_text_buffer_insert_with_tags:
1266 * @buffer: a #GtkTextBuffer
1267 * @iter: an iterator in @buffer
1269 * @len: length of @text, or -1
1270 * @first_tag: first tag to apply to @text
1271 * @Varargs: NULL-terminated list of tags to apply
1273 * Inserts @text into @buffer at @iter, applying the list of tags to
1274 * the newly-inserted text. The last tag specified must be NULL to
1275 * terminate the list. Equivalent to calling gtk_text_buffer_insert (),
1276 * then gtk_text_buffer_apply_tag () on the inserted text;
1277 * gtk_text_buffer_insert_with_tags () is just a convenience function.
1280 gtk_text_buffer_insert_with_tags (GtkTextBuffer *buffer,
1284 GtkTextTag *first_tag,
1292 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1293 g_return_if_fail (iter != NULL);
1294 g_return_if_fail (text != NULL);
1295 g_return_if_fail (gtk_text_iter_get_buffer (iter) == buffer);
1297 start_offset = gtk_text_iter_get_offset (iter);
1299 gtk_text_buffer_insert (buffer, iter, text, len);
1301 if (first_tag == NULL)
1304 gtk_text_buffer_get_iter_at_offset (buffer, &start, start_offset);
1306 va_start (args, first_tag);
1310 gtk_text_buffer_apply_tag (buffer, tag, &start, iter);
1312 tag = va_arg (args, GtkTextTag*);
1319 * gtk_text_buffer_insert_with_tags_by_name:
1320 * @buffer: a #GtkTextBuffer
1321 * @iter: position in @buffer
1323 * @len: length of @text, or -1
1324 * @first_tag_name: name of a tag to apply to @text
1325 * @Varargs: more tag names
1327 * Same as gtk_text_buffer_insert_with_tags (), but allows you
1328 * to pass in tag names instead of tag objects.
1331 gtk_text_buffer_insert_with_tags_by_name (GtkTextBuffer *buffer,
1335 const gchar *first_tag_name,
1341 const gchar *tag_name;
1343 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1344 g_return_if_fail (iter != NULL);
1345 g_return_if_fail (text != NULL);
1346 g_return_if_fail (gtk_text_iter_get_buffer (iter) == buffer);
1348 start_offset = gtk_text_iter_get_offset (iter);
1350 gtk_text_buffer_insert (buffer, iter, text, len);
1352 if (first_tag_name == NULL)
1355 gtk_text_buffer_get_iter_at_offset (buffer, &start, start_offset);
1357 va_start (args, first_tag_name);
1358 tag_name = first_tag_name;
1363 tag = gtk_text_tag_table_lookup (buffer->tag_table,
1368 g_warning ("%s: no tag with name '%s'!", G_STRLOC, tag_name);
1372 gtk_text_buffer_apply_tag (buffer, tag, &start, iter);
1374 tag_name = va_arg (args, const gchar*);
1386 gtk_text_buffer_real_delete_range (GtkTextBuffer *buffer,
1390 gboolean has_selection;
1392 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1393 g_return_if_fail (start != NULL);
1394 g_return_if_fail (end != NULL);
1396 _gtk_text_btree_delete (start, end);
1398 /* may have deleted the selection... */
1399 update_selection_clipboards (buffer);
1401 has_selection = gtk_text_buffer_get_selection_bounds (buffer, NULL, NULL);
1402 if (has_selection != buffer->has_selection)
1404 buffer->has_selection = has_selection;
1405 g_object_notify (G_OBJECT (buffer), "has-selection");
1408 g_signal_emit (buffer, signals[CHANGED], 0);
1409 g_object_notify (G_OBJECT (buffer), "cursor-position");
1413 gtk_text_buffer_emit_delete (GtkTextBuffer *buffer,
1417 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1418 g_return_if_fail (start != NULL);
1419 g_return_if_fail (end != NULL);
1421 if (gtk_text_iter_equal (start, end))
1424 gtk_text_iter_order (start, end);
1426 g_signal_emit (buffer,
1427 signals[DELETE_RANGE],
1433 * gtk_text_buffer_delete:
1434 * @buffer: a #GtkTextBuffer
1435 * @start: a position in @buffer
1436 * @end: another position in @buffer
1438 * Deletes text between @start and @end. The order of @start and @end
1439 * is not actually relevant; gtk_text_buffer_delete () will reorder
1440 * them. This function actually emits the "delete_range" signal, and
1441 * the default handler of that signal deletes the text. Because the
1442 * buffer is modified, all outstanding iterators become invalid after
1443 * calling this function; however, the @start and @end will be
1444 * re-initialized to point to the location where text was deleted.
1448 gtk_text_buffer_delete (GtkTextBuffer *buffer,
1452 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1453 g_return_if_fail (start != NULL);
1454 g_return_if_fail (end != NULL);
1455 g_return_if_fail (gtk_text_iter_get_buffer (start) == buffer);
1456 g_return_if_fail (gtk_text_iter_get_buffer (end) == buffer);
1458 gtk_text_buffer_emit_delete (buffer, start, end);
1462 * gtk_text_buffer_delete_interactive:
1463 * @buffer: a #GtkTextBuffer
1464 * @start_iter: start of range to delete
1465 * @end_iter: end of range
1466 * @default_editable: whether the buffer is editable by default
1468 * Deletes all <emphasis>editable</emphasis> text in the given range.
1469 * Calls gtk_text_buffer_delete () for each editable sub-range of
1470 * [@start,@end). @start and @end are revalidated to point to
1471 * the location of the last deleted range, or left untouched if
1472 * no text was deleted.
1474 * Return value: whether some text was actually deleted
1477 gtk_text_buffer_delete_interactive (GtkTextBuffer *buffer,
1478 GtkTextIter *start_iter,
1479 GtkTextIter *end_iter,
1480 gboolean default_editable)
1482 GtkTextMark *end_mark;
1483 GtkTextMark *start_mark;
1485 gboolean current_state;
1486 gboolean deleted_stuff = FALSE;
1488 /* Delete all editable text in the range start_iter, end_iter */
1490 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
1491 g_return_val_if_fail (start_iter != NULL, FALSE);
1492 g_return_val_if_fail (end_iter != NULL, FALSE);
1493 g_return_val_if_fail (gtk_text_iter_get_buffer (start_iter) == buffer, FALSE);
1494 g_return_val_if_fail (gtk_text_iter_get_buffer (end_iter) == buffer, FALSE);
1497 gtk_text_buffer_begin_user_action (buffer);
1499 gtk_text_iter_order (start_iter, end_iter);
1501 start_mark = gtk_text_buffer_create_mark (buffer, NULL,
1503 end_mark = gtk_text_buffer_create_mark (buffer, NULL,
1506 gtk_text_buffer_get_iter_at_mark (buffer, &iter, start_mark);
1508 current_state = gtk_text_iter_editable (&iter, default_editable);
1513 gboolean done = FALSE;
1516 gtk_text_iter_forward_to_tag_toggle (&iter, NULL);
1518 gtk_text_buffer_get_iter_at_mark (buffer, &end, end_mark);
1520 if (gtk_text_iter_compare (&iter, &end) >= 0)
1523 iter = end; /* clamp to the last boundary */
1526 new_state = gtk_text_iter_editable (&iter, default_editable);
1528 if (current_state == new_state)
1534 /* We're ending an editable region. Delete said region. */
1537 gtk_text_buffer_get_iter_at_mark (buffer, &start, start_mark);
1539 gtk_text_buffer_emit_delete (buffer, &start, &iter);
1541 deleted_stuff = TRUE;
1543 /* revalidate user's iterators. */
1544 *start_iter = start;
1554 if (current_state && !new_state)
1556 /* End of an editable region. Delete it. */
1559 gtk_text_buffer_get_iter_at_mark (buffer, &start, start_mark);
1561 gtk_text_buffer_emit_delete (buffer, &start, &iter);
1563 current_state = FALSE;
1564 deleted_stuff = TRUE;
1566 /* revalidate user's iterators. */
1567 *start_iter = start;
1572 /* We are at the start of an editable region. We won't be deleting
1573 * the previous region. Move start mark to start of this region.
1576 g_assert (!current_state && new_state);
1578 gtk_text_buffer_move_mark (buffer, start_mark,
1582 current_state = TRUE;
1590 gtk_text_buffer_delete_mark (buffer, start_mark);
1591 gtk_text_buffer_delete_mark (buffer, end_mark);
1593 gtk_text_buffer_end_user_action (buffer);
1595 return deleted_stuff;
1599 * Extracting textual buffer contents
1603 * gtk_text_buffer_get_text:
1604 * @buffer: a #GtkTextBuffer
1605 * @start: start of a range
1606 * @end: end of a range
1607 * @include_hidden_chars: whether to include invisible text
1609 * Returns the text in the range [@start,@end). Excludes undisplayed
1610 * text (text marked with tags that set the invisibility attribute) if
1611 * @include_hidden_chars is %FALSE. Does not include characters
1612 * representing embedded images, so byte and character indexes into
1613 * the returned string do <emphasis>not</emphasis> correspond to byte
1614 * and character indexes into the buffer. Contrast with
1615 * gtk_text_buffer_get_slice ().
1617 * Return value: an allocated UTF-8 string
1620 gtk_text_buffer_get_text (GtkTextBuffer *buffer,
1621 const GtkTextIter *start,
1622 const GtkTextIter *end,
1623 gboolean include_hidden_chars)
1625 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
1626 g_return_val_if_fail (start != NULL, NULL);
1627 g_return_val_if_fail (end != NULL, NULL);
1628 g_return_val_if_fail (gtk_text_iter_get_buffer (start) == buffer, NULL);
1629 g_return_val_if_fail (gtk_text_iter_get_buffer (end) == buffer, NULL);
1631 if (include_hidden_chars)
1632 return gtk_text_iter_get_text (start, end);
1634 return gtk_text_iter_get_visible_text (start, end);
1638 * gtk_text_buffer_get_slice:
1639 * @buffer: a #GtkTextBuffer
1640 * @start: start of a range
1641 * @end: end of a range
1642 * @include_hidden_chars: whether to include invisible text
1644 * Returns the text in the range [@start,@end). Excludes undisplayed
1645 * text (text marked with tags that set the invisibility attribute) if
1646 * @include_hidden_chars is %FALSE. The returned string includes a
1647 * 0xFFFC character whenever the buffer contains
1648 * embedded images, so byte and character indexes into
1649 * the returned string <emphasis>do</emphasis> correspond to byte
1650 * and character indexes into the buffer. Contrast with
1651 * gtk_text_buffer_get_text (). Note that 0xFFFC can occur in normal
1652 * text as well, so it is not a reliable indicator that a pixbuf or
1653 * widget is in the buffer.
1655 * Return value: an allocated UTF-8 string
1658 gtk_text_buffer_get_slice (GtkTextBuffer *buffer,
1659 const GtkTextIter *start,
1660 const GtkTextIter *end,
1661 gboolean include_hidden_chars)
1663 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
1664 g_return_val_if_fail (start != NULL, NULL);
1665 g_return_val_if_fail (end != NULL, NULL);
1666 g_return_val_if_fail (gtk_text_iter_get_buffer (start) == buffer, NULL);
1667 g_return_val_if_fail (gtk_text_iter_get_buffer (end) == buffer, NULL);
1669 if (include_hidden_chars)
1670 return gtk_text_iter_get_slice (start, end);
1672 return gtk_text_iter_get_visible_slice (start, end);
1680 gtk_text_buffer_real_insert_pixbuf (GtkTextBuffer *buffer,
1684 _gtk_text_btree_insert_pixbuf (iter, pixbuf);
1686 g_signal_emit (buffer, signals[CHANGED], 0);
1690 * gtk_text_buffer_insert_pixbuf:
1691 * @buffer: a #GtkTextBuffer
1692 * @iter: location to insert the pixbuf
1693 * @pixbuf: a #GdkPixbuf
1695 * Inserts an image into the text buffer at @iter. The image will be
1696 * counted as one character in character counts, and when obtaining
1697 * the buffer contents as a string, will be represented by the Unicode
1698 * "object replacement character" 0xFFFC. Note that the "slice"
1699 * variants for obtaining portions of the buffer as a string include
1700 * this character for pixbufs, but the "text" variants do
1701 * not. e.g. see gtk_text_buffer_get_slice() and
1702 * gtk_text_buffer_get_text().
1706 gtk_text_buffer_insert_pixbuf (GtkTextBuffer *buffer,
1710 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1711 g_return_if_fail (iter != NULL);
1712 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
1713 g_return_if_fail (gtk_text_iter_get_buffer (iter) == buffer);
1715 g_signal_emit (buffer, signals[INSERT_PIXBUF], 0,
1725 gtk_text_buffer_real_insert_anchor (GtkTextBuffer *buffer,
1727 GtkTextChildAnchor *anchor)
1729 _gtk_text_btree_insert_child_anchor (iter, anchor);
1731 g_signal_emit (buffer, signals[CHANGED], 0);
1735 * gtk_text_buffer_insert_child_anchor:
1736 * @buffer: a #GtkTextBuffer
1737 * @iter: location to insert the anchor
1738 * @anchor: a #GtkTextChildAnchor
1740 * Inserts a child widget anchor into the text buffer at @iter. The
1741 * anchor will be counted as one character in character counts, and
1742 * when obtaining the buffer contents as a string, will be represented
1743 * by the Unicode "object replacement character" 0xFFFC. Note that the
1744 * "slice" variants for obtaining portions of the buffer as a string
1745 * include this character for child anchors, but the "text" variants do
1746 * not. e.g. see gtk_text_buffer_get_slice() and
1747 * gtk_text_buffer_get_text(). Consider
1748 * gtk_text_buffer_create_child_anchor() as a more convenient
1749 * alternative to this function. The buffer will add a reference to
1750 * the anchor, so you can unref it after insertion.
1754 gtk_text_buffer_insert_child_anchor (GtkTextBuffer *buffer,
1756 GtkTextChildAnchor *anchor)
1758 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1759 g_return_if_fail (iter != NULL);
1760 g_return_if_fail (GTK_IS_TEXT_CHILD_ANCHOR (anchor));
1761 g_return_if_fail (gtk_text_iter_get_buffer (iter) == buffer);
1763 g_signal_emit (buffer, signals[INSERT_CHILD_ANCHOR], 0,
1769 * gtk_text_buffer_create_child_anchor:
1770 * @buffer: a #GtkTextBuffer
1771 * @iter: location in the buffer
1773 * This is a convenience function which simply creates a child anchor
1774 * with gtk_text_child_anchor_new() and inserts it into the buffer
1775 * with gtk_text_buffer_insert_child_anchor(). The new anchor is
1776 * owned by the buffer; no reference count is returned to
1777 * the caller of gtk_text_buffer_create_child_anchor().
1779 * Return value: the created child anchor
1782 gtk_text_buffer_create_child_anchor (GtkTextBuffer *buffer,
1785 GtkTextChildAnchor *anchor;
1787 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
1788 g_return_val_if_fail (iter != NULL, NULL);
1789 g_return_val_if_fail (gtk_text_iter_get_buffer (iter) == buffer, NULL);
1791 anchor = gtk_text_child_anchor_new ();
1793 gtk_text_buffer_insert_child_anchor (buffer, iter, anchor);
1795 g_object_unref (anchor);
1805 gtk_text_buffer_mark_set (GtkTextBuffer *buffer,
1806 const GtkTextIter *location,
1809 /* IMO this should NOT work like insert_text and delete_range,
1810 * where the real action happens in the default handler.
1812 * The reason is that the default handler would be _required_,
1813 * i.e. the whole widget would start breaking and segfaulting if the
1814 * default handler didn't get run. So you can't really override the
1815 * default handler or stop the emission; that is, this signal is
1816 * purely for notification, and not to allow users to modify the
1820 g_object_ref (mark);
1822 g_signal_emit (buffer,
1828 g_object_unref (mark);
1832 * gtk_text_buffer_set_mark:
1833 * @buffer: a #GtkTextBuffer
1834 * @mark_name: name of the mark
1835 * @iter: location for the mark.
1836 * @left_gravity: if the mark is created by this function, gravity for the new
1838 * @should_exist: if %TRUE, warn if the mark does not exist, and return
1841 * Move the mark to the given position, if not @should_exist, create the mark.
1843 * Return value: mark
1846 gtk_text_buffer_set_mark (GtkTextBuffer *buffer,
1847 GtkTextMark *existing_mark,
1848 const gchar *mark_name,
1849 const GtkTextIter *iter,
1850 gboolean left_gravity,
1851 gboolean should_exist)
1853 GtkTextIter location;
1856 g_return_val_if_fail (gtk_text_iter_get_buffer (iter) == buffer, NULL);
1858 mark = _gtk_text_btree_set_mark (get_btree (buffer),
1865 _gtk_text_btree_get_iter_at_mark (get_btree (buffer),
1869 gtk_text_buffer_mark_set (buffer, &location, mark);
1875 * gtk_text_buffer_create_mark:
1876 * @buffer: a #GtkTextBuffer
1877 * @mark_name: name for mark, or %NULL
1878 * @where: location to place mark
1879 * @left_gravity: whether the mark has left gravity
1881 * Creates a mark at position @where. If @mark_name is %NULL, the mark
1882 * is anonymous; otherwise, the mark can be retrieved by name using
1883 * gtk_text_buffer_get_mark (). If a mark has left gravity, and text is
1884 * inserted at the mark's current location, the mark will be moved to
1885 * the left of the newly-inserted text. If the mark has right gravity
1886 * (@left_gravity = %FALSE), the mark will end up on the right of
1887 * newly-inserted text. The standard left-to-right cursor is a mark
1888 * with right gravity (when you type, the cursor stays on the right
1889 * side of the text you're typing).
1891 * The caller of this function does <emphasis>not</emphasis> own a reference
1892 * to the returned #GtkTextMark, so you can ignore the return value
1893 * if you like. Marks are owned by the buffer and go away when the
1896 * Emits the "mark_set" signal as notification of the mark's initial
1899 * Return value: the new #GtkTextMark object
1902 gtk_text_buffer_create_mark (GtkTextBuffer *buffer,
1903 const gchar *mark_name,
1904 const GtkTextIter *where,
1905 gboolean left_gravity)
1907 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
1909 return gtk_text_buffer_set_mark (buffer, NULL, mark_name, where,
1910 left_gravity, FALSE);
1914 * gtk_text_buffer_move_mark:
1915 * @buffer: a #GtkTextBuffer
1916 * @mark: a #GtkTextMark
1917 * @where: new location for @mark in @buffer
1919 * Moves @mark to the new location @where. Emits the "mark_set" signal
1920 * as notification of the move.
1923 gtk_text_buffer_move_mark (GtkTextBuffer *buffer,
1925 const GtkTextIter *where)
1927 g_return_if_fail (GTK_IS_TEXT_MARK (mark));
1928 g_return_if_fail (!gtk_text_mark_get_deleted (mark));
1929 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1931 gtk_text_buffer_set_mark (buffer, mark, NULL, where, FALSE, TRUE);
1935 * gtk_text_buffer_get_iter_at_mark:
1936 * @buffer: a #GtkTextBuffer
1937 * @iter: iterator to initialize
1938 * @mark: a #GtkTextMark in @buffer
1940 * Initializes @iter with the current position of @mark.
1943 gtk_text_buffer_get_iter_at_mark (GtkTextBuffer *buffer,
1947 g_return_if_fail (GTK_IS_TEXT_MARK (mark));
1948 g_return_if_fail (!gtk_text_mark_get_deleted (mark));
1949 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1951 _gtk_text_btree_get_iter_at_mark (get_btree (buffer),
1957 * gtk_text_buffer_delete_mark:
1958 * @buffer: a #GtkTextBuffer
1959 * @mark: a #GtkTextMark in @buffer
1961 * Deletes @mark, so that it's no longer located anywhere in the
1962 * buffer. Removes the reference the buffer holds to the mark, so if
1963 * you haven't called g_object_ref () on the mark, it will be freed. Even
1964 * if the mark isn't freed, most operations on @mark become
1965 * invalid. There is no way to undelete a
1966 * mark. gtk_text_mark_get_deleted () will return TRUE after this
1967 * function has been called on a mark; gtk_text_mark_get_deleted ()
1968 * indicates that a mark no longer belongs to a buffer. The "mark_deleted"
1969 * signal will be emitted as notification after the mark is deleted.
1972 gtk_text_buffer_delete_mark (GtkTextBuffer *buffer,
1975 g_return_if_fail (GTK_IS_TEXT_MARK (mark));
1976 g_return_if_fail (!gtk_text_mark_get_deleted (mark));
1977 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1979 g_object_ref (mark);
1981 _gtk_text_btree_remove_mark (get_btree (buffer), mark);
1983 /* See rationale above for MARK_SET on why we emit this after
1984 * removing the mark, rather than removing the mark in a default
1987 g_signal_emit (buffer, signals[MARK_DELETED],
1991 g_object_unref (mark);
1995 * gtk_text_buffer_get_mark:
1996 * @buffer: a #GtkTextBuffer
1997 * @name: a mark name
1999 * Returns the mark named @name in buffer @buffer, or NULL if no such
2000 * mark exists in the buffer.
2002 * Return value: a #GtkTextMark, or NULL
2005 gtk_text_buffer_get_mark (GtkTextBuffer *buffer,
2010 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
2011 g_return_val_if_fail (name != NULL, NULL);
2013 mark = _gtk_text_btree_get_mark_by_name (get_btree (buffer), name);
2020 * gtk_text_buffer_move_mark_by_name:
2021 * @buffer: a #GtkTextBuffer
2022 * @name: name of a mark
2023 * @where: new location for mark
2025 * Moves the mark named @name (which must exist) to location @where.
2026 * See gtk_text_buffer_move_mark () for details.
2029 gtk_text_buffer_move_mark_by_name (GtkTextBuffer *buffer,
2031 const GtkTextIter *where)
2035 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2036 g_return_if_fail (name != NULL);
2038 mark = _gtk_text_btree_get_mark_by_name (get_btree (buffer), name);
2042 g_warning ("%s: no mark named '%s'", G_STRLOC, name);
2046 gtk_text_buffer_move_mark (buffer, mark, where);
2050 * gtk_text_buffer_delete_mark_by_name:
2051 * @buffer: a #GtkTextBuffer
2052 * @name: name of a mark in @buffer
2054 * Deletes the mark named @name; the mark must exist. See
2055 * gtk_text_buffer_delete_mark () for details.
2058 gtk_text_buffer_delete_mark_by_name (GtkTextBuffer *buffer,
2063 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2064 g_return_if_fail (name != NULL);
2066 mark = _gtk_text_btree_get_mark_by_name (get_btree (buffer), name);
2070 g_warning ("%s: no mark named '%s'", G_STRLOC, name);
2074 gtk_text_buffer_delete_mark (buffer, mark);
2078 * gtk_text_buffer_get_insert:
2079 * @buffer: a #GtkTextBuffer
2081 * Returns the mark that represents the cursor (insertion point).
2082 * Equivalent to calling gtk_text_buffer_get_mark () to get the mark
2083 * named "insert", but very slightly more efficient, and involves less
2086 * Return value: insertion point mark
2089 gtk_text_buffer_get_insert (GtkTextBuffer *buffer)
2091 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
2093 /* FIXME use struct member in btree */
2094 return gtk_text_buffer_get_mark (buffer, "insert");
2098 * gtk_text_buffer_get_selection_bound:
2099 * @buffer: a #GtkTextBuffer
2101 * Returns the mark that represents the selection bound. Equivalent
2102 * to calling gtk_text_buffer_get_mark () to get the mark named
2103 * "selection_bound", but very slightly more efficient, and involves
2106 * The currently-selected text in @buffer is the region between the
2107 * "selection_bound" and "insert" marks. If "selection_bound" and
2108 * "insert" are in the same place, then there is no current selection.
2109 * gtk_text_buffer_get_selection_bounds() is another convenient function
2110 * for handling the selection, if you just want to know whether there's a
2111 * selection and what its bounds are.
2113 * Return value: selection bound mark
2116 gtk_text_buffer_get_selection_bound (GtkTextBuffer *buffer)
2118 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
2120 /* FIXME use struct member in btree */
2121 return gtk_text_buffer_get_mark (buffer, "selection_bound");
2125 * gtk_text_buffer_get_iter_at_child_anchor:
2126 * @buffer: a #GtkTextBuffer
2127 * @iter: an iterator to be initialized
2128 * @anchor: a child anchor that appears in @buffer
2130 * Obtains the location of @anchor within @buffer.
2134 gtk_text_buffer_get_iter_at_child_anchor (GtkTextBuffer *buffer,
2136 GtkTextChildAnchor *anchor)
2138 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2139 g_return_if_fail (iter != NULL);
2140 g_return_if_fail (GTK_IS_TEXT_CHILD_ANCHOR (anchor));
2141 g_return_if_fail (!gtk_text_child_anchor_get_deleted (anchor));
2143 _gtk_text_btree_get_iter_at_child_anchor (get_btree (buffer),
2149 * gtk_text_buffer_place_cursor:
2150 * @buffer: a #GtkTextBuffer
2151 * @where: where to put the cursor
2153 * This function moves the "insert" and "selection_bound" marks
2154 * simultaneously. If you move them to the same place in two steps
2155 * with gtk_text_buffer_move_mark(), you will temporarily select a
2156 * region in between their old and new locations, which can be pretty
2157 * inefficient since the temporarily-selected region will force stuff
2158 * to be recalculated. This function moves them as a unit, which can
2162 gtk_text_buffer_place_cursor (GtkTextBuffer *buffer,
2163 const GtkTextIter *where)
2165 gtk_text_buffer_select_range (buffer, where, where);
2170 * gtk_text_buffer_select_range:
2171 * @buffer: a #GtkTextBuffer
2172 * @ins: where to put the "insert" mark
2173 * @bound: where to put the "selection_bound" mark
2175 * This function moves the "insert" and "selection_bound" marks
2176 * simultaneously. If you move them in two steps
2177 * with gtk_text_buffer_move_mark(), you will temporarily select a
2178 * region in between their old and new locations, which can be pretty
2179 * inefficient since the temporarily-selected region will force stuff
2180 * to be recalculated. This function moves them as a unit, which can
2186 gtk_text_buffer_select_range (GtkTextBuffer *buffer,
2187 const GtkTextIter *ins,
2188 const GtkTextIter *bound)
2190 GtkTextIter real_ins;
2191 GtkTextIter real_bound;
2193 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2196 real_bound = *bound;
2198 _gtk_text_btree_select_range (get_btree (buffer), &real_ins, &real_bound);
2199 gtk_text_buffer_mark_set (buffer, &real_ins,
2200 gtk_text_buffer_get_mark (buffer,
2202 gtk_text_buffer_mark_set (buffer, &real_bound,
2203 gtk_text_buffer_get_mark (buffer,
2204 "selection_bound"));
2212 * gtk_text_buffer_create_tag:
2213 * @buffer: a #GtkTextBuffer
2214 * @tag_name: name of the new tag, or %NULL
2215 * @first_property_name: name of first property to set, or %NULL
2216 * @Varargs: %NULL-terminated list of property names and values
2219 * Creates a tag and adds it to the tag table for @buffer.
2220 * Equivalent to calling gtk_text_tag_new () and then adding the
2221 * tag to the buffer's tag table. The returned tag is owned by
2222 * the buffer's tag table, so the ref count will be equal to one.
2224 * If @tag_name is %NULL, the tag is anonymous.
2226 * If @tag_name is non-%NULL, a tag called @tag_name must not already
2227 * exist in the tag table for this buffer.
2229 * The @first_property_name argument and subsequent arguments are a list
2230 * of properties to set on the tag, as with g_object_set().
2232 * Return value: a new tag
2235 gtk_text_buffer_create_tag (GtkTextBuffer *buffer,
2236 const gchar *tag_name,
2237 const gchar *first_property_name,
2243 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
2245 tag = gtk_text_tag_new (tag_name);
2247 gtk_text_tag_table_add (get_table (buffer), tag);
2249 if (first_property_name)
2251 va_start (list, first_property_name);
2252 g_object_set_valist (G_OBJECT (tag), first_property_name, list);
2256 g_object_unref (tag);
2262 gtk_text_buffer_real_apply_tag (GtkTextBuffer *buffer,
2264 const GtkTextIter *start,
2265 const GtkTextIter *end)
2267 if (tag->table != buffer->tag_table)
2269 g_warning ("Can only apply tags that are in the tag table for the buffer");
2273 _gtk_text_btree_tag (start, end, tag, TRUE);
2277 gtk_text_buffer_real_remove_tag (GtkTextBuffer *buffer,
2279 const GtkTextIter *start,
2280 const GtkTextIter *end)
2282 if (tag->table != buffer->tag_table)
2284 g_warning ("Can only remove tags that are in the tag table for the buffer");
2288 _gtk_text_btree_tag (start, end, tag, FALSE);
2292 gtk_text_buffer_real_changed (GtkTextBuffer *buffer)
2294 gtk_text_buffer_set_modified (buffer, TRUE);
2298 gtk_text_buffer_real_mark_set (GtkTextBuffer *buffer,
2299 const GtkTextIter *iter,
2302 GtkTextMark *insert;
2304 insert = gtk_text_buffer_get_insert (buffer);
2306 if (mark == insert || mark == gtk_text_buffer_get_selection_bound (buffer))
2308 gboolean has_selection;
2310 update_selection_clipboards (buffer);
2312 has_selection = gtk_text_buffer_get_selection_bounds (buffer,
2316 if (has_selection != buffer->has_selection)
2318 buffer->has_selection = has_selection;
2319 g_object_notify (G_OBJECT (buffer), "has-selection");
2324 g_object_notify (G_OBJECT (buffer), "cursor-position");
2328 gtk_text_buffer_emit_tag (GtkTextBuffer *buffer,
2331 const GtkTextIter *start,
2332 const GtkTextIter *end)
2334 GtkTextIter start_tmp = *start;
2335 GtkTextIter end_tmp = *end;
2337 g_return_if_fail (tag != NULL);
2339 gtk_text_iter_order (&start_tmp, &end_tmp);
2342 g_signal_emit (buffer, signals[APPLY_TAG],
2344 tag, &start_tmp, &end_tmp);
2346 g_signal_emit (buffer, signals[REMOVE_TAG],
2348 tag, &start_tmp, &end_tmp);
2353 * gtk_text_buffer_apply_tag:
2354 * @buffer: a #GtkTextBuffer
2355 * @tag: a #GtkTextTag
2356 * @start: one bound of range to be tagged
2357 * @end: other bound of range to be tagged
2359 * Emits the "apply_tag" signal on @buffer. The default
2360 * handler for the signal applies @tag to the given range.
2361 * @start and @end do not have to be in order.
2365 gtk_text_buffer_apply_tag (GtkTextBuffer *buffer,
2367 const GtkTextIter *start,
2368 const GtkTextIter *end)
2370 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2371 g_return_if_fail (GTK_IS_TEXT_TAG (tag));
2372 g_return_if_fail (start != NULL);
2373 g_return_if_fail (end != NULL);
2374 g_return_if_fail (gtk_text_iter_get_buffer (start) == buffer);
2375 g_return_if_fail (gtk_text_iter_get_buffer (end) == buffer);
2376 g_return_if_fail (tag->table == buffer->tag_table);
2378 gtk_text_buffer_emit_tag (buffer, tag, TRUE, start, end);
2382 * gtk_text_buffer_remove_tag:
2383 * @buffer: a #GtkTextBuffer
2384 * @tag: a #GtkTextTag
2385 * @start: one bound of range to be untagged
2386 * @end: other bound of range to be untagged
2388 * Emits the "remove_tag" signal. The default handler for the signal
2389 * removes all occurrences of @tag from the given range. @start and
2390 * @end don't have to be in order.
2394 gtk_text_buffer_remove_tag (GtkTextBuffer *buffer,
2396 const GtkTextIter *start,
2397 const GtkTextIter *end)
2400 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2401 g_return_if_fail (GTK_IS_TEXT_TAG (tag));
2402 g_return_if_fail (start != NULL);
2403 g_return_if_fail (end != NULL);
2404 g_return_if_fail (gtk_text_iter_get_buffer (start) == buffer);
2405 g_return_if_fail (gtk_text_iter_get_buffer (end) == buffer);
2406 g_return_if_fail (tag->table == buffer->tag_table);
2408 gtk_text_buffer_emit_tag (buffer, tag, FALSE, start, end);
2413 * gtk_text_buffer_apply_tag_by_name:
2414 * @buffer: a #GtkTextBuffer
2415 * @name: name of a named #GtkTextTag
2416 * @start: one bound of range to be tagged
2417 * @end: other bound of range to be tagged
2419 * Calls gtk_text_tag_table_lookup() on the buffer's tag table to
2420 * get a #GtkTextTag, then calls gtk_text_buffer_apply_tag().
2424 gtk_text_buffer_apply_tag_by_name (GtkTextBuffer *buffer,
2426 const GtkTextIter *start,
2427 const GtkTextIter *end)
2431 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2432 g_return_if_fail (name != NULL);
2433 g_return_if_fail (start != NULL);
2434 g_return_if_fail (end != NULL);
2435 g_return_if_fail (gtk_text_iter_get_buffer (start) == buffer);
2436 g_return_if_fail (gtk_text_iter_get_buffer (end) == buffer);
2438 tag = gtk_text_tag_table_lookup (get_table (buffer),
2443 g_warning ("Unknown tag `%s'", name);
2447 gtk_text_buffer_emit_tag (buffer, tag, TRUE, start, end);
2451 * gtk_text_buffer_remove_tag_by_name:
2452 * @buffer: a #GtkTextBuffer
2453 * @name: name of a #GtkTextTag
2454 * @start: one bound of range to be untagged
2455 * @end: other bound of range to be untagged
2457 * Calls gtk_text_tag_table_lookup() on the buffer's tag table to
2458 * get a #GtkTextTag, then calls gtk_text_buffer_remove_tag().
2463 gtk_text_buffer_remove_tag_by_name (GtkTextBuffer *buffer,
2465 const GtkTextIter *start,
2466 const GtkTextIter *end)
2470 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2471 g_return_if_fail (name != NULL);
2472 g_return_if_fail (start != NULL);
2473 g_return_if_fail (end != NULL);
2474 g_return_if_fail (gtk_text_iter_get_buffer (start) == buffer);
2475 g_return_if_fail (gtk_text_iter_get_buffer (end) == buffer);
2477 tag = gtk_text_tag_table_lookup (get_table (buffer),
2482 g_warning ("Unknown tag `%s'", name);
2486 gtk_text_buffer_emit_tag (buffer, tag, FALSE, start, end);
2490 pointer_cmp (gconstpointer a,
2502 * gtk_text_buffer_remove_all_tags:
2503 * @buffer: a #GtkTextBuffer
2504 * @start: one bound of range to be untagged
2505 * @end: other bound of range to be untagged
2507 * Removes all tags in the range between @start and @end. Be careful
2508 * with this function; it could remove tags added in code unrelated to
2509 * the code you're currently writing. That is, using this function is
2510 * probably a bad idea if you have two or more unrelated code sections
2514 gtk_text_buffer_remove_all_tags (GtkTextBuffer *buffer,
2515 const GtkTextIter *start,
2516 const GtkTextIter *end)
2518 GtkTextIter first, second, tmp;
2521 GSList *prev, *next;
2524 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2525 g_return_if_fail (start != NULL);
2526 g_return_if_fail (end != NULL);
2527 g_return_if_fail (gtk_text_iter_get_buffer (start) == buffer);
2528 g_return_if_fail (gtk_text_iter_get_buffer (end) == buffer);
2533 gtk_text_iter_order (&first, &second);
2535 /* Get all tags turned on at the start */
2536 tags = gtk_text_iter_get_tags (&first);
2538 /* Find any that are toggled on within the range */
2540 while (gtk_text_iter_forward_to_tag_toggle (&tmp, NULL))
2545 if (gtk_text_iter_compare (&tmp, &second) >= 0)
2546 break; /* past the end of the range */
2548 toggled = gtk_text_iter_get_toggled_tags (&tmp, TRUE);
2550 /* We could end up with a really big-ass list here.
2553 tmp_list2 = toggled;
2554 while (tmp_list2 != NULL)
2556 tags = g_slist_prepend (tags, tmp_list2->data);
2558 tmp_list2 = g_slist_next (tmp_list2);
2561 g_slist_free (toggled);
2565 tags = g_slist_sort (tags, pointer_cmp);
2567 /* Strip duplicates */
2571 while (tmp_list != NULL)
2573 if (tag == tmp_list->data)
2576 next = tmp_list->next;
2580 tmp_list->next = NULL;
2582 g_slist_free (tmp_list);
2585 /* prev is unchanged */
2589 /* not a duplicate */
2590 tag = GTK_TEXT_TAG (tmp_list->data);
2592 tmp_list = tmp_list->next;
2596 g_slist_foreach (tags, (GFunc) g_object_ref, NULL);
2599 while (tmp_list != NULL)
2601 tag = GTK_TEXT_TAG (tmp_list->data);
2603 gtk_text_buffer_remove_tag (buffer, tag, &first, &second);
2605 tmp_list = tmp_list->next;
2608 g_slist_foreach (tags, (GFunc) g_object_unref, NULL);
2610 g_slist_free (tags);
2615 * Obtain various iterators
2619 * gtk_text_buffer_get_iter_at_line_offset:
2620 * @buffer: a #GtkTextBuffer
2621 * @iter: iterator to initialize
2622 * @line_number: line number counting from 0
2623 * @char_offset: char offset from start of line
2625 * Obtains an iterator pointing to @char_offset within the given
2626 * line. The @char_offset must exist, offsets off the end of the line
2627 * are not allowed. Note <emphasis>characters</emphasis>, not bytes;
2628 * UTF-8 may encode one character as multiple bytes.
2632 gtk_text_buffer_get_iter_at_line_offset (GtkTextBuffer *buffer,
2637 g_return_if_fail (iter != NULL);
2638 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2640 _gtk_text_btree_get_iter_at_line_char (get_btree (buffer),
2641 iter, line_number, char_offset);
2645 * gtk_text_buffer_get_iter_at_line_index:
2646 * @buffer: a #GtkTextBuffer
2647 * @iter: iterator to initialize
2648 * @line_number: line number counting from 0
2649 * @byte_index: byte index from start of line
2651 * Obtains an iterator pointing to @byte_index within the given line.
2652 * @byte_index must be the start of a UTF-8 character, and must not be
2653 * beyond the end of the line. Note <emphasis>bytes</emphasis>, not
2654 * characters; UTF-8 may encode one character as multiple bytes.
2658 gtk_text_buffer_get_iter_at_line_index (GtkTextBuffer *buffer,
2663 g_return_if_fail (iter != NULL);
2664 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2666 _gtk_text_btree_get_iter_at_line_byte (get_btree (buffer),
2667 iter, line_number, byte_index);
2671 * gtk_text_buffer_get_iter_at_line:
2672 * @buffer: a #GtkTextBuffer
2673 * @iter: iterator to initialize
2674 * @line_number: line number counting from 0
2676 * Initializes @iter to the start of the given line.
2679 gtk_text_buffer_get_iter_at_line (GtkTextBuffer *buffer,
2683 g_return_if_fail (iter != NULL);
2684 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2686 gtk_text_buffer_get_iter_at_line_offset (buffer, iter, line_number, 0);
2690 * gtk_text_buffer_get_iter_at_offset:
2691 * @buffer: a #GtkTextBuffer
2692 * @iter: iterator to initialize
2693 * @char_offset: char offset from start of buffer, counting from 0, or -1
2695 * Initializes @iter to a position @char_offset chars from the start
2696 * of the entire buffer. If @char_offset is -1 or greater than the number
2697 * of characters in the buffer, @iter is initialized to the end iterator,
2698 * the iterator one past the last valid character in the buffer.
2701 gtk_text_buffer_get_iter_at_offset (GtkTextBuffer *buffer,
2705 g_return_if_fail (iter != NULL);
2706 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2708 _gtk_text_btree_get_iter_at_char (get_btree (buffer), iter, char_offset);
2712 * gtk_text_buffer_get_start_iter:
2713 * @buffer: a #GtkTextBuffer
2714 * @iter: iterator to initialize
2716 * Initialized @iter with the first position in the text buffer. This
2717 * is the same as using gtk_text_buffer_get_iter_at_offset() to get
2718 * the iter at character offset 0.
2721 gtk_text_buffer_get_start_iter (GtkTextBuffer *buffer,
2724 g_return_if_fail (iter != NULL);
2725 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2727 _gtk_text_btree_get_iter_at_char (get_btree (buffer), iter, 0);
2731 * gtk_text_buffer_get_end_iter:
2732 * @buffer: a #GtkTextBuffer
2733 * @iter: iterator to initialize
2735 * Initializes @iter with the "end iterator," one past the last valid
2736 * character in the text buffer. If dereferenced with
2737 * gtk_text_iter_get_char(), the end iterator has a character value of
2738 * 0. The entire buffer lies in the range from the first position in
2739 * the buffer (call gtk_text_buffer_get_start_iter() to get
2740 * character position 0) to the end iterator.
2744 gtk_text_buffer_get_end_iter (GtkTextBuffer *buffer,
2747 g_return_if_fail (iter != NULL);
2748 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2750 _gtk_text_btree_get_end_iter (get_btree (buffer), iter);
2754 * gtk_text_buffer_get_bounds:
2755 * @buffer: a #GtkTextBuffer
2756 * @start: iterator to initialize with first position in the buffer
2757 * @end: iterator to initialize with the end iterator
2759 * Retrieves the first and last iterators in the buffer, i.e. the
2760 * entire buffer lies within the range [@start,@end).
2764 gtk_text_buffer_get_bounds (GtkTextBuffer *buffer,
2768 g_return_if_fail (start != NULL);
2769 g_return_if_fail (end != NULL);
2770 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2772 _gtk_text_btree_get_iter_at_char (get_btree (buffer), start, 0);
2773 _gtk_text_btree_get_end_iter (get_btree (buffer), end);
2781 * gtk_text_buffer_get_modified:
2782 * @buffer: a #GtkTextBuffer
2784 * Indicates whether the buffer has been modified since the last call
2785 * to gtk_text_buffer_set_modified() set the modification flag to
2786 * %FALSE. Used for example to enable a "save" function in a text
2789 * Return value: %TRUE if the buffer has been modified
2792 gtk_text_buffer_get_modified (GtkTextBuffer *buffer)
2794 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
2796 return buffer->modified;
2800 * gtk_text_buffer_set_modified:
2801 * @buffer: a #GtkTextBuffer
2802 * @setting: modification flag setting
2804 * Used to keep track of whether the buffer has been modified since the
2805 * last time it was saved. Whenever the buffer is saved to disk, call
2806 * gtk_text_buffer_set_modified (@buffer, FALSE). When the buffer is modified,
2807 * it will automatically toggled on the modified bit again. When the modified
2808 * bit flips, the buffer emits a "modified_changed" signal.
2812 gtk_text_buffer_set_modified (GtkTextBuffer *buffer,
2815 gboolean fixed_setting;
2817 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2819 fixed_setting = setting != FALSE;
2821 if (buffer->modified == fixed_setting)
2825 buffer->modified = fixed_setting;
2826 g_signal_emit (buffer, signals[MODIFIED_CHANGED], 0);
2831 * gtk_text_buffer_get_has_selection:
2832 * @buffer: a #GtkTextBuffer
2834 * Indicates whether the buffer has some text currently selected.
2836 * Return value: %TRUE if the there is text selected
2841 gtk_text_buffer_get_has_selection (GtkTextBuffer *buffer)
2843 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
2845 return buffer->has_selection;
2850 * Assorted other stuff
2854 * gtk_text_buffer_get_line_count:
2855 * @buffer: a #GtkTextBuffer
2857 * Obtains the number of lines in the buffer. This value is cached, so
2858 * the function is very fast.
2860 * Return value: number of lines in the buffer
2863 gtk_text_buffer_get_line_count (GtkTextBuffer *buffer)
2865 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), 0);
2867 return _gtk_text_btree_line_count (get_btree (buffer));
2871 * gtk_text_buffer_get_char_count:
2872 * @buffer: a #GtkTextBuffer
2874 * Gets the number of characters in the buffer; note that characters
2875 * and bytes are not the same, you can't e.g. expect the contents of
2876 * the buffer in string form to be this many bytes long. The character
2877 * count is cached, so this function is very fast.
2879 * Return value: number of characters in the buffer
2882 gtk_text_buffer_get_char_count (GtkTextBuffer *buffer)
2884 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), 0);
2886 return _gtk_text_btree_char_count (get_btree (buffer));
2889 /* Called when we lose the primary selection.
2892 clipboard_clear_selection_cb (GtkClipboard *clipboard,
2895 /* Move selection_bound to the insertion point */
2897 GtkTextIter selection_bound;
2898 GtkTextBuffer *buffer = GTK_TEXT_BUFFER (data);
2900 gtk_text_buffer_get_iter_at_mark (buffer, &insert,
2901 gtk_text_buffer_get_mark (buffer, "insert"));
2902 gtk_text_buffer_get_iter_at_mark (buffer, &selection_bound,
2903 gtk_text_buffer_get_mark (buffer, "selection_bound"));
2905 if (!gtk_text_iter_equal (&insert, &selection_bound))
2906 gtk_text_buffer_move_mark (buffer,
2907 gtk_text_buffer_get_mark (buffer, "selection_bound"),
2911 /* Called when we have the primary selection and someone else wants our
2912 * data in order to paste it.
2915 clipboard_get_selection_cb (GtkClipboard *clipboard,
2916 GtkSelectionData *selection_data,
2920 GtkTextBuffer *buffer = GTK_TEXT_BUFFER (data);
2921 GtkTextIter start, end;
2923 if (gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
2925 if (info == GTK_TEXT_BUFFER_TARGET_INFO_BUFFER_CONTENTS)
2927 /* Provide the address of the buffer; this will only be
2928 * used within-process
2930 gtk_selection_data_set (selection_data,
2931 selection_data->target,
2936 else if (info == GTK_TEXT_BUFFER_TARGET_INFO_RICH_TEXT)
2941 str = gtk_text_buffer_serialize (buffer, buffer,
2942 selection_data->target,
2943 &start, &end, &len);
2945 gtk_selection_data_set (selection_data,
2946 selection_data->target,
2955 str = gtk_text_iter_get_visible_text (&start, &end);
2956 gtk_selection_data_set_text (selection_data, str, -1);
2962 static GtkTextBuffer *
2963 create_clipboard_contents_buffer (GtkTextBuffer *buffer)
2965 GtkTextBuffer *contents;
2967 contents = gtk_text_buffer_new (gtk_text_buffer_get_tag_table (buffer));
2969 g_object_set_data (G_OBJECT (contents), I_("gtk-text-buffer-clipboard-source"),
2971 g_object_set_data (G_OBJECT (contents), I_("gtk-text-buffer-clipboard"),
2972 GINT_TO_POINTER (1));
2974 /* Ref the source buffer as long as the clipboard contents buffer
2975 * exists, because it's needed for serializing the contents buffer.
2976 * See http://bugzilla.gnome.org/show_bug.cgi?id=339195
2978 g_object_ref (buffer);
2979 g_object_weak_ref (G_OBJECT (contents), (GWeakNotify) g_object_unref, buffer);
2984 /* Provide cut/copied data */
2986 clipboard_get_contents_cb (GtkClipboard *clipboard,
2987 GtkSelectionData *selection_data,
2991 GtkTextBuffer *contents = GTK_TEXT_BUFFER (data);
2993 g_assert (contents); /* This should never be called unless we own the clipboard */
2995 if (info == GTK_TEXT_BUFFER_TARGET_INFO_BUFFER_CONTENTS)
2997 /* Provide the address of the clipboard buffer; this will only
2998 * be used within-process. OK to supply a NULL value for contents.
3000 gtk_selection_data_set (selection_data,
3001 selection_data->target,
3006 else if (info == GTK_TEXT_BUFFER_TARGET_INFO_RICH_TEXT)
3008 GtkTextBuffer *clipboard_source_buffer;
3009 GtkTextIter start, end;
3013 clipboard_source_buffer = g_object_get_data (G_OBJECT (contents),
3014 "gtk-text-buffer-clipboard-source");
3016 gtk_text_buffer_get_bounds (contents, &start, &end);
3018 str = gtk_text_buffer_serialize (clipboard_source_buffer, contents,
3019 selection_data->target,
3020 &start, &end, &len);
3022 gtk_selection_data_set (selection_data,
3023 selection_data->target,
3031 GtkTextIter start, end;
3033 gtk_text_buffer_get_bounds (contents, &start, &end);
3035 str = gtk_text_iter_get_visible_text (&start, &end);
3036 gtk_selection_data_set_text (selection_data, str, -1);
3042 clipboard_clear_contents_cb (GtkClipboard *clipboard,
3045 GtkTextBuffer *contents = GTK_TEXT_BUFFER (data);
3047 g_object_unref (contents);
3051 get_paste_point (GtkTextBuffer *buffer,
3053 gboolean clear_afterward)
3055 GtkTextIter insert_point;
3056 GtkTextMark *paste_point_override;
3058 paste_point_override = gtk_text_buffer_get_mark (buffer,
3059 "gtk_paste_point_override");
3061 if (paste_point_override != NULL)
3063 gtk_text_buffer_get_iter_at_mark (buffer, &insert_point,
3064 paste_point_override);
3065 if (clear_afterward)
3066 gtk_text_buffer_delete_mark (buffer,
3067 gtk_text_buffer_get_mark (buffer,
3068 "gtk_paste_point_override"));
3072 gtk_text_buffer_get_iter_at_mark (buffer, &insert_point,
3073 gtk_text_buffer_get_mark (buffer,
3077 *iter = insert_point;
3081 pre_paste_prep (ClipboardRequest *request_data,
3082 GtkTextIter *insert_point)
3084 GtkTextBuffer *buffer = request_data->buffer;
3086 get_paste_point (buffer, insert_point, TRUE);
3088 /* If we're going to replace the selection, we insert before it to
3089 * avoid messing it up, then we delete the selection after inserting.
3091 if (request_data->replace_selection)
3093 GtkTextIter start, end;
3095 if (gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
3096 *insert_point = start;
3101 post_paste_cleanup (ClipboardRequest *request_data)
3103 if (request_data->replace_selection)
3105 GtkTextIter start, end;
3107 if (gtk_text_buffer_get_selection_bounds (request_data->buffer,
3110 if (request_data->interactive)
3111 gtk_text_buffer_delete_interactive (request_data->buffer,
3114 request_data->default_editable);
3116 gtk_text_buffer_delete (request_data->buffer, &start, &end);
3122 free_clipboard_request (ClipboardRequest *request_data)
3124 g_object_unref (request_data->buffer);
3125 g_free (request_data);
3128 /* Called when we request a paste and receive the text data
3131 clipboard_text_received (GtkClipboard *clipboard,
3135 ClipboardRequest *request_data = data;
3136 GtkTextBuffer *buffer = request_data->buffer;
3140 GtkTextIter insert_point;
3142 if (request_data->interactive)
3143 gtk_text_buffer_begin_user_action (buffer);
3145 pre_paste_prep (request_data, &insert_point);
3147 if (request_data->interactive)
3148 gtk_text_buffer_insert_interactive (buffer, &insert_point,
3149 str, -1, request_data->default_editable);
3151 gtk_text_buffer_insert (buffer, &insert_point,
3154 post_paste_cleanup (request_data);
3156 if (request_data->interactive)
3157 gtk_text_buffer_end_user_action (buffer);
3160 free_clipboard_request (request_data);
3163 static GtkTextBuffer*
3164 selection_data_get_buffer (GtkSelectionData *selection_data,
3165 ClipboardRequest *request_data)
3168 GtkTextBuffer *src_buffer = NULL;
3170 /* If we can get the owner, the selection is in-process */
3171 owner = gdk_selection_owner_get_for_display (selection_data->display,
3172 selection_data->selection);
3177 if (gdk_window_get_window_type (owner) == GDK_WINDOW_FOREIGN)
3180 if (selection_data->type !=
3181 gdk_atom_intern_static_string ("GTK_TEXT_BUFFER_CONTENTS"))
3184 if (selection_data->length != sizeof (src_buffer))
3187 memcpy (&src_buffer, selection_data->data, sizeof (src_buffer));
3189 if (src_buffer == NULL)
3192 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (src_buffer), NULL);
3194 if (gtk_text_buffer_get_tag_table (src_buffer) !=
3195 gtk_text_buffer_get_tag_table (request_data->buffer))
3202 /* These are pretty handy functions; maybe something like them
3203 * should be in the public API. Also, there are other places in this
3204 * file where they could be used.
3207 save_iter (const GtkTextIter *iter,
3208 gboolean left_gravity)
3210 return gtk_text_buffer_create_mark (gtk_text_iter_get_buffer (iter),
3217 restore_iter (const GtkTextIter *iter,
3220 gtk_text_buffer_get_iter_at_mark (gtk_text_mark_get_buffer (save_id),
3221 (GtkTextIter*) iter,
3223 gtk_text_buffer_delete_mark (gtk_text_mark_get_buffer (save_id),
3229 clipboard_rich_text_received (GtkClipboard *clipboard,
3235 ClipboardRequest *request_data = data;
3236 GtkTextIter insert_point;
3237 gboolean retval = TRUE;
3238 GError *error = NULL;
3240 if (text != NULL && length > 0)
3242 pre_paste_prep (request_data, &insert_point);
3244 if (request_data->interactive)
3245 gtk_text_buffer_begin_user_action (request_data->buffer);
3247 if (!request_data->interactive ||
3248 gtk_text_iter_can_insert (&insert_point,
3249 request_data->default_editable))
3251 retval = gtk_text_buffer_deserialize (request_data->buffer,
3252 request_data->buffer,
3261 g_warning ("error pasting: %s\n", error->message);
3262 g_clear_error (&error);
3265 if (request_data->interactive)
3266 gtk_text_buffer_end_user_action (request_data->buffer);
3270 post_paste_cleanup (request_data);
3275 /* Request the text selection instead */
3276 gtk_clipboard_request_text (clipboard,
3277 clipboard_text_received,
3282 paste_from_buffer (ClipboardRequest *request_data,
3283 GtkTextBuffer *src_buffer,
3284 const GtkTextIter *start,
3285 const GtkTextIter *end)
3287 GtkTextIter insert_point;
3288 GtkTextBuffer *buffer = request_data->buffer;
3290 /* We're about to emit a bunch of signals, so be safe */
3291 g_object_ref (src_buffer);
3293 pre_paste_prep (request_data, &insert_point);
3295 if (request_data->interactive)
3296 gtk_text_buffer_begin_user_action (buffer);
3298 if (!gtk_text_iter_equal (start, end))
3300 if (!request_data->interactive ||
3301 (gtk_text_iter_can_insert (&insert_point,
3302 request_data->default_editable)))
3303 gtk_text_buffer_real_insert_range (buffer,
3307 request_data->interactive);
3310 post_paste_cleanup (request_data);
3312 if (request_data->interactive)
3313 gtk_text_buffer_end_user_action (buffer);
3315 g_object_unref (src_buffer);
3317 free_clipboard_request (request_data);
3321 clipboard_clipboard_buffer_received (GtkClipboard *clipboard,
3322 GtkSelectionData *selection_data,
3325 ClipboardRequest *request_data = data;
3326 GtkTextBuffer *src_buffer;
3328 src_buffer = selection_data_get_buffer (selection_data, request_data);
3332 GtkTextIter start, end;
3334 if (g_object_get_data (G_OBJECT (src_buffer), "gtk-text-buffer-clipboard"))
3336 gtk_text_buffer_get_bounds (src_buffer, &start, &end);
3338 paste_from_buffer (request_data, src_buffer,
3343 if (gtk_text_buffer_get_selection_bounds (src_buffer, &start, &end))
3344 paste_from_buffer (request_data, src_buffer,
3350 if (gtk_clipboard_wait_is_rich_text_available (clipboard,
3351 request_data->buffer))
3353 /* Request rich text */
3354 gtk_clipboard_request_rich_text (clipboard,
3355 request_data->buffer,
3356 clipboard_rich_text_received,
3361 /* Request the text selection instead */
3362 gtk_clipboard_request_text (clipboard,
3363 clipboard_text_received,
3371 GtkClipboard *clipboard;
3373 } SelectionClipboard;
3376 update_selection_clipboards (GtkTextBuffer *buffer)
3378 GtkTextBufferPrivate *priv;
3379 GSList *tmp_list = buffer->selection_clipboards;
3381 priv = GTK_TEXT_BUFFER_GET_PRIVATE (buffer);
3383 gtk_text_buffer_get_copy_target_list (buffer);
3390 SelectionClipboard *selection_clipboard = tmp_list->data;
3391 GtkClipboard *clipboard = selection_clipboard->clipboard;
3393 /* Determine whether we have a selection and adjust X selection
3396 if (!gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
3398 if (gtk_clipboard_get_owner (clipboard) == G_OBJECT (buffer))
3399 gtk_clipboard_clear (clipboard);
3403 /* Even if we already have the selection, we need to update our
3406 if (!gtk_clipboard_set_with_owner (clipboard,
3407 priv->copy_target_entries,
3408 priv->n_copy_target_entries,
3409 clipboard_get_selection_cb,
3410 clipboard_clear_selection_cb,
3412 clipboard_clear_selection_cb (clipboard, buffer);
3415 tmp_list = tmp_list->next;
3419 static SelectionClipboard *
3420 find_selection_clipboard (GtkTextBuffer *buffer,
3421 GtkClipboard *clipboard)
3423 GSList *tmp_list = buffer->selection_clipboards;
3426 SelectionClipboard *selection_clipboard = tmp_list->data;
3427 if (selection_clipboard->clipboard == clipboard)
3428 return selection_clipboard;
3430 tmp_list = tmp_list->next;
3437 * gtk_text_buffer_add_selection_clipboard:
3438 * @buffer: a #GtkTextBuffer
3439 * @clipboard: a #GtkClipboard
3441 * Adds @clipboard to the list of clipboards in which the selection contents
3442 * of @buffer are available. In most cases, @clipboard will be the #GtkClipboard
3443 * of type %GDK_SELECTION_PRIMARY for a view of @buffer.
3446 gtk_text_buffer_add_selection_clipboard (GtkTextBuffer *buffer,
3447 GtkClipboard *clipboard)
3449 SelectionClipboard *selection_clipboard;
3451 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
3452 g_return_if_fail (clipboard != NULL);
3454 selection_clipboard = find_selection_clipboard (buffer, clipboard);
3455 if (selection_clipboard)
3457 selection_clipboard->ref_count++;
3461 selection_clipboard = g_new (SelectionClipboard, 1);
3463 selection_clipboard->clipboard = clipboard;
3464 selection_clipboard->ref_count = 1;
3466 buffer->selection_clipboards = g_slist_prepend (buffer->selection_clipboards, selection_clipboard);
3471 * gtk_text_buffer_remove_selection_clipboard:
3472 * @buffer: a #GtkTextBuffer
3473 * @clipboard: a #GtkClipboard added to @buffer by gtk_text_buffer_add_selection_clipboard().
3475 * Removes a #GtkClipboard added with gtk_text_buffer_add_selection_clipboard()
3478 gtk_text_buffer_remove_selection_clipboard (GtkTextBuffer *buffer,
3479 GtkClipboard *clipboard)
3481 SelectionClipboard *selection_clipboard;
3483 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
3484 g_return_if_fail (clipboard != NULL);
3486 selection_clipboard = find_selection_clipboard (buffer, clipboard);
3487 g_return_if_fail (selection_clipboard != NULL);
3489 selection_clipboard->ref_count--;
3490 if (selection_clipboard->ref_count == 0)
3492 if (gtk_clipboard_get_owner (selection_clipboard->clipboard) == G_OBJECT (buffer))
3493 gtk_clipboard_clear (selection_clipboard->clipboard);
3495 buffer->selection_clipboards = g_slist_remove (buffer->selection_clipboards,
3496 selection_clipboard);
3498 g_free (selection_clipboard);
3503 remove_all_selection_clipboards (GtkTextBuffer *buffer)
3505 g_slist_foreach (buffer->selection_clipboards, (GFunc)g_free, NULL);
3506 g_slist_free (buffer->selection_clipboards);
3507 buffer->selection_clipboards = NULL;
3511 * gtk_text_buffer_paste_clipboard:
3512 * @buffer: a #GtkTextBuffer
3513 * @clipboard: the #GtkClipboard to paste from
3514 * @override_location: location to insert pasted text, or %NULL for at the cursor
3515 * @default_editable: whether the buffer is editable by default
3517 * Pastes the contents of a clipboard at the insertion point, or at @override_location.
3518 * (Note: pasting is asynchronous, that is, we'll ask for the paste data and
3519 * return, and at some point later after the main loop runs, the paste
3520 * data will be inserted.)
3524 gtk_text_buffer_paste_clipboard (GtkTextBuffer *buffer,
3525 GtkClipboard *clipboard,
3526 GtkTextIter *override_location,
3527 gboolean default_editable)
3529 ClipboardRequest *data = g_new (ClipboardRequest, 1);
3530 GtkTextIter paste_point;
3531 GtkTextIter start, end;
3533 if (override_location != NULL)
3534 gtk_text_buffer_create_mark (buffer,
3535 "gtk_paste_point_override",
3536 override_location, FALSE);
3538 data->buffer = g_object_ref (buffer);
3539 data->interactive = TRUE;
3540 data->default_editable = default_editable;
3542 /* When pasting with the cursor inside the selection area, you
3543 * replace the selection with the new text, otherwise, you
3544 * simply insert the new text at the point where the click
3545 * occured, unselecting any selected text. The replace_selection
3546 * flag toggles this behavior.
3548 data->replace_selection = FALSE;
3550 get_paste_point (buffer, &paste_point, FALSE);
3551 if (gtk_text_buffer_get_selection_bounds (buffer, &start, &end) &&
3552 (gtk_text_iter_in_range (&paste_point, &start, &end) ||
3553 gtk_text_iter_equal (&paste_point, &end)))
3554 data->replace_selection = TRUE;
3556 gtk_clipboard_request_contents (clipboard,
3557 gdk_atom_intern_static_string ("GTK_TEXT_BUFFER_CONTENTS"),
3558 clipboard_clipboard_buffer_received, data);
3562 * gtk_text_buffer_delete_selection:
3563 * @buffer: a #GtkTextBuffer
3564 * @interactive: whether the deletion is caused by user interaction
3565 * @default_editable: whether the buffer is editable by default
3567 * Deletes the range between the "insert" and "selection_bound" marks,
3568 * that is, the currently-selected text. If @interactive is %TRUE,
3569 * the editability of the selection will be considered (users can't delete
3572 * Return value: whether there was a non-empty selection to delete
3575 gtk_text_buffer_delete_selection (GtkTextBuffer *buffer,
3576 gboolean interactive,
3577 gboolean default_editable)
3582 if (!gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
3584 return FALSE; /* No selection */
3590 gtk_text_buffer_begin_user_action (buffer);
3591 gtk_text_buffer_delete_interactive (buffer, &start, &end, default_editable);
3592 gtk_text_buffer_end_user_action (buffer);
3595 gtk_text_buffer_delete (buffer, &start, &end);
3597 return TRUE; /* We deleted stuff */
3602 * gtk_text_buffer_backspace:
3603 * @buffer: a #GtkTextBuffer
3604 * @iter: a position in @buffer
3605 * @interactive: whether the deletion is caused by user interaction
3606 * @default_editable: whether the buffer is editable by default
3608 * Performs the appropriate action as if the user hit the delete
3609 * key with the cursor at the position specified by @iter. In the
3610 * normal case a single character will be deleted, but when
3611 * combining accents are involved, more than one character can
3612 * be deleted, and when precomposed character and accent combinations
3613 * are involved, less than one character will be deleted.
3615 * Because the buffer is modified, all outstanding iterators become
3616 * invalid after calling this function; however, the @iter will be
3617 * re-initialized to point to the location where text was deleted.
3619 * Return value: %TRUE if the buffer was modified
3624 gtk_text_buffer_backspace (GtkTextBuffer *buffer,
3626 gboolean interactive,
3627 gboolean default_editable)
3629 gchar *cluster_text;
3632 gboolean retval = FALSE;
3633 const PangoLogAttr *attrs;
3635 gboolean backspace_deletes_character;
3637 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
3638 g_return_val_if_fail (iter != NULL, FALSE);
3643 attrs = _gtk_text_buffer_get_line_log_attrs (buffer, &start, NULL);
3645 /* For no good reason, attrs is NULL for the empty last line in
3646 * a buffer. Special case that here. (#156164)
3650 offset = gtk_text_iter_get_line_offset (&start);
3651 backspace_deletes_character = attrs[offset].backspace_deletes_character;
3654 backspace_deletes_character = FALSE;
3656 gtk_text_iter_backward_cursor_position (&start);
3658 if (gtk_text_iter_equal (&start, &end))
3661 cluster_text = gtk_text_iter_get_text (&start, &end);
3664 gtk_text_buffer_begin_user_action (buffer);
3666 if (gtk_text_buffer_delete_interactive (buffer, &start, &end,
3669 if (backspace_deletes_character)
3671 gchar *normalized_text = g_utf8_normalize (cluster_text,
3672 strlen (cluster_text),
3674 glong len = g_utf8_strlen (normalized_text, -1);
3677 gtk_text_buffer_insert_interactive (buffer,
3680 g_utf8_offset_to_pointer (normalized_text, len - 1) - normalized_text,
3683 g_free (normalized_text);
3690 gtk_text_buffer_end_user_action (buffer);
3692 g_free (cluster_text);
3694 /* Revalidate the users iter */
3701 cut_or_copy (GtkTextBuffer *buffer,
3702 GtkClipboard *clipboard,
3703 gboolean delete_region_after,
3704 gboolean interactive,
3705 gboolean default_editable)
3707 GtkTextBufferPrivate *priv;
3709 /* We prefer to cut the selected region between selection_bound and
3710 * insertion point. If that region is empty, then we cut the region
3711 * between the "anchor" and the insertion point (this is for
3712 * C-space and M-w and other Emacs-style copy/yank keys). Note that
3713 * insert and selection_bound are guaranteed to exist, but the
3714 * anchor only exists sometimes.
3719 priv = GTK_TEXT_BUFFER_GET_PRIVATE (buffer);
3721 gtk_text_buffer_get_copy_target_list (buffer);
3723 if (!gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
3725 /* Let's try the anchor thing */
3726 GtkTextMark * anchor = gtk_text_buffer_get_mark (buffer, "anchor");
3732 gtk_text_buffer_get_iter_at_mark (buffer, &end, anchor);
3733 gtk_text_iter_order (&start, &end);
3737 if (!gtk_text_iter_equal (&start, &end))
3740 GtkTextBuffer *contents;
3742 contents = create_clipboard_contents_buffer (buffer);
3744 gtk_text_buffer_get_iter_at_offset (contents, &ins, 0);
3746 gtk_text_buffer_insert_range (contents, &ins, &start, &end);
3748 if (!gtk_clipboard_set_with_data (clipboard,
3749 priv->copy_target_entries,
3750 priv->n_copy_target_entries,
3751 clipboard_get_contents_cb,
3752 clipboard_clear_contents_cb,
3754 g_object_unref (contents);
3756 gtk_clipboard_set_can_store (clipboard,
3757 priv->copy_target_entries + 1,
3758 priv->n_copy_target_entries - 1);
3760 if (delete_region_after)
3763 gtk_text_buffer_delete_interactive (buffer, &start, &end,
3766 gtk_text_buffer_delete (buffer, &start, &end);
3772 * gtk_text_buffer_cut_clipboard:
3773 * @buffer: a #GtkTextBuffer
3774 * @clipboard: the #GtkClipboard object to cut to.
3775 * @default_editable: default editability of the buffer
3777 * Copies the currently-selected text to a clipboard, then deletes
3778 * said text if it's editable.
3782 gtk_text_buffer_cut_clipboard (GtkTextBuffer *buffer,
3783 GtkClipboard *clipboard,
3784 gboolean default_editable)
3786 gtk_text_buffer_begin_user_action (buffer);
3787 cut_or_copy (buffer, clipboard, TRUE, TRUE, default_editable);
3788 gtk_text_buffer_end_user_action (buffer);
3792 * gtk_text_buffer_copy_clipboard:
3793 * @buffer: a #GtkTextBuffer
3794 * @clipboard: the #GtkClipboard object to copy to.
3796 * Copies the currently-selected text to a clipboard.
3800 gtk_text_buffer_copy_clipboard (GtkTextBuffer *buffer,
3801 GtkClipboard *clipboard)
3803 gtk_text_buffer_begin_user_action (buffer);
3804 cut_or_copy (buffer, clipboard, FALSE, TRUE, TRUE);
3805 gtk_text_buffer_end_user_action (buffer);
3810 * gtk_text_buffer_get_selection_bounds:
3811 * @buffer: a #GtkTextBuffer a #GtkTextBuffer
3812 * @start: iterator to initialize with selection start
3813 * @end: iterator to initialize with selection end
3815 * Returns %TRUE if some text is selected; places the bounds
3816 * of the selection in @start and @end (if the selection has length 0,
3817 * then @start and @end are filled in with the same value).
3818 * @start and @end will be in ascending order. If @start and @end are
3819 * NULL, then they are not filled in, but the return value still indicates
3820 * whether text is selected.
3822 * Return value: whether the selection has nonzero length
3825 gtk_text_buffer_get_selection_bounds (GtkTextBuffer *buffer,
3829 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
3831 return _gtk_text_btree_get_selection_bounds (get_btree (buffer), start, end);
3835 * gtk_text_buffer_begin_user_action:
3836 * @buffer: a #GtkTextBuffer
3838 * Called to indicate that the buffer operations between here and a
3839 * call to gtk_text_buffer_end_user_action() are part of a single
3840 * user-visible operation. The operations between
3841 * gtk_text_buffer_begin_user_action() and
3842 * gtk_text_buffer_end_user_action() can then be grouped when creating
3843 * an undo stack. #GtkTextBuffer maintains a count of calls to
3844 * gtk_text_buffer_begin_user_action() that have not been closed with
3845 * a call to gtk_text_buffer_end_user_action(), and emits the "begin_user_action"
3846 * and "end_user_action" signals only for the outermost pair of calls.
3847 * This allows you to build user actions from other user actions.
3849 * The "interactive" buffer mutation functions, such as
3850 * gtk_text_buffer_insert_interactive(), automatically call begin/end
3851 * user action around the buffer operations they perform, so there's
3852 * no need to add extra calls if you user action consists solely of a
3853 * single call to one of those functions.
3856 gtk_text_buffer_begin_user_action (GtkTextBuffer *buffer)
3858 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
3860 buffer->user_action_count += 1;
3862 if (buffer->user_action_count == 1)
3864 /* Outermost nested user action begin emits the signal */
3865 g_signal_emit (buffer, signals[BEGIN_USER_ACTION], 0);
3870 * gtk_text_buffer_end_user_action:
3871 * @buffer: a #GtkTextBuffer
3873 * Should be paired with a call to gtk_text_buffer_begin_user_action().
3874 * See that function for a full explanation.
3877 gtk_text_buffer_end_user_action (GtkTextBuffer *buffer)
3879 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
3880 g_return_if_fail (buffer->user_action_count > 0);
3882 buffer->user_action_count -= 1;
3884 if (buffer->user_action_count == 0)
3886 /* Ended the outermost-nested user action end, so emit the signal */
3887 g_signal_emit (buffer, signals[END_USER_ACTION], 0);
3892 gtk_text_buffer_free_target_lists (GtkTextBuffer *buffer)
3894 GtkTextBufferPrivate *priv = GTK_TEXT_BUFFER_GET_PRIVATE (buffer);
3896 if (priv->copy_target_list)
3898 gtk_target_list_unref (priv->copy_target_list);
3899 priv->copy_target_list = NULL;
3901 gtk_target_table_free (priv->copy_target_entries,
3902 priv->n_copy_target_entries);
3903 priv->copy_target_entries = NULL;
3904 priv->n_copy_target_entries = 0;
3907 if (priv->paste_target_list)
3909 gtk_target_list_unref (priv->paste_target_list);
3910 priv->paste_target_list = NULL;
3912 gtk_target_table_free (priv->paste_target_entries,
3913 priv->n_paste_target_entries);
3914 priv->paste_target_entries = NULL;
3915 priv->n_paste_target_entries = 0;
3919 static GtkTargetList *
3920 gtk_text_buffer_get_target_list (GtkTextBuffer *buffer,
3921 gboolean deserializable,
3922 GtkTargetEntry **entries,
3925 GtkTargetList *target_list;
3927 target_list = gtk_target_list_new (NULL, 0);
3929 gtk_target_list_add (target_list,
3930 gdk_atom_intern_static_string ("GTK_TEXT_BUFFER_CONTENTS"),
3931 GTK_TARGET_SAME_APP,
3932 GTK_TEXT_BUFFER_TARGET_INFO_BUFFER_CONTENTS);
3934 gtk_target_list_add_rich_text_targets (target_list,
3935 GTK_TEXT_BUFFER_TARGET_INFO_RICH_TEXT,
3939 gtk_target_list_add_text_targets (target_list,
3940 GTK_TEXT_BUFFER_TARGET_INFO_TEXT);
3942 *entries = gtk_target_table_new_from_list (target_list, n_entries);
3948 * gtk_text_buffer_get_copy_target_list:
3949 * @buffer: a #GtkTextBuffer
3951 * This function returns the list of targets this text buffer can
3952 * provide for copying and as DND source. The targets in the list are
3953 * added with %info values from the #GtkTextBufferTargetInfo enum,
3954 * using gtk_target_list_add_rich_text_targets() and
3955 * gtk_target_list_add_text_targets()
3957 * Return value: the #GtkTargetList
3962 gtk_text_buffer_get_copy_target_list (GtkTextBuffer *buffer)
3964 GtkTextBufferPrivate *priv;
3966 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
3968 priv = GTK_TEXT_BUFFER_GET_PRIVATE (buffer);
3970 if (! priv->copy_target_list)
3971 priv->copy_target_list =
3972 gtk_text_buffer_get_target_list (buffer, FALSE,
3973 &priv->copy_target_entries,
3974 &priv->n_copy_target_entries);
3976 return priv->copy_target_list;
3980 * gtk_text_buffer_get_paste_target_list:
3981 * @buffer: a #GtkTextBuffer
3983 * This function returns the list of targets this text buffer supports
3984 * for pasting and as DND destination. The targets in the list are
3985 * added with %info values from the #GtkTextBufferTargetInfo enum,
3986 * using gtk_target_list_add_rich_text_targets() and
3987 * gtk_target_list_add_text_targets()
3989 * Return value: the #GtkTargetList
3994 gtk_text_buffer_get_paste_target_list (GtkTextBuffer *buffer)
3996 GtkTextBufferPrivate *priv;
3998 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
4000 priv = GTK_TEXT_BUFFER_GET_PRIVATE (buffer);
4002 if (! priv->paste_target_list)
4003 priv->paste_target_list =
4004 gtk_text_buffer_get_target_list (buffer, TRUE,
4005 &priv->paste_target_entries,
4006 &priv->n_paste_target_entries);
4008 return priv->paste_target_list;
4012 * Logical attribute cache
4015 #define ATTR_CACHE_SIZE 2
4017 typedef struct _CacheEntry CacheEntry;
4022 PangoLogAttr *attrs;
4026 struct _GtkTextLogAttrCache
4028 gint chars_changed_stamp;
4029 CacheEntry entries[ATTR_CACHE_SIZE];
4033 free_log_attr_cache (GtkTextLogAttrCache *cache)
4036 while (i < ATTR_CACHE_SIZE)
4038 g_free (cache->entries[i].attrs);
4045 clear_log_attr_cache (GtkTextLogAttrCache *cache)
4048 while (i < ATTR_CACHE_SIZE)
4050 g_free (cache->entries[i].attrs);
4051 cache->entries[i].attrs = NULL;
4056 static PangoLogAttr*
4057 compute_log_attrs (const GtkTextIter *iter,
4063 gint char_len, byte_len;
4064 PangoLogAttr *attrs = NULL;
4069 gtk_text_iter_set_line_offset (&start, 0);
4070 gtk_text_iter_forward_line (&end);
4072 paragraph = gtk_text_iter_get_slice (&start, &end);
4073 char_len = g_utf8_strlen (paragraph, -1);
4074 byte_len = strlen (paragraph);
4076 g_assert (char_len > 0);
4079 *char_lenp = char_len;
4081 attrs = g_new (PangoLogAttr, char_len + 1);
4083 /* FIXME we need to follow PangoLayout and allow different language
4084 * tags within the paragraph
4086 pango_get_log_attrs (paragraph, byte_len, -1,
4087 gtk_text_iter_get_language (&start),
4096 /* The return value from this is valid until you call this a second time.
4099 _gtk_text_buffer_get_line_log_attrs (GtkTextBuffer *buffer,
4100 const GtkTextIter *anywhere_in_line,
4104 GtkTextLogAttrCache *cache;
4107 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
4108 g_return_val_if_fail (anywhere_in_line != NULL, NULL);
4110 /* special-case for empty last line in buffer */
4111 if (gtk_text_iter_is_end (anywhere_in_line) &&
4112 gtk_text_iter_get_line_offset (anywhere_in_line) == 0)
4119 /* FIXME we also need to recompute log attrs if the language tag at
4120 * the start of a paragraph changes
4123 if (buffer->log_attr_cache == NULL)
4125 buffer->log_attr_cache = g_new0 (GtkTextLogAttrCache, 1);
4126 buffer->log_attr_cache->chars_changed_stamp =
4127 _gtk_text_btree_get_chars_changed_stamp (get_btree (buffer));
4129 else if (buffer->log_attr_cache->chars_changed_stamp !=
4130 _gtk_text_btree_get_chars_changed_stamp (get_btree (buffer)))
4132 clear_log_attr_cache (buffer->log_attr_cache);
4135 cache = buffer->log_attr_cache;
4136 line = gtk_text_iter_get_line (anywhere_in_line);
4139 while (i < ATTR_CACHE_SIZE)
4141 if (cache->entries[i].attrs &&
4142 cache->entries[i].line == line)
4145 *char_len = cache->entries[i].char_len;
4146 return cache->entries[i].attrs;
4151 /* Not in cache; open up the first cache entry */
4152 if (cache->entries[ATTR_CACHE_SIZE-1].attrs)
4153 g_free (cache->entries[ATTR_CACHE_SIZE-1].attrs);
4155 g_memmove (cache->entries + 1, cache->entries,
4156 sizeof (CacheEntry) * (ATTR_CACHE_SIZE - 1));
4158 cache->entries[0].line = line;
4159 cache->entries[0].attrs = compute_log_attrs (anywhere_in_line,
4160 &cache->entries[0].char_len);
4163 *char_len = cache->entries[0].char_len;
4165 return cache->entries[0].attrs;
4169 _gtk_text_buffer_notify_will_remove_tag (GtkTextBuffer *buffer,
4172 /* This removes tag from the buffer, but DOESN'T emit the
4173 * remove_tag signal, because we can't afford to have user
4174 * code messing things up at this point; the tag MUST be removed
4178 _gtk_text_btree_notify_will_remove_tag (buffer->btree, tag);
4186 _gtk_text_buffer_spew (GtkTextBuffer *buffer)
4188 _gtk_text_btree_spew (get_btree (buffer));
4191 #define __GTK_TEXT_BUFFER_C__
4192 #include "gtkaliasdef.c"