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 * It would be cool if the submenu entries were somehow marked, for example with a dings
41 * on the right menu border or something like that. Think about the possibility of doing
42 * the menu on layers *or* using the native window system instead of OpenGL.
45 /* -- DEFINITIONS ---------------------------------------------------------- */
48 * We'll be using freeglut fonts to draw the menu
50 #define FREEGLUT_MENU_FONT GLUT_BITMAP_8_BY_13
51 /*#define FREEGLUT_MENU_FONT GLUT_BITMAP_HELVETICA_18*/
52 #define FREEGLUT_MENU_HEIGHT (glutBitmapHeight(FREEGLUT_MENU_FONT) + FREEGLUT_MENU_BORDER)
53 #define FREEGLUT_MENU_BORDER 2
57 * These variables should be moved into the freeglut global state, but for now,
58 * we'll put them here. They are for rendering the freeglut menu items.
59 * The choices are fore- and background, with and without h for Highlighting.
60 * Old GLUT appeared to be system-dependant for its colors (sigh) so we are
61 * too. These variables should be stuffed into global state and initialized
62 * via the glutInit*() system.
64 static float menu_pen_fore [4] = {0.0f, 0.0f, 0.0f, 1.0f};
65 static float menu_pen_back [4] = {0.85f, 0.85f, 0.85f, 1.0f};
68 static float menu_pen_hfore [4] = {1.0f, 1.0f, 1.0f, 1.0f};
69 static float menu_pen_hback [4] = {0.15f, 0.15f, 0.45f, 1.0f};
71 static float menu_pen_hfore [4] = {0.0f, 0.0f, 0.0f, 1.0f};
72 static float menu_pen_hback [4] = {1.0f, 1.0f, 1.0f, 1.0f};
76 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */
79 * Private static function to find a menu entry by index
81 static SFG_MenuEntry *fghFindMenuEntry( SFG_Menu* menu, int index )
86 for( entry = (SFG_MenuEntry *)menu->Entries.First; entry; entry = (SFG_MenuEntry *)entry->Node.Next)
97 * Private static function to check for the current menu/sub menu activity state
99 static GLboolean fghCheckMenuStatus( SFG_Window* window, SFG_Menu* menu )
101 SFG_MenuEntry* menuEntry;
105 * First of all check any of the active sub menus...
107 for( menuEntry = (SFG_MenuEntry *)menu->Entries.First; menuEntry;
108 menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next )
111 * Is that an active sub menu by any case?
113 if( menuEntry->SubMenu != NULL && menuEntry->IsActive == TRUE )
116 * OK, have the sub-menu checked, too. If it returns TRUE, it will mean
117 * that it caught the mouse cursor and we do not need to regenerate
118 * the activity list, and so our parents do...
120 GLboolean return_status = fghCheckMenuStatus( window, menuEntry->SubMenu ) ;
123 * Reactivate the submenu as the checkMenuStatus may have turned it off if the mouse
124 * is in its parent menu entry.
126 menuEntry->SubMenu->IsActive = TRUE ;
127 if ( return_status == TRUE )
133 * That much about our sub menus, let's get to checking the current menu:
135 x = window->State.MouseX;
136 y = window->State.MouseY;
139 * Mark all menu entries inactive...
141 for( menuEntry = (SFG_MenuEntry *)menu->Entries.First; menuEntry;
142 menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next )
144 menuEntry->IsActive = FALSE;
148 menu->IsActive = FALSE;
151 * Check if the mouse cursor is contained within the current menu box
153 if ( ( x >= FREEGLUT_MENU_BORDER ) && ( x < menu->Width - FREEGLUT_MENU_BORDER ) &&
154 ( y >= FREEGLUT_MENU_BORDER ) && ( y < menu->Height - FREEGLUT_MENU_BORDER ) &&
155 ( window == menu->Window ) )
158 * Calculation of the highlighted menu item is easy enough now:
160 int menuID = ( y - FREEGLUT_MENU_BORDER ) / FREEGLUT_MENU_HEIGHT ;
163 * The mouse cursor is somewhere over our box, check it out.
165 menuEntry = fghFindMenuEntry( menu, menuID + 1 );
166 assert( menuEntry != NULL );
169 * Mark the menu as active...
171 menuEntry->IsActive = TRUE;
172 menuEntry->Ordinal = menuID;
175 * If this is not the same as the last active menu entry, deactivate the previous entry.
176 * Specifically, if the previous active entry was a submenu then deactivate it.
178 if ( menu->ActiveEntry && ( menuEntry != menu->ActiveEntry ) )
180 if ( menu->ActiveEntry->SubMenu != NULL )
181 fgDeactivateSubMenu ( menu->ActiveEntry ) ;
184 menu->ActiveEntry = menuEntry ;
187 * Don't forget about marking the current menu as active, too:
190 menu->IsActive = TRUE;
193 * OKi, we have marked that entry as active, but it would be also
194 * nice to have its contents updated, in case it's a sub menu.
195 * Also, ignore the return value of the check function:
197 if( menuEntry->SubMenu != NULL )
199 if ( ! menuEntry->SubMenu->IsActive )
201 SFG_Window *current_window = fgStructure.Window ;
204 * Set up the initial menu position now...
208 * Mark the menu as active, so that it gets displayed:
210 menuEntry->SubMenu->IsActive = TRUE ;
213 * Set up the initial submenu position now:
215 menuEntry->SubMenu->X = menu->X + menu->Width ;
216 menuEntry->SubMenu->Y = menu->Y + menuEntry->Ordinal * FREEGLUT_MENU_HEIGHT ;
218 if ( menuEntry->SubMenu->X + menuEntry->SubMenu->Width > glutGet (
219 GLUT_SCREEN_WIDTH ) )
220 menuEntry->SubMenu->X = menu->X - menuEntry->SubMenu->Width ;
222 if ( menuEntry->SubMenu->Y + menuEntry->SubMenu->Height > glutGet (
223 GLUT_SCREEN_HEIGHT ) )
224 menuEntry->SubMenu->Y -=menuEntry->SubMenu->Height ;
226 fgSetWindow ( menuEntry->SubMenu->Window ) ;
227 glutPositionWindow ( menuEntry->SubMenu->X, menuEntry->SubMenu->Y ) ;
228 glutReshapeWindow ( menuEntry->SubMenu->Width, menuEntry->SubMenu->Height ) ;
231 menuEntry->SubMenu->Window->ActiveMenu = menuEntry->SubMenu ;
232 fgSetWindow ( current_window ) ;
236 * ...then check the submenu's state:
238 fghCheckMenuStatus( window, menuEntry->SubMenu );
241 * Even if the submenu turned up inactive, activate it because its parent entry is active
243 menuEntry->SubMenu->IsActive = TRUE ;
247 * Report back that we have caught the menu cursor
253 * Looks like the menu cursor is somewhere else...
259 * Displays a menu box and all of its submenus (if they are active)
261 static void fghDisplayMenuBox( SFG_Menu* menu )
263 SFG_MenuEntry *menuEntry;
265 int border = FREEGLUT_MENU_BORDER ;
268 * Have the menu box drawn first. The +- values are
269 * here just to make it more nice-looking...
271 glColor4f( 1.0f, 1.0f, 1.0f, 1.0f ); /* a non-black dark version of the below. */
272 glBegin( GL_QUAD_STRIP );
273 glVertex2i( menu->Width , 0 );
274 glVertex2i( menu->Width-border, border);
276 glVertex2i( border, border);
277 glVertex2i( 0 , menu->Height );
278 glVertex2i( border, menu->Height-border);
281 glColor4f( 0.5f, 0.5f, 0.5f, 1.0f ); /* a non-black dark version of the below. */
282 glBegin( GL_QUAD_STRIP );
283 glVertex2i( 0 , menu->Height );
284 glVertex2i( border, menu->Height-border);
285 glVertex2i( menu->Width , menu->Height );
286 glVertex2i( menu->Width-border, menu->Height-border);
287 glVertex2i( menu->Width , 0 );
288 glVertex2i( menu->Width-border, border);
291 glColor4fv ( menu_pen_back ) ;
293 glVertex2i( border, border);
294 glVertex2i( menu->Width-border, border);
295 glVertex2i( menu->Width-border, menu->Height-border);
296 glVertex2i( border, menu->Height-border);
300 * Check if any of the submenus is currently active...
302 for( menuEntry = (SFG_MenuEntry *)menu->Entries.First; menuEntry;
303 menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next )
306 * Has the menu been marked as active, maybe?
308 if( menuEntry->IsActive == TRUE )
311 * That's truly right, and we need to have it highlighted.
312 * There is an assumption that mouse cursor didn't move
313 * since the last check of menu activity state:
315 int menuID = menuEntry->Ordinal;
318 * So have the highlight drawn...
320 glColor4fv ( menu_pen_hback ) ;
322 glVertex2i( FREEGLUT_MENU_BORDER, (menuID + 0)*FREEGLUT_MENU_HEIGHT + FREEGLUT_MENU_BORDER );
323 glVertex2i( menu->Width-FREEGLUT_MENU_BORDER, (menuID + 0)*FREEGLUT_MENU_HEIGHT + FREEGLUT_MENU_BORDER );
324 glVertex2i( menu->Width-FREEGLUT_MENU_BORDER, (menuID + 1)*FREEGLUT_MENU_HEIGHT + FREEGLUT_MENU_BORDER );
325 glVertex2i( FREEGLUT_MENU_BORDER, (menuID + 1)*FREEGLUT_MENU_HEIGHT + FREEGLUT_MENU_BORDER );
331 * Print the menu entries now...
334 glColor4fv ( menu_pen_fore );
336 for( menuEntry = (SFG_MenuEntry *)menu->Entries.First, i=0; menuEntry;
337 menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next, ++i )
340 * If the menu entry is active, set the color to white
342 if ( menuEntry->IsActive )
343 glColor4fv ( menu_pen_hfore ) ;
346 * Move the raster into position...
349 2 * FREEGLUT_MENU_BORDER,
350 (i + 1)*FREEGLUT_MENU_HEIGHT-(int)(FREEGLUT_MENU_HEIGHT*0.3 - FREEGLUT_MENU_BORDER ) /* Try to center the text - JCJ 31 July 2003*/
354 * Have the label drawn, character after character:
356 glutBitmapString( FREEGLUT_MENU_FONT, menuEntry->Text);
359 * If it's a submenu, draw a right arrow
361 if ( menuEntry->SubMenu != NULL )
363 int width = glutBitmapWidth ( FREEGLUT_MENU_FONT, '_' ) ;
364 int x_base = menu->Width - 2 - width;
365 int y_base = i*FREEGLUT_MENU_HEIGHT + FREEGLUT_MENU_BORDER;
366 glBegin( GL_TRIANGLES );
367 glVertex2i( x_base, y_base + 2*FREEGLUT_MENU_BORDER);
368 glVertex2i( menu->Width - 2, y_base + (FREEGLUT_MENU_HEIGHT + FREEGLUT_MENU_BORDER) / 2 );
369 glVertex2i( x_base, y_base + FREEGLUT_MENU_HEIGHT - FREEGLUT_MENU_BORDER);
374 * If the menu entry is active, reset the color
376 if ( menuEntry->IsActive )
377 glColor4fv ( menu_pen_fore ) ;
381 * Now we are ready to check if any of our children needs to be redrawn:
383 for( menuEntry = (SFG_MenuEntry *)menu->Entries.First; menuEntry;
384 menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next )
387 * Is that an active sub menu by any case?
389 if( menuEntry->SubMenu != NULL && menuEntry->IsActive == TRUE )
392 * Yeah, indeed. Have it redrawn now:
394 fgSetWindow ( menuEntry->SubMenu->Window ) ;
395 fghDisplayMenuBox( menuEntry->SubMenu );
396 fgSetWindow ( menu->Window ) ;
402 * Private static function to set the parent window of a submenu and all of its submenus
404 static void fghSetSubmenuParentWindow ( SFG_Window *window, SFG_Menu *menu )
406 SFG_MenuEntry *menuEntry ;
408 menu->ParentWindow = window ;
410 for ( menuEntry = (SFG_MenuEntry *)menu->Entries.First; menuEntry; menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next )
412 if ( menuEntry->SubMenu != NULL )
413 fghSetSubmenuParentWindow ( window, menuEntry->SubMenu ) ;
419 * Displays the currently active menu for the current window
421 void fgDisplayMenu( void )
423 SFG_Window* window = fgStructure.Window;
424 SFG_Menu* menu = NULL;
427 * Make sure there is a current window available
429 freeglut_assert_window;
432 * Check if there is an active menu attached to this window...
434 menu = window->ActiveMenu;
437 * Did we find an active menu?
439 freeglut_return_if_fail( menu != NULL );
441 fgSetWindow ( menu->Window ) ;
444 * Prepare the OpenGL state to do the rendering first:
446 glPushAttrib( GL_DEPTH_BUFFER_BIT | GL_TEXTURE_BIT | GL_LIGHTING_BIT | GL_POLYGON_BIT );
448 glDisable( GL_DEPTH_TEST );
449 glDisable( GL_TEXTURE_2D );
450 glDisable( GL_LIGHTING );
451 glDisable( GL_CULL_FACE );
454 * We'll use an orthogonal projection matrix to draw the menu:
456 glMatrixMode( GL_PROJECTION );
460 0, glutGet( GLUT_WINDOW_WIDTH ),
461 glutGet( GLUT_WINDOW_HEIGHT ), 0,
466 * Model-view matix gets reset to identity:
468 glMatrixMode( GL_MODELVIEW );
473 * First of all, have the exact menu status check:
475 fghCheckMenuStatus( window, menu );
478 * The status has been updated and we're ready to have the menu drawn now:
480 fghDisplayMenuBox( menu );
483 * Restore the old OpenGL settings now
487 glMatrixMode( GL_PROJECTION );
489 glMatrixMode( GL_MODELVIEW );
495 * Restore the current window
497 fgSetWindow ( window ) ;
501 * Activates a menu pointed by the function argument
503 void fgActivateMenu( SFG_Window* window, int button )
506 * We'll be referencing this menu a lot, so remember its address:
508 SFG_Menu* menu = window->Menu[ button ];
511 * Mark the menu as active, so that it gets displayed:
513 window->ActiveMenu = menu;
514 menu->IsActive = TRUE ;
515 fgState.ActiveMenus ++ ;
518 * Set up the initial menu position now:
521 menu->X = window->State.MouseX + glutGet ( GLUT_WINDOW_X ) ;
522 menu->Y = window->State.MouseY + glutGet ( GLUT_WINDOW_Y ) ;
524 if ( menu->X + menu->Width > glutGet ( GLUT_SCREEN_WIDTH ) )
525 menu->X -=menu->Width ;
527 if ( menu->Y + menu->Height > glutGet ( GLUT_SCREEN_HEIGHT ) )
528 menu->Y -=menu->Height ;
530 fgSetWindow ( menu->Window ) ;
531 glutPositionWindow ( menu->X, menu->Y ) ;
532 glutReshapeWindow ( menu->Width, menu->Height ) ;
535 menu->Window->ActiveMenu = menu ;
540 * Check whether an active menu absorbs a mouse click
542 GLboolean fgCheckActiveMenu ( SFG_Window *window, SFG_Menu *menu )
545 * Near as I can tell, this is the active menu behaviour:
546 * - Down-click any button outside the menu, menu active: deactivate the menu
547 * - Down-click any button inside the menu, menu active: select the menu entry and deactivate the menu
548 * - Up-click the menu button outside the menu, menu active: nothing happens
549 * - Up-click the menu button inside the menu, menu active: select the menu entry and deactivate the menu
550 * Since menus can have submenus, we need to check this recursively.
552 return fghCheckMenuStatus ( window, menu ) ;
556 * Function to check for menu entry selection on menu deactivation
558 void fgExecuteMenuCallback( SFG_Menu* menu )
560 SFG_MenuEntry *menuEntry;
563 * First of all check any of the active sub menus...
565 for( menuEntry = (SFG_MenuEntry *)menu->Entries.First; menuEntry; menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next)
568 * Is this menu entry active?
570 if( menuEntry->IsActive == TRUE )
573 * If there is not a sub menu, execute the menu callback and return...
575 if( menuEntry->SubMenu == NULL )
578 * ...certainly given that there is one...
580 if( menu->Callback != NULL )
581 menu->Callback( menuEntry->ID );
587 * Otherwise recurse into the submenu.
589 fgExecuteMenuCallback( menuEntry->SubMenu );
592 * There is little sense in dwelling the search on
600 * Deactivates a menu pointed by the function argument.
602 void fgDeactivateMenu( SFG_Window *window )
604 SFG_Window *current_window = fgStructure.Window ;
607 * Check if there is an active menu attached to this window...
609 SFG_Menu* menu = window->ActiveMenu;
610 SFG_MenuEntry *menuEntry ;
613 * Did we find an active window?
615 freeglut_return_if_fail( menu != NULL );
618 * Hide the present menu's window
620 fgSetWindow ( menu->Window ) ;
624 * Forget about having that menu active anymore, now:
626 menu->Window->ActiveMenu = NULL ;
627 menu->ParentWindow->ActiveMenu = NULL ;
628 menu->IsActive = FALSE ;
630 fgState.ActiveMenus -- ;
633 * Hide all submenu windows, and the root menu's window.
635 for ( menuEntry = (SFG_MenuEntry *)menu->Entries.First; menuEntry;
636 menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next )
639 * Is that an active submenu by any case?
641 if ( menuEntry->SubMenu != NULL )
642 fgDeactivateSubMenu ( menuEntry ) ;
645 fgSetWindow ( current_window ) ;
649 * Deactivates a menu pointed by the function argument.
651 void fgDeactivateSubMenu( SFG_MenuEntry *menuEntry )
653 SFG_Window *current_window = fgStructure.Window ;
654 SFG_MenuEntry *subMenuIter ;
656 * Hide the present menu's window
658 fgSetWindow ( menuEntry->SubMenu->Window ) ;
662 * Forget about having that menu active anymore, now:
664 menuEntry->SubMenu->Window->ActiveMenu = NULL ;
665 menuEntry->SubMenu->IsActive = FALSE ;
668 * Hide all submenu windows, and the root menu's window.
670 for ( subMenuIter = (SFG_MenuEntry *)menuEntry->SubMenu->Entries.First; subMenuIter;
671 subMenuIter = (SFG_MenuEntry *)subMenuIter->Node.Next )
674 * Is that an active submenu by any case?
676 if ( subMenuIter->SubMenu != NULL )
677 fgDeactivateSubMenu ( subMenuIter ) ;
680 fgSetWindow ( current_window ) ;
684 * Recalculates current menu's box size
686 void fghCalculateMenuBoxSize( void )
688 SFG_MenuEntry* menuEntry;
689 int width = 0, height = 0;
692 * Make sure there is a current menu set
694 freeglut_assert_ready; freeglut_return_if_fail( fgStructure.Menu != NULL );
697 * The menu's box size depends on the menu entries:
699 for( menuEntry = (SFG_MenuEntry *)fgStructure.Menu->Entries.First; menuEntry;
700 menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next)
703 * Update the menu entry's width value
705 menuEntry->Width = glutBitmapLength( FREEGLUT_MENU_FONT, menuEntry->Text );
708 * If the entry is a submenu, then it needs to be wider to accomodate the arrow. JCJ 31 July 2003
711 if (menuEntry->SubMenu != NULL)
712 menuEntry->Width += glutBitmapLength( FREEGLUT_MENU_FONT, "_" );
715 * Check if it's the biggest we've found
717 if( menuEntry->Width > width )
718 width = menuEntry->Width;
720 height += FREEGLUT_MENU_HEIGHT;
724 * Store the menu's box size now:
726 fgStructure.Menu->Height = height + 2 * FREEGLUT_MENU_BORDER ;
727 fgStructure.Menu->Width = width + 4 * FREEGLUT_MENU_BORDER ;
731 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
734 * Creates a new menu object, adding it to the freeglut structure
736 int FGAPIENTRY glutCreateMenu( void (* callback)( int ) )
739 * The menu object creation code resides in freeglut_structure.c
741 return( fgCreateMenu( callback )->ID );
745 * Destroys a menu object, removing all references to it
747 void FGAPIENTRY glutDestroyMenu( int menuID )
749 SFG_Menu* menu = fgMenuByID( menuID );
751 freeglut_assert_ready; freeglut_return_if_fail( menu != NULL );
754 * The menu object destruction code resides in freeglut_structure.c
756 fgDestroyMenu( menu );
760 * Returns the ID number of the currently active menu
762 int FGAPIENTRY glutGetMenu( void )
764 freeglut_assert_ready;
767 * Is there a current menu set?
769 if( fgStructure.Menu != NULL )
772 * Yes, there is indeed...
774 return( fgStructure.Menu->ID );
778 * No, there is no current menu at all
784 * Sets the current menu given its menu ID
786 void FGAPIENTRY glutSetMenu( int menuID )
788 SFG_Menu* menu = fgMenuByID( menuID );
790 freeglut_assert_ready; freeglut_return_if_fail( menu != NULL );
793 * The current menu pointer is stored in fgStructure.Menu
795 fgStructure.Menu = menu;
799 * Adds a menu entry to the bottom of the current menu
801 void FGAPIENTRY glutAddMenuEntry( const char* label, int value )
803 SFG_MenuEntry* menuEntry = (SFG_MenuEntry *)calloc( sizeof(SFG_MenuEntry), 1 );
806 * Make sure there is a current menu set
808 freeglut_assert_ready; freeglut_return_if_fail( fgStructure.Menu != NULL );
811 * Fill in the appropriate values...
813 menuEntry->Text = strdup( label );
814 menuEntry->ID = value;
817 * Have the new menu entry attached to the current menu
819 fgListAppend( &fgStructure.Menu->Entries, &menuEntry->Node );
822 * Update the menu's dimensions now
824 fghCalculateMenuBoxSize();
828 * Add a sub menu to the bottom of the current menu
830 void FGAPIENTRY glutAddSubMenu( const char* label, int subMenuID )
832 SFG_MenuEntry* menuEntry = (SFG_MenuEntry *)calloc( sizeof(SFG_MenuEntry), 1 );
833 SFG_Menu* subMenu = fgMenuByID( subMenuID );
836 * Make sure there is a current menu and the sub menu
837 * we want to attach actually exists...
839 freeglut_assert_ready; freeglut_return_if_fail( fgStructure.Menu != NULL );
840 freeglut_return_if_fail( subMenu != NULL );
843 * Fill in the appropriate values
845 menuEntry->Text = strdup( label );
846 menuEntry->SubMenu = subMenu;
850 * Make the submenu's parent window be the menu's parent window
852 fghSetSubmenuParentWindow ( fgStructure.Menu->ParentWindow, subMenu ) ;
855 * Have the new menu entry attached to the current menu
857 fgListAppend( &fgStructure.Menu->Entries, &menuEntry->Node );
860 * Update the menu's dimensions now
862 fghCalculateMenuBoxSize();
866 * Changes the specified menu item in the current menu into a menu entry
868 void FGAPIENTRY glutChangeToMenuEntry( int item, const char* label, int value )
870 SFG_MenuEntry* menuEntry = NULL;
873 * Make sure there is a current menu set...
875 freeglut_assert_ready; freeglut_return_if_fail( fgStructure.Menu != NULL );
878 * Get n-th menu entry in the current menu, starting from one:
880 menuEntry = fghFindMenuEntry( fgStructure.Menu, item );
883 * Make sure the menu entry exists
885 freeglut_return_if_fail( menuEntry != NULL );
888 * We want it to become a normal menu entry, so:
890 if( menuEntry->Text != NULL )
891 free( menuEntry->Text );
893 menuEntry->Text = strdup( label );
894 menuEntry->ID = value;
895 menuEntry->SubMenu = NULL;
898 * Update the menu's dimensions now
900 fghCalculateMenuBoxSize();
904 * Changes the specified menu item in the current menu into a sub-menu trigger.
906 void FGAPIENTRY glutChangeToSubMenu( int item, const char* label, int subMenuID )
908 SFG_Menu* subMenu = fgMenuByID( subMenuID );
909 SFG_MenuEntry* menuEntry = NULL;
912 * Make sure there is a current menu set and the sub menu exists...
914 freeglut_assert_ready; freeglut_return_if_fail( fgStructure.Menu != NULL );
915 freeglut_return_if_fail( subMenu != NULL );
918 * Get n-th menu entry in the current menu, starting from one:
920 menuEntry = fghFindMenuEntry( fgStructure.Menu, item );
923 * Make sure the menu entry exists
925 freeglut_return_if_fail( menuEntry != NULL );
928 * We want it to become a sub menu entry, so:
930 if( menuEntry->Text != NULL )
931 free( menuEntry->Text );
933 menuEntry->Text = strdup( label );
934 menuEntry->SubMenu = subMenu;
938 * Update the menu's dimensions now
940 fghCalculateMenuBoxSize();
944 * Removes the specified menu item from the current menu
946 void FGAPIENTRY glutRemoveMenuItem( int item )
948 SFG_MenuEntry* menuEntry;
951 * Make sure there is a current menu set
953 freeglut_assert_ready; freeglut_return_if_fail( fgStructure.Menu != NULL );
956 * Get n-th menu entry in the current menu, starting from one:
958 menuEntry = fghFindMenuEntry( fgStructure.Menu, item );
961 * Make sure the menu entry exists
963 freeglut_return_if_fail( menuEntry != NULL );
966 * Removing a menu entry is quite simple...
968 fgListRemove( &fgStructure.Menu->Entries, &menuEntry->Node );
971 * Free the entry label string, too
973 free( menuEntry->Text );
978 * Update the menu's dimensions now
980 fghCalculateMenuBoxSize();
984 * Attaches a menu to the current window
986 void FGAPIENTRY glutAttachMenu( int button )
988 freeglut_assert_ready;
991 * There must be a current window and a current menu set:
993 freeglut_return_if_fail( fgStructure.Window != NULL || fgStructure.Menu != NULL );
996 * Make sure the button value is valid (0, 1 or 2, see freeglut.h)
998 freeglut_return_if_fail( button == GLUT_LEFT_BUTTON || button == GLUT_MIDDLE_BUTTON || button == GLUT_RIGHT_BUTTON );
1001 * It is safe now to attach the menu
1003 fgStructure.Window->Menu[ button ] = fgStructure.Menu;
1006 * Make the parent window of the menu (and all submenus) the current window
1008 fghSetSubmenuParentWindow ( fgStructure.Window, fgStructure.Menu ) ;
1012 * Detaches a menu from the current window
1014 void FGAPIENTRY glutDetachMenu( int button )
1016 freeglut_assert_ready;
1019 * There must be a current window set:
1021 freeglut_return_if_fail( fgStructure.Window != NULL );
1024 * Make sure the button value is valid (0, 1 or 2, see freeglut.h)
1026 freeglut_return_if_fail( button != 0 && button != 1 && button != 2 );
1029 * It is safe now to detach the menu
1031 fgStructure.Window->Menu[ button ] = NULL;
1035 * A.Donev: Set and retrieve the menu's user data
1037 void* FGAPIENTRY glutGetMenuData( void )
1039 return(fgStructure.Menu->UserData);
1042 void FGAPIENTRY glutSetMenuData(void* data)
1044 fgStructure.Menu->UserData=data;
1047 /*** END OF FILE ***/