Branch libreoffice-5-0-4
[LibreOffice.git] / oox / source / vml / vmldrawing.cxx
blob1a7d2e09637645b1a2d21a02bd5228185a97150a
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 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()
85 void ControlInfo::setShapeId( sal_Int32 nShapeId )
87 maShapeId = lclGetShapeId( nShapeId );
90 Drawing::Drawing( XmlFilterBase& rFilter, const Reference< XDrawPage >& rxDrawPage, DrawingType eType ) :
91 mrFilter( rFilter ),
92 mxDrawPage( rxDrawPage ),
93 mxShapes( new ShapeContainer( *this ) ),
94 meType( eType )
96 OSL_ENSURE( mxDrawPage.is(), "Drawing::Drawing - missing UNO draw page" );
99 Drawing::~Drawing()
103 ::oox::ole::EmbeddedForm& Drawing::getControlForm() const
105 if( !mxCtrlForm.get() )
106 mxCtrlForm.reset( new ::oox::ole::EmbeddedForm(
107 mrFilter.getModel(), mxDrawPage, mrFilter.getGraphicHelper() ) );
108 return *mxCtrlForm;
111 void Drawing::registerBlockId( sal_Int32 nBlockId )
113 OSL_ENSURE( nBlockId > 0, "Drawing::registerBlockId - invalid block index" );
114 if( nBlockId > 0 )
116 // lower_bound() returns iterator pointing to element equal to nBlockId, if existing
117 BlockIdVector::iterator aIt = ::std::lower_bound( maBlockIds.begin(), maBlockIds.end(), nBlockId );
118 if( (aIt == maBlockIds.end()) || (nBlockId != *aIt) )
119 maBlockIds.insert( aIt, nBlockId );
123 void Drawing::registerOleObject( const OleObjectInfo& rOleObject )
125 OSL_ENSURE( !rOleObject.maShapeId.isEmpty(), "Drawing::registerOleObject - missing OLE object shape id" );
126 OSL_ENSURE( maOleObjects.count( rOleObject.maShapeId ) == 0, "Drawing::registerOleObject - OLE object already registered" );
127 maOleObjects.insert( OleObjectInfoMap::value_type( rOleObject.maShapeId, rOleObject ) );
130 void Drawing::registerControl( const ControlInfo& rControl )
132 OSL_ENSURE( !rControl.maShapeId.isEmpty(), "Drawing::registerControl - missing form control shape id" );
133 OSL_ENSURE( !rControl.maName.isEmpty(), "Drawing::registerControl - missing form control name" );
134 OSL_ENSURE( maControls.count( rControl.maShapeId ) == 0, "Drawing::registerControl - form control already registered" );
135 maControls.insert( ControlInfoMap::value_type( rControl.maShapeId, rControl ) );
138 void Drawing::finalizeFragmentImport()
140 mxShapes->finalizeFragmentImport();
143 void Drawing::convertAndInsert() const
145 Reference< XShapes > xShapes( mxDrawPage, UNO_QUERY );
146 mxShapes->convertAndInsert( xShapes );
149 sal_Int32 Drawing::getLocalShapeIndex( const OUString& rShapeId ) const
151 sal_Int32 nShapeId = lclGetShapeId( rShapeId );
152 if( nShapeId <= 0 ) return -1;
154 /* Shapes in a drawing are counted per registered shape identifier blocks
155 as stored in the o:idmap element. The contents of this element have
156 been stored in our member maBlockIds. Each block represents 1024 shape
157 identifiers, starting with identifier 1 for the block #0. This means,
158 block #0 represents the identifiers 1-1024, block #1 represents the
159 identifiers 1025-2048, and so on. The local shape index has to be
160 calculated according to all blocks registered for this drawing.
162 Example:
163 Registered for this drawing are blocks #1 and #3 (shape identifiers
164 1025-2048 and 3073-4096).
165 Shape identifier 1025 -> local shape index 1.
166 Shape identifier 1026 -> local shape index 2.
168 Shape identifier 2048 -> local shape index 1024.
169 Shape identifier 3073 -> local shape index 1025.
171 Shape identifier 4096 -> local shape index 2048.
174 // get block id from shape id and find its index in the list of used blocks
175 sal_Int32 nBlockId = (nShapeId - 1) / 1024;
176 BlockIdVector::iterator aIt = ::std::lower_bound( maBlockIds.begin(), maBlockIds.end(), nBlockId );
177 sal_Int32 nIndex = static_cast< sal_Int32 >( aIt - maBlockIds.begin() );
179 // block id not found in set -> register it now (value of nIndex remains valid)
180 if( (aIt == maBlockIds.end()) || (*aIt != nBlockId) )
181 maBlockIds.insert( aIt, nBlockId );
183 // get one-based offset of shape id in its block
184 sal_Int32 nBlockOffset = (nShapeId - 1) % 1024 + 1;
186 // calculate the local shape index
187 return 1024 * nIndex + nBlockOffset;
190 const OleObjectInfo* Drawing::getOleObjectInfo( const OUString& rShapeId ) const
192 return ContainerHelper::getMapElement( maOleObjects, rShapeId );
195 const ControlInfo* Drawing::getControlInfo( const OUString& rShapeId ) const
197 return ContainerHelper::getMapElement( maControls, rShapeId );
200 Reference< XShape > Drawing::createAndInsertXShape( const OUString& rService,
201 const Reference< XShapes >& rxShapes, const awt::Rectangle& rShapeRect ) const
203 OSL_ENSURE( !rService.isEmpty(), "Drawing::createAndInsertXShape - missing UNO shape service name" );
204 OSL_ENSURE( rxShapes.is(), "Drawing::createAndInsertXShape - missing XShapes container" );
205 Reference< XShape > xShape;
206 if( !rService.isEmpty() && rxShapes.is() ) try
208 Reference< XMultiServiceFactory > xModelFactory( mrFilter.getModelFactory(), UNO_SET_THROW );
209 xShape.set( xModelFactory->createInstance( rService ), UNO_QUERY_THROW );
210 if ( rService != "com.sun.star.text.TextFrame" )
212 // insert shape into passed shape collection (maybe drawpage or group shape)
213 rxShapes->add( xShape );
214 xShape->setPosition( awt::Point( rShapeRect.X, rShapeRect.Y ) );
216 else
218 Reference< XPropertySet > xPropSet( xShape, UNO_QUERY_THROW );
219 xPropSet->setPropertyValue( "HoriOrient", makeAny( HoriOrientation::NONE ) );
220 xPropSet->setPropertyValue( "VertOrient", makeAny( VertOrientation::NONE ) );
221 xPropSet->setPropertyValue( "HoriOrientPosition", makeAny( rShapeRect.X ) );
222 xPropSet->setPropertyValue( "VertOrientPosition", makeAny( rShapeRect.Y ) );
223 xPropSet->setPropertyValue( "HoriOrientRelation", makeAny( RelOrientation::FRAME ) );
224 xPropSet->setPropertyValue( "VertOrientRelation", makeAny( RelOrientation::FRAME ) );
226 xShape->setSize( awt::Size( rShapeRect.Width, rShapeRect.Height ) );
228 catch( Exception& e )
230 SAL_WARN( "oox", "Drawing::createAndInsertXShape - error during shape object creation: " << e.Message );
232 OSL_ENSURE( xShape.is(), "Drawing::createAndInsertXShape - cannot instanciate shape object" );
233 return xShape;
236 Reference< XShape > Drawing::createAndInsertXControlShape( const ::oox::ole::EmbeddedControl& rControl,
237 const Reference< XShapes >& rxShapes, const awt::Rectangle& rShapeRect, sal_Int32& rnCtrlIndex ) const
239 Reference< XShape > xShape;
242 // create control model and insert it into the form of the draw page
243 Reference< XControlModel > xCtrlModel( getControlForm().convertAndInsert( rControl, rnCtrlIndex ), UNO_SET_THROW );
245 // create the control shape
246 xShape = createAndInsertXShape( "com.sun.star.drawing.ControlShape", rxShapes, rShapeRect );
248 // set the control model at the shape
249 Reference< XControlShape >( xShape, UNO_QUERY_THROW )->setControl( xCtrlModel );
251 catch (Exception const& e)
253 SAL_WARN("oox", "exception inserting Shape: " << e.Message);
255 return xShape;
258 bool Drawing::isShapeSupported( const ShapeBase& /*rShape*/ ) const
260 return true;
263 OUString Drawing::getShapeBaseName( const ShapeBase& /*rShape*/ ) const
265 return OUString();
268 bool Drawing::convertClientAnchor( awt::Rectangle& /*orShapeRect*/, const OUString& /*rShapeAnchor*/ ) const
270 return false;
273 Reference< XShape > Drawing::createAndInsertClientXShape( const ShapeBase& /*rShape*/,
274 const Reference< XShapes >& /*rxShapes*/, const awt::Rectangle& /*rShapeRect*/ ) const
276 return Reference< XShape >();
279 void Drawing::notifyXShapeInserted( const Reference< XShape >& /*rxShape*/,
280 const awt::Rectangle& /*rShapeRect*/, const ShapeBase& /*rShape*/, bool /*bGroupChild*/ )
284 } // namespace vml
285 } // namespave oox
287 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */