xref: /AOO41X/main/sc/source/core/tool/charthelper.cxx (revision 17e36312ee44e3838ff672103ce90e6b75cbd9fb) !
1b3f79822SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3b3f79822SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4b3f79822SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5b3f79822SAndrew Rist  * distributed with this work for additional information
6b3f79822SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7b3f79822SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8b3f79822SAndrew Rist  * "License"); you may not use this file except in compliance
9b3f79822SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11b3f79822SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13b3f79822SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14b3f79822SAndrew Rist  * software distributed under the License is distributed on an
15b3f79822SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16b3f79822SAndrew Rist  * KIND, either express or implied.  See the License for the
17b3f79822SAndrew Rist  * specific language governing permissions and limitations
18b3f79822SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20b3f79822SAndrew Rist  *************************************************************/
21b3f79822SAndrew Rist 
22b3f79822SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_sc.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "charthelper.hxx"
28cdf0e10cSrcweir #include "document.hxx"
29cdf0e10cSrcweir #include "drwlayer.hxx"
30cdf0e10cSrcweir #include "rangelst.hxx"
31cdf0e10cSrcweir #include "chartlis.hxx"
32cdf0e10cSrcweir #include "docuno.hxx"
33cdf0e10cSrcweir 
34cdf0e10cSrcweir //#include <vcl/svapp.hxx>
35cdf0e10cSrcweir #include <svx/svditer.hxx>
36cdf0e10cSrcweir #include <svx/svdoole2.hxx>
37cdf0e10cSrcweir #include <svx/svdpage.hxx>
38cdf0e10cSrcweir 
39cdf0e10cSrcweir #include <com/sun/star/chart2/data/XDataReceiver.hpp>
40*17e36312SWang Lei #include <com/sun/star/util/XModifiable.hpp>
41cdf0e10cSrcweir 
42cdf0e10cSrcweir using namespace com::sun::star;
43cdf0e10cSrcweir using ::com::sun::star::uno::Reference;
44cdf0e10cSrcweir 
45cdf0e10cSrcweir 
46cdf0e10cSrcweir // ====================================================================
47cdf0e10cSrcweir 
48cdf0e10cSrcweir namespace
49cdf0e10cSrcweir {
50cdf0e10cSrcweir 
51cdf0e10cSrcweir 
lcl_DoUpdateCharts(const ScAddress & rPos,ScDocument * pDoc,sal_Bool bAllCharts)52cdf0e10cSrcweir sal_uInt16 lcl_DoUpdateCharts( const ScAddress& rPos, ScDocument* pDoc, sal_Bool bAllCharts )
53cdf0e10cSrcweir {
54cdf0e10cSrcweir 	ScDrawLayer* pModel = pDoc->GetDrawLayer();
55cdf0e10cSrcweir 	if (!pModel)
56cdf0e10cSrcweir 		return 0;
57cdf0e10cSrcweir 
58cdf0e10cSrcweir 	sal_uInt16 nFound = 0;
59cdf0e10cSrcweir 
60cdf0e10cSrcweir 	sal_uInt16 nPageCount = pModel->GetPageCount();
61cdf0e10cSrcweir 	for (sal_uInt16 nPageNo=0; nPageNo<nPageCount; nPageNo++)
62cdf0e10cSrcweir 	{
63cdf0e10cSrcweir 		SdrPage* pPage = pModel->GetPage(nPageNo);
64cdf0e10cSrcweir 		DBG_ASSERT(pPage,"Page ?");
65cdf0e10cSrcweir 
66cdf0e10cSrcweir 		SdrObjListIter aIter( *pPage, IM_DEEPNOGROUPS );
67cdf0e10cSrcweir 		SdrObject* pObject = aIter.Next();
68cdf0e10cSrcweir 		while (pObject)
69cdf0e10cSrcweir 		{
70cdf0e10cSrcweir             if ( pObject->GetObjIdentifier() == OBJ_OLE2 && pDoc->IsChart( pObject ) )
71cdf0e10cSrcweir 			{
72cdf0e10cSrcweir                 String aName = ((SdrOle2Obj*)pObject)->GetPersistName();
73cdf0e10cSrcweir                 sal_Bool bHit = sal_True;
74cdf0e10cSrcweir                 if ( !bAllCharts )
75cdf0e10cSrcweir                 {
76cdf0e10cSrcweir                     ScRangeList aRanges;
77cdf0e10cSrcweir                     sal_Bool bColHeaders = sal_False;
78cdf0e10cSrcweir                     sal_Bool bRowHeaders = sal_False;
79cdf0e10cSrcweir                     pDoc->GetOldChartParameters( aName, aRanges, bColHeaders, bRowHeaders );
80cdf0e10cSrcweir                     bHit = aRanges.In( rPos );
81cdf0e10cSrcweir                 }
82cdf0e10cSrcweir                 if ( bHit )
83cdf0e10cSrcweir                 {
84cdf0e10cSrcweir                     pDoc->UpdateChart( aName );
85cdf0e10cSrcweir                     ++nFound;
86cdf0e10cSrcweir                 }
87cdf0e10cSrcweir 			}
88cdf0e10cSrcweir 			pObject = aIter.Next();
89cdf0e10cSrcweir 		}
90cdf0e10cSrcweir 	}
91cdf0e10cSrcweir 	return nFound;
92cdf0e10cSrcweir }
93cdf0e10cSrcweir 
lcl_AdjustRanges(ScRangeList & rRanges,SCTAB nSourceTab,SCTAB nDestTab,SCTAB nTabCount)94cdf0e10cSrcweir sal_Bool lcl_AdjustRanges( ScRangeList& rRanges, SCTAB nSourceTab, SCTAB nDestTab, SCTAB nTabCount )
95cdf0e10cSrcweir {
96cdf0e10cSrcweir 	//!	if multiple sheets are copied, update references into the other copied sheets?
97cdf0e10cSrcweir 
98cdf0e10cSrcweir 	sal_Bool bChanged = sal_False;
99cdf0e10cSrcweir 
100cdf0e10cSrcweir 	sal_uLong nCount = rRanges.Count();
101cdf0e10cSrcweir 	for (sal_uLong i=0; i<nCount; i++)
102cdf0e10cSrcweir 	{
103cdf0e10cSrcweir 		ScRange* pRange = rRanges.GetObject(i);
104cdf0e10cSrcweir 		if ( pRange->aStart.Tab() == nSourceTab && pRange->aEnd.Tab() == nSourceTab )
105cdf0e10cSrcweir 		{
106cdf0e10cSrcweir 			pRange->aStart.SetTab( nDestTab );
107cdf0e10cSrcweir 			pRange->aEnd.SetTab( nDestTab );
108cdf0e10cSrcweir 			bChanged = sal_True;
109cdf0e10cSrcweir 		}
110cdf0e10cSrcweir 		if ( pRange->aStart.Tab() >= nTabCount )
111cdf0e10cSrcweir 		{
112cdf0e10cSrcweir 			pRange->aStart.SetTab( nTabCount > 0 ? ( nTabCount - 1 ) : 0 );
113cdf0e10cSrcweir 			bChanged = sal_True;
114cdf0e10cSrcweir 		}
115cdf0e10cSrcweir 		if ( pRange->aEnd.Tab() >= nTabCount )
116cdf0e10cSrcweir 		{
117cdf0e10cSrcweir 			pRange->aEnd.SetTab( nTabCount > 0 ? ( nTabCount - 1 ) : 0 );
118cdf0e10cSrcweir 			bChanged = sal_True;
119cdf0e10cSrcweir 		}
120cdf0e10cSrcweir 	}
121cdf0e10cSrcweir 
122cdf0e10cSrcweir 	return bChanged;
123cdf0e10cSrcweir }
124cdf0e10cSrcweir 
125cdf0e10cSrcweir }//end anonymous namespace
126cdf0e10cSrcweir 
127cdf0e10cSrcweir // === ScChartHelper ======================================
128cdf0e10cSrcweir 
129cdf0e10cSrcweir //static
DoUpdateCharts(const ScAddress & rPos,ScDocument * pDoc)130cdf0e10cSrcweir sal_uInt16 ScChartHelper::DoUpdateCharts( const ScAddress& rPos, ScDocument* pDoc )
131cdf0e10cSrcweir {
132cdf0e10cSrcweir     return lcl_DoUpdateCharts( rPos, pDoc, sal_False );
133cdf0e10cSrcweir }
134cdf0e10cSrcweir 
135cdf0e10cSrcweir //static
DoUpdateAllCharts(ScDocument * pDoc)136cdf0e10cSrcweir sal_uInt16 ScChartHelper::DoUpdateAllCharts( ScDocument* pDoc )
137cdf0e10cSrcweir {
138cdf0e10cSrcweir     return lcl_DoUpdateCharts( ScAddress(), pDoc, sal_True );
139cdf0e10cSrcweir }
140cdf0e10cSrcweir 
141cdf0e10cSrcweir //static
AdjustRangesOfChartsOnDestinationPage(ScDocument * pSrcDoc,ScDocument * pDestDoc,const SCTAB nSrcTab,const SCTAB nDestTab)142cdf0e10cSrcweir void ScChartHelper::AdjustRangesOfChartsOnDestinationPage( ScDocument* pSrcDoc, ScDocument* pDestDoc, const SCTAB nSrcTab, const SCTAB nDestTab )
143cdf0e10cSrcweir {
144cdf0e10cSrcweir     if( !pSrcDoc || !pDestDoc )
145cdf0e10cSrcweir         return;
146cdf0e10cSrcweir     ScDrawLayer* pDrawLayer = pDestDoc->GetDrawLayer();
147cdf0e10cSrcweir     if( !pDrawLayer )
148cdf0e10cSrcweir         return;
149cdf0e10cSrcweir 
150cdf0e10cSrcweir     SdrPage* pDestPage = pDrawLayer->GetPage(static_cast<sal_uInt16>(nDestTab));
151cdf0e10cSrcweir     if( pDestPage )
152cdf0e10cSrcweir     {
153cdf0e10cSrcweir         SdrObjListIter aIter( *pDestPage, IM_FLAT );
154cdf0e10cSrcweir         SdrObject* pObject = aIter.Next();
155cdf0e10cSrcweir         while( pObject )
156cdf0e10cSrcweir         {
157cdf0e10cSrcweir             if( pObject->GetObjIdentifier() == OBJ_OLE2 && ((SdrOle2Obj*)pObject)->IsChart() )
158cdf0e10cSrcweir             {
159cdf0e10cSrcweir                 String aChartName = ((SdrOle2Obj*)pObject)->GetPersistName();
160cdf0e10cSrcweir 
161cdf0e10cSrcweir                 Reference< chart2::XChartDocument > xChartDoc( pDestDoc->GetChartByName( aChartName ) );
162cdf0e10cSrcweir                 Reference< chart2::data::XDataReceiver > xReceiver( xChartDoc, uno::UNO_QUERY );
163cdf0e10cSrcweir                 if( xChartDoc.is() && xReceiver.is() && !xChartDoc->hasInternalDataProvider() )
164cdf0e10cSrcweir                 {
165cdf0e10cSrcweir                     ::std::vector< ScRangeList > aRangesVector;
166cdf0e10cSrcweir                     pDestDoc->GetChartRanges( aChartName, aRangesVector, pSrcDoc );
167cdf0e10cSrcweir 
168cdf0e10cSrcweir                     ::std::vector< ScRangeList >::iterator aIt( aRangesVector.begin() );
169cdf0e10cSrcweir                     for( ; aIt!=aRangesVector.end(); aIt++ )
170cdf0e10cSrcweir                     {
171cdf0e10cSrcweir                         ScRangeList& rScRangeList( *aIt );
172cdf0e10cSrcweir                         lcl_AdjustRanges( rScRangeList, nSrcTab, nDestTab, pDestDoc->GetTableCount() );
173cdf0e10cSrcweir                     }
174cdf0e10cSrcweir                     pDestDoc->SetChartRanges( aChartName, aRangesVector );
175cdf0e10cSrcweir                 }
176cdf0e10cSrcweir             }
177cdf0e10cSrcweir             pObject = aIter.Next();
178cdf0e10cSrcweir         }
179cdf0e10cSrcweir     }
180cdf0e10cSrcweir }
181cdf0e10cSrcweir 
182cdf0e10cSrcweir //static
UpdateChartsOnDestinationPage(ScDocument * pDestDoc,const SCTAB nDestTab)183*17e36312SWang Lei void ScChartHelper::UpdateChartsOnDestinationPage( ScDocument* pDestDoc, const SCTAB nDestTab )
184*17e36312SWang Lei {
185*17e36312SWang Lei 	if( !pDestDoc )
186*17e36312SWang Lei         return;
187*17e36312SWang Lei     ScDrawLayer* pDrawLayer = pDestDoc->GetDrawLayer();
188*17e36312SWang Lei     if( !pDrawLayer )
189*17e36312SWang Lei         return;
190*17e36312SWang Lei 
191*17e36312SWang Lei     SdrPage* pDestPage = pDrawLayer->GetPage(static_cast<sal_uInt16>(nDestTab));
192*17e36312SWang Lei     if( pDestPage )
193*17e36312SWang Lei     {
194*17e36312SWang Lei         SdrObjListIter aIter( *pDestPage, IM_FLAT );
195*17e36312SWang Lei         SdrObject* pObject = aIter.Next();
196*17e36312SWang Lei         while( pObject )
197*17e36312SWang Lei         {
198*17e36312SWang Lei             if( pObject->GetObjIdentifier() == OBJ_OLE2 && ((SdrOle2Obj*)pObject)->IsChart() )
199*17e36312SWang Lei             {
200*17e36312SWang Lei                 String aChartName = ((SdrOle2Obj*)pObject)->GetPersistName();
201*17e36312SWang Lei                 Reference< chart2::XChartDocument > xChartDoc( pDestDoc->GetChartByName( aChartName ) );
202*17e36312SWang Lei 				Reference< util::XModifiable > xModif(xChartDoc, uno::UNO_QUERY_THROW);
203*17e36312SWang Lei 				xModif->setModified( sal_True);
204*17e36312SWang Lei 			}
205*17e36312SWang Lei 			pObject = aIter.Next();
206*17e36312SWang Lei 		}
207*17e36312SWang Lei 	}
208*17e36312SWang Lei }
209*17e36312SWang Lei 
210*17e36312SWang Lei //static
GetChartFromSdrObject(SdrObject * pObject)211cdf0e10cSrcweir uno::Reference< chart2::XChartDocument > ScChartHelper::GetChartFromSdrObject( SdrObject* pObject )
212cdf0e10cSrcweir {
213cdf0e10cSrcweir     uno::Reference< chart2::XChartDocument > xReturn;
214cdf0e10cSrcweir     if( pObject )
215cdf0e10cSrcweir     {
216cdf0e10cSrcweir         if( pObject->GetObjIdentifier() == OBJ_OLE2 && ((SdrOle2Obj*)pObject)->IsChart() )
217cdf0e10cSrcweir         {
218cdf0e10cSrcweir             uno::Reference< embed::XEmbeddedObject > xIPObj = ((SdrOle2Obj*)pObject)->GetObjRef();
219cdf0e10cSrcweir             if( xIPObj.is() )
220cdf0e10cSrcweir             {
221cdf0e10cSrcweir                 svt::EmbeddedObjectRef::TryRunningState( xIPObj );
222cdf0e10cSrcweir                 uno::Reference< util::XCloseable > xComponent = xIPObj->getComponent();
223cdf0e10cSrcweir                 xReturn.set( uno::Reference< chart2::XChartDocument >( xComponent, uno::UNO_QUERY ) );
224cdf0e10cSrcweir             }
225cdf0e10cSrcweir         }
226cdf0e10cSrcweir     }
227cdf0e10cSrcweir     return xReturn;
228cdf0e10cSrcweir }
229cdf0e10cSrcweir 
GetChartRanges(const uno::Reference<chart2::XChartDocument> & xChartDoc,uno::Sequence<rtl::OUString> & rRanges)230cdf0e10cSrcweir void ScChartHelper::GetChartRanges( const uno::Reference< chart2::XChartDocument >& xChartDoc,
231cdf0e10cSrcweir             uno::Sequence< rtl::OUString >& rRanges )
232cdf0e10cSrcweir {
233cdf0e10cSrcweir     rRanges.realloc(0);
234cdf0e10cSrcweir     uno::Reference< chart2::data::XDataSource > xDataSource( xChartDoc, uno::UNO_QUERY );
235cdf0e10cSrcweir     if( !xDataSource.is() )
236cdf0e10cSrcweir         return;
237cdf0e10cSrcweir     //uno::Reference< chart2::data::XDataProvider > xProvider = xChartDoc->getDataProvider();
238cdf0e10cSrcweir 
239cdf0e10cSrcweir     uno::Sequence< uno::Reference< chart2::data::XLabeledDataSequence > > aLabeledDataSequences( xDataSource->getDataSequences() );
240cdf0e10cSrcweir     rRanges.realloc(2*aLabeledDataSequences.getLength());
241cdf0e10cSrcweir     sal_Int32 nRealCount=0;
242cdf0e10cSrcweir     for( sal_Int32 nN=0;nN<aLabeledDataSequences.getLength();nN++)
243cdf0e10cSrcweir     {
244cdf0e10cSrcweir         uno::Reference< chart2::data::XLabeledDataSequence > xLabeledSequence( aLabeledDataSequences[nN] );
245cdf0e10cSrcweir         if(!xLabeledSequence.is())
246cdf0e10cSrcweir             continue;
247cdf0e10cSrcweir         uno::Reference< chart2::data::XDataSequence > xLabel( xLabeledSequence->getLabel());
248cdf0e10cSrcweir         uno::Reference< chart2::data::XDataSequence > xValues( xLabeledSequence->getValues());
249cdf0e10cSrcweir 
250cdf0e10cSrcweir         if( xLabel.is())
251cdf0e10cSrcweir             rRanges[nRealCount++] = xLabel->getSourceRangeRepresentation();
252cdf0e10cSrcweir         if( xValues.is())
253cdf0e10cSrcweir             rRanges[nRealCount++] = xValues->getSourceRangeRepresentation();
254cdf0e10cSrcweir     }
255cdf0e10cSrcweir     rRanges.realloc(nRealCount);
256cdf0e10cSrcweir }
257cdf0e10cSrcweir 
SetChartRanges(const uno::Reference<chart2::XChartDocument> & xChartDoc,const uno::Sequence<rtl::OUString> & rRanges)258cdf0e10cSrcweir void ScChartHelper::SetChartRanges( const uno::Reference< chart2::XChartDocument >& xChartDoc,
259cdf0e10cSrcweir             const uno::Sequence< rtl::OUString >& rRanges )
260cdf0e10cSrcweir {
261cdf0e10cSrcweir     uno::Reference< chart2::data::XDataSource > xDataSource( xChartDoc, uno::UNO_QUERY );
262cdf0e10cSrcweir     if( !xDataSource.is() )
263cdf0e10cSrcweir         return;
264cdf0e10cSrcweir     uno::Reference< chart2::data::XDataProvider > xDataProvider = xChartDoc->getDataProvider();
265cdf0e10cSrcweir     if( !xDataProvider.is() )
266cdf0e10cSrcweir         return;
267cdf0e10cSrcweir 
268cdf0e10cSrcweir     uno::Reference< frame::XModel > xModel( xChartDoc, uno::UNO_QUERY );
269cdf0e10cSrcweir     if( xModel.is() )
270cdf0e10cSrcweir         xModel->lockControllers();
271cdf0e10cSrcweir 
272cdf0e10cSrcweir     try
273cdf0e10cSrcweir     {
274cdf0e10cSrcweir         rtl::OUString aPropertyNameRole( ::rtl::OUString::createFromAscii("Role") );
275cdf0e10cSrcweir 
276cdf0e10cSrcweir         uno::Sequence< uno::Reference< chart2::data::XLabeledDataSequence > > aLabeledDataSequences( xDataSource->getDataSequences() );
277cdf0e10cSrcweir         sal_Int32 nRange=0;
278cdf0e10cSrcweir         for( sal_Int32 nN=0; (nN<aLabeledDataSequences.getLength()) && (nRange<rRanges.getLength()); nN++ )
279cdf0e10cSrcweir         {
280cdf0e10cSrcweir             uno::Reference< chart2::data::XLabeledDataSequence > xLabeledSequence( aLabeledDataSequences[nN] );
281cdf0e10cSrcweir             if(!xLabeledSequence.is())
282cdf0e10cSrcweir                 continue;
283cdf0e10cSrcweir             uno::Reference< beans::XPropertySet > xLabel( xLabeledSequence->getLabel(), uno::UNO_QUERY );
284cdf0e10cSrcweir             uno::Reference< beans::XPropertySet > xValues( xLabeledSequence->getValues(), uno::UNO_QUERY );
285cdf0e10cSrcweir 
286cdf0e10cSrcweir             if( xLabel.is())
287cdf0e10cSrcweir             {
288cdf0e10cSrcweir                 // the range string must be in Calc A1 format.
289cdf0e10cSrcweir                 uno::Reference< chart2::data::XDataSequence > xNewSeq(
290cdf0e10cSrcweir                     xDataProvider->createDataSequenceByRangeRepresentation( rRanges[nRange++] ));
291cdf0e10cSrcweir 
292cdf0e10cSrcweir                 uno::Reference< beans::XPropertySet > xNewProps( xNewSeq, uno::UNO_QUERY );
293cdf0e10cSrcweir                 if( xNewProps.is() )
294cdf0e10cSrcweir                     xNewProps->setPropertyValue( aPropertyNameRole, xLabel->getPropertyValue( aPropertyNameRole ) );
295cdf0e10cSrcweir 
296cdf0e10cSrcweir                 xLabeledSequence->setLabel( xNewSeq );
297cdf0e10cSrcweir             }
298cdf0e10cSrcweir 
299cdf0e10cSrcweir             if( !(nRange<rRanges.getLength()) )
300cdf0e10cSrcweir                 break;
301cdf0e10cSrcweir 
302cdf0e10cSrcweir             if( xValues.is())
303cdf0e10cSrcweir             {
304cdf0e10cSrcweir                 // the range string must be in Calc A1 format.
305cdf0e10cSrcweir                 uno::Reference< chart2::data::XDataSequence > xNewSeq(
306cdf0e10cSrcweir                     xDataProvider->createDataSequenceByRangeRepresentation( rRanges[nRange++] ));
307cdf0e10cSrcweir 
308cdf0e10cSrcweir                 uno::Reference< beans::XPropertySet > xNewProps( xNewSeq, uno::UNO_QUERY );
309cdf0e10cSrcweir                 if( xNewProps.is() )
310cdf0e10cSrcweir                     xNewProps->setPropertyValue( aPropertyNameRole, xValues->getPropertyValue( aPropertyNameRole ) );
311cdf0e10cSrcweir 
312cdf0e10cSrcweir                 xLabeledSequence->setValues( xNewSeq );
313cdf0e10cSrcweir             }
314cdf0e10cSrcweir         }
315cdf0e10cSrcweir     }
316cdf0e10cSrcweir     catch ( uno::Exception& ex )
317cdf0e10cSrcweir     {
318cdf0e10cSrcweir         (void)ex;
319cdf0e10cSrcweir         DBG_ERROR("Exception in ScChartHelper::SetChartRanges - invalid range string?");
320cdf0e10cSrcweir     }
321cdf0e10cSrcweir 
322cdf0e10cSrcweir     if( xModel.is() )
323cdf0e10cSrcweir         xModel->unlockControllers();
324cdf0e10cSrcweir }
325cdf0e10cSrcweir 
AddRangesIfProtectedChart(ScRangeListVector & rRangesVector,ScDocument * pDocument,SdrObject * pObject)326cdf0e10cSrcweir void ScChartHelper::AddRangesIfProtectedChart( ScRangeListVector& rRangesVector, ScDocument* pDocument, SdrObject* pObject )
327cdf0e10cSrcweir {
328cdf0e10cSrcweir     if ( pDocument && pObject && ( pObject->GetObjIdentifier() == OBJ_OLE2 ) )
329cdf0e10cSrcweir     {
330cdf0e10cSrcweir         SdrOle2Obj* pSdrOle2Obj = dynamic_cast< SdrOle2Obj* >( pObject );
331cdf0e10cSrcweir         if ( pSdrOle2Obj && pSdrOle2Obj->IsChart() )
332cdf0e10cSrcweir         {
333cdf0e10cSrcweir             uno::Reference< embed::XEmbeddedObject > xEmbeddedObj = pSdrOle2Obj->GetObjRef();
334cdf0e10cSrcweir             if ( xEmbeddedObj.is() )
335cdf0e10cSrcweir             {
336cdf0e10cSrcweir                 bool bDisableDataTableDialog = false;
337cdf0e10cSrcweir                 sal_Int32 nOldState = xEmbeddedObj->getCurrentState();
338cdf0e10cSrcweir                 svt::EmbeddedObjectRef::TryRunningState( xEmbeddedObj );
339cdf0e10cSrcweir                 uno::Reference< beans::XPropertySet > xProps( xEmbeddedObj->getComponent(), uno::UNO_QUERY );
340cdf0e10cSrcweir                 if ( xProps.is() &&
341cdf0e10cSrcweir                      ( xProps->getPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "DisableDataTableDialog" ) ) ) >>= bDisableDataTableDialog ) &&
342cdf0e10cSrcweir                      bDisableDataTableDialog )
343cdf0e10cSrcweir                 {
344cdf0e10cSrcweir                     ::rtl::OUString aChartName = pSdrOle2Obj->GetPersistName();
345cdf0e10cSrcweir                     ScRange aEmptyRange;
346cdf0e10cSrcweir                     ScChartListener aSearcher( aChartName, pDocument, aEmptyRange );
347cdf0e10cSrcweir                     sal_uInt16 nIndex = 0;
348cdf0e10cSrcweir                     ScChartListenerCollection* pCollection = pDocument->GetChartListenerCollection();
349cdf0e10cSrcweir                     if ( pCollection && pCollection->Search( &aSearcher, nIndex ) )
350cdf0e10cSrcweir                     {
351cdf0e10cSrcweir                         ScChartListener* pListener = static_cast< ScChartListener* >( pCollection->At( nIndex ) );
352cdf0e10cSrcweir                         if ( pListener )
353cdf0e10cSrcweir                         {
354cdf0e10cSrcweir                             const ScRangeListRef& rRangeList = pListener->GetRangeList();
355cdf0e10cSrcweir                             if ( rRangeList.Is() )
356cdf0e10cSrcweir                             {
357cdf0e10cSrcweir                                 rRangesVector.push_back( *rRangeList );
358cdf0e10cSrcweir                             }
359cdf0e10cSrcweir                         }
360cdf0e10cSrcweir                     }
361cdf0e10cSrcweir                 }
362cdf0e10cSrcweir                 if ( xEmbeddedObj->getCurrentState() != nOldState )
363cdf0e10cSrcweir                 {
364cdf0e10cSrcweir                     xEmbeddedObj->changeState( nOldState );
365cdf0e10cSrcweir                 }
366cdf0e10cSrcweir             }
367cdf0e10cSrcweir         }
368cdf0e10cSrcweir     }
369cdf0e10cSrcweir }
370cdf0e10cSrcweir 
FillProtectedChartRangesVector(ScRangeListVector & rRangesVector,ScDocument * pDocument,SdrPage * pPage)371cdf0e10cSrcweir void ScChartHelper::FillProtectedChartRangesVector( ScRangeListVector& rRangesVector, ScDocument* pDocument, SdrPage* pPage )
372cdf0e10cSrcweir {
373cdf0e10cSrcweir     if ( pDocument && pPage )
374cdf0e10cSrcweir     {
375cdf0e10cSrcweir         SdrObjListIter aIter( *pPage, IM_DEEPNOGROUPS );
376cdf0e10cSrcweir         SdrObject* pObject = aIter.Next();
377cdf0e10cSrcweir         while ( pObject )
378cdf0e10cSrcweir         {
379cdf0e10cSrcweir             AddRangesIfProtectedChart( rRangesVector, pDocument, pObject );
380cdf0e10cSrcweir             pObject = aIter.Next();
381cdf0e10cSrcweir         }
382cdf0e10cSrcweir     }
383cdf0e10cSrcweir }
384cdf0e10cSrcweir 
GetChartNames(::std::vector<::rtl::OUString> & rChartNames,SdrPage * pPage)385cdf0e10cSrcweir void ScChartHelper::GetChartNames( ::std::vector< ::rtl::OUString >& rChartNames, SdrPage* pPage )
386cdf0e10cSrcweir {
387cdf0e10cSrcweir     if ( pPage )
388cdf0e10cSrcweir     {
389cdf0e10cSrcweir         SdrObjListIter aIter( *pPage, IM_DEEPNOGROUPS );
390cdf0e10cSrcweir         SdrObject* pObject = aIter.Next();
391cdf0e10cSrcweir         while ( pObject )
392cdf0e10cSrcweir         {
393cdf0e10cSrcweir             if ( pObject->GetObjIdentifier() == OBJ_OLE2 )
394cdf0e10cSrcweir             {
395cdf0e10cSrcweir                 SdrOle2Obj* pSdrOle2Obj = dynamic_cast< SdrOle2Obj* >( pObject );
396cdf0e10cSrcweir                 if ( pSdrOle2Obj && pSdrOle2Obj->IsChart() )
397cdf0e10cSrcweir                 {
398cdf0e10cSrcweir                     rChartNames.push_back( pSdrOle2Obj->GetPersistName() );
399cdf0e10cSrcweir                 }
400cdf0e10cSrcweir             }
401cdf0e10cSrcweir             pObject = aIter.Next();
402cdf0e10cSrcweir         }
403cdf0e10cSrcweir     }
404cdf0e10cSrcweir }
405cdf0e10cSrcweir 
CreateProtectedChartListenersAndNotify(ScDocument * pDoc,SdrPage * pPage,ScModelObj * pModelObj,SCTAB nTab,const ScRangeListVector & rRangesVector,const::std::vector<::rtl::OUString> & rExcludedChartNames,bool bSameDoc)406cdf0e10cSrcweir void ScChartHelper::CreateProtectedChartListenersAndNotify( ScDocument* pDoc, SdrPage* pPage, ScModelObj* pModelObj, SCTAB nTab,
407cdf0e10cSrcweir     const ScRangeListVector& rRangesVector, const ::std::vector< ::rtl::OUString >& rExcludedChartNames, bool bSameDoc )
408cdf0e10cSrcweir {
409cdf0e10cSrcweir     if ( pDoc && pPage && pModelObj )
410cdf0e10cSrcweir     {
411cdf0e10cSrcweir         size_t nRangeListCount = rRangesVector.size();
412cdf0e10cSrcweir         size_t nRangeList = 0;
413cdf0e10cSrcweir         SdrObjListIter aIter( *pPage, IM_DEEPNOGROUPS );
414cdf0e10cSrcweir         SdrObject* pObject = aIter.Next();
415cdf0e10cSrcweir         while ( pObject )
416cdf0e10cSrcweir         {
417cdf0e10cSrcweir             if ( pObject->GetObjIdentifier() == OBJ_OLE2 )
418cdf0e10cSrcweir             {
419cdf0e10cSrcweir                 SdrOle2Obj* pSdrOle2Obj = dynamic_cast< SdrOle2Obj* >( pObject );
420cdf0e10cSrcweir                 if ( pSdrOle2Obj && pSdrOle2Obj->IsChart() )
421cdf0e10cSrcweir                 {
422cdf0e10cSrcweir                     ::rtl::OUString aChartName = pSdrOle2Obj->GetPersistName();
423cdf0e10cSrcweir                     ::std::vector< ::rtl::OUString >::const_iterator aEnd = rExcludedChartNames.end();
424cdf0e10cSrcweir                     ::std::vector< ::rtl::OUString >::const_iterator aFound = ::std::find( rExcludedChartNames.begin(), aEnd, aChartName );
425cdf0e10cSrcweir                     if ( aFound == aEnd )
426cdf0e10cSrcweir                     {
427cdf0e10cSrcweir                         uno::Reference< embed::XEmbeddedObject > xEmbeddedObj = pSdrOle2Obj->GetObjRef();
428cdf0e10cSrcweir                         if ( xEmbeddedObj.is() && ( nRangeList < nRangeListCount ) )
429cdf0e10cSrcweir                         {
430cdf0e10cSrcweir                             bool bDisableDataTableDialog = false;
431cdf0e10cSrcweir                             svt::EmbeddedObjectRef::TryRunningState( xEmbeddedObj );
432cdf0e10cSrcweir                             uno::Reference< beans::XPropertySet > xProps( xEmbeddedObj->getComponent(), uno::UNO_QUERY );
433cdf0e10cSrcweir                             if ( xProps.is() &&
434cdf0e10cSrcweir                                  ( xProps->getPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "DisableDataTableDialog" ) ) ) >>= bDisableDataTableDialog ) &&
435cdf0e10cSrcweir                                  bDisableDataTableDialog )
436cdf0e10cSrcweir                             {
437cdf0e10cSrcweir                                 if ( bSameDoc )
438cdf0e10cSrcweir                                 {
439cdf0e10cSrcweir                                     ScRange aEmptyRange;
440cdf0e10cSrcweir                                     ScChartListener aSearcher( aChartName, pDoc, aEmptyRange );
441cdf0e10cSrcweir                                     sal_uInt16 nIndex = 0;
442cdf0e10cSrcweir                                     ScChartListenerCollection* pCollection = pDoc->GetChartListenerCollection();
443cdf0e10cSrcweir                                     if ( pCollection && !pCollection->Search( &aSearcher, nIndex ) )
444cdf0e10cSrcweir                                     {
445cdf0e10cSrcweir                                         ScRangeList aRangeList( rRangesVector[ nRangeList++ ] );
446cdf0e10cSrcweir                                         ScRangeListRef rRangeList( new ScRangeList( aRangeList ) );
447cdf0e10cSrcweir                                         ScChartListener* pChartListener = new ScChartListener( aChartName, pDoc, rRangeList );
448cdf0e10cSrcweir                                         pCollection->Insert( pChartListener );
449cdf0e10cSrcweir                                         pChartListener->StartListeningTo();
450cdf0e10cSrcweir                                     }
451cdf0e10cSrcweir                                 }
452cdf0e10cSrcweir                                 else
453cdf0e10cSrcweir                                 {
454cdf0e10cSrcweir                                     xProps->setPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "DisableDataTableDialog" ) ),
455cdf0e10cSrcweir                                         uno::makeAny( sal_False ) );
456cdf0e10cSrcweir                                     xProps->setPropertyValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "DisableComplexChartTypes" ) ),
457cdf0e10cSrcweir                                         uno::makeAny( sal_False ) );
458cdf0e10cSrcweir                                 }
459cdf0e10cSrcweir                             }
460cdf0e10cSrcweir                         }
461cdf0e10cSrcweir 
462cdf0e10cSrcweir                         if ( pModelObj && pModelObj->HasChangesListeners() )
463cdf0e10cSrcweir                         {
464cdf0e10cSrcweir                             Rectangle aRectangle = pSdrOle2Obj->GetSnapRect();
465cdf0e10cSrcweir                             ScRange aRange( pDoc->GetRange( nTab, aRectangle ) );
466cdf0e10cSrcweir                             ScRangeList aChangeRanges;
467cdf0e10cSrcweir                             aChangeRanges.Append( aRange );
468cdf0e10cSrcweir 
469cdf0e10cSrcweir                             uno::Sequence< beans::PropertyValue > aProperties( 1 );
470cdf0e10cSrcweir                             aProperties[ 0 ].Name = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Name" ) );
471cdf0e10cSrcweir                             aProperties[ 0 ].Value <<= aChartName;
472cdf0e10cSrcweir 
473cdf0e10cSrcweir                             pModelObj->NotifyChanges( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "insert-chart" ) ), aChangeRanges, aProperties );
474cdf0e10cSrcweir                         }
475cdf0e10cSrcweir                     }
476cdf0e10cSrcweir                 }
477cdf0e10cSrcweir             }
478cdf0e10cSrcweir             pObject = aIter.Next();
479cdf0e10cSrcweir         }
480cdf0e10cSrcweir     }
481cdf0e10cSrcweir }
482