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