Index: svx/source/form/fmundo.cxx
===================================================================
RCS file: /cvs/graphics/svx/source/form/fmundo.cxx,v
retrieving revision 1.31
diff -u -p -r1.31 fmundo.cxx
--- svx/source/form/fmundo.cxx	13 Apr 2005 08:30:13 -0000	1.31
+++ svx/source/form/fmundo.cxx	12 Sep 2005 19:57:22 -0000
@@ -181,6 +181,7 @@
 #include <comphelper/stl_types.hxx>
 #endif
 
+
 using namespace ::com::sun::star::uno;
 using namespace ::com::sun::star::awt;
 using namespace ::com::sun::star::beans;
@@ -192,6 +193,94 @@ using namespace ::com::sun::star::util;
 using namespace ::com::sun::star::form::binding;
 using namespace ::svxform;
 
+
+#ifdef ENABLE_VBA
+
+#include <com/sun/star/script/XScriptListener.hdl>
+#include <comphelper/processfactory.hxx>
+#include <cppuhelper/implbase1.hxx>
+
+
+typedef cppu::WeakImplHelper1< XScriptListener > ScriptEventListener_BASE;
+class ScriptEventListenerWrapper : public ScriptEventListener_BASE
+{
+public:
+	ScriptEventListenerWrapper( FmFormModel& _rModel):pModel(&_rModel)
+	{
+		Reference < XPropertySet > xProps(
+			::comphelper::getProcessServiceFactory(), UNO_QUERY );
+		if ( xProps.is() )
+		{
+			Reference< XComponentContext > xCtx( xProps->getPropertyValue(
+				rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "DefaultContext" ))), UNO_QUERY );
+			if ( xCtx.is() )
+			{
+				Reference< XMultiComponentFactory > xMFac( 
+					xCtx->getServiceManager(), UNO_QUERY );
+				if ( xMFac.is() )
+				{
+					m_vbaListener.set( xMFac->createInstanceWithContext( 
+						rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( 
+							"org.openoffice.vba.EventListener"  ) ), xCtx ), 
+								UNO_QUERY );
+				}
+			}
+		}
+	}
+    // XEventListener
+    virtual void SAL_CALL disposing(const EventObject& Source) throw( RuntimeException ){}
+
+    // XScriptListener
+    virtual void SAL_CALL firing(const  ScriptEvent& evt) throw(RuntimeException)	
+	{
+		setModel();
+		if ( m_vbaListener.is() )
+		{
+			m_vbaListener->firing( evt );	
+		}
+	}
+
+    virtual Any SAL_CALL approveFiring(const ScriptEvent& evt) throw( com::sun::star::reflection::InvocationTargetException, RuntimeException)
+	{
+		setModel();
+		if ( m_vbaListener.is() )
+		{
+			return m_vbaListener->approveFiring( evt );	
+		}
+		return Any();	
+	}
+    
+private:
+	void setModel()
+	{
+		Reference< XPropertySet > xProps( m_vbaListener, UNO_QUERY );
+		if ( xProps.is() )
+		{
+			try
+			{
+				SfxObjectShellRef xObjSh = pModel->GetObjectShell();
+				if ( xObjSh.Is() && m_vbaListener.is() )
+				{
+					Any aVal;
+					aVal <<= xObjSh->GetModel();
+					xProps->setPropertyValue( 
+						::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Model" ) ), 
+						aVal );
+				}
+			}
+			catch( Exception& e )
+			{
+				//swallow any errors
+			}
+		}
+	}
+    FmFormModel* pModel;
+	Reference< XScriptListener > m_vbaListener;
+
+
+};
+#endif // ENABLE_VBA
+
 //------------------------------------------------------------------------------
 // some helper structs for caching property infos
 //------------------------------------------------------------------------------
@@ -232,6 +321,9 @@ FmXUndoEnvironment::FmXUndoEnvironment(F
 				   ,m_pPropertySetCache(NULL)
 {
 	DBG_CTOR(FmXUndoEnvironment,NULL);
+#ifdef ENABLE_VBA
+	m_vbaListener = new ScriptEventListenerWrapper( _rModel );
+#endif
 }
 
 //------------------------------------------------------------------------------
@@ -810,9 +902,19 @@ void FmXUndoEnvironment::switchListening
 	    Reference< XEventAttacherManager > xManager( _rxContainer, UNO_QUERY );
 	    if ( xManager.is() )
             if ( _bStartListening )
+            {
 		        xManager->addScriptListener( this );
+#ifdef ENABLE_VBA
+		        xManager->addScriptListener( m_vbaListener );
+#endif
+            }
             else
+            {
 		        xManager->removeScriptListener( this );
+#ifdef ENABLE_VBA
+		        xManager->removeScriptListener( m_vbaListener );
+#endif
+            }
 
         // also handle all children of this element
 	    sal_uInt32 nCount = _rxContainer->getCount();
@@ -921,7 +1023,15 @@ void FmXUndoEnvironment::firing_Impl( co
 		if (xThis.is())
 		{
 			::rtl::OUString sScriptType = evt.ScriptType;
 			::rtl::OUString sScriptCode = evt.ScriptCode;
+
+#ifdef ENABLE_VBA
+			static const ::rtl::OUString vbaInterOp =
+				::rtl::OUString::createFromAscii("VBAInterop");
+                        if ( sScriptType.equals(vbaInterOp) )
+                            return; // not handled here
+#endif
+
 			Sequence< Any > aArguments = evt.Arguments;
 
 			::rtl::OUString sMacroLocation;
