@@ -716,11 +716,52 @@ void MenuManager::deleteInstance()
716716}
717717
718718
719- int MenuManager::findPluginCommand (const TCHAR *pluginName, const TCHAR *menuOption)
719+ int MenuManager::findPluginCommand (const TCHAR *pluginName, const TCHAR *menuOption, bool refreshCache )
720720{
721+ int retVal = 0 ;
722+ bool fromCache = false ;
723+ if (!refreshCache)
724+ {
725+ MenuCommandCacheTD::iterator it = m_pluginCommandCache.find (pair<tstring, tstring>(tstring (pluginName), tstring (menuOption)));
726+ if (it != m_pluginCommandCache.end ())
727+ {
728+ retVal = it->second ;
729+ fromCache = true ;
730+ }
731+ }
732+
733+ if (0 == retVal)
734+ {
735+ HMENU hPluginMenu = (HMENU)::SendMessage (m_hNotepad, NPPM_GETMENUHANDLE, 0 , 0 );
721736
722- HMENU hPluginMenu = (HMENU)::SendMessage (m_hNotepad, NPPM_GETMENUHANDLE, 0 , 0 );
723-
737+ int iMenuItems = GetMenuItemCount (hPluginMenu);
738+ TCHAR strBuffer[500 ];
739+ for ( int i = 0 ; i < iMenuItems; ++i )
740+ {
741+ MENUITEMINFO mii;
742+ mii.cbSize = sizeof (MENUITEMINFO);
743+ mii.fMask = MIIM_ID | MIIM_STRING | MIIM_SUBMENU;
744+ mii.cch = 500 ;
745+ mii.dwTypeData = strBuffer;
746+
747+ ::GetMenuItemInfo (hPluginMenu, i, TRUE , &mii);
748+ tstring thisMenuName = formatMenuName (strBuffer);
749+ if (NULL != mii.hSubMenu && 0 == _tcsicmp (pluginName, thisMenuName.c_str ()))
750+ {
751+ retVal = findMenuCommand (mii.hSubMenu , NULL , menuOption);
752+ break ;
753+ }
754+
755+ }
756+ }
757+
758+ if (!fromCache && retVal != 0 )
759+ {
760+ m_pluginCommandCache[pair<tstring,tstring>(tstring (pluginName), tstring (menuOption))] = retVal;
761+ }
762+
763+ return retVal;
764+ /*
724765 int iMenuItems = GetMenuItemCount(hPluginMenu);
725766 TCHAR strBuffer[500];
726767
@@ -752,17 +793,45 @@ int MenuManager::findPluginCommand(const TCHAR *pluginName, const TCHAR *menuOpt
752793 }
753794
754795 return 0;
796+ */
755797}
756798
757799
758800
759- int MenuManager::findMenuCommand (const TCHAR *menuName, const TCHAR *menuOption)
801+ int MenuManager::findMenuCommand (const TCHAR *menuName, const TCHAR *menuOption, bool refreshCache )
760802{
803+ int retVal = 0 ;
804+
805+ if (!refreshCache)
806+ {
807+ MenuCommandCacheTD::iterator it = m_menuCommandCache.find (pair<tstring, tstring>(tstring (menuName), tstring (menuOption)));
808+ if (it != m_menuCommandCache.end ())
809+ {
810+ return it->second ;
811+ }
812+ }
813+
761814 HMENU hMenuBar = ::GetMenu (m_hNotepad);
815+ retVal = findMenuCommand (hMenuBar, menuName, menuOption);
816+
817+ if (retVal != 0 )
818+ {
819+ m_menuCommandCache[pair<tstring,tstring>(tstring (menuName), tstring (menuOption))] = retVal;
820+ }
762821
763- return findMenuCommand (hMenuBar, menuName, menuOption) ;
822+ return retVal ;
764823}
765824
825+ tstring MenuManager::formatMenuName (const TCHAR *name)
826+ {
827+ tstring nameStr (name);
828+ size_t pos = nameStr.find (_T (' &' ));
829+ if (pos != tstring::npos && pos < nameStr.size () - 1 && nameStr[pos + 1 ] != _T (' &' ))
830+ {
831+ nameStr.erase (pos, 1 );
832+ }
833+ return nameStr;
834+ }
766835
767836int MenuManager::findMenuCommand (HMENU hParentMenu, const TCHAR *menuName, const TCHAR *menuOption)
768837{
@@ -780,25 +849,30 @@ int MenuManager::findMenuCommand(HMENU hParentMenu, const TCHAR *menuName, const
780849 mii.dwTypeData = strBuffer;
781850
782851 ::GetMenuItemInfo (hParentMenu, i, TRUE , &mii);
783-
784- if (NULL != mii.hSubMenu && 0 == _tcsicmp (menuName, strBuffer ))
852+ tstring thisMenuName = formatMenuName (strBuffer);
853+ if (NULL != mii.hSubMenu && ( NULL == menuName || 0 == _tcsicmp (menuName, thisMenuName. c_str ()) ))
785854 {
786855 int subMenuItems = ::GetMenuItemCount (mii.hSubMenu );
787856 for (int subMenuPos = 0 ; subMenuPos < subMenuItems; ++subMenuPos)
788857 {
789858 TCHAR *context = NULL ;;
790859 ::GetMenuString (mii.hSubMenu, subMenuPos, strBuffer, 500 , MF_BYPOSITION);
791860 TCHAR *name = _tcstok_s (strBuffer, _T (" \t " ), &context);
792-
793- if (name && 0 == _tcsicmp (menuOption, name))
861+ if (name)
794862 {
795- return ::GetMenuItemID (mii.hSubMenu , subMenuPos);
863+ tstring nameStr = formatMenuName (name);
864+
865+ if (0 == _tcsicmp (menuOption, nameStr.c_str ()))
866+ {
867+ return ::GetMenuItemID (mii.hSubMenu , subMenuPos);
868+ }
796869 }
797870 }
798871 }
799872
800873 if (NULL != mii.hSubMenu )
801874 {
875+
802876 retVal = findMenuCommand (mii.hSubMenu , menuName, menuOption);
803877 // If we've found it in the sub menu (or within the sub menu)
804878 if (0 != retVal)
@@ -811,6 +885,9 @@ int MenuManager::findMenuCommand(HMENU hParentMenu, const TCHAR *menuName, const
811885
812886}
813887
888+
889+
890+
814891void MenuManager::initPreviousScript ()
815892{
816893 ShortcutKey key;
0 commit comments