merge the formfield patch from ooo-build
[ooovba.git] / sc / source / ui / drawfunc / fuins1.cxx
blob6b2e3ebb0ed6941a107881674432ce6c6b821f6e
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
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"
57 #include "sc.hrc"
61 ////========================================================================
62 //// class ImportProgress
63 ////
64 //// Bemerkung:
65 //// Diese Klasse stellt lediglich den Handler fuer den ImportProgress des
66 //// Grafikfilters bereit.
67 ////========================================================================
69 //class ImportProgress
70 //{
71 //public:
72 // ImportProgress( GraphicFilter& rFilter );
73 // ~ImportProgress();
75 // DECL_LINK( Update, GraphicFilter* );
77 //private:
78 // ScProgress aProgress;
79 //};
81 ////------------------------------------------------------------------------
83 //ImportProgress::ImportProgress( GraphicFilter& rFilter )
84 // : aProgress( NULL, // SfxViewFrame*, NULL == alle Docs locken
85 // String( ScResId(STR_INSERTGRAPHIC) ),
86 // 100 )
87 //{
88 // rFilter.SetUpdatePercentHdl( LINK( this, ImportProgress, Update) );
89 //}
91 ////------------------------------------------------------------------------
93 //__EXPORT ImportProgress::~ImportProgress()
94 //{
95 // aProgress.SetState( 100 );
96 //}
98 ////------------------------------------------------------------------------
100 //IMPL_LINK( ImportProgress, Update, GraphicFilter*, pGraphicFilter )
102 // aProgress.SetState( pGraphicFilter->GetPercent() );
103 // return 0;
107 //------------------------------------------------------------------------
109 void SC_DLLPUBLIC ScLimitSizeOnDrawPage( Size& rSize, Point& rPos, const Size& rPage )
111 if ( !rPage.Width() || !rPage.Height() )
112 return;
114 Size aPageSize = rPage;
115 BOOL bNegative = aPageSize.Width() < 0;
116 if ( bNegative )
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();
128 if ( fX < fY )
130 rSize.Width() = aPageSize.Width();
131 rSize.Height() = (long) ( rSize.Height() * fX );
133 else
135 rSize.Height() = aPageSize.Height();
136 rSize.Width() = (long) ( rSize.Width() * fY );
139 if (!rSize.Width())
140 rSize.Width() = 1;
141 if (!rSize.Height())
142 rSize.Height() = 1;
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();
150 if ( bNegative )
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 );
177 // Limit size
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.
207 // See #i37444#.
208 if ( bAsLink )
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() );
222 Size aSize;
224 if( rPrefSize.Width() && rPrefSize.Height() )
226 if( pWindow )
227 aSize = pWindow->PixelToLogic( rPrefSize, MAP_100TH_MM );
228 else
229 aSize = Application::GetDefaultDevice()->PixelToLogic( rPrefSize, MAP_100TH_MM );
231 else
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 \************************************************************************/
251 #ifdef _MSC_VER
252 #pragma optimize("",off)
253 #endif
255 FuInsertGraphic::FuInsertGraphic( ScTabViewShell* pViewSh,
256 Window* pWin,
257 ScDrawView* pViewP,
258 SdrModel* pDoc,
259 SfxRequest& rReq )
260 : FuPoor(pViewSh, pWin, pViewP, pDoc, rReq)
262 const SfxItemSet* pReqArgs = rReq.GetArgs();
263 const SfxPoolItem* pItem;
264 if ( pReqArgs &&
265 pReqArgs->GetItemState( SID_INSERT_GRAPHIC, TRUE, &pItem ) == SFX_ITEM_SET )
267 String aFileName = ((const SfxStringItem*)pItem)->GetValue();
269 String aFilterName;
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();
277 Graphic aGraphic;
278 int nError = ::LoadGraphic( aFileName, aFilterName, aGraphic, ::GetGrfFilter() );
279 if ( nError == GRFILTER_OK )
281 lcl_InsertGraphic( aGraphic, aFileName, aFilterName, bAsLink, TRUE, pViewSh, pWindow, pView );
284 else
286 SvxOpenGraphicDialog aDlg(ScResId(STR_INSERTGRAPHIC));
288 if( aDlg.Execute() == GRFILTER_OK )
290 Graphic aGraphic;
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 ) );
312 rReq.Done();
314 else
316 // error is handled in SvxOpenGraphicDialog::GetGraphic
318 #if 0
319 USHORT nRes = 0;
320 switch ( nError )
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;
329 if ( nRes )
331 InfoBox aInfoBox( pWindow, String(ScResId(nRes)) );
332 aInfoBox.Execute();
334 else
336 ULONG nStreamError = GetGrfFilter()->GetLastError().nStreamError;
337 if( ERRCODE_NONE != nStreamError )
338 ErrorHandler::HandleError( nStreamError );
340 #endif
346 /*************************************************************************
348 |* FuInsertGraphic::Destruktor
350 \************************************************************************/
352 FuInsertGraphic::~FuInsertGraphic()
356 /*************************************************************************
358 |* FuInsertGraphic::Function aktivieren
360 \************************************************************************/
362 void FuInsertGraphic::Activate()
364 FuPoor::Activate();
367 /*************************************************************************
369 |* FuInsertGraphic::Function deaktivieren
371 \************************************************************************/
373 void FuInsertGraphic::Deactivate()
375 FuPoor::Deactivate();
378 /*************************************************************************
380 |* FuInsertMedia::Konstruktor
382 \************************************************************************/
384 FuInsertMedia::FuInsertMedia( ScTabViewShell* pViewSh,
385 Window* pWin,
386 ScDrawView* pViewP,
387 SdrModel* pDoc,
388 SfxRequest& rReq ) :
389 FuPoor(pViewSh, pWin, pViewP, pDoc, rReq)
391 ::rtl::OUString aURL;
392 const SfxItemSet* pReqArgs = rReq.GetArgs();
393 bool bAPI = false;
395 if( pReqArgs )
397 const SfxStringItem* pStringItem = PTR_CAST( SfxStringItem, &pReqArgs->Get( rReq.GetSlot() ) );
399 if( pStringItem )
401 aURL = pStringItem->GetValue();
402 bAPI = aURL.getLength();
406 if( bAPI || ::avmedia::MediaWindow::executeMediaURLDialog( pWindow, aURL ) )
408 Size aPrefSize;
410 if( pWin )
411 pWin->EnterWait();
413 if( !::avmedia::MediaWindow::isMediaURL( aURL, true, &aPrefSize ) )
415 if( pWin )
416 pWin->LeaveWait();
418 if( !bAPI )
419 ::avmedia::MediaWindow::executeFormatErrorBox( pWindow );
421 else
423 lcl_InsertMedia( aURL, bAPI, pViewSh, pWindow, pView, aPrefSize );
425 if( pWin )
426 pWin->LeaveWait();
431 /*************************************************************************
433 |* FuInsertMedia::Destruktor
435 \************************************************************************/
437 FuInsertMedia::~FuInsertMedia()
441 /*************************************************************************
443 |* FuInsertMedia::Function aktivieren
445 \************************************************************************/
447 void FuInsertMedia::Activate()
449 FuPoor::Activate();
452 /*************************************************************************
454 |* FuInsertMedia::Function deaktivieren
456 \************************************************************************/
458 void FuInsertMedia::Deactivate()
460 FuPoor::Deactivate();