bump product version to 6.3.0.0.beta1
[LibreOffice.git] / extensions / source / propctrlr / propertyhandler.cxx
blobd261ae48ecbec3ae192edc221fcfd86406c750ca
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 <memory>
21 #include "propertyhandler.hxx"
22 #include "formmetadata.hxx"
23 #include "formbrowsertools.hxx"
24 #include "handlerhelper.hxx"
25 #include "formstrings.hxx"
27 #include <com/sun/star/beans/PropertyAttribute.hpp>
28 #include <com/sun/star/lang/NullPointerException.hpp>
29 #include <com/sun/star/util/XModifiable.hpp>
30 #include <com/sun/star/script/Converter.hpp>
32 #include <cppuhelper/supportsservice.hxx>
33 #include <tools/debug.hxx>
34 #include <unotools/confignode.hxx>
35 #include <unotools/localedatawrapper.hxx>
36 #include <unotools/syslocale.hxx>
37 #include <toolkit/helper/vclunohelper.hxx>
39 #include <algorithm>
41 namespace pcr
44 using namespace ::com::sun::star::uno;
45 using namespace ::com::sun::star::awt;
46 using namespace ::com::sun::star::beans;
47 using namespace ::com::sun::star::script;
48 using namespace ::com::sun::star::lang;
49 using namespace ::com::sun::star::util;
50 using namespace ::com::sun::star::frame;
51 using namespace ::com::sun::star::inspection;
52 using namespace ::comphelper;
55 PropertyHandler::PropertyHandler( const Reference< XComponentContext >& _rxContext )
56 :PropertyHandler_Base( m_aMutex )
57 ,m_bSupportedPropertiesAreKnown( false )
58 ,m_aPropertyListeners( m_aMutex )
59 ,m_xContext( _rxContext )
60 ,m_pInfoService ( new OPropertyInfoService )
63 m_xTypeConverter = Converter::create(_rxContext);
66 PropertyHandler::~PropertyHandler()
70 void SAL_CALL PropertyHandler::inspect( const Reference< XInterface >& _rxIntrospectee )
72 if ( !_rxIntrospectee.is() )
73 throw NullPointerException();
75 ::osl::MutexGuard aGuard( m_aMutex );
77 Reference< XPropertySet > xNewComponent( _rxIntrospectee, UNO_QUERY );
78 if ( xNewComponent == m_xComponent )
79 return;
81 // remove all old property change listeners
82 std::unique_ptr< ::comphelper::OInterfaceIteratorHelper2 > removeListener = m_aPropertyListeners.createIterator();
83 std::unique_ptr< ::comphelper::OInterfaceIteratorHelper2 > readdListener = m_aPropertyListeners.createIterator(); // will copy the container as needed
84 while ( removeListener->hasMoreElements() )
85 removePropertyChangeListener( static_cast< XPropertyChangeListener* >( removeListener->next() ) );
86 OSL_ENSURE( m_aPropertyListeners.empty(), "PropertyHandler::inspect: derived classes are expected to forward the removePropertyChangeListener call to their base class (me)!" );
88 // remember the new component, and give derived classes the chance to react on it
89 m_xComponent = xNewComponent;
90 onNewComponent();
92 // add the listeners, again
93 while ( readdListener->hasMoreElements() )
94 addPropertyChangeListener( static_cast< XPropertyChangeListener* >( readdListener->next() ) );
97 void PropertyHandler::onNewComponent()
99 if ( m_xComponent.is() )
100 m_xComponentPropertyInfo = m_xComponent->getPropertySetInfo();
101 else
102 m_xComponentPropertyInfo.clear();
104 m_bSupportedPropertiesAreKnown = false;
105 m_aSupportedProperties.realloc( 0 );
108 Sequence< Property > SAL_CALL PropertyHandler::getSupportedProperties()
110 ::osl::MutexGuard aGuard( m_aMutex );
111 if ( !m_bSupportedPropertiesAreKnown )
113 m_aSupportedProperties = StlSyntaxSequence<css::beans::Property>(doDescribeSupportedProperties());
114 m_bSupportedPropertiesAreKnown = true;
116 return m_aSupportedProperties;
119 Sequence< OUString > SAL_CALL PropertyHandler::getSupersededProperties( )
121 return Sequence< OUString >();
124 Sequence< OUString > SAL_CALL PropertyHandler::getActuatingProperties( )
126 return Sequence< OUString >();
129 Any SAL_CALL PropertyHandler::convertToPropertyValue( const OUString& _rPropertyName, const Any& _rControlValue )
131 ::osl::MutexGuard aGuard( m_aMutex );
132 PropertyId nPropId = m_pInfoService->getPropertyId( _rPropertyName );
133 Property aProperty( impl_getPropertyFromName_throw( _rPropertyName ) );
135 Any aPropertyValue;
136 if ( !_rControlValue.hasValue() )
137 // NULL is converted to NULL
138 return aPropertyValue;
140 if ( ( m_pInfoService->getPropertyUIFlags( nPropId ) & PROP_FLAG_ENUM ) != 0 )
142 OUString sControlValue;
143 OSL_VERIFY( _rControlValue >>= sControlValue );
144 ::rtl::Reference< IPropertyEnumRepresentation > aEnumConversion(
145 new DefaultEnumRepresentation( *m_pInfoService, aProperty.Type, nPropId ) );
146 // TODO/UNOize: cache those converters?
147 aEnumConversion->getValueFromDescription( sControlValue, aPropertyValue );
149 else
150 aPropertyValue = PropertyHandlerHelper::convertToPropertyValue(
151 m_xContext, m_xTypeConverter, aProperty, _rControlValue );
152 return aPropertyValue;
155 Any SAL_CALL PropertyHandler::convertToControlValue( const OUString& _rPropertyName, const Any& _rPropertyValue, const Type& _rControlValueType )
157 ::osl::MutexGuard aGuard( m_aMutex );
158 PropertyId nPropId = m_pInfoService->getPropertyId( _rPropertyName );
160 if ( ( m_pInfoService->getPropertyUIFlags( nPropId ) & PROP_FLAG_ENUM ) != 0 )
162 DBG_ASSERT( _rControlValueType.getTypeClass() == TypeClass_STRING, "PropertyHandler::convertToControlValue: ENUM, but not STRING?" );
164 ::rtl::Reference< IPropertyEnumRepresentation > aEnumConversion(
165 new DefaultEnumRepresentation( *m_pInfoService, _rPropertyValue.getValueType(), nPropId ) );
166 // TODO/UNOize: cache those converters?
167 return makeAny( aEnumConversion->getDescriptionForValue( _rPropertyValue ) );
170 return PropertyHandlerHelper::convertToControlValue(
171 m_xContext, m_xTypeConverter, _rPropertyValue, _rControlValueType );
174 PropertyState SAL_CALL PropertyHandler::getPropertyState( const OUString& /*_rPropertyName*/ )
176 return PropertyState_DIRECT_VALUE;
179 LineDescriptor SAL_CALL PropertyHandler::describePropertyLine( const OUString& _rPropertyName,
180 const Reference< XPropertyControlFactory >& _rxControlFactory )
182 if ( !_rxControlFactory.is() )
183 throw NullPointerException();
185 ::osl::MutexGuard aGuard( m_aMutex );
186 PropertyId nPropId( impl_getPropertyId_throwUnknownProperty( _rPropertyName ) );
187 const Property& rProperty( impl_getPropertyFromId_throw( nPropId ) );
189 LineDescriptor aDescriptor;
190 if ( ( m_pInfoService->getPropertyUIFlags( nPropId ) & PROP_FLAG_ENUM ) != 0 )
192 aDescriptor.Control = PropertyHandlerHelper::createListBoxControl(
193 _rxControlFactory, m_pInfoService->getPropertyEnumRepresentations( nPropId ),
194 PropertyHandlerHelper::requiresReadOnlyControl( rProperty.Attributes ), false );
196 else
197 PropertyHandlerHelper::describePropertyLine( rProperty, aDescriptor, _rxControlFactory );
199 aDescriptor.HelpURL = HelpIdUrl::getHelpURL( m_pInfoService->getPropertyHelpId( nPropId ) );
200 aDescriptor.DisplayName = m_pInfoService->getPropertyTranslation( nPropId );
202 if ( ( m_pInfoService->getPropertyUIFlags( nPropId ) & PROP_FLAG_DATA_PROPERTY ) != 0 )
203 aDescriptor.Category = "Data";
204 else
205 aDescriptor.Category = "General";
206 return aDescriptor;
209 sal_Bool SAL_CALL PropertyHandler::isComposable( const OUString& _rPropertyName )
211 ::osl::MutexGuard aGuard( m_aMutex );
212 return m_pInfoService->isComposeable( _rPropertyName );
215 InteractiveSelectionResult SAL_CALL PropertyHandler::onInteractivePropertySelection( const OUString& /*_rPropertyName*/, sal_Bool /*_bPrimary*/, Any& /*_rData*/, const Reference< XObjectInspectorUI >& /*_rxInspectorUI*/ )
217 OSL_FAIL( "PropertyHandler::onInteractivePropertySelection: not implemented!" );
218 return InteractiveSelectionResult_Cancelled;
221 void SAL_CALL PropertyHandler::actuatingPropertyChanged( const OUString& /*_rActuatingPropertyName*/, const Any& /*_rNewValue*/, const Any& /*_rOldValue*/, const Reference< XObjectInspectorUI >& /*_rxInspectorUI*/, sal_Bool /*_bFirstTimeInit*/ )
223 OSL_FAIL( "PropertyHandler::actuatingPropertyChanged: not implemented!" );
226 void SAL_CALL PropertyHandler::addPropertyChangeListener( const Reference< XPropertyChangeListener >& _rxListener )
228 ::osl::MutexGuard aGuard( m_aMutex );
229 if ( !_rxListener.is() )
230 throw NullPointerException();
231 m_aPropertyListeners.addListener( _rxListener );
234 void SAL_CALL PropertyHandler::removePropertyChangeListener( const Reference< XPropertyChangeListener >& _rxListener )
236 ::osl::MutexGuard aGuard( m_aMutex );
237 m_aPropertyListeners.removeListener( _rxListener );
240 sal_Bool SAL_CALL PropertyHandler::suspend( sal_Bool /*_bSuspend*/ )
242 return true;
245 IMPLEMENT_FORWARD_XCOMPONENT( PropertyHandler, PropertyHandler_Base )
247 void SAL_CALL PropertyHandler::disposing()
249 m_xComponent.clear();
250 m_aPropertyListeners.clear();
251 m_xTypeConverter.clear();
252 m_aSupportedProperties.realloc( 0 );
255 void PropertyHandler::firePropertyChange( const OUString& _rPropName, PropertyId _nPropId, const Any& _rOldValue, const Any& _rNewValue )
257 PropertyChangeEvent aEvent;
258 aEvent.Source = m_xComponent;
259 aEvent.PropertyHandle = _nPropId;
260 aEvent.PropertyName = _rPropName;
261 aEvent.OldValue = _rOldValue;
262 aEvent.NewValue = _rNewValue;
263 m_aPropertyListeners.notify( aEvent, &XPropertyChangeListener::propertyChange );
266 const Property* PropertyHandler::impl_getPropertyFromId_nothrow( PropertyId _nPropId ) const
268 const_cast< PropertyHandler* >( this )->getSupportedProperties();
269 const Property* pFound = std::find_if( m_aSupportedProperties.begin(), m_aSupportedProperties.end(),
270 FindPropertyByHandle( _nPropId )
272 if ( pFound != m_aSupportedProperties.end() )
273 return pFound;
274 return nullptr;
277 const Property& PropertyHandler::impl_getPropertyFromId_throw( PropertyId _nPropId ) const
279 const Property* pProperty = impl_getPropertyFromId_nothrow( _nPropId );
280 if ( !pProperty )
281 throw UnknownPropertyException();
283 return *pProperty;
286 const Property& PropertyHandler::impl_getPropertyFromName_throw( const OUString& _rPropertyName ) const
288 const_cast< PropertyHandler* >( this )->getSupportedProperties();
289 StlSyntaxSequence< Property >::const_iterator pFound = std::find_if( m_aSupportedProperties.begin(), m_aSupportedProperties.end(),
290 FindPropertyByName( _rPropertyName )
292 if ( pFound == m_aSupportedProperties.end() )
293 throw UnknownPropertyException();
295 return *pFound;
298 void PropertyHandler::implAddPropertyDescription( std::vector< Property >& _rProperties, const OUString& _rPropertyName, const Type& _rType, sal_Int16 _nAttribs ) const
300 _rProperties.push_back( Property(
301 _rPropertyName,
302 m_pInfoService->getPropertyId( _rPropertyName ),
303 _rType,
304 _nAttribs
305 ) );
308 vcl::Window* PropertyHandler::impl_getDefaultDialogParent_nothrow() const
310 return PropertyHandlerHelper::getDialogParentWindow( m_xContext );
313 weld::Window* PropertyHandler::impl_getDefaultDialogFrame_nothrow() const
315 return PropertyHandlerHelper::getDialogParentFrame(m_xContext);
318 PropertyId PropertyHandler::impl_getPropertyId_throwUnknownProperty( const OUString& _rPropertyName ) const
320 PropertyId nPropId = m_pInfoService->getPropertyId( _rPropertyName );
321 if ( nPropId == -1 )
322 throw UnknownPropertyException();
323 return nPropId;
326 PropertyId PropertyHandler::impl_getPropertyId_throwRuntime( const OUString& _rPropertyName ) const
328 PropertyId nPropId = m_pInfoService->getPropertyId( _rPropertyName );
329 if ( nPropId == -1 )
330 throw RuntimeException();
331 return nPropId;
334 PropertyId PropertyHandler::impl_getPropertyId_nothrow( const OUString& _rPropertyName ) const
336 return m_pInfoService->getPropertyId( _rPropertyName );
339 void PropertyHandler::impl_setContextDocumentModified_nothrow() const
341 Reference< XModifiable > xModifiable( impl_getContextDocument_nothrow(), UNO_QUERY );
342 if ( xModifiable.is() )
343 xModifiable->setModified( true );
346 bool PropertyHandler::impl_componentHasProperty_throw( const OUString& _rPropName ) const
348 return m_xComponentPropertyInfo.is() && m_xComponentPropertyInfo->hasPropertyByName( _rPropName );
351 sal_Int16 PropertyHandler::impl_getDocumentMeasurementUnit_throw() const
353 FieldUnit eUnit = FieldUnit::NONE;
355 Reference< XServiceInfo > xDocumentSI( impl_getContextDocument_nothrow(), UNO_QUERY );
356 OSL_ENSURE( xDocumentSI.is(), "PropertyHandlerHelper::impl_getDocumentMeasurementUnit_throw: No context document - where do I live?" );
357 if ( xDocumentSI.is() )
359 // determine the application type we live in
360 OUString sConfigurationLocation;
361 OUString sConfigurationProperty;
362 if ( xDocumentSI->supportsService( SERVICE_WEB_DOCUMENT ) )
363 { // writer
364 sConfigurationLocation = "/org.openoffice.Office.WriterWeb/Layout/Other";
365 sConfigurationProperty = "MeasureUnit";
367 else if ( xDocumentSI->supportsService( SERVICE_TEXT_DOCUMENT ) )
368 { // writer
369 sConfigurationLocation = "/org.openoffice.Office.Writer/Layout/Other";
370 sConfigurationProperty = "MeasureUnit";
372 else if ( xDocumentSI->supportsService( SERVICE_SPREADSHEET_DOCUMENT ) )
373 { // calc
374 sConfigurationLocation = "/org.openoffice.Office.Calc/Layout/Other/MeasureUnit";
375 sConfigurationProperty = "Metric";
377 else if ( xDocumentSI->supportsService( SERVICE_DRAWING_DOCUMENT ) )
379 sConfigurationLocation = "/org.openoffice.Office.Draw/Layout/Other/MeasureUnit";
380 sConfigurationProperty = "Metric";
382 else if ( xDocumentSI->supportsService( SERVICE_PRESENTATION_DOCUMENT ) )
384 sConfigurationLocation = "/org.openoffice.Office.Impress/Layout/Other/MeasureUnit";
385 sConfigurationProperty = "Metric";
388 // read the measurement unit from the configuration
389 if ( !(sConfigurationLocation.isEmpty() || sConfigurationProperty.isEmpty()) )
391 ::utl::OConfigurationTreeRoot aConfigTree( ::utl::OConfigurationTreeRoot::createWithComponentContext(
392 m_xContext, sConfigurationLocation, -1, ::utl::OConfigurationTreeRoot::CM_READONLY ) );
393 sal_Int32 nUnitAsInt = sal_Int32(FieldUnit::NONE);
394 aConfigTree.getNodeValue( sConfigurationProperty ) >>= nUnitAsInt;
396 // if this denotes a valid (and accepted) unit, then use it
397 if ( ( nUnitAsInt > sal_Int32(FieldUnit::NONE) ) && ( nUnitAsInt <= sal_Int32(FieldUnit::MM_100TH) ) )
398 eUnit = static_cast< FieldUnit >( nUnitAsInt );
402 if ( FieldUnit::NONE == eUnit )
404 MeasurementSystem eSystem = SvtSysLocale().GetLocaleData().getMeasurementSystemEnum();
405 eUnit = MeasurementSystem::Metric == eSystem ? FieldUnit::CM : FieldUnit::INCH;
408 return VCLUnoHelper::ConvertToMeasurementUnit( eUnit, 1 );
411 PropertyHandlerComponent::PropertyHandlerComponent( const Reference< XComponentContext >& _rxContext )
412 :PropertyHandler( _rxContext )
416 IMPLEMENT_FORWARD_XINTERFACE2( PropertyHandlerComponent, PropertyHandler, PropertyHandlerComponent_Base )
417 IMPLEMENT_FORWARD_XTYPEPROVIDER2( PropertyHandlerComponent, PropertyHandler, PropertyHandlerComponent_Base )
419 sal_Bool SAL_CALL PropertyHandlerComponent::supportsService( const OUString& ServiceName )
421 return cppu::supportsService(this, ServiceName);
424 } // namespace pcr
426 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */