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;
113 window->State.IgnoreKeyRepeat = GL_FALSE;
114 window->State.KeyRepeating = GL_FALSE;
117 * Open the window now. The fgOpenWindow() function is system
118 * dependant, and resides in freeglut_window.c. Uses fgState.
120 fgOpenWindow( window, title, x, y, w, h, gameMode,
121 parent ? GL_TRUE : GL_FALSE );
127 * This private function creates a menu and adds it to the menus list
129 SFG_Menu* fgCreateMenu( FGCBMenu menuCallback )
131 int x = 100, y = 100, w = 100, h = 100;
132 SFG_Window *current_window = fgStructure.Window;
135 * Have the menu object created
137 SFG_Menu* menu = (SFG_Menu *)calloc( sizeof(SFG_Menu), 1 );
141 * If the freeglut internals haven't been initialized yet,
142 * do it now. Hack's idea courtesy of Chris Purnell...
144 if( !fgState.Initialised )
145 glutInit( &fakeArgc, NULL );
147 menu->ParentWindow = fgStructure.Window;
150 * Create a window for the menu to reside in.
153 fgCreateWindow( NULL, "freeglut menu", x, y, w, h, GL_FALSE, GL_TRUE );
154 menu->Window = fgStructure.Window;
155 glutDisplayFunc( fgDisplayMenu );
157 glutHideWindow( ); /* Hide the window for now */
158 fgSetWindow( current_window );
161 * Initialize the object properties:
163 menu->ID = ++fgStructure.MenuID;
164 menu->Callback = menuCallback;
165 menu->ActiveEntry = NULL;
167 fgListInit( &menu->Entries );
168 fgListAppend( &fgStructure.Menus, &menu->Node );
171 * Newly created menus implicitly become current ones
173 fgStructure.Menu = menu;
179 * Function to add a window to the linked list of windows to destroy.
180 * Subwindows are automatically added because they hang from the window
183 void fgAddToWindowDestroyList( SFG_Window* window )
185 SFG_WindowList *new_list_entry =
186 ( SFG_WindowList* )malloc( sizeof(SFG_WindowList ) );
187 new_list_entry->window = window;
188 fgListAppend( &fgStructure.WindowsToDestroy, &new_list_entry->node );
191 * Check if the window is the current one...
193 if( fgStructure.Window == window )
194 fgStructure.Window = NULL;
197 * Clear all window callbacks except Destroy, which will
198 * be invoked later. Right now, we are potentially carrying
199 * out a freeglut operation at the behest of a client callback,
200 * so we are reluctant to re-enter the client with the Destroy
201 * callback, right now. The others are all wiped out, however,
202 * to ensure that they are no longer called after this point.
205 FGCBDestroy destroy = FETCH_WCB( *window, Destroy );
206 fgClearCallBacks( window );
207 SET_WCB( *window, Destroy, destroy );
212 * Function to close down all the windows in the "WindowsToDestroy" list
214 void fgCloseWindows( )
216 while( fgStructure.WindowsToDestroy.First )
218 SFG_WindowList *window_ptr = fgStructure.WindowsToDestroy.First;
219 fgDestroyWindow( window_ptr->window );
220 fgListRemove( &fgStructure.WindowsToDestroy, &window_ptr->node );
226 * This function destroys a window and all of its subwindows. Actually,
227 * another function, defined in freeglut_window.c is called, but this is
228 * a whole different story...
230 void fgDestroyWindow( SFG_Window* window )
235 freeglut_assert_ready;
237 while( window->Children.First )
238 fgDestroyWindow( ( SFG_Window * )window->Children.First );
241 SFG_Window *activeWindow = fgStructure.Window;
242 INVOKE_WCB( *window, Destroy, ( ) );
243 fgSetWindow( activeWindow );
247 fgListRemove( &window->Parent->Children, &window->Node );
249 fgListRemove( &fgStructure.Windows, &window->Node );
251 if( window->ActiveMenu )
252 fgDeactivateMenu( window );
254 for( menu_index = 0; menu_index < 3; menu_index ++ )
255 if( window->Menu[ menu_index ] )
256 window->Menu[ menu_index ]->ParentWindow = NULL;
258 fgClearCallBacks( window );
259 fgCloseWindow( window );
261 if( fgStructure.Window == window )
262 fgStructure.Window = NULL;
266 * This is a helper static function that removes a menu (given its pointer)
267 * from any windows that can be accessed from a given parent...
269 static void fghRemoveMenuFromWindow( SFG_Window* window, SFG_Menu* menu )
271 SFG_Window *subWindow;
275 * Check if the menu is attached to the current window,
276 * if so, have it detached (by overwriting with a NULL):
278 for( i = 0; i < 3; i++ )
279 if( window->Menu[ i ] == menu )
280 window->Menu[ i ] = NULL;
283 * Call this function for all of the window's children recursively:
285 for( subWindow = (SFG_Window *)window->Children.First;
287 subWindow = (SFG_Window *)subWindow->Node.Next)
288 fghRemoveMenuFromWindow( subWindow, menu );
292 * This is a static helper function that removes menu references
293 * from another menu, given two pointers to them...
295 static void fghRemoveMenuFromMenu( SFG_Menu* from, SFG_Menu* menu )
297 SFG_MenuEntry *entry;
299 for( entry = (SFG_MenuEntry *)from->Entries.First;
301 entry = ( SFG_MenuEntry * )entry->Node.Next )
302 if( entry->SubMenu == menu )
303 entry->SubMenu = NULL;
307 * This function destroys a menu specified by the parameter. All menus
308 * and windows are updated to make sure no ill pointers hang around.
310 void fgDestroyMenu( SFG_Menu* menu )
316 freeglut_assert_ready;
319 * First of all, have all references to this menu removed from all windows:
321 for( window = (SFG_Window *)fgStructure.Windows.First;
323 window = (SFG_Window *)window->Node.Next )
324 fghRemoveMenuFromWindow( window, menu );
327 * Now proceed with removing menu entries that lead to this menu
329 for( from = ( SFG_Menu * )fgStructure.Menus.First;
331 from = ( SFG_Menu * )from->Node.Next )
332 fghRemoveMenuFromMenu( from, menu );
335 * If the programmer defined a destroy callback, call it
336 * A. Donev: But first make this the active menu
340 SFG_Menu *activeMenu=fgStructure.Menu;
341 fgStructure.Menu = menu;
343 fgStructure.Menu = activeMenu;
347 * Now we are pretty sure the menu is not used anywhere
348 * and that we can remove all of its entries
350 while( menu->Entries.First )
352 SFG_MenuEntry *entry = ( SFG_MenuEntry * ) menu->Entries.First;
354 fgListRemove( &menu->Entries, &entry->Node );
363 if( fgStructure.Window == menu->Window )
364 fgSetWindow( menu->ParentWindow );
365 fgDestroyWindow( menu->Window );
366 fgListRemove( &fgStructure.Menus, &menu->Node );
367 if( fgStructure.Menu == menu )
368 fgStructure.Menu = NULL;
374 * This function should be called on glutInit(). It will prepare the internal
375 * structure of freeglut to be used in the application. The structure will be
376 * destroyed using fgDestroyStructure() on glutMainLoop() return. In that
377 * case further use of freeglut should be preceeded with a glutInit() call.
379 void fgCreateStructure( void )
382 * We will be needing two lists: the first containing windows,
383 * and the second containing the user-defined menus.
384 * Also, no current window/menu is set, as none has been created yet.
387 fgListInit(&fgStructure.Windows);
388 fgListInit(&fgStructure.Menus);
389 fgListInit(&fgStructure.WindowsToDestroy);
393 * This function is automatically called on glutMainLoop() return.
394 * It should deallocate and destroy all remnants of previous
395 * glutInit()-enforced structure initialization...
397 void fgDestroyStructure( void )
399 freeglut_assert_ready;
402 * Clean up the WindowsToDestroy list.
407 * Make sure all windows and menus have been deallocated
409 while( fgStructure.Menus.First )
410 fgDestroyMenu( ( SFG_Menu * )fgStructure.Menus.First );
412 while( fgStructure.Windows.First )
413 fgDestroyWindow( ( SFG_Window * )fgStructure.Windows.First );
417 * Helper function to enumerate through all registered top-level windows
419 void fgEnumWindows( FGCBenumerator enumCallback, SFG_Enumerator* enumerator )
423 assert( enumCallback && enumerator );
424 freeglut_assert_ready;
427 * Check every of the top-level windows
429 for( window = ( SFG_Window * )fgStructure.Windows.First;
431 window = ( SFG_Window * )window->Node.Next )
433 enumCallback( window, enumerator );
434 if( enumerator->found )
440 * Helper function to enumerate through all a window's subwindows
441 * (single level descent)
443 void fgEnumSubWindows( SFG_Window* window, FGCBenumerator enumCallback,
444 SFG_Enumerator* enumerator )
448 assert( enumCallback && enumerator );
449 freeglut_assert_ready;
451 for( child = ( SFG_Window * )window->Children.First;
453 child = ( SFG_Window * )child->Node.Next )
455 enumCallback( child, enumerator );
456 if( enumerator->found )
462 * A static helper function to look for a window given its handle
464 static void fghcbWindowByHandle( SFG_Window *window,
465 SFG_Enumerator *enumerator )
467 if ( enumerator->found )
471 * Check the window's handle. Hope this works. Looks ugly. That's for sure.
473 if( window->Window.Handle == (SFG_WindowHandleType) (enumerator->data) )
475 enumerator->found = GL_TRUE;
476 enumerator->data = window;
482 * Otherwise, check this window's children
484 fgEnumSubWindows( window, fghcbWindowByHandle, enumerator );
488 * fgWindowByHandle returns a (SFG_Window *) value pointing to the
489 * first window in the queue matching the specified window handle.
490 * The function is defined in freeglut_structure.c file.
492 SFG_Window* fgWindowByHandle ( SFG_WindowHandleType hWindow )
494 SFG_Enumerator enumerator;
497 * This is easy and makes use of the windows enumeration defined above
499 enumerator.found = GL_FALSE;
500 enumerator.data = (void *)hWindow;
501 fgEnumWindows( fghcbWindowByHandle, &enumerator );
503 if( enumerator.found )
504 return( SFG_Window *) enumerator.data;
509 * A static helper function to look for a window given its ID
511 static void fghcbWindowByID( SFG_Window *window, SFG_Enumerator *enumerator )
514 * Make sure we do not overwrite our precious results...
516 if( enumerator->found )
520 * Check the window's handle. Hope this works. Looks ugly. That's for sure.
522 if( window->ID == *( int *)(enumerator->data) )
524 enumerator->found = GL_TRUE;
525 enumerator->data = window;
531 * Otherwise, check this window's children
533 fgEnumSubWindows( window, fghcbWindowByID, enumerator );
537 * This function is similiar to the previous one, except it is
538 * looking for a specified (sub)window identifier. The function
539 * is defined in freeglut_structure.c file.
541 SFG_Window* fgWindowByID( int windowID )
543 SFG_Enumerator enumerator;
546 * Uses a method very similiar for fgWindowByHandle...
548 enumerator.found = GL_FALSE;
549 enumerator.data = ( void * )&windowID;
550 fgEnumWindows( fghcbWindowByID, &enumerator );
551 if( enumerator.found )
552 return ( SFG_Window * )enumerator.data;
557 * Looks up a menu given its ID. This is easier that fgWindowByXXX
558 * as all menus are placed in a single doubly linked list...
560 SFG_Menu* fgMenuByID( int menuID )
562 SFG_Menu *menu = NULL;
564 freeglut_assert_ready;
567 * It's enough to check all entries in fgStructure.Menus...
569 for( menu = (SFG_Menu *)fgStructure.Menus.First;
571 menu = (SFG_Menu *)menu->Node.Next )
572 if( menu->ID == menuID )
580 void fgListInit(SFG_List *list)
586 void fgListAppend(SFG_List *list, SFG_Node *node)
590 SFG_Node *ln = (SFG_Node *) list->Last;
604 void fgListRemove(SFG_List *list, SFG_Node *node)
607 ( ( SFG_Node * )node->Next )->Prev = node->Prev;
609 ( ( SFG_Node * )node->Prev )->Next = node->Next;
610 if( ( ( SFG_Node * )list->First ) == node )
611 list->First = node->Next;
612 if( ( ( SFG_Node * )list->Last ) == node )
613 list->Last = node->Prev;
616 int fgListLength(SFG_List *list)
621 for( node =( SFG_Node * )list->First;
623 node = ( SFG_Node * )node->Next )
630 void fgListInsert(SFG_List *list, SFG_Node *next, SFG_Node *node)
634 if( (node->Next = next) )
645 if( (node->Prev = prev) )
651 /*** END OF FILE ***/