initial checkin
[org.ibex.widgets.git] / src / ibex / lib / spin.t
1 <!-- Copyleft 2004 - see COPYING for details [LGPL] -->
2
3 <ibex>
4     <meta:doc>
5         Author: Charles Goodwin
6
7         FIXME: 
8             - be more restrictive of puts to value
9             - value needs to be trappable
10     </meta:doc>
11
12     <ui:box>
13
14         // public variables
15
16         thisbox.interval;   // interval by which up/down in/de-crement the result
17         thisbox.maxvalue;   // maximum value of the result
18         thisbox.minvalue;   // minimum value of the result
19         thisbox.precision;  // number of decimal places of result
20
21         var is_int;         // parse integeters or floats
22
23         // theme box variables
24
25         thisbox.th_less;   // the button to decrement
26         thisbox.th_more;   // the button to increment
27         thisbox.th_output; // the th_output for the result
28
29         // private function to get the value from a string
30         var parse = function(v) { return is_int ? ibex.math.parseInt(v) : ibex.math.parseFloat(v); }
31
32         // write trap to establish is_int based on interval
33         interval ++= function(v) { is_int = !(v % 1); }
34
35         KeyPressed ++= function(v) {
36             if (v == "down" or v == "DOWN") th_less.action = true;
37             else if (v == "up" or v == "UP") th_more.action = true;
38         }
39
40         value ++= function() { return ibex.string.parse(th_output.text); }
41
42         value ++= function(v) { th_output.text = "" + v; }
43
44         // theme box traps
45
46         th_less ++= function(v) {
47             v.action ++= function(v) {
48                 th_output.text = "" + ibex.math.max(parse(th_output.text) - interval, minvalue);
49             }
50         }
51
52         th_more ++= function(v) {
53             v.action ++= function(v) {
54                 th_output.text = "" + ibex.math.min(parse(th_output.text) + interval, maxvalue);
55             }
56         }
57
58     </ui:box>
59 </ibex>