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 :
21 : #ifdef SOLARIS
22 : #include <ctime>
23 : #endif
24 :
25 : #include <string>
26 : #include <com/sun/star/uno/Reference.h>
27 : #include <com/sun/star/beans/PropertyValue.hpp>
28 : #include <com/sun/star/frame/Desktop.hpp>
29 : #include <com/sun/star/frame/XDispatch.hpp>
30 : #include <com/sun/star/frame/XDispatchProvider.hpp>
31 : #include <com/sun/star/frame/XFrame.hpp>
32 : #include <com/sun/star/util/URL.hpp>
33 : #include <com/sun/star/util/URLTransformer.hpp>
34 : #include <com/sun/star/util/XURLTransformer.hpp>
35 : #include <comphelper/processfactory.hxx>
36 : #include <tools/urlobj.hxx>
37 : #include <svl/eitem.hxx>
38 : #include <svl/stritem.hxx>
39 : #include <svtools/imagemgr.hxx>
40 : #include <svtools/menuoptions.hxx>
41 : #include <framework/menuconfiguration.hxx>
42 :
43 : #include <sfx2/app.hxx>
44 : #include <sfx2/sfx.hrc>
45 : #include <sfx2/msgpool.hxx>
46 : #include <sfx2/msg.hxx>
47 : #include <sfx2/bindings.hxx>
48 : #include <sfx2/dispatch.hxx>
49 : #include "idpool.hxx"
50 : #include "sfxtypes.hxx"
51 : #include "virtmenu.hxx"
52 : #include <sfx2/mnuitem.hxx>
53 : #include <sfx2/tbxctrl.hxx>
54 : #include <sfx2/module.hxx>
55 : #include <sfx2/unoctitm.hxx>
56 : #include <sfx2/viewfrm.hxx>
57 : #include <sfx2/imgmgr.hxx>
58 : #include <sfx2/imagemgr.hxx>
59 : #include <sfx2/sfxresid.hxx>
60 : #include "../doc/doc.hrc"
61 : #include <vcl/settings.hxx>
62 :
63 : using namespace ::com::sun::star::uno;
64 : using namespace ::com::sun::star::frame;
65 : using namespace ::com::sun::star::beans;
66 : using namespace ::com::sun::star::util;
67 :
68 :
69 :
70 : // binds the instance to the specified id and assignes the title
71 :
72 0 : void SfxMenuControl::Bind(
73 : SfxVirtualMenu* pOwn,
74 : sal_uInt16 nSlotId,
75 : const OUString& rTitle,
76 : SfxBindings &rBindings )
77 : {
78 0 : aTitle = rTitle;
79 0 : pOwnMenu = pOwn;
80 0 : pSubMenu = 0;
81 0 : if ( pOwn )
82 0 : SfxControllerItem::Bind(nSlotId, &rBindings);
83 : else
84 0 : SetId( nSlotId );
85 :
86 : #ifdef DBG_UTIL
87 : CheckConfigure_Impl(SFX_SLOT_MENUCONFIG);
88 : #endif
89 0 : }
90 :
91 :
92 :
93 :
94 : // binds the item to the specified menu and assignes the title
95 :
96 0 : void SfxMenuControl::Bind(
97 : SfxVirtualMenu* pOwn,
98 : sal_uInt16 nSlotId,
99 : SfxVirtualMenu& rMenu,
100 : const OUString& rTitle,
101 : SfxBindings &rBindings )
102 : {
103 0 : SetId( nSlotId );
104 0 : SetBindings(rBindings);
105 0 : pOwnMenu = pOwn;
106 0 : pSubMenu = &rMenu;
107 0 : aTitle = rTitle;
108 0 : }
109 :
110 :
111 :
112 : // Constructor for explicit registration
113 :
114 0 : SfxMenuControl::SfxMenuControl( bool bShowStrings )
115 : : pOwnMenu(0),
116 : pSubMenu(0),
117 0 : b_ShowStrings(bShowStrings)
118 : {
119 0 : }
120 :
121 :
122 :
123 : // Constructor for array
124 0 : SfxMenuControl::SfxMenuControl():
125 : pOwnMenu(0),
126 : pSubMenu(0),
127 0 : b_ShowStrings(false)
128 : {
129 0 : }
130 :
131 :
132 :
133 0 : SfxMenuControl::SfxMenuControl(sal_uInt16 nSlotId, SfxBindings& rBindings):
134 : SfxControllerItem(nSlotId, rBindings),
135 : pOwnMenu(0),
136 : pSubMenu(0),
137 0 : b_ShowStrings(false)
138 : {
139 : // This constructor should make it possible already during the design
140 : // to fall back to the bindings, but can as always be bound later.
141 : // The usefulness of this is for example if a StatusForwarder should
142 : // be created in the constructor of a derived class.
143 0 : UnBind();
144 0 : }
145 :
146 :
147 :
148 :
149 0 : SfxMenuControl::~SfxMenuControl()
150 : {
151 0 : delete pSubMenu;
152 0 : }
153 :
154 :
155 :
156 : // changes the state in the virtual menu
157 :
158 0 : void SfxMenuControl::StateChanged
159 : (
160 : sal_uInt16 nSID,
161 : SfxItemState eState,
162 : const SfxPoolItem* pState
163 : )
164 : {
165 : (void)nSID;
166 : DBG_ASSERT( nSID == GetId(), "strange SID" );
167 : DBG_ASSERT( pOwnMenu != 0, "setting state to dangling SfxMenuControl" );
168 :
169 : bool bIsObjMenu =
170 0 : GetId() >= SID_OBJECTMENU0 && GetId() < SID_OBJECTMENU_LAST;
171 :
172 : // Fix inclusion of enabled/disabled-Flag
173 :
174 : #ifdef UNIX
175 0 : if (nSID == SID_PASTE)
176 0 : pOwnMenu->EnableItem( GetId(), true );
177 : else
178 : #endif
179 0 : pOwnMenu->EnableItem( GetId(), bIsObjMenu
180 0 : ? 0 != pOwnMenu->GetSVMenu()->GetPopupMenu( GetId() )
181 0 : : eState != SfxItemState::DISABLED );
182 :
183 0 : if ( eState != SfxItemState::DEFAULT )
184 : {
185 : // check only for non-Object Menus
186 0 : if ( !bIsObjMenu )
187 0 : pOwnMenu->CheckItem( GetId(), false );
188 :
189 0 : if ( pOwnMenu->GetSVMenu()->GetItemText( GetId() ) != GetTitle() )
190 : {
191 : DBG_WARNING("Title of menu item changed - please check if this needs correction!");
192 : }
193 0 : return;
194 : }
195 :
196 0 : bool bCheck = false;
197 0 : if ( pState->ISA(SfxBoolItem) )
198 : {
199 : // BoolItem for check
200 : DBG_ASSERT( GetId() < SID_OBJECTMENU0 || GetId() > SID_OBJECTMENU_LAST,
201 : "SfxBoolItem not allowed for SID_OBJECTMENUx" );
202 0 : bCheck = static_cast<const SfxBoolItem*>(pState)->GetValue();
203 0 : Menu* pMenu = pOwnMenu->GetSVMenu();
204 0 : pMenu->SetItemBits( GetId() , pMenu->GetItemBits( GetId() ) | MenuItemBits::CHECKABLE);
205 : }
206 0 : else if ( pState->ISA(SfxEnumItemInterface) &&
207 0 : static_cast<const SfxEnumItemInterface *>(pState)->HasBoolValue() )
208 : {
209 : // Treat EnumItem as Bool
210 : DBG_ASSERT( GetId() < SID_OBJECTMENU0 || GetId() > SID_OBJECTMENU_LAST,
211 : "SfxEnumItem not allowed for SID_OBJECTMENUx" );
212 0 : bCheck = static_cast<const SfxEnumItemInterface *>(pState)->GetBoolValue();
213 0 : Menu* pMenu = pOwnMenu->GetSVMenu();
214 0 : pMenu->SetItemBits( GetId() , pMenu->GetItemBits( GetId() ) | MenuItemBits::CHECKABLE);
215 : }
216 0 : else if ( ( b_ShowStrings || bIsObjMenu ) && pState->ISA(SfxStringItem) )
217 : {
218 : // Get MenuText from SfxStringItem
219 0 : OUString aStr( static_cast<const SfxStringItem*>(pState)->GetValue() );
220 0 : if ( aStr.startsWith("($1)") )
221 : {
222 0 : OUString aEntry(SfxResId(STR_UPDATEDOC).toString());
223 0 : aEntry += " ";
224 0 : aEntry += aStr.copy(4);
225 0 : aStr = aEntry;
226 : }
227 0 : else if ( aStr.startsWith("($2)") )
228 : {
229 0 : OUString aEntry(SfxResId(STR_CLOSEDOC_ANDRETURN).toString());
230 0 : aEntry += aStr.copy(4);
231 0 : aStr = aEntry;
232 : }
233 :
234 0 : pOwnMenu->SetItemText( GetId(), aStr );
235 : }
236 :
237 0 : pOwnMenu->CheckItem( GetId(), bCheck );
238 : }
239 :
240 :
241 :
242 0 : SfxMenuControl* SfxMenuControl::CreateImpl( sal_uInt16 /*nId*/, Menu& /*rMenu*/, SfxBindings& /*rBindings*/ )
243 : {
244 0 : return new SfxMenuControl( true );
245 : }
246 :
247 108 : void SfxMenuControl::RegisterControl( sal_uInt16 nSlotId, SfxModule *pMod )
248 : {
249 : RegisterMenuControl( pMod, new SfxMenuCtrlFactory(
250 108 : SfxMenuControl::CreateImpl, TYPE(SfxStringItem), nSlotId ) );
251 108 : }
252 :
253 :
254 766 : void SfxMenuControl::RegisterMenuControl(SfxModule* pMod, SfxMenuCtrlFactory* pFact)
255 : {
256 766 : SfxGetpApp()->RegisterMenuControl_Impl( pMod, pFact );
257 766 : }
258 :
259 0 : SfxMenuControl* SfxMenuControl::CreateControl( sal_uInt16 nId, Menu &rMenu, SfxBindings &rBindings )
260 : {
261 0 : TypeId aSlotType = SFX_SLOTPOOL().GetSlotType(nId);
262 0 : if ( aSlotType )
263 : {
264 0 : SfxApplication *pApp = SfxGetpApp();
265 0 : SfxDispatcher *pDisp = rBindings.GetDispatcher_Impl();
266 0 : SfxModule *pMod = pDisp ? SfxModule::GetActiveModule( pDisp->GetFrame() ) :0;
267 0 : if ( pMod )
268 : {
269 0 : SfxMenuCtrlFactArr_Impl *pFactories = pMod->GetMenuCtrlFactories_Impl();
270 0 : if ( pFactories )
271 : {
272 0 : SfxMenuCtrlFactArr_Impl &rFactories = *pFactories;
273 0 : for ( sal_uInt16 nFactory = 0; nFactory < rFactories.size(); ++nFactory )
274 0 : if ( rFactories[nFactory].nTypeId == aSlotType &&
275 0 : ( ( rFactories[nFactory].nSlotId == 0 ) ||
276 0 : ( rFactories[nFactory].nSlotId == nId) ) )
277 0 : return rFactories[nFactory].pCtor( nId, rMenu, rBindings );
278 : }
279 : }
280 :
281 0 : SfxMenuCtrlFactArr_Impl &rFactories = pApp->GetMenuCtrlFactories_Impl();
282 :
283 0 : for ( sal_uInt16 nFactory = 0; nFactory < rFactories.size(); ++nFactory )
284 0 : if ( rFactories[nFactory].nTypeId == aSlotType &&
285 0 : ( ( rFactories[nFactory].nSlotId == 0 ) ||
286 0 : ( rFactories[nFactory].nSlotId == nId) ) )
287 0 : return rFactories[nFactory].pCtor( nId, rMenu, rBindings );
288 : }
289 0 : return 0;
290 : }
291 :
292 :
293 :
294 0 : PopupMenu* SfxMenuControl::GetPopup () const
295 : {
296 0 : if (GetPopupMenu())
297 0 : return static_cast<PopupMenu*>(GetPopupMenu()->GetSVMenu());
298 : else
299 0 : return 0;
300 : }
301 :
302 : long Select_Impl( void* pHdl, void* pVoid );
303 :
304 0 : SFX_IMPL_MENU_CONTROL( SfxAppMenuControl_Impl, SfxStringItem );
305 :
306 0 : SfxAppMenuControl_Impl::SfxAppMenuControl_Impl(
307 : sal_uInt16 nPos, Menu& rMenu, SfxBindings& rBindings )
308 0 : : SfxMenuControl( nPos, rBindings ), pMenu(0)
309 : {
310 : // Determine the current background color setting for menus
311 0 : const StyleSettings& rSettings = Application::GetSettings().GetStyleSettings();
312 0 : m_sIconTheme = rSettings.DetermineIconTheme();
313 0 : m_bShowMenuImages = rSettings.GetUseImagesInMenus();
314 :
315 0 : ::framework::MenuConfiguration aConf( ::comphelper::getProcessComponentContext() );
316 0 : Reference<com::sun::star::frame::XFrame> aXFrame( GetBindings().GetDispatcher_Impl()->GetFrame()->GetFrame().GetFrameInterface() );
317 0 : pMenu = aConf.CreateBookmarkMenu( aXFrame, GetId() == SID_NEWDOCDIRECT ? BOOKMARK_NEWMENU : BOOKMARK_WIZARDMENU );
318 0 : if( pMenu )
319 : {
320 0 : pMenu->SetSelectHdl( Link( &(this->GetBindings()), Select_Impl ) );
321 0 : pMenu->SetActivateHdl( LINK(this, SfxAppMenuControl_Impl, Activate) );
322 0 : rMenu.SetPopupMenu( nPos, pMenu );
323 0 : }
324 0 : }
325 :
326 0 : SfxAppMenuControl_Impl::~SfxAppMenuControl_Impl()
327 : {
328 0 : delete pMenu;
329 0 : }
330 :
331 0 : IMPL_LINK( SfxAppMenuControl_Impl, Activate, Menu *, pActMenu )
332 : {
333 0 : if ( pActMenu )
334 : {
335 0 : const StyleSettings& rSettings = Application::GetSettings().GetStyleSettings();
336 0 : OUString sIconTheme = rSettings.DetermineIconTheme();
337 0 : bool bShowMenuImages = rSettings.GetUseImagesInMenus();
338 :
339 0 : if (( sIconTheme != m_sIconTheme ) ||
340 0 : ( bShowMenuImages != m_bShowMenuImages ))
341 : {
342 0 : m_sIconTheme = sIconTheme;
343 0 : m_bShowMenuImages = bShowMenuImages;
344 :
345 0 : sal_uInt16 nCount = pActMenu->GetItemCount();
346 0 : for ( sal_uInt16 nSVPos = 0; nSVPos < nCount; nSVPos++ )
347 : {
348 0 : sal_uInt16 nItemId = pActMenu->GetItemId( nSVPos );
349 0 : if ( pActMenu->GetItemType( nSVPos ) != MenuItemType::SEPARATOR )
350 : {
351 0 : if ( bShowMenuImages )
352 : {
353 0 : bool bImageSet = false;
354 0 : OUString aImageId;
355 : ::framework::MenuConfiguration::Attributes* pMenuAttributes =
356 0 : reinterpret_cast< ::framework::MenuConfiguration::Attributes*>(pMenu->GetUserValue( nItemId ));
357 :
358 0 : if ( pMenuAttributes )
359 0 : aImageId = pMenuAttributes->aImageId; // Retrieve image id from menu attributes
360 :
361 0 : if ( aImageId.getLength() > 0 )
362 : {
363 0 : Reference< ::com::sun::star::frame::XFrame > xFrame;
364 0 : Image aImage = GetImage( xFrame, aImageId, false );
365 0 : if ( !!aImage )
366 : {
367 0 : bImageSet = true;
368 0 : pActMenu->SetItemImage( nItemId, aImage );
369 0 : }
370 : }
371 :
372 0 : OUString aCmd( pActMenu->GetItemCommand( nItemId ) );
373 0 : if ( !bImageSet && !aCmd.isEmpty() )
374 : {
375 : Image aImage = SvFileInformationManager::GetImage(
376 0 : INetURLObject(aCmd), false );
377 0 : if ( !!aImage )
378 0 : pActMenu->SetItemImage( nItemId, aImage );
379 0 : }
380 : }
381 : else
382 0 : pActMenu->SetItemImage( nItemId, Image() );
383 : }
384 : }
385 : }
386 :
387 0 : return sal_True;
388 : }
389 :
390 0 : return sal_False;
391 : }
392 :
393 0 : SfxUnoMenuControl* SfxMenuControl::CreateControl( const OUString& rCmd,
394 : sal_uInt16 nId, Menu& rMenu, const OUString& sItemText,
395 : SfxBindings& rBindings, SfxVirtualMenu* pVirt)
396 : {
397 0 : return new SfxUnoMenuControl( rCmd, nId, rMenu, sItemText, rBindings, pVirt);
398 : }
399 :
400 0 : SfxUnoMenuControl::SfxUnoMenuControl(
401 : const OUString& rCmd, sal_uInt16 nSlotId, Menu& /*rMenu*/,
402 : const OUString& rItemText,
403 : SfxBindings& rBindings, SfxVirtualMenu* pVirt)
404 0 : : SfxMenuControl( nSlotId, rBindings )
405 : {
406 0 : Bind( pVirt, nSlotId, rItemText, rBindings);
407 0 : UnBind();
408 0 : pUnoCtrl = new SfxUnoControllerItem( this, rBindings, rCmd );
409 0 : pUnoCtrl->acquire();
410 0 : pUnoCtrl->GetNewDispatch();
411 0 : }
412 :
413 0 : SfxUnoMenuControl::~SfxUnoMenuControl()
414 : {
415 0 : pUnoCtrl->UnBind();
416 0 : pUnoCtrl->release();
417 0 : }
418 :
419 0 : long Select_Impl( void* /*pHdl*/, void* pVoid )
420 : {
421 0 : Menu* pMenu = (Menu*)pVoid;
422 0 : OUString aURL( pMenu->GetItemCommand( pMenu->GetCurItemId() ) );
423 :
424 0 : if( aURL.isEmpty() )
425 0 : return 0;
426 :
427 : Reference < ::com::sun::star::frame::XDesktop2 > xDesktop =
428 0 : ::com::sun::star::frame::Desktop::create( ::comphelper::getProcessComponentContext() );
429 :
430 0 : URL aTargetURL;
431 0 : aTargetURL.Complete = aURL;
432 0 : Reference < XURLTransformer > xTrans( URLTransformer::create( ::comphelper::getProcessComponentContext() ) );
433 0 : xTrans->parseStrict( aTargetURL );
434 :
435 0 : Reference < XDispatch > xDisp;
436 0 : if (aTargetURL.Protocol == "slot:")
437 0 : xDisp = xDesktop->queryDispatch( aTargetURL, OUString(), 0 );
438 : else
439 : {
440 0 : OUString aTargetFrame( "_blank" );
441 : ::framework::MenuConfiguration::Attributes* pMenuAttributes =
442 0 : reinterpret_cast< ::framework::MenuConfiguration::Attributes*>(pMenu->GetUserValue( pMenu->GetCurItemId() ));
443 :
444 0 : if ( pMenuAttributes )
445 0 : aTargetFrame = pMenuAttributes->aTargetFrame;
446 :
447 0 : xDisp = xDesktop->queryDispatch( aTargetURL, aTargetFrame , 0 );
448 : }
449 :
450 0 : if ( xDisp.is() )
451 : {
452 0 : SfxAppMenuControl_Impl::ExecuteInfo* pExecuteInfo = new SfxAppMenuControl_Impl::ExecuteInfo;
453 0 : pExecuteInfo->xDispatch = xDisp;
454 0 : pExecuteInfo->aTargetURL = aTargetURL;
455 0 : pExecuteInfo->aArgs = Sequence< PropertyValue >();
456 0 : Application::PostUserEvent( STATIC_LINK( 0, SfxAppMenuControl_Impl, ExecuteHdl_Impl), pExecuteInfo );
457 : }
458 :
459 0 : return sal_True;
460 : }
461 :
462 0 : IMPL_STATIC_LINK_NOINSTANCE( SfxAppMenuControl_Impl, ExecuteHdl_Impl, ExecuteInfo*, pExecuteInfo )
463 : {
464 0 : pExecuteInfo->xDispatch->dispatch( pExecuteInfo->aTargetURL, pExecuteInfo->aArgs );
465 0 : delete pExecuteInfo;
466 0 : return 0;
467 951 : }
468 :
469 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|