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

📄 ch21_11.htm

📁 by Randal L. Schwartz and Tom Phoenix ISBN 0-596-00132-0 Third Edition, published July 2001. (See
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<html><head><title>The Text Widget (Perl in a Nutshell, 2nd Edition)</title><link rel="stylesheet" type="text/css" href="../style/style1.css" /><meta name="DC.Creator" content="Stephen Spainhour" /><meta name="DC.Format" content="text/xml" scheme="MIME" /><meta name="DC.Language" content="en-US" /><meta name="DC.Publisher" content="O'Reilly &amp; Associates, Inc." /><meta name="DC.Source" scheme="ISBN" content="0596002416L" /><meta name="DC.Subject.Keyword" content="stuff" /><meta name="DC.Title" content="Perl in a Nutshell, 2nd Edition" /><meta name="DC.Type" content="Text.Monograph" /></head><body bgcolor="#ffffff"><img src="gifs/smbanner.gif" usemap="#banner-map" border="0" alt="Book Home" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Java and XSLT" /><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch21_10.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td align="center" valign="top" width="228" /><td align="right" valign="top" width="228"><a href="ch21_12.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr></table></div><h2 class="sect1">21.11. The Text Widget</h2><p><a name="INDEX-2946" /><a name="INDEX-2947" /><a name="INDEX-2948" />Create a Text widget with the<tt class="literal">Text</tt> method:</p><blockquote><pre class="code">$parentwidget-&gt;Text ( <em class="replaceable"><tt>options</tt></em>)</pre></blockquote><p>The standard configuration options that apply to<tt class="literal">Text</tt> are: <tt class="literal">-background</tt>,<tt class="literal">-bg</tt>,<tt class="literal">-bor</tt>-<tt class="literal">derwidth</tt>,<tt class="literal">-bd</tt>, <tt class="literal">-cursor</tt>,<tt class="literal">-exportselection</tt>, <tt class="literal">-font</tt>,<tt class="literal">-foreground</tt>, <tt class="literal">-fg</tt>,<tt class="literal">-height</tt>,<tt class="literal">-high</tt>-<tt class="literal">lightbackground</tt>,<tt class="literal">-highlightcolor</tt>,<tt class="literal">-highlightthickness</tt>,<tt class="literal">-insertbackground</tt>,<tt class="literal">-in</tt>-<tt class="literal">sertborderwidth</tt>,<tt class="literal">-insertofftime</tt>, <tt class="literal">-insertontime</tt>,<tt class="literal">-insertwidth</tt>, <tt class="literal">-padx</tt>,<tt class="literal">-pady</tt>,<tt class="literal">-re</tt>-<tt class="literal">lief</tt>,<tt class="literal">-selectbackground</tt>,<tt class="literal">-selectborderwidth</tt>,<tt class="literal">-selectforeground</tt>, <tt class="literal">-state</tt>,<tt class="literal">-takefo</tt>-<tt class="literal">cus</tt>,<tt class="literal">-width</tt>, <tt class="literal">-xscrollcommand</tt>, and<tt class="literal">-yscrollcommand</tt>.</p><p>Other options are:</p><dl><dt><b><tt class="literal">-setgrid =&gt;</tt> <em class="replaceable">boolean</em></b></dt><dd><a name="INDEX-2949" />Turnson gridding for the Text widget. Default is <tt class="literal">0</tt>(off).</p></dd><dt><b><tt class="literal">-spacing1 =&gt;</tt> <em class="replaceable">amount</em></b></dt><dd><a name="INDEX-2950" />Defines the amount of space left on thetop of a line of text that starts on its own line. Default is<tt class="literal">0</tt>. </p></dd><dt><b><tt class="literal">-spacing2 =&gt;</tt> <em class="replaceable">amount</em></b></dt><dd>Defines the amount of space left on the top of a line of text afterit has been automatically wrapped by the Text widget. Default is<tt class="literal">0</tt>. </p></dd><dt><b><tt class="literal">-spacing3 =&gt;</tt> <em class="replaceable">amount</em></b></dt><dd>Defines the amount of space left after a line of text that has beenended by <tt class="literal">\n</tt>. Default is <tt class="literal">0</tt>.</p></dd><dt><b><tt class="literal">-tabs =&gt;</tt> <em class="replaceable">list</em></b></dt><dd><a name="INDEX-2951" />Specifies a list of tab stops to use inthe text widget. Default is undefined (no tab stops).</p></dd><dt><b><tt class="literal">-wrap =&gt;</tt> <em class="replaceable">mode</em></b></dt><dd><a name="INDEX-2952" />Sets the mode for determining automaticline wrapping. Values are <tt class="literal">none</tt> (no wrapping),<tt class="literal">char</tt> (wrap at any character), or<tt class="literal">word</tt> (wrap at a word boundary). Default is<tt class="literal">char</tt>.</p></dd></dl><a name="perlnut2-CHP-21-SECT-11.1" /><div class="sect2"><h3 class="sect2">21.11.1. Text Indexes and Modifiers</h3><p>In a Text widget, several indexes are defined to identify positionsin the text. These are used by the methods that retrieve andmanipulate text.</p><dl><dt><i><em class="replaceable"><tt>n</em>.<em class="replaceable">m</tt></em></i></dt><dd><a name="INDEX-2953" />Numbers representing character<em class="replaceable"><tt>m</em> on line <em class="replaceable">n</tt></em></p></dd><dt><b><tt class="literal">@</tt><em class="replaceable">x,y</em></b></dt><dd>The character closest to the <em class="replaceable"><tt>x,y</tt></em> coordinate</p></dd><dt><b><tt class="literal">end</tt></b></dt><dd><a name="INDEX-2954" />The end of the text</p></dd><dt><b><tt class="literal">insert</tt></b></dt><dd><a name="INDEX-2955" />The character after the insert cursor</p></dd><dt><b><tt class="literal">current</tt></b></dt><dd><a name="INDEX-2956" />The position closest to the mouse cursor</p></dd><dt><i><em class="replaceable"><tt>mark</tt></em></i></dt><dd>Other marks defined for the widget (see discussion of text markslater in this section)</p></dd><dt><b><tt class="literal">sel.first</tt></b></dt><dd><a name="INDEX-2957" />Thefirst selected character</p></dd><dt><b><tt class="literal">sel.last</tt></b></dt><dd>The character just after the last selected character</p></dd><dt><em class="replaceable"><tt>tag</tt></em>.<tt class="literal">first</tt></dt><dd>The first character in the widget of the specified tag type</p></dd><dt><em class="replaceable"><tt>tag</tt></em>.<tt class="literal">last</tt></dt><dd>The character just after the last character of the specified tag type</p></dd><dt><i><em class="replaceable"><tt>widget</tt></em></i></dt><dd>The location of an embedded widget</p></dd></dl><p>There are also several modifiers to use with text indexes. They are:</p><dl><dt><b><tt class="literal">-</tt> <em class="replaceable">n lines</em></b></dt><dt><b><tt class="literal">+</tt> <em class="replaceable">n lines</em></b></dt><dd><em class="replaceable"><tt>n</tt></em> lines before or after the index</p></dd><dt><b><tt class="literal">-</tt> <em class="replaceable">n chars</em></b></dt><dt><b><tt class="literal">+</tt> <em class="replaceable">n chars</em></b></dt><dd><em class="replaceable"><tt>n</tt></em> characters before or after the index</p></dd><dt><b><tt class="literal">linestart</tt></b></dt><dd><a name="INDEX-2958" />The first character on the line</p></dd><dt><b><tt class="literal">lineend</tt></b></dt><dd>The last character on the line (often a newline)</p></dd><dt><b><tt class="literal">wordstart</tt></b></dt><dd><a name="INDEX-2959" />The first character in the word</p></dd><dt><b><tt class="literal">wordend</tt></b></dt><dd>The character after the last character in the word</p></dd></dl></div><a name="perlnut2-CHP-21-SECT-11.2" /><div class="sect2"><h3 class="sect2">21.11.2. Text Methods</h3><p>In addition to <tt class="literal">configure</tt> and<tt class="literal">cget</tt>, the following methods are defined for theText widget:</p><dl><dt><b><tt class="literal">bbox</tt></b></dt><dd><a name="INDEX-2960" />Returns the location and dimensions ofthe bounding box surrounding the character at the specified index.The returned list contains four numbers representing (respectively)the x coordinate of the upper-left corner, the y coordinate of theupper-left corner, the width of the text in pixels, and the height ofthe text in pixels.</p></dd><dt><b><tt class="literal">compare</tt></b></dt><dd><a name="INDEX-2961" />Performs a comparison on two indexes.For example:</p><blockquote><pre class="code">if ($text-&gt;compare('insert', '==', 'end') {         # we're at the end of the text}</pre></blockquote><p>The valid operators are <tt class="literal">&lt;</tt>,<tt class="literal">&lt;=</tt>, <tt class="literal">==</tt>,<tt class="literal">&gt;=</tt>, and <tt class="literal">!=</tt>.</p></dd><dt><b><tt class="literal">debug</tt></b></dt><dd><a name="INDEX-2962" />Given a Boolean, turns debugging on oroff.</p></dd><dt><b><tt class="literal">delete</tt></b></dt><dd><a name="INDEX-2963" />Deletes text from the text widget. Todelete everything:</p><blockquote><pre class="code">$text-&gt;delete(0, 'end');</pre></blockquote></dd><dt><b><tt class="literal">dlineinfo</tt></b></dt><dd><a name="INDEX-2964" />Returns the location and dimensions ofthe bounding box surrounding the line at the specified index. Thereturned list contains five numbers representing (respectively) the xcoordinate of the upper-left corner, the y coordinate of theupper-left corner, the width of the text in pixels, the height of thetext in pixels, and the baseline position of the line.</p></dd><dt><b><tt class="literal">get</tt></b></dt><dd><a name="INDEX-2965" />Returns the text located in the givenindex range.</p></dd><dt><b><tt class="literal">index</tt></b></dt><dd><a name="INDEX-2966" />Given a named index, returns itsnumeric equivalent in the format<em class="replaceable"><tt>line.char</tt></em>.</p></dd><dt><b><tt class="literal">insert</tt></b></dt><dd><a name="INDEX-2967" />Inserts text into the widget at thespecified location. The second argument is the text to insert, andthe third argument is either a single tag or a list referencecontaining the names of multiple tags to apply to the text.Subsequent arguments alternate between text and tags. For example:</p><blockquote><pre class="code">$text-&gt;insert('end', 'You want to do ', 'normal',                      'what?!', ['bold','red']);</pre></blockquote></dd><dt><b><tt class="literal">search</tt></b></dt><dd><a name="INDEX-2968" />Returns the index containing aparticular string in the Text widget. For example, to searchbackwards for a case-insensitive hostname starting from the end ofthe text:</p><blockquote><pre class="code">$hostindex = $text-&gt;search(-nocase, -backwards, $hostname, 'end');</pre></blockquote><p>The <tt class="literal">search</tt> method takes several switches to modifythe search, each starting with -. The first argument that does notstart with - is taken to be the search string. The switches are:</p><dl><dt><i> <tt class="literal">-forwards</tt> </i></dt><dd>Searches forwards starting at the specified index.</p></dd><dt><i> <tt class="literal">-backwards</tt></i></dt><dd>Searches backwards starting at the specified index.</p></dd><dt><i> <tt class="literal">-exact</tt></i></dt><dd>Matches the string exactly (default).</p></dd><dt><i> <tt class="literal">-regexp</tt></i></dt><dd>Treat the pattern as a regular expression.</p></dd><dt><i> <tt class="literal">-nocase</tt></i></dt><dd>Ignores case.</p></dd><dt><i> <tt class="literal">-count =&gt;</tt> <em class="replaceable"><tt>$variable</tt></em> </i></dt><dd>Stores the number of matches into the specified variable.</p></dd><dt><b><tt class="literal">--</tt></b></dt><dd>Interprets the next argument as the pattern. (Useful when the patternstarts with a -.)</p></dd></dl></dd><dt><b><tt class="literal">see</tt></b></dt><dd><a name="INDEX-2969" />Scrolls the text so that the portion ofthe text containing the specified index is visible.</p></dd><dt><b><tt class="literal">window</tt></b></dt><dd><a name="INDEX-2970" />Embeds widgets within the Text widget.The first argument can be any of: <tt class="literal">create</tt>,<tt class="literal">names</tt>, <tt class="literal">cget</tt>, and<tt class="literal">configure</tt>.</p></dd><dt><b><tt class="literal">create</tt> </b></dt><dd>Inserts an embedded widget at a specified index. Each widget occupiesone character in the Text widget. The widget must have already beencreated as a child of the Text widget. For example:</p><blockquote><pre class="code">$button = $text-&gt;Label(-text =&gt; "How ya doing?");$text-&gt;window('create','end', -window =&gt; $button);</pre></blockquote><p>Here, the <tt class="literal">-window</tt> option is used to identify thewidget to embed. The list of options to <tt class="literal">window('create')</tt> is:</p><dl><dt><i> <tt class="literal">-align</tt></i></dt><dd>Determines the positioning within the line of text. Values are<tt class="literal">baseline</tt>, <tt class="literal">bottom</tt>,<tt class="literal">top</tt>, or <tt class="literal">center</tt> (default).</p></dd><dt><i> <tt class="literal">-padx</tt></i></dt><dd>Adds padding in the <tt class="literal">x</tt> direction.</p></dd><dt><i> <tt class="literal">-pady</tt></i></dt><dd>Adds padding in the <tt class="literal">y</tt> direction.</p></dd><dt><i> <tt class="literal">-window</tt></i></dt><dd>Identifies the widget to embed.</p></dd><dt><i> <tt class="literal">names</tt></i></dt><dd>Returns a list of widget types embedded into the Text widget.</p></dd>

⌨️ 快捷键说明

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