merge the formfield patch from ooo-build
[ooovba.git] / sw / source / core / doc / swstylemanager.cxx
blob99fa615419f3c376046205cbe513451b7db98eb4
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: swstylemanager.cxx,v $
10 * $Revision: 1.7 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sw.hxx"
35 #include "swstylemanager.hxx"
36 #include <hash_map>
37 #include <svtools/stylepool.hxx>
38 #include <doc.hxx>
39 #include <charfmt.hxx>
40 #include <docary.hxx>
41 #include <swtypes.hxx>
42 #include <istyleaccess.hxx>
44 typedef ::std::hash_map< const ::rtl::OUString,
45 StylePool::SfxItemSet_Pointer_t,
46 ::rtl::OUStringHash,
47 ::std::equal_to< ::rtl::OUString > > SwStyleNameCache;
49 class SwStyleCache
51 SwStyleNameCache mMap;
52 public:
53 SwStyleCache() {}
54 void addStyleName( StylePool::SfxItemSet_Pointer_t pStyle )
55 { mMap[ StylePool::nameOf(pStyle) ] = pStyle; }
56 void addCompletePool( StylePool& rPool );
57 StylePool::SfxItemSet_Pointer_t getByName( const rtl::OUString& rName ) { return mMap[rName]; }
60 void SwStyleCache::addCompletePool( StylePool& rPool )
62 IStylePoolIteratorAccess *pIter = rPool.createIterator();
63 StylePool::SfxItemSet_Pointer_t pStyle = pIter->getNext();
64 while( pStyle.get() )
66 rtl::OUString aName( StylePool::nameOf(pStyle) );
67 mMap[ aName ] = pStyle;
68 pStyle = pIter->getNext();
70 delete pIter;
73 class SwStyleManager : public IStyleAccess
75 StylePool aAutoCharPool;
76 StylePool aAutoParaPool;
77 SwStyleCache *mpCharCache;
78 SwStyleCache *mpParaCache;
80 public:
81 // --> OD 2008-03-07 #refactorlists#
82 // accept empty item set for ignorable paragraph items.
83 SwStyleManager( SfxItemSet* pIgnorableParagraphItems )
84 : aAutoCharPool(),
85 aAutoParaPool( pIgnorableParagraphItems ),
86 mpCharCache(0),
87 mpParaCache(0)
89 // <--
90 virtual ~SwStyleManager();
91 virtual StylePool::SfxItemSet_Pointer_t getAutomaticStyle( const SfxItemSet& rSet,
92 IStyleAccess::SwAutoStyleFamily eFamily );
93 virtual StylePool::SfxItemSet_Pointer_t getByName( const rtl::OUString& rName,
94 IStyleAccess::SwAutoStyleFamily eFamily );
95 virtual void getAllStyles( std::vector<StylePool::SfxItemSet_Pointer_t> &rStyles,
96 IStyleAccess::SwAutoStyleFamily eFamily );
97 virtual StylePool::SfxItemSet_Pointer_t cacheAutomaticStyle( const SfxItemSet& rSet,
98 SwAutoStyleFamily eFamily );
99 virtual void clearCaches();
102 IStyleAccess *createStyleManager( SfxItemSet* pIgnorableParagraphItems )
104 return new SwStyleManager( pIgnorableParagraphItems );
107 SwStyleManager::~SwStyleManager()
109 delete mpCharCache;
110 delete mpParaCache;
113 void SwStyleManager::clearCaches()
115 delete mpCharCache;
116 mpCharCache = 0;
117 delete mpParaCache;
118 mpParaCache = 0;
121 StylePool::SfxItemSet_Pointer_t SwStyleManager::getAutomaticStyle( const SfxItemSet& rSet,
122 IStyleAccess::SwAutoStyleFamily eFamily )
124 StylePool& rAutoPool = eFamily == IStyleAccess::AUTO_STYLE_CHAR ? aAutoCharPool : aAutoParaPool;
125 return rAutoPool.insertItemSet( rSet );
128 StylePool::SfxItemSet_Pointer_t SwStyleManager::cacheAutomaticStyle( const SfxItemSet& rSet,
129 IStyleAccess::SwAutoStyleFamily eFamily )
131 StylePool& rAutoPool = eFamily == IStyleAccess::AUTO_STYLE_CHAR ? aAutoCharPool : aAutoParaPool;
132 StylePool::SfxItemSet_Pointer_t pStyle = rAutoPool.insertItemSet( rSet );
133 SwStyleCache* &rpCache = eFamily == IStyleAccess::AUTO_STYLE_CHAR ?
134 mpCharCache : mpParaCache;
135 if( !rpCache )
136 rpCache = new SwStyleCache();
137 rpCache->addStyleName( pStyle );
138 return pStyle;
141 StylePool::SfxItemSet_Pointer_t SwStyleManager::getByName( const rtl::OUString& rName,
142 IStyleAccess::SwAutoStyleFamily eFamily )
144 StylePool& rAutoPool = eFamily == IStyleAccess::AUTO_STYLE_CHAR ? aAutoCharPool : aAutoParaPool;
145 SwStyleCache* &rpCache = eFamily == IStyleAccess::AUTO_STYLE_CHAR ? mpCharCache : mpParaCache;
146 if( !rpCache )
147 rpCache = new SwStyleCache();
148 StylePool::SfxItemSet_Pointer_t pStyle = rpCache->getByName( rName );
149 if( !pStyle.get() )
151 // Ok, ok, it's allowed to ask for uncached styles (from UNO) but it should not be done
152 // during loading a document
153 ASSERT( false, "Don't ask for uncached styles" );
154 rpCache->addCompletePool( rAutoPool );
155 pStyle = rpCache->getByName( rName );
157 return pStyle;
160 void SwStyleManager::getAllStyles( std::vector<StylePool::SfxItemSet_Pointer_t> &rStyles,
161 IStyleAccess::SwAutoStyleFamily eFamily )
163 StylePool& rAutoPool = eFamily == IStyleAccess::AUTO_STYLE_CHAR ? aAutoCharPool : aAutoParaPool;
164 // --> OD 2008-03-07 #refactorlists#
165 // setup <StylePool> iterator, which skips unused styles and ignorable items
166 IStylePoolIteratorAccess *pIter = rAutoPool.createIterator( true, true );
167 // <--
168 StylePool::SfxItemSet_Pointer_t pStyle = pIter->getNext();
169 while( pStyle.get() )
171 rStyles.push_back( pStyle );
173 pStyle = pIter->getNext();
175 delete pIter;