1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: fuins1.cxx,v $
10 * $Revision: 1.12.128.1 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sc.hxx"
34 #include <svx/impgrf.hxx>
35 #include <svx/opengrf.hxx>
36 #include <svx/svdograf.hxx>
37 #include <svx/svdomedia.hxx>
38 #include <svx/svdpage.hxx>
39 #include <svx/svdpagv.hxx>
40 #include <svx/svdview.hxx>
41 #include <svx/linkwarn.hxx>
42 #include <svtools/filter.hxx>
43 #include <svtools/stritem.hxx>
44 #include <svtools/miscopt.hxx>
45 #include <vcl/msgbox.hxx>
46 #include <tools/urlobj.hxx>
47 #include <avmedia/mediawindow.hxx>
48 #include <vcl/svapp.hxx>
50 #include "fuinsert.hxx"
51 #include "tabvwsh.hxx"
52 #include "drwlayer.hxx"
53 #include "drawview.hxx"
54 #include "document.hxx"
55 #include "scresid.hxx"
56 #include "progress.hxx"
61 ////========================================================================
62 //// class ImportProgress
65 //// Diese Klasse stellt lediglich den Handler fuer den ImportProgress des
66 //// Grafikfilters bereit.
67 ////========================================================================
69 //class ImportProgress
72 // ImportProgress( GraphicFilter& rFilter );
75 // DECL_LINK( Update, GraphicFilter* );
78 // ScProgress aProgress;
81 ////------------------------------------------------------------------------
83 //ImportProgress::ImportProgress( GraphicFilter& rFilter )
84 // : aProgress( NULL, // SfxViewFrame*, NULL == alle Docs locken
85 // String( ScResId(STR_INSERTGRAPHIC) ),
88 // rFilter.SetUpdatePercentHdl( LINK( this, ImportProgress, Update) );
91 ////------------------------------------------------------------------------
93 //__EXPORT ImportProgress::~ImportProgress()
95 // aProgress.SetState( 100 );
98 ////------------------------------------------------------------------------
100 //IMPL_LINK( ImportProgress, Update, GraphicFilter*, pGraphicFilter )
102 // aProgress.SetState( pGraphicFilter->GetPercent() );
107 //------------------------------------------------------------------------
109 void SC_DLLPUBLIC
ScLimitSizeOnDrawPage( Size
& rSize
, Point
& rPos
, const Size
& rPage
)
111 if ( !rPage
.Width() || !rPage
.Height() )
114 Size aPageSize
= rPage
;
115 BOOL bNegative
= aPageSize
.Width() < 0;
118 // make everything positive temporarily
119 aPageSize
.Width() = -aPageSize
.Width();
120 rPos
.X() = -rPos
.X() - rSize
.Width();
123 if ( rSize
.Width() > aPageSize
.Width() || rSize
.Height() > aPageSize
.Height() )
125 double fX
= aPageSize
.Width() / (double) rSize
.Width();
126 double fY
= aPageSize
.Height() / (double) rSize
.Height();
130 rSize
.Width() = aPageSize
.Width();
131 rSize
.Height() = (long) ( rSize
.Height() * fX
);
135 rSize
.Height() = aPageSize
.Height();
136 rSize
.Width() = (long) ( rSize
.Width() * fY
);
145 if ( rPos
.X() + rSize
.Width() > aPageSize
.Width() )
146 rPos
.X() = aPageSize
.Width() - rSize
.Width();
147 if ( rPos
.Y() + rSize
.Height() > aPageSize
.Height() )
148 rPos
.Y() = aPageSize
.Height() - rSize
.Height();
151 rPos
.X() = -rPos
.X() - rSize
.Width(); // back to real position
154 //------------------------------------------------------------------------
156 void lcl_InsertGraphic( const Graphic
& rGraphic
,
157 const String
& rFileName
, const String
& rFilterName
, BOOL bAsLink
, BOOL bApi
,
158 ScTabViewShell
* pViewSh
, Window
* pWindow
, SdrView
* pView
)
160 // #74778# set the size so the graphic has its original pixel size
161 // at 100% view scale (as in SetMarkedOriginalSize),
162 // instead of respecting the current view scale
164 ScDrawView
* pDrawView
= pViewSh
->GetScDrawView();
165 MapMode aSourceMap
= rGraphic
.GetPrefMapMode();
166 MapMode
aDestMap( MAP_100TH_MM
);
167 if ( aSourceMap
.GetMapUnit() == MAP_PIXEL
&& pDrawView
)
169 Fraction aScaleX
, aScaleY
;
170 pDrawView
->CalcNormScale( aScaleX
, aScaleY
);
171 aDestMap
.SetScaleX(aScaleX
);
172 aDestMap
.SetScaleY(aScaleY
);
174 Size aLogicSize
= pWindow
->LogicToLogic(
175 rGraphic
.GetPrefSize(), &aSourceMap
, &aDestMap
);
179 SdrPageView
* pPV
= pView
->GetSdrPageView();
180 SdrPage
* pPage
= pPV
->GetPage();
181 Point aInsertPos
= pViewSh
->GetInsertPos();
183 ScViewData
* pData
= pViewSh
->GetViewData();
184 if ( pData
->GetDocument()->IsNegativePage( pData
->GetTabNo() ) )
185 aInsertPos
.X() -= aLogicSize
.Width(); // move position to left edge
187 ScLimitSizeOnDrawPage( aLogicSize
, aInsertPos
, pPage
->GetSize() );
189 Rectangle
aRect ( aInsertPos
, aLogicSize
);
191 SdrGrafObj
* pObj
= new SdrGrafObj( rGraphic
, aRect
);
193 // #118522# calling SetGraphicLink here doesn't work
195 // #49961# Path is no longer used as name for the graphics object
197 ScDrawLayer
* pLayer
= (ScDrawLayer
*) pView
->GetModel();
198 String aName
= pLayer
->GetNewGraphicName(); // "Grafik x"
199 pObj
->SetName(aName
);
201 // don't select if from (dispatch) API, to allow subsequent cell operations
202 ULONG nInsOptions
= bApi
? SDRINSERT_DONTMARK
: 0;
203 pView
->InsertObjectAtView( pObj
, *pPV
, nInsOptions
);
205 // #118522# SetGraphicLink has to be used after inserting the object,
206 // otherwise an empty graphic is swapped in and the contact stuff crashes.
209 pObj
->SetGraphicLink( rFileName
, rFilterName
);
212 //------------------------------------------------------------------------
214 void lcl_InsertMedia( const ::rtl::OUString
& rMediaURL
, bool bApi
,
215 ScTabViewShell
* pViewSh
, Window
* pWindow
, SdrView
* pView
,
216 const Size
& rPrefSize
)
218 SdrPageView
* pPV
= pView
->GetSdrPageView();
219 SdrPage
* pPage
= pPV
->GetPage();
220 ScViewData
* pData
= pViewSh
->GetViewData();
221 Point
aInsertPos( pViewSh
->GetInsertPos() );
224 if( rPrefSize
.Width() && rPrefSize
.Height() )
227 aSize
= pWindow
->PixelToLogic( rPrefSize
, MAP_100TH_MM
);
229 aSize
= Application::GetDefaultDevice()->PixelToLogic( rPrefSize
, MAP_100TH_MM
);
232 aSize
= Size( 5000, 5000 );
234 ScLimitSizeOnDrawPage( aSize
, aInsertPos
, pPage
->GetSize() );
236 if( pData
->GetDocument()->IsNegativePage( pData
->GetTabNo() ) )
237 aInsertPos
.X() -= aSize
.Width();
239 SdrMediaObj
* pObj
= new SdrMediaObj( Rectangle( aInsertPos
, aSize
) );
241 pObj
->setURL( rMediaURL
);
242 pView
->InsertObjectAtView( pObj
, *pPV
, bApi
? SDRINSERT_DONTMARK
: 0 );
245 /*************************************************************************
247 |* FuInsertGraphic::Konstruktor
249 \************************************************************************/
252 #pragma optimize("",off)
255 FuInsertGraphic::FuInsertGraphic( ScTabViewShell
* pViewSh
,
260 : FuPoor(pViewSh
, pWin
, pViewP
, pDoc
, rReq
)
262 const SfxItemSet
* pReqArgs
= rReq
.GetArgs();
263 const SfxPoolItem
* pItem
;
265 pReqArgs
->GetItemState( SID_INSERT_GRAPHIC
, TRUE
, &pItem
) == SFX_ITEM_SET
)
267 String aFileName
= ((const SfxStringItem
*)pItem
)->GetValue();
270 if ( pReqArgs
->GetItemState( FN_PARAM_FILTER
, TRUE
, &pItem
) == SFX_ITEM_SET
)
271 aFilterName
= ((const SfxStringItem
*)pItem
)->GetValue();
273 BOOL bAsLink
= FALSE
;
274 if ( pReqArgs
->GetItemState( FN_PARAM_1
, TRUE
, &pItem
) == SFX_ITEM_SET
)
275 bAsLink
= ((const SfxBoolItem
*)pItem
)->GetValue();
278 int nError
= ::LoadGraphic( aFileName
, aFilterName
, aGraphic
, ::GetGrfFilter() );
279 if ( nError
== GRFILTER_OK
)
281 lcl_InsertGraphic( aGraphic
, aFileName
, aFilterName
, bAsLink
, TRUE
, pViewSh
, pWindow
, pView
);
286 SvxOpenGraphicDialog
aDlg(ScResId(STR_INSERTGRAPHIC
));
288 if( aDlg
.Execute() == GRFILTER_OK
)
291 int nError
= aDlg
.GetGraphic(aGraphic
);
292 if( nError
== GRFILTER_OK
)
294 String aFileName
= aDlg
.GetPath();
295 String aFilterName
= aDlg
.GetCurrentFilter();
296 BOOL bAsLink
= aDlg
.IsAsLink();
298 // really store as link only?
299 if( bAsLink
&& SvtMiscOptions().ShowLinkWarningDialog() )
301 SvxLinkWarningDialog
aWarnDlg(pWin
,aFileName
);
302 if( aWarnDlg
.Execute() != RET_OK
)
303 bAsLink
= sal_False
; // don't store as link
306 lcl_InsertGraphic( aGraphic
, aFileName
, aFilterName
, bAsLink
, FALSE
, pViewSh
, pWindow
, pView
);
308 // append items for recording
309 rReq
.AppendItem( SfxStringItem( SID_INSERT_GRAPHIC
, aFileName
) );
310 rReq
.AppendItem( SfxStringItem( FN_PARAM_FILTER
, aFilterName
) );
311 rReq
.AppendItem( SfxBoolItem( FN_PARAM_1
, bAsLink
) );
316 // error is handled in SvxOpenGraphicDialog::GetGraphic
322 case GRFILTER_OPENERROR
: nRes
= SCSTR_GRFILTER_OPENERROR
; break;
323 case GRFILTER_IOERROR
: nRes
= SCSTR_GRFILTER_IOERROR
; break;
324 case GRFILTER_FORMATERROR
: nRes
= SCSTR_GRFILTER_FORMATERROR
; break;
325 case GRFILTER_VERSIONERROR
: nRes
= SCSTR_GRFILTER_VERSIONERROR
; break;
326 case GRFILTER_FILTERERROR
: nRes
= SCSTR_GRFILTER_FILTERERROR
; break;
327 case GRFILTER_TOOBIG
: nRes
= SCSTR_GRFILTER_TOOBIG
; break;
331 InfoBox
aInfoBox( pWindow
, String(ScResId(nRes
)) );
336 ULONG nStreamError
= GetGrfFilter()->GetLastError().nStreamError
;
337 if( ERRCODE_NONE
!= nStreamError
)
338 ErrorHandler::HandleError( nStreamError
);
346 /*************************************************************************
348 |* FuInsertGraphic::Destruktor
350 \************************************************************************/
352 FuInsertGraphic::~FuInsertGraphic()
356 /*************************************************************************
358 |* FuInsertGraphic::Function aktivieren
360 \************************************************************************/
362 void FuInsertGraphic::Activate()
367 /*************************************************************************
369 |* FuInsertGraphic::Function deaktivieren
371 \************************************************************************/
373 void FuInsertGraphic::Deactivate()
375 FuPoor::Deactivate();
378 /*************************************************************************
380 |* FuInsertMedia::Konstruktor
382 \************************************************************************/
384 FuInsertMedia::FuInsertMedia( ScTabViewShell
* pViewSh
,
389 FuPoor(pViewSh
, pWin
, pViewP
, pDoc
, rReq
)
391 ::rtl::OUString aURL
;
392 const SfxItemSet
* pReqArgs
= rReq
.GetArgs();
397 const SfxStringItem
* pStringItem
= PTR_CAST( SfxStringItem
, &pReqArgs
->Get( rReq
.GetSlot() ) );
401 aURL
= pStringItem
->GetValue();
402 bAPI
= aURL
.getLength();
406 if( bAPI
|| ::avmedia::MediaWindow::executeMediaURLDialog( pWindow
, aURL
) )
413 if( !::avmedia::MediaWindow::isMediaURL( aURL
, true, &aPrefSize
) )
419 ::avmedia::MediaWindow::executeFormatErrorBox( pWindow
);
423 lcl_InsertMedia( aURL
, bAPI
, pViewSh
, pWindow
, pView
, aPrefSize
);
431 /*************************************************************************
433 |* FuInsertMedia::Destruktor
435 \************************************************************************/
437 FuInsertMedia::~FuInsertMedia()
441 /*************************************************************************
443 |* FuInsertMedia::Function aktivieren
445 \************************************************************************/
447 void FuInsertMedia::Activate()
452 /*************************************************************************
454 |* FuInsertMedia::Function deaktivieren
456 \************************************************************************/
458 void FuInsertMedia::Deactivate()
460 FuPoor::Deactivate();