resetting manifest requested domain to floor
[platform/upstream/freeglut.git] / src / freeglut_structure.c
1 /*
2  * freeglut_structure.c
3  *
4  * Windows and menus need tree structure
5  *
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
9  *
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:
16  *
17  * The above copyright notice and this permission notice shall be included
18  * in all copies or substantial portions of the Software.
19  *
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.
26  */
27
28 #include <GL/freeglut.h>
29 #include "freeglut_internal.h"
30
31 /* -- GLOBAL EXPORTS ------------------------------------------------------- */
32
33 /*
34  * The SFG_Structure container holds information about windows and menus
35  * created between glutInit() and glutMainLoop() return.
36  */
37
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   */
47
48
49 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */
50
51 static void fghClearCallBacks( SFG_Window *window )
52 {
53     if( window )
54     {
55         int i;
56         for( i = 0; i < TOTAL_CALLBACKS; ++i )
57             window->CallBacks[ i ] = NULL;
58     }
59 }
60
61 /*
62  * This private function creates, opens and adds to the hierarchy
63  * a freeglut window complete with OpenGL context and stuff...
64  *
65  * If parent is set to NULL, the window created will be a topmost one.
66  */
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 )
71 {
72     /* Have the window object created */
73     SFG_Window *window = (SFG_Window *)calloc( sizeof(SFG_Window), 1 );
74
75 #if TARGET_HOST_UNIX_X11
76     window->Window.FBConfig = NULL;
77 #endif
78     fghClearCallBacks( window );
79
80     /* Initialize the object properties */
81     window->ID = ++fgStructure.WindowID;
82 #if TARGET_HOST_POSIX_X11
83     window->State.OldHeight = window->State.OldWidth = -1;
84 #endif
85
86     fgListInit( &window->Children );
87     if( parent )
88     {
89         fgListAppend( &parent->Children, &window->Node );
90         window->Parent = parent;
91     }
92     else
93         fgListAppend( &fgStructure.Windows, &window->Node );
94
95     /* Set the default mouse cursor and reset the modifiers value */
96     window->State.Cursor    = GLUT_CURSOR_INHERIT;
97
98     window->IsMenu = isMenu;
99
100     window->State.IgnoreKeyRepeat = GL_FALSE;
101     window->State.KeyRepeating    = GL_FALSE;
102     window->State.IsFullscreen    = GL_FALSE;
103
104     /*
105      * Open the window now. The fgOpenWindow() function is system
106      * dependant, and resides in freeglut_window.c. Uses fgState.
107      */
108     fgOpenWindow( window, title, positionUse, x, y, sizeUse, w, h, gameMode,
109                   (GLboolean)(parent ? GL_TRUE : GL_FALSE) );
110
111     return window;
112 }
113
114 /*
115  * This private function creates a menu and adds it to the menus list
116  */
117 SFG_Menu* fgCreateMenu( FGCBMenu menuCallback )
118 {
119     int x = 100, y = 100, w = 1, h = 1;
120     SFG_Window *current_window = fgStructure.CurrentWindow;
121
122     /* Have the menu object created */
123     SFG_Menu* menu = (SFG_Menu *)calloc( sizeof(SFG_Menu), 1 );
124
125     menu->ParentWindow = NULL;
126
127     /* Create a window for the menu to reside in. */
128
129     fgCreateWindow( NULL, "freeglut menu", GL_TRUE, x, y, GL_TRUE, w, h,
130                     GL_FALSE, GL_TRUE );
131     menu->Window = fgStructure.CurrentWindow;
132     glutDisplayFunc( fgDisplayMenu );
133
134     glutHideWindow( );  /* Hide the window for now */
135     fgSetWindow( current_window );
136
137     /* Initialize the object properties: */
138     menu->ID       = ++fgStructure.MenuID;
139     menu->Callback = menuCallback;
140     menu->ActiveEntry = NULL;
141
142     fgListInit( &menu->Entries );
143     fgListAppend( &fgStructure.Menus, &menu->Node );
144
145     /* Newly created menus implicitly become current ones */
146     fgStructure.CurrentMenu = menu;
147
148     return menu;
149 }
150
151 /*
152  * Function to add a window to the linked list of windows to destroy.
153  * Subwindows are automatically added because they hang from the window
154  * structure.
155  */
156 void fgAddToWindowDestroyList( SFG_Window* window )
157 {
158     SFG_WindowList *new_list_entry =
159         ( SFG_WindowList* )malloc( sizeof(SFG_WindowList ) );
160     new_list_entry->window = window;
161     fgListAppend( &fgStructure.WindowsToDestroy, &new_list_entry->node );
162
163     /* Check if the window is the current one... */
164     if( fgStructure.CurrentWindow == window )
165         fgStructure.CurrentWindow = NULL;
166
167     /*
168      * Clear all window callbacks except Destroy, which will
169      * be invoked later.  Right now, we are potentially carrying
170      * out a freeglut operation at the behest of a client callback,
171      * so we are reluctant to re-enter the client with the Destroy
172      * callback, right now.  The others are all wiped out, however,
173      * to ensure that they are no longer called after this point.
174      */
175     {
176         FGCBDestroy destroy = (FGCBDestroy)FETCH_WCB( *window, Destroy );
177         fghClearCallBacks( window );
178         SET_WCB( *window, Destroy, destroy );
179     }
180 }
181
182 /*
183  * Function to close down all the windows in the "WindowsToDestroy" list
184  */
185 void fgCloseWindows( )
186 {
187     while( fgStructure.WindowsToDestroy.First )
188     {
189         SFG_WindowList *window_ptr = fgStructure.WindowsToDestroy.First;
190         fgDestroyWindow( window_ptr->window );
191         fgListRemove( &fgStructure.WindowsToDestroy, &window_ptr->node );
192         free( window_ptr );
193     }
194 }
195
196 /*
197  * This function destroys a window and all of its subwindows. Actually,
198  * another function, defined in freeglut_window.c is called, but this is
199  * a whole different story...
200  */
201 void fgDestroyWindow( SFG_Window* window )
202 {
203     FREEGLUT_INTERNAL_ERROR_EXIT ( window, "Window destroy function called with null window",
204                                    "fgDestroyWindow" );
205
206     while( window->Children.First )
207         fgDestroyWindow( ( SFG_Window * )window->Children.First );
208
209     {
210         SFG_Window *activeWindow = fgStructure.CurrentWindow;
211         INVOKE_WCB( *window, Destroy, ( ) );
212         fgSetWindow( activeWindow );
213     }
214
215     if( window->Parent )
216         fgListRemove( &window->Parent->Children, &window->Node );
217     else
218         fgListRemove( &fgStructure.Windows, &window->Node );
219
220     if( window->ActiveMenu )
221       fgDeactivateMenu( window );
222
223     fghClearCallBacks( window );
224     fgCloseWindow( window );
225     free( window );
226     if( fgStructure.CurrentWindow == window )
227         fgStructure.CurrentWindow = NULL;
228 }
229
230 /*
231  * This is a helper static function that removes a menu (given its pointer)
232  * from any windows that can be accessed from a given parent...
233  */
234 static void fghRemoveMenuFromWindow( SFG_Window* window, SFG_Menu* menu )
235 {
236     SFG_Window *subWindow;
237     int i;
238
239     /* Check whether this is the active menu in the window */
240     if ( menu == window->ActiveMenu )
241         window->ActiveMenu = NULL ;
242
243     /*
244      * Check if the menu is attached to the current window,
245      * if so, have it detached (by overwriting with a NULL):
246      */
247     for( i = 0; i < FREEGLUT_MAX_MENUS; i++ )
248         if( window->Menu[ i ] == menu )
249             window->Menu[ i ] = NULL;
250
251     /* Call this function for all of the window's children recursively: */
252     for( subWindow = (SFG_Window *)window->Children.First;
253          subWindow;
254          subWindow = (SFG_Window *)subWindow->Node.Next)
255         fghRemoveMenuFromWindow( subWindow, menu );
256 }
257
258 /*
259  * This is a static helper function that removes menu references
260  * from another menu, given two pointers to them...
261  */
262 static void fghRemoveMenuFromMenu( SFG_Menu* from, SFG_Menu* menu )
263 {
264     SFG_MenuEntry *entry;
265
266     for( entry = (SFG_MenuEntry *)from->Entries.First;
267          entry;
268          entry = ( SFG_MenuEntry * )entry->Node.Next )
269         if( entry->SubMenu == menu )
270             entry->SubMenu = NULL;
271 }
272
273 /*
274  * This function destroys a menu specified by the parameter. All menus
275  * and windows are updated to make sure no ill pointers hang around.
276  */
277 void fgDestroyMenu( SFG_Menu* menu )
278 {
279     SFG_Window *window;
280     SFG_Menu *from;
281
282     FREEGLUT_INTERNAL_ERROR_EXIT ( menu, "Menu destroy function called with null menu",
283                                    "fgDestroyMenu" );
284
285     /* First of all, have all references to this menu removed from all windows: */
286     for( window = (SFG_Window *)fgStructure.Windows.First;
287          window;
288          window = (SFG_Window *)window->Node.Next )
289         fghRemoveMenuFromWindow( window, menu );
290
291     /* Now proceed with removing menu entries that lead to this menu */
292     for( from = ( SFG_Menu * )fgStructure.Menus.First;
293          from;
294          from = ( SFG_Menu * )from->Node.Next )
295         fghRemoveMenuFromMenu( from, menu );
296
297     /*
298      * If the programmer defined a destroy callback, call it
299      * A. Donev: But first make this the active menu
300      */
301     if( menu->Destroy )
302     {
303         SFG_Menu *activeMenu=fgStructure.CurrentMenu;
304         fgStructure.CurrentMenu = menu;
305         menu->Destroy( );
306         fgStructure.CurrentMenu = activeMenu;
307     }
308
309     /*
310      * Now we are pretty sure the menu is not used anywhere
311      * and that we can remove all of its entries
312      */
313     while( menu->Entries.First )
314     {
315         SFG_MenuEntry *entry = ( SFG_MenuEntry * ) menu->Entries.First;
316
317         fgListRemove( &menu->Entries, &entry->Node );
318
319         if( entry->Text )
320             free( entry->Text );
321         entry->Text = NULL;
322
323         free( entry );
324     }
325
326     if( fgStructure.CurrentWindow == menu->Window )
327         fgSetWindow( NULL );
328     fgDestroyWindow( menu->Window );
329     fgListRemove( &fgStructure.Menus, &menu->Node );
330     if( fgStructure.CurrentMenu == menu )
331         fgStructure.CurrentMenu = NULL;
332
333     free( menu );
334 }
335
336 /*
337  * This function should be called on glutInit(). It will prepare the internal
338  * structure of freeglut to be used in the application. The structure will be
339  * destroyed using fgDestroyStructure() on glutMainLoop() return. In that
340  * case further use of freeglut should be preceeded with a glutInit() call.
341  */
342 void fgCreateStructure( void )
343 {
344     /*
345      * We will be needing two lists: the first containing windows,
346      * and the second containing the user-defined menus.
347      * Also, no current window/menu is set, as none has been created yet.
348      */
349
350     fgListInit(&fgStructure.Windows);
351     fgListInit(&fgStructure.Menus);
352     fgListInit(&fgStructure.WindowsToDestroy);
353
354     fgStructure.CurrentWindow = NULL;
355     fgStructure.CurrentMenu = NULL;
356     fgStructure.MenuContext = NULL;
357     fgStructure.GameModeWindow = NULL;
358     fgStructure.WindowID = 0;
359     fgStructure.MenuID = 0;
360 }
361
362 /*
363  * This function is automatically called on glutMainLoop() return.
364  * It should deallocate and destroy all remnants of previous
365  * glutInit()-enforced structure initialization...
366  */
367 void fgDestroyStructure( void )
368 {
369     /* Clean up the WindowsToDestroy list. */
370     fgCloseWindows( );
371
372     /* Make sure all windows and menus have been deallocated */
373     while( fgStructure.Menus.First )
374         fgDestroyMenu( ( SFG_Menu * )fgStructure.Menus.First );
375
376     while( fgStructure.Windows.First )
377         fgDestroyWindow( ( SFG_Window * )fgStructure.Windows.First );
378 }
379
380 /*
381  * Helper function to enumerate through all registered top-level windows
382  */
383 void fgEnumWindows( FGCBenumerator enumCallback, SFG_Enumerator* enumerator )
384 {
385     SFG_Window *window;
386
387     FREEGLUT_INTERNAL_ERROR_EXIT ( enumCallback && enumerator,
388                                    "Enumerator or callback missing from window enumerator call",
389                                    "fgEnumWindows" );
390
391     /* Check every of the top-level windows */
392     for( window = ( SFG_Window * )fgStructure.Windows.First;
393          window;
394          window = ( SFG_Window * )window->Node.Next )
395     {
396         enumCallback( window, enumerator );
397         if( enumerator->found )
398             return;
399     }
400 }
401
402 /*
403  * Helper function to enumerate through all a window's subwindows
404  * (single level descent)
405  */
406 void fgEnumSubWindows( SFG_Window* window, FGCBenumerator enumCallback,
407                        SFG_Enumerator* enumerator )
408 {
409     SFG_Window *child;
410
411     FREEGLUT_INTERNAL_ERROR_EXIT ( enumCallback && enumerator,
412                                    "Enumerator or callback missing from subwindow enumerator call",
413                                    "fgEnumSubWindows" );
414     FREEGLUT_INTERNAL_ERROR_EXIT_IF_NOT_INITIALISED ( "Window Enumeration" );
415
416     for( child = ( SFG_Window * )window->Children.First;
417          child;
418          child = ( SFG_Window * )child->Node.Next )
419     {
420         enumCallback( child, enumerator );
421         if( enumerator->found )
422             return;
423     }
424 }
425
426 /*
427  * A static helper function to look for a window given its handle
428  */
429 static void fghcbWindowByHandle( SFG_Window *window,
430                                  SFG_Enumerator *enumerator )
431 {
432     if ( enumerator->found )
433         return;
434
435     /* Check the window's handle. Hope this works. Looks ugly. That's for sure. */
436     if( window->Window.Handle == (SFG_WindowHandleType) (enumerator->data) )
437     {
438         enumerator->found = GL_TRUE;
439         enumerator->data = window;
440
441         return;
442     }
443
444     /* Otherwise, check this window's children */
445     fgEnumSubWindows( window, fghcbWindowByHandle, enumerator );
446 }
447
448 /*
449  * fgWindowByHandle returns a (SFG_Window *) value pointing to the
450  * first window in the queue matching the specified window handle.
451  * The function is defined in freeglut_structure.c file.
452  */
453 SFG_Window* fgWindowByHandle ( SFG_WindowHandleType hWindow )
454 {
455     SFG_Enumerator enumerator;
456
457     /* This is easy and makes use of the windows enumeration defined above */
458     enumerator.found = GL_FALSE;
459     enumerator.data = (void *)hWindow;
460     fgEnumWindows( fghcbWindowByHandle, &enumerator );
461
462     if( enumerator.found )
463         return( SFG_Window *) enumerator.data;
464     return NULL;
465 }
466
467 /*
468  * A static helper function to look for a window given its ID
469  */
470 static void fghcbWindowByID( SFG_Window *window, SFG_Enumerator *enumerator )
471 {
472     /* Make sure we do not overwrite our precious results... */
473     if( enumerator->found )
474         return;
475
476     /* Check the window's handle. Hope this works. Looks ugly. That's for sure. */
477     if( window->ID == *( int *)(enumerator->data) )
478     {
479         enumerator->found = GL_TRUE;
480         enumerator->data = window;
481
482         return;
483     }
484
485     /* Otherwise, check this window's children */
486     fgEnumSubWindows( window, fghcbWindowByID, enumerator );
487 }
488
489 /*
490  * This function is similiar to the previous one, except it is
491  * looking for a specified (sub)window identifier. The function
492  * is defined in freeglut_structure.c file.
493  */
494 SFG_Window* fgWindowByID( int windowID )
495 {
496     SFG_Enumerator enumerator;
497
498     /* Uses a method very similiar for fgWindowByHandle... */
499     enumerator.found = GL_FALSE;
500     enumerator.data = ( void * )&windowID;
501     fgEnumWindows( fghcbWindowByID, &enumerator );
502     if( enumerator.found )
503         return ( SFG_Window * )enumerator.data;
504     return NULL;
505 }
506
507 /*
508  * Looks up a menu given its ID. This is easier that fgWindowByXXX
509  * as all menus are placed in one doubly linked list...
510  */
511 SFG_Menu* fgMenuByID( int menuID )
512 {
513     SFG_Menu *menu = NULL;
514
515     /* It's enough to check all entries in fgStructure.Menus... */
516     for( menu = (SFG_Menu *)fgStructure.Menus.First;
517          menu;
518          menu = (SFG_Menu *)menu->Node.Next )
519         if( menu->ID == menuID )
520             return menu;
521     return NULL;
522 }
523
524 /*
525  * List functions...
526  */
527 void fgListInit(SFG_List *list)
528 {
529     list->First = NULL;
530     list->Last = NULL;
531 }
532
533 void fgListAppend(SFG_List *list, SFG_Node *node)
534 {
535     if ( list->Last )
536     {
537         SFG_Node *ln = (SFG_Node *) list->Last;
538         ln->Next = node;
539         node->Prev = ln;
540     }
541     else
542     {
543         node->Prev = NULL;
544         list->First = node;
545     }
546
547     node->Next = NULL;
548     list->Last = node;
549 }
550
551 void fgListRemove(SFG_List *list, SFG_Node *node)
552 {
553     if( node->Next )
554         ( ( SFG_Node * )node->Next )->Prev = node->Prev;
555     if( node->Prev )
556         ( ( SFG_Node * )node->Prev )->Next = node->Next;
557     if( ( ( SFG_Node * )list->First ) == node )
558         list->First = node->Next;
559     if( ( ( SFG_Node * )list->Last ) == node )
560         list->Last = node->Prev;
561 }
562
563 int fgListLength(SFG_List *list)
564 {
565     SFG_Node *node;
566     int length = 0;
567
568     for( node =( SFG_Node * )list->First;
569          node;
570          node = ( SFG_Node * )node->Next )
571         ++length;
572
573     return length;
574 }
575
576
577 void fgListInsert(SFG_List *list, SFG_Node *next, SFG_Node *node)
578 {
579     SFG_Node *prev;
580
581     if( (node->Next = next) )
582     {
583         prev = next->Prev;
584         next->Prev = node;
585     }
586     else
587     {
588         prev = list->Last;
589         list->Last = node;
590     }
591
592     if( (node->Prev = prev) )
593         prev->Next = node;
594     else
595         list->First = node;
596 }
597
598 /*** END OF FILE ***/