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