1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: stlfamily.cxx,v $
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"
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
;
71 PresStyleMap
& getStyleSheets();
72 rtl::Reference
< SfxStyleSheetPool
> mxPool
;
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
)
114 // ----------------------------------------------------------
116 SdStyleFamily::SdStyleFamily( const rtl::Reference
< SfxStyleSheetPool
>& xPool
, const SdPage
* pMasterPage
)
117 : mnFamily( SD_STYLE_FAMILY_MASTERPAGE
)
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!" );
133 // ----------------------------------------------------------
135 void SdStyleFamily::throwIfDisposed() const throw(RuntimeException
)
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();
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();
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
) )
185 throw NoSuchElementException();
188 // ----------------------------------------------------------
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 );
213 // ----------------------------------------------------------
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() );
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
);
233 return SdStyleSheet::GetFamilyString( mnFamily
);
237 // ----------------------------------------------------------
239 void SAL_CALL
SdStyleFamily::setName( const ::rtl::OUString
& ) throw (RuntimeException
)
243 // ----------------------------------------------------------
245 // ----------------------------------------------------------
247 Any SAL_CALL
SdStyleFamily::getByName( const OUString
& rName
) throw(NoSuchElementException
, WrappedTargetException
, RuntimeException
)
249 OGuard
aGuard( Application::GetSolarMutex() );
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() );
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
);
275 *pNames
++ = xStyle
->GetApiName();
284 // *pNames++ = (*iter++).second->GetApiName();
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() );
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
;
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
) )
331 // ----------------------------------------------------------
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() );
347 if( mnFamily
== SD_STYLE_FAMILY_MASTERPAGE
)
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
) )
365 // ----------------------------------------------------------
367 // ----------------------------------------------------------
369 sal_Int32 SAL_CALL
SdStyleFamily::getCount() throw(RuntimeException
)
371 OGuard
aGuard( Application::GetSolarMutex() );
374 sal_Int32 nCount
= 0;
375 if( mnFamily
== SD_STYLE_FAMILY_MASTERPAGE
)
377 return mpImpl
->getStyleSheets().size();
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
) )
393 // ----------------------------------------------------------
395 Any SAL_CALL
SdStyleFamily::getByIndex( sal_Int32 Index
) throw(IndexOutOfBoundsException
, WrappedTargetException
, RuntimeException
)
397 OGuard
aGuard( Application::GetSolarMutex() );
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()) )
411 if( (Index
==-1) && (iter
!= rStyleSheets
.end()) )
412 return Any( Reference
< XStyle
>( (*iter
).second
.get() ) );
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
) )
424 return Any( Reference
< XStyle
>( pStyle
) );
430 throw IndexOutOfBoundsException();
433 // ----------------------------------------------------------
435 // ----------------------------------------------------------
437 void SAL_CALL
SdStyleFamily::insertByName( const OUString
& rName
, const Any
& rElement
) throw(IllegalArgumentException
, ElementExistException
, WrappedTargetException
, RuntimeException
)
439 OGuard
aGuard( Application::GetSolarMutex() );
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() );
460 SdStyleSheet
* pStyle
= GetSheetByName( rName
);
462 if( !pStyle
->IsUserDefined() )
463 throw WrappedTargetException();
465 mxPool
->Remove( pStyle
);
468 // ----------------------------------------------------------
470 // ----------------------------------------------------------
472 void SAL_CALL
SdStyleFamily::replaceByName( const OUString
& rName
, const Any
& aElement
) throw(IllegalArgumentException
, NoSuchElementException
, WrappedTargetException
, RuntimeException
)
474 OGuard
aGuard( Application::GetSolarMutex() );
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() );
493 if( mnFamily
== SD_STYLE_FAMILY_MASTERPAGE
)
495 throw IllegalAccessException();
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 // ----------------------------------------------------------
512 // ----------------------------------------------------------
514 void SAL_CALL
SdStyleFamily::dispose( ) throw (RuntimeException
)
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 // ----------------------------------------------------------
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
;
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
);
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!" );