1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 #include "vbacommandbarcontrols.hxx"
24 #include "vbacommandbarcontrol.hxx"
25 
26 using namespace com::sun::star;
27 using namespace ooo::vba;
28 
29 typedef ::cppu::WeakImplHelper1< container::XEnumeration > CommandBarControlEnumeration_BASE;
30 class CommandBarControlEnumeration : public CommandBarControlEnumeration_BASE
31 {
32     //uno::Reference< uno::XComponentContext > m_xContext;
33     CommandBarControls_BASE* m_pCommandBarControls;
34     sal_Int32 m_nCurrentPosition;
35 public:
CommandBarControlEnumeration(CommandBarControls_BASE * pCommandBarControls)36     CommandBarControlEnumeration( CommandBarControls_BASE* pCommandBarControls ) : m_pCommandBarControls( pCommandBarControls ), m_nCurrentPosition( 0 ) {}
hasMoreElements()37     virtual sal_Bool SAL_CALL hasMoreElements() throw ( uno::RuntimeException )
38     {
39         if( m_nCurrentPosition < m_pCommandBarControls->getCount() )
40             return sal_True;
41         return sal_False;
42     }
nextElement()43     virtual uno::Any SAL_CALL nextElement() throw ( container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException )
44     {
45         if( hasMoreElements() )
46         {
47             return m_pCommandBarControls->createCollectionObject( uno::makeAny( m_nCurrentPosition++ ) );
48         }
49         else
50             throw container::NoSuchElementException();
51     }
52 };
53 
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 rtl::OUString & sResourceUrl)54 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 rtl::OUString& sResourceUrl ) throw (uno::RuntimeException) : CommandBarControls_BASE( xParent, xContext, xIndexAccess ), pCBarHelper( pHelper ), m_xBarSettings( xBarSettings ), m_sResourceUrl( sResourceUrl )
55 {
56     m_bIsMenu = sResourceUrl.equalsAscii( ITEM_MENUBAR_URL ) ? sal_True : sal_False;
57 }
58 
CreateMenuItemData(const rtl::OUString & sCommandURL,const rtl::OUString & sHelpURL,const rtl::OUString & sLabel,sal_uInt16 nType,const uno::Any & aSubMenu,sal_Bool isVisible,sal_Bool isEnabled)59 uno::Sequence< beans::PropertyValue > ScVbaCommandBarControls::CreateMenuItemData( const rtl::OUString& sCommandURL,
60                                                                                    const rtl::OUString& sHelpURL,
61                                                                                    const rtl::OUString& sLabel,
62                                                                                    sal_uInt16 nType,
63                                                                                    const uno::Any& aSubMenu,
64                                                                                    sal_Bool isVisible,
65                                                                                    sal_Bool isEnabled )
66 {
67     uno::Sequence< beans::PropertyValue > aProps(7);
68 
69     aProps[0].Name = rtl::OUString::createFromAscii( ITEM_DESCRIPTOR_COMMANDURL );
70     aProps[0].Value <<= sCommandURL;
71     aProps[1].Name = rtl::OUString::createFromAscii( ITEM_DESCRIPTOR_HELPURL );
72     aProps[1].Value <<= sHelpURL;
73     aProps[2].Name = rtl::OUString::createFromAscii( ITEM_DESCRIPTOR_LABEL );
74     aProps[2].Value <<= sLabel;
75     aProps[3].Name = rtl::OUString::createFromAscii( ITEM_DESCRIPTOR_TYPE );
76     aProps[3].Value <<= nType;
77     aProps[4].Name = rtl::OUString::createFromAscii( ITEM_DESCRIPTOR_CONTAINER );
78     aProps[4].Value = aSubMenu;
79     aProps[5].Name = rtl::OUString::createFromAscii( ITEM_DESCRIPTOR_ISVISIBLE );
80     aProps[5].Value <<= isVisible;
81     aProps[6].Name = rtl::OUString::createFromAscii( ITEM_DESCRIPTOR_ENABLED );
82     aProps[6].Value <<= isEnabled;
83 
84     return aProps;
85 }
86 
CreateToolbarItemData(const rtl::OUString & sCommandURL,const rtl::OUString & sHelpURL,const rtl::OUString & sLabel,sal_uInt16 nType,const uno::Any & aSubMenu,sal_Bool isVisible,sal_Int32 nStyle)87 uno::Sequence< beans::PropertyValue > ScVbaCommandBarControls::CreateToolbarItemData( const rtl::OUString& sCommandURL, const rtl::OUString& sHelpURL, const rtl::OUString& sLabel, sal_uInt16 nType, const uno::Any& aSubMenu, sal_Bool isVisible, sal_Int32 nStyle )
88 {
89     uno::Sequence< beans::PropertyValue > aProps(7);
90 
91     aProps[0].Name = rtl::OUString::createFromAscii( ITEM_DESCRIPTOR_COMMANDURL );
92     aProps[0].Value <<= sCommandURL;
93     aProps[1].Name = rtl::OUString::createFromAscii( ITEM_DESCRIPTOR_HELPURL );
94     aProps[1].Value <<= sHelpURL;
95     aProps[2].Name = rtl::OUString::createFromAscii( ITEM_DESCRIPTOR_LABEL );
96     aProps[2].Value <<= sLabel;
97     aProps[3].Name = rtl::OUString::createFromAscii( ITEM_DESCRIPTOR_TYPE );
98     aProps[3].Value <<= nType;
99     aProps[4].Name = rtl::OUString::createFromAscii( ITEM_DESCRIPTOR_CONTAINER );
100     aProps[4].Value = aSubMenu;
101     aProps[5].Name = rtl::OUString::createFromAscii( ITEM_DESCRIPTOR_ISVISIBLE );
102     aProps[5].Value <<= isVisible;
103     aProps[6].Name = rtl::OUString::createFromAscii( 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() throw ( uno::RuntimeException )
112 {
113     return XCommandBarControl::static_type( 0 );
114 }
115 
116 uno::Reference< container::XEnumeration >
createEnumeration()117 ScVbaCommandBarControls::createEnumeration() throw ( uno::RuntimeException )
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, rtl::OUString::createFromAscii( ITEM_DESCRIPTOR_CONTAINER ) ) >>= xSubMenu;
131     ScVbaCommandBarControl* pNewCommandBarControl = NULL;
132     if( xSubMenu.is() )
133         pNewCommandBarControl = new ScVbaCommandBarPopup( this, mxContext, m_xIndexAccess, pCBarHelper, m_xBarSettings, m_sResourceUrl, nPosition, sal_True );
134     else
135         pNewCommandBarControl = new ScVbaCommandBarButton( this, mxContext, m_xIndexAccess, pCBarHelper, m_xBarSettings, m_sResourceUrl, nPosition, sal_True );
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*/ ) throw (uno::RuntimeException)
143 {
144     sal_Int32 nPosition = -1;
145     if( aIndex.getValueTypeClass() == uno::TypeClass_STRING )
146     {
147         rtl::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,const uno::Any & Temporary)165 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)
166 {
167     // Parameter is not supported
168     // the following name needs to be individually created;
169     rtl::OUString sLabel( rtl::OUString::createFromAscii("Custom") );
170     rtl::OUString sCommandUrl = rtl::OUString::createFromAscii( CUSTOM_MENU_STR ) + sLabel;
171     sal_Int32 nType = office::MsoControlType::msoControlButton;
172     sal_Int32 nPosition = 0;
173     sal_Bool bTemporary = sal_True;
174 
175     if( Type.hasValue() )
176     {
177         Type >>= nType;
178     }
179 
180     if( nType != office::MsoControlType::msoControlButton &&
181         nType != office::MsoControlType::msoControlPopup )
182         throw uno::RuntimeException( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Not implemented") ), uno::Reference< uno::XInterface >() );
183 
184     if( Id.hasValue() || Parameter.hasValue( ) )
185     {
186         throw uno::RuntimeException( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Not implemented") ), uno::Reference< uno::XInterface >() );
187     }
188 
189     if( Before.hasValue() )
190         Before >>= nPosition;
191     else
192         nPosition = m_xIndexAccess->getCount();
193 
194     if( Temporary.hasValue() )
195         Temporary >>= bTemporary;
196 
197     uno::Any aSubMenu;
198     if( nType == office::MsoControlType::msoControlPopup )
199     {
200         // it is a Popmenu
201         uno::Reference< lang::XSingleComponentFactory > xSCF( m_xBarSettings, uno::UNO_QUERY_THROW );
202         aSubMenu <<= xSCF->createInstanceWithContext( mxContext );
203     }
204 
205     // create control
206     uno::Sequence< beans::PropertyValue > aProps;
207     rtl::OUString sHelpUrl;
208     sal_uInt16 nItemType = 0;
209     if( IsMenu() )
210     {
211         aProps = CreateMenuItemData( sCommandUrl, sHelpUrl, sLabel, nItemType, aSubMenu, true, true );
212     }
213     else
214     {
215         sal_Bool isVisible = sal_True;
216         sal_Int32 nStyle = 0;
217         aProps = CreateToolbarItemData( sCommandUrl, sHelpUrl, sLabel, nItemType, aSubMenu, isVisible, nStyle );
218     }
219 
220 
221     uno::Reference< container::XIndexContainer > xIndexContainer( m_xIndexAccess, uno::UNO_QUERY_THROW );
222     xIndexContainer->insertByIndex( nPosition, uno::makeAny( aProps ) );
223 
224     pCBarHelper->ApplyChange( m_sResourceUrl, m_xBarSettings );
225 
226     ScVbaCommandBarControl* pNewCommandBarControl = NULL;
227     if( nType == office::MsoControlType::msoControlPopup )
228         pNewCommandBarControl = new ScVbaCommandBarPopup( this, mxContext, m_xIndexAccess, pCBarHelper, m_xBarSettings, m_sResourceUrl, nPosition, bTemporary );
229     else
230         pNewCommandBarControl = new ScVbaCommandBarButton( this, mxContext, m_xIndexAccess, pCBarHelper, m_xBarSettings, m_sResourceUrl, nPosition, bTemporary );
231 
232     return uno::Reference< XCommandBarControl >( pNewCommandBarControl );
233 }
234 
235 // XHelperInterface
236 rtl::OUString&
getServiceImplName()237 ScVbaCommandBarControls::getServiceImplName()
238 {
239     static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("ScVbaCommandBarControls") );
240     return sImplName;
241 }
242 uno::Sequence<rtl::OUString>
getServiceNames()243 ScVbaCommandBarControls::getServiceNames()
244 {
245     static uno::Sequence< rtl::OUString > aServiceNames;
246     if ( aServiceNames.getLength() == 0 )
247     {
248         aServiceNames.realloc( 1 );
249         aServiceNames[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.CommandBarControls" ) );
250     }
251     return aServiceNames;
252 }
253 
254 // ============================================================================
255 
256 class VbaDummyIndexAccess : public ::cppu::WeakImplHelper1< container::XIndexAccess >
257 {
258 public:
VbaDummyIndexAccess()259     inline VbaDummyIndexAccess() {}
260     // XIndexAccess
getCount()261     virtual ::sal_Int32 SAL_CALL getCount(  ) throw (uno::RuntimeException)
262         { return 0; }
getByIndex(::sal_Int32)263     virtual uno::Any SAL_CALL getByIndex( ::sal_Int32 /*Index*/ ) throw (lang::IndexOutOfBoundsException, lang::WrappedTargetException, uno::RuntimeException)
264         { throw lang::IndexOutOfBoundsException(); }
265     // XElementAccess
getElementType()266     virtual uno::Type SAL_CALL getElementType(  ) throw (uno::RuntimeException)
267         { return XCommandBarControl::static_type( 0 ); }
hasElements()268     virtual ::sal_Bool SAL_CALL hasElements(  ) throw (::com::sun::star::uno::RuntimeException)
269         { return false; }
270 };
271 
272 // ----------------------------------------------------------------------------
273 
VbaDummyCommandBarControls(const uno::Reference<XHelperInterface> & xParent,const uno::Reference<uno::XComponentContext> & xContext)274 VbaDummyCommandBarControls::VbaDummyCommandBarControls(
275         const uno::Reference< XHelperInterface >& xParent,
276         const uno::Reference< uno::XComponentContext >& xContext ) throw (uno::RuntimeException) :
277     CommandBarControls_BASE( xParent, xContext, new VbaDummyIndexAccess )
278 {
279 }
280 
281 // XEnumerationAccess
getElementType()282 uno::Type SAL_CALL VbaDummyCommandBarControls::getElementType() throw ( uno::RuntimeException )
283 {
284     return XCommandBarControl::static_type( 0 );
285 }
286 
createEnumeration()287 uno::Reference< container::XEnumeration > VbaDummyCommandBarControls::createEnumeration() throw ( uno::RuntimeException )
288 {
289     return uno::Reference< container::XEnumeration >( new CommandBarControlEnumeration( this ) );
290 }
291 
createCollectionObject(const uno::Any &)292 uno::Any VbaDummyCommandBarControls::createCollectionObject( const uno::Any& /*aSource*/ )
293 {
294     return uno::Any( uno::Reference< XCommandBarControl >() );
295 }
296 
297 // Methods
Item(const uno::Any &,const uno::Any &)298 uno::Any SAL_CALL VbaDummyCommandBarControls::Item( const uno::Any& /*aIndex*/, const uno::Any& /*aIndex*/ ) throw (uno::RuntimeException)
299 {
300     return uno::Any( uno::Reference< XCommandBarControl >() );
301 }
302 
Add(const uno::Any &,const uno::Any &,const uno::Any &,const uno::Any &,const uno::Any &)303 uno::Reference< XCommandBarControl > SAL_CALL VbaDummyCommandBarControls::Add(
304         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)
305 {
306     return uno::Reference< XCommandBarControl >();
307 }
308 
309 // XHelperInterface
getServiceImplName()310 rtl::OUString& VbaDummyCommandBarControls::getServiceImplName()
311 {
312     static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("VbaDummyCommandBarControls") );
313     return sImplName;
314 }
315 
getServiceNames()316 uno::Sequence<rtl::OUString> VbaDummyCommandBarControls::getServiceNames()
317 {
318     static uno::Sequence< rtl::OUString > aServiceNames;
319     if ( aServiceNames.getLength() == 0 )
320     {
321         aServiceNames.realloc( 1 );
322         aServiceNames[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.CommandBarControls" ) );
323     }
324     return aServiceNames;
325 }
326