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
18namespace 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
47}
48
50
51IMPL_LINK_NOARG(DropDownFormFieldDialog, ListChangedHdl, weld::TreeView&, void) { UpdateButtons(); }
52
53IMPL_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
63IMPL_LINK_NOARG(DropDownFormFieldDialog, EntryChangedHdl, weld::Entry&, void) { UpdateButtons(); }
64
65IMPL_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
106 = m_pDropDownField->GetParameters();
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 (nSelection >= 0 && nSelection < vListEntries.getLength())
123 m_xListItemsTreeView->select_text(vListEntries[nSelection]);
124 }
125 }
126 }
128}
129
131{
132 if (!m_xListAddButton->get_sensitive())
133 return;
134
136 {
137 std::unique_ptr<weld::MessageDialog> xInfoBox(Application::CreateMessageDialog(
138 m_xDialog.get(), VclMessageType::Info, VclButtonsType::Ok,
139 SwResId(STR_DROP_DOWN_FIELD_ITEM_LIMIT)));
140 xInfoBox->run();
141 return;
142 }
143
144 const OUString sEntry(m_xListItemEntry->get_text());
145 if (!sEntry.isEmpty())
146 {
147 m_xListItemsTreeView->append_text(sEntry);
148 m_xListItemsTreeView->select_text(sEntry);
149 m_bListHasChanged = true;
150
151 // Clear entry
152 m_xListItemEntry->set_text(OUString());
153 m_xListItemEntry->grab_focus();
154 }
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 return;
175
176 mark::IFieldmark::parameter_map_t* pParameters = m_pDropDownField->GetParameters();
177
178 css::uno::Sequence<OUString> vListEntries(m_xListItemsTreeView->n_children());
179 auto vListEntriesRange = asNonConstRange(vListEntries);
180 for (int nIndex = 0; nIndex < m_xListItemsTreeView->n_children(); ++nIndex)
181 {
182 vListEntriesRange[nIndex] = m_xListItemsTreeView->get_text(nIndex);
183 }
184
185 if (m_xListItemsTreeView->n_children() != 0)
186 {
187 (*pParameters)[ODF_FORMDROPDOWN_LISTENTRY] <<= vListEntries;
188 }
189 else
190 {
191 pParameters->erase(ODF_FORMDROPDOWN_LISTENTRY);
192 }
193
194 // After editing the drop down field's list we don't specify the selected item
195 pParameters->erase(ODF_FORMDROPDOWN_RESULT);
196}
197
198} // namespace sw
199
200/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
static weld::MessageDialog * CreateMessageDialog(weld::Widget *pParent, VclMessageType eMessageType, VclButtonsType eButtonType, const OUString &rPrimaryMessage, const ILibreOfficeKitNotifier *pNotifier=nullptr)
std::unique_ptr< weld::Button > m_xListDownButton
std::unique_ptr< weld::TreeView > m_xListItemsTreeView
std::unique_ptr< weld::Button > m_xListUpButton
std::unique_ptr< weld::Button > m_xListRemoveButton
DropDownFormFieldDialog(weld::Widget *pParent, mark::IFieldmark *pDropDownField)
std::unique_ptr< weld::Entry > m_xListItemEntry
std::unique_ptr< weld::Button > m_xListAddButton
std::shared_ptr< weld::Dialog > m_xDialog
sal_Int32 nIndex
constexpr sal_uInt16 KEY_RETURN
Dialog to specify the properties of date form field.
IMPL_LINK_NOARG(DocumentTimerManager, DoIdleJobs, Timer *, void)
IMPL_LINK(DocumentStatisticsManager, DoIdleStatsUpdate, Timer *, pIdle, void)
constexpr OUStringLiteral ODF_FORMDROPDOWN_RESULT
#define ODF_FORMDROPDOWN_ENTRY_COUNT_LIMIT
constexpr OUStringLiteral ODF_FORMDROPDOWN_LISTENTRY
OUString SwResId(TranslateId aId)
Definition: swmodule.cxx:168