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( FGCBMenu menuCallback )
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->ActiveEntry = NULL;
140 menu->Font = fgState.MenuFont;
142 fgListInit( &menu->Entries );
143 fgListAppend( &fgStructure.Menus, &menu->Node );
145 /* Newly created menus implicitly become current ones */
146 fgStructure.CurrentMenu = menu;
152 * Function to add a window to the linked list of windows to destroy.
153 * Subwindows are automatically added because they hang from the window
156 void fgAddToWindowDestroyList( SFG_Window* window )
158 SFG_WindowList *new_list_entry =
159 ( SFG_WindowList* )malloc( sizeof(SFG_WindowList ) );
160 new_list_entry->window = window;
161 fgListAppend( &fgStructure.WindowsToDestroy, &new_list_entry->node );
163 /* Check if the window is the current one... */
164 if( fgStructure.CurrentWindow == window )
165 fgStructure.CurrentWindow = NULL;
168 * Clear all window callbacks except Destroy, which will
169 * be invoked later. Right now, we are potentially carrying
170 * out a freeglut operation at the behest of a client callback,
171 * so we are reluctant to re-enter the client with the Destroy
172 * callback, right now. The others are all wiped out, however,
173 * to ensure that they are no longer called after this point.
176 FGCBDestroy destroy = (FGCBDestroy)FETCH_WCB( *window, Destroy );
177 fghClearCallBacks( window );
178 SET_WCB( *window, Destroy, destroy );
183 * Function to close down all the windows in the "WindowsToDestroy" list
185 void fgCloseWindows( )
187 while( fgStructure.WindowsToDestroy.First )
189 SFG_WindowList *window_ptr = fgStructure.WindowsToDestroy.First;
190 fgDestroyWindow( window_ptr->window );
191 fgListRemove( &fgStructure.WindowsToDestroy, &window_ptr->node );
197 * This function destroys a window and all of its subwindows. Actually,
198 * another function, defined in fg_window.c is called, but this is
199 * a whole different story...
201 void fgDestroyWindow( SFG_Window* window )
203 FREEGLUT_INTERNAL_ERROR_EXIT ( window, "Window destroy function called with null window",
206 while( window->Children.First )
207 fgDestroyWindow( ( SFG_Window * )window->Children.First );
210 SFG_Window *activeWindow = fgStructure.CurrentWindow;
211 INVOKE_WCB( *window, Destroy, ( ) );
212 fgSetWindow( activeWindow );
216 fgListRemove( &window->Parent->Children, &window->Node );
218 fgListRemove( &fgStructure.Windows, &window->Node );
220 if( window->ActiveMenu )
221 fgDeactivateMenu( window );
223 fghClearCallBacks( window );
224 fgCloseWindow( window );
226 if( fgStructure.CurrentWindow == window )
227 fgStructure.CurrentWindow = NULL;
231 * This is a helper static function that removes a menu (given its pointer)
232 * from any windows that can be accessed from a given parent...
234 static void fghRemoveMenuFromWindow( SFG_Window* window, SFG_Menu* menu )
236 SFG_Window *subWindow;
239 /* Check whether this is the active menu in the window */
240 if ( menu == window->ActiveMenu )
241 window->ActiveMenu = NULL ;
244 * Check if the menu is attached to the current window,
245 * if so, have it detached (by overwriting with a NULL):
247 for( i = 0; i < FREEGLUT_MAX_MENUS; i++ )
248 if( window->Menu[ i ] == menu )
249 window->Menu[ i ] = NULL;
251 /* Call this function for all of the window's children recursively: */
252 for( subWindow = (SFG_Window *)window->Children.First;
254 subWindow = (SFG_Window *)subWindow->Node.Next)
255 fghRemoveMenuFromWindow( subWindow, menu );
259 * This is a static helper function that removes menu references
260 * from another menu, given two pointers to them...
262 static void fghRemoveMenuFromMenu( SFG_Menu* from, SFG_Menu* menu )
264 SFG_MenuEntry *entry;
266 for( entry = (SFG_MenuEntry *)from->Entries.First;
268 entry = ( SFG_MenuEntry * )entry->Node.Next )
269 if( entry->SubMenu == menu )
270 entry->SubMenu = NULL;
274 * This function destroys a menu specified by the parameter. All menus
275 * and windows are updated to make sure no ill pointers hang around.
277 void fgDestroyMenu( SFG_Menu* menu )
282 FREEGLUT_INTERNAL_ERROR_EXIT ( menu, "Menu destroy function called with null menu",
285 /* First of all, have all references to this menu removed from all windows: */
286 for( window = (SFG_Window *)fgStructure.Windows.First;
288 window = (SFG_Window *)window->Node.Next )
289 fghRemoveMenuFromWindow( window, menu );
291 /* Now proceed with removing menu entries that lead to this menu */
292 for( from = ( SFG_Menu * )fgStructure.Menus.First;
294 from = ( SFG_Menu * )from->Node.Next )
295 fghRemoveMenuFromMenu( from, menu );
298 * If the programmer defined a destroy callback, call it
299 * A. Donev: But first make this the active menu
303 SFG_Menu *activeMenu=fgStructure.CurrentMenu;
304 fgStructure.CurrentMenu = menu;
306 fgStructure.CurrentMenu = activeMenu;
310 * Now we are pretty sure the menu is not used anywhere
311 * and that we can remove all of its entries
313 while( menu->Entries.First )
315 SFG_MenuEntry *entry = ( SFG_MenuEntry * ) menu->Entries.First;
317 fgListRemove( &menu->Entries, &entry->Node );
326 if( fgStructure.CurrentWindow == menu->Window )
328 fgDestroyWindow( menu->Window );
329 fgListRemove( &fgStructure.Menus, &menu->Node );
330 if( fgStructure.CurrentMenu == menu )
331 fgStructure.CurrentMenu = NULL;
337 * This function should be called on glutInit(). It will prepare the internal
338 * structure of freeglut to be used in the application. The structure will be
339 * destroyed using fgDestroyStructure() on glutMainLoop() return. In that
340 * case further use of freeglut should be preceded with a glutInit() call.
342 void fgCreateStructure( void )
345 * We will be needing two lists: the first containing windows,
346 * and the second containing the user-defined menus.
347 * Also, no current window/menu is set, as none has been created yet.
350 fgListInit(&fgStructure.Windows);
351 fgListInit(&fgStructure.Menus);
352 fgListInit(&fgStructure.WindowsToDestroy);
354 fgStructure.CurrentWindow = NULL;
355 fgStructure.CurrentMenu = NULL;
356 fgStructure.MenuContext = NULL;
357 fgStructure.GameModeWindow = NULL;
358 fgStructure.WindowID = 0;
359 fgStructure.MenuID = 0;
363 * This function is automatically called on glutMainLoop() return.
364 * It should deallocate and destroy all remnants of previous
365 * glutInit()-enforced structure initialization...
367 void fgDestroyStructure( void )
369 /* Clean up the WindowsToDestroy list. */
372 /* Make sure all windows and menus have been deallocated */
373 while( fgStructure.Menus.First )
374 fgDestroyMenu( ( SFG_Menu * )fgStructure.Menus.First );
376 while( fgStructure.Windows.First )
377 fgDestroyWindow( ( SFG_Window * )fgStructure.Windows.First );
381 * Helper function to enumerate through all registered top-level windows
383 void fgEnumWindows( FGCBWindowEnumerator enumCallback, SFG_Enumerator* enumerator )
387 FREEGLUT_INTERNAL_ERROR_EXIT ( enumCallback && enumerator,
388 "Enumerator or callback missing from window enumerator call",
391 /* Check every of the top-level windows */
392 for( window = ( SFG_Window * )fgStructure.Windows.First;
394 window = ( SFG_Window * )window->Node.Next )
396 enumCallback( window, enumerator );
397 if( enumerator->found )
403 * Helper function to enumerate through all registered top-level windows
405 void fgEnumMenus( FGCBMenuEnumerator enumCallback, SFG_Enumerator* enumerator )
409 FREEGLUT_INTERNAL_ERROR_EXIT ( enumCallback && enumerator,
410 "Enumerator or callback missing from window enumerator call",
413 /* It's enough to check all entries in fgStructure.Menus... */
414 for( menu = (SFG_Menu *)fgStructure.Menus.First;
416 menu = (SFG_Menu *)menu->Node.Next )
418 enumCallback( menu, enumerator );
419 if( enumerator->found )
425 * Helper function to enumerate through all a window's subwindows
426 * (single level descent)
428 void fgEnumSubWindows( SFG_Window* window, FGCBWindowEnumerator enumCallback,
429 SFG_Enumerator* enumerator )
433 FREEGLUT_INTERNAL_ERROR_EXIT ( enumCallback && enumerator,
434 "Enumerator or callback missing from subwindow enumerator call",
435 "fgEnumSubWindows" );
436 FREEGLUT_INTERNAL_ERROR_EXIT_IF_NOT_INITIALISED ( "Window Enumeration" );
438 for( child = ( SFG_Window * )window->Children.First;
440 child = ( SFG_Window * )child->Node.Next )
442 enumCallback( child, enumerator );
443 if( enumerator->found )
449 * A static helper function to look for a window given its handle
451 static void fghcbWindowByHandle( SFG_Window *window,
452 SFG_Enumerator *enumerator )
454 if ( enumerator->found )
457 /* Check the window's handle. Hope this works. Looks ugly. That's for sure. */
458 if( window->Window.Handle == (SFG_WindowHandleType) (enumerator->data) )
460 enumerator->found = GL_TRUE;
461 enumerator->data = window;
466 /* Otherwise, check this window's children */
467 fgEnumSubWindows( window, fghcbWindowByHandle, enumerator );
471 * fgWindowByHandle returns a (SFG_Window *) value pointing to the
472 * first window in the queue matching the specified window handle.
473 * The function is defined in fg_structure.c file.
475 SFG_Window* fgWindowByHandle ( SFG_WindowHandleType hWindow )
477 SFG_Enumerator enumerator;
479 /* This is easy and makes use of the windows enumeration defined above */
480 enumerator.found = GL_FALSE;
481 enumerator.data = (void *)hWindow;
482 fgEnumWindows( fghcbWindowByHandle, &enumerator );
484 if( enumerator.found )
485 return( SFG_Window *) enumerator.data;
490 * A static helper function to look for a window given its ID
492 static void fghcbWindowByID( SFG_Window *window, SFG_Enumerator *enumerator )
494 /* Make sure we do not overwrite our precious results... */
495 if( enumerator->found )
498 /* Check the window's handle. Hope this works. Looks ugly. That's for sure. */
499 if( window->ID == *( int *)(enumerator->data) )
501 enumerator->found = GL_TRUE;
502 enumerator->data = window;
507 /* Otherwise, check this window's children */
508 fgEnumSubWindows( window, fghcbWindowByID, enumerator );
512 * This function is similar to the previous one, except it is
513 * looking for a specified (sub)window identifier. The function
514 * is defined in fg_structure.c file.
516 SFG_Window* fgWindowByID( int windowID )
518 SFG_Enumerator enumerator;
520 /* Uses a method very similar for fgWindowByHandle... */
521 enumerator.found = GL_FALSE;
522 enumerator.data = ( void * )&windowID;
523 fgEnumWindows( fghcbWindowByID, &enumerator );
524 if( enumerator.found )
525 return ( SFG_Window * )enumerator.data;
530 * A static helper function to look for a menu given its ID
532 static void fghcbMenuByID( SFG_Menu *menu,
533 SFG_Enumerator *enumerator )
535 if ( enumerator->found )
538 /* Check the menu's ID. */
539 if( menu->ID == *( int *)(enumerator->data) )
541 enumerator->found = GL_TRUE;
542 enumerator->data = menu;
549 * Looks up a menu given its ID. This is easier than fgWindowByXXX
550 * as all menus are placed in one doubly linked list...
552 SFG_Menu* fgMenuByID( int menuID )
554 SFG_Enumerator enumerator;
556 /* This is easy and makes use of the menus enumeration defined above */
557 enumerator.found = GL_FALSE;
558 enumerator.data = (void *)&menuID;
559 fgEnumMenus( fghcbMenuByID, &enumerator );
561 if( enumerator.found )
562 return( SFG_Menu *) enumerator.data;
568 * A static helper function to look for an active menu
570 static void fghcbGetActiveMenu( SFG_Menu *menu,
571 SFG_Enumerator *enumerator )
573 if ( enumerator->found )
576 /* Check the menu's is active */
579 enumerator->found = GL_TRUE;
580 enumerator->data = menu;
587 * Returns active menu, if any. Assumption: only one menu active throughout application at any one time.
588 * This is false when a submenu is also open.
589 * This is easier than fgWindowByXXX as all menus are placed in one doubly linked list...
591 SFG_Menu* fgGetActiveMenu( )
593 SFG_Enumerator enumerator;
595 /* This is easy and makes use of the menus enumeration defined above */
596 enumerator.found = GL_FALSE;
597 fgEnumMenus( fghcbGetActiveMenu, &enumerator );
599 if( enumerator.found )
600 return( SFG_Menu *) enumerator.data;
608 void fgListInit(SFG_List *list)
614 void fgListAppend(SFG_List *list, SFG_Node *node)
618 SFG_Node *ln = (SFG_Node *) list->Last;
632 void fgListRemove(SFG_List *list, SFG_Node *node)
635 ( ( SFG_Node * )node->Next )->Prev = node->Prev;
637 ( ( SFG_Node * )node->Prev )->Next = node->Next;
638 if( ( ( SFG_Node * )list->First ) == node )
639 list->First = node->Next;
640 if( ( ( SFG_Node * )list->Last ) == node )
641 list->Last = node->Prev;
644 int fgListLength(SFG_List *list)
649 for( node =( SFG_Node * )list->First;
651 node = ( SFG_Node * )node->Next )
658 void fgListInsert(SFG_List *list, SFG_Node *next, SFG_Node *node)
662 if( (node->Next = next) )
673 if( (node->Prev = prev) )
679 /*** END OF FILE ***/