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