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;
98 PROP_COPY_TARGET_LIST,
99 PROP_PASTE_TARGET_LIST
102 static void gtk_text_buffer_init (GtkTextBuffer *tkxt_buffer);
103 static void gtk_text_buffer_class_init (GtkTextBufferClass *klass);
104 static void gtk_text_buffer_finalize (GObject *object);
106 static void gtk_text_buffer_real_insert_text (GtkTextBuffer *buffer,
110 static void gtk_text_buffer_real_insert_pixbuf (GtkTextBuffer *buffer,
113 static void gtk_text_buffer_real_insert_anchor (GtkTextBuffer *buffer,
115 GtkTextChildAnchor *anchor);
116 static void gtk_text_buffer_real_delete_range (GtkTextBuffer *buffer,
119 static void gtk_text_buffer_real_apply_tag (GtkTextBuffer *buffer,
121 const GtkTextIter *start_char,
122 const GtkTextIter *end_char);
123 static void gtk_text_buffer_real_remove_tag (GtkTextBuffer *buffer,
125 const GtkTextIter *start_char,
126 const GtkTextIter *end_char);
127 static void gtk_text_buffer_real_changed (GtkTextBuffer *buffer);
128 static void gtk_text_buffer_real_mark_set (GtkTextBuffer *buffer,
129 const GtkTextIter *iter,
132 static GtkTextBTree* get_btree (GtkTextBuffer *buffer);
133 static void free_log_attr_cache (GtkTextLogAttrCache *cache);
135 static void remove_all_selection_clipboards (GtkTextBuffer *buffer);
136 static void update_selection_clipboards (GtkTextBuffer *buffer);
138 static GtkTextBuffer *create_clipboard_contents_buffer (GtkTextBuffer *buffer);
140 static void gtk_text_buffer_free_target_lists (GtkTextBuffer *buffer);
142 static GObjectClass *parent_class = NULL;
143 static guint signals[LAST_SIGNAL] = { 0 };
145 static void gtk_text_buffer_set_property (GObject *object,
149 static void gtk_text_buffer_get_property (GObject *object,
153 static void gtk_text_buffer_notify (GObject *object,
158 gtk_text_buffer_get_type (void)
160 static GType our_type = 0;
164 static const GTypeInfo our_info =
166 sizeof (GtkTextBufferClass),
167 (GBaseInitFunc) NULL,
168 (GBaseFinalizeFunc) NULL,
169 (GClassInitFunc) gtk_text_buffer_class_init,
170 NULL, /* class_finalize */
171 NULL, /* class_data */
172 sizeof (GtkTextBuffer),
174 (GInstanceInitFunc) gtk_text_buffer_init
177 our_type = g_type_register_static (G_TYPE_OBJECT, I_("GtkTextBuffer"),
185 gtk_text_buffer_class_init (GtkTextBufferClass *klass)
187 GObjectClass *object_class = G_OBJECT_CLASS (klass);
189 parent_class = g_type_class_peek_parent (klass);
191 object_class->finalize = gtk_text_buffer_finalize;
192 object_class->set_property = gtk_text_buffer_set_property;
193 object_class->get_property = gtk_text_buffer_get_property;
194 object_class->notify = gtk_text_buffer_notify;
196 klass->insert_text = gtk_text_buffer_real_insert_text;
197 klass->insert_pixbuf = gtk_text_buffer_real_insert_pixbuf;
198 klass->insert_child_anchor = gtk_text_buffer_real_insert_anchor;
199 klass->delete_range = gtk_text_buffer_real_delete_range;
200 klass->apply_tag = gtk_text_buffer_real_apply_tag;
201 klass->remove_tag = gtk_text_buffer_real_remove_tag;
202 klass->changed = gtk_text_buffer_real_changed;
203 klass->mark_set = gtk_text_buffer_real_mark_set;
206 g_object_class_install_property (object_class,
208 g_param_spec_object ("tag-table",
210 P_("Text Tag Table"),
211 GTK_TYPE_TEXT_TAG_TABLE,
212 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
214 /* Normal properties*/
217 * GtkTextBuffer:text:
219 * The text content of the buffer. Without child widgets and images,
220 * see gtk_text_buffer_get_text () for more information.
224 g_object_class_install_property (object_class,
226 g_param_spec_string ("text",
228 P_("Current text of the buffer"),
230 GTK_PARAM_READWRITE));
233 * GtkTextBuffer:has-selection:
235 * Whether the buffer has some text currently selected.
239 g_object_class_install_property (object_class,
241 g_param_spec_boolean ("has-selection",
243 P_("Whether the buffer has some text currently selected"),
245 GTK_PARAM_READABLE));
248 * GtkTextBuffer:copy-target-list:
250 * The list of targets this buffer supports for clipboard copying
255 g_object_class_install_property (object_class,
256 PROP_COPY_TARGET_LIST,
257 g_param_spec_boxed ("copy-target-list",
258 P_("Copy target list"),
259 P_("The list of targets this buffer supports for clipboard copying and DND source"),
260 GTK_TYPE_TARGET_LIST,
261 GTK_PARAM_READABLE));
264 * GtkTextBuffer:paste-target-list:
266 * The list of targets this buffer supports for clipboard pasting
267 * and as DND destination.
271 g_object_class_install_property (object_class,
272 PROP_PASTE_TARGET_LIST,
273 g_param_spec_boxed ("paste-target-list",
274 P_("Paste target list"),
275 P_("The list of targets this buffer supports for clipboard pasting and DND destination"),
276 GTK_TYPE_TARGET_LIST,
277 GTK_PARAM_READABLE));
279 signals[INSERT_TEXT] =
280 g_signal_new (I_("insert_text"),
281 G_OBJECT_CLASS_TYPE (object_class),
283 G_STRUCT_OFFSET (GtkTextBufferClass, insert_text),
285 _gtk_marshal_VOID__BOXED_STRING_INT,
288 GTK_TYPE_TEXT_ITER | G_SIGNAL_TYPE_STATIC_SCOPE,
289 G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE,
292 signals[INSERT_PIXBUF] =
293 g_signal_new (I_("insert_pixbuf"),
294 G_OBJECT_CLASS_TYPE (object_class),
296 G_STRUCT_OFFSET (GtkTextBufferClass, insert_pixbuf),
298 _gtk_marshal_VOID__BOXED_OBJECT,
301 GTK_TYPE_TEXT_ITER | G_SIGNAL_TYPE_STATIC_SCOPE,
304 signals[INSERT_CHILD_ANCHOR] =
305 g_signal_new (I_("insert_child_anchor"),
306 G_OBJECT_CLASS_TYPE (object_class),
308 G_STRUCT_OFFSET (GtkTextBufferClass, insert_child_anchor),
310 _gtk_marshal_VOID__BOXED_OBJECT,
313 GTK_TYPE_TEXT_ITER | G_SIGNAL_TYPE_STATIC_SCOPE,
314 GTK_TYPE_TEXT_CHILD_ANCHOR);
317 * GtkTextBuffer::delete_range:
318 * @buffer: the object which received the signal.
319 * @start: the start of the range to be deleted
320 * @end: the end of the range to be deleted
322 * The ::delete_range signal is emitted to delete a range from
323 * a #GtkTextBuffer. Note that your handler must not invalidate the
324 * @start and @end iters (or has to revalidate them), if it runs before the
325 * default handler. There is no need to keep the iters valid in handlers
326 * which run after the default handler (see g_signal_connect_after()), but
327 * those don't have access to the deleted text.
329 signals[DELETE_RANGE] =
330 g_signal_new (I_("delete_range"),
331 G_OBJECT_CLASS_TYPE (object_class),
333 G_STRUCT_OFFSET (GtkTextBufferClass, delete_range),
335 _gtk_marshal_VOID__BOXED_BOXED,
338 GTK_TYPE_TEXT_ITER | G_SIGNAL_TYPE_STATIC_SCOPE,
339 GTK_TYPE_TEXT_ITER | G_SIGNAL_TYPE_STATIC_SCOPE);
342 g_signal_new (I_("changed"),
343 G_OBJECT_CLASS_TYPE (object_class),
345 G_STRUCT_OFFSET (GtkTextBufferClass, changed),
347 _gtk_marshal_VOID__VOID,
351 signals[MODIFIED_CHANGED] =
352 g_signal_new (I_("modified_changed"),
353 G_OBJECT_CLASS_TYPE (object_class),
355 G_STRUCT_OFFSET (GtkTextBufferClass, modified_changed),
357 _gtk_marshal_VOID__VOID,
362 g_signal_new (I_("mark_set"),
363 G_OBJECT_CLASS_TYPE (object_class),
365 G_STRUCT_OFFSET (GtkTextBufferClass, mark_set),
367 _gtk_marshal_VOID__BOXED_OBJECT,
373 signals[MARK_DELETED] =
374 g_signal_new (I_("mark_deleted"),
375 G_OBJECT_CLASS_TYPE (object_class),
377 G_STRUCT_OFFSET (GtkTextBufferClass, mark_deleted),
379 _gtk_marshal_VOID__OBJECT,
385 g_signal_new (I_("apply_tag"),
386 G_OBJECT_CLASS_TYPE (object_class),
388 G_STRUCT_OFFSET (GtkTextBufferClass, apply_tag),
390 _gtk_marshal_VOID__OBJECT_BOXED_BOXED,
397 signals[REMOVE_TAG] =
398 g_signal_new (I_("remove_tag"),
399 G_OBJECT_CLASS_TYPE (object_class),
401 G_STRUCT_OFFSET (GtkTextBufferClass, remove_tag),
403 _gtk_marshal_VOID__OBJECT_BOXED_BOXED,
410 signals[BEGIN_USER_ACTION] =
411 g_signal_new (I_("begin_user_action"),
412 G_OBJECT_CLASS_TYPE (object_class),
414 G_STRUCT_OFFSET (GtkTextBufferClass, begin_user_action),
416 _gtk_marshal_VOID__VOID,
420 signals[END_USER_ACTION] =
421 g_signal_new (I_("end_user_action"),
422 G_OBJECT_CLASS_TYPE (object_class),
424 G_STRUCT_OFFSET (GtkTextBufferClass, end_user_action),
426 _gtk_marshal_VOID__VOID,
430 g_type_class_add_private (object_class, sizeof (GtkTextBufferPrivate));
434 gtk_text_buffer_init (GtkTextBuffer *buffer)
436 buffer->clipboard_contents_buffers = NULL;
437 buffer->tag_table = NULL;
439 /* allow copying of arbiatray stuff in the internal rich text format */
440 gtk_text_buffer_register_serialize_tagset (buffer, NULL);
444 set_table (GtkTextBuffer *buffer, GtkTextTagTable *table)
446 g_return_if_fail (buffer->tag_table == NULL);
450 buffer->tag_table = table;
451 g_object_ref (buffer->tag_table);
452 _gtk_text_tag_table_add_buffer (table, buffer);
456 static GtkTextTagTable*
457 get_table (GtkTextBuffer *buffer)
459 if (buffer->tag_table == NULL)
461 buffer->tag_table = gtk_text_tag_table_new ();
462 _gtk_text_tag_table_add_buffer (buffer->tag_table, buffer);
465 return buffer->tag_table;
469 gtk_text_buffer_set_property (GObject *object,
474 GtkTextBuffer *text_buffer;
476 text_buffer = GTK_TEXT_BUFFER (object);
481 set_table (text_buffer, g_value_get_object (value));
485 gtk_text_buffer_set_text (text_buffer,
486 g_value_get_string (value), -1);
495 gtk_text_buffer_get_property (GObject *object,
500 GtkTextBuffer *text_buffer;
502 text_buffer = GTK_TEXT_BUFFER (object);
507 g_value_set_object (value, get_table (text_buffer));
512 GtkTextIter start, end;
514 gtk_text_buffer_get_start_iter (text_buffer, &start);
515 gtk_text_buffer_get_end_iter (text_buffer, &end);
517 g_value_take_string (value,
518 gtk_text_buffer_get_text (text_buffer,
519 &start, &end, FALSE));
523 case PROP_HAS_SELECTION:
524 g_value_set_boolean (value, text_buffer->has_selection);
527 case PROP_COPY_TARGET_LIST:
528 g_value_set_boxed (value, gtk_text_buffer_get_copy_target_list (text_buffer));
531 case PROP_PASTE_TARGET_LIST:
532 g_value_set_boxed (value, gtk_text_buffer_get_paste_target_list (text_buffer));
541 gtk_text_buffer_notify (GObject *object,
544 if (!strcmp (pspec->name, "copy-target-list") ||
545 !strcmp (pspec->name, "paste-target-list"))
547 gtk_text_buffer_free_target_lists (GTK_TEXT_BUFFER (object));
552 * gtk_text_buffer_new:
553 * @table: a tag table, or NULL to create a new one
555 * Creates a new text buffer.
557 * Return value: a new text buffer
560 gtk_text_buffer_new (GtkTextTagTable *table)
562 GtkTextBuffer *text_buffer;
564 text_buffer = g_object_new (GTK_TYPE_TEXT_BUFFER, "tag-table", table, NULL);
570 gtk_text_buffer_finalize (GObject *object)
572 GtkTextBuffer *buffer;
573 GtkTextBufferPrivate *priv;
575 buffer = GTK_TEXT_BUFFER (object);
577 remove_all_selection_clipboards (buffer);
579 priv = GTK_TEXT_BUFFER_GET_PRIVATE (buffer);
581 if (buffer->tag_table)
583 _gtk_text_tag_table_remove_buffer (buffer->tag_table, buffer);
584 g_object_unref (buffer->tag_table);
585 buffer->tag_table = NULL;
590 _gtk_text_btree_unref (buffer->btree);
591 buffer->btree = NULL;
594 if (buffer->log_attr_cache)
595 free_log_attr_cache (buffer->log_attr_cache);
597 buffer->log_attr_cache = NULL;
599 gtk_text_buffer_free_target_lists (buffer);
601 G_OBJECT_CLASS (parent_class)->finalize (object);
605 get_btree (GtkTextBuffer *buffer)
607 if (buffer->btree == NULL)
608 buffer->btree = _gtk_text_btree_new (gtk_text_buffer_get_tag_table (buffer),
611 return buffer->btree;
615 _gtk_text_buffer_get_btree (GtkTextBuffer *buffer)
617 return get_btree (buffer);
621 * gtk_text_buffer_get_tag_table:
622 * @buffer: a #GtkTextBuffer
624 * Get the #GtkTextTagTable associated with this buffer.
626 * Return value: the buffer's tag table
629 gtk_text_buffer_get_tag_table (GtkTextBuffer *buffer)
631 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
633 return get_table (buffer);
637 * gtk_text_buffer_set_text:
638 * @buffer: a #GtkTextBuffer
639 * @text: UTF-8 text to insert
640 * @len: length of @text in bytes
642 * Deletes current contents of @buffer, and inserts @text instead. If
643 * @len is -1, @text must be nul-terminated. @text must be valid UTF-8.
646 gtk_text_buffer_set_text (GtkTextBuffer *buffer,
650 GtkTextIter start, end;
652 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
653 g_return_if_fail (text != NULL);
658 gtk_text_buffer_get_bounds (buffer, &start, &end);
660 gtk_text_buffer_delete (buffer, &start, &end);
664 gtk_text_buffer_get_iter_at_offset (buffer, &start, 0);
665 gtk_text_buffer_insert (buffer, &start, text, len);
668 g_object_notify (G_OBJECT (buffer), "text");
678 gtk_text_buffer_real_insert_text (GtkTextBuffer *buffer,
683 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
684 g_return_if_fail (iter != NULL);
686 _gtk_text_btree_insert (iter, text, len);
688 g_signal_emit (buffer, signals[CHANGED], 0);
692 gtk_text_buffer_emit_insert (GtkTextBuffer *buffer,
697 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
698 g_return_if_fail (iter != NULL);
699 g_return_if_fail (text != NULL);
704 g_return_if_fail (g_utf8_validate (text, len, NULL));
708 g_signal_emit (buffer, signals[INSERT_TEXT], 0,
714 * gtk_text_buffer_insert:
715 * @buffer: a #GtkTextBuffer
716 * @iter: a position in the buffer
717 * @text: UTF-8 format text to insert
718 * @len: length of text in bytes, or -1
720 * Inserts @len bytes of @text at position @iter. If @len is -1,
721 * @text must be nul-terminated and will be inserted in its
722 * entirety. Emits the "insert_text" signal; insertion actually occurs
723 * in the default handler for the signal. @iter is invalidated when
724 * insertion occurs (because the buffer contents change), but the
725 * default signal handler revalidates it to point to the end of the
730 gtk_text_buffer_insert (GtkTextBuffer *buffer,
735 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
736 g_return_if_fail (iter != NULL);
737 g_return_if_fail (text != NULL);
738 g_return_if_fail (gtk_text_iter_get_buffer (iter) == buffer);
740 gtk_text_buffer_emit_insert (buffer, iter, text, len);
744 * gtk_text_buffer_insert_at_cursor:
745 * @buffer: a #GtkTextBuffer
746 * @text: some text in UTF-8 format
747 * @len: length of text, in bytes
749 * Simply calls gtk_text_buffer_insert (), using the current
750 * cursor position as the insertion point.
753 gtk_text_buffer_insert_at_cursor (GtkTextBuffer *buffer,
759 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
760 g_return_if_fail (text != NULL);
762 gtk_text_buffer_get_iter_at_mark (buffer, &iter,
763 gtk_text_buffer_get_mark (buffer,
766 gtk_text_buffer_insert (buffer, &iter, text, len);
770 * gtk_text_buffer_insert_interactive:
771 * @buffer: a #GtkTextBuffer
772 * @iter: a position in @buffer
773 * @text: some UTF-8 text
774 * @len: length of text in bytes, or -1
775 * @default_editable: default editability of buffer
777 * Like gtk_text_buffer_insert(), but the insertion will not occur if
778 * @iter is at a non-editable location in the buffer. Usually you
779 * want to prevent insertions at ineditable locations if the insertion
780 * results from a user action (is interactive).
782 * @default_editable indicates the editability of text that doesn't
783 * have a tag affecting editability applied to it. Typically the
784 * result of gtk_text_view_get_editable() is appropriate here.
786 * Return value: whether text was actually inserted
789 gtk_text_buffer_insert_interactive (GtkTextBuffer *buffer,
793 gboolean default_editable)
795 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
796 g_return_val_if_fail (text != NULL, FALSE);
797 g_return_val_if_fail (gtk_text_iter_get_buffer (iter) == buffer, FALSE);
799 if (gtk_text_iter_can_insert (iter, default_editable))
801 gtk_text_buffer_begin_user_action (buffer);
802 gtk_text_buffer_emit_insert (buffer, iter, text, len);
803 gtk_text_buffer_end_user_action (buffer);
811 * gtk_text_buffer_insert_interactive_at_cursor:
812 * @buffer: a #GtkTextBuffer
813 * @text: text in UTF-8 format
814 * @len: length of text in bytes, or -1
815 * @default_editable: default editability of buffer
817 * Calls gtk_text_buffer_insert_interactive () at the cursor
820 * @default_editable indicates the editability of text that doesn't
821 * have a tag affecting editability applied to it. Typically the
822 * result of gtk_text_view_get_editable() is appropriate here.
824 * Return value: whether text was actually inserted
827 gtk_text_buffer_insert_interactive_at_cursor (GtkTextBuffer *buffer,
830 gboolean default_editable)
834 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
835 g_return_val_if_fail (text != NULL, FALSE);
837 gtk_text_buffer_get_iter_at_mark (buffer, &iter,
838 gtk_text_buffer_get_mark (buffer,
841 return gtk_text_buffer_insert_interactive (buffer, &iter, text, len,
846 possibly_not_text (gunichar ch,
849 return ch == GTK_TEXT_UNKNOWN_CHAR;
853 insert_text_range (GtkTextBuffer *buffer,
855 const GtkTextIter *orig_start,
856 const GtkTextIter *orig_end,
857 gboolean interactive)
861 text = gtk_text_iter_get_text (orig_start, orig_end);
863 gtk_text_buffer_emit_insert (buffer, iter, text, -1);
868 typedef struct _Range Range;
871 GtkTextBuffer *buffer;
872 GtkTextMark *start_mark;
873 GtkTextMark *end_mark;
874 GtkTextMark *whole_end_mark;
875 GtkTextIter *range_start;
876 GtkTextIter *range_end;
877 GtkTextIter *whole_end;
881 save_range (GtkTextIter *range_start,
882 GtkTextIter *range_end,
883 GtkTextIter *whole_end)
887 r = g_new (Range, 1);
889 r->buffer = gtk_text_iter_get_buffer (range_start);
890 g_object_ref (r->buffer);
893 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),
904 gtk_text_buffer_create_mark (gtk_text_iter_get_buffer (range_start),
909 r->range_start = range_start;
910 r->range_end = range_end;
911 r->whole_end = whole_end;
917 restore_range (Range *r)
919 gtk_text_buffer_get_iter_at_mark (r->buffer,
923 gtk_text_buffer_get_iter_at_mark (r->buffer,
927 gtk_text_buffer_get_iter_at_mark (r->buffer,
931 gtk_text_buffer_delete_mark (r->buffer, r->start_mark);
932 gtk_text_buffer_delete_mark (r->buffer, r->end_mark);
933 gtk_text_buffer_delete_mark (r->buffer, r->whole_end_mark);
935 /* Due to the gravities on the marks, the ordering could have
936 * gotten mangled; we switch to an empty range in that
940 if (gtk_text_iter_compare (r->range_start, r->range_end) > 0)
941 *r->range_start = *r->range_end;
943 if (gtk_text_iter_compare (r->range_end, r->whole_end) > 0)
944 *r->range_end = *r->whole_end;
946 g_object_unref (r->buffer);
951 insert_range_untagged (GtkTextBuffer *buffer,
953 const GtkTextIter *orig_start,
954 const GtkTextIter *orig_end,
955 gboolean interactive)
957 GtkTextIter range_start;
958 GtkTextIter range_end;
959 GtkTextIter start, end;
962 if (gtk_text_iter_equal (orig_start, orig_end))
973 if (gtk_text_iter_equal (&range_start, &range_end))
975 /* Figure out how to move forward */
977 g_assert (gtk_text_iter_compare (&range_end, &end) <= 0);
979 if (gtk_text_iter_equal (&range_end, &end))
981 /* nothing left to do */
984 else if (gtk_text_iter_get_char (&range_end) == GTK_TEXT_UNKNOWN_CHAR)
986 GdkPixbuf *pixbuf = NULL;
987 GtkTextChildAnchor *anchor = NULL;
988 pixbuf = gtk_text_iter_get_pixbuf (&range_end);
989 anchor = gtk_text_iter_get_child_anchor (&range_end);
993 r = save_range (&range_start,
997 gtk_text_buffer_insert_pixbuf (buffer,
1004 gtk_text_iter_forward_char (&range_end);
1006 range_start = range_end;
1010 /* Just skip anchors */
1012 gtk_text_iter_forward_char (&range_end);
1013 range_start = range_end;
1017 /* The GTK_TEXT_UNKNOWN_CHAR was in a text segment, so
1020 gtk_text_iter_forward_find_char (&range_end,
1021 possibly_not_text, NULL,
1024 g_assert (gtk_text_iter_compare (&range_end, &end) <= 0);
1029 /* Text segment starts here, so forward search to
1030 * find its possible endpoint
1032 gtk_text_iter_forward_find_char (&range_end,
1033 possibly_not_text, NULL,
1036 g_assert (gtk_text_iter_compare (&range_end, &end) <= 0);
1041 r = save_range (&range_start,
1045 insert_text_range (buffer,
1054 range_start = range_end;
1060 insert_range_not_inside_self (GtkTextBuffer *buffer,
1062 const GtkTextIter *orig_start,
1063 const GtkTextIter *orig_end,
1064 gboolean interactive)
1066 /* Find each range of uniformly-tagged text, insert it,
1067 * then apply the tags.
1069 GtkTextIter start = *orig_start;
1070 GtkTextIter end = *orig_end;
1071 GtkTextIter range_start;
1072 GtkTextIter range_end;
1074 if (gtk_text_iter_equal (orig_start, orig_end))
1077 gtk_text_iter_order (&start, &end);
1079 range_start = start;
1085 GtkTextIter start_iter;
1090 if (gtk_text_iter_equal (&range_start, &end))
1091 break; /* All done */
1093 g_assert (gtk_text_iter_compare (&range_start, &end) < 0);
1095 gtk_text_iter_forward_to_tag_toggle (&range_end, NULL);
1097 g_assert (!gtk_text_iter_equal (&range_start, &range_end));
1099 /* Clamp to the end iterator */
1100 if (gtk_text_iter_compare (&range_end, &end) > 0)
1103 /* We have a range with unique tags; insert it, and
1106 start_offset = gtk_text_iter_get_offset (iter);
1108 r = save_range (&range_start, &range_end, &end);
1110 insert_range_untagged (buffer, iter, &range_start, &range_end, interactive);
1115 gtk_text_buffer_get_iter_at_offset (buffer, &start_iter, start_offset);
1117 tags = gtk_text_iter_get_tags (&range_start);
1119 while (tmp_list != NULL)
1121 gtk_text_buffer_apply_tag (buffer,
1126 tmp_list = g_slist_next (tmp_list);
1128 g_slist_free (tags);
1130 range_start = range_end;
1135 gtk_text_buffer_real_insert_range (GtkTextBuffer *buffer,
1137 const GtkTextIter *orig_start,
1138 const GtkTextIter *orig_end,
1139 gboolean interactive)
1141 GtkTextBuffer *src_buffer;
1143 /* Find each range of uniformly-tagged text, insert it,
1144 * then apply the tags.
1146 if (gtk_text_iter_equal (orig_start, orig_end))
1150 gtk_text_buffer_begin_user_action (buffer);
1152 src_buffer = gtk_text_iter_get_buffer (orig_start);
1154 if (gtk_text_iter_get_buffer (iter) != src_buffer ||
1155 !gtk_text_iter_in_range (iter, orig_start, orig_end))
1157 insert_range_not_inside_self (buffer, iter, orig_start, orig_end, interactive);
1161 /* If you insert a range into itself, it could loop infinitely
1162 * because the region being copied keeps growing as we insert. So
1163 * we have to separately copy the range before and after
1164 * the insertion point.
1166 GtkTextIter start = *orig_start;
1167 GtkTextIter end = *orig_end;
1168 GtkTextIter range_start;
1169 GtkTextIter range_end;
1173 gtk_text_iter_order (&start, &end);
1175 range_start = start;
1177 first_half = save_range (&range_start, &range_end, &end);
1179 range_start = *iter;
1181 second_half = save_range (&range_start, &range_end, &end);
1183 restore_range (first_half);
1184 insert_range_not_inside_self (buffer, iter, &range_start, &range_end, interactive);
1186 restore_range (second_half);
1187 insert_range_not_inside_self (buffer, iter, &range_start, &range_end, interactive);
1191 gtk_text_buffer_end_user_action (buffer);
1195 * gtk_text_buffer_insert_range:
1196 * @buffer: a #GtkTextBuffer
1197 * @iter: a position in @buffer
1198 * @start: a position in a #GtkTextBuffer
1199 * @end: another position in the same buffer as @start
1201 * Copies text, tags, and pixbufs between @start and @end (the order
1202 * of @start and @end doesn't matter) and inserts the copy at @iter.
1203 * Used instead of simply getting/inserting text because it preserves
1204 * images and tags. If @start and @end are in a different buffer from
1205 * @buffer, the two buffers must share the same tag table.
1207 * Implemented via emissions of the insert_text and apply_tag signals,
1211 gtk_text_buffer_insert_range (GtkTextBuffer *buffer,
1213 const GtkTextIter *start,
1214 const GtkTextIter *end)
1216 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1217 g_return_if_fail (iter != NULL);
1218 g_return_if_fail (start != NULL);
1219 g_return_if_fail (end != NULL);
1220 g_return_if_fail (gtk_text_iter_get_buffer (start) ==
1221 gtk_text_iter_get_buffer (end));
1222 g_return_if_fail (gtk_text_iter_get_buffer (start)->tag_table ==
1224 g_return_if_fail (gtk_text_iter_get_buffer (iter) == buffer);
1226 gtk_text_buffer_real_insert_range (buffer, iter, start, end, FALSE);
1230 * gtk_text_buffer_insert_range_interactive:
1231 * @buffer: a #GtkTextBuffer
1232 * @iter: a position in @buffer
1233 * @start: a position in a #GtkTextBuffer
1234 * @end: another position in the same buffer as @start
1235 * @default_editable: default editability of the buffer
1237 * Same as gtk_text_buffer_insert_range(), but does nothing if the
1238 * insertion point isn't editable. The @default_editable parameter
1239 * indicates whether the text is editable at @iter if no tags
1240 * enclosing @iter affect editability. Typically the result of
1241 * gtk_text_view_get_editable() is appropriate here.
1243 * Returns: whether an insertion was possible at @iter
1246 gtk_text_buffer_insert_range_interactive (GtkTextBuffer *buffer,
1248 const GtkTextIter *start,
1249 const GtkTextIter *end,
1250 gboolean default_editable)
1252 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
1253 g_return_val_if_fail (iter != NULL, FALSE);
1254 g_return_val_if_fail (start != NULL, FALSE);
1255 g_return_val_if_fail (end != NULL, FALSE);
1256 g_return_val_if_fail (gtk_text_iter_get_buffer (start) ==
1257 gtk_text_iter_get_buffer (end), FALSE);
1258 g_return_val_if_fail (gtk_text_iter_get_buffer (start)->tag_table ==
1259 buffer->tag_table, FALSE);
1262 if (gtk_text_iter_can_insert (iter, default_editable))
1264 gtk_text_buffer_real_insert_range (buffer, iter, start, end, TRUE);
1272 * gtk_text_buffer_insert_with_tags:
1273 * @buffer: a #GtkTextBuffer
1274 * @iter: an iterator in @buffer
1276 * @len: length of @text, or -1
1277 * @first_tag: first tag to apply to @text
1278 * @Varargs: NULL-terminated list of tags to apply
1280 * Inserts @text into @buffer at @iter, applying the list of tags to
1281 * the newly-inserted text. The last tag specified must be NULL to
1282 * terminate the list. Equivalent to calling gtk_text_buffer_insert (),
1283 * then gtk_text_buffer_apply_tag () on the inserted text;
1284 * gtk_text_buffer_insert_with_tags () is just a convenience function.
1287 gtk_text_buffer_insert_with_tags (GtkTextBuffer *buffer,
1291 GtkTextTag *first_tag,
1299 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1300 g_return_if_fail (iter != NULL);
1301 g_return_if_fail (text != NULL);
1302 g_return_if_fail (gtk_text_iter_get_buffer (iter) == buffer);
1304 start_offset = gtk_text_iter_get_offset (iter);
1306 gtk_text_buffer_insert (buffer, iter, text, len);
1308 if (first_tag == NULL)
1311 gtk_text_buffer_get_iter_at_offset (buffer, &start, start_offset);
1313 va_start (args, first_tag);
1317 gtk_text_buffer_apply_tag (buffer, tag, &start, iter);
1319 tag = va_arg (args, GtkTextTag*);
1326 * gtk_text_buffer_insert_with_tags_by_name:
1327 * @buffer: a #GtkTextBuffer
1328 * @iter: position in @buffer
1330 * @len: length of @text, or -1
1331 * @first_tag_name: name of a tag to apply to @text
1332 * @Varargs: more tag names
1334 * Same as gtk_text_buffer_insert_with_tags (), but allows you
1335 * to pass in tag names instead of tag objects.
1338 gtk_text_buffer_insert_with_tags_by_name (GtkTextBuffer *buffer,
1342 const gchar *first_tag_name,
1348 const gchar *tag_name;
1350 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1351 g_return_if_fail (iter != NULL);
1352 g_return_if_fail (text != NULL);
1353 g_return_if_fail (gtk_text_iter_get_buffer (iter) == buffer);
1355 start_offset = gtk_text_iter_get_offset (iter);
1357 gtk_text_buffer_insert (buffer, iter, text, len);
1359 if (first_tag_name == NULL)
1362 gtk_text_buffer_get_iter_at_offset (buffer, &start, start_offset);
1364 va_start (args, first_tag_name);
1365 tag_name = first_tag_name;
1370 tag = gtk_text_tag_table_lookup (buffer->tag_table,
1375 g_warning ("%s: no tag with name '%s'!", G_STRLOC, tag_name);
1379 gtk_text_buffer_apply_tag (buffer, tag, &start, iter);
1381 tag_name = va_arg (args, const gchar*);
1393 gtk_text_buffer_real_delete_range (GtkTextBuffer *buffer,
1397 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1398 g_return_if_fail (start != NULL);
1399 g_return_if_fail (end != NULL);
1401 _gtk_text_btree_delete (start, end);
1403 /* may have deleted the selection... */
1404 update_selection_clipboards (buffer);
1406 g_signal_emit (buffer, signals[CHANGED], 0);
1410 gtk_text_buffer_emit_delete (GtkTextBuffer *buffer,
1414 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1415 g_return_if_fail (start != NULL);
1416 g_return_if_fail (end != NULL);
1418 if (gtk_text_iter_equal (start, end))
1421 gtk_text_iter_order (start, end);
1423 g_signal_emit (buffer,
1424 signals[DELETE_RANGE],
1430 * gtk_text_buffer_delete:
1431 * @buffer: a #GtkTextBuffer
1432 * @start: a position in @buffer
1433 * @end: another position in @buffer
1435 * Deletes text between @start and @end. The order of @start and @end
1436 * is not actually relevant; gtk_text_buffer_delete () will reorder
1437 * them. This function actually emits the "delete_range" signal, and
1438 * the default handler of that signal deletes the text. Because the
1439 * buffer is modified, all outstanding iterators become invalid after
1440 * calling this function; however, the @start and @end will be
1441 * re-initialized to point to the location where text was deleted.
1445 gtk_text_buffer_delete (GtkTextBuffer *buffer,
1449 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1450 g_return_if_fail (start != NULL);
1451 g_return_if_fail (end != NULL);
1452 g_return_if_fail (gtk_text_iter_get_buffer (start) == buffer);
1453 g_return_if_fail (gtk_text_iter_get_buffer (end) == buffer);
1455 gtk_text_buffer_emit_delete (buffer, start, end);
1459 * gtk_text_buffer_delete_interactive:
1460 * @buffer: a #GtkTextBuffer
1461 * @start_iter: start of range to delete
1462 * @end_iter: end of range
1463 * @default_editable: whether the buffer is editable by default
1465 * Deletes all <emphasis>editable</emphasis> text in the given range.
1466 * Calls gtk_text_buffer_delete () for each editable sub-range of
1467 * [@start,@end). @start and @end are revalidated to point to
1468 * the location of the last deleted range, or left untouched if
1469 * no text was deleted.
1471 * Return value: whether some text was actually deleted
1474 gtk_text_buffer_delete_interactive (GtkTextBuffer *buffer,
1475 GtkTextIter *start_iter,
1476 GtkTextIter *end_iter,
1477 gboolean default_editable)
1479 GtkTextMark *end_mark;
1480 GtkTextMark *start_mark;
1482 gboolean current_state;
1483 gboolean deleted_stuff = FALSE;
1485 /* Delete all editable text in the range start_iter, end_iter */
1487 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
1488 g_return_val_if_fail (start_iter != NULL, FALSE);
1489 g_return_val_if_fail (end_iter != NULL, FALSE);
1490 g_return_val_if_fail (gtk_text_iter_get_buffer (start_iter) == buffer, FALSE);
1491 g_return_val_if_fail (gtk_text_iter_get_buffer (end_iter) == buffer, FALSE);
1494 gtk_text_buffer_begin_user_action (buffer);
1496 gtk_text_iter_order (start_iter, end_iter);
1498 start_mark = gtk_text_buffer_create_mark (buffer, NULL,
1500 end_mark = gtk_text_buffer_create_mark (buffer, NULL,
1503 gtk_text_buffer_get_iter_at_mark (buffer, &iter, start_mark);
1505 current_state = gtk_text_iter_editable (&iter, default_editable);
1510 gboolean done = FALSE;
1513 gtk_text_iter_forward_to_tag_toggle (&iter, NULL);
1515 gtk_text_buffer_get_iter_at_mark (buffer, &end, end_mark);
1517 if (gtk_text_iter_compare (&iter, &end) >= 0)
1520 iter = end; /* clamp to the last boundary */
1523 new_state = gtk_text_iter_editable (&iter, default_editable);
1525 if (current_state == new_state)
1531 /* We're ending an editable region. Delete said region. */
1534 gtk_text_buffer_get_iter_at_mark (buffer, &start, start_mark);
1536 gtk_text_buffer_emit_delete (buffer, &start, &iter);
1538 deleted_stuff = TRUE;
1540 /* revalidate user's iterators. */
1541 *start_iter = start;
1551 if (current_state && !new_state)
1553 /* End of an editable region. Delete it. */
1556 gtk_text_buffer_get_iter_at_mark (buffer, &start, start_mark);
1558 gtk_text_buffer_emit_delete (buffer, &start, &iter);
1560 current_state = FALSE;
1561 deleted_stuff = TRUE;
1563 /* revalidate user's iterators. */
1564 *start_iter = start;
1569 /* We are at the start of an editable region. We won't be deleting
1570 * the previous region. Move start mark to start of this region.
1573 g_assert (!current_state && new_state);
1575 gtk_text_buffer_move_mark (buffer, start_mark,
1579 current_state = TRUE;
1587 gtk_text_buffer_delete_mark (buffer, start_mark);
1588 gtk_text_buffer_delete_mark (buffer, end_mark);
1590 gtk_text_buffer_end_user_action (buffer);
1592 return deleted_stuff;
1596 * Extracting textual buffer contents
1600 * gtk_text_buffer_get_text:
1601 * @buffer: a #GtkTextBuffer
1602 * @start: start of a range
1603 * @end: end of a range
1604 * @include_hidden_chars: whether to include invisible text
1606 * Returns the text in the range [@start,@end). Excludes undisplayed
1607 * text (text marked with tags that set the invisibility attribute) if
1608 * @include_hidden_chars is %FALSE. Does not include characters
1609 * representing embedded images, so byte and character indexes into
1610 * the returned string do <emphasis>not</emphasis> correspond to byte
1611 * and character indexes into the buffer. Contrast with
1612 * gtk_text_buffer_get_slice ().
1614 * Return value: an allocated UTF-8 string
1617 gtk_text_buffer_get_text (GtkTextBuffer *buffer,
1618 const GtkTextIter *start,
1619 const GtkTextIter *end,
1620 gboolean include_hidden_chars)
1622 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
1623 g_return_val_if_fail (start != NULL, NULL);
1624 g_return_val_if_fail (end != NULL, NULL);
1625 g_return_val_if_fail (gtk_text_iter_get_buffer (start) == buffer, NULL);
1626 g_return_val_if_fail (gtk_text_iter_get_buffer (end) == buffer, NULL);
1628 if (include_hidden_chars)
1629 return gtk_text_iter_get_text (start, end);
1631 return gtk_text_iter_get_visible_text (start, end);
1635 * gtk_text_buffer_get_slice:
1636 * @buffer: a #GtkTextBuffer
1637 * @start: start of a range
1638 * @end: end of a range
1639 * @include_hidden_chars: whether to include invisible text
1641 * Returns the text in the range [@start,@end). Excludes undisplayed
1642 * text (text marked with tags that set the invisibility attribute) if
1643 * @include_hidden_chars is %FALSE. The returned string includes a
1644 * 0xFFFC character whenever the buffer contains
1645 * embedded images, so byte and character indexes into
1646 * the returned string <emphasis>do</emphasis> correspond to byte
1647 * and character indexes into the buffer. Contrast with
1648 * gtk_text_buffer_get_text (). Note that 0xFFFC can occur in normal
1649 * text as well, so it is not a reliable indicator that a pixbuf or
1650 * widget is in the buffer.
1652 * Return value: an allocated UTF-8 string
1655 gtk_text_buffer_get_slice (GtkTextBuffer *buffer,
1656 const GtkTextIter *start,
1657 const GtkTextIter *end,
1658 gboolean include_hidden_chars)
1660 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
1661 g_return_val_if_fail (start != NULL, NULL);
1662 g_return_val_if_fail (end != NULL, NULL);
1663 g_return_val_if_fail (gtk_text_iter_get_buffer (start) == buffer, NULL);
1664 g_return_val_if_fail (gtk_text_iter_get_buffer (end) == buffer, NULL);
1666 if (include_hidden_chars)
1667 return gtk_text_iter_get_slice (start, end);
1669 return gtk_text_iter_get_visible_slice (start, end);
1677 gtk_text_buffer_real_insert_pixbuf (GtkTextBuffer *buffer,
1681 _gtk_text_btree_insert_pixbuf (iter, pixbuf);
1683 g_signal_emit (buffer, signals[CHANGED], 0);
1687 * gtk_text_buffer_insert_pixbuf:
1688 * @buffer: a #GtkTextBuffer
1689 * @iter: location to insert the pixbuf
1690 * @pixbuf: a #GdkPixbuf
1692 * Inserts an image into the text buffer at @iter. The image will be
1693 * counted as one character in character counts, and when obtaining
1694 * the buffer contents as a string, will be represented by the Unicode
1695 * "object replacement character" 0xFFFC. Note that the "slice"
1696 * variants for obtaining portions of the buffer as a string include
1697 * this character for pixbufs, but the "text" variants do
1698 * not. e.g. see gtk_text_buffer_get_slice() and
1699 * gtk_text_buffer_get_text().
1703 gtk_text_buffer_insert_pixbuf (GtkTextBuffer *buffer,
1707 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1708 g_return_if_fail (iter != NULL);
1709 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
1710 g_return_if_fail (gtk_text_iter_get_buffer (iter) == buffer);
1712 g_signal_emit (buffer, signals[INSERT_PIXBUF], 0,
1722 gtk_text_buffer_real_insert_anchor (GtkTextBuffer *buffer,
1724 GtkTextChildAnchor *anchor)
1726 _gtk_text_btree_insert_child_anchor (iter, anchor);
1728 g_signal_emit (buffer, signals[CHANGED], 0);
1732 * gtk_text_buffer_insert_child_anchor:
1733 * @buffer: a #GtkTextBuffer
1734 * @iter: location to insert the anchor
1735 * @anchor: a #GtkTextChildAnchor
1737 * Inserts a child widget anchor into the text buffer at @iter. The
1738 * anchor will be counted as one character in character counts, and
1739 * when obtaining the buffer contents as a string, will be represented
1740 * by the Unicode "object replacement character" 0xFFFC. Note that the
1741 * "slice" variants for obtaining portions of the buffer as a string
1742 * include this character for child anchors, but the "text" variants do
1743 * not. e.g. see gtk_text_buffer_get_slice() and
1744 * gtk_text_buffer_get_text(). Consider
1745 * gtk_text_buffer_create_child_anchor() as a more convenient
1746 * alternative to this function. The buffer will add a reference to
1747 * the anchor, so you can unref it after insertion.
1751 gtk_text_buffer_insert_child_anchor (GtkTextBuffer *buffer,
1753 GtkTextChildAnchor *anchor)
1755 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1756 g_return_if_fail (iter != NULL);
1757 g_return_if_fail (GTK_IS_TEXT_CHILD_ANCHOR (anchor));
1758 g_return_if_fail (gtk_text_iter_get_buffer (iter) == buffer);
1760 g_signal_emit (buffer, signals[INSERT_CHILD_ANCHOR], 0,
1766 * gtk_text_buffer_create_child_anchor:
1767 * @buffer: a #GtkTextBuffer
1768 * @iter: location in the buffer
1770 * This is a convenience function which simply creates a child anchor
1771 * with gtk_text_child_anchor_new() and inserts it into the buffer
1772 * with gtk_text_buffer_insert_child_anchor(). The new anchor is
1773 * owned by the buffer; no reference count is returned to
1774 * the caller of gtk_text_buffer_create_child_anchor().
1776 * Return value: the created child anchor
1779 gtk_text_buffer_create_child_anchor (GtkTextBuffer *buffer,
1782 GtkTextChildAnchor *anchor;
1784 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
1785 g_return_val_if_fail (iter != NULL, NULL);
1786 g_return_val_if_fail (gtk_text_iter_get_buffer (iter) == buffer, NULL);
1788 anchor = gtk_text_child_anchor_new ();
1790 gtk_text_buffer_insert_child_anchor (buffer, iter, anchor);
1792 g_object_unref (anchor);
1802 gtk_text_buffer_mark_set (GtkTextBuffer *buffer,
1803 const GtkTextIter *location,
1806 /* IMO this should NOT work like insert_text and delete_range,
1807 * where the real action happens in the default handler.
1809 * The reason is that the default handler would be _required_,
1810 * i.e. the whole widget would start breaking and segfaulting if the
1811 * default handler didn't get run. So you can't really override the
1812 * default handler or stop the emission; that is, this signal is
1813 * purely for notification, and not to allow users to modify the
1817 g_object_ref (mark);
1819 g_signal_emit (buffer,
1825 g_object_unref (mark);
1829 * gtk_text_buffer_set_mark:
1830 * @buffer: a #GtkTextBuffer
1831 * @mark_name: name of the mark
1832 * @iter: location for the mark.
1833 * @left_gravity: if the mark is created by this function, gravity for the new
1835 * @should_exist: if %TRUE, warn if the mark does not exist, and return
1838 * Move the mark to the given position, if not @should_exist, create the mark.
1840 * Return value: mark
1843 gtk_text_buffer_set_mark (GtkTextBuffer *buffer,
1844 GtkTextMark *existing_mark,
1845 const gchar *mark_name,
1846 const GtkTextIter *iter,
1847 gboolean left_gravity,
1848 gboolean should_exist)
1850 GtkTextIter location;
1853 g_return_val_if_fail (gtk_text_iter_get_buffer (iter) == buffer, NULL);
1855 mark = _gtk_text_btree_set_mark (get_btree (buffer),
1862 if (_gtk_text_btree_mark_is_insert (get_btree (buffer), mark) ||
1863 _gtk_text_btree_mark_is_selection_bound (get_btree (buffer), mark))
1865 update_selection_clipboards (buffer);
1868 _gtk_text_btree_get_iter_at_mark (get_btree (buffer),
1872 gtk_text_buffer_mark_set (buffer, &location, mark);
1878 * gtk_text_buffer_create_mark:
1879 * @buffer: a #GtkTextBuffer
1880 * @mark_name: name for mark, or %NULL
1881 * @where: location to place mark
1882 * @left_gravity: whether the mark has left gravity
1884 * Creates a mark at position @where. If @mark_name is %NULL, the mark
1885 * is anonymous; otherwise, the mark can be retrieved by name using
1886 * gtk_text_buffer_get_mark (). If a mark has left gravity, and text is
1887 * inserted at the mark's current location, the mark will be moved to
1888 * the left of the newly-inserted text. If the mark has right gravity
1889 * (@left_gravity = %FALSE), the mark will end up on the right of
1890 * newly-inserted text. The standard left-to-right cursor is a mark
1891 * with right gravity (when you type, the cursor stays on the right
1892 * side of the text you're typing).
1894 * The caller of this function does <emphasis>not</emphasis> own a reference
1895 * to the returned #GtkTextMark, so you can ignore the return value
1896 * if you like. Marks are owned by the buffer and go away when the
1899 * Emits the "mark_set" signal as notification of the mark's initial
1902 * Return value: the new #GtkTextMark object
1905 gtk_text_buffer_create_mark (GtkTextBuffer *buffer,
1906 const gchar *mark_name,
1907 const GtkTextIter *where,
1908 gboolean left_gravity)
1910 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
1912 return gtk_text_buffer_set_mark (buffer, NULL, mark_name, where,
1913 left_gravity, FALSE);
1917 * gtk_text_buffer_move_mark:
1918 * @buffer: a #GtkTextBuffer
1919 * @mark: a #GtkTextMark
1920 * @where: new location for @mark in @buffer
1922 * Moves @mark to the new location @where. Emits the "mark_set" signal
1923 * as notification of the move.
1926 gtk_text_buffer_move_mark (GtkTextBuffer *buffer,
1928 const GtkTextIter *where)
1930 g_return_if_fail (GTK_IS_TEXT_MARK (mark));
1931 g_return_if_fail (!gtk_text_mark_get_deleted (mark));
1932 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1934 gtk_text_buffer_set_mark (buffer, mark, NULL, where, FALSE, TRUE);
1938 * gtk_text_buffer_get_iter_at_mark:
1939 * @buffer: a #GtkTextBuffer
1940 * @iter: iterator to initialize
1941 * @mark: a #GtkTextMark in @buffer
1943 * Initializes @iter with the current position of @mark.
1946 gtk_text_buffer_get_iter_at_mark (GtkTextBuffer *buffer,
1950 g_return_if_fail (GTK_IS_TEXT_MARK (mark));
1951 g_return_if_fail (!gtk_text_mark_get_deleted (mark));
1952 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1954 _gtk_text_btree_get_iter_at_mark (get_btree (buffer),
1960 * gtk_text_buffer_delete_mark:
1961 * @buffer: a #GtkTextBuffer
1962 * @mark: a #GtkTextMark in @buffer
1964 * Deletes @mark, so that it's no longer located anywhere in the
1965 * buffer. Removes the reference the buffer holds to the mark, so if
1966 * you haven't called g_object_ref () on the mark, it will be freed. Even
1967 * if the mark isn't freed, most operations on @mark become
1968 * invalid. There is no way to undelete a
1969 * mark. gtk_text_mark_get_deleted () will return TRUE after this
1970 * function has been called on a mark; gtk_text_mark_get_deleted ()
1971 * indicates that a mark no longer belongs to a buffer. The "mark_deleted"
1972 * signal will be emitted as notification after the mark is deleted.
1975 gtk_text_buffer_delete_mark (GtkTextBuffer *buffer,
1978 g_return_if_fail (GTK_IS_TEXT_MARK (mark));
1979 g_return_if_fail (!gtk_text_mark_get_deleted (mark));
1980 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1982 g_object_ref (mark);
1984 _gtk_text_btree_remove_mark (get_btree (buffer), mark);
1986 /* See rationale above for MARK_SET on why we emit this after
1987 * removing the mark, rather than removing the mark in a default
1990 g_signal_emit (buffer, signals[MARK_DELETED],
1994 g_object_unref (mark);
1998 * gtk_text_buffer_get_mark:
1999 * @buffer: a #GtkTextBuffer
2000 * @name: a mark name
2002 * Returns the mark named @name in buffer @buffer, or NULL if no such
2003 * mark exists in the buffer.
2005 * Return value: a #GtkTextMark, or NULL
2008 gtk_text_buffer_get_mark (GtkTextBuffer *buffer,
2013 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
2014 g_return_val_if_fail (name != NULL, NULL);
2016 mark = _gtk_text_btree_get_mark_by_name (get_btree (buffer), name);
2023 * gtk_text_buffer_move_mark_by_name:
2024 * @buffer: a #GtkTextBuffer
2025 * @name: name of a mark
2026 * @where: new location for mark
2028 * Moves the mark named @name (which must exist) to location @where.
2029 * See gtk_text_buffer_move_mark () for details.
2032 gtk_text_buffer_move_mark_by_name (GtkTextBuffer *buffer,
2034 const GtkTextIter *where)
2038 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2039 g_return_if_fail (name != NULL);
2041 mark = _gtk_text_btree_get_mark_by_name (get_btree (buffer), name);
2045 g_warning ("%s: no mark named '%s'", G_STRLOC, name);
2049 gtk_text_buffer_move_mark (buffer, mark, where);
2053 * gtk_text_buffer_delete_mark_by_name:
2054 * @buffer: a #GtkTextBuffer
2055 * @name: name of a mark in @buffer
2057 * Deletes the mark named @name; the mark must exist. See
2058 * gtk_text_buffer_delete_mark () for details.
2061 gtk_text_buffer_delete_mark_by_name (GtkTextBuffer *buffer,
2066 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2067 g_return_if_fail (name != NULL);
2069 mark = _gtk_text_btree_get_mark_by_name (get_btree (buffer), name);
2073 g_warning ("%s: no mark named '%s'", G_STRLOC, name);
2077 gtk_text_buffer_delete_mark (buffer, mark);
2081 * gtk_text_buffer_get_insert:
2082 * @buffer: a #GtkTextBuffer
2084 * Returns the mark that represents the cursor (insertion point).
2085 * Equivalent to calling gtk_text_buffer_get_mark () to get the mark
2086 * named "insert", but very slightly more efficient, and involves less
2089 * Return value: insertion point mark
2092 gtk_text_buffer_get_insert (GtkTextBuffer *buffer)
2094 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
2096 /* FIXME use struct member in btree */
2097 return gtk_text_buffer_get_mark (buffer, "insert");
2101 * gtk_text_buffer_get_selection_bound:
2102 * @buffer: a #GtkTextBuffer
2104 * Returns the mark that represents the selection bound. Equivalent
2105 * to calling gtk_text_buffer_get_mark () to get the mark named
2106 * "selection_bound", but very slightly more efficient, and involves
2109 * The currently-selected text in @buffer is the region between the
2110 * "selection_bound" and "insert" marks. If "selection_bound" and
2111 * "insert" are in the same place, then there is no current selection.
2112 * gtk_text_buffer_get_selection_bounds() is another convenient function
2113 * for handling the selection, if you just want to know whether there's a
2114 * selection and what its bounds are.
2116 * Return value: selection bound mark
2119 gtk_text_buffer_get_selection_bound (GtkTextBuffer *buffer)
2121 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
2123 /* FIXME use struct member in btree */
2124 return gtk_text_buffer_get_mark (buffer, "selection_bound");
2128 * gtk_text_buffer_get_iter_at_child_anchor:
2129 * @buffer: a #GtkTextBuffer
2130 * @iter: an iterator to be initialized
2131 * @anchor: a child anchor that appears in @buffer
2133 * Obtains the location of @anchor within @buffer.
2137 gtk_text_buffer_get_iter_at_child_anchor (GtkTextBuffer *buffer,
2139 GtkTextChildAnchor *anchor)
2141 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2142 g_return_if_fail (iter != NULL);
2143 g_return_if_fail (GTK_IS_TEXT_CHILD_ANCHOR (anchor));
2144 g_return_if_fail (!gtk_text_child_anchor_get_deleted (anchor));
2146 _gtk_text_btree_get_iter_at_child_anchor (get_btree (buffer),
2152 * gtk_text_buffer_place_cursor:
2153 * @buffer: a #GtkTextBuffer
2154 * @where: where to put the cursor
2156 * This function moves the "insert" and "selection_bound" marks
2157 * simultaneously. If you move them to the same place in two steps
2158 * with gtk_text_buffer_move_mark(), you will temporarily select a
2159 * region in between their old and new locations, which can be pretty
2160 * inefficient since the temporarily-selected region will force stuff
2161 * to be recalculated. This function moves them as a unit, which can
2165 gtk_text_buffer_place_cursor (GtkTextBuffer *buffer,
2166 const GtkTextIter *where)
2168 gtk_text_buffer_select_range (buffer, where, where);
2173 * gtk_text_buffer_select_range:
2174 * @buffer: a #GtkTextBuffer
2175 * @ins: where to put the "insert" mark
2176 * @bound: where to put the "selection_bound" mark
2178 * This function moves the "insert" and "selection_bound" marks
2179 * simultaneously. If you move them in two steps
2180 * with gtk_text_buffer_move_mark(), you will temporarily select a
2181 * region in between their old and new locations, which can be pretty
2182 * inefficient since the temporarily-selected region will force stuff
2183 * to be recalculated. This function moves them as a unit, which can
2189 gtk_text_buffer_select_range (GtkTextBuffer *buffer,
2190 const GtkTextIter *ins,
2191 const GtkTextIter *bound)
2193 GtkTextIter real_ins;
2194 GtkTextIter real_bound;
2196 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2199 real_bound = *bound;
2201 _gtk_text_btree_select_range (get_btree (buffer), &real_ins, &real_bound);
2202 gtk_text_buffer_mark_set (buffer, &real_ins,
2203 gtk_text_buffer_get_mark (buffer,
2205 gtk_text_buffer_mark_set (buffer, &real_bound,
2206 gtk_text_buffer_get_mark (buffer,
2207 "selection_bound"));
2208 update_selection_clipboards (buffer);
2216 * gtk_text_buffer_create_tag:
2217 * @buffer: a #GtkTextBuffer
2218 * @tag_name: name of the new tag, or %NULL
2219 * @first_property_name: name of first property to set, or %NULL
2220 * @Varargs: %NULL-terminated list of property names and values
2223 * Creates a tag and adds it to the tag table for @buffer.
2224 * Equivalent to calling gtk_text_tag_new () and then adding the
2225 * tag to the buffer's tag table. The returned tag is owned by
2226 * the buffer's tag table, so the ref count will be equal to one.
2228 * If @tag_name is %NULL, the tag is anonymous.
2230 * If @tag_name is non-%NULL, a tag called @tag_name must not already
2231 * exist in the tag table for this buffer.
2233 * The @first_property_name argument and subsequent arguments are a list
2234 * of properties to set on the tag, as with g_object_set().
2236 * Return value: a new tag
2239 gtk_text_buffer_create_tag (GtkTextBuffer *buffer,
2240 const gchar *tag_name,
2241 const gchar *first_property_name,
2247 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
2249 tag = gtk_text_tag_new (tag_name);
2251 gtk_text_tag_table_add (get_table (buffer), tag);
2253 if (first_property_name)
2255 va_start (list, first_property_name);
2256 g_object_set_valist (G_OBJECT (tag), first_property_name, list);
2260 g_object_unref (tag);
2266 gtk_text_buffer_real_apply_tag (GtkTextBuffer *buffer,
2268 const GtkTextIter *start,
2269 const GtkTextIter *end)
2271 if (tag->table != buffer->tag_table)
2273 g_warning ("Can only apply tags that are in the tag table for the buffer");
2277 _gtk_text_btree_tag (start, end, tag, TRUE);
2281 gtk_text_buffer_real_remove_tag (GtkTextBuffer *buffer,
2283 const GtkTextIter *start,
2284 const GtkTextIter *end)
2286 if (tag->table != buffer->tag_table)
2288 g_warning ("Can only remove tags that are in the tag table for the buffer");
2292 _gtk_text_btree_tag (start, end, tag, FALSE);
2296 gtk_text_buffer_real_changed (GtkTextBuffer *buffer)
2298 gtk_text_buffer_set_modified (buffer, TRUE);
2302 gtk_text_buffer_real_mark_set (GtkTextBuffer *buffer,
2303 const GtkTextIter *iter,
2306 if (mark == gtk_text_buffer_get_insert (buffer) ||
2307 mark == gtk_text_buffer_get_selection_bound (buffer))
2309 gboolean has_selection;
2311 has_selection = gtk_text_buffer_get_selection_bounds (buffer,
2315 if (has_selection != buffer->has_selection)
2317 buffer->has_selection = has_selection;
2318 g_object_notify (G_OBJECT (buffer), "has-selection");
2324 gtk_text_buffer_emit_tag (GtkTextBuffer *buffer,
2327 const GtkTextIter *start,
2328 const GtkTextIter *end)
2330 GtkTextIter start_tmp = *start;
2331 GtkTextIter end_tmp = *end;
2333 g_return_if_fail (tag != NULL);
2335 gtk_text_iter_order (&start_tmp, &end_tmp);
2338 g_signal_emit (buffer, signals[APPLY_TAG],
2340 tag, &start_tmp, &end_tmp);
2342 g_signal_emit (buffer, signals[REMOVE_TAG],
2344 tag, &start_tmp, &end_tmp);
2349 * gtk_text_buffer_apply_tag:
2350 * @buffer: a #GtkTextBuffer
2351 * @tag: a #GtkTextTag
2352 * @start: one bound of range to be tagged
2353 * @end: other bound of range to be tagged
2355 * Emits the "apply_tag" signal on @buffer. The default
2356 * handler for the signal applies @tag to the given range.
2357 * @start and @end do not have to be in order.
2361 gtk_text_buffer_apply_tag (GtkTextBuffer *buffer,
2363 const GtkTextIter *start,
2364 const GtkTextIter *end)
2366 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2367 g_return_if_fail (GTK_IS_TEXT_TAG (tag));
2368 g_return_if_fail (start != NULL);
2369 g_return_if_fail (end != NULL);
2370 g_return_if_fail (gtk_text_iter_get_buffer (start) == buffer);
2371 g_return_if_fail (gtk_text_iter_get_buffer (end) == buffer);
2372 g_return_if_fail (tag->table == buffer->tag_table);
2374 gtk_text_buffer_emit_tag (buffer, tag, TRUE, start, end);
2378 * gtk_text_buffer_remove_tag:
2379 * @buffer: a #GtkTextBuffer
2380 * @tag: a #GtkTextTag
2381 * @start: one bound of range to be untagged
2382 * @end: other bound of range to be untagged
2384 * Emits the "remove_tag" signal. The default handler for the signal
2385 * removes all occurrences of @tag from the given range. @start and
2386 * @end don't have to be in order.
2390 gtk_text_buffer_remove_tag (GtkTextBuffer *buffer,
2392 const GtkTextIter *start,
2393 const GtkTextIter *end)
2396 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2397 g_return_if_fail (GTK_IS_TEXT_TAG (tag));
2398 g_return_if_fail (start != NULL);
2399 g_return_if_fail (end != NULL);
2400 g_return_if_fail (gtk_text_iter_get_buffer (start) == buffer);
2401 g_return_if_fail (gtk_text_iter_get_buffer (end) == buffer);
2402 g_return_if_fail (tag->table == buffer->tag_table);
2404 gtk_text_buffer_emit_tag (buffer, tag, FALSE, start, end);
2409 * gtk_text_buffer_apply_tag_by_name:
2410 * @buffer: a #GtkTextBuffer
2411 * @name: name of a named #GtkTextTag
2412 * @start: one bound of range to be tagged
2413 * @end: other bound of range to be tagged
2415 * Calls gtk_text_tag_table_lookup() on the buffer's tag table to
2416 * get a #GtkTextTag, then calls gtk_text_buffer_apply_tag().
2420 gtk_text_buffer_apply_tag_by_name (GtkTextBuffer *buffer,
2422 const GtkTextIter *start,
2423 const GtkTextIter *end)
2427 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2428 g_return_if_fail (name != NULL);
2429 g_return_if_fail (start != NULL);
2430 g_return_if_fail (end != NULL);
2431 g_return_if_fail (gtk_text_iter_get_buffer (start) == buffer);
2432 g_return_if_fail (gtk_text_iter_get_buffer (end) == buffer);
2434 tag = gtk_text_tag_table_lookup (get_table (buffer),
2439 g_warning ("Unknown tag `%s'", name);
2443 gtk_text_buffer_emit_tag (buffer, tag, TRUE, start, end);
2447 * gtk_text_buffer_remove_tag_by_name:
2448 * @buffer: a #GtkTextBuffer
2449 * @name: name of a #GtkTextTag
2450 * @start: one bound of range to be untagged
2451 * @end: other bound of range to be untagged
2453 * Calls gtk_text_tag_table_lookup() on the buffer's tag table to
2454 * get a #GtkTextTag, then calls gtk_text_buffer_remove_tag().
2459 gtk_text_buffer_remove_tag_by_name (GtkTextBuffer *buffer,
2461 const GtkTextIter *start,
2462 const GtkTextIter *end)
2466 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2467 g_return_if_fail (name != NULL);
2468 g_return_if_fail (start != NULL);
2469 g_return_if_fail (end != NULL);
2470 g_return_if_fail (gtk_text_iter_get_buffer (start) == buffer);
2471 g_return_if_fail (gtk_text_iter_get_buffer (end) == buffer);
2473 tag = gtk_text_tag_table_lookup (get_table (buffer),
2478 g_warning ("Unknown tag `%s'", name);
2482 gtk_text_buffer_emit_tag (buffer, tag, FALSE, start, end);
2486 pointer_cmp (gconstpointer a,
2498 * gtk_text_buffer_remove_all_tags:
2499 * @buffer: a #GtkTextBuffer
2500 * @start: one bound of range to be untagged
2501 * @end: other bound of range to be untagged
2503 * Removes all tags in the range between @start and @end. Be careful
2504 * with this function; it could remove tags added in code unrelated to
2505 * the code you're currently writing. That is, using this function is
2506 * probably a bad idea if you have two or more unrelated code sections
2510 gtk_text_buffer_remove_all_tags (GtkTextBuffer *buffer,
2511 const GtkTextIter *start,
2512 const GtkTextIter *end)
2514 GtkTextIter first, second, tmp;
2517 GSList *prev, *next;
2520 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2521 g_return_if_fail (start != NULL);
2522 g_return_if_fail (end != NULL);
2523 g_return_if_fail (gtk_text_iter_get_buffer (start) == buffer);
2524 g_return_if_fail (gtk_text_iter_get_buffer (end) == buffer);
2529 gtk_text_iter_order (&first, &second);
2531 /* Get all tags turned on at the start */
2532 tags = gtk_text_iter_get_tags (&first);
2534 /* Find any that are toggled on within the range */
2536 while (gtk_text_iter_forward_to_tag_toggle (&tmp, NULL))
2541 if (gtk_text_iter_compare (&tmp, &second) >= 0)
2542 break; /* past the end of the range */
2544 toggled = gtk_text_iter_get_toggled_tags (&tmp, TRUE);
2546 /* We could end up with a really big-ass list here.
2549 tmp_list2 = toggled;
2550 while (tmp_list2 != NULL)
2552 tags = g_slist_prepend (tags, tmp_list2->data);
2554 tmp_list2 = g_slist_next (tmp_list2);
2557 g_slist_free (toggled);
2561 tags = g_slist_sort (tags, pointer_cmp);
2563 /* Strip duplicates */
2567 while (tmp_list != NULL)
2569 if (tag == tmp_list->data)
2572 next = tmp_list->next;
2576 tmp_list->next = NULL;
2578 g_slist_free (tmp_list);
2581 /* prev is unchanged */
2585 /* not a duplicate */
2586 tag = GTK_TEXT_TAG (tmp_list->data);
2588 tmp_list = tmp_list->next;
2592 g_slist_foreach (tags, (GFunc) g_object_ref, NULL);
2595 while (tmp_list != NULL)
2597 tag = GTK_TEXT_TAG (tmp_list->data);
2599 gtk_text_buffer_remove_tag (buffer, tag, &first, &second);
2601 tmp_list = tmp_list->next;
2604 g_slist_foreach (tags, (GFunc) g_object_unref, NULL);
2606 g_slist_free (tags);
2611 * Obtain various iterators
2615 * gtk_text_buffer_get_iter_at_line_offset:
2616 * @buffer: a #GtkTextBuffer
2617 * @iter: iterator to initialize
2618 * @line_number: line number counting from 0
2619 * @char_offset: char offset from start of line
2621 * Obtains an iterator pointing to @char_offset within the given
2622 * line. The @char_offset must exist, offsets off the end of the line
2623 * are not allowed. Note <emphasis>characters</emphasis>, not bytes;
2624 * UTF-8 may encode one character as multiple bytes.
2628 gtk_text_buffer_get_iter_at_line_offset (GtkTextBuffer *buffer,
2633 g_return_if_fail (iter != NULL);
2634 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2636 _gtk_text_btree_get_iter_at_line_char (get_btree (buffer),
2637 iter, line_number, char_offset);
2641 * gtk_text_buffer_get_iter_at_line_index:
2642 * @buffer: a #GtkTextBuffer
2643 * @iter: iterator to initialize
2644 * @line_number: line number counting from 0
2645 * @byte_index: byte index from start of line
2647 * Obtains an iterator pointing to @byte_index within the given line.
2648 * @byte_index must be the start of a UTF-8 character, and must not be
2649 * beyond the end of the line. Note <emphasis>bytes</emphasis>, not
2650 * characters; UTF-8 may encode one character as multiple bytes.
2654 gtk_text_buffer_get_iter_at_line_index (GtkTextBuffer *buffer,
2659 g_return_if_fail (iter != NULL);
2660 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2662 _gtk_text_btree_get_iter_at_line_byte (get_btree (buffer),
2663 iter, line_number, byte_index);
2667 * gtk_text_buffer_get_iter_at_line:
2668 * @buffer: a #GtkTextBuffer
2669 * @iter: iterator to initialize
2670 * @line_number: line number counting from 0
2672 * Initializes @iter to the start of the given line.
2675 gtk_text_buffer_get_iter_at_line (GtkTextBuffer *buffer,
2679 g_return_if_fail (iter != NULL);
2680 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2682 gtk_text_buffer_get_iter_at_line_offset (buffer, iter, line_number, 0);
2686 * gtk_text_buffer_get_iter_at_offset:
2687 * @buffer: a #GtkTextBuffer
2688 * @iter: iterator to initialize
2689 * @char_offset: char offset from start of buffer, counting from 0, or -1
2691 * Initializes @iter to a position @char_offset chars from the start
2692 * of the entire buffer. If @char_offset is -1 or greater than the number
2693 * of characters in the buffer, @iter is initialized to the end iterator,
2694 * the iterator one past the last valid character in the buffer.
2697 gtk_text_buffer_get_iter_at_offset (GtkTextBuffer *buffer,
2701 g_return_if_fail (iter != NULL);
2702 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2704 _gtk_text_btree_get_iter_at_char (get_btree (buffer), iter, char_offset);
2708 * gtk_text_buffer_get_start_iter:
2709 * @buffer: a #GtkTextBuffer
2710 * @iter: iterator to initialize
2712 * Initialized @iter with the first position in the text buffer. This
2713 * is the same as using gtk_text_buffer_get_iter_at_offset() to get
2714 * the iter at character offset 0.
2717 gtk_text_buffer_get_start_iter (GtkTextBuffer *buffer,
2720 g_return_if_fail (iter != NULL);
2721 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2723 _gtk_text_btree_get_iter_at_char (get_btree (buffer), iter, 0);
2727 * gtk_text_buffer_get_end_iter:
2728 * @buffer: a #GtkTextBuffer
2729 * @iter: iterator to initialize
2731 * Initializes @iter with the "end iterator," one past the last valid
2732 * character in the text buffer. If dereferenced with
2733 * gtk_text_iter_get_char(), the end iterator has a character value of
2734 * 0. The entire buffer lies in the range from the first position in
2735 * the buffer (call gtk_text_buffer_get_start_iter() to get
2736 * character position 0) to the end iterator.
2740 gtk_text_buffer_get_end_iter (GtkTextBuffer *buffer,
2743 g_return_if_fail (iter != NULL);
2744 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2746 _gtk_text_btree_get_end_iter (get_btree (buffer), iter);
2750 * gtk_text_buffer_get_bounds:
2751 * @buffer: a #GtkTextBuffer
2752 * @start: iterator to initialize with first position in the buffer
2753 * @end: iterator to initialize with the end iterator
2755 * Retrieves the first and last iterators in the buffer, i.e. the
2756 * entire buffer lies within the range [@start,@end).
2760 gtk_text_buffer_get_bounds (GtkTextBuffer *buffer,
2764 g_return_if_fail (start != NULL);
2765 g_return_if_fail (end != NULL);
2766 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2768 _gtk_text_btree_get_iter_at_char (get_btree (buffer), start, 0);
2769 _gtk_text_btree_get_end_iter (get_btree (buffer), end);
2777 * gtk_text_buffer_get_modified:
2778 * @buffer: a #GtkTextBuffer
2780 * Indicates whether the buffer has been modified since the last call
2781 * to gtk_text_buffer_set_modified() set the modification flag to
2782 * %FALSE. Used for example to enable a "save" function in a text
2785 * Return value: %TRUE if the buffer has been modified
2788 gtk_text_buffer_get_modified (GtkTextBuffer *buffer)
2790 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
2792 return buffer->modified;
2796 * gtk_text_buffer_set_modified:
2797 * @buffer: a #GtkTextBuffer
2798 * @setting: modification flag setting
2800 * Used to keep track of whether the buffer has been modified since the
2801 * last time it was saved. Whenever the buffer is saved to disk, call
2802 * gtk_text_buffer_set_modified (@buffer, FALSE). When the buffer is modified,
2803 * it will automatically toggled on the modified bit again. When the modified
2804 * bit flips, the buffer emits a "modified_changed" signal.
2808 gtk_text_buffer_set_modified (GtkTextBuffer *buffer,
2811 gboolean fixed_setting;
2813 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2815 fixed_setting = setting != FALSE;
2817 if (buffer->modified == fixed_setting)
2821 buffer->modified = fixed_setting;
2822 g_signal_emit (buffer, signals[MODIFIED_CHANGED], 0);
2827 * gtk_text_buffer_get_has_selection:
2828 * @buffer: a #GtkTextBuffer
2830 * Indicates whether the buffer has some text currently selected.
2832 * Return value: %TRUE if the there is text selected
2837 gtk_text_buffer_get_has_selection (GtkTextBuffer *buffer)
2839 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
2841 return buffer->has_selection;
2846 * Assorted other stuff
2850 * gtk_text_buffer_get_line_count:
2851 * @buffer: a #GtkTextBuffer
2853 * Obtains the number of lines in the buffer. This value is cached, so
2854 * the function is very fast.
2856 * Return value: number of lines in the buffer
2859 gtk_text_buffer_get_line_count (GtkTextBuffer *buffer)
2861 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), 0);
2863 return _gtk_text_btree_line_count (get_btree (buffer));
2867 * gtk_text_buffer_get_char_count:
2868 * @buffer: a #GtkTextBuffer
2870 * Gets the number of characters in the buffer; note that characters
2871 * and bytes are not the same, you can't e.g. expect the contents of
2872 * the buffer in string form to be this many bytes long. The character
2873 * count is cached, so this function is very fast.
2875 * Return value: number of characters in the buffer
2878 gtk_text_buffer_get_char_count (GtkTextBuffer *buffer)
2880 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), 0);
2882 return _gtk_text_btree_char_count (get_btree (buffer));
2885 /* Called when we lose the primary selection.
2888 clipboard_clear_selection_cb (GtkClipboard *clipboard,
2891 /* Move selection_bound to the insertion point */
2893 GtkTextIter selection_bound;
2894 GtkTextBuffer *buffer = GTK_TEXT_BUFFER (data);
2896 gtk_text_buffer_get_iter_at_mark (buffer, &insert,
2897 gtk_text_buffer_get_mark (buffer, "insert"));
2898 gtk_text_buffer_get_iter_at_mark (buffer, &selection_bound,
2899 gtk_text_buffer_get_mark (buffer, "selection_bound"));
2901 if (!gtk_text_iter_equal (&insert, &selection_bound))
2902 gtk_text_buffer_move_mark (buffer,
2903 gtk_text_buffer_get_mark (buffer, "selection_bound"),
2907 /* Called when we have the primary selection and someone else wants our
2908 * data in order to paste it.
2911 clipboard_get_selection_cb (GtkClipboard *clipboard,
2912 GtkSelectionData *selection_data,
2916 GtkTextBuffer *buffer = GTK_TEXT_BUFFER (data);
2917 GtkTextIter start, end;
2919 if (gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
2921 if (info == GTK_TEXT_BUFFER_TARGET_INFO_BUFFER_CONTENTS)
2923 /* Provide the address of the buffer; this will only be
2924 * used within-process
2926 gtk_selection_data_set (selection_data,
2927 selection_data->target,
2932 else if (info == GTK_TEXT_BUFFER_TARGET_INFO_RICH_TEXT)
2937 str = gtk_text_buffer_serialize (buffer, buffer,
2938 selection_data->target,
2939 &start, &end, &len);
2941 gtk_selection_data_set (selection_data,
2942 selection_data->target,
2951 str = gtk_text_iter_get_visible_text (&start, &end);
2952 gtk_selection_data_set_text (selection_data, str, -1);
2958 static GtkTextBuffer *
2959 create_clipboard_contents_buffer (GtkTextBuffer *buffer)
2961 GtkTextBuffer *contents;
2963 contents = gtk_text_buffer_new (gtk_text_buffer_get_tag_table (buffer));
2965 g_object_set_data (G_OBJECT (contents), I_("gtk-text-buffer-clipboard-source"),
2967 g_object_set_data (G_OBJECT (contents), I_("gtk-text-buffer-clipboard"),
2968 GINT_TO_POINTER (1));
2970 /* Ref the source buffer as long as the clipboard contents buffer
2971 * exists, because it's needed for serializing the contents buffer.
2972 * See http://bugzilla.gnome.org/show_bug.cgi?id=339195
2974 g_object_ref (buffer);
2975 g_object_weak_ref (G_OBJECT (contents), (GWeakNotify) g_object_unref, buffer);
2980 /* Provide cut/copied data */
2982 clipboard_get_contents_cb (GtkClipboard *clipboard,
2983 GtkSelectionData *selection_data,
2987 GtkTextBuffer *contents = GTK_TEXT_BUFFER (data);
2989 g_assert (contents); /* This should never be called unless we own the clipboard */
2991 if (info == GTK_TEXT_BUFFER_TARGET_INFO_BUFFER_CONTENTS)
2993 /* Provide the address of the clipboard buffer; this will only
2994 * be used within-process. OK to supply a NULL value for contents.
2996 gtk_selection_data_set (selection_data,
2997 selection_data->target,
3002 else if (info == GTK_TEXT_BUFFER_TARGET_INFO_RICH_TEXT)
3004 GtkTextBuffer *clipboard_source_buffer;
3005 GtkTextIter start, end;
3009 clipboard_source_buffer = g_object_get_data (G_OBJECT (contents),
3010 "gtk-text-buffer-clipboard-source");
3012 gtk_text_buffer_get_bounds (contents, &start, &end);
3014 str = gtk_text_buffer_serialize (clipboard_source_buffer, contents,
3015 selection_data->target,
3016 &start, &end, &len);
3018 gtk_selection_data_set (selection_data,
3019 selection_data->target,
3027 GtkTextIter start, end;
3029 gtk_text_buffer_get_bounds (contents, &start, &end);
3031 str = gtk_text_iter_get_visible_text (&start, &end);
3032 gtk_selection_data_set_text (selection_data, str, -1);
3038 clipboard_clear_contents_cb (GtkClipboard *clipboard,
3041 GtkTextBuffer *contents = GTK_TEXT_BUFFER (data);
3043 g_object_unref (contents);
3047 get_paste_point (GtkTextBuffer *buffer,
3049 gboolean clear_afterward)
3051 GtkTextIter insert_point;
3052 GtkTextMark *paste_point_override;
3054 paste_point_override = gtk_text_buffer_get_mark (buffer,
3055 "gtk_paste_point_override");
3057 if (paste_point_override != NULL)
3059 gtk_text_buffer_get_iter_at_mark (buffer, &insert_point,
3060 paste_point_override);
3061 if (clear_afterward)
3062 gtk_text_buffer_delete_mark (buffer,
3063 gtk_text_buffer_get_mark (buffer,
3064 "gtk_paste_point_override"));
3068 gtk_text_buffer_get_iter_at_mark (buffer, &insert_point,
3069 gtk_text_buffer_get_mark (buffer,
3073 *iter = insert_point;
3077 pre_paste_prep (ClipboardRequest *request_data,
3078 GtkTextIter *insert_point)
3080 GtkTextBuffer *buffer = request_data->buffer;
3082 get_paste_point (buffer, insert_point, TRUE);
3084 /* If we're going to replace the selection, we insert before it to
3085 * avoid messing it up, then we delete the selection after inserting.
3087 if (request_data->replace_selection)
3089 GtkTextIter start, end;
3091 if (gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
3092 *insert_point = start;
3097 post_paste_cleanup (ClipboardRequest *request_data)
3099 if (request_data->replace_selection)
3101 GtkTextIter start, end;
3103 if (gtk_text_buffer_get_selection_bounds (request_data->buffer,
3106 if (request_data->interactive)
3107 gtk_text_buffer_delete_interactive (request_data->buffer,
3110 request_data->default_editable);
3112 gtk_text_buffer_delete (request_data->buffer, &start, &end);
3118 free_clipboard_request (ClipboardRequest *request_data)
3120 g_object_unref (request_data->buffer);
3121 g_free (request_data);
3124 /* Called when we request a paste and receive the text data
3127 clipboard_text_received (GtkClipboard *clipboard,
3131 ClipboardRequest *request_data = data;
3132 GtkTextBuffer *buffer = request_data->buffer;
3136 GtkTextIter insert_point;
3138 if (request_data->interactive)
3139 gtk_text_buffer_begin_user_action (buffer);
3141 pre_paste_prep (request_data, &insert_point);
3143 if (request_data->interactive)
3144 gtk_text_buffer_insert_interactive (buffer, &insert_point,
3145 str, -1, request_data->default_editable);
3147 gtk_text_buffer_insert (buffer, &insert_point,
3150 post_paste_cleanup (request_data);
3152 if (request_data->interactive)
3153 gtk_text_buffer_end_user_action (buffer);
3156 free_clipboard_request (request_data);
3159 static GtkTextBuffer*
3160 selection_data_get_buffer (GtkSelectionData *selection_data,
3161 ClipboardRequest *request_data)
3164 GtkTextBuffer *src_buffer = NULL;
3166 /* If we can get the owner, the selection is in-process */
3167 owner = gdk_selection_owner_get_for_display (selection_data->display,
3168 selection_data->selection);
3173 if (gdk_window_get_window_type (owner) == GDK_WINDOW_FOREIGN)
3176 if (selection_data->type !=
3177 gdk_atom_intern_static_string ("GTK_TEXT_BUFFER_CONTENTS"))
3180 if (selection_data->length != sizeof (src_buffer))
3183 memcpy (&src_buffer, selection_data->data, sizeof (src_buffer));
3185 if (src_buffer == NULL)
3188 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (src_buffer), NULL);
3190 if (gtk_text_buffer_get_tag_table (src_buffer) !=
3191 gtk_text_buffer_get_tag_table (request_data->buffer))
3198 /* These are pretty handy functions; maybe something like them
3199 * should be in the public API. Also, there are other places in this
3200 * file where they could be used.
3203 save_iter (const GtkTextIter *iter,
3204 gboolean left_gravity)
3206 return gtk_text_buffer_create_mark (gtk_text_iter_get_buffer (iter),
3213 restore_iter (const GtkTextIter *iter,
3216 gtk_text_buffer_get_iter_at_mark (gtk_text_mark_get_buffer (save_id),
3217 (GtkTextIter*) iter,
3219 gtk_text_buffer_delete_mark (gtk_text_mark_get_buffer (save_id),
3225 clipboard_rich_text_received (GtkClipboard *clipboard,
3231 ClipboardRequest *request_data = data;
3232 GtkTextIter insert_point;
3233 gboolean retval = TRUE;
3234 GError *error = NULL;
3235 GtkTextBufferPrivate *priv;
3237 priv = GTK_TEXT_BUFFER_GET_PRIVATE (request_data->buffer);
3239 if (text != NULL && length > 0)
3241 pre_paste_prep (request_data, &insert_point);
3243 if (request_data->interactive)
3244 gtk_text_buffer_begin_user_action (request_data->buffer);
3246 if (!request_data->interactive ||
3247 gtk_text_iter_can_insert (&insert_point,
3248 request_data->default_editable))
3250 retval = gtk_text_buffer_deserialize (request_data->buffer,
3251 request_data->buffer,
3260 g_warning ("error pasting: %s\n", error->message);
3261 g_clear_error (&error);
3264 if (request_data->interactive)
3265 gtk_text_buffer_end_user_action (request_data->buffer);
3269 post_paste_cleanup (request_data);
3274 /* Request the text selection instead */
3275 gtk_clipboard_request_text (clipboard,
3276 clipboard_text_received,
3281 paste_from_buffer (ClipboardRequest *request_data,
3282 GtkTextBuffer *src_buffer,
3283 const GtkTextIter *start,
3284 const GtkTextIter *end)
3286 GtkTextIter insert_point;
3287 GtkTextBuffer *buffer = request_data->buffer;
3289 /* We're about to emit a bunch of signals, so be safe */
3290 g_object_ref (src_buffer);
3292 pre_paste_prep (request_data, &insert_point);
3294 if (request_data->interactive)
3295 gtk_text_buffer_begin_user_action (buffer);
3297 if (!gtk_text_iter_equal (start, end))
3299 if (!request_data->interactive ||
3300 (gtk_text_iter_can_insert (&insert_point,
3301 request_data->default_editable)))
3302 gtk_text_buffer_real_insert_range (buffer,
3306 request_data->interactive);
3309 post_paste_cleanup (request_data);
3311 if (request_data->interactive)
3312 gtk_text_buffer_end_user_action (buffer);
3314 g_object_unref (src_buffer);
3316 free_clipboard_request (request_data);
3320 clipboard_clipboard_buffer_received (GtkClipboard *clipboard,
3321 GtkSelectionData *selection_data,
3324 ClipboardRequest *request_data = data;
3325 GtkTextBuffer *src_buffer;
3326 GtkTextBufferPrivate *priv;
3328 src_buffer = selection_data_get_buffer (selection_data, request_data);
3332 GtkTextIter start, end;
3334 if (g_object_get_data (G_OBJECT (src_buffer), "gtk-text-buffer-clipboard"))
3336 gtk_text_buffer_get_bounds (src_buffer, &start, &end);
3338 paste_from_buffer (request_data, src_buffer,
3343 if (gtk_text_buffer_get_selection_bounds (src_buffer, &start, &end))
3344 paste_from_buffer (request_data, src_buffer,
3350 priv = GTK_TEXT_BUFFER_GET_PRIVATE (request_data->buffer);
3352 if (gtk_clipboard_wait_is_rich_text_available (clipboard,
3353 request_data->buffer))
3355 /* Request rich text */
3356 gtk_clipboard_request_rich_text (clipboard,
3357 request_data->buffer,
3358 clipboard_rich_text_received,
3363 /* Request the text selection instead */
3364 gtk_clipboard_request_text (clipboard,
3365 clipboard_text_received,
3373 GtkClipboard *clipboard;
3375 } SelectionClipboard;
3378 update_selection_clipboards (GtkTextBuffer *buffer)
3380 GtkTextBufferPrivate *priv;
3381 GSList *tmp_list = buffer->selection_clipboards;
3383 priv = GTK_TEXT_BUFFER_GET_PRIVATE (buffer);
3385 gtk_text_buffer_get_copy_target_list (buffer);
3392 SelectionClipboard *selection_clipboard = tmp_list->data;
3393 GtkClipboard *clipboard = selection_clipboard->clipboard;
3395 /* Determine whether we have a selection and adjust X selection
3398 if (!gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
3400 if (gtk_clipboard_get_owner (clipboard) == G_OBJECT (buffer))
3401 gtk_clipboard_clear (clipboard);
3405 /* Even if we already have the selection, we need to update our
3408 if (!gtk_clipboard_set_with_owner (clipboard,
3409 priv->copy_target_entries,
3410 priv->n_copy_target_entries,
3411 clipboard_get_selection_cb,
3412 clipboard_clear_selection_cb,
3414 clipboard_clear_selection_cb (clipboard, buffer);
3417 tmp_list = tmp_list->next;
3421 static SelectionClipboard *
3422 find_selection_clipboard (GtkTextBuffer *buffer,
3423 GtkClipboard *clipboard)
3425 GSList *tmp_list = buffer->selection_clipboards;
3428 SelectionClipboard *selection_clipboard = tmp_list->data;
3429 if (selection_clipboard->clipboard == clipboard)
3430 return selection_clipboard;
3432 tmp_list = tmp_list->next;
3439 * gtk_text_buffer_add_selection_clipboard:
3440 * @buffer: a #GtkTextBuffer
3441 * @clipboard: a #GtkClipboard
3443 * Adds @clipboard to the list of clipboards in which the selection contents
3444 * of @buffer are available. In most cases, @clipboard will be the #GtkClipboard
3445 * of type %GDK_SELECTION_PRIMARY for a view of @buffer.
3448 gtk_text_buffer_add_selection_clipboard (GtkTextBuffer *buffer,
3449 GtkClipboard *clipboard)
3451 SelectionClipboard *selection_clipboard;
3453 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
3454 g_return_if_fail (clipboard != NULL);
3456 selection_clipboard = find_selection_clipboard (buffer, clipboard);
3457 if (selection_clipboard)
3459 selection_clipboard->ref_count++;
3463 selection_clipboard = g_new (SelectionClipboard, 1);
3465 selection_clipboard->clipboard = clipboard;
3466 selection_clipboard->ref_count = 1;
3468 buffer->selection_clipboards = g_slist_prepend (buffer->selection_clipboards, selection_clipboard);
3473 * gtk_text_buffer_remove_selection_clipboard:
3474 * @buffer: a #GtkTextBuffer
3475 * @clipboard: a #GtkClipboard added to @buffer by gtk_text_buffer_add_selection_clipboard().
3477 * Removes a #GtkClipboard added with gtk_text_buffer_add_selection_clipboard()
3480 gtk_text_buffer_remove_selection_clipboard (GtkTextBuffer *buffer,
3481 GtkClipboard *clipboard)
3483 SelectionClipboard *selection_clipboard;
3485 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
3486 g_return_if_fail (clipboard != NULL);
3488 selection_clipboard = find_selection_clipboard (buffer, clipboard);
3489 g_return_if_fail (selection_clipboard != NULL);
3491 selection_clipboard->ref_count--;
3492 if (selection_clipboard->ref_count == 0)
3494 if (gtk_clipboard_get_owner (selection_clipboard->clipboard) == G_OBJECT (buffer))
3495 gtk_clipboard_clear (selection_clipboard->clipboard);
3497 buffer->selection_clipboards = g_slist_remove (buffer->selection_clipboards,
3498 selection_clipboard);
3500 g_free (selection_clipboard);
3505 remove_all_selection_clipboards (GtkTextBuffer *buffer)
3507 GSList *tmp_list = buffer->selection_clipboards;
3510 SelectionClipboard *selection_clipboard = tmp_list->data;
3512 if (gtk_clipboard_get_owner (selection_clipboard->clipboard) == G_OBJECT (buffer))
3513 gtk_clipboard_clear (selection_clipboard->clipboard);
3515 g_free (selection_clipboard);
3517 tmp_list = tmp_list->next;
3520 g_slist_free (buffer->selection_clipboards);
3521 buffer->selection_clipboards = NULL;
3525 * gtk_text_buffer_paste_clipboard:
3526 * @buffer: a #GtkTextBuffer
3527 * @clipboard: the #GtkClipboard to paste from
3528 * @override_location: location to insert pasted text, or %NULL for at the cursor
3529 * @default_editable: whether the buffer is editable by default
3531 * Pastes the contents of a clipboard at the insertion point, or at @override_location.
3532 * (Note: pasting is asynchronous, that is, we'll ask for the paste data and
3533 * return, and at some point later after the main loop runs, the paste
3534 * data will be inserted.)
3538 gtk_text_buffer_paste_clipboard (GtkTextBuffer *buffer,
3539 GtkClipboard *clipboard,
3540 GtkTextIter *override_location,
3541 gboolean default_editable)
3543 ClipboardRequest *data = g_new (ClipboardRequest, 1);
3544 GtkTextIter paste_point;
3545 GtkTextIter start, end;
3547 if (override_location != NULL)
3548 gtk_text_buffer_create_mark (buffer,
3549 "gtk_paste_point_override",
3550 override_location, FALSE);
3552 data->buffer = g_object_ref (buffer);
3553 data->interactive = TRUE;
3554 data->default_editable = default_editable;
3556 /* When pasting with the cursor inside the selection area, you
3557 * replace the selection with the new text, otherwise, you
3558 * simply insert the new text at the point where the click
3559 * occured, unselecting any selected text. The replace_selection
3560 * flag toggles this behavior.
3562 data->replace_selection = FALSE;
3564 get_paste_point (buffer, &paste_point, FALSE);
3565 if (gtk_text_buffer_get_selection_bounds (buffer, &start, &end) &&
3566 (gtk_text_iter_in_range (&paste_point, &start, &end) ||
3567 gtk_text_iter_equal (&paste_point, &end)))
3568 data->replace_selection = TRUE;
3570 gtk_clipboard_request_contents (clipboard,
3571 gdk_atom_intern_static_string ("GTK_TEXT_BUFFER_CONTENTS"),
3572 clipboard_clipboard_buffer_received, data);
3576 * gtk_text_buffer_delete_selection:
3577 * @buffer: a #GtkTextBuffer
3578 * @interactive: whether the deletion is caused by user interaction
3579 * @default_editable: whether the buffer is editable by default
3581 * Deletes the range between the "insert" and "selection_bound" marks,
3582 * that is, the currently-selected text. If @interactive is %TRUE,
3583 * the editability of the selection will be considered (users can't delete
3586 * Return value: whether there was a non-empty selection to delete
3589 gtk_text_buffer_delete_selection (GtkTextBuffer *buffer,
3590 gboolean interactive,
3591 gboolean default_editable)
3596 if (!gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
3598 return FALSE; /* No selection */
3604 gtk_text_buffer_begin_user_action (buffer);
3605 gtk_text_buffer_delete_interactive (buffer, &start, &end, default_editable);
3606 gtk_text_buffer_end_user_action (buffer);
3609 gtk_text_buffer_delete (buffer, &start, &end);
3611 return TRUE; /* We deleted stuff */
3616 * gtk_text_buffer_backspace:
3617 * @buffer: a #GtkTextBuffer
3618 * @iter: a position in @buffer
3619 * @interactive: whether the deletion is caused by user interaction
3620 * @default_editable: whether the buffer is editable by default
3622 * Performs the appropriate action as if the user hit the delete
3623 * key with the cursor at the position specified by @iter. In the
3624 * normal case a single character will be deleted, but when
3625 * combining accents are involved, more than one character can
3626 * be deleted, and when precomposed character and accent combinations
3627 * are involved, less than one character will be deleted.
3629 * Because the buffer is modified, all outstanding iterators become
3630 * invalid after calling this function; however, the @iter will be
3631 * re-initialized to point to the location where text was deleted.
3633 * Return value: %TRUE if the buffer was modified
3638 gtk_text_buffer_backspace (GtkTextBuffer *buffer,
3640 gboolean interactive,
3641 gboolean default_editable)
3643 gchar *cluster_text;
3646 gboolean retval = FALSE;
3647 const PangoLogAttr *attrs;
3649 gboolean backspace_deletes_character;
3651 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
3652 g_return_val_if_fail (iter != NULL, FALSE);
3657 attrs = _gtk_text_buffer_get_line_log_attrs (buffer, &start, NULL);
3659 /* For no good reason, attrs is NULL for the empty last line in
3660 * a buffer. Special case that here. (#156164)
3664 offset = gtk_text_iter_get_line_offset (&start);
3665 backspace_deletes_character = attrs[offset].backspace_deletes_character;
3668 backspace_deletes_character = FALSE;
3670 gtk_text_iter_backward_cursor_position (&start);
3672 if (gtk_text_iter_equal (&start, &end))
3675 cluster_text = gtk_text_iter_get_text (&start, &end);
3678 gtk_text_buffer_begin_user_action (buffer);
3680 if (gtk_text_buffer_delete_interactive (buffer, &start, &end,
3683 if (backspace_deletes_character)
3685 gchar *normalized_text = g_utf8_normalize (cluster_text,
3686 strlen (cluster_text),
3688 glong len = g_utf8_strlen (normalized_text, -1);
3691 gtk_text_buffer_insert_interactive (buffer,
3694 g_utf8_offset_to_pointer (normalized_text, len - 1) - normalized_text,
3697 g_free (normalized_text);
3704 gtk_text_buffer_end_user_action (buffer);
3706 g_free (cluster_text);
3708 /* Revalidate the users iter */
3715 cut_or_copy (GtkTextBuffer *buffer,
3716 GtkClipboard *clipboard,
3717 gboolean delete_region_after,
3718 gboolean interactive,
3719 gboolean default_editable)
3721 GtkTextBufferPrivate *priv;
3723 /* We prefer to cut the selected region between selection_bound and
3724 * insertion point. If that region is empty, then we cut the region
3725 * between the "anchor" and the insertion point (this is for
3726 * C-space and M-w and other Emacs-style copy/yank keys). Note that
3727 * insert and selection_bound are guaranteed to exist, but the
3728 * anchor only exists sometimes.
3733 priv = GTK_TEXT_BUFFER_GET_PRIVATE (buffer);
3735 gtk_text_buffer_get_copy_target_list (buffer);
3737 if (!gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
3739 /* Let's try the anchor thing */
3740 GtkTextMark * anchor = gtk_text_buffer_get_mark (buffer, "anchor");
3746 gtk_text_buffer_get_iter_at_mark (buffer, &end, anchor);
3747 gtk_text_iter_order (&start, &end);
3751 if (!gtk_text_iter_equal (&start, &end))
3754 GtkTextBuffer *contents;
3756 contents = create_clipboard_contents_buffer (buffer);
3758 gtk_text_buffer_get_iter_at_offset (contents, &ins, 0);
3760 gtk_text_buffer_insert_range (contents, &ins, &start, &end);
3762 if (!gtk_clipboard_set_with_data (clipboard,
3763 priv->copy_target_entries,
3764 priv->n_copy_target_entries,
3765 clipboard_get_contents_cb,
3766 clipboard_clear_contents_cb,
3768 g_object_unref (contents);
3770 gtk_clipboard_set_can_store (clipboard,
3771 priv->copy_target_entries + 1,
3772 priv->n_copy_target_entries - 1);
3774 if (delete_region_after)
3777 gtk_text_buffer_delete_interactive (buffer, &start, &end,
3780 gtk_text_buffer_delete (buffer, &start, &end);
3786 * gtk_text_buffer_cut_clipboard:
3787 * @buffer: a #GtkTextBuffer
3788 * @clipboard: the #GtkClipboard object to cut to.
3789 * @default_editable: default editability of the buffer
3791 * Copies the currently-selected text to a clipboard, then deletes
3792 * said text if it's editable.
3796 gtk_text_buffer_cut_clipboard (GtkTextBuffer *buffer,
3797 GtkClipboard *clipboard,
3798 gboolean default_editable)
3800 gtk_text_buffer_begin_user_action (buffer);
3801 cut_or_copy (buffer, clipboard, TRUE, TRUE, default_editable);
3802 gtk_text_buffer_end_user_action (buffer);
3806 * gtk_text_buffer_copy_clipboard:
3807 * @buffer: a #GtkTextBuffer
3808 * @clipboard: the #GtkClipboard object to copy to.
3810 * Copies the currently-selected text to a clipboard.
3814 gtk_text_buffer_copy_clipboard (GtkTextBuffer *buffer,
3815 GtkClipboard *clipboard)
3817 gtk_text_buffer_begin_user_action (buffer);
3818 cut_or_copy (buffer, clipboard, FALSE, TRUE, TRUE);
3819 gtk_text_buffer_end_user_action (buffer);
3824 * gtk_text_buffer_get_selection_bounds:
3825 * @buffer: a #GtkTextBuffer a #GtkTextBuffer
3826 * @start: iterator to initialize with selection start
3827 * @end: iterator to initialize with selection end
3829 * Returns %TRUE if some text is selected; places the bounds
3830 * of the selection in @start and @end (if the selection has length 0,
3831 * then @start and @end are filled in with the same value).
3832 * @start and @end will be in ascending order. If @start and @end are
3833 * NULL, then they are not filled in, but the return value still indicates
3834 * whether text is selected.
3836 * Return value: whether the selection has nonzero length
3839 gtk_text_buffer_get_selection_bounds (GtkTextBuffer *buffer,
3843 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
3845 return _gtk_text_btree_get_selection_bounds (get_btree (buffer), start, end);
3849 * gtk_text_buffer_begin_user_action:
3850 * @buffer: a #GtkTextBuffer
3852 * Called to indicate that the buffer operations between here and a
3853 * call to gtk_text_buffer_end_user_action() are part of a single
3854 * user-visible operation. The operations between
3855 * gtk_text_buffer_begin_user_action() and
3856 * gtk_text_buffer_end_user_action() can then be grouped when creating
3857 * an undo stack. #GtkTextBuffer maintains a count of calls to
3858 * gtk_text_buffer_begin_user_action() that have not been closed with
3859 * a call to gtk_text_buffer_end_user_action(), and emits the "begin_user_action"
3860 * and "end_user_action" signals only for the outermost pair of calls.
3861 * This allows you to build user actions from other user actions.
3863 * The "interactive" buffer mutation functions, such as
3864 * gtk_text_buffer_insert_interactive(), automatically call begin/end
3865 * user action around the buffer operations they perform, so there's
3866 * no need to add extra calls if you user action consists solely of a
3867 * single call to one of those functions.
3870 gtk_text_buffer_begin_user_action (GtkTextBuffer *buffer)
3872 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
3874 buffer->user_action_count += 1;
3876 if (buffer->user_action_count == 1)
3878 /* Outermost nested user action begin emits the signal */
3879 g_signal_emit (buffer, signals[BEGIN_USER_ACTION], 0);
3884 * gtk_text_buffer_end_user_action:
3885 * @buffer: a #GtkTextBuffer
3887 * Should be paired with a call to gtk_text_buffer_begin_user_action().
3888 * See that function for a full explanation.
3891 gtk_text_buffer_end_user_action (GtkTextBuffer *buffer)
3893 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
3894 g_return_if_fail (buffer->user_action_count > 0);
3896 buffer->user_action_count -= 1;
3898 if (buffer->user_action_count == 0)
3900 /* Ended the outermost-nested user action end, so emit the signal */
3901 g_signal_emit (buffer, signals[END_USER_ACTION], 0);
3906 gtk_text_buffer_free_target_lists (GtkTextBuffer *buffer)
3908 GtkTextBufferPrivate *priv = GTK_TEXT_BUFFER_GET_PRIVATE (buffer);
3910 if (priv->copy_target_list)
3912 gtk_target_list_unref (priv->copy_target_list);
3913 priv->copy_target_list = NULL;
3915 gtk_target_table_free (priv->copy_target_entries,
3916 priv->n_copy_target_entries);
3917 priv->copy_target_entries = NULL;
3918 priv->n_copy_target_entries = 0;
3921 if (priv->paste_target_list)
3923 gtk_target_list_unref (priv->paste_target_list);
3924 priv->paste_target_list = NULL;
3926 gtk_target_table_free (priv->paste_target_entries,
3927 priv->n_paste_target_entries);
3928 priv->paste_target_entries = NULL;
3929 priv->n_paste_target_entries = 0;
3933 static GtkTargetList *
3934 gtk_text_buffer_get_target_list (GtkTextBuffer *buffer,
3935 gboolean deserializable,
3936 GtkTargetEntry **entries,
3939 GtkTargetList *target_list;
3941 target_list = gtk_target_list_new (NULL, 0);
3943 gtk_target_list_add (target_list,
3944 gdk_atom_intern_static_string ("GTK_TEXT_BUFFER_CONTENTS"),
3945 GTK_TARGET_SAME_APP,
3946 GTK_TEXT_BUFFER_TARGET_INFO_BUFFER_CONTENTS);
3948 gtk_target_list_add_rich_text_targets (target_list,
3949 GTK_TEXT_BUFFER_TARGET_INFO_RICH_TEXT,
3953 gtk_target_list_add_text_targets (target_list,
3954 GTK_TEXT_BUFFER_TARGET_INFO_TEXT);
3956 *entries = gtk_target_table_new_from_list (target_list, n_entries);
3962 * gtk_text_buffer_get_copy_target_list:
3963 * @buffer: a #GtkTextBuffer
3965 * This function returns the list of targets this text buffer can
3966 * provide for copying and as DND source. The targets in the list are
3967 * added with %info values from the #GtkTextBufferTargetInfo enum,
3968 * using gtk_target_list_add_rich_text_targets() and
3969 * gtk_target_list_add_text_targets()
3971 * Return value: the #GtkTargetList
3976 gtk_text_buffer_get_copy_target_list (GtkTextBuffer *buffer)
3978 GtkTextBufferPrivate *priv;
3980 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
3982 priv = GTK_TEXT_BUFFER_GET_PRIVATE (buffer);
3984 if (! priv->copy_target_list)
3985 priv->copy_target_list =
3986 gtk_text_buffer_get_target_list (buffer, FALSE,
3987 &priv->copy_target_entries,
3988 &priv->n_copy_target_entries);
3990 return priv->copy_target_list;
3994 * gtk_text_buffer_get_paste_target_list:
3995 * @buffer: a #GtkTextBuffer
3997 * This function returns the list of targets this text buffer supports
3998 * for pasting and as DND destination. The targets in the list are
3999 * added with %info values from the #GtkTextBufferTargetInfo enum,
4000 * using gtk_target_list_add_rich_text_targets() and
4001 * gtk_target_list_add_text_targets()
4003 * Return value: the #GtkTargetList
4008 gtk_text_buffer_get_paste_target_list (GtkTextBuffer *buffer)
4010 GtkTextBufferPrivate *priv;
4012 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
4014 priv = GTK_TEXT_BUFFER_GET_PRIVATE (buffer);
4016 if (! priv->paste_target_list)
4017 priv->paste_target_list =
4018 gtk_text_buffer_get_target_list (buffer, TRUE,
4019 &priv->paste_target_entries,
4020 &priv->n_paste_target_entries);
4022 return priv->paste_target_list;
4026 * Logical attribute cache
4029 #define ATTR_CACHE_SIZE 2
4031 typedef struct _CacheEntry CacheEntry;
4036 PangoLogAttr *attrs;
4040 struct _GtkTextLogAttrCache
4042 gint chars_changed_stamp;
4043 CacheEntry entries[ATTR_CACHE_SIZE];
4047 free_log_attr_cache (GtkTextLogAttrCache *cache)
4050 while (i < ATTR_CACHE_SIZE)
4052 g_free (cache->entries[i].attrs);
4059 clear_log_attr_cache (GtkTextLogAttrCache *cache)
4062 while (i < ATTR_CACHE_SIZE)
4064 g_free (cache->entries[i].attrs);
4065 cache->entries[i].attrs = NULL;
4070 static PangoLogAttr*
4071 compute_log_attrs (const GtkTextIter *iter,
4077 gint char_len, byte_len;
4078 PangoLogAttr *attrs = NULL;
4083 gtk_text_iter_set_line_offset (&start, 0);
4084 gtk_text_iter_forward_line (&end);
4086 paragraph = gtk_text_iter_get_slice (&start, &end);
4087 char_len = g_utf8_strlen (paragraph, -1);
4088 byte_len = strlen (paragraph);
4090 g_assert (char_len > 0);
4093 *char_lenp = char_len;
4095 attrs = g_new (PangoLogAttr, char_len + 1);
4097 /* FIXME we need to follow PangoLayout and allow different language
4098 * tags within the paragraph
4100 pango_get_log_attrs (paragraph, byte_len, -1,
4101 gtk_text_iter_get_language (&start),
4110 /* The return value from this is valid until you call this a second time.
4113 _gtk_text_buffer_get_line_log_attrs (GtkTextBuffer *buffer,
4114 const GtkTextIter *anywhere_in_line,
4118 GtkTextLogAttrCache *cache;
4121 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
4122 g_return_val_if_fail (anywhere_in_line != NULL, NULL);
4124 /* special-case for empty last line in buffer */
4125 if (gtk_text_iter_is_end (anywhere_in_line) &&
4126 gtk_text_iter_get_line_offset (anywhere_in_line) == 0)
4133 /* FIXME we also need to recompute log attrs if the language tag at
4134 * the start of a paragraph changes
4137 if (buffer->log_attr_cache == NULL)
4139 buffer->log_attr_cache = g_new0 (GtkTextLogAttrCache, 1);
4140 buffer->log_attr_cache->chars_changed_stamp =
4141 _gtk_text_btree_get_chars_changed_stamp (get_btree (buffer));
4143 else if (buffer->log_attr_cache->chars_changed_stamp !=
4144 _gtk_text_btree_get_chars_changed_stamp (get_btree (buffer)))
4146 clear_log_attr_cache (buffer->log_attr_cache);
4149 cache = buffer->log_attr_cache;
4150 line = gtk_text_iter_get_line (anywhere_in_line);
4153 while (i < ATTR_CACHE_SIZE)
4155 if (cache->entries[i].attrs &&
4156 cache->entries[i].line == line)
4159 *char_len = cache->entries[i].char_len;
4160 return cache->entries[i].attrs;
4165 /* Not in cache; open up the first cache entry */
4166 if (cache->entries[ATTR_CACHE_SIZE-1].attrs)
4167 g_free (cache->entries[ATTR_CACHE_SIZE-1].attrs);
4169 g_memmove (cache->entries + 1, cache->entries,
4170 sizeof (CacheEntry) * (ATTR_CACHE_SIZE - 1));
4172 cache->entries[0].line = line;
4173 cache->entries[0].attrs = compute_log_attrs (anywhere_in_line,
4174 &cache->entries[0].char_len);
4177 *char_len = cache->entries[0].char_len;
4179 return cache->entries[0].attrs;
4183 _gtk_text_buffer_notify_will_remove_tag (GtkTextBuffer *buffer,
4186 /* This removes tag from the buffer, but DOESN'T emit the
4187 * remove_tag signal, because we can't afford to have user
4188 * code messing things up at this point; the tag MUST be removed
4192 _gtk_text_btree_notify_will_remove_tag (buffer->btree, tag);
4200 _gtk_text_buffer_spew (GtkTextBuffer *buffer)
4202 _gtk_text_btree_spew (get_btree (buffer));
4205 #define __GTK_TEXT_BUFFER_C__
4206 #include "gtkaliasdef.c"