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 int x, int y, int w, int h,
69 GLboolean gameMode, GLboolean isMenu )
71 /* Have the window object created */
72 SFG_Window *window = (SFG_Window *)calloc( sizeof(SFG_Window), 1 );
74 fghClearCallBacks( window );
76 /* Initialize the object properties */
77 window->ID = ++fgStructure.WindowID;
78 window->State.OldHeight = window->State.OldWidth = -1;
80 fgListInit( &window->Children );
83 fgListAppend( &parent->Children, &window->Node );
84 window->Parent = parent;
87 fgListAppend( &fgStructure.Windows, &window->Node );
89 /* Set the default mouse cursor and reset the modifiers value */
90 window->State.Cursor = GLUT_CURSOR_INHERIT;
92 window->IsMenu = isMenu;
94 window->State.IgnoreKeyRepeat = GL_FALSE;
95 window->State.KeyRepeating = GL_FALSE;
98 * Open the window now. The fgOpenWindow() function is system
99 * dependant, and resides in freeglut_window.c. Uses fgState.
101 fgOpenWindow( window, title, x, y, w, h, gameMode,
102 (GLboolean)(parent ? GL_TRUE : GL_FALSE) );
108 * This private function creates a menu and adds it to the menus list
110 SFG_Menu* fgCreateMenu( FGCBMenu menuCallback )
112 int x = 100, y = 100, w = 100, h = 100;
113 SFG_Window *current_window = fgStructure.CurrentWindow;
115 /* Have the menu object created */
116 SFG_Menu* menu = (SFG_Menu *)calloc( sizeof(SFG_Menu), 1 );
118 menu->ParentWindow = NULL;
120 /* Create a window for the menu to reside in. */
122 fgCreateWindow( NULL, "freeglut menu", x, y, w, h, GL_FALSE, GL_TRUE );
123 menu->Window = fgStructure.CurrentWindow;
124 glutDisplayFunc( fgDisplayMenu );
126 glutHideWindow( ); /* Hide the window for now */
127 fgSetWindow( current_window );
129 /* Initialize the object properties: */
130 menu->ID = ++fgStructure.MenuID;
131 menu->Callback = menuCallback;
132 menu->ActiveEntry = NULL;
134 fgListInit( &menu->Entries );
135 fgListAppend( &fgStructure.Menus, &menu->Node );
137 /* Newly created menus implicitly become current ones */
138 fgStructure.CurrentMenu = menu;
144 * Function to add a window to the linked list of windows to destroy.
145 * Subwindows are automatically added because they hang from the window
148 void fgAddToWindowDestroyList( SFG_Window* window )
150 SFG_WindowList *new_list_entry =
151 ( SFG_WindowList* )malloc( sizeof(SFG_WindowList ) );
152 new_list_entry->window = window;
153 fgListAppend( &fgStructure.WindowsToDestroy, &new_list_entry->node );
155 /* Check if the window is the current one... */
156 if( fgStructure.CurrentWindow == window )
157 fgStructure.CurrentWindow = NULL;
160 * Clear all window callbacks except Destroy, which will
161 * be invoked later. Right now, we are potentially carrying
162 * out a freeglut operation at the behest of a client callback,
163 * so we are reluctant to re-enter the client with the Destroy
164 * callback, right now. The others are all wiped out, however,
165 * to ensure that they are no longer called after this point.
168 FGCBDestroy destroy = FETCH_WCB( *window, Destroy );
169 fghClearCallBacks( window );
170 SET_WCB( *window, Destroy, destroy );
175 * Function to close down all the windows in the "WindowsToDestroy" list
177 void fgCloseWindows( )
179 while( fgStructure.WindowsToDestroy.First )
181 SFG_WindowList *window_ptr = fgStructure.WindowsToDestroy.First;
182 fgDestroyWindow( window_ptr->window );
183 fgListRemove( &fgStructure.WindowsToDestroy, &window_ptr->node );
189 * This function destroys a window and all of its subwindows. Actually,
190 * another function, defined in freeglut_window.c is called, but this is
191 * a whole different story...
193 void fgDestroyWindow( SFG_Window* window )
195 FREEGLUT_INTERNAL_ERROR_EXIT ( window, "Window destroy function called with null window",
198 while( window->Children.First )
199 fgDestroyWindow( ( SFG_Window * )window->Children.First );
202 SFG_Window *activeWindow = fgStructure.CurrentWindow;
203 INVOKE_WCB( *window, Destroy, ( ) );
204 fgSetWindow( activeWindow );
208 fgListRemove( &window->Parent->Children, &window->Node );
210 fgListRemove( &fgStructure.Windows, &window->Node );
212 if( window->ActiveMenu )
213 fgDeactivateMenu( window );
215 fghClearCallBacks( window );
216 fgCloseWindow( window );
218 if( fgStructure.CurrentWindow == window )
219 fgStructure.CurrentWindow = NULL;
223 * This is a helper static function that removes a menu (given its pointer)
224 * from any windows that can be accessed from a given parent...
226 static void fghRemoveMenuFromWindow( SFG_Window* window, SFG_Menu* menu )
228 SFG_Window *subWindow;
231 /* Check whether this is the active menu in the window */
232 if ( menu == window->ActiveMenu )
233 window->ActiveMenu = NULL ;
236 * Check if the menu is attached to the current window,
237 * if so, have it detached (by overwriting with a NULL):
239 for( i = 0; i < 3; i++ )
240 if( window->Menu[ i ] == menu )
241 window->Menu[ i ] = NULL;
243 /* Call this function for all of the window's children recursively: */
244 for( subWindow = (SFG_Window *)window->Children.First;
246 subWindow = (SFG_Window *)subWindow->Node.Next)
247 fghRemoveMenuFromWindow( subWindow, menu );
251 * This is a static helper function that removes menu references
252 * from another menu, given two pointers to them...
254 static void fghRemoveMenuFromMenu( SFG_Menu* from, SFG_Menu* menu )
256 SFG_MenuEntry *entry;
258 for( entry = (SFG_MenuEntry *)from->Entries.First;
260 entry = ( SFG_MenuEntry * )entry->Node.Next )
261 if( entry->SubMenu == menu )
262 entry->SubMenu = NULL;
266 * This function destroys a menu specified by the parameter. All menus
267 * and windows are updated to make sure no ill pointers hang around.
269 void fgDestroyMenu( SFG_Menu* menu )
274 FREEGLUT_INTERNAL_ERROR_EXIT ( menu, "Menu destroy function called with null menu",
277 /* First of all, have all references to this menu removed from all windows: */
278 for( window = (SFG_Window *)fgStructure.Windows.First;
280 window = (SFG_Window *)window->Node.Next )
281 fghRemoveMenuFromWindow( window, menu );
283 /* Now proceed with removing menu entries that lead to this menu */
284 for( from = ( SFG_Menu * )fgStructure.Menus.First;
286 from = ( SFG_Menu * )from->Node.Next )
287 fghRemoveMenuFromMenu( from, menu );
290 * If the programmer defined a destroy callback, call it
291 * A. Donev: But first make this the active menu
295 SFG_Menu *activeMenu=fgStructure.CurrentMenu;
296 fgStructure.CurrentMenu = menu;
298 fgStructure.CurrentMenu = activeMenu;
302 * Now we are pretty sure the menu is not used anywhere
303 * and that we can remove all of its entries
305 while( menu->Entries.First )
307 SFG_MenuEntry *entry = ( SFG_MenuEntry * ) menu->Entries.First;
309 fgListRemove( &menu->Entries, &entry->Node );
318 if( fgStructure.CurrentWindow == menu->Window )
320 fgDestroyWindow( menu->Window );
321 fgListRemove( &fgStructure.Menus, &menu->Node );
322 if( fgStructure.CurrentMenu == menu )
323 fgStructure.CurrentMenu = NULL;
329 * This function should be called on glutInit(). It will prepare the internal
330 * structure of freeglut to be used in the application. The structure will be
331 * destroyed using fgDestroyStructure() on glutMainLoop() return. In that
332 * case further use of freeglut should be preceeded with a glutInit() call.
334 void fgCreateStructure( void )
337 * We will be needing two lists: the first containing windows,
338 * and the second containing the user-defined menus.
339 * Also, no current window/menu is set, as none has been created yet.
342 fgListInit(&fgStructure.Windows);
343 fgListInit(&fgStructure.Menus);
344 fgListInit(&fgStructure.WindowsToDestroy);
348 * This function is automatically called on glutMainLoop() return.
349 * It should deallocate and destroy all remnants of previous
350 * glutInit()-enforced structure initialization...
352 void fgDestroyStructure( void )
354 /* Clean up the WindowsToDestroy list. */
357 /* Make sure all windows and menus have been deallocated */
358 while( fgStructure.Menus.First )
359 fgDestroyMenu( ( SFG_Menu * )fgStructure.Menus.First );
361 while( fgStructure.Windows.First )
362 fgDestroyWindow( ( SFG_Window * )fgStructure.Windows.First );
366 * Helper function to enumerate through all registered top-level windows
368 void fgEnumWindows( FGCBenumerator enumCallback, SFG_Enumerator* enumerator )
372 FREEGLUT_INTERNAL_ERROR_EXIT ( enumCallback && enumerator,
373 "Enumerator or callback missing from window enumerator call",
376 /* Check every of the top-level windows */
377 for( window = ( SFG_Window * )fgStructure.Windows.First;
379 window = ( SFG_Window * )window->Node.Next )
381 enumCallback( window, enumerator );
382 if( enumerator->found )
388 * Helper function to enumerate through all a window's subwindows
389 * (single level descent)
391 void fgEnumSubWindows( SFG_Window* window, FGCBenumerator enumCallback,
392 SFG_Enumerator* enumerator )
396 FREEGLUT_INTERNAL_ERROR_EXIT ( enumCallback && enumerator,
397 "Enumerator or callback missing from subwindow enumerator call",
398 "fgEnumSubWindows" );
399 FREEGLUT_INTERNAL_ERROR_EXIT_IF_NOT_INITIALISED ( "Window Enumeration" );
401 for( child = ( SFG_Window * )window->Children.First;
403 child = ( SFG_Window * )child->Node.Next )
405 enumCallback( child, enumerator );
406 if( enumerator->found )
412 * A static helper function to look for a window given its handle
414 static void fghcbWindowByHandle( SFG_Window *window,
415 SFG_Enumerator *enumerator )
417 if ( enumerator->found )
420 /* Check the window's handle. Hope this works. Looks ugly. That's for sure. */
421 if( window->Window.Handle == (SFG_WindowHandleType) (enumerator->data) )
423 enumerator->found = GL_TRUE;
424 enumerator->data = window;
429 /* Otherwise, check this window's children */
430 fgEnumSubWindows( window, fghcbWindowByHandle, enumerator );
434 * fgWindowByHandle returns a (SFG_Window *) value pointing to the
435 * first window in the queue matching the specified window handle.
436 * The function is defined in freeglut_structure.c file.
438 SFG_Window* fgWindowByHandle ( SFG_WindowHandleType hWindow )
440 SFG_Enumerator enumerator;
442 /* This is easy and makes use of the windows enumeration defined above */
443 enumerator.found = GL_FALSE;
444 enumerator.data = (void *)hWindow;
445 fgEnumWindows( fghcbWindowByHandle, &enumerator );
447 if( enumerator.found )
448 return( SFG_Window *) enumerator.data;
453 * A static helper function to look for a window given its ID
455 static void fghcbWindowByID( SFG_Window *window, SFG_Enumerator *enumerator )
457 /* Make sure we do not overwrite our precious results... */
458 if( enumerator->found )
461 /* Check the window's handle. Hope this works. Looks ugly. That's for sure. */
462 if( window->ID == *( int *)(enumerator->data) )
464 enumerator->found = GL_TRUE;
465 enumerator->data = window;
470 /* Otherwise, check this window's children */
471 fgEnumSubWindows( window, fghcbWindowByID, enumerator );
475 * This function is similiar to the previous one, except it is
476 * looking for a specified (sub)window identifier. The function
477 * is defined in freeglut_structure.c file.
479 SFG_Window* fgWindowByID( int windowID )
481 SFG_Enumerator enumerator;
483 /* Uses a method very similiar for fgWindowByHandle... */
484 enumerator.found = GL_FALSE;
485 enumerator.data = ( void * )&windowID;
486 fgEnumWindows( fghcbWindowByID, &enumerator );
487 if( enumerator.found )
488 return ( SFG_Window * )enumerator.data;
493 * Looks up a menu given its ID. This is easier that fgWindowByXXX
494 * as all menus are placed in one doubly linked list...
496 SFG_Menu* fgMenuByID( int menuID )
498 SFG_Menu *menu = NULL;
500 /* It's enough to check all entries in fgStructure.Menus... */
501 for( menu = (SFG_Menu *)fgStructure.Menus.First;
503 menu = (SFG_Menu *)menu->Node.Next )
504 if( menu->ID == menuID )
512 void fgListInit(SFG_List *list)
518 void fgListAppend(SFG_List *list, SFG_Node *node)
522 SFG_Node *ln = (SFG_Node *) list->Last;
536 void fgListRemove(SFG_List *list, SFG_Node *node)
539 ( ( SFG_Node * )node->Next )->Prev = node->Prev;
541 ( ( SFG_Node * )node->Prev )->Next = node->Next;
542 if( ( ( SFG_Node * )list->First ) == node )
543 list->First = node->Next;
544 if( ( ( SFG_Node * )list->Last ) == node )
545 list->Last = node->Prev;
548 int fgListLength(SFG_List *list)
553 for( node =( SFG_Node * )list->First;
555 node = ( SFG_Node * )node->Next )
562 void fgListInsert(SFG_List *list, SFG_Node *next, SFG_Node *node)
566 if( (node->Next = next) )
577 if( (node->Prev = prev) )
583 /*** END OF FILE ***/