tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / sc / source / ui / view / spelleng.cxx
blobf883f17e5611f2d2ec6eeece6f003e3fe2838725
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 #include <spelleng.hxx>
21 #include <com/sun/star/i18n/TextConversionOption.hpp>
23 #include <scitems.hxx>
25 #include <editeng/langitem.hxx>
26 #include <editeng/editobj.hxx>
27 #include <editeng/editview.hxx>
28 #include <sfx2/viewfrm.hxx>
29 #include <utility>
30 #include <vcl/settings.hxx>
31 #include <vcl/svapp.hxx>
32 #include <vcl/weld.hxx>
33 #include <osl/diagnose.h>
35 #include <spelldialog.hxx>
36 #include <tabvwsh.hxx>
37 #include <docsh.hxx>
38 #include <cellvalue.hxx>
39 #include <cellform.hxx>
40 #include <patattr.hxx>
41 #include <globstr.hrc>
42 #include <scresid.hxx>
43 #include <markdata.hxx>
45 #include <memory>
47 using namespace ::com::sun::star;
49 ScConversionEngineBase::ScConversionEngineBase(
50 SfxItemPool* pEnginePoolP, ScViewData& rViewData,
51 ScDocument* pUndoDoc, ScDocument* pRedoDoc ) :
52 ScEditEngineDefaulter( pEnginePoolP ),
53 mrViewData( rViewData ),
54 mrDocShell( *rViewData.GetDocShell() ),
55 mrDoc( rViewData.GetDocShell()->GetDocument() ),
56 maSelState( rViewData ),
57 mpUndoDoc( pUndoDoc ),
58 mpRedoDoc( pRedoDoc ),
59 meCurrLang( LANGUAGE_ENGLISH_US ),
60 mbIsAnyModified( false ),
61 mbInitialState( true ),
62 mbWrappedInTable( false ),
63 mbFinished( false )
65 maSelState.GetCellCursor().GetVars( mnStartCol, mnStartRow, mnStartTab );
66 // start with cell A1 in cell/range/multi-selection, will seek to first selected
67 if( maSelState.GetSelectionType() == SC_SELECTTYPE_SHEET )
69 mnStartCol = 0;
70 mnStartRow = 0;
72 mnCurrCol = mnStartCol;
73 mnCurrRow = mnStartRow;
76 ScConversionEngineBase::~ScConversionEngineBase()
80 bool ScConversionEngineBase::FindNextConversionCell()
82 ScMarkData& rMark = mrViewData.GetMarkData();
83 ScTabViewShell* pViewShell = mrViewData.GetViewShell();
84 const ScPatternAttr* pPattern = nullptr;
85 const ScPatternAttr* pLastPattern = nullptr;
87 SfxItemSet aEditDefaults(GetEmptyItemSet());
89 if( IsModified() )
91 mbIsAnyModified = true;
93 OUString aNewStr = GetText();
95 // Check if the user has changed the language. If the new language is
96 // applied to the entire string length, we will set the language as cell
97 // attribute. Otherwise we will commit this as an edit-engine string.
98 editeng::LanguageSpan aLang = GetLanguage(0, 0);
100 bool bSimpleString = GetParagraphCount() == 1 &&
101 aLang.nLang != LANGUAGE_DONTKNOW &&
102 aLang.nStart == 0 &&
103 aLang.nEnd == aNewStr.getLength();
105 bool bMultiTab = (rMark.GetSelectCount() > 1);
107 OUString aVisibleStr;
108 if( bMultiTab )
109 aVisibleStr = mrDoc.GetString(mnCurrCol, mnCurrRow, mnStartTab);
111 for( SCTAB nTab = 0, nTabCount = mrDoc.GetTableCount(); nTab < nTabCount; ++nTab )
113 // always change the cell on the visible tab,
114 // on the other selected tabs only if they contain the same text
116 if ((nTab == mnStartTab) ||
117 (bMultiTab && rMark.GetTableSelect(nTab) && mrDoc.GetString(mnCurrCol, mnCurrRow, nTab) == aVisibleStr))
119 ScAddress aPos( mnCurrCol, mnCurrRow, nTab );
120 CellType eCellType = mrDoc.GetCellType( aPos );
121 bool bEmptyCell = eCellType == CELLTYPE_NONE;
123 if (mpUndoDoc && !bEmptyCell)
124 mrDoc.CopyCellToDocument(aPos, aPos, *mpUndoDoc);
126 if (!bSimpleString || eCellType == CELLTYPE_EDIT)
128 std::unique_ptr<EditTextObject> pEditObj(CreateTextObject());
129 mrDoc.SetEditText(aPos, *pEditObj, GetEditTextObjectPool());
131 else
133 // Set the new string and update the language with the cell.
134 mrDoc.SetString(aPos, aNewStr);
136 const ScPatternAttr* pAttr(mrDoc.GetPattern(aPos));
137 ScPatternAttr* pNewAttr(nullptr);
139 if (nullptr != pAttr)
140 pNewAttr = new ScPatternAttr(*pAttr);
141 else
142 pNewAttr = new ScPatternAttr(mrDoc.getCellAttributeHelper());
144 pNewAttr->GetItemSet().Put(SvxLanguageItem(aLang.nLang, ATTR_FONT_LANGUAGE));
145 mrDoc.SetPattern(aPos, CellAttributeHolder(pNewAttr, true));
148 if (mpRedoDoc && !bEmptyCell)
149 mrDoc.CopyCellToDocument(aPos, aPos, *mpRedoDoc);
151 mrDocShell.PostPaintCell(aPos);
156 SCCOL nNewCol = mnCurrCol;
157 SCROW nNewRow = mnCurrRow;
159 if( mbInitialState )
161 /* On very first call, decrement row to let GetNextSpellingCell() find
162 the first cell of current range. */
163 mbInitialState = false;
164 --nNewRow;
167 bool bSheetSel = maSelState.GetSelectionType() == SC_SELECTTYPE_SHEET;
168 bool bLoop = true;
169 bool bFound = false;
170 while( bLoop && !bFound )
172 bLoop = mrDoc.GetNextSpellingCell( nNewCol, nNewRow, mnStartTab, bSheetSel, rMark );
173 if( bLoop )
175 FillFromCell( mnCurrCol, mnCurrRow, mnStartTab );
177 if( mbWrappedInTable && ((nNewCol > mnStartCol) || ((nNewCol == mnStartCol) && (nNewRow >= mnStartRow))) )
179 ShowFinishDialog();
180 bLoop = false;
181 mbFinished = true;
183 else if( nNewCol >= mrDoc.GetAllocatedColumnsCount(mnStartTab) )
185 // no more cells in the sheet - try to restart at top of sheet
187 if( bSheetSel || ((mnStartCol == 0) && (mnStartRow == 0)) )
189 // conversion started at cell A1 or in selection, do not query to restart at top
190 ShowFinishDialog();
191 bLoop = false;
192 mbFinished = true;
194 else if( ShowTableWrapDialog() )
196 // conversion started anywhere but in cell A1, user wants to restart
197 nNewRow = mrDoc.MaxRow() + 2;
198 mbWrappedInTable = true;
200 else
202 bLoop = false;
203 mbFinished = true;
206 else
208 // GetPattern may implicitly allocates the column if not exists,
209 pPattern = mrDoc.GetPattern( nNewCol, nNewRow, mnStartTab );
210 if( pPattern && !ScPatternAttr::areSame(pPattern, pLastPattern) )
212 pPattern->FillEditItemSet( &aEditDefaults );
213 SetDefaults( aEditDefaults );
214 pLastPattern = pPattern;
217 // language changed?
218 const SfxPoolItem* pItem = mrDoc.GetAttr( nNewCol, nNewRow, mnStartTab, ATTR_FONT_LANGUAGE );
219 if( const SvxLanguageItem* pLangItem = dynamic_cast<const SvxLanguageItem*>( pItem ) )
221 LanguageType eLang = pLangItem->GetValue();
222 if( eLang == LANGUAGE_SYSTEM )
223 eLang = Application::GetSettings().GetLanguageTag().getLanguageType(); // never use SYSTEM for spelling
224 if( eLang != meCurrLang )
226 meCurrLang = eLang;
227 SetDefaultLanguage( eLang );
231 FillFromCell( nNewCol, nNewRow, mnStartTab );
233 bFound = bLoop && NeedsConversion();
238 if( bFound )
240 pViewShell->AlignToCursor( nNewCol, nNewRow, SC_FOLLOW_JUMP );
241 pViewShell->SetCursor( nNewCol, nNewRow, true );
242 mrViewData.GetView()->MakeEditView( this, nNewCol, nNewRow );
243 EditView* pEditView = mrViewData.GetSpellingView();
244 // maSelState.GetEditSelection() returns (0,0) if not in edit mode -> ok
245 pEditView->SetSelection( maSelState.GetEditSelection() );
247 ClearModifyFlag();
248 mnCurrCol = nNewCol;
249 mnCurrRow = nNewRow;
252 return bFound;
255 void ScConversionEngineBase::RestoreCursorPos()
257 const ScAddress& rPos = maSelState.GetCellCursor();
258 mrViewData.GetViewShell()->SetCursor( rPos.Col(), rPos.Row() );
261 bool ScConversionEngineBase::ShowTableWrapDialog()
263 // default: no dialog, always restart at top
264 return true;
267 void ScConversionEngineBase::ShowFinishDialog()
269 // default: no dialog
272 // private --------------------------------------------------------------------
274 void ScConversionEngineBase::FillFromCell( SCCOL nCol, SCROW nRow, SCTAB nTab )
276 ScAddress aPos(nCol, nRow, nTab);
278 ScRefCellValue aCell(mrDoc, aPos);
279 switch (aCell.getType())
281 case CELLTYPE_STRING:
283 sal_uInt32 nNumFmt = mrDoc.GetNumberFormat(ScRange(aPos));
284 const Color* pColor;
285 OUString aText = ScCellFormat::GetString(aCell, nNumFmt, &pColor, nullptr, mrDoc);
287 SetTextCurrentDefaults(aText);
289 break;
290 case CELLTYPE_EDIT:
292 const EditTextObject* pNewEditObj = aCell.getEditText();
293 SetTextCurrentDefaults(*pNewEditObj);
295 break;
296 default:
297 SetTextCurrentDefaults(OUString());
301 ScSpellingEngine::ScSpellingEngine(
302 SfxItemPool* pEnginePoolP, ScViewData& rViewData,
303 ScDocument* pUndoDoc, ScDocument* pRedoDoc,
304 css::uno::Reference< css::linguistic2::XSpellChecker1 > const & xSpeller ) :
305 ScConversionEngineBase( pEnginePoolP, rViewData, pUndoDoc, pRedoDoc )
307 SetSpeller( xSpeller );
310 void ScSpellingEngine::ConvertAll(weld::Widget* pDialogParent, EditView& rEditView)
312 EESpellState eState = EESpellState::Ok;
313 if( FindNextConversionCell() )
314 eState = rEditView.StartSpeller(pDialogParent, true);
316 OSL_ENSURE( eState != EESpellState::NoSpeller, "ScSpellingEngine::Convert - no spell checker" );
319 bool ScSpellingEngine::SpellNextDocument()
321 return FindNextConversionCell();
324 bool ScSpellingEngine::NeedsConversion()
326 return HasSpellErrors() != EESpellState::Ok;
329 bool ScSpellingEngine::ShowTableWrapDialog()
331 weld::Widget* pParent = GetDialogParent();
332 weld::WaitObject aWaitOff(pParent);
334 std::unique_ptr<weld::MessageDialog> xBox(Application::CreateMessageDialog(pParent,
335 VclMessageType::Question, VclButtonsType::YesNo,
336 ScResId(STR_SPELLING_BEGIN_TAB))); // "delete data?"
337 xBox->set_title(ScResId(STR_MSSG_DOSUBTOTALS_0));
338 xBox->set_default_response(RET_YES);
339 return xBox->run() == RET_YES;
342 void ScSpellingEngine::ShowFinishDialog()
344 weld::Widget* pParent = GetDialogParent();
345 weld::WaitObject aWaitOff(pParent);
346 std::unique_ptr<weld::MessageDialog> xInfoBox(Application::CreateMessageDialog(pParent,
347 VclMessageType::Info, VclButtonsType::Ok,
348 ScResId(STR_SPELLING_STOP_OK)));
349 xInfoBox->run();
352 weld::Widget* ScSpellingEngine::GetDialogParent()
354 sal_uInt16 nWinId = ScSpellDialogChildWindow::GetChildWindowId();
355 SfxViewFrame& rViewFrm = mrViewData.GetViewShell()->GetViewFrame();
356 if( rViewFrm.HasChildWindow( nWinId ) )
358 if( SfxChildWindow* pChild = rViewFrm.GetChildWindow( nWinId ) )
360 auto xController = pChild->GetController();
361 if (xController)
363 if (weld::Window *pRet = xController->getDialog())
365 if (pRet->get_visible())
366 return pRet;
372 // fall back to standard dialog parent
373 return ScDocShell::GetActiveDialogParent();
376 ScConversionParam::ScConversionParam( ScConversionType eConvType ) :
377 meConvType( eConvType ),
378 meSourceLang( LANGUAGE_NONE ),
379 meTargetLang( LANGUAGE_NONE ),
380 mnOptions( 0 ),
381 mbUseTargetFont( false ),
382 mbIsInteractive( false )
386 ScConversionParam::ScConversionParam( ScConversionType eConvType,
387 LanguageType eLang, sal_Int32 nOptions, bool bIsInteractive ) :
388 meConvType( eConvType ),
389 meSourceLang( eLang ),
390 meTargetLang( eLang ),
391 mnOptions( nOptions ),
392 mbUseTargetFont( false ),
393 mbIsInteractive( bIsInteractive )
395 if (LANGUAGE_KOREAN == eLang)
396 mnOptions = i18n::TextConversionOption::CHARACTER_BY_CHARACTER;
399 ScConversionParam::ScConversionParam( ScConversionType eConvType,
400 LanguageType eSourceLang, LanguageType eTargetLang, vcl::Font aTargetFont,
401 sal_Int32 nOptions, bool bIsInteractive ) :
402 meConvType( eConvType ),
403 meSourceLang( eSourceLang ),
404 meTargetLang( eTargetLang ),
405 maTargetFont(std::move( aTargetFont )),
406 mnOptions( nOptions ),
407 mbUseTargetFont( true ),
408 mbIsInteractive( bIsInteractive )
410 if (LANGUAGE_KOREAN == meSourceLang && LANGUAGE_KOREAN == meTargetLang)
411 mnOptions = i18n::TextConversionOption::CHARACTER_BY_CHARACTER;
414 ScTextConversionEngine::ScTextConversionEngine(
415 SfxItemPool* pEnginePoolP, ScViewData& rViewData,
416 ScConversionParam aConvParam,
417 ScDocument* pUndoDoc, ScDocument* pRedoDoc ) :
418 ScConversionEngineBase( pEnginePoolP, rViewData, pUndoDoc, pRedoDoc ),
419 maConvParam(std::move( aConvParam ))
423 void ScTextConversionEngine::ConvertAll(weld::Widget* pDialogParent, EditView& rEditView)
425 if( FindNextConversionCell() )
427 rEditView.StartTextConversion(pDialogParent,
428 maConvParam.GetSourceLang(), maConvParam.GetTargetLang(), maConvParam.GetTargetFont(),
429 maConvParam.GetOptions(), maConvParam.IsInteractive(), true );
430 // #i34769# restore initial cursor position
431 RestoreCursorPos();
435 bool ScTextConversionEngine::ConvertNextDocument()
437 return FindNextConversionCell();
440 bool ScTextConversionEngine::NeedsConversion()
442 return HasConvertibleTextPortion( maConvParam.GetSourceLang() );
445 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */