Bump for 3.6-28
[LibreOffice.git] / extensions / source / propctrlr / usercontrol.cxx
blob84108c64e4e6693ee398ad5778cd185fadc74d3a
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 #include "usercontrol.hxx"
31 /** === begin UNO includes === **/
32 #include <com/sun/star/inspection/PropertyControlType.hpp>
33 /** === end UNO includes === **/
34 #include <svl/numuno.hxx>
35 #include <rtl/math.hxx>
36 #include <tools/debug.hxx>
37 #include <svl/zformat.hxx>
38 #include <connectivity/dbconversion.hxx>
39 #include <com/sun/star/util/Time.hpp>
40 #include "modulepcr.hxx"
41 #include "propresid.hrc"
43 //............................................................................
44 namespace pcr
46 //............................................................................
48 /** === begin UNO using === **/
49 using ::com::sun::star::uno::Any;
50 using ::com::sun::star::uno::Type;
51 using ::com::sun::star::beans::IllegalTypeException;
52 using ::com::sun::star::uno::RuntimeException;
53 /** === end UNO using === **/
54 namespace PropertyControlType = ::com::sun::star::inspection::PropertyControlType;
56 //==================================================================
57 // NumberFormatSampleField
58 //==================================================================
59 //------------------------------------------------------------------
60 long NumberFormatSampleField::PreNotify( NotifyEvent& rNEvt )
62 // want to handle two keys myself : Del/Backspace should empty the window (setting my prop to "standard" this way)
63 if (EVENT_KEYINPUT == rNEvt.GetType())
65 sal_uInt16 nKey = rNEvt.GetKeyEvent()->GetKeyCode().GetCode();
67 if ((KEY_DELETE == nKey) || (KEY_BACKSPACE == nKey))
69 SetText( String() );
70 if ( m_pHelper )
71 m_pHelper->ModifiedHdl( this );
72 return 1;
76 return BaseClass::PreNotify( rNEvt );
79 //------------------------------------------------------------------
80 void NumberFormatSampleField::SetFormatSupplier( const SvNumberFormatsSupplierObj* pSupplier )
82 if ( pSupplier )
84 TreatAsNumber( sal_True );
86 SvNumberFormatter* pFormatter = pSupplier->GetNumberFormatter();
87 SetFormatter( pFormatter, sal_True );
88 SetValue( 1234.56789 );
90 else
92 TreatAsNumber( sal_False );
93 SetFormatter( NULL, sal_True );
94 SetText( String() );
98 //==================================================================
99 // OFormatSampleControl
100 //==================================================================
101 //------------------------------------------------------------------
102 OFormatSampleControl::OFormatSampleControl( Window* pParent, WinBits nWinStyle )
103 :OFormatSampleControl_Base( PropertyControlType::Unknown, pParent, nWinStyle )
107 //------------------------------------------------------------------
108 void SAL_CALL OFormatSampleControl::setValue( const Any& _rValue ) throw (IllegalTypeException, RuntimeException)
110 sal_Int32 nFormatKey = 0;
111 if ( _rValue >>= nFormatKey )
113 // else set the new format key, the text will be reformatted
114 getTypedControlWindow()->SetFormatKey( nFormatKey );
116 SvNumberFormatter* pNF = getTypedControlWindow()->GetFormatter();
117 const SvNumberformat* pEntry = pNF->GetEntry( nFormatKey );
118 OSL_ENSURE( pEntry, "OFormatSampleControl::setValue: invalid format entry!" );
120 const bool bIsTextFormat = ( pEntry && pEntry->IsTextFormat() );
121 if ( bIsTextFormat )
122 getTypedControlWindow()->SetText( String( PcrRes( RID_STR_TEXT_FORMAT ) ) );
123 else
124 getTypedControlWindow()->SetValue( pEntry ? getPreviewValue( *pEntry ) : 1234.56789 );
126 else
127 getTypedControlWindow()->SetText( String() );
129 //------------------------------------------------------------------
130 double OFormatSampleControl::getPreviewValue( const SvNumberformat& i_rEntry )
132 double nValue = 1234.56789;
133 switch ( i_rEntry.GetType() & ~NUMBERFORMAT_DEFINED )
135 case NUMBERFORMAT_DATE:
137 Date aCurrentDate( Date::SYSTEM );
138 static ::com::sun::star::util::Date STANDARD_DB_DATE(30,12,1899);
139 nValue = ::dbtools::DBTypeConversion::toDouble(::dbtools::DBTypeConversion::toDate(static_cast<sal_Int32>(aCurrentDate.GetDate())),STANDARD_DB_DATE);
141 break;
142 case NUMBERFORMAT_TIME:
143 case NUMBERFORMAT_DATETIME:
145 Time aCurrentTime( Time::SYSTEM );
146 nValue = ::dbtools::DBTypeConversion::toDouble(::dbtools::DBTypeConversion::toTime(aCurrentTime.GetTime()));
148 break;
149 default:
150 break;
152 return nValue;
155 //------------------------------------------------------------------
156 double OFormatSampleControl::getPreviewValue(SvNumberFormatter* _pNF,sal_Int32 _nFormatKey)
158 const SvNumberformat* pEntry = _pNF->GetEntry(_nFormatKey);
159 DBG_ASSERT( pEntry, "OFormattedNumericControl::SetFormatDescription: invalid format key!" );
160 double nValue = 1234.56789;
161 if ( pEntry )
162 nValue = getPreviewValue( *pEntry );
163 return nValue;
165 //------------------------------------------------------------------
166 Any SAL_CALL OFormatSampleControl::getValue() throw (RuntimeException)
168 Any aPropValue;
169 if ( getTypedControlWindow()->GetText().Len() )
170 aPropValue <<= (sal_Int32)getTypedControlWindow()->GetFormatKey();
171 return aPropValue;
174 //------------------------------------------------------------------
175 Type SAL_CALL OFormatSampleControl::getValueType() throw (RuntimeException)
177 return ::getCppuType( static_cast< sal_Int32* >( NULL ) );
180 //==================================================================
181 // class OFormattedNumericControl
182 //==================================================================
183 DBG_NAME(OFormattedNumericControl);
184 //------------------------------------------------------------------
185 OFormattedNumericControl::OFormattedNumericControl( Window* pParent, WinBits nWinStyle )
186 :OFormattedNumericControl_Base( PropertyControlType::Unknown, pParent, nWinStyle )
188 DBG_CTOR(OFormattedNumericControl,NULL);
190 getTypedControlWindow()->TreatAsNumber(sal_True);
192 m_nLastDecimalDigits = getTypedControlWindow()->GetDecimalDigits();
195 //------------------------------------------------------------------
196 OFormattedNumericControl::~OFormattedNumericControl()
198 DBG_DTOR(OFormattedNumericControl,NULL);
201 //------------------------------------------------------------------
202 void SAL_CALL OFormattedNumericControl::setValue( const Any& _rValue ) throw (IllegalTypeException, RuntimeException)
204 double nValue( 0 );
205 if ( _rValue >>= nValue )
206 getTypedControlWindow()->SetValue( nValue );
207 else
208 getTypedControlWindow()->SetText(String());
211 //------------------------------------------------------------------
212 Any SAL_CALL OFormattedNumericControl::getValue() throw (RuntimeException)
214 Any aPropValue;
215 if ( getTypedControlWindow()->GetText().Len() )
216 aPropValue <<= (double)getTypedControlWindow()->GetValue();
217 return aPropValue;
220 //------------------------------------------------------------------
221 Type SAL_CALL OFormattedNumericControl::getValueType() throw (RuntimeException)
223 return ::getCppuType( static_cast< double* >( NULL ) );
226 //------------------------------------------------------------------
227 void OFormattedNumericControl::SetFormatDescription(const FormatDescription& rDesc)
229 sal_Bool bFallback = sal_True;
231 if (rDesc.pSupplier)
233 getTypedControlWindow()->TreatAsNumber(sal_True);
235 SvNumberFormatter* pFormatter = rDesc.pSupplier->GetNumberFormatter();
236 if (pFormatter != getTypedControlWindow()->GetFormatter())
237 getTypedControlWindow()->SetFormatter(pFormatter, sal_True);
238 getTypedControlWindow()->SetFormatKey(rDesc.nKey);
240 const SvNumberformat* pEntry = getTypedControlWindow()->GetFormatter()->GetEntry(getTypedControlWindow()->GetFormatKey());
241 DBG_ASSERT( pEntry, "OFormattedNumericControl::SetFormatDescription: invalid format key!" );
242 if ( pEntry )
244 switch (pEntry->GetType() & ~NUMBERFORMAT_DEFINED)
246 case NUMBERFORMAT_NUMBER:
247 case NUMBERFORMAT_CURRENCY:
248 case NUMBERFORMAT_SCIENTIFIC:
249 case NUMBERFORMAT_FRACTION:
250 case NUMBERFORMAT_PERCENT:
251 m_nLastDecimalDigits = getTypedControlWindow()->GetDecimalDigits();
252 break;
253 case NUMBERFORMAT_DATETIME:
254 case NUMBERFORMAT_DATE:
255 case NUMBERFORMAT_TIME:
256 m_nLastDecimalDigits = 7;
257 break;
258 default:
259 m_nLastDecimalDigits = 0;
260 break;
262 bFallback = sal_False;
267 if ( bFallback )
269 getTypedControlWindow()->TreatAsNumber(sal_False);
270 getTypedControlWindow()->SetFormatter(NULL, sal_True);
271 getTypedControlWindow()->SetText(String());
272 m_nLastDecimalDigits = 0;
276 //========================================================================
277 //= OFileUrlControl
278 //========================================================================
279 //------------------------------------------------------------------
280 OFileUrlControl::OFileUrlControl( Window* pParent, WinBits nWinStyle )
281 :OFileUrlControl_Base( PropertyControlType::Unknown, pParent, nWinStyle | WB_DROPDOWN )
283 getTypedControlWindow()->SetDropDownLineCount( 10 );
284 getTypedControlWindow()->SetPlaceHolder( String( PcrRes( RID_EMBED_IMAGE_PLACEHOLDER ) ) ) ;
287 //------------------------------------------------------------------
288 OFileUrlControl::~OFileUrlControl()
292 //------------------------------------------------------------------
293 void SAL_CALL OFileUrlControl::setValue( const Any& _rValue ) throw (IllegalTypeException, RuntimeException)
295 ::rtl::OUString sURL;
296 if ( ( _rValue >>= sURL ) )
298 if ( sURL.indexOf( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "vnd.sun.star.GraphicObject:" ) ) ) == 0 )
299 getTypedControlWindow()->DisplayURL( getTypedControlWindow()->GetPlaceHolder() );
300 else
301 getTypedControlWindow()->DisplayURL( sURL );
303 else
304 getTypedControlWindow()->SetText( String() );
307 //------------------------------------------------------------------
308 Any SAL_CALL OFileUrlControl::getValue() throw (RuntimeException)
310 Any aPropValue;
311 if ( getTypedControlWindow()->GetText().Len() )
312 aPropValue <<= (::rtl::OUString)getTypedControlWindow()->GetURL();
313 return aPropValue;
316 //------------------------------------------------------------------
317 Type SAL_CALL OFileUrlControl::getValueType() throw (RuntimeException)
319 return ::getCppuType( static_cast< ::rtl::OUString* >( NULL ) );
322 //========================================================================
323 //= OTimeDurationControl
324 //========================================================================
325 //------------------------------------------------------------------
326 OTimeDurationControl::OTimeDurationControl( ::Window* pParent, WinBits nWinStyle )
327 :ONumericControl( pParent, nWinStyle )
329 getTypedControlWindow()->SetUnit( FUNIT_CUSTOM );
330 getTypedControlWindow()->SetCustomUnitText( String::CreateFromAscii( " ms" ) );
331 getTypedControlWindow()->SetCustomConvertHdl( LINK( this, OTimeDurationControl, OnCustomConvert ) );
334 //------------------------------------------------------------------
335 OTimeDurationControl::~OTimeDurationControl()
339 //------------------------------------------------------------------
340 ::sal_Int16 SAL_CALL OTimeDurationControl::getControlType() throw (::com::sun::star::uno::RuntimeException)
342 // don't use the base class'es method, it would claim we're a standard control, which
343 // we in fact aren't
344 return PropertyControlType::Unknown;
347 //------------------------------------------------------------------
348 IMPL_LINK( OTimeDurationControl, OnCustomConvert, MetricField*, /*pField*/ )
350 long nMultiplier = 1;
351 if ( getTypedControlWindow()->GetCurUnitText().EqualsIgnoreCaseAscii( "ms" ) )
352 nMultiplier = 1;
353 if ( getTypedControlWindow()->GetCurUnitText().EqualsIgnoreCaseAscii( "s" ) )
354 nMultiplier = 1000;
355 else if ( getTypedControlWindow()->GetCurUnitText().EqualsIgnoreCaseAscii( "m" ) )
356 nMultiplier = 1000 * 60;
357 else if ( getTypedControlWindow()->GetCurUnitText().EqualsIgnoreCaseAscii( "h" ) )
358 nMultiplier = 1000 * 60 * 60;
360 getTypedControlWindow()->SetValue( getTypedControlWindow()->GetLastValue() * nMultiplier );
362 return 0L;
365 //............................................................................
366 } // namespace pcr
367 //............................................................................
369 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */