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