Bump version to 24.04.3.4
[LibreOffice.git] / oox / source / vml / vmldrawing.cxx
blobeaa0ecf1c1102626822e8ce90c2dc8ca4a34c67b
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/XDrawPage.hpp>
26 #include <com/sun/star/drawing/XShapes.hpp>
27 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
28 #include <com/sun/star/text/HoriOrientation.hpp>
29 #include <com/sun/star/text/RelOrientation.hpp>
30 #include <com/sun/star/text/VertOrientation.hpp>
31 #include <osl/diagnose.h>
32 #include <rtl/ustring.hxx>
33 #include <sal/log.hxx>
34 #include <oox/core/xmlfilterbase.hxx>
35 #include <oox/helper/containerhelper.hxx>
36 #include <oox/ole/axcontrol.hxx>
37 #include <oox/vml/vmlshape.hxx>
38 #include <oox/vml/vmlshapecontainer.hxx>
39 #include <comphelper/diagnose_ex.hxx>
40 #include <tools/gen.hxx>
41 #include <o3tl/string_view.hxx>
43 namespace oox::vml {
45 using namespace ::com::sun::star;
46 using namespace ::com::sun::star::awt;
47 using namespace ::com::sun::star::beans;
48 using namespace ::com::sun::star::drawing;
49 using namespace ::com::sun::star::lang;
50 using namespace ::com::sun::star::text;
51 using namespace ::com::sun::star::uno;
53 using ::oox::core::XmlFilterBase;
55 namespace {
57 /** Returns the textual representation of a numeric VML shape identifier. */
58 OUString lclGetShapeId( sal_Int32 nShapeId )
60 // identifier consists of a literal NUL character, a lowercase 's', and the id
61 static constexpr OUStringLiteral aStr = u"\0s";
62 return aStr + OUString::number( nShapeId );
65 /** Returns the numeric VML shape identifier from its textual representation. */
66 sal_Int32 lclGetShapeId( std::u16string_view rShapeId )
68 // identifier consists of a literal NUL character, a lowercase 's', and the id
69 return ((rShapeId.size() >= 3) && (rShapeId[ 0 ] == '\0') && (rShapeId[ 1 ] == 's')) ? o3tl::toInt32(rShapeId.substr( 2 )) : -1;
72 } // namespace
74 OleObjectInfo::OleObjectInfo( bool bDmlShape ) :
75 mbAutoLoad( false ),
76 mbDmlShape( bDmlShape )
80 void OleObjectInfo::setShapeId( sal_Int32 nShapeId )
82 maShapeId = lclGetShapeId( nShapeId );
85 ControlInfo::ControlInfo()
86 : mbTextContentShape(false)
90 void ControlInfo::setShapeId( sal_Int32 nShapeId )
92 maShapeId = lclGetShapeId(nShapeId);
95 Drawing::Drawing( XmlFilterBase& rFilter, const Reference< XDrawPage >& rxDrawPage, DrawingType eType ) :
96 mrFilter( rFilter ),
97 mxDrawPage( rxDrawPage ),
98 mxShapes( new ShapeContainer( *this ) ),
99 meType( eType )
101 OSL_ENSURE( mxDrawPage.is(), "Drawing::Drawing - missing UNO draw page" );
104 Drawing::~Drawing()
108 ::oox::ole::EmbeddedForm& Drawing::getControlForm() const
110 if (!mxCtrlForm)
111 mxCtrlForm.reset( new ::oox::ole::EmbeddedForm(
112 mrFilter.getModel(), mxDrawPage, mrFilter.getGraphicHelper() ) );
113 return *mxCtrlForm;
116 void Drawing::registerBlockId( sal_Int32 nBlockId )
118 OSL_ENSURE( nBlockId > 0, "Drawing::registerBlockId - invalid block index" );
119 if( nBlockId > 0 )
121 // lower_bound() returns iterator pointing to element equal to nBlockId, if existing
122 BlockIdVector::iterator aIt = ::std::lower_bound( maBlockIds.begin(), maBlockIds.end(), nBlockId );
123 if( (aIt == maBlockIds.end()) || (nBlockId != *aIt) )
124 maBlockIds.insert( aIt, nBlockId );
128 void Drawing::registerOleObject( const OleObjectInfo& rOleObject )
130 OSL_ENSURE( !rOleObject.maShapeId.isEmpty(), "Drawing::registerOleObject - missing OLE object shape id" );
131 OSL_ENSURE( maOleObjects.count( rOleObject.maShapeId ) == 0, "Drawing::registerOleObject - OLE object already registered" );
132 maOleObjects.emplace( rOleObject.maShapeId, rOleObject );
135 void Drawing::registerControl( const ControlInfo& rControl )
137 OSL_ENSURE( !rControl.maShapeId.isEmpty(), "Drawing::registerControl - missing form control shape id" );
138 OSL_ENSURE( !rControl.maName.isEmpty(), "Drawing::registerControl - missing form control name" );
139 OSL_ENSURE( maControls.count( rControl.maShapeId ) == 0, "Drawing::registerControl - form control already registered" );
140 maControls.emplace( rControl.maShapeId, rControl );
143 void Drawing::finalizeFragmentImport()
145 mxShapes->finalizeFragmentImport();
148 void Drawing::convertAndInsert() const
150 Reference< XShapes > xShapes( mxDrawPage );
151 mxShapes->convertAndInsert( xShapes );
153 // Group together form control radio buttons that are in the same groupBox
154 std::map<OUString, tools::Rectangle> GroupBoxMap;
155 std::map<Reference< XPropertySet >, tools::Rectangle> RadioButtonMap;
156 for ( sal_Int32 i = 0; i < xShapes->getCount(); ++i )
160 Reference< XControlShape > xCtrlShape( xShapes->getByIndex(i), UNO_QUERY );
161 if (!xCtrlShape.is())
162 continue;
163 Reference< XControlModel > xCtrlModel( xCtrlShape->getControl(), UNO_SET_THROW );
164 Reference< XServiceInfo > xModelSI (xCtrlModel, UNO_QUERY_THROW );
165 Reference< XPropertySet > aProps( xCtrlModel, UNO_QUERY_THROW );
167 OUString sName;
168 aProps->getPropertyValue("Name") >>= sName;
169 const ::Point aPoint( xCtrlShape->getPosition().X, xCtrlShape->getPosition().Y );
170 const ::Size aSize( xCtrlShape->getSize().Width, xCtrlShape->getSize().Height );
171 const tools::Rectangle aRect( aPoint, aSize );
172 if ( !sName.isEmpty()
173 && xModelSI->supportsService("com.sun.star.awt.UnoControlGroupBoxModel") )
175 GroupBoxMap[sName] = aRect;
177 else if ( xModelSI->supportsService("com.sun.star.awt.UnoControlRadioButtonModel") )
179 OUString sGroupName;
180 aProps->getPropertyValue("GroupName") >>= sGroupName;
181 // only Form Controls are affected by Group Boxes - see drawingfragment.cxx
182 if ( sGroupName == "autoGroup_formControl" )
183 RadioButtonMap[aProps] = aRect;
186 catch (uno::Exception&)
188 DBG_UNHANDLED_EXCEPTION("oox.vml");
191 for ( const auto& BoxItr : GroupBoxMap )
193 const uno::Any aGroup( "autoGroup_" + BoxItr.first );
194 for ( auto RadioItr = RadioButtonMap.begin(); RadioItr != RadioButtonMap.end(); )
196 if ( BoxItr.second.Contains(RadioItr->second) )
198 RadioItr->first->setPropertyValue("GroupName", aGroup );
199 // If conflict, first created GroupBox wins
200 RadioItr = RadioButtonMap.erase(RadioItr);
202 else
203 ++RadioItr;
209 sal_Int32 Drawing::getLocalShapeIndex( std::u16string_view rShapeId ) const
211 sal_Int32 nShapeId = lclGetShapeId( rShapeId );
212 if( nShapeId <= 0 ) return -1;
214 /* Shapes in a drawing are counted per registered shape identifier blocks
215 as stored in the o:idmap element. The contents of this element have
216 been stored in our member maBlockIds. Each block represents 1024 shape
217 identifiers, starting with identifier 1 for the block #0. This means,
218 block #0 represents the identifiers 1-1024, block #1 represents the
219 identifiers 1025-2048, and so on. The local shape index has to be
220 calculated according to all blocks registered for this drawing.
222 Example:
223 Registered for this drawing are blocks #1 and #3 (shape identifiers
224 1025-2048 and 3073-4096).
225 Shape identifier 1025 -> local shape index 1.
226 Shape identifier 1026 -> local shape index 2.
228 Shape identifier 2048 -> local shape index 1024.
229 Shape identifier 3073 -> local shape index 1025.
231 Shape identifier 4096 -> local shape index 2048.
234 // get block id from shape id and find its index in the list of used blocks
235 sal_Int32 nBlockId = (nShapeId - 1) / 1024;
236 BlockIdVector::iterator aIt = ::std::lower_bound( maBlockIds.begin(), maBlockIds.end(), nBlockId );
237 sal_Int32 nIndex = static_cast< sal_Int32 >( aIt - maBlockIds.begin() );
239 // block id not found in set -> register it now (value of nIndex remains valid)
240 if( (aIt == maBlockIds.end()) || (*aIt != nBlockId) )
241 maBlockIds.insert( aIt, nBlockId );
243 // get one-based offset of shape id in its block
244 sal_Int32 nBlockOffset = (nShapeId - 1) % 1024 + 1;
246 // calculate the local shape index
247 return 1024 * nIndex + nBlockOffset;
250 const OleObjectInfo* Drawing::getOleObjectInfo( const OUString& rShapeId ) const
252 return ContainerHelper::getMapElement( maOleObjects, rShapeId );
255 const ControlInfo* Drawing::getControlInfo( const OUString& rShapeId ) const
257 return ContainerHelper::getMapElement( maControls, rShapeId );
260 Reference< XShape > Drawing::createAndInsertXShape( const OUString& rService,
261 const Reference< XShapes >& rxShapes, const awt::Rectangle& rShapeRect ) const
263 OSL_ENSURE( !rService.isEmpty(), "Drawing::createAndInsertXShape - missing UNO shape service name" );
264 OSL_ENSURE( rxShapes.is(), "Drawing::createAndInsertXShape - missing XShapes container" );
265 Reference< XShape > xShape;
266 if( !rService.isEmpty() && rxShapes.is() ) try
268 Reference< XMultiServiceFactory > xModelFactory( mrFilter.getModelFactory(), UNO_SET_THROW );
269 xShape.set( xModelFactory->createInstance( rService ), UNO_QUERY_THROW );
270 if ( rService != "com.sun.star.text.TextFrame" )
272 // insert shape into passed shape collection (maybe drawpage or group shape)
273 rxShapes->add( xShape );
274 xShape->setPosition( awt::Point( rShapeRect.X, rShapeRect.Y ) );
276 else
278 Reference< XPropertySet > xPropSet( xShape, UNO_QUERY_THROW );
279 xPropSet->setPropertyValue( "HoriOrient", Any( HoriOrientation::NONE ) );
280 xPropSet->setPropertyValue( "VertOrient", Any( VertOrientation::NONE ) );
281 xPropSet->setPropertyValue( "HoriOrientPosition", Any( rShapeRect.X ) );
282 xPropSet->setPropertyValue( "VertOrientPosition", Any( rShapeRect.Y ) );
283 xPropSet->setPropertyValue( "HoriOrientRelation", Any( RelOrientation::FRAME ) );
284 xPropSet->setPropertyValue( "VertOrientRelation", Any( RelOrientation::FRAME ) );
286 xShape->setSize( awt::Size( rShapeRect.Width, rShapeRect.Height ) );
288 catch( const Exception& )
290 TOOLS_WARN_EXCEPTION( "oox", "Drawing::createAndInsertXShape - error during shape object creation" );
292 OSL_ENSURE( xShape.is(), "Drawing::createAndInsertXShape - cannot instantiate shape object" );
293 return xShape;
296 Reference< XShape > Drawing::createAndInsertXControlShape( const ::oox::ole::EmbeddedControl& rControl,
297 const Reference< XShapes >& rxShapes, const awt::Rectangle& rShapeRect, sal_Int32& rnCtrlIndex ) const
299 Reference< XShape > xShape;
302 // create control model and insert it into the form of the draw page
303 Reference< XControlModel > xCtrlModel( getControlForm().convertAndInsert( rControl, rnCtrlIndex ), UNO_SET_THROW );
305 // create the control shape
306 xShape = createAndInsertXShape( "com.sun.star.drawing.ControlShape", rxShapes, rShapeRect );
308 // set the control model at the shape
309 Reference< XControlShape >( xShape, UNO_QUERY_THROW )->setControl( xCtrlModel );
311 catch (Exception const&)
313 TOOLS_WARN_EXCEPTION("oox", "exception inserting Shape");
315 return xShape;
318 bool Drawing::isShapeSupported( const ShapeBase& /*rShape*/ ) const
320 return true;
323 OUString Drawing::getShapeBaseName( const ShapeBase& /*rShape*/ ) const
325 return OUString();
328 bool Drawing::convertClientAnchor( awt::Rectangle& /*orShapeRect*/, const OUString& /*rShapeAnchor*/ ) const
330 return false;
333 Reference< XShape > Drawing::createAndInsertClientXShape( const ShapeBase& /*rShape*/,
334 const Reference< XShapes >& /*rxShapes*/, const awt::Rectangle& /*rShapeRect*/ ) const
336 return Reference< XShape >();
339 void Drawing::notifyXShapeInserted( const Reference< XShape >& /*rxShape*/,
340 const awt::Rectangle& /*rShapeRect*/, const ShapeBase& /*rShape*/, bool /*bGroupChild*/ )
344 } // namespace oox::vml
346 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */