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>
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"
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 ),
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
)
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()));
90 mbIsAnyModified
= true;
92 OUString aNewStr
= GetText();
94 bool bMultiTab
= (rMark
.GetSelectCount() > 1);
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());
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
;
135 /* On very first call, decrement row to let GetNextSpellingCell() find
136 the first cell of current range. */
137 mbInitialState
= false;
141 bool bSheetSel
= maSelState
.GetSelectionType() == SC_SELECTTYPE_SHEET
;
144 while( bLoop
&& !bFound
)
146 bLoop
= mrDoc
.GetNextSpellingCell( nNewCol
, nNewRow
, mnStartTab
, bSheetSel
, rMark
);
149 FillFromCell( mnCurrCol
, mnCurrRow
, mnStartTab
);
151 if( mbWrappedInTable
&& ((nNewCol
> mnStartCol
) || ((nNewCol
== mnStartCol
) && (nNewRow
>= mnStartRow
))) )
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
168 else if( ShowTableWrapDialog() )
170 // conversion started anywhere but in cell A1, user wants to restart
171 nNewRow
= MAXROW
+ 2;
172 mbWrappedInTable
= true;
182 pPattern
= mrDoc
.GetPattern( nNewCol
, nNewRow
, mnStartTab
);
183 if( pPattern
&& (pPattern
!= pLastPattern
) )
185 pPattern
->FillEditItemSet( pEditDefaults
.get() );
186 SetDefaults( *pEditDefaults
);
187 pLastPattern
= pPattern
;
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
)
200 SetDefaultLanguage( eLang
);
204 FillFromCell( nNewCol
, nNewRow
, mnStartTab
);
206 bFound
= bLoop
&& NeedsConversion();
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() );
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
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
);
261 const EditTextObject
* pNewEditObj
= mrDoc
.GetEditText(aPos
);
263 SetText(*pNewEditObj
);
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() )
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
),
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
395 bool ScTextConversionEngine::ConvertNextDocument()
397 return FindNextConversionCell();
400 bool ScTextConversionEngine::NeedsConversion()
402 return HasConvertibleTextPortion( maConvParam
.GetSourceLang() );
405 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */