Version 6.1.4.1, tag libreoffice-6.1.4.1
[LibreOffice.git] / oox / source / vml / vmldrawing.cxx
blob57c099c0745dd528b4b34fab9ecc23f842a958db
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <oox/vml/vmldrawing.hxx>
22 #include <algorithm>
23 #include <com/sun/star/beans/XPropertySet.hpp>
24 #include <com/sun/star/drawing/XControlShape.hpp>
25 #include <com/sun/star/drawing/XShapes.hpp>
26 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
27 #include <com/sun/star/text/HoriOrientation.hpp>
28 #include <com/sun/star/text/RelOrientation.hpp>
29 #include <com/sun/star/text/VertOrientation.hpp>
30 #include <osl/diagnose.h>
31 #include <rtl/ustring.hxx>
32 #include <oox/core/xmlfilterbase.hxx>
33 #include <oox/helper/containerhelper.hxx>
34 #include <oox/ole/axcontrol.hxx>
35 #include <oox/vml/vmlshape.hxx>
36 #include <oox/vml/vmlshapecontainer.hxx>
38 namespace oox {
39 namespace vml {
41 using namespace ::com::sun::star;
42 using namespace ::com::sun::star::awt;
43 using namespace ::com::sun::star::beans;
44 using namespace ::com::sun::star::drawing;
45 using namespace ::com::sun::star::lang;
46 using namespace ::com::sun::star::text;
47 using namespace ::com::sun::star::uno;
49 using ::oox::core::XmlFilterBase;
51 namespace {
53 /** Returns the textual representation of a numeric VML shape identifier. */
54 OUString lclGetShapeId( sal_Int32 nShapeId )
56 // identifier consists of a literal NUL character, a lowercase 's', and the id
57 sal_Unicode const aStr[2] = { '\0', 's' };
58 return OUString( aStr, 2 ) + OUString::number( nShapeId );
61 /** Returns the numeric VML shape identifier from its textual representation. */
62 sal_Int32 lclGetShapeId( const OUString& rShapeId )
64 // identifier consists of a literal NUL character, a lowercase 's', and the id
65 return ((rShapeId.getLength() >= 3) && (rShapeId[ 0 ] == '\0') && (rShapeId[ 1 ] == 's')) ? rShapeId.copy( 2 ).toInt32() : -1;
68 } // namespace
70 OleObjectInfo::OleObjectInfo( bool bDmlShape ) :
71 mbAutoLoad( false ),
72 mbDmlShape( bDmlShape )
76 void OleObjectInfo::setShapeId( sal_Int32 nShapeId )
78 maShapeId = lclGetShapeId( nShapeId );
81 ControlInfo::ControlInfo()
82 : mbTextContentShape(false)
86 void ControlInfo::setShapeId( sal_Int32 nShapeId )
88 maShapeId = lclGetShapeId(nShapeId);
91 Drawing::Drawing( XmlFilterBase& rFilter, const Reference< XDrawPage >& rxDrawPage, DrawingType eType ) :
92 mrFilter( rFilter ),
93 mxDrawPage( rxDrawPage ),
94 mxShapes( new ShapeContainer( *this ) ),
95 meType( eType )
97 OSL_ENSURE( mxDrawPage.is(), "Drawing::Drawing - missing UNO draw page" );
100 Drawing::~Drawing()
104 ::oox::ole::EmbeddedForm& Drawing::getControlForm() const
106 if( !mxCtrlForm.get() )
107 mxCtrlForm.reset( new ::oox::ole::EmbeddedForm(
108 mrFilter.getModel(), mxDrawPage, mrFilter.getGraphicHelper() ) );
109 return *mxCtrlForm;
112 void Drawing::registerBlockId( sal_Int32 nBlockId )
114 OSL_ENSURE( nBlockId > 0, "Drawing::registerBlockId - invalid block index" );
115 if( nBlockId > 0 )
117 // lower_bound() returns iterator pointing to element equal to nBlockId, if existing
118 BlockIdVector::iterator aIt = ::std::lower_bound( maBlockIds.begin(), maBlockIds.end(), nBlockId );
119 if( (aIt == maBlockIds.end()) || (nBlockId != *aIt) )
120 maBlockIds.insert( aIt, nBlockId );
124 void Drawing::registerOleObject( const OleObjectInfo& rOleObject )
126 OSL_ENSURE( !rOleObject.maShapeId.isEmpty(), "Drawing::registerOleObject - missing OLE object shape id" );
127 OSL_ENSURE( maOleObjects.count( rOleObject.maShapeId ) == 0, "Drawing::registerOleObject - OLE object already registered" );
128 maOleObjects.emplace( rOleObject.maShapeId, rOleObject );
131 void Drawing::registerControl( const ControlInfo& rControl )
133 OSL_ENSURE( !rControl.maShapeId.isEmpty(), "Drawing::registerControl - missing form control shape id" );
134 OSL_ENSURE( !rControl.maName.isEmpty(), "Drawing::registerControl - missing form control name" );
135 OSL_ENSURE( maControls.count( rControl.maShapeId ) == 0, "Drawing::registerControl - form control already registered" );
136 maControls.emplace( rControl.maShapeId, rControl );
139 void Drawing::finalizeFragmentImport()
141 mxShapes->finalizeFragmentImport();
144 void Drawing::convertAndInsert() const
146 Reference< XShapes > xShapes( mxDrawPage, UNO_QUERY );
147 mxShapes->convertAndInsert( xShapes );
150 sal_Int32 Drawing::getLocalShapeIndex( const OUString& rShapeId ) const
152 sal_Int32 nShapeId = lclGetShapeId( rShapeId );
153 if( nShapeId <= 0 ) return -1;
155 /* Shapes in a drawing are counted per registered shape identifier blocks
156 as stored in the o:idmap element. The contents of this element have
157 been stored in our member maBlockIds. Each block represents 1024 shape
158 identifiers, starting with identifier 1 for the block #0. This means,
159 block #0 represents the identifiers 1-1024, block #1 represents the
160 identifiers 1025-2048, and so on. The local shape index has to be
161 calculated according to all blocks registered for this drawing.
163 Example:
164 Registered for this drawing are blocks #1 and #3 (shape identifiers
165 1025-2048 and 3073-4096).
166 Shape identifier 1025 -> local shape index 1.
167 Shape identifier 1026 -> local shape index 2.
169 Shape identifier 2048 -> local shape index 1024.
170 Shape identifier 3073 -> local shape index 1025.
172 Shape identifier 4096 -> local shape index 2048.
175 // get block id from shape id and find its index in the list of used blocks
176 sal_Int32 nBlockId = (nShapeId - 1) / 1024;
177 BlockIdVector::iterator aIt = ::std::lower_bound( maBlockIds.begin(), maBlockIds.end(), nBlockId );
178 sal_Int32 nIndex = static_cast< sal_Int32 >( aIt - maBlockIds.begin() );
180 // block id not found in set -> register it now (value of nIndex remains valid)
181 if( (aIt == maBlockIds.end()) || (*aIt != nBlockId) )
182 maBlockIds.insert( aIt, nBlockId );
184 // get one-based offset of shape id in its block
185 sal_Int32 nBlockOffset = (nShapeId - 1) % 1024 + 1;
187 // calculate the local shape index
188 return 1024 * nIndex + nBlockOffset;
191 const OleObjectInfo* Drawing::getOleObjectInfo( const OUString& rShapeId ) const
193 return ContainerHelper::getMapElement( maOleObjects, rShapeId );
196 const ControlInfo* Drawing::getControlInfo( const OUString& rShapeId ) const
198 return ContainerHelper::getMapElement( maControls, rShapeId );
201 Reference< XShape > Drawing::createAndInsertXShape( const OUString& rService,
202 const Reference< XShapes >& rxShapes, const awt::Rectangle& rShapeRect ) const
204 OSL_ENSURE( !rService.isEmpty(), "Drawing::createAndInsertXShape - missing UNO shape service name" );
205 OSL_ENSURE( rxShapes.is(), "Drawing::createAndInsertXShape - missing XShapes container" );
206 Reference< XShape > xShape;
207 if( !rService.isEmpty() && rxShapes.is() ) try
209 Reference< XMultiServiceFactory > xModelFactory( mrFilter.getModelFactory(), UNO_SET_THROW );
210 xShape.set( xModelFactory->createInstance( rService ), UNO_QUERY_THROW );
211 if ( rService != "com.sun.star.text.TextFrame" )
213 // insert shape into passed shape collection (maybe drawpage or group shape)
214 rxShapes->add( xShape );
215 xShape->setPosition( awt::Point( rShapeRect.X, rShapeRect.Y ) );
217 else
219 Reference< XPropertySet > xPropSet( xShape, UNO_QUERY_THROW );
220 xPropSet->setPropertyValue( "HoriOrient", makeAny( HoriOrientation::NONE ) );
221 xPropSet->setPropertyValue( "VertOrient", makeAny( VertOrientation::NONE ) );
222 xPropSet->setPropertyValue( "HoriOrientPosition", makeAny( rShapeRect.X ) );
223 xPropSet->setPropertyValue( "VertOrientPosition", makeAny( rShapeRect.Y ) );
224 xPropSet->setPropertyValue( "HoriOrientRelation", makeAny( RelOrientation::FRAME ) );
225 xPropSet->setPropertyValue( "VertOrientRelation", makeAny( RelOrientation::FRAME ) );
227 xShape->setSize( awt::Size( rShapeRect.Width, rShapeRect.Height ) );
229 catch( Exception& e )
231 SAL_WARN( "oox", "Drawing::createAndInsertXShape - error during shape object creation: " << e );
233 OSL_ENSURE( xShape.is(), "Drawing::createAndInsertXShape - cannot instantiate shape object" );
234 return xShape;
237 Reference< XShape > Drawing::createAndInsertXControlShape( const ::oox::ole::EmbeddedControl& rControl,
238 const Reference< XShapes >& rxShapes, const awt::Rectangle& rShapeRect, sal_Int32& rnCtrlIndex ) const
240 Reference< XShape > xShape;
243 // create control model and insert it into the form of the draw page
244 Reference< XControlModel > xCtrlModel( getControlForm().convertAndInsert( rControl, rnCtrlIndex ), UNO_SET_THROW );
246 // create the control shape
247 xShape = createAndInsertXShape( "com.sun.star.drawing.ControlShape", rxShapes, rShapeRect );
249 // set the control model at the shape
250 Reference< XControlShape >( xShape, UNO_QUERY_THROW )->setControl( xCtrlModel );
252 catch (Exception const& e)
254 SAL_WARN("oox", "exception inserting Shape: " << e);
256 return xShape;
259 bool Drawing::isShapeSupported( const ShapeBase& /*rShape*/ ) const
261 return true;
264 OUString Drawing::getShapeBaseName( const ShapeBase& /*rShape*/ ) const
266 return OUString();
269 bool Drawing::convertClientAnchor( awt::Rectangle& /*orShapeRect*/, const OUString& /*rShapeAnchor*/ ) const
271 return false;
274 Reference< XShape > Drawing::createAndInsertClientXShape( const ShapeBase& /*rShape*/,
275 const Reference< XShapes >& /*rxShapes*/, const awt::Rectangle& /*rShapeRect*/ ) const
277 return Reference< XShape >();
280 void Drawing::notifyXShapeInserted( const Reference< XShape >& /*rxShape*/,
281 const awt::Rectangle& /*rShapeRect*/, const ShapeBase& /*rShape*/, bool /*bGroupChild*/ )
285 } // namespace vml
286 } // namespave oox
288 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */