Merging Two Menus






4.25/5 (9 votes)
Nov 16, 2001

75622
This function will merge two menus
Introduction
This function will merge two menus. This can be used for context menus as well as for top level menus. At top level known popup menus will be appended or new popups will be created before Window
or Help
. Thanks to M. Knaup (mknaup@bigfoot.de) for the modifications. The function calls itself recursively and does not care about consecutive separators.
bool
MergeMenu(CMenu * pMenuDestination, const CMenu * pMenuAdd, bool bTopLevel /*=false*/)
{
// Abstract:
// Merges two menus.
//
// Parameters:
// pMenuDestination - [in, retval] destination menu handle
// pMenuAdd - [in] menu to merge
// bTopLevel - [in] indicator for special top level behavior
//
// Return value:
// <false> in case of error.
//
// Comments:
// This function calles itself recursivley. If bTopLevel is set to true,
// we append popups at top level or we insert before <Window> or <Help>.
// get the number menu items in the menus
int iMenuAddItemCount = pMenuAdd->GetMenuItemCount();
int iMenuDestItemCount = pMenuDestination->GetMenuItemCount();
// if there are no items return
if( iMenuAddItemCount == 0 )
return true;
// if we are not at top level and the destination menu is not empty
// -> we append a seperator
if( !bTopLevel && iMenuDestItemCount > 0 )
pMenuDestination->AppendMenu(MF_SEPARATOR);
// iterate through the top level of <pmenuadd>
for( int iLoop = 0; iLoop < iMenuAddItemCount; iLoop++ )
{
// get the menu string from the add menu
CString sMenuAddString;
pMenuAdd->GetMenuString( iLoop, sMenuAddString, MF_BYPOSITION );
// try to get the submenu of the current menu item
CMenu* pSubMenu = pMenuAdd->GetSubMenu(iLoop);
// check if we have a sub menu
if (!pSubMenu)
{
// normal menu item
// read the source and append at the destination
UINT nState = pMenuAdd->GetMenuState( iLoop, MF_BYPOSITION );
UINT nItemID = pMenuAdd->GetMenuItemID( iLoop );
if( pMenuDestination->AppendMenu( nState, nItemID, sMenuAddString ))
{
// menu item added, don't forget to correct the item count
iMenuDestItemCount++;
}
else
{
TRACE( "MergeMenu: AppendMenu failed!\n" );
return false;
}
}
else
{
// create or insert a new popup menu item
// default insert pos is like ap
int iInsertPosDefault = -1;
// if we are at top level merge into existing popups rather than
// creating new ones
if( bTopLevel )
{
ASSERT( sMenuAddString != "&?" && sMenuAddString != "?" );
CString sAdd( sMenuAddString );
sAdd.Remove('&'); // for comparison of menu items supress '&'
bool bAdded = false;
// try to find existing popup
for( int iLoop1 = 0; iLoop1 < iMenuDestItemCount; iLoop1++ )
{
// get the menu string from the destination menu
CString sDest;
pMenuDestination->GetMenuString( iLoop1, sDest, MF_BYPOSITION );
sDest.Remove( '&' ); // for a better compare (s.a.)
if( sAdd == sDest )
{
// we got a hit -> merge the two popups
// try to get the submenu of the desired destination menu item
CMenu* pSubMenuDest = pMenuDestination->GetSubMenu( iLoop1 );
if( pSubMenuDest )
{
// merge the popup recursivly and continue with outer for loop
if( !MergeMenu( pSubMenuDest, pSubMenu ))
return false;
bAdded = true;
break;
}
}
// alternativ insert before <Window> or <Help>
if( iInsertPosDefault == -1 &&
( sDest == "Window" || sDest == "?" || sDest == "Help" ))
iInsertPosDefault = iLoop1;
}
if( bAdded )
{
// menu added, so go on with loop over pMenuAdd's top level
continue;
}
}
// if the top level search did not find a position append the menu
if( iInsertPosDefault == -1 )
iInsertPosDefault = pMenuDestination->GetMenuItemCount();
// create a new popup and insert before <Window> or <Help>
CMenu NewPopupMenu;
if( !NewPopupMenu.CreatePopupMenu() )
{
TRACE( "MergeMenu: CreatePopupMenu failed!\n" );
return false;
}
// merge the new popup recursivly
if( !MergeMenu( &NewPopupMenu, pSubMenu ))
return false;
// insert the new popup menu into the destination menu
HMENU hNewMenu = NewPopupMenu.GetSafeHmenu();
if( pMenuDestination->InsertMenu( iInsertPosDefault,
MF_BYPOSITION | MF_POPUP | MF_ENABLED,
(UINT)hNewMenu, sMenuAddString ))
{
// don't forget to correct the item count
iMenuDestItemCount++;
}
else
{
TRACE( "MergeMenu: InsertMenu failed!\n" );
return false;
}
// don't destroy the new menu
NewPopupMenu.Detach();
}
}
return true;
}</pmenuadd>
License
This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt, please contact the author via the discussion board below.
A list of licenses authors might use can be found here.