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