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