Files
libreoffice/sw/source/core/edit/acorrect.cxx

523 lines
16 KiB
C++
Raw Normal View History

2000-09-18 23:08:29 +00:00
/*************************************************************************
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2000-09-18 23:08:29 +00:00
*
* Copyright 2008 by Sun Microsystems, Inc.
2000-09-18 23:08:29 +00:00
*
* OpenOffice.org - a multi-platform office productivity suite
2000-09-18 23:08:29 +00:00
*
* $RCSfile: acorrect.cxx,v $
* $Revision: 1.17 $
2000-09-18 23:08:29 +00:00
*
* This file is part of OpenOffice.org.
2000-09-18 23:08:29 +00:00
*
* OpenOffice.org is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 3
* only, as published by the Free Software Foundation.
2000-09-18 23:08:29 +00:00
*
* OpenOffice.org is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License version 3 for more details
* (a copy is included in the LICENSE file that accompanied this code).
2000-09-18 23:08:29 +00:00
*
* You should have received a copy of the GNU Lesser General Public License
* version 3 along with OpenOffice.org. If not, see
* <http://www.openoffice.org/license.html>
* for a copy of the LGPLv3 License.
2000-09-18 23:08:29 +00:00
*
************************************************************************/
// MARKER(update_precomp.py): autogen include statement, do not remove
#include "precompiled_sw.hxx"
2000-09-18 23:08:29 +00:00
#define _STD_VAR_ARRAYS
#include <hintids.hxx>
#ifndef _SVX_SVXIDS_HRC
#include <svx/svxids.hrc>
#endif
#include <svx/langitem.hxx>
#include <fmtinfmt.hxx>
#include <txtatr.hxx>
#include <txtinet.hxx>
#include <fmthbsh.hxx>
#include <editsh.hxx>
#include <doc.hxx>
#include <pam.hxx>
#include <ndtxt.hxx>
#include <acorrect.hxx>
#include <shellio.hxx>
#include <swundo.hxx>
#include <viscrs.hxx>
#include <svx/acorrcfg.hxx>
using namespace ::com::sun::star;
2000-09-18 23:08:29 +00:00
class _PaMIntoCrsrShellRing
{
SwCrsrShell& rSh;
SwPaM &rDelPam, &rCrsr;
Ring *pPrevDelPam, *pPrevCrsr;
void RemoveFromRing( SwPaM& rPam, Ring* pPrev );
public:
_PaMIntoCrsrShellRing( SwCrsrShell& rSh, SwPaM& rCrsr, SwPaM& rPam );
~_PaMIntoCrsrShellRing();
};
_PaMIntoCrsrShellRing::_PaMIntoCrsrShellRing( SwCrsrShell& rCSh,
SwPaM& rShCrsr, SwPaM& rPam )
: rSh( rCSh ), rDelPam( rPam ), rCrsr( rShCrsr )
{
SwPaM* pShCrsr = rSh._GetCrsr();
pPrevDelPam = rDelPam.GetPrev();
pPrevCrsr = rCrsr.GetPrev();
rDelPam.MoveRingTo( pShCrsr );
rCrsr.MoveRingTo( pShCrsr );
}
_PaMIntoCrsrShellRing::~_PaMIntoCrsrShellRing()
{
// und den Pam wieder herausnehmen:
RemoveFromRing( rDelPam, pPrevDelPam );
RemoveFromRing( rCrsr, pPrevCrsr );
}
void _PaMIntoCrsrShellRing::RemoveFromRing( SwPaM& rPam, Ring* pPrev )
{
Ring *p, *pNext = (Ring*)&rPam;
do {
p = pNext;
pNext = p->GetNext();
p->MoveTo( &rPam );
} while( p != pPrev );
}
SwAutoCorrDoc::SwAutoCorrDoc( SwEditShell& rEditShell, SwPaM& rPam,
sal_Unicode cIns )
: rEditSh( rEditShell ), rCrsr( rPam ), pIdx( 0 ),
nUndoId( UNDO_EMPTY ),
bUndoIdInitialized( cIns ? false : true )
2000-09-18 23:08:29 +00:00
{
}
SwAutoCorrDoc::~SwAutoCorrDoc()
{
if( UNDO_EMPTY != nUndoId )
2000-09-18 23:08:29 +00:00
rEditSh.EndUndo( nUndoId );
delete pIdx;
}
void SwAutoCorrDoc::DeleteSel( SwPaM& rDelPam )
{
SwDoc* pDoc = rEditSh.GetDoc();
if( pDoc->IsAutoFmtRedline() )
{
// damit der DelPam auch verschoben wird, in den Shell-Cursr-Ring
// mit aufnehmen !!
_PaMIntoCrsrShellRing aTmp( rEditSh, rCrsr, rDelPam );
pDoc->DeleteAndJoin( rDelPam );
}
else
pDoc->Delete( rDelPam );
}
BOOL SwAutoCorrDoc::Delete( xub_StrLen nStt, xub_StrLen nEnd )
{
const SwNodeIndex& rNd = rCrsr.GetPoint()->nNode;
SwPaM aSel( rNd, nStt, rNd, nEnd );
DeleteSel( aSel );
if( bUndoIdInitialized )
bUndoIdInitialized = true;
2000-09-18 23:08:29 +00:00
return TRUE;
}
BOOL SwAutoCorrDoc::Insert( xub_StrLen nPos, const String& rTxt )
{
SwPaM aPam( rCrsr.GetPoint()->nNode.GetNode(), nPos );
rEditSh.GetDoc()->Insert( aPam, rTxt, true );
if( !bUndoIdInitialized )
2000-09-18 23:08:29 +00:00
{
bUndoIdInitialized = true;
2000-09-18 23:08:29 +00:00
if( 1 == rTxt.Len() )
rEditSh.StartUndo( nUndoId = UNDO_AUTOCORRECT );
}
return TRUE;
}
BOOL SwAutoCorrDoc::Replace( xub_StrLen nPos, const String& rTxt )
{
SwPaM* pPam = &rCrsr;
if( pPam->GetPoint()->nContent.GetIndex() != nPos )
{
pPam = new SwPaM( *rCrsr.GetPoint() );
pPam->GetPoint()->nContent = nPos;
}
BOOL bChg = TRUE;
SwTxtNode* pNd = pPam->GetNode()->GetTxtNode();
if( pNd )
{
// TextAttribute ohne Ende duerfen nie ersetzt werden!
sal_Unicode cChr;
for( xub_StrLen n = 0, nLen = rTxt.Len(); n < nLen; ++n )
if( ( CH_TXTATR_BREAKWORD == (cChr = pNd->GetTxt().
GetChar( n + nPos )) || CH_TXTATR_INWORD == cChr ) &&
pNd->GetTxtAttr( n + nPos ) )
{
bChg = FALSE;
break;
}
}
if( bChg )
{
SwDoc* pDoc = rEditSh.GetDoc();
// if( !pDoc->IsAutoFmtRedline() &&
// pPam != &rCrsr ) // nur an akt. Position das Redline sichern
// pDoc->SetRedlineMode_intern( eOld | REDLINE_IGNORE );
if( pDoc->IsAutoFmtRedline() )
{
if( nPos == pNd->GetTxt().Len() ) // am Ende erfolgt ein Insert
pDoc->Insert( *pPam, rTxt, true );
2000-09-18 23:08:29 +00:00
else
{
_PaMIntoCrsrShellRing aTmp( rEditSh, rCrsr, *pPam );
pPam->SetMark();
pPam->GetPoint()->nContent = Min( pNd->GetTxt().Len(),
xub_StrLen( nPos + rTxt.Len() ));
pDoc->Replace( *pPam, rTxt, FALSE );
pPam->Exchange();
pPam->DeleteMark();
}
}
else
pDoc->Overwrite( *pPam, rTxt );
// pDoc->SetRedlineMode_intern( eOld );
if( bUndoIdInitialized )
2000-09-18 23:08:29 +00:00
{
bUndoIdInitialized = true;
2000-09-18 23:08:29 +00:00
if( 1 == rTxt.Len() )
rEditSh.StartUndo( nUndoId = UNDO_AUTOCORRECT );
}
}
if( pPam != &rCrsr )
delete pPam;
return TRUE;
}
BOOL SwAutoCorrDoc::SetAttr( xub_StrLen nStt, xub_StrLen nEnd, USHORT nSlotId,
SfxPoolItem& rItem )
{
const SwNodeIndex& rNd = rCrsr.GetPoint()->nNode;
SwPaM aPam( rNd, nStt, rNd, nEnd );
SfxItemPool& rPool = rEditSh.GetDoc()->GetAttrPool();
USHORT nWhich = rPool.GetWhich( nSlotId, FALSE );
2000-09-18 23:08:29 +00:00
if( nWhich )
{
rItem.SetWhich( nWhich );
SfxItemSet aSet( rPool, aCharFmtSetRange );
SetAllScriptItem( aSet, rItem );
rEditSh.GetDoc()->SetFmtItemByAutoFmt( aPam, aSet );
2000-09-18 23:08:29 +00:00
if( bUndoIdInitialized )
bUndoIdInitialized = true;
2000-09-18 23:08:29 +00:00
}
return 0 != nWhich;
}
BOOL SwAutoCorrDoc::SetINetAttr( xub_StrLen nStt, xub_StrLen nEnd, const String& rURL )
{
const SwNodeIndex& rNd = rCrsr.GetPoint()->nNode;
SwPaM aPam( rNd, nStt, rNd, nEnd );
SfxItemSet aSet( rEditSh.GetDoc()->GetAttrPool(),
RES_TXTATR_INETFMT, RES_TXTATR_INETFMT );
aSet.Put( SwFmtINetFmt( rURL, aEmptyStr ));
rEditSh.GetDoc()->SetFmtItemByAutoFmt( aPam, aSet );
if( bUndoIdInitialized )
bUndoIdInitialized = true;
2000-09-18 23:08:29 +00:00
return TRUE;
}
// returne den Text eines vorherigen Absatzes.
// Dieser darf nicht leer sein!
// Gibt es diesen nicht oder gibt es davor nur Leere, dann returne 0
// Das Flag gibt an:
// TRUE: den, vor der normalen Einfuegeposition (TRUE)
// FALSE: den, in den das korrigierte Wort eingfuegt wurde.
// (Muss nicht der gleiche Absatz sein!!!!)
const String* SwAutoCorrDoc::GetPrevPara( BOOL bAtNormalPos )
{
const String* pStr = 0;
if( bAtNormalPos || !pIdx )
pIdx = new SwNodeIndex( rCrsr.GetPoint()->nNode, -1 );
else
(*pIdx)--;
SwTxtNode* pTNd = pIdx->GetNode().GetTxtNode();
while( pTNd && !pTNd->GetTxt().Len() )
{
(*pIdx)--;
pTNd = pIdx->GetNode().GetTxtNode();
}
CWS-TOOLING: integrate CWS outlinelevel 2008-12-19 10:32:51 +0100 od r265718 : #i70748# method <SwWW8Writer::StartTOX(..)> - correction for custom to outline style assigned paragraph styles 2008-12-19 09:24:41 +0100 od r265715 : #i70748# method <HandleModifyAtTxtNode(..)> - retrieve former applied list style before potential reset of empty list style due to set outline level 2008-12-17 15:33:57 +0100 hde r265608 : #i97013# 2008-12-17 14:43:42 +0100 od r265603 : #i70748# adjust fix i44177 - adjustment of to outline style assigned paragraph styles only for OOo-Templates. 2008-12-17 12:59:42 +0100 od r265598 : #i70748# method <XMLTextImportHelper::SetStyleAndAttrs(..)> - special handling of OOo 2.x document regarding outline numbering of headings. 2008-12-17 12:45:53 +0100 od r265597 : #i70748# method <HandleModifyAtTxtNode(..)> - correct determination of new and former applied list style 2008-12-17 10:18:41 +0100 od r265581 : #i97312# method <XMLTextNumRuleInfo::Set(..)> - check, if numbering rules instance contains any numbering rule. 2008-12-16 14:34:22 +0100 hde r265542 : #i97013 2008-12-16 14:20:24 +0100 od r265541 : #i70748# adjust documentation of "Which"-ID numbers 2008-12-16 14:19:49 +0100 od r265539 : #i70748# Adjust Attribute-Function-Mapping table due to new attribute 2008-12-16 14:06:24 +0100 od r265538 : #i70748# - Correct handling of to outline style assigned paragraph styles on reset of all paragraph style attributes - WW8 import: Consider refactoring of paragraph style's outline level attribute - NO_NUMBERING define no longer exsits. 2008-12-16 10:37:19 +0100 od r265530 : #i70478# Correction on moving outline paragraph up respectively down in its outline level: - Check also outline paragraph, which are not an outline via a to outline style assigned paragraph style, if action is applicable. 2008-12-16 09:10:13 +0100 hde r265523 : #i97277 2008-12-16 09:09:30 +0100 hde r265522 : #i97277 2008-12-16 09:07:40 +0100 hde r265521 : Added control and purged obsolete id's 2008-12-10 13:09:36 +0100 od r265174 : #i70748# correction of previous fix due to warning-free code 2008-12-10 13:03:30 +0100 od r265172 : #i70748# method <XMLTextImportHelper::SetStyleAndAttrs(..)> - Due to performance issues avoid retrieving outline level value from paragraph's paragraph style for each paragraph. Instead retrieve current outline level value from paragraph, which is the inherited value from its paragraph style, and only when it is needed. 2008-12-09 16:50:36 +0100 ufi r265117 : help 2008-12-09 16:49:50 +0100 ufi r265116 : help 2008-12-09 15:45:05 +0100 od r265107 : #i70748# method <XMLTextImportHelper::SetStyleAndAttrs(..)> - some minor rework and check access to paragraph styles 2008-12-09 15:42:04 +0100 od r265106 : #i70748# adjust name of local variable to avoid hiding of class variable 2008-12-09 15:40:51 +0100 od r265105 : #i70748# correct initialization order 2008-12-09 15:35:07 +0100 od r265101 : #i70748# remove duplicate entry in certain property map 2008-12-09 15:33:39 +0100 od r265100 : #i70748# add end of file token 2008-12-07 15:11:54 +0100 zhaojianwei r264952 : conflicts left not to be resolved when cws rebase 2008-12-04 14:52:21 +0100 zhaojianwei r264846 : CWS-TOOLING: rebase CWS outlinelevel to trunk@264325 (milestone: DEV300:m36) 2008-11-28 03:42:09 +0100 zhaojianwei r264525 : #i70748#: migrate CWS outlinelevel to SVN 2008-11-28 03:41:39 +0100 zhaojianwei r264524 : #i70748#: migrate CWS outlinelevel to SVN 2008-11-28 03:41:11 +0100 zhaojianwei r264523 : #i70748#: migrate CWS outlinelevel to SVN
2009-01-07 11:57:24 +00:00
//if( pTNd && NO_NUMBERING == pTNd->GetTxtColl()->GetOutlineLevel() )
if( pTNd && 0 == pTNd->GetAttrOutlineLevel() )//#outline level,zhaojianwei
2000-09-18 23:08:29 +00:00
pStr = &pTNd->GetTxt();
if( bUndoIdInitialized )
bUndoIdInitialized = true;
2000-09-18 23:08:29 +00:00
return pStr;
}
BOOL SwAutoCorrDoc::ChgAutoCorrWord( xub_StrLen & rSttPos, xub_StrLen nEndPos,
SvxAutoCorrect& rACorrect,
const String** ppPara )
{
if( bUndoIdInitialized )
bUndoIdInitialized = true;
2000-09-18 23:08:29 +00:00
// Absatz-Anfang oder ein Blank gefunden, suche nach dem Wort
// Kuerzel im Auto
SwTxtNode* pTxtNd = rCrsr.GetNode()->GetTxtNode();
ASSERT( pTxtNd, "wo ist denn der TextNode?" );
BOOL bRet = FALSE;
if( nEndPos == rSttPos )
return bRet;
LanguageType eLang = GetLanguage(nEndPos, FALSE);
if(LANGUAGE_SYSTEM == eLang)
eLang = (LanguageType)GetAppLanguage();
2000-09-18 23:08:29 +00:00
//JP 22.04.99: Bug 63883 - Sonderbehandlung fuer Punkte.
BOOL bLastCharIsPoint = nEndPos < pTxtNd->GetTxt().Len() &&
'.' == pTxtNd->GetTxt().GetChar( nEndPos );
const SvxAutocorrWord* pFnd = rACorrect.SearchWordsInList(
pTxtNd->GetTxt(), rSttPos, nEndPos, *this, eLang );
SwDoc* pDoc = rEditSh.GetDoc();
if( pFnd )
{
const SwNodeIndex& rNd = rCrsr.GetPoint()->nNode;
SwPaM aPam( rNd, rSttPos, rNd, nEndPos );
if( pFnd->IsTextOnly() )
{
//JP 22.04.99: Bug 63883 - Sonderbehandlung fuer Punkte.
if( !bLastCharIsPoint || !pFnd->GetLong().Len() ||
'.' != pFnd->GetLong().GetChar( pFnd->GetLong().Len() - 1 ) )
{
// replace the selection
pDoc->Replace( aPam, pFnd->GetLong(), false);
2000-09-18 23:08:29 +00:00
bRet = TRUE;
}
}
else
{
SwTextBlocks aTBlks( rACorrect.GetAutoCorrFileName( eLang, FALSE, TRUE ));
2000-09-18 23:08:29 +00:00
USHORT nPos = aTBlks.GetIndex( pFnd->GetShort() );
if( USHRT_MAX != nPos && aTBlks.BeginGetDoc( nPos ) )
{
DeleteSel( aPam );
pDoc->DontExpandFmt( *aPam.GetPoint() );
if( ppPara )
{
ASSERT( !pIdx, "wer hat seinen Index nicht geloescht?" );
pIdx = new SwNodeIndex( rCrsr.GetPoint()->nNode, -1 );
}
//
SwDoc* pAutoDoc = aTBlks.GetDoc();
SwNodeIndex aSttIdx( pAutoDoc->GetNodes().GetEndOfExtras(), 1 );
SwCntntNode* pCntntNd = pAutoDoc->GetNodes().GoNext( &aSttIdx );
SwPaM aCpyPam( aSttIdx );
const SwTableNode* pTblNd = pCntntNd->FindTableNode();
if( pTblNd )
{
aCpyPam.GetPoint()->nContent.Assign( 0, 0 );
aCpyPam.GetPoint()->nNode = *pTblNd;
}
aCpyPam.SetMark();
// dann bis zum Ende vom Nodes Array
aCpyPam.GetPoint()->nNode.Assign( pAutoDoc->GetNodes().GetEndOfContent(), -1 );
pCntntNd = aCpyPam.GetCntntNode();
aCpyPam.GetPoint()->nContent.Assign( pCntntNd, pCntntNd->Len() );
SwDontExpandItem aExpItem;
aExpItem.SaveDontExpandItems( *aPam.GetPoint() );
pAutoDoc->Copy( aCpyPam, *aPam.GetPoint() );
aExpItem.RestoreDontExpandItems( *aPam.GetPoint() );
if( ppPara )
{
(*pIdx)++;
pTxtNd = pIdx->GetNode().GetTxtNode();
}
bRet = TRUE;
}
aTBlks.EndGetDoc();
}
}
if( bRet && ppPara && pTxtNd )
*ppPara = &pTxtNd->GetTxt();
return bRet;
}
// wird nach dem austauschen der Zeichen von den Funktionen
// - FnCptlSttWrd
// - FnCptlSttSntnc
// gerufen. Dann koennen die Worte ggfs. in die Ausnahmelisten
// aufgenommen werden.
void SwAutoCorrDoc::SaveCpltSttWord( ULONG nFlag, xub_StrLen nPos,
const String& rExceptWord,
sal_Unicode cChar )
{
ULONG nNode = pIdx ? pIdx->GetIndex() : rCrsr.GetPoint()->nNode.GetIndex();
LanguageType eLang = GetLanguage(nPos, FALSE);
rEditSh.GetDoc()->SetAutoCorrExceptWord( new SwAutoCorrExceptWord( nFlag,
nNode, nPos, rExceptWord, cChar, eLang ));
}
LanguageType SwAutoCorrDoc::GetLanguage( xub_StrLen nPos, BOOL bPrevPara ) const
{
LanguageType eRet = LANGUAGE_SYSTEM;
SwTxtNode* pNd = (( bPrevPara && pIdx )
? *pIdx
: rCrsr.GetPoint()->nNode ).GetNode().GetTxtNode();
if( pNd )
2001-02-21 16:39:20 +00:00
eRet = pNd->GetLang( nPos, 0 );
2000-09-18 23:08:29 +00:00
if(LANGUAGE_SYSTEM == eRet)
eRet = (LanguageType)GetAppLanguage();
2000-09-18 23:08:29 +00:00
return eRet;
}
void SwAutoCorrExceptWord::CheckChar( const SwPosition& rPos, sal_Unicode cChr )
{
// nur testen ob es eine Verbesserung ist. Wenn ja, dann das Wort
// in die Ausnahmeliste aufnehmen.
if( cChar == cChr && rPos.nNode.GetIndex() == nNode &&
rPos.nContent.GetIndex() == nCntnt )
{
// die akt. Autokorrektur besorgen:
SvxAutoCorrect* pACorr = SvxAutoCorrCfg::Get()->GetAutoCorrect();
2000-09-18 23:08:29 +00:00
// dann in die Liste aufnehmen:
if( CptlSttWrd & nFlags )
pACorr->AddWrtSttException( sWord, eLanguage );
else if( CptlSttSntnc & nFlags )
pACorr->AddCplSttException( sWord, eLanguage );
}
}
BOOL SwAutoCorrExceptWord::CheckDelChar( const SwPosition& rPos )
{
BOOL bRet = FALSE;
if( !bDeleted && rPos.nNode.GetIndex() == nNode &&
rPos.nContent.GetIndex() == nCntnt )
bDeleted = bRet = TRUE;
return bRet;
}
SwDontExpandItem::~SwDontExpandItem()
{
delete pDontExpItems;
}
void SwDontExpandItem::SaveDontExpandItems( const SwPosition& rPos )
{
const SwTxtNode* pTxtNd = rPos.nNode.GetNode().GetTxtNode();
if( pTxtNd )
{
pDontExpItems = new SfxItemSet( ((SwDoc*)pTxtNd->GetDoc())->GetAttrPool(),
aCharFmtSetRange );
xub_StrLen n = rPos.nContent.GetIndex();
if( !pTxtNd->GetAttr( *pDontExpItems, n, n,
n != pTxtNd->GetTxt().Len() ))
delete pDontExpItems, pDontExpItems = 0;
}
}
void SwDontExpandItem::RestoreDontExpandItems( const SwPosition& rPos )
{
SwTxtNode* pTxtNd = rPos.nNode.GetNode().GetTxtNode();
if( pTxtNd )
{
xub_StrLen nStart = rPos.nContent.GetIndex();
if( nStart == pTxtNd->GetTxt().Len() )
pTxtNd->FmtToTxtAttr( pTxtNd );
if( pTxtNd->GetpSwpHints() && pTxtNd->GetpSwpHints()->Count() )
{
const USHORT nSize = pTxtNd->GetpSwpHints()->Count();
USHORT n;
2000-09-18 23:08:29 +00:00
xub_StrLen nAttrStart;
const xub_StrLen* pAttrEnd;
2000-09-18 23:08:29 +00:00
for( n = 0; n < nSize; ++n )
{
SwTxtAttr* pHt = pTxtNd->GetpSwpHints()->GetHt( n );
nAttrStart = *pHt->GetStart();
if( nAttrStart > nStart ) // ueber den Bereich hinaus
break;
if( 0 != ( pAttrEnd = pHt->GetEnd() ) &&
( ( nAttrStart < nStart &&
( pHt->DontExpand() ? nStart < *pAttrEnd
: nStart <= *pAttrEnd )) ||
( nStart == nAttrStart &&
( nAttrStart == *pAttrEnd || !nStart ))) )
{
const SfxPoolItem* pItem;
if( !pDontExpItems || SFX_ITEM_SET != pDontExpItems->
GetItemState( pHt->Which(), FALSE, &pItem ) ||
*pItem != pHt->GetAttr() )
{
// das Attribut war vorher nicht in dieser Form im Absatz
// gesetzt, also kann es nur durchs einfuegen/kopieren erzeugt
// worden sein. Damit ist es ein Kandiadat fuers DontExpand
pHt->SetDontExpand( TRUE );
}
}
}
}
}
}