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 #define G_LOG_DOMAIN "freeglut-menu"
34 #include "../include/GL/freeglut.h"
35 #include "freeglut_internal.h"
38 * TODO BEFORE THE STABLE RELEASE:
40 * Wouldn't 1.0 have been "the" stable release? Now we are past 2.0,
41 * so this comment is probably just out of date. (20031026; rkr)
43 * Think about the possibility of doing the menu on layers *or* using the
44 * native window system instead of OpenGL.
47 /* -- DEFINITIONS ---------------------------------------------------------- */
50 * FREEGLUT_MENU_FONT can be any freeglut bitmapped font.
51 * (Stroked fonts would not be out of the question, but we'd need to alter
52 * code, since GLUT (hence freeglut) does not quite unify stroked and
53 * bitmapped font handling.)
54 * Old UNIX/X11 GLUT (BSD, UNIX, IRIX, LINUX, HPUX, ...) used a system
55 * font best approximated by an 18-pixel HELVETICA, I think. MS-WINDOWS
56 * GLUT used something closest to the 8x13 fixed-width font. (Old
57 * GLUT apparently uses host-system menus rather than building its own.
58 * freeglut is building its own menus from scratch.)
60 * FREEGLUT_MENU_HEIGHT gives the height of ONE menu box. This should be
61 * the distances between two adjacent menu entries. It should scale
62 * automatically with the font choice, so you needn't alter it---unless you
65 * FREEGLUT_MENU_BORDER says how many pixels to allow around the edge of a
66 * menu. (It also seems to be the same as the number of pixels used as
67 * a border around *items* to separate them from neighbors. John says
68 * that that wasn't the original intent...if not, perhaps we need another
69 * symbolic constant, FREEGLUT_MENU_ITEM_BORDER, or such.)
72 #define FREEGLUT_MENU_FONT GLUT_BITMAP_8_BY_13
74 #define FREEGLUT_MENU_FONT GLUT_BITMAP_HELVETICA_18
77 #define FREEGLUT_MENU_HEIGHT (glutBitmapHeight(FREEGLUT_MENU_FONT) + FREEGLUT_MENU_BORDER)
78 #define FREEGLUT_MENU_BORDER 2
82 * These variables are for rendering the freeglut menu items.
84 * The choices are fore- and background, with and without h for Highlighting.
85 * Old GLUT appeared to be system-dependant for its colors (sigh) so we are
86 * too. These variables should be stuffed into global state and initialized
87 * via the glutInit*() system.
90 static float menu_pen_fore [4] = {0.0f, 0.0f, 0.0f, 1.0f};
91 static float menu_pen_back [4] = {0.85f, 0.85f, 0.85f, 1.0f};
92 static float menu_pen_hfore [4] = {1.0f, 1.0f, 1.0f, 1.0f};
93 static float menu_pen_hback [4] = {0.15f, 0.15f, 0.45f, 1.0f};
95 static float menu_pen_fore [4] = {0.0f, 0.0f, 0.0f, 1.0f};
96 static float menu_pen_back [4] = {0.70f, 0.70f, 0.70f, 1.0f};
97 static float menu_pen_hfore [4] = {0.0f, 0.0f, 0.0f, 1.0f};
98 static float menu_pen_hback [4] = {1.0f, 1.0f, 1.0f, 1.0f};
102 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */
105 * Private function to find a menu entry by index
107 static SFG_MenuEntry *fghFindMenuEntry( SFG_Menu* menu, int index )
109 SFG_MenuEntry *entry;
112 for( entry = (SFG_MenuEntry *)menu->Entries.First; entry; entry = (SFG_MenuEntry *)entry->Node.Next)
123 * Private function to check for the current menu/sub menu activity state
125 static GLboolean fghCheckMenuStatus( SFG_Window* window, SFG_Menu* menu )
127 SFG_MenuEntry* menuEntry;
131 * First of all check any of the active sub menus...
133 for( menuEntry = (SFG_MenuEntry *)menu->Entries.First; menuEntry;
134 menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next )
136 if( menuEntry->SubMenu != NULL && menuEntry->IsActive == TRUE )
139 * OK, have the sub-menu checked, too. If it returns TRUE, it will mean
140 * that it caught the mouse cursor and we do not need to regenerate
141 * the activity list, and so our parents do...
143 GLboolean return_status = fghCheckMenuStatus( window, menuEntry->SubMenu ) ;
146 * Reactivate the submenu as the checkMenuStatus may have turned it off
147 * if the mouse is in its parent menu entry.
149 menuEntry->SubMenu->IsActive = TRUE ;
150 if ( return_status == TRUE )
156 * That much about our sub menus, let's get to checking the current menu:
158 x = window->State.MouseX;
159 y = window->State.MouseY;
161 for( menuEntry = (SFG_MenuEntry *)menu->Entries.First; menuEntry;
162 menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next )
164 menuEntry->IsActive = FALSE;
168 menu->IsActive = FALSE;
171 * Check if the mouse cursor is contained within the current menu box
173 if( ( x >= FREEGLUT_MENU_BORDER ) &&
174 ( x < menu->Width - FREEGLUT_MENU_BORDER ) &&
175 ( y >= FREEGLUT_MENU_BORDER ) &&
176 ( y < menu->Height - FREEGLUT_MENU_BORDER ) &&
177 ( window == menu->Window ) )
179 int menuID = ( y - FREEGLUT_MENU_BORDER ) / FREEGLUT_MENU_HEIGHT ;
182 * The mouse cursor is somewhere over our box, check it out.
184 menuEntry = fghFindMenuEntry( menu, menuID + 1 );
185 assert( menuEntry != NULL );
187 menuEntry->IsActive = TRUE;
188 menuEntry->Ordinal = menuID;
191 * If this is not the same as the last active menu entry, deactivate the
192 * previous entry. Specifically, if the previous active entry was a
193 * submenu then deactivate it.
195 if ( menu->ActiveEntry && ( menuEntry != menu->ActiveEntry ) )
197 if ( menu->ActiveEntry->SubMenu != NULL )
198 fgDeactivateSubMenu ( menu->ActiveEntry ) ;
201 menu->ActiveEntry = menuEntry ;
202 menu->IsActive = TRUE;
205 * OKi, we have marked that entry as active, but it would be also
206 * nice to have its contents updated, in case it's a sub menu.
207 * Also, ignore the return value of the check function:
209 if( menuEntry->SubMenu != NULL )
211 if ( ! menuEntry->SubMenu->IsActive )
213 SFG_Window *current_window = fgStructure.Window ;
216 * Set up the initial menu position now...
219 menuEntry->SubMenu->IsActive = TRUE ;
222 * Set up the initial submenu position now:
224 menuEntry->SubMenu->X = menu->X + menu->Width ;
225 menuEntry->SubMenu->Y = menu->Y + menuEntry->Ordinal * FREEGLUT_MENU_HEIGHT ;
227 if ( menuEntry->SubMenu->X + menuEntry->SubMenu->Width > glutGet (GLUT_SCREEN_WIDTH ) )
228 menuEntry->SubMenu->X = menu->X - menuEntry->SubMenu->Width;
230 if ( menuEntry->SubMenu->Y + menuEntry->SubMenu->Height > glutGet (GLUT_SCREEN_HEIGHT ) )
231 menuEntry->SubMenu->Y -= (menuEntry->SubMenu->Height -
232 FREEGLUT_MENU_HEIGHT - 2*FREEGLUT_MENU_BORDER);
234 fgSetWindow ( menuEntry->SubMenu->Window ) ;
235 glutPositionWindow ( menuEntry->SubMenu->X, menuEntry->SubMenu->Y ) ;
236 glutReshapeWindow ( menuEntry->SubMenu->Width, menuEntry->SubMenu->Height ) ;
239 menuEntry->SubMenu->Window->ActiveMenu = menuEntry->SubMenu ;
240 fgSetWindow ( current_window ) ;
243 fghCheckMenuStatus( window, menuEntry->SubMenu );
246 * Activate it because its parent entry is active
248 menuEntry->SubMenu->IsActive = TRUE ;
252 * Report back that we have caught the menu cursor
258 * Looks like the menu cursor is somewhere else...
264 * Displays a menu box and all of its submenus (if they are active)
266 static void fghDisplayMenuBox( SFG_Menu* menu )
268 SFG_MenuEntry *menuEntry;
270 int border = FREEGLUT_MENU_BORDER ;
273 * Have the menu box drawn first. The +- values are
274 * here just to make it more nice-looking...
276 glColor4f( 1.0f, 1.0f, 1.0f, 1.0f ); /* a non-black dark version of the below. */
277 glBegin( GL_QUAD_STRIP );
278 glVertex2i( menu->Width , 0 );
279 glVertex2i( menu->Width-border, border);
281 glVertex2i( border, border);
282 glVertex2i( 0 , menu->Height );
283 glVertex2i( border, menu->Height-border);
286 glColor4f( 0.5f, 0.5f, 0.5f, 1.0f ); /* a non-black dark version of the below. */
287 glBegin( GL_QUAD_STRIP );
288 glVertex2i( 0 , menu->Height );
289 glVertex2i( border, menu->Height-border);
290 glVertex2i( menu->Width , menu->Height );
291 glVertex2i( menu->Width-border, menu->Height-border);
292 glVertex2i( menu->Width , 0 );
293 glVertex2i( menu->Width-border, border);
296 glColor4fv ( menu_pen_back ) ;
298 glVertex2i( border, border);
299 glVertex2i( menu->Width-border, border);
300 glVertex2i( menu->Width-border, menu->Height-border);
301 glVertex2i( border, menu->Height-border);
305 * Check if any of the submenus is currently active...
307 for( menuEntry = (SFG_MenuEntry *)menu->Entries.First; menuEntry;
308 menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next )
311 * Has the menu been marked as active, maybe?
313 if( menuEntry->IsActive == TRUE )
316 * That's truly right, and we need to have it highlighted.
317 * There is an assumption that mouse cursor didn't move
318 * since the last check of menu activity state:
320 int menuID = menuEntry->Ordinal;
323 * So have the highlight drawn...
325 glColor4fv ( menu_pen_hback ) ;
327 glVertex2i( FREEGLUT_MENU_BORDER, (menuID + 0)*FREEGLUT_MENU_HEIGHT + FREEGLUT_MENU_BORDER );
328 glVertex2i( menu->Width-FREEGLUT_MENU_BORDER, (menuID + 0)*FREEGLUT_MENU_HEIGHT + FREEGLUT_MENU_BORDER );
329 glVertex2i( menu->Width-FREEGLUT_MENU_BORDER, (menuID + 1)*FREEGLUT_MENU_HEIGHT + FREEGLUT_MENU_BORDER );
330 glVertex2i( FREEGLUT_MENU_BORDER, (menuID + 1)*FREEGLUT_MENU_HEIGHT + FREEGLUT_MENU_BORDER );
336 * Print the menu entries now...
339 glColor4fv ( menu_pen_fore );
341 for( menuEntry = (SFG_MenuEntry *)menu->Entries.First, i=0; menuEntry;
342 menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next, ++i )
345 * If the menu entry is active, set the color to white
347 if ( menuEntry->IsActive )
348 glColor4fv ( menu_pen_hfore ) ;
351 * Move the raster into position...
354 2 * FREEGLUT_MENU_BORDER,
355 (i + 1)*FREEGLUT_MENU_HEIGHT-(int)(FREEGLUT_MENU_HEIGHT*0.3 - FREEGLUT_MENU_BORDER ) /* Try to center the text - JCJ 31 July 2003*/
359 * Have the label drawn, character after character:
361 glutBitmapString( FREEGLUT_MENU_FONT, menuEntry->Text);
364 * If it's a submenu, draw a right arrow
366 if ( menuEntry->SubMenu != NULL )
368 int width = glutBitmapWidth ( FREEGLUT_MENU_FONT, '_' ) ;
369 int x_base = menu->Width - 2 - width;
370 int y_base = i*FREEGLUT_MENU_HEIGHT + FREEGLUT_MENU_BORDER;
371 glBegin( GL_TRIANGLES );
372 glVertex2i( x_base, y_base + 2*FREEGLUT_MENU_BORDER);
373 glVertex2i( menu->Width - 2, y_base + (FREEGLUT_MENU_HEIGHT + FREEGLUT_MENU_BORDER) / 2 );
374 glVertex2i( x_base, y_base + FREEGLUT_MENU_HEIGHT - FREEGLUT_MENU_BORDER);
379 * If the menu entry is active, reset the color
381 if ( menuEntry->IsActive )
382 glColor4fv ( menu_pen_fore ) ;
386 * Now we are ready to check if any of our children needs to be redrawn:
388 for( menuEntry = (SFG_MenuEntry *)menu->Entries.First; menuEntry;
389 menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next )
392 * Is that an active sub menu by any case?
394 if( menuEntry->SubMenu != NULL && menuEntry->IsActive == TRUE )
397 * Yeah, indeed. Have it redrawn now:
399 fgSetWindow ( menuEntry->SubMenu->Window ) ;
400 fghDisplayMenuBox( menuEntry->SubMenu );
401 fgSetWindow ( menu->Window ) ;
407 * Private static function to set the parent window of a submenu and all of its submenus
409 static void fghSetSubmenuParentWindow ( SFG_Window *window, SFG_Menu *menu )
411 SFG_MenuEntry *menuEntry ;
413 menu->ParentWindow = window ;
415 for ( menuEntry = (SFG_MenuEntry *)menu->Entries.First; menuEntry; menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next )
417 if ( menuEntry->SubMenu != NULL )
418 fghSetSubmenuParentWindow ( window, menuEntry->SubMenu ) ;
424 * Displays the currently active menu for the current window
426 void fgDisplayMenu( void )
428 SFG_Window* window = fgStructure.Window;
429 SFG_Menu* menu = NULL;
432 * Make sure there is a current window available
434 freeglut_assert_window;
437 * Check if there is an active menu attached to this window...
439 menu = window->ActiveMenu;
442 * Did we find an active menu?
444 freeglut_return_if_fail( menu != NULL );
446 fgSetWindow ( menu->Window ) ;
449 * Prepare the OpenGL state to do the rendering first:
451 glPushAttrib( GL_DEPTH_BUFFER_BIT | GL_TEXTURE_BIT | GL_LIGHTING_BIT | GL_POLYGON_BIT );
453 glDisable( GL_DEPTH_TEST );
454 glDisable( GL_TEXTURE_2D );
455 glDisable( GL_LIGHTING );
456 glDisable( GL_CULL_FACE );
459 * We'll use an orthogonal projection matrix to draw the menu:
461 glMatrixMode( GL_PROJECTION );
465 0, glutGet( GLUT_WINDOW_WIDTH ),
466 glutGet( GLUT_WINDOW_HEIGHT ), 0,
471 * Model-view matix gets reset to identity:
473 glMatrixMode( GL_MODELVIEW );
478 * First of all, have the exact menu status check:
480 fghCheckMenuStatus( window, menu );
483 * The status has been updated and we're ready to have the menu drawn now:
485 fghDisplayMenuBox( menu );
488 * Restore the old OpenGL settings now
492 glMatrixMode( GL_PROJECTION );
494 glMatrixMode( GL_MODELVIEW );
500 * Restore the current window
502 fgSetWindow ( window ) ;
506 * Activates a menu pointed by the function argument
508 void fgActivateMenu( SFG_Window* window, int button )
511 * We'll be referencing this menu a lot, so remember its address:
513 SFG_Menu* menu = window->Menu[ button ];
516 * Mark the menu as active, so that it gets displayed:
518 window->ActiveMenu = menu;
519 menu->IsActive = TRUE ;
520 fgState.ActiveMenus ++ ;
523 * Set up the initial menu position now:
526 menu->X = window->State.MouseX + glutGet ( GLUT_WINDOW_X ) ;
527 menu->Y = window->State.MouseY + glutGet ( GLUT_WINDOW_Y ) ;
529 if ( menu->X + menu->Width > glutGet ( GLUT_SCREEN_WIDTH ) )
530 menu->X -=menu->Width ;
532 if ( menu->Y + menu->Height > glutGet ( GLUT_SCREEN_HEIGHT ) )
533 menu->Y -=menu->Height ;
535 fgSetWindow ( menu->Window ) ;
536 glutPositionWindow ( menu->X, menu->Y ) ;
537 glutReshapeWindow ( menu->Width, menu->Height ) ;
540 menu->Window->ActiveMenu = menu ;
545 * Check whether an active menu absorbs a mouse click
547 GLboolean fgCheckActiveMenu ( SFG_Window *window, SFG_Menu *menu )
550 * Near as I can tell, this is the active menu behaviour:
551 * - Down-click any button outside the menu, menu active: deactivate the menu
552 * - Down-click any button inside the menu, menu active: select the menu entry and deactivate the menu
553 * - Up-click the menu button outside the menu, menu active: nothing happens
554 * - Up-click the menu button inside the menu, menu active: select the menu entry and deactivate the menu
555 * Since menus can have submenus, we need to check this recursively.
557 return fghCheckMenuStatus ( window, menu ) ;
561 * Function to check for menu entry selection on menu deactivation
563 void fgExecuteMenuCallback( SFG_Menu* menu )
565 SFG_MenuEntry *menuEntry;
568 * First of all check any of the active sub menus...
570 for( menuEntry = (SFG_MenuEntry *)menu->Entries.First; menuEntry; menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next)
573 * Is this menu entry active?
575 if( menuEntry->IsActive == TRUE )
578 * If there is not a sub menu, execute the menu callback and return...
580 if( menuEntry->SubMenu == NULL )
583 * ...certainly given that there is one...
585 if( menu->Callback != NULL )
586 menu->Callback( menuEntry->ID );
592 * Otherwise recurse into the submenu.
594 fgExecuteMenuCallback( menuEntry->SubMenu );
597 * There is little sense in dwelling the search on
605 * Deactivates a menu pointed by the function argument.
607 void fgDeactivateMenu( SFG_Window *window )
609 SFG_Window *current_window = fgStructure.Window ;
612 * Check if there is an active menu attached to this window...
614 SFG_Menu* menu = window->ActiveMenu;
615 SFG_MenuEntry *menuEntry ;
618 * Did we find an active window?
620 freeglut_return_if_fail( menu != NULL );
623 * Hide the present menu's window
625 fgSetWindow ( menu->Window ) ;
629 * Forget about having that menu active anymore, now:
631 menu->Window->ActiveMenu = NULL ;
632 menu->ParentWindow->ActiveMenu = NULL ;
633 menu->IsActive = FALSE ;
635 fgState.ActiveMenus -- ;
638 * Hide all submenu windows, and the root menu's window.
640 for ( menuEntry = (SFG_MenuEntry *)menu->Entries.First; menuEntry;
641 menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next )
644 * Is that an active submenu by any case?
646 if ( menuEntry->SubMenu != NULL )
647 fgDeactivateSubMenu ( menuEntry ) ;
650 fgSetWindow ( current_window ) ;
654 * Deactivates a menu pointed by the function argument.
656 void fgDeactivateSubMenu( SFG_MenuEntry *menuEntry )
658 SFG_Window *current_window = fgStructure.Window ;
659 SFG_MenuEntry *subMenuIter ;
661 * Hide the present menu's window
663 fgSetWindow ( menuEntry->SubMenu->Window ) ;
667 * Forget about having that menu active anymore, now:
669 menuEntry->SubMenu->Window->ActiveMenu = NULL ;
670 menuEntry->SubMenu->IsActive = FALSE ;
673 * Hide all submenu windows, and the root menu's window.
675 for ( subMenuIter = (SFG_MenuEntry *)menuEntry->SubMenu->Entries.First; subMenuIter;
676 subMenuIter = (SFG_MenuEntry *)subMenuIter->Node.Next )
679 * Is that an active submenu by any case?
681 if ( subMenuIter->SubMenu != NULL )
682 fgDeactivateSubMenu ( subMenuIter ) ;
685 fgSetWindow ( current_window ) ;
689 * Recalculates current menu's box size
691 void fghCalculateMenuBoxSize( void )
693 SFG_MenuEntry* menuEntry;
694 int width = 0, height = 0;
697 * Make sure there is a current menu set
699 freeglut_assert_ready; freeglut_return_if_fail( fgStructure.Menu != NULL );
702 * The menu's box size depends on the menu entries:
704 for( menuEntry = (SFG_MenuEntry *)fgStructure.Menu->Entries.First; menuEntry;
705 menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next)
708 * Update the menu entry's width value
710 menuEntry->Width = glutBitmapLength( FREEGLUT_MENU_FONT, menuEntry->Text );
713 * If the entry is a submenu, then it needs to be wider to accomodate the arrow. JCJ 31 July 2003
716 if (menuEntry->SubMenu != NULL)
717 menuEntry->Width += glutBitmapLength( FREEGLUT_MENU_FONT, "_" );
720 * Check if it's the biggest we've found
722 if( menuEntry->Width > width )
723 width = menuEntry->Width;
725 height += FREEGLUT_MENU_HEIGHT;
729 * Store the menu's box size now:
731 fgStructure.Menu->Height = height + 2 * FREEGLUT_MENU_BORDER ;
732 fgStructure.Menu->Width = width + 4 * FREEGLUT_MENU_BORDER ;
736 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
739 * Creates a new menu object, adding it to the freeglut structure
741 int FGAPIENTRY glutCreateMenu( void (* callback)( int ) )
744 * The menu object creation code resides in freeglut_structure.c
746 return( fgCreateMenu( callback )->ID );
750 * Destroys a menu object, removing all references to it
752 void FGAPIENTRY glutDestroyMenu( int menuID )
754 SFG_Menu* menu = fgMenuByID( menuID );
756 freeglut_assert_ready; freeglut_return_if_fail( menu != NULL );
759 * The menu object destruction code resides in freeglut_structure.c
761 fgDestroyMenu( menu );
765 * Returns the ID number of the currently active menu
767 int FGAPIENTRY glutGetMenu( void )
769 freeglut_assert_ready;
770 if( fgStructure.Menu != NULL )
771 return( fgStructure.Menu->ID );
776 * Sets the current menu given its menu ID
778 void FGAPIENTRY glutSetMenu( int menuID )
780 SFG_Menu* menu = fgMenuByID( menuID );
782 freeglut_assert_ready;
783 freeglut_return_if_fail( menu != NULL );
784 fgStructure.Menu = menu;
788 * Adds a menu entry to the bottom of the current menu
790 void FGAPIENTRY glutAddMenuEntry( const char* label, int value )
792 SFG_MenuEntry* menuEntry = (SFG_MenuEntry *)calloc( sizeof(SFG_MenuEntry), 1 );
794 freeglut_assert_ready;
795 freeglut_return_if_fail( fgStructure.Menu != NULL );
796 menuEntry->Text = strdup( label );
797 menuEntry->ID = value;
800 * Have the new menu entry attached to the current menu
802 fgListAppend( &fgStructure.Menu->Entries, &menuEntry->Node );
803 fghCalculateMenuBoxSize();
807 * Add a sub menu to the bottom of the current menu
809 void FGAPIENTRY glutAddSubMenu( const char* label, int subMenuID )
811 SFG_MenuEntry* menuEntry = (SFG_MenuEntry *)calloc( sizeof(SFG_MenuEntry), 1 );
812 SFG_Menu* subMenu = fgMenuByID( subMenuID );
814 freeglut_assert_ready;
815 freeglut_return_if_fail( fgStructure.Menu != NULL );
816 freeglut_return_if_fail( subMenu != NULL );
818 menuEntry->Text = strdup( label );
819 menuEntry->SubMenu = subMenu;
823 * Make the submenu's parent window be the menu's parent window
825 fghSetSubmenuParentWindow ( fgStructure.Menu->ParentWindow, subMenu ) ;
826 fgListAppend( &fgStructure.Menu->Entries, &menuEntry->Node );
827 fghCalculateMenuBoxSize();
831 * Changes the specified menu item in the current menu into a menu entry
833 void FGAPIENTRY glutChangeToMenuEntry( int item, const char* label, int value )
835 SFG_MenuEntry* menuEntry = NULL;
837 freeglut_assert_ready;
838 freeglut_return_if_fail( fgStructure.Menu != NULL );
839 menuEntry = fghFindMenuEntry( fgStructure.Menu, item );
840 freeglut_return_if_fail( menuEntry != NULL );
843 * We want it to become a normal menu entry, so:
845 if( menuEntry->Text != NULL )
846 free( menuEntry->Text );
848 menuEntry->Text = strdup( label );
849 menuEntry->ID = value;
850 menuEntry->SubMenu = NULL;
852 fghCalculateMenuBoxSize();
856 * Changes the specified menu item in the current menu into a sub-menu trigger.
858 void FGAPIENTRY glutChangeToSubMenu( int item, const char* label, int subMenuID )
860 SFG_Menu* subMenu = fgMenuByID( subMenuID );
861 SFG_MenuEntry* menuEntry = NULL;
863 freeglut_assert_ready;
864 freeglut_return_if_fail( fgStructure.Menu );
865 freeglut_return_if_fail( subMenu );
866 menuEntry = fghFindMenuEntry( fgStructure.Menu, item );
867 freeglut_return_if_fail( menuEntry );
870 * We want it to become a sub menu entry, so:
872 if( menuEntry->Text )
873 free( menuEntry->Text );
875 menuEntry->Text = strdup( label );
876 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 );
891 menuEntry = fghFindMenuEntry( fgStructure.Menu, item );
892 freeglut_return_if_fail( menuEntry );
893 fgListRemove( &fgStructure.Menu->Entries, &menuEntry->Node );
894 free( menuEntry->Text );
896 fghCalculateMenuBoxSize();
900 * Attaches a menu to the current window
902 void FGAPIENTRY glutAttachMenu( int button )
904 freeglut_assert_ready;
905 freeglut_return_if_fail( fgStructure.Window );
906 freeglut_return_if_fail( fgStructure.Menu );
907 freeglut_return_if_fail( button >= 0 );
908 freeglut_return_if_fail( button < FREEGLUT_MAX_MENUS );
909 fgStructure.Window->Menu[ button ] = fgStructure.Menu;
912 * Make the parent window of the menu (and all submenus) the current window
914 fghSetSubmenuParentWindow ( fgStructure.Window, fgStructure.Menu ) ;
918 * Detaches a menu from the current window
920 void FGAPIENTRY glutDetachMenu( int button )
922 freeglut_assert_ready;
923 freeglut_return_if_fail( fgStructure.Window );
924 freeglut_return_if_fail( fgStructure.Menu );
925 freeglut_return_if_fail( button >= 0 );
926 freeglut_return_if_fail( button < FREEGLUT_MAX_MENUS );
927 fgStructure.Window->Menu[ button ] = NULL;
931 * A.Donev: Set and retrieve the menu's user data
933 void* FGAPIENTRY glutGetMenuData( void )
935 return(fgStructure.Menu->UserData);
938 void FGAPIENTRY glutSetMenuData(void* data)
940 fgStructure.Menu->UserData=data;
943 /*** END OF FILE ***/