Version 4.3.0.0.beta1, tag libreoffice-4.3.0.0.beta1
[LibreOffice.git] / sc / source / ui / drawfunc / fuins1.cxx
blob096219e379e3a787c9387a3dc86ffc9f4bfb2735
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"
43 #include "globstr.hrc"
46 using namespace ::com::sun::star;
48 void SC_DLLPUBLIC ScLimitSizeOnDrawPage( Size& rSize, Point& rPos, const Size& rPage )
50 if ( !rPage.Width() || !rPage.Height() )
51 return;
53 Size aPageSize = rPage;
54 bool bNegative = aPageSize.Width() < 0;
55 if ( bNegative )
57 // make everything positive temporarily
58 aPageSize.Width() = -aPageSize.Width();
59 rPos.X() = -rPos.X() - rSize.Width();
62 if ( rSize.Width() > aPageSize.Width() || rSize.Height() > aPageSize.Height() )
64 double fX = aPageSize.Width() / (double) rSize.Width();
65 double fY = aPageSize.Height() / (double) rSize.Height();
67 if ( fX < fY )
69 rSize.Width() = aPageSize.Width();
70 rSize.Height() = (long) ( rSize.Height() * fX );
72 else
74 rSize.Height() = aPageSize.Height();
75 rSize.Width() = (long) ( rSize.Width() * fY );
78 if (!rSize.Width())
79 rSize.Width() = 1;
80 if (!rSize.Height())
81 rSize.Height() = 1;
84 if ( rPos.X() + rSize.Width() > aPageSize.Width() )
85 rPos.X() = aPageSize.Width() - rSize.Width();
86 if ( rPos.Y() + rSize.Height() > aPageSize.Height() )
87 rPos.Y() = aPageSize.Height() - rSize.Height();
89 if ( bNegative )
90 rPos.X() = -rPos.X() - rSize.Width(); // back to real position
93 static void lcl_InsertGraphic( const Graphic& rGraphic,
94 const OUString& rFileName, const OUString& rFilterName, bool bAsLink, bool bApi,
95 ScTabViewShell* pViewSh, Window* pWindow, SdrView* pView )
97 ScDrawView* pDrawView = pViewSh->GetScDrawView();
99 // #i123922# check if an existing object is selected; if yes, evtl. replace
100 // the graphic for a SdrGraphObj (including link state updates) or adapt the fill
101 // style for other objects
102 if(pDrawView && 1 == pDrawView->GetMarkedObjectCount())
104 SdrObject* pPickObj = pDrawView->GetMarkedObjectByIndex(0);
106 if(pPickObj)
108 //sal_Int8 nAction(DND_ACTION_MOVE);
109 //Point aPos;
110 const OUString aBeginUndo(ScGlobal::GetRscString(STR_UNDO_DRAGDROP));
111 const OUString aEmpty;
113 SdrObject* pResult = pDrawView->ApplyGraphicToObject(
114 *pPickObj,
115 rGraphic,
116 aBeginUndo,
117 bAsLink ? rFileName : aEmpty,
118 bAsLink ? rFilterName : aEmpty);
120 if(pResult)
122 // we are done; mark the modified/new object
123 pDrawView->MarkObj(pResult, pDrawView->GetSdrPageView());
124 return;
129 // set the size so the graphic has its original pixel size
130 // at 100% view scale (as in SetMarkedOriginalSize),
131 // instead of respecting the current view scale
132 MapMode aSourceMap = rGraphic.GetPrefMapMode();
133 MapMode aDestMap( MAP_100TH_MM );
134 if ( aSourceMap.GetMapUnit() == MAP_PIXEL && pDrawView )
136 Fraction aScaleX, aScaleY;
137 pDrawView->CalcNormScale( aScaleX, aScaleY );
138 aDestMap.SetScaleX(aScaleX);
139 aDestMap.SetScaleY(aScaleY);
141 Size aLogicSize = pWindow->LogicToLogic(
142 rGraphic.GetPrefSize(), &aSourceMap, &aDestMap );
144 // Limit size
146 SdrPageView* pPV = pView->GetSdrPageView();
147 SdrPage* pPage = pPV->GetPage();
148 Point aInsertPos = pViewSh->GetInsertPos();
150 ScViewData* pData = pViewSh->GetViewData();
151 if ( pData->GetDocument()->IsNegativePage( pData->GetTabNo() ) )
152 aInsertPos.X() -= aLogicSize.Width(); // move position to left edge
154 ScLimitSizeOnDrawPage( aLogicSize, aInsertPos, pPage->GetSize() );
156 Rectangle aRect ( aInsertPos, aLogicSize );
158 SdrGrafObj* pObj = new SdrGrafObj( rGraphic, aRect );
160 // calling SetGraphicLink here doesn't work
162 // Path is no longer used as name for the graphics object
164 ScDrawLayer* pLayer = (ScDrawLayer*) pView->GetModel();
165 OUString aName = pLayer->GetNewGraphicName(); // "Graphics"
166 pObj->SetName(aName);
168 // don't select if from (dispatch) API, to allow subsequent cell operations
169 sal_uLong nInsOptions = bApi ? SDRINSERT_DONTMARK : 0;
170 pView->InsertObjectAtView( pObj, *pPV, nInsOptions );
172 // SetGraphicLink has to be used after inserting the object,
173 // otherwise an empty graphic is swapped in and the contact stuff crashes.
174 // See #i37444#.
175 if ( bAsLink )
176 pObj->SetGraphicLink( rFileName, ""/*TODO?*/, rFilterName );
179 static void lcl_InsertMedia( const OUString& rMediaURL, bool bApi,
180 ScTabViewShell* pViewSh, Window* pWindow, SdrView* pView,
181 const Size& rPrefSize, bool const bLink )
183 SdrPageView* pPV = pView->GetSdrPageView();
184 SdrPage* pPage = pPV->GetPage();
185 ScViewData* pData = pViewSh->GetViewData();
186 Point aInsertPos( pViewSh->GetInsertPos() );
187 Size aSize;
189 if( rPrefSize.Width() && rPrefSize.Height() )
191 if( pWindow )
192 aSize = pWindow->PixelToLogic( rPrefSize, MAP_100TH_MM );
193 else
194 aSize = Application::GetDefaultDevice()->PixelToLogic( rPrefSize, MAP_100TH_MM );
196 else
197 aSize = Size( 5000, 5000 );
199 ScLimitSizeOnDrawPage( aSize, aInsertPos, pPage->GetSize() );
201 if( pData->GetDocument()->IsNegativePage( pData->GetTabNo() ) )
202 aInsertPos.X() -= aSize.Width();
204 OUString realURL;
205 if (bLink)
207 realURL = rMediaURL;
209 else
211 uno::Reference<frame::XModel> const xModel(
212 pData->GetDocument()->GetDocumentShell()->GetModel());
213 bool const bRet = ::avmedia::EmbedMedia(xModel, rMediaURL, realURL);
214 if (!bRet) { return; }
217 SdrMediaObj* pObj = new SdrMediaObj( Rectangle( aInsertPos, aSize ) );
219 pObj->SetModel(pData->GetDocument()->GetDrawLayer()); // set before setURL
220 pObj->setURL( realURL, ""/*TODO?*/ );
221 pView->InsertObjectAtView( pObj, *pPV, bApi ? SDRINSERT_DONTMARK : 0 );
224 /*************************************************************************
226 |* FuInsertGraphic::Konstruktor
228 \************************************************************************/
230 FuInsertGraphic::FuInsertGraphic( ScTabViewShell* pViewSh,
231 Window* pWin,
232 ScDrawView* pViewP,
233 SdrModel* pDoc,
234 SfxRequest& rReq )
235 : FuPoor(pViewSh, pWin, pViewP, pDoc, rReq)
237 const SfxItemSet* pReqArgs = rReq.GetArgs();
238 const SfxPoolItem* pItem;
239 if ( pReqArgs &&
240 pReqArgs->GetItemState( SID_INSERT_GRAPHIC, true, &pItem ) == SFX_ITEM_SET )
242 OUString aFileName = ((const SfxStringItem*)pItem)->GetValue();
244 OUString aFilterName;
245 if ( pReqArgs->GetItemState( FN_PARAM_FILTER, true, &pItem ) == SFX_ITEM_SET )
246 aFilterName = ((const SfxStringItem*)pItem)->GetValue();
248 bool bAsLink = false;
249 if ( pReqArgs->GetItemState( FN_PARAM_1, true, &pItem ) == SFX_ITEM_SET )
250 bAsLink = ((const SfxBoolItem*)pItem)->GetValue();
252 Graphic aGraphic;
253 int nError = GraphicFilter::LoadGraphic( aFileName, aFilterName, aGraphic, &GraphicFilter::GetGraphicFilter() );
254 if ( nError == GRFILTER_OK )
256 lcl_InsertGraphic( aGraphic, aFileName, aFilterName, bAsLink, true, pViewSh, pWindow, pView );
259 else
261 SvxOpenGraphicDialog aDlg(ScResId(STR_INSERTGRAPHIC));
263 if( aDlg.Execute() == GRFILTER_OK )
265 Graphic aGraphic;
266 int nError = aDlg.GetGraphic(aGraphic);
267 if( nError == GRFILTER_OK )
269 OUString aFileName = aDlg.GetPath();
270 OUString aFilterName = aDlg.GetCurrentFilter();
271 bool bAsLink = aDlg.IsAsLink();
273 // really store as link only?
274 if( bAsLink && SvtMiscOptions().ShowLinkWarningDialog() )
276 SvxLinkWarningDialog aWarnDlg(pWin,aFileName);
277 if( aWarnDlg.Execute() != RET_OK )
278 bAsLink = false; // don't store as link
281 lcl_InsertGraphic( aGraphic, aFileName, aFilterName, bAsLink, false, pViewSh, pWindow, pView );
283 // append items for recording
284 rReq.AppendItem( SfxStringItem( SID_INSERT_GRAPHIC, aFileName ) );
285 rReq.AppendItem( SfxStringItem( FN_PARAM_FILTER, aFilterName ) );
286 rReq.AppendItem( SfxBoolItem( FN_PARAM_1, bAsLink ) );
287 rReq.Done();
289 else
291 // error is handled in SvxOpenGraphicDialog::GetGraphic
297 /*************************************************************************
299 |* FuInsertGraphic::Destruktor
301 \************************************************************************/
303 FuInsertGraphic::~FuInsertGraphic()
307 /*************************************************************************
309 |* FuInsertGraphic::Function aktivieren
311 \************************************************************************/
313 void FuInsertGraphic::Activate()
315 FuPoor::Activate();
318 /*************************************************************************
320 |* FuInsertGraphic::Function deaktivieren
322 \************************************************************************/
324 void FuInsertGraphic::Deactivate()
326 FuPoor::Deactivate();
329 /*************************************************************************
331 |* FuInsertMedia::Konstruktor
333 \************************************************************************/
335 FuInsertMedia::FuInsertMedia( ScTabViewShell* pViewSh,
336 Window* pWin,
337 ScDrawView* pViewP,
338 SdrModel* pDoc,
339 SfxRequest& rReq ) :
340 FuPoor(pViewSh, pWin, pViewP, pDoc, rReq)
342 OUString aURL;
343 const SfxItemSet* pReqArgs = rReq.GetArgs();
344 bool bAPI = false;
346 if( pReqArgs )
348 const SfxStringItem* pStringItem = PTR_CAST( SfxStringItem, &pReqArgs->Get( rReq.GetSlot() ) );
350 if( pStringItem )
352 aURL = pStringItem->GetValue();
353 bAPI = aURL.getLength();
357 bool bLink(true);
358 if (bAPI ||
359 ::avmedia::MediaWindow::executeMediaURLDialog(pWindow, aURL, & bLink))
361 Size aPrefSize;
363 if( pWin )
364 pWin->EnterWait();
366 if( !::avmedia::MediaWindow::isMediaURL( aURL, ""/*TODO?*/, true, &aPrefSize ) )
368 if( pWin )
369 pWin->LeaveWait();
371 if( !bAPI )
372 ::avmedia::MediaWindow::executeFormatErrorBox( pWindow );
374 else
376 lcl_InsertMedia( aURL, bAPI, pViewSh, pWindow, pView, aPrefSize,
377 bLink );
379 if( pWin )
380 pWin->LeaveWait();
385 /*************************************************************************
387 |* FuInsertMedia::Destruktor
389 \************************************************************************/
391 FuInsertMedia::~FuInsertMedia()
395 /*************************************************************************
397 |* FuInsertMedia::Function aktivieren
399 \************************************************************************/
401 void FuInsertMedia::Activate()
403 FuPoor::Activate();
406 /*************************************************************************
408 |* FuInsertMedia::Function deaktivieren
410 \************************************************************************/
412 void FuInsertMedia::Deactivate()
414 FuPoor::Deactivate();
417 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */