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,
73 GLboolean gameMode, GLboolean isMenu )
76 * Have the window object created
78 SFG_Window *window = (SFG_Window *)calloc( sizeof(SFG_Window), 1 );
81 fgClearCallBacks( window );
84 * If the freeglut internals haven't been initialized yet,
85 * do it now. Hack's idea courtesy of Chris Purnell...
87 if( !fgState.Initialised )
88 glutInit( &fakeArgc, NULL );
91 * Initialize the object properties
93 window->ID = ++fgStructure.WindowID;
95 fgListInit( &window->Children );
98 fgListAppend( &parent->Children, &window->Node );
99 window->Parent = parent;
102 fgListAppend( &fgStructure.Windows, &window->Node );
105 * Set the default mouse cursor and reset the modifiers value
107 window->State.Cursor = GLUT_CURSOR_INHERIT;
109 window->IsMenu = isMenu;
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.Initialised )
140 glutInit( &fakeArgc, NULL );
142 menu->ParentWindow = fgStructure.Window;
145 * Create a window for the menu to reside in.
148 fgCreateWindow( NULL, NULL, x, y, w, h, GL_FALSE, GL_TRUE );
149 menu->Window = fgStructure.Window;
150 glutDisplayFunc( fgDisplayMenu );
152 glutHideWindow( ); /* Hide the window for now */
153 fgSetWindow( current_window );
156 * Initialize the object properties:
158 menu->ID = ++fgStructure.MenuID;
159 menu->Callback = menuCallback;
160 menu->ActiveEntry = NULL;
162 fgListInit( &menu->Entries );
163 fgListAppend( &fgStructure.Menus, &menu->Node );
166 * Newly created menus implicitly become current ones
168 fgStructure.Menu = menu;
174 * Linked list of windows to destroy ... this is so we don't destroy a
175 * window from the middle of its callback. Some C compilers take an
176 * extremely dim view of this.
179 static SFG_WindowList* WindowsToDestroy = ( SFG_WindowList* )NULL;
182 * Function to add a window to the linked list of windows to destroy.
183 * Subwindows are automatically added because they hang from the window
186 void fgAddToWindowDestroyList( SFG_Window* window, GLboolean needToClose )
188 SFG_WindowList *new_list_entry =
189 ( SFG_WindowList* )malloc( sizeof(SFG_WindowList ) );
190 new_list_entry->window = window;
191 new_list_entry->needToClose = needToClose;
192 new_list_entry->next = WindowsToDestroy;
193 WindowsToDestroy = new_list_entry;
196 * Check if the window is the current one...
198 if( fgStructure.Window == window )
199 fgStructure.Window = NULL;
202 * Clear all window callbacks except Destroy, which will
203 * be invoked later. Right now, we are potentially carrying
204 * out a freeglut operation at the behest of a client callback,
205 * so we are reluctant to re-enter the client with the Destroy
206 * callback, right now. The others are all wiped out, however,
207 * to ensure that they are no longer called after this point.
210 void *destroy = FETCH_WCB( *window, Destroy );
211 fgClearCallBacks( window );
212 FETCH_WCB( *window, Destroy ) = destroy;
216 * Check the execution state. If this has been called from
217 * "glutDestroyWindow", a statement in that function will reset the
218 * "ExecState" after this function returns.
220 if( fgState.ActionOnWindowClose != GLUT_ACTION_CONTINUE_EXECUTION )
222 * Set the execution state flag to drop out of the main loop.
224 if( fgState.ActionOnWindowClose == GLUT_ACTION_EXIT )
225 fgState.ExecState = GLUT_EXEC_STATE_STOP;
229 * Function to close down all the windows in the "WindowsToDestroy" list
231 void fgCloseWindows( )
233 SFG_WindowList *window_ptr = WindowsToDestroy;
234 WindowsToDestroy = ( SFG_WindowList* )NULL;
235 /* In case the destroy callbacks cause more windows to be closed */
239 SFG_WindowList *next = window_ptr->next;
240 fgDestroyWindow( window_ptr->window, window_ptr->needToClose );
246 window_ptr = WindowsToDestroy;
247 WindowsToDestroy = ( SFG_WindowList* )NULL;
253 * This function destroys a window and all of its subwindows. Actually,
254 * another function, defined in freeglut_window.c is called, but this is
255 * a whole different story...
257 void fgDestroyWindow( SFG_Window* window, GLboolean needToClose )
259 SFG_Window* subWindow;
263 freeglut_assert_ready;
265 while( subWindow = ( SFG_Window * )window->Children.First )
266 fgDestroyWindow( subWindow, needToClose );
269 SFG_Window *activeWindow = fgStructure.Window ;
270 INVOKE_WCB( *window, Destroy, ( ) );
271 fgSetWindow ( activeWindow );
275 fgListRemove( &window->Parent->Children, &window->Node );
277 fgListRemove( &fgStructure.Windows, &window->Node );
279 if( window->ActiveMenu )
280 fgDeactivateMenu( window );
282 for ( menu_index = 0; menu_index < 3; menu_index ++ )
284 if ( window->Menu[menu_index] )
285 window->Menu[menu_index]->ParentWindow = NULL ;
288 fgClearCallBacks( window );
290 fgCloseWindow( window );
292 if( fgStructure.Window == window )
293 fgStructure.Window = NULL;
297 * This is a helper static function that removes a menu (given its pointer)
298 * from any windows that can be accessed from a given parent...
300 static void fghRemoveMenuFromWindow( SFG_Window* window, SFG_Menu* menu )
302 SFG_Window *subWindow;
306 * Check if the menu is attached to the current window,
307 * if so, have it detached (by overwriting with a NULL):
309 for( i = 0; i < 3; i++ )
310 if( window->Menu[ i ] == menu )
311 window->Menu[ i ] = NULL;
314 * Call this function for all of the window's children recursively:
316 for( subWindow = (SFG_Window *)window->Children.First;
318 subWindow = (SFG_Window *)subWindow->Node.Next)
319 fghRemoveMenuFromWindow( subWindow, menu );
323 * This is a static helper function that removes menu references
324 * from another menu, given two pointers to them...
326 static void fghRemoveMenuFromMenu( SFG_Menu* from, SFG_Menu* menu )
328 SFG_MenuEntry *entry;
330 for( entry = (SFG_MenuEntry *)from->Entries.First;
332 entry = ( SFG_MenuEntry * )entry->Node.Next )
333 if( entry->SubMenu == menu )
334 entry->SubMenu = NULL;
338 * This function destroys a menu specified by the parameter. All menus
339 * and windows are updated to make sure no ill pointers hang around.
341 void fgDestroyMenu( SFG_Menu* menu )
345 SFG_MenuEntry *entry;
348 freeglut_assert_ready;
351 * First of all, have all references to this menu removed from all windows:
353 for( window = (SFG_Window *)fgStructure.Windows.First;
355 window = (SFG_Window *)window->Node.Next )
356 fghRemoveMenuFromWindow( window, menu );
359 * Now proceed with removing menu entries that lead to this menu
361 for( from = ( SFG_Menu * )fgStructure.Menus.First;
363 from = ( SFG_Menu * )from->Node.Next )
364 fghRemoveMenuFromMenu( from, menu );
367 * If the programmer defined a destroy callback, call it
368 * A. Donev: But first make this the active menu
372 SFG_Menu *activeMenu=fgStructure.Menu;
373 fgStructure.Menu = menu;
375 fgStructure.Menu = activeMenu;
379 * Now we are pretty sure the menu is not used anywhere
380 * and that we can remove all of its entries
382 while( entry = ( SFG_MenuEntry * )menu->Entries.First )
384 fgListRemove( &menu->Entries, &entry->Node );
394 if( fgStructure.Window == menu->Window )
395 fgSetWindow( menu->ParentWindow );
396 fgDestroyWindow( menu->Window, GL_TRUE );
397 fgListRemove( &fgStructure.Menus, &menu->Node );
398 if( fgStructure.Menu == menu )
399 fgStructure.Menu = NULL;
405 * This function should be called on glutInit(). It will prepare the internal
406 * structure of freeglut to be used in the application. The structure will be
407 * destroyed using fgDestroyStructure() on glutMainLoop() return. In that
408 * case further use of freeglut should be preceeded with a glutInit() call.
410 void fgCreateStructure( void )
413 * We will be needing two lists: the first containing windows,
414 * and the second containing the user-defined menus.
415 * Also, no current window/menu is set, as none has been created yet.
418 fgListInit(&fgStructure.Windows);
419 fgListInit(&fgStructure.Menus);
423 * This function is automatically called on glutMainLoop() return.
424 * It should deallocate and destroy all remnants of previous
425 * glutInit()-enforced structure initialization...
427 void fgDestroyStructure( void )
432 freeglut_assert_ready;
435 * Make sure all windows and menus have been deallocated
437 while( menu = ( SFG_Menu * )fgStructure.Menus.First )
438 fgDestroyMenu( menu );
440 while( window = ( SFG_Window * )fgStructure.Windows.First )
441 fgDestroyWindow( window, GL_TRUE );
445 * Helper function to enumerate through all registered top-level windows
447 void fgEnumWindows( FGCBenumerator enumCallback, SFG_Enumerator* enumerator )
451 assert( enumCallback && enumerator );
452 freeglut_assert_ready;
455 * Check every of the top-level windows
457 for( window = ( SFG_Window * )fgStructure.Windows.First;
459 window = ( SFG_Window * )window->Node.Next )
461 enumCallback( window, enumerator );
462 if( enumerator->found )
468 * Helper function to enumerate through all a window's subwindows
469 * (single level descent)
471 void fgEnumSubWindows( SFG_Window* window, FGCBenumerator enumCallback,
472 SFG_Enumerator* enumerator )
476 assert( enumCallback && enumerator );
477 freeglut_assert_ready;
479 for( child = ( SFG_Window * )window->Children.First;
481 child = ( SFG_Window * )child->Node.Next )
483 enumCallback( child, enumerator );
484 if( enumerator->found )
490 * A static helper function to look for a window given its handle
492 static void fghcbWindowByHandle( SFG_Window *window,
493 SFG_Enumerator *enumerator )
495 if ( enumerator->found )
499 * Check the window's handle. Hope this works. Looks ugly. That's for sure.
501 if( window->Window.Handle == (SFG_WindowHandleType) (enumerator->data) )
503 enumerator->found = GL_TRUE;
504 enumerator->data = window;
510 * Otherwise, check this window's children
512 fgEnumSubWindows( window, fghcbWindowByHandle, enumerator );
516 * fgWindowByHandle returns a (SFG_Window *) value pointing to the
517 * first window in the queue matching the specified window handle.
518 * The function is defined in freeglut_structure.c file.
520 SFG_Window* fgWindowByHandle ( SFG_WindowHandleType hWindow )
522 SFG_Enumerator enumerator;
525 * This is easy and makes use of the windows enumeration defined above
527 enumerator.found = GL_FALSE;
528 enumerator.data = (void *)hWindow;
529 fgEnumWindows( fghcbWindowByHandle, &enumerator );
531 if( enumerator.found )
532 return( SFG_Window *) enumerator.data;
537 * A static helper function to look for a window given its ID
539 static void fghcbWindowByID( SFG_Window *window, SFG_Enumerator *enumerator )
542 * Make sure we do not overwrite our precious results...
544 if ( enumerator->found )
548 * Check the window's handle. Hope this works. Looks ugly. That's for sure.
550 if( window->ID == (int) (enumerator->data) ) /* XXX int/ptr conversion! */
552 enumerator->found = GL_TRUE;
553 enumerator->data = window;
559 * Otherwise, check this window's children
561 fgEnumSubWindows( window, fghcbWindowByID, enumerator );
565 * This function is similiar to the previous one, except it is
566 * looking for a specified (sub)window identifier. The function
567 * is defined in freeglut_structure.c file.
569 SFG_Window* fgWindowByID( int windowID )
571 SFG_Enumerator enumerator;
574 * Uses a method very similiar for fgWindowByHandle...
576 enumerator.found = GL_FALSE;
577 enumerator.data = (void *) windowID; /* XXX int/pointer conversion! */
578 fgEnumWindows( fghcbWindowByID, &enumerator );
579 if( enumerator.found )
580 return( SFG_Window *) enumerator.data;
585 * Looks up a menu given its ID. This is easier that fgWindowByXXX
586 * as all menus are placed in a single doubly linked list...
588 SFG_Menu* fgMenuByID( int menuID )
590 SFG_Menu *menu = NULL;
592 freeglut_assert_ready;
595 * It's enough to check all entries in fgStructure.Menus...
597 for( menu = (SFG_Menu *)fgStructure.Menus.First;
599 menu = (SFG_Menu *)menu->Node.Next )
600 if( menu->ID == menuID )
608 void fgListInit(SFG_List *list)
614 void fgListAppend(SFG_List *list, SFG_Node *node)
618 if ( ln = (SFG_Node *)list->Last )
633 void fgListRemove(SFG_List *list, SFG_Node *node)
637 if( ln = (SFG_Node *)node->Next )
638 ln->Prev = node->Prev;
639 if( ln = (SFG_Node *)node->Prev )
640 ln->Next = node->Next;
641 if( (ln = (SFG_Node *)list->First) == node )
642 list->First = node->Next;
643 if( (ln = (SFG_Node *)list->Last) == node )
644 list->Last = node->Prev;
647 int fgListLength(SFG_List *list)
652 for( node =( SFG_Node * )list->First;
654 node = ( SFG_Node * )node->Next )
660 /*** END OF FILE ***/