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 #include <GL/freeglut.h>
29 #include "freeglut_internal.h"
31 /* -- DEFINITIONS ---------------------------------------------------------- */
34 * FREEGLUT_MENU_FONT can be any freeglut bitmapped font.
35 * (Stroked fonts would not be out of the question, but we'd need to alter
36 * code, since GLUT (hence freeglut) does not quite unify stroked and
37 * bitmapped font handling.)
38 * Old UNIX/X11 GLUT (BSD, UNIX, IRIX, LINUX, HPUX, ...) used a system
39 * font best approximated by an 18-pixel HELVETICA, I think. MS-WINDOWS
40 * GLUT used something closest to the 8x13 fixed-width font. (Old
41 * GLUT apparently uses host-system menus rather than building its own.
42 * freeglut is building its own menus from scratch.)
44 * FREEGLUT_MENU_HEIGHT gives the height of ONE menu box. This should be
45 * the distances between two adjacent menu entries. It should scale
46 * automatically with the font choice, so you needn't alter it---unless you
49 * FREEGLUT_MENU_BORDER says how many pixels to allow around the edge of a
50 * menu. (It also seems to be the same as the number of pixels used as
51 * a border around *items* to separate them from neighbors. John says
52 * that that wasn't the original intent...if not, perhaps we need another
53 * symbolic constant, FREEGLUT_MENU_ITEM_BORDER, or such.)
55 #if TARGET_HOST_WIN32 || TARGET_HOST_WINCE
56 #define FREEGLUT_MENU_FONT GLUT_BITMAP_8_BY_13
58 #define FREEGLUT_MENU_FONT GLUT_BITMAP_HELVETICA_18
61 #define FREEGLUT_MENU_HEIGHT (glutBitmapHeight(FREEGLUT_MENU_FONT) + \
63 #define FREEGLUT_MENU_BORDER 2
67 * These variables are for rendering the freeglut menu items.
69 * The choices are fore- and background, with and without h for Highlighting.
70 * Old GLUT appeared to be system-dependant for its colors (sigh) so we are
71 * too. These variables should be stuffed into global state and initialized
72 * via the glutInit*() system.
74 #if TARGET_HOST_WIN32 || TARGET_HOST_WINCE
75 static float menu_pen_fore [4] = {0.0f, 0.0f, 0.0f, 1.0f};
76 static float menu_pen_back [4] = {0.85f, 0.85f, 0.85f, 1.0f};
77 static float menu_pen_hfore [4] = {1.0f, 1.0f, 1.0f, 1.0f};
78 static float menu_pen_hback [4] = {0.15f, 0.15f, 0.45f, 1.0f};
80 static float menu_pen_fore [4] = {0.0f, 0.0f, 0.0f, 1.0f};
81 static float menu_pen_back [4] = {0.70f, 0.70f, 0.70f, 1.0f};
82 static float menu_pen_hfore [4] = {0.0f, 0.0f, 0.0f, 1.0f};
83 static float menu_pen_hback [4] = {1.0f, 1.0f, 1.0f, 1.0f};
86 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */
89 * Private function to find a menu entry by index
91 static SFG_MenuEntry *fghFindMenuEntry( SFG_Menu* menu, int index )
96 for( entry = (SFG_MenuEntry *)menu->Entries.First;
98 entry = (SFG_MenuEntry *)entry->Node.Next )
109 * Deactivates a menu pointed by the function argument.
111 static void fghDeactivateSubMenu( SFG_MenuEntry *menuEntry )
113 SFG_MenuEntry *subMenuIter;
114 /* Hide the present menu's window */
115 fgSetWindow( menuEntry->SubMenu->Window );
118 /* Forget about having that menu active anymore, now: */
119 menuEntry->SubMenu->Window->ActiveMenu = NULL;
120 menuEntry->SubMenu->IsActive = GL_FALSE;
121 menuEntry->SubMenu->ActiveEntry = NULL;
123 /* Hide all submenu windows, and the root menu's window. */
124 for ( subMenuIter = (SFG_MenuEntry *)menuEntry->SubMenu->Entries.First;
126 subMenuIter = (SFG_MenuEntry *)subMenuIter->Node.Next )
128 subMenuIter->IsActive = GL_FALSE;
130 /* Is that an active submenu by any case? */
131 if( subMenuIter->SubMenu )
132 fghDeactivateSubMenu( subMenuIter );
135 fgSetWindow ( menuEntry->SubMenu->ParentWindow ) ;
139 * Private function to get the virtual maximum screen extent
141 static GLvoid fghGetVMaxExtent( SFG_Window* window, int* x, int* y )
143 if( fgStructure.GameMode )
145 #if TARGET_HOST_UNIX_X11
149 XTranslateCoordinates(
151 window->Window.Handle,
152 fgDisplay.RootWindow,
155 *x = fgState.GameModeSize.X + wx;
156 *y = fgState.GameModeSize.Y + wy;
158 *x = glutGet ( GLUT_SCREEN_WIDTH );
159 *y = glutGet ( GLUT_SCREEN_HEIGHT );
164 *x = fgDisplay.ScreenWidth;
165 *y = fgDisplay.ScreenHeight;
170 * Private function to check for the current menu/sub menu activity state
172 static GLboolean fghCheckMenuStatus( SFG_Menu* menu )
174 SFG_MenuEntry* menuEntry;
177 /* First of all check any of the active sub menus... */
178 for( menuEntry = (SFG_MenuEntry *)menu->Entries.First;
180 menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next )
182 if( menuEntry->SubMenu && menuEntry->IsActive )
185 * OK, have the sub-menu checked, too. If it returns GL_TRUE, it
186 * will mean that it caught the mouse cursor and we do not need
187 * to regenerate the activity list, and so our parents do...
189 GLboolean return_status;
191 menuEntry->SubMenu->Window->State.MouseX =
192 menu->Window->State.MouseX + menu->X - menuEntry->SubMenu->X;
193 menuEntry->SubMenu->Window->State.MouseY =
194 menu->Window->State.MouseY + menu->Y - menuEntry->SubMenu->Y;
195 return_status = fghCheckMenuStatus( menuEntry->SubMenu );
202 /* That much about our sub menus, let's get to checking the current menu: */
203 x = menu->Window->State.MouseX;
204 y = menu->Window->State.MouseY;
206 /* Check if the mouse cursor is contained within the current menu box */
207 if( ( x >= FREEGLUT_MENU_BORDER ) &&
208 ( x < menu->Width - FREEGLUT_MENU_BORDER ) &&
209 ( y >= FREEGLUT_MENU_BORDER ) &&
210 ( y < menu->Height - FREEGLUT_MENU_BORDER ) )
212 int menuID = ( y - FREEGLUT_MENU_BORDER ) / FREEGLUT_MENU_HEIGHT;
214 /* The mouse cursor is somewhere over our box, check it out. */
215 menuEntry = fghFindMenuEntry( menu, menuID + 1 );
216 FREEGLUT_INTERNAL_ERROR_EXIT( menuEntry, "Cannot find menu entry",
217 "fghCheckMenuStatus" );
219 menuEntry->IsActive = GL_TRUE;
220 menuEntry->Ordinal = menuID;
223 * If this is not the same as the last active menu entry, deactivate
224 * the previous entry. Specifically, if the previous active entry
225 * was a submenu then deactivate it.
227 if( menu->ActiveEntry && ( menuEntry != menu->ActiveEntry ) )
228 if( menu->ActiveEntry->SubMenu )
229 fghDeactivateSubMenu( menu->ActiveEntry );
231 if( menuEntry != menu->ActiveEntry )
233 menu->Window->State.Redisplay = GL_TRUE;
234 if( menu->ActiveEntry )
235 menu->ActiveEntry->IsActive = GL_FALSE;
238 menu->ActiveEntry = menuEntry;
239 menu->IsActive = GL_TRUE; /* XXX Do we need this? */
242 * OKi, we have marked that entry as active, but it would be also
243 * nice to have its contents updated, in case it's a sub menu.
244 * Also, ignore the return value of the check function:
246 if( menuEntry->SubMenu )
248 if ( ! menuEntry->SubMenu->IsActive )
251 SFG_Window *current_window = fgStructure.CurrentWindow;
253 /* Set up the initial menu position now... */
254 menuEntry->SubMenu->IsActive = GL_TRUE;
256 /* Set up the initial submenu position now: */
257 fghGetVMaxExtent(menu->ParentWindow, &max_x, &max_y);
258 menuEntry->SubMenu->X = menu->X + menu->Width;
259 menuEntry->SubMenu->Y = menu->Y +
260 menuEntry->Ordinal * FREEGLUT_MENU_HEIGHT;
262 if( menuEntry->SubMenu->X + menuEntry->SubMenu->Width > max_x )
263 menuEntry->SubMenu->X = menu->X - menuEntry->SubMenu->Width;
265 if( menuEntry->SubMenu->Y + menuEntry->SubMenu->Height > max_y )
266 menuEntry->SubMenu->Y -= ( menuEntry->SubMenu->Height -
267 FREEGLUT_MENU_HEIGHT -
268 2 * FREEGLUT_MENU_BORDER );
270 fgSetWindow( menuEntry->SubMenu->Window );
271 glutPositionWindow( menuEntry->SubMenu->X,
272 menuEntry->SubMenu->Y );
273 glutReshapeWindow( menuEntry->SubMenu->Width,
274 menuEntry->SubMenu->Height );
277 menuEntry->SubMenu->Window->ActiveMenu = menuEntry->SubMenu;
278 fgSetWindow( current_window );
279 menuEntry->SubMenu->Window->State.MouseX =
280 x + menu->X - menuEntry->SubMenu->X;
281 menuEntry->SubMenu->Window->State.MouseY =
282 y + menu->Y - menuEntry->SubMenu->Y;
283 fghCheckMenuStatus( menuEntry->SubMenu );
286 /* Activate it because its parent entry is active */
287 menuEntry->SubMenu->IsActive = GL_TRUE; /* XXX Do we need this? */
290 /* Report back that we have caught the menu cursor */
294 /* Looks like the menu cursor is somewhere else... */
295 if( menu->ActiveEntry && menu->ActiveEntry->IsActive &&
296 ( !menu->ActiveEntry->SubMenu ||
297 !menu->ActiveEntry->SubMenu->IsActive ) )
299 menu->Window->State.Redisplay = GL_TRUE;
300 menu->ActiveEntry->IsActive = GL_FALSE;
301 menu->ActiveEntry = NULL;
308 * Displays a menu box and all of its submenus (if they are active)
310 static void fghDisplayMenuBox( SFG_Menu* menu )
312 SFG_MenuEntry *menuEntry;
314 int border = FREEGLUT_MENU_BORDER;
317 * Have the menu box drawn first. The +- values are
318 * here just to make it more nice-looking...
320 /* a non-black dark version of the below. */
321 glColor4f( 1.0f, 1.0f, 1.0f, 1.0f );
322 glBegin( GL_QUAD_STRIP );
323 glVertex2i( menu->Width , 0 );
324 glVertex2i( menu->Width - border, border);
326 glVertex2i( border, border);
327 glVertex2i( 0 , menu->Height );
328 glVertex2i( border, menu->Height - border);
331 /* a non-black dark version of the below. */
332 glColor4f( 0.5f, 0.5f, 0.5f, 1.0f );
333 glBegin( GL_QUAD_STRIP );
334 glVertex2i( 0 , menu->Height );
335 glVertex2i( border, menu->Height - border);
336 glVertex2i( menu->Width , menu->Height );
337 glVertex2i( menu->Width - border, menu->Height - border);
338 glVertex2i( menu->Width , 0 );
339 glVertex2i( menu->Width - border, border);
342 glColor4fv( menu_pen_back );
344 glVertex2i( border, border);
345 glVertex2i( menu->Width - border, border);
346 glVertex2i( menu->Width - border, menu->Height - border);
347 glVertex2i( border, menu->Height - border);
350 /* Check if any of the submenus is currently active... */
351 for( menuEntry = (SFG_MenuEntry *)menu->Entries.First;
353 menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next )
355 /* Has the menu been marked as active, maybe? */
356 if( menuEntry->IsActive )
359 * That's truly right, and we need to have it highlighted.
360 * There is an assumption that mouse cursor didn't move
361 * since the last check of menu activity state:
363 int menuID = menuEntry->Ordinal;
365 /* So have the highlight drawn... */
366 glColor4fv( menu_pen_hback );
369 (menuID + 0)*FREEGLUT_MENU_HEIGHT + border );
370 glVertex2i( menu->Width - border,
371 (menuID + 0)*FREEGLUT_MENU_HEIGHT + border );
372 glVertex2i( menu->Width - border,
373 (menuID + 1)*FREEGLUT_MENU_HEIGHT + border );
375 (menuID + 1)*FREEGLUT_MENU_HEIGHT + border );
380 /* Print the menu entries now... */
382 glColor4fv( menu_pen_fore );
384 for( menuEntry = (SFG_MenuEntry *)menu->Entries.First, i = 0;
386 menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next, ++i )
388 /* If the menu entry is active, set the color to white */
389 if( menuEntry->IsActive )
390 glColor4fv( menu_pen_hfore );
392 /* Move the raster into position... */
393 /* Try to center the text - JCJ 31 July 2003*/
396 ( i + 1 )*FREEGLUT_MENU_HEIGHT -
397 ( int )( FREEGLUT_MENU_HEIGHT*0.3 - border )
400 /* Have the label drawn, character after character: */
401 glutBitmapString( FREEGLUT_MENU_FONT,
402 (unsigned char *)menuEntry->Text);
404 /* If it's a submenu, draw a right arrow */
405 if( menuEntry->SubMenu )
407 int width = glutBitmapWidth( FREEGLUT_MENU_FONT, '_' );
408 int x_base = menu->Width - 2 - width;
409 int y_base = i*FREEGLUT_MENU_HEIGHT + border;
410 glBegin( GL_TRIANGLES );
411 glVertex2i( x_base, y_base + 2*border);
412 glVertex2i( menu->Width - 2, y_base +
413 ( FREEGLUT_MENU_HEIGHT + border) / 2 );
414 glVertex2i( x_base, y_base + FREEGLUT_MENU_HEIGHT - border );
418 /* If the menu entry is active, reset the color */
419 if( menuEntry->IsActive )
420 glColor4fv( menu_pen_fore );
425 * Private static function to set the parent window of a submenu and all
428 static void fghSetMenuParentWindow( SFG_Window *window, SFG_Menu *menu )
430 SFG_MenuEntry *menuEntry;
432 menu->ParentWindow = window;
434 for( menuEntry = ( SFG_MenuEntry * )menu->Entries.First;
436 menuEntry = ( SFG_MenuEntry * )menuEntry->Node.Next )
437 if( menuEntry->SubMenu )
438 fghSetMenuParentWindow( window, menuEntry->SubMenu );
442 * Function to check for menu entry selection on menu deactivation
444 static void fghExecuteMenuCallback( SFG_Menu* menu )
446 SFG_MenuEntry *menuEntry;
448 /* First of all check any of the active sub menus... */
449 for( menuEntry = (SFG_MenuEntry *)menu->Entries.First;
451 menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next)
453 if( menuEntry->IsActive )
455 if( menuEntry->SubMenu )
456 fghExecuteMenuCallback( menuEntry->SubMenu );
460 SFG_Menu *save_menu = fgStructure.CurrentMenu;
461 fgStructure.CurrentMenu = menu;
462 menu->Callback( menuEntry->ID );
463 fgStructure.CurrentMenu = save_menu;
473 * Displays the currently active menu for the current window
475 void fgDisplayMenu( void )
477 SFG_Window* window = fgStructure.CurrentWindow;
478 SFG_Menu* menu = NULL;
480 FREEGLUT_INTERNAL_ERROR_EXIT ( fgStructure.CurrentWindow, "Displaying menu in nonexistent window",
483 /* Check if there is an active menu attached to this window... */
484 menu = window->ActiveMenu;
485 freeglut_return_if_fail( menu );
487 fgSetWindow( menu->Window );
489 glPushAttrib( GL_DEPTH_BUFFER_BIT | GL_TEXTURE_BIT | GL_LIGHTING_BIT |
492 glDisable( GL_DEPTH_TEST );
493 glDisable( GL_TEXTURE_2D );
494 glDisable( GL_LIGHTING );
495 glDisable( GL_CULL_FACE );
497 glMatrixMode( GL_PROJECTION );
501 0, glutGet( GLUT_WINDOW_WIDTH ),
502 glutGet( GLUT_WINDOW_HEIGHT ), 0,
506 glMatrixMode( GL_MODELVIEW );
510 fghDisplayMenuBox( menu );
514 glMatrixMode( GL_PROJECTION );
516 glMatrixMode( GL_MODELVIEW );
521 fgSetWindow ( window );
525 * Activates a menu pointed by the function argument
527 static void fghActivateMenu( SFG_Window* window, int button )
531 /* We'll be referencing this menu a lot, so remember its address: */
532 SFG_Menu* menu = window->Menu[ button ];
533 SFG_Window* current_window = fgStructure.CurrentWindow;
535 /* If the menu is already active in another window, deactivate it there */
536 if ( menu->ParentWindow )
537 menu->ParentWindow->ActiveMenu = NULL ;
539 /* Mark the menu as active, so that it gets displayed: */
540 window->ActiveMenu = menu;
541 menu->IsActive = GL_TRUE;
542 fghSetMenuParentWindow ( window, menu );
543 fgState.ActiveMenus++;
545 /* Set up the initial menu position now: */
546 fghGetVMaxExtent(menu->ParentWindow, &max_x, &max_y);
547 fgSetWindow( window );
548 menu->X = window->State.MouseX + glutGet( GLUT_WINDOW_X );
549 menu->Y = window->State.MouseY + glutGet( GLUT_WINDOW_Y );
551 if( menu->X + menu->Width > max_x )
552 menu->X -=menu->Width;
554 if( menu->Y + menu->Height > max_y )
555 menu->Y -=menu->Height;
557 menu->Window->State.MouseX =
558 window->State.MouseX + glutGet( GLUT_WINDOW_X ) - menu->X;
559 menu->Window->State.MouseY =
560 window->State.MouseY + glutGet( GLUT_WINDOW_Y ) - menu->Y;
562 fgSetWindow( menu->Window );
563 glutPositionWindow( menu->X, menu->Y );
564 glutReshapeWindow( menu->Width, menu->Height );
567 menu->Window->ActiveMenu = menu;
568 fghCheckMenuStatus( menu );
569 fgSetWindow( current_window );
573 * Update Highlight states of the menu
575 * Current mouse position is in menu->Window->State.MouseX/Y.
577 void fgUpdateMenuHighlight ( SFG_Menu *menu )
579 fghCheckMenuStatus( menu );
583 * Check whether an active menu absorbs a mouse click
585 GLboolean fgCheckActiveMenu ( SFG_Window *window, int button, GLboolean pressed,
586 int mouse_x, int mouse_y )
589 * Near as I can tell, this is the menu behaviour:
590 * - Down-click the menu button, menu not active: activate
591 * the menu with its upper left-hand corner at the mouse
593 * - Down-click any button outside the menu, menu active:
594 * deactivate the menu
595 * - Down-click any button inside the menu, menu active:
596 * select the menu entry and deactivate the menu
597 * - Up-click the menu button, menu not active: nothing happens
598 * - Up-click the menu button outside the menu, menu active:
600 * - Up-click the menu button inside the menu, menu active:
601 * select the menu entry and deactivate the menu
602 * Since menus can have submenus, we need to check this recursively.
604 if( window->ActiveMenu )
606 if( window == window->ActiveMenu->ParentWindow )
608 window->ActiveMenu->Window->State.MouseX =
609 mouse_x - window->ActiveMenu->X;
610 window->ActiveMenu->Window->State.MouseY =
611 mouse_y - window->ActiveMenu->Y;
614 /* In the menu, invoke the callback and deactivate the menu */
615 if( fghCheckMenuStatus( window->ActiveMenu ) )
618 * Save the current window and menu and set the current
619 * window to the window whose menu this is
621 SFG_Window *save_window = fgStructure.CurrentWindow;
622 SFG_Menu *save_menu = fgStructure.CurrentMenu;
623 SFG_Window *parent_window = window->ActiveMenu->ParentWindow;
624 fgSetWindow( parent_window );
625 fgStructure.CurrentMenu = window->ActiveMenu;
627 /* Execute the menu callback */
628 fghExecuteMenuCallback( window->ActiveMenu );
629 fgDeactivateMenu( parent_window );
631 /* Restore the current window and menu */
632 fgSetWindow( save_window );
633 fgStructure.CurrentMenu = save_menu;
637 * Outside the menu, deactivate if it's a downclick
639 * XXX This isn't enough. A downclick outside of
640 * XXX the interior of our freeglut windows should also
641 * XXX deactivate the menu. This is more complicated.
643 fgDeactivateMenu( window->ActiveMenu->ParentWindow );
646 * XXX Why does an active menu require a redisplay at
647 * XXX this point? If this can come out cleanly, then
648 * XXX it probably should do so; if not, a comment should
651 if( ! window->IsMenu )
652 window->State.Redisplay = GL_TRUE;
657 /* No active menu, let's check whether we need to activate one. */
658 if( ( 0 <= button ) &&
659 ( FREEGLUT_MAX_MENUS > button ) &&
660 ( window->Menu[ button ] ) &&
663 /* XXX Posting a requisite Redisplay seems bogus. */
664 window->State.Redisplay = GL_TRUE;
665 fghActivateMenu( window, button );
673 * Deactivates a menu pointed by the function argument.
675 void fgDeactivateMenu( SFG_Window *window )
677 SFG_Window *parent_window = NULL;
679 /* Check if there is an active menu attached to this window... */
680 SFG_Menu* menu = window->ActiveMenu;
681 SFG_MenuEntry *menuEntry;
683 /* Did we find an active window? */
684 freeglut_return_if_fail( menu );
686 parent_window = menu->ParentWindow;
688 /* Hide the present menu's window */
689 fgSetWindow( menu->Window );
692 /* Forget about having that menu active anymore, now: */
693 menu->Window->ActiveMenu = NULL;
694 menu->ParentWindow->ActiveMenu = NULL;
695 fghSetMenuParentWindow ( NULL, menu );
696 menu->IsActive = GL_FALSE;
697 menu->ActiveEntry = NULL;
699 fgState.ActiveMenus--;
701 /* Hide all submenu windows, and the root menu's window. */
702 for ( menuEntry = ( SFG_MenuEntry * )menu->Entries.First;
704 menuEntry = ( SFG_MenuEntry * )menuEntry->Node.Next )
706 menuEntry->IsActive = GL_FALSE;
708 /* Is that an active submenu by any case? */
709 if( menuEntry->SubMenu )
710 fghDeactivateSubMenu( menuEntry );
713 fgSetWindow ( parent_window ) ;
717 * Recalculates current menu's box size
719 void fghCalculateMenuBoxSize( void )
721 SFG_MenuEntry* menuEntry;
722 int width = 0, height = 0;
724 /* Make sure there is a current menu set */
725 freeglut_return_if_fail( fgStructure.CurrentMenu );
727 /* The menu's box size depends on the menu entries: */
728 for( menuEntry = ( SFG_MenuEntry * )fgStructure.CurrentMenu->Entries.First;
730 menuEntry = ( SFG_MenuEntry * )menuEntry->Node.Next )
732 /* Update the menu entry's width value */
733 menuEntry->Width = glutBitmapLength(
735 (unsigned char *)menuEntry->Text
739 * If the entry is a submenu, then it needs to be wider to
740 * accomodate the arrow. JCJ 31 July 2003
742 if (menuEntry->SubMenu )
743 menuEntry->Width += glutBitmapLength(
748 /* Check if it's the biggest we've found */
749 if( menuEntry->Width > width )
750 width = menuEntry->Width;
752 height += FREEGLUT_MENU_HEIGHT;
755 /* Store the menu's box size now: */
756 fgStructure.CurrentMenu->Height = height + 2 * FREEGLUT_MENU_BORDER;
757 fgStructure.CurrentMenu->Width = width + 4 * FREEGLUT_MENU_BORDER;
761 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
764 * Creates a new menu object, adding it to the freeglut structure
766 int FGAPIENTRY glutCreateMenu( void(* callback)( int ) )
768 /* The menu object creation code resides in freeglut_structure.c */
769 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutCreateMenu" );
770 return fgCreateMenu( callback )->ID;
774 * Destroys a menu object, removing all references to it
776 void FGAPIENTRY glutDestroyMenu( int menuID )
780 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutDestroyMenu" );
781 menu = fgMenuByID( menuID );
783 freeglut_return_if_fail( menu );
785 /* The menu object destruction code resides in freeglut_structure.c */
786 fgDestroyMenu( menu );
790 * Returns the ID number of the currently active menu
792 int FGAPIENTRY glutGetMenu( void )
794 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGetMenu" );
796 if( fgStructure.CurrentMenu )
797 return fgStructure.CurrentMenu->ID;
803 * Sets the current menu given its menu ID
805 void FGAPIENTRY glutSetMenu( int menuID )
809 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetMenu" );
810 menu = fgMenuByID( menuID );
812 freeglut_return_if_fail( menu );
814 fgStructure.CurrentMenu = menu;
818 * Adds a menu entry to the bottom of the current menu
820 void FGAPIENTRY glutAddMenuEntry( const char* label, int value )
822 SFG_MenuEntry* menuEntry;
823 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutAddMenuEntry" );
824 menuEntry = (SFG_MenuEntry *)calloc( sizeof(SFG_MenuEntry), 1 );
825 freeglut_return_if_fail( fgStructure.CurrentMenu );
827 menuEntry->Text = strdup( label );
828 menuEntry->ID = value;
830 /* Have the new menu entry attached to the current menu */
831 fgListAppend( &fgStructure.CurrentMenu->Entries, &menuEntry->Node );
833 fghCalculateMenuBoxSize( );
837 * Add a sub menu to the bottom of the current menu
839 void FGAPIENTRY glutAddSubMenu( const char *label, int subMenuID )
841 SFG_MenuEntry *menuEntry;
844 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutAddSubMenu" );
845 menuEntry = ( SFG_MenuEntry * )calloc( sizeof( SFG_MenuEntry ), 1 );
846 subMenu = fgMenuByID( subMenuID );
848 freeglut_return_if_fail( fgStructure.CurrentMenu );
849 freeglut_return_if_fail( subMenu );
851 menuEntry->Text = strdup( label );
852 menuEntry->SubMenu = subMenu;
855 fgListAppend( &fgStructure.CurrentMenu->Entries, &menuEntry->Node );
856 fghCalculateMenuBoxSize( );
860 * Changes the specified menu item in the current menu into a menu entry
862 void FGAPIENTRY glutChangeToMenuEntry( int item, const char* label, int value )
864 SFG_MenuEntry* menuEntry = NULL;
866 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutChangeToMenuEntry" );
867 freeglut_return_if_fail( fgStructure.CurrentMenu );
869 /* Get n-th menu entry in the current menu, starting from one: */
870 menuEntry = fghFindMenuEntry( fgStructure.CurrentMenu, item );
872 freeglut_return_if_fail( menuEntry );
874 /* We want it to become a normal menu entry, so: */
875 if( menuEntry->Text )
876 free( menuEntry->Text );
878 menuEntry->Text = strdup( label );
879 menuEntry->ID = value;
880 menuEntry->SubMenu = NULL;
881 fghCalculateMenuBoxSize( );
885 * Changes the specified menu item in the current menu into a sub-menu trigger.
887 void FGAPIENTRY glutChangeToSubMenu( int item, const char* label,
891 SFG_MenuEntry* menuEntry;
893 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutChangeToSubMenu" );
894 subMenu = fgMenuByID( subMenuID );
897 freeglut_return_if_fail( fgStructure.CurrentMenu );
898 freeglut_return_if_fail( subMenu );
900 /* Get n-th menu entry in the current menu, starting from one: */
901 menuEntry = fghFindMenuEntry( fgStructure.CurrentMenu, item );
903 freeglut_return_if_fail( menuEntry );
905 /* We want it to become a sub menu entry, so: */
906 if( menuEntry->Text )
907 free( menuEntry->Text );
909 menuEntry->Text = strdup( label );
910 menuEntry->SubMenu = subMenu;
912 fghCalculateMenuBoxSize( );
916 * Removes the specified menu item from the current menu
918 void FGAPIENTRY glutRemoveMenuItem( int item )
920 SFG_MenuEntry* menuEntry;
922 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutRemoveMenuItem" );
923 freeglut_return_if_fail( fgStructure.CurrentMenu );
925 /* Get n-th menu entry in the current menu, starting from one: */
926 menuEntry = fghFindMenuEntry( fgStructure.CurrentMenu, item );
928 freeglut_return_if_fail( menuEntry );
930 fgListRemove( &fgStructure.CurrentMenu->Entries, &menuEntry->Node );
931 if ( menuEntry->Text )
932 free( menuEntry->Text );
935 fghCalculateMenuBoxSize( );
939 * Attaches a menu to the current window
941 void FGAPIENTRY glutAttachMenu( int button )
943 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutAttachMenu" );
945 freeglut_return_if_fail( fgStructure.CurrentWindow );
946 freeglut_return_if_fail( fgStructure.CurrentMenu );
948 freeglut_return_if_fail( button >= 0 );
949 freeglut_return_if_fail( button < FREEGLUT_MAX_MENUS );
951 fgStructure.CurrentWindow->Menu[ button ] = fgStructure.CurrentMenu;
955 * Detaches a menu from the current window
957 void FGAPIENTRY glutDetachMenu( int button )
959 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutDetachMenu" );
961 freeglut_return_if_fail( fgStructure.CurrentWindow );
962 freeglut_return_if_fail( fgStructure.CurrentMenu );
964 freeglut_return_if_fail( button >= 0 );
965 freeglut_return_if_fail( button < FREEGLUT_MAX_MENUS );
967 fgStructure.CurrentWindow->Menu[ button ] = NULL;
971 * A.Donev: Set and retrieve the menu's user data
973 void* FGAPIENTRY glutGetMenuData( void )
975 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGetMenuData" );
976 return fgStructure.CurrentMenu->UserData;
979 void FGAPIENTRY glutSetMenuData(void* data)
981 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetMenuData" );
982 fgStructure.CurrentMenu->UserData=data;
985 /*** END OF FILE ***/