1 /* GTK - The GIMP Toolkit
2 * gtktextbuffer.c Copyright (C) 2000 Red Hat, Inc.
3 * Copyright (C) 2004 Nokia Corporation
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
22 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
23 * file for a list of people on the GTK+ Team. See the ChangeLog
24 * files for a list of changes. These files are distributed with
25 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
32 #define GTK_TEXT_USE_INTERNAL_UNSUPPORTED_API
33 #include "gtkclipboard.h"
35 #include "gtkinvisible.h"
36 #include "gtkmarshalers.h"
37 #include "gtktextbuffer.h"
38 #include "gtktextbufferrichtext.h"
39 #include "gtktextbtree.h"
40 #include "gtktextiterprivate.h"
41 #include "gtkprivate.h"
46 #define GTK_TEXT_BUFFER_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_TEXT_BUFFER, GtkTextBufferPrivate))
48 typedef struct _GtkTextBufferPrivate GtkTextBufferPrivate;
50 struct _GtkTextBufferPrivate
52 GtkTargetList *copy_target_list;
53 GtkTargetEntry *copy_target_entries;
54 gint n_copy_target_entries;
56 GtkTargetList *paste_target_list;
57 GtkTargetEntry *paste_target_entries;
58 gint n_paste_target_entries;
62 typedef struct _ClipboardRequest ClipboardRequest;
64 struct _ClipboardRequest
66 GtkTextBuffer *buffer;
68 gboolean default_editable;
69 gboolean is_clipboard;
70 gboolean replace_selection;
99 PROP_COPY_TARGET_LIST,
100 PROP_PASTE_TARGET_LIST
103 static void gtk_text_buffer_finalize (GObject *object);
105 static void gtk_text_buffer_real_insert_text (GtkTextBuffer *buffer,
109 static void gtk_text_buffer_real_insert_pixbuf (GtkTextBuffer *buffer,
112 static void gtk_text_buffer_real_insert_anchor (GtkTextBuffer *buffer,
114 GtkTextChildAnchor *anchor);
115 static void gtk_text_buffer_real_delete_range (GtkTextBuffer *buffer,
118 static void gtk_text_buffer_real_apply_tag (GtkTextBuffer *buffer,
120 const GtkTextIter *start_char,
121 const GtkTextIter *end_char);
122 static void gtk_text_buffer_real_remove_tag (GtkTextBuffer *buffer,
124 const GtkTextIter *start_char,
125 const GtkTextIter *end_char);
126 static void gtk_text_buffer_real_changed (GtkTextBuffer *buffer);
127 static void gtk_text_buffer_real_mark_set (GtkTextBuffer *buffer,
128 const GtkTextIter *iter,
131 static GtkTextBTree* get_btree (GtkTextBuffer *buffer);
132 static void free_log_attr_cache (GtkTextLogAttrCache *cache);
134 static void remove_all_selection_clipboards (GtkTextBuffer *buffer);
135 static void update_selection_clipboards (GtkTextBuffer *buffer);
137 static GtkTextBuffer *create_clipboard_contents_buffer (GtkTextBuffer *buffer);
139 static void gtk_text_buffer_free_target_lists (GtkTextBuffer *buffer);
141 static guint signals[LAST_SIGNAL] = { 0 };
143 static void gtk_text_buffer_set_property (GObject *object,
147 static void gtk_text_buffer_get_property (GObject *object,
151 static void gtk_text_buffer_notify (GObject *object,
154 G_DEFINE_TYPE (GtkTextBuffer, gtk_text_buffer, G_TYPE_OBJECT)
157 gtk_text_buffer_class_init (GtkTextBufferClass *klass)
159 GObjectClass *object_class = G_OBJECT_CLASS (klass);
161 object_class->finalize = gtk_text_buffer_finalize;
162 object_class->set_property = gtk_text_buffer_set_property;
163 object_class->get_property = gtk_text_buffer_get_property;
164 object_class->notify = gtk_text_buffer_notify;
166 klass->insert_text = gtk_text_buffer_real_insert_text;
167 klass->insert_pixbuf = gtk_text_buffer_real_insert_pixbuf;
168 klass->insert_child_anchor = gtk_text_buffer_real_insert_anchor;
169 klass->delete_range = gtk_text_buffer_real_delete_range;
170 klass->apply_tag = gtk_text_buffer_real_apply_tag;
171 klass->remove_tag = gtk_text_buffer_real_remove_tag;
172 klass->changed = gtk_text_buffer_real_changed;
173 klass->mark_set = gtk_text_buffer_real_mark_set;
176 g_object_class_install_property (object_class,
178 g_param_spec_object ("tag-table",
180 P_("Text Tag Table"),
181 GTK_TYPE_TEXT_TAG_TABLE,
182 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
184 /* Normal properties*/
187 * GtkTextBuffer:text:
189 * The text content of the buffer. Without child widgets and images,
190 * see gtk_text_buffer_get_text() for more information.
194 g_object_class_install_property (object_class,
196 g_param_spec_string ("text",
198 P_("Current text of the buffer"),
200 GTK_PARAM_READWRITE));
203 * GtkTextBuffer:has-selection:
205 * Whether the buffer has some text currently selected.
209 g_object_class_install_property (object_class,
211 g_param_spec_boolean ("has-selection",
213 P_("Whether the buffer has some text currently selected"),
215 GTK_PARAM_READABLE));
218 * GtkTextBuffer:cursor-position:
220 * The position of the insert mark (as offset from the beginning
221 * of the buffer). It is useful for getting notified when the
226 g_object_class_install_property (object_class,
227 PROP_CURSOR_POSITION,
228 g_param_spec_int ("cursor-position",
229 P_("Cursor position"),
230 P_("The position of the insert mark (as offset from the beginning of the buffer)"),
232 GTK_PARAM_READABLE));
235 * GtkTextBuffer:copy-target-list:
237 * The list of targets this buffer supports for clipboard copying
242 g_object_class_install_property (object_class,
243 PROP_COPY_TARGET_LIST,
244 g_param_spec_boxed ("copy-target-list",
245 P_("Copy target list"),
246 P_("The list of targets this buffer supports for clipboard copying and DND source"),
247 GTK_TYPE_TARGET_LIST,
248 GTK_PARAM_READABLE));
251 * GtkTextBuffer:paste-target-list:
253 * The list of targets this buffer supports for clipboard pasting
254 * and as DND destination.
258 g_object_class_install_property (object_class,
259 PROP_PASTE_TARGET_LIST,
260 g_param_spec_boxed ("paste-target-list",
261 P_("Paste target list"),
262 P_("The list of targets this buffer supports for clipboard pasting and DND destination"),
263 GTK_TYPE_TARGET_LIST,
264 GTK_PARAM_READABLE));
267 * GtkTextBuffer::insert-text:
268 * @textbuffer: the object which received the signal
269 * @location: position to insert @text in @textbuffer
270 * @text: the UTF-8 text to be inserted
271 * @len: length of the inserted text in bytes
273 * The insert_text signal is emitted to insert text in a #GtkTextBuffer.
274 * Insertion actually occurs in the default handler.
276 * Note that if your handler runs before the default handler it must not
277 * invalidate the @location iter (or has to revalidate it).
278 * The default signal handler revalidates it to point to the end of the
282 * gtk_text_buffer_insert(),
283 * gtk_text_buffer_insert_range().
285 signals[INSERT_TEXT] =
286 g_signal_new (I_("insert-text"),
287 G_OBJECT_CLASS_TYPE (object_class),
289 G_STRUCT_OFFSET (GtkTextBufferClass, insert_text),
291 _gtk_marshal_VOID__BOXED_STRING_INT,
294 GTK_TYPE_TEXT_ITER | G_SIGNAL_TYPE_STATIC_SCOPE,
295 G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE,
299 * GtkTextBuffer::insert-pixbuf:
300 * @textbuffer: the object which received the signal
301 * @location: position to insert @pixbuf in @textbuffer
302 * @pixbuf: the #GdkPixbuf to be inserted
304 * The insert_pixbuf signal is emitted to insert a #GdkPixbuf
305 * in a #GtkTextBuffer. Insertion actually occurs in the default handler.
307 * Note that if your handler runs before the default handler it must not
308 * invalidate the @location iter (or has to revalidate it).
309 * The default signal handler revalidates it to be placed after the
312 * See also: gtk_text_buffer_insert_pixbuf().
314 signals[INSERT_PIXBUF] =
315 g_signal_new (I_("insert-pixbuf"),
316 G_OBJECT_CLASS_TYPE (object_class),
318 G_STRUCT_OFFSET (GtkTextBufferClass, insert_pixbuf),
320 _gtk_marshal_VOID__BOXED_OBJECT,
323 GTK_TYPE_TEXT_ITER | G_SIGNAL_TYPE_STATIC_SCOPE,
328 * GtkTextBuffer::insert-child-anchor:
329 * @textbuffer: the object which received the signal
330 * @location: position to insert @anchor in @textbuffer
331 * @anchor: the #GtkTextChildAnchor to be inserted
333 * The insert_child_anchor signal is emitted to insert a
334 * #GtkTextChildAnchor in a #GtkTextBuffer.
335 * Insertion actually occurs in the default handler.
337 * Note that if your handler runs before the default handler it must
338 * not invalidate the @location iter (or has to revalidate it).
339 * The default signal handler revalidates it to be placed after the
342 * See also: gtk_text_buffer_insert_child_anchor().
344 signals[INSERT_CHILD_ANCHOR] =
345 g_signal_new (I_("insert-child-anchor"),
346 G_OBJECT_CLASS_TYPE (object_class),
348 G_STRUCT_OFFSET (GtkTextBufferClass, insert_child_anchor),
350 _gtk_marshal_VOID__BOXED_OBJECT,
353 GTK_TYPE_TEXT_ITER | G_SIGNAL_TYPE_STATIC_SCOPE,
354 GTK_TYPE_TEXT_CHILD_ANCHOR);
357 * GtkTextBuffer::delete-range:
358 * @textbuffer: the object which received the signal
359 * @start: the start of the range to be deleted
360 * @end: the end of the range to be deleted
362 * The delete_range signal is emitted to delete a range
363 * from a #GtkTextBuffer.
365 * Note that if your handler runs before the default handler it must not
366 * invalidate the @start and @end iters (or has to revalidate them).
367 * The default signal handler revalidates the @start and @end iters to
368 * both point point to the location where text was deleted. Handlers
369 * which run after the default handler (see g_signal_connect_after())
370 * do not have access to the deleted text.
372 * See also: gtk_text_buffer_delete().
374 signals[DELETE_RANGE] =
375 g_signal_new (I_("delete-range"),
376 G_OBJECT_CLASS_TYPE (object_class),
378 G_STRUCT_OFFSET (GtkTextBufferClass, delete_range),
380 _gtk_marshal_VOID__BOXED_BOXED,
383 GTK_TYPE_TEXT_ITER | G_SIGNAL_TYPE_STATIC_SCOPE,
384 GTK_TYPE_TEXT_ITER | G_SIGNAL_TYPE_STATIC_SCOPE);
387 * GtkTextBuffer::changed:
388 * @textbuffer: the object which received the signal
390 * The changed signal is emitted when the content of a #GtkTextBuffer
394 g_signal_new (I_("changed"),
395 G_OBJECT_CLASS_TYPE (object_class),
397 G_STRUCT_OFFSET (GtkTextBufferClass, changed),
399 _gtk_marshal_VOID__VOID,
404 * GtkTextBuffer::modified-changed:
405 * @textbuffer: the object which received the signal
407 * The modified_changed signal is emitted when the modified bit of a
408 * #GtkTextBuffer flips.
411 * gtk_text_buffer_set_modified().
413 signals[MODIFIED_CHANGED] =
414 g_signal_new (I_("modified-changed"),
415 G_OBJECT_CLASS_TYPE (object_class),
417 G_STRUCT_OFFSET (GtkTextBufferClass, modified_changed),
419 _gtk_marshal_VOID__VOID,
424 * GtkTextBuffer::mark-set:
425 * @textbuffer: the object which received the signal
426 * @location: The location of @mark in @textbuffer
427 * @mark: The mark that is set
429 * The mark_set signal is emitted as notification
430 * after a #GtkTextMark is set.
433 * gtk_text_buffer_create_mark(),
434 * gtk_text_buffer_move_mark().
437 g_signal_new (I_("mark-set"),
438 G_OBJECT_CLASS_TYPE (object_class),
440 G_STRUCT_OFFSET (GtkTextBufferClass, mark_set),
442 _gtk_marshal_VOID__BOXED_OBJECT,
449 * GtkTextBuffer::mark-deleted:
450 * @textbuffer: the object which received the signal
451 * @mark: The mark that was deleted
453 * The mark_deleted signal is emitted as notification
454 * after a #GtkTextMark is deleted.
457 * gtk_text_buffer_delete_mark().
459 signals[MARK_DELETED] =
460 g_signal_new (I_("mark-deleted"),
461 G_OBJECT_CLASS_TYPE (object_class),
463 G_STRUCT_OFFSET (GtkTextBufferClass, mark_deleted),
465 _gtk_marshal_VOID__OBJECT,
471 * GtkTextBuffer::apply-tag:
472 * @textbuffer: the object which received the signal
473 * @tag: the applied tag
474 * @start: the start of the range the tag is applied to
475 * @end: the end of the range the tag is applied to
477 * The apply_tag signal is emitted to apply a tag to a
478 * range of text in a #GtkTextBuffer.
479 * Applying actually occurs in the default handler.
481 * Note that if your handler runs before the default handler it must not
482 * invalidate the @start and @end iters (or has to revalidate them).
485 * gtk_text_buffer_apply_tag(),
486 * gtk_text_buffer_insert_with_tags(),
487 * gtk_text_buffer_insert_range().
490 g_signal_new (I_("apply-tag"),
491 G_OBJECT_CLASS_TYPE (object_class),
493 G_STRUCT_OFFSET (GtkTextBufferClass, apply_tag),
495 _gtk_marshal_VOID__OBJECT_BOXED_BOXED,
504 * GtkTextBuffer::remove-tag:
505 * @textbuffer: the object which received the signal
506 * @tag: the tag to be removed
507 * @start: the start of the range the tag is removed from
508 * @end: the end of the range the tag is removed from
510 * The remove_tag signal is emitted to remove all occurrences of @tag from a
511 * range of text in a #GtkTextBuffer.
512 * Removal actually occurs in the default handler.
514 * Note that if your handler runs before the default handler it must not
515 * invalidate the @start and @end iters (or has to revalidate them).
518 * gtk_text_buffer_remove_tag().
520 signals[REMOVE_TAG] =
521 g_signal_new (I_("remove-tag"),
522 G_OBJECT_CLASS_TYPE (object_class),
524 G_STRUCT_OFFSET (GtkTextBufferClass, remove_tag),
526 _gtk_marshal_VOID__OBJECT_BOXED_BOXED,
534 * GtkTextBuffer::begin-user-action:
535 * @textbuffer: the object which received the signal
537 * The begin_user_action signal is emitted at the beginning of a single
538 * user-visible operation on a #GtkTextBuffer.
541 * gtk_text_buffer_begin_user_action(),
542 * gtk_text_buffer_insert_interactive(),
543 * gtk_text_buffer_insert_range_interactive(),
544 * gtk_text_buffer_delete_interactive(),
545 * gtk_text_buffer_backspace(),
546 * gtk_text_buffer_delete_selection().
548 signals[BEGIN_USER_ACTION] =
549 g_signal_new (I_("begin-user-action"),
550 G_OBJECT_CLASS_TYPE (object_class),
552 G_STRUCT_OFFSET (GtkTextBufferClass, begin_user_action),
554 _gtk_marshal_VOID__VOID,
559 * GtkTextBuffer::end-user-action:
560 * @textbuffer: the object which received the signal
562 * The end_user_action signal is emitted at the end of a single
563 * user-visible operation #GtkTextBuffer.
566 * gtk_text_buffer_end_user_action(),
567 * gtk_text_buffer_insert_interactive(),
568 * gtk_text_buffer_insert_range_interactive(),
569 * gtk_text_buffer_delete_interactive(),
570 * gtk_text_buffer_backspace(),
571 * gtk_text_buffer_delete_selection(),
572 * gtk_text_buffer_backspace().
574 signals[END_USER_ACTION] =
575 g_signal_new (I_("end-user-action"),
576 G_OBJECT_CLASS_TYPE (object_class),
578 G_STRUCT_OFFSET (GtkTextBufferClass, end_user_action),
580 _gtk_marshal_VOID__VOID,
584 g_type_class_add_private (object_class, sizeof (GtkTextBufferPrivate));
588 gtk_text_buffer_init (GtkTextBuffer *buffer)
590 buffer->clipboard_contents_buffers = NULL;
591 buffer->tag_table = NULL;
593 /* allow copying of arbiatray stuff in the internal rich text format */
594 gtk_text_buffer_register_serialize_tagset (buffer, NULL);
598 set_table (GtkTextBuffer *buffer, GtkTextTagTable *table)
600 g_return_if_fail (buffer->tag_table == NULL);
604 buffer->tag_table = table;
605 g_object_ref (buffer->tag_table);
606 _gtk_text_tag_table_add_buffer (table, buffer);
610 static GtkTextTagTable*
611 get_table (GtkTextBuffer *buffer)
613 if (buffer->tag_table == NULL)
615 buffer->tag_table = gtk_text_tag_table_new ();
616 _gtk_text_tag_table_add_buffer (buffer->tag_table, buffer);
619 return buffer->tag_table;
623 gtk_text_buffer_set_property (GObject *object,
628 GtkTextBuffer *text_buffer;
630 text_buffer = GTK_TEXT_BUFFER (object);
635 set_table (text_buffer, g_value_get_object (value));
639 gtk_text_buffer_set_text (text_buffer,
640 g_value_get_string (value), -1);
644 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
650 gtk_text_buffer_get_property (GObject *object,
655 GtkTextBuffer *text_buffer;
658 text_buffer = GTK_TEXT_BUFFER (object);
663 g_value_set_object (value, get_table (text_buffer));
668 GtkTextIter start, end;
670 gtk_text_buffer_get_start_iter (text_buffer, &start);
671 gtk_text_buffer_get_end_iter (text_buffer, &end);
673 g_value_take_string (value,
674 gtk_text_buffer_get_text (text_buffer,
675 &start, &end, FALSE));
679 case PROP_HAS_SELECTION:
680 g_value_set_boolean (value, text_buffer->has_selection);
683 case PROP_CURSOR_POSITION:
684 gtk_text_buffer_get_iter_at_mark (text_buffer, &iter,
685 gtk_text_buffer_get_insert (text_buffer));
686 g_value_set_int (value, gtk_text_iter_get_offset (&iter));
689 case PROP_COPY_TARGET_LIST:
690 g_value_set_boxed (value, gtk_text_buffer_get_copy_target_list (text_buffer));
693 case PROP_PASTE_TARGET_LIST:
694 g_value_set_boxed (value, gtk_text_buffer_get_paste_target_list (text_buffer));
698 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
704 gtk_text_buffer_notify (GObject *object,
707 if (!strcmp (pspec->name, "copy-target-list") ||
708 !strcmp (pspec->name, "paste-target-list"))
710 gtk_text_buffer_free_target_lists (GTK_TEXT_BUFFER (object));
715 * gtk_text_buffer_new:
716 * @table: a tag table, or %NULL to create a new one
718 * Creates a new text buffer.
720 * Return value: a new text buffer
723 gtk_text_buffer_new (GtkTextTagTable *table)
725 GtkTextBuffer *text_buffer;
727 text_buffer = g_object_new (GTK_TYPE_TEXT_BUFFER, "tag-table", table, NULL);
733 gtk_text_buffer_finalize (GObject *object)
735 GtkTextBuffer *buffer;
737 buffer = GTK_TEXT_BUFFER (object);
739 remove_all_selection_clipboards (buffer);
741 if (buffer->tag_table)
743 _gtk_text_tag_table_remove_buffer (buffer->tag_table, buffer);
744 g_object_unref (buffer->tag_table);
745 buffer->tag_table = NULL;
750 _gtk_text_btree_unref (buffer->btree);
751 buffer->btree = NULL;
754 if (buffer->log_attr_cache)
755 free_log_attr_cache (buffer->log_attr_cache);
757 buffer->log_attr_cache = NULL;
759 gtk_text_buffer_free_target_lists (buffer);
761 G_OBJECT_CLASS (gtk_text_buffer_parent_class)->finalize (object);
765 get_btree (GtkTextBuffer *buffer)
767 if (buffer->btree == NULL)
768 buffer->btree = _gtk_text_btree_new (gtk_text_buffer_get_tag_table (buffer),
771 return buffer->btree;
775 _gtk_text_buffer_get_btree (GtkTextBuffer *buffer)
777 return get_btree (buffer);
781 * gtk_text_buffer_get_tag_table:
782 * @buffer: a #GtkTextBuffer
784 * Get the #GtkTextTagTable associated with this buffer.
786 * Return value: the buffer's tag table
789 gtk_text_buffer_get_tag_table (GtkTextBuffer *buffer)
791 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
793 return get_table (buffer);
797 * gtk_text_buffer_set_text:
798 * @buffer: a #GtkTextBuffer
799 * @text: UTF-8 text to insert
800 * @len: length of @text in bytes
802 * Deletes current contents of @buffer, and inserts @text instead. If
803 * @len is -1, @text must be nul-terminated. @text must be valid UTF-8.
806 gtk_text_buffer_set_text (GtkTextBuffer *buffer,
810 GtkTextIter start, end;
812 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
813 g_return_if_fail (text != NULL);
818 gtk_text_buffer_get_bounds (buffer, &start, &end);
820 gtk_text_buffer_delete (buffer, &start, &end);
824 gtk_text_buffer_get_iter_at_offset (buffer, &start, 0);
825 gtk_text_buffer_insert (buffer, &start, text, len);
828 g_object_notify (G_OBJECT (buffer), "text");
838 gtk_text_buffer_real_insert_text (GtkTextBuffer *buffer,
843 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
844 g_return_if_fail (iter != NULL);
846 _gtk_text_btree_insert (iter, text, len);
848 g_signal_emit (buffer, signals[CHANGED], 0);
849 g_object_notify (G_OBJECT (buffer), "cursor-position");
853 gtk_text_buffer_emit_insert (GtkTextBuffer *buffer,
858 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
859 g_return_if_fail (iter != NULL);
860 g_return_if_fail (text != NULL);
865 g_return_if_fail (g_utf8_validate (text, len, NULL));
869 g_signal_emit (buffer, signals[INSERT_TEXT], 0,
875 * gtk_text_buffer_insert:
876 * @buffer: a #GtkTextBuffer
877 * @iter: a position in the buffer
878 * @text: UTF-8 format text to insert
879 * @len: length of text in bytes, or -1
881 * Inserts @len bytes of @text at position @iter. If @len is -1,
882 * @text must be nul-terminated and will be inserted in its
883 * entirety. Emits the "insert_text" signal; insertion actually occurs
884 * in the default handler for the signal. @iter is invalidated when
885 * insertion occurs (because the buffer contents change), but the
886 * default signal handler revalidates it to point to the end of the
890 gtk_text_buffer_insert (GtkTextBuffer *buffer,
895 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
896 g_return_if_fail (iter != NULL);
897 g_return_if_fail (text != NULL);
898 g_return_if_fail (gtk_text_iter_get_buffer (iter) == buffer);
900 gtk_text_buffer_emit_insert (buffer, iter, text, len);
904 * gtk_text_buffer_insert_at_cursor:
905 * @buffer: a #GtkTextBuffer
906 * @text: some text in UTF-8 format
907 * @len: length of text, in bytes
909 * Simply calls gtk_text_buffer_insert(), using the current
910 * cursor position as the insertion point.
913 gtk_text_buffer_insert_at_cursor (GtkTextBuffer *buffer,
919 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
920 g_return_if_fail (text != NULL);
922 gtk_text_buffer_get_iter_at_mark (buffer, &iter,
923 gtk_text_buffer_get_insert (buffer));
925 gtk_text_buffer_insert (buffer, &iter, text, len);
929 * gtk_text_buffer_insert_interactive:
930 * @buffer: a #GtkTextBuffer
931 * @iter: a position in @buffer
932 * @text: some UTF-8 text
933 * @len: length of text in bytes, or -1
934 * @default_editable: default editability of buffer
936 * Like gtk_text_buffer_insert(), but the insertion will not occur if
937 * @iter is at a non-editable location in the buffer. Usually you
938 * want to prevent insertions at ineditable locations if the insertion
939 * results from a user action (is interactive).
941 * @default_editable indicates the editability of text that doesn't
942 * have a tag affecting editability applied to it. Typically the
943 * result of gtk_text_view_get_editable() is appropriate here.
945 * Return value: whether text was actually inserted
948 gtk_text_buffer_insert_interactive (GtkTextBuffer *buffer,
952 gboolean default_editable)
954 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
955 g_return_val_if_fail (text != NULL, FALSE);
956 g_return_val_if_fail (gtk_text_iter_get_buffer (iter) == buffer, FALSE);
958 if (gtk_text_iter_can_insert (iter, default_editable))
960 gtk_text_buffer_begin_user_action (buffer);
961 gtk_text_buffer_emit_insert (buffer, iter, text, len);
962 gtk_text_buffer_end_user_action (buffer);
970 * gtk_text_buffer_insert_interactive_at_cursor:
971 * @buffer: a #GtkTextBuffer
972 * @text: text in UTF-8 format
973 * @len: length of text in bytes, or -1
974 * @default_editable: default editability of buffer
976 * Calls gtk_text_buffer_insert_interactive() at the cursor
979 * @default_editable indicates the editability of text that doesn't
980 * have a tag affecting editability applied to it. Typically the
981 * result of gtk_text_view_get_editable() is appropriate here.
983 * Return value: whether text was actually inserted
986 gtk_text_buffer_insert_interactive_at_cursor (GtkTextBuffer *buffer,
989 gboolean default_editable)
993 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
994 g_return_val_if_fail (text != NULL, FALSE);
996 gtk_text_buffer_get_iter_at_mark (buffer, &iter,
997 gtk_text_buffer_get_insert (buffer));
999 return gtk_text_buffer_insert_interactive (buffer, &iter, text, len,
1004 possibly_not_text (gunichar ch,
1007 return ch == GTK_TEXT_UNKNOWN_CHAR;
1011 insert_text_range (GtkTextBuffer *buffer,
1013 const GtkTextIter *orig_start,
1014 const GtkTextIter *orig_end,
1015 gboolean interactive)
1019 text = gtk_text_iter_get_text (orig_start, orig_end);
1021 gtk_text_buffer_emit_insert (buffer, iter, text, -1);
1026 typedef struct _Range Range;
1029 GtkTextBuffer *buffer;
1030 GtkTextMark *start_mark;
1031 GtkTextMark *end_mark;
1032 GtkTextMark *whole_end_mark;
1033 GtkTextIter *range_start;
1034 GtkTextIter *range_end;
1035 GtkTextIter *whole_end;
1039 save_range (GtkTextIter *range_start,
1040 GtkTextIter *range_end,
1041 GtkTextIter *whole_end)
1045 r = g_new (Range, 1);
1047 r->buffer = gtk_text_iter_get_buffer (range_start);
1048 g_object_ref (r->buffer);
1051 gtk_text_buffer_create_mark (gtk_text_iter_get_buffer (range_start),
1056 gtk_text_buffer_create_mark (gtk_text_iter_get_buffer (range_start),
1062 gtk_text_buffer_create_mark (gtk_text_iter_get_buffer (range_start),
1067 r->range_start = range_start;
1068 r->range_end = range_end;
1069 r->whole_end = whole_end;
1075 restore_range (Range *r)
1077 gtk_text_buffer_get_iter_at_mark (r->buffer,
1081 gtk_text_buffer_get_iter_at_mark (r->buffer,
1085 gtk_text_buffer_get_iter_at_mark (r->buffer,
1089 gtk_text_buffer_delete_mark (r->buffer, r->start_mark);
1090 gtk_text_buffer_delete_mark (r->buffer, r->end_mark);
1091 gtk_text_buffer_delete_mark (r->buffer, r->whole_end_mark);
1093 /* Due to the gravities on the marks, the ordering could have
1094 * gotten mangled; we switch to an empty range in that
1098 if (gtk_text_iter_compare (r->range_start, r->range_end) > 0)
1099 *r->range_start = *r->range_end;
1101 if (gtk_text_iter_compare (r->range_end, r->whole_end) > 0)
1102 *r->range_end = *r->whole_end;
1104 g_object_unref (r->buffer);
1109 insert_range_untagged (GtkTextBuffer *buffer,
1111 const GtkTextIter *orig_start,
1112 const GtkTextIter *orig_end,
1113 gboolean interactive)
1115 GtkTextIter range_start;
1116 GtkTextIter range_end;
1117 GtkTextIter start, end;
1120 if (gtk_text_iter_equal (orig_start, orig_end))
1123 start = *orig_start;
1126 range_start = start;
1131 if (gtk_text_iter_equal (&range_start, &range_end))
1133 /* Figure out how to move forward */
1135 g_assert (gtk_text_iter_compare (&range_end, &end) <= 0);
1137 if (gtk_text_iter_equal (&range_end, &end))
1139 /* nothing left to do */
1142 else if (gtk_text_iter_get_char (&range_end) == GTK_TEXT_UNKNOWN_CHAR)
1144 GdkPixbuf *pixbuf = NULL;
1145 GtkTextChildAnchor *anchor = NULL;
1146 pixbuf = gtk_text_iter_get_pixbuf (&range_end);
1147 anchor = gtk_text_iter_get_child_anchor (&range_end);
1151 r = save_range (&range_start,
1155 gtk_text_buffer_insert_pixbuf (buffer,
1162 gtk_text_iter_forward_char (&range_end);
1164 range_start = range_end;
1168 /* Just skip anchors */
1170 gtk_text_iter_forward_char (&range_end);
1171 range_start = range_end;
1175 /* The GTK_TEXT_UNKNOWN_CHAR was in a text segment, so
1178 gtk_text_iter_forward_find_char (&range_end,
1179 possibly_not_text, NULL,
1182 g_assert (gtk_text_iter_compare (&range_end, &end) <= 0);
1187 /* Text segment starts here, so forward search to
1188 * find its possible endpoint
1190 gtk_text_iter_forward_find_char (&range_end,
1191 possibly_not_text, NULL,
1194 g_assert (gtk_text_iter_compare (&range_end, &end) <= 0);
1199 r = save_range (&range_start,
1203 insert_text_range (buffer,
1212 range_start = range_end;
1218 insert_range_not_inside_self (GtkTextBuffer *buffer,
1220 const GtkTextIter *orig_start,
1221 const GtkTextIter *orig_end,
1222 gboolean interactive)
1224 /* Find each range of uniformly-tagged text, insert it,
1225 * then apply the tags.
1227 GtkTextIter start = *orig_start;
1228 GtkTextIter end = *orig_end;
1229 GtkTextIter range_start;
1230 GtkTextIter range_end;
1232 if (gtk_text_iter_equal (orig_start, orig_end))
1235 gtk_text_iter_order (&start, &end);
1237 range_start = start;
1243 GtkTextIter start_iter;
1248 if (gtk_text_iter_equal (&range_start, &end))
1249 break; /* All done */
1251 g_assert (gtk_text_iter_compare (&range_start, &end) < 0);
1253 gtk_text_iter_forward_to_tag_toggle (&range_end, NULL);
1255 g_assert (!gtk_text_iter_equal (&range_start, &range_end));
1257 /* Clamp to the end iterator */
1258 if (gtk_text_iter_compare (&range_end, &end) > 0)
1261 /* We have a range with unique tags; insert it, and
1264 start_offset = gtk_text_iter_get_offset (iter);
1266 r = save_range (&range_start, &range_end, &end);
1268 insert_range_untagged (buffer, iter, &range_start, &range_end, interactive);
1273 gtk_text_buffer_get_iter_at_offset (buffer, &start_iter, start_offset);
1275 tags = gtk_text_iter_get_tags (&range_start);
1277 while (tmp_list != NULL)
1279 gtk_text_buffer_apply_tag (buffer,
1284 tmp_list = g_slist_next (tmp_list);
1286 g_slist_free (tags);
1288 range_start = range_end;
1293 gtk_text_buffer_real_insert_range (GtkTextBuffer *buffer,
1295 const GtkTextIter *orig_start,
1296 const GtkTextIter *orig_end,
1297 gboolean interactive)
1299 GtkTextBuffer *src_buffer;
1301 /* Find each range of uniformly-tagged text, insert it,
1302 * then apply the tags.
1304 if (gtk_text_iter_equal (orig_start, orig_end))
1308 gtk_text_buffer_begin_user_action (buffer);
1310 src_buffer = gtk_text_iter_get_buffer (orig_start);
1312 if (gtk_text_iter_get_buffer (iter) != src_buffer ||
1313 !gtk_text_iter_in_range (iter, orig_start, orig_end))
1315 insert_range_not_inside_self (buffer, iter, orig_start, orig_end, interactive);
1319 /* If you insert a range into itself, it could loop infinitely
1320 * because the region being copied keeps growing as we insert. So
1321 * we have to separately copy the range before and after
1322 * the insertion point.
1324 GtkTextIter start = *orig_start;
1325 GtkTextIter end = *orig_end;
1326 GtkTextIter range_start;
1327 GtkTextIter range_end;
1331 gtk_text_iter_order (&start, &end);
1333 range_start = start;
1335 first_half = save_range (&range_start, &range_end, &end);
1337 range_start = *iter;
1339 second_half = save_range (&range_start, &range_end, &end);
1341 restore_range (first_half);
1342 insert_range_not_inside_self (buffer, iter, &range_start, &range_end, interactive);
1344 restore_range (second_half);
1345 insert_range_not_inside_self (buffer, iter, &range_start, &range_end, interactive);
1349 gtk_text_buffer_end_user_action (buffer);
1353 * gtk_text_buffer_insert_range:
1354 * @buffer: a #GtkTextBuffer
1355 * @iter: a position in @buffer
1356 * @start: a position in a #GtkTextBuffer
1357 * @end: another position in the same buffer as @start
1359 * Copies text, tags, and pixbufs between @start and @end (the order
1360 * of @start and @end doesn't matter) and inserts the copy at @iter.
1361 * Used instead of simply getting/inserting text because it preserves
1362 * images and tags. If @start and @end are in a different buffer from
1363 * @buffer, the two buffers must share the same tag table.
1365 * Implemented via emissions of the insert_text and apply_tag signals,
1369 gtk_text_buffer_insert_range (GtkTextBuffer *buffer,
1371 const GtkTextIter *start,
1372 const GtkTextIter *end)
1374 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1375 g_return_if_fail (iter != NULL);
1376 g_return_if_fail (start != NULL);
1377 g_return_if_fail (end != NULL);
1378 g_return_if_fail (gtk_text_iter_get_buffer (start) ==
1379 gtk_text_iter_get_buffer (end));
1380 g_return_if_fail (gtk_text_iter_get_buffer (start)->tag_table ==
1382 g_return_if_fail (gtk_text_iter_get_buffer (iter) == buffer);
1384 gtk_text_buffer_real_insert_range (buffer, iter, start, end, FALSE);
1388 * gtk_text_buffer_insert_range_interactive:
1389 * @buffer: a #GtkTextBuffer
1390 * @iter: a position in @buffer
1391 * @start: a position in a #GtkTextBuffer
1392 * @end: another position in the same buffer as @start
1393 * @default_editable: default editability of the buffer
1395 * Same as gtk_text_buffer_insert_range(), but does nothing if the
1396 * insertion point isn't editable. The @default_editable parameter
1397 * indicates whether the text is editable at @iter if no tags
1398 * enclosing @iter affect editability. Typically the result of
1399 * gtk_text_view_get_editable() is appropriate here.
1401 * Returns: whether an insertion was possible at @iter
1404 gtk_text_buffer_insert_range_interactive (GtkTextBuffer *buffer,
1406 const GtkTextIter *start,
1407 const GtkTextIter *end,
1408 gboolean default_editable)
1410 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
1411 g_return_val_if_fail (iter != NULL, FALSE);
1412 g_return_val_if_fail (start != NULL, FALSE);
1413 g_return_val_if_fail (end != NULL, FALSE);
1414 g_return_val_if_fail (gtk_text_iter_get_buffer (start) ==
1415 gtk_text_iter_get_buffer (end), FALSE);
1416 g_return_val_if_fail (gtk_text_iter_get_buffer (start)->tag_table ==
1417 buffer->tag_table, FALSE);
1419 if (gtk_text_iter_can_insert (iter, default_editable))
1421 gtk_text_buffer_real_insert_range (buffer, iter, start, end, TRUE);
1429 * gtk_text_buffer_insert_with_tags:
1430 * @buffer: a #GtkTextBuffer
1431 * @iter: an iterator in @buffer
1433 * @len: length of @text, or -1
1434 * @first_tag: first tag to apply to @text
1435 * @Varargs: NULL-terminated list of tags to apply
1437 * Inserts @text into @buffer at @iter, applying the list of tags to
1438 * the newly-inserted text. The last tag specified must be NULL to
1439 * terminate the list. Equivalent to calling gtk_text_buffer_insert(),
1440 * then gtk_text_buffer_apply_tag() on the inserted text;
1441 * gtk_text_buffer_insert_with_tags() is just a convenience function.
1444 gtk_text_buffer_insert_with_tags (GtkTextBuffer *buffer,
1448 GtkTextTag *first_tag,
1456 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1457 g_return_if_fail (iter != NULL);
1458 g_return_if_fail (text != NULL);
1459 g_return_if_fail (gtk_text_iter_get_buffer (iter) == buffer);
1461 start_offset = gtk_text_iter_get_offset (iter);
1463 gtk_text_buffer_insert (buffer, iter, text, len);
1465 if (first_tag == NULL)
1468 gtk_text_buffer_get_iter_at_offset (buffer, &start, start_offset);
1470 va_start (args, first_tag);
1474 gtk_text_buffer_apply_tag (buffer, tag, &start, iter);
1476 tag = va_arg (args, GtkTextTag*);
1483 * gtk_text_buffer_insert_with_tags_by_name:
1484 * @buffer: a #GtkTextBuffer
1485 * @iter: position in @buffer
1487 * @len: length of @text, or -1
1488 * @first_tag_name: name of a tag to apply to @text
1489 * @Varargs: more tag names
1491 * Same as gtk_text_buffer_insert_with_tags(), but allows you
1492 * to pass in tag names instead of tag objects.
1495 gtk_text_buffer_insert_with_tags_by_name (GtkTextBuffer *buffer,
1499 const gchar *first_tag_name,
1505 const gchar *tag_name;
1507 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1508 g_return_if_fail (iter != NULL);
1509 g_return_if_fail (text != NULL);
1510 g_return_if_fail (gtk_text_iter_get_buffer (iter) == buffer);
1512 start_offset = gtk_text_iter_get_offset (iter);
1514 gtk_text_buffer_insert (buffer, iter, text, len);
1516 if (first_tag_name == NULL)
1519 gtk_text_buffer_get_iter_at_offset (buffer, &start, start_offset);
1521 va_start (args, first_tag_name);
1522 tag_name = first_tag_name;
1527 tag = gtk_text_tag_table_lookup (buffer->tag_table,
1532 g_warning ("%s: no tag with name '%s'!", G_STRLOC, tag_name);
1536 gtk_text_buffer_apply_tag (buffer, tag, &start, iter);
1538 tag_name = va_arg (args, const gchar*);
1550 gtk_text_buffer_real_delete_range (GtkTextBuffer *buffer,
1554 gboolean has_selection;
1556 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1557 g_return_if_fail (start != NULL);
1558 g_return_if_fail (end != NULL);
1560 _gtk_text_btree_delete (start, end);
1562 /* may have deleted the selection... */
1563 update_selection_clipboards (buffer);
1565 has_selection = gtk_text_buffer_get_selection_bounds (buffer, NULL, NULL);
1566 if (has_selection != buffer->has_selection)
1568 buffer->has_selection = has_selection;
1569 g_object_notify (G_OBJECT (buffer), "has-selection");
1572 g_signal_emit (buffer, signals[CHANGED], 0);
1573 g_object_notify (G_OBJECT (buffer), "cursor-position");
1577 gtk_text_buffer_emit_delete (GtkTextBuffer *buffer,
1581 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1582 g_return_if_fail (start != NULL);
1583 g_return_if_fail (end != NULL);
1585 if (gtk_text_iter_equal (start, end))
1588 gtk_text_iter_order (start, end);
1590 g_signal_emit (buffer,
1591 signals[DELETE_RANGE],
1597 * gtk_text_buffer_delete:
1598 * @buffer: a #GtkTextBuffer
1599 * @start: a position in @buffer
1600 * @end: another position in @buffer
1602 * Deletes text between @start and @end. The order of @start and @end
1603 * is not actually relevant; gtk_text_buffer_delete() will reorder
1604 * them. This function actually emits the "delete_range" signal, and
1605 * the default handler of that signal deletes the text. Because the
1606 * buffer is modified, all outstanding iterators become invalid after
1607 * calling this function; however, the @start and @end will be
1608 * re-initialized to point to the location where text was deleted.
1611 gtk_text_buffer_delete (GtkTextBuffer *buffer,
1615 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1616 g_return_if_fail (start != NULL);
1617 g_return_if_fail (end != NULL);
1618 g_return_if_fail (gtk_text_iter_get_buffer (start) == buffer);
1619 g_return_if_fail (gtk_text_iter_get_buffer (end) == buffer);
1621 gtk_text_buffer_emit_delete (buffer, start, end);
1625 * gtk_text_buffer_delete_interactive:
1626 * @buffer: a #GtkTextBuffer
1627 * @start_iter: start of range to delete
1628 * @end_iter: end of range
1629 * @default_editable: whether the buffer is editable by default
1631 * Deletes all <emphasis>editable</emphasis> text in the given range.
1632 * Calls gtk_text_buffer_delete() for each editable sub-range of
1633 * [@start,@end). @start and @end are revalidated to point to
1634 * the location of the last deleted range, or left untouched if
1635 * no text was deleted.
1637 * Return value: whether some text was actually deleted
1640 gtk_text_buffer_delete_interactive (GtkTextBuffer *buffer,
1641 GtkTextIter *start_iter,
1642 GtkTextIter *end_iter,
1643 gboolean default_editable)
1645 GtkTextMark *end_mark;
1646 GtkTextMark *start_mark;
1648 gboolean current_state;
1649 gboolean deleted_stuff = FALSE;
1651 /* Delete all editable text in the range start_iter, end_iter */
1653 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
1654 g_return_val_if_fail (start_iter != NULL, FALSE);
1655 g_return_val_if_fail (end_iter != NULL, FALSE);
1656 g_return_val_if_fail (gtk_text_iter_get_buffer (start_iter) == buffer, FALSE);
1657 g_return_val_if_fail (gtk_text_iter_get_buffer (end_iter) == buffer, FALSE);
1660 gtk_text_buffer_begin_user_action (buffer);
1662 gtk_text_iter_order (start_iter, end_iter);
1664 start_mark = gtk_text_buffer_create_mark (buffer, NULL,
1666 end_mark = gtk_text_buffer_create_mark (buffer, NULL,
1669 gtk_text_buffer_get_iter_at_mark (buffer, &iter, start_mark);
1671 current_state = gtk_text_iter_editable (&iter, default_editable);
1676 gboolean done = FALSE;
1679 gtk_text_iter_forward_to_tag_toggle (&iter, NULL);
1681 gtk_text_buffer_get_iter_at_mark (buffer, &end, end_mark);
1683 if (gtk_text_iter_compare (&iter, &end) >= 0)
1686 iter = end; /* clamp to the last boundary */
1689 new_state = gtk_text_iter_editable (&iter, default_editable);
1691 if (current_state == new_state)
1697 /* We're ending an editable region. Delete said region. */
1700 gtk_text_buffer_get_iter_at_mark (buffer, &start, start_mark);
1702 gtk_text_buffer_emit_delete (buffer, &start, &iter);
1704 deleted_stuff = TRUE;
1706 /* revalidate user's iterators. */
1707 *start_iter = start;
1717 if (current_state && !new_state)
1719 /* End of an editable region. Delete it. */
1722 gtk_text_buffer_get_iter_at_mark (buffer, &start, start_mark);
1724 gtk_text_buffer_emit_delete (buffer, &start, &iter);
1726 /* It's more robust to ask for the state again then to assume that
1727 * we're on the next not-editable segment. We don't know what the
1728 * ::delete-range handler did.... maybe it deleted the following not-editable
1729 * segment because it was associated with the editable segment.
1731 current_state = gtk_text_iter_editable (&iter, default_editable);
1732 deleted_stuff = TRUE;
1734 /* revalidate user's iterators. */
1735 *start_iter = start;
1740 /* We are at the start of an editable region. We won't be deleting
1741 * the previous region. Move start mark to start of this region.
1744 g_assert (!current_state && new_state);
1746 gtk_text_buffer_move_mark (buffer, start_mark, &iter);
1748 current_state = TRUE;
1755 gtk_text_buffer_delete_mark (buffer, start_mark);
1756 gtk_text_buffer_delete_mark (buffer, end_mark);
1758 gtk_text_buffer_end_user_action (buffer);
1760 return deleted_stuff;
1764 * Extracting textual buffer contents
1768 * gtk_text_buffer_get_text:
1769 * @buffer: a #GtkTextBuffer
1770 * @start: start of a range
1771 * @end: end of a range
1772 * @include_hidden_chars: whether to include invisible text
1774 * Returns the text in the range [@start,@end). Excludes undisplayed
1775 * text (text marked with tags that set the invisibility attribute) if
1776 * @include_hidden_chars is %FALSE. Does not include characters
1777 * representing embedded images, so byte and character indexes into
1778 * the returned string do <emphasis>not</emphasis> correspond to byte
1779 * and character indexes into the buffer. Contrast with
1780 * gtk_text_buffer_get_slice().
1782 * Return value: an allocated UTF-8 string
1785 gtk_text_buffer_get_text (GtkTextBuffer *buffer,
1786 const GtkTextIter *start,
1787 const GtkTextIter *end,
1788 gboolean include_hidden_chars)
1790 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
1791 g_return_val_if_fail (start != NULL, NULL);
1792 g_return_val_if_fail (end != NULL, NULL);
1793 g_return_val_if_fail (gtk_text_iter_get_buffer (start) == buffer, NULL);
1794 g_return_val_if_fail (gtk_text_iter_get_buffer (end) == buffer, NULL);
1796 if (include_hidden_chars)
1797 return gtk_text_iter_get_text (start, end);
1799 return gtk_text_iter_get_visible_text (start, end);
1803 * gtk_text_buffer_get_slice:
1804 * @buffer: a #GtkTextBuffer
1805 * @start: start of a range
1806 * @end: end of a range
1807 * @include_hidden_chars: whether to include invisible text
1809 * Returns the text in the range [@start,@end). Excludes undisplayed
1810 * text (text marked with tags that set the invisibility attribute) if
1811 * @include_hidden_chars is %FALSE. The returned string includes a
1812 * 0xFFFC character whenever the buffer contains
1813 * embedded images, so byte and character indexes into
1814 * the returned string <emphasis>do</emphasis> correspond to byte
1815 * and character indexes into the buffer. Contrast with
1816 * gtk_text_buffer_get_text(). Note that 0xFFFC can occur in normal
1817 * text as well, so it is not a reliable indicator that a pixbuf or
1818 * widget is in the buffer.
1820 * Return value: an allocated UTF-8 string
1823 gtk_text_buffer_get_slice (GtkTextBuffer *buffer,
1824 const GtkTextIter *start,
1825 const GtkTextIter *end,
1826 gboolean include_hidden_chars)
1828 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
1829 g_return_val_if_fail (start != NULL, NULL);
1830 g_return_val_if_fail (end != NULL, NULL);
1831 g_return_val_if_fail (gtk_text_iter_get_buffer (start) == buffer, NULL);
1832 g_return_val_if_fail (gtk_text_iter_get_buffer (end) == buffer, NULL);
1834 if (include_hidden_chars)
1835 return gtk_text_iter_get_slice (start, end);
1837 return gtk_text_iter_get_visible_slice (start, end);
1845 gtk_text_buffer_real_insert_pixbuf (GtkTextBuffer *buffer,
1849 _gtk_text_btree_insert_pixbuf (iter, pixbuf);
1851 g_signal_emit (buffer, signals[CHANGED], 0);
1855 * gtk_text_buffer_insert_pixbuf:
1856 * @buffer: a #GtkTextBuffer
1857 * @iter: location to insert the pixbuf
1858 * @pixbuf: a #GdkPixbuf
1860 * Inserts an image into the text buffer at @iter. The image will be
1861 * counted as one character in character counts, and when obtaining
1862 * the buffer contents as a string, will be represented by the Unicode
1863 * "object replacement character" 0xFFFC. Note that the "slice"
1864 * variants for obtaining portions of the buffer as a string include
1865 * this character for pixbufs, but the "text" variants do
1866 * not. e.g. see gtk_text_buffer_get_slice() and
1867 * gtk_text_buffer_get_text().
1870 gtk_text_buffer_insert_pixbuf (GtkTextBuffer *buffer,
1874 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1875 g_return_if_fail (iter != NULL);
1876 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
1877 g_return_if_fail (gtk_text_iter_get_buffer (iter) == buffer);
1879 g_signal_emit (buffer, signals[INSERT_PIXBUF], 0,
1889 gtk_text_buffer_real_insert_anchor (GtkTextBuffer *buffer,
1891 GtkTextChildAnchor *anchor)
1893 _gtk_text_btree_insert_child_anchor (iter, anchor);
1895 g_signal_emit (buffer, signals[CHANGED], 0);
1899 * gtk_text_buffer_insert_child_anchor:
1900 * @buffer: a #GtkTextBuffer
1901 * @iter: location to insert the anchor
1902 * @anchor: a #GtkTextChildAnchor
1904 * Inserts a child widget anchor into the text buffer at @iter. The
1905 * anchor will be counted as one character in character counts, and
1906 * when obtaining the buffer contents as a string, will be represented
1907 * by the Unicode "object replacement character" 0xFFFC. Note that the
1908 * "slice" variants for obtaining portions of the buffer as a string
1909 * include this character for child anchors, but the "text" variants do
1910 * not. E.g. see gtk_text_buffer_get_slice() and
1911 * gtk_text_buffer_get_text(). Consider
1912 * gtk_text_buffer_create_child_anchor() as a more convenient
1913 * alternative to this function. The buffer will add a reference to
1914 * the anchor, so you can unref it after insertion.
1917 gtk_text_buffer_insert_child_anchor (GtkTextBuffer *buffer,
1919 GtkTextChildAnchor *anchor)
1921 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1922 g_return_if_fail (iter != NULL);
1923 g_return_if_fail (GTK_IS_TEXT_CHILD_ANCHOR (anchor));
1924 g_return_if_fail (gtk_text_iter_get_buffer (iter) == buffer);
1926 g_signal_emit (buffer, signals[INSERT_CHILD_ANCHOR], 0,
1931 * gtk_text_buffer_create_child_anchor:
1932 * @buffer: a #GtkTextBuffer
1933 * @iter: location in the buffer
1935 * This is a convenience function which simply creates a child anchor
1936 * with gtk_text_child_anchor_new() and inserts it into the buffer
1937 * with gtk_text_buffer_insert_child_anchor(). The new anchor is
1938 * owned by the buffer; no reference count is returned to
1939 * the caller of gtk_text_buffer_create_child_anchor().
1941 * Return value: the created child anchor
1944 gtk_text_buffer_create_child_anchor (GtkTextBuffer *buffer,
1947 GtkTextChildAnchor *anchor;
1949 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
1950 g_return_val_if_fail (iter != NULL, NULL);
1951 g_return_val_if_fail (gtk_text_iter_get_buffer (iter) == buffer, NULL);
1953 anchor = gtk_text_child_anchor_new ();
1955 gtk_text_buffer_insert_child_anchor (buffer, iter, anchor);
1957 g_object_unref (anchor);
1967 gtk_text_buffer_mark_set (GtkTextBuffer *buffer,
1968 const GtkTextIter *location,
1971 /* IMO this should NOT work like insert_text and delete_range,
1972 * where the real action happens in the default handler.
1974 * The reason is that the default handler would be _required_,
1975 * i.e. the whole widget would start breaking and segfaulting if the
1976 * default handler didn't get run. So you can't really override the
1977 * default handler or stop the emission; that is, this signal is
1978 * purely for notification, and not to allow users to modify the
1982 g_object_ref (mark);
1984 g_signal_emit (buffer,
1990 g_object_unref (mark);
1994 * gtk_text_buffer_set_mark:
1995 * @buffer: a #GtkTextBuffer
1996 * @mark_name: name of the mark
1997 * @iter: location for the mark
1998 * @left_gravity: if the mark is created by this function, gravity for
2000 * @should_exist: if %TRUE, warn if the mark does not exist, and return
2003 * Move the mark to the given position, if not @should_exist,
2006 * Return value: mark
2009 gtk_text_buffer_set_mark (GtkTextBuffer *buffer,
2010 GtkTextMark *existing_mark,
2011 const gchar *mark_name,
2012 const GtkTextIter *iter,
2013 gboolean left_gravity,
2014 gboolean should_exist)
2016 GtkTextIter location;
2019 g_return_val_if_fail (gtk_text_iter_get_buffer (iter) == buffer, NULL);
2021 mark = _gtk_text_btree_set_mark (get_btree (buffer),
2028 _gtk_text_btree_get_iter_at_mark (get_btree (buffer),
2032 gtk_text_buffer_mark_set (buffer, &location, mark);
2038 * gtk_text_buffer_create_mark:
2039 * @buffer: a #GtkTextBuffer
2040 * @mark_name: name for mark, or %NULL
2041 * @where: location to place mark
2042 * @left_gravity: whether the mark has left gravity
2044 * Creates a mark at position @where. If @mark_name is %NULL, the mark
2045 * is anonymous; otherwise, the mark can be retrieved by name using
2046 * gtk_text_buffer_get_mark(). If a mark has left gravity, and text is
2047 * inserted at the mark's current location, the mark will be moved to
2048 * the left of the newly-inserted text. If the mark has right gravity
2049 * (@left_gravity = %FALSE), the mark will end up on the right of
2050 * newly-inserted text. The standard left-to-right cursor is a mark
2051 * with right gravity (when you type, the cursor stays on the right
2052 * side of the text you're typing).
2054 * The caller of this function does <emphasis>not</emphasis> own a
2055 * reference to the returned #GtkTextMark, so you can ignore the
2056 * return value if you like. Marks are owned by the buffer and go
2057 * away when the buffer does.
2059 * Emits the "mark_set" signal as notification of the mark's initial
2062 * Return value: the new #GtkTextMark object
2065 gtk_text_buffer_create_mark (GtkTextBuffer *buffer,
2066 const gchar *mark_name,
2067 const GtkTextIter *where,
2068 gboolean left_gravity)
2070 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
2072 return gtk_text_buffer_set_mark (buffer, NULL, mark_name, where,
2073 left_gravity, FALSE);
2077 * gtk_text_buffer_add_mark:
2078 * @buffer: a #GtkTextBuffer
2079 * @mark: the mark to add
2080 * @where: location to place mark
2082 * Adds the mark at position @where. The mark must not be added to
2083 * another buffer, and if its name is not %NULL then there must not
2084 * be another mark in the buffer with the same name.
2086 * Emits the "mark_set" signal as notification of the mark's initial
2092 gtk_text_buffer_add_mark (GtkTextBuffer *buffer,
2094 const GtkTextIter *where)
2098 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2099 g_return_if_fail (GTK_IS_TEXT_MARK (mark));
2100 g_return_if_fail (where != NULL);
2101 g_return_if_fail (gtk_text_mark_get_buffer (mark) == NULL);
2103 name = gtk_text_mark_get_name (mark);
2105 if (name != NULL && gtk_text_buffer_get_mark (buffer, name) != NULL)
2107 g_critical ("Mark %s already exists in the buffer", name);
2111 gtk_text_buffer_set_mark (buffer, mark, NULL, where, FALSE, FALSE);
2115 * gtk_text_buffer_move_mark:
2116 * @buffer: a #GtkTextBuffer
2117 * @mark: a #GtkTextMark
2118 * @where: new location for @mark in @buffer
2120 * Moves @mark to the new location @where. Emits the "mark_set" signal
2121 * as notification of the move.
2124 gtk_text_buffer_move_mark (GtkTextBuffer *buffer,
2126 const GtkTextIter *where)
2128 g_return_if_fail (GTK_IS_TEXT_MARK (mark));
2129 g_return_if_fail (!gtk_text_mark_get_deleted (mark));
2130 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2132 gtk_text_buffer_set_mark (buffer, mark, NULL, where, FALSE, TRUE);
2136 * gtk_text_buffer_get_iter_at_mark:
2137 * @buffer: a #GtkTextBuffer
2138 * @iter: iterator to initialize
2139 * @mark: a #GtkTextMark in @buffer
2141 * Initializes @iter with the current position of @mark.
2144 gtk_text_buffer_get_iter_at_mark (GtkTextBuffer *buffer,
2148 g_return_if_fail (GTK_IS_TEXT_MARK (mark));
2149 g_return_if_fail (!gtk_text_mark_get_deleted (mark));
2150 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2152 _gtk_text_btree_get_iter_at_mark (get_btree (buffer),
2158 * gtk_text_buffer_delete_mark:
2159 * @buffer: a #GtkTextBuffer
2160 * @mark: a #GtkTextMark in @buffer
2162 * Deletes @mark, so that it's no longer located anywhere in the
2163 * buffer. Removes the reference the buffer holds to the mark, so if
2164 * you haven't called g_object_ref() on the mark, it will be freed. Even
2165 * if the mark isn't freed, most operations on @mark become
2166 * invalid, until it gets added to a buffer again with
2167 * gtk_text_buffer_add_mark(). Use gtk_text_mark_get_deleted() to
2168 * find out if a mark has been removed from its buffer.
2169 * The "mark_deleted" signal will be emitted as notification after
2170 * the mark is deleted.
2173 gtk_text_buffer_delete_mark (GtkTextBuffer *buffer,
2176 g_return_if_fail (GTK_IS_TEXT_MARK (mark));
2177 g_return_if_fail (!gtk_text_mark_get_deleted (mark));
2178 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2180 g_object_ref (mark);
2182 _gtk_text_btree_remove_mark (get_btree (buffer), mark);
2184 /* See rationale above for MARK_SET on why we emit this after
2185 * removing the mark, rather than removing the mark in a default
2188 g_signal_emit (buffer, signals[MARK_DELETED],
2192 g_object_unref (mark);
2196 * gtk_text_buffer_get_mark:
2197 * @buffer: a #GtkTextBuffer
2198 * @name: a mark name
2200 * Returns the mark named @name in buffer @buffer, or %NULL if no such
2201 * mark exists in the buffer.
2203 * Return value: a #GtkTextMark, or %NULL
2206 gtk_text_buffer_get_mark (GtkTextBuffer *buffer,
2211 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
2212 g_return_val_if_fail (name != NULL, NULL);
2214 mark = _gtk_text_btree_get_mark_by_name (get_btree (buffer), name);
2220 * gtk_text_buffer_move_mark_by_name:
2221 * @buffer: a #GtkTextBuffer
2222 * @name: name of a mark
2223 * @where: new location for mark
2225 * Moves the mark named @name (which must exist) to location @where.
2226 * See gtk_text_buffer_move_mark() for details.
2229 gtk_text_buffer_move_mark_by_name (GtkTextBuffer *buffer,
2231 const GtkTextIter *where)
2235 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2236 g_return_if_fail (name != NULL);
2238 mark = _gtk_text_btree_get_mark_by_name (get_btree (buffer), name);
2242 g_warning ("%s: no mark named '%s'", G_STRLOC, name);
2246 gtk_text_buffer_move_mark (buffer, mark, where);
2250 * gtk_text_buffer_delete_mark_by_name:
2251 * @buffer: a #GtkTextBuffer
2252 * @name: name of a mark in @buffer
2254 * Deletes the mark named @name; the mark must exist. See
2255 * gtk_text_buffer_delete_mark() for details.
2258 gtk_text_buffer_delete_mark_by_name (GtkTextBuffer *buffer,
2263 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2264 g_return_if_fail (name != NULL);
2266 mark = _gtk_text_btree_get_mark_by_name (get_btree (buffer), name);
2270 g_warning ("%s: no mark named '%s'", G_STRLOC, name);
2274 gtk_text_buffer_delete_mark (buffer, mark);
2278 * gtk_text_buffer_get_insert:
2279 * @buffer: a #GtkTextBuffer
2281 * Returns the mark that represents the cursor (insertion point).
2282 * Equivalent to calling gtk_text_buffer_get_mark() to get the mark
2283 * named "insert", but very slightly more efficient, and involves less
2286 * Return value: insertion point mark
2289 gtk_text_buffer_get_insert (GtkTextBuffer *buffer)
2291 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
2293 return _gtk_text_btree_get_insert (get_btree (buffer));
2297 * gtk_text_buffer_get_selection_bound:
2298 * @buffer: a #GtkTextBuffer
2300 * Returns the mark that represents the selection bound. Equivalent
2301 * to calling gtk_text_buffer_get_mark() to get the mark named
2302 * "selection_bound", but very slightly more efficient, and involves
2305 * The currently-selected text in @buffer is the region between the
2306 * "selection_bound" and "insert" marks. If "selection_bound" and
2307 * "insert" are in the same place, then there is no current selection.
2308 * gtk_text_buffer_get_selection_bounds() is another convenient function
2309 * for handling the selection, if you just want to know whether there's a
2310 * selection and what its bounds are.
2312 * Return value: selection bound mark
2315 gtk_text_buffer_get_selection_bound (GtkTextBuffer *buffer)
2317 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
2319 return _gtk_text_btree_get_selection_bound (get_btree (buffer));
2323 * gtk_text_buffer_get_iter_at_child_anchor:
2324 * @buffer: a #GtkTextBuffer
2325 * @iter: an iterator to be initialized
2326 * @anchor: a child anchor that appears in @buffer
2328 * Obtains the location of @anchor within @buffer.
2331 gtk_text_buffer_get_iter_at_child_anchor (GtkTextBuffer *buffer,
2333 GtkTextChildAnchor *anchor)
2335 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2336 g_return_if_fail (iter != NULL);
2337 g_return_if_fail (GTK_IS_TEXT_CHILD_ANCHOR (anchor));
2338 g_return_if_fail (!gtk_text_child_anchor_get_deleted (anchor));
2340 _gtk_text_btree_get_iter_at_child_anchor (get_btree (buffer),
2346 * gtk_text_buffer_place_cursor:
2347 * @buffer: a #GtkTextBuffer
2348 * @where: where to put the cursor
2350 * This function moves the "insert" and "selection_bound" marks
2351 * simultaneously. If you move them to the same place in two steps
2352 * with gtk_text_buffer_move_mark(), you will temporarily select a
2353 * region in between their old and new locations, which can be pretty
2354 * inefficient since the temporarily-selected region will force stuff
2355 * to be recalculated. This function moves them as a unit, which can
2359 gtk_text_buffer_place_cursor (GtkTextBuffer *buffer,
2360 const GtkTextIter *where)
2362 gtk_text_buffer_select_range (buffer, where, where);
2366 * gtk_text_buffer_select_range:
2367 * @buffer: a #GtkTextBuffer
2368 * @ins: where to put the "insert" mark
2369 * @bound: where to put the "selection_bound" mark
2371 * This function moves the "insert" and "selection_bound" marks
2372 * simultaneously. If you move them in two steps
2373 * with gtk_text_buffer_move_mark(), you will temporarily select a
2374 * region in between their old and new locations, which can be pretty
2375 * inefficient since the temporarily-selected region will force stuff
2376 * to be recalculated. This function moves them as a unit, which can
2382 gtk_text_buffer_select_range (GtkTextBuffer *buffer,
2383 const GtkTextIter *ins,
2384 const GtkTextIter *bound)
2386 GtkTextIter real_ins;
2387 GtkTextIter real_bound;
2389 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2392 real_bound = *bound;
2394 _gtk_text_btree_select_range (get_btree (buffer), &real_ins, &real_bound);
2395 gtk_text_buffer_mark_set (buffer, &real_ins,
2396 gtk_text_buffer_get_insert (buffer));
2397 gtk_text_buffer_mark_set (buffer, &real_bound,
2398 gtk_text_buffer_get_selection_bound (buffer));
2406 * gtk_text_buffer_create_tag:
2407 * @buffer: a #GtkTextBuffer
2408 * @tag_name: name of the new tag, or %NULL
2409 * @first_property_name: name of first property to set, or %NULL
2410 * @Varargs: %NULL-terminated list of property names and values
2413 * Creates a tag and adds it to the tag table for @buffer.
2414 * Equivalent to calling gtk_text_tag_new() and then adding the
2415 * tag to the buffer's tag table. The returned tag is owned by
2416 * the buffer's tag table, so the ref count will be equal to one.
2418 * If @tag_name is %NULL, the tag is anonymous.
2420 * If @tag_name is non-%NULL, a tag called @tag_name must not already
2421 * exist in the tag table for this buffer.
2423 * The @first_property_name argument and subsequent arguments are a list
2424 * of properties to set on the tag, as with g_object_set().
2426 * Return value: a new tag
2429 gtk_text_buffer_create_tag (GtkTextBuffer *buffer,
2430 const gchar *tag_name,
2431 const gchar *first_property_name,
2437 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
2439 tag = gtk_text_tag_new (tag_name);
2441 gtk_text_tag_table_add (get_table (buffer), tag);
2443 if (first_property_name)
2445 va_start (list, first_property_name);
2446 g_object_set_valist (G_OBJECT (tag), first_property_name, list);
2450 g_object_unref (tag);
2456 gtk_text_buffer_real_apply_tag (GtkTextBuffer *buffer,
2458 const GtkTextIter *start,
2459 const GtkTextIter *end)
2461 if (tag->table != buffer->tag_table)
2463 g_warning ("Can only apply tags that are in the tag table for the buffer");
2467 _gtk_text_btree_tag (start, end, tag, TRUE);
2471 gtk_text_buffer_real_remove_tag (GtkTextBuffer *buffer,
2473 const GtkTextIter *start,
2474 const GtkTextIter *end)
2476 if (tag->table != buffer->tag_table)
2478 g_warning ("Can only remove tags that are in the tag table for the buffer");
2482 _gtk_text_btree_tag (start, end, tag, FALSE);
2486 gtk_text_buffer_real_changed (GtkTextBuffer *buffer)
2488 gtk_text_buffer_set_modified (buffer, TRUE);
2492 gtk_text_buffer_real_mark_set (GtkTextBuffer *buffer,
2493 const GtkTextIter *iter,
2496 GtkTextMark *insert;
2498 insert = gtk_text_buffer_get_insert (buffer);
2500 if (mark == insert || mark == gtk_text_buffer_get_selection_bound (buffer))
2502 gboolean has_selection;
2504 update_selection_clipboards (buffer);
2506 has_selection = gtk_text_buffer_get_selection_bounds (buffer,
2510 if (has_selection != buffer->has_selection)
2512 buffer->has_selection = has_selection;
2513 g_object_notify (G_OBJECT (buffer), "has-selection");
2518 g_object_notify (G_OBJECT (buffer), "cursor-position");
2522 gtk_text_buffer_emit_tag (GtkTextBuffer *buffer,
2525 const GtkTextIter *start,
2526 const GtkTextIter *end)
2528 GtkTextIter start_tmp = *start;
2529 GtkTextIter end_tmp = *end;
2531 g_return_if_fail (tag != NULL);
2533 gtk_text_iter_order (&start_tmp, &end_tmp);
2536 g_signal_emit (buffer, signals[APPLY_TAG],
2538 tag, &start_tmp, &end_tmp);
2540 g_signal_emit (buffer, signals[REMOVE_TAG],
2542 tag, &start_tmp, &end_tmp);
2546 * gtk_text_buffer_apply_tag:
2547 * @buffer: a #GtkTextBuffer
2548 * @tag: a #GtkTextTag
2549 * @start: one bound of range to be tagged
2550 * @end: other bound of range to be tagged
2552 * Emits the "apply_tag" signal on @buffer. The default
2553 * handler for the signal applies @tag to the given range.
2554 * @start and @end do not have to be in order.
2557 gtk_text_buffer_apply_tag (GtkTextBuffer *buffer,
2559 const GtkTextIter *start,
2560 const GtkTextIter *end)
2562 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2563 g_return_if_fail (GTK_IS_TEXT_TAG (tag));
2564 g_return_if_fail (start != NULL);
2565 g_return_if_fail (end != NULL);
2566 g_return_if_fail (gtk_text_iter_get_buffer (start) == buffer);
2567 g_return_if_fail (gtk_text_iter_get_buffer (end) == buffer);
2568 g_return_if_fail (tag->table == buffer->tag_table);
2570 gtk_text_buffer_emit_tag (buffer, tag, TRUE, start, end);
2574 * gtk_text_buffer_remove_tag:
2575 * @buffer: a #GtkTextBuffer
2576 * @tag: a #GtkTextTag
2577 * @start: one bound of range to be untagged
2578 * @end: other bound of range to be untagged
2580 * Emits the "remove_tag" signal. The default handler for the signal
2581 * removes all occurrences of @tag from the given range. @start and
2582 * @end don't have to be in order.
2585 gtk_text_buffer_remove_tag (GtkTextBuffer *buffer,
2587 const GtkTextIter *start,
2588 const GtkTextIter *end)
2591 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2592 g_return_if_fail (GTK_IS_TEXT_TAG (tag));
2593 g_return_if_fail (start != NULL);
2594 g_return_if_fail (end != NULL);
2595 g_return_if_fail (gtk_text_iter_get_buffer (start) == buffer);
2596 g_return_if_fail (gtk_text_iter_get_buffer (end) == buffer);
2597 g_return_if_fail (tag->table == buffer->tag_table);
2599 gtk_text_buffer_emit_tag (buffer, tag, FALSE, start, end);
2603 * gtk_text_buffer_apply_tag_by_name:
2604 * @buffer: a #GtkTextBuffer
2605 * @name: name of a named #GtkTextTag
2606 * @start: one bound of range to be tagged
2607 * @end: other bound of range to be tagged
2609 * Calls gtk_text_tag_table_lookup() on the buffer's tag table to
2610 * get a #GtkTextTag, then calls gtk_text_buffer_apply_tag().
2613 gtk_text_buffer_apply_tag_by_name (GtkTextBuffer *buffer,
2615 const GtkTextIter *start,
2616 const GtkTextIter *end)
2620 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2621 g_return_if_fail (name != NULL);
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);
2627 tag = gtk_text_tag_table_lookup (get_table (buffer),
2632 g_warning ("Unknown tag `%s'", name);
2636 gtk_text_buffer_emit_tag (buffer, tag, TRUE, start, end);
2640 * gtk_text_buffer_remove_tag_by_name:
2641 * @buffer: a #GtkTextBuffer
2642 * @name: name of a #GtkTextTag
2643 * @start: one bound of range to be untagged
2644 * @end: other bound of range to be untagged
2646 * Calls gtk_text_tag_table_lookup() on the buffer's tag table to
2647 * get a #GtkTextTag, then calls gtk_text_buffer_remove_tag().
2650 gtk_text_buffer_remove_tag_by_name (GtkTextBuffer *buffer,
2652 const GtkTextIter *start,
2653 const GtkTextIter *end)
2657 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2658 g_return_if_fail (name != NULL);
2659 g_return_if_fail (start != NULL);
2660 g_return_if_fail (end != NULL);
2661 g_return_if_fail (gtk_text_iter_get_buffer (start) == buffer);
2662 g_return_if_fail (gtk_text_iter_get_buffer (end) == buffer);
2664 tag = gtk_text_tag_table_lookup (get_table (buffer),
2669 g_warning ("Unknown tag `%s'", name);
2673 gtk_text_buffer_emit_tag (buffer, tag, FALSE, start, end);
2677 pointer_cmp (gconstpointer a,
2689 * gtk_text_buffer_remove_all_tags:
2690 * @buffer: a #GtkTextBuffer
2691 * @start: one bound of range to be untagged
2692 * @end: other bound of range to be untagged
2694 * Removes all tags in the range between @start and @end. Be careful
2695 * with this function; it could remove tags added in code unrelated to
2696 * the code you're currently writing. That is, using this function is
2697 * probably a bad idea if you have two or more unrelated code sections
2701 gtk_text_buffer_remove_all_tags (GtkTextBuffer *buffer,
2702 const GtkTextIter *start,
2703 const GtkTextIter *end)
2705 GtkTextIter first, second, tmp;
2708 GSList *prev, *next;
2711 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2712 g_return_if_fail (start != NULL);
2713 g_return_if_fail (end != NULL);
2714 g_return_if_fail (gtk_text_iter_get_buffer (start) == buffer);
2715 g_return_if_fail (gtk_text_iter_get_buffer (end) == buffer);
2720 gtk_text_iter_order (&first, &second);
2722 /* Get all tags turned on at the start */
2723 tags = gtk_text_iter_get_tags (&first);
2725 /* Find any that are toggled on within the range */
2727 while (gtk_text_iter_forward_to_tag_toggle (&tmp, NULL))
2732 if (gtk_text_iter_compare (&tmp, &second) >= 0)
2733 break; /* past the end of the range */
2735 toggled = gtk_text_iter_get_toggled_tags (&tmp, TRUE);
2737 /* We could end up with a really big-ass list here.
2740 tmp_list2 = toggled;
2741 while (tmp_list2 != NULL)
2743 tags = g_slist_prepend (tags, tmp_list2->data);
2745 tmp_list2 = g_slist_next (tmp_list2);
2748 g_slist_free (toggled);
2752 tags = g_slist_sort (tags, pointer_cmp);
2754 /* Strip duplicates */
2758 while (tmp_list != NULL)
2760 if (tag == tmp_list->data)
2763 next = tmp_list->next;
2767 tmp_list->next = NULL;
2769 g_slist_free (tmp_list);
2772 /* prev is unchanged */
2776 /* not a duplicate */
2777 tag = GTK_TEXT_TAG (tmp_list->data);
2779 tmp_list = tmp_list->next;
2783 g_slist_foreach (tags, (GFunc) g_object_ref, NULL);
2786 while (tmp_list != NULL)
2788 tag = GTK_TEXT_TAG (tmp_list->data);
2790 gtk_text_buffer_remove_tag (buffer, tag, &first, &second);
2792 tmp_list = tmp_list->next;
2795 g_slist_foreach (tags, (GFunc) g_object_unref, NULL);
2797 g_slist_free (tags);
2802 * Obtain various iterators
2806 * gtk_text_buffer_get_iter_at_line_offset:
2807 * @buffer: a #GtkTextBuffer
2808 * @iter: iterator to initialize
2809 * @line_number: line number counting from 0
2810 * @char_offset: char offset from start of line
2812 * Obtains an iterator pointing to @char_offset within the given
2813 * line. The @char_offset must exist, offsets off the end of the line
2814 * are not allowed. Note <emphasis>characters</emphasis>, not bytes;
2815 * UTF-8 may encode one character as multiple bytes.
2818 gtk_text_buffer_get_iter_at_line_offset (GtkTextBuffer *buffer,
2823 g_return_if_fail (iter != NULL);
2824 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2826 _gtk_text_btree_get_iter_at_line_char (get_btree (buffer),
2827 iter, line_number, char_offset);
2831 * gtk_text_buffer_get_iter_at_line_index:
2832 * @buffer: a #GtkTextBuffer
2833 * @iter: iterator to initialize
2834 * @line_number: line number counting from 0
2835 * @byte_index: byte index from start of line
2837 * Obtains an iterator pointing to @byte_index within the given line.
2838 * @byte_index must be the start of a UTF-8 character, and must not be
2839 * beyond the end of the line. Note <emphasis>bytes</emphasis>, not
2840 * characters; UTF-8 may encode one character as multiple bytes.
2843 gtk_text_buffer_get_iter_at_line_index (GtkTextBuffer *buffer,
2848 g_return_if_fail (iter != NULL);
2849 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2851 _gtk_text_btree_get_iter_at_line_byte (get_btree (buffer),
2852 iter, line_number, byte_index);
2856 * gtk_text_buffer_get_iter_at_line:
2857 * @buffer: a #GtkTextBuffer
2858 * @iter: iterator to initialize
2859 * @line_number: line number counting from 0
2861 * Initializes @iter to the start of the given line.
2864 gtk_text_buffer_get_iter_at_line (GtkTextBuffer *buffer,
2868 g_return_if_fail (iter != NULL);
2869 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2871 gtk_text_buffer_get_iter_at_line_offset (buffer, iter, line_number, 0);
2875 * gtk_text_buffer_get_iter_at_offset:
2876 * @buffer: a #GtkTextBuffer
2877 * @iter: iterator to initialize
2878 * @char_offset: char offset from start of buffer, counting from 0, or -1
2880 * Initializes @iter to a position @char_offset chars from the start
2881 * of the entire buffer. If @char_offset is -1 or greater than the number
2882 * of characters in the buffer, @iter is initialized to the end iterator,
2883 * the iterator one past the last valid character in the buffer.
2886 gtk_text_buffer_get_iter_at_offset (GtkTextBuffer *buffer,
2890 g_return_if_fail (iter != NULL);
2891 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2893 _gtk_text_btree_get_iter_at_char (get_btree (buffer), iter, char_offset);
2897 * gtk_text_buffer_get_start_iter:
2898 * @buffer: a #GtkTextBuffer
2899 * @iter: iterator to initialize
2901 * Initialized @iter with the first position in the text buffer. This
2902 * is the same as using gtk_text_buffer_get_iter_at_offset() to get
2903 * the iter at character offset 0.
2906 gtk_text_buffer_get_start_iter (GtkTextBuffer *buffer,
2909 g_return_if_fail (iter != NULL);
2910 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2912 _gtk_text_btree_get_iter_at_char (get_btree (buffer), iter, 0);
2916 * gtk_text_buffer_get_end_iter:
2917 * @buffer: a #GtkTextBuffer
2918 * @iter: iterator to initialize
2920 * Initializes @iter with the "end iterator," one past the last valid
2921 * character in the text buffer. If dereferenced with
2922 * gtk_text_iter_get_char(), the end iterator has a character value of
2923 * 0. The entire buffer lies in the range from the first position in
2924 * the buffer (call gtk_text_buffer_get_start_iter() to get
2925 * character position 0) to the end iterator.
2928 gtk_text_buffer_get_end_iter (GtkTextBuffer *buffer,
2931 g_return_if_fail (iter != NULL);
2932 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2934 _gtk_text_btree_get_end_iter (get_btree (buffer), iter);
2938 * gtk_text_buffer_get_bounds:
2939 * @buffer: a #GtkTextBuffer
2940 * @start: iterator to initialize with first position in the buffer
2941 * @end: iterator to initialize with the end iterator
2943 * Retrieves the first and last iterators in the buffer, i.e. the
2944 * entire buffer lies within the range [@start,@end).
2947 gtk_text_buffer_get_bounds (GtkTextBuffer *buffer,
2951 g_return_if_fail (start != NULL);
2952 g_return_if_fail (end != NULL);
2953 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2955 _gtk_text_btree_get_iter_at_char (get_btree (buffer), start, 0);
2956 _gtk_text_btree_get_end_iter (get_btree (buffer), end);
2964 * gtk_text_buffer_get_modified:
2965 * @buffer: a #GtkTextBuffer
2967 * Indicates whether the buffer has been modified since the last call
2968 * to gtk_text_buffer_set_modified() set the modification flag to
2969 * %FALSE. Used for example to enable a "save" function in a text
2972 * Return value: %TRUE if the buffer has been modified
2975 gtk_text_buffer_get_modified (GtkTextBuffer *buffer)
2977 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
2979 return buffer->modified;
2983 * gtk_text_buffer_set_modified:
2984 * @buffer: a #GtkTextBuffer
2985 * @setting: modification flag setting
2987 * Used to keep track of whether the buffer has been modified since the
2988 * last time it was saved. Whenever the buffer is saved to disk, call
2989 * gtk_text_buffer_set_modified (@buffer, FALSE). When the buffer is modified,
2990 * it will automatically toggled on the modified bit again. When the modified
2991 * bit flips, the buffer emits a "modified_changed" signal.
2994 gtk_text_buffer_set_modified (GtkTextBuffer *buffer,
2997 gboolean fixed_setting;
2999 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
3001 fixed_setting = setting != FALSE;
3003 if (buffer->modified == fixed_setting)
3007 buffer->modified = fixed_setting;
3008 g_signal_emit (buffer, signals[MODIFIED_CHANGED], 0);
3013 * gtk_text_buffer_get_has_selection:
3014 * @buffer: a #GtkTextBuffer
3016 * Indicates whether the buffer has some text currently selected.
3018 * Return value: %TRUE if the there is text selected
3023 gtk_text_buffer_get_has_selection (GtkTextBuffer *buffer)
3025 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
3027 return buffer->has_selection;
3032 * Assorted other stuff
3036 * gtk_text_buffer_get_line_count:
3037 * @buffer: a #GtkTextBuffer
3039 * Obtains the number of lines in the buffer. This value is cached, so
3040 * the function is very fast.
3042 * Return value: number of lines in the buffer
3045 gtk_text_buffer_get_line_count (GtkTextBuffer *buffer)
3047 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), 0);
3049 return _gtk_text_btree_line_count (get_btree (buffer));
3053 * gtk_text_buffer_get_char_count:
3054 * @buffer: a #GtkTextBuffer
3056 * Gets the number of characters in the buffer; note that characters
3057 * and bytes are not the same, you can't e.g. expect the contents of
3058 * the buffer in string form to be this many bytes long. The character
3059 * count is cached, so this function is very fast.
3061 * Return value: number of characters in the buffer
3064 gtk_text_buffer_get_char_count (GtkTextBuffer *buffer)
3066 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), 0);
3068 return _gtk_text_btree_char_count (get_btree (buffer));
3071 /* Called when we lose the primary selection.
3074 clipboard_clear_selection_cb (GtkClipboard *clipboard,
3077 /* Move selection_bound to the insertion point */
3079 GtkTextIter selection_bound;
3080 GtkTextBuffer *buffer = GTK_TEXT_BUFFER (data);
3082 gtk_text_buffer_get_iter_at_mark (buffer, &insert,
3083 gtk_text_buffer_get_insert (buffer));
3084 gtk_text_buffer_get_iter_at_mark (buffer, &selection_bound,
3085 gtk_text_buffer_get_selection_bound (buffer));
3087 if (!gtk_text_iter_equal (&insert, &selection_bound))
3088 gtk_text_buffer_move_mark (buffer,
3089 gtk_text_buffer_get_selection_bound (buffer),
3093 /* Called when we have the primary selection and someone else wants our
3094 * data in order to paste it.
3097 clipboard_get_selection_cb (GtkClipboard *clipboard,
3098 GtkSelectionData *selection_data,
3102 GtkTextBuffer *buffer = GTK_TEXT_BUFFER (data);
3103 GtkTextIter start, end;
3105 if (gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
3107 if (info == GTK_TEXT_BUFFER_TARGET_INFO_BUFFER_CONTENTS)
3109 /* Provide the address of the buffer; this will only be
3110 * used within-process
3112 gtk_selection_data_set (selection_data,
3113 selection_data->target,
3118 else if (info == GTK_TEXT_BUFFER_TARGET_INFO_RICH_TEXT)
3123 str = gtk_text_buffer_serialize (buffer, buffer,
3124 selection_data->target,
3125 &start, &end, &len);
3127 gtk_selection_data_set (selection_data,
3128 selection_data->target,
3137 str = gtk_text_iter_get_visible_text (&start, &end);
3138 gtk_selection_data_set_text (selection_data, str, -1);
3144 static GtkTextBuffer *
3145 create_clipboard_contents_buffer (GtkTextBuffer *buffer)
3147 GtkTextBuffer *contents;
3149 contents = gtk_text_buffer_new (gtk_text_buffer_get_tag_table (buffer));
3151 g_object_set_data (G_OBJECT (contents), I_("gtk-text-buffer-clipboard-source"),
3153 g_object_set_data (G_OBJECT (contents), I_("gtk-text-buffer-clipboard"),
3154 GINT_TO_POINTER (1));
3156 /* Ref the source buffer as long as the clipboard contents buffer
3157 * exists, because it's needed for serializing the contents buffer.
3158 * See http://bugzilla.gnome.org/show_bug.cgi?id=339195
3160 g_object_ref (buffer);
3161 g_object_weak_ref (G_OBJECT (contents), (GWeakNotify) g_object_unref, buffer);
3166 /* Provide cut/copied data */
3168 clipboard_get_contents_cb (GtkClipboard *clipboard,
3169 GtkSelectionData *selection_data,
3173 GtkTextBuffer *contents = GTK_TEXT_BUFFER (data);
3175 g_assert (contents); /* This should never be called unless we own the clipboard */
3177 if (info == GTK_TEXT_BUFFER_TARGET_INFO_BUFFER_CONTENTS)
3179 /* Provide the address of the clipboard buffer; this will only
3180 * be used within-process. OK to supply a NULL value for contents.
3182 gtk_selection_data_set (selection_data,
3183 selection_data->target,
3188 else if (info == GTK_TEXT_BUFFER_TARGET_INFO_RICH_TEXT)
3190 GtkTextBuffer *clipboard_source_buffer;
3191 GtkTextIter start, end;
3195 clipboard_source_buffer = g_object_get_data (G_OBJECT (contents),
3196 "gtk-text-buffer-clipboard-source");
3198 gtk_text_buffer_get_bounds (contents, &start, &end);
3200 str = gtk_text_buffer_serialize (clipboard_source_buffer, contents,
3201 selection_data->target,
3202 &start, &end, &len);
3204 gtk_selection_data_set (selection_data,
3205 selection_data->target,
3213 GtkTextIter start, end;
3215 gtk_text_buffer_get_bounds (contents, &start, &end);
3217 str = gtk_text_iter_get_visible_text (&start, &end);
3218 gtk_selection_data_set_text (selection_data, str, -1);
3224 clipboard_clear_contents_cb (GtkClipboard *clipboard,
3227 GtkTextBuffer *contents = GTK_TEXT_BUFFER (data);
3229 g_object_unref (contents);
3233 get_paste_point (GtkTextBuffer *buffer,
3235 gboolean clear_afterward)
3237 GtkTextIter insert_point;
3238 GtkTextMark *paste_point_override;
3240 paste_point_override = gtk_text_buffer_get_mark (buffer,
3241 "gtk_paste_point_override");
3243 if (paste_point_override != NULL)
3245 gtk_text_buffer_get_iter_at_mark (buffer, &insert_point,
3246 paste_point_override);
3247 if (clear_afterward)
3248 gtk_text_buffer_delete_mark (buffer,
3249 gtk_text_buffer_get_mark (buffer,
3250 "gtk_paste_point_override"));
3254 gtk_text_buffer_get_iter_at_mark (buffer, &insert_point,
3255 gtk_text_buffer_get_insert (buffer));
3258 *iter = insert_point;
3262 pre_paste_prep (ClipboardRequest *request_data,
3263 GtkTextIter *insert_point)
3265 GtkTextBuffer *buffer = request_data->buffer;
3267 get_paste_point (buffer, insert_point, TRUE);
3269 /* If we're going to replace the selection, we insert before it to
3270 * avoid messing it up, then we delete the selection after inserting.
3272 if (request_data->replace_selection)
3274 GtkTextIter start, end;
3276 if (gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
3277 *insert_point = start;
3282 post_paste_cleanup (ClipboardRequest *request_data)
3284 if (request_data->replace_selection)
3286 GtkTextIter start, end;
3288 if (gtk_text_buffer_get_selection_bounds (request_data->buffer,
3291 if (request_data->interactive)
3292 gtk_text_buffer_delete_interactive (request_data->buffer,
3295 request_data->default_editable);
3297 gtk_text_buffer_delete (request_data->buffer, &start, &end);
3303 free_clipboard_request (ClipboardRequest *request_data)
3305 g_object_unref (request_data->buffer);
3306 g_free (request_data);
3309 /* Called when we request a paste and receive the text data
3312 clipboard_text_received (GtkClipboard *clipboard,
3316 ClipboardRequest *request_data = data;
3317 GtkTextBuffer *buffer = request_data->buffer;
3321 GtkTextIter insert_point;
3323 if (request_data->interactive)
3324 gtk_text_buffer_begin_user_action (buffer);
3326 pre_paste_prep (request_data, &insert_point);
3328 if (request_data->interactive)
3329 gtk_text_buffer_insert_interactive (buffer, &insert_point,
3330 str, -1, request_data->default_editable);
3332 gtk_text_buffer_insert (buffer, &insert_point,
3335 post_paste_cleanup (request_data);
3337 if (request_data->interactive)
3338 gtk_text_buffer_end_user_action (buffer);
3341 free_clipboard_request (request_data);
3344 static GtkTextBuffer*
3345 selection_data_get_buffer (GtkSelectionData *selection_data,
3346 ClipboardRequest *request_data)
3349 GtkTextBuffer *src_buffer = NULL;
3351 /* If we can get the owner, the selection is in-process */
3352 owner = gdk_selection_owner_get_for_display (selection_data->display,
3353 selection_data->selection);
3358 if (gdk_window_get_window_type (owner) == GDK_WINDOW_FOREIGN)
3361 if (selection_data->type !=
3362 gdk_atom_intern_static_string ("GTK_TEXT_BUFFER_CONTENTS"))
3365 if (selection_data->length != sizeof (src_buffer))
3368 memcpy (&src_buffer, selection_data->data, sizeof (src_buffer));
3370 if (src_buffer == NULL)
3373 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (src_buffer), NULL);
3375 if (gtk_text_buffer_get_tag_table (src_buffer) !=
3376 gtk_text_buffer_get_tag_table (request_data->buffer))
3383 /* These are pretty handy functions; maybe something like them
3384 * should be in the public API. Also, there are other places in this
3385 * file where they could be used.
3388 save_iter (const GtkTextIter *iter,
3389 gboolean left_gravity)
3391 return gtk_text_buffer_create_mark (gtk_text_iter_get_buffer (iter),
3398 restore_iter (const GtkTextIter *iter,
3401 gtk_text_buffer_get_iter_at_mark (gtk_text_mark_get_buffer (save_id),
3402 (GtkTextIter*) iter,
3404 gtk_text_buffer_delete_mark (gtk_text_mark_get_buffer (save_id),
3410 clipboard_rich_text_received (GtkClipboard *clipboard,
3416 ClipboardRequest *request_data = data;
3417 GtkTextIter insert_point;
3418 gboolean retval = TRUE;
3419 GError *error = NULL;
3421 if (text != NULL && length > 0)
3423 pre_paste_prep (request_data, &insert_point);
3425 if (request_data->interactive)
3426 gtk_text_buffer_begin_user_action (request_data->buffer);
3428 if (!request_data->interactive ||
3429 gtk_text_iter_can_insert (&insert_point,
3430 request_data->default_editable))
3432 retval = gtk_text_buffer_deserialize (request_data->buffer,
3433 request_data->buffer,
3442 g_warning ("error pasting: %s\n", error->message);
3443 g_clear_error (&error);
3446 if (request_data->interactive)
3447 gtk_text_buffer_end_user_action (request_data->buffer);
3451 post_paste_cleanup (request_data);
3456 /* Request the text selection instead */
3457 gtk_clipboard_request_text (clipboard,
3458 clipboard_text_received,
3463 paste_from_buffer (ClipboardRequest *request_data,
3464 GtkTextBuffer *src_buffer,
3465 const GtkTextIter *start,
3466 const GtkTextIter *end)
3468 GtkTextIter insert_point;
3469 GtkTextBuffer *buffer = request_data->buffer;
3471 /* We're about to emit a bunch of signals, so be safe */
3472 g_object_ref (src_buffer);
3474 pre_paste_prep (request_data, &insert_point);
3476 if (request_data->interactive)
3477 gtk_text_buffer_begin_user_action (buffer);
3479 if (!gtk_text_iter_equal (start, end))
3481 if (!request_data->interactive ||
3482 (gtk_text_iter_can_insert (&insert_point,
3483 request_data->default_editable)))
3484 gtk_text_buffer_real_insert_range (buffer,
3488 request_data->interactive);
3491 post_paste_cleanup (request_data);
3493 if (request_data->interactive)
3494 gtk_text_buffer_end_user_action (buffer);
3496 g_object_unref (src_buffer);
3498 free_clipboard_request (request_data);
3502 clipboard_clipboard_buffer_received (GtkClipboard *clipboard,
3503 GtkSelectionData *selection_data,
3506 ClipboardRequest *request_data = data;
3507 GtkTextBuffer *src_buffer;
3509 src_buffer = selection_data_get_buffer (selection_data, request_data);
3513 GtkTextIter start, end;
3515 if (g_object_get_data (G_OBJECT (src_buffer), "gtk-text-buffer-clipboard"))
3517 gtk_text_buffer_get_bounds (src_buffer, &start, &end);
3519 paste_from_buffer (request_data, src_buffer,
3524 if (gtk_text_buffer_get_selection_bounds (src_buffer, &start, &end))
3525 paste_from_buffer (request_data, src_buffer,
3531 if (gtk_clipboard_wait_is_rich_text_available (clipboard,
3532 request_data->buffer))
3534 /* Request rich text */
3535 gtk_clipboard_request_rich_text (clipboard,
3536 request_data->buffer,
3537 clipboard_rich_text_received,
3542 /* Request the text selection instead */
3543 gtk_clipboard_request_text (clipboard,
3544 clipboard_text_received,
3552 GtkClipboard *clipboard;
3554 } SelectionClipboard;
3557 update_selection_clipboards (GtkTextBuffer *buffer)
3559 GtkTextBufferPrivate *priv;
3560 GSList *tmp_list = buffer->selection_clipboards;
3562 priv = GTK_TEXT_BUFFER_GET_PRIVATE (buffer);
3564 gtk_text_buffer_get_copy_target_list (buffer);
3571 SelectionClipboard *selection_clipboard = tmp_list->data;
3572 GtkClipboard *clipboard = selection_clipboard->clipboard;
3574 /* Determine whether we have a selection and adjust X selection
3577 if (!gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
3579 if (gtk_clipboard_get_owner (clipboard) == G_OBJECT (buffer))
3580 gtk_clipboard_clear (clipboard);
3584 /* Even if we already have the selection, we need to update our
3587 if (!gtk_clipboard_set_with_owner (clipboard,
3588 priv->copy_target_entries,
3589 priv->n_copy_target_entries,
3590 clipboard_get_selection_cb,
3591 clipboard_clear_selection_cb,
3593 clipboard_clear_selection_cb (clipboard, buffer);
3596 tmp_list = tmp_list->next;
3600 static SelectionClipboard *
3601 find_selection_clipboard (GtkTextBuffer *buffer,
3602 GtkClipboard *clipboard)
3604 GSList *tmp_list = buffer->selection_clipboards;
3607 SelectionClipboard *selection_clipboard = tmp_list->data;
3608 if (selection_clipboard->clipboard == clipboard)
3609 return selection_clipboard;
3611 tmp_list = tmp_list->next;
3618 * gtk_text_buffer_add_selection_clipboard:
3619 * @buffer: a #GtkTextBuffer
3620 * @clipboard: a #GtkClipboard
3622 * Adds @clipboard to the list of clipboards in which the selection
3623 * contents of @buffer are available. In most cases, @clipboard will be
3624 * the #GtkClipboard of type %GDK_SELECTION_PRIMARY for a view of @buffer.
3627 gtk_text_buffer_add_selection_clipboard (GtkTextBuffer *buffer,
3628 GtkClipboard *clipboard)
3630 SelectionClipboard *selection_clipboard;
3632 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
3633 g_return_if_fail (clipboard != NULL);
3635 selection_clipboard = find_selection_clipboard (buffer, clipboard);
3636 if (selection_clipboard)
3638 selection_clipboard->ref_count++;
3642 selection_clipboard = g_new (SelectionClipboard, 1);
3644 selection_clipboard->clipboard = clipboard;
3645 selection_clipboard->ref_count = 1;
3647 buffer->selection_clipboards = g_slist_prepend (buffer->selection_clipboards, selection_clipboard);
3652 * gtk_text_buffer_remove_selection_clipboard:
3653 * @buffer: a #GtkTextBuffer
3654 * @clipboard: a #GtkClipboard added to @buffer by
3655 * gtk_text_buffer_add_selection_clipboard()
3657 * Removes a #GtkClipboard added with
3658 * gtk_text_buffer_add_selection_clipboard().
3661 gtk_text_buffer_remove_selection_clipboard (GtkTextBuffer *buffer,
3662 GtkClipboard *clipboard)
3664 SelectionClipboard *selection_clipboard;
3666 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
3667 g_return_if_fail (clipboard != NULL);
3669 selection_clipboard = find_selection_clipboard (buffer, clipboard);
3670 g_return_if_fail (selection_clipboard != NULL);
3672 selection_clipboard->ref_count--;
3673 if (selection_clipboard->ref_count == 0)
3675 if (gtk_clipboard_get_owner (selection_clipboard->clipboard) == G_OBJECT (buffer))
3676 gtk_clipboard_clear (selection_clipboard->clipboard);
3678 buffer->selection_clipboards = g_slist_remove (buffer->selection_clipboards,
3679 selection_clipboard);
3681 g_free (selection_clipboard);
3686 remove_all_selection_clipboards (GtkTextBuffer *buffer)
3688 g_slist_foreach (buffer->selection_clipboards, (GFunc)g_free, NULL);
3689 g_slist_free (buffer->selection_clipboards);
3690 buffer->selection_clipboards = NULL;
3694 * gtk_text_buffer_paste_clipboard:
3695 * @buffer: a #GtkTextBuffer
3696 * @clipboard: the #GtkClipboard to paste from
3697 * @override_location: location to insert pasted text, or %NULL for
3699 * @default_editable: whether the buffer is editable by default
3701 * Pastes the contents of a clipboard at the insertion point, or at
3702 * @override_location. (Note: pasting is asynchronous, that is, we'll
3703 * ask for the paste data and return, and at some point later after
3704 * the main loop runs, the paste data will be inserted.)
3707 gtk_text_buffer_paste_clipboard (GtkTextBuffer *buffer,
3708 GtkClipboard *clipboard,
3709 GtkTextIter *override_location,
3710 gboolean default_editable)
3712 ClipboardRequest *data = g_new (ClipboardRequest, 1);
3713 GtkTextIter paste_point;
3714 GtkTextIter start, end;
3716 if (override_location != NULL)
3717 gtk_text_buffer_create_mark (buffer,
3718 "gtk_paste_point_override",
3719 override_location, FALSE);
3721 data->buffer = g_object_ref (buffer);
3722 data->interactive = TRUE;
3723 data->default_editable = default_editable;
3725 /* When pasting with the cursor inside the selection area, you
3726 * replace the selection with the new text, otherwise, you
3727 * simply insert the new text at the point where the click
3728 * occured, unselecting any selected text. The replace_selection
3729 * flag toggles this behavior.
3731 data->replace_selection = FALSE;
3733 get_paste_point (buffer, &paste_point, FALSE);
3734 if (gtk_text_buffer_get_selection_bounds (buffer, &start, &end) &&
3735 (gtk_text_iter_in_range (&paste_point, &start, &end) ||
3736 gtk_text_iter_equal (&paste_point, &end)))
3737 data->replace_selection = TRUE;
3739 gtk_clipboard_request_contents (clipboard,
3740 gdk_atom_intern_static_string ("GTK_TEXT_BUFFER_CONTENTS"),
3741 clipboard_clipboard_buffer_received, data);
3745 * gtk_text_buffer_delete_selection:
3746 * @buffer: a #GtkTextBuffer
3747 * @interactive: whether the deletion is caused by user interaction
3748 * @default_editable: whether the buffer is editable by default
3750 * Deletes the range between the "insert" and "selection_bound" marks,
3751 * that is, the currently-selected text. If @interactive is %TRUE,
3752 * the editability of the selection will be considered (users can't delete
3755 * Return value: whether there was a non-empty selection to delete
3758 gtk_text_buffer_delete_selection (GtkTextBuffer *buffer,
3759 gboolean interactive,
3760 gboolean default_editable)
3765 if (!gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
3767 return FALSE; /* No selection */
3773 gtk_text_buffer_begin_user_action (buffer);
3774 gtk_text_buffer_delete_interactive (buffer, &start, &end, default_editable);
3775 gtk_text_buffer_end_user_action (buffer);
3778 gtk_text_buffer_delete (buffer, &start, &end);
3780 return TRUE; /* We deleted stuff */
3785 * gtk_text_buffer_backspace:
3786 * @buffer: a #GtkTextBuffer
3787 * @iter: a position in @buffer
3788 * @interactive: whether the deletion is caused by user interaction
3789 * @default_editable: whether the buffer is editable by default
3791 * Performs the appropriate action as if the user hit the delete
3792 * key with the cursor at the position specified by @iter. In the
3793 * normal case a single character will be deleted, but when
3794 * combining accents are involved, more than one character can
3795 * be deleted, and when precomposed character and accent combinations
3796 * are involved, less than one character will be deleted.
3798 * Because the buffer is modified, all outstanding iterators become
3799 * invalid after calling this function; however, the @iter will be
3800 * re-initialized to point to the location where text was deleted.
3802 * Return value: %TRUE if the buffer was modified
3807 gtk_text_buffer_backspace (GtkTextBuffer *buffer,
3809 gboolean interactive,
3810 gboolean default_editable)
3812 gchar *cluster_text;
3815 gboolean retval = FALSE;
3816 const PangoLogAttr *attrs;
3818 gboolean backspace_deletes_character;
3820 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
3821 g_return_val_if_fail (iter != NULL, FALSE);
3826 attrs = _gtk_text_buffer_get_line_log_attrs (buffer, &start, NULL);
3828 /* For no good reason, attrs is NULL for the empty last line in
3829 * a buffer. Special case that here. (#156164)
3833 offset = gtk_text_iter_get_line_offset (&start);
3834 backspace_deletes_character = attrs[offset].backspace_deletes_character;
3837 backspace_deletes_character = FALSE;
3839 gtk_text_iter_backward_cursor_position (&start);
3841 if (gtk_text_iter_equal (&start, &end))
3844 cluster_text = gtk_text_iter_get_text (&start, &end);
3847 gtk_text_buffer_begin_user_action (buffer);
3849 if (gtk_text_buffer_delete_interactive (buffer, &start, &end,
3852 if (backspace_deletes_character)
3854 gchar *normalized_text = g_utf8_normalize (cluster_text,
3855 strlen (cluster_text),
3857 glong len = g_utf8_strlen (normalized_text, -1);
3860 gtk_text_buffer_insert_interactive (buffer,
3863 g_utf8_offset_to_pointer (normalized_text, len - 1) - normalized_text,
3866 g_free (normalized_text);
3873 gtk_text_buffer_end_user_action (buffer);
3875 g_free (cluster_text);
3877 /* Revalidate the users iter */
3884 cut_or_copy (GtkTextBuffer *buffer,
3885 GtkClipboard *clipboard,
3886 gboolean delete_region_after,
3887 gboolean interactive,
3888 gboolean default_editable)
3890 GtkTextBufferPrivate *priv;
3892 /* We prefer to cut the selected region between selection_bound and
3893 * insertion point. If that region is empty, then we cut the region
3894 * between the "anchor" and the insertion point (this is for
3895 * C-space and M-w and other Emacs-style copy/yank keys). Note that
3896 * insert and selection_bound are guaranteed to exist, but the
3897 * anchor only exists sometimes.
3902 priv = GTK_TEXT_BUFFER_GET_PRIVATE (buffer);
3904 gtk_text_buffer_get_copy_target_list (buffer);
3906 if (!gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
3908 /* Let's try the anchor thing */
3909 GtkTextMark * anchor = gtk_text_buffer_get_mark (buffer, "anchor");
3915 gtk_text_buffer_get_iter_at_mark (buffer, &end, anchor);
3916 gtk_text_iter_order (&start, &end);
3920 if (!gtk_text_iter_equal (&start, &end))
3923 GtkTextBuffer *contents;
3925 contents = create_clipboard_contents_buffer (buffer);
3927 gtk_text_buffer_get_iter_at_offset (contents, &ins, 0);
3929 gtk_text_buffer_insert_range (contents, &ins, &start, &end);
3931 if (!gtk_clipboard_set_with_data (clipboard,
3932 priv->copy_target_entries,
3933 priv->n_copy_target_entries,
3934 clipboard_get_contents_cb,
3935 clipboard_clear_contents_cb,
3937 g_object_unref (contents);
3939 gtk_clipboard_set_can_store (clipboard,
3940 priv->copy_target_entries + 1,
3941 priv->n_copy_target_entries - 1);
3943 if (delete_region_after)
3946 gtk_text_buffer_delete_interactive (buffer, &start, &end,
3949 gtk_text_buffer_delete (buffer, &start, &end);
3955 * gtk_text_buffer_cut_clipboard:
3956 * @buffer: a #GtkTextBuffer
3957 * @clipboard: the #GtkClipboard object to cut to
3958 * @default_editable: default editability of the buffer
3960 * Copies the currently-selected text to a clipboard, then deletes
3961 * said text if it's editable.
3964 gtk_text_buffer_cut_clipboard (GtkTextBuffer *buffer,
3965 GtkClipboard *clipboard,
3966 gboolean default_editable)
3968 gtk_text_buffer_begin_user_action (buffer);
3969 cut_or_copy (buffer, clipboard, TRUE, TRUE, default_editable);
3970 gtk_text_buffer_end_user_action (buffer);
3974 * gtk_text_buffer_copy_clipboard:
3975 * @buffer: a #GtkTextBuffer
3976 * @clipboard: the #GtkClipboard object to copy to
3978 * Copies the currently-selected text to a clipboard.
3981 gtk_text_buffer_copy_clipboard (GtkTextBuffer *buffer,
3982 GtkClipboard *clipboard)
3984 gtk_text_buffer_begin_user_action (buffer);
3985 cut_or_copy (buffer, clipboard, FALSE, TRUE, TRUE);
3986 gtk_text_buffer_end_user_action (buffer);
3990 * gtk_text_buffer_get_selection_bounds:
3991 * @buffer: a #GtkTextBuffer a #GtkTextBuffer
3992 * @start: iterator to initialize with selection start
3993 * @end: iterator to initialize with selection end
3995 * Returns %TRUE if some text is selected; places the bounds
3996 * of the selection in @start and @end (if the selection has length 0,
3997 * then @start and @end are filled in with the same value).
3998 * @start and @end will be in ascending order. If @start and @end are
3999 * NULL, then they are not filled in, but the return value still indicates
4000 * whether text is selected.
4002 * Return value: whether the selection has nonzero length
4005 gtk_text_buffer_get_selection_bounds (GtkTextBuffer *buffer,
4009 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
4011 return _gtk_text_btree_get_selection_bounds (get_btree (buffer), start, end);
4015 * gtk_text_buffer_begin_user_action:
4016 * @buffer: a #GtkTextBuffer
4018 * Called to indicate that the buffer operations between here and a
4019 * call to gtk_text_buffer_end_user_action() are part of a single
4020 * user-visible operation. The operations between
4021 * gtk_text_buffer_begin_user_action() and
4022 * gtk_text_buffer_end_user_action() can then be grouped when creating
4023 * an undo stack. #GtkTextBuffer maintains a count of calls to
4024 * gtk_text_buffer_begin_user_action() that have not been closed with
4025 * a call to gtk_text_buffer_end_user_action(), and emits the
4026 * "begin_user_action" and "end_user_action" signals only for the
4027 * outermost pair of calls. This allows you to build user actions
4028 * from other user actions.
4030 * The "interactive" buffer mutation functions, such as
4031 * gtk_text_buffer_insert_interactive(), automatically call begin/end
4032 * user action around the buffer operations they perform, so there's
4033 * no need to add extra calls if you user action consists solely of a
4034 * single call to one of those functions.
4037 gtk_text_buffer_begin_user_action (GtkTextBuffer *buffer)
4039 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
4041 buffer->user_action_count += 1;
4043 if (buffer->user_action_count == 1)
4045 /* Outermost nested user action begin emits the signal */
4046 g_signal_emit (buffer, signals[BEGIN_USER_ACTION], 0);
4051 * gtk_text_buffer_end_user_action:
4052 * @buffer: a #GtkTextBuffer
4054 * Should be paired with a call to gtk_text_buffer_begin_user_action().
4055 * See that function for a full explanation.
4058 gtk_text_buffer_end_user_action (GtkTextBuffer *buffer)
4060 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
4061 g_return_if_fail (buffer->user_action_count > 0);
4063 buffer->user_action_count -= 1;
4065 if (buffer->user_action_count == 0)
4067 /* Ended the outermost-nested user action end, so emit the signal */
4068 g_signal_emit (buffer, signals[END_USER_ACTION], 0);
4073 gtk_text_buffer_free_target_lists (GtkTextBuffer *buffer)
4075 GtkTextBufferPrivate *priv = GTK_TEXT_BUFFER_GET_PRIVATE (buffer);
4077 if (priv->copy_target_list)
4079 gtk_target_list_unref (priv->copy_target_list);
4080 priv->copy_target_list = NULL;
4082 gtk_target_table_free (priv->copy_target_entries,
4083 priv->n_copy_target_entries);
4084 priv->copy_target_entries = NULL;
4085 priv->n_copy_target_entries = 0;
4088 if (priv->paste_target_list)
4090 gtk_target_list_unref (priv->paste_target_list);
4091 priv->paste_target_list = NULL;
4093 gtk_target_table_free (priv->paste_target_entries,
4094 priv->n_paste_target_entries);
4095 priv->paste_target_entries = NULL;
4096 priv->n_paste_target_entries = 0;
4100 static GtkTargetList *
4101 gtk_text_buffer_get_target_list (GtkTextBuffer *buffer,
4102 gboolean deserializable,
4103 GtkTargetEntry **entries,
4106 GtkTargetList *target_list;
4108 target_list = gtk_target_list_new (NULL, 0);
4110 gtk_target_list_add (target_list,
4111 gdk_atom_intern_static_string ("GTK_TEXT_BUFFER_CONTENTS"),
4112 GTK_TARGET_SAME_APP,
4113 GTK_TEXT_BUFFER_TARGET_INFO_BUFFER_CONTENTS);
4115 gtk_target_list_add_rich_text_targets (target_list,
4116 GTK_TEXT_BUFFER_TARGET_INFO_RICH_TEXT,
4120 gtk_target_list_add_text_targets (target_list,
4121 GTK_TEXT_BUFFER_TARGET_INFO_TEXT);
4123 *entries = gtk_target_table_new_from_list (target_list, n_entries);
4129 * gtk_text_buffer_get_copy_target_list:
4130 * @buffer: a #GtkTextBuffer
4132 * This function returns the list of targets this text buffer can
4133 * provide for copying and as DND source. The targets in the list are
4134 * added with %info values from the #GtkTextBufferTargetInfo enum,
4135 * using gtk_target_list_add_rich_text_targets() and
4136 * gtk_target_list_add_text_targets().
4138 * Return value: the #GtkTargetList
4143 gtk_text_buffer_get_copy_target_list (GtkTextBuffer *buffer)
4145 GtkTextBufferPrivate *priv;
4147 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
4149 priv = GTK_TEXT_BUFFER_GET_PRIVATE (buffer);
4151 if (! priv->copy_target_list)
4152 priv->copy_target_list =
4153 gtk_text_buffer_get_target_list (buffer, FALSE,
4154 &priv->copy_target_entries,
4155 &priv->n_copy_target_entries);
4157 return priv->copy_target_list;
4161 * gtk_text_buffer_get_paste_target_list:
4162 * @buffer: a #GtkTextBuffer
4164 * This function returns the list of targets this text buffer supports
4165 * for pasting and as DND destination. The targets in the list are
4166 * added with %info values from the #GtkTextBufferTargetInfo enum,
4167 * using gtk_target_list_add_rich_text_targets() and
4168 * gtk_target_list_add_text_targets().
4170 * Return value: the #GtkTargetList
4175 gtk_text_buffer_get_paste_target_list (GtkTextBuffer *buffer)
4177 GtkTextBufferPrivate *priv;
4179 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
4181 priv = GTK_TEXT_BUFFER_GET_PRIVATE (buffer);
4183 if (! priv->paste_target_list)
4184 priv->paste_target_list =
4185 gtk_text_buffer_get_target_list (buffer, TRUE,
4186 &priv->paste_target_entries,
4187 &priv->n_paste_target_entries);
4189 return priv->paste_target_list;
4193 * Logical attribute cache
4196 #define ATTR_CACHE_SIZE 2
4198 typedef struct _CacheEntry CacheEntry;
4203 PangoLogAttr *attrs;
4206 struct _GtkTextLogAttrCache
4208 gint chars_changed_stamp;
4209 CacheEntry entries[ATTR_CACHE_SIZE];
4213 free_log_attr_cache (GtkTextLogAttrCache *cache)
4216 while (i < ATTR_CACHE_SIZE)
4218 g_free (cache->entries[i].attrs);
4225 clear_log_attr_cache (GtkTextLogAttrCache *cache)
4228 while (i < ATTR_CACHE_SIZE)
4230 g_free (cache->entries[i].attrs);
4231 cache->entries[i].attrs = NULL;
4236 static PangoLogAttr*
4237 compute_log_attrs (const GtkTextIter *iter,
4243 gint char_len, byte_len;
4244 PangoLogAttr *attrs = NULL;
4249 gtk_text_iter_set_line_offset (&start, 0);
4250 gtk_text_iter_forward_line (&end);
4252 paragraph = gtk_text_iter_get_slice (&start, &end);
4253 char_len = g_utf8_strlen (paragraph, -1);
4254 byte_len = strlen (paragraph);
4256 g_assert (char_len > 0);
4259 *char_lenp = char_len;
4261 attrs = g_new (PangoLogAttr, char_len + 1);
4263 /* FIXME we need to follow PangoLayout and allow different language
4264 * tags within the paragraph
4266 pango_get_log_attrs (paragraph, byte_len, -1,
4267 gtk_text_iter_get_language (&start),
4276 /* The return value from this is valid until you call this a second time.
4279 _gtk_text_buffer_get_line_log_attrs (GtkTextBuffer *buffer,
4280 const GtkTextIter *anywhere_in_line,
4284 GtkTextLogAttrCache *cache;
4287 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
4288 g_return_val_if_fail (anywhere_in_line != NULL, NULL);
4290 /* special-case for empty last line in buffer */
4291 if (gtk_text_iter_is_end (anywhere_in_line) &&
4292 gtk_text_iter_get_line_offset (anywhere_in_line) == 0)
4299 /* FIXME we also need to recompute log attrs if the language tag at
4300 * the start of a paragraph changes
4303 if (buffer->log_attr_cache == NULL)
4305 buffer->log_attr_cache = g_new0 (GtkTextLogAttrCache, 1);
4306 buffer->log_attr_cache->chars_changed_stamp =
4307 _gtk_text_btree_get_chars_changed_stamp (get_btree (buffer));
4309 else if (buffer->log_attr_cache->chars_changed_stamp !=
4310 _gtk_text_btree_get_chars_changed_stamp (get_btree (buffer)))
4312 clear_log_attr_cache (buffer->log_attr_cache);
4315 cache = buffer->log_attr_cache;
4316 line = gtk_text_iter_get_line (anywhere_in_line);
4319 while (i < ATTR_CACHE_SIZE)
4321 if (cache->entries[i].attrs &&
4322 cache->entries[i].line == line)
4325 *char_len = cache->entries[i].char_len;
4326 return cache->entries[i].attrs;
4331 /* Not in cache; open up the first cache entry */
4332 g_free (cache->entries[ATTR_CACHE_SIZE-1].attrs);
4334 g_memmove (cache->entries + 1, cache->entries,
4335 sizeof (CacheEntry) * (ATTR_CACHE_SIZE - 1));
4337 cache->entries[0].line = line;
4338 cache->entries[0].attrs = compute_log_attrs (anywhere_in_line,
4339 &cache->entries[0].char_len);
4342 *char_len = cache->entries[0].char_len;
4344 return cache->entries[0].attrs;
4348 _gtk_text_buffer_notify_will_remove_tag (GtkTextBuffer *buffer,
4351 /* This removes tag from the buffer, but DOESN'T emit the
4352 * remove_tag signal, because we can't afford to have user
4353 * code messing things up at this point; the tag MUST be removed
4357 _gtk_text_btree_notify_will_remove_tag (buffer->btree, tag);
4365 _gtk_text_buffer_spew (GtkTextBuffer *buffer)
4367 _gtk_text_btree_spew (get_btree (buffer));
4370 #define __GTK_TEXT_BUFFER_C__
4371 #include "gtkaliasdef.c"