bump product version to 5.0.4.1
[LibreOffice.git] / extensions / source / propctrlr / handlerhelper.cxx
blob54fa2ef2e0d8aa55fa896cd9b5fac85dd353300a
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 "handlerhelper.hxx"
21 #include "propresid.hrc"
22 #include "formresid.hrc"
23 #include <comphelper/extract.hxx>
24 #include "modulepcr.hxx"
25 #include "enumrepresentation.hxx"
26 #include "formmetadata.hxx"
28 #include "com/sun/star/inspection/StringRepresentation.hpp"
29 #include <com/sun/star/beans/PropertyAttribute.hpp>
30 #include <com/sun/star/uno/XComponentContext.hpp>
31 #include <com/sun/star/util/XModifiable.hpp>
32 #include <com/sun/star/awt/XWindow.hpp>
33 #include <com/sun/star/inspection/LineDescriptor.hpp>
34 #include <com/sun/star/inspection/PropertyControlType.hpp>
35 #include <com/sun/star/inspection/XStringListControl.hpp>
36 #include <com/sun/star/inspection/XNumericControl.hpp>
37 #include <tools/diagnose_ex.h>
38 #include <tools/StringListResource.hxx>
39 #include <toolkit/helper/vclunohelper.hxx>
41 #include <algorithm>
44 namespace pcr
48 using namespace ::com::sun::star::uno;
49 using namespace ::com::sun::star::lang;
50 using namespace ::com::sun::star::awt;
51 using namespace ::com::sun::star::util;
52 using namespace ::com::sun::star::beans;
53 using namespace ::com::sun::star::script;
54 using namespace ::com::sun::star::inspection;
57 //= PropertyHandlerHelper
60 void PropertyHandlerHelper::describePropertyLine( const Property& _rProperty,
61 LineDescriptor& /* [out] */ _out_rDescriptor, const Reference< XPropertyControlFactory >& _rxControlFactory )
63 // display the pure property name - no L10N
64 _out_rDescriptor.DisplayName = _rProperty.Name;
66 OSL_PRECOND( _rxControlFactory.is(), "PropertyHandlerHelper::describePropertyLine: no factory -> no control!" );
67 if ( !_rxControlFactory.is() )
68 return;
70 bool bReadOnlyControl = requiresReadOnlyControl( _rProperty.Attributes );
72 // special handling for booleans (this will become a list)
73 if ( _rProperty.Type.getTypeClass() == TypeClass_BOOLEAN )
75 ::std::vector< OUString > aListEntries;
76 tools::StringListResource aRes(PcrRes(RID_RSC_ENUM_YESNO),aListEntries);
77 _out_rDescriptor.Control = createListBoxControl( _rxControlFactory, aListEntries, bReadOnlyControl, false );
78 return;
81 sal_Int16 nControlType = PropertyControlType::TextField;
82 switch ( _rProperty.Type.getTypeClass() )
84 case TypeClass_BYTE:
85 case TypeClass_SHORT:
86 case TypeClass_UNSIGNED_SHORT:
87 case TypeClass_LONG:
88 case TypeClass_UNSIGNED_LONG:
89 case TypeClass_HYPER:
90 case TypeClass_UNSIGNED_HYPER:
91 case TypeClass_FLOAT:
92 case TypeClass_DOUBLE:
93 nControlType = PropertyControlType::NumericField;
94 break;
96 case TypeClass_SEQUENCE:
97 nControlType = PropertyControlType::StringListField;
98 break;
100 default:
101 OSL_FAIL( "PropertyHandlerHelper::describePropertyLine: don't know how to represent this at the UI!" );
102 // NO break!
104 case TypeClass_STRING:
105 nControlType = PropertyControlType::TextField;
106 break;
109 // create a control
110 _out_rDescriptor.Control = _rxControlFactory->createPropertyControl( nControlType, bReadOnlyControl );
114 namespace
116 Reference< XPropertyControl > lcl_implCreateListLikeControl(
117 const Reference< XPropertyControlFactory >& _rxControlFactory,
118 const ::std::vector< OUString >& _rInitialListEntries,
119 bool _bReadOnlyControl,
120 bool _bSorted,
121 bool _bTrueIfListBoxFalseIfComboBox
124 Reference< XStringListControl > xListControl(
125 _rxControlFactory->createPropertyControl(
126 _bTrueIfListBoxFalseIfComboBox ? PropertyControlType::ListBox : PropertyControlType::ComboBox, _bReadOnlyControl
128 UNO_QUERY_THROW
131 ::std::vector< OUString > aInitialEntries( _rInitialListEntries );
132 if ( _bSorted )
133 ::std::sort( aInitialEntries.begin(), aInitialEntries.end() );
135 for ( ::std::vector< OUString >::const_iterator loop = aInitialEntries.begin();
136 loop != aInitialEntries.end();
137 ++loop
139 xListControl->appendListEntry( *loop );
140 return xListControl.get();
145 Reference< XPropertyControl > PropertyHandlerHelper::createListBoxControl( const Reference< XPropertyControlFactory >& _rxControlFactory,
146 const ::std::vector< OUString >& _rInitialListEntries, bool _bReadOnlyControl, bool _bSorted )
148 return lcl_implCreateListLikeControl( _rxControlFactory, _rInitialListEntries, _bReadOnlyControl, _bSorted, true );
152 Reference< XPropertyControl > PropertyHandlerHelper::createComboBoxControl( const Reference< XPropertyControlFactory >& _rxControlFactory,
153 const ::std::vector< OUString >& _rInitialListEntries, bool _bReadOnlyControl, bool _bSorted )
155 return lcl_implCreateListLikeControl( _rxControlFactory, _rInitialListEntries, _bReadOnlyControl, _bSorted, false );
159 Reference< XPropertyControl > PropertyHandlerHelper::createNumericControl( const Reference< XPropertyControlFactory >& _rxControlFactory,
160 sal_Int16 _nDigits, const Optional< double >& _rMinValue, const Optional< double >& _rMaxValue, bool _bReadOnlyControl )
162 Reference< XNumericControl > xNumericControl(
163 _rxControlFactory->createPropertyControl( PropertyControlType::NumericField, _bReadOnlyControl ),
164 UNO_QUERY_THROW
167 xNumericControl->setDecimalDigits( _nDigits );
168 xNumericControl->setMinValue( _rMinValue );
169 xNumericControl->setMaxValue( _rMaxValue );
171 return xNumericControl.get();
175 Any PropertyHandlerHelper::convertToPropertyValue( const Reference< XComponentContext >& _rxContext,const Reference< XTypeConverter >& _rxTypeConverter,
176 const Property& _rProperty, const Any& _rControlValue )
178 Any aPropertyValue( _rControlValue );
179 if ( !aPropertyValue.hasValue() )
180 // NULL is converted to NULL
181 return aPropertyValue;
183 if ( aPropertyValue.getValueType().equals( _rProperty.Type ) )
184 // nothing to do, type is already as desired
185 return aPropertyValue;
187 if ( _rControlValue.getValueType().getTypeClass() == TypeClass_STRING )
189 OUString sControlValue;
190 _rControlValue >>= sControlValue;
192 Reference< XStringRepresentation > xConversionHelper = StringRepresentation::create( _rxContext,_rxTypeConverter );
193 aPropertyValue = xConversionHelper->convertToPropertyValue( sControlValue, _rProperty.Type );
195 else
199 if ( _rxTypeConverter.is() )
200 aPropertyValue = _rxTypeConverter->convertTo( _rControlValue, _rProperty.Type );
202 catch( const Exception& )
204 OSL_FAIL( "PropertyHandlerHelper::convertToPropertyValue: caught an exception while converting via TypeConverter!" );
208 return aPropertyValue;
212 Any PropertyHandlerHelper::convertToControlValue( const Reference< XComponentContext >& _rxContext,const Reference< XTypeConverter >& _rxTypeConverter,
213 const Any& _rPropertyValue, const Type& _rControlValueType )
215 Any aControlValue( _rPropertyValue );
216 if ( !aControlValue.hasValue() )
217 // NULL is converted to NULL
218 return aControlValue;
220 if ( _rControlValueType.getTypeClass() == TypeClass_STRING )
222 Reference< XStringRepresentation > xConversionHelper = StringRepresentation::create( _rxContext,_rxTypeConverter );
223 aControlValue <<= xConversionHelper->convertToControlValue( _rPropertyValue );
225 else
229 if ( _rxTypeConverter.is() )
230 aControlValue = _rxTypeConverter->convertTo( _rPropertyValue, _rControlValueType );
232 catch( const Exception& )
234 OSL_FAIL( "PropertyHandlerHelper::convertToControlValue: caught an exception while converting via TypeConverter!" );
238 return aControlValue;
242 void PropertyHandlerHelper::setContextDocumentModified( const Reference<XComponentContext> & _rContext )
246 Reference< XModifiable > xDocumentModifiable( getContextDocument_throw(_rContext), UNO_QUERY_THROW );
247 xDocumentModifiable->setModified( sal_True );
249 catch( const Exception& )
251 DBG_UNHANDLED_EXCEPTION();
255 Reference< XInterface > PropertyHandlerHelper::getContextDocument( const Reference<XComponentContext> & _rContext )
257 Reference< XInterface > xI;
260 xI = getContextDocument_throw( _rContext );
262 catch( const Exception& )
264 OSL_FAIL( "PropertyHandler::getContextValueByName: caught an exception!" );
266 return xI;
269 Reference< XInterface > PropertyHandlerHelper::getContextDocument_throw( const Reference<XComponentContext> & _rContext ) throw (RuntimeException)
271 Reference< XInterface > xI;
272 Any aReturn = _rContext->getValueByName( "ContextDocument" );
273 aReturn >>= xI;
274 return xI;
278 vcl::Window* PropertyHandlerHelper::getDialogParentWindow( const Reference<XComponentContext>& _rContext )
280 vcl::Window* pInspectorWindow = NULL;
283 Reference< XWindow > xInspectorWindow( _rContext->getValueByName( "DialogParentWindow" ), UNO_QUERY_THROW );
284 pInspectorWindow = VCLUnoHelper::GetWindow( xInspectorWindow );
286 catch( const Exception& )
288 DBG_UNHANDLED_EXCEPTION();
290 return pInspectorWindow;
294 } // namespace pcr
297 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */