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