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