/* * Displays the currently active menu for the current window */ void fgDisplayMenu( void ) { SFG_Window* window = fgStructure.Window; SFG_Menu* menu = NULL; freeglut_assert_window; /* * Check if there is an active menu attached to this window... */ menu = window->ActiveMenu; freeglut_return_if_fail( menu ); fgSetWindow( menu->Window ); glPushAttrib( GL_DEPTH_BUFFER_BIT | GL_TEXTURE_BIT | GL_LIGHTING_BIT | GL_POLYGON_BIT ); glDisable( GL_DEPTH_TEST ); glDisable( GL_TEXTURE_2D ); glDisable( GL_LIGHTING ); glDisable( GL_CULL_FACE ); glMatrixMode( GL_PROJECTION ); glPushMatrix( ); glLoadIdentity( ); glOrtho( 0, glutGet( GLUT_WINDOW_WIDTH ), glutGet( GLUT_WINDOW_HEIGHT ), 0, -1, 1 ); glMatrixMode( GL_MODELVIEW ); glPushMatrix( ); glLoadIdentity( ); fghCheckMenuStatus( window, menu ); fghDisplayMenuBox( menu ); glPopAttrib( ); glMatrixMode( GL_PROJECTION ); glPopMatrix( ); glMatrixMode( GL_MODELVIEW ); glPopMatrix( ); glutSwapBuffers( ); fgSetWindow ( window ); }
/* * Activates a menu pointed by the function argument */ static void fghActivateMenu( SFG_Window* window, int button ) { int max_x, max_y; /* We'll be referencing this menu a lot, so remember its address: */ SFG_Menu* menu = window->Menu[ button ]; SFG_Window* current_window = fgStructure.CurrentWindow; /* If the menu is already active in another window, deactivate it there */ if ( menu->ParentWindow ) menu->ParentWindow->ActiveMenu = NULL ; /* Mark the menu as active, so that it gets displayed: */ window->ActiveMenu = menu; menu->IsActive = GL_TRUE; fghSetMenuParentWindow ( window, menu ); fgState.ActiveMenus++; /* Set up the initial menu position now: */ fghGetVMaxExtent(menu->ParentWindow, &max_x, &max_y); fgSetWindow( window ); menu->X = window->State.MouseX + glutGet( GLUT_WINDOW_X ); menu->Y = window->State.MouseY + glutGet( GLUT_WINDOW_Y ); if( menu->X + menu->Width > max_x ) menu->X -=menu->Width; if( menu->Y + menu->Height > max_y ) { menu->Y -=menu->Height; if( menu->Y < 0 ) menu->Y = 0; } menu->Window->State.MouseX = window->State.MouseX + glutGet( GLUT_WINDOW_X ) - menu->X; menu->Window->State.MouseY = window->State.MouseY + glutGet( GLUT_WINDOW_Y ) - menu->Y; fgSetWindow( menu->Window ); glutPositionWindow( menu->X, menu->Y ); glutReshapeWindow( menu->Width, menu->Height ); glutPopWindow( ); glutShowWindow( ); menu->Window->ActiveMenu = menu; fghCheckMenuStatus( menu ); fgSetWindow( current_window ); }
/* * Check whether an active menu absorbs a mouse click */ GLboolean fgCheckActiveMenu ( SFG_Window *window, SFG_Menu *menu ) { /* * Near as I can tell, this is the active menu behaviour: * - Down-click any button outside the menu, menu active: * deactivate the menu * - Down-click any button inside the menu, menu active: * select the menu entry and deactivate the menu * - Up-click the menu button outside the menu, menu active: * nothing happens * - Up-click the menu button inside the menu, menu active: * select the menu entry and deactivate the menu * Since menus can have submenus, we need to check this recursively. */ return fghCheckMenuStatus( window, menu ); }
/* * Private function to check for the current menu/sub menu activity state */ static GLboolean fghCheckMenuStatus( SFG_Window* window, SFG_Menu* menu ) { SFG_MenuEntry* menuEntry; int x, y; /* * First of all check any of the active sub menus... */ for( menuEntry = (SFG_MenuEntry *)menu->Entries.First; menuEntry; menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next ) { if( menuEntry->SubMenu && menuEntry->IsActive ) { /* * OK, have the sub-menu checked, too. If it returns GL_TRUE, it * will mean that it caught the mouse cursor and we do not need * to regenerate the activity list, and so our parents do... */ GLboolean return_status = fghCheckMenuStatus( window, menuEntry->SubMenu ); /* * Reactivate the submenu as the checkMenuStatus may have turned * it off if the mouse is in its parent menu entry. */ menuEntry->SubMenu->IsActive = GL_TRUE; if ( return_status ) return GL_TRUE; } } /* * That much about our sub menus, let's get to checking the current menu: */ x = window->State.MouseX; y = window->State.MouseY; for( menuEntry = (SFG_MenuEntry *)menu->Entries.First; menuEntry; menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next ) menuEntry->IsActive = GL_FALSE; menu->IsActive = GL_FALSE; /* * Check if the mouse cursor is contained within the current menu box */ if( ( x >= FREEGLUT_MENU_BORDER ) && ( x < menu->Width - FREEGLUT_MENU_BORDER ) && ( y >= FREEGLUT_MENU_BORDER ) && ( y < menu->Height - FREEGLUT_MENU_BORDER ) && ( window == menu->Window ) ) { int menuID = ( y - FREEGLUT_MENU_BORDER ) / FREEGLUT_MENU_HEIGHT; /* * The mouse cursor is somewhere over our box, check it out. */ menuEntry = fghFindMenuEntry( menu, menuID + 1 ); assert( menuEntry ); menuEntry->IsActive = GL_TRUE; menuEntry->Ordinal = menuID; /* * If this is not the same as the last active menu entry, deactivate * the previous entry. Specifically, if the previous active entry * was a submenu then deactivate it. */ if( menu->ActiveEntry && ( menuEntry != menu->ActiveEntry ) ) if( menu->ActiveEntry->SubMenu ) fgDeactivateSubMenu( menu->ActiveEntry ); menu->ActiveEntry = menuEntry; menu->IsActive = GL_TRUE; /* * OKi, we have marked that entry as active, but it would be also * nice to have its contents updated, in case it's a sub menu. * Also, ignore the return value of the check function: */ if( menuEntry->SubMenu ) { if ( ! menuEntry->SubMenu->IsActive ) { SFG_Window *current_window = fgStructure.Window; /* * Set up the initial menu position now... */ menuEntry->SubMenu->IsActive = GL_TRUE; /* * Set up the initial submenu position now: */ menuEntry->SubMenu->X = menu->X + menu->Width; menuEntry->SubMenu->Y = menu->Y + menuEntry->Ordinal * FREEGLUT_MENU_HEIGHT; if( menuEntry->SubMenu->X + menuEntry->SubMenu->Width > glutGet( GLUT_SCREEN_WIDTH ) ) menuEntry->SubMenu->X = menu->X - menuEntry->SubMenu->Width; if( menuEntry->SubMenu->Y + menuEntry->SubMenu->Height > glutGet( GLUT_SCREEN_HEIGHT ) ) menuEntry->SubMenu->Y -= ( menuEntry->SubMenu->Height - FREEGLUT_MENU_HEIGHT - 2 * FREEGLUT_MENU_BORDER ); fgSetWindow( menuEntry->SubMenu->Window ); glutPositionWindow( menuEntry->SubMenu->X, menuEntry->SubMenu->Y ); glutReshapeWindow( menuEntry->SubMenu->Width, menuEntry->SubMenu->Height ); glutPopWindow( ); glutShowWindow( ); menuEntry->SubMenu->Window->ActiveMenu = menuEntry->SubMenu; fgSetWindow( current_window ); } fghCheckMenuStatus( window, menuEntry->SubMenu ); /* * Activate it because its parent entry is active */ menuEntry->SubMenu->IsActive = GL_TRUE; } /* * Report back that we have caught the menu cursor */ return GL_TRUE; } /* * Looks like the menu cursor is somewhere else... */ return GL_FALSE; }
/* * Check whether an active menu absorbs a mouse click */ GLboolean fgCheckActiveMenu ( SFG_Window *window, int button, GLboolean pressed, int mouse_x, int mouse_y ) { /* * Near as I can tell, this is the menu behaviour: * - Down-click the menu button, menu not active: activate * the menu with its upper left-hand corner at the mouse * location. * - Down-click any button outside the menu, menu active: * deactivate the menu * - Down-click any button inside the menu, menu active: * select the menu entry and deactivate the menu * - Up-click the menu button, menu not active: nothing happens * - Up-click the menu button outside the menu, menu active: * nothing happens * - Up-click the menu button inside the menu, menu active: * select the menu entry and deactivate the menu * Since menus can have submenus, we need to check this recursively. */ if( window->ActiveMenu ) { if( window == window->ActiveMenu->ParentWindow ) { window->ActiveMenu->Window->State.MouseX = mouse_x - window->ActiveMenu->X; window->ActiveMenu->Window->State.MouseY = mouse_y - window->ActiveMenu->Y; } /* In the menu, invoke the callback and deactivate the menu */ if( fghCheckMenuStatus( window->ActiveMenu ) ) { /* * Save the current window and menu and set the current * window to the window whose menu this is */ SFG_Window *save_window = fgStructure.CurrentWindow; SFG_Menu *save_menu = fgStructure.CurrentMenu; SFG_Window *parent_window = window->ActiveMenu->ParentWindow; fgSetWindow( parent_window ); fgStructure.CurrentMenu = window->ActiveMenu; /* Execute the menu callback */ fghExecuteMenuCallback( window->ActiveMenu ); fgDeactivateMenu( parent_window ); /* Restore the current window and menu */ fgSetWindow( save_window ); fgStructure.CurrentMenu = save_menu; } else if( pressed ) /* * Outside the menu, deactivate if it's a downclick * * A downclick outside of the interior of our freeglut windows * is dealt with in the WM_KILLFOCUS handler of fgPlatformWindowProc */ fgDeactivateMenu( window->ActiveMenu->ParentWindow ); /* * XXX Why does an active menu require a redisplay at * XXX this point? If this can come out cleanly, then * XXX it probably should do so; if not, a comment should * XXX explain it. */ if( ! window->IsMenu ) window->State.Redisplay = GL_TRUE; return GL_TRUE; } /* No active menu, let's check whether we need to activate one. */ if( ( 0 <= button ) && ( FREEGLUT_MAX_MENUS > button ) && ( window->Menu[ button ] ) && pressed ) { /* XXX Posting a requisite Redisplay seems bogus. */ window->State.Redisplay = GL_TRUE; fghActivateMenu( window, button ); return GL_TRUE; } return GL_FALSE; }
/* * Update Highlight states of the menu * * Current mouse position is in menu->Window->State.MouseX/Y. */ void fgUpdateMenuHighlight ( SFG_Menu *menu ) { fghCheckMenuStatus( menu ); }
/* * Activates a menu pointed by the function argument */ static void fghActivateMenu( SFG_Window* window, int button ) { int max_x, max_y; SFG_XYUse mouse_pos; /* We'll be referencing this menu a lot, so remember its address: */ SFG_Menu* menu = window->Menu[ button ]; SFG_Window* current_window = fgStructure.CurrentWindow; /* If the menu is already active in another window, deactivate it (and any submenus) there */ if ( menu->ParentWindow ) fgDeactivateMenu(menu->ParentWindow); /* Mark the menu as active, so that it gets displayed: */ window->ActiveMenu = menu; menu->IsActive = GL_TRUE; fghSetMenuParentWindow ( window, menu ); fgState.ActiveMenus++; /* Set up the initial menu position now: */ fghGetVMaxExtent(menu->ParentWindow, &max_x, &max_y); fgSetWindow( window ); /* get mouse position on screen (window->State.MouseX and window->State.MouseY * 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 */ fghGetCursorPos(&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 ) menu->X -=menu->Width; if( menu->Y + menu->Height > max_y ) { menu->Y -=menu->Height; if( menu->Y < 0 ) menu->Y = 0; } /* Set position of mouse relative to top-left menu in menu's window state (could as well set 0 at creation time...) */ menu->Window->State.MouseX = mouse_pos.X - menu->X; menu->Window->State.MouseY = mouse_pos.Y - menu->Y; /* Menu status callback */ if (fgState.MenuStateCallback || fgState.MenuStatusCallback) { fgStructure.CurrentMenu = menu; fgStructure.CurrentWindow = window; if (fgState.MenuStateCallback) fgState.MenuStateCallback(GLUT_MENU_IN_USE); 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); } fgSetWindow( menu->Window ); glutPositionWindow( menu->X, menu->Y ); glutReshapeWindow( menu->Width, menu->Height ); glutPopWindow( ); glutShowWindow( ); menu->Window->ActiveMenu = menu; fghCheckMenuStatus( menu ); fgSetWindow( current_window ); }
/* * Private function to check for the current menu/sub menu activity state */ static GLboolean fghCheckMenuStatus( SFG_Menu* menu ) { SFG_MenuEntry* menuEntry; int x, y; /* First of all check any of the active sub menus... */ for( menuEntry = (SFG_MenuEntry *)menu->Entries.First; menuEntry; menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next ) { if( menuEntry->SubMenu && menuEntry->IsActive ) { /* * OK, have the sub-menu checked, too. If it returns GL_TRUE, it * will mean that it caught the mouse cursor and we do not need * to regenerate the activity list, and so our parents do... */ GLboolean return_status; menuEntry->SubMenu->Window->State.MouseX = menu->Window->State.MouseX + menu->X - menuEntry->SubMenu->X; menuEntry->SubMenu->Window->State.MouseY = menu->Window->State.MouseY + menu->Y - menuEntry->SubMenu->Y; return_status = fghCheckMenuStatus( menuEntry->SubMenu ); if ( return_status ) return GL_TRUE; } } /* That much about our sub menus, let's get to checking the current menu: */ x = menu->Window->State.MouseX; y = menu->Window->State.MouseY; /* Check if the mouse cursor is contained within the current menu box */ if( ( x >= FREEGLUT_MENU_BORDER ) && ( x < menu->Width - FREEGLUT_MENU_BORDER ) && ( y >= FREEGLUT_MENU_BORDER ) && ( y < menu->Height - FREEGLUT_MENU_BORDER ) ) { int menuID = ( y - FREEGLUT_MENU_BORDER ) / FREEGLUT_MENU_HEIGHT; /* The mouse cursor is somewhere over our box, check it out. */ menuEntry = fghFindMenuEntry( menu, menuID + 1 ); FREEGLUT_INTERNAL_ERROR_EXIT( menuEntry, "Cannot find menu entry", "fghCheckMenuStatus" ); menuEntry->IsActive = GL_TRUE; menuEntry->Ordinal = menuID; /* * If this is not the same as the last active menu entry, deactivate * the previous entry. Specifically, if the previous active entry * was a submenu then deactivate it. */ if( menu->ActiveEntry && ( menuEntry != menu->ActiveEntry ) ) if( menu->ActiveEntry->SubMenu ) fghDeactivateSubMenu( menu->ActiveEntry ); if( menuEntry != menu->ActiveEntry ) { menu->Window->State.Redisplay = GL_TRUE; if( menu->ActiveEntry ) menu->ActiveEntry->IsActive = GL_FALSE; } menu->ActiveEntry = menuEntry; menu->IsActive = GL_TRUE; /* XXX Do we need this? */ /* * OKi, we have marked that entry as active, but it would be also * nice to have its contents updated, in case it's a sub menu. * Also, ignore the return value of the check function: */ if( menuEntry->SubMenu ) { if ( ! menuEntry->SubMenu->IsActive ) { int max_x, max_y; SFG_Window *current_window = fgStructure.CurrentWindow; /* Set up the initial menu position now... */ menuEntry->SubMenu->IsActive = GL_TRUE; /* Set up the initial submenu position now: */ fghGetVMaxExtent(menu->ParentWindow, &max_x, &max_y); menuEntry->SubMenu->X = menu->X + menu->Width; menuEntry->SubMenu->Y = menu->Y + menuEntry->Ordinal * FREEGLUT_MENU_HEIGHT; if( menuEntry->SubMenu->X + menuEntry->SubMenu->Width > max_x ) menuEntry->SubMenu->X = menu->X - menuEntry->SubMenu->Width; if( menuEntry->SubMenu->Y + menuEntry->SubMenu->Height > max_y ) { menuEntry->SubMenu->Y -= ( menuEntry->SubMenu->Height - FREEGLUT_MENU_HEIGHT - 2 * FREEGLUT_MENU_BORDER ); if( menuEntry->SubMenu->Y < 0 ) menuEntry->SubMenu->Y = 0; } fgSetWindow( menuEntry->SubMenu->Window ); glutPositionWindow( menuEntry->SubMenu->X, menuEntry->SubMenu->Y ); glutReshapeWindow( menuEntry->SubMenu->Width, menuEntry->SubMenu->Height ); glutPopWindow( ); glutShowWindow( ); menuEntry->SubMenu->Window->ActiveMenu = menuEntry->SubMenu; fgSetWindow( current_window ); menuEntry->SubMenu->Window->State.MouseX = x + menu->X - menuEntry->SubMenu->X; menuEntry->SubMenu->Window->State.MouseY = y + menu->Y - menuEntry->SubMenu->Y; fghCheckMenuStatus( menuEntry->SubMenu ); } /* Activate it because its parent entry is active */ menuEntry->SubMenu->IsActive = GL_TRUE; /* XXX Do we need this? */ } /* Report back that we have caught the menu cursor */ return GL_TRUE; } /* Looks like the menu cursor is somewhere else... */ if( menu->ActiveEntry && menu->ActiveEntry->IsActive && ( !menu->ActiveEntry->SubMenu || !menu->ActiveEntry->SubMenu->IsActive ) ) { menu->Window->State.Redisplay = GL_TRUE; menu->ActiveEntry->IsActive = GL_FALSE; menu->ActiveEntry = NULL; } return GL_FALSE; }
/* * Check whether an active menu absorbs a mouse click */ GLboolean fgCheckActiveMenu ( SFG_Window *window, int button, GLboolean pressed, int mouse_x, int mouse_y ) { GLboolean is_handled = GL_FALSE; GLboolean is_clicked = GL_FALSE; /* * Near as I can tell, this is the menu behaviour: * - Down-click the menu button, menu not active: activate * the menu with its upper left-hand corner at the mouse * location. * - Down-click any button outside the menu, menu active: * deactivate the menu, and potentially activate a new menu * at the new mouse location. This includes clicks in * different windows of course * - Down-click any button inside the menu, menu active: * select the menu entry and deactivate the menu * - Up-click the menu button, menu not active: nothing happens * - Up-click the menu button outside the menu, menu active: * nothing happens * - Up-click the menu button inside the menu, menu active: * select the menu entry and deactivate the menu * Since menus can have submenus, we need to check this recursively. */ if( window->ActiveMenu ) { if( window == window->ActiveMenu->ParentWindow ) { window->ActiveMenu->Window->State.MouseX = mouse_x - window->ActiveMenu->X; window->ActiveMenu->Window->State.MouseY = mouse_y - window->ActiveMenu->Y; } /* In the menu, deactivate the menu and invoke the callback */ if( fghCheckMenuStatus( window->ActiveMenu ) ) { /* * Save the current window and menu and set the current * window to the window whose menu this is */ SFG_Window *save_window = fgStructure.CurrentWindow; SFG_Menu *save_menu = fgStructure.CurrentMenu, *active_menu = window->ActiveMenu; /* active menu is always the one with the mouse in it, due to fghCheckMenuStatus */ SFG_MenuEntry *active_entry = active_menu->ActiveEntry; /* currently highlighted item -> must be the one that was just clicked */ SFG_Window *parent_window = window->ActiveMenu->ParentWindow; /* ignore clicks on the submenu entry */ if (!active_entry->SubMenu) { fgSetWindow( parent_window ); fgStructure.CurrentMenu = active_menu; /* Deactivate menu and then call callback (we don't want menu to stay in view while callback is executing, and user should be able to change menus in callback) */ fgDeactivateMenu( parent_window ); active_menu->Callback( active_entry->ID ); /* Restore the current window and menu */ fgSetWindow( save_window ); fgStructure.CurrentMenu = save_menu; } is_clicked = GL_TRUE; /* Don't reopen... */ } else if( pressed ) /* * Outside the menu, deactivate if it's a downclick * * A downclick outside of the interior of our freeglut windows * is dealt with in the WM_KILLFOCUS handler of fgPlatformWindowProc */ { fgDeactivateMenu( window->ActiveMenu->ParentWindow ); /* Could reopen again in different location, as is_clicked remains false */ } is_handled = GL_TRUE; } else if ( fgState.ActiveMenus ) /* Don't have to check whether this was a downpress or an uppress, there is no way to get an uppress in another window before a downpress... */ { /* if another window than the one clicked in has an open menu, close it */ SFG_Menu *menu = fgGetActiveMenu(); if ( menu ) /* any open menu? */ fgDeactivateMenu( menu->ParentWindow ); /* Leave is_handled to false, we didn't do anything relevant from the perspective of the window that was clicked */ } /* No active menu, let's check whether we need to activate one. */ if( !is_clicked && ( 0 <= button ) && ( FREEGLUT_MAX_MENUS > button ) && ( window->Menu[ button ] ) && pressed ) { /* If mouseclick was outside the parent window, ignore. This can * happen when another mouse button is already depressed and the * window thus has mouse capture */ if (window->State.MouseX>0 && window->State.MouseY>0 && window->State.MouseX<window->State.Width && window->State.MouseY<window->State.Height) { fghActivateMenu( window, button ); is_handled = GL_TRUE; } } return is_handled; }