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 .
20 #include <com/sun/star/lang/DisposedException.hpp>
21 #include <com/sun/star/lang/IllegalAccessException.hpp>
22 #include <com/sun/star/lang/IndexOutOfBoundsException.hpp>
23 #include <cppuhelper/supportsservice.hxx>
25 #include <vcl/svapp.hxx>
27 #include <svl/style.hxx>
29 #include <svx/unoprov.hxx>
31 #include <strings.hrc>
32 #include <stlfamily.hxx>
33 #include <stlsheet.hxx>
34 #include <sdresid.hxx>
35 #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 typedef std::map
< OUString
, rtl::Reference
< SdStyleSheet
> > PresStyleMap
;
50 struct SdStyleFamilyImpl
52 tools::WeakReference
<SdPage
> mxMasterPage
;
53 OUString maLayoutName
;
55 PresStyleMap
& getStyleSheets();
56 rtl::Reference
< SfxStyleSheetPool
> mxPool
;
59 PresStyleMap maStyleSheets
;
62 PresStyleMap
& SdStyleFamilyImpl::getStyleSheets()
64 if( mxMasterPage
.is() && (mxMasterPage
->GetLayoutName() != maLayoutName
) )
66 maLayoutName
= mxMasterPage
->GetLayoutName();
68 OUString
aLayoutName( maLayoutName
);
69 const sal_Int32 nLen
= aLayoutName
.indexOf(SD_LT_SEPARATOR
) + 4;
70 aLayoutName
= aLayoutName
.copy(0, nLen
);
72 if( (maStyleSheets
.empty()) || !(*maStyleSheets
.begin()).second
->GetName().startsWith( aLayoutName
) )
74 maStyleSheets
.clear();
76 // The iterator will return only style sheets of family master page
77 std::shared_ptr
<SfxStyleSheetIterator
> aSSSIterator
= std::make_shared
<SfxStyleSheetIterator
>(mxPool
.get(), SfxStyleFamily::Page
);
78 for ( SfxStyleSheetBase
* pStyle
= aSSSIterator
->First(); pStyle
;
79 pStyle
= aSSSIterator
->Next() )
81 // we assume that we have only SdStyleSheets
82 SdStyleSheet
* pSdStyle
= static_cast< SdStyleSheet
* >( pStyle
);
83 if (pSdStyle
->GetName().startsWith(aLayoutName
))
85 maStyleSheets
[ pSdStyle
->GetApiName() ].set( pSdStyle
);
94 SdStyleFamily::SdStyleFamily( const rtl::Reference
< SfxStyleSheetPool
>& xPool
, SfxStyleFamily nFamily
)
100 SdStyleFamily::SdStyleFamily( const rtl::Reference
< SfxStyleSheetPool
>& xPool
, const SdPage
* pMasterPage
)
101 : mnFamily( SfxStyleFamily::Page
)
103 , mpImpl( new SdStyleFamilyImpl
)
105 mpImpl
->mxMasterPage
.reset( const_cast< SdPage
* >( pMasterPage
) );
106 mpImpl
->mxPool
= xPool
;
109 SdStyleFamily::~SdStyleFamily()
111 DBG_ASSERT( !mxPool
.is(), "SdStyleFamily::~SdStyleFamily(), dispose me first!" );
114 void SdStyleFamily::throwIfDisposed() const
117 throw DisposedException();
120 SdStyleSheet
* SdStyleFamily::GetValidNewSheet( const Any
& rElement
)
122 Reference
< XStyle
> xStyle( rElement
, UNO_QUERY
);
123 SdStyleSheet
* pStyle
= static_cast< SdStyleSheet
* >( xStyle
.get() );
125 if( pStyle
== nullptr || (pStyle
->GetFamily() != mnFamily
) || (pStyle
->GetPool() != mxPool
.get()) || (mxPool
->Find( pStyle
->GetName(), mnFamily
) != nullptr) )
126 throw IllegalArgumentException();
131 SdStyleSheet
* SdStyleFamily::GetSheetByName( const OUString
& rName
)
133 SdStyleSheet
* pRet
= nullptr;
134 if( !rName
.isEmpty() )
136 if( mnFamily
== SfxStyleFamily::Page
)
138 PresStyleMap
& rStyleMap
= mpImpl
->getStyleSheets();
139 PresStyleMap::iterator
iter( rStyleMap
.find(rName
) );
140 if( iter
!= rStyleMap
.end() )
141 pRet
= (*iter
).second
.get();
145 std::shared_ptr
<SfxStyleSheetIterator
> aSSSIterator
= std::make_shared
<SfxStyleSheetIterator
>(mxPool
.get(), mnFamily
);
146 for ( SfxStyleSheetBase
* pStyle
= aSSSIterator
->First(); pStyle
;
147 pStyle
= aSSSIterator
->Next() )
149 // we assume that we have only SdStyleSheets
150 SdStyleSheet
* pSdStyle
= static_cast< SdStyleSheet
* >( pStyle
);
151 if( pSdStyle
&& pSdStyle
->GetApiName() == rName
)
162 throw NoSuchElementException();
166 OUString SAL_CALL
SdStyleFamily::getImplementationName()
168 return OUString( "SdStyleFamily" );
171 sal_Bool SAL_CALL
SdStyleFamily::supportsService( const OUString
& ServiceName
)
173 return cppu::supportsService( this, ServiceName
);
176 Sequence
< OUString
> SAL_CALL
SdStyleFamily::getSupportedServiceNames()
178 OUString
aServiceName( "com.sun.star.style.StyleFamily" );
179 Sequence
< OUString
> aSeq( &aServiceName
, 1 );
184 OUString SAL_CALL
SdStyleFamily::getName()
186 if( mnFamily
== SfxStyleFamily::Page
)
188 SdPage
* pPage
= mpImpl
->mxMasterPage
.get();
189 if( pPage
== nullptr )
190 throw DisposedException();
192 OUString
aLayoutName( pPage
->GetLayoutName() );
193 const OUString
aSep( SD_LT_SEPARATOR
);
194 sal_Int32 nIndex
= aLayoutName
.indexOf(aSep
);
196 aLayoutName
= aLayoutName
.copy(0, nIndex
);
202 return SdStyleSheet::GetFamilyString( mnFamily
);
206 void SAL_CALL
SdStyleFamily::setName( const OUString
& )
212 Any SAL_CALL
SdStyleFamily::getByName( const OUString
& rName
)
214 SolarMutexGuard aGuard
;
216 return Any( Reference
< XStyle
>( static_cast<SfxUnoStyleSheet
*>(GetSheetByName( rName
)) ) );
219 Sequence
< OUString
> SAL_CALL
SdStyleFamily::getElementNames()
221 SolarMutexGuard aGuard
;
225 if( mnFamily
== SfxStyleFamily::Page
)
227 PresStyleMap
& rStyleMap
= mpImpl
->getStyleSheets();
228 Sequence
< OUString
> aNames( rStyleMap
.size() );
230 PresStyleMap::iterator
iter( rStyleMap
.begin() );
231 OUString
* pNames
= aNames
.getArray();
232 while( iter
!= rStyleMap
.end() )
234 rtl::Reference
< SdStyleSheet
> xStyle( (*iter
++).second
);
237 *pNames
++ = xStyle
->GetApiName();
245 std::vector
< OUString
> aNames
;
246 std::shared_ptr
<SfxStyleSheetIterator
> aSSSIterator
= std::make_shared
<SfxStyleSheetIterator
>(mxPool
.get(), mnFamily
);
247 for ( SfxStyleSheetBase
* pStyle
= aSSSIterator
->First(); pStyle
;
248 pStyle
= aSSSIterator
->Next() )
250 // we assume that we have only SdStyleSheets
251 SdStyleSheet
* pSdStyle
= static_cast< SdStyleSheet
* >( pStyle
);
254 aNames
.push_back( pSdStyle
->GetApiName() );
257 return Sequence
< OUString
>( &(*aNames
.begin()), aNames
.size() );
261 sal_Bool SAL_CALL
SdStyleFamily::hasByName( const OUString
& aName
)
263 SolarMutexGuard aGuard
;
266 if( !aName
.isEmpty() )
268 if( mnFamily
== SfxStyleFamily::Page
)
270 PresStyleMap
& rStyleSheets
= mpImpl
->getStyleSheets();
271 PresStyleMap::iterator
iter( rStyleSheets
.find(aName
) );
272 return iter
!= rStyleSheets
.end();
276 std::shared_ptr
<SfxStyleSheetIterator
> aSSSIterator
= std::make_shared
<SfxStyleSheetIterator
>(mxPool
.get(), mnFamily
);
277 for ( SfxStyleSheetBase
* pStyle
= aSSSIterator
->First(); pStyle
;
278 pStyle
= aSSSIterator
->Next() )
280 // we assume that we have only SdStyleSheets
281 SdStyleSheet
* pSdStyle
= static_cast< SdStyleSheet
* >( pStyle
);
284 if (pSdStyle
->GetApiName() == aName
)
298 Type SAL_CALL
SdStyleFamily::getElementType()
300 return cppu::UnoType
<XStyle
>::get();
303 sal_Bool SAL_CALL
SdStyleFamily::hasElements()
305 SolarMutexGuard aGuard
;
308 if( mnFamily
== SfxStyleFamily::Page
)
314 std::shared_ptr
<SfxStyleSheetIterator
> aSSSIterator
= std::make_shared
<SfxStyleSheetIterator
>(mxPool
.get(), mnFamily
);
315 if (aSSSIterator
->First())
326 sal_Int32 SAL_CALL
SdStyleFamily::getCount()
328 SolarMutexGuard aGuard
;
331 sal_Int32 nCount
= 0;
332 if( mnFamily
== SfxStyleFamily::Page
)
334 return mpImpl
->getStyleSheets().size();
338 std::shared_ptr
<SfxStyleSheetIterator
> aSSSIterator
= std::make_shared
<SfxStyleSheetIterator
>(mxPool
.get(), mnFamily
);
339 for ( SfxStyleSheetBase
* pStyle
= aSSSIterator
->First(); pStyle
;
340 pStyle
= aSSSIterator
->Next() )
349 Any SAL_CALL
SdStyleFamily::getByIndex( sal_Int32 Index
)
351 SolarMutexGuard aGuard
;
356 if( mnFamily
== SfxStyleFamily::Page
)
358 PresStyleMap
& rStyleSheets
= mpImpl
->getStyleSheets();
359 if( !rStyleSheets
.empty() )
361 PresStyleMap::iterator
iter( rStyleSheets
.begin() );
362 while( Index
-- && (iter
!= rStyleSheets
.end()) )
365 if( (Index
==-1) && (iter
!= rStyleSheets
.end()) )
366 return Any( Reference
< XStyle
>( (*iter
).second
.get() ) );
371 std::shared_ptr
<SfxStyleSheetIterator
> aSSSIterator
= std::make_shared
<SfxStyleSheetIterator
>(mxPool
.get(), mnFamily
);
372 for ( SfxStyleSheetBase
* pStyle
= aSSSIterator
->First(); pStyle
;
373 pStyle
= aSSSIterator
->Next() )
375 // we assume that we have only SdStyleSheets
376 SdStyleSheet
* pSdStyle
= static_cast< SdStyleSheet
* >( pStyle
);
379 return Any( Reference
< XStyle
>( pSdStyle
) );
385 throw IndexOutOfBoundsException();
390 void SAL_CALL
SdStyleFamily::insertByName( const OUString
& rName
, const Any
& rElement
)
392 SolarMutexGuard aGuard
;
396 throw IllegalArgumentException();
398 SdStyleSheet
* pStyle
= GetValidNewSheet( rElement
);
399 if( !pStyle
->SetName( rName
) )
400 throw ElementExistException();
402 pStyle
->SetApiName( rName
);
403 mxPool
->Insert( pStyle
);
406 void SAL_CALL
SdStyleFamily::removeByName( const OUString
& rName
)
408 SolarMutexGuard aGuard
;
411 SdStyleSheet
* pStyle
= GetSheetByName( rName
);
413 if( !pStyle
->IsUserDefined() )
414 throw WrappedTargetException();
416 mxPool
->Remove( pStyle
);
421 void SAL_CALL
SdStyleFamily::replaceByName( const OUString
& rName
, const Any
& aElement
)
423 SolarMutexGuard aGuard
;
426 SdStyleSheet
* pOldStyle
= GetSheetByName( rName
);
427 SdStyleSheet
* pNewStyle
= GetValidNewSheet( aElement
);
429 mxPool
->Remove( pOldStyle
);
430 mxPool
->Insert( pNewStyle
);
433 // XSingleServiceFactory
435 Reference
< XInterface
> SAL_CALL
SdStyleFamily::createInstance()
437 SolarMutexGuard aGuard
;
440 if( mnFamily
== SfxStyleFamily::Page
)
442 throw IllegalAccessException();
444 return Reference
< XInterface
>( static_cast< XStyle
* >( SdStyleSheet::CreateEmptyUserStyle( *mxPool
.get(), mnFamily
) ) );
447 Reference
< XInterface
> SAL_CALL
SdStyleFamily::createInstanceWithArguments( const Sequence
< Any
>& )
449 return createInstance();
454 void SAL_CALL
SdStyleFamily::dispose( )
462 void SAL_CALL
SdStyleFamily::addEventListener( const Reference
< XEventListener
>& )
466 void SAL_CALL
SdStyleFamily::removeEventListener( const Reference
< XEventListener
>& )
472 Reference
<XPropertySetInfo
> SdStyleFamily::getPropertySetInfo()
474 OSL_FAIL( "###unexpected!" );
475 return Reference
<XPropertySetInfo
>();
478 void SdStyleFamily::setPropertyValue( const OUString
& , const Any
& )
480 OSL_FAIL( "###unexpected!" );
483 Any
SdStyleFamily::getPropertyValue( const OUString
& PropertyName
)
485 if ( PropertyName
!= "DisplayName" )
487 throw UnknownPropertyException( "unknown property: " + PropertyName
, static_cast<OWeakObject
*>(this) );
490 SolarMutexGuard aGuard
;
491 OUString sDisplayName
;
494 case SfxStyleFamily::Page
: sDisplayName
= getName(); break;
495 case SfxStyleFamily::Frame
: sDisplayName
= SdResId(STR_CELL_STYLE_FAMILY
); break;
496 default: sDisplayName
= SdResId(STR_GRAPHICS_STYLE_FAMILY
); break;
498 return Any( sDisplayName
);
501 void SdStyleFamily::addPropertyChangeListener( const OUString
& , const Reference
<XPropertyChangeListener
>& )
503 OSL_FAIL( "###unexpected!" );
506 void SdStyleFamily::removePropertyChangeListener( const OUString
& , const Reference
<XPropertyChangeListener
>& )
508 OSL_FAIL( "###unexpected!" );
511 void SdStyleFamily::addVetoableChangeListener( const OUString
& , const Reference
<XVetoableChangeListener
>& )
513 OSL_FAIL( "###unexpected!" );
516 void SdStyleFamily::removeVetoableChangeListener( const OUString
& , const Reference
<XVetoableChangeListener
>& )
518 OSL_FAIL( "###unexpected!" );
521 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */