2008-07-10 03:27:56 +00:00
|
|
|
<?xml version="1.0"?>
|
|
|
|
<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.3//EN"
|
|
|
|
"http://www.oasis-open.org/docbook/xml/4.3/docbookx.dtd" [
|
|
|
|
]>
|
2011-01-15 00:35:34 +00:00
|
|
|
<refentry id="TextWidget">
|
2000-10-24 00:15:14 +00:00
|
|
|
<refmeta>
|
|
|
|
<refentrytitle>Text Widget Overview</refentrytitle>
|
|
|
|
<manvolnum>3</manvolnum>
|
|
|
|
<refmiscinfo>GTK Library</refmiscinfo>
|
|
|
|
</refmeta>
|
|
|
|
|
|
|
|
<refnamediv>
|
2005-05-16 19:34:39 +00:00
|
|
|
<refname>Text Widget Overview</refname>
|
2007-06-11 03:41:27 +00:00
|
|
|
<refpurpose>Overview of GtkTextBuffer, GtkTextView, and friends</refpurpose>
|
2000-10-24 00:15:14 +00:00
|
|
|
</refnamediv>
|
|
|
|
|
|
|
|
<refsect1>
|
|
|
|
<title>Conceptual Overview</title>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
GTK+ has an extremely powerful framework for multiline text editing. The
|
2005-05-16 19:34:39 +00:00
|
|
|
primary objects involved in the process are #GtkTextBuffer, which represents the
|
|
|
|
text being edited, and #GtkTextView, a widget which can display a #GtkTextBuffer.
|
|
|
|
Each buffer can be displayed by any number of views.
|
2000-10-24 00:15:14 +00:00
|
|
|
</para>
|
|
|
|
|
2000-10-24 22:44:14 +00:00
|
|
|
<para>
|
|
|
|
One of the important things to remember about text in GTK+ is that it's in the
|
|
|
|
UTF-8 encoding. This means that one character can be encoded as multiple
|
|
|
|
bytes. Character counts are usually referred to as
|
|
|
|
<firstterm>offsets</firstterm>, while byte counts are called
|
|
|
|
<firstterm>indexes</firstterm>. If you confuse these two, things will work fine
|
2002-12-11 19:26:58 +00:00
|
|
|
with ASCII, but as soon as your buffer contains multibyte characters, bad
|
|
|
|
things will happen.
|
2000-10-24 22:44:14 +00:00
|
|
|
</para>
|
|
|
|
|
2000-10-24 00:15:14 +00:00
|
|
|
<para>
|
|
|
|
Text in a buffer can be marked with <firstterm>tags</firstterm>. A tag is an
|
2002-12-11 19:26:58 +00:00
|
|
|
attribute that can be applied to some range of text. For example, a tag might
|
|
|
|
be called "bold" and make the text inside the tag bold. However, the tag
|
|
|
|
concept is more general than that; tags don't have to affect appearance. They
|
|
|
|
can instead affect the behavior of mouse and key presses, "lock" a range of
|
|
|
|
text so the user can't edit it, or countless other things. A tag is
|
2007-06-10 06:52:51 +00:00
|
|
|
represented by a #GtkTextTag object. One #GtkTextTag can be applied to any
|
|
|
|
number of text ranges in any number of buffers.
|
2000-10-24 00:15:14 +00:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2005-05-16 19:34:39 +00:00
|
|
|
Each tag is stored in a #GtkTextTagTable. A tag table defines a set of
|
2000-10-24 00:15:14 +00:00
|
|
|
tags that can be used together. Each buffer has one tag table associated with
|
|
|
|
it; only tags from that tag table can be used with the buffer. A single tag
|
|
|
|
table can be shared between multiple buffers, however.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
Tags can have names, which is convenient sometimes (for example, you can name
|
|
|
|
your tag that makes things bold "bold"), but they can also be anonymous (which
|
|
|
|
is convenient if you're creating tags on-the-fly).
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
Most text manipulation is accomplished with <firstterm>iterators</firstterm>,
|
2005-05-16 19:34:39 +00:00
|
|
|
represented by a #GtkTextIter. An iterator represents a position between two
|
2007-06-10 06:52:51 +00:00
|
|
|
characters in the text buffer. #GtkTextIter is a struct designed to be
|
|
|
|
allocated on the stack; it's guaranteed to be copiable by value and never
|
|
|
|
contain any heap-allocated data. Iterators are not valid indefinitely;
|
|
|
|
whenever the buffer is modified in a way that affects the number of characters
|
|
|
|
in the buffer, all outstanding iterators become invalid. (Note that deleting
|
|
|
|
5 characters and then reinserting 5 still invalidates iterators, though you
|
|
|
|
end up with the same number of characters you pass through a state with a
|
|
|
|
different number).
|
2000-10-24 00:15:14 +00:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
Because of this, iterators can't be used to preserve positions across buffer
|
2007-06-10 06:52:51 +00:00
|
|
|
modifications. To preserve a position, the #GtkTextMark object is ideal. You
|
|
|
|
can think of a mark as an invisible cursor or insertion point; it floats in
|
|
|
|
the buffer, saving a position. If the text surrounding the mark is deleted,
|
|
|
|
the mark remains in the position the text once occupied; if text is inserted
|
|
|
|
at the mark, the mark ends up either to the left or to the right of the new
|
|
|
|
text, depending on its <firstterm>gravity</firstterm>. The standard text
|
|
|
|
cursor in left-to-right languages is a mark with right gravity, because it
|
|
|
|
stays to the right of inserted text.
|
2000-10-24 00:15:14 +00:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
Like tags, marks can be either named or anonymous. There are two marks built-in
|
2005-05-16 19:34:39 +00:00
|
|
|
to #GtkTextBuffer; these are named <literal>"insert"</literal> and
|
|
|
|
<literal>"selection_bound"</literal> and refer to the insertion point and the
|
2007-06-10 06:52:51 +00:00
|
|
|
boundary of the selection which is not the insertion point, respectively. If
|
|
|
|
no text is selected, these two marks will be in the same position. You can
|
|
|
|
manipulate what is selected and where the cursor appears by moving these
|
|
|
|
marks around.
|
2000-10-24 00:15:14 +00:00
|
|
|
<footnote>
|
|
|
|
<para>
|
|
|
|
If you want to place the cursor in response to a user action, be sure to use
|
2005-05-16 19:34:39 +00:00
|
|
|
gtk_text_buffer_place_cursor(), which moves both at once without causing a
|
|
|
|
temporary selection (moving one then the other temporarily selects the range in
|
|
|
|
between the old and new positions).
|
2000-10-24 00:15:14 +00:00
|
|
|
</para>
|
|
|
|
</footnote>
|
|
|
|
</para>
|
|
|
|
|
2001-09-24 23:12:39 +00:00
|
|
|
<para>
|
2002-01-01 17:44:46 +00:00
|
|
|
Text buffers always contain at least one line, but may be empty (that
|
|
|
|
is, buffers can contain zero characters). The last line in the text
|
|
|
|
buffer never ends in a line separator (such as newline); the other
|
|
|
|
lines in the buffer always end in a line separator. Line separators
|
|
|
|
count as characters when computing character counts and character
|
|
|
|
offsets. Note that some Unicode line separators are represented with
|
|
|
|
multiple bytes in UTF-8, and the two-character sequence "\r\n" is also
|
|
|
|
considered a line separator.
|
2001-09-24 23:12:39 +00:00
|
|
|
</para>
|
|
|
|
|
2000-10-24 00:15:14 +00:00
|
|
|
</refsect1>
|
|
|
|
|
|
|
|
|
|
|
|
<refsect1>
|
|
|
|
<title>Simple Example</title>
|
|
|
|
|
|
|
|
<para>
|
2005-05-16 19:34:39 +00:00
|
|
|
The simplest usage of #GtkTextView might look like this:
|
Small additions.
* gtk/tmpl/gtktextview.sgml: Small additions.
* gtk/tmpl/gtksignal.sgml: Explain what to use instead.
* gtk/question_index.sgml, gtk/text_widget.sgml, gtk/tree_widget.sgml,
gtk/changes-1.2.sgml, gtk/changes-2.0.sgml,
gtk/framebuffer.sgml: SGML fixes and additions.
* gtk/tmpl/gtksignal.sgml, gtk/tmpl/gtkdialog.sgml,
gtk/tmpl/gtkeditable.sgml, gtk/tmpl/gtkfilesel.sgml,
gtk/tmpl/gtkmain.sgml, gtk/tmpl/gtkmenu.sgml,
gtk/tmpl/gtkmessagedialog.sgml, gtk/tmpl/gtkrc.sgml,
gtk/tmpl/gtktogglebutton.sgml, gtk/tmpl/gtkspinbutton.sgml,
gtk/tmpl/gtkpaned.sgml, gtk/tmpl/gtkwindow.sgml:
Replace references to deprecated functions.
2002-01-03 23:04:44 +00:00
|
|
|
<informalexample><programlisting>
|
2000-11-05 16:09:04 +00:00
|
|
|
GtkWidget *view;
|
|
|
|
GtkTextBuffer *buffer;
|
2000-10-24 00:15:14 +00:00
|
|
|
|
2005-05-16 19:34:39 +00:00
|
|
|
view = gtk_text_view_new (<!-- -->);
|
2000-10-24 00:15:14 +00:00
|
|
|
|
2000-11-05 16:09:04 +00:00
|
|
|
buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
|
2000-10-24 00:15:14 +00:00
|
|
|
|
2001-12-12 06:40:08 +00:00
|
|
|
gtk_text_buffer_set_text (buffer, "Hello, this is some text", -1);
|
2000-10-24 00:15:14 +00:00
|
|
|
|
|
|
|
/* Now you might put the view in a container and display it on the
|
|
|
|
* screen; when the user edits the text, signals on the buffer
|
|
|
|
* will be emitted, such as "changed", "insert_text", and so on.
|
|
|
|
*/
|
Small additions.
* gtk/tmpl/gtktextview.sgml: Small additions.
* gtk/tmpl/gtksignal.sgml: Explain what to use instead.
* gtk/question_index.sgml, gtk/text_widget.sgml, gtk/tree_widget.sgml,
gtk/changes-1.2.sgml, gtk/changes-2.0.sgml,
gtk/framebuffer.sgml: SGML fixes and additions.
* gtk/tmpl/gtksignal.sgml, gtk/tmpl/gtkdialog.sgml,
gtk/tmpl/gtkeditable.sgml, gtk/tmpl/gtkfilesel.sgml,
gtk/tmpl/gtkmain.sgml, gtk/tmpl/gtkmenu.sgml,
gtk/tmpl/gtkmessagedialog.sgml, gtk/tmpl/gtkrc.sgml,
gtk/tmpl/gtktogglebutton.sgml, gtk/tmpl/gtkspinbutton.sgml,
gtk/tmpl/gtkpaned.sgml, gtk/tmpl/gtkwindow.sgml:
Replace references to deprecated functions.
2002-01-03 23:04:44 +00:00
|
|
|
</programlisting></informalexample>
|
2005-05-16 19:34:39 +00:00
|
|
|
In many cases it's also convenient to first create the buffer with
|
|
|
|
gtk_text_buffer_new(), then create a widget for that buffer with
|
|
|
|
gtk_text_view_new_with_buffer(). Or you can change the buffer the widget
|
|
|
|
displays after the widget is created with gtk_text_view_set_buffer().
|
2000-10-24 00:15:14 +00:00
|
|
|
</para>
|
|
|
|
|
|
|
|
</refsect1>
|
|
|
|
|
2001-12-12 06:40:08 +00:00
|
|
|
<refsect1>
|
|
|
|
<title>Example of Changing Text Attributes</title>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
|
2005-05-16 19:34:39 +00:00
|
|
|
There are two ways to affect text attributes in #GtkTextView.
|
|
|
|
You can change the default attributes for a given #GtkTextView, and you can
|
|
|
|
apply tags that change the attributes for a region of text. For text features
|
|
|
|
that come from the theme — such as font and foreground color — use
|
2011-04-26 15:14:52 +00:00
|
|
|
standard #GtkWidget functions such as gtk_widget_modify_font() or
|
|
|
|
gtk_widget_override_text(). For other attributes there are dedicated methods on
|
2005-05-16 19:34:39 +00:00
|
|
|
#GtkTextView such as gtk_text_view_set_tabs().
|
2001-12-12 06:40:08 +00:00
|
|
|
|
Small additions.
* gtk/tmpl/gtktextview.sgml: Small additions.
* gtk/tmpl/gtksignal.sgml: Explain what to use instead.
* gtk/question_index.sgml, gtk/text_widget.sgml, gtk/tree_widget.sgml,
gtk/changes-1.2.sgml, gtk/changes-2.0.sgml,
gtk/framebuffer.sgml: SGML fixes and additions.
* gtk/tmpl/gtksignal.sgml, gtk/tmpl/gtkdialog.sgml,
gtk/tmpl/gtkeditable.sgml, gtk/tmpl/gtkfilesel.sgml,
gtk/tmpl/gtkmain.sgml, gtk/tmpl/gtkmenu.sgml,
gtk/tmpl/gtkmessagedialog.sgml, gtk/tmpl/gtkrc.sgml,
gtk/tmpl/gtktogglebutton.sgml, gtk/tmpl/gtkspinbutton.sgml,
gtk/tmpl/gtkpaned.sgml, gtk/tmpl/gtkwindow.sgml:
Replace references to deprecated functions.
2002-01-03 23:04:44 +00:00
|
|
|
<informalexample><programlisting>
|
2001-12-12 06:40:08 +00:00
|
|
|
GtkWidget *view;
|
|
|
|
GtkTextBuffer *buffer;
|
2002-04-20 23:57:41 +00:00
|
|
|
GtkTextIter start, end;
|
2001-12-12 06:40:08 +00:00
|
|
|
PangoFontDescription *font_desc;
|
2011-04-26 15:14:52 +00:00
|
|
|
GdkRGBA rgba;
|
2001-12-12 06:40:08 +00:00
|
|
|
GtkTextTag *tag;
|
|
|
|
|
2005-05-16 19:34:39 +00:00
|
|
|
view = gtk_text_view_new (<!-- -->);
|
2001-12-12 06:40:08 +00:00
|
|
|
|
|
|
|
buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
|
|
|
|
|
|
|
|
gtk_text_buffer_set_text (buffer, "Hello, this is some text", -1);
|
|
|
|
|
|
|
|
/* Change default font throughout the widget */
|
|
|
|
font_desc = pango_font_description_from_string ("Serif 15");
|
|
|
|
gtk_widget_modify_font (view, font_desc);
|
|
|
|
pango_font_description_free (font_desc);
|
|
|
|
|
|
|
|
/* Change default color throughout the widget */
|
2011-04-26 15:14:52 +00:00
|
|
|
gdk_rgba_parse ("green", &rgba);
|
|
|
|
gtk_widget_override_color (view, GTK_STATE_FLAG_NORMAL, &rgba);
|
2001-12-12 06:40:08 +00:00
|
|
|
|
|
|
|
/* Change left margin throughout the widget */
|
|
|
|
gtk_text_view_set_left_margin (GTK_TEXT_VIEW (view), 30);
|
|
|
|
|
|
|
|
/* Use a tag to change the color for just one part of the widget */
|
|
|
|
tag = gtk_text_buffer_create_tag (buffer, "blue_foreground",
|
|
|
|
"foreground", "blue", NULL);
|
2001-12-23 22:55:17 +00:00
|
|
|
gtk_text_buffer_get_iter_at_offset (buffer, &start, 7);
|
|
|
|
gtk_text_buffer_get_iter_at_offset (buffer, &end, 12);
|
|
|
|
gtk_text_buffer_apply_tag (buffer, tag, &start, &end);
|
Small additions.
* gtk/tmpl/gtktextview.sgml: Small additions.
* gtk/tmpl/gtksignal.sgml: Explain what to use instead.
* gtk/question_index.sgml, gtk/text_widget.sgml, gtk/tree_widget.sgml,
gtk/changes-1.2.sgml, gtk/changes-2.0.sgml,
gtk/framebuffer.sgml: SGML fixes and additions.
* gtk/tmpl/gtksignal.sgml, gtk/tmpl/gtkdialog.sgml,
gtk/tmpl/gtkeditable.sgml, gtk/tmpl/gtkfilesel.sgml,
gtk/tmpl/gtkmain.sgml, gtk/tmpl/gtkmenu.sgml,
gtk/tmpl/gtkmessagedialog.sgml, gtk/tmpl/gtkrc.sgml,
gtk/tmpl/gtktogglebutton.sgml, gtk/tmpl/gtkspinbutton.sgml,
gtk/tmpl/gtkpaned.sgml, gtk/tmpl/gtkwindow.sgml:
Replace references to deprecated functions.
2002-01-03 23:04:44 +00:00
|
|
|
</programlisting></informalexample>
|
2001-12-12 06:40:08 +00:00
|
|
|
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
The <application>gtk-demo</application> application that comes with
|
2005-05-16 19:34:39 +00:00
|
|
|
GTK+ contains more example code for #GtkTextView.
|
2001-12-12 06:40:08 +00:00
|
|
|
</para>
|
|
|
|
|
|
|
|
</refsect1>
|
|
|
|
|
2000-10-24 00:15:14 +00:00
|
|
|
</refentry>
|