Avoid potential negative array index access to cached text.
[LibreOffice.git] / chart2 / source / controller / chartapiwrapper / TitleWrapper.cxx
blob9c802462ff090af8355883bb71aa9b08ace1d333
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 .
20 #include "TitleWrapper.hxx"
21 #include "Chart2ModelContact.hxx"
22 #include <ControllerLockGuard.hxx>
24 #include <comphelper/sequence.hxx>
25 #include <cppuhelper/supportsservice.hxx>
26 #include <com/sun/star/beans/PropertyAttribute.hpp>
27 #include <com/sun/star/chart2/RelativePosition.hpp>
28 #include <com/sun/star/chart2/XTitle.hpp>
30 #include <CharacterProperties.hxx>
31 #include <LinePropertiesHelper.hxx>
32 #include <FillProperties.hxx>
33 #include <UserDefinedProperties.hxx>
34 #include "WrappedCharacterHeightProperty.hxx"
35 #include "WrappedTextRotationProperty.hxx"
36 #include "WrappedAutomaticPositionProperties.hxx"
37 #include "WrappedScaleTextProperties.hxx"
39 #include <algorithm>
40 #include <rtl/ustrbuf.hxx>
41 #include <cppuhelper/propshlp.hxx>
42 #include <utility>
44 using namespace ::com::sun::star;
45 using ::com::sun::star::beans::Property;
46 using ::com::sun::star::uno::Any;
47 using ::com::sun::star::uno::Reference;
48 using ::com::sun::star::uno::Sequence;
50 namespace chart
52 namespace {
54 class WrappedTitleStringProperty : public WrappedProperty
56 public:
57 explicit WrappedTitleStringProperty( const Reference< uno::XComponentContext >& xContext );
59 virtual void setPropertyValue( const Any& rOuterValue, const Reference< beans::XPropertySet >& xInnerPropertySet ) const override;
60 virtual Any getPropertyValue( const Reference< beans::XPropertySet >& xInnerPropertySet ) const override;
61 virtual Any getPropertyDefault( const Reference< beans::XPropertyState >& xInnerPropertyState ) const override;
63 protected:
64 Reference< uno::XComponentContext > m_xContext;
69 WrappedTitleStringProperty::WrappedTitleStringProperty( const Reference< uno::XComponentContext >& xContext )
70 : ::chart::WrappedProperty( "String", OUString() )
71 , m_xContext( xContext )
75 void WrappedTitleStringProperty::setPropertyValue( const Any& rOuterValue, const Reference< beans::XPropertySet >& xInnerPropertySet ) const
77 Title* pTitle = dynamic_cast<Title*>(xInnerPropertySet.get());
78 if(pTitle)
80 OUString aString;
81 rOuterValue >>= aString;
82 TitleHelper::setCompleteString( aString, pTitle, m_xContext );
85 Any WrappedTitleStringProperty::getPropertyValue( const Reference< beans::XPropertySet >& xInnerPropertySet ) const
87 Any aRet( getPropertyDefault( Reference< beans::XPropertyState >( xInnerPropertySet, uno::UNO_QUERY ) ) );
88 Reference< chart2::XTitle > xTitle(xInnerPropertySet,uno::UNO_QUERY);
89 if(xTitle.is())
91 const Sequence< Reference< chart2::XFormattedString > > aStrings( xTitle->getText());
93 OUStringBuffer aBuf;
94 for( Reference< chart2::XFormattedString > const & formattedStr : aStrings )
96 aBuf.append( formattedStr->getString());
98 aRet <<= aBuf.makeStringAndClear();
100 return aRet;
102 Any WrappedTitleStringProperty::getPropertyDefault( const Reference< beans::XPropertyState >& /*xInnerPropertyState*/ ) const
104 return uno::Any( OUString() );//default title is an empty String
107 namespace {
109 class WrappedStackedTextProperty : public WrappedProperty
111 public:
112 WrappedStackedTextProperty();
117 WrappedStackedTextProperty::WrappedStackedTextProperty()
118 : ::chart::WrappedProperty( "StackedText", "StackCharacters" )
122 }// end namespace chart
124 namespace
127 enum
129 PROP_TITLE_STRING,
130 PROP_TITLE_VISIBLE,
131 PROP_TITLE_TEXT_ROTATION,
132 PROP_TITLE_TEXT_STACKED
135 void lcl_AddPropertiesToVector(
136 std::vector< Property > & rOutProperties )
138 rOutProperties.emplace_back( "String",
139 PROP_TITLE_STRING,
140 cppu::UnoType<OUString>::get(),
141 beans::PropertyAttribute::BOUND
142 | beans::PropertyAttribute::MAYBEVOID );
144 rOutProperties.emplace_back( "Visible",
145 PROP_TITLE_VISIBLE,
146 cppu::UnoType<OUString>::get(),
147 beans::PropertyAttribute::BOUND
148 | beans::PropertyAttribute::MAYBEVOID );
150 rOutProperties.emplace_back( "TextRotation",
151 PROP_TITLE_TEXT_ROTATION,
152 cppu::UnoType<sal_Int32>::get(),
153 beans::PropertyAttribute::BOUND
154 | beans::PropertyAttribute::MAYBEDEFAULT );
155 rOutProperties.emplace_back( "StackedText",
156 PROP_TITLE_TEXT_STACKED,
157 cppu::UnoType<bool>::get(),
158 beans::PropertyAttribute::BOUND
159 | beans::PropertyAttribute::MAYBEDEFAULT );
162 const Sequence< Property > & StaticTitleWrapperPropertyArray()
164 static Sequence< Property > aPropSeq = []()
166 std::vector< beans::Property > aProperties;
167 lcl_AddPropertiesToVector( aProperties );
168 ::chart::CharacterProperties::AddPropertiesToVector( aProperties );
169 ::chart::LinePropertiesHelper::AddPropertiesToVector( aProperties );
170 ::chart::FillProperties::AddPropertiesToVector( aProperties );
171 ::chart::UserDefinedProperties::AddPropertiesToVector( aProperties );
172 ::chart::wrapper::WrappedAutomaticPositionProperties::addProperties( aProperties );
173 ::chart::wrapper::WrappedScaleTextProperties::addProperties( aProperties );
175 std::sort( aProperties.begin(), aProperties.end(),
176 ::chart::PropertyNameLess() );
178 return comphelper::containerToSequence( aProperties );
179 }();
180 return aPropSeq;
184 } // anonymous namespace
186 namespace chart::wrapper
189 TitleWrapper::TitleWrapper( ::chart::TitleHelper::eTitleType eTitleType,
190 std::shared_ptr<Chart2ModelContact> spChart2ModelContact ) :
191 m_spChart2ModelContact(std::move( spChart2ModelContact )),
192 m_eTitleType(eTitleType)
194 ControllerLockGuardUNO aCtrlLockGuard( m_spChart2ModelContact->getDocumentModel() );
195 if( !getTitleObject().is() ) //#i83831# create an empty title at the model, thus references to properties can be mapped correctly
196 TitleHelper::createTitle( m_eTitleType, OUString(), m_spChart2ModelContact->getDocumentModel(), m_spChart2ModelContact->m_xContext );
199 TitleWrapper::~TitleWrapper()
203 // ____ XShape ____
204 awt::Point SAL_CALL TitleWrapper::getPosition()
206 return m_spChart2ModelContact->GetTitlePosition( getTitleObject() );
209 void SAL_CALL TitleWrapper::setPosition( const awt::Point& aPosition )
211 Reference< beans::XPropertySet > xPropertySet( getInnerPropertySet() );
212 if(xPropertySet.is())
214 awt::Size aPageSize( m_spChart2ModelContact->GetPageSize() );
216 chart2::RelativePosition aRelativePosition;
217 aRelativePosition.Anchor = drawing::Alignment_TOP_LEFT;
218 aRelativePosition.Primary = double(aPosition.X)/double(aPageSize.Width);
219 aRelativePosition.Secondary = double(aPosition.Y)/double(aPageSize.Height);
220 xPropertySet->setPropertyValue( "RelativePosition", uno::Any(aRelativePosition) );
224 awt::Size SAL_CALL TitleWrapper::getSize()
226 return m_spChart2ModelContact->GetTitleSize( getTitleObject() );
229 void SAL_CALL TitleWrapper::setSize( const awt::Size& /*aSize*/ )
231 OSL_FAIL( "trying to set size of title" );
234 // ____ XShapeDescriptor (base of XShape) ____
235 OUString SAL_CALL TitleWrapper::getShapeType()
237 return "com.sun.star.chart.ChartTitle";
240 // ____ XComponent ____
241 void SAL_CALL TitleWrapper::dispose()
243 std::unique_lock g(m_aMutex);
244 Reference< uno::XInterface > xSource( static_cast< ::cppu::OWeakObject* >( this ) );
245 m_aEventListenerContainer.disposeAndClear( g, lang::EventObject( xSource ) );
247 clearWrappedPropertySet();
250 void SAL_CALL TitleWrapper::addEventListener(
251 const Reference< lang::XEventListener >& xListener )
253 std::unique_lock g(m_aMutex);
254 m_aEventListenerContainer.addInterface( g, xListener );
257 void SAL_CALL TitleWrapper::removeEventListener(
258 const Reference< lang::XEventListener >& aListener )
260 std::unique_lock g(m_aMutex);
261 m_aEventListenerContainer.removeInterface( g, aListener );
264 Reference< beans::XPropertySet > TitleWrapper::getFirstCharacterPropertySet()
266 Reference< beans::XPropertySet > xProp;
268 Reference< chart2::XTitle > xTitle( getTitleObject() );
269 if( xTitle.is())
271 Sequence< Reference< chart2::XFormattedString > > aStrings( xTitle->getText());
272 if( aStrings.hasElements() )
273 xProp.set( aStrings[0], uno::UNO_QUERY );
276 return xProp;
279 void TitleWrapper::getFastCharacterPropertyValue( sal_Int32 nHandle, Any& rValue )
281 OSL_ASSERT( FAST_PROPERTY_ID_START_CHAR_PROP <= nHandle &&
282 nHandle < CharacterProperties::FAST_PROPERTY_ID_END_CHAR_PROP );
284 Reference< beans::XPropertySet > xProp = getFirstCharacterPropertySet();
285 Reference< beans::XFastPropertySet > xFastProp( xProp, uno::UNO_QUERY );
286 if(xProp.is())
288 const WrappedProperty* pWrappedProperty = getWrappedProperty( nHandle );
289 if( pWrappedProperty )
291 rValue = pWrappedProperty->getPropertyValue( xProp );
293 else if( xFastProp.is() )
295 rValue = xFastProp->getFastPropertyValue( nHandle );
301 void TitleWrapper::setFastCharacterPropertyValue(
302 sal_Int32 nHandle, const Any& rValue )
304 OSL_ASSERT( FAST_PROPERTY_ID_START_CHAR_PROP <= nHandle &&
305 nHandle < CharacterProperties::FAST_PROPERTY_ID_END_CHAR_PROP );
307 Reference< chart2::XTitle > xTitle( getTitleObject() );
308 if( !xTitle.is())
309 return;
311 const Sequence< Reference< chart2::XFormattedString > > aStrings( xTitle->getText());
312 const WrappedProperty* pWrappedProperty = getWrappedProperty( nHandle );
314 for( Reference< chart2::XFormattedString > const & formattedStr : aStrings )
316 Reference< beans::XFastPropertySet > xFastPropertySet( formattedStr, uno::UNO_QUERY );
317 Reference< beans::XPropertySet > xPropSet( xFastPropertySet, uno::UNO_QUERY );
319 if( pWrappedProperty )
320 pWrappedProperty->setPropertyValue( rValue, xPropSet );
321 else if( xFastPropertySet.is() )
322 xFastPropertySet->setFastPropertyValue( nHandle, rValue );
326 // WrappedPropertySet
328 void SAL_CALL TitleWrapper::setPropertyValue( const OUString& rPropertyName, const Any& rValue )
330 sal_Int32 nHandle = getInfoHelper().getHandleByName( rPropertyName );
331 if( CharacterProperties::IsCharacterPropertyHandle( nHandle ) )
333 setFastCharacterPropertyValue( nHandle, rValue );
335 else
336 WrappedPropertySet::setPropertyValue( rPropertyName, rValue );
339 Any SAL_CALL TitleWrapper::getPropertyValue( const OUString& rPropertyName )
341 Any aRet;
342 sal_Int32 nHandle = getInfoHelper().getHandleByName( rPropertyName );
343 if( CharacterProperties::IsCharacterPropertyHandle( nHandle ) )
344 getFastCharacterPropertyValue( nHandle, aRet );
345 else
346 aRet = WrappedPropertySet::getPropertyValue( rPropertyName );
347 return aRet;
350 beans::PropertyState SAL_CALL TitleWrapper::getPropertyState( const OUString& rPropertyName )
352 beans::PropertyState aState( beans::PropertyState_DIRECT_VALUE );
354 sal_Int32 nHandle = getInfoHelper().getHandleByName( rPropertyName );
355 if( CharacterProperties::IsCharacterPropertyHandle( nHandle ) )
357 Reference< beans::XPropertyState > xPropState( getFirstCharacterPropertySet(), uno::UNO_QUERY );
358 if( xPropState.is() )
360 const WrappedProperty* pWrappedProperty = getWrappedProperty( rPropertyName );
361 if( pWrappedProperty )
362 aState = pWrappedProperty->getPropertyState( xPropState );
363 else
364 aState = xPropState->getPropertyState( rPropertyName );
367 else
368 aState = WrappedPropertySet::getPropertyState( rPropertyName );
370 return aState;
372 void SAL_CALL TitleWrapper::setPropertyToDefault( const OUString& rPropertyName )
374 sal_Int32 nHandle = getInfoHelper().getHandleByName( rPropertyName );
375 if( CharacterProperties::IsCharacterPropertyHandle( nHandle ) )
377 Any aDefault = getPropertyDefault( rPropertyName );
378 setFastCharacterPropertyValue( nHandle, aDefault );
380 else
381 WrappedPropertySet::setPropertyToDefault( rPropertyName );
383 Any SAL_CALL TitleWrapper::getPropertyDefault( const OUString& rPropertyName )
385 Any aRet;
387 sal_Int32 nHandle = getInfoHelper().getHandleByName( rPropertyName );
388 if( CharacterProperties::IsCharacterPropertyHandle( nHandle ) )
390 Reference< beans::XPropertyState > xPropState( getFirstCharacterPropertySet(), uno::UNO_QUERY );
391 if( xPropState.is() )
393 const WrappedProperty* pWrappedProperty = getWrappedProperty( rPropertyName );
394 if( pWrappedProperty )
395 aRet = pWrappedProperty->getPropertyDefault(xPropState);
396 else
397 aRet = xPropState->getPropertyDefault( rPropertyName );
400 else
401 aRet = WrappedPropertySet::getPropertyDefault( rPropertyName );
403 return aRet;
406 void SAL_CALL TitleWrapper::addPropertyChangeListener( const OUString& rPropertyName, const Reference< beans::XPropertyChangeListener >& xListener )
408 sal_Int32 nHandle = getInfoHelper().getHandleByName( rPropertyName );
409 if( CharacterProperties::IsCharacterPropertyHandle( nHandle ) )
411 Reference< beans::XPropertySet > xPropSet = getFirstCharacterPropertySet();
412 if( xPropSet.is() )
413 xPropSet->addPropertyChangeListener( rPropertyName, xListener );
415 else
416 WrappedPropertySet::addPropertyChangeListener( rPropertyName, xListener );
418 void SAL_CALL TitleWrapper::removePropertyChangeListener( const OUString& rPropertyName, const Reference< beans::XPropertyChangeListener >& xListener )
420 sal_Int32 nHandle = getInfoHelper().getHandleByName( rPropertyName );
421 if( CharacterProperties::IsCharacterPropertyHandle( nHandle ) )
423 Reference< beans::XPropertySet > xPropSet = getFirstCharacterPropertySet();
424 if( xPropSet.is() )
425 xPropSet->removePropertyChangeListener( rPropertyName, xListener );
427 else
428 WrappedPropertySet::removePropertyChangeListener( rPropertyName, xListener );
431 //ReferenceSizePropertyProvider
432 void TitleWrapper::updateReferenceSize()
434 Reference< beans::XPropertySet > xProp( getTitleObject(), uno::UNO_QUERY );
435 if( xProp.is() )
437 if( xProp->getPropertyValue( "ReferencePageSize" ).hasValue() )
438 xProp->setPropertyValue( "ReferencePageSize", uno::Any(
439 m_spChart2ModelContact->GetPageSize() ));
442 Any TitleWrapper::getReferenceSize()
444 Any aRet;
445 Reference< beans::XPropertySet > xProp( getTitleObject(), uno::UNO_QUERY );
446 if( xProp.is() )
447 aRet = xProp->getPropertyValue( "ReferencePageSize" );
449 return aRet;
451 awt::Size TitleWrapper::getCurrentSizeForReference()
453 return m_spChart2ModelContact->GetPageSize();
456 Reference< chart2::XTitle > TitleWrapper::getTitleObject()
458 return TitleHelper::getTitle( m_eTitleType, m_spChart2ModelContact->getDocumentModel() );
461 // WrappedPropertySet
463 Reference< beans::XPropertySet > TitleWrapper::getInnerPropertySet()
465 return Reference< beans::XPropertySet >( getTitleObject(), uno::UNO_QUERY );
468 const Sequence< beans::Property >& TitleWrapper::getPropertySequence()
470 return StaticTitleWrapperPropertyArray();
473 std::vector< std::unique_ptr<WrappedProperty> > TitleWrapper::createWrappedProperties()
475 std::vector< std::unique_ptr<WrappedProperty> > aWrappedProperties;
477 aWrappedProperties.emplace_back( new WrappedTitleStringProperty( m_spChart2ModelContact->m_xContext ) );
478 aWrappedProperties.emplace_back( new WrappedTextRotationProperty( true ) );
479 aWrappedProperties.emplace_back( new WrappedStackedTextProperty() );
480 WrappedCharacterHeightProperty::addWrappedProperties( aWrappedProperties, this );
481 WrappedAutomaticPositionProperties::addWrappedProperties( aWrappedProperties );
482 WrappedScaleTextProperties::addWrappedProperties( aWrappedProperties, m_spChart2ModelContact );
484 return aWrappedProperties;
487 OUString SAL_CALL TitleWrapper::getImplementationName()
489 return "com.sun.star.comp.chart.Title";
492 sal_Bool SAL_CALL TitleWrapper::supportsService( const OUString& rServiceName )
494 return cppu::supportsService(this, rServiceName);
497 css::uno::Sequence< OUString > SAL_CALL TitleWrapper::getSupportedServiceNames()
499 return {
500 "com.sun.star.chart.ChartTitle",
501 "com.sun.star.drawing.Shape",
502 "com.sun.star.xml.UserDefinedAttributesSupplier",
503 "com.sun.star.style.CharacterProperties"
507 } // namespace chart
509 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */