bump product version to 7.6.3.2-android
[LibreOffice.git] / xmloff / source / forms / gridcolumnproptranslator.cxx
blobaf52a89602b45d21916355272f0be7243fc8c4c3
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 "gridcolumnproptranslator.hxx"
22 #include <com/sun/star/beans/XPropertySetInfo.hpp>
23 #include <com/sun/star/awt/TextAlign.hpp>
24 #include <com/sun/star/style/ParagraphAdjust.hpp>
25 #include <osl/diagnose.h>
26 #include <cppuhelper/implbase1.hxx>
28 #include <algorithm>
30 namespace xmloff
33 using namespace ::com::sun::star::uno;
34 using namespace ::com::sun::star::awt;
35 using namespace ::com::sun::star;
36 using namespace ::com::sun::star::lang;
37 using namespace ::com::sun::star::beans;
38 using namespace ::com::sun::star::style;
40 namespace
42 OUString getParaAlignProperty()
44 return "ParaAdjust";
47 OUString getAlignProperty()
49 return "Align";
52 sal_Int32 findStringElement( const Sequence< OUString >& _rNames, const OUString& _rName )
54 const OUString* pPos = ::std::find( _rNames.begin(), _rNames.end(), _rName );
55 if ( pPos != _rNames.end() )
56 return pPos - _rNames.begin();
57 return -1;
60 struct AlignmentTranslationEntry
62 ParagraphAdjust nParagraphValue;
63 sal_Int16 nControlValue;
65 const AlignmentTranslations[] =
67 // note that order matters:
68 // valueAlignToParaAdjust and valueParaAdjustToAlign search this map from the _beginning_
69 // and use the first matching entry
70 { ParagraphAdjust_LEFT, awt::TextAlign::LEFT },
71 { ParagraphAdjust_CENTER, awt::TextAlign::CENTER },
72 { ParagraphAdjust_RIGHT, awt::TextAlign::RIGHT },
73 { ParagraphAdjust_BLOCK, awt::TextAlign::RIGHT },
74 { ParagraphAdjust_STRETCH, awt::TextAlign::LEFT },
75 { ParagraphAdjust::ParagraphAdjust_MAKE_FIXED_SIZE, awt::TextAlign::LEFT },
76 { ParagraphAdjust::ParagraphAdjust_MAKE_FIXED_SIZE, -1 }
79 void valueAlignToParaAdjust(Any& rValue)
81 sal_Int16 nValue = 0;
82 rValue >>= nValue;
83 const AlignmentTranslationEntry* pTranslation = AlignmentTranslations;
84 while (-1 != pTranslation->nControlValue)
86 if ( nValue == pTranslation->nControlValue )
88 rValue <<= pTranslation->nParagraphValue;
89 return;
91 ++pTranslation;
93 OSL_FAIL( "valueAlignToParaAdjust: unreachable!" );
96 void valueParaAdjustToAlign(Any& rValue)
98 sal_Int32 nValue = 0;
99 rValue >>= nValue;
100 const AlignmentTranslationEntry* pTranslation = AlignmentTranslations;
101 while ( ParagraphAdjust::ParagraphAdjust_MAKE_FIXED_SIZE != pTranslation->nParagraphValue)
103 if ( static_cast<ParagraphAdjust>(nValue) == pTranslation->nParagraphValue)
105 rValue <<= pTranslation->nControlValue;
106 return;
108 ++pTranslation;
110 OSL_FAIL( "valueParaAdjustToAlign: unreachable!" );
113 //= OMergedPropertySetInfo
114 typedef ::cppu::WeakAggImplHelper1 < XPropertySetInfo
115 > OMergedPropertySetInfo_Base;
116 class OMergedPropertySetInfo : public OMergedPropertySetInfo_Base
118 private:
119 Reference< XPropertySetInfo > m_xMasterInfo;
121 public:
122 explicit OMergedPropertySetInfo( const Reference< XPropertySetInfo >& _rxMasterInfo );
124 protected:
125 virtual ~OMergedPropertySetInfo() override;
127 // XPropertySetInfo
128 virtual css::uno::Sequence< css::beans::Property > SAL_CALL getProperties( ) override;
129 virtual css::beans::Property SAL_CALL getPropertyByName( const OUString& aName ) override;
130 virtual sal_Bool SAL_CALL hasPropertyByName( const OUString& Name ) override;
133 OMergedPropertySetInfo::OMergedPropertySetInfo( const Reference< XPropertySetInfo >& _rxMasterInfo )
134 :m_xMasterInfo( _rxMasterInfo )
136 OSL_ENSURE( m_xMasterInfo.is(), "OMergedPropertySetInfo::OMergedPropertySetInfo: hmm?" );
139 OMergedPropertySetInfo::~OMergedPropertySetInfo()
143 Sequence< Property > SAL_CALL OMergedPropertySetInfo::getProperties( )
145 // add a "ParaAdjust" property to the master properties
146 Sequence< Property > aProperties;
147 if ( m_xMasterInfo.is() )
148 aProperties = m_xMasterInfo->getProperties();
150 sal_Int32 nOldLength = aProperties.getLength();
151 aProperties.realloc( nOldLength + 1 );
152 aProperties.getArray()[ nOldLength ] = getPropertyByName( getParaAlignProperty() );
154 return aProperties;
157 Property SAL_CALL OMergedPropertySetInfo::getPropertyByName( const OUString& aName )
159 if ( aName == getParaAlignProperty() )
160 return Property( getParaAlignProperty(), -1,
161 ::cppu::UnoType<ParagraphAdjust>::get(), 0 );
163 if ( !m_xMasterInfo.is() )
164 return Property();
166 return m_xMasterInfo->getPropertyByName( aName );
169 sal_Bool SAL_CALL OMergedPropertySetInfo::hasPropertyByName( const OUString& Name )
171 if ( Name == getParaAlignProperty() )
172 return true;
174 if ( !m_xMasterInfo.is() )
175 return false;
177 return m_xMasterInfo->hasPropertyByName( Name );
181 //= OGridColumnPropertyTranslator
182 OGridColumnPropertyTranslator::OGridColumnPropertyTranslator( const Reference< XMultiPropertySet >& _rxGridColumn )
183 :m_xGridColumn( _rxGridColumn )
185 OSL_ENSURE( m_xGridColumn.is(), "OGridColumnPropertyTranslator: invalid grid column!" );
188 OGridColumnPropertyTranslator::~OGridColumnPropertyTranslator()
192 Reference< XPropertySetInfo > SAL_CALL OGridColumnPropertyTranslator::getPropertySetInfo( )
194 Reference< XPropertySetInfo > xColumnPropInfo;
195 if ( m_xGridColumn.is() )
196 xColumnPropInfo = m_xGridColumn->getPropertySetInfo();
197 return new OMergedPropertySetInfo( xColumnPropInfo );
200 void SAL_CALL OGridColumnPropertyTranslator::setPropertyValue( const OUString& _rPropertyName, const Any& aValue )
202 // we implement this by delegating it to setPropertyValues, which is to ignore unknown properties. On the other hand, our
203 // contract requires us to throw a UnknownPropertyException for unknown properties, so check this first.
205 if ( !getPropertySetInfo()->hasPropertyByName( _rPropertyName ) )
206 throw UnknownPropertyException( _rPropertyName, *this );
208 Sequence< OUString > aNames( &_rPropertyName, 1 );
209 Sequence< Any > aValues( &aValue, 1 );
210 setPropertyValues( aNames, aValues );
213 Any SAL_CALL OGridColumnPropertyTranslator::getPropertyValue( const OUString& PropertyName )
215 Sequence< OUString > aNames( &PropertyName, 1 );
216 Sequence< Any > aValues = getPropertyValues( aNames );
217 OSL_ENSURE( aValues.getLength() == 1, "OGridColumnPropertyTranslator::getPropertyValue: nonsense!" );
218 if ( aValues.getLength() == 1 )
219 return aValues[0];
220 return Any();
223 void SAL_CALL OGridColumnPropertyTranslator::addPropertyChangeListener( const OUString&, const Reference< XPropertyChangeListener >& )
225 OSL_FAIL( "OGridColumnPropertyTranslator::addPropertyChangeListener: not implemented - this should not be needed!" );
228 void SAL_CALL OGridColumnPropertyTranslator::removePropertyChangeListener( const OUString&, const Reference< XPropertyChangeListener >& )
230 OSL_FAIL( "OGridColumnPropertyTranslator::removePropertyChangeListener: not implemented - this should not be needed!" );
233 void SAL_CALL OGridColumnPropertyTranslator::addVetoableChangeListener( const OUString&, const Reference< XVetoableChangeListener >& )
235 OSL_FAIL( "OGridColumnPropertyTranslator::addVetoableChangeListener: not implemented - this should not be needed!" );
238 void SAL_CALL OGridColumnPropertyTranslator::removeVetoableChangeListener( const OUString&, const Reference< XVetoableChangeListener >& )
240 OSL_FAIL( "OGridColumnPropertyTranslator::removeVetoableChangeListener: not implemented - this should not be needed!" );
243 void SAL_CALL OGridColumnPropertyTranslator::setPropertyValues( const Sequence< OUString >& aPropertyNames, const Sequence< Any >& aValues )
245 if ( !m_xGridColumn.is() )
246 return;
248 // if there's ever the need for more than one property being translated, then we should
249 // certainly have a more clever implementation than this ...
251 Sequence< OUString > aTranslatedNames( aPropertyNames );
252 Sequence< Any > aTranslatedValues( aValues );
254 sal_Int32 nParaAlignPos = findStringElement( aTranslatedNames, getParaAlignProperty() );
255 if ( nParaAlignPos != -1 )
257 if (aTranslatedNames.getLength() != aTranslatedValues.getLength())
258 throw css::lang::IllegalArgumentException(
259 "lengths do not match", getXWeak(), -1);
260 aTranslatedNames.getArray()[ nParaAlignPos ] = getAlignProperty();
261 valueParaAdjustToAlign( aTranslatedValues.getArray()[ nParaAlignPos ] );
264 m_xGridColumn->setPropertyValues( aTranslatedNames, aTranslatedValues );
267 Sequence< Any > SAL_CALL OGridColumnPropertyTranslator::getPropertyValues( const Sequence< OUString >& aPropertyNames )
269 Sequence< Any > aValues( aPropertyNames.getLength() );
270 if ( !m_xGridColumn.is() )
271 return aValues;
273 Sequence< OUString > aTranslatedNames( aPropertyNames );
274 sal_Int32 nAlignPos = findStringElement( aTranslatedNames, getParaAlignProperty() );
275 if ( nAlignPos != -1 )
276 aTranslatedNames.getArray()[ nAlignPos ] = getAlignProperty();
278 aValues = m_xGridColumn->getPropertyValues( aPropertyNames );
279 if ( nAlignPos != -1 )
280 valueAlignToParaAdjust( aValues.getArray()[ nAlignPos ] );
282 return aValues;
285 void SAL_CALL OGridColumnPropertyTranslator::addPropertiesChangeListener( const Sequence< OUString >&, const Reference< XPropertiesChangeListener >& )
287 OSL_FAIL( "OGridColumnPropertyTranslator::addPropertiesChangeListener: not implemented - this should not be needed!" );
290 void SAL_CALL OGridColumnPropertyTranslator::removePropertiesChangeListener( const Reference< XPropertiesChangeListener >& )
292 OSL_FAIL( "OGridColumnPropertyTranslator::removePropertiesChangeListener: not implemented - this should not be needed!" );
295 void SAL_CALL OGridColumnPropertyTranslator::firePropertiesChangeEvent( const Sequence< OUString >&, const Reference< XPropertiesChangeListener >& )
297 OSL_FAIL( "OGridColumnPropertyTranslator::firePropertiesChangeEvent: not implemented - this should not be needed!" );
300 } // namespace xmloff
302 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */