?
Solved

update command UI in a popup menu

Posted on 2003-03-04
4
Medium Priority
?
804 Views
Last Modified: 2013-11-20
I have a number of popup menus in my app which I show by calling CMenu::TrackPopupMenu().  Unfortunately, the popup menu does not send update-command-UI messages for the commands in the menu until after the menu command is chosen.  So several menu commands which should be disabled show as enabled but function as if they are disabled.

How can I change the following function to cause all the menu items to process update command UI messages before the popup menu is shown (or immediately after, or during the TrackPopupMenu() call)

void StandardPopupMenu(UINT nPopupMenu, CWnd* pCallbackWindow, CPoint TrackPoint)
{
    CMenu PopupMenu;
    VERIFY(PopupMenu.LoadMenu(nPopupMenu));
    //NOTE: every popup menu is one menu deep in the menu resource
    CMenu* pPopupMenu = PopupMenu.GetSubMenu(0);
    ASSERT(pPopupMenu);
    //TODONOW: make sure all of the commands in the menu process
    //  update command UI messages before showing the menu
    VERIFY(pPopupMenu->TrackPopupMenu(TPM_LEFTALIGN|TPM_LEFTBUTTON|TPM_RIGHTBUTTON,
        TrackPoint.x, TrackPoint.y, pCallbackWindow));
} //end StandardPopupMenu()
0
Comment
Question by:ris
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 1

Expert Comment

by:hoabeo
ID: 8086971
I want to know more about pCallbackWindow, what is it when you call StandardPopupMenu?

Generally, when you pass pCallbackWindow to StandardPopupMenu, you should add update-command-ui handlers to that window.

For example: you add those handlers to your Main Window, and pass the pointer to StandardPoppMenu.


Hope this help,
0
 
LVL 1

Author Comment

by:ris
ID: 8088726
CWnd* pCallbackWindow is the window that calls StandardPopupMenu(), and it already has update-command-ui handlers for the applicable menu commands, and those handlers are called.  However, those update-command-ui handlers aren't called before the menu is displayed, but they are called after a menu item is clicked on.  So the behavior is something like this:

1) I right click on the window and that window class's MyWindow::OnContextMenu() handler is called.

2) MyWindow::OnContextMenu() calls StandardPopupMenu(IDR_POPUP_MENU, this, Point)

3) StandardPopupMenu() shows the menu.  Menu items which should be disabled appear enabled

4) I click on a menu item which should be disabled but appears enabled

5) the framework sends an update-command-ui message for that menu item command that I clicked

6) MyWindow::OnUpdateCommandUIMyMenuItem() handles the update-command-ui message for the should-be-disabled menu item that I clicked, and disables the menu item after I clicked it

7) since the menu item is now disabled (after I clicked it) the actual command message for the menu item I clicked is never sent, just as if the menu item had been disabled before I clicked it.

So how can I fix this problem?  Do I have to iterate through the items in pPopupMenu, get the command IDs, and manually set update-command-UI messages to pCallbackWindow before I call pPopupMenu->TrackPopupMenu()?  Will that work?  Is there an easier way?
0
 
LVL 1

Accepted Solution

by:
hoabeo earned 200 total points
ID: 8092350
The problem here is the framework does not support update-command-ui like in Doc-View architecture.
So you must manually handle WM_INITMENUPOPUP in the pCallbackWindow. In that handler, create an instance of CCmdUI, then set approxiate parameters for this, finally call DoUpdate to update command ui
here is the code to do that:
(I just copied from CFrameWnd::OnInitMenuPopup, change something to suit the need of Dialog-based app)
If you like, give me your email addr, then i will send a demo-app.

void CDialogTestDlg::OnInitMenuPopup(CMenu* pMenu, UINT nIndex, BOOL bSysMenu)
{
     CDialog::OnInitMenuPopup(pMenu, nIndex, bSysMenu);

     if (bSysMenu)
          return;     // don't support system menu

     ASSERT(pMenu != NULL);
     // check the enabled state of various menu items

     CCmdUI state;
     state.m_pMenu = pMenu;
     ASSERT(state.m_pOther == NULL);
     ASSERT(state.m_pParentMenu == NULL);

     // determine if menu is popup in top-level menu and set m_pOther to
     //  it if so (m_pParentMenu == NULL indicates that it is secondary popup)
     HMENU hParentMenu;
     if (AfxGetThreadState()->m_hTrackingMenu == pMenu->m_hMenu)
          state.m_pParentMenu = pMenu;    // parent == child for tracking popup
     else if ((hParentMenu = ::GetMenu(m_hWnd)) != NULL)
     {
          CWnd* pParent = GetTopLevelParent();
          // child windows don't have menus -- need to go to the top!
          if (pParent != NULL &&
               (hParentMenu = ::GetMenu(pParent->m_hWnd)) != NULL)
          {
               int nIndexMax = ::GetMenuItemCount(hParentMenu);
               for (int nIndex = 0; nIndex < nIndexMax; nIndex++)
               {
                    if (::GetSubMenu(hParentMenu, nIndex) == pMenu->m_hMenu)
                    {
                         // when popup is found, m_pParentMenu is containing menu
                         state.m_pParentMenu = CMenu::FromHandle(hParentMenu);
                         break;
                    }
               }
          }
     }

     state.m_nIndexMax = pMenu->GetMenuItemCount();
     for (state.m_nIndex = 0; state.m_nIndex < state.m_nIndexMax;
          state.m_nIndex++)
     {
          state.m_nID = pMenu->GetMenuItemID(state.m_nIndex);
          if (state.m_nID == 0)
               continue; // menu separator or invalid cmd - ignore it

          ASSERT(state.m_pOther == NULL);
          ASSERT(state.m_pMenu != NULL);
          if (state.m_nID == (UINT)-1)
          {
               // possibly a popup menu, route to first item of that popup
               state.m_pSubMenu = pMenu->GetSubMenu(state.m_nIndex);
               if (state.m_pSubMenu == NULL ||
                    (state.m_nID = state.m_pSubMenu->GetMenuItemID(0)) == 0 ||
                    state.m_nID == (UINT)-1)
               {
                    continue;       // first item of popup can't be routed to
               }
               state.DoUpdate(this, FALSE);    // popups are never auto disabled
          }
          else
          {
               // normal menu item
               // Auto enable/disable if frame window has 'm_bAutoMenuEnable'
               //    set and command is _not_ a system command.
               state.m_pSubMenu = NULL;
               state.DoUpdate(this, state.m_nID < 0xF000);
          }

          // adjust for menu deletions and additions
          UINT nCount = pMenu->GetMenuItemCount();
          if (nCount < state.m_nIndexMax)
          {
               state.m_nIndex -= (state.m_nIndexMax - nCount);
               while (state.m_nIndex < nCount &&
                    pMenu->GetMenuItemID(state.m_nIndex) == state.m_nID)
               {
                    state.m_nIndex++;
               }
          }
          state.m_nIndexMax = nCount;
     }
}
0
 
LVL 1

Author Comment

by:ris
ID: 8103716
Wow, code and everything!  Thank you so much!  That is exactly what I needed and it works very nicely!
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question