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.
28 #define FREEGLUT_BUILDING_LIB
29 #include <GL/freeglut.h>
30 #include "freeglut_internal.h"
32 /* -- DEFINITIONS ---------------------------------------------------------- */
35 * FREEGLUT_MENU_FONT can be any freeglut bitmapped font.
36 * (Stroked fonts would not be out of the question, but we'd need to alter
37 * code, since GLUT (hence freeglut) does not quite unify stroked and
38 * bitmapped font handling.)
39 * Old UNIX/X11 GLUT (BSD, UNIX, IRIX, LINUX, HPUX, ...) used a system
40 * font best approximated by an 18-pixel HELVETICA, I think. MS-WINDOWS
41 * GLUT used something closest to the 8x13 fixed-width font. (Old
42 * GLUT apparently uses host-system menus rather than building its own.
43 * freeglut is building its own menus from scratch.)
45 * FREEGLUT_MENU_HEIGHT gives the height of ONE menu box. This should be
46 * the distances between two adjacent menu entries. It should scale
47 * automatically with the font choice, so you needn't alter it---unless you
50 * FREEGLUT_MENU_BORDER says how many pixels to allow around the edge of a
51 * menu. (It also seems to be the same as the number of pixels used as
52 * a border around *items* to separate them from neighbors. John says
53 * that that wasn't the original intent...if not, perhaps we need another
54 * symbolic constant, FREEGLUT_MENU_ITEM_BORDER, or such.)
56 #if TARGET_HOST_MS_WINDOWS
57 #define FREEGLUT_MENU_FONT GLUT_BITMAP_8_BY_13
59 #define FREEGLUT_MENU_FONT GLUT_BITMAP_HELVETICA_18
62 #define FREEGLUT_MENU_HEIGHT (glutBitmapHeight(FREEGLUT_MENU_FONT) + \
64 #define FREEGLUT_MENU_BORDER 2
68 * These variables are for rendering the freeglut menu items.
70 * The choices are fore- and background, with and without h for Highlighting.
71 * Old GLUT appeared to be system-dependant for its colors (sigh) so we are
72 * too. These variables should be stuffed into global state and initialized
73 * via the glutInit*() system.
75 #if TARGET_HOST_MS_WINDOWS
76 static float menu_pen_fore [4] = {0.0f, 0.0f, 0.0f, 1.0f};
77 static float menu_pen_back [4] = {0.85f, 0.85f, 0.85f, 1.0f};
78 static float menu_pen_hfore [4] = {1.0f, 1.0f, 1.0f, 1.0f};
79 static float menu_pen_hback [4] = {0.15f, 0.15f, 0.45f, 1.0f};
81 static float menu_pen_fore [4] = {0.0f, 0.0f, 0.0f, 1.0f};
82 static float menu_pen_back [4] = {0.70f, 0.70f, 0.70f, 1.0f};
83 static float menu_pen_hfore [4] = {0.0f, 0.0f, 0.0f, 1.0f};
84 static float menu_pen_hback [4] = {1.0f, 1.0f, 1.0f, 1.0f};
87 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */
90 * Private function to find a menu entry by index
92 static SFG_MenuEntry *fghFindMenuEntry( SFG_Menu* menu, int index )
97 for( entry = (SFG_MenuEntry *)menu->Entries.First;
99 entry = (SFG_MenuEntry *)entry->Node.Next )
110 * Deactivates a menu pointed by the function argument.
112 static void fghDeactivateSubMenu( SFG_MenuEntry *menuEntry )
114 SFG_MenuEntry *subMenuIter;
115 /* Hide the present menu's window */
116 fgSetWindow( menuEntry->SubMenu->Window );
119 /* Forget about having that menu active anymore, now: */
120 menuEntry->SubMenu->Window->ActiveMenu = NULL;
121 menuEntry->SubMenu->IsActive = GL_FALSE;
122 menuEntry->SubMenu->ActiveEntry = NULL;
124 /* Hide all submenu windows, and the root menu's window. */
125 for ( subMenuIter = (SFG_MenuEntry *)menuEntry->SubMenu->Entries.First;
127 subMenuIter = (SFG_MenuEntry *)subMenuIter->Node.Next )
129 subMenuIter->IsActive = GL_FALSE;
131 /* Is that an active submenu by any case? */
132 if( subMenuIter->SubMenu )
133 fghDeactivateSubMenu( subMenuIter );
136 fgSetWindow ( menuEntry->SubMenu->ParentWindow ) ;
140 * Private function to get the virtual maximum screen extent
142 static GLvoid fghGetVMaxExtent( SFG_Window* window, int* x, int* y )
144 if( fgStructure.GameModeWindow )
146 #if TARGET_HOST_POSIX_X11
150 XTranslateCoordinates(
152 window->Window.Handle,
153 fgDisplay.RootWindow,
156 *x = fgState.GameModeSize.X + wx;
157 *y = fgState.GameModeSize.Y + wy;
159 *x = glutGet ( GLUT_SCREEN_WIDTH );
160 *y = glutGet ( GLUT_SCREEN_HEIGHT );
165 *x = fgDisplay.ScreenWidth;
166 *y = fgDisplay.ScreenHeight;
171 * Private function to check for the current menu/sub menu activity state
173 static GLboolean fghCheckMenuStatus( SFG_Menu* menu )
175 SFG_MenuEntry* menuEntry;
178 /* First of all check any of the active sub menus... */
179 for( menuEntry = (SFG_MenuEntry *)menu->Entries.First;
181 menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next )
183 if( menuEntry->SubMenu && menuEntry->IsActive )
186 * OK, have the sub-menu checked, too. If it returns GL_TRUE, it
187 * will mean that it caught the mouse cursor and we do not need
188 * to regenerate the activity list, and so our parents do...
190 GLboolean return_status;
192 menuEntry->SubMenu->Window->State.MouseX =
193 menu->Window->State.MouseX + menu->X - menuEntry->SubMenu->X;
194 menuEntry->SubMenu->Window->State.MouseY =
195 menu->Window->State.MouseY + menu->Y - menuEntry->SubMenu->Y;
196 return_status = fghCheckMenuStatus( menuEntry->SubMenu );
203 /* That much about our sub menus, let's get to checking the current menu: */
204 x = menu->Window->State.MouseX;
205 y = menu->Window->State.MouseY;
207 /* Check if the mouse cursor is contained within the current menu box */
208 if( ( x >= FREEGLUT_MENU_BORDER ) &&
209 ( x < menu->Width - FREEGLUT_MENU_BORDER ) &&
210 ( y >= FREEGLUT_MENU_BORDER ) &&
211 ( y < menu->Height - FREEGLUT_MENU_BORDER ) )
213 int menuID = ( y - FREEGLUT_MENU_BORDER ) / FREEGLUT_MENU_HEIGHT;
215 /* The mouse cursor is somewhere over our box, check it out. */
216 menuEntry = fghFindMenuEntry( menu, menuID + 1 );
217 FREEGLUT_INTERNAL_ERROR_EXIT( menuEntry, "Cannot find menu entry",
218 "fghCheckMenuStatus" );
220 menuEntry->IsActive = GL_TRUE;
221 menuEntry->Ordinal = menuID;
224 * If this is not the same as the last active menu entry, deactivate
225 * the previous entry. Specifically, if the previous active entry
226 * was a submenu then deactivate it.
228 if( menu->ActiveEntry && ( menuEntry != menu->ActiveEntry ) )
229 if( menu->ActiveEntry->SubMenu )
230 fghDeactivateSubMenu( menu->ActiveEntry );
232 if( menuEntry != menu->ActiveEntry )
234 menu->Window->State.Redisplay = GL_TRUE;
235 if( menu->ActiveEntry )
236 menu->ActiveEntry->IsActive = GL_FALSE;
239 menu->ActiveEntry = menuEntry;
240 menu->IsActive = GL_TRUE; /* XXX Do we need this? */
243 * OKi, we have marked that entry as active, but it would be also
244 * nice to have its contents updated, in case it's a sub menu.
245 * Also, ignore the return value of the check function:
247 if( menuEntry->SubMenu )
249 if ( ! menuEntry->SubMenu->IsActive )
252 SFG_Window *current_window = fgStructure.CurrentWindow;
254 /* Set up the initial menu position now... */
255 menuEntry->SubMenu->IsActive = GL_TRUE;
257 /* Set up the initial submenu position now: */
258 fghGetVMaxExtent(menu->ParentWindow, &max_x, &max_y);
259 menuEntry->SubMenu->X = menu->X + menu->Width;
260 menuEntry->SubMenu->Y = menu->Y +
261 menuEntry->Ordinal * FREEGLUT_MENU_HEIGHT;
263 if( menuEntry->SubMenu->X + menuEntry->SubMenu->Width > max_x )
264 menuEntry->SubMenu->X = menu->X - menuEntry->SubMenu->Width;
266 if( menuEntry->SubMenu->Y + menuEntry->SubMenu->Height > max_y )
268 menuEntry->SubMenu->Y -= ( menuEntry->SubMenu->Height -
269 FREEGLUT_MENU_HEIGHT -
270 2 * FREEGLUT_MENU_BORDER );
271 if( menuEntry->SubMenu->Y < 0 )
272 menuEntry->SubMenu->Y = 0;
275 fgSetWindow( menuEntry->SubMenu->Window );
276 glutPositionWindow( menuEntry->SubMenu->X,
277 menuEntry->SubMenu->Y );
278 glutReshapeWindow( menuEntry->SubMenu->Width,
279 menuEntry->SubMenu->Height );
282 menuEntry->SubMenu->Window->ActiveMenu = menuEntry->SubMenu;
283 fgSetWindow( current_window );
284 menuEntry->SubMenu->Window->State.MouseX =
285 x + menu->X - menuEntry->SubMenu->X;
286 menuEntry->SubMenu->Window->State.MouseY =
287 y + menu->Y - menuEntry->SubMenu->Y;
288 fghCheckMenuStatus( menuEntry->SubMenu );
291 /* Activate it because its parent entry is active */
292 menuEntry->SubMenu->IsActive = GL_TRUE; /* XXX Do we need this? */
295 /* Report back that we have caught the menu cursor */
299 /* Looks like the menu cursor is somewhere else... */
300 if( menu->ActiveEntry && menu->ActiveEntry->IsActive &&
301 ( !menu->ActiveEntry->SubMenu ||
302 !menu->ActiveEntry->SubMenu->IsActive ) )
304 menu->Window->State.Redisplay = GL_TRUE;
305 menu->ActiveEntry->IsActive = GL_FALSE;
306 menu->ActiveEntry = NULL;
313 * Displays a menu box and all of its submenus (if they are active)
315 static void fghDisplayMenuBox( SFG_Menu* menu )
317 SFG_MenuEntry *menuEntry;
319 int border = FREEGLUT_MENU_BORDER;
322 * Have the menu box drawn first. The +- values are
323 * here just to make it more nice-looking...
325 /* a non-black dark version of the below. */
326 glColor4f( 1.0f, 1.0f, 1.0f, 1.0f );
327 glBegin( GL_QUAD_STRIP );
328 glVertex2i( menu->Width , 0 );
329 glVertex2i( menu->Width - border, border);
331 glVertex2i( border, border);
332 glVertex2i( 0 , menu->Height );
333 glVertex2i( border, menu->Height - border);
336 /* a non-black dark version of the below. */
337 glColor4f( 0.5f, 0.5f, 0.5f, 1.0f );
338 glBegin( GL_QUAD_STRIP );
339 glVertex2i( 0 , menu->Height );
340 glVertex2i( border, menu->Height - border);
341 glVertex2i( menu->Width , menu->Height );
342 glVertex2i( menu->Width - border, menu->Height - border);
343 glVertex2i( menu->Width , 0 );
344 glVertex2i( menu->Width - border, border);
347 glColor4fv( menu_pen_back );
349 glVertex2i( border, border);
350 glVertex2i( menu->Width - border, border);
351 glVertex2i( menu->Width - border, menu->Height - border);
352 glVertex2i( border, menu->Height - border);
355 /* Check if any of the submenus is currently active... */
356 for( menuEntry = (SFG_MenuEntry *)menu->Entries.First;
358 menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next )
360 /* Has the menu been marked as active, maybe? */
361 if( menuEntry->IsActive )
364 * That's truly right, and we need to have it highlighted.
365 * There is an assumption that mouse cursor didn't move
366 * since the last check of menu activity state:
368 int menuID = menuEntry->Ordinal;
370 /* So have the highlight drawn... */
371 glColor4fv( menu_pen_hback );
374 (menuID + 0)*FREEGLUT_MENU_HEIGHT + border );
375 glVertex2i( menu->Width - border,
376 (menuID + 0)*FREEGLUT_MENU_HEIGHT + border );
377 glVertex2i( menu->Width - border,
378 (menuID + 1)*FREEGLUT_MENU_HEIGHT + border );
380 (menuID + 1)*FREEGLUT_MENU_HEIGHT + border );
385 /* Print the menu entries now... */
387 glColor4fv( menu_pen_fore );
389 for( menuEntry = (SFG_MenuEntry *)menu->Entries.First, i = 0;
391 menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next, ++i )
393 /* If the menu entry is active, set the color to white */
394 if( menuEntry->IsActive )
395 glColor4fv( menu_pen_hfore );
397 /* Move the raster into position... */
398 /* Try to center the text - JCJ 31 July 2003*/
401 ( i + 1 )*FREEGLUT_MENU_HEIGHT -
402 ( int )( FREEGLUT_MENU_HEIGHT*0.3 - border )
405 /* Have the label drawn, character after character: */
406 glutBitmapString( FREEGLUT_MENU_FONT,
407 (unsigned char *)menuEntry->Text);
409 /* If it's a submenu, draw a right arrow */
410 if( menuEntry->SubMenu )
412 int width = glutBitmapWidth( FREEGLUT_MENU_FONT, '_' );
413 int x_base = menu->Width - 2 - width;
414 int y_base = i*FREEGLUT_MENU_HEIGHT + border;
415 glBegin( GL_TRIANGLES );
416 glVertex2i( x_base, y_base + 2*border);
417 glVertex2i( menu->Width - 2, y_base +
418 ( FREEGLUT_MENU_HEIGHT + border) / 2 );
419 glVertex2i( x_base, y_base + FREEGLUT_MENU_HEIGHT - border );
423 /* If the menu entry is active, reset the color */
424 if( menuEntry->IsActive )
425 glColor4fv( menu_pen_fore );
430 * Private static function to set the parent window of a submenu and all
433 static void fghSetMenuParentWindow( SFG_Window *window, SFG_Menu *menu )
435 SFG_MenuEntry *menuEntry;
437 menu->ParentWindow = window;
439 for( menuEntry = ( SFG_MenuEntry * )menu->Entries.First;
441 menuEntry = ( SFG_MenuEntry * )menuEntry->Node.Next )
442 if( menuEntry->SubMenu )
443 fghSetMenuParentWindow( window, menuEntry->SubMenu );
447 * Function to check for menu entry selection on menu deactivation
449 static void fghExecuteMenuCallback( SFG_Menu* menu )
451 SFG_MenuEntry *menuEntry;
453 /* First of all check any of the active sub menus... */
454 for( menuEntry = (SFG_MenuEntry *)menu->Entries.First;
456 menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next)
458 if( menuEntry->IsActive )
460 if( menuEntry->SubMenu )
461 fghExecuteMenuCallback( menuEntry->SubMenu );
465 SFG_Menu *save_menu = fgStructure.CurrentMenu;
466 fgStructure.CurrentMenu = menu;
467 menu->Callback( menuEntry->ID );
468 fgStructure.CurrentMenu = save_menu;
478 * Displays the currently active menu for the current window
480 void fgDisplayMenu( void )
482 SFG_Window* window = fgStructure.CurrentWindow;
483 SFG_Menu* menu = NULL;
485 FREEGLUT_INTERNAL_ERROR_EXIT ( fgStructure.CurrentWindow, "Displaying menu in nonexistent window",
488 /* Check if there is an active menu attached to this window... */
489 menu = window->ActiveMenu;
490 freeglut_return_if_fail( menu );
492 fgSetWindow( menu->Window );
494 glPushAttrib( GL_DEPTH_BUFFER_BIT | GL_TEXTURE_BIT | GL_LIGHTING_BIT |
497 glDisable( GL_DEPTH_TEST );
498 glDisable( GL_TEXTURE_2D );
499 glDisable( GL_LIGHTING );
500 glDisable( GL_CULL_FACE );
502 glMatrixMode( GL_PROJECTION );
506 0, glutGet( GLUT_WINDOW_WIDTH ),
507 glutGet( GLUT_WINDOW_HEIGHT ), 0,
511 glMatrixMode( GL_MODELVIEW );
515 fghDisplayMenuBox( menu );
519 glMatrixMode( GL_PROJECTION );
521 glMatrixMode( GL_MODELVIEW );
526 fgSetWindow ( window );
530 * Activates a menu pointed by the function argument
532 static void fghActivateMenu( SFG_Window* window, int button )
536 /* We'll be referencing this menu a lot, so remember its address: */
537 SFG_Menu* menu = window->Menu[ button ];
538 SFG_Window* current_window = fgStructure.CurrentWindow;
540 /* If the menu is already active in another window, deactivate it there */
541 if ( menu->ParentWindow )
542 menu->ParentWindow->ActiveMenu = NULL ;
544 /* Mark the menu as active, so that it gets displayed: */
545 window->ActiveMenu = menu;
546 menu->IsActive = GL_TRUE;
547 fghSetMenuParentWindow ( window, menu );
548 fgState.ActiveMenus++;
550 /* Set up the initial menu position now: */
551 fghGetVMaxExtent(menu->ParentWindow, &max_x, &max_y);
552 fgSetWindow( window );
553 menu->X = window->State.MouseX + glutGet( GLUT_WINDOW_X );
554 menu->Y = window->State.MouseY + glutGet( GLUT_WINDOW_Y );
556 if( menu->X + menu->Width > max_x )
557 menu->X -=menu->Width;
559 if( menu->Y + menu->Height > max_y )
561 menu->Y -=menu->Height;
566 menu->Window->State.MouseX =
567 window->State.MouseX + glutGet( GLUT_WINDOW_X ) - menu->X;
568 menu->Window->State.MouseY =
569 window->State.MouseY + glutGet( GLUT_WINDOW_Y ) - menu->Y;
571 fgSetWindow( menu->Window );
572 glutPositionWindow( menu->X, menu->Y );
573 glutReshapeWindow( menu->Width, menu->Height );
576 menu->Window->ActiveMenu = menu;
577 fghCheckMenuStatus( menu );
578 fgSetWindow( current_window );
582 * Update Highlight states of the menu
584 * Current mouse position is in menu->Window->State.MouseX/Y.
586 void fgUpdateMenuHighlight ( SFG_Menu *menu )
588 fghCheckMenuStatus( menu );
592 * Check whether an active menu absorbs a mouse click
594 GLboolean fgCheckActiveMenu ( SFG_Window *window, int button, GLboolean pressed,
595 int mouse_x, int mouse_y )
598 * Near as I can tell, this is the menu behaviour:
599 * - Down-click the menu button, menu not active: activate
600 * the menu with its upper left-hand corner at the mouse
602 * - Down-click any button outside the menu, menu active:
603 * deactivate the menu
604 * - Down-click any button inside the menu, menu active:
605 * select the menu entry and deactivate the menu
606 * - Up-click the menu button, menu not active: nothing happens
607 * - Up-click the menu button outside the menu, menu active:
609 * - Up-click the menu button inside the menu, menu active:
610 * select the menu entry and deactivate the menu
611 * Since menus can have submenus, we need to check this recursively.
613 if( window->ActiveMenu )
615 if( window == window->ActiveMenu->ParentWindow )
617 window->ActiveMenu->Window->State.MouseX =
618 mouse_x - window->ActiveMenu->X;
619 window->ActiveMenu->Window->State.MouseY =
620 mouse_y - window->ActiveMenu->Y;
623 /* In the menu, invoke the callback and deactivate the menu */
624 if( fghCheckMenuStatus( window->ActiveMenu ) )
627 * Save the current window and menu and set the current
628 * window to the window whose menu this is
630 SFG_Window *save_window = fgStructure.CurrentWindow;
631 SFG_Menu *save_menu = fgStructure.CurrentMenu;
632 SFG_Window *parent_window = window->ActiveMenu->ParentWindow;
633 fgSetWindow( parent_window );
634 fgStructure.CurrentMenu = window->ActiveMenu;
636 /* Execute the menu callback */
637 fghExecuteMenuCallback( window->ActiveMenu );
638 fgDeactivateMenu( parent_window );
640 /* Restore the current window and menu */
641 fgSetWindow( save_window );
642 fgStructure.CurrentMenu = save_menu;
646 * Outside the menu, deactivate if it's a downclick
648 * XXX This isn't enough. A downclick outside of
649 * XXX the interior of our freeglut windows should also
650 * XXX deactivate the menu. This is more complicated.
652 fgDeactivateMenu( window->ActiveMenu->ParentWindow );
655 * XXX Why does an active menu require a redisplay at
656 * XXX this point? If this can come out cleanly, then
657 * XXX it probably should do so; if not, a comment should
660 if( ! window->IsMenu )
661 window->State.Redisplay = GL_TRUE;
666 /* No active menu, let's check whether we need to activate one. */
667 if( ( 0 <= button ) &&
668 ( FREEGLUT_MAX_MENUS > button ) &&
669 ( window->Menu[ button ] ) &&
672 /* XXX Posting a requisite Redisplay seems bogus. */
673 window->State.Redisplay = GL_TRUE;
674 fghActivateMenu( window, button );
682 * Deactivates a menu pointed by the function argument.
684 void fgDeactivateMenu( SFG_Window *window )
686 SFG_Window *parent_window = NULL;
688 /* Check if there is an active menu attached to this window... */
689 SFG_Menu* menu = window->ActiveMenu;
690 SFG_MenuEntry *menuEntry;
692 /* Did we find an active window? */
693 freeglut_return_if_fail( menu );
695 parent_window = menu->ParentWindow;
697 /* Hide the present menu's window */
698 fgSetWindow( menu->Window );
701 /* Forget about having that menu active anymore, now: */
702 menu->Window->ActiveMenu = NULL;
703 menu->ParentWindow->ActiveMenu = NULL;
704 fghSetMenuParentWindow ( NULL, menu );
705 menu->IsActive = GL_FALSE;
706 menu->ActiveEntry = NULL;
708 fgState.ActiveMenus--;
710 /* Hide all submenu windows, and the root menu's window. */
711 for ( menuEntry = ( SFG_MenuEntry * )menu->Entries.First;
713 menuEntry = ( SFG_MenuEntry * )menuEntry->Node.Next )
715 menuEntry->IsActive = GL_FALSE;
717 /* Is that an active submenu by any case? */
718 if( menuEntry->SubMenu )
719 fghDeactivateSubMenu( menuEntry );
722 fgSetWindow ( parent_window ) ;
726 * Recalculates current menu's box size
728 void fghCalculateMenuBoxSize( void )
730 SFG_MenuEntry* menuEntry;
731 int width = 0, height = 0;
733 /* Make sure there is a current menu set */
734 freeglut_return_if_fail( fgStructure.CurrentMenu );
736 /* The menu's box size depends on the menu entries: */
737 for( menuEntry = ( SFG_MenuEntry * )fgStructure.CurrentMenu->Entries.First;
739 menuEntry = ( SFG_MenuEntry * )menuEntry->Node.Next )
741 /* Update the menu entry's width value */
742 menuEntry->Width = glutBitmapLength(
744 (unsigned char *)menuEntry->Text
748 * If the entry is a submenu, then it needs to be wider to
749 * accomodate the arrow. JCJ 31 July 2003
751 if (menuEntry->SubMenu )
752 menuEntry->Width += glutBitmapLength(
757 /* Check if it's the biggest we've found */
758 if( menuEntry->Width > width )
759 width = menuEntry->Width;
761 height += FREEGLUT_MENU_HEIGHT;
764 /* Store the menu's box size now: */
765 fgStructure.CurrentMenu->Height = height + 2 * FREEGLUT_MENU_BORDER;
766 fgStructure.CurrentMenu->Width = width + 4 * FREEGLUT_MENU_BORDER;
770 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
773 * Creates a new menu object, adding it to the freeglut structure
775 int FGAPIENTRY glutCreateMenu( void(* callback)( int ) )
777 /* The menu object creation code resides in freeglut_structure.c */
778 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutCreateMenu" );
779 return fgCreateMenu( callback )->ID;
782 #if TARGET_HOST_MS_WINDOWS
783 int FGAPIENTRY __glutCreateMenuWithExit( void(* callback)( int ), void (__cdecl *exit_function)(int) )
785 __glutExitFunc = exit_function;
786 return glutCreateMenu( callback );
791 * Destroys a menu object, removing all references to it
793 void FGAPIENTRY glutDestroyMenu( int menuID )
797 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutDestroyMenu" );
798 menu = fgMenuByID( menuID );
800 freeglut_return_if_fail( menu );
802 /* The menu object destruction code resides in freeglut_structure.c */
803 fgDestroyMenu( menu );
807 * Returns the ID number of the currently active menu
809 int FGAPIENTRY glutGetMenu( void )
811 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGetMenu" );
813 if( fgStructure.CurrentMenu )
814 return fgStructure.CurrentMenu->ID;
820 * Sets the current menu given its menu ID
822 void FGAPIENTRY glutSetMenu( int menuID )
826 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetMenu" );
827 menu = fgMenuByID( menuID );
829 freeglut_return_if_fail( menu );
831 fgStructure.CurrentMenu = menu;
835 * Adds a menu entry to the bottom of the current menu
837 void FGAPIENTRY glutAddMenuEntry( const char* label, int value )
839 SFG_MenuEntry* menuEntry;
840 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutAddMenuEntry" );
841 menuEntry = (SFG_MenuEntry *)calloc( sizeof(SFG_MenuEntry), 1 );
842 freeglut_return_if_fail( fgStructure.CurrentMenu );
844 menuEntry->Text = strdup( label );
845 menuEntry->ID = value;
847 /* Have the new menu entry attached to the current menu */
848 fgListAppend( &fgStructure.CurrentMenu->Entries, &menuEntry->Node );
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;
861 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutAddSubMenu" );
862 menuEntry = ( SFG_MenuEntry * )calloc( sizeof( SFG_MenuEntry ), 1 );
863 subMenu = fgMenuByID( subMenuID );
865 freeglut_return_if_fail( fgStructure.CurrentMenu );
866 freeglut_return_if_fail( subMenu );
868 menuEntry->Text = strdup( label );
869 menuEntry->SubMenu = subMenu;
872 fgListAppend( &fgStructure.CurrentMenu->Entries, &menuEntry->Node );
873 fghCalculateMenuBoxSize( );
877 * Changes the specified menu item in the current menu into a menu entry
879 void FGAPIENTRY glutChangeToMenuEntry( int item, const char* label, int value )
881 SFG_MenuEntry* menuEntry = NULL;
883 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutChangeToMenuEntry" );
884 freeglut_return_if_fail( fgStructure.CurrentMenu );
886 /* Get n-th menu entry in the current menu, starting from one: */
887 menuEntry = fghFindMenuEntry( fgStructure.CurrentMenu, item );
889 freeglut_return_if_fail( menuEntry );
891 /* We want it to become a normal menu entry, so: */
892 if( menuEntry->Text )
893 free( menuEntry->Text );
895 menuEntry->Text = strdup( label );
896 menuEntry->ID = value;
897 menuEntry->SubMenu = NULL;
898 fghCalculateMenuBoxSize( );
902 * Changes the specified menu item in the current menu into a sub-menu trigger.
904 void FGAPIENTRY glutChangeToSubMenu( int item, const char* label,
908 SFG_MenuEntry* menuEntry;
910 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutChangeToSubMenu" );
911 subMenu = fgMenuByID( subMenuID );
914 freeglut_return_if_fail( fgStructure.CurrentMenu );
915 freeglut_return_if_fail( subMenu );
917 /* Get n-th menu entry in the current menu, starting from one: */
918 menuEntry = fghFindMenuEntry( fgStructure.CurrentMenu, item );
920 freeglut_return_if_fail( menuEntry );
922 /* We want it to become a sub menu entry, so: */
923 if( menuEntry->Text )
924 free( menuEntry->Text );
926 menuEntry->Text = strdup( label );
927 menuEntry->SubMenu = subMenu;
929 fghCalculateMenuBoxSize( );
933 * Removes the specified menu item from the current menu
935 void FGAPIENTRY glutRemoveMenuItem( int item )
937 SFG_MenuEntry* menuEntry;
939 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutRemoveMenuItem" );
940 freeglut_return_if_fail( fgStructure.CurrentMenu );
942 /* Get n-th menu entry in the current menu, starting from one: */
943 menuEntry = fghFindMenuEntry( fgStructure.CurrentMenu, item );
945 freeglut_return_if_fail( menuEntry );
947 fgListRemove( &fgStructure.CurrentMenu->Entries, &menuEntry->Node );
948 if ( menuEntry->Text )
949 free( menuEntry->Text );
952 fghCalculateMenuBoxSize( );
956 * Attaches a menu to the current window
958 void FGAPIENTRY glutAttachMenu( int button )
960 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutAttachMenu" );
962 freeglut_return_if_fail( fgStructure.CurrentWindow );
963 freeglut_return_if_fail( fgStructure.CurrentMenu );
965 freeglut_return_if_fail( button >= 0 );
966 freeglut_return_if_fail( button < FREEGLUT_MAX_MENUS );
968 fgStructure.CurrentWindow->Menu[ button ] = fgStructure.CurrentMenu;
972 * Detaches a menu from the current window
974 void FGAPIENTRY glutDetachMenu( int button )
976 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutDetachMenu" );
978 freeglut_return_if_fail( fgStructure.CurrentWindow );
979 freeglut_return_if_fail( fgStructure.CurrentMenu );
981 freeglut_return_if_fail( button >= 0 );
982 freeglut_return_if_fail( button < FREEGLUT_MAX_MENUS );
984 fgStructure.CurrentWindow->Menu[ button ] = NULL;
988 * A.Donev: Set and retrieve the menu's user data
990 void* FGAPIENTRY glutGetMenuData( void )
992 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGetMenuData" );
993 return fgStructure.CurrentMenu->UserData;
996 void FGAPIENTRY glutSetMenuData(void* data)
998 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetMenuData" );
999 fgStructure.CurrentMenu->UserData=data;
1002 /*** END OF FILE ***/