LibreOffice Module sc (master)  1
vbamenuitems.cxx
Go to the documentation of this file.
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 <cppuhelper/implbase.hxx>
13 #include <ooo/vba/office/MsoControlType.hpp>
14 #include <ooo/vba/XCommandBarControls.hpp>
15 
16 using namespace com::sun::star;
17 using namespace ooo::vba;
18 
19 typedef ::cppu::WeakImplHelper< container::XEnumeration > MenuEnumeration_BASE;
20 
21 namespace {
22 
23 class MenuEnumeration : public MenuEnumeration_BASE
24 {
25  uno::Reference< XHelperInterface > m_xParent;
26  uno::Reference< uno::XComponentContext > m_xContext;
27  uno::Reference< container::XEnumeration > m_xEnumeration;
28 public:
30  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 )
31  {
32  }
33  virtual sal_Bool SAL_CALL hasMoreElements() override
34  {
35  return m_xEnumeration->hasMoreElements();
36  }
37  virtual uno::Any SAL_CALL nextElement() override
38  {
39  // FIXME: should be add menu
40  if( !hasMoreElements() )
41  throw container::NoSuchElementException();
42 
43  uno::Reference< XCommandBarControl > xCommandBarControl( m_xEnumeration->nextElement(), uno::UNO_QUERY_THROW );
44  if( xCommandBarControl->getType() == office::MsoControlType::msoControlPopup )
45  {
46  uno::Reference< excel::XMenu > xMenu( new ScVbaMenu( m_xParent, m_xContext, xCommandBarControl ) );
47  return uno::makeAny( xMenu );
48  }
49  else if( xCommandBarControl->getType() == office::MsoControlType::msoControlButton )
50  {
51  uno::Reference< excel::XMenuItem > xMenuItem( new ScVbaMenuItem( m_xParent, m_xContext, xCommandBarControl ) );
52  return uno::makeAny( xMenuItem );
53  }
54  nextElement();
55 
56  return uno::Any();
57  }
58 };
59 
60 }
61 
62 ScVbaMenuItems::ScVbaMenuItems( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext >& xContext, const uno::Reference< XCommandBarControls >& xCommandBarControls ) : MenuItems_BASE( xParent, xContext, uno::Reference< container::XIndexAccess>() ), m_xCommandBarControls( xCommandBarControls )
63 {
64 }
65 
66 // XEnumerationAccess
67 uno::Type SAL_CALL
69 {
71 }
72 
73 uno::Reference< container::XEnumeration >
75 {
76  uno::Reference< container::XEnumerationAccess > xEnumAccess( m_xCommandBarControls, uno::UNO_QUERY_THROW );
77  return uno::Reference< container::XEnumeration >( new MenuEnumeration( this, mxContext, xEnumAccess->createEnumeration() ) );
78 }
79 
82 {
83  // make no sense
84  return aSource;
85 }
86 
87 sal_Int32 SAL_CALL
89 {
90  // FIXME: should check if it is a popup menu
91  return m_xCommandBarControls->getCount();
92 }
93 
94 // ScVbaCollectionBaseImpl
95 uno::Any SAL_CALL
96 ScVbaMenuItems::Item( const uno::Any& aIndex, const uno::Any& /*aIndex2*/ )
97 {
98  uno::Reference< XCommandBarControl > xCommandBarControl( m_xCommandBarControls->Item( aIndex, uno::Any() ), uno::UNO_QUERY_THROW );
99  if( xCommandBarControl->getType() == office::MsoControlType::msoControlPopup )
100  return uno::makeAny( uno::Reference< excel::XMenu > ( new ScVbaMenu( this, mxContext, xCommandBarControl ) ) );
101  else if( xCommandBarControl->getType() == office::MsoControlType::msoControlButton )
102  return uno::makeAny( uno::Reference< excel::XMenuItem > ( new ScVbaMenuItem( this, mxContext, xCommandBarControl ) ) );
103  throw uno::RuntimeException();
104 }
105 
106 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*/ )
107 {
108  uno::Reference< XCommandBarControl > xCommandBarControl = m_xCommandBarControls->Add(
109  uno::makeAny( office::MsoControlType::msoControlButton ),
110  uno::Any(), uno::Any(), Before, Restore );
111  xCommandBarControl->setCaption( Caption );
112  if( OnAction.hasValue() )
113  {
114  OUString sAction;
115  OnAction >>= sAction;
116  xCommandBarControl->setOnAction( sAction );
117  }
118  return uno::Reference< excel::XMenuItem >( new ScVbaMenuItem( this, mxContext, xCommandBarControl ) );
119 }
120 
121 // XHelperInterface
122 OUString
124 {
125  return "ScVbaMenuItems";
126 }
127 
128 uno::Sequence<OUString>
130 {
131  static uno::Sequence< OUString > const aServiceNames
132  {
133  "ooo.vba.excel.MenuItems"
134  };
135  return aServiceNames;
136 }
137 
138 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual sal_Int32 SAL_CALL getCount() override
virtual css::uno::Type SAL_CALL getElementType() override
Reference
Sequence< OUString > aServiceNames
virtual css::uno::Any SAL_CALL Item(const css::uno::Any &Index, const css::uno::Any &) override
exports com.sun.star. container
::cppu::WeakImplHelper< container::XEnumeration > MenuEnumeration_BASE
css::uno::Reference< ov::XCommandBarControls > m_xCommandBarControls
virtual css::uno::Reference< css::container::XEnumeration > SAL_CALL createEnumeration() override
unsigned char sal_Bool
css::uno::Type const & get()
ScVbaMenuItems(const css::uno::Reference< ov::XHelperInterface > &xParent, const css::uno::Reference< css::uno::XComponentContext > &xContext, const css::uno::Reference< ov::XCommandBarControls > &xCommandBarControls)
css::uno::Reference< css::uno::XComponentContext > mxContext
Any makeAny(Color const &value)
virtual OUString getServiceImplName() override
virtual css::uno::Any createCollectionObject(const css::uno::Any &aSource) override
virtual css::uno::Sequence< OUString > getServiceNames() override
Reference< XComponentContext > m_xContext
virtual css::uno::Reference< ov::excel::XMenuItem > SAL_CALL 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) override