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.
28 #include <GL/freeglut.h>
29 #include "freeglut_internal.h"
31 /* -- GLOBAL EXPORTS ------------------------------------------------------- */
34 * The SFG_Structure container holds information about windows and menus
35 * created between glutInit() and glutMainLoop() return.
38 SFG_Structure fgStructure = { { NULL, NULL }, /* The list of windows */
39 { NULL, NULL }, /* The list of menus */
40 { NULL, NULL }, /* Windows to Destroy list */
41 NULL, /* The current window */
42 NULL, /* The current menu */
43 NULL, /* The menu OpenGL context */
44 NULL, /* The game mode window */
45 0, /* The current new window ID */
46 0 }; /* The current new menu ID */
49 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */
51 static void fghClearCallBacks( SFG_Window *window )
56 for( i = 0; i < TOTAL_CALLBACKS; ++i )
57 window->CallBacks[ i ] = NULL;
62 * This private function creates, opens and adds to the hierarchy
63 * a freeglut window complete with OpenGL context and stuff...
65 * If parent is set to NULL, the window created will be a topmost one.
67 SFG_Window* fgCreateWindow( SFG_Window* parent, const char* title,
68 GLboolean positionUse, int x, int y,
69 GLboolean sizeUse, int w, int h,
70 GLboolean gameMode, GLboolean isMenu )
72 /* Have the window object created */
73 SFG_Window *window = (SFG_Window *)calloc( sizeof(SFG_Window), 1 );
75 #if TARGET_HOST_UNIX_X11
76 window->Window.FBConfig = NULL;
78 fghClearCallBacks( window );
80 /* Initialize the object properties */
81 window->ID = ++fgStructure.WindowID;
82 window->State.OldHeight = window->State.OldWidth = -1;
84 fgListInit( &window->Children );
87 fgListAppend( &parent->Children, &window->Node );
88 window->Parent = parent;
91 fgListAppend( &fgStructure.Windows, &window->Node );
93 /* Set the default mouse cursor and reset the modifiers value */
94 window->State.Cursor = GLUT_CURSOR_INHERIT;
96 window->IsMenu = isMenu;
98 window->State.IgnoreKeyRepeat = GL_FALSE;
99 window->State.KeyRepeating = GL_FALSE;
102 * Open the window now. The fgOpenWindow() function is system
103 * dependant, and resides in freeglut_window.c. Uses fgState.
105 fgOpenWindow( window, title, positionUse, x, y, sizeUse, w, h, gameMode,
106 (GLboolean)(parent ? GL_TRUE : GL_FALSE) );
112 * This private function creates a menu and adds it to the menus list
114 SFG_Menu* fgCreateMenu( FGCBMenu menuCallback )
116 int x = 100, y = 100, w = 100, h = 100;
117 SFG_Window *current_window = fgStructure.CurrentWindow;
119 /* Have the menu object created */
120 SFG_Menu* menu = (SFG_Menu *)calloc( sizeof(SFG_Menu), 1 );
122 menu->ParentWindow = NULL;
124 /* Create a window for the menu to reside in. */
126 fgCreateWindow( NULL, "freeglut menu", GL_TRUE, x, y, GL_TRUE, w, h,
128 menu->Window = fgStructure.CurrentWindow;
129 glutDisplayFunc( fgDisplayMenu );
131 glutHideWindow( ); /* Hide the window for now */
132 fgSetWindow( current_window );
134 /* Initialize the object properties: */
135 menu->ID = ++fgStructure.MenuID;
136 menu->Callback = menuCallback;
137 menu->ActiveEntry = NULL;
139 fgListInit( &menu->Entries );
140 fgListAppend( &fgStructure.Menus, &menu->Node );
142 /* Newly created menus implicitly become current ones */
143 fgStructure.CurrentMenu = menu;
149 * Function to add a window to the linked list of windows to destroy.
150 * Subwindows are automatically added because they hang from the window
153 void fgAddToWindowDestroyList( SFG_Window* window )
155 SFG_WindowList *new_list_entry =
156 ( SFG_WindowList* )malloc( sizeof(SFG_WindowList ) );
157 new_list_entry->window = window;
158 fgListAppend( &fgStructure.WindowsToDestroy, &new_list_entry->node );
160 /* Check if the window is the current one... */
161 if( fgStructure.CurrentWindow == window )
162 fgStructure.CurrentWindow = NULL;
165 * Clear all window callbacks except Destroy, which will
166 * be invoked later. Right now, we are potentially carrying
167 * out a freeglut operation at the behest of a client callback,
168 * so we are reluctant to re-enter the client with the Destroy
169 * callback, right now. The others are all wiped out, however,
170 * to ensure that they are no longer called after this point.
173 FGCBDestroy destroy = (FGCBDestroy)FETCH_WCB( *window, Destroy );
174 fghClearCallBacks( window );
175 SET_WCB( *window, Destroy, destroy );
180 * Function to close down all the windows in the "WindowsToDestroy" list
182 void fgCloseWindows( )
184 while( fgStructure.WindowsToDestroy.First )
186 SFG_WindowList *window_ptr = fgStructure.WindowsToDestroy.First;
187 fgDestroyWindow( window_ptr->window );
188 fgListRemove( &fgStructure.WindowsToDestroy, &window_ptr->node );
194 * This function destroys a window and all of its subwindows. Actually,
195 * another function, defined in freeglut_window.c is called, but this is
196 * a whole different story...
198 void fgDestroyWindow( SFG_Window* window )
200 FREEGLUT_INTERNAL_ERROR_EXIT ( window, "Window destroy function called with null window",
203 while( window->Children.First )
204 fgDestroyWindow( ( SFG_Window * )window->Children.First );
207 SFG_Window *activeWindow = fgStructure.CurrentWindow;
208 INVOKE_WCB( *window, Destroy, ( ) );
209 fgSetWindow( activeWindow );
213 fgListRemove( &window->Parent->Children, &window->Node );
215 fgListRemove( &fgStructure.Windows, &window->Node );
217 if( window->ActiveMenu )
218 fgDeactivateMenu( window );
220 fghClearCallBacks( window );
221 fgCloseWindow( window );
223 if( fgStructure.CurrentWindow == window )
224 fgStructure.CurrentWindow = NULL;
228 * This is a helper static function that removes a menu (given its pointer)
229 * from any windows that can be accessed from a given parent...
231 static void fghRemoveMenuFromWindow( SFG_Window* window, SFG_Menu* menu )
233 SFG_Window *subWindow;
236 /* Check whether this is the active menu in the window */
237 if ( menu == window->ActiveMenu )
238 window->ActiveMenu = NULL ;
241 * Check if the menu is attached to the current window,
242 * if so, have it detached (by overwriting with a NULL):
244 for( i = 0; i < FREEGLUT_MAX_MENUS; i++ )
245 if( window->Menu[ i ] == menu )
246 window->Menu[ i ] = NULL;
248 /* Call this function for all of the window's children recursively: */
249 for( subWindow = (SFG_Window *)window->Children.First;
251 subWindow = (SFG_Window *)subWindow->Node.Next)
252 fghRemoveMenuFromWindow( subWindow, menu );
256 * This is a static helper function that removes menu references
257 * from another menu, given two pointers to them...
259 static void fghRemoveMenuFromMenu( SFG_Menu* from, SFG_Menu* menu )
261 SFG_MenuEntry *entry;
263 for( entry = (SFG_MenuEntry *)from->Entries.First;
265 entry = ( SFG_MenuEntry * )entry->Node.Next )
266 if( entry->SubMenu == menu )
267 entry->SubMenu = NULL;
271 * This function destroys a menu specified by the parameter. All menus
272 * and windows are updated to make sure no ill pointers hang around.
274 void fgDestroyMenu( SFG_Menu* menu )
279 FREEGLUT_INTERNAL_ERROR_EXIT ( menu, "Menu destroy function called with null menu",
282 /* First of all, have all references to this menu removed from all windows: */
283 for( window = (SFG_Window *)fgStructure.Windows.First;
285 window = (SFG_Window *)window->Node.Next )
286 fghRemoveMenuFromWindow( window, menu );
288 /* Now proceed with removing menu entries that lead to this menu */
289 for( from = ( SFG_Menu * )fgStructure.Menus.First;
291 from = ( SFG_Menu * )from->Node.Next )
292 fghRemoveMenuFromMenu( from, menu );
295 * If the programmer defined a destroy callback, call it
296 * A. Donev: But first make this the active menu
300 SFG_Menu *activeMenu=fgStructure.CurrentMenu;
301 fgStructure.CurrentMenu = menu;
303 fgStructure.CurrentMenu = activeMenu;
307 * Now we are pretty sure the menu is not used anywhere
308 * and that we can remove all of its entries
310 while( menu->Entries.First )
312 SFG_MenuEntry *entry = ( SFG_MenuEntry * ) menu->Entries.First;
314 fgListRemove( &menu->Entries, &entry->Node );
323 if( fgStructure.CurrentWindow == menu->Window )
325 fgDestroyWindow( menu->Window );
326 fgListRemove( &fgStructure.Menus, &menu->Node );
327 if( fgStructure.CurrentMenu == menu )
328 fgStructure.CurrentMenu = NULL;
334 * This function should be called on glutInit(). It will prepare the internal
335 * structure of freeglut to be used in the application. The structure will be
336 * destroyed using fgDestroyStructure() on glutMainLoop() return. In that
337 * case further use of freeglut should be preceeded with a glutInit() call.
339 void fgCreateStructure( void )
342 * We will be needing two lists: the first containing windows,
343 * and the second containing the user-defined menus.
344 * Also, no current window/menu is set, as none has been created yet.
347 fgListInit(&fgStructure.Windows);
348 fgListInit(&fgStructure.Menus);
349 fgListInit(&fgStructure.WindowsToDestroy);
351 fgStructure.CurrentWindow = NULL;
352 fgStructure.CurrentMenu = NULL;
353 fgStructure.MenuContext = NULL;
354 fgStructure.GameModeWindow = NULL;
355 fgStructure.WindowID = 0;
356 fgStructure.MenuID = 0;
360 * This function is automatically called on glutMainLoop() return.
361 * It should deallocate and destroy all remnants of previous
362 * glutInit()-enforced structure initialization...
364 void fgDestroyStructure( void )
366 /* Clean up the WindowsToDestroy list. */
369 /* Make sure all windows and menus have been deallocated */
370 while( fgStructure.Menus.First )
371 fgDestroyMenu( ( SFG_Menu * )fgStructure.Menus.First );
373 while( fgStructure.Windows.First )
374 fgDestroyWindow( ( SFG_Window * )fgStructure.Windows.First );
378 * Helper function to enumerate through all registered top-level windows
380 void fgEnumWindows( FGCBenumerator enumCallback, SFG_Enumerator* enumerator )
384 FREEGLUT_INTERNAL_ERROR_EXIT ( enumCallback && enumerator,
385 "Enumerator or callback missing from window enumerator call",
388 /* Check every of the top-level windows */
389 for( window = ( SFG_Window * )fgStructure.Windows.First;
391 window = ( SFG_Window * )window->Node.Next )
393 enumCallback( window, enumerator );
394 if( enumerator->found )
400 * Helper function to enumerate through all a window's subwindows
401 * (single level descent)
403 void fgEnumSubWindows( SFG_Window* window, FGCBenumerator enumCallback,
404 SFG_Enumerator* enumerator )
408 FREEGLUT_INTERNAL_ERROR_EXIT ( enumCallback && enumerator,
409 "Enumerator or callback missing from subwindow enumerator call",
410 "fgEnumSubWindows" );
411 FREEGLUT_INTERNAL_ERROR_EXIT_IF_NOT_INITIALISED ( "Window Enumeration" );
413 for( child = ( SFG_Window * )window->Children.First;
415 child = ( SFG_Window * )child->Node.Next )
417 enumCallback( child, enumerator );
418 if( enumerator->found )
424 * A static helper function to look for a window given its handle
426 static void fghcbWindowByHandle( SFG_Window *window,
427 SFG_Enumerator *enumerator )
429 if ( enumerator->found )
432 /* Check the window's handle. Hope this works. Looks ugly. That's for sure. */
433 if( window->Window.Handle == (SFG_WindowHandleType) (enumerator->data) )
435 enumerator->found = GL_TRUE;
436 enumerator->data = window;
441 /* Otherwise, check this window's children */
442 fgEnumSubWindows( window, fghcbWindowByHandle, enumerator );
446 * fgWindowByHandle returns a (SFG_Window *) value pointing to the
447 * first window in the queue matching the specified window handle.
448 * The function is defined in freeglut_structure.c file.
450 SFG_Window* fgWindowByHandle ( SFG_WindowHandleType hWindow )
452 SFG_Enumerator enumerator;
454 /* This is easy and makes use of the windows enumeration defined above */
455 enumerator.found = GL_FALSE;
456 enumerator.data = (void *)hWindow;
457 fgEnumWindows( fghcbWindowByHandle, &enumerator );
459 if( enumerator.found )
460 return( SFG_Window *) enumerator.data;
465 * A static helper function to look for a window given its ID
467 static void fghcbWindowByID( SFG_Window *window, SFG_Enumerator *enumerator )
469 /* Make sure we do not overwrite our precious results... */
470 if( enumerator->found )
473 /* Check the window's handle. Hope this works. Looks ugly. That's for sure. */
474 if( window->ID == *( int *)(enumerator->data) )
476 enumerator->found = GL_TRUE;
477 enumerator->data = window;
482 /* Otherwise, check this window's children */
483 fgEnumSubWindows( window, fghcbWindowByID, enumerator );
487 * This function is similiar to the previous one, except it is
488 * looking for a specified (sub)window identifier. The function
489 * is defined in freeglut_structure.c file.
491 SFG_Window* fgWindowByID( int windowID )
493 SFG_Enumerator enumerator;
495 /* Uses a method very similiar for fgWindowByHandle... */
496 enumerator.found = GL_FALSE;
497 enumerator.data = ( void * )&windowID;
498 fgEnumWindows( fghcbWindowByID, &enumerator );
499 if( enumerator.found )
500 return ( SFG_Window * )enumerator.data;
505 * Looks up a menu given its ID. This is easier that fgWindowByXXX
506 * as all menus are placed in one doubly linked list...
508 SFG_Menu* fgMenuByID( int menuID )
510 SFG_Menu *menu = NULL;
512 /* It's enough to check all entries in fgStructure.Menus... */
513 for( menu = (SFG_Menu *)fgStructure.Menus.First;
515 menu = (SFG_Menu *)menu->Node.Next )
516 if( menu->ID == menuID )
524 void fgListInit(SFG_List *list)
530 void fgListAppend(SFG_List *list, SFG_Node *node)
534 SFG_Node *ln = (SFG_Node *) list->Last;
548 void fgListRemove(SFG_List *list, SFG_Node *node)
551 ( ( SFG_Node * )node->Next )->Prev = node->Prev;
553 ( ( SFG_Node * )node->Prev )->Next = node->Next;
554 if( ( ( SFG_Node * )list->First ) == node )
555 list->First = node->Next;
556 if( ( ( SFG_Node * )list->Last ) == node )
557 list->Last = node->Prev;
560 int fgListLength(SFG_List *list)
565 for( node =( SFG_Node * )list->First;
567 node = ( SFG_Node * )node->Next )
574 void fgListInsert(SFG_List *list, SFG_Node *next, SFG_Node *node)
578 if( (node->Next = next) )
589 if( (node->Prev = prev) )
595 /*** END OF FILE ***/