Changed an "#elif TARGET_HOST_UNIX_X11" to "#else", since this simply
[freeglut] / src / freeglut_menu.c
1 /*
2  * freeglut_menu.c
3  *
4  * Pull-down menu creation and handling.
5  *
6  * Copyright (c) 1999-2000 Pawel W. Olszta. All Rights Reserved.
7  * Written by Pawel W. Olszta, <olszta@sourceforge.net>
8  * Creation date: Thu Dec 16 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-menu"
33
34 #include "../include/GL/freeglut.h"
35 #include "freeglut_internal.h"
36
37 /*
38  * TODO BEFORE THE STABLE RELEASE:
39  *
40  * Wouldn't 1.0 have been "the" stable release?  Now we are past 2.0,
41  * so this comment is probably just out of date.  (20031026; rkr)
42  *
43  * Think about the possibility of doing the menu on layers *or* using the
44  * native window system instead of OpenGL.
45  */
46
47 /* -- DEFINITIONS ---------------------------------------------------------- */
48
49 /*
50  * FREEGLUT_MENU_FONT can be any freeglut bitmapped font.
51  * (Stroked fonts would not be out of the question, but we'd need to alter
52  *  code, since GLUT (hence freeglut) does not quite unify stroked and
53  *  bitmapped font handling.)
54  * Old UNIX/X11 GLUT (BSD, UNIX, IRIX, LINUX, HPUX, ...) used a system
55  * font best approximated by an 18-pixel HELVETICA, I think.  MS-WINDOWS
56  * GLUT used something closest to the 8x13 fixed-width font.  (Old
57  * GLUT apparently uses host-system menus rather than building its own.
58  * freeglut is building its own menus from scratch.)
59  *
60  * FREEGLUT_MENU_HEIGHT gives the height of ONE menu box.  This should be
61  * the distances between two adjacent menu entries.  It should scale
62  * automatically with the font choice, so you needn't alter it---unless you
63  * use a stroked font.
64  *
65  * FREEGLUT_MENU_BORDER says how many pixels to allow around the edge of a
66  * menu.  (It also seems to be the same as the number of pixels used as
67  * a border around *items* to separate them from neighbors.  John says
68  * that that wasn't the original intent...if not, perhaps we need another
69  * symbolic constant, FREEGLUT_MENU_ITEM_BORDER, or such.)
70  */
71 #if TARGET_HOST_WIN32
72 #define FREEGLUT_MENU_FONT    GLUT_BITMAP_8_BY_13
73 #else
74 #define FREEGLUT_MENU_FONT    GLUT_BITMAP_HELVETICA_18
75 #endif
76
77 #define FREEGLUT_MENU_HEIGHT  (glutBitmapHeight(FREEGLUT_MENU_FONT) + FREEGLUT_MENU_BORDER)
78 #define  FREEGLUT_MENU_BORDER   2
79
80
81 /*
82  * These variables should be moved into the freeglut global state, but for now,
83  * we'll put them here.  They are for rendering the freeglut menu items.
84  * The choices are fore- and background, with and without h for Highlighting.
85  * Old GLUT appeared to be system-dependant for its colors (sigh) so we are
86  * too.  These variables should be stuffed into global state and initialized
87  * via the glutInit*() system.
88  */
89
90 #if TARGET_HOST_WIN32
91 static float menu_pen_fore  [4] = {0.0f,  0.0f,  0.0f,  1.0f};
92 static float menu_pen_back  [4] = {0.85f, 0.85f, 0.85f, 1.0f};
93 static float menu_pen_hfore [4] = {1.0f,  1.0f,  1.0f,  1.0f};
94 static float menu_pen_hback [4] = {0.15f, 0.15f, 0.45f, 1.0f};
95 #else
96 static float menu_pen_fore  [4] = {0.0f,  0.0f,  0.0f,  1.0f};
97 static float menu_pen_back  [4] = {0.70f, 0.70f, 0.70f, 1.0f};
98 static float menu_pen_hfore [4] = {0.0f,  0.0f,  0.0f,  1.0f};
99 static float menu_pen_hback [4] = {1.0f,  1.0f,  1.0f,  1.0f};
100 #endif
101
102
103 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */
104
105 /*
106  * Private static function to find a menu entry by index
107  */
108 static SFG_MenuEntry *fghFindMenuEntry( SFG_Menu* menu, int index )
109 {
110     SFG_MenuEntry *entry;
111     int i = 1;
112
113     for( entry = (SFG_MenuEntry *)menu->Entries.First; entry; entry = (SFG_MenuEntry *)entry->Node.Next)
114     {
115         if (i == index)
116             break;
117         ++i;
118     }
119
120     return entry;
121 }
122
123 /*
124  * Private static function to check for the current menu/sub menu activity state
125  */
126 static GLboolean fghCheckMenuStatus( SFG_Window* window, SFG_Menu* menu )
127 {
128   SFG_MenuEntry* menuEntry;
129   int x, y;
130
131   /*
132    * First of all check any of the active sub menus...
133    */
134   for( menuEntry = (SFG_MenuEntry *)menu->Entries.First; menuEntry;
135        menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next )
136   {
137     /*
138      * Is that an active sub menu by any case?
139      */
140     if( menuEntry->SubMenu != NULL && menuEntry->IsActive == TRUE )
141     {
142       /*
143        * OK, have the sub-menu checked, too. If it returns TRUE, it will mean
144        * that it caught the mouse cursor and we do not need to regenerate
145        * the activity list, and so our parents do...
146        */
147       GLboolean return_status = fghCheckMenuStatus( window, menuEntry->SubMenu ) ;
148
149       /*
150        * Reactivate the submenu as the checkMenuStatus may have turned it off if the mouse
151        * is in its parent menu entry.
152        */
153       menuEntry->SubMenu->IsActive = TRUE ;
154       if ( return_status == TRUE )
155         return( TRUE );
156     }
157   }
158
159   /*
160    * That much about our sub menus, let's get to checking the current menu:
161    */
162   x = window->State.MouseX;
163   y = window->State.MouseY;
164
165   /*
166    * Mark all menu entries inactive...
167    */
168   for( menuEntry = (SFG_MenuEntry *)menu->Entries.First; menuEntry;
169        menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next )
170   {
171     menuEntry->IsActive = FALSE;
172   }
173
174
175   menu->IsActive = FALSE;
176
177   /*
178    * Check if the mouse cursor is contained within the current menu box
179    */
180   if ( ( x >= FREEGLUT_MENU_BORDER ) && ( x < menu->Width  - FREEGLUT_MENU_BORDER ) &&
181        ( y >= FREEGLUT_MENU_BORDER ) && ( y < menu->Height - FREEGLUT_MENU_BORDER ) &&
182        ( window == menu->Window ) )
183   {
184     /*
185      * Calculation of the highlighted menu item is easy enough now:
186      */
187     int menuID = ( y - FREEGLUT_MENU_BORDER ) / FREEGLUT_MENU_HEIGHT ;
188
189     /*
190      * The mouse cursor is somewhere over our box, check it out.
191      */
192     menuEntry = fghFindMenuEntry( menu, menuID + 1 );
193     assert( menuEntry != NULL );
194
195     /*
196      * Mark the menu as active...
197      */
198     menuEntry->IsActive = TRUE;
199     menuEntry->Ordinal = menuID;
200
201     /*
202      * If this is not the same as the last active menu entry, deactivate the previous entry.
203      * Specifically, if the previous active entry was a submenu then deactivate it.
204      */
205     if ( menu->ActiveEntry && ( menuEntry != menu->ActiveEntry ) )
206     {
207       if ( menu->ActiveEntry->SubMenu != NULL )
208         fgDeactivateSubMenu ( menu->ActiveEntry ) ;
209     }
210
211     menu->ActiveEntry = menuEntry ;
212
213     /*
214      * Don't forget about marking the current menu as active, too:
215      */
216
217     menu->IsActive = TRUE;
218
219     /*
220      * OKi, we have marked that entry as active, but it would be also
221      * nice to have its contents updated, in case it's a sub menu.
222      * Also, ignore the return value of the check function:
223      */
224     if( menuEntry->SubMenu != NULL )
225     {
226       if ( ! menuEntry->SubMenu->IsActive )
227       {
228         SFG_Window *current_window = fgStructure.Window ;
229
230         /*
231          * Set up the initial menu position now...
232          */
233
234         /*
235          * Mark the menu as active, so that it gets displayed:
236          */
237         menuEntry->SubMenu->IsActive = TRUE ;
238
239         /*
240          * Set up the initial submenu position now:
241          */
242         menuEntry->SubMenu->X = menu->X + menu->Width ;
243         menuEntry->SubMenu->Y = menu->Y + menuEntry->Ordinal * FREEGLUT_MENU_HEIGHT ;
244
245         if ( menuEntry->SubMenu->X + menuEntry->SubMenu->Width > glutGet (GLUT_SCREEN_WIDTH ) )
246         menuEntry->SubMenu->X = menu->X - menuEntry->SubMenu->Width;
247
248         if ( menuEntry->SubMenu->Y + menuEntry->SubMenu->Height > glutGet (GLUT_SCREEN_HEIGHT ) )
249         menuEntry->SubMenu->Y -= (menuEntry->SubMenu->Height -
250             FREEGLUT_MENU_HEIGHT - 2*FREEGLUT_MENU_BORDER);
251
252         fgSetWindow ( menuEntry->SubMenu->Window ) ;
253         glutPositionWindow ( menuEntry->SubMenu->X, menuEntry->SubMenu->Y ) ;
254         glutReshapeWindow ( menuEntry->SubMenu->Width, menuEntry->SubMenu->Height ) ;
255         glutPopWindow () ;
256         glutShowWindow () ;
257         menuEntry->SubMenu->Window->ActiveMenu = menuEntry->SubMenu ;
258         fgSetWindow ( current_window ) ;
259       }
260
261       /*
262        * ...then check the submenu's state:
263        */
264       fghCheckMenuStatus( window, menuEntry->SubMenu );
265
266       /*
267        * Even if the submenu turned up inactive, activate it because its parent entry is active
268        */
269       menuEntry->SubMenu->IsActive = TRUE ;
270     }
271
272     /*
273      * Report back that we have caught the menu cursor
274      */
275     return( TRUE );
276   }
277
278   /*
279    * Looks like the menu cursor is somewhere else...
280    */
281   return( FALSE );
282 }
283
284 /*
285  * Displays a menu box and all of its submenus (if they are active)
286  */
287 static void fghDisplayMenuBox( SFG_Menu* menu )
288 {
289   SFG_MenuEntry *menuEntry;
290   int i;
291   int border = FREEGLUT_MENU_BORDER ;
292
293   /*
294    * Have the menu box drawn first. The +- values are
295    * here just to make it more nice-looking...
296    */
297   glColor4f( 1.0f, 1.0f, 1.0f, 1.0f ); /* a non-black dark version of the below. */
298   glBegin( GL_QUAD_STRIP );
299     glVertex2i( menu->Width       , 0                  );
300     glVertex2i( menu->Width-border,              border);
301     glVertex2i( 0                 , 0                  );
302     glVertex2i(             border,              border);
303     glVertex2i( 0                 , menu->Height       );
304     glVertex2i(             border, menu->Height-border);
305   glEnd();
306
307   glColor4f( 0.5f, 0.5f, 0.5f, 1.0f ); /* a non-black dark version of the below. */
308   glBegin( GL_QUAD_STRIP );
309     glVertex2i( 0                 , menu->Height       );
310     glVertex2i(             border, menu->Height-border);
311     glVertex2i( menu->Width       , menu->Height       );
312     glVertex2i( menu->Width-border, menu->Height-border);
313     glVertex2i( menu->Width       , 0                  );
314     glVertex2i( menu->Width-border,              border);
315   glEnd();
316
317   glColor4fv ( menu_pen_back ) ;
318   glBegin( GL_QUADS );
319     glVertex2i(             border,              border);
320     glVertex2i( menu->Width-border,              border);
321     glVertex2i( menu->Width-border, menu->Height-border);
322     glVertex2i(             border, menu->Height-border);
323   glEnd();
324
325   /*
326    * Check if any of the submenus is currently active...
327    */
328   for( menuEntry = (SFG_MenuEntry *)menu->Entries.First; menuEntry;
329        menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next )
330   {
331     /*
332      * Has the menu been marked as active, maybe?
333      */
334     if( menuEntry->IsActive == TRUE )
335     {
336       /*
337        * That's truly right, and we need to have it highlighted.
338        * There is an assumption that mouse cursor didn't move
339        * since the last check of menu activity state:
340        */
341       int menuID = menuEntry->Ordinal;
342
343       /*
344        * So have the highlight drawn...
345        */
346       glColor4fv ( menu_pen_hback ) ;
347       glBegin( GL_QUADS );
348         glVertex2i(             FREEGLUT_MENU_BORDER, (menuID + 0)*FREEGLUT_MENU_HEIGHT + FREEGLUT_MENU_BORDER );
349         glVertex2i( menu->Width-FREEGLUT_MENU_BORDER, (menuID + 0)*FREEGLUT_MENU_HEIGHT + FREEGLUT_MENU_BORDER );
350         glVertex2i( menu->Width-FREEGLUT_MENU_BORDER, (menuID + 1)*FREEGLUT_MENU_HEIGHT + FREEGLUT_MENU_BORDER );
351         glVertex2i(             FREEGLUT_MENU_BORDER, (menuID + 1)*FREEGLUT_MENU_HEIGHT + FREEGLUT_MENU_BORDER );
352       glEnd();
353     }
354   }
355
356   /*
357    * Print the menu entries now...
358    */
359
360   glColor4fv ( menu_pen_fore );
361
362   for( menuEntry = (SFG_MenuEntry *)menu->Entries.First, i=0; menuEntry;
363        menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next, ++i )
364   {
365     /*
366      * If the menu entry is active, set the color to white
367      */
368     if ( menuEntry->IsActive )
369       glColor4fv ( menu_pen_hfore ) ;
370
371     /*
372      * Move the raster into position...
373      */
374     glRasterPos2i(
375         2 * FREEGLUT_MENU_BORDER,
376         (i + 1)*FREEGLUT_MENU_HEIGHT-(int)(FREEGLUT_MENU_HEIGHT*0.3 - FREEGLUT_MENU_BORDER ) /* Try to center the text - JCJ 31 July 2003*/
377     );
378
379     /*
380      * Have the label drawn, character after character:
381      */
382     glutBitmapString( FREEGLUT_MENU_FONT, menuEntry->Text);
383
384     /*
385      * If it's a submenu, draw a right arrow
386      */
387     if ( menuEntry->SubMenu != NULL )
388     {
389       int width = glutBitmapWidth ( FREEGLUT_MENU_FONT, '_' ) ;
390       int x_base = menu->Width - 2 - width;
391       int y_base = i*FREEGLUT_MENU_HEIGHT + FREEGLUT_MENU_BORDER;
392       glBegin( GL_TRIANGLES );
393       glVertex2i( x_base, y_base + 2*FREEGLUT_MENU_BORDER);
394       glVertex2i( menu->Width - 2, y_base + (FREEGLUT_MENU_HEIGHT + FREEGLUT_MENU_BORDER) / 2 );
395       glVertex2i( x_base, y_base + FREEGLUT_MENU_HEIGHT - FREEGLUT_MENU_BORDER);
396       glEnd( );
397     }
398
399     /*
400      * If the menu entry is active, reset the color
401      */
402     if ( menuEntry->IsActive )
403       glColor4fv ( menu_pen_fore ) ;
404   }
405
406   /*
407    * Now we are ready to check if any of our children needs to be redrawn:
408    */
409   for( menuEntry = (SFG_MenuEntry *)menu->Entries.First; menuEntry;
410        menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next )
411   {
412     /*
413      * Is that an active sub menu by any case?
414      */
415     if( menuEntry->SubMenu != NULL && menuEntry->IsActive == TRUE )
416     {
417       /*
418        * Yeah, indeed. Have it redrawn now:
419        */
420       fgSetWindow ( menuEntry->SubMenu->Window ) ;
421       fghDisplayMenuBox( menuEntry->SubMenu );
422       fgSetWindow ( menu->Window ) ;
423     }
424   }
425 }
426
427 /*
428  * Private static function to set the parent window of a submenu and all of its submenus
429  */
430 static void fghSetSubmenuParentWindow ( SFG_Window *window, SFG_Menu *menu )
431 {
432   SFG_MenuEntry *menuEntry ;
433
434   menu->ParentWindow = window ;
435
436   for ( menuEntry = (SFG_MenuEntry *)menu->Entries.First; menuEntry; menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next )
437   {
438     if ( menuEntry->SubMenu != NULL )
439       fghSetSubmenuParentWindow ( window, menuEntry->SubMenu ) ;
440   }
441 }
442
443
444 /*
445  * Displays the currently active menu for the current window
446  */
447 void fgDisplayMenu( void )
448 {
449     SFG_Window* window = fgStructure.Window;
450     SFG_Menu* menu = NULL;
451
452     /*
453      * Make sure there is a current window available
454      */
455     freeglut_assert_window;
456
457     /*
458      * Check if there is an active menu attached to this window...
459      */
460     menu = window->ActiveMenu;
461
462     /*
463      * Did we find an active menu?
464      */
465     freeglut_return_if_fail( menu != NULL );
466
467     fgSetWindow ( menu->Window ) ;
468
469     /*
470      * Prepare the OpenGL state to do the rendering first:
471      */
472     glPushAttrib( GL_DEPTH_BUFFER_BIT | GL_TEXTURE_BIT | GL_LIGHTING_BIT | GL_POLYGON_BIT );
473
474     glDisable( GL_DEPTH_TEST );
475     glDisable( GL_TEXTURE_2D );
476     glDisable( GL_LIGHTING   );
477     glDisable( GL_CULL_FACE  );
478
479     /*
480      * We'll use an orthogonal projection matrix to draw the menu:
481      */
482     glMatrixMode( GL_PROJECTION );
483     glPushMatrix();
484     glLoadIdentity();
485     glOrtho(
486          0, glutGet( GLUT_WINDOW_WIDTH  ),
487          glutGet( GLUT_WINDOW_HEIGHT ), 0,
488         -1, 1
489     );
490
491     /*
492      * Model-view matix gets reset to identity:
493      */
494     glMatrixMode( GL_MODELVIEW );
495     glPushMatrix();
496     glLoadIdentity();
497
498     /*
499      * First of all, have the exact menu status check:
500      */
501     fghCheckMenuStatus( window, menu );
502
503     /*
504      * The status has been updated and we're ready to have the menu drawn now:
505      */
506     fghDisplayMenuBox( menu );
507
508     /*
509      * Restore the old OpenGL settings now
510      */
511     glPopAttrib();
512
513     glMatrixMode( GL_PROJECTION );
514     glPopMatrix();
515     glMatrixMode( GL_MODELVIEW );
516     glPopMatrix();
517
518     glutSwapBuffers () ;
519
520     /*
521      * Restore the current window
522      */
523     fgSetWindow ( window ) ;
524 }
525
526 /*
527  * Activates a menu pointed by the function argument
528  */
529 void fgActivateMenu( SFG_Window* window, int button )
530 {
531   /*
532    * We'll be referencing this menu a lot, so remember its address:
533    */
534   SFG_Menu* menu = window->Menu[ button ];
535
536   /*
537    * Mark the menu as active, so that it gets displayed:
538    */
539   window->ActiveMenu = menu;
540   menu->IsActive = TRUE ;
541   fgState.ActiveMenus ++ ;
542
543   /*
544    * Set up the initial menu position now:
545    */
546
547   menu->X = window->State.MouseX + glutGet ( GLUT_WINDOW_X ) ;
548   menu->Y = window->State.MouseY + glutGet ( GLUT_WINDOW_Y ) ;
549
550   if ( menu->X + menu->Width > glutGet ( GLUT_SCREEN_WIDTH ) )
551     menu->X -=menu->Width ;
552
553   if ( menu->Y + menu->Height > glutGet ( GLUT_SCREEN_HEIGHT ) )
554     menu->Y -=menu->Height ;
555
556   fgSetWindow ( menu->Window ) ;
557   glutPositionWindow ( menu->X, menu->Y ) ;
558   glutReshapeWindow ( menu->Width, menu->Height ) ;
559   glutPopWindow () ;
560   glutShowWindow () ;
561   menu->Window->ActiveMenu = menu ;
562
563 }
564
565 /*
566  * Check whether an active menu absorbs a mouse click
567  */
568 GLboolean fgCheckActiveMenu ( SFG_Window *window, SFG_Menu *menu )
569 {
570   /*
571    * Near as I can tell, this is the active menu behaviour:
572    *  - Down-click any button outside the menu, menu active:  deactivate the menu
573    *  - Down-click any button inside the menu, menu active:  select the menu entry and deactivate the menu
574    *  - Up-click the menu button outside the menu, menu active:  nothing happens
575    *  - Up-click the menu button inside the menu, menu active:  select the menu entry and deactivate the menu
576    * Since menus can have submenus, we need to check this recursively.
577    */
578   return fghCheckMenuStatus ( window, menu ) ;
579 }
580
581 /*
582  * Function to check for menu entry selection on menu deactivation
583  */
584 void fgExecuteMenuCallback( SFG_Menu* menu )
585 {
586   SFG_MenuEntry *menuEntry;
587
588   /*
589    * First of all check any of the active sub menus...
590    */
591   for( menuEntry = (SFG_MenuEntry *)menu->Entries.First; menuEntry; menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next)
592   {
593     /*
594      * Is this menu entry active?
595      */
596     if( menuEntry->IsActive == TRUE )
597     {
598       /*
599        * If there is not a sub menu, execute the menu callback and return...
600        */
601       if( menuEntry->SubMenu == NULL )
602       {
603         /*
604          * ...certainly given that there is one...
605          */
606         if( menu->Callback != NULL )
607           menu->Callback( menuEntry->ID );
608
609         return;
610       }
611
612       /*
613        * Otherwise recurse into the submenu.
614        */
615       fgExecuteMenuCallback( menuEntry->SubMenu );
616
617       /*
618        * There is little sense in dwelling the search on
619        */
620       return;
621     }
622   }
623 }
624
625 /*
626  * Deactivates a menu pointed by the function argument.
627  */
628 void fgDeactivateMenu( SFG_Window *window )
629 {
630   SFG_Window *current_window = fgStructure.Window ;
631
632     /*
633      * Check if there is an active menu attached to this window...
634      */
635     SFG_Menu* menu = window->ActiveMenu;
636     SFG_MenuEntry *menuEntry ;
637
638     /*
639      * Did we find an active window?
640      */
641     freeglut_return_if_fail( menu != NULL );
642
643     /*
644      * Hide the present menu's window
645      */
646     fgSetWindow ( menu->Window ) ;
647     glutHideWindow () ;
648
649     /*
650      * Forget about having that menu active anymore, now:
651      */
652     menu->Window->ActiveMenu = NULL ;
653     menu->ParentWindow->ActiveMenu = NULL ;
654     menu->IsActive = FALSE ;
655
656     fgState.ActiveMenus -- ;
657
658     /*
659      * Hide all submenu windows, and the root menu's window.
660      */
661     for ( menuEntry = (SFG_MenuEntry *)menu->Entries.First; menuEntry;
662           menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next )
663     {
664       /*
665        * Is that an active submenu by any case?
666        */
667       if ( menuEntry->SubMenu != NULL )
668         fgDeactivateSubMenu ( menuEntry ) ;
669     }
670
671     fgSetWindow ( current_window ) ;
672 }
673
674 /*
675  * Deactivates a menu pointed by the function argument.
676  */
677 void fgDeactivateSubMenu( SFG_MenuEntry *menuEntry )
678 {
679   SFG_Window *current_window = fgStructure.Window ;
680   SFG_MenuEntry *subMenuIter ;
681     /*
682      * Hide the present menu's window
683      */
684     fgSetWindow ( menuEntry->SubMenu->Window ) ;
685     glutHideWindow () ;
686
687     /*
688      * Forget about having that menu active anymore, now:
689      */
690     menuEntry->SubMenu->Window->ActiveMenu = NULL ;
691     menuEntry->SubMenu->IsActive = FALSE ;
692
693     /*
694      * Hide all submenu windows, and the root menu's window.
695      */
696     for ( subMenuIter = (SFG_MenuEntry *)menuEntry->SubMenu->Entries.First; subMenuIter;
697           subMenuIter = (SFG_MenuEntry *)subMenuIter->Node.Next )
698     {
699       /*
700        * Is that an active submenu by any case?
701        */
702       if ( subMenuIter->SubMenu != NULL )
703         fgDeactivateSubMenu ( subMenuIter ) ;
704     }
705
706     fgSetWindow ( current_window ) ;
707 }
708
709 /*
710  * Recalculates current menu's box size
711  */
712 void fghCalculateMenuBoxSize( void )
713 {
714   SFG_MenuEntry* menuEntry;
715   int width = 0, height = 0;
716
717   /*
718    * Make sure there is a current menu set
719    */
720   freeglut_assert_ready; freeglut_return_if_fail( fgStructure.Menu != NULL );
721
722   /*
723    * The menu's box size depends on the menu entries:
724    */
725   for( menuEntry = (SFG_MenuEntry *)fgStructure.Menu->Entries.First; menuEntry;
726        menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next)
727   {
728     /*
729      * Update the menu entry's width value
730      */
731     menuEntry->Width = glutBitmapLength( FREEGLUT_MENU_FONT, menuEntry->Text );
732
733     /*
734      * If the entry is a submenu, then it needs to be wider to accomodate the arrow. JCJ 31 July 2003
735      */
736
737     if (menuEntry->SubMenu != NULL)
738        menuEntry->Width += glutBitmapLength( FREEGLUT_MENU_FONT, "_" );
739
740     /*
741      * Check if it's the biggest we've found
742      */
743     if( menuEntry->Width > width )
744       width = menuEntry->Width;
745
746     height += FREEGLUT_MENU_HEIGHT;
747   }
748
749   /*
750    * Store the menu's box size now:
751    */
752   fgStructure.Menu->Height = height + 2 * FREEGLUT_MENU_BORDER ; 
753   fgStructure.Menu->Width  = width  + 4 * FREEGLUT_MENU_BORDER ;
754 }
755
756
757 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
758
759 /*
760  * Creates a new menu object, adding it to the freeglut structure
761  */
762 int FGAPIENTRY glutCreateMenu( void (* callback)( int ) )
763 {
764     /*
765      * The menu object creation code resides in freeglut_structure.c
766      */
767     return( fgCreateMenu( callback )->ID );
768 }
769
770 /*
771  * Destroys a menu object, removing all references to it
772  */
773 void FGAPIENTRY glutDestroyMenu( int menuID )
774 {
775     SFG_Menu* menu = fgMenuByID( menuID );
776
777     freeglut_assert_ready; freeglut_return_if_fail( menu != NULL );
778
779     /*
780      * The menu object destruction code resides in freeglut_structure.c
781      */
782     fgDestroyMenu( menu );
783 }
784
785 /*
786  * Returns the ID number of the currently active menu
787  */
788 int FGAPIENTRY glutGetMenu( void )
789 {
790     freeglut_assert_ready;
791
792     /*
793      * Is there a current menu set?
794      */
795     if( fgStructure.Menu != NULL )
796     {
797         /*
798          * Yes, there is indeed...
799          */
800         return( fgStructure.Menu->ID );
801     }
802
803     /*
804      * No, there is no current menu at all
805      */
806     return( 0 );
807 }
808
809 /*
810  * Sets the current menu given its menu ID
811  */
812 void FGAPIENTRY glutSetMenu( int menuID )
813 {
814     SFG_Menu* menu = fgMenuByID( menuID );
815
816     freeglut_assert_ready; freeglut_return_if_fail( menu != NULL );
817
818     /*
819      * The current menu pointer is stored in fgStructure.Menu
820      */
821     fgStructure.Menu = menu;
822 }
823
824 /*
825  * Adds a menu entry to the bottom of the current menu
826  */
827 void FGAPIENTRY glutAddMenuEntry( const char* label, int value )
828 {
829     SFG_MenuEntry* menuEntry = (SFG_MenuEntry *)calloc( sizeof(SFG_MenuEntry), 1 );
830
831     /*
832      * Make sure there is a current menu set
833      */
834     freeglut_assert_ready; freeglut_return_if_fail( fgStructure.Menu != NULL );
835
836     /*
837      * Fill in the appropriate values...
838      */
839     menuEntry->Text = strdup( label );
840     menuEntry->ID   = value;
841
842     /*
843      * Have the new menu entry attached to the current menu
844      */
845     fgListAppend( &fgStructure.Menu->Entries, &menuEntry->Node );
846
847     /*
848      * Update the menu's dimensions now
849      */
850     fghCalculateMenuBoxSize();
851 }
852
853 /*
854  * Add a sub menu to the bottom of the current menu
855  */
856 void FGAPIENTRY glutAddSubMenu( const char* label, int subMenuID )
857 {
858   SFG_MenuEntry* menuEntry = (SFG_MenuEntry *)calloc( sizeof(SFG_MenuEntry), 1 );
859   SFG_Menu*      subMenu = fgMenuByID( subMenuID );
860
861   /*
862    * Make sure there is a current menu and the sub menu
863    * we want to attach actually exists...
864    */
865   freeglut_assert_ready; freeglut_return_if_fail( fgStructure.Menu != NULL );
866   freeglut_return_if_fail( subMenu != NULL );
867
868   /*
869    * Fill in the appropriate values
870    */
871   menuEntry->Text = strdup( label );
872   menuEntry->SubMenu = subMenu;
873   menuEntry->ID      = -1;
874
875   /*
876    * Make the submenu's parent window be the menu's parent window
877    */
878   fghSetSubmenuParentWindow ( fgStructure.Menu->ParentWindow, subMenu ) ;
879
880   /*
881    * Have the new menu entry attached to the current menu
882    */
883   fgListAppend( &fgStructure.Menu->Entries, &menuEntry->Node );
884
885   /*
886    * Update the menu's dimensions now
887    */
888   fghCalculateMenuBoxSize();
889 }
890
891 /*
892  * Changes the specified menu item in the current menu into a menu entry
893  */
894 void FGAPIENTRY glutChangeToMenuEntry( int item, const char* label, int value )
895 {
896     SFG_MenuEntry* menuEntry = NULL;
897
898     /*
899      * Make sure there is a current menu set...
900      */
901     freeglut_assert_ready; freeglut_return_if_fail( fgStructure.Menu != NULL );
902
903     /*
904      * Get n-th menu entry in the current menu, starting from one:
905      */
906     menuEntry = fghFindMenuEntry( fgStructure.Menu, item );
907
908     /*
909      * Make sure the menu entry exists
910      */
911     freeglut_return_if_fail( menuEntry != NULL );
912
913     /*
914      * We want it to become a normal menu entry, so:
915      */
916     if( menuEntry->Text != NULL )
917         free( menuEntry->Text );
918
919     menuEntry->Text = strdup( label );
920     menuEntry->ID      = value;
921     menuEntry->SubMenu = NULL;
922
923     /*
924      * Update the menu's dimensions now
925      */
926     fghCalculateMenuBoxSize();
927 }
928
929 /*
930  * Changes the specified menu item in the current menu into a sub-menu trigger.
931  */
932 void FGAPIENTRY glutChangeToSubMenu( int item, const char* label, int subMenuID )
933 {
934     SFG_Menu*      subMenu = fgMenuByID( subMenuID );
935     SFG_MenuEntry* menuEntry = NULL;
936
937     /*
938      * Make sure there is a current menu set and the sub menu exists...
939      */
940     freeglut_assert_ready; freeglut_return_if_fail( fgStructure.Menu != NULL );
941     freeglut_return_if_fail( subMenu != NULL );
942
943     /*
944      * Get n-th menu entry in the current menu, starting from one:
945      */
946     menuEntry = fghFindMenuEntry( fgStructure.Menu, item );
947
948     /*
949      * Make sure the menu entry exists
950      */
951     freeglut_return_if_fail( menuEntry != NULL );
952
953     /*
954      * We want it to become a sub menu entry, so:
955      */
956     if( menuEntry->Text != NULL )
957         free( menuEntry->Text );
958
959     menuEntry->Text = strdup( label );
960     menuEntry->SubMenu = subMenu;
961     menuEntry->ID      = -1;
962
963     /*
964      * Update the menu's dimensions now
965      */
966     fghCalculateMenuBoxSize();
967 }
968
969 /*
970  * Removes the specified menu item from the current menu
971  */
972 void FGAPIENTRY glutRemoveMenuItem( int item )
973 {
974     SFG_MenuEntry* menuEntry;
975
976     /*
977      * Make sure there is a current menu set
978      */
979     freeglut_assert_ready; freeglut_return_if_fail( fgStructure.Menu != NULL );
980
981     /*
982      * Get n-th menu entry in the current menu, starting from one:
983      */
984     menuEntry = fghFindMenuEntry( fgStructure.Menu, item );
985
986     /*
987      * Make sure the menu entry exists
988      */
989     freeglut_return_if_fail( menuEntry != NULL );
990
991     /*
992      * Removing a menu entry is quite simple...
993      */
994     fgListRemove( &fgStructure.Menu->Entries, &menuEntry->Node );
995
996     /*
997      * Free the entry label string, too
998      */
999     free( menuEntry->Text );
1000
1001     free( menuEntry );
1002
1003     /*
1004      * Update the menu's dimensions now
1005      */
1006     fghCalculateMenuBoxSize();
1007 }
1008
1009 /*
1010  * Attaches a menu to the current window
1011  */
1012 void FGAPIENTRY glutAttachMenu( int button )
1013 {
1014     freeglut_assert_ready;
1015
1016     /*
1017      * There must be a current window and a current menu set:
1018      */
1019     freeglut_return_if_fail( fgStructure.Window != NULL || fgStructure.Menu != NULL );
1020
1021     /*
1022      * Make sure the button value is valid (0, 1 or 2, see freeglut.h)
1023      */
1024     freeglut_return_if_fail( button == GLUT_LEFT_BUTTON || button == GLUT_MIDDLE_BUTTON || button == GLUT_RIGHT_BUTTON );
1025
1026     /*
1027      * It is safe now to attach the menu
1028      */
1029     fgStructure.Window->Menu[ button ] = fgStructure.Menu;
1030
1031     /*
1032      * Make the parent window of the menu (and all submenus) the current window
1033      */
1034     fghSetSubmenuParentWindow ( fgStructure.Window, fgStructure.Menu ) ;
1035 }
1036
1037 /*
1038  * Detaches a menu from the current window
1039  */
1040 void FGAPIENTRY glutDetachMenu( int button )
1041 {
1042     freeglut_assert_ready;
1043
1044     /*
1045      * There must be a current window set:
1046      */
1047     freeglut_return_if_fail( fgStructure.Window != NULL );
1048
1049     /*
1050      * Make sure the button value is valid (0, 1 or 2, see freeglut.h)
1051      */
1052     freeglut_return_if_fail( button != 0 && button != 1 && button != 2 );
1053
1054     /*
1055      * It is safe now to detach the menu
1056      */
1057     fgStructure.Window->Menu[ button ] = NULL;
1058 }
1059
1060 /*
1061  * A.Donev: Set and retrieve the menu's user data
1062  */
1063 void* FGAPIENTRY glutGetMenuData( void )
1064 {
1065    return(fgStructure.Menu->UserData);
1066 }
1067
1068 void FGAPIENTRY glutSetMenuData(void* data)
1069 {
1070   fgStructure.Menu->UserData=data;
1071 }
1072
1073 /*** END OF FILE ***/