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