Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
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.
15 : : *
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).
21 : : *
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.
26 : : *
27 : : ************************************************************************/
28 : :
29 : : #include "usercontrol.hxx"
30 : :
31 : : #include <com/sun/star/inspection/PropertyControlType.hpp>
32 : : #include <svl/numuno.hxx>
33 : : #include <rtl/math.hxx>
34 : : #include <tools/debug.hxx>
35 : : #include <svl/zformat.hxx>
36 : : #include <connectivity/dbconversion.hxx>
37 : : #include <com/sun/star/util/Time.hpp>
38 : : #include "modulepcr.hxx"
39 : : #include "propresid.hrc"
40 : :
41 : : //............................................................................
42 : : namespace pcr
43 : : {
44 : : //............................................................................
45 : :
46 : : /** === begin UNO using === **/
47 : : using ::com::sun::star::uno::Any;
48 : : using ::com::sun::star::uno::Type;
49 : : using ::com::sun::star::beans::IllegalTypeException;
50 : : using ::com::sun::star::uno::RuntimeException;
51 : : /** === end UNO using === **/
52 : : namespace PropertyControlType = ::com::sun::star::inspection::PropertyControlType;
53 : :
54 : : //==================================================================
55 : : // NumberFormatSampleField
56 : : //==================================================================
57 : : //------------------------------------------------------------------
58 : 0 : long NumberFormatSampleField::PreNotify( NotifyEvent& rNEvt )
59 : : {
60 : : // want to handle two keys myself : Del/Backspace should empty the window (setting my prop to "standard" this way)
61 : 0 : if (EVENT_KEYINPUT == rNEvt.GetType())
62 : : {
63 : 0 : sal_uInt16 nKey = rNEvt.GetKeyEvent()->GetKeyCode().GetCode();
64 : :
65 : 0 : if ((KEY_DELETE == nKey) || (KEY_BACKSPACE == nKey))
66 : : {
67 : 0 : SetText( String() );
68 : 0 : if ( m_pHelper )
69 : 0 : m_pHelper->ModifiedHdl( this );
70 : 0 : return 1;
71 : : }
72 : : }
73 : :
74 : 0 : return BaseClass::PreNotify( rNEvt );
75 : : }
76 : :
77 : : //------------------------------------------------------------------
78 : 0 : void NumberFormatSampleField::SetFormatSupplier( const SvNumberFormatsSupplierObj* pSupplier )
79 : : {
80 : 0 : if ( pSupplier )
81 : : {
82 : 0 : TreatAsNumber( sal_True );
83 : :
84 : 0 : SvNumberFormatter* pFormatter = pSupplier->GetNumberFormatter();
85 : 0 : SetFormatter( pFormatter, sal_True );
86 : 0 : SetValue( 1234.56789 );
87 : : }
88 : : else
89 : : {
90 : 0 : TreatAsNumber( sal_False );
91 : 0 : SetFormatter( NULL, sal_True );
92 : 0 : SetText( String() );
93 : : }
94 : 0 : }
95 : :
96 : : //==================================================================
97 : : // OFormatSampleControl
98 : : //==================================================================
99 : : //------------------------------------------------------------------
100 : 0 : OFormatSampleControl::OFormatSampleControl( Window* pParent, WinBits nWinStyle )
101 : 0 : :OFormatSampleControl_Base( PropertyControlType::Unknown, pParent, nWinStyle )
102 : : {
103 : 0 : }
104 : :
105 : : //------------------------------------------------------------------
106 : 0 : void SAL_CALL OFormatSampleControl::setValue( const Any& _rValue ) throw (IllegalTypeException, RuntimeException)
107 : : {
108 : 0 : sal_Int32 nFormatKey = 0;
109 : 0 : if ( _rValue >>= nFormatKey )
110 : : {
111 : : // else set the new format key, the text will be reformatted
112 : 0 : getTypedControlWindow()->SetFormatKey( nFormatKey );
113 : :
114 : 0 : SvNumberFormatter* pNF = getTypedControlWindow()->GetFormatter();
115 : 0 : const SvNumberformat* pEntry = pNF->GetEntry( nFormatKey );
116 : : OSL_ENSURE( pEntry, "OFormatSampleControl::setValue: invalid format entry!" );
117 : :
118 : 0 : const bool bIsTextFormat = ( pEntry && pEntry->IsTextFormat() );
119 : 0 : if ( bIsTextFormat )
120 : 0 : getTypedControlWindow()->SetText( String( PcrRes( RID_STR_TEXT_FORMAT ) ) );
121 : : else
122 : 0 : getTypedControlWindow()->SetValue( pEntry ? getPreviewValue( *pEntry ) : 1234.56789 );
123 : : }
124 : : else
125 : 0 : getTypedControlWindow()->SetText( String() );
126 : 0 : }
127 : : //------------------------------------------------------------------
128 : 0 : double OFormatSampleControl::getPreviewValue( const SvNumberformat& i_rEntry )
129 : : {
130 : 0 : double nValue = 1234.56789;
131 : 0 : switch ( i_rEntry.GetType() & ~NUMBERFORMAT_DEFINED )
132 : : {
133 : : case NUMBERFORMAT_DATE:
134 : : {
135 : 0 : Date aCurrentDate( Date::SYSTEM );
136 : 0 : static ::com::sun::star::util::Date STANDARD_DB_DATE(30,12,1899);
137 : 0 : nValue = ::dbtools::DBTypeConversion::toDouble(::dbtools::DBTypeConversion::toDate(static_cast<sal_Int32>(aCurrentDate.GetDate())),STANDARD_DB_DATE);
138 : : }
139 : 0 : break;
140 : : case NUMBERFORMAT_TIME:
141 : : case NUMBERFORMAT_DATETIME:
142 : : {
143 : 0 : Time aCurrentTime( Time::SYSTEM );
144 : 0 : nValue = ::dbtools::DBTypeConversion::toDouble(::dbtools::DBTypeConversion::toTime(aCurrentTime.GetTime()));
145 : : }
146 : 0 : break;
147 : : default:
148 : 0 : break;
149 : : }
150 : 0 : return nValue;
151 : : }
152 : :
153 : : //------------------------------------------------------------------
154 : 0 : double OFormatSampleControl::getPreviewValue(SvNumberFormatter* _pNF,sal_Int32 _nFormatKey)
155 : : {
156 : 0 : const SvNumberformat* pEntry = _pNF->GetEntry(_nFormatKey);
157 : : DBG_ASSERT( pEntry, "OFormattedNumericControl::SetFormatDescription: invalid format key!" );
158 : 0 : double nValue = 1234.56789;
159 : 0 : if ( pEntry )
160 : 0 : nValue = getPreviewValue( *pEntry );
161 : 0 : return nValue;
162 : : }
163 : : //------------------------------------------------------------------
164 : 0 : Any SAL_CALL OFormatSampleControl::getValue() throw (RuntimeException)
165 : : {
166 : 0 : Any aPropValue;
167 : 0 : if ( getTypedControlWindow()->GetText().Len() )
168 : 0 : aPropValue <<= (sal_Int32)getTypedControlWindow()->GetFormatKey();
169 : 0 : return aPropValue;
170 : : }
171 : :
172 : : //------------------------------------------------------------------
173 : 0 : Type SAL_CALL OFormatSampleControl::getValueType() throw (RuntimeException)
174 : : {
175 : 0 : return ::getCppuType( static_cast< sal_Int32* >( NULL ) );
176 : : }
177 : :
178 : : //==================================================================
179 : : // class OFormattedNumericControl
180 : : //==================================================================
181 : : DBG_NAME(OFormattedNumericControl);
182 : : //------------------------------------------------------------------
183 : 0 : OFormattedNumericControl::OFormattedNumericControl( Window* pParent, WinBits nWinStyle )
184 : 0 : :OFormattedNumericControl_Base( PropertyControlType::Unknown, pParent, nWinStyle )
185 : : {
186 : : DBG_CTOR(OFormattedNumericControl,NULL);
187 : :
188 : 0 : getTypedControlWindow()->TreatAsNumber(sal_True);
189 : :
190 : 0 : m_nLastDecimalDigits = getTypedControlWindow()->GetDecimalDigits();
191 : 0 : }
192 : :
193 : : //------------------------------------------------------------------
194 : 0 : OFormattedNumericControl::~OFormattedNumericControl()
195 : : {
196 : : DBG_DTOR(OFormattedNumericControl,NULL);
197 : 0 : }
198 : :
199 : : //------------------------------------------------------------------
200 : 0 : void SAL_CALL OFormattedNumericControl::setValue( const Any& _rValue ) throw (IllegalTypeException, RuntimeException)
201 : : {
202 : 0 : double nValue( 0 );
203 : 0 : if ( _rValue >>= nValue )
204 : 0 : getTypedControlWindow()->SetValue( nValue );
205 : : else
206 : 0 : getTypedControlWindow()->SetText(String());
207 : 0 : }
208 : :
209 : : //------------------------------------------------------------------
210 : 0 : Any SAL_CALL OFormattedNumericControl::getValue() throw (RuntimeException)
211 : : {
212 : 0 : Any aPropValue;
213 : 0 : if ( getTypedControlWindow()->GetText().Len() )
214 : 0 : aPropValue <<= (double)getTypedControlWindow()->GetValue();
215 : 0 : return aPropValue;
216 : : }
217 : :
218 : : //------------------------------------------------------------------
219 : 0 : Type SAL_CALL OFormattedNumericControl::getValueType() throw (RuntimeException)
220 : : {
221 : 0 : return ::getCppuType( static_cast< double* >( NULL ) );
222 : : }
223 : :
224 : : //------------------------------------------------------------------
225 : 0 : void OFormattedNumericControl::SetFormatDescription(const FormatDescription& rDesc)
226 : : {
227 : 0 : sal_Bool bFallback = sal_True;
228 : :
229 : 0 : if (rDesc.pSupplier)
230 : : {
231 : 0 : getTypedControlWindow()->TreatAsNumber(sal_True);
232 : :
233 : 0 : SvNumberFormatter* pFormatter = rDesc.pSupplier->GetNumberFormatter();
234 : 0 : if (pFormatter != getTypedControlWindow()->GetFormatter())
235 : 0 : getTypedControlWindow()->SetFormatter(pFormatter, sal_True);
236 : 0 : getTypedControlWindow()->SetFormatKey(rDesc.nKey);
237 : :
238 : 0 : const SvNumberformat* pEntry = getTypedControlWindow()->GetFormatter()->GetEntry(getTypedControlWindow()->GetFormatKey());
239 : : DBG_ASSERT( pEntry, "OFormattedNumericControl::SetFormatDescription: invalid format key!" );
240 : 0 : if ( pEntry )
241 : : {
242 : 0 : switch (pEntry->GetType() & ~NUMBERFORMAT_DEFINED)
243 : : {
244 : : case NUMBERFORMAT_NUMBER:
245 : : case NUMBERFORMAT_CURRENCY:
246 : : case NUMBERFORMAT_SCIENTIFIC:
247 : : case NUMBERFORMAT_FRACTION:
248 : : case NUMBERFORMAT_PERCENT:
249 : 0 : m_nLastDecimalDigits = getTypedControlWindow()->GetDecimalDigits();
250 : 0 : break;
251 : : case NUMBERFORMAT_DATETIME:
252 : : case NUMBERFORMAT_DATE:
253 : : case NUMBERFORMAT_TIME:
254 : 0 : m_nLastDecimalDigits = 7;
255 : 0 : break;
256 : : default:
257 : 0 : m_nLastDecimalDigits = 0;
258 : 0 : break;
259 : : }
260 : 0 : bFallback = sal_False;
261 : : }
262 : :
263 : : }
264 : :
265 : 0 : if ( bFallback )
266 : : {
267 : 0 : getTypedControlWindow()->TreatAsNumber(sal_False);
268 : 0 : getTypedControlWindow()->SetFormatter(NULL, sal_True);
269 : 0 : getTypedControlWindow()->SetText(String());
270 : 0 : m_nLastDecimalDigits = 0;
271 : : }
272 : 0 : }
273 : :
274 : : //========================================================================
275 : : //= OFileUrlControl
276 : : //========================================================================
277 : : //------------------------------------------------------------------
278 : 0 : OFileUrlControl::OFileUrlControl( Window* pParent, WinBits nWinStyle )
279 : 0 : :OFileUrlControl_Base( PropertyControlType::Unknown, pParent, nWinStyle | WB_DROPDOWN )
280 : : {
281 : 0 : getTypedControlWindow()->SetDropDownLineCount( 10 );
282 : 0 : getTypedControlWindow()->SetPlaceHolder( String( PcrRes( RID_EMBED_IMAGE_PLACEHOLDER ) ) ) ;
283 : 0 : }
284 : :
285 : : //------------------------------------------------------------------
286 : 0 : OFileUrlControl::~OFileUrlControl()
287 : : {
288 : 0 : }
289 : :
290 : : //------------------------------------------------------------------
291 : 0 : void SAL_CALL OFileUrlControl::setValue( const Any& _rValue ) throw (IllegalTypeException, RuntimeException)
292 : : {
293 : 0 : ::rtl::OUString sURL;
294 : 0 : if ( ( _rValue >>= sURL ) )
295 : : {
296 : 0 : if ( sURL.indexOf( "vnd.sun.star.GraphicObject:" ) == 0 )
297 : 0 : getTypedControlWindow()->DisplayURL( getTypedControlWindow()->GetPlaceHolder() );
298 : : else
299 : 0 : getTypedControlWindow()->DisplayURL( sURL );
300 : : }
301 : : else
302 : 0 : getTypedControlWindow()->SetText( String() );
303 : 0 : }
304 : :
305 : : //------------------------------------------------------------------
306 : 0 : Any SAL_CALL OFileUrlControl::getValue() throw (RuntimeException)
307 : : {
308 : 0 : Any aPropValue;
309 : 0 : if ( getTypedControlWindow()->GetText().Len() )
310 : 0 : aPropValue <<= (::rtl::OUString)getTypedControlWindow()->GetURL();
311 : 0 : return aPropValue;
312 : : }
313 : :
314 : : //------------------------------------------------------------------
315 : 0 : Type SAL_CALL OFileUrlControl::getValueType() throw (RuntimeException)
316 : : {
317 : 0 : return ::getCppuType( static_cast< ::rtl::OUString* >( NULL ) );
318 : : }
319 : :
320 : : //========================================================================
321 : : //= OTimeDurationControl
322 : : //========================================================================
323 : : //------------------------------------------------------------------
324 : 0 : OTimeDurationControl::OTimeDurationControl( ::Window* pParent, WinBits nWinStyle )
325 : 0 : :ONumericControl( pParent, nWinStyle )
326 : : {
327 : 0 : getTypedControlWindow()->SetUnit( FUNIT_CUSTOM );
328 : 0 : getTypedControlWindow()->SetCustomUnitText(rtl::OUString(" ms"));
329 : 0 : getTypedControlWindow()->SetCustomConvertHdl( LINK( this, OTimeDurationControl, OnCustomConvert ) );
330 : 0 : }
331 : :
332 : : //------------------------------------------------------------------
333 : 0 : OTimeDurationControl::~OTimeDurationControl()
334 : : {
335 : 0 : }
336 : :
337 : : //------------------------------------------------------------------
338 : 0 : ::sal_Int16 SAL_CALL OTimeDurationControl::getControlType() throw (::com::sun::star::uno::RuntimeException)
339 : : {
340 : : // don't use the base class'es method, it would claim we're a standard control, which
341 : : // we in fact aren't
342 : 0 : return PropertyControlType::Unknown;
343 : : }
344 : :
345 : : //------------------------------------------------------------------
346 : 0 : IMPL_LINK( OTimeDurationControl, OnCustomConvert, MetricField*, /*pField*/ )
347 : : {
348 : 0 : long nMultiplier = 1;
349 : 0 : if ( getTypedControlWindow()->GetCurUnitText().EqualsIgnoreCaseAscii( "ms" ) )
350 : 0 : nMultiplier = 1;
351 : 0 : if ( getTypedControlWindow()->GetCurUnitText().EqualsIgnoreCaseAscii( "s" ) )
352 : 0 : nMultiplier = 1000;
353 : 0 : else if ( getTypedControlWindow()->GetCurUnitText().EqualsIgnoreCaseAscii( "m" ) )
354 : 0 : nMultiplier = 1000 * 60;
355 : 0 : else if ( getTypedControlWindow()->GetCurUnitText().EqualsIgnoreCaseAscii( "h" ) )
356 : 0 : nMultiplier = 1000 * 60 * 60;
357 : :
358 : 0 : getTypedControlWindow()->SetValue( getTypedControlWindow()->GetLastValue() * nMultiplier );
359 : :
360 : 0 : return 0L;
361 : : }
362 : :
363 : : //............................................................................
364 : : } // namespace pcr
365 : : //............................................................................
366 : :
367 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|