Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
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 : // header for class SfxBoolItem
28 : #include <svl/eitem.hxx>
29 : // header for define EE_PARA_HYPHENATE
30 : #include <editeng/eeitem.hxx>
31 : // header for class Svx3DPercentDiagonalItem
32 : #include <svx/svx3ditems.hxx>
33 : // header for class SvtPathOptions
34 : #include <unotools/pathoptions.hxx>
35 : // header E3dObjFactory
36 : #include <svx/objfac3d.hxx>
37 : // header for class SdrObjList
38 : #include <svx/svdpage.hxx>
39 : // header for SvxUnoXDashTable_createInstance
40 : #include <svx/XPropertyTable.hxx>
41 : // header for class XDashList
42 : #include <svx/xtable.hxx>
43 : // header for class SdrOutliner
44 : #include <svx/svdoutl.hxx>
45 : // header for class LinguMgr
46 : #include <editeng/unolingu.hxx>
47 : // header for class Application
48 : #include <vcl/svapp.hxx>
49 : // header for class VirtualDevice
50 : #include <vcl/virdev.hxx>
51 :
52 : #include <com/sun/star/container/XChild.hpp>
53 : #include <com/sun/star/lang/XUnoTunnel.hpp>
54 :
55 : #include <sfx2/objsh.hxx>
56 : #include <com/sun/star/linguistic2/XHyphenator.hpp>
57 : #include <com/sun/star/linguistic2/XSpellChecker1.hpp>
58 :
59 : using namespace ::com::sun::star;
60 :
61 : namespace
62 : {
63 : // this code is copied from sfx2/source/doc/objembed.cxx
64 0 : SfxObjectShell * lcl_GetParentObjectShell( const uno::Reference< frame::XModel > & xModel )
65 : {
66 0 : SfxObjectShell* pResult = NULL;
67 :
68 : try
69 : {
70 0 : uno::Reference< container::XChild > xChildModel( xModel, uno::UNO_QUERY );
71 0 : if ( xChildModel.is() )
72 : {
73 0 : uno::Reference< lang::XUnoTunnel > xParentTunnel( xChildModel->getParent(), uno::UNO_QUERY );
74 0 : if ( xParentTunnel.is() )
75 : {
76 0 : SvGlobalName aSfxIdent( SFX_GLOBAL_CLASSID );
77 : pResult = reinterpret_cast< SfxObjectShell * >(
78 0 : xParentTunnel->getSomething( uno::Sequence< sal_Int8 >( aSfxIdent.GetByteSequence() ) ) );
79 0 : }
80 0 : }
81 : }
82 0 : catch( const uno::Exception& )
83 : {
84 : // TODO: error handling
85 : }
86 :
87 0 : return pResult;
88 : }
89 :
90 : // this code is copied from sfx2/source/doc/objembed.cxx. It is a workaround to
91 : // get the reference device (e.g. printer) fromthe parent document
92 0 : OutputDevice * lcl_GetParentRefDevice( const uno::Reference< frame::XModel > & xModel )
93 : {
94 0 : SfxObjectShell * pParent = lcl_GetParentObjectShell( xModel );
95 0 : if ( pParent )
96 0 : return pParent->GetDocumentRefDev();
97 0 : return NULL;
98 : }
99 :
100 : } // anonymous namespace
101 :
102 : namespace chart
103 : {
104 :
105 0 : DrawModelWrapper::DrawModelWrapper( const uno::Reference<uno::XComponentContext>& /*xContext*/ )
106 0 : : SdrModel( SvtPathOptions().GetPalettePath(), NULL, NULL, false, false )
107 : , m_pChartItemPool(0)
108 : , m_xMainDrawPage(0)
109 0 : , m_xHiddenDrawPage(0)
110 : {
111 0 : m_pChartItemPool = ChartItemPool::CreateChartItemPool();
112 :
113 0 : SetScaleUnit(MAP_100TH_MM);
114 0 : SetScaleFraction(Fraction(1, 1));
115 0 : SetDefaultFontHeight(423); // 12pt
116 :
117 0 : SfxItemPool* pMasterPool = &GetItemPool();
118 0 : pMasterPool->SetDefaultMetric(SFX_MAPUNIT_100TH_MM);
119 0 : pMasterPool->SetPoolDefaultItem(SfxBoolItem(EE_PARA_HYPHENATE, true) );
120 0 : pMasterPool->SetPoolDefaultItem(Svx3DPercentDiagonalItem (5));
121 :
122 0 : SfxItemPool* pPool = pMasterPool;
123 : // append chart pool to end of pool chain
124 : for (;;)
125 : {
126 0 : SfxItemPool* pSecondary = pPool->GetSecondaryPool();
127 0 : if (!pSecondary)
128 0 : break;
129 :
130 0 : pPool = pSecondary;
131 0 : }
132 0 : pPool->SetSecondaryPool(m_pChartItemPool);
133 0 : pMasterPool->FreezeIdRanges();
134 :
135 : //this factory needs to be created before first use of 3D scenes once upon an office runtime
136 : //@todo in future this should be done by drawing engine itself on demand
137 : static bool b3dFactoryInitialized = false;
138 0 : if(!b3dFactoryInitialized)
139 : {
140 0 : E3dObjFactory aObjFactory;
141 0 : b3dFactoryInitialized = true;
142 : }
143 :
144 : //Hyphenyation and spellchecking
145 0 : SdrOutliner& rOutliner = GetDrawOutliner();
146 : try
147 : {
148 0 : uno::Reference< linguistic2::XHyphenator > xHyphenator( LinguMgr::GetHyphenator() );
149 0 : if( xHyphenator.is() )
150 0 : rOutliner.SetHyphenator( xHyphenator );
151 :
152 0 : uno::Reference< linguistic2::XSpellChecker1 > xSpellChecker( LinguMgr::GetSpellChecker() );
153 0 : if ( xSpellChecker.is() )
154 0 : rOutliner.SetSpeller( xSpellChecker );
155 : }
156 0 : catch(...)
157 : {
158 : OSL_FAIL("Can't get Hyphenator or SpellChecker for chart");
159 : }
160 :
161 : //ref device for font rendering
162 0 : OutputDevice* pDefaultDevice = rOutliner.GetRefDevice();
163 0 : if( !pDefaultDevice )
164 0 : pDefaultDevice = Application::GetDefaultDevice();
165 0 : m_pRefDevice.reset(new VirtualDevice(*pDefaultDevice));
166 0 : MapMode aMapMode = m_pRefDevice->GetMapMode();
167 0 : aMapMode.SetMapUnit(MAP_100TH_MM);
168 0 : m_pRefDevice->SetMapMode(aMapMode);
169 0 : SetRefDevice(m_pRefDevice.get());
170 0 : rOutliner.SetRefDevice(m_pRefDevice.get());
171 0 : }
172 :
173 0 : DrawModelWrapper::~DrawModelWrapper()
174 : {
175 : //remove m_pChartItemPool from pool chain
176 0 : if(m_pChartItemPool)
177 : {
178 0 : SfxItemPool* pPool = &GetItemPool();
179 : for (;;)
180 : {
181 0 : SfxItemPool* pSecondary = pPool->GetSecondaryPool();
182 0 : if(pSecondary == m_pChartItemPool)
183 : {
184 0 : pPool->SetSecondaryPool (NULL);
185 0 : break;
186 : }
187 0 : pPool = pSecondary;
188 0 : }
189 0 : SfxItemPool::Free(m_pChartItemPool);
190 : }
191 0 : }
192 :
193 0 : uno::Reference< uno::XInterface > DrawModelWrapper
194 : ::createUnoModel()
195 : {
196 0 : 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
197 0 : return uno::Reference< uno::XInterface >::query( xComponent );
198 : }
199 :
200 0 : uno::Reference< frame::XModel > DrawModelWrapper::getUnoModel()
201 : {
202 0 : uno::Reference< uno::XInterface > xI = this->SdrModel::getUnoModel();
203 0 : return uno::Reference<frame::XModel>::query( xI );
204 : }
205 :
206 0 : SdrModel& DrawModelWrapper::getSdrModel()
207 : {
208 0 : return *this;
209 : }
210 :
211 0 : uno::Reference< lang::XMultiServiceFactory > DrawModelWrapper::getShapeFactory()
212 : {
213 0 : uno::Reference< lang::XMultiServiceFactory > xShapeFactory( this->getUnoModel(), uno::UNO_QUERY );
214 0 : return xShapeFactory;
215 : }
216 :
217 0 : uno::Reference< drawing::XDrawPage > DrawModelWrapper::getMainDrawPage()
218 : {
219 : //create draw page:
220 0 : if( !m_xMainDrawPage.is() )
221 : {
222 0 : uno::Reference< drawing::XDrawPagesSupplier > xDrawPagesSuplier( this->getUnoModel(), uno::UNO_QUERY );
223 0 : if( xDrawPagesSuplier.is() )
224 : {
225 0 : uno::Reference< drawing::XDrawPages > xDrawPages( xDrawPagesSuplier->getDrawPages () );
226 0 : if( xDrawPages->getCount()>1 )
227 : {
228 0 : uno::Any aPage = xDrawPages->getByIndex( 0 ) ;
229 0 : aPage >>= m_xMainDrawPage;
230 : }
231 0 : if(!m_xMainDrawPage.is())
232 : {
233 0 : m_xMainDrawPage = xDrawPages->insertNewByIndex( 0 );
234 0 : }
235 0 : }
236 : }
237 : //ensure that additional shapes are in front of the chart objects so create the chart root before
238 : // let us disable this call for now
239 : // TODO:moggi
240 : // AbstractShapeFactory::getOrCreateShapeFactory(this->getShapeFactory())->getOrCreateChartRootShape( m_xMainDrawPage );
241 0 : return m_xMainDrawPage;
242 : }
243 0 : uno::Reference< drawing::XDrawPage > DrawModelWrapper::getHiddenDrawPage()
244 : {
245 0 : if( !m_xHiddenDrawPage.is() )
246 : {
247 0 : uno::Reference< drawing::XDrawPagesSupplier > xDrawPagesSuplier( this->getUnoModel(), uno::UNO_QUERY );
248 0 : if( xDrawPagesSuplier.is() )
249 : {
250 0 : uno::Reference< drawing::XDrawPages > xDrawPages( xDrawPagesSuplier->getDrawPages () );
251 0 : if( xDrawPages->getCount()>1 )
252 : {
253 0 : uno::Any aPage = xDrawPages->getByIndex( 1 ) ;
254 0 : aPage >>= m_xHiddenDrawPage;
255 : }
256 :
257 0 : if(!m_xHiddenDrawPage.is())
258 : {
259 0 : if( xDrawPages->getCount()==0 )
260 0 : m_xMainDrawPage = xDrawPages->insertNewByIndex( 0 );
261 0 : m_xHiddenDrawPage = xDrawPages->insertNewByIndex( 1 );
262 0 : }
263 0 : }
264 : }
265 0 : return m_xHiddenDrawPage;
266 : }
267 0 : void DrawModelWrapper::clearMainDrawPage()
268 : {
269 : //uno::Reference<drawing::XShapes> xChartRoot( m_xMainDrawPage, uno::UNO_QUERY );
270 0 : uno::Reference<drawing::XShapes> xChartRoot( AbstractShapeFactory::getChartRootShape( m_xMainDrawPage ) );
271 0 : if( xChartRoot.is() )
272 : {
273 0 : sal_Int32 nSubCount = xChartRoot->getCount();
274 0 : uno::Reference< drawing::XShape > xShape;
275 0 : for( sal_Int32 nS = nSubCount; nS--; )
276 : {
277 0 : if( xChartRoot->getByIndex( nS ) >>= xShape )
278 0 : xChartRoot->remove( xShape );
279 0 : }
280 0 : }
281 0 : }
282 :
283 0 : uno::Reference< drawing::XShapes > DrawModelWrapper::getChartRootShape(
284 : const uno::Reference< drawing::XDrawPage>& xDrawPage )
285 : {
286 0 : return AbstractShapeFactory::getChartRootShape( xDrawPage );
287 : }
288 :
289 0 : void DrawModelWrapper::lockControllers()
290 : {
291 0 : uno::Reference< frame::XModel > xDrawModel( this->getUnoModel() );
292 0 : if( xDrawModel.is())
293 0 : xDrawModel->lockControllers();
294 0 : }
295 0 : void DrawModelWrapper::unlockControllers()
296 : {
297 0 : uno::Reference< frame::XModel > xDrawModel( this->getUnoModel() );
298 0 : if( xDrawModel.is())
299 0 : xDrawModel->unlockControllers();
300 0 : }
301 :
302 0 : void DrawModelWrapper::attachParentReferenceDevice( const uno::Reference< frame::XModel > & xChartModel )
303 : {
304 0 : OutputDevice * pParentRefDev( lcl_GetParentRefDevice( xChartModel ));
305 0 : if( pParentRefDev )
306 : {
307 0 : SetRefDevice( pParentRefDev );
308 : }
309 0 : }
310 :
311 0 : OutputDevice* DrawModelWrapper::getReferenceDevice() const
312 : {
313 0 : return SdrModel::GetRefDevice();
314 : }
315 :
316 0 : SfxItemPool& DrawModelWrapper::GetItemPool()
317 : {
318 0 : return this->SdrModel::GetItemPool();
319 : }
320 0 : const SfxItemPool& DrawModelWrapper::GetItemPool() const
321 : {
322 0 : return this->SdrModel::GetItemPool();
323 : }
324 0 : XColorListRef DrawModelWrapper::GetColorList() const
325 : {
326 0 : return this->SdrModel::GetColorList();
327 : }
328 0 : XDashListRef DrawModelWrapper::GetDashList() const
329 : {
330 0 : return this->SdrModel::GetDashList();
331 : }
332 0 : XLineEndListRef DrawModelWrapper::GetLineEndList() const
333 : {
334 0 : return this->SdrModel::GetLineEndList();
335 : }
336 0 : XGradientListRef DrawModelWrapper::GetGradientList() const
337 : {
338 0 : return this->SdrModel::GetGradientList();
339 : }
340 0 : XHatchListRef DrawModelWrapper::GetHatchList() const
341 : {
342 0 : return this->SdrModel::GetHatchList();
343 : }
344 0 : XBitmapListRef DrawModelWrapper::GetBitmapList() const
345 : {
346 0 : return this->SdrModel::GetBitmapList();
347 : }
348 :
349 0 : SdrObject* DrawModelWrapper::getNamedSdrObject( const OUString& rName )
350 : {
351 0 : if( rName.isEmpty() )
352 0 : return 0;
353 0 : return getNamedSdrObject( rName, GetPage(0) );
354 : }
355 :
356 0 : SdrObject* DrawModelWrapper::getNamedSdrObject( const OUString& rObjectCID, SdrObjList* pSearchList )
357 : {
358 0 : if(!pSearchList || rObjectCID.isEmpty())
359 0 : return 0;
360 0 : sal_uLong nCount = pSearchList->GetObjCount();
361 0 : for( sal_uLong nN=0; nN<nCount; nN++ )
362 : {
363 0 : SdrObject* pObj = pSearchList->GetObj(nN);
364 0 : if(!pObj)
365 0 : continue;
366 0 : if( ObjectIdentifier::areIdenticalObjects( rObjectCID, pObj->GetName() ) )
367 0 : return pObj;
368 0 : pObj = DrawModelWrapper::getNamedSdrObject( rObjectCID, pObj->GetSubList() );
369 0 : if(pObj)
370 0 : return pObj;
371 : }
372 0 : return 0;
373 : }
374 :
375 0 : bool DrawModelWrapper::removeShape( const uno::Reference< drawing::XShape >& xShape )
376 : {
377 0 : uno::Reference< container::XChild > xChild( xShape, uno::UNO_QUERY );
378 0 : if( xChild.is() )
379 : {
380 0 : uno::Reference<drawing::XShapes> xShapes( xChild->getParent(), uno::UNO_QUERY );
381 0 : if( xShapes.is() )
382 : {
383 0 : xShapes->remove(xShape);
384 0 : return true;
385 0 : }
386 : }
387 0 : return false;
388 : }
389 :
390 : } //namespace chart
391 :
392 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|