Further normalization of the code's style. No substantial changes.
[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     /*
270      * XXX Since INVOKE_WCB() tests the function pointer, why not make
271      * XXX this unconditional?  Overhead is close to nil, and it would
272      * XXX clarify the code by omitting a conditional test.
273      */
274     if ( FETCH_WCB( *window, Destroy ) )
275     {
276       SFG_Window *activeWindow = fgStructure.Window ;
277       INVOKE_WCB( *window, Destroy, ( ) );
278       fgSetWindow ( activeWindow ) ;
279     }
280
281     if ( window->Parent != NULL )
282         fgListRemove( &window->Parent->Children, &window->Node );
283     else
284         fgListRemove( &fgStructure.Windows, &window->Node );
285
286     if ( window->ActiveMenu != NULL )
287       fgDeactivateMenu ( window ) ;
288
289     for ( menu_index = 0; menu_index < 3; menu_index ++ )
290     {
291       if ( window->Menu[menu_index] != NULL )
292         window->Menu[menu_index]->ParentWindow = NULL ;
293     }
294
295     if( needToClose == TRUE )
296         fgCloseWindow( window );
297     free( window );
298     if ( fgStructure.Window == window )
299         fgStructure.Window = NULL ;
300 }
301
302 /*
303  * This is a helper static function that removes a menu (given its pointer)
304  * from any windows that can be accessed from a given parent...
305  */
306 static void fghRemoveMenuFromWindow( SFG_Window* window, SFG_Menu* menu )
307 {
308     SFG_Window *subWindow;
309     int i;
310
311     /*
312      * Check if the menu is attached to the current window,
313      * if so, have it detached (by overwriting with a NULL):
314      */
315     for( i=0; i<3; i++ )
316         if( window->Menu[ i ] == menu )
317             window->Menu[ i ] = NULL;
318
319     /*
320      * Call this function for all of the window's children recursively:
321      */
322     for( subWindow = (SFG_Window *)window->Children.First; subWindow;
323          subWindow = (SFG_Window *)subWindow->Node.Next)
324         fghRemoveMenuFromWindow( subWindow, menu );
325 }
326
327 /*
328  * This is a static helper function that removes menu references
329  * from another menu, given two pointers to them...
330  */
331 static void fghRemoveMenuFromMenu( SFG_Menu* from, SFG_Menu* menu )
332 {
333   SFG_MenuEntry *entry;
334
335   for( entry = (SFG_MenuEntry *)from->Entries.First;
336        entry;
337        entry = (SFG_MenuEntry *)entry->Node.Next )
338     if (entry->SubMenu == menu)
339       entry->SubMenu = NULL;
340 }
341
342 /*
343  * This function destroys a menu specified by the parameter. All menus
344  * and windows are updated to make sure no ill pointers hang around.
345  */
346 void fgDestroyMenu( SFG_Menu* menu )
347 {
348   SFG_Window *window;
349   SFG_Menu *from;
350   SFG_MenuEntry *entry;
351
352   assert( menu != NULL );
353   freeglut_assert_ready;
354
355   /*
356    * First of all, have all references to this menu removed from all windows:
357    */
358   for( window = (SFG_Window *)fgStructure.Windows.First;
359        window;
360        window = (SFG_Window *)window->Node.Next )
361     fghRemoveMenuFromWindow( window, menu );
362
363   /*
364    * Now proceed with removing menu entries that lead to this menu
365    */
366   for( from = (SFG_Menu *)fgStructure.Menus.First;
367        from;
368        from = (SFG_Menu *)from->Node.Next )
369     fghRemoveMenuFromMenu( from, menu );
370
371   /*
372    * If the programmer defined a destroy callback, call it
373    * A. Donev: But first make this the active menu
374    */    
375   if ( menu->Destroy != NULL )
376   {
377     SFG_Menu *activeMenu=fgStructure.Menu;
378     fgStructure.Menu = menu;    
379     menu->Destroy () ;  
380     fgStructure.Menu = activeMenu;     
381   }        
382
383   /*
384    * Now we are pretty sure the menu is not used anywhere
385    * and that we can remove all of its entries
386    */
387   while( (entry = (SFG_MenuEntry *)menu->Entries.First) != NULL )
388   {
389     fgListRemove(&menu->Entries, &entry->Node);
390
391     if( entry->Text )
392         free( entry->Text );
393     entry->Text = NULL;
394     
395     free( entry );
396     entry = NULL;
397   }
398
399   if ( fgStructure.Window == menu->Window )
400     fgSetWindow ( menu->ParentWindow ) ;
401   fgDestroyWindow ( menu->Window, TRUE ) ;
402   fgListRemove( &fgStructure.Menus, &menu->Node );
403   if( fgStructure.Menu == menu )
404     fgStructure.Menu = NULL;
405
406   free( menu );
407 }
408
409 /*
410  * This function should be called on glutInit(). It will prepare the internal
411  * structure of freeglut to be used in the application. The structure will be
412  * destroyed using fgDestroyStructure() on glutMainLoop() return. In that
413  * case further use of freeglut should be preceeded with a glutInit() call.
414  */
415 void fgCreateStructure( void )
416 {
417   /*
418    * We will be needing two lists: the first containing windows,
419    * and the second containing the user-defined menus.
420    * Also, no current window/menu is set, as none has been created yet.
421    */
422
423   fgListInit(&fgStructure.Windows);
424   fgListInit(&fgStructure.Menus);
425 }
426
427 /*
428  * This function is automatically called on glutMainLoop() return.
429  * It should deallocate and destroy all remnants of previous
430  * glutInit()-enforced structure initialization...
431  */
432 void fgDestroyStructure( void )
433 {
434   SFG_Window *window;
435   SFG_Menu *menu;
436
437   freeglut_assert_ready;
438
439   /*
440    * Make sure all windows and menus have been deallocated
441    */
442   while( (menu = (SFG_Menu *)fgStructure.Menus.First) != NULL )
443     fgDestroyMenu( menu );
444
445   while( (window = (SFG_Window *)fgStructure.Windows.First) != NULL )
446     fgDestroyWindow( window, TRUE );
447 }
448
449 /*
450  * Helper function to enumerate through all registered top-level windows
451  */
452 void fgEnumWindows( FGCBenumerator enumCallback, SFG_Enumerator* enumerator )
453 {
454   SFG_Window *window;
455
456   assert( (enumCallback != NULL) && (enumerator != NULL) );
457   freeglut_assert_ready;
458
459   /*
460    * Check every of the top-level windows
461    */
462   for( window = (SFG_Window *)fgStructure.Windows.First;
463        window;
464        window = (SFG_Window *)window->Node.Next )
465   {
466     enumCallback( window, enumerator );
467     if( enumerator->found == TRUE )
468       return;
469   }
470 }
471
472 /*
473  * Helper function to enumerate through all a window's subwindows
474  * (single level descent)
475  */
476 void fgEnumSubWindows( SFG_Window* window, FGCBenumerator enumCallback,
477                        SFG_Enumerator* enumerator )
478 {
479   SFG_Window *child;
480
481   assert( (enumCallback != NULL) && (enumerator != NULL) );
482   freeglut_assert_ready;
483
484   for( child = (SFG_Window *)window->Children.First;
485        child;
486        child = (SFG_Window *)child->Node.Next )
487   {
488     enumCallback( child, enumerator );
489     if( enumerator->found == TRUE )
490       return;
491   }
492 }
493
494 /*
495  * A static helper function to look for a window given its handle
496  */
497 static void fghcbWindowByHandle( SFG_Window *window,
498                                  SFG_Enumerator *enumerator )
499 {
500     freeglut_return_if_fail( enumerator->found == FALSE );
501
502 #if TARGET_HOST_UNIX_X11
503     #define WBHANDLE (Window)
504 #elif TARGET_HOST_WIN32
505     #define WBHANDLE (HWND)
506 #endif
507
508     /*
509      * Check the window's handle. Hope this works. Looks ugly. That's for sure.
510      */
511     if( window->Window.Handle == WBHANDLE (enumerator->data) )
512     {
513         enumerator->found = TRUE;
514         enumerator->data = window;
515
516         return;
517     }
518
519     /*
520      * Otherwise, check this window's children
521      */
522     fgEnumSubWindows( window, fghcbWindowByHandle, enumerator );
523
524 #undef WBHANDLE
525 }
526
527 /*
528  * fgWindowByHandle returns a (SFG_Window *) value pointing to the
529  * first window in the queue matching the specified window handle.
530  * The function is defined in freeglut_structure.c file.
531  */
532 SFG_Window* fgWindowByHandle
533 #if TARGET_HOST_UNIX_X11
534 ( Window hWindow )
535 #elif TARGET_HOST_WIN32
536 ( HWND hWindow )
537 #endif
538 {
539     SFG_Enumerator enumerator;
540
541     /*
542      * This is easy and makes use of the windows enumeration defined above
543      */
544     enumerator.found = FALSE;
545     enumerator.data = (void *)hWindow;
546     fgEnumWindows( fghcbWindowByHandle, &enumerator );
547
548     if( enumerator.found == TRUE )
549         return( SFG_Window *) enumerator.data;
550     return NULL;
551 }
552
553 /*
554  * A static helper function to look for a window given its ID
555  */
556 static void fghcbWindowByID( SFG_Window *window, SFG_Enumerator *enumerator )
557 {
558     /*
559      * Make sure we do not overwrite our precious results...
560      */
561     freeglut_return_if_fail( enumerator->found == FALSE );
562
563     /*
564      * Check the window's handle. Hope this works. Looks ugly. That's for sure.
565      */
566     if( window->ID == (int) (enumerator->data) ) /* XXX int/ptr conversion! */
567     {
568         enumerator->found = TRUE;
569         enumerator->data = window;
570
571         return;
572     }
573
574     /*
575      * Otherwise, check this window's children
576      */
577     fgEnumSubWindows( window, fghcbWindowByID, enumerator );
578 }
579
580 /*
581  * This function is similiar to the previous one, except it is
582  * looking for a specified (sub)window identifier. The function
583  * is defined in freeglut_structure.c file.
584  */
585 SFG_Window* fgWindowByID( int windowID )
586 {
587     SFG_Enumerator enumerator;
588
589     /*
590      * Uses a method very similiar for fgWindowByHandle...
591      */
592     enumerator.found = FALSE;
593     enumerator.data = (void *) windowID; /* XXX int/pointer conversion! */
594     fgEnumWindows( fghcbWindowByID, &enumerator );
595     if( enumerator.found == TRUE )
596         return( SFG_Window *) enumerator.data;
597     return NULL;
598 }
599
600 /*
601  * Looks up a menu given its ID. This is easier that fgWindowByXXX
602  * as all menus are placed in a single doubly linked list...
603  */
604 SFG_Menu* fgMenuByID( int menuID )
605 {
606     SFG_Menu *menu = NULL;
607
608     freeglut_assert_ready;
609
610     /*
611      * It's enough to check all entries in fgStructure.Menus...
612      */
613     for( menu = (SFG_Menu *)fgStructure.Menus.First;
614          menu;
615          menu = (SFG_Menu *)menu->Node.Next )
616         if( menu->ID == menuID )
617             return menu;
618     return NULL;
619 }
620
621 /*
622  * List functions...
623  */
624 void fgListInit(SFG_List *list)
625 {
626     list->First = NULL;
627     list->Last = NULL;
628 }
629
630 void fgListAppend(SFG_List *list, SFG_Node *node)
631 {
632     SFG_Node *ln;
633
634     if ( (ln = (SFG_Node *)list->Last) != NULL )
635     {
636         ln->Next = node;
637         node->Prev = ln;
638     }
639     else
640     {
641         node->Prev = NULL;
642         list->First = node;
643     }
644
645     node->Next = NULL;
646     list->Last = node;
647 }
648
649 void fgListRemove(SFG_List *list, SFG_Node *node)
650 {
651     SFG_Node *ln;
652
653     if ( (ln = (SFG_Node *)node->Next) != NULL )
654         ln->Prev = node->Prev;
655     if ( (ln = (SFG_Node *)node->Prev) != NULL )
656         ln->Next = node->Next;
657     if ( (ln = (SFG_Node *)list->First) == node )
658         list->First = node->Next;
659     if ( (ln = (SFG_Node *)list->Last) == node )
660         list->Last = node->Prev;
661 }
662
663 int fgListLength(SFG_List *list)
664 {
665     SFG_Node *node;
666     int length = 0;
667
668     for( node = (SFG_Node *)list->First; node; node = (SFG_Node *)node->Next )
669         ++length;
670
671     return length;
672 }
673
674 /*** END OF FILE ***/