text_widget.sgml
来自「linux下电话本所依赖的一些图形库」· SGML 代码 · 共 218 行
SGML
218 行
<refentry id="TextWidget" revision="18 Oct 2000"><refmeta><refentrytitle>Text Widget Overview</refentrytitle><manvolnum>3</manvolnum><refmiscinfo>GTK Library</refmiscinfo></refmeta><refnamediv><refname>Text Widget Overview</refname><refpurpose>Overview of <link linkend="GtkTextBuffer">GtkTextBuffer</link>, <link linkend="GtkTextView">GtkTextView</link>, and friends</refpurpose></refnamediv><refsect1><title>Conceptual Overview</title><para>GTK+ has an extremely powerful framework for multiline text editing. Theprimary objects involved in the process are <linklinkend="GtkTextBuffer">GtkTextBuffer</link>, which represents the text beingedited, and <link linkend="GtkTextView">GtkTextView</link>, a widget which candisplay a <link linkend="GtkTextBuffer">GtkTextBuffer</link>. Each buffer canbe displayed by any number of views.</para><para>One of the important things to remember about text in GTK+ is that it's in theUTF-8 encoding. This means that one character can be encoded as multiplebytes. 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 finewith ASCII, but as soon as your buffer contains multibyte characters, bad things will happen.</para><para>Text in a buffer can be marked with <firstterm>tags</firstterm>. A tag is anattribute 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 tagconcept 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 represented by a<link linkend="GtkTextTag">GtkTextTag</link> object. One <linklinkend="GtkTextTag">GtkTextTag</link> can be applied to any number of textranges in any number of buffers.</para><para>Each tag is stored in a <linklinkend="GtkTextTagTable">GtkTextTagTable</link>. A tag table defines a set oftags that can be used together. Each buffer has one tag table associated withit; only tags from that tag table can be used with the buffer. A single tagtable can be shared between multiple buffers, however.</para><para>Tags can have names, which is convenient sometimes (for example, you can nameyour tag that makes things bold "bold"), but they can also be anonymous (whichis convenient if you're creating tags on-the-fly).</para><para>Most text manipulation is accomplished with <firstterm>iterators</firstterm>,represented by a <link linkend="GtkTextIter">GtkTextIter</link>. An iteratorrepresents a position between two characters in the text buffer. <linklinkend="GtkTextIter">GtkTextIter</link> is a struct designed to be allocated on the stack; it's guaranteed to be copiable by value and never contain anyheap-allocated data. Iterators are not valid indefinitely; whenever the bufferis modified in a way that affects the number of characters in the buffer, alloutstanding iterators become invalid. (Note that deleting 5 characters and thenreinserting 5 still invalidates iterators, though you end up with the samenumber of characters you pass through a state with a different number).</para><para>Because of this, iterators can't be used to preserve positions across buffermodifications. To preserve a position, the <linklinkend="GtkTextMark">GtkTextMark</link> object is ideal. You can think of amark as an invisible cursor or insertion point; it floats in the buffer, savinga 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-rightlanguages is a mark with right gravity, because it stays to the right ofinserted text.</para><para>Like tags, marks can be either named or anonymous. There are two marks built-into <link linkend="GtkTextBuffer">GtkTextBuffer</link>; these are named<literal>"insert"</literal> and <literal>"selection_bound"</literal> and referto the insertion point and the boundary of the selection which is not theinsertion point, respectively. If no text is selected, these two marks will bein the same position. You can manipulate what is selected and where the cursorappears by moving these marks around.<footnote><para>If you want to place the cursor in response to a user action, be sure to use<link linkend="gtk-text-buffer-place-cursor">gtk_text_buffer_place_cursor()</link>, 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).</para></footnote></para><para>Text buffers always contain at least one line, but may be empty (thatis, buffers can contain zero characters). The last line in the textbuffer never ends in a line separator (such as newline); the otherlines in the buffer always end in a line separator. Line separatorscount as characters when computing character counts and characteroffsets. Note that some Unicode line separators are represented with multiple bytes in UTF-8, and the two-character sequence "\r\n" is alsoconsidered a line separator.</para></refsect1><refsect1><title>Simple Example</title><para>The simplest usage of <link linkend="GtkTextView">GtkTextView</link> might look like this:<informalexample><programlisting> GtkWidget *view; GtkTextBuffer *buffer; view = gtk_text_view_new (); buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view)); gtk_text_buffer_set_text (buffer, "Hello, this is some text", -1); /* 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. */</programlisting></informalexample>In many cases it's also convenient to first create the buffer with <link linkend="gtk-text-buffer-new">gtk_text_buffer_new()</link>, then create a widget for that buffer with <link linkend="gtk-text-view-new-with-buffer">gtk_text_view_new_with_buffer()</link>. Or you can change the buffer the widget displays after the widget is createdwith <link linkend="gtk-text-view-set-buffer">gtk_text_view_set_buffer()</link>.</para></refsect1><refsect1><title>Example of Changing Text Attributes</title><para>There are two ways to affect text attributes in <link linkend="GtkTextView">GtkTextView</link>.You can change the default attributes for a given <link linkend="GtkTextView">GtkTextView</link>, 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 standard <link linkend="GtkWidget">GtkWidget</link>functions such as <link linkend="gtk-widget-modify-font">gtk_widget_modify_font()</link>or <link linkend="gtk-widget-modify-text">gtk_widget_modify_text()</link>.For other attributes there are dedicated methods on <link linkend="GtkTextView">GtkTextView</link> such as <link linkend="gtk-text-view-set-tabs">gtk_text_view_set_tabs()</link>.<informalexample><programlisting> GtkWidget *view; GtkTextBuffer *buffer; GtkTextIter start, end; PangoFontDescription *font_desc; GdkColor color; GtkTextTag *tag; view = gtk_text_view_new (); 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 */ gdk_color_parse ("green", &color); gtk_widget_modify_text (view, GTK_STATE_NORMAL, &color); /* 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); 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);</programlisting></informalexample></para><para>The <application>gtk-demo</application> application that comes withGTK+ contains more example code for <linklinkend="GtkTextView">GtkTextView</link>.</para></refsect1></refentry>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?