merge the formfield patch from ooo-build
[ooovba.git] / extensions / source / propctrlr / usercontrol.cxx
blobf2a6bb90258fe3f4a77098b73cb46c5fba8b459d
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: usercontrol.cxx,v $
10 * $Revision: 1.12 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_extensions.hxx"
33 #include "usercontrol.hxx"
35 /** === begin UNO includes === **/
36 #include <com/sun/star/inspection/PropertyControlType.hpp>
37 #include <com/sun/star/inspection/PropertyControlType.hpp>
38 /** === end UNO includes === **/
39 #include <svtools/numuno.hxx>
40 #include <rtl/math.hxx>
41 #include <tools/debug.hxx>
42 #include <svtools/zformat.hxx>
43 #include <connectivity/dbconversion.hxx>
44 #include <com/sun/star/util/Time.hpp>
45 #include "modulepcr.hxx"
46 #include "propresid.hrc"
47 //............................................................................
48 namespace pcr
50 //............................................................................
52 /** === begin UNO using === **/
53 using ::com::sun::star::uno::Any;
54 using ::com::sun::star::uno::Type;
55 using ::com::sun::star::beans::IllegalTypeException;
56 using ::com::sun::star::uno::RuntimeException;
57 /** === end UNO using === **/
58 namespace PropertyControlType = ::com::sun::star::inspection::PropertyControlType;
60 //==================================================================
61 // NumberFormatSampleField
62 //==================================================================
63 //------------------------------------------------------------------
64 long NumberFormatSampleField::PreNotify( NotifyEvent& rNEvt )
66 // want to handle two keys myself : Del/Backspace should empty the window (setting my prop to "standard" this way)
67 if (EVENT_KEYINPUT == rNEvt.GetType())
69 sal_uInt16 nKey = rNEvt.GetKeyEvent()->GetKeyCode().GetCode();
71 if ((KEY_DELETE == nKey) || (KEY_BACKSPACE == nKey))
73 SetText( String() );
74 if ( m_pHelper )
75 m_pHelper->ModifiedHdl( this );
76 return 1;
80 return BaseClass::PreNotify( rNEvt );
83 //------------------------------------------------------------------
84 void NumberFormatSampleField::SetFormatSupplier( const SvNumberFormatsSupplierObj* pSupplier )
86 if ( pSupplier )
88 TreatAsNumber( sal_True );
90 SvNumberFormatter* pFormatter = pSupplier->GetNumberFormatter();
91 SetFormatter( pFormatter, sal_True );
92 SetValue( 1234.56789 );
94 else
96 TreatAsNumber( sal_False );
97 SetFormatter( NULL, sal_True );
98 SetText( String() );
102 //==================================================================
103 // OFormatSampleControl
104 //==================================================================
105 //------------------------------------------------------------------
106 OFormatSampleControl::OFormatSampleControl( Window* pParent, WinBits nWinStyle )
107 :OFormatSampleControl_Base( PropertyControlType::Unknown, pParent, nWinStyle )
111 //------------------------------------------------------------------
112 void SAL_CALL OFormatSampleControl::setValue( const Any& _rValue ) throw (IllegalTypeException, RuntimeException)
114 sal_Int32 nFormatKey = 0;
115 if ( _rValue >>= nFormatKey )
117 // else set the new format key, the text will be reformatted
118 getTypedControlWindow()->SetFormatKey( nFormatKey );
120 SvNumberFormatter* pNF = getTypedControlWindow()->GetFormatter();
121 getTypedControlWindow()->SetValue( getPreviewValue(pNF,getTypedControlWindow()->GetFormatKey()) );
123 else
124 getTypedControlWindow()->SetText( String() );
126 //------------------------------------------------------------------
127 double OFormatSampleControl::getPreviewValue(SvNumberFormatter* _pNF,sal_Int32 _nFormatKey)
129 const SvNumberformat* pEntry = _pNF->GetEntry(_nFormatKey);
130 DBG_ASSERT( pEntry, "OFormattedNumericControl::SetFormatDescription: invalid format key!" );
131 double nValue = 1234.56789;
132 if ( pEntry )
134 switch (pEntry->GetType() & ~NUMBERFORMAT_DEFINED)
136 case NUMBERFORMAT_DATE:
138 Date aCurrentDate;
139 static ::com::sun::star::util::Date STANDARD_DB_DATE(30,12,1899);
140 nValue = ::dbtools::DBTypeConversion::toDouble(::dbtools::DBTypeConversion::toDate(static_cast<sal_Int32>(aCurrentDate.GetDate())),STANDARD_DB_DATE);
142 break;
143 case NUMBERFORMAT_TIME:
144 case NUMBERFORMAT_DATETIME:
146 Time aCurrentTime;
147 nValue = ::dbtools::DBTypeConversion::toDouble(::dbtools::DBTypeConversion::toTime(aCurrentTime.GetTime()));
149 break;
150 default:
151 break;
154 return nValue;
156 //------------------------------------------------------------------
157 Any SAL_CALL OFormatSampleControl::getValue() throw (RuntimeException)
159 Any aPropValue;
160 if ( getTypedControlWindow()->GetText().Len() )
161 aPropValue <<= (sal_Int32)getTypedControlWindow()->GetFormatKey();
162 return aPropValue;
165 //------------------------------------------------------------------
166 Type SAL_CALL OFormatSampleControl::getValueType() throw (RuntimeException)
168 return ::getCppuType( static_cast< sal_Int32* >( NULL ) );
171 //==================================================================
172 // class OFormattedNumericControl
173 //==================================================================
174 DBG_NAME(OFormattedNumericControl);
175 //------------------------------------------------------------------
176 OFormattedNumericControl::OFormattedNumericControl( Window* pParent, WinBits nWinStyle )
177 :OFormattedNumericControl_Base( PropertyControlType::Unknown, pParent, nWinStyle )
179 DBG_CTOR(OFormattedNumericControl,NULL);
181 getTypedControlWindow()->TreatAsNumber(sal_True);
183 m_nLastDecimalDigits = getTypedControlWindow()->GetDecimalDigits();
186 //------------------------------------------------------------------
187 OFormattedNumericControl::~OFormattedNumericControl()
189 DBG_DTOR(OFormattedNumericControl,NULL);
192 //------------------------------------------------------------------
193 void SAL_CALL OFormattedNumericControl::setValue( const Any& _rValue ) throw (IllegalTypeException, RuntimeException)
195 double nValue( 0 );
196 if ( _rValue >>= nValue )
197 getTypedControlWindow()->SetValue( nValue );
198 else
199 getTypedControlWindow()->SetText(String());
202 //------------------------------------------------------------------
203 Any SAL_CALL OFormattedNumericControl::getValue() throw (RuntimeException)
205 Any aPropValue;
206 if ( getTypedControlWindow()->GetText().Len() )
207 aPropValue <<= (double)getTypedControlWindow()->GetValue();
208 return aPropValue;
211 //------------------------------------------------------------------
212 Type SAL_CALL OFormattedNumericControl::getValueType() throw (RuntimeException)
214 return ::getCppuType( static_cast< double* >( NULL ) );
217 //------------------------------------------------------------------
218 void OFormattedNumericControl::SetFormatDescription(const FormatDescription& rDesc)
220 sal_Bool bFallback = sal_True;
222 if (rDesc.pSupplier)
224 getTypedControlWindow()->TreatAsNumber(sal_True);
226 SvNumberFormatter* pFormatter = rDesc.pSupplier->GetNumberFormatter();
227 if (pFormatter != getTypedControlWindow()->GetFormatter())
228 getTypedControlWindow()->SetFormatter(pFormatter, sal_True);
229 getTypedControlWindow()->SetFormatKey(rDesc.nKey);
231 const SvNumberformat* pEntry = getTypedControlWindow()->GetFormatter()->GetEntry(getTypedControlWindow()->GetFormatKey());
232 DBG_ASSERT( pEntry, "OFormattedNumericControl::SetFormatDescription: invalid format key!" );
233 if ( pEntry )
235 switch (pEntry->GetType() & ~NUMBERFORMAT_DEFINED)
237 case NUMBERFORMAT_NUMBER:
238 case NUMBERFORMAT_CURRENCY:
239 case NUMBERFORMAT_SCIENTIFIC:
240 case NUMBERFORMAT_FRACTION:
241 case NUMBERFORMAT_PERCENT:
242 m_nLastDecimalDigits = getTypedControlWindow()->GetDecimalDigits();
243 break;
244 case NUMBERFORMAT_DATETIME:
245 case NUMBERFORMAT_DATE:
246 case NUMBERFORMAT_TIME:
247 m_nLastDecimalDigits = 7;
248 break;
249 default:
250 m_nLastDecimalDigits = 0;
251 break;
253 bFallback = sal_False;
258 if ( bFallback )
260 getTypedControlWindow()->TreatAsNumber(sal_False);
261 getTypedControlWindow()->SetFormatter(NULL, sal_True);
262 getTypedControlWindow()->SetText(String());
263 m_nLastDecimalDigits = 0;
267 //========================================================================
268 //= OFileUrlControl
269 //========================================================================
270 //------------------------------------------------------------------
271 OFileUrlControl::OFileUrlControl( Window* pParent, WinBits nWinStyle )
272 :OFileUrlControl_Base( PropertyControlType::Unknown, pParent, nWinStyle | WB_DROPDOWN )
274 getTypedControlWindow()->SetDropDownLineCount( 10 );
275 getTypedControlWindow()->SetPlaceHolder( String( PcrRes( RID_EMBED_IMAGE_PLACEHOLDER ) ) ) ;
278 //------------------------------------------------------------------
279 OFileUrlControl::~OFileUrlControl()
283 //------------------------------------------------------------------
284 void SAL_CALL OFileUrlControl::setValue( const Any& _rValue ) throw (IllegalTypeException, RuntimeException)
286 ::rtl::OUString sURL;
287 if ( ( _rValue >>= sURL ) )
289 if ( sURL.indexOf( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "vnd.sun.star.GraphicObject:" ) ) ) == 0 )
290 getTypedControlWindow()->DisplayURL( getTypedControlWindow()->GetPlaceHolder() );
291 else
292 getTypedControlWindow()->DisplayURL( sURL );
294 else
295 getTypedControlWindow()->SetText( String() );
298 //------------------------------------------------------------------
299 Any SAL_CALL OFileUrlControl::getValue() throw (RuntimeException)
301 Any aPropValue;
302 if ( getTypedControlWindow()->GetText().Len() )
303 aPropValue <<= (::rtl::OUString)getTypedControlWindow()->GetURL();
304 return aPropValue;
307 //------------------------------------------------------------------
308 Type SAL_CALL OFileUrlControl::getValueType() throw (RuntimeException)
310 return ::getCppuType( static_cast< ::rtl::OUString* >( NULL ) );
313 //========================================================================
314 //= OTimeDurationControl
315 //========================================================================
316 //------------------------------------------------------------------
317 OTimeDurationControl::OTimeDurationControl( ::Window* pParent, WinBits nWinStyle )
318 :ONumericControl( pParent, nWinStyle )
320 getTypedControlWindow()->SetUnit( FUNIT_CUSTOM );
321 getTypedControlWindow()->SetCustomUnitText( String::CreateFromAscii( " ms" ) );
322 getTypedControlWindow()->SetCustomConvertHdl( LINK( this, OTimeDurationControl, OnCustomConvert ) );
325 //------------------------------------------------------------------
326 OTimeDurationControl::~OTimeDurationControl()
330 //------------------------------------------------------------------
331 ::sal_Int16 SAL_CALL OTimeDurationControl::getControlType() throw (::com::sun::star::uno::RuntimeException)
333 // don't use the base class'es method, it would claim we're a standard control, which
334 // we in fact aren't
335 return PropertyControlType::Unknown;
338 //------------------------------------------------------------------
339 IMPL_LINK( OTimeDurationControl, OnCustomConvert, MetricField*, /*pField*/ )
341 long nMultiplier = 1;
342 if ( getTypedControlWindow()->GetCurUnitText().EqualsIgnoreCaseAscii( "ms" ) )
343 nMultiplier = 1;
344 if ( getTypedControlWindow()->GetCurUnitText().EqualsIgnoreCaseAscii( "s" ) )
345 nMultiplier = 1000;
346 else if ( getTypedControlWindow()->GetCurUnitText().EqualsIgnoreCaseAscii( "m" ) )
347 nMultiplier = 1000 * 60;
348 else if ( getTypedControlWindow()->GetCurUnitText().EqualsIgnoreCaseAscii( "h" ) )
349 nMultiplier = 1000 * 60 * 60;
351 getTypedControlWindow()->SetValue( getTypedControlWindow()->GetLastValue() * nMultiplier );
353 return 0L;
356 //............................................................................
357 } // namespace pcr
358 //............................................................................