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