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 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_svtools.hxx"
30 
31 #include <svtools/chartprettypainter.hxx>
32 
33 #include <tools/globname.hxx>
34 #include <sot/clsids.hxx>
35 // header for function rtl_createUuid
36 #include <rtl/uuid.h>
37 #include <vcl/pdfextoutdevdata.hxx>
38 
39 #include <com/sun/star/lang/XUnoTunnel.hpp>
40 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
41 #include <svtools/embedhlp.hxx>
42 
43 using namespace ::com::sun::star;
44 
45 ChartPrettyPainter::ChartPrettyPainter()
46 {
47 }
48 
49 ChartPrettyPainter::~ChartPrettyPainter()
50 {
51 }
52 
53 bool ChartPrettyPainter::DoPaint(OutputDevice* /*pOutDev*/, const Rectangle& /*rLogicObjectRect*/) const
54 {
55     return false;
56 }
57 
58 //static
59 const uno::Sequence<sal_Int8>& ChartPrettyPainter::getUnoTunnelId()
60 {
61     static uno::Sequence<sal_Int8> * pSeq = 0;
62 	if( !pSeq )
63 	{
64 		osl::Guard< osl::Mutex > aGuard( osl::Mutex::getGlobalMutex() );
65 		if( !pSeq )
66 		{
67             static uno::Sequence< sal_Int8 > aSeq( 16 );
68 			rtl_createUuid( (sal_uInt8*)aSeq.getArray(), 0, sal_True );
69 			pSeq = &aSeq;
70 		}
71 	}
72 	return *pSeq;
73 }
74 
75 bool ChartPrettyPainter::IsChart( const svt::EmbeddedObjectRef& xObjRef )
76 {
77     if ( !xObjRef.is() )
78         return false;
79 
80     SvGlobalName aObjClsId( xObjRef->getClassID() );
81     if(
82         SvGlobalName(SO3_SCH_CLASSID_30) == aObjClsId
83         || SvGlobalName(SO3_SCH_CLASSID_40) == aObjClsId
84         || SvGlobalName(SO3_SCH_CLASSID_50) == aObjClsId
85         || SvGlobalName(SO3_SCH_CLASSID_60) == aObjClsId)
86     {
87         return true;
88     }
89 
90 	return false;
91 }
92 
93 bool ChartPrettyPainter::ShouldPrettyPaintChartOnThisDevice( OutputDevice* pOutDev )
94 {
95     if( !pOutDev )
96         return false;
97     //at least the print preview in calc has a paint loop due to too much invalidate calls deep in sdr
98     //to avoid the paint loop we use the metafile replacement in this case instead of direct rendering
99     if( OUTDEV_WINDOW == pOutDev->GetOutDevType() )
100         return false;
101     if( OUTDEV_PRINTER == pOutDev->GetOutDevType() )
102         return true;
103     vcl::PDFExtOutDevData* pPDFData = PTR_CAST( vcl::PDFExtOutDevData, pOutDev->GetExtOutDevData() );
104     if( pPDFData )
105         return true;
106     return false;
107 }
108 
109 bool ChartPrettyPainter::DoPrettyPaintChart( uno::Reference< frame::XModel > xChartModel, OutputDevice* pOutDev, const Rectangle& rLogicObjectRect )
110 {
111     //charts must be painted resolution dependent!! #i82893#, #i75867#
112     if( !xChartModel.is() || !ShouldPrettyPaintChartOnThisDevice( pOutDev ) )
113         return false;
114 
115     try
116     {
117         uno::Reference< lang::XMultiServiceFactory > xFact( xChartModel, uno::UNO_QUERY );
118         OSL_ENSURE( xFact.is(), "Chart cannot be painted pretty!\n" );
119         if( xFact.is() )
120         {
121             uno::Reference< lang::XUnoTunnel > xChartRenderer( xFact->createInstance(
122                 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.chart2.ChartRenderer" ) ) ), uno::UNO_QUERY );
123             OSL_ENSURE( xChartRenderer.is(), "Chart cannot be painted pretty!\n" );
124             if( xChartRenderer.is() )
125             {
126                 ChartPrettyPainter* pPrettyPainter = reinterpret_cast<ChartPrettyPainter*>(
127                     xChartRenderer->getSomething( ChartPrettyPainter::getUnoTunnelId() ));
128                 if( pPrettyPainter )
129                     return pPrettyPainter->DoPaint(pOutDev, rLogicObjectRect);
130             }
131         }
132     }
133     catch( uno::Exception& e )
134 	{
135 		(void)e;
136 		DBG_ERROR( "Chart cannot be painted pretty!" );
137 	}
138     return false;
139 }
140 
141