bump product version to 4.1.6.2
[LibreOffice.git] / extensions / source / propctrlr / handlerhelper.cxx
blob8561f818d6d6103f8523964779aa55638de8ad5e
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"
27 #include "pcrcomponentcontext.hxx"
29 #include "com/sun/star/inspection/StringRepresentation.hpp"
30 #include <com/sun/star/beans/PropertyAttribute.hpp>
31 #include <com/sun/star/uno/XComponentContext.hpp>
32 #include <com/sun/star/util/XModifiable.hpp>
33 #include <com/sun/star/awt/XWindow.hpp>
34 #include <com/sun/star/inspection/LineDescriptor.hpp>
35 #include <com/sun/star/inspection/PropertyControlType.hpp>
36 #include <com/sun/star/inspection/XStringListControl.hpp>
37 #include <com/sun/star/inspection/XNumericControl.hpp>
38 #include <tools/debug.hxx>
39 #include <tools/diagnose_ex.h>
40 #include <tools/StringListResource.hxx>
41 #include <toolkit/helper/vclunohelper.hxx>
43 #include <algorithm>
45 //........................................................................
46 namespace pcr
48 //........................................................................
50 using namespace ::com::sun::star::uno;
51 using namespace ::com::sun::star::lang;
52 using namespace ::com::sun::star::awt;
53 using namespace ::com::sun::star::util;
54 using namespace ::com::sun::star::beans;
55 using namespace ::com::sun::star::script;
56 using namespace ::com::sun::star::inspection;
58 //====================================================================
59 //= PropertyHandlerHelper
60 //====================================================================
61 //--------------------------------------------------------------------
62 void PropertyHandlerHelper::describePropertyLine( const Property& _rProperty,
63 LineDescriptor& /* [out] */ _out_rDescriptor, const Reference< XPropertyControlFactory >& _rxControlFactory )
65 // display the pure property name - no L10N
66 _out_rDescriptor.DisplayName = _rProperty.Name;
68 OSL_PRECOND( _rxControlFactory.is(), "PropertyHandlerHelper::describePropertyLine: no factory -> no control!" );
69 if ( !_rxControlFactory.is() )
70 return;
72 sal_Bool bReadOnlyControl = requiresReadOnlyControl( _rProperty.Attributes );
74 // special handling for booleans (this will become a list)
75 if ( _rProperty.Type.getTypeClass() == TypeClass_BOOLEAN )
77 ::std::vector< OUString > aListEntries;
78 tools::StringListResource aRes(PcrRes(RID_RSC_ENUM_YESNO),aListEntries);
79 _out_rDescriptor.Control = createListBoxControl( _rxControlFactory, aListEntries, bReadOnlyControl, sal_False );
80 return;
83 sal_Int16 nControlType = PropertyControlType::TextField;
84 switch ( _rProperty.Type.getTypeClass() )
86 case TypeClass_BYTE:
87 case TypeClass_SHORT:
88 case TypeClass_UNSIGNED_SHORT:
89 case TypeClass_LONG:
90 case TypeClass_UNSIGNED_LONG:
91 case TypeClass_HYPER:
92 case TypeClass_UNSIGNED_HYPER:
93 case TypeClass_FLOAT:
94 case TypeClass_DOUBLE:
95 nControlType = PropertyControlType::NumericField;
96 break;
98 case TypeClass_SEQUENCE:
99 nControlType = PropertyControlType::StringListField;
100 break;
102 default:
103 OSL_FAIL( "PropertyHandlerHelper::describePropertyLine: don't know how to represent this at the UI!" );
104 // NO break!
106 case TypeClass_STRING:
107 nControlType = PropertyControlType::TextField;
108 break;
111 // create a control
112 _out_rDescriptor.Control = _rxControlFactory->createPropertyControl( nControlType, bReadOnlyControl );
115 //--------------------------------------------------------------------
116 namespace
118 Reference< XPropertyControl > lcl_implCreateListLikeControl(
119 const Reference< XPropertyControlFactory >& _rxControlFactory,
120 const ::std::vector< OUString >& _rInitialListEntries,
121 sal_Bool _bReadOnlyControl,
122 sal_Bool _bSorted,
123 sal_Bool _bTrueIfListBoxFalseIfComboBox
126 Reference< XStringListControl > xListControl(
127 _rxControlFactory->createPropertyControl(
128 _bTrueIfListBoxFalseIfComboBox ? PropertyControlType::ListBox : PropertyControlType::ComboBox, _bReadOnlyControl
130 UNO_QUERY_THROW
133 ::std::vector< OUString > aInitialEntries( _rInitialListEntries );
134 if ( _bSorted )
135 ::std::sort( aInitialEntries.begin(), aInitialEntries.end() );
137 for ( ::std::vector< OUString >::const_iterator loop = aInitialEntries.begin();
138 loop != aInitialEntries.end();
139 ++loop
141 xListControl->appendListEntry( *loop );
142 return xListControl.get();
146 //--------------------------------------------------------------------
147 Reference< XPropertyControl > PropertyHandlerHelper::createListBoxControl( const Reference< XPropertyControlFactory >& _rxControlFactory,
148 const ::std::vector< OUString >& _rInitialListEntries, sal_Bool _bReadOnlyControl, sal_Bool _bSorted )
150 return lcl_implCreateListLikeControl( _rxControlFactory, _rInitialListEntries, _bReadOnlyControl, _bSorted, sal_True );
153 //--------------------------------------------------------------------
154 Reference< XPropertyControl > PropertyHandlerHelper::createComboBoxControl( const Reference< XPropertyControlFactory >& _rxControlFactory,
155 const ::std::vector< OUString >& _rInitialListEntries, sal_Bool _bReadOnlyControl, sal_Bool _bSorted )
157 return lcl_implCreateListLikeControl( _rxControlFactory, _rInitialListEntries, _bReadOnlyControl, _bSorted, sal_False );
160 //--------------------------------------------------------------------
161 Reference< XPropertyControl > PropertyHandlerHelper::createNumericControl( const Reference< XPropertyControlFactory >& _rxControlFactory,
162 sal_Int16 _nDigits, const Optional< double >& _rMinValue, const Optional< double >& _rMaxValue, sal_Bool _bReadOnlyControl )
164 Reference< XNumericControl > xNumericControl(
165 _rxControlFactory->createPropertyControl( PropertyControlType::NumericField, _bReadOnlyControl ),
166 UNO_QUERY_THROW
169 xNumericControl->setDecimalDigits( _nDigits );
170 xNumericControl->setMinValue( _rMinValue );
171 xNumericControl->setMaxValue( _rMaxValue );
173 return xNumericControl.get();
176 //--------------------------------------------------------------------
177 Any PropertyHandlerHelper::convertToPropertyValue( const Reference< XComponentContext >& _rxContext,const Reference< XTypeConverter >& _rxTypeConverter,
178 const Property& _rProperty, const Any& _rControlValue )
180 Any aPropertyValue( _rControlValue );
181 if ( !aPropertyValue.hasValue() )
182 // NULL is converted to NULL
183 return aPropertyValue;
185 if ( aPropertyValue.getValueType().equals( _rProperty.Type ) )
186 // nothing to do, type is already as desired
187 return aPropertyValue;
189 if ( _rControlValue.getValueType().getTypeClass() == TypeClass_STRING )
191 OUString sControlValue;
192 _rControlValue >>= sControlValue;
194 Reference< XStringRepresentation > xConversionHelper = StringRepresentation::create( _rxContext,_rxTypeConverter );
195 aPropertyValue = xConversionHelper->convertToPropertyValue( sControlValue, _rProperty.Type );
197 else
201 if ( _rxTypeConverter.is() )
202 aPropertyValue = _rxTypeConverter->convertTo( _rControlValue, _rProperty.Type );
204 catch( const Exception& )
206 OSL_FAIL( "PropertyHandlerHelper::convertToPropertyValue: caught an exception while converting via TypeConverter!" );
210 return aPropertyValue;
213 //--------------------------------------------------------------------
214 Any PropertyHandlerHelper::convertToControlValue( const Reference< XComponentContext >& _rxContext,const Reference< XTypeConverter >& _rxTypeConverter,
215 const Any& _rPropertyValue, const Type& _rControlValueType )
217 Any aControlValue( _rPropertyValue );
218 if ( !aControlValue.hasValue() )
219 // NULL is converted to NULL
220 return aControlValue;
222 if ( _rControlValueType.getTypeClass() == TypeClass_STRING )
224 Reference< XStringRepresentation > xConversionHelper = StringRepresentation::create( _rxContext,_rxTypeConverter );
225 aControlValue <<= xConversionHelper->convertToControlValue( _rPropertyValue );
227 else
231 if ( _rxTypeConverter.is() )
232 aControlValue = _rxTypeConverter->convertTo( _rPropertyValue, _rControlValueType );
234 catch( const Exception& )
236 OSL_FAIL( "PropertyHandlerHelper::convertToControlValue: caught an exception while converting via TypeConverter!" );
240 return aControlValue;
243 //--------------------------------------------------------------------
244 void PropertyHandlerHelper::setContextDocumentModified( const ComponentContext& _rContext )
248 Reference< XModifiable > xDocumentModifiable( _rContext.getContextValueByAsciiName( "ContextDocument" ), UNO_QUERY_THROW );
249 xDocumentModifiable->setModified( sal_True );
251 catch( const Exception& )
253 DBG_UNHANDLED_EXCEPTION();
257 //--------------------------------------------------------------------
258 Window* PropertyHandlerHelper::getDialogParentWindow( const ComponentContext& _rContext )
260 Window* pInspectorWindow = NULL;
263 Reference< XWindow > xInspectorWindow( _rContext.getContextValueByAsciiName( "DialogParentWindow" ), UNO_QUERY_THROW );
264 pInspectorWindow = VCLUnoHelper::GetWindow( xInspectorWindow );
266 catch( const Exception& )
268 DBG_UNHANDLED_EXCEPTION();
270 return pInspectorWindow;
273 //........................................................................
274 } // namespace pcr
275 //........................................................................
277 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */