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