4 * Windows and menus need tree structure
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
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:
17 * The above copyright notice and this permission notice shall be included
18 * in all copies or substantial portions of the Software.
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.
32 #define G_LOG_DOMAIN "freeglut-structure"
34 #include "../include/GL/freeglut.h"
35 #include "freeglut_internal.h"
38 /* -- GLOBAL EXPORTS ------------------------------------------------------- */
41 * The SFG_Structure container holds information about windows and menus
42 * created between glutInit() and glutMainLoop() return.
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 */
55 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */
58 * This private function creates, opens and adds to the hierarchy
59 * a freeglut window complete with OpenGL context and stuff...
61 * If parent is set to NULL, the window created will be a topmost one.
63 SFG_Window* fgCreateWindow( SFG_Window* parent, const char* title, int x, int y, int w, int h, GLboolean gameMode )
66 * Have the window object created
68 SFG_Window *window = (SFG_Window *)calloc( sizeof(SFG_Window), 1 );
72 * If the freeglut internals haven't been initialized yet,
73 * do it now. Hack's idea courtesy of Chris Purnell...
75 if( !fgState.Time.Set )
76 glutInit( &fakeArgc, NULL );
79 * Initialize the object properties
81 window->ID = ++fgStructure.WindowID;
84 * Initialize the children list
86 fgListInit( &window->Children );
89 * Does this window have a parent?
94 * That's quite right, attach this windows as a child window
96 fgListAppend( &parent->Children, &window->Node );
97 window->Parent = parent;
102 * Otherwise add the newly created window to the topmost windows list
104 fgListAppend( &fgStructure.Windows, &window->Node );
108 * Set the default mouse cursor and reset the modifiers value
110 window->State.Cursor = GLUT_CURSOR_INHERIT;
111 window->State.Modifiers = 0xffffffff;
114 * If this window is a menu, set IsMenu in the structure
116 window->IsMenu = fgState.BuildingAMenu ;
119 * Open the window now. The fgOpenWindow() function is system
120 * dependant, and resides in freeglut_window.c. Uses fgState.
122 fgOpenWindow( window, title, x, y, w, h, gameMode, (parent != NULL) ? TRUE : FALSE );
125 * Return a pointer to the newly created window
131 * This private function creates a menu and adds it to the menus list
133 SFG_Menu* fgCreateMenu( FGCBmenu menuCallback )
135 int x = 100, y = 100, w = 100, h = 100 ;
136 SFG_Window *current_window = fgStructure.Window ;
139 * Have the menu object created
141 SFG_Menu* menu = (SFG_Menu *)calloc( sizeof(SFG_Menu), 1 );
145 * If the freeglut internals haven't been initialized yet,
146 * do it now. Hack's idea courtesy of Chris Purnell...
148 if( !fgState.Time.Set )
149 glutInit( &fakeArgc, NULL );
151 menu->ParentWindow = fgStructure.Window ;
154 * Create a window for the menu to reside in. Set the
155 * global variable BuildingAMenu to true so we can ensure
156 * it is created without decorations.
158 fgState.BuildingAMenu = TRUE ;
160 fgCreateWindow ( NULL, NULL, x, y, w, h, FALSE ) ;
161 menu->Window = fgStructure.Window ;
162 glutDisplayFunc ( fgDisplayMenu ) ;
165 * While BuildingAMenu is true, all windows built have no decorations. That's
166 * not a good default behavior, so let's set it false again.
168 fgState.BuildingAMenu = FALSE ;
170 glutHideWindow () ; /* Hide the window for now */
171 fgStructure.Window = current_window ;
174 * Initialize the object properties:
176 menu->ID = ++fgStructure.MenuID;
177 menu->Callback = menuCallback;
178 menu->ActiveEntry = NULL ;
181 * Initialize the entries list
183 fgListInit( &menu->Entries );
186 * Add it to the menu structure hierarchy
188 fgListAppend( &fgStructure.Menus, &menu->Node );
191 * Newly created menus implicitly become current ones
193 fgStructure.Menu = menu;
196 * Return the result to the caller
202 * Linked list of windows to destroy ... this is so we don't destroy a window from the middle of
203 * its callback. Some C compilers take an extremely dim view of this.
206 static SFG_WindowList* WindowsToDestroy = (SFG_WindowList*)NULL ;
209 * Function to add a window to the linked list of windows to destroy. Subwindows are automatically
210 * added because they hang from the window structure.
212 void fgAddToWindowDestroyList ( SFG_Window* window, GLboolean needToClose )
214 SFG_WindowList *new_list_entry = (SFG_WindowList*)malloc ( sizeof(SFG_WindowList) ) ;
215 new_list_entry->window = window ;
216 new_list_entry->needToClose = needToClose ;
217 new_list_entry->next = WindowsToDestroy ;
218 WindowsToDestroy = new_list_entry ;
221 * Check if the window is the current one...
223 if ( fgStructure.Window == window )
224 fgStructure.Window = NULL;
227 * If the destroyed window has the highest window ID number, decrement the window ID number
229 if ( window->ID == fgStructure.WindowID ) fgStructure.WindowID-- ;
232 * Check the execution state. If this has been called from "glutDestroyWindow",
233 * a statement in that function will reset the "ExecState" after this function returns.
235 if ( fgState.ActionOnWindowClose != GLUT_ACTION_CONTINUE_EXECUTION )
238 * Set the execution state flag to drop out of the main loop.
240 if ( fgState.ActionOnWindowClose == GLUT_ACTION_EXIT )
241 fgState.ExecState = GLUT_EXEC_STATE_STOP ;
246 * Function to close down all the windows in the "WindowsToDestroy" list
248 void fgCloseWindows ()
250 SFG_WindowList *window_ptr = WindowsToDestroy ;
251 WindowsToDestroy = (SFG_WindowList*)NULL ; /* In case the destroy callbacks cause more windows to be closed */
255 SFG_WindowList *next = window_ptr->next ;
256 fgDestroyWindow ( window_ptr->window, window_ptr->needToClose ) ;
257 free ( window_ptr ) ;
262 window_ptr = WindowsToDestroy ;
263 WindowsToDestroy = (SFG_WindowList*)NULL ;
269 * This function destroys a window and all of its subwindows. Actually,
270 * another function, defined in freeglut_window.c is called, but this is
271 * a whole different story...
273 void fgDestroyWindow( SFG_Window* window, GLboolean needToClose )
275 SFG_Window* subWindow;
277 assert( window != NULL );
278 freeglut_assert_ready;
281 * Does this window have any subwindows?
283 while ( (subWindow = (SFG_Window *)window->Children.First) != NULL )
286 * Destroy the first window in the list (possibly destroying
287 * its subwindows too). This is not very effective, but works
289 fgDestroyWindow( subWindow, needToClose );
293 * If the programmer defined a destroy callback, call it
294 * A. Donev: But first make this the active window
296 if ( window->Callbacks.Destroy != NULL )
298 SFG_Window *activeWindow = fgStructure.Window ;
299 fgStructure.Window = window ;
300 window->Callbacks.Destroy () ;
301 fgStructure.Window = activeWindow ;
305 * Now we should remove the reference to this window from its parent
307 if ( window->Parent != NULL )
308 fgListRemove( &window->Parent->Children, &window->Node );
310 fgListRemove( &fgStructure.Windows, &window->Node );
313 * OK, this window seems disconnected from the structure enough
314 * in order to be closed without any bigger risks...
316 if( needToClose == TRUE )
317 fgCloseWindow( window );
320 * Finally, we can delete the window's object. It hopefully does
321 * have everything inside it freed and we do not have to care...
327 * This is a helper static function that removes a menu (given its pointer)
328 * from any windows that can be accessed from a given parent...
330 static void fghRemoveMenuFromWindow( SFG_Window* window, SFG_Menu* menu )
332 SFG_Window *subWindow;
336 * Check if the menu is attached to the current window,
337 * if so, have it detached (by overwriting with a NULL):
341 if( window->Menu[ i ] == menu )
342 window->Menu[ i ] = NULL;
346 * Call this function for all of the window's children recursively:
348 for( subWindow = (SFG_Window *)window->Children.First; subWindow;
349 subWindow = (SFG_Window *)subWindow->Node.Next)
351 fghRemoveMenuFromWindow( subWindow, menu );
356 * This is a static helper function that removes menu references
357 * from another menu, given two pointers to them...
359 static void fghRemoveMenuFromMenu( SFG_Menu* from, SFG_Menu* menu )
361 SFG_MenuEntry *entry;
363 for( entry = (SFG_MenuEntry *)from->Entries.First; entry; entry = (SFG_MenuEntry *)entry->Node.Next )
365 if (entry->SubMenu == menu)
367 entry->SubMenu = NULL;
373 * This function destroys a menu specified by the parameter. All menus
374 * and windows are updated to make sure no ill pointers hang around.
376 void fgDestroyMenu( SFG_Menu* menu )
380 SFG_MenuEntry *entry;
382 assert( menu != NULL );
383 freeglut_assert_ready;
386 * First of all, have all references to this menu removed from all windows:
388 for( window = (SFG_Window *)fgStructure.Windows.First; window; window = (SFG_Window *)window->Node.Next )
390 fghRemoveMenuFromWindow( window, menu );
394 * Now proceed with removing menu entries that lead to this menu
396 for( from = (SFG_Menu *)fgStructure.Menus.First; from; from = (SFG_Menu *)from->Node.Next )
398 fghRemoveMenuFromMenu( from, menu );
402 * If the programmer defined a destroy callback, call it
403 * A. Donev: But first make this the active menu
405 if ( menu->Destroy != NULL )
407 SFG_Menu *activeMenu=fgStructure.Menu;
408 fgStructure.Menu = menu;
410 fgStructure.Menu = activeMenu;
414 * Now we are pretty sure the menu is not used anywhere
415 * and that we can remove all of its entries
417 while( (entry = (SFG_MenuEntry *)menu->Entries.First) != NULL )
419 fgListRemove(&menu->Entries, &entry->Node);
422 * There might be a string allocated, have it freed:
427 * Deallocate the entry itself:
433 * Destroy the window associated with the menu
435 fgDestroyWindow ( menu->Window, TRUE ) ;
438 * Remove the menu from the menus list
440 fgListRemove( &fgStructure.Menus, &menu->Node );
443 * If that menu was the current one...
445 if( fgStructure.Menu == menu )
446 fgStructure.Menu = NULL;
449 * Have the menu structure freed
455 * This function should be called on glutInit(). It will prepare the internal
456 * structure of freeglut to be used in the application. The structure will be
457 * destroyed using fgDestroyStructure() on glutMainLoop() return. In that
458 * case further use of freeglut should be preceeded with a glutInit() call.
460 void fgCreateStructure( void )
463 * We will be needing two lists: the first containing windows,
464 * and the second containing the user-defined menus.
465 * Also, no current window/menu is set, as none has been created yet.
468 fgListInit(&fgStructure.Windows);
469 fgListInit(&fgStructure.Menus);
473 * This function is automatically called on glutMainLoop() return. It should deallocate
474 * and destroy all remnants of previous glutInit()-enforced structure initialization...
476 void fgDestroyStructure( void )
482 * Just make sure we are not called in vain...
484 freeglut_assert_ready;
487 * Make sure all windows and menus have been deallocated
489 while( (window = (SFG_Window *)fgStructure.Windows.First) != NULL )
490 fgDestroyWindow( window, TRUE );
492 while( (menu = (SFG_Menu *)fgStructure.Menus.First) != NULL )
493 fgDestroyMenu( menu );
497 * Helper function to enumerate through all registered top-level windows
499 void fgEnumWindows( FGCBenumerator enumCallback, SFG_Enumerator* enumerator )
503 assert( (enumCallback != NULL) && (enumerator != NULL) );
504 freeglut_assert_ready;
507 * Check every of the top-level windows
509 for( window = (SFG_Window *)fgStructure.Windows.First; window;
510 window = (SFG_Window *)window->Node.Next )
513 * Execute the callback...
515 enumCallback( window, enumerator );
518 * If it has been marked as 'found', stop searching
520 if( enumerator->found == TRUE )
526 * Helper function to enumerate through all a window's subwindows (single level descent)
528 void fgEnumSubWindows( SFG_Window* window, FGCBenumerator enumCallback, SFG_Enumerator* enumerator )
532 assert( (enumCallback != NULL) && (enumerator != NULL) );
533 freeglut_assert_ready;
536 * Check every of the window's children:
538 for( child = (SFG_Window *)window->Children.First; child; child = (SFG_Window *)child->Node.Next )
541 * Execute the callback...
543 enumCallback( child, enumerator );
546 * If it has been marked as 'found', stop searching
548 if( enumerator->found == TRUE )
554 * A static helper function to look for a window given its handle
556 static void fghcbWindowByHandle( SFG_Window *window, SFG_Enumerator *enumerator )
559 * Make sure we do not overwrite our precious results...
561 freeglut_return_if_fail( enumerator->found == FALSE );
563 #if TARGET_HOST_UNIX_X11
564 #define WBHANDLE (Window)
565 #elif TARGET_HOST_WIN32
566 #define WBHANDLE (HWND)
570 * Check the window's handle. Hope this works. Looks ugly. That's for sure.
572 if( window->Window.Handle == WBHANDLE (enumerator->data) )
574 enumerator->found = TRUE;
575 enumerator->data = window;
581 * Otherwise, check this window's children
583 fgEnumSubWindows( window, fghcbWindowByHandle, enumerator );
589 * fgWindowByHandle returns a (SFG_Window *) value pointing to the
590 * first window in the queue matching the specified window handle.
591 * The function is defined in freeglut_structure.c file.
593 SFG_Window* fgWindowByHandle
594 #if TARGET_HOST_UNIX_X11
596 #elif TARGET_HOST_WIN32
600 SFG_Enumerator enumerator;
603 * This is easy and makes use of the windows enumeration defined above
605 enumerator.found = FALSE;
606 enumerator.data = (void *)hWindow;
609 * Start the enumeration now:
611 fgEnumWindows( fghcbWindowByHandle, &enumerator );
614 * Check if the window has been found or not:
616 if( enumerator.found == TRUE )
617 return( (SFG_Window *) enumerator.data );
620 * Otherwise return NULL to mark the failure
626 * A static helper function to look for a window given its ID
628 static void fghcbWindowByID( SFG_Window *window, SFG_Enumerator *enumerator )
631 * Make sure we do not overwrite our precious results...
633 freeglut_return_if_fail( enumerator->found == FALSE );
636 * Check the window's handle. Hope this works. Looks ugly. That's for sure.
638 if( window->ID == (int) (enumerator->data) )
640 enumerator->found = TRUE;
641 enumerator->data = window;
647 * Otherwise, check this window's children
649 fgEnumSubWindows( window, fghcbWindowByID, enumerator );
653 * This function is similiar to the previous one, except it is
654 * looking for a specified (sub)window identifier. The function
655 * is defined in freeglut_structure.c file.
657 SFG_Window* fgWindowByID( int windowID )
659 SFG_Enumerator enumerator;
662 * Uses a method very similiar for fgWindowByHandle...
664 enumerator.found = FALSE;
665 enumerator.data = (void *) windowID;
668 * Start the enumeration now:
670 fgEnumWindows( fghcbWindowByID, &enumerator );
673 * Check if the window has been found or not:
675 if( enumerator.found == TRUE )
676 return( (SFG_Window *) enumerator.data );
679 * Otherwise return NULL to mark the failure
685 * Looks up a menu given its ID. This is easier that fgWindowByXXX
686 * as all menus are placed in a single doubly linked list...
688 SFG_Menu* fgMenuByID( int menuID )
690 SFG_Menu *menu = NULL;
692 freeglut_assert_ready;
695 * It's enough to check all entries in fgStructure.Menus...
697 for( menu = (SFG_Menu *)fgStructure.Menus.First; menu; menu = (SFG_Menu *)menu->Node.Next )
700 * Does the ID number match?
702 if( menu->ID == menuID )
707 * We have not found the requested menu ID
715 void fgListInit(SFG_List *list)
721 void fgListAppend(SFG_List *list, SFG_Node *node)
725 if ( (ln = (SFG_Node *)list->Last) != NULL )
740 void fgListRemove(SFG_List *list, SFG_Node *node)
744 if ( (ln = (SFG_Node *)node->Next) != NULL )
745 ln->Prev = node->Prev;
746 if ( (ln = (SFG_Node *)node->Prev) != NULL )
747 ln->Next = node->Next;
748 if ( (ln = (SFG_Node *)list->First) == node )
749 list->First = node->Next;
750 if ( (ln = (SFG_Node *)list->Last) == node )
751 list->Last = node->Prev;
754 int fgListLength(SFG_List *list)
759 for( node = (SFG_Node *)list->First; node; node = (SFG_Node *)node->Next )
765 /*** END OF FILE ***/