4274667efe339887404f484c8b14e2c11a5b6a57
[freeglut] / src / fg_structure.c
1 /*
2  * fg_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 #include <GL/freeglut.h>
29 #include "fg_internal.h"
30
31 /* -- GLOBAL EXPORTS ------------------------------------------------------- */
32
33 /*
34  * The SFG_Structure container holds information about windows and menus
35  * created between glutInit() and glutMainLoop() return.
36  */
37
38 SFG_Structure fgStructure = { { NULL, NULL },  /* The list of windows       */
39                               { NULL, NULL },  /* The list of menus         */
40                               { NULL, NULL },  /* Windows to Destroy list   */
41                               NULL,            /* The current window        */
42                               NULL,            /* The current menu          */
43                               NULL,            /* The menu OpenGL context   */
44                               NULL,            /* The game mode window      */
45                               0,               /* The current new window ID */
46                               0 };             /* The current new menu ID   */
47
48
49 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */
50
51 extern void fgPlatformCreateWindow ( SFG_Window *window );
52 extern void fghDefaultReshape(int width, int height, FGCBUserData userData);
53
54 static void fghClearCallBacks( SFG_Window *window )
55 {
56     if( window )
57     {
58         int i;
59         for( i = 0; i < TOTAL_CALLBACKS; ++i )
60         {
61             window->CallBacks[ i ] = NULL;
62             window->CallbackDatas[ i ] = NULL;
63         }
64     }
65 }
66
67 /*
68  * This private function creates, opens and adds to the hierarchy
69  * a freeglut window complete with OpenGL context and stuff...
70  *
71  * If parent is set to NULL, the window created will be a topmost one.
72  */
73 SFG_Window* fgCreateWindow( SFG_Window* parent, const char* title,
74                             GLboolean positionUse, int x, int y,
75                             GLboolean sizeUse, int w, int h,
76                             GLboolean gameMode, GLboolean isMenu )
77 {
78     /* Have the window object created */
79     SFG_Window *window = (SFG_Window *)calloc( 1, sizeof(SFG_Window) );
80
81     if( !window )
82     {
83         fgError( "Out of memory. Could not create window." );
84     }
85
86         fgPlatformCreateWindow ( window );
87
88     fghClearCallBacks( window );
89     SET_WCB( *window, Reshape, fghDefaultReshape, NULL);
90
91     /* Initialize the object properties */
92     window->ID = ++fgStructure.WindowID;
93
94     fgListInit( &window->Children );
95     if( parent )
96     {
97         fgListAppend( &parent->Children, &window->Node );
98         window->Parent = parent;
99     }
100     else
101         fgListAppend( &fgStructure.Windows, &window->Node );
102
103     /* Set the default mouse cursor */
104     window->State.Cursor    = GLUT_CURSOR_INHERIT;
105
106     /* Mark window as menu if a menu is to be created */
107     window->IsMenu          = isMenu;
108
109     /*
110      * Open the window now. The fgOpenWindow() function is system
111      * dependent, and resides in fg_window.c. Uses fgState.
112      */
113     fgOpenWindow( window, title, positionUse, x, y, sizeUse, w, h, gameMode,
114                   (GLboolean)(parent ? GL_TRUE : GL_FALSE) );
115
116     return window;
117 }
118
119 /*
120  * This private function creates a menu and adds it to the menus list
121  */
122 SFG_Menu* fgCreateMenu( FGCBMenuUC menuCallback, FGCBUserData userData )
123 {
124     SFG_Window *current_window = fgStructure.CurrentWindow;
125
126     /* Have the menu object created */
127     SFG_Menu* menu = (SFG_Menu *)calloc( sizeof(SFG_Menu), 1 );
128
129     menu->ParentWindow = NULL;
130
131     /* Create a window for the menu to reside in. */
132     fgCreateWindow( NULL, "freeglut menu", GL_FALSE, 0, 0, GL_FALSE, 0, 0,
133                     GL_FALSE, GL_TRUE );
134     menu->Window = fgStructure.CurrentWindow;
135     glutDisplayFunc( fgDisplayMenu );
136
137     fgSetWindow( current_window );
138
139     /* Initialize the object properties: */
140     menu->ID           = ++fgStructure.MenuID;
141     menu->Callback     = menuCallback;
142     menu->CallbackData = userData;
143     menu->ActiveEntry  = NULL;
144     menu->Font         = fgState.MenuFont;
145
146     fgListInit( &menu->Entries );
147     fgListAppend( &fgStructure.Menus, &menu->Node );
148
149     /* Newly created menus implicitly become current ones */
150     fgStructure.CurrentMenu = menu;
151
152     return menu;
153 }
154
155 /*
156  * Function to add a window to the linked list of windows to destroy.
157  * Subwindows are automatically added because they hang from the window
158  * structure.
159  */
160 void fgAddToWindowDestroyList( SFG_Window* window )
161 {
162     SFG_WindowList *new_list_entry =
163         ( SFG_WindowList* )malloc( sizeof(SFG_WindowList ) );
164     new_list_entry->window = window;
165     fgListAppend( &fgStructure.WindowsToDestroy, &new_list_entry->node );
166
167     /* Check if the window is the current one... */
168     if( fgStructure.CurrentWindow == window )
169         fgStructure.CurrentWindow = NULL;
170
171     /*
172      * Clear all window callbacks except Destroy, which will
173      * be invoked later.  Right now, we are potentially carrying
174      * out a freeglut operation at the behest of a client callback,
175      * so we are reluctant to re-enter the client with the Destroy
176      * callback, right now.  The others are all wiped out, however,
177      * to ensure that they are no longer called after this point.
178      */
179     {
180         FGCBDestroyUC destroy = (FGCBDestroyUC)FETCH_WCB( *window, Destroy );
181         FGCBUserData destroyData = FETCH_USER_DATA_WCB( *window, Destroy );
182         fghClearCallBacks( window );
183         SET_WCB( *window, Destroy, destroy, destroyData );
184     }
185 }
186
187 /*
188  * Function to close down all the windows in the "WindowsToDestroy" list
189  */
190 void fgCloseWindows( )
191 {
192     while( fgStructure.WindowsToDestroy.First )
193     {
194         SFG_WindowList *window_ptr = fgStructure.WindowsToDestroy.First;
195         fgDestroyWindow( window_ptr->window );
196         fgListRemove( &fgStructure.WindowsToDestroy, &window_ptr->node );
197         free( window_ptr );
198     }
199 }
200
201 /*
202  * This function destroys a window and all of its subwindows. Actually,
203  * another function, defined in fg_window.c is called, but this is
204  * a whole different story...
205  */
206 void fgDestroyWindow( SFG_Window* window )
207 {
208     FREEGLUT_INTERNAL_ERROR_EXIT ( window, "Window destroy function called with null window",
209                                    "fgDestroyWindow" );
210
211     while( window->Children.First )
212         fgDestroyWindow( ( SFG_Window * )window->Children.First );
213
214     {
215         SFG_Window *activeWindow = fgStructure.CurrentWindow;
216         INVOKE_WCB( *window, Destroy, ( ) );
217         fgSetWindow( activeWindow );
218     }
219
220     if( window->Parent )
221         fgListRemove( &window->Parent->Children, &window->Node );
222     else
223         fgListRemove( &fgStructure.Windows, &window->Node );
224
225     if( window->ActiveMenu )
226       fgDeactivateMenu( window );
227
228     fghClearCallBacks( window );
229     fgCloseWindow( window );
230     free( window );
231     if( fgStructure.CurrentWindow == window )
232         fgStructure.CurrentWindow = NULL;
233 }
234
235 /*
236  * This is a helper static function that removes a menu (given its pointer)
237  * from any windows that can be accessed from a given parent...
238  */
239 static void fghRemoveMenuFromWindow( SFG_Window* window, SFG_Menu* menu )
240 {
241     SFG_Window *subWindow;
242     int i;
243
244     /* Check whether this is the active menu in the window */
245     if ( menu == window->ActiveMenu )
246         window->ActiveMenu = NULL ;
247
248     /*
249      * Check if the menu is attached to the current window,
250      * if so, have it detached (by overwriting with a NULL):
251      */
252     for( i = 0; i < FREEGLUT_MAX_MENUS; i++ )
253         if( window->Menu[ i ] == menu )
254             window->Menu[ i ] = NULL;
255
256     /* Call this function for all of the window's children recursively: */
257     for( subWindow = (SFG_Window *)window->Children.First;
258          subWindow;
259          subWindow = (SFG_Window *)subWindow->Node.Next)
260         fghRemoveMenuFromWindow( subWindow, menu );
261 }
262
263 /*
264  * This is a static helper function that removes menu references
265  * from another menu, given two pointers to them...
266  */
267 static void fghRemoveMenuFromMenu( SFG_Menu* from, SFG_Menu* menu )
268 {
269     SFG_MenuEntry *entry;
270
271     for( entry = (SFG_MenuEntry *)from->Entries.First;
272          entry;
273          entry = ( SFG_MenuEntry * )entry->Node.Next )
274         if( entry->SubMenu == menu )
275             entry->SubMenu = NULL;
276 }
277
278 /*
279  * This function destroys a menu specified by the parameter. All menus
280  * and windows are updated to make sure no ill pointers hang around.
281  */
282 void fgDestroyMenu( SFG_Menu* menu )
283 {
284     SFG_Window *window;
285     SFG_Menu *from;
286
287     FREEGLUT_INTERNAL_ERROR_EXIT ( menu, "Menu destroy function called with null menu",
288                                    "fgDestroyMenu" );
289
290     /* First of all, have all references to this menu removed from all windows: */
291     for( window = (SFG_Window *)fgStructure.Windows.First;
292          window;
293          window = (SFG_Window *)window->Node.Next )
294         fghRemoveMenuFromWindow( window, menu );
295
296     /* Now proceed with removing menu entries that lead to this menu */
297     for( from = ( SFG_Menu * )fgStructure.Menus.First;
298          from;
299          from = ( SFG_Menu * )from->Node.Next )
300         fghRemoveMenuFromMenu( from, menu );
301
302     /*
303      * If the programmer defined a destroy callback, call it
304      * A. Donev: But first make this the active menu
305      */
306     if( menu->Destroy )
307     {
308         SFG_Menu *activeMenu=fgStructure.CurrentMenu;
309         fgStructure.CurrentMenu = menu;
310         menu->Destroy( menu->DestroyData );
311         fgStructure.CurrentMenu = activeMenu;
312     }
313
314     /*
315      * Now we are pretty sure the menu is not used anywhere
316      * and that we can remove all of its entries
317      */
318     while( menu->Entries.First )
319     {
320         SFG_MenuEntry *entry = ( SFG_MenuEntry * ) menu->Entries.First;
321
322         fgListRemove( &menu->Entries, &entry->Node );
323
324         if( entry->Text )
325             free( entry->Text );
326         entry->Text = NULL;
327
328         free( entry );
329     }
330
331     if( fgStructure.CurrentWindow == menu->Window )
332         fgSetWindow( NULL );
333     fgDestroyWindow( menu->Window );
334     fgListRemove( &fgStructure.Menus, &menu->Node );
335     if( fgStructure.CurrentMenu == menu )
336         fgStructure.CurrentMenu = NULL;
337
338     free( menu );
339 }
340
341 /*
342  * This function should be called on glutInit(). It will prepare the internal
343  * structure of freeglut to be used in the application. The structure will be
344  * destroyed using fgDestroyStructure() on glutMainLoop() return. In that
345  * case further use of freeglut should be preceded with a glutInit() call.
346  */
347 void fgCreateStructure( void )
348 {
349     /*
350      * We will be needing two lists: the first containing windows,
351      * and the second containing the user-defined menus.
352      * Also, no current window/menu is set, as none has been created yet.
353      */
354
355     fgListInit(&fgStructure.Windows);
356     fgListInit(&fgStructure.Menus);
357     fgListInit(&fgStructure.WindowsToDestroy);
358
359     fgStructure.CurrentWindow = NULL;
360     fgStructure.CurrentMenu = NULL;
361     fgStructure.MenuContext = NULL;
362     fgStructure.GameModeWindow = NULL;
363     fgStructure.WindowID = 0;
364     fgStructure.MenuID = 0;
365 }
366
367 /*
368  * This function is automatically called on glutMainLoop() return.
369  * It should deallocate and destroy all remnants of previous
370  * glutInit()-enforced structure initialization...
371  */
372 void fgDestroyStructure( void )
373 {
374     /* Clean up the WindowsToDestroy list. */
375     fgCloseWindows( );
376
377     /* Make sure all windows and menus have been deallocated */
378     while( fgStructure.Menus.First )
379         fgDestroyMenu( ( SFG_Menu * )fgStructure.Menus.First );
380
381     while( fgStructure.Windows.First )
382         fgDestroyWindow( ( SFG_Window * )fgStructure.Windows.First );
383 }
384
385 /*
386  * Helper function to enumerate through all registered top-level windows
387  */
388 void fgEnumWindows( FGCBWindowEnumerator enumCallback, SFG_Enumerator* enumerator )
389 {
390     SFG_Window *window;
391
392     FREEGLUT_INTERNAL_ERROR_EXIT ( enumCallback && enumerator,
393                                    "Enumerator or callback missing from window enumerator call",
394                                    "fgEnumWindows" );
395
396     /* Check every of the top-level windows */
397     for( window = ( SFG_Window * )fgStructure.Windows.First;
398          window;
399          window = ( SFG_Window * )window->Node.Next )
400     {
401         enumCallback( window, enumerator );
402         if( enumerator->found )
403             return;
404     }
405 }
406
407 /*
408 * Helper function to enumerate through all registered top-level windows
409 */
410 void fgEnumMenus( FGCBMenuEnumerator enumCallback, SFG_Enumerator* enumerator )
411 {
412     SFG_Menu *menu;
413
414     FREEGLUT_INTERNAL_ERROR_EXIT ( enumCallback && enumerator,
415         "Enumerator or callback missing from window enumerator call",
416         "fgEnumWindows" );
417
418     /* It's enough to check all entries in fgStructure.Menus... */
419     for( menu = (SFG_Menu *)fgStructure.Menus.First;
420         menu;
421         menu = (SFG_Menu *)menu->Node.Next )
422     {
423         enumCallback( menu, enumerator );
424         if( enumerator->found )
425             return;
426     }
427 }
428
429 /*
430  * Helper function to enumerate through all a window's subwindows
431  * (single level descent)
432  */
433 void fgEnumSubWindows( SFG_Window* window, FGCBWindowEnumerator enumCallback,
434                        SFG_Enumerator* enumerator )
435 {
436     SFG_Window *child;
437
438     FREEGLUT_INTERNAL_ERROR_EXIT ( enumCallback && enumerator,
439                                    "Enumerator or callback missing from subwindow enumerator call",
440                                    "fgEnumSubWindows" );
441     FREEGLUT_INTERNAL_ERROR_EXIT_IF_NOT_INITIALISED ( "Window Enumeration" );
442
443     for( child = ( SFG_Window * )window->Children.First;
444          child;
445          child = ( SFG_Window * )child->Node.Next )
446     {
447         enumCallback( child, enumerator );
448         if( enumerator->found )
449             return;
450     }
451 }
452
453 /*
454  * A static helper function to look for a window given its handle
455  */
456 static void fghcbWindowByHandle( SFG_Window *window,
457                                  SFG_Enumerator *enumerator )
458 {
459     if ( enumerator->found )
460         return;
461
462     /* Check the window's handle. Hope this works. Looks ugly. That's for sure. */
463     if( window->Window.Handle == (SFG_WindowHandleType) (enumerator->data) )
464     {
465         enumerator->found = GL_TRUE;
466         enumerator->data = window;
467
468         return;
469     }
470
471     /* Otherwise, check this window's children */
472     fgEnumSubWindows( window, fghcbWindowByHandle, enumerator );
473 }
474
475 /*
476  * fgWindowByHandle returns a (SFG_Window *) value pointing to the
477  * first window in the queue matching the specified window handle.
478  * The function is defined in fg_structure.c file.
479  */
480 SFG_Window* fgWindowByHandle ( SFG_WindowHandleType hWindow )
481 {
482     SFG_Enumerator enumerator;
483
484     /* This is easy and makes use of the windows enumeration defined above */
485     enumerator.found = GL_FALSE;
486     enumerator.data = (void *)hWindow;
487     fgEnumWindows( fghcbWindowByHandle, &enumerator );
488
489     if( enumerator.found )
490         return( SFG_Window *) enumerator.data;
491     return NULL;
492 }
493
494 /*
495  * A static helper function to look for a window given its ID
496  */
497 static void fghcbWindowByID( SFG_Window *window, SFG_Enumerator *enumerator )
498 {
499     /* Make sure we do not overwrite our precious results... */
500     if( enumerator->found )
501         return;
502
503     /* Check the window's handle. Hope this works. Looks ugly. That's for sure. */
504     if( window->ID == *( int *)(enumerator->data) )
505     {
506         enumerator->found = GL_TRUE;
507         enumerator->data = window;
508
509         return;
510     }
511
512     /* Otherwise, check this window's children */
513     fgEnumSubWindows( window, fghcbWindowByID, enumerator );
514 }
515
516 /*
517  * This function is similar to the previous one, except it is
518  * looking for a specified (sub)window identifier. The function
519  * is defined in fg_structure.c file.
520  */
521 SFG_Window* fgWindowByID( int windowID )
522 {
523     SFG_Enumerator enumerator;
524
525     /* Uses a method very similar for fgWindowByHandle... */
526     enumerator.found = GL_FALSE;
527     enumerator.data = ( void * )&windowID;
528     fgEnumWindows( fghcbWindowByID, &enumerator );
529     if( enumerator.found )
530         return ( SFG_Window * )enumerator.data;
531     return NULL;
532 }
533
534 /*
535  * A static helper function to look for a menu given its ID
536  */
537 static void fghcbMenuByID( SFG_Menu *menu,
538     SFG_Enumerator *enumerator )
539 {
540     if ( enumerator->found )
541         return;
542
543     /* Check the menu's ID. */
544     if( menu->ID == *( int *)(enumerator->data) )
545     {
546         enumerator->found = GL_TRUE;
547         enumerator->data = menu;
548
549         return;
550     }
551 }
552
553 /*
554  * Looks up a menu given its ID. This is easier than fgWindowByXXX
555  * as all menus are placed in one doubly linked list...
556  */
557 SFG_Menu* fgMenuByID( int menuID )
558 {
559     SFG_Enumerator enumerator;
560
561     /* This is easy and makes use of the menus enumeration defined above */
562     enumerator.found = GL_FALSE;
563     enumerator.data = (void *)&menuID;
564     fgEnumMenus( fghcbMenuByID, &enumerator );
565
566     if( enumerator.found )
567         return( SFG_Menu *) enumerator.data;
568
569     return NULL;
570 }
571
572 /*
573  * A static helper function to look for an active menu
574  */
575 static void fghcbGetActiveMenu( SFG_Menu *menu,
576     SFG_Enumerator *enumerator )
577 {
578     if ( enumerator->found )
579         return;
580
581     /* Check the menu's is active */
582     if( menu->IsActive )
583     {
584         enumerator->found = GL_TRUE;
585         enumerator->data = menu;
586
587         return;
588     }
589 }
590
591 /*
592  * Returns active menu, if any. Assumption: only one menu active throughout application at any one time.
593  * This is false when a submenu is also open.
594  * This is easier than fgWindowByXXX as all menus are placed in one doubly linked list...
595  */
596 SFG_Menu* fgGetActiveMenu( )
597 {
598     SFG_Enumerator enumerator;
599
600     /* This is easy and makes use of the menus enumeration defined above */
601     enumerator.found = GL_FALSE;
602     fgEnumMenus( fghcbGetActiveMenu, &enumerator );
603
604     if( enumerator.found )
605         return( SFG_Menu *) enumerator.data;
606
607     return NULL;
608 }
609
610 /*
611  * List functions...
612  */
613 void fgListInit(SFG_List *list)
614 {
615     list->First = NULL;
616     list->Last = NULL;
617 }
618
619 void fgListAppend(SFG_List *list, SFG_Node *node)
620 {
621     if ( list->Last )
622     {
623         SFG_Node *ln = (SFG_Node *) list->Last;
624         ln->Next = node;
625         node->Prev = ln;
626     }
627     else
628     {
629         node->Prev = NULL;
630         list->First = node;
631     }
632
633     node->Next = NULL;
634     list->Last = node;
635 }
636
637 void fgListRemove(SFG_List *list, SFG_Node *node)
638 {
639     if( node->Next )
640         ( ( SFG_Node * )node->Next )->Prev = node->Prev;
641     if( node->Prev )
642         ( ( SFG_Node * )node->Prev )->Next = node->Next;
643     if( ( ( SFG_Node * )list->First ) == node )
644         list->First = node->Next;
645     if( ( ( SFG_Node * )list->Last ) == node )
646         list->Last = node->Prev;
647 }
648
649 int fgListLength(SFG_List *list)
650 {
651     SFG_Node *node;
652     int length = 0;
653
654     for( node =( SFG_Node * )list->First;
655          node;
656          node = ( SFG_Node * )node->Next )
657         ++length;
658
659     return length;
660 }
661
662
663 void fgListInsert(SFG_List *list, SFG_Node *next, SFG_Node *node)
664 {
665     SFG_Node *prev;
666
667     if( (node->Next = next) )
668     {
669         prev = next->Prev;
670         next->Prev = node;
671     }
672     else
673     {
674         prev = list->Last;
675         list->Last = node;
676     }
677
678     if( (node->Prev = prev) )
679         prev->Next = node;
680     else
681         list->First = node;
682 }
683
684 /*** END OF FILE ***/