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_Window *current_window = fgStructure.CurrentWindow;
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;
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 /* Is that an active submenu by any case? */
129 if( subMenuIter->SubMenu )
130 fghDeactivateSubMenu( subMenuIter );
133 fgSetWindow( current_window );
137 * Private function to get the virtual maximum screen extent
139 static GLvoid fghGetVMaxExtent( SFG_Window* window, int* x, int* y )
141 if( fgStructure.GameMode )
143 #if TARGET_HOST_UNIX_X11
147 XTranslateCoordinates(
149 window->Window.Handle,
150 fgDisplay.RootWindow,
153 *x = fgState.GameModeSize.X + wx;
154 *y = fgState.GameModeSize.Y + wy;
156 *x = glutGet ( GLUT_SCREEN_WIDTH );
157 *y = glutGet ( GLUT_SCREEN_HEIGHT );
162 *x = fgDisplay.ScreenWidth;
163 *y = fgDisplay.ScreenHeight;
168 * Private function to check for the current menu/sub menu activity state
170 static GLboolean fghCheckMenuStatus( SFG_Window* window, SFG_Menu* menu )
172 SFG_MenuEntry* menuEntry;
175 /* First of all check any of the active sub menus... */
176 for( menuEntry = (SFG_MenuEntry *)menu->Entries.First;
178 menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next )
180 if( menuEntry->SubMenu && menuEntry->IsActive )
183 * OK, have the sub-menu checked, too. If it returns GL_TRUE, it
184 * will mean that it caught the mouse cursor and we do not need
185 * to regenerate the activity list, and so our parents do...
187 GLboolean return_status = fghCheckMenuStatus( window,
188 menuEntry->SubMenu );
191 * Reactivate the submenu as the checkMenuStatus may have turned
192 * it off if the mouse is in its parent menu entry.
194 menuEntry->SubMenu->IsActive = GL_TRUE;
200 /* That much about our sub menus, let's get to checking the current menu: */
201 x = window->State.MouseX;
202 y = window->State.MouseY;
204 for( menuEntry = (SFG_MenuEntry *)menu->Entries.First;
206 menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next )
207 menuEntry->IsActive = GL_FALSE;
209 menu->IsActive = GL_FALSE;
211 /* Check if the mouse cursor is contained within the current menu box */
212 if( ( x >= FREEGLUT_MENU_BORDER ) &&
213 ( x < menu->Width - FREEGLUT_MENU_BORDER ) &&
214 ( y >= FREEGLUT_MENU_BORDER ) &&
215 ( y < menu->Height - FREEGLUT_MENU_BORDER ) &&
216 ( window == menu->Window ) )
218 int menuID = ( y - FREEGLUT_MENU_BORDER ) / FREEGLUT_MENU_HEIGHT;
220 /* The mouse cursor is somewhere over our box, check it out. */
221 menuEntry = fghFindMenuEntry( menu, menuID + 1 );
222 FREEGLUT_INTERNAL_ERROR_EXIT( menuEntry, "Cannot find menu entry",
223 "fghCheckMenuStatus" );
225 menuEntry->IsActive = GL_TRUE;
226 menuEntry->Ordinal = menuID;
229 * If this is not the same as the last active menu entry, deactivate
230 * the previous entry. Specifically, if the previous active entry
231 * was a submenu then deactivate it.
233 if( menu->ActiveEntry && ( menuEntry != menu->ActiveEntry ) )
234 if( menu->ActiveEntry->SubMenu )
235 fghDeactivateSubMenu( menu->ActiveEntry );
237 menu->ActiveEntry = menuEntry;
238 menu->IsActive = GL_TRUE;
241 * OKi, we have marked that entry as active, but it would be also
242 * nice to have its contents updated, in case it's a sub menu.
243 * Also, ignore the return value of the check function:
245 if( menuEntry->SubMenu )
247 if ( ! menuEntry->SubMenu->IsActive )
250 SFG_Window *current_window = fgStructure.CurrentWindow;
252 /* Set up the initial menu position now... */
253 menuEntry->SubMenu->IsActive = GL_TRUE;
255 /* Set up the initial submenu position now: */
256 fghGetVMaxExtent(menu->ParentWindow, &max_x, &max_y);
257 menuEntry->SubMenu->X = menu->X + menu->Width;
258 menuEntry->SubMenu->Y = menu->Y +
259 menuEntry->Ordinal * FREEGLUT_MENU_HEIGHT;
261 if( menuEntry->SubMenu->X + menuEntry->SubMenu->Width > max_x )
262 menuEntry->SubMenu->X = menu->X - menuEntry->SubMenu->Width;
264 if( menuEntry->SubMenu->Y + menuEntry->SubMenu->Height > max_y )
265 menuEntry->SubMenu->Y -= ( menuEntry->SubMenu->Height -
266 FREEGLUT_MENU_HEIGHT -
267 2 * FREEGLUT_MENU_BORDER );
269 fgSetWindow( menuEntry->SubMenu->Window );
270 glutPositionWindow( menuEntry->SubMenu->X,
271 menuEntry->SubMenu->Y );
272 glutReshapeWindow( menuEntry->SubMenu->Width,
273 menuEntry->SubMenu->Height );
276 menuEntry->SubMenu->Window->ActiveMenu = menuEntry->SubMenu;
277 fgSetWindow( current_window );
280 fghCheckMenuStatus( window, menuEntry->SubMenu );
282 /* Activate it because its parent entry is active */
283 menuEntry->SubMenu->IsActive = GL_TRUE;
286 /* Report back that we have caught the menu cursor */
290 /* Looks like the menu cursor is somewhere else... */
295 * Displays a menu box and all of its submenus (if they are active)
297 static void fghDisplayMenuBox( SFG_Menu* menu )
299 SFG_MenuEntry *menuEntry;
301 int border = FREEGLUT_MENU_BORDER;
304 * Have the menu box drawn first. The +- values are
305 * here just to make it more nice-looking...
307 /* a non-black dark version of the below. */
308 glColor4f( 1.0f, 1.0f, 1.0f, 1.0f );
309 glBegin( GL_QUAD_STRIP );
310 glVertex2i( menu->Width , 0 );
311 glVertex2i( menu->Width - border, border);
313 glVertex2i( border, border);
314 glVertex2i( 0 , menu->Height );
315 glVertex2i( border, menu->Height - border);
318 /* a non-black dark version of the below. */
319 glColor4f( 0.5f, 0.5f, 0.5f, 1.0f );
320 glBegin( GL_QUAD_STRIP );
321 glVertex2i( 0 , menu->Height );
322 glVertex2i( border, menu->Height - border);
323 glVertex2i( menu->Width , menu->Height );
324 glVertex2i( menu->Width - border, menu->Height - border);
325 glVertex2i( menu->Width , 0 );
326 glVertex2i( menu->Width - border, border);
329 glColor4fv( menu_pen_back );
331 glVertex2i( border, border);
332 glVertex2i( menu->Width - border, border);
333 glVertex2i( menu->Width - border, menu->Height - border);
334 glVertex2i( border, menu->Height - border);
337 /* Check if any of the submenus is currently active... */
338 for( menuEntry = (SFG_MenuEntry *)menu->Entries.First;
340 menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next )
342 /* Has the menu been marked as active, maybe? */
343 if( menuEntry->IsActive )
346 * That's truly right, and we need to have it highlighted.
347 * There is an assumption that mouse cursor didn't move
348 * since the last check of menu activity state:
350 int menuID = menuEntry->Ordinal;
352 /* So have the highlight drawn... */
353 glColor4fv( menu_pen_hback );
356 (menuID + 0)*FREEGLUT_MENU_HEIGHT + border );
357 glVertex2i( menu->Width - border,
358 (menuID + 0)*FREEGLUT_MENU_HEIGHT + border );
359 glVertex2i( menu->Width - border,
360 (menuID + 1)*FREEGLUT_MENU_HEIGHT + border );
362 (menuID + 1)*FREEGLUT_MENU_HEIGHT + border );
367 /* Print the menu entries now... */
369 glColor4fv( menu_pen_fore );
371 for( menuEntry = (SFG_MenuEntry *)menu->Entries.First, i = 0;
373 menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next, ++i )
375 /* If the menu entry is active, set the color to white */
376 if( menuEntry->IsActive )
377 glColor4fv( menu_pen_hfore );
379 /* Move the raster into position... */
380 /* Try to center the text - JCJ 31 July 2003*/
383 ( i + 1 )*FREEGLUT_MENU_HEIGHT -
384 ( int )( FREEGLUT_MENU_HEIGHT*0.3 - border )
387 /* Have the label drawn, character after character: */
388 glutBitmapString( FREEGLUT_MENU_FONT,
389 (unsigned char *)menuEntry->Text);
391 /* If it's a submenu, draw a right arrow */
392 if( menuEntry->SubMenu )
394 int width = glutBitmapWidth( FREEGLUT_MENU_FONT, '_' );
395 int x_base = menu->Width - 2 - width;
396 int y_base = i*FREEGLUT_MENU_HEIGHT + border;
397 glBegin( GL_TRIANGLES );
398 glVertex2i( x_base, y_base + 2*border);
399 glVertex2i( menu->Width - 2, y_base +
400 ( FREEGLUT_MENU_HEIGHT + border) / 2 );
401 glVertex2i( x_base, y_base + FREEGLUT_MENU_HEIGHT - border );
405 /* If the menu entry is active, reset the color */
406 if( menuEntry->IsActive )
407 glColor4fv( menu_pen_fore );
410 /* Now we are ready to check if any of our children needs to be redrawn: */
411 for( menuEntry = ( SFG_MenuEntry * )menu->Entries.First;
413 menuEntry = ( SFG_MenuEntry * )menuEntry->Node.Next )
415 /* Is that an active sub menu by any case? */
416 if( menuEntry->SubMenu && menuEntry->IsActive )
418 /* Yeah, indeed. Have it redrawn now: */
419 fgSetWindow( menuEntry->SubMenu->Window );
420 fghDisplayMenuBox( menuEntry->SubMenu );
421 fgSetWindow( menu->Window );
427 * Private static function to set the parent window of a submenu and all
430 static void fghSetMenuParentWindow( SFG_Window *window, SFG_Menu *menu )
432 SFG_MenuEntry *menuEntry;
434 menu->ParentWindow = window;
436 for( menuEntry = ( SFG_MenuEntry * )menu->Entries.First;
438 menuEntry = ( SFG_MenuEntry * )menuEntry->Node.Next )
439 if( menuEntry->SubMenu )
440 fghSetMenuParentWindow( window, menuEntry->SubMenu );
444 * Function to check for menu entry selection on menu deactivation
446 static void fghExecuteMenuCallback( SFG_Menu* menu )
448 SFG_MenuEntry *menuEntry;
450 /* First of all check any of the active sub menus... */
451 for( menuEntry = (SFG_MenuEntry *)menu->Entries.First;
453 menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next)
455 if( menuEntry->IsActive )
457 if( menuEntry->SubMenu )
458 fghExecuteMenuCallback( menuEntry->SubMenu );
461 menu->Callback( menuEntry->ID );
469 * Displays the currently active menu for the current window
471 void fgDisplayMenu( void )
473 SFG_Window* window = fgStructure.CurrentWindow;
474 SFG_Menu* menu = NULL;
476 FREEGLUT_INTERNAL_ERROR_EXIT ( fgStructure.CurrentWindow, "Displaying menu in nonexistent window",
479 /* Check if there is an active menu attached to this window... */
480 menu = window->ActiveMenu;
481 freeglut_return_if_fail( menu );
483 fgSetWindow( menu->Window );
485 glPushAttrib( GL_DEPTH_BUFFER_BIT | GL_TEXTURE_BIT | GL_LIGHTING_BIT |
488 glDisable( GL_DEPTH_TEST );
489 glDisable( GL_TEXTURE_2D );
490 glDisable( GL_LIGHTING );
491 glDisable( GL_CULL_FACE );
493 glMatrixMode( GL_PROJECTION );
497 0, glutGet( GLUT_WINDOW_WIDTH ),
498 glutGet( GLUT_WINDOW_HEIGHT ), 0,
502 glMatrixMode( GL_MODELVIEW );
506 fghCheckMenuStatus( window, menu );
507 fghDisplayMenuBox( menu );
511 glMatrixMode( GL_PROJECTION );
513 glMatrixMode( GL_MODELVIEW );
518 fgSetWindow ( window );
522 * Activates a menu pointed by the function argument
524 static void fghActivateMenu( SFG_Window* window, int button )
528 /* We'll be referencing this menu a lot, so remember its address: */
529 SFG_Menu* menu = window->Menu[ button ];
531 /* If the menu is already active in another window, deactivate it there */
532 if ( menu->ParentWindow )
533 menu->ParentWindow->ActiveMenu = NULL ;
535 /* Mark the menu as active, so that it gets displayed: */
536 window->ActiveMenu = menu;
537 menu->IsActive = GL_TRUE;
538 fghSetMenuParentWindow ( window, menu );
539 fgState.ActiveMenus++;
541 /* Set up the initial menu position now: */
542 fghGetVMaxExtent(menu->ParentWindow, &max_x, &max_y);
543 menu->X = window->State.MouseX + max_x;
544 menu->Y = window->State.MouseY + max_y;
546 if( menu->X + menu->Width > glutGet ( GLUT_SCREEN_WIDTH ) )
547 menu->X -=menu->Width;
549 if( menu->Y + menu->Height > glutGet ( GLUT_SCREEN_HEIGHT ) )
550 menu->Y -=menu->Height;
552 fgSetWindow( menu->Window );
553 glutPositionWindow( menu->X, menu->Y );
554 glutReshapeWindow( menu->Width, menu->Height );
557 menu->Window->ActiveMenu = menu;
561 * Check whether an active menu absorbs a mouse click
563 GLboolean fgCheckActiveMenu ( SFG_Window *window, int button, GLboolean pressed,
564 int mouse_x, int mouse_y )
567 * Near as I can tell, this is the menu behaviour:
568 * - Down-click the menu button, menu not active: activate
569 * the menu with its upper left-hand corner at the mouse
571 * - Down-click any button outside the menu, menu active:
572 * deactivate the menu
573 * - Down-click any button inside the menu, menu active:
574 * select the menu entry and deactivate the menu
575 * - Up-click the menu button, menu not active: nothing happens
576 * - Up-click the menu button outside the menu, menu active:
578 * - Up-click the menu button inside the menu, menu active:
579 * select the menu entry and deactivate the menu
580 * Since menus can have submenus, we need to check this recursively.
582 if( window->ActiveMenu )
584 if( window == window->ActiveMenu->ParentWindow )
586 window->ActiveMenu->Window->State.MouseX =
587 mouse_x - window->ActiveMenu->X;
588 window->ActiveMenu->Window->State.MouseY =
589 mouse_y - window->ActiveMenu->Y;
592 /* In the menu, invoke the callback and deactivate the menu */
593 if( fghCheckMenuStatus( window->ActiveMenu->Window,
594 window->ActiveMenu ) )
597 * Save the current window and menu and set the current
598 * window to the window whose menu this is
600 SFG_Window *save_window = fgStructure.CurrentWindow;
601 SFG_Menu *save_menu = fgStructure.CurrentMenu;
602 SFG_Window *parent_window = window->ActiveMenu->ParentWindow;
603 fgSetWindow( parent_window );
604 fgStructure.CurrentMenu = window->ActiveMenu;
606 /* Execute the menu callback */
607 fghExecuteMenuCallback( window->ActiveMenu );
608 fgDeactivateMenu( parent_window );
610 /* Restore the current window and menu */
611 fgSetWindow( save_window );
612 fgStructure.CurrentMenu = save_menu;
616 * Outside the menu, deactivate if it's a downclick
618 * XXX This isn't enough. A downclick outside of
619 * XXX the interior of our freeglut windows should also
620 * XXX deactivate the menu. This is more complicated.
622 fgDeactivateMenu( window->ActiveMenu->ParentWindow );
625 * XXX Why does an active menu require a redisplay at
626 * XXX this point? If this can come out cleanly, then
627 * XXX it probably should do so; if not, a comment should
630 if( ! window->IsMenu )
631 window->State.Redisplay = GL_TRUE;
636 /* No active menu, let's check whether we need to activate one. */
637 if( ( 0 <= button ) &&
638 ( FREEGLUT_MAX_MENUS > button ) &&
639 ( window->Menu[ button ] ) &&
642 /* XXX Posting a requisite Redisplay seems bogus. */
643 window->State.Redisplay = GL_TRUE;
644 fgSetWindow( window );
645 fghActivateMenu( window, button );
653 * Deactivates a menu pointed by the function argument.
655 void fgDeactivateMenu( SFG_Window *window )
657 SFG_Window *current_window = fgStructure.CurrentWindow;
659 /* Check if there is an active menu attached to this window... */
660 SFG_Menu* menu = window->ActiveMenu;
661 SFG_MenuEntry *menuEntry;
663 /* Did we find an active window? */
664 freeglut_return_if_fail( menu );
666 /* Hide the present menu's window */
667 fgSetWindow( menu->Window );
670 /* Forget about having that menu active anymore, now: */
671 menu->Window->ActiveMenu = NULL;
672 menu->ParentWindow->ActiveMenu = NULL;
673 fghSetMenuParentWindow ( NULL, menu );
674 menu->IsActive = GL_FALSE;
676 fgState.ActiveMenus--;
678 /* Hide all submenu windows, and the root menu's window. */
679 for ( menuEntry = ( SFG_MenuEntry * )menu->Entries.First;
681 menuEntry = ( SFG_MenuEntry * )menuEntry->Node.Next )
683 /* Is that an active submenu by any case? */
684 if( menuEntry->SubMenu )
685 fghDeactivateSubMenu( menuEntry );
688 fgSetWindow( current_window );
692 * Recalculates current menu's box size
694 void fghCalculateMenuBoxSize( void )
696 SFG_MenuEntry* menuEntry;
697 int width = 0, height = 0;
699 /* Make sure there is a current menu set */
700 freeglut_return_if_fail( fgStructure.CurrentMenu );
702 /* The menu's box size depends on the menu entries: */
703 for( menuEntry = ( SFG_MenuEntry * )fgStructure.CurrentMenu->Entries.First;
705 menuEntry = ( SFG_MenuEntry * )menuEntry->Node.Next )
707 /* Update the menu entry's width value */
708 menuEntry->Width = glutBitmapLength(
710 (unsigned char *)menuEntry->Text
714 * If the entry is a submenu, then it needs to be wider to
715 * accomodate the arrow. JCJ 31 July 2003
717 if (menuEntry->SubMenu )
718 menuEntry->Width += glutBitmapLength(
723 /* Check if it's the biggest we've found */
724 if( menuEntry->Width > width )
725 width = menuEntry->Width;
727 height += FREEGLUT_MENU_HEIGHT;
730 /* Store the menu's box size now: */
731 fgStructure.CurrentMenu->Height = height + 2 * FREEGLUT_MENU_BORDER;
732 fgStructure.CurrentMenu->Width = width + 4 * FREEGLUT_MENU_BORDER;
736 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
739 * Creates a new menu object, adding it to the freeglut structure
741 int FGAPIENTRY glutCreateMenu( void(* callback)( int ) )
743 /* The menu object creation code resides in freeglut_structure.c */
744 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutCreateMenu" );
745 return fgCreateMenu( callback )->ID;
749 * Destroys a menu object, removing all references to it
751 void FGAPIENTRY glutDestroyMenu( int menuID )
755 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutDestroyMenu" );
756 menu = fgMenuByID( menuID );
758 freeglut_return_if_fail( menu );
760 /* The menu object destruction code resides in freeglut_structure.c */
761 fgDestroyMenu( menu );
765 * Returns the ID number of the currently active menu
767 int FGAPIENTRY glutGetMenu( void )
769 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGetMenu" );
771 if( fgStructure.CurrentMenu )
772 return fgStructure.CurrentMenu->ID;
778 * Sets the current menu given its menu ID
780 void FGAPIENTRY glutSetMenu( int menuID )
784 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetMenu" );
785 menu = fgMenuByID( menuID );
787 freeglut_return_if_fail( menu );
789 fgStructure.CurrentMenu = menu;
793 * Adds a menu entry to the bottom of the current menu
795 void FGAPIENTRY glutAddMenuEntry( const char* label, int value )
797 SFG_MenuEntry* menuEntry;
798 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutAddMenuEntry" );
799 menuEntry = (SFG_MenuEntry *)calloc( sizeof(SFG_MenuEntry), 1 );
800 freeglut_return_if_fail( fgStructure.CurrentMenu );
802 menuEntry->Text = strdup( label );
803 menuEntry->ID = value;
805 /* Have the new menu entry attached to the current menu */
806 fgListAppend( &fgStructure.CurrentMenu->Entries, &menuEntry->Node );
808 fghCalculateMenuBoxSize( );
812 * Add a sub menu to the bottom of the current menu
814 void FGAPIENTRY glutAddSubMenu( const char *label, int subMenuID )
816 SFG_MenuEntry *menuEntry;
819 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutAddSubMenu" );
820 menuEntry = ( SFG_MenuEntry * )calloc( sizeof( SFG_MenuEntry ), 1 );
821 subMenu = fgMenuByID( subMenuID );
823 freeglut_return_if_fail( fgStructure.CurrentMenu );
824 freeglut_return_if_fail( subMenu );
826 menuEntry->Text = strdup( label );
827 menuEntry->SubMenu = subMenu;
830 fgListAppend( &fgStructure.CurrentMenu->Entries, &menuEntry->Node );
831 fghCalculateMenuBoxSize( );
835 * Changes the specified menu item in the current menu into a menu entry
837 void FGAPIENTRY glutChangeToMenuEntry( int item, const char* label, int value )
839 SFG_MenuEntry* menuEntry = NULL;
841 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutChangeToMenuEntry" );
842 freeglut_return_if_fail( fgStructure.CurrentMenu );
844 /* Get n-th menu entry in the current menu, starting from one: */
845 menuEntry = fghFindMenuEntry( fgStructure.CurrentMenu, item );
847 freeglut_return_if_fail( menuEntry );
849 /* We want it to become a normal menu entry, so: */
850 if( menuEntry->Text )
851 free( menuEntry->Text );
853 menuEntry->Text = strdup( label );
854 menuEntry->ID = value;
855 menuEntry->SubMenu = NULL;
856 fghCalculateMenuBoxSize( );
860 * Changes the specified menu item in the current menu into a sub-menu trigger.
862 void FGAPIENTRY glutChangeToSubMenu( int item, const char* label,
866 SFG_MenuEntry* menuEntry;
868 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutChangeToSubMenu" );
869 subMenu = fgMenuByID( subMenuID );
872 freeglut_return_if_fail( fgStructure.CurrentMenu );
873 freeglut_return_if_fail( subMenu );
875 /* Get n-th menu entry in the current menu, starting from one: */
876 menuEntry = fghFindMenuEntry( fgStructure.CurrentMenu, item );
878 freeglut_return_if_fail( menuEntry );
880 /* We want it to become a sub menu entry, so: */
881 if( menuEntry->Text )
882 free( menuEntry->Text );
884 menuEntry->Text = strdup( label );
885 menuEntry->SubMenu = subMenu;
887 fghCalculateMenuBoxSize( );
891 * Removes the specified menu item from the current menu
893 void FGAPIENTRY glutRemoveMenuItem( int item )
895 SFG_MenuEntry* menuEntry;
897 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutRemoveMenuItem" );
898 freeglut_return_if_fail( fgStructure.CurrentMenu );
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 fgListRemove( &fgStructure.CurrentMenu->Entries, &menuEntry->Node );
906 if ( menuEntry->Text )
907 free( menuEntry->Text );
910 fghCalculateMenuBoxSize( );
914 * Attaches a menu to the current window
916 void FGAPIENTRY glutAttachMenu( int button )
918 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutAttachMenu" );
920 freeglut_return_if_fail( fgStructure.CurrentWindow );
921 freeglut_return_if_fail( fgStructure.CurrentMenu );
923 freeglut_return_if_fail( button >= 0 );
924 freeglut_return_if_fail( button < FREEGLUT_MAX_MENUS );
926 fgStructure.CurrentWindow->Menu[ button ] = fgStructure.CurrentMenu;
930 * Detaches a menu from the current window
932 void FGAPIENTRY glutDetachMenu( int button )
934 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutDetachMenu" );
936 freeglut_return_if_fail( fgStructure.CurrentWindow );
937 freeglut_return_if_fail( fgStructure.CurrentMenu );
939 freeglut_return_if_fail( button >= 0 );
940 freeglut_return_if_fail( button < FREEGLUT_MAX_MENUS );
942 fgStructure.CurrentWindow->Menu[ button ] = NULL;
946 * A.Donev: Set and retrieve the menu's user data
948 void* FGAPIENTRY glutGetMenuData( void )
950 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGetMenuData" );
951 return fgStructure.CurrentMenu->UserData;
954 void FGAPIENTRY glutSetMenuData(void* data)
956 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetMenuData" );
957 fgStructure.CurrentMenu->UserData=data;
960 /*** END OF FILE ***/