android: Update app-specific/MIME type icons
[LibreOffice.git] / sw / source / ui / table / tautofmt.cxx
blob8dbc965e80ba12e3d403b26678486a24ce51fd25
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,
38 const OUString& rEditTitle, const OUString& rDefault)
39 : SfxDialogController(pParent, "modules/swriter/ui/stringinput.ui",
40 "StringInputDialog")
41 , m_xLabel(m_xBuilder->weld_label("name"))
42 , m_xEdInput(m_xBuilder->weld_entry("edit"))
44 m_xLabel->set_label(rEditTitle);
45 m_xDialog->set_title(rTitle);
46 m_xEdInput->set_text(rDefault);
47 m_xEdInput->select_region(0, -1);
50 OUString GetInputString() const
52 return m_xEdInput->get_text();
58 // AutoFormat-Dialogue:
59 SwAutoFormatDlg::SwAutoFormatDlg(weld::Window* pParent, SwWrtShell* pWrtShell,
60 bool bAutoFormat, const SwTableAutoFormat* pSelFormat)
61 : SfxDialogController(pParent, "modules/swriter/ui/autoformattable.ui",
62 "AutoFormatTableDialog")
63 , m_aStrTitle(SwResId(STR_ADD_AUTOFORMAT_TITLE))
64 , m_aStrLabel(SwResId(STR_ADD_AUTOFORMAT_LABEL))
65 , m_aStrClose(SwResId(STR_BTN_AUTOFORMAT_CLOSE))
66 , m_aStrDelTitle(SwResId(STR_DEL_AUTOFORMAT_TITLE))
67 , m_aStrDelMsg(SwResId(STR_DEL_AUTOFORMAT_MSG))
68 , m_aStrRenameTitle(SwResId(STR_RENAME_AUTOFORMAT_TITLE))
69 , m_aStrInvalidFormat(SwResId(STR_INVALID_AUTOFORMAT_NAME))
70 , m_pShell(pWrtShell)
71 , m_nIndex(0)
72 , m_nDfltStylePos(0)
73 , m_bCoreDataChanged(false)
74 , m_bSetAutoFormat(bAutoFormat)
75 , m_xTableTable(new SwTableAutoFormatTable)
76 , m_xLbFormat(m_xBuilder->weld_tree_view("formatlb"))
77 , m_xBtnNumFormat(m_xBuilder->weld_check_button("numformatcb"))
78 , m_xBtnBorder(m_xBuilder->weld_check_button("bordercb"))
79 , m_xBtnFont(m_xBuilder->weld_check_button("fontcb"))
80 , m_xBtnPattern(m_xBuilder->weld_check_button("patterncb"))
81 , m_xBtnAlignment(m_xBuilder->weld_check_button("alignmentcb"))
82 , m_xBtnCancel(m_xBuilder->weld_button("cancel"))
83 , m_xBtnAdd(m_xBuilder->weld_button("add"))
84 , m_xBtnRemove(m_xBuilder->weld_button("remove"))
85 , m_xBtnRename(m_xBuilder->weld_button("rename"))
86 , m_xWndPreview(new weld::CustomWeld(*m_xBuilder, "preview", m_aWndPreview))
88 m_aWndPreview.DetectRTL(pWrtShell);
89 m_xTableTable->Load();
91 const int nWidth = m_xLbFormat->get_approximate_digit_width() * 32;
92 const int nHeight = m_xLbFormat->get_height_rows(8);
93 m_xLbFormat->set_size_request(nWidth, nHeight);
94 m_xWndPreview->set_size_request(nWidth, nHeight);
96 Init(pSelFormat);
99 SwAutoFormatDlg::~SwAutoFormatDlg()
103 if (m_bCoreDataChanged)
104 m_xTableTable->Save();
106 catch (...)
109 m_xTableTable.reset();
112 void SwAutoFormatDlg::Init( const SwTableAutoFormat* pSelFormat )
114 Link<weld::Toggleable&, void> aLk(LINK(this, SwAutoFormatDlg, CheckHdl));
115 m_xBtnBorder->connect_toggled(aLk);
116 m_xBtnFont->connect_toggled(aLk);
117 m_xBtnPattern->connect_toggled(aLk);
118 m_xBtnAlignment->connect_toggled(aLk);
119 m_xBtnNumFormat->connect_toggled(aLk);
121 m_xBtnAdd->connect_clicked(LINK(this, SwAutoFormatDlg, AddHdl));
122 m_xBtnRemove->connect_clicked(LINK(this, SwAutoFormatDlg, RemoveHdl));
123 m_xBtnRename->connect_clicked(LINK(this, SwAutoFormatDlg, RenameHdl));
124 m_xLbFormat->connect_changed(LINK(this, SwAutoFormatDlg, SelFormatHdl));
126 m_xBtnAdd->set_sensitive(m_bSetAutoFormat);
128 m_nIndex = 0;
129 if( !m_bSetAutoFormat )
131 // Then the list to be expanded by the entry "- none -".
132 m_xLbFormat->append_text(SwViewShell::GetShellRes()->aStrNone);
133 m_nDfltStylePos = 1;
134 m_nIndex = 255;
137 for (sal_uInt8 i = 0, nCount = static_cast<sal_uInt8>(m_xTableTable->size());
138 i < nCount; i++)
140 SwTableAutoFormat const& rFormat = (*m_xTableTable)[ i ];
141 m_xLbFormat->append_text(rFormat.GetName());
142 if (pSelFormat && rFormat.GetName() == pSelFormat->GetName())
143 m_nIndex = i;
146 m_xLbFormat->select(255 != m_nIndex ? (m_nDfltStylePos + m_nIndex) : 0);
147 SelFormatHdl(*m_xLbFormat);
150 void SwAutoFormatDlg::UpdateChecks( const SwTableAutoFormat& rFormat, bool bEnable )
152 m_xBtnNumFormat->set_sensitive(bEnable);
153 m_xBtnNumFormat->set_active(rFormat.IsValueFormat());
155 m_xBtnBorder->set_sensitive(bEnable);
156 m_xBtnBorder->set_active(rFormat.IsFrame());
158 m_xBtnFont->set_sensitive(bEnable);
159 m_xBtnFont->set_active(rFormat.IsFont());
161 m_xBtnPattern->set_sensitive(bEnable);
162 m_xBtnPattern->set_active(rFormat.IsBackground());
164 m_xBtnAlignment->set_sensitive(bEnable);
165 m_xBtnAlignment->set_active(rFormat.IsJustify());
168 std::unique_ptr<SwTableAutoFormat> SwAutoFormatDlg::FillAutoFormatOfIndex() const
170 if( 255 != m_nIndex )
172 return std::make_unique<SwTableAutoFormat>( (*m_xTableTable)[ m_nIndex ] );
175 return nullptr;
178 // Handler:
179 IMPL_LINK(SwAutoFormatDlg, CheckHdl, weld::Toggleable&, rBtn, void)
181 if (m_nIndex == 255)
182 return;
184 SwTableAutoFormat& rData = (*m_xTableTable)[m_nIndex];
185 bool bCheck = rBtn.get_active(), bDataChgd = true;
187 if (&rBtn == m_xBtnNumFormat.get())
188 rData.SetValueFormat( bCheck );
189 else if (&rBtn == m_xBtnBorder.get())
190 rData.SetFrame( bCheck );
191 else if (&rBtn == m_xBtnFont.get())
192 rData.SetFont( bCheck );
193 else if (&rBtn == m_xBtnPattern.get())
194 rData.SetBackground( bCheck );
195 else if (&rBtn == m_xBtnAlignment.get())
196 rData.SetJustify( bCheck );
197 else
198 bDataChgd = false;
200 if( bDataChgd )
202 if( !m_bCoreDataChanged )
204 m_xBtnCancel->set_label(m_aStrClose);
205 m_bCoreDataChanged = true;
208 m_aWndPreview.NotifyChange(rData);
212 IMPL_LINK_NOARG(SwAutoFormatDlg, AddHdl, weld::Button&, void)
214 bool bOk = false, bFormatInserted = false;
215 while( !bOk )
217 SwStringInputDlg aDlg(m_xDialog.get(), m_aStrTitle, m_aStrLabel, OUString());
218 if (RET_OK == aDlg.run())
220 const OUString aFormatName(aDlg.GetInputString());
222 if ( !aFormatName.isEmpty() )
224 size_t n;
225 for( n = 0; n < m_xTableTable->size(); ++n )
226 if( (*m_xTableTable)[n].GetName() == aFormatName )
227 break;
229 if( n >= m_xTableTable->size() )
231 // Format with the name does not already exist, so take up.
232 std::unique_ptr<SwTableAutoFormat> pNewData(
233 new SwTableAutoFormat(aFormatName));
234 bool bGetOk = m_pShell->GetTableAutoFormat( *pNewData );
235 SAL_WARN_IF(!bGetOk, "sw.ui", "GetTableAutoFormat failed for: " << aFormatName);
237 // Insert sorted!!
238 for( n = 1; n < m_xTableTable->size(); ++n )
239 if( (*m_xTableTable)[ n ].GetName() > aFormatName )
240 break;
242 m_xTableTable->InsertAutoFormat(n, std::move(pNewData));
243 m_xLbFormat->insert_text(m_nDfltStylePos + n, aFormatName);
244 m_xLbFormat->select(m_nDfltStylePos + n);
245 bFormatInserted = true;
246 m_xBtnAdd->set_sensitive(false);
247 if ( !m_bCoreDataChanged )
249 m_xBtnCancel->set_label(m_aStrClose);
250 m_bCoreDataChanged = true;
253 SelFormatHdl(*m_xLbFormat);
254 bOk = true;
258 if( !bFormatInserted )
260 std::unique_ptr<weld::MessageDialog> xBox(Application::CreateMessageDialog(m_xDialog.get(), VclMessageType::Error, VclButtonsType::OkCancel, m_aStrInvalidFormat));
261 bOk = RET_CANCEL == xBox->run();
264 else
265 bOk = true;
269 IMPL_LINK_NOARG(SwAutoFormatDlg, RemoveHdl, weld::Button&, void)
271 OUString aMessage = m_aStrDelMsg + "\n\n" +
272 m_xLbFormat->get_selected_text() + "\n";
274 std::unique_ptr<weld::MessageDialog> xBox(Application::CreateMessageDialog(m_xDialog.get(), VclMessageType::Question,
275 VclButtonsType::OkCancel, m_aStrDelTitle));
276 xBox->set_secondary_text(aMessage);
278 if (xBox->run() == RET_OK)
280 sal_uInt8 nIndex = m_nIndex;
282 m_xLbFormat->remove(m_nDfltStylePos + nIndex);
283 m_xLbFormat->select(m_nDfltStylePos + nIndex - 1);
285 m_xTableTable->EraseAutoFormat(nIndex);
286 m_nIndex = nIndex - 1;
288 if( !m_nIndex )
290 m_xBtnRemove->set_sensitive(false);
291 m_xBtnRename->set_sensitive(false);
294 if( !m_bCoreDataChanged )
296 m_xBtnCancel->set_label(m_aStrClose);
297 m_bCoreDataChanged = true;
301 SelFormatHdl(*m_xLbFormat);
304 IMPL_LINK_NOARG(SwAutoFormatDlg, RenameHdl, weld::Button&, void)
306 bool bOk = false;
307 while( !bOk )
309 SwStringInputDlg aDlg(m_xDialog.get(), m_aStrRenameTitle, m_aStrLabel, m_xLbFormat->get_selected_text());
310 if (aDlg.run() == RET_OK)
312 bool bFormatRenamed = false;
313 const OUString aFormatName(aDlg.GetInputString());
315 if ( !aFormatName.isEmpty() )
317 size_t n;
318 for( n = 0; n < m_xTableTable->size(); ++n )
319 if ((*m_xTableTable)[n].GetName() == aFormatName)
320 break;
322 if( n >= m_xTableTable->size() )
324 sal_uInt8 nIndex = m_nIndex;
326 // no format with this name exists, so rename it
327 m_xLbFormat->remove(m_nDfltStylePos + nIndex);
328 std::unique_ptr<SwTableAutoFormat> p(
329 m_xTableTable->ReleaseAutoFormat(nIndex));
331 p->SetName( aFormatName );
333 // keep all arrays sorted!
334 for( n = 1; n < m_xTableTable->size(); ++n )
335 if ((*m_xTableTable)[n].GetName() > aFormatName)
337 break;
340 m_xTableTable->InsertAutoFormat( n, std::move(p) );
341 m_xLbFormat->insert_text(m_nDfltStylePos + n, aFormatName);
342 m_xLbFormat->select(m_nDfltStylePos + n);
344 if ( !m_bCoreDataChanged )
346 m_xBtnCancel->set_label(m_aStrClose);
347 m_bCoreDataChanged = true;
350 SelFormatHdl(*m_xLbFormat);
351 bOk = true;
352 bFormatRenamed = true;
356 if( !bFormatRenamed )
358 std::unique_ptr<weld::MessageDialog> xBox(Application::CreateMessageDialog(m_xDialog.get(), VclMessageType::Error, VclButtonsType::OkCancel, m_aStrInvalidFormat));
359 bOk = RET_CANCEL == xBox->run();
362 else
363 bOk = true;
367 IMPL_LINK_NOARG(SwAutoFormatDlg, SelFormatHdl, weld::TreeView&, void)
369 bool bBtnEnable = false;
370 sal_uInt8 nOldIdx = m_nIndex;
371 int nSelPos = m_xLbFormat->get_selected_index();
372 if (nSelPos >= m_nDfltStylePos)
374 m_nIndex = nSelPos - m_nDfltStylePos;
375 m_aWndPreview.NotifyChange((*m_xTableTable)[m_nIndex]);
376 bBtnEnable = 0 != m_nIndex;
377 UpdateChecks( (*m_xTableTable)[m_nIndex], true );
379 else
381 m_nIndex = 255;
383 SwTableAutoFormat aTmp( SwViewShell::GetShellRes()->aStrNone );
384 aTmp.SetFont( false );
385 aTmp.SetJustify( false );
386 aTmp.SetFrame( false );
387 aTmp.SetBackground( false );
388 aTmp.SetValueFormat( false );
389 aTmp.SetWidthHeight( false );
391 if (nOldIdx != m_nIndex)
392 m_aWndPreview.NotifyChange(aTmp);
393 UpdateChecks( aTmp, false );
396 m_xBtnRemove->set_sensitive(bBtnEnable);
397 m_xBtnRename->set_sensitive(bBtnEnable);
400 short SwAutoFormatDlg::run()
402 short nRet = SfxDialogController::run();
403 if (nRet == RET_OK && m_bSetAutoFormat)
404 m_pShell->SetTableStyle((*m_xTableTable)[m_nIndex]);
405 return nRet;
408 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */