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