xref: /aoo42x/main/sc/source/ui/vba/vbacomment.cxx (revision cdf0e10c)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir #include "vbacomment.hxx"
28*cdf0e10cSrcweir 
29*cdf0e10cSrcweir #include <ooo/vba/excel/XlCreator.hpp>
30*cdf0e10cSrcweir #include <com/sun/star/drawing/XDrawPageSupplier.hpp>
31*cdf0e10cSrcweir #include <com/sun/star/sheet/XSpreadsheet.hpp>
32*cdf0e10cSrcweir #include <com/sun/star/sheet/XSheetAnnotationAnchor.hpp>
33*cdf0e10cSrcweir #include <com/sun/star/sheet/XSheetAnnotationsSupplier.hpp>
34*cdf0e10cSrcweir #include <com/sun/star/sheet/XSheetAnnotationShapeSupplier.hpp>
35*cdf0e10cSrcweir #include <com/sun/star/sheet/XSheetCellRange.hpp>
36*cdf0e10cSrcweir #include <com/sun/star/table/CellAddress.hpp>
37*cdf0e10cSrcweir #include <com/sun/star/table/XCell.hpp>
38*cdf0e10cSrcweir #include <com/sun/star/text/XText.hpp>
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir #include <vbahelper/vbashape.hxx>
41*cdf0e10cSrcweir #include "vbaglobals.hxx"
42*cdf0e10cSrcweir #include "vbacomments.hxx"
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir using namespace ::ooo::vba;
46*cdf0e10cSrcweir using namespace ::com::sun::star;
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir ScVbaComment::ScVbaComment(
49*cdf0e10cSrcweir         const uno::Reference< XHelperInterface >& xParent,
50*cdf0e10cSrcweir         const uno::Reference< uno::XComponentContext >& xContext,
51*cdf0e10cSrcweir         const uno::Reference< frame::XModel >& xModel,
52*cdf0e10cSrcweir         const uno::Reference< table::XCellRange >& xRange ) throw( lang::IllegalArgumentException ) :
53*cdf0e10cSrcweir     ScVbaComment_BASE( xParent, xContext ),
54*cdf0e10cSrcweir     mxModel( xModel, uno::UNO_SET_THROW ),
55*cdf0e10cSrcweir     mxRange( xRange )
56*cdf0e10cSrcweir {
57*cdf0e10cSrcweir 	if  ( !xRange.is() )
58*cdf0e10cSrcweir 		throw lang::IllegalArgumentException( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "range is not set " ) ), uno::Reference< uno::XInterface >() , 1 );
59*cdf0e10cSrcweir 	uno::Reference< text::XSimpleText > xAnnoText( getAnnotation(), uno::UNO_QUERY );
60*cdf0e10cSrcweir }
61*cdf0e10cSrcweir 
62*cdf0e10cSrcweir // private helper functions
63*cdf0e10cSrcweir 
64*cdf0e10cSrcweir uno::Reference< sheet::XSheetAnnotation > SAL_CALL
65*cdf0e10cSrcweir ScVbaComment::getAnnotation() throw (uno::RuntimeException)
66*cdf0e10cSrcweir {
67*cdf0e10cSrcweir 	uno::Reference< table::XCell > xCell( mxRange->getCellByPosition(0, 0), uno::UNO_QUERY_THROW );
68*cdf0e10cSrcweir 	uno::Reference< sheet::XSheetAnnotationAnchor > xAnnoAnchor( xCell, uno::UNO_QUERY_THROW );
69*cdf0e10cSrcweir 	return uno::Reference< sheet::XSheetAnnotation > ( xAnnoAnchor->getAnnotation(), uno::UNO_QUERY_THROW );
70*cdf0e10cSrcweir }
71*cdf0e10cSrcweir 
72*cdf0e10cSrcweir uno::Reference< sheet::XSheetAnnotations > SAL_CALL
73*cdf0e10cSrcweir ScVbaComment::getAnnotations() throw (uno::RuntimeException)
74*cdf0e10cSrcweir {
75*cdf0e10cSrcweir 	uno::Reference< sheet::XSheetCellRange > xSheetCellRange(mxRange, ::uno::UNO_QUERY_THROW );
76*cdf0e10cSrcweir 	uno::Reference< sheet::XSpreadsheet > xSheet = xSheetCellRange->getSpreadsheet();
77*cdf0e10cSrcweir 	uno::Reference< sheet::XSheetAnnotationsSupplier > xAnnosSupp( xSheet, uno::UNO_QUERY_THROW );
78*cdf0e10cSrcweir 
79*cdf0e10cSrcweir 	return uno::Reference< sheet::XSheetAnnotations > ( xAnnosSupp->getAnnotations(), uno::UNO_QUERY_THROW );
80*cdf0e10cSrcweir }
81*cdf0e10cSrcweir 
82*cdf0e10cSrcweir sal_Int32 SAL_CALL
83*cdf0e10cSrcweir ScVbaComment::getAnnotationIndex() throw (uno::RuntimeException)
84*cdf0e10cSrcweir {
85*cdf0e10cSrcweir 	uno::Reference< sheet::XSheetAnnotations > xAnnos = getAnnotations();
86*cdf0e10cSrcweir 	table::CellAddress aAddress = getAnnotation()->getPosition();
87*cdf0e10cSrcweir 
88*cdf0e10cSrcweir 	sal_Int32 aIndex = 0;
89*cdf0e10cSrcweir 	sal_Int32 aCount = xAnnos->getCount();
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir 	for ( ; aIndex < aCount ; aIndex++ )
92*cdf0e10cSrcweir 	{
93*cdf0e10cSrcweir 		uno::Reference< sheet::XSheetAnnotation > xAnno( xAnnos->getByIndex( aIndex ), uno::UNO_QUERY_THROW );
94*cdf0e10cSrcweir 		table::CellAddress xAddress = xAnno->getPosition();
95*cdf0e10cSrcweir 
96*cdf0e10cSrcweir 		if ( xAddress.Column == aAddress.Column && xAddress.Row == aAddress.Row && xAddress.Sheet == aAddress.Sheet )
97*cdf0e10cSrcweir 		{
98*cdf0e10cSrcweir 			OSL_TRACE("** terminating search, index is %d", aIndex );
99*cdf0e10cSrcweir 			break;
100*cdf0e10cSrcweir 		}
101*cdf0e10cSrcweir 	}
102*cdf0e10cSrcweir 	OSL_TRACE("** returning index is %d", aIndex );
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir        return aIndex;
105*cdf0e10cSrcweir }
106*cdf0e10cSrcweir 
107*cdf0e10cSrcweir uno::Reference< excel::XComment > SAL_CALL
108*cdf0e10cSrcweir ScVbaComment::getCommentByIndex( sal_Int32 Index ) throw (uno::RuntimeException)
109*cdf0e10cSrcweir {
110*cdf0e10cSrcweir 	uno::Reference< container::XIndexAccess > xIndexAccess( getAnnotations(), uno::UNO_QUERY_THROW );
111*cdf0e10cSrcweir 	// parent is sheet ( parent of the range which is the parent of the comment )
112*cdf0e10cSrcweir 	uno::Reference< XCollection > xColl(  new ScVbaComments( getParent()->getParent(), mxContext, mxModel, xIndexAccess ) );
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir 	return uno::Reference< excel::XComment > ( xColl->Item( uno::makeAny( Index ), uno::Any() ), uno::UNO_QUERY_THROW );
115*cdf0e10cSrcweir  }
116*cdf0e10cSrcweir 
117*cdf0e10cSrcweir // public vba functions
118*cdf0e10cSrcweir 
119*cdf0e10cSrcweir rtl::OUString SAL_CALL
120*cdf0e10cSrcweir ScVbaComment::getAuthor() throw (uno::RuntimeException)
121*cdf0e10cSrcweir {
122*cdf0e10cSrcweir 	return getAnnotation()->getAuthor();
123*cdf0e10cSrcweir }
124*cdf0e10cSrcweir 
125*cdf0e10cSrcweir void SAL_CALL
126*cdf0e10cSrcweir ScVbaComment::setAuthor( const rtl::OUString& /*_author*/ ) throw (uno::RuntimeException)
127*cdf0e10cSrcweir {
128*cdf0e10cSrcweir 	// #TODO #FIXME  implementation needed
129*cdf0e10cSrcweir }
130*cdf0e10cSrcweir 
131*cdf0e10cSrcweir uno::Reference< msforms::XShape > SAL_CALL
132*cdf0e10cSrcweir ScVbaComment::getShape() throw (uno::RuntimeException)
133*cdf0e10cSrcweir {
134*cdf0e10cSrcweir     uno::Reference< sheet::XSheetAnnotationShapeSupplier > xAnnoShapeSupp( getAnnotation(), uno::UNO_QUERY_THROW );
135*cdf0e10cSrcweir     uno::Reference< drawing::XShape > xAnnoShape( xAnnoShapeSupp->getAnnotationShape(), uno::UNO_SET_THROW );
136*cdf0e10cSrcweir 	uno::Reference< sheet::XSheetCellRange > xCellRange( mxRange, uno::UNO_QUERY_THROW );
137*cdf0e10cSrcweir 	uno::Reference< drawing::XDrawPageSupplier > xDrawPageSupp( xCellRange->getSpreadsheet(), uno::UNO_QUERY_THROW );
138*cdf0e10cSrcweir 	uno::Reference< drawing::XShapes > xShapes( xDrawPageSupp->getDrawPage(), uno::UNO_QUERY_THROW );
139*cdf0e10cSrcweir     return new ScVbaShape( this, mxContext, xAnnoShape, xShapes, mxModel, office::MsoShapeType::msoComment );
140*cdf0e10cSrcweir }
141*cdf0e10cSrcweir 
142*cdf0e10cSrcweir sal_Bool SAL_CALL
143*cdf0e10cSrcweir ScVbaComment::getVisible() throw (uno::RuntimeException)
144*cdf0e10cSrcweir {
145*cdf0e10cSrcweir 	return getAnnotation()->getIsVisible();
146*cdf0e10cSrcweir }
147*cdf0e10cSrcweir 
148*cdf0e10cSrcweir void SAL_CALL
149*cdf0e10cSrcweir ScVbaComment::setVisible( sal_Bool _visible ) throw (uno::RuntimeException)
150*cdf0e10cSrcweir {
151*cdf0e10cSrcweir 	getAnnotation()->setIsVisible( _visible );
152*cdf0e10cSrcweir }
153*cdf0e10cSrcweir 
154*cdf0e10cSrcweir void SAL_CALL
155*cdf0e10cSrcweir ScVbaComment::Delete() throw (uno::RuntimeException)
156*cdf0e10cSrcweir {
157*cdf0e10cSrcweir 	getAnnotations()->removeByIndex( getAnnotationIndex() );
158*cdf0e10cSrcweir }
159*cdf0e10cSrcweir 
160*cdf0e10cSrcweir uno::Reference< excel::XComment > SAL_CALL
161*cdf0e10cSrcweir ScVbaComment::Next() throw (uno::RuntimeException)
162*cdf0e10cSrcweir {
163*cdf0e10cSrcweir 	// index: uno = 0, vba = 1
164*cdf0e10cSrcweir 	return getCommentByIndex( getAnnotationIndex() + 2 );
165*cdf0e10cSrcweir }
166*cdf0e10cSrcweir 
167*cdf0e10cSrcweir uno::Reference< excel::XComment > SAL_CALL
168*cdf0e10cSrcweir ScVbaComment::Previous() throw (uno::RuntimeException)
169*cdf0e10cSrcweir {
170*cdf0e10cSrcweir 	// index: uno = 0, vba = 1
171*cdf0e10cSrcweir 	return getCommentByIndex( getAnnotationIndex() );
172*cdf0e10cSrcweir }
173*cdf0e10cSrcweir 
174*cdf0e10cSrcweir rtl::OUString SAL_CALL
175*cdf0e10cSrcweir ScVbaComment::Text( const uno::Any& aText, const uno::Any& aStart, const uno::Any& Overwrite ) throw (uno::RuntimeException)
176*cdf0e10cSrcweir {
177*cdf0e10cSrcweir 	rtl::OUString sText;
178*cdf0e10cSrcweir 	aText >>= sText;
179*cdf0e10cSrcweir 
180*cdf0e10cSrcweir 	uno::Reference< text::XSimpleText > xAnnoText( getAnnotation(), uno::UNO_QUERY_THROW );
181*cdf0e10cSrcweir 	rtl::OUString sAnnoText = xAnnoText->getString();
182*cdf0e10cSrcweir 
183*cdf0e10cSrcweir 	if ( aStart.hasValue() )
184*cdf0e10cSrcweir 	{
185*cdf0e10cSrcweir 		sal_Int16 nStart = 0;
186*cdf0e10cSrcweir 		sal_Bool bOverwrite = sal_True;
187*cdf0e10cSrcweir 		Overwrite >>= bOverwrite;
188*cdf0e10cSrcweir 
189*cdf0e10cSrcweir 		if ( aStart >>= nStart )
190*cdf0e10cSrcweir 		{
191*cdf0e10cSrcweir 			uno::Reference< text::XTextCursor > xTextCursor( xAnnoText->createTextCursor(), uno::UNO_QUERY_THROW );
192*cdf0e10cSrcweir 
193*cdf0e10cSrcweir 			if ( bOverwrite )
194*cdf0e10cSrcweir 			{
195*cdf0e10cSrcweir 				xTextCursor->collapseToStart();
196*cdf0e10cSrcweir 				xTextCursor->gotoStart( sal_False );
197*cdf0e10cSrcweir 				xTextCursor->goRight( nStart - 1, sal_False );
198*cdf0e10cSrcweir 				xTextCursor->gotoEnd( sal_True );
199*cdf0e10cSrcweir 			}
200*cdf0e10cSrcweir 			else
201*cdf0e10cSrcweir 			{
202*cdf0e10cSrcweir 				xTextCursor->collapseToStart();
203*cdf0e10cSrcweir 				xTextCursor->gotoStart( sal_False );
204*cdf0e10cSrcweir 				xTextCursor->goRight( nStart - 1 , sal_True );
205*cdf0e10cSrcweir 			}
206*cdf0e10cSrcweir 
207*cdf0e10cSrcweir 			uno::Reference< text::XTextRange > xRange( xTextCursor, uno::UNO_QUERY_THROW );
208*cdf0e10cSrcweir 			xAnnoText->insertString( xRange, sText, bOverwrite );
209*cdf0e10cSrcweir 			return xAnnoText->getString();
210*cdf0e10cSrcweir 		}
211*cdf0e10cSrcweir 		throw uno::RuntimeException( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "ScVbaComment::Text - bad Start value " ) ), uno::Reference< uno::XInterface >() );
212*cdf0e10cSrcweir 	}
213*cdf0e10cSrcweir 	else if ( aText.hasValue() )
214*cdf0e10cSrcweir 	{
215*cdf0e10cSrcweir 		xAnnoText->setString( sText );
216*cdf0e10cSrcweir 		return sText;
217*cdf0e10cSrcweir 	}
218*cdf0e10cSrcweir 
219*cdf0e10cSrcweir 	return sAnnoText;
220*cdf0e10cSrcweir }
221*cdf0e10cSrcweir 
222*cdf0e10cSrcweir rtl::OUString&
223*cdf0e10cSrcweir ScVbaComment::getServiceImplName()
224*cdf0e10cSrcweir {
225*cdf0e10cSrcweir 	static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("ScVbaComment") );
226*cdf0e10cSrcweir 	return sImplName;
227*cdf0e10cSrcweir }
228*cdf0e10cSrcweir 
229*cdf0e10cSrcweir uno::Sequence< rtl::OUString >
230*cdf0e10cSrcweir ScVbaComment::getServiceNames()
231*cdf0e10cSrcweir {
232*cdf0e10cSrcweir 	static uno::Sequence< rtl::OUString > aServiceNames;
233*cdf0e10cSrcweir 	if ( aServiceNames.getLength() == 0 )
234*cdf0e10cSrcweir 	{
235*cdf0e10cSrcweir 		aServiceNames.realloc( 1 );
236*cdf0e10cSrcweir 		aServiceNames[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.excel.ScVbaComment" ) );
237*cdf0e10cSrcweir 	}
238*cdf0e10cSrcweir 	return aServiceNames;
239*cdf0e10cSrcweir }
240