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>
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() )
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 0 : PopupMenu* InsertThesaurusSubmenu_Impl( SfxBindings* pBindings, Menu* pSVMenu )
132 : {
133 :
134 : // build thesaurus sub menu if look-up string is available
135 :
136 0 : PopupMenu* pThesSubMenu = 0;
137 0 : SfxPoolItem *pItem = 0;
138 0 : pBindings->QueryState( SID_THES, pItem );
139 0 : OUString aThesLookUpStr;
140 0 : SfxStringItem *pStrItem = dynamic_cast< SfxStringItem * >(pItem);
141 0 : sal_Int32 nDelimPos = -1;
142 0 : if (pStrItem)
143 : {
144 0 : aThesLookUpStr = pStrItem->GetValue();
145 0 : nDelimPos = aThesLookUpStr.lastIndexOf( '#' );
146 : }
147 0 : delete pItem;
148 0 : if ( !aThesLookUpStr.isEmpty() && nDelimPos != -1 )
149 : {
150 : // get synonym list for sub menu
151 0 : std::vector< OUString > aSynonyms;
152 0 : SfxThesSubMenuHelper aHelper;
153 0 : OUString aText( aHelper.GetText( aThesLookUpStr, nDelimPos ) );
154 0 : lang::Locale aLocale;
155 0 : aHelper.GetLocale( aLocale, aThesLookUpStr, nDelimPos );
156 0 : const bool bHasMoreSynonyms = aHelper.GetMeanings( aSynonyms, aText, aLocale, 7 /*max number of synonyms to retrieve*/ );
157 : (void) bHasMoreSynonyms;
158 :
159 0 : pThesSubMenu = new PopupMenu;
160 0 : pThesSubMenu->SetMenuFlags(MENU_FLAG_NOAUTOMNEMONICS);
161 0 : const size_t nNumSynonyms = aSynonyms.size();
162 0 : if (nNumSynonyms > 0)
163 : {
164 0 : SvtLinguConfig aCfg;
165 :
166 0 : Image aImage;
167 0 : OUString sThesImplName( aHelper.GetThesImplName( aLocale ) );
168 0 : OUString aSynonymsImageUrl( aCfg.GetSynonymsContextImage( sThesImplName ) );
169 0 : if (!sThesImplName.isEmpty() && !aSynonymsImageUrl.isEmpty())
170 0 : aImage = Image( aSynonymsImageUrl );
171 :
172 0 : for (sal_uInt16 i = 0; (size_t)i < nNumSynonyms; ++i)
173 : {
174 : //! item ids should start with values > 0, since 0 has special meaning
175 0 : const sal_uInt16 nId = i + 1;
176 :
177 0 : OUString aItemText( linguistic::GetThesaurusReplaceText( aSynonyms[i] ) );
178 0 : pThesSubMenu->InsertItem( nId, aItemText );
179 0 : OUString aCmd(".uno:ThesaurusFromContext?WordReplace:string=" );
180 0 : aCmd += aItemText;
181 0 : pThesSubMenu->SetItemCommand( nId, aCmd );
182 :
183 0 : if (!aSynonymsImageUrl.isEmpty())
184 0 : pThesSubMenu->SetItemImage( nId, aImage );
185 0 : }
186 : }
187 : else // nNumSynonyms == 0
188 : {
189 0 : const OUString aItemText( SfxResId(STR_MENU_NO_SYNONYM_FOUND).toString() );
190 0 : pThesSubMenu->InsertItem( 1, aItemText, MIB_NOSELECT );
191 : }
192 0 : pThesSubMenu->InsertSeparator();
193 0 : const OUString sThesaurus( SfxResId(STR_MENU_THESAURUS).toString() );
194 0 : pThesSubMenu->InsertItem( 100, sThesaurus );
195 0 : pThesSubMenu->SetItemCommand( 100, OUString(".uno:ThesaurusDialog") );
196 :
197 0 : pSVMenu->InsertSeparator();
198 0 : const OUString sSynonyms( SfxResId(STR_MENU_SYNONYMS).toString() );
199 0 : pSVMenu->InsertItem( SID_THES, sSynonyms );
200 0 : pSVMenu->SetPopupMenu( SID_THES, pThesSubMenu );
201 : }
202 :
203 0 : return pThesSubMenu;
204 : }
205 :
206 :
207 :
208 : // executes the function for the selected item
209 0 : IMPL_LINK( SfxMenuManager, Select, Menu *, pSelMenu )
210 : {
211 0 : sal_uInt16 nId = (sal_uInt16) pSelMenu->GetCurItemId();
212 0 : OUString aCommand = pSelMenu->GetItemCommand( nId );
213 0 : if ( aCommand.isEmpty() && pBindings )
214 : {
215 0 : const SfxSlot* pSlot = SfxSlotPool::GetSlotPool( pBindings->GetDispatcher()->GetFrame() ).GetSlot( nId );
216 0 : if ( pSlot && pSlot->pUnoName )
217 : {
218 0 : aCommand = ".uno:" + OUString::createFromAscii( pSlot->GetUnoName() );
219 : }
220 : }
221 :
222 0 : if (!aCommand.isEmpty() && pBindings)
223 : {
224 0 : pBindings->ExecuteCommand_Impl( aCommand );
225 0 : return sal_True;
226 : }
227 :
228 0 : if (!pBindings)
229 0 : return sal_True;
230 :
231 0 : if ( pBindings->IsBound(nId) )
232 : // normal function
233 0 : pBindings->Execute( nId );
234 : else
235 : // special menu function
236 0 : pBindings->GetDispatcher_Impl()->Execute( nId );
237 :
238 0 : return sal_True;
239 : }
240 :
241 0 : SfxPopupMenuManager::~SfxPopupMenuManager()
242 : {
243 0 : }
244 :
245 :
246 :
247 0 : void SfxPopupMenuManager::RemoveDisabledEntries()
248 : {
249 0 : if ( pSVMenu )
250 0 : TryToHideDisabledEntries_Impl( pSVMenu );
251 0 : }
252 :
253 :
254 :
255 0 : sal_uInt16 SfxPopupMenuManager::Execute( const Point& rPos, Window* pWindow )
256 : {
257 0 : sal_uInt16 nVal = ( (PopupMenu*) GetMenu()->GetSVMenu() )->Execute( pWindow, rPos );
258 0 : delete pStaticThesSubMenu; pStaticThesSubMenu = NULL;
259 0 : return nVal;
260 : }
261 :
262 :
263 :
264 0 : SfxMenuManager::SfxMenuManager( Menu* pMenuArg, SfxBindings &rBindings )
265 : : pMenu(0),
266 : pOldMenu(0),
267 : pBindings(&rBindings),
268 : pResMgr(NULL),
269 0 : nType(0)
270 : {
271 0 : bAddClipboardFuncs = false;
272 0 : SfxVirtualMenu* pVMenu = new SfxVirtualMenu( pMenuArg, false, rBindings, true, true );
273 0 : Construct(*pVMenu);
274 0 : }
275 :
276 0 : SfxPopupMenuManager::SfxPopupMenuManager( PopupMenu* pMenuArg, SfxBindings& rBindings )
277 : : SfxMenuManager( pMenuArg, rBindings )
278 0 : , pSVMenu( pMenuArg )
279 : {
280 0 : }
281 :
282 0 : SfxPopupMenuManager* SfxPopupMenuManager::Popup( const ResId& rResId, SfxViewFrame* pFrame,const Point& rPoint, Window* pWindow )
283 : {
284 0 : PopupMenu *pSVMenu = new PopupMenu( rResId );
285 0 : sal_uInt16 n, nCount = pSVMenu->GetItemCount();
286 0 : for ( n=0; n<nCount; n++ )
287 : {
288 0 : sal_uInt16 nId = pSVMenu->GetItemId( n );
289 0 : if ( nId == SID_COPY || nId == SID_CUT || nId == SID_PASTE )
290 : break;
291 : }
292 :
293 0 : PopupMenu* pThesSubMenu = InsertThesaurusSubmenu_Impl( &pFrame->GetBindings(), pSVMenu );
294 : // #i107205# (see comment in header file)
295 0 : pStaticThesSubMenu = pThesSubMenu;
296 :
297 0 : if ( n == nCount )
298 : {
299 0 : PopupMenu aPop( SfxResId( MN_CLIPBOARDFUNCS ) );
300 0 : nCount = aPop.GetItemCount();
301 0 : pSVMenu->InsertSeparator();
302 0 : for ( n=0; n<nCount; n++ )
303 : {
304 0 : sal_uInt16 nId = aPop.GetItemId( n );
305 0 : pSVMenu->InsertItem( nId, aPop.GetItemText( nId ), aPop.GetItemBits( nId ) );
306 0 : pSVMenu->SetHelpId( nId, aPop.GetHelpId( nId ));
307 0 : }
308 : }
309 :
310 0 : InsertVerbs_Impl( &pFrame->GetBindings(), pFrame->GetViewShell()->GetVerbs(), pSVMenu );
311 0 : Menu* pMenu = NULL;
312 0 : ::com::sun::star::ui::ContextMenuExecuteEvent aEvent;
313 0 : aEvent.SourceWindow = VCLUnoHelper::GetInterface( pWindow );
314 0 : aEvent.ExecutePosition.X = rPoint.X();
315 0 : aEvent.ExecutePosition.Y = rPoint.Y();
316 0 : OUString sDummyMenuName;
317 0 : if ( pFrame->GetViewShell()->TryContextMenuInterception( *pSVMenu, sDummyMenuName, pMenu, aEvent ) )
318 : {
319 0 : if ( pMenu )
320 : {
321 0 : delete pSVMenu;
322 0 : pSVMenu = (PopupMenu*) pMenu;
323 : }
324 :
325 0 : SfxPopupMenuManager* aMgr = new SfxPopupMenuManager( pSVMenu, pFrame->GetBindings());
326 0 : aMgr->RemoveDisabledEntries();
327 0 : return aMgr;
328 : }
329 :
330 0 : return 0;
331 : }
332 :
333 :
334 0 : void SfxPopupMenuManager::ExecutePopup( const ResId& rResId, SfxViewFrame* pFrame, const Point& rPoint, Window* pWindow )
335 : {
336 0 : PopupMenu *pSVMenu = new PopupMenu( rResId );
337 0 : sal_uInt16 n, nCount = pSVMenu->GetItemCount();
338 0 : for ( n=0; n<nCount; n++ )
339 : {
340 0 : sal_uInt16 nId = pSVMenu->GetItemId( n );
341 0 : if ( nId == SID_COPY || nId == SID_CUT || nId == SID_PASTE )
342 : break;
343 : }
344 :
345 0 : PopupMenu* pThesSubMenu = InsertThesaurusSubmenu_Impl( &pFrame->GetBindings(), pSVMenu );
346 :
347 0 : if ( n == nCount )
348 : {
349 0 : PopupMenu aPop( SfxResId( MN_CLIPBOARDFUNCS ) );
350 0 : nCount = aPop.GetItemCount();
351 0 : pSVMenu->InsertSeparator();
352 0 : for ( n=0; n<nCount; n++ )
353 : {
354 0 : sal_uInt16 nId = aPop.GetItemId( n );
355 0 : pSVMenu->InsertItem( nId, aPop.GetItemText( nId ), aPop.GetItemBits( nId ) );
356 0 : pSVMenu->SetHelpId( nId, aPop.GetHelpId( nId ));
357 0 : }
358 : }
359 :
360 0 : InsertVerbs_Impl( &pFrame->GetBindings(), pFrame->GetViewShell()->GetVerbs(), pSVMenu );
361 0 : Menu* pMenu = NULL;
362 0 : ::com::sun::star::ui::ContextMenuExecuteEvent aEvent;
363 0 : aEvent.SourceWindow = VCLUnoHelper::GetInterface( pWindow );
364 0 : aEvent.ExecutePosition.X = rPoint.X();
365 0 : aEvent.ExecutePosition.Y = rPoint.Y();
366 0 : OUString sDummyMenuName;
367 0 : if ( pFrame->GetViewShell()->TryContextMenuInterception( *pSVMenu, sDummyMenuName, pMenu, aEvent ) )
368 : {
369 0 : if ( pMenu )
370 : {
371 0 : delete pSVMenu;
372 0 : pSVMenu = (PopupMenu*) pMenu;
373 : }
374 :
375 0 : SfxPopupMenuManager aPop( pSVMenu, pFrame->GetBindings() );
376 0 : aPop.RemoveDisabledEntries();
377 0 : aPop.Execute( rPoint, pWindow );
378 :
379 : // #i112646 avoid crash when context menu is closed.
380 : // the (manually inserted) sub-menu needs to be destroyed before
381 : // aPop gets destroyed.
382 0 : delete pThesSubMenu;
383 0 : pThesSubMenu = 0;
384 : }
385 :
386 0 : delete pThesSubMenu;
387 0 : }
388 :
389 0 : Menu* SfxPopupMenuManager::GetSVMenu()
390 : {
391 0 : return (Menu*) GetMenu()->GetSVMenu();
392 : }
393 :
394 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|