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