1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 #include "vbacommandbarcontrol.hxx" 28 #include "vbacommandbarcontrols.hxx" 29 #include <vbahelper/vbahelper.hxx> 30 #include <filter/msfilter/msvbahelper.hxx> 31 32 using namespace com::sun::star; 33 using namespace ooo::vba; 34 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 ) 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 ) 36 { 37 } 38 39 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 ) 40 { 41 m_xCurrentSettings->getByIndex( nPosition ) >>= m_aPropertyValues; 42 } 43 44 void ScVbaCommandBarControl::ApplyChange() throw ( uno::RuntimeException ) 45 { 46 uno::Reference< container::XIndexContainer > xIndexContainer( m_xCurrentSettings, uno::UNO_QUERY_THROW ); 47 xIndexContainer->replaceByIndex( m_nPosition, uno::makeAny( m_aPropertyValues ) ); 48 pCBarHelper->ApplyChange( m_sResourceUrl, m_xBarSettings ); 49 } 50 51 ::rtl::OUString SAL_CALL 52 ScVbaCommandBarControl::getCaption() throw ( uno::RuntimeException ) 53 { 54 // "Label" always empty 55 rtl::OUString sCaption; 56 getPropertyValue( m_aPropertyValues, rtl::OUString::createFromAscii("Label") ) >>= sCaption; 57 return sCaption; 58 } 59 60 void SAL_CALL 61 ScVbaCommandBarControl::setCaption( const ::rtl::OUString& _caption ) throw (uno::RuntimeException) 62 { 63 rtl::OUString sCaption = _caption.replace('&','~'); 64 setPropertyValue( m_aPropertyValues, rtl::OUString::createFromAscii("Label"), uno::makeAny( sCaption ) ); 65 ApplyChange(); 66 } 67 68 ::rtl::OUString SAL_CALL 69 ScVbaCommandBarControl::getOnAction() throw (uno::RuntimeException) 70 { 71 rtl::OUString sCommandURL; 72 getPropertyValue( m_aPropertyValues, rtl::OUString::createFromAscii("CommandURL") ) >>= sCommandURL; 73 return sCommandURL; 74 } 75 76 void SAL_CALL 77 ScVbaCommandBarControl::setOnAction( const ::rtl::OUString& _onaction ) throw (uno::RuntimeException) 78 { 79 // get the current model 80 uno::Reference< frame::XModel > xModel( pCBarHelper->getModel() ); 81 MacroResolvedInfo aResolvedMacro = ooo::vba::resolveVBAMacro( getSfxObjShell( xModel ), _onaction, true ); 82 if ( aResolvedMacro.mbFound ) 83 { 84 rtl::OUString aCommandURL = ooo::vba::makeMacroURL( aResolvedMacro.msResolvedMacro ); 85 OSL_TRACE(" ScVbaCommandBarControl::setOnAction: %s", rtl::OUStringToOString( aCommandURL, RTL_TEXTENCODING_UTF8 ).getStr() ); 86 setPropertyValue( m_aPropertyValues, rtl::OUString::createFromAscii("CommandURL"), uno::makeAny( aCommandURL ) ); 87 ApplyChange(); 88 } 89 } 90 91 ::sal_Bool SAL_CALL 92 ScVbaCommandBarControl::getVisible() throw (uno::RuntimeException) 93 { 94 sal_Bool bVisible = sal_True; 95 uno::Any aValue = getPropertyValue( m_aPropertyValues, rtl::OUString::createFromAscii( ITEM_DESCRIPTOR_ISVISIBLE ) ); 96 if( aValue.hasValue() ) 97 aValue >>= bVisible; 98 return bVisible; 99 } 100 void SAL_CALL 101 ScVbaCommandBarControl::setVisible( ::sal_Bool _visible ) throw (uno::RuntimeException) 102 { 103 uno::Any aValue = getPropertyValue( m_aPropertyValues, rtl::OUString::createFromAscii( ITEM_DESCRIPTOR_ISVISIBLE ) ); 104 if( aValue.hasValue() ) 105 { 106 setPropertyValue( m_aPropertyValues, rtl::OUString::createFromAscii( ITEM_DESCRIPTOR_ISVISIBLE ), uno::makeAny( _visible ) ); 107 ApplyChange(); 108 } 109 } 110 111 ::sal_Bool SAL_CALL 112 ScVbaCommandBarControl::getEnabled() throw (uno::RuntimeException) 113 { 114 sal_Bool bEnabled = sal_True; 115 116 uno::Any aValue = getPropertyValue( m_aPropertyValues, rtl::OUString::createFromAscii( ITEM_DESCRIPTOR_ENABLED ) ); 117 if( aValue.hasValue() ) 118 { 119 aValue >>= bEnabled; 120 } 121 else 122 { 123 // emulated with Visible 124 bEnabled = getVisible(); 125 } 126 return bEnabled; 127 } 128 129 void SAL_CALL 130 ScVbaCommandBarControl::setEnabled( sal_Bool _enabled ) throw (uno::RuntimeException) 131 { 132 uno::Any aValue = getPropertyValue( m_aPropertyValues, rtl::OUString::createFromAscii( ITEM_DESCRIPTOR_ENABLED ) ); 133 if( aValue.hasValue() ) 134 { 135 setPropertyValue( m_aPropertyValues, rtl::OUString::createFromAscii( ITEM_DESCRIPTOR_ENABLED ), uno::makeAny( _enabled ) ); 136 ApplyChange(); 137 } 138 else 139 { 140 // emulated with Visible 141 setVisible( _enabled ); 142 } 143 } 144 145 ::sal_Bool SAL_CALL 146 ScVbaCommandBarControl::getBeginGroup() throw (css::uno::RuntimeException) 147 { 148 // TODO: need to check if the item before this item is of type 'separator' 149 return sal_False; 150 } 151 152 void SAL_CALL 153 ScVbaCommandBarControl::setBeginGroup( ::sal_Bool _begin ) throw (css::uno::RuntimeException) 154 { 155 if( getBeginGroup() != _begin ) 156 { 157 // TODO: need to insert or remove an item of type 'separator' before this item 158 } 159 } 160 161 void SAL_CALL 162 ScVbaCommandBarControl::Delete( ) throw (script::BasicErrorException, uno::RuntimeException) 163 { 164 if( m_xCurrentSettings.is() ) 165 { 166 uno::Reference< container::XIndexContainer > xIndexContainer( m_xCurrentSettings, uno::UNO_QUERY_THROW ); 167 xIndexContainer->removeByIndex( m_nPosition ); 168 169 pCBarHelper->ApplyChange( m_sResourceUrl, m_xBarSettings ); 170 } 171 } 172 173 uno::Any SAL_CALL 174 ScVbaCommandBarControl::Controls( const uno::Any& aIndex ) throw (script::BasicErrorException, uno::RuntimeException) 175 { 176 // only Popup Menu has controls 177 uno::Reference< container::XIndexAccess > xSubMenu; 178 getPropertyValue( m_aPropertyValues, rtl::OUString::createFromAscii( ITEM_DESCRIPTOR_CONTAINER ) ) >>= xSubMenu; 179 if( !xSubMenu.is() ) 180 throw uno::RuntimeException(); 181 182 uno::Reference< XCommandBarControls > xCommandBarControls( new ScVbaCommandBarControls( this, mxContext, xSubMenu, pCBarHelper, m_xBarSettings, m_sResourceUrl ) ); 183 if( aIndex.hasValue() ) 184 { 185 return xCommandBarControls->Item( aIndex, uno::Any() ); 186 } 187 return uno::makeAny( xCommandBarControls ); 188 } 189 190 rtl::OUString& 191 ScVbaCommandBarControl::getServiceImplName() 192 { 193 static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("ScVbaCommandBarControl") ); 194 return sImplName; 195 } 196 197 uno::Sequence<rtl::OUString> 198 ScVbaCommandBarControl::getServiceNames() 199 { 200 static uno::Sequence< rtl::OUString > aServiceNames; 201 if ( aServiceNames.getLength() == 0 ) 202 { 203 aServiceNames.realloc( 1 ); 204 aServiceNames[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.CommandBarControl" ) ); 205 } 206 return aServiceNames; 207 } 208 209 //////////// ScVbaCommandBarPopup ////////////////////////////// 210 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 ) 211 { 212 m_nPosition = nPosition; 213 m_bTemporary = bTemporary; 214 m_xCurrentSettings->getByIndex( m_nPosition ) >>= m_aPropertyValues; 215 } 216 217 rtl::OUString& 218 ScVbaCommandBarPopup::getServiceImplName() 219 { 220 static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("ScVbaCommandBarPopup") ); 221 return sImplName; 222 } 223 uno::Sequence<rtl::OUString> 224 ScVbaCommandBarPopup::getServiceNames() 225 { 226 static uno::Sequence< rtl::OUString > aServiceNames; 227 if ( aServiceNames.getLength() == 0 ) 228 { 229 aServiceNames.realloc( 1 ); 230 aServiceNames[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.CommandBarPopup" ) ); 231 } 232 return aServiceNames; 233 } 234 235 //////////// ScVbaCommandBarButton ////////////////////////////// 236 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 ) 237 { 238 m_nPosition = nPosition; 239 m_bTemporary = bTemporary; 240 m_xCurrentSettings->getByIndex( m_nPosition ) >>= m_aPropertyValues; 241 } 242 243 rtl::OUString& 244 ScVbaCommandBarButton::getServiceImplName() 245 { 246 static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("ScVbaCommandBarButton") ); 247 return sImplName; 248 } 249 uno::Sequence<rtl::OUString> 250 ScVbaCommandBarButton::getServiceNames() 251 { 252 static uno::Sequence< rtl::OUString > aServiceNames; 253 if ( aServiceNames.getLength() == 0 ) 254 { 255 aServiceNames.realloc( 1 ); 256 aServiceNames[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.CommandBarButton" ) ); 257 } 258 return aServiceNames; 259 } 260