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