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 <cppuhelper/implbase.hxx>
13 #include <ooo/vba/office/MsoControlType.hpp>
14 #include <ooo/vba/XCommandBarControls.hpp>
17 using namespace com::sun::star
;
18 using namespace ooo::vba
;
20 typedef ::cppu::WeakImplHelper
< container::XEnumeration
> MenuEnumeration_BASE
;
24 class MenuEnumeration
: public MenuEnumeration_BASE
26 uno::Reference
< XHelperInterface
> m_xParent
;
27 uno::Reference
< uno::XComponentContext
> m_xContext
;
28 uno::Reference
< container::XEnumeration
> m_xEnumeration
;
30 /// @throws uno::RuntimeException
31 MenuEnumeration( uno::Reference
< XHelperInterface
> xParent
, uno::Reference
< uno::XComponentContext
> xContext
, uno::Reference
< container::XEnumeration
> xEnumeration
) : m_xParent(std::move( xParent
)), m_xContext(std::move( xContext
)), m_xEnumeration(std::move( xEnumeration
))
34 virtual sal_Bool SAL_CALL
hasMoreElements() override
36 return m_xEnumeration
->hasMoreElements();
38 virtual uno::Any SAL_CALL
nextElement() override
40 // FIXME: should be add menu
41 if( !hasMoreElements() )
42 throw container::NoSuchElementException();
44 uno::Reference
< XCommandBarControl
> xCommandBarControl( m_xEnumeration
->nextElement(), uno::UNO_QUERY_THROW
);
45 if( xCommandBarControl
->getType() == office::MsoControlType::msoControlPopup
)
47 uno::Reference
< excel::XMenu
> xMenu( new ScVbaMenu( m_xParent
, m_xContext
, xCommandBarControl
) );
48 return uno::Any( xMenu
);
50 else if( xCommandBarControl
->getType() == office::MsoControlType::msoControlButton
)
52 uno::Reference
< excel::XMenuItem
> xMenuItem( new ScVbaMenuItem( m_xParent
, m_xContext
, xCommandBarControl
) );
53 return uno::Any( xMenuItem
);
63 ScVbaMenuItems::ScVbaMenuItems( const uno::Reference
< XHelperInterface
>& xParent
, const uno::Reference
< uno::XComponentContext
>& xContext
, uno::Reference
< XCommandBarControls
> xCommandBarControls
) : MenuItems_BASE( xParent
, xContext
, uno::Reference
< container::XIndexAccess
>() ), m_xCommandBarControls(std::move( xCommandBarControls
))
69 ScVbaMenuItems::getElementType()
71 return cppu::UnoType
<excel::XMenuItem
>::get();
74 uno::Reference
< container::XEnumeration
>
75 ScVbaMenuItems::createEnumeration()
77 uno::Reference
< container::XEnumerationAccess
> xEnumAccess( m_xCommandBarControls
, uno::UNO_QUERY_THROW
);
78 return uno::Reference
< container::XEnumeration
>( new MenuEnumeration( this, mxContext
, xEnumAccess
->createEnumeration() ) );
82 ScVbaMenuItems::createCollectionObject( const uno::Any
& aSource
)
89 ScVbaMenuItems::getCount()
91 // FIXME: should check if it is a popup menu
92 return m_xCommandBarControls
->getCount();
95 // ScVbaCollectionBaseImpl
97 ScVbaMenuItems::Item( const uno::Any
& aIndex
, const uno::Any
& /*aIndex2*/ )
99 uno::Reference
< XCommandBarControl
> xCommandBarControl( m_xCommandBarControls
->Item( aIndex
, uno::Any() ), uno::UNO_QUERY_THROW
);
100 if( xCommandBarControl
->getType() == office::MsoControlType::msoControlPopup
)
101 return uno::Any( uno::Reference
< excel::XMenu
> ( new ScVbaMenu( this, mxContext
, xCommandBarControl
) ) );
102 else if( xCommandBarControl
->getType() == office::MsoControlType::msoControlButton
)
103 return uno::Any( uno::Reference
< excel::XMenuItem
> ( new ScVbaMenuItem( this, mxContext
, xCommandBarControl
) ) );
104 throw uno::RuntimeException();
107 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*/ )
109 uno::Reference
< XCommandBarControl
> xCommandBarControl
= m_xCommandBarControls
->Add(
110 uno::Any( office::MsoControlType::msoControlButton
),
111 uno::Any(), uno::Any(), Before
, Restore
);
112 xCommandBarControl
->setCaption( Caption
);
113 if( OnAction
.hasValue() )
116 OnAction
>>= sAction
;
117 xCommandBarControl
->setOnAction( sAction
);
119 return uno::Reference
< excel::XMenuItem
>( new ScVbaMenuItem( this, mxContext
, xCommandBarControl
) );
124 ScVbaMenuItems::getServiceImplName()
126 return u
"ScVbaMenuItems"_ustr
;
129 uno::Sequence
<OUString
>
130 ScVbaMenuItems::getServiceNames()
132 static uno::Sequence
< OUString
> const aServiceNames
134 u
"ooo.vba.excel.MenuItems"_ustr
136 return aServiceNames
;
139 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */