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