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 #include "vbamenus.hxx"
10 #include "vbamenu.hxx"
11 #include <cppuhelper/implbase.hxx>
12 #include <ooo/vba/office/MsoControlType.hpp>
13 #include <ooo/vba/XCommandBarControls.hpp>
14 
15 using namespace com::sun::star;
16 using namespace ooo::vba;
17 
18 typedef ::cppu::WeakImplHelper< container::XEnumeration > MenuEnumeration_BASE;
19 
20 namespace {
21 
22 class MenuEnumeration : public MenuEnumeration_BASE
23 {
24     uno::Reference< XHelperInterface > m_xParent;
25     uno::Reference< uno::XComponentContext > m_xContext;
26     uno::Reference< container::XEnumeration > m_xEnumeration;
27 public:
28     /// @throws uno::RuntimeException
MenuEnumeration(const uno::Reference<XHelperInterface> & xParent,const uno::Reference<uno::XComponentContext> & xContext,const uno::Reference<container::XEnumeration> & xEnumeration)29     MenuEnumeration( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext >& xContext, const uno::Reference< container::XEnumeration >& xEnumeration) : m_xParent( xParent ), m_xContext( xContext ), m_xEnumeration( xEnumeration )
30     {
31     }
hasMoreElements()32     virtual sal_Bool SAL_CALL hasMoreElements() override
33     {
34         return m_xEnumeration->hasMoreElements();
35     }
nextElement()36     virtual uno::Any SAL_CALL nextElement() override
37     {
38         // FIXME: should be add menu
39         if( !hasMoreElements() )
40             throw container::NoSuchElementException();
41 
42         uno::Reference< XCommandBarControl > xCommandBarControl( m_xEnumeration->nextElement(), uno::UNO_QUERY_THROW );
43         if( xCommandBarControl->getType() == office::MsoControlType::msoControlPopup )
44         {
45             uno::Reference< excel::XMenu > xMenu( new ScVbaMenu( m_xParent, m_xContext, xCommandBarControl ) );
46             return uno::makeAny( xMenu );
47         }
48         nextElement();
49 
50         return uno::Any();
51     }
52 };
53 
54 }
55 
ScVbaMenus(const uno::Reference<XHelperInterface> & xParent,const uno::Reference<uno::XComponentContext> & xContext,const uno::Reference<XCommandBarControls> & xCommandBarControls)56 ScVbaMenus::ScVbaMenus( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext >& xContext, const uno::Reference< XCommandBarControls >& xCommandBarControls ) : Menus_BASE( xParent, xContext, uno::Reference< container::XIndexAccess>() ), m_xCommandBarControls( xCommandBarControls )
57 {
58 }
59 
60 // XEnumerationAccess
61 uno::Type SAL_CALL
getElementType()62 ScVbaMenus::getElementType()
63 {
64     return cppu::UnoType<excel::XMenu>::get();
65 }
66 
67 uno::Reference< container::XEnumeration >
createEnumeration()68 ScVbaMenus::createEnumeration()
69 {
70     uno::Reference< container::XEnumerationAccess > xEnumAccess( m_xCommandBarControls, uno::UNO_QUERY_THROW );
71     return uno::Reference< container::XEnumeration >( new MenuEnumeration( this, mxContext, xEnumAccess->createEnumeration() ) );
72 }
73 
74 uno::Any
createCollectionObject(const uno::Any & aSource)75 ScVbaMenus::createCollectionObject( const uno::Any& aSource )
76 {
77     // make no sense
78     return aSource;
79 }
80 
81 sal_Int32 SAL_CALL
getCount()82 ScVbaMenus::getCount()
83 {
84     // FIXME: should check if it is a popup menu
85     return m_xCommandBarControls->getCount();
86 }
87 
88 // ScVbaCollectionBaseImpl
89 uno::Any SAL_CALL
Item(const uno::Any & aIndex,const uno::Any &)90 ScVbaMenus::Item( const uno::Any& aIndex, const uno::Any& /*aIndex2*/ )
91 {
92     uno::Reference< XCommandBarControl > xCommandBarControl( m_xCommandBarControls->Item( aIndex, uno::Any() ), uno::UNO_QUERY_THROW );
93     if( xCommandBarControl->getType() != office::MsoControlType::msoControlPopup )
94         throw uno::RuntimeException();
95     return uno::makeAny( uno::Reference< excel::XMenu > ( new ScVbaMenu( this, mxContext, xCommandBarControl ) ) );
96 }
97 
Add(const OUString & Caption,const css::uno::Any & Before,const css::uno::Any & Restore)98 uno::Reference< excel::XMenu > SAL_CALL ScVbaMenus::Add( const OUString& Caption, const css::uno::Any& Before, const css::uno::Any& Restore )
99 {
100     uno::Reference< XCommandBarControl > xCommandBarControl = m_xCommandBarControls->Add(
101             uno::makeAny( office::MsoControlType::msoControlPopup ),
102             uno::Any(), uno::Any(), Before, Restore );
103     xCommandBarControl->setCaption( Caption );
104     return uno::Reference< excel::XMenu >( new ScVbaMenu( this, mxContext, xCommandBarControl ) );
105 }
106 
107 // XHelperInterface
108 OUString
getServiceImplName()109 ScVbaMenus::getServiceImplName()
110 {
111     return "ScVbaMenus";
112 }
113 
114 uno::Sequence<OUString>
getServiceNames()115 ScVbaMenus::getServiceNames()
116 {
117     static uno::Sequence< OUString > const aServiceNames
118     {
119         "ooo.vba.excel.Menus"
120     };
121     return aServiceNames;
122 }
123 
124 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
125