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