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