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 #include "vbamenuitems.hxx"
10 #include "vbamenuitem.hxx"
11 #include "vbamenu.hxx"
12 #include <ooo/vba/office/MsoControlType.hpp>
14 using namespace com::sun::star
;
15 using namespace ooo::vba
;
17 typedef ::cppu::WeakImplHelper1
< container::XEnumeration
> MenuEnumeration_BASE
;
19 class MenuEnumeration
: public MenuEnumeration_BASE
21 uno::Reference
< XHelperInterface
> m_xParent
;
22 uno::Reference
< uno::XComponentContext
> m_xContext
;
23 uno::Reference
< container::XEnumeration
> m_xEnumeration
;
25 MenuEnumeration( const uno::Reference
< XHelperInterface
>& xParent
, const uno::Reference
< uno::XComponentContext
>& xContext
, const uno::Reference
< container::XEnumeration
>& xEnumeration
) throw ( uno::RuntimeException
) : m_xParent( xParent
), m_xContext( xContext
), m_xEnumeration( xEnumeration
)
28 virtual sal_Bool SAL_CALL
hasMoreElements() throw ( uno::RuntimeException
, std::exception
) SAL_OVERRIDE
30 return m_xEnumeration
->hasMoreElements();
32 virtual uno::Any SAL_CALL
nextElement() throw ( container::NoSuchElementException
, lang::WrappedTargetException
, uno::RuntimeException
, std::exception
) SAL_OVERRIDE
34 // FIXME: should be add menu
35 if( hasMoreElements() )
37 uno::Reference
< XCommandBarControl
> xCommandBarControl( m_xEnumeration
->nextElement(), uno::UNO_QUERY_THROW
);
38 if( xCommandBarControl
->getType() == office::MsoControlType::msoControlPopup
)
40 uno::Reference
< excel::XMenu
> xMenu( new ScVbaMenu( m_xParent
, m_xContext
, xCommandBarControl
) );
41 return uno::makeAny( xMenu
);
43 else if( xCommandBarControl
->getType() == office::MsoControlType::msoControlButton
)
45 uno::Reference
< excel::XMenuItem
> xMenuItem( new ScVbaMenuItem( m_xParent
, m_xContext
, xCommandBarControl
) );
46 return uno::makeAny( xMenuItem
);
51 throw container::NoSuchElementException();
56 ScVbaMenuItems::ScVbaMenuItems( const uno::Reference
< XHelperInterface
>& xParent
, const uno::Reference
< uno::XComponentContext
>& xContext
, const uno::Reference
< XCommandBarControls
>& xCommandBarControls
) throw ( uno::RuntimeException
) : MenuItems_BASE( xParent
, xContext
, uno::Reference
< container::XIndexAccess
>() ), m_xCommandBarControls( xCommandBarControls
)
62 ScVbaMenuItems::getElementType() throw ( uno::RuntimeException
)
64 return cppu::UnoType
<excel::XMenuItem
>::get();
67 uno::Reference
< container::XEnumeration
>
68 ScVbaMenuItems::createEnumeration() throw ( uno::RuntimeException
)
70 uno::Reference
< container::XEnumerationAccess
> xEnumAccess( m_xCommandBarControls
, uno::UNO_QUERY_THROW
);
71 return uno::Reference
< container::XEnumeration
>( new MenuEnumeration( this, mxContext
, xEnumAccess
->createEnumeration() ) );
75 ScVbaMenuItems::createCollectionObject( const uno::Any
& aSource
)
82 ScVbaMenuItems::getCount() throw(css::uno::RuntimeException
)
84 // FIXME: should check if it is a popup menu
85 return m_xCommandBarControls
->getCount();
88 // ScVbaCollectionBaseImpl
90 ScVbaMenuItems::Item( const uno::Any
& aIndex
, const uno::Any
& /*aIndex2*/ ) throw( uno::RuntimeException
)
92 uno::Reference
< XCommandBarControl
> xCommandBarControl( m_xCommandBarControls
->Item( aIndex
, uno::Any() ), uno::UNO_QUERY_THROW
);
93 if( xCommandBarControl
->getType() == office::MsoControlType::msoControlPopup
)
94 return uno::makeAny( uno::Reference
< excel::XMenu
> ( new ScVbaMenu( this, mxContext
, xCommandBarControl
) ) );
95 else if( xCommandBarControl
->getType() == office::MsoControlType::msoControlButton
)
96 return uno::makeAny( uno::Reference
< excel::XMenuItem
> ( new ScVbaMenuItem( this, mxContext
, xCommandBarControl
) ) );
97 throw uno::RuntimeException();
100 uno::Reference
< excel::XMenuItem
> SAL_CALL
ScVbaMenuItems::Add( const OUString
& Caption
, const css::uno::Any
& OnAction
, const css::uno::Any
& /*ShortcutKey*/, const css::uno::Any
& Before
, const css::uno::Any
& Restore
, const css::uno::Any
& /*StatusBar*/, const css::uno::Any
& /*HelpFile*/, const css::uno::Any
& /*HelpContextID*/ ) throw (css::script::BasicErrorException
, css::uno::RuntimeException
, std::exception
)
102 sal_Int32 nType
= office::MsoControlType::msoControlButton
;
103 uno::Reference
< XCommandBarControl
> xCommandBarControl
= m_xCommandBarControls
->Add( uno::makeAny( nType
), uno::Any(), uno::Any(), Before
, Restore
);
104 xCommandBarControl
->setCaption( Caption
);
105 if( OnAction
.hasValue() )
108 OnAction
>>= sAction
;
109 xCommandBarControl
->setOnAction( sAction
);
111 return uno::Reference
< excel::XMenuItem
>( new ScVbaMenuItem( this, mxContext
, xCommandBarControl
) );
116 ScVbaMenuItems::getServiceImplName()
118 return OUString("ScVbaMenuItems");
121 uno::Sequence
<OUString
>
122 ScVbaMenuItems::getServiceNames()
124 static uno::Sequence
< OUString
> aServiceNames
;
125 if ( aServiceNames
.getLength() == 0 )
127 aServiceNames
.realloc( 1 );
128 aServiceNames
[ 0 ] = "ooo.vba.excel.MenuItems";
130 return aServiceNames
;
133 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */