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