bump product version to 6.3.0.0.beta1
[LibreOffice.git] / oox / source / vml / vmldrawing.cxx
blob6fa26118f2466b2aefec7cc6decd3729aab7e5db
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 <sal/log.hxx>
33 #include <oox/core/xmlfilterbase.hxx>
34 #include <oox/helper/containerhelper.hxx>
35 #include <oox/ole/axcontrol.hxx>
36 #include <oox/vml/vmlshape.hxx>
37 #include <oox/vml/vmlshapecontainer.hxx>
38 #include <tools/diagnose_ex.h>
39 #include <tools/gen.hxx>
41 namespace oox {
42 namespace vml {
44 using namespace ::com::sun::star;
45 using namespace ::com::sun::star::awt;
46 using namespace ::com::sun::star::beans;
47 using namespace ::com::sun::star::drawing;
48 using namespace ::com::sun::star::lang;
49 using namespace ::com::sun::star::text;
50 using namespace ::com::sun::star::uno;
52 using ::oox::core::XmlFilterBase;
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 const 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 OleObjectInfo::OleObjectInfo( bool bDmlShape ) :
74 mbAutoLoad( false ),
75 mbDmlShape( bDmlShape )
79 void OleObjectInfo::setShapeId( sal_Int32 nShapeId )
81 maShapeId = lclGetShapeId( nShapeId );
84 ControlInfo::ControlInfo()
85 : mbTextContentShape(false)
89 void ControlInfo::setShapeId( sal_Int32 nShapeId )
91 maShapeId = lclGetShapeId(nShapeId);
94 Drawing::Drawing( XmlFilterBase& rFilter, const Reference< XDrawPage >& rxDrawPage, DrawingType eType ) :
95 mrFilter( rFilter ),
96 mxDrawPage( rxDrawPage ),
97 mxShapes( new ShapeContainer( *this ) ),
98 meType( eType )
100 OSL_ENSURE( mxDrawPage.is(), "Drawing::Drawing - missing UNO draw page" );
103 Drawing::~Drawing()
107 ::oox::ole::EmbeddedForm& Drawing::getControlForm() const
109 if (!mxCtrlForm)
110 mxCtrlForm.reset( new ::oox::ole::EmbeddedForm(
111 mrFilter.getModel(), mxDrawPage, mrFilter.getGraphicHelper() ) );
112 return *mxCtrlForm;
115 void Drawing::registerBlockId( sal_Int32 nBlockId )
117 OSL_ENSURE( nBlockId > 0, "Drawing::registerBlockId - invalid block index" );
118 if( nBlockId > 0 )
120 // lower_bound() returns iterator pointing to element equal to nBlockId, if existing
121 BlockIdVector::iterator aIt = ::std::lower_bound( maBlockIds.begin(), maBlockIds.end(), nBlockId );
122 if( (aIt == maBlockIds.end()) || (nBlockId != *aIt) )
123 maBlockIds.insert( aIt, nBlockId );
127 void Drawing::registerOleObject( const OleObjectInfo& rOleObject )
129 OSL_ENSURE( !rOleObject.maShapeId.isEmpty(), "Drawing::registerOleObject - missing OLE object shape id" );
130 OSL_ENSURE( maOleObjects.count( rOleObject.maShapeId ) == 0, "Drawing::registerOleObject - OLE object already registered" );
131 maOleObjects.emplace( rOleObject.maShapeId, rOleObject );
134 void Drawing::registerControl( const ControlInfo& rControl )
136 OSL_ENSURE( !rControl.maShapeId.isEmpty(), "Drawing::registerControl - missing form control shape id" );
137 OSL_ENSURE( !rControl.maName.isEmpty(), "Drawing::registerControl - missing form control name" );
138 OSL_ENSURE( maControls.count( rControl.maShapeId ) == 0, "Drawing::registerControl - form control already registered" );
139 maControls.emplace( rControl.maShapeId, rControl );
142 void Drawing::finalizeFragmentImport()
144 mxShapes->finalizeFragmentImport();
147 void Drawing::convertAndInsert() const
149 Reference< XShapes > xShapes( mxDrawPage, UNO_QUERY );
150 mxShapes->convertAndInsert( xShapes );
152 // Group together form control radio buttons that are in the same groupBox
153 std::map<OUString, tools::Rectangle> GroupBoxMap;
154 std::map<Reference< XPropertySet >, tools::Rectangle> RadioButtonMap;
155 for ( sal_Int32 i = 0; i < xShapes->getCount(); ++i )
159 Reference< XControlShape > xCtrlShape( xShapes->getByIndex(i), UNO_QUERY_THROW );
160 Reference< XControlModel > xCtrlModel( xCtrlShape->getControl(), UNO_SET_THROW );
161 Reference< XServiceInfo > xModelSI (xCtrlModel, UNO_QUERY_THROW );
162 Reference< XPropertySet > aProps( xCtrlModel, UNO_QUERY_THROW );
164 OUString sName;
165 aProps->getPropertyValue("Name") >>= sName;
166 const ::Point aPoint( xCtrlShape->getPosition().X, xCtrlShape->getPosition().Y );
167 const ::Size aSize( xCtrlShape->getSize().Width, xCtrlShape->getSize().Height );
168 const tools::Rectangle aRect( aPoint, aSize );
169 if ( !sName.isEmpty()
170 && xModelSI->supportsService("com.sun.star.awt.UnoControlGroupBoxModel") )
172 GroupBoxMap[sName] = aRect;
174 else if ( xModelSI->supportsService("com.sun.star.awt.UnoControlRadioButtonModel") )
176 OUString sGroupName;
177 aProps->getPropertyValue("GroupName") >>= sGroupName;
178 // only Form Controls are affected by Group Boxes - see drawingfragment.cxx
179 if ( sGroupName == "autoGroup_formControl" )
180 RadioButtonMap[aProps] = aRect;
183 catch (uno::Exception&)
185 DBG_UNHANDLED_EXCEPTION("oox.vml");
188 for ( auto& BoxItr : GroupBoxMap )
190 const uno::Any aGroup( OUString("autoGroup_").concat(BoxItr.first) );
191 for ( auto RadioItr = RadioButtonMap.begin(); RadioItr != RadioButtonMap.end(); )
193 if ( BoxItr.second.IsInside(RadioItr->second) )
195 RadioItr->first->setPropertyValue("GroupName", aGroup );
196 // If conflict, first created GroupBox wins
197 RadioButtonMap.erase( RadioItr++ );
199 else
200 RadioItr++;
206 sal_Int32 Drawing::getLocalShapeIndex( const OUString& rShapeId ) const
208 sal_Int32 nShapeId = lclGetShapeId( rShapeId );
209 if( nShapeId <= 0 ) return -1;
211 /* Shapes in a drawing are counted per registered shape identifier blocks
212 as stored in the o:idmap element. The contents of this element have
213 been stored in our member maBlockIds. Each block represents 1024 shape
214 identifiers, starting with identifier 1 for the block #0. This means,
215 block #0 represents the identifiers 1-1024, block #1 represents the
216 identifiers 1025-2048, and so on. The local shape index has to be
217 calculated according to all blocks registered for this drawing.
219 Example:
220 Registered for this drawing are blocks #1 and #3 (shape identifiers
221 1025-2048 and 3073-4096).
222 Shape identifier 1025 -> local shape index 1.
223 Shape identifier 1026 -> local shape index 2.
225 Shape identifier 2048 -> local shape index 1024.
226 Shape identifier 3073 -> local shape index 1025.
228 Shape identifier 4096 -> local shape index 2048.
231 // get block id from shape id and find its index in the list of used blocks
232 sal_Int32 nBlockId = (nShapeId - 1) / 1024;
233 BlockIdVector::iterator aIt = ::std::lower_bound( maBlockIds.begin(), maBlockIds.end(), nBlockId );
234 sal_Int32 nIndex = static_cast< sal_Int32 >( aIt - maBlockIds.begin() );
236 // block id not found in set -> register it now (value of nIndex remains valid)
237 if( (aIt == maBlockIds.end()) || (*aIt != nBlockId) )
238 maBlockIds.insert( aIt, nBlockId );
240 // get one-based offset of shape id in its block
241 sal_Int32 nBlockOffset = (nShapeId - 1) % 1024 + 1;
243 // calculate the local shape index
244 return 1024 * nIndex + nBlockOffset;
247 const OleObjectInfo* Drawing::getOleObjectInfo( const OUString& rShapeId ) const
249 return ContainerHelper::getMapElement( maOleObjects, rShapeId );
252 const ControlInfo* Drawing::getControlInfo( const OUString& rShapeId ) const
254 return ContainerHelper::getMapElement( maControls, rShapeId );
257 Reference< XShape > Drawing::createAndInsertXShape( const OUString& rService,
258 const Reference< XShapes >& rxShapes, const awt::Rectangle& rShapeRect ) const
260 OSL_ENSURE( !rService.isEmpty(), "Drawing::createAndInsertXShape - missing UNO shape service name" );
261 OSL_ENSURE( rxShapes.is(), "Drawing::createAndInsertXShape - missing XShapes container" );
262 Reference< XShape > xShape;
263 if( !rService.isEmpty() && rxShapes.is() ) try
265 Reference< XMultiServiceFactory > xModelFactory( mrFilter.getModelFactory(), UNO_SET_THROW );
266 xShape.set( xModelFactory->createInstance( rService ), UNO_QUERY_THROW );
267 if ( rService != "com.sun.star.text.TextFrame" )
269 // insert shape into passed shape collection (maybe drawpage or group shape)
270 rxShapes->add( xShape );
271 xShape->setPosition( awt::Point( rShapeRect.X, rShapeRect.Y ) );
273 else
275 Reference< XPropertySet > xPropSet( xShape, UNO_QUERY_THROW );
276 xPropSet->setPropertyValue( "HoriOrient", makeAny( HoriOrientation::NONE ) );
277 xPropSet->setPropertyValue( "VertOrient", makeAny( VertOrientation::NONE ) );
278 xPropSet->setPropertyValue( "HoriOrientPosition", makeAny( rShapeRect.X ) );
279 xPropSet->setPropertyValue( "VertOrientPosition", makeAny( rShapeRect.Y ) );
280 xPropSet->setPropertyValue( "HoriOrientRelation", makeAny( RelOrientation::FRAME ) );
281 xPropSet->setPropertyValue( "VertOrientRelation", makeAny( RelOrientation::FRAME ) );
283 xShape->setSize( awt::Size( rShapeRect.Width, rShapeRect.Height ) );
285 catch( Exception& e )
287 SAL_WARN( "oox", "Drawing::createAndInsertXShape - error during shape object creation: " << e );
289 OSL_ENSURE( xShape.is(), "Drawing::createAndInsertXShape - cannot instantiate shape object" );
290 return xShape;
293 Reference< XShape > Drawing::createAndInsertXControlShape( const ::oox::ole::EmbeddedControl& rControl,
294 const Reference< XShapes >& rxShapes, const awt::Rectangle& rShapeRect, sal_Int32& rnCtrlIndex ) const
296 Reference< XShape > xShape;
299 // create control model and insert it into the form of the draw page
300 Reference< XControlModel > xCtrlModel( getControlForm().convertAndInsert( rControl, rnCtrlIndex ), UNO_SET_THROW );
302 // create the control shape
303 xShape = createAndInsertXShape( "com.sun.star.drawing.ControlShape", rxShapes, rShapeRect );
305 // set the control model at the shape
306 Reference< XControlShape >( xShape, UNO_QUERY_THROW )->setControl( xCtrlModel );
308 catch (Exception const&)
310 css::uno::Any ex( cppu::getCaughtException() );
311 SAL_WARN("oox", "exception inserting Shape: " << exceptionToString(ex));
313 return xShape;
316 bool Drawing::isShapeSupported( const ShapeBase& /*rShape*/ ) const
318 return true;
321 OUString Drawing::getShapeBaseName( const ShapeBase& /*rShape*/ ) const
323 return OUString();
326 bool Drawing::convertClientAnchor( awt::Rectangle& /*orShapeRect*/, const OUString& /*rShapeAnchor*/ ) const
328 return false;
331 Reference< XShape > Drawing::createAndInsertClientXShape( const ShapeBase& /*rShape*/,
332 const Reference< XShapes >& /*rxShapes*/, const awt::Rectangle& /*rShapeRect*/ ) const
334 return Reference< XShape >();
337 void Drawing::notifyXShapeInserted( const Reference< XShape >& /*rxShape*/,
338 const awt::Rectangle& /*rShapeRect*/, const ShapeBase& /*rShape*/, bool /*bGroupChild*/ )
342 } // namespace vml
343 } // namespave oox
345 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */