bump product version to 4.2.0.1
[LibreOffice.git] / oox / source / vml / vmldrawing.cxx
blobf76777d6c12c58758848dec5e25c409ef197502a
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 <rtl/ustring.hxx>
31 #include "oox/core/xmlfilterbase.hxx"
32 #include "oox/helper/containerhelper.hxx"
33 #include "oox/ole/axcontrol.hxx"
34 #include "oox/vml/vmlshape.hxx"
35 #include "oox/vml/vmlshapecontainer.hxx"
37 namespace oox {
38 namespace vml {
40 // ============================================================================
42 using namespace ::com::sun::star;
43 using namespace ::com::sun::star::awt;
44 using namespace ::com::sun::star::beans;
45 using namespace ::com::sun::star::drawing;
46 using namespace ::com::sun::star::lang;
47 using namespace ::com::sun::star::text;
48 using namespace ::com::sun::star::uno;
50 using ::oox::core::XmlFilterBase;
52 // ============================================================================
54 namespace {
56 /** Returns the textual representation of a numeric VML shape identifier. */
57 OUString lclGetShapeId( sal_Int32 nShapeId )
59 // identifier consists of a literal NUL character, a lowercase 's', and the id
60 sal_Unicode aStr[2] = { '\0', 's' };
61 return OUString( aStr, 2 ) + OUString::number( nShapeId );
64 /** Returns the numeric VML shape identifier from its textual representation. */
65 sal_Int32 lclGetShapeId( const OUString& rShapeId )
67 // identifier consists of a literal NUL character, a lowercase 's', and the id
68 return ((rShapeId.getLength() >= 3) && (rShapeId[ 0 ] == '\0') && (rShapeId[ 1 ] == 's')) ? rShapeId.copy( 2 ).toInt32() : -1;
71 } // namespace
73 // ============================================================================
75 OleObjectInfo::OleObjectInfo( bool bDmlShape ) :
76 mbAutoLoad( false ),
77 mbDmlShape( bDmlShape )
81 void OleObjectInfo::setShapeId( sal_Int32 nShapeId )
83 maShapeId = lclGetShapeId( nShapeId );
86 // ============================================================================
88 ControlInfo::ControlInfo()
92 void ControlInfo::setShapeId( sal_Int32 nShapeId )
94 maShapeId = lclGetShapeId( nShapeId );
97 // ============================================================================
99 Drawing::Drawing( XmlFilterBase& rFilter, const Reference< XDrawPage >& rxDrawPage, DrawingType eType ) :
100 mrFilter( rFilter ),
101 mxDrawPage( rxDrawPage ),
102 mxShapes( new ShapeContainer( *this ) ),
103 meType( eType )
105 OSL_ENSURE( mxDrawPage.is(), "Drawing::Drawing - missing UNO draw page" );
108 Drawing::~Drawing()
112 ::oox::ole::EmbeddedForm& Drawing::getControlForm() const
114 if( !mxCtrlForm.get() )
115 mxCtrlForm.reset( new ::oox::ole::EmbeddedForm(
116 mrFilter.getModel(), mxDrawPage, mrFilter.getGraphicHelper() ) );
117 return *mxCtrlForm;
120 void Drawing::registerBlockId( sal_Int32 nBlockId )
122 OSL_ENSURE( nBlockId > 0, "Drawing::registerBlockId - invalid block index" );
123 if( nBlockId > 0 )
125 // lower_bound() returns iterator pointing to element equal to nBlockId, if existing
126 BlockIdVector::iterator aIt = ::std::lower_bound( maBlockIds.begin(), maBlockIds.end(), nBlockId );
127 if( (aIt == maBlockIds.end()) || (nBlockId != *aIt) )
128 maBlockIds.insert( aIt, nBlockId );
132 void Drawing::registerOleObject( const OleObjectInfo& rOleObject )
134 OSL_ENSURE( !rOleObject.maShapeId.isEmpty(), "Drawing::registerOleObject - missing OLE object shape id" );
135 OSL_ENSURE( maOleObjects.count( rOleObject.maShapeId ) == 0, "Drawing::registerOleObject - OLE object already registered" );
136 maOleObjects.insert( OleObjectInfoMap::value_type( rOleObject.maShapeId, rOleObject ) );
139 void Drawing::registerControl( const ControlInfo& rControl )
141 OSL_ENSURE( !rControl.maShapeId.isEmpty(), "Drawing::registerControl - missing form control shape id" );
142 OSL_ENSURE( !rControl.maName.isEmpty(), "Drawing::registerControl - missing form control name" );
143 OSL_ENSURE( maControls.count( rControl.maShapeId ) == 0, "Drawing::registerControl - form control already registered" );
144 maControls.insert( ControlInfoMap::value_type( rControl.maShapeId, rControl ) );
147 void Drawing::finalizeFragmentImport()
149 mxShapes->finalizeFragmentImport();
152 void Drawing::convertAndInsert() const
154 Reference< XShapes > xShapes( mxDrawPage, UNO_QUERY );
155 mxShapes->convertAndInsert( xShapes );
158 sal_Int32 Drawing::getLocalShapeIndex( const OUString& rShapeId ) const
160 sal_Int32 nShapeId = lclGetShapeId( rShapeId );
161 if( nShapeId <= 0 ) return -1;
163 /* Shapes in a drawing are counted per registered shape identifier blocks
164 as stored in the o:idmap element. The contents of this element have
165 been stored in our member maBlockIds. Each block represents 1024 shape
166 identifiers, starting with identifier 1 for the block #0. This means,
167 block #0 represents the identifiers 1-1024, block #1 represents the
168 identifiers 1025-2048, and so on. The local shape index has to be
169 calculated according to all blocks registered for this drawing.
171 Example:
172 Registered for this drawing are blocks #1 and #3 (shape identifiers
173 1025-2048 and 3073-4096).
174 Shape identifier 1025 -> local shape index 1.
175 Shape identifier 1026 -> local shape index 2.
177 Shape identifier 2048 -> local shape index 1024.
178 Shape identifier 3073 -> local shape index 1025.
180 Shape identifier 4096 -> local shape index 2048.
183 // get block id from shape id and find its index in the list of used blocks
184 sal_Int32 nBlockId = (nShapeId - 1) / 1024;
185 BlockIdVector::iterator aIt = ::std::lower_bound( maBlockIds.begin(), maBlockIds.end(), nBlockId );
186 sal_Int32 nIndex = static_cast< sal_Int32 >( aIt - maBlockIds.begin() );
188 // block id not found in set -> register it now (value of nIndex remains valid)
189 if( (aIt == maBlockIds.end()) || (*aIt != nBlockId) )
190 maBlockIds.insert( aIt, nBlockId );
192 // get one-based offset of shape id in its block
193 sal_Int32 nBlockOffset = (nShapeId - 1) % 1024 + 1;
195 // calculate the local shape index
196 return 1024 * nIndex + nBlockOffset;
199 const OleObjectInfo* Drawing::getOleObjectInfo( const OUString& rShapeId ) const
201 return ContainerHelper::getMapElement( maOleObjects, rShapeId );
204 const ControlInfo* Drawing::getControlInfo( const OUString& rShapeId ) const
206 return ContainerHelper::getMapElement( maControls, rShapeId );
209 Reference< XShape > Drawing::createAndInsertXShape( const OUString& rService,
210 const Reference< XShapes >& rxShapes, const awt::Rectangle& rShapeRect ) const
212 OSL_ENSURE( !rService.isEmpty(), "Drawing::createAndInsertXShape - missing UNO shape service name" );
213 OSL_ENSURE( rxShapes.is(), "Drawing::createAndInsertXShape - missing XShapes container" );
214 Reference< XShape > xShape;
215 if( !rService.isEmpty() && rxShapes.is() ) try
217 Reference< XMultiServiceFactory > xModelFactory( mrFilter.getModelFactory(), UNO_SET_THROW );
218 xShape.set( xModelFactory->createInstance( rService ), UNO_QUERY_THROW );
219 if ( !rService.equalsAscii( "com.sun.star.text.TextFrame" ) )
221 // insert shape into passed shape collection (maybe drawpage or group shape)
222 rxShapes->add( xShape );
223 xShape->setPosition( awt::Point( rShapeRect.X, rShapeRect.Y ) );
225 else
227 Reference< XPropertySet > xPropSet( xShape, UNO_QUERY_THROW );
228 xPropSet->setPropertyValue( "HoriOrient", makeAny( HoriOrientation::NONE ) );
229 xPropSet->setPropertyValue( "VertOrient", makeAny( VertOrientation::NONE ) );
230 xPropSet->setPropertyValue( "HoriOrientPosition", makeAny( rShapeRect.X ) );
231 xPropSet->setPropertyValue( "VertOrientPosition", makeAny( rShapeRect.Y ) );
232 xPropSet->setPropertyValue( "HoriOrientRelation", makeAny( RelOrientation::FRAME ) );
233 xPropSet->setPropertyValue( "VertOrientRelation", makeAny( RelOrientation::FRAME ) );
235 xShape->setSize( awt::Size( rShapeRect.Width, rShapeRect.Height ) );
237 catch( Exception& e )
239 SAL_WARN( "oox", "Drawing::createAndInsertXShape - error during shape object creation: " << e.Message );
241 OSL_ENSURE( xShape.is(), "Drawing::createAndInsertXShape - cannot instanciate shape object" );
242 return xShape;
245 Reference< XShape > Drawing::createAndInsertXControlShape( const ::oox::ole::EmbeddedControl& rControl,
246 const Reference< XShapes >& rxShapes, const awt::Rectangle& rShapeRect, sal_Int32& rnCtrlIndex ) const
248 Reference< XShape > xShape;
251 // create control model and insert it into the form of the draw page
252 Reference< XControlModel > xCtrlModel( getControlForm().convertAndInsert( rControl, rnCtrlIndex ), UNO_SET_THROW );
254 // create the control shape
255 xShape = createAndInsertXShape( "com.sun.star.drawing.ControlShape", rxShapes, rShapeRect );
257 // set the control model at the shape
258 Reference< XControlShape >( xShape, UNO_QUERY_THROW )->setControl( xCtrlModel );
260 catch (Exception const& e)
262 SAL_WARN("oox", "exception inserting Shape: " << e.Message);
264 return xShape;
267 bool Drawing::isShapeSupported( const ShapeBase& /*rShape*/ ) const
269 return true;
272 OUString Drawing::getShapeBaseName( const ShapeBase& /*rShape*/ ) const
274 return OUString();
277 bool Drawing::convertClientAnchor( awt::Rectangle& /*orShapeRect*/, const OUString& /*rShapeAnchor*/ ) const
279 return false;
282 Reference< XShape > Drawing::createAndInsertClientXShape( const ShapeBase& /*rShape*/,
283 const Reference< XShapes >& /*rxShapes*/, const awt::Rectangle& /*rShapeRect*/ ) const
285 return Reference< XShape >();
288 void Drawing::notifyXShapeInserted( const Reference< XShape >& /*rxShape*/,
289 const awt::Rectangle& /*rShapeRect*/, const ShapeBase& /*rShape*/, bool /*bGroupChild*/ )
293 // ============================================================================
295 } // namespace vml
296 } // namespave oox
298 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */