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 <tools/debug.hxx>
26 #include <svl/zformat.hxx>
27 #include <connectivity/dbconversion.hxx>
28 #include <com/sun/star/util/Time.hpp>
29 #include "modulepcr.hxx"
30 #include "propresid.hrc"
32 //............................................................................
35 //............................................................................
37 using ::com::sun::star::uno::Any
;
38 using ::com::sun::star::uno::Type
;
39 using ::com::sun::star::beans::IllegalTypeException
;
40 using ::com::sun::star::uno::RuntimeException
;
42 namespace PropertyControlType
= ::com::sun::star::inspection::PropertyControlType
;
44 //==================================================================
45 // NumberFormatSampleField
46 //==================================================================
47 //------------------------------------------------------------------
48 long 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 (EVENT_KEYINPUT
== rNEvt
.GetType())
53 sal_uInt16 nKey
= rNEvt
.GetKeyEvent()->GetKeyCode().GetCode();
55 if ((KEY_DELETE
== nKey
) || (KEY_BACKSPACE
== nKey
))
59 m_pHelper
->ModifiedHdl( this );
64 return BaseClass::PreNotify( rNEvt
);
67 //------------------------------------------------------------------
68 void NumberFormatSampleField::SetFormatSupplier( const SvNumberFormatsSupplierObj
* pSupplier
)
72 TreatAsNumber( sal_True
);
74 SvNumberFormatter
* pFormatter
= pSupplier
->GetNumberFormatter();
75 SetFormatter( pFormatter
, sal_True
);
76 SetValue( 1234.56789 );
80 TreatAsNumber( sal_False
);
81 SetFormatter( NULL
, sal_True
);
86 //==================================================================
87 // OFormatSampleControl
88 //==================================================================
89 //------------------------------------------------------------------
90 OFormatSampleControl::OFormatSampleControl( Window
* pParent
, WinBits nWinStyle
)
91 :OFormatSampleControl_Base( PropertyControlType::Unknown
, pParent
, nWinStyle
)
95 //------------------------------------------------------------------
96 void SAL_CALL
OFormatSampleControl::setValue( const Any
& _rValue
) throw (IllegalTypeException
, RuntimeException
)
98 sal_Int32 nFormatKey
= 0;
99 if ( _rValue
>>= nFormatKey
)
101 // else set the new format key, the text will be reformatted
102 getTypedControlWindow()->SetFormatKey( nFormatKey
);
104 SvNumberFormatter
* pNF
= getTypedControlWindow()->GetFormatter();
105 const SvNumberformat
* pEntry
= pNF
->GetEntry( nFormatKey
);
106 OSL_ENSURE( pEntry
, "OFormatSampleControl::setValue: invalid format entry!" );
108 const bool bIsTextFormat
= ( pEntry
&& pEntry
->IsTextFormat() );
110 getTypedControlWindow()->SetText( String( PcrRes( RID_STR_TEXT_FORMAT
) ) );
112 getTypedControlWindow()->SetValue( pEntry
? getPreviewValue( *pEntry
) : 1234.56789 );
115 getTypedControlWindow()->SetText( String() );
117 //------------------------------------------------------------------
118 double OFormatSampleControl::getPreviewValue( const SvNumberformat
& i_rEntry
)
120 double nValue
= 1234.56789;
121 switch ( i_rEntry
.GetType() & ~NUMBERFORMAT_DEFINED
)
123 case NUMBERFORMAT_DATE
:
125 Date
aCurrentDate( Date::SYSTEM
);
126 static ::com::sun::star::util::Date
STANDARD_DB_DATE(30,12,1899);
127 nValue
= ::dbtools::DBTypeConversion::toDouble(::dbtools::DBTypeConversion::toDate(static_cast<sal_Int32
>(aCurrentDate
.GetDate())),STANDARD_DB_DATE
);
130 case NUMBERFORMAT_TIME
:
131 case NUMBERFORMAT_DATETIME
:
133 Time
aCurrentTime( Time::SYSTEM
);
134 nValue
= ::dbtools::DBTypeConversion::toDouble(::dbtools::DBTypeConversion::toTime(aCurrentTime
.GetTime()));
143 //------------------------------------------------------------------
144 double OFormatSampleControl::getPreviewValue(SvNumberFormatter
* _pNF
,sal_Int32 _nFormatKey
)
146 const SvNumberformat
* pEntry
= _pNF
->GetEntry(_nFormatKey
);
147 DBG_ASSERT( pEntry
, "OFormattedNumericControl::SetFormatDescription: invalid format key!" );
148 double nValue
= 1234.56789;
150 nValue
= getPreviewValue( *pEntry
);
153 //------------------------------------------------------------------
154 Any SAL_CALL
OFormatSampleControl::getValue() throw (RuntimeException
)
157 if ( !getTypedControlWindow()->GetText().isEmpty() )
158 aPropValue
<<= (sal_Int32
)getTypedControlWindow()->GetFormatKey();
162 //------------------------------------------------------------------
163 Type SAL_CALL
OFormatSampleControl::getValueType() throw (RuntimeException
)
165 return ::getCppuType( static_cast< sal_Int32
* >( NULL
) );
168 //==================================================================
169 // class OFormattedNumericControl
170 //==================================================================
171 DBG_NAME(OFormattedNumericControl
);
172 //------------------------------------------------------------------
173 OFormattedNumericControl::OFormattedNumericControl( Window
* pParent
, WinBits nWinStyle
)
174 :OFormattedNumericControl_Base( PropertyControlType::Unknown
, pParent
, nWinStyle
)
176 DBG_CTOR(OFormattedNumericControl
,NULL
);
178 getTypedControlWindow()->TreatAsNumber(sal_True
);
180 m_nLastDecimalDigits
= getTypedControlWindow()->GetDecimalDigits();
183 //------------------------------------------------------------------
184 OFormattedNumericControl::~OFormattedNumericControl()
186 DBG_DTOR(OFormattedNumericControl
,NULL
);
189 //------------------------------------------------------------------
190 void SAL_CALL
OFormattedNumericControl::setValue( const Any
& _rValue
) throw (IllegalTypeException
, RuntimeException
)
193 if ( _rValue
>>= nValue
)
194 getTypedControlWindow()->SetValue( nValue
);
196 getTypedControlWindow()->SetText(String());
199 //------------------------------------------------------------------
200 Any SAL_CALL
OFormattedNumericControl::getValue() throw (RuntimeException
)
203 if ( !getTypedControlWindow()->GetText().isEmpty() )
204 aPropValue
<<= (double)getTypedControlWindow()->GetValue();
208 //------------------------------------------------------------------
209 Type SAL_CALL
OFormattedNumericControl::getValueType() throw (RuntimeException
)
211 return ::getCppuType( static_cast< double* >( NULL
) );
214 //------------------------------------------------------------------
215 void OFormattedNumericControl::SetFormatDescription(const FormatDescription
& rDesc
)
217 sal_Bool bFallback
= sal_True
;
221 getTypedControlWindow()->TreatAsNumber(sal_True
);
223 SvNumberFormatter
* pFormatter
= rDesc
.pSupplier
->GetNumberFormatter();
224 if (pFormatter
!= getTypedControlWindow()->GetFormatter())
225 getTypedControlWindow()->SetFormatter(pFormatter
, sal_True
);
226 getTypedControlWindow()->SetFormatKey(rDesc
.nKey
);
228 const SvNumberformat
* pEntry
= getTypedControlWindow()->GetFormatter()->GetEntry(getTypedControlWindow()->GetFormatKey());
229 DBG_ASSERT( pEntry
, "OFormattedNumericControl::SetFormatDescription: invalid format key!" );
232 switch (pEntry
->GetType() & ~NUMBERFORMAT_DEFINED
)
234 case NUMBERFORMAT_NUMBER
:
235 case NUMBERFORMAT_CURRENCY
:
236 case NUMBERFORMAT_SCIENTIFIC
:
237 case NUMBERFORMAT_FRACTION
:
238 case NUMBERFORMAT_PERCENT
:
239 m_nLastDecimalDigits
= getTypedControlWindow()->GetDecimalDigits();
241 case NUMBERFORMAT_DATETIME
:
242 case NUMBERFORMAT_DATE
:
243 case NUMBERFORMAT_TIME
:
244 m_nLastDecimalDigits
= 7;
247 m_nLastDecimalDigits
= 0;
250 bFallback
= sal_False
;
257 getTypedControlWindow()->TreatAsNumber(sal_False
);
258 getTypedControlWindow()->SetFormatter(NULL
, sal_True
);
259 getTypedControlWindow()->SetText(String());
260 m_nLastDecimalDigits
= 0;
264 //========================================================================
266 //========================================================================
267 //------------------------------------------------------------------
268 OFileUrlControl::OFileUrlControl( Window
* pParent
, WinBits nWinStyle
)
269 :OFileUrlControl_Base( PropertyControlType::Unknown
, pParent
, nWinStyle
| WB_DROPDOWN
)
271 getTypedControlWindow()->SetDropDownLineCount( 10 );
272 getTypedControlWindow()->SetPlaceHolder( String( PcrRes( RID_EMBED_IMAGE_PLACEHOLDER
) ) ) ;
275 //------------------------------------------------------------------
276 OFileUrlControl::~OFileUrlControl()
280 //------------------------------------------------------------------
281 void SAL_CALL
OFileUrlControl::setValue( const Any
& _rValue
) throw (IllegalTypeException
, RuntimeException
)
284 if ( ( _rValue
>>= sURL
) )
286 if ( sURL
.indexOf( "vnd.sun.star.GraphicObject:" ) == 0 )
287 getTypedControlWindow()->DisplayURL( getTypedControlWindow()->GetPlaceHolder() );
289 getTypedControlWindow()->DisplayURL( sURL
);
292 getTypedControlWindow()->SetText( String() );
295 //------------------------------------------------------------------
296 Any SAL_CALL
OFileUrlControl::getValue() throw (RuntimeException
)
299 if ( !getTypedControlWindow()->GetText().isEmpty() )
300 aPropValue
<<= (OUString
)getTypedControlWindow()->GetURL();
304 //------------------------------------------------------------------
305 Type SAL_CALL
OFileUrlControl::getValueType() throw (RuntimeException
)
307 return ::getCppuType( static_cast< OUString
* >( NULL
) );
310 //========================================================================
311 //= OTimeDurationControl
312 //========================================================================
313 //------------------------------------------------------------------
314 OTimeDurationControl::OTimeDurationControl( ::Window
* pParent
, WinBits nWinStyle
)
315 :ONumericControl( pParent
, nWinStyle
)
317 getTypedControlWindow()->SetUnit( FUNIT_CUSTOM
);
318 getTypedControlWindow()->SetCustomUnitText(OUString(" ms"));
319 getTypedControlWindow()->SetCustomConvertHdl( LINK( this, OTimeDurationControl
, OnCustomConvert
) );
322 //------------------------------------------------------------------
323 OTimeDurationControl::~OTimeDurationControl()
327 //------------------------------------------------------------------
328 ::sal_Int16 SAL_CALL
OTimeDurationControl::getControlType() throw (::com::sun::star::uno::RuntimeException
)
330 // don't use the base class'es method, it would claim we're a standard control, which
332 return PropertyControlType::Unknown
;
335 //------------------------------------------------------------------
336 IMPL_LINK( OTimeDurationControl
, OnCustomConvert
, MetricField
*, /*pField*/ )
338 long nMultiplier
= 1;
339 if ( getTypedControlWindow()->GetCurUnitText().equalsIgnoreAsciiCase( "ms" ) )
341 if ( getTypedControlWindow()->GetCurUnitText().equalsIgnoreAsciiCase( "s" ) )
343 else if ( getTypedControlWindow()->GetCurUnitText().equalsIgnoreAsciiCase( "m" ) )
344 nMultiplier
= 1000 * 60;
345 else if ( getTypedControlWindow()->GetCurUnitText().equalsIgnoreAsciiCase( "h" ) )
346 nMultiplier
= 1000 * 60 * 60;
348 getTypedControlWindow()->SetValue( getTypedControlWindow()->GetLastValue() * nMultiplier
);
353 //............................................................................
355 //............................................................................
357 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */