configspecs.html

来自「perl教程」· HTML 代码 · 共 338 行 · 第 1/2 页

HTML
338 行
字号
</dd>
<dd>
<pre>
     <span class="variable">Tk::Config</span><span class="operator">-&gt;</span><span class="variable">new</span><span class="operator">(</span><span class="variable">setmethod</span><span class="operator">,</span><span class="variable">getmethod</span><span class="operator">,</span><span class="variable">args</span><span class="operator">,...);</span>
</pre>
</dd>
<dd>
<p><strong>Tk::Config</strong> class is used to implement all the above keyword types.  The
class has <code>configure</code> and <code>cget</code> methods so allows higher level code to
<em>always</em> just call one of those methods on an <em>object</em> of some kind.</p>
</dd>
</li>
<dt><strong><a name="item_hash_reference"><strong>hash reference</strong></a></strong>

<dd>
<p>Defining:</p>
</dd>
<dd>
<pre>
        <span class="variable">$cw</span><span class="operator">-&gt;</span><span class="variable">ConfigSpecs</span><span class="operator">(</span>
                <span class="operator">...</span>
                <span class="string">-option</span> <span class="operator">=&gt;</span> <span class="operator">[</span> <span class="operator">{</span> <span class="string">-optionX</span><span class="operator">=&gt;</span><span class="variable">$w1</span><span class="operator">,</span> <span class="string">-optionY</span><span class="operator">=&gt;</span><span class="operator">[</span><span class="variable">$w2</span><span class="operator">,</span> <span class="variable">$w3</span><span class="operator">]</span> <span class="operator">}</span><span class="operator">,</span>
                                <span class="variable">dbname</span> <span class="variable">dbclass</span> <span class="variable">default</span> <span class="operator">]</span><span class="operator">,</span>
                <span class="operator">...</span>
                <span class="operator">);</span>
</pre>
</dd>
<dd>
<p>So <code>$cw-&gt;configure(-option =&gt; value)</code> actually does</p>
</dd>
<dd>
<pre>
        <span class="variable">$w1</span><span class="operator">-&gt;</span><span class="variable">configure</span><span class="operator">(</span><span class="string">-optionX</span> <span class="operator">=&gt;</span> <span class="variable">value</span><span class="operator">);</span>
        <span class="variable">$w2</span><span class="operator">-&gt;</span><span class="variable">configure</span><span class="operator">(</span><span class="string">-optionY</span> <span class="operator">=&gt;</span> <span class="variable">value</span><span class="operator">);</span>
        <span class="variable">$w3</span><span class="operator">-&gt;</span><span class="variable">configure</span><span class="operator">(</span><span class="string">-optionY</span> <span class="operator">=&gt;</span> <span class="variable">value</span><span class="operator">);</span>
</pre>
</dd>
</li>
<dt><strong><a name="item__27otherstring_27"><strong>'otherstring'</strong></a></strong>

<dd>
<p>Call</p>
</dd>
<dd>
<pre>
    <span class="variable">$composite</span><span class="operator">-&gt;</span><span class="variable">Subwidget</span><span class="operator">(</span><span class="string">'otherstring'</span><span class="operator">)-&gt;</span><span class="variable">configure</span><span class="operator">(</span> <span class="string">-attribute</span> <span class="operator">=&gt;</span> <span class="variable">value</span> <span class="operator">);</span>
</pre>
</dd>
<dd>
<p>While this is here for backward compatibility with Tk-b5, it is probably
better just to use the subwidget reference directly.  The only
case for retaining this form is to allow an additional layer of
abstraction - perhaps having a 'current' subwidget - this is unproven.</p>
</dd>
</li>
<dt><strong><a name="item_aliases"><strong>Aliases</strong></a></strong>

<dd>
<p><code>ConfigSpecs( -alias =&gt; '-otherattribute' )</code> is used to make <code>-alias</code>
equivalent to <code>-otherattribute</code>. For example the aliases</p>
</dd>
<dd>
<pre>
    -fg =&gt; '-foreground',
    -bg =&gt; '-background'</pre>
</dd>
<dd>
<p>are provided automatically (if not already specified).</p>
</dd>
</li>
</dl>
<p>
</p>
<h2><a name="delegating_all_options_of_a_widget_class_to_a_subwidget">Delegating all options of a widget class to a subwidget</a></h2>
<pre>
    <span class="variable">$composite</span><span class="operator">-&gt;</span><span class="variable">ConfigSpecs</span><span class="operator">(</span><span class="variable">$subwidget</span><span class="operator">-&gt;</span><span class="variable">ConfigSpecs</span><span class="operator">);</span>
</pre>
<p>The above generates a list of <em>composite</em> ConfigSpecs arguments, one
for each valid option in $subwidget's class, and delegates said option
to $subwidget.  See <a href="../../lib/Tk/Widget.html">the Tk::Widget manpage</a> and the <em>widget</em> method
ConfigSpecs.  Duplicating <em>composite</em> ConfigSpecs and <em>widget</em>
ConfigSpecs keys will yield undefined results.</p>
<p>
</p>
<h2><a name="default_values">Default values</a></h2>
<p>When the <strong>Populate</strong> method returns <strong>ConfigDefault</strong> is called.  This calls</p>
<pre>
    <span class="variable">$composite</span><span class="operator">-&gt;</span><span class="variable">ConfigSpecs</span><span class="operator">;</span>
</pre>
<p>(with no arguments) to return a reference to a hash. Entries in the hash
take the form:</p>
<pre>
    '-attribute' =&gt; [ where, dbName, dbClass, default ]</pre>
<p><strong>ConfigDefault</strong> ignores 'where' completely (and also the DEFAULT entry) and
checks the 'options' database on the widget's behalf, and if an entry is
present matching dbName/dbClass</p>
<pre>
    -attribute =&gt; value</pre>
<p>is added to the list of options that <strong>new</strong> will eventually apply to the
widget. Likewise if there is not a match and default is defined this
default value will be added.</p>
<p>Alias entries in the hash are used to convert user-specified values for the
alias into values for the real attribute.</p>
<p>
</p>
<h2><a name="new__time_configure"><strong>New()-time configure</strong></a></h2>
<p>Once control returns to <strong>new</strong>, the list of user-supplied options
augmented by those from <strong>ConfigDefault</strong> are applied to the widget using the
<strong>configure</strong> method below.</p>
<p>Widgets are most flexible and most Tk-like if they handle the majority of
their attributes this way.</p>
<p>
</p>
<h2><a name="configuring_composites">Configuring composites</a></h2>
<p>Once the above have occurred calls of the form:</p>
<pre>
    <span class="variable">$composite</span><span class="operator">-&gt;</span><span class="variable">configure</span><span class="operator">(</span> <span class="string">-attribute</span> <span class="operator">=&gt;</span> <span class="variable">value</span> <span class="operator">);</span>
</pre>
<p>should behave like any other widget as far as end-user code is concerned.
<strong>configure</strong> will be handled by <strong>Tk::Derived::configure</strong> as follows:</p>
<pre>
    <span class="variable">$composite</span><span class="operator">-&gt;</span><span class="variable">ConfigSpecs</span><span class="operator">;</span>
</pre>
<p>is called (with no arguments) to return a reference to a hash <strong>-attribute</strong> is
looked up in this hash, if <strong>-attribute</strong> is not present in the hash then
<strong>'DEFAULT'</strong> is looked for instead.  (Aliases are tried as well and cause
redirection to the aliased attribute).  The result should be a reference to a
list like:</p>
<pre>
  [ where, dbName, dbClass, default ]</pre>
<p>at this stage only <em>where</em> is of interest, it maps to a list of object
references (maybe only one) foreach one</p>
<pre>
   <span class="variable">$object</span><span class="operator">-&gt;</span><span class="variable">configure</span><span class="operator">(</span> <span class="string">-attribute</span> <span class="operator">=&gt;</span> <span class="variable">value</span> <span class="operator">);</span>
</pre>
<p>is <strong>eval</strong>ed.</p>
<p>
</p>
<h2><a name="inquiring_attributes_of_composites">Inquiring attributes of composites</a></h2>
<pre>
   <span class="variable">$composite</span><span class="operator">-&gt;</span><span class="variable">cget</span><span class="operator">(</span> <span class="string">'-attribute'</span> <span class="operator">);</span>
</pre>
<p>This is handled by  <strong>Tk::Derived::cget</strong> in a similar manner to configure. At
present if <em>where</em> is a list of more than one object it is ignored completely
and the &quot;cached&quot; value in</p>
<pre>
   <span class="variable">$composite</span><span class="operator">-&gt;</span><span class="operator">{</span><span class="string">Configure</span><span class="operator">}{</span><span class="string">-attribute</span><span class="operator">}</span><span class="operator">.</span>
</pre>
<p>is returned.</p>
<p>
</p>
<hr />
<h1><a name="caveats">CAVEATS</a></h1>
<p>It is the author's intention to port as many of the &quot;Tix&quot; composite widgets
as make sense. The mechanism described above may have to evolve in order to
make this possible, although now aliases are handled I think the above is
sufficient.</p>
<p>
</p>
<hr />
<h1><a name="see_also">SEE ALSO</a></h1>
<p><a href="../../lib/Tk/composite.html">Tk::composite</a>,
<a href="../../lib/Tk/options.html">Tk::options</a>,
<a href="../../lib/Tk/Widget.html">the Tk::Widget manpage</a></p>

</body>

</html>

⌨️ 快捷键说明

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