Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 :
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>
34 :
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"
44 :
45 :
46 : using namespace ::com::sun::star;
47 :
48 0 : void SC_DLLPUBLIC ScLimitSizeOnDrawPage( Size& rSize, Point& rPos, const Size& rPage )
49 : {
50 0 : if ( !rPage.Width() || !rPage.Height() )
51 0 : return;
52 :
53 0 : Size aPageSize = rPage;
54 0 : sal_Bool bNegative = aPageSize.Width() < 0;
55 0 : if ( bNegative )
56 : {
57 : // make everything positive temporarily
58 0 : aPageSize.Width() = -aPageSize.Width();
59 0 : rPos.X() = -rPos.X() - rSize.Width();
60 : }
61 :
62 0 : if ( rSize.Width() > aPageSize.Width() || rSize.Height() > aPageSize.Height() )
63 : {
64 0 : double fX = aPageSize.Width() / (double) rSize.Width();
65 0 : double fY = aPageSize.Height() / (double) rSize.Height();
66 :
67 0 : if ( fX < fY )
68 : {
69 0 : rSize.Width() = aPageSize.Width();
70 0 : rSize.Height() = (long) ( rSize.Height() * fX );
71 : }
72 : else
73 : {
74 0 : rSize.Height() = aPageSize.Height();
75 0 : rSize.Width() = (long) ( rSize.Width() * fY );
76 : }
77 :
78 0 : if (!rSize.Width())
79 0 : rSize.Width() = 1;
80 0 : if (!rSize.Height())
81 0 : rSize.Height() = 1;
82 : }
83 :
84 0 : if ( rPos.X() + rSize.Width() > aPageSize.Width() )
85 0 : rPos.X() = aPageSize.Width() - rSize.Width();
86 0 : if ( rPos.Y() + rSize.Height() > aPageSize.Height() )
87 0 : rPos.Y() = aPageSize.Height() - rSize.Height();
88 :
89 0 : if ( bNegative )
90 0 : rPos.X() = -rPos.X() - rSize.Width(); // back to real position
91 : }
92 :
93 0 : static void lcl_InsertGraphic( const Graphic& rGraphic,
94 : const OUString& rFileName, const OUString& rFilterName, sal_Bool bAsLink, sal_Bool bApi,
95 : ScTabViewShell* pViewSh, Window* pWindow, SdrView* pView )
96 : {
97 0 : ScDrawView* pDrawView = pViewSh->GetScDrawView();
98 :
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 0 : if(pDrawView && 1 == pDrawView->GetMarkedObjectCount())
103 : {
104 0 : SdrObject* pPickObj = pDrawView->GetMarkedObjectByIndex(0);
105 :
106 0 : if(pPickObj)
107 : {
108 : //sal_Int8 nAction(DND_ACTION_MOVE);
109 : //Point aPos;
110 0 : const OUString aBeginUndo(ScGlobal::GetRscString(STR_UNDO_DRAGDROP));
111 0 : const OUString aEmpty;
112 :
113 : SdrObject* pResult = pDrawView->ApplyGraphicToObject(
114 : *pPickObj,
115 : rGraphic,
116 : aBeginUndo,
117 : bAsLink ? rFileName : aEmpty,
118 0 : bAsLink ? rFilterName : aEmpty);
119 :
120 0 : if(pResult)
121 : {
122 : // we are done; mark the modified/new object
123 0 : pDrawView->MarkObj(pResult, pDrawView->GetSdrPageView());
124 0 : return;
125 0 : }
126 : }
127 : }
128 :
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 0 : MapMode aSourceMap = rGraphic.GetPrefMapMode();
133 0 : MapMode aDestMap( MAP_100TH_MM );
134 0 : if ( aSourceMap.GetMapUnit() == MAP_PIXEL && pDrawView )
135 : {
136 0 : Fraction aScaleX, aScaleY;
137 0 : pDrawView->CalcNormScale( aScaleX, aScaleY );
138 0 : aDestMap.SetScaleX(aScaleX);
139 0 : aDestMap.SetScaleY(aScaleY);
140 : }
141 : Size aLogicSize = pWindow->LogicToLogic(
142 0 : rGraphic.GetPrefSize(), &aSourceMap, &aDestMap );
143 :
144 : // Limit size
145 :
146 0 : SdrPageView* pPV = pView->GetSdrPageView();
147 0 : SdrPage* pPage = pPV->GetPage();
148 0 : Point aInsertPos = pViewSh->GetInsertPos();
149 :
150 0 : ScViewData* pData = pViewSh->GetViewData();
151 0 : if ( pData->GetDocument()->IsNegativePage( pData->GetTabNo() ) )
152 0 : aInsertPos.X() -= aLogicSize.Width(); // move position to left edge
153 :
154 0 : ScLimitSizeOnDrawPage( aLogicSize, aInsertPos, pPage->GetSize() );
155 :
156 0 : Rectangle aRect ( aInsertPos, aLogicSize );
157 :
158 0 : SdrGrafObj* pObj = new SdrGrafObj( rGraphic, aRect );
159 :
160 : // calling SetGraphicLink here doesn't work
161 :
162 : // Path is no longer used as name for the graphics object
163 :
164 0 : ScDrawLayer* pLayer = (ScDrawLayer*) pView->GetModel();
165 0 : OUString aName = pLayer->GetNewGraphicName(); // "Graphics"
166 0 : pObj->SetName(aName);
167 :
168 : // don't select if from (dispatch) API, to allow subsequent cell operations
169 0 : sal_uLong nInsOptions = bApi ? SDRINSERT_DONTMARK : 0;
170 0 : pView->InsertObjectAtView( pObj, *pPV, nInsOptions );
171 :
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 0 : if ( bAsLink )
176 0 : pObj->SetGraphicLink( rFileName, ""/*TODO?*/, rFilterName );
177 : }
178 :
179 0 : static void lcl_InsertMedia( const OUString& rMediaURL, bool bApi,
180 : ScTabViewShell* pViewSh, Window* pWindow, SdrView* pView,
181 : const Size& rPrefSize, bool const bLink )
182 : {
183 0 : SdrPageView* pPV = pView->GetSdrPageView();
184 0 : SdrPage* pPage = pPV->GetPage();
185 0 : ScViewData* pData = pViewSh->GetViewData();
186 0 : Point aInsertPos( pViewSh->GetInsertPos() );
187 0 : Size aSize;
188 :
189 0 : if( rPrefSize.Width() && rPrefSize.Height() )
190 : {
191 0 : if( pWindow )
192 0 : aSize = pWindow->PixelToLogic( rPrefSize, MAP_100TH_MM );
193 : else
194 0 : aSize = Application::GetDefaultDevice()->PixelToLogic( rPrefSize, MAP_100TH_MM );
195 : }
196 : else
197 0 : aSize = Size( 5000, 5000 );
198 :
199 0 : ScLimitSizeOnDrawPage( aSize, aInsertPos, pPage->GetSize() );
200 :
201 0 : if( pData->GetDocument()->IsNegativePage( pData->GetTabNo() ) )
202 0 : aInsertPos.X() -= aSize.Width();
203 :
204 0 : OUString realURL;
205 0 : if (bLink)
206 : {
207 0 : realURL = rMediaURL;
208 : }
209 : else
210 : {
211 : uno::Reference<frame::XModel> const xModel(
212 0 : pData->GetDocument()->GetDocumentShell()->GetModel());
213 0 : bool const bRet = ::avmedia::EmbedMedia(xModel, rMediaURL, realURL);
214 0 : if (!bRet) { return; }
215 : }
216 :
217 0 : SdrMediaObj* pObj = new SdrMediaObj( Rectangle( aInsertPos, aSize ) );
218 :
219 0 : pObj->SetModel(pData->GetDocument()->GetDrawLayer()); // set before setURL
220 0 : pObj->setURL( realURL, ""/*TODO?*/ );
221 0 : pView->InsertObjectAtView( pObj, *pPV, bApi ? SDRINSERT_DONTMARK : 0 );
222 : }
223 :
224 : /*************************************************************************
225 : |*
226 : |* FuInsertGraphic::Konstruktor
227 : |*
228 : \************************************************************************/
229 :
230 0 : FuInsertGraphic::FuInsertGraphic( ScTabViewShell* pViewSh,
231 : Window* pWin,
232 : ScDrawView* pViewP,
233 : SdrModel* pDoc,
234 : SfxRequest& rReq )
235 0 : : FuPoor(pViewSh, pWin, pViewP, pDoc, rReq)
236 : {
237 0 : const SfxItemSet* pReqArgs = rReq.GetArgs();
238 : const SfxPoolItem* pItem;
239 0 : if ( pReqArgs &&
240 0 : pReqArgs->GetItemState( SID_INSERT_GRAPHIC, true, &pItem ) == SFX_ITEM_SET )
241 : {
242 0 : OUString aFileName = ((const SfxStringItem*)pItem)->GetValue();
243 :
244 0 : OUString aFilterName;
245 0 : if ( pReqArgs->GetItemState( FN_PARAM_FILTER, true, &pItem ) == SFX_ITEM_SET )
246 0 : aFilterName = ((const SfxStringItem*)pItem)->GetValue();
247 :
248 0 : sal_Bool bAsLink = false;
249 0 : if ( pReqArgs->GetItemState( FN_PARAM_1, true, &pItem ) == SFX_ITEM_SET )
250 0 : bAsLink = ((const SfxBoolItem*)pItem)->GetValue();
251 :
252 0 : Graphic aGraphic;
253 0 : int nError = GraphicFilter::LoadGraphic( aFileName, aFilterName, aGraphic, &GraphicFilter::GetGraphicFilter() );
254 0 : if ( nError == GRFILTER_OK )
255 : {
256 0 : lcl_InsertGraphic( aGraphic, aFileName, aFilterName, bAsLink, sal_True, pViewSh, pWindow, pView );
257 0 : }
258 : }
259 : else
260 : {
261 0 : SvxOpenGraphicDialog aDlg(ScResId(STR_INSERTGRAPHIC));
262 :
263 0 : if( aDlg.Execute() == GRFILTER_OK )
264 : {
265 0 : Graphic aGraphic;
266 0 : int nError = aDlg.GetGraphic(aGraphic);
267 0 : if( nError == GRFILTER_OK )
268 : {
269 0 : OUString aFileName = aDlg.GetPath();
270 0 : OUString aFilterName = aDlg.GetCurrentFilter();
271 0 : sal_Bool bAsLink = aDlg.IsAsLink();
272 :
273 : // really store as link only?
274 0 : if( bAsLink && SvtMiscOptions().ShowLinkWarningDialog() )
275 : {
276 0 : SvxLinkWarningDialog aWarnDlg(pWin,aFileName);
277 0 : if( aWarnDlg.Execute() != RET_OK )
278 0 : bAsLink = false; // don't store as link
279 : }
280 :
281 0 : lcl_InsertGraphic( aGraphic, aFileName, aFilterName, bAsLink, false, pViewSh, pWindow, pView );
282 :
283 : // append items for recording
284 0 : rReq.AppendItem( SfxStringItem( SID_INSERT_GRAPHIC, aFileName ) );
285 0 : rReq.AppendItem( SfxStringItem( FN_PARAM_FILTER, aFilterName ) );
286 0 : rReq.AppendItem( SfxBoolItem( FN_PARAM_1, bAsLink ) );
287 0 : rReq.Done();
288 : }
289 : else
290 : {
291 : // error is handled in SvxOpenGraphicDialog::GetGraphic
292 0 : }
293 0 : }
294 : }
295 0 : }
296 :
297 : /*************************************************************************
298 : |*
299 : |* FuInsertGraphic::Destruktor
300 : |*
301 : \************************************************************************/
302 :
303 0 : FuInsertGraphic::~FuInsertGraphic()
304 : {
305 0 : }
306 :
307 : /*************************************************************************
308 : |*
309 : |* FuInsertGraphic::Function aktivieren
310 : |*
311 : \************************************************************************/
312 :
313 0 : void FuInsertGraphic::Activate()
314 : {
315 0 : FuPoor::Activate();
316 0 : }
317 :
318 : /*************************************************************************
319 : |*
320 : |* FuInsertGraphic::Function deaktivieren
321 : |*
322 : \************************************************************************/
323 :
324 0 : void FuInsertGraphic::Deactivate()
325 : {
326 0 : FuPoor::Deactivate();
327 0 : }
328 :
329 : /*************************************************************************
330 : |*
331 : |* FuInsertMedia::Konstruktor
332 : |*
333 : \************************************************************************/
334 :
335 0 : FuInsertMedia::FuInsertMedia( ScTabViewShell* pViewSh,
336 : Window* pWin,
337 : ScDrawView* pViewP,
338 : SdrModel* pDoc,
339 : SfxRequest& rReq ) :
340 0 : FuPoor(pViewSh, pWin, pViewP, pDoc, rReq)
341 : {
342 0 : OUString aURL;
343 0 : const SfxItemSet* pReqArgs = rReq.GetArgs();
344 0 : bool bAPI = false;
345 :
346 0 : if( pReqArgs )
347 : {
348 0 : const SfxStringItem* pStringItem = PTR_CAST( SfxStringItem, &pReqArgs->Get( rReq.GetSlot() ) );
349 :
350 0 : if( pStringItem )
351 : {
352 0 : aURL = pStringItem->GetValue();
353 0 : bAPI = aURL.getLength();
354 : }
355 : }
356 :
357 0 : bool bLink(true);
358 0 : if (bAPI ||
359 0 : ::avmedia::MediaWindow::executeMediaURLDialog(pWindow, aURL, & bLink))
360 : {
361 0 : Size aPrefSize;
362 :
363 0 : if( pWin )
364 0 : pWin->EnterWait();
365 :
366 0 : if( !::avmedia::MediaWindow::isMediaURL( aURL, ""/*TODO?*/, true, &aPrefSize ) )
367 : {
368 0 : if( pWin )
369 0 : pWin->LeaveWait();
370 :
371 0 : if( !bAPI )
372 0 : ::avmedia::MediaWindow::executeFormatErrorBox( pWindow );
373 : }
374 : else
375 : {
376 : lcl_InsertMedia( aURL, bAPI, pViewSh, pWindow, pView, aPrefSize,
377 0 : bLink );
378 :
379 0 : if( pWin )
380 0 : pWin->LeaveWait();
381 : }
382 0 : }
383 0 : }
384 :
385 : /*************************************************************************
386 : |*
387 : |* FuInsertMedia::Destruktor
388 : |*
389 : \************************************************************************/
390 :
391 0 : FuInsertMedia::~FuInsertMedia()
392 : {
393 0 : }
394 :
395 : /*************************************************************************
396 : |*
397 : |* FuInsertMedia::Function aktivieren
398 : |*
399 : \************************************************************************/
400 :
401 0 : void FuInsertMedia::Activate()
402 : {
403 0 : FuPoor::Activate();
404 0 : }
405 :
406 : /*************************************************************************
407 : |*
408 : |* FuInsertMedia::Function deaktivieren
409 : |*
410 : \************************************************************************/
411 :
412 0 : void FuInsertMedia::Deactivate()
413 : {
414 0 : FuPoor::Deactivate();
415 0 : }
416 :
417 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|