initial checkin
[org.ibex.widgets.git] / src / ibex / lib / slider.t
1 <!-- Copyright 2004 - see COPYING for details [LGPL] -->\r
2 \r
3 <ibex>\r
4     <meta:doc>\r
5         Author: Charles Goodwin\r
6     </meta:doc>\r
7 \r
8     <ui:box>\r
9 \r
10         // public variables\r
11 \r
12         thisbox.interval;   // user defined interval between steps\r
13         thisbox.lowerlimit; // the lower limit of the value range\r
14         thisbox.upperlimit; // the upper limit of the value range\r
15         thisbox.step;       // the current step (integer)\r
16         thisbox.value;      // the current value\r
17 \r
18         // theme box traps\r
19 \r
20         thisbox.th_track;   // the track in which the slider runs\r
21         thisbox.th_handle;  // the slider handle\r
22 \r
23         // private variables\r
24 \r
25         var numsteps;       // the number of steps\r
26 \r
27         // write trap adjust the step according to the mouse position\r
28         action ++= function(v) {\r
29             step = ibex.math.round(numsteps * (track[mousepos] - handle[dim] / 2) / (track[dim] - handle[dim]));\r
30         }\r
31 \r
32         var synclimits = function(v) {\r
33             cascade = v;\r
34 \r
35             // we need all of the following to be set\r
36             if (interval == null || lowerlimit == null || upperlimit == null) return;\r
37 \r
38             // interval should to be a factor of (upperlimit - lowerlimit)\r
39             if ((upperlimit - lowerlimit) % interval)\r
40                 ibex.println("WARNING: limit difference is not divisible by interval");\r
41  \r
42             // work out the number of steps\r
43             numsteps = (upperlimit - lowerlimit) / interval;\r
44         }\r
45 \r
46         interval ++= synclimits;\r
47         lowerlimit ++= synclimits;\r
48         upperlimit ++= synclimits;\r
49 \r
50         orient ++= function(v) {\r
51             arguments.cascade(v);\r
52             track[mindim] = 0;\r
53             track[flip(mindim)] = handle[flip(dim)];\r
54         }\r
55 \r
56         step ++= function(v) {\r
57             // make sure step has a valid value\r
58             arguments.cascade((0 > v) ? 0 : (v > numsteps) ? numsteps : v);\r
59             // adjust the handle position according to the step\r
60             handle[pos] = step * (track[dim] - handle[dim]) / numsteps;\r
61         }\r
62 \r
63         // read trap to return value based on current step\r
64         value ++= function() {\r
65             return ibex.math.min(lowerlimit + step * interval, upperlimit);\r
66         }\r
67 \r
68         // write trap to set the step according to the given value\r
69         value ++= function(v) {\r
70             step = ibex.math.round((v - lowerlimit) / interval);\r
71         }\r
72 \r
73         // theme box traps\r
74 \r
75         handle ++= function(v) {\r
76             // always absolute so we can be moved\r
77             v.packed = false;\r
78 \r
79             // make sure the widget always has the correct flip(dim)\r
80             v.SizeChange ++= function(v) {\r
81                 track[flip(dim)] = handle[flip(dim)];\r
82             }\r
83         }\r
84 \r
85         KeyPressed ++= function(v) {\r
86             if (v == "left" or v == "down") step -= 1;\r
87             else if (v == "right" or v == "up") step += 1;\r
88         }\r
89 \r
90         Press1 ++= function(v) {\r
91             // activate the slider handle\r
92             var shift = surface.addEvent("_Move", thisbox, "Move");\r
93 \r
94             /* argh what to do? */\r
95             if (root.Move == null) {\r
96                 action = true;\r
97                 root.Move = function() { action = true; }\r
98                 root.Release1 = function() { root._Move = null; root._Release1 = null; }\r
99             }\r
100         }\r
101 \r
102     </ui:box>\r
103 </ibex>\r