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 "../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, /* The current window */
46 NULL, /* The current menu */
47 NULL, /* The menu OpenGL context */
48 NULL, /* The game mode window */
49 0, /* The current new window ID */
50 0 }; /* The current new menu ID */
53 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */
55 void fgClearCallBacks( SFG_Window *window )
60 for( i = 0; i < TOTAL_CALLBACKS; ++i )
61 window->CallBacks[ i ] = NULL;
66 * This private function creates, opens and adds to the hierarchy
67 * a freeglut window complete with OpenGL context and stuff...
69 * If parent is set to NULL, the window created will be a topmost one.
71 SFG_Window* fgCreateWindow( SFG_Window* parent, const char* title,
72 int x, int y, int w, int h, GLboolean gameMode )
75 * Have the window object created
77 SFG_Window *window = (SFG_Window *)calloc( sizeof(SFG_Window), 1 );
80 fgClearCallBacks( window );
83 * If the freeglut internals haven't been initialized yet,
84 * do it now. Hack's idea courtesy of Chris Purnell...
86 if( !fgState.Initalized )
87 glutInit( &fakeArgc, NULL );
90 * 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 );
104 * Set the default mouse cursor and reset the modifiers value
106 window->State.Cursor = GLUT_CURSOR_INHERIT;
107 window->State.Modifiers = 0xffffffff;
109 window->IsMenu = fgState.BuildingAMenu ;
112 * Open the window now. The fgOpenWindow() function is system
113 * dependant, and resides in freeglut_window.c. Uses fgState.
115 fgOpenWindow( window, title, x, y, w, h, gameMode,
116 parent ? GL_TRUE : GL_FALSE );
122 * This private function creates a menu and adds it to the menus list
124 SFG_Menu* fgCreateMenu( FGCBMenu menuCallback )
126 int x = 100, y = 100, w = 100, h = 100;
127 SFG_Window *current_window = fgStructure.Window;
130 * Have the menu object created
132 SFG_Menu* menu = (SFG_Menu *)calloc( sizeof(SFG_Menu), 1 );
136 * If the freeglut internals haven't been initialized yet,
137 * do it now. Hack's idea courtesy of Chris Purnell...
139 if( !fgState.Initalized )
140 glutInit( &fakeArgc, NULL );
142 menu->ParentWindow = fgStructure.Window;
145 * Create a window for the menu to reside in. Set the
146 * global variable BuildingAMenu to true so we can ensure
147 * it is created without decorations.
149 fgState.BuildingAMenu = GL_TRUE;
151 fgCreateWindow( NULL, NULL, x, y, w, h, GL_FALSE );
152 menu->Window = fgStructure.Window;
153 glutDisplayFunc( fgDisplayMenu );
156 * While BuildingAMenu is true, all windows built have no decorations.
157 * That's not a good default behavior, so let's set it false again.
159 fgState.BuildingAMenu = GL_FALSE;
161 glutHideWindow( ); /* Hide the window for now */
162 fgSetWindow( current_window );
165 * Initialize the object properties:
167 menu->ID = ++fgStructure.MenuID;
168 menu->Callback = menuCallback;
169 menu->ActiveEntry = NULL;
171 fgListInit( &menu->Entries );
172 fgListAppend( &fgStructure.Menus, &menu->Node );
175 * Newly created menus implicitly become current ones
177 fgStructure.Menu = menu;
183 * Linked list of windows to destroy ... this is so we don't destroy a
184 * window from the middle of its callback. Some C compilers take an
185 * extremely dim view of this.
188 static SFG_WindowList* WindowsToDestroy = ( SFG_WindowList* )NULL;
191 * Function to add a window to the linked list of windows to destroy.
192 * Subwindows are automatically added because they hang from the window
195 void fgAddToWindowDestroyList( SFG_Window* window, GLboolean needToClose )
197 SFG_WindowList *new_list_entry =
198 ( SFG_WindowList* )malloc( sizeof(SFG_WindowList ) );
199 new_list_entry->window = window;
200 new_list_entry->needToClose = needToClose;
201 new_list_entry->next = WindowsToDestroy;
202 WindowsToDestroy = new_list_entry;
205 * Check if the window is the current one...
207 if( fgStructure.Window == window )
208 fgStructure.Window = NULL;
211 * Clear all window callbacks except Destroy, which will
212 * be invoked later. Right now, we are potentially carrying
213 * out a freeglut operation at the behest of a client callback,
214 * so we are reluctant to re-enter the client with the Destroy
215 * callback, right now. The others are all wiped out, however,
216 * to ensure that they are no longer called after this point.
219 void *destroy = FETCH_WCB( *window, Destroy );
220 fgClearCallBacks( window );
221 FETCH_WCB( *window, Destroy ) = destroy;
226 * If the destroyed window has the highest window ID number, decrement
227 * the window ID number.
229 * XXX Do we REALLY want to *ever* recycle window IDs? Integers are
230 * XXX plentiful, and clients may rely upon the implied promise in
231 * XXX the GLUT docs to not recycle these. (I can't remember if it
234 * XXX If we *do* want to do this, we should actually recompute the
235 * XXX highest window-ID; the new highest may not in fact be one less
236 * XXX than what we have just deleted.
238 if ( window->ID == fgStructure.WindowID )
239 fgStructure.WindowID--;
242 * Check the execution state. If this has been called from
243 * "glutDestroyWindow", a statement in that function will reset the
244 * "ExecState" after this function returns.
246 if( fgState.ActionOnWindowClose != GLUT_ACTION_CONTINUE_EXECUTION )
248 * Set the execution state flag to drop out of the main loop.
250 if( fgState.ActionOnWindowClose == GLUT_ACTION_EXIT )
251 fgState.ExecState = GLUT_EXEC_STATE_STOP;
255 * Function to close down all the windows in the "WindowsToDestroy" list
257 void fgCloseWindows( )
259 SFG_WindowList *window_ptr = WindowsToDestroy;
260 WindowsToDestroy = ( SFG_WindowList* )NULL;
261 /* In case the destroy callbacks cause more windows to be closed */
265 SFG_WindowList *next = window_ptr->next;
266 fgDestroyWindow( window_ptr->window, window_ptr->needToClose );
272 window_ptr = WindowsToDestroy;
273 WindowsToDestroy = ( SFG_WindowList* )NULL;
279 * This function destroys a window and all of its subwindows. Actually,
280 * another function, defined in freeglut_window.c is called, but this is
281 * a whole different story...
283 void fgDestroyWindow( SFG_Window* window, GLboolean needToClose )
285 SFG_Window* subWindow;
289 freeglut_assert_ready;
291 while( subWindow = ( SFG_Window * )window->Children.First )
292 fgDestroyWindow( subWindow, needToClose );
295 * XXX Since INVOKE_WCB() tests the function pointer, why not make
296 * XXX this unconditional? Overhead is close to nil, and it would
297 * XXX clarify the code by omitting a conditional test.
299 if( FETCH_WCB( *window, Destroy ) )
301 SFG_Window *activeWindow = fgStructure.Window ;
302 INVOKE_WCB( *window, Destroy, ( ) );
303 fgSetWindow ( activeWindow ) ;
307 fgListRemove( &window->Parent->Children, &window->Node );
309 fgListRemove( &fgStructure.Windows, &window->Node );
311 if( window->ActiveMenu )
312 fgDeactivateMenu( window );
314 for ( menu_index = 0; menu_index < 3; menu_index ++ )
316 if ( window->Menu[menu_index] != NULL )
317 window->Menu[menu_index]->ParentWindow = NULL ;
320 fgClearCallBacks( window );
322 fgCloseWindow( window );
324 if( fgStructure.Window == window )
325 fgStructure.Window = NULL;
329 * This is a helper static function that removes a menu (given its pointer)
330 * from any windows that can be accessed from a given parent...
332 static void fghRemoveMenuFromWindow( SFG_Window* window, SFG_Menu* menu )
334 SFG_Window *subWindow;
338 * Check if the menu is attached to the current window,
339 * if so, have it detached (by overwriting with a NULL):
341 for( i = 0; i < 3; i++ )
342 if( window->Menu[ i ] == menu )
343 window->Menu[ i ] = NULL;
346 * Call this function for all of the window's children recursively:
348 for( subWindow = (SFG_Window *)window->Children.First;
350 subWindow = (SFG_Window *)subWindow->Node.Next)
351 fghRemoveMenuFromWindow( subWindow, menu );
355 * This is a static helper function that removes menu references
356 * from another menu, given two pointers to them...
358 static void fghRemoveMenuFromMenu( SFG_Menu* from, SFG_Menu* menu )
360 SFG_MenuEntry *entry;
362 for( entry = (SFG_MenuEntry *)from->Entries.First;
364 entry = ( SFG_MenuEntry * )entry->Node.Next )
365 if( entry->SubMenu == menu )
366 entry->SubMenu = NULL;
370 * This function destroys a menu specified by the parameter. All menus
371 * and windows are updated to make sure no ill pointers hang around.
373 void fgDestroyMenu( SFG_Menu* menu )
377 SFG_MenuEntry *entry;
380 freeglut_assert_ready;
383 * First of all, have all references to this menu removed from all windows:
385 for( window = (SFG_Window *)fgStructure.Windows.First;
387 window = (SFG_Window *)window->Node.Next )
388 fghRemoveMenuFromWindow( window, menu );
391 * Now proceed with removing menu entries that lead to this menu
393 for( from = ( SFG_Menu * )fgStructure.Menus.First;
395 from = ( SFG_Menu * )from->Node.Next )
396 fghRemoveMenuFromMenu( from, menu );
399 * If the programmer defined a destroy callback, call it
400 * A. Donev: But first make this the active menu
404 SFG_Menu *activeMenu=fgStructure.Menu;
405 fgStructure.Menu = menu;
407 fgStructure.Menu = activeMenu;
411 * Now we are pretty sure the menu is not used anywhere
412 * and that we can remove all of its entries
414 while( entry = ( SFG_MenuEntry * )menu->Entries.First )
416 fgListRemove( &menu->Entries, &entry->Node );
426 if( fgStructure.Window == menu->Window )
427 fgSetWindow( menu->ParentWindow );
428 fgDestroyWindow( menu->Window, GL_TRUE );
429 fgListRemove( &fgStructure.Menus, &menu->Node );
430 if( fgStructure.Menu == menu )
431 fgStructure.Menu = NULL;
437 * This function should be called on glutInit(). It will prepare the internal
438 * structure of freeglut to be used in the application. The structure will be
439 * destroyed using fgDestroyStructure() on glutMainLoop() return. In that
440 * case further use of freeglut should be preceeded with a glutInit() call.
442 void fgCreateStructure( void )
445 * We will be needing two lists: the first containing windows,
446 * and the second containing the user-defined menus.
447 * Also, no current window/menu is set, as none has been created yet.
450 fgListInit(&fgStructure.Windows);
451 fgListInit(&fgStructure.Menus);
455 * This function is automatically called on glutMainLoop() return.
456 * It should deallocate and destroy all remnants of previous
457 * glutInit()-enforced structure initialization...
459 void fgDestroyStructure( void )
464 freeglut_assert_ready;
467 * Make sure all windows and menus have been deallocated
469 while( menu = ( SFG_Menu * )fgStructure.Menus.First )
470 fgDestroyMenu( menu );
472 while( window = ( SFG_Window * )fgStructure.Windows.First )
473 fgDestroyWindow( window, GL_TRUE );
477 * Helper function to enumerate through all registered top-level windows
479 void fgEnumWindows( FGCBenumerator enumCallback, SFG_Enumerator* enumerator )
483 assert( enumCallback && enumerator );
484 freeglut_assert_ready;
487 * Check every of the top-level windows
489 for( window = ( SFG_Window * )fgStructure.Windows.First;
491 window = ( SFG_Window * )window->Node.Next )
493 enumCallback( window, enumerator );
494 if( enumerator->found )
500 * Helper function to enumerate through all a window's subwindows
501 * (single level descent)
503 void fgEnumSubWindows( SFG_Window* window, FGCBenumerator enumCallback,
504 SFG_Enumerator* enumerator )
508 assert( enumCallback && enumerator );
509 freeglut_assert_ready;
511 for( child = ( SFG_Window * )window->Children.First;
513 child = ( SFG_Window * )child->Node.Next )
515 enumCallback( child, enumerator );
516 if( enumerator->found )
522 * A static helper function to look for a window given its handle
524 static void fghcbWindowByHandle( SFG_Window *window,
525 SFG_Enumerator *enumerator )
527 if ( enumerator->found )
530 #if TARGET_HOST_UNIX_X11
531 #define WBHANDLE (Window)
532 #elif TARGET_HOST_WIN32
533 #define WBHANDLE (HWND)
537 * Check the window's handle. Hope this works. Looks ugly. That's for sure.
539 if( window->Window.Handle == WBHANDLE (enumerator->data) )
541 enumerator->found = GL_TRUE;
542 enumerator->data = window;
548 * Otherwise, check this window's children
550 fgEnumSubWindows( window, fghcbWindowByHandle, enumerator );
556 * fgWindowByHandle returns a (SFG_Window *) value pointing to the
557 * first window in the queue matching the specified window handle.
558 * The function is defined in freeglut_structure.c file.
560 SFG_Window* fgWindowByHandle
561 #if TARGET_HOST_UNIX_X11
563 #elif TARGET_HOST_WIN32
567 SFG_Enumerator enumerator;
570 * This is easy and makes use of the windows enumeration defined above
572 enumerator.found = GL_FALSE;
573 enumerator.data = (void *)hWindow;
574 fgEnumWindows( fghcbWindowByHandle, &enumerator );
576 if( enumerator.found )
577 return( SFG_Window *) enumerator.data;
582 * A static helper function to look for a window given its ID
584 static void fghcbWindowByID( SFG_Window *window, SFG_Enumerator *enumerator )
587 * Make sure we do not overwrite our precious results...
589 if ( enumerator->found )
593 * Check the window's handle. Hope this works. Looks ugly. That's for sure.
595 if( window->ID == (int) (enumerator->data) ) /* XXX int/ptr conversion! */
597 enumerator->found = GL_TRUE;
598 enumerator->data = window;
604 * Otherwise, check this window's children
606 fgEnumSubWindows( window, fghcbWindowByID, enumerator );
610 * This function is similiar to the previous one, except it is
611 * looking for a specified (sub)window identifier. The function
612 * is defined in freeglut_structure.c file.
614 SFG_Window* fgWindowByID( int windowID )
616 SFG_Enumerator enumerator;
619 * Uses a method very similiar for fgWindowByHandle...
621 enumerator.found = GL_FALSE;
622 enumerator.data = (void *) windowID; /* XXX int/pointer conversion! */
623 fgEnumWindows( fghcbWindowByID, &enumerator );
624 if( enumerator.found )
625 return( SFG_Window *) enumerator.data;
630 * Looks up a menu given its ID. This is easier that fgWindowByXXX
631 * as all menus are placed in a single doubly linked list...
633 SFG_Menu* fgMenuByID( int menuID )
635 SFG_Menu *menu = NULL;
637 freeglut_assert_ready;
640 * It's enough to check all entries in fgStructure.Menus...
642 for( menu = (SFG_Menu *)fgStructure.Menus.First;
644 menu = (SFG_Menu *)menu->Node.Next )
645 if( menu->ID == menuID )
653 void fgListInit(SFG_List *list)
659 void fgListAppend(SFG_List *list, SFG_Node *node)
663 if ( (ln = (SFG_Node *)list->Last) != NULL )
678 void fgListRemove(SFG_List *list, SFG_Node *node)
682 if( ln = (SFG_Node *)node->Next )
683 ln->Prev = node->Prev;
684 if( ln = (SFG_Node *)node->Prev )
685 ln->Next = node->Next;
686 if( ( ln = (SFG_Node *)list->First ) == node )
687 list->First = node->Next;
688 if( ( ln = (SFG_Node *)list->Last ) == node )
689 list->Last = node->Prev;
692 int fgListLength(SFG_List *list)
697 for( node =( SFG_Node * )list->First;
699 node = ( SFG_Node * )node->Next )
705 /*** END OF FILE ***/