1 /*
2 * freeglut_structure.c
3 *
4 * Windows and menus need tree structure
5 *
6 * Copyright (c) 1999-2000 Pawel W. Olszta. All Rights Reserved.
7 * Written by Pawel W. Olszta, <olszta@sourceforge.net>
8 * Creation date: Sat Dec 18 1999
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining a
11 * copy of this software and associated documentation files (the "Software"),
12 * to deal in the Software without restriction, including without limitation
13 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
14 * and/or sell copies of the Software, and to permit persons to whom the
15 * Software is furnished to do so, subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be included
18 * in all copies or substantial portions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 * PAWEL W. OLSZTA BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
24 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
25 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 */
27
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31
32 #include "../include/GL/freeglut.h"
33 #include "freeglut_internal.h"
34
35
36 /* -- GLOBAL EXPORTS ------------------------------------------------------- */
37
38 /*
39 * The SFG_Structure container holds information about windows and menus
40 * created between glutInit() and glutMainLoop() return.
41 */
42
43 SFG_Structure fgStructure = { { NULL, NULL }, /* The list of windows */
44 { NULL, NULL }, /* The list of menus */
45 NULL, /* The current window */
46 NULL, /* The current menu */
47 NULL, /* The menu OpenGL context */
48 NULL, /* The game mode window */
49 0, /* The current new window ID */
50 0 }; /* The current new menu ID */
51
52
53 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */
54
fgClearCallBacks(SFG_Window * window)55 void fgClearCallBacks( SFG_Window *window )
56 {
57 if( window )
58 {
59 int i;
60 for( i = 0; i < TOTAL_CALLBACKS; ++i )
61 window->CallBacks[ i ] = NULL;
62 }
63 }
64
65 /*
66 * This private function creates, opens and adds to the hierarchy
67 * a freeglut window complete with OpenGL context and stuff...
68 *
69 * If parent is set to NULL, the window created will be a topmost one.
70 */
fgCreateWindow(SFG_Window * parent,const char * title,int x,int y,int w,int h,GLboolean gameMode,GLboolean isMenu)71 SFG_Window* fgCreateWindow( SFG_Window* parent, const char* title,
72 int x, int y, int w, int h,
73 GLboolean gameMode, GLboolean isMenu )
74 {
75 /*
76 * Have the window object created
77 */
78 SFG_Window *window = (SFG_Window *)calloc( sizeof(SFG_Window), 1 );
79 int fakeArgc = 0;
80
81 fgClearCallBacks( window );
82
83 /*
84 * If the freeglut internals haven't been initialized yet,
85 * do it now. Hack's idea courtesy of Chris Purnell...
86 */
87 if( !fgState.Initialised )
88 glutInit( &fakeArgc, NULL );
89
90 /*
91 * Initialize the object properties
92 */
93 window->ID = ++fgStructure.WindowID;
94 window->State.OldHeight = window->State.OldWidth = -1;
95
96 fgListInit( &window->Children );
97 if( parent )
98 {
99 fgListAppend( &parent->Children, &window->Node );
100 window->Parent = parent;
101 }
102 else
103 fgListAppend( &fgStructure.Windows, &window->Node );
104
105 /*
106 * Set the default mouse cursor and reset the modifiers value
107 */
108 window->State.Cursor = GLUT_CURSOR_INHERIT;
109
110 window->IsMenu = isMenu;
111
112 /*
113 * Open the window now. The fgOpenWindow() function is system
114 * dependant, and resides in freeglut_window.c. Uses fgState.
115 */
116 fgOpenWindow( window, title, x, y, w, h, gameMode,
117 parent ? GL_TRUE : GL_FALSE );
118
119 return window;
120 }
121
122 /*
123 * This private function creates a menu and adds it to the menus list
124 */
fgCreateMenu(FGCBMenu menuCallback)125 SFG_Menu* fgCreateMenu( FGCBMenu menuCallback )
126 {
127 int x = 100, y = 100, w = 100, h = 100;
128 SFG_Window *current_window = fgStructure.Window;
129
130 /*
131 * Have the menu object created
132 */
133 SFG_Menu* menu = (SFG_Menu *)calloc( sizeof(SFG_Menu), 1 );
134 int fakeArgc = 0;
135
136 /*
137 * If the freeglut internals haven't been initialized yet,
138 * do it now. Hack's idea courtesy of Chris Purnell...
139 */
140 if( !fgState.Initialised )
141 glutInit( &fakeArgc, NULL );
142
143 menu->ParentWindow = fgStructure.Window;
144
145 /*
146 * Create a window for the menu to reside in.
147 */
148
149 fgCreateWindow( NULL, NULL, x, y, w, h, GL_FALSE, GL_TRUE );
150 menu->Window = fgStructure.Window;
151 glutDisplayFunc( fgDisplayMenu );
152
153 glutHideWindow( ); /* Hide the window for now */
154 fgSetWindow( current_window );
155
156 /*
157 * Initialize the object properties:
158 */
159 menu->ID = ++fgStructure.MenuID;
160 menu->Callback = menuCallback;
161 menu->ActiveEntry = NULL;
162
163 fgListInit( &menu->Entries );
164 fgListAppend( &fgStructure.Menus, &menu->Node );
165
166 /*
167 * Newly created menus implicitly become current ones
168 */
169 fgStructure.Menu = menu;
170
171 return menu;
172 }
173
174 /*
175 * Linked list of windows to destroy ... this is so we don't destroy a
176 * window from the middle of its callback. Some C compilers take an
177 * extremely dim view of this.
178 */
179
180 static SFG_WindowList* WindowsToDestroy = ( SFG_WindowList* )NULL;
181
182 /*
183 * Function to add a window to the linked list of windows to destroy.
184 * Subwindows are automatically added because they hang from the window
185 * structure.
186 */
fgAddToWindowDestroyList(SFG_Window * window)187 void fgAddToWindowDestroyList( SFG_Window* window )
188 {
189 SFG_WindowList *new_list_entry =
190 ( SFG_WindowList* )malloc( sizeof(SFG_WindowList ) );
191 new_list_entry->window = window;
192 new_list_entry->next = WindowsToDestroy;
193 WindowsToDestroy = new_list_entry;
194
195 /*
196 * Check if the window is the current one...
197 */
198 if( fgStructure.Window == window )
199 fgStructure.Window = NULL;
200
201 /*
202 * Clear all window callbacks except Destroy, which will
203 * be invoked later. Right now, we are potentially carrying
204 * out a freeglut operation at the behest of a client callback,
205 * so we are reluctant to re-enter the client with the Destroy
206 * callback, right now. The others are all wiped out, however,
207 * to ensure that they are no longer called after this point.
208 */
209 {
210 void *destroy = FETCH_WCB( *window, Destroy );
211 fgClearCallBacks( window );
212 FETCH_WCB( *window, Destroy ) = destroy;
213 }
214 }
215
216 /*
217 * Function to close down all the windows in the "WindowsToDestroy" list
218 */
fgCloseWindows()219 void fgCloseWindows( )
220 {
221 SFG_WindowList *window_ptr = WindowsToDestroy;
222 WindowsToDestroy = ( SFG_WindowList* )NULL;
223 /* In case the destroy callbacks cause more windows to be closed */
224
225 while( window_ptr )
226 {
227 SFG_WindowList *next = window_ptr->next;
228 fgDestroyWindow( window_ptr->window );
229 free( window_ptr );
230 window_ptr = next;
231
232 if( !window_ptr )
233 {
234 window_ptr = WindowsToDestroy;
235 WindowsToDestroy = ( SFG_WindowList* )NULL;
236 }
237 }
238 }
239
240 /*
241 * This function destroys a window and all of its subwindows. Actually,
242 * another function, defined in freeglut_window.c is called, but this is
243 * a whole different story...
244 */
fgDestroyWindow(SFG_Window * window)245 void fgDestroyWindow( SFG_Window* window )
246 {
247 SFG_Window* subWindow;
248 int menu_index ;
249
250 assert( window );
251 freeglut_assert_ready;
252
253 while( subWindow = ( SFG_Window * )window->Children.First )
254 fgDestroyWindow( subWindow );
255
256 {
257 SFG_Window *activeWindow = fgStructure.Window ;
258 INVOKE_WCB( *window, Destroy, ( ) );
259 fgSetWindow ( activeWindow );
260 }
261
262 if( window->Parent )
263 fgListRemove( &window->Parent->Children, &window->Node );
264 else
265 fgListRemove( &fgStructure.Windows, &window->Node );
266
267 if( window->ActiveMenu )
268 fgDeactivateMenu( window );
269
270 for ( menu_index = 0; menu_index < 3; menu_index ++ )
271 {
272 if ( window->Menu[menu_index] )
273 window->Menu[menu_index]->ParentWindow = NULL ;
274 }
275
276 fgClearCallBacks( window );
277 fgCloseWindow( window );
278 free( window );
279 if( fgStructure.Window == window )
280 fgStructure.Window = NULL;
281 }
282
283 /*
284 * This is a helper static function that removes a menu (given its pointer)
285 * from any windows that can be accessed from a given parent...
286 */
fghRemoveMenuFromWindow(SFG_Window * window,SFG_Menu * menu)287 static void fghRemoveMenuFromWindow( SFG_Window* window, SFG_Menu* menu )
288 {
289 SFG_Window *subWindow;
290 int i;
291
292 /*
293 * Check if the menu is attached to the current window,
294 * if so, have it detached (by overwriting with a NULL):
295 */
296 for( i = 0; i < 3; i++ )
297 if( window->Menu[ i ] == menu )
298 window->Menu[ i ] = NULL;
299
300 /*
301 * Call this function for all of the window's children recursively:
302 */
303 for( subWindow = (SFG_Window *)window->Children.First;
304 subWindow;
305 subWindow = (SFG_Window *)subWindow->Node.Next)
306 fghRemoveMenuFromWindow( subWindow, menu );
307 }
308
309 /*
310 * This is a static helper function that removes menu references
311 * from another menu, given two pointers to them...
312 */
fghRemoveMenuFromMenu(SFG_Menu * from,SFG_Menu * menu)313 static void fghRemoveMenuFromMenu( SFG_Menu* from, SFG_Menu* menu )
314 {
315 SFG_MenuEntry *entry;
316
317 for( entry = (SFG_MenuEntry *)from->Entries.First;
318 entry;
319 entry = ( SFG_MenuEntry * )entry->Node.Next )
320 if( entry->SubMenu == menu )
321 entry->SubMenu = NULL;
322 }
323
324 /*
325 * This function destroys a menu specified by the parameter. All menus
326 * and windows are updated to make sure no ill pointers hang around.
327 */
fgDestroyMenu(SFG_Menu * menu)328 void fgDestroyMenu( SFG_Menu* menu )
329 {
330 SFG_Window *window;
331 SFG_Menu *from;
332 SFG_MenuEntry *entry;
333
334 assert( menu );
335 freeglut_assert_ready;
336
337 /*
338 * First of all, have all references to this menu removed from all windows:
339 */
340 for( window = (SFG_Window *)fgStructure.Windows.First;
341 window;
342 window = (SFG_Window *)window->Node.Next )
343 fghRemoveMenuFromWindow( window, menu );
344
345 /*
346 * Now proceed with removing menu entries that lead to this menu
347 */
348 for( from = ( SFG_Menu * )fgStructure.Menus.First;
349 from;
350 from = ( SFG_Menu * )from->Node.Next )
351 fghRemoveMenuFromMenu( from, menu );
352
353 /*
354 * If the programmer defined a destroy callback, call it
355 * A. Donev: But first make this the active menu
356 */
357 if( menu->Destroy )
358 {
359 SFG_Menu *activeMenu=fgStructure.Menu;
360 fgStructure.Menu = menu;
361 menu->Destroy( );
362 fgStructure.Menu = activeMenu;
363 }
364
365 /*
366 * Now we are pretty sure the menu is not used anywhere
367 * and that we can remove all of its entries
368 */
369 while( entry = ( SFG_MenuEntry * )menu->Entries.First )
370 {
371 fgListRemove( &menu->Entries, &entry->Node );
372
373 if( entry->Text )
374 free( entry->Text );
375 entry->Text = NULL;
376
377 free( entry );
378 entry = NULL;
379 }
380
381 if( fgStructure.Window == menu->Window )
382 fgSetWindow( menu->ParentWindow );
383 fgDestroyWindow( menu->Window );
384 fgListRemove( &fgStructure.Menus, &menu->Node );
385 if( fgStructure.Menu == menu )
386 fgStructure.Menu = NULL;
387
388 free( menu );
389 }
390
391 /*
392 * This function should be called on glutInit(). It will prepare the internal
393 * structure of freeglut to be used in the application. The structure will be
394 * destroyed using fgDestroyStructure() on glutMainLoop() return. In that
395 * case further use of freeglut should be preceeded with a glutInit() call.
396 */
fgCreateStructure(void)397 void fgCreateStructure( void )
398 {
399 /*
400 * We will be needing two lists: the first containing windows,
401 * and the second containing the user-defined menus.
402 * Also, no current window/menu is set, as none has been created yet.
403 */
404
405 fgListInit(&fgStructure.Windows);
406 fgListInit(&fgStructure.Menus);
407 }
408
409 /*
410 * This function is automatically called on glutMainLoop() return.
411 * It should deallocate and destroy all remnants of previous
412 * glutInit()-enforced structure initialization...
413 */
fgDestroyStructure(void)414 void fgDestroyStructure( void )
415 {
416 SFG_Window *window;
417 SFG_Menu *menu;
418
419 freeglut_assert_ready;
420
421 /*
422 * Make sure all windows and menus have been deallocated
423 */
424 while( menu = ( SFG_Menu * )fgStructure.Menus.First )
425 fgDestroyMenu( menu );
426
427 while( window = ( SFG_Window * )fgStructure.Windows.First )
428 fgDestroyWindow( window );
429 }
430
431 /*
432 * Helper function to enumerate through all registered top-level windows
433 */
fgEnumWindows(FGCBenumerator enumCallback,SFG_Enumerator * enumerator)434 void fgEnumWindows( FGCBenumerator enumCallback, SFG_Enumerator* enumerator )
435 {
436 SFG_Window *window;
437
438 assert( enumCallback && enumerator );
439 freeglut_assert_ready;
440
441 /*
442 * Check every of the top-level windows
443 */
444 for( window = ( SFG_Window * )fgStructure.Windows.First;
445 window;
446 window = ( SFG_Window * )window->Node.Next )
447 {
448 enumCallback( window, enumerator );
449 if( enumerator->found )
450 return;
451 }
452 }
453
454 /*
455 * Helper function to enumerate through all a window's subwindows
456 * (single level descent)
457 */
fgEnumSubWindows(SFG_Window * window,FGCBenumerator enumCallback,SFG_Enumerator * enumerator)458 void fgEnumSubWindows( SFG_Window* window, FGCBenumerator enumCallback,
459 SFG_Enumerator* enumerator )
460 {
461 SFG_Window *child;
462
463 assert( enumCallback && enumerator );
464 freeglut_assert_ready;
465
466 for( child = ( SFG_Window * )window->Children.First;
467 child;
468 child = ( SFG_Window * )child->Node.Next )
469 {
470 enumCallback( child, enumerator );
471 if( enumerator->found )
472 return;
473 }
474 }
475
476 /*
477 * A static helper function to look for a window given its handle
478 */
fghcbWindowByHandle(SFG_Window * window,SFG_Enumerator * enumerator)479 static void fghcbWindowByHandle( SFG_Window *window,
480 SFG_Enumerator *enumerator )
481 {
482 if ( enumerator->found )
483 return;
484
485 /*
486 * Check the window's handle. Hope this works. Looks ugly. That's for sure.
487 */
488 if( window->Window.Handle == (SFG_WindowHandleType) (enumerator->data) )
489 {
490 enumerator->found = GL_TRUE;
491 enumerator->data = window;
492
493 return;
494 }
495
496 /*
497 * Otherwise, check this window's children
498 */
499 fgEnumSubWindows( window, fghcbWindowByHandle, enumerator );
500 }
501
502 /*
503 * fgWindowByHandle returns a (SFG_Window *) value pointing to the
504 * first window in the queue matching the specified window handle.
505 * The function is defined in freeglut_structure.c file.
506 */
fgWindowByHandle(SFG_WindowHandleType hWindow)507 SFG_Window* fgWindowByHandle ( SFG_WindowHandleType hWindow )
508 {
509 SFG_Enumerator enumerator;
510
511 /*
512 * This is easy and makes use of the windows enumeration defined above
513 */
514 enumerator.found = GL_FALSE;
515 enumerator.data = (void *)hWindow;
516 fgEnumWindows( fghcbWindowByHandle, &enumerator );
517
518 if( enumerator.found )
519 return( SFG_Window *) enumerator.data;
520 return NULL;
521 }
522
523 /*
524 * A static helper function to look for a window given its ID
525 */
fghcbWindowByID(SFG_Window * window,SFG_Enumerator * enumerator)526 static void fghcbWindowByID( SFG_Window *window, SFG_Enumerator *enumerator )
527 {
528 /*
529 * Make sure we do not overwrite our precious results...
530 */
531 if ( enumerator->found )
532 return;
533
534 /*
535 * Check the window's handle. Hope this works. Looks ugly. That's for sure.
536 */
537 if( window->ID == (int) (enumerator->data) ) /* XXX int/ptr conversion! */
538 {
539 enumerator->found = GL_TRUE;
540 enumerator->data = window;
541
542 return;
543 }
544
545 /*
546 * Otherwise, check this window's children
547 */
548 fgEnumSubWindows( window, fghcbWindowByID, enumerator );
549 }
550
551 /*
552 * This function is similiar to the previous one, except it is
553 * looking for a specified (sub)window identifier. The function
554 * is defined in freeglut_structure.c file.
555 */
fgWindowByID(int windowID)556 SFG_Window* fgWindowByID( int windowID )
557 {
558 SFG_Enumerator enumerator;
559
560 /*
561 * Uses a method very similiar for fgWindowByHandle...
562 */
563 enumerator.found = GL_FALSE;
564 enumerator.data = (void *) windowID; /* XXX int/pointer conversion! */
565 fgEnumWindows( fghcbWindowByID, &enumerator );
566 if( enumerator.found )
567 return( SFG_Window *) enumerator.data;
568 return NULL;
569 }
570
571 /*
572 * Looks up a menu given its ID. This is easier that fgWindowByXXX
573 * as all menus are placed in a single doubly linked list...
574 */
fgMenuByID(int menuID)575 SFG_Menu* fgMenuByID( int menuID )
576 {
577 SFG_Menu *menu = NULL;
578
579 freeglut_assert_ready;
580
581 /*
582 * It's enough to check all entries in fgStructure.Menus...
583 */
584 for( menu = (SFG_Menu *)fgStructure.Menus.First;
585 menu;
586 menu = (SFG_Menu *)menu->Node.Next )
587 if( menu->ID == menuID )
588 return menu;
589 return NULL;
590 }
591
592 /*
593 * List functions...
594 */
fgListInit(SFG_List * list)595 void fgListInit(SFG_List *list)
596 {
597 list->First = NULL;
598 list->Last = NULL;
599 }
600
fgListAppend(SFG_List * list,SFG_Node * node)601 void fgListAppend(SFG_List *list, SFG_Node *node)
602 {
603 SFG_Node *ln;
604
605 if ( ln = (SFG_Node *)list->Last )
606 {
607 ln->Next = node;
608 node->Prev = ln;
609 }
610 else
611 {
612 node->Prev = NULL;
613 list->First = node;
614 }
615
616 node->Next = NULL;
617 list->Last = node;
618 }
619
fgListRemove(SFG_List * list,SFG_Node * node)620 void fgListRemove(SFG_List *list, SFG_Node *node)
621 {
622 SFG_Node *ln;
623
624 if( ln = (SFG_Node *)node->Next )
625 ln->Prev = node->Prev;
626 if( ln = (SFG_Node *)node->Prev )
627 ln->Next = node->Next;
628 if( (ln = (SFG_Node *)list->First) == node )
629 list->First = node->Next;
630 if( (ln = (SFG_Node *)list->Last) == node )
631 list->Last = node->Prev;
632 }
633
fgListLength(SFG_List * list)634 int fgListLength(SFG_List *list)
635 {
636 SFG_Node *node;
637 int length = 0;
638
639 for( node =( SFG_Node * )list->First;
640 node;
641 node = ( SFG_Node * )node->Next )
642 ++length;
643
644 return length;
645 }
646
647
fgListInsert(SFG_List * list,SFG_Node * next,SFG_Node * node)648 void fgListInsert(SFG_List *list, SFG_Node *next, SFG_Node *node)
649 {
650 SFG_Node *prev;
651
652 if( (node->Next = next) )
653 {
654 prev = next->Prev;
655 next->Prev = node;
656 }
657 else
658 {
659 prev = list->Last;
660 list->Last = node;
661 }
662
663 if( (node->Prev = prev) )
664 prev->Next = node;
665 else
666 list->First = node;
667 }
668
669 /*** END OF FILE ***/
670