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