Branch data Line data Source code
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/XShapes.hpp>
26 : : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
27 : : #include <com/sun/star/text/HoriOrientation.hpp>
28 : : #include <com/sun/star/text/VertOrientation.hpp>
29 : : #include <rtl/oustringostreaminserter.hxx>
30 : : #include "oox/core/xmlfilterbase.hxx"
31 : : #include "oox/helper/containerhelper.hxx"
32 : : #include "oox/ole/axcontrol.hxx"
33 : : #include "oox/vml/vmlshape.hxx"
34 : : #include "oox/vml/vmlshapecontainer.hxx"
35 : :
36 : : namespace oox {
37 : : namespace vml {
38 : :
39 : : // ============================================================================
40 : :
41 : : using namespace ::com::sun::star::awt;
42 : : using namespace ::com::sun::star::beans;
43 : : using namespace ::com::sun::star::drawing;
44 : : using namespace ::com::sun::star::lang;
45 : : using namespace ::com::sun::star::text;
46 : : using namespace ::com::sun::star::uno;
47 : :
48 : : using ::oox::core::XmlFilterBase;
49 : : using ::rtl::OUString;
50 : :
51 : : // ============================================================================
52 : :
53 : : namespace {
54 : :
55 : : /** Returns the textual representation of a numeric VML shape identifier. */
56 : 0 : OUString lclGetShapeId( sal_Int32 nShapeId )
57 : : {
58 : : // identifier consists of a literal NUL character, a lowercase 's', and the id
59 [ # # ]: 0 : return CREATE_OUSTRING( "\0s" ) + OUString::valueOf( nShapeId );
60 : : }
61 : :
62 : : /** Returns the numeric VML shape identifier from its textual representation. */
63 : 3 : sal_Int32 lclGetShapeId( const OUString& rShapeId )
64 : : {
65 : : // identifier consists of a literal NUL character, a lowercase 's', and the id
66 [ + - ][ + - ]: 3 : return ((rShapeId.getLength() >= 3) && (rShapeId[ 0 ] == '\0') && (rShapeId[ 1 ] == 's')) ? rShapeId.copy( 2 ).toInt32() : -1;
[ + - ][ + - ]
67 : : }
68 : :
69 : : } // namespace
70 : :
71 : : // ============================================================================
72 : :
73 : 0 : OleObjectInfo::OleObjectInfo( bool bDmlShape ) :
74 : : mbAutoLoad( false ),
75 : 0 : mbDmlShape( bDmlShape )
76 : : {
77 : 0 : }
78 : :
79 : 0 : void OleObjectInfo::setShapeId( sal_Int32 nShapeId )
80 : : {
81 : 0 : maShapeId = lclGetShapeId( nShapeId );
82 : 0 : }
83 : :
84 : : // ============================================================================
85 : :
86 : 0 : ControlInfo::ControlInfo()
87 : : {
88 : 0 : }
89 : :
90 : 0 : void ControlInfo::setShapeId( sal_Int32 nShapeId )
91 : : {
92 : 0 : maShapeId = lclGetShapeId( nShapeId );
93 : 0 : }
94 : :
95 : : // ============================================================================
96 : :
97 : 117 : Drawing::Drawing( XmlFilterBase& rFilter, const Reference< XDrawPage >& rxDrawPage, DrawingType eType ) :
98 : : mrFilter( rFilter ),
99 : : mxDrawPage( rxDrawPage ),
100 [ + - ]: 117 : mxShapes( new ShapeContainer( *this ) ),
101 [ + - ][ + - ]: 234 : meType( eType )
[ + - ][ + - ]
102 : : {
103 : : OSL_ENSURE( mxDrawPage.is(), "Drawing::Drawing - missing UNO draw page" );
104 : 117 : }
105 : :
106 [ + - ][ + - ]: 117 : Drawing::~Drawing()
107 : : {
108 [ - + ]: 174 : }
109 : :
110 : 3 : ::oox::ole::EmbeddedForm& Drawing::getControlForm() const
111 : : {
112 [ + - ]: 3 : if( !mxCtrlForm.get() )
113 : : mxCtrlForm.reset( new ::oox::ole::EmbeddedForm(
114 [ + - ]: 3 : mrFilter.getModel(), mxDrawPage, mrFilter.getGraphicHelper() ) );
115 : 3 : return *mxCtrlForm;
116 : : }
117 : :
118 : 3 : void Drawing::registerBlockId( sal_Int32 nBlockId )
119 : : {
120 : : OSL_ENSURE( nBlockId > 0, "Drawing::registerBlockId - invalid block index" );
121 [ + - ]: 3 : if( nBlockId > 0 )
122 : : {
123 : : // lower_bound() returns iterator pointing to element equal to nBlockId, if existing
124 [ + - ]: 3 : BlockIdVector::iterator aIt = ::std::lower_bound( maBlockIds.begin(), maBlockIds.end(), nBlockId );
125 [ + - ][ - + ]: 3 : if( (aIt == maBlockIds.end()) || (nBlockId != *aIt) )
[ # # ][ # # ]
[ + - ]
[ + - # # ]
126 [ + - ]: 3 : maBlockIds.insert( aIt, nBlockId );
127 : : }
128 : 3 : }
129 : :
130 : 0 : void Drawing::registerOleObject( const OleObjectInfo& rOleObject )
131 : : {
132 : : OSL_ENSURE( !rOleObject.maShapeId.isEmpty(), "Drawing::registerOleObject - missing OLE object shape id" );
133 : : OSL_ENSURE( maOleObjects.count( rOleObject.maShapeId ) == 0, "Drawing::registerOleObject - OLE object already registered" );
134 [ # # ]: 0 : maOleObjects.insert( OleObjectInfoMap::value_type( rOleObject.maShapeId, rOleObject ) );
135 : 0 : }
136 : :
137 : 0 : void Drawing::registerControl( const ControlInfo& rControl )
138 : : {
139 : : OSL_ENSURE( !rControl.maShapeId.isEmpty(), "Drawing::registerControl - missing form control shape id" );
140 : : OSL_ENSURE( !rControl.maName.isEmpty(), "Drawing::registerControl - missing form control name" );
141 : : OSL_ENSURE( maControls.count( rControl.maShapeId ) == 0, "Drawing::registerControl - form control already registered" );
142 [ # # ]: 0 : maControls.insert( ControlInfoMap::value_type( rControl.maShapeId, rControl ) );
143 : 0 : }
144 : :
145 : 48 : void Drawing::finalizeFragmentImport()
146 : : {
147 : 48 : mxShapes->finalizeFragmentImport();
148 : 48 : }
149 : :
150 : 39 : void Drawing::convertAndInsert() const
151 : : {
152 [ + - ]: 39 : Reference< XShapes > xShapes( mxDrawPage, UNO_QUERY );
153 [ + - ]: 39 : mxShapes->convertAndInsert( xShapes );
154 : 39 : }
155 : :
156 : 3 : sal_Int32 Drawing::getLocalShapeIndex( const OUString& rShapeId ) const
157 : : {
158 : 3 : sal_Int32 nShapeId = lclGetShapeId( rShapeId );
159 [ - + ]: 3 : if( nShapeId <= 0 ) return -1;
160 : :
161 : : /* Shapes in a drawing are counted per registered shape identifier blocks
162 : : as stored in the o:idmap element. The contents of this element have
163 : : been stored in our member maBlockIds. Each block represents 1024 shape
164 : : identifiers, starting with identifier 1 for the block #0. This means,
165 : : block #0 represents the identifiers 1-1024, block #1 represents the
166 : : identifiers 1025-2048, and so on. The local shape index has to be
167 : : calculated according to all blocks registered for this drawing.
168 : :
169 : : Example:
170 : : Registered for this drawing are blocks #1 and #3 (shape identifiers
171 : : 1025-2048 and 3073-4096).
172 : : Shape identifier 1025 -> local shape index 1.
173 : : Shape identifier 1026 -> local shape index 2.
174 : : ...
175 : : Shape identifier 2048 -> local shape index 1024.
176 : : Shape identifier 3073 -> local shape index 1025.
177 : : ...
178 : : Shape identifier 4096 -> local shape index 2048.
179 : : */
180 : :
181 : : // get block id from shape id and find its index in the list of used blocks
182 : 3 : sal_Int32 nBlockId = (nShapeId - 1) / 1024;
183 [ + - ]: 3 : BlockIdVector::iterator aIt = ::std::lower_bound( maBlockIds.begin(), maBlockIds.end(), nBlockId );
184 [ + - ]: 3 : sal_Int32 nIndex = static_cast< sal_Int32 >( aIt - maBlockIds.begin() );
185 : :
186 : : // block id not found in set -> register it now (value of nIndex remains valid)
187 [ + - ][ + - ]: 3 : if( (aIt == maBlockIds.end()) || (*aIt != nBlockId) )
[ + - ][ - + ]
[ + - ]
[ - + # # ]
188 [ # # ]: 0 : maBlockIds.insert( aIt, nBlockId );
189 : :
190 : : // get one-based offset of shape id in its block
191 : 3 : sal_Int32 nBlockOffset = (nShapeId - 1) % 1024 + 1;
192 : :
193 : : // calculate the local shape index
194 : 3 : return 1024 * nIndex + nBlockOffset;
195 : : }
196 : :
197 : 33 : const OleObjectInfo* Drawing::getOleObjectInfo( const OUString& rShapeId ) const
198 : : {
199 : 33 : return ContainerHelper::getMapElement( maOleObjects, rShapeId );
200 : : }
201 : :
202 : 33 : const ControlInfo* Drawing::getControlInfo( const OUString& rShapeId ) const
203 : : {
204 : 33 : return ContainerHelper::getMapElement( maControls, rShapeId );
205 : : }
206 : :
207 : 54 : Reference< XShape > Drawing::createAndInsertXShape( const OUString& rService,
208 : : const Reference< XShapes >& rxShapes, const Rectangle& rShapeRect ) const
209 : : {
210 : : OSL_ENSURE( !rService.isEmpty(), "Drawing::createAndInsertXShape - missing UNO shape service name" );
211 : : OSL_ENSURE( rxShapes.is(), "Drawing::createAndInsertXShape - missing XShapes container" );
212 : 54 : Reference< XShape > xShape;
213 [ + - ][ + - ]: 54 : if( !rService.isEmpty() && rxShapes.is() ) try
[ + - ]
214 : : {
215 [ + - ][ + - ]: 54 : Reference< XMultiServiceFactory > xModelFactory( mrFilter.getModelFactory(), UNO_SET_THROW );
216 [ + - ][ + - ]: 54 : xShape.set( xModelFactory->createInstance( rService ), UNO_QUERY_THROW );
[ + - ]
217 [ + + ]: 54 : if ( !rService.equalsAscii( "com.sun.star.text.TextFrame" ) )
218 : : {
219 : : // insert shape into passed shape collection (maybe drawpage or group shape)
220 [ + - ][ + - ]: 30 : rxShapes->add( xShape );
221 [ + - ][ + - ]: 30 : xShape->setPosition( Point( rShapeRect.X, rShapeRect.Y ) );
222 : : }
223 : : else
224 : : {
225 [ + - ]: 24 : Reference< XPropertySet > xPropSet( xShape, UNO_QUERY_THROW );
226 [ + - ][ + - ]: 24 : xPropSet->setPropertyValue( OUString::createFromAscii( "HoriOrient" ), makeAny( HoriOrientation::NONE ) );
[ + - ]
227 [ + - ][ + - ]: 24 : xPropSet->setPropertyValue( OUString::createFromAscii( "VertOrient" ), makeAny( VertOrientation::NONE ) );
[ + - ]
228 [ + - ][ + - ]: 24 : xPropSet->setPropertyValue( OUString::createFromAscii( "HoriOrientPosition" ), makeAny( rShapeRect.X ) );
[ + - ]
229 [ + - ][ + - ]: 24 : xPropSet->setPropertyValue( OUString::createFromAscii( "VertOrientPosition" ), makeAny( rShapeRect.Y ) );
[ + - ]
230 : : }
231 [ + - ][ + - ]: 54 : xShape->setSize( Size( rShapeRect.Width, rShapeRect.Height ) );
[ # # ]
232 : : }
233 [ # # ]: 0 : catch( Exception& e )
234 : : {
235 : : SAL_WARN( "oox", "Drawing::createAndInsertXShape - error during shape object creation: " << e.Message );
236 : : }
237 : : OSL_ENSURE( xShape.is(), "Drawing::createAndInsertXShape - cannot instanciate shape object" );
238 : 54 : return xShape;
239 : : }
240 : :
241 : 3 : Reference< XShape > Drawing::createAndInsertXControlShape( const ::oox::ole::EmbeddedControl& rControl,
242 : : const Reference< XShapes >& rxShapes, const Rectangle& rShapeRect, sal_Int32& rnCtrlIndex ) const
243 : : {
244 : 3 : Reference< XShape > xShape;
245 : : try
246 : : {
247 : : // create control model and insert it into the form of the draw page
248 [ + - ][ + - ]: 3 : Reference< XControlModel > xCtrlModel( getControlForm().convertAndInsert( rControl, rnCtrlIndex ), UNO_SET_THROW );
[ + - ]
249 : :
250 : : // create the control shape
251 [ + - ][ + - ]: 3 : xShape = createAndInsertXShape( CREATE_OUSTRING( "com.sun.star.drawing.ControlShape" ), rxShapes, rShapeRect );
[ + - ]
252 : :
253 : : // set the control model at the shape
254 [ + - ][ + - ]: 3 : Reference< XControlShape >( xShape, UNO_QUERY_THROW )->setControl( xCtrlModel );
[ # # ][ + - ]
255 : : }
256 [ # # ]: 0 : catch( Exception& )
257 : : {
258 : : }
259 : 3 : return xShape;
260 : : }
261 : :
262 : 51 : bool Drawing::isShapeSupported( const ShapeBase& /*rShape*/ ) const
263 : : {
264 : 51 : return true;
265 : : }
266 : :
267 : 15 : OUString Drawing::getShapeBaseName( const ShapeBase& /*rShape*/ ) const
268 : : {
269 : 15 : return OUString();
270 : : }
271 : :
272 : 0 : bool Drawing::convertClientAnchor( Rectangle& /*orShapeRect*/, const OUString& /*rShapeAnchor*/ ) const
273 : : {
274 : 0 : return false;
275 : : }
276 : :
277 : 0 : Reference< XShape > Drawing::createAndInsertClientXShape( const ShapeBase& /*rShape*/,
278 : : const Reference< XShapes >& /*rxShapes*/, const Rectangle& /*rShapeRect*/ ) const
279 : : {
280 : 0 : return Reference< XShape >();
281 : : }
282 : :
283 : 45 : void Drawing::notifyXShapeInserted( const Reference< XShape >& /*rxShape*/,
284 : : const Rectangle& /*rShapeRect*/, const ShapeBase& /*rShape*/, bool /*bGroupChild*/ )
285 : : {
286 : 45 : }
287 : :
288 : : // ============================================================================
289 : :
290 : : } // namespace vml
291 [ + - ][ + - ]: 285 : } // namespave oox
292 : :
293 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|