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 );
187 * Similarly, clear all work set for the window, none of this has to be executed anymore
189 window->State.WorkMask = 0;
193 * Function to close down all the windows in the "WindowsToDestroy" list
195 void fgCloseWindows( )
197 while( fgStructure.WindowsToDestroy.First )
199 SFG_WindowList *window_ptr = fgStructure.WindowsToDestroy.First;
200 fgDestroyWindow( window_ptr->window );
201 fgListRemove( &fgStructure.WindowsToDestroy, &window_ptr->node );
207 * This function destroys a window and all of its subwindows. Actually,
208 * another function, defined in fg_window.c is called, but this is
209 * a whole different story...
211 void fgDestroyWindow( SFG_Window* window )
213 FREEGLUT_INTERNAL_ERROR_EXIT ( window, "Window destroy function called with null window",
216 while( window->Children.First )
217 fgDestroyWindow( ( SFG_Window * )window->Children.First );
220 SFG_Window *activeWindow = fgStructure.CurrentWindow;
221 INVOKE_WCB( *window, Destroy, ( ) );
222 fgSetWindow( activeWindow );
226 fgListRemove( &window->Parent->Children, &window->Node );
228 fgListRemove( &fgStructure.Windows, &window->Node );
230 if( window->ActiveMenu )
231 fgDeactivateMenu( window );
233 fghClearCallBacks( window );
234 fgCloseWindow( window );
236 if( fgStructure.CurrentWindow == window )
237 fgStructure.CurrentWindow = NULL;
241 * This is a helper static function that removes a menu (given its pointer)
242 * from any windows that can be accessed from a given parent...
244 static void fghRemoveMenuFromWindow( SFG_Window* window, SFG_Menu* menu )
246 SFG_Window *subWindow;
249 /* Check whether this is the active menu in the window */
250 if ( menu == window->ActiveMenu )
251 window->ActiveMenu = NULL ;
254 * Check if the menu is attached to the current window,
255 * if so, have it detached (by overwriting with a NULL):
257 for( i = 0; i < FREEGLUT_MAX_MENUS; i++ )
258 if( window->Menu[ i ] == menu )
259 window->Menu[ i ] = NULL;
261 /* Call this function for all of the window's children recursively: */
262 for( subWindow = (SFG_Window *)window->Children.First;
264 subWindow = (SFG_Window *)subWindow->Node.Next)
265 fghRemoveMenuFromWindow( subWindow, menu );
269 * This is a static helper function that removes menu references
270 * from another menu, given two pointers to them...
272 static void fghRemoveMenuFromMenu( SFG_Menu* from, SFG_Menu* menu )
274 SFG_MenuEntry *entry;
276 for( entry = (SFG_MenuEntry *)from->Entries.First;
278 entry = ( SFG_MenuEntry * )entry->Node.Next )
279 if( entry->SubMenu == menu )
280 entry->SubMenu = NULL;
284 * This function destroys a menu specified by the parameter. All menus
285 * and windows are updated to make sure no ill pointers hang around.
287 void fgDestroyMenu( SFG_Menu* menu )
292 FREEGLUT_INTERNAL_ERROR_EXIT ( menu, "Menu destroy function called with null menu",
295 /* First of all, have all references to this menu removed from all windows: */
296 for( window = (SFG_Window *)fgStructure.Windows.First;
298 window = (SFG_Window *)window->Node.Next )
299 fghRemoveMenuFromWindow( window, menu );
301 /* Now proceed with removing menu entries that lead to this menu */
302 for( from = ( SFG_Menu * )fgStructure.Menus.First;
304 from = ( SFG_Menu * )from->Node.Next )
305 fghRemoveMenuFromMenu( from, menu );
308 * If the programmer defined a destroy callback, call it
309 * A. Donev: But first make this the active menu
313 SFG_Menu *activeMenu=fgStructure.CurrentMenu;
314 fgStructure.CurrentMenu = menu;
315 menu->Destroy( menu->DestroyData );
316 fgStructure.CurrentMenu = activeMenu;
320 * Now we are pretty sure the menu is not used anywhere
321 * and that we can remove all of its entries
323 while( menu->Entries.First )
325 SFG_MenuEntry *entry = ( SFG_MenuEntry * ) menu->Entries.First;
327 fgListRemove( &menu->Entries, &entry->Node );
336 if( fgStructure.CurrentWindow == menu->Window )
338 fgDestroyWindow( menu->Window );
339 fgListRemove( &fgStructure.Menus, &menu->Node );
340 if( fgStructure.CurrentMenu == menu )
341 fgStructure.CurrentMenu = NULL;
347 * This function should be called on glutInit(). It will prepare the internal
348 * structure of freeglut to be used in the application. The structure will be
349 * destroyed using fgDestroyStructure() on glutMainLoop() return. In that
350 * case further use of freeglut should be preceded with a glutInit() call.
352 void fgCreateStructure( void )
355 * We will be needing two lists: the first containing windows,
356 * and the second containing the user-defined menus.
357 * Also, no current window/menu is set, as none has been created yet.
360 fgListInit(&fgStructure.Windows);
361 fgListInit(&fgStructure.Menus);
362 fgListInit(&fgStructure.WindowsToDestroy);
364 fgStructure.CurrentWindow = NULL;
365 fgStructure.CurrentMenu = NULL;
366 fgStructure.MenuContext = NULL;
367 fgStructure.GameModeWindow = NULL;
368 fgStructure.WindowID = 0;
369 fgStructure.MenuID = 0;
373 * This function is automatically called on glutMainLoop() return.
374 * It should deallocate and destroy all remnants of previous
375 * glutInit()-enforced structure initialization...
377 void fgDestroyStructure( void )
379 /* Clean up the WindowsToDestroy list. */
382 /* Make sure all windows and menus have been deallocated */
383 while( fgStructure.Menus.First )
384 fgDestroyMenu( ( SFG_Menu * )fgStructure.Menus.First );
386 while( fgStructure.Windows.First )
387 fgDestroyWindow( ( SFG_Window * )fgStructure.Windows.First );
391 * Helper function to enumerate through all registered top-level windows
393 void fgEnumWindows( FGCBWindowEnumerator enumCallback, SFG_Enumerator* enumerator )
397 FREEGLUT_INTERNAL_ERROR_EXIT ( enumCallback && enumerator,
398 "Enumerator or callback missing from window enumerator call",
401 /* Check every of the top-level windows */
402 for( window = ( SFG_Window * )fgStructure.Windows.First;
404 window = ( SFG_Window * )window->Node.Next )
406 enumCallback( window, enumerator );
407 if( enumerator->found )
413 * Helper function to enumerate through all registered top-level windows
415 void fgEnumMenus( FGCBMenuEnumerator enumCallback, SFG_Enumerator* enumerator )
419 FREEGLUT_INTERNAL_ERROR_EXIT ( enumCallback && enumerator,
420 "Enumerator or callback missing from window enumerator call",
423 /* It's enough to check all entries in fgStructure.Menus... */
424 for( menu = (SFG_Menu *)fgStructure.Menus.First;
426 menu = (SFG_Menu *)menu->Node.Next )
428 enumCallback( menu, enumerator );
429 if( enumerator->found )
435 * Helper function to enumerate through all a window's subwindows
436 * (single level descent)
438 void fgEnumSubWindows( SFG_Window* window, FGCBWindowEnumerator enumCallback,
439 SFG_Enumerator* enumerator )
443 FREEGLUT_INTERNAL_ERROR_EXIT ( enumCallback && enumerator,
444 "Enumerator or callback missing from subwindow enumerator call",
445 "fgEnumSubWindows" );
446 FREEGLUT_INTERNAL_ERROR_EXIT_IF_NOT_INITIALISED ( "Window Enumeration" );
448 for( child = ( SFG_Window * )window->Children.First;
450 child = ( SFG_Window * )child->Node.Next )
452 enumCallback( child, enumerator );
453 if( enumerator->found )
459 * A static helper function to look for a window given its handle
461 static void fghcbWindowByHandle( SFG_Window *window,
462 SFG_Enumerator *enumerator )
464 if ( enumerator->found )
467 /* Check the window's handle. Hope this works. Looks ugly. That's for sure. */
468 if( window->Window.Handle == (SFG_WindowHandleType) (enumerator->data) )
470 enumerator->found = GL_TRUE;
471 enumerator->data = window;
476 /* Otherwise, check this window's children */
477 fgEnumSubWindows( window, fghcbWindowByHandle, enumerator );
481 * fgWindowByHandle returns a (SFG_Window *) value pointing to the
482 * first window in the queue matching the specified window handle.
483 * The function is defined in fg_structure.c file.
485 SFG_Window* fgWindowByHandle ( SFG_WindowHandleType hWindow )
487 SFG_Enumerator enumerator;
489 /* This is easy and makes use of the windows enumeration defined above */
490 enumerator.found = GL_FALSE;
491 enumerator.data = (void *)hWindow;
492 fgEnumWindows( fghcbWindowByHandle, &enumerator );
494 if( enumerator.found )
495 return( SFG_Window *) enumerator.data;
500 * A static helper function to look for a window given its ID
502 static void fghcbWindowByID( SFG_Window *window, SFG_Enumerator *enumerator )
504 /* Make sure we do not overwrite our precious results... */
505 if( enumerator->found )
508 /* Check the window's handle. Hope this works. Looks ugly. That's for sure. */
509 if( window->ID == *( int *)(enumerator->data) )
511 enumerator->found = GL_TRUE;
512 enumerator->data = window;
517 /* Otherwise, check this window's children */
518 fgEnumSubWindows( window, fghcbWindowByID, enumerator );
522 * This function is similar to the previous one, except it is
523 * looking for a specified (sub)window identifier. The function
524 * is defined in fg_structure.c file.
526 SFG_Window* fgWindowByID( int windowID )
528 SFG_Enumerator enumerator;
530 /* Uses a method very similar for fgWindowByHandle... */
531 enumerator.found = GL_FALSE;
532 enumerator.data = ( void * )&windowID;
533 fgEnumWindows( fghcbWindowByID, &enumerator );
534 if( enumerator.found )
535 return ( SFG_Window * )enumerator.data;
540 * A static helper function to look for a menu given its ID
542 static void fghcbMenuByID( SFG_Menu *menu,
543 SFG_Enumerator *enumerator )
545 if ( enumerator->found )
548 /* Check the menu's ID. */
549 if( menu->ID == *( int *)(enumerator->data) )
551 enumerator->found = GL_TRUE;
552 enumerator->data = menu;
559 * Looks up a menu given its ID. This is easier than fgWindowByXXX
560 * as all menus are placed in one doubly linked list...
562 SFG_Menu* fgMenuByID( int menuID )
564 SFG_Enumerator enumerator;
566 /* This is easy and makes use of the menus enumeration defined above */
567 enumerator.found = GL_FALSE;
568 enumerator.data = (void *)&menuID;
569 fgEnumMenus( fghcbMenuByID, &enumerator );
571 if( enumerator.found )
572 return( SFG_Menu *) enumerator.data;
578 * A static helper function to look for an active menu
580 static void fghcbGetActiveMenu( SFG_Menu *menu,
581 SFG_Enumerator *enumerator )
583 if ( enumerator->found )
586 /* Check the menu's is active */
589 enumerator->found = GL_TRUE;
590 enumerator->data = menu;
597 * Returns active menu, if any. Assumption: only one menu active throughout application at any one time.
598 * This is false when a submenu is also open.
599 * This is easier than fgWindowByXXX as all menus are placed in one doubly linked list...
601 SFG_Menu* fgGetActiveMenu( )
603 SFG_Enumerator enumerator;
605 /* This is easy and makes use of the menus enumeration defined above */
606 enumerator.found = GL_FALSE;
607 fgEnumMenus( fghcbGetActiveMenu, &enumerator );
609 if( enumerator.found )
610 return( SFG_Menu *) enumerator.data;
618 void fgListInit(SFG_List *list)
624 void fgListAppend(SFG_List *list, SFG_Node *node)
628 SFG_Node *ln = (SFG_Node *) list->Last;
642 void fgListRemove(SFG_List *list, SFG_Node *node)
645 ( ( SFG_Node * )node->Next )->Prev = node->Prev;
647 ( ( SFG_Node * )node->Prev )->Next = node->Next;
648 if( ( ( SFG_Node * )list->First ) == node )
649 list->First = node->Next;
650 if( ( ( SFG_Node * )list->Last ) == node )
651 list->Last = node->Prev;
654 int fgListLength(SFG_List *list)
659 for( node =( SFG_Node * )list->First;
661 node = ( SFG_Node * )node->Next )
668 void fgListInsert(SFG_List *list, SFG_Node *next, SFG_Node *node)
672 if( (node->Next = next) )
683 if( (node->Prev = prev) )
689 /*** END OF FILE ***/