1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
19 #include "vbacomment.hxx"
21 #include <ooo/vba/excel/XlCreator.hpp>
22 #include <com/sun/star/drawing/XDrawPageSupplier.hpp>
23 #include <com/sun/star/sheet/XSpreadsheet.hpp>
24 #include <com/sun/star/sheet/XSheetAnnotationAnchor.hpp>
25 #include <com/sun/star/sheet/XSheetAnnotationsSupplier.hpp>
26 #include <com/sun/star/sheet/XSheetAnnotationShapeSupplier.hpp>
27 #include <com/sun/star/sheet/XSheetCellRange.hpp>
28 #include <com/sun/star/sheet/XCellAddressable.hpp>
29 #include <com/sun/star/table/CellAddress.hpp>
30 #include <com/sun/star/table/XCell.hpp>
31 #include <com/sun/star/text/XText.hpp>
33 #include <vbahelper/vbashape.hxx>
34 #include "vbaglobals.hxx"
35 #include "vbacomments.hxx"
37 using namespace ::ooo::vba
;
38 using namespace ::com::sun::star
;
40 ScVbaComment::ScVbaComment(
41 const uno::Reference
< XHelperInterface
>& xParent
,
42 const uno::Reference
< uno::XComponentContext
>& xContext
,
43 const uno::Reference
< frame::XModel
>& xModel
,
44 const uno::Reference
< table::XCellRange
>& xRange
) throw( lang::IllegalArgumentException
, uno::RuntimeException
) :
45 ScVbaComment_BASE( xParent
, xContext
),
46 mxModel( xModel
, uno::UNO_SET_THROW
),
50 throw lang::IllegalArgumentException("range is not set ", uno::Reference
< uno::XInterface
>() , 1 );
51 uno::Reference
< text::XSimpleText
> xAnnoText( getAnnotation(), uno::UNO_QUERY
);
54 // private helper functions
56 uno::Reference
< sheet::XSheetAnnotation
> SAL_CALL
57 ScVbaComment::getAnnotation() throw (uno::RuntimeException
)
59 uno::Reference
< table::XCell
> xCell( mxRange
->getCellByPosition(0, 0), uno::UNO_QUERY_THROW
);
60 uno::Reference
< sheet::XSheetAnnotationAnchor
> xAnnoAnchor( xCell
, uno::UNO_QUERY_THROW
);
61 return uno::Reference
< sheet::XSheetAnnotation
> ( xAnnoAnchor
->getAnnotation(), uno::UNO_QUERY_THROW
);
64 uno::Reference
< sheet::XSheetAnnotations
> SAL_CALL
65 ScVbaComment::getAnnotations() throw (uno::RuntimeException
)
67 uno::Reference
< sheet::XSheetCellRange
> xSheetCellRange(mxRange
, ::uno::UNO_QUERY_THROW
);
68 uno::Reference
< sheet::XSpreadsheet
> xSheet
= xSheetCellRange
->getSpreadsheet();
69 uno::Reference
< sheet::XSheetAnnotationsSupplier
> xAnnosSupp( xSheet
, uno::UNO_QUERY_THROW
);
71 return uno::Reference
< sheet::XSheetAnnotations
> ( xAnnosSupp
->getAnnotations(), uno::UNO_QUERY_THROW
);
75 ScVbaComment::getAnnotationIndex() throw (uno::RuntimeException
)
77 uno::Reference
< sheet::XSheetAnnotations
> xAnnos
= getAnnotations();
78 table::CellAddress aAddress
= getAnnotation()->getPosition();
81 sal_Int32 aCount
= xAnnos
->getCount();
83 for ( ; aIndex
< aCount
; aIndex
++ )
85 uno::Reference
< sheet::XSheetAnnotation
> xAnno( xAnnos
->getByIndex( aIndex
), uno::UNO_QUERY_THROW
);
86 table::CellAddress xAddress
= xAnno
->getPosition();
88 if ( xAddress
.Column
== aAddress
.Column
&& xAddress
.Row
== aAddress
.Row
&& xAddress
.Sheet
== aAddress
.Sheet
)
90 SAL_INFO("sc.ui", "terminating search, index is " << aIndex
);
94 SAL_INFO("sc.ui", "returning index is " << aIndex
);
99 uno::Reference
< excel::XComment
> SAL_CALL
100 ScVbaComment::getCommentByIndex( sal_Int32 Index
) throw (uno::RuntimeException
)
102 uno::Reference
< container::XIndexAccess
> xIndexAccess( getAnnotations(), uno::UNO_QUERY_THROW
);
103 // parent is sheet ( parent of the range which is the parent of the comment )
104 uno::Reference
< XCollection
> xColl( new ScVbaComments( getParent()->getParent(), mxContext
, mxModel
, xIndexAccess
) );
106 return uno::Reference
< excel::XComment
> ( xColl
->Item( uno::makeAny( Index
), uno::Any() ), uno::UNO_QUERY_THROW
);
109 // public vba functions
112 ScVbaComment::getAuthor() throw (uno::RuntimeException
, std::exception
)
114 return getAnnotation()->getAuthor();
118 ScVbaComment::setAuthor( const OUString
& /*_author*/ ) throw (uno::RuntimeException
, std::exception
)
120 // #TODO #FIXME implementation needed
123 uno::Reference
< msforms::XShape
> SAL_CALL
124 ScVbaComment::getShape() throw (uno::RuntimeException
, std::exception
)
126 uno::Reference
< sheet::XSheetAnnotationShapeSupplier
> xAnnoShapeSupp( getAnnotation(), uno::UNO_QUERY_THROW
);
127 uno::Reference
< drawing::XShape
> xAnnoShape( xAnnoShapeSupp
->getAnnotationShape(), uno::UNO_SET_THROW
);
128 uno::Reference
< sheet::XSheetCellRange
> xCellRange( mxRange
, uno::UNO_QUERY_THROW
);
129 uno::Reference
< drawing::XDrawPageSupplier
> xDrawPageSupp( xCellRange
->getSpreadsheet(), uno::UNO_QUERY_THROW
);
130 uno::Reference
< drawing::XShapes
> xShapes( xDrawPageSupp
->getDrawPage(), uno::UNO_QUERY_THROW
);
131 return new ScVbaShape( this, mxContext
, xAnnoShape
, xShapes
, mxModel
, office::MsoShapeType::msoComment
);
135 ScVbaComment::getVisible() throw (uno::RuntimeException
, std::exception
)
137 return getAnnotation()->getIsVisible();
141 ScVbaComment::setVisible( sal_Bool _visible
) throw (uno::RuntimeException
, std::exception
)
143 getAnnotation()->setIsVisible( _visible
);
147 ScVbaComment::Delete() throw (uno::RuntimeException
, std::exception
)
149 getAnnotations()->removeByIndex( getAnnotationIndex() );
152 uno::Reference
< excel::XComment
> SAL_CALL
153 ScVbaComment::Next() throw (uno::RuntimeException
, std::exception
)
155 // index: uno = 0, vba = 1
156 return getCommentByIndex( getAnnotationIndex() + 2 );
159 uno::Reference
< excel::XComment
> SAL_CALL
160 ScVbaComment::Previous() throw (uno::RuntimeException
, std::exception
)
162 // index: uno = 0, vba = 1
163 return getCommentByIndex( getAnnotationIndex() );
167 ScVbaComment::Text( const uno::Any
& aText
, const uno::Any
& aStart
, const uno::Any
& Overwrite
) throw (uno::RuntimeException
, std::exception
)
172 uno::Reference
< text::XSimpleText
> xAnnoText( getAnnotation(), uno::UNO_QUERY_THROW
);
173 OUString sAnnoText
= xAnnoText
->getString();
175 if ( aStart
.hasValue() )
177 sal_Int16 nStart
= 0;
178 bool bOverwrite
= true;
179 Overwrite
>>= bOverwrite
;
181 if ( aStart
>>= nStart
)
183 uno::Reference
< text::XTextCursor
> xTextCursor( xAnnoText
->createTextCursor(), uno::UNO_QUERY_THROW
);
187 xTextCursor
->collapseToStart();
188 xTextCursor
->gotoStart( false );
189 xTextCursor
->goRight( nStart
- 1, false );
190 xTextCursor
->gotoEnd( sal_True
);
194 xTextCursor
->collapseToStart();
195 xTextCursor
->gotoStart( false );
196 xTextCursor
->goRight( nStart
- 1 , sal_True
);
199 uno::Reference
< text::XTextRange
> xRange( xTextCursor
, uno::UNO_QUERY_THROW
);
200 xAnnoText
->insertString( xRange
, sText
, bOverwrite
);
201 return xAnnoText
->getString();
203 throw uno::RuntimeException("ScVbaComment::Text - bad Start value " );
205 else if ( aText
.hasValue() )
207 uno::Reference
< sheet::XCellAddressable
> xCellAddr(mxRange
->getCellByPosition(0, 0), uno::UNO_QUERY_THROW
);
208 table::CellAddress aAddress
= xCellAddr
->getCellAddress();
209 getAnnotations()->insertNew( aAddress
, sText
);
216 ScVbaComment::getServiceImplName()
218 return OUString("ScVbaComment");
221 uno::Sequence
< OUString
>
222 ScVbaComment::getServiceNames()
224 static uno::Sequence
< OUString
> aServiceNames
;
225 if ( aServiceNames
.getLength() == 0 )
227 aServiceNames
.realloc( 1 );
228 aServiceNames
[ 0 ] = "ooo.vba.excel.ScVbaComment";
230 return aServiceNames
;
233 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */