1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_extensions.hxx"
26 #include "usercontrol.hxx"
27 
28 /** === begin UNO includes === **/
29 #include <com/sun/star/inspection/PropertyControlType.hpp>
30 #include <com/sun/star/inspection/PropertyControlType.hpp>
31 /** === end UNO includes === **/
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 	//------------------------------------------------------------------
PreNotify(NotifyEvent & rNEvt)58 	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 		if (EVENT_KEYINPUT == rNEvt.GetType())
62 		{
63 			sal_uInt16 nKey = rNEvt.GetKeyEvent()->GetKeyCode().GetCode();
64 
65 			if ((KEY_DELETE == nKey) || (KEY_BACKSPACE == nKey))
66 			{
67 				SetText( String() );
68                 if ( m_pHelper )
69 				    m_pHelper->ModifiedHdl( this );
70 				return 1;
71 			}
72 		}
73 
74         return BaseClass::PreNotify( rNEvt );
75 	}
76 
77 	//------------------------------------------------------------------
SetFormatSupplier(const SvNumberFormatsSupplierObj * pSupplier)78 	void NumberFormatSampleField::SetFormatSupplier( const SvNumberFormatsSupplierObj* pSupplier )
79 	{
80 		if ( pSupplier )
81 		{
82 			TreatAsNumber( sal_True );
83 
84 			SvNumberFormatter* pFormatter = pSupplier->GetNumberFormatter();
85 			SetFormatter( pFormatter, sal_True );
86 			SetValue( 1234.56789 );
87 		}
88 		else
89 		{
90 			TreatAsNumber( sal_False );
91 			SetFormatter( NULL, sal_True );
92 			SetText( String() );
93 		}
94 	}
95 
96 	//==================================================================
97 	// OFormatSampleControl
98 	//==================================================================
99 	//------------------------------------------------------------------
OFormatSampleControl(Window * pParent,WinBits nWinStyle)100 	OFormatSampleControl::OFormatSampleControl( Window* pParent, WinBits nWinStyle )
101         :OFormatSampleControl_Base( PropertyControlType::Unknown, pParent, nWinStyle )
102 	{
103 	}
104 
105 	//------------------------------------------------------------------
setValue(const Any & _rValue)106 	void SAL_CALL OFormatSampleControl::setValue( const Any& _rValue ) throw (IllegalTypeException, RuntimeException)
107 	{
108         sal_Int32 nFormatKey = 0;
109         if ( _rValue >>= nFormatKey )
110 		{
111 			// else set the new format key, the text will be reformatted
112             getTypedControlWindow()->SetFormatKey( nFormatKey );
113 
114             SvNumberFormatter* pNF = getTypedControlWindow()->GetFormatter();
115             const SvNumberformat* pEntry = pNF->GetEntry( nFormatKey );
116             OSL_ENSURE( pEntry, "OFormatSampleControl::setValue: invalid format entry!" );
117 
118             const bool bIsTextFormat = ( pEntry && pEntry->IsTextFormat() );
119             if ( bIsTextFormat )
120                 getTypedControlWindow()->SetText( String( PcrRes( RID_STR_TEXT_FORMAT ) ) );
121             else
122                 getTypedControlWindow()->SetValue( pEntry ? getPreviewValue( *pEntry ) : 1234.56789 );
123 		}
124         else
125 			getTypedControlWindow()->SetText( String() );
126 	}
127     //------------------------------------------------------------------
getPreviewValue(const SvNumberformat & i_rEntry)128     double OFormatSampleControl::getPreviewValue( const SvNumberformat& i_rEntry )
129     {
130         double nValue = 1234.56789;
131 		switch ( i_rEntry.GetType() & ~NUMBERFORMAT_DEFINED )
132 		{
133 			case NUMBERFORMAT_DATE:
134                 {
135                     Date aCurrentDate;
136 	                static ::com::sun::star::util::Date STANDARD_DB_DATE(30,12,1899);
137 	                nValue = ::dbtools::DBTypeConversion::toDouble(::dbtools::DBTypeConversion::toDate(static_cast<sal_Int32>(aCurrentDate.GetDate())),STANDARD_DB_DATE);
138                 }
139                 break;
140 			case NUMBERFORMAT_TIME:
141             case NUMBERFORMAT_DATETIME:
142                 {
143                     Time aCurrentTime;
144                     nValue = ::dbtools::DBTypeConversion::toDouble(::dbtools::DBTypeConversion::toTime(aCurrentTime.GetTime()));
145                 }
146 				break;
147 			default:
148 				break;
149 		}
150         return nValue;
151     }
152 
153     //------------------------------------------------------------------
getPreviewValue(SvNumberFormatter * _pNF,sal_Int32 _nFormatKey)154     double OFormatSampleControl::getPreviewValue(SvNumberFormatter* _pNF,sal_Int32 _nFormatKey)
155     {
156         const SvNumberformat* pEntry = _pNF->GetEntry(_nFormatKey);
157         DBG_ASSERT( pEntry, "OFormattedNumericControl::SetFormatDescription: invalid format key!" );
158         double nValue = 1234.56789;
159 		if ( pEntry )
160             nValue = getPreviewValue( *pEntry );
161         return nValue;
162     }
163 	//------------------------------------------------------------------
getValue()164 	Any SAL_CALL OFormatSampleControl::getValue() throw (RuntimeException)
165 	{
166         Any aPropValue;
167 		if ( getTypedControlWindow()->GetText().Len() )
168             aPropValue <<= (sal_Int32)getTypedControlWindow()->GetFormatKey();
169         return aPropValue;
170 	}
171 
172 	//------------------------------------------------------------------
getValueType()173     Type SAL_CALL OFormatSampleControl::getValueType() throw (RuntimeException)
174     {
175         return ::getCppuType( static_cast< sal_Int32* >( NULL ) );
176     }
177 
178 	//==================================================================
179 	// class OFormattedNumericControl
180 	//==================================================================
181 	DBG_NAME(OFormattedNumericControl);
182 	//------------------------------------------------------------------
OFormattedNumericControl(Window * pParent,WinBits nWinStyle)183 	OFormattedNumericControl::OFormattedNumericControl( Window* pParent, WinBits nWinStyle )
184         :OFormattedNumericControl_Base( PropertyControlType::Unknown, pParent, nWinStyle )
185 	{
186 		DBG_CTOR(OFormattedNumericControl,NULL);
187 
188 		getTypedControlWindow()->TreatAsNumber(sal_True);
189 
190 		m_nLastDecimalDigits = getTypedControlWindow()->GetDecimalDigits();
191 	}
192 
193 	//------------------------------------------------------------------
~OFormattedNumericControl()194 	OFormattedNumericControl::~OFormattedNumericControl()
195 	{
196 		DBG_DTOR(OFormattedNumericControl,NULL);
197 	}
198 
199 	//------------------------------------------------------------------
setValue(const Any & _rValue)200 	void SAL_CALL OFormattedNumericControl::setValue( const Any& _rValue ) throw (IllegalTypeException, RuntimeException)
201 	{
202         double nValue( 0 );
203         if ( _rValue >>= nValue )
204 			getTypedControlWindow()->SetValue( nValue );
205 		else
206 			getTypedControlWindow()->SetText(String());
207 	}
208 
209 	//------------------------------------------------------------------
getValue()210 	Any SAL_CALL OFormattedNumericControl::getValue() throw (RuntimeException)
211 	{
212         Any aPropValue;
213 		if ( getTypedControlWindow()->GetText().Len() )
214             aPropValue <<= (double)getTypedControlWindow()->GetValue();
215         return aPropValue;
216 	}
217 
218 	//------------------------------------------------------------------
getValueType()219     Type SAL_CALL OFormattedNumericControl::getValueType() throw (RuntimeException)
220     {
221         return ::getCppuType( static_cast< double* >( NULL ) );
222     }
223 
224 	//------------------------------------------------------------------
SetFormatDescription(const FormatDescription & rDesc)225 	void OFormattedNumericControl::SetFormatDescription(const FormatDescription& rDesc)
226 	{
227 		sal_Bool bFallback = sal_True;
228 
229 		if (rDesc.pSupplier)
230 		{
231 			getTypedControlWindow()->TreatAsNumber(sal_True);
232 
233 			SvNumberFormatter* pFormatter = rDesc.pSupplier->GetNumberFormatter();
234 			if (pFormatter != getTypedControlWindow()->GetFormatter())
235 				getTypedControlWindow()->SetFormatter(pFormatter, sal_True);
236 			getTypedControlWindow()->SetFormatKey(rDesc.nKey);
237 
238 			const SvNumberformat* pEntry = getTypedControlWindow()->GetFormatter()->GetEntry(getTypedControlWindow()->GetFormatKey());
239 			DBG_ASSERT( pEntry, "OFormattedNumericControl::SetFormatDescription: invalid format key!" );
240 			if ( pEntry )
241 			{
242 				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 						m_nLastDecimalDigits = getTypedControlWindow()->GetDecimalDigits();
250 						break;
251 					case NUMBERFORMAT_DATETIME:
252 					case NUMBERFORMAT_DATE:
253 					case NUMBERFORMAT_TIME:
254 						m_nLastDecimalDigits = 7;
255 						break;
256 					default:
257 						m_nLastDecimalDigits = 0;
258 						break;
259 				}
260 				bFallback = sal_False;
261 			}
262 
263 		}
264 
265 		if ( bFallback )
266 		{
267 			getTypedControlWindow()->TreatAsNumber(sal_False);
268 			getTypedControlWindow()->SetFormatter(NULL, sal_True);
269 			getTypedControlWindow()->SetText(String());
270 			m_nLastDecimalDigits = 0;
271 		}
272 	}
273 
274 	//========================================================================
275 	//= OFileUrlControl
276 	//========================================================================
277 	//------------------------------------------------------------------
OFileUrlControl(Window * pParent,WinBits nWinStyle)278     OFileUrlControl::OFileUrlControl( Window* pParent, WinBits nWinStyle )
279         :OFileUrlControl_Base( PropertyControlType::Unknown, pParent, nWinStyle | WB_DROPDOWN )
280     {
281         getTypedControlWindow()->SetDropDownLineCount( 10 );
282         getTypedControlWindow()->SetPlaceHolder( String( PcrRes( RID_EMBED_IMAGE_PLACEHOLDER ) ) ) ;
283     }
284 
285 	//------------------------------------------------------------------
~OFileUrlControl()286     OFileUrlControl::~OFileUrlControl()
287     {
288     }
289 
290 	//------------------------------------------------------------------
setValue(const Any & _rValue)291 	void SAL_CALL OFileUrlControl::setValue( const Any& _rValue ) throw (IllegalTypeException, RuntimeException)
292     {
293         ::rtl::OUString sURL;
294         if ( ( _rValue >>= sURL ) )
295         {
296             if ( sURL.indexOf( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "vnd.sun.star.GraphicObject:"  ) ) ) == 0  )
297                 getTypedControlWindow()->DisplayURL( getTypedControlWindow()->GetPlaceHolder() );
298             else
299                 getTypedControlWindow()->DisplayURL( sURL );
300         }
301         else
302             getTypedControlWindow()->SetText( String() );
303     }
304 
305 	//------------------------------------------------------------------
getValue()306 	Any SAL_CALL OFileUrlControl::getValue() throw (RuntimeException)
307     {
308         Any aPropValue;
309         if ( getTypedControlWindow()->GetText().Len() )
310             	aPropValue <<= (::rtl::OUString)getTypedControlWindow()->GetURL();
311         return aPropValue;
312     }
313 
314 	//------------------------------------------------------------------
getValueType()315     Type SAL_CALL OFileUrlControl::getValueType() throw (RuntimeException)
316     {
317         return ::getCppuType( static_cast< ::rtl::OUString* >( NULL ) );
318     }
319 
320 	//========================================================================
321 	//= OTimeDurationControl
322 	//========================================================================
323 	//------------------------------------------------------------------
OTimeDurationControl(::Window * pParent,WinBits nWinStyle)324     OTimeDurationControl::OTimeDurationControl( ::Window* pParent, WinBits nWinStyle )
325         :ONumericControl( pParent, nWinStyle )
326     {
327         getTypedControlWindow()->SetUnit( FUNIT_CUSTOM );
328         getTypedControlWindow()->SetCustomUnitText( String::CreateFromAscii( " ms" ) );
329         getTypedControlWindow()->SetCustomConvertHdl( LINK( this, OTimeDurationControl, OnCustomConvert ) );
330     }
331 
332 	//------------------------------------------------------------------
~OTimeDurationControl()333     OTimeDurationControl::~OTimeDurationControl()
334     {
335     }
336 
337 	//------------------------------------------------------------------
getControlType()338     ::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         return PropertyControlType::Unknown;
343     }
344 
345 	//------------------------------------------------------------------
346     IMPL_LINK( OTimeDurationControl, OnCustomConvert, MetricField*, /*pField*/ )
347     {
348         long nMultiplier = 1;
349         if ( getTypedControlWindow()->GetCurUnitText().EqualsIgnoreCaseAscii( "ms" ) )
350             nMultiplier = 1;
351         if ( getTypedControlWindow()->GetCurUnitText().EqualsIgnoreCaseAscii( "s" ) )
352             nMultiplier = 1000;
353         else if ( getTypedControlWindow()->GetCurUnitText().EqualsIgnoreCaseAscii( "m" ) )
354             nMultiplier = 1000 * 60;
355         else if ( getTypedControlWindow()->GetCurUnitText().EqualsIgnoreCaseAscii( "h" ) )
356             nMultiplier = 1000 * 60 * 60;
357 
358         getTypedControlWindow()->SetValue( getTypedControlWindow()->GetLastValue() * nMultiplier );
359 
360         return 0L;
361     }
362 
363 //............................................................................
364 } // namespace pcr
365 //............................................................................
366 
367