merged tag ooo/OOO330_m14
[LibreOffice.git] / extensions / source / propctrlr / propertyhandler.cxx
blobba40fabdcd1708975d47a21758329163cf0c62e9
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_extensions.hxx"
30 #include "propertyhandler.hxx"
31 #include "formmetadata.hxx"
32 #include "formbrowsertools.hxx"
33 #include "handlerhelper.hxx"
34 #include "formstrings.hxx"
36 /** === begin UNO includes === **/
37 #include <com/sun/star/beans/PropertyAttribute.hpp>
38 #include <com/sun/star/lang/NullPointerException.hpp>
39 #include <com/sun/star/util/XModifiable.hpp>
40 /** === end UNO includes === **/
42 #include <tools/debug.hxx>
43 #include <unotools/confignode.hxx>
44 #include <unotools/localedatawrapper.hxx>
45 #include <unotools/syslocale.hxx>
46 #include <toolkit/helper/vclunohelper.hxx>
48 #include <algorithm>
50 //........................................................................
51 namespace pcr
53 //........................................................................
55 using namespace ::com::sun::star::uno;
56 using namespace ::com::sun::star::awt;
57 using namespace ::com::sun::star::beans;
58 using namespace ::com::sun::star::script;
59 using namespace ::com::sun::star::lang;
60 using namespace ::com::sun::star::util;
61 using namespace ::com::sun::star::frame;
62 using namespace ::com::sun::star::inspection;
63 using namespace ::comphelper;
65 //====================================================================
66 //= PropertyHandler
67 //====================================================================
68 DBG_NAME( PropertyHandler )
69 //--------------------------------------------------------------------
70 PropertyHandler::PropertyHandler( const Reference< XComponentContext >& _rxContext )
71 :PropertyHandler_Base( m_aMutex )
72 ,m_bSupportedPropertiesAreKnown( false )
73 ,m_aPropertyListeners( m_aMutex )
74 ,m_aContext( _rxContext )
75 ,m_pInfoService ( new OPropertyInfoService )
77 DBG_CTOR( PropertyHandler, NULL );
79 m_xTypeConverter = Reference< XTypeConverter >(
80 m_aContext.createComponent( "com.sun.star.script.Converter" ),
81 UNO_QUERY_THROW
85 //--------------------------------------------------------------------
86 PropertyHandler::~PropertyHandler()
88 DBG_DTOR( PropertyHandler, NULL );
91 //--------------------------------------------------------------------
92 void SAL_CALL PropertyHandler::inspect( const Reference< XInterface >& _rxIntrospectee ) throw (RuntimeException, NullPointerException)
94 if ( !_rxIntrospectee.is() )
95 throw NullPointerException();
97 ::osl::MutexGuard aGuard( m_aMutex );
99 Reference< XPropertySet > xNewComponent( _rxIntrospectee, UNO_QUERY );
100 if ( xNewComponent == m_xComponent )
101 return;
103 // remove all old property change listeners
104 ::std::auto_ptr< ::cppu::OInterfaceIteratorHelper > removeListener = m_aPropertyListeners.createIterator();
105 ::std::auto_ptr< ::cppu::OInterfaceIteratorHelper > readdListener = m_aPropertyListeners.createIterator(); // will copy the container as needed
106 while ( removeListener->hasMoreElements() )
107 removePropertyChangeListener( static_cast< XPropertyChangeListener* >( removeListener->next() ) );
108 OSL_ENSURE( m_aPropertyListeners.empty(), "PropertyHandler::inspect: derived classes are expected to forward the removePropertyChangeListener call to their base class (me)!" );
110 // remember the new component, and give derived classes the chance to react on it
111 m_xComponent = xNewComponent;
112 onNewComponent();
114 // add the listeners, again
115 while ( readdListener->hasMoreElements() )
116 addPropertyChangeListener( static_cast< XPropertyChangeListener* >( readdListener->next() ) );
119 //--------------------------------------------------------------------
120 void PropertyHandler::onNewComponent()
122 if ( m_xComponent.is() )
123 m_xComponentPropertyInfo = m_xComponent->getPropertySetInfo();
124 else
125 m_xComponentPropertyInfo.clear();
127 m_bSupportedPropertiesAreKnown = false;
128 m_aSupportedProperties.realloc( 0 );
131 //--------------------------------------------------------------------
132 Sequence< Property > SAL_CALL PropertyHandler::getSupportedProperties() throw (RuntimeException)
134 ::osl::MutexGuard aGuard( m_aMutex );
135 if ( !m_bSupportedPropertiesAreKnown )
137 m_aSupportedProperties = doDescribeSupportedProperties();
138 m_bSupportedPropertiesAreKnown = true;
140 return (Sequence< Property >)m_aSupportedProperties;
143 //--------------------------------------------------------------------
144 Sequence< ::rtl::OUString > SAL_CALL PropertyHandler::getSupersededProperties( ) throw (RuntimeException)
146 return Sequence< ::rtl::OUString >();
149 //--------------------------------------------------------------------
150 Sequence< ::rtl::OUString > SAL_CALL PropertyHandler::getActuatingProperties( ) throw (RuntimeException)
152 return Sequence< ::rtl::OUString >();
155 //--------------------------------------------------------------------
156 Any SAL_CALL PropertyHandler::convertToPropertyValue( const ::rtl::OUString& _rPropertyName, const Any& _rControlValue ) throw (UnknownPropertyException, RuntimeException)
158 ::osl::MutexGuard aGuard( m_aMutex );
159 PropertyId nPropId = m_pInfoService->getPropertyId( _rPropertyName );
160 Property aProperty( impl_getPropertyFromName_throw( _rPropertyName ) );
162 Any aPropertyValue;
163 if ( !_rControlValue.hasValue() )
164 // NULL is converted to NULL
165 return aPropertyValue;
167 if ( ( m_pInfoService->getPropertyUIFlags( nPropId ) & PROP_FLAG_ENUM ) != 0 )
169 ::rtl::OUString sControlValue;
170 OSL_VERIFY( _rControlValue >>= sControlValue );
171 ::rtl::Reference< IPropertyEnumRepresentation > aEnumConversion(
172 new DefaultEnumRepresentation( *m_pInfoService, aProperty.Type, nPropId ) );
173 // TODO/UNOize: cache those converters?
174 aEnumConversion->getValueFromDescription( sControlValue, aPropertyValue );
176 else
177 aPropertyValue = PropertyHandlerHelper::convertToPropertyValue(
178 m_aContext.getContext(),m_xTypeConverter, aProperty, _rControlValue );
179 return aPropertyValue;
182 //--------------------------------------------------------------------
183 Any SAL_CALL PropertyHandler::convertToControlValue( const ::rtl::OUString& _rPropertyName, const Any& _rPropertyValue, const Type& _rControlValueType ) throw (UnknownPropertyException, RuntimeException)
185 ::osl::MutexGuard aGuard( m_aMutex );
186 PropertyId nPropId = m_pInfoService->getPropertyId( _rPropertyName );
188 if ( ( m_pInfoService->getPropertyUIFlags( nPropId ) & PROP_FLAG_ENUM ) != 0 )
190 DBG_ASSERT( _rControlValueType.getTypeClass() == TypeClass_STRING, "PropertyHandler::convertToControlValue: ENUM, but not STRING?" );
192 ::rtl::Reference< IPropertyEnumRepresentation > aEnumConversion(
193 new DefaultEnumRepresentation( *m_pInfoService, _rPropertyValue.getValueType(), nPropId ) );
194 // TODO/UNOize: cache those converters?
195 return makeAny( aEnumConversion->getDescriptionForValue( _rPropertyValue ) );
198 return PropertyHandlerHelper::convertToControlValue(
199 m_aContext.getContext(),m_xTypeConverter, _rPropertyValue, _rControlValueType );
202 //--------------------------------------------------------------------
203 PropertyState SAL_CALL PropertyHandler::getPropertyState( const ::rtl::OUString& /*_rPropertyName*/ ) throw (UnknownPropertyException, RuntimeException)
205 return PropertyState_DIRECT_VALUE;
208 //--------------------------------------------------------------------
209 LineDescriptor SAL_CALL PropertyHandler::describePropertyLine( const ::rtl::OUString& _rPropertyName,
210 const Reference< XPropertyControlFactory >& _rxControlFactory )
211 throw (UnknownPropertyException, NullPointerException, RuntimeException)
213 if ( !_rxControlFactory.is() )
214 throw NullPointerException();
216 ::osl::MutexGuard aGuard( m_aMutex );
217 PropertyId nPropId( impl_getPropertyId_throw( _rPropertyName ) );
218 const Property& rProperty( impl_getPropertyFromId_throw( nPropId ) );
220 LineDescriptor aDescriptor;
221 if ( ( m_pInfoService->getPropertyUIFlags( nPropId ) & PROP_FLAG_ENUM ) != 0 )
223 aDescriptor.Control = PropertyHandlerHelper::createListBoxControl(
224 _rxControlFactory, m_pInfoService->getPropertyEnumRepresentations( nPropId ),
225 PropertyHandlerHelper::requiresReadOnlyControl( rProperty.Attributes ), sal_False );
227 else
228 PropertyHandlerHelper::describePropertyLine( rProperty, aDescriptor, _rxControlFactory );
230 aDescriptor.HelpURL = HelpIdUrl::getHelpURL( m_pInfoService->getPropertyHelpId( nPropId ) );
231 aDescriptor.DisplayName = m_pInfoService->getPropertyTranslation( nPropId );
233 if ( ( m_pInfoService->getPropertyUIFlags( nPropId ) & PROP_FLAG_DATA_PROPERTY ) != 0 )
234 aDescriptor.Category = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Data" ) );
235 else
236 aDescriptor.Category = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "General" ) );
237 return aDescriptor;
240 //--------------------------------------------------------------------
241 ::sal_Bool SAL_CALL PropertyHandler::isComposable( const ::rtl::OUString& _rPropertyName ) throw (UnknownPropertyException, RuntimeException)
243 ::osl::MutexGuard aGuard( m_aMutex );
244 return m_pInfoService->isComposeable( _rPropertyName );
247 //--------------------------------------------------------------------
248 InteractiveSelectionResult SAL_CALL PropertyHandler::onInteractivePropertySelection( const ::rtl::OUString& /*_rPropertyName*/, sal_Bool /*_bPrimary*/, Any& /*_rData*/, const Reference< XObjectInspectorUI >& /*_rxInspectorUI*/ ) throw (UnknownPropertyException, NullPointerException, RuntimeException)
250 DBG_ERROR( "PropertyHandler::onInteractivePropertySelection: not implemented!" );
251 return InteractiveSelectionResult_Cancelled;
254 //--------------------------------------------------------------------
255 void SAL_CALL PropertyHandler::actuatingPropertyChanged( const ::rtl::OUString& /*_rActuatingPropertyName*/, const Any& /*_rNewValue*/, const Any& /*_rOldValue*/, const Reference< XObjectInspectorUI >& /*_rxInspectorUI*/, sal_Bool /*_bFirstTimeInit*/ ) throw (NullPointerException, RuntimeException)
257 DBG_ERROR( "PropertyHandler::actuatingPropertyChanged: not implemented!" );
260 //--------------------------------------------------------------------
261 void SAL_CALL PropertyHandler::addPropertyChangeListener( const Reference< XPropertyChangeListener >& _rxListener ) throw (RuntimeException)
263 ::osl::MutexGuard aGuard( m_aMutex );
264 if ( !_rxListener.is() )
265 throw NullPointerException();
266 m_aPropertyListeners.addListener( _rxListener );
269 //--------------------------------------------------------------------
270 void SAL_CALL PropertyHandler::removePropertyChangeListener( const Reference< XPropertyChangeListener >& _rxListener ) throw (RuntimeException)
272 ::osl::MutexGuard aGuard( m_aMutex );
273 m_aPropertyListeners.removeListener( _rxListener );
276 //--------------------------------------------------------------------
277 sal_Bool SAL_CALL PropertyHandler::suspend( sal_Bool /*_bSuspend*/ ) throw (RuntimeException)
279 return sal_True;
282 //--------------------------------------------------------------------
283 IMPLEMENT_FORWARD_XCOMPONENT( PropertyHandler, PropertyHandler_Base )
284 //--------------------------------------------------------------------
285 void SAL_CALL PropertyHandler::disposing()
287 m_xComponent.clear();
288 m_aPropertyListeners.clear();
289 m_xTypeConverter.clear();
290 m_aSupportedProperties.realloc( 0 );
293 //--------------------------------------------------------------------
294 void PropertyHandler::firePropertyChange( const ::rtl::OUString& _rPropName, PropertyId _nPropId, const Any& _rOldValue, const Any& _rNewValue ) SAL_THROW(())
296 PropertyChangeEvent aEvent;
297 aEvent.Source = m_xComponent;
298 aEvent.PropertyHandle = _nPropId;
299 aEvent.PropertyName = _rPropName;
300 aEvent.OldValue = _rOldValue;
301 aEvent.NewValue = _rNewValue;
302 m_aPropertyListeners.notify( aEvent, &XPropertyChangeListener::propertyChange );
305 //--------------------------------------------------------------------
306 const Property* PropertyHandler::impl_getPropertyFromId_nothrow( PropertyId _nPropId ) const
308 const_cast< PropertyHandler* >( this )->getSupportedProperties();
309 const Property* pFound = ::std::find_if( m_aSupportedProperties.begin(), m_aSupportedProperties.end(),
310 FindPropertyByHandle( _nPropId )
312 if ( pFound != m_aSupportedProperties.end() )
313 return &(*pFound);
314 return NULL;
317 //--------------------------------------------------------------------
318 const Property& PropertyHandler::impl_getPropertyFromId_throw( PropertyId _nPropId ) const
320 const Property* pProperty = impl_getPropertyFromId_nothrow( _nPropId );
321 if ( !pProperty )
322 throw UnknownPropertyException();
324 return *pProperty;
327 //--------------------------------------------------------------------
328 const Property& PropertyHandler::impl_getPropertyFromName_throw( const ::rtl::OUString& _rPropertyName ) const
330 const_cast< PropertyHandler* >( this )->getSupportedProperties();
331 StlSyntaxSequence< Property >::const_iterator pFound = ::std::find_if( m_aSupportedProperties.begin(), m_aSupportedProperties.end(),
332 FindPropertyByName( _rPropertyName )
334 if ( pFound == m_aSupportedProperties.end() )
335 throw UnknownPropertyException();
337 return *pFound;
340 //--------------------------------------------------------------------
341 void PropertyHandler::implAddPropertyDescription( ::std::vector< Property >& _rProperties, const ::rtl::OUString& _rPropertyName, const Type& _rType, sal_Int16 _nAttribs ) const
343 _rProperties.push_back( Property(
344 _rPropertyName,
345 m_pInfoService->getPropertyId( _rPropertyName ),
346 _rType,
347 _nAttribs
348 ) );
351 //------------------------------------------------------------------------
352 Window* PropertyHandler::impl_getDefaultDialogParent_nothrow() const
354 return PropertyHandlerHelper::getDialogParentWindow( m_aContext );
357 //------------------------------------------------------------------------
358 PropertyId PropertyHandler::impl_getPropertyId_throw( const ::rtl::OUString& _rPropertyName ) const
360 PropertyId nPropId = m_pInfoService->getPropertyId( _rPropertyName );
361 if ( nPropId == -1 )
362 throw UnknownPropertyException();
363 return nPropId;
366 //------------------------------------------------------------------------
367 void PropertyHandler::impl_setContextDocumentModified_nothrow() const
369 Reference< XModifiable > xModifiable( impl_getContextDocument_nothrow(), UNO_QUERY );
370 if ( xModifiable.is() )
371 xModifiable->setModified( sal_True );
374 //------------------------------------------------------------------------
375 bool PropertyHandler::impl_componentHasProperty_throw( const ::rtl::OUString& _rPropName ) const
377 return m_xComponentPropertyInfo.is() && m_xComponentPropertyInfo->hasPropertyByName( _rPropName );
380 //--------------------------------------------------------------------
381 sal_Int16 PropertyHandler::impl_getDocumentMeasurementUnit_throw() const
383 FieldUnit eUnit = FUNIT_NONE;
385 Reference< XServiceInfo > xDocumentSI( impl_getContextDocument_nothrow(), UNO_QUERY );
386 OSL_ENSURE( xDocumentSI.is(), "PropertyHandlerHelper::impl_getDocumentMeasurementUnit_throw: No context document - where do I live?" );
387 if ( xDocumentSI.is() )
389 // determine the application type we live in
390 ::rtl::OUString sConfigurationLocation;
391 ::rtl::OUString sConfigurationProperty;
392 if ( xDocumentSI->supportsService( SERVICE_WEB_DOCUMENT ) )
393 { // writer
394 sConfigurationLocation = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "/org.openoffice.Office.WriterWeb/Layout/Other" ) );
395 sConfigurationProperty = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "MeasureUnit" ) );
397 else if ( xDocumentSI->supportsService( SERVICE_TEXT_DOCUMENT ) )
398 { // writer
399 sConfigurationLocation = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "/org.openoffice.Office.Writer/Layout/Other" ) );
400 sConfigurationProperty = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "MeasureUnit" ) );
402 else if ( xDocumentSI->supportsService( SERVICE_SPREADSHEET_DOCUMENT ) )
403 { // calc
404 sConfigurationLocation = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "/org.openoffice.Office.Calc/Layout/Other/MeasureUnit" ) );
405 sConfigurationProperty = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Metric" ) );
407 else if ( xDocumentSI->supportsService( SERVICE_DRAWING_DOCUMENT ) )
409 sConfigurationLocation = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "/org.openoffice.Office.Draw/Layout/Other/MeasureUnit" ) );
410 sConfigurationProperty = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Metric" ) );
412 else if ( xDocumentSI->supportsService( SERVICE_PRESENTATION_DOCUMENT ) )
414 sConfigurationLocation = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "/org.openoffice.Office.Impress/Layout/Other/MeasureUnit" ) );
415 sConfigurationProperty = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Metric" ) );
418 // read the measurement unit from the configuration
419 if ( sConfigurationLocation.getLength() && sConfigurationProperty.getLength() )
421 ::utl::OConfigurationTreeRoot aConfigTree( ::utl::OConfigurationTreeRoot::createWithServiceFactory(
422 m_aContext.getLegacyServiceFactory(), sConfigurationLocation, -1, ::utl::OConfigurationTreeRoot::CM_READONLY ) );
423 sal_Int32 nUnitAsInt = (sal_Int32)FUNIT_NONE;
424 aConfigTree.getNodeValue( sConfigurationProperty ) >>= nUnitAsInt;
426 // if this denotes a valid (and accepted) unit, then use it
427 if ( ( nUnitAsInt > FUNIT_NONE ) && ( nUnitAsInt <= FUNIT_100TH_MM ) )
428 eUnit = static_cast< FieldUnit >( nUnitAsInt );
432 if ( FUNIT_NONE == eUnit )
434 MeasurementSystem eSystem = SvtSysLocale().GetLocaleData().getMeasurementSystemEnum();
435 eUnit = MEASURE_METRIC == eSystem ? FUNIT_CM : FUNIT_INCH;
438 return VCLUnoHelper::ConvertToMeasurementUnit( eUnit, 1 );
441 //====================================================================
442 //= PropertyHandlerComponent
443 //====================================================================
444 //------------------------------------------------------------------------
445 PropertyHandlerComponent::PropertyHandlerComponent( const Reference< XComponentContext >& _rxContext )
446 :PropertyHandler( _rxContext )
450 //--------------------------------------------------------------------
451 IMPLEMENT_FORWARD_XINTERFACE2( PropertyHandlerComponent, PropertyHandler, PropertyHandlerComponent_Base )
452 IMPLEMENT_FORWARD_XTYPEPROVIDER2( PropertyHandlerComponent, PropertyHandler, PropertyHandlerComponent_Base )
454 //--------------------------------------------------------------------
455 ::sal_Bool SAL_CALL PropertyHandlerComponent::supportsService( const ::rtl::OUString& ServiceName ) throw (RuntimeException)
457 StlSyntaxSequence< ::rtl::OUString > aAllServices( getSupportedServiceNames() );
458 return ::std::find( aAllServices.begin(), aAllServices.end(), ServiceName ) != aAllServices.end();
461 //........................................................................
462 } // namespace pcr
463 //........................................................................