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