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 : bool 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( "" );
58 0 : if ( m_pHelper )
59 0 : m_pHelper->ModifiedHdl( this );
60 0 : return true;
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( true );
73 :
74 0 : SvNumberFormatter* pFormatter = pSupplier->GetNumberFormatter();
75 0 : SetFormatter( pFormatter, true );
76 0 : SetValue( 1234.56789 );
77 : }
78 : else
79 : {
80 0 : TreatAsNumber( false );
81 0 : SetFormatter( NULL, true );
82 0 : SetText( "" );
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, std::exception)
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( PcrRes( RID_STR_TEXT_FORMAT ).toString() );
111 : else
112 0 : getTypedControlWindow()->SetValue( pEntry ? getPreviewValue( *pEntry ) : 1234.56789 );
113 : }
114 : else
115 0 : getTypedControlWindow()->SetText( "" );
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, std::exception)
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, std::exception)
164 : {
165 0 : return ::getCppuType( static_cast< sal_Int32* >( NULL ) );
166 : }
167 :
168 :
169 : // class OFormattedNumericControl
170 :
171 :
172 0 : OFormattedNumericControl::OFormattedNumericControl( Window* pParent, WinBits nWinStyle )
173 0 : :OFormattedNumericControl_Base( PropertyControlType::Unknown, pParent, nWinStyle )
174 : {
175 0 : getTypedControlWindow()->TreatAsNumber(true);
176 :
177 0 : m_nLastDecimalDigits = getTypedControlWindow()->GetDecimalDigits();
178 0 : }
179 :
180 :
181 0 : OFormattedNumericControl::~OFormattedNumericControl()
182 : {
183 0 : }
184 :
185 :
186 0 : void SAL_CALL OFormattedNumericControl::setValue( const Any& _rValue ) throw (IllegalTypeException, RuntimeException, std::exception)
187 : {
188 0 : double nValue( 0 );
189 0 : if ( _rValue >>= nValue )
190 0 : getTypedControlWindow()->SetValue( nValue );
191 : else
192 0 : getTypedControlWindow()->SetText("");
193 0 : }
194 :
195 :
196 0 : Any SAL_CALL OFormattedNumericControl::getValue() throw (RuntimeException, std::exception)
197 : {
198 0 : Any aPropValue;
199 0 : if ( !getTypedControlWindow()->GetText().isEmpty() )
200 0 : aPropValue <<= (double)getTypedControlWindow()->GetValue();
201 0 : return aPropValue;
202 : }
203 :
204 :
205 0 : Type SAL_CALL OFormattedNumericControl::getValueType() throw (RuntimeException, std::exception)
206 : {
207 0 : return ::getCppuType( static_cast< double* >( NULL ) );
208 : }
209 :
210 :
211 0 : void OFormattedNumericControl::SetFormatDescription(const FormatDescription& rDesc)
212 : {
213 0 : sal_Bool bFallback = sal_True;
214 :
215 0 : if (rDesc.pSupplier)
216 : {
217 0 : getTypedControlWindow()->TreatAsNumber(true);
218 :
219 0 : SvNumberFormatter* pFormatter = rDesc.pSupplier->GetNumberFormatter();
220 0 : if (pFormatter != getTypedControlWindow()->GetFormatter())
221 0 : getTypedControlWindow()->SetFormatter(pFormatter, true);
222 0 : getTypedControlWindow()->SetFormatKey(rDesc.nKey);
223 :
224 0 : const SvNumberformat* pEntry = getTypedControlWindow()->GetFormatter()->GetEntry(getTypedControlWindow()->GetFormatKey());
225 : DBG_ASSERT( pEntry, "OFormattedNumericControl::SetFormatDescription: invalid format key!" );
226 0 : if ( pEntry )
227 : {
228 0 : switch (pEntry->GetType() & ~NUMBERFORMAT_DEFINED)
229 : {
230 : case NUMBERFORMAT_NUMBER:
231 : case NUMBERFORMAT_CURRENCY:
232 : case NUMBERFORMAT_SCIENTIFIC:
233 : case NUMBERFORMAT_FRACTION:
234 : case NUMBERFORMAT_PERCENT:
235 0 : m_nLastDecimalDigits = getTypedControlWindow()->GetDecimalDigits();
236 0 : break;
237 : case NUMBERFORMAT_DATETIME:
238 : case NUMBERFORMAT_DATE:
239 : case NUMBERFORMAT_TIME:
240 0 : m_nLastDecimalDigits = 7;
241 0 : break;
242 : default:
243 0 : m_nLastDecimalDigits = 0;
244 0 : break;
245 : }
246 0 : bFallback = sal_False;
247 : }
248 :
249 : }
250 :
251 0 : if ( bFallback )
252 : {
253 0 : getTypedControlWindow()->TreatAsNumber(false);
254 0 : getTypedControlWindow()->SetFormatter(NULL, true);
255 0 : getTypedControlWindow()->SetText("");
256 0 : m_nLastDecimalDigits = 0;
257 : }
258 0 : }
259 :
260 :
261 : //= OFileUrlControl
262 :
263 :
264 0 : OFileUrlControl::OFileUrlControl( Window* pParent, WinBits nWinStyle )
265 0 : :OFileUrlControl_Base( PropertyControlType::Unknown, pParent, nWinStyle | WB_DROPDOWN )
266 : {
267 0 : getTypedControlWindow()->SetDropDownLineCount( 10 );
268 0 : getTypedControlWindow()->SetPlaceHolder( PcrRes( RID_EMBED_IMAGE_PLACEHOLDER ).toString() ) ;
269 0 : }
270 :
271 :
272 0 : OFileUrlControl::~OFileUrlControl()
273 : {
274 0 : }
275 :
276 :
277 0 : void SAL_CALL OFileUrlControl::setValue( const Any& _rValue ) throw (IllegalTypeException, RuntimeException, std::exception)
278 : {
279 0 : OUString sURL;
280 0 : if ( ( _rValue >>= sURL ) )
281 : {
282 0 : if ( sURL.startsWith( "vnd.sun.star.GraphicObject:" ) )
283 0 : getTypedControlWindow()->DisplayURL( getTypedControlWindow()->GetPlaceHolder() );
284 : else
285 0 : getTypedControlWindow()->DisplayURL( sURL );
286 : }
287 : else
288 0 : getTypedControlWindow()->SetText( "" );
289 0 : }
290 :
291 :
292 0 : Any SAL_CALL OFileUrlControl::getValue() throw (RuntimeException, std::exception)
293 : {
294 0 : Any aPropValue;
295 0 : if ( !getTypedControlWindow()->GetText().isEmpty() )
296 0 : aPropValue <<= (OUString)getTypedControlWindow()->GetURL();
297 0 : return aPropValue;
298 : }
299 :
300 :
301 0 : Type SAL_CALL OFileUrlControl::getValueType() throw (RuntimeException, std::exception)
302 : {
303 0 : return ::getCppuType( static_cast< OUString* >( NULL ) );
304 : }
305 :
306 :
307 : //= OTimeDurationControl
308 :
309 :
310 0 : OTimeDurationControl::OTimeDurationControl( ::Window* pParent, WinBits nWinStyle )
311 0 : :ONumericControl( pParent, nWinStyle )
312 : {
313 0 : getTypedControlWindow()->SetUnit( FUNIT_CUSTOM );
314 0 : getTypedControlWindow()->SetCustomUnitText(OUString(" ms"));
315 0 : getTypedControlWindow()->SetCustomConvertHdl( LINK( this, OTimeDurationControl, OnCustomConvert ) );
316 0 : }
317 :
318 :
319 0 : OTimeDurationControl::~OTimeDurationControl()
320 : {
321 0 : }
322 :
323 :
324 0 : ::sal_Int16 SAL_CALL OTimeDurationControl::getControlType() throw (::com::sun::star::uno::RuntimeException)
325 : {
326 : // don't use the base class'es method, it would claim we're a standard control, which
327 : // we in fact aren't
328 0 : return PropertyControlType::Unknown;
329 : }
330 :
331 :
332 0 : IMPL_LINK( OTimeDurationControl, OnCustomConvert, MetricField*, /*pField*/ )
333 : {
334 0 : long nMultiplier = 1;
335 0 : if ( getTypedControlWindow()->GetCurUnitText().equalsIgnoreAsciiCase( "ms" ) )
336 0 : nMultiplier = 1;
337 0 : if ( getTypedControlWindow()->GetCurUnitText().equalsIgnoreAsciiCase( "s" ) )
338 0 : nMultiplier = 1000;
339 0 : else if ( getTypedControlWindow()->GetCurUnitText().equalsIgnoreAsciiCase( "m" ) )
340 0 : nMultiplier = 1000 * 60;
341 0 : else if ( getTypedControlWindow()->GetCurUnitText().equalsIgnoreAsciiCase( "h" ) )
342 0 : nMultiplier = 1000 * 60 * 60;
343 :
344 0 : getTypedControlWindow()->SetValue( getTypedControlWindow()->GetLastValue() * nMultiplier );
345 :
346 0 : return 0L;
347 : }
348 :
349 :
350 : } // namespace pcr
351 :
352 :
353 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|