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.
28 #include <GL/freeglut.h>
29 #include "fg_internal.h"
31 /* -- GLOBAL EXPORTS ------------------------------------------------------- */
34 * The SFG_Structure container holds information about windows and menus
35 * created between glutInit() and glutMainLoop() return.
38 SFG_Structure fgStructure = { { NULL, NULL }, /* The list of windows */
39 { NULL, NULL }, /* The list of menus */
40 { NULL, NULL }, /* Windows to Destroy list */
41 NULL, /* The current window */
42 NULL, /* The current menu */
43 NULL, /* The menu OpenGL context */
44 NULL, /* The game mode window */
45 0, /* The current new window ID */
46 0 }; /* The current new menu ID */
49 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */
51 extern void fgPlatformCreateWindow ( SFG_Window *window );
52 extern void fghDefaultReshape(int width, int height, FGCBUserData userData);
54 static void fghClearCallBacks( SFG_Window *window )
59 for( i = 0; i < TOTAL_CALLBACKS; ++i )
61 window->CallBacks[ i ] = NULL;
62 window->CallbackDatas[ i ] = NULL;
68 * This private function creates, opens and adds to the hierarchy
69 * a freeglut window complete with OpenGL context and stuff...
71 * If parent is set to NULL, the window created will be a topmost one.
73 SFG_Window* fgCreateWindow( SFG_Window* parent, const char* title,
74 GLboolean positionUse, int x, int y,
75 GLboolean sizeUse, int w, int h,
76 GLboolean gameMode, GLboolean isMenu )
78 /* Have the window object created */
79 SFG_Window *window = (SFG_Window *)calloc( 1, sizeof(SFG_Window) );
83 fgError( "Out of memory. Could not create window." );
86 fgPlatformCreateWindow ( window );
88 fghClearCallBacks( window );
89 SET_WCB( *window, Reshape, fghDefaultReshape, NULL);
91 /* Initialize the object properties */
92 window->ID = ++fgStructure.WindowID;
94 fgListInit( &window->Children );
97 fgListAppend( &parent->Children, &window->Node );
98 window->Parent = parent;
101 fgListAppend( &fgStructure.Windows, &window->Node );
103 /* Set the default mouse cursor */
104 window->State.Cursor = GLUT_CURSOR_INHERIT;
106 /* Mark window as menu if a menu is to be created */
107 window->IsMenu = isMenu;
110 * Open the window now. The fgOpenWindow() function is system
111 * dependent, and resides in fg_window.c. Uses fgState.
113 fgOpenWindow( window, title, positionUse, x, y, sizeUse, w, h, gameMode,
114 (GLboolean)(parent ? GL_TRUE : GL_FALSE) );
120 * This private function creates a menu and adds it to the menus list
122 SFG_Menu* fgCreateMenu( FGCBMenuUC menuCallback, FGCBUserData userData )
124 SFG_Window *current_window = fgStructure.CurrentWindow;
126 /* Have the menu object created */
127 SFG_Menu* menu = (SFG_Menu *)calloc( sizeof(SFG_Menu), 1 );
129 menu->ParentWindow = NULL;
131 /* Create a window for the menu to reside in. */
132 fgCreateWindow( NULL, "freeglut menu", GL_FALSE, 0, 0, GL_FALSE, 0, 0,
134 menu->Window = fgStructure.CurrentWindow;
135 glutDisplayFunc( fgDisplayMenu );
137 fgSetWindow( current_window );
139 /* Initialize the object properties: */
140 menu->ID = ++fgStructure.MenuID;
141 menu->Callback = menuCallback;
142 menu->CallbackData = userData;
143 menu->ActiveEntry = NULL;
144 menu->Font = fgState.MenuFont;
146 fgListInit( &menu->Entries );
147 fgListAppend( &fgStructure.Menus, &menu->Node );
149 /* Newly created menus implicitly become current ones */
150 fgStructure.CurrentMenu = menu;
156 * Function to add a window to the linked list of windows to destroy.
157 * Subwindows are automatically added because they hang from the window
160 void fgAddToWindowDestroyList( SFG_Window* window )
162 SFG_WindowList *new_list_entry =
163 ( SFG_WindowList* )malloc( sizeof(SFG_WindowList ) );
164 new_list_entry->window = window;
165 fgListAppend( &fgStructure.WindowsToDestroy, &new_list_entry->node );
167 /* Check if the window is the current one... */
168 if( fgStructure.CurrentWindow == window )
169 fgStructure.CurrentWindow = NULL;
172 * Clear all window callbacks except Destroy, which will
173 * be invoked later. Right now, we are potentially carrying
174 * out a freeglut operation at the behest of a client callback,
175 * so we are reluctant to re-enter the client with the Destroy
176 * callback, right now. The others are all wiped out, however,
177 * to ensure that they are no longer called after this point.
180 FGCBDestroyUC destroy = (FGCBDestroyUC)FETCH_WCB( *window, Destroy );
181 FGCBUserData destroyData = FETCH_USER_DATA_WCB( *window, Destroy );
182 fghClearCallBacks( window );
183 SET_WCB( *window, Destroy, destroy, destroyData );
188 * Function to close down all the windows in the "WindowsToDestroy" list
190 void fgCloseWindows( )
192 while( fgStructure.WindowsToDestroy.First )
194 SFG_WindowList *window_ptr = fgStructure.WindowsToDestroy.First;
195 fgDestroyWindow( window_ptr->window );
196 fgListRemove( &fgStructure.WindowsToDestroy, &window_ptr->node );
202 * This function destroys a window and all of its subwindows. Actually,
203 * another function, defined in fg_window.c is called, but this is
204 * a whole different story...
206 void fgDestroyWindow( SFG_Window* window )
208 FREEGLUT_INTERNAL_ERROR_EXIT ( window, "Window destroy function called with null window",
211 while( window->Children.First )
212 fgDestroyWindow( ( SFG_Window * )window->Children.First );
215 SFG_Window *activeWindow = fgStructure.CurrentWindow;
216 INVOKE_WCB( *window, Destroy, ( ) );
217 fgSetWindow( activeWindow );
221 fgListRemove( &window->Parent->Children, &window->Node );
223 fgListRemove( &fgStructure.Windows, &window->Node );
225 if( window->ActiveMenu )
226 fgDeactivateMenu( window );
228 fghClearCallBacks( window );
229 fgCloseWindow( window );
231 if( fgStructure.CurrentWindow == window )
232 fgStructure.CurrentWindow = NULL;
236 * This is a helper static function that removes a menu (given its pointer)
237 * from any windows that can be accessed from a given parent...
239 static void fghRemoveMenuFromWindow( SFG_Window* window, SFG_Menu* menu )
241 SFG_Window *subWindow;
244 /* Check whether this is the active menu in the window */
245 if ( menu == window->ActiveMenu )
246 window->ActiveMenu = NULL ;
249 * Check if the menu is attached to the current window,
250 * if so, have it detached (by overwriting with a NULL):
252 for( i = 0; i < FREEGLUT_MAX_MENUS; i++ )
253 if( window->Menu[ i ] == menu )
254 window->Menu[ i ] = NULL;
256 /* Call this function for all of the window's children recursively: */
257 for( subWindow = (SFG_Window *)window->Children.First;
259 subWindow = (SFG_Window *)subWindow->Node.Next)
260 fghRemoveMenuFromWindow( subWindow, menu );
264 * This is a static helper function that removes menu references
265 * from another menu, given two pointers to them...
267 static void fghRemoveMenuFromMenu( SFG_Menu* from, SFG_Menu* menu )
269 SFG_MenuEntry *entry;
271 for( entry = (SFG_MenuEntry *)from->Entries.First;
273 entry = ( SFG_MenuEntry * )entry->Node.Next )
274 if( entry->SubMenu == menu )
275 entry->SubMenu = NULL;
279 * This function destroys a menu specified by the parameter. All menus
280 * and windows are updated to make sure no ill pointers hang around.
282 void fgDestroyMenu( SFG_Menu* menu )
287 FREEGLUT_INTERNAL_ERROR_EXIT ( menu, "Menu destroy function called with null menu",
290 /* First of all, have all references to this menu removed from all windows: */
291 for( window = (SFG_Window *)fgStructure.Windows.First;
293 window = (SFG_Window *)window->Node.Next )
294 fghRemoveMenuFromWindow( window, menu );
296 /* Now proceed with removing menu entries that lead to this menu */
297 for( from = ( SFG_Menu * )fgStructure.Menus.First;
299 from = ( SFG_Menu * )from->Node.Next )
300 fghRemoveMenuFromMenu( from, menu );
303 * If the programmer defined a destroy callback, call it
304 * A. Donev: But first make this the active menu
308 SFG_Menu *activeMenu=fgStructure.CurrentMenu;
309 fgStructure.CurrentMenu = menu;
310 menu->Destroy( menu->DestroyData );
311 fgStructure.CurrentMenu = activeMenu;
315 * Now we are pretty sure the menu is not used anywhere
316 * and that we can remove all of its entries
318 while( menu->Entries.First )
320 SFG_MenuEntry *entry = ( SFG_MenuEntry * ) menu->Entries.First;
322 fgListRemove( &menu->Entries, &entry->Node );
331 if( fgStructure.CurrentWindow == menu->Window )
333 fgDestroyWindow( menu->Window );
334 fgListRemove( &fgStructure.Menus, &menu->Node );
335 if( fgStructure.CurrentMenu == menu )
336 fgStructure.CurrentMenu = NULL;
342 * This function should be called on glutInit(). It will prepare the internal
343 * structure of freeglut to be used in the application. The structure will be
344 * destroyed using fgDestroyStructure() on glutMainLoop() return. In that
345 * case further use of freeglut should be preceded with a glutInit() call.
347 void fgCreateStructure( void )
350 * We will be needing two lists: the first containing windows,
351 * and the second containing the user-defined menus.
352 * Also, no current window/menu is set, as none has been created yet.
355 fgListInit(&fgStructure.Windows);
356 fgListInit(&fgStructure.Menus);
357 fgListInit(&fgStructure.WindowsToDestroy);
359 fgStructure.CurrentWindow = NULL;
360 fgStructure.CurrentMenu = NULL;
361 fgStructure.MenuContext = NULL;
362 fgStructure.GameModeWindow = NULL;
363 fgStructure.WindowID = 0;
364 fgStructure.MenuID = 0;
368 * This function is automatically called on glutMainLoop() return.
369 * It should deallocate and destroy all remnants of previous
370 * glutInit()-enforced structure initialization...
372 void fgDestroyStructure( void )
374 /* Clean up the WindowsToDestroy list. */
377 /* Make sure all windows and menus have been deallocated */
378 while( fgStructure.Menus.First )
379 fgDestroyMenu( ( SFG_Menu * )fgStructure.Menus.First );
381 while( fgStructure.Windows.First )
382 fgDestroyWindow( ( SFG_Window * )fgStructure.Windows.First );
386 * Helper function to enumerate through all registered top-level windows
388 void fgEnumWindows( FGCBWindowEnumerator enumCallback, SFG_Enumerator* enumerator )
392 FREEGLUT_INTERNAL_ERROR_EXIT ( enumCallback && enumerator,
393 "Enumerator or callback missing from window enumerator call",
396 /* Check every of the top-level windows */
397 for( window = ( SFG_Window * )fgStructure.Windows.First;
399 window = ( SFG_Window * )window->Node.Next )
401 enumCallback( window, enumerator );
402 if( enumerator->found )
408 * Helper function to enumerate through all registered top-level windows
410 void fgEnumMenus( FGCBMenuEnumerator enumCallback, SFG_Enumerator* enumerator )
414 FREEGLUT_INTERNAL_ERROR_EXIT ( enumCallback && enumerator,
415 "Enumerator or callback missing from window enumerator call",
418 /* It's enough to check all entries in fgStructure.Menus... */
419 for( menu = (SFG_Menu *)fgStructure.Menus.First;
421 menu = (SFG_Menu *)menu->Node.Next )
423 enumCallback( menu, enumerator );
424 if( enumerator->found )
430 * Helper function to enumerate through all a window's subwindows
431 * (single level descent)
433 void fgEnumSubWindows( SFG_Window* window, FGCBWindowEnumerator enumCallback,
434 SFG_Enumerator* enumerator )
438 FREEGLUT_INTERNAL_ERROR_EXIT ( enumCallback && enumerator,
439 "Enumerator or callback missing from subwindow enumerator call",
440 "fgEnumSubWindows" );
441 FREEGLUT_INTERNAL_ERROR_EXIT_IF_NOT_INITIALISED ( "Window Enumeration" );
443 for( child = ( SFG_Window * )window->Children.First;
445 child = ( SFG_Window * )child->Node.Next )
447 enumCallback( child, enumerator );
448 if( enumerator->found )
454 * A static helper function to look for a window given its handle
456 static void fghcbWindowByHandle( SFG_Window *window,
457 SFG_Enumerator *enumerator )
459 if ( enumerator->found )
462 /* Check the window's handle. Hope this works. Looks ugly. That's for sure. */
463 if( window->Window.Handle == (SFG_WindowHandleType) (enumerator->data) )
465 enumerator->found = GL_TRUE;
466 enumerator->data = window;
471 /* Otherwise, check this window's children */
472 fgEnumSubWindows( window, fghcbWindowByHandle, enumerator );
476 * fgWindowByHandle returns a (SFG_Window *) value pointing to the
477 * first window in the queue matching the specified window handle.
478 * The function is defined in fg_structure.c file.
480 SFG_Window* fgWindowByHandle ( SFG_WindowHandleType hWindow )
482 SFG_Enumerator enumerator;
484 /* This is easy and makes use of the windows enumeration defined above */
485 enumerator.found = GL_FALSE;
486 enumerator.data = (void *)hWindow;
487 fgEnumWindows( fghcbWindowByHandle, &enumerator );
489 if( enumerator.found )
490 return( SFG_Window *) enumerator.data;
495 * A static helper function to look for a window given its ID
497 static void fghcbWindowByID( SFG_Window *window, SFG_Enumerator *enumerator )
499 /* Make sure we do not overwrite our precious results... */
500 if( enumerator->found )
503 /* Check the window's handle. Hope this works. Looks ugly. That's for sure. */
504 if( window->ID == *( int *)(enumerator->data) )
506 enumerator->found = GL_TRUE;
507 enumerator->data = window;
512 /* Otherwise, check this window's children */
513 fgEnumSubWindows( window, fghcbWindowByID, enumerator );
517 * This function is similar to the previous one, except it is
518 * looking for a specified (sub)window identifier. The function
519 * is defined in fg_structure.c file.
521 SFG_Window* fgWindowByID( int windowID )
523 SFG_Enumerator enumerator;
525 /* Uses a method very similar for fgWindowByHandle... */
526 enumerator.found = GL_FALSE;
527 enumerator.data = ( void * )&windowID;
528 fgEnumWindows( fghcbWindowByID, &enumerator );
529 if( enumerator.found )
530 return ( SFG_Window * )enumerator.data;
535 * A static helper function to look for a menu given its ID
537 static void fghcbMenuByID( SFG_Menu *menu,
538 SFG_Enumerator *enumerator )
540 if ( enumerator->found )
543 /* Check the menu's ID. */
544 if( menu->ID == *( int *)(enumerator->data) )
546 enumerator->found = GL_TRUE;
547 enumerator->data = menu;
554 * Looks up a menu given its ID. This is easier than fgWindowByXXX
555 * as all menus are placed in one doubly linked list...
557 SFG_Menu* fgMenuByID( int menuID )
559 SFG_Enumerator enumerator;
561 /* This is easy and makes use of the menus enumeration defined above */
562 enumerator.found = GL_FALSE;
563 enumerator.data = (void *)&menuID;
564 fgEnumMenus( fghcbMenuByID, &enumerator );
566 if( enumerator.found )
567 return( SFG_Menu *) enumerator.data;
573 * A static helper function to look for an active menu
575 static void fghcbGetActiveMenu( SFG_Menu *menu,
576 SFG_Enumerator *enumerator )
578 if ( enumerator->found )
581 /* Check the menu's is active */
584 enumerator->found = GL_TRUE;
585 enumerator->data = menu;
592 * Returns active menu, if any. Assumption: only one menu active throughout application at any one time.
593 * This is false when a submenu is also open.
594 * This is easier than fgWindowByXXX as all menus are placed in one doubly linked list...
596 SFG_Menu* fgGetActiveMenu( )
598 SFG_Enumerator enumerator;
600 /* This is easy and makes use of the menus enumeration defined above */
601 enumerator.found = GL_FALSE;
602 fgEnumMenus( fghcbGetActiveMenu, &enumerator );
604 if( enumerator.found )
605 return( SFG_Menu *) enumerator.data;
613 void fgListInit(SFG_List *list)
619 void fgListAppend(SFG_List *list, SFG_Node *node)
623 SFG_Node *ln = (SFG_Node *) list->Last;
637 void fgListRemove(SFG_List *list, SFG_Node *node)
640 ( ( SFG_Node * )node->Next )->Prev = node->Prev;
642 ( ( SFG_Node * )node->Prev )->Next = node->Next;
643 if( ( ( SFG_Node * )list->First ) == node )
644 list->First = node->Next;
645 if( ( ( SFG_Node * )list->Last ) == node )
646 list->Last = node->Prev;
649 int fgListLength(SFG_List *list)
654 for( node =( SFG_Node * )list->First;
656 node = ( SFG_Node * )node->Next )
663 void fgListInsert(SFG_List *list, SFG_Node *next, SFG_Node *node)
667 if( (node->Next = next) )
678 if( (node->Prev = prev) )
684 /*** END OF FILE ***/