android: Update app-specific/MIME type icons
[LibreOffice.git] / sw / source / ui / fldui / DateFormFieldDialog.cxx
blob24461c47d6b28ad599777c1b4db64a498bd3d641
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
2 /*
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/.
8 */
10 #include <DateFormFieldDialog.hxx>
11 #include <IMark.hxx>
12 #include <xmloff/odffields.hxx>
13 #include <svl/numformat.hxx>
14 #include <svl/zforlist.hxx>
15 #include <svl/zformat.hxx>
16 #include <doc.hxx>
18 namespace sw
20 DateFormFieldDialog::DateFormFieldDialog(weld::Widget* pParent,
21 sw::mark::IDateFieldmark* pDateField, SwDoc& rDoc)
22 : GenericDialogController(pParent, "modules/swriter/ui/dateformfielddialog.ui",
23 "DateFormFieldDialog")
24 , m_pDateField(pDateField)
25 , m_pNumberFormatter(rDoc.GetNumberFormatter())
26 , m_xFormatLB(new SwNumFormatTreeView(m_xBuilder->weld_tree_view("date_formats_treeview")))
28 m_xFormatLB->SetFormatType(SvNumFormatType::DATE);
29 m_xFormatLB->SetAutomaticLanguage(true);
30 m_xFormatLB->SetShowLanguageControl(true);
31 m_xFormatLB->SetOneArea(true);
33 // Set a default height
34 weld::TreeView& rTreeView = dynamic_cast<weld::TreeView&>(m_xFormatLB->get_widget());
35 rTreeView.set_size_request(rTreeView.get_preferred_size().Width(),
36 rTreeView.get_height_rows(10));
37 InitControls();
40 DateFormFieldDialog::~DateFormFieldDialog() {}
42 void DateFormFieldDialog::Apply()
44 if (m_pDateField == nullptr)
45 return;
47 // Try to find out the current date value and replace the content
48 // with the right formatted date string
49 sw::mark::IFieldmark::parameter_map_t* pParameters = m_pDateField->GetParameters();
50 const SvNumberformat* pFormat = m_pNumberFormatter->GetEntry(m_xFormatLB->GetFormat());
52 // Get date value first
53 std::pair<bool, double> aResult = m_pDateField->GetCurrentDate();
55 // Then set the date format
56 (*pParameters)[ODF_FORMDATE_DATEFORMAT] <<= pFormat->GetFormatstring();
57 (*pParameters)[ODF_FORMDATE_DATEFORMAT_LANGUAGE]
58 <<= LanguageTag(pFormat->GetLanguage()).getBcp47();
60 // Update current date
61 if (aResult.first)
63 m_pDateField->SetCurrentDate(aResult.second);
65 else
67 (*pParameters)[ODF_FORMDATE_CURRENTDATE] <<= OUString();
71 void DateFormFieldDialog::InitControls()
73 if (m_pDateField == nullptr)
74 return;
76 sw::mark::IFieldmark::parameter_map_t* pParameters = m_pDateField->GetParameters();
78 OUString sFormatString;
79 auto pResult = pParameters->find(ODF_FORMDATE_DATEFORMAT);
80 if (pResult != pParameters->end())
82 pResult->second >>= sFormatString;
85 OUString sLang;
86 pResult = pParameters->find(ODF_FORMDATE_DATEFORMAT_LANGUAGE);
87 if (pResult != pParameters->end())
89 pResult->second >>= sLang;
92 if (sFormatString.isEmpty() || sLang.isEmpty())
93 return;
95 LanguageType aLangType = LanguageTag(sLang).getLanguageType();
96 sal_uInt32 nFormat = m_pNumberFormatter->GetEntryKey(sFormatString, aLangType);
97 if (nFormat == NUMBERFORMAT_ENTRY_NOT_FOUND)
99 sal_Int32 nCheckPos = 0;
100 SvNumFormatType nType;
101 m_pNumberFormatter->PutEntry(sFormatString, nCheckPos, nType, nFormat,
102 LanguageTag(sLang).getLanguageType());
105 if (aLangType == LANGUAGE_DONTKNOW || nFormat == NUMBERFORMAT_ENTRY_NOT_FOUND)
106 return;
108 if (m_xFormatLB->GetCurLanguage() == aLangType)
110 m_xFormatLB->SetAutomaticLanguage(true);
112 else
114 m_xFormatLB->SetAutomaticLanguage(false);
115 m_xFormatLB->SetLanguage(aLangType);
117 // Change format and change back for regenerating the list
118 m_xFormatLB->SetFormatType(SvNumFormatType::ALL);
119 m_xFormatLB->SetFormatType(SvNumFormatType::DATE);
121 m_xFormatLB->SetDefFormat(nFormat);
124 } // namespace sw
126 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */