merge the formfield patch from ooo-build
[ooovba.git] / svtools / source / items1 / dtritem.cxx
blob02fadb47549e7308572235f15b4a75e402654280
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: dtritem.cxx,v $
10 * $Revision: 1.8 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_svtools.hxx"
33 #include <com/sun/star/uno/Any.hxx>
34 #include <com/sun/star/util/DateTimeRange.hpp>
36 #include <unotools/intlwrapper.hxx>
37 #include <comphelper/processfactory.hxx>
39 #include <tools/stream.hxx>
40 #include <tools/debug.hxx>
41 #include <tools/datetime.hxx>
42 #include <rtl/math.hxx>
44 #include <vcl/svapp.hxx>
46 #include <svtools/dtritem.hxx>
49 DBG_NAME( SfxDateTimeRangeItem )
50 TYPEINIT1( SfxDateTimeRangeItem, SfxPoolItem );
52 // -----------------------------------------------------------------------
54 SfxDateTimeRangeItem::SfxDateTimeRangeItem( USHORT which ) :
55 SfxPoolItem( which )
57 DBG_CTOR( SfxDateTimeRangeItem, 0 );
60 // -----------------------------------------------------------------------
62 SfxDateTimeRangeItem::SfxDateTimeRangeItem( USHORT which, const DateTime& rStartDT,
63 const DateTime& rEndDT ) :
64 SfxPoolItem( which ),
65 aStartDateTime( rStartDT ),
66 aEndDateTime( rEndDT )
68 DBG_CTOR( SfxDateTimeRangeItem, 0 );
71 // -----------------------------------------------------------------------
73 SfxDateTimeRangeItem::SfxDateTimeRangeItem( const SfxDateTimeRangeItem& rItem ) :
74 SfxPoolItem( rItem ),
75 aStartDateTime( rItem.aStartDateTime ),
76 aEndDateTime( rItem.aEndDateTime )
78 DBG_CTOR( SfxDateTimeRangeItem, 0 );
81 // -----------------------------------------------------------------------
83 int SfxDateTimeRangeItem::operator==( const SfxPoolItem& rItem ) const
85 DBG_CHKTHIS( SfxDateTimeRangeItem, 0 );
86 DBG_ASSERT( SfxPoolItem::operator==( rItem ), "unequal type" );
88 return ((SfxDateTimeRangeItem&)rItem ).aStartDateTime == aStartDateTime &&
89 ((SfxDateTimeRangeItem&)rItem ).aEndDateTime == aEndDateTime;
92 // -----------------------------------------------------------------------
94 int SfxDateTimeRangeItem::Compare( const SfxPoolItem& rItem ) const
96 DBG_CHKTHIS( SfxDateTimeRangeItem, 0 );
97 DBG_ASSERT( SfxPoolItem::operator==( rItem ), "unequal type" );
99 double fThisRange = aEndDateTime - aStartDateTime;
100 double fRange = ((const SfxDateTimeRangeItem&)rItem).aEndDateTime -
101 ((const SfxDateTimeRangeItem&)rItem).aStartDateTime;
103 if ( ::rtl::math::approxEqual( fRange, fThisRange ) )
104 return 0;
105 else if ( fRange < fThisRange )
106 return -1;
107 else
108 return 1;
111 // -----------------------------------------------------------------------
113 SfxPoolItem* SfxDateTimeRangeItem::Create( SvStream& rStream, USHORT ) const
115 DBG_CHKTHIS(SfxDateTimeRangeItem, 0);
117 sal_uInt32 nStartDate, nEndDate;
118 sal_Int32 nStartTime, nEndTime;
120 rStream >> nStartDate;
121 rStream >> nStartTime;
123 rStream >> nEndDate;
124 rStream >> nEndTime;
126 DateTime aStartDT, aEndDT;
128 aStartDT.SetDate( nStartDate );
129 aStartDT.SetTime( nStartTime );
131 aEndDT.SetDate( nEndDate );
132 aEndDT.SetTime( nEndTime );
134 return new SfxDateTimeRangeItem( Which(), aStartDT, aEndDT );
137 // -----------------------------------------------------------------------
139 SvStream& SfxDateTimeRangeItem::Store( SvStream& rStream, USHORT ) const
141 DBG_CHKTHIS( SfxDateTimeRangeItem, 0 );
143 rStream << aStartDateTime.GetDate();
144 rStream << aStartDateTime.GetTime();
146 rStream << aEndDateTime.GetDate();
147 rStream << aEndDateTime.GetTime();
149 return rStream;
152 // -----------------------------------------------------------------------
154 SfxPoolItem* SfxDateTimeRangeItem::Clone( SfxItemPool* ) const
156 DBG_CHKTHIS( SfxDateTimeRangeItem, 0 );
158 return new SfxDateTimeRangeItem( *this );
160 // -----------------------------------------------------------------------
162 SfxItemPresentation SfxDateTimeRangeItem::GetPresentation
164 SfxItemPresentation /*ePresentation*/,
165 SfxMapUnit /*eCoreMetric*/,
166 SfxMapUnit /*ePresentationMetric*/,
167 XubString& rText,
168 const IntlWrapper * pIntlWrapper
169 ) const
171 DBG_CHKTHIS(SfxDateTimeRangeItem, 0);
172 DateTime aRange(aEndDateTime - (const Time&)aStartDateTime);
173 if (pIntlWrapper)
175 rText = pIntlWrapper->getLocaleData()->getDate(aRange);
176 rText.AppendAscii(RTL_CONSTASCII_STRINGPARAM(", "));
177 rText += pIntlWrapper->getLocaleData()->getTime(aRange);
179 else
181 DBG_WARNING("SfxDateTimeRangeItem::GetPresentation():"
182 " Using default en_US IntlWrapper");
183 const IntlWrapper aIntlWrapper(
184 ::comphelper::getProcessServiceFactory(), LANGUAGE_ENGLISH_US );
185 rText = aIntlWrapper.getLocaleData()->getDate(aRange);
186 rText.AppendAscii(RTL_CONSTASCII_STRINGPARAM(", "));
187 rText += aIntlWrapper.getLocaleData()->getTime(aRange);
189 return SFX_ITEM_PRESENTATION_NAMELESS;
192 //----------------------------------------------------------------------------
193 // virtual
194 BOOL SfxDateTimeRangeItem::PutValue( const com::sun::star::uno::Any& rVal,BYTE )
196 com::sun::star::util::DateTimeRange aValue;
197 if ( rVal >>= aValue )
199 aStartDateTime = DateTime( Date( aValue.StartDay,
200 aValue.StartMonth,
201 aValue.StartYear ),
202 Time( aValue.StartHours,
203 aValue.StartMinutes,
204 aValue.StartSeconds,
205 aValue.StartHundredthSeconds ) );
206 aEndDateTime = DateTime( Date( aValue.EndDay,
207 aValue.EndMonth,
208 aValue.EndYear ),
209 Time( aValue.EndHours,
210 aValue.EndMinutes,
211 aValue.EndSeconds,
212 aValue.EndHundredthSeconds ) );
213 return TRUE;
216 DBG_ERROR( "SfxDateTimeRangeItem::PutValue - Wrong type!" );
217 return FALSE;
220 //----------------------------------------------------------------------------
221 // virtual
222 BOOL SfxDateTimeRangeItem::QueryValue( com::sun::star::uno::Any& rVal,
223 BYTE nMemberId ) const
225 nMemberId &= ~CONVERT_TWIPS;
226 com::sun::star::util::DateTimeRange aValue( aStartDateTime.Get100Sec(),
227 aStartDateTime.GetSec(),
228 aStartDateTime.GetMin(),
229 aStartDateTime.GetHour(),
230 aStartDateTime.GetDay(),
231 aStartDateTime.GetMonth(),
232 aStartDateTime.GetYear(),
233 aEndDateTime.Get100Sec(),
234 aEndDateTime.GetSec(),
235 aEndDateTime.GetMin(),
236 aEndDateTime.GetHour(),
237 aEndDateTime.GetDay(),
238 aEndDateTime.GetMonth(),
239 aEndDateTime.GetYear() );
240 rVal <<= aValue;
241 return TRUE;