/* * Changes the specified menu item in the current menu into a sub-menu trigger. */ void FGAPIENTRY glutChangeToSubMenu( int item, const char* label, int subMenuID ) { SFG_Menu* subMenu = fgMenuByID( subMenuID ); SFG_MenuEntry* menuEntry = NULL; freeglut_assert_ready; freeglut_return_if_fail( fgStructure.Menu ); freeglut_return_if_fail( subMenu ); /* * Get n-th menu entry in the current menu, starting from one: */ menuEntry = fghFindMenuEntry( fgStructure.Menu, item ); freeglut_return_if_fail( menuEntry ); /* * We want it to become a sub menu entry, so: */ if( menuEntry->Text ) free( menuEntry->Text ); menuEntry->Text = strdup( label ); menuEntry->SubMenu = subMenu; menuEntry->ID = -1; fghCalculateMenuBoxSize( ); }
/* * Changes the specified menu item in the current menu into a sub-menu trigger. */ void FGAPIENTRY glutChangeToSubMenu( int item, const char* label, int subMenuID ) { SFG_Menu* subMenu; SFG_MenuEntry* menuEntry; FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutChangeToSubMenu" ); freeglut_return_if_fail( fgStructure.CurrentMenu ); if (fgGetActiveMenu()) fgError("Menu manipulation not allowed while menus in use."); /* Get handle to sub menu */ subMenu = fgMenuByID( subMenuID ); menuEntry = NULL; freeglut_return_if_fail( subMenu ); /* Get n-th menu entry in the current menu, starting from one: */ menuEntry = fghFindMenuEntry( fgStructure.CurrentMenu, item ); freeglut_return_if_fail( menuEntry ); /* We want it to become a sub menu entry, so: */ if( menuEntry->Text ) free( menuEntry->Text ); menuEntry->Text = strdup( label ); menuEntry->SubMenu = subMenu; menuEntry->ID = -1; fghCalculateMenuBoxSize( ); }
/* * Removes the specified menu item from the current menu */ void FGAPIENTRY glutRemoveMenuItem( int item ) { SFG_MenuEntry* menuEntry; FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutRemoveMenuItem" ); freeglut_return_if_fail( fgStructure.CurrentMenu ); /* Get n-th menu entry in the current menu, starting from one: */ menuEntry = fghFindMenuEntry( fgStructure.CurrentMenu, item ); freeglut_return_if_fail( menuEntry ); fgListRemove( &fgStructure.CurrentMenu->Entries, &menuEntry->Node ); if ( menuEntry->Text ) free( menuEntry->Text ); free( menuEntry ); fghCalculateMenuBoxSize( ); }
/* * Removes the specified menu item from the current menu */ void FGAPIENTRY glutRemoveMenuItem( int item ) { SFG_MenuEntry* menuEntry; freeglut_assert_ready; freeglut_return_if_fail( fgStructure.Menu ); /* * Get n-th menu entry in the current menu, starting from one: */ menuEntry = fghFindMenuEntry( fgStructure.Menu, item ); freeglut_return_if_fail( menuEntry ); fgListRemove( &fgStructure.Menu->Entries, &menuEntry->Node ); if ( menuEntry->Text ) free( menuEntry->Text ); free( menuEntry ); fghCalculateMenuBoxSize( ); }
/* * Changes the specified menu item in the current menu into a menu entry */ void FGAPIENTRY glutChangeToMenuEntry( int item, const char* label, int value ) { SFG_MenuEntry* menuEntry = NULL; FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutChangeToMenuEntry" ); freeglut_return_if_fail( fgStructure.CurrentMenu ); /* Get n-th menu entry in the current menu, starting from one: */ menuEntry = fghFindMenuEntry( fgStructure.CurrentMenu, item ); freeglut_return_if_fail( menuEntry ); /* We want it to become a normal menu entry, so: */ if( menuEntry->Text ) free( menuEntry->Text ); menuEntry->Text = strdup( label ); menuEntry->ID = value; menuEntry->SubMenu = NULL; fghCalculateMenuBoxSize( ); }
/* * 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; }
/* * 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; }