1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 #include "vbacomment.hxx"
20 
21 #include <com/sun/star/drawing/XDrawPageSupplier.hpp>
22 #include <com/sun/star/sheet/XSpreadsheet.hpp>
23 #include <com/sun/star/sheet/XSheetAnnotationAnchor.hpp>
24 #include <com/sun/star/sheet/XSheetAnnotationsSupplier.hpp>
25 #include <com/sun/star/sheet/XSheetAnnotationShapeSupplier.hpp>
26 #include <com/sun/star/sheet/XSheetCellRange.hpp>
27 #include <com/sun/star/sheet/XCellAddressable.hpp>
28 #include <com/sun/star/table/CellAddress.hpp>
29 #include <com/sun/star/table/XCell.hpp>
30 #include <com/sun/star/text/XSimpleText.hpp>
31 #include <com/sun/star/text/XTextCursor.hpp>
32 #include <com/sun/star/text/XTextRange.hpp>
33 #include <com/sun/star/frame/XModel.hpp>
34 #include <ooo/vba/office/MsoShapeType.hpp>
35 
36 #include <vbahelper/vbashape.hxx>
37 #include <sal/log.hxx>
38 #include "vbacomments.hxx"
39 
40 using namespace ::ooo::vba;
41 using namespace ::com::sun::star;
42 
ScVbaComment(const uno::Reference<XHelperInterface> & xParent,const uno::Reference<uno::XComponentContext> & xContext,const uno::Reference<frame::XModel> & xModel,const uno::Reference<table::XCellRange> & xRange)43 ScVbaComment::ScVbaComment(
44         const uno::Reference< XHelperInterface >& xParent,
45         const uno::Reference< uno::XComponentContext >& xContext,
46         const uno::Reference< frame::XModel >& xModel,
47         const uno::Reference< table::XCellRange >& xRange ) :
48     ScVbaComment_BASE( xParent, xContext ),
49     mxModel( xModel, uno::UNO_SET_THROW ),
50     mxRange( xRange )
51 {
52     if  ( !xRange.is() )
53         throw lang::IllegalArgumentException("range is not set ", uno::Reference< uno::XInterface >() , 1 );
54     uno::Reference< text::XSimpleText > xAnnoText( getAnnotation(), uno::UNO_QUERY );
55 }
56 
57 // private helper functions
58 
59 uno::Reference< sheet::XSheetAnnotation >
getAnnotation()60 ScVbaComment::getAnnotation()
61 {
62     uno::Reference< table::XCell > xCell( mxRange->getCellByPosition(0, 0), uno::UNO_SET_THROW );
63     uno::Reference< sheet::XSheetAnnotationAnchor > xAnnoAnchor( xCell, uno::UNO_QUERY_THROW );
64     return uno::Reference< sheet::XSheetAnnotation > ( xAnnoAnchor->getAnnotation(), uno::UNO_SET_THROW );
65 }
66 
67 uno::Reference< sheet::XSheetAnnotations >
getAnnotations() const68 ScVbaComment::getAnnotations() const
69 {
70     uno::Reference< sheet::XSheetCellRange > xSheetCellRange(mxRange, ::uno::UNO_QUERY_THROW );
71     uno::Reference< sheet::XSpreadsheet > xSheet = xSheetCellRange->getSpreadsheet();
72     uno::Reference< sheet::XSheetAnnotationsSupplier > xAnnosSupp( xSheet, uno::UNO_QUERY_THROW );
73 
74     return uno::Reference< sheet::XSheetAnnotations > ( xAnnosSupp->getAnnotations(), uno::UNO_SET_THROW );
75 }
76 
77 sal_Int32
getAnnotationIndex()78 ScVbaComment::getAnnotationIndex()
79 {
80     uno::Reference< sheet::XSheetAnnotations > xAnnos = getAnnotations();
81     table::CellAddress aAddress = getAnnotation()->getPosition();
82 
83     sal_Int32 aIndex = 0;
84     sal_Int32 aCount = xAnnos->getCount();
85 
86     for ( ; aIndex < aCount ; aIndex++ )
87     {
88         uno::Reference< sheet::XSheetAnnotation > xAnno( xAnnos->getByIndex( aIndex ), uno::UNO_QUERY_THROW );
89         table::CellAddress aAnnoAddress = xAnno->getPosition();
90 
91         if ( aAnnoAddress.Column == aAddress.Column && aAnnoAddress.Row == aAddress.Row && aAnnoAddress.Sheet == aAddress.Sheet )
92         {
93             SAL_INFO("sc.ui", "terminating search, index is " << aIndex);
94             break;
95         }
96     }
97     SAL_INFO("sc.ui", "returning index is " << aIndex);
98 
99     return aIndex;
100 }
101 
102 uno::Reference< excel::XComment >
getCommentByIndex(sal_Int32 Index)103 ScVbaComment::getCommentByIndex( sal_Int32 Index )
104 {
105     uno::Reference< container::XIndexAccess > xIndexAccess( getAnnotations(), uno::UNO_QUERY_THROW );
106     // parent is sheet ( parent of the range which is the parent of the comment )
107     uno::Reference< XCollection > xColl(  new ScVbaComments( getParent()->getParent(), mxContext, mxModel, xIndexAccess ) );
108 
109     return uno::Reference< excel::XComment > ( xColl->Item( uno::makeAny( Index ), uno::Any() ), uno::UNO_QUERY_THROW );
110  }
111 
112 // public vba functions
113 
114 OUString SAL_CALL
getAuthor()115 ScVbaComment::getAuthor()
116 {
117     return getAnnotation()->getAuthor();
118 }
119 
120 void SAL_CALL
setAuthor(const OUString &)121 ScVbaComment::setAuthor( const OUString& /*_author*/ )
122 {
123     // #TODO #FIXME  implementation needed
124 }
125 
126 uno::Reference< msforms::XShape > SAL_CALL
getShape()127 ScVbaComment::getShape()
128 {
129     uno::Reference< sheet::XSheetAnnotationShapeSupplier > xAnnoShapeSupp( getAnnotation(), uno::UNO_QUERY_THROW );
130     uno::Reference< drawing::XShape > xAnnoShape( xAnnoShapeSupp->getAnnotationShape(), uno::UNO_SET_THROW );
131     uno::Reference< sheet::XSheetCellRange > xCellRange( mxRange, uno::UNO_QUERY_THROW );
132     uno::Reference< drawing::XDrawPageSupplier > xDrawPageSupp( xCellRange->getSpreadsheet(), uno::UNO_QUERY_THROW );
133     uno::Reference< drawing::XShapes > xShapes( xDrawPageSupp->getDrawPage(), uno::UNO_QUERY_THROW );
134     return new ScVbaShape( this, mxContext, xAnnoShape, xShapes, mxModel, office::MsoShapeType::msoComment );
135 }
136 
137 sal_Bool SAL_CALL
getVisible()138 ScVbaComment::getVisible()
139 {
140     return getAnnotation()->getIsVisible();
141 }
142 
143 void SAL_CALL
setVisible(sal_Bool _visible)144 ScVbaComment::setVisible( sal_Bool _visible )
145 {
146     getAnnotation()->setIsVisible( _visible );
147 }
148 
149 void SAL_CALL
Delete()150 ScVbaComment::Delete()
151 {
152     getAnnotations()->removeByIndex( getAnnotationIndex() );
153 }
154 
155 uno::Reference< excel::XComment > SAL_CALL
Next()156 ScVbaComment::Next()
157 {
158     // index: uno = 0, vba = 1
159     return getCommentByIndex( getAnnotationIndex() + 2 );
160 }
161 
162 uno::Reference< excel::XComment > SAL_CALL
Previous()163 ScVbaComment::Previous()
164 {
165     // index: uno = 0, vba = 1
166     return getCommentByIndex( getAnnotationIndex() );
167 }
168 
169 OUString SAL_CALL
Text(const uno::Any & aText,const uno::Any & aStart,const uno::Any & Overwrite)170 ScVbaComment::Text( const uno::Any& aText, const uno::Any& aStart, const uno::Any& Overwrite )
171 {
172     OUString sText;
173     aText >>= sText;
174 
175     uno::Reference< text::XSimpleText > xAnnoText( getAnnotation(), uno::UNO_QUERY_THROW );
176     OUString sAnnoText = xAnnoText->getString();
177 
178     if ( aStart.hasValue() )
179     {
180         sal_Int16 nStart = 0;
181         bool bOverwrite = true;
182         Overwrite >>= bOverwrite;
183 
184         if ( aStart >>= nStart )
185         {
186             uno::Reference< text::XTextCursor > xTextCursor( xAnnoText->createTextCursor(), uno::UNO_SET_THROW );
187 
188             if ( bOverwrite )
189             {
190                 xTextCursor->collapseToStart();
191                 xTextCursor->gotoStart( false );
192                 xTextCursor->goRight( nStart - 1, false );
193                 xTextCursor->gotoEnd( true );
194             }
195             else
196             {
197                 xTextCursor->collapseToStart();
198                 xTextCursor->gotoStart( false );
199                 xTextCursor->goRight( nStart - 1 , true );
200             }
201 
202             uno::Reference< text::XTextRange > xRange( xTextCursor, uno::UNO_QUERY_THROW );
203             xAnnoText->insertString( xRange, sText, bOverwrite );
204             return xAnnoText->getString();
205         }
206         throw uno::RuntimeException("ScVbaComment::Text - bad Start value " );
207     }
208     else if ( aText.hasValue() )
209     {
210      uno::Reference< sheet::XCellAddressable > xCellAddr(mxRange->getCellByPosition(0, 0), uno::UNO_QUERY_THROW );
211      table::CellAddress aAddress = xCellAddr->getCellAddress();
212      getAnnotations()->insertNew( aAddress, sText );
213     }
214 
215     return sAnnoText;
216 }
217 
218 OUString
getServiceImplName()219 ScVbaComment::getServiceImplName()
220 {
221     return "ScVbaComment";
222 }
223 
224 uno::Sequence< OUString >
getServiceNames()225 ScVbaComment::getServiceNames()
226 {
227     static uno::Sequence< OUString > const aServiceNames
228     {
229        "ooo.vba.excel.ScVbaComment"
230     };
231     return aServiceNames;
232 }
233 
234 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
235