Stop leaking all ScPostIt instances.
[LibreOffice.git] / sc / source / ui / drawfunc / fuins1.cxx
blobeb184d0d5d332e2aee0110e258dada9265bff136
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 .
21 #include <sfx2/opengrf.hxx>
22 #include <svx/svdograf.hxx>
23 #include <svx/svdomedia.hxx>
24 #include <svx/svdpage.hxx>
25 #include <svx/svdpagv.hxx>
26 #include <svx/svdview.hxx>
27 #include <svx/linkwarn.hxx>
28 #include <vcl/graphicfilter.hxx>
29 #include <svl/stritem.hxx>
30 #include <svtools/miscopt.hxx>
31 #include <vcl/msgbox.hxx>
32 #include <avmedia/mediawindow.hxx>
33 #include <vcl/svapp.hxx>
35 #include "fuinsert.hxx"
36 #include "tabvwsh.hxx"
37 #include "drwlayer.hxx"
38 #include "drawview.hxx"
39 #include "document.hxx"
40 #include "scresid.hxx"
41 #include "progress.hxx"
42 #include "sc.hrc"
45 using namespace ::com::sun::star;
47 //------------------------------------------------------------------------
49 void SC_DLLPUBLIC ScLimitSizeOnDrawPage( Size& rSize, Point& rPos, const Size& rPage )
51 if ( !rPage.Width() || !rPage.Height() )
52 return;
54 Size aPageSize = rPage;
55 sal_Bool bNegative = aPageSize.Width() < 0;
56 if ( bNegative )
58 // make everything positive temporarily
59 aPageSize.Width() = -aPageSize.Width();
60 rPos.X() = -rPos.X() - rSize.Width();
63 if ( rSize.Width() > aPageSize.Width() || rSize.Height() > aPageSize.Height() )
65 double fX = aPageSize.Width() / (double) rSize.Width();
66 double fY = aPageSize.Height() / (double) rSize.Height();
68 if ( fX < fY )
70 rSize.Width() = aPageSize.Width();
71 rSize.Height() = (long) ( rSize.Height() * fX );
73 else
75 rSize.Height() = aPageSize.Height();
76 rSize.Width() = (long) ( rSize.Width() * fY );
79 if (!rSize.Width())
80 rSize.Width() = 1;
81 if (!rSize.Height())
82 rSize.Height() = 1;
85 if ( rPos.X() + rSize.Width() > aPageSize.Width() )
86 rPos.X() = aPageSize.Width() - rSize.Width();
87 if ( rPos.Y() + rSize.Height() > aPageSize.Height() )
88 rPos.Y() = aPageSize.Height() - rSize.Height();
90 if ( bNegative )
91 rPos.X() = -rPos.X() - rSize.Width(); // back to real position
94 //------------------------------------------------------------------------
96 static void lcl_InsertGraphic( const Graphic& rGraphic,
97 const OUString& rFileName, const OUString& rFilterName, sal_Bool bAsLink, sal_Bool bApi,
98 ScTabViewShell* pViewSh, Window* pWindow, SdrView* pView )
100 // set the size so the graphic has its original pixel size
101 // at 100% view scale (as in SetMarkedOriginalSize),
102 // instead of respecting the current view scale
104 ScDrawView* pDrawView = pViewSh->GetScDrawView();
105 MapMode aSourceMap = rGraphic.GetPrefMapMode();
106 MapMode aDestMap( MAP_100TH_MM );
107 if ( aSourceMap.GetMapUnit() == MAP_PIXEL && pDrawView )
109 Fraction aScaleX, aScaleY;
110 pDrawView->CalcNormScale( aScaleX, aScaleY );
111 aDestMap.SetScaleX(aScaleX);
112 aDestMap.SetScaleY(aScaleY);
114 Size aLogicSize = pWindow->LogicToLogic(
115 rGraphic.GetPrefSize(), &aSourceMap, &aDestMap );
117 // Limit size
119 SdrPageView* pPV = pView->GetSdrPageView();
120 SdrPage* pPage = pPV->GetPage();
121 Point aInsertPos = pViewSh->GetInsertPos();
123 ScViewData* pData = pViewSh->GetViewData();
124 if ( pData->GetDocument()->IsNegativePage( pData->GetTabNo() ) )
125 aInsertPos.X() -= aLogicSize.Width(); // move position to left edge
127 ScLimitSizeOnDrawPage( aLogicSize, aInsertPos, pPage->GetSize() );
129 Rectangle aRect ( aInsertPos, aLogicSize );
131 SdrGrafObj* pObj = new SdrGrafObj( rGraphic, aRect );
133 // calling SetGraphicLink here doesn't work
135 // Path is no longer used as name for the graphics object
137 ScDrawLayer* pLayer = (ScDrawLayer*) pView->GetModel();
138 OUString aName = pLayer->GetNewGraphicName(); // "Graphics"
139 pObj->SetName(aName);
141 // don't select if from (dispatch) API, to allow subsequent cell operations
142 sal_uLong nInsOptions = bApi ? SDRINSERT_DONTMARK : 0;
143 pView->InsertObjectAtView( pObj, *pPV, nInsOptions );
145 // SetGraphicLink has to be used after inserting the object,
146 // otherwise an empty graphic is swapped in and the contact stuff crashes.
147 // See #i37444#.
148 if ( bAsLink )
149 pObj->SetGraphicLink( rFileName, ""/*TODO?*/, rFilterName );
152 //------------------------------------------------------------------------
154 static void lcl_InsertMedia( const OUString& rMediaURL, bool bApi,
155 ScTabViewShell* pViewSh, Window* pWindow, SdrView* pView,
156 const Size& rPrefSize, bool const bLink )
158 SdrPageView* pPV = pView->GetSdrPageView();
159 SdrPage* pPage = pPV->GetPage();
160 ScViewData* pData = pViewSh->GetViewData();
161 Point aInsertPos( pViewSh->GetInsertPos() );
162 Size aSize;
164 if( rPrefSize.Width() && rPrefSize.Height() )
166 if( pWindow )
167 aSize = pWindow->PixelToLogic( rPrefSize, MAP_100TH_MM );
168 else
169 aSize = Application::GetDefaultDevice()->PixelToLogic( rPrefSize, MAP_100TH_MM );
171 else
172 aSize = Size( 5000, 5000 );
174 ScLimitSizeOnDrawPage( aSize, aInsertPos, pPage->GetSize() );
176 if( pData->GetDocument()->IsNegativePage( pData->GetTabNo() ) )
177 aInsertPos.X() -= aSize.Width();
179 OUString realURL;
180 if (bLink)
182 realURL = rMediaURL;
184 else
186 uno::Reference<frame::XModel> const xModel(
187 pData->GetDocument()->GetDocumentShell()->GetModel());
188 bool const bRet = ::avmedia::EmbedMedia(xModel, rMediaURL, realURL);
189 if (!bRet) { return; }
192 SdrMediaObj* pObj = new SdrMediaObj( Rectangle( aInsertPos, aSize ) );
194 pObj->SetModel(pData->GetDocument()->GetDrawLayer()); // set before setURL
195 pObj->setURL( realURL, ""/*TODO?*/ );
196 pView->InsertObjectAtView( pObj, *pPV, bApi ? SDRINSERT_DONTMARK : 0 );
199 /*************************************************************************
201 |* FuInsertGraphic::Konstruktor
203 \************************************************************************/
205 FuInsertGraphic::FuInsertGraphic( ScTabViewShell* pViewSh,
206 Window* pWin,
207 ScDrawView* pViewP,
208 SdrModel* pDoc,
209 SfxRequest& rReq )
210 : FuPoor(pViewSh, pWin, pViewP, pDoc, rReq)
212 const SfxItemSet* pReqArgs = rReq.GetArgs();
213 const SfxPoolItem* pItem;
214 if ( pReqArgs &&
215 pReqArgs->GetItemState( SID_INSERT_GRAPHIC, sal_True, &pItem ) == SFX_ITEM_SET )
217 OUString aFileName = ((const SfxStringItem*)pItem)->GetValue();
219 OUString aFilterName;
220 if ( pReqArgs->GetItemState( FN_PARAM_FILTER, sal_True, &pItem ) == SFX_ITEM_SET )
221 aFilterName = ((const SfxStringItem*)pItem)->GetValue();
223 sal_Bool bAsLink = false;
224 if ( pReqArgs->GetItemState( FN_PARAM_1, sal_True, &pItem ) == SFX_ITEM_SET )
225 bAsLink = ((const SfxBoolItem*)pItem)->GetValue();
227 Graphic aGraphic;
228 int nError = GraphicFilter::LoadGraphic( aFileName, aFilterName, aGraphic, &GraphicFilter::GetGraphicFilter() );
229 if ( nError == GRFILTER_OK )
231 lcl_InsertGraphic( aGraphic, aFileName, aFilterName, bAsLink, sal_True, pViewSh, pWindow, pView );
234 else
236 SvxOpenGraphicDialog aDlg(ScResId(STR_INSERTGRAPHIC));
238 if( aDlg.Execute() == GRFILTER_OK )
240 Graphic aGraphic;
241 int nError = aDlg.GetGraphic(aGraphic);
242 if( nError == GRFILTER_OK )
244 OUString aFileName = aDlg.GetPath();
245 OUString aFilterName = aDlg.GetCurrentFilter();
246 sal_Bool bAsLink = aDlg.IsAsLink();
248 // really store as link only?
249 if( bAsLink && SvtMiscOptions().ShowLinkWarningDialog() )
251 SvxLinkWarningDialog aWarnDlg(pWin,aFileName);
252 if( aWarnDlg.Execute() != RET_OK )
253 bAsLink = false; // don't store as link
256 lcl_InsertGraphic( aGraphic, aFileName, aFilterName, bAsLink, false, pViewSh, pWindow, pView );
258 // append items for recording
259 rReq.AppendItem( SfxStringItem( SID_INSERT_GRAPHIC, aFileName ) );
260 rReq.AppendItem( SfxStringItem( FN_PARAM_FILTER, aFilterName ) );
261 rReq.AppendItem( SfxBoolItem( FN_PARAM_1, bAsLink ) );
262 rReq.Done();
264 else
266 // error is handled in SvxOpenGraphicDialog::GetGraphic
272 /*************************************************************************
274 |* FuInsertGraphic::Destruktor
276 \************************************************************************/
278 FuInsertGraphic::~FuInsertGraphic()
282 /*************************************************************************
284 |* FuInsertGraphic::Function aktivieren
286 \************************************************************************/
288 void FuInsertGraphic::Activate()
290 FuPoor::Activate();
293 /*************************************************************************
295 |* FuInsertGraphic::Function deaktivieren
297 \************************************************************************/
299 void FuInsertGraphic::Deactivate()
301 FuPoor::Deactivate();
304 /*************************************************************************
306 |* FuInsertMedia::Konstruktor
308 \************************************************************************/
310 FuInsertMedia::FuInsertMedia( ScTabViewShell* pViewSh,
311 Window* pWin,
312 ScDrawView* pViewP,
313 SdrModel* pDoc,
314 SfxRequest& rReq ) :
315 FuPoor(pViewSh, pWin, pViewP, pDoc, rReq)
317 OUString aURL;
318 const SfxItemSet* pReqArgs = rReq.GetArgs();
319 bool bAPI = false;
321 if( pReqArgs )
323 const SfxStringItem* pStringItem = PTR_CAST( SfxStringItem, &pReqArgs->Get( rReq.GetSlot() ) );
325 if( pStringItem )
327 aURL = pStringItem->GetValue();
328 bAPI = aURL.getLength();
332 bool bLink(true);
333 if (bAPI ||
334 ::avmedia::MediaWindow::executeMediaURLDialog(pWindow, aURL, & bLink))
336 Size aPrefSize;
338 if( pWin )
339 pWin->EnterWait();
341 if( !::avmedia::MediaWindow::isMediaURL( aURL, ""/*TODO?*/, true, &aPrefSize ) )
343 if( pWin )
344 pWin->LeaveWait();
346 if( !bAPI )
347 ::avmedia::MediaWindow::executeFormatErrorBox( pWindow );
349 else
351 lcl_InsertMedia( aURL, bAPI, pViewSh, pWindow, pView, aPrefSize,
352 bLink );
354 if( pWin )
355 pWin->LeaveWait();
360 /*************************************************************************
362 |* FuInsertMedia::Destruktor
364 \************************************************************************/
366 FuInsertMedia::~FuInsertMedia()
370 /*************************************************************************
372 |* FuInsertMedia::Function aktivieren
374 \************************************************************************/
376 void FuInsertMedia::Activate()
378 FuPoor::Activate();
381 /*************************************************************************
383 |* FuInsertMedia::Function deaktivieren
385 \************************************************************************/
387 void FuInsertMedia::Deactivate()
389 FuPoor::Deactivate();
392 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */