1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #include "oox/vml/vmlshapecontainer.hxx"
25 
26 #include "oox/vml/vmldrawing.hxx"
27 #include "oox/vml/vmlshape.hxx"
28 
29 namespace oox {
30 namespace vml {
31 
32 // ============================================================================
33 
34 using namespace ::com::sun::star::awt;
35 using namespace ::com::sun::star::drawing;
36 using namespace ::com::sun::star::uno;
37 
38 using ::rtl::OUString;
39 
40 // ============================================================================
41 
42 namespace {
43 
44 template< typename ShapeType >
lclMapShapesById(RefMap<OUString,ShapeType> & orMap,const RefVector<ShapeType> & rVector)45 void lclMapShapesById( RefMap< OUString, ShapeType >& orMap, const RefVector< ShapeType >& rVector )
46 {
47     for( typename RefVector< ShapeType >::const_iterator aIt = rVector.begin(), aEnd = rVector.end(); aIt != aEnd; ++aIt )
48     {
49         const OUString& rShapeId = (*aIt)->getShapeId();
50         OSL_ENSURE( rShapeId.getLength() > 0, "lclMapShapesById - missing shape identifier" );
51         if( rShapeId.getLength() > 0 )
52         {
53             OSL_ENSURE( orMap.find( rShapeId ) == orMap.end(), "lclMapShapesById - shape identifier already used" );
54             orMap[ rShapeId ] = *aIt;
55         }
56     }
57 }
58 
59 } // namespace
60 
61 // ============================================================================
62 
ShapeContainer(Drawing & rDrawing)63 ShapeContainer::ShapeContainer( Drawing& rDrawing ) :
64     mrDrawing( rDrawing )
65 {
66 }
67 
~ShapeContainer()68 ShapeContainer::~ShapeContainer()
69 {
70 }
71 
createShapeType()72 ShapeType& ShapeContainer::createShapeType()
73 {
74     ::boost::shared_ptr< ShapeType > xShape( new ShapeType( mrDrawing ) );
75     maTypes.push_back( xShape );
76     return *xShape;
77 }
78 
finalizeFragmentImport()79 void ShapeContainer::finalizeFragmentImport()
80 {
81     // map all shape templates by shape identifier
82     lclMapShapesById( maTypesById, maTypes );
83     // map all shapes by shape identifier
84     lclMapShapesById( maShapesById, maShapes );
85     /*  process all shapes (map all children templates/shapes in group shapes,
86         resolve template references in all shapes) */
87     maShapes.forEachMem( &ShapeBase::finalizeFragmentImport );
88 }
89 
getShapeTypeById(const OUString & rShapeId,bool bDeep) const90 const ShapeType* ShapeContainer::getShapeTypeById( const OUString& rShapeId, bool bDeep ) const
91 {
92     // search in own shape template list
93     if( const ShapeType* pType = maTypesById.get( rShapeId ).get() )
94         return pType;
95     // search deep in child shapes
96     if( bDeep )
97         for( ShapeVector::const_iterator aVIt = maShapes.begin(), aVEnd = maShapes.end(); aVIt != aVEnd; ++aVIt )
98             if( const ShapeType* pType = (*aVIt)->getChildTypeById( rShapeId ) )
99                 return pType;
100    return 0;
101 }
102 
getShapeById(const OUString & rShapeId,bool bDeep) const103 const ShapeBase* ShapeContainer::getShapeById( const OUString& rShapeId, bool bDeep ) const
104 {
105     // search in own shape list
106     if( const ShapeBase* pShape = maShapesById.get( rShapeId ).get() )
107         return pShape;
108     // search deep in child shapes
109     if( bDeep )
110         for( ShapeVector::const_iterator aVIt = maShapes.begin(), aVEnd = maShapes.end(); aVIt != aVEnd; ++aVIt )
111             if( const ShapeBase* pShape = (*aVIt)->getChildById( rShapeId ) )
112                 return pShape;
113    return 0;
114 }
115 
getFirstShape() const116 const ShapeBase* ShapeContainer::getFirstShape() const
117 {
118     OSL_ENSURE( mrDrawing.getType() == VMLDRAWING_WORD, "ShapeContainer::getFirstShape - illegal call, Word filter only" );
119     OSL_ENSURE( maShapes.size() == 1, "ShapeContainer::getFirstShape - single shape expected" );
120     return maShapes.get( 0 ).get();
121 }
122 
convertAndInsert(const Reference<XShapes> & rxShapes,const ShapeParentAnchor * pParentAnchor) const123 void ShapeContainer::convertAndInsert( const Reference< XShapes >& rxShapes, const ShapeParentAnchor* pParentAnchor ) const
124 {
125     for( ShapeVector::const_iterator aIt = maShapes.begin(), aEnd = maShapes.end(); aIt != aEnd; ++aIt )
126         (*aIt)->convertAndInsert( rxShapes, pParentAnchor );
127 }
128 
129 // ============================================================================
130 
131 } // namespace vml
132 } // namespace oox
133