fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / sc / source / ui / drawfunc / fuins1.cxx
blob0c94d8b3c7face56e54f2e721b53de3e533ccacb
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 <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"
41 #include "sc.hrc"
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() )
49 return;
51 Size aPageSize = rPage;
52 bool bNegative = aPageSize.Width() < 0;
53 if ( bNegative )
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();
65 if ( fX < fY )
67 rSize.Width() = aPageSize.Width();
68 rSize.Height() = (long) ( rSize.Height() * fX );
70 else
72 rSize.Height() = aPageSize.Height();
73 rSize.Width() = (long) ( rSize.Width() * fY );
76 if (!rSize.Width())
77 rSize.Width() = 1;
78 if (!rSize.Height())
79 rSize.Height() = 1;
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();
87 if ( bNegative )
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);
104 if(pPickObj)
106 //sal_Int8 nAction(DND_ACTION_MOVE);
107 //Point aPos;
108 const OUString aBeginUndo(ScGlobal::GetRscString(STR_UNDO_DRAGDROP));
109 const OUString aEmpty;
111 SdrObject* pResult = pDrawView->ApplyGraphicToObject(
112 *pPickObj,
113 rGraphic,
114 aBeginUndo,
115 bAsLink ? rFileName : aEmpty,
116 bAsLink ? rFilterName : aEmpty);
118 if(pResult)
120 // we are done; mark the modified/new object
121 pDrawView->MarkObj(pResult, pDrawView->GetSdrPageView());
122 return;
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 );
142 // Limit size
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.
172 // See #i37444#.
173 if ( bAsLink )
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() );
185 Size aSize;
187 if( rPrefSize.Width() && rPrefSize.Height() )
189 if( pWindow )
190 aSize = pWindow->PixelToLogic( rPrefSize, MAP_100TH_MM );
191 else
192 aSize = Application::GetDefaultDevice()->PixelToLogic( rPrefSize, MAP_100TH_MM );
194 else
195 aSize = Size( 5000, 5000 );
197 ScLimitSizeOnDrawPage( aSize, aInsertPos, pPage->GetSize() );
199 if( rData.GetDocument()->IsNegativePage( rData.GetTabNo() ) )
200 aInsertPos.X() -= aSize.Width();
202 OUString realURL;
203 if (bLink)
205 realURL = rMediaURL;
207 else
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,
229 vcl::Window* pWin,
230 ScDrawView* pViewP,
231 SdrModel* pDoc,
232 SfxRequest& rReq )
233 : FuPoor(pViewSh, pWin, pViewP, pDoc, rReq)
235 const SfxItemSet* pReqArgs = rReq.GetArgs();
236 const SfxPoolItem* pItem;
237 if ( pReqArgs &&
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();
250 Graphic aGraphic;
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 );
257 else
259 SvxOpenGraphicDialog aDlg(ScResId(STR_INSERTGRAPHIC));
261 if( aDlg.Execute() == GRFILTER_OK )
263 Graphic aGraphic;
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 ) );
285 rReq.Done();
287 else
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()
313 FuPoor::Activate();
316 /*************************************************************************
318 |* FuInsertGraphic::Function deaktivieren
320 \************************************************************************/
322 void FuInsertGraphic::Deactivate()
324 FuPoor::Deactivate();
327 /*************************************************************************
329 |* FuInsertMedia::Konstruktor
331 \************************************************************************/
333 FuInsertMedia::FuInsertMedia( ScTabViewShell* pViewSh,
334 vcl::Window* pWin,
335 ScDrawView* pViewP,
336 SdrModel* pDoc,
337 SfxRequest& rReq ) :
338 FuPoor(pViewSh, pWin, pViewP, pDoc, rReq)
340 OUString aURL;
341 const SfxItemSet* pReqArgs = rReq.GetArgs();
342 bool bAPI = false;
344 if( pReqArgs )
346 const SfxStringItem* pStringItem = PTR_CAST( SfxStringItem, &pReqArgs->Get( rReq.GetSlot() ) );
348 if( pStringItem )
350 aURL = pStringItem->GetValue();
351 bAPI = aURL.getLength();
355 bool bLink(true);
356 if (bAPI ||
357 ::avmedia::MediaWindow::executeMediaURLDialog(pWindow, aURL, & bLink))
359 Size aPrefSize;
361 if( pWin )
362 pWin->EnterWait();
364 if( !::avmedia::MediaWindow::isMediaURL( aURL, ""/*TODO?*/, true, &aPrefSize ) )
366 if( pWin )
367 pWin->LeaveWait();
369 if( !bAPI )
370 ::avmedia::MediaWindow::executeFormatErrorBox( pWindow );
372 else
374 lcl_InsertMedia( aURL, bAPI, pViewSh, pWindow, pView, aPrefSize,
375 bLink );
377 if( pWin )
378 pWin->LeaveWait();
383 /*************************************************************************
385 |* FuInsertMedia::Destruktor
387 \************************************************************************/
389 FuInsertMedia::~FuInsertMedia()
393 /*************************************************************************
395 |* FuInsertMedia::Function aktivieren
397 \************************************************************************/
399 void FuInsertMedia::Activate()
401 FuPoor::Activate();
404 /*************************************************************************
406 |* FuInsertMedia::Function deaktivieren
408 \************************************************************************/
410 void FuInsertMedia::Deactivate()
412 FuPoor::Deactivate();
415 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */