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 <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.)
59 #if TARGET_HOST_WIN32 || TARGET_HOST_WINCE
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) + \
67 #define FREEGLUT_MENU_BORDER 2
71 * These variables are for rendering the freeglut menu items.
73 * The choices are fore- and background, with and without h for Highlighting.
74 * Old GLUT appeared to be system-dependant for its colors (sigh) so we are
75 * too. These variables should be stuffed into global state and initialized
76 * via the glutInit*() system.
78 #if TARGET_HOST_WIN32 || TARGET_HOST_WINCE
79 static float menu_pen_fore [4] = {0.0f, 0.0f, 0.0f, 1.0f};
80 static float menu_pen_back [4] = {0.85f, 0.85f, 0.85f, 1.0f};
81 static float menu_pen_hfore [4] = {1.0f, 1.0f, 1.0f, 1.0f};
82 static float menu_pen_hback [4] = {0.15f, 0.15f, 0.45f, 1.0f};
84 static float menu_pen_fore [4] = {0.0f, 0.0f, 0.0f, 1.0f};
85 static float menu_pen_back [4] = {0.70f, 0.70f, 0.70f, 1.0f};
86 static float menu_pen_hfore [4] = {0.0f, 0.0f, 0.0f, 1.0f};
87 static float menu_pen_hback [4] = {1.0f, 1.0f, 1.0f, 1.0f};
90 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */
93 * Private function to find a menu entry by index
95 static SFG_MenuEntry *fghFindMenuEntry( SFG_Menu* menu, int index )
100 for( entry = (SFG_MenuEntry *)menu->Entries.First;
102 entry = (SFG_MenuEntry *)entry->Node.Next )
113 * Private function to check for the current menu/sub menu activity state
115 static GLboolean fghCheckMenuStatus( SFG_Window* window, SFG_Menu* menu )
117 SFG_MenuEntry* menuEntry;
120 /* First of all check any of the active sub menus... */
121 for( menuEntry = (SFG_MenuEntry *)menu->Entries.First;
123 menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next )
125 if( menuEntry->SubMenu && menuEntry->IsActive )
128 * OK, have the sub-menu checked, too. If it returns GL_TRUE, it
129 * will mean that it caught the mouse cursor and we do not need
130 * to regenerate the activity list, and so our parents do...
132 GLboolean return_status = fghCheckMenuStatus( window,
133 menuEntry->SubMenu );
136 * Reactivate the submenu as the checkMenuStatus may have turned
137 * it off if the mouse is in its parent menu entry.
139 menuEntry->SubMenu->IsActive = GL_TRUE;
145 /* That much about our sub menus, let's get to checking the current menu: */
146 x = window->State.MouseX;
147 y = window->State.MouseY;
149 for( menuEntry = (SFG_MenuEntry *)menu->Entries.First;
151 menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next )
152 menuEntry->IsActive = GL_FALSE;
154 menu->IsActive = GL_FALSE;
156 /* Check if the mouse cursor is contained within the current menu box */
157 if( ( x >= FREEGLUT_MENU_BORDER ) &&
158 ( x < menu->Width - FREEGLUT_MENU_BORDER ) &&
159 ( y >= FREEGLUT_MENU_BORDER ) &&
160 ( y < menu->Height - FREEGLUT_MENU_BORDER ) &&
161 ( window == menu->Window ) )
163 int menuID = ( y - FREEGLUT_MENU_BORDER ) / FREEGLUT_MENU_HEIGHT;
165 /* The mouse cursor is somewhere over our box, check it out. */
166 menuEntry = fghFindMenuEntry( menu, menuID + 1 );
167 FREEGLUT_INTERNAL_ERROR_EXIT( menuEntry, "Cannot find menu entry",
168 "fghCheckMenuStatus" );
170 menuEntry->IsActive = GL_TRUE;
171 menuEntry->Ordinal = menuID;
174 * If this is not the same as the last active menu entry, deactivate
175 * the previous entry. Specifically, if the previous active entry
176 * was a submenu then deactivate it.
178 if( menu->ActiveEntry && ( menuEntry != menu->ActiveEntry ) )
179 if( menu->ActiveEntry->SubMenu )
180 fgDeactivateSubMenu( menu->ActiveEntry );
182 menu->ActiveEntry = menuEntry;
183 menu->IsActive = GL_TRUE;
186 * OKi, we have marked that entry as active, but it would be also
187 * nice to have its contents updated, in case it's a sub menu.
188 * Also, ignore the return value of the check function:
190 if( menuEntry->SubMenu )
192 if ( ! menuEntry->SubMenu->IsActive )
194 SFG_Window *current_window = fgStructure.Window;
196 /* Set up the initial menu position now... */
197 menuEntry->SubMenu->IsActive = GL_TRUE;
199 /* Set up the initial submenu position now: */
200 menuEntry->SubMenu->X = menu->X + menu->Width;
201 menuEntry->SubMenu->Y = menu->Y +
202 menuEntry->Ordinal * FREEGLUT_MENU_HEIGHT;
204 if( menuEntry->SubMenu->X + menuEntry->SubMenu->Width >
205 glutGet( GLUT_SCREEN_WIDTH ) )
206 menuEntry->SubMenu->X = menu->X -
207 menuEntry->SubMenu->Width;
209 if( menuEntry->SubMenu->Y + menuEntry->SubMenu->Height >
210 glutGet( GLUT_SCREEN_HEIGHT ) )
211 menuEntry->SubMenu->Y -= ( menuEntry->SubMenu->Height -
212 FREEGLUT_MENU_HEIGHT -
213 2 * FREEGLUT_MENU_BORDER );
215 fgSetWindow( menuEntry->SubMenu->Window );
216 glutPositionWindow( menuEntry->SubMenu->X,
217 menuEntry->SubMenu->Y );
218 glutReshapeWindow( menuEntry->SubMenu->Width,
219 menuEntry->SubMenu->Height );
222 menuEntry->SubMenu->Window->ActiveMenu = menuEntry->SubMenu;
223 fgSetWindow( current_window );
226 fghCheckMenuStatus( window, menuEntry->SubMenu );
228 /* Activate it because its parent entry is active */
229 menuEntry->SubMenu->IsActive = GL_TRUE;
232 /* Report back that we have caught the menu cursor */
236 /* Looks like the menu cursor is somewhere else... */
241 * Displays a menu box and all of its submenus (if they are active)
243 static void fghDisplayMenuBox( SFG_Menu* menu )
245 SFG_MenuEntry *menuEntry;
247 int border = FREEGLUT_MENU_BORDER;
250 * Have the menu box drawn first. The +- values are
251 * here just to make it more nice-looking...
253 /* a non-black dark version of the below. */
254 glColor4f( 1.0f, 1.0f, 1.0f, 1.0f );
255 glBegin( GL_QUAD_STRIP );
256 glVertex2i( menu->Width , 0 );
257 glVertex2i( menu->Width - border, border);
259 glVertex2i( border, border);
260 glVertex2i( 0 , menu->Height );
261 glVertex2i( border, menu->Height - border);
264 /* a non-black dark version of the below. */
265 glColor4f( 0.5f, 0.5f, 0.5f, 1.0f );
266 glBegin( GL_QUAD_STRIP );
267 glVertex2i( 0 , menu->Height );
268 glVertex2i( border, menu->Height - border);
269 glVertex2i( menu->Width , menu->Height );
270 glVertex2i( menu->Width - border, menu->Height - border);
271 glVertex2i( menu->Width , 0 );
272 glVertex2i( menu->Width - border, border);
275 glColor4fv( menu_pen_back );
277 glVertex2i( border, border);
278 glVertex2i( menu->Width - border, border);
279 glVertex2i( menu->Width - border, menu->Height - border);
280 glVertex2i( border, menu->Height - border);
283 /* Check if any of the submenus is currently active... */
284 for( menuEntry = (SFG_MenuEntry *)menu->Entries.First;
286 menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next )
288 /* Has the menu been marked as active, maybe? */
289 if( menuEntry->IsActive )
292 * That's truly right, and we need to have it highlighted.
293 * There is an assumption that mouse cursor didn't move
294 * since the last check of menu activity state:
296 int menuID = menuEntry->Ordinal;
298 /* So have the highlight drawn... */
299 glColor4fv( menu_pen_hback );
302 (menuID + 0)*FREEGLUT_MENU_HEIGHT + border );
303 glVertex2i( menu->Width - border,
304 (menuID + 0)*FREEGLUT_MENU_HEIGHT + border );
305 glVertex2i( menu->Width - border,
306 (menuID + 1)*FREEGLUT_MENU_HEIGHT + border );
308 (menuID + 1)*FREEGLUT_MENU_HEIGHT + border );
313 /* Print the menu entries now... */
315 glColor4fv( menu_pen_fore );
317 for( menuEntry = (SFG_MenuEntry *)menu->Entries.First, i = 0;
319 menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next, ++i )
321 /* If the menu entry is active, set the color to white */
322 if( menuEntry->IsActive )
323 glColor4fv( menu_pen_hfore );
325 /* Move the raster into position... */
326 /* Try to center the text - JCJ 31 July 2003*/
329 ( i + 1 )*FREEGLUT_MENU_HEIGHT -
330 ( int )( FREEGLUT_MENU_HEIGHT*0.3 - border )
333 /* Have the label drawn, character after character: */
334 glutBitmapString( FREEGLUT_MENU_FONT,
335 (unsigned char *)menuEntry->Text);
337 /* If it's a submenu, draw a right arrow */
338 if( menuEntry->SubMenu )
340 int width = glutBitmapWidth( FREEGLUT_MENU_FONT, '_' );
341 int x_base = menu->Width - 2 - width;
342 int y_base = i*FREEGLUT_MENU_HEIGHT + border;
343 glBegin( GL_TRIANGLES );
344 glVertex2i( x_base, y_base + 2*border);
345 glVertex2i( menu->Width - 2, y_base +
346 ( FREEGLUT_MENU_HEIGHT + border) / 2 );
347 glVertex2i( x_base, y_base + FREEGLUT_MENU_HEIGHT - border );
351 /* If the menu entry is active, reset the color */
352 if( menuEntry->IsActive )
353 glColor4fv( menu_pen_fore );
356 /* Now we are ready to check if any of our children needs to be redrawn: */
357 for( menuEntry = ( SFG_MenuEntry * )menu->Entries.First;
359 menuEntry = ( SFG_MenuEntry * )menuEntry->Node.Next )
361 /* Is that an active sub menu by any case? */
362 if( menuEntry->SubMenu && menuEntry->IsActive )
364 /* Yeah, indeed. Have it redrawn now: */
365 fgSetWindow( menuEntry->SubMenu->Window );
366 fghDisplayMenuBox( menuEntry->SubMenu );
367 fgSetWindow( menu->Window );
373 * Private static function to set the parent window of a submenu and all
376 static void fghSetSubmenuParentWindow( SFG_Window *window, SFG_Menu *menu )
378 SFG_MenuEntry *menuEntry;
380 menu->ParentWindow = window;
382 for( menuEntry = ( SFG_MenuEntry * )menu->Entries.First;
384 menuEntry = ( SFG_MenuEntry * )menuEntry->Node.Next )
385 if( menuEntry->SubMenu )
386 fghSetSubmenuParentWindow( window, menuEntry->SubMenu );
390 * Function to check for menu entry selection on menu deactivation
392 static void fghExecuteMenuCallback( SFG_Menu* menu )
394 SFG_MenuEntry *menuEntry;
396 /* First of all check any of the active sub menus... */
397 for( menuEntry = (SFG_MenuEntry *)menu->Entries.First;
399 menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next)
401 if( menuEntry->IsActive )
403 if( menuEntry->SubMenu )
404 fghExecuteMenuCallback( menuEntry->SubMenu );
407 menu->Callback( menuEntry->ID );
415 * Displays the currently active menu for the current window
417 void fgDisplayMenu( void )
419 SFG_Window* window = fgStructure.Window;
420 SFG_Menu* menu = NULL;
422 FREEGLUT_INTERNAL_ERROR_EXIT ( fgStructure.Window, "Displaying menu in nonexistent window",
425 /* Check if there is an active menu attached to this window... */
426 menu = window->ActiveMenu;
427 freeglut_return_if_fail( menu );
429 fgSetWindow( menu->Window );
431 glPushAttrib( GL_DEPTH_BUFFER_BIT | GL_TEXTURE_BIT | GL_LIGHTING_BIT |
434 glDisable( GL_DEPTH_TEST );
435 glDisable( GL_TEXTURE_2D );
436 glDisable( GL_LIGHTING );
437 glDisable( GL_CULL_FACE );
439 glMatrixMode( GL_PROJECTION );
443 0, glutGet( GLUT_WINDOW_WIDTH ),
444 glutGet( GLUT_WINDOW_HEIGHT ), 0,
448 glMatrixMode( GL_MODELVIEW );
452 fghCheckMenuStatus( window, menu );
453 fghDisplayMenuBox( menu );
457 glMatrixMode( GL_PROJECTION );
459 glMatrixMode( GL_MODELVIEW );
464 fgSetWindow ( window );
468 * Activates a menu pointed by the function argument
470 void fgActivateMenu( SFG_Window* window, int button )
472 /* We'll be referencing this menu a lot, so remember its address: */
473 SFG_Menu* menu = window->Menu[ button ];
475 /* Mark the menu as active, so that it gets displayed: */
476 window->ActiveMenu = menu;
477 menu->IsActive = GL_TRUE;
478 fgState.ActiveMenus++;
480 /* Set up the initial menu position now: */
481 menu->X = window->State.MouseX + glutGet( GLUT_WINDOW_X );
482 menu->Y = window->State.MouseY + glutGet( GLUT_WINDOW_Y );
484 if( menu->X + menu->Width > glutGet ( GLUT_SCREEN_WIDTH ) )
485 menu->X -=menu->Width;
487 if( menu->Y + menu->Height > glutGet ( GLUT_SCREEN_HEIGHT ) )
488 menu->Y -=menu->Height;
490 fgSetWindow( menu->Window );
491 glutPositionWindow( menu->X, menu->Y );
492 glutReshapeWindow( menu->Width, menu->Height );
495 menu->Window->ActiveMenu = menu;
499 * Check whether an active menu absorbs a mouse click
501 GLboolean fgCheckActiveMenu ( SFG_Window *window, int button, GLboolean pressed,
502 int mouse_x, int mouse_y )
505 * Near as I can tell, this is the menu behaviour:
506 * - Down-click the menu button, menu not active: activate
507 * the menu with its upper left-hand corner at the mouse
509 * - Down-click any button outside the menu, menu active:
510 * deactivate the menu
511 * - Down-click any button inside the menu, menu active:
512 * select the menu entry and deactivate the menu
513 * - Up-click the menu button, menu not active: nothing happens
514 * - Up-click the menu button outside the menu, menu active:
516 * - Up-click the menu button inside the menu, menu active:
517 * select the menu entry and deactivate the menu
518 * Since menus can have submenus, we need to check this recursively.
520 if( window->ActiveMenu )
522 if( window == window->ActiveMenu->ParentWindow )
524 window->ActiveMenu->Window->State.MouseX =
525 mouse_x - window->ActiveMenu->X;
526 window->ActiveMenu->Window->State.MouseY =
527 mouse_y - window->ActiveMenu->Y;
530 /* In the menu, invoke the callback and deactivate the menu */
531 if( fghCheckMenuStatus( window->ActiveMenu->Window,
532 window->ActiveMenu ) )
535 * Save the current window and menu and set the current
536 * window to the window whose menu this is
538 SFG_Window *save_window = fgStructure.Window;
539 SFG_Menu *save_menu = fgStructure.Menu;
540 SFG_Window *parent_window = window->ActiveMenu->ParentWindow;
541 fgSetWindow( parent_window );
542 fgStructure.Menu = window->ActiveMenu;
544 /* Execute the menu callback */
545 fghExecuteMenuCallback( window->ActiveMenu );
546 fgDeactivateMenu( parent_window );
548 /* Restore the current window and menu */
549 fgSetWindow( save_window );
550 fgStructure.Menu = save_menu;
554 * Outside the menu, deactivate if it's a downclick
556 * XXX This isn't enough. A downclick outside of
557 * XXX the interior of our freeglut windows should also
558 * XXX deactivate the menu. This is more complicated.
560 fgDeactivateMenu( window->ActiveMenu->ParentWindow );
563 * XXX Why does an active menu require a redisplay at
564 * XXX this point? If this can come out cleanly, then
565 * XXX it probably should do so; if not, a comment should
568 if( ! window->IsMenu )
569 window->State.Redisplay = GL_TRUE;
574 /* No active menu, let's check whether we need to activate one. */
575 if( ( 0 <= button ) &&
576 ( FREEGLUT_MAX_MENUS > button ) &&
577 ( window->Menu[ button ] ) &&
580 /* XXX Posting a requisite Redisplay seems bogus. */
581 window->State.Redisplay = GL_TRUE;
582 fgSetWindow( window );
583 fgActivateMenu( window, button );
591 * Deactivates a menu pointed by the function argument.
593 void fgDeactivateMenu( SFG_Window *window )
595 SFG_Window *current_window = fgStructure.Window;
597 /* Check if there is an active menu attached to this window... */
598 SFG_Menu* menu = window->ActiveMenu;
599 SFG_MenuEntry *menuEntry;
601 /* Did we find an active window? */
602 freeglut_return_if_fail( menu );
604 /* Hide the present menu's window */
605 fgSetWindow( menu->Window );
608 /* Forget about having that menu active anymore, now: */
609 menu->Window->ActiveMenu = NULL;
610 menu->ParentWindow->ActiveMenu = NULL;
611 menu->IsActive = GL_FALSE;
613 fgState.ActiveMenus--;
615 /* Hide all submenu windows, and the root menu's window. */
616 for ( menuEntry = ( SFG_MenuEntry * )menu->Entries.First;
618 menuEntry = ( SFG_MenuEntry * )menuEntry->Node.Next )
620 /* Is that an active submenu by any case? */
621 if( menuEntry->SubMenu )
622 fgDeactivateSubMenu( menuEntry );
625 fgSetWindow( current_window );
629 * Deactivates a menu pointed by the function argument.
631 void fgDeactivateSubMenu( SFG_MenuEntry *menuEntry )
633 SFG_Window *current_window = fgStructure.Window;
634 SFG_MenuEntry *subMenuIter;
635 /* Hide the present menu's window */
636 fgSetWindow( menuEntry->SubMenu->Window );
639 /* Forget about having that menu active anymore, now: */
640 menuEntry->SubMenu->Window->ActiveMenu = NULL;
641 menuEntry->SubMenu->IsActive = GL_FALSE;
643 /* Hide all submenu windows, and the root menu's window. */
644 for ( subMenuIter = (SFG_MenuEntry *)menuEntry->SubMenu->Entries.First;
646 subMenuIter = (SFG_MenuEntry *)subMenuIter->Node.Next )
648 /* Is that an active submenu by any case? */
649 if( subMenuIter->SubMenu )
650 fgDeactivateSubMenu( subMenuIter );
653 fgSetWindow( current_window );
657 * Recalculates current menu's box size
659 void fghCalculateMenuBoxSize( void )
661 SFG_MenuEntry* menuEntry;
662 int width = 0, height = 0;
664 /* Make sure there is a current menu set */
665 freeglut_return_if_fail( fgStructure.Menu );
667 /* The menu's box size depends on the menu entries: */
668 for( menuEntry = ( SFG_MenuEntry * )fgStructure.Menu->Entries.First;
670 menuEntry = ( SFG_MenuEntry * )menuEntry->Node.Next )
672 /* Update the menu entry's width value */
673 menuEntry->Width = glutBitmapLength(
675 (unsigned char *)menuEntry->Text
679 * If the entry is a submenu, then it needs to be wider to
680 * accomodate the arrow. JCJ 31 July 2003
682 if (menuEntry->SubMenu )
683 menuEntry->Width += glutBitmapLength(
688 /* Check if it's the biggest we've found */
689 if( menuEntry->Width > width )
690 width = menuEntry->Width;
692 height += FREEGLUT_MENU_HEIGHT;
695 /* Store the menu's box size now: */
696 fgStructure.Menu->Height = height + 2 * FREEGLUT_MENU_BORDER;
697 fgStructure.Menu->Width = width + 4 * FREEGLUT_MENU_BORDER;
701 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
704 * Creates a new menu object, adding it to the freeglut structure
706 int FGAPIENTRY glutCreateMenu( void(* callback)( int ) )
708 /* The menu object creation code resides in freeglut_structure.c */
709 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutCreateMenu" );
710 return fgCreateMenu( callback )->ID;
714 * Destroys a menu object, removing all references to it
716 void FGAPIENTRY glutDestroyMenu( int menuID )
720 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutDestroyMenu" );
721 menu = fgMenuByID( menuID );
723 freeglut_return_if_fail( menu );
725 /* The menu object destruction code resides in freeglut_structure.c */
726 fgDestroyMenu( menu );
730 * Returns the ID number of the currently active menu
732 int FGAPIENTRY glutGetMenu( void )
734 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGetMenu" );
736 if( fgStructure.Menu )
737 return fgStructure.Menu->ID;
743 * Sets the current menu given its menu ID
745 void FGAPIENTRY glutSetMenu( int menuID )
749 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetMenu" );
750 menu = fgMenuByID( menuID );
752 freeglut_return_if_fail( menu );
754 fgStructure.Menu = menu;
758 * Adds a menu entry to the bottom of the current menu
760 void FGAPIENTRY glutAddMenuEntry( const char* label, int value )
762 SFG_MenuEntry* menuEntry;
763 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutAddMenuEntry" );
764 menuEntry = (SFG_MenuEntry *)calloc( sizeof(SFG_MenuEntry), 1 );
765 freeglut_return_if_fail( fgStructure.Menu );
767 menuEntry->Text = strdup( label );
768 menuEntry->ID = value;
770 /* Have the new menu entry attached to the current menu */
771 fgListAppend( &fgStructure.Menu->Entries, &menuEntry->Node );
773 fghCalculateMenuBoxSize( );
777 * Add a sub menu to the bottom of the current menu
779 void FGAPIENTRY glutAddSubMenu( const char *label, int subMenuID )
781 SFG_MenuEntry *menuEntry;
784 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutAddSubMenu" );
785 menuEntry = ( SFG_MenuEntry * )calloc( sizeof( SFG_MenuEntry ), 1 );
786 subMenu = fgMenuByID( subMenuID );
788 freeglut_return_if_fail( fgStructure.Menu );
789 freeglut_return_if_fail( subMenu );
791 menuEntry->Text = strdup( label );
792 menuEntry->SubMenu = subMenu;
795 /* Make the submenu's parent window be the menu's parent window */
796 fghSetSubmenuParentWindow( fgStructure.Menu->ParentWindow, subMenu );
798 fgListAppend( &fgStructure.Menu->Entries, &menuEntry->Node );
799 fghCalculateMenuBoxSize( );
803 * Changes the specified menu item in the current menu into a menu entry
805 void FGAPIENTRY glutChangeToMenuEntry( int item, const char* label, int value )
807 SFG_MenuEntry* menuEntry = NULL;
809 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutChangeToMenuEntry" );
810 freeglut_return_if_fail( fgStructure.Menu );
812 /* Get n-th menu entry in the current menu, starting from one: */
813 menuEntry = fghFindMenuEntry( fgStructure.Menu, item );
815 freeglut_return_if_fail( menuEntry );
817 /* We want it to become a normal menu entry, so: */
818 if( menuEntry->Text )
819 free( menuEntry->Text );
821 menuEntry->Text = strdup( label );
822 menuEntry->ID = value;
823 menuEntry->SubMenu = NULL;
824 fghCalculateMenuBoxSize( );
828 * Changes the specified menu item in the current menu into a sub-menu trigger.
830 void FGAPIENTRY glutChangeToSubMenu( int item, const char* label,
834 SFG_MenuEntry* menuEntry;
836 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutChangeToSubMenu" );
837 subMenu = fgMenuByID( subMenuID );
840 freeglut_return_if_fail( fgStructure.Menu );
841 freeglut_return_if_fail( subMenu );
843 /* Get n-th menu entry in the current menu, starting from one: */
844 menuEntry = fghFindMenuEntry( fgStructure.Menu, item );
846 freeglut_return_if_fail( menuEntry );
848 /* We want it to become a sub menu entry, so: */
849 if( menuEntry->Text )
850 free( menuEntry->Text );
852 menuEntry->Text = strdup( label );
853 menuEntry->SubMenu = subMenu;
855 fghCalculateMenuBoxSize( );
859 * Removes the specified menu item from the current menu
861 void FGAPIENTRY glutRemoveMenuItem( int item )
863 SFG_MenuEntry* menuEntry;
865 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutRemoveMenuItem" );
866 freeglut_return_if_fail( fgStructure.Menu );
868 /* Get n-th menu entry in the current menu, starting from one: */
869 menuEntry = fghFindMenuEntry( fgStructure.Menu, item );
871 freeglut_return_if_fail( menuEntry );
873 fgListRemove( &fgStructure.Menu->Entries, &menuEntry->Node );
874 if ( menuEntry->Text )
875 free( menuEntry->Text );
878 fghCalculateMenuBoxSize( );
882 * Attaches a menu to the current window
884 void FGAPIENTRY glutAttachMenu( int button )
886 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutAttachMenu" );
888 freeglut_return_if_fail( fgStructure.Window );
889 freeglut_return_if_fail( fgStructure.Menu );
891 freeglut_return_if_fail( button >= 0 );
892 freeglut_return_if_fail( button < FREEGLUT_MAX_MENUS );
894 fgStructure.Window->Menu[ button ] = fgStructure.Menu;
896 /* Make the parent window of the menu (and all submenus) the current window */
897 fghSetSubmenuParentWindow( fgStructure.Window, fgStructure.Menu );
901 * Detaches a menu from the current window
903 void FGAPIENTRY glutDetachMenu( int button )
905 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutDetachMenu" );
907 freeglut_return_if_fail( fgStructure.Window );
908 freeglut_return_if_fail( fgStructure.Menu );
910 freeglut_return_if_fail( button >= 0 );
911 freeglut_return_if_fail( button < FREEGLUT_MAX_MENUS );
913 fgStructure.Window->Menu[ button ] = NULL;
917 * A.Donev: Set and retrieve the menu's user data
919 void* FGAPIENTRY glutGetMenuData( void )
921 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGetMenuData" );
922 return fgStructure.Menu->UserData;
925 void FGAPIENTRY glutSetMenuData(void* data)
927 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetMenuData" );
928 fgStructure.Menu->UserData=data;
931 /*** END OF FILE ***/