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