]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/menu-div0test/item/inputcontainer.c
fix selection bugs when enabling/disabling the checkboxes
[divverent/nexuiz.git] / data / qcsrc / menu-div0test / item / inputcontainer.c
1 #ifdef INTERFACE
2 CLASS(InputContainer) EXTENDS(Container)
3         METHOD(InputContainer, keyDown, float(entity, float, float, float))
4         METHOD(InputContainer, mouseMove, float(entity, vector))
5         METHOD(InputContainer, mousePress, float(entity, vector))
6         METHOD(InputContainer, mouseRelease, float(entity, vector))
7         METHOD(InputContainer, mouseDrag, float(entity, vector))
8         METHOD(InputContainer, focusLeave, void(entity))
9         METHOD(InputContainer, resizeNotify, void(entity, vector, vector, vector, vector))
10
11         METHOD(InputContainer, _changeFocusXY, float(entity, vector))
12         ATTRIB(InputContainer, mouseFocusedChild, entity, NULL)
13         ATTRIB(InputContainer, isTabRoot, float, 0)
14 ENDCLASS(InputContainer)
15 #endif
16
17 #ifdef IMPLEMENTATION
18 void resizeNotifyInputContainer(entity me, vector relOrigin, vector relSize, vector absOrigin, vector absSize)
19 {
20         resizeNotifyContainer(me, relOrigin, relSize, absOrigin, absSize);
21         if(me.parent.instanceOfInputContainer)
22                 me.isTabRoot = 0;
23         else
24                 me.isTabRoot = 1;
25 }
26
27 void focusLeaveInputContainer(entity me)
28 {
29         focusLeaveContainer(me);
30         me.mouseFocusedChild = NULL;
31 }
32
33 float keyDownInputContainer(entity me, float scan, float ascii, float shift)
34 {
35         entity f, ff;
36         if(keyDownContainer(me, scan, ascii, shift))
37                 return 1;
38         if(scan == K_ESCAPE)
39         {
40                 f = me.focusedChild;
41                 if(f)
42                 {
43                         me.setFocus(me, NULL);
44                         return 1;
45                 }
46                 return 0;
47         }
48         if(scan == K_TAB)
49         {
50                 f = me.focusedChild;
51                 if(shift & S_SHIFT)
52                 {
53                         if(f)
54                         {
55                                 for(ff = f.prevSibling; ff; ff = ff.prevSibling)
56                                 {
57                                         if not(ff.focusable)
58                                                 continue;
59                                         me.setFocus(me, ff);
60                                         return 1;
61                                 }
62                         }
63                         if(!f || me.isTabRoot)
64                         {
65                                 for(ff = me.lastChild; ff; ff = ff.prevSibling)
66                                 {
67                                         if not(ff.focusable)
68                                                 continue;
69                                         me.setFocus(me, ff);
70                                         return 1;
71                                 }
72                                 return 0; // AIIIIEEEEE!
73                         }
74                 }
75                 else
76                 {
77                         if(f)
78                         {
79                                 for(ff = f.nextSibling; ff; ff = ff.nextSibling)
80                                 {
81                                         if not(ff.focusable)
82                                                 continue;
83                                         me.setFocus(me, ff);
84                                         return 1;
85                                 }
86                         }
87                         if(!f || me.isTabRoot)
88                         {
89                                 for(ff = me.firstChild; ff; ff = ff.nextSibling)
90                                 {
91                                         if not(ff.focusable)
92                                                 continue;
93                                         me.setFocus(me, ff);
94                                         return 1;
95                                 }
96                                 return 0; // AIIIIEEEEE!
97                         }
98                 }
99         }
100         return 0;
101 }
102
103 float _changeFocusXYInputContainer(entity me, vector pos)
104 {
105         entity e, ne;
106         e = me.mouseFocusedChild;
107         ne = me.itemFromPoint(me, pos);
108         if(ne)
109                 if not(ne.focusable)
110                         ne = NULL;
111         me.mouseFocusedChild = ne;
112         if(ne)
113                 if(ne != e)
114                 {
115                         me.setFocus(me, ne);
116                         if(ne.instanceOfInputContainer)
117                         {
118                                 ne.focusedChild = NULL;
119                                 ne._changeFocusXY(e, globalToBox(pos, ne.Container_origin, ne.Container_size));
120                         }
121                 }
122         return (ne != NULL);
123 }
124
125 float mouseDragInputContainer(entity me, vector pos)
126 {
127         if(mouseDragContainer(me, pos))
128                 return 1;
129         if(pos_x >= 0 && pos_y >= 0 && pos_x < 1 && pos_y < 1)
130                 return 1;
131         return 0;
132 }
133 float mouseMoveInputContainer(entity me, vector pos)
134 {
135         if(me._changeFocusXY(me, pos))
136                 if(mouseMoveContainer(me, pos))
137                         return 1;
138         if(pos_x >= 0 && pos_y >= 0 && pos_x < 1 && pos_y < 1)
139                 return 1;
140         return 0;
141 }
142 float mousePressInputContainer(entity me, vector pos)
143 {
144         me.mouseFocusedChild = NULL; // force focusing
145         if(me._changeFocusXY(me, pos))
146                 if(mousePressContainer(me, pos))
147                         return 1;
148         if(pos_x >= 0 && pos_y >= 0 && pos_x < 1 && pos_y < 1)
149                 return 1;
150         return 0;
151 }
152 float mouseReleaseInputContainer(entity me, vector pos)
153 {
154         float r;
155         r = mouseReleaseContainer(me, pos);
156         if(me.focused) // am I still eligible for this? (UGLY HACK, but a mouse event could have changed focus away)
157                 if(me._changeFocusXY(me, pos))
158                         return 1;
159         if(pos_x >= 0 && pos_y >= 0 && pos_x < 1 && pos_y < 1)
160                 return 1;
161         return 0;
162 }
163 #endif