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