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

📄 mega.html

📁 perl教程
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<p><em>$package</em> is the package of the mega-widget (e.g., <strong>Tk::MyText</strong>,
<em>$parent</em> the parent of the widget to be created and $args the hash
reference to the options specified in the widget constructor call.</p>
<p>Don't forget to call <em>$package</em>-&gt;<strong>SUPER::CreateArgs</strong>(<em>$parent</em>, <em>$args</em>) in
<strong>CreateArgs</strong>.</p>
<p>
</p>
<h2><a name="delegates">Delegates</a></h2>
<p>Redirect a method of the mega-widget to a subwidget of
the composite widget</p>
<p>Usage:</p>
<pre>
    <span class="variable">$cw</span><span class="operator">-&gt;</span><span class="variable">Delegates</span><span class="operator">(</span>
        <span class="string">'method1'</span> <span class="operator">=&gt;</span> <span class="variable">$subwidget1</span><span class="operator">,</span>
        <span class="string">'method2'</span> <span class="operator">=&gt;</span> <span class="string">'advertived_name'</span><span class="operator">,</span>
        <span class="operator">...,</span>
        <span class="string">'Construct'</span> <span class="operator">=&gt;</span> <span class="variable">$subwidget2</span><span class="operator">,</span>
        <span class="string">'DEFAULT'</span>   <span class="operator">=&gt;</span> <span class="variable">$subwidget3</span><span class="operator">,</span>
    <span class="operator">);</span>
</pre>
<p>The <strong>'Construct'</strong> delegation has a special meaning.  After
'Construct' is delegated all Widget constructors are redirected.
E.g. after</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;<em>$self</em>-&gt;<strong>Delegates</strong>(<strong>'Construct'</strong>=&gt;<em>$subframe</em>);</p>
<p>a <em>$self</em>-&gt;<strong>Button</strong> does really a <em>$subframe</em>-&gt;<strong>Button</strong>
so the created button is a child of <em>$subframe</em> and not <em>$self</em>.</p>
<p><strong>Comment:</strong> Delegates works only with methods that <em>$cw</em> does
not have itself.</p>
<p>
</p>
<h2><a name="initobject">InitObject</a></h2>
<p><em>Note: this method should not, in general, be used, as it has been
superceeded by <strong>Populate</strong> and specifying <strong>Tk::Derived</strong> as one of the base
classes.</em></p>
<p>Defines construction and interface of derived widgets.</p>
<p>Usage:</p>
<pre>
    <span class="keyword">sub</span><span class="variable"> InitObject </span><span class="operator">{</span>
        <span class="keyword">my</span> <span class="operator">(</span><span class="variable">$derived</span><span class="operator">,</span> <span class="variable">$args</span><span class="operator">)</span> <span class="operator">=</span> <span class="variable">@_</span><span class="operator">;</span>
        <span class="operator">...</span>
    <span class="operator">}</span>
</pre>
<p>where <em>$derived</em> is the widget reference of the already created
baseclass widget and <em>$args</em> is the reference to a hash of
<em>-option-value</em> pairs.</p>
<p><strong>InitObject</strong> is almost identical to <a href="#populate">Populate</a> method.
<strong>Populate</strong> does some more 'magic' things useful for mega-widgets
with several widgets.</p>
<p>Don't forget to call <em>$derived</em>-&gt;<strong>SUPER::InitObject</strong>(<em>$args</em>) in
<strong>InitObject</strong>.</p>
<p>
</p>
<h2><a name="ondestroy">OnDestroy</a></h2>
<p>Define a callback invoked when the mega-widget is destroyed.</p>
<p>Usage:</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;<em>$widget</em>-&gt;<strong>OnDestroy</strong>(<em>callback</em>);</p>
<p><strong>OnDestroy</strong> installs a <a href="../../lib/Tk/callbacks.html">callback</a> that's called
when a widget is going to to be destroyed.  Useful
for special cleanup actions.  It differs from a normal <strong>destroy</strong>
in that all the widget's data structures are still intact.</p>
<p><strong>Comment:</strong> This method could be used with any widgets not just
for mega-widgets.  It's listed here because of it's usefulness.</p>
<p>
</p>
<h2><a name="populate">Populate</a></h2>
<p>Defines construction and interface of the composite
widget.</p>
<p>Usage:</p>
<pre>
    <span class="keyword">sub</span><span class="variable"> Populate </span><span class="operator">{</span>
        <span class="keyword">my</span> <span class="operator">(</span><span class="variable">$self</span><span class="operator">,</span> <span class="variable">$args</span><span class="operator">)</span> <span class="operator">=</span> <span class="variable">@_</span><span class="operator">;</span>
        <span class="operator">...</span>
    <span class="operator">}</span>
</pre>
<p>where <em>$self</em> is the widget reference of the already created baseclass
widget and <em>$args</em> is the
reference to a hash of <em>-option-value</em> pairs.</p>
<p>Most the other support function are normally used inside the <strong>Populate</strong>
subroutine.</p>
<p>Don't forget to call <em>$cw</em>-&gt;<strong>SUPER::Populate</strong>(<em>$args</em>) in
<strong>Populate</strong>.</p>
<p>
</p>
<h2><a name="privatedata">privateData</a></h2>
<p>Set/get a private hash of a widget to storage
composite internal data</p>
<p>Usage:</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;<em>$hashref</em> = <em>$self</em>-&gt;<strong>privateData</strong>();</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;<em>$another</em> = <em>$self</em>-&gt;<strong>privateData</strong>(<em>unique_key</em>|<em>package</em>);</p>
<p>
</p>
<h2><a name="subwidget">Subwidget</a></h2>
<p>Get the widget reference of an advertised subwidget.</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;<em>@subwidget</em> = <em>$cw</em>-&gt;<strong>Subwidget</strong>();</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;<em>$subwidget</em> = <em>$cw</em>-&gt;<strong>Subwidget</strong>(<em>name</em>);</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;<em>@subwidget</em> = <em>$cw</em>-&gt;<strong>Subwidget</strong>(<em>name</em> ?,...?);</p>
<p>Returns the widget <code>reference(s)</code> of the subwidget known under the given
name(s). Without arguments, return all known subwidgets of <em>$cw</em>. See
<a href="#advertise">Advertise</a> method how to define <em>name</em> for a subwidget.</p>
<p><strong>Comment:</strong> Mega-Widget Users: Use <strong>Subwidget</strong> to get <em>only</em>
documented subwidgets.</p>
<p>
</p>
<hr />
<h1><a name="pitfalls">PITFALLS</a></h1>
<ul>
<li><strong><a name="item_resource_db_class_name">Resource DB class name</a></strong>

<p>Some of the standard options use a resource date base class
that is not equal to the resource database name.  E.g.,</p>
<pre>
  Switch:            Name:             Class:</pre>
<pre>
  -padx              padX              Pad
  -activerelief      activeRelief      Relief
  -activebackground  activeBackground  Foreground
  -status            undef             undef</pre>
<p>One should do the same when one defines one of these
options via <strong>ConfigSpecs</strong>.</p>
</li>
<li><strong><a name="item_method_delegation">Method delegation</a></strong>

<p>Redirecting methods to a subwidget with <strong>Delegate</strong>
can only work if the base widget itself does have a
method with this name.  Therefore one can't ``<em>delegate</em>''
any of the methods listed in <a href="../../lib/Tk/Widget.html">Tk::Widget</a>.
A common problematic method is <strong>bind</strong>.  In this case
one as to explicitely redirect the method.</p>
<pre>
  <span class="keyword">sub</span><span class="variable"> bind </span><span class="operator">{</span>
      <span class="keyword">my</span> <span class="variable">$self</span> <span class="operator">=</span> <span class="keyword">shift</span><span class="operator">;</span>
      <span class="keyword">my</span> <span class="variable">$to</span> <span class="operator">=</span> <span class="variable">$self</span><span class="operator">-&gt;</span><span class="variable">privateData</span><span class="operator">-&gt;</span><span class="operator">{</span><span class="string">'my_bind_target'</span><span class="operator">}</span><span class="operator">;</span>
      <span class="variable">$to</span><span class="operator">-&gt;</span><span class="keyword">bind</span><span class="operator">(</span><span class="variable">@_</span><span class="operator">);</span>
  <span class="operator">}</span>
</pre>
</li>
<li><strong><a name="item_privatedata">privateData</a></strong>

<p>Graham Barr wrote: ... It is probably
more private than most people think. Not all calls to privateData will
return that same HASH reference. The HASH reference that is returned
depends on the package it was called from, a different HASH is returned
for each package. This allows a widget to hold private data, but then
if it is sub-classed the sub-class will get a different HASH and so not
cause duplicate name clashes.</p>
<p>But privateData does take an optional argument if you want to
force which HASH is returned.</p>
</li>
<li><strong><a name="item_scrolled_and_composite">Scrolled and Composite</a></strong>

<p><strong>Scrolled</strong>(<em>Kind</em>,...) constructor can not be used with <strong>Composite</strong>.
One has to use $cw-&gt;<strong>Composite</strong>(<strong>Scrl</strong><em>Kind</em> =&gt; <strong>'name'</strong>, ...);</p>
</li>
</ul>
<p>
</p>
<hr />
<h1><a name="missing">MISSING</a></h1>
<p>Of course Perl/Tk does not define support function for
all necessities.  Here's a short list of things you have to
handle yourself:</p>
<ul>
<li>
<p>No support to define construction-time only options.</p>
</li>
<li>
<p>No support to remove an option that is known to the
base widget.</p>
</li>
<li>
<p>It's hard to define <strong>undef</strong> as fallback for an widget
option that is not already <strong>undef</strong>.</p>
</li>
<li>
<p>Frame in Perl/Tk carries magic and overhead not needed
for composite widget class definition.</p>
</li>
<li>
<p>No support methods for bindings that are shared between all
widgets of a composite widget (makes sense at all?)</p>
</li>
</ul>
<p>
</p>
<hr />
<h1><a name="keywords">KEYWORDS</a></h1>
<p>mega, composite, derived, widget</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/ConfigSpecs.html">Tk::ConfigSpecs</a>
<a href="../../lib/Tk/option.html">Tk::option</a>
<a href="../../lib/Tk/callbacks.html">Tk::callbacks</a>
<a href="../../lib/Tk/bind.html">Tk::bind</a></p>

</body>

</html>

⌨️ 快捷键说明

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