1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_extensions.hxx"
30 #include "usercontrol.hxx"
32 /** === begin UNO includes === **/
33 #include <com/sun/star/inspection/PropertyControlType.hpp>
34 #include <com/sun/star/inspection/PropertyControlType.hpp>
35 /** === end UNO includes === **/
36 #include <svl/numuno.hxx>
37 #include <rtl/math.hxx>
38 #include <tools/debug.hxx>
39 #include <svl/zformat.hxx>
40 #include <connectivity/dbconversion.hxx>
41 #include <com/sun/star/util/Time.hpp>
42 #include "modulepcr.hxx"
43 #include "propresid.hrc"
45 //............................................................................
48 //............................................................................
50 /** === begin UNO using === **/
51 using ::com::sun::star::uno::Any
;
52 using ::com::sun::star::uno::Type
;
53 using ::com::sun::star::beans::IllegalTypeException
;
54 using ::com::sun::star::uno::RuntimeException
;
55 /** === end UNO using === **/
56 namespace PropertyControlType
= ::com::sun::star::inspection::PropertyControlType
;
58 //==================================================================
59 // NumberFormatSampleField
60 //==================================================================
61 //------------------------------------------------------------------
62 long NumberFormatSampleField::PreNotify( NotifyEvent
& rNEvt
)
64 // want to handle two keys myself : Del/Backspace should empty the window (setting my prop to "standard" this way)
65 if (EVENT_KEYINPUT
== rNEvt
.GetType())
67 sal_uInt16 nKey
= rNEvt
.GetKeyEvent()->GetKeyCode().GetCode();
69 if ((KEY_DELETE
== nKey
) || (KEY_BACKSPACE
== nKey
))
73 m_pHelper
->ModifiedHdl( this );
78 return BaseClass::PreNotify( rNEvt
);
81 //------------------------------------------------------------------
82 void NumberFormatSampleField::SetFormatSupplier( const SvNumberFormatsSupplierObj
* pSupplier
)
86 TreatAsNumber( sal_True
);
88 SvNumberFormatter
* pFormatter
= pSupplier
->GetNumberFormatter();
89 SetFormatter( pFormatter
, sal_True
);
90 SetValue( 1234.56789 );
94 TreatAsNumber( sal_False
);
95 SetFormatter( NULL
, sal_True
);
100 //==================================================================
101 // OFormatSampleControl
102 //==================================================================
103 //------------------------------------------------------------------
104 OFormatSampleControl::OFormatSampleControl( Window
* pParent
, WinBits nWinStyle
)
105 :OFormatSampleControl_Base( PropertyControlType::Unknown
, pParent
, nWinStyle
)
109 //------------------------------------------------------------------
110 void SAL_CALL
OFormatSampleControl::setValue( const Any
& _rValue
) throw (IllegalTypeException
, RuntimeException
)
112 sal_Int32 nFormatKey
= 0;
113 if ( _rValue
>>= nFormatKey
)
115 // else set the new format key, the text will be reformatted
116 getTypedControlWindow()->SetFormatKey( nFormatKey
);
118 SvNumberFormatter
* pNF
= getTypedControlWindow()->GetFormatter();
119 const SvNumberformat
* pEntry
= pNF
->GetEntry( nFormatKey
);
120 OSL_ENSURE( pEntry
, "OFormatSampleControl::setValue: invalid format entry!" );
122 const bool bIsTextFormat
= ( pEntry
&& pEntry
->IsTextFormat() );
124 getTypedControlWindow()->SetText( String( PcrRes( RID_STR_TEXT_FORMAT
) ) );
126 getTypedControlWindow()->SetValue( pEntry
? getPreviewValue( *pEntry
) : 1234.56789 );
129 getTypedControlWindow()->SetText( String() );
131 //------------------------------------------------------------------
132 double OFormatSampleControl::getPreviewValue( const SvNumberformat
& i_rEntry
)
134 double nValue
= 1234.56789;
135 switch ( i_rEntry
.GetType() & ~NUMBERFORMAT_DEFINED
)
137 case NUMBERFORMAT_DATE
:
140 static ::com::sun::star::util::Date
STANDARD_DB_DATE(30,12,1899);
141 nValue
= ::dbtools::DBTypeConversion::toDouble(::dbtools::DBTypeConversion::toDate(static_cast<sal_Int32
>(aCurrentDate
.GetDate())),STANDARD_DB_DATE
);
144 case NUMBERFORMAT_TIME
:
145 case NUMBERFORMAT_DATETIME
:
148 nValue
= ::dbtools::DBTypeConversion::toDouble(::dbtools::DBTypeConversion::toTime(aCurrentTime
.GetTime()));
157 //------------------------------------------------------------------
158 double OFormatSampleControl::getPreviewValue(SvNumberFormatter
* _pNF
,sal_Int32 _nFormatKey
)
160 const SvNumberformat
* pEntry
= _pNF
->GetEntry(_nFormatKey
);
161 DBG_ASSERT( pEntry
, "OFormattedNumericControl::SetFormatDescription: invalid format key!" );
162 double nValue
= 1234.56789;
164 nValue
= getPreviewValue( *pEntry
);
167 //------------------------------------------------------------------
168 Any SAL_CALL
OFormatSampleControl::getValue() throw (RuntimeException
)
171 if ( getTypedControlWindow()->GetText().Len() )
172 aPropValue
<<= (sal_Int32
)getTypedControlWindow()->GetFormatKey();
176 //------------------------------------------------------------------
177 Type SAL_CALL
OFormatSampleControl::getValueType() throw (RuntimeException
)
179 return ::getCppuType( static_cast< sal_Int32
* >( NULL
) );
182 //==================================================================
183 // class OFormattedNumericControl
184 //==================================================================
185 DBG_NAME(OFormattedNumericControl
);
186 //------------------------------------------------------------------
187 OFormattedNumericControl::OFormattedNumericControl( Window
* pParent
, WinBits nWinStyle
)
188 :OFormattedNumericControl_Base( PropertyControlType::Unknown
, pParent
, nWinStyle
)
190 DBG_CTOR(OFormattedNumericControl
,NULL
);
192 getTypedControlWindow()->TreatAsNumber(sal_True
);
194 m_nLastDecimalDigits
= getTypedControlWindow()->GetDecimalDigits();
197 //------------------------------------------------------------------
198 OFormattedNumericControl::~OFormattedNumericControl()
200 DBG_DTOR(OFormattedNumericControl
,NULL
);
203 //------------------------------------------------------------------
204 void SAL_CALL
OFormattedNumericControl::setValue( const Any
& _rValue
) throw (IllegalTypeException
, RuntimeException
)
207 if ( _rValue
>>= nValue
)
208 getTypedControlWindow()->SetValue( nValue
);
210 getTypedControlWindow()->SetText(String());
213 //------------------------------------------------------------------
214 Any SAL_CALL
OFormattedNumericControl::getValue() throw (RuntimeException
)
217 if ( getTypedControlWindow()->GetText().Len() )
218 aPropValue
<<= (double)getTypedControlWindow()->GetValue();
222 //------------------------------------------------------------------
223 Type SAL_CALL
OFormattedNumericControl::getValueType() throw (RuntimeException
)
225 return ::getCppuType( static_cast< double* >( NULL
) );
228 //------------------------------------------------------------------
229 void OFormattedNumericControl::SetFormatDescription(const FormatDescription
& rDesc
)
231 sal_Bool bFallback
= sal_True
;
235 getTypedControlWindow()->TreatAsNumber(sal_True
);
237 SvNumberFormatter
* pFormatter
= rDesc
.pSupplier
->GetNumberFormatter();
238 if (pFormatter
!= getTypedControlWindow()->GetFormatter())
239 getTypedControlWindow()->SetFormatter(pFormatter
, sal_True
);
240 getTypedControlWindow()->SetFormatKey(rDesc
.nKey
);
242 const SvNumberformat
* pEntry
= getTypedControlWindow()->GetFormatter()->GetEntry(getTypedControlWindow()->GetFormatKey());
243 DBG_ASSERT( pEntry
, "OFormattedNumericControl::SetFormatDescription: invalid format key!" );
246 switch (pEntry
->GetType() & ~NUMBERFORMAT_DEFINED
)
248 case NUMBERFORMAT_NUMBER
:
249 case NUMBERFORMAT_CURRENCY
:
250 case NUMBERFORMAT_SCIENTIFIC
:
251 case NUMBERFORMAT_FRACTION
:
252 case NUMBERFORMAT_PERCENT
:
253 m_nLastDecimalDigits
= getTypedControlWindow()->GetDecimalDigits();
255 case NUMBERFORMAT_DATETIME
:
256 case NUMBERFORMAT_DATE
:
257 case NUMBERFORMAT_TIME
:
258 m_nLastDecimalDigits
= 7;
261 m_nLastDecimalDigits
= 0;
264 bFallback
= sal_False
;
271 getTypedControlWindow()->TreatAsNumber(sal_False
);
272 getTypedControlWindow()->SetFormatter(NULL
, sal_True
);
273 getTypedControlWindow()->SetText(String());
274 m_nLastDecimalDigits
= 0;
278 //========================================================================
280 //========================================================================
281 //------------------------------------------------------------------
282 OFileUrlControl::OFileUrlControl( Window
* pParent
, WinBits nWinStyle
)
283 :OFileUrlControl_Base( PropertyControlType::Unknown
, pParent
, nWinStyle
| WB_DROPDOWN
)
285 getTypedControlWindow()->SetDropDownLineCount( 10 );
286 getTypedControlWindow()->SetPlaceHolder( String( PcrRes( RID_EMBED_IMAGE_PLACEHOLDER
) ) ) ;
289 //------------------------------------------------------------------
290 OFileUrlControl::~OFileUrlControl()
294 //------------------------------------------------------------------
295 void SAL_CALL
OFileUrlControl::setValue( const Any
& _rValue
) throw (IllegalTypeException
, RuntimeException
)
297 ::rtl::OUString sURL
;
298 if ( ( _rValue
>>= sURL
) )
300 if ( sURL
.indexOf( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "vnd.sun.star.GraphicObject:" ) ) ) == 0 )
301 getTypedControlWindow()->DisplayURL( getTypedControlWindow()->GetPlaceHolder() );
303 getTypedControlWindow()->DisplayURL( sURL
);
306 getTypedControlWindow()->SetText( String() );
309 //------------------------------------------------------------------
310 Any SAL_CALL
OFileUrlControl::getValue() throw (RuntimeException
)
313 if ( getTypedControlWindow()->GetText().Len() )
314 aPropValue
<<= (::rtl::OUString
)getTypedControlWindow()->GetURL();
318 //------------------------------------------------------------------
319 Type SAL_CALL
OFileUrlControl::getValueType() throw (RuntimeException
)
321 return ::getCppuType( static_cast< ::rtl::OUString
* >( NULL
) );
324 //========================================================================
325 //= OTimeDurationControl
326 //========================================================================
327 //------------------------------------------------------------------
328 OTimeDurationControl::OTimeDurationControl( ::Window
* pParent
, WinBits nWinStyle
)
329 :ONumericControl( pParent
, nWinStyle
)
331 getTypedControlWindow()->SetUnit( FUNIT_CUSTOM
);
332 getTypedControlWindow()->SetCustomUnitText( String::CreateFromAscii( " ms" ) );
333 getTypedControlWindow()->SetCustomConvertHdl( LINK( this, OTimeDurationControl
, OnCustomConvert
) );
336 //------------------------------------------------------------------
337 OTimeDurationControl::~OTimeDurationControl()
341 //------------------------------------------------------------------
342 ::sal_Int16 SAL_CALL
OTimeDurationControl::getControlType() throw (::com::sun::star::uno::RuntimeException
)
344 // don't use the base class'es method, it would claim we're a standard control, which
346 return PropertyControlType::Unknown
;
349 //------------------------------------------------------------------
350 IMPL_LINK( OTimeDurationControl
, OnCustomConvert
, MetricField
*, /*pField*/ )
352 long nMultiplier
= 1;
353 if ( getTypedControlWindow()->GetCurUnitText().EqualsIgnoreCaseAscii( "ms" ) )
355 if ( getTypedControlWindow()->GetCurUnitText().EqualsIgnoreCaseAscii( "s" ) )
357 else if ( getTypedControlWindow()->GetCurUnitText().EqualsIgnoreCaseAscii( "m" ) )
358 nMultiplier
= 1000 * 60;
359 else if ( getTypedControlWindow()->GetCurUnitText().EqualsIgnoreCaseAscii( "h" ) )
360 nMultiplier
= 1000 * 60 * 60;
362 getTypedControlWindow()->SetValue( getTypedControlWindow()->GetLastValue() * nMultiplier
);
367 //............................................................................
369 //............................................................................