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