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.Initalized )
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;
108 window->State.Modifiers = 0xffffffff;
110 window->IsMenu = isMenu;
113 * Open the window now. The fgOpenWindow() function is system
114 * dependant, and resides in freeglut_window.c. Uses fgState.
116 fgOpenWindow( window, title, x, y, w, h, gameMode,
117 parent ? GL_TRUE : GL_FALSE );
123 * This private function creates a menu and adds it to the menus list
125 SFG_Menu* fgCreateMenu( FGCBMenu menuCallback )
127 int x = 100, y = 100, w = 100, h = 100;
128 SFG_Window *current_window = fgStructure.Window;
131 * Have the menu object created
133 SFG_Menu* menu = (SFG_Menu *)calloc( sizeof(SFG_Menu), 1 );
137 * If the freeglut internals haven't been initialized yet,
138 * do it now. Hack's idea courtesy of Chris Purnell...
140 if( !fgState.Initalized )
141 glutInit( &fakeArgc, NULL );
143 menu->ParentWindow = fgStructure.Window;
146 * Create a window for the menu to reside in.
149 fgCreateWindow( NULL, NULL, x, y, w, h, GL_FALSE, GL_TRUE );
150 menu->Window = fgStructure.Window;
151 glutDisplayFunc( fgDisplayMenu );
153 glutHideWindow( ); /* Hide the window for now */
154 fgSetWindow( current_window );
157 * Initialize the object properties:
159 menu->ID = ++fgStructure.MenuID;
160 menu->Callback = menuCallback;
161 menu->ActiveEntry = NULL;
163 fgListInit( &menu->Entries );
164 fgListAppend( &fgStructure.Menus, &menu->Node );
167 * Newly created menus implicitly become current ones
169 fgStructure.Menu = menu;
175 * Linked list of windows to destroy ... this is so we don't destroy a
176 * window from the middle of its callback. Some C compilers take an
177 * extremely dim view of this.
180 static SFG_WindowList* WindowsToDestroy = ( SFG_WindowList* )NULL;
183 * Function to add a window to the linked list of windows to destroy.
184 * Subwindows are automatically added because they hang from the window
187 void fgAddToWindowDestroyList( SFG_Window* window, GLboolean needToClose )
189 SFG_WindowList *new_list_entry =
190 ( SFG_WindowList* )malloc( sizeof(SFG_WindowList ) );
191 new_list_entry->window = window;
192 new_list_entry->needToClose = needToClose;
193 new_list_entry->next = WindowsToDestroy;
194 WindowsToDestroy = new_list_entry;
197 * Check if the window is the current one...
199 if( fgStructure.Window == window )
200 fgStructure.Window = NULL;
203 * Clear all window callbacks except Destroy, which will
204 * be invoked later. Right now, we are potentially carrying
205 * out a freeglut operation at the behest of a client callback,
206 * so we are reluctant to re-enter the client with the Destroy
207 * callback, right now. The others are all wiped out, however,
208 * to ensure that they are no longer called after this point.
211 void *destroy = FETCH_WCB( *window, Destroy );
212 fgClearCallBacks( window );
213 FETCH_WCB( *window, Destroy ) = destroy;
218 * If the destroyed window has the highest window ID number, decrement
219 * the window ID number.
221 * XXX Do we REALLY want to *ever* recycle window IDs? Integers are
222 * XXX plentiful, and clients may rely upon the implied promise in
223 * XXX the GLUT docs to not recycle these. (I can't remember if it
226 * XXX If we *do* want to do this, we should actually recompute the
227 * XXX highest window-ID; the new highest may not in fact be one less
228 * XXX than what we have just deleted.
230 if ( window->ID == fgStructure.WindowID )
231 fgStructure.WindowID--;
234 * Check the execution state. If this has been called from
235 * "glutDestroyWindow", a statement in that function will reset the
236 * "ExecState" after this function returns.
238 if( fgState.ActionOnWindowClose != GLUT_ACTION_CONTINUE_EXECUTION )
240 * Set the execution state flag to drop out of the main loop.
242 if( fgState.ActionOnWindowClose == GLUT_ACTION_EXIT )
243 fgState.ExecState = GLUT_EXEC_STATE_STOP;
247 * Function to close down all the windows in the "WindowsToDestroy" list
249 void fgCloseWindows( )
251 SFG_WindowList *window_ptr = WindowsToDestroy;
252 WindowsToDestroy = ( SFG_WindowList* )NULL;
253 /* In case the destroy callbacks cause more windows to be closed */
257 SFG_WindowList *next = window_ptr->next;
258 fgDestroyWindow( window_ptr->window, window_ptr->needToClose );
264 window_ptr = WindowsToDestroy;
265 WindowsToDestroy = ( SFG_WindowList* )NULL;
271 * This function destroys a window and all of its subwindows. Actually,
272 * another function, defined in freeglut_window.c is called, but this is
273 * a whole different story...
275 void fgDestroyWindow( SFG_Window* window, GLboolean needToClose )
277 SFG_Window* subWindow;
281 freeglut_assert_ready;
283 while( subWindow = ( SFG_Window * )window->Children.First )
284 fgDestroyWindow( subWindow, needToClose );
287 * XXX Since INVOKE_WCB() tests the function pointer, why not make
288 * XXX this unconditional? Overhead is close to nil, and it would
289 * XXX clarify the code by omitting a conditional test.
291 if( FETCH_WCB( *window, Destroy ) )
293 SFG_Window *activeWindow = fgStructure.Window ;
294 INVOKE_WCB( *window, Destroy, ( ) );
295 fgSetWindow ( activeWindow ) ;
299 fgListRemove( &window->Parent->Children, &window->Node );
301 fgListRemove( &fgStructure.Windows, &window->Node );
303 if( window->ActiveMenu )
304 fgDeactivateMenu( window );
306 for ( menu_index = 0; menu_index < 3; menu_index ++ )
308 if ( window->Menu[menu_index] != NULL )
309 window->Menu[menu_index]->ParentWindow = NULL ;
312 fgClearCallBacks( window );
314 fgCloseWindow( window );
316 if( fgStructure.Window == window )
317 fgStructure.Window = NULL;
321 * This is a helper static function that removes a menu (given its pointer)
322 * from any windows that can be accessed from a given parent...
324 static void fghRemoveMenuFromWindow( SFG_Window* window, SFG_Menu* menu )
326 SFG_Window *subWindow;
330 * Check if the menu is attached to the current window,
331 * if so, have it detached (by overwriting with a NULL):
333 for( i = 0; i < 3; i++ )
334 if( window->Menu[ i ] == menu )
335 window->Menu[ i ] = NULL;
338 * Call this function for all of the window's children recursively:
340 for( subWindow = (SFG_Window *)window->Children.First;
342 subWindow = (SFG_Window *)subWindow->Node.Next)
343 fghRemoveMenuFromWindow( subWindow, menu );
347 * This is a static helper function that removes menu references
348 * from another menu, given two pointers to them...
350 static void fghRemoveMenuFromMenu( SFG_Menu* from, SFG_Menu* menu )
352 SFG_MenuEntry *entry;
354 for( entry = (SFG_MenuEntry *)from->Entries.First;
356 entry = ( SFG_MenuEntry * )entry->Node.Next )
357 if( entry->SubMenu == menu )
358 entry->SubMenu = NULL;
362 * This function destroys a menu specified by the parameter. All menus
363 * and windows are updated to make sure no ill pointers hang around.
365 void fgDestroyMenu( SFG_Menu* menu )
369 SFG_MenuEntry *entry;
372 freeglut_assert_ready;
375 * First of all, have all references to this menu removed from all windows:
377 for( window = (SFG_Window *)fgStructure.Windows.First;
379 window = (SFG_Window *)window->Node.Next )
380 fghRemoveMenuFromWindow( window, menu );
383 * Now proceed with removing menu entries that lead to this menu
385 for( from = ( SFG_Menu * )fgStructure.Menus.First;
387 from = ( SFG_Menu * )from->Node.Next )
388 fghRemoveMenuFromMenu( from, menu );
391 * If the programmer defined a destroy callback, call it
392 * A. Donev: But first make this the active menu
396 SFG_Menu *activeMenu=fgStructure.Menu;
397 fgStructure.Menu = menu;
399 fgStructure.Menu = activeMenu;
403 * Now we are pretty sure the menu is not used anywhere
404 * and that we can remove all of its entries
406 while( entry = ( SFG_MenuEntry * )menu->Entries.First )
408 fgListRemove( &menu->Entries, &entry->Node );
418 if( fgStructure.Window == menu->Window )
419 fgSetWindow( menu->ParentWindow );
420 fgDestroyWindow( menu->Window, GL_TRUE );
421 fgListRemove( &fgStructure.Menus, &menu->Node );
422 if( fgStructure.Menu == menu )
423 fgStructure.Menu = NULL;
429 * This function should be called on glutInit(). It will prepare the internal
430 * structure of freeglut to be used in the application. The structure will be
431 * destroyed using fgDestroyStructure() on glutMainLoop() return. In that
432 * case further use of freeglut should be preceeded with a glutInit() call.
434 void fgCreateStructure( void )
437 * We will be needing two lists: the first containing windows,
438 * and the second containing the user-defined menus.
439 * Also, no current window/menu is set, as none has been created yet.
442 fgListInit(&fgStructure.Windows);
443 fgListInit(&fgStructure.Menus);
447 * This function is automatically called on glutMainLoop() return.
448 * It should deallocate and destroy all remnants of previous
449 * glutInit()-enforced structure initialization...
451 void fgDestroyStructure( void )
456 freeglut_assert_ready;
459 * Make sure all windows and menus have been deallocated
461 while( menu = ( SFG_Menu * )fgStructure.Menus.First )
462 fgDestroyMenu( menu );
464 while( window = ( SFG_Window * )fgStructure.Windows.First )
465 fgDestroyWindow( window, GL_TRUE );
469 * Helper function to enumerate through all registered top-level windows
471 void fgEnumWindows( FGCBenumerator enumCallback, SFG_Enumerator* enumerator )
475 assert( enumCallback && enumerator );
476 freeglut_assert_ready;
479 * Check every of the top-level windows
481 for( window = ( SFG_Window * )fgStructure.Windows.First;
483 window = ( SFG_Window * )window->Node.Next )
485 enumCallback( window, enumerator );
486 if( enumerator->found )
492 * Helper function to enumerate through all a window's subwindows
493 * (single level descent)
495 void fgEnumSubWindows( SFG_Window* window, FGCBenumerator enumCallback,
496 SFG_Enumerator* enumerator )
500 assert( enumCallback && enumerator );
501 freeglut_assert_ready;
503 for( child = ( SFG_Window * )window->Children.First;
505 child = ( SFG_Window * )child->Node.Next )
507 enumCallback( child, enumerator );
508 if( enumerator->found )
514 * A static helper function to look for a window given its handle
516 static void fghcbWindowByHandle( SFG_Window *window,
517 SFG_Enumerator *enumerator )
519 if ( enumerator->found )
522 #if TARGET_HOST_UNIX_X11
523 #define WBHANDLE (Window)
524 #elif TARGET_HOST_WIN32
525 #define WBHANDLE (HWND)
529 * Check the window's handle. Hope this works. Looks ugly. That's for sure.
531 if( window->Window.Handle == WBHANDLE (enumerator->data) )
533 enumerator->found = GL_TRUE;
534 enumerator->data = window;
540 * Otherwise, check this window's children
542 fgEnumSubWindows( window, fghcbWindowByHandle, enumerator );
548 * fgWindowByHandle returns a (SFG_Window *) value pointing to the
549 * first window in the queue matching the specified window handle.
550 * The function is defined in freeglut_structure.c file.
552 SFG_Window* fgWindowByHandle
553 #if TARGET_HOST_UNIX_X11
555 #elif TARGET_HOST_WIN32
559 SFG_Enumerator enumerator;
562 * This is easy and makes use of the windows enumeration defined above
564 enumerator.found = GL_FALSE;
565 enumerator.data = (void *)hWindow;
566 fgEnumWindows( fghcbWindowByHandle, &enumerator );
568 if( enumerator.found )
569 return( SFG_Window *) enumerator.data;
574 * A static helper function to look for a window given its ID
576 static void fghcbWindowByID( SFG_Window *window, SFG_Enumerator *enumerator )
579 * Make sure we do not overwrite our precious results...
581 if ( enumerator->found )
585 * Check the window's handle. Hope this works. Looks ugly. That's for sure.
587 if( window->ID == (int) (enumerator->data) ) /* XXX int/ptr conversion! */
589 enumerator->found = GL_TRUE;
590 enumerator->data = window;
596 * Otherwise, check this window's children
598 fgEnumSubWindows( window, fghcbWindowByID, enumerator );
602 * This function is similiar to the previous one, except it is
603 * looking for a specified (sub)window identifier. The function
604 * is defined in freeglut_structure.c file.
606 SFG_Window* fgWindowByID( int windowID )
608 SFG_Enumerator enumerator;
611 * Uses a method very similiar for fgWindowByHandle...
613 enumerator.found = GL_FALSE;
614 enumerator.data = (void *) windowID; /* XXX int/pointer conversion! */
615 fgEnumWindows( fghcbWindowByID, &enumerator );
616 if( enumerator.found )
617 return( SFG_Window *) enumerator.data;
622 * Looks up a menu given its ID. This is easier that fgWindowByXXX
623 * as all menus are placed in a single doubly linked list...
625 SFG_Menu* fgMenuByID( int menuID )
627 SFG_Menu *menu = NULL;
629 freeglut_assert_ready;
632 * It's enough to check all entries in fgStructure.Menus...
634 for( menu = (SFG_Menu *)fgStructure.Menus.First;
636 menu = (SFG_Menu *)menu->Node.Next )
637 if( menu->ID == menuID )
645 void fgListInit(SFG_List *list)
651 void fgListAppend(SFG_List *list, SFG_Node *node)
655 if ( (ln = (SFG_Node *)list->Last) != NULL )
670 void fgListRemove(SFG_List *list, SFG_Node *node)
674 if( ln = (SFG_Node *)node->Next )
675 ln->Prev = node->Prev;
676 if( ln = (SFG_Node *)node->Prev )
677 ln->Next = node->Next;
678 if( ( ln = (SFG_Node *)list->First ) == node )
679 list->First = node->Next;
680 if( ( ln = (SFG_Node *)list->Last ) == node )
681 list->Last = node->Prev;
684 int fgListLength(SFG_List *list)
689 for( node =( SFG_Node * )list->First;
691 node = ( SFG_Node * )node->Next )
697 /*** END OF FILE ***/