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