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.
44 SFG_Structure fgStructure;
47 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */
50 * This private function creates, opens and adds to the hierarchy
51 * a freeglut window complete with OpenGL context and stuff...
53 * If parent is set to NULL, the window created will be a topmost one.
55 SFG_Window* fgCreateWindow( SFG_Window* parent, const char* title, int x, int y, int w, int h, GLboolean gameMode )
58 * Have the window object created
60 SFG_Window* window = calloc( sizeof(SFG_Window), 1 );
64 * If the freeglut internals haven't been initialized yet,
65 * do it now. Hack's idea courtesy of Chris Purnell...
67 if( !fgState.Time.Set )
68 glutInit( &fakeArgc, NULL );
71 * Initialize the object properties
73 window->ID = ++fgStructure.WindowID;
76 * Initialize the children list
78 fgListInit( &window->Children );
81 * Does this window have a parent?
86 * That's quite right, attach this windows as a child window
88 fgListAppend( &parent->Children, &window->Node );
89 window->Parent = parent;
94 * Otherwise add the newly created window to the topmost windows list
96 fgListAppend( &fgStructure.Windows, &window->Node );
100 * Set the default mouse cursor and reset the modifiers value
102 window->State.Cursor = GLUT_CURSOR_INHERIT;
103 window->State.Modifiers = 0xffffffff;
106 * Open the window now. The fgOpenWindow() function is system
107 * dependant, and resides in freeglut_window.c. Uses fgState.
109 fgOpenWindow( window, title, x, y, w, h, gameMode, (parent != NULL) ? TRUE : FALSE );
112 * Return a pointer to the newly created window
118 * This private function creates a menu and adds it to the menus list
120 SFG_Menu* fgCreateMenu( FGCBmenu menuCallback )
123 * Have the menu object created
125 SFG_Menu* menu = calloc( sizeof(SFG_Menu), 1 );
129 * If the freeglut internals haven't been initialized yet,
130 * do it now. Hack's idea courtesy of Chris Purnell...
132 if( !fgState.Time.Set )
133 glutInit( &fakeArgc, NULL );
136 * Initialize the object properties:
138 menu->ID = ++fgStructure.MenuID;
139 menu->Callback = menuCallback;
142 * Initialize the entries list
144 fgListInit( &menu->Entries );
147 * Add it to the menu structure hierarchy
149 fgListAppend( &fgStructure.Menus, &menu->Node );
152 * Newly created menus implicitly become current ones
154 fgStructure.Menu = menu;
157 * Return the result to the caller
163 * Linked list of windows to destroy ... this is so we don't destroy a window from the middle of
164 * its callback. Some C compilers take an extremely dim view of this.
167 static SFG_WindowList* WindowsToDestroy = (SFG_WindowList*)NULL ;
170 * Function to add a window to the linked list of windows to destroy. Subwindows are automatically
171 * added because they hang from the window structure.
173 void fgAddToWindowDestroyList ( SFG_Window* window, GLboolean needToClose )
175 SFG_WindowList *new_list_entry = (SFG_WindowList*)malloc ( sizeof(SFG_WindowList) ) ;
176 new_list_entry->window = window ;
177 new_list_entry->needToClose = needToClose ;
178 new_list_entry->next = WindowsToDestroy ;
179 WindowsToDestroy = new_list_entry ;
182 * Check if the window is the current one...
184 if ( fgStructure.Window == window )
185 fgStructure.Window = NULL;
188 * If the destroyed window has the highest window ID number, decrement the window ID number
190 if ( window->ID == fgStructure.WindowID ) fgStructure.WindowID-- ;
193 * Check the execution state. If this has been called from "glutDestroyWindow",
194 * a statement in that function will reset the "ExecState" after this function returns.
196 if ( fgState.ActionOnWindowClose != GLUT_ACTION_CONTINUE_EXECUTION )
199 * Set the execution state flag to drop out of the main loop.
201 if ( fgState.ActionOnWindowClose == GLUT_ACTION_EXIT )
202 fgState.ExecState = GLUT_EXEC_STATE_STOP ;
207 * Function to close down all the windows in the "WindowsToDestroy" list
209 void fgCloseWindows ()
211 SFG_WindowList *window_ptr = WindowsToDestroy ;
212 WindowsToDestroy = (SFG_WindowList*)NULL ; /* In case the destroy callbacks cause more windows to be closed */
216 SFG_WindowList *next = window_ptr->next ;
217 fgDestroyWindow ( window_ptr->window, window_ptr->needToClose ) ;
218 free ( window_ptr ) ;
223 window_ptr = WindowsToDestroy ;
224 WindowsToDestroy = (SFG_WindowList*)NULL ;
230 * This function destroys a window and all of its subwindows. Actually,
231 * another function, defined in freeglut_window.c is called, but this is
232 * a whole different story...
234 void fgDestroyWindow( SFG_Window* window, GLboolean needToClose )
236 SFG_Window* subWindow;
238 assert( window != NULL );
239 freeglut_assert_ready;
242 * Does this window have any subwindows?
244 while ( (subWindow = window->Children.First) != NULL )
247 * Destroy the first window in the list (possibly destroying
248 * its subwindows too). This is not very effective, but works
250 fgDestroyWindow( subWindow, needToClose );
254 * If the programmer defined a destroy callback, call it
256 if ( window->Callbacks.Destroy != NULL )
258 SFG_Window *activeWindow = fgStructure.Window ;
259 fgStructure.Window = window ;
260 window->Callbacks.Destroy () ;
261 fgStructure.Window = activeWindow ;
265 * Now we should remove the reference to this window from its parent
267 if ( window->Parent != NULL )
268 fgListRemove( &window->Parent->Children, &window->Node );
270 fgListRemove( &fgStructure.Windows, &window->Node );
273 * OK, this window seems disconnected from the structure enough
274 * in order to be closed without any bigger risks...
276 if( needToClose == TRUE )
277 fgCloseWindow( window );
280 * Finally, we can delete the window's object. It hopefully does
281 * have everything inside it freed and we do not have to care...
287 * This is a helper static function that removes a menu (given its pointer)
288 * from any windows that can be accessed from a given parent...
290 static void fghRemoveMenuFromWindow( SFG_Window* window, SFG_Menu* menu )
292 SFG_Window *subWindow;
296 * Check if the menu is attached to the current window,
297 * if so, have it detached (by overwriting with a NULL):
301 if( window->Menu[ i ] == menu )
302 window->Menu[ i ] = NULL;
306 * Call this function for all of the window's children recursively:
308 for( subWindow = window->Children.First; subWindow;
309 subWindow = subWindow->Node.Next)
311 fghRemoveMenuFromWindow( subWindow, menu );
316 * This is a static helper function that removes menu references
317 * from another menu, given two pointers to them...
319 static void fghRemoveMenuFromMenu( SFG_Menu* from, SFG_Menu* menu )
321 SFG_MenuEntry *entry;
323 for( entry = from->Entries.First; entry; entry = entry->Node.Next )
325 if (entry->SubMenu == menu)
327 entry->SubMenu = NULL;
333 * This function destroys a menu specified by the parameter. All menus
334 * and windows are updated to make sure no ill pointers hang around.
336 void fgDestroyMenu( SFG_Menu* menu )
340 SFG_MenuEntry *entry;
342 assert( menu != NULL );
343 freeglut_assert_ready;
346 * First of all, have all references to this menu removed from all windows:
348 for( window = fgStructure.Windows.First; window; window = window->Node.Next )
350 fghRemoveMenuFromWindow( window, menu );
354 * Now proceed with removing menu entries that lead to this menu
356 for( from = fgStructure.Menus.First; from; from = from->Node.Next )
358 fghRemoveMenuFromMenu( from, menu );
362 * Now we are pretty sure the menu is not used anywhere
363 * and that we can remove all of its entries
365 while( (entry = menu->Entries.First) != NULL )
367 fgListRemove(&menu->Entries, &entry->Node);
370 * There might be a string allocated, have it freed:
375 * Deallocate the entry itself:
381 * Remove the menu from the menus list
383 fgListRemove( &fgStructure.Menus, &menu->Node );
386 * If that menu was the current one...
388 if( fgStructure.Menu == menu )
389 fgStructure.Menu = NULL;
392 * Have the menu structure freed
398 * This function should be called on glutInit(). It will prepare the internal
399 * structure of freeglut to be used in the application. The structure will be
400 * destroyed using fgDestroyStructure() on glutMainLoop() return. In that
401 * case further use of freeglut should be preceeded with a glutInit() call.
403 void fgCreateStructure( void )
406 * We will be needing two lists: the first containing windows,
407 * and the second containing the user-defined menus.
408 * Also, no current window/menu is set, as none has been created yet.
411 fgListInit(&fgStructure.Windows);
412 fgListInit(&fgStructure.Menus);
416 * This function is automatically called on glutMainLoop() return. It should deallocate
417 * and destroy all remnants of previous glutInit()-enforced structure initialization...
419 void fgDestroyStructure( void )
425 * Just make sure we are not called in vain...
427 freeglut_assert_ready;
430 * Make sure all windows and menus have been deallocated
432 while( (window = fgStructure.Windows.First) != NULL )
433 fgDestroyWindow( window, TRUE );
435 while( (menu = fgStructure.Menus.First) != NULL )
436 fgDestroyMenu( menu );
440 * Helper function to enumerate through all registered top-level windows
442 void fgEnumWindows( FGCBenumerator enumCallback, SFG_Enumerator* enumerator )
446 assert( (enumCallback != NULL) && (enumerator != NULL) );
447 freeglut_assert_ready;
450 * Check every of the top-level windows
452 for( window = fgStructure.Windows.First; window;
453 window = window->Node.Next )
456 * Execute the callback...
458 enumCallback( window, enumerator );
461 * If it has been marked as 'found', stop searching
463 if( enumerator->found == TRUE )
469 * Helper function to enumerate through all a window's subwindows (single level descent)
471 void fgEnumSubWindows( SFG_Window* window, FGCBenumerator enumCallback, SFG_Enumerator* enumerator )
475 assert( (enumCallback != NULL) && (enumerator != NULL) );
476 freeglut_assert_ready;
479 * Check every of the window's children:
481 for( child = window->Children.First; child; child = child->Node.Next )
484 * Execute the callback...
486 enumCallback( child, enumerator );
489 * If it has been marked as 'found', stop searching
491 if( enumerator->found == TRUE )
497 * A static helper function to look for a window given its handle
499 static void fghcbWindowByHandle( SFG_Window *window, SFG_Enumerator *enumerator )
502 * Make sure we do not overwrite our precious results...
504 freeglut_return_if_fail( enumerator->found == FALSE );
506 #if TARGET_HOST_UNIX_X11
507 #define WBHANDLE (Window)
508 #elif TARGET_HOST_WIN32
509 #define WBHANDLE (HWND)
513 * Check the window's handle. Hope this works. Looks ugly. That's for sure.
515 if( window->Window.Handle == WBHANDLE (enumerator->data) )
517 enumerator->found = TRUE;
518 enumerator->data = window;
524 * Otherwise, check this window's children
526 fgEnumSubWindows( window, fghcbWindowByHandle, enumerator );
532 * fgWindowByHandle returns a (SFG_Window *) value pointing to the
533 * first window in the queue matching the specified window handle.
534 * The function is defined in freeglut_structure.c file.
536 SFG_Window* fgWindowByHandle
537 #if TARGET_HOST_UNIX_X11
539 #elif TARGET_HOST_WIN32
543 SFG_Enumerator enumerator;
546 * This is easy and makes use of the windows enumeration defined above
548 enumerator.found = FALSE;
549 enumerator.data = (void *)hWindow;
552 * Start the enumeration now:
554 fgEnumWindows( fghcbWindowByHandle, &enumerator );
557 * Check if the window has been found or not:
559 if( enumerator.found == TRUE )
560 return( (SFG_Window *) enumerator.data );
563 * Otherwise return NULL to mark the failure
569 * A static helper function to look for a window given its ID
571 static void fghcbWindowByID( SFG_Window *window, SFG_Enumerator *enumerator )
574 * Make sure we do not overwrite our precious results...
576 freeglut_return_if_fail( enumerator->found == FALSE );
579 * Check the window's handle. Hope this works. Looks ugly. That's for sure.
581 if( window->ID == (int) (enumerator->data) )
583 enumerator->found = TRUE;
584 enumerator->data = window;
590 * Otherwise, check this window's children
592 fgEnumSubWindows( window, fghcbWindowByID, enumerator );
596 * This function is similiar to the previous one, except it is
597 * looking for a specified (sub)window identifier. The function
598 * is defined in freeglut_structure.c file.
600 SFG_Window* fgWindowByID( int windowID )
602 SFG_Enumerator enumerator;
605 * Uses a method very similiar for fgWindowByHandle...
607 enumerator.found = FALSE;
608 enumerator.data = (void *) windowID;
611 * Start the enumeration now:
613 fgEnumWindows( fghcbWindowByID, &enumerator );
616 * Check if the window has been found or not:
618 if( enumerator.found == TRUE )
619 return( (SFG_Window *) enumerator.data );
622 * Otherwise return NULL to mark the failure
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...
631 SFG_Menu* fgMenuByID( int menuID )
633 SFG_Menu *menu = NULL;
635 freeglut_assert_ready;
638 * It's enough to check all entries in fgStructure.Menus...
640 for( menu = fgStructure.Menus.First; menu; menu = menu->Node.Next )
643 * Does the ID number match?
645 if( menu->ID == menuID )
650 * We have not found the requested menu ID
658 void fgListInit(SFG_List *list)
664 void fgListAppend(SFG_List *list, SFG_Node *node)
668 if ( (ln = list->Last) != NULL )
683 void fgListRemove(SFG_List *list, SFG_Node *node)
687 if ( (ln = node->Next) != NULL )
688 ln->Prev = node->Prev;
689 if ( (ln = node->Prev) != NULL )
690 ln->Next = node->Next;
691 if ( (ln = list->First) == node )
692 list->First = node->Next;
693 if ( (ln = list->Last) == node )
694 list->Last = node->Prev;
697 int fgListLength(SFG_List *list)
702 for( node = list->First; node; node = node->Next )
708 /*** END OF FILE ***/