xref: /aoo41x/main/sw/source/ui/docvw/romenu.cxx (revision efeef26f)
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 		case GFX_LINK_TYPE_NATIVE_TIF:      pExt = "tif"; break;
385 		case GFX_LINK_TYPE_NATIVE_WMF:      pExt = "wmf"; break;
386 		case GFX_LINK_TYPE_NATIVE_MET:      pExt = "met"; break;
387 		case GFX_LINK_TYPE_NATIVE_PCT:		pExt = "pct"; break;
388 		case GFX_LINK_TYPE_NATIVE_JPG:		pExt = "jpg"; break;
389         default:; //prevent warning
390 	}
391 	rExt.AssignAscii( pExt );
392 }
393 
394 
395 String SwReadOnlyPopup::SaveGraphic( sal_uInt16 nId )
396 {
397 
398 	//Namen der Grafik herausfischen.
399 	String aName;
400 	if ( MN_READONLY_SAVEBACKGROUND == nId )
401 	{
402 		if ( pItem->GetGraphicLink() )
403             sGrfName = *pItem->GetGraphicLink();
404 		((SvxBrushItem*)pItem)->SetDoneLink( Link() );
405 		const Graphic *pGrf = pItem->GetGraphic();
406 		if ( pGrf )
407 		{
408 			aGraphic = *pGrf;
409 			if ( pItem->GetGraphicLink() )
410 				sGrfName = *pItem->GetGraphicLink();
411 		}
412 		else
413 			return aEmptyStr;
414 	}
415     return ExportGraphic( aGraphic, sGrfName );
416 }
417 
418 String ExportGraphic( const Graphic &rGraphic, const String &rGrfName )
419 {
420 	SvtPathOptions aPathOpt;
421 	String sGrfPath( aPathOpt.GetGraphicPath() );
422 
423     FileDialogHelper aDlgHelper( TemplateDescription::FILESAVE_AUTOEXTENSION, 0 );
424     Reference < XFilePicker > xFP = aDlgHelper.GetFilePicker();
425 
426 //    aExpDlg.SetHelpId(HID_FILEDLG_ROMENU);
427 	INetURLObject aPath;
428 	aPath.SetSmartURL( sGrfPath );
429 
430 	//Namen der Grafik herausfischen.
431 	String aName = rGrfName;
432 
433 	aDlgHelper.SetTitle( SW_RESSTR(STR_EXPORT_GRAFIK_TITLE));
434 	aDlgHelper.SetDisplayDirectory( aPath.GetMainURL(INetURLObject::DECODE_TO_IURI) );
435     INetURLObject aURL;
436     aURL.SetSmartURL( aName );
437     aDlgHelper.SetFileName( aURL.GetName() );
438 
439 	GraphicFilter& rGF = *GraphicFilter::GetGraphicFilter();
440 	const sal_uInt16 nCount = rGF.GetExportFormatCount();
441 
442 	String aExt( aURL.GetExtension() );
443 	if( !aExt.Len() )
444 		lcl_GetPreferedExtension( aExt, rGraphic );
445 
446 	aExt.ToLowerAscii();
447     sal_uInt16 nDfltFilter = USHRT_MAX;
448 
449     Reference<XFilterManager> xFltMgr(xFP, UNO_QUERY);
450 
451     for ( sal_uInt16 i = 0; i < nCount; i++ )
452 	{
453         xFltMgr->appendFilter( rGF.GetExportFormatName( i ), rGF.GetExportWildcard( i ) );
454 		if ( COMPARE_EQUAL == aExt.CompareIgnoreCaseToAscii(rGF.GetExportFormatShortName( i ).ToLowerAscii() ))
455 			nDfltFilter = i;
456 	}
457     if ( USHRT_MAX == nDfltFilter )
458 	{
459 		//"falsche" Extension?
460 		lcl_GetPreferedExtension( aExt, rGraphic );
461         for ( sal_uInt16 i = 0; i < nCount; ++i )
462 			if ( aExt == rGF.GetExportFormatShortName( i ).ToLowerAscii() )
463 			{
464 				nDfltFilter =  i;
465 				break;
466 			}
467 	}
468 
469     if( USHRT_MAX != nDfltFilter )
470 	{
471         xFltMgr->setCurrentFilter( rGF.GetExportFormatName( nDfltFilter ) ) ;
472 
473         if( aDlgHelper.Execute() == ERRCODE_NONE )
474 		{
475             String sPath( xFP->getFiles().getConstArray()[0] );
476 			//verwendeten Pfad merken - bitte nicht wieder wegoptimieren!
477 			aPath.SetSmartURL( sPath);
478 			sGrfPath = aPath.GetPath();
479 
480 			if( rGrfName.Len() &&
481                  nDfltFilter == rGF.GetExportFormatNumber( xFltMgr->getCurrentFilter()))
482 			{
483 				//Versuchen die Originalgrafik zu speichern.
484 				SfxMedium aIn( rGrfName, STREAM_READ | STREAM_NOCREATE,
485 								sal_True );
486 				if( aIn.GetInStream() && !aIn.GetInStream()->GetError() )
487 				{
488 					SfxMedium aOut( sPath, STREAM_WRITE | STREAM_SHARE_DENYNONE,
489 											sal_False);
490 					if( aOut.GetOutStream() && !aOut.GetOutStream()->GetError())
491 					{
492 						*aOut.GetOutStream() << *aIn.GetInStream();
493 						if ( 0 == aIn.GetError() )
494 						{
495 							aOut.Close();
496 							aOut.Commit();
497 							if ( 0 == aOut.GetError() )
498 								return sPath;
499 						}
500 					}
501 				}
502 			}
503 
504             sal_uInt16 nFilter;
505             if ( xFltMgr->getCurrentFilter().getLength() && rGF.GetExportFormatCount() )
506                 nFilter = rGF.GetExportFormatNumber( xFltMgr->getCurrentFilter() );
507 			else
508 				nFilter = GRFILTER_FORMAT_DONTKNOW;
509 			String aFilter( rGF.GetExportFormatShortName( nFilter ) );
510 			XOutBitmap::WriteGraphic( rGraphic, sPath, aFilter,
511                                         XOUTBMP_DONT_EXPAND_FILENAME|XOUTBMP_DONT_ADD_EXTENSION );
512 			return sPath;
513 		}
514 	}
515 	return aEmptyStr;
516 }
517 
518 
519 
520