LibreOffice Module sc (master)  1
scuiautofmt.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 #undef SC_DLLIMPLEMENTATION
21 
22 #include <vcl/svapp.hxx>
23 #include <vcl/weld.hxx>
24 #include <sfx2/strings.hrc>
25 #include <sfx2/sfxresid.hxx>
26 #include <strings.hrc>
27 #include <global.hxx>
28 #include <globstr.hrc>
29 #include <autoform.hxx>
30 #include <strindlg.hxx>
31 #include <scuiautofmt.hxx>
32 #include <scresid.hxx>
33 #include <helpids.h>
34 
35 // AutoFormat-Dialog:
36 
38  ScAutoFormat* pAutoFormat,
39  const ScAutoFormatData* pSelFormatData,
40  const ScViewData *pViewData)
41  : GenericDialogController(pParent, "modules/scalc/ui/autoformattable.ui", "AutoFormatTableDialog")
42  , aStrTitle(ScResId(STR_ADD_AUTOFORMAT_TITLE))
43  , aStrLabel(ScResId(STR_ADD_AUTOFORMAT_LABEL))
44  , aStrClose(ScResId(STR_BTN_AUTOFORMAT_CLOSE))
45  , aStrDelMsg(ScResId(STR_DEL_AUTOFORMAT_MSG))
46  , aStrRename(ScResId(STR_RENAME_AUTOFORMAT_TITLE))
47  , pFormat(pAutoFormat)
48  , pSelFmtData(pSelFormatData)
49  , nIndex(0)
50  , bCoreDataChanged(false)
51  , bFmtInserted(false)
52  , m_xLbFormat(m_xBuilder->weld_tree_view("formatlb"))
53  , m_xBtnOk(m_xBuilder->weld_button("ok"))
54  , m_xBtnCancel(m_xBuilder->weld_button("cancel"))
55  , m_xBtnAdd(m_xBuilder->weld_button("add"))
56  , m_xBtnRemove(m_xBuilder->weld_button("remove"))
57  , m_xBtnRename(m_xBuilder->weld_button("rename"))
58  , m_xBtnNumFormat(m_xBuilder->weld_check_button("numformatcb"))
59  , m_xBtnBorder(m_xBuilder->weld_check_button("bordercb"))
60  , m_xBtnFont(m_xBuilder->weld_check_button("fontcb"))
61  , m_xBtnPattern(m_xBuilder->weld_check_button("patterncb"))
62  , m_xBtnAlignment(m_xBuilder->weld_check_button("alignmentcb"))
63  , m_xBtnAdjust(m_xBuilder->weld_check_button("autofitcb"))
64  , m_xWndPreview(new weld::CustomWeld(*m_xBuilder, "preview", m_aWndPreview))
65 {
66  m_aWndPreview.DetectRTL(pViewData);
67 
68  const int nWidth = m_xLbFormat->get_approximate_digit_width() * 32;
69  const int nHeight = m_xLbFormat->get_height_rows(8);
70  m_xLbFormat->set_size_request(nWidth, nHeight);
71  m_xWndPreview->set_size_request(nWidth, nHeight);
72 
73  Init();
75  m_aWndPreview.NotifyChange(it->second.get());
76 }
77 
79 {
80 }
81 
83 {
84  m_xLbFormat->connect_changed( LINK( this, ScAutoFormatDlg, SelFmtHdl ) );
85  m_xBtnNumFormat->connect_toggled( LINK( this, ScAutoFormatDlg, CheckHdl ) );
86  m_xBtnBorder->connect_toggled( LINK( this, ScAutoFormatDlg, CheckHdl ) );
87  m_xBtnFont->connect_toggled( LINK( this, ScAutoFormatDlg, CheckHdl ) );
88  m_xBtnPattern->connect_toggled( LINK( this, ScAutoFormatDlg, CheckHdl ) );
89  m_xBtnAlignment->connect_toggled( LINK( this, ScAutoFormatDlg, CheckHdl ) );
90  m_xBtnAdjust->connect_toggled( LINK( this, ScAutoFormatDlg, CheckHdl ) );
91  m_xBtnAdd->connect_clicked ( LINK( this, ScAutoFormatDlg, AddHdl ) );
92  m_xBtnRemove->connect_clicked ( LINK( this, ScAutoFormatDlg, RemoveHdl ) );
93  m_xBtnOk->connect_clicked ( LINK( this, ScAutoFormatDlg, CloseHdl ) );
94  m_xBtnCancel->connect_clicked ( LINK( this, ScAutoFormatDlg, CloseHdl ) );
95  m_xBtnRename->connect_clicked ( LINK( this, ScAutoFormatDlg, RenameHdl ) );
96  m_xLbFormat->connect_row_activated( LINK( this, ScAutoFormatDlg, DblClkHdl ) );
97 
98  for (const auto& rEntry : *pFormat)
99  m_xLbFormat->append_text(rEntry.second->GetName());
100 
101  if (pFormat->size() == 1)
102  m_xBtnRemove->set_sensitive(false);
103 
104  m_xLbFormat->select(0);
105  m_xBtnRename->set_sensitive(false);
106  m_xBtnRemove->set_sensitive(false);
107 
108  nIndex = 0;
109  UpdateChecks();
110 
111  if ( !pSelFmtData )
112  {
113  m_xBtnAdd->set_sensitive(false);
114  m_xBtnRemove->set_sensitive(false);
115  bFmtInserted = true;
116  }
117 }
118 
120 {
122 
123  m_xBtnNumFormat->set_active( pData->GetIncludeValueFormat() );
124  m_xBtnBorder->set_active( pData->GetIncludeFrame() );
125  m_xBtnFont->set_active( pData->GetIncludeFont() );
126  m_xBtnPattern->set_active( pData->GetIncludeBackground() );
127  m_xBtnAlignment->set_active( pData->GetIncludeJustify() );
128  m_xBtnAdjust->set_active( pData->GetIncludeWidthHeight() );
129 }
130 
131 // Handler:
132 
133 IMPL_LINK(ScAutoFormatDlg, CloseHdl, weld::Button&, rBtn, void)
134 {
135  if (&rBtn == m_xBtnOk.get() || &rBtn == m_xBtnCancel.get())
136  {
137  if ( bCoreDataChanged )
139 
140  m_xDialog->response( (&rBtn == m_xBtnOk.get()) ? RET_OK : RET_CANCEL );
141  }
142 }
143 
145 {
146  if ( bCoreDataChanged )
148 
149  m_xDialog->response( RET_OK );
150 
151  return true;
152 }
153 
155 {
156  ScAutoFormatData* pData = pFormat->findByIndex(nIndex);
157  bool bCheck = rBtn.get_active();
158 
159  if (&rBtn == m_xBtnNumFormat.get())
160  pData->SetIncludeValueFormat( bCheck );
161  else if (&rBtn == m_xBtnBorder.get())
162  pData->SetIncludeFrame( bCheck );
163  else if (&rBtn == m_xBtnFont.get())
164  pData->SetIncludeFont( bCheck );
165  else if (&rBtn == m_xBtnPattern.get())
166  pData->SetIncludeBackground( bCheck );
167  else if (&rBtn == m_xBtnAlignment.get())
168  pData->SetIncludeJustify( bCheck );
169  else if (&rBtn == m_xBtnAdjust.get())
170  pData->SetIncludeWidthHeight( bCheck );
171 
172  if ( !bCoreDataChanged )
173  {
174  m_xBtnCancel->set_label(aStrClose);
175  bCoreDataChanged = true;
176  }
177 
178  m_aWndPreview.NotifyChange( pData );
179 }
180 
182 {
183  if ( !bFmtInserted && pSelFmtData )
184  {
185  OUString aStrStandard( SfxResId(STR_STANDARD) );
186  OUString aFormatName;
187  bool bOk = false;
188 
189  while ( !bOk )
190  {
191  ScStringInputDlg aDlg(m_xDialog.get(), aStrTitle, aStrLabel, aFormatName,
193 
194  if (aDlg.run() == RET_OK)
195  {
196  aFormatName = aDlg.GetInputString();
197 
198  if ( !aFormatName.isEmpty() && aFormatName != aStrStandard && pFormat->find(aFormatName) == pFormat->end() )
199  {
200  std::unique_ptr<ScAutoFormatData> pNewData(
201  new ScAutoFormatData( *pSelFmtData ));
202 
203  pNewData->SetName( aFormatName );
204  ScAutoFormat::iterator it = pFormat->insert(std::move(pNewData));
205  bFmtInserted = it != pFormat->end();
206 
207  if ( bFmtInserted )
208  {
209  size_t nPos = std::distance(pFormat->begin(), it);
210  m_xLbFormat->insert_text(nPos, aFormatName);
211  m_xLbFormat->select_text( aFormatName );
212  m_xBtnAdd->set_sensitive(false);
213 
214  if ( !bCoreDataChanged )
215  {
216  m_xBtnCancel->set_label( aStrClose );
217  bCoreDataChanged = true;
218  }
219 
220  SelFmtHdl( *m_xLbFormat );
221  bOk = true;
222  }
223  }
224 
225  if ( !bFmtInserted )
226  {
227  std::unique_ptr<weld::MessageDialog> xBox(Application::CreateMessageDialog(m_xDialog.get(),
228  VclMessageType::Error, VclButtonsType::OkCancel,
229  ScResId(STR_INVALID_AFNAME)));
230 
231  sal_uInt16 nRet = xBox->run();
232 
233  bOk = ( nRet == RET_CANCEL );
234  }
235  }
236  else
237  bOk = true;
238  }
239  }
240 }
241 
243 {
244  if ( (nIndex > 0) && (m_xLbFormat->n_children() > 0) )
245  {
246  OUString aMsg = aStrDelMsg.getToken( 0, '#' )
247  + m_xLbFormat->get_selected_text()
248  + aStrDelMsg.getToken( 1, '#' );
249 
250  std::unique_ptr<weld::MessageDialog> xQueryBox(Application::CreateMessageDialog(m_xDialog.get(),
251  VclMessageType::Question, VclButtonsType::YesNo,
252  aMsg));
253  xQueryBox->set_default_response(RET_YES);
254 
255  if (RET_YES == xQueryBox->run())
256  {
257  m_xLbFormat->remove(nIndex);
258  m_xLbFormat->select(nIndex-1);
259 
260  if ( nIndex-1 == 0 )
261  m_xBtnRemove->set_sensitive(false);
262 
263  if ( !bCoreDataChanged )
264  {
265  m_xBtnCancel->set_label( aStrClose );
266  bCoreDataChanged = true;
267  }
268 
269  ScAutoFormat::iterator it = pFormat->begin();
270  std::advance(it, nIndex);
271  pFormat->erase(it);
272  nIndex--;
273 
274  SelFmtHdl( *m_xLbFormat );
275  }
276  }
277 
278  SelFmtHdl( *m_xLbFormat );
279 }
280 
282 {
283  bool bOk = false;
284  while( !bOk )
285  {
286 
287  OUString aFormatName = m_xLbFormat->get_selected_text();
288  OUString aEntry;
289 
290  ScStringInputDlg aDlg(m_xDialog.get(), aStrRename, aStrLabel, aFormatName,
292  if (aDlg.run() == RET_OK)
293  {
294  bool bFmtRenamed = false;
295  aFormatName = aDlg.GetInputString();
296 
297  if (!aFormatName.isEmpty())
298  {
299  ScAutoFormat::iterator it = pFormat->begin(), itEnd = pFormat->end();
300  for (; it != itEnd; ++it)
301  {
302  aEntry = it->second->GetName();
303  if (aFormatName == aEntry)
304  break;
305  }
306  if (it == itEnd)
307  {
308  // no format with this name yet, so we can rename
309 
310  m_xLbFormat->remove(nIndex);
311  const ScAutoFormatData* p = pFormat->findByIndex(nIndex);
312  std::unique_ptr<ScAutoFormatData> pNewData(new ScAutoFormatData(*p));
313 
314  it = pFormat->begin();
315  std::advance(it, nIndex);
316  pFormat->erase(it);
317 
318  pNewData->SetName( aFormatName );
319 
320  pFormat->insert(std::move(pNewData));
321 
322  m_xLbFormat->freeze();
323  m_xLbFormat->clear();
324  for (it = pFormat->begin(); it != itEnd; ++it)
325  {
326  aEntry = it->second->GetName();
327  m_xLbFormat->append_text(aEntry);
328  }
329 
330  m_xLbFormat->thaw();
331  m_xLbFormat->select_text(aFormatName);
332 
333  if ( !bCoreDataChanged )
334  {
335  m_xBtnCancel->set_label( aStrClose );
336  bCoreDataChanged = true;
337  }
338 
339  SelFmtHdl( *m_xLbFormat );
340  bOk = true;
341  bFmtRenamed = true;
342  }
343  }
344  if( !bFmtRenamed )
345  {
346  std::unique_ptr<weld::MessageDialog> xBox(Application::CreateMessageDialog(m_xDialog.get(),
347  VclMessageType::Error, VclButtonsType::OkCancel,
348  ScResId(STR_INVALID_AFNAME)));
349 
350  bOk = RET_CANCEL == xBox->run();
351  }
352  }
353  else
354  bOk = true;
355  }
356 }
357 
359 {
360  nIndex = m_xLbFormat->get_selected_index();
361  UpdateChecks();
362 
363  if ( nIndex == 0 )
364  {
365  m_xBtnRename->set_sensitive(false);
366  m_xBtnRemove->set_sensitive(false);
367  }
368  else
369  {
370  m_xBtnRename->set_sensitive(true);
371  m_xBtnRemove->set_sensitive(true);
372  }
373 
374  ScAutoFormatData* p = pFormat->findByIndex(nIndex);
375  m_aWndPreview.NotifyChange(p);
376 }
377 
379 {
381  return p ? p->GetName() : OUString();
382 }
383 
384 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_Int32 nIndex
std::unique_ptr< weld::CheckButton > m_xBtnNumFormat
Definition: scuiautofmt.hxx:57
std::unique_ptr< weld::Button > m_xBtnCancel
Definition: scuiautofmt.hxx:53
std::unique_ptr< ContentProperties > pData
void SetIncludeValueFormat(bool bValueFormat)
Definition: autoform.hxx:150
const OUString & GetName() const
Definition: autoform.hxx:141
OUString GetCurrFormatName()
void SetIncludeWidthHeight(bool bWidthHeight)
Definition: autoform.hxx:155
RET_CANCEL
std::unique_ptr< weld::TreeView > m_xLbFormat
Definition: scuiautofmt.hxx:51
std::unique_ptr< weld::CheckButton > m_xBtnBorder
Definition: scuiautofmt.hxx:58
#define HID_SC_REN_AFMT_NAME
Definition: helpids.h:50
std::unique_ptr< weld::Button > m_xBtnRemove
Definition: scuiautofmt.hxx:55
RET_YES
bool GetIncludeWidthHeight() const
Definition: autoform.hxx:148
MapType::iterator iterator
Definition: autoform.hxx:192
IMPL_LINK_NOARG(ScAutoFormatDlg, DblClkHdl, weld::TreeView &, bool)
ScAutoFmtPreview m_aWndPreview
Definition: scuiautofmt.hxx:50
const_iterator begin() const
Definition: autoform.cxx:814
std::unique_ptr< weld::CheckButton > m_xBtnAdjust
Definition: scuiautofmt.hxx:62
void NotifyChange(ScAutoFormatData *pNewData)
Definition: autofmt.cxx:481
#define HID_SC_REN_AFMT_DLG
Definition: helpids.h:55
bool GetIncludeBackground() const
Definition: autoform.hxx:147
void SetIncludeJustify(bool bJustify)
Definition: autoform.hxx:152
std::unique_ptr< weld::Button > m_xBtnAdd
Definition: scuiautofmt.hxx:54
std::unique_ptr< weld::Button > m_xBtnOk
Definition: scuiautofmt.hxx:52
OUString ScResId(const char *pId)
Definition: scdll.cxx:95
void DetectRTL(const ScViewData *pViewData)
Definition: autofmt.cxx:425
ScAutoFormatDlg(weld::Window *pParent, ScAutoFormat *pAutoFormat, const ScAutoFormatData *pSelFormatData, const ScViewData *pViewData)
Definition: scuiautofmt.cxx:37
const ScAutoFormatData * findByIndex(size_t nIndex) const
Definition: autoform.cxx:773
void SetIncludeBackground(bool bBackground)
Definition: autoform.hxx:154
std::unique_ptr< weld::CheckButton > m_xBtnAlignment
Definition: scuiautofmt.hxx:61
void SetIncludeFont(bool bFont)
Definition: autoform.hxx:151
bool Save()
Definition: autoform.cxx:890
bool GetIncludeFrame() const
Definition: autoform.hxx:146
std::unique_ptr< weld::CheckButton > m_xBtnPattern
Definition: scuiautofmt.hxx:60
std::unique_ptr< weld::CheckButton > m_xBtnFont
Definition: scuiautofmt.hxx:59
bool GetIncludeFont() const
Definition: autoform.hxx:144
sal_uInt16 nIndex
Definition: scuiautofmt.hxx:46
#define HID_SC_ADD_AUTOFMT
Definition: helpids.h:46
OUString GetInputString() const
Definition: strindlg.hxx:38
RET_OK
Reference< XExecutableDialog > m_xDialog
void * p
IMPL_LINK(ScAutoFormatDlg, CloseHdl, weld::Button &, rBtn, void)
std::unique_ptr< weld::Button > m_xBtnRename
Definition: scuiautofmt.hxx:56
virtual ~ScAutoFormatDlg() override
Definition: scuiautofmt.cxx:78
ScAutoFormat * pFormat
Definition: scuiautofmt.hxx:44
#define HID_SC_AUTOFMT_NAME
Definition: helpids.h:47
const ScAutoFormatData * pSelFmtData
Definition: scuiautofmt.hxx:45
bool GetIncludeValueFormat() const
Definition: autoform.hxx:143
static SC_DLLPUBLIC ScAutoFormat * GetOrCreateAutoFormat()
Definition: global.cxx:255
bool GetIncludeJustify() const
Definition: autoform.hxx:145
void SetIncludeFrame(bool bFrame)
Definition: autoform.hxx:153
static weld::MessageDialog * CreateMessageDialog(weld::Widget *pParent, VclMessageType eMessageType, VclButtonsType eButtonType, const OUString &rPrimaryMessage, bool bMobile=false)
OUString SfxResId(const char *pId)
std::unique_ptr< weld::CustomWeld > m_xWndPreview
Definition: scuiautofmt.hxx:63
sal_uInt16 nPos