xref: /trunk/main/sc/source/ui/drawfunc/fuins2.cxx (revision 6e9cccf4)
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_sc.hxx"
26 
27 
28 #include <com/sun/star/embed/NoVisualAreaSizeException.hpp>
29 #include <com/sun/star/embed/Aspects.hpp>
30 #include <com/sun/star/beans/XPropertySet.hpp>
31 
32 //------------------------------------------------------------------------
33 
34 #include <toolkit/helper/vclunohelper.hxx>
35 #include <sot/exchange.hxx>
36 #include <svl/globalnameitem.hxx>
37 #include <sfx2/viewfrm.hxx>
38 #include <sfx2/docfile.hxx>
39 #include <svl/stritem.hxx>
40 #include <svx/svdoole2.hxx>
41 #include <svx/pfiledlg.hxx>
42 #include <tools/urlobj.hxx>
43 #include <vcl/msgbox.hxx>
44 #include <svl/urihelper.hxx>
45 #include <unotools/moduleoptions.hxx>
46 #include <svtools/insdlg.hxx>
47 #include <svtools/soerr.hxx>
48 #include <svx/svxdlg.hxx>
49 #include <sot/clsids.hxx>
50 #include <svx/svdpagv.hxx>
51 #include <svx/svdpage.hxx>
52 #include <svx/svdundo.hxx>
53 #include <sfx2/msgpool.hxx>
54 #include <svx/charthelper.hxx>
55 #include <scmod.hxx>
56 
57 // BM/IHA --
58 #include <cppuhelper/component_context.hxx>
59 #include <comphelper/processfactory.hxx>
60 #include <comphelper/storagehelper.hxx>
61 #include <com/sun/star/beans/XPropertySet.hpp>
62 #include <com/sun/star/frame/XSynchronousFrameLoader.hpp>
63 #include <com/sun/star/frame/XComponentLoader.hpp>
64 #include <com/sun/star/beans/PropertyValue.hpp>
65 #include <com/sun/star/chart2/data/XDataProvider.hpp>
66 #include <com/sun/star/chart2/data/XDataReceiver.hpp>
67 #include <com/sun/star/ui/dialogs/XExecutableDialog.hpp>
68 #include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp>
69 #include <com/sun/star/lang/XInitialization.hpp>
70 #include <com/sun/star/frame/XModel.hpp>
71 #include <com/sun/star/chart/ChartDataRowSource.hpp>
72 #include <cppuhelper/bootstrap.hxx>
73 
74 using namespace ::com::sun::star;
75 // BM/IHA --
76 
77 // erAck
78 #include "chart2uno.hxx"
79 // erAck
80 
81 #include "fuinsert.hxx"
82 #include "tabvwsh.hxx"
83 #include "sc.hrc"
84 #include "chartarr.hxx"
85 #include "docsh.hxx"
86 #include "document.hxx"
87 #include "undotab.hxx"
88 #include "chartlis.hxx"
89 #include "uiitems.hxx"
90 #include "globstr.hrc"
91 #include "drawview.hxx"
92 
93 //------------------------------------------------------------------------
94 
95 #define IS_AVAILABLE(WhichId,ppItem) \
96     (pReqArgs->GetItemState((WhichId), sal_True, ppItem ) == SFX_ITEM_SET)
97 
lcl_ChartInit(const uno::Reference<embed::XEmbeddedObject> & xObj,ScViewData * pViewData,const rtl::OUString & rRangeParam)98 void lcl_ChartInit( const uno::Reference < embed::XEmbeddedObject >& xObj, ScViewData* pViewData,
99                     const rtl::OUString& rRangeParam )
100 {
101     ScDocShell* pDocShell = pViewData->GetDocShell();
102     ScDocument* pScDoc = pDocShell->GetDocument();
103 
104     rtl::OUString aRangeString( rRangeParam );
105     if ( !aRangeString.getLength() )
106     {
107         SCCOL nCol1 = 0;
108         SCROW nRow1 = 0;
109         SCTAB nTab1 = 0;
110         SCCOL nCol2 = 0;
111         SCROW nRow2 = 0;
112         SCTAB nTab2 = 0;
113 
114         ScMarkData& rMark = pViewData->GetMarkData();
115         if ( !rMark.IsMarked() )
116             pViewData->GetView()->MarkDataArea( sal_True );
117 
118         if ( pViewData->GetSimpleArea( nCol1,nRow1,nTab1, nCol2,nRow2,nTab2 ) == SC_MARK_SIMPLE )
119         {
120             PutInOrder( nCol1, nCol2 );
121             PutInOrder( nRow1, nRow2 );
122             if ( nCol2>nCol1 || nRow2>nRow1 )
123             {
124                 ScDocument* pDoc = pViewData->GetDocument();
125                 pDoc->LimitChartArea( nTab1, nCol1,nRow1, nCol2,nRow2 );
126 
127                 String aStr;
128                 ScRange aRange( nCol1, nRow1, nTab1, nCol2, nRow2, nTab2 );
129                 aRange.Format( aStr, SCR_ABS_3D, pScDoc );
130                 aRangeString = aStr;
131             }
132         }
133     }
134 
135     if ( aRangeString.getLength() )
136     {
137         // connect to Calc data (if no range string, leave chart alone, with its own data)
138 
139         uno::Reference< ::com::sun::star::chart2::data::XDataReceiver > xReceiver;
140         uno::Reference< embed::XComponentSupplier > xCompSupp( xObj, uno::UNO_QUERY );
141         if( xCompSupp.is())
142             xReceiver.set( xCompSupp->getComponent(), uno::UNO_QUERY );
143         OSL_ASSERT( xReceiver.is());
144         if( xReceiver.is() )
145         {
146             uno::Reference< chart2::data::XDataProvider > xDataProvider = new ScChart2DataProvider( pScDoc );
147             xReceiver->attachDataProvider( xDataProvider );
148 
149             uno::Reference< util::XNumberFormatsSupplier > xNumberFormatsSupplier( pDocShell->GetModel(), uno::UNO_QUERY );
150             xReceiver->attachNumberFormatsSupplier( xNumberFormatsSupplier );
151 
152             // Same behavior as with old chart: Always assume data series in columns
153             chart::ChartDataRowSource eDataRowSource = chart::ChartDataRowSource_COLUMNS;
154             bool bHasCategories = false;
155             bool bFirstCellAsLabel = false;
156 
157             // use ScChartPositioner to auto-detect column/row headers (like ScChartArray in old version)
158             ScRangeListRef aRangeListRef( new ScRangeList );
159             aRangeListRef->Parse( aRangeString, pScDoc );
160             if ( aRangeListRef->Count() )
161             {
162                 pScDoc->LimitChartIfAll( aRangeListRef );               // limit whole columns/rows to used area
163 
164                 // update string from modified ranges.  The ranges must be in the current formula syntax.
165                 String aTmpStr;
166                 aRangeListRef->Format( aTmpStr, SCR_ABS_3D, pScDoc, pScDoc->GetAddressConvention() );
167                 aRangeString = aTmpStr;
168 
169                 ScChartPositioner aChartPositioner( pScDoc, aRangeListRef );
170                 const ScChartPositionMap* pPositionMap( aChartPositioner.GetPositionMap() );
171                 if( pPositionMap )
172                 {
173                     SCSIZE nRowCount = pPositionMap->GetRowCount();
174                     if( 1==nRowCount )
175                         eDataRowSource = chart::ChartDataRowSource_ROWS;
176                 }
177                 if ( eDataRowSource == chart::ChartDataRowSource_COLUMNS )
178                 {
179                     bHasCategories = aChartPositioner.HasRowHeaders();
180                     bFirstCellAsLabel = aChartPositioner.HasColHeaders();
181                 }
182                 else    // in case the default is changed
183                 {
184                     bHasCategories = aChartPositioner.HasColHeaders();
185                     bFirstCellAsLabel = aChartPositioner.HasRowHeaders();
186                 }
187             }
188 
189             uno::Sequence< beans::PropertyValue > aArgs( 4 );
190             aArgs[0] = beans::PropertyValue(
191                 ::rtl::OUString::createFromAscii("CellRangeRepresentation"), -1,
192                 uno::makeAny( aRangeString ), beans::PropertyState_DIRECT_VALUE );
193             aArgs[1] = beans::PropertyValue(
194                 ::rtl::OUString::createFromAscii("HasCategories"), -1,
195                 uno::makeAny( bHasCategories ), beans::PropertyState_DIRECT_VALUE );
196             aArgs[2] = beans::PropertyValue(
197                 ::rtl::OUString::createFromAscii("FirstCellAsLabel"), -1,
198                 uno::makeAny( bFirstCellAsLabel ), beans::PropertyState_DIRECT_VALUE );
199             aArgs[3] = beans::PropertyValue(
200                 ::rtl::OUString::createFromAscii("DataRowSource"), -1,
201                 uno::makeAny( eDataRowSource ), beans::PropertyState_DIRECT_VALUE );
202             xReceiver->setArguments( aArgs );
203 
204             // don't create chart listener here (range may be modified in chart dialog)
205         }
206     }
207 }
208 
209 /*************************************************************************
210 |*
211 |* FuInsertOLE::Konstruktor
212 |*
213 \************************************************************************/
214 
FuInsertOLE(ScTabViewShell * pViewSh,Window * pWin,ScDrawView * pViewP,SdrModel * pDoc,SfxRequest & rReq)215 FuInsertOLE::FuInsertOLE(ScTabViewShell* pViewSh, Window* pWin, ScDrawView* pViewP,
216 		   SdrModel* pDoc, SfxRequest& rReq)
217     : FuPoor(pViewSh, pWin, pViewP, pDoc, rReq)
218 {
219 	if( ! rReq.IsAPI() )
220 		rReq.Done();
221 
222 	//!	hier DLL's initialisieren, damit die Factories existieren?
223 
224     uno::Reference < embed::XEmbeddedObject > xObj;
225     uno::Reference < embed::XStorage > xStorage = comphelper::OStorageHelper::GetTemporaryStorage();
226 	sal_Bool bIsFromFile = sal_False;
227     ::rtl::OUString aName;
228 
229     sal_Int64 nAspect = embed::Aspects::MSOLE_CONTENT;
230     ::rtl::OUString aIconMediaType;
231     uno::Reference< io::XInputStream > xIconMetaFile;
232 
233 
234 	sal_uInt16 nSlot = rReq.GetSlot();
235     SFX_REQUEST_ARG( rReq, pNameItem, SfxGlobalNameItem, SID_INSERT_OBJECT, sal_False );
236     if ( nSlot == SID_INSERT_OBJECT && pNameItem )
237 	{
238         SvGlobalName aClassName = pNameItem->GetValue();
239         xObj = pViewShell->GetViewFrame()->GetObjectShell()->GetEmbeddedObjectContainer().CreateEmbeddedObject( aClassName.GetByteSequence(), aName );
240     }
241     else if ( nSlot == SID_INSERT_SMATH )
242     {
243         if ( SvtModuleOptions().IsMath() )
244         {
245             nSlot = SID_INSERT_OBJECT;
246             xObj = pViewShell->GetViewFrame()->GetObjectShell()->GetEmbeddedObjectContainer().CreateEmbeddedObject( SvGlobalName( SO3_SM_CLASSID_60 ).GetByteSequence(), aName );
247             rReq.AppendItem( SfxGlobalNameItem( SID_INSERT_OBJECT, SvGlobalName( SO3_SM_CLASSID_60 ) ) );
248         }
249     }
250     else
251     {
252         SvObjectServerList aServerLst;
253         switch ( nSlot )
254         {
255             case SID_INSERT_OBJECT :
256                 aServerLst.FillInsertObjects();
257                 aServerLst.Remove( ScDocShell::Factory().GetClassId() );   // Starcalc nicht anzeigen
258                 //TODO/LATER: currently no inserting of ClassId into SfxRequest!
259             case SID_INSERT_PLUGIN :
260             case SID_INSERT_FLOATINGFRAME :
261             {
262                 SvxAbstractDialogFactory* pFact = SvxAbstractDialogFactory::Create();
263                 SfxAbstractInsertObjectDialog* pDlg =
264                         pFact->CreateInsertObjectDialog( pViewShell->GetWindow(), SC_MOD()->GetSlotPool()->GetSlot(nSlot)->GetCommandString(),
265                         xStorage, &aServerLst );
266                 if ( pDlg )
267                 {
268                     pDlg->Execute();
269                     xObj = pDlg->GetObject();
270 
271 					xIconMetaFile = pDlg->GetIconIfIconified( &aIconMediaType );
272 					if ( xIconMetaFile.is() )
273 						nAspect = embed::Aspects::MSOLE_ICON;
274 
275                     if ( xObj.is() )
276                         pViewSh->GetObjectShell()->GetEmbeddedObjectContainer().InsertEmbeddedObject( xObj, aName );
277                     // damit DrawShell eingeschaltet wird (Objekt aktivieren ist unnoetig):
278                     bIsFromFile = !pDlg->IsCreateNew();
279                     DELETEZ( pDlg );
280                 }
281 
282                 break;
283             }
284             case SID_INSERT_SOUND :
285             case SID_INSERT_VIDEO :
286             {
287                 // create special filedialog for plugins
288                 SvxPluginFileDlg aPluginFileDialog(pWin, nSlot);
289 
290                 // open filedlg
291                 if ( ERRCODE_NONE == aPluginFileDialog.Execute() )
292                 {
293                     // get URL
294                     INetURLObject aURL;
295                     aURL.SetSmartProtocol( INET_PROT_FILE );
296                     if ( aURL.SetURL( aPluginFileDialog.GetPath() ) )
297                     {
298                         // create a plugin object
299                         ::rtl::OUString aObjName;
300                         SvGlobalName aClassId( SO3_PLUGIN_CLASSID );
301                         comphelper::EmbeddedObjectContainer aCnt( xStorage );
302                         xObj = aCnt.CreateEmbeddedObject( aClassId.GetByteSequence(), aObjName );
303                         if ( xObj.is() && svt::EmbeddedObjectRef::TryRunningState( xObj ) )
304                         {
305                             // set properties from dialog
306                             uno::Reference < beans::XPropertySet > xSet( xObj->getComponent(), uno::UNO_QUERY );
307                             if ( xSet.is() )
308                             {
309                                 xSet->setPropertyValue( ::rtl::OUString::createFromAscii("PluginURL"),
310                                         uno::makeAny( ::rtl::OUString( aURL.GetMainURL( INetURLObject::NO_DECODE ) ) ) );
311                             }
312                         }
313                     }
314                     else
315                     {
316                         DBG_ERROR("Invalid URL!");
317                         //! error message
318                         //! can this happen???
319                     }
320                 }
321             }
322         }
323     }
324 
325 	//	SvInsertObjectDialog (alles in einem Dialog) wird nicht mehr benutzt
326     if (xObj.is())
327 	{
328 		pView->UnmarkAll();
329 
330 		try
331 		{
332 			::svt::EmbeddedObjectRef aObjRef( xObj, nAspect );
333 			Size aSize;
334 			MapMode aMap100( MAP_100TH_MM );
335 			MapUnit aMapUnit = MAP_100TH_MM;
336 
337 			if ( nAspect == embed::Aspects::MSOLE_ICON )
338 			{
339 				aObjRef.SetGraphicStream( xIconMetaFile, aIconMediaType );
340 				aSize = aObjRef.GetSize( &aMap100 );
341 			}
342 			else
343 			{
344             	awt::Size aSz;
345 				try
346 				{
347 					aSz = xObj->getVisualAreaSize( nAspect );
348 				}
349 				catch( embed::NoVisualAreaSizeException& )
350 				{
351 					// the default size will be set later
352 				}
353 
354             	aSize = Size( aSz.Width, aSz.Height );
355 
356             	aMapUnit = VCLUnoHelper::UnoEmbed2VCLMapUnit( xObj->getMapUnit( nAspect ) );
357             	if (aSize.Height() == 0 || aSize.Width() == 0)
358             	{
359                 	// Rechteck mit ausgewogenem Kantenverhaeltnis
360 					aSize.Width() = 5000;
361 					aSize.Height() = 5000;
362                 	Size aTmp = OutputDevice::LogicToLogic( aSize, MAP_100TH_MM, aMapUnit );
363                 	aSz.Width = aTmp.Width();
364                 	aSz.Height = aTmp.Height();
365                 	xObj->setVisualAreaSize( nAspect, aSz );
366 
367                 	//  re-convert aSize to 1/100th mm to avoid rounding errors in comparison below
368                 	aSize = Window::LogicToLogic( aTmp,
369                                 	MapMode( aMapUnit ), aMap100 );
370 				}
371 				else
372 					aSize = Window::LogicToLogic( aSize,
373                                 	MapMode( aMapUnit ), aMap100 );
374 			}
375 
376 			//	Chart initialisieren ?
377             if ( SvtModuleOptions().IsChart() && SotExchange::IsChart( SvGlobalName( xObj->getClassID() ) ) )
378                 lcl_ChartInit( xObj, pViewSh->GetViewData(), rtl::OUString() );
379 
380 			ScViewData* pData = pViewSh->GetViewData();
381 
382 			Point aPnt = pViewSh->GetInsertPos();
383 			if ( pData->GetDocument()->IsNegativePage( pData->GetTabNo() ) )
384 				aPnt.X() -= aSize.Width();		// move position to left edge
385 			Rectangle aRect (aPnt, aSize);
386             SdrOle2Obj* pObj = new SdrOle2Obj( aObjRef, aName, aRect);
387 			SdrPageView* pPV = pView->GetSdrPageView();
388 			pView->InsertObjectAtView(pObj, *pPV);
389 
390 			if ( nAspect != embed::Aspects::MSOLE_ICON )
391 			{
392 				//	#73279# Math objects change their object size during InsertObject.
393 				//	New size must be set in SdrObject, or a wrong scale will be set at
394 				//	ActivateObject.
395 
396 				try
397 				{
398             		awt::Size aSz = xObj->getVisualAreaSize( nAspect );
399 
400             		Size aNewSize( aSz.Width, aSz.Height );
401             		aNewSize = OutputDevice::LogicToLogic( aNewSize, aMapUnit, MAP_100TH_MM );
402 
403             		if ( aNewSize != aSize )
404 					{
405 						aRect.SetSize( aNewSize );
406 						pObj->SetLogicRect( aRect );
407 					}
408 				}
409 				catch( embed::NoVisualAreaSizeException& )
410 				{}
411 			}
412 
413 			if ( !rReq.IsAPI() )
414 			{
415 				// XXX Activate aus Makro ist toedlich !!! ???
416 				if (bIsFromFile)
417 				{
418 					// #45012# Objekt ist selektiert, also Draw-Shell aktivieren
419 					pViewShell->SetDrawShell( sal_True );
420 				}
421 				else
422 				{
423 					pViewShell->ActivateObject( (SdrOle2Obj*) pObj, SVVERB_SHOW );
424 				}
425 			}
426 
427 			rReq.Done();
428 		}
429 		catch( uno::Exception& )
430 		{
431 			OSL_ASSERT( "May need error handling here!\n" );
432 		}
433 	}
434 	else
435 		rReq.Ignore();
436 }
437 
438 /*************************************************************************
439 |*
440 |* FuInsertOLE::Destruktor
441 |*
442 \************************************************************************/
443 
~FuInsertOLE()444 FuInsertOLE::~FuInsertOLE()
445 {
446 }
447 
448 /*************************************************************************
449 |*
450 |* FuInsertOLE::Function aktivieren
451 |*
452 \************************************************************************/
453 
Activate()454 void FuInsertOLE::Activate()
455 {
456 	FuPoor::Activate();
457 }
458 
459 /*************************************************************************
460 |*
461 |* FuInsertOLE::Function deaktivieren
462 |*
463 \************************************************************************/
464 
Deactivate()465 void FuInsertOLE::Deactivate()
466 {
467 	FuPoor::Deactivate();
468 }
469 
470 /*************************************************************************
471 |*
472 |* FuInsertChart::Konstruktor
473 |*
474 \************************************************************************/
475 
FuInsertChart(ScTabViewShell * pViewSh,Window * pWin,ScDrawView * pViewP,SdrModel * pDoc,SfxRequest & rReq)476 FuInsertChart::FuInsertChart(ScTabViewShell* pViewSh, Window* pWin, ScDrawView* pViewP,
477 		   SdrModel* pDoc, SfxRequest& rReq)
478     : FuPoor(pViewSh, pWin, pViewP, pDoc, rReq)
479 {
480 	const SfxItemSet*	pReqArgs	= rReq.GetArgs();
481 
482 	if( ! rReq.IsAPI() )
483 		rReq.Done();
484 
485     if( SvtModuleOptions().IsChart() )
486     {
487         // ----------------------------------------
488         // BM/IHA --
489 
490         // get range
491         ::rtl::OUString aRangeString;
492         ScRange aPositionRange;             // cell range for chart positioning
493         if( pReqArgs )
494         {
495             const SfxPoolItem* pItem;
496             if( IS_AVAILABLE( FN_PARAM_5, &pItem ) )
497             aRangeString = ::rtl::OUString( ((const SfxStringItem*)pItem)->GetValue());
498 
499             aPositionRange = pViewSh->GetViewData()->GetCurPos();
500         }
501         else
502         {
503             ScMarkData& rMark = pViewSh->GetViewData()->GetMarkData();
504             bool bAutomaticMark = false;
505             if ( !rMark.IsMarked() && !rMark.IsMultiMarked() )
506             {
507 		        pViewSh->GetViewData()->GetView()->MarkDataArea( sal_True );
508                 bAutomaticMark = true;
509             }
510 
511             ScMarkData aMultiMark( rMark );
512             aMultiMark.MarkToMulti();
513 
514             ScRangeList aRanges;
515             aMultiMark.FillRangeListWithMarks( &aRanges, sal_False );
516             String aStr;
517             ScDocument* pDocument = pViewSh->GetViewData()->GetDocument();
518             aRanges.Format( aStr, SCR_ABS_3D, pDocument, pDocument->GetAddressConvention() );
519             aRangeString = aStr;
520 
521             // get "total" range for positioning
522             sal_uLong nCount = aRanges.Count();
523             if ( nCount > 0 )
524             {
525                 aPositionRange = *aRanges.GetObject(0);
526                 for (sal_uLong i=1; i<nCount; i++)
527                     aPositionRange.ExtendTo( *aRanges.GetObject(i) );
528             }
529 
530             if(bAutomaticMark)
531                 pViewSh->GetViewData()->GetView()->Unmark();
532         }
533 
534         // ----------------------------------------
535         // adapted old code
536         pView->UnmarkAll();
537 
538         ::rtl::OUString aName;
539         const sal_Int64 nAspect = embed::Aspects::MSOLE_CONTENT;
540 
541         uno::Reference < embed::XEmbeddedObject > xObj =
542             pViewShell->GetObjectShell()->GetEmbeddedObjectContainer().CreateEmbeddedObject( SvGlobalName( SO3_SCH_CLASSID_60 ).GetByteSequence(), aName );
543 
544         uno::Reference< ::com::sun::star::chart2::data::XDataReceiver > xReceiver;
545         uno::Reference< embed::XComponentSupplier > xCompSupp( xObj, uno::UNO_QUERY );
546         if( xCompSupp.is())
547             xReceiver.set( xCompSupp->getComponent(), uno::UNO_QUERY );
548 
549         // lock the model to suppress any internal updates
550         uno::Reference< frame::XModel > xChartModel( xReceiver, uno::UNO_QUERY );
551         if( xChartModel.is() )
552             xChartModel->lockControllers();
553 
554         ScRangeListRef aDummy;
555         Rectangle aMarkDest;
556         SCTAB nMarkTab;
557         sal_Bool bDrawRect = pViewShell->GetChartArea( aDummy, aMarkDest, nMarkTab );
558 
559         //	Objekt-Groesse
560         awt::Size aSz = xObj->getVisualAreaSize( nAspect );
561         Size aSize( aSz.Width, aSz.Height );
562 
563         MapUnit aMapUnit = VCLUnoHelper::UnoEmbed2VCLMapUnit( xObj->getMapUnit( nAspect ) );
564 
565         sal_Bool bSizeCh = sal_False;
566         if (bDrawRect && !aMarkDest.IsEmpty())
567         {
568             aSize = aMarkDest.GetSize();
569             bSizeCh = sal_True;
570         }
571         if (aSize.Height() <= 0 || aSize.Width() <= 0)
572         {
573             aSize.Width() = 5000;
574             aSize.Height() = 5000;
575             bSizeCh = sal_True;
576         }
577         if (bSizeCh)
578         {
579             aSize = Window::LogicToLogic( aSize, MapMode( MAP_100TH_MM ), MapMode( aMapUnit ) );
580             aSz.Width = aSize.Width();
581             aSz.Height = aSize.Height();
582             xObj->setVisualAreaSize( nAspect, aSz );
583         }
584 
585         ScViewData* pData = pViewSh->GetViewData();
586         ScDocShell* pScDocSh = pData->GetDocShell();
587         ScDocument* pScDoc	 = pScDocSh->GetDocument();
588         sal_Bool bUndo (pScDoc->IsUndoEnabled());
589 
590         if( pReqArgs )
591         {
592             const SfxPoolItem* pItem;
593             sal_uInt16 nToTable = 0;
594 
595             if( IS_AVAILABLE( FN_PARAM_4, &pItem ) )
596             {
597                 if ( pItem->ISA( SfxUInt16Item ) )
598                     nToTable = ((const SfxUInt16Item*)pItem)->GetValue();
599                 else if ( pItem->ISA( SfxBoolItem ) )
600                 {
601                     //	#46033# in der idl fuer Basic steht FN_PARAM_4 als SfxBoolItem
602                     //	-> wenn gesetzt, neue Tabelle, sonst aktuelle Tabelle
603 
604                     if ( ((const SfxBoolItem*)pItem)->GetValue() )
605                         nToTable = static_cast<sal_uInt16>(pScDoc->GetTableCount());
606                     else
607                         nToTable = static_cast<sal_uInt16>(pData->GetTabNo());
608                 }
609             }
610             else
611             {
612                 if (bDrawRect)
613                     nToTable = static_cast<sal_uInt16>(nMarkTab);
614                 rReq.AppendItem( SfxUInt16Item( FN_PARAM_4, nToTable ) );
615             }
616 
617             // auf neue Tabelle ausgeben?
618             if ( nToTable == pScDoc->GetTableCount() )
619             {
620                 // dann los...
621                 String		aTabName;
622                 SCTAB		nNewTab = pScDoc->GetTableCount();
623 
624                 pScDoc->CreateValidTabName( aTabName );
625 
626                 if ( pScDoc->InsertTab( nNewTab, aTabName ) )
627                 {
628                     sal_Bool bAppend = sal_True;
629 
630                     if (bUndo)
631                     {
632                         pScDocSh->GetUndoManager()->AddUndoAction(
633                             new ScUndoInsertTab( pScDocSh, nNewTab,
634                                                  bAppend, aTabName ) );
635                     }
636 
637                     pScDocSh->Broadcast( ScTablesHint( SC_TAB_INSERTED, nNewTab ) );
638                     pViewSh->SetTabNo( nNewTab, sal_True );
639                     pScDocSh->PostPaintExtras();			//! erst hinterher ???
640                 }
641                 else
642                 {
643                     DBG_ERROR( "Could not create new table :-/" );
644                 }
645             }
646             else if ( nToTable != pData->GetTabNo() )
647             {
648                 pViewSh->SetTabNo( nToTable, sal_True );
649             }
650         }
651 
652         lcl_ChartInit( xObj, pData, aRangeString );         // set source range, auto-detect column/row headers
653 
654         //	Objekt-Position
655 
656         Point aStart;
657         if ( bDrawRect )
658             aStart = aMarkDest.TopLeft();						// marked by hand
659         else
660         {
661             // get chart position (from window size and data range)
662             aStart = pViewSh->GetChartInsertPos( aSize, aPositionRange );
663         }
664 
665         Rectangle aRect (aStart, aSize);
666         SdrOle2Obj* pObj = new SdrOle2Obj( svt::EmbeddedObjectRef( xObj, nAspect ), aName, aRect);
667         SdrPageView* pPV = pView->GetSdrPageView();
668 
669         // #121334# This call will change the chart's default background fill from white to transparent.
670         // Add here again if this is wanted (see task description for details)
671         // ChartHelper::AdaptDefaultsForChart( xObj );
672 
673 //        pView->InsertObjectAtView(pObj, *pPV);//this call leads to an immidiate redraw and asks the chart for a visual representation
674 
675         // use the page instead of the view to insert, so no undo action is created yet
676         SdrPage* pInsPage = pPV->GetPage();
677         pInsPage->InsertObject( pObj );
678         pView->UnmarkAllObj();
679         pView->MarkObj( pObj, pPV );
680         bool bAddUndo = true;               // add undo action later, unless the dialog is canceled
681 
682         if (rReq.IsAPI())
683         {
684             if( xChartModel.is() )
685                 xChartModel->unlockControllers();
686         }
687         else
688         {
689             //the controller will be unlocked by the dialog when the dialog is told to do so
690 
691             // only activate object if not called via API (e.g. macro)
692             pViewShell->ActivateObject( (SdrOle2Obj*) pObj, SVVERB_SHOW );
693 
694             //open wizard
695             //@todo get context from calc if that has one
696             uno::Reference< uno::XComponentContext > xContext(
697                 ::cppu::defaultBootstrap_InitialComponentContext() );
698             if(xContext.is())
699             {
700                 uno::Reference< lang::XMultiComponentFactory > xMCF( xContext->getServiceManager() );
701                 if(xMCF.is())
702                 {
703                     uno::Reference< ui::dialogs::XExecutableDialog > xDialog(
704                         xMCF->createInstanceWithContext(
705                             rtl::OUString::createFromAscii("com.sun.star.comp.chart2.WizardDialog")
706                             , xContext), uno::UNO_QUERY);
707                     uno::Reference< lang::XInitialization > xInit( xDialog, uno::UNO_QUERY );
708                     if( xChartModel.is() && xInit.is() )
709                     {
710                         uno::Reference< awt::XWindow > xDialogParentWindow(0);
711                         //	initialize dialog
712                         uno::Sequence<uno::Any> aSeq(2);
713                         uno::Any* pArray = aSeq.getArray();
714                         beans::PropertyValue aParam1;
715                         aParam1.Name = rtl::OUString::createFromAscii("ParentWindow");
716                         aParam1.Value <<= uno::makeAny(xDialogParentWindow);
717                         beans::PropertyValue aParam2;
718                         aParam2.Name = rtl::OUString::createFromAscii("ChartModel");
719                         aParam2.Value <<= uno::makeAny(xChartModel);
720                         pArray[0] <<= uno::makeAny(aParam1);
721                         pArray[1] <<= uno::makeAny(aParam2);
722                         xInit->initialize( aSeq );
723 
724                         // try to set the dialog's position so it doesn't hide the chart
725                         uno::Reference < beans::XPropertySet > xDialogProps( xDialog, uno::UNO_QUERY );
726                         if ( xDialogProps.is() )
727                         {
728                             try
729                             {
730                                 //get dialog size:
731                                 awt::Size aDialogAWTSize;
732                                 if( xDialogProps->getPropertyValue( ::rtl::OUString::createFromAscii("Size") )
733                                     >>= aDialogAWTSize )
734                                 {
735                                     Size aDialogSize( aDialogAWTSize.Width, aDialogAWTSize.Height );
736                                     if ( aDialogSize.Width() > 0 && aDialogSize.Height() > 0 )
737                                     {
738                                         //calculate and set new position
739                                         Point aDialogPos = pViewShell->GetChartDialogPos( aDialogSize, aRect );
740                                         xDialogProps->setPropertyValue( ::rtl::OUString::createFromAscii("Position"),
741                                             uno::makeAny( awt::Point(aDialogPos.getX(),aDialogPos.getY()) ) );
742                                     }
743                                 }
744                                 //tell the dialog to unlock controller
745                                 xDialogProps->setPropertyValue( ::rtl::OUString::createFromAscii("UnlockControllersOnExecute"),
746                                             uno::makeAny( sal_True ) );
747 
748                             }
749                             catch( uno::Exception& )
750 		                    {
751 			                    OSL_ASSERT( "Chart wizard couldn't be positioned automatically\n" );
752 		                    }
753                         }
754 
755                         sal_Int16 nDialogRet = xDialog->execute();
756                         if( nDialogRet == ui::dialogs::ExecutableDialogResults::CANCEL )
757                         {
758                             // leave OLE inplace mode and unmark
759                             OSL_ASSERT( pViewShell );
760                             OSL_ASSERT( pView );
761                             pViewShell->DeactivateOle();
762                             pView->UnmarkAll();
763 
764                             // old page view pointer is invalid after switching sheets
765                             pPV = pView->GetSdrPageView();
766 
767                             // remove the chart
768                             OSL_ASSERT( pPV );
769                             SdrPage * pPage( pPV->GetPage());
770                             OSL_ASSERT( pPage );
771                             OSL_ASSERT( pObj );
772                             if( pPage )
773                                 pPage->RemoveObject( pObj->GetOrdNum());
774 
775                             bAddUndo = false;       // don't create the undo action for inserting
776 
777                             // leave the draw shell
778                             pViewShell->SetDrawShell( sal_False );
779                         }
780                         else
781                         {
782                             OSL_ASSERT( nDialogRet == ui::dialogs::ExecutableDialogResults::OK );
783                             //@todo maybe move chart to different table
784                         }
785                     }
786                     uno::Reference< lang::XComponent > xComponent( xDialog, uno::UNO_QUERY );
787                     if( xComponent.is())
788                         xComponent->dispose();
789                 }
790             }
791         }
792 
793         if ( bAddUndo )
794         {
795             // add undo action the same way as in SdrEditView::InsertObjectAtView
796             // (using UndoActionHdl etc.)
797             pView->AddUndo(pDoc->GetSdrUndoFactory().CreateUndoNewObject(*pObj));
798         }
799 
800         // BM/IHA --
801     }
802 }
803 
804 /*************************************************************************
805 |*
806 |* FuInsertChart::Destruktor
807 |*
808 \************************************************************************/
809 
~FuInsertChart()810 FuInsertChart::~FuInsertChart()
811 {
812 }
813 
814 /*************************************************************************
815 |*
816 |* FuInsertChart::Function aktivieren
817 |*
818 \************************************************************************/
819 
Activate()820 void FuInsertChart::Activate()
821 {
822 	FuPoor::Activate();
823 }
824 
825 /*************************************************************************
826 |*
827 |* FuInsertChart::Function deaktivieren
828 |*
829 \************************************************************************/
830 
Deactivate()831 void FuInsertChart::Deactivate()
832 {
833 	FuPoor::Deactivate();
834 }
835 
836 
837