1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
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/.
10 #include <DateFormFieldDialog.hxx>
12 #include <xmloff/odffields.hxx>
13 #include <svl/numformat.hxx>
14 #include <svl/zforlist.hxx>
15 #include <svl/zformat.hxx>
20 DateFormFieldDialog::DateFormFieldDialog(weld::Widget
* pParent
, sw::mark::DateFieldmark
* pDateField
,
22 : GenericDialogController(pParent
, u
"modules/swriter/ui/dateformfielddialog.ui"_ustr
,
23 u
"DateFormFieldDialog"_ustr
)
24 , m_pDateField(pDateField
)
25 , m_pNumberFormatter(rDoc
.GetNumberFormatter())
27 new SwNumFormatTreeView(m_xBuilder
->weld_tree_view(u
"date_formats_treeview"_ustr
)))
29 m_xFormatLB
->SetFormatType(SvNumFormatType::DATE
);
30 m_xFormatLB
->SetAutomaticLanguage(true);
31 m_xFormatLB
->SetShowLanguageControl(true);
32 m_xFormatLB
->SetOneArea(true);
34 // Set a default height
35 weld::TreeView
& rTreeView
= dynamic_cast<weld::TreeView
&>(m_xFormatLB
->get_widget());
36 rTreeView
.set_size_request(rTreeView
.get_preferred_size().Width(),
37 rTreeView
.get_height_rows(10));
41 DateFormFieldDialog::~DateFormFieldDialog() {}
43 void DateFormFieldDialog::Apply()
45 if (m_pDateField
== nullptr)
48 // Try to find out the current date value and replace the content
49 // with the right formatted date string
50 sw::mark::Fieldmark::parameter_map_t
* pParameters
= m_pDateField
->GetParameters();
51 const SvNumberformat
* pFormat
= m_pNumberFormatter
->GetEntry(m_xFormatLB
->GetFormat());
53 // Get date value first
54 std::pair
<bool, double> aResult
= m_pDateField
->GetCurrentDate();
56 // Then set the date format
57 (*pParameters
)[ODF_FORMDATE_DATEFORMAT
] <<= pFormat
->GetFormatstring();
58 (*pParameters
)[ODF_FORMDATE_DATEFORMAT_LANGUAGE
]
59 <<= LanguageTag(pFormat
->GetLanguage()).getBcp47();
61 // Update current date
64 m_pDateField
->SetCurrentDate(aResult
.second
);
68 (*pParameters
)[ODF_FORMDATE_CURRENTDATE
] <<= OUString();
72 void DateFormFieldDialog::InitControls()
74 if (m_pDateField
== nullptr)
77 sw::mark::Fieldmark::parameter_map_t
* pParameters
= m_pDateField
->GetParameters();
79 OUString sFormatString
;
80 auto pResult
= pParameters
->find(ODF_FORMDATE_DATEFORMAT
);
81 if (pResult
!= pParameters
->end())
83 pResult
->second
>>= sFormatString
;
87 pResult
= pParameters
->find(ODF_FORMDATE_DATEFORMAT_LANGUAGE
);
88 if (pResult
!= pParameters
->end())
90 pResult
->second
>>= sLang
;
93 if (sFormatString
.isEmpty() || sLang
.isEmpty())
96 LanguageType aLangType
= LanguageTag(sLang
).getLanguageType();
97 sal_uInt32 nFormat
= m_pNumberFormatter
->GetEntryKey(sFormatString
, aLangType
);
98 if (nFormat
== NUMBERFORMAT_ENTRY_NOT_FOUND
)
100 sal_Int32 nCheckPos
= 0;
101 SvNumFormatType nType
;
102 m_pNumberFormatter
->PutEntry(sFormatString
, nCheckPos
, nType
, nFormat
,
103 LanguageTag(sLang
).getLanguageType());
106 if (aLangType
== LANGUAGE_DONTKNOW
|| nFormat
== NUMBERFORMAT_ENTRY_NOT_FOUND
)
109 if (m_xFormatLB
->GetCurLanguage() == aLangType
)
111 m_xFormatLB
->SetAutomaticLanguage(true);
115 m_xFormatLB
->SetAutomaticLanguage(false);
116 m_xFormatLB
->SetLanguage(aLangType
);
118 // Change format and change back for regenerating the list
119 m_xFormatLB
->SetFormatType(SvNumFormatType::ALL
);
120 m_xFormatLB
->SetFormatType(SvNumFormatType::DATE
);
122 m_xFormatLB
->SetDefFormat(nFormat
);
127 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */