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