LibreOffice Module oox (master)  1
vmldrawing.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
20 #include <oox/vml/vmldrawing.hxx>
21 
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>
36 #include <oox/ole/axcontrol.hxx>
37 #include <oox/vml/vmlshape.hxx>
39 #include <tools/diagnose_ex.h>
40 #include <tools/gen.hxx>
41 
42 namespace oox::vml {
43 
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;
51 
52 using ::oox::core::XmlFilterBase;
53 
54 namespace {
55 
57 OUString lclGetShapeId( sal_Int32 nShapeId )
58 {
59  // identifier consists of a literal NUL character, a lowercase 's', and the id
60  static constexpr OUStringLiteral aStr = u"\0s";
61  return aStr + OUString::number( nShapeId );
62 }
63 
65 sal_Int32 lclGetShapeId( const OUString& rShapeId )
66 {
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;
69 }
70 
71 } // namespace
72 
73 OleObjectInfo::OleObjectInfo( bool bDmlShape ) :
74  mbAutoLoad( false ),
75  mbDmlShape( bDmlShape )
76 {
77 }
78 
79 void OleObjectInfo::setShapeId( sal_Int32 nShapeId )
80 {
81  maShapeId = lclGetShapeId( nShapeId );
82 }
83 
85  : mbTextContentShape(false)
86 {
87 }
88 
89 void ControlInfo::setShapeId( sal_Int32 nShapeId )
90 {
91  maShapeId = lclGetShapeId(nShapeId);
92 }
93 
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 )
99 {
100  OSL_ENSURE( mxDrawPage.is(), "Drawing::Drawing - missing UNO draw page" );
101 }
102 
104 {
105 }
106 
108 {
109  if (!mxCtrlForm)
110  mxCtrlForm.reset( new ::oox::ole::EmbeddedForm(
112  return *mxCtrlForm;
113 }
114 
115 void Drawing::registerBlockId( sal_Int32 nBlockId )
116 {
117  OSL_ENSURE( nBlockId > 0, "Drawing::registerBlockId - invalid block index" );
118  if( nBlockId > 0 )
119  {
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 );
124  }
125 }
126 
127 void Drawing::registerOleObject( const OleObjectInfo& rOleObject )
128 {
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 );
132 }
133 
134 void Drawing::registerControl( const ControlInfo& rControl )
135 {
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 );
140 }
141 
143 {
144  mxShapes->finalizeFragmentImport();
145 }
146 
148 {
149  Reference< XShapes > xShapes( mxDrawPage );
150  mxShapes->convertAndInsert( xShapes );
151 
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 )
156  {
157  try
158  {
159  Reference< XControlShape > xCtrlShape( xShapes->getByIndex(i), UNO_QUERY );
160  if (!xCtrlShape.is())
161  continue;
162  Reference< XControlModel > xCtrlModel( xCtrlShape->getControl(), UNO_SET_THROW );
163  Reference< XServiceInfo > xModelSI (xCtrlModel, UNO_QUERY_THROW );
164  Reference< XPropertySet > aProps( xCtrlModel, UNO_QUERY_THROW );
165 
166  OUString sName;
167  aProps->getPropertyValue("Name") >>= sName;
168  const ::Point aPoint( xCtrlShape->getPosition().X, xCtrlShape->getPosition().Y );
169  const ::Size aSize( xCtrlShape->getSize().Width, xCtrlShape->getSize().Height );
170  const tools::Rectangle aRect( aPoint, aSize );
171  if ( !sName.isEmpty()
172  && xModelSI->supportsService("com.sun.star.awt.UnoControlGroupBoxModel") )
173  {
174  GroupBoxMap[sName] = aRect;
175  }
176  else if ( xModelSI->supportsService("com.sun.star.awt.UnoControlRadioButtonModel") )
177  {
178  OUString sGroupName;
179  aProps->getPropertyValue("GroupName") >>= sGroupName;
180  // only Form Controls are affected by Group Boxes - see drawingfragment.cxx
181  if ( sGroupName == "autoGroup_formControl" )
182  RadioButtonMap[aProps] = aRect;
183  }
184  }
185  catch (uno::Exception&)
186  {
187  DBG_UNHANDLED_EXCEPTION("oox.vml");
188  }
189  }
190  for ( const auto& BoxItr : GroupBoxMap )
191  {
192  const uno::Any aGroup( "autoGroup_" + BoxItr.first );
193  for ( auto RadioItr = RadioButtonMap.begin(); RadioItr != RadioButtonMap.end(); )
194  {
195  if ( BoxItr.second.Contains(RadioItr->second) )
196  {
197  RadioItr->first->setPropertyValue("GroupName", aGroup );
198  // If conflict, first created GroupBox wins
199  RadioItr = RadioButtonMap.erase(RadioItr);
200  }
201  else
202  ++RadioItr;
203  }
204  }
205 
206 }
207 
208 sal_Int32 Drawing::getLocalShapeIndex( const OUString& rShapeId ) const
209 {
210  sal_Int32 nShapeId = lclGetShapeId( rShapeId );
211  if( nShapeId <= 0 ) return -1;
212 
213  /* Shapes in a drawing are counted per registered shape identifier blocks
214  as stored in the o:idmap element. The contents of this element have
215  been stored in our member maBlockIds. Each block represents 1024 shape
216  identifiers, starting with identifier 1 for the block #0. This means,
217  block #0 represents the identifiers 1-1024, block #1 represents the
218  identifiers 1025-2048, and so on. The local shape index has to be
219  calculated according to all blocks registered for this drawing.
220 
221  Example:
222  Registered for this drawing are blocks #1 and #3 (shape identifiers
223  1025-2048 and 3073-4096).
224  Shape identifier 1025 -> local shape index 1.
225  Shape identifier 1026 -> local shape index 2.
226  ...
227  Shape identifier 2048 -> local shape index 1024.
228  Shape identifier 3073 -> local shape index 1025.
229  ...
230  Shape identifier 4096 -> local shape index 2048.
231  */
232 
233  // get block id from shape id and find its index in the list of used blocks
234  sal_Int32 nBlockId = (nShapeId - 1) / 1024;
235  BlockIdVector::iterator aIt = ::std::lower_bound( maBlockIds.begin(), maBlockIds.end(), nBlockId );
236  sal_Int32 nIndex = static_cast< sal_Int32 >( aIt - maBlockIds.begin() );
237 
238  // block id not found in set -> register it now (value of nIndex remains valid)
239  if( (aIt == maBlockIds.end()) || (*aIt != nBlockId) )
240  maBlockIds.insert( aIt, nBlockId );
241 
242  // get one-based offset of shape id in its block
243  sal_Int32 nBlockOffset = (nShapeId - 1) % 1024 + 1;
244 
245  // calculate the local shape index
246  return 1024 * nIndex + nBlockOffset;
247 }
248 
249 const OleObjectInfo* Drawing::getOleObjectInfo( const OUString& rShapeId ) const
250 {
251  return ContainerHelper::getMapElement( maOleObjects, rShapeId );
252 }
253 
254 const ControlInfo* Drawing::getControlInfo( const OUString& rShapeId ) const
255 {
256  return ContainerHelper::getMapElement( maControls, rShapeId );
257 }
258 
259 Reference< XShape > Drawing::createAndInsertXShape( const OUString& rService,
260  const Reference< XShapes >& rxShapes, const awt::Rectangle& rShapeRect ) const
261 {
262  OSL_ENSURE( !rService.isEmpty(), "Drawing::createAndInsertXShape - missing UNO shape service name" );
263  OSL_ENSURE( rxShapes.is(), "Drawing::createAndInsertXShape - missing XShapes container" );
264  Reference< XShape > xShape;
265  if( !rService.isEmpty() && rxShapes.is() ) try
266  {
267  Reference< XMultiServiceFactory > xModelFactory( mrFilter.getModelFactory(), UNO_SET_THROW );
268  xShape.set( xModelFactory->createInstance( rService ), UNO_QUERY_THROW );
269  if ( rService != "com.sun.star.text.TextFrame" )
270  {
271  // insert shape into passed shape collection (maybe drawpage or group shape)
272  rxShapes->add( xShape );
273  xShape->setPosition( awt::Point( rShapeRect.X, rShapeRect.Y ) );
274  }
275  else
276  {
277  Reference< XPropertySet > xPropSet( xShape, UNO_QUERY_THROW );
278  xPropSet->setPropertyValue( "HoriOrient", makeAny( HoriOrientation::NONE ) );
279  xPropSet->setPropertyValue( "VertOrient", makeAny( VertOrientation::NONE ) );
280  xPropSet->setPropertyValue( "HoriOrientPosition", makeAny( rShapeRect.X ) );
281  xPropSet->setPropertyValue( "VertOrientPosition", makeAny( rShapeRect.Y ) );
282  xPropSet->setPropertyValue( "HoriOrientRelation", makeAny( RelOrientation::FRAME ) );
283  xPropSet->setPropertyValue( "VertOrientRelation", makeAny( RelOrientation::FRAME ) );
284  }
285  xShape->setSize( awt::Size( rShapeRect.Width, rShapeRect.Height ) );
286  }
287  catch( const Exception& )
288  {
289  TOOLS_WARN_EXCEPTION( "oox", "Drawing::createAndInsertXShape - error during shape object creation" );
290  }
291  OSL_ENSURE( xShape.is(), "Drawing::createAndInsertXShape - cannot instantiate shape object" );
292  return xShape;
293 }
294 
295 Reference< XShape > Drawing::createAndInsertXControlShape( const ::oox::ole::EmbeddedControl& rControl,
296  const Reference< XShapes >& rxShapes, const awt::Rectangle& rShapeRect, sal_Int32& rnCtrlIndex ) const
297 {
298  Reference< XShape > xShape;
299  try
300  {
301  // create control model and insert it into the form of the draw page
302  Reference< XControlModel > xCtrlModel( getControlForm().convertAndInsert( rControl, rnCtrlIndex ), UNO_SET_THROW );
303 
304  // create the control shape
305  xShape = createAndInsertXShape( "com.sun.star.drawing.ControlShape", rxShapes, rShapeRect );
306 
307  // set the control model at the shape
308  Reference< XControlShape >( xShape, UNO_QUERY_THROW )->setControl( xCtrlModel );
309  }
310  catch (Exception const&)
311  {
312  TOOLS_WARN_EXCEPTION("oox", "exception inserting Shape");
313  }
314  return xShape;
315 }
316 
317 bool Drawing::isShapeSupported( const ShapeBase& /*rShape*/ ) const
318 {
319  return true;
320 }
321 
322 OUString Drawing::getShapeBaseName( const ShapeBase& /*rShape*/ ) const
323 {
324  return OUString();
325 }
326 
327 bool Drawing::convertClientAnchor( awt::Rectangle& /*orShapeRect*/, const OUString& /*rShapeAnchor*/ ) const
328 {
329  return false;
330 }
331 
332 Reference< XShape > Drawing::createAndInsertClientXShape( const ShapeBase& /*rShape*/,
333  const Reference< XShapes >& /*rxShapes*/, const awt::Rectangle& /*rShapeRect*/ ) const
334 {
335  return Reference< XShape >();
336 }
337 
338 void Drawing::notifyXShapeInserted( const Reference< XShape >& /*rxShape*/,
339  const awt::Rectangle& /*rShapeRect*/, const ShapeBase& /*rShape*/, bool /*bGroupChild*/ )
340 {
341 }
342 
343 } // namespace oox::vml
344 
345 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
OUString maShapeId
Shape identifier for shape lookup.
Definition: vmldrawing.hxx:64
const OleObjectInfo * getOleObjectInfo(const OUString &rShapeId) const
Returns the registered info structure for an OLE object, if extant.
Definition: vmldrawing.cxx:249
const char * sName
Definition: olehelper.cxx:92
sal_Int32 nIndex
virtual bool isShapeSupported(const ShapeBase &rShape) const
Derived classes may disable conversion of specific shapes.
Definition: vmldrawing.cxx:317
A shape object that is part of a drawing.
Definition: vmlshape.hxx:234
A wrapper for a control form embedded directly in a draw page.
Definition: axcontrol.hxx:959
BlockIdVector maBlockIds
Block identifiers used by this drawing.
Definition: vmldrawing.hxx:194
void convertAndInsert() const
Creates and inserts all UNO shapes into the draw page.
Definition: vmldrawing.cxx:147
DrawingType
Enumerates different types of VML drawings.
Definition: vmldrawing.hxx:53
OleObjectInfoMap maOleObjects
Info about all embedded OLE objects, mapped by shape id.
Definition: vmldrawing.hxx:196
uno::Reference< drawing::XDrawPage > mxDrawPage
virtual css::uno::Reference< css::drawing::XShape > createAndInsertClientXShape(const ShapeBase &rShape, const css::uno::Reference< css::drawing::XShapes > &rxShapes, const css::awt::Rectangle &rShapeRect) const
Derived classes create a UNO shape according to the passed shape model.
Definition: vmldrawing.cxx:332
std::unique_ptr<::oox::ole::EmbeddedForm > mxCtrlForm
The control form used to process embedded controls.
Definition: vmldrawing.hxx:193
void registerOleObject(const OleObjectInfo &rOleObject)
Registers the passed embedded OLE object.
Definition: vmldrawing.cxx:127
css::uno::Reference< css::drawing::XDrawPage > mxDrawPage
UNO draw page used to insert the shapes.
Definition: vmldrawing.hxx:192
const ControlInfo * getControlInfo(const OUString &rShapeId) const
Returns the registered info structure for a form control, if extant.
Definition: vmldrawing.cxx:254
virtual void notifyXShapeInserted(const css::uno::Reference< css::drawing::XShape > &rxShape, const css::awt::Rectangle &rShapeRect, const ShapeBase &rShape, bool bGroupChild)
Derived classes may want to know that a UNO shape has been inserted.
Definition: vmldrawing.cxx:338
virtual OUString getShapeBaseName(const ShapeBase &rShape) const
Derived classes may return additional base names for automatic shape name creation.
Definition: vmldrawing.cxx:322
GraphicHelper & getGraphicHelper() const
Returns a helper for the handling of graphics and graphic objects.
Definition: filterbase.cxx:343
css::uno::Reference< css::drawing::XShape > createAndInsertXShape(const OUString &rService, const css::uno::Reference< css::drawing::XShapes > &rxShapes, const css::awt::Rectangle &rShapeRect) const
Creates a new UNO shape object, inserts it into the passed UNO shape container, and sets the shape po...
Definition: vmldrawing.cxx:259
::oox::ole::EmbeddedForm & getControlForm() const
Returns the form object used to process ActiveX form controls.
Definition: vmldrawing.cxx:107
Contains information about a form control embedded in a draw page.
Definition: vmldrawing.hxx:78
sal_Int32 getLocalShapeIndex(const OUString &rShapeId) const
Returns the local shape index from the passed global shape identifier.
Definition: vmldrawing.cxx:208
Container that holds a list of shapes and shape templates.
#define DBG_UNHANDLED_EXCEPTION(...)
void finalizeFragmentImport()
Final processing after import of the fragment.
Definition: vmldrawing.cxx:142
#define TOOLS_WARN_EXCEPTION(area, stream)
virtual ~Drawing()
Definition: vmldrawing.cxx:103
Contains information about an OLE object embedded in a draw page.
Definition: vmldrawing.hxx:62
float u
OUString maName
Programmatical name of the form control.
Definition: vmldrawing.hxx:82
PropType meType
Definition: dffdumper.cxx:161
ControlInfoMap maControls
Info about all embedded form controls, mapped by control name.
Definition: vmldrawing.hxx:197
void setShapeId(sal_Int32 nShapeId)
Sets the string representation of the passed numeric shape identifier.
Definition: vmldrawing.cxx:79
const css::uno::Reference< css::frame::XModel > & getModel() const
Returns the document model (always existing).
Definition: filterbase.cxx:217
const css::uno::Reference< css::lang::XMultiServiceFactory > & getModelFactory() const
Returns the service factory provided by the document model (always existing).
Definition: filterbase.cxx:222
static const MapType::mapped_type * getMapElement(const MapType &rMap, const typename MapType::key_type &rKey)
Returns the pointer to an existing element of the passed map, or a null pointer, if an element with t...
OUString maShapeId
Shape identifier for shape lookup.
Definition: vmldrawing.hxx:80
Drawing(::oox::core::XmlFilterBase &rFilter, const css::uno::Reference< css::drawing::XDrawPage > &rxDrawPage, DrawingType eType)
Definition: vmldrawing.cxx:94
css::uno::Reference< css::drawing::XShape > createAndInsertXControlShape(const ::oox::ole::EmbeddedControl &rControl, const css::uno::Reference< css::drawing::XShapes > &rxShapes, const css::awt::Rectangle &rShapeRect, sal_Int32 &rnCtrlIndex) const
Creates a new UNO shape object for a form control, inserts the control model into the form...
Definition: vmldrawing.cxx:295
::oox::core::XmlFilterBase & mrFilter
Filter object that imports/exports the VML drawing.
Definition: vmldrawing.hxx:190
void registerControl(const ControlInfo &rControl)
Registers the passed embedded form control.
Definition: vmldrawing.cxx:134
const PowerPointImport & mrFilter
Definition: pptimport.cxx:253
void registerBlockId(sal_Int32 nBlockId)
Registers a block of shape identifiers reserved by this drawing.
Definition: vmldrawing.cxx:115
std::unique_ptr< ShapeContainer > mxShapes
All shapes and shape templates.
Definition: vmldrawing.hxx:195
aStr
void setShapeId(sal_Int32 nShapeId)
Sets the string representation of the passed numeric shape identifier.
Definition: vmldrawing.cxx:89
virtual bool convertClientAnchor(css::awt::Rectangle &orShapeRect, const OUString &rShapeAnchor) const
Derived classes may calculate the shape rectangle from a non-standard anchor information string...
Definition: vmldrawing.cxx:327
css::uno::Any SAL_CALL makeAny(const SharedUNOComponent< INTERFACE, COMPONENT > &value)