LibreOffice Module vbahelper (master) 1
vbashapes.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
20#include <com/sun/star/view/XSelectionSupplier.hpp>
21#include <com/sun/star/text/WrapTextMode.hpp>
22#include <ooo/vba/msforms/XShapeRange.hpp>
23#include <ooo/vba/office/MsoAutoShapeType.hpp>
24#include <com/sun/star/awt/Point.hpp>
25#include <com/sun/star/awt/Size.hpp>
26#include <com/sun/star/beans/XPropertySet.hpp>
27#include <com/sun/star/frame/XModel.hpp>
28#include <com/sun/star/lang/XMultiServiceFactory.hpp>
29#include <com/sun/star/lang/XServiceInfo.hpp>
30#include <com/sun/star/script/BasicErrorException.hpp>
31#include <com/sun/star/script/XTypeConverter.hpp>
32#include <com/sun/star/text/TextContentAnchorType.hpp>
33#include <com/sun/star/text/HoriOrientation.hpp>
34#include <com/sun/star/text/VertOrientation.hpp>
35#include <com/sun/star/text/RelOrientation.hpp>
36#include <com/sun/star/drawing/LineStyle.hpp>
37#include <com/sun/star/drawing/XDrawPage.hpp>
38#include <com/sun/star/drawing/XShape.hpp>
39#include <com/sun/star/drawing/XShapes.hpp>
40
41#include <utility>
46#include <rtl/ref.hxx>
47
48using namespace ::ooo::vba;
49using namespace ::com::sun::star;
50
51namespace {
52
53class VbShapeEnumHelper : public EnumerationHelper_BASE
54{
55 uno::Reference<msforms::XShapes > m_xParent;
56 uno::Reference<container::XIndexAccess > m_xIndexAccess;
57 sal_Int32 nIndex;
58public:
59 VbShapeEnumHelper( uno::Reference< msforms::XShapes > xParent, uno::Reference< container::XIndexAccess > xIndexAccess ) : m_xParent(std::move( xParent )), m_xIndexAccess(std::move( xIndexAccess )), nIndex( 0 ) {}
60 virtual sal_Bool SAL_CALL hasMoreElements( ) override
61 {
62 return ( nIndex < m_xIndexAccess->getCount() );
63 }
64 virtual uno::Any SAL_CALL nextElement( ) override
65 {
66 ScVbaShapes* pShapes = dynamic_cast< ScVbaShapes* >(m_xParent.get());
67 if ( pShapes && hasMoreElements() )
68 return pShapes->createCollectionObject( m_xIndexAccess->getByIndex( nIndex++ ) );
69 throw container::NoSuchElementException();
70 }
71
72};
73
74}
75
77{
78 if ( m_xNameAccess.is() ) // already has NameAccess
79 return;
80 // no NameAccess then use ShapeCollectionHelper
82 sal_Int32 nLen = m_xIndexAccess->getCount();
83 aShapes.reserve( nLen );
84 for ( sal_Int32 index=0; index<nLen; ++index )
85 aShapes.emplace_back( m_xIndexAccess->getByIndex( index ) , uno::UNO_QUERY );
86 uno::Reference< container::XIndexAccess > xShapes( new XNamedObjectCollectionHelper< drawing::XShape >( std::move(aShapes) ) );
87 m_xIndexAccess = xShapes;
88 m_xNameAccess.set( xShapes, uno::UNO_QUERY );
89}
90
91ScVbaShapes::ScVbaShapes( const css::uno::Reference< ov::XHelperInterface >& xParent, const css::uno::Reference< css::uno::XComponentContext >& xContext, const css::uno::Reference< css::container::XIndexAccess >& xShapes, uno::Reference< frame::XModel> xModel ): ScVbaShapes_BASE( xParent, xContext, xShapes, true ), m_nNewShapeCount(0), m_xModel(std::move( xModel ))
92{
93 m_xShapes.set( xShapes, uno::UNO_QUERY_THROW );
94 m_xDrawPage.set( xShapes, uno::UNO_QUERY_THROW );
96}
97
98uno::Reference< container::XEnumeration >
100{
101 return new VbShapeEnumHelper( this, m_xIndexAccess );
102}
103
105ScVbaShapes::createCollectionObject( const css::uno::Any& aSource )
106{
107 if( aSource.hasValue() )
108 {
109 uno::Reference< drawing::XShape > xShape( aSource, uno::UNO_QUERY_THROW );
110 return uno::Any( uno::Reference< msforms::XShape >( new ScVbaShape( getParent(), mxContext, xShape, m_xShapes, m_xModel, ScVbaShape::getType( xShape ) ) ) );
111 }
112 return uno::Any();
113}
114
117{
119}
120
121OUString
123{
124 return "ScVbaShapes";
125}
126
127uno::Sequence< OUString >
129{
130 static uno::Sequence< OUString > const aServiceNames
131 {
132 "ooo.vba.msform.Shapes"
133 };
134 return aServiceNames;
135}
136
137css::uno::Reference< css::container::XIndexAccess >
139{
140 if ( Index.getValueTypeClass() != uno::TypeClass_SEQUENCE )
141 throw uno::RuntimeException();
142
143 const uno::Reference< script::XTypeConverter >& xConverter = getTypeConverter(mxContext);
144 uno::Any aConverted = xConverter->convertTo( Index, cppu::UnoType<uno::Sequence< uno::Any >>::get() );
145
146 uno::Sequence< uno::Any > sIndices;
147 aConverted >>= sIndices;
149 for( const auto& rIndex : std::as_const(sIndices) )
150 {
151 uno::Reference< drawing::XShape > xShape;
152 if ( rIndex.getValueTypeClass() == uno::TypeClass_STRING )
153 {
154 OUString sName;
155 rIndex >>= sName;
156 xShape.set( m_xNameAccess->getByName( sName ), uno::UNO_QUERY );
157 }
158 else
159 {
160 sal_Int32 nIndex = 0;
161 rIndex >>= nIndex;
162 // adjust for 1 based mso indexing
163 xShape.set( m_xIndexAccess->getByIndex( nIndex - 1 ), uno::UNO_QUERY );
164
165 }
166 // populate map with drawing::XShapes
167 if ( xShape.is() )
168 aShapes.push_back( xShape );
169 }
170 uno::Reference< container::XIndexAccess > xIndexAccess( new XNamedObjectCollectionHelper< drawing::XShape >( std::move(aShapes) ) );
171 return xIndexAccess;
172}
173
174uno::Reference< msforms::XShapeRange > SAL_CALL
176{
177 // shapes, can be an index or an array of indices
178 uno::Reference< container::XIndexAccess > xShapes;
179 if ( shapes.getValueTypeClass() == uno::TypeClass_SEQUENCE )
180 xShapes = getShapesByArrayIndices( shapes );
181 else
182 {
183 // wrap single index into a sequence
184 uno::Sequence< uno::Any > sIndices { shapes };
186 aIndex <<= sIndices;
187 xShapes = getShapesByArrayIndices( aIndex );
188 }
189 return new ScVbaShapeRange( getParent(), mxContext, xShapes, m_xDrawPage, m_xModel );
190}
191
192void SAL_CALL
194{
195 uno::Reference< view::XSelectionSupplier > xSelectSupp( m_xModel->getCurrentController(), uno::UNO_QUERY_THROW );
196 try
197 {
198 xSelectSupp->select( uno::Any( m_xShapes ) );
199 }
200 // viewuno.cxx ScTabViewObj::select will throw IllegalArgumentException
201 // if one of the shapes is no 'markable' e.g. a button
202 // the method still works
203 catch(const lang::IllegalArgumentException&)
204 {
205 }
206}
207
208uno::Reference< drawing::XShape >
209ScVbaShapes::createShape( const OUString& service )
210{
211 uno::Reference< lang::XMultiServiceFactory > xMSF( m_xModel, uno::UNO_QUERY_THROW );
212 uno::Reference< drawing::XShape > xShape( xMSF->createInstance( service ), uno::UNO_QUERY_THROW );
213 return xShape;
214}
215
217ScVbaShapes::AddRectangle(sal_Int32 startX, sal_Int32 startY, sal_Int32 nLineWidth, sal_Int32 nLineHeight)
218{
219 sal_Int32 nXPos = Millimeter::getInHundredthsOfOneMillimeter( startX );
220 sal_Int32 nYPos = Millimeter::getInHundredthsOfOneMillimeter( startY );
221 sal_Int32 nWidth = Millimeter::getInHundredthsOfOneMillimeter( nLineWidth );
222 sal_Int32 nHeight = Millimeter::getInHundredthsOfOneMillimeter( nLineHeight );
223
224 uno::Reference< drawing::XShape > xShape( createShape( "com.sun.star.drawing.RectangleShape" ), uno::UNO_SET_THROW );
225 m_xShapes->add( xShape );
226
227 OUString sName(createName( u"Rectangle" ));
229 setShape_NameProperty( xShape, sName );
230
231 awt::Point aMovePositionIfRange(0, 0);
232 awt::Point position;
233 position.X = nXPos - aMovePositionIfRange.X;
234 position.Y = nYPos - aMovePositionIfRange.Y;
235 xShape->setPosition( position );
236
237 awt::Size size;
238 size.Height = nHeight;
239 size.Width = nWidth;
240 xShape->setSize( size );
241
243 return uno::Any( uno::Reference< msforms::XShape > ( pScVbaShape ) );
244}
245
247ScVbaShapes::AddEllipse(sal_Int32 startX, sal_Int32 startY, sal_Int32 nLineWidth, sal_Int32 nLineHeight)
248{
249 sal_Int32 nXPos = Millimeter::getInHundredthsOfOneMillimeter( startX );
250 sal_Int32 nYPos = Millimeter::getInHundredthsOfOneMillimeter( startY );
251 sal_Int32 nWidth = Millimeter::getInHundredthsOfOneMillimeter( nLineWidth );
252 sal_Int32 nHeight = Millimeter::getInHundredthsOfOneMillimeter( nLineHeight );
253
254 uno::Reference< drawing::XShape > xShape( createShape( "com.sun.star.drawing.EllipseShape" ), uno::UNO_SET_THROW );
255 m_xShapes->add( xShape );
256
257 awt::Point aMovePositionIfRange( 0, 0 );
258 //TODO helperapi using a writer document
259 /*
260 XDocument xDocument = (XDocument)getParent();
261 if (AnyConverter.isVoid(_aRange))
262 {
263 _aRange = xDocument.Range(new Integer(0), new Integer(1));
264 // Top&Left in Word is Top&Left of the paper and not the writeable area.
265 aMovePositionIfRange = calculateTopLeftMargin((HelperInterfaceAdaptor)xDocument);
266 }
267
268 setShape_AnchorTypeAndRangeProperty(xShape, _aRange);
269 */
270 OUString name(createName( u"Oval" ));
273
274 awt::Point position;
275 position.X = nXPos - aMovePositionIfRange.X;
276 position.Y = nYPos - aMovePositionIfRange.Y;
277 xShape->setPosition(position);
278
279 awt::Size size;
280 size.Height = nHeight;
281 size.Width = nWidth;
282 xShape->setSize(size);
283
285 return uno::Any( uno::Reference< msforms::XShape > ( pScVbaShape ) );
286}
287
288//helperapi calc
289uno::Any SAL_CALL
290ScVbaShapes::AddLine( sal_Int32 StartX, sal_Int32 StartY, sal_Int32 endX, sal_Int32 endY )
291{
292 sal_Int32 nLineWidth = endX - StartX;
293 sal_Int32 nLineHeight = endY - StartY;
294
295 sal_Int32 nHeight = Millimeter::getInHundredthsOfOneMillimeter( nLineHeight );
296 sal_Int32 nWidth = Millimeter::getInHundredthsOfOneMillimeter( nLineWidth );
297 sal_Int32 nXPos = Millimeter::getInHundredthsOfOneMillimeter( StartX );
298 sal_Int32 nYPos = Millimeter::getInHundredthsOfOneMillimeter( StartY );
299
300 uno::Reference< drawing::XShape > xShape( createShape( "com.sun.star.drawing.LineShape" ), uno::UNO_SET_THROW );
301 m_xShapes->add( xShape );
302
303 awt::Point aMovePositionIfRange( 0, 0 );
304
305 OUString name(createName( u"Line" ));
308
309 awt::Point position;
310 position.X = nXPos - aMovePositionIfRange.X;
311 position.Y = nYPos - aMovePositionIfRange.Y;
312 xShape->setPosition(position);
313
314 awt::Size size;
315 size.Height = nHeight;
316 size.Width = nWidth;
317 xShape->setSize(size);
318
320 return uno::Any( uno::Reference< msforms::XShape > ( pScVbaShape ) );
321}
322
323uno::Any SAL_CALL
324ScVbaShapes::AddShape( sal_Int32 _nType, sal_Int32 _nLeft, sal_Int32 _nTop, sal_Int32 _nWidth, sal_Int32 _nHeight )
325{
326 if (_nType == office::MsoAutoShapeType::msoShapeRectangle)
327 {
328 return AddRectangle(_nLeft, _nTop, _nWidth, _nHeight);
329 }
330 else if (_nType == office::MsoAutoShapeType::msoShapeOval)
331 {
332 return AddEllipse(_nLeft, _nTop, _nWidth, _nHeight);
333 }
334 return uno::Any();
335}
336
337uno::Any SAL_CALL
338ScVbaShapes::AddTextbox( sal_Int32 /*_nOrientation*/, sal_Int32 _nLeft, sal_Int32 _nTop, sal_Int32 _nWidth, sal_Int32 _nHeight )
339{
340 uno::Reference< lang::XServiceInfo > xServiceInfo( m_xModel, uno::UNO_QUERY_THROW );
341 if( xServiceInfo->supportsService( "com.sun.star.text.TextDocument" ) )
342 {
343 return AddTextboxInWriter( _nLeft, _nTop, _nWidth, _nHeight );
344 }
345 throw uno::RuntimeException( "Not implemented" );
346}
347
349ScVbaShapes::AddTextboxInWriter( sal_Int32 _nLeft, sal_Int32 _nTop, sal_Int32 _nWidth, sal_Int32 _nHeight )
350{
351 sal_Int32 nXPos = Millimeter::getInHundredthsOfOneMillimeter( _nLeft );
352 sal_Int32 nYPos = Millimeter::getInHundredthsOfOneMillimeter( _nTop );
353 sal_Int32 nWidth = Millimeter::getInHundredthsOfOneMillimeter( _nWidth );
354 sal_Int32 nHeight = Millimeter::getInHundredthsOfOneMillimeter( _nHeight );
355
356 uno::Reference< drawing::XShape > xShape( createShape( "com.sun.star.drawing.TextShape" ), uno::UNO_SET_THROW );
357 m_xShapes->add( xShape );
358
360
361 OUString sName(createName( u"Text Box" ));
362 setShape_NameProperty( xShape, sName );
363
364 awt::Size size;
365 size.Height = nHeight;
366 size.Width = nWidth;
367 xShape->setSize(size);
368
369 uno::Reference< beans::XPropertySet > xShapeProps( xShape, uno::UNO_QUERY_THROW );
370 xShapeProps->setPropertyValue( "AnchorType", uno::Any( text::TextContentAnchorType_AT_PAGE ) );
371 xShapeProps->setPropertyValue( "HoriOrientRelation", uno::Any( text::RelOrientation::PAGE_LEFT ) );
372 xShapeProps->setPropertyValue( "HoriOrient", uno::Any( text::HoriOrientation::NONE ) );
373 xShapeProps->setPropertyValue( "HoriOrientPosition", uno::Any( nXPos ) );
374
375 xShapeProps->setPropertyValue( "VertOrientRelation", uno::Any( text::RelOrientation::PAGE_FRAME ) );
376 xShapeProps->setPropertyValue( "VertOrient", uno::Any( text::VertOrientation::NONE ) );
377 xShapeProps->setPropertyValue( "VertOrientPosition", uno::Any( nYPos ) );
378
379 // set to visible
380 xShapeProps->setPropertyValue( "LineStyle", uno::Any( drawing::LineStyle_SOLID ) );
381 // set to font
382 xShapeProps->setPropertyValue( "LayerID", uno::Any( sal_Int16(1) ) );
383 xShapeProps->setPropertyValue( "LayerName", uno::Any( OUString("Heaven") ) );
384
385
387 return uno::Any( uno::Reference< msforms::XShape > ( pScVbaShape ) );
388}
389
390void
391ScVbaShapes::setDefaultShapeProperties( const uno::Reference< drawing::XShape >& xShape )
392{
393 uno::Reference< beans::XPropertySet > xPropertySet( xShape, uno::UNO_QUERY_THROW );
394 xPropertySet->setPropertyValue( "FillStyle", uno::Any( OUString("SOLID") ) );
395 xPropertySet->setPropertyValue( "FillColor", uno::Any( sal_Int32(0xFFFFFF) ) );
396 xPropertySet->setPropertyValue( "TextWordWrap", uno::Any( text::WrapTextMode_THROUGH ) );
397 //not find in OOo2.3
398 //xPropertySet->setPropertyValue("Opaque", uno::makeAny( sal_True ) );
399}
400
401void
402ScVbaShapes::setShape_NameProperty( const uno::Reference< css::drawing::XShape >& xShape, const OUString& sName )
403{
404 uno::Reference< beans::XPropertySet > xPropertySet( xShape, uno::UNO_QUERY_THROW );
405 try
406 {
407 xPropertySet->setPropertyValue( "Name", uno::Any( sName ) );
408 }
409 catch(const script::BasicErrorException&)
410 {
411 }
412}
413
414OUString
415ScVbaShapes::createName( std::u16string_view sName )
416{
417 sal_Int32 nActNumber = 1 + m_nNewShapeCount;
419 return sName + OUString::number( nActNumber );
420}
421
422/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
struct _ADOIndex Index
sal_Int32 nLineWidth
css::uno::Reference< css::uno::XComponentContext > mxContext
virtual css::uno::Reference< ov::XHelperInterface > SAL_CALL getParent() override
css::uno::Reference< css::container::XNameAccess > m_xNameAccess
css::uno::Reference< css::container::XIndexAccess > m_xIndexAccess
virtual sal_Int32 SAL_CALL getType() override
Definition: vbashape.cxx:348
static void setShape_NameProperty(const css::uno::Reference< css::drawing::XShape > &xShape, const OUString &sName)
Definition: vbashapes.cxx:402
css::uno::Reference< css::frame::XModel > m_xModel
Definition: vbashapes.hxx:62
css::uno::Any AddEllipse(sal_Int32 startX, sal_Int32 startY, sal_Int32 nLineWidth, sal_Int32 nLineHeight)
Definition: vbashapes.cxx:247
css::uno::Reference< css::container::XIndexAccess > getShapesByArrayIndices(const css::uno::Any &Index)
Definition: vbashapes.cxx:138
css::uno::Any AddTextboxInWriter(sal_Int32 _nLeft, sal_Int32 _nTop, sal_Int32 _nWidth, sal_Int32 _nHeight)
Definition: vbashapes.cxx:349
virtual css::uno::Any createCollectionObject(const css::uno::Any &aSource) override
Definition: vbashapes.cxx:105
virtual css::uno::Type SAL_CALL getElementType() override
Definition: vbashapes.cxx:116
css::uno::Reference< css::drawing::XShape > createShape(const OUString &service)
Definition: vbashapes.cxx:209
sal_Int32 m_nNewShapeCount
Definition: vbashapes.hxx:60
css::uno::Any AddRectangle(sal_Int32 startX, sal_Int32 startY, sal_Int32 nLineWidth, sal_Int32 nLineHeight)
Definition: vbashapes.cxx:217
virtual void SAL_CALL SelectAll() override
Definition: vbashapes.cxx:193
virtual css::uno::Any SAL_CALL AddShape(sal_Int32 _nType, sal_Int32 _nLeft, sal_Int32 _nTop, sal_Int32 _nWidth, sal_Int32 _nHeight) override
Definition: vbashapes.cxx:324
ScVbaShapes(const css::uno::Reference< ov::XHelperInterface > &xParent, const css::uno::Reference< css::uno::XComponentContext > &xContext, const css::uno::Reference< css::container::XIndexAccess > &xShapes, css::uno::Reference< css::frame::XModel > xModel)
Definition: vbashapes.cxx:91
css::uno::Reference< css::drawing::XShapes > m_xShapes
Definition: vbashapes.hxx:58
void initBaseCollection()
Definition: vbashapes.cxx:76
virtual OUString getServiceImplName() override
Definition: vbashapes.cxx:122
virtual css::uno::Any SAL_CALL AddTextbox(sal_Int32 _nOrientation, sal_Int32 _nLeft, sal_Int32 _nTop, sal_Int32 _nWidth, sal_Int32 _nHeight) override
Definition: vbashapes.cxx:338
virtual css::uno::Reference< css::container::XEnumeration > SAL_CALL createEnumeration() override
Definition: vbashapes.cxx:99
static void setDefaultShapeProperties(const css::uno::Reference< css::drawing::XShape > &xShape)
Definition: vbashapes.cxx:391
virtual css::uno::Reference< ov::msforms::XShapeRange > SAL_CALL Range(const css::uno::Any &shapes) override
Definition: vbashapes.cxx:175
virtual css::uno::Sequence< OUString > getServiceNames() override
Definition: vbashapes.cxx:128
css::uno::Reference< css::drawing::XDrawPage > m_xDrawPage
Definition: vbashapes.hxx:59
OUString createName(std::u16string_view sName)
Definition: vbashapes.cxx:415
virtual css::uno::Any SAL_CALL AddLine(sal_Int32 StartX, sal_Int32 StartY, sal_Int32 endX, sal_Int32 endY) override
Definition: vbashapes.cxx:290
std::vector< css::uno::Reference< OneIfc > > XNamedVec
css::uno::Type const & get()
Reference< frame::XModel > m_xModel
float u
std::deque< AttacherIndex_Impl > aIndex
Reference< XTypeConverter > xConverter
Sequence< OUString > aServiceNames
OUString sName
const char * name
sal_Int32 nIndex
def position(n=-1)
size
index
uno::Reference< script::XTypeConverter > const & getTypeConverter(const uno::Reference< uno::XComponentContext > &xContext)
Definition: vbahelper.cxx:108
Reference< XModel > xModel
unsigned char sal_Bool
::cppu::WeakImplHelper< css::container::XEnumeration > EnumerationHelper_BASE