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;
566 GtkTextBufferPrivate *priv;
568 buffer = GTK_TEXT_BUFFER (object);
570 remove_all_selection_clipboards (buffer);
572 priv = GTK_TEXT_BUFFER_GET_PRIVATE (buffer);
574 if (buffer->tag_table)
576 _gtk_text_tag_table_remove_buffer (buffer->tag_table, buffer);
577 g_object_unref (buffer->tag_table);
578 buffer->tag_table = NULL;
583 _gtk_text_btree_unref (buffer->btree);
584 buffer->btree = NULL;
587 if (buffer->log_attr_cache)
588 free_log_attr_cache (buffer->log_attr_cache);
590 buffer->log_attr_cache = NULL;
592 gtk_text_buffer_free_target_lists (buffer);
594 G_OBJECT_CLASS (gtk_text_buffer_parent_class)->finalize (object);
598 get_btree (GtkTextBuffer *buffer)
600 if (buffer->btree == NULL)
601 buffer->btree = _gtk_text_btree_new (gtk_text_buffer_get_tag_table (buffer),
604 return buffer->btree;
608 _gtk_text_buffer_get_btree (GtkTextBuffer *buffer)
610 return get_btree (buffer);
614 * gtk_text_buffer_get_tag_table:
615 * @buffer: a #GtkTextBuffer
617 * Get the #GtkTextTagTable associated with this buffer.
619 * Return value: the buffer's tag table
622 gtk_text_buffer_get_tag_table (GtkTextBuffer *buffer)
624 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
626 return get_table (buffer);
630 * gtk_text_buffer_set_text:
631 * @buffer: a #GtkTextBuffer
632 * @text: UTF-8 text to insert
633 * @len: length of @text in bytes
635 * Deletes current contents of @buffer, and inserts @text instead. If
636 * @len is -1, @text must be nul-terminated. @text must be valid UTF-8.
639 gtk_text_buffer_set_text (GtkTextBuffer *buffer,
643 GtkTextIter start, end;
645 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
646 g_return_if_fail (text != NULL);
651 gtk_text_buffer_get_bounds (buffer, &start, &end);
653 gtk_text_buffer_delete (buffer, &start, &end);
657 gtk_text_buffer_get_iter_at_offset (buffer, &start, 0);
658 gtk_text_buffer_insert (buffer, &start, text, len);
661 g_object_notify (G_OBJECT (buffer), "text");
671 gtk_text_buffer_real_insert_text (GtkTextBuffer *buffer,
676 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
677 g_return_if_fail (iter != NULL);
679 _gtk_text_btree_insert (iter, text, len);
681 g_signal_emit (buffer, signals[CHANGED], 0);
682 g_object_notify (G_OBJECT (buffer), "cursor-position");
686 gtk_text_buffer_emit_insert (GtkTextBuffer *buffer,
691 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
692 g_return_if_fail (iter != NULL);
693 g_return_if_fail (text != NULL);
698 g_return_if_fail (g_utf8_validate (text, len, NULL));
702 g_signal_emit (buffer, signals[INSERT_TEXT], 0,
708 * gtk_text_buffer_insert:
709 * @buffer: a #GtkTextBuffer
710 * @iter: a position in the buffer
711 * @text: UTF-8 format text to insert
712 * @len: length of text in bytes, or -1
714 * Inserts @len bytes of @text at position @iter. If @len is -1,
715 * @text must be nul-terminated and will be inserted in its
716 * entirety. Emits the "insert_text" signal; insertion actually occurs
717 * in the default handler for the signal. @iter is invalidated when
718 * insertion occurs (because the buffer contents change), but the
719 * default signal handler revalidates it to point to the end of the
724 gtk_text_buffer_insert (GtkTextBuffer *buffer,
729 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
730 g_return_if_fail (iter != NULL);
731 g_return_if_fail (text != NULL);
732 g_return_if_fail (gtk_text_iter_get_buffer (iter) == buffer);
734 gtk_text_buffer_emit_insert (buffer, iter, text, len);
738 * gtk_text_buffer_insert_at_cursor:
739 * @buffer: a #GtkTextBuffer
740 * @text: some text in UTF-8 format
741 * @len: length of text, in bytes
743 * Simply calls gtk_text_buffer_insert (), using the current
744 * cursor position as the insertion point.
747 gtk_text_buffer_insert_at_cursor (GtkTextBuffer *buffer,
753 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
754 g_return_if_fail (text != NULL);
756 gtk_text_buffer_get_iter_at_mark (buffer, &iter,
757 gtk_text_buffer_get_mark (buffer,
760 gtk_text_buffer_insert (buffer, &iter, text, len);
764 * gtk_text_buffer_insert_interactive:
765 * @buffer: a #GtkTextBuffer
766 * @iter: a position in @buffer
767 * @text: some UTF-8 text
768 * @len: length of text in bytes, or -1
769 * @default_editable: default editability of buffer
771 * Like gtk_text_buffer_insert(), but the insertion will not occur if
772 * @iter is at a non-editable location in the buffer. Usually you
773 * want to prevent insertions at ineditable locations if the insertion
774 * results from a user action (is interactive).
776 * @default_editable indicates the editability of text that doesn't
777 * have a tag affecting editability applied to it. Typically the
778 * result of gtk_text_view_get_editable() is appropriate here.
780 * Return value: whether text was actually inserted
783 gtk_text_buffer_insert_interactive (GtkTextBuffer *buffer,
787 gboolean default_editable)
789 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
790 g_return_val_if_fail (text != NULL, FALSE);
791 g_return_val_if_fail (gtk_text_iter_get_buffer (iter) == buffer, FALSE);
793 if (gtk_text_iter_can_insert (iter, default_editable))
795 gtk_text_buffer_begin_user_action (buffer);
796 gtk_text_buffer_emit_insert (buffer, iter, text, len);
797 gtk_text_buffer_end_user_action (buffer);
805 * gtk_text_buffer_insert_interactive_at_cursor:
806 * @buffer: a #GtkTextBuffer
807 * @text: text in UTF-8 format
808 * @len: length of text in bytes, or -1
809 * @default_editable: default editability of buffer
811 * Calls gtk_text_buffer_insert_interactive () at the cursor
814 * @default_editable indicates the editability of text that doesn't
815 * have a tag affecting editability applied to it. Typically the
816 * result of gtk_text_view_get_editable() is appropriate here.
818 * Return value: whether text was actually inserted
821 gtk_text_buffer_insert_interactive_at_cursor (GtkTextBuffer *buffer,
824 gboolean default_editable)
828 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
829 g_return_val_if_fail (text != NULL, FALSE);
831 gtk_text_buffer_get_iter_at_mark (buffer, &iter,
832 gtk_text_buffer_get_mark (buffer,
835 return gtk_text_buffer_insert_interactive (buffer, &iter, text, len,
840 possibly_not_text (gunichar ch,
843 return ch == GTK_TEXT_UNKNOWN_CHAR;
847 insert_text_range (GtkTextBuffer *buffer,
849 const GtkTextIter *orig_start,
850 const GtkTextIter *orig_end,
851 gboolean interactive)
855 text = gtk_text_iter_get_text (orig_start, orig_end);
857 gtk_text_buffer_emit_insert (buffer, iter, text, -1);
862 typedef struct _Range Range;
865 GtkTextBuffer *buffer;
866 GtkTextMark *start_mark;
867 GtkTextMark *end_mark;
868 GtkTextMark *whole_end_mark;
869 GtkTextIter *range_start;
870 GtkTextIter *range_end;
871 GtkTextIter *whole_end;
875 save_range (GtkTextIter *range_start,
876 GtkTextIter *range_end,
877 GtkTextIter *whole_end)
881 r = g_new (Range, 1);
883 r->buffer = gtk_text_iter_get_buffer (range_start);
884 g_object_ref (r->buffer);
887 gtk_text_buffer_create_mark (gtk_text_iter_get_buffer (range_start),
892 gtk_text_buffer_create_mark (gtk_text_iter_get_buffer (range_start),
898 gtk_text_buffer_create_mark (gtk_text_iter_get_buffer (range_start),
903 r->range_start = range_start;
904 r->range_end = range_end;
905 r->whole_end = whole_end;
911 restore_range (Range *r)
913 gtk_text_buffer_get_iter_at_mark (r->buffer,
917 gtk_text_buffer_get_iter_at_mark (r->buffer,
921 gtk_text_buffer_get_iter_at_mark (r->buffer,
925 gtk_text_buffer_delete_mark (r->buffer, r->start_mark);
926 gtk_text_buffer_delete_mark (r->buffer, r->end_mark);
927 gtk_text_buffer_delete_mark (r->buffer, r->whole_end_mark);
929 /* Due to the gravities on the marks, the ordering could have
930 * gotten mangled; we switch to an empty range in that
934 if (gtk_text_iter_compare (r->range_start, r->range_end) > 0)
935 *r->range_start = *r->range_end;
937 if (gtk_text_iter_compare (r->range_end, r->whole_end) > 0)
938 *r->range_end = *r->whole_end;
940 g_object_unref (r->buffer);
945 insert_range_untagged (GtkTextBuffer *buffer,
947 const GtkTextIter *orig_start,
948 const GtkTextIter *orig_end,
949 gboolean interactive)
951 GtkTextIter range_start;
952 GtkTextIter range_end;
953 GtkTextIter start, end;
956 if (gtk_text_iter_equal (orig_start, orig_end))
967 if (gtk_text_iter_equal (&range_start, &range_end))
969 /* Figure out how to move forward */
971 g_assert (gtk_text_iter_compare (&range_end, &end) <= 0);
973 if (gtk_text_iter_equal (&range_end, &end))
975 /* nothing left to do */
978 else if (gtk_text_iter_get_char (&range_end) == GTK_TEXT_UNKNOWN_CHAR)
980 GdkPixbuf *pixbuf = NULL;
981 GtkTextChildAnchor *anchor = NULL;
982 pixbuf = gtk_text_iter_get_pixbuf (&range_end);
983 anchor = gtk_text_iter_get_child_anchor (&range_end);
987 r = save_range (&range_start,
991 gtk_text_buffer_insert_pixbuf (buffer,
998 gtk_text_iter_forward_char (&range_end);
1000 range_start = range_end;
1004 /* Just skip anchors */
1006 gtk_text_iter_forward_char (&range_end);
1007 range_start = range_end;
1011 /* The GTK_TEXT_UNKNOWN_CHAR was in a text segment, so
1014 gtk_text_iter_forward_find_char (&range_end,
1015 possibly_not_text, NULL,
1018 g_assert (gtk_text_iter_compare (&range_end, &end) <= 0);
1023 /* Text segment starts here, so forward search to
1024 * find its possible endpoint
1026 gtk_text_iter_forward_find_char (&range_end,
1027 possibly_not_text, NULL,
1030 g_assert (gtk_text_iter_compare (&range_end, &end) <= 0);
1035 r = save_range (&range_start,
1039 insert_text_range (buffer,
1048 range_start = range_end;
1054 insert_range_not_inside_self (GtkTextBuffer *buffer,
1056 const GtkTextIter *orig_start,
1057 const GtkTextIter *orig_end,
1058 gboolean interactive)
1060 /* Find each range of uniformly-tagged text, insert it,
1061 * then apply the tags.
1063 GtkTextIter start = *orig_start;
1064 GtkTextIter end = *orig_end;
1065 GtkTextIter range_start;
1066 GtkTextIter range_end;
1068 if (gtk_text_iter_equal (orig_start, orig_end))
1071 gtk_text_iter_order (&start, &end);
1073 range_start = start;
1079 GtkTextIter start_iter;
1084 if (gtk_text_iter_equal (&range_start, &end))
1085 break; /* All done */
1087 g_assert (gtk_text_iter_compare (&range_start, &end) < 0);
1089 gtk_text_iter_forward_to_tag_toggle (&range_end, NULL);
1091 g_assert (!gtk_text_iter_equal (&range_start, &range_end));
1093 /* Clamp to the end iterator */
1094 if (gtk_text_iter_compare (&range_end, &end) > 0)
1097 /* We have a range with unique tags; insert it, and
1100 start_offset = gtk_text_iter_get_offset (iter);
1102 r = save_range (&range_start, &range_end, &end);
1104 insert_range_untagged (buffer, iter, &range_start, &range_end, interactive);
1109 gtk_text_buffer_get_iter_at_offset (buffer, &start_iter, start_offset);
1111 tags = gtk_text_iter_get_tags (&range_start);
1113 while (tmp_list != NULL)
1115 gtk_text_buffer_apply_tag (buffer,
1120 tmp_list = g_slist_next (tmp_list);
1122 g_slist_free (tags);
1124 range_start = range_end;
1129 gtk_text_buffer_real_insert_range (GtkTextBuffer *buffer,
1131 const GtkTextIter *orig_start,
1132 const GtkTextIter *orig_end,
1133 gboolean interactive)
1135 GtkTextBuffer *src_buffer;
1137 /* Find each range of uniformly-tagged text, insert it,
1138 * then apply the tags.
1140 if (gtk_text_iter_equal (orig_start, orig_end))
1144 gtk_text_buffer_begin_user_action (buffer);
1146 src_buffer = gtk_text_iter_get_buffer (orig_start);
1148 if (gtk_text_iter_get_buffer (iter) != src_buffer ||
1149 !gtk_text_iter_in_range (iter, orig_start, orig_end))
1151 insert_range_not_inside_self (buffer, iter, orig_start, orig_end, interactive);
1155 /* If you insert a range into itself, it could loop infinitely
1156 * because the region being copied keeps growing as we insert. So
1157 * we have to separately copy the range before and after
1158 * the insertion point.
1160 GtkTextIter start = *orig_start;
1161 GtkTextIter end = *orig_end;
1162 GtkTextIter range_start;
1163 GtkTextIter range_end;
1167 gtk_text_iter_order (&start, &end);
1169 range_start = start;
1171 first_half = save_range (&range_start, &range_end, &end);
1173 range_start = *iter;
1175 second_half = save_range (&range_start, &range_end, &end);
1177 restore_range (first_half);
1178 insert_range_not_inside_self (buffer, iter, &range_start, &range_end, interactive);
1180 restore_range (second_half);
1181 insert_range_not_inside_self (buffer, iter, &range_start, &range_end, interactive);
1185 gtk_text_buffer_end_user_action (buffer);
1189 * gtk_text_buffer_insert_range:
1190 * @buffer: a #GtkTextBuffer
1191 * @iter: a position in @buffer
1192 * @start: a position in a #GtkTextBuffer
1193 * @end: another position in the same buffer as @start
1195 * Copies text, tags, and pixbufs between @start and @end (the order
1196 * of @start and @end doesn't matter) and inserts the copy at @iter.
1197 * Used instead of simply getting/inserting text because it preserves
1198 * images and tags. If @start and @end are in a different buffer from
1199 * @buffer, the two buffers must share the same tag table.
1201 * Implemented via emissions of the insert_text and apply_tag signals,
1205 gtk_text_buffer_insert_range (GtkTextBuffer *buffer,
1207 const GtkTextIter *start,
1208 const GtkTextIter *end)
1210 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1211 g_return_if_fail (iter != NULL);
1212 g_return_if_fail (start != NULL);
1213 g_return_if_fail (end != NULL);
1214 g_return_if_fail (gtk_text_iter_get_buffer (start) ==
1215 gtk_text_iter_get_buffer (end));
1216 g_return_if_fail (gtk_text_iter_get_buffer (start)->tag_table ==
1218 g_return_if_fail (gtk_text_iter_get_buffer (iter) == buffer);
1220 gtk_text_buffer_real_insert_range (buffer, iter, start, end, FALSE);
1224 * gtk_text_buffer_insert_range_interactive:
1225 * @buffer: a #GtkTextBuffer
1226 * @iter: a position in @buffer
1227 * @start: a position in a #GtkTextBuffer
1228 * @end: another position in the same buffer as @start
1229 * @default_editable: default editability of the buffer
1231 * Same as gtk_text_buffer_insert_range(), but does nothing if the
1232 * insertion point isn't editable. The @default_editable parameter
1233 * indicates whether the text is editable at @iter if no tags
1234 * enclosing @iter affect editability. Typically the result of
1235 * gtk_text_view_get_editable() is appropriate here.
1237 * Returns: whether an insertion was possible at @iter
1240 gtk_text_buffer_insert_range_interactive (GtkTextBuffer *buffer,
1242 const GtkTextIter *start,
1243 const GtkTextIter *end,
1244 gboolean default_editable)
1246 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
1247 g_return_val_if_fail (iter != NULL, FALSE);
1248 g_return_val_if_fail (start != NULL, FALSE);
1249 g_return_val_if_fail (end != NULL, FALSE);
1250 g_return_val_if_fail (gtk_text_iter_get_buffer (start) ==
1251 gtk_text_iter_get_buffer (end), FALSE);
1252 g_return_val_if_fail (gtk_text_iter_get_buffer (start)->tag_table ==
1253 buffer->tag_table, FALSE);
1256 if (gtk_text_iter_can_insert (iter, default_editable))
1258 gtk_text_buffer_real_insert_range (buffer, iter, start, end, TRUE);
1266 * gtk_text_buffer_insert_with_tags:
1267 * @buffer: a #GtkTextBuffer
1268 * @iter: an iterator in @buffer
1270 * @len: length of @text, or -1
1271 * @first_tag: first tag to apply to @text
1272 * @Varargs: NULL-terminated list of tags to apply
1274 * Inserts @text into @buffer at @iter, applying the list of tags to
1275 * the newly-inserted text. The last tag specified must be NULL to
1276 * terminate the list. Equivalent to calling gtk_text_buffer_insert (),
1277 * then gtk_text_buffer_apply_tag () on the inserted text;
1278 * gtk_text_buffer_insert_with_tags () is just a convenience function.
1281 gtk_text_buffer_insert_with_tags (GtkTextBuffer *buffer,
1285 GtkTextTag *first_tag,
1293 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1294 g_return_if_fail (iter != NULL);
1295 g_return_if_fail (text != NULL);
1296 g_return_if_fail (gtk_text_iter_get_buffer (iter) == buffer);
1298 start_offset = gtk_text_iter_get_offset (iter);
1300 gtk_text_buffer_insert (buffer, iter, text, len);
1302 if (first_tag == NULL)
1305 gtk_text_buffer_get_iter_at_offset (buffer, &start, start_offset);
1307 va_start (args, first_tag);
1311 gtk_text_buffer_apply_tag (buffer, tag, &start, iter);
1313 tag = va_arg (args, GtkTextTag*);
1320 * gtk_text_buffer_insert_with_tags_by_name:
1321 * @buffer: a #GtkTextBuffer
1322 * @iter: position in @buffer
1324 * @len: length of @text, or -1
1325 * @first_tag_name: name of a tag to apply to @text
1326 * @Varargs: more tag names
1328 * Same as gtk_text_buffer_insert_with_tags (), but allows you
1329 * to pass in tag names instead of tag objects.
1332 gtk_text_buffer_insert_with_tags_by_name (GtkTextBuffer *buffer,
1336 const gchar *first_tag_name,
1342 const gchar *tag_name;
1344 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1345 g_return_if_fail (iter != NULL);
1346 g_return_if_fail (text != NULL);
1347 g_return_if_fail (gtk_text_iter_get_buffer (iter) == buffer);
1349 start_offset = gtk_text_iter_get_offset (iter);
1351 gtk_text_buffer_insert (buffer, iter, text, len);
1353 if (first_tag_name == NULL)
1356 gtk_text_buffer_get_iter_at_offset (buffer, &start, start_offset);
1358 va_start (args, first_tag_name);
1359 tag_name = first_tag_name;
1364 tag = gtk_text_tag_table_lookup (buffer->tag_table,
1369 g_warning ("%s: no tag with name '%s'!", G_STRLOC, tag_name);
1373 gtk_text_buffer_apply_tag (buffer, tag, &start, iter);
1375 tag_name = va_arg (args, const gchar*);
1387 gtk_text_buffer_real_delete_range (GtkTextBuffer *buffer,
1391 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1392 g_return_if_fail (start != NULL);
1393 g_return_if_fail (end != NULL);
1395 _gtk_text_btree_delete (start, end);
1397 /* may have deleted the selection... */
1398 update_selection_clipboards (buffer);
1400 g_signal_emit (buffer, signals[CHANGED], 0);
1401 g_object_notify (G_OBJECT (buffer), "cursor-position");
1405 gtk_text_buffer_emit_delete (GtkTextBuffer *buffer,
1409 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1410 g_return_if_fail (start != NULL);
1411 g_return_if_fail (end != NULL);
1413 if (gtk_text_iter_equal (start, end))
1416 gtk_text_iter_order (start, end);
1418 g_signal_emit (buffer,
1419 signals[DELETE_RANGE],
1425 * gtk_text_buffer_delete:
1426 * @buffer: a #GtkTextBuffer
1427 * @start: a position in @buffer
1428 * @end: another position in @buffer
1430 * Deletes text between @start and @end. The order of @start and @end
1431 * is not actually relevant; gtk_text_buffer_delete () will reorder
1432 * them. This function actually emits the "delete_range" signal, and
1433 * the default handler of that signal deletes the text. Because the
1434 * buffer is modified, all outstanding iterators become invalid after
1435 * calling this function; however, the @start and @end will be
1436 * re-initialized to point to the location where text was deleted.
1440 gtk_text_buffer_delete (GtkTextBuffer *buffer,
1444 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1445 g_return_if_fail (start != NULL);
1446 g_return_if_fail (end != NULL);
1447 g_return_if_fail (gtk_text_iter_get_buffer (start) == buffer);
1448 g_return_if_fail (gtk_text_iter_get_buffer (end) == buffer);
1450 gtk_text_buffer_emit_delete (buffer, start, end);
1454 * gtk_text_buffer_delete_interactive:
1455 * @buffer: a #GtkTextBuffer
1456 * @start_iter: start of range to delete
1457 * @end_iter: end of range
1458 * @default_editable: whether the buffer is editable by default
1460 * Deletes all <emphasis>editable</emphasis> text in the given range.
1461 * Calls gtk_text_buffer_delete () for each editable sub-range of
1462 * [@start,@end). @start and @end are revalidated to point to
1463 * the location of the last deleted range, or left untouched if
1464 * no text was deleted.
1466 * Return value: whether some text was actually deleted
1469 gtk_text_buffer_delete_interactive (GtkTextBuffer *buffer,
1470 GtkTextIter *start_iter,
1471 GtkTextIter *end_iter,
1472 gboolean default_editable)
1474 GtkTextMark *end_mark;
1475 GtkTextMark *start_mark;
1477 gboolean current_state;
1478 gboolean deleted_stuff = FALSE;
1480 /* Delete all editable text in the range start_iter, end_iter */
1482 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
1483 g_return_val_if_fail (start_iter != NULL, FALSE);
1484 g_return_val_if_fail (end_iter != NULL, FALSE);
1485 g_return_val_if_fail (gtk_text_iter_get_buffer (start_iter) == buffer, FALSE);
1486 g_return_val_if_fail (gtk_text_iter_get_buffer (end_iter) == buffer, FALSE);
1489 gtk_text_buffer_begin_user_action (buffer);
1491 gtk_text_iter_order (start_iter, end_iter);
1493 start_mark = gtk_text_buffer_create_mark (buffer, NULL,
1495 end_mark = gtk_text_buffer_create_mark (buffer, NULL,
1498 gtk_text_buffer_get_iter_at_mark (buffer, &iter, start_mark);
1500 current_state = gtk_text_iter_editable (&iter, default_editable);
1505 gboolean done = FALSE;
1508 gtk_text_iter_forward_to_tag_toggle (&iter, NULL);
1510 gtk_text_buffer_get_iter_at_mark (buffer, &end, end_mark);
1512 if (gtk_text_iter_compare (&iter, &end) >= 0)
1515 iter = end; /* clamp to the last boundary */
1518 new_state = gtk_text_iter_editable (&iter, default_editable);
1520 if (current_state == new_state)
1526 /* We're ending an editable region. Delete said region. */
1529 gtk_text_buffer_get_iter_at_mark (buffer, &start, start_mark);
1531 gtk_text_buffer_emit_delete (buffer, &start, &iter);
1533 deleted_stuff = TRUE;
1535 /* revalidate user's iterators. */
1536 *start_iter = start;
1546 if (current_state && !new_state)
1548 /* End of an editable region. Delete it. */
1551 gtk_text_buffer_get_iter_at_mark (buffer, &start, start_mark);
1553 gtk_text_buffer_emit_delete (buffer, &start, &iter);
1555 current_state = FALSE;
1556 deleted_stuff = TRUE;
1558 /* revalidate user's iterators. */
1559 *start_iter = start;
1564 /* We are at the start of an editable region. We won't be deleting
1565 * the previous region. Move start mark to start of this region.
1568 g_assert (!current_state && new_state);
1570 gtk_text_buffer_move_mark (buffer, start_mark,
1574 current_state = TRUE;
1582 gtk_text_buffer_delete_mark (buffer, start_mark);
1583 gtk_text_buffer_delete_mark (buffer, end_mark);
1585 gtk_text_buffer_end_user_action (buffer);
1587 return deleted_stuff;
1591 * Extracting textual buffer contents
1595 * gtk_text_buffer_get_text:
1596 * @buffer: a #GtkTextBuffer
1597 * @start: start of a range
1598 * @end: end of a range
1599 * @include_hidden_chars: whether to include invisible text
1601 * Returns the text in the range [@start,@end). Excludes undisplayed
1602 * text (text marked with tags that set the invisibility attribute) if
1603 * @include_hidden_chars is %FALSE. Does not include characters
1604 * representing embedded images, so byte and character indexes into
1605 * the returned string do <emphasis>not</emphasis> correspond to byte
1606 * and character indexes into the buffer. Contrast with
1607 * gtk_text_buffer_get_slice ().
1609 * Return value: an allocated UTF-8 string
1612 gtk_text_buffer_get_text (GtkTextBuffer *buffer,
1613 const GtkTextIter *start,
1614 const GtkTextIter *end,
1615 gboolean include_hidden_chars)
1617 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
1618 g_return_val_if_fail (start != NULL, NULL);
1619 g_return_val_if_fail (end != NULL, NULL);
1620 g_return_val_if_fail (gtk_text_iter_get_buffer (start) == buffer, NULL);
1621 g_return_val_if_fail (gtk_text_iter_get_buffer (end) == buffer, NULL);
1623 if (include_hidden_chars)
1624 return gtk_text_iter_get_text (start, end);
1626 return gtk_text_iter_get_visible_text (start, end);
1630 * gtk_text_buffer_get_slice:
1631 * @buffer: a #GtkTextBuffer
1632 * @start: start of a range
1633 * @end: end of a range
1634 * @include_hidden_chars: whether to include invisible text
1636 * Returns the text in the range [@start,@end). Excludes undisplayed
1637 * text (text marked with tags that set the invisibility attribute) if
1638 * @include_hidden_chars is %FALSE. The returned string includes a
1639 * 0xFFFC character whenever the buffer contains
1640 * embedded images, so byte and character indexes into
1641 * the returned string <emphasis>do</emphasis> correspond to byte
1642 * and character indexes into the buffer. Contrast with
1643 * gtk_text_buffer_get_text (). Note that 0xFFFC can occur in normal
1644 * text as well, so it is not a reliable indicator that a pixbuf or
1645 * widget is in the buffer.
1647 * Return value: an allocated UTF-8 string
1650 gtk_text_buffer_get_slice (GtkTextBuffer *buffer,
1651 const GtkTextIter *start,
1652 const GtkTextIter *end,
1653 gboolean include_hidden_chars)
1655 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
1656 g_return_val_if_fail (start != NULL, NULL);
1657 g_return_val_if_fail (end != NULL, NULL);
1658 g_return_val_if_fail (gtk_text_iter_get_buffer (start) == buffer, NULL);
1659 g_return_val_if_fail (gtk_text_iter_get_buffer (end) == buffer, NULL);
1661 if (include_hidden_chars)
1662 return gtk_text_iter_get_slice (start, end);
1664 return gtk_text_iter_get_visible_slice (start, end);
1672 gtk_text_buffer_real_insert_pixbuf (GtkTextBuffer *buffer,
1676 _gtk_text_btree_insert_pixbuf (iter, pixbuf);
1678 g_signal_emit (buffer, signals[CHANGED], 0);
1682 * gtk_text_buffer_insert_pixbuf:
1683 * @buffer: a #GtkTextBuffer
1684 * @iter: location to insert the pixbuf
1685 * @pixbuf: a #GdkPixbuf
1687 * Inserts an image into the text buffer at @iter. The image will be
1688 * counted as one character in character counts, and when obtaining
1689 * the buffer contents as a string, will be represented by the Unicode
1690 * "object replacement character" 0xFFFC. Note that the "slice"
1691 * variants for obtaining portions of the buffer as a string include
1692 * this character for pixbufs, but the "text" variants do
1693 * not. e.g. see gtk_text_buffer_get_slice() and
1694 * gtk_text_buffer_get_text().
1698 gtk_text_buffer_insert_pixbuf (GtkTextBuffer *buffer,
1702 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1703 g_return_if_fail (iter != NULL);
1704 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
1705 g_return_if_fail (gtk_text_iter_get_buffer (iter) == buffer);
1707 g_signal_emit (buffer, signals[INSERT_PIXBUF], 0,
1717 gtk_text_buffer_real_insert_anchor (GtkTextBuffer *buffer,
1719 GtkTextChildAnchor *anchor)
1721 _gtk_text_btree_insert_child_anchor (iter, anchor);
1723 g_signal_emit (buffer, signals[CHANGED], 0);
1727 * gtk_text_buffer_insert_child_anchor:
1728 * @buffer: a #GtkTextBuffer
1729 * @iter: location to insert the anchor
1730 * @anchor: a #GtkTextChildAnchor
1732 * Inserts a child widget anchor into the text buffer at @iter. The
1733 * anchor will be counted as one character in character counts, and
1734 * when obtaining the buffer contents as a string, will be represented
1735 * by the Unicode "object replacement character" 0xFFFC. Note that the
1736 * "slice" variants for obtaining portions of the buffer as a string
1737 * include this character for child anchors, but the "text" variants do
1738 * not. e.g. see gtk_text_buffer_get_slice() and
1739 * gtk_text_buffer_get_text(). Consider
1740 * gtk_text_buffer_create_child_anchor() as a more convenient
1741 * alternative to this function. The buffer will add a reference to
1742 * the anchor, so you can unref it after insertion.
1746 gtk_text_buffer_insert_child_anchor (GtkTextBuffer *buffer,
1748 GtkTextChildAnchor *anchor)
1750 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1751 g_return_if_fail (iter != NULL);
1752 g_return_if_fail (GTK_IS_TEXT_CHILD_ANCHOR (anchor));
1753 g_return_if_fail (gtk_text_iter_get_buffer (iter) == buffer);
1755 g_signal_emit (buffer, signals[INSERT_CHILD_ANCHOR], 0,
1761 * gtk_text_buffer_create_child_anchor:
1762 * @buffer: a #GtkTextBuffer
1763 * @iter: location in the buffer
1765 * This is a convenience function which simply creates a child anchor
1766 * with gtk_text_child_anchor_new() and inserts it into the buffer
1767 * with gtk_text_buffer_insert_child_anchor(). The new anchor is
1768 * owned by the buffer; no reference count is returned to
1769 * the caller of gtk_text_buffer_create_child_anchor().
1771 * Return value: the created child anchor
1774 gtk_text_buffer_create_child_anchor (GtkTextBuffer *buffer,
1777 GtkTextChildAnchor *anchor;
1779 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
1780 g_return_val_if_fail (iter != NULL, NULL);
1781 g_return_val_if_fail (gtk_text_iter_get_buffer (iter) == buffer, NULL);
1783 anchor = gtk_text_child_anchor_new ();
1785 gtk_text_buffer_insert_child_anchor (buffer, iter, anchor);
1787 g_object_unref (anchor);
1797 gtk_text_buffer_mark_set (GtkTextBuffer *buffer,
1798 const GtkTextIter *location,
1801 /* IMO this should NOT work like insert_text and delete_range,
1802 * where the real action happens in the default handler.
1804 * The reason is that the default handler would be _required_,
1805 * i.e. the whole widget would start breaking and segfaulting if the
1806 * default handler didn't get run. So you can't really override the
1807 * default handler or stop the emission; that is, this signal is
1808 * purely for notification, and not to allow users to modify the
1812 g_object_ref (mark);
1814 g_signal_emit (buffer,
1820 g_object_unref (mark);
1824 * gtk_text_buffer_set_mark:
1825 * @buffer: a #GtkTextBuffer
1826 * @mark_name: name of the mark
1827 * @iter: location for the mark.
1828 * @left_gravity: if the mark is created by this function, gravity for the new
1830 * @should_exist: if %TRUE, warn if the mark does not exist, and return
1833 * Move the mark to the given position, if not @should_exist, create the mark.
1835 * Return value: mark
1838 gtk_text_buffer_set_mark (GtkTextBuffer *buffer,
1839 GtkTextMark *existing_mark,
1840 const gchar *mark_name,
1841 const GtkTextIter *iter,
1842 gboolean left_gravity,
1843 gboolean should_exist)
1845 GtkTextIter location;
1848 g_return_val_if_fail (gtk_text_iter_get_buffer (iter) == buffer, NULL);
1850 mark = _gtk_text_btree_set_mark (get_btree (buffer),
1857 if (_gtk_text_btree_mark_is_insert (get_btree (buffer), mark) ||
1858 _gtk_text_btree_mark_is_selection_bound (get_btree (buffer), mark))
1860 update_selection_clipboards (buffer);
1863 _gtk_text_btree_get_iter_at_mark (get_btree (buffer),
1867 gtk_text_buffer_mark_set (buffer, &location, mark);
1873 * gtk_text_buffer_create_mark:
1874 * @buffer: a #GtkTextBuffer
1875 * @mark_name: name for mark, or %NULL
1876 * @where: location to place mark
1877 * @left_gravity: whether the mark has left gravity
1879 * Creates a mark at position @where. If @mark_name is %NULL, the mark
1880 * is anonymous; otherwise, the mark can be retrieved by name using
1881 * gtk_text_buffer_get_mark (). If a mark has left gravity, and text is
1882 * inserted at the mark's current location, the mark will be moved to
1883 * the left of the newly-inserted text. If the mark has right gravity
1884 * (@left_gravity = %FALSE), the mark will end up on the right of
1885 * newly-inserted text. The standard left-to-right cursor is a mark
1886 * with right gravity (when you type, the cursor stays on the right
1887 * side of the text you're typing).
1889 * The caller of this function does <emphasis>not</emphasis> own a reference
1890 * to the returned #GtkTextMark, so you can ignore the return value
1891 * if you like. Marks are owned by the buffer and go away when the
1894 * Emits the "mark_set" signal as notification of the mark's initial
1897 * Return value: the new #GtkTextMark object
1900 gtk_text_buffer_create_mark (GtkTextBuffer *buffer,
1901 const gchar *mark_name,
1902 const GtkTextIter *where,
1903 gboolean left_gravity)
1905 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
1907 return gtk_text_buffer_set_mark (buffer, NULL, mark_name, where,
1908 left_gravity, FALSE);
1912 * gtk_text_buffer_move_mark:
1913 * @buffer: a #GtkTextBuffer
1914 * @mark: a #GtkTextMark
1915 * @where: new location for @mark in @buffer
1917 * Moves @mark to the new location @where. Emits the "mark_set" signal
1918 * as notification of the move.
1921 gtk_text_buffer_move_mark (GtkTextBuffer *buffer,
1923 const GtkTextIter *where)
1925 g_return_if_fail (GTK_IS_TEXT_MARK (mark));
1926 g_return_if_fail (!gtk_text_mark_get_deleted (mark));
1927 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1929 gtk_text_buffer_set_mark (buffer, mark, NULL, where, FALSE, TRUE);
1933 * gtk_text_buffer_get_iter_at_mark:
1934 * @buffer: a #GtkTextBuffer
1935 * @iter: iterator to initialize
1936 * @mark: a #GtkTextMark in @buffer
1938 * Initializes @iter with the current position of @mark.
1941 gtk_text_buffer_get_iter_at_mark (GtkTextBuffer *buffer,
1945 g_return_if_fail (GTK_IS_TEXT_MARK (mark));
1946 g_return_if_fail (!gtk_text_mark_get_deleted (mark));
1947 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1949 _gtk_text_btree_get_iter_at_mark (get_btree (buffer),
1955 * gtk_text_buffer_delete_mark:
1956 * @buffer: a #GtkTextBuffer
1957 * @mark: a #GtkTextMark in @buffer
1959 * Deletes @mark, so that it's no longer located anywhere in the
1960 * buffer. Removes the reference the buffer holds to the mark, so if
1961 * you haven't called g_object_ref () on the mark, it will be freed. Even
1962 * if the mark isn't freed, most operations on @mark become
1963 * invalid. There is no way to undelete a
1964 * mark. gtk_text_mark_get_deleted () will return TRUE after this
1965 * function has been called on a mark; gtk_text_mark_get_deleted ()
1966 * indicates that a mark no longer belongs to a buffer. The "mark_deleted"
1967 * signal will be emitted as notification after the mark is deleted.
1970 gtk_text_buffer_delete_mark (GtkTextBuffer *buffer,
1973 g_return_if_fail (GTK_IS_TEXT_MARK (mark));
1974 g_return_if_fail (!gtk_text_mark_get_deleted (mark));
1975 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1977 g_object_ref (mark);
1979 _gtk_text_btree_remove_mark (get_btree (buffer), mark);
1981 /* See rationale above for MARK_SET on why we emit this after
1982 * removing the mark, rather than removing the mark in a default
1985 g_signal_emit (buffer, signals[MARK_DELETED],
1989 g_object_unref (mark);
1993 * gtk_text_buffer_get_mark:
1994 * @buffer: a #GtkTextBuffer
1995 * @name: a mark name
1997 * Returns the mark named @name in buffer @buffer, or NULL if no such
1998 * mark exists in the buffer.
2000 * Return value: a #GtkTextMark, or NULL
2003 gtk_text_buffer_get_mark (GtkTextBuffer *buffer,
2008 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
2009 g_return_val_if_fail (name != NULL, NULL);
2011 mark = _gtk_text_btree_get_mark_by_name (get_btree (buffer), name);
2018 * gtk_text_buffer_move_mark_by_name:
2019 * @buffer: a #GtkTextBuffer
2020 * @name: name of a mark
2021 * @where: new location for mark
2023 * Moves the mark named @name (which must exist) to location @where.
2024 * See gtk_text_buffer_move_mark () for details.
2027 gtk_text_buffer_move_mark_by_name (GtkTextBuffer *buffer,
2029 const GtkTextIter *where)
2033 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2034 g_return_if_fail (name != NULL);
2036 mark = _gtk_text_btree_get_mark_by_name (get_btree (buffer), name);
2040 g_warning ("%s: no mark named '%s'", G_STRLOC, name);
2044 gtk_text_buffer_move_mark (buffer, mark, where);
2048 * gtk_text_buffer_delete_mark_by_name:
2049 * @buffer: a #GtkTextBuffer
2050 * @name: name of a mark in @buffer
2052 * Deletes the mark named @name; the mark must exist. See
2053 * gtk_text_buffer_delete_mark () for details.
2056 gtk_text_buffer_delete_mark_by_name (GtkTextBuffer *buffer,
2061 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2062 g_return_if_fail (name != NULL);
2064 mark = _gtk_text_btree_get_mark_by_name (get_btree (buffer), name);
2068 g_warning ("%s: no mark named '%s'", G_STRLOC, name);
2072 gtk_text_buffer_delete_mark (buffer, mark);
2076 * gtk_text_buffer_get_insert:
2077 * @buffer: a #GtkTextBuffer
2079 * Returns the mark that represents the cursor (insertion point).
2080 * Equivalent to calling gtk_text_buffer_get_mark () to get the mark
2081 * named "insert", but very slightly more efficient, and involves less
2084 * Return value: insertion point mark
2087 gtk_text_buffer_get_insert (GtkTextBuffer *buffer)
2089 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
2091 /* FIXME use struct member in btree */
2092 return gtk_text_buffer_get_mark (buffer, "insert");
2096 * gtk_text_buffer_get_selection_bound:
2097 * @buffer: a #GtkTextBuffer
2099 * Returns the mark that represents the selection bound. Equivalent
2100 * to calling gtk_text_buffer_get_mark () to get the mark named
2101 * "selection_bound", but very slightly more efficient, and involves
2104 * The currently-selected text in @buffer is the region between the
2105 * "selection_bound" and "insert" marks. If "selection_bound" and
2106 * "insert" are in the same place, then there is no current selection.
2107 * gtk_text_buffer_get_selection_bounds() is another convenient function
2108 * for handling the selection, if you just want to know whether there's a
2109 * selection and what its bounds are.
2111 * Return value: selection bound mark
2114 gtk_text_buffer_get_selection_bound (GtkTextBuffer *buffer)
2116 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
2118 /* FIXME use struct member in btree */
2119 return gtk_text_buffer_get_mark (buffer, "selection_bound");
2123 * gtk_text_buffer_get_iter_at_child_anchor:
2124 * @buffer: a #GtkTextBuffer
2125 * @iter: an iterator to be initialized
2126 * @anchor: a child anchor that appears in @buffer
2128 * Obtains the location of @anchor within @buffer.
2132 gtk_text_buffer_get_iter_at_child_anchor (GtkTextBuffer *buffer,
2134 GtkTextChildAnchor *anchor)
2136 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2137 g_return_if_fail (iter != NULL);
2138 g_return_if_fail (GTK_IS_TEXT_CHILD_ANCHOR (anchor));
2139 g_return_if_fail (!gtk_text_child_anchor_get_deleted (anchor));
2141 _gtk_text_btree_get_iter_at_child_anchor (get_btree (buffer),
2147 * gtk_text_buffer_place_cursor:
2148 * @buffer: a #GtkTextBuffer
2149 * @where: where to put the cursor
2151 * This function moves the "insert" and "selection_bound" marks
2152 * simultaneously. If you move them to the same place in two steps
2153 * with gtk_text_buffer_move_mark(), you will temporarily select a
2154 * region in between their old and new locations, which can be pretty
2155 * inefficient since the temporarily-selected region will force stuff
2156 * to be recalculated. This function moves them as a unit, which can
2160 gtk_text_buffer_place_cursor (GtkTextBuffer *buffer,
2161 const GtkTextIter *where)
2163 gtk_text_buffer_select_range (buffer, where, where);
2168 * gtk_text_buffer_select_range:
2169 * @buffer: a #GtkTextBuffer
2170 * @ins: where to put the "insert" mark
2171 * @bound: where to put the "selection_bound" mark
2173 * This function moves the "insert" and "selection_bound" marks
2174 * simultaneously. If you move them in two steps
2175 * with gtk_text_buffer_move_mark(), you will temporarily select a
2176 * region in between their old and new locations, which can be pretty
2177 * inefficient since the temporarily-selected region will force stuff
2178 * to be recalculated. This function moves them as a unit, which can
2184 gtk_text_buffer_select_range (GtkTextBuffer *buffer,
2185 const GtkTextIter *ins,
2186 const GtkTextIter *bound)
2188 GtkTextIter real_ins;
2189 GtkTextIter real_bound;
2191 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2194 real_bound = *bound;
2196 _gtk_text_btree_select_range (get_btree (buffer), &real_ins, &real_bound);
2197 gtk_text_buffer_mark_set (buffer, &real_ins,
2198 gtk_text_buffer_get_mark (buffer,
2200 gtk_text_buffer_mark_set (buffer, &real_bound,
2201 gtk_text_buffer_get_mark (buffer,
2202 "selection_bound"));
2203 update_selection_clipboards (buffer);
2211 * gtk_text_buffer_create_tag:
2212 * @buffer: a #GtkTextBuffer
2213 * @tag_name: name of the new tag, or %NULL
2214 * @first_property_name: name of first property to set, or %NULL
2215 * @Varargs: %NULL-terminated list of property names and values
2218 * Creates a tag and adds it to the tag table for @buffer.
2219 * Equivalent to calling gtk_text_tag_new () and then adding the
2220 * tag to the buffer's tag table. The returned tag is owned by
2221 * the buffer's tag table, so the ref count will be equal to one.
2223 * If @tag_name is %NULL, the tag is anonymous.
2225 * If @tag_name is non-%NULL, a tag called @tag_name must not already
2226 * exist in the tag table for this buffer.
2228 * The @first_property_name argument and subsequent arguments are a list
2229 * of properties to set on the tag, as with g_object_set().
2231 * Return value: a new tag
2234 gtk_text_buffer_create_tag (GtkTextBuffer *buffer,
2235 const gchar *tag_name,
2236 const gchar *first_property_name,
2242 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
2244 tag = gtk_text_tag_new (tag_name);
2246 gtk_text_tag_table_add (get_table (buffer), tag);
2248 if (first_property_name)
2250 va_start (list, first_property_name);
2251 g_object_set_valist (G_OBJECT (tag), first_property_name, list);
2255 g_object_unref (tag);
2261 gtk_text_buffer_real_apply_tag (GtkTextBuffer *buffer,
2263 const GtkTextIter *start,
2264 const GtkTextIter *end)
2266 if (tag->table != buffer->tag_table)
2268 g_warning ("Can only apply tags that are in the tag table for the buffer");
2272 _gtk_text_btree_tag (start, end, tag, TRUE);
2276 gtk_text_buffer_real_remove_tag (GtkTextBuffer *buffer,
2278 const GtkTextIter *start,
2279 const GtkTextIter *end)
2281 if (tag->table != buffer->tag_table)
2283 g_warning ("Can only remove tags that are in the tag table for the buffer");
2287 _gtk_text_btree_tag (start, end, tag, FALSE);
2291 gtk_text_buffer_real_changed (GtkTextBuffer *buffer)
2293 gtk_text_buffer_set_modified (buffer, TRUE);
2297 gtk_text_buffer_real_mark_set (GtkTextBuffer *buffer,
2298 const GtkTextIter *iter,
2301 GtkTextMark *insert;
2303 insert = gtk_text_buffer_get_insert (buffer);
2305 if (mark == insert || mark == gtk_text_buffer_get_selection_bound (buffer))
2307 gboolean has_selection;
2309 has_selection = gtk_text_buffer_get_selection_bounds (buffer,
2313 if (has_selection != buffer->has_selection)
2315 buffer->has_selection = has_selection;
2316 g_object_notify (G_OBJECT (buffer), "has-selection");
2321 g_object_notify (G_OBJECT (buffer), "cursor-position");
2325 gtk_text_buffer_emit_tag (GtkTextBuffer *buffer,
2328 const GtkTextIter *start,
2329 const GtkTextIter *end)
2331 GtkTextIter start_tmp = *start;
2332 GtkTextIter end_tmp = *end;
2334 g_return_if_fail (tag != NULL);
2336 gtk_text_iter_order (&start_tmp, &end_tmp);
2339 g_signal_emit (buffer, signals[APPLY_TAG],
2341 tag, &start_tmp, &end_tmp);
2343 g_signal_emit (buffer, signals[REMOVE_TAG],
2345 tag, &start_tmp, &end_tmp);
2350 * gtk_text_buffer_apply_tag:
2351 * @buffer: a #GtkTextBuffer
2352 * @tag: a #GtkTextTag
2353 * @start: one bound of range to be tagged
2354 * @end: other bound of range to be tagged
2356 * Emits the "apply_tag" signal on @buffer. The default
2357 * handler for the signal applies @tag to the given range.
2358 * @start and @end do not have to be in order.
2362 gtk_text_buffer_apply_tag (GtkTextBuffer *buffer,
2364 const GtkTextIter *start,
2365 const GtkTextIter *end)
2367 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2368 g_return_if_fail (GTK_IS_TEXT_TAG (tag));
2369 g_return_if_fail (start != NULL);
2370 g_return_if_fail (end != NULL);
2371 g_return_if_fail (gtk_text_iter_get_buffer (start) == buffer);
2372 g_return_if_fail (gtk_text_iter_get_buffer (end) == buffer);
2373 g_return_if_fail (tag->table == buffer->tag_table);
2375 gtk_text_buffer_emit_tag (buffer, tag, TRUE, start, end);
2379 * gtk_text_buffer_remove_tag:
2380 * @buffer: a #GtkTextBuffer
2381 * @tag: a #GtkTextTag
2382 * @start: one bound of range to be untagged
2383 * @end: other bound of range to be untagged
2385 * Emits the "remove_tag" signal. The default handler for the signal
2386 * removes all occurrences of @tag from the given range. @start and
2387 * @end don't have to be in order.
2391 gtk_text_buffer_remove_tag (GtkTextBuffer *buffer,
2393 const GtkTextIter *start,
2394 const GtkTextIter *end)
2397 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2398 g_return_if_fail (GTK_IS_TEXT_TAG (tag));
2399 g_return_if_fail (start != NULL);
2400 g_return_if_fail (end != NULL);
2401 g_return_if_fail (gtk_text_iter_get_buffer (start) == buffer);
2402 g_return_if_fail (gtk_text_iter_get_buffer (end) == buffer);
2403 g_return_if_fail (tag->table == buffer->tag_table);
2405 gtk_text_buffer_emit_tag (buffer, tag, FALSE, start, end);
2410 * gtk_text_buffer_apply_tag_by_name:
2411 * @buffer: a #GtkTextBuffer
2412 * @name: name of a named #GtkTextTag
2413 * @start: one bound of range to be tagged
2414 * @end: other bound of range to be tagged
2416 * Calls gtk_text_tag_table_lookup() on the buffer's tag table to
2417 * get a #GtkTextTag, then calls gtk_text_buffer_apply_tag().
2421 gtk_text_buffer_apply_tag_by_name (GtkTextBuffer *buffer,
2423 const GtkTextIter *start,
2424 const GtkTextIter *end)
2428 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2429 g_return_if_fail (name != NULL);
2430 g_return_if_fail (start != NULL);
2431 g_return_if_fail (end != NULL);
2432 g_return_if_fail (gtk_text_iter_get_buffer (start) == buffer);
2433 g_return_if_fail (gtk_text_iter_get_buffer (end) == buffer);
2435 tag = gtk_text_tag_table_lookup (get_table (buffer),
2440 g_warning ("Unknown tag `%s'", name);
2444 gtk_text_buffer_emit_tag (buffer, tag, TRUE, start, end);
2448 * gtk_text_buffer_remove_tag_by_name:
2449 * @buffer: a #GtkTextBuffer
2450 * @name: name of a #GtkTextTag
2451 * @start: one bound of range to be untagged
2452 * @end: other bound of range to be untagged
2454 * Calls gtk_text_tag_table_lookup() on the buffer's tag table to
2455 * get a #GtkTextTag, then calls gtk_text_buffer_remove_tag().
2460 gtk_text_buffer_remove_tag_by_name (GtkTextBuffer *buffer,
2462 const GtkTextIter *start,
2463 const GtkTextIter *end)
2467 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2468 g_return_if_fail (name != NULL);
2469 g_return_if_fail (start != NULL);
2470 g_return_if_fail (end != NULL);
2471 g_return_if_fail (gtk_text_iter_get_buffer (start) == buffer);
2472 g_return_if_fail (gtk_text_iter_get_buffer (end) == buffer);
2474 tag = gtk_text_tag_table_lookup (get_table (buffer),
2479 g_warning ("Unknown tag `%s'", name);
2483 gtk_text_buffer_emit_tag (buffer, tag, FALSE, start, end);
2487 pointer_cmp (gconstpointer a,
2499 * gtk_text_buffer_remove_all_tags:
2500 * @buffer: a #GtkTextBuffer
2501 * @start: one bound of range to be untagged
2502 * @end: other bound of range to be untagged
2504 * Removes all tags in the range between @start and @end. Be careful
2505 * with this function; it could remove tags added in code unrelated to
2506 * the code you're currently writing. That is, using this function is
2507 * probably a bad idea if you have two or more unrelated code sections
2511 gtk_text_buffer_remove_all_tags (GtkTextBuffer *buffer,
2512 const GtkTextIter *start,
2513 const GtkTextIter *end)
2515 GtkTextIter first, second, tmp;
2518 GSList *prev, *next;
2521 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2522 g_return_if_fail (start != NULL);
2523 g_return_if_fail (end != NULL);
2524 g_return_if_fail (gtk_text_iter_get_buffer (start) == buffer);
2525 g_return_if_fail (gtk_text_iter_get_buffer (end) == buffer);
2530 gtk_text_iter_order (&first, &second);
2532 /* Get all tags turned on at the start */
2533 tags = gtk_text_iter_get_tags (&first);
2535 /* Find any that are toggled on within the range */
2537 while (gtk_text_iter_forward_to_tag_toggle (&tmp, NULL))
2542 if (gtk_text_iter_compare (&tmp, &second) >= 0)
2543 break; /* past the end of the range */
2545 toggled = gtk_text_iter_get_toggled_tags (&tmp, TRUE);
2547 /* We could end up with a really big-ass list here.
2550 tmp_list2 = toggled;
2551 while (tmp_list2 != NULL)
2553 tags = g_slist_prepend (tags, tmp_list2->data);
2555 tmp_list2 = g_slist_next (tmp_list2);
2558 g_slist_free (toggled);
2562 tags = g_slist_sort (tags, pointer_cmp);
2564 /* Strip duplicates */
2568 while (tmp_list != NULL)
2570 if (tag == tmp_list->data)
2573 next = tmp_list->next;
2577 tmp_list->next = NULL;
2579 g_slist_free (tmp_list);
2582 /* prev is unchanged */
2586 /* not a duplicate */
2587 tag = GTK_TEXT_TAG (tmp_list->data);
2589 tmp_list = tmp_list->next;
2593 g_slist_foreach (tags, (GFunc) g_object_ref, NULL);
2596 while (tmp_list != NULL)
2598 tag = GTK_TEXT_TAG (tmp_list->data);
2600 gtk_text_buffer_remove_tag (buffer, tag, &first, &second);
2602 tmp_list = tmp_list->next;
2605 g_slist_foreach (tags, (GFunc) g_object_unref, NULL);
2607 g_slist_free (tags);
2612 * Obtain various iterators
2616 * gtk_text_buffer_get_iter_at_line_offset:
2617 * @buffer: a #GtkTextBuffer
2618 * @iter: iterator to initialize
2619 * @line_number: line number counting from 0
2620 * @char_offset: char offset from start of line
2622 * Obtains an iterator pointing to @char_offset within the given
2623 * line. The @char_offset must exist, offsets off the end of the line
2624 * are not allowed. Note <emphasis>characters</emphasis>, not bytes;
2625 * UTF-8 may encode one character as multiple bytes.
2629 gtk_text_buffer_get_iter_at_line_offset (GtkTextBuffer *buffer,
2634 g_return_if_fail (iter != NULL);
2635 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2637 _gtk_text_btree_get_iter_at_line_char (get_btree (buffer),
2638 iter, line_number, char_offset);
2642 * gtk_text_buffer_get_iter_at_line_index:
2643 * @buffer: a #GtkTextBuffer
2644 * @iter: iterator to initialize
2645 * @line_number: line number counting from 0
2646 * @byte_index: byte index from start of line
2648 * Obtains an iterator pointing to @byte_index within the given line.
2649 * @byte_index must be the start of a UTF-8 character, and must not be
2650 * beyond the end of the line. Note <emphasis>bytes</emphasis>, not
2651 * characters; UTF-8 may encode one character as multiple bytes.
2655 gtk_text_buffer_get_iter_at_line_index (GtkTextBuffer *buffer,
2660 g_return_if_fail (iter != NULL);
2661 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2663 _gtk_text_btree_get_iter_at_line_byte (get_btree (buffer),
2664 iter, line_number, byte_index);
2668 * gtk_text_buffer_get_iter_at_line:
2669 * @buffer: a #GtkTextBuffer
2670 * @iter: iterator to initialize
2671 * @line_number: line number counting from 0
2673 * Initializes @iter to the start of the given line.
2676 gtk_text_buffer_get_iter_at_line (GtkTextBuffer *buffer,
2680 g_return_if_fail (iter != NULL);
2681 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2683 gtk_text_buffer_get_iter_at_line_offset (buffer, iter, line_number, 0);
2687 * gtk_text_buffer_get_iter_at_offset:
2688 * @buffer: a #GtkTextBuffer
2689 * @iter: iterator to initialize
2690 * @char_offset: char offset from start of buffer, counting from 0, or -1
2692 * Initializes @iter to a position @char_offset chars from the start
2693 * of the entire buffer. If @char_offset is -1 or greater than the number
2694 * of characters in the buffer, @iter is initialized to the end iterator,
2695 * the iterator one past the last valid character in the buffer.
2698 gtk_text_buffer_get_iter_at_offset (GtkTextBuffer *buffer,
2702 g_return_if_fail (iter != NULL);
2703 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2705 _gtk_text_btree_get_iter_at_char (get_btree (buffer), iter, char_offset);
2709 * gtk_text_buffer_get_start_iter:
2710 * @buffer: a #GtkTextBuffer
2711 * @iter: iterator to initialize
2713 * Initialized @iter with the first position in the text buffer. This
2714 * is the same as using gtk_text_buffer_get_iter_at_offset() to get
2715 * the iter at character offset 0.
2718 gtk_text_buffer_get_start_iter (GtkTextBuffer *buffer,
2721 g_return_if_fail (iter != NULL);
2722 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2724 _gtk_text_btree_get_iter_at_char (get_btree (buffer), iter, 0);
2728 * gtk_text_buffer_get_end_iter:
2729 * @buffer: a #GtkTextBuffer
2730 * @iter: iterator to initialize
2732 * Initializes @iter with the "end iterator," one past the last valid
2733 * character in the text buffer. If dereferenced with
2734 * gtk_text_iter_get_char(), the end iterator has a character value of
2735 * 0. The entire buffer lies in the range from the first position in
2736 * the buffer (call gtk_text_buffer_get_start_iter() to get
2737 * character position 0) to the end iterator.
2741 gtk_text_buffer_get_end_iter (GtkTextBuffer *buffer,
2744 g_return_if_fail (iter != NULL);
2745 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2747 _gtk_text_btree_get_end_iter (get_btree (buffer), iter);
2751 * gtk_text_buffer_get_bounds:
2752 * @buffer: a #GtkTextBuffer
2753 * @start: iterator to initialize with first position in the buffer
2754 * @end: iterator to initialize with the end iterator
2756 * Retrieves the first and last iterators in the buffer, i.e. the
2757 * entire buffer lies within the range [@start,@end).
2761 gtk_text_buffer_get_bounds (GtkTextBuffer *buffer,
2765 g_return_if_fail (start != NULL);
2766 g_return_if_fail (end != NULL);
2767 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2769 _gtk_text_btree_get_iter_at_char (get_btree (buffer), start, 0);
2770 _gtk_text_btree_get_end_iter (get_btree (buffer), end);
2778 * gtk_text_buffer_get_modified:
2779 * @buffer: a #GtkTextBuffer
2781 * Indicates whether the buffer has been modified since the last call
2782 * to gtk_text_buffer_set_modified() set the modification flag to
2783 * %FALSE. Used for example to enable a "save" function in a text
2786 * Return value: %TRUE if the buffer has been modified
2789 gtk_text_buffer_get_modified (GtkTextBuffer *buffer)
2791 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
2793 return buffer->modified;
2797 * gtk_text_buffer_set_modified:
2798 * @buffer: a #GtkTextBuffer
2799 * @setting: modification flag setting
2801 * Used to keep track of whether the buffer has been modified since the
2802 * last time it was saved. Whenever the buffer is saved to disk, call
2803 * gtk_text_buffer_set_modified (@buffer, FALSE). When the buffer is modified,
2804 * it will automatically toggled on the modified bit again. When the modified
2805 * bit flips, the buffer emits a "modified_changed" signal.
2809 gtk_text_buffer_set_modified (GtkTextBuffer *buffer,
2812 gboolean fixed_setting;
2814 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2816 fixed_setting = setting != FALSE;
2818 if (buffer->modified == fixed_setting)
2822 buffer->modified = fixed_setting;
2823 g_signal_emit (buffer, signals[MODIFIED_CHANGED], 0);
2828 * gtk_text_buffer_get_has_selection:
2829 * @buffer: a #GtkTextBuffer
2831 * Indicates whether the buffer has some text currently selected.
2833 * Return value: %TRUE if the there is text selected
2838 gtk_text_buffer_get_has_selection (GtkTextBuffer *buffer)
2840 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
2842 return buffer->has_selection;
2847 * Assorted other stuff
2851 * gtk_text_buffer_get_line_count:
2852 * @buffer: a #GtkTextBuffer
2854 * Obtains the number of lines in the buffer. This value is cached, so
2855 * the function is very fast.
2857 * Return value: number of lines in the buffer
2860 gtk_text_buffer_get_line_count (GtkTextBuffer *buffer)
2862 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), 0);
2864 return _gtk_text_btree_line_count (get_btree (buffer));
2868 * gtk_text_buffer_get_char_count:
2869 * @buffer: a #GtkTextBuffer
2871 * Gets the number of characters in the buffer; note that characters
2872 * and bytes are not the same, you can't e.g. expect the contents of
2873 * the buffer in string form to be this many bytes long. The character
2874 * count is cached, so this function is very fast.
2876 * Return value: number of characters in the buffer
2879 gtk_text_buffer_get_char_count (GtkTextBuffer *buffer)
2881 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), 0);
2883 return _gtk_text_btree_char_count (get_btree (buffer));
2886 /* Called when we lose the primary selection.
2889 clipboard_clear_selection_cb (GtkClipboard *clipboard,
2892 /* Move selection_bound to the insertion point */
2894 GtkTextIter selection_bound;
2895 GtkTextBuffer *buffer = GTK_TEXT_BUFFER (data);
2897 gtk_text_buffer_get_iter_at_mark (buffer, &insert,
2898 gtk_text_buffer_get_mark (buffer, "insert"));
2899 gtk_text_buffer_get_iter_at_mark (buffer, &selection_bound,
2900 gtk_text_buffer_get_mark (buffer, "selection_bound"));
2902 if (!gtk_text_iter_equal (&insert, &selection_bound))
2903 gtk_text_buffer_move_mark (buffer,
2904 gtk_text_buffer_get_mark (buffer, "selection_bound"),
2908 /* Called when we have the primary selection and someone else wants our
2909 * data in order to paste it.
2912 clipboard_get_selection_cb (GtkClipboard *clipboard,
2913 GtkSelectionData *selection_data,
2917 GtkTextBuffer *buffer = GTK_TEXT_BUFFER (data);
2918 GtkTextIter start, end;
2920 if (gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
2922 if (info == GTK_TEXT_BUFFER_TARGET_INFO_BUFFER_CONTENTS)
2924 /* Provide the address of the buffer; this will only be
2925 * used within-process
2927 gtk_selection_data_set (selection_data,
2928 selection_data->target,
2933 else if (info == GTK_TEXT_BUFFER_TARGET_INFO_RICH_TEXT)
2938 str = gtk_text_buffer_serialize (buffer, buffer,
2939 selection_data->target,
2940 &start, &end, &len);
2942 gtk_selection_data_set (selection_data,
2943 selection_data->target,
2952 str = gtk_text_iter_get_visible_text (&start, &end);
2953 gtk_selection_data_set_text (selection_data, str, -1);
2959 static GtkTextBuffer *
2960 create_clipboard_contents_buffer (GtkTextBuffer *buffer)
2962 GtkTextBuffer *contents;
2964 contents = gtk_text_buffer_new (gtk_text_buffer_get_tag_table (buffer));
2966 g_object_set_data (G_OBJECT (contents), I_("gtk-text-buffer-clipboard-source"),
2968 g_object_set_data (G_OBJECT (contents), I_("gtk-text-buffer-clipboard"),
2969 GINT_TO_POINTER (1));
2971 /* Ref the source buffer as long as the clipboard contents buffer
2972 * exists, because it's needed for serializing the contents buffer.
2973 * See http://bugzilla.gnome.org/show_bug.cgi?id=339195
2975 g_object_ref (buffer);
2976 g_object_weak_ref (G_OBJECT (contents), (GWeakNotify) g_object_unref, buffer);
2981 /* Provide cut/copied data */
2983 clipboard_get_contents_cb (GtkClipboard *clipboard,
2984 GtkSelectionData *selection_data,
2988 GtkTextBuffer *contents = GTK_TEXT_BUFFER (data);
2990 g_assert (contents); /* This should never be called unless we own the clipboard */
2992 if (info == GTK_TEXT_BUFFER_TARGET_INFO_BUFFER_CONTENTS)
2994 /* Provide the address of the clipboard buffer; this will only
2995 * be used within-process. OK to supply a NULL value for contents.
2997 gtk_selection_data_set (selection_data,
2998 selection_data->target,
3003 else if (info == GTK_TEXT_BUFFER_TARGET_INFO_RICH_TEXT)
3005 GtkTextBuffer *clipboard_source_buffer;
3006 GtkTextIter start, end;
3010 clipboard_source_buffer = g_object_get_data (G_OBJECT (contents),
3011 "gtk-text-buffer-clipboard-source");
3013 gtk_text_buffer_get_bounds (contents, &start, &end);
3015 str = gtk_text_buffer_serialize (clipboard_source_buffer, contents,
3016 selection_data->target,
3017 &start, &end, &len);
3019 gtk_selection_data_set (selection_data,
3020 selection_data->target,
3028 GtkTextIter start, end;
3030 gtk_text_buffer_get_bounds (contents, &start, &end);
3032 str = gtk_text_iter_get_visible_text (&start, &end);
3033 gtk_selection_data_set_text (selection_data, str, -1);
3039 clipboard_clear_contents_cb (GtkClipboard *clipboard,
3042 GtkTextBuffer *contents = GTK_TEXT_BUFFER (data);
3044 g_object_unref (contents);
3048 get_paste_point (GtkTextBuffer *buffer,
3050 gboolean clear_afterward)
3052 GtkTextIter insert_point;
3053 GtkTextMark *paste_point_override;
3055 paste_point_override = gtk_text_buffer_get_mark (buffer,
3056 "gtk_paste_point_override");
3058 if (paste_point_override != NULL)
3060 gtk_text_buffer_get_iter_at_mark (buffer, &insert_point,
3061 paste_point_override);
3062 if (clear_afterward)
3063 gtk_text_buffer_delete_mark (buffer,
3064 gtk_text_buffer_get_mark (buffer,
3065 "gtk_paste_point_override"));
3069 gtk_text_buffer_get_iter_at_mark (buffer, &insert_point,
3070 gtk_text_buffer_get_mark (buffer,
3074 *iter = insert_point;
3078 pre_paste_prep (ClipboardRequest *request_data,
3079 GtkTextIter *insert_point)
3081 GtkTextBuffer *buffer = request_data->buffer;
3083 get_paste_point (buffer, insert_point, TRUE);
3085 /* If we're going to replace the selection, we insert before it to
3086 * avoid messing it up, then we delete the selection after inserting.
3088 if (request_data->replace_selection)
3090 GtkTextIter start, end;
3092 if (gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
3093 *insert_point = start;
3098 post_paste_cleanup (ClipboardRequest *request_data)
3100 if (request_data->replace_selection)
3102 GtkTextIter start, end;
3104 if (gtk_text_buffer_get_selection_bounds (request_data->buffer,
3107 if (request_data->interactive)
3108 gtk_text_buffer_delete_interactive (request_data->buffer,
3111 request_data->default_editable);
3113 gtk_text_buffer_delete (request_data->buffer, &start, &end);
3119 free_clipboard_request (ClipboardRequest *request_data)
3121 g_object_unref (request_data->buffer);
3122 g_free (request_data);
3125 /* Called when we request a paste and receive the text data
3128 clipboard_text_received (GtkClipboard *clipboard,
3132 ClipboardRequest *request_data = data;
3133 GtkTextBuffer *buffer = request_data->buffer;
3137 GtkTextIter insert_point;
3139 if (request_data->interactive)
3140 gtk_text_buffer_begin_user_action (buffer);
3142 pre_paste_prep (request_data, &insert_point);
3144 if (request_data->interactive)
3145 gtk_text_buffer_insert_interactive (buffer, &insert_point,
3146 str, -1, request_data->default_editable);
3148 gtk_text_buffer_insert (buffer, &insert_point,
3151 post_paste_cleanup (request_data);
3153 if (request_data->interactive)
3154 gtk_text_buffer_end_user_action (buffer);
3157 free_clipboard_request (request_data);
3160 static GtkTextBuffer*
3161 selection_data_get_buffer (GtkSelectionData *selection_data,
3162 ClipboardRequest *request_data)
3165 GtkTextBuffer *src_buffer = NULL;
3167 /* If we can get the owner, the selection is in-process */
3168 owner = gdk_selection_owner_get_for_display (selection_data->display,
3169 selection_data->selection);
3174 if (gdk_window_get_window_type (owner) == GDK_WINDOW_FOREIGN)
3177 if (selection_data->type !=
3178 gdk_atom_intern_static_string ("GTK_TEXT_BUFFER_CONTENTS"))
3181 if (selection_data->length != sizeof (src_buffer))
3184 memcpy (&src_buffer, selection_data->data, sizeof (src_buffer));
3186 if (src_buffer == NULL)
3189 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (src_buffer), NULL);
3191 if (gtk_text_buffer_get_tag_table (src_buffer) !=
3192 gtk_text_buffer_get_tag_table (request_data->buffer))
3199 /* These are pretty handy functions; maybe something like them
3200 * should be in the public API. Also, there are other places in this
3201 * file where they could be used.
3204 save_iter (const GtkTextIter *iter,
3205 gboolean left_gravity)
3207 return gtk_text_buffer_create_mark (gtk_text_iter_get_buffer (iter),
3214 restore_iter (const GtkTextIter *iter,
3217 gtk_text_buffer_get_iter_at_mark (gtk_text_mark_get_buffer (save_id),
3218 (GtkTextIter*) iter,
3220 gtk_text_buffer_delete_mark (gtk_text_mark_get_buffer (save_id),
3226 clipboard_rich_text_received (GtkClipboard *clipboard,
3232 ClipboardRequest *request_data = data;
3233 GtkTextIter insert_point;
3234 gboolean retval = TRUE;
3235 GError *error = NULL;
3236 GtkTextBufferPrivate *priv;
3238 priv = GTK_TEXT_BUFFER_GET_PRIVATE (request_data->buffer);
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;
3327 GtkTextBufferPrivate *priv;
3329 src_buffer = selection_data_get_buffer (selection_data, request_data);
3333 GtkTextIter start, end;
3335 if (g_object_get_data (G_OBJECT (src_buffer), "gtk-text-buffer-clipboard"))
3337 gtk_text_buffer_get_bounds (src_buffer, &start, &end);
3339 paste_from_buffer (request_data, src_buffer,
3344 if (gtk_text_buffer_get_selection_bounds (src_buffer, &start, &end))
3345 paste_from_buffer (request_data, src_buffer,
3351 priv = GTK_TEXT_BUFFER_GET_PRIVATE (request_data->buffer);
3353 if (gtk_clipboard_wait_is_rich_text_available (clipboard,
3354 request_data->buffer))
3356 /* Request rich text */
3357 gtk_clipboard_request_rich_text (clipboard,
3358 request_data->buffer,
3359 clipboard_rich_text_received,
3364 /* Request the text selection instead */
3365 gtk_clipboard_request_text (clipboard,
3366 clipboard_text_received,
3374 GtkClipboard *clipboard;
3376 } SelectionClipboard;
3379 update_selection_clipboards (GtkTextBuffer *buffer)
3381 GtkTextBufferPrivate *priv;
3382 GSList *tmp_list = buffer->selection_clipboards;
3384 priv = GTK_TEXT_BUFFER_GET_PRIVATE (buffer);
3386 gtk_text_buffer_get_copy_target_list (buffer);
3393 SelectionClipboard *selection_clipboard = tmp_list->data;
3394 GtkClipboard *clipboard = selection_clipboard->clipboard;
3396 /* Determine whether we have a selection and adjust X selection
3399 if (!gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
3401 if (gtk_clipboard_get_owner (clipboard) == G_OBJECT (buffer))
3402 gtk_clipboard_clear (clipboard);
3406 /* Even if we already have the selection, we need to update our
3409 if (!gtk_clipboard_set_with_owner (clipboard,
3410 priv->copy_target_entries,
3411 priv->n_copy_target_entries,
3412 clipboard_get_selection_cb,
3413 clipboard_clear_selection_cb,
3415 clipboard_clear_selection_cb (clipboard, buffer);
3418 tmp_list = tmp_list->next;
3422 static SelectionClipboard *
3423 find_selection_clipboard (GtkTextBuffer *buffer,
3424 GtkClipboard *clipboard)
3426 GSList *tmp_list = buffer->selection_clipboards;
3429 SelectionClipboard *selection_clipboard = tmp_list->data;
3430 if (selection_clipboard->clipboard == clipboard)
3431 return selection_clipboard;
3433 tmp_list = tmp_list->next;
3440 * gtk_text_buffer_add_selection_clipboard:
3441 * @buffer: a #GtkTextBuffer
3442 * @clipboard: a #GtkClipboard
3444 * Adds @clipboard to the list of clipboards in which the selection contents
3445 * of @buffer are available. In most cases, @clipboard will be the #GtkClipboard
3446 * of type %GDK_SELECTION_PRIMARY for a view of @buffer.
3449 gtk_text_buffer_add_selection_clipboard (GtkTextBuffer *buffer,
3450 GtkClipboard *clipboard)
3452 SelectionClipboard *selection_clipboard;
3454 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
3455 g_return_if_fail (clipboard != NULL);
3457 selection_clipboard = find_selection_clipboard (buffer, clipboard);
3458 if (selection_clipboard)
3460 selection_clipboard->ref_count++;
3464 selection_clipboard = g_new (SelectionClipboard, 1);
3466 selection_clipboard->clipboard = clipboard;
3467 selection_clipboard->ref_count = 1;
3469 buffer->selection_clipboards = g_slist_prepend (buffer->selection_clipboards, selection_clipboard);
3474 * gtk_text_buffer_remove_selection_clipboard:
3475 * @buffer: a #GtkTextBuffer
3476 * @clipboard: a #GtkClipboard added to @buffer by gtk_text_buffer_add_selection_clipboard().
3478 * Removes a #GtkClipboard added with gtk_text_buffer_add_selection_clipboard()
3481 gtk_text_buffer_remove_selection_clipboard (GtkTextBuffer *buffer,
3482 GtkClipboard *clipboard)
3484 SelectionClipboard *selection_clipboard;
3486 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
3487 g_return_if_fail (clipboard != NULL);
3489 selection_clipboard = find_selection_clipboard (buffer, clipboard);
3490 g_return_if_fail (selection_clipboard != NULL);
3492 selection_clipboard->ref_count--;
3493 if (selection_clipboard->ref_count == 0)
3495 if (gtk_clipboard_get_owner (selection_clipboard->clipboard) == G_OBJECT (buffer))
3496 gtk_clipboard_clear (selection_clipboard->clipboard);
3498 buffer->selection_clipboards = g_slist_remove (buffer->selection_clipboards,
3499 selection_clipboard);
3501 g_free (selection_clipboard);
3506 remove_all_selection_clipboards (GtkTextBuffer *buffer)
3508 GSList *tmp_list = buffer->selection_clipboards;
3511 SelectionClipboard *selection_clipboard = tmp_list->data;
3513 if (gtk_clipboard_get_owner (selection_clipboard->clipboard) == G_OBJECT (buffer))
3514 gtk_clipboard_clear (selection_clipboard->clipboard);
3516 g_free (selection_clipboard);
3518 tmp_list = tmp_list->next;
3521 g_slist_free (buffer->selection_clipboards);
3522 buffer->selection_clipboards = NULL;
3526 * gtk_text_buffer_paste_clipboard:
3527 * @buffer: a #GtkTextBuffer
3528 * @clipboard: the #GtkClipboard to paste from
3529 * @override_location: location to insert pasted text, or %NULL for at the cursor
3530 * @default_editable: whether the buffer is editable by default
3532 * Pastes the contents of a clipboard at the insertion point, or at @override_location.
3533 * (Note: pasting is asynchronous, that is, we'll ask for the paste data and
3534 * return, and at some point later after the main loop runs, the paste
3535 * data will be inserted.)
3539 gtk_text_buffer_paste_clipboard (GtkTextBuffer *buffer,
3540 GtkClipboard *clipboard,
3541 GtkTextIter *override_location,
3542 gboolean default_editable)
3544 ClipboardRequest *data = g_new (ClipboardRequest, 1);
3545 GtkTextIter paste_point;
3546 GtkTextIter start, end;
3548 if (override_location != NULL)
3549 gtk_text_buffer_create_mark (buffer,
3550 "gtk_paste_point_override",
3551 override_location, FALSE);
3553 data->buffer = g_object_ref (buffer);
3554 data->interactive = TRUE;
3555 data->default_editable = default_editable;
3557 /* When pasting with the cursor inside the selection area, you
3558 * replace the selection with the new text, otherwise, you
3559 * simply insert the new text at the point where the click
3560 * occured, unselecting any selected text. The replace_selection
3561 * flag toggles this behavior.
3563 data->replace_selection = FALSE;
3565 get_paste_point (buffer, &paste_point, FALSE);
3566 if (gtk_text_buffer_get_selection_bounds (buffer, &start, &end) &&
3567 (gtk_text_iter_in_range (&paste_point, &start, &end) ||
3568 gtk_text_iter_equal (&paste_point, &end)))
3569 data->replace_selection = TRUE;
3571 gtk_clipboard_request_contents (clipboard,
3572 gdk_atom_intern_static_string ("GTK_TEXT_BUFFER_CONTENTS"),
3573 clipboard_clipboard_buffer_received, data);
3577 * gtk_text_buffer_delete_selection:
3578 * @buffer: a #GtkTextBuffer
3579 * @interactive: whether the deletion is caused by user interaction
3580 * @default_editable: whether the buffer is editable by default
3582 * Deletes the range between the "insert" and "selection_bound" marks,
3583 * that is, the currently-selected text. If @interactive is %TRUE,
3584 * the editability of the selection will be considered (users can't delete
3587 * Return value: whether there was a non-empty selection to delete
3590 gtk_text_buffer_delete_selection (GtkTextBuffer *buffer,
3591 gboolean interactive,
3592 gboolean default_editable)
3597 if (!gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
3599 return FALSE; /* No selection */
3605 gtk_text_buffer_begin_user_action (buffer);
3606 gtk_text_buffer_delete_interactive (buffer, &start, &end, default_editable);
3607 gtk_text_buffer_end_user_action (buffer);
3610 gtk_text_buffer_delete (buffer, &start, &end);
3612 return TRUE; /* We deleted stuff */
3617 * gtk_text_buffer_backspace:
3618 * @buffer: a #GtkTextBuffer
3619 * @iter: a position in @buffer
3620 * @interactive: whether the deletion is caused by user interaction
3621 * @default_editable: whether the buffer is editable by default
3623 * Performs the appropriate action as if the user hit the delete
3624 * key with the cursor at the position specified by @iter. In the
3625 * normal case a single character will be deleted, but when
3626 * combining accents are involved, more than one character can
3627 * be deleted, and when precomposed character and accent combinations
3628 * are involved, less than one character will be deleted.
3630 * Because the buffer is modified, all outstanding iterators become
3631 * invalid after calling this function; however, the @iter will be
3632 * re-initialized to point to the location where text was deleted.
3634 * Return value: %TRUE if the buffer was modified
3639 gtk_text_buffer_backspace (GtkTextBuffer *buffer,
3641 gboolean interactive,
3642 gboolean default_editable)
3644 gchar *cluster_text;
3647 gboolean retval = FALSE;
3648 const PangoLogAttr *attrs;
3650 gboolean backspace_deletes_character;
3652 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
3653 g_return_val_if_fail (iter != NULL, FALSE);
3658 attrs = _gtk_text_buffer_get_line_log_attrs (buffer, &start, NULL);
3660 /* For no good reason, attrs is NULL for the empty last line in
3661 * a buffer. Special case that here. (#156164)
3665 offset = gtk_text_iter_get_line_offset (&start);
3666 backspace_deletes_character = attrs[offset].backspace_deletes_character;
3669 backspace_deletes_character = FALSE;
3671 gtk_text_iter_backward_cursor_position (&start);
3673 if (gtk_text_iter_equal (&start, &end))
3676 cluster_text = gtk_text_iter_get_text (&start, &end);
3679 gtk_text_buffer_begin_user_action (buffer);
3681 if (gtk_text_buffer_delete_interactive (buffer, &start, &end,
3684 if (backspace_deletes_character)
3686 gchar *normalized_text = g_utf8_normalize (cluster_text,
3687 strlen (cluster_text),
3689 glong len = g_utf8_strlen (normalized_text, -1);
3692 gtk_text_buffer_insert_interactive (buffer,
3695 g_utf8_offset_to_pointer (normalized_text, len - 1) - normalized_text,
3698 g_free (normalized_text);
3705 gtk_text_buffer_end_user_action (buffer);
3707 g_free (cluster_text);
3709 /* Revalidate the users iter */
3716 cut_or_copy (GtkTextBuffer *buffer,
3717 GtkClipboard *clipboard,
3718 gboolean delete_region_after,
3719 gboolean interactive,
3720 gboolean default_editable)
3722 GtkTextBufferPrivate *priv;
3724 /* We prefer to cut the selected region between selection_bound and
3725 * insertion point. If that region is empty, then we cut the region
3726 * between the "anchor" and the insertion point (this is for
3727 * C-space and M-w and other Emacs-style copy/yank keys). Note that
3728 * insert and selection_bound are guaranteed to exist, but the
3729 * anchor only exists sometimes.
3734 priv = GTK_TEXT_BUFFER_GET_PRIVATE (buffer);
3736 gtk_text_buffer_get_copy_target_list (buffer);
3738 if (!gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
3740 /* Let's try the anchor thing */
3741 GtkTextMark * anchor = gtk_text_buffer_get_mark (buffer, "anchor");
3747 gtk_text_buffer_get_iter_at_mark (buffer, &end, anchor);
3748 gtk_text_iter_order (&start, &end);
3752 if (!gtk_text_iter_equal (&start, &end))
3755 GtkTextBuffer *contents;
3757 contents = create_clipboard_contents_buffer (buffer);
3759 gtk_text_buffer_get_iter_at_offset (contents, &ins, 0);
3761 gtk_text_buffer_insert_range (contents, &ins, &start, &end);
3763 if (!gtk_clipboard_set_with_data (clipboard,
3764 priv->copy_target_entries,
3765 priv->n_copy_target_entries,
3766 clipboard_get_contents_cb,
3767 clipboard_clear_contents_cb,
3769 g_object_unref (contents);
3771 gtk_clipboard_set_can_store (clipboard,
3772 priv->copy_target_entries + 1,
3773 priv->n_copy_target_entries - 1);
3775 if (delete_region_after)
3778 gtk_text_buffer_delete_interactive (buffer, &start, &end,
3781 gtk_text_buffer_delete (buffer, &start, &end);
3787 * gtk_text_buffer_cut_clipboard:
3788 * @buffer: a #GtkTextBuffer
3789 * @clipboard: the #GtkClipboard object to cut to.
3790 * @default_editable: default editability of the buffer
3792 * Copies the currently-selected text to a clipboard, then deletes
3793 * said text if it's editable.
3797 gtk_text_buffer_cut_clipboard (GtkTextBuffer *buffer,
3798 GtkClipboard *clipboard,
3799 gboolean default_editable)
3801 gtk_text_buffer_begin_user_action (buffer);
3802 cut_or_copy (buffer, clipboard, TRUE, TRUE, default_editable);
3803 gtk_text_buffer_end_user_action (buffer);
3807 * gtk_text_buffer_copy_clipboard:
3808 * @buffer: a #GtkTextBuffer
3809 * @clipboard: the #GtkClipboard object to copy to.
3811 * Copies the currently-selected text to a clipboard.
3815 gtk_text_buffer_copy_clipboard (GtkTextBuffer *buffer,
3816 GtkClipboard *clipboard)
3818 gtk_text_buffer_begin_user_action (buffer);
3819 cut_or_copy (buffer, clipboard, FALSE, TRUE, TRUE);
3820 gtk_text_buffer_end_user_action (buffer);
3825 * gtk_text_buffer_get_selection_bounds:
3826 * @buffer: a #GtkTextBuffer a #GtkTextBuffer
3827 * @start: iterator to initialize with selection start
3828 * @end: iterator to initialize with selection end
3830 * Returns %TRUE if some text is selected; places the bounds
3831 * of the selection in @start and @end (if the selection has length 0,
3832 * then @start and @end are filled in with the same value).
3833 * @start and @end will be in ascending order. If @start and @end are
3834 * NULL, then they are not filled in, but the return value still indicates
3835 * whether text is selected.
3837 * Return value: whether the selection has nonzero length
3840 gtk_text_buffer_get_selection_bounds (GtkTextBuffer *buffer,
3844 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
3846 return _gtk_text_btree_get_selection_bounds (get_btree (buffer), start, end);
3850 * gtk_text_buffer_begin_user_action:
3851 * @buffer: a #GtkTextBuffer
3853 * Called to indicate that the buffer operations between here and a
3854 * call to gtk_text_buffer_end_user_action() are part of a single
3855 * user-visible operation. The operations between
3856 * gtk_text_buffer_begin_user_action() and
3857 * gtk_text_buffer_end_user_action() can then be grouped when creating
3858 * an undo stack. #GtkTextBuffer maintains a count of calls to
3859 * gtk_text_buffer_begin_user_action() that have not been closed with
3860 * a call to gtk_text_buffer_end_user_action(), and emits the "begin_user_action"
3861 * and "end_user_action" signals only for the outermost pair of calls.
3862 * This allows you to build user actions from other user actions.
3864 * The "interactive" buffer mutation functions, such as
3865 * gtk_text_buffer_insert_interactive(), automatically call begin/end
3866 * user action around the buffer operations they perform, so there's
3867 * no need to add extra calls if you user action consists solely of a
3868 * single call to one of those functions.
3871 gtk_text_buffer_begin_user_action (GtkTextBuffer *buffer)
3873 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
3875 buffer->user_action_count += 1;
3877 if (buffer->user_action_count == 1)
3879 /* Outermost nested user action begin emits the signal */
3880 g_signal_emit (buffer, signals[BEGIN_USER_ACTION], 0);
3885 * gtk_text_buffer_end_user_action:
3886 * @buffer: a #GtkTextBuffer
3888 * Should be paired with a call to gtk_text_buffer_begin_user_action().
3889 * See that function for a full explanation.
3892 gtk_text_buffer_end_user_action (GtkTextBuffer *buffer)
3894 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
3895 g_return_if_fail (buffer->user_action_count > 0);
3897 buffer->user_action_count -= 1;
3899 if (buffer->user_action_count == 0)
3901 /* Ended the outermost-nested user action end, so emit the signal */
3902 g_signal_emit (buffer, signals[END_USER_ACTION], 0);
3907 gtk_text_buffer_free_target_lists (GtkTextBuffer *buffer)
3909 GtkTextBufferPrivate *priv = GTK_TEXT_BUFFER_GET_PRIVATE (buffer);
3911 if (priv->copy_target_list)
3913 gtk_target_list_unref (priv->copy_target_list);
3914 priv->copy_target_list = NULL;
3916 gtk_target_table_free (priv->copy_target_entries,
3917 priv->n_copy_target_entries);
3918 priv->copy_target_entries = NULL;
3919 priv->n_copy_target_entries = 0;
3922 if (priv->paste_target_list)
3924 gtk_target_list_unref (priv->paste_target_list);
3925 priv->paste_target_list = NULL;
3927 gtk_target_table_free (priv->paste_target_entries,
3928 priv->n_paste_target_entries);
3929 priv->paste_target_entries = NULL;
3930 priv->n_paste_target_entries = 0;
3934 static GtkTargetList *
3935 gtk_text_buffer_get_target_list (GtkTextBuffer *buffer,
3936 gboolean deserializable,
3937 GtkTargetEntry **entries,
3940 GtkTargetList *target_list;
3942 target_list = gtk_target_list_new (NULL, 0);
3944 gtk_target_list_add (target_list,
3945 gdk_atom_intern_static_string ("GTK_TEXT_BUFFER_CONTENTS"),
3946 GTK_TARGET_SAME_APP,
3947 GTK_TEXT_BUFFER_TARGET_INFO_BUFFER_CONTENTS);
3949 gtk_target_list_add_rich_text_targets (target_list,
3950 GTK_TEXT_BUFFER_TARGET_INFO_RICH_TEXT,
3954 gtk_target_list_add_text_targets (target_list,
3955 GTK_TEXT_BUFFER_TARGET_INFO_TEXT);
3957 *entries = gtk_target_table_new_from_list (target_list, n_entries);
3963 * gtk_text_buffer_get_copy_target_list:
3964 * @buffer: a #GtkTextBuffer
3966 * This function returns the list of targets this text buffer can
3967 * provide for copying and as DND source. The targets in the list are
3968 * added with %info values from the #GtkTextBufferTargetInfo enum,
3969 * using gtk_target_list_add_rich_text_targets() and
3970 * gtk_target_list_add_text_targets()
3972 * Return value: the #GtkTargetList
3977 gtk_text_buffer_get_copy_target_list (GtkTextBuffer *buffer)
3979 GtkTextBufferPrivate *priv;
3981 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
3983 priv = GTK_TEXT_BUFFER_GET_PRIVATE (buffer);
3985 if (! priv->copy_target_list)
3986 priv->copy_target_list =
3987 gtk_text_buffer_get_target_list (buffer, FALSE,
3988 &priv->copy_target_entries,
3989 &priv->n_copy_target_entries);
3991 return priv->copy_target_list;
3995 * gtk_text_buffer_get_paste_target_list:
3996 * @buffer: a #GtkTextBuffer
3998 * This function returns the list of targets this text buffer supports
3999 * for pasting and as DND destination. The targets in the list are
4000 * added with %info values from the #GtkTextBufferTargetInfo enum,
4001 * using gtk_target_list_add_rich_text_targets() and
4002 * gtk_target_list_add_text_targets()
4004 * Return value: the #GtkTargetList
4009 gtk_text_buffer_get_paste_target_list (GtkTextBuffer *buffer)
4011 GtkTextBufferPrivate *priv;
4013 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
4015 priv = GTK_TEXT_BUFFER_GET_PRIVATE (buffer);
4017 if (! priv->paste_target_list)
4018 priv->paste_target_list =
4019 gtk_text_buffer_get_target_list (buffer, TRUE,
4020 &priv->paste_target_entries,
4021 &priv->n_paste_target_entries);
4023 return priv->paste_target_list;
4027 * Logical attribute cache
4030 #define ATTR_CACHE_SIZE 2
4032 typedef struct _CacheEntry CacheEntry;
4037 PangoLogAttr *attrs;
4041 struct _GtkTextLogAttrCache
4043 gint chars_changed_stamp;
4044 CacheEntry entries[ATTR_CACHE_SIZE];
4048 free_log_attr_cache (GtkTextLogAttrCache *cache)
4051 while (i < ATTR_CACHE_SIZE)
4053 g_free (cache->entries[i].attrs);
4060 clear_log_attr_cache (GtkTextLogAttrCache *cache)
4063 while (i < ATTR_CACHE_SIZE)
4065 g_free (cache->entries[i].attrs);
4066 cache->entries[i].attrs = NULL;
4071 static PangoLogAttr*
4072 compute_log_attrs (const GtkTextIter *iter,
4078 gint char_len, byte_len;
4079 PangoLogAttr *attrs = NULL;
4084 gtk_text_iter_set_line_offset (&start, 0);
4085 gtk_text_iter_forward_line (&end);
4087 paragraph = gtk_text_iter_get_slice (&start, &end);
4088 char_len = g_utf8_strlen (paragraph, -1);
4089 byte_len = strlen (paragraph);
4091 g_assert (char_len > 0);
4094 *char_lenp = char_len;
4096 attrs = g_new (PangoLogAttr, char_len + 1);
4098 /* FIXME we need to follow PangoLayout and allow different language
4099 * tags within the paragraph
4101 pango_get_log_attrs (paragraph, byte_len, -1,
4102 gtk_text_iter_get_language (&start),
4111 /* The return value from this is valid until you call this a second time.
4114 _gtk_text_buffer_get_line_log_attrs (GtkTextBuffer *buffer,
4115 const GtkTextIter *anywhere_in_line,
4119 GtkTextLogAttrCache *cache;
4122 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
4123 g_return_val_if_fail (anywhere_in_line != NULL, NULL);
4125 /* special-case for empty last line in buffer */
4126 if (gtk_text_iter_is_end (anywhere_in_line) &&
4127 gtk_text_iter_get_line_offset (anywhere_in_line) == 0)
4134 /* FIXME we also need to recompute log attrs if the language tag at
4135 * the start of a paragraph changes
4138 if (buffer->log_attr_cache == NULL)
4140 buffer->log_attr_cache = g_new0 (GtkTextLogAttrCache, 1);
4141 buffer->log_attr_cache->chars_changed_stamp =
4142 _gtk_text_btree_get_chars_changed_stamp (get_btree (buffer));
4144 else if (buffer->log_attr_cache->chars_changed_stamp !=
4145 _gtk_text_btree_get_chars_changed_stamp (get_btree (buffer)))
4147 clear_log_attr_cache (buffer->log_attr_cache);
4150 cache = buffer->log_attr_cache;
4151 line = gtk_text_iter_get_line (anywhere_in_line);
4154 while (i < ATTR_CACHE_SIZE)
4156 if (cache->entries[i].attrs &&
4157 cache->entries[i].line == line)
4160 *char_len = cache->entries[i].char_len;
4161 return cache->entries[i].attrs;
4166 /* Not in cache; open up the first cache entry */
4167 if (cache->entries[ATTR_CACHE_SIZE-1].attrs)
4168 g_free (cache->entries[ATTR_CACHE_SIZE-1].attrs);
4170 g_memmove (cache->entries + 1, cache->entries,
4171 sizeof (CacheEntry) * (ATTR_CACHE_SIZE - 1));
4173 cache->entries[0].line = line;
4174 cache->entries[0].attrs = compute_log_attrs (anywhere_in_line,
4175 &cache->entries[0].char_len);
4178 *char_len = cache->entries[0].char_len;
4180 return cache->entries[0].attrs;
4184 _gtk_text_buffer_notify_will_remove_tag (GtkTextBuffer *buffer,
4187 /* This removes tag from the buffer, but DOESN'T emit the
4188 * remove_tag signal, because we can't afford to have user
4189 * code messing things up at this point; the tag MUST be removed
4193 _gtk_text_btree_notify_will_remove_tag (buffer->btree, tag);
4201 _gtk_text_buffer_spew (GtkTextBuffer *buffer)
4203 _gtk_text_btree_spew (get_btree (buffer));
4206 #define __GTK_TEXT_BUFFER_C__
4207 #include "gtkaliasdef.c"