Hi, <br><br>I just added an entry to the comments in the Scheme cookbook in the 'GUI: <a name="Aggregating_Widgets_in_a_Single_">Aggregating Widgets in a Single Widget' section<br></a> -- <a href="http://schemecookbook.org/Cookbook/GUIWidgetAggregation">
http://schemecookbook.org/Cookbook/GUIWidgetAggregation</a><br clear="all"><p>It is an example of a simple widget created from drawing toolkit elements, rather than existing widgets.</p><p>I'd appreciate any comments/edits to improve it. (technically or pedagogically)
</p><p>Cheers, Stephen</p><p>
</p><div class="scheme"><pre><span class="comment">; make new widget'scatter-chart%'<br></span><span class="comment">;; do this all in Pretty Big language (DrScheme only)<br></span>(<span class="keyword">define</span>
<span class="variable">scatter-chart%</span> (<span class="variable">class</span> <span class="variable">pane%</span> ()<br> (<span class="variable">init-field</span> <span class="variable">parent
</span>)<br> <span class="comment">; Make the drawing area<br></span> (<span class="variable">super-instantiate</span> () (<span class="variable">parent</span> <span class="variable">
parent</span>) <span class="comment">; instantiate this FIRST<br></span> [<span class="variable">min-width</span> <span class="selfeval">100</span>] [<span class="variable">stretchable-width</span>
<span class="selfeval">#f</span>]<br> (<span class="variable">min-height</span> <span class="selfeval">200</span>) [<span class="variable">stretchable-height</span> <span class="selfeval">#f</span>
])<br> <br> <span class="comment">;; internal data<br></span> (<span class="keyword">define</span> <span class="variable">list-of-points</span> <span class="keyword">
'</span>()) <span class="comment">;; start with empty list<br></span> (<span class="keyword">define</span> <span class="variable">canvas</span> (<span class="variable">new</span> <span class="variable">
canvas%</span> [<span class="variable">parent</span> <span class="variable">this</span>] <br> (<span class="variable">paint-callback</span> (<span class="keyword">lambda</span>
(<span class="variable">canvas</span> <span class="variable">dc</span>) (<span class="variable">draw-chart</span>)))<br> [<span class="variable">min-width</span> <span class="selfeval">
100</span>] [<span class="variable">stretchable-width</span> <span class="selfeval">#f</span>]<br> [<span class="variable">min-height</span> <span class="selfeval">200</span>] [
<span class="variable">stretchable-height</span> <span class="selfeval">#f</span>]<br> ))<br> (<span class="keyword">define</span> <span class="variable">
dc</span> (<span class="variable">send</span> <span class="variable">canvas</span> <span class="variable">get-dc</span>)) <span class="comment">; Get the canvas's drawing context<br></span> <span class="comment">
; Make some pens and brushes for use later<br></span> (<span class="keyword">define</span> <span class="variable">no-pen</span> (<span class="variable">make-object</span> <span class="variable">pen%
</span> <span class="selfeval">"BLACK"</span> <span class="selfeval">1</span> <span class="keyword">'</span><span class="variable">transparent</span>))<br> (<span class="keyword">define
</span> <span class="variable">blue-brush</span> (<span class="variable">make-object</span> <span class="variable">brush%</span> <span class="selfeval">"BLUE"</span> <span class="keyword">'</span><span class="variable">
solid</span>))<br> (<span class="keyword">define</span> <span class="variable">yellow-brush</span> (<span class="variable">make-object</span> <span class="variable">brush%</span> <span class="selfeval">
"YELLOW"</span> <span class="keyword">'</span><span class="variable">solid</span>))<br> <br> <span class="comment">;; public methods<br></span>
<span class="comment">;; add-point : new-point<br></span> (<span class="variable">define/public</span> (<span class="variable">add-point</span> <span class="variable">new-point</span>)<br> (
<span class="keyword">set!</span> <span class="variable">list-of-points</span> (<span class="builtin">cons</span> <span class="variable">new-point</span> <span class="variable">list-of-points</span>)) <span class="comment">
;; add to the list<br></span> (<span class="variable">send</span> <span class="variable">dc</span> <span class="variable">draw-rectangle</span> <span class="selfeval">0</span> <span class="variable">
new-point</span> <span class="selfeval">200</span> <span class="selfeval">1</span>)) <span class="comment">;; and draw the line<br></span> <span class="comment">;; draw-chart : draw a chart based on the list of integers supplied
<br></span> <span class="comment">;; see <a href="http://download.plt-scheme.org/doc/371/html/mred/mred-Z-H-397.html">http://download.plt-scheme.org/doc/371/html/mred/mred-Z-H-397.html</a><br></span>
(<span class="variable">define/public</span> (<span class="variable">draw-chart</span>)<br> (<span class="variable">send</span> <span class="variable">dc</span> <span class="variable">
set-pen</span> <span class="variable">no-pen</span>)<br> (<span class="variable">send</span> <span class="variable">dc</span> <span class="variable">set-brush</span> <span class="variable">blue-brush
</span>)<br> (<span class="variable">send</span> <span class="variable">dc</span> <span class="variable">draw-rectangle</span> <span class="selfeval">0</span> <span class="selfeval">0</span> <span class="selfeval">
100</span> <span class="selfeval">200</span>) <span class="comment">; big blue rectangle<br></span> (<span class="variable">send</span> <span class="variable">dc</span> <span class="variable">set-brush
</span> <span class="variable">yellow-brush</span>)<br> (<span class="builtin">map</span><br> (<span class="keyword">lambda</span> (<span class="variable">y</span>) (<span class="variable">
send</span> <span class="variable">dc</span> <span class="variable">draw-rectangle</span> <span class="selfeval">0</span> <span class="variable">y</span> <span class="selfeval">200</span> <span class="selfeval">1</span>))
<span class="comment">; draw a line for each integer.<br></span> <span class="variable">list-of-points</span> ))<br> (<span class="variable">draw-chart</span>))) <span class="comment">
; draw it for the first time when this class is instantiated<br></span><br><span class="comment">; Make a frame<br></span>(<span class="keyword">define</span> <span class="variable">frame</span> (<span class="variable">new
</span> <span class="variable">frame%</span> [<span class="variable">label</span> <span class="selfeval">"scatter-chart% Example"</span>] [<span class="variable">width</span> <span class="selfeval">100</span>] [
<span class="variable">height</span> <span class="selfeval">200</span>]))<br><br><span class="comment">; Show the frame<br></span>(<span class="variable">send</span> <span class="variable">frame</span> <span class="variable">
show</span> <span class="selfeval">#t</span>)<br><span class="comment">; Wait a second to let the window get ready<br></span><span class="comment">;(sleep/yield 1)<br></span>(<span class="keyword">define</span> <span class="variable">
new-my-widget</span> (<span class="variable">new</span> <span class="variable">scatter-chart%</span> (<span class="variable">parent</span> <span class="variable">frame</span>) )) <span class="comment">; instantiate the widget in the frame
<br></span>(<span class="keyword">define</span> <span class="variable">new-his-widget</span> (<span class="variable">new</span> <span class="variable">scatter-chart%</span> (<span class="variable">parent</span> <span class="variable">
frame</span>) )) <span class="comment">; make another one<br></span><br>(<span class="variable">send</span> <span class="variable">new-my-widget</span> <span class="variable">draw-chart</span>)<br><span class="comment">; Draw the face
<br></span>(<span class="keyword">define</span> (<span class="variable">go</span>) <br> (<span class="builtin">map</span> (<span class="keyword">lambda</span> (<span class="variable">y</span>)<br><br> (<span class="variable">
send</span> <span class="variable">new-my-widget</span> <span class="variable">add-point</span> <span class="variable">y</span>) <span class="comment">; add a point at a time<br></span> (<span class="variable">sleep/yield
</span> <span class="selfeval">0.05</span>)) <span class="comment">; draw it slowly<br></span> <span class="keyword">'</span>(<span class="selfeval">10</span> <span class="selfeval">30</span> <span class="selfeval">
22</span> <span class="selfeval">77</span> <span class="selfeval">78</span> <span class="selfeval">79</span> <span class="selfeval">17</span> <span class="selfeval">65</span> <span class="selfeval">97</span> <span class="selfeval">
54</span> <span class="selfeval">55</span> <span class="selfeval">56</span> <span class="selfeval">57</span> <span class="selfeval">91</span> <span class="selfeval">33</span> <span class="selfeval">55</span> <span class="selfeval">
95</span> <span class="selfeval">99</span> <span class="selfeval">125</span> <span class="selfeval">155</span> <span class="selfeval">157</span> <span class="selfeval">159</span> <span class="selfeval">188</span> <span class="selfeval">
187</span> <span class="selfeval">186</span> <span class="selfeval">191</span>))) <span class="comment">;; list of datapoints<br></span><br>(<span class="variable">go</span>) <span class="comment">;; !!!<br></span></pre>
</div><br>-- <br>Cheers,<br><br>Stephen<br><br><br><br>--<br>Stephen De Gabrielle<br><a href="mailto:s.degabrielle@ucl.ac.uk">s.degabrielle@ucl.ac.uk</a><br>Telephone +44 (0)20 7679 5242 (x45242)<br>Mobile 079 851 890 45
<br><a href="http://www.uclic.ucl.ac.uk/annb/MaSI.html">http://www.uclic.ucl.ac.uk/annb/MaSI.html</a><br>University College London Interaction Centre<br>Remax House - 31/32 Alfred Place<br>London - WC1E 7DP