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 <view.hxx>
26 #include <shellres.hxx>
27 #include <tautofmt.hxx>
28 
29 namespace {
30 
31 class SwStringInputDlg : public SfxDialogController
32 {
33 private:
34  std::unique_ptr<weld::Label> m_xLabel;
35  std::unique_ptr<weld::Entry> m_xEdInput; // Edit obtains the focus.
36 
37 public:
38  SwStringInputDlg(weld::Window* pParent, const OUString& rTitle,
39  const OUString& rEditTitle, const OUString& rDefault)
40  : SfxDialogController(pParent, "modules/swriter/ui/stringinput.ui",
41  "StringInputDialog")
42  , m_xLabel(m_xBuilder->weld_label("name"))
43  , m_xEdInput(m_xBuilder->weld_entry("edit"))
44  {
45  m_xLabel->set_label(rEditTitle);
46  m_xDialog->set_title(rTitle);
47  m_xEdInput->set_text(rDefault);
48  m_xEdInput->select_region(0, -1);
49  }
50 
51  OUString GetInputString() const
52  {
53  return m_xEdInput->get_text();
54  }
55 };
56 
57 }
58 
59 // AutoFormat-Dialogue:
61  bool bAutoFormat, const SwTableAutoFormat* pSelFormat)
62  : SfxDialogController(pParent, "modules/swriter/ui/autoformattable.ui",
63  "AutoFormatTableDialog")
64  , m_aStrTitle(SwResId(STR_ADD_AUTOFORMAT_TITLE))
65  , m_aStrLabel(SwResId(STR_ADD_AUTOFORMAT_LABEL))
66  , m_aStrClose(SwResId(STR_BTN_AUTOFORMAT_CLOSE))
67  , m_aStrDelTitle(SwResId(STR_DEL_AUTOFORMAT_TITLE))
68  , m_aStrDelMsg(SwResId(STR_DEL_AUTOFORMAT_MSG))
69  , m_aStrRenameTitle(SwResId(STR_RENAME_AUTOFORMAT_TITLE))
70  , m_aStrInvalidFormat(SwResId(STR_INVALID_AUTOFORMAT_NAME))
71  , m_pShell(pWrtShell)
72  , m_nIndex(0)
73  , m_nDfltStylePos(0)
74  , m_bCoreDataChanged(false)
75  , m_bSetAutoFormat(bAutoFormat)
76  , m_xTableTable(new SwTableAutoFormatTable)
77  , m_xLbFormat(m_xBuilder->weld_tree_view("formatlb"))
78  , m_xBtnNumFormat(m_xBuilder->weld_check_button("numformatcb"))
79  , m_xBtnBorder(m_xBuilder->weld_check_button("bordercb"))
80  , m_xBtnFont(m_xBuilder->weld_check_button("fontcb"))
81  , m_xBtnPattern(m_xBuilder->weld_check_button("patterncb"))
82  , m_xBtnAlignment(m_xBuilder->weld_check_button("alignmentcb"))
83  , m_xBtnCancel(m_xBuilder->weld_button("cancel"))
84  , m_xBtnAdd(m_xBuilder->weld_button("add"))
85  , m_xBtnRemove(m_xBuilder->weld_button("remove"))
86  , m_xBtnRename(m_xBuilder->weld_button("rename"))
87  , m_xWndPreview(new weld::CustomWeld(*m_xBuilder, "preview", m_aWndPreview))
88 {
89  m_aWndPreview.DetectRTL(pWrtShell);
90  m_xTableTable->Load();
91 
92  const int nWidth = m_xLbFormat->get_approximate_digit_width() * 32;
93  const int nHeight = m_xLbFormat->get_height_rows(8);
94  m_xLbFormat->set_size_request(nWidth, nHeight);
95  m_xWndPreview->set_size_request(nWidth, nHeight);
96 
97  Init(pSelFormat);
98 }
99 
101 {
102  try
103  {
104  if (m_bCoreDataChanged)
105  m_xTableTable->Save();
106  }
107  catch (...)
108  {
109  }
110  m_xTableTable.reset();
111 }
112 
113 void SwAutoFormatDlg::Init( const SwTableAutoFormat* pSelFormat )
114 {
116  m_xBtnBorder->connect_toggled(aLk);
117  m_xBtnFont->connect_toggled(aLk);
118  m_xBtnPattern->connect_toggled(aLk);
119  m_xBtnAlignment->connect_toggled(aLk);
120  m_xBtnNumFormat->connect_toggled(aLk);
121 
122  m_xBtnAdd->connect_clicked(LINK(this, SwAutoFormatDlg, AddHdl));
123  m_xBtnRemove->connect_clicked(LINK(this, SwAutoFormatDlg, RemoveHdl));
124  m_xBtnRename->connect_clicked(LINK(this, SwAutoFormatDlg, RenameHdl));
125  m_xLbFormat->connect_changed(LINK(this, SwAutoFormatDlg, SelFormatHdl));
126 
127  m_xBtnAdd->set_sensitive(m_bSetAutoFormat);
128 
129  m_nIndex = 0;
130  if( !m_bSetAutoFormat )
131  {
132  // Then the list to be expanded by the entry "- none -".
133  m_xLbFormat->append_text(SwViewShell::GetShellRes()->aStrNone);
134  m_nDfltStylePos = 1;
135  m_nIndex = 255;
136  }
137 
138  for (sal_uInt8 i = 0, nCount = static_cast<sal_uInt8>(m_xTableTable->size());
139  i < nCount; i++)
140  {
141  SwTableAutoFormat const& rFormat = (*m_xTableTable)[ i ];
142  m_xLbFormat->append_text(rFormat.GetName());
143  if (pSelFormat && rFormat.GetName() == pSelFormat->GetName())
144  m_nIndex = i;
145  }
146 
147  m_xLbFormat->select(255 != m_nIndex ? (m_nDfltStylePos + m_nIndex) : 0);
148  SelFormatHdl(*m_xLbFormat);
149 }
150 
151 void SwAutoFormatDlg::UpdateChecks( const SwTableAutoFormat& rFormat, bool bEnable )
152 {
153  m_xBtnNumFormat->set_sensitive(bEnable);
154  m_xBtnNumFormat->set_active(rFormat.IsValueFormat());
155 
156  m_xBtnBorder->set_sensitive(bEnable);
157  m_xBtnBorder->set_active(rFormat.IsFrame());
158 
159  m_xBtnFont->set_sensitive(bEnable);
160  m_xBtnFont->set_active(rFormat.IsFont());
161 
162  m_xBtnPattern->set_sensitive(bEnable);
163  m_xBtnPattern->set_active(rFormat.IsBackground());
164 
165  m_xBtnAlignment->set_sensitive(bEnable);
166  m_xBtnAlignment->set_active(rFormat.IsJustify());
167 }
168 
169 std::unique_ptr<SwTableAutoFormat> SwAutoFormatDlg::FillAutoFormatOfIndex() const
170 {
171  if( 255 != m_nIndex )
172  {
173  return std::make_unique<SwTableAutoFormat>( (*m_xTableTable)[ m_nIndex ] );
174  }
175 
176  return nullptr;
177 }
178 
179 // Handler:
181 {
182  if (m_nIndex == 255)
183  return;
184 
185  SwTableAutoFormat& rData = (*m_xTableTable)[m_nIndex];
186  bool bCheck = rBtn.get_active(), bDataChgd = true;
187 
188  if (&rBtn == m_xBtnNumFormat.get())
189  rData.SetValueFormat( bCheck );
190  else if (&rBtn == m_xBtnBorder.get())
191  rData.SetFrame( bCheck );
192  else if (&rBtn == m_xBtnFont.get())
193  rData.SetFont( bCheck );
194  else if (&rBtn == m_xBtnPattern.get())
195  rData.SetBackground( bCheck );
196  else if (&rBtn == m_xBtnAlignment.get())
197  rData.SetJustify( bCheck );
198  else
199  bDataChgd = false;
200 
201  if( bDataChgd )
202  {
203  if( !m_bCoreDataChanged )
204  {
205  m_xBtnCancel->set_label(m_aStrClose);
206  m_bCoreDataChanged = true;
207  }
208 
209  m_aWndPreview.NotifyChange(rData);
210  }
211 }
212 
214 {
215  bool bOk = false, bFormatInserted = false;
216  while( !bOk )
217  {
218  SwStringInputDlg aDlg(m_xDialog.get(), m_aStrTitle, m_aStrLabel, OUString());
219  if (RET_OK == aDlg.run())
220  {
221  const OUString aFormatName(aDlg.GetInputString());
222 
223  if ( !aFormatName.isEmpty() )
224  {
225  size_t n;
226  for( n = 0; n < m_xTableTable->size(); ++n )
227  if( (*m_xTableTable)[n].GetName() == aFormatName )
228  break;
229 
230  if( n >= m_xTableTable->size() )
231  {
232  // Format with the name does not already exist, so take up.
233  std::unique_ptr<SwTableAutoFormat> pNewData(
234  new SwTableAutoFormat(aFormatName));
235  bool bGetOk = m_pShell->GetTableAutoFormat( *pNewData );
236  SAL_WARN_IF(!bGetOk, "sw.ui", "GetTableAutoFormat failed for: " << aFormatName);
237 
238  // Insert sorted!!
239  for( n = 1; n < m_xTableTable->size(); ++n )
240  if( (*m_xTableTable)[ n ].GetName() > aFormatName )
241  break;
242 
243  m_xTableTable->InsertAutoFormat(n, std::move(pNewData));
244  m_xLbFormat->insert_text(m_nDfltStylePos + n, aFormatName);
245  m_xLbFormat->select(m_nDfltStylePos + n);
246  bFormatInserted = true;
247  m_xBtnAdd->set_sensitive(false);
248  if ( !m_bCoreDataChanged )
249  {
250  m_xBtnCancel->set_label(m_aStrClose);
251  m_bCoreDataChanged = true;
252  }
253 
254  SelFormatHdl(*m_xLbFormat);
255  bOk = true;
256  }
257  }
258 
259  if( !bFormatInserted )
260  {
261  std::unique_ptr<weld::MessageDialog> xBox(Application::CreateMessageDialog(m_xDialog.get(), VclMessageType::Error, VclButtonsType::OkCancel, m_aStrInvalidFormat));
262  bOk = RET_CANCEL == xBox->run();
263  }
264  }
265  else
266  bOk = true;
267  }
268 }
269 
271 {
272  OUString aMessage = m_aStrDelMsg + "\n\n" +
273  m_xLbFormat->get_selected_text() + "\n";
274 
275  std::unique_ptr<weld::MessageDialog> xBox(Application::CreateMessageDialog(m_xDialog.get(), VclMessageType::Question,
276  VclButtonsType::OkCancel, m_aStrDelTitle));
277  xBox->set_secondary_text(aMessage);
278 
279  if (xBox->run() == RET_OK)
280  {
281  sal_uInt8 nIndex = m_nIndex;
282 
283  m_xLbFormat->remove(m_nDfltStylePos + nIndex);
284  m_xLbFormat->select(m_nDfltStylePos + nIndex - 1);
285 
286  m_xTableTable->EraseAutoFormat(nIndex);
287  m_nIndex = nIndex - 1;
288 
289  if( !m_nIndex )
290  {
291  m_xBtnRemove->set_sensitive(false);
292  m_xBtnRename->set_sensitive(false);
293  }
294 
295  if( !m_bCoreDataChanged )
296  {
297  m_xBtnCancel->set_label(m_aStrClose);
298  m_bCoreDataChanged = true;
299  }
300  }
301 
302  SelFormatHdl(*m_xLbFormat);
303 }
304 
306 {
307  bool bOk = false;
308  while( !bOk )
309  {
310  SwStringInputDlg aDlg(m_xDialog.get(), m_aStrRenameTitle, m_aStrLabel, m_xLbFormat->get_selected_text());
311  if (aDlg.run() == RET_OK)
312  {
313  bool bFormatRenamed = false;
314  const OUString aFormatName(aDlg.GetInputString());
315 
316  if ( !aFormatName.isEmpty() )
317  {
318  size_t n;
319  for( n = 0; n < m_xTableTable->size(); ++n )
320  if ((*m_xTableTable)[n].GetName() == aFormatName)
321  break;
322 
323  if( n >= m_xTableTable->size() )
324  {
325  sal_uInt8 nIndex = m_nIndex;
326 
327  // no format with this name exists, so rename it
328  m_xLbFormat->remove(m_nDfltStylePos + nIndex);
329  std::unique_ptr<SwTableAutoFormat> p(
330  m_xTableTable->ReleaseAutoFormat(nIndex));
331 
332  p->SetName( aFormatName );
333 
334  // keep all arrays sorted!
335  for( n = 1; n < m_xTableTable->size(); ++n )
336  if ((*m_xTableTable)[n].GetName() > aFormatName)
337  {
338  break;
339  }
340 
341  m_xTableTable->InsertAutoFormat( n, std::move(p) );
342  m_xLbFormat->insert_text(m_nDfltStylePos + n, aFormatName);
343  m_xLbFormat->select(m_nDfltStylePos + n);
344 
345  if ( !m_bCoreDataChanged )
346  {
347  m_xBtnCancel->set_label(m_aStrClose);
348  m_bCoreDataChanged = true;
349  }
350 
351  SelFormatHdl(*m_xLbFormat);
352  bOk = true;
353  bFormatRenamed = true;
354  }
355  }
356 
357  if( !bFormatRenamed )
358  {
359  std::unique_ptr<weld::MessageDialog> xBox(Application::CreateMessageDialog(m_xDialog.get(), VclMessageType::Error, VclButtonsType::OkCancel, m_aStrInvalidFormat));
360  bOk = RET_CANCEL == xBox->run();
361  }
362  }
363  else
364  bOk = true;
365  }
366 }
367 
369 {
370  bool bBtnEnable = false;
371  sal_uInt8 nOldIdx = m_nIndex;
372  int nSelPos = m_xLbFormat->get_selected_index();
373  if (nSelPos >= m_nDfltStylePos)
374  {
375  m_nIndex = nSelPos - m_nDfltStylePos;
376  m_aWndPreview.NotifyChange((*m_xTableTable)[m_nIndex]);
377  bBtnEnable = 0 != m_nIndex;
378  UpdateChecks( (*m_xTableTable)[m_nIndex], true );
379  }
380  else
381  {
382  m_nIndex = 255;
383 
384  SwTableAutoFormat aTmp( SwViewShell::GetShellRes()->aStrNone );
385  aTmp.SetFont( false );
386  aTmp.SetJustify( false );
387  aTmp.SetFrame( false );
388  aTmp.SetBackground( false );
389  aTmp.SetValueFormat( false );
390  aTmp.SetWidthHeight( false );
391 
392  if (nOldIdx != m_nIndex)
393  m_aWndPreview.NotifyChange(aTmp);
394  UpdateChecks( aTmp, false );
395  }
396 
397  m_xBtnRemove->set_sensitive(bBtnEnable);
398  m_xBtnRename->set_sensitive(bBtnEnable);
399 }
400 
402 {
403  short nRet = SfxDialogController::run();
404  if (nRet == RET_OK && m_bSetAutoFormat)
406  return nRet;
407 }
408 
409 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
std::unique_ptr< SwTableAutoFormat > FillAutoFormatOfIndex() const
Definition: tautofmt.cxx:169
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:2480
bool m_bCoreDataChanged
Definition: tautofmt.hxx:49
IMPL_LINK_NOARG(SwAutoFormatDlg, AddHdl, weld::Button &, void)
Definition: tautofmt.cxx:213
void SetFrame(const bool bNew)
Definition: tblafmt.hxx:232
sal_Int64 n
virtual ~SwAutoFormatDlg() override
Definition: tautofmt.cxx:100
const ContentProperties & rData
sal_uInt8 m_nIndex
Definition: tautofmt.hxx:47
RET_CANCEL
std::unique_ptr< weld::CheckButton > m_xBtnFont
Definition: tautofmt.hxx:58
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
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:401
void SetJustify(const bool bNew)
Definition: tblafmt.hxx:231
void Init(const SwTableAutoFormat *pSelFormat)
Definition: tautofmt.cxx:113
int i
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:180
bool const m_bSetAutoFormat
Definition: tautofmt.hxx:50
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:60
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:151