a2a144e
From 24578b804007d8c3201e5ed32b8485e1725c33c1 Mon Sep 17 00:00:00 2001
a2a144e
From: David Tardon <dtardon@redhat.com>
a2a144e
Date: Thu, 17 Jan 2013 15:01:32 +0100
a2a144e
Subject: [PATCH] rhbz#760765 copy custom styles on copy & paste
a2a144e
a2a144e
Change-Id: Icaacf3bc1a02a017692432aec36aba06d3f5dde5
a2a144e
---
a2a144e
 sd/inc/stlpool.hxx          |  3 +++
a2a144e
 sd/source/core/drawdoc3.cxx | 49 ++++++++++++++++++++++++++++++++++++++++++---
a2a144e
 sd/source/core/stlpool.cxx  | 18 +++++++++++++++++
a2a144e
 sd/source/core/stlsheet.cxx | 16 +++++++++------
a2a144e
 4 files changed, 77 insertions(+), 9 deletions(-)
a2a144e
a2a144e
diff --git a/sd/inc/stlpool.hxx b/sd/inc/stlpool.hxx
a2a144e
index 9e76c2a..4833dd8 100644
a2a144e
--- a/sd/inc/stlpool.hxx
a2a144e
+++ b/sd/inc/stlpool.hxx
a2a144e
@@ -80,6 +80,8 @@ public:
a2a144e
     void                CopyGraphicSheets(SdStyleSheetPool& rSourcePool);
a2a144e
     void                CopyCellSheets(SdStyleSheetPool& rSourcePool);
a2a144e
     void                CopyTableStyles(SdStyleSheetPool& rSourcePool);
a2a144e
+    void                CopyGraphicSheets(SdStyleSheetPool& rSourcePool, SdStyleSheetVector& rCreatedSheets);
a2a144e
+    void                CopyCellSheets(SdStyleSheetPool& rSourcePool, SdStyleSheetVector& rCreatedSheets);
a2a144e
 
a2a144e
     void                CreatePseudosIfNecessary();
a2a144e
     void                UpdateStdNames();
a2a144e
@@ -121,6 +123,7 @@ public:
a2a144e
     virtual void SAL_CALL release (void) throw ();
a2a144e
 protected:
a2a144e
     void CopySheets(SdStyleSheetPool& rSourcePool, SfxStyleFamily eFamily );
a2a144e
+    void CopySheets(SdStyleSheetPool& rSourcePool, SfxStyleFamily eFamily, SdStyleSheetVector& rCreatedSheets );
a2a144e
 
a2a144e
     virtual SfxStyleSheetBase* Create(const String& rName, SfxStyleFamily eFamily, sal_uInt16 nMask);
a2a144e
     virtual SfxStyleSheetBase* Create(const SdStyleSheet& rStyle);
a2a144e
diff --git a/sd/source/core/drawdoc3.cxx b/sd/source/core/drawdoc3.cxx
a2a144e
index e7ac380..b18c254 100644
a2a144e
--- a/sd/source/core/drawdoc3.cxx
a2a144e
+++ b/sd/source/core/drawdoc3.cxx
a2a144e
@@ -357,6 +357,26 @@ sal_Bool SdDrawDocument::InsertBookmark(
a2a144e
     return bOK;
a2a144e
 }
a2a144e
 
a2a144e
+namespace
a2a144e
+{
a2a144e
+
a2a144e
+void
a2a144e
+lcl_removeUnusedStyles(SfxStyleSheetBasePool* const pStyleSheetPool, SdStyleSheetVector& rStyles)
a2a144e
+{
a2a144e
+    SdStyleSheetVector aUsedStyles;
a2a144e
+    aUsedStyles.reserve(rStyles.size());
a2a144e
+    for (SdStyleSheetVector::const_iterator aIt(rStyles.begin()), aLast(rStyles.end()); aIt != aLast; ++aIt)
a2a144e
+    {
a2a144e
+        if ((*aIt)->IsUsed())
a2a144e
+            aUsedStyles.push_back(*aIt);
a2a144e
+        else
a2a144e
+            pStyleSheetPool->Remove((*aIt).get());
a2a144e
+    }
a2a144e
+    rStyles = aUsedStyles;
a2a144e
+}
a2a144e
+
a2a144e
+}
a2a144e
+
a2a144e
 sal_Bool SdDrawDocument::InsertBookmarkAsPage(
a2a144e
     const std::vector<rtl::OUString> &rBookmarkList,
a2a144e
     std::vector<rtl::OUString> *pExchangeList,            // Liste der zu verwendenen Namen
a2a144e
@@ -484,8 +504,8 @@ sal_Bool SdDrawDocument::InsertBookmarkAsPage(
a2a144e
     /**************************************************************************
a2a144e
     * Die tatsaechlich benoetigten Vorlagen kopieren
a2a144e
     **************************************************************************/
a2a144e
-    SdStyleSheetPool* pBookmarkStyleSheetPool =
a2a144e
-    (SdStyleSheetPool*) pBookmarkDoc->GetStyleSheetPool();
a2a144e
+    SdStyleSheetPool* pBookmarkStyleSheetPool = dynamic_cast<SdStyleSheetPool*>(pBookmarkDoc->GetStyleSheetPool());
a2a144e
+    SdStyleSheetPool* pStyleSheetPool = dynamic_cast<SdStyleSheetPool*>(GetStyleSheetPool());
a2a144e
 
a2a144e
     // Wenn Vorlagen kopiert werden muessen, dann muessen auch die
a2a144e
     // MasterPages kopiert werden!
a2a144e
@@ -498,7 +518,7 @@ sal_Bool SdDrawDocument::InsertBookmarkAsPage(
a2a144e
         SdStyleSheetVector aCreatedStyles;
a2a144e
         String layoutName = *pIter;
a2a144e
 
a2a144e
-        ((SdStyleSheetPool*)GetStyleSheetPool())->CopyLayoutSheets(layoutName, *pBookmarkStyleSheetPool,aCreatedStyles);
a2a144e
+        pStyleSheetPool->CopyLayoutSheets(layoutName, *pBookmarkStyleSheetPool,aCreatedStyles);
a2a144e
 
a2a144e
         if(!aCreatedStyles.empty())
a2a144e
         {
a2a144e
@@ -510,6 +530,18 @@ sal_Bool SdDrawDocument::InsertBookmarkAsPage(
a2a144e
         }
a2a144e
     }
a2a144e
 
a2a144e
+    // Copy styles. This unconditionally copies all styles, even those
a2a144e
+    // that are not used in any of the inserted pages. The unused styles
a2a144e
+    // are then removed at the end of the function, where we also create
a2a144e
+    // undo records for the inserted styles.
a2a144e
+    SdStyleSheetVector aNewGraphicStyles;
a2a144e
+    pStyleSheetPool->CopyGraphicSheets(*pBookmarkStyleSheetPool, aNewGraphicStyles);
a2a144e
+    SdStyleSheetVector aNewCellStyles;
a2a144e
+    pStyleSheetPool->CopyCellSheets(*pBookmarkStyleSheetPool, aNewCellStyles);
a2a144e
+
a2a144e
+    // TODO handle undo of table styles too
a2a144e
+    pStyleSheetPool->CopyTableStyles(*pBookmarkStyleSheetPool);
a2a144e
+
a2a144e
     /**************************************************************************
a2a144e
     * Dokument einfuegen
a2a144e
     **************************************************************************/
a2a144e
@@ -910,6 +942,17 @@ sal_Bool SdDrawDocument::InsertBookmarkAsPage(
a2a144e
     // Make absolutely sure no double masterpages are there
a2a144e
     RemoveUnnecessaryMasterPages(NULL, sal_True, sal_True);
a2a144e
 
a2a144e
+    // remove copied styles not used on any inserted page and create
a2a144e
+    // undo records
a2a144e
+    // WARNING: SdMoveStyleSheetsUndoAction clears the passed list of
a2a144e
+    // styles, so it cannot be used after this point
a2a144e
+    lcl_removeUnusedStyles(GetStyleSheetPool(), aNewGraphicStyles);
a2a144e
+    if (!aNewGraphicStyles.empty() && pUndoMgr)
a2a144e
+        pUndoMgr->AddUndoAction(new SdMoveStyleSheetsUndoAction(this, aNewGraphicStyles, sal_True));
a2a144e
+    lcl_removeUnusedStyles(GetStyleSheetPool(), aNewCellStyles);
a2a144e
+    if (!aNewCellStyles.empty() && pUndoMgr)
a2a144e
+        pUndoMgr->AddUndoAction(new SdMoveStyleSheetsUndoAction(this, aNewCellStyles, sal_True));
a2a144e
+
a2a144e
     if( bUndo )
a2a144e
         EndUndo();
a2a144e
     pUndoMgr->LeaveListAction();
a2a144e
diff --git a/sd/source/core/stlpool.cxx b/sd/source/core/stlpool.cxx
a2a144e
index 078a64d..cfe6c82 100644
a2a144e
--- a/sd/source/core/stlpool.cxx
a2a144e
+++ b/sd/source/core/stlpool.cxx
a2a144e
@@ -582,8 +582,24 @@ void SdStyleSheetPool::CopyTableStyles(SdStyleSheetPool& rSourcePool)
a2a144e
     }
a2a144e
 }
a2a144e
 
a2a144e
+void SdStyleSheetPool::CopyGraphicSheets(SdStyleSheetPool& rSourcePool, SdStyleSheetVector& rCreatedSheets)
a2a144e
+{
a2a144e
+    CopySheets( rSourcePool, SD_STYLE_FAMILY_GRAPHICS, rCreatedSheets );
a2a144e
+}
a2a144e
+
a2a144e
+void SdStyleSheetPool::CopyCellSheets(SdStyleSheetPool& rSourcePool, SdStyleSheetVector& rCreatedSheets)
a2a144e
+{
a2a144e
+    CopySheets( rSourcePool, SD_STYLE_FAMILY_CELL, rCreatedSheets );
a2a144e
+}
a2a144e
+
a2a144e
 void SdStyleSheetPool::CopySheets(SdStyleSheetPool& rSourcePool, SfxStyleFamily eFamily )
a2a144e
 {
a2a144e
+    SdStyleSheetVector aTmpSheets;
a2a144e
+    CopySheets(rSourcePool, eFamily, aTmpSheets);
a2a144e
+}
a2a144e
+
a2a144e
+void SdStyleSheetPool::CopySheets(SdStyleSheetPool& rSourcePool, SfxStyleFamily eFamily, SdStyleSheetVector& rCreatedSheets)
a2a144e
+{
a2a144e
     String aHelpFile;
a2a144e
 
a2a144e
     sal_uInt32 nCount = rSourcePool.aStyles.size();
a2a144e
@@ -610,6 +626,8 @@ void SdStyleSheetPool::CopySheets(SdStyleSheetPool& rSourcePool, SfxStyleFamily
a2a144e
 
a2a144e
                 xNewSheet->SetHelpId( aHelpFile, xSheet->GetHelpId( aHelpFile ) );
a2a144e
                 xNewSheet->GetItemSet().Put( xSheet->GetItemSet() );
a2a144e
+
a2a144e
+                rCreatedSheets.push_back( SdStyleSheetRef( static_cast< SdStyleSheet* >( xNewSheet.get() ) ) );
a2a144e
             }
a2a144e
         }
a2a144e
     }
a2a144e
diff --git a/sd/source/core/stlsheet.cxx b/sd/source/core/stlsheet.cxx
a2a144e
index ecd84d7..f013739 100644
a2a144e
--- a/sd/source/core/stlsheet.cxx
a2a144e
+++ b/sd/source/core/stlsheet.cxx
a2a144e
@@ -35,6 +35,7 @@
a2a144e
 #include <svl/smplhint.hxx>
a2a144e
 #include <svl/itemset.hxx>
a2a144e
 
a2a144e
+#include <svx/sdr/properties/attributeproperties.hxx>
a2a144e
 #include <svx/xflbmtit.hxx>
a2a144e
 #include <svx/xflbstit.hxx>
a2a144e
 #include <editeng/bulitem.hxx>
a2a144e
@@ -349,13 +350,16 @@ bool SdStyleSheet::IsUsed() const
a2a144e
                 continue;
a2a144e
 
a2a144e
             // NULL-Pointer ist im Listener-Array erlaubt
a2a144e
-            if (pListener && pListener->ISA(SdrAttrObj))
a2a144e
+            if (pListener)
a2a144e
             {
a2a144e
-                bResult = ((SdrAttrObj*)pListener)->IsInserted();
a2a144e
-            }
a2a144e
-            else if (pListener && pListener->ISA(SfxStyleSheet))
a2a144e
-            {
a2a144e
-                bResult = ((SfxStyleSheet*)pListener)->IsUsed();
a2a144e
+                if (pListener->ISA(sdr::properties::AttributeProperties))
a2a144e
+                {
a2a144e
+                    bResult = true;
a2a144e
+                }
a2a144e
+                else if (pListener->ISA(SfxStyleSheet))
a2a144e
+                {
a2a144e
+                    bResult = ((SfxStyleSheet*)pListener)->IsUsed();
a2a144e
+                }
a2a144e
             }
a2a144e
             if (bResult)
a2a144e
                 break;
a2a144e
-- 
a2a144e
1.8.0.2
a2a144e