Index: src/chext/MenuExt.cpp =================================================================== diff -u -r9ddf8fdd5f641491dd30c49eb90f8f740314b6af -r250c9a4d3a066af0d40def01e15df5753748aefe --- src/chext/MenuExt.cpp (.../MenuExt.cpp) (revision 9ddf8fdd5f641491dd30c49eb90f8f740314b6af) +++ src/chext/MenuExt.cpp (.../MenuExt.cpp) (revision 250c9a4d3a066af0d40def01e15df5753748aefe) @@ -105,8 +105,11 @@ } } -STDMETHODIMP CMenuExt::QueryContextMenu(HMENU hMenu, UINT indexMenu, UINT idCmdFirst, UINT /*idCmdLast*/, UINT /*uFlags*/) +STDMETHODIMP CMenuExt::QueryContextMenu(HMENU hMenu, UINT indexMenu, UINT idCmdFirst, UINT idCmdLast, UINT uFlags) { + idCmdLast; + uFlags; + try { LOG_DEBUG(m_spLog) << L"Querying context menu"; @@ -122,6 +125,9 @@ if(!hWnd) return S_OK; + if (m_tContextMenuHandler.HasCHItems(hMenu)) + return S_OK; + // current commands count in menu TCHAR szText[ _MAX_PATH ]; int iCount = ::GetMenuItemCount(hMenu); Index: src/chext/TContextMenuHandler.cpp =================================================================== diff -u -r9ddf8fdd5f641491dd30c49eb90f8f740314b6af -r250c9a4d3a066af0d40def01e15df5753748aefe --- src/chext/TContextMenuHandler.cpp (.../TContextMenuHandler.cpp) (revision 9ddf8fdd5f641491dd30c49eb90f8f740314b6af) +++ src/chext/TContextMenuHandler.cpp (.../TContextMenuHandler.cpp) (revision 250c9a4d3a066af0d40def01e15df5753748aefe) @@ -50,6 +50,31 @@ UpdateMenuRecursive(spRootMenuItem, hMenu, uiFirstItemPosition, rShellExtData, spFormatter, bShowFreeSpace, bOverrideDefaultItem); } +bool TContextMenuHandler::HasCHItems(HMENU hMenu) const +{ + // current commands count in menu + TCHAR szText[_MAX_PATH]; + int iCount = ::GetMenuItemCount(hMenu); + + // find a place where the commands should be inserted + for (int iMenuIndex = 0; iMenuIndex < iCount; iMenuIndex++) + { + MENUITEMINFO mii = { 0 }; + mii.cbSize = sizeof(mii); + mii.fMask = MIIM_STRING | MIIM_DATA; + mii.dwTypeData = szText; + mii.cch = _MAX_PATH; + + if (!::GetMenuItemInfo(hMenu, iMenuIndex, TRUE, &mii)) + continue; + + if (mii.dwItemData == CHItemMarker) + return true; + } + + return false; +} + void TContextMenuHandler::UpdateMenuRecursive(const TShellMenuItemPtr& spRootMenuItem, HMENU hMenu, UINT uiFirstItemPosition, const TShellExtData& rShellExtData, const chengine::TSizeFormatterPtr& spFormatter, bool bShowFreeSpace, bool bOverrideDefaultItem) { @@ -64,14 +89,15 @@ HMENU hSubMenu = CreatePopupMenu(); UpdateMenuRecursive(spMenuItem, hSubMenu, 0, rShellExtData, spFormatter, bShowFreeSpace, bOverrideDefaultItem); - MENUITEMINFO mii; + MENUITEMINFO mii = {0}; mii.cbSize = sizeof(MENUITEMINFO); - mii.fMask = MIIM_ID | MIIM_STATE | MIIM_SUBMENU | MIIM_STRING; + mii.fMask = MIIM_ID | MIIM_STATE | MIIM_SUBMENU | MIIM_STRING | MIIM_DATA; mii.fType = MFT_STRING; mii.fState = (spRootMenuItem->GetChildrenCount() > 0) ? MFS_ENABLED : MFS_GRAYED; mii.wID = m_uiNextMenuID++; mii.hSubMenu = hSubMenu; mii.dwTypeData = (PTSTR)spMenuItem->GetLocalName().c_str(); + mii.dwItemData = CHItemMarker; mii.cch = 0; ::InsertMenuItem(hMenu, uiFirstItemPosition++, TRUE, &mii); @@ -89,8 +115,16 @@ std::wstring wstrItemName = GetDisplayText(spMenuItem, spFormatter, bShowFreeSpace); - ::InsertMenu(hMenu, uiFirstItemPosition++, MF_BYPOSITION | MF_STRING | (bEnableItem ? MF_ENABLED : MF_GRAYED) | (bEnableOwnerDrawnItem ? MF_OWNERDRAW : 0), - m_uiNextMenuID, wstrItemName.c_str()); + MENUITEMINFO mii = {0}; + mii.cbSize = sizeof(MENUITEMINFO); + mii.fMask = MIIM_ID | MIIM_STATE | MIIM_STRING | MIIM_DATA; + mii.fType = MFT_STRING | (bEnableOwnerDrawnItem ? MFT_OWNERDRAW : 0); + mii.fState = bEnableItem ? MFS_ENABLED : MFS_GRAYED; + mii.wID = m_uiNextMenuID; + mii.dwTypeData = (PTSTR)wstrItemName.c_str(); + mii.dwItemData = CHItemMarker; + mii.cch = 0; + ::InsertMenuItem(hMenu, uiFirstItemPosition++, TRUE, &mii); if(bOverrideDefaultItem && rShellExtData.IsDefaultItem(spMenuItem)) ::SetMenuDefaultItem(hMenu, m_uiNextMenuID, FALSE); Index: src/chext/TContextMenuHandler.h =================================================================== diff -u -r0d5b67ee96b435d63f7bf075dc8e28603793b187 -r250c9a4d3a066af0d40def01e15df5753748aefe --- src/chext/TContextMenuHandler.h (.../TContextMenuHandler.h) (revision 0d5b67ee96b435d63f7bf075dc8e28603793b187) +++ src/chext/TContextMenuHandler.h (.../TContextMenuHandler.h) (revision 250c9a4d3a066af0d40def01e15df5753748aefe) @@ -33,13 +33,17 @@ class TContextMenuHandler { +private: + const unsigned long CHItemMarker = L'C' << 16 | L'H'; + public: TContextMenuHandler(); ~TContextMenuHandler(); void Init(const TShellMenuItemPtr& spRootMenuItem, HMENU hMenu, UINT uiFirstItemID, UINT uiFirstItemPosition, const TShellExtData& rShellExtData, const chengine::TSizeFormatterPtr& spFormatter, bool bShowFreeSpace, bool bEnableOwnerDrawnPaths, bool bOverrideDefaultItem); + bool HasCHItems(HMENU hMenu) const; void Clear(); UINT GetLastCommandID() const { return m_uiNextMenuID; }