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,
64 int x, int y, int w, int h, GLboolean gameMode )
67 * Have the window object created
69 SFG_Window *window = (SFG_Window *)calloc( sizeof(SFG_Window), 1 );
73 * If the freeglut internals haven't been initialized yet,
74 * do it now. Hack's idea courtesy of Chris Purnell...
76 if( !fgState.Time.Set )
77 glutInit( &fakeArgc, NULL );
80 * Initialize the object properties
82 window->ID = ++fgStructure.WindowID;
84 fgListInit( &window->Children );
87 fgListAppend( &parent->Children, &window->Node );
88 window->Parent = parent;
91 fgListAppend( &fgStructure.Windows, &window->Node );
94 * Set the default mouse cursor and reset the modifiers value
96 window->State.Cursor = GLUT_CURSOR_INHERIT;
97 window->State.Modifiers = 0xffffffff;
99 window->IsMenu = fgState.BuildingAMenu ;
102 * Open the window now. The fgOpenWindow() function is system
103 * dependant, and resides in freeglut_window.c. Uses fgState.
105 fgOpenWindow( window, title, x, y, w, h, gameMode,
106 (parent != NULL) ? TRUE : FALSE );
112 * This private function creates a menu and adds it to the menus list
114 SFG_Menu* fgCreateMenu( FGCBmenu menuCallback )
116 int x = 100, y = 100, w = 100, h = 100 ;
117 SFG_Window *current_window = fgStructure.Window ;
120 * Have the menu object created
122 SFG_Menu* menu = (SFG_Menu *)calloc( sizeof(SFG_Menu), 1 );
126 * If the freeglut internals haven't been initialized yet,
127 * do it now. Hack's idea courtesy of Chris Purnell...
129 if( !fgState.Time.Set )
130 glutInit( &fakeArgc, NULL );
132 menu->ParentWindow = fgStructure.Window ;
135 * Create a window for the menu to reside in. Set the
136 * global variable BuildingAMenu to true so we can ensure
137 * it is created without decorations.
139 fgState.BuildingAMenu = TRUE ;
141 fgCreateWindow ( NULL, NULL, x, y, w, h, FALSE ) ;
142 menu->Window = fgStructure.Window ;
143 glutDisplayFunc ( fgDisplayMenu ) ;
146 * While BuildingAMenu is true, all windows built have no decorations.
147 * That's not a good default behavior, so let's set it false again.
149 fgState.BuildingAMenu = FALSE ;
151 glutHideWindow () ; /* Hide the window for now */
152 fgSetWindow ( current_window ) ;
155 * Initialize the object properties:
157 menu->ID = ++fgStructure.MenuID;
158 menu->Callback = menuCallback;
159 menu->ActiveEntry = NULL ;
161 fgListInit( &menu->Entries );
162 fgListAppend( &fgStructure.Menus, &menu->Node );
165 * Newly created menus implicitly become current ones
167 fgStructure.Menu = menu;
173 * Linked list of windows to destroy ... this is so we don't destroy a
174 * window from the middle of its callback. Some C compilers take an
175 * extremely dim view of this.
178 static SFG_WindowList* WindowsToDestroy = (SFG_WindowList*)NULL ;
181 * Function to add a window to the linked list of windows to destroy.
182 * Subwindows are automatically added because they hang from the window
185 void fgAddToWindowDestroyList ( SFG_Window* window, GLboolean needToClose )
187 SFG_WindowList *new_list_entry =
188 (SFG_WindowList*)malloc ( sizeof(SFG_WindowList) ) ;
189 new_list_entry->window = window ;
190 new_list_entry->needToClose = needToClose ;
191 new_list_entry->next = WindowsToDestroy ;
192 WindowsToDestroy = new_list_entry ;
195 * Check if the window is the current one...
197 if ( fgStructure.Window == window )
198 fgStructure.Window = NULL;
201 * If the destroyed window has the highest window ID number, decrement
202 * the window ID number.
204 * XXX Do we REALLY want to *ever* recycle window IDs? Integers are
205 * XXX plentiful, and clients may rely upon the implied promise in
206 * XXX the GLUT docs to not recycle these. (I can't remember if it
209 * XXX If we *do* want to do this, we should actually recompute the
210 * XXX highest window-ID; the new highest may not in fact be one less
211 * XXX than what we have just deleted.
213 if ( window->ID == fgStructure.WindowID )
214 fgStructure.WindowID-- ;
217 * Check the execution state. If this has been called from
218 * "glutDestroyWindow", a statement in that function will reset the
219 * "ExecState" after this function returns.
221 if ( fgState.ActionOnWindowClose != GLUT_ACTION_CONTINUE_EXECUTION )
224 * Set the execution state flag to drop out of the main loop.
226 if ( fgState.ActionOnWindowClose == GLUT_ACTION_EXIT )
227 fgState.ExecState = GLUT_EXEC_STATE_STOP ;
232 * Function to close down all the windows in the "WindowsToDestroy" list
234 void fgCloseWindows ()
236 SFG_WindowList *window_ptr = WindowsToDestroy ;
237 WindowsToDestroy = (SFG_WindowList*)NULL ;
238 /* In case the destroy callbacks cause more windows to be closed */
242 SFG_WindowList *next = window_ptr->next ;
243 fgDestroyWindow ( window_ptr->window, window_ptr->needToClose ) ;
244 free ( window_ptr ) ;
249 window_ptr = WindowsToDestroy ;
250 WindowsToDestroy = (SFG_WindowList*)NULL ;
256 * This function destroys a window and all of its subwindows. Actually,
257 * another function, defined in freeglut_window.c is called, but this is
258 * a whole different story...
260 void fgDestroyWindow( SFG_Window* window, GLboolean needToClose )
262 SFG_Window* subWindow;
265 assert( window != NULL );
266 freeglut_assert_ready;
268 while ( (subWindow = (SFG_Window *)window->Children.First) != NULL )
269 fgDestroyWindow( subWindow, needToClose );
271 if ( window->Callbacks.Destroy != NULL )
273 SFG_Window *activeWindow = fgStructure.Window ;
274 fgSetWindow ( window ) ;
275 window->Callbacks.Destroy () ;
276 fgSetWindow ( activeWindow ) ;
279 if ( window->Parent != NULL )
280 fgListRemove( &window->Parent->Children, &window->Node );
282 fgListRemove( &fgStructure.Windows, &window->Node );
284 if ( window->ActiveMenu != NULL )
285 fgDeactivateMenu ( window ) ;
287 for ( menu_index = 0; menu_index < 3; menu_index ++ )
289 if ( window->Menu[menu_index] != NULL )
290 window->Menu[menu_index]->ParentWindow = NULL ;
293 if( needToClose == TRUE )
294 fgCloseWindow( window );
296 if ( fgStructure.Window == window )
297 fgStructure.Window = NULL ;
301 * This is a helper static function that removes a menu (given its pointer)
302 * from any windows that can be accessed from a given parent...
304 static void fghRemoveMenuFromWindow( SFG_Window* window, SFG_Menu* menu )
306 SFG_Window *subWindow;
310 * Check if the menu is attached to the current window,
311 * if so, have it detached (by overwriting with a NULL):
314 if( window->Menu[ i ] == menu )
315 window->Menu[ i ] = NULL;
318 * Call this function for all of the window's children recursively:
320 for( subWindow = (SFG_Window *)window->Children.First; subWindow;
321 subWindow = (SFG_Window *)subWindow->Node.Next)
322 fghRemoveMenuFromWindow( subWindow, menu );
326 * This is a static helper function that removes menu references
327 * from another menu, given two pointers to them...
329 static void fghRemoveMenuFromMenu( SFG_Menu* from, SFG_Menu* menu )
331 SFG_MenuEntry *entry;
333 for( entry = (SFG_MenuEntry *)from->Entries.First;
335 entry = (SFG_MenuEntry *)entry->Node.Next )
336 if (entry->SubMenu == menu)
337 entry->SubMenu = NULL;
341 * This function destroys a menu specified by the parameter. All menus
342 * and windows are updated to make sure no ill pointers hang around.
344 void fgDestroyMenu( SFG_Menu* menu )
348 SFG_MenuEntry *entry;
350 assert( menu != NULL );
351 freeglut_assert_ready;
354 * First of all, have all references to this menu removed from all windows:
356 for( window = (SFG_Window *)fgStructure.Windows.First;
358 window = (SFG_Window *)window->Node.Next )
359 fghRemoveMenuFromWindow( window, menu );
362 * Now proceed with removing menu entries that lead to this menu
364 for( from = (SFG_Menu *)fgStructure.Menus.First;
366 from = (SFG_Menu *)from->Node.Next )
367 fghRemoveMenuFromMenu( from, menu );
370 * If the programmer defined a destroy callback, call it
371 * A. Donev: But first make this the active menu
373 if ( menu->Destroy != NULL )
375 SFG_Menu *activeMenu=fgStructure.Menu;
376 fgStructure.Menu = menu;
378 fgStructure.Menu = activeMenu;
382 * Now we are pretty sure the menu is not used anywhere
383 * and that we can remove all of its entries
385 while( (entry = (SFG_MenuEntry *)menu->Entries.First) != NULL )
387 fgListRemove(&menu->Entries, &entry->Node);
397 if ( fgStructure.Window == menu->Window )
398 fgSetWindow ( menu->ParentWindow ) ;
399 fgDestroyWindow ( menu->Window, TRUE ) ;
400 fgListRemove( &fgStructure.Menus, &menu->Node );
401 if( fgStructure.Menu == menu )
402 fgStructure.Menu = NULL;
408 * This function should be called on glutInit(). It will prepare the internal
409 * structure of freeglut to be used in the application. The structure will be
410 * destroyed using fgDestroyStructure() on glutMainLoop() return. In that
411 * case further use of freeglut should be preceeded with a glutInit() call.
413 void fgCreateStructure( void )
416 * We will be needing two lists: the first containing windows,
417 * and the second containing the user-defined menus.
418 * Also, no current window/menu is set, as none has been created yet.
421 fgListInit(&fgStructure.Windows);
422 fgListInit(&fgStructure.Menus);
426 * This function is automatically called on glutMainLoop() return.
427 * It should deallocate and destroy all remnants of previous
428 * glutInit()-enforced structure initialization...
430 void fgDestroyStructure( void )
435 freeglut_assert_ready;
438 * Make sure all windows and menus have been deallocated
440 while( (menu = (SFG_Menu *)fgStructure.Menus.First) != NULL )
441 fgDestroyMenu( menu );
443 while( (window = (SFG_Window *)fgStructure.Windows.First) != NULL )
444 fgDestroyWindow( window, TRUE );
448 * Helper function to enumerate through all registered top-level windows
450 void fgEnumWindows( FGCBenumerator enumCallback, SFG_Enumerator* enumerator )
454 assert( (enumCallback != NULL) && (enumerator != NULL) );
455 freeglut_assert_ready;
458 * Check every of the top-level windows
460 for( window = (SFG_Window *)fgStructure.Windows.First;
462 window = (SFG_Window *)window->Node.Next )
464 enumCallback( window, enumerator );
465 if( enumerator->found == TRUE )
471 * Helper function to enumerate through all a window's subwindows
472 * (single level descent)
474 void fgEnumSubWindows( SFG_Window* window, FGCBenumerator enumCallback,
475 SFG_Enumerator* enumerator )
479 assert( (enumCallback != NULL) && (enumerator != NULL) );
480 freeglut_assert_ready;
482 for( child = (SFG_Window *)window->Children.First;
484 child = (SFG_Window *)child->Node.Next )
486 enumCallback( child, enumerator );
487 if( enumerator->found == TRUE )
493 * A static helper function to look for a window given its handle
495 static void fghcbWindowByHandle( SFG_Window *window,
496 SFG_Enumerator *enumerator )
498 freeglut_return_if_fail( enumerator->found == FALSE );
500 #if TARGET_HOST_UNIX_X11
501 #define WBHANDLE (Window)
502 #elif TARGET_HOST_WIN32
503 #define WBHANDLE (HWND)
507 * Check the window's handle. Hope this works. Looks ugly. That's for sure.
509 if( window->Window.Handle == WBHANDLE (enumerator->data) )
511 enumerator->found = TRUE;
512 enumerator->data = window;
518 * Otherwise, check this window's children
520 fgEnumSubWindows( window, fghcbWindowByHandle, enumerator );
526 * fgWindowByHandle returns a (SFG_Window *) value pointing to the
527 * first window in the queue matching the specified window handle.
528 * The function is defined in freeglut_structure.c file.
530 SFG_Window* fgWindowByHandle
531 #if TARGET_HOST_UNIX_X11
533 #elif TARGET_HOST_WIN32
537 SFG_Enumerator enumerator;
540 * This is easy and makes use of the windows enumeration defined above
542 enumerator.found = FALSE;
543 enumerator.data = (void *)hWindow;
544 fgEnumWindows( fghcbWindowByHandle, &enumerator );
546 if( enumerator.found == TRUE )
547 return( SFG_Window *) enumerator.data;
552 * A static helper function to look for a window given its ID
554 static void fghcbWindowByID( SFG_Window *window, SFG_Enumerator *enumerator )
557 * Make sure we do not overwrite our precious results...
559 freeglut_return_if_fail( enumerator->found == FALSE );
562 * Check the window's handle. Hope this works. Looks ugly. That's for sure.
564 if( window->ID == (int) (enumerator->data) )
566 enumerator->found = TRUE;
567 enumerator->data = window;
573 * Otherwise, check this window's children
575 fgEnumSubWindows( window, fghcbWindowByID, enumerator );
579 * This function is similiar to the previous one, except it is
580 * looking for a specified (sub)window identifier. The function
581 * is defined in freeglut_structure.c file.
583 SFG_Window* fgWindowByID( int windowID )
585 SFG_Enumerator enumerator;
588 * Uses a method very similiar for fgWindowByHandle...
590 enumerator.found = FALSE;
591 enumerator.data = (void *) windowID;
592 fgEnumWindows( fghcbWindowByID, &enumerator );
593 if( enumerator.found == TRUE )
594 return( SFG_Window *) enumerator.data;
599 * Looks up a menu given its ID. This is easier that fgWindowByXXX
600 * as all menus are placed in a single doubly linked list...
602 SFG_Menu* fgMenuByID( int menuID )
604 SFG_Menu *menu = NULL;
606 freeglut_assert_ready;
609 * It's enough to check all entries in fgStructure.Menus...
611 for( menu = (SFG_Menu *)fgStructure.Menus.First; menu; menu = (SFG_Menu *)menu->Node.Next )
614 * Does the ID number match?
616 if( menu->ID == menuID )
621 * We have not found the requested menu ID
629 void fgListInit(SFG_List *list)
635 void fgListAppend(SFG_List *list, SFG_Node *node)
639 if ( (ln = (SFG_Node *)list->Last) != NULL )
654 void fgListRemove(SFG_List *list, SFG_Node *node)
658 if ( (ln = (SFG_Node *)node->Next) != NULL )
659 ln->Prev = node->Prev;
660 if ( (ln = (SFG_Node *)node->Prev) != NULL )
661 ln->Next = node->Next;
662 if ( (ln = (SFG_Node *)list->First) == node )
663 list->First = node->Next;
664 if ( (ln = (SFG_Node *)list->Last) == node )
665 list->Last = node->Prev;
668 int fgListLength(SFG_List *list)
673 for( node = (SFG_Node *)list->First; node; node = (SFG_Node *)node->Next )
679 /*** END OF FILE ***/