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 "fg_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 /* See platform-specific header files for menu font and color definitions */
58 #define FREEGLUT_MENU_HEIGHT (glutBitmapHeight(FREEGLUT_MENU_FONT) + \
60 #define FREEGLUT_MENU_BORDER 2
64 * These variables are for rendering the freeglut menu items.
66 * The choices are fore- and background, with and without h for Highlighting.
67 * Old GLUT appeared to be system-dependant for its colors (sigh) so we are
68 * too. These variables should be stuffed into global state and initialized
69 * via the glutInit*() system.
71 static float menu_pen_fore [4] = FREEGLUT_MENU_PEN_FORE_COLORS ;
72 static float menu_pen_back [4] = FREEGLUT_MENU_PEN_BACK_COLORS ;
73 static float menu_pen_hfore [4] = FREEGLUT_MENU_PEN_HFORE_COLORS;
74 static float menu_pen_hback [4] = FREEGLUT_MENU_PEN_HBACK_COLORS;
77 extern GLvoid fgPlatformGetGameModeVMaxExtent( SFG_Window* window, int* x, int* y );
79 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */
82 * Private function to find a menu entry by index
84 static SFG_MenuEntry *fghFindMenuEntry( SFG_Menu* menu, int index )
89 for( entry = (SFG_MenuEntry *)menu->Entries.First;
91 entry = (SFG_MenuEntry *)entry->Node.Next )
102 * Deactivates a menu pointed by the function argument.
104 static void fghDeactivateSubMenu( SFG_MenuEntry *menuEntry )
106 SFG_MenuEntry *subMenuIter;
107 /* Hide the present menu's window */
108 fgSetWindow( menuEntry->SubMenu->Window );
111 /* Forget about having that menu active anymore, now: */
112 menuEntry->SubMenu->Window->ActiveMenu = NULL;
113 menuEntry->SubMenu->IsActive = GL_FALSE;
114 menuEntry->SubMenu->ActiveEntry = NULL;
116 /* Hide all submenu windows, and the root menu's window. */
117 for ( subMenuIter = (SFG_MenuEntry *)menuEntry->SubMenu->Entries.First;
119 subMenuIter = (SFG_MenuEntry *)subMenuIter->Node.Next )
121 subMenuIter->IsActive = GL_FALSE;
123 /* Is that an active submenu by any case? */
124 if( subMenuIter->SubMenu )
125 fghDeactivateSubMenu( subMenuIter );
128 fgSetWindow ( menuEntry->SubMenu->ParentWindow ) ;
132 * Private function to get the virtual maximum screen extent
134 static GLvoid fghGetVMaxExtent( SFG_Window* window, int* x, int* y )
136 if( fgStructure.GameModeWindow )
137 fgPlatformGetGameModeVMaxExtent ( window, x, y );
140 *x = fgDisplay.ScreenWidth;
141 *y = fgDisplay.ScreenHeight;
146 * Private function to check for the current menu/sub menu activity state
148 static GLboolean fghCheckMenuStatus( SFG_Menu* menu )
150 SFG_MenuEntry* menuEntry;
153 /* First of all check any of the active sub menus... */
154 for( menuEntry = (SFG_MenuEntry *)menu->Entries.First;
156 menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next )
158 if( menuEntry->SubMenu && menuEntry->IsActive )
161 * OK, have the sub-menu checked, too. If it returns GL_TRUE, it
162 * will mean that it caught the mouse cursor and we do not need
163 * to regenerate the activity list, and so our parents do...
165 GLboolean return_status;
167 menuEntry->SubMenu->Window->State.MouseX =
168 menu->Window->State.MouseX + menu->X - menuEntry->SubMenu->X;
169 menuEntry->SubMenu->Window->State.MouseY =
170 menu->Window->State.MouseY + menu->Y - menuEntry->SubMenu->Y;
171 return_status = fghCheckMenuStatus( menuEntry->SubMenu );
178 /* That much about our sub menus, let's get to checking the current menu: */
179 x = menu->Window->State.MouseX;
180 y = menu->Window->State.MouseY;
182 /* Check if the mouse cursor is contained within the current menu box */
183 if( ( x >= FREEGLUT_MENU_BORDER ) &&
184 ( x < menu->Width - FREEGLUT_MENU_BORDER ) &&
185 ( y >= FREEGLUT_MENU_BORDER ) &&
186 ( y < menu->Height - FREEGLUT_MENU_BORDER ) )
188 int menuID = ( y - FREEGLUT_MENU_BORDER ) / FREEGLUT_MENU_HEIGHT;
190 /* The mouse cursor is somewhere over our box, check it out. */
191 menuEntry = fghFindMenuEntry( menu, menuID + 1 );
192 FREEGLUT_INTERNAL_ERROR_EXIT( menuEntry, "Cannot find menu entry",
193 "fghCheckMenuStatus" );
195 menuEntry->IsActive = GL_TRUE;
196 menuEntry->Ordinal = menuID;
199 * If this is not the same as the last active menu entry, deactivate
200 * the previous entry. Specifically, if the previous active entry
201 * was a submenu then deactivate it.
203 if( menu->ActiveEntry && ( menuEntry != menu->ActiveEntry ) )
204 if( menu->ActiveEntry->SubMenu )
205 fghDeactivateSubMenu( menu->ActiveEntry );
207 if( menuEntry != menu->ActiveEntry )
209 menu->Window->State.Redisplay = GL_TRUE;
210 if( menu->ActiveEntry )
211 menu->ActiveEntry->IsActive = GL_FALSE;
214 menu->ActiveEntry = menuEntry;
215 menu->IsActive = GL_TRUE; /* XXX Do we need this? */
218 * OKi, we have marked that entry as active, but it would be also
219 * nice to have its contents updated, in case it's a sub menu.
220 * Also, ignore the return value of the check function:
222 if( menuEntry->SubMenu )
224 if ( ! menuEntry->SubMenu->IsActive )
227 SFG_Window *current_window = fgStructure.CurrentWindow;
229 /* Set up the initial menu position now... */
230 menuEntry->SubMenu->IsActive = GL_TRUE;
232 /* Set up the initial submenu position now: */
233 fghGetVMaxExtent(menu->ParentWindow, &max_x, &max_y);
234 menuEntry->SubMenu->X = menu->X + menu->Width;
235 menuEntry->SubMenu->Y = menu->Y +
236 menuEntry->Ordinal * FREEGLUT_MENU_HEIGHT;
238 if( menuEntry->SubMenu->X + menuEntry->SubMenu->Width > max_x )
239 menuEntry->SubMenu->X = menu->X - menuEntry->SubMenu->Width;
241 if( menuEntry->SubMenu->Y + menuEntry->SubMenu->Height > max_y )
243 menuEntry->SubMenu->Y -= ( menuEntry->SubMenu->Height -
244 FREEGLUT_MENU_HEIGHT -
245 2 * FREEGLUT_MENU_BORDER );
246 if( menuEntry->SubMenu->Y < 0 )
247 menuEntry->SubMenu->Y = 0;
250 fgSetWindow( menuEntry->SubMenu->Window );
251 glutPositionWindow( menuEntry->SubMenu->X,
252 menuEntry->SubMenu->Y );
253 glutReshapeWindow( menuEntry->SubMenu->Width,
254 menuEntry->SubMenu->Height );
257 menuEntry->SubMenu->Window->ActiveMenu = menuEntry->SubMenu;
258 fgSetWindow( current_window );
259 menuEntry->SubMenu->Window->State.MouseX =
260 x + menu->X - menuEntry->SubMenu->X;
261 menuEntry->SubMenu->Window->State.MouseY =
262 y + menu->Y - menuEntry->SubMenu->Y;
263 fghCheckMenuStatus( menuEntry->SubMenu );
266 /* Activate it because its parent entry is active */
267 menuEntry->SubMenu->IsActive = GL_TRUE; /* XXX Do we need this? */
270 /* Report back that we have caught the menu cursor */
274 /* Looks like the menu cursor is somewhere else... */
275 if( menu->ActiveEntry && menu->ActiveEntry->IsActive &&
276 ( !menu->ActiveEntry->SubMenu ||
277 !menu->ActiveEntry->SubMenu->IsActive ) )
279 menu->Window->State.Redisplay = GL_TRUE;
280 menu->ActiveEntry->IsActive = GL_FALSE;
281 menu->ActiveEntry = NULL;
288 * Displays a menu box and all of its submenus (if they are active)
290 static void fghDisplayMenuBox( SFG_Menu* menu )
292 SFG_MenuEntry *menuEntry;
294 int border = FREEGLUT_MENU_BORDER;
297 * Have the menu box drawn first. The +- values are
298 * here just to make it more nice-looking...
300 /* a non-black dark version of the below. */
301 glColor4f( 1.0f, 1.0f, 1.0f, 1.0f );
302 glBegin( GL_QUAD_STRIP );
303 glVertex2i( menu->Width , 0 );
304 glVertex2i( menu->Width - border, border);
306 glVertex2i( border, border);
307 glVertex2i( 0 , menu->Height );
308 glVertex2i( border, menu->Height - border);
311 /* a non-black dark version of the below. */
312 glColor4f( 0.5f, 0.5f, 0.5f, 1.0f );
313 glBegin( GL_QUAD_STRIP );
314 glVertex2i( 0 , menu->Height );
315 glVertex2i( border, menu->Height - border);
316 glVertex2i( menu->Width , menu->Height );
317 glVertex2i( menu->Width - border, menu->Height - border);
318 glVertex2i( menu->Width , 0 );
319 glVertex2i( menu->Width - border, border);
322 glColor4fv( menu_pen_back );
324 glVertex2i( border, border);
325 glVertex2i( menu->Width - border, border);
326 glVertex2i( menu->Width - border, menu->Height - border);
327 glVertex2i( border, menu->Height - border);
330 /* Check if any of the submenus is currently active... */
331 for( menuEntry = (SFG_MenuEntry *)menu->Entries.First;
333 menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next )
335 /* Has the menu been marked as active, maybe? */
336 if( menuEntry->IsActive )
339 * That's truly right, and we need to have it highlighted.
340 * There is an assumption that mouse cursor didn't move
341 * since the last check of menu activity state:
343 int menuID = menuEntry->Ordinal;
345 /* So have the highlight drawn... */
346 glColor4fv( menu_pen_hback );
349 (menuID + 0)*FREEGLUT_MENU_HEIGHT + border );
350 glVertex2i( menu->Width - border,
351 (menuID + 0)*FREEGLUT_MENU_HEIGHT + border );
352 glVertex2i( menu->Width - border,
353 (menuID + 1)*FREEGLUT_MENU_HEIGHT + border );
355 (menuID + 1)*FREEGLUT_MENU_HEIGHT + border );
360 /* Print the menu entries now... */
362 glColor4fv( menu_pen_fore );
364 for( menuEntry = (SFG_MenuEntry *)menu->Entries.First, i = 0;
366 menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next, ++i )
368 /* If the menu entry is active, set the color to white */
369 if( menuEntry->IsActive )
370 glColor4fv( menu_pen_hfore );
372 /* Move the raster into position... */
373 /* Try to center the text - JCJ 31 July 2003*/
376 ( i + 1 )*FREEGLUT_MENU_HEIGHT -
377 ( int )( FREEGLUT_MENU_HEIGHT*0.3 - border )
380 /* Have the label drawn, character after character: */
381 glutBitmapString( FREEGLUT_MENU_FONT,
382 (unsigned char *)menuEntry->Text);
384 /* If it's a submenu, draw a right arrow */
385 if( menuEntry->SubMenu )
387 int width = glutBitmapWidth( FREEGLUT_MENU_FONT, '_' );
388 int x_base = menu->Width - 2 - width;
389 int y_base = i*FREEGLUT_MENU_HEIGHT + border;
390 glBegin( GL_TRIANGLES );
391 glVertex2i( x_base, y_base + 2*border);
392 glVertex2i( menu->Width - 2, y_base +
393 ( FREEGLUT_MENU_HEIGHT + border) / 2 );
394 glVertex2i( x_base, y_base + FREEGLUT_MENU_HEIGHT - border );
398 /* If the menu entry is active, reset the color */
399 if( menuEntry->IsActive )
400 glColor4fv( menu_pen_fore );
405 * Private static function to set the parent window of a submenu and all
408 static void fghSetMenuParentWindow( SFG_Window *window, SFG_Menu *menu )
410 SFG_MenuEntry *menuEntry;
412 menu->ParentWindow = window;
414 for( menuEntry = ( SFG_MenuEntry * )menu->Entries.First;
416 menuEntry = ( SFG_MenuEntry * )menuEntry->Node.Next )
417 if( menuEntry->SubMenu )
418 fghSetMenuParentWindow( window, menuEntry->SubMenu );
422 * Function to check for menu entry selection on menu deactivation
424 static void fghExecuteMenuCallback( SFG_Menu* menu )
426 SFG_MenuEntry *menuEntry;
428 /* First of all check any of the active sub menus... */
429 for( menuEntry = (SFG_MenuEntry *)menu->Entries.First;
431 menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next)
433 if( menuEntry->IsActive )
435 if( menuEntry->SubMenu )
436 fghExecuteMenuCallback( menuEntry->SubMenu );
440 SFG_Menu *save_menu = fgStructure.CurrentMenu;
441 fgStructure.CurrentMenu = menu;
442 menu->Callback( menuEntry->ID );
443 fgStructure.CurrentMenu = save_menu;
453 * Displays the currently active menu for the current window
455 void fgDisplayMenu( void )
457 SFG_Window* window = fgStructure.CurrentWindow;
458 SFG_Menu* menu = NULL;
460 FREEGLUT_INTERNAL_ERROR_EXIT ( fgStructure.CurrentWindow, "Displaying menu in nonexistent window",
463 /* Check if there is an active menu attached to this window... */
464 menu = window->ActiveMenu;
465 freeglut_return_if_fail( menu );
467 fgSetWindow( menu->Window );
469 glPushAttrib( GL_DEPTH_BUFFER_BIT | GL_TEXTURE_BIT | GL_LIGHTING_BIT |
472 glDisable( GL_DEPTH_TEST );
473 glDisable( GL_TEXTURE_2D );
474 glDisable( GL_LIGHTING );
475 glDisable( GL_CULL_FACE );
477 glMatrixMode( GL_PROJECTION );
481 0, glutGet( GLUT_WINDOW_WIDTH ),
482 glutGet( GLUT_WINDOW_HEIGHT ), 0,
486 glMatrixMode( GL_MODELVIEW );
490 fghDisplayMenuBox( menu );
494 glMatrixMode( GL_PROJECTION );
496 glMatrixMode( GL_MODELVIEW );
501 fgSetWindow ( window );
505 * Activates a menu pointed by the function argument
507 static void fghActivateMenu( SFG_Window* window, int button )
511 /* We'll be referencing this menu a lot, so remember its address: */
512 SFG_Menu* menu = window->Menu[ button ];
513 SFG_Window* current_window = fgStructure.CurrentWindow;
515 /* If the menu is already active in another window, deactivate it there */
516 if ( menu->ParentWindow )
517 menu->ParentWindow->ActiveMenu = NULL ;
519 /* Mark the menu as active, so that it gets displayed: */
520 window->ActiveMenu = menu;
521 menu->IsActive = GL_TRUE;
522 fghSetMenuParentWindow ( window, menu );
523 fgState.ActiveMenus++;
525 /* Set up the initial menu position now: */
526 fghGetVMaxExtent(menu->ParentWindow, &max_x, &max_y);
527 fgSetWindow( window );
528 menu->X = window->State.MouseX + glutGet( GLUT_WINDOW_X );
529 menu->Y = window->State.MouseY + glutGet( GLUT_WINDOW_Y );
531 if( menu->X + menu->Width > max_x )
532 menu->X -=menu->Width;
534 if( menu->Y + menu->Height > max_y )
536 menu->Y -=menu->Height;
541 menu->Window->State.MouseX =
542 window->State.MouseX + glutGet( GLUT_WINDOW_X ) - menu->X;
543 menu->Window->State.MouseY =
544 window->State.MouseY + glutGet( GLUT_WINDOW_Y ) - menu->Y;
546 fgSetWindow( menu->Window );
547 glutPositionWindow( menu->X, menu->Y );
548 glutReshapeWindow( menu->Width, menu->Height );
551 menu->Window->ActiveMenu = menu;
552 fghCheckMenuStatus( menu );
553 fgSetWindow( current_window );
557 * Update Highlight states of the menu
559 * Current mouse position is in menu->Window->State.MouseX/Y.
561 void fgUpdateMenuHighlight ( SFG_Menu *menu )
563 fghCheckMenuStatus( menu );
567 * Check whether an active menu absorbs a mouse click
569 GLboolean fgCheckActiveMenu ( SFG_Window *window, int button, GLboolean pressed,
570 int mouse_x, int mouse_y )
573 * Near as I can tell, this is the menu behaviour:
574 * - Down-click the menu button, menu not active: activate
575 * the menu with its upper left-hand corner at the mouse
577 * - Down-click any button outside the menu, menu active:
578 * deactivate the menu
579 * - Down-click any button inside the menu, menu active:
580 * select the menu entry and deactivate the menu
581 * - Up-click the menu button, menu not active: nothing happens
582 * - Up-click the menu button outside the menu, menu active:
584 * - Up-click the menu button inside the menu, menu active:
585 * select the menu entry and deactivate the menu
586 * Since menus can have submenus, we need to check this recursively.
588 if( window->ActiveMenu )
590 if( window == window->ActiveMenu->ParentWindow )
592 window->ActiveMenu->Window->State.MouseX =
593 mouse_x - window->ActiveMenu->X;
594 window->ActiveMenu->Window->State.MouseY =
595 mouse_y - window->ActiveMenu->Y;
598 /* In the menu, invoke the callback and deactivate the menu */
599 if( fghCheckMenuStatus( window->ActiveMenu ) )
602 * Save the current window and menu and set the current
603 * window to the window whose menu this is
605 SFG_Window *save_window = fgStructure.CurrentWindow;
606 SFG_Menu *save_menu = fgStructure.CurrentMenu;
607 SFG_Window *parent_window = window->ActiveMenu->ParentWindow;
608 fgSetWindow( parent_window );
609 fgStructure.CurrentMenu = window->ActiveMenu;
611 /* Execute the menu callback */
612 fghExecuteMenuCallback( window->ActiveMenu );
613 fgDeactivateMenu( parent_window );
615 /* Restore the current window and menu */
616 fgSetWindow( save_window );
617 fgStructure.CurrentMenu = save_menu;
621 * Outside the menu, deactivate if it's a downclick
623 * XXX This isn't enough. A downclick outside of
624 * XXX the interior of our freeglut windows should also
625 * XXX deactivate the menu. This is more complicated.
627 fgDeactivateMenu( window->ActiveMenu->ParentWindow );
630 * XXX Why does an active menu require a redisplay at
631 * XXX this point? If this can come out cleanly, then
632 * XXX it probably should do so; if not, a comment should
635 if( ! window->IsMenu )
636 window->State.Redisplay = GL_TRUE;
641 /* No active menu, let's check whether we need to activate one. */
642 if( ( 0 <= button ) &&
643 ( FREEGLUT_MAX_MENUS > button ) &&
644 ( window->Menu[ button ] ) &&
647 /* XXX Posting a requisite Redisplay seems bogus. */
648 window->State.Redisplay = GL_TRUE;
649 fghActivateMenu( window, button );
657 * Deactivates a menu pointed by the function argument.
659 void fgDeactivateMenu( SFG_Window *window )
661 SFG_Window *parent_window = NULL;
663 /* Check if there is an active menu attached to this window... */
664 SFG_Menu* menu = window->ActiveMenu;
665 SFG_MenuEntry *menuEntry;
667 /* Did we find an active window? */
668 freeglut_return_if_fail( menu );
670 parent_window = menu->ParentWindow;
672 /* Hide the present menu's window */
673 fgSetWindow( menu->Window );
676 /* Forget about having that menu active anymore, now: */
677 menu->Window->ActiveMenu = NULL;
678 menu->ParentWindow->ActiveMenu = NULL;
679 fghSetMenuParentWindow ( NULL, menu );
680 menu->IsActive = GL_FALSE;
681 menu->ActiveEntry = NULL;
683 fgState.ActiveMenus--;
685 /* Hide all submenu windows, and the root menu's window. */
686 for ( menuEntry = ( SFG_MenuEntry * )menu->Entries.First;
688 menuEntry = ( SFG_MenuEntry * )menuEntry->Node.Next )
690 menuEntry->IsActive = GL_FALSE;
692 /* Is that an active submenu by any case? */
693 if( menuEntry->SubMenu )
694 fghDeactivateSubMenu( menuEntry );
697 fgSetWindow ( parent_window ) ;
701 * Recalculates current menu's box size
703 void fghCalculateMenuBoxSize( void )
705 SFG_MenuEntry* menuEntry;
706 int width = 0, height = 0;
708 /* Make sure there is a current menu set */
709 freeglut_return_if_fail( fgStructure.CurrentMenu );
711 /* The menu's box size depends on the menu entries: */
712 for( menuEntry = ( SFG_MenuEntry * )fgStructure.CurrentMenu->Entries.First;
714 menuEntry = ( SFG_MenuEntry * )menuEntry->Node.Next )
716 /* Update the menu entry's width value */
717 menuEntry->Width = glutBitmapLength(
719 (unsigned char *)menuEntry->Text
723 * If the entry is a submenu, then it needs to be wider to
724 * accomodate the arrow. JCJ 31 July 2003
726 if (menuEntry->SubMenu )
727 menuEntry->Width += glutBitmapLength(
732 /* Check if it's the biggest we've found */
733 if( menuEntry->Width > width )
734 width = menuEntry->Width;
736 height += FREEGLUT_MENU_HEIGHT;
739 /* Store the menu's box size now: */
740 fgStructure.CurrentMenu->Height = height + 2 * FREEGLUT_MENU_BORDER;
741 fgStructure.CurrentMenu->Width = width + 4 * FREEGLUT_MENU_BORDER;
745 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
748 * Creates a new menu object, adding it to the freeglut structure
750 int FGAPIENTRY glutCreateMenu( void(* callback)( int ) )
752 /* The menu object creation code resides in freeglut_structure.c */
753 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutCreateMenu" );
754 return fgCreateMenu( callback )->ID;
758 * Destroys a menu object, removing all references to it
760 void FGAPIENTRY glutDestroyMenu( int menuID )
764 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutDestroyMenu" );
765 menu = fgMenuByID( menuID );
767 freeglut_return_if_fail( menu );
769 /* The menu object destruction code resides in freeglut_structure.c */
770 fgDestroyMenu( menu );
774 * Returns the ID number of the currently active menu
776 int FGAPIENTRY glutGetMenu( void )
778 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGetMenu" );
780 if( fgStructure.CurrentMenu )
781 return fgStructure.CurrentMenu->ID;
787 * Sets the current menu given its menu ID
789 void FGAPIENTRY glutSetMenu( int menuID )
793 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetMenu" );
794 menu = fgMenuByID( menuID );
796 freeglut_return_if_fail( menu );
798 fgStructure.CurrentMenu = menu;
802 * Adds a menu entry to the bottom of the current menu
804 void FGAPIENTRY glutAddMenuEntry( const char* label, int value )
806 SFG_MenuEntry* menuEntry;
807 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutAddMenuEntry" );
808 menuEntry = (SFG_MenuEntry *)calloc( sizeof(SFG_MenuEntry), 1 );
809 freeglut_return_if_fail( fgStructure.CurrentMenu );
811 menuEntry->Text = strdup( label );
812 menuEntry->ID = value;
814 /* Have the new menu entry attached to the current menu */
815 fgListAppend( &fgStructure.CurrentMenu->Entries, &menuEntry->Node );
817 fghCalculateMenuBoxSize( );
821 * Add a sub menu to the bottom of the current menu
823 void FGAPIENTRY glutAddSubMenu( const char *label, int subMenuID )
825 SFG_MenuEntry *menuEntry;
828 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutAddSubMenu" );
829 menuEntry = ( SFG_MenuEntry * )calloc( sizeof( SFG_MenuEntry ), 1 );
830 subMenu = fgMenuByID( subMenuID );
832 freeglut_return_if_fail( fgStructure.CurrentMenu );
833 freeglut_return_if_fail( subMenu );
835 menuEntry->Text = strdup( label );
836 menuEntry->SubMenu = subMenu;
839 fgListAppend( &fgStructure.CurrentMenu->Entries, &menuEntry->Node );
840 fghCalculateMenuBoxSize( );
844 * Changes the specified menu item in the current menu into a menu entry
846 void FGAPIENTRY glutChangeToMenuEntry( int item, const char* label, int value )
848 SFG_MenuEntry* menuEntry = NULL;
850 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutChangeToMenuEntry" );
851 freeglut_return_if_fail( fgStructure.CurrentMenu );
853 /* Get n-th menu entry in the current menu, starting from one: */
854 menuEntry = fghFindMenuEntry( fgStructure.CurrentMenu, item );
856 freeglut_return_if_fail( menuEntry );
858 /* We want it to become a normal menu entry, so: */
859 if( menuEntry->Text )
860 free( menuEntry->Text );
862 menuEntry->Text = strdup( label );
863 menuEntry->ID = value;
864 menuEntry->SubMenu = NULL;
865 fghCalculateMenuBoxSize( );
869 * Changes the specified menu item in the current menu into a sub-menu trigger.
871 void FGAPIENTRY glutChangeToSubMenu( int item, const char* label,
875 SFG_MenuEntry* menuEntry;
877 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutChangeToSubMenu" );
878 subMenu = fgMenuByID( subMenuID );
881 freeglut_return_if_fail( fgStructure.CurrentMenu );
882 freeglut_return_if_fail( subMenu );
884 /* Get n-th menu entry in the current menu, starting from one: */
885 menuEntry = fghFindMenuEntry( fgStructure.CurrentMenu, item );
887 freeglut_return_if_fail( menuEntry );
889 /* We want it to become a sub menu entry, so: */
890 if( menuEntry->Text )
891 free( menuEntry->Text );
893 menuEntry->Text = strdup( label );
894 menuEntry->SubMenu = subMenu;
896 fghCalculateMenuBoxSize( );
900 * Removes the specified menu item from the current menu
902 void FGAPIENTRY glutRemoveMenuItem( int item )
904 SFG_MenuEntry* menuEntry;
906 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutRemoveMenuItem" );
907 freeglut_return_if_fail( fgStructure.CurrentMenu );
909 /* Get n-th menu entry in the current menu, starting from one: */
910 menuEntry = fghFindMenuEntry( fgStructure.CurrentMenu, item );
912 freeglut_return_if_fail( menuEntry );
914 fgListRemove( &fgStructure.CurrentMenu->Entries, &menuEntry->Node );
915 if ( menuEntry->Text )
916 free( menuEntry->Text );
919 fghCalculateMenuBoxSize( );
923 * Attaches a menu to the current window
925 void FGAPIENTRY glutAttachMenu( int button )
927 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutAttachMenu" );
929 freeglut_return_if_fail( fgStructure.CurrentWindow );
930 freeglut_return_if_fail( fgStructure.CurrentMenu );
932 freeglut_return_if_fail( button >= 0 );
933 freeglut_return_if_fail( button < FREEGLUT_MAX_MENUS );
935 fgStructure.CurrentWindow->Menu[ button ] = fgStructure.CurrentMenu;
939 * Detaches a menu from the current window
941 void FGAPIENTRY glutDetachMenu( int button )
943 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutDetachMenu" );
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 ] = NULL;
955 * A.Donev: Set and retrieve the menu's user data
957 void* FGAPIENTRY glutGetMenuData( void )
959 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGetMenuData" );
960 return fgStructure.CurrentMenu->UserData;
963 void FGAPIENTRY glutSetMenuData(void* data)
965 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetMenuData" );
966 fgStructure.CurrentMenu->UserData=data;
969 /*** END OF FILE ***/