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