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