]> git.mxchange.org Git - simgear.git/blob - PUI/puInterface.cxx
Added an FG_SERIAL type to the FG_LOG macro.
[simgear.git] / PUI / puInterface.cxx
1
2 #include "puLocal.h"
3
4 #define PUSTACK_MAX 100
5
6 static int currLiveInterface = -1 ;
7 static puInterface *liveInterfaceStack [ PUSTACK_MAX ] ;
8 static int currInterface = -1 ;
9 static puInterface *interfaceStack [ PUSTACK_MAX ] ;
10
11 void puPushLiveInterface ( puInterface *in )
12 {
13   if ( currLiveInterface < PUSTACK_MAX )
14     liveInterfaceStack [ ++currLiveInterface ] = in ;
15   else
16     fprintf ( stderr, "PUI: Too many live puInterfaces open at once!\n" ) ;
17 }
18
19 void puPushInterface ( puInterface *in )
20 {
21   if ( currInterface < PUSTACK_MAX )
22     interfaceStack [ ++currInterface ] = in ;
23   else
24     fprintf ( stderr, "PUI: Too many puInterfaces open at once!\n" ) ;
25 }
26
27 void  puPopLiveInterface ( void )
28 {
29   if ( currLiveInterface > 0 )
30     --currLiveInterface ;
31   else 
32     fprintf ( stderr, "PUI: Live puInterface stack is empty!\n" ) ;
33 }
34
35 void  puPopInterface ( void )
36 {
37   if ( currInterface > 0 )
38     --currInterface ;
39   else 
40     fprintf ( stderr, "PUI: puInterface stack is empty!\n" ) ;
41 }
42
43 int  puNoLiveInterface ( void )
44 {
45   return currLiveInterface < 0 ;
46 }
47
48 int  puNoInterface ( void )
49 {
50   return currInterface < 0 ;
51 }
52
53 puInterface *puGetUltimateLiveInterface ( void )
54 {
55   if ( currLiveInterface < 0 )
56   {
57     fprintf ( stderr, "PUI: No Live Interface!\n" ) ;
58     return NULL ;
59   }
60
61   return liveInterfaceStack [ 0 ] ;
62 }
63
64
65 puInterface *puGetBaseLiveInterface ( void )
66 {
67   if ( currLiveInterface < 0 )
68   {
69     fprintf ( stderr, "PUI: No Live Interface!\n" ) ;
70     return NULL ;
71   }
72
73   /*
74     Work down the interface stack until you
75     either get to the bottom or find a block
76     in the form of a puDialogBox.
77   */
78
79   for ( int i = currLiveInterface ; i > 0 ; i-- )
80     if ( liveInterfaceStack [ i ] -> getType () & PUCLASS_DIALOGBOX )
81       return liveInterfaceStack [ i ] ; 
82
83   return liveInterfaceStack [ 0 ] ;
84 }
85
86 puInterface *puGetCurrInterface ( void )
87 {
88   if ( currInterface < 0 )
89   {
90     fprintf ( stderr, "PUI: No Interface!\n" ) ;
91     return NULL ;
92   }
93
94   return interfaceStack [ currInterface ] ;
95 }
96
97 void puInterface::remove ( puObject *obj )
98 {
99   if ( dlist == NULL )
100     return ;
101
102   /* Are we the first object in the list */
103
104   if ( obj -> prev == NULL )
105     dlist = obj -> next ;
106   else
107     obj -> prev -> next = obj -> next ;
108
109   /* Are we the last object in the list */
110
111   if ( obj -> next != NULL )
112     obj -> next -> prev = obj -> prev ;
113
114   obj -> next = NULL ;
115   obj -> prev = NULL ;
116
117   num_children-- ;
118   recalc_bbox () ;
119 }
120
121 void puInterface::add ( puObject *new_obj )
122 {
123   if ( dlist == NULL )
124   {
125     dlist = new_obj ;
126     new_obj -> next = NULL ;
127     new_obj -> prev = NULL ;
128   }
129   else
130   {
131     puObject *last ;
132
133     for ( last = dlist ; last->next != NULL ; last = last->next )
134       /* Search for end of list. */ ;
135
136     last -> next = new_obj ;
137     new_obj -> prev = last ;
138     new_obj -> next = NULL ;
139   }
140
141   num_children++ ;
142   recalc_bbox () ;
143 }
144
145 int puInterface::checkKey ( int key, int updown )
146 {
147   if ( dlist == NULL || ! isVisible () || ! isActive () )
148     return FALSE ;
149
150   puObject *bo ;
151
152   /*
153     We have to walk the list backwards to ensure that
154     the click order is the same as the DRAW order.
155   */
156
157   for ( bo = dlist ; bo->next != NULL ; bo = bo->next )
158     /* Find the last object in our list. */ ;
159
160   for ( ; bo != NULL ; bo = bo->prev )
161     if ( bo -> checkKey ( key, updown ) )
162       return TRUE ;
163
164   return FALSE ;
165 }
166
167 int puInterface::checkHit ( int button, int updown, int x, int y )
168 {
169   if ( dlist == NULL || ! isVisible () || ! isActive () )
170     return FALSE ;
171
172   /*
173     This might be a bit redundant - but it's too hard to keep
174     track of changing abox sizes when daughter objects are
175     changing sizes.
176   */
177
178   recalc_bbox () ;
179
180   puObject *bo ;
181
182   x -= abox.min[0] ;
183   y -= abox.min[1] ;
184
185   /*
186     We have to walk the list backwards to ensure that
187     the click order is the same as the DRAW order.
188   */
189
190   for ( bo = dlist ; bo->next != NULL ; bo = bo->next )
191     /* Find the last object in our list. */ ;
192
193   for ( ; bo != NULL ; bo = bo->prev )
194     if ( bo -> checkHit ( button, updown, x, y ) )
195       return TRUE ;
196
197   return FALSE ;
198 }
199
200
201 void puInterface::draw ( int dx, int dy )
202 {
203   if ( ! isVisible () )
204     return ;
205
206   for ( puObject *bo = dlist ; bo != NULL ; bo = bo->next )
207   {
208     /* June 16th, 98, Shammi :
209      * The next if statement checks if the object is
210      * a menu bar and makes sure it is repositioned
211      * correctly.
212      */
213
214     if ( bo->getType() & PUCLASS_MENUBAR )
215     {
216       int obWidth, obHeight ;
217       bo -> getSize ( &obWidth, &obHeight ) ;
218       bo -> setPosition ( 0, puGetWindowHeight() - obHeight ) ;
219     }
220
221     bo -> draw ( dx + abox.min[0], dy + abox.min[1] ) ;
222   }
223 }
224
225
226 void puInterface::recalc_bbox ( void ) 
227 {
228   puBox contents ;
229   contents . empty () ;
230
231   for ( puObject *bo = dlist ; bo != NULL ; bo = bo->next )
232     contents . extend ( bo -> getBBox() ) ;
233
234   if ( contents . isEmpty () )
235   {
236     abox . max[0] = abox . min[0] ;
237     abox . max[1] = abox . min[1] ;
238   }
239   else
240   {
241     abox . max[0] = abox . min[0] + contents . max[0] ;
242     abox . max[1] = abox . min[1] + contents . max[1] ;
243   }
244
245   puObject::recalc_bbox () ;
246 }
247
248
249 void puInterface::doHit ( int, int, int, int )
250 {
251 }
252
253
254 puInterface::~puInterface ()
255 {
256     puPopLiveInterface () ;
257
258     puObject *bo = dlist ;
259
260     while ( bo != NULL ) {
261         puObject *tmp_bo = bo->next ;
262         delete bo ;
263         bo = tmp_bo ;
264     }
265 }
266
267
268