Files
libreoffice/svtools/source/edit/xtextedt.cxx

434 lines
13 KiB
C++
Raw Normal View History

2000-09-18 16:07:07 +00:00
/*************************************************************************
*
* OpenOffice.org - a multi-platform office productivity suite
2000-09-18 16:07:07 +00:00
*
* $RCSfile: xtextedt.cxx,v $
2000-09-18 16:07:07 +00:00
*
* $Revision: 1.12 $
2000-09-18 16:07:07 +00:00
*
* last change: $Author: obo $ $Date: 2006-09-17 14:49:23 $
2000-09-18 16:07:07 +00:00
*
* The Contents of this file are made available subject to
* the terms of GNU Lesser General Public License Version 2.1.
2000-09-18 16:07:07 +00:00
*
*
* GNU Lesser General Public License Version 2.1
* =============================================
* Copyright 2005 by Sun Microsystems, Inc.
* 901 San Antonio Road, Palo Alto, CA 94303, USA
2000-09-18 16:07:07 +00:00
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1, as published by the Free Software Foundation.
2000-09-18 16:07:07 +00:00
*
* This library 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 for more details.
2000-09-18 16:07:07 +00:00
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
2000-09-18 16:07:07 +00:00
*
************************************************************************/
// MARKER(update_precomp.py): autogen include statement, do not remove
#include "precompiled_svtools.hxx"
2000-09-18 16:07:07 +00:00
#include <xtextedt.hxx>
#include <vcl/svapp.hxx> // International
2000-11-20 13:05:50 +00:00
#include <unotools/textsearch.hxx>
2001-03-09 09:21:41 +00:00
#ifndef _COM_SUN_STAR_UTIL_SEARCHOPTIONS_HPP_
#include <com/sun/star/util/SearchOptions.hpp>
#endif
#ifndef _COM_SUN_STAR_UTIL_SEARCHFLAGS_HPP_
#include <com/sun/star/util/SearchFlags.hpp>
#endif
using namespace ::com::sun::star;
2000-09-18 16:07:07 +00:00
// -------------------------------------------------------------------------
// class ExtTextEngine
// -------------------------------------------------------------------------
ExtTextEngine::ExtTextEngine() : maGroupChars( String::CreateFromAscii( "(){}[]", 6 ) )
{
}
ExtTextEngine::~ExtTextEngine()
{
}
TextSelection ExtTextEngine::MatchGroup( const TextPaM& rCursor ) const
{
TextSelection aSel( rCursor );
USHORT nPos = rCursor.GetIndex();
ULONG nPara = rCursor.GetPara();
ULONG nParas = GetParagraphCount();
if ( ( nPara < nParas ) && ( nPos < GetTextLen( nPara ) ) )
{
USHORT nMatchChar = maGroupChars.Search( GetText( rCursor.GetPara() ).GetChar( nPos ) );
if ( nMatchChar != STRING_NOTFOUND )
{
if ( ( nMatchChar % 2 ) == 0 )
{
// Vorwaerts suchen...
sal_Unicode nSC = maGroupChars.GetChar( nMatchChar );
sal_Unicode nEC = maGroupChars.GetChar( nMatchChar+1 );
USHORT nCur = nPos+1;
USHORT nLevel = 1;
while ( nLevel && ( nPara < nParas ) )
{
XubString aStr = GetText( nPara );
while ( nCur < aStr.Len() )
{
if ( aStr.GetChar( nCur ) == nSC )
nLevel++;
else if ( aStr.GetChar( nCur ) == nEC )
{
nLevel--;
if ( !nLevel )
break; // while nCur...
}
nCur++;
}
if ( nLevel )
{
nPara++;
nCur = 0;
}
}
if ( nLevel == 0 ) // gefunden
{
aSel.GetStart() = rCursor;
aSel.GetEnd() = TextPaM( nPara, nCur+1 );
}
}
else
{
// Rueckwaerts suchen...
xub_Unicode nEC = maGroupChars.GetChar( nMatchChar );
xub_Unicode nSC = maGroupChars.GetChar( nMatchChar-1 );
USHORT nCur = rCursor.GetIndex()-1;
USHORT nLevel = 1;
while ( nLevel )
{
if ( GetTextLen( nPara ) )
{
XubString aStr = GetText( nPara );
while ( nCur )
{
if ( aStr.GetChar( nCur ) == nSC )
{
nLevel--;
if ( !nLevel )
break; // while nCur...
}
else if ( aStr.GetChar( nCur ) == nEC )
nLevel++;
nCur--;
}
}
if ( nLevel )
{
if ( nPara )
{
nPara--;
nCur = GetTextLen( nPara )-1; // egal ob negativ, weil if Len()
}
else
break;
}
}
if ( nLevel == 0 ) // gefunden
{
aSel.GetStart() = rCursor;
aSel.GetStart().GetIndex()++; // hinter das Zeichen
aSel.GetEnd() = TextPaM( nPara, nCur );
}
}
}
}
return aSel;
}
2001-03-09 09:21:41 +00:00
BOOL ExtTextEngine::Search( TextSelection& rSel, const util::SearchOptions& rSearchOptions, BOOL bForward )
2000-09-18 16:07:07 +00:00
{
TextSelection aSel( rSel );
aSel.Justify();
2002-12-03 11:02:30 +00:00
BOOL bSearchInSelection = (0 != (rSearchOptions.searchFlag & util::SearchFlags::REG_NOT_BEGINOFLINE) );
2001-03-09 09:21:41 +00:00
2000-09-18 16:07:07 +00:00
TextPaM aStartPaM( aSel.GetEnd() );
2001-03-09 09:21:41 +00:00
if ( aSel.HasRange() && ( ( bSearchInSelection && bForward ) || ( !bSearchInSelection && !bForward ) ) )
2000-09-18 16:07:07 +00:00
{
aStartPaM = aSel.GetStart();
}
BOOL bFound = FALSE;
ULONG nStartNode, nEndNode;
2001-03-09 09:21:41 +00:00
if ( bSearchInSelection )
2000-09-18 16:07:07 +00:00
nEndNode = bForward ? aSel.GetEnd().GetPara() : aSel.GetStart().GetPara();
else
nEndNode = bForward ? (GetParagraphCount()-1) : 0;
nStartNode = aStartPaM.GetPara();
2001-03-09 09:21:41 +00:00
util::SearchOptions aOptions( rSearchOptions );
aOptions.Locale = Application::GetSettings().GetLocale();
utl::TextSearch aSearcher( rSearchOptions );
2000-09-18 16:07:07 +00:00
// ueber die Absaetze iterieren...
for ( ULONG nNode = nStartNode;
bForward ? ( nNode <= nEndNode) : ( nNode >= nEndNode );
bForward ? nNode++ : nNode-- )
{
String aText = GetText( nNode );
USHORT nStartPos = 0;
USHORT nEndPos = aText.Len();
if ( nNode == nStartNode )
{
if ( bForward )
nStartPos = aStartPaM.GetIndex();
else
nEndPos = aStartPaM.GetIndex();
}
2001-03-09 09:21:41 +00:00
if ( ( nNode == nEndNode ) && bSearchInSelection )
2000-09-18 16:07:07 +00:00
{
if ( bForward )
nEndPos = aSel.GetEnd().GetIndex();
else
nStartPos = aSel.GetStart().GetIndex();
}
if ( bForward )
bFound = aSearcher.SearchFrwrd( aText, &nStartPos, &nEndPos );
else
bFound = aSearcher.SearchBkwrd( aText, &nEndPos, &nStartPos );
if ( bFound )
{
rSel.GetStart().GetPara() = nNode;
rSel.GetStart().GetIndex() = nStartPos;
2000-09-18 16:07:07 +00:00
rSel.GetEnd().GetPara() = nNode;
rSel.GetEnd().GetIndex() = nEndPos;
2000-09-18 16:07:07 +00:00
// Ueber den Absatz selektieren?
// Select over the paragraph?
// FIXME This should be max long...
if( nEndPos == sal::static_int_cast<USHORT>(-1) ) // USHORT for 0 and -1 !
2000-09-18 16:07:07 +00:00
{
if ( (rSel.GetEnd().GetPara()+1) < GetParagraphCount() )
{
rSel.GetEnd().GetPara()++;
rSel.GetEnd().GetIndex() = 0;
}
else
{
rSel.GetEnd().GetIndex() = nStartPos;
bFound = FALSE;
}
}
break;
}
if ( !bForward && !nNode ) // Bei rueckwaertsuche, wenn nEndNode = 0:
break;
}
return bFound;
}
// -------------------------------------------------------------------------
// class ExtTextView
// -------------------------------------------------------------------------
ExtTextView::ExtTextView( ExtTextEngine* pEng, Window* pWindow )
: TextView( pEng, pWindow )
{
}
ExtTextView::~ExtTextView()
{
}
BOOL ExtTextView::MatchGroup()
{
TextSelection aTmpSel( GetSelection() );
aTmpSel.Justify();
if ( ( aTmpSel.GetStart().GetPara() != aTmpSel.GetEnd().GetPara() ) ||
( ( aTmpSel.GetEnd().GetIndex() - aTmpSel.GetStart().GetIndex() ) > 1 ) )
{
return FALSE;
}
TextSelection aMatchSel = ((ExtTextEngine*)GetTextEngine())->MatchGroup( aTmpSel.GetStart() );
if ( aMatchSel.HasRange() )
SetSelection( aMatchSel );
return aMatchSel.HasRange() ? TRUE : FALSE;
}
2001-03-09 09:21:41 +00:00
BOOL ExtTextView::Search( const util::SearchOptions& rSearchOptions, BOOL bForward )
2000-09-18 16:07:07 +00:00
{
BOOL bFound = FALSE;
TextSelection aSel( GetSelection() );
2001-03-09 09:21:41 +00:00
if ( ((ExtTextEngine*)GetTextEngine())->Search( aSel, rSearchOptions, bForward ) )
2000-09-18 16:07:07 +00:00
{
bFound = TRUE;
// Erstmal den Anfang des Wortes als Selektion einstellen,
// damit das ganze Wort in den sichtbaren Bereich kommt.
SetSelection( aSel.GetStart() );
ShowCursor( TRUE, FALSE );
}
else
{
aSel = GetSelection().GetEnd();
}
SetSelection( aSel );
ShowCursor();
return bFound;
}
2001-03-09 09:21:41 +00:00
USHORT ExtTextView::Replace( const util::SearchOptions& rSearchOptions, BOOL bAll, BOOL bForward )
2000-09-18 16:07:07 +00:00
{
USHORT nFound = 0;
if ( !bAll )
{
if ( GetSelection().HasRange() )
{
2001-03-09 09:21:41 +00:00
InsertText( rSearchOptions.replaceString );
2000-09-18 16:07:07 +00:00
nFound = 1;
2001-03-09 09:21:41 +00:00
Search( rSearchOptions, bForward ); // gleich zum naechsten
2000-09-18 16:07:07 +00:00
}
else
{
2001-03-09 09:21:41 +00:00
if( Search( rSearchOptions, bForward ) )
2000-09-18 16:07:07 +00:00
nFound = 1;
}
}
else
{
// Der Writer ersetzt alle, vom Anfang bis Ende...
ExtTextEngine* pTextEngine = (ExtTextEngine*)GetTextEngine();
// HideSelection();
TextSelection aSel;
2002-12-03 11:02:30 +00:00
BOOL bSearchInSelection = (0 != (rSearchOptions.searchFlag & util::SearchFlags::REG_NOT_BEGINOFLINE) );
2001-03-09 09:21:41 +00:00
if ( bSearchInSelection )
2000-09-18 16:07:07 +00:00
{
aSel = GetSelection();
aSel.Justify();
}
TextSelection aSearchSel( aSel );
2001-03-09 09:21:41 +00:00
BOOL bFound = pTextEngine->Search( aSel, rSearchOptions, TRUE );
2000-09-18 16:07:07 +00:00
if ( bFound )
pTextEngine->UndoActionStart( XTEXTUNDO_REPLACEALL );
while ( bFound )
{
nFound++;
2001-03-09 09:21:41 +00:00
TextPaM aNewStart = pTextEngine->ImpInsertText( aSel, rSearchOptions.replaceString );
2000-09-18 16:07:07 +00:00
aSel = aSearchSel;
aSel.GetStart() = aNewStart;
2001-03-09 09:21:41 +00:00
bFound = pTextEngine->Search( aSel, rSearchOptions, TRUE );
2000-09-18 16:07:07 +00:00
}
if ( nFound )
{
SetSelection( aSel.GetStart() );
pTextEngine->FormatAndUpdate( this );
pTextEngine->UndoActionEnd( XTEXTUNDO_REPLACEALL );
}
}
return nFound;
}
BOOL ExtTextView::ImpIndentBlock( BOOL bRight )
{
BOOL bDone = FALSE;
TextSelection aSel = GetSelection();
aSel.Justify();
HideSelection();
GetTextEngine()->UndoActionStart( bRight ? XTEXTUNDO_INDENTBLOCK : XTEXTUNDO_UNINDENTBLOCK );
ULONG nStartPara = aSel.GetStart().GetPara();
ULONG nEndPara = aSel.GetEnd().GetPara();
if ( aSel.HasRange() && !aSel.GetEnd().GetIndex() )
{
nEndPara--; // den dann nicht einruecken...
}
for ( ULONG nPara = nStartPara; nPara <= nEndPara; nPara++ )
{
if ( bRight )
{
// Tabs hinzufuegen
GetTextEngine()->ImpInsertText( TextPaM( nPara, 0 ), '\t' );
bDone = TRUE;
}
else
{
// Tabs/Blanks entfernen
String aText = GetTextEngine()->GetText( nPara );
if ( aText.Len() && (
( aText.GetChar( 0 ) == '\t' ) ||
( aText.GetChar( 0 ) == ' ' ) ) )
{
GetTextEngine()->ImpDeleteText( TextSelection( TextPaM( nPara, 0 ), TextPaM( nPara, 1 ) ) );
bDone = TRUE;
}
}
}
GetTextEngine()->UndoActionEnd( bRight ? XTEXTUNDO_INDENTBLOCK : XTEXTUNDO_UNINDENTBLOCK );
BOOL bRange = aSel.HasRange();
if ( bRight )
{
aSel.GetStart().GetIndex()++;
if ( bRange && ( aSel.GetEnd().GetPara() == nEndPara ) )
aSel.GetEnd().GetIndex()++;
}
else
{
if ( aSel.GetStart().GetIndex() )
aSel.GetStart().GetIndex()--;
if ( bRange && aSel.GetEnd().GetIndex() )
aSel.GetEnd().GetIndex()--;
}
2002-08-23 11:34:04 +00:00
ImpSetSelection( aSel );
2000-09-18 16:07:07 +00:00
GetTextEngine()->FormatAndUpdate( this );
return bDone;
}
BOOL ExtTextView::IndentBlock()
{
return ImpIndentBlock( TRUE );
}
BOOL ExtTextView::UnindentBlock()
{
return ImpIndentBlock( FALSE );
}