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 #include <GL/freeglut.h>
33 #include "freeglut_internal.h"
36 /* -- GLOBAL EXPORTS ------------------------------------------------------- */
39 * The SFG_Structure container holds information about windows and menus
40 * created between glutInit() and glutMainLoop() return.
43 SFG_Structure fgStructure = { { NULL, NULL }, /* The list of windows */
44 { NULL, NULL }, /* The list of menus */
45 { NULL, NULL }, /* Windows to Destroy list */
46 NULL, /* The current window */
47 NULL, /* The current menu */
48 NULL, /* The menu OpenGL context */
49 NULL, /* The game mode window */
50 0, /* The current new window ID */
51 0 }; /* The current new menu ID */
54 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */
56 void fgClearCallBacks( SFG_Window *window )
61 for( i = 0; i < TOTAL_CALLBACKS; ++i )
62 window->CallBacks[ i ] = NULL;
67 * This private function creates, opens and adds to the hierarchy
68 * a freeglut window complete with OpenGL context and stuff...
70 * If parent is set to NULL, the window created will be a topmost one.
72 SFG_Window* fgCreateWindow( SFG_Window* parent, const char* title,
73 int x, int y, int w, int h,
74 GLboolean gameMode, GLboolean isMenu )
77 * Have the window object created
79 SFG_Window *window = (SFG_Window *)calloc( sizeof(SFG_Window), 1 );
82 fgClearCallBacks( window );
85 * If the freeglut internals haven't been initialized yet,
86 * do it now. Hack's idea courtesy of Chris Purnell...
88 if( !fgState.Initialised )
89 glutInit( &fakeArgc, NULL );
92 * Initialize the object properties
94 window->ID = ++fgStructure.WindowID;
95 window->State.OldHeight = window->State.OldWidth = -1;
97 fgListInit( &window->Children );
100 fgListAppend( &parent->Children, &window->Node );
101 window->Parent = parent;
104 fgListAppend( &fgStructure.Windows, &window->Node );
107 * Set the default mouse cursor and reset the modifiers value
109 window->State.Cursor = GLUT_CURSOR_INHERIT;
111 window->IsMenu = isMenu;
114 * Open the window now. The fgOpenWindow() function is system
115 * dependant, and resides in freeglut_window.c. Uses fgState.
117 fgOpenWindow( window, title, x, y, w, h, gameMode,
118 parent ? GL_TRUE : GL_FALSE );
124 * This private function creates a menu and adds it to the menus list
126 SFG_Menu* fgCreateMenu( FGCBMenu menuCallback )
128 int x = 100, y = 100, w = 100, h = 100;
129 SFG_Window *current_window = fgStructure.Window;
132 * Have the menu object created
134 SFG_Menu* menu = (SFG_Menu *)calloc( sizeof(SFG_Menu), 1 );
138 * If the freeglut internals haven't been initialized yet,
139 * do it now. Hack's idea courtesy of Chris Purnell...
141 if( !fgState.Initialised )
142 glutInit( &fakeArgc, NULL );
144 menu->ParentWindow = fgStructure.Window;
147 * Create a window for the menu to reside in.
150 fgCreateWindow( NULL, NULL, x, y, w, h, GL_FALSE, GL_TRUE );
151 menu->Window = fgStructure.Window;
152 glutDisplayFunc( fgDisplayMenu );
154 glutHideWindow( ); /* Hide the window for now */
155 fgSetWindow( current_window );
158 * Initialize the object properties:
160 menu->ID = ++fgStructure.MenuID;
161 menu->Callback = menuCallback;
162 menu->ActiveEntry = NULL;
164 fgListInit( &menu->Entries );
165 fgListAppend( &fgStructure.Menus, &menu->Node );
168 * Newly created menus implicitly become current ones
170 fgStructure.Menu = menu;
176 * Function to add a window to the linked list of windows to destroy.
177 * Subwindows are automatically added because they hang from the window
180 void fgAddToWindowDestroyList( SFG_Window* window )
182 SFG_WindowList *new_list_entry =
183 ( SFG_WindowList* )malloc( sizeof(SFG_WindowList ) );
184 new_list_entry->window = window;
185 fgListAppend( &fgStructure.WindowsToDestroy, &new_list_entry->node );
188 * Check if the window is the current one...
190 if( fgStructure.Window == window )
191 fgStructure.Window = NULL;
194 * Clear all window callbacks except Destroy, which will
195 * be invoked later. Right now, we are potentially carrying
196 * out a freeglut operation at the behest of a client callback,
197 * so we are reluctant to re-enter the client with the Destroy
198 * callback, right now. The others are all wiped out, however,
199 * to ensure that they are no longer called after this point.
202 FGCBDestroy destroy = FETCH_WCB( *window, Destroy );
203 fgClearCallBacks( window );
204 SET_WCB( *window, Destroy, destroy );
209 * Function to close down all the windows in the "WindowsToDestroy" list
211 void fgCloseWindows( )
213 while( fgStructure.WindowsToDestroy.First )
215 SFG_WindowList *window_ptr = fgStructure.WindowsToDestroy.First;
216 fgDestroyWindow( window_ptr->window );
217 fgListRemove( &fgStructure.WindowsToDestroy, &window_ptr->node );
223 * This function destroys a window and all of its subwindows. Actually,
224 * another function, defined in freeglut_window.c is called, but this is
225 * a whole different story...
227 void fgDestroyWindow( SFG_Window* window )
232 freeglut_assert_ready;
234 while( window->Children.First )
235 fgDestroyWindow( ( SFG_Window * )window->Children.First );
238 SFG_Window *activeWindow = fgStructure.Window;
239 INVOKE_WCB( *window, Destroy, ( ) );
240 fgSetWindow( activeWindow );
244 fgListRemove( &window->Parent->Children, &window->Node );
246 fgListRemove( &fgStructure.Windows, &window->Node );
248 if( window->ActiveMenu )
249 fgDeactivateMenu( window );
251 for( menu_index = 0; menu_index < 3; menu_index ++ )
252 if( window->Menu[ menu_index ] )
253 window->Menu[ menu_index ]->ParentWindow = NULL;
255 fgClearCallBacks( window );
256 fgCloseWindow( window );
258 if( fgStructure.Window == window )
259 fgStructure.Window = NULL;
263 * This is a helper static function that removes a menu (given its pointer)
264 * from any windows that can be accessed from a given parent...
266 static void fghRemoveMenuFromWindow( SFG_Window* window, SFG_Menu* menu )
268 SFG_Window *subWindow;
272 * Check if the menu is attached to the current window,
273 * if so, have it detached (by overwriting with a NULL):
275 for( i = 0; i < 3; i++ )
276 if( window->Menu[ i ] == menu )
277 window->Menu[ i ] = NULL;
280 * Call this function for all of the window's children recursively:
282 for( subWindow = (SFG_Window *)window->Children.First;
284 subWindow = (SFG_Window *)subWindow->Node.Next)
285 fghRemoveMenuFromWindow( subWindow, menu );
289 * This is a static helper function that removes menu references
290 * from another menu, given two pointers to them...
292 static void fghRemoveMenuFromMenu( SFG_Menu* from, SFG_Menu* menu )
294 SFG_MenuEntry *entry;
296 for( entry = (SFG_MenuEntry *)from->Entries.First;
298 entry = ( SFG_MenuEntry * )entry->Node.Next )
299 if( entry->SubMenu == menu )
300 entry->SubMenu = NULL;
304 * This function destroys a menu specified by the parameter. All menus
305 * and windows are updated to make sure no ill pointers hang around.
307 void fgDestroyMenu( SFG_Menu* menu )
313 freeglut_assert_ready;
316 * First of all, have all references to this menu removed from all windows:
318 for( window = (SFG_Window *)fgStructure.Windows.First;
320 window = (SFG_Window *)window->Node.Next )
321 fghRemoveMenuFromWindow( window, menu );
324 * Now proceed with removing menu entries that lead to this menu
326 for( from = ( SFG_Menu * )fgStructure.Menus.First;
328 from = ( SFG_Menu * )from->Node.Next )
329 fghRemoveMenuFromMenu( from, menu );
332 * If the programmer defined a destroy callback, call it
333 * A. Donev: But first make this the active menu
337 SFG_Menu *activeMenu=fgStructure.Menu;
338 fgStructure.Menu = menu;
340 fgStructure.Menu = activeMenu;
344 * Now we are pretty sure the menu is not used anywhere
345 * and that we can remove all of its entries
347 while( menu->Entries.First )
349 SFG_MenuEntry *entry = ( SFG_MenuEntry * ) menu->Entries.First;
351 fgListRemove( &menu->Entries, &entry->Node );
360 if( fgStructure.Window == menu->Window )
361 fgSetWindow( menu->ParentWindow );
362 fgDestroyWindow( menu->Window );
363 fgListRemove( &fgStructure.Menus, &menu->Node );
364 if( fgStructure.Menu == menu )
365 fgStructure.Menu = NULL;
371 * This function should be called on glutInit(). It will prepare the internal
372 * structure of freeglut to be used in the application. The structure will be
373 * destroyed using fgDestroyStructure() on glutMainLoop() return. In that
374 * case further use of freeglut should be preceeded with a glutInit() call.
376 void fgCreateStructure( void )
379 * We will be needing two lists: the first containing windows,
380 * and the second containing the user-defined menus.
381 * Also, no current window/menu is set, as none has been created yet.
384 fgListInit(&fgStructure.Windows);
385 fgListInit(&fgStructure.Menus);
386 fgListInit(&fgStructure.WindowsToDestroy);
390 * This function is automatically called on glutMainLoop() return.
391 * It should deallocate and destroy all remnants of previous
392 * glutInit()-enforced structure initialization...
394 void fgDestroyStructure( void )
396 freeglut_assert_ready;
399 * Clean up the WindowsToDestroy list.
404 * Make sure all windows and menus have been deallocated
406 while( fgStructure.Menus.First )
407 fgDestroyMenu( ( SFG_Menu * )fgStructure.Menus.First );
409 while( fgStructure.Windows.First )
410 fgDestroyWindow( ( SFG_Window * )fgStructure.Windows.First );
414 * Helper function to enumerate through all registered top-level windows
416 void fgEnumWindows( FGCBenumerator enumCallback, SFG_Enumerator* enumerator )
420 assert( enumCallback && enumerator );
421 freeglut_assert_ready;
424 * Check every of the top-level windows
426 for( window = ( SFG_Window * )fgStructure.Windows.First;
428 window = ( SFG_Window * )window->Node.Next )
430 enumCallback( window, enumerator );
431 if( enumerator->found )
437 * Helper function to enumerate through all a window's subwindows
438 * (single level descent)
440 void fgEnumSubWindows( SFG_Window* window, FGCBenumerator enumCallback,
441 SFG_Enumerator* enumerator )
445 assert( enumCallback && enumerator );
446 freeglut_assert_ready;
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 )
468 * Check the window's handle. Hope this works. Looks ugly. That's for sure.
470 if( window->Window.Handle == (SFG_WindowHandleType) (enumerator->data) )
472 enumerator->found = GL_TRUE;
473 enumerator->data = window;
479 * Otherwise, check this window's children
481 fgEnumSubWindows( window, fghcbWindowByHandle, enumerator );
485 * fgWindowByHandle returns a (SFG_Window *) value pointing to the
486 * first window in the queue matching the specified window handle.
487 * The function is defined in freeglut_structure.c file.
489 SFG_Window* fgWindowByHandle ( SFG_WindowHandleType hWindow )
491 SFG_Enumerator enumerator;
494 * This is easy and makes use of the windows enumeration defined above
496 enumerator.found = GL_FALSE;
497 enumerator.data = (void *)hWindow;
498 fgEnumWindows( fghcbWindowByHandle, &enumerator );
500 if( enumerator.found )
501 return( SFG_Window *) enumerator.data;
506 * A static helper function to look for a window given its ID
508 static void fghcbWindowByID( SFG_Window *window, SFG_Enumerator *enumerator )
511 * Make sure we do not overwrite our precious results...
513 if( enumerator->found )
517 * Check the window's handle. Hope this works. Looks ugly. That's for sure.
519 if( window->ID == *( int *)(enumerator->data) )
521 enumerator->found = GL_TRUE;
522 enumerator->data = window;
528 * Otherwise, check this window's children
530 fgEnumSubWindows( window, fghcbWindowByID, enumerator );
534 * This function is similiar to the previous one, except it is
535 * looking for a specified (sub)window identifier. The function
536 * is defined in freeglut_structure.c file.
538 SFG_Window* fgWindowByID( int windowID )
540 SFG_Enumerator enumerator;
543 * Uses a method very similiar for fgWindowByHandle...
545 enumerator.found = GL_FALSE;
546 enumerator.data = ( void * )&windowID;
547 fgEnumWindows( fghcbWindowByID, &enumerator );
548 if( enumerator.found )
549 return ( SFG_Window * )enumerator.data;
554 * Looks up a menu given its ID. This is easier that fgWindowByXXX
555 * as all menus are placed in a single doubly linked list...
557 SFG_Menu* fgMenuByID( int menuID )
559 SFG_Menu *menu = NULL;
561 freeglut_assert_ready;
564 * It's enough to check all entries in fgStructure.Menus...
566 for( menu = (SFG_Menu *)fgStructure.Menus.First;
568 menu = (SFG_Menu *)menu->Node.Next )
569 if( menu->ID == menuID )
577 void fgListInit(SFG_List *list)
583 void fgListAppend(SFG_List *list, SFG_Node *node)
587 SFG_Node *ln = (SFG_Node *) list->Last;
601 void fgListRemove(SFG_List *list, SFG_Node *node)
604 ( ( SFG_Node * )node->Next )->Prev = node->Prev;
606 ( ( SFG_Node * )node->Prev )->Next = node->Next;
607 if( ( ( SFG_Node * )list->First ) == node )
608 list->First = node->Next;
609 if( ( ( SFG_Node * )list->Last ) == node )
610 list->Last = node->Prev;
613 int fgListLength(SFG_List *list)
618 for( node =( SFG_Node * )list->First;
620 node = ( SFG_Node * )node->Next )
627 void fgListInsert(SFG_List *list, SFG_Node *next, SFG_Node *node)
631 if( (node->Next = next) )
642 if( (node->Prev = prev) )
648 /*** END OF FILE ***/