Update ooo320-m1
[ooovba.git] / reportdesign / source / ui / dlg / DateTime.cxx
blobdca80f3acf25dd6f8a6b801bc6cef3ebf8f39898
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: DateTime.cxx,v $
10 * $Revision: 1.5 $
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 ************************************************************************/
30 #include "precompiled_reportdesign.hxx"
31 #include "DateTime.hxx"
32 #ifndef RPTUI_DATETIME_HRC
33 #include "DateTime.hrc"
34 #endif
35 #include <com/sun/star/beans/XPropertySet.hpp>
36 #include <tools/debug.hxx>
37 #ifndef _RPTUI_DLGRESID_HRC
38 #include "RptResId.hrc"
39 #endif
40 #ifndef _RPTUI_SLOTID_HRC_
41 #include "rptui_slotid.hrc"
42 #endif
43 #ifndef _RPTUI_MODULE_HELPER_DBU_HXX_
44 #include "ModuleHelper.hxx"
45 #endif
46 #ifndef RTPUI_REPORTDESIGN_HELPID_HRC
47 #include "helpids.hrc"
48 #endif
49 #include <vcl/msgbox.hxx>
50 #ifndef _GLOBLMN_HRC
51 #include <svx/globlmn.hrc>
52 #endif
53 #ifndef _SBASLTID_HRC
54 #include <svx/svxids.hrc>
55 #endif
56 #include <connectivity/dbconversion.hxx>
57 #include <svtools/syslocale.hxx>
58 #ifndef RPTUI_TOOLS_HXX
59 #include "UITools.hxx"
60 #endif
61 #include "RptDef.hxx"
62 #ifndef REPORTDESIGN_SHARED_UISTRINGS_HRC
63 #include "uistrings.hrc"
64 #endif
65 #include "ReportController.hxx"
66 #include <com/sun/star/report/XFormattedField.hpp>
67 #include <com/sun/star/util/Time.hpp>
68 #include <com/sun/star/util/NumberFormat.hpp>
69 #include <com/sun/star/util/XNumberFormatPreviewer.hpp>
70 #include <com/sun/star/util/XNumberFormatTypes.hpp>
71 #include <com/sun/star/i18n/NumberFormatIndex.hpp>
72 #include <comphelper/numbers.hxx>
73 #include <algorithm>
75 namespace rptui
77 using namespace ::com::sun::star;
78 using namespace ::comphelper;
80 DBG_NAME( rpt_ODateTimeDialog )
81 //========================================================================
82 // class ODateTimeDialog
83 //========================================================================
84 ODateTimeDialog::ODateTimeDialog( Window* _pParent
85 ,const uno::Reference< report::XSection >& _xHoldAlive
86 ,OReportController* _pController)
87 : ModalDialog( _pParent, ModuleRes(RID_DATETIME_DLG) )
88 ,m_aDate(this, ModuleRes(CB_DATE ) )
89 ,m_aFTDateFormat(this, ModuleRes(FT_DATE_FORMAT ) )
90 ,m_aDateListBox(this, ModuleRes(LB_DATE_TYPE ) )
91 ,m_aFL0(this, ModuleRes(FL_SEPARATOR0 ) )
92 ,m_aTime(this, ModuleRes(CB_TIME ) )
93 ,m_aFTTimeFormat(this, ModuleRes(FT_TIME_FORMAT ) )
94 ,m_aTimeListBox(this, ModuleRes(LB_TIME_TYPE ) )
95 ,m_aFL1(this, ModuleRes(FL_SEPARATOR1) )
96 ,m_aPB_OK(this, ModuleRes(PB_OK))
97 ,m_aPB_CANCEL(this, ModuleRes(PB_CANCEL))
98 ,m_aPB_Help(this, ModuleRes(PB_HELP))
99 ,m_aDateControlling()
100 ,m_aTimeControlling()
101 ,m_pController(_pController)
102 ,m_xHoldAlive(_xHoldAlive)
104 DBG_CTOR( rpt_ODateTimeDialog,NULL);
108 SvtSysLocale aSysLocale;
109 m_nLocale = aSysLocale.GetLocaleData().getLocale();
110 // Fill listbox with all well known date types
111 InsertEntry(util::NumberFormat::DATE);
112 InsertEntry(util::NumberFormat::TIME);
114 catch(uno::Exception&)
118 m_aDateListBox.SetDropDownLineCount(20);
119 m_aDateListBox.SelectEntryPos(0);
121 m_aTimeListBox.SetDropDownLineCount(20);
122 m_aTimeListBox.SelectEntryPos(0);
124 // use nice enhancement, to toggle enable/disable if a checkbox is checked or not
125 m_aDateControlling.enableOnCheckMark( m_aDate, m_aFTDateFormat, m_aDateListBox);
126 m_aTimeControlling.enableOnCheckMark( m_aTime, m_aFTTimeFormat, m_aTimeListBox);
128 CheckBox* pCheckBoxes[] = { &m_aDate,&m_aTime};
129 for ( size_t i = 0 ; i < sizeof(pCheckBoxes)/sizeof(pCheckBoxes[0]); ++i)
130 pCheckBoxes[i]->SetClickHdl(LINK(this,ODateTimeDialog,CBClickHdl));
132 FreeResource();
134 // -----------------------------------------------------------------------------
135 void ODateTimeDialog::InsertEntry(sal_Int16 _nNumberFormatId)
137 const bool bTime = util::NumberFormat::TIME == _nNumberFormatId;
138 ListBox* pListBox = &m_aDateListBox;
139 if ( bTime )
140 pListBox = &m_aTimeListBox;
142 const uno::Reference< util::XNumberFormatter> xNumberFormatter = m_pController->getReportNumberFormatter();
143 const uno::Reference< util::XNumberFormats> xFormats = xNumberFormatter->getNumberFormatsSupplier()->getNumberFormats();
144 const uno::Sequence<sal_Int32> aFormatKeys = xFormats->queryKeys(_nNumberFormatId,m_nLocale,sal_True);
145 const sal_Int32* pIter = aFormatKeys.getConstArray();
146 const sal_Int32* pEnd = pIter + aFormatKeys.getLength();
147 for(;pIter != pEnd;++pIter)
149 const sal_Int16 nPos = pListBox->InsertEntry(getFormatStringByKey(*pIter,xFormats,bTime));
150 pListBox->SetEntryData(nPos, reinterpret_cast<void*>(*pIter));
153 //------------------------------------------------------------------------
154 ODateTimeDialog::~ODateTimeDialog()
156 DBG_DTOR( rpt_ODateTimeDialog,NULL);
158 // -----------------------------------------------------------------------------
159 short ODateTimeDialog::Execute()
161 DBG_CHKTHIS( rpt_ODateTimeDialog,NULL);
162 short nRet = ModalDialog::Execute();
163 if ( nRet == RET_OK && (m_aDate.IsChecked() || m_aTime.IsChecked()) )
167 sal_Int32 nLength = 0;
168 uno::Sequence<beans::PropertyValue> aValues( 6 );
169 aValues[nLength].Name = PROPERTY_SECTION;
170 aValues[nLength++].Value <<= m_xHoldAlive;
172 aValues[nLength].Name = PROPERTY_TIME_STATE;
173 aValues[nLength++].Value <<= m_aTime.IsChecked();
175 aValues[nLength].Name = PROPERTY_DATE_STATE;
176 aValues[nLength++].Value <<= m_aDate.IsChecked();
178 aValues[nLength].Name = PROPERTY_FORMATKEYDATE;
179 aValues[nLength++].Value <<= getFormatKey(sal_True);
181 aValues[nLength].Name = PROPERTY_FORMATKEYTIME;
182 aValues[nLength++].Value <<= getFormatKey(sal_False);
184 sal_Int32 nWidth = 0;
185 if ( m_aDate.IsChecked() )
187 String sDateFormat = m_aDateListBox.GetEntry( m_aDateListBox.GetSelectEntryPos() );
188 nWidth = LogicToLogic(PixelToLogic(Size(GetCtrlTextWidth(sDateFormat),0)).Width(),GetMapMode().GetMapUnit(),MAP_100TH_MM);
190 if ( m_aTime.IsChecked() )
192 String sDateFormat = m_aTimeListBox.GetEntry( m_aTimeListBox.GetSelectEntryPos() );
193 nWidth = ::std::max<sal_Int32>(LogicToLogic(PixelToLogic(Size(GetCtrlTextWidth(sDateFormat),0)).Width(),GetMapMode().GetMapUnit(),MAP_100TH_MM),nWidth);
196 if ( nWidth > 4000 )
198 aValues[nLength].Name = PROPERTY_WIDTH;
199 aValues[nLength++].Value <<= nWidth;
202 m_pController->executeChecked(SID_DATETIME,aValues);
204 catch(uno::Exception&)
206 nRet = RET_NO;
209 return nRet;
211 // -----------------------------------------------------------------------------
212 ::rtl::OUString ODateTimeDialog::getFormatStringByKey(::sal_Int32 _nNumberFormatKey,const uno::Reference< util::XNumberFormats>& _xFormats,bool _bTime)
214 uno::Reference< beans::XPropertySet> xFormSet = _xFormats->getByKey(_nNumberFormatKey);
215 OSL_ENSURE(xFormSet.is(),"XPropertySet is null!");
216 ::rtl::OUString sFormat;
217 xFormSet->getPropertyValue(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("FormatString"))) >>= sFormat;
219 double nValue = 0;
220 if ( _bTime )
222 Time aCurrentTime;
223 nValue = ::dbtools::DBTypeConversion::toDouble(::dbtools::DBTypeConversion::toTime(aCurrentTime.GetTime()));
225 else
227 Date aCurrentDate;
228 static ::com::sun::star::util::Date STANDARD_DB_DATE(30,12,1899);
229 nValue = ::dbtools::DBTypeConversion::toDouble(::dbtools::DBTypeConversion::toDate(static_cast<sal_Int32>(aCurrentDate.GetDate())),STANDARD_DB_DATE);
232 uno::Reference< util::XNumberFormatPreviewer> xPreViewer(m_pController->getReportNumberFormatter(),uno::UNO_QUERY);
233 OSL_ENSURE(xPreViewer.is(),"XNumberFormatPreviewer is null!");
234 return xPreViewer->convertNumberToPreviewString(sFormat,nValue,m_nLocale,sal_True);
236 // -----------------------------------------------------------------------------
237 IMPL_LINK( ODateTimeDialog, CBClickHdl, CheckBox*, _pBox )
239 (void)_pBox;
240 DBG_CHKTHIS( rpt_ODateTimeDialog,NULL);
242 if ( _pBox == &m_aDate || _pBox == &m_aTime)
244 sal_Bool bDate = m_aDate.IsChecked();
245 sal_Bool bTime = m_aTime.IsChecked();
246 if (!bDate && !bTime)
248 m_aPB_OK.Disable();
250 else
252 m_aPB_OK.Enable();
255 return 1L;
257 // -----------------------------------------------------------------------------
258 sal_Int32 ODateTimeDialog::getFormatKey(sal_Bool _bDate) const
260 DBG_CHKTHIS( rpt_ODateTimeDialog,NULL);
261 sal_Int32 nFormatKey;
262 if ( _bDate )
264 // nFormat = m_aDateF1.IsChecked() ? i18n::NumberFormatIndex::DATE_SYSTEM_LONG : (m_aDateF2.IsChecked() ? i18n::NumberFormatIndex::DATE_SYS_DMMMYYYY : i18n::NumberFormatIndex::DATE_SYSTEM_SHORT);
265 nFormatKey = static_cast<sal_Int32>(reinterpret_cast<sal_IntPtr>(m_aDateListBox.GetEntryData( m_aDateListBox.GetSelectEntryPos() )));
267 else
269 // nFormat = m_aTimeF1.IsChecked() ? i18n::NumberFormatIndex::TIME_HHMMSS : (m_aTimeF2.IsChecked() ? i18n::NumberFormatIndex::TIME_HHMMSSAMPM : i18n::NumberFormatIndex::TIME_HHMM);
270 nFormatKey = static_cast<sal_Int32>(reinterpret_cast<sal_IntPtr>(m_aTimeListBox.GetEntryData( m_aTimeListBox.GetSelectEntryPos() )));
272 return nFormatKey;
274 // =============================================================================
275 } // rptui
276 // =============================================================================