1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #include "oox/drawingml/graphicshapecontext.hxx"
29 #include <osl/diagnose.h>
30 
31 #include "oox/drawingml/fillpropertiesgroupcontext.hxx"
32 #include "oox/drawingml/customshapeproperties.hxx"
33 #include "oox/drawingml/diagram/diagramfragmenthandler.hxx"
34 #include "oox/drawingml/table/tablecontext.hxx"
35 #include "oox/core/xmlfilterbase.hxx"
36 #include "oox/helper/attributelist.hxx"
37 #include "oox/helper/graphichelper.hxx"
38 #include "oox/helper/propertyset.hxx"
39 #include "oox/vml/vmldrawing.hxx"
40 #include "oox/vml/vmlshape.hxx"
41 #include "oox/vml/vmlshapecontainer.hxx"
42 #include "oox/drawingml/fillproperties.hxx"
43 #include "oox/drawingml/transform2dcontext.hxx"
44 
45 using ::rtl::OUString;
46 using namespace ::com::sun::star;
47 using namespace ::com::sun::star::io;
48 using namespace ::com::sun::star::uno;
49 using namespace ::com::sun::star::lang;
50 using namespace ::com::sun::star::beans;
51 using namespace ::com::sun::star::xml::sax;
52 using namespace ::oox::core;
53 
54 namespace oox {
55 namespace drawingml {
56 
57 // ============================================================================
58 // CT_Picture
59 
60 GraphicShapeContext::GraphicShapeContext( ContextHandler& rParent, ShapePtr pMasterShapePtr, ShapePtr pShapePtr )
61 : ShapeContext( rParent, pMasterShapePtr, pShapePtr )
62 {
63 }
64 
65 Reference< XFastContextHandler > GraphicShapeContext::createFastChildContext( sal_Int32 aElementToken, const Reference< XFastAttributeList >& xAttribs ) throw (SAXException, RuntimeException)
66 {
67 	Reference< XFastContextHandler > xRet;
68 
69     switch( getBaseToken( aElementToken ) )
70 	{
71 	// CT_ShapeProperties
72 	case XML_xfrm:
73         xRet.set( new Transform2DContext( *this, xAttribs, *mpShapePtr ) );
74 		break;
75 	case XML_blipFill:
76         xRet.set( new BlipFillContext( *this, xAttribs, mpShapePtr->getGraphicProperties().maBlipProps ) );
77 		break;
78 	}
79 
80     if ((getNamespace( aElementToken ) == NMSP_vml) && mpShapePtr)
81     {
82         mpShapePtr->setServiceName("com.sun.star.drawing.CustomShape");
83         CustomShapePropertiesPtr pCstmShpProps
84             (mpShapePtr->getCustomShapeProperties());
85 
86         sal_uInt32 nType = getBaseToken( aElementToken );
87         OUString sType(GetShapeType(nType));
88 
89         if (sType.getLength() > 0)
90             pCstmShpProps->setShapePresetType(sType);
91     }
92 
93 	if( !xRet.is() )
94 		xRet.set( ShapeContext::createFastChildContext( aElementToken, xAttribs ) );
95 
96 	return xRet;
97 }
98 
99 // ============================================================================
100 // CT_GraphicalObjectFrameContext
101 
102 GraphicalObjectFrameContext::GraphicalObjectFrameContext( ContextHandler& rParent, ShapePtr pMasterShapePtr, ShapePtr pShapePtr, bool bEmbedShapesInChart ) :
103     ShapeContext( rParent, pMasterShapePtr, pShapePtr ),
104     mbEmbedShapesInChart( bEmbedShapesInChart )
105 {
106 }
107 
108 Reference< XFastContextHandler > GraphicalObjectFrameContext::createFastChildContext( sal_Int32 aElementToken, const Reference< XFastAttributeList >& xAttribs ) throw (SAXException, RuntimeException)
109 {
110 	Reference< XFastContextHandler > xRet;
111 
112 	switch( getBaseToken( aElementToken ) )
113 	{
114 	// CT_ShapeProperties
115 	case XML_nvGraphicFramePr:		// CT_GraphicalObjectFrameNonVisual
116 		break;
117 	case XML_xfrm:					// CT_Transform2D
118         xRet.set( new Transform2DContext( *this, xAttribs, *mpShapePtr ) );
119 		break;
120 	case XML_graphic:				// CT_GraphicalObject
121 		xRet.set( this );
122 		break;
123 
124 		case XML_graphicData :			// CT_GraphicalObjectData
125 		{
126             OUString sUri( xAttribs->getOptionalValue( XML_uri ) );
127             if ( sUri.equalsAscii( "http://schemas.openxmlformats.org/presentationml/2006/ole" ) )
128                 xRet.set( new OleObjectGraphicDataContext( *this, mpShapePtr ) );
129             else if ( sUri.equalsAscii( "http://schemas.openxmlformats.org/drawingml/2006/diagram" ) )
130                 xRet.set( new DiagramGraphicDataContext( *this, mpShapePtr ) );
131             else if ( sUri.equalsAscii( "http://schemas.openxmlformats.org/drawingml/2006/chart" ) )
132                 xRet.set( new ChartGraphicDataContext( *this, mpShapePtr, mbEmbedShapesInChart ) );
133 			else if ( sUri.compareToAscii( "http://schemas.openxmlformats.org/drawingml/2006/table" ) == 0 )
134 				xRet.set( new table::TableContext( *this, mpShapePtr ) );
135 			else
136 			{
137 				OSL_TRACE( "OOX: Ignore graphicsData of %s", OUSTRING_TO_CSTR( sUri ) );
138 				return xRet;
139 			}
140 		}
141 		break;
142 	}
143 	if( !xRet.is() )
144 		xRet.set( ShapeContext::createFastChildContext( aElementToken, xAttribs ) );
145 
146 	return xRet;
147 }
148 
149 // ============================================================================
150 
151 OleObjectGraphicDataContext::OleObjectGraphicDataContext( ContextHandler& rParent, ShapePtr xShape ) :
152     ShapeContext( rParent, ShapePtr(), xShape ),
153     mrOleObjectInfo( xShape->setOleObjectType() )
154 {
155 }
156 
157 OleObjectGraphicDataContext::~OleObjectGraphicDataContext()
158 {
159     /*  Register the OLE shape at the VML drawing, this prevents that the
160         related VML shape converts the OLE object by itself. */
161     if( mrOleObjectInfo.maShapeId.getLength() > 0 )
162         if( ::oox::vml::Drawing* pVmlDrawing = getFilter().getVmlDrawing() )
163             pVmlDrawing->registerOleObject( mrOleObjectInfo );
164 }
165 
166 Reference< XFastContextHandler > OleObjectGraphicDataContext::createFastChildContext( sal_Int32 nElement, const Reference< XFastAttributeList >& xAttribs ) throw (SAXException, RuntimeException)
167 {
168 	Reference< XFastContextHandler > xRet;
169     AttributeList aAttribs( xAttribs );
170 
171     switch( nElement )
172 	{
173         case PPT_TOKEN( oleObj ):
174         {
175             mrOleObjectInfo.maShapeId = aAttribs.getXString( XML_spid, OUString() );
176             const Relation* pRelation = getRelations().getRelationFromRelId( aAttribs.getString( R_TOKEN( id ), OUString() ) );
177             OSL_ENSURE( pRelation, "OleObjectGraphicDataContext::createFastChildContext - missing relation for OLE object" );
178             if( pRelation )
179             {
180                 mrOleObjectInfo.mbLinked = pRelation->mbExternal;
181                 if( pRelation->mbExternal )
182                 {
183                     mrOleObjectInfo.maTargetLink = getFilter().getAbsoluteUrl( pRelation->maTarget );
184                 }
185                 else
186                 {
187                     OUString aFragmentPath = getFragmentPathFromRelation( *pRelation );
188                     if( aFragmentPath.getLength() > 0 )
189                         getFilter().importBinaryData( mrOleObjectInfo.maEmbeddedData, aFragmentPath );
190                 }
191             }
192             mrOleObjectInfo.maName = aAttribs.getXString( XML_name, OUString() );
193             mrOleObjectInfo.maProgId = aAttribs.getXString( XML_progId, OUString() );
194             mrOleObjectInfo.mbShowAsIcon = aAttribs.getBool( XML_showAsIcon, false );
195             xRet.set( this );
196         }
197 		break;
198 
199         case PPT_TOKEN( embed ):
200             OSL_ENSURE( !mrOleObjectInfo.mbLinked, "OleObjectGraphicDataContext::createFastChildContext - unexpected child element" );
201         break;
202 
203         case PPT_TOKEN( link ):
204             OSL_ENSURE( mrOleObjectInfo.mbLinked, "OleObjectGraphicDataContext::createFastChildContext - unexpected child element" );
205             mrOleObjectInfo.mbAutoUpdate = aAttribs.getBool( XML_updateAutomatic, false );
206         break;
207 	}
208     return xRet;
209 }
210 
211 // ============================================================================
212 
213 DiagramGraphicDataContext::DiagramGraphicDataContext( ContextHandler& rParent, ShapePtr pShapePtr )
214 : ShapeContext( rParent, ShapePtr(), pShapePtr )
215 {
216 	pShapePtr->setDiagramType();
217 }
218 
219 DiagramGraphicDataContext::~DiagramGraphicDataContext()
220 {
221 }
222 
223 DiagramPtr DiagramGraphicDataContext::loadDiagram()
224 {
225 	DiagramPtr pDiagram( new Diagram() );
226     XmlFilterBase& rFilter = getFilter();
227 
228 	// data
229     OUString sDmPath = getFragmentPathFromRelId( msDm );
230 	if( sDmPath.getLength() > 0 )
231 	{
232 		DiagramDataPtr pData( new DiagramData() );
233 		pDiagram->setData( pData );
234         rFilter.importFragment( new DiagramDataFragmentHandler( rFilter, sDmPath, pData ) );
235 	}
236 	// layout
237     OUString sLoPath = getFragmentPathFromRelId( msLo );
238 	if( sLoPath.getLength() > 0 )
239 	{
240 		DiagramLayoutPtr pLayout( new DiagramLayout() );
241 		pDiagram->setLayout( pLayout );
242         rFilter.importFragment( new DiagramLayoutFragmentHandler( rFilter, sLoPath, pLayout ) );
243 	}
244 	// style
245     OUString sQsPath = getFragmentPathFromRelId( msQs );
246 	if( sQsPath.getLength() > 0 )
247 	{
248 		DiagramQStylesPtr pStyles( new DiagramQStyles() );
249 		pDiagram->setQStyles( pStyles );
250         rFilter.importFragment( new DiagramQStylesFragmentHandler( rFilter, sQsPath, pStyles ) );
251 	}
252 	// colors
253     OUString sCsPath = getFragmentPathFromRelId( msCs );
254 	if( sCsPath.getLength() > 0 )
255 	{
256 		DiagramColorsPtr pColors( new DiagramColors() );
257 		pDiagram->setColors( pColors );
258         rFilter.importFragment( new DiagramColorsFragmentHandler( rFilter, sCsPath, pColors ) ) ;
259 	}
260 
261 	return pDiagram;
262 }
263 
264 
265 Reference< XFastContextHandler > DiagramGraphicDataContext::createFastChildContext( ::sal_Int32 aElementToken, const Reference< XFastAttributeList >& xAttribs )
266 	throw (SAXException, RuntimeException)
267 {
268 	Reference< XFastContextHandler > xRet;
269 
270 	switch( aElementToken )
271 	{
272 	case DGM_TOKEN( relIds ):
273 	{
274 		msDm = xAttribs->getOptionalValue( R_TOKEN( dm ) );
275 		msLo = xAttribs->getOptionalValue( R_TOKEN( lo ) );
276 		msQs = xAttribs->getOptionalValue( R_TOKEN( qs ) );
277 		msCs = xAttribs->getOptionalValue( R_TOKEN( cs ) );
278 		DiagramPtr pDiagram = loadDiagram();
279 		pDiagram->addTo( mpShapePtr );
280 		OSL_TRACE("diagram added shape %s of type %s", OUSTRING_TO_CSTR( mpShapePtr->getName() ),
281 				  OUSTRING_TO_CSTR( mpShapePtr->getServiceName() ) );
282 		break;
283 	}
284 	default:
285 		break;
286 	}
287 
288 	if( !xRet.is() )
289 		xRet.set( ShapeContext::createFastChildContext( aElementToken, xAttribs ) );
290 
291 	return xRet;
292 }
293 
294 // ============================================================================
295 
296 ChartGraphicDataContext::ChartGraphicDataContext( ContextHandler& rParent, const ShapePtr& rxShape, bool bEmbedShapes ) :
297     ShapeContext( rParent, ShapePtr(), rxShape ),
298     mrChartShapeInfo( rxShape->setChartType( bEmbedShapes ) )
299 {
300 }
301 
302 Reference< XFastContextHandler > ChartGraphicDataContext::createFastChildContext( ::sal_Int32 nElement, const Reference< XFastAttributeList >& rxAttribs )
303     throw (SAXException, RuntimeException)
304 {
305     if( nElement == C_TOKEN( chart ) )
306     {
307         AttributeList aAttribs( rxAttribs );
308         mrChartShapeInfo.maFragmentPath = getFragmentPathFromRelId( aAttribs.getString( R_TOKEN( id ), OUString() ) );
309     }
310     return 0;
311 }
312 
313 // ============================================================================
314 
315 } // namespace drawingml
316 } // namespace oox
317 
318