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"
33 /* -- DEFINITIONS ---------------------------------------------------------- */
36 * FREEGLUT_MENU_FONT can be any freeglut bitmapped font.
37 * (Stroked fonts would not be out of the question, but we'd need to alter
38 * code, since GLUT (hence freeglut) does not quite unify stroked and
39 * bitmapped font handling.)
40 * Old UNIX/X11 GLUT (BSD, UNIX, IRIX, LINUX, HPUX, ...) used a system
41 * font best approximated by an 18-pixel HELVETICA, I think. MS-WINDOWS
42 * GLUT used something closest to the 8x13 fixed-width font. (Old
43 * GLUT apparently uses host-system menus rather than building its own.
44 * freeglut is building its own menus from scratch.)
46 * FREEGLUT_MENU_HEIGHT gives the height of ONE menu box. This should be
47 * the distances between two adjacent menu entries. It should scale
48 * automatically with the font choice, so you needn't alter it---unless you
51 * FREEGLUT_MENU_BORDER says how many pixels to allow around the edge of a
52 * menu. (It also seems to be the same as the number of pixels used as
53 * a border around *items* to separate them from neighbors. John says
54 * that that wasn't the original intent...if not, perhaps we need another
55 * symbolic constant, FREEGLUT_MENU_ITEM_BORDER, or such.)
57 /* See platform-specific header files for menu font and color definitions */
59 #define FREEGLUT_MENU_HEIGHT (glutBitmapHeight(FREEGLUT_MENU_FONT) + \
61 #define FREEGLUT_MENU_BORDER 2
65 * These variables are for rendering the freeglut menu items.
67 * The choices are fore- and background, with and without h for Highlighting.
68 * Old GLUT appeared to be system-dependant for its colors (sigh) so we are
69 * too. These variables should be stuffed into global state and initialized
70 * via the glutInit*() system.
72 static float menu_pen_fore [4] = FREEGLUT_MENU_PEN_FORE_COLORS ;
73 static float menu_pen_back [4] = FREEGLUT_MENU_PEN_BACK_COLORS ;
74 static float menu_pen_hfore [4] = FREEGLUT_MENU_PEN_HFORE_COLORS;
75 static float menu_pen_hback [4] = FREEGLUT_MENU_PEN_HBACK_COLORS;
78 extern GLvoid fgPlatformGetGameModeVMaxExtent( SFG_Window* window, int* x, int* y );
79 extern void fghPlatformGetCursorPos(SFG_XYUse *mouse_pos);
81 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */
84 * Private function to find a menu entry by index
86 static SFG_MenuEntry *fghFindMenuEntry( SFG_Menu* menu, int index )
91 for( entry = (SFG_MenuEntry *)menu->Entries.First;
93 entry = (SFG_MenuEntry *)entry->Node.Next )
104 * Deactivates a menu pointed by the function argument.
106 static void fghDeactivateSubMenu( SFG_MenuEntry *menuEntry )
108 SFG_MenuEntry *subMenuIter;
109 /* Hide the present menu's window */
110 fgSetWindow( menuEntry->SubMenu->Window );
113 /* Forget about having that menu active anymore, now: */
114 menuEntry->SubMenu->Window->ActiveMenu = NULL;
115 menuEntry->SubMenu->IsActive = GL_FALSE;
116 menuEntry->SubMenu->ActiveEntry = NULL;
118 /* Hide all submenu windows, and the root menu's window. */
119 for ( subMenuIter = (SFG_MenuEntry *)menuEntry->SubMenu->Entries.First;
121 subMenuIter = (SFG_MenuEntry *)subMenuIter->Node.Next )
123 subMenuIter->IsActive = GL_FALSE;
125 /* Is that an active submenu by any case? */
126 if( subMenuIter->SubMenu )
127 fghDeactivateSubMenu( subMenuIter );
130 fgSetWindow ( menuEntry->SubMenu->ParentWindow ) ;
134 * Private function to get the virtual maximum screen extent
136 static GLvoid fghGetVMaxExtent( SFG_Window* window, int* x, int* y )
138 if( fgStructure.GameModeWindow )
139 fgPlatformGetGameModeVMaxExtent ( window, x, y );
142 *x = fgDisplay.ScreenWidth;
143 *y = fgDisplay.ScreenHeight;
148 * Private function to check for the current menu/sub menu activity state
150 static GLboolean fghCheckMenuStatus( SFG_Menu* menu )
152 SFG_MenuEntry* menuEntry;
155 /* First of all check any of the active sub menus... */
156 for( menuEntry = (SFG_MenuEntry *)menu->Entries.First;
158 menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next )
160 if( menuEntry->SubMenu && menuEntry->IsActive )
163 * OK, have the sub-menu checked, too. If it returns GL_TRUE, it
164 * will mean that it caught the mouse cursor and we do not need
165 * to regenerate the activity list, and so our parents do...
167 GLboolean return_status;
169 menuEntry->SubMenu->Window->State.MouseX =
170 menu->Window->State.MouseX + menu->X - menuEntry->SubMenu->X;
171 menuEntry->SubMenu->Window->State.MouseY =
172 menu->Window->State.MouseY + menu->Y - menuEntry->SubMenu->Y;
173 return_status = fghCheckMenuStatus( menuEntry->SubMenu );
180 /* That much about our sub menus, let's get to checking the current menu: */
181 x = menu->Window->State.MouseX;
182 y = menu->Window->State.MouseY;
184 /* Check if the mouse cursor is contained within the current menu box */
185 if( ( x >= FREEGLUT_MENU_BORDER ) &&
186 ( x < menu->Width - FREEGLUT_MENU_BORDER ) &&
187 ( y >= FREEGLUT_MENU_BORDER ) &&
188 ( y < menu->Height - FREEGLUT_MENU_BORDER ) )
190 int menuID = ( y - FREEGLUT_MENU_BORDER ) / FREEGLUT_MENU_HEIGHT;
192 /* The mouse cursor is somewhere over our box, check it out. */
193 menuEntry = fghFindMenuEntry( menu, menuID + 1 );
194 FREEGLUT_INTERNAL_ERROR_EXIT( menuEntry, "Cannot find menu entry",
195 "fghCheckMenuStatus" );
197 menuEntry->IsActive = GL_TRUE;
198 menuEntry->Ordinal = menuID;
201 * If this is not the same as the last active menu entry, deactivate
202 * the previous entry. Specifically, if the previous active entry
203 * was a submenu then deactivate it.
205 if( menu->ActiveEntry && ( menuEntry != menu->ActiveEntry ) )
206 if( menu->ActiveEntry->SubMenu )
207 fghDeactivateSubMenu( menu->ActiveEntry );
209 if( menuEntry != menu->ActiveEntry )
211 menu->Window->State.Redisplay = GL_TRUE;
212 if( menu->ActiveEntry )
213 menu->ActiveEntry->IsActive = GL_FALSE;
216 menu->ActiveEntry = menuEntry;
217 menu->IsActive = GL_TRUE; /* XXX Do we need this? */
220 * OKi, we have marked that entry as active, but it would be also
221 * nice to have its contents updated, in case it's a sub menu.
222 * Also, ignore the return value of the check function:
224 if( menuEntry->SubMenu )
226 if ( ! menuEntry->SubMenu->IsActive )
229 SFG_Window *current_window = fgStructure.CurrentWindow;
231 /* Set up the initial menu position now... */
232 menuEntry->SubMenu->IsActive = GL_TRUE;
234 /* Set up the initial submenu position now: */
235 fghGetVMaxExtent(menu->ParentWindow, &max_x, &max_y);
236 menuEntry->SubMenu->X = menu->X + menu->Width;
237 menuEntry->SubMenu->Y = menu->Y +
238 menuEntry->Ordinal * FREEGLUT_MENU_HEIGHT;
240 if( menuEntry->SubMenu->X + menuEntry->SubMenu->Width > max_x )
241 menuEntry->SubMenu->X = menu->X - menuEntry->SubMenu->Width;
243 if( menuEntry->SubMenu->Y + menuEntry->SubMenu->Height > max_y )
245 menuEntry->SubMenu->Y -= ( menuEntry->SubMenu->Height -
246 FREEGLUT_MENU_HEIGHT -
247 2 * FREEGLUT_MENU_BORDER );
248 if( menuEntry->SubMenu->Y < 0 )
249 menuEntry->SubMenu->Y = 0;
252 fgSetWindow( menuEntry->SubMenu->Window );
253 glutPositionWindow( menuEntry->SubMenu->X,
254 menuEntry->SubMenu->Y );
255 glutReshapeWindow( menuEntry->SubMenu->Width,
256 menuEntry->SubMenu->Height );
259 menuEntry->SubMenu->Window->ActiveMenu = menuEntry->SubMenu;
260 fgSetWindow( current_window );
261 menuEntry->SubMenu->Window->State.MouseX =
262 x + menu->X - menuEntry->SubMenu->X;
263 menuEntry->SubMenu->Window->State.MouseY =
264 y + menu->Y - menuEntry->SubMenu->Y;
265 fghCheckMenuStatus( menuEntry->SubMenu );
268 /* Activate it because its parent entry is active */
269 menuEntry->SubMenu->IsActive = GL_TRUE; /* XXX Do we need this? */
272 /* Report back that we have caught the menu cursor */
276 /* Looks like the menu cursor is somewhere else... */
277 if( menu->ActiveEntry && menu->ActiveEntry->IsActive &&
278 ( !menu->ActiveEntry->SubMenu ||
279 !menu->ActiveEntry->SubMenu->IsActive ) )
281 menu->Window->State.Redisplay = GL_TRUE;
282 menu->ActiveEntry->IsActive = GL_FALSE;
283 menu->ActiveEntry = NULL;
290 * Displays a menu box and all of its submenus (if they are active)
292 static void fghDisplayMenuBox( SFG_Menu* menu )
294 SFG_MenuEntry *menuEntry;
296 int border = FREEGLUT_MENU_BORDER;
299 * Have the menu box drawn first. The +- values are
300 * here just to make it more nice-looking...
302 /* a non-black dark version of the below. */
303 glColor4f( 1.0f, 1.0f, 1.0f, 1.0f );
304 glBegin( GL_QUAD_STRIP );
305 glVertex2i( menu->Width , 0 );
306 glVertex2i( menu->Width - border, border);
308 glVertex2i( border, border);
309 glVertex2i( 0 , menu->Height );
310 glVertex2i( border, menu->Height - border);
313 /* a non-black dark version of the below. */
314 glColor4f( 0.5f, 0.5f, 0.5f, 1.0f );
315 glBegin( GL_QUAD_STRIP );
316 glVertex2i( 0 , menu->Height );
317 glVertex2i( border, menu->Height - border);
318 glVertex2i( menu->Width , menu->Height );
319 glVertex2i( menu->Width - border, menu->Height - border);
320 glVertex2i( menu->Width , 0 );
321 glVertex2i( menu->Width - border, border);
324 glColor4fv( menu_pen_back );
326 glVertex2i( border, border);
327 glVertex2i( menu->Width - border, border);
328 glVertex2i( menu->Width - border, menu->Height - border);
329 glVertex2i( border, menu->Height - border);
332 /* Check if any of the submenus is currently active... */
333 for( menuEntry = (SFG_MenuEntry *)menu->Entries.First;
335 menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next )
337 /* Has the menu been marked as active, maybe? */
338 if( menuEntry->IsActive )
341 * That's truly right, and we need to have it highlighted.
342 * There is an assumption that mouse cursor didn't move
343 * since the last check of menu activity state:
345 int menuID = menuEntry->Ordinal;
347 /* So have the highlight drawn... */
348 glColor4fv( menu_pen_hback );
351 (menuID + 0)*FREEGLUT_MENU_HEIGHT + border );
352 glVertex2i( menu->Width - border,
353 (menuID + 0)*FREEGLUT_MENU_HEIGHT + border );
354 glVertex2i( menu->Width - border,
355 (menuID + 1)*FREEGLUT_MENU_HEIGHT + border );
357 (menuID + 1)*FREEGLUT_MENU_HEIGHT + border );
362 /* Print the menu entries now... */
364 glColor4fv( menu_pen_fore );
366 for( menuEntry = (SFG_MenuEntry *)menu->Entries.First, i = 0;
368 menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next, ++i )
370 /* If the menu entry is active, set the color to white */
371 if( menuEntry->IsActive )
372 glColor4fv( menu_pen_hfore );
374 /* Move the raster into position... */
375 /* Try to center the text - JCJ 31 July 2003*/
378 ( i + 1 )*FREEGLUT_MENU_HEIGHT -
379 ( int )( FREEGLUT_MENU_HEIGHT*0.3 - border )
382 /* Have the label drawn, character after character: */
383 glutBitmapString( FREEGLUT_MENU_FONT,
384 (unsigned char *)menuEntry->Text);
386 /* If it's a submenu, draw a right arrow */
387 if( menuEntry->SubMenu )
389 int width = glutBitmapWidth( FREEGLUT_MENU_FONT, '_' );
390 int x_base = menu->Width - 2 - width;
391 int y_base = i*FREEGLUT_MENU_HEIGHT + border;
392 glBegin( GL_TRIANGLES );
393 glVertex2i( x_base, y_base + 2*border);
394 glVertex2i( menu->Width - 2, y_base +
395 ( FREEGLUT_MENU_HEIGHT + border) / 2 );
396 glVertex2i( x_base, y_base + FREEGLUT_MENU_HEIGHT - border );
400 /* If the menu entry is active, reset the color */
401 if( menuEntry->IsActive )
402 glColor4fv( menu_pen_fore );
407 * Private static function to set the parent window of a submenu and all
410 static void fghSetMenuParentWindow( SFG_Window *window, SFG_Menu *menu )
412 SFG_MenuEntry *menuEntry;
414 menu->ParentWindow = window;
416 for( menuEntry = ( SFG_MenuEntry * )menu->Entries.First;
418 menuEntry = ( SFG_MenuEntry * )menuEntry->Node.Next )
419 if( menuEntry->SubMenu )
420 fghSetMenuParentWindow( window, menuEntry->SubMenu );
424 * Function to check for menu entry selection on menu deactivation
426 static void fghExecuteMenuCallback( SFG_Menu* menu )
428 SFG_MenuEntry *menuEntry;
430 /* First of all check any of the active sub menus... */
431 for( menuEntry = (SFG_MenuEntry *)menu->Entries.First;
433 menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next)
435 if( menuEntry->IsActive )
437 if( menuEntry->SubMenu )
438 fghExecuteMenuCallback( menuEntry->SubMenu );
442 SFG_Menu *save_menu = fgStructure.CurrentMenu;
443 fgStructure.CurrentMenu = menu;
444 menu->Callback( menuEntry->ID );
445 fgStructure.CurrentMenu = save_menu;
455 * Displays the currently active menu for the current window
457 void fgDisplayMenu( void )
459 SFG_Window* window = fgStructure.CurrentWindow;
460 SFG_Menu* menu = NULL;
462 FREEGLUT_INTERNAL_ERROR_EXIT ( fgStructure.CurrentWindow, "Displaying menu in nonexistent window",
465 /* Check if there is an active menu attached to this window... */
466 menu = window->ActiveMenu;
467 freeglut_return_if_fail( menu );
469 fgSetWindow( menu->Window );
471 glPushAttrib( GL_DEPTH_BUFFER_BIT | GL_TEXTURE_BIT | GL_LIGHTING_BIT |
474 glDisable( GL_DEPTH_TEST );
475 glDisable( GL_TEXTURE_2D );
476 glDisable( GL_LIGHTING );
477 glDisable( GL_CULL_FACE );
479 glMatrixMode( GL_PROJECTION );
483 0, glutGet( GLUT_WINDOW_WIDTH ),
484 glutGet( GLUT_WINDOW_HEIGHT ), 0,
488 glMatrixMode( GL_MODELVIEW );
492 fghDisplayMenuBox( menu );
496 glMatrixMode( GL_PROJECTION );
498 glMatrixMode( GL_MODELVIEW );
503 fgSetWindow ( window );
507 * Activates a menu pointed by the function argument
509 static void fghActivateMenu( SFG_Window* window, int button )
513 /* We'll be referencing this menu a lot, so remember its address: */
514 SFG_Menu* menu = window->Menu[ button ];
515 SFG_Window* current_window = fgStructure.CurrentWindow;
517 /* If the menu is already active in another window, deactivate it (and any submenus) there */
518 if ( menu->ParentWindow )
519 fgDeactivateMenu(menu->ParentWindow);
521 /* Mark the menu as active, so that it gets displayed: */
522 window->ActiveMenu = menu;
523 menu->IsActive = GL_TRUE;
524 fghSetMenuParentWindow ( window, menu );
525 fgState.ActiveMenus++;
527 /* Set up the initial menu position now: */
528 fghGetVMaxExtent(menu->ParentWindow, &max_x, &max_y);
529 fgSetWindow( window );
530 menu->X = window->State.MouseX + glutGet( GLUT_WINDOW_X );
531 menu->Y = window->State.MouseY + glutGet( GLUT_WINDOW_Y );
533 if( menu->X + menu->Width > max_x )
534 menu->X -=menu->Width;
536 if( menu->Y + menu->Height > max_y )
538 menu->Y -=menu->Height;
543 menu->Window->State.MouseX =
544 window->State.MouseX + glutGet( GLUT_WINDOW_X ) - menu->X;
545 menu->Window->State.MouseY =
546 window->State.MouseY + glutGet( GLUT_WINDOW_Y ) - menu->Y;
548 /* Menu status callback */
549 if (fgState.MenuStateCallback || fgState.MenuStatusCallback)
551 fgStructure.CurrentMenu = menu;
552 fgStructure.CurrentWindow = window;
553 if (fgState.MenuStateCallback)
554 fgState.MenuStateCallback(GLUT_MENU_IN_USE);
555 if (fgState.MenuStatusCallback)
556 fgState.MenuStatusCallback(GLUT_MENU_IN_USE, window->State.MouseX, window->State.MouseY);
559 fgSetWindow( menu->Window );
560 glutPositionWindow( menu->X, menu->Y );
561 glutReshapeWindow( menu->Width, menu->Height );
564 menu->Window->ActiveMenu = menu;
565 fghCheckMenuStatus( menu );
566 fgSetWindow( current_window );
570 * Update Highlight states of the menu
572 * Current mouse position is in menu->Window->State.MouseX/Y.
574 void fgUpdateMenuHighlight ( SFG_Menu *menu )
576 fghCheckMenuStatus( menu );
580 * Check whether an active menu absorbs a mouse click
582 GLboolean fgCheckActiveMenu ( SFG_Window *window, int button, GLboolean pressed,
583 int mouse_x, int mouse_y )
586 * Near as I can tell, this is the menu behaviour:
587 * - Down-click the menu button, menu not active: activate
588 * the menu with its upper left-hand corner at the mouse
590 * - Down-click any button outside the menu, menu active:
591 * deactivate the menu
592 * - Down-click any button inside the menu, menu active:
593 * select the menu entry and deactivate the menu
594 * - Up-click the menu button, menu not active: nothing happens
595 * - Up-click the menu button outside the menu, menu active:
597 * - Up-click the menu button inside the menu, menu active:
598 * select the menu entry and deactivate the menu
599 * Since menus can have submenus, we need to check this recursively.
601 if( window->ActiveMenu )
603 if( window == window->ActiveMenu->ParentWindow )
605 window->ActiveMenu->Window->State.MouseX =
606 mouse_x - window->ActiveMenu->X;
607 window->ActiveMenu->Window->State.MouseY =
608 mouse_y - window->ActiveMenu->Y;
611 /* In the menu, invoke the callback and deactivate the menu */
612 if( fghCheckMenuStatus( window->ActiveMenu ) )
615 * Save the current window and menu and set the current
616 * window to the window whose menu this is
618 SFG_Window *save_window = fgStructure.CurrentWindow;
619 SFG_Menu *save_menu = fgStructure.CurrentMenu;
620 SFG_Window *parent_window = window->ActiveMenu->ParentWindow;
621 fgSetWindow( parent_window );
622 fgStructure.CurrentMenu = window->ActiveMenu;
624 /* Execute the menu callback */
625 fghExecuteMenuCallback( window->ActiveMenu );
626 fgDeactivateMenu( parent_window );
628 /* Restore the current window and menu */
629 fgSetWindow( save_window );
630 fgStructure.CurrentMenu = save_menu;
634 * Outside the menu, deactivate if it's a downclick
636 * A downclick outside of the interior of our freeglut windows
637 * is dealt with in the WM_KILLFOCUS handler of fgPlatformWindowProc
640 fgDeactivateMenu( window->ActiveMenu->ParentWindow );
644 * XXX Why does an active menu require a redisplay at
645 * XXX this point? If this can come out cleanly, then
646 * XXX it probably should do so; if not, a comment should
649 if( ! window->IsMenu )
650 window->State.Redisplay = GL_TRUE;
655 /* No active menu, let's check whether we need to activate one. */
656 if( ( 0 <= button ) &&
657 ( FREEGLUT_MAX_MENUS > button ) &&
658 ( window->Menu[ button ] ) &&
661 /* XXX Posting a requisite Redisplay seems bogus. */
662 window->State.Redisplay = GL_TRUE;
663 fghActivateMenu( window, button );
671 * Deactivates a menu pointed by the function argument.
673 void fgDeactivateMenu( SFG_Window *window )
675 SFG_Window *parent_window = NULL;
677 SFG_MenuEntry *menuEntry;
679 /* Did we find an active window? */
680 freeglut_return_if_fail( window );
681 /* Check if there is an active menu attached to this window... */
682 menu = window->ActiveMenu;
683 freeglut_return_if_fail( menu );
685 parent_window = menu->ParentWindow;
687 /* Hide the present menu's window */
688 fgSetWindow( menu->Window );
691 /* Forget about having that menu active anymore, now: */
692 menu->Window->ActiveMenu = NULL;
693 menu->ParentWindow->ActiveMenu = NULL;
694 fghSetMenuParentWindow ( NULL, menu );
695 menu->IsActive = GL_FALSE;
696 menu->ActiveEntry = NULL;
698 fgState.ActiveMenus--;
700 /* Hide all submenu windows, and the root menu's window. */
701 for ( menuEntry = ( SFG_MenuEntry * )menu->Entries.First;
703 menuEntry = ( SFG_MenuEntry * )menuEntry->Node.Next )
705 menuEntry->IsActive = GL_FALSE;
707 /* Is that an active submenu by any chance? */
708 if( menuEntry->SubMenu )
709 fghDeactivateSubMenu( menuEntry );
712 fgSetWindow ( parent_window ) ;
714 /* Menu status callback */
715 if (fgState.MenuStateCallback || fgState.MenuStatusCallback)
717 fgStructure.CurrentMenu = menu;
718 fgStructure.CurrentWindow = parent_window;
719 if (fgState.MenuStateCallback)
720 fgState.MenuStateCallback(GLUT_MENU_NOT_IN_USE);
721 if (fgState.MenuStatusCallback)
723 /* Get cursor position on screen and convert to relative to parent_window's client area */
725 fghPlatformGetCursorPos(&mouse_pos);
727 mouse_pos.X -= glutGet( GLUT_WINDOW_X );
728 mouse_pos.Y -= glutGet( GLUT_WINDOW_Y );
730 fgState.MenuStatusCallback(GLUT_MENU_NOT_IN_USE, mouse_pos.X, mouse_pos.Y);
736 * Recalculates current menu's box size
738 void fghCalculateMenuBoxSize( void )
740 SFG_MenuEntry* menuEntry;
741 int width = 0, height = 0;
743 /* Make sure there is a current menu set */
744 freeglut_return_if_fail( fgStructure.CurrentMenu );
746 /* The menu's box size depends on the menu entries: */
747 for( menuEntry = ( SFG_MenuEntry * )fgStructure.CurrentMenu->Entries.First;
749 menuEntry = ( SFG_MenuEntry * )menuEntry->Node.Next )
751 /* Update the menu entry's width value */
752 menuEntry->Width = glutBitmapLength(
754 (unsigned char *)menuEntry->Text
758 * If the entry is a submenu, then it needs to be wider to
759 * accomodate the arrow. JCJ 31 July 2003
761 if (menuEntry->SubMenu )
762 menuEntry->Width += glutBitmapLength(
767 /* Check if it's the biggest we've found */
768 if( menuEntry->Width > width )
769 width = menuEntry->Width;
771 height += FREEGLUT_MENU_HEIGHT;
774 /* Store the menu's box size now: */
775 fgStructure.CurrentMenu->Height = height + 2 * FREEGLUT_MENU_BORDER;
776 fgStructure.CurrentMenu->Width = width + 4 * FREEGLUT_MENU_BORDER;
780 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
783 * Creates a new menu object, adding it to the freeglut structure
785 int FGAPIENTRY glutCreateMenu( void(* callback)( int ) )
787 /* The menu object creation code resides in freeglut_structure.c */
788 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutCreateMenu" );
789 if (fgGetActiveMenu())
790 fgError("Menu manipulation not allowed while menus in use.");
792 return fgCreateMenu( callback )->ID;
796 * Destroys a menu object, removing all references to it
798 void FGAPIENTRY glutDestroyMenu( int menuID )
802 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutDestroyMenu" );
803 menu = fgMenuByID( menuID );
805 freeglut_return_if_fail( menu );
806 if (fgGetActiveMenu())
807 fgError("Menu manipulation not allowed while menus in use.");
809 /* The menu object destruction code resides in freeglut_structure.c */
810 fgDestroyMenu( menu );
814 * Returns the ID number of the currently active menu
816 int FGAPIENTRY glutGetMenu( void )
818 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGetMenu" );
820 if( fgStructure.CurrentMenu )
821 return fgStructure.CurrentMenu->ID;
827 * Sets the current menu given its menu ID
829 void FGAPIENTRY glutSetMenu( int menuID )
833 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetMenu" );
834 menu = fgMenuByID( menuID );
836 freeglut_return_if_fail( menu );
838 fgStructure.CurrentMenu = menu;
842 * Adds a menu entry to the bottom of the current menu
844 void FGAPIENTRY glutAddMenuEntry( const char* label, int value )
846 SFG_MenuEntry* menuEntry;
847 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutAddMenuEntry" );
848 menuEntry = (SFG_MenuEntry *)calloc( sizeof(SFG_MenuEntry), 1 );
850 freeglut_return_if_fail( fgStructure.CurrentMenu );
851 if (fgGetActiveMenu())
852 fgError("Menu manipulation not allowed while menus in use.");
854 menuEntry->Text = strdup( label );
855 menuEntry->ID = value;
857 /* Have the new menu entry attached to the current menu */
858 fgListAppend( &fgStructure.CurrentMenu->Entries, &menuEntry->Node );
860 fghCalculateMenuBoxSize( );
864 * Add a sub menu to the bottom of the current menu
866 void FGAPIENTRY glutAddSubMenu( const char *label, int subMenuID )
868 SFG_MenuEntry *menuEntry;
871 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutAddSubMenu" );
872 menuEntry = ( SFG_MenuEntry * )calloc( sizeof( SFG_MenuEntry ), 1 );
873 subMenu = fgMenuByID( subMenuID );
875 freeglut_return_if_fail( fgStructure.CurrentMenu );
876 if (fgGetActiveMenu())
877 fgError("Menu manipulation not allowed while menus in use.");
879 freeglut_return_if_fail( subMenu );
881 menuEntry->Text = strdup( label );
882 menuEntry->SubMenu = subMenu;
885 fgListAppend( &fgStructure.CurrentMenu->Entries, &menuEntry->Node );
886 fghCalculateMenuBoxSize( );
890 * Changes the specified menu item in the current menu into a menu entry
892 void FGAPIENTRY glutChangeToMenuEntry( int item, const char* label, int value )
894 SFG_MenuEntry* menuEntry = NULL;
896 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutChangeToMenuEntry" );
898 freeglut_return_if_fail( fgStructure.CurrentMenu );
899 if (fgGetActiveMenu())
900 fgError("Menu manipulation not allowed while menus in use.");
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 normal menu entry, so: */
908 if( menuEntry->Text )
909 free( menuEntry->Text );
911 menuEntry->Text = strdup( label );
912 menuEntry->ID = value;
913 menuEntry->SubMenu = NULL;
914 fghCalculateMenuBoxSize( );
918 * Changes the specified menu item in the current menu into a sub-menu trigger.
920 void FGAPIENTRY glutChangeToSubMenu( int item, const char* label,
924 SFG_MenuEntry* menuEntry;
926 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutChangeToSubMenu" );
928 freeglut_return_if_fail( fgStructure.CurrentMenu );
929 if (fgGetActiveMenu())
930 fgError("Menu manipulation not allowed while menus in use.");
932 /* Get handle to sub menu */
933 subMenu = fgMenuByID( subMenuID );
935 freeglut_return_if_fail( subMenu );
937 /* Get n-th menu entry in the current menu, starting from one: */
938 menuEntry = fghFindMenuEntry( fgStructure.CurrentMenu, item );
940 freeglut_return_if_fail( menuEntry );
942 /* We want it to become a sub menu entry, so: */
943 if( menuEntry->Text )
944 free( menuEntry->Text );
946 menuEntry->Text = strdup( label );
947 menuEntry->SubMenu = subMenu;
949 fghCalculateMenuBoxSize( );
953 * Removes the specified menu item from the current menu
955 void FGAPIENTRY glutRemoveMenuItem( int item )
957 SFG_MenuEntry* menuEntry;
959 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutRemoveMenuItem" );
961 freeglut_return_if_fail( fgStructure.CurrentMenu );
962 if (fgGetActiveMenu())
963 fgError("Menu manipulation not allowed while menus in use.");
965 /* Get n-th menu entry in the current menu, starting from one: */
966 menuEntry = fghFindMenuEntry( fgStructure.CurrentMenu, item );
968 freeglut_return_if_fail( menuEntry );
970 fgListRemove( &fgStructure.CurrentMenu->Entries, &menuEntry->Node );
971 if ( menuEntry->Text )
972 free( menuEntry->Text );
975 fghCalculateMenuBoxSize( );
979 * Attaches a menu to the current window
981 void FGAPIENTRY glutAttachMenu( int button )
983 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutAttachMenu" );
985 freeglut_return_if_fail( fgStructure.CurrentWindow );
987 freeglut_return_if_fail( fgStructure.CurrentMenu );
988 if (fgGetActiveMenu())
989 fgError("Menu manipulation not allowed while menus in use.");
991 freeglut_return_if_fail( button >= 0 );
992 freeglut_return_if_fail( button < FREEGLUT_MAX_MENUS );
994 fgStructure.CurrentWindow->Menu[ button ] = fgStructure.CurrentMenu;
998 * Detaches a menu from the current window
1000 void FGAPIENTRY glutDetachMenu( int button )
1002 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutDetachMenu" );
1004 freeglut_return_if_fail( fgStructure.CurrentWindow );
1006 freeglut_return_if_fail( fgStructure.CurrentMenu );
1007 if (fgGetActiveMenu())
1008 fgError("Menu manipulation not allowed while menus in use.");
1010 freeglut_return_if_fail( button >= 0 );
1011 freeglut_return_if_fail( button < FREEGLUT_MAX_MENUS );
1013 fgStructure.CurrentWindow->Menu[ button ] = NULL;
1017 * A.Donev: Set and retrieve the menu's user data
1019 void* FGAPIENTRY glutGetMenuData( void )
1021 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGetMenuData" );
1022 return fgStructure.CurrentMenu->UserData;
1025 void FGAPIENTRY glutSetMenuData(void* data)
1027 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetMenuData" );
1028 fgStructure.CurrentMenu->UserData=data;
1031 /*** END OF FILE ***/