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