tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / sc / source / ui / miscdlgs / scuiautofmt.cxx
blob94e38ee77fd14141560af72596f152b499569666
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #undef SC_DLLIMPLEMENTATION
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>
36 // AutoFormat-Dialog:
38 ScAutoFormatDlg::ScAutoFormatDlg(weld::Window* pParent,
39 ScAutoFormat* pAutoFormat,
40 const ScAutoFormatData* pSelFormatData,
41 const ScViewData& rViewData)
42 : GenericDialogController(pParent, u"modules/scalc/ui/autoformattable.ui"_ustr, u"AutoFormatTableDialog"_ustr)
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(u"formatlb"_ustr))
54 , m_xBtnOk(m_xBuilder->weld_button(u"ok"_ustr))
55 , m_xBtnCancel(m_xBuilder->weld_button(u"cancel"_ustr))
56 , m_xBtnAdd(m_xBuilder->weld_button(u"add"_ustr))
57 , m_xBtnRemove(m_xBuilder->weld_button(u"remove"_ustr))
58 , m_xBtnRename(m_xBuilder->weld_button(u"rename"_ustr))
59 , m_xBtnNumFormat(m_xBuilder->weld_check_button(u"numformatcb"_ustr))
60 , m_xBtnBorder(m_xBuilder->weld_check_button(u"bordercb"_ustr))
61 , m_xBtnFont(m_xBuilder->weld_check_button(u"fontcb"_ustr))
62 , m_xBtnPattern(m_xBuilder->weld_check_button(u"patterncb"_ustr))
63 , m_xBtnAlignment(m_xBuilder->weld_check_button(u"alignmentcb"_ustr))
64 , m_xBtnAdjust(m_xBuilder->weld_check_button(u"autofitcb"_ustr))
65 , m_xWndPreview(new weld::CustomWeld(*m_xBuilder, u"preview"_ustr, m_aWndPreview))
67 m_aWndPreview.DetectRTL(rViewData);
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);
74 Init();
75 ScAutoFormat::iterator it = pFormat->begin();
76 m_aWndPreview.NotifyChange(it->second.get());
79 ScAutoFormatDlg::~ScAutoFormatDlg()
83 void ScAutoFormatDlg::Init()
85 m_xLbFormat->connect_selection_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 ) );
99 for (const auto& rEntry : *pFormat)
100 m_xLbFormat->append_text(rEntry.second->GetName());
102 if (pFormat->size() == 1)
103 m_xBtnRemove->set_sensitive(false);
105 m_xLbFormat->select(0);
106 m_xBtnRename->set_sensitive(false);
107 m_xBtnRemove->set_sensitive(false);
109 nIndex = 0;
110 UpdateChecks();
112 if ( !pSelFmtData )
114 m_xBtnAdd->set_sensitive(false);
115 m_xBtnRemove->set_sensitive(false);
116 bFmtInserted = true;
120 void ScAutoFormatDlg::UpdateChecks()
122 const ScAutoFormatData* pData = pFormat->findByIndex(nIndex);
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() );
132 // Handler:
134 IMPL_LINK(ScAutoFormatDlg, CloseHdl, weld::Button&, rBtn, void)
136 if (&rBtn == m_xBtnOk.get() || &rBtn == m_xBtnCancel.get())
138 if ( bCoreDataChanged )
139 ScGlobal::GetOrCreateAutoFormat()->Save();
141 m_xDialog->response( (&rBtn == m_xBtnOk.get()) ? RET_OK : RET_CANCEL );
145 IMPL_LINK_NOARG(ScAutoFormatDlg, DblClkHdl, weld::TreeView&, bool)
147 if ( bCoreDataChanged )
148 ScGlobal::GetOrCreateAutoFormat()->Save();
150 m_xDialog->response( RET_OK );
152 return true;
155 IMPL_LINK(ScAutoFormatDlg, CheckHdl, weld::Toggleable&, rBtn, void)
157 ScAutoFormatData* pData = pFormat->findByIndex(nIndex);
158 bool bCheck = rBtn.get_active();
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 );
173 if ( !bCoreDataChanged )
175 m_xBtnCancel->set_label(aStrClose);
176 bCoreDataChanged = true;
179 m_aWndPreview.NotifyChange( pData );
182 IMPL_LINK_NOARG(ScAutoFormatDlg, AddHdl, weld::Button&, void)
184 if ( bFmtInserted || !pSelFmtData )
185 return;
187 OUString aStrStandard( SfxResId(STR_STANDARD) );
188 OUString aFormatName;
189 bool bOk = false;
191 while ( !bOk )
193 ScStringInputDlg aDlg(m_xDialog.get(), aStrTitle, aStrLabel, aFormatName,
194 HID_SC_ADD_AUTOFMT, HID_SC_AUTOFMT_NAME);
196 if (aDlg.run() == RET_OK)
198 aFormatName = aDlg.GetInputString();
200 if ( !aFormatName.isEmpty() && aFormatName != aStrStandard && pFormat->find(aFormatName) == pFormat->end() )
202 std::unique_ptr<ScAutoFormatData> pNewData(
203 new ScAutoFormatData( *pSelFmtData ));
205 pNewData->SetName( aFormatName );
206 ScAutoFormat::iterator it = pFormat->insert(std::move(pNewData));
207 bFmtInserted = it != pFormat->end();
209 if ( bFmtInserted )
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);
216 if ( !bCoreDataChanged )
218 m_xBtnCancel->set_label( aStrClose );
219 bCoreDataChanged = true;
222 SelFmtHdl( *m_xLbFormat );
223 bOk = true;
227 if ( !bFmtInserted )
229 std::unique_ptr<weld::MessageDialog> xBox(Application::CreateMessageDialog(m_xDialog.get(),
230 VclMessageType::Error, VclButtonsType::OkCancel,
231 ScResId(STR_INVALID_AFNAME)));
233 sal_uInt16 nRet = xBox->run();
235 bOk = ( nRet == RET_CANCEL );
238 else
239 bOk = true;
243 IMPL_LINK_NOARG(ScAutoFormatDlg, RemoveHdl, weld::Button&, void)
245 if ( (nIndex > 0) && (m_xLbFormat->n_children() > 0) )
247 OUString aMsg = o3tl::getToken(aStrDelMsg, 0, '#' )
248 + m_xLbFormat->get_selected_text()
249 + o3tl::getToken(aStrDelMsg, 1, '#' );
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);
256 if (RET_YES == xQueryBox->run())
258 m_xLbFormat->remove(nIndex);
259 m_xLbFormat->select(nIndex-1);
261 if ( nIndex-1 == 0 )
262 m_xBtnRemove->set_sensitive(false);
264 if ( !bCoreDataChanged )
266 m_xBtnCancel->set_label( aStrClose );
267 bCoreDataChanged = true;
270 ScAutoFormat::iterator it = pFormat->begin();
271 std::advance(it, nIndex);
272 pFormat->erase(it);
273 nIndex--;
275 SelFmtHdl( *m_xLbFormat );
279 SelFmtHdl( *m_xLbFormat );
282 IMPL_LINK_NOARG(ScAutoFormatDlg, RenameHdl, weld::Button&, void)
284 bool bOk = false;
285 while( !bOk )
288 OUString aFormatName = m_xLbFormat->get_selected_text();
289 OUString aEntry;
291 ScStringInputDlg aDlg(m_xDialog.get(), aStrRename, aStrLabel, aFormatName,
292 HID_SC_REN_AFMT_DLG, HID_SC_REN_AFMT_NAME);
293 if (aDlg.run() == RET_OK)
295 bool bFmtRenamed = false;
296 aFormatName = aDlg.GetInputString();
298 if (!aFormatName.isEmpty())
300 ScAutoFormat::iterator it = pFormat->begin(), itEnd = pFormat->end();
301 for (; it != itEnd; ++it)
303 aEntry = it->second->GetName();
304 if (aFormatName == aEntry)
305 break;
307 if (it == itEnd)
309 // no format with this name yet, so we can rename
311 m_xLbFormat->remove(nIndex);
312 const ScAutoFormatData* p = pFormat->findByIndex(nIndex);
313 std::unique_ptr<ScAutoFormatData> pNewData(new ScAutoFormatData(*p));
315 it = pFormat->begin();
316 std::advance(it, nIndex);
317 pFormat->erase(it);
319 pNewData->SetName( aFormatName );
321 pFormat->insert(std::move(pNewData));
323 m_xLbFormat->freeze();
324 m_xLbFormat->clear();
325 for (it = pFormat->begin(); it != itEnd; ++it)
327 aEntry = it->second->GetName();
328 m_xLbFormat->append_text(aEntry);
331 m_xLbFormat->thaw();
332 m_xLbFormat->select_text(aFormatName);
334 if ( !bCoreDataChanged )
336 m_xBtnCancel->set_label( aStrClose );
337 bCoreDataChanged = true;
340 SelFmtHdl( *m_xLbFormat );
341 bOk = true;
342 bFmtRenamed = true;
345 if( !bFmtRenamed )
347 std::unique_ptr<weld::MessageDialog> xBox(Application::CreateMessageDialog(m_xDialog.get(),
348 VclMessageType::Error, VclButtonsType::OkCancel,
349 ScResId(STR_INVALID_AFNAME)));
351 bOk = RET_CANCEL == xBox->run();
354 else
355 bOk = true;
359 IMPL_LINK_NOARG(ScAutoFormatDlg, SelFmtHdl, weld::TreeView&, void)
361 nIndex = m_xLbFormat->get_selected_index();
362 UpdateChecks();
364 if ( nIndex == 0 )
366 m_xBtnRename->set_sensitive(false);
367 m_xBtnRemove->set_sensitive(false);
369 else
371 m_xBtnRename->set_sensitive(true);
372 m_xBtnRemove->set_sensitive(true);
375 ScAutoFormatData* p = pFormat->findByIndex(nIndex);
376 m_aWndPreview.NotifyChange(p);
379 OUString ScAutoFormatDlg::GetCurrFormatName()
381 const ScAutoFormatData* p = pFormat->findByIndex(nIndex);
382 return p ? p->GetName() : OUString();
385 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */