Add Marathi autocorrect
[LibreOffice.git] / sw / source / ui / table / tautofmt.cxx
blob3d4a9c38abe8da90f6429d6966ca8d4ac57034b5
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <memory>
21 #include <vcl/svapp.hxx>
22 #include <vcl/weld.hxx>
23 #include <sal/log.hxx>
24 #include <strings.hrc>
25 #include <shellres.hxx>
26 #include <tautofmt.hxx>
28 namespace
30 class SwStringInputDlg : public SfxDialogController
32 private:
33 std::unique_ptr<weld::Label> m_xLabel;
34 std::unique_ptr<weld::Entry> m_xEdInput; // Edit obtains the focus.
36 public:
37 SwStringInputDlg(weld::Window* pParent, const OUString& rTitle, const OUString& rEditTitle,
38 const OUString& rDefault)
39 : SfxDialogController(pParent, u"modules/swriter/ui/stringinput.ui"_ustr, u"StringInputDialog"_ustr)
40 , m_xLabel(m_xBuilder->weld_label(u"name"_ustr))
41 , m_xEdInput(m_xBuilder->weld_entry(u"edit"_ustr))
43 m_xLabel->set_label(rEditTitle);
44 m_xDialog->set_title(rTitle);
45 m_xEdInput->set_text(rDefault);
46 m_xEdInput->select_region(0, -1);
49 OUString GetInputString() const { return m_xEdInput->get_text(); }
53 // AutoFormat-Dialogue:
54 SwAutoFormatDlg::SwAutoFormatDlg(weld::Window* pParent, SwWrtShell* pWrtShell, bool bAutoFormat,
55 const SwTableAutoFormat* pSelFormat)
56 : SfxDialogController(pParent, u"modules/swriter/ui/autoformattable.ui"_ustr, u"AutoFormatTableDialog"_ustr)
57 , m_aStrTitle(SwResId(STR_ADD_AUTOFORMAT_TITLE))
58 , m_aStrLabel(SwResId(STR_ADD_AUTOFORMAT_LABEL))
59 , m_aStrClose(SwResId(STR_BTN_AUTOFORMAT_CLOSE))
60 , m_aStrDelTitle(SwResId(STR_DEL_AUTOFORMAT_TITLE))
61 , m_aStrDelMsg(SwResId(STR_DEL_AUTOFORMAT_MSG))
62 , m_aStrRenameTitle(SwResId(STR_RENAME_AUTOFORMAT_TITLE))
63 , m_aStrInvalidFormat(SwResId(STR_INVALID_AUTOFORMAT_NAME))
64 , m_pShell(pWrtShell)
65 , m_nIndex(0)
66 , m_nDfltStylePos(0)
67 , m_bCoreDataChanged(false)
68 , m_bSetAutoFormat(bAutoFormat)
69 , m_xTableTable(new SwTableAutoFormatTable)
70 , m_xLbFormat(m_xBuilder->weld_tree_view(u"formatlb"_ustr))
71 , m_xBtnNumFormat(m_xBuilder->weld_check_button(u"numformatcb"_ustr))
72 , m_xBtnBorder(m_xBuilder->weld_check_button(u"bordercb"_ustr))
73 , m_xBtnFont(m_xBuilder->weld_check_button(u"fontcb"_ustr))
74 , m_xBtnPattern(m_xBuilder->weld_check_button(u"patterncb"_ustr))
75 , m_xBtnAlignment(m_xBuilder->weld_check_button(u"alignmentcb"_ustr))
76 , m_xBtnCancel(m_xBuilder->weld_button(u"cancel"_ustr))
77 , m_xBtnAdd(m_xBuilder->weld_button(u"add"_ustr))
78 , m_xBtnRemove(m_xBuilder->weld_button(u"remove"_ustr))
79 , m_xBtnRename(m_xBuilder->weld_button(u"rename"_ustr))
80 , m_xWndPreview(new weld::CustomWeld(*m_xBuilder, u"preview"_ustr, m_aWndPreview))
82 m_aWndPreview.DetectRTL(pWrtShell);
83 m_xTableTable->Load();
85 const int nWidth = m_xLbFormat->get_approximate_digit_width() * 32;
86 const int nHeight = m_xLbFormat->get_height_rows(8);
87 m_xLbFormat->set_size_request(nWidth, nHeight);
88 m_xWndPreview->set_size_request(nWidth, nHeight);
90 Init(pSelFormat);
93 SwAutoFormatDlg::~SwAutoFormatDlg()
95 try
97 if (m_bCoreDataChanged)
98 m_xTableTable->Save();
100 catch (...)
103 m_xTableTable.reset();
106 void SwAutoFormatDlg::Init(const SwTableAutoFormat* pSelFormat)
108 Link<weld::Toggleable&, void> aLk(LINK(this, SwAutoFormatDlg, CheckHdl));
109 m_xBtnBorder->connect_toggled(aLk);
110 m_xBtnFont->connect_toggled(aLk);
111 m_xBtnPattern->connect_toggled(aLk);
112 m_xBtnAlignment->connect_toggled(aLk);
113 m_xBtnNumFormat->connect_toggled(aLk);
115 m_xBtnAdd->connect_clicked(LINK(this, SwAutoFormatDlg, AddHdl));
116 m_xBtnRemove->connect_clicked(LINK(this, SwAutoFormatDlg, RemoveHdl));
117 m_xBtnRename->connect_clicked(LINK(this, SwAutoFormatDlg, RenameHdl));
118 m_xLbFormat->connect_selection_changed(LINK(this, SwAutoFormatDlg, SelFormatHdl));
120 m_xBtnAdd->set_sensitive(m_bSetAutoFormat);
122 // Expand list with the entry "- none -".
123 m_xLbFormat->append_text(SwViewShell::GetShellRes()->aStrNone);
124 m_nDfltStylePos = 1;
125 m_nIndex = 255;
127 for (sal_uInt8 i = 0, nCount = static_cast<sal_uInt8>(m_xTableTable->size()); i < nCount; i++)
129 SwTableAutoFormat const& rFormat = (*m_xTableTable)[i];
130 m_xLbFormat->append_text(rFormat.GetName());
131 if (pSelFormat && rFormat.GetName() == pSelFormat->GetName())
132 m_nIndex = i;
135 m_xLbFormat->select(255 != m_nIndex ? (m_nDfltStylePos + m_nIndex) : 0);
136 SelFormatHdl(*m_xLbFormat);
139 void SwAutoFormatDlg::UpdateChecks(const SwTableAutoFormat& rFormat, bool bEnable)
141 m_xBtnNumFormat->set_sensitive(bEnable);
142 m_xBtnNumFormat->set_active(rFormat.IsValueFormat());
144 m_xBtnBorder->set_sensitive(bEnable);
145 m_xBtnBorder->set_active(rFormat.IsFrame());
147 m_xBtnFont->set_sensitive(bEnable);
148 m_xBtnFont->set_active(rFormat.IsFont());
150 m_xBtnPattern->set_sensitive(bEnable);
151 m_xBtnPattern->set_active(rFormat.IsBackground());
153 m_xBtnAlignment->set_sensitive(bEnable);
154 m_xBtnAlignment->set_active(rFormat.IsJustify());
157 std::unique_ptr<SwTableAutoFormat> SwAutoFormatDlg::FillAutoFormatOfIndex() const
159 if (255 != m_nIndex)
161 return std::make_unique<SwTableAutoFormat>((*m_xTableTable)[m_nIndex]);
164 return nullptr;
167 // Handler:
168 IMPL_LINK(SwAutoFormatDlg, CheckHdl, weld::Toggleable&, rBtn, void)
170 if (m_nIndex == 255)
171 return;
173 SwTableAutoFormat& rData = (*m_xTableTable)[m_nIndex];
174 bool bCheck = rBtn.get_active(), bDataChgd = true;
176 if (&rBtn == m_xBtnNumFormat.get())
177 rData.SetValueFormat(bCheck);
178 else if (&rBtn == m_xBtnBorder.get())
179 rData.SetFrame(bCheck);
180 else if (&rBtn == m_xBtnFont.get())
181 rData.SetFont(bCheck);
182 else if (&rBtn == m_xBtnPattern.get())
183 rData.SetBackground(bCheck);
184 else if (&rBtn == m_xBtnAlignment.get())
185 rData.SetJustify(bCheck);
186 else
187 bDataChgd = false;
189 if (bDataChgd)
191 if (!m_bCoreDataChanged)
193 m_xBtnCancel->set_label(m_aStrClose);
194 m_bCoreDataChanged = true;
197 m_aWndPreview.NotifyChange(rData);
201 IMPL_LINK_NOARG(SwAutoFormatDlg, AddHdl, weld::Button&, void)
203 bool bOk = false, bFormatInserted = false;
204 while (!bOk)
206 SwStringInputDlg aDlg(m_xDialog.get(), m_aStrTitle, m_aStrLabel, OUString());
207 if (RET_OK == aDlg.run())
209 const OUString aFormatName(aDlg.GetInputString());
211 if (!aFormatName.isEmpty())
213 size_t n;
214 for (n = 0; n < m_xTableTable->size(); ++n)
215 if ((*m_xTableTable)[n].GetName() == aFormatName)
216 break;
218 if (n >= m_xTableTable->size())
220 // Format with the name does not already exist, so take up.
221 std::unique_ptr<SwTableAutoFormat> pNewData(new SwTableAutoFormat(aFormatName));
222 bool bGetOk = m_pShell->GetTableAutoFormat(*pNewData);
223 SAL_WARN_IF(!bGetOk, "sw.ui", "GetTableAutoFormat failed for: " << aFormatName);
225 // Insert sorted!!
226 for (n = 1; n < m_xTableTable->size(); ++n)
227 if ((*m_xTableTable)[n].GetName() > aFormatName)
228 break;
230 m_xTableTable->InsertAutoFormat(n, std::move(pNewData));
231 m_xLbFormat->insert_text(m_nDfltStylePos + n, aFormatName);
232 m_xLbFormat->select(m_nDfltStylePos + n);
233 bFormatInserted = true;
234 m_xBtnAdd->set_sensitive(false);
235 if (!m_bCoreDataChanged)
237 m_xBtnCancel->set_label(m_aStrClose);
238 m_bCoreDataChanged = true;
241 SelFormatHdl(*m_xLbFormat);
242 bOk = true;
246 if (!bFormatInserted)
248 std::unique_ptr<weld::MessageDialog> xBox(Application::CreateMessageDialog(
249 m_xDialog.get(), VclMessageType::Error, VclButtonsType::OkCancel,
250 m_aStrInvalidFormat));
251 bOk = RET_CANCEL == xBox->run();
254 else
255 bOk = true;
259 IMPL_LINK_NOARG(SwAutoFormatDlg, RemoveHdl, weld::Button&, void)
261 OUString aMessage = m_aStrDelMsg + "\n\n" + m_xLbFormat->get_selected_text() + "\n";
263 std::unique_ptr<weld::MessageDialog> xBox(Application::CreateMessageDialog(
264 m_xDialog.get(), VclMessageType::Question, VclButtonsType::OkCancel, m_aStrDelTitle));
265 xBox->set_secondary_text(aMessage);
267 if (xBox->run() == RET_OK)
269 sal_uInt8 nIndex = m_nIndex;
271 m_xLbFormat->remove(m_nDfltStylePos + nIndex);
272 m_xLbFormat->select(m_nDfltStylePos + nIndex - 1);
274 m_xTableTable->EraseAutoFormat(nIndex);
275 m_nIndex = nIndex - 1;
277 if (!m_nIndex)
279 m_xBtnRemove->set_sensitive(false);
280 m_xBtnRename->set_sensitive(false);
283 if (!m_bCoreDataChanged)
285 m_xBtnCancel->set_label(m_aStrClose);
286 m_bCoreDataChanged = true;
290 SelFormatHdl(*m_xLbFormat);
293 IMPL_LINK_NOARG(SwAutoFormatDlg, RenameHdl, weld::Button&, void)
295 bool bOk = false;
296 while (!bOk)
298 SwStringInputDlg aDlg(m_xDialog.get(), m_aStrRenameTitle, m_aStrLabel,
299 m_xLbFormat->get_selected_text());
300 if (aDlg.run() == RET_OK)
302 bool bFormatRenamed = false;
303 const OUString aFormatName(aDlg.GetInputString());
305 if (!aFormatName.isEmpty())
307 size_t n;
308 for (n = 0; n < m_xTableTable->size(); ++n)
309 if ((*m_xTableTable)[n].GetName() == aFormatName)
310 break;
312 if (n >= m_xTableTable->size())
314 sal_uInt8 nIndex = m_nIndex;
316 // no format with this name exists, so rename it
317 m_xLbFormat->remove(m_nDfltStylePos + nIndex);
318 std::unique_ptr<SwTableAutoFormat> p(m_xTableTable->ReleaseAutoFormat(nIndex));
320 p->SetName(aFormatName);
322 // keep all arrays sorted!
323 for (n = 1; n < m_xTableTable->size(); ++n)
324 if ((*m_xTableTable)[n].GetName() > aFormatName)
326 break;
329 m_xTableTable->InsertAutoFormat(n, std::move(p));
330 m_xLbFormat->insert_text(m_nDfltStylePos + n, aFormatName);
331 m_xLbFormat->select(m_nDfltStylePos + n);
333 if (!m_bCoreDataChanged)
335 m_xBtnCancel->set_label(m_aStrClose);
336 m_bCoreDataChanged = true;
339 SelFormatHdl(*m_xLbFormat);
340 bOk = true;
341 bFormatRenamed = true;
345 if (!bFormatRenamed)
347 std::unique_ptr<weld::MessageDialog> xBox(Application::CreateMessageDialog(
348 m_xDialog.get(), VclMessageType::Error, VclButtonsType::OkCancel,
349 m_aStrInvalidFormat));
350 bOk = RET_CANCEL == xBox->run();
353 else
354 bOk = true;
358 IMPL_LINK_NOARG(SwAutoFormatDlg, SelFormatHdl, weld::TreeView&, void)
360 bool bBtnEnable = false;
361 sal_uInt8 nOldIdx = m_nIndex;
362 int nSelPos = m_xLbFormat->get_selected_index();
363 if (nSelPos >= m_nDfltStylePos)
365 m_nIndex = nSelPos - m_nDfltStylePos;
366 m_aWndPreview.NotifyChange((*m_xTableTable)[m_nIndex]);
367 bBtnEnable = 0 != m_nIndex;
368 UpdateChecks((*m_xTableTable)[m_nIndex], true);
370 else
372 m_nIndex = 255;
374 SwTableAutoFormat aTmp(SwViewShell::GetShellRes()->aStrNone);
375 aTmp.SetFont(false);
376 aTmp.SetJustify(false);
377 aTmp.SetFrame(false);
378 aTmp.SetBackground(false);
379 aTmp.SetValueFormat(false);
380 aTmp.SetWidthHeight(false);
382 if (nOldIdx != m_nIndex)
383 m_aWndPreview.NotifyChange(aTmp);
384 UpdateChecks(aTmp, false);
387 m_xBtnRemove->set_sensitive(bBtnEnable);
388 m_xBtnRename->set_sensitive(bBtnEnable);
391 void SwAutoFormatDlg::Apply()
393 if (m_bSetAutoFormat)
395 if (m_nIndex == 255)
396 m_pShell->ResetTableStyle();
397 else
398 m_pShell->SetTableStyle((*m_xTableTable)[m_nIndex]);
402 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */