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