xref: /AOO41X/main/sc/source/ui/drawfunc/fuins1.cxx (revision 54628ca40d27d15cc98fe861da7fff7e60c2f7d6)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_sc.hxx"
26 
27 #include <sfx2/opengrf.hxx>
28 #include <svx/svdograf.hxx>
29 #include <svx/svdomedia.hxx>
30 #include <svx/svdpage.hxx>
31 #include <svx/svdpagv.hxx>
32 #include <svx/svdview.hxx>
33 #include <svtools/filter.hxx>
34 #include <svl/stritem.hxx>
35 #include <vcl/msgbox.hxx>
36 #include <tools/urlobj.hxx>
37 #include <avmedia/mediawindow.hxx>
38 #include <vcl/svapp.hxx>
39 
40 #include "fuinsert.hxx"
41 #include "tabvwsh.hxx"
42 #include "drwlayer.hxx"
43 #include "drawview.hxx"
44 #include "document.hxx"
45 #include "scresid.hxx"
46 #include "progress.hxx"
47 #include "sc.hrc"
48 
49 
50 
51 ////========================================================================
52 ////    class ImportProgress
53 ////
54 ////  Bemerkung:
55 ////    Diese Klasse stellt lediglich den Handler fuer den ImportProgress des
56 ////    Grafikfilters bereit.
57 ////========================================================================
58 //
59 //class ImportProgress
60 //{
61 //public:
62 //      ImportProgress( GraphicFilter& rFilter );
63 //      ~ImportProgress();
64 //
65 //  DECL_LINK( Update, GraphicFilter* );
66 //
67 //private:
68 //  ScProgress aProgress;
69 //};
70 //
71 ////------------------------------------------------------------------------
72 //
73 //ImportProgress::ImportProgress( GraphicFilter& rFilter )
74 //  : aProgress( NULL, // SfxViewFrame*, NULL == alle Docs locken
75 //               String( ScResId(STR_INSERTGRAPHIC) ),
76 //               100 )
77 //{
78 //  rFilter.SetUpdatePercentHdl( LINK( this, ImportProgress, Update) );
79 //}
80 //
81 ////------------------------------------------------------------------------
82 //
83 //__EXPORT ImportProgress::~ImportProgress()
84 //{
85 //  aProgress.SetState( 100 );
86 //}
87 //
88 ////------------------------------------------------------------------------
89 //
90 //IMPL_LINK( ImportProgress, Update, GraphicFilter*, pGraphicFilter )
91 //{
92 //  aProgress.SetState( pGraphicFilter->GetPercent() );
93 //  return 0;
94 //}
95 
96 
97 //------------------------------------------------------------------------
98 
99 void SC_DLLPUBLIC ScLimitSizeOnDrawPage( Size& rSize, Point& rPos, const Size& rPage )
100 {
101     if ( !rPage.Width() || !rPage.Height() )
102         return;
103 
104     Size aPageSize = rPage;
105     sal_Bool bNegative = aPageSize.Width() < 0;
106     if ( bNegative )
107     {
108         //  make everything positive temporarily
109         aPageSize.Width() = -aPageSize.Width();
110         rPos.X() = -rPos.X() - rSize.Width();
111     }
112 
113     if ( rSize.Width() > aPageSize.Width() || rSize.Height() > aPageSize.Height() )
114     {
115         double fX = aPageSize.Width()  / (double) rSize.Width();
116         double fY = aPageSize.Height() / (double) rSize.Height();
117 
118         if ( fX < fY )
119         {
120             rSize.Width()  = aPageSize.Width();
121             rSize.Height() = (long) ( rSize.Height() * fX );
122         }
123         else
124         {
125             rSize.Height() = aPageSize.Height();
126             rSize.Width()  = (long) ( rSize.Width() * fY );
127         }
128 
129         if (!rSize.Width())
130             rSize.Width() = 1;
131         if (!rSize.Height())
132             rSize.Height() = 1;
133     }
134 
135     if ( rPos.X() + rSize.Width() > aPageSize.Width() )
136         rPos.X() = aPageSize.Width() - rSize.Width();
137     if ( rPos.Y() + rSize.Height() > aPageSize.Height() )
138         rPos.Y() = aPageSize.Height() - rSize.Height();
139 
140     if ( bNegative )
141         rPos.X() = -rPos.X() - rSize.Width();       // back to real position
142 }
143 
144 //------------------------------------------------------------------------
145 
146 void lcl_InsertGraphic( const Graphic& rGraphic,
147                         const String& rFileName, const String& rFilterName, sal_Bool bAsLink, sal_Bool bApi,
148                         ScTabViewShell* pViewSh, Window* pWindow, SdrView* pView )
149 {
150     //  #74778# set the size so the graphic has its original pixel size
151     //  at 100% view scale (as in SetMarkedOriginalSize),
152     //  instead of respecting the current view scale
153 
154     ScDrawView* pDrawView = pViewSh->GetScDrawView();
155     MapMode aSourceMap = rGraphic.GetPrefMapMode();
156     MapMode aDestMap( MAP_100TH_MM );
157     if ( aSourceMap.GetMapUnit() == MAP_PIXEL && pDrawView )
158     {
159         Fraction aScaleX, aScaleY;
160         pDrawView->CalcNormScale( aScaleX, aScaleY );
161         aDestMap.SetScaleX(aScaleX);
162         aDestMap.SetScaleY(aScaleY);
163     }
164     Size aLogicSize = pWindow->LogicToLogic(
165                             rGraphic.GetPrefSize(), &aSourceMap, &aDestMap );
166 
167     //  Limit size
168 
169     SdrPageView* pPV  = pView->GetSdrPageView();
170     SdrPage* pPage = pPV->GetPage();
171     Point aInsertPos = pViewSh->GetInsertPos();
172 
173     ScViewData* pData = pViewSh->GetViewData();
174     if ( pData->GetDocument()->IsNegativePage( pData->GetTabNo() ) )
175         aInsertPos.X() -= aLogicSize.Width();       // move position to left edge
176 
177     ScLimitSizeOnDrawPage( aLogicSize, aInsertPos, pPage->GetSize() );
178 
179     Rectangle aRect ( aInsertPos, aLogicSize );
180 
181     SdrGrafObj* pObj = new SdrGrafObj( rGraphic, aRect );
182 
183     // #118522# calling SetGraphicLink here doesn't work
184 
185     //  #49961# Path is no longer used as name for the graphics object
186 
187     ScDrawLayer* pLayer = (ScDrawLayer*) pView->GetModel();
188     String aName = pLayer->GetNewGraphicName();                 // "Grafik x"
189     pObj->SetName(aName);
190 
191     //  don't select if from (dispatch) API, to allow subsequent cell operations
192     sal_uLong nInsOptions = bApi ? SDRINSERT_DONTMARK : 0;
193     pView->InsertObjectAtView( pObj, *pPV, nInsOptions );
194 
195     // #118522# SetGraphicLink has to be used after inserting the object,
196     // otherwise an empty graphic is swapped in and the contact stuff crashes.
197     // See #i37444#.
198     if ( bAsLink )
199         pObj->SetGraphicLink( rFileName, rFilterName );
200 }
201 
202 //------------------------------------------------------------------------
203 
204 void lcl_InsertMedia( const ::rtl::OUString& rMediaURL, bool bApi,
205                       ScTabViewShell* pViewSh, Window* pWindow, SdrView* pView,
206                       const Size& rPrefSize )
207 {
208     SdrPageView*    pPV  = pView->GetSdrPageView();
209     SdrPage*        pPage = pPV->GetPage();
210     ScViewData*     pData = pViewSh->GetViewData();
211     Point           aInsertPos( pViewSh->GetInsertPos() );
212     Size            aSize;
213 
214     if( rPrefSize.Width() && rPrefSize.Height() )
215     {
216         if( pWindow )
217             aSize = pWindow->PixelToLogic( rPrefSize, MAP_100TH_MM );
218         else
219             aSize = Application::GetDefaultDevice()->PixelToLogic( rPrefSize, MAP_100TH_MM );
220     }
221     else
222         aSize = Size( 5000, 5000 );
223 
224     ScLimitSizeOnDrawPage( aSize, aInsertPos, pPage->GetSize() );
225 
226     if( pData->GetDocument()->IsNegativePage( pData->GetTabNo() ) )
227         aInsertPos.X() -= aSize.Width();
228 
229     SdrMediaObj* pObj = new SdrMediaObj( Rectangle( aInsertPos, aSize ) );
230 
231     pObj->setURL( rMediaURL );
232     pView->InsertObjectAtView( pObj, *pPV, bApi ? SDRINSERT_DONTMARK : 0 );
233 }
234 
235 /*************************************************************************
236 |*
237 |* FuInsertGraphic::Konstruktor
238 |*
239 \************************************************************************/
240 
241 #ifdef _MSC_VER
242 #pragma optimize("",off)
243 #endif
244 
245 FuInsertGraphic::FuInsertGraphic( ScTabViewShell*   pViewSh,
246                                   Window*           pWin,
247                                   ScDrawView*       pViewP,
248                                   SdrModel*         pDoc,
249                                   SfxRequest&       rReq )
250        : FuPoor(pViewSh, pWin, pViewP, pDoc, rReq)
251 {
252     const SfxItemSet* pReqArgs = rReq.GetArgs();
253     const SfxPoolItem* pItem;
254     if ( pReqArgs &&
255          pReqArgs->GetItemState( SID_INSERT_GRAPHIC, sal_True, &pItem ) == SFX_ITEM_SET )
256     {
257         String aFileName = ((const SfxStringItem*)pItem)->GetValue();
258 
259         String aFilterName;
260         if ( pReqArgs->GetItemState( FN_PARAM_FILTER, sal_True, &pItem ) == SFX_ITEM_SET )
261             aFilterName = ((const SfxStringItem*)pItem)->GetValue();
262 
263         sal_Bool bAsLink = sal_False;
264         if ( pReqArgs->GetItemState( FN_PARAM_1, sal_True, &pItem ) == SFX_ITEM_SET )
265             bAsLink = ((const SfxBoolItem*)pItem)->GetValue();
266 
267         Graphic aGraphic;
268         int nError = GraphicFilter::LoadGraphic( aFileName, aFilterName, aGraphic, GraphicFilter::GetGraphicFilter() );
269         if ( nError == GRFILTER_OK )
270         {
271             lcl_InsertGraphic( aGraphic, aFileName, aFilterName, bAsLink, sal_True, pViewSh, pWindow, pView );
272         }
273     }
274     else
275     {
276         SvxOpenGraphicDialog aDlg(ScResId(STR_INSERTGRAPHIC));
277 
278         if( aDlg.Execute() == GRFILTER_OK )
279         {
280             Graphic aGraphic;
281             int nError = aDlg.GetGraphic(aGraphic);
282             if( nError == GRFILTER_OK )
283             {
284                 String aFileName = aDlg.GetPath();
285                 String aFilterName = aDlg.GetCurrentFilter();
286                 sal_Bool bAsLink = aDlg.IsAsLink();
287 
288                 lcl_InsertGraphic( aGraphic, aFileName, aFilterName, bAsLink, sal_False, pViewSh, pWindow, pView );
289 
290                 //  append items for recording
291                 rReq.AppendItem( SfxStringItem( SID_INSERT_GRAPHIC, aFileName ) );
292                 rReq.AppendItem( SfxStringItem( FN_PARAM_FILTER, aFilterName ) );
293                 rReq.AppendItem( SfxBoolItem( FN_PARAM_1, bAsLink ) );
294                 rReq.Done();
295             }
296             else
297             {
298                 //  error is handled in SvxOpenGraphicDialog::GetGraphic
299 
300 #if 0
301                 sal_uInt16 nRes = 0;
302                 switch ( nError )
303                 {
304                     case GRFILTER_OPENERROR:    nRes = SCSTR_GRFILTER_OPENERROR;    break;
305                     case GRFILTER_IOERROR:      nRes = SCSTR_GRFILTER_IOERROR;      break;
306                     case GRFILTER_FORMATERROR:  nRes = SCSTR_GRFILTER_FORMATERROR;  break;
307                     case GRFILTER_VERSIONERROR: nRes = SCSTR_GRFILTER_VERSIONERROR; break;
308                     case GRFILTER_FILTERERROR:  nRes = SCSTR_GRFILTER_FILTERERROR;  break;
309                     case GRFILTER_TOOBIG:       nRes = SCSTR_GRFILTER_TOOBIG;       break;
310                 }
311                 if ( nRes )
312                 {
313                     InfoBox aInfoBox( pWindow, String(ScResId(nRes)) );
314                     aInfoBox.Execute();
315                 }
316                 else
317                 {
318                     sal_uLong nStreamError = GetGrfFilter()->GetLastError().nStreamError;
319                     if( ERRCODE_NONE != nStreamError )
320                         ErrorHandler::HandleError( nStreamError );
321                 }
322 #endif
323             }
324         }
325     }
326 }
327 
328 /*************************************************************************
329 |*
330 |* FuInsertGraphic::Destruktor
331 |*
332 \************************************************************************/
333 
334 FuInsertGraphic::~FuInsertGraphic()
335 {
336 }
337 
338 /*************************************************************************
339 |*
340 |* FuInsertGraphic::Function aktivieren
341 |*
342 \************************************************************************/
343 
344 void FuInsertGraphic::Activate()
345 {
346     FuPoor::Activate();
347 }
348 
349 /*************************************************************************
350 |*
351 |* FuInsertGraphic::Function deaktivieren
352 |*
353 \************************************************************************/
354 
355 void FuInsertGraphic::Deactivate()
356 {
357     FuPoor::Deactivate();
358 }
359 
360 /*************************************************************************
361 |*
362 |* FuInsertMedia::Konstruktor
363 |*
364 \************************************************************************/
365 
366 FuInsertMedia::FuInsertMedia( ScTabViewShell*   pViewSh,
367                               Window*           pWin,
368                               ScDrawView*       pViewP,
369                               SdrModel*         pDoc,
370                               SfxRequest&       rReq ) :
371     FuPoor(pViewSh, pWin, pViewP, pDoc, rReq)
372 {
373     ::rtl::OUString     aURL;
374     const SfxItemSet*   pReqArgs = rReq.GetArgs();
375     bool                bAPI = false;
376 
377     if( pReqArgs )
378     {
379         const SfxStringItem* pStringItem = PTR_CAST( SfxStringItem, &pReqArgs->Get( rReq.GetSlot() ) );
380 
381         if( pStringItem )
382         {
383             aURL = pStringItem->GetValue();
384             bAPI = aURL.getLength();
385         }
386     }
387 
388     if( bAPI || ::avmedia::MediaWindow::executeMediaURLDialog( pWindow, aURL ) )
389     {
390         Size aPrefSize;
391 
392         if( pWin )
393             pWin->EnterWait();
394 
395         if( !::avmedia::MediaWindow::isMediaURL( aURL, true, &aPrefSize ) )
396         {
397             if( pWin )
398                 pWin->LeaveWait();
399 
400             if( !bAPI )
401                 ::avmedia::MediaWindow::executeFormatErrorBox( pWindow );
402         }
403         else
404         {
405             lcl_InsertMedia( aURL, bAPI, pViewSh, pWindow, pView, aPrefSize );
406 
407             if( pWin )
408                 pWin->LeaveWait();
409         }
410     }
411 }
412 
413 /*************************************************************************
414 |*
415 |* FuInsertMedia::Destruktor
416 |*
417 \************************************************************************/
418 
419 FuInsertMedia::~FuInsertMedia()
420 {
421 }
422 
423 /*************************************************************************
424 |*
425 |* FuInsertMedia::Function aktivieren
426 |*
427 \************************************************************************/
428 
429 void FuInsertMedia::Activate()
430 {
431     FuPoor::Activate();
432 }
433 
434 /*************************************************************************
435 |*
436 |* FuInsertMedia::Function deaktivieren
437 |*
438 \************************************************************************/
439 
440 void FuInsertMedia::Deactivate()
441 {
442     FuPoor::Deactivate();
443 }
444