1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
19 #include "DateTime.hxx"
20 #include <com/sun/star/beans/XPropertySet.hpp>
21 #include <tools/debug.hxx>
22 #include "RptResId.hrc"
23 #include "rptui_slotid.hrc"
24 #include "ModuleHelper.hxx"
25 #include "helpids.hrc"
26 #include <vcl/msgbox.hxx>
27 #include <connectivity/dbconversion.hxx>
28 #include <unotools/syslocale.hxx>
29 #include "UITools.hxx"
31 #include "uistrings.hrc"
32 #include "ReportController.hxx"
33 #include <com/sun/star/report/XFormattedField.hpp>
34 #include <com/sun/star/util/Time.hpp>
35 #include <com/sun/star/util/NumberFormat.hpp>
36 #include <com/sun/star/util/XNumberFormatPreviewer.hpp>
37 #include <com/sun/star/util/XNumberFormatTypes.hpp>
38 #include <com/sun/star/i18n/NumberFormatIndex.hpp>
39 #include <comphelper/numbers.hxx>
44 using namespace ::com::sun::star
;
45 using namespace ::comphelper
;
48 // class ODateTimeDialog
50 ODateTimeDialog::ODateTimeDialog( vcl::Window
* _pParent
51 ,const uno::Reference
< report::XSection
>& _xHoldAlive
52 ,OReportController
* _pController
)
53 : ModalDialog( _pParent
, "DateTimeDialog" , "modules/dbreport/ui/datetimedialog.ui" )
55 , m_aDateControlling()
56 , m_aTimeControlling()
57 , m_pController(_pController
)
58 , m_xHoldAlive(_xHoldAlive
)
61 get(m_pFTDateFormat
,"datelistbox_label");
62 get(m_pDateListBox
,"datelistbox");
64 get(m_pFTTimeFormat
,"timelistbox_label");
65 get(m_pTimeListBox
,"timelistbox");
71 SvtSysLocale aSysLocale
;
72 m_nLocale
= aSysLocale
.GetLanguageTag().getLocale();
73 // Fill listbox with all well known date types
74 InsertEntry(util::NumberFormat::DATE
);
75 InsertEntry(util::NumberFormat::TIME
);
77 catch (const uno::Exception
&)
81 m_pDateListBox
->SelectEntryPos(0);
83 m_pTimeListBox
->SelectEntryPos(0);
85 // use nice enhancement, to toggle enable/disable if a checkbox is checked or not
86 m_aDateControlling
.enableOnCheckMark( *m_pDate
, *m_pFTDateFormat
, *m_pDateListBox
);
87 m_aTimeControlling
.enableOnCheckMark( *m_pTime
, *m_pFTTimeFormat
, *m_pTimeListBox
);
89 CheckBox
* aCheckBoxes
[] = { m_pDate
,m_pTime
};
90 for ( size_t i
= 0 ; i
< SAL_N_ELEMENTS(aCheckBoxes
); ++i
)
91 aCheckBoxes
[i
]->SetClickHdl(LINK(this,ODateTimeDialog
,CBClickHdl
));
95 void ODateTimeDialog::InsertEntry(sal_Int16 _nNumberFormatId
)
97 const bool bTime
= util::NumberFormat::TIME
== _nNumberFormatId
;
98 ListBox
* pListBox
= m_pDateListBox
;
100 pListBox
= m_pTimeListBox
;
102 const uno::Reference
< util::XNumberFormatter
> xNumberFormatter
= m_pController
->getReportNumberFormatter();
103 const uno::Reference
< util::XNumberFormats
> xFormats
= xNumberFormatter
->getNumberFormatsSupplier()->getNumberFormats();
104 const uno::Sequence
<sal_Int32
> aFormatKeys
= xFormats
->queryKeys(_nNumberFormatId
,m_nLocale
,sal_True
);
105 const sal_Int32
* pIter
= aFormatKeys
.getConstArray();
106 const sal_Int32
* pEnd
= pIter
+ aFormatKeys
.getLength();
107 for(;pIter
!= pEnd
;++pIter
)
109 const sal_Int16 nPos
= pListBox
->InsertEntry(getFormatStringByKey(*pIter
,xFormats
,bTime
));
110 pListBox
->SetEntryData(nPos
, reinterpret_cast<void*>(*pIter
));
114 ODateTimeDialog::~ODateTimeDialog()
119 void ODateTimeDialog::dispose()
122 m_pFTDateFormat
.clear();
123 m_pDateListBox
.clear();
125 m_pFTTimeFormat
.clear();
126 m_pTimeListBox
.clear();
128 ModalDialog::dispose();
131 short ODateTimeDialog::Execute()
133 short nRet
= ModalDialog::Execute();
134 if ( nRet
== RET_OK
&& (m_pDate
->IsChecked() || m_pTime
->IsChecked()) )
138 sal_Int32 nLength
= 0;
139 uno::Sequence
<beans::PropertyValue
> aValues( 6 );
140 aValues
[nLength
].Name
= PROPERTY_SECTION
;
141 aValues
[nLength
++].Value
<<= m_xHoldAlive
;
143 aValues
[nLength
].Name
= PROPERTY_TIME_STATE
;
144 aValues
[nLength
++].Value
<<= m_pTime
->IsChecked();
146 aValues
[nLength
].Name
= PROPERTY_DATE_STATE
;
147 aValues
[nLength
++].Value
<<= m_pDate
->IsChecked();
149 aValues
[nLength
].Name
= PROPERTY_FORMATKEYDATE
;
150 aValues
[nLength
++].Value
<<= getFormatKey(true);
152 aValues
[nLength
].Name
= PROPERTY_FORMATKEYTIME
;
153 aValues
[nLength
++].Value
<<= getFormatKey(false);
155 sal_Int32 nWidth
= 0;
156 if ( m_pDate
->IsChecked() )
158 OUString sDateFormat
= m_pDateListBox
->GetSelectEntry();
159 nWidth
= LogicToLogic(PixelToLogic(Size(GetCtrlTextWidth(sDateFormat
),0)).Width(),GetMapMode().GetMapUnit(),MAP_100TH_MM
);
161 if ( m_pTime
->IsChecked() )
163 OUString sDateFormat
= m_pTimeListBox
->GetSelectEntry();
164 nWidth
= ::std::max
<sal_Int32
>(LogicToLogic(PixelToLogic(Size(GetCtrlTextWidth(sDateFormat
),0)).Width(),GetMapMode().GetMapUnit(),MAP_100TH_MM
),nWidth
);
169 aValues
[nLength
].Name
= PROPERTY_WIDTH
;
170 aValues
[nLength
++].Value
<<= nWidth
;
173 m_pController
->executeChecked(SID_DATETIME
,aValues
);
175 catch (const uno::Exception
&)
183 OUString
ODateTimeDialog::getFormatStringByKey(::sal_Int32 _nNumberFormatKey
,const uno::Reference
< util::XNumberFormats
>& _xFormats
,bool _bTime
)
185 uno::Reference
< beans::XPropertySet
> xFormSet
= _xFormats
->getByKey(_nNumberFormatKey
);
186 OSL_ENSURE(xFormSet
.is(),"XPropertySet is null!");
188 xFormSet
->getPropertyValue("FormatString") >>= sFormat
;
193 tools::Time
aCurrentTime( tools::Time::SYSTEM
);
194 nValue
= ::dbtools::DBTypeConversion::toDouble(::dbtools::DBTypeConversion::toTime(aCurrentTime
.GetTime()));
198 Date
aCurrentDate( Date::SYSTEM
);
199 static ::com::sun::star::util::Date
STANDARD_DB_DATE(30,12,1899);
200 nValue
= ::dbtools::DBTypeConversion::toDouble(::dbtools::DBTypeConversion::toDate(static_cast<sal_Int32
>(aCurrentDate
.GetDate())),STANDARD_DB_DATE
);
203 uno::Reference
< util::XNumberFormatPreviewer
> xPreviewer(m_pController
->getReportNumberFormatter(),uno::UNO_QUERY
);
204 OSL_ENSURE(xPreviewer
.is(),"XNumberFormatPreviewer is null!");
205 return xPreviewer
->convertNumberToPreviewString(sFormat
,nValue
,m_nLocale
,sal_True
);
208 IMPL_LINK( ODateTimeDialog
, CBClickHdl
, CheckBox
*, _pBox
)
212 if ( _pBox
== m_pDate
|| _pBox
== m_pTime
)
214 bool bDate
= m_pDate
->IsChecked();
215 bool bTime
= m_pTime
->IsChecked();
216 if (!bDate
&& !bTime
)
228 sal_Int32
ODateTimeDialog::getFormatKey(bool _bDate
) const
230 sal_Int32 nFormatKey
;
233 nFormatKey
= static_cast<sal_Int32
>(reinterpret_cast<sal_IntPtr
>(m_pDateListBox
->GetSelectEntryData()));
237 nFormatKey
= static_cast<sal_Int32
>(reinterpret_cast<sal_IntPtr
>(m_pTimeListBox
->GetSelectEntryData()));
245 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */