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 #include "../include/GL/freeglut.h"
33 #include "freeglut_internal.h"
36 /* -- GLOBAL EXPORTS ------------------------------------------------------- */
39 * The SFG_Structure container holds information about windows and menus
40 * created between glutInit() and glutMainLoop() return.
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 */
53 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */
56 * This private function creates, opens and adds to the hierarchy
57 * a freeglut window complete with OpenGL context and stuff...
59 * If parent is set to NULL, the window created will be a topmost one.
61 SFG_Window* fgCreateWindow( SFG_Window* parent, const char* title,
62 int x, int y, int w, int h, GLboolean gameMode )
65 * Have the window object created
67 SFG_Window *window = (SFG_Window *)calloc( sizeof(SFG_Window), 1 );
71 * If the freeglut internals haven't been initialized yet,
72 * do it now. Hack's idea courtesy of Chris Purnell...
74 if( !fgState.Time.Set )
75 glutInit( &fakeArgc, NULL );
78 * Initialize the object properties
80 window->ID = ++fgStructure.WindowID;
82 fgListInit( &window->Children );
85 fgListAppend( &parent->Children, &window->Node );
86 window->Parent = parent;
89 fgListAppend( &fgStructure.Windows, &window->Node );
92 * Set the default mouse cursor and reset the modifiers value
94 window->State.Cursor = GLUT_CURSOR_INHERIT;
95 window->State.Modifiers = 0xffffffff;
97 window->IsMenu = fgState.BuildingAMenu ;
100 * Open the window now. The fgOpenWindow() function is system
101 * dependant, and resides in freeglut_window.c. Uses fgState.
103 fgOpenWindow( window, title, x, y, w, h, gameMode,
104 (parent != NULL) ? TRUE : FALSE );
110 * This private function creates a menu and adds it to the menus list
112 SFG_Menu* fgCreateMenu( FGCBMenu menuCallback )
114 int x = 100, y = 100, w = 100, h = 100 ;
115 SFG_Window *current_window = fgStructure.Window ;
118 * Have the menu object created
120 SFG_Menu* menu = (SFG_Menu *)calloc( sizeof(SFG_Menu), 1 );
124 * If the freeglut internals haven't been initialized yet,
125 * do it now. Hack's idea courtesy of Chris Purnell...
127 if( !fgState.Time.Set )
128 glutInit( &fakeArgc, NULL );
130 menu->ParentWindow = fgStructure.Window ;
133 * Create a window for the menu to reside in. Set the
134 * global variable BuildingAMenu to true so we can ensure
135 * it is created without decorations.
137 fgState.BuildingAMenu = TRUE ;
139 fgCreateWindow ( NULL, NULL, x, y, w, h, FALSE ) ;
140 menu->Window = fgStructure.Window ;
141 glutDisplayFunc ( fgDisplayMenu ) ;
144 * While BuildingAMenu is true, all windows built have no decorations.
145 * That's not a good default behavior, so let's set it false again.
147 fgState.BuildingAMenu = FALSE ;
149 glutHideWindow () ; /* Hide the window for now */
150 fgSetWindow ( current_window ) ;
153 * Initialize the object properties:
155 menu->ID = ++fgStructure.MenuID;
156 menu->Callback = menuCallback;
157 menu->ActiveEntry = NULL ;
159 fgListInit( &menu->Entries );
160 fgListAppend( &fgStructure.Menus, &menu->Node );
163 * Newly created menus implicitly become current ones
165 fgStructure.Menu = menu;
171 * Linked list of windows to destroy ... this is so we don't destroy a
172 * window from the middle of its callback. Some C compilers take an
173 * extremely dim view of this.
176 static SFG_WindowList* WindowsToDestroy = (SFG_WindowList*)NULL ;
179 * Function to add a window to the linked list of windows to destroy.
180 * Subwindows are automatically added because they hang from the window
183 void fgAddToWindowDestroyList ( SFG_Window* window, GLboolean needToClose )
185 SFG_WindowList *new_list_entry =
186 (SFG_WindowList*)malloc ( sizeof(SFG_WindowList) ) ;
187 new_list_entry->window = window ;
188 new_list_entry->needToClose = needToClose ;
189 new_list_entry->next = WindowsToDestroy ;
190 WindowsToDestroy = new_list_entry ;
193 * Check if the window is the current one...
195 if ( fgStructure.Window == window )
196 fgStructure.Window = NULL;
199 * If the destroyed window has the highest window ID number, decrement
200 * the window ID number.
202 * XXX Do we REALLY want to *ever* recycle window IDs? Integers are
203 * XXX plentiful, and clients may rely upon the implied promise in
204 * XXX the GLUT docs to not recycle these. (I can't remember if it
207 * XXX If we *do* want to do this, we should actually recompute the
208 * XXX highest window-ID; the new highest may not in fact be one less
209 * XXX than what we have just deleted.
211 if ( window->ID == fgStructure.WindowID )
212 fgStructure.WindowID-- ;
215 * Check the execution state. If this has been called from
216 * "glutDestroyWindow", a statement in that function will reset the
217 * "ExecState" after this function returns.
219 if ( fgState.ActionOnWindowClose != GLUT_ACTION_CONTINUE_EXECUTION )
222 * Set the execution state flag to drop out of the main loop.
224 if ( fgState.ActionOnWindowClose == GLUT_ACTION_EXIT )
225 fgState.ExecState = GLUT_EXEC_STATE_STOP ;
230 * Function to close down all the windows in the "WindowsToDestroy" list
232 void fgCloseWindows ()
234 SFG_WindowList *window_ptr = WindowsToDestroy ;
235 WindowsToDestroy = (SFG_WindowList*)NULL ;
236 /* In case the destroy callbacks cause more windows to be closed */
240 SFG_WindowList *next = window_ptr->next ;
241 fgDestroyWindow ( window_ptr->window, window_ptr->needToClose ) ;
242 free ( window_ptr ) ;
247 window_ptr = WindowsToDestroy ;
248 WindowsToDestroy = (SFG_WindowList*)NULL ;
254 * This function destroys a window and all of its subwindows. Actually,
255 * another function, defined in freeglut_window.c is called, but this is
256 * a whole different story...
258 void fgDestroyWindow( SFG_Window* window, GLboolean needToClose )
260 SFG_Window* subWindow;
263 assert( window != NULL );
264 freeglut_assert_ready;
266 while ( (subWindow = (SFG_Window *)window->Children.First) != NULL )
267 fgDestroyWindow( subWindow, needToClose );
269 if ( FETCH_WCB( *window, Destroy ) )
271 SFG_Window *activeWindow = fgStructure.Window ;
272 /* fgSetWindow ( window ) ; */
273 /* window->Callbacks.Destroy () ; */
274 INVOKE_WCB( *window, Destroy, ( ) );
275 fgSetWindow ( activeWindow ) ;
278 if ( window->Parent != NULL )
279 fgListRemove( &window->Parent->Children, &window->Node );
281 fgListRemove( &fgStructure.Windows, &window->Node );
283 if ( window->ActiveMenu != NULL )
284 fgDeactivateMenu ( window ) ;
286 for ( menu_index = 0; menu_index < 3; menu_index ++ )
288 if ( window->Menu[menu_index] != NULL )
289 window->Menu[menu_index]->ParentWindow = NULL ;
292 if( needToClose == TRUE )
293 fgCloseWindow( window );
295 if ( fgStructure.Window == window )
296 fgStructure.Window = NULL ;
300 * This is a helper static function that removes a menu (given its pointer)
301 * from any windows that can be accessed from a given parent...
303 static void fghRemoveMenuFromWindow( SFG_Window* window, SFG_Menu* menu )
305 SFG_Window *subWindow;
309 * Check if the menu is attached to the current window,
310 * if so, have it detached (by overwriting with a NULL):
313 if( window->Menu[ i ] == menu )
314 window->Menu[ i ] = NULL;
317 * Call this function for all of the window's children recursively:
319 for( subWindow = (SFG_Window *)window->Children.First; subWindow;
320 subWindow = (SFG_Window *)subWindow->Node.Next)
321 fghRemoveMenuFromWindow( subWindow, menu );
325 * This is a static helper function that removes menu references
326 * from another menu, given two pointers to them...
328 static void fghRemoveMenuFromMenu( SFG_Menu* from, SFG_Menu* menu )
330 SFG_MenuEntry *entry;
332 for( entry = (SFG_MenuEntry *)from->Entries.First;
334 entry = (SFG_MenuEntry *)entry->Node.Next )
335 if (entry->SubMenu == menu)
336 entry->SubMenu = NULL;
340 * This function destroys a menu specified by the parameter. All menus
341 * and windows are updated to make sure no ill pointers hang around.
343 void fgDestroyMenu( SFG_Menu* menu )
347 SFG_MenuEntry *entry;
349 assert( menu != NULL );
350 freeglut_assert_ready;
353 * First of all, have all references to this menu removed from all windows:
355 for( window = (SFG_Window *)fgStructure.Windows.First;
357 window = (SFG_Window *)window->Node.Next )
358 fghRemoveMenuFromWindow( window, menu );
361 * Now proceed with removing menu entries that lead to this menu
363 for( from = (SFG_Menu *)fgStructure.Menus.First;
365 from = (SFG_Menu *)from->Node.Next )
366 fghRemoveMenuFromMenu( from, menu );
369 * If the programmer defined a destroy callback, call it
370 * A. Donev: But first make this the active menu
372 if ( menu->Destroy != NULL )
374 SFG_Menu *activeMenu=fgStructure.Menu;
375 fgStructure.Menu = menu;
377 fgStructure.Menu = activeMenu;
381 * Now we are pretty sure the menu is not used anywhere
382 * and that we can remove all of its entries
384 while( (entry = (SFG_MenuEntry *)menu->Entries.First) != NULL )
386 fgListRemove(&menu->Entries, &entry->Node);
396 if ( fgStructure.Window == menu->Window )
397 fgSetWindow ( menu->ParentWindow ) ;
398 fgDestroyWindow ( menu->Window, TRUE ) ;
399 fgListRemove( &fgStructure.Menus, &menu->Node );
400 if( fgStructure.Menu == menu )
401 fgStructure.Menu = NULL;
407 * This function should be called on glutInit(). It will prepare the internal
408 * structure of freeglut to be used in the application. The structure will be
409 * destroyed using fgDestroyStructure() on glutMainLoop() return. In that
410 * case further use of freeglut should be preceeded with a glutInit() call.
412 void fgCreateStructure( void )
415 * We will be needing two lists: the first containing windows,
416 * and the second containing the user-defined menus.
417 * Also, no current window/menu is set, as none has been created yet.
420 fgListInit(&fgStructure.Windows);
421 fgListInit(&fgStructure.Menus);
425 * This function is automatically called on glutMainLoop() return.
426 * It should deallocate and destroy all remnants of previous
427 * glutInit()-enforced structure initialization...
429 void fgDestroyStructure( void )
434 freeglut_assert_ready;
437 * Make sure all windows and menus have been deallocated
439 while( (menu = (SFG_Menu *)fgStructure.Menus.First) != NULL )
440 fgDestroyMenu( menu );
442 while( (window = (SFG_Window *)fgStructure.Windows.First) != NULL )
443 fgDestroyWindow( window, TRUE );
447 * Helper function to enumerate through all registered top-level windows
449 void fgEnumWindows( FGCBenumerator enumCallback, SFG_Enumerator* enumerator )
453 assert( (enumCallback != NULL) && (enumerator != NULL) );
454 freeglut_assert_ready;
457 * Check every of the top-level windows
459 for( window = (SFG_Window *)fgStructure.Windows.First;
461 window = (SFG_Window *)window->Node.Next )
463 enumCallback( window, enumerator );
464 if( enumerator->found == TRUE )
470 * Helper function to enumerate through all a window's subwindows
471 * (single level descent)
473 void fgEnumSubWindows( SFG_Window* window, FGCBenumerator enumCallback,
474 SFG_Enumerator* enumerator )
478 assert( (enumCallback != NULL) && (enumerator != NULL) );
479 freeglut_assert_ready;
481 for( child = (SFG_Window *)window->Children.First;
483 child = (SFG_Window *)child->Node.Next )
485 enumCallback( child, enumerator );
486 if( enumerator->found == TRUE )
492 * A static helper function to look for a window given its handle
494 static void fghcbWindowByHandle( SFG_Window *window,
495 SFG_Enumerator *enumerator )
497 freeglut_return_if_fail( enumerator->found == FALSE );
499 #if TARGET_HOST_UNIX_X11
500 #define WBHANDLE (Window)
501 #elif TARGET_HOST_WIN32
502 #define WBHANDLE (HWND)
506 * Check the window's handle. Hope this works. Looks ugly. That's for sure.
508 if( window->Window.Handle == WBHANDLE (enumerator->data) )
510 enumerator->found = TRUE;
511 enumerator->data = window;
517 * Otherwise, check this window's children
519 fgEnumSubWindows( window, fghcbWindowByHandle, enumerator );
525 * fgWindowByHandle returns a (SFG_Window *) value pointing to the
526 * first window in the queue matching the specified window handle.
527 * The function is defined in freeglut_structure.c file.
529 SFG_Window* fgWindowByHandle
530 #if TARGET_HOST_UNIX_X11
532 #elif TARGET_HOST_WIN32
536 SFG_Enumerator enumerator;
539 * This is easy and makes use of the windows enumeration defined above
541 enumerator.found = FALSE;
542 enumerator.data = (void *)hWindow;
543 fgEnumWindows( fghcbWindowByHandle, &enumerator );
545 if( enumerator.found == TRUE )
546 return( SFG_Window *) enumerator.data;
551 * A static helper function to look for a window given its ID
553 static void fghcbWindowByID( SFG_Window *window, SFG_Enumerator *enumerator )
556 * Make sure we do not overwrite our precious results...
558 freeglut_return_if_fail( enumerator->found == FALSE );
561 * Check the window's handle. Hope this works. Looks ugly. That's for sure.
563 if( window->ID == (int) (enumerator->data) ) /* XXX int/ptr conversion! */
565 enumerator->found = TRUE;
566 enumerator->data = window;
572 * Otherwise, check this window's children
574 fgEnumSubWindows( window, fghcbWindowByID, enumerator );
578 * This function is similiar to the previous one, except it is
579 * looking for a specified (sub)window identifier. The function
580 * is defined in freeglut_structure.c file.
582 SFG_Window* fgWindowByID( int windowID )
584 SFG_Enumerator enumerator;
587 * Uses a method very similiar for fgWindowByHandle...
589 enumerator.found = FALSE;
590 enumerator.data = (void *) windowID; /* XXX int/pointer conversion! */
591 fgEnumWindows( fghcbWindowByID, &enumerator );
592 if( enumerator.found == TRUE )
593 return( SFG_Window *) enumerator.data;
598 * Looks up a menu given its ID. This is easier that fgWindowByXXX
599 * as all menus are placed in a single doubly linked list...
601 SFG_Menu* fgMenuByID( int menuID )
603 SFG_Menu *menu = NULL;
605 freeglut_assert_ready;
608 * It's enough to check all entries in fgStructure.Menus...
610 for( menu = (SFG_Menu *)fgStructure.Menus.First;
612 menu = (SFG_Menu *)menu->Node.Next )
613 if( menu->ID == menuID )
621 void fgListInit(SFG_List *list)
627 void fgListAppend(SFG_List *list, SFG_Node *node)
631 if ( (ln = (SFG_Node *)list->Last) != NULL )
646 void fgListRemove(SFG_List *list, SFG_Node *node)
650 if ( (ln = (SFG_Node *)node->Next) != NULL )
651 ln->Prev = node->Prev;
652 if ( (ln = (SFG_Node *)node->Prev) != NULL )
653 ln->Next = node->Next;
654 if ( (ln = (SFG_Node *)list->First) == node )
655 list->First = node->Next;
656 if ( (ln = (SFG_Node *)list->Last) == node )
657 list->Last = node->Prev;
660 int fgListLength(SFG_List *list)
665 for( node = (SFG_Node *)list->First; node; node = (SFG_Node *)node->Next )
671 /*** END OF FILE ***/