merge the formfield patch from ooo-build
[ooovba.git] / chart2 / source / view / main / DrawModelWrapper.cxx
blobe03e9e42d79eb4243c5e24baedac86bdca85a46f
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: DrawModelWrapper.cxx,v $
10 * $Revision: 1.4 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_chart2.hxx"
34 #include "chartview/DrawModelWrapper.hxx"
35 #include "macros.hxx"
36 #include "ShapeFactory.hxx"
37 #include "ChartItemPool.hxx"
38 #include "ObjectIdentifier.hxx"
39 #include <svx/unomodel.hxx>
40 #include <svtools/itempool.hxx>
41 // header for class SfxBoolItem
42 #include <svtools/eitem.hxx>
43 // header for define EE_PARA_HYPHENATE
44 #include <svx/eeitem.hxx>
45 // header for class Svx3DPercentDiagonalItem
46 #include <svx/svx3ditems.hxx>
47 // header for class SvtPathOptions
48 #include <svtools/pathoptions.hxx>
49 // header E3dObjFactory
50 #include <svx/objfac3d.hxx>
51 // header for class SdrObjList
52 #include <svx/svdpage.hxx>
53 // header for SvxUnoXDashTable_createInstance
54 #include <svx/XPropertyTable.hxx>
55 // header for class XDashList
56 #include <svx/xtable.hxx>
57 // header for class SdrOutliner
58 #include <svx/svdoutl.hxx>
59 // header for class LinguMgr
60 #include <svx/unolingu.hxx>
61 // header for class Application
62 #include <vcl/svapp.hxx>
63 // header for class VirtualDevice
64 #include <vcl/virdev.hxx>
66 #include <com/sun/star/container/XChild.hpp>
67 #include <com/sun/star/lang/XUnoTunnel.hpp>
69 #include <sfx2/objsh.hxx>
70 #include <com/sun/star/linguistic2/XHyphenator.hpp>
71 #include <com/sun/star/linguistic2/XSpellChecker1.hpp>
73 using namespace ::com::sun::star;
75 //.............................................................................
76 namespace
78 // this code is copied from sfx2/source/doc/objembed.cxx
79 SfxObjectShell * lcl_GetParentObjectShell( const uno::Reference< frame::XModel > & xModel )
81 SfxObjectShell* pResult = NULL;
83 try
85 uno::Reference< container::XChild > xChildModel( xModel, uno::UNO_QUERY );
86 if ( xChildModel.is() )
88 uno::Reference< lang::XUnoTunnel > xParentTunnel( xChildModel->getParent(), uno::UNO_QUERY );
89 if ( xParentTunnel.is() )
91 SvGlobalName aSfxIdent( SFX_GLOBAL_CLASSID );
92 pResult = reinterpret_cast< SfxObjectShell * >(
93 xParentTunnel->getSomething( uno::Sequence< sal_Int8 >( aSfxIdent.GetByteSequence() ) ) );
97 catch( uno::Exception& )
99 // TODO: error handling
102 return pResult;
105 // this code is copied from sfx2/source/doc/objembed.cxx. It is a workaround to
106 // get the reference device (e.g. printer) fromthe parent document
107 OutputDevice * lcl_GetParentRefDevice( const uno::Reference< frame::XModel > & xModel )
109 SfxObjectShell * pParent = lcl_GetParentObjectShell( xModel );
110 if ( pParent )
111 return pParent->GetDocumentRefDev();
112 return NULL;
115 } // anonymous namespace
117 //.. ...........................................................................
118 namespace chart
120 //.............................................................................
122 DrawModelWrapper::DrawModelWrapper(
123 uno::Reference<uno::XComponentContext> const & xContext )
124 : SdrModel( SvtPathOptions().GetPalettePath() )
125 , m_xMCF(0)
126 , m_pChartItemPool(0)
127 , m_xMainDrawPage(0)
128 , m_xHiddenDrawPage(0)
129 , m_apRefDevice(0)
131 m_pChartItemPool = ChartItemPool::CreateChartItemPool();
133 m_xMCF = xContext->getServiceManager();
135 SetScaleUnit(MAP_100TH_MM);
136 SetScaleFraction(Fraction(1, 1));
137 SetDefaultFontHeight(847); // 24pt
139 SfxItemPool* pMasterPool = &GetItemPool();
140 pMasterPool->SetDefaultMetric(SFX_MAPUNIT_100TH_MM);
141 pMasterPool->SetPoolDefaultItem(SfxBoolItem(EE_PARA_HYPHENATE, TRUE) );
142 pMasterPool->SetPoolDefaultItem(Svx3DPercentDiagonalItem (5));
144 SfxItemPool* pPool = pMasterPool;
145 // append chart pool to end of pool chain
146 for (;;)
148 SfxItemPool* pSecondary = pPool->GetSecondaryPool();
149 if (!pSecondary)
150 break;
152 pPool = pSecondary;
154 pPool->SetSecondaryPool(m_pChartItemPool);
155 pMasterPool->FreezeIdRanges();
157 //this factory needs to be created before first use of 3D scenes once upon an office runtime
158 //@todo in future this should be done by drawing engine itself on demand
159 static bool b3dFactoryInitialized = false;
160 if(!b3dFactoryInitialized)
162 E3dObjFactory aObjFactory;
163 b3dFactoryInitialized = true;
166 //Hyphenyation and spellchecking
167 SdrOutliner& rOutliner = GetDrawOutliner();
170 uno::Reference< linguistic2::XHyphenator > xHyphenator( LinguMgr::GetHyphenator() );
171 if( xHyphenator.is() )
172 rOutliner.SetHyphenator( xHyphenator );
174 uno::Reference< linguistic2::XSpellChecker1 > xSpellChecker( LinguMgr::GetSpellChecker() );
175 if ( xSpellChecker.is() )
176 rOutliner.SetSpeller( xSpellChecker );
178 catch(...)
180 DBG_ERROR("Can't get Hyphenator or SpellChecker for chart");
183 //ref device for font rendering
184 OutputDevice* pDefaultDevice = rOutliner.GetRefDevice();
185 if( !pDefaultDevice )
186 pDefaultDevice = Application::GetDefaultDevice();
187 m_apRefDevice = std::auto_ptr< OutputDevice >( new VirtualDevice( *pDefaultDevice ) );
188 MapMode aMapMode = m_apRefDevice->GetMapMode();
189 aMapMode.SetMapUnit(MAP_100TH_MM);
190 m_apRefDevice->SetMapMode(aMapMode);
191 SetRefDevice(m_apRefDevice.get());
192 rOutliner.SetRefDevice(m_apRefDevice.get());
195 DrawModelWrapper::~DrawModelWrapper()
197 //remove m_pChartItemPool from pool chain
198 if(m_pChartItemPool)
200 SfxItemPool* pPool = &GetItemPool();
201 for (;;)
203 SfxItemPool* pSecondary = pPool->GetSecondaryPool();
204 if(pSecondary == m_pChartItemPool)
206 pPool->SetSecondaryPool (NULL);
207 break;
209 pPool = pSecondary;
211 SfxItemPool::Free(m_pChartItemPool);
215 uno::Reference< uno::XInterface > DrawModelWrapper
216 ::createUnoModel()
218 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
219 return uno::Reference< uno::XInterface >::query( xComponent );
222 uno::Reference< frame::XModel > DrawModelWrapper::getUnoModel()
224 uno::Reference< uno::XInterface > xI = this->SdrModel::getUnoModel();
225 return uno::Reference<frame::XModel>::query( xI );
228 SdrModel& DrawModelWrapper::getSdrModel()
230 return *this;
233 uno::Reference< lang::XMultiServiceFactory > DrawModelWrapper::getShapeFactory()
235 uno::Reference< lang::XMultiServiceFactory > xShapeFactory( this->getUnoModel(), uno::UNO_QUERY );
236 return xShapeFactory;
239 uno::Reference< drawing::XDrawPage > DrawModelWrapper::getMainDrawPage()
241 //create draw page:
242 if( !m_xMainDrawPage.is() )
244 uno::Reference< drawing::XDrawPagesSupplier > xDrawPagesSuplier( this->getUnoModel(), uno::UNO_QUERY );
245 if( xDrawPagesSuplier.is() )
247 uno::Reference< drawing::XDrawPages > xDrawPages( xDrawPagesSuplier->getDrawPages () );
248 if( xDrawPages->getCount()>1 )
250 uno::Any aPage = xDrawPages->getByIndex( 0 ) ;
251 aPage >>= m_xMainDrawPage;
253 if(!m_xMainDrawPage.is())
255 m_xMainDrawPage = xDrawPages->insertNewByIndex( 0 );
259 //ensure that additional shapes are in front of the chart objects so create the chart root before
260 ShapeFactory(this->getShapeFactory()).getOrCreateChartRootShape( m_xMainDrawPage );
261 return m_xMainDrawPage;
263 uno::Reference< drawing::XDrawPage > DrawModelWrapper::getHiddenDrawPage()
265 if( !m_xHiddenDrawPage.is() )
267 uno::Reference< drawing::XDrawPagesSupplier > xDrawPagesSuplier( this->getUnoModel(), uno::UNO_QUERY );
268 if( xDrawPagesSuplier.is() )
270 uno::Reference< drawing::XDrawPages > xDrawPages( xDrawPagesSuplier->getDrawPages () );
271 if( xDrawPages->getCount()>1 )
273 uno::Any aPage = xDrawPages->getByIndex( 1 ) ;
274 aPage >>= m_xHiddenDrawPage;
277 if(!m_xHiddenDrawPage.is())
279 if( xDrawPages->getCount()==0 )
280 m_xMainDrawPage = xDrawPages->insertNewByIndex( 0 );
281 m_xHiddenDrawPage = xDrawPages->insertNewByIndex( 1 );
285 return m_xHiddenDrawPage;
287 void DrawModelWrapper::clearMainDrawPage()
289 //uno::Reference<drawing::XShapes> xChartRoot( m_xMainDrawPage, uno::UNO_QUERY );
290 uno::Reference<drawing::XShapes> xChartRoot( ShapeFactory::getChartRootShape( m_xMainDrawPage ) );
291 if( xChartRoot.is() )
293 sal_Int32 nSubCount = xChartRoot->getCount();
294 uno::Reference< drawing::XShape > xShape;
295 for( sal_Int32 nS = nSubCount; nS--; )
297 if( xChartRoot->getByIndex( nS ) >>= xShape )
298 xChartRoot->remove( xShape );
303 uno::Reference< drawing::XShapes > DrawModelWrapper::getChartRootShape(
304 const uno::Reference< drawing::XDrawPage>& xDrawPage )
306 return ShapeFactory::getChartRootShape( xDrawPage );
309 void DrawModelWrapper::lockControllers()
311 uno::Reference< frame::XModel > xDrawModel( this->getUnoModel() );
312 if( xDrawModel.is())
313 xDrawModel->lockControllers();
315 void DrawModelWrapper::unlockControllers()
317 uno::Reference< frame::XModel > xDrawModel( this->getUnoModel() );
318 if( xDrawModel.is())
319 xDrawModel->unlockControllers();
322 void DrawModelWrapper::attachParentReferenceDevice( const uno::Reference< frame::XModel > & xChartModel )
324 OutputDevice * pParentRefDev( lcl_GetParentRefDevice( xChartModel ));
325 if( pParentRefDev )
327 SetRefDevice( pParentRefDev );
331 OutputDevice* DrawModelWrapper::getReferenceDevice() const
333 return SdrModel::GetRefDevice();
336 SfxItemPool& DrawModelWrapper::GetItemPool()
338 return this->SdrModel::GetItemPool();
340 const SfxItemPool& DrawModelWrapper::GetItemPool() const
342 return this->SdrModel::GetItemPool();
344 XColorTable* DrawModelWrapper::GetColorTable() const
346 return this->SdrModel::GetColorTable();
348 XDashList* DrawModelWrapper::GetDashList() const
350 return this->SdrModel::GetDashList();
352 XLineEndList* DrawModelWrapper::GetLineEndList() const
354 return this->SdrModel::GetLineEndList();
356 XGradientList* DrawModelWrapper::GetGradientList() const
358 return this->SdrModel::GetGradientList();
360 XHatchList* DrawModelWrapper::GetHatchList() const
362 return this->SdrModel::GetHatchList();
364 XBitmapList* DrawModelWrapper::GetBitmapList() const
366 return this->SdrModel::GetBitmapList();
369 SdrObject* DrawModelWrapper::getNamedSdrObject( const rtl::OUString& rName )
371 if(rName.getLength()==0)
372 return 0;
373 return getNamedSdrObject( rName, GetPage(0) );
376 //static
377 SdrObject* DrawModelWrapper::getNamedSdrObject( const String& rObjectCID, SdrObjList* pSearchList )
379 if(!pSearchList || rObjectCID.Len()==0)
380 return 0;
381 ULONG nCount = pSearchList->GetObjCount();
382 for( ULONG nN=0; nN<nCount; nN++ )
384 SdrObject* pObj = pSearchList->GetObj(nN);
385 if(!pObj)
386 continue;
387 if( ObjectIdentifier::areIdenticalObjects( rObjectCID, pObj->GetName() ) )
388 return pObj;
389 pObj = DrawModelWrapper::getNamedSdrObject( rObjectCID, pObj->GetSubList() );
390 if(pObj)
391 return pObj;
393 return 0;
396 namespace
398 void impl_addOrExchangeElements( const uno::Reference< uno::XInterface >& xSource
399 , const uno::Reference< uno::XInterface >& xTarget )
401 uno::Reference< container::XNameContainer > xSourceContainer( xSource, uno::UNO_QUERY );
402 uno::Reference< container::XNameContainer > xTargetContainer( xTarget, uno::UNO_QUERY );
404 if(!xSourceContainer.is()||!xTargetContainer.is())
405 return;
409 //put each element of xSourceContainer to xTargetContainer
410 uno::Sequence< rtl::OUString > aNames( xSourceContainer->getElementNames() );
411 for( sal_Int32 nN = aNames.getLength(); nN--; )
413 rtl::OUString aName(aNames[nN]);
414 uno::Any aNewValue( xSourceContainer->getByName( aName ) );
415 if( xTargetContainer->hasByName(aName) )
417 uno::Any aOldValue( xTargetContainer->getByName( aName ) );
418 if( aOldValue != aNewValue )
419 xTargetContainer->replaceByName(aName,aNewValue);
421 else
422 xTargetContainer->insertByName( aName, aNewValue );
425 catch( const uno::Exception & ex )
427 ASSERT_EXCEPTION( ex );
432 void DrawModelWrapper::updateTablesFromChartModel( const uno::Reference< frame::XModel >& xChartModel )
434 //all gradients, etc contained in the model need to be offered fot the view and GUI also:
436 DBG_ASSERT( xChartModel.is(),"need a chart model for updateTablesFromChartModel");
437 if( !xChartModel.is() )
438 return;
440 uno::Reference< lang::XMultiServiceFactory > xTableFactory( xChartModel, uno::UNO_QUERY );
441 DBG_ASSERT( xTableFactory.is(), "new model is expected to implement service factory for gradient table etc" );
442 if( !xTableFactory.is() )
443 return;
445 //todo? colorTable...
446 //impl_addOrExchangeElements( xTableFactory->createInstance( C2U("com.sun.star.drawing.XXX") )
447 // , SvxUnoXColorTable_createInstance( this->GetColorTable() ) );
448 impl_addOrExchangeElements( xTableFactory->createInstance( C2U("com.sun.star.drawing.DashTable") )
449 , SvxUnoXDashTable_createInstance( this->GetDashList() ) );
450 impl_addOrExchangeElements( xTableFactory->createInstance( C2U("com.sun.star.drawing.MarkerTable") )
451 , SvxUnoXLineEndTable_createInstance( this->GetLineEndList() ) );
452 impl_addOrExchangeElements( xTableFactory->createInstance( C2U("com.sun.star.drawing.GradientTable") )
453 , SvxUnoXGradientTable_createInstance( this->GetGradientList() ) );
454 impl_addOrExchangeElements( xTableFactory->createInstance( C2U("com.sun.star.drawing.HatchTable") )
455 , SvxUnoXHatchTable_createInstance( this->GetHatchList() ) );
456 impl_addOrExchangeElements( xTableFactory->createInstance( C2U("com.sun.star.drawing.BitmapTable") )
457 , SvxUnoXBitmapTable_createInstance( this->GetBitmapList() ) );
459 // transparency gradients
460 uno::Reference< uno::XInterface > xSource(
461 xTableFactory->createInstance( C2U("com.sun.star.drawing.TransparencyGradientTable")));
462 uno::Reference< uno::XInterface > xTarget(
463 this->getShapeFactory()->createInstance( C2U("com.sun.star.drawing.TransparencyGradientTable")));
464 impl_addOrExchangeElements( xSource, xTarget );
467 //static
468 bool DrawModelWrapper::removeShape( const uno::Reference< drawing::XShape >& xShape )
470 uno::Reference< container::XChild > xChild( xShape, uno::UNO_QUERY );
471 if( xChild.is() )
473 uno::Reference<drawing::XShapes> xShapes( xChild->getParent(), uno::UNO_QUERY );
474 if( xShapes.is() )
476 xShapes->remove(xShape);
477 return true;
480 return false;
483 //.............................................................................
484 } //namespace chart
485 //.............................................................................