xref: /trunk/main/sw/source/ui/docvw/romenu.cxx (revision 6bcc9fe0e9ad8e3adcad10b35c622961480f57f3)
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_sw.hxx"
26 #include <hintids.hxx>
27 
28 
29 #include <tools/urlobj.hxx>
30 #include <vcl/graph.hxx>
31 #include <vcl/msgbox.hxx>
32 #include <sot/formats.hxx>
33 #include <svl/eitem.hxx>
34 #include <svl/stritem.hxx>
35 #include <unotools/pathoptions.hxx>
36 #include <svtools/filter.hxx>
37 #include <svtools/imap.hxx>
38 #include <svtools/inetimg.hxx>
39 #include <svtools/transfer.hxx>
40 #include <sfx2/docfile.hxx>
41 #include <sfx2/dispatch.hxx>
42 #include <svx/xoutbmp.hxx>
43 #include <svx/gallery.hxx>
44 #include <editeng/brshitem.hxx>
45 
46 
47 #include <swunodef.hxx>
48 #include <frmatr.hxx>
49 #include <fmturl.hxx>
50 #include <fmtinfmt.hxx>
51 #include <docsh.hxx>
52 #include <view.hxx>
53 #include <wrtsh.hxx>
54 #include <viewopt.hxx>
55 #include <swmodule.hxx>
56 #include <romenu.hxx>
57 #include <pagedesc.hxx>
58 #include <modcfg.hxx>
59 
60 #include <cmdid.h>
61 #include <helpid.h>
62 #include <docvw.hrc>
63 #include <docvw.hrc>
64 #include <com/sun/star/ui/dialogs/XFilePicker.hpp>
65 #include <com/sun/star/ui/dialogs/XFilterManager.hpp>
66 #include <com/sun/star/ui/dialogs/TemplateDescription.hpp>
67 #include <sfx2/filedlghelper.hxx>
68 
69 using namespace ::com::sun::star::lang;
70 using namespace ::com::sun::star::uno;
71 using namespace ::com::sun::star;
72 using namespace ::com::sun::star::ui::dialogs;
73 using namespace ::sfx2;
74 
75 SwReadOnlyPopup::~SwReadOnlyPopup()
76 {
77     String *pDel = (String*)aThemeList.First();
78     while ( pDel )
79     {
80         delete pDel;
81         pDel = (String*)aThemeList.Next();
82     }
83     delete pImageMap;
84     delete pTargetURL;
85 }
86 
87 
88 
89 void SwReadOnlyPopup::Check( sal_uInt16 nMID, sal_uInt16 nSID, SfxDispatcher &rDis )
90 {
91     SfxPoolItem *_pItem = 0;
92     SfxItemState eState = rDis.GetBindings()->QueryState( nSID, _pItem );
93     if (eState >= SFX_ITEM_AVAILABLE)
94     {
95         EnableItem( nMID, sal_True );
96         if (_pItem)
97         {
98             CheckItem ( nMID, !_pItem->ISA(SfxVoidItem) &&
99                             _pItem->ISA(SfxBoolItem) &&
100                             ((SfxBoolItem*)_pItem)->GetValue());
101             //remove full screen entry when not in full screen mode
102             if(SID_WIN_FULLSCREEN == nSID && !IsItemChecked(SID_WIN_FULLSCREEN) )
103                 EnableItem(nMID, sal_False);
104         }
105     }
106     else
107         EnableItem( nMID, sal_False );
108 
109     delete _pItem;
110 }
111 
112 
113 SwReadOnlyPopup::SwReadOnlyPopup( const Point &rDPos, SwView &rV ) :
114     PopupMenu( SW_RES(MN_READONLY_POPUP) ),
115     rView  ( rV ),
116     rDocPos( rDPos ),
117     pImageMap( 0 ),
118     pTargetURL( 0 )
119 {
120     bGrfToGalleryAsLnk = SW_MOD()->GetModuleConfig()->IsGrfToGalleryAsLnk();
121     SwWrtShell &rSh = rView.GetWrtShell();
122     rSh.IsURLGrfAtPos( rDocPos, &sURL, &sTargetFrameName, &sDescription );
123     if ( !sURL.Len() )
124     {
125         SwContentAtPos aCntntAtPos( SwContentAtPos::SW_INETATTR );
126         if( rSh.GetContentAtPos( rDocPos, aCntntAtPos, sal_False))
127         {
128             SwFmtINetFmt &rIItem = *(SwFmtINetFmt*)aCntntAtPos.aFnd.pAttr;
129             sURL = rIItem.GetValue();
130             sTargetFrameName = rIItem.GetTargetFrame();
131             sDescription = aCntntAtPos.sStr;
132         }
133     }
134 
135     sal_Bool bLink = sal_False;
136     const Graphic *pGrf;
137     if ( 0 == (pGrf = rSh.GetGrfAtPos( rDocPos, sGrfName, bLink )) )
138     {
139         EnableItem( MN_READONLY_SAVEGRAPHIC, sal_False );
140         EnableItem( MN_READONLY_COPYGRAPHIC, sal_False );
141     }
142     else
143     {
144         aGraphic = *pGrf;
145         const SwFrmFmt* pGrfFmt = rSh.GetFmtFromObj( rDocPos );
146         const SfxPoolItem* pURLItem;
147         if( pGrfFmt && SFX_ITEM_SET == pGrfFmt->GetItemState(
148             RES_URL, sal_True, &pURLItem ))
149         {
150             const SwFmtURL& rURL = *(SwFmtURL*)pURLItem;
151             if( rURL.GetMap() )
152                 pImageMap = new ImageMap( *rURL.GetMap() );
153             else if( rURL.GetURL().Len() )
154                 pTargetURL = new INetImage( bLink ? sGrfName : aEmptyStr,
155                                             rURL.GetURL(),
156                                             rURL.GetTargetFrameName(),
157                                             aEmptyStr, Size() );
158         }
159     }
160 
161     sal_Bool bEnableGraphicToGallery;
162     if ( sal_True == (bEnableGraphicToGallery = bLink) )
163     {
164         GalleryExplorer::FillThemeList( aThemeList );
165         if ( aThemeList.Count() )
166         {
167             PopupMenu *pMenu = GetPopupMenu(MN_READONLY_GRAPHICTOGALLERY);
168             pMenu->CheckItem( MN_READONLY_TOGALLERYLINK,  bGrfToGalleryAsLnk );
169             pMenu->CheckItem( MN_READONLY_TOGALLERYCOPY, !bGrfToGalleryAsLnk );
170             for ( sal_uInt16 i=0; i < aThemeList.Count(); ++i )
171                 pMenu->InsertItem( MN_READONLY_GRAPHICTOGALLERY+i + 3,
172                                    *(String*)aThemeList.GetObject( i ) );
173         }
174         else
175             bEnableGraphicToGallery = sal_False;
176     }
177     EnableItem( MN_READONLY_GRAPHICTOGALLERY, bEnableGraphicToGallery );
178 
179     SfxViewFrame * pVFrame = rV.GetViewFrame();
180     SfxDispatcher &rDis = *pVFrame->GetDispatcher();
181     const SwPageDesc &rDesc = rSh.GetPageDesc( rSh.GetCurPageDesc() );
182     pItem = &rDesc.GetMaster().GetBackground();
183     sal_Bool bEnableBackGallery = sal_False,
184          bEnableBack = sal_False;
185 
186     if ( GPOS_NONE != pItem->GetGraphicPos() )
187     {
188         bEnableBack = sal_True;
189         if ( pItem->GetGraphicLink() )
190         {
191             if ( !aThemeList.Count() )
192                 GalleryExplorer::FillThemeList( aThemeList );
193             if ( aThemeList.Count() )
194             {
195                 PopupMenu *pMenu = GetPopupMenu(MN_READONLY_BACKGROUNDTOGALLERY);
196                 pMenu->CheckItem( MN_READONLY_TOGALLERYLINK,  bGrfToGalleryAsLnk );
197                 pMenu->CheckItem( MN_READONLY_TOGALLERYCOPY, !bGrfToGalleryAsLnk );
198                 bEnableBackGallery = sal_True;
199                 for ( sal_uInt16 i=0; i < aThemeList.Count(); ++i )
200                     pMenu->InsertItem( MN_READONLY_BACKGROUNDTOGALLERY+i + 3,
201                                        *(String*)aThemeList.GetObject( i ) );
202             }
203         }
204     }
205     EnableItem( MN_READONLY_SAVEBACKGROUND, bEnableBack );
206     EnableItem( MN_READONLY_BACKGROUNDTOGALLERY, bEnableBackGallery );
207 
208     if ( !rSh.GetViewOptions()->IsGraphic() )
209         CheckItem( MN_READONLY_GRAPHICOFF );
210     else
211         EnableItem( MN_READONLY_LOADGRAPHIC, sal_False );
212 
213     sal_Bool bReloadFrame = 0 != rSh.GetView().GetViewFrame()->GetFrame().GetParentFrame();
214     EnableItem( MN_READONLY_RELOAD_FRAME,
215             bReloadFrame );
216     EnableItem( MN_READONLY_RELOAD, !bReloadFrame);
217 
218     Check( MN_READONLY_EDITDOC,         SID_EDITDOC,        rDis );
219     Check( MN_READONLY_SELECTION_MODE,  FN_READONLY_SELECTION_MODE,    rDis );
220     Check( MN_READONLY_SOURCEVIEW,      SID_SOURCEVIEW,     rDis );
221     Check( MN_READONLY_BROWSE_BACKWARD, SID_BROWSE_BACKWARD,rDis );
222     Check( MN_READONLY_BROWSE_FORWARD,  SID_BROWSE_FORWARD, rDis );
223 #ifdef WNT
224     Check( MN_READONLY_PLUGINOFF,       SID_PLUGINS_ACTIVE, rDis );
225 #endif
226     Check( MN_READONLY_OPENURL,         SID_OPENDOC,        rDis );
227     Check( MN_READONLY_OPENURLNEW,      SID_OPENDOC,        rDis );
228 
229     SfxPoolItem* pState = NULL;
230 
231     SfxItemState eState = pVFrame->GetBindings().QueryState( SID_COPY, pState );
232     Check( MN_READONLY_COPY,            SID_COPY,           rDis );
233     if(eState < SFX_ITEM_AVAILABLE)
234         EnableItem( MN_READONLY_COPY, sal_False );
235 
236     eState = pVFrame->GetBindings().QueryState( SID_EDITDOC, pState );
237     if (
238         eState < SFX_ITEM_DEFAULT ||
239         (rSh.IsGlobalDoc() && rView.GetDocShell()->IsReadOnlyUI())
240        )
241     {
242         EnableItem( MN_READONLY_EDITDOC, sal_False );
243     }
244     delete pState;
245 
246     if ( !sURL.Len() )
247     {
248         EnableItem( MN_READONLY_OPENURL, sal_False );
249         EnableItem( MN_READONLY_OPENURLNEW, sal_False );
250         EnableItem( MN_READONLY_COPYLINK, sal_False );
251     }
252     Check( SID_WIN_FULLSCREEN,         SID_WIN_FULLSCREEN,        rDis );
253 
254     RemoveDisabledEntries( sal_True, sal_True );
255 }
256 
257 void SwReadOnlyPopup::Execute( Window* pWin, const Point &rPixPos )
258 {
259     sal_uInt16 nId     = PopupMenu::Execute(
260     pWin,
261     rPixPos );
262     Execute(pWin, nId);
263 }
264 
265 /*-- 17.03.2004 13:06:18---------------------------------------------------
266     execute the resulting ID only - necessary to support XContextMenuInterception
267   -----------------------------------------------------------------------*/
268 void SwReadOnlyPopup::Execute( Window* pWin, sal_uInt16 nId )
269 {
270     SwWrtShell &rSh = rView.GetWrtShell();
271     SfxDispatcher &rDis = *rView.GetViewFrame()->GetDispatcher();
272     if ( nId >= MN_READONLY_GRAPHICTOGALLERY )
273     {
274         String sTmp;
275         sal_uInt16 nSaveId;
276         if ( nId >= MN_READONLY_BACKGROUNDTOGALLERY )
277         {
278             nId -= MN_READONLY_BACKGROUNDTOGALLERY+3;
279             nSaveId = MN_READONLY_SAVEBACKGROUND;
280             sTmp = *pItem->GetGraphicLink();
281         }
282         else
283         {
284             nId -= MN_READONLY_GRAPHICTOGALLERY+3;
285             nSaveId = MN_READONLY_SAVEGRAPHIC;
286             sTmp = sGrfName;
287         }
288         if ( !bGrfToGalleryAsLnk )
289             sTmp = SaveGraphic( nSaveId );
290 
291         if ( sTmp.Len() )
292         {
293             String sThemeName( *(String*)aThemeList.GetObject( nId ));
294             GalleryExplorer::InsertURL( sThemeName, sTmp );
295         }
296         return;
297     }
298 
299     TransferDataContainer* pClipCntnr = 0;
300 
301     sal_uInt16 nExecId = USHRT_MAX;
302     sal_uInt16 nFilter = USHRT_MAX;
303     switch( nId )
304     {
305         case SID_WIN_FULLSCREEN :           nExecId = SID_WIN_FULLSCREEN; break;
306         case MN_READONLY_OPENURL:           nFilter = URLLOAD_NOFILTER;   break;
307         case MN_READONLY_OPENURLNEW:        nFilter = URLLOAD_NEWVIEW;    break;
308         case MN_READONLY_COPY:              nExecId = SID_COPY;           break;
309 
310         case MN_READONLY_EDITDOC:           nExecId = SID_EDITDOC;        break;
311         case MN_READONLY_SELECTION_MODE:    nExecId = FN_READONLY_SELECTION_MODE; break;
312         case MN_READONLY_RELOAD:
313         case MN_READONLY_RELOAD_FRAME:
314             rSh.GetView().GetViewFrame()->GetDispatcher()->Execute(SID_RELOAD);
315         break;
316 
317         case MN_READONLY_BROWSE_BACKWARD:   nExecId = SID_BROWSE_BACKWARD;break;
318         case MN_READONLY_BROWSE_FORWARD:    nExecId = SID_BROWSE_FORWARD; break;
319         case MN_READONLY_SOURCEVIEW:        nExecId = SID_SOURCEVIEW;     break;
320         case MN_READONLY_SAVEGRAPHIC:
321         case MN_READONLY_SAVEBACKGROUND:
322             {
323                 SaveGraphic( nId );
324                 break;
325             }
326         case MN_READONLY_COPYLINK:
327             pClipCntnr = new TransferDataContainer;
328             pClipCntnr->CopyString( sURL );
329             break;
330 
331         case MN_READONLY_COPYGRAPHIC:
332             pClipCntnr = new TransferDataContainer;
333             pClipCntnr->CopyGraphic( aGraphic );
334 
335             if( pImageMap )
336                 pClipCntnr->CopyImageMap( *pImageMap );
337             if( pTargetURL )
338                 pClipCntnr->CopyINetImage( *pTargetURL );
339             break;
340 
341         case MN_READONLY_LOADGRAPHIC:
342             {
343                 sal_Bool bModified = rSh.IsModified();
344                 SwViewOption aOpt( *rSh.GetViewOptions() );
345                 aOpt.SetGraphic( sal_True );
346                 rSh.ApplyViewOptions( aOpt );
347                 if(!bModified)
348                     rSh.ResetModified();
349                 break;
350             }
351         case MN_READONLY_GRAPHICOFF:        nExecId = FN_VIEW_GRAPHIC;    break;
352 #ifdef WNT
353         case MN_READONLY_PLUGINOFF:         nExecId = SID_PLUGINS_ACTIVE; break;
354 #endif
355         case MN_READONLY_TOGALLERYLINK:
356             SW_MOD()->GetModuleConfig()->SetGrfToGalleryAsLnk( sal_True );
357             break;
358         case MN_READONLY_TOGALLERYCOPY:
359             SW_MOD()->GetModuleConfig()->SetGrfToGalleryAsLnk( sal_False );
360             break;
361 
362         default: //forward the id to the SfxBindings
363             nExecId = nId;
364     }
365     if( USHRT_MAX != nExecId )
366         rDis.GetBindings()->Execute( nExecId );
367     if( USHRT_MAX != nFilter )
368         ::LoadURL( sURL, &rSh, nFilter, &sTargetFrameName);
369 
370     if( pClipCntnr )
371     {
372         STAR_REFERENCE( datatransfer::XTransferable ) xRef( pClipCntnr );
373         if( pClipCntnr->HasAnyData() )
374             pClipCntnr->CopyToClipboard( pWin );
375     }
376 }
377 static void lcl_GetPreferedExtension( String &rExt, const Graphic &rGrf )
378 {
379     // dann ggfs. ueber die native-Info der Grafik den "besten"
380     // Filter vorschlagen
381     const sal_Char* pExt = "png";
382     switch( const_cast<Graphic&>(rGrf).GetLink().GetType() )
383     {
384         case GFX_LINK_TYPE_NATIVE_GIF:      pExt = "gif"; break;
385 
386         // #15508# added BMP type for better exports (writer export graphic - checked, works)
387         case GFX_LINK_TYPE_NATIVE_BMP:      pExt = "bmp"; break;
388 
389         case GFX_LINK_TYPE_NATIVE_TIF:      pExt = "tif"; break;
390         case GFX_LINK_TYPE_NATIVE_WMF:      pExt = "wmf"; break;
391         case GFX_LINK_TYPE_NATIVE_MET:      pExt = "met"; break;
392         case GFX_LINK_TYPE_NATIVE_PCT:      pExt = "pct"; break;
393         case GFX_LINK_TYPE_NATIVE_JPG:      pExt = "jpg"; break;
394         default:; //prevent warning
395     }
396     rExt.AssignAscii( pExt );
397 }
398 
399 
400 String SwReadOnlyPopup::SaveGraphic( sal_uInt16 nId )
401 {
402 
403     //Namen der Grafik herausfischen.
404     String aName;
405     if ( MN_READONLY_SAVEBACKGROUND == nId )
406     {
407         if ( pItem->GetGraphicLink() )
408             sGrfName = *pItem->GetGraphicLink();
409         ((SvxBrushItem*)pItem)->SetDoneLink( Link() );
410         const Graphic *pGrf = pItem->GetGraphic();
411         if ( pGrf )
412         {
413             aGraphic = *pGrf;
414             if ( pItem->GetGraphicLink() )
415                 sGrfName = *pItem->GetGraphicLink();
416         }
417         else
418             return aEmptyStr;
419     }
420     return ExportGraphic( aGraphic, sGrfName );
421 }
422 
423 String ExportGraphic( const Graphic &rGraphic, const String &rGrfName )
424 {
425     SvtPathOptions aPathOpt;
426     String sGrfPath( aPathOpt.GetGraphicPath() );
427 
428     FileDialogHelper aDlgHelper( TemplateDescription::FILESAVE_AUTOEXTENSION, 0 );
429     Reference < XFilePicker > xFP = aDlgHelper.GetFilePicker();
430 
431 //    aExpDlg.SetHelpId(HID_FILEDLG_ROMENU);
432     INetURLObject aPath;
433     aPath.SetSmartURL( sGrfPath );
434 
435     //Namen der Grafik herausfischen.
436     String aName = rGrfName;
437 
438     aDlgHelper.SetTitle( SW_RESSTR(STR_EXPORT_GRAFIK_TITLE));
439     aDlgHelper.SetDisplayDirectory( aPath.GetMainURL(INetURLObject::DECODE_TO_IURI) );
440     INetURLObject aURL;
441     aURL.SetSmartURL( aName );
442     aDlgHelper.SetFileName( aURL.GetName() );
443 
444     GraphicFilter& rGF = *GraphicFilter::GetGraphicFilter();
445     const sal_uInt16 nCount = rGF.GetExportFormatCount();
446 
447     String aExt( aURL.GetExtension() );
448     if( !aExt.Len() )
449         lcl_GetPreferedExtension( aExt, rGraphic );
450 
451     aExt.ToLowerAscii();
452     sal_uInt16 nDfltFilter = USHRT_MAX;
453 
454     Reference<XFilterManager> xFltMgr(xFP, UNO_QUERY);
455 
456     for ( sal_uInt16 i = 0; i < nCount; i++ )
457     {
458         xFltMgr->appendFilter( rGF.GetExportFormatName( i ), rGF.GetExportWildcard( i ) );
459         if ( COMPARE_EQUAL == aExt.CompareIgnoreCaseToAscii(rGF.GetExportFormatShortName( i ).ToLowerAscii() ))
460             nDfltFilter = i;
461     }
462     if ( USHRT_MAX == nDfltFilter )
463     {
464         //"falsche" Extension?
465         lcl_GetPreferedExtension( aExt, rGraphic );
466         for ( sal_uInt16 i = 0; i < nCount; ++i )
467             if ( aExt == rGF.GetExportFormatShortName( i ).ToLowerAscii() )
468             {
469                 nDfltFilter =  i;
470                 break;
471             }
472     }
473 
474     if( USHRT_MAX != nDfltFilter )
475     {
476         xFltMgr->setCurrentFilter( rGF.GetExportFormatName( nDfltFilter ) ) ;
477 
478         if( aDlgHelper.Execute() == ERRCODE_NONE )
479         {
480             String sPath( xFP->getFiles().getConstArray()[0] );
481             //verwendeten Pfad merken - bitte nicht wieder wegoptimieren!
482             aPath.SetSmartURL( sPath);
483             sGrfPath = aPath.GetPath();
484 
485             if( rGrfName.Len() &&
486                  nDfltFilter == rGF.GetExportFormatNumber( xFltMgr->getCurrentFilter()))
487             {
488                 //Versuchen die Originalgrafik zu speichern.
489                 SfxMedium aIn( rGrfName, STREAM_READ | STREAM_NOCREATE,
490                                 sal_True );
491                 if( aIn.GetInStream() && !aIn.GetInStream()->GetError() )
492                 {
493                     SfxMedium aOut( sPath, STREAM_WRITE | STREAM_SHARE_DENYNONE,
494                                             sal_False);
495                     if( aOut.GetOutStream() && !aOut.GetOutStream()->GetError())
496                     {
497                         *aOut.GetOutStream() << *aIn.GetInStream();
498                         if ( 0 == aIn.GetError() )
499                         {
500                             aOut.Close();
501                             aOut.Commit();
502                             if ( 0 == aOut.GetError() )
503                                 return sPath;
504                         }
505                     }
506                 }
507             }
508 
509             sal_uInt16 nFilter;
510             if ( xFltMgr->getCurrentFilter().getLength() && rGF.GetExportFormatCount() )
511                 nFilter = rGF.GetExportFormatNumber( xFltMgr->getCurrentFilter() );
512             else
513                 nFilter = GRFILTER_FORMAT_DONTKNOW;
514             String aFilter( rGF.GetExportFormatShortName( nFilter ) );
515             XOutBitmap::WriteGraphic( rGraphic, sPath, aFilter,
516                                         XOUTBMP_DONT_EXPAND_FILENAME|XOUTBMP_DONT_ADD_EXTENSION );
517             return sPath;
518         }
519     }
520     return aEmptyStr;
521 }
522