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