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 */
19 #include "vbacommandbarcontrols.hxx"
20 #include "vbacommandbarcontrol.hxx"
21 #include <com/sun/star/lang/XSingleComponentFactory.hpp>
22
23 using namespace com::sun::star;
24 using namespace ooo::vba;
25
26 class CommandBarControlEnumeration : public ::cppu::WeakImplHelper< container::XEnumeration >
27 {
28 //uno::Reference< uno::XComponentContext > m_xContext;
29 CommandBarControls_BASE* m_pCommandBarControls;
30 sal_Int32 m_nCurrentPosition;
31 public:
CommandBarControlEnumeration(CommandBarControls_BASE * pCommandBarControls)32 explicit CommandBarControlEnumeration( CommandBarControls_BASE* pCommandBarControls ) : m_pCommandBarControls( pCommandBarControls ), m_nCurrentPosition( 0 ) {}
hasMoreElements()33 virtual sal_Bool SAL_CALL hasMoreElements() override
34 {
35 if( m_nCurrentPosition < m_pCommandBarControls->getCount() )
36 return true;
37 return false;
38 }
nextElement()39 virtual uno::Any SAL_CALL nextElement() override
40 {
41 if( !hasMoreElements() )
42 throw container::NoSuchElementException();
43
44 return m_pCommandBarControls->createCollectionObject( uno::makeAny( m_nCurrentPosition++ ) );
45 }
46 };
47
ScVbaCommandBarControls(const uno::Reference<XHelperInterface> & xParent,const uno::Reference<uno::XComponentContext> & xContext,const uno::Reference<container::XIndexAccess> & xIndexAccess,VbaCommandBarHelperRef const & pHelper,const uno::Reference<container::XIndexAccess> & xBarSettings,const OUString & sResourceUrl)48 ScVbaCommandBarControls::ScVbaCommandBarControls( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext >& xContext, const uno::Reference< container::XIndexAccess>& xIndexAccess, VbaCommandBarHelperRef const & pHelper, const uno::Reference< container::XIndexAccess>& xBarSettings, const OUString& sResourceUrl ) : CommandBarControls_BASE( xParent, xContext, xIndexAccess ), pCBarHelper( pHelper ), m_xBarSettings( xBarSettings ), m_sResourceUrl( sResourceUrl )
49 {
50 m_bIsMenu = sResourceUrl == ITEM_MENUBAR_URL;
51 }
52
CreateMenuItemData(const OUString & sCommandURL,const OUString & sHelpURL,const OUString & sLabel,sal_uInt16 nType,const uno::Any & aSubMenu,bool isVisible,bool isEnabled)53 uno::Sequence< beans::PropertyValue > ScVbaCommandBarControls::CreateMenuItemData( const OUString& sCommandURL,
54 const OUString& sHelpURL,
55 const OUString& sLabel,
56 sal_uInt16 nType,
57 const uno::Any& aSubMenu,
58 bool isVisible,
59 bool isEnabled )
60 {
61 uno::Sequence< beans::PropertyValue > aProps(7);
62
63 aProps[0].Name = ITEM_DESCRIPTOR_COMMANDURL;
64 aProps[0].Value <<= sCommandURL;
65 aProps[1].Name = ITEM_DESCRIPTOR_HELPURL;
66 aProps[1].Value <<= sHelpURL;
67 aProps[2].Name = ITEM_DESCRIPTOR_LABEL;
68 aProps[2].Value <<= sLabel;
69 aProps[3].Name = ITEM_DESCRIPTOR_TYPE;
70 aProps[3].Value <<= nType;
71 aProps[4].Name = ITEM_DESCRIPTOR_CONTAINER;
72 aProps[4].Value = aSubMenu;
73 aProps[5].Name = ITEM_DESCRIPTOR_ISVISIBLE;
74 aProps[5].Value <<= isVisible;
75 aProps[6].Name = ITEM_DESCRIPTOR_ENABLED;
76 aProps[6].Value <<= isEnabled;
77
78 return aProps;
79 }
80
CreateToolbarItemData(const OUString & sCommandURL,const OUString & sHelpURL,const OUString & sLabel,sal_uInt16 nType,const uno::Any & aSubMenu,bool isVisible,sal_Int32 nStyle)81 uno::Sequence< beans::PropertyValue > ScVbaCommandBarControls::CreateToolbarItemData( const OUString& sCommandURL,
82 const OUString& sHelpURL,
83 const OUString& sLabel,
84 sal_uInt16 nType,
85 const uno::Any& aSubMenu,
86 bool isVisible,
87 sal_Int32 nStyle )
88 {
89 uno::Sequence< beans::PropertyValue > aProps(7);
90
91 aProps[0].Name = ITEM_DESCRIPTOR_COMMANDURL;
92 aProps[0].Value <<= sCommandURL;
93 aProps[1].Name = ITEM_DESCRIPTOR_HELPURL;
94 aProps[1].Value <<= sHelpURL;
95 aProps[2].Name = ITEM_DESCRIPTOR_LABEL;
96 aProps[2].Value <<= sLabel;
97 aProps[3].Name = ITEM_DESCRIPTOR_TYPE;
98 aProps[3].Value <<= nType;
99 aProps[4].Name = ITEM_DESCRIPTOR_CONTAINER;
100 aProps[4].Value = aSubMenu;
101 aProps[5].Name = ITEM_DESCRIPTOR_ISVISIBLE;
102 aProps[5].Value <<= isVisible;
103 aProps[6].Name = ITEM_DESCRIPTOR_STYLE;
104 aProps[6].Value <<= nStyle;
105
106 return aProps;
107 }
108
109 // XEnumerationAccess
110 uno::Type SAL_CALL
getElementType()111 ScVbaCommandBarControls::getElementType()
112 {
113 return cppu::UnoType<XCommandBarControl>::get();
114 }
115
116 uno::Reference< container::XEnumeration >
createEnumeration()117 ScVbaCommandBarControls::createEnumeration()
118 {
119 return uno::Reference< container::XEnumeration >( new CommandBarControlEnumeration( this ) );
120 }
121
122 uno::Any
createCollectionObject(const uno::Any & aSource)123 ScVbaCommandBarControls::createCollectionObject( const uno::Any& aSource )
124 {
125 sal_Int32 nPosition = -1;
126 aSource >>= nPosition;
127 uno::Sequence< beans::PropertyValue > aProps;
128 m_xIndexAccess->getByIndex( nPosition ) >>= aProps;
129 uno::Reference< container::XIndexAccess > xSubMenu;
130 getPropertyValue( aProps, ITEM_DESCRIPTOR_CONTAINER ) >>= xSubMenu;
131 ScVbaCommandBarControl* pNewCommandBarControl = nullptr;
132 if( xSubMenu.is() )
133 pNewCommandBarControl = new ScVbaCommandBarPopup( this, mxContext, m_xIndexAccess, pCBarHelper, m_xBarSettings, m_sResourceUrl, nPosition );
134 else
135 pNewCommandBarControl = new ScVbaCommandBarButton( this, mxContext, m_xIndexAccess, pCBarHelper, m_xBarSettings, m_sResourceUrl, nPosition );
136
137 return uno::makeAny( uno::Reference< XCommandBarControl > ( pNewCommandBarControl ) );
138 }
139
140 // Methods
141 uno::Any SAL_CALL
Item(const uno::Any & aIndex,const uno::Any &)142 ScVbaCommandBarControls::Item( const uno::Any& aIndex, const uno::Any& /*aIndex*/ )
143 {
144 sal_Int32 nPosition = -1;
145 if( aIndex.getValueTypeClass() == uno::TypeClass_STRING )
146 {
147 OUString sName;
148 aIndex >>= sName;
149 nPosition = VbaCommandBarHelper::findControlByName( m_xIndexAccess, sName, m_bIsMenu );
150 }
151 else
152 {
153 aIndex >>= nPosition;
154 }
155
156 if( nPosition < 0 || nPosition >= getCount() )
157 {
158 throw uno::RuntimeException();
159 }
160
161 return createCollectionObject( uno::makeAny( nPosition ) );
162 }
163
164 uno::Reference< XCommandBarControl > SAL_CALL
Add(const uno::Any & Type,const uno::Any & Id,const uno::Any & Parameter,const uno::Any & Before,SAL_UNUSED_PARAMETER const uno::Any &)165 ScVbaCommandBarControls::Add( const uno::Any& Type, const uno::Any& Id, const uno::Any& Parameter, const uno::Any& Before, SAL_UNUSED_PARAMETER const uno::Any& )
166 {
167 // Parameter is not supported
168 // the following name needs to be individually created;
169 OUString sLabel("Custom");
170 OUString sCommandUrl( CUSTOM_MENU_STR + sLabel);
171 sal_Int32 nType = office::MsoControlType::msoControlButton;
172 sal_Int32 nPosition = 0;
173
174 if( Type.hasValue() )
175 {
176 Type >>= nType;
177 }
178
179 if( nType != office::MsoControlType::msoControlButton &&
180 nType != office::MsoControlType::msoControlPopup )
181 throw uno::RuntimeException( "Not implemented" );
182
183 if( Id.hasValue() || Parameter.hasValue( ) )
184 {
185 throw uno::RuntimeException( "Not implemented" );
186 }
187
188 if( Before.hasValue() )
189 Before >>= nPosition;
190 else
191 nPosition = m_xIndexAccess->getCount();
192
193 uno::Any aSubMenu;
194 if( nType == office::MsoControlType::msoControlPopup )
195 {
196 // it is a Popmenu
197 uno::Reference< lang::XSingleComponentFactory > xSCF( m_xBarSettings, uno::UNO_QUERY_THROW );
198 aSubMenu <<= xSCF->createInstanceWithContext( mxContext );
199 }
200
201 // create control
202 uno::Sequence< beans::PropertyValue > aProps;
203 OUString sHelpUrl;
204 sal_uInt16 nItemType = 0;
205 if( m_bIsMenu )
206 {
207 aProps = CreateMenuItemData( sCommandUrl, sHelpUrl, sLabel, nItemType, aSubMenu, true, true );
208 }
209 else
210 {
211 aProps = CreateToolbarItemData( sCommandUrl, sHelpUrl, sLabel, nItemType, aSubMenu, true/*isVisible*/, 0/*nStyle*/ );
212 }
213
214
215 uno::Reference< container::XIndexContainer > xIndexContainer( m_xIndexAccess, uno::UNO_QUERY_THROW );
216 xIndexContainer->insertByIndex( nPosition, uno::makeAny( aProps ) );
217
218 pCBarHelper->ApplyTempChange( m_sResourceUrl, m_xBarSettings );
219
220 ScVbaCommandBarControl* pNewCommandBarControl = nullptr;
221 if( nType == office::MsoControlType::msoControlPopup )
222 pNewCommandBarControl = new ScVbaCommandBarPopup( this, mxContext, m_xIndexAccess, pCBarHelper, m_xBarSettings, m_sResourceUrl, nPosition );
223 else
224 pNewCommandBarControl = new ScVbaCommandBarButton( this, mxContext, m_xIndexAccess, pCBarHelper, m_xBarSettings, m_sResourceUrl, nPosition );
225
226 return uno::Reference< XCommandBarControl >( pNewCommandBarControl );
227 }
228
229 // XHelperInterface
230 OUString
getServiceImplName()231 ScVbaCommandBarControls::getServiceImplName()
232 {
233 return "ScVbaCommandBarControls";
234 }
235
236 uno::Sequence<OUString>
getServiceNames()237 ScVbaCommandBarControls::getServiceNames()
238 {
239 static uno::Sequence< OUString > const aServiceNames
240 {
241 "ooo.vba.CommandBarControls"
242 };
243 return aServiceNames;
244 }
245
246
247 class VbaDummyIndexAccess : public ::cppu::WeakImplHelper< container::XIndexAccess >
248 {
249 public:
VbaDummyIndexAccess()250 VbaDummyIndexAccess() {}
251 // XIndexAccess
getCount()252 virtual ::sal_Int32 SAL_CALL getCount( ) override
253 { return 0; }
getByIndex(::sal_Int32)254 virtual uno::Any SAL_CALL getByIndex( ::sal_Int32 /*Index*/ ) override
255 { throw lang::IndexOutOfBoundsException(); }
256 // XElementAccess
getElementType()257 virtual uno::Type SAL_CALL getElementType( ) override
258 { return cppu::UnoType<XCommandBarControl>::get(); }
hasElements()259 virtual sal_Bool SAL_CALL hasElements( ) override
260 { return false; }
261 };
262
263
VbaDummyCommandBarControls(const uno::Reference<XHelperInterface> & xParent,const uno::Reference<uno::XComponentContext> & xContext)264 VbaDummyCommandBarControls::VbaDummyCommandBarControls(
265 const uno::Reference< XHelperInterface >& xParent,
266 const uno::Reference< uno::XComponentContext >& xContext ) :
267 CommandBarControls_BASE( xParent, xContext, new VbaDummyIndexAccess )
268 {
269 }
270
271 // XEnumerationAccess
getElementType()272 uno::Type SAL_CALL VbaDummyCommandBarControls::getElementType()
273 {
274 return cppu::UnoType<XCommandBarControl>::get();
275 }
276
createEnumeration()277 uno::Reference< container::XEnumeration > VbaDummyCommandBarControls::createEnumeration()
278 {
279 return uno::Reference< container::XEnumeration >( new CommandBarControlEnumeration( this ) );
280 }
281
createCollectionObject(const uno::Any &)282 uno::Any VbaDummyCommandBarControls::createCollectionObject( const uno::Any& /*aSource*/ )
283 {
284 return uno::Any( uno::Reference< XCommandBarControl >() );
285 }
286
287 // Methods
Item(const uno::Any &,const uno::Any &)288 uno::Any SAL_CALL VbaDummyCommandBarControls::Item( const uno::Any& /*aIndex*/, const uno::Any& /*aIndex*/ )
289 {
290 return uno::Any( uno::Reference< XCommandBarControl >() );
291 }
292
Add(const uno::Any &,const uno::Any &,const uno::Any &,const uno::Any &,const uno::Any &)293 uno::Reference< XCommandBarControl > SAL_CALL VbaDummyCommandBarControls::Add(
294 const uno::Any& /*Type*/, const uno::Any& /*Id*/, const uno::Any& /*Parameter*/, const uno::Any& /*Before*/, const uno::Any& /*Temporary*/ )
295 {
296 return uno::Reference< XCommandBarControl >();
297 }
298
299 // XHelperInterface
getServiceImplName()300 OUString VbaDummyCommandBarControls::getServiceImplName()
301 {
302 return "VbaDummyCommandBarControls";
303 }
304
getServiceNames()305 uno::Sequence<OUString> VbaDummyCommandBarControls::getServiceNames()
306 {
307 static uno::Sequence< OUString > const aServiceNames
308 {
309 "ooo.vba.CommandBarControls"
310 };
311 return aServiceNames;
312 }
313
314 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
315