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 )
58 for( i = 0; i < TOTAL_CALLBACKS; ++i )
59 window->CallBacks[ i ] = NULL;
63 * This private function creates, opens and adds to the hierarchy
64 * a freeglut window complete with OpenGL context and stuff...
66 * If parent is set to NULL, the window created will be a topmost one.
68 SFG_Window* fgCreateWindow( SFG_Window* parent, const char* title,
69 int x, int y, int w, int h, GLboolean gameMode )
72 * Have the window object created
74 SFG_Window *window = (SFG_Window *)calloc( sizeof(SFG_Window), 1 );
77 fgClearCallBacks( window );
80 * If the freeglut internals haven't been initialized yet,
81 * do it now. Hack's idea courtesy of Chris Purnell...
83 if( !fgState.Time.Set )
84 glutInit( &fakeArgc, NULL );
87 * 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 );
101 * Set the default mouse cursor and reset the modifiers value
103 window->State.Cursor = GLUT_CURSOR_INHERIT;
104 window->State.Modifiers = 0xffffffff;
106 window->IsMenu = fgState.BuildingAMenu ;
109 * Open the window now. The fgOpenWindow() function is system
110 * dependant, and resides in freeglut_window.c. Uses fgState.
112 fgOpenWindow( window, title, x, y, w, h, gameMode,
113 (parent != NULL) ? TRUE : FALSE );
119 * This private function creates a menu and adds it to the menus list
121 SFG_Menu* fgCreateMenu( FGCBMenu menuCallback )
123 int x = 100, y = 100, w = 100, h = 100 ;
124 SFG_Window *current_window = fgStructure.Window ;
127 * Have the menu object created
129 SFG_Menu* menu = (SFG_Menu *)calloc( sizeof(SFG_Menu), 1 );
133 * If the freeglut internals haven't been initialized yet,
134 * do it now. Hack's idea courtesy of Chris Purnell...
136 if( !fgState.Time.Set )
137 glutInit( &fakeArgc, NULL );
139 menu->ParentWindow = fgStructure.Window ;
142 * Create a window for the menu to reside in. Set the
143 * global variable BuildingAMenu to true so we can ensure
144 * it is created without decorations.
146 fgState.BuildingAMenu = TRUE ;
148 fgCreateWindow ( NULL, NULL, x, y, w, h, FALSE ) ;
149 menu->Window = fgStructure.Window ;
150 glutDisplayFunc ( fgDisplayMenu ) ;
153 * While BuildingAMenu is true, all windows built have no decorations.
154 * That's not a good default behavior, so let's set it false again.
156 fgState.BuildingAMenu = FALSE ;
158 glutHideWindow () ; /* Hide the window for now */
159 fgSetWindow ( current_window ) ;
162 * Initialize the object properties:
164 menu->ID = ++fgStructure.MenuID;
165 menu->Callback = menuCallback;
166 menu->ActiveEntry = NULL ;
168 fgListInit( &menu->Entries );
169 fgListAppend( &fgStructure.Menus, &menu->Node );
172 * Newly created menus implicitly become current ones
174 fgStructure.Menu = menu;
180 * Linked list of windows to destroy ... this is so we don't destroy a
181 * window from the middle of its callback. Some C compilers take an
182 * extremely dim view of this.
185 static SFG_WindowList* WindowsToDestroy = (SFG_WindowList*)NULL ;
188 * Function to add a window to the linked list of windows to destroy.
189 * Subwindows are automatically added because they hang from the window
192 void fgAddToWindowDestroyList ( SFG_Window* window, GLboolean needToClose )
194 SFG_WindowList *new_list_entry =
195 ( SFG_WindowList* )malloc( sizeof(SFG_WindowList ) );
196 new_list_entry->window = window;
197 new_list_entry->needToClose = needToClose;
198 new_list_entry->next = WindowsToDestroy;
199 WindowsToDestroy = new_list_entry;
202 * Check if the window is the current one...
204 if( fgStructure.Window == window )
205 fgStructure.Window = NULL;
208 * Clear all window callbacks except Destroy, which will
209 * be invoked later. Right now, we are potentially carrying
210 * out a freeglut operation at the behest of a client callback,
211 * so we are reluctant to re-enter the client with the Destroy
212 * callback, right now. The others are all wiped out, however,
213 * to ensure that they are no longer called after this point.
216 void *destroy = FETCH_WCB( *window, Destroy );
217 fgClearCallBacks( window );
218 FETCH_WCB( *window, Destroy ) = destroy;
223 * If the destroyed window has the highest window ID number, decrement
224 * the window ID number.
226 * XXX Do we REALLY want to *ever* recycle window IDs? Integers are
227 * XXX plentiful, and clients may rely upon the implied promise in
228 * XXX the GLUT docs to not recycle these. (I can't remember if it
231 * XXX If we *do* want to do this, we should actually recompute the
232 * XXX highest window-ID; the new highest may not in fact be one less
233 * XXX than what we have just deleted.
235 if ( window->ID == fgStructure.WindowID )
236 fgStructure.WindowID--;
239 * Check the execution state. If this has been called from
240 * "glutDestroyWindow", a statement in that function will reset the
241 * "ExecState" after this function returns.
243 if( fgState.ActionOnWindowClose != GLUT_ACTION_CONTINUE_EXECUTION )
245 * Set the execution state flag to drop out of the main loop.
247 if( fgState.ActionOnWindowClose == GLUT_ACTION_EXIT )
248 fgState.ExecState = GLUT_EXEC_STATE_STOP;
252 * Function to close down all the windows in the "WindowsToDestroy" list
254 void fgCloseWindows( )
256 SFG_WindowList *window_ptr = WindowsToDestroy;
257 WindowsToDestroy = ( SFG_WindowList* )NULL;
258 /* In case the destroy callbacks cause more windows to be closed */
262 SFG_WindowList *next = window_ptr->next;
263 fgDestroyWindow( window_ptr->window, window_ptr->needToClose );
269 window_ptr = WindowsToDestroy;
270 WindowsToDestroy = ( SFG_WindowList* )NULL;
276 * This function destroys a window and all of its subwindows. Actually,
277 * another function, defined in freeglut_window.c is called, but this is
278 * a whole different story...
280 void fgDestroyWindow( SFG_Window* window, GLboolean needToClose )
282 SFG_Window* subWindow;
285 assert( window != NULL );
286 freeglut_assert_ready;
288 while ( (subWindow = (SFG_Window *)window->Children.First) != NULL )
289 fgDestroyWindow( subWindow, needToClose );
292 * XXX Since INVOKE_WCB() tests the function pointer, why not make
293 * XXX this unconditional? Overhead is close to nil, and it would
294 * XXX clarify the code by omitting a conditional test.
296 if ( FETCH_WCB( *window, Destroy ) )
298 SFG_Window *activeWindow = fgStructure.Window ;
299 INVOKE_WCB( *window, Destroy, ( ) );
300 fgSetWindow ( activeWindow ) ;
303 if ( window->Parent != NULL )
304 fgListRemove( &window->Parent->Children, &window->Node );
306 fgListRemove( &fgStructure.Windows, &window->Node );
308 if ( window->ActiveMenu != NULL )
309 fgDeactivateMenu ( window ) ;
311 for ( menu_index = 0; menu_index < 3; menu_index ++ )
313 if ( window->Menu[menu_index] != NULL )
314 window->Menu[menu_index]->ParentWindow = NULL ;
317 if( needToClose == TRUE )
318 fgCloseWindow( window );
320 if ( fgStructure.Window == window )
321 fgStructure.Window = NULL ;
322 fgClearCallBacks( window );
326 * This is a helper static function that removes a menu (given its pointer)
327 * from any windows that can be accessed from a given parent...
329 static void fghRemoveMenuFromWindow( SFG_Window* window, SFG_Menu* menu )
331 SFG_Window *subWindow;
335 * Check if the menu is attached to the current window,
336 * if so, have it detached (by overwriting with a NULL):
339 if( window->Menu[ i ] == menu )
340 window->Menu[ i ] = NULL;
343 * Call this function for all of the window's children recursively:
345 for( subWindow = (SFG_Window *)window->Children.First; subWindow;
346 subWindow = (SFG_Window *)subWindow->Node.Next)
347 fghRemoveMenuFromWindow( subWindow, menu );
351 * This is a static helper function that removes menu references
352 * from another menu, given two pointers to them...
354 static void fghRemoveMenuFromMenu( SFG_Menu* from, SFG_Menu* menu )
356 SFG_MenuEntry *entry;
358 for( entry = (SFG_MenuEntry *)from->Entries.First;
360 entry = (SFG_MenuEntry *)entry->Node.Next )
361 if (entry->SubMenu == menu)
362 entry->SubMenu = NULL;
366 * This function destroys a menu specified by the parameter. All menus
367 * and windows are updated to make sure no ill pointers hang around.
369 void fgDestroyMenu( SFG_Menu* menu )
373 SFG_MenuEntry *entry;
375 assert( menu != NULL );
376 freeglut_assert_ready;
379 * First of all, have all references to this menu removed from all windows:
381 for( window = (SFG_Window *)fgStructure.Windows.First;
383 window = (SFG_Window *)window->Node.Next )
384 fghRemoveMenuFromWindow( window, menu );
387 * Now proceed with removing menu entries that lead to this menu
389 for( from = (SFG_Menu *)fgStructure.Menus.First;
391 from = (SFG_Menu *)from->Node.Next )
392 fghRemoveMenuFromMenu( from, menu );
395 * If the programmer defined a destroy callback, call it
396 * A. Donev: But first make this the active menu
398 if ( menu->Destroy != NULL )
400 SFG_Menu *activeMenu=fgStructure.Menu;
401 fgStructure.Menu = menu;
403 fgStructure.Menu = activeMenu;
407 * Now we are pretty sure the menu is not used anywhere
408 * and that we can remove all of its entries
410 while( (entry = (SFG_MenuEntry *)menu->Entries.First) != NULL )
412 fgListRemove(&menu->Entries, &entry->Node);
422 if ( fgStructure.Window == menu->Window )
423 fgSetWindow ( menu->ParentWindow ) ;
424 fgDestroyWindow ( menu->Window, TRUE ) ;
425 fgListRemove( &fgStructure.Menus, &menu->Node );
426 if( fgStructure.Menu == menu )
427 fgStructure.Menu = NULL;
433 * This function should be called on glutInit(). It will prepare the internal
434 * structure of freeglut to be used in the application. The structure will be
435 * destroyed using fgDestroyStructure() on glutMainLoop() return. In that
436 * case further use of freeglut should be preceeded with a glutInit() call.
438 void fgCreateStructure( void )
441 * We will be needing two lists: the first containing windows,
442 * and the second containing the user-defined menus.
443 * Also, no current window/menu is set, as none has been created yet.
446 fgListInit(&fgStructure.Windows);
447 fgListInit(&fgStructure.Menus);
451 * This function is automatically called on glutMainLoop() return.
452 * It should deallocate and destroy all remnants of previous
453 * glutInit()-enforced structure initialization...
455 void fgDestroyStructure( void )
460 freeglut_assert_ready;
463 * Make sure all windows and menus have been deallocated
465 while( (menu = (SFG_Menu *)fgStructure.Menus.First) != NULL )
466 fgDestroyMenu( menu );
468 while( (window = (SFG_Window *)fgStructure.Windows.First) != NULL )
469 fgDestroyWindow( window, TRUE );
473 * Helper function to enumerate through all registered top-level windows
475 void fgEnumWindows( FGCBenumerator enumCallback, SFG_Enumerator* enumerator )
479 assert( (enumCallback != NULL) && (enumerator != NULL) );
480 freeglut_assert_ready;
483 * Check every of the top-level windows
485 for( window = (SFG_Window *)fgStructure.Windows.First;
487 window = (SFG_Window *)window->Node.Next )
489 enumCallback( window, enumerator );
490 if( enumerator->found == TRUE )
496 * Helper function to enumerate through all a window's subwindows
497 * (single level descent)
499 void fgEnumSubWindows( SFG_Window* window, FGCBenumerator enumCallback,
500 SFG_Enumerator* enumerator )
504 assert( (enumCallback != NULL) && (enumerator != NULL) );
505 freeglut_assert_ready;
507 for( child = (SFG_Window *)window->Children.First;
509 child = (SFG_Window *)child->Node.Next )
511 enumCallback( child, enumerator );
512 if( enumerator->found == TRUE )
518 * A static helper function to look for a window given its handle
520 static void fghcbWindowByHandle( SFG_Window *window,
521 SFG_Enumerator *enumerator )
523 freeglut_return_if_fail( enumerator->found == FALSE );
525 #if TARGET_HOST_UNIX_X11
526 #define WBHANDLE (Window)
527 #elif TARGET_HOST_WIN32
528 #define WBHANDLE (HWND)
532 * Check the window's handle. Hope this works. Looks ugly. That's for sure.
534 if( window->Window.Handle == WBHANDLE (enumerator->data) )
536 enumerator->found = TRUE;
537 enumerator->data = window;
543 * Otherwise, check this window's children
545 fgEnumSubWindows( window, fghcbWindowByHandle, enumerator );
551 * fgWindowByHandle returns a (SFG_Window *) value pointing to the
552 * first window in the queue matching the specified window handle.
553 * The function is defined in freeglut_structure.c file.
555 SFG_Window* fgWindowByHandle
556 #if TARGET_HOST_UNIX_X11
558 #elif TARGET_HOST_WIN32
562 SFG_Enumerator enumerator;
565 * This is easy and makes use of the windows enumeration defined above
567 enumerator.found = FALSE;
568 enumerator.data = (void *)hWindow;
569 fgEnumWindows( fghcbWindowByHandle, &enumerator );
571 if( enumerator.found == TRUE )
572 return( SFG_Window *) enumerator.data;
577 * A static helper function to look for a window given its ID
579 static void fghcbWindowByID( SFG_Window *window, SFG_Enumerator *enumerator )
582 * Make sure we do not overwrite our precious results...
584 freeglut_return_if_fail( enumerator->found == FALSE );
587 * Check the window's handle. Hope this works. Looks ugly. That's for sure.
589 if( window->ID == (int) (enumerator->data) ) /* XXX int/ptr conversion! */
591 enumerator->found = TRUE;
592 enumerator->data = window;
598 * Otherwise, check this window's children
600 fgEnumSubWindows( window, fghcbWindowByID, enumerator );
604 * This function is similiar to the previous one, except it is
605 * looking for a specified (sub)window identifier. The function
606 * is defined in freeglut_structure.c file.
608 SFG_Window* fgWindowByID( int windowID )
610 SFG_Enumerator enumerator;
613 * Uses a method very similiar for fgWindowByHandle...
615 enumerator.found = FALSE;
616 enumerator.data = (void *) windowID; /* XXX int/pointer conversion! */
617 fgEnumWindows( fghcbWindowByID, &enumerator );
618 if( enumerator.found == TRUE )
619 return( SFG_Window *) enumerator.data;
624 * Looks up a menu given its ID. This is easier that fgWindowByXXX
625 * as all menus are placed in a single doubly linked list...
627 SFG_Menu* fgMenuByID( int menuID )
629 SFG_Menu *menu = NULL;
631 freeglut_assert_ready;
634 * It's enough to check all entries in fgStructure.Menus...
636 for( menu = (SFG_Menu *)fgStructure.Menus.First;
638 menu = (SFG_Menu *)menu->Node.Next )
639 if( menu->ID == menuID )
647 void fgListInit(SFG_List *list)
653 void fgListAppend(SFG_List *list, SFG_Node *node)
657 if ( (ln = (SFG_Node *)list->Last) != NULL )
672 void fgListRemove(SFG_List *list, SFG_Node *node)
676 if ( (ln = (SFG_Node *)node->Next) != NULL )
677 ln->Prev = node->Prev;
678 if ( (ln = (SFG_Node *)node->Prev) != NULL )
679 ln->Next = node->Next;
680 if ( (ln = (SFG_Node *)list->First) == node )
681 list->First = node->Next;
682 if ( (ln = (SFG_Node *)list->Last) == node )
683 list->Last = node->Prev;
686 int fgListLength(SFG_List *list)
691 for( node = (SFG_Node *)list->First; node; node = (SFG_Node *)node->Next )
697 /*** END OF FILE ***/