2 * freeglut_structure.c
\r
4 * Windows and menus need tree structure
\r
6 * Copyright (c) 1999-2000 Pawel W. Olszta. All Rights Reserved.
\r
7 * Written by Pawel W. Olszta, <olszta@sourceforge.net>
\r
8 * Creation date: Sat Dec 18 1999
\r
10 * Permission is hereby granted, free of charge, to any person obtaining a
\r
11 * copy of this software and associated documentation files (the "Software"),
\r
12 * to deal in the Software without restriction, including without limitation
\r
13 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
\r
14 * and/or sell copies of the Software, and to permit persons to whom the
\r
15 * Software is furnished to do so, subject to the following conditions:
\r
17 * The above copyright notice and this permission notice shall be included
\r
18 * in all copies or substantial portions of the Software.
\r
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
\r
21 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
\r
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
\r
23 * PAWEL W. OLSZTA BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
24 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
25 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
28 #include <GL/freeglut.h>
\r
29 #include "freeglut_internal.h"
\r
31 /* -- GLOBAL EXPORTS ------------------------------------------------------- */
\r
34 * The SFG_Structure container holds information about windows and menus
\r
35 * created between glutInit() and glutMainLoop() return.
\r
38 SFG_Structure fgStructure = { { NULL, NULL }, /* The list of windows */
\r
39 { NULL, NULL }, /* The list of menus */
\r
40 { NULL, NULL }, /* Windows to Destroy list */
\r
41 NULL, /* The current window */
\r
42 NULL, /* The current menu */
\r
43 NULL, /* The menu OpenGL context */
\r
44 NULL, /* The game mode window */
\r
45 0, /* The current new window ID */
\r
46 0 }; /* The current new menu ID */
\r
49 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */
\r
51 extern void fgPlatformCreateWindow ( SFG_Window *window );
\r
53 static void fghClearCallBacks( SFG_Window *window )
\r
58 for( i = 0; i < TOTAL_CALLBACKS; ++i )
\r
59 window->CallBacks[ i ] = NULL;
\r
64 * This private function creates, opens and adds to the hierarchy
\r
65 * a freeglut window complete with OpenGL context and stuff...
\r
67 * If parent is set to NULL, the window created will be a topmost one.
\r
69 SFG_Window* fgCreateWindow( SFG_Window* parent, const char* title,
\r
70 GLboolean positionUse, int x, int y,
\r
71 GLboolean sizeUse, int w, int h,
\r
72 GLboolean gameMode, GLboolean isMenu )
\r
74 /* Have the window object created */
\r
75 SFG_Window *window = (SFG_Window *)calloc( sizeof(SFG_Window), 1 );
\r
77 fgPlatformCreateWindow ( window );
\r
79 fghClearCallBacks( window );
\r
81 /* Initialize the object properties */
\r
82 window->ID = ++fgStructure.WindowID;
\r
84 fgListInit( &window->Children );
\r
87 fgListAppend( &parent->Children, &window->Node );
\r
88 window->Parent = parent;
\r
91 fgListAppend( &fgStructure.Windows, &window->Node );
\r
93 /* Set the default mouse cursor and reset the modifiers value */
\r
94 window->State.Cursor = GLUT_CURSOR_INHERIT;
\r
96 window->IsMenu = isMenu;
\r
98 window->State.IgnoreKeyRepeat = GL_FALSE;
\r
99 window->State.KeyRepeating = GL_FALSE;
\r
100 window->State.IsFullscreen = GL_FALSE;
\r
103 * Open the window now. The fgOpenWindow() function is system
\r
104 * dependant, and resides in freeglut_window.c. Uses fgState.
\r
106 fgOpenWindow( window, title, positionUse, x, y, sizeUse, w, h, gameMode,
\r
107 (GLboolean)(parent ? GL_TRUE : GL_FALSE) );
\r
113 * This private function creates a menu and adds it to the menus list
\r
115 SFG_Menu* fgCreateMenu( FGCBMenu menuCallback )
\r
117 int x = 100, y = 100, w = 1, h = 1;
\r
118 SFG_Window *current_window = fgStructure.CurrentWindow;
\r
120 /* Have the menu object created */
\r
121 SFG_Menu* menu = (SFG_Menu *)calloc( sizeof(SFG_Menu), 1 );
\r
123 menu->ParentWindow = NULL;
\r
125 /* Create a window for the menu to reside in. */
\r
127 fgCreateWindow( NULL, "freeglut menu", GL_TRUE, x, y, GL_TRUE, w, h,
\r
128 GL_FALSE, GL_TRUE );
\r
129 menu->Window = fgStructure.CurrentWindow;
\r
130 glutDisplayFunc( fgDisplayMenu );
\r
132 glutHideWindow( ); /* Hide the window for now */
\r
133 fgSetWindow( current_window );
\r
135 /* Initialize the object properties: */
\r
136 menu->ID = ++fgStructure.MenuID;
\r
137 menu->Callback = menuCallback;
\r
138 menu->ActiveEntry = NULL;
\r
140 fgListInit( &menu->Entries );
\r
141 fgListAppend( &fgStructure.Menus, &menu->Node );
\r
143 /* Newly created menus implicitly become current ones */
\r
144 fgStructure.CurrentMenu = menu;
\r
150 * Function to add a window to the linked list of windows to destroy.
\r
151 * Subwindows are automatically added because they hang from the window
\r
154 void fgAddToWindowDestroyList( SFG_Window* window )
\r
156 SFG_WindowList *new_list_entry =
\r
157 ( SFG_WindowList* )malloc( sizeof(SFG_WindowList ) );
\r
158 new_list_entry->window = window;
\r
159 fgListAppend( &fgStructure.WindowsToDestroy, &new_list_entry->node );
\r
161 /* Check if the window is the current one... */
\r
162 if( fgStructure.CurrentWindow == window )
\r
163 fgStructure.CurrentWindow = NULL;
\r
166 * Clear all window callbacks except Destroy, which will
\r
167 * be invoked later. Right now, we are potentially carrying
\r
168 * out a freeglut operation at the behest of a client callback,
\r
169 * so we are reluctant to re-enter the client with the Destroy
\r
170 * callback, right now. The others are all wiped out, however,
\r
171 * to ensure that they are no longer called after this point.
\r
174 FGCBDestroy destroy = (FGCBDestroy)FETCH_WCB( *window, Destroy );
\r
175 fghClearCallBacks( window );
\r
176 SET_WCB( *window, Destroy, destroy );
\r
181 * Function to close down all the windows in the "WindowsToDestroy" list
\r
183 void fgCloseWindows( )
\r
185 while( fgStructure.WindowsToDestroy.First )
\r
187 SFG_WindowList *window_ptr = fgStructure.WindowsToDestroy.First;
\r
188 fgDestroyWindow( window_ptr->window );
\r
189 fgListRemove( &fgStructure.WindowsToDestroy, &window_ptr->node );
\r
190 free( window_ptr );
\r
195 * This function destroys a window and all of its subwindows. Actually,
\r
196 * another function, defined in freeglut_window.c is called, but this is
\r
197 * a whole different story...
\r
199 void fgDestroyWindow( SFG_Window* window )
\r
201 FREEGLUT_INTERNAL_ERROR_EXIT ( window, "Window destroy function called with null window",
\r
202 "fgDestroyWindow" );
\r
204 while( window->Children.First )
\r
205 fgDestroyWindow( ( SFG_Window * )window->Children.First );
\r
208 SFG_Window *activeWindow = fgStructure.CurrentWindow;
\r
209 INVOKE_WCB( *window, Destroy, ( ) );
\r
210 fgSetWindow( activeWindow );
\r
213 if( window->Parent )
\r
214 fgListRemove( &window->Parent->Children, &window->Node );
\r
216 fgListRemove( &fgStructure.Windows, &window->Node );
\r
218 if( window->ActiveMenu )
\r
219 fgDeactivateMenu( window );
\r
221 fghClearCallBacks( window );
\r
222 fgCloseWindow( window );
\r
224 if( fgStructure.CurrentWindow == window )
\r
225 fgStructure.CurrentWindow = NULL;
\r
229 * This is a helper static function that removes a menu (given its pointer)
\r
230 * from any windows that can be accessed from a given parent...
\r
232 static void fghRemoveMenuFromWindow( SFG_Window* window, SFG_Menu* menu )
\r
234 SFG_Window *subWindow;
\r
237 /* Check whether this is the active menu in the window */
\r
238 if ( menu == window->ActiveMenu )
\r
239 window->ActiveMenu = NULL ;
\r
242 * Check if the menu is attached to the current window,
\r
243 * if so, have it detached (by overwriting with a NULL):
\r
245 for( i = 0; i < FREEGLUT_MAX_MENUS; i++ )
\r
246 if( window->Menu[ i ] == menu )
\r
247 window->Menu[ i ] = NULL;
\r
249 /* Call this function for all of the window's children recursively: */
\r
250 for( subWindow = (SFG_Window *)window->Children.First;
\r
252 subWindow = (SFG_Window *)subWindow->Node.Next)
\r
253 fghRemoveMenuFromWindow( subWindow, menu );
\r
257 * This is a static helper function that removes menu references
\r
258 * from another menu, given two pointers to them...
\r
260 static void fghRemoveMenuFromMenu( SFG_Menu* from, SFG_Menu* menu )
\r
262 SFG_MenuEntry *entry;
\r
264 for( entry = (SFG_MenuEntry *)from->Entries.First;
\r
266 entry = ( SFG_MenuEntry * )entry->Node.Next )
\r
267 if( entry->SubMenu == menu )
\r
268 entry->SubMenu = NULL;
\r
272 * This function destroys a menu specified by the parameter. All menus
\r
273 * and windows are updated to make sure no ill pointers hang around.
\r
275 void fgDestroyMenu( SFG_Menu* menu )
\r
277 SFG_Window *window;
\r
280 FREEGLUT_INTERNAL_ERROR_EXIT ( menu, "Menu destroy function called with null menu",
\r
283 /* First of all, have all references to this menu removed from all windows: */
\r
284 for( window = (SFG_Window *)fgStructure.Windows.First;
\r
286 window = (SFG_Window *)window->Node.Next )
\r
287 fghRemoveMenuFromWindow( window, menu );
\r
289 /* Now proceed with removing menu entries that lead to this menu */
\r
290 for( from = ( SFG_Menu * )fgStructure.Menus.First;
\r
292 from = ( SFG_Menu * )from->Node.Next )
\r
293 fghRemoveMenuFromMenu( from, menu );
\r
296 * If the programmer defined a destroy callback, call it
\r
297 * A. Donev: But first make this the active menu
\r
299 if( menu->Destroy )
\r
301 SFG_Menu *activeMenu=fgStructure.CurrentMenu;
\r
302 fgStructure.CurrentMenu = menu;
\r
304 fgStructure.CurrentMenu = activeMenu;
\r
308 * Now we are pretty sure the menu is not used anywhere
\r
309 * and that we can remove all of its entries
\r
311 while( menu->Entries.First )
\r
313 SFG_MenuEntry *entry = ( SFG_MenuEntry * ) menu->Entries.First;
\r
315 fgListRemove( &menu->Entries, &entry->Node );
\r
318 free( entry->Text );
\r
319 entry->Text = NULL;
\r
324 if( fgStructure.CurrentWindow == menu->Window )
\r
325 fgSetWindow( NULL );
\r
326 fgDestroyWindow( menu->Window );
\r
327 fgListRemove( &fgStructure.Menus, &menu->Node );
\r
328 if( fgStructure.CurrentMenu == menu )
\r
329 fgStructure.CurrentMenu = NULL;
\r
335 * This function should be called on glutInit(). It will prepare the internal
\r
336 * structure of freeglut to be used in the application. The structure will be
\r
337 * destroyed using fgDestroyStructure() on glutMainLoop() return. In that
\r
338 * case further use of freeglut should be preceeded with a glutInit() call.
\r
340 void fgCreateStructure( void )
\r
343 * We will be needing two lists: the first containing windows,
\r
344 * and the second containing the user-defined menus.
\r
345 * Also, no current window/menu is set, as none has been created yet.
\r
348 fgListInit(&fgStructure.Windows);
\r
349 fgListInit(&fgStructure.Menus);
\r
350 fgListInit(&fgStructure.WindowsToDestroy);
\r
352 fgStructure.CurrentWindow = NULL;
\r
353 fgStructure.CurrentMenu = NULL;
\r
354 fgStructure.MenuContext = NULL;
\r
355 fgStructure.GameModeWindow = NULL;
\r
356 fgStructure.WindowID = 0;
\r
357 fgStructure.MenuID = 0;
\r
361 * This function is automatically called on glutMainLoop() return.
\r
362 * It should deallocate and destroy all remnants of previous
\r
363 * glutInit()-enforced structure initialization...
\r
365 void fgDestroyStructure( void )
\r
367 /* Clean up the WindowsToDestroy list. */
\r
370 /* Make sure all windows and menus have been deallocated */
\r
371 while( fgStructure.Menus.First )
\r
372 fgDestroyMenu( ( SFG_Menu * )fgStructure.Menus.First );
\r
374 while( fgStructure.Windows.First )
\r
375 fgDestroyWindow( ( SFG_Window * )fgStructure.Windows.First );
\r
379 * Helper function to enumerate through all registered top-level windows
\r
381 void fgEnumWindows( FGCBenumerator enumCallback, SFG_Enumerator* enumerator )
\r
383 SFG_Window *window;
\r
385 FREEGLUT_INTERNAL_ERROR_EXIT ( enumCallback && enumerator,
\r
386 "Enumerator or callback missing from window enumerator call",
\r
389 /* Check every of the top-level windows */
\r
390 for( window = ( SFG_Window * )fgStructure.Windows.First;
\r
392 window = ( SFG_Window * )window->Node.Next )
\r
394 enumCallback( window, enumerator );
\r
395 if( enumerator->found )
\r
401 * Helper function to enumerate through all a window's subwindows
\r
402 * (single level descent)
\r
404 void fgEnumSubWindows( SFG_Window* window, FGCBenumerator enumCallback,
\r
405 SFG_Enumerator* enumerator )
\r
409 FREEGLUT_INTERNAL_ERROR_EXIT ( enumCallback && enumerator,
\r
410 "Enumerator or callback missing from subwindow enumerator call",
\r
411 "fgEnumSubWindows" );
\r
412 FREEGLUT_INTERNAL_ERROR_EXIT_IF_NOT_INITIALISED ( "Window Enumeration" );
\r
414 for( child = ( SFG_Window * )window->Children.First;
\r
416 child = ( SFG_Window * )child->Node.Next )
\r
418 enumCallback( child, enumerator );
\r
419 if( enumerator->found )
\r
425 * A static helper function to look for a window given its handle
\r
427 static void fghcbWindowByHandle( SFG_Window *window,
\r
428 SFG_Enumerator *enumerator )
\r
430 if ( enumerator->found )
\r
433 /* Check the window's handle. Hope this works. Looks ugly. That's for sure. */
\r
434 if( window->Window.Handle == (SFG_WindowHandleType) (enumerator->data) )
\r
436 enumerator->found = GL_TRUE;
\r
437 enumerator->data = window;
\r
442 /* Otherwise, check this window's children */
\r
443 fgEnumSubWindows( window, fghcbWindowByHandle, enumerator );
\r
447 * fgWindowByHandle returns a (SFG_Window *) value pointing to the
\r
448 * first window in the queue matching the specified window handle.
\r
449 * The function is defined in freeglut_structure.c file.
\r
451 SFG_Window* fgWindowByHandle ( SFG_WindowHandleType hWindow )
\r
453 SFG_Enumerator enumerator;
\r
455 /* This is easy and makes use of the windows enumeration defined above */
\r
456 enumerator.found = GL_FALSE;
\r
457 enumerator.data = (void *)hWindow;
\r
458 fgEnumWindows( fghcbWindowByHandle, &enumerator );
\r
460 if( enumerator.found )
\r
461 return( SFG_Window *) enumerator.data;
\r
466 * A static helper function to look for a window given its ID
\r
468 static void fghcbWindowByID( SFG_Window *window, SFG_Enumerator *enumerator )
\r
470 /* Make sure we do not overwrite our precious results... */
\r
471 if( enumerator->found )
\r
474 /* Check the window's handle. Hope this works. Looks ugly. That's for sure. */
\r
475 if( window->ID == *( int *)(enumerator->data) )
\r
477 enumerator->found = GL_TRUE;
\r
478 enumerator->data = window;
\r
483 /* Otherwise, check this window's children */
\r
484 fgEnumSubWindows( window, fghcbWindowByID, enumerator );
\r
488 * This function is similiar to the previous one, except it is
\r
489 * looking for a specified (sub)window identifier. The function
\r
490 * is defined in freeglut_structure.c file.
\r
492 SFG_Window* fgWindowByID( int windowID )
\r
494 SFG_Enumerator enumerator;
\r
496 /* Uses a method very similiar for fgWindowByHandle... */
\r
497 enumerator.found = GL_FALSE;
\r
498 enumerator.data = ( void * )&windowID;
\r
499 fgEnumWindows( fghcbWindowByID, &enumerator );
\r
500 if( enumerator.found )
\r
501 return ( SFG_Window * )enumerator.data;
\r
506 * Looks up a menu given its ID. This is easier that fgWindowByXXX
\r
507 * as all menus are placed in one doubly linked list...
\r
509 SFG_Menu* fgMenuByID( int menuID )
\r
511 SFG_Menu *menu = NULL;
\r
513 /* It's enough to check all entries in fgStructure.Menus... */
\r
514 for( menu = (SFG_Menu *)fgStructure.Menus.First;
\r
516 menu = (SFG_Menu *)menu->Node.Next )
\r
517 if( menu->ID == menuID )
\r
523 * List functions...
\r
525 void fgListInit(SFG_List *list)
\r
527 list->First = NULL;
\r
531 void fgListAppend(SFG_List *list, SFG_Node *node)
\r
535 SFG_Node *ln = (SFG_Node *) list->Last;
\r
542 list->First = node;
\r
549 void fgListRemove(SFG_List *list, SFG_Node *node)
\r
552 ( ( SFG_Node * )node->Next )->Prev = node->Prev;
\r
554 ( ( SFG_Node * )node->Prev )->Next = node->Next;
\r
555 if( ( ( SFG_Node * )list->First ) == node )
\r
556 list->First = node->Next;
\r
557 if( ( ( SFG_Node * )list->Last ) == node )
\r
558 list->Last = node->Prev;
\r
561 int fgListLength(SFG_List *list)
\r
566 for( node =( SFG_Node * )list->First;
\r
568 node = ( SFG_Node * )node->Next )
\r
575 void fgListInsert(SFG_List *list, SFG_Node *next, SFG_Node *node)
\r
579 if( (node->Next = next) )
\r
590 if( (node->Prev = prev) )
\r
593 list->First = node;
\r
596 /*** END OF FILE ***/
\r