4 * Pull-down menu creation and handling.
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
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:
17 * The above copyright notice and this permission notice shall be included
18 * in all copies or substantial portions of the Software.
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.
32 #include "../include/GL/freeglut.h"
33 #include "freeglut_internal.h"
35 /* -- DEFINITIONS ---------------------------------------------------------- */
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.)
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
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.)
60 #define FREEGLUT_MENU_FONT GLUT_BITMAP_8_BY_13
62 #define FREEGLUT_MENU_FONT GLUT_BITMAP_HELVETICA_18
65 #define FREEGLUT_MENU_HEIGHT (glutBitmapHeight(FREEGLUT_MENU_FONT) + FREEGLUT_MENU_BORDER)
66 #define FREEGLUT_MENU_BORDER 2
70 * These variables are for rendering the freeglut menu items.
72 * The choices are fore- and background, with and without h for Highlighting.
73 * Old GLUT appeared to be system-dependant for its colors (sigh) so we are
74 * too. These variables should be stuffed into global state and initialized
75 * via the glutInit*() system.
78 static float menu_pen_fore [4] = {0.0f, 0.0f, 0.0f, 1.0f};
79 static float menu_pen_back [4] = {0.85f, 0.85f, 0.85f, 1.0f};
80 static float menu_pen_hfore [4] = {1.0f, 1.0f, 1.0f, 1.0f};
81 static float menu_pen_hback [4] = {0.15f, 0.15f, 0.45f, 1.0f};
83 static float menu_pen_fore [4] = {0.0f, 0.0f, 0.0f, 1.0f};
84 static float menu_pen_back [4] = {0.70f, 0.70f, 0.70f, 1.0f};
85 static float menu_pen_hfore [4] = {0.0f, 0.0f, 0.0f, 1.0f};
86 static float menu_pen_hback [4] = {1.0f, 1.0f, 1.0f, 1.0f};
89 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */
92 * Private function to find a menu entry by index
94 static SFG_MenuEntry *fghFindMenuEntry( SFG_Menu* menu, int index )
99 for( entry = (SFG_MenuEntry *)menu->Entries.First; entry; entry = (SFG_MenuEntry *)entry->Node.Next)
110 * Private function to check for the current menu/sub menu activity state
112 static GLboolean fghCheckMenuStatus( SFG_Window* window, SFG_Menu* menu )
114 SFG_MenuEntry* menuEntry;
118 * First of all check any of the active sub menus...
120 for( menuEntry = (SFG_MenuEntry *)menu->Entries.First; menuEntry;
121 menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next )
123 if( menuEntry->SubMenu != NULL && menuEntry->IsActive == TRUE )
126 * OK, have the sub-menu checked, too. If it returns TRUE, it will mean
127 * that it caught the mouse cursor and we do not need to regenerate
128 * the activity list, and so our parents do...
130 GLboolean return_status = fghCheckMenuStatus( window, menuEntry->SubMenu ) ;
133 * Reactivate the submenu as the checkMenuStatus may have turned it off
134 * if the mouse is in its parent menu entry.
136 menuEntry->SubMenu->IsActive = TRUE ;
137 if ( return_status == TRUE )
143 * That much about our sub menus, let's get to checking the current menu:
145 x = window->State.MouseX;
146 y = window->State.MouseY;
148 for( menuEntry = (SFG_MenuEntry *)menu->Entries.First; menuEntry;
149 menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next )
151 menuEntry->IsActive = FALSE;
155 menu->IsActive = FALSE;
158 * Check if the mouse cursor is contained within the current menu box
160 if ( ( x >= FREEGLUT_MENU_BORDER ) &&
161 ( x < menu->Width - FREEGLUT_MENU_BORDER ) &&
162 ( y >= FREEGLUT_MENU_BORDER ) &&
163 ( y < menu->Height - FREEGLUT_MENU_BORDER ) &&
164 ( window == menu->Window ) )
166 int menuID = ( y - FREEGLUT_MENU_BORDER ) / FREEGLUT_MENU_HEIGHT ;
169 * The mouse cursor is somewhere over our box, check it out.
171 menuEntry = fghFindMenuEntry( menu, menuID + 1 );
172 assert( menuEntry != NULL );
174 menuEntry->IsActive = TRUE;
175 menuEntry->Ordinal = menuID;
178 * If this is not the same as the last active menu entry, deactivate the
179 * previous entry. Specifically, if the previous active entry was a
180 * submenu then deactivate it.
182 if ( menu->ActiveEntry && ( menuEntry != menu->ActiveEntry ) )
184 if ( menu->ActiveEntry->SubMenu != NULL )
185 fgDeactivateSubMenu ( menu->ActiveEntry ) ;
188 menu->ActiveEntry = menuEntry ;
189 menu->IsActive = TRUE;
192 * OKi, we have marked that entry as active, but it would be also
193 * nice to have its contents updated, in case it's a sub menu.
194 * Also, ignore the return value of the check function:
196 if( menuEntry->SubMenu != NULL )
198 if ( ! menuEntry->SubMenu->IsActive )
200 SFG_Window *current_window = fgStructure.Window ;
203 * Set up the initial menu position now...
206 menuEntry->SubMenu->IsActive = TRUE ;
209 * Set up the initial submenu position now:
211 menuEntry->SubMenu->X = menu->X + menu->Width ;
212 menuEntry->SubMenu->Y = menu->Y + menuEntry->Ordinal * FREEGLUT_MENU_HEIGHT ;
214 if ( menuEntry->SubMenu->X + menuEntry->SubMenu->Width > glutGet ( GLUT_SCREEN_WIDTH ) )
215 menuEntry->SubMenu->X = menu->X - menuEntry->SubMenu->Width ;
217 if ( menuEntry->SubMenu->Y + menuEntry->SubMenu->Height > glutGet ( GLUT_SCREEN_HEIGHT ) )
218 menuEntry->SubMenu->Y -= ( menuEntry->SubMenu->Height -
219 FREEGLUT_MENU_HEIGHT - 2 * FREEGLUT_MENU_BORDER ) ;
221 fgSetWindow ( menuEntry->SubMenu->Window ) ;
222 glutPositionWindow ( menuEntry->SubMenu->X, menuEntry->SubMenu->Y ) ;
223 glutReshapeWindow ( menuEntry->SubMenu->Width, menuEntry->SubMenu->Height ) ;
226 menuEntry->SubMenu->Window->ActiveMenu = menuEntry->SubMenu ;
227 fgSetWindow ( current_window ) ;
230 fghCheckMenuStatus( window, menuEntry->SubMenu );
233 * Activate it because its parent entry is active
235 menuEntry->SubMenu->IsActive = TRUE ;
239 * Report back that we have caught the menu cursor
245 * Looks like the menu cursor is somewhere else...
251 * Displays a menu box and all of its submenus (if they are active)
253 static void fghDisplayMenuBox( SFG_Menu* menu )
255 SFG_MenuEntry *menuEntry;
257 int border = FREEGLUT_MENU_BORDER ;
260 * Have the menu box drawn first. The +- values are
261 * here just to make it more nice-looking...
263 glColor4f( 1.0f, 1.0f, 1.0f, 1.0f ); /* a non-black dark version of the below. */
264 glBegin( GL_QUAD_STRIP );
265 glVertex2i( menu->Width , 0 );
266 glVertex2i( menu->Width-border, border);
268 glVertex2i( border, border);
269 glVertex2i( 0 , menu->Height );
270 glVertex2i( border, menu->Height-border);
273 glColor4f( 0.5f, 0.5f, 0.5f, 1.0f ); /* a non-black dark version of the below. */
274 glBegin( GL_QUAD_STRIP );
275 glVertex2i( 0 , menu->Height );
276 glVertex2i( border, menu->Height-border);
277 glVertex2i( menu->Width , menu->Height );
278 glVertex2i( menu->Width-border, menu->Height-border);
279 glVertex2i( menu->Width , 0 );
280 glVertex2i( menu->Width-border, border);
283 glColor4fv ( menu_pen_back ) ;
285 glVertex2i( border, border);
286 glVertex2i( menu->Width-border, border);
287 glVertex2i( menu->Width-border, menu->Height-border);
288 glVertex2i( border, menu->Height-border);
292 * Check if any of the submenus is currently active...
294 for( menuEntry = (SFG_MenuEntry *)menu->Entries.First; menuEntry;
295 menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next )
298 * Has the menu been marked as active, maybe?
300 if( menuEntry->IsActive == TRUE )
303 * That's truly right, and we need to have it highlighted.
304 * There is an assumption that mouse cursor didn't move
305 * since the last check of menu activity state:
307 int menuID = menuEntry->Ordinal;
310 * So have the highlight drawn...
312 glColor4fv ( menu_pen_hback ) ;
314 glVertex2i( FREEGLUT_MENU_BORDER, (menuID + 0)*FREEGLUT_MENU_HEIGHT + FREEGLUT_MENU_BORDER );
315 glVertex2i( menu->Width-FREEGLUT_MENU_BORDER, (menuID + 0)*FREEGLUT_MENU_HEIGHT + FREEGLUT_MENU_BORDER );
316 glVertex2i( menu->Width-FREEGLUT_MENU_BORDER, (menuID + 1)*FREEGLUT_MENU_HEIGHT + FREEGLUT_MENU_BORDER );
317 glVertex2i( FREEGLUT_MENU_BORDER, (menuID + 1)*FREEGLUT_MENU_HEIGHT + FREEGLUT_MENU_BORDER );
323 * Print the menu entries now...
326 glColor4fv ( menu_pen_fore );
328 for( menuEntry = (SFG_MenuEntry *)menu->Entries.First, i=0; menuEntry;
329 menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next, ++i )
332 * If the menu entry is active, set the color to white
334 if ( menuEntry->IsActive )
335 glColor4fv ( menu_pen_hfore ) ;
338 * Move the raster into position...
341 2 * FREEGLUT_MENU_BORDER,
342 (i + 1)*FREEGLUT_MENU_HEIGHT-(int)(FREEGLUT_MENU_HEIGHT*0.3 - FREEGLUT_MENU_BORDER ) /* Try to center the text - JCJ 31 July 2003*/
346 * Have the label drawn, character after character:
348 glutBitmapString( FREEGLUT_MENU_FONT, menuEntry->Text);
351 * If it's a submenu, draw a right arrow
353 if ( menuEntry->SubMenu != NULL )
355 int width = glutBitmapWidth ( FREEGLUT_MENU_FONT, '_' ) ;
356 int x_base = menu->Width - 2 - width;
357 int y_base = i*FREEGLUT_MENU_HEIGHT + FREEGLUT_MENU_BORDER;
358 glBegin( GL_TRIANGLES );
359 glVertex2i( x_base, y_base + 2*FREEGLUT_MENU_BORDER);
360 glVertex2i( menu->Width - 2, y_base + (FREEGLUT_MENU_HEIGHT + FREEGLUT_MENU_BORDER) / 2 );
361 glVertex2i( x_base, y_base + FREEGLUT_MENU_HEIGHT - FREEGLUT_MENU_BORDER);
366 * If the menu entry is active, reset the color
368 if ( menuEntry->IsActive )
369 glColor4fv ( menu_pen_fore ) ;
373 * Now we are ready to check if any of our children needs to be redrawn:
375 for( menuEntry = (SFG_MenuEntry *)menu->Entries.First; menuEntry;
376 menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next )
379 * Is that an active sub menu by any case?
381 if( menuEntry->SubMenu != NULL && menuEntry->IsActive == TRUE )
384 * Yeah, indeed. Have it redrawn now:
386 fgSetWindow ( menuEntry->SubMenu->Window ) ;
387 fghDisplayMenuBox( menuEntry->SubMenu );
388 fgSetWindow ( menu->Window ) ;
394 * Private static function to set the parent window of a submenu and all of its submenus
396 static void fghSetSubmenuParentWindow ( SFG_Window *window, SFG_Menu *menu )
398 SFG_MenuEntry *menuEntry ;
400 menu->ParentWindow = window ;
402 for ( menuEntry = (SFG_MenuEntry *)menu->Entries.First; menuEntry; menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next )
404 if ( menuEntry->SubMenu != NULL )
405 fghSetSubmenuParentWindow ( window, menuEntry->SubMenu ) ;
411 * Displays the currently active menu for the current window
413 void fgDisplayMenu( void )
415 SFG_Window* window = fgStructure.Window;
416 SFG_Menu* menu = NULL;
419 * Make sure there is a current window available
421 freeglut_assert_window;
424 * Check if there is an active menu attached to this window...
426 menu = window->ActiveMenu;
429 * Did we find an active menu?
431 freeglut_return_if_fail( menu != NULL );
433 fgSetWindow ( menu->Window ) ;
436 * Prepare the OpenGL state to do the rendering first:
438 glPushAttrib( GL_DEPTH_BUFFER_BIT | GL_TEXTURE_BIT | GL_LIGHTING_BIT | GL_POLYGON_BIT );
440 glDisable( GL_DEPTH_TEST );
441 glDisable( GL_TEXTURE_2D );
442 glDisable( GL_LIGHTING );
443 glDisable( GL_CULL_FACE );
446 * We'll use an orthogonal projection matrix to draw the menu:
448 glMatrixMode( GL_PROJECTION );
452 0, glutGet( GLUT_WINDOW_WIDTH ),
453 glutGet( GLUT_WINDOW_HEIGHT ), 0,
458 * Model-view matix gets reset to identity:
460 glMatrixMode( GL_MODELVIEW );
465 * First of all, have the exact menu status check:
467 fghCheckMenuStatus( window, menu );
470 * The status has been updated and we're ready to have the menu drawn now:
472 fghDisplayMenuBox( menu );
475 * Restore the old OpenGL settings now
479 glMatrixMode( GL_PROJECTION );
481 glMatrixMode( GL_MODELVIEW );
487 * Restore the current window
489 fgSetWindow ( window ) ;
493 * Activates a menu pointed by the function argument
495 void fgActivateMenu( SFG_Window* window, int button )
498 * We'll be referencing this menu a lot, so remember its address:
500 SFG_Menu* menu = window->Menu[ button ];
503 * Mark the menu as active, so that it gets displayed:
505 window->ActiveMenu = menu;
506 menu->IsActive = TRUE ;
507 fgState.ActiveMenus ++ ;
510 * Set up the initial menu position now:
513 menu->X = window->State.MouseX + glutGet ( GLUT_WINDOW_X ) ;
514 menu->Y = window->State.MouseY + glutGet ( GLUT_WINDOW_Y ) ;
516 if ( menu->X + menu->Width > glutGet ( GLUT_SCREEN_WIDTH ) )
517 menu->X -=menu->Width ;
519 if ( menu->Y + menu->Height > glutGet ( GLUT_SCREEN_HEIGHT ) )
520 menu->Y -=menu->Height ;
522 fgSetWindow ( menu->Window ) ;
523 glutPositionWindow ( menu->X, menu->Y ) ;
524 glutReshapeWindow ( menu->Width, menu->Height ) ;
527 menu->Window->ActiveMenu = menu ;
531 * Check whether an active menu absorbs a mouse click
533 GLboolean fgCheckActiveMenu ( SFG_Window *window, SFG_Menu *menu )
536 * Near as I can tell, this is the active menu behaviour:
537 * - Down-click any button outside the menu, menu active: deactivate the menu
538 * - Down-click any button inside the menu, menu active: select the menu entry and deactivate the menu
539 * - Up-click the menu button outside the menu, menu active: nothing happens
540 * - Up-click the menu button inside the menu, menu active: select the menu entry and deactivate the menu
541 * Since menus can have submenus, we need to check this recursively.
543 return fghCheckMenuStatus ( window, menu ) ;
547 * Function to check for menu entry selection on menu deactivation
549 void fgExecuteMenuCallback( SFG_Menu* menu )
551 SFG_MenuEntry *menuEntry;
554 * First of all check any of the active sub menus...
556 for( menuEntry = (SFG_MenuEntry *)menu->Entries.First; menuEntry; menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next)
559 * Is this menu entry active?
561 if( menuEntry->IsActive == TRUE )
564 * If there is not a sub menu, execute the menu callback and return...
566 if( menuEntry->SubMenu == NULL )
569 * ...certainly given that there is one...
571 if( menu->Callback != NULL )
572 menu->Callback( menuEntry->ID );
578 * Otherwise recurse into the submenu.
580 fgExecuteMenuCallback( menuEntry->SubMenu );
583 * There is little sense in dwelling the search on
591 * Deactivates a menu pointed by the function argument.
593 void fgDeactivateMenu( SFG_Window *window )
595 SFG_Window *current_window = fgStructure.Window ;
598 * Check if there is an active menu attached to this window...
600 SFG_Menu* menu = window->ActiveMenu;
601 SFG_MenuEntry *menuEntry ;
604 * Did we find an active window?
606 freeglut_return_if_fail( menu != NULL );
609 * Hide the present menu's window
611 fgSetWindow ( menu->Window ) ;
615 * Forget about having that menu active anymore, now:
617 menu->Window->ActiveMenu = NULL ;
618 menu->ParentWindow->ActiveMenu = NULL ;
619 menu->IsActive = FALSE ;
621 fgState.ActiveMenus -- ;
624 * Hide all submenu windows, and the root menu's window.
626 for ( menuEntry = (SFG_MenuEntry *)menu->Entries.First; menuEntry;
627 menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next )
630 * Is that an active submenu by any case?
632 if ( menuEntry->SubMenu != NULL )
633 fgDeactivateSubMenu ( menuEntry ) ;
636 fgSetWindow ( current_window ) ;
640 * Deactivates a menu pointed by the function argument.
642 void fgDeactivateSubMenu( SFG_MenuEntry *menuEntry )
644 SFG_Window *current_window = fgStructure.Window ;
645 SFG_MenuEntry *subMenuIter ;
647 * Hide the present menu's window
649 fgSetWindow ( menuEntry->SubMenu->Window ) ;
653 * Forget about having that menu active anymore, now:
655 menuEntry->SubMenu->Window->ActiveMenu = NULL ;
656 menuEntry->SubMenu->IsActive = FALSE ;
659 * Hide all submenu windows, and the root menu's window.
661 for ( subMenuIter = (SFG_MenuEntry *)menuEntry->SubMenu->Entries.First; subMenuIter;
662 subMenuIter = (SFG_MenuEntry *)subMenuIter->Node.Next )
665 * Is that an active submenu by any case?
667 if ( subMenuIter->SubMenu != NULL )
668 fgDeactivateSubMenu ( subMenuIter ) ;
671 fgSetWindow ( current_window ) ;
675 * Recalculates current menu's box size
677 void fghCalculateMenuBoxSize( void )
679 SFG_MenuEntry* menuEntry;
680 int width = 0, height = 0;
683 * Make sure there is a current menu set
685 freeglut_assert_ready; freeglut_return_if_fail( fgStructure.Menu != NULL );
688 * The menu's box size depends on the menu entries:
690 for( menuEntry = (SFG_MenuEntry *)fgStructure.Menu->Entries.First; menuEntry;
691 menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next)
694 * Update the menu entry's width value
696 menuEntry->Width = glutBitmapLength( FREEGLUT_MENU_FONT, menuEntry->Text );
699 * If the entry is a submenu, then it needs to be wider to accomodate the arrow. JCJ 31 July 2003
702 if (menuEntry->SubMenu != NULL)
703 menuEntry->Width += glutBitmapLength( FREEGLUT_MENU_FONT, "_" );
706 * Check if it's the biggest we've found
708 if( menuEntry->Width > width )
709 width = menuEntry->Width;
711 height += FREEGLUT_MENU_HEIGHT;
715 * Store the menu's box size now:
717 fgStructure.Menu->Height = height + 2 * FREEGLUT_MENU_BORDER ;
718 fgStructure.Menu->Width = width + 4 * FREEGLUT_MENU_BORDER ;
722 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
725 * Creates a new menu object, adding it to the freeglut structure
727 int FGAPIENTRY glutCreateMenu( void (* callback)( int ) )
730 * The menu object creation code resides in freeglut_structure.c
732 return( fgCreateMenu( callback )->ID );
736 * Destroys a menu object, removing all references to it
738 void FGAPIENTRY glutDestroyMenu( int menuID )
740 SFG_Menu* menu = fgMenuByID( menuID );
742 freeglut_assert_ready; freeglut_return_if_fail( menu != NULL );
745 * The menu object destruction code resides in freeglut_structure.c
747 fgDestroyMenu( menu );
751 * Returns the ID number of the currently active menu
753 int FGAPIENTRY glutGetMenu( void )
755 freeglut_assert_ready;
757 if( fgStructure.Menu != NULL )
758 return( fgStructure.Menu->ID );
764 * Sets the current menu given its menu ID
766 void FGAPIENTRY glutSetMenu( int menuID )
768 SFG_Menu* menu = fgMenuByID( menuID );
770 freeglut_assert_ready;
771 freeglut_return_if_fail( menu != NULL );
773 fgStructure.Menu = menu;
777 * Adds a menu entry to the bottom of the current menu
779 void FGAPIENTRY glutAddMenuEntry( const char* label, int value )
781 SFG_MenuEntry* menuEntry = (SFG_MenuEntry *)calloc( sizeof(SFG_MenuEntry), 1 );
783 freeglut_assert_ready;
784 freeglut_return_if_fail( fgStructure.Menu != NULL );
786 menuEntry->Text = strdup( label );
787 menuEntry->ID = value;
790 * Have the new menu entry attached to the current menu
792 fgListAppend( &fgStructure.Menu->Entries, &menuEntry->Node );
794 fghCalculateMenuBoxSize();
798 * Add a sub menu to the bottom of the current menu
800 void FGAPIENTRY glutAddSubMenu( const char* label, int subMenuID )
802 SFG_MenuEntry* menuEntry = (SFG_MenuEntry *)calloc( sizeof(SFG_MenuEntry), 1 );
803 SFG_Menu* subMenu = fgMenuByID( subMenuID );
805 freeglut_assert_ready;
806 freeglut_return_if_fail( fgStructure.Menu != NULL );
807 freeglut_return_if_fail( subMenu != NULL );
809 menuEntry->Text = strdup( label );
810 menuEntry->SubMenu = subMenu;
814 * Make the submenu's parent window be the menu's parent window
816 fghSetSubmenuParentWindow ( fgStructure.Menu->ParentWindow, subMenu ) ;
818 fgListAppend( &fgStructure.Menu->Entries, &menuEntry->Node );
819 fghCalculateMenuBoxSize();
823 * Changes the specified menu item in the current menu into a menu entry
825 void FGAPIENTRY glutChangeToMenuEntry( int item, const char* label, int value )
827 SFG_MenuEntry* menuEntry = NULL;
829 freeglut_assert_ready;
830 freeglut_return_if_fail( fgStructure.Menu );
833 * Get n-th menu entry in the current menu, starting from one:
835 menuEntry = fghFindMenuEntry( fgStructure.Menu, item );
837 freeglut_return_if_fail( menuEntry );
840 * We want it to become a normal menu entry, so:
842 if( menuEntry->Text != NULL )
843 free( menuEntry->Text );
845 menuEntry->Text = strdup( label );
846 menuEntry->ID = value;
847 menuEntry->SubMenu = NULL;
848 fghCalculateMenuBoxSize();
852 * Changes the specified menu item in the current menu into a sub-menu trigger.
854 void FGAPIENTRY glutChangeToSubMenu( int item, const char* label, int subMenuID )
856 SFG_Menu* subMenu = fgMenuByID( subMenuID );
857 SFG_MenuEntry* menuEntry = NULL;
859 freeglut_assert_ready;
860 freeglut_return_if_fail( fgStructure.Menu );
861 freeglut_return_if_fail( subMenu );
864 * Get n-th menu entry in the current menu, starting from one:
866 menuEntry = fghFindMenuEntry( fgStructure.Menu, item );
868 freeglut_return_if_fail( menuEntry );
871 * We want it to become a sub menu entry, so:
873 if( menuEntry->Text )
874 free( menuEntry->Text );
876 menuEntry->Text = strdup( label );
877 menuEntry->SubMenu = subMenu;
879 fghCalculateMenuBoxSize();
883 * Removes the specified menu item from the current menu
885 void FGAPIENTRY glutRemoveMenuItem( int item )
887 SFG_MenuEntry* menuEntry;
889 freeglut_assert_ready;
890 freeglut_return_if_fail( fgStructure.Menu );
893 * Get n-th menu entry in the current menu, starting from one:
895 menuEntry = fghFindMenuEntry( fgStructure.Menu, item );
897 freeglut_return_if_fail( menuEntry );
899 fgListRemove( &fgStructure.Menu->Entries, &menuEntry->Node );
900 if ( menuEntry->Text )
901 free( menuEntry->Text );
904 fghCalculateMenuBoxSize();
908 * Attaches a menu to the current window
910 void FGAPIENTRY glutAttachMenu( int button )
912 freeglut_assert_ready;
914 freeglut_return_if_fail( fgStructure.Window );
915 freeglut_return_if_fail( fgStructure.Menu );
917 freeglut_return_if_fail( button >= 0 );
918 freeglut_return_if_fail( button < FREEGLUT_MAX_MENUS );
920 fgStructure.Window->Menu[ button ] = fgStructure.Menu;
923 * Make the parent window of the menu (and all submenus) the current window
925 fghSetSubmenuParentWindow ( fgStructure.Window, fgStructure.Menu ) ;
929 * Detaches a menu from the current window
931 void FGAPIENTRY glutDetachMenu( int button )
933 freeglut_assert_ready;
935 freeglut_return_if_fail( fgStructure.Window );
936 freeglut_return_if_fail( fgStructure.Menu );
938 freeglut_return_if_fail( button >= 0 );
939 freeglut_return_if_fail( button < FREEGLUT_MAX_MENUS );
941 fgStructure.Window->Menu[ button ] = NULL;
945 * A.Donev: Set and retrieve the menu's user data
947 void* FGAPIENTRY glutGetMenuData( void )
949 return(fgStructure.Menu->UserData);
952 void FGAPIENTRY glutSetMenuData(void* data)
954 fgStructure.Menu->UserData=data;
957 /*** END OF FILE ***/