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
63 #if TARGET_HOST_POSIX_X11
\r
64 void fgPlatformCreateWindow ( SFG_Window *window )
\r
66 window->Window.FBConfig = NULL;
\r
68 window->State.OldHeight = window->State.OldWidth = -1;
\r
73 * This private function creates, opens and adds to the hierarchy
\r
74 * a freeglut window complete with OpenGL context and stuff...
\r
76 * If parent is set to NULL, the window created will be a topmost one.
\r
78 SFG_Window* fgCreateWindow( SFG_Window* parent, const char* title,
\r
79 GLboolean positionUse, int x, int y,
\r
80 GLboolean sizeUse, int w, int h,
\r
81 GLboolean gameMode, GLboolean isMenu )
\r
83 /* Have the window object created */
\r
84 SFG_Window *window = (SFG_Window *)calloc( sizeof(SFG_Window), 1 );
\r
86 fgPlatformCreateWindow ( window );
\r
88 fghClearCallBacks( window );
\r
90 /* Initialize the object properties */
\r
91 window->ID = ++fgStructure.WindowID;
\r
93 fgListInit( &window->Children );
\r
96 fgListAppend( &parent->Children, &window->Node );
\r
97 window->Parent = parent;
\r
100 fgListAppend( &fgStructure.Windows, &window->Node );
\r
102 /* Set the default mouse cursor and reset the modifiers value */
\r
103 window->State.Cursor = GLUT_CURSOR_INHERIT;
\r
105 window->IsMenu = isMenu;
\r
107 window->State.IgnoreKeyRepeat = GL_FALSE;
\r
108 window->State.KeyRepeating = GL_FALSE;
\r
109 window->State.IsFullscreen = GL_FALSE;
\r
112 * Open the window now. The fgOpenWindow() function is system
\r
113 * dependant, and resides in freeglut_window.c. Uses fgState.
\r
115 fgOpenWindow( window, title, positionUse, x, y, sizeUse, w, h, gameMode,
\r
116 (GLboolean)(parent ? GL_TRUE : GL_FALSE) );
\r
122 * This private function creates a menu and adds it to the menus list
\r
124 SFG_Menu* fgCreateMenu( FGCBMenu menuCallback )
\r
126 int x = 100, y = 100, w = 1, h = 1;
\r
127 SFG_Window *current_window = fgStructure.CurrentWindow;
\r
129 /* Have the menu object created */
\r
130 SFG_Menu* menu = (SFG_Menu *)calloc( sizeof(SFG_Menu), 1 );
\r
132 menu->ParentWindow = NULL;
\r
134 /* Create a window for the menu to reside in. */
\r
136 fgCreateWindow( NULL, "freeglut menu", GL_TRUE, x, y, GL_TRUE, w, h,
\r
137 GL_FALSE, GL_TRUE );
\r
138 menu->Window = fgStructure.CurrentWindow;
\r
139 glutDisplayFunc( fgDisplayMenu );
\r
141 glutHideWindow( ); /* Hide the window for now */
\r
142 fgSetWindow( current_window );
\r
144 /* Initialize the object properties: */
\r
145 menu->ID = ++fgStructure.MenuID;
\r
146 menu->Callback = menuCallback;
\r
147 menu->ActiveEntry = NULL;
\r
149 fgListInit( &menu->Entries );
\r
150 fgListAppend( &fgStructure.Menus, &menu->Node );
\r
152 /* Newly created menus implicitly become current ones */
\r
153 fgStructure.CurrentMenu = menu;
\r
159 * Function to add a window to the linked list of windows to destroy.
\r
160 * Subwindows are automatically added because they hang from the window
\r
163 void fgAddToWindowDestroyList( SFG_Window* window )
\r
165 SFG_WindowList *new_list_entry =
\r
166 ( SFG_WindowList* )malloc( sizeof(SFG_WindowList ) );
\r
167 new_list_entry->window = window;
\r
168 fgListAppend( &fgStructure.WindowsToDestroy, &new_list_entry->node );
\r
170 /* Check if the window is the current one... */
\r
171 if( fgStructure.CurrentWindow == window )
\r
172 fgStructure.CurrentWindow = NULL;
\r
175 * Clear all window callbacks except Destroy, which will
\r
176 * be invoked later. Right now, we are potentially carrying
\r
177 * out a freeglut operation at the behest of a client callback,
\r
178 * so we are reluctant to re-enter the client with the Destroy
\r
179 * callback, right now. The others are all wiped out, however,
\r
180 * to ensure that they are no longer called after this point.
\r
183 FGCBDestroy destroy = (FGCBDestroy)FETCH_WCB( *window, Destroy );
\r
184 fghClearCallBacks( window );
\r
185 SET_WCB( *window, Destroy, destroy );
\r
190 * Function to close down all the windows in the "WindowsToDestroy" list
\r
192 void fgCloseWindows( )
\r
194 while( fgStructure.WindowsToDestroy.First )
\r
196 SFG_WindowList *window_ptr = fgStructure.WindowsToDestroy.First;
\r
197 fgDestroyWindow( window_ptr->window );
\r
198 fgListRemove( &fgStructure.WindowsToDestroy, &window_ptr->node );
\r
199 free( window_ptr );
\r
204 * This function destroys a window and all of its subwindows. Actually,
\r
205 * another function, defined in freeglut_window.c is called, but this is
\r
206 * a whole different story...
\r
208 void fgDestroyWindow( SFG_Window* window )
\r
210 FREEGLUT_INTERNAL_ERROR_EXIT ( window, "Window destroy function called with null window",
\r
211 "fgDestroyWindow" );
\r
213 while( window->Children.First )
\r
214 fgDestroyWindow( ( SFG_Window * )window->Children.First );
\r
217 SFG_Window *activeWindow = fgStructure.CurrentWindow;
\r
218 INVOKE_WCB( *window, Destroy, ( ) );
\r
219 fgSetWindow( activeWindow );
\r
222 if( window->Parent )
\r
223 fgListRemove( &window->Parent->Children, &window->Node );
\r
225 fgListRemove( &fgStructure.Windows, &window->Node );
\r
227 if( window->ActiveMenu )
\r
228 fgDeactivateMenu( window );
\r
230 fghClearCallBacks( window );
\r
231 fgCloseWindow( window );
\r
233 if( fgStructure.CurrentWindow == window )
\r
234 fgStructure.CurrentWindow = NULL;
\r
238 * This is a helper static function that removes a menu (given its pointer)
\r
239 * from any windows that can be accessed from a given parent...
\r
241 static void fghRemoveMenuFromWindow( SFG_Window* window, SFG_Menu* menu )
\r
243 SFG_Window *subWindow;
\r
246 /* Check whether this is the active menu in the window */
\r
247 if ( menu == window->ActiveMenu )
\r
248 window->ActiveMenu = NULL ;
\r
251 * Check if the menu is attached to the current window,
\r
252 * if so, have it detached (by overwriting with a NULL):
\r
254 for( i = 0; i < FREEGLUT_MAX_MENUS; i++ )
\r
255 if( window->Menu[ i ] == menu )
\r
256 window->Menu[ i ] = NULL;
\r
258 /* Call this function for all of the window's children recursively: */
\r
259 for( subWindow = (SFG_Window *)window->Children.First;
\r
261 subWindow = (SFG_Window *)subWindow->Node.Next)
\r
262 fghRemoveMenuFromWindow( subWindow, menu );
\r
266 * This is a static helper function that removes menu references
\r
267 * from another menu, given two pointers to them...
\r
269 static void fghRemoveMenuFromMenu( SFG_Menu* from, SFG_Menu* menu )
\r
271 SFG_MenuEntry *entry;
\r
273 for( entry = (SFG_MenuEntry *)from->Entries.First;
\r
275 entry = ( SFG_MenuEntry * )entry->Node.Next )
\r
276 if( entry->SubMenu == menu )
\r
277 entry->SubMenu = NULL;
\r
281 * This function destroys a menu specified by the parameter. All menus
\r
282 * and windows are updated to make sure no ill pointers hang around.
\r
284 void fgDestroyMenu( SFG_Menu* menu )
\r
286 SFG_Window *window;
\r
289 FREEGLUT_INTERNAL_ERROR_EXIT ( menu, "Menu destroy function called with null menu",
\r
292 /* First of all, have all references to this menu removed from all windows: */
\r
293 for( window = (SFG_Window *)fgStructure.Windows.First;
\r
295 window = (SFG_Window *)window->Node.Next )
\r
296 fghRemoveMenuFromWindow( window, menu );
\r
298 /* Now proceed with removing menu entries that lead to this menu */
\r
299 for( from = ( SFG_Menu * )fgStructure.Menus.First;
\r
301 from = ( SFG_Menu * )from->Node.Next )
\r
302 fghRemoveMenuFromMenu( from, menu );
\r
305 * If the programmer defined a destroy callback, call it
\r
306 * A. Donev: But first make this the active menu
\r
308 if( menu->Destroy )
\r
310 SFG_Menu *activeMenu=fgStructure.CurrentMenu;
\r
311 fgStructure.CurrentMenu = menu;
\r
313 fgStructure.CurrentMenu = activeMenu;
\r
317 * Now we are pretty sure the menu is not used anywhere
\r
318 * and that we can remove all of its entries
\r
320 while( menu->Entries.First )
\r
322 SFG_MenuEntry *entry = ( SFG_MenuEntry * ) menu->Entries.First;
\r
324 fgListRemove( &menu->Entries, &entry->Node );
\r
327 free( entry->Text );
\r
328 entry->Text = NULL;
\r
333 if( fgStructure.CurrentWindow == menu->Window )
\r
334 fgSetWindow( NULL );
\r
335 fgDestroyWindow( menu->Window );
\r
336 fgListRemove( &fgStructure.Menus, &menu->Node );
\r
337 if( fgStructure.CurrentMenu == menu )
\r
338 fgStructure.CurrentMenu = NULL;
\r
344 * This function should be called on glutInit(). It will prepare the internal
\r
345 * structure of freeglut to be used in the application. The structure will be
\r
346 * destroyed using fgDestroyStructure() on glutMainLoop() return. In that
\r
347 * case further use of freeglut should be preceeded with a glutInit() call.
\r
349 void fgCreateStructure( void )
\r
352 * We will be needing two lists: the first containing windows,
\r
353 * and the second containing the user-defined menus.
\r
354 * Also, no current window/menu is set, as none has been created yet.
\r
357 fgListInit(&fgStructure.Windows);
\r
358 fgListInit(&fgStructure.Menus);
\r
359 fgListInit(&fgStructure.WindowsToDestroy);
\r
361 fgStructure.CurrentWindow = NULL;
\r
362 fgStructure.CurrentMenu = NULL;
\r
363 fgStructure.MenuContext = NULL;
\r
364 fgStructure.GameModeWindow = NULL;
\r
365 fgStructure.WindowID = 0;
\r
366 fgStructure.MenuID = 0;
\r
370 * This function is automatically called on glutMainLoop() return.
\r
371 * It should deallocate and destroy all remnants of previous
\r
372 * glutInit()-enforced structure initialization...
\r
374 void fgDestroyStructure( void )
\r
376 /* Clean up the WindowsToDestroy list. */
\r
379 /* Make sure all windows and menus have been deallocated */
\r
380 while( fgStructure.Menus.First )
\r
381 fgDestroyMenu( ( SFG_Menu * )fgStructure.Menus.First );
\r
383 while( fgStructure.Windows.First )
\r
384 fgDestroyWindow( ( SFG_Window * )fgStructure.Windows.First );
\r
388 * Helper function to enumerate through all registered top-level windows
\r
390 void fgEnumWindows( FGCBenumerator enumCallback, SFG_Enumerator* enumerator )
\r
392 SFG_Window *window;
\r
394 FREEGLUT_INTERNAL_ERROR_EXIT ( enumCallback && enumerator,
\r
395 "Enumerator or callback missing from window enumerator call",
\r
398 /* Check every of the top-level windows */
\r
399 for( window = ( SFG_Window * )fgStructure.Windows.First;
\r
401 window = ( SFG_Window * )window->Node.Next )
\r
403 enumCallback( window, enumerator );
\r
404 if( enumerator->found )
\r
410 * Helper function to enumerate through all a window's subwindows
\r
411 * (single level descent)
\r
413 void fgEnumSubWindows( SFG_Window* window, FGCBenumerator enumCallback,
\r
414 SFG_Enumerator* enumerator )
\r
418 FREEGLUT_INTERNAL_ERROR_EXIT ( enumCallback && enumerator,
\r
419 "Enumerator or callback missing from subwindow enumerator call",
\r
420 "fgEnumSubWindows" );
\r
421 FREEGLUT_INTERNAL_ERROR_EXIT_IF_NOT_INITIALISED ( "Window Enumeration" );
\r
423 for( child = ( SFG_Window * )window->Children.First;
\r
425 child = ( SFG_Window * )child->Node.Next )
\r
427 enumCallback( child, enumerator );
\r
428 if( enumerator->found )
\r
434 * A static helper function to look for a window given its handle
\r
436 static void fghcbWindowByHandle( SFG_Window *window,
\r
437 SFG_Enumerator *enumerator )
\r
439 if ( enumerator->found )
\r
442 /* Check the window's handle. Hope this works. Looks ugly. That's for sure. */
\r
443 if( window->Window.Handle == (SFG_WindowHandleType) (enumerator->data) )
\r
445 enumerator->found = GL_TRUE;
\r
446 enumerator->data = window;
\r
451 /* Otherwise, check this window's children */
\r
452 fgEnumSubWindows( window, fghcbWindowByHandle, enumerator );
\r
456 * fgWindowByHandle returns a (SFG_Window *) value pointing to the
\r
457 * first window in the queue matching the specified window handle.
\r
458 * The function is defined in freeglut_structure.c file.
\r
460 SFG_Window* fgWindowByHandle ( SFG_WindowHandleType hWindow )
\r
462 SFG_Enumerator enumerator;
\r
464 /* This is easy and makes use of the windows enumeration defined above */
\r
465 enumerator.found = GL_FALSE;
\r
466 enumerator.data = (void *)hWindow;
\r
467 fgEnumWindows( fghcbWindowByHandle, &enumerator );
\r
469 if( enumerator.found )
\r
470 return( SFG_Window *) enumerator.data;
\r
475 * A static helper function to look for a window given its ID
\r
477 static void fghcbWindowByID( SFG_Window *window, SFG_Enumerator *enumerator )
\r
479 /* Make sure we do not overwrite our precious results... */
\r
480 if( enumerator->found )
\r
483 /* Check the window's handle. Hope this works. Looks ugly. That's for sure. */
\r
484 if( window->ID == *( int *)(enumerator->data) )
\r
486 enumerator->found = GL_TRUE;
\r
487 enumerator->data = window;
\r
492 /* Otherwise, check this window's children */
\r
493 fgEnumSubWindows( window, fghcbWindowByID, enumerator );
\r
497 * This function is similiar to the previous one, except it is
\r
498 * looking for a specified (sub)window identifier. The function
\r
499 * is defined in freeglut_structure.c file.
\r
501 SFG_Window* fgWindowByID( int windowID )
\r
503 SFG_Enumerator enumerator;
\r
505 /* Uses a method very similiar for fgWindowByHandle... */
\r
506 enumerator.found = GL_FALSE;
\r
507 enumerator.data = ( void * )&windowID;
\r
508 fgEnumWindows( fghcbWindowByID, &enumerator );
\r
509 if( enumerator.found )
\r
510 return ( SFG_Window * )enumerator.data;
\r
515 * Looks up a menu given its ID. This is easier that fgWindowByXXX
\r
516 * as all menus are placed in one doubly linked list...
\r
518 SFG_Menu* fgMenuByID( int menuID )
\r
520 SFG_Menu *menu = NULL;
\r
522 /* It's enough to check all entries in fgStructure.Menus... */
\r
523 for( menu = (SFG_Menu *)fgStructure.Menus.First;
\r
525 menu = (SFG_Menu *)menu->Node.Next )
\r
526 if( menu->ID == menuID )
\r
532 * List functions...
\r
534 void fgListInit(SFG_List *list)
\r
536 list->First = NULL;
\r
540 void fgListAppend(SFG_List *list, SFG_Node *node)
\r
544 SFG_Node *ln = (SFG_Node *) list->Last;
\r
551 list->First = node;
\r
558 void fgListRemove(SFG_List *list, SFG_Node *node)
\r
561 ( ( SFG_Node * )node->Next )->Prev = node->Prev;
\r
563 ( ( SFG_Node * )node->Prev )->Next = node->Next;
\r
564 if( ( ( SFG_Node * )list->First ) == node )
\r
565 list->First = node->Next;
\r
566 if( ( ( SFG_Node * )list->Last ) == node )
\r
567 list->Last = node->Prev;
\r
570 int fgListLength(SFG_List *list)
\r
575 for( node =( SFG_Node * )list->First;
\r
577 node = ( SFG_Node * )node->Next )
\r
584 void fgListInsert(SFG_List *list, SFG_Node *next, SFG_Node *node)
\r
588 if( (node->Next = next) )
\r
599 if( (node->Prev = prev) )
\r
602 list->First = node;
\r
605 /*** END OF FILE ***/
\r