1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include <com/sun/star/lang/DisposedException.hpp>
22 #include <com/sun/star/lang/IllegalAccessException.hpp>
23 #include <comphelper/serviceinfohelper.hxx>
25 #include <osl/mutex.hxx>
26 #include <vcl/svapp.hxx>
28 #include <svl/style.hxx>
30 #include <svx/unoprov.hxx>
32 #include "../ui/inc/strings.hrc"
33 #include "stlfamily.hxx"
34 #include "stlsheet.hxx"
35 #include "sdresid.hxx"
36 #include "drawdoc.hxx"
42 using namespace ::com::sun::star::uno
;
43 using namespace ::com::sun::star::lang
;
44 using namespace ::com::sun::star::container
;
45 using namespace ::com::sun::star::style
;
46 using namespace ::com::sun::star::beans
;
48 // ----------------------------------------------------------
50 typedef std::map
< OUString
, rtl::Reference
< SdStyleSheet
> > PresStyleMap
;
52 struct SdStyleFamilyImpl
54 SdrPageWeakRef mxMasterPage
;
55 OUString maLayoutName
;
57 PresStyleMap
& getStyleSheets();
58 rtl::Reference
< SfxStyleSheetPool
> mxPool
;
61 PresStyleMap maStyleSheets
;
64 PresStyleMap
& SdStyleFamilyImpl::getStyleSheets()
66 if( mxMasterPage
.is() && (mxMasterPage
->GetLayoutName() != maLayoutName
) )
68 maLayoutName
= mxMasterPage
->GetLayoutName();
70 String
aLayoutName( maLayoutName
);
71 const sal_uInt16 nLen
= aLayoutName
.Search(String( RTL_CONSTASCII_USTRINGPARAM(SD_LT_SEPARATOR
)))+4;
72 aLayoutName
.Erase( nLen
);
74 if( (maStyleSheets
.empty()) || !((*maStyleSheets
.begin()).second
->GetName().Equals( aLayoutName
, 0, nLen
)) )
76 maStyleSheets
.clear();
78 const SfxStyles
& rStyles
= mxPool
->GetStyles();
79 for( SfxStyles::const_iterator
iter( rStyles
.begin() ); iter
!= rStyles
.end(); ++iter
)
81 SdStyleSheet
* pStyle
= static_cast< SdStyleSheet
* >( (*iter
).get() );
82 if( pStyle
&& (pStyle
->GetFamily() == SD_STYLE_FAMILY_MASTERPAGE
) && (pStyle
->GetName().Equals( aLayoutName
, 0, nLen
)) )
83 maStyleSheets
[ pStyle
->GetApiName() ] = rtl::Reference
< SdStyleSheet
>( pStyle
);
91 // ----------------------------------------------------------
93 SdStyleFamily::SdStyleFamily( const rtl::Reference
< SfxStyleSheetPool
>& xPool
, SfxStyleFamily nFamily
)
100 // ----------------------------------------------------------
102 SdStyleFamily::SdStyleFamily( const rtl::Reference
< SfxStyleSheetPool
>& xPool
, const SdPage
* pMasterPage
)
103 : mnFamily( SD_STYLE_FAMILY_MASTERPAGE
)
105 , mpImpl( new SdStyleFamilyImpl() )
107 mpImpl
->mxMasterPage
.reset( const_cast< SdPage
* >( pMasterPage
) );
108 mpImpl
->mxPool
= xPool
;
111 // ----------------------------------------------------------
113 SdStyleFamily::~SdStyleFamily()
115 DBG_ASSERT( !mxPool
.is(), "SdStyleFamily::~SdStyleFamily(), dispose me first!" );
119 // ----------------------------------------------------------
121 void SdStyleFamily::throwIfDisposed() const throw(RuntimeException
)
124 throw DisposedException();
127 // ----------------------------------------------------------
129 SdStyleSheet
* SdStyleFamily::GetValidNewSheet( const Any
& rElement
) throw(IllegalArgumentException
)
131 Reference
< XStyle
> xStyle( rElement
, UNO_QUERY
);
132 SdStyleSheet
* pStyle
= static_cast< SdStyleSheet
* >( xStyle
.get() );
134 if( pStyle
== 0 || (pStyle
->GetFamily() != mnFamily
) || (&pStyle
->GetPool() != mxPool
.get()) || (mxPool
->Find( pStyle
->GetName(), mnFamily
) != 0) )
135 throw IllegalArgumentException();
140 // ----------------------------------------------------------
142 SdStyleSheet
* SdStyleFamily::GetSheetByName( const OUString
& rName
) throw(NoSuchElementException
, WrappedTargetException
)
144 SdStyleSheet
* pRet
= 0;
145 if( !rName
.isEmpty() )
147 if( mnFamily
== SD_STYLE_FAMILY_MASTERPAGE
)
149 PresStyleMap
& rStyleMap
= mpImpl
->getStyleSheets();
150 PresStyleMap::iterator
iter( rStyleMap
.find(rName
) );
151 if( iter
!= rStyleMap
.end() )
152 pRet
= (*iter
).second
.get();
156 const SfxStyles
& rStyles
= mxPool
->GetStyles();
157 for( SfxStyles::const_iterator
iter( rStyles
.begin() ); iter
!= rStyles
.end(); ++iter
)
159 SdStyleSheet
* pStyle
= static_cast< SdStyleSheet
* >( (*iter
).get() );
160 if( pStyle
&& (pStyle
->GetFamily() == mnFamily
) && (pStyle
->GetApiName() == rName
) )
171 throw NoSuchElementException();
174 // ----------------------------------------------------------
176 // ----------------------------------------------------------
178 OUString SAL_CALL
SdStyleFamily::getImplementationName() throw(RuntimeException
)
180 return OUString( "SdStyleFamily" );
183 // ----------------------------------------------------------
185 sal_Bool SAL_CALL
SdStyleFamily::supportsService( const OUString
& ServiceName
) throw(RuntimeException
)
187 return comphelper::ServiceInfoHelper::supportsService( ServiceName
, getSupportedServiceNames() );
190 // ----------------------------------------------------------
192 Sequence
< OUString
> SAL_CALL
SdStyleFamily::getSupportedServiceNames() throw(RuntimeException
)
194 OUString
aServiceName( "com.sun.star.style.StyleFamily" );
195 Sequence
< OUString
> aSeq( &aServiceName
, 1 );
199 // ----------------------------------------------------------
201 // ----------------------------------------------------------
203 OUString SAL_CALL
SdStyleFamily::getName() throw (RuntimeException
)
205 if( mnFamily
== SD_STYLE_FAMILY_MASTERPAGE
)
207 SdPage
* pPage
= static_cast< SdPage
* >( mpImpl
->mxMasterPage
.get() );
209 throw DisposedException();
211 String
aLayoutName( pPage
->GetLayoutName() );
212 const String
aSep( RTL_CONSTASCII_USTRINGPARAM( SD_LT_SEPARATOR
));
213 aLayoutName
.Erase(aLayoutName
.Search(aSep
));
215 return OUString( aLayoutName
);
219 return SdStyleSheet::GetFamilyString( mnFamily
);
223 // ----------------------------------------------------------
225 void SAL_CALL
SdStyleFamily::setName( const OUString
& ) throw (RuntimeException
)
229 // ----------------------------------------------------------
231 // ----------------------------------------------------------
233 Any SAL_CALL
SdStyleFamily::getByName( const OUString
& rName
) throw(NoSuchElementException
, WrappedTargetException
, RuntimeException
)
235 SolarMutexGuard aGuard
;
237 return Any( Reference
< XStyle
>( static_cast<SfxUnoStyleSheet
*>(GetSheetByName( rName
)) ) );
240 // ----------------------------------------------------------
242 Sequence
< OUString
> SAL_CALL
SdStyleFamily::getElementNames() throw(RuntimeException
)
244 SolarMutexGuard aGuard
;
248 if( mnFamily
== SD_STYLE_FAMILY_MASTERPAGE
)
250 PresStyleMap
& rStyleMap
= mpImpl
->getStyleSheets();
251 Sequence
< OUString
> aNames( rStyleMap
.size() );
253 PresStyleMap::iterator
iter( rStyleMap
.begin() );
254 OUString
* pNames
= aNames
.getArray();
255 while( iter
!= rStyleMap
.end() )
257 rtl::Reference
< SdStyleSheet
> xStyle( (*iter
++).second
);
260 *pNames
++ = xStyle
->GetApiName();
268 std::vector
< OUString
> aNames
;
269 const SfxStyles
& rStyles
= mxPool
->GetStyles();
270 for( SfxStyles::const_iterator
iter( rStyles
.begin() ); iter
!= rStyles
.end(); ++iter
)
272 SdStyleSheet
* pStyle
= static_cast< SdStyleSheet
* >( (*iter
).get() );
273 if( pStyle
&& (pStyle
->GetFamily() == mnFamily
) )
274 aNames
.push_back( pStyle
->GetApiName() );
276 return Sequence
< OUString
>( &(*aNames
.begin()), aNames
.size() );
280 // ----------------------------------------------------------
282 sal_Bool SAL_CALL
SdStyleFamily::hasByName( const OUString
& aName
) throw(RuntimeException
)
284 SolarMutexGuard aGuard
;
287 if( !aName
.isEmpty() )
289 if( mnFamily
== SD_STYLE_FAMILY_MASTERPAGE
)
291 PresStyleMap
& rStyleSheets
= mpImpl
->getStyleSheets();
292 PresStyleMap::iterator
iter( rStyleSheets
.find(aName
) );
293 return ( iter
!= rStyleSheets
.end() ) ? sal_True
: sal_False
;
297 const SfxStyles
& rStyles
= mxPool
->GetStyles();
298 for( SfxStyles::const_iterator
iter( rStyles
.begin() ); iter
!= rStyles
.end(); ++iter
)
300 SdStyleSheet
* pStyle
= static_cast< SdStyleSheet
* >( (*iter
).get() );
301 if( pStyle
&& (pStyle
->GetFamily() == mnFamily
) && ( pStyle
->GetApiName() == aName
) )
310 // ----------------------------------------------------------
312 // ----------------------------------------------------------
314 Type SAL_CALL
SdStyleFamily::getElementType() throw(RuntimeException
)
316 return XStyle::static_type();
319 // ----------------------------------------------------------
321 sal_Bool SAL_CALL
SdStyleFamily::hasElements() throw(RuntimeException
)
323 SolarMutexGuard aGuard
;
326 if( mnFamily
== SD_STYLE_FAMILY_MASTERPAGE
)
332 const SfxStyles
& rStyles
= mxPool
->GetStyles();
333 for( SfxStyles::const_iterator
iter( rStyles
.begin() ); iter
!= rStyles
.end(); ++iter
)
335 SdStyleSheet
* pStyle
= static_cast< SdStyleSheet
* >( (*iter
).get() );
336 if( pStyle
&& (pStyle
->GetFamily() == mnFamily
) )
344 // ----------------------------------------------------------
346 // ----------------------------------------------------------
348 sal_Int32 SAL_CALL
SdStyleFamily::getCount() throw(RuntimeException
)
350 SolarMutexGuard aGuard
;
353 sal_Int32 nCount
= 0;
354 if( mnFamily
== SD_STYLE_FAMILY_MASTERPAGE
)
356 return mpImpl
->getStyleSheets().size();
360 const SfxStyles
& rStyles
= mxPool
->GetStyles();
361 for( SfxStyles::const_iterator
iter( rStyles
.begin() ); iter
!= rStyles
.end(); ++iter
)
363 SdStyleSheet
* pStyle
= static_cast< SdStyleSheet
* >( (*iter
).get() );
364 if( pStyle
&& (pStyle
->GetFamily() == mnFamily
) )
372 // ----------------------------------------------------------
374 Any SAL_CALL
SdStyleFamily::getByIndex( sal_Int32 Index
) throw(IndexOutOfBoundsException
, WrappedTargetException
, RuntimeException
)
376 SolarMutexGuard aGuard
;
381 if( mnFamily
== SD_STYLE_FAMILY_MASTERPAGE
)
383 PresStyleMap
& rStyleSheets
= mpImpl
->getStyleSheets();
384 if( !rStyleSheets
.empty() )
386 PresStyleMap::iterator
iter( rStyleSheets
.begin() );
387 while( Index
-- && (iter
!= rStyleSheets
.end()) )
390 if( (Index
==-1) && (iter
!= rStyleSheets
.end()) )
391 return Any( Reference
< XStyle
>( (*iter
).second
.get() ) );
396 const SfxStyles
& rStyles
= mxPool
->GetStyles();
397 for( SfxStyles::const_iterator
iter( rStyles
.begin() ); iter
!= rStyles
.end(); ++iter
)
399 SdStyleSheet
* pStyle
= static_cast< SdStyleSheet
* >( (*iter
).get() );
400 if( pStyle
&& (pStyle
->GetFamily() == mnFamily
) )
403 return Any( Reference
< XStyle
>( pStyle
) );
409 throw IndexOutOfBoundsException();
412 // ----------------------------------------------------------
414 // ----------------------------------------------------------
416 void SAL_CALL
SdStyleFamily::insertByName( const OUString
& rName
, const Any
& rElement
) throw(IllegalArgumentException
, ElementExistException
, WrappedTargetException
, RuntimeException
)
418 SolarMutexGuard aGuard
;
422 throw IllegalArgumentException();
424 SdStyleSheet
* pStyle
= GetValidNewSheet( rElement
);
425 if( !pStyle
->SetName( rName
) )
426 throw ElementExistException();
428 pStyle
->SetApiName( rName
);
429 mxPool
->Insert( pStyle
);
432 // ----------------------------------------------------------
434 void SAL_CALL
SdStyleFamily::removeByName( const OUString
& rName
) throw(NoSuchElementException
, WrappedTargetException
, RuntimeException
)
436 SolarMutexGuard aGuard
;
439 SdStyleSheet
* pStyle
= GetSheetByName( rName
);
441 if( !pStyle
->IsUserDefined() )
442 throw WrappedTargetException();
444 mxPool
->Remove( pStyle
);
447 // ----------------------------------------------------------
449 // ----------------------------------------------------------
451 void SAL_CALL
SdStyleFamily::replaceByName( const OUString
& rName
, const Any
& aElement
) throw(IllegalArgumentException
, NoSuchElementException
, WrappedTargetException
, RuntimeException
)
453 SolarMutexGuard aGuard
;
456 SdStyleSheet
* pOldStyle
= GetSheetByName( rName
);
457 SdStyleSheet
* pNewStyle
= GetValidNewSheet( aElement
);
459 mxPool
->Remove( pOldStyle
);
460 mxPool
->Insert( pNewStyle
);
463 // ----------------------------------------------------------
464 // XSingleServiceFactory
465 // ----------------------------------------------------------
467 Reference
< XInterface
> SAL_CALL
SdStyleFamily::createInstance() throw(Exception
, RuntimeException
)
469 SolarMutexGuard aGuard
;
472 if( mnFamily
== SD_STYLE_FAMILY_MASTERPAGE
)
474 throw IllegalAccessException();
478 return Reference
< XInterface
>( static_cast< XStyle
* >( SdStyleSheet::CreateEmptyUserStyle( *mxPool
.get(), mnFamily
) ) );
482 // ----------------------------------------------------------
484 Reference
< XInterface
> SAL_CALL
SdStyleFamily::createInstanceWithArguments( const Sequence
< Any
>& ) throw(Exception
, RuntimeException
)
486 return createInstance();
489 // ----------------------------------------------------------
491 // ----------------------------------------------------------
493 void SAL_CALL
SdStyleFamily::dispose( ) throw (RuntimeException
)
505 // ----------------------------------------------------------
507 void SAL_CALL
SdStyleFamily::addEventListener( const Reference
< XEventListener
>& ) throw (RuntimeException
)
511 // ----------------------------------------------------------
513 void SAL_CALL
SdStyleFamily::removeEventListener( const Reference
< XEventListener
>& ) throw (RuntimeException
)
517 // ----------------------------------------------------------
519 // ----------------------------------------------------------
521 Reference
<XPropertySetInfo
> SdStyleFamily::getPropertySetInfo() throw (RuntimeException
)
523 OSL_FAIL( "###unexpected!" );
524 return Reference
<XPropertySetInfo
>();
527 // ----------------------------------------------------------
529 void SdStyleFamily::setPropertyValue( const OUString
& , const Any
& ) throw (UnknownPropertyException
, PropertyVetoException
, IllegalArgumentException
, WrappedTargetException
, RuntimeException
)
531 OSL_FAIL( "###unexpected!" );
534 // ----------------------------------------------------------
536 Any
SdStyleFamily::getPropertyValue( const OUString
& PropertyName
) throw (UnknownPropertyException
, WrappedTargetException
, RuntimeException
)
538 if ( PropertyName
== "DisplayName" )
540 SolarMutexGuard aGuard
;
541 OUString sDisplayName
;
544 case SD_STYLE_FAMILY_MASTERPAGE
: sDisplayName
= getName(); break;
545 case SD_STYLE_FAMILY_CELL
: sDisplayName
= String( SdResId(STR_CELL_STYLE_FAMILY
) ); break;
546 default: sDisplayName
= String( SdResId(STR_GRAPHICS_STYLE_FAMILY
) ); break;
548 return Any( sDisplayName
);
552 throw UnknownPropertyException( "unknown property: " + PropertyName
, static_cast<OWeakObject
*>(this) );
556 // ----------------------------------------------------------
558 void SdStyleFamily::addPropertyChangeListener( const OUString
& , const Reference
<XPropertyChangeListener
>& ) throw (UnknownPropertyException
, WrappedTargetException
, RuntimeException
)
560 OSL_FAIL( "###unexpected!" );
563 // ----------------------------------------------------------
565 void SdStyleFamily::removePropertyChangeListener( const OUString
& , const Reference
<XPropertyChangeListener
>& ) throw (UnknownPropertyException
, WrappedTargetException
, RuntimeException
)
567 OSL_FAIL( "###unexpected!" );
570 // ----------------------------------------------------------
572 void SdStyleFamily::addVetoableChangeListener( const OUString
& , const Reference
<XVetoableChangeListener
>& ) throw (UnknownPropertyException
, WrappedTargetException
, RuntimeException
)
574 OSL_FAIL( "###unexpected!" );
577 // ----------------------------------------------------------
579 void SdStyleFamily::removeVetoableChangeListener( const OUString
& , const Reference
<XVetoableChangeListener
>& ) throw (UnknownPropertyException
, WrappedTargetException
, RuntimeException
)
581 OSL_FAIL( "###unexpected!" );
584 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */