From 1acf64e7fc0386fabe02059c9fe80f0b9d767abc Mon Sep 17 00:00:00 2001
From: Markus Mohrhard <markus.mohrhard@googlemail.com>
Date: Thu, 21 Apr 2011 22:01:59 +0200
Subject: [PATCH] make VbaWorksheet Autofilter work with sheet local DBData

---
 sc/inc/document.hxx               |    4 +-
 sc/source/ui/vba/vbaworksheet.cxx |   55 +++++++++++++++++++-----------------
 sc/source/ui/vba/vbaworksheet.hxx |    2 -
 3 files changed, 31 insertions(+), 30 deletions(-)

diff --git a/sc/inc/document.hxx b/sc/inc/document.hxx
index b58782c..43a989a 100644
--- a/sc/inc/document.hxx
+++ b/sc/inc/document.hxx
@@ -529,7 +529,7 @@ public:
     SC_DLLPUBLIC sal_Bool			GetTable( const String& rName, SCTAB& rTab ) const;
 
     void            SetAnonymousDBData(SCTAB nTab, ScDBData* pDBData);
-    ScDBData*       GetAnonymousDBData(SCTAB nTab);
+    SC_DLLPUBLIC ScDBData*       GetAnonymousDBData(SCTAB nTab);
 
     SC_DLLPUBLIC inline SCTAB	GetTableCount() const { return nMaxTableNumber; }
     SvNumberFormatterIndexTable* GetFormatExchangeList() const { return pFormatExchangeList; }
@@ -1238,7 +1238,7 @@ public:
     SC_DLLPUBLIC sal_Bool			ApplyFlagsTab( SCCOL nStartCol, SCROW nStartRow,
                                             SCCOL nEndCol, SCROW nEndRow,
                                             SCTAB nTab, sal_Int16 nFlags );
-    sal_Bool			RemoveFlagsTab( SCCOL nStartCol, SCROW nStartRow,
+    SC_DLLPUBLIC sal_Bool			RemoveFlagsTab( SCCOL nStartCol, SCROW nStartRow,
                                             SCCOL nEndCol, SCROW nEndRow,
                                             SCTAB nTab, sal_Int16 nFlags );
 
diff --git a/sc/source/ui/vba/vbaworksheet.cxx b/sc/source/ui/vba/vbaworksheet.cxx
index f3c5bb1..8346c29 100644
--- a/sc/source/ui/vba/vbaworksheet.cxx
+++ b/sc/source/ui/vba/vbaworksheet.cxx
@@ -101,6 +101,8 @@
 #include "vbasheetobjects.hxx"
 #include "viewuno.hxx"
 
+#include "attrib.hxx"
+
 #define STANDARDWIDTH 2267
 #define STANDARDHEIGHT 427
 #define DOESNOTEXIST -1
@@ -434,38 +436,39 @@ ScVbaWorksheet::setEnableSelection( sal_Int32 nSelection ) throw (uno::RuntimeEx
 
 }
 
-uno::Reference< beans::XPropertySet > ScVbaWorksheet::getFirstDBRangeProperties() throw (uno::RuntimeException)
-{
-    uno::Reference< beans::XPropertySet > xModelProps( mxModel, uno::UNO_QUERY_THROW );
-    uno::Reference< container::XIndexAccess > xDBRangesIA( xModelProps->getPropertyValue(
-        ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "DatabaseRanges" ) ) ), uno::UNO_QUERY_THROW );
-
-    for( sal_Int32 nIndex = 0, nCount = xDBRangesIA->getCount(); nIndex < nCount; ++nIndex )
-    {
-        uno::Reference< sheet::XCellRangeReferrer > xDBRange( xDBRangesIA->getByIndex( nIndex ), uno::UNO_QUERY_THROW );
-        // check if the database area is on this sheet
-        uno::Reference< sheet::XCellRangeAddressable > xRangeAddr( xDBRange->getReferredCells(), uno::UNO_QUERY_THROW );
-        if( getSheetID() == xRangeAddr->getRangeAddress().Sheet )
-            return uno::Reference< beans::XPropertySet >( xDBRange, uno::UNO_QUERY_THROW );
-    }
-    return uno::Reference< beans::XPropertySet >();
-}
-
 sal_Bool SAL_CALL ScVbaWorksheet::getAutoFilterMode() throw (uno::RuntimeException)
 {
-    uno::Reference< beans::XPropertySet > xDBRangeProps = getFirstDBRangeProperties();
-    sal_Bool bAutoFilterMode = false;
-    return
-        xDBRangeProps.is() &&
-        (xDBRangeProps->getPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "AutoFilter" ) ) ) >>= bAutoFilterMode) &&
-        bAutoFilterMode;
+    uno::Reference< frame::XModel > xModel( getModel(), uno::UNO_QUERY_THROW );
+    ScDocument* pDoc = excel::getDocShell( xModel )->GetDocument();
+    ScDBData* pDBData = pDoc->GetAnonymousDBData(getSheetID());
+    if (pDBData)
+        return pDBData->HasAutoFilter();
+    return false;
 }
 
 void SAL_CALL ScVbaWorksheet::setAutoFilterMode( sal_Bool bAutoFilterMode ) throw (uno::RuntimeException)
 {
-    uno::Reference< beans::XPropertySet > xDBRangeProps = getFirstDBRangeProperties();
-    if( xDBRangeProps.is() )
-        xDBRangeProps->setPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "AutoFilter" ) ), uno::Any( bAutoFilterMode ) );
+    uno::Reference< frame::XModel > xModel( getModel(), uno::UNO_QUERY_THROW );
+    ScDocShell* pDocShell = excel::getDocShell( xModel );
+    ScDocument* pDoc = pDocShell->GetDocument();
+    ScDBData* pDBData = pDoc->GetAnonymousDBData(getSheetID());
+    if (pDBData)
+    {
+        pDBData->SetAutoFilter(bAutoFilterMode);
+        ScRange aRange;
+        pDBData->GetArea(aRange);
+        if (bAutoFilterMode && pDoc)
+            pDoc->ApplyFlagsTab( aRange.aStart.Col(), aRange.aStart.Row(),
+                                    aRange.aEnd.Col(), aRange.aStart.Row(),
+                                    aRange.aStart.Tab(), SC_MF_AUTO );
+        else  if (!bAutoFilterMode && pDoc)
+            pDoc->RemoveFlagsTab(aRange.aStart.Col(), aRange.aStart.Row(),
+                                    aRange.aEnd.Col(), aRange.aStart.Row(),
+                                    aRange.aStart.Tab(), SC_MF_AUTO );
+        ScRange aPaintRange(aRange.aStart, aRange.aEnd);
+        aPaintRange.aEnd.SetRow(aPaintRange.aStart.Row());
+        pDocShell->PostPaint(aPaintRange, PAINT_GRID);
+    }
 }
 
 uno::Reference< excel::XRange >
diff --git a/sc/source/ui/vba/vbaworksheet.hxx b/sc/source/ui/vba/vbaworksheet.hxx
index c9a2425..79f5455 100644
--- a/sc/source/ui/vba/vbaworksheet.hxx
+++ b/sc/source/ui/vba/vbaworksheet.hxx
@@ -72,8 +72,6 @@ class ScVbaWorksheet : public WorksheetImpl_BASE
     css::uno::Reference< css::container::XNameAccess > getFormControls();
     css::uno::Any getControlShape( const rtl::OUString& sName );
     
-    css::uno::Reference< css::beans::XPropertySet > getFirstDBRangeProperties() throw (css::uno::RuntimeException);
-    
 protected:
 
     ScVbaWorksheet( const css::uno::Reference< ov::XHelperInterface >& xParent,  const css::uno::Reference< css::uno::XComponentContext >& xContext );
-- 
1.7.2.5

