bump product version to 4.1.6.2
[LibreOffice.git] / sd / source / core / stlfamily.cxx
blob2f140a2d438fd8fcb8ab1f13fda4e6fd744ebcdf
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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"
37 #include "sdpage.hxx"
38 #include "glob.hxx"
40 #include <map>
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;
60 private:
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 );
88 return maStyleSheets;
91 // ----------------------------------------------------------
93 SdStyleFamily::SdStyleFamily( const rtl::Reference< SfxStyleSheetPool >& xPool, SfxStyleFamily nFamily )
94 : mnFamily( nFamily )
95 , mxPool( xPool )
96 , mpImpl( 0 )
100 // ----------------------------------------------------------
102 SdStyleFamily::SdStyleFamily( const rtl::Reference< SfxStyleSheetPool >& xPool, const SdPage* pMasterPage )
103 : mnFamily( SD_STYLE_FAMILY_MASTERPAGE )
104 , mxPool( xPool )
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!" );
116 delete mpImpl;
119 // ----------------------------------------------------------
121 void SdStyleFamily::throwIfDisposed() const throw(RuntimeException)
123 if( !mxPool.is() )
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();
137 return pStyle;
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();
154 else
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) )
162 pRet = pStyle;
163 break;
168 if( pRet )
169 return pRet;
171 throw NoSuchElementException();
174 // ----------------------------------------------------------
175 // XServiceInfo
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 );
196 return aSeq;
199 // ----------------------------------------------------------
200 // XNamed
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() );
208 if( pPage == 0 )
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 );
217 else
219 return SdStyleSheet::GetFamilyString( mnFamily );
223 // ----------------------------------------------------------
225 void SAL_CALL SdStyleFamily::setName( const OUString& ) throw (RuntimeException)
229 // ----------------------------------------------------------
230 // XNameAccess
231 // ----------------------------------------------------------
233 Any SAL_CALL SdStyleFamily::getByName( const OUString& rName ) throw(NoSuchElementException, WrappedTargetException, RuntimeException)
235 SolarMutexGuard aGuard;
236 throwIfDisposed();
237 return Any( Reference< XStyle >( static_cast<SfxUnoStyleSheet*>(GetSheetByName( rName )) ) );
240 // ----------------------------------------------------------
242 Sequence< OUString > SAL_CALL SdStyleFamily::getElementNames() throw(RuntimeException)
244 SolarMutexGuard aGuard;
246 throwIfDisposed();
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 );
258 if( xStyle.is() )
260 *pNames++ = xStyle->GetApiName();
264 return aNames;
266 else
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;
285 throwIfDisposed();
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;
295 else
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 ) )
302 return sal_True;
307 return sal_False;
310 // ----------------------------------------------------------
311 // XElementAccess
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;
324 throwIfDisposed();
326 if( mnFamily == SD_STYLE_FAMILY_MASTERPAGE )
328 return sal_True;
330 else
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) )
337 return sal_True;
341 return sal_False;
344 // ----------------------------------------------------------
345 // XIndexAccess
346 // ----------------------------------------------------------
348 sal_Int32 SAL_CALL SdStyleFamily::getCount() throw(RuntimeException)
350 SolarMutexGuard aGuard;
351 throwIfDisposed();
353 sal_Int32 nCount = 0;
354 if( mnFamily == SD_STYLE_FAMILY_MASTERPAGE )
356 return mpImpl->getStyleSheets().size();
358 else
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) )
365 nCount++;
369 return nCount;
372 // ----------------------------------------------------------
374 Any SAL_CALL SdStyleFamily::getByIndex( sal_Int32 Index ) throw(IndexOutOfBoundsException, WrappedTargetException, RuntimeException)
376 SolarMutexGuard aGuard;
377 throwIfDisposed();
379 if( Index >= 0 )
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()) )
388 ++iter;
390 if( (Index==-1) && (iter != rStyleSheets.end()) )
391 return Any( Reference< XStyle >( (*iter).second.get() ) );
394 else
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) )
402 if( Index-- == 0 )
403 return Any( Reference< XStyle >( pStyle ) );
409 throw IndexOutOfBoundsException();
412 // ----------------------------------------------------------
413 // XNameContainer
414 // ----------------------------------------------------------
416 void SAL_CALL SdStyleFamily::insertByName( const OUString& rName, const Any& rElement ) throw(IllegalArgumentException, ElementExistException, WrappedTargetException, RuntimeException)
418 SolarMutexGuard aGuard;
419 throwIfDisposed();
421 if(rName.isEmpty())
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;
437 throwIfDisposed();
439 SdStyleSheet* pStyle = GetSheetByName( rName );
441 if( !pStyle->IsUserDefined() )
442 throw WrappedTargetException();
444 mxPool->Remove( pStyle );
447 // ----------------------------------------------------------
448 // XNameReplace
449 // ----------------------------------------------------------
451 void SAL_CALL SdStyleFamily::replaceByName( const OUString& rName, const Any& aElement ) throw(IllegalArgumentException, NoSuchElementException, WrappedTargetException, RuntimeException)
453 SolarMutexGuard aGuard;
454 throwIfDisposed();
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;
470 throwIfDisposed();
472 if( mnFamily == SD_STYLE_FAMILY_MASTERPAGE )
474 throw IllegalAccessException();
476 else
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 // ----------------------------------------------------------
490 // XComponent
491 // ----------------------------------------------------------
493 void SAL_CALL SdStyleFamily::dispose( ) throw (RuntimeException)
495 if( mxPool.is() )
496 mxPool.clear();
498 if( mpImpl )
500 delete mpImpl;
501 mpImpl = 0;
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 // ----------------------------------------------------------
518 // XPropertySet
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;
542 switch( mnFamily )
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 );
550 else
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: */