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