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