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 static void fghClearCallBacks( 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 )
76 /* Have the window object created */
77 SFG_Window *window = (SFG_Window *)calloc( sizeof(SFG_Window), 1 );
80 fghClearCallBacks( 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.Initialised )
87 glutInit( &fakeArgc, NULL );
89 /* Initialize the object properties */
90 window->ID = ++fgStructure.WindowID;
91 window->State.OldHeight = window->State.OldWidth = -1;
93 fgListInit( &window->Children );
96 fgListAppend( &parent->Children, &window->Node );
97 window->Parent = parent;
100 fgListAppend( &fgStructure.Windows, &window->Node );
102 /* Set the default mouse cursor and reset the modifiers value */
103 window->State.Cursor = GLUT_CURSOR_INHERIT;
105 window->IsMenu = isMenu;
107 window->State.IgnoreKeyRepeat = GL_FALSE;
108 window->State.KeyRepeating = GL_FALSE;
111 * Open the window now. The fgOpenWindow() function is system
112 * dependant, and resides in freeglut_window.c. Uses fgState.
114 fgOpenWindow( window, title, x, y, w, h, gameMode,
115 parent ? GL_TRUE : GL_FALSE );
121 * This private function creates a menu and adds it to the menus list
123 SFG_Menu* fgCreateMenu( FGCBMenu menuCallback )
125 int x = 100, y = 100, w = 100, h = 100;
126 SFG_Window *current_window = fgStructure.Window;
128 /* 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.Initialised )
137 glutInit( &fakeArgc, NULL );
139 menu->ParentWindow = fgStructure.Window;
141 /* Create a window for the menu to reside in. */
143 fgCreateWindow( NULL, "freeglut menu", x, y, w, h, GL_FALSE, GL_TRUE );
144 menu->Window = fgStructure.Window;
145 glutDisplayFunc( fgDisplayMenu );
147 glutHideWindow( ); /* Hide the window for now */
148 fgSetWindow( current_window );
150 /* Initialize the object properties: */
151 menu->ID = ++fgStructure.MenuID;
152 menu->Callback = menuCallback;
153 menu->ActiveEntry = NULL;
155 fgListInit( &menu->Entries );
156 fgListAppend( &fgStructure.Menus, &menu->Node );
158 /* Newly created menus implicitly become current ones */
159 fgStructure.Menu = menu;
165 * Function to add a window to the linked list of windows to destroy.
166 * Subwindows are automatically added because they hang from the window
169 void fgAddToWindowDestroyList( SFG_Window* window )
171 SFG_WindowList *new_list_entry =
172 ( SFG_WindowList* )malloc( sizeof(SFG_WindowList ) );
173 new_list_entry->window = window;
174 fgListAppend( &fgStructure.WindowsToDestroy, &new_list_entry->node );
176 /* Check if the window is the current one... */
177 if( fgStructure.Window == window )
178 fgStructure.Window = NULL;
181 * Clear all window callbacks except Destroy, which will
182 * be invoked later. Right now, we are potentially carrying
183 * out a freeglut operation at the behest of a client callback,
184 * so we are reluctant to re-enter the client with the Destroy
185 * callback, right now. The others are all wiped out, however,
186 * to ensure that they are no longer called after this point.
189 FGCBDestroy destroy = FETCH_WCB( *window, Destroy );
190 fghClearCallBacks( window );
191 SET_WCB( *window, Destroy, destroy );
196 * Function to close down all the windows in the "WindowsToDestroy" list
198 void fgCloseWindows( )
200 while( fgStructure.WindowsToDestroy.First )
202 SFG_WindowList *window_ptr = fgStructure.WindowsToDestroy.First;
203 fgDestroyWindow( window_ptr->window );
204 fgListRemove( &fgStructure.WindowsToDestroy, &window_ptr->node );
210 * This function destroys a window and all of its subwindows. Actually,
211 * another function, defined in freeglut_window.c is called, but this is
212 * a whole different story...
214 void fgDestroyWindow( SFG_Window* window )
219 freeglut_assert_ready;
221 while( window->Children.First )
222 fgDestroyWindow( ( SFG_Window * )window->Children.First );
225 SFG_Window *activeWindow = fgStructure.Window;
226 INVOKE_WCB( *window, Destroy, ( ) );
227 fgSetWindow( activeWindow );
231 fgListRemove( &window->Parent->Children, &window->Node );
233 fgListRemove( &fgStructure.Windows, &window->Node );
235 if( window->ActiveMenu )
236 fgDeactivateMenu( window );
238 for( menu_index = 0; menu_index < 3; menu_index ++ )
239 if( window->Menu[ menu_index ] )
240 window->Menu[ menu_index ]->ParentWindow = NULL;
242 fghClearCallBacks( window );
243 fgCloseWindow( window );
245 if( fgStructure.Window == window )
246 fgStructure.Window = NULL;
250 * This is a helper static function that removes a menu (given its pointer)
251 * from any windows that can be accessed from a given parent...
253 static void fghRemoveMenuFromWindow( SFG_Window* window, SFG_Menu* menu )
255 SFG_Window *subWindow;
259 * Check if the menu is attached to the current window,
260 * if so, have it detached (by overwriting with a NULL):
262 for( i = 0; i < 3; i++ )
263 if( window->Menu[ i ] == menu )
264 window->Menu[ i ] = NULL;
266 /* Call this function for all of the window's children recursively: */
267 for( subWindow = (SFG_Window *)window->Children.First;
269 subWindow = (SFG_Window *)subWindow->Node.Next)
270 fghRemoveMenuFromWindow( subWindow, menu );
274 * This is a static helper function that removes menu references
275 * from another menu, given two pointers to them...
277 static void fghRemoveMenuFromMenu( SFG_Menu* from, SFG_Menu* menu )
279 SFG_MenuEntry *entry;
281 for( entry = (SFG_MenuEntry *)from->Entries.First;
283 entry = ( SFG_MenuEntry * )entry->Node.Next )
284 if( entry->SubMenu == menu )
285 entry->SubMenu = NULL;
289 * This function destroys a menu specified by the parameter. All menus
290 * and windows are updated to make sure no ill pointers hang around.
292 void fgDestroyMenu( SFG_Menu* menu )
298 freeglut_assert_ready;
300 /* First of all, have all references to this menu removed from all windows: */
301 for( window = (SFG_Window *)fgStructure.Windows.First;
303 window = (SFG_Window *)window->Node.Next )
304 fghRemoveMenuFromWindow( window, menu );
306 /* Now proceed with removing menu entries that lead to this menu */
307 for( from = ( SFG_Menu * )fgStructure.Menus.First;
309 from = ( SFG_Menu * )from->Node.Next )
310 fghRemoveMenuFromMenu( from, menu );
313 * If the programmer defined a destroy callback, call it
314 * A. Donev: But first make this the active menu
318 SFG_Menu *activeMenu=fgStructure.Menu;
319 fgStructure.Menu = menu;
321 fgStructure.Menu = activeMenu;
325 * Now we are pretty sure the menu is not used anywhere
326 * and that we can remove all of its entries
328 while( menu->Entries.First )
330 SFG_MenuEntry *entry = ( SFG_MenuEntry * ) menu->Entries.First;
332 fgListRemove( &menu->Entries, &entry->Node );
341 if( fgStructure.Window == menu->Window )
342 fgSetWindow( menu->ParentWindow );
343 fgDestroyWindow( menu->Window );
344 fgListRemove( &fgStructure.Menus, &menu->Node );
345 if( fgStructure.Menu == menu )
346 fgStructure.Menu = NULL;
352 * This function should be called on glutInit(). It will prepare the internal
353 * structure of freeglut to be used in the application. The structure will be
354 * destroyed using fgDestroyStructure() on glutMainLoop() return. In that
355 * case further use of freeglut should be preceeded with a glutInit() call.
357 void fgCreateStructure( void )
360 * We will be needing two lists: the first containing windows,
361 * and the second containing the user-defined menus.
362 * Also, no current window/menu is set, as none has been created yet.
365 fgListInit(&fgStructure.Windows);
366 fgListInit(&fgStructure.Menus);
367 fgListInit(&fgStructure.WindowsToDestroy);
371 * This function is automatically called on glutMainLoop() return.
372 * It should deallocate and destroy all remnants of previous
373 * glutInit()-enforced structure initialization...
375 void fgDestroyStructure( void )
377 freeglut_assert_ready;
379 /* Clean up the WindowsToDestroy list. */
382 /* Make sure all windows and menus have been deallocated */
383 while( fgStructure.Menus.First )
384 fgDestroyMenu( ( SFG_Menu * )fgStructure.Menus.First );
386 while( fgStructure.Windows.First )
387 fgDestroyWindow( ( SFG_Window * )fgStructure.Windows.First );
391 * Helper function to enumerate through all registered top-level windows
393 void fgEnumWindows( FGCBenumerator enumCallback, SFG_Enumerator* enumerator )
397 assert( enumCallback && enumerator );
398 freeglut_assert_ready;
400 /* Check every of the top-level windows */
401 for( window = ( SFG_Window * )fgStructure.Windows.First;
403 window = ( SFG_Window * )window->Node.Next )
405 enumCallback( window, enumerator );
406 if( enumerator->found )
412 * Helper function to enumerate through all a window's subwindows
413 * (single level descent)
415 void fgEnumSubWindows( SFG_Window* window, FGCBenumerator enumCallback,
416 SFG_Enumerator* enumerator )
420 assert( enumCallback && enumerator );
421 freeglut_assert_ready;
423 for( child = ( SFG_Window * )window->Children.First;
425 child = ( SFG_Window * )child->Node.Next )
427 enumCallback( child, enumerator );
428 if( enumerator->found )
434 * A static helper function to look for a window given its handle
436 static void fghcbWindowByHandle( SFG_Window *window,
437 SFG_Enumerator *enumerator )
439 if ( enumerator->found )
442 /* Check the window's handle. Hope this works. Looks ugly. That's for sure. */
443 if( window->Window.Handle == (SFG_WindowHandleType) (enumerator->data) )
445 enumerator->found = GL_TRUE;
446 enumerator->data = window;
451 /* Otherwise, check this window's children */
452 fgEnumSubWindows( window, fghcbWindowByHandle, enumerator );
456 * fgWindowByHandle returns a (SFG_Window *) value pointing to the
457 * first window in the queue matching the specified window handle.
458 * The function is defined in freeglut_structure.c file.
460 SFG_Window* fgWindowByHandle ( SFG_WindowHandleType hWindow )
462 SFG_Enumerator enumerator;
464 /* This is easy and makes use of the windows enumeration defined above */
465 enumerator.found = GL_FALSE;
466 enumerator.data = (void *)hWindow;
467 fgEnumWindows( fghcbWindowByHandle, &enumerator );
469 if( enumerator.found )
470 return( SFG_Window *) enumerator.data;
475 * A static helper function to look for a window given its ID
477 static void fghcbWindowByID( SFG_Window *window, SFG_Enumerator *enumerator )
479 /* Make sure we do not overwrite our precious results... */
480 if( enumerator->found )
483 /* Check the window's handle. Hope this works. Looks ugly. That's for sure. */
484 if( window->ID == *( int *)(enumerator->data) )
486 enumerator->found = GL_TRUE;
487 enumerator->data = window;
492 /* Otherwise, check this window's children */
493 fgEnumSubWindows( window, fghcbWindowByID, enumerator );
497 * This function is similiar to the previous one, except it is
498 * looking for a specified (sub)window identifier. The function
499 * is defined in freeglut_structure.c file.
501 SFG_Window* fgWindowByID( int windowID )
503 SFG_Enumerator enumerator;
505 /* Uses a method very similiar for fgWindowByHandle... */
506 enumerator.found = GL_FALSE;
507 enumerator.data = ( void * )&windowID;
508 fgEnumWindows( fghcbWindowByID, &enumerator );
509 if( enumerator.found )
510 return ( SFG_Window * )enumerator.data;
515 * Looks up a menu given its ID. This is easier that fgWindowByXXX
516 * as all menus are placed in one doubly linked list...
518 SFG_Menu* fgMenuByID( int menuID )
520 SFG_Menu *menu = NULL;
522 freeglut_assert_ready;
524 /* It's enough to check all entries in fgStructure.Menus... */
525 for( menu = (SFG_Menu *)fgStructure.Menus.First;
527 menu = (SFG_Menu *)menu->Node.Next )
528 if( menu->ID == menuID )
536 void fgListInit(SFG_List *list)
542 void fgListAppend(SFG_List *list, SFG_Node *node)
546 SFG_Node *ln = (SFG_Node *) list->Last;
560 void fgListRemove(SFG_List *list, SFG_Node *node)
563 ( ( SFG_Node * )node->Next )->Prev = node->Prev;
565 ( ( SFG_Node * )node->Prev )->Next = node->Next;
566 if( ( ( SFG_Node * )list->First ) == node )
567 list->First = node->Next;
568 if( ( ( SFG_Node * )list->Last ) == node )
569 list->Last = node->Prev;
572 int fgListLength(SFG_List *list)
577 for( node =( SFG_Node * )list->First;
579 node = ( SFG_Node * )node->Next )
586 void fgListInsert(SFG_List *list, SFG_Node *next, SFG_Node *node)
590 if( (node->Next = next) )
601 if( (node->Prev = prev) )
607 /*** END OF FILE ***/