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 static void fghClearCallBacks( SFG_Window *window )
\r
56 for( i = 0; i < TOTAL_CALLBACKS; ++i )
\r
57 window->CallBacks[ i ] = NULL;
\r
62 * This private function creates, opens and adds to the hierarchy
\r
63 * a freeglut window complete with OpenGL context and stuff...
\r
65 * If parent is set to NULL, the window created will be a topmost one.
\r
67 SFG_Window* fgCreateWindow( SFG_Window* parent, const char* title,
\r
68 GLboolean positionUse, int x, int y,
\r
69 GLboolean sizeUse, int w, int h,
\r
70 GLboolean gameMode, GLboolean isMenu )
\r
72 /* Have the window object created */
\r
73 SFG_Window *window = (SFG_Window *)calloc( sizeof(SFG_Window), 1 );
\r
75 #if TARGET_HOST_UNIX_X11
\r
76 window->Window.FBConfig = NULL;
\r
78 fghClearCallBacks( window );
\r
80 /* Initialize the object properties */
\r
81 window->ID = ++fgStructure.WindowID;
\r
82 #if TARGET_HOST_POSIX_X11
\r
83 window->State.OldHeight = window->State.OldWidth = -1;
\r
86 fgListInit( &window->Children );
\r
89 fgListAppend( &parent->Children, &window->Node );
\r
90 window->Parent = parent;
\r
93 fgListAppend( &fgStructure.Windows, &window->Node );
\r
95 /* Set the default mouse cursor and reset the modifiers value */
\r
96 window->State.Cursor = GLUT_CURSOR_INHERIT;
\r
98 window->IsMenu = isMenu;
\r
100 window->State.IgnoreKeyRepeat = GL_FALSE;
\r
101 window->State.KeyRepeating = GL_FALSE;
\r
102 window->State.IsFullscreen = GL_FALSE;
\r
105 * Open the window now. The fgOpenWindow() function is system
\r
106 * dependant, and resides in freeglut_window.c. Uses fgState.
\r
108 fgOpenWindow( window, title, positionUse, x, y, sizeUse, w, h, gameMode,
\r
109 (GLboolean)(parent ? GL_TRUE : GL_FALSE) );
\r
115 * This private function creates a menu and adds it to the menus list
\r
117 SFG_Menu* fgCreateMenu( FGCBMenu menuCallback )
\r
119 int x = 100, y = 100, w = 1, h = 1;
\r
120 SFG_Window *current_window = fgStructure.CurrentWindow;
\r
122 /* Have the menu object created */
\r
123 SFG_Menu* menu = (SFG_Menu *)calloc( sizeof(SFG_Menu), 1 );
\r
125 menu->ParentWindow = NULL;
\r
127 /* Create a window for the menu to reside in. */
\r
129 fgCreateWindow( NULL, "freeglut menu", GL_TRUE, x, y, GL_TRUE, w, h,
\r
130 GL_FALSE, GL_TRUE );
\r
131 menu->Window = fgStructure.CurrentWindow;
\r
132 glutDisplayFunc( fgDisplayMenu );
\r
134 glutHideWindow( ); /* Hide the window for now */
\r
135 fgSetWindow( current_window );
\r
137 /* Initialize the object properties: */
\r
138 menu->ID = ++fgStructure.MenuID;
\r
139 menu->Callback = menuCallback;
\r
140 menu->ActiveEntry = NULL;
\r
142 fgListInit( &menu->Entries );
\r
143 fgListAppend( &fgStructure.Menus, &menu->Node );
\r
145 /* Newly created menus implicitly become current ones */
\r
146 fgStructure.CurrentMenu = menu;
\r
152 * Function to add a window to the linked list of windows to destroy.
\r
153 * Subwindows are automatically added because they hang from the window
\r
156 void fgAddToWindowDestroyList( SFG_Window* window )
\r
158 SFG_WindowList *new_list_entry =
\r
159 ( SFG_WindowList* )malloc( sizeof(SFG_WindowList ) );
\r
160 new_list_entry->window = window;
\r
161 fgListAppend( &fgStructure.WindowsToDestroy, &new_list_entry->node );
\r
163 /* Check if the window is the current one... */
\r
164 if( fgStructure.CurrentWindow == window )
\r
165 fgStructure.CurrentWindow = NULL;
\r
168 * Clear all window callbacks except Destroy, which will
\r
169 * be invoked later. Right now, we are potentially carrying
\r
170 * out a freeglut operation at the behest of a client callback,
\r
171 * so we are reluctant to re-enter the client with the Destroy
\r
172 * callback, right now. The others are all wiped out, however,
\r
173 * to ensure that they are no longer called after this point.
\r
176 FGCBDestroy destroy = (FGCBDestroy)FETCH_WCB( *window, Destroy );
\r
177 fghClearCallBacks( window );
\r
178 SET_WCB( *window, Destroy, destroy );
\r
183 * Function to close down all the windows in the "WindowsToDestroy" list
\r
185 void fgCloseWindows( )
\r
187 while( fgStructure.WindowsToDestroy.First )
\r
189 SFG_WindowList *window_ptr = fgStructure.WindowsToDestroy.First;
\r
190 fgDestroyWindow( window_ptr->window );
\r
191 fgListRemove( &fgStructure.WindowsToDestroy, &window_ptr->node );
\r
192 free( window_ptr );
\r
197 * This function destroys a window and all of its subwindows. Actually,
\r
198 * another function, defined in freeglut_window.c is called, but this is
\r
199 * a whole different story...
\r
201 void fgDestroyWindow( SFG_Window* window )
\r
203 FREEGLUT_INTERNAL_ERROR_EXIT ( window, "Window destroy function called with null window",
\r
204 "fgDestroyWindow" );
\r
206 while( window->Children.First )
\r
207 fgDestroyWindow( ( SFG_Window * )window->Children.First );
\r
210 SFG_Window *activeWindow = fgStructure.CurrentWindow;
\r
211 INVOKE_WCB( *window, Destroy, ( ) );
\r
212 fgSetWindow( activeWindow );
\r
215 if( window->Parent )
\r
216 fgListRemove( &window->Parent->Children, &window->Node );
\r
218 fgListRemove( &fgStructure.Windows, &window->Node );
\r
220 if( window->ActiveMenu )
\r
221 fgDeactivateMenu( window );
\r
223 fghClearCallBacks( window );
\r
224 fgCloseWindow( window );
\r
226 if( fgStructure.CurrentWindow == window )
\r
227 fgStructure.CurrentWindow = NULL;
\r
231 * This is a helper static function that removes a menu (given its pointer)
\r
232 * from any windows that can be accessed from a given parent...
\r
234 static void fghRemoveMenuFromWindow( SFG_Window* window, SFG_Menu* menu )
\r
236 SFG_Window *subWindow;
\r
239 /* Check whether this is the active menu in the window */
\r
240 if ( menu == window->ActiveMenu )
\r
241 window->ActiveMenu = NULL ;
\r
244 * Check if the menu is attached to the current window,
\r
245 * if so, have it detached (by overwriting with a NULL):
\r
247 for( i = 0; i < FREEGLUT_MAX_MENUS; i++ )
\r
248 if( window->Menu[ i ] == menu )
\r
249 window->Menu[ i ] = NULL;
\r
251 /* Call this function for all of the window's children recursively: */
\r
252 for( subWindow = (SFG_Window *)window->Children.First;
\r
254 subWindow = (SFG_Window *)subWindow->Node.Next)
\r
255 fghRemoveMenuFromWindow( subWindow, menu );
\r
259 * This is a static helper function that removes menu references
\r
260 * from another menu, given two pointers to them...
\r
262 static void fghRemoveMenuFromMenu( SFG_Menu* from, SFG_Menu* menu )
\r
264 SFG_MenuEntry *entry;
\r
266 for( entry = (SFG_MenuEntry *)from->Entries.First;
\r
268 entry = ( SFG_MenuEntry * )entry->Node.Next )
\r
269 if( entry->SubMenu == menu )
\r
270 entry->SubMenu = NULL;
\r
274 * This function destroys a menu specified by the parameter. All menus
\r
275 * and windows are updated to make sure no ill pointers hang around.
\r
277 void fgDestroyMenu( SFG_Menu* menu )
\r
279 SFG_Window *window;
\r
282 FREEGLUT_INTERNAL_ERROR_EXIT ( menu, "Menu destroy function called with null menu",
\r
285 /* First of all, have all references to this menu removed from all windows: */
\r
286 for( window = (SFG_Window *)fgStructure.Windows.First;
\r
288 window = (SFG_Window *)window->Node.Next )
\r
289 fghRemoveMenuFromWindow( window, menu );
\r
291 /* Now proceed with removing menu entries that lead to this menu */
\r
292 for( from = ( SFG_Menu * )fgStructure.Menus.First;
\r
294 from = ( SFG_Menu * )from->Node.Next )
\r
295 fghRemoveMenuFromMenu( from, menu );
\r
298 * If the programmer defined a destroy callback, call it
\r
299 * A. Donev: But first make this the active menu
\r
301 if( menu->Destroy )
\r
303 SFG_Menu *activeMenu=fgStructure.CurrentMenu;
\r
304 fgStructure.CurrentMenu = menu;
\r
306 fgStructure.CurrentMenu = activeMenu;
\r
310 * Now we are pretty sure the menu is not used anywhere
\r
311 * and that we can remove all of its entries
\r
313 while( menu->Entries.First )
\r
315 SFG_MenuEntry *entry = ( SFG_MenuEntry * ) menu->Entries.First;
\r
317 fgListRemove( &menu->Entries, &entry->Node );
\r
320 free( entry->Text );
\r
321 entry->Text = NULL;
\r
326 if( fgStructure.CurrentWindow == menu->Window )
\r
327 fgSetWindow( NULL );
\r
328 fgDestroyWindow( menu->Window );
\r
329 fgListRemove( &fgStructure.Menus, &menu->Node );
\r
330 if( fgStructure.CurrentMenu == menu )
\r
331 fgStructure.CurrentMenu = NULL;
\r
337 * This function should be called on glutInit(). It will prepare the internal
\r
338 * structure of freeglut to be used in the application. The structure will be
\r
339 * destroyed using fgDestroyStructure() on glutMainLoop() return. In that
\r
340 * case further use of freeglut should be preceeded with a glutInit() call.
\r
342 void fgCreateStructure( void )
\r
345 * We will be needing two lists: the first containing windows,
\r
346 * and the second containing the user-defined menus.
\r
347 * Also, no current window/menu is set, as none has been created yet.
\r
350 fgListInit(&fgStructure.Windows);
\r
351 fgListInit(&fgStructure.Menus);
\r
352 fgListInit(&fgStructure.WindowsToDestroy);
\r
354 fgStructure.CurrentWindow = NULL;
\r
355 fgStructure.CurrentMenu = NULL;
\r
356 fgStructure.MenuContext = NULL;
\r
357 fgStructure.GameModeWindow = NULL;
\r
358 fgStructure.WindowID = 0;
\r
359 fgStructure.MenuID = 0;
\r
363 * This function is automatically called on glutMainLoop() return.
\r
364 * It should deallocate and destroy all remnants of previous
\r
365 * glutInit()-enforced structure initialization...
\r
367 void fgDestroyStructure( void )
\r
369 /* Clean up the WindowsToDestroy list. */
\r
372 /* Make sure all windows and menus have been deallocated */
\r
373 while( fgStructure.Menus.First )
\r
374 fgDestroyMenu( ( SFG_Menu * )fgStructure.Menus.First );
\r
376 while( fgStructure.Windows.First )
\r
377 fgDestroyWindow( ( SFG_Window * )fgStructure.Windows.First );
\r
381 * Helper function to enumerate through all registered top-level windows
\r
383 void fgEnumWindows( FGCBenumerator enumCallback, SFG_Enumerator* enumerator )
\r
385 SFG_Window *window;
\r
387 FREEGLUT_INTERNAL_ERROR_EXIT ( enumCallback && enumerator,
\r
388 "Enumerator or callback missing from window enumerator call",
\r
391 /* Check every of the top-level windows */
\r
392 for( window = ( SFG_Window * )fgStructure.Windows.First;
\r
394 window = ( SFG_Window * )window->Node.Next )
\r
396 enumCallback( window, enumerator );
\r
397 if( enumerator->found )
\r
403 * Helper function to enumerate through all a window's subwindows
\r
404 * (single level descent)
\r
406 void fgEnumSubWindows( SFG_Window* window, FGCBenumerator enumCallback,
\r
407 SFG_Enumerator* enumerator )
\r
411 FREEGLUT_INTERNAL_ERROR_EXIT ( enumCallback && enumerator,
\r
412 "Enumerator or callback missing from subwindow enumerator call",
\r
413 "fgEnumSubWindows" );
\r
414 FREEGLUT_INTERNAL_ERROR_EXIT_IF_NOT_INITIALISED ( "Window Enumeration" );
\r
416 for( child = ( SFG_Window * )window->Children.First;
\r
418 child = ( SFG_Window * )child->Node.Next )
\r
420 enumCallback( child, enumerator );
\r
421 if( enumerator->found )
\r
427 * A static helper function to look for a window given its handle
\r
429 static void fghcbWindowByHandle( SFG_Window *window,
\r
430 SFG_Enumerator *enumerator )
\r
432 if ( enumerator->found )
\r
435 /* Check the window's handle. Hope this works. Looks ugly. That's for sure. */
\r
436 if( window->Window.Handle == (SFG_WindowHandleType) (enumerator->data) )
\r
438 enumerator->found = GL_TRUE;
\r
439 enumerator->data = window;
\r
444 /* Otherwise, check this window's children */
\r
445 fgEnumSubWindows( window, fghcbWindowByHandle, enumerator );
\r
449 * fgWindowByHandle returns a (SFG_Window *) value pointing to the
\r
450 * first window in the queue matching the specified window handle.
\r
451 * The function is defined in freeglut_structure.c file.
\r
453 SFG_Window* fgWindowByHandle ( SFG_WindowHandleType hWindow )
\r
455 SFG_Enumerator enumerator;
\r
457 /* This is easy and makes use of the windows enumeration defined above */
\r
458 enumerator.found = GL_FALSE;
\r
459 enumerator.data = (void *)hWindow;
\r
460 fgEnumWindows( fghcbWindowByHandle, &enumerator );
\r
462 if( enumerator.found )
\r
463 return( SFG_Window *) enumerator.data;
\r
468 * A static helper function to look for a window given its ID
\r
470 static void fghcbWindowByID( SFG_Window *window, SFG_Enumerator *enumerator )
\r
472 /* Make sure we do not overwrite our precious results... */
\r
473 if( enumerator->found )
\r
476 /* Check the window's handle. Hope this works. Looks ugly. That's for sure. */
\r
477 if( window->ID == *( int *)(enumerator->data) )
\r
479 enumerator->found = GL_TRUE;
\r
480 enumerator->data = window;
\r
485 /* Otherwise, check this window's children */
\r
486 fgEnumSubWindows( window, fghcbWindowByID, enumerator );
\r
490 * This function is similiar to the previous one, except it is
\r
491 * looking for a specified (sub)window identifier. The function
\r
492 * is defined in freeglut_structure.c file.
\r
494 SFG_Window* fgWindowByID( int windowID )
\r
496 SFG_Enumerator enumerator;
\r
498 /* Uses a method very similiar for fgWindowByHandle... */
\r
499 enumerator.found = GL_FALSE;
\r
500 enumerator.data = ( void * )&windowID;
\r
501 fgEnumWindows( fghcbWindowByID, &enumerator );
\r
502 if( enumerator.found )
\r
503 return ( SFG_Window * )enumerator.data;
\r
508 * Looks up a menu given its ID. This is easier that fgWindowByXXX
\r
509 * as all menus are placed in one doubly linked list...
\r
511 SFG_Menu* fgMenuByID( int menuID )
\r
513 SFG_Menu *menu = NULL;
\r
515 /* It's enough to check all entries in fgStructure.Menus... */
\r
516 for( menu = (SFG_Menu *)fgStructure.Menus.First;
\r
518 menu = (SFG_Menu *)menu->Node.Next )
\r
519 if( menu->ID == menuID )
\r
525 * List functions...
\r
527 void fgListInit(SFG_List *list)
\r
529 list->First = NULL;
\r
533 void fgListAppend(SFG_List *list, SFG_Node *node)
\r
537 SFG_Node *ln = (SFG_Node *) list->Last;
\r
544 list->First = node;
\r
551 void fgListRemove(SFG_List *list, SFG_Node *node)
\r
554 ( ( SFG_Node * )node->Next )->Prev = node->Prev;
\r
556 ( ( SFG_Node * )node->Prev )->Next = node->Next;
\r
557 if( ( ( SFG_Node * )list->First ) == node )
\r
558 list->First = node->Next;
\r
559 if( ( ( SFG_Node * )list->Last ) == node )
\r
560 list->Last = node->Prev;
\r
563 int fgListLength(SFG_List *list)
\r
568 for( node =( SFG_Node * )list->First;
\r
570 node = ( SFG_Node * )node->Next )
\r
577 void fgListInsert(SFG_List *list, SFG_Node *next, SFG_Node *node)
\r
581 if( (node->Next = next) )
\r
592 if( (node->Prev = prev) )
\r
595 list->First = node;
\r
598 /*** END OF FILE ***/
\r