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