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 );
270 * XXX Since INVOKE_WCB() tests the function pointer, why not make
271 * XXX this unconditional? Overhead is close to nil, and it would
272 * XXX clarify the code by omitting a conditional test.
274 if ( FETCH_WCB( *window, Destroy ) )
276 SFG_Window *activeWindow = fgStructure.Window ;
277 INVOKE_WCB( *window, Destroy, ( ) );
278 fgSetWindow ( activeWindow ) ;
281 if ( window->Parent != NULL )
282 fgListRemove( &window->Parent->Children, &window->Node );
284 fgListRemove( &fgStructure.Windows, &window->Node );
286 if ( window->ActiveMenu != NULL )
287 fgDeactivateMenu ( window ) ;
289 for ( menu_index = 0; menu_index < 3; menu_index ++ )
291 if ( window->Menu[menu_index] != NULL )
292 window->Menu[menu_index]->ParentWindow = NULL ;
295 if( needToClose == TRUE )
296 fgCloseWindow( window );
298 if ( fgStructure.Window == window )
299 fgStructure.Window = NULL ;
303 * This is a helper static function that removes a menu (given its pointer)
304 * from any windows that can be accessed from a given parent...
306 static void fghRemoveMenuFromWindow( SFG_Window* window, SFG_Menu* menu )
308 SFG_Window *subWindow;
312 * Check if the menu is attached to the current window,
313 * if so, have it detached (by overwriting with a NULL):
316 if( window->Menu[ i ] == menu )
317 window->Menu[ i ] = NULL;
320 * Call this function for all of the window's children recursively:
322 for( subWindow = (SFG_Window *)window->Children.First; subWindow;
323 subWindow = (SFG_Window *)subWindow->Node.Next)
324 fghRemoveMenuFromWindow( subWindow, menu );
328 * This is a static helper function that removes menu references
329 * from another menu, given two pointers to them...
331 static void fghRemoveMenuFromMenu( SFG_Menu* from, SFG_Menu* menu )
333 SFG_MenuEntry *entry;
335 for( entry = (SFG_MenuEntry *)from->Entries.First;
337 entry = (SFG_MenuEntry *)entry->Node.Next )
338 if (entry->SubMenu == menu)
339 entry->SubMenu = NULL;
343 * This function destroys a menu specified by the parameter. All menus
344 * and windows are updated to make sure no ill pointers hang around.
346 void fgDestroyMenu( SFG_Menu* menu )
350 SFG_MenuEntry *entry;
352 assert( menu != NULL );
353 freeglut_assert_ready;
356 * First of all, have all references to this menu removed from all windows:
358 for( window = (SFG_Window *)fgStructure.Windows.First;
360 window = (SFG_Window *)window->Node.Next )
361 fghRemoveMenuFromWindow( window, menu );
364 * Now proceed with removing menu entries that lead to this menu
366 for( from = (SFG_Menu *)fgStructure.Menus.First;
368 from = (SFG_Menu *)from->Node.Next )
369 fghRemoveMenuFromMenu( from, menu );
372 * If the programmer defined a destroy callback, call it
373 * A. Donev: But first make this the active menu
375 if ( menu->Destroy != NULL )
377 SFG_Menu *activeMenu=fgStructure.Menu;
378 fgStructure.Menu = menu;
380 fgStructure.Menu = activeMenu;
384 * Now we are pretty sure the menu is not used anywhere
385 * and that we can remove all of its entries
387 while( (entry = (SFG_MenuEntry *)menu->Entries.First) != NULL )
389 fgListRemove(&menu->Entries, &entry->Node);
399 if ( fgStructure.Window == menu->Window )
400 fgSetWindow ( menu->ParentWindow ) ;
401 fgDestroyWindow ( menu->Window, TRUE ) ;
402 fgListRemove( &fgStructure.Menus, &menu->Node );
403 if( fgStructure.Menu == menu )
404 fgStructure.Menu = NULL;
410 * This function should be called on glutInit(). It will prepare the internal
411 * structure of freeglut to be used in the application. The structure will be
412 * destroyed using fgDestroyStructure() on glutMainLoop() return. In that
413 * case further use of freeglut should be preceeded with a glutInit() call.
415 void fgCreateStructure( void )
418 * We will be needing two lists: the first containing windows,
419 * and the second containing the user-defined menus.
420 * Also, no current window/menu is set, as none has been created yet.
423 fgListInit(&fgStructure.Windows);
424 fgListInit(&fgStructure.Menus);
428 * This function is automatically called on glutMainLoop() return.
429 * It should deallocate and destroy all remnants of previous
430 * glutInit()-enforced structure initialization...
432 void fgDestroyStructure( void )
437 freeglut_assert_ready;
440 * Make sure all windows and menus have been deallocated
442 while( (menu = (SFG_Menu *)fgStructure.Menus.First) != NULL )
443 fgDestroyMenu( menu );
445 while( (window = (SFG_Window *)fgStructure.Windows.First) != NULL )
446 fgDestroyWindow( window, TRUE );
450 * Helper function to enumerate through all registered top-level windows
452 void fgEnumWindows( FGCBenumerator enumCallback, SFG_Enumerator* enumerator )
456 assert( (enumCallback != NULL) && (enumerator != NULL) );
457 freeglut_assert_ready;
460 * Check every of the top-level windows
462 for( window = (SFG_Window *)fgStructure.Windows.First;
464 window = (SFG_Window *)window->Node.Next )
466 enumCallback( window, enumerator );
467 if( enumerator->found == TRUE )
473 * Helper function to enumerate through all a window's subwindows
474 * (single level descent)
476 void fgEnumSubWindows( SFG_Window* window, FGCBenumerator enumCallback,
477 SFG_Enumerator* enumerator )
481 assert( (enumCallback != NULL) && (enumerator != NULL) );
482 freeglut_assert_ready;
484 for( child = (SFG_Window *)window->Children.First;
486 child = (SFG_Window *)child->Node.Next )
488 enumCallback( child, enumerator );
489 if( enumerator->found == TRUE )
495 * A static helper function to look for a window given its handle
497 static void fghcbWindowByHandle( SFG_Window *window,
498 SFG_Enumerator *enumerator )
500 freeglut_return_if_fail( enumerator->found == FALSE );
502 #if TARGET_HOST_UNIX_X11
503 #define WBHANDLE (Window)
504 #elif TARGET_HOST_WIN32
505 #define WBHANDLE (HWND)
509 * Check the window's handle. Hope this works. Looks ugly. That's for sure.
511 if( window->Window.Handle == WBHANDLE (enumerator->data) )
513 enumerator->found = TRUE;
514 enumerator->data = window;
520 * Otherwise, check this window's children
522 fgEnumSubWindows( window, fghcbWindowByHandle, enumerator );
528 * fgWindowByHandle returns a (SFG_Window *) value pointing to the
529 * first window in the queue matching the specified window handle.
530 * The function is defined in freeglut_structure.c file.
532 SFG_Window* fgWindowByHandle
533 #if TARGET_HOST_UNIX_X11
535 #elif TARGET_HOST_WIN32
539 SFG_Enumerator enumerator;
542 * This is easy and makes use of the windows enumeration defined above
544 enumerator.found = FALSE;
545 enumerator.data = (void *)hWindow;
546 fgEnumWindows( fghcbWindowByHandle, &enumerator );
548 if( enumerator.found == TRUE )
549 return( SFG_Window *) enumerator.data;
554 * A static helper function to look for a window given its ID
556 static void fghcbWindowByID( SFG_Window *window, SFG_Enumerator *enumerator )
559 * Make sure we do not overwrite our precious results...
561 freeglut_return_if_fail( enumerator->found == FALSE );
564 * Check the window's handle. Hope this works. Looks ugly. That's for sure.
566 if( window->ID == (int) (enumerator->data) ) /* XXX int/ptr conversion! */
568 enumerator->found = TRUE;
569 enumerator->data = window;
575 * Otherwise, check this window's children
577 fgEnumSubWindows( window, fghcbWindowByID, enumerator );
581 * This function is similiar to the previous one, except it is
582 * looking for a specified (sub)window identifier. The function
583 * is defined in freeglut_structure.c file.
585 SFG_Window* fgWindowByID( int windowID )
587 SFG_Enumerator enumerator;
590 * Uses a method very similiar for fgWindowByHandle...
592 enumerator.found = FALSE;
593 enumerator.data = (void *) windowID; /* XXX int/pointer conversion! */
594 fgEnumWindows( fghcbWindowByID, &enumerator );
595 if( enumerator.found == TRUE )
596 return( SFG_Window *) enumerator.data;
601 * Looks up a menu given its ID. This is easier that fgWindowByXXX
602 * as all menus are placed in a single doubly linked list...
604 SFG_Menu* fgMenuByID( int menuID )
606 SFG_Menu *menu = NULL;
608 freeglut_assert_ready;
611 * It's enough to check all entries in fgStructure.Menus...
613 for( menu = (SFG_Menu *)fgStructure.Menus.First;
615 menu = (SFG_Menu *)menu->Node.Next )
616 if( menu->ID == menuID )
624 void fgListInit(SFG_List *list)
630 void fgListAppend(SFG_List *list, SFG_Node *node)
634 if ( (ln = (SFG_Node *)list->Last) != NULL )
649 void fgListRemove(SFG_List *list, SFG_Node *node)
653 if ( (ln = (SFG_Node *)node->Next) != NULL )
654 ln->Prev = node->Prev;
655 if ( (ln = (SFG_Node *)node->Prev) != NULL )
656 ln->Next = node->Next;
657 if ( (ln = (SFG_Node *)list->First) == node )
658 list->First = node->Next;
659 if ( (ln = (SFG_Node *)list->Last) == node )
660 list->Last = node->Prev;
663 int fgListLength(SFG_List *list)
668 for( node = (SFG_Node *)list->First; node; node = (SFG_Node *)node->Next )
674 /*** END OF FILE ***/