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;
122 menuEntry->SubMenu->ActiveEntry = NULL;
124 /* Hide all submenu windows, and the root menu's window. */
125 for ( subMenuIter = (SFG_MenuEntry *)menuEntry->SubMenu->Entries.First;
127 subMenuIter = (SFG_MenuEntry *)subMenuIter->Node.Next )
129 subMenuIter->IsActive = GL_FALSE;
131 /* Is that an active submenu by any case? */
132 if( subMenuIter->SubMenu )
133 fghDeactivateSubMenu( subMenuIter );
136 fgSetWindow ( menuEntry->SubMenu->ParentWindow ) ;
140 * Private function to get the virtual maximum screen extent
142 static GLvoid fghGetVMaxExtent( SFG_Window* window, int* x, int* y )
144 if( fgStructure.GameMode )
146 #if TARGET_HOST_UNIX_X11
150 XTranslateCoordinates(
152 window->Window.Handle,
153 fgDisplay.RootWindow,
156 *x = fgState.GameModeSize.X + wx;
157 *y = fgState.GameModeSize.Y + wy;
159 *x = glutGet ( GLUT_SCREEN_WIDTH );
160 *y = glutGet ( GLUT_SCREEN_HEIGHT );
165 *x = fgDisplay.ScreenWidth;
166 *y = fgDisplay.ScreenHeight;
171 * Private function to check for the current menu/sub menu activity state
173 static GLboolean fghCheckMenuStatus( SFG_Menu* menu )
175 SFG_MenuEntry* menuEntry;
178 /* First of all check any of the active sub menus... */
179 for( menuEntry = (SFG_MenuEntry *)menu->Entries.First;
181 menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next )
183 if( menuEntry->SubMenu && menuEntry->IsActive )
186 * OK, have the sub-menu checked, too. If it returns GL_TRUE, it
187 * will mean that it caught the mouse cursor and we do not need
188 * to regenerate the activity list, and so our parents do...
190 GLboolean return_status;
192 menuEntry->SubMenu->Window->State.MouseX =
193 menu->Window->State.MouseX + menu->X - menuEntry->SubMenu->X;
194 menuEntry->SubMenu->Window->State.MouseY =
195 menu->Window->State.MouseY + menu->Y - menuEntry->SubMenu->Y;
196 return_status = fghCheckMenuStatus( menuEntry->SubMenu );
203 /* That much about our sub menus, let's get to checking the current menu: */
204 x = menu->Window->State.MouseX;
205 y = menu->Window->State.MouseY;
207 /* Check if the mouse cursor is contained within the current menu box */
208 if( ( x >= FREEGLUT_MENU_BORDER ) &&
209 ( x < menu->Width - FREEGLUT_MENU_BORDER ) &&
210 ( y >= FREEGLUT_MENU_BORDER ) &&
211 ( y < menu->Height - FREEGLUT_MENU_BORDER ) )
213 int menuID = ( y - FREEGLUT_MENU_BORDER ) / FREEGLUT_MENU_HEIGHT;
215 /* The mouse cursor is somewhere over our box, check it out. */
216 menuEntry = fghFindMenuEntry( menu, menuID + 1 );
217 FREEGLUT_INTERNAL_ERROR_EXIT( menuEntry, "Cannot find menu entry",
218 "fghCheckMenuStatus" );
220 menuEntry->IsActive = GL_TRUE;
221 menuEntry->Ordinal = menuID;
224 * If this is not the same as the last active menu entry, deactivate
225 * the previous entry. Specifically, if the previous active entry
226 * was a submenu then deactivate it.
228 if( menu->ActiveEntry && ( menuEntry != menu->ActiveEntry ) )
229 if( menu->ActiveEntry->SubMenu )
230 fghDeactivateSubMenu( menu->ActiveEntry );
232 if( menuEntry != menu->ActiveEntry )
234 menu->Window->State.Redisplay = GL_TRUE;
235 if( menu->ActiveEntry )
236 menu->ActiveEntry->IsActive = GL_FALSE;
239 menu->ActiveEntry = menuEntry;
240 menu->IsActive = GL_TRUE; /* XXX Do we need this? */
243 * OKi, we have marked that entry as active, but it would be also
244 * nice to have its contents updated, in case it's a sub menu.
245 * Also, ignore the return value of the check function:
247 if( menuEntry->SubMenu )
249 if ( ! menuEntry->SubMenu->IsActive )
252 SFG_Window *current_window = fgStructure.CurrentWindow;
254 /* Set up the initial menu position now... */
255 menuEntry->SubMenu->IsActive = GL_TRUE;
257 /* Set up the initial submenu position now: */
258 fghGetVMaxExtent(menu->ParentWindow, &max_x, &max_y);
259 menuEntry->SubMenu->X = menu->X + menu->Width;
260 menuEntry->SubMenu->Y = menu->Y +
261 menuEntry->Ordinal * FREEGLUT_MENU_HEIGHT;
263 if( menuEntry->SubMenu->X + menuEntry->SubMenu->Width > max_x )
264 menuEntry->SubMenu->X = menu->X - menuEntry->SubMenu->Width;
266 if( menuEntry->SubMenu->Y + menuEntry->SubMenu->Height > max_y )
267 menuEntry->SubMenu->Y -= ( menuEntry->SubMenu->Height -
268 FREEGLUT_MENU_HEIGHT -
269 2 * FREEGLUT_MENU_BORDER );
271 fgSetWindow( menuEntry->SubMenu->Window );
272 glutPositionWindow( menuEntry->SubMenu->X,
273 menuEntry->SubMenu->Y );
274 glutReshapeWindow( menuEntry->SubMenu->Width,
275 menuEntry->SubMenu->Height );
278 menuEntry->SubMenu->Window->ActiveMenu = menuEntry->SubMenu;
279 fgSetWindow( current_window );
280 menuEntry->SubMenu->Window->State.MouseX =
281 x + menu->X - menuEntry->SubMenu->X;
282 menuEntry->SubMenu->Window->State.MouseY =
283 y + menu->Y - menuEntry->SubMenu->Y;
284 fghCheckMenuStatus( menuEntry->SubMenu );
287 /* Activate it because its parent entry is active */
288 menuEntry->SubMenu->IsActive = GL_TRUE; /* XXX Do we need this? */
291 /* Report back that we have caught the menu cursor */
295 /* Looks like the menu cursor is somewhere else... */
296 if( menu->ActiveEntry && menu->ActiveEntry->IsActive &&
297 ( !menu->ActiveEntry->SubMenu ||
298 !menu->ActiveEntry->SubMenu->IsActive ) )
300 menu->Window->State.Redisplay = GL_TRUE;
301 menu->ActiveEntry->IsActive = GL_FALSE;
302 menu->ActiveEntry = NULL;
309 * Displays a menu box and all of its submenus (if they are active)
311 static void fghDisplayMenuBox( SFG_Menu* menu )
313 SFG_MenuEntry *menuEntry;
315 int border = FREEGLUT_MENU_BORDER;
318 * Have the menu box drawn first. The +- values are
319 * here just to make it more nice-looking...
321 /* a non-black dark version of the below. */
322 glColor4f( 1.0f, 1.0f, 1.0f, 1.0f );
323 glBegin( GL_QUAD_STRIP );
324 glVertex2i( menu->Width , 0 );
325 glVertex2i( menu->Width - border, border);
327 glVertex2i( border, border);
328 glVertex2i( 0 , menu->Height );
329 glVertex2i( border, menu->Height - border);
332 /* a non-black dark version of the below. */
333 glColor4f( 0.5f, 0.5f, 0.5f, 1.0f );
334 glBegin( GL_QUAD_STRIP );
335 glVertex2i( 0 , menu->Height );
336 glVertex2i( border, menu->Height - border);
337 glVertex2i( menu->Width , menu->Height );
338 glVertex2i( menu->Width - border, menu->Height - border);
339 glVertex2i( menu->Width , 0 );
340 glVertex2i( menu->Width - border, border);
343 glColor4fv( menu_pen_back );
345 glVertex2i( border, border);
346 glVertex2i( menu->Width - border, border);
347 glVertex2i( menu->Width - border, menu->Height - border);
348 glVertex2i( border, menu->Height - border);
351 /* Check if any of the submenus is currently active... */
352 for( menuEntry = (SFG_MenuEntry *)menu->Entries.First;
354 menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next )
356 /* Has the menu been marked as active, maybe? */
357 if( menuEntry->IsActive )
360 * That's truly right, and we need to have it highlighted.
361 * There is an assumption that mouse cursor didn't move
362 * since the last check of menu activity state:
364 int menuID = menuEntry->Ordinal;
366 /* So have the highlight drawn... */
367 glColor4fv( menu_pen_hback );
370 (menuID + 0)*FREEGLUT_MENU_HEIGHT + border );
371 glVertex2i( menu->Width - border,
372 (menuID + 0)*FREEGLUT_MENU_HEIGHT + border );
373 glVertex2i( menu->Width - border,
374 (menuID + 1)*FREEGLUT_MENU_HEIGHT + border );
376 (menuID + 1)*FREEGLUT_MENU_HEIGHT + border );
381 /* Print the menu entries now... */
383 glColor4fv( menu_pen_fore );
385 for( menuEntry = (SFG_MenuEntry *)menu->Entries.First, i = 0;
387 menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next, ++i )
389 /* If the menu entry is active, set the color to white */
390 if( menuEntry->IsActive )
391 glColor4fv( menu_pen_hfore );
393 /* Move the raster into position... */
394 /* Try to center the text - JCJ 31 July 2003*/
397 ( i + 1 )*FREEGLUT_MENU_HEIGHT -
398 ( int )( FREEGLUT_MENU_HEIGHT*0.3 - border )
401 /* Have the label drawn, character after character: */
402 glutBitmapString( FREEGLUT_MENU_FONT,
403 (unsigned char *)menuEntry->Text);
405 /* If it's a submenu, draw a right arrow */
406 if( menuEntry->SubMenu )
408 int width = glutBitmapWidth( FREEGLUT_MENU_FONT, '_' );
409 int x_base = menu->Width - 2 - width;
410 int y_base = i*FREEGLUT_MENU_HEIGHT + border;
411 glBegin( GL_TRIANGLES );
412 glVertex2i( x_base, y_base + 2*border);
413 glVertex2i( menu->Width - 2, y_base +
414 ( FREEGLUT_MENU_HEIGHT + border) / 2 );
415 glVertex2i( x_base, y_base + FREEGLUT_MENU_HEIGHT - border );
419 /* If the menu entry is active, reset the color */
420 if( menuEntry->IsActive )
421 glColor4fv( menu_pen_fore );
426 * Private static function to set the parent window of a submenu and all
429 static void fghSetMenuParentWindow( SFG_Window *window, SFG_Menu *menu )
431 SFG_MenuEntry *menuEntry;
433 menu->ParentWindow = window;
435 for( menuEntry = ( SFG_MenuEntry * )menu->Entries.First;
437 menuEntry = ( SFG_MenuEntry * )menuEntry->Node.Next )
438 if( menuEntry->SubMenu )
439 fghSetMenuParentWindow( window, menuEntry->SubMenu );
443 * Function to check for menu entry selection on menu deactivation
445 static void fghExecuteMenuCallback( SFG_Menu* menu )
447 SFG_MenuEntry *menuEntry;
449 /* First of all check any of the active sub menus... */
450 for( menuEntry = (SFG_MenuEntry *)menu->Entries.First;
452 menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next)
454 if( menuEntry->IsActive )
456 if( menuEntry->SubMenu )
457 fghExecuteMenuCallback( menuEntry->SubMenu );
461 SFG_Menu *save_menu = fgStructure.CurrentMenu;
462 fgStructure.CurrentMenu = menu;
463 menu->Callback( menuEntry->ID );
464 fgStructure.CurrentMenu = save_menu;
474 * Displays the currently active menu for the current window
476 void fgDisplayMenu( void )
478 SFG_Window* window = fgStructure.CurrentWindow;
479 SFG_Menu* menu = NULL;
481 FREEGLUT_INTERNAL_ERROR_EXIT ( fgStructure.CurrentWindow, "Displaying menu in nonexistent window",
484 /* Check if there is an active menu attached to this window... */
485 menu = window->ActiveMenu;
486 freeglut_return_if_fail( menu );
488 fgSetWindow( menu->Window );
490 glPushAttrib( GL_DEPTH_BUFFER_BIT | GL_TEXTURE_BIT | GL_LIGHTING_BIT |
493 glDisable( GL_DEPTH_TEST );
494 glDisable( GL_TEXTURE_2D );
495 glDisable( GL_LIGHTING );
496 glDisable( GL_CULL_FACE );
498 glMatrixMode( GL_PROJECTION );
502 0, glutGet( GLUT_WINDOW_WIDTH ),
503 glutGet( GLUT_WINDOW_HEIGHT ), 0,
507 glMatrixMode( GL_MODELVIEW );
511 fghDisplayMenuBox( menu );
515 glMatrixMode( GL_PROJECTION );
517 glMatrixMode( GL_MODELVIEW );
522 fgSetWindow ( window );
526 * Activates a menu pointed by the function argument
528 static void fghActivateMenu( SFG_Window* window, int button )
532 /* We'll be referencing this menu a lot, so remember its address: */
533 SFG_Menu* menu = window->Menu[ button ];
534 SFG_Window* current_window = fgStructure.CurrentWindow;
536 /* If the menu is already active in another window, deactivate it there */
537 if ( menu->ParentWindow )
538 menu->ParentWindow->ActiveMenu = NULL ;
540 /* Mark the menu as active, so that it gets displayed: */
541 window->ActiveMenu = menu;
542 menu->IsActive = GL_TRUE;
543 fghSetMenuParentWindow ( window, menu );
544 fgState.ActiveMenus++;
546 /* Set up the initial menu position now: */
547 fghGetVMaxExtent(menu->ParentWindow, &max_x, &max_y);
548 fgSetWindow( window );
549 menu->X = window->State.MouseX + glutGet( GLUT_WINDOW_X );
550 menu->Y = window->State.MouseY + glutGet( GLUT_WINDOW_Y );
552 if( menu->X + menu->Width > max_x )
553 menu->X -=menu->Width;
555 if( menu->Y + menu->Height > max_y )
556 menu->Y -=menu->Height;
558 menu->Window->State.MouseX =
559 window->State.MouseX + glutGet( GLUT_WINDOW_X ) - menu->X;
560 menu->Window->State.MouseY =
561 window->State.MouseY + glutGet( GLUT_WINDOW_Y ) - menu->Y;
563 fgSetWindow( menu->Window );
564 glutPositionWindow( menu->X, menu->Y );
565 glutReshapeWindow( menu->Width, menu->Height );
568 menu->Window->ActiveMenu = menu;
569 fghCheckMenuStatus( menu );
570 fgSetWindow( current_window );
574 * Update Highlight states of the menu
576 * Current mouse position is in menu->Window->State.MouseX/Y.
578 void fgUpdateMenuHighlight ( SFG_Menu *menu )
580 fghCheckMenuStatus( menu );
584 * Check whether an active menu absorbs a mouse click
586 GLboolean fgCheckActiveMenu ( SFG_Window *window, int button, GLboolean pressed,
587 int mouse_x, int mouse_y )
590 * Near as I can tell, this is the menu behaviour:
591 * - Down-click the menu button, menu not active: activate
592 * the menu with its upper left-hand corner at the mouse
594 * - Down-click any button outside the menu, menu active:
595 * deactivate the menu
596 * - Down-click any button inside the menu, menu active:
597 * select the menu entry and deactivate the menu
598 * - Up-click the menu button, menu not active: nothing happens
599 * - Up-click the menu button outside the menu, menu active:
601 * - Up-click the menu button inside the menu, menu active:
602 * select the menu entry and deactivate the menu
603 * Since menus can have submenus, we need to check this recursively.
605 if( window->ActiveMenu )
607 if( window == window->ActiveMenu->ParentWindow )
609 window->ActiveMenu->Window->State.MouseX =
610 mouse_x - window->ActiveMenu->X;
611 window->ActiveMenu->Window->State.MouseY =
612 mouse_y - window->ActiveMenu->Y;
615 /* In the menu, invoke the callback and deactivate the menu */
616 if( fghCheckMenuStatus( window->ActiveMenu ) )
619 * Save the current window and menu and set the current
620 * window to the window whose menu this is
622 SFG_Window *save_window = fgStructure.CurrentWindow;
623 SFG_Menu *save_menu = fgStructure.CurrentMenu;
624 SFG_Window *parent_window = window->ActiveMenu->ParentWindow;
625 fgSetWindow( parent_window );
626 fgStructure.CurrentMenu = window->ActiveMenu;
628 /* Execute the menu callback */
629 fghExecuteMenuCallback( window->ActiveMenu );
630 fgDeactivateMenu( parent_window );
632 /* Restore the current window and menu */
633 fgSetWindow( save_window );
634 fgStructure.CurrentMenu = save_menu;
638 * Outside the menu, deactivate if it's a downclick
640 * XXX This isn't enough. A downclick outside of
641 * XXX the interior of our freeglut windows should also
642 * XXX deactivate the menu. This is more complicated.
644 fgDeactivateMenu( window->ActiveMenu->ParentWindow );
647 * XXX Why does an active menu require a redisplay at
648 * XXX this point? If this can come out cleanly, then
649 * XXX it probably should do so; if not, a comment should
652 if( ! window->IsMenu )
653 window->State.Redisplay = GL_TRUE;
658 /* No active menu, let's check whether we need to activate one. */
659 if( ( 0 <= button ) &&
660 ( FREEGLUT_MAX_MENUS > button ) &&
661 ( window->Menu[ button ] ) &&
664 /* XXX Posting a requisite Redisplay seems bogus. */
665 window->State.Redisplay = GL_TRUE;
666 fghActivateMenu( window, button );
674 * Deactivates a menu pointed by the function argument.
676 void fgDeactivateMenu( SFG_Window *window )
678 SFG_Window *current_window = fgStructure.CurrentWindow;
679 SFG_Window *parent_window = NULL;
681 /* Check if there is an active menu attached to this window... */
682 SFG_Menu* menu = window->ActiveMenu;
683 SFG_MenuEntry *menuEntry;
685 /* Did we find an active window? */
686 freeglut_return_if_fail( menu );
688 parent_window = menu->ParentWindow;
690 /* Hide the present menu's window */
691 fgSetWindow( menu->Window );
694 /* Forget about having that menu active anymore, now: */
695 menu->Window->ActiveMenu = NULL;
696 menu->ParentWindow->ActiveMenu = NULL;
697 fghSetMenuParentWindow ( NULL, menu );
698 menu->IsActive = GL_FALSE;
699 menu->ActiveEntry = NULL;
701 fgState.ActiveMenus--;
703 /* Hide all submenu windows, and the root menu's window. */
704 for ( menuEntry = ( SFG_MenuEntry * )menu->Entries.First;
706 menuEntry = ( SFG_MenuEntry * )menuEntry->Node.Next )
708 menuEntry->IsActive = GL_FALSE;
710 /* Is that an active submenu by any case? */
711 if( menuEntry->SubMenu )
712 fghDeactivateSubMenu( menuEntry );
715 fgSetWindow ( parent_window ) ;
719 * Recalculates current menu's box size
721 void fghCalculateMenuBoxSize( void )
723 SFG_MenuEntry* menuEntry;
724 int width = 0, height = 0;
726 /* Make sure there is a current menu set */
727 freeglut_return_if_fail( fgStructure.CurrentMenu );
729 /* The menu's box size depends on the menu entries: */
730 for( menuEntry = ( SFG_MenuEntry * )fgStructure.CurrentMenu->Entries.First;
732 menuEntry = ( SFG_MenuEntry * )menuEntry->Node.Next )
734 /* Update the menu entry's width value */
735 menuEntry->Width = glutBitmapLength(
737 (unsigned char *)menuEntry->Text
741 * If the entry is a submenu, then it needs to be wider to
742 * accomodate the arrow. JCJ 31 July 2003
744 if (menuEntry->SubMenu )
745 menuEntry->Width += glutBitmapLength(
750 /* Check if it's the biggest we've found */
751 if( menuEntry->Width > width )
752 width = menuEntry->Width;
754 height += FREEGLUT_MENU_HEIGHT;
757 /* Store the menu's box size now: */
758 fgStructure.CurrentMenu->Height = height + 2 * FREEGLUT_MENU_BORDER;
759 fgStructure.CurrentMenu->Width = width + 4 * FREEGLUT_MENU_BORDER;
763 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
766 * Creates a new menu object, adding it to the freeglut structure
768 int FGAPIENTRY glutCreateMenu( void(* callback)( int ) )
770 /* The menu object creation code resides in freeglut_structure.c */
771 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutCreateMenu" );
772 return fgCreateMenu( callback )->ID;
776 * Destroys a menu object, removing all references to it
778 void FGAPIENTRY glutDestroyMenu( int menuID )
782 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutDestroyMenu" );
783 menu = fgMenuByID( menuID );
785 freeglut_return_if_fail( menu );
787 /* The menu object destruction code resides in freeglut_structure.c */
788 fgDestroyMenu( menu );
792 * Returns the ID number of the currently active menu
794 int FGAPIENTRY glutGetMenu( void )
796 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGetMenu" );
798 if( fgStructure.CurrentMenu )
799 return fgStructure.CurrentMenu->ID;
805 * Sets the current menu given its menu ID
807 void FGAPIENTRY glutSetMenu( int menuID )
811 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetMenu" );
812 menu = fgMenuByID( menuID );
814 freeglut_return_if_fail( menu );
816 fgStructure.CurrentMenu = menu;
820 * Adds a menu entry to the bottom of the current menu
822 void FGAPIENTRY glutAddMenuEntry( const char* label, int value )
824 SFG_MenuEntry* menuEntry;
825 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutAddMenuEntry" );
826 menuEntry = (SFG_MenuEntry *)calloc( sizeof(SFG_MenuEntry), 1 );
827 freeglut_return_if_fail( fgStructure.CurrentMenu );
829 menuEntry->Text = strdup( label );
830 menuEntry->ID = value;
832 /* Have the new menu entry attached to the current menu */
833 fgListAppend( &fgStructure.CurrentMenu->Entries, &menuEntry->Node );
835 fghCalculateMenuBoxSize( );
839 * Add a sub menu to the bottom of the current menu
841 void FGAPIENTRY glutAddSubMenu( const char *label, int subMenuID )
843 SFG_MenuEntry *menuEntry;
846 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutAddSubMenu" );
847 menuEntry = ( SFG_MenuEntry * )calloc( sizeof( SFG_MenuEntry ), 1 );
848 subMenu = fgMenuByID( subMenuID );
850 freeglut_return_if_fail( fgStructure.CurrentMenu );
851 freeglut_return_if_fail( subMenu );
853 menuEntry->Text = strdup( label );
854 menuEntry->SubMenu = subMenu;
857 fgListAppend( &fgStructure.CurrentMenu->Entries, &menuEntry->Node );
858 fghCalculateMenuBoxSize( );
862 * Changes the specified menu item in the current menu into a menu entry
864 void FGAPIENTRY glutChangeToMenuEntry( int item, const char* label, int value )
866 SFG_MenuEntry* menuEntry = NULL;
868 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutChangeToMenuEntry" );
869 freeglut_return_if_fail( fgStructure.CurrentMenu );
871 /* Get n-th menu entry in the current menu, starting from one: */
872 menuEntry = fghFindMenuEntry( fgStructure.CurrentMenu, item );
874 freeglut_return_if_fail( menuEntry );
876 /* We want it to become a normal menu entry, so: */
877 if( menuEntry->Text )
878 free( menuEntry->Text );
880 menuEntry->Text = strdup( label );
881 menuEntry->ID = value;
882 menuEntry->SubMenu = NULL;
883 fghCalculateMenuBoxSize( );
887 * Changes the specified menu item in the current menu into a sub-menu trigger.
889 void FGAPIENTRY glutChangeToSubMenu( int item, const char* label,
893 SFG_MenuEntry* menuEntry;
895 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutChangeToSubMenu" );
896 subMenu = fgMenuByID( subMenuID );
899 freeglut_return_if_fail( fgStructure.CurrentMenu );
900 freeglut_return_if_fail( subMenu );
902 /* Get n-th menu entry in the current menu, starting from one: */
903 menuEntry = fghFindMenuEntry( fgStructure.CurrentMenu, item );
905 freeglut_return_if_fail( menuEntry );
907 /* We want it to become a sub menu entry, so: */
908 if( menuEntry->Text )
909 free( menuEntry->Text );
911 menuEntry->Text = strdup( label );
912 menuEntry->SubMenu = subMenu;
914 fghCalculateMenuBoxSize( );
918 * Removes the specified menu item from the current menu
920 void FGAPIENTRY glutRemoveMenuItem( int item )
922 SFG_MenuEntry* menuEntry;
924 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutRemoveMenuItem" );
925 freeglut_return_if_fail( fgStructure.CurrentMenu );
927 /* Get n-th menu entry in the current menu, starting from one: */
928 menuEntry = fghFindMenuEntry( fgStructure.CurrentMenu, item );
930 freeglut_return_if_fail( menuEntry );
932 fgListRemove( &fgStructure.CurrentMenu->Entries, &menuEntry->Node );
933 if ( menuEntry->Text )
934 free( menuEntry->Text );
937 fghCalculateMenuBoxSize( );
941 * Attaches a menu to the current window
943 void FGAPIENTRY glutAttachMenu( int button )
945 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutAttachMenu" );
947 freeglut_return_if_fail( fgStructure.CurrentWindow );
948 freeglut_return_if_fail( fgStructure.CurrentMenu );
950 freeglut_return_if_fail( button >= 0 );
951 freeglut_return_if_fail( button < FREEGLUT_MAX_MENUS );
953 fgStructure.CurrentWindow->Menu[ button ] = fgStructure.CurrentMenu;
957 * Detaches a menu from the current window
959 void FGAPIENTRY glutDetachMenu( int button )
961 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutDetachMenu" );
963 freeglut_return_if_fail( fgStructure.CurrentWindow );
964 freeglut_return_if_fail( fgStructure.CurrentMenu );
966 freeglut_return_if_fail( button >= 0 );
967 freeglut_return_if_fail( button < FREEGLUT_MAX_MENUS );
969 fgStructure.CurrentWindow->Menu[ button ] = NULL;
973 * A.Donev: Set and retrieve the menu's user data
975 void* FGAPIENTRY glutGetMenuData( void )
977 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGetMenuData" );
978 return fgStructure.CurrentMenu->UserData;
981 void FGAPIENTRY glutSetMenuData(void* data)
983 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetMenuData" );
984 fgStructure.CurrentMenu->UserData=data;
987 /*** END OF FILE ***/