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.
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 
10 #include <DropDownFormFieldButton.hxx>
11 #include <edtwin.hxx>
12 #include <bookmrk.hxx>
13 #include <vcl/settings.hxx>
14 #include <vcl/svapp.hxx>
15 #include <xmloff/odffields.hxx>
16 #include <IMark.hxx>
17 #include <view.hxx>
18 #include <docsh.hxx>
19 #include <strings.hrc>
20 
21 /**
22  * Popup dialog for drop-down form field showing the list items of the field.
23  * The user can select the item using this popup while filling in a form.
24  */
25 
InitDropdown()26 void DropDownFormFieldButton::InitDropdown()
27 {
28     const sw::mark::IFieldmark::parameter_map_t* const pParameters = m_rFieldmark.GetParameters();
29 
30     sw::mark::IFieldmark::parameter_map_t::const_iterator pListEntries
31         = pParameters->find(ODF_FORMDROPDOWN_LISTENTRY);
32     css::uno::Sequence<OUString> vListEntries;
33     if (pListEntries != pParameters->end())
34     {
35         pListEntries->second >>= vListEntries;
36         for (OUString const& i : std::as_const(vListEntries))
37             m_xTreeView->append_text(i);
38     }
39 
40     if (!vListEntries.hasElements())
41     {
42         m_xTreeView->append_text(SwResId(STR_DROP_DOWN_EMPTY_LIST));
43     }
44 
45     // Select the current one
46     sw::mark::IFieldmark::parameter_map_t::const_iterator pResult
47         = pParameters->find(ODF_FORMDROPDOWN_RESULT);
48     if (pResult != pParameters->end())
49     {
50         sal_Int32 nSelection = -1;
51         pResult->second >>= nSelection;
52         m_xTreeView->set_cursor(nSelection);
53         m_xTreeView->select(nSelection);
54     }
55 
56     auto nHeight = m_xTreeView->get_height_rows(
57         std::min<int>(Application::GetSettings().GetStyleSettings().GetListBoxMaximumLineCount(),
58                       m_xTreeView->n_children()));
59     m_xTreeView->set_size_request(-1, nHeight);
60     Size lbSize(m_xTreeView->get_preferred_size());
61     lbSize.AdjustWidth(4);
62     lbSize.AdjustHeight(4);
63     auto nMinListWidth = GetSizePixel().Width();
64     lbSize.setWidth(std::max(lbSize.Width(), nMinListWidth));
65     m_xTreeView->set_size_request(lbSize.Width(), lbSize.Height());
66 }
67 
IMPL_LINK(DropDownFormFieldButton,MyListBoxHandler,weld::TreeView &,rBox,bool)68 IMPL_LINK(DropDownFormFieldButton, MyListBoxHandler, weld::TreeView&, rBox, bool)
69 {
70     OUString sSelection = rBox.get_selected_text();
71     if (sSelection == SwResId(STR_DROP_DOWN_EMPTY_LIST))
72     {
73         m_xFieldPopup->popdown();
74         return true;
75     }
76 
77     sal_Int32 nSelection = rBox.get_selected_index();
78     if (nSelection >= 0)
79     {
80         (*m_rFieldmark.GetParameters())[ODF_FORMDROPDOWN_RESULT] <<= nSelection;
81         m_rFieldmark.Invalidate();
82         SwView& rView = static_cast<SwEditWin*>(GetParent())->GetView();
83         rView.GetDocShell()->SetModified();
84     }
85 
86     m_xFieldPopup->popdown();
87 
88     return true;
89 }
90 
DropDownFormFieldButton(SwEditWin * pEditWin,sw::mark::DropDownFieldmark & rFieldmark)91 DropDownFormFieldButton::DropDownFormFieldButton(SwEditWin* pEditWin,
92                                                  sw::mark::DropDownFieldmark& rFieldmark)
93     : FormFieldButton(pEditWin, rFieldmark)
94 {
95 }
96 
~DropDownFormFieldButton()97 DropDownFormFieldButton::~DropDownFormFieldButton() { disposeOnce(); }
98 
LaunchPopup()99 void DropDownFormFieldButton::LaunchPopup()
100 {
101     m_xFieldPopupBuilder.reset(
102         Application::CreateBuilder(GetFrameWeld(), "modules/swriter/ui/formdropdown.ui"));
103     m_xFieldPopup = m_xFieldPopupBuilder->weld_popover("FormDropDown");
104     m_xTreeView = m_xFieldPopupBuilder->weld_tree_view("list");
105     InitDropdown();
106     m_xTreeView->connect_row_activated(LINK(this, DropDownFormFieldButton, MyListBoxHandler));
107     FormFieldButton::LaunchPopup();
108     m_xTreeView->grab_focus();
109 }
110 
DestroyPopup()111 void DropDownFormFieldButton::DestroyPopup()
112 {
113     m_xTreeView.reset();
114     FormFieldButton::DestroyPopup();
115 }
116 
117 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
118