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