1 /*************************************************************************
3 * OpenOffice.org - a multi-platform office productivity suite
9 * last change: $Author$ $Date$
11 * The Contents of this file are made available subject to
12 * the terms of GNU Lesser General Public License Version 2.1.
15 * GNU Lesser General Public License Version 2.1
16 * =============================================
17 * Copyright 2005 by Sun Microsystems, Inc.
18 * 901 San Antonio Road, Palo Alto, CA 94303, USA
20 * This library is free software; you can redistribute it and/or
21 * modify it under the terms of the GNU Lesser General Public
22 * License version 2.1, as published by the Free Software Foundation.
24 * This library is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 * Lesser General Public License for more details.
29 * You should have received a copy of the GNU Lesser General Public
30 * License along with this library; if not, write to the Free Software
31 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
34 ************************************************************************/
35 #include "vbamenuitems.hxx"
36 #include "vbamenuitem.hxx"
37 #include "vbamenu.hxx"
38 #include <ooo/vba/office/MsoControlType.hpp>
40 using namespace com::sun::star
;
41 using namespace ooo::vba
;
44 typedef ::cppu::WeakImplHelper1
< container::XEnumeration
> MenuEnumeration_BASE
;
46 class MenuEnumeration
: public MenuEnumeration_BASE
48 uno::Reference
< XHelperInterface
> m_xParent
;
49 uno::Reference
< uno::XComponentContext
> m_xContext
;
50 uno::Reference
< container::XEnumeration
> m_xEnumeration
;
52 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
)
55 virtual sal_Bool SAL_CALL
hasMoreElements() throw ( uno::RuntimeException
)
57 return m_xEnumeration
->hasMoreElements();
59 virtual uno::Any SAL_CALL
nextElement() throw ( container::NoSuchElementException
, lang::WrappedTargetException
, uno::RuntimeException
)
61 // FIXME: should be add menu
62 if( hasMoreElements() )
64 uno::Reference
< XCommandBarControl
> xCommandBarControl( m_xEnumeration
->nextElement(), uno::UNO_QUERY_THROW
);
65 if( xCommandBarControl
->getType() == office::MsoControlType::msoControlPopup
)
67 uno::Reference
< excel::XMenu
> xMenu( new ScVbaMenu( m_xParent
, m_xContext
, xCommandBarControl
) );
68 return uno::makeAny( xMenu
);
70 else if( xCommandBarControl
->getType() == office::MsoControlType::msoControlButton
)
72 uno::Reference
< excel::XMenuItem
> xMenuItem( new ScVbaMenuItem( m_xParent
, m_xContext
, xCommandBarControl
) );
73 return uno::makeAny( xMenuItem
);
78 throw container::NoSuchElementException();
83 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
)
89 ScVbaMenuItems::getElementType() throw ( uno::RuntimeException
)
91 return excel::XMenuItem::static_type( 0 );
94 uno::Reference
< container::XEnumeration
>
95 ScVbaMenuItems::createEnumeration() throw ( uno::RuntimeException
)
97 uno::Reference
< container::XEnumerationAccess
> xEnumAccess( m_xCommandBarControls
, uno::UNO_QUERY_THROW
);
98 return uno::Reference
< container::XEnumeration
>( new MenuEnumeration( this, mxContext
, xEnumAccess
->createEnumeration() ) );
102 ScVbaMenuItems::createCollectionObject( const uno::Any
& aSource
)
109 ScVbaMenuItems::getCount() throw(css::uno::RuntimeException
)
111 // FIXME: should check if it is a popup menu
112 return m_xCommandBarControls
->getCount();
115 // ScVbaCollectionBaseImpl
117 ScVbaMenuItems::Item( const uno::Any
& aIndex
, const uno::Any
& /*aIndex2*/ ) throw( uno::RuntimeException
)
119 uno::Reference
< XCommandBarControl
> xCommandBarControl( m_xCommandBarControls
->Item( aIndex
, uno::Any() ), uno::UNO_QUERY_THROW
);
120 if( xCommandBarControl
->getType() == office::MsoControlType::msoControlPopup
)
121 return uno::makeAny( uno::Reference
< excel::XMenu
> ( new ScVbaMenu( this, mxContext
, xCommandBarControl
) ) );
122 else if( xCommandBarControl
->getType() == office::MsoControlType::msoControlButton
)
123 return uno::makeAny( uno::Reference
< excel::XMenuItem
> ( new ScVbaMenuItem( this, mxContext
, xCommandBarControl
) ) );
124 throw uno::RuntimeException();
127 uno::Reference
< excel::XMenuItem
> SAL_CALL
ScVbaMenuItems::Add( const rtl::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
)
129 sal_Int32 nType
= office::MsoControlType::msoControlButton
;
130 uno::Reference
< XCommandBarControl
> xCommandBarControl
= m_xCommandBarControls
->Add( uno::makeAny( nType
), uno::Any(), uno::Any(), Before
, Restore
);
131 xCommandBarControl
->setCaption( Caption
);
132 if( OnAction
.hasValue() )
134 rtl::OUString sAction
;
135 OnAction
>>= sAction
;
136 xCommandBarControl
->setOnAction( sAction
);
138 return uno::Reference
< excel::XMenuItem
>( new ScVbaMenuItem( this, mxContext
, xCommandBarControl
) );
143 ScVbaMenuItems::getServiceImplName()
145 static rtl::OUString
sImplName( RTL_CONSTASCII_USTRINGPARAM("ScVbaMenuItems") );
148 uno::Sequence
<rtl::OUString
>
149 ScVbaMenuItems::getServiceNames()
151 static uno::Sequence
< rtl::OUString
> aServiceNames
;
152 if ( aServiceNames
.getLength() == 0 )
154 aServiceNames
.realloc( 1 );
155 aServiceNames
[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.excel.MenuItems" ) );
157 return aServiceNames
;