Stores attributed text for display in a GtkTextView
Class GtkTextBuffer( [table] )
table | (GtkTextTable) a tag table, or nil to create a new one. |
You may wish to begin by reading the text widget conceptual overview which gives an overview of all the objects and data types related to the text widget and how they work together.
Methods | |
add_mark | Adds the mark at position where. |
apply_tag | Emits the "apply-tag" signal on buffer. |
apply_tag_by_name | Calls gtk_text_tag_table_lookup() on the buffer's tag table to get a GtkTextTag, then calls gtk_text_buffer_apply_tag(). |
backspace | Performs the appropriate action as if the user hit the delete key with the cursor at the position specified by iter. |
begin_user_action | Called to indicate that the buffer operations between here and a call to gtk_text_buffer_end_user_action() are part of a single user-visible operation. |
create_mark | Creates a mark at position where. |
delete | Deletes text between start and end. |
delete_interactive | Deletes all editable text in the given range. |
delete_mark | Deletes mark, so that it's no longer located anywhere in the buffer. |
delete_mark_by_name | Deletes the mark named name; the mark must exist. |
delete_selection | Deletes the range between the "insert" and "selection_bound" marks, that is, the currently-selected text. |
end_user_action | Should be paired with a call to begin_user_action(). See that function for a full explanation. |
get_bounds | Retrieves the first and last iterators in the buffer, i.e. the entire buffer lies within the range [start,end]. |
get_char_count | Gets the number of characters in the buffer. |
get_end_iter | Initializes iter with the "end iterator," one past the last valid character in the text buffer. |
get_has_selection | Indicates whether the buffer has some text currently selected. |
get_insert | Returns the mark that represents the cursor (insertion point). |
get_iter_at_line | Initializes iter to the start of the given line. |
get_iter_at_line_index | Obtains an iterator pointing to byte_index within the given line. |
get_iter_at_line_offset | Obtains an iterator pointing to char_offset within the given line. |
get_iter_at_offset | Initializes iter to a position char_offset chars from the start of the entire buffer. |
get_line_count | Obtains the number of lines in the buffer. |
get_mark | Returns the mark named name in buffer buffer, or nil if no such mark exists in the buffer. |
get_modified | Indicates whether the buffer has been modified since the last call to set_modified() set the modification flag to false. |
get_selection_bound | Returns the mark that represents the selection bound. |
get_selection_bounds | Returns true if some text is selected; places the bounds of the selection in start and end (if the selection has length 0, then start and end are filled in with the same value). |
get_slice | Returns the text in the range [start,end). |
get_start_iter | Initialize iter with the first position in the text buffer. |
get_tag_table | Get the GtkTextTagTable associated with this buffer. |
get_text | Returns the text in the range [start,end). |
insert | Inserts len bytes of text at position iter. |
insert_at_cursor | Simply calls insert(), using the current cursor position as the insertion point. |
insert_interactive | Like insert(), but the insertion will not occur if iter is at a non-editable location in the buffer. |
insert_interactive_at_cursor | Calls gtk_text_buffer_insert_interactive() at the cursor position. |
insert_pixbuf | Inserts an image into the text buffer at iter. |
insert_range | Copies text, tags, and pixbufs between start and end (the order of start and end doesn't matter) and inserts the copy at iter. |
insert_range_interactive | Like insert(), but the insertion will not occur if iter is at a non-editable location in the buffer. |
move_mark | Moves mark to the new location where. |
move_mark_by_name | Moves the mark named name (which must exist) to location where. |
place_cursor | This function moves the "insert" and "selection_bound" marks simultaneously. |
remove_all_tags | Removes all tags in the range between start and end. |
remove_tag | Emits the "remove-tag" signal. |
remove_tag_by_name | Calls gtk_text_tag_table_lookup() on the buffer's tag table to get a GtkTextTag, then calls gtk_text_buffer_remove_tag(). |
select_range | This function moves the "insert" and "selection_bound" marks simultaneously. |
set_modified | Used to keep track of whether the buffer has been modified since the last time it was saved. |
set_text | Deletes current contents of buffer, and inserts text instead. |
Adds the mark at position where.
GtkTextBuffer.add_mark( mark, where )
mark | the mark to add |
where | location to place mark |
The mark must not be added to another buffer, and if its name is not NULL then there must not be another mark in the buffer with the same name.
Emits the "mark-set" signal as notification of the mark's initial placement.
Emits the "apply-tag" signal on buffer.
GtkTextBuffer.apply_tag( tag, start, end )
tag | a GtkTextTag |
start | one bound of range to be tagged |
end | other bound of range to be tagged |
The default handler for the signal applies tag to the given range. start and end do not have to be in order.
Calls gtk_text_tag_table_lookup() on the buffer's tag table to get a GtkTextTag, then calls gtk_text_buffer_apply_tag().
GtkTextBuffer.apply_tag_by_name( name, start, end )
name | name of a named GtkTextTag |
start | one bound of range to be tagged |
end | other bound of range to be tagged |
Performs the appropriate action as if the user hit the delete key with the cursor at the position specified by iter.
GtkTextBuffer.backspace( iter, interactive, default_editable )
iter | a position in buffer |
interactive | whether the deletion is caused by user interaction |
default_editable | whether the buffer is editable by default |
Return | true if the buffer was modified |
In the normal case a single character will be deleted, but when combining accents are involved, more than one character can be deleted, and when precomposed character and accent combinations are involved, less than one character will be deleted.
Because the buffer is modified, all outstanding iterators become invalid after calling this function; however, the iter will be re-initialized to point to the location where text was deleted.
Called to indicate that the buffer operations between here and a call to gtk_text_buffer_end_user_action() are part of a single user-visible operation.
GtkTextBuffer.begin_user_action()
The operations between begin_user_action() and end_user_action() can then be grouped when creating an undo stack. GtkTextBuffer maintains a count of calls to begin_user_action() that have not been closed with a call to end_user_action(), and emits the "begin-user-action" and "end-user-action" signals only for the outermost pair of calls. This allows you to build user actions from other user actions.
The "interactive" buffer mutation functions, such as insert_interactive(), automatically call begin/end user action around the buffer operations they perform, so there's no need to add extra calls if you user action consists solely of a single call to one of those functions.
Creates a mark at position where.
GtkTextBuffer.create_mark( mark_name, where, left_gravity )
mark_name | name for mark, or nil |
where | (GtkTextIter) location to place mark |
left_gravity | (boolean) whether the mark has left gravity |
If mark_name is NULL, the mark is anonymous; otherwise, the mark can be retrieved by name using gtk_text_buffer_get_mark(). If a mark has left gravity, and text is inserted at the mark's current location, the mark will be moved to the left of the newly-inserted text. If the mark has right gravity (left_gravity = FALSE), the mark will end up on the right of newly-inserted text. The standard left-to-right cursor is a mark with right gravity (when you type, the cursor stays on the right side of the text you're typing).
The caller of this function does not own a reference to the returned GtkTextMark, so you can ignore the return value if you like. Marks are owned by the buffer and go away when the buffer does.
Emits the "mark-set" signal as notification of the mark's initial placement.
Deletes text between start and end.
GtkTextBuffer.delete( start, end )
start | a position in buffer |
end | another position in buffer |
The order of start and end is not actually relevant; delete() will reorder them. This function actually emits the "delete-range" signal, and the default handler of that signal deletes the text. Because the buffer is modified, all outstanding iterators become invalid after calling this function; however, the start and end will be re-initialized to point to the location where text was deleted.
Deletes all editable text in the given range.
GtkTextBuffer.delete_interactive( start, end, default_editable )
start | a position in buffer |
end | another position in buffer |
default_editable | whether the buffer is editable by default |
Return | whether some text was actually deleted |
Calls delete() for each editable sub-range of [start,end]. start and end are revalidated to point to the location of the last deleted range, or left untouched if no text was deleted.
Deletes mark, so that it's no longer located anywhere in the buffer.
GtkTextBuffer.delete_mark( amrk )
amrk | a GtkTextMark in buffer |
Most operations on mark become invalid, until the mark gets added to a buffer again with gtk_text_buffer_add_mark(). Use gtk_text_mark_get_deleted() to find out if a mark has been removed from its buffer. The "mark-deleted" signal will be emitted as notification after the mark is deleted.
Deletes the mark named name; the mark must exist.
GtkTextBuffer.delete_mark_by_name( name )
name | name of a mark in buffer |
See delete_mark() for details.
Deletes the range between the "insert" and "selection_bound" marks, that is, the currently-selected text.
GtkTextBuffer.delete_selection( interactive, default_editable )
interactive | whether the deletion is caused by user interaction |
default_editable | whether the buffer is editable by default |
Return | whether there was a non-empty selection to delete |
If interactive is true, the editability of the selection will be considered (users can't delete uneditable text).
Should be paired with a call to begin_user_action(). See that function for a full explanation.
GtkTextBuffer.end_user_action()
Retrieves the first and last iterators in the buffer, i.e. the entire buffer lies within the range [start,end].
GtkTextBuffer.get_bounds()
Return | [ GtkTextIter, GtkTextIter ] |
Gets the number of characters in the buffer.
GtkTextBuffer.get_char_count()
Note that characters and bytes are not the same, you can't e.g. expect the contents of the buffer in string form to be this many bytes long. The character count is cached, so this function is very fast.
Initializes iter with the "end iterator," one past the last valid character in the text buffer.
GtkTextBuffer.get_end_iter()
Return | (GtkTextIter) |
If dereferenced with gtk_text_iter_get_char(), the end iterator has a character value of 0. The entire buffer lies in the range from the first position in the buffer (call gtk_text_buffer_get_start_iter() to get character position 0) to the end iterator.
Indicates whether the buffer has some text currently selected.
GtkTextBuffer.get_has_selection()
Return | true if the there is text selected |
Returns the mark that represents the cursor (insertion point).
GtkTextBuffer.get_insert()
Return | (GtkTextMark) insertion point mark. |
Equivalent to calling gtk_text_buffer_get_mark() to get the mark named "insert", but very slightly more efficient, and involves less typing.
Initializes iter to the start of the given line.
GtkTextBuffer.get_iter_at_line( line_number )
line_number | line number counting from 0 |
Return | (GtkTextIter) |
Obtains an iterator pointing to byte_index within the given line.
GtkTextBuffer.get_iter_at_line_index( line_number, byte_offset )
line_number | line number counting from 0 |
byte_offset | byte index from start of line |
Return | (GtkTextIter) |
byte_index must be the start of a UTF-8 character, and must not be beyond the end of the line. Note bytes, not characters; UTF-8 may encode one character as multiple bytes.
Obtains an iterator pointing to char_offset within the given line.
GtkTextBuffer.get_iter_at_line_offset( line_number, line_offset )
line_number | line number counting from 0 |
line_offset | char offset from start of line |
Return | (GtkTextIter) |
The char_offset must exist, offsets off the end of the line are not allowed. Note characters, not bytes; UTF-8 may encode one character as multiple bytes.
Initializes iter to a position char_offset chars from the start of the entire buffer.
GtkTextBuffer.get_iter_at_offset( char_offset )
char_offset | char offset from start of buffer, counting from 0, or -1 |
Return | (GtkTextIter) |
If char_offset is -1 or greater than the number of characters in the buffer, iter is initialized to the end iterator, the iterator one past the last valid character in the buffer.
Obtains the number of lines in the buffer.
GtkTextBuffer.get_line_count()
Return | number of lines in the buffer |
This value is cached, so the function is very fast.
Returns the mark named name in buffer buffer, or nil if no such mark exists in the buffer.
GtkTextBuffer.get_mark( name )
name | a mark name |
Return | a GtkTextMark, or nil. |
Indicates whether the buffer has been modified since the last call to set_modified() set the modification flag to false.
GtkTextBuffer.get_modified()
Return | true if the buffer has been modified Used for example to enable a "save" function in a text editor. |
Returns the mark that represents the selection bound.
GtkTextBuffer.get_selection_bound()
Return | (GtkTextMark) selection bound mark. |
Equivalent to calling get_mark() to get the mark named "selection_bound", but very slightly more efficient, and involves less typing.
The currently-selected text in buffer is the region between the "selection_bound" and "insert" marks. If "selection_bound" and "insert" are in the same place, then there is no current selection. get_selection_bounds() is another convenient function for handling the selection, if you just want to know whether there's a selection and what its bounds are.
Returns true if some text is selected; places the bounds of the selection in start and end (if the selection has length 0, then start and end are filled in with the same value).
GtkTextBuffer.get_selection_bounds()
Return | [ boolean (whether the selection has nonzero length ), (GtkTextIter) start, (GtkTextIter) end ] |
start and end will be in ascending order. If start and end are nil, then they are not filled in, but the return value still indicates whether text is selected.
Returns the text in the range [start,end).
GtkTextBuffer.get_slice( start, end, include_hidden_chars )
start | start of range |
end | end of range |
include_hidden_chars | whether to include invisible text |
Return | an UTF-8 string |
Excludes undisplayed text (text marked with tags that set the invisibility attribute) if include_hidden_chars is FALSE. The returned string includes a 0xFFFC character whenever the buffer contains embedded images, so byte and character indexes into the returned string do correspond to byte and character indexes into the buffer. Contrast with gtk_text_buffer_get_text(). Note that 0xFFFC can occur in normal text as well, so it is not a reliable indicator that a pixbuf or widget is in the buffer.
Initialize iter with the first position in the text buffer.
GtkTextBuffer.get_start_iter()
Return | (GtkTextIter) |
This is the same as using gtk_text_buffer_get_iter_at_offset() to get the iter at character offset 0.
Get the GtkTextTagTable associated with this buffer.
GtkTextBuffer.get_tag_table()
Return | the buffer's tag table. |
Returns the text in the range [start,end).
GtkTextBuffer.get_text( start, end, include_hidden_chars )
start | start of range |
end | end of range |
include_hidden_chars | whether to include invisible text |
Return | an UTF-8 string |
Excludes undisplayed text (text marked with tags that set the invisibility attribute) if include_hidden_chars is FALSE. Does not include characters representing embedded images, so byte and character indexes into the returned string do not correspond to byte and character indexes into the buffer. Contrast with gtk_text_buffer_get_slice().
Inserts len bytes of text at position iter.
GtkTextBuffer.insert( iter, text, len )
iter | (GtkTextIter) a position in the buffer |
text | some text in UTF-8 format |
len | length of text, in bytes |
If len is -1, text must be nul-terminated and will be inserted in its entirety.
Emits the "insert-text" signal; insertion actually occurs in the default handler for the signal. iter is invalidated when insertion occurs (because the buffer contents change), but the default signal handler revalidates it to point to the end of the inserted text.
Simply calls insert(), using the current cursor position as the insertion point.
GtkTextBuffer.insert_at_cursor( text, len )
text | some text in UTF-8 format |
len | length of text, in bytes |
Like insert(), but the insertion will not occur if iter is at a non-editable location in the buffer.
GtkTextBuffer.insert_interactive( iter, text, len, default_editable )
iter | (GtkTextIter) a position in the buffer |
text | some text in UTF-8 format |
len | length of text, in bytes |
default_editable | (boolean) |
Return | whether text was actually inserted |
Usually you want to prevent insertions at ineditable locations if the insertion results from a user action (is interactive).
default_editable indicates the editability of text that doesn't have a tag affecting editability applied to it. Typically the result of gtk_text_view_get_editable() is appropriate here.
Calls gtk_text_buffer_insert_interactive() at the cursor position.
GtkTextBuffer.insert_interactive_at_cursor( text, len, default_editable )
text | some text in UTF-8 format |
len | length of text, in bytes |
default_editable | (boolean) default editability of buffer |
Return | whether text was actually inserted |
default_editable indicates the editability of text that doesn't have a tag affecting editability applied to it. Typically the result of gtk_text_view_get_editable() is appropriate here.
Inserts an image into the text buffer at iter.
GtkTextBuffer.insert_pixbuf( iter, pixbuf )
iter | location to insert the pixbuf |
pixbuf | a GdkPixbuf |
The image will be counted as one character in character counts, and when obtaining the buffer contents as a string, will be represented by the Unicode "object replacement character" 0xFFFC. Note that the "slice" variants for obtaining portions of the buffer as a string include this character for pixbufs, but the "text" variants do not. e.g. see gtk_text_buffer_get_slice() and gtk_text_buffer_get_text().
Copies text, tags, and pixbufs between start and end (the order of start and end doesn't matter) and inserts the copy at iter.
GtkTextBuffer.insert_range( iter, start, end )
iter | (GtkTextIter) a position in buffer |
start | (GtkTextIter) a position in a GtkTextBuffer |
end | (GtkTextIter) another position in the same buffer as start |
Used instead of simply getting/inserting text because it preserves images and tags. If start and end are in a different buffer from buffer, the two buffers must share the same tag table.
Implemented via emissions of the insert_text and apply_tag signals, so expect those.
Like insert(), but the insertion will not occur if iter is at a non-editable location in the buffer.
GtkTextBuffer.insert_range_interactive( iter, start, end, default_editable )
iter | (GtkTextIter) a position in buffer |
start | (GtkTextIter) a position in a GtkTextBuffer |
end | (GtkTextIter) another position in the same buffer as start |
default_editable | (boolean) default editability of buffer |
Return | whether an insertion was possible at iter |
Usually you want to prevent insertions at ineditable locations if the insertion results from a user action (is interactive).
default_editable indicates the editability of text that doesn't have a tag affecting editability applied to it. Typically the result of gtk_text_view_get_editable() is appropriate here.
Moves mark to the new location where.
GtkTextBuffer.move_mark( mark, where )
mark | a GtkTextMark |
where | new location for mark in buffer |
Emits the "mark-set" signal as notification of the move.
Moves the mark named name (which must exist) to location where.
GtkTextBuffer.move_mark_by_name( name, where )
name | name of a mark |
where | new location for mark |
See move_mark() for details.
This function moves the "insert" and "selection_bound" marks simultaneously.
GtkTextBuffer.place_cursor( where )
where | (GtkTextIter) where to put the cursor |
If you move them to the same place in two steps with move_mark(), you will temporarily select a region in between their old and new locations, which can be pretty inefficient since the temporarily-selected region will force stuff to be recalculated. This function moves them as a unit, which can be optimized.
Removes all tags in the range between start and end.
GtkTextBuffer.remove_all_tags( start, end )
start | one bound of range to be untagged |
end | other bound of range to be untagged |
Be careful with this function; it could remove tags added in code unrelated to the code you're currently writing. That is, using this function is probably a bad idea if you have two or more unrelated code sections that add tags.
Emits the "remove-tag" signal.
GtkTextBuffer.remove_tag( tag, start, end )
tag | a GtkTextTag |
start | one bound of range to be untagged |
end | other bound of range to be untagged |
The default handler for the signal removes all occurrences of tag from the given range. start and end don't have to be in order.
Calls gtk_text_tag_table_lookup() on the buffer's tag table to get a GtkTextTag, then calls gtk_text_buffer_remove_tag().
GtkTextBuffer.remove_tag_by_name( name, start, end )
name | name of a named GtkTextTag |
start | one bound of range to be untagged |
end | other bound of range to be untagged |
This function moves the "insert" and "selection_bound" marks simultaneously.
GtkTextBuffer.select_range( ins, bound )
ins | (GtkTextIter) where to put the "insert" mark |
bound | (GtkTextIter) where to put the "selection_bound" mark |
If you move them in two steps with move_mark(), you will temporarily select a region in between their old and new locations, which can be pretty inefficient since the temporarily-selected region will force stuff to be recalculated. This function moves them as a unit, which can be optimized.
Used to keep track of whether the buffer has been modified since the last time it was saved.
GtkTextBuffer.set_modified( setting )
setting | modification flag setting |
Whenever the buffer is saved to disk, call set_modified (buffer, FALSE). When the buffer is modified, it will automatically toggled on the modified bit again. When the modified bit flips, the buffer emits a "modified-changed" signal.
Deletes current contents of buffer, and inserts text instead.
GtkTextBuffer.set_text( text, len )
text | UTF-8 text to insert |
len | length of text in bytes |
If len is -1, text must be nul-terminated. text must be valid UTF-8.