1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <sfx2/opengrf.hxx>
21 #include <svx/svdograf.hxx>
22 #include <svx/svdomedia.hxx>
23 #include <svx/svdpage.hxx>
24 #include <svx/svdpagv.hxx>
25 #include <svx/svdview.hxx>
26 #include <svx/linkwarn.hxx>
27 #include <vcl/graphicfilter.hxx>
28 #include <svl/stritem.hxx>
29 #include <svtools/miscopt.hxx>
30 #include <vcl/msgbox.hxx>
31 #include <avmedia/mediawindow.hxx>
32 #include <vcl/svapp.hxx>
34 #include "fuinsert.hxx"
35 #include "tabvwsh.hxx"
36 #include "drwlayer.hxx"
37 #include "drawview.hxx"
38 #include "document.hxx"
39 #include "scresid.hxx"
40 #include "progress.hxx"
42 #include "globstr.hrc"
44 using namespace ::com::sun::star
;
46 void ScLimitSizeOnDrawPage( Size
& rSize
, Point
& rPos
, const Size
& rPage
)
48 if ( !rPage
.Width() || !rPage
.Height() )
51 Size aPageSize
= rPage
;
52 bool bNegative
= aPageSize
.Width() < 0;
55 // make everything positive temporarily
56 aPageSize
.Width() = -aPageSize
.Width();
57 rPos
.X() = -rPos
.X() - rSize
.Width();
60 if ( rSize
.Width() > aPageSize
.Width() || rSize
.Height() > aPageSize
.Height() )
62 double fX
= aPageSize
.Width() / (double) rSize
.Width();
63 double fY
= aPageSize
.Height() / (double) rSize
.Height();
67 rSize
.Width() = aPageSize
.Width();
68 rSize
.Height() = (long) ( rSize
.Height() * fX
);
72 rSize
.Height() = aPageSize
.Height();
73 rSize
.Width() = (long) ( rSize
.Width() * fY
);
82 if ( rPos
.X() + rSize
.Width() > aPageSize
.Width() )
83 rPos
.X() = aPageSize
.Width() - rSize
.Width();
84 if ( rPos
.Y() + rSize
.Height() > aPageSize
.Height() )
85 rPos
.Y() = aPageSize
.Height() - rSize
.Height();
88 rPos
.X() = -rPos
.X() - rSize
.Width(); // back to real position
91 static void lcl_InsertGraphic( const Graphic
& rGraphic
,
92 const OUString
& rFileName
, const OUString
& rFilterName
, bool bAsLink
, bool bApi
,
93 ScTabViewShell
* pViewSh
, vcl::Window
* pWindow
, SdrView
* pView
)
95 ScDrawView
* pDrawView
= pViewSh
->GetScDrawView();
97 // #i123922# check if an existing object is selected; if yes, evtl. replace
98 // the graphic for a SdrGraphObj (including link state updates) or adapt the fill
99 // style for other objects
100 if(pDrawView
&& 1 == pDrawView
->GetMarkedObjectCount())
102 SdrObject
* pPickObj
= pDrawView
->GetMarkedObjectByIndex(0);
106 //sal_Int8 nAction(DND_ACTION_MOVE);
108 const OUString
aBeginUndo(ScGlobal::GetRscString(STR_UNDO_DRAGDROP
));
109 const OUString aEmpty
;
111 SdrObject
* pResult
= pDrawView
->ApplyGraphicToObject(
115 bAsLink
? rFileName
: aEmpty
,
116 bAsLink
? rFilterName
: aEmpty
);
120 // we are done; mark the modified/new object
121 pDrawView
->MarkObj(pResult
, pDrawView
->GetSdrPageView());
127 // set the size so the graphic has its original pixel size
128 // at 100% view scale (as in SetMarkedOriginalSize),
129 // instead of respecting the current view scale
130 MapMode aSourceMap
= rGraphic
.GetPrefMapMode();
131 MapMode
aDestMap( MAP_100TH_MM
);
132 if ( aSourceMap
.GetMapUnit() == MAP_PIXEL
&& pDrawView
)
134 Fraction aScaleX
, aScaleY
;
135 pDrawView
->CalcNormScale( aScaleX
, aScaleY
);
136 aDestMap
.SetScaleX(aScaleX
);
137 aDestMap
.SetScaleY(aScaleY
);
139 Size aLogicSize
= pWindow
->LogicToLogic(
140 rGraphic
.GetPrefSize(), &aSourceMap
, &aDestMap
);
144 SdrPageView
* pPV
= pView
->GetSdrPageView();
145 SdrPage
* pPage
= pPV
->GetPage();
146 Point aInsertPos
= pViewSh
->GetInsertPos();
148 ScViewData
& rData
= pViewSh
->GetViewData();
149 if ( rData
.GetDocument()->IsNegativePage( rData
.GetTabNo() ) )
150 aInsertPos
.X() -= aLogicSize
.Width(); // move position to left edge
152 ScLimitSizeOnDrawPage( aLogicSize
, aInsertPos
, pPage
->GetSize() );
154 Rectangle
aRect ( aInsertPos
, aLogicSize
);
156 SdrGrafObj
* pObj
= new SdrGrafObj( rGraphic
, aRect
);
158 // calling SetGraphicLink here doesn't work
160 // Path is no longer used as name for the graphics object
162 ScDrawLayer
* pLayer
= static_cast<ScDrawLayer
*>(pView
->GetModel());
163 OUString aName
= pLayer
->GetNewGraphicName(); // "Graphics"
164 pObj
->SetName(aName
);
166 // don't select if from (dispatch) API, to allow subsequent cell operations
167 SdrInsertFlags nInsOptions
= bApi
? SdrInsertFlags::DONTMARK
: SdrInsertFlags::NONE
;
168 pView
->InsertObjectAtView( pObj
, *pPV
, nInsOptions
);
170 // SetGraphicLink has to be used after inserting the object,
171 // otherwise an empty graphic is swapped in and the contact stuff crashes.
174 pObj
->SetGraphicLink( rFileName
, ""/*TODO?*/, rFilterName
);
177 static void lcl_InsertMedia( const OUString
& rMediaURL
, bool bApi
,
178 ScTabViewShell
* pViewSh
, vcl::Window
* pWindow
, SdrView
* pView
,
179 const Size
& rPrefSize
, bool const bLink
)
181 SdrPageView
* pPV
= pView
->GetSdrPageView();
182 SdrPage
* pPage
= pPV
->GetPage();
183 ScViewData
& rData
= pViewSh
->GetViewData();
184 Point
aInsertPos( pViewSh
->GetInsertPos() );
187 if( rPrefSize
.Width() && rPrefSize
.Height() )
190 aSize
= pWindow
->PixelToLogic( rPrefSize
, MAP_100TH_MM
);
192 aSize
= Application::GetDefaultDevice()->PixelToLogic( rPrefSize
, MAP_100TH_MM
);
195 aSize
= Size( 5000, 5000 );
197 ScLimitSizeOnDrawPage( aSize
, aInsertPos
, pPage
->GetSize() );
199 if( rData
.GetDocument()->IsNegativePage( rData
.GetTabNo() ) )
200 aInsertPos
.X() -= aSize
.Width();
209 uno::Reference
<frame::XModel
> const xModel(
210 rData
.GetDocument()->GetDocumentShell()->GetModel());
211 bool const bRet
= ::avmedia::EmbedMedia(xModel
, rMediaURL
, realURL
);
212 if (!bRet
) { return; }
215 SdrMediaObj
* pObj
= new SdrMediaObj( Rectangle( aInsertPos
, aSize
) );
217 pObj
->SetModel(rData
.GetDocument()->GetDrawLayer()); // set before setURL
218 pObj
->setURL( realURL
, ""/*TODO?*/ );
219 pView
->InsertObjectAtView( pObj
, *pPV
, bApi
? SdrInsertFlags::DONTMARK
: SdrInsertFlags::NONE
);
222 /*************************************************************************
224 |* FuInsertGraphic::Konstruktor
226 \************************************************************************/
228 FuInsertGraphic::FuInsertGraphic( ScTabViewShell
* pViewSh
,
233 : FuPoor(pViewSh
, pWin
, pViewP
, pDoc
, rReq
)
235 const SfxItemSet
* pReqArgs
= rReq
.GetArgs();
236 const SfxPoolItem
* pItem
;
238 pReqArgs
->GetItemState( SID_INSERT_GRAPHIC
, true, &pItem
) == SfxItemState::SET
)
240 OUString aFileName
= static_cast<const SfxStringItem
*>(pItem
)->GetValue();
242 OUString aFilterName
;
243 if ( pReqArgs
->GetItemState( FN_PARAM_FILTER
, true, &pItem
) == SfxItemState::SET
)
244 aFilterName
= static_cast<const SfxStringItem
*>(pItem
)->GetValue();
246 bool bAsLink
= false;
247 if ( pReqArgs
->GetItemState( FN_PARAM_1
, true, &pItem
) == SfxItemState::SET
)
248 bAsLink
= static_cast<const SfxBoolItem
*>(pItem
)->GetValue();
251 int nError
= GraphicFilter::LoadGraphic( aFileName
, aFilterName
, aGraphic
, &GraphicFilter::GetGraphicFilter() );
252 if ( nError
== GRFILTER_OK
)
254 lcl_InsertGraphic( aGraphic
, aFileName
, aFilterName
, bAsLink
, true, pViewSh
, pWindow
, pView
);
259 SvxOpenGraphicDialog
aDlg(ScResId(STR_INSERTGRAPHIC
));
261 if( aDlg
.Execute() == GRFILTER_OK
)
264 int nError
= aDlg
.GetGraphic(aGraphic
);
265 if( nError
== GRFILTER_OK
)
267 OUString aFileName
= aDlg
.GetPath();
268 OUString aFilterName
= aDlg
.GetCurrentFilter();
269 bool bAsLink
= aDlg
.IsAsLink();
271 // really store as link only?
272 if( bAsLink
&& SvtMiscOptions().ShowLinkWarningDialog() )
274 ScopedVclPtrInstance
< SvxLinkWarningDialog
> aWarnDlg(pWin
,aFileName
);
275 if( aWarnDlg
->Execute() != RET_OK
)
276 bAsLink
= false; // don't store as link
279 lcl_InsertGraphic( aGraphic
, aFileName
, aFilterName
, bAsLink
, false, pViewSh
, pWindow
, pView
);
281 // append items for recording
282 rReq
.AppendItem( SfxStringItem( SID_INSERT_GRAPHIC
, aFileName
) );
283 rReq
.AppendItem( SfxStringItem( FN_PARAM_FILTER
, aFilterName
) );
284 rReq
.AppendItem( SfxBoolItem( FN_PARAM_1
, bAsLink
) );
289 // error is handled in SvxOpenGraphicDialog::GetGraphic
295 /*************************************************************************
297 |* FuInsertGraphic::Destruktor
299 \************************************************************************/
301 FuInsertGraphic::~FuInsertGraphic()
305 /*************************************************************************
307 |* FuInsertGraphic::Function aktivieren
309 \************************************************************************/
311 void FuInsertGraphic::Activate()
316 /*************************************************************************
318 |* FuInsertGraphic::Function deaktivieren
320 \************************************************************************/
322 void FuInsertGraphic::Deactivate()
324 FuPoor::Deactivate();
327 /*************************************************************************
329 |* FuInsertMedia::Konstruktor
331 \************************************************************************/
333 FuInsertMedia::FuInsertMedia( ScTabViewShell
* pViewSh
,
338 FuPoor(pViewSh
, pWin
, pViewP
, pDoc
, rReq
)
341 const SfxItemSet
* pReqArgs
= rReq
.GetArgs();
346 const SfxStringItem
* pStringItem
= PTR_CAST( SfxStringItem
, &pReqArgs
->Get( rReq
.GetSlot() ) );
350 aURL
= pStringItem
->GetValue();
351 bAPI
= aURL
.getLength();
357 ::avmedia::MediaWindow::executeMediaURLDialog(pWindow
, aURL
, & bLink
))
364 if( !::avmedia::MediaWindow::isMediaURL( aURL
, ""/*TODO?*/, true, &aPrefSize
) )
370 ::avmedia::MediaWindow::executeFormatErrorBox( pWindow
);
374 lcl_InsertMedia( aURL
, bAPI
, pViewSh
, pWindow
, pView
, aPrefSize
,
383 /*************************************************************************
385 |* FuInsertMedia::Destruktor
387 \************************************************************************/
389 FuInsertMedia::~FuInsertMedia()
393 /*************************************************************************
395 |* FuInsertMedia::Function aktivieren
397 \************************************************************************/
399 void FuInsertMedia::Activate()
404 /*************************************************************************
406 |* FuInsertMedia::Function deaktivieren
408 \************************************************************************/
410 void FuInsertMedia::Deactivate()
412 FuPoor::Deactivate();
415 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */