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
73 #elif TARGET_HOST_UNIX_X11
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 should be moved into the freeglut global state, but for now,
83 * we'll put them here. They 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.
91 static float menu_pen_fore [4] = {0.0f, 0.0f, 0.0f, 1.0f};
92 static float menu_pen_back [4] = {0.85f, 0.85f, 0.85f, 1.0f};
93 static float menu_pen_hfore [4] = {1.0f, 1.0f, 1.0f, 1.0f};
94 static float menu_pen_hback [4] = {0.15f, 0.15f, 0.45f, 1.0f};
96 static float menu_pen_fore [4] = {0.0f, 0.0f, 0.0f, 1.0f};
97 static float menu_pen_back [4] = {0.70f, 0.70f, 0.70f, 1.0f};
98 static float menu_pen_hfore [4] = {0.0f, 0.0f, 0.0f, 1.0f};
99 static float menu_pen_hback [4] = {1.0f, 1.0f, 1.0f, 1.0f};
103 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */
106 * Private static function to find a menu entry by index
108 static SFG_MenuEntry *fghFindMenuEntry( SFG_Menu* menu, int index )
110 SFG_MenuEntry *entry;
113 for( entry = (SFG_MenuEntry *)menu->Entries.First; entry; entry = (SFG_MenuEntry *)entry->Node.Next)
124 * Private static function to check for the current menu/sub menu activity state
126 static GLboolean fghCheckMenuStatus( SFG_Window* window, SFG_Menu* menu )
128 SFG_MenuEntry* menuEntry;
132 * First of all check any of the active sub menus...
134 for( menuEntry = (SFG_MenuEntry *)menu->Entries.First; menuEntry;
135 menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next )
138 * Is that an active sub menu by any case?
140 if( menuEntry->SubMenu != NULL && menuEntry->IsActive == TRUE )
143 * OK, have the sub-menu checked, too. If it returns TRUE, it will mean
144 * that it caught the mouse cursor and we do not need to regenerate
145 * the activity list, and so our parents do...
147 GLboolean return_status = fghCheckMenuStatus( window, menuEntry->SubMenu ) ;
150 * Reactivate the submenu as the checkMenuStatus may have turned it off if the mouse
151 * is in its parent menu entry.
153 menuEntry->SubMenu->IsActive = TRUE ;
154 if ( return_status == TRUE )
160 * That much about our sub menus, let's get to checking the current menu:
162 x = window->State.MouseX;
163 y = window->State.MouseY;
166 * Mark all menu entries inactive...
168 for( menuEntry = (SFG_MenuEntry *)menu->Entries.First; menuEntry;
169 menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next )
171 menuEntry->IsActive = FALSE;
175 menu->IsActive = FALSE;
178 * Check if the mouse cursor is contained within the current menu box
180 if ( ( x >= FREEGLUT_MENU_BORDER ) && ( x < menu->Width - FREEGLUT_MENU_BORDER ) &&
181 ( y >= FREEGLUT_MENU_BORDER ) && ( y < menu->Height - FREEGLUT_MENU_BORDER ) &&
182 ( window == menu->Window ) )
185 * Calculation of the highlighted menu item is easy enough now:
187 int menuID = ( y - FREEGLUT_MENU_BORDER ) / FREEGLUT_MENU_HEIGHT ;
190 * The mouse cursor is somewhere over our box, check it out.
192 menuEntry = fghFindMenuEntry( menu, menuID + 1 );
193 assert( menuEntry != NULL );
196 * Mark the menu as active...
198 menuEntry->IsActive = TRUE;
199 menuEntry->Ordinal = menuID;
202 * If this is not the same as the last active menu entry, deactivate the previous entry.
203 * Specifically, if the previous active entry was a submenu then deactivate it.
205 if ( menu->ActiveEntry && ( menuEntry != menu->ActiveEntry ) )
207 if ( menu->ActiveEntry->SubMenu != NULL )
208 fgDeactivateSubMenu ( menu->ActiveEntry ) ;
211 menu->ActiveEntry = menuEntry ;
214 * Don't forget about marking the current menu as active, too:
217 menu->IsActive = TRUE;
220 * OKi, we have marked that entry as active, but it would be also
221 * nice to have its contents updated, in case it's a sub menu.
222 * Also, ignore the return value of the check function:
224 if( menuEntry->SubMenu != NULL )
226 if ( ! menuEntry->SubMenu->IsActive )
228 SFG_Window *current_window = fgStructure.Window ;
231 * Set up the initial menu position now...
235 * Mark the menu as active, so that it gets displayed:
237 menuEntry->SubMenu->IsActive = TRUE ;
240 * Set up the initial submenu position now:
242 menuEntry->SubMenu->X = menu->X + menu->Width ;
243 menuEntry->SubMenu->Y = menu->Y + menuEntry->Ordinal * FREEGLUT_MENU_HEIGHT ;
245 if ( menuEntry->SubMenu->X + menuEntry->SubMenu->Width > glutGet (GLUT_SCREEN_WIDTH ) )
246 menuEntry->SubMenu->X = menu->X - menuEntry->SubMenu->Width;
248 if ( menuEntry->SubMenu->Y + menuEntry->SubMenu->Height > glutGet (GLUT_SCREEN_HEIGHT ) )
249 menuEntry->SubMenu->Y -= (menuEntry->SubMenu->Height -
250 FREEGLUT_MENU_HEIGHT - 2*FREEGLUT_MENU_BORDER);
252 fgSetWindow ( menuEntry->SubMenu->Window ) ;
253 glutPositionWindow ( menuEntry->SubMenu->X, menuEntry->SubMenu->Y ) ;
254 glutReshapeWindow ( menuEntry->SubMenu->Width, menuEntry->SubMenu->Height ) ;
257 menuEntry->SubMenu->Window->ActiveMenu = menuEntry->SubMenu ;
258 fgSetWindow ( current_window ) ;
262 * ...then check the submenu's state:
264 fghCheckMenuStatus( window, menuEntry->SubMenu );
267 * Even if the submenu turned up inactive, activate it because its parent entry is active
269 menuEntry->SubMenu->IsActive = TRUE ;
273 * Report back that we have caught the menu cursor
279 * Looks like the menu cursor is somewhere else...
285 * Displays a menu box and all of its submenus (if they are active)
287 static void fghDisplayMenuBox( SFG_Menu* menu )
289 SFG_MenuEntry *menuEntry;
291 int border = FREEGLUT_MENU_BORDER ;
294 * Have the menu box drawn first. The +- values are
295 * here just to make it more nice-looking...
297 glColor4f( 1.0f, 1.0f, 1.0f, 1.0f ); /* a non-black dark version of the below. */
298 glBegin( GL_QUAD_STRIP );
299 glVertex2i( menu->Width , 0 );
300 glVertex2i( menu->Width-border, border);
302 glVertex2i( border, border);
303 glVertex2i( 0 , menu->Height );
304 glVertex2i( border, menu->Height-border);
307 glColor4f( 0.5f, 0.5f, 0.5f, 1.0f ); /* a non-black dark version of the below. */
308 glBegin( GL_QUAD_STRIP );
309 glVertex2i( 0 , menu->Height );
310 glVertex2i( border, menu->Height-border);
311 glVertex2i( menu->Width , menu->Height );
312 glVertex2i( menu->Width-border, menu->Height-border);
313 glVertex2i( menu->Width , 0 );
314 glVertex2i( menu->Width-border, border);
317 glColor4fv ( menu_pen_back ) ;
319 glVertex2i( border, border);
320 glVertex2i( menu->Width-border, border);
321 glVertex2i( menu->Width-border, menu->Height-border);
322 glVertex2i( border, menu->Height-border);
326 * Check if any of the submenus is currently active...
328 for( menuEntry = (SFG_MenuEntry *)menu->Entries.First; menuEntry;
329 menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next )
332 * Has the menu been marked as active, maybe?
334 if( menuEntry->IsActive == TRUE )
337 * That's truly right, and we need to have it highlighted.
338 * There is an assumption that mouse cursor didn't move
339 * since the last check of menu activity state:
341 int menuID = menuEntry->Ordinal;
344 * So have the highlight drawn...
346 glColor4fv ( menu_pen_hback ) ;
348 glVertex2i( FREEGLUT_MENU_BORDER, (menuID + 0)*FREEGLUT_MENU_HEIGHT + FREEGLUT_MENU_BORDER );
349 glVertex2i( menu->Width-FREEGLUT_MENU_BORDER, (menuID + 0)*FREEGLUT_MENU_HEIGHT + FREEGLUT_MENU_BORDER );
350 glVertex2i( menu->Width-FREEGLUT_MENU_BORDER, (menuID + 1)*FREEGLUT_MENU_HEIGHT + FREEGLUT_MENU_BORDER );
351 glVertex2i( FREEGLUT_MENU_BORDER, (menuID + 1)*FREEGLUT_MENU_HEIGHT + FREEGLUT_MENU_BORDER );
357 * Print the menu entries now...
360 glColor4fv ( menu_pen_fore );
362 for( menuEntry = (SFG_MenuEntry *)menu->Entries.First, i=0; menuEntry;
363 menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next, ++i )
366 * If the menu entry is active, set the color to white
368 if ( menuEntry->IsActive )
369 glColor4fv ( menu_pen_hfore ) ;
372 * Move the raster into position...
375 2 * FREEGLUT_MENU_BORDER,
376 (i + 1)*FREEGLUT_MENU_HEIGHT-(int)(FREEGLUT_MENU_HEIGHT*0.3 - FREEGLUT_MENU_BORDER ) /* Try to center the text - JCJ 31 July 2003*/
380 * Have the label drawn, character after character:
382 glutBitmapString( FREEGLUT_MENU_FONT, menuEntry->Text);
385 * If it's a submenu, draw a right arrow
387 if ( menuEntry->SubMenu != NULL )
389 int width = glutBitmapWidth ( FREEGLUT_MENU_FONT, '_' ) ;
390 int x_base = menu->Width - 2 - width;
391 int y_base = i*FREEGLUT_MENU_HEIGHT + FREEGLUT_MENU_BORDER;
392 glBegin( GL_TRIANGLES );
393 glVertex2i( x_base, y_base + 2*FREEGLUT_MENU_BORDER);
394 glVertex2i( menu->Width - 2, y_base + (FREEGLUT_MENU_HEIGHT + FREEGLUT_MENU_BORDER) / 2 );
395 glVertex2i( x_base, y_base + FREEGLUT_MENU_HEIGHT - FREEGLUT_MENU_BORDER);
400 * If the menu entry is active, reset the color
402 if ( menuEntry->IsActive )
403 glColor4fv ( menu_pen_fore ) ;
407 * Now we are ready to check if any of our children needs to be redrawn:
409 for( menuEntry = (SFG_MenuEntry *)menu->Entries.First; menuEntry;
410 menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next )
413 * Is that an active sub menu by any case?
415 if( menuEntry->SubMenu != NULL && menuEntry->IsActive == TRUE )
418 * Yeah, indeed. Have it redrawn now:
420 fgSetWindow ( menuEntry->SubMenu->Window ) ;
421 fghDisplayMenuBox( menuEntry->SubMenu );
422 fgSetWindow ( menu->Window ) ;
428 * Private static function to set the parent window of a submenu and all of its submenus
430 static void fghSetSubmenuParentWindow ( SFG_Window *window, SFG_Menu *menu )
432 SFG_MenuEntry *menuEntry ;
434 menu->ParentWindow = window ;
436 for ( menuEntry = (SFG_MenuEntry *)menu->Entries.First; menuEntry; menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next )
438 if ( menuEntry->SubMenu != NULL )
439 fghSetSubmenuParentWindow ( window, menuEntry->SubMenu ) ;
445 * Displays the currently active menu for the current window
447 void fgDisplayMenu( void )
449 SFG_Window* window = fgStructure.Window;
450 SFG_Menu* menu = NULL;
453 * Make sure there is a current window available
455 freeglut_assert_window;
458 * Check if there is an active menu attached to this window...
460 menu = window->ActiveMenu;
463 * Did we find an active menu?
465 freeglut_return_if_fail( menu != NULL );
467 fgSetWindow ( menu->Window ) ;
470 * Prepare the OpenGL state to do the rendering first:
472 glPushAttrib( GL_DEPTH_BUFFER_BIT | GL_TEXTURE_BIT | GL_LIGHTING_BIT | GL_POLYGON_BIT );
474 glDisable( GL_DEPTH_TEST );
475 glDisable( GL_TEXTURE_2D );
476 glDisable( GL_LIGHTING );
477 glDisable( GL_CULL_FACE );
480 * We'll use an orthogonal projection matrix to draw the menu:
482 glMatrixMode( GL_PROJECTION );
486 0, glutGet( GLUT_WINDOW_WIDTH ),
487 glutGet( GLUT_WINDOW_HEIGHT ), 0,
492 * Model-view matix gets reset to identity:
494 glMatrixMode( GL_MODELVIEW );
499 * First of all, have the exact menu status check:
501 fghCheckMenuStatus( window, menu );
504 * The status has been updated and we're ready to have the menu drawn now:
506 fghDisplayMenuBox( menu );
509 * Restore the old OpenGL settings now
513 glMatrixMode( GL_PROJECTION );
515 glMatrixMode( GL_MODELVIEW );
521 * Restore the current window
523 fgSetWindow ( window ) ;
527 * Activates a menu pointed by the function argument
529 void fgActivateMenu( SFG_Window* window, int button )
532 * We'll be referencing this menu a lot, so remember its address:
534 SFG_Menu* menu = window->Menu[ button ];
537 * Mark the menu as active, so that it gets displayed:
539 window->ActiveMenu = menu;
540 menu->IsActive = TRUE ;
541 fgState.ActiveMenus ++ ;
544 * Set up the initial menu position now:
547 menu->X = window->State.MouseX + glutGet ( GLUT_WINDOW_X ) ;
548 menu->Y = window->State.MouseY + glutGet ( GLUT_WINDOW_Y ) ;
550 if ( menu->X + menu->Width > glutGet ( GLUT_SCREEN_WIDTH ) )
551 menu->X -=menu->Width ;
553 if ( menu->Y + menu->Height > glutGet ( GLUT_SCREEN_HEIGHT ) )
554 menu->Y -=menu->Height ;
556 fgSetWindow ( menu->Window ) ;
557 glutPositionWindow ( menu->X, menu->Y ) ;
558 glutReshapeWindow ( menu->Width, menu->Height ) ;
561 menu->Window->ActiveMenu = menu ;
566 * Check whether an active menu absorbs a mouse click
568 GLboolean fgCheckActiveMenu ( SFG_Window *window, SFG_Menu *menu )
571 * Near as I can tell, this is the active menu behaviour:
572 * - Down-click any button outside the menu, menu active: deactivate the menu
573 * - Down-click any button inside the menu, menu active: select the menu entry and deactivate the menu
574 * - Up-click the menu button outside the menu, menu active: nothing happens
575 * - Up-click the menu button inside the menu, menu active: select the menu entry and deactivate the menu
576 * Since menus can have submenus, we need to check this recursively.
578 return fghCheckMenuStatus ( window, menu ) ;
582 * Function to check for menu entry selection on menu deactivation
584 void fgExecuteMenuCallback( SFG_Menu* menu )
586 SFG_MenuEntry *menuEntry;
589 * First of all check any of the active sub menus...
591 for( menuEntry = (SFG_MenuEntry *)menu->Entries.First; menuEntry; menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next)
594 * Is this menu entry active?
596 if( menuEntry->IsActive == TRUE )
599 * If there is not a sub menu, execute the menu callback and return...
601 if( menuEntry->SubMenu == NULL )
604 * ...certainly given that there is one...
606 if( menu->Callback != NULL )
607 menu->Callback( menuEntry->ID );
613 * Otherwise recurse into the submenu.
615 fgExecuteMenuCallback( menuEntry->SubMenu );
618 * There is little sense in dwelling the search on
626 * Deactivates a menu pointed by the function argument.
628 void fgDeactivateMenu( SFG_Window *window )
630 SFG_Window *current_window = fgStructure.Window ;
633 * Check if there is an active menu attached to this window...
635 SFG_Menu* menu = window->ActiveMenu;
636 SFG_MenuEntry *menuEntry ;
639 * Did we find an active window?
641 freeglut_return_if_fail( menu != NULL );
644 * Hide the present menu's window
646 fgSetWindow ( menu->Window ) ;
650 * Forget about having that menu active anymore, now:
652 menu->Window->ActiveMenu = NULL ;
653 menu->ParentWindow->ActiveMenu = NULL ;
654 menu->IsActive = FALSE ;
656 fgState.ActiveMenus -- ;
659 * Hide all submenu windows, and the root menu's window.
661 for ( menuEntry = (SFG_MenuEntry *)menu->Entries.First; menuEntry;
662 menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next )
665 * Is that an active submenu by any case?
667 if ( menuEntry->SubMenu != NULL )
668 fgDeactivateSubMenu ( menuEntry ) ;
671 fgSetWindow ( current_window ) ;
675 * Deactivates a menu pointed by the function argument.
677 void fgDeactivateSubMenu( SFG_MenuEntry *menuEntry )
679 SFG_Window *current_window = fgStructure.Window ;
680 SFG_MenuEntry *subMenuIter ;
682 * Hide the present menu's window
684 fgSetWindow ( menuEntry->SubMenu->Window ) ;
688 * Forget about having that menu active anymore, now:
690 menuEntry->SubMenu->Window->ActiveMenu = NULL ;
691 menuEntry->SubMenu->IsActive = FALSE ;
694 * Hide all submenu windows, and the root menu's window.
696 for ( subMenuIter = (SFG_MenuEntry *)menuEntry->SubMenu->Entries.First; subMenuIter;
697 subMenuIter = (SFG_MenuEntry *)subMenuIter->Node.Next )
700 * Is that an active submenu by any case?
702 if ( subMenuIter->SubMenu != NULL )
703 fgDeactivateSubMenu ( subMenuIter ) ;
706 fgSetWindow ( current_window ) ;
710 * Recalculates current menu's box size
712 void fghCalculateMenuBoxSize( void )
714 SFG_MenuEntry* menuEntry;
715 int width = 0, height = 0;
718 * Make sure there is a current menu set
720 freeglut_assert_ready; freeglut_return_if_fail( fgStructure.Menu != NULL );
723 * The menu's box size depends on the menu entries:
725 for( menuEntry = (SFG_MenuEntry *)fgStructure.Menu->Entries.First; menuEntry;
726 menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next)
729 * Update the menu entry's width value
731 menuEntry->Width = glutBitmapLength( FREEGLUT_MENU_FONT, menuEntry->Text );
734 * If the entry is a submenu, then it needs to be wider to accomodate the arrow. JCJ 31 July 2003
737 if (menuEntry->SubMenu != NULL)
738 menuEntry->Width += glutBitmapLength( FREEGLUT_MENU_FONT, "_" );
741 * Check if it's the biggest we've found
743 if( menuEntry->Width > width )
744 width = menuEntry->Width;
746 height += FREEGLUT_MENU_HEIGHT;
750 * Store the menu's box size now:
752 fgStructure.Menu->Height = height + 2 * FREEGLUT_MENU_BORDER ;
753 fgStructure.Menu->Width = width + 4 * FREEGLUT_MENU_BORDER ;
757 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
760 * Creates a new menu object, adding it to the freeglut structure
762 int FGAPIENTRY glutCreateMenu( void (* callback)( int ) )
765 * The menu object creation code resides in freeglut_structure.c
767 return( fgCreateMenu( callback )->ID );
771 * Destroys a menu object, removing all references to it
773 void FGAPIENTRY glutDestroyMenu( int menuID )
775 SFG_Menu* menu = fgMenuByID( menuID );
777 freeglut_assert_ready; freeglut_return_if_fail( menu != NULL );
780 * The menu object destruction code resides in freeglut_structure.c
782 fgDestroyMenu( menu );
786 * Returns the ID number of the currently active menu
788 int FGAPIENTRY glutGetMenu( void )
790 freeglut_assert_ready;
793 * Is there a current menu set?
795 if( fgStructure.Menu != NULL )
798 * Yes, there is indeed...
800 return( fgStructure.Menu->ID );
804 * No, there is no current menu at all
810 * Sets the current menu given its menu ID
812 void FGAPIENTRY glutSetMenu( int menuID )
814 SFG_Menu* menu = fgMenuByID( menuID );
816 freeglut_assert_ready; freeglut_return_if_fail( menu != NULL );
819 * The current menu pointer is stored in fgStructure.Menu
821 fgStructure.Menu = menu;
825 * Adds a menu entry to the bottom of the current menu
827 void FGAPIENTRY glutAddMenuEntry( const char* label, int value )
829 SFG_MenuEntry* menuEntry = (SFG_MenuEntry *)calloc( sizeof(SFG_MenuEntry), 1 );
832 * Make sure there is a current menu set
834 freeglut_assert_ready; freeglut_return_if_fail( fgStructure.Menu != NULL );
837 * Fill in the appropriate values...
839 menuEntry->Text = strdup( label );
840 menuEntry->ID = value;
843 * Have the new menu entry attached to the current menu
845 fgListAppend( &fgStructure.Menu->Entries, &menuEntry->Node );
848 * Update the menu's dimensions now
850 fghCalculateMenuBoxSize();
854 * Add a sub menu to the bottom of the current menu
856 void FGAPIENTRY glutAddSubMenu( const char* label, int subMenuID )
858 SFG_MenuEntry* menuEntry = (SFG_MenuEntry *)calloc( sizeof(SFG_MenuEntry), 1 );
859 SFG_Menu* subMenu = fgMenuByID( subMenuID );
862 * Make sure there is a current menu and the sub menu
863 * we want to attach actually exists...
865 freeglut_assert_ready; freeglut_return_if_fail( fgStructure.Menu != NULL );
866 freeglut_return_if_fail( subMenu != NULL );
869 * Fill in the appropriate values
871 menuEntry->Text = strdup( label );
872 menuEntry->SubMenu = subMenu;
876 * Make the submenu's parent window be the menu's parent window
878 fghSetSubmenuParentWindow ( fgStructure.Menu->ParentWindow, subMenu ) ;
881 * Have the new menu entry attached to the current menu
883 fgListAppend( &fgStructure.Menu->Entries, &menuEntry->Node );
886 * Update the menu's dimensions now
888 fghCalculateMenuBoxSize();
892 * Changes the specified menu item in the current menu into a menu entry
894 void FGAPIENTRY glutChangeToMenuEntry( int item, const char* label, int value )
896 SFG_MenuEntry* menuEntry = NULL;
899 * Make sure there is a current menu set...
901 freeglut_assert_ready; freeglut_return_if_fail( fgStructure.Menu != NULL );
904 * Get n-th menu entry in the current menu, starting from one:
906 menuEntry = fghFindMenuEntry( fgStructure.Menu, item );
909 * Make sure the menu entry exists
911 freeglut_return_if_fail( menuEntry != NULL );
914 * We want it to become a normal menu entry, so:
916 if( menuEntry->Text != NULL )
917 free( menuEntry->Text );
919 menuEntry->Text = strdup( label );
920 menuEntry->ID = value;
921 menuEntry->SubMenu = NULL;
924 * Update the menu's dimensions now
926 fghCalculateMenuBoxSize();
930 * Changes the specified menu item in the current menu into a sub-menu trigger.
932 void FGAPIENTRY glutChangeToSubMenu( int item, const char* label, int subMenuID )
934 SFG_Menu* subMenu = fgMenuByID( subMenuID );
935 SFG_MenuEntry* menuEntry = NULL;
938 * Make sure there is a current menu set and the sub menu exists...
940 freeglut_assert_ready; freeglut_return_if_fail( fgStructure.Menu != NULL );
941 freeglut_return_if_fail( subMenu != NULL );
944 * Get n-th menu entry in the current menu, starting from one:
946 menuEntry = fghFindMenuEntry( fgStructure.Menu, item );
949 * Make sure the menu entry exists
951 freeglut_return_if_fail( menuEntry != NULL );
954 * We want it to become a sub menu entry, so:
956 if( menuEntry->Text != NULL )
957 free( menuEntry->Text );
959 menuEntry->Text = strdup( label );
960 menuEntry->SubMenu = subMenu;
964 * Update the menu's dimensions now
966 fghCalculateMenuBoxSize();
970 * Removes the specified menu item from the current menu
972 void FGAPIENTRY glutRemoveMenuItem( int item )
974 SFG_MenuEntry* menuEntry;
977 * Make sure there is a current menu set
979 freeglut_assert_ready; freeglut_return_if_fail( fgStructure.Menu != NULL );
982 * Get n-th menu entry in the current menu, starting from one:
984 menuEntry = fghFindMenuEntry( fgStructure.Menu, item );
987 * Make sure the menu entry exists
989 freeglut_return_if_fail( menuEntry != NULL );
992 * Removing a menu entry is quite simple...
994 fgListRemove( &fgStructure.Menu->Entries, &menuEntry->Node );
997 * Free the entry label string, too
999 free( menuEntry->Text );
1004 * Update the menu's dimensions now
1006 fghCalculateMenuBoxSize();
1010 * Attaches a menu to the current window
1012 void FGAPIENTRY glutAttachMenu( int button )
1014 freeglut_assert_ready;
1017 * There must be a current window and a current menu set:
1019 freeglut_return_if_fail( fgStructure.Window != NULL || fgStructure.Menu != NULL );
1022 * Make sure the button value is valid (0, 1 or 2, see freeglut.h)
1024 freeglut_return_if_fail( button == GLUT_LEFT_BUTTON || button == GLUT_MIDDLE_BUTTON || button == GLUT_RIGHT_BUTTON );
1027 * It is safe now to attach the menu
1029 fgStructure.Window->Menu[ button ] = fgStructure.Menu;
1032 * Make the parent window of the menu (and all submenus) the current window
1034 fghSetSubmenuParentWindow ( fgStructure.Window, fgStructure.Menu ) ;
1038 * Detaches a menu from the current window
1040 void FGAPIENTRY glutDetachMenu( int button )
1042 freeglut_assert_ready;
1045 * There must be a current window set:
1047 freeglut_return_if_fail( fgStructure.Window != NULL );
1050 * Make sure the button value is valid (0, 1 or 2, see freeglut.h)
1052 freeglut_return_if_fail( button != 0 && button != 1 && button != 2 );
1055 * It is safe now to detach the menu
1057 fgStructure.Window->Menu[ button ] = NULL;
1061 * A.Donev: Set and retrieve the menu's user data
1063 void* FGAPIENTRY glutGetMenuData( void )
1065 return(fgStructure.Menu->UserData);
1068 void FGAPIENTRY glutSetMenuData(void* data)
1070 fgStructure.Menu->UserData=data;
1073 /*** END OF FILE ***/