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_MS_WINDOWS
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_MS_WINDOWS
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.GameModeWindow )
145 #if TARGET_HOST_POSIX_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 )
267 menuEntry->SubMenu->Y -= ( menuEntry->SubMenu->Height -
268 FREEGLUT_MENU_HEIGHT -
269 2 * FREEGLUT_MENU_BORDER );
270 if( menuEntry->SubMenu->Y < 0 )
271 menuEntry->SubMenu->Y = 0;
274 fgSetWindow( menuEntry->SubMenu->Window );
275 glutPositionWindow( menuEntry->SubMenu->X,
276 menuEntry->SubMenu->Y );
277 glutReshapeWindow( menuEntry->SubMenu->Width,
278 menuEntry->SubMenu->Height );
281 menuEntry->SubMenu->Window->ActiveMenu = menuEntry->SubMenu;
282 fgSetWindow( current_window );
283 menuEntry->SubMenu->Window->State.MouseX =
284 x + menu->X - menuEntry->SubMenu->X;
285 menuEntry->SubMenu->Window->State.MouseY =
286 y + menu->Y - menuEntry->SubMenu->Y;
287 fghCheckMenuStatus( menuEntry->SubMenu );
290 /* Activate it because its parent entry is active */
291 menuEntry->SubMenu->IsActive = GL_TRUE; /* XXX Do we need this? */
294 /* Report back that we have caught the menu cursor */
298 /* Looks like the menu cursor is somewhere else... */
299 if( menu->ActiveEntry && menu->ActiveEntry->IsActive &&
300 ( !menu->ActiveEntry->SubMenu ||
301 !menu->ActiveEntry->SubMenu->IsActive ) )
303 menu->Window->State.Redisplay = GL_TRUE;
304 menu->ActiveEntry->IsActive = GL_FALSE;
305 menu->ActiveEntry = NULL;
312 * Displays a menu box and all of its submenus (if they are active)
314 static void fghDisplayMenuBox( SFG_Menu* menu )
316 SFG_MenuEntry *menuEntry;
318 int border = FREEGLUT_MENU_BORDER;
321 * Have the menu box drawn first. The +- values are
322 * here just to make it more nice-looking...
324 /* a non-black dark version of the below. */
325 glColor4f( 1.0f, 1.0f, 1.0f, 1.0f );
326 glBegin( GL_QUAD_STRIP );
327 glVertex2i( menu->Width , 0 );
328 glVertex2i( menu->Width - border, border);
330 glVertex2i( border, border);
331 glVertex2i( 0 , menu->Height );
332 glVertex2i( border, menu->Height - border);
335 /* a non-black dark version of the below. */
336 glColor4f( 0.5f, 0.5f, 0.5f, 1.0f );
337 glBegin( GL_QUAD_STRIP );
338 glVertex2i( 0 , menu->Height );
339 glVertex2i( border, menu->Height - border);
340 glVertex2i( menu->Width , menu->Height );
341 glVertex2i( menu->Width - border, menu->Height - border);
342 glVertex2i( menu->Width , 0 );
343 glVertex2i( menu->Width - border, border);
346 glColor4fv( menu_pen_back );
348 glVertex2i( border, border);
349 glVertex2i( menu->Width - border, border);
350 glVertex2i( menu->Width - border, menu->Height - border);
351 glVertex2i( border, menu->Height - border);
354 /* Check if any of the submenus is currently active... */
355 for( menuEntry = (SFG_MenuEntry *)menu->Entries.First;
357 menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next )
359 /* Has the menu been marked as active, maybe? */
360 if( menuEntry->IsActive )
363 * That's truly right, and we need to have it highlighted.
364 * There is an assumption that mouse cursor didn't move
365 * since the last check of menu activity state:
367 int menuID = menuEntry->Ordinal;
369 /* So have the highlight drawn... */
370 glColor4fv( menu_pen_hback );
373 (menuID + 0)*FREEGLUT_MENU_HEIGHT + border );
374 glVertex2i( menu->Width - border,
375 (menuID + 0)*FREEGLUT_MENU_HEIGHT + border );
376 glVertex2i( menu->Width - border,
377 (menuID + 1)*FREEGLUT_MENU_HEIGHT + border );
379 (menuID + 1)*FREEGLUT_MENU_HEIGHT + border );
384 /* Print the menu entries now... */
386 glColor4fv( menu_pen_fore );
388 for( menuEntry = (SFG_MenuEntry *)menu->Entries.First, i = 0;
390 menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next, ++i )
392 /* If the menu entry is active, set the color to white */
393 if( menuEntry->IsActive )
394 glColor4fv( menu_pen_hfore );
396 /* Move the raster into position... */
397 /* Try to center the text - JCJ 31 July 2003*/
400 ( i + 1 )*FREEGLUT_MENU_HEIGHT -
401 ( int )( FREEGLUT_MENU_HEIGHT*0.3 - border )
404 /* Have the label drawn, character after character: */
405 glutBitmapString( FREEGLUT_MENU_FONT,
406 (unsigned char *)menuEntry->Text);
408 /* If it's a submenu, draw a right arrow */
409 if( menuEntry->SubMenu )
411 int width = glutBitmapWidth( FREEGLUT_MENU_FONT, '_' );
412 int x_base = menu->Width - 2 - width;
413 int y_base = i*FREEGLUT_MENU_HEIGHT + border;
414 glBegin( GL_TRIANGLES );
415 glVertex2i( x_base, y_base + 2*border);
416 glVertex2i( menu->Width - 2, y_base +
417 ( FREEGLUT_MENU_HEIGHT + border) / 2 );
418 glVertex2i( x_base, y_base + FREEGLUT_MENU_HEIGHT - border );
422 /* If the menu entry is active, reset the color */
423 if( menuEntry->IsActive )
424 glColor4fv( menu_pen_fore );
429 * Private static function to set the parent window of a submenu and all
432 static void fghSetMenuParentWindow( SFG_Window *window, SFG_Menu *menu )
434 SFG_MenuEntry *menuEntry;
436 menu->ParentWindow = window;
438 for( menuEntry = ( SFG_MenuEntry * )menu->Entries.First;
440 menuEntry = ( SFG_MenuEntry * )menuEntry->Node.Next )
441 if( menuEntry->SubMenu )
442 fghSetMenuParentWindow( window, menuEntry->SubMenu );
446 * Function to check for menu entry selection on menu deactivation
448 static void fghExecuteMenuCallback( SFG_Menu* menu )
450 SFG_MenuEntry *menuEntry;
452 /* First of all check any of the active sub menus... */
453 for( menuEntry = (SFG_MenuEntry *)menu->Entries.First;
455 menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next)
457 if( menuEntry->IsActive )
459 if( menuEntry->SubMenu )
460 fghExecuteMenuCallback( menuEntry->SubMenu );
464 SFG_Menu *save_menu = fgStructure.CurrentMenu;
465 fgStructure.CurrentMenu = menu;
466 menu->Callback( menuEntry->ID );
467 fgStructure.CurrentMenu = save_menu;
477 * Displays the currently active menu for the current window
479 void fgDisplayMenu( void )
481 SFG_Window* window = fgStructure.CurrentWindow;
482 SFG_Menu* menu = NULL;
484 FREEGLUT_INTERNAL_ERROR_EXIT ( fgStructure.CurrentWindow, "Displaying menu in nonexistent window",
487 /* Check if there is an active menu attached to this window... */
488 menu = window->ActiveMenu;
489 freeglut_return_if_fail( menu );
491 fgSetWindow( menu->Window );
493 glPushAttrib( GL_DEPTH_BUFFER_BIT | GL_TEXTURE_BIT | GL_LIGHTING_BIT |
496 glDisable( GL_DEPTH_TEST );
497 glDisable( GL_TEXTURE_2D );
498 glDisable( GL_LIGHTING );
499 glDisable( GL_CULL_FACE );
501 glMatrixMode( GL_PROJECTION );
505 0, glutGet( GLUT_WINDOW_WIDTH ),
506 glutGet( GLUT_WINDOW_HEIGHT ), 0,
510 glMatrixMode( GL_MODELVIEW );
514 fghDisplayMenuBox( menu );
518 glMatrixMode( GL_PROJECTION );
520 glMatrixMode( GL_MODELVIEW );
525 fgSetWindow ( window );
529 * Activates a menu pointed by the function argument
531 static void fghActivateMenu( SFG_Window* window, int button )
535 /* We'll be referencing this menu a lot, so remember its address: */
536 SFG_Menu* menu = window->Menu[ button ];
537 SFG_Window* current_window = fgStructure.CurrentWindow;
539 /* If the menu is already active in another window, deactivate it there */
540 if ( menu->ParentWindow )
541 menu->ParentWindow->ActiveMenu = NULL ;
543 /* Mark the menu as active, so that it gets displayed: */
544 window->ActiveMenu = menu;
545 menu->IsActive = GL_TRUE;
546 fghSetMenuParentWindow ( window, menu );
547 fgState.ActiveMenus++;
549 /* Set up the initial menu position now: */
550 fghGetVMaxExtent(menu->ParentWindow, &max_x, &max_y);
551 fgSetWindow( window );
552 menu->X = window->State.MouseX + glutGet( GLUT_WINDOW_X );
553 menu->Y = window->State.MouseY + glutGet( GLUT_WINDOW_Y );
555 if( menu->X + menu->Width > max_x )
556 menu->X -=menu->Width;
558 if( menu->Y + menu->Height > max_y )
560 menu->Y -=menu->Height;
565 menu->Window->State.MouseX =
566 window->State.MouseX + glutGet( GLUT_WINDOW_X ) - menu->X;
567 menu->Window->State.MouseY =
568 window->State.MouseY + glutGet( GLUT_WINDOW_Y ) - menu->Y;
570 fgSetWindow( menu->Window );
571 glutPositionWindow( menu->X, menu->Y );
572 glutReshapeWindow( menu->Width, menu->Height );
575 menu->Window->ActiveMenu = menu;
576 fghCheckMenuStatus( menu );
577 fgSetWindow( current_window );
581 * Update Highlight states of the menu
583 * Current mouse position is in menu->Window->State.MouseX/Y.
585 void fgUpdateMenuHighlight ( SFG_Menu *menu )
587 fghCheckMenuStatus( menu );
591 * Check whether an active menu absorbs a mouse click
593 GLboolean fgCheckActiveMenu ( SFG_Window *window, int button, GLboolean pressed,
594 int mouse_x, int mouse_y )
597 * Near as I can tell, this is the menu behaviour:
598 * - Down-click the menu button, menu not active: activate
599 * the menu with its upper left-hand corner at the mouse
601 * - Down-click any button outside the menu, menu active:
602 * deactivate the menu
603 * - Down-click any button inside the menu, menu active:
604 * select the menu entry and deactivate the menu
605 * - Up-click the menu button, menu not active: nothing happens
606 * - Up-click the menu button outside the menu, menu active:
608 * - Up-click the menu button inside the menu, menu active:
609 * select the menu entry and deactivate the menu
610 * Since menus can have submenus, we need to check this recursively.
612 if( window->ActiveMenu )
614 if( window == window->ActiveMenu->ParentWindow )
616 window->ActiveMenu->Window->State.MouseX =
617 mouse_x - window->ActiveMenu->X;
618 window->ActiveMenu->Window->State.MouseY =
619 mouse_y - window->ActiveMenu->Y;
622 /* In the menu, invoke the callback and deactivate the menu */
623 if( fghCheckMenuStatus( window->ActiveMenu ) )
626 * Save the current window and menu and set the current
627 * window to the window whose menu this is
629 SFG_Window *save_window = fgStructure.CurrentWindow;
630 SFG_Menu *save_menu = fgStructure.CurrentMenu;
631 SFG_Window *parent_window = window->ActiveMenu->ParentWindow;
632 fgSetWindow( parent_window );
633 fgStructure.CurrentMenu = window->ActiveMenu;
635 /* Execute the menu callback */
636 fghExecuteMenuCallback( window->ActiveMenu );
637 fgDeactivateMenu( parent_window );
639 /* Restore the current window and menu */
640 fgSetWindow( save_window );
641 fgStructure.CurrentMenu = save_menu;
645 * Outside the menu, deactivate if it's a downclick
647 * XXX This isn't enough. A downclick outside of
648 * XXX the interior of our freeglut windows should also
649 * XXX deactivate the menu. This is more complicated.
651 fgDeactivateMenu( window->ActiveMenu->ParentWindow );
654 * XXX Why does an active menu require a redisplay at
655 * XXX this point? If this can come out cleanly, then
656 * XXX it probably should do so; if not, a comment should
659 if( ! window->IsMenu )
660 window->State.Redisplay = GL_TRUE;
665 /* No active menu, let's check whether we need to activate one. */
666 if( ( 0 <= button ) &&
667 ( FREEGLUT_MAX_MENUS > button ) &&
668 ( window->Menu[ button ] ) &&
671 /* XXX Posting a requisite Redisplay seems bogus. */
672 window->State.Redisplay = GL_TRUE;
673 fghActivateMenu( window, button );
681 * Deactivates a menu pointed by the function argument.
683 void fgDeactivateMenu( SFG_Window *window )
685 SFG_Window *parent_window = NULL;
687 /* Check if there is an active menu attached to this window... */
688 SFG_Menu* menu = window->ActiveMenu;
689 SFG_MenuEntry *menuEntry;
691 /* Did we find an active window? */
692 freeglut_return_if_fail( menu );
694 parent_window = menu->ParentWindow;
696 /* Hide the present menu's window */
697 fgSetWindow( menu->Window );
700 /* Forget about having that menu active anymore, now: */
701 menu->Window->ActiveMenu = NULL;
702 menu->ParentWindow->ActiveMenu = NULL;
703 fghSetMenuParentWindow ( NULL, menu );
704 menu->IsActive = GL_FALSE;
705 menu->ActiveEntry = NULL;
707 fgState.ActiveMenus--;
709 /* Hide all submenu windows, and the root menu's window. */
710 for ( menuEntry = ( SFG_MenuEntry * )menu->Entries.First;
712 menuEntry = ( SFG_MenuEntry * )menuEntry->Node.Next )
714 menuEntry->IsActive = GL_FALSE;
716 /* Is that an active submenu by any case? */
717 if( menuEntry->SubMenu )
718 fghDeactivateSubMenu( menuEntry );
721 fgSetWindow ( parent_window ) ;
725 * Recalculates current menu's box size
727 void fghCalculateMenuBoxSize( void )
729 SFG_MenuEntry* menuEntry;
730 int width = 0, height = 0;
732 /* Make sure there is a current menu set */
733 freeglut_return_if_fail( fgStructure.CurrentMenu );
735 /* The menu's box size depends on the menu entries: */
736 for( menuEntry = ( SFG_MenuEntry * )fgStructure.CurrentMenu->Entries.First;
738 menuEntry = ( SFG_MenuEntry * )menuEntry->Node.Next )
740 /* Update the menu entry's width value */
741 menuEntry->Width = glutBitmapLength(
743 (unsigned char *)menuEntry->Text
747 * If the entry is a submenu, then it needs to be wider to
748 * accomodate the arrow. JCJ 31 July 2003
750 if (menuEntry->SubMenu )
751 menuEntry->Width += glutBitmapLength(
756 /* Check if it's the biggest we've found */
757 if( menuEntry->Width > width )
758 width = menuEntry->Width;
760 height += FREEGLUT_MENU_HEIGHT;
763 /* Store the menu's box size now: */
764 fgStructure.CurrentMenu->Height = height + 2 * FREEGLUT_MENU_BORDER;
765 fgStructure.CurrentMenu->Width = width + 4 * FREEGLUT_MENU_BORDER;
769 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
772 * Creates a new menu object, adding it to the freeglut structure
774 int FGAPIENTRY glutCreateMenu( void(* callback)( int ) )
776 /* The menu object creation code resides in freeglut_structure.c */
777 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutCreateMenu" );
778 return fgCreateMenu( callback )->ID;
782 * Destroys a menu object, removing all references to it
784 void FGAPIENTRY glutDestroyMenu( int menuID )
788 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutDestroyMenu" );
789 menu = fgMenuByID( menuID );
791 freeglut_return_if_fail( menu );
793 /* The menu object destruction code resides in freeglut_structure.c */
794 fgDestroyMenu( menu );
798 * Returns the ID number of the currently active menu
800 int FGAPIENTRY glutGetMenu( void )
802 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGetMenu" );
804 if( fgStructure.CurrentMenu )
805 return fgStructure.CurrentMenu->ID;
811 * Sets the current menu given its menu ID
813 void FGAPIENTRY glutSetMenu( int menuID )
817 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetMenu" );
818 menu = fgMenuByID( menuID );
820 freeglut_return_if_fail( menu );
822 fgStructure.CurrentMenu = menu;
826 * Adds a menu entry to the bottom of the current menu
828 void FGAPIENTRY glutAddMenuEntry( const char* label, int value )
830 SFG_MenuEntry* menuEntry;
831 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutAddMenuEntry" );
832 menuEntry = (SFG_MenuEntry *)calloc( sizeof(SFG_MenuEntry), 1 );
833 freeglut_return_if_fail( fgStructure.CurrentMenu );
835 menuEntry->Text = strdup( label );
836 menuEntry->ID = value;
838 /* Have the new menu entry attached to the current menu */
839 fgListAppend( &fgStructure.CurrentMenu->Entries, &menuEntry->Node );
841 fghCalculateMenuBoxSize( );
845 * Add a sub menu to the bottom of the current menu
847 void FGAPIENTRY glutAddSubMenu( const char *label, int subMenuID )
849 SFG_MenuEntry *menuEntry;
852 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutAddSubMenu" );
853 menuEntry = ( SFG_MenuEntry * )calloc( sizeof( SFG_MenuEntry ), 1 );
854 subMenu = fgMenuByID( subMenuID );
856 freeglut_return_if_fail( fgStructure.CurrentMenu );
857 freeglut_return_if_fail( subMenu );
859 menuEntry->Text = strdup( label );
860 menuEntry->SubMenu = subMenu;
863 fgListAppend( &fgStructure.CurrentMenu->Entries, &menuEntry->Node );
864 fghCalculateMenuBoxSize( );
868 * Changes the specified menu item in the current menu into a menu entry
870 void FGAPIENTRY glutChangeToMenuEntry( int item, const char* label, int value )
872 SFG_MenuEntry* menuEntry = NULL;
874 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutChangeToMenuEntry" );
875 freeglut_return_if_fail( fgStructure.CurrentMenu );
877 /* Get n-th menu entry in the current menu, starting from one: */
878 menuEntry = fghFindMenuEntry( fgStructure.CurrentMenu, item );
880 freeglut_return_if_fail( menuEntry );
882 /* We want it to become a normal menu entry, so: */
883 if( menuEntry->Text )
884 free( menuEntry->Text );
886 menuEntry->Text = strdup( label );
887 menuEntry->ID = value;
888 menuEntry->SubMenu = NULL;
889 fghCalculateMenuBoxSize( );
893 * Changes the specified menu item in the current menu into a sub-menu trigger.
895 void FGAPIENTRY glutChangeToSubMenu( int item, const char* label,
899 SFG_MenuEntry* menuEntry;
901 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutChangeToSubMenu" );
902 subMenu = fgMenuByID( subMenuID );
905 freeglut_return_if_fail( fgStructure.CurrentMenu );
906 freeglut_return_if_fail( subMenu );
908 /* Get n-th menu entry in the current menu, starting from one: */
909 menuEntry = fghFindMenuEntry( fgStructure.CurrentMenu, item );
911 freeglut_return_if_fail( menuEntry );
913 /* We want it to become a sub menu entry, so: */
914 if( menuEntry->Text )
915 free( menuEntry->Text );
917 menuEntry->Text = strdup( label );
918 menuEntry->SubMenu = subMenu;
920 fghCalculateMenuBoxSize( );
924 * Removes the specified menu item from the current menu
926 void FGAPIENTRY glutRemoveMenuItem( int item )
928 SFG_MenuEntry* menuEntry;
930 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutRemoveMenuItem" );
931 freeglut_return_if_fail( fgStructure.CurrentMenu );
933 /* Get n-th menu entry in the current menu, starting from one: */
934 menuEntry = fghFindMenuEntry( fgStructure.CurrentMenu, item );
936 freeglut_return_if_fail( menuEntry );
938 fgListRemove( &fgStructure.CurrentMenu->Entries, &menuEntry->Node );
939 if ( menuEntry->Text )
940 free( menuEntry->Text );
943 fghCalculateMenuBoxSize( );
947 * Attaches a menu to the current window
949 void FGAPIENTRY glutAttachMenu( int button )
951 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutAttachMenu" );
953 freeglut_return_if_fail( fgStructure.CurrentWindow );
954 freeglut_return_if_fail( fgStructure.CurrentMenu );
956 freeglut_return_if_fail( button >= 0 );
957 freeglut_return_if_fail( button < FREEGLUT_MAX_MENUS );
959 fgStructure.CurrentWindow->Menu[ button ] = fgStructure.CurrentMenu;
963 * Detaches a menu from the current window
965 void FGAPIENTRY glutDetachMenu( int button )
967 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutDetachMenu" );
969 freeglut_return_if_fail( fgStructure.CurrentWindow );
970 freeglut_return_if_fail( fgStructure.CurrentMenu );
972 freeglut_return_if_fail( button >= 0 );
973 freeglut_return_if_fail( button < FREEGLUT_MAX_MENUS );
975 fgStructure.CurrentWindow->Menu[ button ] = NULL;
979 * A.Donev: Set and retrieve the menu's user data
981 void* FGAPIENTRY glutGetMenuData( void )
983 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGetMenuData" );
984 return fgStructure.CurrentMenu->UserData;
987 void FGAPIENTRY glutSetMenuData(void* data)
989 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetMenuData" );
990 fgStructure.CurrentMenu->UserData=data;
993 /*** END OF FILE ***/