LCOV - code coverage report
Current view: top level - sc/source/ui/view - spelleng.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 1 177 0.6 %
Date: 2015-06-13 12:38:46 Functions: 2 24 8.3 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       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/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      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 .
      18             :  */
      19             : 
      20             : #include "spelleng.hxx"
      21             : #include <com/sun/star/i18n/TextConversionOption.hpp>
      22             : 
      23             : #include "scitems.hxx"
      24             : #include <editeng/eeitem.hxx>
      25             : 
      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>
      33             : 
      34             : #include "spelldialog.hxx"
      35             : #include "tabvwsh.hxx"
      36             : #include "docsh.hxx"
      37             : #include "formulacell.hxx"
      38             : #include "patattr.hxx"
      39             : #include "waitoff.hxx"
      40             : #include "globstr.hrc"
      41             : #include "markdata.hxx"
      42             : 
      43             : #include <boost/scoped_ptr.hpp>
      44             : 
      45             : using namespace ::com::sun::star;
      46             : 
      47           0 : ScConversionEngineBase::ScConversionEngineBase(
      48             :         SfxItemPool* pEnginePoolP, ScViewData& rViewData,
      49             :         ScDocument* pUndoDoc, ScDocument* pRedoDoc ) :
      50             :     ScEditEngineDefaulter( pEnginePoolP ),
      51             :     mrViewData( rViewData ),
      52           0 :     mrDocShell( *rViewData.GetDocShell() ),
      53           0 :     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 ),
      61           0 :     mbFinished( false )
      62             : {
      63           0 :     maSelState.GetCellCursor().GetVars( mnStartCol, mnStartRow, mnStartTab );
      64             :     // start with cell A1 in cell/range/multi-selection, will seek to first selected
      65           0 :     if( maSelState.GetSelectionType() == SC_SELECTTYPE_SHEET )
      66             :     {
      67           0 :         mnStartCol = 0;
      68           0 :         mnStartRow = 0;
      69             :     }
      70           0 :     mnCurrCol = mnStartCol;
      71           0 :     mnCurrRow = mnStartRow;
      72           0 : }
      73             : 
      74           0 : ScConversionEngineBase::~ScConversionEngineBase()
      75             : {
      76           0 : }
      77             : 
      78           0 : bool ScConversionEngineBase::FindNextConversionCell()
      79             : {
      80           0 :     ScMarkData& rMark = mrViewData.GetMarkData();
      81           0 :     ScTabViewShell* pViewShell = mrViewData.GetViewShell();
      82           0 :     const ScPatternAttr* pPattern = NULL;
      83           0 :     const ScPatternAttr* pLastPattern = NULL;
      84             : 
      85           0 :     boost::scoped_ptr<SfxItemSet> pEditDefaults(new SfxItemSet(GetEmptyItemSet()));
      86             : 
      87           0 :     if( IsModified() )
      88             :     {
      89           0 :         mbIsAnyModified = true;
      90             : 
      91           0 :         OUString aNewStr = GetText();
      92             : 
      93           0 :         bool bMultiTab = (rMark.GetSelectCount() > 1);
      94           0 :         OUString aVisibleStr;
      95           0 :         if( bMultiTab )
      96           0 :             aVisibleStr = mrDoc.GetString(mnCurrCol, mnCurrRow, mnStartTab);
      97             : 
      98           0 :         for( SCTAB nTab = 0, nTabCount = mrDoc.GetTableCount(); nTab < nTabCount; ++nTab )
      99             :         {
     100             :             //  always change the cell on the visible tab,
     101             :             //  on the other selected tabs only if they contain the same text
     102             : 
     103           0 :             if ((nTab == mnStartTab) ||
     104           0 :                 (bMultiTab && rMark.GetTableSelect(nTab) && mrDoc.GetString(mnCurrCol, mnCurrRow, nTab) == aVisibleStr))
     105             :             {
     106           0 :                 ScAddress aPos( mnCurrCol, mnCurrRow, nTab );
     107           0 :                 CellType eCellType = mrDoc.GetCellType( aPos );
     108           0 :                 bool bEmptyCell = eCellType == CELLTYPE_NONE;
     109             : 
     110           0 :                 if (mpUndoDoc && !bEmptyCell)
     111           0 :                     mrDoc.CopyCellToDocument(aPos, aPos, *mpUndoDoc);
     112             : 
     113           0 :                 if (eCellType == CELLTYPE_EDIT)
     114             :                 {
     115           0 :                     boost::scoped_ptr<EditTextObject> pEditObj(CreateTextObject());
     116           0 :                     mrDoc.SetEditText(aPos, *pEditObj, GetEditTextObjectPool());
     117             :                 }
     118             :                 else
     119           0 :                     mrDoc.SetString(aPos, aNewStr);
     120             : 
     121           0 :                 if (mpRedoDoc && !bEmptyCell)
     122           0 :                     mrDoc.CopyCellToDocument(aPos, aPos, *mpRedoDoc);
     123             : 
     124           0 :                 mrDocShell.PostPaintCell(aPos);
     125             :             }
     126           0 :         }
     127             :     }
     128             : 
     129           0 :     SCCOL nNewCol = mnCurrCol;
     130           0 :     SCROW nNewRow = mnCurrRow;
     131             : 
     132           0 :     if( mbInitialState )
     133             :     {
     134             :         /*  On very first call, decrement row to let GetNextSpellingCell() find
     135             :             the first cell of current range. */
     136           0 :         mbInitialState = false;
     137           0 :         --nNewRow;
     138             :     }
     139             : 
     140           0 :     bool bSheetSel = maSelState.GetSelectionType() == SC_SELECTTYPE_SHEET;
     141           0 :     bool bLoop = true;
     142           0 :     bool bFound = false;
     143           0 :     while( bLoop && !bFound )
     144             :     {
     145           0 :         bLoop = mrDoc.GetNextSpellingCell( nNewCol, nNewRow, mnStartTab, bSheetSel, rMark );
     146           0 :         if( bLoop )
     147             :         {
     148           0 :             FillFromCell( mnCurrCol, mnCurrRow, mnStartTab );
     149             : 
     150           0 :             if( mbWrappedInTable && ((nNewCol > mnStartCol) || ((nNewCol == mnStartCol) && (nNewRow >= mnStartRow))) )
     151             :             {
     152           0 :                 ShowFinishDialog();
     153           0 :                 bLoop = false;
     154           0 :                 mbFinished = true;
     155             :             }
     156           0 :             else if( nNewCol > MAXCOL )
     157             :             {
     158             :                 // no more cells in the sheet - try to restart at top of sheet
     159             : 
     160           0 :                 if( bSheetSel || ((mnStartCol == 0) && (mnStartRow == 0)) )
     161             :                 {
     162             :                     // conversion started at cell A1 or in selection, do not query to restart at top
     163           0 :                     ShowFinishDialog();
     164           0 :                     bLoop = false;
     165           0 :                     mbFinished = true;
     166             :                 }
     167           0 :                 else if( ShowTableWrapDialog() )
     168             :                 {
     169             :                     // conversion started anywhere but in cell A1, user wants to restart
     170           0 :                     nNewRow = MAXROW + 2;
     171           0 :                     mbWrappedInTable = true;
     172             :                 }
     173             :                 else
     174             :                 {
     175           0 :                     bLoop = false;
     176           0 :                     mbFinished = true;
     177             :                 }
     178             :             }
     179             :             else
     180             :             {
     181           0 :                 pPattern = mrDoc.GetPattern( nNewCol, nNewRow, mnStartTab );
     182           0 :                 if( pPattern && (pPattern != pLastPattern) )
     183             :                 {
     184           0 :                     pPattern->FillEditItemSet( pEditDefaults.get() );
     185           0 :                     SetDefaults( *pEditDefaults );
     186           0 :                     pLastPattern = pPattern;
     187             :                 }
     188             : 
     189             :                 // language changed?
     190           0 :                 const SfxPoolItem* pItem = mrDoc.GetAttr( nNewCol, nNewRow, mnStartTab, ATTR_FONT_LANGUAGE );
     191           0 :                 if( const SvxLanguageItem* pLangItem = PTR_CAST( SvxLanguageItem, pItem ) )
     192             :                 {
     193           0 :                     LanguageType eLang = static_cast< LanguageType >( pLangItem->GetValue() );
     194           0 :                     if( eLang == LANGUAGE_SYSTEM )
     195           0 :                         eLang = Application::GetSettings().GetLanguageTag().getLanguageType();   // never use SYSTEM for spelling
     196           0 :                     if( eLang != meCurrLang )
     197             :                     {
     198           0 :                         meCurrLang = eLang;
     199           0 :                         SetDefaultLanguage( eLang );
     200             :                     }
     201             :                 }
     202             : 
     203           0 :                 FillFromCell( nNewCol, nNewRow, mnStartTab );
     204             : 
     205           0 :                 bFound = bLoop && NeedsConversion();
     206             :             }
     207             :         }
     208             :     }
     209             : 
     210           0 :     if( bFound )
     211             :     {
     212           0 :         pViewShell->AlignToCursor( nNewCol, nNewRow, SC_FOLLOW_JUMP );
     213           0 :         pViewShell->SetCursor( nNewCol, nNewRow, true );
     214           0 :         mrViewData.GetView()->MakeEditView( this, nNewCol, nNewRow );
     215           0 :         EditView* pEditView = mrViewData.GetSpellingView();
     216             :         // maSelState.GetEditSelection() returns (0,0) if not in edit mode -> ok
     217           0 :         pEditView->SetSelection( maSelState.GetEditSelection() );
     218             : 
     219           0 :         ClearModifyFlag();
     220           0 :         mnCurrCol = nNewCol;
     221           0 :         mnCurrRow = nNewRow;
     222             :     }
     223             : 
     224           0 :     return bFound;
     225             : }
     226             : 
     227           0 : void ScConversionEngineBase::RestoreCursorPos()
     228             : {
     229           0 :     const ScAddress& rPos = maSelState.GetCellCursor();
     230           0 :     mrViewData.GetViewShell()->SetCursor( rPos.Col(), rPos.Row() );
     231           0 : }
     232             : 
     233           0 : bool ScConversionEngineBase::ShowTableWrapDialog()
     234             : {
     235             :     // default: no dialog, always restart at top
     236           0 :     return true;
     237             : }
     238             : 
     239           0 : void ScConversionEngineBase::ShowFinishDialog()
     240             : {
     241             :     // default: no dialog
     242           0 : }
     243             : 
     244             : // private --------------------------------------------------------------------
     245             : 
     246           0 : void ScConversionEngineBase::FillFromCell( SCCOL nCol, SCROW nRow, SCTAB nTab )
     247             : {
     248           0 :     ScAddress aPos(nCol, nRow, nTab);
     249             : 
     250           0 :     switch (mrDoc.GetCellType(aPos))
     251             :     {
     252             :         case CELLTYPE_STRING:
     253             :         {
     254           0 :             OUString aText = mrDoc.GetString(aPos);
     255           0 :             SetText( aText );
     256             :         }
     257           0 :         break;
     258             :         case CELLTYPE_EDIT:
     259             :         {
     260           0 :             const EditTextObject* pNewEditObj = mrDoc.GetEditText(aPos);
     261           0 :             if (pNewEditObj)
     262           0 :                 SetText(*pNewEditObj);
     263             :         }
     264           0 :         break;
     265             :         default:
     266           0 :             SetText(EMPTY_OUSTRING);
     267             :     }
     268           0 : }
     269             : 
     270           0 : ScSpellingEngine::ScSpellingEngine(
     271             :         SfxItemPool* pEnginePoolP, ScViewData& rViewData,
     272             :         ScDocument* pUndoDoc, ScDocument* pRedoDoc,
     273             :         XSpellCheckerRef xSpeller ) :
     274           0 :     ScConversionEngineBase( pEnginePoolP, rViewData, pUndoDoc, pRedoDoc )
     275             : {
     276           0 :     SetSpeller( xSpeller );
     277           0 : }
     278             : 
     279           0 : void ScSpellingEngine::ConvertAll( EditView& rEditView )
     280             : {
     281           0 :     EESpellState eState = EE_SPELL_OK;
     282           0 :     if( FindNextConversionCell() )
     283           0 :         eState = rEditView.StartSpeller( true );
     284             : 
     285             :     OSL_ENSURE( eState != EE_SPELL_NOSPELLER, "ScSpellingEngine::Convert - no spell checker" );
     286           0 :     if( eState == EE_SPELL_NOLANGUAGE )
     287             :     {
     288           0 :         vcl::Window* pParent = GetDialogParent();
     289           0 :         ScWaitCursorOff aWaitOff( pParent );
     290           0 :         ScopedVclPtr<InfoBox>::Create( pParent, ScGlobal::GetRscString( STR_NOLANGERR ) )->Execute();
     291             :     }
     292           0 : }
     293             : 
     294           0 : bool ScSpellingEngine::SpellNextDocument()
     295             : {
     296           0 :     return FindNextConversionCell();
     297             : }
     298             : 
     299           0 : bool ScSpellingEngine::NeedsConversion()
     300             : {
     301           0 :     return HasSpellErrors() != EE_SPELL_OK;
     302             : }
     303             : 
     304           0 : bool ScSpellingEngine::ShowTableWrapDialog()
     305             : {
     306           0 :     vcl::Window* pParent = GetDialogParent();
     307           0 :     ScWaitCursorOff aWaitOff( pParent );
     308             :     ScopedVclPtrInstance<MessBox> aMsgBox( pParent, WinBits( WB_YES_NO | WB_DEF_YES ),
     309           0 :         ScGlobal::GetRscString( STR_MSSG_DOSUBTOTALS_0 ),
     310           0 :         ScGlobal::GetRscString( STR_SPELLING_BEGIN_TAB) );
     311           0 :     return aMsgBox->Execute() == RET_YES;
     312             : }
     313             : 
     314           0 : void ScSpellingEngine::ShowFinishDialog()
     315             : {
     316           0 :     vcl::Window* pParent = GetDialogParent();
     317           0 :     ScWaitCursorOff aWaitOff( pParent );
     318           0 :     ScopedVclPtr<InfoBox>::Create( pParent, ScGlobal::GetRscString( STR_SPELLING_STOP_OK ) )->Execute();
     319           0 : }
     320             : 
     321           0 : vcl::Window* ScSpellingEngine::GetDialogParent()
     322             : {
     323           0 :     sal_uInt16 nWinId = ScSpellDialogChildWindow::GetChildWindowId();
     324           0 :     SfxViewFrame* pViewFrm = mrViewData.GetViewShell()->GetViewFrame();
     325           0 :     if( pViewFrm->HasChildWindow( nWinId ) )
     326           0 :         if( SfxChildWindow* pChild = pViewFrm->GetChildWindow( nWinId ) )
     327           0 :             if( vcl::Window* pWin = pChild->GetWindow() )
     328           0 :                 if( pWin->IsVisible() )
     329           0 :                     return pWin;
     330             : 
     331             :     // fall back to standard dialog parent
     332           0 :     return ScDocShell::GetActiveDialogParent();
     333             : }
     334             : 
     335           0 : ScConversionParam::ScConversionParam( ScConversionType eConvType ) :
     336             :     meConvType( eConvType ),
     337             :     meSourceLang( LANGUAGE_NONE ),
     338             :     meTargetLang( LANGUAGE_NONE ),
     339             :     mnOptions( 0 ),
     340             :     mbUseTargetFont( false ),
     341           0 :     mbIsInteractive( false )
     342             : {
     343           0 : }
     344             : 
     345           0 : 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           0 :     mbIsInteractive( bIsInteractive )
     353             : {
     354           0 :     if (LANGUAGE_KOREAN == eLang)
     355           0 :         mnOptions = i18n::TextConversionOption::CHARACTER_BY_CHARACTER;
     356           0 : }
     357             : 
     358           0 : 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           0 :     mbIsInteractive( bIsInteractive )
     368             : {
     369           0 :     if (LANGUAGE_KOREAN == meSourceLang && LANGUAGE_KOREAN == meTargetLang)
     370           0 :         mnOptions = i18n::TextConversionOption::CHARACTER_BY_CHARACTER;
     371           0 : }
     372             : 
     373           0 : ScTextConversionEngine::ScTextConversionEngine(
     374             :         SfxItemPool* pEnginePoolP, ScViewData& rViewData,
     375             :         const ScConversionParam& rConvParam,
     376             :         ScDocument* pUndoDoc, ScDocument* pRedoDoc ) :
     377             :     ScConversionEngineBase( pEnginePoolP, rViewData, pUndoDoc, pRedoDoc ),
     378           0 :     maConvParam( rConvParam )
     379             : {
     380           0 : }
     381             : 
     382           0 : void ScTextConversionEngine::ConvertAll( EditView& rEditView )
     383             : {
     384           0 :     if( FindNextConversionCell() )
     385             :     {
     386             :         rEditView.StartTextConversion(
     387           0 :             maConvParam.GetSourceLang(), maConvParam.GetTargetLang(), maConvParam.GetTargetFont(),
     388           0 :             maConvParam.GetOptions(), maConvParam.IsInteractive(), true );
     389             :         // #i34769# restore initial cursor position
     390           0 :         RestoreCursorPos();
     391             :     }
     392           0 : }
     393             : 
     394           0 : bool ScTextConversionEngine::ConvertNextDocument()
     395             : {
     396           0 :     return FindNextConversionCell();
     397             : }
     398             : 
     399           0 : bool ScTextConversionEngine::NeedsConversion()
     400             : {
     401           0 :     return HasConvertibleTextPortion( maConvParam.GetSourceLang() );
     402         156 : }
     403             : 
     404             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11