fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / chart2 / source / view / main / DrawModelWrapper.cxx
blobc9c1ba2930da40822e4a1658ca33d8b1f7a9a442
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "chartview/DrawModelWrapper.hxx"
21 #include "macros.hxx"
22 #include "AbstractShapeFactory.hxx"
23 #include "ChartItemPool.hxx"
24 #include "ObjectIdentifier.hxx"
25 #include <svx/unomodel.hxx>
26 #include <svl/itempool.hxx>
27 #include <svl/eitem.hxx>
28 #include <editeng/eeitem.hxx>
29 #include <svx/svx3ditems.hxx>
30 #include <unotools/pathoptions.hxx>
31 #include <svx/objfac3d.hxx>
32 #include <svx/svdpage.hxx>
33 #include <svx/XPropertyTable.hxx>
34 #include <svx/xtable.hxx>
35 #include <svx/svdoutl.hxx>
36 #include <editeng/unolingu.hxx>
37 #include <vcl/svapp.hxx>
38 #include <vcl/virdev.hxx>
40 #include <com/sun/star/container/XChild.hpp>
41 #include <com/sun/star/lang/XUnoTunnel.hpp>
43 #include <sfx2/objsh.hxx>
44 #include <com/sun/star/linguistic2/XHyphenator.hpp>
45 #include <com/sun/star/linguistic2/XSpellChecker1.hpp>
47 using namespace ::com::sun::star;
49 namespace
51 // this code is copied from sfx2/source/doc/objembed.cxx
52 SfxObjectShell * lcl_GetParentObjectShell( const uno::Reference< frame::XModel > & xModel )
54 SfxObjectShell* pResult = NULL;
56 try
58 uno::Reference< container::XChild > xChildModel( xModel, uno::UNO_QUERY );
59 if ( xChildModel.is() )
61 uno::Reference< lang::XUnoTunnel > xParentTunnel( xChildModel->getParent(), uno::UNO_QUERY );
62 if ( xParentTunnel.is() )
64 SvGlobalName aSfxIdent( SFX_GLOBAL_CLASSID );
65 pResult = reinterpret_cast< SfxObjectShell * >(
66 xParentTunnel->getSomething( uno::Sequence< sal_Int8 >( aSfxIdent.GetByteSequence() ) ) );
70 catch( const uno::Exception& )
72 // TODO: error handling
75 return pResult;
78 // this code is copied from sfx2/source/doc/objembed.cxx. It is a workaround to
79 // get the reference device (e.g. printer) fromthe parent document
80 OutputDevice * lcl_GetParentRefDevice( const uno::Reference< frame::XModel > & xModel )
82 SfxObjectShell * pParent = lcl_GetParentObjectShell( xModel );
83 if ( pParent )
84 return pParent->GetDocumentRefDev();
85 return NULL;
88 } // anonymous namespace
90 namespace chart
93 DrawModelWrapper::DrawModelWrapper( const uno::Reference<uno::XComponentContext>& /*xContext*/ )
94 : SdrModel( SvtPathOptions().GetPalettePath(), NULL, NULL, false, false )
95 , m_pChartItemPool(0)
96 , m_xMainDrawPage(0)
97 , m_xHiddenDrawPage(0)
99 m_pChartItemPool = ChartItemPool::CreateChartItemPool();
101 SetScaleUnit(MAP_100TH_MM);
102 SetScaleFraction(Fraction(1, 1));
103 SetDefaultFontHeight(423); // 12pt
105 SfxItemPool* pMasterPool = &GetItemPool();
106 pMasterPool->SetDefaultMetric(SFX_MAPUNIT_100TH_MM);
107 pMasterPool->SetPoolDefaultItem(SfxBoolItem(EE_PARA_HYPHENATE, true) );
108 pMasterPool->SetPoolDefaultItem(makeSvx3DPercentDiagonalItem (5));
110 SfxItemPool* pPool = pMasterPool;
111 // append chart pool to end of pool chain
112 for (;;)
114 SfxItemPool* pSecondary = pPool->GetSecondaryPool();
115 if (!pSecondary)
116 break;
118 pPool = pSecondary;
120 pPool->SetSecondaryPool(m_pChartItemPool);
121 pMasterPool->FreezeIdRanges();
123 //this factory needs to be created before first use of 3D scenes once upon an office runtime
124 //@todo in future this should be done by drawing engine itself on demand
125 static bool b3dFactoryInitialized = false;
126 if(!b3dFactoryInitialized)
128 E3dObjFactory aObjFactory;
129 b3dFactoryInitialized = true;
132 //Hyphenyation and spellchecking
133 SdrOutliner& rOutliner = GetDrawOutliner();
136 uno::Reference< linguistic2::XHyphenator > xHyphenator( LinguMgr::GetHyphenator() );
137 if( xHyphenator.is() )
138 rOutliner.SetHyphenator( xHyphenator );
140 uno::Reference< linguistic2::XSpellChecker1 > xSpellChecker( LinguMgr::GetSpellChecker() );
141 if ( xSpellChecker.is() )
142 rOutliner.SetSpeller( xSpellChecker );
144 catch(...)
146 OSL_FAIL("Can't get Hyphenator or SpellChecker for chart");
149 //ref device for font rendering
150 OutputDevice* pDefaultDevice = rOutliner.GetRefDevice();
151 if( !pDefaultDevice )
152 pDefaultDevice = Application::GetDefaultDevice();
153 m_pRefDevice.disposeAndClear();
154 m_pRefDevice = VclPtr<VirtualDevice>::Create(*pDefaultDevice);
155 MapMode aMapMode = m_pRefDevice->GetMapMode();
156 aMapMode.SetMapUnit(MAP_100TH_MM);
157 m_pRefDevice->SetMapMode(aMapMode);
158 SetRefDevice(m_pRefDevice.get());
159 rOutliner.SetRefDevice(m_pRefDevice.get());
162 DrawModelWrapper::~DrawModelWrapper()
164 //remove m_pChartItemPool from pool chain
165 if(m_pChartItemPool)
167 SfxItemPool* pPool = &GetItemPool();
168 for (;;)
170 SfxItemPool* pSecondary = pPool->GetSecondaryPool();
171 if(pSecondary == m_pChartItemPool)
173 pPool->SetSecondaryPool (NULL);
174 break;
176 pPool = pSecondary;
178 SfxItemPool::Free(m_pChartItemPool);
180 m_pRefDevice.disposeAndClear();
183 uno::Reference< uno::XInterface > DrawModelWrapper
184 ::createUnoModel()
186 uno::Reference< lang::XComponent > xComponent = new SvxUnoDrawingModel( this ); //tell Andreas Schluens if SvxUnoDrawingModel is not needed anymore -> remove export from svx to avoid link problems in writer
187 return uno::Reference< uno::XInterface >::query( xComponent );
190 uno::Reference< frame::XModel > DrawModelWrapper::getUnoModel()
192 uno::Reference< uno::XInterface > xI = this->SdrModel::getUnoModel();
193 return uno::Reference<frame::XModel>::query( xI );
196 SdrModel& DrawModelWrapper::getSdrModel()
198 return *this;
201 uno::Reference< lang::XMultiServiceFactory > DrawModelWrapper::getShapeFactory()
203 uno::Reference< lang::XMultiServiceFactory > xShapeFactory( this->getUnoModel(), uno::UNO_QUERY );
204 return xShapeFactory;
207 uno::Reference< drawing::XDrawPage > DrawModelWrapper::getMainDrawPage()
209 if (m_xMainDrawPage.is())
210 return m_xMainDrawPage;
212 // Create draw page.
213 uno::Reference<drawing::XDrawPagesSupplier> xDrawPagesSuplier(getUnoModel(), uno::UNO_QUERY);
214 if (!xDrawPagesSuplier.is())
215 return m_xMainDrawPage;
217 uno::Reference<drawing::XDrawPages> xDrawPages = xDrawPagesSuplier->getDrawPages();
218 if (xDrawPages->getCount() > 1)
220 // Take the first page in case of multiple pages.
221 uno::Any aPage = xDrawPages->getByIndex(0);
222 aPage >>= m_xMainDrawPage;
225 if (!m_xMainDrawPage.is())
227 m_xMainDrawPage = xDrawPages->insertNewByIndex(0);
230 //ensure that additional shapes are in front of the chart objects so create the chart root before
231 // let us disable this call for now
232 // TODO:moggi
233 // AbstractShapeFactory::getOrCreateShapeFactory(this->getShapeFactory())->getOrCreateChartRootShape( m_xMainDrawPage );
234 return m_xMainDrawPage;
236 uno::Reference< drawing::XDrawPage > DrawModelWrapper::getHiddenDrawPage()
238 if( !m_xHiddenDrawPage.is() )
240 uno::Reference< drawing::XDrawPagesSupplier > xDrawPagesSuplier( this->getUnoModel(), uno::UNO_QUERY );
241 if( xDrawPagesSuplier.is() )
243 uno::Reference< drawing::XDrawPages > xDrawPages( xDrawPagesSuplier->getDrawPages () );
244 if( xDrawPages->getCount()>1 )
246 uno::Any aPage = xDrawPages->getByIndex( 1 ) ;
247 aPage >>= m_xHiddenDrawPage;
250 if(!m_xHiddenDrawPage.is())
252 if( xDrawPages->getCount()==0 )
253 m_xMainDrawPage = xDrawPages->insertNewByIndex( 0 );
254 m_xHiddenDrawPage = xDrawPages->insertNewByIndex( 1 );
258 return m_xHiddenDrawPage;
260 void DrawModelWrapper::clearMainDrawPage()
262 //uno::Reference<drawing::XShapes> xChartRoot( m_xMainDrawPage, uno::UNO_QUERY );
263 uno::Reference<drawing::XShapes> xChartRoot( AbstractShapeFactory::getChartRootShape( m_xMainDrawPage ) );
264 if( xChartRoot.is() )
266 sal_Int32 nSubCount = xChartRoot->getCount();
267 uno::Reference< drawing::XShape > xShape;
268 for( sal_Int32 nS = nSubCount; nS--; )
270 if( xChartRoot->getByIndex( nS ) >>= xShape )
271 xChartRoot->remove( xShape );
276 uno::Reference< drawing::XShapes > DrawModelWrapper::getChartRootShape(
277 const uno::Reference< drawing::XDrawPage>& xDrawPage )
279 return AbstractShapeFactory::getChartRootShape( xDrawPage );
282 void DrawModelWrapper::lockControllers()
284 uno::Reference< frame::XModel > xDrawModel( this->getUnoModel() );
285 if( xDrawModel.is())
286 xDrawModel->lockControllers();
288 void DrawModelWrapper::unlockControllers()
290 uno::Reference< frame::XModel > xDrawModel( this->getUnoModel() );
291 if( xDrawModel.is())
292 xDrawModel->unlockControllers();
295 void DrawModelWrapper::attachParentReferenceDevice( const uno::Reference< frame::XModel > & xChartModel )
297 OutputDevice * pParentRefDev( lcl_GetParentRefDevice( xChartModel ));
298 if( pParentRefDev )
300 SetRefDevice( pParentRefDev );
304 OutputDevice* DrawModelWrapper::getReferenceDevice() const
306 return SdrModel::GetRefDevice();
309 SfxItemPool& DrawModelWrapper::GetItemPool()
311 return this->SdrModel::GetItemPool();
313 const SfxItemPool& DrawModelWrapper::GetItemPool() const
315 return this->SdrModel::GetItemPool();
317 XColorListRef DrawModelWrapper::GetColorList() const
319 return this->SdrModel::GetColorList();
321 XDashListRef DrawModelWrapper::GetDashList() const
323 return this->SdrModel::GetDashList();
325 XLineEndListRef DrawModelWrapper::GetLineEndList() const
327 return this->SdrModel::GetLineEndList();
329 XGradientListRef DrawModelWrapper::GetGradientList() const
331 return this->SdrModel::GetGradientList();
333 XHatchListRef DrawModelWrapper::GetHatchList() const
335 return this->SdrModel::GetHatchList();
337 XBitmapListRef DrawModelWrapper::GetBitmapList() const
339 return this->SdrModel::GetBitmapList();
342 SdrObject* DrawModelWrapper::getNamedSdrObject( const OUString& rName )
344 if( rName.isEmpty() )
345 return 0;
346 return getNamedSdrObject( rName, GetPage(0) );
349 SdrObject* DrawModelWrapper::getNamedSdrObject( const OUString& rObjectCID, SdrObjList* pSearchList )
351 if(!pSearchList || rObjectCID.isEmpty())
352 return 0;
353 const size_t nCount = pSearchList->GetObjCount();
354 for( size_t nN=0; nN<nCount; ++nN )
356 SdrObject* pObj = pSearchList->GetObj(nN);
357 if(!pObj)
358 continue;
359 if( ObjectIdentifier::areIdenticalObjects( rObjectCID, pObj->GetName() ) )
360 return pObj;
361 pObj = DrawModelWrapper::getNamedSdrObject( rObjectCID, pObj->GetSubList() );
362 if(pObj)
363 return pObj;
365 return 0;
368 bool DrawModelWrapper::removeShape( const uno::Reference< drawing::XShape >& xShape )
370 uno::Reference< container::XChild > xChild( xShape, uno::UNO_QUERY );
371 if( xChild.is() )
373 uno::Reference<drawing::XShapes> xShapes( xChild->getParent(), uno::UNO_QUERY );
374 if( xShapes.is() )
376 xShapes->remove(xShape);
377 return true;
380 return false;
383 } //namespace chart
385 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */