update dev300-m58
[ooovba.git] / sd / source / core / stlfamily.cxx
blob10258dbb9a4d2d9c94a62bd97b7611663c24e7d7
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: stlfamily.cxx,v $
10 * $Revision: 1.4 $
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_sd.hxx"
34 #include <com/sun/star/lang/DisposedException.hpp>
35 #include <com/sun/star/lang/IllegalAccessException.hpp>
37 #include <vos/mutex.hxx>
38 #include <vcl/svapp.hxx>
40 #include <svtools/style.hxx>
42 #include <svx/unoprov.hxx>
44 #include "../ui/inc/strings.hrc"
45 #include "stlfamily.hxx"
46 #include "stlsheet.hxx"
47 #include "sdresid.hxx"
48 #include "drawdoc.hxx"
49 #include "sdpage.hxx"
50 #include "glob.hxx"
52 #include <map>
54 using ::rtl::OUString;
55 using namespace ::vos;
56 using namespace ::com::sun::star::uno;
57 using namespace ::com::sun::star::lang;
58 using namespace ::com::sun::star::container;
59 using namespace ::com::sun::star::style;
60 using namespace ::com::sun::star::beans;
62 // ----------------------------------------------------------
64 typedef std::map< rtl::OUString, rtl::Reference< SdStyleSheet > > PresStyleMap;
66 struct SdStyleFamilyImpl
68 SdrPageWeakRef mxMasterPage;
69 String maLayoutName;
71 PresStyleMap& getStyleSheets();
72 rtl::Reference< SfxStyleSheetPool > mxPool;
74 private:
75 PresStyleMap maStyleSheets;
78 PresStyleMap& SdStyleFamilyImpl::getStyleSheets()
80 if( mxMasterPage.is() && (mxMasterPage->GetLayoutName() != maLayoutName) )
82 maLayoutName = mxMasterPage->GetLayoutName();
84 String aLayoutName( maLayoutName );
85 const sal_uInt16 nLen = aLayoutName.Search(String( RTL_CONSTASCII_USTRINGPARAM(SD_LT_SEPARATOR)))+4;
86 aLayoutName.Erase( nLen );
88 if( (maStyleSheets.size() == 0) || !((*maStyleSheets.begin()).second->GetName().Equals( aLayoutName, 0, nLen )) )
90 maStyleSheets.clear();
92 const SfxStyles& rStyles = mxPool->GetStyles();
93 for( SfxStyles::const_iterator iter( rStyles.begin() ); iter != rStyles.end(); iter++ )
95 SdStyleSheet* pStyle = static_cast< SdStyleSheet* >( (*iter).get() );
96 if( pStyle && (pStyle->GetFamily() == SD_STYLE_FAMILY_MASTERPAGE) && (pStyle->GetName().Equals( aLayoutName, 0, nLen )) )
97 maStyleSheets[ pStyle->GetApiName() ] = rtl::Reference< SdStyleSheet >( pStyle );
102 return maStyleSheets;
105 // ----------------------------------------------------------
107 SdStyleFamily::SdStyleFamily( const rtl::Reference< SfxStyleSheetPool >& xPool, SfxStyleFamily nFamily )
108 : mnFamily( nFamily )
109 , mxPool( xPool )
110 , mpImpl( 0 )
114 // ----------------------------------------------------------
116 SdStyleFamily::SdStyleFamily( const rtl::Reference< SfxStyleSheetPool >& xPool, const SdPage* pMasterPage )
117 : mnFamily( SD_STYLE_FAMILY_MASTERPAGE )
118 , mxPool( xPool )
119 , mpImpl( new SdStyleFamilyImpl() )
121 mpImpl->mxMasterPage.reset( const_cast< SdPage* >( pMasterPage ) );
122 mpImpl->mxPool = xPool;
125 // ----------------------------------------------------------
127 SdStyleFamily::~SdStyleFamily()
129 DBG_ASSERT( !mxPool.is(), "SdStyleFamily::~SdStyleFamily(), dispose me first!" );
130 delete mpImpl;
133 // ----------------------------------------------------------
135 void SdStyleFamily::throwIfDisposed() const throw(RuntimeException)
137 if( !mxPool.is() )
138 throw DisposedException();
141 // ----------------------------------------------------------
143 SdStyleSheet* SdStyleFamily::GetValidNewSheet( const Any& rElement ) throw(IllegalArgumentException)
145 Reference< XStyle > xStyle( rElement, UNO_QUERY );
146 SdStyleSheet* pStyle = static_cast< SdStyleSheet* >( xStyle.get() );
148 if( pStyle == 0 || (pStyle->GetFamily() != mnFamily) || (&pStyle->GetPool() != mxPool.get()) || (mxPool->Find( pStyle->GetName(), mnFamily) != 0) )
149 throw IllegalArgumentException();
151 return pStyle;
154 // ----------------------------------------------------------
156 SdStyleSheet* SdStyleFamily::GetSheetByName( const OUString& rName ) throw(NoSuchElementException, WrappedTargetException )
158 SdStyleSheet* pRet = 0;
159 if( rName.getLength() )
161 if( mnFamily == SD_STYLE_FAMILY_MASTERPAGE )
163 PresStyleMap& rStyleMap = mpImpl->getStyleSheets();
164 PresStyleMap::iterator iter( rStyleMap.find(rName) );
165 if( iter != rStyleMap.end() )
166 pRet = (*iter).second.get();
168 else
170 const SfxStyles& rStyles = mxPool->GetStyles();
171 for( SfxStyles::const_iterator iter( rStyles.begin() ); iter != rStyles.end(); iter++ )
173 SdStyleSheet* pStyle = static_cast< SdStyleSheet* >( (*iter).get() );
174 if( pStyle && (pStyle->GetFamily() == mnFamily) && (pStyle->GetApiName() == rName) )
176 pRet = pStyle;
177 break;
182 if( pRet )
183 return pRet;
185 throw NoSuchElementException();
188 // ----------------------------------------------------------
189 // XServiceInfo
190 // ----------------------------------------------------------
192 OUString SAL_CALL SdStyleFamily::getImplementationName() throw(RuntimeException)
194 return OUString( RTL_CONSTASCII_USTRINGPARAM("SdStyleFamily") );
197 // ----------------------------------------------------------
199 sal_Bool SAL_CALL SdStyleFamily::supportsService( const OUString& ServiceName ) throw(RuntimeException)
201 return SvxServiceInfoHelper::supportsService( ServiceName, getSupportedServiceNames() );
204 // ----------------------------------------------------------
206 Sequence< OUString > SAL_CALL SdStyleFamily::getSupportedServiceNames() throw(RuntimeException)
208 OUString aServiceName( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.style.StyleFamily") );
209 Sequence< OUString > aSeq( &aServiceName, 1 );
210 return aSeq;
213 // ----------------------------------------------------------
214 // XNamed
215 // ----------------------------------------------------------
217 OUString SAL_CALL SdStyleFamily::getName() throw (RuntimeException)
219 if( mnFamily == SD_STYLE_FAMILY_MASTERPAGE )
221 SdPage* pPage = static_cast< SdPage* >( mpImpl->mxMasterPage.get() );
222 if( pPage == 0 )
223 throw DisposedException();
225 String aLayoutName( pPage->GetLayoutName() );
226 const String aSep( RTL_CONSTASCII_USTRINGPARAM( SD_LT_SEPARATOR ));
227 aLayoutName.Erase(aLayoutName.Search(aSep));
229 return OUString( aLayoutName );
231 else
233 return SdStyleSheet::GetFamilyString( mnFamily );
237 // ----------------------------------------------------------
239 void SAL_CALL SdStyleFamily::setName( const ::rtl::OUString& ) throw (RuntimeException)
243 // ----------------------------------------------------------
244 // XNameAccess
245 // ----------------------------------------------------------
247 Any SAL_CALL SdStyleFamily::getByName( const OUString& rName ) throw(NoSuchElementException, WrappedTargetException, RuntimeException)
249 OGuard aGuard( Application::GetSolarMutex() );
250 throwIfDisposed();
251 return Any( Reference< XStyle >( static_cast<SfxUnoStyleSheet*>(GetSheetByName( rName )) ) );
254 // ----------------------------------------------------------
256 Sequence< OUString > SAL_CALL SdStyleFamily::getElementNames() throw(RuntimeException)
258 OGuard aGuard( Application::GetSolarMutex() );
260 throwIfDisposed();
262 if( mnFamily == SD_STYLE_FAMILY_MASTERPAGE )
264 PresStyleMap& rStyleMap = mpImpl->getStyleSheets();
265 Sequence< OUString > aNames( rStyleMap.size() );
267 PresStyleMap::iterator iter( rStyleMap.begin() );
268 OUString* pNames = aNames.getArray();
269 while( iter != rStyleMap.end() )
271 const OUString sName( (*iter).first );
272 rtl::Reference< SdStyleSheet > xStyle( (*iter++).second );
273 if( xStyle.is() )
275 *pNames++ = xStyle->GetApiName();
277 else
279 int i = 0;
280 i++;
284 // *pNames++ = (*iter++).second->GetApiName();
285 return aNames;
287 else
289 std::vector< OUString > aNames;
290 const SfxStyles& rStyles = mxPool->GetStyles();
291 for( SfxStyles::const_iterator iter( rStyles.begin() ); iter != rStyles.end(); iter++ )
293 SdStyleSheet* pStyle = static_cast< SdStyleSheet* >( (*iter).get() );
294 if( pStyle && (pStyle->GetFamily() == mnFamily) )
295 aNames.push_back( pStyle->GetApiName() );
297 return Sequence< OUString >( &(*aNames.begin()), aNames.size() );
301 // ----------------------------------------------------------
303 sal_Bool SAL_CALL SdStyleFamily::hasByName( const OUString& aName ) throw(RuntimeException)
305 OGuard aGuard( Application::GetSolarMutex() );
306 throwIfDisposed();
308 if( aName.getLength() )
310 if( mnFamily == SD_STYLE_FAMILY_MASTERPAGE )
312 PresStyleMap& rStyleSheets = mpImpl->getStyleSheets();
313 PresStyleMap::iterator iter( rStyleSheets.find(aName) );
314 return ( iter != rStyleSheets.end() ) ? sal_True : sal_False;
316 else
318 const SfxStyles& rStyles = mxPool->GetStyles();
319 for( SfxStyles::const_iterator iter( rStyles.begin() ); iter != rStyles.end(); iter++ )
321 SdStyleSheet* pStyle = static_cast< SdStyleSheet* >( (*iter).get() );
322 if( pStyle && (pStyle->GetFamily() == mnFamily) && ( pStyle->GetApiName() == aName ) )
323 return sal_True;
328 return sal_False;
331 // ----------------------------------------------------------
332 // XElementAccess
333 // ----------------------------------------------------------
335 Type SAL_CALL SdStyleFamily::getElementType() throw(RuntimeException)
337 return XStyle::static_type();
340 // ----------------------------------------------------------
342 sal_Bool SAL_CALL SdStyleFamily::hasElements() throw(RuntimeException)
344 OGuard aGuard( Application::GetSolarMutex() );
345 throwIfDisposed();
347 if( mnFamily == SD_STYLE_FAMILY_MASTERPAGE )
349 return sal_True;
351 else
353 const SfxStyles& rStyles = mxPool->GetStyles();
354 for( SfxStyles::const_iterator iter( rStyles.begin() ); iter != rStyles.end(); iter++ )
356 SdStyleSheet* pStyle = static_cast< SdStyleSheet* >( (*iter).get() );
357 if( pStyle && (pStyle->GetFamily() == mnFamily) )
358 return sal_True;
362 return sal_False;
365 // ----------------------------------------------------------
366 // XIndexAccess
367 // ----------------------------------------------------------
369 sal_Int32 SAL_CALL SdStyleFamily::getCount() throw(RuntimeException)
371 OGuard aGuard( Application::GetSolarMutex() );
372 throwIfDisposed();
374 sal_Int32 nCount = 0;
375 if( mnFamily == SD_STYLE_FAMILY_MASTERPAGE )
377 return mpImpl->getStyleSheets().size();
379 else
381 const SfxStyles& rStyles = mxPool->GetStyles();
382 for( SfxStyles::const_iterator iter( rStyles.begin() ); iter != rStyles.end(); iter++ )
384 SdStyleSheet* pStyle = static_cast< SdStyleSheet* >( (*iter).get() );
385 if( pStyle && (pStyle->GetFamily() == mnFamily) )
386 nCount++;
390 return nCount;
393 // ----------------------------------------------------------
395 Any SAL_CALL SdStyleFamily::getByIndex( sal_Int32 Index ) throw(IndexOutOfBoundsException, WrappedTargetException, RuntimeException)
397 OGuard aGuard( Application::GetSolarMutex() );
398 throwIfDisposed();
400 if( Index >= 0 )
402 if( mnFamily == SD_STYLE_FAMILY_MASTERPAGE )
404 PresStyleMap& rStyleSheets = mpImpl->getStyleSheets();
405 if( !rStyleSheets.empty() )
407 PresStyleMap::iterator iter( rStyleSheets.begin() );
408 while( Index-- && (iter != rStyleSheets.end()) )
409 iter++;
411 if( (Index==-1) && (iter != rStyleSheets.end()) )
412 return Any( Reference< XStyle >( (*iter).second.get() ) );
415 else
417 const SfxStyles& rStyles = mxPool->GetStyles();
418 for( SfxStyles::const_iterator iter( rStyles.begin() ); iter != rStyles.end(); iter++ )
420 SdStyleSheet* pStyle = static_cast< SdStyleSheet* >( (*iter).get() );
421 if( pStyle && (pStyle->GetFamily() == mnFamily) )
423 if( Index-- == 0 )
424 return Any( Reference< XStyle >( pStyle ) );
430 throw IndexOutOfBoundsException();
433 // ----------------------------------------------------------
434 // XNameContainer
435 // ----------------------------------------------------------
437 void SAL_CALL SdStyleFamily::insertByName( const OUString& rName, const Any& rElement ) throw(IllegalArgumentException, ElementExistException, WrappedTargetException, RuntimeException)
439 OGuard aGuard( Application::GetSolarMutex() );
440 throwIfDisposed();
442 if(rName.getLength() == 0)
443 throw IllegalArgumentException();
445 SdStyleSheet* pStyle = GetValidNewSheet( rElement );
446 if( !pStyle->SetName( rName ) )
447 throw ElementExistException();
449 pStyle->SetApiName( rName );
450 mxPool->Insert( pStyle );
453 // ----------------------------------------------------------
455 void SAL_CALL SdStyleFamily::removeByName( const OUString& rName ) throw(NoSuchElementException, WrappedTargetException, RuntimeException)
457 OGuard aGuard( Application::GetSolarMutex() );
458 throwIfDisposed();
460 SdStyleSheet* pStyle = GetSheetByName( rName );
462 if( !pStyle->IsUserDefined() )
463 throw WrappedTargetException();
465 mxPool->Remove( pStyle );
468 // ----------------------------------------------------------
469 // XNameReplace
470 // ----------------------------------------------------------
472 void SAL_CALL SdStyleFamily::replaceByName( const OUString& rName, const Any& aElement ) throw(IllegalArgumentException, NoSuchElementException, WrappedTargetException, RuntimeException)
474 OGuard aGuard( Application::GetSolarMutex() );
475 throwIfDisposed();
477 SdStyleSheet* pOldStyle = GetSheetByName( rName );
478 SdStyleSheet* pNewStyle = GetValidNewSheet( aElement );
480 mxPool->Remove( pOldStyle );
481 mxPool->Insert( pNewStyle );
484 // ----------------------------------------------------------
485 // XSingleServiceFactory
486 // ----------------------------------------------------------
488 Reference< XInterface > SAL_CALL SdStyleFamily::createInstance() throw(Exception, RuntimeException)
490 OGuard aGuard( Application::GetSolarMutex() );
491 throwIfDisposed();
493 if( mnFamily == SD_STYLE_FAMILY_MASTERPAGE )
495 throw IllegalAccessException();
497 else
499 return Reference< XInterface >( static_cast< XStyle* >( SdStyleSheet::CreateEmptyUserStyle( *mxPool.get(), mnFamily ) ) );
503 // ----------------------------------------------------------
505 Reference< XInterface > SAL_CALL SdStyleFamily::createInstanceWithArguments( const Sequence< Any >& ) throw(Exception, RuntimeException)
507 return createInstance();
510 // ----------------------------------------------------------
511 // XComponent
512 // ----------------------------------------------------------
514 void SAL_CALL SdStyleFamily::dispose( ) throw (RuntimeException)
516 if( mxPool.is() )
517 mxPool.clear();
519 if( mpImpl )
521 delete mpImpl;
522 mpImpl = 0;
526 // ----------------------------------------------------------
528 void SAL_CALL SdStyleFamily::addEventListener( const Reference< XEventListener >& ) throw (RuntimeException)
532 // ----------------------------------------------------------
534 void SAL_CALL SdStyleFamily::removeEventListener( const Reference< XEventListener >& ) throw (RuntimeException)
538 // ----------------------------------------------------------
539 // XPropertySet
540 // ----------------------------------------------------------
542 Reference<XPropertySetInfo> SdStyleFamily::getPropertySetInfo() throw (RuntimeException)
544 OSL_ENSURE( 0, "###unexpected!" );
545 return Reference<XPropertySetInfo>();
548 // ----------------------------------------------------------
550 void SdStyleFamily::setPropertyValue( const OUString& , const Any& ) throw (UnknownPropertyException, PropertyVetoException, IllegalArgumentException, WrappedTargetException, RuntimeException)
552 OSL_ENSURE( 0, "###unexpected!" );
555 // ----------------------------------------------------------
557 Any SdStyleFamily::getPropertyValue( const OUString& PropertyName ) throw (UnknownPropertyException, WrappedTargetException, RuntimeException)
559 if (PropertyName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM("DisplayName") ))
561 OGuard aGuard( Application::GetSolarMutex() );
562 OUString sDisplayName;
563 switch( mnFamily )
565 case SD_STYLE_FAMILY_MASTERPAGE: sDisplayName = getName(); break;
566 case SD_STYLE_FAMILY_CELL: sDisplayName = String( SdResId(STR_CELL_STYLE_FAMILY) ); break;
567 // case SD_STYLE_FAMILY_GRAPHICS:
568 default: sDisplayName = String( SdResId(STR_GRAPHICS_STYLE_FAMILY) ); break;
570 return Any( sDisplayName );
572 else
574 throw UnknownPropertyException( OUString( RTL_CONSTASCII_USTRINGPARAM("unknown property: ") ) + PropertyName, static_cast<OWeakObject *>(this) );
578 // ----------------------------------------------------------
580 void SdStyleFamily::addPropertyChangeListener( const OUString& , const Reference<XPropertyChangeListener>& ) throw (UnknownPropertyException, WrappedTargetException, RuntimeException)
582 OSL_ENSURE( 0, "###unexpected!" );
585 // ----------------------------------------------------------
587 void SdStyleFamily::removePropertyChangeListener( const OUString& , const Reference<XPropertyChangeListener>& ) throw (UnknownPropertyException, WrappedTargetException, RuntimeException)
589 OSL_ENSURE( 0, "###unexpected!" );
592 // ----------------------------------------------------------
594 void SdStyleFamily::addVetoableChangeListener( const OUString& , const Reference<XVetoableChangeListener>& ) throw (UnknownPropertyException, WrappedTargetException, RuntimeException)
596 OSL_ENSURE( 0, "###unexpected!" );
599 // ----------------------------------------------------------
601 void SdStyleFamily::removeVetoableChangeListener( const OUString& , const Reference<XVetoableChangeListener>& ) throw (UnknownPropertyException, WrappedTargetException, RuntimeException)
603 OSL_ENSURE( 0, "###unexpected!" );