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 <svtools/filter.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/minstack.hxx>
56 : #include <sfx2/app.hxx>
57 : #include "sfxtypes.hxx"
58 : #include <sfx2/bindings.hxx>
59 : #include "mnucfga.hxx"
60 : #include "sfx2/sfxresid.hxx"
61 : #include <sfx2/msgpool.hxx>
62 : #include <sfx2/sfx.hrc>
63 : #include "menu.hrc"
64 : #include <sfx2/viewfrm.hxx>
65 : #include <sfx2/viewsh.hxx>
66 : #include <sfx2/objface.hxx>
67 : #include "thessubmenu.hxx"
68 :
69 : // static member initialization
70 : PopupMenu * SfxPopupMenuManager::pStaticThesSubMenu = NULL;
71 :
72 : using namespace com::sun::star;
73 :
74 0 : void TryToHideDisabledEntries_Impl( Menu* pMenu )
75 : {
76 : DBG_ASSERT( pMenu, "invalid menu" );
77 0 : if( SvtMenuOptions().IsEntryHidingEnabled() == sal_False )
78 : {
79 0 : pMenu->SetMenuFlags( pMenu->GetMenuFlags() | MENU_FLAG_HIDEDISABLEDENTRIES );
80 : }
81 0 : }
82 :
83 : //--------------------------------------------------------------------
84 :
85 0 : SfxMenuManager::~SfxMenuManager()
86 : {
87 0 : pBindings->ENTERREGISTRATIONS();
88 0 : delete pMenu;
89 0 : pBindings->LEAVEREGISTRATIONS();
90 0 : }
91 :
92 : //--------------------------------------------------------------------
93 :
94 0 : void SfxMenuManager::Construct( SfxVirtualMenu& rMenu )
95 : {
96 0 : pMenu = &rMenu;
97 :
98 : // set the handlers
99 0 : Menu *pSvMenu = pMenu->GetSVMenu();
100 0 : pSvMenu->SetSelectHdl( LINK(this, SfxMenuManager, Select) );
101 0 : TryToHideDisabledEntries_Impl( pSvMenu );
102 0 : }
103 :
104 : //-------------------------------------------------------------------------
105 0 : void InsertVerbs_Impl( SfxBindings* pBindings, const com::sun::star::uno::Sequence < com::sun::star::embed::VerbDescriptor >& aVerbs, Menu* pMenu )
106 : {
107 0 : SfxViewShell *pView = pBindings->GetDispatcher()->GetFrame()->GetViewShell();
108 0 : if ( pView && aVerbs.getLength() )
109 : {
110 0 : SfxObjectShell* pDoc = pView->GetObjectShell();
111 0 : pMenu->InsertSeparator();
112 0 : sal_uInt16 nr=0;
113 0 : for ( sal_uInt16 n = 0; n < aVerbs.getLength(); ++n )
114 : {
115 : // check for ReadOnly verbs
116 0 : if ( pDoc->IsReadOnly() && !(aVerbs[n].VerbAttributes & embed::VerbAttributes::MS_VERBATTR_NEVERDIRTIES) )
117 0 : continue;
118 :
119 : // check for verbs that shouldn't appear in the menu
120 0 : if ( !(aVerbs[n].VerbAttributes & embed::VerbAttributes::MS_VERBATTR_ONCONTAINERMENU) )
121 0 : continue;
122 :
123 0 : sal_uInt16 nId = SID_VERB_START + nr++;
124 : DBG_ASSERT(nId <= SID_VERB_END, "Too many verbs!");
125 0 : if ( nId > SID_VERB_END )
126 0 : break;
127 :
128 0 : pMenu->InsertItem( nId, aVerbs[n].VerbName );
129 : }
130 : }
131 0 : }
132 :
133 :
134 : //--------------------------------------------------------------------
135 :
136 :
137 0 : static Image lcl_GetImageFromPngUrl( const ::rtl::OUString &rFileUrl )
138 : {
139 0 : Image aRes;
140 :
141 0 : ::rtl::OUString aTmp;
142 0 : osl::FileBase::getSystemPathFromFileURL( rFileUrl, aTmp );
143 :
144 0 : Graphic aGraphic;
145 0 : const String aFilterName( IMP_PNG );
146 0 : if( GRFILTER_OK == GraphicFilter::LoadGraphic( aTmp, aFilterName, aGraphic ) )
147 : {
148 0 : aRes = Image( aGraphic.GetBitmapEx() );
149 : }
150 0 : return aRes;
151 : }
152 :
153 :
154 0 : PopupMenu* InsertThesaurusSubmenu_Impl( SfxBindings* pBindings, Menu* pSVMenu )
155 : {
156 :
157 : // build thesaurus sub menu if look-up string is available
158 :
159 0 : PopupMenu* pThesSubMenu = 0;
160 0 : SfxPoolItem *pItem = 0;
161 0 : pBindings->QueryState( SID_THES, pItem );
162 0 : String aThesLookUpStr;
163 0 : SfxStringItem *pStrItem = dynamic_cast< SfxStringItem * >(pItem);
164 0 : xub_StrLen nDelimPos = STRING_LEN;
165 0 : if (pStrItem)
166 : {
167 0 : aThesLookUpStr = pStrItem->GetValue();
168 0 : nDelimPos = aThesLookUpStr.SearchBackward( '#' );
169 : }
170 0 : if (aThesLookUpStr.Len() > 0 && nDelimPos != STRING_NOTFOUND)
171 : {
172 : // get synonym list for sub menu
173 0 : std::vector< ::rtl::OUString > aSynonyms;
174 0 : SfxThesSubMenuHelper aHelper;
175 0 : ::rtl::OUString aText( aHelper.GetText( aThesLookUpStr, nDelimPos ) );
176 0 : lang::Locale aLocale;
177 0 : aHelper.GetLocale( aLocale, aThesLookUpStr, nDelimPos );
178 0 : const bool bHasMoreSynonyms = aHelper.GetMeanings( aSynonyms, aText, aLocale, 7 /*max number of synonyms to retrieve*/ );
179 : (void) bHasMoreSynonyms;
180 :
181 0 : pThesSubMenu = new PopupMenu;
182 0 : pThesSubMenu->SetMenuFlags(MENU_FLAG_NOAUTOMNEMONICS);
183 0 : const size_t nNumSynonyms = aSynonyms.size();
184 0 : if (nNumSynonyms > 0)
185 : {
186 0 : SvtLinguConfig aCfg;
187 :
188 0 : Image aImage;
189 0 : String sThesImplName( aHelper.GetThesImplName( aLocale ) );
190 0 : ::rtl::OUString aSynonymsImageUrl( aCfg.GetSynonymsContextImage( sThesImplName ) );
191 0 : if (sThesImplName.Len() > 0 && !aSynonymsImageUrl.isEmpty())
192 0 : aImage = Image( lcl_GetImageFromPngUrl( aSynonymsImageUrl ) );
193 :
194 0 : for (sal_uInt16 i = 0; (size_t)i < nNumSynonyms; ++i)
195 : {
196 : //! item ids should start with values > 0, since 0 has special meaning
197 0 : const sal_uInt16 nId = i + 1;
198 :
199 0 : String aItemText( linguistic::GetThesaurusReplaceText( aSynonyms[i] ) );
200 0 : pThesSubMenu->InsertItem( nId, aItemText );
201 0 : ::rtl::OUString aCmd(".uno:ThesaurusFromContext?WordReplace:string=" );
202 0 : aCmd += aItemText;
203 0 : pThesSubMenu->SetItemCommand( nId, aCmd );
204 :
205 0 : if (!aSynonymsImageUrl.isEmpty())
206 0 : pThesSubMenu->SetItemImage( nId, aImage );
207 0 : }
208 : }
209 : else // nNumSynonyms == 0
210 : {
211 0 : const rtl::OUString aItemText( SfxResId(STR_MENU_NO_SYNONYM_FOUND).toString() );
212 0 : pThesSubMenu->InsertItem( 1, aItemText, MIB_NOSELECT );
213 : }
214 0 : pThesSubMenu->InsertSeparator();
215 0 : const rtl::OUString sThesaurus( SfxResId(STR_MENU_THESAURUS).toString() );
216 0 : pThesSubMenu->InsertItem( 100, sThesaurus );
217 0 : pThesSubMenu->SetItemCommand( 100, ::rtl::OUString(".uno:ThesaurusDialog") );
218 :
219 0 : pSVMenu->InsertSeparator();
220 0 : const rtl::OUString sSynonyms( SfxResId(STR_MENU_SYNONYMS).toString() );
221 0 : pSVMenu->InsertItem( SID_THES, sSynonyms );
222 0 : pSVMenu->SetPopupMenu( SID_THES, pThesSubMenu );
223 : }
224 :
225 0 : return pThesSubMenu;
226 : }
227 :
228 : // ------------------------------------------------------------------------
229 :
230 : // executes the function for the selected item
231 0 : IMPL_LINK( SfxMenuManager, Select, Menu *, pSelMenu )
232 : {
233 0 : sal_uInt16 nId = (sal_uInt16) pSelMenu->GetCurItemId();
234 0 : String aCommand = pSelMenu->GetItemCommand( nId );
235 0 : if ( !aCommand.Len() && pBindings )
236 : {
237 0 : const SfxSlot* pSlot = SfxSlotPool::GetSlotPool( pBindings->GetDispatcher()->GetFrame() ).GetSlot( nId );
238 0 : if ( pSlot && pSlot->pUnoName )
239 : {
240 0 : aCommand = DEFINE_CONST_UNICODE(".uno:");
241 0 : aCommand += rtl::OUString::createFromAscii( pSlot->GetUnoName() );
242 : }
243 : }
244 :
245 0 : if ( aCommand.Len() )
246 : {
247 0 : pBindings->ExecuteCommand_Impl( aCommand );
248 : }
249 0 : else if ( pBindings->IsBound(nId) )
250 : // normal function
251 0 : pBindings->Execute( nId );
252 : else
253 : // special menu function
254 0 : pBindings->GetDispatcher_Impl()->Execute( nId );
255 :
256 0 : return sal_True;
257 : }
258 :
259 0 : SfxPopupMenuManager::~SfxPopupMenuManager()
260 : {
261 0 : }
262 :
263 : //-------------------------------------------------------------------------
264 :
265 0 : void SfxPopupMenuManager::RemoveDisabledEntries()
266 : {
267 0 : if ( pSVMenu )
268 0 : TryToHideDisabledEntries_Impl( pSVMenu );
269 0 : }
270 :
271 : //--------------------------------------------------------------------
272 :
273 0 : sal_uInt16 SfxPopupMenuManager::Execute( const Point& rPos, Window* pWindow )
274 : {
275 0 : sal_uInt16 nVal = ( (PopupMenu*) GetMenu()->GetSVMenu() )->Execute( pWindow, rPos );
276 0 : delete pStaticThesSubMenu; pStaticThesSubMenu = NULL;
277 0 : return nVal;
278 : }
279 :
280 : //-------------------------------------------------------------------------
281 :
282 0 : SfxMenuManager::SfxMenuManager( Menu* pMenuArg, SfxBindings &rBindings )
283 : : pMenu(0),
284 : pOldMenu(0),
285 : pBindings(&rBindings),
286 : pResMgr(NULL),
287 0 : nType(0)
288 : {
289 0 : bAddClipboardFuncs = sal_False;
290 0 : SfxVirtualMenu* pVMenu = new SfxVirtualMenu( pMenuArg, sal_False, rBindings, sal_True, sal_True );
291 0 : Construct(*pVMenu);
292 0 : }
293 :
294 0 : SfxPopupMenuManager::SfxPopupMenuManager( PopupMenu* pMenuArg, SfxBindings& rBindings )
295 : : SfxMenuManager( pMenuArg, rBindings )
296 0 : , pSVMenu( pMenuArg )
297 : {
298 0 : }
299 :
300 0 : SfxPopupMenuManager* SfxPopupMenuManager::Popup( const ResId& rResId, SfxViewFrame* pFrame,const Point& rPoint, Window* pWindow )
301 : {
302 0 : PopupMenu *pSVMenu = new PopupMenu( rResId );
303 0 : sal_uInt16 n, nCount = pSVMenu->GetItemCount();
304 0 : for ( n=0; n<nCount; n++ )
305 : {
306 0 : sal_uInt16 nId = pSVMenu->GetItemId( n );
307 0 : if ( nId == SID_COPY || nId == SID_CUT || nId == SID_PASTE )
308 0 : break;
309 : }
310 :
311 0 : PopupMenu* pThesSubMenu = InsertThesaurusSubmenu_Impl( &pFrame->GetBindings(), pSVMenu );
312 : // #i107205# (see comment in header file)
313 0 : pStaticThesSubMenu = pThesSubMenu;
314 :
315 0 : if ( n == nCount )
316 : {
317 0 : PopupMenu aPop( SfxResId( MN_CLIPBOARDFUNCS ) );
318 0 : nCount = aPop.GetItemCount();
319 0 : pSVMenu->InsertSeparator();
320 0 : for ( n=0; n<nCount; n++ )
321 : {
322 0 : sal_uInt16 nId = aPop.GetItemId( n );
323 0 : pSVMenu->InsertItem( nId, aPop.GetItemText( nId ), aPop.GetItemBits( nId ) );
324 0 : pSVMenu->SetHelpId( nId, aPop.GetHelpId( nId ));
325 0 : }
326 : }
327 :
328 0 : InsertVerbs_Impl( &pFrame->GetBindings(), pFrame->GetViewShell()->GetVerbs(), pSVMenu );
329 0 : Menu* pMenu = NULL;
330 0 : ::com::sun::star::ui::ContextMenuExecuteEvent aEvent;
331 0 : aEvent.SourceWindow = VCLUnoHelper::GetInterface( pWindow );
332 0 : aEvent.ExecutePosition.X = rPoint.X();
333 0 : aEvent.ExecutePosition.Y = rPoint.Y();
334 0 : ::rtl::OUString sDummyMenuName;
335 0 : if ( pFrame->GetViewShell()->TryContextMenuInterception( *pSVMenu, sDummyMenuName, pMenu, aEvent ) )
336 : {
337 0 : if ( pMenu )
338 : {
339 0 : delete pSVMenu;
340 0 : pSVMenu = (PopupMenu*) pMenu;
341 : }
342 :
343 0 : SfxPopupMenuManager* aMgr = new SfxPopupMenuManager( pSVMenu, pFrame->GetBindings());
344 0 : aMgr->RemoveDisabledEntries();
345 0 : return aMgr;
346 : }
347 :
348 0 : return 0;
349 : }
350 :
351 :
352 0 : void SfxPopupMenuManager::ExecutePopup( const ResId& rResId, SfxViewFrame* pFrame, const Point& rPoint, Window* pWindow )
353 : {
354 0 : PopupMenu *pSVMenu = new PopupMenu( rResId );
355 0 : sal_uInt16 n, nCount = pSVMenu->GetItemCount();
356 0 : for ( n=0; n<nCount; n++ )
357 : {
358 0 : sal_uInt16 nId = pSVMenu->GetItemId( n );
359 0 : if ( nId == SID_COPY || nId == SID_CUT || nId == SID_PASTE )
360 0 : break;
361 : }
362 :
363 0 : PopupMenu* pThesSubMenu = InsertThesaurusSubmenu_Impl( &pFrame->GetBindings(), pSVMenu );
364 :
365 0 : if ( n == nCount )
366 : {
367 0 : PopupMenu aPop( SfxResId( MN_CLIPBOARDFUNCS ) );
368 0 : nCount = aPop.GetItemCount();
369 0 : pSVMenu->InsertSeparator();
370 0 : for ( n=0; n<nCount; n++ )
371 : {
372 0 : sal_uInt16 nId = aPop.GetItemId( n );
373 0 : pSVMenu->InsertItem( nId, aPop.GetItemText( nId ), aPop.GetItemBits( nId ) );
374 0 : pSVMenu->SetHelpId( nId, aPop.GetHelpId( nId ));
375 0 : }
376 : }
377 :
378 0 : InsertVerbs_Impl( &pFrame->GetBindings(), pFrame->GetViewShell()->GetVerbs(), pSVMenu );
379 0 : Menu* pMenu = NULL;
380 0 : ::com::sun::star::ui::ContextMenuExecuteEvent aEvent;
381 0 : aEvent.SourceWindow = VCLUnoHelper::GetInterface( pWindow );
382 0 : aEvent.ExecutePosition.X = rPoint.X();
383 0 : aEvent.ExecutePosition.Y = rPoint.Y();
384 0 : ::rtl::OUString sDummyMenuName;
385 0 : if ( pFrame->GetViewShell()->TryContextMenuInterception( *pSVMenu, sDummyMenuName, pMenu, aEvent ) )
386 : {
387 0 : if ( pMenu )
388 : {
389 0 : delete pSVMenu;
390 0 : pSVMenu = (PopupMenu*) pMenu;
391 : }
392 :
393 0 : SfxPopupMenuManager aPop( pSVMenu, pFrame->GetBindings() );
394 0 : aPop.RemoveDisabledEntries();
395 0 : aPop.Execute( rPoint, pWindow );
396 :
397 : // #i112646 avoid crash when context menu is closed.
398 : // the (manually inserted) sub-menu needs to be destroyed before
399 : // aPop gets destroyed.
400 0 : delete pThesSubMenu;
401 0 : pThesSubMenu = 0;
402 : }
403 :
404 0 : delete pThesSubMenu;
405 0 : }
406 :
407 0 : Menu* SfxPopupMenuManager::GetSVMenu()
408 : {
409 0 : return (Menu*) GetMenu()->GetSVMenu();
410 : }
411 :
412 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|