/src/libreoffice/svtools/source/dialogs/dlgname.cxx
Line | Count | Source |
1 | | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
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 | | * This file incorporates work covered by the following license notice: |
10 | | * |
11 | | * Licensed to the Apache Software Foundation (ASF) under one or more |
12 | | * contributor license agreements. See the NOTICE file distributed |
13 | | * with this work for additional information regarding copyright |
14 | | * ownership. The ASF licenses this file to you under the Apache |
15 | | * License, Version 2.0 (the "License"); you may not use this file |
16 | | * except in compliance with the License. You may obtain a copy of |
17 | | * the License at http://www.apache.org/licenses/LICENSE-2.0 . |
18 | | */ |
19 | | |
20 | | #include <comphelper/string.hxx> |
21 | | #include <svtools/dlgname.hxx> |
22 | | #include <vcl/weld/Builder.hxx> |
23 | | #include <vcl/weld/Dialog.hxx> |
24 | | |
25 | | /************************************************************************* |
26 | | |* |
27 | | |* Dialog for editing a name |
28 | | |* |
29 | | \************************************************************************/ |
30 | | |
31 | | SvxNameDialog::SvxNameDialog(weld::Window* pParent, const OUString& rName, const OUString& rDesc, |
32 | | const OUString& rTitle) |
33 | 0 | : GenericDialogController(pParent, u"cui/ui/namedialog.ui"_ustr, u"NameDialog"_ustr) |
34 | 0 | , m_xEdtName(m_xBuilder->weld_entry(u"name_entry"_ustr)) |
35 | 0 | , m_xFtDescription(m_xBuilder->weld_label(u"description_label"_ustr)) |
36 | 0 | , m_xBtnOK(m_xBuilder->weld_button(u"ok"_ustr)) |
37 | 0 | , m_aCheckName(nullptr) |
38 | 0 | { |
39 | 0 | m_xFtDescription->set_label(rDesc); |
40 | 0 | m_xEdtName->set_text(rName); |
41 | 0 | m_xEdtName->select_region(0, -1); |
42 | 0 | ModifyHdl(*m_xEdtName); |
43 | 0 | m_xEdtName->connect_changed(LINK(this, SvxNameDialog, ModifyHdl)); |
44 | 0 | if (!rTitle.isEmpty()) |
45 | 0 | set_title(rTitle); |
46 | 0 | } |
47 | | |
48 | | IMPL_LINK_NOARG(SvxNameDialog, ModifyHdl, weld::Entry&, void) |
49 | 0 | { |
50 | | // Do not allow empty names, unless custom CheckNameHdl is specified |
51 | 0 | bool bEnable; |
52 | 0 | if (m_aCheckNameHdl.IsSet()) |
53 | 0 | bEnable = m_aCheckNameHdl.Call(*this); |
54 | 0 | else if (m_aCheckName) |
55 | 0 | bEnable = m_aCheckName(m_xEdtName->get_text()); |
56 | 0 | else |
57 | 0 | bEnable = !m_xEdtName->get_text().isEmpty(); |
58 | 0 | m_xBtnOK->set_sensitive(bEnable); |
59 | | // tdf#129032: feedback on reason to disabled controls |
60 | 0 | m_xEdtName->set_message_type(bEnable ? weld::EntryMessageType::Normal |
61 | 0 | : weld::EntryMessageType::Error); |
62 | 0 | OUString rTip = u""_ustr; |
63 | 0 | if (!bEnable && m_aCheckNameTooltipHdl.IsSet()) |
64 | 0 | rTip = m_aCheckNameTooltipHdl.Call(*this); |
65 | 0 | m_xBtnOK->set_tooltip_text(rTip); |
66 | 0 | m_xEdtName->set_tooltip_text(rTip); |
67 | 0 | } |
68 | | |
69 | | SvxNumberDialog::SvxNumberDialog(weld::Window* pParent, const OUString& rDesc, sal_Int64 nValue, |
70 | | sal_Int64 nMin, sal_Int64 nMax) |
71 | 0 | : GenericDialogController(pParent, u"cui/ui/numberdialog.ui"_ustr, u"NumberDialog"_ustr) |
72 | 0 | , m_xEdtNumber(m_xBuilder->weld_spin_button(u"number_spinbtn"_ustr)) |
73 | 0 | , m_xFtDescription(m_xBuilder->weld_label(u"description_label"_ustr)) |
74 | 0 | { |
75 | 0 | m_xFtDescription->set_label(rDesc); |
76 | 0 | m_xEdtNumber->set_digits(0); |
77 | 0 | m_xEdtNumber->set_min(nMin); |
78 | 0 | m_xEdtNumber->set_max(nMax); |
79 | 0 | m_xEdtNumber->set_value(nValue); |
80 | 0 | } |
81 | | |
82 | | SvxDecimalNumberDialog::SvxDecimalNumberDialog(weld::Window* pParent, const OUString& rDesc, |
83 | | double fValue) |
84 | 0 | : GenericDialogController(pParent, u"cui/ui/numberdialog.ui"_ustr, u"NumberDialog"_ustr) |
85 | 0 | , m_xEdtNumber(m_xBuilder->weld_formatted_spin_button(u"number_spinbtn"_ustr)) |
86 | 0 | , m_xFtDescription(m_xBuilder->weld_label(u"description_label"_ustr)) |
87 | 0 | { |
88 | 0 | m_xFtDescription->set_label(rDesc); |
89 | 0 | m_xEdtNumber->GetFormatter().SetValue(fValue); |
90 | 0 | } |
91 | | |
92 | | // #i68101# |
93 | | // Dialog for editing Object Name |
94 | | // plus uniqueness-callback-linkHandler |
95 | | |
96 | | SvxObjectNameDialog::SvxObjectNameDialog(weld::Window* pParent, const OUString& rName) |
97 | 0 | : GenericDialogController(pParent, u"cui/ui/objectnamedialog.ui"_ustr, u"ObjectNameDialog"_ustr) |
98 | 0 | , m_xEdtName(m_xBuilder->weld_entry(u"object_name_entry"_ustr)) |
99 | 0 | , m_xBtnOK(m_xBuilder->weld_button(u"ok"_ustr)) |
100 | 0 | { |
101 | | // set name |
102 | 0 | m_xEdtName->set_text(rName); |
103 | 0 | m_xEdtName->select_region(0, -1); |
104 | | |
105 | | // activate name |
106 | 0 | ModifyHdl(*m_xEdtName); |
107 | 0 | m_xEdtName->connect_changed(LINK(this, SvxObjectNameDialog, ModifyHdl)); |
108 | 0 | } |
109 | | |
110 | | IMPL_LINK_NOARG(SvxObjectNameDialog, ModifyHdl, weld::Entry&, void) |
111 | 0 | { |
112 | 0 | if (aCheckNameHdl.IsSet()) |
113 | 0 | { |
114 | 0 | m_xBtnOK->set_sensitive(aCheckNameHdl.Call(*this)); |
115 | 0 | } |
116 | 0 | } |
117 | | |
118 | | // #i68101# |
119 | | // Dialog for editing Object Title and Description |
120 | | |
121 | | SvxObjectTitleDescDialog::SvxObjectTitleDescDialog(weld::Window* pParent, const OUString& rTitle, |
122 | | const OUString& rDescription, |
123 | | bool const isDecorative) |
124 | 0 | : GenericDialogController(pParent, u"cui/ui/objecttitledescdialog.ui"_ustr, |
125 | 0 | u"ObjectTitleDescDialog"_ustr) |
126 | 0 | , m_xTitleFT(m_xBuilder->weld_label(u"object_title_label"_ustr)) |
127 | 0 | , m_xEdtTitle(m_xBuilder->weld_entry(u"object_title_entry"_ustr)) |
128 | 0 | , m_xDescriptionFT(m_xBuilder->weld_label(u"desc_label"_ustr)) |
129 | 0 | , m_xEdtDescription(m_xBuilder->weld_text_view(u"desc_entry"_ustr)) |
130 | 0 | , m_xDecorativeCB(m_xBuilder->weld_check_button(u"decorative"_ustr)) |
131 | 0 | { |
132 | | //lock height to initial height |
133 | 0 | m_xEdtDescription->set_size_request(-1, m_xEdtDescription->get_text_height() * 5); |
134 | | // set title & desc |
135 | 0 | m_xEdtTitle->set_text(rTitle); |
136 | 0 | m_xEdtDescription->set_text(rDescription); |
137 | | |
138 | | // activate title |
139 | 0 | m_xEdtTitle->select_region(0, -1); |
140 | |
|
141 | 0 | m_xDecorativeCB->set_active(isDecorative); |
142 | 0 | m_xDecorativeCB->connect_toggled(LINK(this, SvxObjectTitleDescDialog, DecorativeHdl)); |
143 | 0 | DecorativeHdl(*m_xDecorativeCB); |
144 | 0 | } |
145 | | |
146 | | IMPL_LINK_NOARG(SvxObjectTitleDescDialog, DecorativeHdl, weld::Toggleable&, void) |
147 | 0 | { |
148 | 0 | bool const bEnable(!m_xDecorativeCB->get_active()); |
149 | 0 | m_xEdtTitle->set_sensitive(bEnable); |
150 | 0 | m_xTitleFT->set_sensitive(bEnable); |
151 | 0 | m_xEdtDescription->set_sensitive(bEnable); |
152 | 0 | m_xDescriptionFT->set_sensitive(bEnable); |
153 | 0 | } |
154 | | |
155 | | SvxListDialog::SvxListDialog(weld::Window* pParent) |
156 | 0 | : GenericDialogController(pParent, u"cui/ui/listdialog.ui"_ustr, u"ListDialog"_ustr) |
157 | 0 | , m_aMode(ListMode::String) |
158 | 0 | , m_xList(m_xBuilder->weld_tree_view(u"assignlist"_ustr)) |
159 | 0 | , m_xAddBtn(m_xBuilder->weld_button(u"addbtn"_ustr)) |
160 | 0 | , m_xRemoveBtn(m_xBuilder->weld_button(u"removebtn"_ustr)) |
161 | 0 | , m_xEditBtn(m_xBuilder->weld_button(u"editbtn"_ustr)) |
162 | 0 | { |
163 | 0 | m_xList->set_size_request(m_xList->get_approximate_digit_width() * 54, |
164 | 0 | m_xList->get_height_rows(6)); |
165 | 0 | m_xAddBtn->connect_clicked(LINK(this, SvxListDialog, AddHdl_Impl)); |
166 | 0 | m_xRemoveBtn->connect_clicked(LINK(this, SvxListDialog, RemoveHdl_Impl)); |
167 | 0 | m_xEditBtn->connect_clicked(LINK(this, SvxListDialog, EditHdl_Impl)); |
168 | 0 | m_xList->connect_selection_changed(LINK(this, SvxListDialog, SelectHdl_Impl)); |
169 | 0 | m_xList->connect_row_activated(LINK(this, SvxListDialog, DblClickHdl_Impl)); |
170 | |
|
171 | 0 | SelectionChanged(); |
172 | 0 | } |
173 | | |
174 | 0 | SvxListDialog::~SvxListDialog() {} |
175 | | |
176 | | IMPL_LINK_NOARG(SvxListDialog, AddHdl_Impl, weld::Button&, void) |
177 | 0 | { |
178 | 0 | SvxNameDialog aNameDlg(m_xDialog.get(), u""_ustr, u""_ustr); |
179 | |
|
180 | 0 | if (!aNameDlg.run()) |
181 | 0 | return; |
182 | 0 | OUString sNewText = comphelper::string::strip(aNameDlg.GetName(), ' '); |
183 | 0 | if (!sNewText.isEmpty()) |
184 | 0 | { |
185 | 0 | m_xList->insert_text(-1, sNewText); |
186 | 0 | m_xList->unselect_all(); |
187 | 0 | } |
188 | 0 | } |
189 | | |
190 | 0 | IMPL_LINK_NOARG(SvxListDialog, EditHdl_Impl, weld::Button&, void) { EditEntry(); } |
191 | | |
192 | 0 | IMPL_LINK_NOARG(SvxListDialog, SelectHdl_Impl, weld::TreeView&, void) { SelectionChanged(); } |
193 | | |
194 | | IMPL_LINK_NOARG(SvxListDialog, DblClickHdl_Impl, weld::TreeView&, bool) |
195 | 0 | { |
196 | 0 | EditEntry(); |
197 | 0 | return true; |
198 | 0 | } |
199 | | |
200 | | IMPL_LINK_NOARG(SvxListDialog, RemoveHdl_Impl, weld::Button&, void) |
201 | 0 | { |
202 | 0 | int nPos = m_xList->get_selected_index(); |
203 | 0 | if (nPos == -1) |
204 | 0 | return; |
205 | 0 | m_xList->remove(nPos); |
206 | 0 | int nCount = m_xList->n_children(); |
207 | 0 | if (nCount) |
208 | 0 | { |
209 | 0 | if (nPos >= nCount) |
210 | 0 | nPos = nCount - 1; |
211 | 0 | m_xList->select(nPos); |
212 | 0 | } |
213 | 0 | SelectionChanged(); |
214 | 0 | } |
215 | | |
216 | | void SvxListDialog::SelectionChanged() |
217 | 0 | { |
218 | 0 | bool bEnable = m_xList->get_selected_index() != -1; |
219 | 0 | m_xRemoveBtn->set_sensitive(bEnable); |
220 | 0 | m_xEditBtn->set_sensitive(bEnable); |
221 | 0 | } |
222 | | |
223 | | std::vector<OUString> SvxListDialog::GetEntries() |
224 | 0 | { |
225 | 0 | int nCount = m_xList->n_children(); |
226 | 0 | std::vector<OUString> aList; |
227 | 0 | aList.reserve(nCount); |
228 | 0 | for (int i = 0; i < nCount; ++i) |
229 | 0 | aList.push_back(m_xList->get_text(i)); |
230 | 0 | return aList; |
231 | 0 | } |
232 | | |
233 | | void SvxListDialog::SetEntries(std::vector<OUString> const& rEntries) |
234 | 0 | { |
235 | 0 | m_xList->clear(); |
236 | 0 | for (auto const& sEntry : rEntries) |
237 | 0 | { |
238 | 0 | m_xList->append_text(sEntry); |
239 | 0 | } |
240 | 0 | SelectionChanged(); |
241 | 0 | } |
242 | | |
243 | | void SvxListDialog::EditEntry() |
244 | 0 | { |
245 | 0 | int nPos = m_xList->get_selected_index(); |
246 | 0 | if (nPos == -1) |
247 | 0 | return; |
248 | | |
249 | 0 | OUString sOldText(m_xList->get_selected_text()); |
250 | 0 | OUString sNewText; |
251 | |
|
252 | 0 | if (m_aMode == ListMode::String) |
253 | 0 | { |
254 | 0 | SvxNameDialog aNameDlg(m_xDialog.get(), sOldText, u""_ustr); |
255 | 0 | if (!aNameDlg.run()) |
256 | 0 | return; |
257 | 0 | sNewText = comphelper::string::strip(aNameDlg.GetName(), ' '); |
258 | 0 | } |
259 | 0 | else if (m_aMode == ListMode::Int16 || m_aMode == ListMode::Int32 || m_aMode == ListMode::Int64) |
260 | 0 | { |
261 | 0 | sal_Int64 nMin = m_aMode == ListMode::Int16 |
262 | 0 | ? SAL_MIN_INT16 |
263 | 0 | : m_aMode == ListMode::Int32 ? SAL_MIN_INT32 : SAL_MIN_INT64; |
264 | 0 | sal_Int64 nMax = m_aMode == ListMode::Int16 |
265 | 0 | ? SAL_MAX_INT16 |
266 | 0 | : m_aMode == ListMode::Int32 ? SAL_MAX_INT32 : SAL_MAX_INT64; |
267 | 0 | SvxNumberDialog aNumberDlg(m_xDialog.get(), u""_ustr, sOldText.toInt64(), nMin, nMax); |
268 | 0 | if (!aNumberDlg.run()) |
269 | 0 | return; |
270 | 0 | sNewText = OUString::number(aNumberDlg.GetNumber()); |
271 | 0 | } |
272 | 0 | else if (m_aMode == ListMode::Double) |
273 | 0 | { |
274 | 0 | SvxDecimalNumberDialog aNumberDlg(m_xDialog.get(), u""_ustr, sOldText.toDouble()); |
275 | 0 | if (!aNumberDlg.run()) |
276 | 0 | return; |
277 | 0 | sNewText = OUString::number(aNumberDlg.GetNumber()); |
278 | 0 | } |
279 | | |
280 | 0 | if (!sNewText.isEmpty() && sNewText != sOldText) |
281 | 0 | { |
282 | 0 | m_xList->remove(nPos); |
283 | 0 | m_xList->insert_text(nPos, sNewText); |
284 | 0 | m_xList->select(nPos); |
285 | 0 | } |
286 | 0 | } |
287 | | |
288 | 0 | void SvxListDialog::SetMode(ListMode aMode) { m_aMode = aMode; }; |
289 | | |
290 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |