Files
libreoffice/sw/source/ui/vba/vbarange.cxx

429 lines
18 KiB
C++
Raw Normal View History

/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
re-base on ALv2 code. Includes: Patches contributed by Oliver-Rainer Wittmann sw34bf06: #i117783# - Writer's implementation of XPagePrintable - apply print settings to new printing routines http://svn.apache.org/viewvc?view=revision&revision=1172115 sw34bf06: #o12311627# use <rtl_random> methods to create unique ids for list styles and list ids http://svn.apache.org/viewvc?view=revision&revision=1172112 sw34bf06 #i114725#,#i115828# - method <SwDoc::ClearDoc()> - clear list structures completely http://svn.apache.org/viewvc?view=revision&revision=1172122 i#118572 - remove ui string and help content regarding usage of Java Mail in Writer's Mail Merge as Java Mail is not used. http://svn.apache.org/viewvc?view=revision&revision=1197035 Patches contributed by Mathias Bauer cws mba34issues01: #i117718#: provide filter name in case storage of medium does not allow to detect one http://svn.apache.org/viewvc?view=revision&revision=1172350 cws mba34issues01: #i117721#: directly provide parameters retrieved from SfxMedium http://svn.apache.org/viewvc?view=revision&revision=1172353 gnumake4 work variously http://svn.apache.org/viewvc?view=revision&revision=1394707 http://svn.apache.org/viewvc?view=revision&revision=1394326 http://svn.apache.org/viewvc?view=revision&revision=1396797 http://svn.apache.org/viewvc?view=revision&revision=1397315 cws mba34issues01: #i117723#: convert assertion into trace http://svn.apache.org/viewvc?view=revision&revision=1172355 cws mba34issues01: #i117699#: keep layout alive until swdoc dies http://svn.apache.org/viewvc?view=revision&revision=1172362 cws mba34issues01: #i117943#: missing color attributes in RTF clipboard http://svn.apache.org/viewvc?view=revision&revision=1172363 Patch contributed by Henning Brinkmann imported patch i#103878 http://svn.apache.org/viewvc?view=revision&revision=1172109 Patches contributed by Michael Stahl sw34bf06: #i117955#: WW8 export: disable storing of section breaks in endnotes http://svn.apache.org/viewvc?view=revision&revision=1172119 Patch contributed by imacat Fixed the Asian language work count. http://svn.apache.org/viewvc?view=revision&revision=1241345 Patch contributed by Pedro Giffuni i#20878 - Add comment with BZ issue for reference. http://svn.apache.org/viewvc?view=revision&revision=1244517 Patch contributed by Andre Fischer Do not add targets for junit tests when junit is disabled. http://svn.apache.org/viewvc?view=revision&revision=1241508 add writerperfect dependency.
2011-03-31 10:05:04 +02:00
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
2009-09-18 15:35:47 +00:00
#include "vbarange.hxx"
#include <vbahelper/vbahelper.hxx>
#include <tools/diagnose_ex.h>
#include "vbarangehelper.hxx"
#include <ooo/vba/word/WdBreakType.hpp>
#include <com/sun/star/style/BreakType.hpp>
#include <com/sun/star/text/ControlCharacter.hpp>
#include <com/sun/star/style/XStyleFamiliesSupplier.hpp>
#include <com/sun/star/text/XTextRangeCompare.hpp>
#include <com/sun/star/text/XWordCursor.hpp>
#include <com/sun/star/text/XParagraphCursor.hpp>
#include <ooo/vba/word/WdUnits.hpp>
#include <ooo/vba/word/WdMovementType.hpp>
2009-09-18 15:35:47 +00:00
#include "vbaparagraphformat.hxx"
#include "vbastyle.hxx"
#include "vbafont.hxx"
#include "vbapalette.hxx"
#include "vbapagesetup.hxx"
#include "vbalistformat.hxx"
#include "vbarevisions.hxx"
#include "vbabookmarks.hxx"
#include "vbasections.hxx"
#include "vbafield.hxx"
2009-09-18 15:35:47 +00:00
using namespace ::ooo::vba;
using namespace ::com::sun::star;
SwVbaRange::SwVbaRange( const uno::Reference< ooo::vba::XHelperInterface >& rParent, const uno::Reference< uno::XComponentContext >& rContext, const uno::Reference< text::XTextDocument >& rTextDocument, const uno::Reference< text::XTextRange >& rStart, sal_Bool _bMaySpanEndOfDocument ) throw (uno::RuntimeException) : SwVbaRange_BASE( rParent, rContext ), mxTextDocument( rTextDocument ), mbMaySpanEndOfDocument( _bMaySpanEndOfDocument )
{
uno::Reference< text::XTextRange > xEnd;
initialize( rStart, xEnd );
}
SwVbaRange::SwVbaRange( const uno::Reference< ooo::vba::XHelperInterface >& rParent, const uno::Reference< uno::XComponentContext >& rContext, const uno::Reference< text::XTextDocument >& rTextDocument, const uno::Reference< text::XTextRange >& rStart, const uno::Reference< text::XTextRange >& rEnd, sal_Bool _bMaySpanEndOfDocument ) throw (uno::RuntimeException) : SwVbaRange_BASE( rParent, rContext ), mxTextDocument( rTextDocument ), mbMaySpanEndOfDocument( _bMaySpanEndOfDocument )
{
initialize( rStart, rEnd );
}
2009-09-23 14:06:30 +00:00
SwVbaRange::SwVbaRange( const uno::Reference< ooo::vba::XHelperInterface >& rParent, const uno::Reference< uno::XComponentContext >& rContext, const uno::Reference< text::XTextDocument >& rTextDocument, const uno::Reference< text::XTextRange >& rStart, const uno::Reference< text::XTextRange >& rEnd, const uno::Reference< text::XText >& rText, sal_Bool _bMaySpanEndOfDocument ) throw (uno::RuntimeException) : SwVbaRange_BASE( rParent, rContext ),mxTextDocument( rTextDocument ), mxText( rText ), mbMaySpanEndOfDocument( _bMaySpanEndOfDocument )
2009-09-18 15:35:47 +00:00
{
initialize( rStart, rEnd );
}
SwVbaRange::~SwVbaRange()
{
}
void SwVbaRange::initialize( const uno::Reference< text::XTextRange >& rStart, const uno::Reference< text::XTextRange >& rEnd ) throw (uno::RuntimeException)
{
if( !mxText.is() )
{
mxText = mxTextDocument->getText();
}
mxTextCursor = SwVbaRangeHelper::initCursor( rStart, mxText );
if( !mxTextCursor.is() )
throw uno::RuntimeException( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Fails to create text cursor") ), uno::Reference< uno::XInterface >() );
2009-09-18 15:35:47 +00:00
mxTextCursor->collapseToStart();
if( rEnd.is() )
mxTextCursor->gotoRange( rEnd, sal_True );
else
mxTextCursor->gotoEnd( sal_True );
}
uno::Reference< text::XTextRange > SAL_CALL
SwVbaRange::getXTextRange() throw (uno::RuntimeException)
{
uno::Reference< text::XTextRange > xTextRange( mxTextCursor, uno::UNO_QUERY_THROW );
return xTextRange;
}
2009-09-18 15:35:47 +00:00
/**
* The complexity in this method is because we need to workaround
* an issue that the last paragraph in a document does not have a trailing CRLF.
* @return
*/
rtl::OUString SAL_CALL
SwVbaRange::getText() throw ( uno::RuntimeException )
{
rtl::OUString aText = mxTextCursor->getString();
sal_Int32 nLen = aText.getLength();
// FIXME: should add a line separator if the range includes the last paragraph
if( nLen == 0 )
{
if( mxTextCursor->isCollapsed() )
{
mxTextCursor->goRight( 1, sal_True );
aText = mxTextCursor->getString();
mxTextCursor->collapseToStart();
}
else
{
uno::Reference< text::XTextRange > xStart = mxTextCursor->getStart();
uno::Reference< text::XTextRange > xEnd = mxTextCursor->getEnd();
mxTextCursor->collapseToEnd();
mxTextCursor->goRight( 1, sal_True );
mxTextCursor->gotoRange( xStart, sal_False );
mxTextCursor->gotoRange( xEnd, sal_True );
}
}
return aText;
}
void SAL_CALL
SwVbaRange::setText( const rtl::OUString& rText ) throw ( uno::RuntimeException )
{
// Emulate the MSWord behavior, Don't delete the bookmark
// which contains no text string in current inserting position,
rtl::OUString sName;
uno::Reference< text::XTextRange > xRange( mxTextCursor, uno::UNO_QUERY_THROW );
try
{
uno::Reference< text::XTextContent > xBookmark = SwVbaRangeHelper::findBookmarkByPosition( mxTextDocument, xRange->getStart() );
if( xBookmark.is() )
{
uno::Reference< container::XNamed > xNamed( xBookmark, uno::UNO_QUERY_THROW );
sName = xNamed->getName();
}
}
2011-11-22 09:06:06 +00:00
catch (const uno::Exception&)
{
// do nothing
}
2009-09-18 15:35:47 +00:00
if( rText.indexOf( '\n' ) != -1 )
{
mxTextCursor->setString( rtl::OUString() );
// process CR in strings
SwVbaRangeHelper::insertString( xRange, mxText, rText, sal_True );
}
else
{
mxTextCursor->setString( rText );
}
// insert the bookmark if the bookmark is deleted during setting text string
if( !sName.isEmpty() )
{
uno::Reference< text::XBookmarksSupplier > xBookmarksSupplier( mxTextDocument, uno::UNO_QUERY_THROW );
uno::Reference< container::XNameAccess > xNameAccess( xBookmarksSupplier->getBookmarks(), uno::UNO_QUERY_THROW );
if( !xNameAccess->hasByName( sName ) )
{
uno::Reference< frame::XModel > xModel( mxTextDocument, uno::UNO_QUERY_THROW );
SwVbaBookmarks::addBookmarkByName( xModel, sName, xRange->getStart() );
}
}
2009-09-18 15:35:47 +00:00
}
// FIXME: test is not pass
void SAL_CALL SwVbaRange::InsertBreak( const uno::Any& _breakType ) throw (uno::RuntimeException)
{
// default type is wdPageBreak;
sal_Int32 nBreakType = word::WdBreakType::wdPageBreak;
if( _breakType.hasValue() )
_breakType >>= nBreakType;
style::BreakType eBreakType = style::BreakType_NONE;
switch( nBreakType )
{
case word::WdBreakType::wdPageBreak:
eBreakType = style::BreakType_PAGE_BEFORE;
break;
case word::WdBreakType::wdColumnBreak:
eBreakType = style::BreakType_COLUMN_AFTER;
break;
case word::WdBreakType::wdLineBreak:
case word::WdBreakType::wdLineBreakClearLeft:
case word::WdBreakType::wdLineBreakClearRight:
case word::WdBreakType::wdSectionBreakContinuous:
case word::WdBreakType::wdSectionBreakEvenPage:
case word::WdBreakType::wdSectionBreakNextPage:
case word::WdBreakType::wdSectionBreakOddPage:
case word::WdBreakType::wdTextWrappingBreak:
DebugHelper::exception( SbERR_NOT_IMPLEMENTED, rtl::OUString() );
break;
default:
DebugHelper::exception( SbERR_BAD_PARAMETER, rtl::OUString() );
}
if( eBreakType != style::BreakType_NONE )
{
if( !mxTextCursor->isCollapsed() )
{
mxTextCursor->setString( rtl::OUString() );
mxTextCursor->collapseToStart();
}
uno::Reference< beans::XPropertySet > xProp( mxTextCursor, uno::UNO_QUERY_THROW );
xProp->setPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("BreakType") ), uno::makeAny( eBreakType ) );
}
}
void SAL_CALL
SwVbaRange::Select() throw ( uno::RuntimeException )
{
uno::Reference< frame::XModel > xModel( mxTextDocument, uno::UNO_QUERY_THROW );
uno::Reference< text::XTextViewCursor > xTextViewCursor = word::getXTextViewCursor( xModel );
xTextViewCursor->gotoRange( mxTextCursor->getStart(), sal_False );
xTextViewCursor->gotoRange( mxTextCursor->getEnd(), sal_True );
}
void SAL_CALL
SwVbaRange::InsertParagraph() throw ( uno::RuntimeException )
{
mxTextCursor->setString( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("") ) );
InsertParagraphBefore();
}
void SAL_CALL
SwVbaRange::InsertParagraphBefore() throw ( uno::RuntimeException )
{
uno::Reference< text::XTextRange > xTextRange = mxTextCursor->getStart();
mxText->insertControlCharacter( xTextRange, text::ControlCharacter::PARAGRAPH_BREAK, sal_True );
mxTextCursor->gotoRange( xTextRange, sal_True );
}
void SAL_CALL
SwVbaRange::InsertParagraphAfter() throw ( uno::RuntimeException )
{
uno::Reference< text::XTextRange > xTextRange = mxTextCursor->getEnd();
mxText->insertControlCharacter( xTextRange, text::ControlCharacter::PARAGRAPH_BREAK, sal_True );
}
uno::Reference< word::XParagraphFormat > SAL_CALL
SwVbaRange::getParagraphFormat() throw ( uno::RuntimeException )
{
uno::Reference< beans::XPropertySet > xParaProps( mxTextCursor, uno::UNO_QUERY_THROW );
return uno::Reference< word::XParagraphFormat >( new SwVbaParagraphFormat( this, mxContext, mxTextDocument, xParaProps ) );
}
void SAL_CALL
SwVbaRange::setParagraphFormat( const uno::Reference< word::XParagraphFormat >& /*rParagraphFormat*/ ) throw ( uno::RuntimeException )
{
throw uno::RuntimeException( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Not implemented") ), uno::Reference< uno::XInterface >() );
}
void SwVbaRange::GetStyleInfo(rtl::OUString& aStyleName, rtl::OUString& aStyleType ) throw ( uno::RuntimeException )
2009-09-18 15:35:47 +00:00
{
uno::Reference< beans::XPropertySet > xProp( mxTextCursor, uno::UNO_QUERY_THROW );
if( ( xProp->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("CharStyleName") ) ) >>= aStyleName ) && !aStyleName.isEmpty() )
2009-09-18 15:35:47 +00:00
{
aStyleType = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("CharacterStyles") );
}
else if( ( xProp->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ParaStyleName") ) ) >>= aStyleName ) && !aStyleName.isEmpty() )
2009-09-18 15:35:47 +00:00
{
aStyleType = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ParagraphStyles") );
}
if( aStyleType.isEmpty() )
2009-09-18 15:35:47 +00:00
{
DebugHelper::exception( SbERR_INTERNAL_ERROR, rtl::OUString() );
}
}
uno::Any SAL_CALL
SwVbaRange::getStyle() throw ( uno::RuntimeException )
{
rtl::OUString aStyleName;
rtl::OUString aStyleType;
GetStyleInfo( aStyleName, aStyleType );
2009-09-18 15:35:47 +00:00
uno::Reference< style::XStyleFamiliesSupplier > xStyleSupplier( mxTextDocument, uno::UNO_QUERY_THROW);
uno::Reference< container::XNameAccess > xStylesAccess( xStyleSupplier->getStyleFamilies()->getByName( aStyleType ), uno::UNO_QUERY_THROW );
uno::Reference< beans::XPropertySet > xStyleProps( xStylesAccess->getByName( aStyleName ), uno::UNO_QUERY_THROW );
uno::Reference< frame::XModel > xModel( mxTextDocument, uno::UNO_QUERY_THROW );
return uno::makeAny( uno::Reference< word::XStyle >( new SwVbaStyle( this, mxContext, xModel, xStyleProps ) ) );
2009-09-18 15:35:47 +00:00
}
void SAL_CALL
SwVbaRange::setStyle( const uno::Any& rStyle ) throw ( uno::RuntimeException )
2009-09-18 15:35:47 +00:00
{
uno::Reference< beans::XPropertySet > xParaProps( mxTextCursor, uno::UNO_QUERY_THROW );
SwVbaStyle::setStyle( xParaProps, rStyle );
}
uno::Reference< word::XFont > SAL_CALL
SwVbaRange::getFont() throw ( uno::RuntimeException )
{
VbaPalette aColors;
return new SwVbaFont( mxParent, mxContext, aColors.getPalette(), uno::Reference< beans::XPropertySet >( getXTextRange(), uno::UNO_QUERY_THROW ) );
}
uno::Reference< word::XListFormat > SAL_CALL
SwVbaRange::getListFormat() throw ( uno::RuntimeException )
{
return uno::Reference< word::XListFormat >( new SwVbaListFormat( this, mxContext, getXTextRange() ) );
}
2009-09-18 15:35:47 +00:00
::sal_Int32 SAL_CALL SwVbaRange::getLanguageID() throw (uno::RuntimeException)
{
uno::Reference< beans::XPropertySet > xParaProps( mxTextCursor, uno::UNO_QUERY_THROW );
return SwVbaStyle::getLanguageID( xParaProps );
}
void SAL_CALL SwVbaRange::setLanguageID( ::sal_Int32 _languageid ) throw (uno::RuntimeException)
{
uno::Reference< beans::XPropertySet > xParaProps( mxTextCursor, uno::UNO_QUERY_THROW );
SwVbaStyle::setLanguageID( xParaProps, _languageid );
}
uno::Any SAL_CALL
SwVbaRange::PageSetup( ) throw (uno::RuntimeException)
{
uno::Reference< beans::XPropertySet > xParaProps( mxTextCursor, uno::UNO_QUERY_THROW );
uno::Reference< frame::XModel > xModel( mxTextDocument, uno::UNO_QUERY_THROW );
rtl::OUString aPageStyleName;
xParaProps->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("PageStyleName"))) >>= aPageStyleName;
uno::Reference< style::XStyleFamiliesSupplier > xSytleFamSupp( xModel, uno::UNO_QUERY_THROW );
uno::Reference< container::XNameAccess > xSytleFamNames( xSytleFamSupp->getStyleFamilies(), uno::UNO_QUERY_THROW );
uno::Reference< container::XNameAccess > xPageStyles( xSytleFamNames->getByName( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("PageStyles") ) ), uno::UNO_QUERY_THROW );
uno::Reference< beans::XPropertySet > xPageProps( xPageStyles->getByName( aPageStyleName ), uno::UNO_QUERY_THROW );
return uno::makeAny( uno::Reference< word::XPageSetup >( new SwVbaPageSetup( this, mxContext, xModel, xPageProps ) ) );
}
::sal_Int32 SAL_CALL SwVbaRange::getStart() throw (uno::RuntimeException)
{
uno::Reference< text::XText > xText = mxTextDocument->getText();
return SwVbaRangeHelper::getPosition( xText, mxTextCursor->getStart() );
}
void SAL_CALL SwVbaRange::setStart( ::sal_Int32 _start ) throw (uno::RuntimeException)
{
uno::Reference< text::XText > xText = mxTextDocument->getText();
uno::Reference< text::XTextRange > xStart = SwVbaRangeHelper::getRangeByPosition( xText, _start );
uno::Reference< text::XTextRange > xEnd = mxTextCursor->getEnd();
mxTextCursor->gotoRange( xStart, sal_False );
mxTextCursor->gotoRange( xEnd, sal_True );
}
::sal_Int32 SAL_CALL SwVbaRange::getEnd() throw (uno::RuntimeException)
{
uno::Reference< text::XText > xText = mxTextDocument->getText();
return SwVbaRangeHelper::getPosition( xText, mxTextCursor->getEnd() );
}
void SAL_CALL SwVbaRange::setEnd( ::sal_Int32 _end ) throw (uno::RuntimeException)
{
uno::Reference< text::XText > xText = mxTextDocument->getText();
uno::Reference< text::XTextRange > xEnd = SwVbaRangeHelper::getRangeByPosition( xText, _end );
mxTextCursor->collapseToStart();
mxTextCursor->gotoRange( xEnd, sal_True );
}
::sal_Bool SAL_CALL SwVbaRange::InRange( const uno::Reference< ::ooo::vba::word::XRange >& Range ) throw (uno::RuntimeException)
{
SwVbaRange* pRange = dynamic_cast< SwVbaRange* >( Range.get() );
if( !pRange )
throw uno::RuntimeException();
uno::Reference< text::XTextRange > xTextRange = pRange->getXTextRange();
uno::Reference< text::XTextRangeCompare > xTRC( mxTextCursor->getText(), uno::UNO_QUERY_THROW );
if( xTRC->compareRegionStarts( xTextRange, getXTextRange() ) >= 0 && xTRC->compareRegionEnds( xTextRange, getXTextRange() ) <= 0 )
return sal_True;
return sal_False;
}
uno::Any SAL_CALL
SwVbaRange::Revisions( const uno::Any& index ) throw (uno::RuntimeException)
{
uno::Reference< text::XTextRange > xTextRange = getXTextRange();
uno::Reference< frame::XModel > xModel( mxTextDocument, uno::UNO_QUERY_THROW );
uno::Reference< XCollection > xCol( new SwVbaRevisions( mxParent, mxContext, xModel, xTextRange ) );
if ( index.hasValue() )
return xCol->Item( index, uno::Any() );
return uno::makeAny( xCol );
}
uno::Any SAL_CALL
SwVbaRange::Sections( const uno::Any& index ) throw (uno::RuntimeException)
{
uno::Reference< text::XTextRange > xTextRange = getXTextRange();
uno::Reference< frame::XModel > xModel( mxTextDocument, uno::UNO_QUERY_THROW );
uno::Reference< XCollection > xCol( new SwVbaSections( mxParent, mxContext, xModel, xTextRange ) );
if ( index.hasValue() )
return xCol->Item( index, uno::Any() );
return uno::makeAny( xCol );
}
uno::Any SAL_CALL
SwVbaRange::Fields( const uno::Any& index ) throw (uno::RuntimeException)
{
//FIXME: should be get the field in current range
uno::Reference< frame::XModel > xModel( mxTextDocument, uno::UNO_QUERY_THROW );
uno::Reference< XCollection > xCol( new SwVbaFields( mxParent, mxContext, xModel ) );
if ( index.hasValue() )
return xCol->Item( index, uno::Any() );
return uno::makeAny( xCol );
}
2012-03-24 14:00:06 +00:00
rtl::OUString
2009-09-18 15:35:47 +00:00
SwVbaRange::getServiceImplName()
{
2012-03-24 14:00:06 +00:00
return rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("SwVbaRange"));
2009-09-18 15:35:47 +00:00
}
uno::Sequence< rtl::OUString >
SwVbaRange::getServiceNames()
{
static uno::Sequence< rtl::OUString > aServiceNames;
if ( aServiceNames.getLength() == 0 )
{
aServiceNames.realloc( 1 );
aServiceNames[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.word.Range" ) );
}
return aServiceNames;
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */