1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: vbacomment.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
30 #include "vbacomment.hxx"
32 #include <ooo/vba/excel/XlCreator.hpp>
33 #include <com/sun/star/sheet/XSpreadsheet.hpp>
34 #include <com/sun/star/sheet/XSheetAnnotationAnchor.hpp>
35 #include <com/sun/star/sheet/XSheetAnnotationsSupplier.hpp>
36 #include <com/sun/star/sheet/XSheetCellRange.hpp>
37 #include <com/sun/star/sheet/XCellAddressable.hpp>
38 #include <com/sun/star/table/CellAddress.hpp>
39 #include <com/sun/star/table/XCell.hpp>
40 #include <com/sun/star/text/XText.hpp>
42 #include "vbaglobals.hxx"
43 #include "vbacomments.hxx"
46 using namespace ::ooo::vba
;
47 using namespace ::com::sun::star
;
49 ScVbaComment::ScVbaComment( const uno::Reference
< XHelperInterface
>& xParent
, const uno::Reference
< uno::XComponentContext
>& xContext
, const uno::Reference
< table::XCellRange
>& xRange
) throw( lang::IllegalArgumentException
)
50 : ScVbaComment_BASE( xParent
, xContext
), mxRange( xRange
)
53 throw lang::IllegalArgumentException( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "range is not set " ) ), uno::Reference
< uno::XInterface
>() , 1 );
54 uno::Reference
< text::XSimpleText
> xAnnoText( getAnnotation(), uno::UNO_QUERY
);
57 // private helper functions
59 uno::Reference
< sheet::XSheetAnnotation
> SAL_CALL
60 ScVbaComment::getAnnotation() throw (uno::RuntimeException
)
62 uno::Reference
< table::XCell
> xCell( mxRange
->getCellByPosition(0, 0), uno::UNO_QUERY_THROW
);
63 uno::Reference
< sheet::XSheetAnnotationAnchor
> xAnnoAnchor( xCell
, uno::UNO_QUERY_THROW
);
64 return uno::Reference
< sheet::XSheetAnnotation
> ( xAnnoAnchor
->getAnnotation(), uno::UNO_QUERY_THROW
);
67 uno::Reference
< sheet::XSheetAnnotations
> SAL_CALL
68 ScVbaComment::getAnnotations() throw (uno::RuntimeException
)
70 uno::Reference
< sheet::XSheetCellRange
> xSheetCellRange(mxRange
, ::uno::UNO_QUERY_THROW
);
71 uno::Reference
< sheet::XSpreadsheet
> xSheet
= xSheetCellRange
->getSpreadsheet();
72 uno::Reference
< sheet::XSheetAnnotationsSupplier
> xAnnosSupp( xSheet
, uno::UNO_QUERY_THROW
);
74 return uno::Reference
< sheet::XSheetAnnotations
> ( xAnnosSupp
->getAnnotations(), uno::UNO_QUERY_THROW
);
78 ScVbaComment::getAnnotationIndex() throw (uno::RuntimeException
)
80 uno::Reference
< sheet::XSheetAnnotations
> xAnnos
= getAnnotations();
81 table::CellAddress aAddress
= getAnnotation()->getPosition();
84 sal_Int32 aCount
= xAnnos
->getCount();
86 for ( ; aIndex
< aCount
; aIndex
++ )
88 uno::Reference
< sheet::XSheetAnnotation
> xAnno( xAnnos
->getByIndex( aIndex
), uno::UNO_QUERY_THROW
);
89 table::CellAddress xAddress
= xAnno
->getPosition();
91 if ( xAddress
.Column
== aAddress
.Column
&& xAddress
.Row
== aAddress
.Row
&& xAddress
.Sheet
== aAddress
.Sheet
)
93 OSL_TRACE("** terminating search, index is %d", aIndex
);
97 OSL_TRACE("** returning index is %d", aIndex
);
102 uno::Reference
< excel::XComment
> SAL_CALL
103 ScVbaComment::getCommentByIndex( sal_Int32 Index
) throw (uno::RuntimeException
)
105 uno::Reference
< container::XIndexAccess
> xIndexAccess( getAnnotations(), uno::UNO_QUERY_THROW
);
106 // parent is sheet ( parent of the range which is the parent of the comment )
107 uno::Reference
< XCollection
> xColl( new ScVbaComments( getParent()->getParent(), mxContext
, xIndexAccess
) );
109 return uno::Reference
< excel::XComment
> ( xColl
->Item( uno::makeAny( Index
), uno::Any() ), uno::UNO_QUERY_THROW
);
112 // public vba functions
114 rtl::OUString SAL_CALL
115 ScVbaComment::getAuthor() throw (uno::RuntimeException
)
117 return getAnnotation()->getAuthor();
121 ScVbaComment::setAuthor( const rtl::OUString
& /*_author*/ ) throw (uno::RuntimeException
)
123 // #TODO #FIXME implementation needed
127 ScVbaComment::getVisible() throw (uno::RuntimeException
)
129 return getAnnotation()->getIsVisible();
133 ScVbaComment::setVisible( sal_Bool _visible
) throw (uno::RuntimeException
)
135 getAnnotation()->setIsVisible( _visible
);
139 ScVbaComment::Delete() throw (uno::RuntimeException
)
141 getAnnotations()->removeByIndex( getAnnotationIndex() );
144 uno::Reference
< excel::XComment
> SAL_CALL
145 ScVbaComment::Next() throw (uno::RuntimeException
)
147 // index: uno = 0, vba = 1
148 return getCommentByIndex( getAnnotationIndex() + 2 );
151 uno::Reference
< excel::XComment
> SAL_CALL
152 ScVbaComment::Previous() throw (uno::RuntimeException
)
154 // index: uno = 0, vba = 1
155 return getCommentByIndex( getAnnotationIndex() );
158 rtl::OUString SAL_CALL
159 ScVbaComment::Text( const uno::Any
& aText
, const uno::Any
& aStart
, const uno::Any
& Overwrite
) throw (uno::RuntimeException
)
164 uno::Reference
< text::XSimpleText
> xAnnoText( getAnnotation(), uno::UNO_QUERY_THROW
);
166 if ( aStart
.hasValue() )
168 sal_Int16 nStart
= 0;
169 sal_Bool bOverwrite
= sal_True
;
170 Overwrite
>>= bOverwrite
;
172 if ( aStart
>>= nStart
)
174 uno::Reference
< text::XTextCursor
> xTextCursor( xAnnoText
->createTextCursor(), uno::UNO_QUERY_THROW
);
178 xTextCursor
->collapseToStart();
179 xTextCursor
->gotoStart( sal_False
);
180 xTextCursor
->goRight( nStart
- 1, sal_False
);
181 xTextCursor
->gotoEnd( sal_True
);
185 xTextCursor
->collapseToStart();
186 xTextCursor
->gotoStart( sal_False
);
187 xTextCursor
->goRight( nStart
- 1 , sal_True
);
190 uno::Reference
< text::XTextRange
> xRange( xTextCursor
, uno::UNO_QUERY_THROW
);
191 xAnnoText
->insertString( xRange
, sText
, bOverwrite
);
194 throw uno::RuntimeException( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "ScVbaComment::Text - bad Start value " ) ), uno::Reference
< uno::XInterface
>() );
196 else if ( aText
.hasValue() )
198 uno::Reference
< sheet::XCellAddressable
> xCellAddr(mxRange
->getCellByPosition(0, 0), uno::UNO_QUERY_THROW
);
199 table::CellAddress aAddress
= xCellAddr
->getCellAddress();
200 getAnnotations()->insertNew( aAddress
, sText
);
203 rtl::OUString sAnnoText
= xAnnoText
->getString();
208 ScVbaComment::getServiceImplName()
210 static rtl::OUString
sImplName( RTL_CONSTASCII_USTRINGPARAM("ScVbaComment") );
214 uno::Sequence
< rtl::OUString
>
215 ScVbaComment::getServiceNames()
217 static uno::Sequence
< rtl::OUString
> aServiceNames
;
218 if ( aServiceNames
.getLength() == 0 )
220 aServiceNames
.realloc( 1 );
221 aServiceNames
[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.excel.ScVbaComment" ) );
223 return aServiceNames
;