1 /**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21
22
23
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_toolkit.hxx"
26
27 #include <toolkit/awt/vclxmenu.hxx>
28 #include <toolkit/helper/convert.hxx>
29 #include <toolkit/helper/macros.hxx>
30 #include <toolkit/helper/servicenames.hxx>
31 #include <toolkit/helper/vclunohelper.hxx>
32
33 #include <cppuhelper/typeprovider.hxx>
34 #include <rtl/memory.h>
35 #include <rtl/ustrbuf.hxx>
36 #include <rtl/uuid.h>
37 #include <vcl/image.hxx>
38 #include <vcl/keycod.hxx>
39 #include <vcl/menu.hxx>
40 #include <vcl/mnemonic.hxx>
41 #include <vcl/svapp.hxx>
42 #include <vos/mutex.hxx>
43
44 #include <com/sun/star/awt/KeyModifier.hpp>
45
46 using rtl::OUString;
47 using rtl::OUStringBuffer;
48
49
DBG_NAME(VCLXMenu)50 DBG_NAME(VCLXMenu)
51
52 VCLXMenu::VCLXMenu()
53 : maMenuListeners( *this )
54 {
55 DBG_CTOR( VCLXMenu, 0 );
56 mpMenu = NULL;
57 }
58
VCLXMenu(Menu * pMenu)59 VCLXMenu::VCLXMenu( Menu* pMenu )
60 : maMenuListeners( *this )
61 {
62 DBG_CTOR( VCLXMenu, 0 );
63 mpMenu = pMenu;
64 }
65
~VCLXMenu()66 VCLXMenu::~VCLXMenu()
67 {
68 DBG_DTOR( VCLXMenu, 0 );
69 for ( sal_uInt32 n = maPopupMenueRefs.Count(); n; )
70 {
71 css::uno::Reference< css::awt::XPopupMenu > * pRef = maPopupMenueRefs.GetObject( --n );
72 delete pRef;
73 }
74 if ( mpMenu )
75 {
76 mpMenu->RemoveEventListener( LINK( this, VCLXMenu, MenuEventListener ) );
77 delete mpMenu;
78 }
79 }
80
IsPopupMenu() const81 sal_Bool VCLXMenu::IsPopupMenu() const
82 {
83 return (mpMenu && ! mpMenu->IsMenuBar());
84 }
85
ImplCreateMenu(sal_Bool bPopup)86 void VCLXMenu::ImplCreateMenu( sal_Bool bPopup )
87 {
88 DBG_ASSERT( !mpMenu, "CreateMenu: Menu exists!" );
89
90 if ( bPopup )
91 mpMenu = new PopupMenu;
92 else
93 mpMenu = new MenuBar;
94
95 mpMenu->AddEventListener( LINK( this, VCLXMenu, MenuEventListener ) );
96 }
97
IMPL_LINK(VCLXMenu,MenuEventListener,VclSimpleEvent *,pEvent)98 IMPL_LINK( VCLXMenu, MenuEventListener, VclSimpleEvent*, pEvent )
99 {
100 DBG_ASSERT( pEvent && pEvent->ISA( VclMenuEvent ), "Unknown Event!" );
101 if ( pEvent && pEvent->ISA( VclMenuEvent ) )
102 {
103 DBG_ASSERT( ((VclMenuEvent*)pEvent)->GetMenu() && mpMenu, "Menu???" );
104
105 VclMenuEvent* pMenuEvent = (VclMenuEvent*)pEvent;
106 if ( pMenuEvent->GetMenu() == mpMenu ) // Also called for the root menu
107 {
108 switch ( pMenuEvent->GetId() )
109 {
110 case VCLEVENT_MENU_SELECT:
111 {
112 if ( maMenuListeners.getLength() )
113 {
114 css::awt::MenuEvent aEvent;
115 aEvent.Source = (::cppu::OWeakObject*)this;
116 aEvent.MenuId = mpMenu->GetCurItemId();
117 maMenuListeners.itemSelected( aEvent );
118 }
119 }
120 break;
121 case VCLEVENT_OBJECT_DYING:
122 {
123 mpMenu = NULL;
124 }
125 break;
126 case VCLEVENT_MENU_HIGHLIGHT:
127 {
128 if ( maMenuListeners.getLength() )
129 {
130 css::awt::MenuEvent aEvent;
131 aEvent.Source = (::cppu::OWeakObject*)this;
132 aEvent.MenuId = mpMenu->GetCurItemId();
133 maMenuListeners.itemHighlighted( aEvent );
134 }
135 }
136 break;
137 case VCLEVENT_MENU_ACTIVATE:
138 {
139 if ( maMenuListeners.getLength() )
140 {
141 css::awt::MenuEvent aEvent;
142 aEvent.Source = (::cppu::OWeakObject*)this;
143 aEvent.MenuId = mpMenu->GetCurItemId();
144 maMenuListeners.itemActivated( aEvent );
145 }
146 }
147 break;
148 case VCLEVENT_MENU_DEACTIVATE:
149 {
150 if ( maMenuListeners.getLength() )
151 {
152 css::awt::MenuEvent aEvent;
153 aEvent.Source = (::cppu::OWeakObject*)this;
154 aEvent.MenuId = mpMenu->GetCurItemId();
155 maMenuListeners.itemDeactivated( aEvent );
156 }
157 }
158 break;
159
160 // ignore accessibility events
161 case VCLEVENT_MENU_ENABLE:
162 case VCLEVENT_MENU_INSERTITEM:
163 case VCLEVENT_MENU_REMOVEITEM:
164 case VCLEVENT_MENU_SUBMENUACTIVATE:
165 case VCLEVENT_MENU_SUBMENUDEACTIVATE:
166 case VCLEVENT_MENU_SUBMENUCHANGED:
167 case VCLEVENT_MENU_DEHIGHLIGHT:
168 case VCLEVENT_MENU_DISABLE:
169 case VCLEVENT_MENU_ITEMTEXTCHANGED:
170 case VCLEVENT_MENU_ITEMCHECKED:
171 case VCLEVENT_MENU_ITEMUNCHECKED:
172 case VCLEVENT_MENU_SHOW:
173 case VCLEVENT_MENU_HIDE:
174 break;
175
176 default: DBG_ERROR( "MenuEventListener - Unknown event!" );
177 }
178 }
179 }
180 return 0;
181 }
182
183
getImplementationName()184 OUString SAL_CALL VCLXMenu::getImplementationName( )
185 {
186 ::osl::ResettableGuard < ::osl::Mutex > aGuard( GetMutex() );
187 const sal_Bool bIsPopupMenu = IsPopupMenu();
188 aGuard.clear();
189
190 OUStringBuffer implName;
191 implName.appendAscii( RTL_CONSTASCII_STRINGPARAM( "stardiv.Toolkit." ) );
192 if ( bIsPopupMenu )
193 implName.appendAscii( RTL_CONSTASCII_STRINGPARAM( "VCLXPopupMenu" ) );
194 else
195 implName.appendAscii( RTL_CONSTASCII_STRINGPARAM( "VCLXMenuBar" ) );
196
197 return implName.makeStringAndClear();
198 }
199
200
getSupportedServiceNames()201 css::uno::Sequence< OUString > SAL_CALL VCLXMenu::getSupportedServiceNames( )
202 {
203 ::osl::ResettableGuard < ::osl::Mutex > aGuard( GetMutex() );
204 const sal_Bool bIsPopupMenu = IsPopupMenu();
205 aGuard.clear();
206
207 css::uno::Sequence< OUString > aNames( 1 );
208 if ( bIsPopupMenu )
209 aNames[ 0 ] = OUString::createFromAscii( szServiceName2_PopupMenu );
210 else
211 aNames[ 0 ] = OUString::createFromAscii( szServiceName2_MenuBar );
212
213 return aNames;
214 }
215
216
supportsService(const OUString & rServiceName)217 ::sal_Bool SAL_CALL VCLXMenu::supportsService(
218 const OUString& rServiceName )
219 {
220 css::uno::Sequence< OUString > aServiceNames( getSupportedServiceNames() );
221
222 if ( aServiceNames[ 0 ] == rServiceName )
223 return sal_True;
224
225 return sal_False;
226 }
227
228
queryInterface(const css::uno::Type & rType)229 css::uno::Any VCLXMenu::queryInterface(
230 const css::uno::Type & rType )
231 {
232 ::osl::ResettableGuard < ::osl::Mutex > aGuard( GetMutex() );
233 const sal_Bool bIsPopupMenu = IsPopupMenu();
234 aGuard.clear();
235
236 css::uno::Any aRet;
237
238 if ( bIsPopupMenu )
239 aRet = ::cppu::queryInterface( rType,
240 SAL_STATIC_CAST( css::awt::XMenu*, (css::awt::XPopupMenu*) this ),
241 SAL_STATIC_CAST( css::awt::XPopupMenu*, this ),
242 SAL_STATIC_CAST( css::lang::XTypeProvider*, this ),
243 SAL_STATIC_CAST( css::lang::XServiceInfo*, this ),
244 SAL_STATIC_CAST( css::lang::XUnoTunnel*, this ) );
245 else
246 aRet = ::cppu::queryInterface( rType,
247 SAL_STATIC_CAST( css::awt::XMenu*, (css::awt::XMenuBar*) this ),
248 SAL_STATIC_CAST( css::awt::XMenuBar*, this ),
249 SAL_STATIC_CAST( css::lang::XTypeProvider*, this ),
250 SAL_STATIC_CAST( css::lang::XServiceInfo*, this ),
251 SAL_STATIC_CAST( css::lang::XUnoTunnel*, this ) );
252
253 return (aRet.hasValue() ? aRet : OWeakObject::queryInterface( rType ));
254 }
255
256
IMPL_XUNOTUNNEL(VCLXMenu)257 IMPL_XUNOTUNNEL( VCLXMenu )
258
259
260 css::uno::Sequence< css::uno::Type > VCLXMenu::getTypes()
261 {
262 ::osl::ResettableGuard < ::osl::Mutex > aGuard( GetMutex() );
263 const sal_Bool bIsPopupMenu = IsPopupMenu();
264 aGuard.clear();
265
266 static ::cppu::OTypeCollection* pCollectionMenuBar = NULL;
267 static ::cppu::OTypeCollection* pCollectionPopupMenu = NULL;
268
269 if ( bIsPopupMenu )
270 {
271 if( !pCollectionPopupMenu )
272 {
273 ::osl::Guard< ::osl::Mutex > aGlobalGuard( ::osl::Mutex::getGlobalMutex() );
274 if( !pCollectionPopupMenu )
275 {
276 static ::cppu::OTypeCollection collectionPopupMenu(
277 getCppuType( ( css::uno::Reference< css::lang::XTypeProvider>* ) NULL ),
278 getCppuType( ( css::uno::Reference< css::awt::XMenu>* ) NULL ),
279 getCppuType( ( css::uno::Reference< css::awt::XPopupMenu>* ) NULL ),
280 getCppuType( ( css::uno::Reference< css::lang::XServiceInfo>* ) NULL ) );
281 pCollectionPopupMenu = &collectionPopupMenu;
282 }
283 }
284
285 return (*pCollectionPopupMenu).getTypes();
286 }
287 else
288 {
289 if( !pCollectionMenuBar )
290 {
291 ::osl::Guard< ::osl::Mutex > aGlobalGuard( ::osl::Mutex::getGlobalMutex() );
292 if( !pCollectionMenuBar )
293 {
294 static ::cppu::OTypeCollection collectionMenuBar(
295 getCppuType( ( css::uno::Reference< css::lang::XTypeProvider>* ) NULL ),
296 getCppuType( ( css::uno::Reference< css::awt::XMenu>* ) NULL ),
297 getCppuType( ( css::uno::Reference< css::awt::XMenuBar>* ) NULL ),
298 getCppuType( ( css::uno::Reference< css::lang::XServiceInfo>* ) NULL ) );
299 pCollectionMenuBar = &collectionMenuBar;
300 }
301 }
302 return (*pCollectionMenuBar).getTypes();
303 }
304 }
305
306
getImplementationId()307 css::uno::Sequence< sal_Int8 > VCLXMenu::getImplementationId()
308 {
309 ::osl::ResettableGuard < ::osl::Mutex > aGuard( GetMutex() );
310 const sal_Bool bIsPopupMenu = IsPopupMenu();
311 aGuard.clear();
312
313 static ::cppu::OImplementationId* pIdMenuBar = NULL;
314 static ::cppu::OImplementationId* pIdPopupMenu = NULL;
315
316 if ( bIsPopupMenu )
317 {
318 if( !pIdPopupMenu )
319 {
320 ::osl::Guard< ::osl::Mutex > aGlobalGuard( ::osl::Mutex::getGlobalMutex() );
321 if( !pIdPopupMenu )
322 {
323 static ::cppu::OImplementationId idPopupMenu( sal_False );
324 pIdPopupMenu = &idPopupMenu;
325 }
326 }
327
328 return (*pIdPopupMenu).getImplementationId();
329 }
330 else
331 {
332 if( !pIdMenuBar )
333 {
334 ::osl::Guard< ::osl::Mutex > aGlobalGuard( ::osl::Mutex::getGlobalMutex() );
335 if( !pIdMenuBar )
336 {
337 static ::cppu::OImplementationId idMenuBar( sal_False );
338 pIdMenuBar = &idMenuBar;
339 }
340 }
341
342 return (*pIdMenuBar).getImplementationId();
343 }
344 }
345
addMenuListener(const css::uno::Reference<css::awt::XMenuListener> & rxListener)346 void VCLXMenu::addMenuListener(
347 const css::uno::Reference< css::awt::XMenuListener >& rxListener )
348 {
349 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
350
351 maMenuListeners.addInterface( rxListener );
352 }
353
removeMenuListener(const css::uno::Reference<css::awt::XMenuListener> & rxListener)354 void VCLXMenu::removeMenuListener(
355 const css::uno::Reference< css::awt::XMenuListener >& rxListener )
356 {
357 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
358
359 maMenuListeners.removeInterface( rxListener );
360 }
361
insertItem(sal_Int16 nItemId,const OUString & aText,sal_Int16 nItemStyle,sal_Int16 nPos)362 void VCLXMenu::insertItem(
363 sal_Int16 nItemId,
364 const OUString& aText,
365 sal_Int16 nItemStyle,
366 sal_Int16 nPos )
367 {
368 ::vos::OGuard aSolarGuard( Application::GetSolarMutex() );
369 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
370
371 if ( mpMenu )
372 mpMenu->InsertItem( nItemId, aText, (MenuItemBits)nItemStyle, nPos );
373 }
374
removeItem(sal_Int16 nPos,sal_Int16 nCount)375 void VCLXMenu::removeItem(
376 sal_Int16 nPos,
377 sal_Int16 nCount )
378 {
379 ::vos::OGuard aSolarGuard( Application::GetSolarMutex() );
380 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
381
382 sal_Int32 nItemCount = (sal_Int32)mpMenu->GetItemCount();
383 if ( mpMenu && ( nCount > 0 ) && ( nPos >= 0 ) && ( nPos < nItemCount ) && ( nItemCount > 0 ))
384 {
385 sal_Int16 nP = sal::static_int_cast< sal_Int16 >(
386 Min( (int)(nPos+nCount), (int)nItemCount ));
387 while( nP-nPos > 0 )
388 mpMenu->RemoveItem( --nP );
389 }
390 }
391
getItemCount()392 sal_Int16 VCLXMenu::getItemCount( )
393 {
394 ::vos::OGuard aSolarGuard( Application::GetSolarMutex() );
395 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
396
397 return mpMenu ? mpMenu->GetItemCount() : 0;
398 }
399
getItemId(sal_Int16 nPos)400 sal_Int16 VCLXMenu::getItemId(
401 sal_Int16 nPos )
402 {
403 ::vos::OGuard aSolarGuard( Application::GetSolarMutex() );
404 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
405
406 return mpMenu ? mpMenu->GetItemId( nPos ) : 0;
407 }
408
getItemPos(sal_Int16 nId)409 sal_Int16 VCLXMenu::getItemPos(
410 sal_Int16 nId )
411 {
412 ::vos::OGuard aSolarGuard( Application::GetSolarMutex() );
413 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
414
415 return mpMenu ? mpMenu->GetItemPos( nId ) : 0;
416 }
417
enableItem(sal_Int16 nItemId,sal_Bool bEnable)418 void VCLXMenu::enableItem(
419 sal_Int16 nItemId,
420 sal_Bool bEnable )
421 {
422 ::vos::OGuard aSolarGuard( Application::GetSolarMutex() );
423 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
424
425 if ( mpMenu )
426 mpMenu->EnableItem( nItemId, bEnable );
427 }
428
isItemEnabled(sal_Int16 nItemId)429 sal_Bool VCLXMenu::isItemEnabled(
430 sal_Int16 nItemId )
431 {
432 ::vos::OGuard aSolarGuard( Application::GetSolarMutex() );
433 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
434
435 return mpMenu ? mpMenu->IsItemEnabled( nItemId ) : sal_False;
436 }
437
setItemText(sal_Int16 nItemId,const OUString & aText)438 void VCLXMenu::setItemText(
439 sal_Int16 nItemId,
440 const OUString& aText )
441 {
442 ::vos::OGuard aSolarGuard( Application::GetSolarMutex() );
443 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
444
445 if ( mpMenu )
446 mpMenu->SetItemText( nItemId, aText );
447 }
448
getItemText(sal_Int16 nItemId)449 OUString VCLXMenu::getItemText(
450 sal_Int16 nItemId )
451 {
452 ::vos::OGuard aSolarGuard( Application::GetSolarMutex() );
453 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
454
455 OUString aItemText;
456 if ( mpMenu )
457 aItemText = mpMenu->GetItemText( nItemId );
458 return aItemText;
459 }
460
setPopupMenu(sal_Int16 nItemId,const css::uno::Reference<css::awt::XPopupMenu> & rxPopupMenu)461 void VCLXMenu::setPopupMenu(
462 sal_Int16 nItemId,
463 const css::uno::Reference< css::awt::XPopupMenu >& rxPopupMenu )
464 {
465 ::vos::OGuard aSolarGuard( Application::GetSolarMutex() );
466 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
467
468 VCLXMenu* pVCLMenu = VCLXMenu::GetImplementation( rxPopupMenu );
469 DBG_ASSERT( pVCLMenu && pVCLMenu->GetMenu() && pVCLMenu->IsPopupMenu(), "setPopupMenu: Invalid Menu!" );
470
471 if ( mpMenu && pVCLMenu && pVCLMenu->GetMenu() && pVCLMenu->IsPopupMenu() )
472 {
473 // Selbst eine Ref halten!
474 css::uno::Reference< css::awt::XPopupMenu > * pNewRef = new css::uno::Reference< css::awt::XPopupMenu > ;
475 *pNewRef = rxPopupMenu;
476 maPopupMenueRefs.Insert( pNewRef, LIST_APPEND );
477
478 mpMenu->SetPopupMenu( nItemId, (PopupMenu*) pVCLMenu->GetMenu() );
479 }
480 }
481
getPopupMenu(sal_Int16 nItemId)482 css::uno::Reference< css::awt::XPopupMenu > VCLXMenu::getPopupMenu(
483 sal_Int16 nItemId )
484 {
485 ::vos::OGuard aSolarGuard( Application::GetSolarMutex() );
486 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
487
488 css::uno::Reference< css::awt::XPopupMenu > aRef;
489 Menu* pMenu = mpMenu ? mpMenu->GetPopupMenu( nItemId ) : NULL;
490 if ( pMenu )
491 {
492 for ( sal_uInt32 n = maPopupMenueRefs.Count(); n; )
493 {
494 css::uno::Reference< css::awt::XPopupMenu > * pRef = maPopupMenueRefs.GetObject( --n );
495 Menu* pM = ((VCLXMenu*)pRef->get())->GetMenu();
496 if ( pM == pMenu )
497 {
498 aRef = *pRef;
499 break;
500 }
501 }
502 }
503 return aRef;
504 }
505
506 // css::awt::XPopupMenu
insertSeparator(sal_Int16 nPos)507 void VCLXMenu::insertSeparator(
508 sal_Int16 nPos )
509 {
510 ::vos::OGuard aSolarGuard( Application::GetSolarMutex() );
511 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
512
513 if ( mpMenu )
514 mpMenu->InsertSeparator( nPos );
515 }
516
setDefaultItem(sal_Int16 nItemId)517 void VCLXMenu::setDefaultItem(
518 sal_Int16 nItemId )
519 {
520 ::vos::OGuard aSolarGuard( Application::GetSolarMutex() );
521 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
522
523 if ( mpMenu )
524 mpMenu->SetDefaultItem( nItemId );
525 }
526
getDefaultItem()527 sal_Int16 VCLXMenu::getDefaultItem( )
528 {
529 ::vos::OGuard aSolarGuard( Application::GetSolarMutex() );
530 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
531
532 return mpMenu ? mpMenu->GetDefaultItem() : 0;
533 }
534
checkItem(sal_Int16 nItemId,sal_Bool bCheck)535 void VCLXMenu::checkItem(
536 sal_Int16 nItemId,
537 sal_Bool bCheck )
538 {
539 ::vos::OGuard aSolarGuard( Application::GetSolarMutex() );
540 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
541
542 if ( mpMenu )
543 mpMenu->CheckItem( nItemId, bCheck );
544 }
545
isItemChecked(sal_Int16 nItemId)546 sal_Bool VCLXMenu::isItemChecked(
547 sal_Int16 nItemId )
548 {
549 ::vos::OGuard aSolarGuard( Application::GetSolarMutex() );
550 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
551
552 return mpMenu ? mpMenu->IsItemChecked( nItemId ) : sal_False;
553 }
554
execute(const css::uno::Reference<css::awt::XWindowPeer> & rxWindowPeer,const css::awt::Rectangle & rPos,sal_Int16 nFlags)555 sal_Int16 VCLXMenu::execute(
556 const css::uno::Reference< css::awt::XWindowPeer >& rxWindowPeer,
557 const css::awt::Rectangle& rPos,
558 sal_Int16 nFlags )
559 {
560 ::vos::OGuard aSolarGuard( Application::GetSolarMutex() );
561 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
562
563 sal_Int16 nRet = 0;
564 if ( mpMenu && IsPopupMenu() )
565 {
566 nRet = ((PopupMenu*)mpMenu)->Execute( VCLUnoHelper::GetWindow( rxWindowPeer ),
567 VCLRectangle( rPos ),
568 nFlags | POPUPMENU_NOMOUSEUPCLOSE );
569 }
570 return nRet;
571 }
572
573
setCommand(sal_Int16 nItemId,const OUString & aCommand)574 void SAL_CALL VCLXMenu::setCommand(
575 sal_Int16 nItemId,
576 const OUString& aCommand )
577 {
578 ::vos::OGuard aSolarGuard( Application::GetSolarMutex() );
579 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
580
581 if ( mpMenu )
582 mpMenu->SetItemCommand( nItemId, aCommand );
583 }
584
getCommand(sal_Int16 nItemId)585 OUString SAL_CALL VCLXMenu::getCommand(
586 sal_Int16 nItemId )
587 {
588 ::vos::OGuard aSolarGuard( Application::GetSolarMutex() );
589 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
590
591 OUString aItemCommand;
592 if ( mpMenu )
593 aItemCommand = mpMenu->GetItemCommand( nItemId );
594 return aItemCommand;
595 }
596
setHelpCommand(sal_Int16 nItemId,const OUString & aHelp)597 void SAL_CALL VCLXMenu::setHelpCommand(
598 sal_Int16 nItemId,
599 const OUString& aHelp )
600 {
601 ::vos::OGuard aSolarGuard( Application::GetSolarMutex() );
602 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
603
604 if ( mpMenu )
605 mpMenu->SetHelpCommand( nItemId, aHelp );
606 }
607
getHelpCommand(sal_Int16 nItemId)608 OUString SAL_CALL VCLXMenu::getHelpCommand(
609 sal_Int16 nItemId )
610 {
611 ::vos::OGuard aSolarGuard( Application::GetSolarMutex() );
612 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
613
614 OUString aHelpCommand;
615 if ( mpMenu )
616 aHelpCommand = mpMenu->GetHelpCommand( nItemId );
617 return aHelpCommand;
618 }
619
620
621 namespace
622 {
lcl_XGraphic2VCLImage(const css::uno::Reference<css::graphic::XGraphic> & xGraphic,sal_Bool bResize)623 static Image lcl_XGraphic2VCLImage(
624 const css::uno::Reference< css::graphic::XGraphic >& xGraphic,
625 sal_Bool bResize )
626 {
627 Image aImage;
628 if ( !xGraphic.is() )
629 return aImage;
630
631 aImage = Image( xGraphic );
632 const ::Size aCurSize = aImage.GetSizePixel();
633 const sal_Int32 nCurWidth = aCurSize.Width();
634 const sal_Int32 nCurHeight = aCurSize.Height();
635 const sal_Int32 nIdeal( 16 );
636
637 if ( nCurWidth > 0 && nCurHeight > 0 )
638 {
639 if ( bResize && ( nCurWidth > nIdeal || nCurHeight > nIdeal ) )
640 {
641 sal_Int32 nIdealWidth = nCurWidth > nIdeal ? nIdeal : nCurWidth;
642 sal_Int32 nIdealHeight = nCurHeight > nIdeal ? nIdeal : nCurHeight;
643
644 ::Size aNewSize( nIdealWidth, nIdealHeight );
645
646 sal_Bool bModified( sal_False );
647 BitmapEx aBitmapEx = aImage.GetBitmapEx();
648 bModified = aBitmapEx.Scale( aNewSize, BMP_SCALE_INTERPOLATE );
649
650 if ( bModified )
651 aImage = Image( aBitmapEx );
652 }
653 }
654 return aImage;
655 }
656
657 /** Copied from svtools/inc/acceleratorexecute.hxx */
lcl_VCLKey2AWTKey(const KeyCode & aVCLKey)658 static css::awt::KeyEvent lcl_VCLKey2AWTKey(
659 const KeyCode& aVCLKey)
660 {
661 css::awt::KeyEvent aAWTKey;
662 aAWTKey.Modifiers = 0;
663 aAWTKey.KeyCode = (sal_Int16)aVCLKey.GetCode();
664
665 if (aVCLKey.IsShift())
666 aAWTKey.Modifiers |= css::awt::KeyModifier::SHIFT;
667 if (aVCLKey.IsMod1())
668 aAWTKey.Modifiers |= css::awt::KeyModifier::MOD1;
669 if (aVCLKey.IsMod2())
670 aAWTKey.Modifiers |= css::awt::KeyModifier::MOD2;
671 if (aVCLKey.IsMod3())
672 aAWTKey.Modifiers |= css::awt::KeyModifier::MOD3;
673
674 return aAWTKey;
675 }
676
lcl_AWTKey2VCLKey(const css::awt::KeyEvent & aAWTKey)677 KeyCode lcl_AWTKey2VCLKey(const css::awt::KeyEvent& aAWTKey)
678 {
679 sal_Bool bShift = ((aAWTKey.Modifiers & css::awt::KeyModifier::SHIFT) == css::awt::KeyModifier::SHIFT );
680 sal_Bool bMod1 = ((aAWTKey.Modifiers & css::awt::KeyModifier::MOD1 ) == css::awt::KeyModifier::MOD1 );
681 sal_Bool bMod2 = ((aAWTKey.Modifiers & css::awt::KeyModifier::MOD2 ) == css::awt::KeyModifier::MOD2 );
682 sal_Bool bMod3 = ((aAWTKey.Modifiers & css::awt::KeyModifier::MOD3 ) == css::awt::KeyModifier::MOD3 );
683 sal_uInt16 nKey = (sal_uInt16)aAWTKey.KeyCode;
684
685 return KeyCode(nKey, bShift, bMod1, bMod2, bMod3);
686 }
687
688 }
689
690
isPopupMenu()691 ::sal_Bool SAL_CALL VCLXMenu::isPopupMenu( )
692 {
693 ::vos::OGuard aSolarGuard( Application::GetSolarMutex() );
694 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
695 return IsPopupMenu();
696 }
697
clear()698 void SAL_CALL VCLXMenu::clear( )
699 {
700 ::vos::OGuard aSolarGuard( Application::GetSolarMutex() );
701 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
702 if ( mpMenu )
703 mpMenu->Clear();
704 }
705
706
getItemType(::sal_Int16 nItemPos)707 css::awt::MenuItemType SAL_CALL VCLXMenu::getItemType(
708 ::sal_Int16 nItemPos )
709 {
710 ::vos::OGuard aSolarGuard( Application::GetSolarMutex() );
711 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
712
713 css::awt::MenuItemType aMenuItemType =
714 css::awt::MenuItemType_DONTKNOW;
715 if ( mpMenu )
716 {
717 aMenuItemType = ( (css::awt::MenuItemType) mpMenu->GetItemType( nItemPos ) );
718 }
719
720 return aMenuItemType;
721 }
722
hideDisabledEntries(::sal_Bool bHide)723 void SAL_CALL VCLXMenu::hideDisabledEntries(
724 ::sal_Bool bHide )
725 {
726 ::vos::OGuard aSolarGuard( Application::GetSolarMutex() );
727 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
728 if ( mpMenu )
729 {
730 if ( bHide )
731 mpMenu->SetMenuFlags( mpMenu->GetMenuFlags() | MENU_FLAG_HIDEDISABLEDENTRIES );
732 else
733 mpMenu->SetMenuFlags( mpMenu->GetMenuFlags() & ~MENU_FLAG_HIDEDISABLEDENTRIES );
734 }
735 }
736
737
isInExecute()738 ::sal_Bool SAL_CALL VCLXMenu::isInExecute( )
739 {
740 ::vos::OGuard aSolarGuard( Application::GetSolarMutex() );
741 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
742
743 if ( mpMenu && IsPopupMenu() )
744 return ( (PopupMenu*) mpMenu )->IsInExecute();
745 else
746 return sal_False;
747 }
748
749
endExecute()750 void SAL_CALL VCLXMenu::endExecute()
751 {
752 ::vos::OGuard aSolarGuard( Application::GetSolarMutex() );
753 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
754
755 if ( mpMenu && IsPopupMenu() )
756 ( (PopupMenu*) mpMenu )->EndExecute();
757 }
758
759
enableAutoMnemonics(::sal_Bool bEnable)760 void SAL_CALL VCLXMenu::enableAutoMnemonics(
761 ::sal_Bool bEnable )
762 {
763 ::vos::OGuard aSolarGuard( Application::GetSolarMutex() );
764 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
765 if ( mpMenu )
766 {
767 if ( !bEnable )
768 mpMenu->SetMenuFlags( mpMenu->GetMenuFlags() | MENU_FLAG_NOAUTOMNEMONICS );
769 else
770 mpMenu->SetMenuFlags( mpMenu->GetMenuFlags() & ~MENU_FLAG_NOAUTOMNEMONICS );
771 }
772 }
773
774
setAcceleratorKeyEvent(::sal_Int16 nItemId,const css::awt::KeyEvent & aKeyEvent)775 void SAL_CALL VCLXMenu::setAcceleratorKeyEvent(
776 ::sal_Int16 nItemId,
777 const css::awt::KeyEvent& aKeyEvent )
778 {
779 ::vos::OGuard aSolarGuard( Application::GetSolarMutex() );
780 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
781
782 if ( mpMenu && IsPopupMenu() && MENU_ITEM_NOTFOUND != mpMenu->GetItemPos( nItemId ) )
783 {
784 KeyCode aVCLKeyCode = lcl_AWTKey2VCLKey( aKeyEvent );
785 mpMenu->SetAccelKey( nItemId, aVCLKeyCode );
786 }
787 }
788
789
getAcceleratorKeyEvent(::sal_Int16 nItemId)790 css::awt::KeyEvent SAL_CALL VCLXMenu::getAcceleratorKeyEvent(
791 ::sal_Int16 nItemId )
792 {
793 ::vos::OGuard aSolarGuard( Application::GetSolarMutex() );
794 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
795
796 css::awt::KeyEvent aKeyEvent;
797 if ( mpMenu && IsPopupMenu() && MENU_ITEM_NOTFOUND != mpMenu->GetItemPos( nItemId ) )
798 {
799 KeyCode nKeyCode = mpMenu->GetAccelKey( nItemId );
800 aKeyEvent = lcl_VCLKey2AWTKey( nKeyCode );
801 }
802
803 return aKeyEvent;
804 }
805
806
setHelpText(::sal_Int16 nItemId,const OUString & sHelpText)807 void SAL_CALL VCLXMenu::setHelpText(
808 ::sal_Int16 nItemId,
809 const OUString& sHelpText )
810 {
811 ::vos::OGuard aSolarGuard( Application::GetSolarMutex() );
812 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
813
814 if ( mpMenu && MENU_ITEM_NOTFOUND != mpMenu->GetItemPos( nItemId ) )
815 {
816 mpMenu->SetHelpText( nItemId, sHelpText );
817 }
818 }
819
820
getHelpText(::sal_Int16 nItemId)821 OUString SAL_CALL VCLXMenu::getHelpText(
822 ::sal_Int16 nItemId )
823 {
824 ::vos::OGuard aSolarGuard( Application::GetSolarMutex() );
825 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
826
827 rtl::OUString sHelpText;
828 if ( mpMenu && MENU_ITEM_NOTFOUND != mpMenu->GetItemPos( nItemId ) )
829 {
830 sHelpText = mpMenu->GetHelpText( nItemId );
831 }
832
833 return sHelpText;
834 }
835
836
setTipHelpText(::sal_Int16 nItemId,const OUString & sTipHelpText)837 void SAL_CALL VCLXMenu::setTipHelpText(
838 ::sal_Int16 nItemId,
839 const OUString& sTipHelpText )
840 {
841 ::vos::OGuard aSolarGuard( Application::GetSolarMutex() );
842 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
843
844 if ( mpMenu && MENU_ITEM_NOTFOUND != mpMenu->GetItemPos( nItemId ) )
845 {
846 mpMenu->SetTipHelpText( nItemId, sTipHelpText );
847 }
848 }
849
850
getTipHelpText(::sal_Int16 nItemId)851 OUString SAL_CALL VCLXMenu::getTipHelpText(
852 ::sal_Int16 nItemId )
853 {
854 ::vos::OGuard aSolarGuard( Application::GetSolarMutex() );
855 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
856
857 rtl::OUString sTipHelpText;
858 if ( mpMenu && MENU_ITEM_NOTFOUND != mpMenu->GetItemPos( nItemId ) )
859 {
860 sTipHelpText = mpMenu->GetTipHelpText( nItemId );
861 }
862 return sTipHelpText;
863 }
864
865
setItemImage(::sal_Int16 nItemId,const css::uno::Reference<css::graphic::XGraphic> & xGraphic,::sal_Bool bScale)866 void SAL_CALL VCLXMenu::setItemImage(
867 ::sal_Int16 nItemId,
868 const css::uno::Reference< css::graphic::XGraphic >& xGraphic,
869 ::sal_Bool bScale )
870 {
871 ::vos::OGuard aSolarGuard( Application::GetSolarMutex() );
872 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
873
874 if ( mpMenu && IsPopupMenu() && MENU_ITEM_NOTFOUND != mpMenu->GetItemPos( nItemId ) )
875 {
876 Image aImage = lcl_XGraphic2VCLImage( xGraphic, bScale );
877 mpMenu->SetItemImage( nItemId, aImage );
878 }
879 }
880
881
882 css::uno::Reference< css::graphic::XGraphic > SAL_CALL
getItemImage(::sal_Int16 nItemId)883 VCLXMenu::getItemImage(
884 ::sal_Int16 nItemId )
885 {
886 ::vos::OGuard aSolarGuard( Application::GetSolarMutex() );
887 ::osl::Guard< ::osl::Mutex > aGuard( GetMutex() );
888
889 css::uno::Reference< css::graphic::XGraphic > rxGraphic;
890
891 if ( mpMenu && IsPopupMenu() && MENU_ITEM_NOTFOUND != mpMenu->GetItemPos( nItemId ) )
892 {
893 Image aImage = mpMenu->GetItemImage( nItemId );
894 if ( !!aImage )
895 rxGraphic = aImage.GetXGraphic();
896 }
897 return rxGraphic;
898 }
899
900
901
902 DBG_NAME(VCLXMenuBar);
903
VCLXMenuBar()904 VCLXMenuBar::VCLXMenuBar()
905 {
906 DBG_CTOR( VCLXMenuBar, 0 );
907 ImplCreateMenu( sal_False );
908 }
909
VCLXMenuBar(MenuBar * pMenuBar)910 VCLXMenuBar::VCLXMenuBar( MenuBar* pMenuBar ) : VCLXMenu( (Menu *)pMenuBar )
911 {
912 DBG_CTOR( VCLXMenuBar, 0 );
913 }
914
915
916 DBG_NAME(VCLXPopupMenu);
917
VCLXPopupMenu()918 VCLXPopupMenu::VCLXPopupMenu()
919 {
920 DBG_CTOR( VCLXPopupMenu, 0 );
921 ImplCreateMenu( sal_True );
922 }
923