LibreOffice Module sc (master)  1
vbacomment.cxx
Go to the documentation of this file.
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 
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  getAnnotation();
55 }
56 
57 // private helper functions
58 
59 uno::Reference< sheet::XSheetAnnotation >
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 >
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
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 >
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
116 {
117  return getAnnotation()->getAuthor();
118 }
119 
120 void SAL_CALL
121 ScVbaComment::setAuthor( const OUString& /*_author*/ )
122 {
123  // #TODO #FIXME implementation needed
124 }
125 
126 uno::Reference< msforms::XShape > SAL_CALL
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
139 {
140  return getAnnotation()->getIsVisible();
141 }
142 
143 void SAL_CALL
145 {
146  getAnnotation()->setIsVisible( _visible );
147 }
148 
149 void SAL_CALL
151 {
152  getAnnotations()->removeByIndex( getAnnotationIndex() );
153 }
154 
155 uno::Reference< excel::XComment > SAL_CALL
157 {
158  // index: uno = 0, vba = 1
159  return getCommentByIndex( getAnnotationIndex() + 2 );
160 }
161 
162 uno::Reference< excel::XComment > SAL_CALL
164 {
165  // index: uno = 0, vba = 1
167 }
168 
169 OUString SAL_CALL
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
220 {
221  return "ScVbaComment";
222 }
223 
224 uno::Sequence< OUString >
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: */
bool hasValue()
virtual void SAL_CALL setAuthor(const OUString &_author) override
Definition: vbacomment.cxx:121
virtual OUString SAL_CALL getAuthor() override
Definition: vbacomment.cxx:115
css::uno::Reference< css::frame::XModel2 > mxModel
Sequence< OUString > aServiceNames
virtual css::uno::Reference< ov::excel::XComment > SAL_CALL Previous() override
Definition: vbacomment.cxx:163
virtual css::uno::Reference< ov::msforms::XShape > SAL_CALL getShape() override
Definition: vbacomment.cxx:127
virtual void SAL_CALL Delete() override
Definition: vbacomment.cxx:150
virtual sal_Bool SAL_CALL getVisible() override
Definition: vbacomment.cxx:138
sal_Int32 getAnnotationIndex()
Definition: vbacomment.cxx:78
virtual css::uno::Sequence< OUString > getServiceNames() override
Definition: vbacomment.cxx:225
virtual OUString getServiceImplName() override
Definition: vbacomment.cxx:219
virtual css::uno::Reference< ov::excel::XComment > SAL_CALL Next() override
Definition: vbacomment.cxx:156
virtual void SAL_CALL setVisible(sal_Bool _visible) override
Definition: vbacomment.cxx:144
unsigned char sal_Bool
css::uno::Reference< css::sheet::XSheetAnnotation > getAnnotation()
Definition: vbacomment.cxx:60
css::uno::Reference< css::sheet::XSheetAnnotations > getAnnotations() const
Definition: vbacomment.cxx:68
std::deque< AttacherIndex_Impl > aIndex
css::uno::Reference< css::uno::XComponentContext > mxContext
virtual css::uno::Reference< ov::XHelperInterface > SAL_CALL getParent() override
ScVbaComment(const css::uno::Reference< ov::XHelperInterface > &xParent, const css::uno::Reference< css::uno::XComponentContext > &xContext, const css::uno::Reference< css::frame::XModel > &xModel, const css::uno::Reference< css::table::XCellRange > &xRange)
Definition: vbacomment.cxx:43
#define SAL_INFO(area, stream)
css::uno::Reference< css::frame::XModel > mxModel
Definition: vbacomment.hxx:34
css::uno::Reference< ov::excel::XComment > getCommentByIndex(sal_Int32 Index)
Definition: vbacomment.cxx:103
virtual OUString SAL_CALL Text(const css::uno::Any &Text, const css::uno::Any &Start, const css::uno::Any &Overwrite) override
Definition: vbacomment.cxx:170
css::uno::Reference< css::table::XCellRange > mxRange
Definition: vbacomment.hxx:35
struct _ADOIndex Index