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