⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sec-textmanipulation.html

📁 gtk 开发手册和参考文档。 包括gtk glib gdk等
💻 HTML
字号:
<HTML><HEAD><TITLE>Text Manipulation</TITLE><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.49"><LINKREL="HOME"TITLE="GTK+ 1.2 Tutorial"HREF="gtk-tut.html"><LINKREL="UP"TITLE="Text Widget"HREF="ch-textwidget.html"><LINKREL="PREVIOUS"TITLE="Text Widget"HREF="ch-textwidget.html"><LINKREL="NEXT"TITLE="Keyboard Shortcuts"HREF="sec-keyboardshortcuts.html"></HEAD></head><body bgcolor="#FFFFFF" marginheight=0 marginwidth=0 width="100%"><table cellspacing=6 border=0 cellpadding=0 width="100%">  <tr>    <td bgcolor="#FFFFFF" valign=top nowrap>    <centeR><A HREF="/"><img src="/images/gtk-logo-rgb.gif" width=107 height=140 border=0></A>      <font face="helvetica,lucidia" color="#000000"><BR><BR><b>GTK+<BR>The GIMP Toolkit</b></center><BR><table width=100% cellspacing=0 cellpadding=2 border=0><tr><td bgcolor="#000000"><table width=100% cellspacing=0 cellpadding=4 border=0><tr><td bgcolor="#AAAAEE" nowrap><B>General</B></td></tr><tr><td bgcolor="#FFFFFF" nowrap><A HREF="/">Introduction</A><BR><A HREF="/screenshots/">Screenshots</A><br><A HREF="/download/">Download</A><br><A HREF="/mailinglists.html">Mailing Lists</A><BR><A HREF="/bindings.html">Language Bindings</A><BR><A HREF="http://gtk.themes.org/">Themes</A><BR><A HREF="/bugs.html">Bug Tracker</A><BR></td></tr></table></td></tr></table><BR>  <table width=100% cellspacing=0 cellpadding=2 border=0><tr><td bgcolor="#000000"><table width=100% cellspacing=0 cellpadding=4 border=0><tr><td bgcolor="#AAAAEE" nowrap><B>Documentation</B></td></tr><tr><td bgcolor="#FFFFFF" nowrap><A HREF="/faq/">FAQ</A><br><A HREF="/tutorial/">Tutorial</A><BR><A HREF="/api/">API Reference</A><br><A HREF="/books.html">Published Books</A><BR></td></tr></table></td></tr></table><BR>  <table width=100% cellspacing=0 cellpadding=2 border=0><tr><td bgcolor="#000000"><table width=100% cellspacing=0 cellpadding=4 border=0><tr><td bgcolor="#AAAAEE" nowrap><B>Projects</B></td></tr><tr><td bgcolor="#FFFFFF" nowrap><A HREF="http://www.pango.org/">Pango</A><BR><A HREF="http://sources.redhat.com/inti/">Inti</A><BR><A HREF="http://www.gnome.org/">GNOME</A><BR><A HREF="http://user.sgic.fi/~tml/gimp/win32/">GTK+ for Win32</A><br><A HREF="http://people.redhat.com/sopwith/gtkfb/">GtkFB (Framebuffer)</A><br><A HREF="http://www.directfb.org/gtk.xml">GTK+ on DirectFB</A><BR><A HREF="/beos/">GTK+ for BeOS</A></td></tr></table></td></tr></table><BR>  <table width=100% cellspacing=0 cellpadding=2 border=0><tr><td bgcolor="#000000"><table width=100% cellspacing=0 cellpadding=4 border=0><tr><td bgcolor="#AAAAEE" nowrap><B><B>Applications</B></B></td></tr><tr><td bgcolor="#FFFFFF" nowrap><A HREF="http://www.gimp.org/">GIMP</A><BR><A HREF="http://www.abiword.org/">Abiword</A><BR><A HREF="http://www.lysator.liu.se/~alla/dia/dia.html">Dia</A><BR><A HREF="http://glade.pn.org/">Glade</A><BR><A HREF="http://www.gnucash.org/">GnuCash</A><BR><A HREF="http://www.gnome.org/projects/gnumeric/">Gnumeric</A><BR><BR><A HREF="http://www.gnome.org/applist/">GNOME Software Map</A><br></td></tr></table></td></tr></table><BR>      </td>  <td bgcolor="#ffffff" valign=top width="99%"><font face="lucida,helvetica"><BODYCLASS="SECT1"BGCOLOR="#FFFFFF"TEXT="#000000"LINK="#0000FF"VLINK="#840084"ALINK="#0000FF"><DIVCLASS="NAVHEADER"><TABLEWIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><THCOLSPAN="3"ALIGN="center">GTK+ 1.2 Tutorial</TH></TR><TR><TDWIDTH="10%"ALIGN="left"VALIGN="bottom"><AHREF="ch-textwidget.html">&#60;&#60;&#60; Previous</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom">Chapter 15. Text Widget</TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="sec-keyboardshortcuts.html">Next &#62;&#62;&#62;</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="SECT1"><H1CLASS="SECT1"><ANAME="SEC-TEXTMANIPULATION">15.2. Text Manipulation</A></H1><P>The current insertion point of a Text widget can be set using</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">void gtk_text_set_point( GtkText *text,                         guint    index );</PRE></TD></TR></TABLE><P>where <TTCLASS="LITERAL">index</TT> is the position to set the insertion point.</P><P>Analogous to this is the function for getting the current insertionpoint:</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">guint gtk_text_get_point( GtkText *text );</PRE></TD></TR></TABLE><P>A function that is useful in combination with the above two functionsis</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">guint gtk_text_get_length( GtkText *text );</PRE></TD></TR></TABLE><P>which returns the current length of the Text widget. The length is thenumber of characters that are within the text block of the widget,including characters such as newline, which marks the end oflines.</P><P>In order to insert text at the current insertion point of a Textwidget, the function gtk_text_insert is used, which also allows us tospecify background and foreground colors and a font for the text.</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">void gtk_text_insert( GtkText    *text,                      GdkFont    *font,                      GdkColor   *fore,                      GdkColor   *back,                      const char *chars,                      gint        length );</PRE></TD></TR></TABLE><P>Passing a value of <TTCLASS="LITERAL">NULL</TT> in as the value for the foreground color,background color or font will result in the values set within thewidget style to be used. Using a value of <TTCLASS="LITERAL">-1</TT> for the lengthparameter will result in the whole of the text string given beinginserted.</P><P>The text widget is one of the few within GTK that redraws itselfdynamically, outside of the gtk_main function. This means that allchanges to the contents of the text widget take effectimmediately. This may be undesirable when performing multiple changesto the text widget. In order to allow us to perform multiple updatesto the text widget without it continuously redrawing, we can freezethe widget, which temporarily stops it from automatically redrawingitself every time it is changed. We can then thaw the widget after ourupdates are complete.</P><P>The following two functions perform this freeze and thaw action:</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">void gtk_text_freeze( GtkText *text );void gtk_text_thaw( GtkText *text );         </PRE></TD></TR></TABLE><P>Text is deleted from the text widget relative to the current insertionpoint by the following two functions. The return value is a TRUE orFALSE indicator of whether the operation was successful.</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">gint gtk_text_backward_delete( GtkText *text,                               guint    nchars );gint gtk_text_forward_delete ( GtkText *text,                               guint    nchars );</PRE></TD></TR></TABLE><P>If you want to retrieve the contents of the text widget, then themacro <TTCLASS="LITERAL">GTK_TEXT_INDEX(t, index)</TT> allows you to retrieve thecharacter at position <TTCLASS="LITERAL">index</TT> within the text widget <TTCLASS="LITERAL">t</TT>.</P><P>To retrieve larger blocks of text, we can use the function</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">gchar *gtk_editable_get_chars( GtkEditable *editable,                               gint         start_pos,                               gint         end_pos );   </PRE></TD></TR></TABLE><P>This is a function of the parent class of the text widget. A value of-1 as <TTCLASS="LITERAL">end_pos</TT> signifies the end of the text. The index of thetext starts at 0.</P><P>The function allocates a new chunk of memory for the text block, sodon't forget to free it with a call to g_free when you have finishedwith it.</P></DIV><DIVCLASS="NAVFOOTER"><HRALIGN="LEFT"WIDTH="100%"><TABLEWIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><AHREF="ch-textwidget.html">&#60;&#60;&#60; Previous</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="gtk-tut.html">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><AHREF="sec-keyboardshortcuts.html">Next &#62;&#62;&#62;</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Text Widget</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="ch-textwidget.html">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">Keyboard Shortcuts</TD></TR></TABLE></DIV>        </td>    </tr></table>  </td>  </tr></table></body></BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -