Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include <com/sun/star/embed/VerbDescriptor.hpp>
21 : #include <com/sun/star/embed/VerbAttributes.hpp>
22 : #include <com/sun/star/container/XNamed.hpp>
23 :
24 : #ifdef SOLARIS
25 : #include <ctime>
26 : #endif
27 :
28 : #include <string>
29 : #include <cstdarg> // std::va_list
30 :
31 : #include <vcl/pointr.hxx>
32 :
33 : #include <unotools/streamwrap.hxx>
34 : #include <sfx2/objsh.hxx>
35 : #include <framework/menuconfiguration.hxx>
36 : #include <framework/addonmenu.hxx>
37 : #include <comphelper/processfactory.hxx>
38 : #include <unotools/ucbstreamhelper.hxx>
39 : #include <unotools/lingucfg.hxx>
40 : #include <unotools/pathoptions.hxx>
41 : #include <svl/stritem.hxx>
42 : #include <toolkit/helper/vclunohelper.hxx>
43 : #include <osl/file.hxx>
44 : #include <vcl/graph.hxx>
45 : #include <vcl/graphicfilter.hxx>
46 : #include <svl/lngmisc.hxx>
47 :
48 : #include <sfx2/mnumgr.hxx>
49 :
50 : #include <svtools/menuoptions.hxx>
51 :
52 : #include "virtmenu.hxx"
53 : #include <sfx2/msg.hxx>
54 : #include <sfx2/dispatch.hxx>
55 : #include <sfx2/app.hxx>
56 : #include "sfxtypes.hxx"
57 : #include <sfx2/bindings.hxx>
58 : #include "sfx2/sfxresid.hxx"
59 : #include <sfx2/msgpool.hxx>
60 : #include <sfx2/sfx.hrc>
61 : #include "menu.hrc"
62 : #include <sfx2/viewfrm.hxx>
63 : #include <sfx2/viewsh.hxx>
64 : #include <sfx2/objface.hxx>
65 : #include "thessubmenu.hxx"
66 :
67 : // static member initialization
68 : PopupMenu * SfxPopupMenuManager::pStaticThesSubMenu = NULL;
69 :
70 : using namespace com::sun::star;
71 :
72 0 : void TryToHideDisabledEntries_Impl( Menu* pMenu )
73 : {
74 : DBG_ASSERT( pMenu, "invalid menu" );
75 0 : if( SvtMenuOptions().IsEntryHidingEnabled() == sal_False )
76 : {
77 0 : pMenu->SetMenuFlags( pMenu->GetMenuFlags() | MENU_FLAG_HIDEDISABLEDENTRIES );
78 : }
79 0 : }
80 :
81 : //--------------------------------------------------------------------
82 :
83 0 : SfxMenuManager::~SfxMenuManager()
84 : {
85 0 : pBindings->ENTERREGISTRATIONS();
86 0 : delete pMenu;
87 0 : pBindings->LEAVEREGISTRATIONS();
88 0 : }
89 :
90 : //--------------------------------------------------------------------
91 :
92 0 : void SfxMenuManager::Construct( SfxVirtualMenu& rMenu )
93 : {
94 0 : pMenu = &rMenu;
95 :
96 : // set the handlers
97 0 : Menu *pSvMenu = pMenu->GetSVMenu();
98 0 : pSvMenu->SetSelectHdl( LINK(this, SfxMenuManager, Select) );
99 0 : TryToHideDisabledEntries_Impl( pSvMenu );
100 0 : }
101 :
102 : //-------------------------------------------------------------------------
103 0 : void InsertVerbs_Impl( SfxBindings* pBindings, const com::sun::star::uno::Sequence < com::sun::star::embed::VerbDescriptor >& aVerbs, Menu* pMenu )
104 : {
105 0 : SfxViewShell *pView = pBindings->GetDispatcher()->GetFrame()->GetViewShell();
106 0 : if ( pView && aVerbs.getLength() )
107 : {
108 0 : SfxObjectShell* pDoc = pView->GetObjectShell();
109 0 : pMenu->InsertSeparator();
110 0 : sal_uInt16 nr=0;
111 0 : for ( sal_uInt16 n = 0; n < aVerbs.getLength(); ++n )
112 : {
113 : // check for ReadOnly verbs
114 0 : if ( pDoc->IsReadOnly() && !(aVerbs[n].VerbAttributes & embed::VerbAttributes::MS_VERBATTR_NEVERDIRTIES) )
115 0 : continue;
116 :
117 : // check for verbs that shouldn't appear in the menu
118 0 : if ( !(aVerbs[n].VerbAttributes & embed::VerbAttributes::MS_VERBATTR_ONCONTAINERMENU) )
119 0 : continue;
120 :
121 0 : sal_uInt16 nId = SID_VERB_START + nr++;
122 : DBG_ASSERT(nId <= SID_VERB_END, "Too many verbs!");
123 0 : if ( nId > SID_VERB_END )
124 0 : break;
125 :
126 0 : pMenu->InsertItem( nId, aVerbs[n].VerbName );
127 : }
128 : }
129 0 : }
130 :
131 :
132 : //--------------------------------------------------------------------
133 :
134 :
135 0 : static Image lcl_GetImageFromPngUrl( const OUString &rFileUrl )
136 : {
137 0 : Image aRes;
138 :
139 0 : OUString aTmp;
140 0 : osl::FileBase::getSystemPathFromFileURL( rFileUrl, aTmp );
141 :
142 0 : Graphic aGraphic;
143 0 : const String aFilterName( IMP_PNG );
144 0 : if( GRFILTER_OK == GraphicFilter::LoadGraphic( aTmp, aFilterName, aGraphic ) )
145 : {
146 0 : aRes = Image( aGraphic.GetBitmapEx() );
147 : }
148 0 : return aRes;
149 : }
150 :
151 :
152 0 : PopupMenu* InsertThesaurusSubmenu_Impl( SfxBindings* pBindings, Menu* pSVMenu )
153 : {
154 :
155 : // build thesaurus sub menu if look-up string is available
156 :
157 0 : PopupMenu* pThesSubMenu = 0;
158 0 : SfxPoolItem *pItem = 0;
159 0 : pBindings->QueryState( SID_THES, pItem );
160 0 : String aThesLookUpStr;
161 0 : SfxStringItem *pStrItem = dynamic_cast< SfxStringItem * >(pItem);
162 0 : xub_StrLen nDelimPos = STRING_LEN;
163 0 : if (pStrItem)
164 : {
165 0 : aThesLookUpStr = pStrItem->GetValue();
166 0 : nDelimPos = aThesLookUpStr.SearchBackward( '#' );
167 : }
168 0 : delete pItem;
169 0 : if (aThesLookUpStr.Len() > 0 && nDelimPos != STRING_NOTFOUND)
170 : {
171 : // get synonym list for sub menu
172 0 : std::vector< OUString > aSynonyms;
173 0 : SfxThesSubMenuHelper aHelper;
174 0 : OUString aText( aHelper.GetText( aThesLookUpStr, nDelimPos ) );
175 0 : lang::Locale aLocale;
176 0 : aHelper.GetLocale( aLocale, aThesLookUpStr, nDelimPos );
177 0 : const bool bHasMoreSynonyms = aHelper.GetMeanings( aSynonyms, aText, aLocale, 7 /*max number of synonyms to retrieve*/ );
178 : (void) bHasMoreSynonyms;
179 :
180 0 : pThesSubMenu = new PopupMenu;
181 0 : pThesSubMenu->SetMenuFlags(MENU_FLAG_NOAUTOMNEMONICS);
182 0 : const size_t nNumSynonyms = aSynonyms.size();
183 0 : if (nNumSynonyms > 0)
184 : {
185 0 : SvtLinguConfig aCfg;
186 :
187 0 : Image aImage;
188 0 : String sThesImplName( aHelper.GetThesImplName( aLocale ) );
189 0 : OUString aSynonymsImageUrl( aCfg.GetSynonymsContextImage( sThesImplName ) );
190 0 : if (sThesImplName.Len() > 0 && !aSynonymsImageUrl.isEmpty())
191 0 : aImage = Image( lcl_GetImageFromPngUrl( aSynonymsImageUrl ) );
192 :
193 0 : for (sal_uInt16 i = 0; (size_t)i < nNumSynonyms; ++i)
194 : {
195 : //! item ids should start with values > 0, since 0 has special meaning
196 0 : const sal_uInt16 nId = i + 1;
197 :
198 0 : String aItemText( linguistic::GetThesaurusReplaceText( aSynonyms[i] ) );
199 0 : pThesSubMenu->InsertItem( nId, aItemText );
200 0 : OUString aCmd(".uno:ThesaurusFromContext?WordReplace:string=" );
201 0 : aCmd += aItemText;
202 0 : pThesSubMenu->SetItemCommand( nId, aCmd );
203 :
204 0 : if (!aSynonymsImageUrl.isEmpty())
205 0 : pThesSubMenu->SetItemImage( nId, aImage );
206 0 : }
207 : }
208 : else // nNumSynonyms == 0
209 : {
210 0 : const OUString aItemText( SfxResId(STR_MENU_NO_SYNONYM_FOUND).toString() );
211 0 : pThesSubMenu->InsertItem( 1, aItemText, MIB_NOSELECT );
212 : }
213 0 : pThesSubMenu->InsertSeparator();
214 0 : const OUString sThesaurus( SfxResId(STR_MENU_THESAURUS).toString() );
215 0 : pThesSubMenu->InsertItem( 100, sThesaurus );
216 0 : pThesSubMenu->SetItemCommand( 100, OUString(".uno:ThesaurusDialog") );
217 :
218 0 : pSVMenu->InsertSeparator();
219 0 : const OUString sSynonyms( SfxResId(STR_MENU_SYNONYMS).toString() );
220 0 : pSVMenu->InsertItem( SID_THES, sSynonyms );
221 0 : pSVMenu->SetPopupMenu( SID_THES, pThesSubMenu );
222 : }
223 :
224 0 : return pThesSubMenu;
225 : }
226 :
227 : // ------------------------------------------------------------------------
228 :
229 : // executes the function for the selected item
230 0 : IMPL_LINK( SfxMenuManager, Select, Menu *, pSelMenu )
231 : {
232 0 : sal_uInt16 nId = (sal_uInt16) pSelMenu->GetCurItemId();
233 0 : String aCommand = pSelMenu->GetItemCommand( nId );
234 0 : if ( !aCommand.Len() && pBindings )
235 : {
236 0 : const SfxSlot* pSlot = SfxSlotPool::GetSlotPool( pBindings->GetDispatcher()->GetFrame() ).GetSlot( nId );
237 0 : if ( pSlot && pSlot->pUnoName )
238 : {
239 0 : aCommand = ".uno:" + OUString::createFromAscii( pSlot->GetUnoName() );
240 : }
241 : }
242 :
243 0 : if ( aCommand.Len() )
244 : {
245 0 : pBindings->ExecuteCommand_Impl( aCommand );
246 : }
247 0 : else if ( pBindings->IsBound(nId) )
248 : // normal function
249 0 : pBindings->Execute( nId );
250 : else
251 : // special menu function
252 0 : pBindings->GetDispatcher_Impl()->Execute( nId );
253 :
254 0 : return sal_True;
255 : }
256 :
257 0 : SfxPopupMenuManager::~SfxPopupMenuManager()
258 : {
259 0 : }
260 :
261 : //-------------------------------------------------------------------------
262 :
263 0 : void SfxPopupMenuManager::RemoveDisabledEntries()
264 : {
265 0 : if ( pSVMenu )
266 0 : TryToHideDisabledEntries_Impl( pSVMenu );
267 0 : }
268 :
269 : //--------------------------------------------------------------------
270 :
271 0 : sal_uInt16 SfxPopupMenuManager::Execute( const Point& rPos, Window* pWindow )
272 : {
273 0 : sal_uInt16 nVal = ( (PopupMenu*) GetMenu()->GetSVMenu() )->Execute( pWindow, rPos );
274 0 : delete pStaticThesSubMenu; pStaticThesSubMenu = NULL;
275 0 : return nVal;
276 : }
277 :
278 : //-------------------------------------------------------------------------
279 :
280 0 : SfxMenuManager::SfxMenuManager( Menu* pMenuArg, SfxBindings &rBindings )
281 : : pMenu(0),
282 : pOldMenu(0),
283 : pBindings(&rBindings),
284 : pResMgr(NULL),
285 0 : nType(0)
286 : {
287 0 : bAddClipboardFuncs = sal_False;
288 0 : SfxVirtualMenu* pVMenu = new SfxVirtualMenu( pMenuArg, sal_False, rBindings, sal_True, sal_True );
289 0 : Construct(*pVMenu);
290 0 : }
291 :
292 0 : SfxPopupMenuManager::SfxPopupMenuManager( PopupMenu* pMenuArg, SfxBindings& rBindings )
293 : : SfxMenuManager( pMenuArg, rBindings )
294 0 : , pSVMenu( pMenuArg )
295 : {
296 0 : }
297 :
298 0 : SfxPopupMenuManager* SfxPopupMenuManager::Popup( const ResId& rResId, SfxViewFrame* pFrame,const Point& rPoint, Window* pWindow )
299 : {
300 0 : PopupMenu *pSVMenu = new PopupMenu( rResId );
301 0 : sal_uInt16 n, nCount = pSVMenu->GetItemCount();
302 0 : for ( n=0; n<nCount; n++ )
303 : {
304 0 : sal_uInt16 nId = pSVMenu->GetItemId( n );
305 0 : if ( nId == SID_COPY || nId == SID_CUT || nId == SID_PASTE )
306 : break;
307 : }
308 :
309 0 : PopupMenu* pThesSubMenu = InsertThesaurusSubmenu_Impl( &pFrame->GetBindings(), pSVMenu );
310 : // #i107205# (see comment in header file)
311 0 : pStaticThesSubMenu = pThesSubMenu;
312 :
313 0 : if ( n == nCount )
314 : {
315 0 : PopupMenu aPop( SfxResId( MN_CLIPBOARDFUNCS ) );
316 0 : nCount = aPop.GetItemCount();
317 0 : pSVMenu->InsertSeparator();
318 0 : for ( n=0; n<nCount; n++ )
319 : {
320 0 : sal_uInt16 nId = aPop.GetItemId( n );
321 0 : pSVMenu->InsertItem( nId, aPop.GetItemText( nId ), aPop.GetItemBits( nId ) );
322 0 : pSVMenu->SetHelpId( nId, aPop.GetHelpId( nId ));
323 0 : }
324 : }
325 :
326 0 : InsertVerbs_Impl( &pFrame->GetBindings(), pFrame->GetViewShell()->GetVerbs(), pSVMenu );
327 0 : Menu* pMenu = NULL;
328 0 : ::com::sun::star::ui::ContextMenuExecuteEvent aEvent;
329 0 : aEvent.SourceWindow = VCLUnoHelper::GetInterface( pWindow );
330 0 : aEvent.ExecutePosition.X = rPoint.X();
331 0 : aEvent.ExecutePosition.Y = rPoint.Y();
332 0 : OUString sDummyMenuName;
333 0 : if ( pFrame->GetViewShell()->TryContextMenuInterception( *pSVMenu, sDummyMenuName, pMenu, aEvent ) )
334 : {
335 0 : if ( pMenu )
336 : {
337 0 : delete pSVMenu;
338 0 : pSVMenu = (PopupMenu*) pMenu;
339 : }
340 :
341 0 : SfxPopupMenuManager* aMgr = new SfxPopupMenuManager( pSVMenu, pFrame->GetBindings());
342 0 : aMgr->RemoveDisabledEntries();
343 0 : return aMgr;
344 : }
345 :
346 0 : return 0;
347 : }
348 :
349 :
350 0 : void SfxPopupMenuManager::ExecutePopup( const ResId& rResId, SfxViewFrame* pFrame, const Point& rPoint, Window* pWindow )
351 : {
352 0 : PopupMenu *pSVMenu = new PopupMenu( rResId );
353 0 : sal_uInt16 n, nCount = pSVMenu->GetItemCount();
354 0 : for ( n=0; n<nCount; n++ )
355 : {
356 0 : sal_uInt16 nId = pSVMenu->GetItemId( n );
357 0 : if ( nId == SID_COPY || nId == SID_CUT || nId == SID_PASTE )
358 : break;
359 : }
360 :
361 0 : PopupMenu* pThesSubMenu = InsertThesaurusSubmenu_Impl( &pFrame->GetBindings(), pSVMenu );
362 :
363 0 : if ( n == nCount )
364 : {
365 0 : PopupMenu aPop( SfxResId( MN_CLIPBOARDFUNCS ) );
366 0 : nCount = aPop.GetItemCount();
367 0 : pSVMenu->InsertSeparator();
368 0 : for ( n=0; n<nCount; n++ )
369 : {
370 0 : sal_uInt16 nId = aPop.GetItemId( n );
371 0 : pSVMenu->InsertItem( nId, aPop.GetItemText( nId ), aPop.GetItemBits( nId ) );
372 0 : pSVMenu->SetHelpId( nId, aPop.GetHelpId( nId ));
373 0 : }
374 : }
375 :
376 0 : InsertVerbs_Impl( &pFrame->GetBindings(), pFrame->GetViewShell()->GetVerbs(), pSVMenu );
377 0 : Menu* pMenu = NULL;
378 0 : ::com::sun::star::ui::ContextMenuExecuteEvent aEvent;
379 0 : aEvent.SourceWindow = VCLUnoHelper::GetInterface( pWindow );
380 0 : aEvent.ExecutePosition.X = rPoint.X();
381 0 : aEvent.ExecutePosition.Y = rPoint.Y();
382 0 : OUString sDummyMenuName;
383 0 : if ( pFrame->GetViewShell()->TryContextMenuInterception( *pSVMenu, sDummyMenuName, pMenu, aEvent ) )
384 : {
385 0 : if ( pMenu )
386 : {
387 0 : delete pSVMenu;
388 0 : pSVMenu = (PopupMenu*) pMenu;
389 : }
390 :
391 0 : SfxPopupMenuManager aPop( pSVMenu, pFrame->GetBindings() );
392 0 : aPop.RemoveDisabledEntries();
393 0 : aPop.Execute( rPoint, pWindow );
394 :
395 : // #i112646 avoid crash when context menu is closed.
396 : // the (manually inserted) sub-menu needs to be destroyed before
397 : // aPop gets destroyed.
398 0 : delete pThesSubMenu;
399 0 : pThesSubMenu = 0;
400 : }
401 :
402 0 : delete pThesSubMenu;
403 0 : }
404 :
405 0 : Menu* SfxPopupMenuManager::GetSVMenu()
406 : {
407 0 : return (Menu*) GetMenu()->GetSVMenu();
408 408 : }
409 :
410 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|