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