initial checkin
[org.ibex.widgets.git] / src / ibex / lib / focusmanager.t
1 <!-- Copyleft 2004 - see COPYING for details [LGPL] -->
2
3 <ibex>
4     <meta:doc>
5         Authors: Charles Goodwin, David Crawshaw
6
7         For documentation, see ibex.theme.surface
8     </meta:doc>
9
10     <ui:box>
11
12         var focus = null;   // the current focus
13
14         // the focus list for this surface
15         var fvector = ibex..ibex.util.vector..newVector();
16         //var fvector = ibex..ibex.util.vector(ibex.box);
17
18         // read trap to access the focused child
19         surface.focus ++= function() { return focus; }
20
21         surface.setFocus ++= function(v) {
22             if (v and v.focusable and fvector.contains(v)) {
23                 if (focus and focus != v) focus.focused = false;
24                 return true;
25             }
26             else if (!v and focus) {
27                 focus.focused = false;
28                 return true;
29             }
30             else return false;
31         }
32
33         // function to add a focus to the focus vector, optionally after w
34         surface.addFocus = function(v, w) {
35             // make sure we're not already in the focus list
36             surface.dropFocus(v);
37             
38             if (w and fvector.contains(w))
39                 return fvector.insert(v, w);
40             else return fvector.push(v);
41         }
42
43         // function to drop a focus from the focus vector for this surface
44         surface.dropFocus = function(v) {
45             if (fvector.contains(v)) {
46                 if (focus == v) focus = v.nextfocus;
47                 return fvector.remove(v);
48             }
49             else return false;
50         }
51
52         // function to return the next-in-line for focus after v
53         surface.nextFocus ++= function(v) {
54             var nf = fvector.after(v);
55             while (!nf.focusable and nf != v) nf = fvector.after(nf);
56             return nf;
57         }
58
59         // function to return the previous-in-line for focus after v
60         surface.prevFocus ++= function(v) {
61             var pf = fvector.after(v);
62             while (!pf.focusable and pf != v) pf = fvector.before(pf);
63             return pf;
64         }
65
66         // private function to map events from this root ui:box onto the focused child
67         var passEvent = function(v) { if (focus) focus[trapname] = v; }
68
69         _KeyPressed ++= passEvent;      // pass keypress events to focused child
70         _KeyReleased ++= passEvent;     // pass keyrelease events to focused child
71
72     </ui:box>
73 </ibex>