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