1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <strings.hrc>
23 #include "modulepcr.hxx"
24 #include "enumrepresentation.hxx"
25 #include "formmetadata.hxx"
27 #include <com/sun/star/inspection/StringRepresentation.hpp>
28 #include <com/sun/star/beans/PropertyAttribute.hpp>
29 #include <com/sun/star/uno/XComponentContext.hpp>
30 #include <com/sun/star/util/XModifiable.hpp>
31 #include <com/sun/star/awt/XWindow.hpp>
32 #include <com/sun/star/inspection/LineDescriptor.hpp>
33 #include <com/sun/star/inspection/PropertyControlType.hpp>
34 #include <com/sun/star/inspection/XStringListControl.hpp>
35 #include <com/sun/star/inspection/XNumericControl.hpp>
36 #include <tools/diagnose_ex.h>
37 #include <toolkit/helper/vclunohelper.hxx>
38 #include <vcl/svapp.hxx>
39 #include <vcl/window.hxx>
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() )
70 bool bReadOnlyControl
= requiresReadOnlyControl( _rProperty
.Attributes
);
72 // special handling for booleans (this will become a list)
73 if ( _rProperty
.Type
.getTypeClass() == TypeClass_BOOLEAN
)
75 _out_rDescriptor
.Control
= createListBoxControl(_rxControlFactory
, RID_RSC_ENUM_YESNO
, SAL_N_ELEMENTS(RID_RSC_ENUM_YESNO
), bReadOnlyControl
);
79 sal_Int16 nControlType
= PropertyControlType::TextField
;
80 switch ( _rProperty
.Type
.getTypeClass() )
84 case TypeClass_UNSIGNED_SHORT
:
86 case TypeClass_UNSIGNED_LONG
:
88 case TypeClass_UNSIGNED_HYPER
:
90 case TypeClass_DOUBLE
:
91 nControlType
= PropertyControlType::NumericField
;
94 case TypeClass_SEQUENCE
:
95 nControlType
= PropertyControlType::StringListField
;
99 OSL_FAIL( "PropertyHandlerHelper::describePropertyLine: don't know how to represent this at the UI!" );
102 case TypeClass_STRING
:
103 nControlType
= PropertyControlType::TextField
;
108 _out_rDescriptor
.Control
= _rxControlFactory
->createPropertyControl( nControlType
, bReadOnlyControl
);
114 Reference
< XPropertyControl
> lcl_implCreateListLikeControl(
115 const Reference
< XPropertyControlFactory
>& _rxControlFactory
,
116 const std::vector
< OUString
>& _rInitialListEntries
,
117 bool _bReadOnlyControl
,
119 bool _bTrueIfListBoxFalseIfComboBox
122 Reference
< XStringListControl
> xListControl(
123 _rxControlFactory
->createPropertyControl(
124 _bTrueIfListBoxFalseIfComboBox
? PropertyControlType::ListBox
: PropertyControlType::ComboBox
, _bReadOnlyControl
129 std::vector
< OUString
> aInitialEntries( _rInitialListEntries
);
131 std::sort( aInitialEntries
.begin(), aInitialEntries
.end() );
133 for (auto const& initialEntry
: aInitialEntries
)
134 xListControl
->appendListEntry(initialEntry
);
135 return xListControl
.get();
139 Reference
< XPropertyControl
> PropertyHandlerHelper::createListBoxControl( const Reference
< XPropertyControlFactory
>& _rxControlFactory
,
140 const std::vector
< OUString
>& _rInitialListEntries
, bool _bReadOnlyControl
, bool _bSorted
)
142 return lcl_implCreateListLikeControl(_rxControlFactory
, _rInitialListEntries
, _bReadOnlyControl
, _bSorted
, true);
145 Reference
< XPropertyControl
> PropertyHandlerHelper::createListBoxControl( const Reference
< XPropertyControlFactory
>& _rxControlFactory
,
146 const char** pTransIds
, size_t nElements
, bool _bReadOnlyControl
)
148 std::vector
<OUString
> aInitialListEntries
;
149 for (size_t i
= 0; i
< nElements
; ++i
)
150 aInitialListEntries
.push_back(PcrRes(pTransIds
[i
]));
151 return lcl_implCreateListLikeControl(_rxControlFactory
, aInitialListEntries
, _bReadOnlyControl
, /*_bSorted*/false, true);
154 Reference
< XPropertyControl
> PropertyHandlerHelper::createComboBoxControl( const Reference
< XPropertyControlFactory
>& _rxControlFactory
,
155 const std::vector
< OUString
>& _rInitialListEntries
, bool _bSorted
)
157 return lcl_implCreateListLikeControl( _rxControlFactory
, _rInitialListEntries
, /*_bReadOnlyControl*/false, _bSorted
, false );
161 Reference
< XPropertyControl
> PropertyHandlerHelper::createNumericControl( const Reference
< XPropertyControlFactory
>& _rxControlFactory
,
162 sal_Int16 _nDigits
, const Optional
< double >& _rMinValue
, const Optional
< double >& _rMaxValue
)
164 Reference
< XNumericControl
> xNumericControl(
165 _rxControlFactory
->createPropertyControl( PropertyControlType::NumericField
, /*_bReadOnlyControl*/false ),
169 xNumericControl
->setDecimalDigits( _nDigits
);
170 xNumericControl
->setMinValue( _rMinValue
);
171 xNumericControl
->setMaxValue( _rMaxValue
);
173 return xNumericControl
.get();
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
);
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
;
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
);
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
;
244 void PropertyHandlerHelper::setContextDocumentModified( const Reference
<XComponentContext
> & _rContext
)
248 Reference
< XModifiable
> xDocumentModifiable( getContextDocument_throw(_rContext
), UNO_QUERY_THROW
);
249 xDocumentModifiable
->setModified( true );
251 catch( const Exception
& )
253 DBG_UNHANDLED_EXCEPTION("extensions.propctrlr");
257 Reference
< XInterface
> PropertyHandlerHelper::getContextDocument( const Reference
<XComponentContext
> & _rContext
)
259 Reference
< XInterface
> xI
;
262 xI
= getContextDocument_throw( _rContext
);
264 catch( const Exception
& )
266 OSL_FAIL( "PropertyHandler::getContextValueByName: caught an exception!" );
271 Reference
< XInterface
> PropertyHandlerHelper::getContextDocument_throw( const Reference
<XComponentContext
> & _rContext
)
273 Reference
< XInterface
> xI
;
274 Any aReturn
= _rContext
->getValueByName( "ContextDocument" );
280 vcl::Window
* PropertyHandlerHelper::getDialogParentWindow( const Reference
<XComponentContext
>& _rContext
)
282 vcl::Window
* pInspectorWindow
= nullptr;
285 Reference
< XWindow
> xInspectorWindow( _rContext
->getValueByName( "DialogParentWindow" ), UNO_QUERY_THROW
);
286 pInspectorWindow
= VCLUnoHelper::GetWindow( xInspectorWindow
).get();
288 catch( const Exception
& )
290 DBG_UNHANDLED_EXCEPTION("extensions.propctrlr");
292 return pInspectorWindow
;
295 weld::Window
* PropertyHandlerHelper::getDialogParentFrame(const Reference
<XComponentContext
>& _rContext
)
297 weld::Window
* pInspectorWindow
= nullptr;
300 Reference
< XWindow
> xInspectorWindow( _rContext
->getValueByName( "DialogParentWindow" ), UNO_QUERY_THROW
);
301 pInspectorWindow
= Application::GetFrameWeld(xInspectorWindow
);
303 catch( const Exception
& )
305 DBG_UNHANDLED_EXCEPTION("extensions.propctrlr");
307 return pInspectorWindow
;
312 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */