Update from John: Includes mouse button, mouse motion, and mouse wheel
[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 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31
32 #include "../include/GL/freeglut.h"
33 #include "freeglut_internal.h"
34
35
36 /* -- GLOBAL EXPORTS ------------------------------------------------------- */
37
38 /*
39  * The SFG_Structure container holds information about windows and menus
40  * created between glutInit() and glutMainLoop() return.
41  */
42
43 SFG_Structure fgStructure = { { NULL, NULL },  /* The list of windows       */
44                               { NULL, NULL },  /* The list of menus         */
45                               NULL,            /* The current window        */
46                               NULL,            /* The current menu          */
47                               NULL,            /* The menu OpenGL context   */
48                               NULL,            /* The game mode window      */
49                               0,               /* The current new window ID */
50                               0 };             /* The current new menu ID   */
51
52
53 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */
54
55 /*
56  * This private function creates, opens and adds to the hierarchy
57  * a freeglut window complete with OpenGL context and stuff...
58  *
59  * If parent is set to NULL, the window created will be a topmost one.
60  */
61 SFG_Window* fgCreateWindow( SFG_Window* parent, const char* title,
62                             int x, int y, int w, int h, GLboolean gameMode )
63 {
64     /*
65      * Have the window object created
66      */
67     SFG_Window *window = (SFG_Window *)calloc( sizeof(SFG_Window), 1 );
68     int fakeArgc = 0;
69
70     /*
71      * If the freeglut internals haven't been initialized yet,
72      * do it now. Hack's idea courtesy of Chris Purnell...
73      */
74     if( !fgState.Time.Set )
75         glutInit( &fakeArgc, NULL );
76
77     /*
78      * Initialize the object properties
79      */
80     window->ID = ++fgStructure.WindowID;
81
82     fgListInit( &window->Children );
83     if( parent != NULL )
84     {
85         fgListAppend( &parent->Children, &window->Node );
86         window->Parent = parent;
87     }
88     else
89         fgListAppend( &fgStructure.Windows, &window->Node );
90
91     /*
92      * Set the default mouse cursor and reset the modifiers value
93      */
94     window->State.Cursor    = GLUT_CURSOR_INHERIT;
95     window->State.Modifiers = 0xffffffff;
96
97     window->IsMenu = fgState.BuildingAMenu ;
98
99     /*
100      * Open the window now. The fgOpenWindow() function is system
101      * dependant, and resides in freeglut_window.c. Uses fgState.
102      */
103     fgOpenWindow( window, title, x, y, w, h, gameMode,
104                   (parent != NULL) ? TRUE : FALSE );
105
106     return window;
107 }
108
109 /*
110  * This private function creates a menu and adds it to the menus list
111  */
112 SFG_Menu* fgCreateMenu( FGCBmenu menuCallback )
113 {
114     int x = 100, y = 100, w = 100, h = 100 ;
115     SFG_Window *current_window = fgStructure.Window ;
116
117     /*
118      * Have the menu object created
119      */
120     SFG_Menu* menu = (SFG_Menu *)calloc( sizeof(SFG_Menu), 1 );
121     int fakeArgc = 0;
122
123     /*
124      * If the freeglut internals haven't been initialized yet,
125      * do it now. Hack's idea courtesy of Chris Purnell...
126      */
127     if( !fgState.Time.Set )
128         glutInit( &fakeArgc, NULL );
129
130     menu->ParentWindow = fgStructure.Window ;
131
132     /*
133      * Create a window for the menu to reside in.  Set the
134      * global variable BuildingAMenu to true so we can ensure
135      * it is created without decorations.
136      */
137     fgState.BuildingAMenu = TRUE ;
138
139     fgCreateWindow ( NULL, NULL, x, y, w, h, FALSE ) ;
140     menu->Window = fgStructure.Window ;
141     glutDisplayFunc ( fgDisplayMenu ) ;
142
143     /*
144      * While BuildingAMenu is true, all windows built have no decorations.
145      * That's not a good default behavior, so let's set it false again.
146      */
147     fgState.BuildingAMenu = FALSE ;
148
149     glutHideWindow () ;  /* Hide the window for now */
150     fgSetWindow ( current_window ) ;
151
152     /*
153      * Initialize the object properties:
154      */
155     menu->ID       = ++fgStructure.MenuID;
156     menu->Callback = menuCallback;
157     menu->ActiveEntry = NULL ;
158
159     fgListInit( &menu->Entries );
160     fgListAppend( &fgStructure.Menus, &menu->Node );
161
162     /*
163      * Newly created menus implicitly become current ones
164      */
165     fgStructure.Menu = menu;
166
167     return menu;
168 }
169
170 /*
171  * Linked list of windows to destroy ... this is so we don't destroy a
172  * window from the middle of its callback.  Some C compilers take an
173  * extremely dim view of this.
174  */
175
176 static SFG_WindowList* WindowsToDestroy = (SFG_WindowList*)NULL ;
177
178 /*
179  * Function to add a window to the linked list of windows to destroy.
180  * Subwindows are automatically added because they hang from the window
181  * structure.
182  */
183 void fgAddToWindowDestroyList ( SFG_Window* window, GLboolean needToClose )
184 {
185   SFG_WindowList *new_list_entry =
186       (SFG_WindowList*)malloc ( sizeof(SFG_WindowList) ) ;
187   new_list_entry->window = window ;
188   new_list_entry->needToClose = needToClose ;
189   new_list_entry->next = WindowsToDestroy ;
190   WindowsToDestroy = new_list_entry ;
191
192   /*
193    * Check if the window is the current one...
194    */
195   if ( fgStructure.Window == window )
196     fgStructure.Window = NULL;
197
198   /*
199    * If the destroyed window has the highest window ID number, decrement
200    * the window ID number.
201    *
202    * XXX Do we REALLY want to *ever* recycle window IDs?  Integers are
203    * XXX plentiful, and clients may rely upon the implied promise in
204    * XXX the GLUT docs to not recycle these.  (I can't remember if it
205    * XXX is explicit.)
206    *
207    * XXX If we *do* want to do this, we should actually recompute the
208    * XXX highest window-ID; the new highest may not in fact be one less
209    * XXX than what we have just deleted.
210    */
211   if ( window->ID == fgStructure.WindowID )
212       fgStructure.WindowID-- ;
213
214   /*
215    * Check the execution state.  If this has been called from
216    * "glutDestroyWindow", a statement in that function will reset the
217    * "ExecState" after this function returns.
218    */
219   if ( fgState.ActionOnWindowClose != GLUT_ACTION_CONTINUE_EXECUTION )
220   {
221     /*
222      * Set the execution state flag to drop out of the main loop.
223      */
224     if ( fgState.ActionOnWindowClose == GLUT_ACTION_EXIT )
225       fgState.ExecState = GLUT_EXEC_STATE_STOP ;
226   }
227 }
228
229 /*
230  * Function to close down all the windows in the "WindowsToDestroy" list
231  */
232 void fgCloseWindows ()
233 {
234   SFG_WindowList *window_ptr = WindowsToDestroy ;
235   WindowsToDestroy = (SFG_WindowList*)NULL ;
236   /* In case the destroy callbacks cause more windows to be closed */
237
238   while ( window_ptr )
239   {
240     SFG_WindowList *next = window_ptr->next ;
241     fgDestroyWindow ( window_ptr->window, window_ptr->needToClose ) ;
242     free ( window_ptr ) ;
243     window_ptr = next ;
244
245     if ( !window_ptr )
246     {
247       window_ptr = WindowsToDestroy ;
248       WindowsToDestroy = (SFG_WindowList*)NULL ;
249     }
250   }
251 }
252
253 /*
254  * This function destroys a window and all of its subwindows. Actually,
255  * another function, defined in freeglut_window.c is called, but this is
256  * a whole different story...
257  */
258 void fgDestroyWindow( SFG_Window* window, GLboolean needToClose )
259 {
260     SFG_Window* subWindow;
261     int menu_index ;
262
263     assert( window != NULL );
264     freeglut_assert_ready;
265
266     while ( (subWindow = (SFG_Window *)window->Children.First) != NULL )
267         fgDestroyWindow( subWindow, needToClose );
268
269     if ( window->Callbacks.Destroy != NULL )
270     {
271       SFG_Window *activeWindow = fgStructure.Window ;
272       fgSetWindow ( window ) ;
273       window->Callbacks.Destroy () ;
274       fgSetWindow ( activeWindow ) ;
275     }
276
277     if ( window->Parent != NULL )
278         fgListRemove( &window->Parent->Children, &window->Node );
279     else
280         fgListRemove( &fgStructure.Windows, &window->Node );
281
282     if ( window->ActiveMenu != NULL )
283       fgDeactivateMenu ( window ) ;
284
285     for ( menu_index = 0; menu_index < 3; menu_index ++ )
286     {
287       if ( window->Menu[menu_index] != NULL )
288         window->Menu[menu_index]->ParentWindow = NULL ;
289     }
290
291     if( needToClose == TRUE )
292         fgCloseWindow( window );
293     free( window );
294     if ( fgStructure.Window == window )
295         fgStructure.Window = NULL ;
296 }
297
298 /*
299  * This is a helper static function that removes a menu (given its pointer)
300  * from any windows that can be accessed from a given parent...
301  */
302 static void fghRemoveMenuFromWindow( SFG_Window* window, SFG_Menu* menu )
303 {
304     SFG_Window *subWindow;
305     int i;
306
307     /*
308      * Check if the menu is attached to the current window,
309      * if so, have it detached (by overwriting with a NULL):
310      */
311     for( i=0; i<3; i++ )
312         if( window->Menu[ i ] == menu )
313             window->Menu[ i ] = NULL;
314
315     /*
316      * Call this function for all of the window's children recursively:
317      */
318     for( subWindow = (SFG_Window *)window->Children.First; subWindow;
319          subWindow = (SFG_Window *)subWindow->Node.Next)
320         fghRemoveMenuFromWindow( subWindow, menu );
321 }
322
323 /*
324  * This is a static helper function that removes menu references
325  * from another menu, given two pointers to them...
326  */
327 static void fghRemoveMenuFromMenu( SFG_Menu* from, SFG_Menu* menu )
328 {
329   SFG_MenuEntry *entry;
330
331   for( entry = (SFG_MenuEntry *)from->Entries.First;
332        entry;
333        entry = (SFG_MenuEntry *)entry->Node.Next )
334     if (entry->SubMenu == menu)
335       entry->SubMenu = NULL;
336 }
337
338 /*
339  * This function destroys a menu specified by the parameter. All menus
340  * and windows are updated to make sure no ill pointers hang around.
341  */
342 void fgDestroyMenu( SFG_Menu* menu )
343 {
344   SFG_Window *window;
345   SFG_Menu *from;
346   SFG_MenuEntry *entry;
347
348   assert( menu != NULL );
349   freeglut_assert_ready;
350
351   /*
352    * First of all, have all references to this menu removed from all windows:
353    */
354   for( window = (SFG_Window *)fgStructure.Windows.First;
355        window;
356        window = (SFG_Window *)window->Node.Next )
357     fghRemoveMenuFromWindow( window, menu );
358
359   /*
360    * Now proceed with removing menu entries that lead to this menu
361    */
362   for( from = (SFG_Menu *)fgStructure.Menus.First;
363        from;
364        from = (SFG_Menu *)from->Node.Next )
365     fghRemoveMenuFromMenu( from, menu );
366
367   /*
368    * If the programmer defined a destroy callback, call it
369    * A. Donev: But first make this the active menu
370    */    
371   if ( menu->Destroy != NULL )
372   {
373     SFG_Menu *activeMenu=fgStructure.Menu;
374     fgStructure.Menu = menu;    
375     menu->Destroy () ;  
376     fgStructure.Menu = activeMenu;     
377   }        
378
379   /*
380    * Now we are pretty sure the menu is not used anywhere
381    * and that we can remove all of its entries
382    */
383   while( (entry = (SFG_MenuEntry *)menu->Entries.First) != NULL )
384   {
385     fgListRemove(&menu->Entries, &entry->Node);
386
387     if( entry->Text )
388         free( entry->Text );
389     entry->Text = NULL;
390     
391     free( entry );
392     entry = NULL;
393   }
394
395   if ( fgStructure.Window == menu->Window )
396     fgSetWindow ( menu->ParentWindow ) ;
397   fgDestroyWindow ( menu->Window, TRUE ) ;
398   fgListRemove( &fgStructure.Menus, &menu->Node );
399   if( fgStructure.Menu == menu )
400     fgStructure.Menu = NULL;
401
402   free( menu );
403 }
404
405 /*
406  * This function should be called on glutInit(). It will prepare the internal
407  * structure of freeglut to be used in the application. The structure will be
408  * destroyed using fgDestroyStructure() on glutMainLoop() return. In that
409  * case further use of freeglut should be preceeded with a glutInit() call.
410  */
411 void fgCreateStructure( void )
412 {
413   /*
414    * We will be needing two lists: the first containing windows,
415    * and the second containing the user-defined menus.
416    * Also, no current window/menu is set, as none has been created yet.
417    */
418
419   fgListInit(&fgStructure.Windows);
420   fgListInit(&fgStructure.Menus);
421 }
422
423 /*
424  * This function is automatically called on glutMainLoop() return.
425  * It should deallocate and destroy all remnants of previous
426  * glutInit()-enforced structure initialization...
427  */
428 void fgDestroyStructure( void )
429 {
430   SFG_Window *window;
431   SFG_Menu *menu;
432
433   freeglut_assert_ready;
434
435   /*
436    * Make sure all windows and menus have been deallocated
437    */
438   while( (menu = (SFG_Menu *)fgStructure.Menus.First) != NULL )
439     fgDestroyMenu( menu );
440
441   while( (window = (SFG_Window *)fgStructure.Windows.First) != NULL )
442     fgDestroyWindow( window, TRUE );
443 }
444
445 /*
446  * Helper function to enumerate through all registered top-level windows
447  */
448 void fgEnumWindows( FGCBenumerator enumCallback, SFG_Enumerator* enumerator )
449 {
450   SFG_Window *window;
451
452   assert( (enumCallback != NULL) && (enumerator != NULL) );
453   freeglut_assert_ready;
454
455   /*
456    * Check every of the top-level windows
457    */
458   for( window = (SFG_Window *)fgStructure.Windows.First;
459        window;
460        window = (SFG_Window *)window->Node.Next )
461   {
462     enumCallback( window, enumerator );
463     if( enumerator->found == TRUE )
464       return;
465   }
466 }
467
468 /*
469  * Helper function to enumerate through all a window's subwindows
470  * (single level descent)
471  */
472 void fgEnumSubWindows( SFG_Window* window, FGCBenumerator enumCallback,
473                        SFG_Enumerator* enumerator )
474 {
475   SFG_Window *child;
476
477   assert( (enumCallback != NULL) && (enumerator != NULL) );
478   freeglut_assert_ready;
479
480   for( child = (SFG_Window *)window->Children.First;
481        child;
482        child = (SFG_Window *)child->Node.Next )
483   {
484     enumCallback( child, enumerator );
485     if( enumerator->found == TRUE )
486       return;
487   }
488 }
489
490 /*
491  * A static helper function to look for a window given its handle
492  */
493 static void fghcbWindowByHandle( SFG_Window *window,
494                                  SFG_Enumerator *enumerator )
495 {
496     freeglut_return_if_fail( enumerator->found == FALSE );
497
498 #if TARGET_HOST_UNIX_X11
499     #define WBHANDLE (Window)
500 #elif TARGET_HOST_WIN32
501     #define WBHANDLE (HWND)
502 #endif
503
504     /*
505      * Check the window's handle. Hope this works. Looks ugly. That's for sure.
506      */
507     if( window->Window.Handle == WBHANDLE (enumerator->data) )
508     {
509         enumerator->found = TRUE;
510         enumerator->data = window;
511
512         return;
513     }
514
515     /*
516      * Otherwise, check this window's children
517      */
518     fgEnumSubWindows( window, fghcbWindowByHandle, enumerator );
519
520 #undef WBHANDLE
521 }
522
523 /*
524  * fgWindowByHandle returns a (SFG_Window *) value pointing to the
525  * first window in the queue matching the specified window handle.
526  * The function is defined in freeglut_structure.c file.
527  */
528 SFG_Window* fgWindowByHandle
529 #if TARGET_HOST_UNIX_X11
530 ( Window hWindow )
531 #elif TARGET_HOST_WIN32
532 ( HWND hWindow )
533 #endif
534 {
535     SFG_Enumerator enumerator;
536
537     /*
538      * This is easy and makes use of the windows enumeration defined above
539      */
540     enumerator.found = FALSE;
541     enumerator.data = (void *)hWindow;
542     fgEnumWindows( fghcbWindowByHandle, &enumerator );
543
544     if( enumerator.found == TRUE )
545         return( SFG_Window *) enumerator.data;
546     return NULL;
547 }
548
549 /*
550  * A static helper function to look for a window given its ID
551  */
552 static void fghcbWindowByID( SFG_Window *window, SFG_Enumerator *enumerator )
553 {
554     /*
555      * Make sure we do not overwrite our precious results...
556      */
557     freeglut_return_if_fail( enumerator->found == FALSE );
558
559     /*
560      * Check the window's handle. Hope this works. Looks ugly. That's for sure.
561      */
562     if( window->ID == (int) (enumerator->data) ) /* XXX int/ptr conversion! */
563     {
564         enumerator->found = TRUE;
565         enumerator->data = window;
566
567         return;
568     }
569
570     /*
571      * Otherwise, check this window's children
572      */
573     fgEnumSubWindows( window, fghcbWindowByID, enumerator );
574 }
575
576 /*
577  * This function is similiar to the previous one, except it is
578  * looking for a specified (sub)window identifier. The function
579  * is defined in freeglut_structure.c file.
580  */
581 SFG_Window* fgWindowByID( int windowID )
582 {
583     SFG_Enumerator enumerator;
584
585     /*
586      * Uses a method very similiar for fgWindowByHandle...
587      */
588     enumerator.found = FALSE;
589     enumerator.data = (void *) windowID; /* XXX int/pointer conversion! */
590     fgEnumWindows( fghcbWindowByID, &enumerator );
591     if( enumerator.found == TRUE )
592         return( SFG_Window *) enumerator.data;
593     return NULL;
594 }
595
596 /*
597  * Looks up a menu given its ID. This is easier that fgWindowByXXX
598  * as all menus are placed in a single doubly linked list...
599  */
600 SFG_Menu* fgMenuByID( int menuID )
601 {
602     SFG_Menu *menu = NULL;
603
604     freeglut_assert_ready;
605
606     /*
607      * It's enough to check all entries in fgStructure.Menus...
608      */
609     for( menu = (SFG_Menu *)fgStructure.Menus.First;
610          menu;
611          menu = (SFG_Menu *)menu->Node.Next )
612         if( menu->ID == menuID )
613             return menu;
614     return NULL;
615 }
616
617 /*
618  * List functions...
619  */
620 void fgListInit(SFG_List *list)
621 {
622     list->First = NULL;
623     list->Last = NULL;
624 }
625
626 void fgListAppend(SFG_List *list, SFG_Node *node)
627 {
628     SFG_Node *ln;
629
630     if ( (ln = (SFG_Node *)list->Last) != NULL )
631     {
632         ln->Next = node;
633         node->Prev = ln;
634     }
635     else
636     {
637         node->Prev = NULL;
638         list->First = node;
639     }
640
641     node->Next = NULL;
642     list->Last = node;
643 }
644
645 void fgListRemove(SFG_List *list, SFG_Node *node)
646 {
647     SFG_Node *ln;
648
649     if ( (ln = (SFG_Node *)node->Next) != NULL )
650         ln->Prev = node->Prev;
651     if ( (ln = (SFG_Node *)node->Prev) != NULL )
652         ln->Next = node->Next;
653     if ( (ln = (SFG_Node *)list->First) == node )
654         list->First = node->Next;
655     if ( (ln = (SFG_Node *)list->Last) == node )
656         list->Last = node->Prev;
657 }
658
659 int fgListLength(SFG_List *list)
660 {
661     SFG_Node *node;
662     int length = 0;
663
664     for( node = (SFG_Node *)list->First; node; node = (SFG_Node *)node->Next )
665         ++length;
666
667     return length;
668 }
669
670 /*** END OF FILE ***/