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