Using MRU on submenu

The following contains a method for putting MRU's on a separate submenu.

After studying Todd C. Gleason's method for implementing MRU's on a submenu MRU's, I ended up taking a slightly different approach. Namely, a helper function to handle the application's OnUpdateFileMruFile1(CCmdUI* pCmdUI) function.

CRecentFileList::UpdateMenu() seems to work OK if there's another item on the submenu before the MRU. So we insert such an item, update the menu, and then remove the inserted menu.

The update function seems to be called under various circumstances.
  • m_pMenu != 0 && m_pSubMenu != 0 -- popup menu opened up but not the submenu
    Don't do anything
  • m_pMenu != 0 && m_pSubMenu == 0 -- Opening the submenu
    Fix up then menu
  • m_pMenu == 0 && m_pSubMenu == 0 -- We've clicked on menu
    Don't do anything
//////////////////////////////////////////////////////////////////////
// SubmenuMRU.h
//
#ifndef _SUBMENUMRU_H_
#define _SUBMENUMRU_H_

class SubmenuMRU
{
public:
  static void Handle_OnUpdateFileMruFile1(CCmdUI* pCmdUI, CRecentFileList* pRecentFileList);
};

#endif
//////////////////////////////////////////////////////////////////////
// SubmenuMRU.cpp
//
#include 
#include 

#include "SubmenuMRU.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

void SubmenuMRU::Handle_OnUpdateFileMruFile1(CCmdUI* pCmdUI, CRecentFileList* pRecentFileList)
{
	if (pRecentFileList == NULL) // no MRU files
		pCmdUI->Enable(FALSE);
	else
    {
        if (pCmdUI->m_pMenu && !pCmdUI->m_pSubMenu) // Only do something if we're the submenu
        {
            // Insert a separator so that CRecentFileList::UpdateMenu() will behave
            pCmdUI->m_pMenu->InsertMenu(0, MF_SEPARATOR | MF_BYPOSITION);
            pCmdUI->m_nIndex++;
            pCmdUI->m_nIndexMax++;

            pRecentFileList->UpdateMenu(pCmdUI); // Update the menu

            // Get rid of the separator we just inerted
            pCmdUI->m_pMenu->DeleteMenu(0, MF_BYPOSITION);
            pCmdUI->m_nIndex--;
            pCmdUI->m_nIndexMax--;
        }
    }
}
//////////////////////////////////////////////////////////////////////
// Snippet from test program's MenuTest.rc
//
IDR_MAINFRAME MENU PRELOAD DISCARDABLE 
BEGIN
    POPUP "&File"
    BEGIN
		...
        POPUP "Recent &Files"
        BEGIN
            MENUITEM "Recent File", ID_FILE_MRU_FILE1, GRAYED
        END
        MENUITEM SEPARATOR
        MENUITEM "E&xit", ID_APP_EXIT
    END
	...
END
//////////////////////////////////////////////////////////////////////
// Snippet from test program's MenuTest.cpp
//
#include "SubmenuMRU.h"

BEGIN_MESSAGE_MAP(CMenuTestApp, CWinApp)
	...
	ON_UPDATE_COMMAND_UI(ID_FILE_MRU_FILE1, OnUpdateFileMruFile1)
END_MESSAGE_MAP()


void CMenuTestApp::OnUpdateFileMruFile1(CCmdUI* pCmdUI) 
{
    // TODO: Add your command update UI handler code here
    SubmenuMRU::Handle_OnUpdateFileMruFile1(pCmdUI, m_pRecentFileList);
}



Comments

  • How to delete a item of recent file list?

    Posted by Legacy on 08/21/2001 12:00am

    Originally posted by: Cherry

    When select a file from recent file list.
    But the file wat changed by others and not the format I need.
    So I want to delete this file from recent file list.
    I know add a file is AddToRecentFileList(LPCTSTR lpszPathName), but how to delete one?
    Thank you!

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: August 20, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT When you look at natural user interfaces as a developer, it isn't just fun and games. There are some very serious, real-world usage models of how things can help make the world a better place – things like Intel® RealSense™ technology. Check out this upcoming eSeminar and join the panel of experts, both from inside and outside of Intel, as they discuss how natural user interfaces will likely be getting adopted in a wide variety …

  • "Security" is the number one issue holding business leaders back from the cloud. But does the reality match the perception? Keeping data close to home, on premises, makes business and IT leaders feel inherently more secure. But the truth is, cloud solutions can offer companies real, tangible security advantages. Before you assume that on-site is the only way to keep data safe, it's worth taking a comprehensive approach to evaluating risks. Doing so can lead to big benefits.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds