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);
54 static void fghClearCallBacks( SFG_Window *window )
59 for( i = 0; i < TOTAL_CALLBACKS; ++i )
60 window->CallBacks[ i ] = NULL;
65 * This private function creates, opens and adds to the hierarchy
66 * a freeglut window complete with OpenGL context and stuff...
68 * If parent is set to NULL, the window created will be a topmost one.
70 SFG_Window* fgCreateWindow( SFG_Window* parent, const char* title,
71 GLboolean positionUse, int x, int y,
72 GLboolean sizeUse, int w, int h,
73 GLboolean gameMode, GLboolean isMenu )
75 /* Have the window object created */
76 SFG_Window *window = (SFG_Window *)calloc( 1, sizeof(SFG_Window) );
80 fgError( "Out of memory. Could not create window." );
83 fgPlatformCreateWindow ( window );
85 fghClearCallBacks( window );
86 SET_WCB( *window, Reshape, fghDefaultReshape);
88 /* Initialize the object properties */
89 window->ID = ++fgStructure.WindowID;
91 fgListInit( &window->Children );
94 fgListAppend( &parent->Children, &window->Node );
95 window->Parent = parent;
98 fgListAppend( &fgStructure.Windows, &window->Node );
100 /* Set the default mouse cursor */
101 window->State.Cursor = GLUT_CURSOR_INHERIT;
103 /* Mark window as menu if a menu is to be created */
104 window->IsMenu = isMenu;
107 * Open the window now. The fgOpenWindow() function is system
108 * dependent, and resides in fg_window.c. Uses fgState.
110 fgOpenWindow( window, title, positionUse, x, y, sizeUse, w, h, gameMode,
111 (GLboolean)(parent ? GL_TRUE : GL_FALSE) );
117 * This private function creates a menu and adds it to the menus list
119 SFG_Menu* fgCreateMenu( FGCBMenuUC menuCallback, FGCBUserData userData )
121 SFG_Window *current_window = fgStructure.CurrentWindow;
123 /* Have the menu object created */
124 SFG_Menu* menu = (SFG_Menu *)calloc( sizeof(SFG_Menu), 1 );
126 menu->ParentWindow = NULL;
128 /* Create a window for the menu to reside in. */
129 fgCreateWindow( NULL, "freeglut menu", GL_FALSE, 0, 0, GL_FALSE, 0, 0,
131 menu->Window = fgStructure.CurrentWindow;
132 glutDisplayFunc( fgDisplayMenu );
134 fgSetWindow( current_window );
136 /* Initialize the object properties: */
137 menu->ID = ++fgStructure.MenuID;
138 menu->Callback = menuCallback;
139 menu->CallbackData = userData;
140 menu->ActiveEntry = NULL;
141 menu->Font = fgState.MenuFont;
143 fgListInit( &menu->Entries );
144 fgListAppend( &fgStructure.Menus, &menu->Node );
146 /* Newly created menus implicitly become current ones */
147 fgStructure.CurrentMenu = menu;
153 * Function to add a window to the linked list of windows to destroy.
154 * Subwindows are automatically added because they hang from the window
157 void fgAddToWindowDestroyList( SFG_Window* window )
159 SFG_WindowList *new_list_entry =
160 ( SFG_WindowList* )malloc( sizeof(SFG_WindowList ) );
161 new_list_entry->window = window;
162 fgListAppend( &fgStructure.WindowsToDestroy, &new_list_entry->node );
164 /* Check if the window is the current one... */
165 if( fgStructure.CurrentWindow == window )
166 fgStructure.CurrentWindow = NULL;
169 * Clear all window callbacks except Destroy, which will
170 * be invoked later. Right now, we are potentially carrying
171 * out a freeglut operation at the behest of a client callback,
172 * so we are reluctant to re-enter the client with the Destroy
173 * callback, right now. The others are all wiped out, however,
174 * to ensure that they are no longer called after this point.
177 FGCBDestroy destroy = (FGCBDestroy)FETCH_WCB( *window, Destroy );
178 fghClearCallBacks( window );
179 SET_WCB( *window, Destroy, destroy );
184 * Function to close down all the windows in the "WindowsToDestroy" list
186 void fgCloseWindows( )
188 while( fgStructure.WindowsToDestroy.First )
190 SFG_WindowList *window_ptr = fgStructure.WindowsToDestroy.First;
191 fgDestroyWindow( window_ptr->window );
192 fgListRemove( &fgStructure.WindowsToDestroy, &window_ptr->node );
198 * This function destroys a window and all of its subwindows. Actually,
199 * another function, defined in fg_window.c is called, but this is
200 * a whole different story...
202 void fgDestroyWindow( SFG_Window* window )
204 FREEGLUT_INTERNAL_ERROR_EXIT ( window, "Window destroy function called with null window",
207 while( window->Children.First )
208 fgDestroyWindow( ( SFG_Window * )window->Children.First );
211 SFG_Window *activeWindow = fgStructure.CurrentWindow;
212 INVOKE_WCB( *window, Destroy, ( ) );
213 fgSetWindow( activeWindow );
217 fgListRemove( &window->Parent->Children, &window->Node );
219 fgListRemove( &fgStructure.Windows, &window->Node );
221 if( window->ActiveMenu )
222 fgDeactivateMenu( window );
224 fghClearCallBacks( window );
225 fgCloseWindow( window );
227 if( fgStructure.CurrentWindow == window )
228 fgStructure.CurrentWindow = NULL;
232 * This is a helper static function that removes a menu (given its pointer)
233 * from any windows that can be accessed from a given parent...
235 static void fghRemoveMenuFromWindow( SFG_Window* window, SFG_Menu* menu )
237 SFG_Window *subWindow;
240 /* Check whether this is the active menu in the window */
241 if ( menu == window->ActiveMenu )
242 window->ActiveMenu = NULL ;
245 * Check if the menu is attached to the current window,
246 * if so, have it detached (by overwriting with a NULL):
248 for( i = 0; i < FREEGLUT_MAX_MENUS; i++ )
249 if( window->Menu[ i ] == menu )
250 window->Menu[ i ] = NULL;
252 /* Call this function for all of the window's children recursively: */
253 for( subWindow = (SFG_Window *)window->Children.First;
255 subWindow = (SFG_Window *)subWindow->Node.Next)
256 fghRemoveMenuFromWindow( subWindow, menu );
260 * This is a static helper function that removes menu references
261 * from another menu, given two pointers to them...
263 static void fghRemoveMenuFromMenu( SFG_Menu* from, SFG_Menu* menu )
265 SFG_MenuEntry *entry;
267 for( entry = (SFG_MenuEntry *)from->Entries.First;
269 entry = ( SFG_MenuEntry * )entry->Node.Next )
270 if( entry->SubMenu == menu )
271 entry->SubMenu = NULL;
275 * This function destroys a menu specified by the parameter. All menus
276 * and windows are updated to make sure no ill pointers hang around.
278 void fgDestroyMenu( SFG_Menu* menu )
283 FREEGLUT_INTERNAL_ERROR_EXIT ( menu, "Menu destroy function called with null menu",
286 /* First of all, have all references to this menu removed from all windows: */
287 for( window = (SFG_Window *)fgStructure.Windows.First;
289 window = (SFG_Window *)window->Node.Next )
290 fghRemoveMenuFromWindow( window, menu );
292 /* Now proceed with removing menu entries that lead to this menu */
293 for( from = ( SFG_Menu * )fgStructure.Menus.First;
295 from = ( SFG_Menu * )from->Node.Next )
296 fghRemoveMenuFromMenu( from, menu );
299 * If the programmer defined a destroy callback, call it
300 * A. Donev: But first make this the active menu
304 SFG_Menu *activeMenu=fgStructure.CurrentMenu;
305 fgStructure.CurrentMenu = menu;
307 fgStructure.CurrentMenu = activeMenu;
311 * Now we are pretty sure the menu is not used anywhere
312 * and that we can remove all of its entries
314 while( menu->Entries.First )
316 SFG_MenuEntry *entry = ( SFG_MenuEntry * ) menu->Entries.First;
318 fgListRemove( &menu->Entries, &entry->Node );
327 if( fgStructure.CurrentWindow == menu->Window )
329 fgDestroyWindow( menu->Window );
330 fgListRemove( &fgStructure.Menus, &menu->Node );
331 if( fgStructure.CurrentMenu == menu )
332 fgStructure.CurrentMenu = NULL;
338 * This function should be called on glutInit(). It will prepare the internal
339 * structure of freeglut to be used in the application. The structure will be
340 * destroyed using fgDestroyStructure() on glutMainLoop() return. In that
341 * case further use of freeglut should be preceded with a glutInit() call.
343 void fgCreateStructure( void )
346 * We will be needing two lists: the first containing windows,
347 * and the second containing the user-defined menus.
348 * Also, no current window/menu is set, as none has been created yet.
351 fgListInit(&fgStructure.Windows);
352 fgListInit(&fgStructure.Menus);
353 fgListInit(&fgStructure.WindowsToDestroy);
355 fgStructure.CurrentWindow = NULL;
356 fgStructure.CurrentMenu = NULL;
357 fgStructure.MenuContext = NULL;
358 fgStructure.GameModeWindow = NULL;
359 fgStructure.WindowID = 0;
360 fgStructure.MenuID = 0;
364 * This function is automatically called on glutMainLoop() return.
365 * It should deallocate and destroy all remnants of previous
366 * glutInit()-enforced structure initialization...
368 void fgDestroyStructure( void )
370 /* Clean up the WindowsToDestroy list. */
373 /* Make sure all windows and menus have been deallocated */
374 while( fgStructure.Menus.First )
375 fgDestroyMenu( ( SFG_Menu * )fgStructure.Menus.First );
377 while( fgStructure.Windows.First )
378 fgDestroyWindow( ( SFG_Window * )fgStructure.Windows.First );
382 * Helper function to enumerate through all registered top-level windows
384 void fgEnumWindows( FGCBWindowEnumerator enumCallback, SFG_Enumerator* enumerator )
388 FREEGLUT_INTERNAL_ERROR_EXIT ( enumCallback && enumerator,
389 "Enumerator or callback missing from window enumerator call",
392 /* Check every of the top-level windows */
393 for( window = ( SFG_Window * )fgStructure.Windows.First;
395 window = ( SFG_Window * )window->Node.Next )
397 enumCallback( window, enumerator );
398 if( enumerator->found )
404 * Helper function to enumerate through all registered top-level windows
406 void fgEnumMenus( FGCBMenuEnumerator enumCallback, SFG_Enumerator* enumerator )
410 FREEGLUT_INTERNAL_ERROR_EXIT ( enumCallback && enumerator,
411 "Enumerator or callback missing from window enumerator call",
414 /* It's enough to check all entries in fgStructure.Menus... */
415 for( menu = (SFG_Menu *)fgStructure.Menus.First;
417 menu = (SFG_Menu *)menu->Node.Next )
419 enumCallback( menu, enumerator );
420 if( enumerator->found )
426 * Helper function to enumerate through all a window's subwindows
427 * (single level descent)
429 void fgEnumSubWindows( SFG_Window* window, FGCBWindowEnumerator enumCallback,
430 SFG_Enumerator* enumerator )
434 FREEGLUT_INTERNAL_ERROR_EXIT ( enumCallback && enumerator,
435 "Enumerator or callback missing from subwindow enumerator call",
436 "fgEnumSubWindows" );
437 FREEGLUT_INTERNAL_ERROR_EXIT_IF_NOT_INITIALISED ( "Window Enumeration" );
439 for( child = ( SFG_Window * )window->Children.First;
441 child = ( SFG_Window * )child->Node.Next )
443 enumCallback( child, enumerator );
444 if( enumerator->found )
450 * A static helper function to look for a window given its handle
452 static void fghcbWindowByHandle( SFG_Window *window,
453 SFG_Enumerator *enumerator )
455 if ( enumerator->found )
458 /* Check the window's handle. Hope this works. Looks ugly. That's for sure. */
459 if( window->Window.Handle == (SFG_WindowHandleType) (enumerator->data) )
461 enumerator->found = GL_TRUE;
462 enumerator->data = window;
467 /* Otherwise, check this window's children */
468 fgEnumSubWindows( window, fghcbWindowByHandle, enumerator );
472 * fgWindowByHandle returns a (SFG_Window *) value pointing to the
473 * first window in the queue matching the specified window handle.
474 * The function is defined in fg_structure.c file.
476 SFG_Window* fgWindowByHandle ( SFG_WindowHandleType hWindow )
478 SFG_Enumerator enumerator;
480 /* This is easy and makes use of the windows enumeration defined above */
481 enumerator.found = GL_FALSE;
482 enumerator.data = (void *)hWindow;
483 fgEnumWindows( fghcbWindowByHandle, &enumerator );
485 if( enumerator.found )
486 return( SFG_Window *) enumerator.data;
491 * A static helper function to look for a window given its ID
493 static void fghcbWindowByID( SFG_Window *window, SFG_Enumerator *enumerator )
495 /* Make sure we do not overwrite our precious results... */
496 if( enumerator->found )
499 /* Check the window's handle. Hope this works. Looks ugly. That's for sure. */
500 if( window->ID == *( int *)(enumerator->data) )
502 enumerator->found = GL_TRUE;
503 enumerator->data = window;
508 /* Otherwise, check this window's children */
509 fgEnumSubWindows( window, fghcbWindowByID, enumerator );
513 * This function is similar to the previous one, except it is
514 * looking for a specified (sub)window identifier. The function
515 * is defined in fg_structure.c file.
517 SFG_Window* fgWindowByID( int windowID )
519 SFG_Enumerator enumerator;
521 /* Uses a method very similar for fgWindowByHandle... */
522 enumerator.found = GL_FALSE;
523 enumerator.data = ( void * )&windowID;
524 fgEnumWindows( fghcbWindowByID, &enumerator );
525 if( enumerator.found )
526 return ( SFG_Window * )enumerator.data;
531 * A static helper function to look for a menu given its ID
533 static void fghcbMenuByID( SFG_Menu *menu,
534 SFG_Enumerator *enumerator )
536 if ( enumerator->found )
539 /* Check the menu's ID. */
540 if( menu->ID == *( int *)(enumerator->data) )
542 enumerator->found = GL_TRUE;
543 enumerator->data = menu;
550 * Looks up a menu given its ID. This is easier than fgWindowByXXX
551 * as all menus are placed in one doubly linked list...
553 SFG_Menu* fgMenuByID( int menuID )
555 SFG_Enumerator enumerator;
557 /* This is easy and makes use of the menus enumeration defined above */
558 enumerator.found = GL_FALSE;
559 enumerator.data = (void *)&menuID;
560 fgEnumMenus( fghcbMenuByID, &enumerator );
562 if( enumerator.found )
563 return( SFG_Menu *) enumerator.data;
569 * A static helper function to look for an active menu
571 static void fghcbGetActiveMenu( SFG_Menu *menu,
572 SFG_Enumerator *enumerator )
574 if ( enumerator->found )
577 /* Check the menu's is active */
580 enumerator->found = GL_TRUE;
581 enumerator->data = menu;
588 * Returns active menu, if any. Assumption: only one menu active throughout application at any one time.
589 * This is false when a submenu is also open.
590 * This is easier than fgWindowByXXX as all menus are placed in one doubly linked list...
592 SFG_Menu* fgGetActiveMenu( )
594 SFG_Enumerator enumerator;
596 /* This is easy and makes use of the menus enumeration defined above */
597 enumerator.found = GL_FALSE;
598 fgEnumMenus( fghcbGetActiveMenu, &enumerator );
600 if( enumerator.found )
601 return( SFG_Menu *) enumerator.data;
609 void fgListInit(SFG_List *list)
615 void fgListAppend(SFG_List *list, SFG_Node *node)
619 SFG_Node *ln = (SFG_Node *) list->Last;
633 void fgListRemove(SFG_List *list, SFG_Node *node)
636 ( ( SFG_Node * )node->Next )->Prev = node->Prev;
638 ( ( SFG_Node * )node->Prev )->Next = node->Next;
639 if( ( ( SFG_Node * )list->First ) == node )
640 list->First = node->Next;
641 if( ( ( SFG_Node * )list->Last ) == node )
642 list->Last = node->Prev;
645 int fgListLength(SFG_List *list)
650 for( node =( SFG_Node * )list->First;
652 node = ( SFG_Node * )node->Next )
659 void fgListInsert(SFG_List *list, SFG_Node *next, SFG_Node *node)
663 if( (node->Next = next) )
674 if( (node->Prev = prev) )
680 /*** END OF FILE ***/