[Libreoffice-commits] .: 136 commits - sc/source

Jan Holesovsky kendy at kemper.freedesktop.org
Fri May 27 12:15:13 PDT 2011


 sc/source/ui/vba/vbaapplication.cxx  |    6 
 sc/source/ui/vba/vbaeventshelper.cxx |  618 +++++++++++++++++++++--------------
 sc/source/ui/vba/vbaeventshelper.hxx |   21 -
 sc/source/ui/vba/vbawindow.cxx       |  223 +++++++-----
 sc/source/ui/vba/vbawindow.hxx       |   26 +
 sc/source/ui/vba/vbawindows.cxx      |   15 
 sc/source/ui/vba/vbawindows.hxx      |    2 
 7 files changed, 549 insertions(+), 362 deletions(-)

New commits:
commit e9283ac921a2fe8620d8fb352cf19e367275be36
Merge: 7c6d6d5... a48c6d3...
Author: Jan Holesovsky <kendy at suse.cz>
Date:   Fri May 27 20:49:22 2011 +0200

    Merge branch 'master' of git://anongit.freedesktop.org/libreoffice/calc

commit 7c6d6d5d0d30b8e1c1d3a697f4b685ff1ba82c1a
Merge: 4a99f31... 485c421...
Author: Jan Holesovsky <kendy at suse.cz>
Date:   Tue May 24 15:03:17 2011 +0200

    Merge remote-tracking branch 'origin/integration/dev300_m106'

commit 485c421ccf3f17bd6669eba5b7c44bb84f54a6f0
Author: Kohei Yoshida <kyoshida at novell.com>
Date:   Mon May 23 23:44:53 2011 -0400

    Got it to build. Mostly VBA stuff but no idea what kind of shape it is in.

diff --git a/sc/source/ui/vba/vbaapplication.cxx b/sc/source/ui/vba/vbaapplication.cxx
index ba27f71..328e1b0 100644
--- a/sc/source/ui/vba/vbaapplication.cxx
+++ b/sc/source/ui/vba/vbaapplication.cxx
@@ -458,10 +458,10 @@ ScVbaApplication::Dialogs( const uno::Any &aIndex ) throw (uno::RuntimeException
 uno::Reference< excel::XWindow > SAL_CALL
 ScVbaApplication::getActiveWindow() throw (uno::RuntimeException)
 {
-    // #FIXME sofar can't determine Parent
     uno::Reference< frame::XModel > xModel = getCurrentDocument();
-    ScVbaWindow* pWin = new ScVbaWindow( uno::Reference< XHelperInterface >(), mxContext, xModel );
-    uno::Reference< excel::XWindow > xWin( pWin );
+    uno::Reference< frame::XController > xController( xModel->getCurrentController(), uno::UNO_SET_THROW );
+    uno::Reference< XHelperInterface > xParent( getActiveWorkbook(), uno::UNO_QUERY_THROW );
+    uno::Reference< excel::XWindow > xWin( new ScVbaWindow( xParent, mxContext, xModel, xController ) );
     return xWin;
 }
 
diff --git a/sc/source/ui/vba/vbaeventshelper.cxx b/sc/source/ui/vba/vbaeventshelper.cxx
index b140a1f..2d36254 100644
--- a/sc/source/ui/vba/vbaeventshelper.cxx
+++ b/sc/source/ui/vba/vbaeventshelper.cxx
@@ -28,21 +28,22 @@
 
 #include "vbaeventshelper.hxx"
 
+#include <com/sun/star/awt/XTopWindow.hpp>
+#include <com/sun/star/awt/XTopWindowListener.hpp>
 #include <com/sun/star/awt/XWindowListener.hpp>
 #include <com/sun/star/frame/XBorderResizeListener.hpp>
 #include <com/sun/star/frame/XControllerBorder.hpp>
+#include <com/sun/star/script/ModuleType.hpp>
 #include <com/sun/star/script/vba/VBAEventId.hpp>
 #include <com/sun/star/sheet/XCellRangeAddressable.hpp>
 #include <com/sun/star/sheet/XSheetCellRangeContainer.hpp>
 #include <com/sun/star/table/XCellRange.hpp>
 #include <com/sun/star/util/XChangesListener.hpp>
 #include <com/sun/star/util/XChangesNotifier.hpp>
-#include <com/sun/star/util/XCloseListener.hpp>
-
-#include <ooo/vba/excel/XApplication.hpp>
 
 #include <cppuhelper/implbase4.hxx>
 #include <toolkit/unohlp.hxx>
+#include <unotools/eventcfg.hxx>
 #include <vbahelper/helperdecl.hxx>
 #include <vcl/svapp.hxx>
 #include <vcl/window.hxx>
@@ -55,6 +56,8 @@ using namespace ::com::sun::star;
 using namespace ::com::sun::star::script::vba::VBAEventId;
 using namespace ::ooo::vba;
 
+using ::rtl::OUString;
+
 // ============================================================================
 
 namespace {
@@ -66,9 +69,13 @@ SCTAB lclGetTabFromArgs( const uno::Sequence< uno::Any >& rArgs, sal_Int32 nInde
     VbaEventsHelperBase::checkArgument( rArgs, nIndex );
 
     // first try to extract a sheet index
-    SCTAB nTab = -1;
+    sal_Int32 nTab = -1;
     if( rArgs[ nIndex ] >>= nTab )
-        return nTab;
+    {
+        if( (nTab < 0) || (nTab > MAXTAB) )
+            throw lang::IllegalArgumentException();
+        return static_cast< SCTAB >( nTab );
+    }
 
     // try VBA Range object
     uno::Reference< excel::XRange > xVbaRange = getXSomethingFromArgs< excel::XRange >( rArgs, nIndex );
@@ -98,232 +105,271 @@ SCTAB lclGetTabFromArgs( const uno::Sequence< uno::Any >& rArgs, sal_Int32 nInde
     throw lang::IllegalArgumentException();
 }
 
+/** Returns the AWT container window of the passed controller. */
+uno::Reference< awt::XWindow > lclGetWindowForController( const uno::Reference< frame::XController >& rxController )
+{
+    if( rxController.is() ) try
+    {
+        uno::Reference< frame::XFrame > xFrame( rxController->getFrame(), uno::UNO_SET_THROW );
+        return xFrame->getContainerWindow();
+    }
+    catch( uno::Exception& )
+    {
+    }
+    return 0;
+}
+
 } // namespace
 
 // ============================================================================
 
-typedef ::cppu::WeakImplHelper4<
-    awt::XWindowListener, util::XCloseListener, frame::XBorderResizeListener, util::XChangesListener > ScVbaEventsListener_BASE;
+typedef ::cppu::WeakImplHelper4< awt::XTopWindowListener, awt::XWindowListener, frame::XBorderResizeListener, util::XChangesListener > ScVbaEventListener_BASE;
 
 // This class is to process Workbook window related event
-class ScVbaEventsListener : public ScVbaEventsListener_BASE
+class ScVbaEventListener : public ScVbaEventListener_BASE
 {
 public :
-    ScVbaEventsListener( ScVbaEventsHelper& rVbaEvents, const uno::Reference< frame::XModel >& rxModel, ScDocShell* pDocShell );
-    virtual ~ScVbaEventsListener();
-
-    void startListening();
-    void stopListening();
+    ScVbaEventListener( ScVbaEventsHelper& rVbaEvents, const uno::Reference< frame::XModel >& rxModel, ScDocShell* pDocShell );
+    virtual ~ScVbaEventListener();
+
+    /** Starts listening to the passed document controller. */
+    void startControllerListening( const uno::Reference< frame::XController >& rxController );
+    /** Stops listening to the passed document controller. */
+    void stopControllerListening( const uno::Reference< frame::XController >& rxController );
+
+    // XTopWindowListener
+    virtual void SAL_CALL windowOpened( const lang::EventObject& rEvent ) throw (uno::RuntimeException);
+    virtual void SAL_CALL windowClosing( const lang::EventObject& rEvent ) throw (uno::RuntimeException);
+    virtual void SAL_CALL windowClosed( const lang::EventObject& rEvent ) throw (uno::RuntimeException);
+    virtual void SAL_CALL windowMinimized( const lang::EventObject& rEvent ) throw (uno::RuntimeException);
+    virtual void SAL_CALL windowNormalized( const lang::EventObject& rEvent ) throw (uno::RuntimeException);
+    virtual void SAL_CALL windowActivated( const lang::EventObject& rEvent ) throw (uno::RuntimeException);
+    virtual void SAL_CALL windowDeactivated( const lang::EventObject& rEvent ) throw (uno::RuntimeException);
 
     // XWindowListener
-    virtual void SAL_CALL windowResized( const awt::WindowEvent& aEvent ) throw (uno::RuntimeException);
-    virtual void SAL_CALL windowMoved( const awt::WindowEvent& aEvent ) throw (uno::RuntimeException);
-    virtual void SAL_CALL windowShown( const lang::EventObject& aEvent ) throw (uno::RuntimeException);
-    virtual void SAL_CALL windowHidden( const lang::EventObject& aEvent ) throw (uno::RuntimeException);
-    virtual void SAL_CALL disposing( const lang::EventObject& aEvent ) throw (uno::RuntimeException);
-
-    // XCloseListener
-    virtual void SAL_CALL queryClosing( const lang::EventObject& Source, ::sal_Bool GetsOwnership ) throw (util::CloseVetoException, uno::RuntimeException);
-    virtual void SAL_CALL notifyClosing( const lang::EventObject& Source ) throw (uno::RuntimeException);
+    virtual void SAL_CALL windowResized( const awt::WindowEvent& rEvent ) throw (uno::RuntimeException);
+    virtual void SAL_CALL windowMoved( const awt::WindowEvent& rEvent ) throw (uno::RuntimeException);
+    virtual void SAL_CALL windowShown( const lang::EventObject& rEvent ) throw (uno::RuntimeException);
+    virtual void SAL_CALL windowHidden( const lang::EventObject& rEvent ) throw (uno::RuntimeException);
 
     // XBorderResizeListener
-    virtual void SAL_CALL borderWidthsChanged( const uno::Reference< uno::XInterface >& aObject, const frame::BorderWidths& aNewSize ) throw (uno::RuntimeException);
+    virtual void SAL_CALL borderWidthsChanged( const uno::Reference< uno::XInterface >& rSource, const frame::BorderWidths& aNewSize ) throw (uno::RuntimeException);
 
     // XChangesListener
-    virtual void SAL_CALL changesOccurred( const util::ChangesEvent& aEvent ) throw (uno::RuntimeException);
+    virtual void SAL_CALL changesOccurred( const util::ChangesEvent& rEvent ) throw (uno::RuntimeException);
+
+    // XEventListener
+    virtual void SAL_CALL disposing( const lang::EventObject& rEvent ) throw (uno::RuntimeException);
 
 private:
-    uno::Reference< frame::XFrame > getFrame();
-    uno::Reference< awt::XWindow > getContainerWindow();
-    bool isMouseReleased();
-    DECL_LINK( fireResizeMacro, void* );
-    void processWindowResizeMacro();
+    /** Starts listening to the document model. */
+    void startModelListening();
+    /** Stops listening to the document model. */
+    void stopModelListening();
+
+    /** Returns the controller for the passed VCL window. */
+    uno::Reference< frame::XController > getControllerForWindow( Window* pWindow ) const;
+
+    /** Calls the Workbook_Window[Activate|Deactivate] event handler. */
+    void processWindowActivateEvent( Window* pWindow, bool bActivate );
+    /** Posts a Workbook_WindowResize user event. */
+    void postWindowResizeEvent( Window* pWindow );
+    /** Callback link for Application::PostUserEvent(). */
+    DECL_LINK( processWindowResizeEvent, Window* );
 
 private:
-    ::osl::Mutex maMutex;
-    ScVbaEventsHelper& mrVbaEvents;
+    typedef ::std::map< Window*, uno::Reference< frame::XController > > WindowControllerMap;
+
+    ::osl::Mutex        maMutex;
+    ScVbaEventsHelper&  mrVbaEvents;
     uno::Reference< frame::XModel > mxModel;
-    ScDocShell* mpDocShell;
-    bool mbWindowResized;
-    bool mbBorderChanged;
-    bool mbDisposed;
+    ScDocShell*         mpDocShell;
+    WindowControllerMap maControllers;          /// Maps VCL top windows to their controllers.
+    Window*             mpActiveWindow;         /// Currently activated window, to prevent multiple (de)activation.
+    bool                mbWindowResized;        /// True = window resize system event processed.
+    bool                mbBorderChanged;        /// True = borders changed system event processed.
+    bool                mbDisposed;
 };
 
 // ----------------------------------------------------------------------------
 
-ScVbaEventsListener::ScVbaEventsListener( ScVbaEventsHelper& rVbaEvents, const uno::Reference< frame::XModel >& rxModel, ScDocShell* pDocShell ) :
+ScVbaEventListener::ScVbaEventListener( ScVbaEventsHelper& rVbaEvents, const uno::Reference< frame::XModel >& rxModel, ScDocShell* pDocShell ) :
     mrVbaEvents( rVbaEvents ),
     mxModel( rxModel ),
     mpDocShell( pDocShell ),
+    mpActiveWindow( 0 ),
     mbWindowResized( false ),
     mbBorderChanged( false ),
     mbDisposed( !rxModel.is() )
 {
-    OSL_TRACE( "ScVbaEventsListener::ScVbaEventsListener( 0x%x ) - ctor ", this );
+    if( !mxModel.is() )
+        return;
+
+    startModelListening();
+    try
+    {
+        uno::Reference< frame::XController > xController( mxModel->getCurrentController(), uno::UNO_QUERY_THROW );
+        startControllerListening( xController );
+    }
+    catch( uno::Exception& )
+    {
+    }
 }
 
-ScVbaEventsListener::~ScVbaEventsListener()
+ScVbaEventListener::~ScVbaEventListener()
 {
-    OSL_TRACE( "ScVbaEventsListener::~ScVbaEventsListener( 0x%x ) - dtor ", this );
-    stopListening();
 }
 
-void ScVbaEventsListener::startListening()
+void ScVbaEventListener::startControllerListening( const uno::Reference< frame::XController >& rxController )
 {
-    if( !mbDisposed )
+    ::osl::MutexGuard aGuard( maMutex );
+
+    uno::Reference< awt::XWindow > xWindow = lclGetWindowForController( rxController );
+    if( xWindow.is() )
+        try { xWindow->addWindowListener( this ); } catch( uno::Exception& ) {}
+
+    uno::Reference< awt::XTopWindow > xTopWindow( xWindow, uno::UNO_QUERY );
+    if( xTopWindow.is() )
+        try { xTopWindow->addTopWindowListener( this ); } catch( uno::Exception& ) {}
+
+    uno::Reference< frame::XControllerBorder > xControllerBorder( rxController, uno::UNO_QUERY );
+    if( xControllerBorder.is() )
+        try { xControllerBorder->addBorderResizeListener( this ); } catch( uno::Exception& ) {}
+
+    if( Window* pWindow = VCLUnoHelper::GetWindow( xWindow ) )
+        maControllers[ pWindow ] = rxController;
+}
+
+void ScVbaEventListener::stopControllerListening( const uno::Reference< frame::XController >& rxController )
+{
+    ::osl::MutexGuard aGuard( maMutex );
+
+    uno::Reference< awt::XWindow > xWindow = lclGetWindowForController( rxController );
+    if( xWindow.is() )
+        try { xWindow->removeWindowListener( this ); } catch( uno::Exception& ) {}
+
+    uno::Reference< awt::XTopWindow > xTopWindow( xWindow, uno::UNO_QUERY );
+    if( xTopWindow.is() )
+        try { xTopWindow->removeTopWindowListener( this ); } catch( uno::Exception& ) {}
+
+    uno::Reference< frame::XControllerBorder > xControllerBorder( rxController, uno::UNO_QUERY );
+    if( xControllerBorder.is() )
+        try { xControllerBorder->removeBorderResizeListener( this ); } catch( uno::Exception& ) {}
+
+    if( Window* pWindow = VCLUnoHelper::GetWindow( xWindow ) )
     {
-        // add window listener
-        try
-        {
-            uno::Reference< awt::XWindow > xWindow( getContainerWindow(), uno::UNO_QUERY_THROW );
-            xWindow->addWindowListener( this );
-        }
-        catch( uno::Exception& )
-        {
-        }
-        // add close listener
-        try
-        {
-            uno::Reference< util::XCloseBroadcaster > xCloseBroadcaster( mxModel, uno::UNO_QUERY_THROW );
-            xCloseBroadcaster->addCloseListener( this );
-        }
-        catch( uno::Exception& )
-        {
-        }
-        // add Border resize listener
-        try
-        {
-            uno::Reference< frame::XControllerBorder > xControllerBorder( mxModel->getCurrentController(), uno::UNO_QUERY_THROW );
-            xControllerBorder->addBorderResizeListener( this );
-        }
-        catch( uno::Exception& )
-        {
-        }
-        // add content change listener
-        try
-        {
-            uno::Reference< util::XChangesNotifier > xChangesNotifier( mxModel, uno::UNO_QUERY_THROW );
-            xChangesNotifier->addChangesListener( this );
-        }
-        catch( uno::Exception& )
-        {
-        }
+        maControllers.erase( pWindow );
+        if( pWindow == mpActiveWindow )
+            mpActiveWindow = 0;
     }
 }
 
-void ScVbaEventsListener::stopListening()
+void SAL_CALL ScVbaEventListener::windowOpened( const lang::EventObject& /*rEvent*/ ) throw (uno::RuntimeException)
+{
+}
+
+void SAL_CALL ScVbaEventListener::windowClosing( const lang::EventObject& /*rEvent*/ ) throw (uno::RuntimeException)
+{
+}
+
+void SAL_CALL ScVbaEventListener::windowClosed( const lang::EventObject& /*rEvent*/ ) throw (uno::RuntimeException)
+{
+}
+
+void SAL_CALL ScVbaEventListener::windowMinimized( const lang::EventObject& /*rEvent*/ ) throw (uno::RuntimeException)
 {
+}
+
+void SAL_CALL ScVbaEventListener::windowNormalized( const lang::EventObject& /*rEvent*/ ) throw (uno::RuntimeException)
+{
+}
+
+void SAL_CALL ScVbaEventListener::windowActivated( const lang::EventObject& rEvent ) throw (uno::RuntimeException)
+{
+    ::osl::MutexGuard aGuard( maMutex );
+
     if( !mbDisposed )
     {
-        try
-        {
-            uno::Reference< awt::XWindow > xWindow( getContainerWindow(), uno::UNO_QUERY_THROW );
-            xWindow->removeWindowListener( this );
-        }
-        catch( uno::Exception& )
-        {
-        }
-        try
-        {
-            uno::Reference< util::XCloseBroadcaster > xCloseBroadcaster( mxModel, uno::UNO_QUERY_THROW );
-            xCloseBroadcaster->removeCloseListener( this );
-        }
-        catch( uno::Exception& )
-        {
-        }
-        try
-        {
-            uno::Reference< frame::XControllerBorder > xControllerBorder( mxModel->getCurrentController(), uno::UNO_QUERY_THROW );
-            xControllerBorder->removeBorderResizeListener( this );
-        }
-        catch( uno::Exception& )
-        {
-        }
-        try
-        {
-            uno::Reference< util::XChangesNotifier > xChangesNotifier( mxModel, uno::UNO_QUERY_THROW );
-            xChangesNotifier->removeChangesListener( this );
-        }
-        catch( uno::Exception& )
+        uno::Reference< awt::XWindow > xWindow( rEvent.Source, uno::UNO_QUERY );
+        Window* pWindow = VCLUnoHelper::GetWindow( xWindow );
+        OSL_TRACE( "ScVbaEventListener::windowActivated - pWindow = 0x%x, mpActiveWindow = 0x%x", pWindow, mpActiveWindow );
+        // do not fire activation event multiple time for the same window
+        if( pWindow && (pWindow != mpActiveWindow) )
         {
+            // if another window is active, fire deactivation event first
+            if( mpActiveWindow )
+                processWindowActivateEvent( mpActiveWindow, false );
+            // fire activation event for the new window
+            processWindowActivateEvent( pWindow, true );
+            mpActiveWindow = pWindow;
         }
     }
-    mbDisposed = true;
 }
 
-void SAL_CALL ScVbaEventsListener::windowResized(  const awt::WindowEvent& /*aEvent*/ ) throw ( uno::RuntimeException )
+void SAL_CALL ScVbaEventListener::windowDeactivated( const lang::EventObject& rEvent ) throw (uno::RuntimeException)
 {
     ::osl::MutexGuard aGuard( maMutex );
-    // Workbook_window_resize event
-    mbWindowResized = true;
-    if( !mbDisposed && mbBorderChanged )
+
+    if( !mbDisposed )
     {
-        if( /*Window* pWindow =*/ VCLUnoHelper::GetWindow( getContainerWindow() ) )
-        {
-            mbBorderChanged = mbWindowResized = false;
-            acquire(); // ensure we don't get deleted before the event is handled
-            Application::PostUserEvent( LINK( this, ScVbaEventsListener, fireResizeMacro ), 0 );
-        }
+        uno::Reference< awt::XWindow > xWindow( rEvent.Source, uno::UNO_QUERY );
+        Window* pWindow = VCLUnoHelper::GetWindow( xWindow );
+        OSL_TRACE( "ScVbaEventListener::windowDeactivated - pWindow = 0x%x, mpActiveWindow = 0x%x", pWindow, mpActiveWindow );
+        // do not fire the deactivation event, if the window is not active (prevent multiple deactivation)
+        if( pWindow && (pWindow == mpActiveWindow) )
+            processWindowActivateEvent( pWindow, false );
+        // forget pointer to the active window
+        mpActiveWindow = 0;
     }
 }
 
-void SAL_CALL ScVbaEventsListener::windowMoved( const awt::WindowEvent& /*aEvent*/ ) throw ( uno::RuntimeException )
+void SAL_CALL ScVbaEventListener::windowResized( const awt::WindowEvent& rEvent ) throw (uno::RuntimeException)
 {
-    // not interest this time
-}
+    ::osl::MutexGuard aGuard( maMutex );
 
-void SAL_CALL ScVbaEventsListener::windowShown( const lang::EventObject& /*aEvent*/ ) throw ( uno::RuntimeException )
-{
-    // not interest this time
+    mbWindowResized = true;
+    if( !mbDisposed && mbBorderChanged )
+    {
+        uno::Reference< awt::XWindow > xWindow( rEvent.Source, uno::UNO_QUERY );
+        postWindowResizeEvent( VCLUnoHelper::GetWindow( xWindow ) );
+    }
 }
 
-void SAL_CALL ScVbaEventsListener::windowHidden( const lang::EventObject& /*aEvent*/ ) throw ( uno::RuntimeException )
+void SAL_CALL ScVbaEventListener::windowMoved( const awt::WindowEvent& /*rEvent*/ ) throw (uno::RuntimeException)
 {
-    // not interest this time
 }
 
-void SAL_CALL ScVbaEventsListener::disposing( const lang::EventObject& /*aEvent*/ ) throw ( uno::RuntimeException )
+void SAL_CALL ScVbaEventListener::windowShown( const lang::EventObject& /*rEvent*/ ) throw (uno::RuntimeException)
 {
-    ::osl::MutexGuard aGuard( maMutex );
-    OSL_TRACE( "ScVbaEventsListener::disposing( 0x%x )", this );
-    mbDisposed = true;
 }
 
-void SAL_CALL ScVbaEventsListener::queryClosing( const lang::EventObject& /*Source*/, sal_Bool /*GetsOwnership*/ ) throw (util::CloseVetoException, uno::RuntimeException)
+void SAL_CALL ScVbaEventListener::windowHidden( const lang::EventObject& /*rEvent*/ ) throw (uno::RuntimeException)
 {
-     // it can cancel the close, but need to throw a CloseVetoException, and it will be transmit to caller.
 }
 
-void SAL_CALL ScVbaEventsListener::notifyClosing( const lang::EventObject& /*Source*/ ) throw (uno::RuntimeException)
+void SAL_CALL ScVbaEventListener::borderWidthsChanged( const uno::Reference< uno::XInterface >& rSource, const frame::BorderWidths& /*aNewSize*/ ) throw (uno::RuntimeException)
 {
     ::osl::MutexGuard aGuard( maMutex );
-    stopListening();
-}
 
-void SAL_CALL ScVbaEventsListener::borderWidthsChanged( const uno::Reference< uno::XInterface >& /*aObject*/, const frame::BorderWidths& /*aNewSize*/ ) throw (uno::RuntimeException)
-{
-    ::osl::MutexGuard aGuard( maMutex );
-    // work with WindowResized event to guard Window Resize event.
     mbBorderChanged = true;
     if( !mbDisposed && mbWindowResized )
     {
-        if( /*Window* pWindow =*/ VCLUnoHelper::GetWindow( getContainerWindow() ) )
-        {
-            mbWindowResized = mbBorderChanged = false;
-            acquire(); // ensure we don't get deleted before the timer fires.
-            Application::PostUserEvent( LINK( this, ScVbaEventsListener, fireResizeMacro ), 0 );
-        }
+        uno::Reference< frame::XController > xController( rSource, uno::UNO_QUERY );
+        uno::Reference< awt::XWindow > xWindow = lclGetWindowForController( xController );
+        postWindowResizeEvent( VCLUnoHelper::GetWindow( xWindow ) );
     }
 }
 
-void SAL_CALL ScVbaEventsListener::changesOccurred( const util::ChangesEvent& aEvent ) throw (uno::RuntimeException)
+void SAL_CALL ScVbaEventListener::changesOccurred( const util::ChangesEvent& rEvent ) throw (uno::RuntimeException)
 {
-    sal_Int32 nCount = aEvent.Changes.getLength();
-    if( nCount == 0 )
+    ::osl::MutexGuard aGuard( maMutex );
+
+    sal_Int32 nCount = rEvent.Changes.getLength();
+    if( mbDisposed || !mpDocShell || (nCount == 0) )
         return;
 
-    util::ElementChange aChange = aEvent.Changes[ 0 ];
-    rtl::OUString sOperation;
+    util::ElementChange aChange = rEvent.Changes[ 0 ];
+    OUString sOperation;
     aChange.Accessor >>= sOperation;
     if( !sOperation.equalsIgnoreAsciiCaseAscii("cell-change") )
         return;
@@ -336,7 +382,7 @@ void SAL_CALL ScVbaEventsListener::changesOccurred( const util::ChangesEvent& aE
         {
             uno::Sequence< uno::Any > aArgs( 1 );
             aArgs[0] <<= xRangeObj;
-            mrVbaEvents.processVbaEvent( WORKSHEET_CHANGE, aArgs );
+            mrVbaEvents.processVbaEventNoThrow( WORKSHEET_CHANGE, aArgs );
         }
         return;
     }
@@ -344,7 +390,7 @@ void SAL_CALL ScVbaEventsListener::changesOccurred( const util::ChangesEvent& aE
     ScRangeList aRangeList;
     for( sal_Int32 nIndex = 0; nIndex < nCount; ++nIndex )
     {
-        aChange = aEvent.Changes[ nIndex ];
+        aChange = rEvent.Changes[ nIndex ];
         aChange.Accessor >>= sOperation;
         uno::Reference< table::XCellRange > xRangeObj;
         aChange.ReplacedElement >>= xRangeObj;
@@ -360,80 +406,120 @@ void SAL_CALL ScVbaEventsListener::changesOccurred( const util::ChangesEvent& aE
         }
     }
 
-    if( ( !aRangeList.empty() ) && mpDocShell )
+    if (!aRangeList.empty())
     {
         uno::Reference< sheet::XSheetCellRangeContainer > xRanges( new ScCellRangesObj( mpDocShell, aRangeList ) );
         uno::Sequence< uno::Any > aArgs(1);
         aArgs[0] <<= xRanges;
-        mrVbaEvents.processVbaEvent( WORKSHEET_CHANGE, aArgs );
+        mrVbaEvents.processVbaEventNoThrow( WORKSHEET_CHANGE, aArgs );
     }
 }
 
-// ----------------------------------------------------------------------------
-
-uno::Reference< frame::XFrame > ScVbaEventsListener::getFrame()
+void SAL_CALL ScVbaEventListener::disposing( const lang::EventObject& rEvent ) throw (uno::RuntimeException)
 {
-    if( !mbDisposed && mxModel.is() ) try
+    ::osl::MutexGuard aGuard( maMutex );
+
+    uno::Reference< frame::XModel > xModel( rEvent.Source, uno::UNO_QUERY );
+    if( xModel.is() )
     {
-        uno::Reference< frame::XController > xController( mxModel->getCurrentController(), uno::UNO_QUERY_THROW );
-        return xController->getFrame();
+        OSL_ENSURE( xModel.get() == mxModel.get(), "ScVbaEventListener::disposing - disposing from unknown model" );
+        stopModelListening();
+        mbDisposed = true;
+        return;
     }
-    catch( uno::Exception& )
+
+    uno::Reference< frame::XController > xController( rEvent.Source, uno::UNO_QUERY );
+    if( xController.is() )
     {
+        stopControllerListening( xController );
+        return;
     }
-    return uno::Reference< frame::XFrame >();
 }
 
-uno::Reference< awt::XWindow > ScVbaEventsListener::getContainerWindow()
+// private --------------------------------------------------------------------
+
+void ScVbaEventListener::startModelListening()
 {
     try
     {
-        uno::Reference< frame::XFrame > xFrame( getFrame(), uno::UNO_SET_THROW );
-        return xFrame->getContainerWindow();
+        uno::Reference< util::XChangesNotifier > xChangesNotifier( mxModel, uno::UNO_QUERY_THROW );
+        xChangesNotifier->addChangesListener( this );
     }
     catch( uno::Exception& )
     {
     }
-    return uno::Reference< awt::XWindow >();
 }
 
-bool ScVbaEventsListener::isMouseReleased()
+void ScVbaEventListener::stopModelListening()
 {
-    if( Window* pWindow = VCLUnoHelper::GetWindow( getContainerWindow() ) )
+    try
+    {
+        uno::Reference< util::XChangesNotifier > xChangesNotifier( mxModel, uno::UNO_QUERY_THROW );
+        xChangesNotifier->removeChangesListener( this );
+    }
+    catch( uno::Exception& )
     {
-        Window::PointerState aPointerState = pWindow->GetPointerState();
-        return (aPointerState.mnState & ( MOUSE_LEFT | MOUSE_MIDDLE | MOUSE_RIGHT )) == 0;
     }
-    return false;
 }
 
-IMPL_LINK( ScVbaEventsListener, fireResizeMacro, void*, EMPTYARG )
+uno::Reference< frame::XController > ScVbaEventListener::getControllerForWindow( Window* pWindow ) const
 {
-    if( !mbDisposed && isMouseReleased() ) try
+    WindowControllerMap::const_iterator aIt = maControllers.find( pWindow );
+    return (aIt == maControllers.end()) ? uno::Reference< frame::XController >() : aIt->second;
+}
+
+void ScVbaEventListener::processWindowActivateEvent( Window* pWindow, bool bActivate )
+{
+    uno::Reference< frame::XController > xController = getControllerForWindow( pWindow );
+    if( xController.is() )
     {
-        mrVbaEvents.processVbaEvent( WORKBOOK_WINDOWRESIZE, uno::Sequence< uno::Any >() );
+        uno::Sequence< uno::Any > aArgs( 1 );
+        aArgs[ 0 ] <<= xController;
+        mrVbaEvents.processVbaEventNoThrow( bActivate ? WORKBOOK_WINDOWACTIVATE : WORKBOOK_WINDOWDEACTIVATE, aArgs );
     }
-    catch( uno::Exception& )
+}
+
+void ScVbaEventListener::postWindowResizeEvent( Window* pWindow )
+{
+    // check that the passed window is still alive (it must be registered in maControllers)
+    if( pWindow && (maControllers.count( pWindow ) > 0) )
     {
-        // #163419# do not throw exceptions into application core
+        mbWindowResized = mbBorderChanged = false;
+        acquire();  // ensure we don't get deleted before the timer fires
+        Application::PostUserEvent( LINK( this, ScVbaEventListener, processWindowResizeEvent ), pWindow );
     }
-    release();
-    return 0;
 }
 
-void ScVbaEventsListener::processWindowResizeMacro()
+IMPL_LINK( ScVbaEventListener, processWindowResizeEvent, Window*, EMPTYARG pWindow )
 {
-    OSL_TRACE( "**** Attempt to FIRE MACRO **** " );
-    if( !mbDisposed )
+    ::osl::MutexGuard aGuard( maMutex );
+
+    /*  Check that the passed window is still alive (it must be registered in
+        maControllers). While closing a document, postWindowResizeEvent() may
+        be called on the last window which posts a user event via
+        Application::PostUserEvent to call this event handler. VCL will trigger
+        the handler some time later. Sometimes, the window gets deleted before.
+        This is handled via the disposing() function which removes the window
+        pointer from the member maControllers. Thus, checking whether
+        maControllers contains pWindow ensures that the window is still alive. */
+    if( !mbDisposed && pWindow && (maControllers.count( pWindow ) > 0) )
     {
-        try
-        {
-            mrVbaEvents.processVbaEvent( WORKBOOK_WINDOWRESIZE, uno::Sequence< uno::Any >() );
-        }
-        catch( uno::Exception& )
+        // do not fire event unless all mouse buttons have been released
+        Window::PointerState aPointerState = pWindow->GetPointerState();
+        if( (aPointerState.mnState & (MOUSE_LEFT | MOUSE_MIDDLE | MOUSE_RIGHT)) == 0 )
         {
+            uno::Reference< frame::XController > xController = getControllerForWindow( pWindow );
+            if( xController.is() )
+            {
+                uno::Sequence< uno::Any > aArgs( 1 );
+                aArgs[ 0 ] <<= xController;
+                // #163419# do not throw exceptions into application core
+                mrVbaEvents.processVbaEventNoThrow( WORKBOOK_WINDOWRESIZE, aArgs );
+            }
         }
     }
+    release();
+    return 0;
 }
 
 // ============================================================================
@@ -448,19 +534,19 @@ ScVbaEventsHelper::ScVbaEventsHelper( const uno::Sequence< uno::Any >& rArgs, co
     if( !mxModel.is() || !mpDocShell || !mpDoc )
         return;
 
-#define REGISTER_EVENT( eventid, eventname, type, cancelindex, worksheet ) \
-    registerEventHandler( eventid, eventname, type, cancelindex, uno::Any( worksheet ) )
-
+#define REGISTER_EVENT( eventid, moduletype, classname, eventname, cancelindex, worksheet ) \
+    registerEventHandler( eventid, moduletype, classname "_" eventname, cancelindex, uno::Any( worksheet ) )
+#define REGISTER_AUTO_EVENT( eventid, eventname ) \
+    REGISTER_EVENT( AUTO_##eventid, script::ModuleType::NORMAL, "Auto", eventname, -1, false )
 #define REGISTER_WORKBOOK_EVENT( eventid, eventname, cancelindex ) \
-    REGISTER_EVENT( WORKBOOK_##eventid, "Workbook_" eventname, EVENTHANDLER_DOCUMENT, cancelindex, false )
-
+    REGISTER_EVENT( WORKBOOK_##eventid, script::ModuleType::DOCUMENT, "Workbook", eventname, cancelindex, false )
 #define REGISTER_WORKSHEET_EVENT( eventid, eventname, cancelindex ) \
-    REGISTER_EVENT( WORKSHEET_##eventid, "Worksheet_" eventname, EVENTHANDLER_DOCUMENT, cancelindex, true ); \
-    REGISTER_EVENT( (USERDEFINED_START + WORKSHEET_##eventid), "Workbook_Sheet" eventname, EVENTHANDLER_DOCUMENT, (((cancelindex) >= 0) ? ((cancelindex) + 1) : -1), false )
+    REGISTER_EVENT( WORKSHEET_##eventid, script::ModuleType::DOCUMENT, "Worksheet", eventname, cancelindex, true ); \
+    REGISTER_EVENT( (USERDEFINED_START + WORKSHEET_##eventid), script::ModuleType::DOCUMENT, "Workbook", "Sheet" eventname, (((cancelindex) >= 0) ? ((cancelindex) + 1) : -1), false )
 
     // global
-    REGISTER_EVENT( AUTO_OPEN,  "Auto_Open",  EVENTHANDLER_GLOBAL, -1, false );
-    REGISTER_EVENT( AUTO_CLOSE, "Auto_Close", EVENTHANDLER_GLOBAL, -1, false );
+    REGISTER_AUTO_EVENT( OPEN,  "Open" );
+    REGISTER_AUTO_EVENT( CLOSE, "Close" );
 
     // Workbook
     REGISTER_WORKBOOK_EVENT( ACTIVATE,            "Activate",           -1 );
@@ -485,19 +571,68 @@ ScVbaEventsHelper::ScVbaEventsHelper( const uno::Sequence< uno::Any >& rArgs, co
     REGISTER_WORKSHEET_EVENT( SELECTIONCHANGE,    "SelectionChange",    -1 );
     REGISTER_WORKSHEET_EVENT( FOLLOWHYPERLINK,    "FollowHyperlink",    -1 );
 
-#undef REGISTER_EVENT
-#undef REGISTER_WORKBOOK_EVENT
 #undef REGISTER_WORKSHEET_EVENT
+#undef REGISTER_WORKBOOK_EVENT
+#undef REGISTER_AUTO_EVENT
+#undef REGISTER_EVENT
 }
 
 ScVbaEventsHelper::~ScVbaEventsHelper()
 {
 }
 
-void SAL_CALL ScVbaEventsHelper::disposing( const lang::EventObject& rSource ) throw (uno::RuntimeException)
+void SAL_CALL ScVbaEventsHelper::notifyEvent( const css::document::EventObject& rEvent ) throw (css::uno::RuntimeException)
 {
-    mxListener.clear();
-    VbaEventsHelperBase::disposing( rSource );
+    static const uno::Sequence< uno::Any > saEmptyArgs;
+    if( (rEvent.EventName == GlobalEventConfig::GetEventName( STR_EVENT_OPENDOC )) ||
+        (rEvent.EventName == GlobalEventConfig::GetEventName( STR_EVENT_CREATEDOC )) )  // CREATEDOC triggered e.g. during VBA Workbooks.Add
+    {
+        processVbaEventNoThrow( WORKBOOK_OPEN, saEmptyArgs );
+    }
+    else if( rEvent.EventName == GlobalEventConfig::GetEventName( STR_EVENT_ACTIVATEDOC ) )
+    {
+        processVbaEventNoThrow( WORKBOOK_ACTIVATE, saEmptyArgs );
+    }
+    else if( rEvent.EventName == GlobalEventConfig::GetEventName( STR_EVENT_DEACTIVATEDOC ) )
+    {
+        processVbaEventNoThrow( WORKBOOK_DEACTIVATE, saEmptyArgs );
+    }
+    else if( (rEvent.EventName == GlobalEventConfig::GetEventName( STR_EVENT_SAVEDOCDONE )) ||
+             (rEvent.EventName == GlobalEventConfig::GetEventName( STR_EVENT_SAVEASDOCDONE )) ||
+             (rEvent.EventName == GlobalEventConfig::GetEventName( STR_EVENT_SAVETODOCDONE )) )
+    {
+        uno::Sequence< uno::Any > aArgs( 1 );
+        aArgs[ 0 ] <<= true;
+        processVbaEventNoThrow( WORKBOOK_AFTERSAVE, aArgs );
+    }
+    else if( (rEvent.EventName == GlobalEventConfig::GetEventName( STR_EVENT_SAVEDOCFAILED )) ||
+             (rEvent.EventName == GlobalEventConfig::GetEventName( STR_EVENT_SAVEASDOCFAILED )) ||
+             (rEvent.EventName == GlobalEventConfig::GetEventName( STR_EVENT_SAVETODOCFAILED )) )
+    {
+        uno::Sequence< uno::Any > aArgs( 1 );
+        aArgs[ 0 ] <<= false;
+        processVbaEventNoThrow( WORKBOOK_AFTERSAVE, aArgs );
+    }
+    else if( rEvent.EventName == GlobalEventConfig::GetEventName( STR_EVENT_CLOSEDOC ) )
+    {
+        /*  Trigger the WORKBOOK_WINDOWDEACTIVATE and WORKBOOK_DEACTIVATE
+            events and stop listening to the model (done in base class). */
+        uno::Reference< frame::XController > xController( mxModel->getCurrentController() );
+        if( xController.is() )
+        {
+            uno::Sequence< uno::Any > aArgs( 1 );
+            aArgs[ 0 ] <<= xController;
+            processVbaEventNoThrow( WORKBOOK_WINDOWDEACTIVATE, aArgs );
+        }
+        processVbaEventNoThrow( WORKBOOK_DEACTIVATE, saEmptyArgs );
+    }
+    else if( rEvent.EventName == GlobalEventConfig::GetEventName( STR_EVENT_VIEWCREATED ) )
+    {
+        uno::Reference< frame::XController > xController( mxModel->getCurrentController() );
+        if( mxListener.get() && xController.is() )
+            mxListener->startControllerListening( xController );
+    }
+    VbaEventsHelperBase::notifyEvent( rEvent );
 }
 
 // protected ------------------------------------------------------------------
@@ -509,25 +644,35 @@ bool ScVbaEventsHelper::implPrepareEvent( EventQueue& rEventQueue,
     if( !mpShell || !mpDoc )
         throw uno::RuntimeException();
 
-    // framework and Calc fire a few events before 'opened', ignore them
-    bool bExecuteEvent = mbOpened;
-    
+    /*  For document events: check if events are enabled via the
+        Application.EnableEvents symbol (this is an Excel-only attribute).
+        Check this again for every event, as the event handler may change the
+        state of the EnableEvents symbol. Global events such as AUTO_OPEN and
+        AUTO_CLOSE are always enabled. */
+    bool bExecuteEvent = (rInfo.mnModuleType != script::ModuleType::DOCUMENT) || ScVbaApplication::getDocumentEventsEnabled();
+
+    // framework and Calc fire a few events before 'OnLoad', ignore them
+    if( bExecuteEvent )
+        bExecuteEvent = (rInfo.mnEventId == WORKBOOK_OPEN) ? !mbOpened : mbOpened;
+
     // special handling for some events
-    switch( rInfo.mnEventId )
+    if( bExecuteEvent ) switch( rInfo.mnEventId )
     {
         case WORKBOOK_OPEN:
-            bExecuteEvent = !mbOpened;
-            if( bExecuteEvent )
-            {
-                // execute delayed Activate event too (see above)
-                rEventQueue.push_back( WORKBOOK_ACTIVATE );
-                rEventQueue.push_back( WORKBOOK_WINDOWACTIVATE );
-                rEventQueue.push_back( AUTO_OPEN );
-            }
+        {
+            // execute delayed Activate event too (see above)
+            rEventQueue.push_back( WORKBOOK_ACTIVATE );
+            uno::Sequence< uno::Any > aArgs( 1 );
+            aArgs[ 0 ] <<= mxModel->getCurrentController();
+            rEventQueue.push_back( EventQueueEntry( WORKBOOK_WINDOWACTIVATE, aArgs ) );
+            rEventQueue.push_back( AUTO_OPEN );
+            // remember initial selection
+            maOldSelection <<= mxModel->getCurrentSelection();
+        }
         break;
         case WORKSHEET_SELECTIONCHANGE:
             // if selection is not changed, then do not fire the event
-            bExecuteEvent = bExecuteEvent && isSelectionChanged( rArgs, 0 );
+            bExecuteEvent = isSelectionChanged( rArgs, 0 );
         break;
     }
 
@@ -537,14 +682,6 @@ bool ScVbaEventsHelper::implPrepareEvent( EventQueue& rEventQueue,
         bool bSheetEvent = false;
         if( (rInfo.maUserData >>= bSheetEvent) && bSheetEvent )
             rEventQueue.push_back( EventQueueEntry( rInfo.mnEventId + USERDEFINED_START, rArgs ) );
-
-        /*  For document events: check if events are enabled via the
-            Application.EnableEvents symbol (this is an Excel-only attribute).
-            Check this again for every event, as the event handler may change
-            the state of the EnableEvents symbol. Global events such as
-            AUTO_OPEN and AUTO_CLOSE are always enabled. */
-        if( rInfo.meType == EVENTHANDLER_DOCUMENT )
-            bExecuteEvent = ScVbaApplication::getDocumentEventsEnabled();
     }
 
     return bExecuteEvent;
@@ -591,7 +728,7 @@ uno::Sequence< uno::Any > ScVbaEventsHelper::implBuildArgumentList( const EventH
         case WORKBOOK_WINDOWDEACTIVATE:
         case WORKBOOK_WINDOWRESIZE:
             aVbaArgs.realloc( 1 );
-            aVbaArgs[ 0 ] = createWindow();
+            aVbaArgs[ 0 ] = createWindow( rArgs, 0 );
         break;
         // 1 arg: worksheet
         case WORKBOOK_NEWSHEET:
@@ -642,7 +779,7 @@ uno::Sequence< uno::Any > ScVbaEventsHelper::implBuildArgumentList( const EventH
 }
 
 void ScVbaEventsHelper::implPostProcessEvent( EventQueue& rEventQueue,
-        const EventHandlerInfo& rInfo, bool /*bSuccess*/, bool bCancel ) throw (uno::RuntimeException)
+        const EventHandlerInfo& rInfo, bool bCancel ) throw (uno::RuntimeException)
 {
     switch( rInfo.mnEventId )
     {
@@ -650,10 +787,7 @@ void ScVbaEventsHelper::implPostProcessEvent( EventQueue& rEventQueue,
             mbOpened = true;
             // register the listeners
             if( !mxListener.is() )
-            {
-                mxListener = new ScVbaEventsListener( *this, mxModel, mpDocShell );
-                mxListener->startListening();
-            }
+                mxListener = new ScVbaEventListener( *this, mxModel, mpDocShell );
         break;
         case WORKBOOK_BEFORECLOSE:
             /*  Execute Auto_Close only if not cancelled by event handler, but
@@ -664,7 +798,7 @@ void ScVbaEventsHelper::implPostProcessEvent( EventQueue& rEventQueue,
     }
 }
 
-::rtl::OUString ScVbaEventsHelper::implGetDocumentModuleName( const EventHandlerInfo& rInfo,
+OUString ScVbaEventsHelper::implGetDocumentModuleName( const EventHandlerInfo& rInfo,
         const uno::Sequence< uno::Any >& rArgs ) const throw (lang::IllegalArgumentException)
 {
     bool bSheetEvent = false;
@@ -696,7 +830,7 @@ bool lclSelectionChanged( const ScRangeList& rLeft, const ScRangeList& rRight )
         return !(bLeftEmpty && bRightEmpty);
 
     // check sheet indexes of the range lists (assuming that all ranges in a list are on the same sheet)
-    if( rLeft.front()->aStart.Tab() != rRight.front()->aStart.Tab() )
+    if (rLeft[0]->aStart.Tab() != rRight[0]->aStart.Tab())
         return false;
 
     // compare all ranges
@@ -707,15 +841,13 @@ bool lclSelectionChanged( const ScRangeList& rLeft, const ScRangeList& rRight )
 
 bool ScVbaEventsHelper::isSelectionChanged( const uno::Sequence< uno::Any >& rArgs, sal_Int32 nIndex ) throw (lang::IllegalArgumentException, uno::RuntimeException)
 {
+    uno::Reference< uno::XInterface > xOldSelection( maOldSelection, uno::UNO_QUERY );
     uno::Reference< uno::XInterface > xNewSelection = getXSomethingFromArgs< uno::XInterface >( rArgs, nIndex, false );
-    if( ScCellRangesBase* pNewCellRanges = ScCellRangesBase::getImplementation( xNewSelection ) )
-    {
-        bool bChanged = maOldSelection != pNewCellRanges->GetRangeList();
-        maOldSelection = pNewCellRanges->GetRangeList();
-        return bChanged;
-    }
-    maOldSelection.RemoveAll();
-    return true;
+    ScCellRangesBase* pOldCellRanges = ScCellRangesBase::getImplementation( xOldSelection );
+    ScCellRangesBase* pNewCellRanges = ScCellRangesBase::getImplementation( xNewSelection );
+    bool bChanged = !pOldCellRanges || !pNewCellRanges || lclSelectionChanged( pOldCellRanges->GetRangeList(), pNewCellRanges->GetRangeList() );
+    maOldSelection <<= xNewSelection;
+    return bChanged;
 }
 
 uno::Any ScVbaEventsHelper::createWorksheet( const uno::Sequence< uno::Any >& rArgs, sal_Int32 nIndex ) const
@@ -765,11 +897,13 @@ uno::Any ScVbaEventsHelper::createHyperlink( const uno::Sequence< uno::Any >& rA
     return uno::Any( xHyperlink );
 }
 
-uno::Any ScVbaEventsHelper::createWindow() const throw (uno::RuntimeException)
+uno::Any ScVbaEventsHelper::createWindow( const uno::Sequence< uno::Any >& rArgs, sal_Int32 nIndex ) const
+        throw (lang::IllegalArgumentException, uno::RuntimeException)
 {
-    uno::Sequence< uno::Any > aArgs( 2 );
-    aArgs[ 0 ] <<= createVBAUnoAPIService( mpShell, "ooo.vba.Application" );
+    uno::Sequence< uno::Any > aArgs( 3 );
+    aArgs[ 0 ] <<= getVBADocument( mxModel );
     aArgs[ 1 ] <<= mxModel;
+    aArgs[ 2 ] <<= getXSomethingFromArgs< frame::XController >( rArgs, nIndex, false );
     uno::Reference< uno::XInterface > xWindow( createVBAUnoAPIServiceWithArgs( mpShell, "ooo.vba.excel.Window", aArgs ), uno::UNO_SET_THROW );
     return uno::Any( xWindow );
 }
diff --git a/sc/source/ui/vba/vbaeventshelper.hxx b/sc/source/ui/vba/vbaeventshelper.hxx
index b4408d0..564811a 100644
--- a/sc/source/ui/vba/vbaeventshelper.hxx
+++ b/sc/source/ui/vba/vbaeventshelper.hxx
@@ -26,15 +26,15 @@
  *
  ************************************************************************/
 
-#ifndef SC_VBAEVENTS_HXX
-#define SC_VBAEVENTS_HXX
+#ifndef SC_VBAEVENTSHELPER_HXX
+#define SC_VBAEVENTSHELPER_HXX
 
 #include <rtl/ref.hxx>
 #include <vbahelper/vbaeventshelperbase.hxx>
 #include "excelvbahelper.hxx"
 #include "rangelst.hxx"
 
-class ScVbaEventsListener;
+class ScVbaEventListener;
 
 // ============================================================================
 
@@ -46,13 +46,12 @@ public:
         const css::uno::Reference< css::uno::XComponentContext >& rxContext );
     virtual ~ScVbaEventsHelper();
 
-    // XEventListener
-    virtual void SAL_CALL disposing( const css::lang::EventObject& rSource ) throw (css::uno::RuntimeException);
-    
+    virtual void SAL_CALL notifyEvent( const css::document::EventObject& rEvent ) throw (css::uno::RuntimeException);
+
 protected:
     virtual bool implPrepareEvent( EventQueue& rEventQueue, const EventHandlerInfo& rInfo, const css::uno::Sequence< css::uno::Any >& rArgs ) throw (css::uno::RuntimeException);
     virtual css::uno::Sequence< css::uno::Any > implBuildArgumentList( const EventHandlerInfo& rInfo, const css::uno::Sequence< css::uno::Any >& rArgs ) throw (css::lang::IllegalArgumentException);
-    virtual void implPostProcessEvent( EventQueue& rEventQueue, const EventHandlerInfo& rInfo, bool bSuccess, bool bCancel ) throw (css::uno::RuntimeException);
+    virtual void implPostProcessEvent( EventQueue& rEventQueue, const EventHandlerInfo& rInfo, bool bCancel ) throw (css::uno::RuntimeException);
     virtual ::rtl::OUString implGetDocumentModuleName( const EventHandlerInfo& rInfo, const css::uno::Sequence< css::uno::Any >& rArgs ) const throw (css::lang::IllegalArgumentException);
 
 private:
@@ -66,14 +65,14 @@ private:
     css::uno::Any createRange( const css::uno::Sequence< css::uno::Any >& rArgs, sal_Int32 nIndex ) const throw (css::lang::IllegalArgumentException, css::uno::RuntimeException);
     /** Creates a VBA Hyperlink object (the argument must contain a UNO cell). */
     css::uno::Any createHyperlink( const css::uno::Sequence< css::uno::Any >& rArgs, sal_Int32 nIndex ) const throw (css::lang::IllegalArgumentException, css::uno::RuntimeException);
-    /** Creates a VBA Window object. */
-    css::uno::Any createWindow() const throw (css::uno::RuntimeException);
+    /** Creates a VBA Window object (the argument must contain a model controller). */
+    css::uno::Any createWindow( const css::uno::Sequence< css::uno::Any >& rArgs, sal_Int32 nIndex ) const throw (css::lang::IllegalArgumentException, css::uno::RuntimeException);
     
 private:
-    ::rtl::Reference< ScVbaEventsListener > mxListener;
+    ::rtl::Reference< ScVbaEventListener > mxListener;
+    css::uno::Any maOldSelection;
     ScDocShell* mpDocShell;
     ScDocument* mpDoc;
-    ScRangeList maOldSelection;
     bool mbOpened;
 };
 
diff --git a/sc/source/ui/vba/vbawindow.cxx b/sc/source/ui/vba/vbawindow.cxx
index af57638..1e04b50 100644
--- a/sc/source/ui/vba/vbawindow.cxx
+++ b/sc/source/ui/vba/vbawindow.cxx
@@ -60,11 +60,9 @@ typedef  boost::unordered_map< rtl::OUString,
 SCTAB, ::rtl::OUStringHash,
 ::std::equal_to< ::rtl::OUString > > NameIndexHash;
 
-typedef std::vector < uno::Reference< sheet::XSpreadsheet > > Sheets;
+typedef std::vector< uno::Reference< sheet::XSpreadsheet > > Sheets;
 
-typedef ::cppu::WeakImplHelper1< container::XEnumeration
-
-> Enumeration_BASE;
+typedef ::cppu::WeakImplHelper1< container::XEnumeration > Enumeration_BASE;
 
 typedef ::cppu::WeakImplHelper3< container::XEnumerationAccess 
     , com::sun::star::container::XIndexAccess
@@ -201,13 +199,20 @@ public:
 
 };
 
-ScVbaWindow::ScVbaWindow( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext >& xContext, const uno::Reference< frame::XModel >& xModel ) : WindowImpl_BASE( xParent, xContext, xModel )
+ScVbaWindow::ScVbaWindow(
+        const uno::Reference< XHelperInterface >& xParent,
+        const uno::Reference< uno::XComponentContext >& xContext,
+        const uno::Reference< frame::XModel >& xModel,
+        const uno::Reference< frame::XController >& xController ) throw (uno::RuntimeException) :
+    WindowImpl_BASE( xParent, xContext, xModel, xController )
 {
     init();
 }
 
-ScVbaWindow::ScVbaWindow( uno::Sequence< uno::Any > const & args, uno::Reference< uno::XComponentContext > const & xContext )
-        : WindowImpl_BASE( args, xContext )
+ScVbaWindow::ScVbaWindow(
+        const uno::Sequence< uno::Any >& args,
+        const uno::Reference< uno::XComponentContext >& xContext ) throw (uno::RuntimeException) :
+    WindowImpl_BASE( args, xContext )
 {
     init();
 }
@@ -219,20 +224,42 @@ ScVbaWindow::init()
         still zero. The implementation of ActivePane() uses a UNO reference of
         this (to set this window as parent of the pane obejct). This requires
         the own refcount to be non-zero, otherwise this instance will be
-        desctructed immediately! */
+        desctructed immediately! Guard the call to ActivePane() in try/catch to
+        not miss the decrementation of the reference count on exception. */
     osl_incrementInterlockedCount( &m_refCount );
-    uno::Reference< frame::XController > xController( m_xModel->getCurrentController(), uno::UNO_QUERY_THROW );
-    m_xViewPane.set( xController, uno::UNO_QUERY_THROW );
-    m_xViewFreezable.set( xController, uno::UNO_QUERY_THROW );
-    m_xViewSplitable.set( xController, uno::UNO_QUERY_THROW );
-    m_xPane.set( ActivePane(), uno::UNO_QUERY_THROW );
-    m_xDevice.set( xController->getFrame()->getComponentWindow(), uno::UNO_QUERY_THROW );
+    try
+    {
+       m_xPane = ActivePane();
+    }
+    catch( uno::Exception& )
+    {
+    }
     osl_decrementInterlockedCount( &m_refCount );
 }
 
+uno::Reference< beans::XPropertySet >
+ScVbaWindow::getControllerProps() throw (uno::RuntimeException)
+{
+    return uno::Reference< beans::XPropertySet >( getController(), uno::UNO_QUERY_THROW );
+}
+
+uno::Reference< beans::XPropertySet >
+ScVbaWindow::getFrameProps() throw (uno::RuntimeException)
+{
+    return uno::Reference< beans::XPropertySet >( getController()->getFrame(), uno::UNO_QUERY_THROW );
+}
+
+uno::Reference< awt::XDevice >
+ScVbaWindow::getDevice() throw (uno::RuntimeException)
+{
+    return uno::Reference< awt::XDevice >( getWindow(), uno::UNO_QUERY_THROW );
+}
+
 void
 ScVbaWindow::Scroll( const uno::Any& Down, const uno::Any& Up, const uno::Any& ToRight, const uno::Any& ToLeft, bool bLargeScroll ) throw (uno::RuntimeException)
 {
+    if( !m_xPane.is() )
+        throw uno::RuntimeException();
     if( bLargeScroll )
         m_xPane->LargeScroll( Down, Up, ToRight, ToLeft );
     else
@@ -254,7 +281,7 @@ ScVbaWindow::LargeScroll( const uno::Any& Down, const uno::Any& Up, const uno::A
 uno::Any SAL_CALL 
 ScVbaWindow::SelectedSheets( const uno::Any& aIndex ) throw (uno::RuntimeException)
 {
-    uno::Reference< container::XEnumerationAccess > xEnumAccess( new SelectedSheetsEnumAccess( mxContext, m_xModel  ) );
+    uno::Reference< container::XEnumerationAccess > xEnumAccess( new SelectedSheetsEnumAccess( mxContext, m_xModel ) );
     // #FIXME needs a workbook as a parent
     uno::Reference< excel::XWorksheets > xSheets(  new ScVbaWorksheets( uno::Reference< XHelperInterface >(), mxContext, xEnumAccess, m_xModel ) );
     if ( aIndex.hasValue() )
@@ -270,17 +297,20 @@ ScVbaWindow::ScrollWorkbookTabs( const uno::Any& /*Sheets*/, const uno::Any& /*P
 {
 // #TODO #FIXME need some implementation to scroll through the tabs
 // but where is this done?
-}
-uno::Reference< beans::XPropertySet >
-getPropsFromModel( const uno::Reference< frame::XModel >& xModel )
-{
-    uno::Reference< frame::XController > xController = xModel->getCurrentController();
-    if ( !xController.is() )
-        throw uno::RuntimeException( rtl::OUString(
-            RTL_CONSTASCII_USTRINGPARAM ("No controller for model") ), uno::Reference< uno::XInterface >() );	
-    return uno::Reference< beans::XPropertySet >(  xController->getFrame(), uno::UNO_QUERY );
-}
+/*
+    sal_Int32 nSheets = 0;
+    sal_Int32 nPosition = 0;
+    throw uno::RuntimeException( rtl::OUString::createFromAscii("No Implemented" ), uno::Reference< uno::XInterface >() );
+    sal_Bool bSheets = ( Sheets >>= nSheets );
+    sal_Bool bPosition = ( Position >>= nPosition );
+    if ( bSheets || bPosition ) // at least one param specified
+        if ( bSheets )
+            ;// use sheets
+        else if ( bPosition )
+            ; //use position
+*/
 
+}
 
 uno::Any SAL_CALL 
 ScVbaWindow::getCaption() throw (uno::RuntimeException)
@@ -288,9 +318,8 @@ ScVbaWindow::getCaption() throw (uno::RuntimeException)
     static rtl::OUString sCrud(RTL_CONSTASCII_USTRINGPARAM(" - OpenOffice.org Calc" ) );
     static sal_Int32 nCrudLen = sCrud.getLength();
 
-    uno::Reference< beans::XPropertySet > xProps = getPropsFromModel( m_xModel );
     rtl::OUString sTitle;
-    xProps->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( SC_UNONAME_TITLE ) ) ) >>= sTitle;	
+    getFrameProps()->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( SC_UNONAME_TITLE ) ) ) >>= sTitle;
     sal_Int32 nCrudIndex = sTitle.indexOf( sCrud );	
     // adjust title ( by removing crud )
     // sCrud string present
@@ -326,16 +355,14 @@ ScVbaWindow::getCaption() throw (uno::RuntimeException)
 void SAL_CALL 
 ScVbaWindow::setCaption( const uno::Any& _caption ) throw (uno::RuntimeException)
 {
-    
-    uno::Reference< beans::XPropertySet > xProps = getPropsFromModel( m_xModel );
-    xProps->setPropertyValue( rtl::OUString(
-        RTL_CONSTASCII_USTRINGPARAM ( SC_UNONAME_TITLE ) ) , _caption );	
+    getFrameProps()->setPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( SC_UNONAME_TITLE ) ), _caption );
 }
 
 uno::Any SAL_CALL 
 ScVbaWindow::getScrollRow() throw (uno::RuntimeException)
 {	
     sal_Int32 nValue = 0;
+    // !! TODO !! get view shell from controller
     ScTabViewShell* pViewShell = excel::getBestViewShell( m_xModel );	
     if ( pViewShell )
     {
@@ -349,7 +376,8 @@ ScVbaWindow::getScrollRow() throw (uno::RuntimeException)
 void SAL_CALL 
 ScVbaWindow::setScrollRow( const uno::Any& _scrollrow ) throw (uno::RuntimeException)
 {
-    ScTabViewShell* pViewShell = excel::getBestViewShell( m_xModel );	
+    // !! TODO !! get view shell from controller
+    ScTabViewShell* pViewShell = excel::getBestViewShell( m_xModel );
     if ( pViewShell )
     {
         sal_Int32 scrollRow = 0;	
@@ -364,7 +392,8 @@ uno::Any SAL_CALL
 ScVbaWindow::getScrollColumn() throw (uno::RuntimeException)
 {	
     sal_Int32 nValue = 0;
-    ScTabViewShell* pViewShell = excel::getBestViewShell( m_xModel );	
+    // !! TODO !! get view shell from controller
+    ScTabViewShell* pViewShell = excel::getBestViewShell( m_xModel );
     if ( pViewShell )
     {
         ScSplitPos eWhich = pViewShell->GetViewData()->GetActivePart();
@@ -377,7 +406,8 @@ ScVbaWindow::getScrollColumn() throw (uno::RuntimeException)
 void SAL_CALL 
 ScVbaWindow::setScrollColumn( const uno::Any& _scrollcolumn ) throw (uno::RuntimeException)
 {
-    ScTabViewShell* pViewShell = excel::getBestViewShell( m_xModel );	
+    // !! TODO !! get view shell from controller
+    ScTabViewShell* pViewShell = excel::getBestViewShell( m_xModel );
     if ( pViewShell )
     {
         sal_Int32 scrollColumn = 0;	
@@ -392,7 +422,8 @@ uno::Any SAL_CALL
 ScVbaWindow::getWindowState() throw (uno::RuntimeException)
 {	
     sal_Int32 nwindowState = xlNormal;
-    ScTabViewShell* pViewShell = excel::getBestViewShell( m_xModel );	
+    // !! TODO !! get view shell from controller
+    ScTabViewShell* pViewShell = excel::getBestViewShell( m_xModel );
     SfxViewFrame* pViewFrame = pViewShell -> GetViewFrame();
     WorkWindow* pWork = (WorkWindow*) pViewFrame->GetFrame().GetSystemWindow();
     if ( pWork )
@@ -410,7 +441,8 @@ ScVbaWindow::setWindowState( const uno::Any& _windowstate ) throw (uno::RuntimeE
 {
     sal_Int32 nwindowState = xlMaximized;
     _windowstate >>= nwindowState;
-    ScTabViewShell* pViewShell = excel::getBestViewShell( m_xModel );	
+    // !! TODO !! get view shell from controller
+    ScTabViewShell* pViewShell = excel::getBestViewShell( m_xModel );
     SfxViewFrame* pViewFrame = pViewShell -> GetViewFrame();
     WorkWindow* pWork = (WorkWindow*) pViewFrame->GetFrame().GetSystemWindow();
     if ( pWork )
@@ -444,7 +476,8 @@ ScVbaWindow::Close( const uno::Any& SaveChanges, const uno::Any& FileName, const
 uno::Reference< excel::XPane > SAL_CALL
 ScVbaWindow::ActivePane() throw (script::BasicErrorException, uno::RuntimeException) 
 { 
-    return new ScVbaPane( this, mxContext, m_xModel, m_xViewPane );
+    uno::Reference< sheet::XViewPane > xViewPane( getController(), uno::UNO_QUERY_THROW );
+    return new ScVbaPane( this, mxContext, m_xModel, xViewPane );
 }
 
 uno::Reference< excel::XRange > SAL_CALL 
@@ -472,10 +505,9 @@ ScVbaWindow::RangeSelection() throw (script::BasicErrorException, uno::RuntimeEx
 ::sal_Bool SAL_CALL
 ScVbaWindow::getDisplayGridlines() throw (uno::RuntimeException)
 {
-    uno::Reference< beans::XPropertySet > xProps( m_xModel->getCurrentController(), uno::UNO_QUERY_THROW );
     rtl::OUString sName( RTL_CONSTASCII_USTRINGPARAM( SC_UNO_SHOWGRID ) );
     sal_Bool bGrid = sal_True;
-    xProps->getPropertyValue( sName ) >>= bGrid;
+    getControllerProps()->getPropertyValue( sName ) >>= bGrid;
     return bGrid;	
 }
 
@@ -483,131 +515,125 @@ ScVbaWindow::getDisplayGridlines() throw (uno::RuntimeException)
 void SAL_CALL 
 ScVbaWindow::setDisplayGridlines( ::sal_Bool _displaygridlines ) throw (uno::RuntimeException)
 {
-    uno::Reference< beans::XPropertySet > xProps( m_xModel->getCurrentController(), uno::UNO_QUERY_THROW );
     rtl::OUString sName( RTL_CONSTASCII_USTRINGPARAM( SC_UNO_SHOWGRID ) );
-    xProps->setPropertyValue( sName, uno::makeAny( _displaygridlines ));
+    getControllerProps()->setPropertyValue( sName, uno::makeAny( _displaygridlines ));
 }
 
 ::sal_Bool SAL_CALL 
 ScVbaWindow::getDisplayHeadings() throw (uno::RuntimeException)
 {
-    uno::Reference< beans::XPropertySet > xProps( m_xModel->getCurrentController(), uno::UNO_QUERY_THROW );
     rtl::OUString sName( RTL_CONSTASCII_USTRINGPARAM( SC_UNO_COLROWHDR ) );
     sal_Bool bHeading = sal_True;
-    xProps->getPropertyValue( sName ) >>= bHeading;
+    getControllerProps()->getPropertyValue( sName ) >>= bHeading;
     return bHeading;	
 }
 
 void SAL_CALL 
 ScVbaWindow::setDisplayHeadings( ::sal_Bool _bDisplayHeadings ) throw (uno::RuntimeException)
 {
-    uno::Reference< beans::XPropertySet > xProps( m_xModel->getCurrentController(), uno::UNO_QUERY_THROW );
     rtl::OUString sName( RTL_CONSTASCII_USTRINGPARAM( SC_UNO_COLROWHDR ) );
-    xProps->setPropertyValue( sName, uno::makeAny( _bDisplayHeadings ));
+    getControllerProps()->setPropertyValue( sName, uno::makeAny( _bDisplayHeadings ));
 }
 
 ::sal_Bool SAL_CALL 
 ScVbaWindow::getDisplayHorizontalScrollBar() throw (uno::RuntimeException)
 {
-    uno::Reference< beans::XPropertySet > xProps( m_xModel->getCurrentController(), uno::UNO_QUERY_THROW );
     rtl::OUString sName( RTL_CONSTASCII_USTRINGPARAM( SC_UNO_HORSCROLL ) );
     sal_Bool bHorizontalScrollBar = sal_True;
-    xProps->getPropertyValue( sName ) >>= bHorizontalScrollBar;
+    getControllerProps()->getPropertyValue( sName ) >>= bHorizontalScrollBar;
     return bHorizontalScrollBar;	
 }
 
 void SAL_CALL 
 ScVbaWindow::setDisplayHorizontalScrollBar( ::sal_Bool _bDisplayHorizontalScrollBar ) throw (uno::RuntimeException)
 {
-    uno::Reference< beans::XPropertySet > xProps( m_xModel->getCurrentController(), uno::UNO_QUERY_THROW );
     rtl::OUString sName( RTL_CONSTASCII_USTRINGPARAM( SC_UNO_HORSCROLL ) );
-    xProps->setPropertyValue( sName, uno::makeAny( _bDisplayHorizontalScrollBar ));
+    getControllerProps()->setPropertyValue( sName, uno::makeAny( _bDisplayHorizontalScrollBar ));
 }
 
 ::sal_Bool SAL_CALL 
 ScVbaWindow::getDisplayOutline() throw (uno::RuntimeException)
 {
-    uno::Reference< beans::XPropertySet > xProps( m_xModel->getCurrentController(), uno::UNO_QUERY_THROW );
     rtl::OUString sName( RTL_CONSTASCII_USTRINGPARAM( SC_UNO_OUTLSYMB ) );
     sal_Bool bOutline = sal_True;
-    xProps->getPropertyValue( sName ) >>= bOutline;
+    getControllerProps()->getPropertyValue( sName ) >>= bOutline;
     return bOutline;	
 }
 
 void SAL_CALL 
 ScVbaWindow::setDisplayOutline( ::sal_Bool _bDisplayOutline ) throw (uno::RuntimeException)
 {
-    uno::Reference< beans::XPropertySet > xProps( m_xModel->getCurrentController(), uno::UNO_QUERY_THROW );
     rtl::OUString sName( RTL_CONSTASCII_USTRINGPARAM( SC_UNO_OUTLSYMB ) );
-    xProps->setPropertyValue( sName, uno::makeAny( _bDisplayOutline ));
+    getControllerProps()->setPropertyValue( sName, uno::makeAny( _bDisplayOutline ));
 }
 
 ::sal_Bool SAL_CALL 
 ScVbaWindow::getDisplayVerticalScrollBar() throw (uno::RuntimeException)
 {
-    uno::Reference< beans::XPropertySet > xProps( m_xModel->getCurrentController(), uno::UNO_QUERY_THROW );
     rtl::OUString sName( RTL_CONSTASCII_USTRINGPARAM( SC_UNO_VERTSCROLL ) );
     sal_Bool bVerticalScrollBar = sal_True;
-    xProps->getPropertyValue( sName ) >>= bVerticalScrollBar;
+    getControllerProps()->getPropertyValue( sName ) >>= bVerticalScrollBar;
     return bVerticalScrollBar;	
 }
 
 void SAL_CALL 
 ScVbaWindow::setDisplayVerticalScrollBar( ::sal_Bool _bDisplayVerticalScrollBar ) throw (uno::RuntimeException)
 {
-    uno::Reference< beans::XPropertySet > xProps( m_xModel->getCurrentController(), uno::UNO_QUERY_THROW );
     rtl::OUString sName( RTL_CONSTASCII_USTRINGPARAM( SC_UNO_VERTSCROLL ) );
-    xProps->setPropertyValue( sName, uno::makeAny( _bDisplayVerticalScrollBar ));
+    getControllerProps()->setPropertyValue( sName, uno::makeAny( _bDisplayVerticalScrollBar ));
 }
 
 ::sal_Bool SAL_CALL 
 ScVbaWindow::getDisplayWorkbookTabs() throw (uno::RuntimeException)
 {
-    uno::Reference< beans::XPropertySet > xProps( m_xModel->getCurrentController(), uno::UNO_QUERY_THROW );
     rtl::OUString sName( RTL_CONSTASCII_USTRINGPARAM( SC_UNO_SHEETTABS ) );
     sal_Bool bWorkbookTabs = sal_True;
-    xProps->getPropertyValue( sName ) >>= bWorkbookTabs;
+    getControllerProps()->getPropertyValue( sName ) >>= bWorkbookTabs;
     return bWorkbookTabs;	
 }
 
 void SAL_CALL 
 ScVbaWindow::setDisplayWorkbookTabs( ::sal_Bool _bDisplayWorkbookTabs ) throw (uno::RuntimeException)
 {
-    uno::Reference< beans::XPropertySet > xProps( m_xModel->getCurrentController(), uno::UNO_QUERY_THROW );
     rtl::OUString sName( RTL_CONSTASCII_USTRINGPARAM( SC_UNO_SHEETTABS ) );
-    xProps->setPropertyValue( sName, uno::makeAny( _bDisplayWorkbookTabs ));
+    getControllerProps()->setPropertyValue( sName, uno::makeAny( _bDisplayWorkbookTabs ));
 }
 
 ::sal_Bool SAL_CALL 
 ScVbaWindow::getFreezePanes() throw (uno::RuntimeException)
 {
-    return m_xViewFreezable->hasFrozenPanes();	
+    uno::Reference< sheet::XViewFreezable > xViewFreezable( getController(), uno::UNO_QUERY_THROW );
+    return xViewFreezable->hasFrozenPanes();
 }
 
 void SAL_CALL 
 ScVbaWindow::setFreezePanes( ::sal_Bool /*_bFreezePanes*/ ) throw (uno::RuntimeException)
 {
-    if( m_xViewSplitable->getIsWindowSplit() )
+    uno::Reference< sheet::XViewPane > xViewPane( getController(), uno::UNO_QUERY_THROW );
+    uno::Reference< sheet::XViewSplitable > xViewSplitable( xViewPane, uno::UNO_QUERY_THROW );
+    uno::Reference< sheet::XViewFreezable > xViewFreezable( xViewPane, uno::UNO_QUERY_THROW );
+    if( xViewSplitable->getIsWindowSplit() )
     {
         // if there is a split we freeze at the split
         sal_Int32 nColumn = getSplitColumn();
         sal_Int32 nRow = getSplitRow();
-        m_xViewFreezable->freezeAtPosition( nColumn, nRow );
+        xViewFreezable->freezeAtPosition( nColumn, nRow );
     }
     else
     {
         // otherwise we freeze in the center of the visible sheet	
-        table::CellRangeAddress aCellRangeAddress = m_xViewPane->getVisibleRange();
+        table::CellRangeAddress aCellRangeAddress = xViewPane->getVisibleRange();
         sal_Int32 nColumn = aCellRangeAddress.StartColumn + (( aCellRangeAddress.EndColumn - aCellRangeAddress.StartColumn )/2 );
         sal_Int32 nRow = aCellRangeAddress.StartRow + (( aCellRangeAddress.EndRow - aCellRangeAddress.StartRow )/2 );
-        m_xViewFreezable->freezeAtPosition( nColumn, nRow );	
+        xViewFreezable->freezeAtPosition( nColumn, nRow );
     }
 }
 
 ::sal_Bool SAL_CALL 
 ScVbaWindow::getSplit() throw (uno::RuntimeException)
 {
-    return m_xViewSplitable->getIsWindowSplit();	
+    uno::Reference< sheet::XViewSplitable > xViewSplitable( getController(), uno::UNO_QUERY_THROW );
+    return xViewSplitable->getIsWindowSplit();
 }
 
 void SAL_CALL 
@@ -615,14 +641,16 @@ ScVbaWindow::setSplit( ::sal_Bool _bSplit ) throw (uno::RuntimeException)
 {
     if( !_bSplit )
     {
-        m_xViewSplitable->splitAtPosition(0,0);
+        uno::Reference< sheet::XViewSplitable > xViewSplitable( getController(), uno::UNO_QUERY_THROW );
+        xViewSplitable->splitAtPosition(0,0);
     }
     else
     {
+        uno::Reference< sheet::XViewFreezable > xViewFreezable( getController(), uno::UNO_QUERY_THROW );
         uno::Reference< excel::XRange > xRange = ActiveCell();
         sal_Int32 nRow = xRange->getRow();
         sal_Int32 nColumn = xRange->getColumn();
-        m_xViewFreezable->freezeAtPosition( nColumn-1, nRow-1 );
+        xViewFreezable->freezeAtPosition( nColumn-1, nRow-1 );
         SplitAtDefinedPosition( sal_True );
     }
 }
@@ -630,7 +658,8 @@ ScVbaWindow::setSplit( ::sal_Bool _bSplit ) throw (uno::RuntimeException)
 sal_Int32 SAL_CALL 
 ScVbaWindow::getSplitColumn() throw (uno::RuntimeException)
 {
-    return m_xViewSplitable->getSplitColumn();	
+    uno::Reference< sheet::XViewSplitable > xViewSplitable( getController(), uno::UNO_QUERY_THROW );
+    return xViewSplitable->getSplitColumn();
 }
 
 void SAL_CALL 
@@ -638,9 +667,10 @@ ScVbaWindow::setSplitColumn( sal_Int32 _splitcolumn ) throw (uno::RuntimeExcepti
 {
     if( getSplitColumn() != _splitcolumn )
     {
+        uno::Reference< sheet::XViewFreezable > xViewFreezable( getController(), uno::UNO_QUERY_THROW );
         sal_Bool bFrozen = getFreezePanes();
         sal_Int32 nRow = getSplitRow();
-        m_xViewFreezable->freezeAtPosition( _splitcolumn, nRow );
+        xViewFreezable->freezeAtPosition( _splitcolumn, nRow );
         SplitAtDefinedPosition( !bFrozen );
     }
 }
@@ -648,22 +678,23 @@ ScVbaWindow::setSplitColumn( sal_Int32 _splitcolumn ) throw (uno::RuntimeExcepti
 double SAL_CALL 
 ScVbaWindow::getSplitHorizontal() throw (uno::RuntimeException)
 {
-    double fSplitHorizontal = m_xViewSplitable->getSplitHorizontal();
-    double fHoriPoints = PixelsToPoints( m_xDevice, fSplitHorizontal, sal_True );
-    return fHoriPoints;
+    uno::Reference< sheet::XViewSplitable > xViewSplitable( getController(), uno::UNO_QUERY_THROW );
+    return PixelsToPoints( getDevice(), xViewSplitable->getSplitHorizontal(), sal_True );
 }
 
 void SAL_CALL 
 ScVbaWindow::setSplitHorizontal( double _splithorizontal ) throw (uno::RuntimeException)
 {
-    double fHoriPixels = PointsToPixels( m_xDevice, _splithorizontal, sal_True );
-   m_xViewSplitable->splitAtPosition( static_cast<sal_Int32>( fHoriPixels ), 0 );	
+    uno::Reference< sheet::XViewSplitable > xViewSplitable( getController(), uno::UNO_QUERY_THROW );
+    double fHoriPixels = PointsToPixels( getDevice(), _splithorizontal, sal_True );
+    xViewSplitable->splitAtPosition( static_cast< sal_Int32 >( fHoriPixels ), 0 );
 }
 
 sal_Int32 SAL_CALL 
 ScVbaWindow::getSplitRow() throw (uno::RuntimeException)
 {
-    sal_Int32 nValue = m_xViewSplitable->getSplitRow();
+    uno::Reference< sheet::XViewSplitable > xViewSplitable( getController(), uno::UNO_QUERY_THROW );
+    sal_Int32 nValue = xViewSplitable->getSplitRow();
     return nValue ? nValue - 1 : nValue;	
 }
 
@@ -672,9 +703,10 @@ ScVbaWindow::setSplitRow( sal_Int32 _splitrow ) throw (uno::RuntimeException)
 {
     if( getSplitRow() != _splitrow )
     {
+        uno::Reference< sheet::XViewFreezable > xViewFreezable( getController(), uno::UNO_QUERY_THROW );
         sal_Bool bFrozen = getFreezePanes();
         sal_Int32 nColumn = getSplitColumn();
-        m_xViewFreezable->freezeAtPosition( nColumn , _splitrow );
+        xViewFreezable->freezeAtPosition( nColumn , _splitrow );
         SplitAtDefinedPosition( !bFrozen );
     }
 }
@@ -682,33 +714,33 @@ ScVbaWindow::setSplitRow( sal_Int32 _splitrow ) throw (uno::RuntimeException)
 double SAL_CALL 
 ScVbaWindow::getSplitVertical() throw (uno::RuntimeException)
 {
-    double fSplitVertical = m_xViewSplitable->getSplitVertical();
-    double fVertiPoints = PixelsToPoints( m_xDevice, fSplitVertical, false );	
-    return fVertiPoints;
+    uno::Reference< sheet::XViewSplitable > xViewSplitable( getController(), uno::UNO_QUERY_THROW );
+    return PixelsToPoints( getDevice(), xViewSplitable->getSplitVertical(), sal_False );
 }
 
 void SAL_CALL 
 ScVbaWindow::setSplitVertical(double _splitvertical ) throw (uno::RuntimeException)
 {
-    double fVertiPixels = PointsToPixels( m_xDevice, _splitvertical, false );
-    m_xViewSplitable->splitAtPosition( 0, static_cast<sal_Int32>( fVertiPixels ) );
+    uno::Reference< sheet::XViewSplitable > xViewSplitable( getController(), uno::UNO_QUERY_THROW );
+    double fVertiPixels = PointsToPixels( getDevice(), _splitvertical, sal_False );
+    xViewSplitable->splitAtPosition( 0, static_cast<sal_Int32>( fVertiPixels ) );
 }
 
 void ScVbaWindow::SplitAtDefinedPosition(sal_Bool _bUnFreezePane)
 {
-    sal_Int32 nVertSplit = m_xViewSplitable->getSplitVertical();
-    sal_Int32 nHoriSplit = m_xViewSplitable->getSplitHorizontal();
+    uno::Reference< sheet::XViewSplitable > xViewSplitable( getController(), uno::UNO_QUERY_THROW );
+    uno::Reference< sheet::XViewFreezable > xViewFreezable( xViewSplitable, uno::UNO_QUERY_THROW );
+    sal_Int32 nVertSplit = xViewSplitable->getSplitVertical();
+    sal_Int32 nHoriSplit = xViewSplitable->getSplitHorizontal();
     if( _bUnFreezePane )
-    {
-        m_xViewFreezable->freezeAtPosition(0,0);
-    }
-    m_xViewSplitable->splitAtPosition(nHoriSplit, nVertSplit);
+        xViewFreezable->freezeAtPosition(0,0);
+    xViewSplitable->splitAtPosition(nHoriSplit, nVertSplit);
 }
 
 uno::Any SAL_CALL 
 ScVbaWindow::getZoom() throw (uno::RuntimeException)
 {	
-    uno::Reference< beans::XPropertySet > xProps( m_xModel->getCurrentController(), uno::UNO_QUERY_THROW );
+    uno::Reference< beans::XPropertySet > xProps = getControllerProps();
     rtl::OUString sName( RTL_CONSTASCII_USTRINGPARAM( SC_UNO_ZOOMTYPE ) );
     sal_Int16 nZoomType = view::DocumentZoomType::PAGE_WIDTH;
     xProps->getPropertyValue( sName ) >>= nZoomType;
@@ -773,6 +805,7 @@ ScVbaWindow::setView( const uno::Any& _view) throw (uno::RuntimeException)
         default:
             DebugHelper::exception(SbERR_BAD_PARAMETER, rtl::OUString() );
     }
+    // !! TODO !! get view shell from controller
     ScTabViewShell* pViewShell = excel::getBestViewShell( m_xModel );
     if ( pViewShell )
         dispatchExecute( pViewShell, nSlot );
@@ -781,7 +814,7 @@ ScVbaWindow::setView( const uno::Any& _view) throw (uno::RuntimeException)
 uno::Reference< excel::XRange > SAL_CALL
 ScVbaWindow::getVisibleRange() throw (uno::RuntimeException)
 {
-    uno::Reference< container::XIndexAccess > xPanesIA( m_xViewPane, uno::UNO_QUERY_THROW );
+    uno::Reference< container::XIndexAccess > xPanesIA( getController(), uno::UNO_QUERY_THROW );
     uno::Reference< sheet::XViewPane > xTopLeftPane( xPanesIA->getByIndex( 0 ), uno::UNO_QUERY_THROW );
     uno::Reference< excel::XPane > xPane( new ScVbaPane( this, mxContext, m_xModel, xTopLeftPane ) );
     return xPane->getVisibleRange();
@@ -791,7 +824,7 @@ sal_Int32 SAL_CALL
 ScVbaWindow::PointsToScreenPixelsX(sal_Int32 _points) throw (css::script::BasicErrorException, css::uno::RuntimeException)
 {
     sal_Int32 nHundredthsofOneMillimeters = Millimeter::getInHundredthsOfOneMillimeter( _points );
-    double fConvertFactor = (m_xDevice->getInfo().PixelPerMeterX/100000);
+    double fConvertFactor = (getDevice()->getInfo().PixelPerMeterX/100000);
     return static_cast<sal_Int32>(fConvertFactor * nHundredthsofOneMillimeters );
 }
 
@@ -799,7 +832,7 @@ sal_Int32 SAL_CALL
 ScVbaWindow::PointsToScreenPixelsY(sal_Int32 _points) throw (css::script::BasicErrorException, css::uno::RuntimeException)
 {
     sal_Int32 nHundredthsofOneMillimeters = Millimeter::getInHundredthsOfOneMillimeter( _points );
-    double fConvertFactor = (m_xDevice->getInfo().PixelPerMeterY/100000);
+    double fConvertFactor = (getDevice()->getInfo().PixelPerMeterY/100000);
     return static_cast<sal_Int32>(fConvertFactor * nHundredthsofOneMillimeters );
 }
 
@@ -807,6 +840,7 @@ void SAL_CALL
 ScVbaWindow::PrintOut( const css::uno::Any& From, const css::uno::Any&To, const css::uno::Any& Copies, const css::uno::Any& Preview, const css::uno::Any& ActivePrinter, const css::uno::Any& PrintToFile, const css::uno::Any& Collate, const css::uno::Any& PrToFileName ) throw (css::script::BasicErrorException, css::uno::RuntimeException)
 {
     // need test, print current active sheet	
+    // !! TODO !! get view shell from controller
     PrintOutHelper( excel::getBestViewShell( m_xModel ), From, To, Copies, Preview, ActivePrinter, PrintToFile, Collate, PrToFileName, sal_True );
 }
 
@@ -814,7 +848,8 @@ void SAL_CALL
 ScVbaWindow::PrintPreview( const css::uno::Any& EnableChanges ) throw (css::script::BasicErrorException, css::uno::RuntimeException)
 {
     // need test, print preview current active sheet	
-    PrintPreviewHelper( EnableChanges, excel::getBestViewShell( m_xModel ) );	
+    // !! TODO !! get view shell from controller
+    PrintPreviewHelper( EnableChanges, excel::getBestViewShell( m_xModel ) );
 }
 
 rtl::OUString& 
diff --git a/sc/source/ui/vba/vbawindow.hxx b/sc/source/ui/vba/vbawindow.hxx
index 7daf0ba..2af584d 100644
--- a/sc/source/ui/vba/vbawindow.hxx
+++ b/sc/source/ui/vba/vbawindow.hxx
@@ -28,7 +28,6 @@
 #ifndef SC_VBA_WINDOW_HXX
 #define SC_VBA_WINDOW_HXX
 
-#include <cppuhelper/implbase1.hxx>
 #include <ooo/vba/excel/XWindow.hpp>
 #include <com/sun/star/uno/XComponentContext.hpp>
 #include <com/sun/star/sheet/XViewPane.hpp>
@@ -47,19 +46,30 @@ typedef cppu::ImplInheritanceHelper1< VbaWindowBase, ov::excel::XWindow > Window
 class ScVbaWindow : public WindowImpl_BASE
 {		
 private:		
-    css::uno::Reference< css::sheet::XViewPane > m_xViewPane;
-    css::uno::Reference< css::sheet::XViewFreezable > m_xViewFreezable;
-    css::uno::Reference< css::sheet::XViewSplitable > m_xViewSplitable;
     css::uno::Reference< ov::excel::XPane > m_xPane;
-    css::uno::Reference< css::awt::XDevice > m_xDevice;
+
     void init();
+    css::uno::Reference< css::beans::XPropertySet > getControllerProps() throw (css::uno::RuntimeException);
+    css::uno::Reference< css::beans::XPropertySet > getFrameProps() throw (css::uno::RuntimeException);
+    css::uno::Reference< css::awt::XDevice > getDevice() throw (css::uno::RuntimeException);
+
 protected:
     void SplitAtDefinedPosition(sal_Bool _bUnFreezePane);
+
 public:	
-    void  Scroll( const css::uno::Any& Down, const css::uno::Any& Up, const css::uno::Any& ToRight, const css::uno::Any& ToLeft, bool bLargeScroll = false ) throw (css::uno::RuntimeException);	
+    void Scroll( const css::uno::Any& Down, const css::uno::Any& Up, const css::uno::Any& ToRight, const css::uno::Any& ToLeft, bool bLargeScroll = false ) throw (css::uno::RuntimeException);
+
 public:
-    ScVbaWindow( const css::uno::Reference< ov::XHelperInterface >& xParent, const css::uno::Reference< css::uno::XComponentContext >& xContext, const css::uno::Reference< css::frame::XModel >& xModel );
-    ScVbaWindow( css::uno::Sequence< css::uno::Any > const& aArgs, css::uno::Reference< css::uno::XComponentContext > const& xContext );
+    ScVbaWindow(
+        const css::uno::Reference< ov::XHelperInterface >& xParent,
+        const css::uno::Reference< css::uno::XComponentContext >& xContext,
+        const css::uno::Reference< css::frame::XModel >& xModel,
+        const css::uno::Reference< css::frame::XController >& xController )
+        throw (css::uno::RuntimeException);
+    ScVbaWindow(
+        const css::uno::Sequence< css::uno::Any >& aArgs,
+        const css::uno::Reference< css::uno::XComponentContext >& xContext )
+        throw (css::uno::RuntimeException);
 
     // XWindow
     virtual css::uno::Reference< ov::excel::XRange > SAL_CALL ActiveCell(  ) throw (css::script::BasicErrorException, css::uno::RuntimeException); 
diff --git a/sc/source/ui/vba/vbawindows.cxx b/sc/source/ui/vba/vbawindows.cxx
index 87b6e7a..6ff7bb1 100644
--- a/sc/source/ui/vba/vbawindows.cxx
+++ b/sc/source/ui/vba/vbawindows.cxx
@@ -53,7 +53,9 @@ uno::Reference< XHelperInterface > lcl_createWorkbookHIParent( const uno::Refere
 uno::Any ComponentToWindow( const uno::Any& aSource, uno::Reference< uno::XComponentContext > & xContext, const uno::Any& aApplication )
 {
     uno::Reference< frame::XModel > xModel( aSource, uno::UNO_QUERY_THROW );
-    uno::Reference< excel::XWindow > xWin( new ScVbaWindow( lcl_createWorkbookHIParent( xModel, xContext, aApplication ), xContext,xModel ) );
+    // !! TODO !! iterate over all controllers
+    uno::Reference< frame::XController > xController( xModel->getCurrentController(), uno::UNO_SET_THROW );
+    uno::Reference< excel::XWindow > xWin( new ScVbaWindow( lcl_createWorkbookHIParent( xModel, xContext, aApplication ), xContext, xModel, xController ) );
     return uno::makeAny( xWin );
 }
 
@@ -78,7 +80,7 @@ public:
             m_xContext->getServiceManager(), uno::UNO_QUERY_THROW );
 
         uno::Reference< frame::XDesktop > xDesktop
-            (xSMgr->createInstanceWithContext(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.frame.Desktop")), m_xContext), uno::UNO_QUERY_THROW );
+            (xSMgr->createInstanceWithContext(::rtl::OUString::createFromAscii("com.sun.star.frame.Desktop"), m_xContext), uno::UNO_QUERY_THROW );
         uno::Reference< container::XEnumeration > mxComponents = xDesktop->getComponents()->createEnumeration();
         while( mxComponents->hasMoreElements() )
         {
@@ -138,8 +140,10 @@ public:
             {
                 m_windows.push_back( xNext );
                 uno::Reference< frame::XModel > xModel( xNext, uno::UNO_QUERY_THROW ); // that the spreadsheetdocument is a xmodel is a given
+                // !! TODO !! iterate over all controllers
+                uno::Reference< frame::XController > xController( xModel->getCurrentController(), uno::UNO_SET_THROW );
                 uno::Reference< XHelperInterface > xTemp;  // temporary needed for g++ 3.3.5
-                ScVbaWindow window( xTemp,  m_xContext, xModel );
+                ScVbaWindow window( xTemp, m_xContext, xModel, xController );
                 rtl::OUString sCaption;
                 window.getCaption() >>= sCaption;
                 namesToIndices[ sCaption ] = nIndex++;
@@ -206,6 +210,11 @@ public:
 
 };
 
+
+ScVbaWindows::ScVbaWindows( const uno::Reference< ov::XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext > & xContext, const uno::Reference< container::XIndexAccess >& xIndexAccess  ):  ScVbaWindows_BASE( xParent, xContext, xIndexAccess )
+{
+}
+
 ScVbaWindows::ScVbaWindows( const uno::Reference< ov::XHelperInterface >& xParent, const css::uno::Reference< css::uno::XComponentContext >& xContext ) : ScVbaWindows_BASE( xParent, xContext, uno::Reference< container::XIndexAccess > ( new WindowsAccessImpl( xContext ) ) )
 {
 }
diff --git a/sc/source/ui/vba/vbawindows.hxx b/sc/source/ui/vba/vbawindows.hxx
index aef636f..71d5f7c 100644
--- a/sc/source/ui/vba/vbawindows.hxx
+++ b/sc/source/ui/vba/vbawindows.hxx
@@ -42,7 +42,7 @@ typedef CollTestImplHelper< ov::excel::XWindows > ScVbaWindows_BASE;
 class ScVbaWindows : public ScVbaWindows_BASE
 {
 public:
-    //]AE] ScVbaWindows( const css::uno::Reference< ov::XHelperInterface >& xParent, const css::uno::Reference< css::uno::XComponentContext > & xContext, const css::uno::Reference< css::container::XIndexAccess >& xIndexAccess );
+    ScVbaWindows( const css::uno::Reference< ov::XHelperInterface >& xParent, const css::uno::Reference< css::uno::XComponentContext > & xContext, const css::uno::Reference< css::container::XIndexAccess >& xIndexAccess );
     ScVbaWindows( const css::uno::Reference< ov::XHelperInterface >& xParent, const css::uno::Reference< css::uno::XComponentContext > & xContext );
     virtual ~ScVbaWindows() {}
 
commit fbedaa2bd5029d248905a5a9e72098115bd0414d
Author: Kohei Yoshida <kyoshida at novell.com>
Date:   Tue Apr 26 12:23:45 2011 -0400

    n#689442: Don't forget to create undo object for normal merging.

diff --git a/sc/source/ui/docshell/docfunc.cxx b/sc/source/ui/docshell/docfunc.cxx
index 11259cc..30551cf 100644
--- a/sc/source/ui/docshell/docfunc.cxx
+++ b/sc/source/ui/docshell/docfunc.cxx
@@ -4344,17 +4344,14 @@ sal_Bool ScDocFunc::MergeCells( const ScCellMergeOption& rOption, sal_Bool bCont
                 for( aPos.SetRow( nStartRow ); !bHasNotes && (aPos.Row() <= nEndRow); aPos.IncRow() )
                     bHasNotes = ((aPos.Col() != nStartCol) || (aPos.Row() != nStartRow)) && (pDoc->GetNote( aPos ) != 0);
 
-            if (bNeedContents || bHasNotes || rOption.mbCenter)
+            if (!pUndoDoc)
             {
-                if (!pUndoDoc)
-                {
-                    pUndoDoc = new ScDocument( SCDOCMODE_UNDO );
-                    pUndoDoc->InitUndo(pDoc, nTab1, nTab2);
-                }
-                // note captions are collected by drawing undo
-                pDoc->CopyToDocument( nStartCol, nStartRow, nTab, nEndCol, nEndRow, nTab,
-                                      IDF_ALL|IDF_NOCAPTIONS, false, pUndoDoc );
+                pUndoDoc = new ScDocument( SCDOCMODE_UNDO );
+                pUndoDoc->InitUndo(pDoc, nTab1, nTab2);
             }
+            // note captions are collected by drawing undo
+            pDoc->CopyToDocument( nStartCol, nStartRow, nTab, nEndCol, nEndRow, nTab,
+                                  IDF_ALL|IDF_NOCAPTIONS, false, pUndoDoc );
             if( bHasNotes )
                 pDoc->BeginDrawUndo();
         }
commit ff54609c5a4e0132c371960218881e1f75b813c7
Merge: af42d3e... c636c05...
Author: Kohei Yoshida <kyoshida at novell.com>
Date:   Mon Apr 25 15:21:51 2011 -0400

    Merge commit 'ooo/DEV300_m106' into m106

commit c636c055d6f19d6ec5cb472eccb8feb415e1c706
Merge: aed5d1b... 2f2e0ba...
Author: Ivo Hinkelmann <ihi at openoffice.org>
Date:   Fri Apr 1 09:34:55 2011 +0200

    CWS-TOOLING: integrate CWS calcvba

commit aed5d1b0747fb20a84b7090aece52cc545a3f6bc
Merge: fae7a58... 65f59ac...
Author: Ivo Hinkelmann <ihi at openoffice.org>
Date:   Tue Mar 29 13:19:46 2011 +0200

    CWS-TOOLING: integrate CWS obo56

commit fae7a5819bfdbec837b7eec7c0db2685bc1648e9
Merge: 9c89ede... 73f25b3...
Author: Ivo Hinkelmann <ihi at openoffice.org>
Date:   Mon Mar 28 17:00:24 2011 +0200

    CWS-TOOLING: integrate CWS fwk167

commit 9c89edefe65ed273499d43e5bc43e281251cec0f
Merge: 446efb5... 1ebc82c...
Author: Ivo Hinkelmann <ihi at openoffice.org>
Date:   Mon Mar 28 15:56:18 2011 +0200

    CWS-TOOLING: integrate CWS calc66

diff --cc chart2/source/view/axes/ScaleAutomatism.cxx
index c009cfb,f0c4f26..1ba6429
--- a/chart2/source/view/axes/ScaleAutomatism.cxx
+++ b/chart2/source/view/axes/ScaleAutomatism.cxx
@@@ -699,21 -692,8 +699,21 @@@ void ScaleAutomatism::calculateExplicit
          nNumer = static_cast<sal_Int32>( rtl::math::approxCeil( nIntervalDays/nDaysPerInterval ) );
          if(nNumer<=0)
              nNumer=1;
 +        if( rExplicitIncrement.MajorTimeInterval.TimeUnit == DAY )
 +        {
 +            if( nNumer>2 && nNumer<7 )
 +                nNumer=7;
 +            else if( nNumer>7 )
 +            {
 +                rExplicitIncrement.MajorTimeInterval.TimeUnit = MONTH;
 +                nDaysPerInterval = 31.0;
 +                nNumer = static_cast<sal_Int32>( rtl::math::approxCeil( nIntervalDays/nDaysPerInterval ) );
 +                if(nNumer<=0)
 +                    nNumer=1;
 +            }
 +        }
          rExplicitIncrement.MajorTimeInterval.Number = nNumer;
-         nMainIncrementCount = nDayCount/(nNumer*nDaysPerInterval);
+         nMainIncrementCount = static_cast<long>(nDayCount/(nNumer*nDaysPerInterval));
      }
  
      //choose minor time interval:
diff --cc chart2/source/view/charttypes/VSeriesPlotter.cxx
index fe051a4,fdad58b..40f28a4
mode 100755,100644..100644
--- a/chart2/source/view/charttypes/VSeriesPlotter.cxx
+++ b/chart2/source/view/charttypes/VSeriesPlotter.cxx
diff --cc chart2/source/view/main/VLegend.cxx
index 0a3cfd4,f05c0fa..f05c0fa
mode 100755,100644..100755
--- a/chart2/source/view/main/VLegend.cxx
+++ b/chart2/source/view/main/VLegend.cxx
commit 446efb5a0fe07ef9d90e730730b3087906d4d4cb
Merge: 7190faf... bfb3e27...
Author: Ivo Hinkelmann <ihi at openoffice.org>
Date:   Mon Mar 28 15:37:38 2011 +0200

    CWS-TOOLING: integrate CWS dba34d

commit 2f2e0baecd15ced6a57fc9c58c6c80d7dda35754
Merge: 693beaf... 7190faf...
Author: Daniel Rentz [dr] <daniel.rentz at oracle.com>
Date:   Fri Mar 25 13:58:04 2011 +0100

    calcvba: rebase to DEV300_m104

commit 693beaf5afd2202872be1cc302531ef4f304d148
Author: Daniel Rentz [dr] <daniel.rentz at oracle.com>
Date:   Fri Mar 25 10:40:25 2011 +0100

    calcvba: #164410# improve VBA compatibility implementation in various areas: Excel symbols, MSForms symbols, document and forms event handling

diff --git a/sc/source/ui/docshell/docsh.cxx b/sc/source/ui/docshell/docsh.cxx
index e2b8714..4e358ed 100644
--- a/sc/source/ui/docshell/docsh.cxx
+++ b/sc/source/ui/docshell/docsh.cxx
@@ -498,74 +498,23 @@ sal_Bool __EXPORT ScDocShell::Load( SfxMedium& rMedium )
 
 void __EXPORT ScDocShell::Notify( SfxBroadcaster&, const SfxHint& rHint )
 {
-    uno::Reference< script::vba::XVBAEventProcessor > xVbaEvents = aDocument.GetVbaEventProcessor();
-    if ( xVbaEvents.is() ) try
+    if (rHint.ISA(ScTablesHint) )
     {
-        using namespace ::com::sun::star::script::vba::VBAEventId;
-        if (rHint.ISA(ScTablesHint) )
+        const ScTablesHint& rScHint = static_cast< const ScTablesHint& >( rHint );
+        if (rScHint.GetId() == SC_TAB_INSERTED)
         {
-            const ScTablesHint& rScHint = static_cast< const ScTablesHint& >( rHint );
-            if (rScHint.GetId() == SC_TAB_INSERTED)
+            uno::Reference< script::vba::XVBAEventProcessor > xVbaEvents = aDocument.GetVbaEventProcessor();
+            if ( xVbaEvents.is() ) try
             {
                 uno::Sequence< uno::Any > aArgs( 1 );
                 aArgs[0] <<= rScHint.GetTab1();
-                xVbaEvents->processVbaEvent( WORKBOOK_NEWSHEET, aArgs );
+                xVbaEvents->processVbaEvent( script::vba::VBAEventId::WORKBOOK_NEWSHEET, aArgs );
             }
-        }
-        else if ( rHint.ISA( SfxEventHint ) )
-        {
-            sal_uLong nEventId = static_cast< const SfxEventHint& >( rHint ).GetEventId();
-            switch ( nEventId )
+            catch( uno::Exception& )
             {
-                case SFX_EVENT_ACTIVATEDOC:
-                {
-                    uno::Sequence< uno::Any > aArgs;
-                    xVbaEvents->processVbaEvent( WORKBOOK_ACTIVATE, aArgs );
-                }
-                break;
-                case SFX_EVENT_DEACTIVATEDOC:
-                {
-                    uno::Sequence< uno::Any > aArgs;
-                    xVbaEvents->processVbaEvent( WORKBOOK_DEACTIVATE, aArgs );
-                }
-                break;
-                case SFX_EVENT_OPENDOC:
-                {
-                    uno::Sequence< uno::Any > aArgs;
-                    xVbaEvents->processVbaEvent( WORKBOOK_OPEN, aArgs );
-                }
-                break;
-                case SFX_EVENT_SAVEDOCDONE:
-                case SFX_EVENT_SAVEASDOCDONE:
-                case SFX_EVENT_SAVETODOCDONE:
-                {
-                    uno::Sequence< uno::Any > aArgs( 1 );
-                    aArgs[ 0 ] <<= true;
-                    xVbaEvents->processVbaEvent( WORKBOOK_AFTERSAVE, aArgs );
-                }
-                break;
-                case SFX_EVENT_SAVEASDOCFAILED:
-                case SFX_EVENT_SAVEDOCFAILED:
-                case SFX_EVENT_SAVETODOCFAILED:
-                {
-                    uno::Sequence< uno::Any > aArgs( 1 );
-                    aArgs[ 0 ] <<= false;
-                    xVbaEvents->processVbaEvent( WORKBOOK_AFTERSAVE, aArgs );
-                }
-                break;
-                case SFX_EVENT_CLOSEDOC:
-                {
-                    // #163655# prevent event processing after model is disposed
-                    aDocument.SetVbaEventProcessor( uno::Reference< script::vba::XVBAEventProcessor >() );
-                    uno::Reference< lang::XEventListener >( xVbaEvents, uno::UNO_QUERY_THROW )->disposing( lang::EventObject() );
-                }
-                break;
             }
         }
     }
-    catch( uno::Exception& )
-    {
-    }
 
     if (rHint.ISA(SfxSimpleHint))								// ohne Parameter
     {
diff --git a/sc/source/ui/unoobj/docuno.cxx b/sc/source/ui/unoobj/docuno.cxx
index 4f9485e..3fbf1f4 100644
--- a/sc/source/ui/unoobj/docuno.cxx
+++ b/sc/source/ui/unoobj/docuno.cxx
@@ -103,6 +103,9 @@
 
 using namespace com::sun::star;
 
+// #i111553# provides the name of the VBA constant for this document type (e.g. 'ThisExcelDoc' for Calc)
+#define SC_UNO_VBAGLOBNAME "VBAGlobalConstantName"
+
 //------------------------------------------------------------------------
 
 //	alles ohne Which-ID, Map nur fuer PropertySetInfo
@@ -117,6 +120,7 @@ const SfxItemPropertyMapEntry* lcl_GetDocOptPropertyMap()
         {MAP_CHAR_LEN(SC_UNO_AUTOCONTFOC),       0, &getBooleanCppuType(),                                    0, 0},
         {MAP_CHAR_LEN(SC_UNO_BASICLIBRARIES),    0, &getCppuType((uno::Reference< script::XLibraryContainer >*)0), beans::PropertyAttribute::READONLY, 0},
         {MAP_CHAR_LEN(SC_UNO_DIALOGLIBRARIES),   0, &getCppuType((uno::Reference< script::XLibraryContainer >*)0), beans::PropertyAttribute::READONLY, 0},
+        {MAP_CHAR_LEN(SC_UNO_VBAGLOBNAME),       0, &getCppuType(static_cast< const rtl::OUString * >(0)),    beans::PropertyAttribute::READONLY, 0},
         {MAP_CHAR_LEN(SC_UNO_CALCASSHOWN),       PROP_UNO_CALCASSHOWN, &getBooleanCppuType(),                                    0, 0},
         {MAP_CHAR_LEN(SC_UNONAME_CLOCAL),        0, &getCppuType((lang::Locale*)0),                           0, 0},
         {MAP_CHAR_LEN(SC_UNO_CJK_CLOCAL),        0, &getCppuType((lang::Locale*)0),                           0, 0},
@@ -1893,6 +1897,15 @@ uno::Any SAL_CALL ScModelObj::getPropertyValue( const rtl::OUString& aPropertyNa
         {
             aRet <<= pDocShell->GetDialogContainer();
         }
+        else if ( aString.EqualsAscii( SC_UNO_VBAGLOBNAME ) )
+        {
+            /*  #i111553# This property provides the name of the constant that
+                will be used to store this model in the global Basic manager.
+                That constant will be equivelant to 'ThisComponent' but for
+                each application, so e.g. a 'ThisExcelDoc' and a 'ThisWordDoc'
+                constant can co-exist, as required by VBA. */
+            aRet <<= rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "ThisExcelDoc" ) );
+        }
         else if ( aString.EqualsAscii( SC_UNO_RUNTIMEUID ) )
         {
             aRet <<= getRuntimeUID();
diff --git a/sc/source/ui/vba/excelvbahelper.cxx b/sc/source/ui/vba/excelvbahelper.cxx
index 436defe..8997db6 100644
--- a/sc/source/ui/vba/excelvbahelper.cxx
+++ b/sc/source/ui/vba/excelvbahelper.cxx
@@ -247,7 +247,7 @@ getViewFrame( const uno::Reference< frame::XModel >& xModel )
     return NULL;
 }
 
-uno::Reference< vba::XHelperInterface >
+uno::Reference< XHelperInterface >
 getUnoSheetModuleObj( const uno::Reference< sheet::XSpreadsheet >& xSheet ) throw ( uno::RuntimeException )
 {
     uno::Reference< beans::XPropertySet > xProps( xSheet, uno::UNO_QUERY_THROW );
@@ -258,8 +258,7 @@ getUnoSheetModuleObj( const uno::Reference< sheet::XSpreadsheet >& xSheet ) thro
     // are *NO* special document module objects ( of course being able to switch between vba/non vba mode at
     // the document in the future could fix this, especially IF the switching of the vba mode takes care to
     // create the special document module objects if they don't exist.
-    uno::Reference< XHelperInterface > xParent( ov::getUnoDocModule( sCodeName, GetDocShellFromRange( xSheet ) ), uno::UNO_QUERY );
-    return xParent;
+    return getUnoDocModule( sCodeName, GetDocShellFromRange( xSheet ) );
 }
 
 uno::Reference< XHelperInterface >
diff --git a/sc/source/ui/vba/vbaapplication.cxx b/sc/source/ui/vba/vbaapplication.cxx
index c87aee0..f75ce1e 100644
--- a/sc/source/ui/vba/vbaapplication.cxx
+++ b/sc/source/ui/vba/vbaapplication.cxx
@@ -40,6 +40,7 @@
 #include <ooo/vba/excel/XlMousePointer.hpp>
 #include <com/sun/star/sheet/XNamedRanges.hpp>
 #include <com/sun/star/sheet/XCellRangeAddressable.hpp>
+#include <ooo/vba/XExecutableDialog.hpp>
 
 #include "vbaapplication.hxx"
 #include "vbaworkbooks.hxx"
@@ -98,17 +99,6 @@ using namespace ::com::sun::star;
 
 uno::Any sbxToUnoValue( SbxVariable* pVar );
 
-class ActiveWorkbook : public ScVbaWorkbook
-{
-protected:
-    virtual uno::Reference< frame::XModel > getModel()
-    { 	
-        return getCurrentExcelDoc(mxContext); 
-    }
-public:
-    ActiveWorkbook( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext >& xContext) : ScVbaWorkbook(  xParent, xContext ){}
-};
-
 // ============================================================================
 
 /** Global application settings shared by all open workbooks. */
@@ -215,47 +205,15 @@ ScVbaApplication::hasProperty( const ::rtl::OUString& Name ) throw(uno::RuntimeE
 uno::Reference< excel::XWorkbook >
 ScVbaApplication::getActiveWorkbook() throw (uno::RuntimeException)
 {
-    uno::Reference< excel::XWorkbook > xWrkbk;
-    ScDocShell* pShell = excel::getDocShell( getCurrentExcelDoc( mxContext ) );
-    if ( pShell )
-    {
-        String aName;
-        if ( pShell->GetDocument() )
-        {
-            aName = pShell->GetDocument()->GetCodeName();
-            xWrkbk.set( getUnoDocModule(  aName, pShell ), uno::UNO_QUERY );
-            // fallback ( e.g. it's possible a new document was created via the api )
-            // in that case the document will not have the appropriate Document Modules
-            // #TODO #FIXME ( needs to be fixes as part of providing support for an overall document 
-            // vba mode etc. )
-            if ( !xWrkbk.is() )
-                return new ActiveWorkbook( this, mxContext );
-        }
-    }
-    return xWrkbk; 
+    // will throw if active document is not in VBA compatibility mode (no object for codename)
+    return uno::Reference< excel::XWorkbook >( getVBADocument( getCurrentExcelDoc( mxContext ) ), uno::UNO_QUERY_THROW );
 }
 
 uno::Reference< excel::XWorkbook > SAL_CALL 
 ScVbaApplication::getThisWorkbook() throw (uno::RuntimeException)
 {
-    uno::Reference< excel::XWorkbook > xWrkbk;
-    ScDocShell* pShell = excel::getDocShell( getThisExcelDoc( mxContext ) );
-    if ( pShell )
-    {
-        String aName;
-        if ( pShell->GetDocument() )
-        {
-            aName = pShell->GetDocument()->GetCodeName();
-            xWrkbk.set( getUnoDocModule( aName, pShell ), uno::UNO_QUERY );
-            // fallback ( e.g. it's possible a new document was created via the api )
-            // in that case the document will not have the appropriate Document Modules
-            // #TODO #FIXME ( needs to be fixes as part of providing support for an overall document 
-            // vba mode etc. )
-            if ( !xWrkbk.is() )
-                return new ActiveWorkbook( this, mxContext );
-        }
-    }
-    return xWrkbk;
+    // should never throw as this model is in VBA compatibility mode
+    return uno::Reference< excel::XWorkbook >( getVBADocument( getThisExcelDoc( mxContext ) ), uno::UNO_QUERY_THROW );
 }
 
 uno::Reference< XAssistant > SAL_CALL
@@ -343,17 +301,8 @@ ScVbaApplication::Workbooks( const uno::Any& aIndex ) throw (uno::RuntimeExcepti
 uno::Any SAL_CALL
 ScVbaApplication::Worksheets( const uno::Any& aIndex ) throw (uno::RuntimeException)
 {
-    uno::Reference< excel::XWorkbook > xWorkbook( getActiveWorkbook(), uno::UNO_QUERY );
-        uno::Any result;
-    if ( xWorkbook.is() )
-        result  = xWorkbook->Worksheets( aIndex );
-
-    else
-        // Fixme - check if this is reasonable/desired behavior
-        throw uno::RuntimeException( rtl::OUString::createFromAscii(
-            "No ActiveWorkBook available" ), uno::Reference< uno::XInterface >() );
-
-    return result;
+    uno::Reference< excel::XWorkbook > xWorkbook( getActiveWorkbook(), uno::UNO_SET_THROW );
+    return xWorkbook->Worksheets( aIndex );
 }
 
 uno::Any SAL_CALL
@@ -384,10 +333,10 @@ ScVbaApplication::Dialogs( const uno::Any &aIndex ) throw (uno::RuntimeException
 uno::Reference< excel::XWindow > SAL_CALL 
 ScVbaApplication::getActiveWindow() throw (uno::RuntimeException)
 {
-    // #FIXME sofar can't determine Parent
     uno::Reference< frame::XModel > xModel = getCurrentDocument();
-    ScVbaWindow* pWin = new ScVbaWindow( uno::Reference< XHelperInterface >(), mxContext, xModel );
-    uno::Reference< excel::XWindow > xWin( pWin );
+    uno::Reference< frame::XController > xController( xModel->getCurrentController(), uno::UNO_SET_THROW );
+    uno::Reference< XHelperInterface > xParent( getActiveWorkbook(), uno::UNO_QUERY_THROW );
+    uno::Reference< excel::XWindow > xWin( new ScVbaWindow( xParent, mxContext, xModel, xController ) );
     return xWin;
 }
 
@@ -792,37 +741,36 @@ rtl::OUString ScVbaApplication::getOfficePath( const rtl::OUString& _sPathType )
     }		
     return sRetPath;
 }
+
 void SAL_CALL 
-ScVbaApplication::setDefaultFilePath( const ::rtl::OUString& DefaultFilePath ) throw (script::BasicErrorException, uno::RuntimeException)
+ScVbaApplication::setDefaultFilePath( const ::rtl::OUString& DefaultFilePath ) throw (uno::RuntimeException)
 {
     uno::Reference< beans::XPropertySet > xProps = lcl_getPathSettingsService( mxContext );
     rtl::OUString aURL;
     osl::FileBase::getFileURLFromSystemPath( DefaultFilePath, aURL );
-    xProps->setPropertyValue(  rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Work")), uno::makeAny( aURL ) );
-
-    
+    xProps->setPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Work")), uno::Any( aURL ) );
 }
 
 ::rtl::OUString SAL_CALL 
-ScVbaApplication::getDefaultFilePath(  ) throw (script::BasicErrorException, uno::RuntimeException)
+ScVbaApplication::getDefaultFilePath() throw (uno::RuntimeException)
 {
     return getOfficePath( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Work")));
 }
 
 ::rtl::OUString SAL_CALL 
-ScVbaApplication::LibraryPath(  ) throw (script::BasicErrorException, uno::RuntimeException)
+ScVbaApplication::getLibraryPath() throw (uno::RuntimeException)
 {
     return getOfficePath( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Basic")));
 }
 
 ::rtl::OUString SAL_CALL 
-ScVbaApplication::TemplatesPath(  ) throw (script::BasicErrorException, uno::RuntimeException)
+ScVbaApplication::getTemplatesPath() throw (uno::RuntimeException)
 {
     return getOfficePath( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Template")));
 }
 
 ::rtl::OUString SAL_CALL 
-ScVbaApplication::PathSeparator(  ) throw (script::BasicErrorException, uno::RuntimeException)
+ScVbaApplication::getPathSeparator() throw (uno::RuntimeException)
 {
     static rtl::OUString sPathSep( RTL_CONSTASCII_USTRINGPARAM( FILE_PATH_SEPERATOR ) );
     return sPathSep;
@@ -1174,13 +1122,47 @@ ScVbaApplication::Caller( const uno::Any& /*aIndex*/ ) throw ( uno::RuntimeExcep
     return aRet;
 }
 
-uno::Reference< frame::XModel > 
+uno::Any SAL_CALL ScVbaApplication::GetOpenFilename(
+        const uno::Any& rFileFilter, const uno::Any& rFilterIndex, const uno::Any& rTitle,
+        const uno::Any& rButtonText, const uno::Any& rMultiSelect ) throw (uno::RuntimeException)
+{
+    uno::Sequence< uno::Any > aArgs( 6 );
+    aArgs[ 0 ] <<= getThisExcelDoc( mxContext );
+    aArgs[ 1 ] = rFileFilter;
+    aArgs[ 2 ] = rFilterIndex;
+    aArgs[ 3 ] = rTitle;
+    aArgs[ 4 ] = rButtonText;
+    aArgs[ 5 ] = rMultiSelect;

... etc. - the rest is truncated


More information about the Libreoffice-commits mailing list