1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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"
37 #include "formulacell.hxx"
38 #include "patattr.hxx"
39 #include "waitoff.hxx"
40 #include "globstr.hrc"
41 #include "markdata.hxx"
43 #include <boost/scoped_ptr.hpp>
45 using namespace ::com::sun::star
;
47 ScConversionEngineBase::ScConversionEngineBase(
48 SfxItemPool
* pEnginePoolP
, ScViewData
& rViewData
,
49 ScDocument
* pUndoDoc
, ScDocument
* pRedoDoc
) :
50 ScEditEngineDefaulter( pEnginePoolP
),
51 mrViewData( rViewData
),
52 mrDocShell( *rViewData
.GetDocShell() ),
53 mrDoc( rViewData
.GetDocShell()->GetDocument() ),
54 maSelState( rViewData
),
55 mpUndoDoc( pUndoDoc
),
56 mpRedoDoc( pRedoDoc
),
57 meCurrLang( LANGUAGE_ENGLISH_US
),
58 mbIsAnyModified( false ),
59 mbInitialState( true ),
60 mbWrappedInTable( false ),
63 maSelState
.GetCellCursor().GetVars( mnStartCol
, mnStartRow
, mnStartTab
);
64 // start with cell A1 in cell/range/multi-selection, will seek to first selected
65 if( maSelState
.GetSelectionType() == SC_SELECTTYPE_SHEET
)
70 mnCurrCol
= mnStartCol
;
71 mnCurrRow
= mnStartRow
;
74 ScConversionEngineBase::~ScConversionEngineBase()
78 bool ScConversionEngineBase::FindNextConversionCell()
80 ScMarkData
& rMark
= mrViewData
.GetMarkData();
81 ScTabViewShell
* pViewShell
= mrViewData
.GetViewShell();
82 const ScPatternAttr
* pPattern
= NULL
;
83 const ScPatternAttr
* pLastPattern
= NULL
;
85 boost::scoped_ptr
<SfxItemSet
> pEditDefaults(new SfxItemSet(GetEmptyItemSet()));
89 mbIsAnyModified
= true;
91 OUString aNewStr
= GetText();
93 bool bMultiTab
= (rMark
.GetSelectCount() > 1);
96 aVisibleStr
= mrDoc
.GetString(mnCurrCol
, mnCurrRow
, mnStartTab
);
98 for( SCTAB nTab
= 0, nTabCount
= mrDoc
.GetTableCount(); nTab
< nTabCount
; ++nTab
)
100 // always change the cell on the visible tab,
101 // on the other selected tabs only if they contain the same text
103 if ((nTab
== mnStartTab
) ||
104 (bMultiTab
&& rMark
.GetTableSelect(nTab
) && mrDoc
.GetString(mnCurrCol
, mnCurrRow
, nTab
) == aVisibleStr
))
106 ScAddress
aPos( mnCurrCol
, mnCurrRow
, nTab
);
107 CellType eCellType
= mrDoc
.GetCellType( aPos
);
108 bool bEmptyCell
= eCellType
== CELLTYPE_NONE
;
110 if (mpUndoDoc
&& !bEmptyCell
)
111 mrDoc
.CopyCellToDocument(aPos
, aPos
, *mpUndoDoc
);
113 if (eCellType
== CELLTYPE_EDIT
)
115 boost::scoped_ptr
<EditTextObject
> pEditObj(CreateTextObject());
116 mrDoc
.SetEditText(aPos
, *pEditObj
, GetEditTextObjectPool());
119 mrDoc
.SetString(aPos
, aNewStr
);
121 if (mpRedoDoc
&& !bEmptyCell
)
122 mrDoc
.CopyCellToDocument(aPos
, aPos
, *mpRedoDoc
);
124 mrDocShell
.PostPaintCell(aPos
);
129 SCCOL nNewCol
= mnCurrCol
;
130 SCROW nNewRow
= mnCurrRow
;
134 /* On very first call, decrement row to let GetNextSpellingCell() find
135 the first cell of current range. */
136 mbInitialState
= false;
140 bool bSheetSel
= maSelState
.GetSelectionType() == SC_SELECTTYPE_SHEET
;
143 while( bLoop
&& !bFound
)
145 bLoop
= mrDoc
.GetNextSpellingCell( nNewCol
, nNewRow
, mnStartTab
, bSheetSel
, rMark
);
148 FillFromCell( mnCurrCol
, mnCurrRow
, mnStartTab
);
150 if( mbWrappedInTable
&& ((nNewCol
> mnStartCol
) || ((nNewCol
== mnStartCol
) && (nNewRow
>= mnStartRow
))) )
156 else if( nNewCol
> MAXCOL
)
158 // no more cells in the sheet - try to restart at top of sheet
160 if( bSheetSel
|| ((mnStartCol
== 0) && (mnStartRow
== 0)) )
162 // conversion started at cell A1 or in selection, do not query to restart at top
167 else if( ShowTableWrapDialog() )
169 // conversion started anywhere but in cell A1, user wants to restart
170 nNewRow
= MAXROW
+ 2;
171 mbWrappedInTable
= true;
181 pPattern
= mrDoc
.GetPattern( nNewCol
, nNewRow
, mnStartTab
);
182 if( pPattern
&& (pPattern
!= pLastPattern
) )
184 pPattern
->FillEditItemSet( pEditDefaults
.get() );
185 SetDefaults( *pEditDefaults
);
186 pLastPattern
= pPattern
;
190 const SfxPoolItem
* pItem
= mrDoc
.GetAttr( nNewCol
, nNewRow
, mnStartTab
, ATTR_FONT_LANGUAGE
);
191 if( const SvxLanguageItem
* pLangItem
= PTR_CAST( SvxLanguageItem
, pItem
) )
193 LanguageType eLang
= static_cast< LanguageType
>( pLangItem
->GetValue() );
194 if( eLang
== LANGUAGE_SYSTEM
)
195 eLang
= Application::GetSettings().GetLanguageTag().getLanguageType(); // never use SYSTEM for spelling
196 if( eLang
!= meCurrLang
)
199 SetDefaultLanguage( eLang
);
203 FillFromCell( nNewCol
, nNewRow
, mnStartTab
);
205 bFound
= bLoop
&& NeedsConversion();
212 pViewShell
->AlignToCursor( nNewCol
, nNewRow
, SC_FOLLOW_JUMP
);
213 pViewShell
->SetCursor( nNewCol
, nNewRow
, true );
214 mrViewData
.GetView()->MakeEditView( this, nNewCol
, nNewRow
);
215 EditView
* pEditView
= mrViewData
.GetSpellingView();
216 // maSelState.GetEditSelection() returns (0,0) if not in edit mode -> ok
217 pEditView
->SetSelection( maSelState
.GetEditSelection() );
227 void ScConversionEngineBase::RestoreCursorPos()
229 const ScAddress
& rPos
= maSelState
.GetCellCursor();
230 mrViewData
.GetViewShell()->SetCursor( rPos
.Col(), rPos
.Row() );
233 bool ScConversionEngineBase::ShowTableWrapDialog()
235 // default: no dialog, always restart at top
239 void ScConversionEngineBase::ShowFinishDialog()
241 // default: no dialog
244 // private --------------------------------------------------------------------
246 void ScConversionEngineBase::FillFromCell( SCCOL nCol
, SCROW nRow
, SCTAB nTab
)
248 ScAddress
aPos(nCol
, nRow
, nTab
);
250 switch (mrDoc
.GetCellType(aPos
))
252 case CELLTYPE_STRING
:
254 OUString aText
= mrDoc
.GetString(aPos
);
260 const EditTextObject
* pNewEditObj
= mrDoc
.GetEditText(aPos
);
262 SetText(*pNewEditObj
);
266 SetText(EMPTY_OUSTRING
);
270 ScSpellingEngine::ScSpellingEngine(
271 SfxItemPool
* pEnginePoolP
, ScViewData
& rViewData
,
272 ScDocument
* pUndoDoc
, ScDocument
* pRedoDoc
,
273 XSpellCheckerRef xSpeller
) :
274 ScConversionEngineBase( pEnginePoolP
, rViewData
, pUndoDoc
, pRedoDoc
)
276 SetSpeller( xSpeller
);
279 void ScSpellingEngine::ConvertAll( EditView
& rEditView
)
281 EESpellState eState
= EE_SPELL_OK
;
282 if( FindNextConversionCell() )
283 eState
= rEditView
.StartSpeller( true );
285 OSL_ENSURE( eState
!= EE_SPELL_NOSPELLER
, "ScSpellingEngine::Convert - no spell checker" );
286 if( eState
== EE_SPELL_NOLANGUAGE
)
288 vcl::Window
* pParent
= GetDialogParent();
289 ScWaitCursorOff
aWaitOff( pParent
);
290 ScopedVclPtr
<InfoBox
>::Create( pParent
, ScGlobal::GetRscString( STR_NOLANGERR
) )->Execute();
294 bool ScSpellingEngine::SpellNextDocument()
296 return FindNextConversionCell();
299 bool ScSpellingEngine::NeedsConversion()
301 return HasSpellErrors() != EE_SPELL_OK
;
304 bool ScSpellingEngine::ShowTableWrapDialog()
306 vcl::Window
* pParent
= GetDialogParent();
307 ScWaitCursorOff
aWaitOff( pParent
);
308 ScopedVclPtrInstance
<MessBox
> aMsgBox( pParent
, WinBits( WB_YES_NO
| WB_DEF_YES
),
309 ScGlobal::GetRscString( STR_MSSG_DOSUBTOTALS_0
),
310 ScGlobal::GetRscString( STR_SPELLING_BEGIN_TAB
) );
311 return aMsgBox
->Execute() == RET_YES
;
314 void ScSpellingEngine::ShowFinishDialog()
316 vcl::Window
* pParent
= GetDialogParent();
317 ScWaitCursorOff
aWaitOff( pParent
);
318 ScopedVclPtr
<InfoBox
>::Create( pParent
, ScGlobal::GetRscString( STR_SPELLING_STOP_OK
) )->Execute();
321 vcl::Window
* ScSpellingEngine::GetDialogParent()
323 sal_uInt16 nWinId
= ScSpellDialogChildWindow::GetChildWindowId();
324 SfxViewFrame
* pViewFrm
= mrViewData
.GetViewShell()->GetViewFrame();
325 if( pViewFrm
->HasChildWindow( nWinId
) )
326 if( SfxChildWindow
* pChild
= pViewFrm
->GetChildWindow( nWinId
) )
327 if( vcl::Window
* pWin
= pChild
->GetWindow() )
328 if( pWin
->IsVisible() )
331 // fall back to standard dialog parent
332 return ScDocShell::GetActiveDialogParent();
335 ScConversionParam::ScConversionParam( ScConversionType eConvType
) :
336 meConvType( eConvType
),
337 meSourceLang( LANGUAGE_NONE
),
338 meTargetLang( LANGUAGE_NONE
),
340 mbUseTargetFont( false ),
341 mbIsInteractive( false )
345 ScConversionParam::ScConversionParam( ScConversionType eConvType
,
346 LanguageType eLang
, sal_Int32 nOptions
, bool bIsInteractive
) :
347 meConvType( eConvType
),
348 meSourceLang( eLang
),
349 meTargetLang( eLang
),
350 mnOptions( nOptions
),
351 mbUseTargetFont( false ),
352 mbIsInteractive( bIsInteractive
)
354 if (LANGUAGE_KOREAN
== eLang
)
355 mnOptions
= i18n::TextConversionOption::CHARACTER_BY_CHARACTER
;
358 ScConversionParam::ScConversionParam( ScConversionType eConvType
,
359 LanguageType eSourceLang
, LanguageType eTargetLang
, const vcl::Font
& rTargetFont
,
360 sal_Int32 nOptions
, bool bIsInteractive
) :
361 meConvType( eConvType
),
362 meSourceLang( eSourceLang
),
363 meTargetLang( eTargetLang
),
364 maTargetFont( rTargetFont
),
365 mnOptions( nOptions
),
366 mbUseTargetFont( true ),
367 mbIsInteractive( bIsInteractive
)
369 if (LANGUAGE_KOREAN
== meSourceLang
&& LANGUAGE_KOREAN
== meTargetLang
)
370 mnOptions
= i18n::TextConversionOption::CHARACTER_BY_CHARACTER
;
373 ScTextConversionEngine::ScTextConversionEngine(
374 SfxItemPool
* pEnginePoolP
, ScViewData
& rViewData
,
375 const ScConversionParam
& rConvParam
,
376 ScDocument
* pUndoDoc
, ScDocument
* pRedoDoc
) :
377 ScConversionEngineBase( pEnginePoolP
, rViewData
, pUndoDoc
, pRedoDoc
),
378 maConvParam( rConvParam
)
382 void ScTextConversionEngine::ConvertAll( EditView
& rEditView
)
384 if( FindNextConversionCell() )
386 rEditView
.StartTextConversion(
387 maConvParam
.GetSourceLang(), maConvParam
.GetTargetLang(), maConvParam
.GetTargetFont(),
388 maConvParam
.GetOptions(), maConvParam
.IsInteractive(), true );
389 // #i34769# restore initial cursor position
394 bool ScTextConversionEngine::ConvertNextDocument()
396 return FindNextConversionCell();
399 bool ScTextConversionEngine::NeedsConversion()
401 return HasConvertibleTextPortion( maConvParam
.GetSourceLang() );
404 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */