lok: calc: fix needed when position caching is disabled
[LibreOffice.git] / sc / source / ui / view / spelleng.cxx
blob15fedc1f9733537ba588e463ae43f6ce819f2d8f
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>
24 #include <editeng/eeitem.hxx>
26 #include <editeng/langitem.hxx>
27 #include <editeng/editobj.hxx>
28 #include <editeng/editview.hxx>
29 #include <sfx2/viewfrm.hxx>
30 #include <vcl/msgbox.hxx>
31 #include <vcl/svapp.hxx>
32 #include <vcl/settings.hxx>
34 #include <spelldialog.hxx>
35 #include <tabvwsh.hxx>
36 #include <docsh.hxx>
37 #include <cellvalue.hxx>
38 #include <cellform.hxx>
39 #include <formulacell.hxx>
40 #include <patattr.hxx>
41 #include <waitoff.hxx>
42 #include <globstr.hrc>
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 std::unique_ptr<SfxItemSet> pEditDefaults(new SfxItemSet(GetEmptyItemSet()));
89 if( IsModified() )
91 mbIsAnyModified = true;
93 OUString aNewStr = GetText();
95 bool bMultiTab = (rMark.GetSelectCount() > 1);
96 OUString aVisibleStr;
97 if( bMultiTab )
98 aVisibleStr = mrDoc.GetString(mnCurrCol, mnCurrRow, mnStartTab);
100 for( SCTAB nTab = 0, nTabCount = mrDoc.GetTableCount(); nTab < nTabCount; ++nTab )
102 // always change the cell on the visible tab,
103 // on the other selected tabs only if they contain the same text
105 if ((nTab == mnStartTab) ||
106 (bMultiTab && rMark.GetTableSelect(nTab) && mrDoc.GetString(mnCurrCol, mnCurrRow, nTab) == aVisibleStr))
108 ScAddress aPos( mnCurrCol, mnCurrRow, nTab );
109 CellType eCellType = mrDoc.GetCellType( aPos );
110 bool bEmptyCell = eCellType == CELLTYPE_NONE;
112 if (mpUndoDoc && !bEmptyCell)
113 mrDoc.CopyCellToDocument(aPos, aPos, *mpUndoDoc);
115 if (eCellType == CELLTYPE_EDIT)
117 std::unique_ptr<EditTextObject> pEditObj(CreateTextObject());
118 mrDoc.SetEditText(aPos, *pEditObj, GetEditTextObjectPool());
120 else
121 mrDoc.SetString(aPos, aNewStr);
123 if (mpRedoDoc && !bEmptyCell)
124 mrDoc.CopyCellToDocument(aPos, aPos, *mpRedoDoc);
126 mrDocShell.PostPaintCell(aPos);
131 SCCOL nNewCol = mnCurrCol;
132 SCROW nNewRow = mnCurrRow;
134 if( mbInitialState )
136 /* On very first call, decrement row to let GetNextSpellingCell() find
137 the first cell of current range. */
138 mbInitialState = false;
139 --nNewRow;
142 bool bSheetSel = maSelState.GetSelectionType() == SC_SELECTTYPE_SHEET;
143 bool bLoop = true;
144 bool bFound = false;
145 while( bLoop && !bFound )
147 bLoop = mrDoc.GetNextSpellingCell( nNewCol, nNewRow, mnStartTab, bSheetSel, rMark );
148 if( bLoop )
150 FillFromCell( mnCurrCol, mnCurrRow, mnStartTab );
152 if( mbWrappedInTable && ((nNewCol > mnStartCol) || ((nNewCol == mnStartCol) && (nNewRow >= mnStartRow))) )
154 ShowFinishDialog();
155 bLoop = false;
156 mbFinished = true;
158 else if( nNewCol > MAXCOL )
160 // no more cells in the sheet - try to restart at top of sheet
162 if( bSheetSel || ((mnStartCol == 0) && (mnStartRow == 0)) )
164 // conversion started at cell A1 or in selection, do not query to restart at top
165 ShowFinishDialog();
166 bLoop = false;
167 mbFinished = true;
169 else if( ShowTableWrapDialog() )
171 // conversion started anywhere but in cell A1, user wants to restart
172 nNewRow = MAXROW + 2;
173 mbWrappedInTable = true;
175 else
177 bLoop = false;
178 mbFinished = true;
181 else
183 pPattern = mrDoc.GetPattern( nNewCol, nNewRow, mnStartTab );
184 if( pPattern && (pPattern != pLastPattern) )
186 pPattern->FillEditItemSet( pEditDefaults.get() );
187 SetDefaults( *pEditDefaults );
188 pLastPattern = pPattern;
191 // language changed?
192 const SfxPoolItem* pItem = mrDoc.GetAttr( nNewCol, nNewRow, mnStartTab, ATTR_FONT_LANGUAGE );
193 if( const SvxLanguageItem* pLangItem = dynamic_cast<const SvxLanguageItem*>( pItem ) )
195 LanguageType eLang = pLangItem->GetValue();
196 if( eLang == LANGUAGE_SYSTEM )
197 eLang = Application::GetSettings().GetLanguageTag().getLanguageType(); // never use SYSTEM for spelling
198 if( eLang != meCurrLang )
200 meCurrLang = eLang;
201 SetDefaultLanguage( eLang );
205 FillFromCell( nNewCol, nNewRow, mnStartTab );
207 bFound = bLoop && NeedsConversion();
212 if( bFound )
214 pViewShell->AlignToCursor( nNewCol, nNewRow, SC_FOLLOW_JUMP );
215 pViewShell->SetCursor( nNewCol, nNewRow, true );
216 mrViewData.GetView()->MakeEditView( this, nNewCol, nNewRow );
217 EditView* pEditView = mrViewData.GetSpellingView();
218 // maSelState.GetEditSelection() returns (0,0) if not in edit mode -> ok
219 pEditView->SetSelection( maSelState.GetEditSelection() );
221 ClearModifyFlag();
222 mnCurrCol = nNewCol;
223 mnCurrRow = nNewRow;
226 return bFound;
229 void ScConversionEngineBase::RestoreCursorPos()
231 const ScAddress& rPos = maSelState.GetCellCursor();
232 mrViewData.GetViewShell()->SetCursor( rPos.Col(), rPos.Row() );
235 bool ScConversionEngineBase::ShowTableWrapDialog()
237 // default: no dialog, always restart at top
238 return true;
241 void ScConversionEngineBase::ShowFinishDialog()
243 // default: no dialog
246 // private --------------------------------------------------------------------
248 void ScConversionEngineBase::FillFromCell( SCCOL nCol, SCROW nRow, SCTAB nTab )
250 ScAddress aPos(nCol, nRow, nTab);
252 ScRefCellValue aCell(mrDoc, aPos);
253 switch (aCell.meType)
255 case CELLTYPE_STRING:
257 SvNumberFormatter* pFormatter = mrDoc.GetFormatTable();
258 sal_uInt32 nNumFmt = mrDoc.GetNumberFormat(aPos);
259 OUString aText;
260 Color* pColor;
261 ScCellFormat::GetString(aCell, nNumFmt, aText, &pColor, *pFormatter, &mrDoc);
263 SetText(aText);
265 break;
266 case CELLTYPE_EDIT:
268 const EditTextObject* pNewEditObj = aCell.mpEditText;
269 SetText(*pNewEditObj);
271 break;
272 default:
273 SetText(EMPTY_OUSTRING);
277 ScSpellingEngine::ScSpellingEngine(
278 SfxItemPool* pEnginePoolP, ScViewData& rViewData,
279 ScDocument* pUndoDoc, ScDocument* pRedoDoc,
280 css::uno::Reference< css::linguistic2::XSpellChecker1 > const & xSpeller ) :
281 ScConversionEngineBase( pEnginePoolP, rViewData, pUndoDoc, pRedoDoc )
283 SetSpeller( xSpeller );
286 void ScSpellingEngine::ConvertAll( EditView& rEditView )
288 EESpellState eState = EESpellState::Ok;
289 if( FindNextConversionCell() )
290 eState = rEditView.StartSpeller( true );
292 OSL_ENSURE( eState != EESpellState::NoSpeller, "ScSpellingEngine::Convert - no spell checker" );
295 bool ScSpellingEngine::SpellNextDocument()
297 return FindNextConversionCell();
300 bool ScSpellingEngine::NeedsConversion()
302 return HasSpellErrors() != EESpellState::Ok;
305 bool ScSpellingEngine::ShowTableWrapDialog()
307 vcl::Window* pParent = GetDialogParent();
308 ScWaitCursorOff aWaitOff( pParent );
309 ScopedVclPtrInstance<MessBox> aMsgBox( pParent, MessBoxStyle::YesNo | MessBoxStyle::DefaultYes, 0,
310 ScGlobal::GetRscString( STR_MSSG_DOSUBTOTALS_0 ),
311 ScGlobal::GetRscString( STR_SPELLING_BEGIN_TAB) );
312 return aMsgBox->Execute() == RET_YES;
315 void ScSpellingEngine::ShowFinishDialog()
317 vcl::Window* pParent = GetDialogParent();
318 ScWaitCursorOff aWaitOff( pParent );
319 ScopedVclPtrInstance<InfoBox>( pParent, ScGlobal::GetRscString( STR_SPELLING_STOP_OK ) )->Execute();
322 vcl::Window* ScSpellingEngine::GetDialogParent()
324 sal_uInt16 nWinId = ScSpellDialogChildWindow::GetChildWindowId();
325 SfxViewFrame* pViewFrm = mrViewData.GetViewShell()->GetViewFrame();
326 if( pViewFrm->HasChildWindow( nWinId ) )
327 if( SfxChildWindow* pChild = pViewFrm->GetChildWindow( nWinId ) )
328 if( vcl::Window* pWin = pChild->GetWindow() )
329 if( pWin->IsVisible() )
330 return pWin;
332 // fall back to standard dialog parent
333 return ScDocShell::GetActiveDialogParent();
336 ScConversionParam::ScConversionParam( ScConversionType eConvType ) :
337 meConvType( eConvType ),
338 meSourceLang( LANGUAGE_NONE ),
339 meTargetLang( LANGUAGE_NONE ),
340 mnOptions( 0 ),
341 mbUseTargetFont( false ),
342 mbIsInteractive( false )
346 ScConversionParam::ScConversionParam( ScConversionType eConvType,
347 LanguageType eLang, sal_Int32 nOptions, bool bIsInteractive ) :
348 meConvType( eConvType ),
349 meSourceLang( eLang ),
350 meTargetLang( eLang ),
351 mnOptions( nOptions ),
352 mbUseTargetFont( false ),
353 mbIsInteractive( bIsInteractive )
355 if (LANGUAGE_KOREAN == eLang)
356 mnOptions = i18n::TextConversionOption::CHARACTER_BY_CHARACTER;
359 ScConversionParam::ScConversionParam( ScConversionType eConvType,
360 LanguageType eSourceLang, LanguageType eTargetLang, const vcl::Font& rTargetFont,
361 sal_Int32 nOptions, bool bIsInteractive ) :
362 meConvType( eConvType ),
363 meSourceLang( eSourceLang ),
364 meTargetLang( eTargetLang ),
365 maTargetFont( rTargetFont ),
366 mnOptions( nOptions ),
367 mbUseTargetFont( true ),
368 mbIsInteractive( bIsInteractive )
370 if (LANGUAGE_KOREAN == meSourceLang && LANGUAGE_KOREAN == meTargetLang)
371 mnOptions = i18n::TextConversionOption::CHARACTER_BY_CHARACTER;
374 ScTextConversionEngine::ScTextConversionEngine(
375 SfxItemPool* pEnginePoolP, ScViewData& rViewData,
376 const ScConversionParam& rConvParam,
377 ScDocument* pUndoDoc, ScDocument* pRedoDoc ) :
378 ScConversionEngineBase( pEnginePoolP, rViewData, pUndoDoc, pRedoDoc ),
379 maConvParam( rConvParam )
383 void ScTextConversionEngine::ConvertAll( EditView& rEditView )
385 if( FindNextConversionCell() )
387 rEditView.StartTextConversion(
388 maConvParam.GetSourceLang(), maConvParam.GetTargetLang(), maConvParam.GetTargetFont(),
389 maConvParam.GetOptions(), maConvParam.IsInteractive(), true );
390 // #i34769# restore initial cursor position
391 RestoreCursorPos();
395 bool ScTextConversionEngine::ConvertNextDocument()
397 return FindNextConversionCell();
400 bool ScTextConversionEngine::NeedsConversion()
402 return HasConvertibleTextPortion( maConvParam.GetSourceLang() );
405 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */