1*b3f79822SAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3*b3f79822SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4*b3f79822SAndrew Rist * or more contributor license agreements. See the NOTICE file
5*b3f79822SAndrew Rist * distributed with this work for additional information
6*b3f79822SAndrew Rist * regarding copyright ownership. The ASF licenses this file
7*b3f79822SAndrew Rist * to you under the Apache License, Version 2.0 (the
8*b3f79822SAndrew Rist * "License"); you may not use this file except in compliance
9*b3f79822SAndrew Rist * with the License. You may obtain a copy of the License at
10*b3f79822SAndrew Rist *
11*b3f79822SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12*b3f79822SAndrew Rist *
13*b3f79822SAndrew Rist * Unless required by applicable law or agreed to in writing,
14*b3f79822SAndrew Rist * software distributed under the License is distributed on an
15*b3f79822SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*b3f79822SAndrew Rist * KIND, either express or implied. See the License for the
17*b3f79822SAndrew Rist * specific language governing permissions and limitations
18*b3f79822SAndrew Rist * under the License.
19*b3f79822SAndrew Rist *
20*b3f79822SAndrew Rist *************************************************************/
21*b3f79822SAndrew Rist
22*b3f79822SAndrew Rist
23cdf0e10cSrcweir #include "vbacomment.hxx"
24cdf0e10cSrcweir
25cdf0e10cSrcweir #include <ooo/vba/excel/XlCreator.hpp>
26cdf0e10cSrcweir #include <com/sun/star/drawing/XDrawPageSupplier.hpp>
27cdf0e10cSrcweir #include <com/sun/star/sheet/XSpreadsheet.hpp>
28cdf0e10cSrcweir #include <com/sun/star/sheet/XSheetAnnotationAnchor.hpp>
29cdf0e10cSrcweir #include <com/sun/star/sheet/XSheetAnnotationsSupplier.hpp>
30cdf0e10cSrcweir #include <com/sun/star/sheet/XSheetAnnotationShapeSupplier.hpp>
31cdf0e10cSrcweir #include <com/sun/star/sheet/XSheetCellRange.hpp>
32cdf0e10cSrcweir #include <com/sun/star/table/CellAddress.hpp>
33cdf0e10cSrcweir #include <com/sun/star/table/XCell.hpp>
34cdf0e10cSrcweir #include <com/sun/star/text/XText.hpp>
35cdf0e10cSrcweir
36cdf0e10cSrcweir #include <vbahelper/vbashape.hxx>
37cdf0e10cSrcweir #include "vbaglobals.hxx"
38cdf0e10cSrcweir #include "vbacomments.hxx"
39cdf0e10cSrcweir
40cdf0e10cSrcweir
41cdf0e10cSrcweir using namespace ::ooo::vba;
42cdf0e10cSrcweir using namespace ::com::sun::star;
43cdf0e10cSrcweir
ScVbaComment(const uno::Reference<XHelperInterface> & xParent,const uno::Reference<uno::XComponentContext> & xContext,const uno::Reference<frame::XModel> & xModel,const uno::Reference<table::XCellRange> & xRange)44cdf0e10cSrcweir ScVbaComment::ScVbaComment(
45cdf0e10cSrcweir const uno::Reference< XHelperInterface >& xParent,
46cdf0e10cSrcweir const uno::Reference< uno::XComponentContext >& xContext,
47cdf0e10cSrcweir const uno::Reference< frame::XModel >& xModel,
48cdf0e10cSrcweir const uno::Reference< table::XCellRange >& xRange ) throw( lang::IllegalArgumentException ) :
49cdf0e10cSrcweir ScVbaComment_BASE( xParent, xContext ),
50cdf0e10cSrcweir mxModel( xModel, uno::UNO_SET_THROW ),
51cdf0e10cSrcweir mxRange( xRange )
52cdf0e10cSrcweir {
53cdf0e10cSrcweir if ( !xRange.is() )
54cdf0e10cSrcweir throw lang::IllegalArgumentException( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "range is not set " ) ), uno::Reference< uno::XInterface >() , 1 );
55cdf0e10cSrcweir uno::Reference< text::XSimpleText > xAnnoText( getAnnotation(), uno::UNO_QUERY );
56cdf0e10cSrcweir }
57cdf0e10cSrcweir
58cdf0e10cSrcweir // private helper functions
59cdf0e10cSrcweir
60cdf0e10cSrcweir uno::Reference< sheet::XSheetAnnotation > SAL_CALL
getAnnotation()61cdf0e10cSrcweir ScVbaComment::getAnnotation() throw (uno::RuntimeException)
62cdf0e10cSrcweir {
63cdf0e10cSrcweir uno::Reference< table::XCell > xCell( mxRange->getCellByPosition(0, 0), uno::UNO_QUERY_THROW );
64cdf0e10cSrcweir uno::Reference< sheet::XSheetAnnotationAnchor > xAnnoAnchor( xCell, uno::UNO_QUERY_THROW );
65cdf0e10cSrcweir return uno::Reference< sheet::XSheetAnnotation > ( xAnnoAnchor->getAnnotation(), uno::UNO_QUERY_THROW );
66cdf0e10cSrcweir }
67cdf0e10cSrcweir
68cdf0e10cSrcweir uno::Reference< sheet::XSheetAnnotations > SAL_CALL
getAnnotations()69cdf0e10cSrcweir ScVbaComment::getAnnotations() throw (uno::RuntimeException)
70cdf0e10cSrcweir {
71cdf0e10cSrcweir uno::Reference< sheet::XSheetCellRange > xSheetCellRange(mxRange, ::uno::UNO_QUERY_THROW );
72cdf0e10cSrcweir uno::Reference< sheet::XSpreadsheet > xSheet = xSheetCellRange->getSpreadsheet();
73cdf0e10cSrcweir uno::Reference< sheet::XSheetAnnotationsSupplier > xAnnosSupp( xSheet, uno::UNO_QUERY_THROW );
74cdf0e10cSrcweir
75cdf0e10cSrcweir return uno::Reference< sheet::XSheetAnnotations > ( xAnnosSupp->getAnnotations(), uno::UNO_QUERY_THROW );
76cdf0e10cSrcweir }
77cdf0e10cSrcweir
78cdf0e10cSrcweir sal_Int32 SAL_CALL
getAnnotationIndex()79cdf0e10cSrcweir ScVbaComment::getAnnotationIndex() throw (uno::RuntimeException)
80cdf0e10cSrcweir {
81cdf0e10cSrcweir uno::Reference< sheet::XSheetAnnotations > xAnnos = getAnnotations();
82cdf0e10cSrcweir table::CellAddress aAddress = getAnnotation()->getPosition();
83cdf0e10cSrcweir
84cdf0e10cSrcweir sal_Int32 aIndex = 0;
85cdf0e10cSrcweir sal_Int32 aCount = xAnnos->getCount();
86cdf0e10cSrcweir
87cdf0e10cSrcweir for ( ; aIndex < aCount ; aIndex++ )
88cdf0e10cSrcweir {
89cdf0e10cSrcweir uno::Reference< sheet::XSheetAnnotation > xAnno( xAnnos->getByIndex( aIndex ), uno::UNO_QUERY_THROW );
90cdf0e10cSrcweir table::CellAddress xAddress = xAnno->getPosition();
91cdf0e10cSrcweir
92cdf0e10cSrcweir if ( xAddress.Column == aAddress.Column && xAddress.Row == aAddress.Row && xAddress.Sheet == aAddress.Sheet )
93cdf0e10cSrcweir {
94cdf0e10cSrcweir OSL_TRACE("** terminating search, index is %d", aIndex );
95cdf0e10cSrcweir break;
96cdf0e10cSrcweir }
97cdf0e10cSrcweir }
98cdf0e10cSrcweir OSL_TRACE("** returning index is %d", aIndex );
99cdf0e10cSrcweir
100cdf0e10cSrcweir return aIndex;
101cdf0e10cSrcweir }
102cdf0e10cSrcweir
103cdf0e10cSrcweir uno::Reference< excel::XComment > SAL_CALL
getCommentByIndex(sal_Int32 Index)104cdf0e10cSrcweir ScVbaComment::getCommentByIndex( sal_Int32 Index ) throw (uno::RuntimeException)
105cdf0e10cSrcweir {
106cdf0e10cSrcweir uno::Reference< container::XIndexAccess > xIndexAccess( getAnnotations(), uno::UNO_QUERY_THROW );
107cdf0e10cSrcweir // parent is sheet ( parent of the range which is the parent of the comment )
108cdf0e10cSrcweir uno::Reference< XCollection > xColl( new ScVbaComments( getParent()->getParent(), mxContext, mxModel, xIndexAccess ) );
109cdf0e10cSrcweir
110cdf0e10cSrcweir return uno::Reference< excel::XComment > ( xColl->Item( uno::makeAny( Index ), uno::Any() ), uno::UNO_QUERY_THROW );
111cdf0e10cSrcweir }
112cdf0e10cSrcweir
113cdf0e10cSrcweir // public vba functions
114cdf0e10cSrcweir
115cdf0e10cSrcweir rtl::OUString SAL_CALL
getAuthor()116cdf0e10cSrcweir ScVbaComment::getAuthor() throw (uno::RuntimeException)
117cdf0e10cSrcweir {
118cdf0e10cSrcweir return getAnnotation()->getAuthor();
119cdf0e10cSrcweir }
120cdf0e10cSrcweir
121cdf0e10cSrcweir void SAL_CALL
setAuthor(const rtl::OUString &)122cdf0e10cSrcweir ScVbaComment::setAuthor( const rtl::OUString& /*_author*/ ) throw (uno::RuntimeException)
123cdf0e10cSrcweir {
124cdf0e10cSrcweir // #TODO #FIXME implementation needed
125cdf0e10cSrcweir }
126cdf0e10cSrcweir
127cdf0e10cSrcweir uno::Reference< msforms::XShape > SAL_CALL
getShape()128cdf0e10cSrcweir ScVbaComment::getShape() throw (uno::RuntimeException)
129cdf0e10cSrcweir {
130cdf0e10cSrcweir uno::Reference< sheet::XSheetAnnotationShapeSupplier > xAnnoShapeSupp( getAnnotation(), uno::UNO_QUERY_THROW );
131cdf0e10cSrcweir uno::Reference< drawing::XShape > xAnnoShape( xAnnoShapeSupp->getAnnotationShape(), uno::UNO_SET_THROW );
132cdf0e10cSrcweir uno::Reference< sheet::XSheetCellRange > xCellRange( mxRange, uno::UNO_QUERY_THROW );
133cdf0e10cSrcweir uno::Reference< drawing::XDrawPageSupplier > xDrawPageSupp( xCellRange->getSpreadsheet(), uno::UNO_QUERY_THROW );
134cdf0e10cSrcweir uno::Reference< drawing::XShapes > xShapes( xDrawPageSupp->getDrawPage(), uno::UNO_QUERY_THROW );
135cdf0e10cSrcweir return new ScVbaShape( this, mxContext, xAnnoShape, xShapes, mxModel, office::MsoShapeType::msoComment );
136cdf0e10cSrcweir }
137cdf0e10cSrcweir
138cdf0e10cSrcweir sal_Bool SAL_CALL
getVisible()139cdf0e10cSrcweir ScVbaComment::getVisible() throw (uno::RuntimeException)
140cdf0e10cSrcweir {
141cdf0e10cSrcweir return getAnnotation()->getIsVisible();
142cdf0e10cSrcweir }
143cdf0e10cSrcweir
144cdf0e10cSrcweir void SAL_CALL
setVisible(sal_Bool _visible)145cdf0e10cSrcweir ScVbaComment::setVisible( sal_Bool _visible ) throw (uno::RuntimeException)
146cdf0e10cSrcweir {
147cdf0e10cSrcweir getAnnotation()->setIsVisible( _visible );
148cdf0e10cSrcweir }
149cdf0e10cSrcweir
150cdf0e10cSrcweir void SAL_CALL
Delete()151cdf0e10cSrcweir ScVbaComment::Delete() throw (uno::RuntimeException)
152cdf0e10cSrcweir {
153cdf0e10cSrcweir getAnnotations()->removeByIndex( getAnnotationIndex() );
154cdf0e10cSrcweir }
155cdf0e10cSrcweir
156cdf0e10cSrcweir uno::Reference< excel::XComment > SAL_CALL
Next()157cdf0e10cSrcweir ScVbaComment::Next() throw (uno::RuntimeException)
158cdf0e10cSrcweir {
159cdf0e10cSrcweir // index: uno = 0, vba = 1
160cdf0e10cSrcweir return getCommentByIndex( getAnnotationIndex() + 2 );
161cdf0e10cSrcweir }
162cdf0e10cSrcweir
163cdf0e10cSrcweir uno::Reference< excel::XComment > SAL_CALL
Previous()164cdf0e10cSrcweir ScVbaComment::Previous() throw (uno::RuntimeException)
165cdf0e10cSrcweir {
166cdf0e10cSrcweir // index: uno = 0, vba = 1
167cdf0e10cSrcweir return getCommentByIndex( getAnnotationIndex() );
168cdf0e10cSrcweir }
169cdf0e10cSrcweir
170cdf0e10cSrcweir rtl::OUString SAL_CALL
Text(const uno::Any & aText,const uno::Any & aStart,const uno::Any & Overwrite)171cdf0e10cSrcweir ScVbaComment::Text( const uno::Any& aText, const uno::Any& aStart, const uno::Any& Overwrite ) throw (uno::RuntimeException)
172cdf0e10cSrcweir {
173cdf0e10cSrcweir rtl::OUString sText;
174cdf0e10cSrcweir aText >>= sText;
175cdf0e10cSrcweir
176cdf0e10cSrcweir uno::Reference< text::XSimpleText > xAnnoText( getAnnotation(), uno::UNO_QUERY_THROW );
177cdf0e10cSrcweir rtl::OUString sAnnoText = xAnnoText->getString();
178cdf0e10cSrcweir
179cdf0e10cSrcweir if ( aStart.hasValue() )
180cdf0e10cSrcweir {
181cdf0e10cSrcweir sal_Int16 nStart = 0;
182cdf0e10cSrcweir sal_Bool bOverwrite = sal_True;
183cdf0e10cSrcweir Overwrite >>= bOverwrite;
184cdf0e10cSrcweir
185cdf0e10cSrcweir if ( aStart >>= nStart )
186cdf0e10cSrcweir {
187cdf0e10cSrcweir uno::Reference< text::XTextCursor > xTextCursor( xAnnoText->createTextCursor(), uno::UNO_QUERY_THROW );
188cdf0e10cSrcweir
189cdf0e10cSrcweir if ( bOverwrite )
190cdf0e10cSrcweir {
191cdf0e10cSrcweir xTextCursor->collapseToStart();
192cdf0e10cSrcweir xTextCursor->gotoStart( sal_False );
193cdf0e10cSrcweir xTextCursor->goRight( nStart - 1, sal_False );
194cdf0e10cSrcweir xTextCursor->gotoEnd( sal_True );
195cdf0e10cSrcweir }
196cdf0e10cSrcweir else
197cdf0e10cSrcweir {
198cdf0e10cSrcweir xTextCursor->collapseToStart();
199cdf0e10cSrcweir xTextCursor->gotoStart( sal_False );
200cdf0e10cSrcweir xTextCursor->goRight( nStart - 1 , sal_True );
201cdf0e10cSrcweir }
202cdf0e10cSrcweir
203cdf0e10cSrcweir uno::Reference< text::XTextRange > xRange( xTextCursor, uno::UNO_QUERY_THROW );
204cdf0e10cSrcweir xAnnoText->insertString( xRange, sText, bOverwrite );
205cdf0e10cSrcweir return xAnnoText->getString();
206cdf0e10cSrcweir }
207cdf0e10cSrcweir throw uno::RuntimeException( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "ScVbaComment::Text - bad Start value " ) ), uno::Reference< uno::XInterface >() );
208cdf0e10cSrcweir }
209cdf0e10cSrcweir else if ( aText.hasValue() )
210cdf0e10cSrcweir {
211cdf0e10cSrcweir xAnnoText->setString( sText );
212cdf0e10cSrcweir return sText;
213cdf0e10cSrcweir }
214cdf0e10cSrcweir
215cdf0e10cSrcweir return sAnnoText;
216cdf0e10cSrcweir }
217cdf0e10cSrcweir
218cdf0e10cSrcweir rtl::OUString&
getServiceImplName()219cdf0e10cSrcweir ScVbaComment::getServiceImplName()
220cdf0e10cSrcweir {
221cdf0e10cSrcweir static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("ScVbaComment") );
222cdf0e10cSrcweir return sImplName;
223cdf0e10cSrcweir }
224cdf0e10cSrcweir
225cdf0e10cSrcweir uno::Sequence< rtl::OUString >
getServiceNames()226cdf0e10cSrcweir ScVbaComment::getServiceNames()
227cdf0e10cSrcweir {
228cdf0e10cSrcweir static uno::Sequence< rtl::OUString > aServiceNames;
229cdf0e10cSrcweir if ( aServiceNames.getLength() == 0 )
230cdf0e10cSrcweir {
231cdf0e10cSrcweir aServiceNames.realloc( 1 );
232cdf0e10cSrcweir aServiceNames[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.excel.ScVbaComment" ) );
233cdf0e10cSrcweir }
234cdf0e10cSrcweir return aServiceNames;
235cdf0e10cSrcweir }
236