LibreOffice Module sw (master)  1
tautofmt.cxx
Go to the documentation of this file.
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 <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>
27 
28 namespace {
29 
30 class SwStringInputDlg : public SfxDialogController
31 {
32 private:
33  std::unique_ptr<weld::Label> m_xLabel;
34  std::unique_ptr<weld::Entry> m_xEdInput; // Edit obtains the focus.
35 
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"))
43  {
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);
48  }
49 
50  OUString GetInputString() const
51  {
52  return m_xEdInput->get_text();
53  }
54 };
55 
56 }
57 
58 // AutoFormat-Dialogue:
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))
87 {
88  m_aWndPreview.DetectRTL(pWrtShell);
89  m_xTableTable->Load();
90 
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);
95 
96  Init(pSelFormat);
97 }
98 
100 {
101  try
102  {
103  if (m_bCoreDataChanged)
104  m_xTableTable->Save();
105  }
106  catch (...)
107  {
108  }
109  m_xTableTable.reset();
110 }
111 
112 void SwAutoFormatDlg::Init( const SwTableAutoFormat* pSelFormat )
113 {
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);
120 
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));
125 
126  m_xBtnAdd->set_sensitive(m_bSetAutoFormat);
127 
128  m_nIndex = 0;
129  if( !m_bSetAutoFormat )
130  {
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;
135  }
136 
137  for (sal_uInt8 i = 0, nCount = static_cast<sal_uInt8>(m_xTableTable->size());
138  i < nCount; i++)
139  {
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;
144  }
145 
146  m_xLbFormat->select(255 != m_nIndex ? (m_nDfltStylePos + m_nIndex) : 0);
147  SelFormatHdl(*m_xLbFormat);
148 }
149 
150 void SwAutoFormatDlg::UpdateChecks( const SwTableAutoFormat& rFormat, bool bEnable )
151 {
152  m_xBtnNumFormat->set_sensitive(bEnable);
153  m_xBtnNumFormat->set_active(rFormat.IsValueFormat());
154 
155  m_xBtnBorder->set_sensitive(bEnable);
156  m_xBtnBorder->set_active(rFormat.IsFrame());
157 
158  m_xBtnFont->set_sensitive(bEnable);
159  m_xBtnFont->set_active(rFormat.IsFont());
160 
161  m_xBtnPattern->set_sensitive(bEnable);
162  m_xBtnPattern->set_active(rFormat.IsBackground());
163 
164  m_xBtnAlignment->set_sensitive(bEnable);
165  m_xBtnAlignment->set_active(rFormat.IsJustify());
166 }
167 
168 std::unique_ptr<SwTableAutoFormat> SwAutoFormatDlg::FillAutoFormatOfIndex() const
169 {
170  if( 255 != m_nIndex )
171  {
172  return std::make_unique<SwTableAutoFormat>( (*m_xTableTable)[ m_nIndex ] );
173  }
174 
175  return nullptr;
176 }
177 
178 // Handler:
180 {
181  if (m_nIndex == 255)
182  return;
183 
184  SwTableAutoFormat& rData = (*m_xTableTable)[m_nIndex];
185  bool bCheck = rBtn.get_active(), bDataChgd = true;
186 
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;
199 
200  if( bDataChgd )
201  {
202  if( !m_bCoreDataChanged )
203  {
204  m_xBtnCancel->set_label(m_aStrClose);
205  m_bCoreDataChanged = true;
206  }
207 
208  m_aWndPreview.NotifyChange(rData);
209  }
210 }
211 
213 {
214  bool bOk = false, bFormatInserted = false;
215  while( !bOk )
216  {
217  SwStringInputDlg aDlg(m_xDialog.get(), m_aStrTitle, m_aStrLabel, OUString());
218  if (RET_OK == aDlg.run())
219  {
220  const OUString aFormatName(aDlg.GetInputString());
221 
222  if ( !aFormatName.isEmpty() )
223  {
224  size_t n;
225  for( n = 0; n < m_xTableTable->size(); ++n )
226  if( (*m_xTableTable)[n].GetName() == aFormatName )
227  break;
228 
229  if( n >= m_xTableTable->size() )
230  {
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);
236 
237  // Insert sorted!!
238  for( n = 1; n < m_xTableTable->size(); ++n )
239  if( (*m_xTableTable)[ n ].GetName() > aFormatName )
240  break;
241 
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 )
248  {
249  m_xBtnCancel->set_label(m_aStrClose);
250  m_bCoreDataChanged = true;
251  }
252 
253  SelFormatHdl(*m_xLbFormat);
254  bOk = true;
255  }
256  }
257 
258  if( !bFormatInserted )
259  {
260  std::unique_ptr<weld::MessageDialog> xBox(Application::CreateMessageDialog(m_xDialog.get(), VclMessageType::Error, VclButtonsType::OkCancel, m_aStrInvalidFormat));
261  bOk = RET_CANCEL == xBox->run();
262  }
263  }
264  else
265  bOk = true;
266  }
267 }
268 
270 {
271  OUString aMessage = m_aStrDelMsg + "\n\n" +
272  m_xLbFormat->get_selected_text() + "\n";
273 
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);
277 
278  if (xBox->run() == RET_OK)
279  {
280  sal_uInt8 nIndex = m_nIndex;
281 
282  m_xLbFormat->remove(m_nDfltStylePos + nIndex);
283  m_xLbFormat->select(m_nDfltStylePos + nIndex - 1);
284 
285  m_xTableTable->EraseAutoFormat(nIndex);
286  m_nIndex = nIndex - 1;
287 
288  if( !m_nIndex )
289  {
290  m_xBtnRemove->set_sensitive(false);
291  m_xBtnRename->set_sensitive(false);
292  }
293 
294  if( !m_bCoreDataChanged )
295  {
296  m_xBtnCancel->set_label(m_aStrClose);
297  m_bCoreDataChanged = true;
298  }
299  }
300 
301  SelFormatHdl(*m_xLbFormat);
302 }
303 
305 {
306  bool bOk = false;
307  while( !bOk )
308  {
309  SwStringInputDlg aDlg(m_xDialog.get(), m_aStrRenameTitle, m_aStrLabel, m_xLbFormat->get_selected_text());
310  if (aDlg.run() == RET_OK)
311  {
312  bool bFormatRenamed = false;
313  const OUString aFormatName(aDlg.GetInputString());
314 
315  if ( !aFormatName.isEmpty() )
316  {
317  size_t n;
318  for( n = 0; n < m_xTableTable->size(); ++n )
319  if ((*m_xTableTable)[n].GetName() == aFormatName)
320  break;
321 
322  if( n >= m_xTableTable->size() )
323  {
324  sal_uInt8 nIndex = m_nIndex;
325 
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));
330 
331  p->SetName( aFormatName );
332 
333  // keep all arrays sorted!
334  for( n = 1; n < m_xTableTable->size(); ++n )
335  if ((*m_xTableTable)[n].GetName() > aFormatName)
336  {
337  break;
338  }
339 
340  m_xTableTable->InsertAutoFormat( n, std::move(p) );
341  m_xLbFormat->insert_text(m_nDfltStylePos + n, aFormatName);
342  m_xLbFormat->select(m_nDfltStylePos + n);
343 
344  if ( !m_bCoreDataChanged )
345  {
346  m_xBtnCancel->set_label(m_aStrClose);
347  m_bCoreDataChanged = true;
348  }
349 
350  SelFormatHdl(*m_xLbFormat);
351  bOk = true;
352  bFormatRenamed = true;
353  }
354  }
355 
356  if( !bFormatRenamed )
357  {
358  std::unique_ptr<weld::MessageDialog> xBox(Application::CreateMessageDialog(m_xDialog.get(), VclMessageType::Error, VclButtonsType::OkCancel, m_aStrInvalidFormat));
359  bOk = RET_CANCEL == xBox->run();
360  }
361  }
362  else
363  bOk = true;
364  }
365 }
366 
368 {
369  bool bBtnEnable = false;
370  sal_uInt8 nOldIdx = m_nIndex;
371  int nSelPos = m_xLbFormat->get_selected_index();
372  if (nSelPos >= m_nDfltStylePos)
373  {
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 );
378  }
379  else
380  {
381  m_nIndex = 255;
382 
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 );
390 
391  if (nOldIdx != m_nIndex)
392  m_aWndPreview.NotifyChange(aTmp);
393  UpdateChecks( aTmp, false );
394  }
395 
396  m_xBtnRemove->set_sensitive(bBtnEnable);
397  m_xBtnRename->set_sensitive(bBtnEnable);
398 }
399 
401 {
402  short nRet = SfxDialogController::run();
403  if (nRet == RET_OK && m_bSetAutoFormat)
405  return nRet;
406 }
407 
408 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
std::unique_ptr< SwTableAutoFormat > FillAutoFormatOfIndex() const
Definition: tautofmt.cxx:168
bool IsValueFormat() const
Definition: tblafmt.hxx:223
sal_Int32 nIndex
void SetValueFormat(const bool bNew)
Definition: tblafmt.hxx:234
static ShellResource * GetShellRes()
Definition: viewsh.cxx:2487
bool m_bCoreDataChanged
Definition: tautofmt.hxx:49
IMPL_LINK_NOARG(SwAutoFormatDlg, AddHdl, weld::Button &, void)
Definition: tautofmt.cxx:212
void SetFrame(const bool bNew)
Definition: tblafmt.hxx:232
sal_Int64 n
virtual ~SwAutoFormatDlg() override
Definition: tautofmt.cxx:99
const ContentProperties & rData
sal_uInt8 m_nIndex
Definition: tautofmt.hxx:47
RET_CANCEL
std::unique_ptr< weld::CheckButton > m_xBtnFont
Definition: tautofmt.hxx:58
std::unique_ptr< weld::Label > m_xLabel
Used by the UI to modify the document model.
Definition: wrtsh.hxx:90
bool IsFont() const
Definition: tblafmt.hxx:219
void DetectRTL(SwWrtShell const *pWrtShell)
bool IsJustify() const
Definition: tblafmt.hxx:220
std::unique_ptr< SwTableAutoFormatTable > m_xTableTable
Definition: tautofmt.hxx:53
void SetFont(const bool bNew)
Definition: tblafmt.hxx:230
std::unique_ptr< weld::Button > m_xBtnRename
Definition: tautofmt.hxx:64
bool SetTableStyle(const OUString &rStyleName)
Set table style of the current table.
Definition: fetab.cxx:1171
int nCount
bool m_bSetAutoFormat
Definition: tautofmt.hxx:50
std::unique_ptr< weld::CheckButton > m_xBtnBorder
Definition: tautofmt.hxx:57
std::unique_ptr< weld::Button > m_xBtnRemove
Definition: tautofmt.hxx:63
bool IsBackground() const
Definition: tblafmt.hxx:222
virtual short run() override
Definition: tautofmt.cxx:400
void SetJustify(const bool bNew)
Definition: tblafmt.hxx:231
int i
void Init(const SwTableAutoFormat *pSelFormat)
Definition: tautofmt.cxx:112
std::unique_ptr< weld::CheckButton > m_xBtnNumFormat
Definition: tautofmt.hxx:56
std::unique_ptr< weld::CheckButton > m_xBtnPattern
Definition: tautofmt.hxx:59
void SetWidthHeight(const bool bNew)
Definition: tblafmt.hxx:235
OUString SwResId(const char *pId)
Definition: swmodule.cxx:178
void SetBackground(const bool bNew)
Definition: tblafmt.hxx:233
Shell * m_pShell
IMPL_LINK(SwAutoFormatDlg, CheckHdl, weld::ToggleButton &, rBtn, void)
Definition: tautofmt.cxx:179
const OUString & GetName() const
Definition: tblafmt.hxx:208
std::unique_ptr< weld::TreeView > m_xLbFormat
Definition: tautofmt.hxx:55
SwAutoFormatDlg(weld::Window *pParent, SwWrtShell *pShell, bool bSetAutoFormat, const SwTableAutoFormat *pSelFormat)
Definition: tautofmt.cxx:59
AutoFormatPreview m_aWndPreview
Definition: tautofmt.hxx:52
#define SAL_WARN_IF(condition, area, stream)
unsigned char sal_uInt8
std::unique_ptr< weld::Button > m_xBtnAdd
Definition: tautofmt.hxx:62
RET_OK
Reference< XExecutableDialog > m_xDialog
void * p
sal_uInt8 m_nDfltStylePos
Definition: tautofmt.hxx:48
std::unique_ptr< weld::CustomWeld > m_xWndPreview
Definition: tautofmt.hxx:65
static weld::MessageDialog * CreateMessageDialog(weld::Widget *pParent, VclMessageType eMessageType, VclButtonsType eButtonType, const OUString &rPrimaryMessage)
SwWrtShell * m_pShell
Definition: tautofmt.hxx:46
std::unique_ptr< weld::CheckButton > m_xBtnAlignment
Definition: tautofmt.hxx:60
bool IsFrame() const
Definition: tblafmt.hxx:221
void UpdateChecks(const SwTableAutoFormat &, bool bEnableBtn)
Definition: tautofmt.cxx:150