Some assertion changes / removals from Dr. John Fay
[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 || TARGET_HOST_WINCE
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 || TARGET_HOST_WINCE
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     /* First of all check any of the active sub menus... */
121     for( menuEntry = (SFG_MenuEntry *)menu->Entries.First;
122          menuEntry;
123          menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next )
124     {
125         if( menuEntry->SubMenu && menuEntry->IsActive )
126         {
127             /*
128              * OK, have the sub-menu checked, too. If it returns GL_TRUE, it
129              * will mean that it caught the mouse cursor and we do not need
130              * to regenerate the activity list, and so our parents do...
131              */
132             GLboolean return_status = fghCheckMenuStatus( window,
133                                                           menuEntry->SubMenu );
134
135             /*
136              * Reactivate the submenu as the checkMenuStatus may have turned
137              * it off if the mouse is in its parent menu entry.
138              */
139             menuEntry->SubMenu->IsActive = GL_TRUE;
140             if ( return_status )
141                 return GL_TRUE;
142         }
143     }
144
145     /* That much about our sub menus, let's get to checking the current menu: */
146     x = window->State.MouseX;
147     y = window->State.MouseY;
148
149     for( menuEntry = (SFG_MenuEntry *)menu->Entries.First;
150          menuEntry;
151          menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next )
152         menuEntry->IsActive = GL_FALSE;
153
154     menu->IsActive = GL_FALSE;
155
156     /* Check if the mouse cursor is contained within the current menu box */
157     if( ( x >= FREEGLUT_MENU_BORDER ) &&
158         ( x < menu->Width  - FREEGLUT_MENU_BORDER ) &&
159         ( y >= FREEGLUT_MENU_BORDER ) &&
160         ( y < menu->Height - FREEGLUT_MENU_BORDER ) &&
161         ( window == menu->Window ) )
162     {
163         int menuID = ( y - FREEGLUT_MENU_BORDER ) / FREEGLUT_MENU_HEIGHT;
164
165         /* The mouse cursor is somewhere over our box, check it out. */
166         menuEntry = fghFindMenuEntry( menu, menuID + 1 );
167         assert( menuEntry );
168
169         menuEntry->IsActive = GL_TRUE;
170         menuEntry->Ordinal = menuID;
171
172         /*
173          * If this is not the same as the last active menu entry, deactivate
174          * the previous entry.  Specifically, if the previous active entry
175          * was a submenu then deactivate it.
176          */
177         if( menu->ActiveEntry && ( menuEntry != menu->ActiveEntry ) )
178             if( menu->ActiveEntry->SubMenu )
179                 fgDeactivateSubMenu( menu->ActiveEntry );
180
181         menu->ActiveEntry = menuEntry;
182         menu->IsActive = GL_TRUE;
183
184         /*
185          * OKi, we have marked that entry as active, but it would be also
186          * nice to have its contents updated, in case it's a sub menu.
187          * Also, ignore the return value of the check function:
188          */
189         if( menuEntry->SubMenu )
190         {
191             if ( ! menuEntry->SubMenu->IsActive )
192             {
193                 SFG_Window *current_window = fgStructure.Window;
194
195                 /* Set up the initial menu position now... */
196                 menuEntry->SubMenu->IsActive = GL_TRUE;
197
198                 /* Set up the initial submenu position now: */
199                 menuEntry->SubMenu->X = menu->X + menu->Width;
200                 menuEntry->SubMenu->Y = menu->Y +
201                     menuEntry->Ordinal * FREEGLUT_MENU_HEIGHT;
202
203                 if( menuEntry->SubMenu->X + menuEntry->SubMenu->Width >
204                     glutGet( GLUT_SCREEN_WIDTH ) )
205                     menuEntry->SubMenu->X = menu->X -
206                         menuEntry->SubMenu->Width;
207
208                 if( menuEntry->SubMenu->Y + menuEntry->SubMenu->Height >
209                     glutGet( GLUT_SCREEN_HEIGHT ) )
210                     menuEntry->SubMenu->Y -= ( menuEntry->SubMenu->Height -
211                                                FREEGLUT_MENU_HEIGHT -
212                                                2 * FREEGLUT_MENU_BORDER );
213
214                 fgSetWindow( menuEntry->SubMenu->Window );
215                 glutPositionWindow( menuEntry->SubMenu->X,
216                                     menuEntry->SubMenu->Y );
217                 glutReshapeWindow( menuEntry->SubMenu->Width,
218                                    menuEntry->SubMenu->Height );
219                 glutPopWindow( );
220                 glutShowWindow( );
221                 menuEntry->SubMenu->Window->ActiveMenu = menuEntry->SubMenu;
222                 fgSetWindow( current_window );
223             }
224
225             fghCheckMenuStatus( window, menuEntry->SubMenu );
226
227             /* Activate it because its parent entry is active */
228             menuEntry->SubMenu->IsActive = GL_TRUE;
229         }
230
231         /* Report back that we have caught the menu cursor */
232         return GL_TRUE;
233     }
234
235     /* Looks like the menu cursor is somewhere else... */
236     return GL_FALSE;
237 }
238
239 /*
240  * Displays a menu box and all of its submenus (if they are active)
241  */
242 static void fghDisplayMenuBox( SFG_Menu* menu )
243 {
244     SFG_MenuEntry *menuEntry;
245     int i;
246     int border = FREEGLUT_MENU_BORDER;
247
248     /*
249      * Have the menu box drawn first. The +- values are
250      * here just to make it more nice-looking...
251      */
252     /* a non-black dark version of the below. */
253     glColor4f( 1.0f, 1.0f, 1.0f, 1.0f );
254     glBegin( GL_QUAD_STRIP );
255         glVertex2i( menu->Width         , 0                    );
256         glVertex2i( menu->Width - border,                border);
257         glVertex2i( 0                   , 0                    );
258         glVertex2i(               border,                border);
259         glVertex2i( 0                   , menu->Height         );
260         glVertex2i(               border, menu->Height - border);
261     glEnd( );
262
263     /* a non-black dark version of the below. */
264     glColor4f( 0.5f, 0.5f, 0.5f, 1.0f );
265     glBegin( GL_QUAD_STRIP );
266         glVertex2i( 0                   , menu->Height         );
267         glVertex2i(               border, menu->Height - border);
268         glVertex2i( menu->Width         , menu->Height         );
269         glVertex2i( menu->Width - border, menu->Height - border);
270         glVertex2i( menu->Width         , 0                    );
271         glVertex2i( menu->Width - border,                border);
272     glEnd( );
273
274     glColor4fv( menu_pen_back );
275     glBegin( GL_QUADS );
276         glVertex2i(               border,                border);
277         glVertex2i( menu->Width - border,                border);
278         glVertex2i( menu->Width - border, menu->Height - border);
279         glVertex2i(               border, menu->Height - border);
280     glEnd( );
281
282     /* Check if any of the submenus is currently active... */
283     for( menuEntry = (SFG_MenuEntry *)menu->Entries.First;
284          menuEntry;
285          menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next )
286     {
287         /* Has the menu been marked as active, maybe? */
288         if( menuEntry->IsActive )
289         {
290             /*
291              * That's truly right, and we need to have it highlighted.
292              * There is an assumption that mouse cursor didn't move
293              * since the last check of menu activity state:
294              */
295             int menuID = menuEntry->Ordinal;
296
297             /* So have the highlight drawn... */
298             glColor4fv( menu_pen_hback );
299             glBegin( GL_QUADS );
300                 glVertex2i( border,
301                             (menuID + 0)*FREEGLUT_MENU_HEIGHT + border );
302                 glVertex2i( menu->Width - border,
303                             (menuID + 0)*FREEGLUT_MENU_HEIGHT + border );
304                 glVertex2i( menu->Width - border,
305                             (menuID + 1)*FREEGLUT_MENU_HEIGHT + border );
306                 glVertex2i( border,
307                             (menuID + 1)*FREEGLUT_MENU_HEIGHT + border );
308             glEnd( );
309         }
310     }
311
312     /* Print the menu entries now... */
313
314     glColor4fv( menu_pen_fore );
315
316     for( menuEntry = (SFG_MenuEntry *)menu->Entries.First, i = 0;
317          menuEntry;
318          menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next, ++i )
319     {
320         /* If the menu entry is active, set the color to white */
321         if( menuEntry->IsActive )
322             glColor4fv( menu_pen_hfore );
323
324         /* Move the raster into position... */
325         /* Try to center the text - JCJ 31 July 2003*/
326         glRasterPos2i(
327             2 * border,
328             ( i + 1 )*FREEGLUT_MENU_HEIGHT -
329             ( int )( FREEGLUT_MENU_HEIGHT*0.3 - border )
330         );
331
332         /* Have the label drawn, character after character: */
333         glutBitmapString( FREEGLUT_MENU_FONT,
334                           (unsigned char *)menuEntry->Text);
335
336         /* If it's a submenu, draw a right arrow */
337         if( menuEntry->SubMenu )
338         {
339             int width = glutBitmapWidth( FREEGLUT_MENU_FONT, '_' );
340             int x_base = menu->Width - 2 - width;
341             int y_base = i*FREEGLUT_MENU_HEIGHT + border;
342             glBegin( GL_TRIANGLES );
343                 glVertex2i( x_base, y_base + 2*border);
344                 glVertex2i( menu->Width - 2, y_base +
345                             ( FREEGLUT_MENU_HEIGHT + border) / 2 );
346                 glVertex2i( x_base, y_base + FREEGLUT_MENU_HEIGHT - border );
347             glEnd( );
348         }
349
350         /* If the menu entry is active, reset the color */
351         if( menuEntry->IsActive )
352             glColor4fv( menu_pen_fore );
353     }
354
355     /* Now we are ready to check if any of our children needs to be redrawn: */
356     for( menuEntry = ( SFG_MenuEntry * )menu->Entries.First;
357          menuEntry;
358          menuEntry = ( SFG_MenuEntry * )menuEntry->Node.Next )
359     {
360         /* Is that an active sub menu by any case? */
361         if( menuEntry->SubMenu && menuEntry->IsActive )
362         {
363             /* Yeah, indeed. Have it redrawn now: */
364             fgSetWindow( menuEntry->SubMenu->Window );
365             fghDisplayMenuBox( menuEntry->SubMenu );
366             fgSetWindow( menu->Window );
367         }
368     }
369 }
370
371 /*
372  * Private static function to set the parent window of a submenu and all
373  * of its submenus
374  */
375 static void fghSetSubmenuParentWindow( SFG_Window *window, SFG_Menu *menu )
376 {
377     SFG_MenuEntry *menuEntry;
378
379     menu->ParentWindow = window;
380
381     for( menuEntry = ( SFG_MenuEntry * )menu->Entries.First;
382          menuEntry;
383          menuEntry = ( SFG_MenuEntry * )menuEntry->Node.Next )
384         if( menuEntry->SubMenu )
385             fghSetSubmenuParentWindow( window, menuEntry->SubMenu );
386 }
387
388
389 /*
390  * Displays the currently active menu for the current window
391  */
392 void fgDisplayMenu( void )
393 {
394     SFG_Window* window = fgStructure.Window;
395     SFG_Menu* menu = NULL;
396
397     freeglut_return_if_fail ( fgStructure.Window != NULL );
398
399     /* Check if there is an active menu attached to this window... */
400     menu = window->ActiveMenu;
401     freeglut_return_if_fail( menu );
402
403     fgSetWindow( menu->Window );
404
405     glPushAttrib( GL_DEPTH_BUFFER_BIT | GL_TEXTURE_BIT | GL_LIGHTING_BIT |
406                   GL_POLYGON_BIT );
407
408     glDisable( GL_DEPTH_TEST );
409     glDisable( GL_TEXTURE_2D );
410     glDisable( GL_LIGHTING   );
411     glDisable( GL_CULL_FACE  );
412
413     glMatrixMode( GL_PROJECTION );
414     glPushMatrix( );
415     glLoadIdentity( );
416     glOrtho(
417          0, glutGet( GLUT_WINDOW_WIDTH  ),
418          glutGet( GLUT_WINDOW_HEIGHT ), 0,
419         -1, 1
420     );
421
422     glMatrixMode( GL_MODELVIEW );
423     glPushMatrix( );
424     glLoadIdentity( );
425
426     fghCheckMenuStatus( window, menu );
427     fghDisplayMenuBox( menu );
428
429     glPopAttrib( );
430
431     glMatrixMode( GL_PROJECTION );
432     glPopMatrix( );
433     glMatrixMode( GL_MODELVIEW );
434     glPopMatrix( );
435
436     glutSwapBuffers( );
437
438     fgSetWindow ( window );
439 }
440
441 /*
442  * Activates a menu pointed by the function argument
443  */
444 void fgActivateMenu( SFG_Window* window, int button )
445 {
446     /* We'll be referencing this menu a lot, so remember its address: */
447     SFG_Menu* menu = window->Menu[ button ];
448
449     /* Mark the menu as active, so that it gets displayed: */
450     window->ActiveMenu = menu;
451     menu->IsActive = GL_TRUE;
452     fgState.ActiveMenus++;
453
454     /* Set up the initial menu position now: */
455     menu->X = window->State.MouseX + glutGet( GLUT_WINDOW_X );
456     menu->Y = window->State.MouseY + glutGet( GLUT_WINDOW_Y );
457
458     if( menu->X + menu->Width > glutGet ( GLUT_SCREEN_WIDTH ) )
459         menu->X -=menu->Width;
460
461     if( menu->Y + menu->Height > glutGet ( GLUT_SCREEN_HEIGHT ) )
462         menu->Y -=menu->Height;
463
464     fgSetWindow( menu->Window );
465     glutPositionWindow( menu->X, menu->Y );
466     glutReshapeWindow( menu->Width, menu->Height );
467     glutPopWindow( );
468     glutShowWindow( );
469     menu->Window->ActiveMenu = menu;
470 }
471
472 /*
473  * Check whether an active menu absorbs a mouse click
474  */
475 GLboolean fgCheckActiveMenu ( SFG_Window *window, SFG_Menu *menu )
476 {
477     /*
478      * Near as I can tell, this is the active menu behaviour:
479      *  - Down-click any button outside the menu, menu active:
480      *    deactivate the menu
481      *  - Down-click any button inside the menu, menu active:
482      *    select the menu entry and deactivate the menu
483      *  - Up-click the menu button outside the menu, menu active:
484      *    nothing happens
485      *  - Up-click the menu button inside the menu, menu active:
486      *    select the menu entry and deactivate the menu
487      * Since menus can have submenus, we need to check this recursively.
488      */
489   return fghCheckMenuStatus( window, menu );
490 }
491
492 /*
493  * Function to check for menu entry selection on menu deactivation
494  */
495 void fgExecuteMenuCallback( SFG_Menu* menu )
496 {
497     SFG_MenuEntry *menuEntry;
498
499     /* First of all check any of the active sub menus... */
500     for( menuEntry = (SFG_MenuEntry *)menu->Entries.First;
501          menuEntry;
502          menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next)
503     {
504         if( menuEntry->IsActive )
505         {
506             if( menuEntry->SubMenu )
507                 fgExecuteMenuCallback( menuEntry->SubMenu );
508             else
509                 if( menu->Callback )
510                     menu->Callback( menuEntry->ID );
511             return;
512         }
513     }
514 }
515
516 /*
517  * Deactivates a menu pointed by the function argument.
518  */
519 void fgDeactivateMenu( SFG_Window *window )
520 {
521     SFG_Window *current_window = fgStructure.Window;
522
523     /* Check if there is an active menu attached to this window... */
524     SFG_Menu* menu = window->ActiveMenu;
525     SFG_MenuEntry *menuEntry;
526
527     /* Did we find an active window? */
528     freeglut_return_if_fail( menu );
529
530     /* Hide the present menu's window */
531     fgSetWindow( menu->Window );
532     glutHideWindow( );
533
534     /* Forget about having that menu active anymore, now: */
535     menu->Window->ActiveMenu = NULL;
536     menu->ParentWindow->ActiveMenu = NULL;
537     menu->IsActive = GL_FALSE;
538
539     fgState.ActiveMenus--;
540
541     /* Hide all submenu windows, and the root menu's window. */
542     for ( menuEntry = ( SFG_MenuEntry * )menu->Entries.First;
543           menuEntry;
544           menuEntry = ( SFG_MenuEntry * )menuEntry->Node.Next )
545     {
546         /* Is that an active submenu by any case? */
547         if( menuEntry->SubMenu )
548             fgDeactivateSubMenu( menuEntry );
549     }
550
551     fgSetWindow( current_window );
552 }
553
554 /*
555  * Deactivates a menu pointed by the function argument.
556  */
557 void fgDeactivateSubMenu( SFG_MenuEntry *menuEntry )
558 {
559     SFG_Window *current_window = fgStructure.Window;
560     SFG_MenuEntry *subMenuIter;
561     /* Hide the present menu's window */
562     fgSetWindow( menuEntry->SubMenu->Window );
563     glutHideWindow( );
564
565     /* Forget about having that menu active anymore, now: */
566     menuEntry->SubMenu->Window->ActiveMenu = NULL;
567     menuEntry->SubMenu->IsActive = GL_FALSE;
568
569     /* Hide all submenu windows, and the root menu's window. */
570     for ( subMenuIter = (SFG_MenuEntry *)menuEntry->SubMenu->Entries.First;
571           subMenuIter;
572           subMenuIter = (SFG_MenuEntry *)subMenuIter->Node.Next )
573     {
574         /* Is that an active submenu by any case? */
575         if( subMenuIter->SubMenu )
576             fgDeactivateSubMenu( subMenuIter );
577     }
578
579     fgSetWindow( current_window );
580 }
581
582 /*
583  * Recalculates current menu's box size
584  */
585 void fghCalculateMenuBoxSize( void )
586 {
587     SFG_MenuEntry* menuEntry;
588     int width = 0, height = 0;
589
590     /* Make sure there is a current menu set */
591     freeglut_assert_ready;
592     freeglut_return_if_fail( fgStructure.Menu );
593
594     /* The menu's box size depends on the menu entries: */
595     for( menuEntry = ( SFG_MenuEntry * )fgStructure.Menu->Entries.First;
596          menuEntry;
597          menuEntry = ( SFG_MenuEntry * )menuEntry->Node.Next )
598     {
599         /* Update the menu entry's width value */
600         menuEntry->Width = glutBitmapLength(
601             FREEGLUT_MENU_FONT,
602             (unsigned char *)menuEntry->Text
603         );
604
605         /*
606          * If the entry is a submenu, then it needs to be wider to
607          * accomodate the arrow. JCJ 31 July 2003
608          */
609         if (menuEntry->SubMenu )
610             menuEntry->Width += glutBitmapLength(
611                 FREEGLUT_MENU_FONT,
612                 (unsigned char *)"_"
613             );
614
615         /* Check if it's the biggest we've found */
616         if( menuEntry->Width > width )
617             width = menuEntry->Width;
618
619         height += FREEGLUT_MENU_HEIGHT;
620     }
621
622     /* Store the menu's box size now: */
623     fgStructure.Menu->Height = height + 2 * FREEGLUT_MENU_BORDER;
624     fgStructure.Menu->Width  = width  + 4 * FREEGLUT_MENU_BORDER;
625 }
626
627
628 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
629
630 /*
631  * Creates a new menu object, adding it to the freeglut structure
632  */
633 int FGAPIENTRY glutCreateMenu( void(* callback)( int ) )
634 {
635     /* The menu object creation code resides in freeglut_structure.c */
636     return fgCreateMenu( callback )->ID;
637 }
638
639 /*
640  * Destroys a menu object, removing all references to it
641  */
642 void FGAPIENTRY glutDestroyMenu( int menuID )
643 {
644     SFG_Menu* menu = fgMenuByID( menuID );
645
646     freeglut_assert_ready;
647     freeglut_return_if_fail( menu );
648
649     /* The menu object destruction code resides in freeglut_structure.c */
650     fgDestroyMenu( menu );
651 }
652
653 /*
654  * Returns the ID number of the currently active menu
655  */
656 int FGAPIENTRY glutGetMenu( void )
657 {
658     freeglut_assert_ready;
659
660     if( fgStructure.Menu )
661         return fgStructure.Menu->ID;
662
663     return 0;
664 }
665
666 /*
667  * Sets the current menu given its menu ID
668  */
669 void FGAPIENTRY glutSetMenu( int menuID )
670 {
671     SFG_Menu* menu = fgMenuByID( menuID );
672
673     freeglut_assert_ready;
674     freeglut_return_if_fail( menu );
675
676     fgStructure.Menu = menu;
677 }
678
679 /*
680  * Adds a menu entry to the bottom of the current menu
681  */
682 void FGAPIENTRY glutAddMenuEntry( const char* label, int value )
683 {
684     SFG_MenuEntry* menuEntry =
685         (SFG_MenuEntry *)calloc( sizeof(SFG_MenuEntry), 1 );
686
687     freeglut_assert_ready;
688     freeglut_return_if_fail( fgStructure.Menu );
689
690     menuEntry->Text = strdup( label );
691     menuEntry->ID   = value;
692
693     /* Have the new menu entry attached to the current menu */
694     fgListAppend( &fgStructure.Menu->Entries, &menuEntry->Node );
695
696     fghCalculateMenuBoxSize( );
697 }
698
699 /*
700  * Add a sub menu to the bottom of the current menu
701  */
702 void FGAPIENTRY glutAddSubMenu( const char *label, int subMenuID )
703 {
704     SFG_MenuEntry *menuEntry =
705         ( SFG_MenuEntry * )calloc( sizeof( SFG_MenuEntry ), 1 );
706     SFG_Menu *subMenu = fgMenuByID( subMenuID );
707
708     freeglut_assert_ready;
709     freeglut_return_if_fail( fgStructure.Menu );
710     freeglut_return_if_fail( subMenu );
711
712     menuEntry->Text    = strdup( label );
713     menuEntry->SubMenu = subMenu;
714     menuEntry->ID      = -1;
715
716     /* Make the submenu's parent window be the menu's parent window */
717     fghSetSubmenuParentWindow( fgStructure.Menu->ParentWindow, subMenu );
718
719     fgListAppend( &fgStructure.Menu->Entries, &menuEntry->Node );
720     fghCalculateMenuBoxSize( );
721 }
722
723 /*
724  * Changes the specified menu item in the current menu into a menu entry
725  */
726 void FGAPIENTRY glutChangeToMenuEntry( int item, const char* label, int value )
727 {
728     SFG_MenuEntry* menuEntry = NULL;
729
730     freeglut_assert_ready;
731     freeglut_return_if_fail( fgStructure.Menu );
732
733     /* Get n-th menu entry in the current menu, starting from one: */
734     menuEntry = fghFindMenuEntry( fgStructure.Menu, item );
735
736     freeglut_return_if_fail( menuEntry );
737
738     /* We want it to become a normal menu entry, so: */
739     if( menuEntry->Text )
740         free( menuEntry->Text );
741
742     menuEntry->Text    = strdup( label );
743     menuEntry->ID      = value;
744     menuEntry->SubMenu = NULL;
745     fghCalculateMenuBoxSize( );
746 }
747
748 /*
749  * Changes the specified menu item in the current menu into a sub-menu trigger.
750  */
751 void FGAPIENTRY glutChangeToSubMenu( int item, const char* label,
752                                      int subMenuID )
753 {
754     SFG_Menu*      subMenu = fgMenuByID( subMenuID );
755     SFG_MenuEntry* menuEntry = NULL;
756
757     freeglut_assert_ready;
758     freeglut_return_if_fail( fgStructure.Menu );
759     freeglut_return_if_fail( subMenu );
760
761     /* Get n-th menu entry in the current menu, starting from one: */
762     menuEntry = fghFindMenuEntry( fgStructure.Menu, item );
763
764     freeglut_return_if_fail( menuEntry );
765
766     /* We want it to become a sub menu entry, so: */
767     if( menuEntry->Text )
768         free( menuEntry->Text );
769
770     menuEntry->Text    = strdup( label );
771     menuEntry->SubMenu = subMenu;
772     menuEntry->ID      = -1;
773     fghCalculateMenuBoxSize( );
774 }
775
776 /*
777  * Removes the specified menu item from the current menu
778  */
779 void FGAPIENTRY glutRemoveMenuItem( int item )
780 {
781     SFG_MenuEntry* menuEntry;
782
783     freeglut_assert_ready;
784     freeglut_return_if_fail( fgStructure.Menu );
785
786     /* Get n-th menu entry in the current menu, starting from one: */
787     menuEntry = fghFindMenuEntry( fgStructure.Menu, item );
788
789     freeglut_return_if_fail( menuEntry );
790
791     fgListRemove( &fgStructure.Menu->Entries, &menuEntry->Node );
792     if ( menuEntry->Text )
793       free( menuEntry->Text );
794
795     free( menuEntry );
796     fghCalculateMenuBoxSize( );
797 }
798
799 /*
800  * Attaches a menu to the current window
801  */
802 void FGAPIENTRY glutAttachMenu( int button )
803 {
804     freeglut_assert_ready;
805
806     freeglut_return_if_fail( fgStructure.Window );
807     freeglut_return_if_fail( fgStructure.Menu );
808
809     freeglut_return_if_fail( button >= 0 );
810     freeglut_return_if_fail( button < FREEGLUT_MAX_MENUS );
811
812     fgStructure.Window->Menu[ button ] = fgStructure.Menu;
813
814     /* Make the parent window of the menu (and all submenus) the current window */
815     fghSetSubmenuParentWindow( fgStructure.Window, fgStructure.Menu );
816 }
817
818 /*
819  * Detaches a menu from the current window
820  */
821 void FGAPIENTRY glutDetachMenu( int button )
822 {
823     freeglut_assert_ready;
824
825     freeglut_return_if_fail( fgStructure.Window );
826     freeglut_return_if_fail( fgStructure.Menu );
827
828     freeglut_return_if_fail( button >= 0 );
829     freeglut_return_if_fail( button < FREEGLUT_MAX_MENUS );
830
831     fgStructure.Window->Menu[ button ] = NULL;
832 }
833
834 /*
835  * A.Donev: Set and retrieve the menu's user data
836  */
837 void* FGAPIENTRY glutGetMenuData( void )
838 {
839     return fgStructure.Menu->UserData;
840 }
841
842 void FGAPIENTRY glutSetMenuData(void* data)
843 {
844     fgStructure.Menu->UserData=data;
845 }
846
847 /*** END OF FILE ***/