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, see <http://www.gnu.org/licenses/>.
20 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
21 * file for a list of people on the GTK+ Team. See the ChangeLog
22 * files for a list of changes. These files are distributed with
23 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
30 #define GTK_TEXT_USE_INTERNAL_UNSUPPORTED_API
31 #include "gtkclipboard.h"
33 #include "gtkinvisible.h"
34 #include "gtkmarshalers.h"
35 #include "gtktextbuffer.h"
36 #include "gtktextbufferrichtext.h"
37 #include "gtktextbtree.h"
38 #include "gtktextiterprivate.h"
39 #include "gtktexttagprivate.h"
40 #include "gtkprivate.h"
45 * SECTION:gtktextbuffer
46 * @Short_description: Stores attributed text for display in a GtkTextView
47 * @Title: GtkTextBuffer
48 * @See_also: #GtkTextView, #GtkTextIter, #GtkTextMark
50 * You may wish to begin by reading the <link linkend="TextWidget">text widget
51 * conceptual overview</link> which gives an overview of all the objects and data
52 * types related to the text widget and how they work together.
56 typedef struct _GtkTextLogAttrCache GtkTextLogAttrCache;
58 struct _GtkTextBufferPrivate
60 GtkTargetList *copy_target_list;
61 GtkTargetEntry *copy_target_entries;
62 GtkTargetList *paste_target_list;
63 GtkTargetEntry *paste_target_entries;
65 gint n_copy_target_entries;
66 gint n_paste_target_entries;
68 GtkTextTagTable *tag_table;
71 GSList *clipboard_contents_buffers;
72 GSList *selection_clipboards;
74 GtkTextLogAttrCache *log_attr_cache;
76 guint user_action_count;
78 /* Whether the buffer has been modified since last save */
80 guint has_selection : 1;
84 typedef struct _ClipboardRequest ClipboardRequest;
86 struct _ClipboardRequest
88 GtkTextBuffer *buffer;
89 guint interactive : 1;
90 guint default_editable : 1;
91 guint replace_selection : 1;
120 PROP_CURSOR_POSITION,
121 PROP_COPY_TARGET_LIST,
122 PROP_PASTE_TARGET_LIST
125 static void gtk_text_buffer_finalize (GObject *object);
127 static void gtk_text_buffer_real_insert_text (GtkTextBuffer *buffer,
131 static void gtk_text_buffer_real_insert_pixbuf (GtkTextBuffer *buffer,
134 static void gtk_text_buffer_real_insert_anchor (GtkTextBuffer *buffer,
136 GtkTextChildAnchor *anchor);
137 static void gtk_text_buffer_real_delete_range (GtkTextBuffer *buffer,
140 static void gtk_text_buffer_real_apply_tag (GtkTextBuffer *buffer,
142 const GtkTextIter *start_char,
143 const GtkTextIter *end_char);
144 static void gtk_text_buffer_real_remove_tag (GtkTextBuffer *buffer,
146 const GtkTextIter *start_char,
147 const GtkTextIter *end_char);
148 static void gtk_text_buffer_real_changed (GtkTextBuffer *buffer);
149 static void gtk_text_buffer_real_mark_set (GtkTextBuffer *buffer,
150 const GtkTextIter *iter,
153 static GtkTextBTree* get_btree (GtkTextBuffer *buffer);
154 static void free_log_attr_cache (GtkTextLogAttrCache *cache);
156 static void remove_all_selection_clipboards (GtkTextBuffer *buffer);
157 static void update_selection_clipboards (GtkTextBuffer *buffer);
159 static GtkTextBuffer *create_clipboard_contents_buffer (GtkTextBuffer *buffer);
161 static void gtk_text_buffer_free_target_lists (GtkTextBuffer *buffer);
163 static guint signals[LAST_SIGNAL] = { 0 };
165 static void gtk_text_buffer_set_property (GObject *object,
169 static void gtk_text_buffer_get_property (GObject *object,
173 static void gtk_text_buffer_notify (GObject *object,
176 G_DEFINE_TYPE (GtkTextBuffer, gtk_text_buffer, G_TYPE_OBJECT)
179 gtk_text_buffer_class_init (GtkTextBufferClass *klass)
181 GObjectClass *object_class = G_OBJECT_CLASS (klass);
183 object_class->finalize = gtk_text_buffer_finalize;
184 object_class->set_property = gtk_text_buffer_set_property;
185 object_class->get_property = gtk_text_buffer_get_property;
186 object_class->notify = gtk_text_buffer_notify;
188 klass->insert_text = gtk_text_buffer_real_insert_text;
189 klass->insert_pixbuf = gtk_text_buffer_real_insert_pixbuf;
190 klass->insert_child_anchor = gtk_text_buffer_real_insert_anchor;
191 klass->delete_range = gtk_text_buffer_real_delete_range;
192 klass->apply_tag = gtk_text_buffer_real_apply_tag;
193 klass->remove_tag = gtk_text_buffer_real_remove_tag;
194 klass->changed = gtk_text_buffer_real_changed;
195 klass->mark_set = gtk_text_buffer_real_mark_set;
198 g_object_class_install_property (object_class,
200 g_param_spec_object ("tag-table",
202 P_("Text Tag Table"),
203 GTK_TYPE_TEXT_TAG_TABLE,
204 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
206 /* Normal properties*/
209 * GtkTextBuffer:text:
211 * The text content of the buffer. Without child widgets and images,
212 * see gtk_text_buffer_get_text() for more information.
216 g_object_class_install_property (object_class,
218 g_param_spec_string ("text",
220 P_("Current text of the buffer"),
222 GTK_PARAM_READWRITE));
225 * GtkTextBuffer:has-selection:
227 * Whether the buffer has some text currently selected.
231 g_object_class_install_property (object_class,
233 g_param_spec_boolean ("has-selection",
235 P_("Whether the buffer has some text currently selected"),
237 GTK_PARAM_READABLE));
240 * GtkTextBuffer:cursor-position:
242 * The position of the insert mark (as offset from the beginning
243 * of the buffer). It is useful for getting notified when the
248 g_object_class_install_property (object_class,
249 PROP_CURSOR_POSITION,
250 g_param_spec_int ("cursor-position",
251 P_("Cursor position"),
252 P_("The position of the insert mark (as offset from the beginning of the buffer)"),
254 GTK_PARAM_READABLE));
257 * GtkTextBuffer:copy-target-list:
259 * The list of targets this buffer supports for clipboard copying
264 g_object_class_install_property (object_class,
265 PROP_COPY_TARGET_LIST,
266 g_param_spec_boxed ("copy-target-list",
267 P_("Copy target list"),
268 P_("The list of targets this buffer supports for clipboard copying and DND source"),
269 GTK_TYPE_TARGET_LIST,
270 GTK_PARAM_READABLE));
273 * GtkTextBuffer:paste-target-list:
275 * The list of targets this buffer supports for clipboard pasting
276 * and as DND destination.
280 g_object_class_install_property (object_class,
281 PROP_PASTE_TARGET_LIST,
282 g_param_spec_boxed ("paste-target-list",
283 P_("Paste target list"),
284 P_("The list of targets this buffer supports for clipboard pasting and DND destination"),
285 GTK_TYPE_TARGET_LIST,
286 GTK_PARAM_READABLE));
289 * GtkTextBuffer::insert-text:
290 * @textbuffer: the object which received the signal
291 * @location: position to insert @text in @textbuffer
292 * @text: the UTF-8 text to be inserted
293 * @len: length of the inserted text in bytes
295 * The ::insert-text signal is emitted to insert text in a #GtkTextBuffer.
296 * Insertion actually occurs in the default handler.
298 * Note that if your handler runs before the default handler it must not
299 * invalidate the @location iter (or has to revalidate it).
300 * The default signal handler revalidates it to point to the end of the
304 * gtk_text_buffer_insert(),
305 * gtk_text_buffer_insert_range().
307 signals[INSERT_TEXT] =
308 g_signal_new (I_("insert-text"),
309 G_OBJECT_CLASS_TYPE (object_class),
311 G_STRUCT_OFFSET (GtkTextBufferClass, insert_text),
313 _gtk_marshal_VOID__BOXED_STRING_INT,
316 GTK_TYPE_TEXT_ITER | G_SIGNAL_TYPE_STATIC_SCOPE,
317 G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE,
319 g_signal_set_va_marshaller (signals[INSERT_TEXT], G_TYPE_FROM_CLASS (klass),
320 _gtk_marshal_VOID__BOXED_STRING_INTv);
323 * GtkTextBuffer::insert-pixbuf:
324 * @textbuffer: the object which received the signal
325 * @location: position to insert @pixbuf in @textbuffer
326 * @pixbuf: the #GdkPixbuf to be inserted
328 * The ::insert-pixbuf signal is emitted to insert a #GdkPixbuf
329 * in a #GtkTextBuffer. Insertion actually occurs in the default handler.
331 * Note that if your handler runs before the default handler it must not
332 * invalidate the @location iter (or has to revalidate it).
333 * The default signal handler revalidates it to be placed after the
336 * See also: gtk_text_buffer_insert_pixbuf().
338 signals[INSERT_PIXBUF] =
339 g_signal_new (I_("insert-pixbuf"),
340 G_OBJECT_CLASS_TYPE (object_class),
342 G_STRUCT_OFFSET (GtkTextBufferClass, insert_pixbuf),
344 _gtk_marshal_VOID__BOXED_OBJECT,
347 GTK_TYPE_TEXT_ITER | G_SIGNAL_TYPE_STATIC_SCOPE,
352 * GtkTextBuffer::insert-child-anchor:
353 * @textbuffer: the object which received the signal
354 * @location: position to insert @anchor in @textbuffer
355 * @anchor: the #GtkTextChildAnchor to be inserted
357 * The ::insert-child-anchor signal is emitted to insert a
358 * #GtkTextChildAnchor in a #GtkTextBuffer.
359 * Insertion actually occurs in the default handler.
361 * Note that if your handler runs before the default handler it must
362 * not invalidate the @location iter (or has to revalidate it).
363 * The default signal handler revalidates it to be placed after the
366 * See also: gtk_text_buffer_insert_child_anchor().
368 signals[INSERT_CHILD_ANCHOR] =
369 g_signal_new (I_("insert-child-anchor"),
370 G_OBJECT_CLASS_TYPE (object_class),
372 G_STRUCT_OFFSET (GtkTextBufferClass, insert_child_anchor),
374 _gtk_marshal_VOID__BOXED_OBJECT,
377 GTK_TYPE_TEXT_ITER | G_SIGNAL_TYPE_STATIC_SCOPE,
378 GTK_TYPE_TEXT_CHILD_ANCHOR);
381 * GtkTextBuffer::delete-range:
382 * @textbuffer: the object which received the signal
383 * @start: the start of the range to be deleted
384 * @end: the end of the range to be deleted
386 * The ::delete-range signal is emitted to delete a range
387 * from a #GtkTextBuffer.
389 * Note that if your handler runs before the default handler it must not
390 * invalidate the @start and @end iters (or has to revalidate them).
391 * The default signal handler revalidates the @start and @end iters to
392 * both point point to the location where text was deleted. Handlers
393 * which run after the default handler (see g_signal_connect_after())
394 * do not have access to the deleted text.
396 * See also: gtk_text_buffer_delete().
398 signals[DELETE_RANGE] =
399 g_signal_new (I_("delete-range"),
400 G_OBJECT_CLASS_TYPE (object_class),
402 G_STRUCT_OFFSET (GtkTextBufferClass, delete_range),
404 _gtk_marshal_VOID__BOXED_BOXED,
407 GTK_TYPE_TEXT_ITER | G_SIGNAL_TYPE_STATIC_SCOPE,
408 GTK_TYPE_TEXT_ITER | G_SIGNAL_TYPE_STATIC_SCOPE);
411 * GtkTextBuffer::changed:
412 * @textbuffer: the object which received the signal
414 * The ::changed signal is emitted when the content of a #GtkTextBuffer
418 g_signal_new (I_("changed"),
419 G_OBJECT_CLASS_TYPE (object_class),
421 G_STRUCT_OFFSET (GtkTextBufferClass, changed),
423 _gtk_marshal_VOID__VOID,
428 * GtkTextBuffer::modified-changed:
429 * @textbuffer: the object which received the signal
431 * The ::modified-changed signal is emitted when the modified bit of a
432 * #GtkTextBuffer flips.
435 * gtk_text_buffer_set_modified().
437 signals[MODIFIED_CHANGED] =
438 g_signal_new (I_("modified-changed"),
439 G_OBJECT_CLASS_TYPE (object_class),
441 G_STRUCT_OFFSET (GtkTextBufferClass, modified_changed),
443 _gtk_marshal_VOID__VOID,
448 * GtkTextBuffer::mark-set:
449 * @textbuffer: the object which received the signal
450 * @location: The location of @mark in @textbuffer
451 * @mark: The mark that is set
453 * The ::mark-set signal is emitted as notification
454 * after a #GtkTextMark is set.
457 * gtk_text_buffer_create_mark(),
458 * gtk_text_buffer_move_mark().
461 g_signal_new (I_("mark-set"),
462 G_OBJECT_CLASS_TYPE (object_class),
464 G_STRUCT_OFFSET (GtkTextBufferClass, mark_set),
466 _gtk_marshal_VOID__BOXED_OBJECT,
473 * GtkTextBuffer::mark-deleted:
474 * @textbuffer: the object which received the signal
475 * @mark: The mark that was deleted
477 * The ::mark-deleted signal is emitted as notification
478 * after a #GtkTextMark is deleted.
481 * gtk_text_buffer_delete_mark().
483 signals[MARK_DELETED] =
484 g_signal_new (I_("mark-deleted"),
485 G_OBJECT_CLASS_TYPE (object_class),
487 G_STRUCT_OFFSET (GtkTextBufferClass, mark_deleted),
489 _gtk_marshal_VOID__OBJECT,
495 * GtkTextBuffer::apply-tag:
496 * @textbuffer: the object which received the signal
497 * @tag: the applied tag
498 * @start: the start of the range the tag is applied to
499 * @end: the end of the range the tag is applied to
501 * The ::apply-tag signal is emitted to apply a tag to a
502 * range of text in a #GtkTextBuffer.
503 * Applying actually occurs in the default handler.
505 * Note that if your handler runs before the default handler it must not
506 * invalidate the @start and @end iters (or has to revalidate them).
509 * gtk_text_buffer_apply_tag(),
510 * gtk_text_buffer_insert_with_tags(),
511 * gtk_text_buffer_insert_range().
514 g_signal_new (I_("apply-tag"),
515 G_OBJECT_CLASS_TYPE (object_class),
517 G_STRUCT_OFFSET (GtkTextBufferClass, apply_tag),
519 _gtk_marshal_VOID__OBJECT_BOXED_BOXED,
528 * GtkTextBuffer::remove-tag:
529 * @textbuffer: the object which received the signal
530 * @tag: the tag to be removed
531 * @start: the start of the range the tag is removed from
532 * @end: the end of the range the tag is removed from
534 * The ::remove-tag signal is emitted to remove all occurrences of @tag from
535 * a range of text in a #GtkTextBuffer.
536 * Removal actually occurs in the default handler.
538 * Note that if your handler runs before the default handler it must not
539 * invalidate the @start and @end iters (or has to revalidate them).
542 * gtk_text_buffer_remove_tag().
544 signals[REMOVE_TAG] =
545 g_signal_new (I_("remove-tag"),
546 G_OBJECT_CLASS_TYPE (object_class),
548 G_STRUCT_OFFSET (GtkTextBufferClass, remove_tag),
550 _gtk_marshal_VOID__OBJECT_BOXED_BOXED,
558 * GtkTextBuffer::begin-user-action:
559 * @textbuffer: the object which received the signal
561 * The ::begin-user-action signal is emitted at the beginning of a single
562 * user-visible operation on a #GtkTextBuffer.
565 * gtk_text_buffer_begin_user_action(),
566 * gtk_text_buffer_insert_interactive(),
567 * gtk_text_buffer_insert_range_interactive(),
568 * gtk_text_buffer_delete_interactive(),
569 * gtk_text_buffer_backspace(),
570 * gtk_text_buffer_delete_selection().
572 signals[BEGIN_USER_ACTION] =
573 g_signal_new (I_("begin-user-action"),
574 G_OBJECT_CLASS_TYPE (object_class),
576 G_STRUCT_OFFSET (GtkTextBufferClass, begin_user_action),
578 _gtk_marshal_VOID__VOID,
583 * GtkTextBuffer::end-user-action:
584 * @textbuffer: the object which received the signal
586 * The ::end-user-action signal is emitted at the end of a single
587 * user-visible operation on the #GtkTextBuffer.
590 * gtk_text_buffer_end_user_action(),
591 * gtk_text_buffer_insert_interactive(),
592 * gtk_text_buffer_insert_range_interactive(),
593 * gtk_text_buffer_delete_interactive(),
594 * gtk_text_buffer_backspace(),
595 * gtk_text_buffer_delete_selection(),
596 * gtk_text_buffer_backspace().
598 signals[END_USER_ACTION] =
599 g_signal_new (I_("end-user-action"),
600 G_OBJECT_CLASS_TYPE (object_class),
602 G_STRUCT_OFFSET (GtkTextBufferClass, end_user_action),
604 _gtk_marshal_VOID__VOID,
609 * GtkTextBuffer::paste-done:
610 * @textbuffer: the object which received the signal
612 * The paste-done signal is emitted after paste operation has been completed.
613 * This is useful to properly scroll the view to the end of the pasted text.
614 * See gtk_text_buffer_paste_clipboard() for more details.
618 signals[PASTE_DONE] =
619 g_signal_new (I_("paste-done"),
620 G_OBJECT_CLASS_TYPE (object_class),
622 G_STRUCT_OFFSET (GtkTextBufferClass, paste_done),
624 _gtk_marshal_VOID__OBJECT,
629 g_type_class_add_private (object_class, sizeof (GtkTextBufferPrivate));
633 gtk_text_buffer_init (GtkTextBuffer *buffer)
635 buffer->priv = G_TYPE_INSTANCE_GET_PRIVATE (buffer,
636 GTK_TYPE_TEXT_BUFFER,
637 GtkTextBufferPrivate);
639 buffer->priv->clipboard_contents_buffers = NULL;
640 buffer->priv->tag_table = NULL;
642 /* allow copying of arbiatray stuff in the internal rich text format */
643 gtk_text_buffer_register_serialize_tagset (buffer, NULL);
647 set_table (GtkTextBuffer *buffer, GtkTextTagTable *table)
649 GtkTextBufferPrivate *priv = buffer->priv;
651 g_return_if_fail (priv->tag_table == NULL);
655 priv->tag_table = table;
656 g_object_ref (priv->tag_table);
657 _gtk_text_tag_table_add_buffer (table, buffer);
661 static GtkTextTagTable*
662 get_table (GtkTextBuffer *buffer)
664 GtkTextBufferPrivate *priv = buffer->priv;
666 if (priv->tag_table == NULL)
668 priv->tag_table = gtk_text_tag_table_new ();
669 _gtk_text_tag_table_add_buffer (priv->tag_table, buffer);
672 return priv->tag_table;
676 gtk_text_buffer_set_property (GObject *object,
681 GtkTextBuffer *text_buffer;
683 text_buffer = GTK_TEXT_BUFFER (object);
688 set_table (text_buffer, g_value_get_object (value));
692 gtk_text_buffer_set_text (text_buffer,
693 g_value_get_string (value), -1);
697 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
703 gtk_text_buffer_get_property (GObject *object,
708 GtkTextBuffer *text_buffer;
711 text_buffer = GTK_TEXT_BUFFER (object);
716 g_value_set_object (value, get_table (text_buffer));
721 GtkTextIter start, end;
723 gtk_text_buffer_get_start_iter (text_buffer, &start);
724 gtk_text_buffer_get_end_iter (text_buffer, &end);
726 g_value_take_string (value,
727 gtk_text_buffer_get_text (text_buffer,
728 &start, &end, FALSE));
732 case PROP_HAS_SELECTION:
733 g_value_set_boolean (value, text_buffer->priv->has_selection);
736 case PROP_CURSOR_POSITION:
737 gtk_text_buffer_get_iter_at_mark (text_buffer, &iter,
738 gtk_text_buffer_get_insert (text_buffer));
739 g_value_set_int (value, gtk_text_iter_get_offset (&iter));
742 case PROP_COPY_TARGET_LIST:
743 g_value_set_boxed (value, gtk_text_buffer_get_copy_target_list (text_buffer));
746 case PROP_PASTE_TARGET_LIST:
747 g_value_set_boxed (value, gtk_text_buffer_get_paste_target_list (text_buffer));
751 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
757 gtk_text_buffer_notify (GObject *object,
760 if (!strcmp (pspec->name, "copy-target-list") ||
761 !strcmp (pspec->name, "paste-target-list"))
763 gtk_text_buffer_free_target_lists (GTK_TEXT_BUFFER (object));
768 * gtk_text_buffer_new:
769 * @table: (allow-none): a tag table, or %NULL to create a new one
771 * Creates a new text buffer.
773 * Return value: a new text buffer
776 gtk_text_buffer_new (GtkTextTagTable *table)
778 GtkTextBuffer *text_buffer;
780 text_buffer = g_object_new (GTK_TYPE_TEXT_BUFFER, "tag-table", table, NULL);
786 gtk_text_buffer_finalize (GObject *object)
788 GtkTextBuffer *buffer;
789 GtkTextBufferPrivate *priv;
791 buffer = GTK_TEXT_BUFFER (object);
794 remove_all_selection_clipboards (buffer);
798 _gtk_text_tag_table_remove_buffer (priv->tag_table, buffer);
799 g_object_unref (priv->tag_table);
800 priv->tag_table = NULL;
805 _gtk_text_btree_unref (priv->btree);
809 if (priv->log_attr_cache)
810 free_log_attr_cache (priv->log_attr_cache);
812 priv->log_attr_cache = NULL;
814 gtk_text_buffer_free_target_lists (buffer);
816 G_OBJECT_CLASS (gtk_text_buffer_parent_class)->finalize (object);
820 get_btree (GtkTextBuffer *buffer)
822 GtkTextBufferPrivate *priv = buffer->priv;
824 if (priv->btree == NULL)
825 priv->btree = _gtk_text_btree_new (gtk_text_buffer_get_tag_table (buffer),
832 _gtk_text_buffer_get_btree (GtkTextBuffer *buffer)
834 return get_btree (buffer);
838 * gtk_text_buffer_get_tag_table:
839 * @buffer: a #GtkTextBuffer
841 * Get the #GtkTextTagTable associated with this buffer.
843 * Return value: (transfer none): the buffer's tag table
846 gtk_text_buffer_get_tag_table (GtkTextBuffer *buffer)
848 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
850 return get_table (buffer);
854 * gtk_text_buffer_set_text:
855 * @buffer: a #GtkTextBuffer
856 * @text: UTF-8 text to insert
857 * @len: length of @text in bytes
859 * Deletes current contents of @buffer, and inserts @text instead. If
860 * @len is -1, @text must be nul-terminated. @text must be valid UTF-8.
863 gtk_text_buffer_set_text (GtkTextBuffer *buffer,
867 GtkTextIter start, end;
869 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
870 g_return_if_fail (text != NULL);
875 gtk_text_buffer_get_bounds (buffer, &start, &end);
877 gtk_text_buffer_delete (buffer, &start, &end);
881 gtk_text_buffer_get_iter_at_offset (buffer, &start, 0);
882 gtk_text_buffer_insert (buffer, &start, text, len);
885 g_object_notify (G_OBJECT (buffer), "text");
895 gtk_text_buffer_real_insert_text (GtkTextBuffer *buffer,
900 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
901 g_return_if_fail (iter != NULL);
903 _gtk_text_btree_insert (iter, text, len);
905 g_signal_emit (buffer, signals[CHANGED], 0);
906 g_object_notify (G_OBJECT (buffer), "cursor-position");
910 gtk_text_buffer_emit_insert (GtkTextBuffer *buffer,
915 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
916 g_return_if_fail (iter != NULL);
917 g_return_if_fail (text != NULL);
922 g_return_if_fail (g_utf8_validate (text, len, NULL));
926 g_signal_emit (buffer, signals[INSERT_TEXT], 0,
932 * gtk_text_buffer_insert:
933 * @buffer: a #GtkTextBuffer
934 * @iter: a position in the buffer
935 * @text: text in UTF-8 format
936 * @len: length of text in bytes, or -1
938 * Inserts @len bytes of @text at position @iter. If @len is -1,
939 * @text must be nul-terminated and will be inserted in its
940 * entirety. Emits the "insert-text" signal; insertion actually occurs
941 * in the default handler for the signal. @iter is invalidated when
942 * insertion occurs (because the buffer contents change), but the
943 * default signal handler revalidates it to point to the end of the
947 gtk_text_buffer_insert (GtkTextBuffer *buffer,
952 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
953 g_return_if_fail (iter != NULL);
954 g_return_if_fail (text != NULL);
955 g_return_if_fail (gtk_text_iter_get_buffer (iter) == buffer);
957 gtk_text_buffer_emit_insert (buffer, iter, text, len);
961 * gtk_text_buffer_insert_at_cursor:
962 * @buffer: a #GtkTextBuffer
963 * @text: text in UTF-8 format
964 * @len: length of text, in bytes
966 * Simply calls gtk_text_buffer_insert(), using the current
967 * cursor position as the insertion point.
970 gtk_text_buffer_insert_at_cursor (GtkTextBuffer *buffer,
976 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
977 g_return_if_fail (text != NULL);
979 gtk_text_buffer_get_iter_at_mark (buffer, &iter,
980 gtk_text_buffer_get_insert (buffer));
982 gtk_text_buffer_insert (buffer, &iter, text, len);
986 * gtk_text_buffer_insert_interactive:
987 * @buffer: a #GtkTextBuffer
988 * @iter: a position in @buffer
989 * @text: some UTF-8 text
990 * @len: length of text in bytes, or -1
991 * @default_editable: default editability of buffer
993 * Like gtk_text_buffer_insert(), but the insertion will not occur if
994 * @iter is at a non-editable location in the buffer. Usually you
995 * want to prevent insertions at ineditable locations if the insertion
996 * results from a user action (is interactive).
998 * @default_editable indicates the editability of text that doesn't
999 * have a tag affecting editability applied to it. Typically the
1000 * result of gtk_text_view_get_editable() is appropriate here.
1002 * Return value: whether text was actually inserted
1005 gtk_text_buffer_insert_interactive (GtkTextBuffer *buffer,
1009 gboolean default_editable)
1011 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
1012 g_return_val_if_fail (text != NULL, FALSE);
1013 g_return_val_if_fail (gtk_text_iter_get_buffer (iter) == buffer, FALSE);
1015 if (gtk_text_iter_can_insert (iter, default_editable))
1017 gtk_text_buffer_begin_user_action (buffer);
1018 gtk_text_buffer_emit_insert (buffer, iter, text, len);
1019 gtk_text_buffer_end_user_action (buffer);
1027 * gtk_text_buffer_insert_interactive_at_cursor:
1028 * @buffer: a #GtkTextBuffer
1029 * @text: text in UTF-8 format
1030 * @len: length of text in bytes, or -1
1031 * @default_editable: default editability of buffer
1033 * Calls gtk_text_buffer_insert_interactive() at the cursor
1036 * @default_editable indicates the editability of text that doesn't
1037 * have a tag affecting editability applied to it. Typically the
1038 * result of gtk_text_view_get_editable() is appropriate here.
1040 * Return value: whether text was actually inserted
1043 gtk_text_buffer_insert_interactive_at_cursor (GtkTextBuffer *buffer,
1046 gboolean default_editable)
1050 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
1051 g_return_val_if_fail (text != NULL, FALSE);
1053 gtk_text_buffer_get_iter_at_mark (buffer, &iter,
1054 gtk_text_buffer_get_insert (buffer));
1056 return gtk_text_buffer_insert_interactive (buffer, &iter, text, len,
1061 possibly_not_text (gunichar ch,
1064 return ch == GTK_TEXT_UNKNOWN_CHAR;
1068 insert_text_range (GtkTextBuffer *buffer,
1070 const GtkTextIter *orig_start,
1071 const GtkTextIter *orig_end,
1072 gboolean interactive)
1076 text = gtk_text_iter_get_text (orig_start, orig_end);
1078 gtk_text_buffer_emit_insert (buffer, iter, text, -1);
1083 typedef struct _Range Range;
1086 GtkTextBuffer *buffer;
1087 GtkTextMark *start_mark;
1088 GtkTextMark *end_mark;
1089 GtkTextMark *whole_end_mark;
1090 GtkTextIter *range_start;
1091 GtkTextIter *range_end;
1092 GtkTextIter *whole_end;
1096 save_range (GtkTextIter *range_start,
1097 GtkTextIter *range_end,
1098 GtkTextIter *whole_end)
1102 r = g_new (Range, 1);
1104 r->buffer = gtk_text_iter_get_buffer (range_start);
1105 g_object_ref (r->buffer);
1108 gtk_text_buffer_create_mark (gtk_text_iter_get_buffer (range_start),
1113 gtk_text_buffer_create_mark (gtk_text_iter_get_buffer (range_start),
1119 gtk_text_buffer_create_mark (gtk_text_iter_get_buffer (range_start),
1124 r->range_start = range_start;
1125 r->range_end = range_end;
1126 r->whole_end = whole_end;
1132 restore_range (Range *r)
1134 gtk_text_buffer_get_iter_at_mark (r->buffer,
1138 gtk_text_buffer_get_iter_at_mark (r->buffer,
1142 gtk_text_buffer_get_iter_at_mark (r->buffer,
1146 gtk_text_buffer_delete_mark (r->buffer, r->start_mark);
1147 gtk_text_buffer_delete_mark (r->buffer, r->end_mark);
1148 gtk_text_buffer_delete_mark (r->buffer, r->whole_end_mark);
1150 /* Due to the gravities on the marks, the ordering could have
1151 * gotten mangled; we switch to an empty range in that
1155 if (gtk_text_iter_compare (r->range_start, r->range_end) > 0)
1156 *r->range_start = *r->range_end;
1158 if (gtk_text_iter_compare (r->range_end, r->whole_end) > 0)
1159 *r->range_end = *r->whole_end;
1161 g_object_unref (r->buffer);
1166 insert_range_untagged (GtkTextBuffer *buffer,
1168 const GtkTextIter *orig_start,
1169 const GtkTextIter *orig_end,
1170 gboolean interactive)
1172 GtkTextIter range_start;
1173 GtkTextIter range_end;
1174 GtkTextIter start, end;
1177 if (gtk_text_iter_equal (orig_start, orig_end))
1180 start = *orig_start;
1183 range_start = start;
1188 if (gtk_text_iter_equal (&range_start, &range_end))
1190 /* Figure out how to move forward */
1192 g_assert (gtk_text_iter_compare (&range_end, &end) <= 0);
1194 if (gtk_text_iter_equal (&range_end, &end))
1196 /* nothing left to do */
1199 else if (gtk_text_iter_get_char (&range_end) == GTK_TEXT_UNKNOWN_CHAR)
1201 GdkPixbuf *pixbuf = NULL;
1202 GtkTextChildAnchor *anchor = NULL;
1203 pixbuf = gtk_text_iter_get_pixbuf (&range_end);
1204 anchor = gtk_text_iter_get_child_anchor (&range_end);
1208 r = save_range (&range_start,
1212 gtk_text_buffer_insert_pixbuf (buffer,
1219 gtk_text_iter_forward_char (&range_end);
1221 range_start = range_end;
1225 /* Just skip anchors */
1227 gtk_text_iter_forward_char (&range_end);
1228 range_start = range_end;
1232 /* The GTK_TEXT_UNKNOWN_CHAR was in a text segment, so
1235 gtk_text_iter_forward_find_char (&range_end,
1236 possibly_not_text, NULL,
1239 g_assert (gtk_text_iter_compare (&range_end, &end) <= 0);
1244 /* Text segment starts here, so forward search to
1245 * find its possible endpoint
1247 gtk_text_iter_forward_find_char (&range_end,
1248 possibly_not_text, NULL,
1251 g_assert (gtk_text_iter_compare (&range_end, &end) <= 0);
1256 r = save_range (&range_start,
1260 insert_text_range (buffer,
1269 range_start = range_end;
1275 insert_range_not_inside_self (GtkTextBuffer *buffer,
1277 const GtkTextIter *orig_start,
1278 const GtkTextIter *orig_end,
1279 gboolean interactive)
1281 /* Find each range of uniformly-tagged text, insert it,
1282 * then apply the tags.
1284 GtkTextIter start = *orig_start;
1285 GtkTextIter end = *orig_end;
1286 GtkTextIter range_start;
1287 GtkTextIter range_end;
1289 if (gtk_text_iter_equal (orig_start, orig_end))
1292 gtk_text_iter_order (&start, &end);
1294 range_start = start;
1300 GtkTextIter start_iter;
1305 if (gtk_text_iter_equal (&range_start, &end))
1306 break; /* All done */
1308 g_assert (gtk_text_iter_compare (&range_start, &end) < 0);
1310 gtk_text_iter_forward_to_tag_toggle (&range_end, NULL);
1312 g_assert (!gtk_text_iter_equal (&range_start, &range_end));
1314 /* Clamp to the end iterator */
1315 if (gtk_text_iter_compare (&range_end, &end) > 0)
1318 /* We have a range with unique tags; insert it, and
1321 start_offset = gtk_text_iter_get_offset (iter);
1323 r = save_range (&range_start, &range_end, &end);
1325 insert_range_untagged (buffer, iter, &range_start, &range_end, interactive);
1330 gtk_text_buffer_get_iter_at_offset (buffer, &start_iter, start_offset);
1332 tags = gtk_text_iter_get_tags (&range_start);
1334 while (tmp_list != NULL)
1336 gtk_text_buffer_apply_tag (buffer,
1341 tmp_list = g_slist_next (tmp_list);
1343 g_slist_free (tags);
1345 range_start = range_end;
1350 gtk_text_buffer_real_insert_range (GtkTextBuffer *buffer,
1352 const GtkTextIter *orig_start,
1353 const GtkTextIter *orig_end,
1354 gboolean interactive)
1356 GtkTextBuffer *src_buffer;
1358 /* Find each range of uniformly-tagged text, insert it,
1359 * then apply the tags.
1361 if (gtk_text_iter_equal (orig_start, orig_end))
1365 gtk_text_buffer_begin_user_action (buffer);
1367 src_buffer = gtk_text_iter_get_buffer (orig_start);
1369 if (gtk_text_iter_get_buffer (iter) != src_buffer ||
1370 !gtk_text_iter_in_range (iter, orig_start, orig_end))
1372 insert_range_not_inside_self (buffer, iter, orig_start, orig_end, interactive);
1376 /* If you insert a range into itself, it could loop infinitely
1377 * because the region being copied keeps growing as we insert. So
1378 * we have to separately copy the range before and after
1379 * the insertion point.
1381 GtkTextIter start = *orig_start;
1382 GtkTextIter end = *orig_end;
1383 GtkTextIter range_start;
1384 GtkTextIter range_end;
1388 gtk_text_iter_order (&start, &end);
1390 range_start = start;
1392 first_half = save_range (&range_start, &range_end, &end);
1394 range_start = *iter;
1396 second_half = save_range (&range_start, &range_end, &end);
1398 restore_range (first_half);
1399 insert_range_not_inside_self (buffer, iter, &range_start, &range_end, interactive);
1401 restore_range (second_half);
1402 insert_range_not_inside_self (buffer, iter, &range_start, &range_end, interactive);
1406 gtk_text_buffer_end_user_action (buffer);
1410 * gtk_text_buffer_insert_range:
1411 * @buffer: a #GtkTextBuffer
1412 * @iter: a position in @buffer
1413 * @start: a position in a #GtkTextBuffer
1414 * @end: another position in the same buffer as @start
1416 * Copies text, tags, and pixbufs between @start and @end (the order
1417 * of @start and @end doesn't matter) and inserts the copy at @iter.
1418 * Used instead of simply getting/inserting text because it preserves
1419 * images and tags. If @start and @end are in a different buffer from
1420 * @buffer, the two buffers must share the same tag table.
1422 * Implemented via emissions of the insert_text and apply_tag signals,
1426 gtk_text_buffer_insert_range (GtkTextBuffer *buffer,
1428 const GtkTextIter *start,
1429 const GtkTextIter *end)
1431 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1432 g_return_if_fail (iter != NULL);
1433 g_return_if_fail (start != NULL);
1434 g_return_if_fail (end != NULL);
1435 g_return_if_fail (gtk_text_iter_get_buffer (start) ==
1436 gtk_text_iter_get_buffer (end));
1437 g_return_if_fail (gtk_text_iter_get_buffer (start)->priv->tag_table ==
1438 buffer->priv->tag_table);
1439 g_return_if_fail (gtk_text_iter_get_buffer (iter) == buffer);
1441 gtk_text_buffer_real_insert_range (buffer, iter, start, end, FALSE);
1445 * gtk_text_buffer_insert_range_interactive:
1446 * @buffer: a #GtkTextBuffer
1447 * @iter: a position in @buffer
1448 * @start: a position in a #GtkTextBuffer
1449 * @end: another position in the same buffer as @start
1450 * @default_editable: default editability of the buffer
1452 * Same as gtk_text_buffer_insert_range(), but does nothing if the
1453 * insertion point isn't editable. The @default_editable parameter
1454 * indicates whether the text is editable at @iter if no tags
1455 * enclosing @iter affect editability. Typically the result of
1456 * gtk_text_view_get_editable() is appropriate here.
1458 * Returns: whether an insertion was possible at @iter
1461 gtk_text_buffer_insert_range_interactive (GtkTextBuffer *buffer,
1463 const GtkTextIter *start,
1464 const GtkTextIter *end,
1465 gboolean default_editable)
1467 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
1468 g_return_val_if_fail (iter != NULL, FALSE);
1469 g_return_val_if_fail (start != NULL, FALSE);
1470 g_return_val_if_fail (end != NULL, FALSE);
1471 g_return_val_if_fail (gtk_text_iter_get_buffer (start) ==
1472 gtk_text_iter_get_buffer (end), FALSE);
1473 g_return_val_if_fail (gtk_text_iter_get_buffer (start)->priv->tag_table ==
1474 buffer->priv->tag_table, FALSE);
1476 if (gtk_text_iter_can_insert (iter, default_editable))
1478 gtk_text_buffer_real_insert_range (buffer, iter, start, end, TRUE);
1486 * gtk_text_buffer_insert_with_tags:
1487 * @buffer: a #GtkTextBuffer
1488 * @iter: an iterator in @buffer
1490 * @len: length of @text, or -1
1491 * @first_tag: first tag to apply to @text
1492 * @...: %NULL-terminated list of tags to apply
1494 * Inserts @text into @buffer at @iter, applying the list of tags to
1495 * the newly-inserted text. The last tag specified must be %NULL to
1496 * terminate the list. Equivalent to calling gtk_text_buffer_insert(),
1497 * then gtk_text_buffer_apply_tag() on the inserted text;
1498 * gtk_text_buffer_insert_with_tags() is just a convenience function.
1501 gtk_text_buffer_insert_with_tags (GtkTextBuffer *buffer,
1505 GtkTextTag *first_tag,
1513 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1514 g_return_if_fail (iter != NULL);
1515 g_return_if_fail (text != NULL);
1516 g_return_if_fail (gtk_text_iter_get_buffer (iter) == buffer);
1518 start_offset = gtk_text_iter_get_offset (iter);
1520 gtk_text_buffer_insert (buffer, iter, text, len);
1522 if (first_tag == NULL)
1525 gtk_text_buffer_get_iter_at_offset (buffer, &start, start_offset);
1527 va_start (args, first_tag);
1531 gtk_text_buffer_apply_tag (buffer, tag, &start, iter);
1533 tag = va_arg (args, GtkTextTag*);
1540 * gtk_text_buffer_insert_with_tags_by_name:
1541 * @buffer: a #GtkTextBuffer
1542 * @iter: position in @buffer
1544 * @len: length of @text, or -1
1545 * @first_tag_name: name of a tag to apply to @text
1546 * @...: more tag names
1548 * Same as gtk_text_buffer_insert_with_tags(), but allows you
1549 * to pass in tag names instead of tag objects.
1552 gtk_text_buffer_insert_with_tags_by_name (GtkTextBuffer *buffer,
1556 const gchar *first_tag_name,
1562 const gchar *tag_name;
1564 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1565 g_return_if_fail (iter != NULL);
1566 g_return_if_fail (text != NULL);
1567 g_return_if_fail (gtk_text_iter_get_buffer (iter) == buffer);
1569 start_offset = gtk_text_iter_get_offset (iter);
1571 gtk_text_buffer_insert (buffer, iter, text, len);
1573 if (first_tag_name == NULL)
1576 gtk_text_buffer_get_iter_at_offset (buffer, &start, start_offset);
1578 va_start (args, first_tag_name);
1579 tag_name = first_tag_name;
1584 tag = gtk_text_tag_table_lookup (buffer->priv->tag_table,
1589 g_warning ("%s: no tag with name '%s'!", G_STRLOC, tag_name);
1594 gtk_text_buffer_apply_tag (buffer, tag, &start, iter);
1596 tag_name = va_arg (args, const gchar*);
1608 gtk_text_buffer_real_delete_range (GtkTextBuffer *buffer,
1612 gboolean has_selection;
1614 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1615 g_return_if_fail (start != NULL);
1616 g_return_if_fail (end != NULL);
1618 _gtk_text_btree_delete (start, end);
1620 /* may have deleted the selection... */
1621 update_selection_clipboards (buffer);
1623 has_selection = gtk_text_buffer_get_selection_bounds (buffer, NULL, NULL);
1624 if (has_selection != buffer->priv->has_selection)
1626 buffer->priv->has_selection = has_selection;
1627 g_object_notify (G_OBJECT (buffer), "has-selection");
1630 g_signal_emit (buffer, signals[CHANGED], 0);
1631 g_object_notify (G_OBJECT (buffer), "cursor-position");
1635 gtk_text_buffer_emit_delete (GtkTextBuffer *buffer,
1639 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1640 g_return_if_fail (start != NULL);
1641 g_return_if_fail (end != NULL);
1643 if (gtk_text_iter_equal (start, end))
1646 gtk_text_iter_order (start, end);
1648 g_signal_emit (buffer,
1649 signals[DELETE_RANGE],
1655 * gtk_text_buffer_delete:
1656 * @buffer: a #GtkTextBuffer
1657 * @start: a position in @buffer
1658 * @end: another position in @buffer
1660 * Deletes text between @start and @end. The order of @start and @end
1661 * is not actually relevant; gtk_text_buffer_delete() will reorder
1662 * them. This function actually emits the "delete-range" signal, and
1663 * the default handler of that signal deletes the text. Because the
1664 * buffer is modified, all outstanding iterators become invalid after
1665 * calling this function; however, the @start and @end will be
1666 * re-initialized to point to the location where text was deleted.
1669 gtk_text_buffer_delete (GtkTextBuffer *buffer,
1673 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1674 g_return_if_fail (start != NULL);
1675 g_return_if_fail (end != NULL);
1676 g_return_if_fail (gtk_text_iter_get_buffer (start) == buffer);
1677 g_return_if_fail (gtk_text_iter_get_buffer (end) == buffer);
1679 gtk_text_buffer_emit_delete (buffer, start, end);
1683 * gtk_text_buffer_delete_interactive:
1684 * @buffer: a #GtkTextBuffer
1685 * @start_iter: start of range to delete
1686 * @end_iter: end of range
1687 * @default_editable: whether the buffer is editable by default
1689 * Deletes all <emphasis>editable</emphasis> text in the given range.
1690 * Calls gtk_text_buffer_delete() for each editable sub-range of
1691 * [@start,@end). @start and @end are revalidated to point to
1692 * the location of the last deleted range, or left untouched if
1693 * no text was deleted.
1695 * Return value: whether some text was actually deleted
1698 gtk_text_buffer_delete_interactive (GtkTextBuffer *buffer,
1699 GtkTextIter *start_iter,
1700 GtkTextIter *end_iter,
1701 gboolean default_editable)
1703 GtkTextMark *end_mark;
1704 GtkTextMark *start_mark;
1706 gboolean current_state;
1707 gboolean deleted_stuff = FALSE;
1709 /* Delete all editable text in the range start_iter, end_iter */
1711 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
1712 g_return_val_if_fail (start_iter != NULL, FALSE);
1713 g_return_val_if_fail (end_iter != NULL, FALSE);
1714 g_return_val_if_fail (gtk_text_iter_get_buffer (start_iter) == buffer, FALSE);
1715 g_return_val_if_fail (gtk_text_iter_get_buffer (end_iter) == buffer, FALSE);
1718 gtk_text_buffer_begin_user_action (buffer);
1720 gtk_text_iter_order (start_iter, end_iter);
1722 start_mark = gtk_text_buffer_create_mark (buffer, NULL,
1724 end_mark = gtk_text_buffer_create_mark (buffer, NULL,
1727 gtk_text_buffer_get_iter_at_mark (buffer, &iter, start_mark);
1729 current_state = gtk_text_iter_editable (&iter, default_editable);
1734 gboolean done = FALSE;
1737 gtk_text_iter_forward_to_tag_toggle (&iter, NULL);
1739 gtk_text_buffer_get_iter_at_mark (buffer, &end, end_mark);
1741 if (gtk_text_iter_compare (&iter, &end) >= 0)
1744 iter = end; /* clamp to the last boundary */
1747 new_state = gtk_text_iter_editable (&iter, default_editable);
1749 if (current_state == new_state)
1755 /* We're ending an editable region. Delete said region. */
1758 gtk_text_buffer_get_iter_at_mark (buffer, &start, start_mark);
1760 gtk_text_buffer_emit_delete (buffer, &start, &iter);
1762 deleted_stuff = TRUE;
1764 /* revalidate user's iterators. */
1765 *start_iter = start;
1775 if (current_state && !new_state)
1777 /* End of an editable region. Delete it. */
1780 gtk_text_buffer_get_iter_at_mark (buffer, &start, start_mark);
1782 gtk_text_buffer_emit_delete (buffer, &start, &iter);
1784 /* It's more robust to ask for the state again then to assume that
1785 * we're on the next not-editable segment. We don't know what the
1786 * ::delete-range handler did.... maybe it deleted the following
1787 * not-editable segment because it was associated with the editable
1790 current_state = gtk_text_iter_editable (&iter, default_editable);
1791 deleted_stuff = TRUE;
1793 /* revalidate user's iterators. */
1794 *start_iter = start;
1799 /* We are at the start of an editable region. We won't be deleting
1800 * the previous region. Move start mark to start of this region.
1803 g_assert (!current_state && new_state);
1805 gtk_text_buffer_move_mark (buffer, start_mark, &iter);
1807 current_state = TRUE;
1814 gtk_text_buffer_delete_mark (buffer, start_mark);
1815 gtk_text_buffer_delete_mark (buffer, end_mark);
1817 gtk_text_buffer_end_user_action (buffer);
1819 return deleted_stuff;
1823 * Extracting textual buffer contents
1827 * gtk_text_buffer_get_text:
1828 * @buffer: a #GtkTextBuffer
1829 * @start: start of a range
1830 * @end: end of a range
1831 * @include_hidden_chars: whether to include invisible text
1833 * Returns the text in the range [@start,@end). Excludes undisplayed
1834 * text (text marked with tags that set the invisibility attribute) if
1835 * @include_hidden_chars is %FALSE. Does not include characters
1836 * representing embedded images, so byte and character indexes into
1837 * the returned string do <emphasis>not</emphasis> correspond to byte
1838 * and character indexes into the buffer. Contrast with
1839 * gtk_text_buffer_get_slice().
1841 * Return value: an allocated UTF-8 string
1844 gtk_text_buffer_get_text (GtkTextBuffer *buffer,
1845 const GtkTextIter *start,
1846 const GtkTextIter *end,
1847 gboolean include_hidden_chars)
1849 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
1850 g_return_val_if_fail (start != NULL, NULL);
1851 g_return_val_if_fail (end != NULL, NULL);
1852 g_return_val_if_fail (gtk_text_iter_get_buffer (start) == buffer, NULL);
1853 g_return_val_if_fail (gtk_text_iter_get_buffer (end) == buffer, NULL);
1855 if (include_hidden_chars)
1856 return gtk_text_iter_get_text (start, end);
1858 return gtk_text_iter_get_visible_text (start, end);
1862 * gtk_text_buffer_get_slice:
1863 * @buffer: a #GtkTextBuffer
1864 * @start: start of a range
1865 * @end: end of a range
1866 * @include_hidden_chars: whether to include invisible text
1868 * Returns the text in the range [@start,@end). Excludes undisplayed
1869 * text (text marked with tags that set the invisibility attribute) if
1870 * @include_hidden_chars is %FALSE. The returned string includes a
1871 * 0xFFFC character whenever the buffer contains
1872 * embedded images, so byte and character indexes into
1873 * the returned string <emphasis>do</emphasis> correspond to byte
1874 * and character indexes into the buffer. Contrast with
1875 * gtk_text_buffer_get_text(). Note that 0xFFFC can occur in normal
1876 * text as well, so it is not a reliable indicator that a pixbuf or
1877 * widget is in the buffer.
1879 * Return value: an allocated UTF-8 string
1882 gtk_text_buffer_get_slice (GtkTextBuffer *buffer,
1883 const GtkTextIter *start,
1884 const GtkTextIter *end,
1885 gboolean include_hidden_chars)
1887 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
1888 g_return_val_if_fail (start != NULL, NULL);
1889 g_return_val_if_fail (end != NULL, NULL);
1890 g_return_val_if_fail (gtk_text_iter_get_buffer (start) == buffer, NULL);
1891 g_return_val_if_fail (gtk_text_iter_get_buffer (end) == buffer, NULL);
1893 if (include_hidden_chars)
1894 return gtk_text_iter_get_slice (start, end);
1896 return gtk_text_iter_get_visible_slice (start, end);
1904 gtk_text_buffer_real_insert_pixbuf (GtkTextBuffer *buffer,
1908 _gtk_text_btree_insert_pixbuf (iter, pixbuf);
1910 g_signal_emit (buffer, signals[CHANGED], 0);
1914 * gtk_text_buffer_insert_pixbuf:
1915 * @buffer: a #GtkTextBuffer
1916 * @iter: location to insert the pixbuf
1917 * @pixbuf: a #GdkPixbuf
1919 * Inserts an image into the text buffer at @iter. The image will be
1920 * counted as one character in character counts, and when obtaining
1921 * the buffer contents as a string, will be represented by the Unicode
1922 * "object replacement character" 0xFFFC. Note that the "slice"
1923 * variants for obtaining portions of the buffer as a string include
1924 * this character for pixbufs, but the "text" variants do
1925 * not. e.g. see gtk_text_buffer_get_slice() and
1926 * gtk_text_buffer_get_text().
1929 gtk_text_buffer_insert_pixbuf (GtkTextBuffer *buffer,
1933 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1934 g_return_if_fail (iter != NULL);
1935 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
1936 g_return_if_fail (gtk_text_iter_get_buffer (iter) == buffer);
1938 g_signal_emit (buffer, signals[INSERT_PIXBUF], 0,
1948 gtk_text_buffer_real_insert_anchor (GtkTextBuffer *buffer,
1950 GtkTextChildAnchor *anchor)
1952 _gtk_text_btree_insert_child_anchor (iter, anchor);
1954 g_signal_emit (buffer, signals[CHANGED], 0);
1958 * gtk_text_buffer_insert_child_anchor:
1959 * @buffer: a #GtkTextBuffer
1960 * @iter: location to insert the anchor
1961 * @anchor: a #GtkTextChildAnchor
1963 * Inserts a child widget anchor into the text buffer at @iter. The
1964 * anchor will be counted as one character in character counts, and
1965 * when obtaining the buffer contents as a string, will be represented
1966 * by the Unicode "object replacement character" 0xFFFC. Note that the
1967 * "slice" variants for obtaining portions of the buffer as a string
1968 * include this character for child anchors, but the "text" variants do
1969 * not. E.g. see gtk_text_buffer_get_slice() and
1970 * gtk_text_buffer_get_text(). Consider
1971 * gtk_text_buffer_create_child_anchor() as a more convenient
1972 * alternative to this function. The buffer will add a reference to
1973 * the anchor, so you can unref it after insertion.
1976 gtk_text_buffer_insert_child_anchor (GtkTextBuffer *buffer,
1978 GtkTextChildAnchor *anchor)
1980 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1981 g_return_if_fail (iter != NULL);
1982 g_return_if_fail (GTK_IS_TEXT_CHILD_ANCHOR (anchor));
1983 g_return_if_fail (gtk_text_iter_get_buffer (iter) == buffer);
1985 g_signal_emit (buffer, signals[INSERT_CHILD_ANCHOR], 0,
1990 * gtk_text_buffer_create_child_anchor:
1991 * @buffer: a #GtkTextBuffer
1992 * @iter: location in the buffer
1994 * This is a convenience function which simply creates a child anchor
1995 * with gtk_text_child_anchor_new() and inserts it into the buffer
1996 * with gtk_text_buffer_insert_child_anchor(). The new anchor is
1997 * owned by the buffer; no reference count is returned to
1998 * the caller of gtk_text_buffer_create_child_anchor().
2000 * Return value: (transfer none): the created child anchor
2003 gtk_text_buffer_create_child_anchor (GtkTextBuffer *buffer,
2006 GtkTextChildAnchor *anchor;
2008 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
2009 g_return_val_if_fail (iter != NULL, NULL);
2010 g_return_val_if_fail (gtk_text_iter_get_buffer (iter) == buffer, NULL);
2012 anchor = gtk_text_child_anchor_new ();
2014 gtk_text_buffer_insert_child_anchor (buffer, iter, anchor);
2016 g_object_unref (anchor);
2026 gtk_text_buffer_mark_set (GtkTextBuffer *buffer,
2027 const GtkTextIter *location,
2030 /* IMO this should NOT work like insert_text and delete_range,
2031 * where the real action happens in the default handler.
2033 * The reason is that the default handler would be _required_,
2034 * i.e. the whole widget would start breaking and segfaulting if the
2035 * default handler didn't get run. So you can't really override the
2036 * default handler or stop the emission; that is, this signal is
2037 * purely for notification, and not to allow users to modify the
2041 g_object_ref (mark);
2043 g_signal_emit (buffer,
2049 g_object_unref (mark);
2053 * gtk_text_buffer_set_mark:
2054 * @buffer: a #GtkTextBuffer
2055 * @mark_name: name of the mark
2056 * @iter: location for the mark
2057 * @left_gravity: if the mark is created by this function, gravity for
2059 * @should_exist: if %TRUE, warn if the mark does not exist, and return
2062 * Move the mark to the given position, if not @should_exist,
2065 * Return value: mark
2068 gtk_text_buffer_set_mark (GtkTextBuffer *buffer,
2069 GtkTextMark *existing_mark,
2070 const gchar *mark_name,
2071 const GtkTextIter *iter,
2072 gboolean left_gravity,
2073 gboolean should_exist)
2075 GtkTextIter location;
2078 g_return_val_if_fail (gtk_text_iter_get_buffer (iter) == buffer, NULL);
2080 mark = _gtk_text_btree_set_mark (get_btree (buffer),
2087 _gtk_text_btree_get_iter_at_mark (get_btree (buffer),
2091 gtk_text_buffer_mark_set (buffer, &location, mark);
2097 * gtk_text_buffer_create_mark:
2098 * @buffer: a #GtkTextBuffer
2099 * @mark_name: (allow-none): name for mark, or %NULL
2100 * @where: location to place mark
2101 * @left_gravity: whether the mark has left gravity
2103 * Creates a mark at position @where. If @mark_name is %NULL, the mark
2104 * is anonymous; otherwise, the mark can be retrieved by name using
2105 * gtk_text_buffer_get_mark(). If a mark has left gravity, and text is
2106 * inserted at the mark's current location, the mark will be moved to
2107 * the left of the newly-inserted text. If the mark has right gravity
2108 * (@left_gravity = %FALSE), the mark will end up on the right of
2109 * newly-inserted text. The standard left-to-right cursor is a mark
2110 * with right gravity (when you type, the cursor stays on the right
2111 * side of the text you're typing).
2113 * The caller of this function does <emphasis>not</emphasis> own a
2114 * reference to the returned #GtkTextMark, so you can ignore the
2115 * return value if you like. Marks are owned by the buffer and go
2116 * away when the buffer does.
2118 * Emits the "mark-set" signal as notification of the mark's initial
2121 * Return value: (transfer none): the new #GtkTextMark object
2124 gtk_text_buffer_create_mark (GtkTextBuffer *buffer,
2125 const gchar *mark_name,
2126 const GtkTextIter *where,
2127 gboolean left_gravity)
2129 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
2131 return gtk_text_buffer_set_mark (buffer, NULL, mark_name, where,
2132 left_gravity, FALSE);
2136 * gtk_text_buffer_add_mark:
2137 * @buffer: a #GtkTextBuffer
2138 * @mark: the mark to add
2139 * @where: location to place mark
2141 * Adds the mark at position @where. The mark must not be added to
2142 * another buffer, and if its name is not %NULL then there must not
2143 * be another mark in the buffer with the same name.
2145 * Emits the "mark-set" signal as notification of the mark's initial
2151 gtk_text_buffer_add_mark (GtkTextBuffer *buffer,
2153 const GtkTextIter *where)
2157 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2158 g_return_if_fail (GTK_IS_TEXT_MARK (mark));
2159 g_return_if_fail (where != NULL);
2160 g_return_if_fail (gtk_text_mark_get_buffer (mark) == NULL);
2162 name = gtk_text_mark_get_name (mark);
2164 if (name != NULL && gtk_text_buffer_get_mark (buffer, name) != NULL)
2166 g_critical ("Mark %s already exists in the buffer", name);
2170 gtk_text_buffer_set_mark (buffer, mark, NULL, where, FALSE, FALSE);
2174 * gtk_text_buffer_move_mark:
2175 * @buffer: a #GtkTextBuffer
2176 * @mark: a #GtkTextMark
2177 * @where: new location for @mark in @buffer
2179 * Moves @mark to the new location @where. Emits the "mark-set" signal
2180 * as notification of the move.
2183 gtk_text_buffer_move_mark (GtkTextBuffer *buffer,
2185 const GtkTextIter *where)
2187 g_return_if_fail (GTK_IS_TEXT_MARK (mark));
2188 g_return_if_fail (!gtk_text_mark_get_deleted (mark));
2189 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2191 gtk_text_buffer_set_mark (buffer, mark, NULL, where, FALSE, TRUE);
2195 * gtk_text_buffer_get_iter_at_mark:
2196 * @buffer: a #GtkTextBuffer
2197 * @iter: (out): iterator to initialize
2198 * @mark: a #GtkTextMark in @buffer
2200 * Initializes @iter with the current position of @mark.
2203 gtk_text_buffer_get_iter_at_mark (GtkTextBuffer *buffer,
2207 g_return_if_fail (GTK_IS_TEXT_MARK (mark));
2208 g_return_if_fail (!gtk_text_mark_get_deleted (mark));
2209 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2211 _gtk_text_btree_get_iter_at_mark (get_btree (buffer),
2217 * gtk_text_buffer_delete_mark:
2218 * @buffer: a #GtkTextBuffer
2219 * @mark: a #GtkTextMark in @buffer
2221 * Deletes @mark, so that it's no longer located anywhere in the
2222 * buffer. Removes the reference the buffer holds to the mark, so if
2223 * you haven't called g_object_ref() on the mark, it will be freed. Even
2224 * if the mark isn't freed, most operations on @mark become
2225 * invalid, until it gets added to a buffer again with
2226 * gtk_text_buffer_add_mark(). Use gtk_text_mark_get_deleted() to
2227 * find out if a mark has been removed from its buffer.
2228 * The "mark-deleted" signal will be emitted as notification after
2229 * the mark is deleted.
2232 gtk_text_buffer_delete_mark (GtkTextBuffer *buffer,
2235 g_return_if_fail (GTK_IS_TEXT_MARK (mark));
2236 g_return_if_fail (!gtk_text_mark_get_deleted (mark));
2237 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2239 g_object_ref (mark);
2241 _gtk_text_btree_remove_mark (get_btree (buffer), mark);
2243 /* See rationale above for MARK_SET on why we emit this after
2244 * removing the mark, rather than removing the mark in a default
2247 g_signal_emit (buffer, signals[MARK_DELETED],
2251 g_object_unref (mark);
2255 * gtk_text_buffer_get_mark:
2256 * @buffer: a #GtkTextBuffer
2257 * @name: a mark name
2259 * Returns the mark named @name in buffer @buffer, or %NULL if no such
2260 * mark exists in the buffer.
2262 * Return value: (transfer none): a #GtkTextMark, or %NULL
2265 gtk_text_buffer_get_mark (GtkTextBuffer *buffer,
2270 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
2271 g_return_val_if_fail (name != NULL, NULL);
2273 mark = _gtk_text_btree_get_mark_by_name (get_btree (buffer), name);
2279 * gtk_text_buffer_move_mark_by_name:
2280 * @buffer: a #GtkTextBuffer
2281 * @name: name of a mark
2282 * @where: new location for mark
2284 * Moves the mark named @name (which must exist) to location @where.
2285 * See gtk_text_buffer_move_mark() for details.
2288 gtk_text_buffer_move_mark_by_name (GtkTextBuffer *buffer,
2290 const GtkTextIter *where)
2294 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2295 g_return_if_fail (name != NULL);
2297 mark = _gtk_text_btree_get_mark_by_name (get_btree (buffer), name);
2301 g_warning ("%s: no mark named '%s'", G_STRLOC, name);
2305 gtk_text_buffer_move_mark (buffer, mark, where);
2309 * gtk_text_buffer_delete_mark_by_name:
2310 * @buffer: a #GtkTextBuffer
2311 * @name: name of a mark in @buffer
2313 * Deletes the mark named @name; the mark must exist. See
2314 * gtk_text_buffer_delete_mark() for details.
2317 gtk_text_buffer_delete_mark_by_name (GtkTextBuffer *buffer,
2322 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2323 g_return_if_fail (name != NULL);
2325 mark = _gtk_text_btree_get_mark_by_name (get_btree (buffer), name);
2329 g_warning ("%s: no mark named '%s'", G_STRLOC, name);
2333 gtk_text_buffer_delete_mark (buffer, mark);
2337 * gtk_text_buffer_get_insert:
2338 * @buffer: a #GtkTextBuffer
2340 * Returns the mark that represents the cursor (insertion point).
2341 * Equivalent to calling gtk_text_buffer_get_mark() to get the mark
2342 * named "insert", but very slightly more efficient, and involves less
2345 * Return value: (transfer none): insertion point mark
2348 gtk_text_buffer_get_insert (GtkTextBuffer *buffer)
2350 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
2352 return _gtk_text_btree_get_insert (get_btree (buffer));
2356 * gtk_text_buffer_get_selection_bound:
2357 * @buffer: a #GtkTextBuffer
2359 * Returns the mark that represents the selection bound. Equivalent
2360 * to calling gtk_text_buffer_get_mark() to get the mark named
2361 * "selection_bound", but very slightly more efficient, and involves
2364 * The currently-selected text in @buffer is the region between the
2365 * "selection_bound" and "insert" marks. If "selection_bound" and
2366 * "insert" are in the same place, then there is no current selection.
2367 * gtk_text_buffer_get_selection_bounds() is another convenient function
2368 * for handling the selection, if you just want to know whether there's a
2369 * selection and what its bounds are.
2371 * Return value: (transfer none): selection bound mark
2374 gtk_text_buffer_get_selection_bound (GtkTextBuffer *buffer)
2376 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
2378 return _gtk_text_btree_get_selection_bound (get_btree (buffer));
2382 * gtk_text_buffer_get_iter_at_child_anchor:
2383 * @buffer: a #GtkTextBuffer
2384 * @iter: (out): an iterator to be initialized
2385 * @anchor: a child anchor that appears in @buffer
2387 * Obtains the location of @anchor within @buffer.
2390 gtk_text_buffer_get_iter_at_child_anchor (GtkTextBuffer *buffer,
2392 GtkTextChildAnchor *anchor)
2394 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2395 g_return_if_fail (iter != NULL);
2396 g_return_if_fail (GTK_IS_TEXT_CHILD_ANCHOR (anchor));
2397 g_return_if_fail (!gtk_text_child_anchor_get_deleted (anchor));
2399 _gtk_text_btree_get_iter_at_child_anchor (get_btree (buffer),
2405 * gtk_text_buffer_place_cursor:
2406 * @buffer: a #GtkTextBuffer
2407 * @where: where to put the cursor
2409 * This function moves the "insert" and "selection_bound" marks
2410 * simultaneously. If you move them to the same place in two steps
2411 * with gtk_text_buffer_move_mark(), you will temporarily select a
2412 * region in between their old and new locations, which can be pretty
2413 * inefficient since the temporarily-selected region will force stuff
2414 * to be recalculated. This function moves them as a unit, which can
2418 gtk_text_buffer_place_cursor (GtkTextBuffer *buffer,
2419 const GtkTextIter *where)
2421 gtk_text_buffer_select_range (buffer, where, where);
2425 * gtk_text_buffer_select_range:
2426 * @buffer: a #GtkTextBuffer
2427 * @ins: where to put the "insert" mark
2428 * @bound: where to put the "selection_bound" mark
2430 * This function moves the "insert" and "selection_bound" marks
2431 * simultaneously. If you move them in two steps
2432 * with gtk_text_buffer_move_mark(), you will temporarily select a
2433 * region in between their old and new locations, which can be pretty
2434 * inefficient since the temporarily-selected region will force stuff
2435 * to be recalculated. This function moves them as a unit, which can
2441 gtk_text_buffer_select_range (GtkTextBuffer *buffer,
2442 const GtkTextIter *ins,
2443 const GtkTextIter *bound)
2445 GtkTextIter real_ins;
2446 GtkTextIter real_bound;
2448 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2451 real_bound = *bound;
2453 _gtk_text_btree_select_range (get_btree (buffer), &real_ins, &real_bound);
2454 gtk_text_buffer_mark_set (buffer, &real_ins,
2455 gtk_text_buffer_get_insert (buffer));
2456 gtk_text_buffer_mark_set (buffer, &real_bound,
2457 gtk_text_buffer_get_selection_bound (buffer));
2465 * gtk_text_buffer_create_tag:
2466 * @buffer: a #GtkTextBuffer
2467 * @tag_name: (allow-none): name of the new tag, or %NULL
2468 * @first_property_name: (allow-none): name of first property to set, or %NULL
2469 * @...: %NULL-terminated list of property names and values
2471 * Creates a tag and adds it to the tag table for @buffer.
2472 * Equivalent to calling gtk_text_tag_new() and then adding the
2473 * tag to the buffer's tag table. The returned tag is owned by
2474 * the buffer's tag table, so the ref count will be equal to one.
2476 * If @tag_name is %NULL, the tag is anonymous.
2478 * If @tag_name is non-%NULL, a tag called @tag_name must not already
2479 * exist in the tag table for this buffer.
2481 * The @first_property_name argument and subsequent arguments are a list
2482 * of properties to set on the tag, as with g_object_set().
2484 * Return value: (transfer none): a new tag
2487 gtk_text_buffer_create_tag (GtkTextBuffer *buffer,
2488 const gchar *tag_name,
2489 const gchar *first_property_name,
2495 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
2497 tag = gtk_text_tag_new (tag_name);
2499 gtk_text_tag_table_add (get_table (buffer), tag);
2501 if (first_property_name)
2503 va_start (list, first_property_name);
2504 g_object_set_valist (G_OBJECT (tag), first_property_name, list);
2508 g_object_unref (tag);
2514 gtk_text_buffer_real_apply_tag (GtkTextBuffer *buffer,
2516 const GtkTextIter *start,
2517 const GtkTextIter *end)
2519 if (tag->priv->table != buffer->priv->tag_table)
2521 g_warning ("Can only apply tags that are in the tag table for the buffer");
2525 _gtk_text_btree_tag (start, end, tag, TRUE);
2529 gtk_text_buffer_real_remove_tag (GtkTextBuffer *buffer,
2531 const GtkTextIter *start,
2532 const GtkTextIter *end)
2534 if (tag->priv->table != buffer->priv->tag_table)
2536 g_warning ("Can only remove tags that are in the tag table for the buffer");
2540 _gtk_text_btree_tag (start, end, tag, FALSE);
2544 gtk_text_buffer_real_changed (GtkTextBuffer *buffer)
2546 gtk_text_buffer_set_modified (buffer, TRUE);
2550 gtk_text_buffer_real_mark_set (GtkTextBuffer *buffer,
2551 const GtkTextIter *iter,
2554 GtkTextMark *insert;
2556 insert = gtk_text_buffer_get_insert (buffer);
2558 if (mark == insert || mark == gtk_text_buffer_get_selection_bound (buffer))
2560 gboolean has_selection;
2562 update_selection_clipboards (buffer);
2564 has_selection = gtk_text_buffer_get_selection_bounds (buffer,
2568 if (has_selection != buffer->priv->has_selection)
2570 buffer->priv->has_selection = has_selection;
2571 g_object_notify (G_OBJECT (buffer), "has-selection");
2576 g_object_notify (G_OBJECT (buffer), "cursor-position");
2580 gtk_text_buffer_emit_tag (GtkTextBuffer *buffer,
2583 const GtkTextIter *start,
2584 const GtkTextIter *end)
2586 GtkTextIter start_tmp = *start;
2587 GtkTextIter end_tmp = *end;
2589 g_return_if_fail (tag != NULL);
2591 gtk_text_iter_order (&start_tmp, &end_tmp);
2594 g_signal_emit (buffer, signals[APPLY_TAG],
2596 tag, &start_tmp, &end_tmp);
2598 g_signal_emit (buffer, signals[REMOVE_TAG],
2600 tag, &start_tmp, &end_tmp);
2604 * gtk_text_buffer_apply_tag:
2605 * @buffer: a #GtkTextBuffer
2606 * @tag: a #GtkTextTag
2607 * @start: one bound of range to be tagged
2608 * @end: other bound of range to be tagged
2610 * Emits the "apply-tag" signal on @buffer. The default
2611 * handler for the signal applies @tag to the given range.
2612 * @start and @end do not have to be in order.
2615 gtk_text_buffer_apply_tag (GtkTextBuffer *buffer,
2617 const GtkTextIter *start,
2618 const GtkTextIter *end)
2620 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2621 g_return_if_fail (GTK_IS_TEXT_TAG (tag));
2622 g_return_if_fail (start != NULL);
2623 g_return_if_fail (end != NULL);
2624 g_return_if_fail (gtk_text_iter_get_buffer (start) == buffer);
2625 g_return_if_fail (gtk_text_iter_get_buffer (end) == buffer);
2626 g_return_if_fail (tag->priv->table == buffer->priv->tag_table);
2628 gtk_text_buffer_emit_tag (buffer, tag, TRUE, start, end);
2632 * gtk_text_buffer_remove_tag:
2633 * @buffer: a #GtkTextBuffer
2634 * @tag: a #GtkTextTag
2635 * @start: one bound of range to be untagged
2636 * @end: other bound of range to be untagged
2638 * Emits the "remove-tag" signal. The default handler for the signal
2639 * removes all occurrences of @tag from the given range. @start and
2640 * @end don't have to be in order.
2643 gtk_text_buffer_remove_tag (GtkTextBuffer *buffer,
2645 const GtkTextIter *start,
2646 const GtkTextIter *end)
2649 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2650 g_return_if_fail (GTK_IS_TEXT_TAG (tag));
2651 g_return_if_fail (start != NULL);
2652 g_return_if_fail (end != NULL);
2653 g_return_if_fail (gtk_text_iter_get_buffer (start) == buffer);
2654 g_return_if_fail (gtk_text_iter_get_buffer (end) == buffer);
2655 g_return_if_fail (tag->priv->table == buffer->priv->tag_table);
2657 gtk_text_buffer_emit_tag (buffer, tag, FALSE, start, end);
2661 * gtk_text_buffer_apply_tag_by_name:
2662 * @buffer: a #GtkTextBuffer
2663 * @name: name of a named #GtkTextTag
2664 * @start: one bound of range to be tagged
2665 * @end: other bound of range to be tagged
2667 * Calls gtk_text_tag_table_lookup() on the buffer's tag table to
2668 * get a #GtkTextTag, then calls gtk_text_buffer_apply_tag().
2671 gtk_text_buffer_apply_tag_by_name (GtkTextBuffer *buffer,
2673 const GtkTextIter *start,
2674 const GtkTextIter *end)
2678 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2679 g_return_if_fail (name != NULL);
2680 g_return_if_fail (start != NULL);
2681 g_return_if_fail (end != NULL);
2682 g_return_if_fail (gtk_text_iter_get_buffer (start) == buffer);
2683 g_return_if_fail (gtk_text_iter_get_buffer (end) == buffer);
2685 tag = gtk_text_tag_table_lookup (get_table (buffer),
2690 g_warning ("Unknown tag `%s'", name);
2694 gtk_text_buffer_emit_tag (buffer, tag, TRUE, start, end);
2698 * gtk_text_buffer_remove_tag_by_name:
2699 * @buffer: a #GtkTextBuffer
2700 * @name: name of a #GtkTextTag
2701 * @start: one bound of range to be untagged
2702 * @end: other bound of range to be untagged
2704 * Calls gtk_text_tag_table_lookup() on the buffer's tag table to
2705 * get a #GtkTextTag, then calls gtk_text_buffer_remove_tag().
2708 gtk_text_buffer_remove_tag_by_name (GtkTextBuffer *buffer,
2710 const GtkTextIter *start,
2711 const GtkTextIter *end)
2715 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2716 g_return_if_fail (name != NULL);
2717 g_return_if_fail (start != NULL);
2718 g_return_if_fail (end != NULL);
2719 g_return_if_fail (gtk_text_iter_get_buffer (start) == buffer);
2720 g_return_if_fail (gtk_text_iter_get_buffer (end) == buffer);
2722 tag = gtk_text_tag_table_lookup (get_table (buffer),
2727 g_warning ("Unknown tag `%s'", name);
2731 gtk_text_buffer_emit_tag (buffer, tag, FALSE, start, end);
2735 pointer_cmp (gconstpointer a,
2747 * gtk_text_buffer_remove_all_tags:
2748 * @buffer: a #GtkTextBuffer
2749 * @start: one bound of range to be untagged
2750 * @end: other bound of range to be untagged
2752 * Removes all tags in the range between @start and @end. Be careful
2753 * with this function; it could remove tags added in code unrelated to
2754 * the code you're currently writing. That is, using this function is
2755 * probably a bad idea if you have two or more unrelated code sections
2759 gtk_text_buffer_remove_all_tags (GtkTextBuffer *buffer,
2760 const GtkTextIter *start,
2761 const GtkTextIter *end)
2763 GtkTextIter first, second, tmp;
2766 GSList *prev, *next;
2769 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2770 g_return_if_fail (start != NULL);
2771 g_return_if_fail (end != NULL);
2772 g_return_if_fail (gtk_text_iter_get_buffer (start) == buffer);
2773 g_return_if_fail (gtk_text_iter_get_buffer (end) == buffer);
2778 gtk_text_iter_order (&first, &second);
2780 /* Get all tags turned on at the start */
2781 tags = gtk_text_iter_get_tags (&first);
2783 /* Find any that are toggled on within the range */
2785 while (gtk_text_iter_forward_to_tag_toggle (&tmp, NULL))
2790 if (gtk_text_iter_compare (&tmp, &second) >= 0)
2791 break; /* past the end of the range */
2793 toggled = gtk_text_iter_get_toggled_tags (&tmp, TRUE);
2795 /* We could end up with a really big-ass list here.
2798 tmp_list2 = toggled;
2799 while (tmp_list2 != NULL)
2801 tags = g_slist_prepend (tags, tmp_list2->data);
2803 tmp_list2 = g_slist_next (tmp_list2);
2806 g_slist_free (toggled);
2810 tags = g_slist_sort (tags, pointer_cmp);
2812 /* Strip duplicates */
2816 while (tmp_list != NULL)
2818 if (tag == tmp_list->data)
2821 next = tmp_list->next;
2825 tmp_list->next = NULL;
2827 g_slist_free (tmp_list);
2830 /* prev is unchanged */
2834 /* not a duplicate */
2835 tag = GTK_TEXT_TAG (tmp_list->data);
2837 tmp_list = tmp_list->next;
2841 g_slist_foreach (tags, (GFunc) g_object_ref, NULL);
2844 while (tmp_list != NULL)
2846 tag = GTK_TEXT_TAG (tmp_list->data);
2848 gtk_text_buffer_remove_tag (buffer, tag, &first, &second);
2850 tmp_list = tmp_list->next;
2853 g_slist_foreach (tags, (GFunc) g_object_unref, NULL);
2855 g_slist_free (tags);
2860 * Obtain various iterators
2864 * gtk_text_buffer_get_iter_at_line_offset:
2865 * @buffer: a #GtkTextBuffer
2866 * @iter: (out): iterator to initialize
2867 * @line_number: line number counting from 0
2868 * @char_offset: char offset from start of line
2870 * Obtains an iterator pointing to @char_offset within the given
2871 * line. The @char_offset must exist, offsets off the end of the line
2872 * are not allowed. Note <emphasis>characters</emphasis>, not bytes;
2873 * UTF-8 may encode one character as multiple bytes.
2876 gtk_text_buffer_get_iter_at_line_offset (GtkTextBuffer *buffer,
2881 g_return_if_fail (iter != NULL);
2882 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2884 _gtk_text_btree_get_iter_at_line_char (get_btree (buffer),
2885 iter, line_number, char_offset);
2889 * gtk_text_buffer_get_iter_at_line_index:
2890 * @buffer: a #GtkTextBuffer
2891 * @iter: (out): iterator to initialize
2892 * @line_number: line number counting from 0
2893 * @byte_index: byte index from start of line
2895 * Obtains an iterator pointing to @byte_index within the given line.
2896 * @byte_index must be the start of a UTF-8 character, and must not be
2897 * beyond the end of the line. Note <emphasis>bytes</emphasis>, not
2898 * characters; UTF-8 may encode one character as multiple bytes.
2901 gtk_text_buffer_get_iter_at_line_index (GtkTextBuffer *buffer,
2906 g_return_if_fail (iter != NULL);
2907 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2909 _gtk_text_btree_get_iter_at_line_byte (get_btree (buffer),
2910 iter, line_number, byte_index);
2914 * gtk_text_buffer_get_iter_at_line:
2915 * @buffer: a #GtkTextBuffer
2916 * @iter: (out): iterator to initialize
2917 * @line_number: line number counting from 0
2919 * Initializes @iter to the start of the given line.
2922 gtk_text_buffer_get_iter_at_line (GtkTextBuffer *buffer,
2926 g_return_if_fail (iter != NULL);
2927 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2929 gtk_text_buffer_get_iter_at_line_offset (buffer, iter, line_number, 0);
2933 * gtk_text_buffer_get_iter_at_offset:
2934 * @buffer: a #GtkTextBuffer
2935 * @iter: (out): iterator to initialize
2936 * @char_offset: char offset from start of buffer, counting from 0, or -1
2938 * Initializes @iter to a position @char_offset chars from the start
2939 * of the entire buffer. If @char_offset is -1 or greater than the number
2940 * of characters in the buffer, @iter is initialized to the end iterator,
2941 * the iterator one past the last valid character in the buffer.
2944 gtk_text_buffer_get_iter_at_offset (GtkTextBuffer *buffer,
2948 g_return_if_fail (iter != NULL);
2949 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2951 _gtk_text_btree_get_iter_at_char (get_btree (buffer), iter, char_offset);
2955 * gtk_text_buffer_get_start_iter:
2956 * @buffer: a #GtkTextBuffer
2957 * @iter: (out): iterator to initialize
2959 * Initialized @iter with the first position in the text buffer. This
2960 * is the same as using gtk_text_buffer_get_iter_at_offset() to get
2961 * the iter at character offset 0.
2964 gtk_text_buffer_get_start_iter (GtkTextBuffer *buffer,
2967 g_return_if_fail (iter != NULL);
2968 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2970 _gtk_text_btree_get_iter_at_char (get_btree (buffer), iter, 0);
2974 * gtk_text_buffer_get_end_iter:
2975 * @buffer: a #GtkTextBuffer
2976 * @iter: (out): iterator to initialize
2978 * Initializes @iter with the "end iterator," one past the last valid
2979 * character in the text buffer. If dereferenced with
2980 * gtk_text_iter_get_char(), the end iterator has a character value of
2981 * 0. The entire buffer lies in the range from the first position in
2982 * the buffer (call gtk_text_buffer_get_start_iter() to get
2983 * character position 0) to the end iterator.
2986 gtk_text_buffer_get_end_iter (GtkTextBuffer *buffer,
2989 g_return_if_fail (iter != NULL);
2990 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2992 _gtk_text_btree_get_end_iter (get_btree (buffer), iter);
2996 * gtk_text_buffer_get_bounds:
2997 * @buffer: a #GtkTextBuffer
2998 * @start: (out): iterator to initialize with first position in the buffer
2999 * @end: (out): iterator to initialize with the end iterator
3001 * Retrieves the first and last iterators in the buffer, i.e. the
3002 * entire buffer lies within the range [@start,@end).
3005 gtk_text_buffer_get_bounds (GtkTextBuffer *buffer,
3009 g_return_if_fail (start != NULL);
3010 g_return_if_fail (end != NULL);
3011 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
3013 _gtk_text_btree_get_iter_at_char (get_btree (buffer), start, 0);
3014 _gtk_text_btree_get_end_iter (get_btree (buffer), end);
3022 * gtk_text_buffer_get_modified:
3023 * @buffer: a #GtkTextBuffer
3025 * Indicates whether the buffer has been modified since the last call
3026 * to gtk_text_buffer_set_modified() set the modification flag to
3027 * %FALSE. Used for example to enable a "save" function in a text
3030 * Return value: %TRUE if the buffer has been modified
3033 gtk_text_buffer_get_modified (GtkTextBuffer *buffer)
3035 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
3037 return buffer->priv->modified;
3041 * gtk_text_buffer_set_modified:
3042 * @buffer: a #GtkTextBuffer
3043 * @setting: modification flag setting
3045 * Used to keep track of whether the buffer has been modified since the
3046 * last time it was saved. Whenever the buffer is saved to disk, call
3047 * gtk_text_buffer_set_modified (@buffer, FALSE). When the buffer is modified,
3048 * it will automatically toggled on the modified bit again. When the modified
3049 * bit flips, the buffer emits a "modified-changed" signal.
3052 gtk_text_buffer_set_modified (GtkTextBuffer *buffer,
3055 gboolean fixed_setting;
3057 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
3059 fixed_setting = setting != FALSE;
3061 if (buffer->priv->modified == fixed_setting)
3065 buffer->priv->modified = fixed_setting;
3066 g_signal_emit (buffer, signals[MODIFIED_CHANGED], 0);
3071 * gtk_text_buffer_get_has_selection:
3072 * @buffer: a #GtkTextBuffer
3074 * Indicates whether the buffer has some text currently selected.
3076 * Return value: %TRUE if the there is text selected
3081 gtk_text_buffer_get_has_selection (GtkTextBuffer *buffer)
3083 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
3085 return buffer->priv->has_selection;
3090 * Assorted other stuff
3094 * gtk_text_buffer_get_line_count:
3095 * @buffer: a #GtkTextBuffer
3097 * Obtains the number of lines in the buffer. This value is cached, so
3098 * the function is very fast.
3100 * Return value: number of lines in the buffer
3103 gtk_text_buffer_get_line_count (GtkTextBuffer *buffer)
3105 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), 0);
3107 return _gtk_text_btree_line_count (get_btree (buffer));
3111 * gtk_text_buffer_get_char_count:
3112 * @buffer: a #GtkTextBuffer
3114 * Gets the number of characters in the buffer; note that characters
3115 * and bytes are not the same, you can't e.g. expect the contents of
3116 * the buffer in string form to be this many bytes long. The character
3117 * count is cached, so this function is very fast.
3119 * Return value: number of characters in the buffer
3122 gtk_text_buffer_get_char_count (GtkTextBuffer *buffer)
3124 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), 0);
3126 return _gtk_text_btree_char_count (get_btree (buffer));
3129 /* Called when we lose the primary selection.
3132 clipboard_clear_selection_cb (GtkClipboard *clipboard,
3135 /* Move selection_bound to the insertion point */
3137 GtkTextIter selection_bound;
3138 GtkTextBuffer *buffer = GTK_TEXT_BUFFER (data);
3140 gtk_text_buffer_get_iter_at_mark (buffer, &insert,
3141 gtk_text_buffer_get_insert (buffer));
3142 gtk_text_buffer_get_iter_at_mark (buffer, &selection_bound,
3143 gtk_text_buffer_get_selection_bound (buffer));
3145 if (!gtk_text_iter_equal (&insert, &selection_bound))
3146 gtk_text_buffer_move_mark (buffer,
3147 gtk_text_buffer_get_selection_bound (buffer),
3151 /* Called when we have the primary selection and someone else wants our
3152 * data in order to paste it.
3155 clipboard_get_selection_cb (GtkClipboard *clipboard,
3156 GtkSelectionData *selection_data,
3160 GtkTextBuffer *buffer = GTK_TEXT_BUFFER (data);
3161 GtkTextIter start, end;
3163 if (gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
3165 if (info == GTK_TEXT_BUFFER_TARGET_INFO_BUFFER_CONTENTS)
3167 /* Provide the address of the buffer; this will only be
3168 * used within-process
3170 gtk_selection_data_set (selection_data,
3171 gtk_selection_data_get_target (selection_data),
3176 else if (info == GTK_TEXT_BUFFER_TARGET_INFO_RICH_TEXT)
3181 str = gtk_text_buffer_serialize (buffer, buffer,
3182 gtk_selection_data_get_target (selection_data),
3183 &start, &end, &len);
3185 gtk_selection_data_set (selection_data,
3186 gtk_selection_data_get_target (selection_data),
3195 str = gtk_text_iter_get_visible_text (&start, &end);
3196 gtk_selection_data_set_text (selection_data, str, -1);
3202 static GtkTextBuffer *
3203 create_clipboard_contents_buffer (GtkTextBuffer *buffer)
3205 GtkTextBuffer *contents;
3207 contents = gtk_text_buffer_new (gtk_text_buffer_get_tag_table (buffer));
3209 g_object_set_data (G_OBJECT (contents), I_("gtk-text-buffer-clipboard-source"),
3211 g_object_set_data (G_OBJECT (contents), I_("gtk-text-buffer-clipboard"),
3212 GINT_TO_POINTER (1));
3214 /* Ref the source buffer as long as the clipboard contents buffer
3215 * exists, because it's needed for serializing the contents buffer.
3216 * See http://bugzilla.gnome.org/show_bug.cgi?id=339195
3218 g_object_ref (buffer);
3219 g_object_weak_ref (G_OBJECT (contents), (GWeakNotify) g_object_unref, buffer);
3224 /* Provide cut/copied data */
3226 clipboard_get_contents_cb (GtkClipboard *clipboard,
3227 GtkSelectionData *selection_data,
3231 GtkTextBuffer *contents = GTK_TEXT_BUFFER (data);
3233 g_assert (contents); /* This should never be called unless we own the clipboard */
3235 if (info == GTK_TEXT_BUFFER_TARGET_INFO_BUFFER_CONTENTS)
3237 /* Provide the address of the clipboard buffer; this will only
3238 * be used within-process. OK to supply a NULL value for contents.
3240 gtk_selection_data_set (selection_data,
3241 gtk_selection_data_get_target (selection_data),
3246 else if (info == GTK_TEXT_BUFFER_TARGET_INFO_RICH_TEXT)
3248 GtkTextBuffer *clipboard_source_buffer;
3249 GtkTextIter start, end;
3253 clipboard_source_buffer = g_object_get_data (G_OBJECT (contents),
3254 "gtk-text-buffer-clipboard-source");
3256 gtk_text_buffer_get_bounds (contents, &start, &end);
3258 str = gtk_text_buffer_serialize (clipboard_source_buffer, contents,
3259 gtk_selection_data_get_target (selection_data),
3260 &start, &end, &len);
3262 gtk_selection_data_set (selection_data,
3263 gtk_selection_data_get_target (selection_data),
3271 GtkTextIter start, end;
3273 gtk_text_buffer_get_bounds (contents, &start, &end);
3275 str = gtk_text_iter_get_visible_text (&start, &end);
3276 gtk_selection_data_set_text (selection_data, str, -1);
3282 clipboard_clear_contents_cb (GtkClipboard *clipboard,
3285 GtkTextBuffer *contents = GTK_TEXT_BUFFER (data);
3287 g_object_unref (contents);
3291 get_paste_point (GtkTextBuffer *buffer,
3293 gboolean clear_afterward)
3295 GtkTextIter insert_point;
3296 GtkTextMark *paste_point_override;
3298 paste_point_override = gtk_text_buffer_get_mark (buffer,
3299 "gtk_paste_point_override");
3301 if (paste_point_override != NULL)
3303 gtk_text_buffer_get_iter_at_mark (buffer, &insert_point,
3304 paste_point_override);
3305 if (clear_afterward)
3306 gtk_text_buffer_delete_mark (buffer, paste_point_override);
3310 gtk_text_buffer_get_iter_at_mark (buffer, &insert_point,
3311 gtk_text_buffer_get_insert (buffer));
3314 *iter = insert_point;
3318 pre_paste_prep (ClipboardRequest *request_data,
3319 GtkTextIter *insert_point)
3321 GtkTextBuffer *buffer = request_data->buffer;
3323 get_paste_point (buffer, insert_point, TRUE);
3325 /* If we're going to replace the selection, we insert before it to
3326 * avoid messing it up, then we delete the selection after inserting.
3328 if (request_data->replace_selection)
3330 GtkTextIter start, end;
3332 if (gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
3333 *insert_point = start;
3338 post_paste_cleanup (ClipboardRequest *request_data)
3340 if (request_data->replace_selection)
3342 GtkTextIter start, end;
3344 if (gtk_text_buffer_get_selection_bounds (request_data->buffer,
3347 if (request_data->interactive)
3348 gtk_text_buffer_delete_interactive (request_data->buffer,
3351 request_data->default_editable);
3353 gtk_text_buffer_delete (request_data->buffer, &start, &end);
3359 emit_paste_done (GtkTextBuffer *buffer,
3360 GtkClipboard *clipboard)
3362 g_signal_emit (buffer, signals[PASTE_DONE], 0, clipboard);
3366 free_clipboard_request (ClipboardRequest *request_data)
3368 g_object_unref (request_data->buffer);
3369 g_free (request_data);
3372 /* Called when we request a paste and receive the text data
3375 clipboard_text_received (GtkClipboard *clipboard,
3379 ClipboardRequest *request_data = data;
3380 GtkTextBuffer *buffer = request_data->buffer;
3384 GtkTextIter insert_point;
3386 if (request_data->interactive)
3387 gtk_text_buffer_begin_user_action (buffer);
3389 pre_paste_prep (request_data, &insert_point);
3391 if (request_data->interactive)
3392 gtk_text_buffer_insert_interactive (buffer, &insert_point,
3393 str, -1, request_data->default_editable);
3395 gtk_text_buffer_insert (buffer, &insert_point,
3398 post_paste_cleanup (request_data);
3400 if (request_data->interactive)
3401 gtk_text_buffer_end_user_action (buffer);
3403 emit_paste_done (buffer, clipboard);
3407 /* It may happen that we set a point override but we are not inserting
3408 any text, so we must remove it afterwards */
3409 GtkTextMark *paste_point_override;
3411 paste_point_override = gtk_text_buffer_get_mark (buffer,
3412 "gtk_paste_point_override");
3414 if (paste_point_override != NULL)
3415 gtk_text_buffer_delete_mark (buffer, paste_point_override);
3418 free_clipboard_request (request_data);
3421 static GtkTextBuffer*
3422 selection_data_get_buffer (GtkSelectionData *selection_data,
3423 ClipboardRequest *request_data)
3426 GtkTextBuffer *src_buffer = NULL;
3428 /* If we can get the owner, the selection is in-process */
3429 owner = gdk_selection_owner_get_for_display (gtk_selection_data_get_display (selection_data),
3430 gtk_selection_data_get_selection (selection_data));
3435 if (gdk_window_get_window_type (owner) == GDK_WINDOW_FOREIGN)
3438 if (gtk_selection_data_get_data_type (selection_data) != gdk_atom_intern_static_string ("GTK_TEXT_BUFFER_CONTENTS"))
3441 if (gtk_selection_data_get_length (selection_data) != sizeof (src_buffer))
3444 memcpy (&src_buffer, gtk_selection_data_get_data (selection_data), sizeof (src_buffer));
3446 if (src_buffer == NULL)
3449 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (src_buffer), NULL);
3451 if (gtk_text_buffer_get_tag_table (src_buffer) !=
3452 gtk_text_buffer_get_tag_table (request_data->buffer))
3459 /* These are pretty handy functions; maybe something like them
3460 * should be in the public API. Also, there are other places in this
3461 * file where they could be used.
3464 save_iter (const GtkTextIter *iter,
3465 gboolean left_gravity)
3467 return gtk_text_buffer_create_mark (gtk_text_iter_get_buffer (iter),
3474 restore_iter (const GtkTextIter *iter,
3477 gtk_text_buffer_get_iter_at_mark (gtk_text_mark_get_buffer (save_id),
3478 (GtkTextIter*) iter,
3480 gtk_text_buffer_delete_mark (gtk_text_mark_get_buffer (save_id),
3486 clipboard_rich_text_received (GtkClipboard *clipboard,
3492 ClipboardRequest *request_data = data;
3493 GtkTextIter insert_point;
3494 gboolean retval = TRUE;
3495 GError *error = NULL;
3497 if (text != NULL && length > 0)
3499 pre_paste_prep (request_data, &insert_point);
3501 if (request_data->interactive)
3502 gtk_text_buffer_begin_user_action (request_data->buffer);
3504 if (!request_data->interactive ||
3505 gtk_text_iter_can_insert (&insert_point,
3506 request_data->default_editable))
3508 retval = gtk_text_buffer_deserialize (request_data->buffer,
3509 request_data->buffer,
3518 g_warning ("error pasting: %s\n", error->message);
3519 g_clear_error (&error);
3522 if (request_data->interactive)
3523 gtk_text_buffer_end_user_action (request_data->buffer);
3525 emit_paste_done (request_data->buffer, clipboard);
3529 post_paste_cleanup (request_data);
3534 /* Request the text selection instead */
3535 gtk_clipboard_request_text (clipboard,
3536 clipboard_text_received,
3541 paste_from_buffer (GtkClipboard *clipboard,
3542 ClipboardRequest *request_data,
3543 GtkTextBuffer *src_buffer,
3544 const GtkTextIter *start,
3545 const GtkTextIter *end)
3547 GtkTextIter insert_point;
3548 GtkTextBuffer *buffer = request_data->buffer;
3550 /* We're about to emit a bunch of signals, so be safe */
3551 g_object_ref (src_buffer);
3553 pre_paste_prep (request_data, &insert_point);
3555 if (request_data->interactive)
3556 gtk_text_buffer_begin_user_action (buffer);
3558 if (!gtk_text_iter_equal (start, end))
3560 if (!request_data->interactive ||
3561 (gtk_text_iter_can_insert (&insert_point,
3562 request_data->default_editable)))
3563 gtk_text_buffer_real_insert_range (buffer,
3567 request_data->interactive);
3570 post_paste_cleanup (request_data);
3572 if (request_data->interactive)
3573 gtk_text_buffer_end_user_action (buffer);
3575 emit_paste_done (buffer, clipboard);
3577 g_object_unref (src_buffer);
3579 free_clipboard_request (request_data);
3583 clipboard_clipboard_buffer_received (GtkClipboard *clipboard,
3584 GtkSelectionData *selection_data,
3587 ClipboardRequest *request_data = data;
3588 GtkTextBuffer *src_buffer;
3590 src_buffer = selection_data_get_buffer (selection_data, request_data);
3594 GtkTextIter start, end;
3596 if (g_object_get_data (G_OBJECT (src_buffer), "gtk-text-buffer-clipboard"))
3598 gtk_text_buffer_get_bounds (src_buffer, &start, &end);
3600 paste_from_buffer (clipboard, request_data, src_buffer,
3605 if (gtk_text_buffer_get_selection_bounds (src_buffer, &start, &end))
3606 paste_from_buffer (clipboard, request_data, src_buffer,
3612 if (gtk_clipboard_wait_is_rich_text_available (clipboard,
3613 request_data->buffer))
3615 /* Request rich text */
3616 gtk_clipboard_request_rich_text (clipboard,
3617 request_data->buffer,
3618 clipboard_rich_text_received,
3623 /* Request the text selection instead */
3624 gtk_clipboard_request_text (clipboard,
3625 clipboard_text_received,
3633 GtkClipboard *clipboard;
3635 } SelectionClipboard;
3638 update_selection_clipboards (GtkTextBuffer *buffer)
3640 GtkTextBufferPrivate *priv;
3641 GSList *tmp_list = buffer->priv->selection_clipboards;
3643 priv = buffer->priv;
3645 gtk_text_buffer_get_copy_target_list (buffer);
3652 SelectionClipboard *selection_clipboard = tmp_list->data;
3653 GtkClipboard *clipboard = selection_clipboard->clipboard;
3655 /* Determine whether we have a selection and adjust X selection
3658 if (!gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
3660 if (gtk_clipboard_get_owner (clipboard) == G_OBJECT (buffer))
3661 gtk_clipboard_clear (clipboard);
3665 /* Even if we already have the selection, we need to update our
3668 if (!gtk_clipboard_set_with_owner (clipboard,
3669 priv->copy_target_entries,
3670 priv->n_copy_target_entries,
3671 clipboard_get_selection_cb,
3672 clipboard_clear_selection_cb,
3674 clipboard_clear_selection_cb (clipboard, buffer);
3677 tmp_list = tmp_list->next;
3681 static SelectionClipboard *
3682 find_selection_clipboard (GtkTextBuffer *buffer,
3683 GtkClipboard *clipboard)
3685 GSList *tmp_list = buffer->priv->selection_clipboards;
3688 SelectionClipboard *selection_clipboard = tmp_list->data;
3689 if (selection_clipboard->clipboard == clipboard)
3690 return selection_clipboard;
3692 tmp_list = tmp_list->next;
3699 * gtk_text_buffer_add_selection_clipboard:
3700 * @buffer: a #GtkTextBuffer
3701 * @clipboard: a #GtkClipboard
3703 * Adds @clipboard to the list of clipboards in which the selection
3704 * contents of @buffer are available. In most cases, @clipboard will be
3705 * the #GtkClipboard of type %GDK_SELECTION_PRIMARY for a view of @buffer.
3708 gtk_text_buffer_add_selection_clipboard (GtkTextBuffer *buffer,
3709 GtkClipboard *clipboard)
3711 SelectionClipboard *selection_clipboard;
3713 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
3714 g_return_if_fail (clipboard != NULL);
3716 selection_clipboard = find_selection_clipboard (buffer, clipboard);
3717 if (selection_clipboard)
3719 selection_clipboard->ref_count++;
3723 selection_clipboard = g_new (SelectionClipboard, 1);
3725 selection_clipboard->clipboard = clipboard;
3726 selection_clipboard->ref_count = 1;
3728 buffer->priv->selection_clipboards = g_slist_prepend (buffer->priv->selection_clipboards,
3729 selection_clipboard);
3734 * gtk_text_buffer_remove_selection_clipboard:
3735 * @buffer: a #GtkTextBuffer
3736 * @clipboard: a #GtkClipboard added to @buffer by
3737 * gtk_text_buffer_add_selection_clipboard()
3739 * Removes a #GtkClipboard added with
3740 * gtk_text_buffer_add_selection_clipboard().
3743 gtk_text_buffer_remove_selection_clipboard (GtkTextBuffer *buffer,
3744 GtkClipboard *clipboard)
3746 SelectionClipboard *selection_clipboard;
3748 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
3749 g_return_if_fail (clipboard != NULL);
3751 selection_clipboard = find_selection_clipboard (buffer, clipboard);
3752 g_return_if_fail (selection_clipboard != NULL);
3754 selection_clipboard->ref_count--;
3755 if (selection_clipboard->ref_count == 0)
3757 if (gtk_clipboard_get_owner (selection_clipboard->clipboard) == G_OBJECT (buffer))
3758 gtk_clipboard_clear (selection_clipboard->clipboard);
3760 buffer->priv->selection_clipboards = g_slist_remove (buffer->priv->selection_clipboards,
3761 selection_clipboard);
3763 g_free (selection_clipboard);
3768 remove_all_selection_clipboards (GtkTextBuffer *buffer)
3770 GtkTextBufferPrivate *priv = buffer->priv;
3772 g_slist_foreach (priv->selection_clipboards, (GFunc)g_free, NULL);
3773 g_slist_free (priv->selection_clipboards);
3774 priv->selection_clipboards = NULL;
3778 * gtk_text_buffer_paste_clipboard:
3779 * @buffer: a #GtkTextBuffer
3780 * @clipboard: the #GtkClipboard to paste from
3781 * @override_location: (allow-none): location to insert pasted text, or %NULL for
3783 * @default_editable: whether the buffer is editable by default
3785 * Pastes the contents of a clipboard at the insertion point, or
3786 * at @override_location. (Note: pasting is asynchronous, that is,
3787 * we'll ask for the paste data and return, and at some point later
3788 * after the main loop runs, the paste data will be inserted.)
3791 gtk_text_buffer_paste_clipboard (GtkTextBuffer *buffer,
3792 GtkClipboard *clipboard,
3793 GtkTextIter *override_location,
3794 gboolean default_editable)
3796 ClipboardRequest *data = g_new (ClipboardRequest, 1);
3797 GtkTextIter paste_point;
3798 GtkTextIter start, end;
3800 if (override_location != NULL)
3801 gtk_text_buffer_create_mark (buffer,
3802 "gtk_paste_point_override",
3803 override_location, FALSE);
3805 data->buffer = g_object_ref (buffer);
3806 data->interactive = TRUE;
3807 data->default_editable = !!default_editable;
3809 /* When pasting with the cursor inside the selection area, you
3810 * replace the selection with the new text, otherwise, you
3811 * simply insert the new text at the point where the click
3812 * occured, unselecting any selected text. The replace_selection
3813 * flag toggles this behavior.
3815 data->replace_selection = FALSE;
3817 get_paste_point (buffer, &paste_point, FALSE);
3818 if (gtk_text_buffer_get_selection_bounds (buffer, &start, &end) &&
3819 (gtk_text_iter_in_range (&paste_point, &start, &end) ||
3820 gtk_text_iter_equal (&paste_point, &end)))
3821 data->replace_selection = TRUE;
3823 gtk_clipboard_request_contents (clipboard,
3824 gdk_atom_intern_static_string ("GTK_TEXT_BUFFER_CONTENTS"),
3825 clipboard_clipboard_buffer_received, data);
3829 * gtk_text_buffer_delete_selection:
3830 * @buffer: a #GtkTextBuffer
3831 * @interactive: whether the deletion is caused by user interaction
3832 * @default_editable: whether the buffer is editable by default
3834 * Deletes the range between the "insert" and "selection_bound" marks,
3835 * that is, the currently-selected text. If @interactive is %TRUE,
3836 * the editability of the selection will be considered (users can't delete
3839 * Return value: whether there was a non-empty selection to delete
3842 gtk_text_buffer_delete_selection (GtkTextBuffer *buffer,
3843 gboolean interactive,
3844 gboolean default_editable)
3849 if (!gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
3851 return FALSE; /* No selection */
3856 gtk_text_buffer_delete_interactive (buffer, &start, &end, default_editable);
3858 gtk_text_buffer_delete (buffer, &start, &end);
3860 return TRUE; /* We deleted stuff */
3865 * gtk_text_buffer_backspace:
3866 * @buffer: a #GtkTextBuffer
3867 * @iter: a position in @buffer
3868 * @interactive: whether the deletion is caused by user interaction
3869 * @default_editable: whether the buffer is editable by default
3871 * Performs the appropriate action as if the user hit the delete
3872 * key with the cursor at the position specified by @iter. In the
3873 * normal case a single character will be deleted, but when
3874 * combining accents are involved, more than one character can
3875 * be deleted, and when precomposed character and accent combinations
3876 * are involved, less than one character will be deleted.
3878 * Because the buffer is modified, all outstanding iterators become
3879 * invalid after calling this function; however, the @iter will be
3880 * re-initialized to point to the location where text was deleted.
3882 * Return value: %TRUE if the buffer was modified
3887 gtk_text_buffer_backspace (GtkTextBuffer *buffer,
3889 gboolean interactive,
3890 gboolean default_editable)
3892 gchar *cluster_text;
3895 gboolean retval = FALSE;
3896 const PangoLogAttr *attrs;
3898 gboolean backspace_deletes_character;
3900 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
3901 g_return_val_if_fail (iter != NULL, FALSE);
3906 attrs = _gtk_text_buffer_get_line_log_attrs (buffer, &start, NULL);
3908 /* For no good reason, attrs is NULL for the empty last line in
3909 * a buffer. Special case that here. (#156164)
3913 offset = gtk_text_iter_get_line_offset (&start);
3914 backspace_deletes_character = attrs[offset].backspace_deletes_character;
3917 backspace_deletes_character = FALSE;
3919 gtk_text_iter_backward_cursor_position (&start);
3921 if (gtk_text_iter_equal (&start, &end))
3924 cluster_text = gtk_text_iter_get_text (&start, &end);
3927 gtk_text_buffer_begin_user_action (buffer);
3929 if (gtk_text_buffer_delete_interactive (buffer, &start, &end,
3932 /* special case \r\n, since we never want to reinsert \r */
3933 if (backspace_deletes_character && strcmp ("\r\n", cluster_text))
3935 gchar *normalized_text = g_utf8_normalize (cluster_text,
3936 strlen (cluster_text),
3938 glong len = g_utf8_strlen (normalized_text, -1);
3941 gtk_text_buffer_insert_interactive (buffer,
3944 g_utf8_offset_to_pointer (normalized_text, len - 1) - normalized_text,
3947 g_free (normalized_text);
3954 gtk_text_buffer_end_user_action (buffer);
3956 g_free (cluster_text);
3958 /* Revalidate the users iter */
3965 cut_or_copy (GtkTextBuffer *buffer,
3966 GtkClipboard *clipboard,
3967 gboolean delete_region_after,
3968 gboolean interactive,
3969 gboolean default_editable)
3971 GtkTextBufferPrivate *priv;
3973 /* We prefer to cut the selected region between selection_bound and
3974 * insertion point. If that region is empty, then we cut the region
3975 * between the "anchor" and the insertion point (this is for
3976 * C-space and M-w and other Emacs-style copy/yank keys). Note that
3977 * insert and selection_bound are guaranteed to exist, but the
3978 * anchor only exists sometimes.
3983 priv = buffer->priv;
3985 gtk_text_buffer_get_copy_target_list (buffer);
3987 if (!gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
3989 /* Let's try the anchor thing */
3990 GtkTextMark * anchor = gtk_text_buffer_get_mark (buffer, "anchor");
3996 gtk_text_buffer_get_iter_at_mark (buffer, &end, anchor);
3997 gtk_text_iter_order (&start, &end);
4001 if (!gtk_text_iter_equal (&start, &end))
4004 GtkTextBuffer *contents;
4006 contents = create_clipboard_contents_buffer (buffer);
4008 gtk_text_buffer_get_iter_at_offset (contents, &ins, 0);
4010 gtk_text_buffer_insert_range (contents, &ins, &start, &end);
4012 if (!gtk_clipboard_set_with_data (clipboard,
4013 priv->copy_target_entries,
4014 priv->n_copy_target_entries,
4015 clipboard_get_contents_cb,
4016 clipboard_clear_contents_cb,
4018 g_object_unref (contents);
4020 gtk_clipboard_set_can_store (clipboard,
4021 priv->copy_target_entries + 1,
4022 priv->n_copy_target_entries - 1);
4024 if (delete_region_after)
4027 gtk_text_buffer_delete_interactive (buffer, &start, &end,
4030 gtk_text_buffer_delete (buffer, &start, &end);
4036 * gtk_text_buffer_cut_clipboard:
4037 * @buffer: a #GtkTextBuffer
4038 * @clipboard: the #GtkClipboard object to cut to
4039 * @default_editable: default editability of the buffer
4041 * Copies the currently-selected text to a clipboard, then deletes
4042 * said text if it's editable.
4045 gtk_text_buffer_cut_clipboard (GtkTextBuffer *buffer,
4046 GtkClipboard *clipboard,
4047 gboolean default_editable)
4049 gtk_text_buffer_begin_user_action (buffer);
4050 cut_or_copy (buffer, clipboard, TRUE, TRUE, default_editable);
4051 gtk_text_buffer_end_user_action (buffer);
4055 * gtk_text_buffer_copy_clipboard:
4056 * @buffer: a #GtkTextBuffer
4057 * @clipboard: the #GtkClipboard object to copy to
4059 * Copies the currently-selected text to a clipboard.
4062 gtk_text_buffer_copy_clipboard (GtkTextBuffer *buffer,
4063 GtkClipboard *clipboard)
4065 cut_or_copy (buffer, clipboard, FALSE, TRUE, TRUE);
4069 * gtk_text_buffer_get_selection_bounds:
4070 * @buffer: a #GtkTextBuffer a #GtkTextBuffer
4071 * @start: (out): iterator to initialize with selection start
4072 * @end: (out): iterator to initialize with selection end
4074 * Returns %TRUE if some text is selected; places the bounds
4075 * of the selection in @start and @end (if the selection has length 0,
4076 * then @start and @end are filled in with the same value).
4077 * @start and @end will be in ascending order. If @start and @end are
4078 * NULL, then they are not filled in, but the return value still indicates
4079 * whether text is selected.
4081 * Return value: whether the selection has nonzero length
4084 gtk_text_buffer_get_selection_bounds (GtkTextBuffer *buffer,
4088 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
4090 return _gtk_text_btree_get_selection_bounds (get_btree (buffer), start, end);
4094 * gtk_text_buffer_begin_user_action:
4095 * @buffer: a #GtkTextBuffer
4097 * Called to indicate that the buffer operations between here and a
4098 * call to gtk_text_buffer_end_user_action() are part of a single
4099 * user-visible operation. The operations between
4100 * gtk_text_buffer_begin_user_action() and
4101 * gtk_text_buffer_end_user_action() can then be grouped when creating
4102 * an undo stack. #GtkTextBuffer maintains a count of calls to
4103 * gtk_text_buffer_begin_user_action() that have not been closed with
4104 * a call to gtk_text_buffer_end_user_action(), and emits the
4105 * "begin-user-action" and "end-user-action" signals only for the
4106 * outermost pair of calls. This allows you to build user actions
4107 * from other user actions.
4109 * The "interactive" buffer mutation functions, such as
4110 * gtk_text_buffer_insert_interactive(), automatically call begin/end
4111 * user action around the buffer operations they perform, so there's
4112 * no need to add extra calls if you user action consists solely of a
4113 * single call to one of those functions.
4116 gtk_text_buffer_begin_user_action (GtkTextBuffer *buffer)
4118 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
4120 buffer->priv->user_action_count += 1;
4122 if (buffer->priv->user_action_count == 1)
4124 /* Outermost nested user action begin emits the signal */
4125 g_signal_emit (buffer, signals[BEGIN_USER_ACTION], 0);
4130 * gtk_text_buffer_end_user_action:
4131 * @buffer: a #GtkTextBuffer
4133 * Should be paired with a call to gtk_text_buffer_begin_user_action().
4134 * See that function for a full explanation.
4137 gtk_text_buffer_end_user_action (GtkTextBuffer *buffer)
4139 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
4140 g_return_if_fail (buffer->priv->user_action_count > 0);
4142 buffer->priv->user_action_count -= 1;
4144 if (buffer->priv->user_action_count == 0)
4146 /* Ended the outermost-nested user action end, so emit the signal */
4147 g_signal_emit (buffer, signals[END_USER_ACTION], 0);
4152 gtk_text_buffer_free_target_lists (GtkTextBuffer *buffer)
4154 GtkTextBufferPrivate *priv = buffer->priv;
4156 if (priv->copy_target_list)
4158 gtk_target_list_unref (priv->copy_target_list);
4159 priv->copy_target_list = NULL;
4161 gtk_target_table_free (priv->copy_target_entries,
4162 priv->n_copy_target_entries);
4163 priv->copy_target_entries = NULL;
4164 priv->n_copy_target_entries = 0;
4167 if (priv->paste_target_list)
4169 gtk_target_list_unref (priv->paste_target_list);
4170 priv->paste_target_list = NULL;
4172 gtk_target_table_free (priv->paste_target_entries,
4173 priv->n_paste_target_entries);
4174 priv->paste_target_entries = NULL;
4175 priv->n_paste_target_entries = 0;
4179 static GtkTargetList *
4180 gtk_text_buffer_get_target_list (GtkTextBuffer *buffer,
4181 gboolean deserializable,
4182 GtkTargetEntry **entries,
4185 GtkTargetList *target_list;
4187 target_list = gtk_target_list_new (NULL, 0);
4189 gtk_target_list_add (target_list,
4190 gdk_atom_intern_static_string ("GTK_TEXT_BUFFER_CONTENTS"),
4191 GTK_TARGET_SAME_APP,
4192 GTK_TEXT_BUFFER_TARGET_INFO_BUFFER_CONTENTS);
4194 gtk_target_list_add_rich_text_targets (target_list,
4195 GTK_TEXT_BUFFER_TARGET_INFO_RICH_TEXT,
4199 gtk_target_list_add_text_targets (target_list,
4200 GTK_TEXT_BUFFER_TARGET_INFO_TEXT);
4202 *entries = gtk_target_table_new_from_list (target_list, n_entries);
4208 * gtk_text_buffer_get_copy_target_list:
4209 * @buffer: a #GtkTextBuffer
4211 * This function returns the list of targets this text buffer can
4212 * provide for copying and as DND source. The targets in the list are
4213 * added with %info values from the #GtkTextBufferTargetInfo enum,
4214 * using gtk_target_list_add_rich_text_targets() and
4215 * gtk_target_list_add_text_targets().
4217 * Return value: (transfer none): the #GtkTargetList
4222 gtk_text_buffer_get_copy_target_list (GtkTextBuffer *buffer)
4224 GtkTextBufferPrivate *priv;
4226 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
4228 priv = buffer->priv;
4230 if (! priv->copy_target_list)
4231 priv->copy_target_list =
4232 gtk_text_buffer_get_target_list (buffer, FALSE,
4233 &priv->copy_target_entries,
4234 &priv->n_copy_target_entries);
4236 return priv->copy_target_list;
4240 * gtk_text_buffer_get_paste_target_list:
4241 * @buffer: a #GtkTextBuffer
4243 * This function returns the list of targets this text buffer supports
4244 * for pasting and as DND destination. The targets in the list are
4245 * added with %info values from the #GtkTextBufferTargetInfo enum,
4246 * using gtk_target_list_add_rich_text_targets() and
4247 * gtk_target_list_add_text_targets().
4249 * Return value: (transfer none): the #GtkTargetList
4254 gtk_text_buffer_get_paste_target_list (GtkTextBuffer *buffer)
4256 GtkTextBufferPrivate *priv;
4258 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
4260 priv = buffer->priv;
4262 if (! priv->paste_target_list)
4263 priv->paste_target_list =
4264 gtk_text_buffer_get_target_list (buffer, TRUE,
4265 &priv->paste_target_entries,
4266 &priv->n_paste_target_entries);
4268 return priv->paste_target_list;
4272 * Logical attribute cache
4275 #define ATTR_CACHE_SIZE 2
4277 typedef struct _CacheEntry CacheEntry;
4282 PangoLogAttr *attrs;
4285 struct _GtkTextLogAttrCache
4287 gint chars_changed_stamp;
4288 CacheEntry entries[ATTR_CACHE_SIZE];
4292 free_log_attr_cache (GtkTextLogAttrCache *cache)
4295 while (i < ATTR_CACHE_SIZE)
4297 g_free (cache->entries[i].attrs);
4304 clear_log_attr_cache (GtkTextLogAttrCache *cache)
4307 while (i < ATTR_CACHE_SIZE)
4309 g_free (cache->entries[i].attrs);
4310 cache->entries[i].attrs = NULL;
4315 static PangoLogAttr*
4316 compute_log_attrs (const GtkTextIter *iter,
4322 gint char_len, byte_len;
4323 PangoLogAttr *attrs = NULL;
4328 gtk_text_iter_set_line_offset (&start, 0);
4329 gtk_text_iter_forward_line (&end);
4331 paragraph = gtk_text_iter_get_slice (&start, &end);
4332 char_len = g_utf8_strlen (paragraph, -1);
4333 byte_len = strlen (paragraph);
4335 g_assert (char_len > 0);
4338 *char_lenp = char_len;
4340 attrs = g_new (PangoLogAttr, char_len + 1);
4342 /* FIXME we need to follow PangoLayout and allow different language
4343 * tags within the paragraph
4345 pango_get_log_attrs (paragraph, byte_len, -1,
4346 gtk_text_iter_get_language (&start),
4355 /* The return value from this is valid until you call this a second time.
4358 _gtk_text_buffer_get_line_log_attrs (GtkTextBuffer *buffer,
4359 const GtkTextIter *anywhere_in_line,
4362 GtkTextBufferPrivate *priv;
4364 GtkTextLogAttrCache *cache;
4367 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
4368 g_return_val_if_fail (anywhere_in_line != NULL, NULL);
4370 priv = buffer->priv;
4372 /* special-case for empty last line in buffer */
4373 if (gtk_text_iter_is_end (anywhere_in_line) &&
4374 gtk_text_iter_get_line_offset (anywhere_in_line) == 0)
4381 /* FIXME we also need to recompute log attrs if the language tag at
4382 * the start of a paragraph changes
4385 if (priv->log_attr_cache == NULL)
4387 priv->log_attr_cache = g_new0 (GtkTextLogAttrCache, 1);
4388 priv->log_attr_cache->chars_changed_stamp =
4389 _gtk_text_btree_get_chars_changed_stamp (get_btree (buffer));
4391 else if (priv->log_attr_cache->chars_changed_stamp !=
4392 _gtk_text_btree_get_chars_changed_stamp (get_btree (buffer)))
4394 clear_log_attr_cache (priv->log_attr_cache);
4397 cache = priv->log_attr_cache;
4398 line = gtk_text_iter_get_line (anywhere_in_line);
4401 while (i < ATTR_CACHE_SIZE)
4403 if (cache->entries[i].attrs &&
4404 cache->entries[i].line == line)
4407 *char_len = cache->entries[i].char_len;
4408 return cache->entries[i].attrs;
4413 /* Not in cache; open up the first cache entry */
4414 g_free (cache->entries[ATTR_CACHE_SIZE-1].attrs);
4416 g_memmove (cache->entries + 1, cache->entries,
4417 sizeof (CacheEntry) * (ATTR_CACHE_SIZE - 1));
4419 cache->entries[0].line = line;
4420 cache->entries[0].attrs = compute_log_attrs (anywhere_in_line,
4421 &cache->entries[0].char_len);
4424 *char_len = cache->entries[0].char_len;
4426 return cache->entries[0].attrs;
4430 _gtk_text_buffer_notify_will_remove_tag (GtkTextBuffer *buffer,
4433 /* This removes tag from the buffer, but DOESN'T emit the
4434 * remove-tag signal, because we can't afford to have user
4435 * code messing things up at this point; the tag MUST be removed
4438 if (buffer->priv->btree)
4439 _gtk_text_btree_notify_will_remove_tag (buffer->priv->btree, tag);
4447 _gtk_text_buffer_spew (GtkTextBuffer *buffer)
4449 _gtk_text_btree_spew (get_btree (buffer));
4453 _gtk_text_buffer_get_text_before (GtkTextBuffer *buffer,
4454 AtkTextBoundary boundary_type,
4455 GtkTextIter *position,
4464 switch (boundary_type)
4466 case ATK_TEXT_BOUNDARY_CHAR:
4467 gtk_text_iter_backward_char (start);
4470 case ATK_TEXT_BOUNDARY_WORD_START:
4471 if (!gtk_text_iter_starts_word (start))
4472 gtk_text_iter_backward_word_start (start);
4474 gtk_text_iter_backward_word_start (start);
4477 case ATK_TEXT_BOUNDARY_WORD_END:
4478 if (gtk_text_iter_inside_word (start) &&
4479 !gtk_text_iter_starts_word (start))
4480 gtk_text_iter_backward_word_start (start);
4481 while (!gtk_text_iter_ends_word (start))
4483 if (!gtk_text_iter_backward_char (start))
4487 gtk_text_iter_backward_word_start (start);
4488 while (!gtk_text_iter_ends_word (start))
4490 if (!gtk_text_iter_backward_char (start))
4495 case ATK_TEXT_BOUNDARY_SENTENCE_START:
4496 if (!gtk_text_iter_starts_sentence (start))
4497 gtk_text_iter_backward_sentence_start (start);
4499 gtk_text_iter_backward_sentence_start (start);
4502 case ATK_TEXT_BOUNDARY_SENTENCE_END:
4503 if (gtk_text_iter_inside_sentence (start) &&
4504 !gtk_text_iter_starts_sentence (start))
4505 gtk_text_iter_backward_sentence_start (start);
4506 while (!gtk_text_iter_ends_sentence (start))
4508 if (!gtk_text_iter_backward_char (start))
4512 gtk_text_iter_backward_sentence_start (start);
4513 while (!gtk_text_iter_ends_sentence (start))
4515 if (!gtk_text_iter_backward_char (start))
4520 case ATK_TEXT_BOUNDARY_LINE_START:
4521 line_number = gtk_text_iter_get_line (start);
4522 if (line_number == 0)
4524 gtk_text_buffer_get_iter_at_offset (buffer, start, 0);
4528 gtk_text_iter_backward_line (start);
4529 gtk_text_iter_forward_line (start);
4532 gtk_text_iter_backward_line (start);
4535 case ATK_TEXT_BOUNDARY_LINE_END:
4536 line_number = gtk_text_iter_get_line (start);
4537 if (line_number == 0)
4539 gtk_text_buffer_get_iter_at_offset (buffer, start, 0);
4544 gtk_text_iter_backward_line (start);
4546 while (!gtk_text_iter_ends_line (start))
4548 if (!gtk_text_iter_backward_char (start))
4551 gtk_text_iter_forward_to_line_end (end);
4558 _gtk_text_buffer_get_text_at (GtkTextBuffer *buffer,
4559 AtkTextBoundary boundary_type,
4560 GtkTextIter *position,
4569 switch (boundary_type)
4571 case ATK_TEXT_BOUNDARY_CHAR:
4572 gtk_text_iter_forward_char (end);
4575 case ATK_TEXT_BOUNDARY_WORD_START:
4576 if (!gtk_text_iter_starts_word (start))
4577 gtk_text_iter_backward_word_start (start);
4578 if (gtk_text_iter_inside_word (end))
4579 gtk_text_iter_forward_word_end (end);
4580 while (!gtk_text_iter_starts_word (end))
4582 if (!gtk_text_iter_forward_char (end))
4587 case ATK_TEXT_BOUNDARY_WORD_END:
4588 if (gtk_text_iter_inside_word (start) &&
4589 !gtk_text_iter_starts_word (start))
4590 gtk_text_iter_backward_word_start (start);
4591 while (!gtk_text_iter_ends_word (start))
4593 if (!gtk_text_iter_backward_char (start))
4596 gtk_text_iter_forward_word_end (end);
4599 case ATK_TEXT_BOUNDARY_SENTENCE_START:
4600 if (!gtk_text_iter_starts_sentence (start))
4601 gtk_text_iter_backward_sentence_start (start);
4602 if (gtk_text_iter_inside_sentence (end))
4603 gtk_text_iter_forward_sentence_end (end);
4604 while (!gtk_text_iter_starts_sentence (end))
4606 if (!gtk_text_iter_forward_char (end))
4611 case ATK_TEXT_BOUNDARY_SENTENCE_END:
4612 if (gtk_text_iter_inside_sentence (start) &&
4613 !gtk_text_iter_starts_sentence (start))
4614 gtk_text_iter_backward_sentence_start (start);
4615 while (!gtk_text_iter_ends_sentence (start))
4617 if (!gtk_text_iter_backward_char (start))
4620 gtk_text_iter_forward_sentence_end (end);
4623 case ATK_TEXT_BOUNDARY_LINE_START:
4624 line_number = gtk_text_iter_get_line (start);
4625 if (line_number == 0)
4627 gtk_text_buffer_get_iter_at_offset (buffer, start, 0);
4631 gtk_text_iter_backward_line (start);
4632 gtk_text_iter_forward_line (start);
4634 gtk_text_iter_forward_line (end);
4637 case ATK_TEXT_BOUNDARY_LINE_END:
4638 line_number = gtk_text_iter_get_line (start);
4639 if (line_number == 0)
4641 gtk_text_buffer_get_iter_at_offset (buffer, start, 0);
4645 gtk_text_iter_backward_line (start);
4646 gtk_text_iter_forward_line (start);
4648 while (!gtk_text_iter_ends_line (start))
4650 if (!gtk_text_iter_backward_char (start))
4653 gtk_text_iter_forward_to_line_end (end);
4659 _gtk_text_buffer_get_text_after (GtkTextBuffer *buffer,
4660 AtkTextBoundary boundary_type,
4661 GtkTextIter *position,
4668 switch (boundary_type)
4670 case ATK_TEXT_BOUNDARY_CHAR:
4671 gtk_text_iter_forward_char (start);
4672 gtk_text_iter_forward_chars (end, 2);
4675 case ATK_TEXT_BOUNDARY_WORD_START:
4676 if (gtk_text_iter_inside_word (end))
4677 gtk_text_iter_forward_word_end (end);
4678 while (!gtk_text_iter_starts_word (end))
4680 if (!gtk_text_iter_forward_char (end))
4684 if (!gtk_text_iter_is_end (end))
4686 gtk_text_iter_forward_word_end (end);
4687 while (!gtk_text_iter_starts_word (end))
4689 if (!gtk_text_iter_forward_char (end))
4695 case ATK_TEXT_BOUNDARY_WORD_END:
4696 gtk_text_iter_forward_word_end (end);
4698 if (!gtk_text_iter_is_end (end))
4699 gtk_text_iter_forward_word_end (end);
4702 case ATK_TEXT_BOUNDARY_SENTENCE_START:
4703 if (gtk_text_iter_inside_sentence (end))
4704 gtk_text_iter_forward_sentence_end (end);
4705 while (!gtk_text_iter_starts_sentence (end))
4707 if (!gtk_text_iter_forward_char (end))
4711 if (!gtk_text_iter_is_end (end))
4713 gtk_text_iter_forward_sentence_end (end);
4714 while (!gtk_text_iter_starts_sentence (end))
4716 if (!gtk_text_iter_forward_char (end))
4722 case ATK_TEXT_BOUNDARY_SENTENCE_END:
4723 gtk_text_iter_forward_sentence_end (end);
4725 if (!gtk_text_iter_is_end (end))
4726 gtk_text_iter_forward_sentence_end (end);
4729 case ATK_TEXT_BOUNDARY_LINE_START:
4730 gtk_text_iter_forward_line (end);
4732 gtk_text_iter_forward_line (end);
4735 case ATK_TEXT_BOUNDARY_LINE_END:
4736 gtk_text_iter_forward_line (start);
4738 if (!gtk_text_iter_is_end (start))
4740 while (!gtk_text_iter_ends_line (start))
4742 if (!gtk_text_iter_backward_char (start))
4745 gtk_text_iter_forward_to_line_end (end);