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"
22 #include "modulepcr.hxx"
24 #include <com/sun/star/container/XNameContainer.hpp>
25 #include <com/sun/star/inspection/StringRepresentation.hpp>
26 #include <com/sun/star/util/XModifiable.hpp>
27 #include <com/sun/star/awt/XWindow.hpp>
28 #include <com/sun/star/inspection/LineDescriptor.hpp>
29 #include <com/sun/star/inspection/PropertyControlType.hpp>
30 #include <com/sun/star/inspection/XStringListControl.hpp>
31 #include <com/sun/star/inspection/XNumericControl.hpp>
32 #include <comphelper/diagnose_ex.hxx>
33 #include <vcl/svapp.hxx>
34 #include <vcl/weld.hxx>
35 #include <vcl/weldutils.hxx>
44 using namespace ::com::sun::star::uno
;
45 using namespace ::com::sun::star::lang
;
46 using namespace ::com::sun::star::awt
;
47 using namespace ::com::sun::star::util
;
48 using namespace ::com::sun::star::beans
;
49 using namespace ::com::sun::star::script
;
50 using namespace ::com::sun::star::inspection
;
53 //= PropertyHandlerHelper
56 void PropertyHandlerHelper::describePropertyLine( const Property
& _rProperty
,
57 LineDescriptor
& /* [out] */ _out_rDescriptor
, const Reference
< XPropertyControlFactory
>& _rxControlFactory
)
59 // display the pure property name - no L10N
60 _out_rDescriptor
.DisplayName
= _rProperty
.Name
;
62 OSL_PRECOND( _rxControlFactory
.is(), "PropertyHandlerHelper::describePropertyLine: no factory -> no control!" );
63 if ( !_rxControlFactory
.is() )
66 bool bReadOnlyControl
= requiresReadOnlyControl( _rProperty
.Attributes
);
68 // special handling for booleans (this will become a list)
69 if ( _rProperty
.Type
.getTypeClass() == TypeClass_BOOLEAN
)
71 _out_rDescriptor
.Control
= createListBoxControl(_rxControlFactory
, RID_RSC_ENUM_YESNO
, SAL_N_ELEMENTS(RID_RSC_ENUM_YESNO
), bReadOnlyControl
);
75 sal_Int16 nControlType
= PropertyControlType::TextField
;
76 switch ( _rProperty
.Type
.getTypeClass() )
80 case TypeClass_UNSIGNED_SHORT
:
82 case TypeClass_UNSIGNED_LONG
:
84 case TypeClass_UNSIGNED_HYPER
:
86 case TypeClass_DOUBLE
:
87 nControlType
= PropertyControlType::NumericField
;
90 case TypeClass_SEQUENCE
:
91 nControlType
= PropertyControlType::StringListField
;
95 OSL_FAIL( "PropertyHandlerHelper::describePropertyLine: don't know how to represent this at the UI!" );
98 case TypeClass_STRING
:
99 nControlType
= PropertyControlType::TextField
;
104 _out_rDescriptor
.Control
= _rxControlFactory
->createPropertyControl( nControlType
, bReadOnlyControl
);
110 Reference
< XPropertyControl
> lcl_implCreateListLikeControl(
111 const Reference
< XPropertyControlFactory
>& _rxControlFactory
,
112 std::vector
< OUString
>&& _rInitialListEntries
,
113 bool _bReadOnlyControl
,
115 bool _bTrueIfListBoxFalseIfComboBox
118 Reference
< XStringListControl
> xListControl(
119 _rxControlFactory
->createPropertyControl(
120 _bTrueIfListBoxFalseIfComboBox
? PropertyControlType::ListBox
: PropertyControlType::ComboBox
, _bReadOnlyControl
126 std::sort( _rInitialListEntries
.begin(), _rInitialListEntries
.end() );
128 for (auto const& initialEntry
: _rInitialListEntries
)
129 xListControl
->appendListEntry(initialEntry
);
134 Reference
< XPropertyControl
> PropertyHandlerHelper::createListBoxControl( const Reference
< XPropertyControlFactory
>& _rxControlFactory
,
135 std::vector
< OUString
>&& _rInitialListEntries
, bool _bReadOnlyControl
, bool _bSorted
)
137 return lcl_implCreateListLikeControl(_rxControlFactory
, std::move(_rInitialListEntries
), _bReadOnlyControl
, _bSorted
, true);
140 Reference
< XPropertyControl
> PropertyHandlerHelper::createListBoxControl( const Reference
< XPropertyControlFactory
>& _rxControlFactory
,
141 const TranslateId
* pTransIds
, size_t nElements
, bool _bReadOnlyControl
)
143 std::vector
<OUString
> aInitialListEntries
;
144 for (size_t i
= 0; i
< nElements
; ++i
)
145 aInitialListEntries
.push_back(PcrRes(pTransIds
[i
]));
146 return lcl_implCreateListLikeControl(_rxControlFactory
, std::move(aInitialListEntries
), _bReadOnlyControl
, /*_bSorted*/false, true);
149 Reference
< XPropertyControl
> PropertyHandlerHelper::createComboBoxControl( const Reference
< XPropertyControlFactory
>& _rxControlFactory
,
150 std::vector
< OUString
>&& _rInitialListEntries
, bool _bSorted
)
152 return lcl_implCreateListLikeControl( _rxControlFactory
, std::move(_rInitialListEntries
), /*_bReadOnlyControl*/false, _bSorted
, false );
156 Reference
< XPropertyControl
> PropertyHandlerHelper::createNumericControl( const Reference
< XPropertyControlFactory
>& _rxControlFactory
,
157 sal_Int16 _nDigits
, const Optional
< double >& _rMinValue
, const Optional
< double >& _rMaxValue
)
159 Reference
< XNumericControl
> xNumericControl(
160 _rxControlFactory
->createPropertyControl( PropertyControlType::NumericField
, /*_bReadOnlyControl*/false ),
164 xNumericControl
->setDecimalDigits( _nDigits
);
165 xNumericControl
->setMinValue( _rMinValue
);
166 xNumericControl
->setMaxValue( _rMaxValue
);
168 return xNumericControl
;
172 Any
PropertyHandlerHelper::convertToPropertyValue( const Reference
< XComponentContext
>& _rxContext
,const Reference
< XTypeConverter
>& _rxTypeConverter
,
173 const Property
& _rProperty
, const Any
& _rControlValue
)
175 Any
aPropertyValue( _rControlValue
);
176 if ( !aPropertyValue
.hasValue() )
177 // NULL is converted to NULL
178 return aPropertyValue
;
180 if ( aPropertyValue
.getValueType().equals( _rProperty
.Type
) )
181 // nothing to do, type is already as desired
182 return aPropertyValue
;
184 if ( _rControlValue
.getValueTypeClass() == TypeClass_STRING
)
186 OUString sControlValue
;
187 _rControlValue
>>= sControlValue
;
189 Reference
< XStringRepresentation
> xConversionHelper
= StringRepresentation::create( _rxContext
,_rxTypeConverter
);
190 aPropertyValue
= xConversionHelper
->convertToPropertyValue( sControlValue
, _rProperty
.Type
);
196 if ( _rxTypeConverter
.is() )
197 aPropertyValue
= _rxTypeConverter
->convertTo( _rControlValue
, _rProperty
.Type
);
199 catch( const Exception
& )
201 TOOLS_WARN_EXCEPTION("extensions.propctrlr",
202 "caught an exception while converting via TypeConverter!");
206 return aPropertyValue
;
210 Any
PropertyHandlerHelper::convertToControlValue( const Reference
< XComponentContext
>& _rxContext
,const Reference
< XTypeConverter
>& _rxTypeConverter
,
211 const Any
& _rPropertyValue
, const Type
& _rControlValueType
)
213 Any
aControlValue( _rPropertyValue
);
214 if ( !aControlValue
.hasValue() )
215 // NULL is converted to NULL
216 return aControlValue
;
218 if ( _rControlValueType
.getTypeClass() == TypeClass_STRING
)
220 Reference
< XStringRepresentation
> xConversionHelper
= StringRepresentation::create( _rxContext
,_rxTypeConverter
);
221 aControlValue
<<= xConversionHelper
->convertToControlValue( _rPropertyValue
);
227 if ( _rxTypeConverter
.is() )
228 aControlValue
= _rxTypeConverter
->convertTo( _rPropertyValue
, _rControlValueType
);
230 catch( const Exception
& )
232 TOOLS_WARN_EXCEPTION("extensions.propctrlr",
233 "caught an exception while converting via TypeConverter!");
237 return aControlValue
;
241 void PropertyHandlerHelper::setContextDocumentModified( const Reference
<XComponentContext
> & rContext
)
245 Reference
< XModifiable
> xDocumentModifiable( getContextDocument_throw(rContext
), UNO_QUERY_THROW
);
246 xDocumentModifiable
->setModified( true );
248 catch( const Exception
& )
250 DBG_UNHANDLED_EXCEPTION("extensions.propctrlr");
254 Reference
< XInterface
> PropertyHandlerHelper::getContextDocument( const Reference
<XComponentContext
> & rContext
)
256 Reference
< XInterface
> xI
;
259 xI
= getContextDocument_throw( rContext
);
261 catch( const Exception
& )
263 TOOLS_WARN_EXCEPTION( "extensions.propctrlr", "PropertyHandler::getContextValueByName" );
268 Reference
< XInterface
> PropertyHandlerHelper::getContextDocument_throw( const Reference
<XComponentContext
> & rContext
)
270 Reference
< XInterface
> xI
;
271 Any aReturn
= rContext
->getValueByName( u
"ContextDocument"_ustr
);
276 weld::Window
* PropertyHandlerHelper::getDialogParentFrame(const Reference
<XComponentContext
>& rContext
)
278 weld::Window
* pInspectorWindow
= nullptr;
281 Reference
< XWindow
> xInspectorWindow(rContext
->getValueByName( u
"DialogParentWindow"_ustr
), UNO_QUERY_THROW
);
282 pInspectorWindow
= Application::GetFrameWeld(xInspectorWindow
);
284 catch( const Exception
& )
286 DBG_UNHANDLED_EXCEPTION("extensions.propctrlr");
288 return pInspectorWindow
;
291 std::unique_ptr
<weld::Builder
> PropertyHandlerHelper::makeBuilder(const OUString
& rUIFile
, const Reference
<XComponentContext
>& rContext
)
293 Reference
<XWindow
> xWindow(rContext
->getValueByName(u
"BuilderParent"_ustr
), UNO_QUERY_THROW
);
294 weld::TransportAsXWindow
& rTunnel
= dynamic_cast<weld::TransportAsXWindow
&>(*xWindow
);
295 return Application::CreateBuilder(rTunnel
.getWidget(), rUIFile
);
298 void PropertyHandlerHelper::setBuilderParent(const css::uno::Reference
<css::uno::XComponentContext
>& rContext
, weld::Widget
* pParent
)
300 Reference
<css::container::XNameContainer
> xName(rContext
, UNO_QUERY_THROW
);
301 Reference
<XWindow
> xWindow(new weld::TransportAsXWindow(pParent
));
302 xName
->insertByName(u
"BuilderParent"_ustr
, Any(xWindow
));
305 void PropertyHandlerHelper::clearBuilderParent(const css::uno::Reference
<css::uno::XComponentContext
>& rContext
)
307 Reference
<css::container::XNameContainer
> xName(rContext
, UNO_QUERY_THROW
);
308 xName
->removeByName(u
"BuilderParent"_ustr
);
314 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */