bump product version to 4.1.6.2
[LibreOffice.git] / sc / source / ui / view / spelleng.cxx
blobb900c01308c6b4038382f604139dd4440985bfeb
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 <memory>
25 #include "scitems.hxx"
26 #include <editeng/eeitem.hxx>
29 #include <editeng/langitem.hxx>
30 #include <editeng/editobj.hxx>
31 #include <editeng/editview.hxx>
32 #include <sfx2/viewfrm.hxx>
33 #include <vcl/msgbox.hxx>
34 #include <vcl/svapp.hxx>
36 #include "spelldialog.hxx"
37 #include "tabvwsh.hxx"
38 #include "docsh.hxx"
39 #include "formulacell.hxx"
40 #include "patattr.hxx"
41 #include "waitoff.hxx"
42 #include "globstr.hrc"
43 #include "markdata.hxx"
45 #include <boost/scoped_ptr.hpp>
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 = NULL;
85 const ScPatternAttr* pLastPattern = NULL;
87 boost::scoped_ptr<SfxItemSet> pEditDefaults(new SfxItemSet(GetEmptyItemSet()));
89 if( IsModified() )
91 mbIsAnyModified = true;
93 String 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 boost::scoped_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 = PTR_CAST( SvxLanguageItem, pItem ) )
195 LanguageType eLang = static_cast< LanguageType >( 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, sal_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 switch (mrDoc.GetCellType(aPos))
254 case CELLTYPE_STRING:
256 OUString aText = mrDoc.GetString(aPos);
257 SetText( aText );
259 break;
260 case CELLTYPE_EDIT:
262 const EditTextObject* pNewEditObj = mrDoc.GetEditText(aPos);
263 if (pNewEditObj)
264 SetText(*pNewEditObj);
266 break;
267 default:
268 SetText(EMPTY_OUSTRING);
272 // ============================================================================
274 ScSpellingEngine::ScSpellingEngine(
275 SfxItemPool* pEnginePoolP, ScViewData& rViewData,
276 ScDocument* pUndoDoc, ScDocument* pRedoDoc,
277 XSpellCheckerRef xSpeller ) :
278 ScConversionEngineBase( pEnginePoolP, rViewData, pUndoDoc, pRedoDoc )
280 SetSpeller( xSpeller );
283 void ScSpellingEngine::ConvertAll( EditView& rEditView )
285 EESpellState eState = EE_SPELL_OK;
286 if( FindNextConversionCell() )
287 eState = rEditView.StartSpeller( static_cast< sal_Bool >( sal_True ) );
289 OSL_ENSURE( eState != EE_SPELL_NOSPELLER, "ScSpellingEngine::Convert - no spell checker" );
290 if( eState == EE_SPELL_NOLANGUAGE )
292 Window* pParent = GetDialogParent();
293 ScWaitCursorOff aWaitOff( pParent );
294 InfoBox( pParent, ScGlobal::GetRscString( STR_NOLANGERR ) ).Execute();
298 sal_Bool ScSpellingEngine::SpellNextDocument()
300 return FindNextConversionCell();
303 bool ScSpellingEngine::NeedsConversion()
305 return HasSpellErrors() != EE_SPELL_OK;
308 bool ScSpellingEngine::ShowTableWrapDialog()
310 Window* pParent = GetDialogParent();
311 ScWaitCursorOff aWaitOff( pParent );
312 MessBox aMsgBox( pParent, WinBits( WB_YES_NO | WB_DEF_YES ),
313 ScGlobal::GetRscString( STR_MSSG_DOSUBTOTALS_0 ),
314 ScGlobal::GetRscString( STR_SPELLING_BEGIN_TAB) );
315 return aMsgBox.Execute() == RET_YES;
318 void ScSpellingEngine::ShowFinishDialog()
320 Window* pParent = GetDialogParent();
321 ScWaitCursorOff aWaitOff( pParent );
322 InfoBox( pParent, ScGlobal::GetRscString( STR_SPELLING_STOP_OK ) ).Execute();
325 Window* ScSpellingEngine::GetDialogParent()
327 sal_uInt16 nWinId = ScSpellDialogChildWindow::GetChildWindowId();
328 SfxViewFrame* pViewFrm = mrViewData.GetViewShell()->GetViewFrame();
329 if( pViewFrm->HasChildWindow( nWinId ) )
330 if( SfxChildWindow* pChild = pViewFrm->GetChildWindow( nWinId ) )
331 if( Window* pWin = pChild->GetWindow() )
332 if( pWin->IsVisible() )
333 return pWin;
335 // fall back to standard dialog parent
336 return mrDocShell.GetActiveDialogParent();
339 // ============================================================================
341 ScConversionParam::ScConversionParam( ScConversionType eConvType ) :
342 meConvType( eConvType ),
343 meSourceLang( LANGUAGE_NONE ),
344 meTargetLang( LANGUAGE_NONE ),
345 mnOptions( 0 ),
346 mbUseTargetFont( false ),
347 mbIsInteractive( false )
351 ScConversionParam::ScConversionParam( ScConversionType eConvType,
352 LanguageType eLang, sal_Int32 nOptions, bool bIsInteractive ) :
353 meConvType( eConvType ),
354 meSourceLang( eLang ),
355 meTargetLang( eLang ),
356 mnOptions( nOptions ),
357 mbUseTargetFont( false ),
358 mbIsInteractive( bIsInteractive )
360 if (LANGUAGE_KOREAN == eLang)
361 mnOptions = i18n::TextConversionOption::CHARACTER_BY_CHARACTER;
364 ScConversionParam::ScConversionParam( ScConversionType eConvType,
365 LanguageType eSourceLang, LanguageType eTargetLang, const Font& rTargetFont,
366 sal_Int32 nOptions, bool bIsInteractive ) :
367 meConvType( eConvType ),
368 meSourceLang( eSourceLang ),
369 meTargetLang( eTargetLang ),
370 maTargetFont( rTargetFont ),
371 mnOptions( nOptions ),
372 mbUseTargetFont( true ),
373 mbIsInteractive( bIsInteractive )
375 if (LANGUAGE_KOREAN == meSourceLang && LANGUAGE_KOREAN == meTargetLang)
376 mnOptions = i18n::TextConversionOption::CHARACTER_BY_CHARACTER;
379 // ----------------------------------------------------------------------------
381 ScTextConversionEngine::ScTextConversionEngine(
382 SfxItemPool* pEnginePoolP, ScViewData& rViewData,
383 const ScConversionParam& rConvParam,
384 ScDocument* pUndoDoc, ScDocument* pRedoDoc ) :
385 ScConversionEngineBase( pEnginePoolP, rViewData, pUndoDoc, pRedoDoc ),
386 maConvParam( rConvParam )
390 void ScTextConversionEngine::ConvertAll( EditView& rEditView )
392 if( FindNextConversionCell() )
394 rEditView.StartTextConversion(
395 maConvParam.GetSourceLang(), maConvParam.GetTargetLang(), maConvParam.GetTargetFont(),
396 maConvParam.GetOptions(), maConvParam.IsInteractive(), sal_True );
397 // #i34769# restore initial cursor position
398 RestoreCursorPos();
402 sal_Bool ScTextConversionEngine::ConvertNextDocument()
404 return FindNextConversionCell();
407 bool ScTextConversionEngine::NeedsConversion()
409 return HasConvertibleTextPortion( maConvParam.GetSourceLang() );
412 // ============================================================================
414 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */