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 <DropDownFormFieldButton.hxx>
12 #include <bookmark.hxx>
13 #include <vcl/settings.hxx>
14 #include <vcl/svapp.hxx>
15 #include <xmloff/odffields.hxx>
19 #include <strings.hrc>
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.
26 void DropDownFormFieldButton::InitDropdown()
28 const sw::mark::IFieldmark::parameter_map_t
* const pParameters
= m_rFieldmark
.GetParameters();
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())
35 pListEntries
->second
>>= vListEntries
;
36 for (OUString
const& i
: std::as_const(vListEntries
))
37 m_xTreeView
->append_text(i
);
40 if (!vListEntries
.hasElements())
42 m_xTreeView
->append_text(SwResId(STR_DROP_DOWN_EMPTY_LIST
));
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())
50 sal_Int32 nSelection
= -1;
51 pResult
->second
>>= nSelection
;
52 m_xTreeView
->set_cursor(nSelection
);
53 m_xTreeView
->select(nSelection
);
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());
68 IMPL_LINK(DropDownFormFieldButton
, MyListBoxHandler
, weld::TreeView
&, rBox
, bool)
70 OUString sSelection
= rBox
.get_selected_text();
71 if (sSelection
== SwResId(STR_DROP_DOWN_EMPTY_LIST
))
73 m_xFieldPopup
->popdown();
77 sal_Int32 nSelection
= rBox
.get_selected_index();
80 (*m_rFieldmark
.GetParameters())[ODF_FORMDROPDOWN_RESULT
] <<= nSelection
;
81 m_rFieldmark
.Invalidate();
82 SwView
& rView
= static_cast<SwEditWin
*>(GetParent())->GetView();
83 rView
.GetDocShell()->SetModified();
86 m_xFieldPopup
->popdown();
91 DropDownFormFieldButton::DropDownFormFieldButton(SwEditWin
* pEditWin
,
92 sw::mark::DropDownFieldmark
& rFieldmark
)
93 : FormFieldButton(pEditWin
, rFieldmark
)
97 DropDownFormFieldButton::~DropDownFormFieldButton() { disposeOnce(); }
99 void DropDownFormFieldButton::LaunchPopup()
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");
106 m_xTreeView
->connect_row_activated(LINK(this, DropDownFormFieldButton
, MyListBoxHandler
));
107 FormFieldButton::LaunchPopup();
108 m_xTreeView
->grab_focus();
111 void DropDownFormFieldButton::DestroyPopup()
114 FormFieldButton::DestroyPopup();
117 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */