1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <toolkit/awt/vclxmenu.hxx>
21 #include <toolkit/helper/macros.hxx>
22 #include <toolkit/helper/servicenames.hxx>
23 #include <toolkit/helper/vclunohelper.hxx>
24 #include <toolkit/helper/convert.hxx>
25 #include <cppuhelper/typeprovider.hxx>
27 #include <osl/mutex.hxx>
29 #include <vcl/menu.hxx>
30 #include <vcl/keycod.hxx>
31 #include <vcl/image.hxx>
32 #include <vcl/mnemonic.hxx>
33 #include <vcl/svapp.hxx>
35 #include <com/sun/star/awt/KeyModifier.hpp>
39 #define THROW_MENUITEM_NOT_FOUND( Func, nItemId ) \
40 if ( MENU_ITEM_NOTFOUND == mpMenu->GetItemPos( nItemId ) ) \
41 throw ::com::sun::star::container::NoSuchElementException( \
43 ": There is no menu item with " \
44 + OUString::valueOf( sal_Int32( nItemId ) ) \
48 #define THROW_MENUPOS_NOT_FOUND( Func, nPos ) \
49 if ( MENU_ITEM_NOTFOUND == sal_uInt16( nPos ) ) \
50 throw ::com::sun::star::container::NoSuchElementException( \
52 ": There is no menu item at position " \
53 + OUString::valueOf( sal_Int32( nPos ) ), \
57 #define THROW_MENUITEM_NOT_FOUND( Func, nItemId ) \
58 if ( MENU_ITEM_NOTFOUND == mpMenu->GetItemPos( nItemId ) ) \
59 throw ::com::sun::star::container::NoSuchElementException();
60 #define THROW_MENUPOS_NOT_FOUND( Func, nPos ) \
61 if ( MENU_ITEM_NOTFOUND == sal_uInt16( nPos ) ) \
62 throw ::com::sun::star::container::NoSuchElementException();
66 // ----------------------------------------------------
68 // ----------------------------------------------------
72 VCLXMenu::VCLXMenu() : maMenuListeners( *this )
74 DBG_CTOR( VCLXMenu
, 0 );
78 VCLXMenu::VCLXMenu( Menu
* pMenu
) : maMenuListeners( *this )
80 DBG_CTOR( VCLXMenu
, 0 );
86 DBG_DTOR( VCLXMenu
, 0 );
87 for ( size_t n
= maPopupMenuRefs
.size(); n
; ) {
88 delete maPopupMenuRefs
[ --n
];
92 mpMenu
->RemoveEventListener( LINK( this, VCLXMenu
, MenuEventListener
) );
97 sal_Bool
VCLXMenu::IsPopupMenu() const
99 return (mpMenu
&& ! mpMenu
->IsMenuBar());
102 void VCLXMenu::ImplCreateMenu( sal_Bool bPopup
)
104 DBG_ASSERT( !mpMenu
, "CreateMenu: Menu exists!" );
107 mpMenu
= new PopupMenu
;
109 mpMenu
= new MenuBar
;
111 mpMenu
->AddEventListener( LINK( this, VCLXMenu
, MenuEventListener
) );
114 IMPL_LINK( VCLXMenu
, MenuEventListener
, VclSimpleEvent
*, pEvent
)
116 DBG_ASSERT( pEvent
&& pEvent
->ISA( VclMenuEvent
), "Unknown Event!" );
117 if ( pEvent
&& pEvent
->ISA( VclMenuEvent
) )
119 DBG_ASSERT( ((VclMenuEvent
*)pEvent
)->GetMenu() && mpMenu
, "Menu???" );
121 VclMenuEvent
* pMenuEvent
= (VclMenuEvent
*)pEvent
;
122 if ( pMenuEvent
->GetMenu() == mpMenu
) // Also called for the root menu
124 switch ( pMenuEvent
->GetId() )
126 case VCLEVENT_MENU_SELECT
:
128 if ( maMenuListeners
.getLength() )
130 ::com::sun::star::awt::MenuEvent aEvent
;
131 aEvent
.Source
= (::cppu::OWeakObject
*)this;
132 aEvent
.MenuId
= mpMenu
->GetCurItemId();
133 maMenuListeners
.select( aEvent
);
137 case VCLEVENT_OBJECT_DYING
:
142 case VCLEVENT_MENU_HIGHLIGHT
:
144 if ( maMenuListeners
.getLength() )
146 ::com::sun::star::awt::MenuEvent aEvent
;
147 aEvent
.Source
= (::cppu::OWeakObject
*)this;
148 aEvent
.MenuId
= mpMenu
->GetCurItemId();
149 maMenuListeners
.highlight( aEvent
);
153 case VCLEVENT_MENU_ACTIVATE
:
155 if ( maMenuListeners
.getLength() )
157 ::com::sun::star::awt::MenuEvent aEvent
;
158 aEvent
.Source
= (::cppu::OWeakObject
*)this;
159 aEvent
.MenuId
= mpMenu
->GetCurItemId();
160 maMenuListeners
.activate( aEvent
);
164 case VCLEVENT_MENU_DEACTIVATE
:
166 if ( maMenuListeners
.getLength() )
168 ::com::sun::star::awt::MenuEvent aEvent
;
169 aEvent
.Source
= (::cppu::OWeakObject
*)this;
170 aEvent
.MenuId
= mpMenu
->GetCurItemId();
171 maMenuListeners
.deactivate( aEvent
);
176 // ignore accessibility events
177 case VCLEVENT_MENU_ENABLE
:
178 case VCLEVENT_MENU_INSERTITEM
:
179 case VCLEVENT_MENU_REMOVEITEM
:
180 case VCLEVENT_MENU_SUBMENUACTIVATE
:
181 case VCLEVENT_MENU_SUBMENUDEACTIVATE
:
182 case VCLEVENT_MENU_SUBMENUCHANGED
:
183 case VCLEVENT_MENU_DEHIGHLIGHT
:
184 case VCLEVENT_MENU_DISABLE
:
185 case VCLEVENT_MENU_ITEMTEXTCHANGED
:
186 case VCLEVENT_MENU_ITEMCHECKED
:
187 case VCLEVENT_MENU_ITEMUNCHECKED
:
188 case VCLEVENT_MENU_SHOW
:
189 case VCLEVENT_MENU_HIDE
:
192 default: OSL_FAIL( "MenuEventListener - Unknown event!" );
200 //=============================================================================
201 //=============================================================================
202 //=============================================================================
205 // ::com::sun::star::lang::XServiceInfo
206 OUString SAL_CALL
VCLXMenu::getImplementationName( )
207 throw (::com::sun::star::uno::RuntimeException
)
209 ::osl::ResettableGuard
< ::osl::Mutex
> aGuard( GetMutex() );
210 const sal_Bool bIsPopupMenu
= IsPopupMenu();
213 OUString
implName( "stardiv.Toolkit." );
215 implName
+= "VCLXPopupMenu";
217 implName
+= "VCLXMenuBar";
223 ::com::sun::star::uno::Sequence
< OUString
> SAL_CALL
VCLXMenu::getSupportedServiceNames( )
224 throw (::com::sun::star::uno::RuntimeException
)
226 ::osl::ResettableGuard
< ::osl::Mutex
> aGuard( GetMutex() );
227 const sal_Bool bIsPopupMenu
= IsPopupMenu();
230 ::com::sun::star::uno::Sequence
< OUString
> aNames( 1 );
232 aNames
[ 0 ] = OUString::createFromAscii( szServiceName2_PopupMenu
);
234 aNames
[ 0 ] = OUString::createFromAscii( szServiceName2_MenuBar
);
240 ::sal_Bool SAL_CALL
VCLXMenu::supportsService( const OUString
& rServiceName
)
241 throw (::com::sun::star::uno::RuntimeException
)
243 ::com::sun::star::uno::Sequence
< OUString
> aServiceNames( getSupportedServiceNames() );
245 if ( aServiceNames
[ 0 ] == rServiceName
)
252 // ::com::sun::star::uno::XInterface
253 ::com::sun::star::uno::Any
VCLXMenu::queryInterface( const ::com::sun::star::uno::Type
& rType
)
254 throw(::com::sun::star::uno::RuntimeException
)
256 ::osl::ResettableGuard
< ::osl::Mutex
> aGuard( GetMutex() );
257 const sal_Bool bIsPopupMenu
= IsPopupMenu();
260 ::com::sun::star::uno::Any aRet
;
263 aRet
= ::cppu::queryInterface( rType
,
264 (static_cast< ::com::sun::star::awt::XMenu
* >((::com::sun::star::awt::XMenuBar
*) this) ),
265 (static_cast< ::com::sun::star::awt::XPopupMenu
* >(this)),
266 (static_cast< ::com::sun::star::awt::XPopupMenuExtended
* >(this)),
267 (static_cast< ::com::sun::star::awt::XMenuExtended
* >((::com::sun::star::awt::XPopupMenuExtended
*) this) ),
268 (static_cast< ::com::sun::star::awt::XMenuExtended2
* >((::com::sun::star::awt::XPopupMenuExtended
*) this) ),
269 (static_cast< ::com::sun::star::lang::XTypeProvider
* >(this)),
270 (static_cast< ::com::sun::star::lang::XServiceInfo
* >(this)),
271 (static_cast< ::com::sun::star::lang::XUnoTunnel
* >(this)) );
273 aRet
= ::cppu::queryInterface( rType
,
274 (static_cast< ::com::sun::star::awt::XMenu
* >((::com::sun::star::awt::XMenuBar
*) this) ),
275 (static_cast< ::com::sun::star::awt::XMenuBar
* >(this)),
276 (static_cast< ::com::sun::star::awt::XMenuBarExtended
* >(this)),
277 (static_cast< ::com::sun::star::awt::XMenuExtended
* >((::com::sun::star::awt::XMenuBarExtended
*) this) ),
278 (static_cast< ::com::sun::star::awt::XMenuExtended2
* >((::com::sun::star::awt::XMenuBarExtended
*) this) ),
279 (static_cast< ::com::sun::star::lang::XTypeProvider
* >(this)),
280 (static_cast< ::com::sun::star::lang::XServiceInfo
* >(this)),
281 (static_cast< ::com::sun::star::lang::XUnoTunnel
* >(this)) );
283 return (aRet
.hasValue() ? aRet
: OWeakObject::queryInterface( rType
));
286 // ::com::sun::star::lang::XUnoTunnel
287 IMPL_XUNOTUNNEL( VCLXMenu
)
289 // ::com::sun::star::lang::XTypeProvider
290 ::com::sun::star::uno::Sequence
< ::com::sun::star::uno::Type
> VCLXMenu::getTypes()
291 throw(::com::sun::star::uno::RuntimeException
)
293 ::osl::ResettableGuard
< ::osl::Mutex
> aGuard( GetMutex() );
294 const sal_Bool bIsPopupMenu
= IsPopupMenu();
297 static ::cppu::OTypeCollection
* pCollectionMenuBar
= NULL
;
298 static ::cppu::OTypeCollection
* pCollectionPopupMenu
= NULL
;
302 if( !pCollectionPopupMenu
)
304 ::osl::Guard
< ::osl::Mutex
> aGlobalGuard( ::osl::Mutex::getGlobalMutex() );
305 if( !pCollectionPopupMenu
)
307 static ::cppu::OTypeCollection
collectionPopupMenu(
308 getCppuType( ( ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XTypeProvider
>* ) NULL
),
309 getCppuType( ( ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XMenu
>* ) NULL
),
310 getCppuType( ( ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XPopupMenu
>* ) NULL
),
311 getCppuType( ( ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XPopupMenuExtended
>* ) NULL
),
312 getCppuType( ( ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XMenuExtended
>* ) NULL
),
313 getCppuType( ( ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XMenuExtended2
>* ) NULL
),
314 getCppuType( ( ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XServiceInfo
>* ) NULL
) );
315 pCollectionPopupMenu
= &collectionPopupMenu
;
319 return (*pCollectionPopupMenu
).getTypes();
323 if( !pCollectionMenuBar
)
325 ::osl::Guard
< ::osl::Mutex
> aGlobalGuard( ::osl::Mutex::getGlobalMutex() );
326 if( !pCollectionMenuBar
)
328 static ::cppu::OTypeCollection
collectionMenuBar(
329 getCppuType( ( ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XTypeProvider
>* ) NULL
),
330 getCppuType( ( ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XMenu
>* ) NULL
),
331 getCppuType( ( ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XMenuBar
>* ) NULL
),
332 getCppuType( ( ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XMenuBarExtended
>* ) NULL
),
333 getCppuType( ( ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XMenuExtended
>* ) NULL
),
334 getCppuType( ( ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XMenuExtended2
>* ) NULL
),
335 getCppuType( ( ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XServiceInfo
>* ) NULL
) );
336 pCollectionMenuBar
= &collectionMenuBar
;
339 return (*pCollectionMenuBar
).getTypes();
344 ::com::sun::star::uno::Sequence
< sal_Int8
> VCLXMenu::getImplementationId()
345 throw(::com::sun::star::uno::RuntimeException
)
347 ::osl::ResettableGuard
< ::osl::Mutex
> aGuard( GetMutex() );
348 const sal_Bool bIsPopupMenu
= IsPopupMenu();
351 static ::cppu::OImplementationId
* pIdMenuBar
= NULL
;
352 static ::cppu::OImplementationId
* pIdPopupMenu
= NULL
;
358 ::osl::Guard
< ::osl::Mutex
> aGlobalGuard( ::osl::Mutex::getGlobalMutex() );
361 static ::cppu::OImplementationId
idPopupMenu( sal_False
);
362 pIdPopupMenu
= &idPopupMenu
;
366 return (*pIdPopupMenu
).getImplementationId();
372 ::osl::Guard
< ::osl::Mutex
> aGlobalGuard( ::osl::Mutex::getGlobalMutex() );
375 static ::cppu::OImplementationId
idMenuBar( sal_False
);
376 pIdMenuBar
= &idMenuBar
;
380 return (*pIdMenuBar
).getImplementationId();
385 //=============================================================================
386 //=============================================================================
387 //=============================================================================
390 void VCLXMenu::addMenuListener( const ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XMenuListener
>& rxListener
) throw(::com::sun::star::uno::RuntimeException
)
392 ::osl::Guard
< ::osl::Mutex
> aGuard( GetMutex() );
394 maMenuListeners
.addInterface( rxListener
);
397 void VCLXMenu::removeMenuListener( const ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XMenuListener
>& rxListener
) throw(::com::sun::star::uno::RuntimeException
)
399 ::osl::Guard
< ::osl::Mutex
> aGuard( GetMutex() );
401 maMenuListeners
.removeInterface( rxListener
);
404 void VCLXMenu::insertItem( sal_Int16 nItemId
, const OUString
& aText
, sal_Int16 nItemStyle
, sal_Int16 nPos
) throw(::com::sun::star::uno::RuntimeException
)
406 SolarMutexGuard aSolarGuard
;
407 ::osl::Guard
< ::osl::Mutex
> aGuard( GetMutex() );
410 mpMenu
->InsertItem(nItemId
, aText
, (MenuItemBits
)nItemStyle
, OString(), nPos
);
413 void VCLXMenu::removeItem( sal_Int16 nPos
, sal_Int16 nCount
) throw(::com::sun::star::uno::RuntimeException
)
415 SolarMutexGuard aSolarGuard
;
416 ::osl::Guard
< ::osl::Mutex
> aGuard( GetMutex() );
418 sal_Int32 nItemCount
= (sal_Int32
)mpMenu
->GetItemCount();
419 if ( mpMenu
&& ( nCount
> 0 ) && ( nPos
>= 0 ) && ( nPos
< nItemCount
) && ( nItemCount
> 0 ))
421 sal_Int16 nP
= sal::static_int_cast
< sal_Int16
>(
422 std::min( (int)(nPos
+nCount
), (int)nItemCount
));
424 mpMenu
->RemoveItem( --nP
);
428 sal_Int16
VCLXMenu::getItemCount( ) throw(::com::sun::star::uno::RuntimeException
)
430 SolarMutexGuard aSolarGuard
;
431 ::osl::Guard
< ::osl::Mutex
> aGuard( GetMutex() );
433 return mpMenu
? mpMenu
->GetItemCount() : 0;
436 sal_Int16
VCLXMenu::getItemId( sal_Int16 nPos
) throw(::com::sun::star::uno::RuntimeException
)
438 SolarMutexGuard aSolarGuard
;
439 ::osl::Guard
< ::osl::Mutex
> aGuard( GetMutex() );
441 return mpMenu
? mpMenu
->GetItemId( nPos
) : 0;
444 sal_Int16
VCLXMenu::getItemPos( sal_Int16 nId
) throw(::com::sun::star::uno::RuntimeException
)
446 SolarMutexGuard aSolarGuard
;
447 ::osl::Guard
< ::osl::Mutex
> aGuard( GetMutex() );
449 return mpMenu
? mpMenu
->GetItemPos( nId
) : 0;
452 void VCLXMenu::enableItem( sal_Int16 nItemId
, sal_Bool bEnable
) throw(::com::sun::star::uno::RuntimeException
)
454 SolarMutexGuard aSolarGuard
;
455 ::osl::Guard
< ::osl::Mutex
> aGuard( GetMutex() );
458 mpMenu
->EnableItem( nItemId
, bEnable
);
461 sal_Bool
VCLXMenu::isItemEnabled( sal_Int16 nItemId
) throw(::com::sun::star::uno::RuntimeException
)
463 SolarMutexGuard aSolarGuard
;
464 ::osl::Guard
< ::osl::Mutex
> aGuard( GetMutex() );
466 return mpMenu
? mpMenu
->IsItemEnabled( nItemId
) : sal_False
;
469 void VCLXMenu::setItemText( sal_Int16 nItemId
, const OUString
& aText
) throw(::com::sun::star::uno::RuntimeException
)
471 SolarMutexGuard aSolarGuard
;
472 ::osl::Guard
< ::osl::Mutex
> aGuard( GetMutex() );
475 mpMenu
->SetItemText( nItemId
, aText
);
478 OUString
VCLXMenu::getItemText( sal_Int16 nItemId
) throw(::com::sun::star::uno::RuntimeException
)
480 SolarMutexGuard aSolarGuard
;
481 ::osl::Guard
< ::osl::Mutex
> aGuard( GetMutex() );
485 aItemText
= mpMenu
->GetItemText( nItemId
);
489 void VCLXMenu::setPopupMenu( sal_Int16 nItemId
, const ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XPopupMenu
>& rxPopupMenu
) throw(::com::sun::star::uno::RuntimeException
)
491 SolarMutexGuard aSolarGuard
;
492 ::osl::Guard
< ::osl::Mutex
> aGuard( GetMutex() );
494 VCLXMenu
* pVCLMenu
= VCLXMenu::GetImplementation( rxPopupMenu
);
495 DBG_ASSERT( pVCLMenu
&& pVCLMenu
->GetMenu() && pVCLMenu
->IsPopupMenu(), "setPopupMenu: Invalid Menu!" );
497 if ( mpMenu
&& pVCLMenu
&& pVCLMenu
->GetMenu() && pVCLMenu
->IsPopupMenu() )
499 // Selbst eine Ref halten!
500 ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XPopupMenu
> * pNewRef
= new ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XPopupMenu
> ;
501 *pNewRef
= rxPopupMenu
;
502 maPopupMenuRefs
.push_back( pNewRef
);
504 mpMenu
->SetPopupMenu( nItemId
, (PopupMenu
*) pVCLMenu
->GetMenu() );
508 ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XPopupMenu
> VCLXMenu::getPopupMenu( sal_Int16 nItemId
) throw(::com::sun::star::uno::RuntimeException
)
510 SolarMutexGuard aSolarGuard
;
511 ::osl::Guard
< ::osl::Mutex
> aGuard( GetMutex() );
513 ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XPopupMenu
> aRef
;
514 Menu
* pMenu
= mpMenu
? mpMenu
->GetPopupMenu( nItemId
) : NULL
;
517 for ( size_t n
= maPopupMenuRefs
.size(); n
; )
519 ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XPopupMenu
> * pRef
= maPopupMenuRefs
[ --n
];
520 Menu
* pM
= ((VCLXMenu
*)pRef
->get())->GetMenu();
527 // it seems the popup menu is not insert into maPopupMenuRefs
528 // if the popup men is not created by stardiv.Toolkit.VCLXPopupMenu
531 aRef
= new VCLXPopupMenu( (PopupMenu
*)pMenu
);
537 // ::com::sun::star::awt::XPopupMenu
538 void VCLXMenu::insertSeparator( sal_Int16 nPos
) throw(::com::sun::star::uno::RuntimeException
)
540 SolarMutexGuard aSolarGuard
;
541 ::osl::Guard
< ::osl::Mutex
> aGuard( GetMutex() );
544 mpMenu
->InsertSeparator(OString(), nPos
);
547 void VCLXMenu::setDefaultItem( sal_Int16 nItemId
) throw(::com::sun::star::uno::RuntimeException
)
549 SolarMutexGuard aSolarGuard
;
550 ::osl::Guard
< ::osl::Mutex
> aGuard( GetMutex() );
553 mpMenu
->SetDefaultItem( nItemId
);
556 sal_Int16
VCLXMenu::getDefaultItem( ) throw(::com::sun::star::uno::RuntimeException
)
558 SolarMutexGuard aSolarGuard
;
559 ::osl::Guard
< ::osl::Mutex
> aGuard( GetMutex() );
561 return mpMenu
? mpMenu
->GetDefaultItem() : 0;
564 void VCLXMenu::checkItem( sal_Int16 nItemId
, sal_Bool bCheck
) throw(::com::sun::star::uno::RuntimeException
)
566 SolarMutexGuard aSolarGuard
;
567 ::osl::Guard
< ::osl::Mutex
> aGuard( GetMutex() );
570 mpMenu
->CheckItem( nItemId
, bCheck
);
573 sal_Bool
VCLXMenu::isItemChecked( sal_Int16 nItemId
) throw(::com::sun::star::uno::RuntimeException
)
575 SolarMutexGuard aSolarGuard
;
576 ::osl::Guard
< ::osl::Mutex
> aGuard( GetMutex() );
578 return mpMenu
? mpMenu
->IsItemChecked( nItemId
) : sal_False
;
581 sal_Int16
VCLXMenu::execute( const ::com::sun::star::uno::Reference
< ::com::sun::star::awt::XWindowPeer
>& rxWindowPeer
, const ::com::sun::star::awt::Rectangle
& rArea
, sal_Int16 nFlags
) throw(::com::sun::star::uno::RuntimeException
)
583 SolarMutexGuard aSolarGuard
;
584 ::osl::Guard
< ::osl::Mutex
> aGuard( GetMutex() );
587 if ( mpMenu
&& IsPopupMenu() )
588 nRet
= ((PopupMenu
*)mpMenu
)->Execute( VCLUnoHelper::GetWindow( rxWindowPeer
), VCLRectangle(rArea
), nFlags
| POPUPMENU_NOMOUSEUPCLOSE
);
593 void SAL_CALL
VCLXMenu::setCommand( sal_Int16 nItemId
, const OUString
& aCommand
) throw (::com::sun::star::uno::RuntimeException
)
595 SolarMutexGuard aSolarGuard
;
596 ::osl::Guard
< ::osl::Mutex
> aGuard( GetMutex() );
599 mpMenu
->SetItemCommand( nItemId
, aCommand
);
602 OUString SAL_CALL
VCLXMenu::getCommand( sal_Int16 nItemId
) throw (::com::sun::star::uno::RuntimeException
)
604 SolarMutexGuard aSolarGuard
;
605 ::osl::Guard
< ::osl::Mutex
> aGuard( GetMutex() );
607 OUString aItemCommand
;
609 aItemCommand
= mpMenu
->GetItemCommand( nItemId
);
613 void SAL_CALL
VCLXMenu::setHelpCommand( sal_Int16 nItemId
, const OUString
& aHelp
) throw (::com::sun::star::uno::RuntimeException
)
615 SolarMutexGuard aSolarGuard
;
616 ::osl::Guard
< ::osl::Mutex
> aGuard( GetMutex() );
619 mpMenu
->SetHelpCommand( nItemId
, aHelp
);
622 OUString SAL_CALL
VCLXMenu::getHelpCommand( sal_Int16 nItemId
) throw (::com::sun::star::uno::RuntimeException
)
624 SolarMutexGuard aSolarGuard
;
625 ::osl::Guard
< ::osl::Mutex
> aGuard( GetMutex() );
627 OUString aHelpCommand
;
629 aHelpCommand
= mpMenu
->GetHelpCommand( nItemId
);
634 // ============================================================================
635 // ============================================================================
636 // ============================================================================
639 // BEGIN ANONYMOUS NAMESPACE
642 Image
lcl_XGraphic2VCLImage(
643 const css::uno::Reference
< css::graphic::XGraphic
>& xGraphic
,
647 if ( !xGraphic
.is() )
650 aImage
= Image( xGraphic
);
651 const ::Size aCurSize
= aImage
.GetSizePixel();
652 const sal_Int32 nCurWidth
= aCurSize
.Width();
653 const sal_Int32 nCurHeight
= aCurSize
.Height();
654 const sal_Int32
nIdeal( 16 );
656 if ( nCurWidth
> 0 && nCurHeight
> 0 )
658 if ( bResize
&& ( nCurWidth
> nIdeal
|| nCurHeight
> nIdeal
) )
660 sal_Int32 nIdealWidth
= nCurWidth
> nIdeal
? nIdeal
: nCurWidth
;
661 sal_Int32 nIdealHeight
= nCurHeight
> nIdeal
? nIdeal
: nCurHeight
;
663 ::Size
aNewSize( nIdealWidth
, nIdealHeight
);
665 sal_Bool
bModified( sal_False
);
666 BitmapEx aBitmapEx
= aImage
.GetBitmapEx();
667 bModified
= aBitmapEx
.Scale( aNewSize
, BMP_SCALE_BESTQUALITY
);
670 aImage
= Image( aBitmapEx
);
677 As svtools builds after toolkit, we can not include/use
678 svtools/inc/acceleratorexecute.hxx
679 So I just copy here svt::AcceleratorExecute::st_AWTKey2VCLKey
680 and svt::AcceleratorExecute::st_VCLKey2AWTKey
682 css::awt::KeyEvent
lcl_VCLKey2AWTKey(const KeyCode
& aVCLKey
)
684 css::awt::KeyEvent aAWTKey
;
685 aAWTKey
.Modifiers
= 0;
686 aAWTKey
.KeyCode
= (sal_Int16
)aVCLKey
.GetCode();
688 if (aVCLKey
.IsShift())
689 aAWTKey
.Modifiers
|= css::awt::KeyModifier::SHIFT
;
690 if (aVCLKey
.IsMod1())
691 aAWTKey
.Modifiers
|= css::awt::KeyModifier::MOD1
;
692 if (aVCLKey
.IsMod2())
693 aAWTKey
.Modifiers
|= css::awt::KeyModifier::MOD2
;
694 if (aVCLKey
.IsMod3())
695 aAWTKey
.Modifiers
|= css::awt::KeyModifier::MOD3
;
700 KeyCode
lcl_AWTKey2VCLKey(const css::awt::KeyEvent
& aAWTKey
)
702 sal_Bool bShift
= ((aAWTKey
.Modifiers
& css::awt::KeyModifier::SHIFT
) == css::awt::KeyModifier::SHIFT
);
703 sal_Bool bMod1
= ((aAWTKey
.Modifiers
& css::awt::KeyModifier::MOD1
) == css::awt::KeyModifier::MOD1
);
704 sal_Bool bMod2
= ((aAWTKey
.Modifiers
& css::awt::KeyModifier::MOD2
) == css::awt::KeyModifier::MOD2
);
705 sal_Bool bMod3
= ((aAWTKey
.Modifiers
& css::awt::KeyModifier::MOD3
) == css::awt::KeyModifier::MOD3
);
706 sal_uInt16 nKey
= (sal_uInt16
)aAWTKey
.KeyCode
;
708 return KeyCode(nKey
, bShift
, bMod1
, bMod2
, bMod3
);
711 } // END ANONYMOUS NAMESPACE
714 // ============================================================================
715 // ============================================================================
716 // ============================================================================
719 // XMenuExtended2 Methods
721 ::sal_Bool SAL_CALL
VCLXMenu::isPopupMenu( ) throw (::com::sun::star::uno::RuntimeException
)
723 SolarMutexGuard aSolarGuard
;
724 ::osl::Guard
< ::osl::Mutex
> aGuard( GetMutex() );
725 return IsPopupMenu();
728 void SAL_CALL
VCLXMenu::clear( ) throw (::com::sun::star::uno::RuntimeException
)
730 SolarMutexGuard aSolarGuard
;
731 ::osl::Guard
< ::osl::Mutex
> aGuard( GetMutex() );
737 ::com::sun::star::awt::MenuItemType SAL_CALL
VCLXMenu::getItemType( ::sal_Int16 nItemPos
)
738 throw ( ::com::sun::star::container::NoSuchElementException
,
739 ::com::sun::star::uno::RuntimeException
)
741 SolarMutexGuard aSolarGuard
;
742 ::osl::Guard
< ::osl::Mutex
> aGuard( GetMutex() );
744 ::com::sun::star::awt::MenuItemType aMenuItemType
=
745 ::com::sun::star::awt::MenuItemType_DONTKNOW
;
748 THROW_MENUPOS_NOT_FOUND( "VCLXMenu::getItemType()", nItemPos
)
749 aMenuItemType
= ( (::com::sun::star::awt::MenuItemType
) mpMenu
->GetItemType( nItemPos
) );
752 return aMenuItemType
;
755 void SAL_CALL
VCLXMenu::hideDisabledEntries( ::sal_Bool bHide
)
756 throw (::com::sun::star::uno::RuntimeException
)
758 SolarMutexGuard aSolarGuard
;
759 ::osl::Guard
< ::osl::Mutex
> aGuard( GetMutex() );
763 mpMenu
->SetMenuFlags( mpMenu
->GetMenuFlags() | MENU_FLAG_HIDEDISABLEDENTRIES
);
765 mpMenu
->SetMenuFlags( mpMenu
->GetMenuFlags() & ~MENU_FLAG_HIDEDISABLEDENTRIES
);
770 // ============================================================================
771 // ============================================================================
772 // ============================================================================
775 // XPopupMenuExtended Methods
777 ::sal_Bool SAL_CALL
VCLXMenu::isInExecute( )
778 throw (::com::sun::star::uno::RuntimeException
)
780 SolarMutexGuard aSolarGuard
;
781 ::osl::Guard
< ::osl::Mutex
> aGuard( GetMutex() );
783 if ( mpMenu
&& IsPopupMenu() )
784 return ( (PopupMenu
*) mpMenu
)->IsInExecute();
790 void SAL_CALL
VCLXMenu::endExecute()
791 throw (::com::sun::star::uno::RuntimeException
)
793 SolarMutexGuard aSolarGuard
;
794 ::osl::Guard
< ::osl::Mutex
> aGuard( GetMutex() );
796 if ( mpMenu
&& IsPopupMenu() )
797 ( (PopupMenu
*) mpMenu
)->EndExecute();
801 void SAL_CALL
VCLXMenu::setLogo( const ::com::sun::star::awt::MenuLogo
& aMenuLogo
)
802 throw (::com::sun::star::uno::RuntimeException
)
804 SolarMutexGuard aSolarGuard
;
805 ::osl::Guard
< ::osl::Mutex
> aGuard( GetMutex() );
809 if ( aMenuLogo
.Graphic
.is() )
811 Image aImage
= lcl_XGraphic2VCLImage( aMenuLogo
.Graphic
, sal_False
);
812 MenuLogo aVCLMenuLogo
;
814 aVCLMenuLogo
.aBitmap
= aImage
.GetBitmapEx();
815 aVCLMenuLogo
.aStartColor
= Color( (sal_uInt32
)(aMenuLogo
.StartColor
) );
816 aVCLMenuLogo
.aEndColor
= Color( (sal_uInt32
)(aMenuLogo
.EndColor
) );
818 mpMenu
->SetLogo( aVCLMenuLogo
);
826 ::com::sun::star::awt::MenuLogo SAL_CALL
VCLXMenu::getLogo( )
827 throw (::com::sun::star::uno::RuntimeException
)
829 SolarMutexGuard aSolarGuard
;
830 ::osl::Guard
< ::osl::Mutex
> aGuard( GetMutex() );
832 ::com::sun::star::awt::MenuLogo aAWTMenuLogo
;
835 if ( mpMenu
->HasLogo() )
837 MenuLogo aVCLMenuLogo
= mpMenu
->GetLogo();
838 aAWTMenuLogo
.Graphic
= Image(aVCLMenuLogo
.aBitmap
).GetXGraphic();
839 aAWTMenuLogo
.StartColor
= aVCLMenuLogo
.aStartColor
.GetColor();
840 aAWTMenuLogo
.EndColor
= aVCLMenuLogo
.aEndColor
.GetColor();
847 void SAL_CALL
VCLXMenu::enableAutoMnemonics( ::sal_Bool bEnable
)
848 throw (::com::sun::star::uno::RuntimeException
)
850 SolarMutexGuard aSolarGuard
;
851 ::osl::Guard
< ::osl::Mutex
> aGuard( GetMutex() );
855 mpMenu
->SetMenuFlags( mpMenu
->GetMenuFlags() | MENU_FLAG_NOAUTOMNEMONICS
);
857 mpMenu
->SetMenuFlags( mpMenu
->GetMenuFlags() & ~MENU_FLAG_NOAUTOMNEMONICS
);
862 void SAL_CALL
VCLXMenu::setAcceleratorKeyEvent( ::sal_Int16 nItemId
,
863 const ::com::sun::star::awt::KeyEvent
& aKeyEvent
)
864 throw ( ::com::sun::star::container::NoSuchElementException
,
865 ::com::sun::star::uno::RuntimeException
)
867 SolarMutexGuard aSolarGuard
;
868 ::osl::Guard
< ::osl::Mutex
> aGuard( GetMutex() );
870 if ( mpMenu
&& IsPopupMenu() )
872 THROW_MENUITEM_NOT_FOUND( "VCLXMenu::setAcceleratorKeyEvent()", nItemId
)
873 KeyCode aVCLKeyCode
= lcl_AWTKey2VCLKey( aKeyEvent
);
874 mpMenu
->SetAccelKey( nItemId
, aVCLKeyCode
);
879 ::com::sun::star::awt::KeyEvent SAL_CALL
VCLXMenu::getAcceleratorKeyEvent( ::sal_Int16 nItemId
)
880 throw ( ::com::sun::star::container::NoSuchElementException
,
881 ::com::sun::star::uno::RuntimeException
)
883 SolarMutexGuard aSolarGuard
;
884 ::osl::Guard
< ::osl::Mutex
> aGuard( GetMutex() );
886 ::com::sun::star::awt::KeyEvent aKeyEvent
;
887 if ( mpMenu
&& IsPopupMenu() )
889 THROW_MENUITEM_NOT_FOUND( "VCLXMenu::getAcceleratorKeyEvent()", nItemId
)
890 KeyCode nKeyCode
= mpMenu
->GetAccelKey( nItemId
);
891 aKeyEvent
= lcl_VCLKey2AWTKey( nKeyCode
);
898 void SAL_CALL
VCLXMenu::setHelpText( ::sal_Int16 nItemId
, const OUString
& sHelpText
)
899 throw ( ::com::sun::star::container::NoSuchElementException
,
900 ::com::sun::star::uno::RuntimeException
)
902 SolarMutexGuard aSolarGuard
;
903 ::osl::Guard
< ::osl::Mutex
> aGuard( GetMutex() );
905 if ( mpMenu
&& IsPopupMenu() )
907 THROW_MENUITEM_NOT_FOUND( "VCLXMenu::setHelpText()", nItemId
)
908 mpMenu
->SetHelpText( nItemId
, sHelpText
);
913 OUString SAL_CALL
VCLXMenu::getHelpText( ::sal_Int16 nItemId
)
914 throw ( ::com::sun::star::container::NoSuchElementException
,
915 ::com::sun::star::uno::RuntimeException
)
917 SolarMutexGuard aSolarGuard
;
918 ::osl::Guard
< ::osl::Mutex
> aGuard( GetMutex() );
921 if ( mpMenu
&& IsPopupMenu() )
923 THROW_MENUITEM_NOT_FOUND( "VCLXMenu::getHelpText()", nItemId
)
924 sHelpText
= mpMenu
->GetHelpText( nItemId
);
931 void SAL_CALL
VCLXMenu::setTipHelpText( ::sal_Int16 nItemId
, const OUString
& sTipHelpText
)
932 throw ( ::com::sun::star::container::NoSuchElementException
,
933 ::com::sun::star::uno::RuntimeException
)
935 SolarMutexGuard aSolarGuard
;
936 ::osl::Guard
< ::osl::Mutex
> aGuard( GetMutex() );
938 if ( mpMenu
&& IsPopupMenu() )
940 THROW_MENUITEM_NOT_FOUND( "VCLXMenu::setTipHelpText()", nItemId
)
941 mpMenu
->SetTipHelpText( nItemId
, sTipHelpText
);
946 OUString SAL_CALL
VCLXMenu::getTipHelpText( ::sal_Int16 nItemId
)
947 throw ( ::com::sun::star::container::NoSuchElementException
,
948 ::com::sun::star::uno::RuntimeException
)
950 SolarMutexGuard aSolarGuard
;
951 ::osl::Guard
< ::osl::Mutex
> aGuard( GetMutex() );
953 OUString sTipHelpText
;
954 if ( mpMenu
&& IsPopupMenu() )
956 THROW_MENUITEM_NOT_FOUND( "VCLXMenu::getTipHelpText()", nItemId
)
957 sTipHelpText
= mpMenu
->GetTipHelpText( nItemId
);
963 void SAL_CALL
VCLXMenu::setItemImage(
965 const ::com::sun::star::uno::Reference
< ::com::sun::star::graphic::XGraphic
>& xGraphic
, ::sal_Bool bScale
)
966 throw ( ::com::sun::star::container::NoSuchElementException
,
967 ::com::sun::star::uno::RuntimeException
)
969 SolarMutexGuard aSolarGuard
;
970 ::osl::Guard
< ::osl::Mutex
> aGuard( GetMutex() );
972 if ( mpMenu
&& IsPopupMenu() )
974 THROW_MENUITEM_NOT_FOUND( "VCLXMenu::setItemImage()", nItemId
)
975 Image aImage
= lcl_XGraphic2VCLImage( xGraphic
, bScale
);
976 mpMenu
->SetItemImage( nItemId
, aImage
);
981 ::com::sun::star::uno::Reference
< ::com::sun::star::graphic::XGraphic
> SAL_CALL
VCLXMenu::getItemImage( ::sal_Int16 nItemId
)
982 throw ( ::com::sun::star::container::NoSuchElementException
,
983 ::com::sun::star::uno::RuntimeException
)
985 SolarMutexGuard aSolarGuard
;
986 ::osl::Guard
< ::osl::Mutex
> aGuard( GetMutex() );
988 ::com::sun::star::uno::Reference
< ::com::sun::star::graphic::XGraphic
> rxGraphic
;
990 if ( mpMenu
&& IsPopupMenu() )
992 THROW_MENUITEM_NOT_FOUND( "VCLXMenu::getItemImage()", nItemId
)
993 Image aImage
= mpMenu
->GetItemImage( nItemId
);
995 rxGraphic
= aImage
.GetXGraphic();
1001 void SAL_CALL
VCLXMenu::setItemImageAngle( ::sal_Int16 nItemId
, ::sal_Int32 nAngle
)
1002 throw ( ::com::sun::star::container::NoSuchElementException
,
1003 ::com::sun::star::uno::RuntimeException
)
1005 SolarMutexGuard aSolarGuard
;
1006 ::osl::Guard
< ::osl::Mutex
> aGuard( GetMutex() );
1008 if ( mpMenu
&& IsPopupMenu() )
1010 THROW_MENUITEM_NOT_FOUND( "VCLXMenu::setItemImageAngle()", nItemId
)
1011 mpMenu
->SetItemImageAngle( nItemId
, nAngle
);
1016 ::sal_Int32 SAL_CALL
VCLXMenu::getItemImageAngle( ::sal_Int16 nItemId
)
1017 throw ( ::com::sun::star::container::NoSuchElementException
,
1018 ::com::sun::star::uno::RuntimeException
)
1020 SolarMutexGuard aSolarGuard
;
1021 ::osl::Guard
< ::osl::Mutex
> aGuard( GetMutex() );
1023 ::sal_Int32
nItemImageAngle( 0 );
1024 if ( mpMenu
&& IsPopupMenu() )
1026 THROW_MENUITEM_NOT_FOUND( "VCLXMenu::getItemImageAngle()", nItemId
)
1027 nItemImageAngle
= mpMenu
->GetItemImageAngle( nItemId
);
1029 return nItemImageAngle
;
1033 void SAL_CALL
VCLXMenu::setItemImageMirrorMode( ::sal_Int16 nItemId
, ::sal_Bool bMirror
)
1034 throw ( ::com::sun::star::container::NoSuchElementException
,
1035 ::com::sun::star::uno::RuntimeException
)
1037 SolarMutexGuard aSolarGuard
;
1038 ::osl::Guard
< ::osl::Mutex
> aGuard( GetMutex() );
1040 if ( mpMenu
&& IsPopupMenu() )
1042 THROW_MENUITEM_NOT_FOUND( "VCLXMenu::setItemImageMirrorMode()", nItemId
)
1043 mpMenu
->SetItemImageMirrorMode( nItemId
, bMirror
);
1048 ::sal_Bool SAL_CALL
VCLXMenu::isItemImageInMirrorMode( ::sal_Int16 nItemId
)
1049 throw ( ::com::sun::star::container::NoSuchElementException
,
1050 ::com::sun::star::uno::RuntimeException
)
1052 SolarMutexGuard aSolarGuard
;
1053 ::osl::Guard
< ::osl::Mutex
> aGuard( GetMutex() );
1055 sal_Bool
bMirrorMode( sal_False
);
1056 if ( mpMenu
&& IsPopupMenu() )
1058 THROW_MENUITEM_NOT_FOUND( "VCLXMenu::isItemImageInMirrorMode()", nItemId
)
1059 bMirrorMode
= mpMenu
->GetItemImageMirrorMode( nItemId
);
1065 // ----------------------------------------------------
1066 // class VCLXMenuBar
1067 // ----------------------------------------------------
1069 DBG_NAME(VCLXMenuBar
);
1071 VCLXMenuBar::VCLXMenuBar()
1073 DBG_CTOR( VCLXMenuBar
, 0 );
1074 ImplCreateMenu( sal_False
);
1077 VCLXMenuBar::VCLXMenuBar( MenuBar
* pMenuBar
) : VCLXMenu( (Menu
*)pMenuBar
)
1079 DBG_CTOR( VCLXMenuBar
, 0 );
1082 // ----------------------------------------------------
1083 // class VCLXPopupMenu
1084 // ----------------------------------------------------
1086 DBG_NAME(VCLXPopupMenu
);
1088 VCLXPopupMenu::VCLXPopupMenu()
1090 DBG_CTOR( VCLXPopupMenu
, 0 );
1091 ImplCreateMenu( sal_True
);
1094 VCLXPopupMenu::VCLXPopupMenu( PopupMenu
* pPopMenu
) : VCLXMenu( (Menu
*)pPopMenu
)
1096 DBG_CTOR( VCLXPopupMenu
, 0 );
1099 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */