Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : :
30 : : #include <sfx2/opengrf.hxx>
31 : : #include <svx/svdograf.hxx>
32 : : #include <svx/svdomedia.hxx>
33 : : #include <svx/svdpage.hxx>
34 : : #include <svx/svdpagv.hxx>
35 : : #include <svx/svdview.hxx>
36 : : #include <svx/linkwarn.hxx>
37 : : #include <svtools/filter.hxx>
38 : : #include <svl/stritem.hxx>
39 : : #include <svtools/miscopt.hxx>
40 : : #include <vcl/msgbox.hxx>
41 : : #include <avmedia/mediawindow.hxx>
42 : : #include <vcl/svapp.hxx>
43 : :
44 : : #include "fuinsert.hxx"
45 : : #include "tabvwsh.hxx"
46 : : #include "drwlayer.hxx"
47 : : #include "drawview.hxx"
48 : : #include "document.hxx"
49 : : #include "scresid.hxx"
50 : : #include "progress.hxx"
51 : : #include "sc.hrc"
52 : :
53 : :
54 : : using namespace ::com::sun::star;
55 : :
56 : : //------------------------------------------------------------------------
57 : :
58 : 0 : void SC_DLLPUBLIC ScLimitSizeOnDrawPage( Size& rSize, Point& rPos, const Size& rPage )
59 : : {
60 [ # # ][ # # ]: 0 : if ( !rPage.Width() || !rPage.Height() )
[ # # ]
61 : 0 : return;
62 : :
63 : 0 : Size aPageSize = rPage;
64 : 0 : sal_Bool bNegative = aPageSize.Width() < 0;
65 [ # # ]: 0 : if ( bNegative )
66 : : {
67 : : // make everything positive temporarily
68 : 0 : aPageSize.Width() = -aPageSize.Width();
69 : 0 : rPos.X() = -rPos.X() - rSize.Width();
70 : : }
71 : :
72 [ # # ][ # # ]: 0 : if ( rSize.Width() > aPageSize.Width() || rSize.Height() > aPageSize.Height() )
[ # # ]
73 : : {
74 : 0 : double fX = aPageSize.Width() / (double) rSize.Width();
75 : 0 : double fY = aPageSize.Height() / (double) rSize.Height();
76 : :
77 [ # # ]: 0 : if ( fX < fY )
78 : : {
79 : 0 : rSize.Width() = aPageSize.Width();
80 : 0 : rSize.Height() = (long) ( rSize.Height() * fX );
81 : : }
82 : : else
83 : : {
84 : 0 : rSize.Height() = aPageSize.Height();
85 : 0 : rSize.Width() = (long) ( rSize.Width() * fY );
86 : : }
87 : :
88 [ # # ]: 0 : if (!rSize.Width())
89 : 0 : rSize.Width() = 1;
90 [ # # ]: 0 : if (!rSize.Height())
91 : 0 : rSize.Height() = 1;
92 : : }
93 : :
94 [ # # ]: 0 : if ( rPos.X() + rSize.Width() > aPageSize.Width() )
95 : 0 : rPos.X() = aPageSize.Width() - rSize.Width();
96 [ # # ]: 0 : if ( rPos.Y() + rSize.Height() > aPageSize.Height() )
97 : 0 : rPos.Y() = aPageSize.Height() - rSize.Height();
98 : :
99 [ # # ]: 0 : if ( bNegative )
100 : 0 : rPos.X() = -rPos.X() - rSize.Width(); // back to real position
101 : : }
102 : :
103 : : //------------------------------------------------------------------------
104 : :
105 : 0 : void lcl_InsertGraphic( const Graphic& rGraphic,
106 : : const String& rFileName, const String& rFilterName, sal_Bool bAsLink, sal_Bool bApi,
107 : : ScTabViewShell* pViewSh, Window* pWindow, SdrView* pView )
108 : : {
109 : : // set the size so the graphic has its original pixel size
110 : : // at 100% view scale (as in SetMarkedOriginalSize),
111 : : // instead of respecting the current view scale
112 : :
113 : 0 : ScDrawView* pDrawView = pViewSh->GetScDrawView();
114 [ # # ]: 0 : MapMode aSourceMap = rGraphic.GetPrefMapMode();
115 [ # # ]: 0 : MapMode aDestMap( MAP_100TH_MM );
116 [ # # ][ # # ]: 0 : if ( aSourceMap.GetMapUnit() == MAP_PIXEL && pDrawView )
[ # # ]
117 : : {
118 : 0 : Fraction aScaleX, aScaleY;
119 [ # # ]: 0 : pDrawView->CalcNormScale( aScaleX, aScaleY );
120 [ # # ]: 0 : aDestMap.SetScaleX(aScaleX);
121 [ # # ]: 0 : aDestMap.SetScaleY(aScaleY);
122 : : }
123 : : Size aLogicSize = pWindow->LogicToLogic(
124 [ # # ][ # # ]: 0 : rGraphic.GetPrefSize(), &aSourceMap, &aDestMap );
125 : :
126 : : // Limit size
127 : :
128 : 0 : SdrPageView* pPV = pView->GetSdrPageView();
129 : 0 : SdrPage* pPage = pPV->GetPage();
130 [ # # ]: 0 : Point aInsertPos = pViewSh->GetInsertPos();
131 : :
132 : 0 : ScViewData* pData = pViewSh->GetViewData();
133 [ # # ][ # # ]: 0 : if ( pData->GetDocument()->IsNegativePage( pData->GetTabNo() ) )
[ # # ]
134 : 0 : aInsertPos.X() -= aLogicSize.Width(); // move position to left edge
135 : :
136 [ # # ][ # # ]: 0 : ScLimitSizeOnDrawPage( aLogicSize, aInsertPos, pPage->GetSize() );
137 : :
138 [ # # ]: 0 : Rectangle aRect ( aInsertPos, aLogicSize );
139 : :
140 [ # # ][ # # ]: 0 : SdrGrafObj* pObj = new SdrGrafObj( rGraphic, aRect );
141 : :
142 : : // calling SetGraphicLink here doesn't work
143 : :
144 : : // Path is no longer used as name for the graphics object
145 : :
146 : 0 : ScDrawLayer* pLayer = (ScDrawLayer*) pView->GetModel();
147 [ # # ]: 0 : String aName = pLayer->GetNewGraphicName(); // "Grafik x"
148 [ # # ][ # # ]: 0 : pObj->SetName(aName);
149 : :
150 : : // don't select if from (dispatch) API, to allow subsequent cell operations
151 [ # # ]: 0 : sal_uLong nInsOptions = bApi ? SDRINSERT_DONTMARK : 0;
152 [ # # ]: 0 : pView->InsertObjectAtView( pObj, *pPV, nInsOptions );
153 : :
154 : : // SetGraphicLink has to be used after inserting the object,
155 : : // otherwise an empty graphic is swapped in and the contact stuff crashes.
156 : : // See #i37444#.
157 [ # # ]: 0 : if ( bAsLink )
158 [ # # ][ # # ]: 0 : pObj->SetGraphicLink( rFileName, rFilterName );
[ # # ][ # # ]
[ # # ]
159 : 0 : }
160 : :
161 : : //------------------------------------------------------------------------
162 : :
163 : 0 : void lcl_InsertMedia( const ::rtl::OUString& rMediaURL, bool bApi,
164 : : ScTabViewShell* pViewSh, Window* pWindow, SdrView* pView,
165 : : const Size& rPrefSize, bool const bLink )
166 : : {
167 : 0 : SdrPageView* pPV = pView->GetSdrPageView();
168 : 0 : SdrPage* pPage = pPV->GetPage();
169 : 0 : ScViewData* pData = pViewSh->GetViewData();
170 [ # # ]: 0 : Point aInsertPos( pViewSh->GetInsertPos() );
171 : 0 : Size aSize;
172 : :
173 [ # # ][ # # ]: 0 : if( rPrefSize.Width() && rPrefSize.Height() )
[ # # ]
174 : : {
175 [ # # ]: 0 : if( pWindow )
176 [ # # ][ # # ]: 0 : aSize = pWindow->PixelToLogic( rPrefSize, MAP_100TH_MM );
[ # # ]
177 : : else
178 [ # # ][ # # ]: 0 : aSize = Application::GetDefaultDevice()->PixelToLogic( rPrefSize, MAP_100TH_MM );
[ # # ][ # # ]
179 : : }
180 : : else
181 : 0 : aSize = Size( 5000, 5000 );
182 : :
183 [ # # ][ # # ]: 0 : ScLimitSizeOnDrawPage( aSize, aInsertPos, pPage->GetSize() );
184 : :
185 [ # # ][ # # ]: 0 : if( pData->GetDocument()->IsNegativePage( pData->GetTabNo() ) )
[ # # ]
186 : 0 : aInsertPos.X() -= aSize.Width();
187 : :
188 : 0 : ::rtl::OUString realURL;
189 [ # # ]: 0 : if (bLink)
190 : : {
191 : 0 : realURL = rMediaURL;
192 : : }
193 : : else
194 : : {
195 : : uno::Reference<frame::XModel> const xModel(
196 [ # # ][ # # ]: 0 : pData->GetDocument()->GetDocumentShell()->GetModel());
197 [ # # ]: 0 : bool const bRet = ::avmedia::EmbedMedia(xModel, rMediaURL, realURL);
198 [ # # ][ # # ]: 0 : if (!bRet) { return; }
199 : : }
200 : :
201 [ # # ][ # # ]: 0 : SdrMediaObj* pObj = new SdrMediaObj( Rectangle( aInsertPos, aSize ) );
[ # # ]
202 : :
203 [ # # ][ # # ]: 0 : pObj->SetModel(pData->GetDocument()->GetDrawLayer()); // set before setURL
[ # # ]
204 [ # # ]: 0 : pObj->setURL( realURL );
205 [ # # ][ # # ]: 0 : pView->InsertObjectAtView( pObj, *pPV, bApi ? SDRINSERT_DONTMARK : 0 );
[ # # ]
206 : : }
207 : :
208 : : /*************************************************************************
209 : : |*
210 : : |* FuInsertGraphic::Konstruktor
211 : : |*
212 : : \************************************************************************/
213 : :
214 : : #ifdef _MSC_VER
215 : : #pragma optimize("",off)
216 : : #endif
217 : :
218 : 0 : FuInsertGraphic::FuInsertGraphic( ScTabViewShell* pViewSh,
219 : : Window* pWin,
220 : : ScDrawView* pViewP,
221 : : SdrModel* pDoc,
222 : : SfxRequest& rReq )
223 : 0 : : FuPoor(pViewSh, pWin, pViewP, pDoc, rReq)
224 : : {
225 : 0 : const SfxItemSet* pReqArgs = rReq.GetArgs();
226 : : const SfxPoolItem* pItem;
227 [ # # ][ # # ]: 0 : if ( pReqArgs &&
[ # # ]
228 [ # # ]: 0 : pReqArgs->GetItemState( SID_INSERT_GRAPHIC, sal_True, &pItem ) == SFX_ITEM_SET )
229 : : {
230 [ # # ]: 0 : String aFileName = ((const SfxStringItem*)pItem)->GetValue();
231 : :
232 [ # # ]: 0 : String aFilterName;
233 [ # # ][ # # ]: 0 : if ( pReqArgs->GetItemState( FN_PARAM_FILTER, sal_True, &pItem ) == SFX_ITEM_SET )
234 [ # # ]: 0 : aFilterName = ((const SfxStringItem*)pItem)->GetValue();
235 : :
236 : 0 : sal_Bool bAsLink = false;
237 [ # # ][ # # ]: 0 : if ( pReqArgs->GetItemState( FN_PARAM_1, sal_True, &pItem ) == SFX_ITEM_SET )
238 : 0 : bAsLink = ((const SfxBoolItem*)pItem)->GetValue();
239 : :
240 [ # # ]: 0 : Graphic aGraphic;
241 [ # # ][ # # ]: 0 : int nError = GraphicFilter::LoadGraphic( aFileName, aFilterName, aGraphic, &GraphicFilter::GetGraphicFilter() );
242 [ # # ]: 0 : if ( nError == GRFILTER_OK )
243 : : {
244 [ # # ]: 0 : lcl_InsertGraphic( aGraphic, aFileName, aFilterName, bAsLink, sal_True, pViewSh, pWindow, pView );
245 [ # # ][ # # ]: 0 : }
[ # # ]
246 : : }
247 : : else
248 : : {
249 [ # # ][ # # ]: 0 : SvxOpenGraphicDialog aDlg(ScResId(STR_INSERTGRAPHIC));
[ # # ][ # # ]
250 : :
251 [ # # ][ # # ]: 0 : if( aDlg.Execute() == GRFILTER_OK )
252 : : {
253 [ # # ]: 0 : Graphic aGraphic;
254 [ # # ]: 0 : int nError = aDlg.GetGraphic(aGraphic);
255 [ # # ]: 0 : if( nError == GRFILTER_OK )
256 : : {
257 [ # # ]: 0 : String aFileName = aDlg.GetPath();
258 [ # # ]: 0 : String aFilterName = aDlg.GetCurrentFilter();
259 [ # # ]: 0 : sal_Bool bAsLink = aDlg.IsAsLink();
260 : :
261 : : // really store as link only?
262 [ # # ][ # # ]: 0 : if( bAsLink && SvtMiscOptions().ShowLinkWarningDialog() )
[ # # ][ # # ]
[ # # ][ # # ]
[ # # # # ]
263 : : {
264 [ # # ]: 0 : SvxLinkWarningDialog aWarnDlg(pWin,aFileName);
265 [ # # ][ # # ]: 0 : if( aWarnDlg.Execute() != RET_OK )
266 [ # # ]: 0 : bAsLink = false; // don't store as link
267 : : }
268 : :
269 [ # # ]: 0 : lcl_InsertGraphic( aGraphic, aFileName, aFilterName, bAsLink, false, pViewSh, pWindow, pView );
270 : :
271 : : // append items for recording
272 [ # # ][ # # ]: 0 : rReq.AppendItem( SfxStringItem( SID_INSERT_GRAPHIC, aFileName ) );
[ # # ]
273 [ # # ][ # # ]: 0 : rReq.AppendItem( SfxStringItem( FN_PARAM_FILTER, aFilterName ) );
[ # # ]
274 [ # # ][ # # ]: 0 : rReq.AppendItem( SfxBoolItem( FN_PARAM_1, bAsLink ) );
[ # # ]
275 [ # # ][ # # ]: 0 : rReq.Done();
[ # # ]
276 : : }
277 : : else
278 : : {
279 : : // error is handled in SvxOpenGraphicDialog::GetGraphic
280 [ # # ]: 0 : }
281 [ # # ]: 0 : }
282 : : }
283 : 0 : }
284 : :
285 : : /*************************************************************************
286 : : |*
287 : : |* FuInsertGraphic::Destruktor
288 : : |*
289 : : \************************************************************************/
290 : :
291 : 0 : FuInsertGraphic::~FuInsertGraphic()
292 : : {
293 [ # # ]: 0 : }
294 : :
295 : : /*************************************************************************
296 : : |*
297 : : |* FuInsertGraphic::Function aktivieren
298 : : |*
299 : : \************************************************************************/
300 : :
301 : 0 : void FuInsertGraphic::Activate()
302 : : {
303 : 0 : FuPoor::Activate();
304 : 0 : }
305 : :
306 : : /*************************************************************************
307 : : |*
308 : : |* FuInsertGraphic::Function deaktivieren
309 : : |*
310 : : \************************************************************************/
311 : :
312 : 0 : void FuInsertGraphic::Deactivate()
313 : : {
314 : 0 : FuPoor::Deactivate();
315 : 0 : }
316 : :
317 : : /*************************************************************************
318 : : |*
319 : : |* FuInsertMedia::Konstruktor
320 : : |*
321 : : \************************************************************************/
322 : :
323 : 0 : FuInsertMedia::FuInsertMedia( ScTabViewShell* pViewSh,
324 : : Window* pWin,
325 : : ScDrawView* pViewP,
326 : : SdrModel* pDoc,
327 : : SfxRequest& rReq ) :
328 : 0 : FuPoor(pViewSh, pWin, pViewP, pDoc, rReq)
329 : : {
330 : 0 : ::rtl::OUString aURL;
331 : 0 : const SfxItemSet* pReqArgs = rReq.GetArgs();
332 : 0 : bool bAPI = false;
333 : :
334 [ # # ]: 0 : if( pReqArgs )
335 : : {
336 [ # # ][ # # ]: 0 : const SfxStringItem* pStringItem = PTR_CAST( SfxStringItem, &pReqArgs->Get( rReq.GetSlot() ) );
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
337 : :
338 [ # # ]: 0 : if( pStringItem )
339 : : {
340 [ # # ]: 0 : aURL = pStringItem->GetValue();
341 : 0 : bAPI = aURL.getLength();
342 : : }
343 : : }
344 : :
345 : 0 : bool bLink(true);
346 [ # # ][ # # ]: 0 : if (bAPI ||
[ # # ]
347 [ # # ]: 0 : ::avmedia::MediaWindow::executeMediaURLDialog(pWindow, aURL, & bLink))
348 : : {
349 : 0 : Size aPrefSize;
350 : :
351 [ # # ]: 0 : if( pWin )
352 [ # # ]: 0 : pWin->EnterWait();
353 : :
354 [ # # ][ # # ]: 0 : if( !::avmedia::MediaWindow::isMediaURL( aURL, true, &aPrefSize ) )
355 : : {
356 [ # # ]: 0 : if( pWin )
357 [ # # ]: 0 : pWin->LeaveWait();
358 : :
359 [ # # ]: 0 : if( !bAPI )
360 [ # # ]: 0 : ::avmedia::MediaWindow::executeFormatErrorBox( pWindow );
361 : : }
362 : : else
363 : : {
364 : : lcl_InsertMedia( aURL, bAPI, pViewSh, pWindow, pView, aPrefSize,
365 [ # # ]: 0 : bLink );
366 : :
367 [ # # ]: 0 : if( pWin )
368 [ # # ]: 0 : pWin->LeaveWait();
369 : : }
370 : 0 : }
371 : 0 : }
372 : :
373 : : /*************************************************************************
374 : : |*
375 : : |* FuInsertMedia::Destruktor
376 : : |*
377 : : \************************************************************************/
378 : :
379 : 0 : FuInsertMedia::~FuInsertMedia()
380 : : {
381 [ # # ]: 0 : }
382 : :
383 : : /*************************************************************************
384 : : |*
385 : : |* FuInsertMedia::Function aktivieren
386 : : |*
387 : : \************************************************************************/
388 : :
389 : 0 : void FuInsertMedia::Activate()
390 : : {
391 : 0 : FuPoor::Activate();
392 : 0 : }
393 : :
394 : : /*************************************************************************
395 : : |*
396 : : |* FuInsertMedia::Function deaktivieren
397 : : |*
398 : : \************************************************************************/
399 : :
400 : 0 : void FuInsertMedia::Deactivate()
401 : : {
402 : 0 : FuPoor::Deactivate();
403 : 0 : }
404 : :
405 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|