Click here to Skip to main content
Click here to Skip to main content
Go to top

Merging Two Menus

, 15 Nov 2001
Rate this:
Please Sign up or sign in to vote.
This function will merge two menues
<!-- Main HTML starts here -->

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;
}

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

Share

About the Author

Oskar Wieland
Web Developer
Germany Germany
No Biography provided

Comments and Discussions

 
Questionlicense Pinmemberdigicompass2-Jan-13 21:44 
GeneralSimply, great job! Pinmemberpiotrmb16-May-06 22:23 
GeneralNo name put in the menu bar PinmemberFred Foscalina17-Jun-02 20:34 
GeneralRe: No name put in the menu bar PinmemberMichaelDecker2-Dec-02 13:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.141002.1 | Last Updated 16 Nov 2001
Article Copyright 2001 by Oskar Wieland
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid