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 fghClearCallBacks( window );
77 /* Initialize the object properties */
78 window->ID = ++fgStructure.WindowID;
79 window->State.OldHeight = window->State.OldWidth = -1;
81 fgListInit( &window->Children );
84 fgListAppend( &parent->Children, &window->Node );
85 window->Parent = parent;
88 fgListAppend( &fgStructure.Windows, &window->Node );
90 /* Set the default mouse cursor and reset the modifiers value */
91 window->State.Cursor = GLUT_CURSOR_INHERIT;
93 window->IsMenu = isMenu;
95 window->State.IgnoreKeyRepeat = GL_FALSE;
96 window->State.KeyRepeating = GL_FALSE;
99 * Open the window now. The fgOpenWindow() function is system
100 * dependant, and resides in freeglut_window.c. Uses fgState.
102 fgOpenWindow( window, title, positionUse, x, y, sizeUse, w, h, gameMode,
103 (GLboolean)(parent ? GL_TRUE : GL_FALSE) );
109 * This private function creates a menu and adds it to the menus list
111 SFG_Menu* fgCreateMenu( FGCBMenu menuCallback )
113 int x = 100, y = 100, w = 100, h = 100;
114 SFG_Window *current_window = fgStructure.CurrentWindow;
116 /* Have the menu object created */
117 SFG_Menu* menu = (SFG_Menu *)calloc( sizeof(SFG_Menu), 1 );
119 menu->ParentWindow = NULL;
121 /* Create a window for the menu to reside in. */
123 fgCreateWindow( NULL, "freeglut menu", GL_TRUE, x, y, GL_TRUE, w, h,
125 menu->Window = fgStructure.CurrentWindow;
126 glutDisplayFunc( fgDisplayMenu );
128 glutHideWindow( ); /* Hide the window for now */
129 fgSetWindow( current_window );
131 /* Initialize the object properties: */
132 menu->ID = ++fgStructure.MenuID;
133 menu->Callback = menuCallback;
134 menu->ActiveEntry = NULL;
136 fgListInit( &menu->Entries );
137 fgListAppend( &fgStructure.Menus, &menu->Node );
139 /* Newly created menus implicitly become current ones */
140 fgStructure.CurrentMenu = menu;
146 * Function to add a window to the linked list of windows to destroy.
147 * Subwindows are automatically added because they hang from the window
150 void fgAddToWindowDestroyList( SFG_Window* window )
152 SFG_WindowList *new_list_entry =
153 ( SFG_WindowList* )malloc( sizeof(SFG_WindowList ) );
154 new_list_entry->window = window;
155 fgListAppend( &fgStructure.WindowsToDestroy, &new_list_entry->node );
157 /* Check if the window is the current one... */
158 if( fgStructure.CurrentWindow == window )
159 fgStructure.CurrentWindow = NULL;
162 * Clear all window callbacks except Destroy, which will
163 * be invoked later. Right now, we are potentially carrying
164 * out a freeglut operation at the behest of a client callback,
165 * so we are reluctant to re-enter the client with the Destroy
166 * callback, right now. The others are all wiped out, however,
167 * to ensure that they are no longer called after this point.
170 FGCBDestroy destroy = (FGCBDestroy)FETCH_WCB( *window, Destroy );
171 fghClearCallBacks( window );
172 SET_WCB( *window, Destroy, destroy );
177 * Function to close down all the windows in the "WindowsToDestroy" list
179 void fgCloseWindows( )
181 while( fgStructure.WindowsToDestroy.First )
183 SFG_WindowList *window_ptr = fgStructure.WindowsToDestroy.First;
184 fgDestroyWindow( window_ptr->window );
185 fgListRemove( &fgStructure.WindowsToDestroy, &window_ptr->node );
191 * This function destroys a window and all of its subwindows. Actually,
192 * another function, defined in freeglut_window.c is called, but this is
193 * a whole different story...
195 void fgDestroyWindow( SFG_Window* window )
197 FREEGLUT_INTERNAL_ERROR_EXIT ( window, "Window destroy function called with null window",
200 while( window->Children.First )
201 fgDestroyWindow( ( SFG_Window * )window->Children.First );
204 SFG_Window *activeWindow = fgStructure.CurrentWindow;
205 INVOKE_WCB( *window, Destroy, ( ) );
206 fgSetWindow( activeWindow );
210 fgListRemove( &window->Parent->Children, &window->Node );
212 fgListRemove( &fgStructure.Windows, &window->Node );
214 if( window->ActiveMenu )
215 fgDeactivateMenu( window );
217 fghClearCallBacks( window );
218 fgCloseWindow( window );
220 if( fgStructure.CurrentWindow == window )
221 fgStructure.CurrentWindow = NULL;
225 * This is a helper static function that removes a menu (given its pointer)
226 * from any windows that can be accessed from a given parent...
228 static void fghRemoveMenuFromWindow( SFG_Window* window, SFG_Menu* menu )
230 SFG_Window *subWindow;
233 /* Check whether this is the active menu in the window */
234 if ( menu == window->ActiveMenu )
235 window->ActiveMenu = NULL ;
238 * Check if the menu is attached to the current window,
239 * if so, have it detached (by overwriting with a NULL):
241 for( i = 0; i < FREEGLUT_MAX_MENUS; i++ )
242 if( window->Menu[ i ] == menu )
243 window->Menu[ i ] = NULL;
245 /* Call this function for all of the window's children recursively: */
246 for( subWindow = (SFG_Window *)window->Children.First;
248 subWindow = (SFG_Window *)subWindow->Node.Next)
249 fghRemoveMenuFromWindow( subWindow, menu );
253 * This is a static helper function that removes menu references
254 * from another menu, given two pointers to them...
256 static void fghRemoveMenuFromMenu( SFG_Menu* from, SFG_Menu* menu )
258 SFG_MenuEntry *entry;
260 for( entry = (SFG_MenuEntry *)from->Entries.First;
262 entry = ( SFG_MenuEntry * )entry->Node.Next )
263 if( entry->SubMenu == menu )
264 entry->SubMenu = NULL;
268 * This function destroys a menu specified by the parameter. All menus
269 * and windows are updated to make sure no ill pointers hang around.
271 void fgDestroyMenu( SFG_Menu* menu )
276 FREEGLUT_INTERNAL_ERROR_EXIT ( menu, "Menu destroy function called with null menu",
279 /* First of all, have all references to this menu removed from all windows: */
280 for( window = (SFG_Window *)fgStructure.Windows.First;
282 window = (SFG_Window *)window->Node.Next )
283 fghRemoveMenuFromWindow( window, menu );
285 /* Now proceed with removing menu entries that lead to this menu */
286 for( from = ( SFG_Menu * )fgStructure.Menus.First;
288 from = ( SFG_Menu * )from->Node.Next )
289 fghRemoveMenuFromMenu( from, menu );
292 * If the programmer defined a destroy callback, call it
293 * A. Donev: But first make this the active menu
297 SFG_Menu *activeMenu=fgStructure.CurrentMenu;
298 fgStructure.CurrentMenu = menu;
300 fgStructure.CurrentMenu = activeMenu;
304 * Now we are pretty sure the menu is not used anywhere
305 * and that we can remove all of its entries
307 while( menu->Entries.First )
309 SFG_MenuEntry *entry = ( SFG_MenuEntry * ) menu->Entries.First;
311 fgListRemove( &menu->Entries, &entry->Node );
320 if( fgStructure.CurrentWindow == menu->Window )
322 fgDestroyWindow( menu->Window );
323 fgListRemove( &fgStructure.Menus, &menu->Node );
324 if( fgStructure.CurrentMenu == menu )
325 fgStructure.CurrentMenu = NULL;
331 * This function should be called on glutInit(). It will prepare the internal
332 * structure of freeglut to be used in the application. The structure will be
333 * destroyed using fgDestroyStructure() on glutMainLoop() return. In that
334 * case further use of freeglut should be preceeded with a glutInit() call.
336 void fgCreateStructure( void )
339 * We will be needing two lists: the first containing windows,
340 * and the second containing the user-defined menus.
341 * Also, no current window/menu is set, as none has been created yet.
344 fgListInit(&fgStructure.Windows);
345 fgListInit(&fgStructure.Menus);
346 fgListInit(&fgStructure.WindowsToDestroy);
348 fgStructure.CurrentWindow = NULL;
349 fgStructure.CurrentMenu = NULL;
350 fgStructure.MenuContext = NULL;
351 fgStructure.GameModeWindow = NULL;
352 fgStructure.WindowID = 0;
353 fgStructure.MenuID = 0;
357 * This function is automatically called on glutMainLoop() return.
358 * It should deallocate and destroy all remnants of previous
359 * glutInit()-enforced structure initialization...
361 void fgDestroyStructure( void )
363 /* Clean up the WindowsToDestroy list. */
366 /* Make sure all windows and menus have been deallocated */
367 while( fgStructure.Menus.First )
368 fgDestroyMenu( ( SFG_Menu * )fgStructure.Menus.First );
370 while( fgStructure.Windows.First )
371 fgDestroyWindow( ( SFG_Window * )fgStructure.Windows.First );
375 * Helper function to enumerate through all registered top-level windows
377 void fgEnumWindows( FGCBenumerator enumCallback, SFG_Enumerator* enumerator )
381 FREEGLUT_INTERNAL_ERROR_EXIT ( enumCallback && enumerator,
382 "Enumerator or callback missing from window enumerator call",
385 /* Check every of the top-level windows */
386 for( window = ( SFG_Window * )fgStructure.Windows.First;
388 window = ( SFG_Window * )window->Node.Next )
390 enumCallback( window, enumerator );
391 if( enumerator->found )
397 * Helper function to enumerate through all a window's subwindows
398 * (single level descent)
400 void fgEnumSubWindows( SFG_Window* window, FGCBenumerator enumCallback,
401 SFG_Enumerator* enumerator )
405 FREEGLUT_INTERNAL_ERROR_EXIT ( enumCallback && enumerator,
406 "Enumerator or callback missing from subwindow enumerator call",
407 "fgEnumSubWindows" );
408 FREEGLUT_INTERNAL_ERROR_EXIT_IF_NOT_INITIALISED ( "Window Enumeration" );
410 for( child = ( SFG_Window * )window->Children.First;
412 child = ( SFG_Window * )child->Node.Next )
414 enumCallback( child, enumerator );
415 if( enumerator->found )
421 * A static helper function to look for a window given its handle
423 static void fghcbWindowByHandle( SFG_Window *window,
424 SFG_Enumerator *enumerator )
426 if ( enumerator->found )
429 /* Check the window's handle. Hope this works. Looks ugly. That's for sure. */
430 if( window->Window.Handle == (SFG_WindowHandleType) (enumerator->data) )
432 enumerator->found = GL_TRUE;
433 enumerator->data = window;
438 /* Otherwise, check this window's children */
439 fgEnumSubWindows( window, fghcbWindowByHandle, enumerator );
443 * fgWindowByHandle returns a (SFG_Window *) value pointing to the
444 * first window in the queue matching the specified window handle.
445 * The function is defined in freeglut_structure.c file.
447 SFG_Window* fgWindowByHandle ( SFG_WindowHandleType hWindow )
449 SFG_Enumerator enumerator;
451 /* This is easy and makes use of the windows enumeration defined above */
452 enumerator.found = GL_FALSE;
453 enumerator.data = (void *)hWindow;
454 fgEnumWindows( fghcbWindowByHandle, &enumerator );
456 if( enumerator.found )
457 return( SFG_Window *) enumerator.data;
462 * A static helper function to look for a window given its ID
464 static void fghcbWindowByID( SFG_Window *window, SFG_Enumerator *enumerator )
466 /* Make sure we do not overwrite our precious results... */
467 if( enumerator->found )
470 /* Check the window's handle. Hope this works. Looks ugly. That's for sure. */
471 if( window->ID == *( int *)(enumerator->data) )
473 enumerator->found = GL_TRUE;
474 enumerator->data = window;
479 /* Otherwise, check this window's children */
480 fgEnumSubWindows( window, fghcbWindowByID, enumerator );
484 * This function is similiar to the previous one, except it is
485 * looking for a specified (sub)window identifier. The function
486 * is defined in freeglut_structure.c file.
488 SFG_Window* fgWindowByID( int windowID )
490 SFG_Enumerator enumerator;
492 /* Uses a method very similiar for fgWindowByHandle... */
493 enumerator.found = GL_FALSE;
494 enumerator.data = ( void * )&windowID;
495 fgEnumWindows( fghcbWindowByID, &enumerator );
496 if( enumerator.found )
497 return ( SFG_Window * )enumerator.data;
502 * Looks up a menu given its ID. This is easier that fgWindowByXXX
503 * as all menus are placed in one doubly linked list...
505 SFG_Menu* fgMenuByID( int menuID )
507 SFG_Menu *menu = NULL;
509 /* It's enough to check all entries in fgStructure.Menus... */
510 for( menu = (SFG_Menu *)fgStructure.Menus.First;
512 menu = (SFG_Menu *)menu->Node.Next )
513 if( menu->ID == menuID )
521 void fgListInit(SFG_List *list)
527 void fgListAppend(SFG_List *list, SFG_Node *node)
531 SFG_Node *ln = (SFG_Node *) list->Last;
545 void fgListRemove(SFG_List *list, SFG_Node *node)
548 ( ( SFG_Node * )node->Next )->Prev = node->Prev;
550 ( ( SFG_Node * )node->Prev )->Next = node->Next;
551 if( ( ( SFG_Node * )list->First ) == node )
552 list->First = node->Next;
553 if( ( ( SFG_Node * )list->Last ) == node )
554 list->Last = node->Prev;
557 int fgListLength(SFG_List *list)
562 for( node =( SFG_Node * )list->First;
564 node = ( SFG_Node * )node->Next )
571 void fgListInsert(SFG_List *list, SFG_Node *next, SFG_Node *node)
575 if( (node->Next = next) )
586 if( (node->Prev = prev) )
592 /*** END OF FILE ***/