filter/source/msfilter/msvbahelper.cxx |   13 ++++++-------
 include/oox/ole/vbamodule.hxx          |   13 +++++++++++--
 oox/source/ole/vbamodule.cxx           |   29 +++++++++++++++++++----------
 3 files changed, 36 insertions(+), 19 deletions(-)

New commits:
commit a9a9fc732246a38f0e4e59c7eac3f6243c748af4
Author:     Tomaž Vajngerl <tomaz.vajng...@collabora.co.uk>
AuthorDate: Mon Jan 31 15:35:38 2022 +0900
Commit:     Miklos Vajna <vmik...@collabora.com>
CommitDate: Tue Feb 1 11:24:46 2022 +0100

    vba: fix registering shortcuts keys defined by the vba macros
    
    On issue with registering was that the registering happened when
    the macro source was in the process to be read into the library,
    which is just a bit too early, because the macro wasn't found and
    not registered.
    Another issue was with searching for the macro method (hasMacro),
    which doesn't search the same when the module name is known and
    when it isn't. This was changed so we just iterate through the
    modules and call the same "FindMethod" method without any extra
    restrictions.
    
    Change-Id: I2f4f2f6d8186b289867456ebdccad27ce8eee231
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/129197
    Tested-by: Jenkins CollaboraOffice <jenkinscollaboraoff...@gmail.com>
    Reviewed-by: Miklos Vajna <vmik...@collabora.com>

diff --git a/filter/source/msfilter/msvbahelper.cxx 
b/filter/source/msfilter/msvbahelper.cxx
index 1d1c877c652c..662e001d8683 100644
--- a/filter/source/msfilter/msvbahelper.cxx
+++ b/filter/source/msfilter/msvbahelper.cxx
@@ -200,18 +200,17 @@ static bool hasMacro( SfxObjectShell const * pShell, 
const OUString& sLibrary, O
                     SbModule* pModule = pBasic->FindModule( sMod );
                     if ( pModule && pModule->FindMethod( sMacro, 
SbxClassType::Method ))
                     {
-                        bFound = true;
+                        return true;
                     }
                 }
-                else if( SbMethod* pMethod = dynamic_cast< SbMethod* >( 
pBasic->Find( sMacro, SbxClassType::Method ) ) )
+                else
                 {
-                    if( SbModule* pModule = pMethod->GetModule() )
+                    for (auto const& rModuleRef : pBasic->GetModules())
                     {
-                        // when searching for a macro without module name, do 
not search in class/document/form modules
-                        if( pModule->GetModuleType() == 
script::ModuleType::NORMAL )
+                        if (rModuleRef && rModuleRef->FindMethod(sMacro, 
SbxClassType::Method))
                         {
-                            sMod = pModule->GetName();
-                            bFound = true;
+                            sMod = rModuleRef->GetName();
+                            return true;
                         }
                     }
                 }
diff --git a/include/oox/ole/vbamodule.hxx b/include/oox/ole/vbamodule.hxx
index 85f1f3e37103..70c522b50025 100644
--- a/include/oox/ole/vbamodule.hxx
+++ b/include/oox/ole/vbamodule.hxx
@@ -24,6 +24,7 @@
 #include <rtl/textenc.h>
 #include <rtl/ustring.hxx>
 #include <sal/types.h>
+#include <vector>
 
 namespace com::sun::star {
     namespace container { class XNameAccess; }
@@ -39,6 +40,11 @@ namespace oox {
 
 namespace oox::ole {
 
+struct VbaKeyBinding
+{
+    OUString msApiKey;
+    OUString msMethodName;
+};
 
 class VbaModule
 {
@@ -67,15 +73,17 @@ public:
     void                createAndImportModule(
                             StorageBase& rVbaStrg,
                             const css::uno::Reference< 
css::container::XNameContainer >& rxBasicLib,
-                            const css::uno::Reference< 
css::container::XNameAccess >& rxDocObjectNA ) const;
+                            const css::uno::Reference< 
css::container::XNameAccess >& rxDocObjectNA );
     /** Creates an empty Basic module in the passed Basic library. */
     void                createEmptyModule(
                             const css::uno::Reference< 
css::container::XNameContainer >& rxBasicLib,
                             const css::uno::Reference< 
css::container::XNameAccess >& rxDocObjectNA ) const;
 
+    void registerShortcutKeys();
+
 private:
     /** Reads and returns the VBA source code from the passed storage. */
-    OUString     readSourceCode( StorageBase& rVbaStrg ) const;
+    OUString     readSourceCode( StorageBase& rVbaStrg );
 
     /** Creates a new Basic module and inserts it into the passed Basic 
library. */
     void                createModule(
@@ -97,6 +105,7 @@ private:
     bool                mbReadOnly;
     bool                mbPrivate;
     bool                mbExecutable;
+    std::vector<VbaKeyBinding> maKeyBindings;
 };
 
 
diff --git a/oox/source/ole/vbamodule.cxx b/oox/source/ole/vbamodule.cxx
index 7e913124730b..5b055b52f745 100644
--- a/oox/source/ole/vbamodule.cxx
+++ b/oox/source/ole/vbamodule.cxx
@@ -127,10 +127,26 @@ void VbaModule::importDirRecords( BinaryInputStream& 
rDirStrm )
 
 void VbaModule::createAndImportModule( StorageBase& rVbaStrg,
                                        const Reference< 
container::XNameContainer >& rxBasicLib,
-                                       const Reference< container::XNameAccess 
>& rxDocObjectNA ) const
+                                       const Reference< container::XNameAccess 
>& rxDocObjectNA )
 {
     OUString aVBASourceCode = readSourceCode( rVbaStrg );
     createModule( aVBASourceCode, rxBasicLib, rxDocObjectNA );
+    registerShortcutKeys();
+}
+
+void VbaModule::registerShortcutKeys()
+{
+    for (VbaKeyBinding const& rKeyBinding : maKeyBindings)
+    {
+        try
+        {
+            KeyEvent aKeyEvent = ooo::vba::parseKeyEvent(rKeyBinding.msApiKey);
+            ooo::vba::applyShortCutKeyBinding(mxDocModel, aKeyEvent, 
rKeyBinding.msMethodName);
+        }
+        catch (const Exception&)
+        {
+        }
+    }
 }
 
 void VbaModule::createEmptyModule( const Reference< container::XNameContainer 
>& rxBasicLib,
@@ -139,7 +155,7 @@ void VbaModule::createEmptyModule( const Reference< 
container::XNameContainer >&
     createModule( OUString(), rxBasicLib, rxDocObjectNA );
 }
 
-OUString VbaModule::readSourceCode( StorageBase& rVbaStrg ) const
+OUString VbaModule::readSourceCode( StorageBase& rVbaStrg )
 {
     OUStringBuffer aSourceCode(512);
     static const char sUnmatchedRemovedTag[] = "Rem removed unmatched Sub/End: 
";
@@ -189,14 +205,7 @@ OUString VbaModule::readSourceCode( StorageBase& rVbaStrg 
) const
                             // cntrl modifier is explicit ( but could be 
cntrl+shift ), parseKeyEvent
                             // will handle and uppercase letter appropriately
                             OUString sApiKey = "^" + sKey;
-                            try
-                            {
-                                KeyEvent aKeyEvent = ooo::vba::parseKeyEvent( 
sApiKey );
-                                ooo::vba::applyShortCutKeyBinding( mxDocModel, 
aKeyEvent, sProc );
-                            }
-                            catch (const Exception&)
-                            {
-                            }
+                            maKeyBindings.push_back({sApiKey, sProc});
                         }
                     }
                 }

Reply via email to