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 <DropDownFormFieldDialog.hxx>
11 #include <vcl/event.hxx>
13 #include <xmloff/odffields.hxx>
14 #include <vcl/svapp.hxx>
15 #include <strings.hrc>
16 #include <swtypes.hxx>
20 DropDownFormFieldDialog::DropDownFormFieldDialog(weld::Widget
* pParent
,
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"))
33 m_xListItemEntry
->connect_key_press(LINK(this, DropDownFormFieldDialog
, KeyPressedHdl
));
34 m_xListItemEntry
->connect_changed(LINK(this, DropDownFormFieldDialog
, EntryChangedHdl
));
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
));
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
);
49 DropDownFormFieldDialog::~DropDownFormFieldDialog() {}
51 IMPL_LINK_NOARG(DropDownFormFieldDialog
, ListChangedHdl
, weld::TreeView
&, void) { UpdateButtons(); }
53 IMPL_LINK(DropDownFormFieldDialog
, KeyPressedHdl
, const KeyEvent
&, rEvent
, bool)
55 if (rEvent
.GetKeyCode().GetCode() == KEY_RETURN
&& !m_xListItemEntry
->get_text().isEmpty())
63 IMPL_LINK_NOARG(DropDownFormFieldDialog
, EntryChangedHdl
, weld::Entry
&, void) { UpdateButtons(); }
65 IMPL_LINK(DropDownFormFieldDialog
, ButtonPushedHdl
, weld::Button
&, rButton
, void)
67 if (&rButton
== m_xListAddButton
.get())
71 else if (m_xListItemsTreeView
->get_selected_index() != -1)
73 int nSelPos
= m_xListItemsTreeView
->get_selected_index();
74 if (&rButton
== m_xListRemoveButton
.get())
76 m_xListItemsTreeView
->remove(nSelPos
);
77 if (m_xListItemsTreeView
->n_children() > 0)
78 m_xListItemsTreeView
->select(nSelPos
? nSelPos
- 1 : 0);
80 else if (&rButton
== m_xListUpButton
.get())
82 const OUString sEntry
= m_xListItemsTreeView
->get_selected_text();
83 m_xListItemsTreeView
->remove(nSelPos
);
85 m_xListItemsTreeView
->insert_text(nSelPos
, sEntry
);
86 m_xListItemsTreeView
->select(nSelPos
);
88 else if (&rButton
== m_xListDownButton
.get())
90 const OUString sEntry
= m_xListItemsTreeView
->get_selected_text();
91 m_xListItemsTreeView
->remove(nSelPos
);
93 m_xListItemsTreeView
->insert_text(nSelPos
, sEntry
);
94 m_xListItemsTreeView
->select(nSelPos
);
96 m_bListHasChanged
= true;
101 void DropDownFormFieldDialog::InitControls()
103 if (m_pDropDownField
!= nullptr)
105 const mark::IFieldmark::parameter_map_t
* const pParameters
106 = m_pDropDownField
->GetParameters();
108 auto pListEntries
= pParameters
->find(ODF_FORMDROPDOWN_LISTENTRY
);
109 if (pListEntries
!= pParameters
->end())
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
);
116 // Select the current one
117 auto pResult
= pParameters
->find(ODF_FORMDROPDOWN_RESULT
);
118 if (pResult
!= pParameters
->end())
120 sal_Int32 nSelection
= -1;
121 pResult
->second
>>= nSelection
;
122 if (nSelection
>= 0 && nSelection
< vListEntries
.getLength())
123 m_xListItemsTreeView
->select_text(vListEntries
[nSelection
]);
130 void DropDownFormFieldDialog::AppendItemToList()
132 if (!m_xListAddButton
->get_sensitive())
135 if (m_xListItemsTreeView
->n_children() >= ODF_FORMDROPDOWN_ENTRY_COUNT_LIMIT
)
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
)));
144 const OUString
sEntry(m_xListItemEntry
->get_text());
145 if (!sEntry
.isEmpty())
147 m_xListItemsTreeView
->append_text(sEntry
);
148 m_xListItemsTreeView
->select_text(sEntry
);
149 m_bListHasChanged
= true;
152 m_xListItemEntry
->set_text(OUString());
153 m_xListItemEntry
->grab_focus();
158 void DropDownFormFieldDialog::UpdateButtons()
160 m_xListAddButton
->set_sensitive(!m_xListItemEntry
->get_text().isEmpty()
161 && m_xListItemsTreeView
->find_text(m_xListItemEntry
->get_text())
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);
171 void DropDownFormFieldDialog::Apply()
173 if (!(m_pDropDownField
!= nullptr && m_bListHasChanged
))
176 mark::IFieldmark::parameter_map_t
* pParameters
= m_pDropDownField
->GetParameters();
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
)
182 vListEntriesRange
[nIndex
] = m_xListItemsTreeView
->get_text(nIndex
);
185 if (m_xListItemsTreeView
->n_children() != 0)
187 (*pParameters
)[ODF_FORMDROPDOWN_LISTENTRY
] <<= vListEntries
;
191 pParameters
->erase(ODF_FORMDROPDOWN_LISTENTRY
);
194 // After editing the drop down field's list we don't specify the selected item
195 pParameters
->erase(ODF_FORMDROPDOWN_RESULT
);
200 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */