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