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