menus attached to child windows did not appear in the right place.

Fixed in fghActivateMenu, whose logic is now simpler and commented as
well


git-svn-id: https://svn.code.sf.net/p/freeglut/code/trunk@1398 7f0cb862-5218-0410-a997-914c9d46530a
This commit is contained in:
dcnieho 2012-11-17 01:02:30 +00:00
parent de8853e675
commit a7771ab4b2
2 changed files with 28 additions and 9 deletions

View File

@ -277,12 +277,17 @@ void SampleSpecial( int nSpecial, int nMouseX, int nMouseY )
*/ */
void SampleMenu( int menuID ) void SampleMenu( int menuID )
{ {
/*
* Just print something funny
*/
printf( "SampleMenu() callback executed, menuID is %i\n", menuID ); printf( "SampleMenu() callback executed, menuID is %i\n", menuID );
} }
/*
* A sample menu status callback
*/
void SampleMenuStatus( int status, int x, int y )
{
printf ( "SampleMenu() callback executed, MenuStatus is %i at (%i,%i)\n", status, x, y );
}
/* /*
* The sample's entry point * The sample's entry point
*/ */
@ -325,6 +330,7 @@ int main( int argc, char** argv )
glutSpecialFunc( SampleSpecial ); glutSpecialFunc( SampleSpecial );
glutIdleFunc( SampleIdle ); glutIdleFunc( SampleIdle );
glutEntryFunc( SampleEntry ); glutEntryFunc( SampleEntry );
glutMenuStatusFunc( SampleMenuStatus );
glutAttachMenu( GLUT_LEFT_BUTTON ); glutAttachMenu( GLUT_LEFT_BUTTON );
glutInitWindowPosition( 200, 200 ); glutInitWindowPosition( 200, 200 );
@ -334,6 +340,8 @@ int main( int argc, char** argv )
glutKeyboardFunc( SampleKeyboard ); glutKeyboardFunc( SampleKeyboard );
glutSpecialFunc( SampleSpecial ); glutSpecialFunc( SampleSpecial );
glutIdleFunc( SampleIdle ); glutIdleFunc( SampleIdle );
glutEntryFunc( SampleEntry );
glutMenuStatusFunc( SampleMenuStatus );
glutAttachMenu( GLUT_LEFT_BUTTON ); glutAttachMenu( GLUT_LEFT_BUTTON );
glutSetMenu(subMenuA); glutSetMenu(subMenuA);
glutAttachMenu( GLUT_RIGHT_BUTTON ); glutAttachMenu( GLUT_RIGHT_BUTTON );
@ -367,6 +375,8 @@ int main( int argc, char** argv )
glutReshapeFunc( SampleReshape ); glutReshapeFunc( SampleReshape );
glutKeyboardFunc( SampleGameModeKeyboard ); glutKeyboardFunc( SampleGameModeKeyboard );
glutIdleFunc( SampleIdle ); glutIdleFunc( SampleIdle );
glutEntryFunc( SampleEntry );
glutMenuStatusFunc( SampleMenuStatus );
glutSetMenu(menuID); glutSetMenu(menuID);
glutAttachMenu( GLUT_LEFT_BUTTON ); glutAttachMenu( GLUT_LEFT_BUTTON );

View File

@ -509,6 +509,7 @@ void fgDisplayMenu( void )
static void fghActivateMenu( SFG_Window* window, int button ) static void fghActivateMenu( SFG_Window* window, int button )
{ {
int max_x, max_y; int max_x, max_y;
SFG_XYUse mouse_pos;
/* We'll be referencing this menu a lot, so remember its address: */ /* We'll be referencing this menu a lot, so remember its address: */
SFG_Menu* menu = window->Menu[ button ]; SFG_Menu* menu = window->Menu[ button ];
@ -527,9 +528,17 @@ static void fghActivateMenu( SFG_Window* window, int button )
/* Set up the initial menu position now: */ /* Set up the initial menu position now: */
fghGetVMaxExtent(menu->ParentWindow, &max_x, &max_y); fghGetVMaxExtent(menu->ParentWindow, &max_x, &max_y);
fgSetWindow( window ); fgSetWindow( window );
menu->X = window->State.MouseX + glutGet( GLUT_WINDOW_X ); /* get mouse position on screen (window->State.MouseX and window->State.MouseY
menu->Y = window->State.MouseY + glutGet( GLUT_WINDOW_Y ); * are relative to client area origin), and not easy to correct given that
* glutGet( GLUT_WINDOW_X ) and glutGet( GLUT_WINDOW_Y ) return relative to parent
* origin when looking at a child window
* for parent windows: window->State.MouseX + glutGet( GLUT_WINDOW_X ) == mouse_pos.X
*/
fghPlatformGetCursorPos(&mouse_pos);
menu->X = mouse_pos.X;
menu->Y = mouse_pos.Y;
/* Make sure the whole menu is on the screen */
if( menu->X + menu->Width > max_x ) if( menu->X + menu->Width > max_x )
menu->X -=menu->Width; menu->X -=menu->Width;
@ -540,10 +549,9 @@ static void fghActivateMenu( SFG_Window* window, int button )
menu->Y = 0; menu->Y = 0;
} }
menu->Window->State.MouseX = /* Set position of mouse relative to top-left menu in menu's window state (could as well set 0 at creation time...) */
window->State.MouseX + glutGet( GLUT_WINDOW_X ) - menu->X; menu->Window->State.MouseX = mouse_pos.X - menu->X;
menu->Window->State.MouseY = menu->Window->State.MouseY = mouse_pos.Y - menu->Y;
window->State.MouseY + glutGet( GLUT_WINDOW_Y ) - menu->Y;
/* Menu status callback */ /* Menu status callback */
if (fgState.MenuStateCallback || fgState.MenuStatusCallback) if (fgState.MenuStateCallback || fgState.MenuStatusCallback)
@ -553,6 +561,7 @@ static void fghActivateMenu( SFG_Window* window, int button )
if (fgState.MenuStateCallback) if (fgState.MenuStateCallback)
fgState.MenuStateCallback(GLUT_MENU_IN_USE); fgState.MenuStateCallback(GLUT_MENU_IN_USE);
if (fgState.MenuStatusCallback) if (fgState.MenuStatusCallback)
/* window->State.MouseX and window->State.MouseY are relative to client area origin, as needed */
fgState.MenuStatusCallback(GLUT_MENU_IN_USE, window->State.MouseX, window->State.MouseY); fgState.MenuStatusCallback(GLUT_MENU_IN_USE, window->State.MouseX, window->State.MouseY);
} }