1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="STYLESHEET" href="howto.css" type='text/css' />
<link rel="first" href="howto.html" title='Developing applications with Kiwi' />
<link rel='last' href='about.html' title='About this document...' />
<link rel='help' href='about.html' title='About this document...' />
<link rel="next" href="node22.html" />
<link rel="prev" href="node20.html" />
<link rel="parent" href="node16.html" />
<link rel="next" href="node22.html" />
<meta name='aesop' content='information' />
<title>2.9.5 Using Signal Handlers in Proxies</title>
</head>
<body>
<DIV CLASS="navigation">
<div id='top-navigation-panel' xml:id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="2.9.4 Customizing Proxies and"
href="node20.html"><img src='previous.png'
border='0' height='32' alt='Previous Page' width='32' /></A></td>
<td class='online-navigation'><a rel="parent" title="2.9 Proxies and Models"
href="node16.html"><img src='up.png'
border='0' height='32' alt='Up One Level' width='32' /></A></td>
<td class='online-navigation'><a rel="next" title="2.9.6 Widget support in"
href="node22.html"><img src='next.png'
border='0' height='32' alt='Next Page' width='32' /></A></td>
<td align="center" width="100%">Developing applications with Kiwi</td>
<td class='online-navigation'><img src='blank.png'
border='0' height='32' alt='' width='32' /></td>
<td class='online-navigation'><img src='blank.png'
border='0' height='32' alt='' width='32' /></td>
<td class='online-navigation'><img src='blank.png'
border='0' height='32' alt='' width='32' /></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="node20.html">2.9.4 Customizing Proxies and</A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="node16.html">2.9 Proxies and Models</A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="node22.html">2.9.6 Widget support in</A>
</div>
<hr /></div>
</DIV>
<!--End of Navigation Panel-->
<H3><A NAME="SECTION000295000000000000000">
2.9.5 Using Signal Handlers in Proxies</A>
</H3>
<P>
Proxies implement their behavior by using GTK+ signals extensively, but
this mechanism is implemented inside the Proxy and most of it happens
automatically, without user-visible effects beyond updating of the model
and the interface. However, many times an application needs custom
behavior beyond the simple model updating; for this, user-defined
handlers can be used normally.
<P>
Proxies inherit from the Delegate classes, and they can define signal
handlers in the same way as normal Delegates (and Controllers), by using
the special syntax as defined in section <A href="callbacks.html#callbacks">2.6</A>. However, there
is a special detail that should be understood when using custom
callbacks:
<P>
<BLOCKQUOTE>
<B>The Proxy's internal callbacks are executed first</B>.
</BLOCKQUOTE>
<P>
This means that, when your <code>on_*()</code> or <code>after_*()</code> handler
runs, the Proxy has already updated the model. In practice, this is a
<I>good</I> thing, since you usually want to work with the current
contents of the widget, not the contents it used to have. Of course,
signal handlers in the Proxy that are not associated to widgets attached
to model attributes (in other words, widgets not prefixed by a colon
(:) in the widgets list) do not present this issue (as there is no Proxy
signal handler for them anyway).
<P>
One common task of using signal handlers is to trigger updates in some
other part of interface. For instance, you might have a radiobutton that
makes a certain part of the interface sensitive when toggled. Or an
entry that needs to update a label that represents a calculated field in
the interface. You can define handlers normally, and you can take
advantage of the special Proxy method <code>update(<I>attribute_name</I>)</code>, which notifies the Proxy that it should refresh the
interface representation of that attribute.
<P>
A step-by-step analysis of this example:
<P>
<UL>
<LI>We define a class for our domain object, <tt class="class">Temperature</tt>. This
class offers accessors for two "fake" attributes - they don't exist
permanently in the instance, but instead are calculated through
accessors. Note that we <I>must</I> initialize any values that we use in
the accessors, as they will be called upon startup; temperature will not
have been set through the interface at that point yet.
<P>
</LI>
<LI>The accessors return an empty string if temperature is an empty
string, and calculate the values otherwise. This agrees with the
behaviour defined for this application before: an empty temperature
should render no output for farenheit or celsius.
<P>
</LI>
<LI>We create a proxy class for our application. We first set a
conversion format for <code>farenheit</code> and <code>celsius</code> so it will
print out the float values they return correctly. <code>set_format()</code>
is discussed further in section <A href="undo.html#formats">2.9</A>.
<P>
</LI>
<LI>We then initialize the parent class, using the same glade file as
the other Faren examples. We do basic cosmetic updates, and then define
two handlers.
<P>
</LI>
<LI>The first handler is simply for quit, and doesn't do anything
special. The second handler, however, is triggered whenever the
temperature is changed, and it uses the special <code>update()</code> method
to notify the proxy that both <code>farenheit</code> and <code>celsius</code> were
updated.
<P>
</LI>
</UL>
<P>
Upon running the example you will see that it works as expected;
the <code>update()</code> messages in fact make the labels render the correct
values, and the accessors return empty strings when they should.
<P>
There is an additional hook you can use in your Proxy classes to make
updating easier. If you define a method called <code>proxy_updated()</code>,
this method will be called <I>each</I> time a proxy-managed widget is
manipulated by the user. This allows you to easily perform updating of
calculated text indicators, for instance.
<P>
There is a set of more complex examples that demonstrate the use of
custom handlers in the package, under the directory
<span class="file">examples/Diary</span>, which are recommended as further reference.
<P>
<DIV CLASS="navigation">
<div class='online-navigation'>
<p></p><hr />
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="2.9.4 Customizing Proxies and"
href="node20.html"><img src='previous.png'
border='0' height='32' alt='Previous Page' width='32' /></A></td>
<td class='online-navigation'><a rel="parent" title="2.9 Proxies and Models"
href="node16.html"><img src='up.png'
border='0' height='32' alt='Up One Level' width='32' /></A></td>
<td class='online-navigation'><a rel="next" title="2.9.6 Widget support in"
href="node22.html"><img src='next.png'
border='0' height='32' alt='Next Page' width='32' /></A></td>
<td align="center" width="100%">Developing applications with Kiwi</td>
<td class='online-navigation'><img src='blank.png'
border='0' height='32' alt='' width='32' /></td>
<td class='online-navigation'><img src='blank.png'
border='0' height='32' alt='' width='32' /></td>
<td class='online-navigation'><img src='blank.png'
border='0' height='32' alt='' width='32' /></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="node20.html">2.9.4 Customizing Proxies and</A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="node16.html">2.9 Proxies and Models</A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="node22.html">2.9.6 Widget support in</A>
</div>
</div>
<hr />
<span class="release-info">Release 1.9.22, documentation updated on August, 2006.</span>
</DIV>
<!--End of Navigation Panel-->
</BODY>
</HTML>
|