xref: /trunk/main/sw/source/ui/docvw/romenu.cxx (revision 72583341)
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;
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 
245 	if ( !sURL.Len() )
246 	{
247 		EnableItem( MN_READONLY_OPENURL, sal_False );
248 		EnableItem( MN_READONLY_OPENURLNEW, sal_False );
249 		EnableItem( MN_READONLY_COPYLINK, sal_False );
250 	}
251     Check( SID_WIN_FULLSCREEN,         SID_WIN_FULLSCREEN,        rDis );
252 
253 	RemoveDisabledEntries( sal_True, sal_True );
254 }
255 
256 void SwReadOnlyPopup::Execute( Window* pWin, const Point &rPixPos )
257 {
258     sal_uInt16 nId     = PopupMenu::Execute(
259 	pWin,
260 	rPixPos );
261     Execute(pWin, nId);
262 }
263 
264 /*-- 17.03.2004 13:06:18---------------------------------------------------
265     execute the resulting ID only - necessary to support XContextMenuInterception
266   -----------------------------------------------------------------------*/
267 void SwReadOnlyPopup::Execute( Window* pWin, sal_uInt16 nId )
268 {
269     SwWrtShell &rSh = rView.GetWrtShell();
270     SfxDispatcher &rDis = *rView.GetViewFrame()->GetDispatcher();
271     if ( nId >= MN_READONLY_GRAPHICTOGALLERY )
272 	{
273 		String sTmp;
274 		sal_uInt16 nSaveId;
275         if ( nId >= MN_READONLY_BACKGROUNDTOGALLERY )
276 		{
277 			nId -= MN_READONLY_BACKGROUNDTOGALLERY+3;
278 			nSaveId = MN_READONLY_SAVEBACKGROUND;
279 			sTmp = *pItem->GetGraphicLink();
280 		}
281 		else
282 		{
283 			nId -= MN_READONLY_GRAPHICTOGALLERY+3;
284 			nSaveId = MN_READONLY_SAVEGRAPHIC;
285 			sTmp = sGrfName;
286 		}
287 		if ( !bGrfToGalleryAsLnk )
288 			sTmp = SaveGraphic( nSaveId );
289 
290 		if ( sTmp.Len() )
291 		{
292 			String sThemeName( *(String*)aThemeList.GetObject( nId ));
293 			GalleryExplorer::InsertURL( sThemeName, sTmp );
294 		}
295 		return;
296 	}
297 
298 	TransferDataContainer* pClipCntnr = 0;
299 
300 	sal_uInt16 nExecId = USHRT_MAX;
301 	sal_uInt16 nFilter = USHRT_MAX;
302 	switch( nId )
303 	{
304         case SID_WIN_FULLSCREEN :           nExecId = SID_WIN_FULLSCREEN; break;
305         case MN_READONLY_OPENURL:           nFilter = URLLOAD_NOFILTER;   break;
306 		case MN_READONLY_OPENURLNEW:		nFilter = URLLOAD_NEWVIEW;	  break;
307         case MN_READONLY_COPY:              nExecId = SID_COPY;           break;
308 
309 		case MN_READONLY_EDITDOC:			nExecId = SID_EDITDOC;		  break;
310         case MN_READONLY_SELECTION_MODE:    nExecId = FN_READONLY_SELECTION_MODE; break;
311 		case MN_READONLY_RELOAD:
312 		case MN_READONLY_RELOAD_FRAME:
313 			rSh.GetView().GetViewFrame()->GetDispatcher()->Execute(SID_RELOAD);
314 		break;
315 
316 		case MN_READONLY_BROWSE_BACKWARD:	nExecId = SID_BROWSE_BACKWARD;break;
317 		case MN_READONLY_BROWSE_FORWARD:	nExecId = SID_BROWSE_FORWARD; break;
318 		case MN_READONLY_SOURCEVIEW:		nExecId = SID_SOURCEVIEW;	  break;
319         case MN_READONLY_SAVEGRAPHIC:
320 		case MN_READONLY_SAVEBACKGROUND:
321 			{
322 				SaveGraphic( nId );
323 				break;
324 			}
325 		case MN_READONLY_COPYLINK:
326 			pClipCntnr = new TransferDataContainer;
327 			pClipCntnr->CopyString( sURL );
328 			break;
329 
330 		case MN_READONLY_COPYGRAPHIC:
331 			pClipCntnr = new TransferDataContainer;
332 			pClipCntnr->CopyGraphic( aGraphic );
333 
334 			if( pImageMap )
335 				pClipCntnr->CopyImageMap( *pImageMap );
336 			if( pTargetURL )
337 				pClipCntnr->CopyINetImage( *pTargetURL );
338 			break;
339 
340 		case MN_READONLY_LOADGRAPHIC:
341 			{
342 				sal_Bool bModified = rSh.IsModified();
343 				SwViewOption aOpt( *rSh.GetViewOptions() );
344 				aOpt.SetGraphic( sal_True );
345 				rSh.ApplyViewOptions( aOpt );
346 				if(!bModified)
347 					rSh.ResetModified();
348 				break;
349 			}
350 		case MN_READONLY_GRAPHICOFF:		nExecId = FN_VIEW_GRAPHIC;	  break;
351 #ifdef WNT
352 		case MN_READONLY_PLUGINOFF:			nExecId = SID_PLUGINS_ACTIVE; break;
353 #endif
354 		case MN_READONLY_TOGALLERYLINK:
355 			SW_MOD()->GetModuleConfig()->SetGrfToGalleryAsLnk( sal_True );
356 			break;
357 		case MN_READONLY_TOGALLERYCOPY:
358 			SW_MOD()->GetModuleConfig()->SetGrfToGalleryAsLnk( sal_False );
359 			break;
360 
361 		default: //forward the id to the SfxBindings
362             nExecId = nId;
363 	}
364 	if( USHRT_MAX != nExecId )
365 		rDis.GetBindings()->Execute( nExecId );
366 	if( USHRT_MAX != nFilter )
367 		::LoadURL( sURL, &rSh, nFilter, &sTargetFrameName);
368 
369 	if( pClipCntnr )
370 	{
371 		STAR_REFERENCE( datatransfer::XTransferable ) xRef( pClipCntnr );
372 		if( pClipCntnr->HasAnyData() )
373 			pClipCntnr->CopyToClipboard( pWin );
374 	}
375 }
376 static void lcl_GetPreferedExtension( String &rExt, const Graphic &rGrf )
377 {
378 	// dann ggfs. ueber die native-Info der Grafik den "besten"
379 	// Filter vorschlagen
380 	const sal_Char* pExt = "png";
381 	switch( const_cast<Graphic&>(rGrf).GetLink().GetType() )
382 	{
383 		case GFX_LINK_TYPE_NATIVE_GIF:      pExt = "gif"; break;
384 
385         // #15508# added BMP type for better exports (writer export graphic - checked, works)
386         case GFX_LINK_TYPE_NATIVE_BMP:      pExt = "bmp"; break;
387 
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