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