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 "vbacommandbarcontrol.hxx"
24 #include "vbacommandbarcontrols.hxx"
25 #include <vbahelper/vbahelper.hxx>
26 #include <filter/msfilter/msvbahelper.hxx>
27 
28 using namespace com::sun::star;
29 using namespace ooo::vba;
30 
ScVbaCommandBarControl(const css::uno::Reference<ov::XHelperInterface> & xParent,const css::uno::Reference<css::uno::XComponentContext> & xContext,const css::uno::Reference<css::container::XIndexAccess> & xSettings,VbaCommandBarHelperRef pHelper,const css::uno::Reference<css::container::XIndexAccess> & xBarSettings,const rtl::OUString & sResourceUrl)31 ScVbaCommandBarControl::ScVbaCommandBarControl( const css::uno::Reference< ov::XHelperInterface >& xParent, const css::uno::Reference< css::uno::XComponentContext >& xContext, const css::uno::Reference< css::container::XIndexAccess >& xSettings, VbaCommandBarHelperRef pHelper, const css::uno::Reference< css::container::XIndexAccess >& xBarSettings, const rtl::OUString& sResourceUrl ) throw (css::uno::RuntimeException) : CommandBarControl_BASE( xParent, xContext ), pCBarHelper( pHelper ), m_sResourceUrl( sResourceUrl ), m_xCurrentSettings( xSettings ), m_xBarSettings( xBarSettings ), m_nPosition( 0 ), m_bTemporary( sal_True )
32 {
33 }
34 
ScVbaCommandBarControl(const css::uno::Reference<ov::XHelperInterface> & xParent,const css::uno::Reference<css::uno::XComponentContext> & xContext,const css::uno::Reference<css::container::XIndexAccess> & xSettings,VbaCommandBarHelperRef pHelper,const css::uno::Reference<css::container::XIndexAccess> & xBarSettings,const rtl::OUString & sResourceUrl,sal_Int32 nPosition,sal_Bool bTemporary)35 ScVbaCommandBarControl::ScVbaCommandBarControl( const css::uno::Reference< ov::XHelperInterface >& xParent, const css::uno::Reference< css::uno::XComponentContext >& xContext, const css::uno::Reference< css::container::XIndexAccess >& xSettings, VbaCommandBarHelperRef pHelper, const css::uno::Reference< css::container::XIndexAccess >& xBarSettings, const rtl::OUString& sResourceUrl, sal_Int32 nPosition, sal_Bool bTemporary ) throw (css::uno::RuntimeException) : CommandBarControl_BASE( xParent, xContext ), pCBarHelper( pHelper ), m_sResourceUrl( sResourceUrl ), m_xCurrentSettings( xSettings ), m_xBarSettings( xBarSettings ), m_nPosition( nPosition ), m_bTemporary( bTemporary )
36 {
37     m_xCurrentSettings->getByIndex( nPosition ) >>= m_aPropertyValues;
38 }
39 
ApplyChange()40 void ScVbaCommandBarControl::ApplyChange() throw ( uno::RuntimeException )
41 {
42     uno::Reference< container::XIndexContainer > xIndexContainer( m_xCurrentSettings, uno::UNO_QUERY_THROW );
43     xIndexContainer->replaceByIndex( m_nPosition, uno::makeAny( m_aPropertyValues ) );
44     pCBarHelper->ApplyChange( m_sResourceUrl, m_xBarSettings );
45 }
46 
47 ::rtl::OUString SAL_CALL
getCaption()48 ScVbaCommandBarControl::getCaption() throw ( uno::RuntimeException )
49 {
50     // "Label" always empty
51     rtl::OUString sCaption;
52     getPropertyValue( m_aPropertyValues, rtl::OUString::createFromAscii("Label") ) >>= sCaption;
53     return sCaption;
54 }
55 
56 void SAL_CALL
setCaption(const::rtl::OUString & _caption)57 ScVbaCommandBarControl::setCaption( const ::rtl::OUString& _caption ) throw (uno::RuntimeException)
58 {
59     rtl::OUString sCaption = _caption.replace('&','~');
60     setPropertyValue( m_aPropertyValues, rtl::OUString::createFromAscii("Label"), uno::makeAny( sCaption ) );
61     ApplyChange();
62 }
63 
64 ::rtl::OUString SAL_CALL
getOnAction()65 ScVbaCommandBarControl::getOnAction() throw (uno::RuntimeException)
66 {
67     rtl::OUString sCommandURL;
68     getPropertyValue( m_aPropertyValues, rtl::OUString::createFromAscii("CommandURL") ) >>= sCommandURL;
69     return sCommandURL;
70 }
71 
72 void SAL_CALL
setOnAction(const::rtl::OUString & _onaction)73 ScVbaCommandBarControl::setOnAction( const ::rtl::OUString& _onaction ) throw (uno::RuntimeException)
74 {
75     // get the current model
76     uno::Reference< frame::XModel > xModel( pCBarHelper->getModel() );
77     MacroResolvedInfo aResolvedMacro = ooo::vba::resolveVBAMacro( getSfxObjShell( xModel ), _onaction, true );
78     if ( aResolvedMacro.mbFound )
79     {
80         rtl::OUString aCommandURL = ooo::vba::makeMacroURL( aResolvedMacro.msResolvedMacro );
81 		OSL_TRACE(" ScVbaCommandBarControl::setOnAction: %s", rtl::OUStringToOString( aCommandURL, RTL_TEXTENCODING_UTF8 ).getStr() );
82         setPropertyValue( m_aPropertyValues, rtl::OUString::createFromAscii("CommandURL"), uno::makeAny( aCommandURL ) );
83         ApplyChange();
84     }
85 }
86 
87 ::sal_Bool SAL_CALL
getVisible()88 ScVbaCommandBarControl::getVisible() throw (uno::RuntimeException)
89 {
90     sal_Bool bVisible = sal_True;
91     uno::Any aValue = getPropertyValue( m_aPropertyValues, rtl::OUString::createFromAscii( ITEM_DESCRIPTOR_ISVISIBLE ) );
92     if( aValue.hasValue() )
93         aValue >>= bVisible;
94     return bVisible;
95 }
96 void SAL_CALL
setVisible(::sal_Bool _visible)97 ScVbaCommandBarControl::setVisible( ::sal_Bool _visible ) throw (uno::RuntimeException)
98 {
99     uno::Any aValue = getPropertyValue( m_aPropertyValues, rtl::OUString::createFromAscii( ITEM_DESCRIPTOR_ISVISIBLE ) );
100     if( aValue.hasValue() )
101     {
102         setPropertyValue( m_aPropertyValues, rtl::OUString::createFromAscii( ITEM_DESCRIPTOR_ISVISIBLE ), uno::makeAny( _visible ) );
103         ApplyChange();
104     }
105 }
106 
107 ::sal_Bool SAL_CALL
getEnabled()108 ScVbaCommandBarControl::getEnabled() throw (uno::RuntimeException)
109 {
110     sal_Bool bEnabled = sal_True;
111 
112     uno::Any aValue = getPropertyValue( m_aPropertyValues, rtl::OUString::createFromAscii( ITEM_DESCRIPTOR_ENABLED ) );
113     if( aValue.hasValue() )
114     {
115         aValue >>= bEnabled;
116     }
117     else
118     {
119         // emulated with Visible
120         bEnabled = getVisible();
121     }
122     return bEnabled;
123 }
124 
125 void SAL_CALL
setEnabled(sal_Bool _enabled)126 ScVbaCommandBarControl::setEnabled( sal_Bool _enabled ) throw (uno::RuntimeException)
127 {
128     uno::Any aValue = getPropertyValue( m_aPropertyValues, rtl::OUString::createFromAscii( ITEM_DESCRIPTOR_ENABLED ) );
129     if( aValue.hasValue() )
130     {
131         setPropertyValue( m_aPropertyValues, rtl::OUString::createFromAscii( ITEM_DESCRIPTOR_ENABLED ), uno::makeAny( _enabled ) );
132         ApplyChange();
133     }
134     else
135     {
136         // emulated with Visible
137         setVisible( _enabled );
138     }
139 }
140 
141 ::sal_Bool SAL_CALL
getBeginGroup()142 ScVbaCommandBarControl::getBeginGroup() throw (css::uno::RuntimeException)
143 {
144     // TODO: need to check if the item before this item is of type 'separator'
145     return sal_False;
146 }
147 
148 void SAL_CALL
setBeginGroup(::sal_Bool _begin)149 ScVbaCommandBarControl::setBeginGroup( ::sal_Bool _begin ) throw (css::uno::RuntimeException)
150 {
151     if( getBeginGroup() != _begin )
152     {
153         // TODO: need to insert or remove an item of type 'separator' before this item
154     }
155 }
156 
157 void SAL_CALL
Delete()158 ScVbaCommandBarControl::Delete(  ) throw (script::BasicErrorException, uno::RuntimeException)
159 {
160     if( m_xCurrentSettings.is() )
161     {
162         uno::Reference< container::XIndexContainer > xIndexContainer( m_xCurrentSettings, uno::UNO_QUERY_THROW );
163         xIndexContainer->removeByIndex( m_nPosition );
164 
165         pCBarHelper->ApplyChange( m_sResourceUrl, m_xBarSettings );
166     }
167 }
168 
169 uno::Any SAL_CALL
Controls(const uno::Any & aIndex)170 ScVbaCommandBarControl::Controls( const uno::Any& aIndex ) throw (script::BasicErrorException, uno::RuntimeException)
171 {
172     // only Popup Menu has controls
173     uno::Reference< container::XIndexAccess > xSubMenu;
174     getPropertyValue( m_aPropertyValues, rtl::OUString::createFromAscii( ITEM_DESCRIPTOR_CONTAINER ) ) >>= xSubMenu;
175     if( !xSubMenu.is() )
176         throw uno::RuntimeException();
177 
178     uno::Reference< XCommandBarControls > xCommandBarControls( new ScVbaCommandBarControls( this, mxContext, xSubMenu, pCBarHelper, m_xBarSettings, m_sResourceUrl ) );
179     if( aIndex.hasValue() )
180     {
181         return xCommandBarControls->Item( aIndex, uno::Any() );
182     }
183     return uno::makeAny( xCommandBarControls );
184 }
185 
186 rtl::OUString&
getServiceImplName()187 ScVbaCommandBarControl::getServiceImplName()
188 {
189 	static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("ScVbaCommandBarControl") );
190 	return sImplName;
191 }
192 
193 uno::Sequence<rtl::OUString>
getServiceNames()194 ScVbaCommandBarControl::getServiceNames()
195 {
196 	static uno::Sequence< rtl::OUString > aServiceNames;
197 	if ( aServiceNames.getLength() == 0 )
198 	{
199 		aServiceNames.realloc( 1 );
200 		aServiceNames[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.CommandBarControl" ) );
201 	}
202 	return aServiceNames;
203 }
204 
205 //////////// ScVbaCommandBarPopup //////////////////////////////
ScVbaCommandBarPopup(const css::uno::Reference<ov::XHelperInterface> & xParent,const css::uno::Reference<css::uno::XComponentContext> & xContext,const css::uno::Reference<css::container::XIndexAccess> & xSettings,VbaCommandBarHelperRef pHelper,const css::uno::Reference<css::container::XIndexAccess> & xBarSettings,const rtl::OUString & sResourceUrl,sal_Int32 nPosition,sal_Bool bTemporary)206 ScVbaCommandBarPopup::ScVbaCommandBarPopup( const css::uno::Reference< ov::XHelperInterface >& xParent, const css::uno::Reference< css::uno::XComponentContext >& xContext, const css::uno::Reference< css::container::XIndexAccess >& xSettings, VbaCommandBarHelperRef pHelper, const css::uno::Reference< css::container::XIndexAccess >& xBarSettings, const rtl::OUString& sResourceUrl, sal_Int32 nPosition, sal_Bool bTemporary ) throw (css::uno::RuntimeException) : CommandBarPopup_BASE( xParent, xContext, xSettings, pHelper, xBarSettings, sResourceUrl )
207 {
208     m_nPosition = nPosition;
209     m_bTemporary = bTemporary;
210     m_xCurrentSettings->getByIndex( m_nPosition ) >>= m_aPropertyValues;
211 }
212 
213 rtl::OUString&
getServiceImplName()214 ScVbaCommandBarPopup::getServiceImplName()
215 {
216 	static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("ScVbaCommandBarPopup") );
217 	return sImplName;
218 }
219 uno::Sequence<rtl::OUString>
getServiceNames()220 ScVbaCommandBarPopup::getServiceNames()
221 {
222 	static uno::Sequence< rtl::OUString > aServiceNames;
223 	if ( aServiceNames.getLength() == 0 )
224 	{
225 		aServiceNames.realloc( 1 );
226 		aServiceNames[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.CommandBarPopup" ) );
227 	}
228 	return aServiceNames;
229 }
230 
231 //////////// ScVbaCommandBarButton //////////////////////////////
ScVbaCommandBarButton(const css::uno::Reference<ov::XHelperInterface> & xParent,const css::uno::Reference<css::uno::XComponentContext> & xContext,const css::uno::Reference<css::container::XIndexAccess> & xSettings,VbaCommandBarHelperRef pHelper,const css::uno::Reference<css::container::XIndexAccess> & xBarSettings,const rtl::OUString & sResourceUrl,sal_Int32 nPosition,sal_Bool bTemporary)232 ScVbaCommandBarButton::ScVbaCommandBarButton( const css::uno::Reference< ov::XHelperInterface >& xParent, const css::uno::Reference< css::uno::XComponentContext >& xContext, const css::uno::Reference< css::container::XIndexAccess >& xSettings, VbaCommandBarHelperRef pHelper, const css::uno::Reference< css::container::XIndexAccess >& xBarSettings, const rtl::OUString& sResourceUrl, sal_Int32 nPosition, sal_Bool bTemporary ) throw (css::uno::RuntimeException) : CommandBarButton_BASE( xParent, xContext, xSettings, pHelper, xBarSettings, sResourceUrl )
233 {
234     m_nPosition = nPosition;
235     m_bTemporary = bTemporary;
236     m_xCurrentSettings->getByIndex( m_nPosition ) >>= m_aPropertyValues;
237 }
238 
239 rtl::OUString&
getServiceImplName()240 ScVbaCommandBarButton::getServiceImplName()
241 {
242 	static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("ScVbaCommandBarButton") );
243 	return sImplName;
244 }
245 uno::Sequence<rtl::OUString>
getServiceNames()246 ScVbaCommandBarButton::getServiceNames()
247 {
248 	static uno::Sequence< rtl::OUString > aServiceNames;
249 	if ( aServiceNames.getLength() == 0 )
250 	{
251 		aServiceNames.realloc( 1 );
252 		aServiceNames[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.CommandBarButton" ) );
253 	}
254 	return aServiceNames;
255 }
256