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 "usercontrol.hxx"
22 #include <com/sun/star/inspection/PropertyControlType.hpp>
23 #include <svl/numuno.hxx>
24 #include <rtl/math.hxx>
25 #include <vcl/GraphicObject.hxx>
26 #include <vcl/event.hxx>
27 #include <tools/debug.hxx>
28 #include <svl/zformat.hxx>
29 #include <connectivity/dbconversion.hxx>
30 #include <com/sun/star/util/Time.hpp>
31 #include "modulepcr.hxx"
32 #include <strings.hrc>
39 using ::com::sun::star::uno::Any
;
40 using ::com::sun::star::uno::Type
;
42 namespace PropertyControlType
= ::com::sun::star::inspection::PropertyControlType
;
45 // NumberFormatSampleField
48 bool NumberFormatSampleField::PreNotify( NotifyEvent
& rNEvt
)
50 // want to handle two keys myself : Del/Backspace should empty the window (setting my prop to "standard" this way)
51 if (MouseNotifyEvent::KEYINPUT
== rNEvt
.GetType())
53 sal_uInt16 nKey
= rNEvt
.GetKeyEvent()->GetKeyCode().GetCode();
55 if ((KEY_DELETE
== nKey
) || (KEY_BACKSPACE
== nKey
))
59 m_pHelper
->setModified();
64 return FormattedField::PreNotify( rNEvt
);
68 void NumberFormatSampleField::SetFormatSupplier( const SvNumberFormatsSupplierObj
* pSupplier
)
72 TreatAsNumber( true );
74 SvNumberFormatter
* pFormatter
= pSupplier
->GetNumberFormatter();
75 SetFormatter( pFormatter
);
76 SetValue( 1234.56789 );
80 TreatAsNumber( false );
81 SetFormatter( nullptr );
87 // OFormatSampleControl
90 OFormatSampleControl::OFormatSampleControl( vcl::Window
* pParent
)
91 :OFormatSampleControl_Base( PropertyControlType::Unknown
, pParent
, WB_READONLY
| WB_TABSTOP
| WB_BORDER
)
93 getTypedControlWindow()->setControlHelper(*this);
97 void SAL_CALL
OFormatSampleControl::setValue( const Any
& _rValue
)
99 sal_Int32 nFormatKey
= 0;
100 if ( _rValue
>>= nFormatKey
)
102 // else set the new format key, the text will be reformatted
103 getTypedControlWindow()->SetFormatKey( nFormatKey
);
105 SvNumberFormatter
* pNF
= getTypedControlWindow()->GetFormatter();
106 const SvNumberformat
* pEntry
= pNF
->GetEntry( nFormatKey
);
107 OSL_ENSURE( pEntry
, "OFormatSampleControl::setValue: invalid format entry!" );
109 const bool bIsTextFormat
= ( pEntry
&& pEntry
->IsTextFormat() );
111 getTypedControlWindow()->SetText( PcrRes( RID_STR_TEXT_FORMAT
) );
113 getTypedControlWindow()->SetValue( pEntry
? getPreviewValue( *pEntry
) : 1234.56789 );
116 getTypedControlWindow()->SetText( "" );
119 double OFormatSampleControl::getPreviewValue( const SvNumberformat
& i_rEntry
)
121 double nValue
= 1234.56789;
122 switch ( i_rEntry
.GetType() & ~SvNumFormatType::DEFINED
)
124 case SvNumFormatType::DATE
:
126 Date
aCurrentDate( Date::SYSTEM
);
127 static css::util::Date
STANDARD_DB_DATE(30,12,1899);
128 nValue
= ::dbtools::DBTypeConversion::toDouble(::dbtools::DBTypeConversion::toDate(aCurrentDate
.GetDate()),STANDARD_DB_DATE
);
131 case SvNumFormatType::TIME
:
132 case SvNumFormatType::DATETIME
:
134 tools::Time
aCurrentTime( tools::Time::SYSTEM
);
135 nValue
= ::dbtools::DBTypeConversion::toDouble(::dbtools::DBTypeConversion::toTime(aCurrentTime
.GetTime()));
145 double OFormatSampleControl::getPreviewValue(SvNumberFormatter
const * _pNF
, sal_Int32 _nFormatKey
)
147 const SvNumberformat
* pEntry
= _pNF
->GetEntry(_nFormatKey
);
148 DBG_ASSERT( pEntry
, "OFormattedNumericControl::SetFormatDescription: invalid format key!" );
149 double nValue
= 1234.56789;
151 nValue
= getPreviewValue( *pEntry
);
155 Any SAL_CALL
OFormatSampleControl::getValue()
158 if ( !getTypedControlWindow()->GetText().isEmpty() )
159 aPropValue
<<= static_cast<sal_Int32
>(getTypedControlWindow()->GetFormatKey());
164 Type SAL_CALL
OFormatSampleControl::getValueType()
166 return ::cppu::UnoType
<sal_Int32
>::get();
170 // class OFormattedNumericControl
173 OFormattedNumericControl::OFormattedNumericControl( vcl::Window
* pParent
, WinBits nWinStyle
)
174 :OFormattedNumericControl_Base( PropertyControlType::Unknown
, pParent
, nWinStyle
)
176 getTypedControlWindow()->TreatAsNumber(true);
180 OFormattedNumericControl::~OFormattedNumericControl()
185 void SAL_CALL
OFormattedNumericControl::setValue( const Any
& _rValue
)
188 if ( _rValue
>>= nValue
)
189 getTypedControlWindow()->SetValue( nValue
);
191 getTypedControlWindow()->SetText("");
195 Any SAL_CALL
OFormattedNumericControl::getValue()
198 if ( !getTypedControlWindow()->GetText().isEmpty() )
199 aPropValue
<<= getTypedControlWindow()->GetValue();
204 Type SAL_CALL
OFormattedNumericControl::getValueType()
206 return ::cppu::UnoType
<double>::get();
210 void OFormattedNumericControl::SetFormatDescription(const FormatDescription
& rDesc
)
212 bool bFallback
= true;
216 getTypedControlWindow()->TreatAsNumber(true);
218 SvNumberFormatter
* pFormatter
= rDesc
.pSupplier
->GetNumberFormatter();
219 if (pFormatter
!= getTypedControlWindow()->GetFormatter())
220 getTypedControlWindow()->SetFormatter(pFormatter
);
221 getTypedControlWindow()->SetFormatKey(rDesc
.nKey
);
223 const SvNumberformat
* pEntry
= getTypedControlWindow()->GetFormatter()->GetEntry(getTypedControlWindow()->GetFormatKey());
224 DBG_ASSERT( pEntry
, "OFormattedNumericControl::SetFormatDescription: invalid format key!" );
234 getTypedControlWindow()->TreatAsNumber(false);
235 getTypedControlWindow()->SetFormatter(nullptr);
236 getTypedControlWindow()->SetText("");
244 OFileUrlControl::OFileUrlControl( vcl::Window
* pParent
)
245 :OFileUrlControl_Base( PropertyControlType::Unknown
, pParent
, WB_TABSTOP
| WB_BORDER
| WB_DROPDOWN
)
247 getTypedControlWindow()->SetDropDownLineCount( 10 );
248 getTypedControlWindow()->SetPlaceHolder( PcrRes( RID_EMBED_IMAGE_PLACEHOLDER
) ) ;
252 OFileUrlControl::~OFileUrlControl()
257 void SAL_CALL
OFileUrlControl::setValue( const Any
& _rValue
)
260 if ( _rValue
>>= sURL
)
262 if (GraphicObject::isGraphicObjectUniqueIdURL(sURL
))
263 getTypedControlWindow()->DisplayURL( getTypedControlWindow()->GetPlaceHolder() );
265 getTypedControlWindow()->DisplayURL( sURL
);
268 getTypedControlWindow()->SetText( "" );
272 Any SAL_CALL
OFileUrlControl::getValue()
275 if ( !getTypedControlWindow()->GetText().isEmpty() )
276 aPropValue
<<= getTypedControlWindow()->GetURL();
281 Type SAL_CALL
OFileUrlControl::getValueType()
283 return ::cppu::UnoType
<OUString
>::get();
287 //= OTimeDurationControl
290 OTimeDurationControl::OTimeDurationControl( vcl::Window
* pParent
)
291 :ONumericControl( pParent
, WB_BORDER
| WB_TABSTOP
)
293 getTypedControlWindow()->SetUnit( FieldUnit::CUSTOM
);
294 getTypedControlWindow()->SetCustomUnitText(" ms");
295 getTypedControlWindow()->SetCustomConvertHdl( LINK( this, OTimeDurationControl
, OnCustomConvert
) );
299 OTimeDurationControl::~OTimeDurationControl()
304 ::sal_Int16 SAL_CALL
OTimeDurationControl::getControlType()
306 // don't use the base class'es method, it would claim we're a standard control, which
308 return PropertyControlType::Unknown
;
312 IMPL_LINK_NOARG( OTimeDurationControl
, OnCustomConvert
, MetricFormatter
&, void )
314 long nMultiplier
= 1;
315 if ( getTypedControlWindow()->GetCurUnitText().equalsIgnoreAsciiCase( "ms" ) )
317 if ( getTypedControlWindow()->GetCurUnitText().equalsIgnoreAsciiCase( "s" ) )
319 else if ( getTypedControlWindow()->GetCurUnitText().equalsIgnoreAsciiCase( "m" ) )
320 nMultiplier
= 1000 * 60;
321 else if ( getTypedControlWindow()->GetCurUnitText().equalsIgnoreAsciiCase( "h" ) )
322 nMultiplier
= 1000 * 60 * 60;
324 getTypedControlWindow()->SetValue( getTypedControlWindow()->GetLastValue() * nMultiplier
);
331 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */