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(const SFG_Window *window, GLboolean client, 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 )
514 /* We'll be referencing this menu a lot, so remember its address: */
515 SFG_Menu* menu = window->Menu[ button ];
516 SFG_Window* current_window = fgStructure.CurrentWindow;
518 /* If the menu is already active in another window, deactivate it (and any submenus) there */
519 if ( menu->ParentWindow )
520 fgDeactivateMenu(menu->ParentWindow);
522 /* Mark the menu as active, so that it gets displayed: */
523 window->ActiveMenu = menu;
524 menu->IsActive = GL_TRUE;
525 fghSetMenuParentWindow ( window, menu );
526 fgState.ActiveMenus++;
528 /* Set up the initial menu position now: */
529 fghGetVMaxExtent(menu->ParentWindow, &max_x, &max_y);
530 fgSetWindow( window );
531 /* get mouse position on screen (window->State.MouseX and window->State.MouseY
532 * are relative to client area origin), and not easy to correct given that
533 * glutGet( GLUT_WINDOW_X ) and glutGet( GLUT_WINDOW_Y ) return relative to parent
534 * origin when looking at a child window
535 * for parent windows: window->State.MouseX + glutGet( GLUT_WINDOW_X ) == mouse_pos.X
537 fghPlatformGetCursorPos(NULL, GL_FALSE, &mouse_pos);
538 menu->X = mouse_pos.X;
539 menu->Y = mouse_pos.Y;
541 /* Make sure the whole menu is on the screen */
542 if( menu->X + menu->Width > max_x )
543 menu->X -=menu->Width;
545 if( menu->Y + menu->Height > max_y )
547 menu->Y -=menu->Height;
552 /* Set position of mouse relative to top-left menu in menu's window state (could as well set 0 at creation time...) */
553 menu->Window->State.MouseX = mouse_pos.X - menu->X;
554 menu->Window->State.MouseY = mouse_pos.Y - menu->Y;
556 /* Menu status callback */
557 if (fgState.MenuStateCallback || fgState.MenuStatusCallback)
559 fgStructure.CurrentMenu = menu;
560 fgStructure.CurrentWindow = window;
561 if (fgState.MenuStateCallback)
562 fgState.MenuStateCallback(GLUT_MENU_IN_USE);
563 if (fgState.MenuStatusCallback)
564 /* window->State.MouseX and window->State.MouseY are relative to client area origin, as needed */
565 fgState.MenuStatusCallback(GLUT_MENU_IN_USE, window->State.MouseX, window->State.MouseY);
568 fgSetWindow( menu->Window );
569 glutPositionWindow( menu->X, menu->Y );
570 glutReshapeWindow( menu->Width, menu->Height );
573 menu->Window->ActiveMenu = menu;
574 fghCheckMenuStatus( menu );
575 fgSetWindow( current_window );
579 * Update Highlight states of the menu
581 * Current mouse position is in menu->Window->State.MouseX/Y.
583 void fgUpdateMenuHighlight ( SFG_Menu *menu )
585 fghCheckMenuStatus( menu );
589 * Check whether an active menu absorbs a mouse click
591 GLboolean fgCheckActiveMenu ( SFG_Window *window, int button, GLboolean pressed,
592 int mouse_x, int mouse_y )
595 * Near as I can tell, this is the menu behaviour:
596 * - Down-click the menu button, menu not active: activate
597 * the menu with its upper left-hand corner at the mouse
599 * - Down-click any button outside the menu, menu active:
600 * deactivate the menu
601 * - Down-click any button inside the menu, menu active:
602 * select the menu entry and deactivate the menu
603 * - Up-click the menu button, menu not active: nothing happens
604 * - Up-click the menu button outside the menu, menu active:
606 * - Up-click the menu button inside the menu, menu active:
607 * select the menu entry and deactivate the menu
608 * Since menus can have submenus, we need to check this recursively.
610 if( window->ActiveMenu )
612 if( window == window->ActiveMenu->ParentWindow )
614 window->ActiveMenu->Window->State.MouseX =
615 mouse_x - window->ActiveMenu->X;
616 window->ActiveMenu->Window->State.MouseY =
617 mouse_y - window->ActiveMenu->Y;
620 /* In the menu, invoke the callback and deactivate the menu */
621 if( fghCheckMenuStatus( window->ActiveMenu ) )
624 * Save the current window and menu and set the current
625 * window to the window whose menu this is
627 SFG_Window *save_window = fgStructure.CurrentWindow;
628 SFG_Menu *save_menu = fgStructure.CurrentMenu;
629 SFG_Window *parent_window = window->ActiveMenu->ParentWindow;
630 fgSetWindow( parent_window );
631 fgStructure.CurrentMenu = window->ActiveMenu;
633 /* Execute the menu callback */
634 fghExecuteMenuCallback( window->ActiveMenu );
635 fgDeactivateMenu( parent_window );
637 /* Restore the current window and menu */
638 fgSetWindow( save_window );
639 fgStructure.CurrentMenu = save_menu;
643 * Outside the menu, deactivate if it's a downclick
645 * A downclick outside of the interior of our freeglut windows
646 * is dealt with in the WM_KILLFOCUS handler of fgPlatformWindowProc
649 fgDeactivateMenu( window->ActiveMenu->ParentWindow );
653 * XXX Why does an active menu require a redisplay at
654 * XXX this point? If this can come out cleanly, then
655 * XXX it probably should do so; if not, a comment should
658 if( ! window->IsMenu )
659 window->State.Redisplay = GL_TRUE;
664 /* No active menu, let's check whether we need to activate one. */
665 if( ( 0 <= button ) &&
666 ( FREEGLUT_MAX_MENUS > button ) &&
667 ( window->Menu[ button ] ) &&
670 /* If mouseclick was outside the parent window, ignore. This can
671 * happen when another mouse button is already depressed and the
672 * window thus has mouse capture
674 if (window->State.MouseX>0 && window->State.MouseY>0 &&
675 window->State.MouseX<window->State.Width && window->State.MouseX<window->State.Height)
677 /* XXX Posting a requisite Redisplay seems bogus. */
678 window->State.Redisplay = GL_TRUE;
679 fghActivateMenu( window, button );
688 * Deactivates a menu pointed by the function argument.
690 static SFG_Menu* menuDeactivating = NULL;
691 void fgDeactivateMenu( SFG_Window *window )
693 SFG_Window *parent_window = NULL;
695 SFG_MenuEntry *menuEntry;
697 /* Did we find an active window? */
698 freeglut_return_if_fail( window );
699 /* Check if there is an active menu attached to this window... */
700 menu = window->ActiveMenu;
701 freeglut_return_if_fail( menu );
702 /* Check if we are already deactivating this menu, abort in that case (glutHideWindow below can cause this function to be called again on the same menu..) */
703 if (menu==menuDeactivating)
705 menuDeactivating = menu;
707 parent_window = menu->ParentWindow;
709 /* Hide the present menu's window */
710 fgSetWindow( menu->Window );
713 /* Forget about having that menu active anymore, now: */
714 menu->Window->ActiveMenu = NULL;
715 menu->ParentWindow->ActiveMenu = NULL;
716 fghSetMenuParentWindow ( NULL, menu );
717 menu->IsActive = GL_FALSE;
718 menu->ActiveEntry = NULL;
720 fgState.ActiveMenus--;
722 /* Hide all submenu windows, and the root menu's window. */
723 for ( menuEntry = ( SFG_MenuEntry * )menu->Entries.First;
725 menuEntry = ( SFG_MenuEntry * )menuEntry->Node.Next )
727 menuEntry->IsActive = GL_FALSE;
729 /* Is that an active submenu by any chance? */
730 if( menuEntry->SubMenu )
731 fghDeactivateSubMenu( menuEntry );
733 /* Done deactivating menu */
734 menuDeactivating = NULL;
736 fgSetWindow ( parent_window ) ;
738 /* Menu status callback */
739 if (fgState.MenuStateCallback || fgState.MenuStatusCallback)
741 fgStructure.CurrentMenu = menu;
742 fgStructure.CurrentWindow = parent_window;
743 if (fgState.MenuStateCallback)
744 fgState.MenuStateCallback(GLUT_MENU_NOT_IN_USE);
745 if (fgState.MenuStatusCallback)
747 /* Get cursor position relative to parent_window's client area */
749 fghPlatformGetCursorPos(parent_window, GL_TRUE, &mouse_pos);
751 fgState.MenuStatusCallback(GLUT_MENU_NOT_IN_USE, mouse_pos.X, mouse_pos.Y);
757 * Recalculates current menu's box size
759 void fghCalculateMenuBoxSize( void )
761 SFG_MenuEntry* menuEntry;
762 int width = 0, height = 0;
764 /* Make sure there is a current menu set */
765 freeglut_return_if_fail( fgStructure.CurrentMenu );
767 /* The menu's box size depends on the menu entries: */
768 for( menuEntry = ( SFG_MenuEntry * )fgStructure.CurrentMenu->Entries.First;
770 menuEntry = ( SFG_MenuEntry * )menuEntry->Node.Next )
772 /* Update the menu entry's width value */
773 menuEntry->Width = glutBitmapLength(
775 (unsigned char *)menuEntry->Text
779 * If the entry is a submenu, then it needs to be wider to
780 * accomodate the arrow. JCJ 31 July 2003
782 if (menuEntry->SubMenu )
783 menuEntry->Width += glutBitmapLength(
788 /* Check if it's the biggest we've found */
789 if( menuEntry->Width > width )
790 width = menuEntry->Width;
792 height += FREEGLUT_MENU_HEIGHT;
795 /* Store the menu's box size now: */
796 fgStructure.CurrentMenu->Height = height + 2 * FREEGLUT_MENU_BORDER;
797 fgStructure.CurrentMenu->Width = width + 4 * FREEGLUT_MENU_BORDER;
801 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
804 * Creates a new menu object, adding it to the freeglut structure
806 int FGAPIENTRY glutCreateMenu( FGCBMenu callback )
808 /* The menu object creation code resides in freeglut_structure.c */
809 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutCreateMenu" );
810 if (fgGetActiveMenu())
811 fgError("Menu manipulation not allowed while menus in use.");
813 return fgCreateMenu( callback )->ID;
817 * Destroys a menu object, removing all references to it
819 void FGAPIENTRY glutDestroyMenu( int menuID )
823 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutDestroyMenu" );
824 menu = fgMenuByID( menuID );
826 freeglut_return_if_fail( menu );
827 if (fgGetActiveMenu())
828 fgError("Menu manipulation not allowed while menus in use.");
830 /* The menu object destruction code resides in freeglut_structure.c */
831 fgDestroyMenu( menu );
835 * Returns the ID number of the currently active menu
837 int FGAPIENTRY glutGetMenu( void )
839 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGetMenu" );
841 if( fgStructure.CurrentMenu )
842 return fgStructure.CurrentMenu->ID;
848 * Sets the current menu given its menu ID
850 void FGAPIENTRY glutSetMenu( int menuID )
854 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetMenu" );
855 menu = fgMenuByID( menuID );
857 freeglut_return_if_fail( menu );
859 fgStructure.CurrentMenu = menu;
863 * Adds a menu entry to the bottom of the current menu
865 void FGAPIENTRY glutAddMenuEntry( const char* label, int value )
867 SFG_MenuEntry* menuEntry;
868 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutAddMenuEntry" );
869 menuEntry = (SFG_MenuEntry *)calloc( sizeof(SFG_MenuEntry), 1 );
871 freeglut_return_if_fail( fgStructure.CurrentMenu );
872 if (fgGetActiveMenu())
873 fgError("Menu manipulation not allowed while menus in use.");
875 menuEntry->Text = strdup( label );
876 menuEntry->ID = value;
878 /* Have the new menu entry attached to the current menu */
879 fgListAppend( &fgStructure.CurrentMenu->Entries, &menuEntry->Node );
881 fghCalculateMenuBoxSize( );
885 * Add a sub menu to the bottom of the current menu
887 void FGAPIENTRY glutAddSubMenu( const char *label, int subMenuID )
889 SFG_MenuEntry *menuEntry;
892 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutAddSubMenu" );
893 menuEntry = ( SFG_MenuEntry * )calloc( sizeof( SFG_MenuEntry ), 1 );
894 subMenu = fgMenuByID( subMenuID );
896 freeglut_return_if_fail( fgStructure.CurrentMenu );
897 if (fgGetActiveMenu())
898 fgError("Menu manipulation not allowed while menus in use.");
900 freeglut_return_if_fail( subMenu );
902 menuEntry->Text = strdup( label );
903 menuEntry->SubMenu = subMenu;
906 fgListAppend( &fgStructure.CurrentMenu->Entries, &menuEntry->Node );
907 fghCalculateMenuBoxSize( );
911 * Changes the specified menu item in the current menu into a menu entry
913 void FGAPIENTRY glutChangeToMenuEntry( int item, const char* label, int value )
915 SFG_MenuEntry* menuEntry = NULL;
917 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutChangeToMenuEntry" );
919 freeglut_return_if_fail( fgStructure.CurrentMenu );
920 if (fgGetActiveMenu())
921 fgError("Menu manipulation not allowed while menus in use.");
923 /* Get n-th menu entry in the current menu, starting from one: */
924 menuEntry = fghFindMenuEntry( fgStructure.CurrentMenu, item );
926 freeglut_return_if_fail( menuEntry );
928 /* We want it to become a normal menu entry, so: */
929 if( menuEntry->Text )
930 free( menuEntry->Text );
932 menuEntry->Text = strdup( label );
933 menuEntry->ID = value;
934 menuEntry->SubMenu = NULL;
935 fghCalculateMenuBoxSize( );
939 * Changes the specified menu item in the current menu into a sub-menu trigger.
941 void FGAPIENTRY glutChangeToSubMenu( int item, const char* label,
945 SFG_MenuEntry* menuEntry;
947 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutChangeToSubMenu" );
949 freeglut_return_if_fail( fgStructure.CurrentMenu );
950 if (fgGetActiveMenu())
951 fgError("Menu manipulation not allowed while menus in use.");
953 /* Get handle to sub menu */
954 subMenu = fgMenuByID( subMenuID );
956 freeglut_return_if_fail( subMenu );
958 /* Get n-th menu entry in the current menu, starting from one: */
959 menuEntry = fghFindMenuEntry( fgStructure.CurrentMenu, item );
961 freeglut_return_if_fail( menuEntry );
963 /* We want it to become a sub menu entry, so: */
964 if( menuEntry->Text )
965 free( menuEntry->Text );
967 menuEntry->Text = strdup( label );
968 menuEntry->SubMenu = subMenu;
970 fghCalculateMenuBoxSize( );
974 * Removes the specified menu item from the current menu
976 void FGAPIENTRY glutRemoveMenuItem( int item )
978 SFG_MenuEntry* menuEntry;
980 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutRemoveMenuItem" );
982 freeglut_return_if_fail( fgStructure.CurrentMenu );
983 if (fgGetActiveMenu())
984 fgError("Menu manipulation not allowed while menus in use.");
986 /* Get n-th menu entry in the current menu, starting from one: */
987 menuEntry = fghFindMenuEntry( fgStructure.CurrentMenu, item );
989 freeglut_return_if_fail( menuEntry );
991 fgListRemove( &fgStructure.CurrentMenu->Entries, &menuEntry->Node );
992 if ( menuEntry->Text )
993 free( menuEntry->Text );
996 fghCalculateMenuBoxSize( );
1000 * Attaches a menu to the current window
1002 void FGAPIENTRY glutAttachMenu( int button )
1004 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutAttachMenu" );
1006 freeglut_return_if_fail( fgStructure.CurrentWindow );
1008 freeglut_return_if_fail( fgStructure.CurrentMenu );
1009 if (fgGetActiveMenu())
1010 fgError("Menu manipulation not allowed while menus in use.");
1012 freeglut_return_if_fail( button >= 0 );
1013 freeglut_return_if_fail( button < FREEGLUT_MAX_MENUS );
1015 fgStructure.CurrentWindow->Menu[ button ] = fgStructure.CurrentMenu;
1019 * Detaches a menu from the current window
1021 void FGAPIENTRY glutDetachMenu( int button )
1023 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutDetachMenu" );
1025 freeglut_return_if_fail( fgStructure.CurrentWindow );
1027 freeglut_return_if_fail( fgStructure.CurrentMenu );
1028 if (fgGetActiveMenu())
1029 fgError("Menu manipulation not allowed while menus in use.");
1031 freeglut_return_if_fail( button >= 0 );
1032 freeglut_return_if_fail( button < FREEGLUT_MAX_MENUS );
1034 fgStructure.CurrentWindow->Menu[ button ] = NULL;
1038 * A.Donev: Set and retrieve the menu's user data
1040 void* FGAPIENTRY glutGetMenuData( void )
1042 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGetMenuData" );
1043 return fgStructure.CurrentMenu->UserData;
1046 void FGAPIENTRY glutSetMenuData(void* data)
1048 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetMenuData" );
1049 fgStructure.CurrentMenu->UserData=data;
1052 /*** END OF FILE ***/