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