LibreOffice Module vbahelper (master) 1
vbacommandbarcontrols.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 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
21#include <com/sun/star/lang/XSingleComponentFactory.hpp>
22
24#include <rtl/ref.hxx>
25#include <utility>
26
27using namespace com::sun::star;
28using namespace ooo::vba;
29
30namespace {
31
32class CommandBarControlEnumeration : public ::cppu::WeakImplHelper< container::XEnumeration >
33{
34 //uno::Reference< uno::XComponentContext > m_xContext;
35 CommandBarControls_BASE* m_pCommandBarControls;
36 sal_Int32 m_nCurrentPosition;
37public:
38 explicit CommandBarControlEnumeration( CommandBarControls_BASE* pCommandBarControls ) : m_pCommandBarControls( pCommandBarControls ), m_nCurrentPosition( 0 ) {}
39 virtual sal_Bool SAL_CALL hasMoreElements() override
40 {
41 if( m_nCurrentPosition < m_pCommandBarControls->getCount() )
42 return true;
43 return false;
44 }
45 virtual uno::Any SAL_CALL nextElement() override
46 {
47 if( !hasMoreElements() )
48 throw container::NoSuchElementException();
49
50 return m_pCommandBarControls->createCollectionObject( uno::Any( m_nCurrentPosition++ ) );
51 }
52};
53
54}
55
56ScVbaCommandBarControls::ScVbaCommandBarControls( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext >& xContext, const uno::Reference< container::XIndexAccess>& xIndexAccess, VbaCommandBarHelperRef pHelper, uno::Reference< container::XIndexAccess> xBarSettings, const OUString& sResourceUrl ) : CommandBarControls_BASE( xParent, xContext, xIndexAccess ), pCBarHelper(std::move( pHelper )), m_xBarSettings(std::move( xBarSettings )), m_sResourceUrl( sResourceUrl )
57{
58 m_bIsMenu = sResourceUrl == ITEM_MENUBAR_URL;
59}
60
61uno::Sequence< beans::PropertyValue > ScVbaCommandBarControls::CreateMenuItemData( const OUString& sCommandURL,
62 const OUString& sHelpURL,
63 const OUString& sLabel,
64 sal_uInt16 nType,
65 const uno::Any& aSubMenu,
66 bool isVisible,
67 bool isEnabled )
68{
69 uno::Sequence< beans::PropertyValue > aProps{
77 };
78
79 return aProps;
80}
81
82uno::Sequence< beans::PropertyValue > ScVbaCommandBarControls::CreateToolbarItemData( const OUString& sCommandURL,
83 const OUString& sHelpURL,
84 const OUString& sLabel,
85 sal_uInt16 nType,
86 const uno::Any& aSubMenu,
87 bool isVisible,
88 sal_Int32 nStyle )
89{
90 uno::Sequence< beans::PropertyValue > aProps{
98 };
99
100 return aProps;
101}
102
103// XEnumerationAccess
104uno::Type SAL_CALL
106{
108}
109
110uno::Reference< container::XEnumeration >
112{
113 return uno::Reference< container::XEnumeration >( new CommandBarControlEnumeration( this ) );
114}
115
118{
119 sal_Int32 nPosition = -1;
120 aSource >>= nPosition;
121 uno::Sequence< beans::PropertyValue > aProps;
122 m_xIndexAccess->getByIndex( nPosition ) >>= aProps;
123 uno::Reference< container::XIndexAccess > xSubMenu;
124 getPropertyValue( aProps, ITEM_DESCRIPTOR_CONTAINER ) >>= xSubMenu;
125 rtl::Reference<ScVbaCommandBarControl> pNewCommandBarControl;
126 if( xSubMenu.is() )
127 pNewCommandBarControl = new ScVbaCommandBarPopup( this, mxContext, m_xIndexAccess, pCBarHelper, m_xBarSettings, m_sResourceUrl, nPosition );
128 else
129 pNewCommandBarControl = new ScVbaCommandBarButton( this, mxContext, m_xIndexAccess, pCBarHelper, m_xBarSettings, m_sResourceUrl, nPosition );
130
131 return uno::Any( uno::Reference< XCommandBarControl > ( pNewCommandBarControl ) );
132}
133
134// Methods
135uno::Any SAL_CALL
136ScVbaCommandBarControls::Item( const uno::Any& aIndex, const uno::Any& /*aIndex*/ )
137{
138 sal_Int32 nPosition = -1;
139 if( aIndex.getValueTypeClass() == uno::TypeClass_STRING )
140 {
141 OUString sName;
142 aIndex >>= sName;
144 }
145 else
146 {
147 aIndex >>= nPosition;
148 }
149
150 if( nPosition < 0 || nPosition >= getCount() )
151 {
152 throw uno::RuntimeException();
153 }
154
155 return createCollectionObject( uno::Any( nPosition ) );
156}
157
158uno::Reference< XCommandBarControl > SAL_CALL
159ScVbaCommandBarControls::Add( const uno::Any& Type, const uno::Any& Id, const uno::Any& Parameter, const uno::Any& Before, SAL_UNUSED_PARAMETER const uno::Any& )
160{
161 // Parameter is not supported
162 // the following name needs to be individually created;
163 OUString sLabel("Custom");
164 OUString sCommandUrl( CUSTOM_MENU_STR + sLabel);
165 sal_Int32 nType = office::MsoControlType::msoControlButton;
166 sal_Int32 nPosition = 0;
167
168 if( Type.hasValue() )
169 {
170 Type >>= nType;
171 }
172
173 if( nType != office::MsoControlType::msoControlButton &&
174 nType != office::MsoControlType::msoControlPopup )
175 throw uno::RuntimeException( "Not implemented" );
176
177 if( Id.hasValue() || Parameter.hasValue( ) )
178 {
179 throw uno::RuntimeException( "Not implemented" );
180 }
181
182 if( Before.hasValue() )
183 Before >>= nPosition;
184 else
185 nPosition = m_xIndexAccess->getCount();
186
187 uno::Any aSubMenu;
188 if( nType == office::MsoControlType::msoControlPopup )
189 {
190 // it is a Popmenu
191 uno::Reference< lang::XSingleComponentFactory > xSCF( m_xBarSettings, uno::UNO_QUERY_THROW );
192 aSubMenu <<= xSCF->createInstanceWithContext( mxContext );
193 }
194
195 // create control
196 uno::Sequence< beans::PropertyValue > aProps;
197 sal_uInt16 nItemType = 0;
198 if( m_bIsMenu )
199 {
200 aProps = CreateMenuItemData( sCommandUrl, "", sLabel, nItemType, aSubMenu, true, true );
201 }
202 else
203 {
204 aProps = CreateToolbarItemData( sCommandUrl, "", sLabel, nItemType, aSubMenu, true/*isVisible*/, 0/*nStyle*/ );
205 }
206
207
208 uno::Reference< container::XIndexContainer > xIndexContainer( m_xIndexAccess, uno::UNO_QUERY_THROW );
209 xIndexContainer->insertByIndex( nPosition, uno::Any( aProps ) );
210
211 pCBarHelper->ApplyTempChange( m_sResourceUrl, m_xBarSettings );
212
213 rtl::Reference<ScVbaCommandBarControl> pNewCommandBarControl;
214 if( nType == office::MsoControlType::msoControlPopup )
215 pNewCommandBarControl = new ScVbaCommandBarPopup( this, mxContext, m_xIndexAccess, pCBarHelper, m_xBarSettings, m_sResourceUrl, nPosition );
216 else
217 pNewCommandBarControl = new ScVbaCommandBarButton( this, mxContext, m_xIndexAccess, pCBarHelper, m_xBarSettings, m_sResourceUrl, nPosition );
218
219 return pNewCommandBarControl;
220}
221
222// XHelperInterface
223OUString
225{
226 return "ScVbaCommandBarControls";
227}
228
229uno::Sequence<OUString>
231{
232 static uno::Sequence< OUString > const aServiceNames
233 {
234 "ooo.vba.CommandBarControls"
235 };
236 return aServiceNames;
237}
238
239namespace {
240
241class VbaDummyIndexAccess : public ::cppu::WeakImplHelper< container::XIndexAccess >
242{
243public:
244 VbaDummyIndexAccess() {}
245 // XIndexAccess
246 virtual ::sal_Int32 SAL_CALL getCount( ) override
247 { return 0; }
248 virtual uno::Any SAL_CALL getByIndex( ::sal_Int32 /*Index*/ ) override
249 { throw lang::IndexOutOfBoundsException(); }
250 // XElementAccess
251 virtual uno::Type SAL_CALL getElementType( ) override
253 virtual sal_Bool SAL_CALL hasElements( ) override
254 { return false; }
255};
256
257}
258
260 const uno::Reference< XHelperInterface >& xParent,
261 const uno::Reference< uno::XComponentContext >& xContext ) :
262 CommandBarControls_BASE( xParent, xContext, new VbaDummyIndexAccess )
263{
264}
265
266// XEnumerationAccess
268{
270}
271
272uno::Reference< container::XEnumeration > VbaDummyCommandBarControls::createEnumeration()
273{
274 return uno::Reference< container::XEnumeration >( new CommandBarControlEnumeration( this ) );
275}
276
278{
279 return uno::Any( uno::Reference< XCommandBarControl >() );
280}
281
282// Methods
283uno::Any SAL_CALL VbaDummyCommandBarControls::Item( const uno::Any& /*aIndex*/, const uno::Any& /*aIndex*/ )
284{
285 return uno::Any( uno::Reference< XCommandBarControl >() );
286}
287
288uno::Reference< XCommandBarControl > SAL_CALL VbaDummyCommandBarControls::Add(
289 const uno::Any& /*Type*/, const uno::Any& /*Id*/, const uno::Any& /*Parameter*/, const uno::Any& /*Before*/, const uno::Any& /*Temporary*/ )
290{
291 return uno::Reference< XCommandBarControl >();
292}
293
294// XHelperInterface
296{
297 return "VbaDummyCommandBarControls";
298}
299
301{
302 static uno::Sequence< OUString > const aServiceNames
303 {
304 "ooo.vba.CommandBarControls"
305 };
306 return aServiceNames;
307}
308
309/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
css::uno::Reference< css::uno::XComponentContext > mxContext
virtual css::uno::Any createCollectionObject(const css::uno::Any &aSource)=0
css::uno::Reference< css::container::XIndexAccess > m_xIndexAccess
virtual ::sal_Int32 SAL_CALL getCount() override
virtual css::uno::Any SAL_CALL Item(const css::uno::Any &Index, const css::uno::Any &) override
virtual css::uno::Type SAL_CALL getElementType() override
virtual css::uno::Sequence< OUString > getServiceNames() override
virtual css::uno::Any createCollectionObject(const css::uno::Any &aSource) override
static css::uno::Sequence< css::beans::PropertyValue > CreateToolbarItemData(const OUString &sCommandURL, const OUString &sHelpURL, const OUString &sLabel, sal_uInt16 nType, const css::uno::Any &aSubMenu, bool isVisible, sal_Int32 nStyle)
css::uno::Reference< css::container::XIndexAccess > m_xBarSettings
virtual css::uno::Reference< ov::XCommandBarControl > SAL_CALL Add(const css::uno::Any &Type, const css::uno::Any &Id, const css::uno::Any &Parameter, const css::uno::Any &Before, const css::uno::Any &Temporary) override
static css::uno::Sequence< css::beans::PropertyValue > CreateMenuItemData(const OUString &sCommandURL, const OUString &sHelpURL, const OUString &sLabel, sal_uInt16 nType, const css::uno::Any &aSubMenu, bool isVisible, bool isEnabled)
VbaCommandBarHelperRef pCBarHelper
virtual OUString getServiceImplName() override
virtual css::uno::Reference< css::container::XEnumeration > SAL_CALL createEnumeration() override
ScVbaCommandBarControls(const css::uno::Reference< ov::XHelperInterface > &xParent, const css::uno::Reference< css::uno::XComponentContext > &xContext, const css::uno::Reference< css::container::XIndexAccess > &xIndexAccess, VbaCommandBarHelperRef pHelper, css::uno::Reference< css::container::XIndexAccess > xBarSettings, const OUString &sResourceUrl)
static sal_Int32 findControlByName(const css::uno::Reference< css::container::XIndexAccess > &xIndexAccess, std::u16string_view sName, bool bMenu)
virtual OUString getServiceImplName() override
virtual css::uno::Any SAL_CALL Item(const css::uno::Any &Index, const css::uno::Any &) override
VbaDummyCommandBarControls(const css::uno::Reference< ov::XHelperInterface > &xParent, const css::uno::Reference< css::uno::XComponentContext > &xContext)
virtual css::uno::Type SAL_CALL getElementType() override
virtual css::uno::Reference< ov::XCommandBarControl > SAL_CALL Add(const css::uno::Any &Type, const css::uno::Any &Id, const css::uno::Any &Parameter, const css::uno::Any &Before, const css::uno::Any &Temporary) override
virtual css::uno::Sequence< OUString > getServiceNames() override
virtual css::uno::Reference< css::container::XEnumeration > SAL_CALL createEnumeration() override
virtual css::uno::Any createCollectionObject(const css::uno::Any &aSource) override
css::uno::Type const & get()
std::deque< AttacherIndex_Impl > aIndex
Sequence< OUString > aServiceNames
OUString sName
css::beans::PropertyValue makePropertyValue(const OUString &rName, T &&rValue)
Type
uno::Any getPropertyValue(const uno::Sequence< beans::PropertyValue > &aProp, const OUString &aName)
Definition: vbahelper.cxx:714
QPRO_FUNC_TYPE nType
sal_uInt32 Id
unsigned char sal_Bool
constexpr OUStringLiteral ITEM_DESCRIPTOR_ISVISIBLE
constexpr OUStringLiteral ITEM_DESCRIPTOR_STYLE
std::shared_ptr< VbaCommandBarHelper > VbaCommandBarHelperRef
constexpr OUStringLiteral ITEM_DESCRIPTOR_COMMANDURL
constexpr OUStringLiteral ITEM_DESCRIPTOR_TYPE
constexpr OUStringLiteral CUSTOM_MENU_STR
constexpr OUStringLiteral ITEM_DESCRIPTOR_HELPURL
constexpr OUStringLiteral ITEM_DESCRIPTOR_ENABLED
constexpr OUStringLiteral ITEM_DESCRIPTOR_LABEL
constexpr OUStringLiteral ITEM_MENUBAR_URL
constexpr OUStringLiteral ITEM_DESCRIPTOR_CONTAINER