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