LibreOffice Module sw (master)  1
DropDownFormFieldDialog.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 <vcl/event.hxx>
12 #include <IMark.hxx>
13 #include <xmloff/odffields.hxx>
14 
15 namespace sw
16 {
18  mark::IFieldmark* pDropDownField)
19  : GenericDialogController(pParent, "modules/swriter/ui/dropdownformfielddialog.ui",
20  "DropDownFormFieldDialog")
21  , m_pDropDownField(pDropDownField)
22  , m_bListHasChanged(false)
23  , m_xListItemEntry(m_xBuilder->weld_entry("item_entry"))
24  , m_xListAddButton(m_xBuilder->weld_button("add_button"))
25  , m_xListItemsTreeView(m_xBuilder->weld_tree_view("items_treeview"))
26  , m_xListRemoveButton(m_xBuilder->weld_button("remove_button"))
27  , m_xListUpButton(m_xBuilder->weld_button("up_button"))
28  , m_xListDownButton(m_xBuilder->weld_button("down_button"))
29 {
30  m_xListItemEntry->connect_key_press(LINK(this, DropDownFormFieldDialog, KeyPressedHdl));
31  m_xListItemEntry->connect_changed(LINK(this, DropDownFormFieldDialog, EntryChangedHdl));
32 
33  m_xListItemsTreeView->set_size_request(m_xListItemEntry->get_preferred_size().Width(),
34  m_xListItemEntry->get_preferred_size().Height() * 5);
35  m_xListItemsTreeView->connect_changed(LINK(this, DropDownFormFieldDialog, ListChangedHdl));
36 
37  Link<weld::Button&, void> aPushButtonLink(LINK(this, DropDownFormFieldDialog, ButtonPushedHdl));
38  m_xListAddButton->connect_clicked(aPushButtonLink);
39  m_xListRemoveButton->connect_clicked(aPushButtonLink);
40  m_xListUpButton->connect_clicked(aPushButtonLink);
41  m_xListDownButton->connect_clicked(aPushButtonLink);
42 
43  InitControls();
44 }
45 
47 
48 IMPL_LINK_NOARG(DropDownFormFieldDialog, ListChangedHdl, weld::TreeView&, void) { UpdateButtons(); }
49 
50 IMPL_LINK(DropDownFormFieldDialog, KeyPressedHdl, const KeyEvent&, rEvent, bool)
51 {
52  if (rEvent.GetKeyCode().GetCode() == KEY_RETURN && !m_xListItemEntry->get_text().isEmpty())
53  {
54  AppendItemToList();
55  return true;
56  }
57  return false;
58 }
59 
60 IMPL_LINK_NOARG(DropDownFormFieldDialog, EntryChangedHdl, weld::Entry&, void) { UpdateButtons(); }
61 
62 IMPL_LINK(DropDownFormFieldDialog, ButtonPushedHdl, weld::Button&, rButton, void)
63 {
64  if (&rButton == m_xListAddButton.get())
65  {
66  AppendItemToList();
67  }
68  else if (m_xListItemsTreeView->get_selected_index() != -1)
69  {
70  int nSelPos = m_xListItemsTreeView->get_selected_index();
71  if (&rButton == m_xListRemoveButton.get())
72  {
73  m_xListItemsTreeView->remove(nSelPos);
74  if (m_xListItemsTreeView->n_children() > 0)
75  m_xListItemsTreeView->select(nSelPos ? nSelPos - 1 : 0);
76  }
77  else if (&rButton == m_xListUpButton.get())
78  {
79  const OUString sEntry = m_xListItemsTreeView->get_selected_text();
80  m_xListItemsTreeView->remove(nSelPos);
81  nSelPos--;
82  m_xListItemsTreeView->insert_text(nSelPos, sEntry);
83  m_xListItemsTreeView->select(nSelPos);
84  }
85  else if (&rButton == m_xListDownButton.get())
86  {
87  const OUString sEntry = m_xListItemsTreeView->get_selected_text();
88  m_xListItemsTreeView->remove(nSelPos);
89  nSelPos++;
90  m_xListItemsTreeView->insert_text(nSelPos, sEntry);
91  m_xListItemsTreeView->select(nSelPos);
92  }
93  m_bListHasChanged = true;
94  }
95  UpdateButtons();
96 }
97 
99 {
100  if (m_pDropDownField != nullptr)
101  {
102  const mark::IFieldmark::parameter_map_t* const pParameters
104 
105  auto pListEntries = pParameters->find(ODF_FORMDROPDOWN_LISTENTRY);
106  if (pListEntries != pParameters->end())
107  {
108  css::uno::Sequence<OUString> vListEntries;
109  pListEntries->second >>= vListEntries;
110  for (const OUString& rItem : std::as_const(vListEntries))
111  m_xListItemsTreeView->append_text(rItem);
112 
113  // Select the current one
114  auto pResult = pParameters->find(ODF_FORMDROPDOWN_RESULT);
115  if (pResult != pParameters->end())
116  {
117  sal_Int32 nSelection = -1;
118  pResult->second >>= nSelection;
119  if (vListEntries.getLength() > nSelection)
120  m_xListItemsTreeView->select_text(vListEntries[nSelection]);
121  }
122  }
123  }
124  UpdateButtons();
125 }
126 
128 {
129  if (m_xListAddButton->get_sensitive())
130  {
131  const OUString sEntry(m_xListItemEntry->get_text());
132  if (!sEntry.isEmpty())
133  {
134  m_xListItemsTreeView->append_text(sEntry);
135  m_xListItemsTreeView->select_text(sEntry);
136  m_bListHasChanged = true;
137 
138  // Clear entry
139  m_xListItemEntry->set_text(OUString());
140  m_xListItemEntry->grab_focus();
141  }
142  UpdateButtons();
143  }
144 }
145 
147 {
148  m_xListAddButton->set_sensitive(!m_xListItemEntry->get_text().isEmpty()
149  && m_xListItemsTreeView->find_text(m_xListItemEntry->get_text())
150  == -1);
151 
152  int nSelPos = m_xListItemsTreeView->get_selected_index();
153  m_xListRemoveButton->set_sensitive(nSelPos != -1);
154  m_xListUpButton->set_sensitive(nSelPos > 0);
155  m_xListDownButton->set_sensitive(nSelPos != -1
156  && nSelPos < m_xListItemsTreeView->n_children() - 1);
157 }
158 
160 {
161  if (m_pDropDownField != nullptr && m_bListHasChanged)
162  {
164 
165  css::uno::Sequence<OUString> vListEntries(m_xListItemsTreeView->n_children());
166  for (int nIndex = 0; nIndex < m_xListItemsTreeView->n_children(); ++nIndex)
167  {
168  vListEntries[nIndex] = m_xListItemsTreeView->get_text(nIndex);
169  }
170 
171  if (m_xListItemsTreeView->n_children() != 0)
172  {
173  (*pParameters)[ODF_FORMDROPDOWN_LISTENTRY] <<= vListEntries;
174  }
175  else
176  {
177  pParameters->erase(ODF_FORMDROPDOWN_LISTENTRY);
178  }
179 
180  // After editing the drop down field's list we don't specify the selected item
181  pParameters->erase(ODF_FORMDROPDOWN_RESULT);
182  }
183 }
184 
185 } // namespace sw
186 
187 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
std::unique_ptr< weld::Button > m_xListRemoveButton
std::map< OUString, css::uno::Any > parameter_map_t
Definition: IMark.hxx:94
std::unique_ptr< weld::TreeView > m_xListItemsTreeView
std::unique_ptr< weld::Entry > m_xListItemEntry
Dialog to specify the properties of date form field.
Definition: accfrmobj.cxx:40
#define ODF_FORMDROPDOWN_RESULT
IMPL_LINK(DocumentStatisticsManager, DoIdleStatsUpdate, Timer *, pIdle, void)
virtual parameter_map_t * GetParameters()=0
std::unique_ptr< weld::Button > m_xListUpButton
IMPL_LINK_NOARG(DocumentTimerManager, DoIdleJobs, Timer *, void)
DropDownFormFieldDialog(weld::Widget *pParent, mark::IFieldmark *pDropDownField)
std::unique_ptr< weld::Button > m_xListDownButton
#define KEY_RETURN
#define ODF_FORMDROPDOWN_LISTENTRY
std::unique_ptr< weld::Button > m_xListAddButton