Gtk-WARNING gtktreestore.c:1047: Invalid column number 1 added to iter
[LibreOffice.git] / oox / source / vml / vmldrawing.cxx
blob69b0557c173b89ff9dd6b234265e91e8d1027d13
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(u"Name"_ustr) >>= 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(u"com.sun.star.awt.UnoControlGroupBoxModel"_ustr) )
175 GroupBoxMap[sName] = aRect;
177 else if ( xModelSI->supportsService(u"com.sun.star.awt.UnoControlRadioButtonModel"_ustr) )
179 OUString sGroupName;
180 aProps->getPropertyValue(u"GroupName"_ustr) >>= 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(u"GroupName"_ustr, 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 sal_Int32 nRet;
248 if (o3tl::checked_add(1024 * nIndex, nBlockOffset, nRet))
250 SAL_WARN("oox", "getLocalShapeIndex: overflow on " << 1024 * nIndex << " + " << nBlockOffset);
251 nRet = -1;
253 return nRet;
256 const OleObjectInfo* Drawing::getOleObjectInfo( const OUString& rShapeId ) const
258 return ContainerHelper::getMapElement( maOleObjects, rShapeId );
261 const ControlInfo* Drawing::getControlInfo( const OUString& rShapeId ) const
263 return ContainerHelper::getMapElement( maControls, rShapeId );
266 Reference< XShape > Drawing::createAndInsertXShape( const OUString& rService,
267 const Reference< XShapes >& rxShapes, const awt::Rectangle& rShapeRect ) const
269 OSL_ENSURE( !rService.isEmpty(), "Drawing::createAndInsertXShape - missing UNO shape service name" );
270 OSL_ENSURE( rxShapes.is(), "Drawing::createAndInsertXShape - missing XShapes container" );
271 Reference< XShape > xShape;
272 if( !rService.isEmpty() && rxShapes.is() ) try
274 Reference< XMultiServiceFactory > xModelFactory( mrFilter.getModelFactory(), UNO_SET_THROW );
275 xShape.set( xModelFactory->createInstance( rService ), UNO_QUERY_THROW );
276 if ( rService != "com.sun.star.text.TextFrame" )
278 // insert shape into passed shape collection (maybe drawpage or group shape)
279 rxShapes->add( xShape );
280 xShape->setPosition( awt::Point( rShapeRect.X, rShapeRect.Y ) );
282 else
284 Reference< XPropertySet > xPropSet( xShape, UNO_QUERY_THROW );
285 xPropSet->setPropertyValue( u"HoriOrient"_ustr, Any( HoriOrientation::NONE ) );
286 xPropSet->setPropertyValue( u"VertOrient"_ustr, Any( VertOrientation::NONE ) );
287 xPropSet->setPropertyValue( u"HoriOrientPosition"_ustr, Any( rShapeRect.X ) );
288 xPropSet->setPropertyValue( u"VertOrientPosition"_ustr, Any( rShapeRect.Y ) );
289 xPropSet->setPropertyValue( u"HoriOrientRelation"_ustr, Any( RelOrientation::FRAME ) );
290 xPropSet->setPropertyValue( u"VertOrientRelation"_ustr, Any( RelOrientation::FRAME ) );
292 xShape->setSize( awt::Size( rShapeRect.Width, rShapeRect.Height ) );
294 catch( const Exception& )
296 TOOLS_WARN_EXCEPTION( "oox", "Drawing::createAndInsertXShape - error during shape object creation" );
298 OSL_ENSURE( xShape.is(), "Drawing::createAndInsertXShape - cannot instantiate shape object" );
299 return xShape;
302 Reference< XShape > Drawing::createAndInsertXControlShape( const ::oox::ole::EmbeddedControl& rControl,
303 const Reference< XShapes >& rxShapes, const awt::Rectangle& rShapeRect, sal_Int32& rnCtrlIndex ) const
305 Reference< XShape > xShape;
308 // create control model and insert it into the form of the draw page
309 Reference< XControlModel > xCtrlModel( getControlForm().convertAndInsert( rControl, rnCtrlIndex ), UNO_SET_THROW );
311 // create the control shape
312 xShape = createAndInsertXShape( u"com.sun.star.drawing.ControlShape"_ustr, rxShapes, rShapeRect );
314 // set the control model at the shape
315 Reference< XControlShape >( xShape, UNO_QUERY_THROW )->setControl( xCtrlModel );
317 catch (Exception const&)
319 TOOLS_WARN_EXCEPTION("oox", "exception inserting Shape");
321 return xShape;
324 bool Drawing::isShapeSupported( const ShapeBase& /*rShape*/ ) const
326 return true;
329 OUString Drawing::getShapeBaseName( const ShapeBase& /*rShape*/ ) const
331 return OUString();
334 bool Drawing::convertClientAnchor( awt::Rectangle& /*orShapeRect*/, const OUString& /*rShapeAnchor*/ ) const
336 return false;
339 Reference< XShape > Drawing::createAndInsertClientXShape( const ShapeBase& /*rShape*/,
340 const Reference< XShapes >& /*rxShapes*/, const awt::Rectangle& /*rShapeRect*/ ) const
342 return Reference< XShape >();
345 void Drawing::notifyXShapeInserted( const Reference< XShape >& /*rxShape*/,
346 const awt::Rectangle& /*rShapeRect*/, const ShapeBase& /*rShape*/, bool /*bGroupChild*/ )
350 } // namespace oox::vml
352 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */