LibreOffice Module sw (master)  1
DropDownFormFieldButton.cxx
Go to the documentation of this file.
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 
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 
27 {
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 
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 
92  sw::mark::DropDownFieldmark& rFieldmark)
93  : FormFieldButton(pEditWin, rFieldmark)
94 {
95 }
96 
98 
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));
108  m_xTreeView->grab_focus();
109 }
110 
112 {
113  m_xTreeView.reset();
115 }
116 
117 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
virtual ~DropDownFormFieldButton() override
virtual void SetModified(bool=true) override
Definition: docsh2.cxx:1378
std::map< OUString, css::uno::Any > parameter_map_t
Definition: IMark.hxx:94
void InitDropdown()
Popup dialog for drop-down form field showing the list items of the field.
sw::mark::Fieldmark & m_rFieldmark
static const AllSettings & GetSettings()
virtual Size GetSizePixel() const
static weld::Builder * CreateBuilder(weld::Widget *pParent, const OUString &rUIFile, bool bMobile=false)
#define ODF_FORMDROPDOWN_RESULT
constexpr tools::Long Width() const
weld::Window * GetFrameWeld() const
virtual void LaunchPopup() override
std::unique_ptr< weld::Builder > m_xFieldPopupBuilder
This button is shown when the cursor is on a form field with drop-down capability.
virtual void DestroyPopup() override
std::unique_ptr< weld::Popover > m_xFieldPopup
int i
Window class for the Writer edit area, this is the one handling mouse and keyboard events and doing t...
Definition: edtwin.hxx:58
OUString SwResId(const char *pId)
Definition: swmodule.cxx:166
SwDocShell * GetDocShell()
Definition: view.cxx:1126
std::unique_ptr< weld::TreeView > m_xTreeView
DropDownFormFieldButton(SwEditWin *pEditWin, sw::mark::DropDownFieldmark &rFieldMark)
#define ODF_FORMDROPDOWN_LISTENTRY
tools::Long AdjustWidth(tools::Long n)
virtual void LaunchPopup()
virtual void DestroyPopup()
Fieldmark representing a drop-down form field.
Definition: bookmrk.hxx:284
Definition: view.hxx:144
virtual IFieldmark::parameter_map_t * GetParameters() override
Definition: bookmrk.hxx:212
This button is shown when the cursor is on a drop-down form field.