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