android: Update app-specific/MIME type icons
[LibreOffice.git] / sd / source / ui / func / fuinsert.cxx
blob64b266f42060c7a858660db03da46081fe199207
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <config_features.h>
22 #include <fuinsert.hxx>
23 #include <comphelper/storagehelper.hxx>
24 #include <comphelper/propertysequence.hxx>
25 #include <editeng/sizeitem.hxx>
26 #include <officecfg/Office/Common.hxx>
27 #include <toolkit/helper/vclunohelper.hxx>
28 #include <svx/svxdlg.hxx>
29 #include <com/sun/star/chart2/XChartDocument.hpp>
30 #include <com/sun/star/embed/EmbedVerbs.hpp>
31 #include <com/sun/star/embed/NoVisualAreaSizeException.hpp>
32 #include <com/sun/star/embed/Aspects.hpp>
33 #include <com/sun/star/embed/XEmbeddedObject.hpp>
34 #include <com/sun/star/frame/XDispatchProvider.hpp>
35 #include <com/sun/star/media/XPlayer.hpp>
37 #include <svl/stritem.hxx>
38 #include <sfx2/dispatch.hxx>
39 #include <sfx2/msgpool.hxx>
40 #include <sfx2/msg.hxx>
41 #include <svtools/insdlg.hxx>
42 #include <sfx2/request.hxx>
43 #include <svl/globalnameitem.hxx>
44 #include <svtools/embedhlp.hxx>
45 #include <svx/linkwarn.hxx>
46 #include <avmedia/mediawindow.hxx>
47 #include <comphelper/classids.hxx>
48 #include <svtools/sfxecode.hxx>
49 #include <vcl/transfer.hxx>
50 #include <svl/urlbmk.hxx>
51 #include <svx/svdobj.hxx>
52 #include <svx/svdograf.hxx>
53 #include <svx/svdoole2.hxx>
54 #include <sot/formats.hxx>
55 #include <svx/svdpagv.hxx>
56 #include <sfx2/opengrf.hxx>
57 #include <sfx2/viewfrm.hxx>
58 #include <svx/charthelper.hxx>
59 #include <svx/svxids.hrc>
61 #include <sdresid.hxx>
62 #include <View.hxx>
63 #include <sdmod.hxx>
64 #include <Window.hxx>
65 #include <DrawViewShell.hxx>
66 #include <DrawDocShell.hxx>
67 #include <GraphicDocShell.hxx>
68 #include <strings.hrc>
69 #include <drawdoc.hxx>
70 #include <sdpage.hxx>
71 #include <sdgrffilter.hxx>
72 #include <vcl/svapp.hxx>
73 #include <vcl/weld.hxx>
74 #include <vcl/errinf.hxx>
75 #include <vcl/graphicfilter.hxx>
77 #include <vcl/GraphicNativeTransform.hxx>
78 #include <vcl/GraphicNativeMetadata.hxx>
80 #include <comphelper/lok.hxx>
82 using namespace com::sun::star;
84 namespace sd {
87 FuInsertGraphic::FuInsertGraphic (
88 ViewShell* pViewSh,
89 ::sd::Window* pWin,
90 ::sd::View* pView,
91 SdDrawDocument* pDoc,
92 SfxRequest& rReq,
93 bool replaceExistingImage)
94 : FuPoor(pViewSh, pWin, pView, pDoc, rReq),
95 mbReplaceExistingImage(replaceExistingImage)
99 rtl::Reference<FuPoor> FuInsertGraphic::Create( ViewShell* pViewSh, ::sd::Window* pWin, ::sd::View* pView,
100 SdDrawDocument* pDoc, SfxRequest& rReq, bool replaceExistingImage )
102 rtl::Reference<FuPoor> xFunc( new FuInsertGraphic( pViewSh, pWin, pView, pDoc, rReq, replaceExistingImage ) );
103 xFunc->DoExecute(rReq);
104 return xFunc;
107 void FuInsertGraphic::DoExecute( SfxRequest& rReq )
109 OUString aFileName;
110 Graphic aGraphic;
112 bool bAsLink = false;
113 ErrCode nError = ERRCODE_GRFILTER_OPENERROR;
115 const SfxItemSet* pArgs = rReq.GetArgs();
116 const SfxPoolItem* pItem;
118 if ( pArgs &&
119 pArgs->GetItemState( SID_INSERT_GRAPHIC, true, &pItem ) == SfxItemState::SET )
121 aFileName = static_cast<const SfxStringItem*>(pItem)->GetValue();
123 OUString aFilterName;
124 if ( const SfxStringItem* pFilterItem = pArgs->GetItemIfSet( FN_PARAM_FILTER ) )
125 aFilterName = pFilterItem->GetValue();
127 if ( pArgs->GetItemState( FN_PARAM_1, true, &pItem ) == SfxItemState::SET )
128 bAsLink = static_cast<const SfxBoolItem*>(pItem)->GetValue();
130 nError = GraphicFilter::LoadGraphic( aFileName, aFilterName, aGraphic, &GraphicFilter::GetGraphicFilter() );
132 else
134 SvxOpenGraphicDialog aDlg(SdResId(STR_INSERTGRAPHIC), mpWindow ? mpWindow->GetFrameWeld() : nullptr);
136 if( aDlg.Execute() != ERRCODE_NONE )
137 return; // cancel dialog
139 nError = aDlg.GetGraphic(aGraphic);
140 bAsLink = aDlg.IsAsLink();
141 aFileName = aDlg.GetPath();
144 if( nError == ERRCODE_NONE )
146 GraphicNativeMetadata aMetadata;
147 if ( aMetadata.read(aGraphic) )
149 const Degree10 aRotation = aMetadata.getRotation();
150 if (aRotation)
152 GraphicNativeTransform aTransform( aGraphic );
153 aTransform.rotate( aRotation );
156 if( dynamic_cast< DrawViewShell *>( mpViewShell ) )
158 sal_Int8 nAction = DND_ACTION_COPY;
159 SdrObject* pPickObj = nullptr;
160 if (mbReplaceExistingImage)
161 pPickObj = mpView->GetSelectedSingleObject( mpView->GetPage() );
162 if (pPickObj)
163 nAction = DND_ACTION_LINK;
164 else
166 pPickObj = mpView->GetEmptyPresentationObject( PresObjKind::Graphic );
167 if (pPickObj)
168 nAction = DND_ACTION_LINK;
171 Point aPos = mpWindow->GetVisibleCenter();
172 SdrGrafObj* pGrafObj = mpView->InsertGraphic(aGraphic, nAction, aPos, pPickObj, nullptr);
174 if(pGrafObj && bAsLink )
176 // really store as link only?
177 if( officecfg::Office::Common::Misc::ShowLinkWarningDialog::get() )
179 SvxLinkWarningDialog aWarnDlg(mpWindow->GetFrameWeld(), aFileName);
180 if (aWarnDlg.run() != RET_OK)
181 return; // don't store as link
184 // store as link
185 pGrafObj->SetGraphicLink(aFileName);
189 else
191 SdGRFFilter::HandleGraphicFilterError( nError, GraphicFilter::GetGraphicFilter().GetLastError() );
195 FuInsertClipboard::FuInsertClipboard (
196 ViewShell* pViewSh,
197 ::sd::Window* pWin,
198 ::sd::View* pView,
199 SdDrawDocument* pDoc,
200 SfxRequest& rReq)
201 : FuPoor(pViewSh, pWin, pView, pDoc, rReq)
205 rtl::Reference<FuPoor> FuInsertClipboard::Create( ViewShell* pViewSh, ::sd::Window* pWin, ::sd::View* pView, SdDrawDocument* pDoc, SfxRequest& rReq )
207 rtl::Reference<FuPoor> xFunc( new FuInsertClipboard( pViewSh, pWin, pView, pDoc, rReq ) );
208 xFunc->DoExecute(rReq);
209 return xFunc;
212 void FuInsertClipboard::DoExecute( SfxRequest& )
214 TransferableDataHelper aDataHelper( TransferableDataHelper::CreateFromSystemClipboard( mpWindow ) );
215 SotClipboardFormatId nFormatId;
217 SvxAbstractDialogFactory* pFact = SvxAbstractDialogFactory::Create();
218 ScopedVclPtr<SfxAbstractPasteDialog> pDlg(pFact->CreatePasteDialog(mpViewShell->GetFrameWeld()));
219 pDlg->Insert( SotClipboardFormatId::EMBED_SOURCE, OUString() );
220 pDlg->Insert( SotClipboardFormatId::LINK_SOURCE, OUString() );
221 pDlg->Insert( SotClipboardFormatId::DRAWING, OUString() );
222 pDlg->Insert( SotClipboardFormatId::SVXB, OUString() );
223 pDlg->Insert( SotClipboardFormatId::GDIMETAFILE, OUString() );
224 pDlg->Insert( SotClipboardFormatId::BITMAP, OUString() );
225 pDlg->Insert( SotClipboardFormatId::NETSCAPE_BOOKMARK, OUString() );
226 pDlg->Insert( SotClipboardFormatId::STRING, OUString() );
227 pDlg->Insert( SotClipboardFormatId::HTML, OUString() );
228 pDlg->Insert( SotClipboardFormatId::RTF, OUString() );
229 pDlg->Insert( SotClipboardFormatId::RICHTEXT, OUString() );
230 pDlg->Insert( SotClipboardFormatId::EDITENGINE_ODF_TEXT_FLAT, OUString() );
232 //TODO/MBA: testing
233 nFormatId = pDlg->GetFormat( aDataHelper );
234 if( nFormatId == SotClipboardFormatId::NONE || !aDataHelper.GetTransferable().is() )
235 return;
237 sal_Int8 nAction = DND_ACTION_COPY;
238 DrawViewShell* pDrViewSh = nullptr;
240 if (!mpView->InsertData( aDataHelper,
241 mpWindow->PixelToLogic( ::tools::Rectangle( Point(), mpWindow->GetOutputSizePixel() ).Center() ),
242 nAction, false, nFormatId ))
244 pDrViewSh = dynamic_cast<DrawViewShell*>(mpViewShell);
247 if (!pDrViewSh)
248 return;
250 INetBookmark aINetBookmark( "", "" );
252 if( ( aDataHelper.HasFormat( SotClipboardFormatId::NETSCAPE_BOOKMARK ) &&
253 aDataHelper.GetINetBookmark( SotClipboardFormatId::NETSCAPE_BOOKMARK, aINetBookmark ) ) ||
254 ( aDataHelper.HasFormat( SotClipboardFormatId::FILEGRPDESCRIPTOR ) &&
255 aDataHelper.GetINetBookmark( SotClipboardFormatId::FILEGRPDESCRIPTOR, aINetBookmark ) ) ||
256 ( aDataHelper.HasFormat( SotClipboardFormatId::UNIFORMRESOURCELOCATOR ) &&
257 aDataHelper.GetINetBookmark( SotClipboardFormatId::UNIFORMRESOURCELOCATOR, aINetBookmark ) ) )
259 pDrViewSh->InsertURLField( aINetBookmark.GetURL(), aINetBookmark.GetDescription(), "" );
263 FuInsertOLE::FuInsertOLE (
264 ViewShell* pViewSh,
265 ::sd::Window* pWin,
266 ::sd::View* pView,
267 SdDrawDocument* pDoc,
268 SfxRequest& rReq)
269 : FuPoor(pViewSh, pWin, pView, pDoc, rReq)
273 rtl::Reference<FuPoor> FuInsertOLE::Create( ViewShell* pViewSh, ::sd::Window* pWin, ::sd::View* pView, SdDrawDocument* pDoc, SfxRequest& rReq )
275 rtl::Reference<FuPoor> xFunc( new FuInsertOLE( pViewSh, pWin, pView, pDoc, rReq ) );
276 xFunc->DoExecute(rReq);
277 return xFunc;
280 void FuInsertOLE::DoExecute( SfxRequest& rReq )
282 if ( nSlotId == SID_ATTR_TABLE ||
283 nSlotId == SID_INSERT_DIAGRAM ||
284 nSlotId == SID_INSERT_MATH )
286 PresObjKind ePresObjKind = (nSlotId == SID_INSERT_DIAGRAM) ? PresObjKind::Chart : PresObjKind::Object;
288 SdrObject* pPickObj = mpView->GetEmptyPresentationObject( ePresObjKind );
290 // insert diagram or Calc table
291 OUString aObjName;
292 SvGlobalName aName;
293 if (nSlotId == SID_INSERT_DIAGRAM)
294 aName = SvGlobalName( SO3_SCH_CLASSID);
295 else if (nSlotId == SID_ATTR_TABLE)
296 aName = SvGlobalName(SO3_SC_CLASSID);
297 else if (nSlotId == SID_INSERT_MATH)
298 aName = SvGlobalName(SO3_SM_CLASSID);
300 uno::Reference < embed::XEmbeddedObject > xObj = mpViewShell->GetViewFrame()->GetObjectShell()->
301 GetEmbeddedObjectContainer().CreateEmbeddedObject( aName.GetByteSequence(), aObjName );
302 if ( xObj.is() )
304 // Create default chart type.
305 uno::Reference<chart2::XChartDocument> xChartDoc(xObj->getComponent(), uno::UNO_QUERY);
306 if (xChartDoc.is())
307 xChartDoc->createDefaultChart();
309 sal_Int64 nAspect = embed::Aspects::MSOLE_CONTENT;
311 MapUnit aUnit = VCLUnoHelper::UnoEmbed2VCLMapUnit( xObj->getMapUnit( nAspect ) );
313 ::tools::Rectangle aRect;
314 if( pPickObj )
316 aRect = pPickObj->GetLogicRect();
318 awt::Size aSz;
319 aSz.Width = aRect.GetWidth();
320 aSz.Height = aRect.GetHeight();
321 xObj->setVisualAreaSize( nAspect, aSz );
323 else
325 awt::Size aSz;
328 aSz = xObj->getVisualAreaSize( nAspect );
330 catch ( embed::NoVisualAreaSizeException& )
332 // the default size will be set later
335 Size aSize( aSz.Width, aSz.Height );
337 if (aSize.IsEmpty())
339 // rectangle with balanced edge ratio
340 aSize.setWidth( 14100 );
341 aSize.setHeight( 10000 );
342 Size aTmp = OutputDevice::LogicToLogic(aSize, MapMode(MapUnit::Map100thMM), MapMode(aUnit));
343 aSz.Width = aTmp.Width();
344 aSz.Height = aTmp.Height();
345 xObj->setVisualAreaSize( nAspect, aSz );
347 else
349 aSize = OutputDevice::LogicToLogic(aSize, MapMode(aUnit), MapMode(MapUnit::Map100thMM));
352 Point aPos = mpWindow->GetVisibleCenter();
353 aPos.AdjustX( -(aSize.Width() / 2) );
354 aPos.AdjustY( -(aSize.Height() / 2) );
355 aRect = ::tools::Rectangle(aPos, aSize);
358 rtl::Reference<SdrOle2Obj> pOleObj = new SdrOle2Obj(
359 mpView->getSdrModelFromSdrView(),
360 svt::EmbeddedObjectRef( xObj, nAspect ),
361 aObjName,
362 aRect);
363 SdrPageView* pPV = mpView->GetSdrPageView();
365 // if we have a pick obj we need to make this new ole a pres obj replacing the current pick obj
366 if( pPickObj )
368 SdPage* pPage = static_cast< SdPage* >(pPickObj->getSdrPageFromSdrObject());
369 if(pPage && pPage->IsPresObj(pPickObj))
371 pPage->InsertPresObj( pOleObj.get(), ePresObjKind );
372 pOleObj->SetUserCall(pPickObj->GetUserCall());
375 // #i123468# we need to end text edit before replacing the object. There cannot yet
376 // being text typed (else it would not be an EmptyPresObj anymore), but it may be
377 // in text edit mode
378 if (mpView->IsTextEdit())
380 mpView->SdrEndTextEdit();
384 bool bRet = true;
385 if( pPickObj )
386 mpView->ReplaceObjectAtView(pPickObj, *pPV, pOleObj.get() );
387 else
388 bRet = mpView->InsertObjectAtView(pOleObj.get(), *pPV, SdrInsertFlags::SETDEFLAYER);
390 if (bRet && !comphelper::LibreOfficeKit::isActive())
392 // Let the chart be activated after the inserting (unless
393 // via LibreOfficeKit)
394 if (nSlotId == SID_INSERT_DIAGRAM)
396 pOleObj->SetProgName( "StarChart");
398 else if (nSlotId == SID_ATTR_TABLE)
400 pOleObj->SetProgName( "StarCalc" );
402 else if (nSlotId == SID_INSERT_MATH)
404 pOleObj->SetProgName( "StarMath" );
407 pOleObj->SetLogicRect(aRect);
408 Size aTmp( OutputDevice::LogicToLogic(aRect.GetSize(), MapMode(MapUnit::Map100thMM), MapMode(aUnit)) );
409 awt::Size aVisualSize;
410 aVisualSize.Width = aTmp.Width();
411 aVisualSize.Height = aTmp.Height();
412 xObj->setVisualAreaSize( nAspect, aVisualSize );
413 mpViewShell->ActivateObject(pOleObj.get(), embed::EmbedVerbs::MS_OLEVERB_SHOW);
415 if (nSlotId == SID_INSERT_DIAGRAM)
417 // note, that this call modified the chart model which
418 // results in a change notification. So call this after
419 // everything else is finished.
420 ChartHelper::AdaptDefaultsForChart( xObj );
424 else
426 ErrorHandler::HandleError(* new StringErrorInfo(ERRCODE_SFX_OLEGENERAL,
427 "" ) );
430 else
432 // insert object
433 sal_Int64 nAspect = embed::Aspects::MSOLE_CONTENT;
434 bool bCreateNew = false;
435 uno::Reference < embed::XEmbeddedObject > xObj;
436 uno::Reference < embed::XStorage > xStorage = comphelper::OStorageHelper::GetTemporaryStorage();
437 SvObjectServerList aServerLst;
438 OUString aName;
440 OUString aIconMediaType;
441 uno::Reference< io::XInputStream > xIconMetaFile;
443 const SfxGlobalNameItem* pNameItem = rReq.GetArg<SfxGlobalNameItem>(SID_INSERT_OBJECT);
444 if ( nSlotId == SID_INSERT_OBJECT && pNameItem )
446 const SvGlobalName& aClassName = pNameItem->GetValue();
447 xObj = mpViewShell->GetViewFrame()->GetObjectShell()->
448 GetEmbeddedObjectContainer().CreateEmbeddedObject( aClassName.GetByteSequence(), aName );
450 else
452 switch ( nSlotId )
454 case SID_INSERT_OBJECT :
456 aServerLst.FillInsertObjects();
457 if (mpDoc->GetDocumentType() == DocumentType::Draw)
459 aServerLst.Remove( GraphicDocShell::Factory().GetClassId() );
461 else
463 aServerLst.Remove( DrawDocShell::Factory().GetClassId() );
466 [[fallthrough]];
468 case SID_INSERT_FLOATINGFRAME :
470 SvxAbstractDialogFactory* pFact = SvxAbstractDialogFactory::Create();
471 ScopedVclPtr<SfxAbstractInsertObjectDialog> pDlg(
472 pFact->CreateInsertObjectDialog( mpViewShell->GetFrameWeld(), SD_MOD()->GetSlotPool()->GetSlot(nSlotId)->GetCommand(),
473 xStorage, &aServerLst ));
474 pDlg->Execute();
475 bCreateNew = pDlg->IsCreateNew();
476 xObj = pDlg->GetObject();
478 xIconMetaFile = pDlg->GetIconIfIconified( &aIconMediaType );
479 if ( xIconMetaFile.is() )
480 nAspect = embed::Aspects::MSOLE_ICON;
482 if ( xObj.is() )
483 mpViewShell->GetObjectShell()->GetEmbeddedObjectContainer().InsertEmbeddedObject( xObj, aName );
485 break;
492 if (xObj.is())
494 bool bInsertNewObject = true;
496 Size aSize;
497 MapUnit aMapUnit = MapUnit::Map100thMM;
498 if ( nAspect != embed::Aspects::MSOLE_ICON )
500 awt::Size aSz;
503 aSz = xObj->getVisualAreaSize( nAspect );
505 catch( embed::NoVisualAreaSizeException& )
507 // the default size will be set later
510 aSize =Size( aSz.Width, aSz.Height );
512 aMapUnit = VCLUnoHelper::UnoEmbed2VCLMapUnit( xObj->getMapUnit( nAspect ) );
513 if (aSize.IsEmpty())
515 // rectangle with balanced edge ratio
516 aSize.setWidth( 14100 );
517 aSize.setHeight( 10000 );
518 Size aTmp = OutputDevice::LogicToLogic(aSize, MapMode(MapUnit::Map100thMM), MapMode(aMapUnit));
519 aSz.Width = aTmp.Width();
520 aSz.Height = aTmp.Height();
521 xObj->setVisualAreaSize( nAspect, aSz );
523 else
525 aSize = OutputDevice::LogicToLogic(aSize, MapMode(aMapUnit), MapMode(MapUnit::Map100thMM));
529 if ( mpView->AreObjectsMarked() )
531 // as an empty OLE object available?
532 const SdrMarkList& rMarkList = mpView->GetMarkedObjectList();
534 if (rMarkList.GetMarkCount() == 1)
536 SdrMark* pMark = rMarkList.GetMark(0);
537 SdrObject* pObj = pMark->GetMarkedSdrObj();
539 if (pObj->GetObjInventor() == SdrInventor::Default &&
540 pObj->GetObjIdentifier() == SdrObjKind::OLE2)
542 if ( !static_cast<SdrOle2Obj*>(pObj)->GetObjRef().is() )
544 // the empty OLE object gets a new IPObj
545 bInsertNewObject = false;
546 pObj->SetEmptyPresObj(false);
547 static_cast<SdrOle2Obj*>(pObj)->SetOutlinerParaObject(std::nullopt);
548 static_cast<SdrOle2Obj*>(pObj)->SetObjRef(xObj);
549 static_cast<SdrOle2Obj*>(pObj)->SetPersistName(aName);
550 static_cast<SdrOle2Obj*>(pObj)->SetName(aName);
551 static_cast<SdrOle2Obj*>(pObj)->SetAspect(nAspect);
552 ::tools::Rectangle aRect = static_cast<SdrOle2Obj*>(pObj)->GetLogicRect();
554 if ( nAspect == embed::Aspects::MSOLE_ICON )
556 if( xIconMetaFile.is() )
557 static_cast<SdrOle2Obj*>(pObj)->SetGraphicToObj( xIconMetaFile, aIconMediaType );
559 else
561 Size aTmp = OutputDevice::LogicToLogic(aRect.GetSize(), MapMode(MapUnit::Map100thMM), MapMode(aMapUnit));
562 awt::Size aSz( aTmp.Width(), aTmp.Height() );
563 xObj->setVisualAreaSize( nAspect, aSz );
570 if (bInsertNewObject)
572 // we create a new OLE object
573 SdrPageView* pPV = mpView->GetSdrPageView();
574 Size aPageSize = pPV->GetPage()->GetSize();
576 // get the size from the iconified object
577 ::svt::EmbeddedObjectRef aObjRef( xObj, nAspect );
578 if ( nAspect == embed::Aspects::MSOLE_ICON )
580 aObjRef.SetGraphicStream( xIconMetaFile, aIconMediaType );
581 MapMode aMapMode( MapUnit::Map100thMM );
582 aSize = aObjRef.GetSize( &aMapMode );
585 Point aPnt ((aPageSize.Width() - aSize.Width()) / 2,
586 (aPageSize.Height() - aSize.Height()) / 2);
587 ::tools::Rectangle aRect (aPnt, aSize);
588 rtl::Reference<SdrOle2Obj> pObj = new SdrOle2Obj(
589 mpView->getSdrModelFromSdrView(),
590 aObjRef,
591 aName,
592 aRect);
594 if( mpView->InsertObjectAtView(pObj.get(), *pPV, SdrInsertFlags::SETDEFLAYER) )
596 // Math objects change their object size during InsertObject.
597 // New size must be set in SdrObject, or a wrong scale will be set at
598 // ActivateObject.
600 if ( nAspect != embed::Aspects::MSOLE_ICON )
604 awt::Size aSz = xObj->getVisualAreaSize( nAspect );
606 Size aNewSize = OutputDevice::LogicToLogic( Size( aSz.Width, aSz.Height ),
607 MapMode( aMapUnit ), MapMode( MapUnit::Map100thMM ) );
608 if ( aNewSize != aSize )
610 aRect.SetSize( aNewSize );
611 pObj->SetLogicRect( aRect );
614 catch( embed::NoVisualAreaSizeException& )
618 if (bCreateNew)
620 pObj->SetLogicRect(aRect);
622 if ( nAspect != embed::Aspects::MSOLE_ICON )
624 Size aTmp = OutputDevice::LogicToLogic(aRect.GetSize(), MapMode(MapUnit::Map100thMM), MapMode(aMapUnit));
625 awt::Size aSz( aTmp.Width(), aTmp.Height() );
626 xObj->setVisualAreaSize( nAspect, aSz );
629 mpViewShell->ActivateObject(pObj.get(), embed::EmbedVerbs::MS_OLEVERB_SHOW);
632 Size aVisSizePixel = mpWindow->GetOutputSizePixel();
633 ::tools::Rectangle aVisAreaWin = mpWindow->PixelToLogic( ::tools::Rectangle( Point(0,0), aVisSizePixel) );
634 mpViewShell->VisAreaChanged(aVisAreaWin);
635 mpDocSh->SetVisArea(aVisAreaWin);
640 catch (uno::Exception&)
642 // For some reason the object can not be inserted. For example
643 // because it is password protected and is not properly unlocked.
648 FuInsertAVMedia::FuInsertAVMedia(
649 ViewShell* pViewSh,
650 ::sd::Window* pWin,
651 ::sd::View* pView,
652 SdDrawDocument* pDoc,
653 SfxRequest& rReq)
654 : FuPoor(pViewSh, pWin, pView, pDoc, rReq)
658 rtl::Reference<FuPoor> FuInsertAVMedia::Create( ViewShell* pViewSh, ::sd::Window* pWin, ::sd::View* pView, SdDrawDocument* pDoc, SfxRequest& rReq )
660 rtl::Reference<FuPoor> xFunc( new FuInsertAVMedia( pViewSh, pWin, pView, pDoc, rReq ) );
661 xFunc->DoExecute(rReq);
662 return xFunc;
665 void FuInsertAVMedia::DoExecute( SfxRequest& rReq )
667 #if HAVE_FEATURE_AVMEDIA
668 OUString aURL;
669 const SfxItemSet* pReqArgs = rReq.GetArgs();
670 bool bAPI = false;
672 const SvxSizeItem* pSizeItem = rReq.GetArg<SvxSizeItem>(FN_PARAM_1);
673 const SfxBoolItem* pLinkItem = rReq.GetArg<SfxBoolItem>(FN_PARAM_2);
674 const bool bSizeUnknown = !pSizeItem;
675 Size aPrefSize;
677 if( pReqArgs )
679 const SfxStringItem* pStringItem = dynamic_cast<const SfxStringItem*>( &pReqArgs->Get( rReq.GetSlot() ) );
681 if( pStringItem )
683 aURL = pStringItem->GetValue();
684 bAPI = !aURL.isEmpty();
688 bool bLink(pLinkItem ? pLinkItem->GetValue() : true);
689 if (!(bAPI
690 || ::avmedia::MediaWindow::executeMediaURLDialog(mpWindow ? mpWindow->GetFrameWeld() : nullptr, aURL, & bLink)
692 return;
694 if (!bSizeUnknown)
696 aPrefSize = pSizeItem->GetSize();
698 else
700 // If we don't have a size then try and find that out, the resulted might be deliver async, so dispatch a follow up
701 // effort to insert the video, this time with a size.
702 if( mpWindow )
703 mpWindow->EnterWait();
705 css::uno::Reference<css::frame::XDispatchProvider> xDispatchProvider(mpViewShell->GetViewFrame()->GetFrame().GetFrameInterface(), css::uno::UNO_QUERY);
707 rtl::Reference<avmedia::PlayerListener> xPlayerListener(new avmedia::PlayerListener(
708 [xDispatchProvider, aURL, bLink](const css::uno::Reference<css::media::XPlayer>& rPlayer){
709 css::awt::Size aSize = rPlayer->getPreferredPlayerWindowSize();
710 avmedia::MediaWindow::dispatchInsertAVMedia(xDispatchProvider, aSize, aURL, bLink);
711 }));
713 const bool bIsMediaURL = ::avmedia::MediaWindow::isMediaURL(aURL, "", true, xPlayerListener);
715 if( mpWindow )
716 mpWindow->LeaveWait();
718 if (!bIsMediaURL && !bAPI)
719 ::avmedia::MediaWindow::executeFormatErrorBox(mpWindow->GetFrameWeld());
721 return;
724 InsertMediaURL(aURL, aPrefSize, bLink);
726 #else
727 (void)rReq;
728 #endif
731 #if HAVE_FEATURE_AVMEDIA
732 void FuInsertAVMedia::InsertMediaURL(const OUString& rURL, const Size& rPrefSize, bool bLink)
734 if( mpWindow )
735 mpWindow->EnterWait();
737 Point aPos;
738 Size aSize;
739 sal_Int8 nAction = DND_ACTION_COPY;
741 if (rPrefSize.Width() && rPrefSize.Height())
743 if( mpWindow )
744 aSize = mpWindow->PixelToLogic(rPrefSize, MapMode(MapUnit::Map100thMM));
745 else
746 aSize = Application::GetDefaultDevice()->PixelToLogic(rPrefSize, MapMode(MapUnit::Map100thMM));
748 else
749 aSize = Size( 5000, 5000 );
751 if( mpWindow )
753 aPos = mpWindow->PixelToLogic( ::tools::Rectangle( aPos, mpWindow->GetOutputSizePixel() ).Center() );
754 aPos.AdjustX( -(aSize.Width() >> 1) );
755 aPos.AdjustY( -(aSize.Height() >> 1) );
758 mpView->InsertMediaURL(rURL, nAction, aPos, aSize, bLink);
760 if( mpWindow )
761 mpWindow->LeaveWait();
763 #endif
765 } // end of namespace sd
767 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */