Bump version to 4.3-4
[LibreOffice.git] / sc / source / ui / view / spelleng.cxx
blobe243cc82949343901f6fb7aa4deed73c80d82537
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>
27 #include <editeng/langitem.hxx>
28 #include <editeng/editobj.hxx>
29 #include <editeng/editview.hxx>
30 #include <sfx2/viewfrm.hxx>
31 #include <vcl/msgbox.hxx>
32 #include <vcl/svapp.hxx>
33 #include <vcl/settings.hxx>
35 #include "spelldialog.hxx"
36 #include "tabvwsh.hxx"
37 #include "docsh.hxx"
38 #include "formulacell.hxx"
39 #include "patattr.hxx"
40 #include "waitoff.hxx"
41 #include "globstr.hrc"
42 #include "markdata.hxx"
44 #include <boost/scoped_ptr.hpp>
46 using namespace ::com::sun::star;
48 ScConversionEngineBase::ScConversionEngineBase(
49 SfxItemPool* pEnginePoolP, ScViewData& rViewData,
50 ScDocument* pUndoDoc, ScDocument* pRedoDoc ) :
51 ScEditEngineDefaulter( pEnginePoolP ),
52 mrViewData( rViewData ),
53 mrDocShell( *rViewData.GetDocShell() ),
54 mrDoc( *rViewData.GetDocShell()->GetDocument() ),
55 maSelState( rViewData ),
56 mpUndoDoc( pUndoDoc ),
57 mpRedoDoc( pRedoDoc ),
58 meCurrLang( LANGUAGE_ENGLISH_US ),
59 mbIsAnyModified( false ),
60 mbInitialState( true ),
61 mbWrappedInTable( false ),
62 mbFinished( false )
64 maSelState.GetCellCursor().GetVars( mnStartCol, mnStartRow, mnStartTab );
65 // start with cell A1 in cell/range/multi-selection, will seek to first selected
66 if( maSelState.GetSelectionType() == SC_SELECTTYPE_SHEET )
68 mnStartCol = 0;
69 mnStartRow = 0;
71 mnCurrCol = mnStartCol;
72 mnCurrRow = mnStartRow;
75 ScConversionEngineBase::~ScConversionEngineBase()
79 bool ScConversionEngineBase::FindNextConversionCell()
81 ScMarkData& rMark = mrViewData.GetMarkData();
82 ScTabViewShell* pViewShell = mrViewData.GetViewShell();
83 const ScPatternAttr* pPattern = NULL;
84 const ScPatternAttr* pLastPattern = NULL;
86 boost::scoped_ptr<SfxItemSet> pEditDefaults(new SfxItemSet(GetEmptyItemSet()));
88 if( IsModified() )
90 mbIsAnyModified = true;
92 OUString aNewStr = GetText();
94 bool bMultiTab = (rMark.GetSelectCount() > 1);
95 OUString aVisibleStr;
96 if( bMultiTab )
97 aVisibleStr = mrDoc.GetString(mnCurrCol, mnCurrRow, mnStartTab);
99 for( SCTAB nTab = 0, nTabCount = mrDoc.GetTableCount(); nTab < nTabCount; ++nTab )
101 // always change the cell on the visible tab,
102 // on the other selected tabs only if they contain the same text
104 if ((nTab == mnStartTab) ||
105 (bMultiTab && rMark.GetTableSelect(nTab) && mrDoc.GetString(mnCurrCol, mnCurrRow, nTab) == aVisibleStr))
107 ScAddress aPos( mnCurrCol, mnCurrRow, nTab );
108 CellType eCellType = mrDoc.GetCellType( aPos );
109 bool bEmptyCell = eCellType == CELLTYPE_NONE;
111 if (mpUndoDoc && !bEmptyCell)
112 mrDoc.CopyCellToDocument(aPos, aPos, *mpUndoDoc);
114 if (eCellType == CELLTYPE_EDIT)
116 boost::scoped_ptr<EditTextObject> pEditObj(CreateTextObject());
117 mrDoc.SetEditText(aPos, *pEditObj, GetEditTextObjectPool());
119 else
120 mrDoc.SetString(aPos, aNewStr);
122 if (mpRedoDoc && !bEmptyCell)
123 mrDoc.CopyCellToDocument(aPos, aPos, *mpRedoDoc);
125 mrDocShell.PostPaintCell(aPos);
130 SCCOL nNewCol = mnCurrCol;
131 SCROW nNewRow = mnCurrRow;
133 if( mbInitialState )
135 /* On very first call, decrement row to let GetNextSpellingCell() find
136 the first cell of current range. */
137 mbInitialState = false;
138 --nNewRow;
141 bool bSheetSel = maSelState.GetSelectionType() == SC_SELECTTYPE_SHEET;
142 bool bLoop = true;
143 bool bFound = false;
144 while( bLoop && !bFound )
146 bLoop = mrDoc.GetNextSpellingCell( nNewCol, nNewRow, mnStartTab, bSheetSel, rMark );
147 if( bLoop )
149 FillFromCell( mnCurrCol, mnCurrRow, mnStartTab );
151 if( mbWrappedInTable && ((nNewCol > mnStartCol) || ((nNewCol == mnStartCol) && (nNewRow >= mnStartRow))) )
153 ShowFinishDialog();
154 bLoop = false;
155 mbFinished = true;
157 else if( nNewCol > MAXCOL )
159 // no more cells in the sheet - try to restart at top of sheet
161 if( bSheetSel || ((mnStartCol == 0) && (mnStartRow == 0)) )
163 // conversion started at cell A1 or in selection, do not query to restart at top
164 ShowFinishDialog();
165 bLoop = false;
166 mbFinished = true;
168 else if( ShowTableWrapDialog() )
170 // conversion started anywhere but in cell A1, user wants to restart
171 nNewRow = MAXROW + 2;
172 mbWrappedInTable = true;
174 else
176 bLoop = false;
177 mbFinished = true;
180 else
182 pPattern = mrDoc.GetPattern( nNewCol, nNewRow, mnStartTab );
183 if( pPattern && (pPattern != pLastPattern) )
185 pPattern->FillEditItemSet( pEditDefaults.get() );
186 SetDefaults( *pEditDefaults );
187 pLastPattern = pPattern;
190 // language changed?
191 const SfxPoolItem* pItem = mrDoc.GetAttr( nNewCol, nNewRow, mnStartTab, ATTR_FONT_LANGUAGE );
192 if( const SvxLanguageItem* pLangItem = PTR_CAST( SvxLanguageItem, pItem ) )
194 LanguageType eLang = static_cast< LanguageType >( pLangItem->GetValue() );
195 if( eLang == LANGUAGE_SYSTEM )
196 eLang = Application::GetSettings().GetLanguageTag().getLanguageType(); // never use SYSTEM for spelling
197 if( eLang != meCurrLang )
199 meCurrLang = eLang;
200 SetDefaultLanguage( eLang );
204 FillFromCell( nNewCol, nNewRow, mnStartTab );
206 bFound = bLoop && NeedsConversion();
211 if( bFound )
213 pViewShell->AlignToCursor( nNewCol, nNewRow, SC_FOLLOW_JUMP );
214 pViewShell->SetCursor( nNewCol, nNewRow, true );
215 mrViewData.GetView()->MakeEditView( this, nNewCol, nNewRow );
216 EditView* pEditView = mrViewData.GetSpellingView();
217 // maSelState.GetEditSelection() returns (0,0) if not in edit mode -> ok
218 pEditView->SetSelection( maSelState.GetEditSelection() );
220 ClearModifyFlag();
221 mnCurrCol = nNewCol;
222 mnCurrRow = nNewRow;
225 return bFound;
228 void ScConversionEngineBase::RestoreCursorPos()
230 const ScAddress& rPos = maSelState.GetCellCursor();
231 mrViewData.GetViewShell()->SetCursor( rPos.Col(), rPos.Row() );
234 bool ScConversionEngineBase::ShowTableWrapDialog()
236 // default: no dialog, always restart at top
237 return true;
240 void ScConversionEngineBase::ShowFinishDialog()
242 // default: no dialog
245 // private --------------------------------------------------------------------
247 void ScConversionEngineBase::FillFromCell( SCCOL nCol, SCROW nRow, SCTAB nTab )
249 ScAddress aPos(nCol, nRow, nTab);
251 switch (mrDoc.GetCellType(aPos))
253 case CELLTYPE_STRING:
255 OUString aText = mrDoc.GetString(aPos);
256 SetText( aText );
258 break;
259 case CELLTYPE_EDIT:
261 const EditTextObject* pNewEditObj = mrDoc.GetEditText(aPos);
262 if (pNewEditObj)
263 SetText(*pNewEditObj);
265 break;
266 default:
267 SetText(EMPTY_OUSTRING);
271 ScSpellingEngine::ScSpellingEngine(
272 SfxItemPool* pEnginePoolP, ScViewData& rViewData,
273 ScDocument* pUndoDoc, ScDocument* pRedoDoc,
274 XSpellCheckerRef xSpeller ) :
275 ScConversionEngineBase( pEnginePoolP, rViewData, pUndoDoc, pRedoDoc )
277 SetSpeller( xSpeller );
280 void ScSpellingEngine::ConvertAll( EditView& rEditView )
282 EESpellState eState = EE_SPELL_OK;
283 if( FindNextConversionCell() )
284 eState = rEditView.StartSpeller( true );
286 OSL_ENSURE( eState != EE_SPELL_NOSPELLER, "ScSpellingEngine::Convert - no spell checker" );
287 if( eState == EE_SPELL_NOLANGUAGE )
289 Window* pParent = GetDialogParent();
290 ScWaitCursorOff aWaitOff( pParent );
291 InfoBox( pParent, ScGlobal::GetRscString( STR_NOLANGERR ) ).Execute();
295 bool ScSpellingEngine::SpellNextDocument()
297 return FindNextConversionCell();
300 bool ScSpellingEngine::NeedsConversion()
302 return HasSpellErrors() != EE_SPELL_OK;
305 bool ScSpellingEngine::ShowTableWrapDialog()
307 Window* pParent = GetDialogParent();
308 ScWaitCursorOff aWaitOff( pParent );
309 MessBox aMsgBox( pParent, WinBits( WB_YES_NO | WB_DEF_YES ),
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 Window* pParent = GetDialogParent();
318 ScWaitCursorOff aWaitOff( pParent );
319 InfoBox( pParent, ScGlobal::GetRscString( STR_SPELLING_STOP_OK ) ).Execute();
322 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( Window* pWin = pChild->GetWindow() )
329 if( pWin->IsVisible() )
330 return pWin;
332 // fall back to standard dialog parent
333 return mrDocShell.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 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: */