1*e6ed5fbcSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*e6ed5fbcSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*e6ed5fbcSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*e6ed5fbcSAndrew Rist  * distributed with this work for additional information
6*e6ed5fbcSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*e6ed5fbcSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*e6ed5fbcSAndrew Rist  * "License"); you may not use this file except in compliance
9*e6ed5fbcSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*e6ed5fbcSAndrew Rist  *
11*e6ed5fbcSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*e6ed5fbcSAndrew Rist  *
13*e6ed5fbcSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*e6ed5fbcSAndrew Rist  * software distributed under the License is distributed on an
15*e6ed5fbcSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*e6ed5fbcSAndrew Rist  * KIND, either express or implied.  See the License for the
17*e6ed5fbcSAndrew Rist  * specific language governing permissions and limitations
18*e6ed5fbcSAndrew Rist  * under the License.
19*e6ed5fbcSAndrew Rist  *
20*e6ed5fbcSAndrew Rist  *************************************************************/
21*e6ed5fbcSAndrew Rist 
22*e6ed5fbcSAndrew Rist 
23cdf0e10cSrcweir #include "vbacommandbar.hxx"
24cdf0e10cSrcweir #include "vbacommandbarcontrols.hxx"
25cdf0e10cSrcweir #include <com/sun/star/ui/XModuleUIConfigurationManagerSupplier.hpp>
26cdf0e10cSrcweir #include <com/sun/star/frame/XFrame.hpp>
27cdf0e10cSrcweir #include <com/sun/star/frame/XDesktop.hpp>
28cdf0e10cSrcweir #include <com/sun/star/frame/XLayoutManager.hpp>
29cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp>
30cdf0e10cSrcweir #include <com/sun/star/container/XNameContainer.hpp>
31cdf0e10cSrcweir #include <ooo/vba/office/MsoBarType.hpp>
32cdf0e10cSrcweir 
33cdf0e10cSrcweir using namespace com::sun::star;
34cdf0e10cSrcweir using namespace ooo::vba;
35cdf0e10cSrcweir 
ScVbaCommandBar(const uno::Reference<ov::XHelperInterface> xParent,const uno::Reference<uno::XComponentContext> xContext,VbaCommandBarHelperRef pHelper,const uno::Reference<container::XIndexAccess> & xBarSettings,const rtl::OUString & sResourceUrl,sal_Bool bIsMenu,sal_Bool bTemporary)36cdf0e10cSrcweir ScVbaCommandBar::ScVbaCommandBar( const uno::Reference< ov::XHelperInterface > xParent, const uno::Reference< uno::XComponentContext > xContext, VbaCommandBarHelperRef pHelper, const uno::Reference< container::XIndexAccess >& xBarSettings, const rtl::OUString& sResourceUrl, sal_Bool bIsMenu, sal_Bool bTemporary ) throw( uno::RuntimeException ) : CommandBar_BASE( xParent, xContext ), pCBarHelper( pHelper ), m_xBarSettings( xBarSettings ), m_sResourceUrl( sResourceUrl ), m_bIsMenu( bIsMenu ), m_bTemporary( bTemporary )
37cdf0e10cSrcweir {
38cdf0e10cSrcweir }
39cdf0e10cSrcweir 
40cdf0e10cSrcweir ::rtl::OUString SAL_CALL
getName()41cdf0e10cSrcweir ScVbaCommandBar::getName() throw ( uno::RuntimeException )
42cdf0e10cSrcweir {
43cdf0e10cSrcweir     // This will get a "NULL length string" when Name is not set.
44cdf0e10cSrcweir     uno::Reference< beans::XPropertySet > xPropertySet( m_xBarSettings, uno::UNO_QUERY_THROW );
45cdf0e10cSrcweir     uno::Any aName = xPropertySet->getPropertyValue( rtl::OUString::createFromAscii("UIName") );
46cdf0e10cSrcweir     rtl::OUString sName;
47cdf0e10cSrcweir     aName >>= sName;
48cdf0e10cSrcweir     if( sName.getLength() < 1 )
49cdf0e10cSrcweir     {
50cdf0e10cSrcweir         if( m_bIsMenu )
51cdf0e10cSrcweir         {
52cdf0e10cSrcweir             if( m_sResourceUrl.equalsAscii( ITEM_MENUBAR_URL ) )
53cdf0e10cSrcweir             {
54cdf0e10cSrcweir                 if( pCBarHelper->getModuleId().equalsAscii("com.sun.star.sheet.SpreadsheetDocument") )
55cdf0e10cSrcweir                     sName = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Worksheet Menu Bar") );
56cdf0e10cSrcweir                 else if( pCBarHelper->getModuleId().equalsAscii("com.sun.star.text.TextDocument") )
57cdf0e10cSrcweir                     sName = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Menu Bar") );
58cdf0e10cSrcweir                 return sName;
59cdf0e10cSrcweir             }
60cdf0e10cSrcweir         }
61cdf0e10cSrcweir         // Toolbar name
62cdf0e10cSrcweir         uno::Reference< container::XNameAccess > xNameAccess = pCBarHelper->getPersistentWindowState();
63cdf0e10cSrcweir         if( xNameAccess->hasByName( m_sResourceUrl ) )
64cdf0e10cSrcweir         {
65cdf0e10cSrcweir             uno::Sequence< beans::PropertyValue > aToolBar;
66cdf0e10cSrcweir             xNameAccess->getByName( m_sResourceUrl ) >>= aToolBar;
67cdf0e10cSrcweir             getPropertyValue( aToolBar, rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("UIName") ) ) >>= sName;
68cdf0e10cSrcweir         }
69cdf0e10cSrcweir     }
70cdf0e10cSrcweir     return sName;
71cdf0e10cSrcweir }
72cdf0e10cSrcweir void SAL_CALL
setName(const::rtl::OUString & _name)73cdf0e10cSrcweir ScVbaCommandBar::setName( const ::rtl::OUString& _name ) throw (uno::RuntimeException)
74cdf0e10cSrcweir {
75cdf0e10cSrcweir     uno::Reference< beans::XPropertySet > xPropertySet( m_xBarSettings, uno::UNO_QUERY_THROW );
76cdf0e10cSrcweir     xPropertySet->setPropertyValue( rtl::OUString::createFromAscii("UIName"), uno::makeAny( _name ) );
77cdf0e10cSrcweir 
78cdf0e10cSrcweir     pCBarHelper->ApplyChange( m_sResourceUrl, m_xBarSettings );
79cdf0e10cSrcweir }
80cdf0e10cSrcweir ::sal_Bool SAL_CALL
getVisible()81cdf0e10cSrcweir ScVbaCommandBar::getVisible() throw (uno::RuntimeException)
82cdf0e10cSrcweir {
83cdf0e10cSrcweir     // menu bar is allways visible in OOo
84cdf0e10cSrcweir     if( m_bIsMenu )
85cdf0e10cSrcweir         return sal_True;
86cdf0e10cSrcweir 
87cdf0e10cSrcweir     sal_Bool bVisible = sal_False;
88cdf0e10cSrcweir     try
89cdf0e10cSrcweir     {
90cdf0e10cSrcweir         uno::Reference< container::XNameAccess > xNameAccess = pCBarHelper->getPersistentWindowState();
91cdf0e10cSrcweir         if( xNameAccess->hasByName( m_sResourceUrl ) )
92cdf0e10cSrcweir         {
93cdf0e10cSrcweir             uno::Sequence< beans::PropertyValue > aToolBar;
94cdf0e10cSrcweir             xNameAccess->getByName( m_sResourceUrl ) >>= aToolBar;
95cdf0e10cSrcweir             getPropertyValue( aToolBar, rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Visible") ) ) >>= bVisible;
96cdf0e10cSrcweir         }
97cdf0e10cSrcweir     }
98cdf0e10cSrcweir     catch ( uno::Exception e )
99cdf0e10cSrcweir     {
100cdf0e10cSrcweir     }
101cdf0e10cSrcweir     return bVisible;
102cdf0e10cSrcweir }
103cdf0e10cSrcweir void SAL_CALL
setVisible(::sal_Bool _visible)104cdf0e10cSrcweir ScVbaCommandBar::setVisible( ::sal_Bool _visible ) throw (uno::RuntimeException)
105cdf0e10cSrcweir {
106cdf0e10cSrcweir     try
107cdf0e10cSrcweir     {
108cdf0e10cSrcweir         uno::Reference< frame::XLayoutManager > xLayoutManager = pCBarHelper->getLayoutManager();
109cdf0e10cSrcweir         if( _visible )
110cdf0e10cSrcweir         {
111cdf0e10cSrcweir             xLayoutManager->createElement( m_sResourceUrl );
112cdf0e10cSrcweir             xLayoutManager->showElement( m_sResourceUrl );
113cdf0e10cSrcweir         }
114cdf0e10cSrcweir         else
115cdf0e10cSrcweir         {
116cdf0e10cSrcweir             xLayoutManager->hideElement( m_sResourceUrl );
117cdf0e10cSrcweir             xLayoutManager->destroyElement( m_sResourceUrl );
118cdf0e10cSrcweir         }
119cdf0e10cSrcweir     }
120cdf0e10cSrcweir     catch( uno::Exception e )
121cdf0e10cSrcweir     {
122cdf0e10cSrcweir         OSL_TRACE( "SetVisible get an exception\n" );
123cdf0e10cSrcweir     }
124cdf0e10cSrcweir }
125cdf0e10cSrcweir 
126cdf0e10cSrcweir ::sal_Bool SAL_CALL
getEnabled()127cdf0e10cSrcweir ScVbaCommandBar::getEnabled() throw (uno::RuntimeException)
128cdf0e10cSrcweir {
129cdf0e10cSrcweir     // emulated with Visible
130cdf0e10cSrcweir     return getVisible();
131cdf0e10cSrcweir }
132cdf0e10cSrcweir 
133cdf0e10cSrcweir void SAL_CALL
setEnabled(sal_Bool _enabled)134cdf0e10cSrcweir ScVbaCommandBar::setEnabled( sal_Bool _enabled ) throw (uno::RuntimeException)
135cdf0e10cSrcweir {
136cdf0e10cSrcweir     // emulated with Visible
137cdf0e10cSrcweir     setVisible( _enabled );
138cdf0e10cSrcweir }
139cdf0e10cSrcweir 
140cdf0e10cSrcweir void SAL_CALL
Delete()141cdf0e10cSrcweir ScVbaCommandBar::Delete(  ) throw (script::BasicErrorException, uno::RuntimeException)
142cdf0e10cSrcweir {
143cdf0e10cSrcweir     pCBarHelper->removeSettings( m_sResourceUrl );
144cdf0e10cSrcweir     uno::Reference< container::XNameContainer > xNameContainer( pCBarHelper->getPersistentWindowState(), uno::UNO_QUERY_THROW );
145cdf0e10cSrcweir     if( xNameContainer->hasByName( m_sResourceUrl ) )
146cdf0e10cSrcweir     {
147cdf0e10cSrcweir         xNameContainer->removeByName( m_sResourceUrl );
148cdf0e10cSrcweir     }
149cdf0e10cSrcweir }
150cdf0e10cSrcweir uno::Any SAL_CALL
Controls(const uno::Any & aIndex)151cdf0e10cSrcweir ScVbaCommandBar::Controls( const uno::Any& aIndex ) throw (script::BasicErrorException, uno::RuntimeException)
152cdf0e10cSrcweir {
153cdf0e10cSrcweir     uno::Reference< XCommandBarControls > xCommandBarControls( new ScVbaCommandBarControls( this, mxContext, m_xBarSettings, pCBarHelper, m_xBarSettings, m_sResourceUrl ) );
154cdf0e10cSrcweir     if( aIndex.hasValue() )
155cdf0e10cSrcweir     {
156cdf0e10cSrcweir         return xCommandBarControls->Item( aIndex, uno::Any() );
157cdf0e10cSrcweir     }
158cdf0e10cSrcweir     return uno::makeAny( xCommandBarControls );
159cdf0e10cSrcweir }
160cdf0e10cSrcweir 
161cdf0e10cSrcweir sal_Int32 SAL_CALL
Type()162cdf0e10cSrcweir ScVbaCommandBar::Type() throw (script::BasicErrorException, uno::RuntimeException)
163cdf0e10cSrcweir {
164cdf0e10cSrcweir     // #FIXME support msoBarTypePopup
165cdf0e10cSrcweir     sal_Int32 nType = office::MsoBarType::msoBarTypePopup;
166cdf0e10cSrcweir     nType = m_bIsMenu? office::MsoBarType::msoBarTypeNormal : office::MsoBarType::msoBarTypeMenuBar;
167cdf0e10cSrcweir     return nType;
168cdf0e10cSrcweir }
169cdf0e10cSrcweir 
170cdf0e10cSrcweir uno::Any SAL_CALL
FindControl(const uno::Any &,const uno::Any &,const uno::Any &,const uno::Any &,const uno::Any &)171cdf0e10cSrcweir ScVbaCommandBar::FindControl( const uno::Any& /*aType*/, const uno::Any& /*aId*/, const uno::Any& /*aTag*/, const uno::Any& /*aVisible*/, const uno::Any& /*aRecursive*/ ) throw (script::BasicErrorException, uno::RuntimeException)
172cdf0e10cSrcweir {
173cdf0e10cSrcweir     // alwayse fail to find control
174cdf0e10cSrcweir     return uno::makeAny( uno::Reference< XCommandBarControl > () );
175cdf0e10cSrcweir }
176cdf0e10cSrcweir 
177cdf0e10cSrcweir rtl::OUString&
getServiceImplName()178cdf0e10cSrcweir ScVbaCommandBar::getServiceImplName()
179cdf0e10cSrcweir {
180cdf0e10cSrcweir 	static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("ScVbaCommandBar") );
181cdf0e10cSrcweir 	return sImplName;
182cdf0e10cSrcweir }
183cdf0e10cSrcweir uno::Sequence<rtl::OUString>
getServiceNames()184cdf0e10cSrcweir ScVbaCommandBar::getServiceNames()
185cdf0e10cSrcweir {
186cdf0e10cSrcweir 	static uno::Sequence< rtl::OUString > aServiceNames;
187cdf0e10cSrcweir 	if ( aServiceNames.getLength() == 0 )
188cdf0e10cSrcweir 	{
189cdf0e10cSrcweir 		aServiceNames.realloc( 1 );
190cdf0e10cSrcweir 		aServiceNames[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.CommandBar" ) );
191cdf0e10cSrcweir 	}
192cdf0e10cSrcweir 	return aServiceNames;
193cdf0e10cSrcweir }
194cdf0e10cSrcweir 
195cdf0e10cSrcweir 
VbaDummyCommandBar(const uno::Reference<ov::XHelperInterface> xParent,const uno::Reference<uno::XComponentContext> xContext,const::rtl::OUString & rName,sal_Int32 nType)196cdf0e10cSrcweir VbaDummyCommandBar::VbaDummyCommandBar(
197cdf0e10cSrcweir         const uno::Reference< ov::XHelperInterface > xParent,
198cdf0e10cSrcweir         const uno::Reference< uno::XComponentContext > xContext,
199cdf0e10cSrcweir         const ::rtl::OUString& rName, sal_Int32 nType ) throw( uno::RuntimeException ) :
200cdf0e10cSrcweir     CommandBar_BASE( xParent, xContext ),
201cdf0e10cSrcweir     maName( rName ),
202cdf0e10cSrcweir     mnType( nType )
203cdf0e10cSrcweir {
204cdf0e10cSrcweir }
205cdf0e10cSrcweir 
getName()206cdf0e10cSrcweir ::rtl::OUString SAL_CALL VbaDummyCommandBar::getName() throw ( uno::RuntimeException )
207cdf0e10cSrcweir {
208cdf0e10cSrcweir     return maName;
209cdf0e10cSrcweir }
210cdf0e10cSrcweir 
setName(const::rtl::OUString & _name)211cdf0e10cSrcweir void SAL_CALL VbaDummyCommandBar::setName( const ::rtl::OUString& _name ) throw (uno::RuntimeException)
212cdf0e10cSrcweir {
213cdf0e10cSrcweir     maName = _name;
214cdf0e10cSrcweir }
215cdf0e10cSrcweir 
getVisible()216cdf0e10cSrcweir ::sal_Bool SAL_CALL VbaDummyCommandBar::getVisible() throw (uno::RuntimeException)
217cdf0e10cSrcweir {
218cdf0e10cSrcweir     return sal_True;
219cdf0e10cSrcweir }
220cdf0e10cSrcweir 
setVisible(::sal_Bool)221cdf0e10cSrcweir void SAL_CALL VbaDummyCommandBar::setVisible( ::sal_Bool /*_visible*/ ) throw (uno::RuntimeException)
222cdf0e10cSrcweir {
223cdf0e10cSrcweir }
224cdf0e10cSrcweir 
getEnabled()225cdf0e10cSrcweir ::sal_Bool SAL_CALL VbaDummyCommandBar::getEnabled() throw (uno::RuntimeException)
226cdf0e10cSrcweir {
227cdf0e10cSrcweir     // emulated with Visible
228cdf0e10cSrcweir     return getVisible();
229cdf0e10cSrcweir }
230cdf0e10cSrcweir 
setEnabled(sal_Bool _enabled)231cdf0e10cSrcweir void SAL_CALL VbaDummyCommandBar::setEnabled( sal_Bool _enabled ) throw (uno::RuntimeException)
232cdf0e10cSrcweir {
233cdf0e10cSrcweir     // emulated with Visible
234cdf0e10cSrcweir     setVisible( _enabled );
235cdf0e10cSrcweir }
236cdf0e10cSrcweir 
Delete()237cdf0e10cSrcweir void SAL_CALL VbaDummyCommandBar::Delete(  ) throw (script::BasicErrorException, uno::RuntimeException)
238cdf0e10cSrcweir {
239cdf0e10cSrcweir     // no-op
240cdf0e10cSrcweir }
241cdf0e10cSrcweir 
Controls(const uno::Any & aIndex)242cdf0e10cSrcweir uno::Any SAL_CALL VbaDummyCommandBar::Controls( const uno::Any& aIndex ) throw (script::BasicErrorException, uno::RuntimeException)
243cdf0e10cSrcweir {
244cdf0e10cSrcweir     uno::Reference< XCommandBarControls > xCommandBarControls( new VbaDummyCommandBarControls( this, mxContext ) );
245cdf0e10cSrcweir     if( aIndex.hasValue() )
246cdf0e10cSrcweir         return xCommandBarControls->Item( aIndex, uno::Any() );
247cdf0e10cSrcweir     return uno::Any( xCommandBarControls );
248cdf0e10cSrcweir }
249cdf0e10cSrcweir 
Type()250cdf0e10cSrcweir sal_Int32 SAL_CALL VbaDummyCommandBar::Type() throw (script::BasicErrorException, uno::RuntimeException)
251cdf0e10cSrcweir {
252cdf0e10cSrcweir     return mnType;
253cdf0e10cSrcweir }
254cdf0e10cSrcweir 
FindControl(const uno::Any &,const uno::Any &,const uno::Any &,const uno::Any &,const uno::Any &)255cdf0e10cSrcweir uno::Any SAL_CALL VbaDummyCommandBar::FindControl( const uno::Any& /*aType*/, const uno::Any& /*aId*/, const uno::Any& /*aTag*/, const uno::Any& /*aVisible*/, const uno::Any& /*aRecursive*/ ) throw (script::BasicErrorException, uno::RuntimeException)
256cdf0e10cSrcweir {
257cdf0e10cSrcweir     return uno::Any( uno::Reference< XCommandBarControl >() );
258cdf0e10cSrcweir }
259cdf0e10cSrcweir 
getServiceImplName()260cdf0e10cSrcweir rtl::OUString& VbaDummyCommandBar::getServiceImplName()
261cdf0e10cSrcweir {
262cdf0e10cSrcweir 	static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("VbaDummyCommandBar") );
263cdf0e10cSrcweir 	return sImplName;
264cdf0e10cSrcweir }
265cdf0e10cSrcweir 
getServiceNames()266cdf0e10cSrcweir uno::Sequence< rtl::OUString > VbaDummyCommandBar::getServiceNames()
267cdf0e10cSrcweir {
268cdf0e10cSrcweir 	static uno::Sequence< rtl::OUString > aServiceNames;
269cdf0e10cSrcweir 	if ( aServiceNames.getLength() == 0 )
270cdf0e10cSrcweir 	{
271cdf0e10cSrcweir 		aServiceNames.realloc( 1 );
272cdf0e10cSrcweir 		aServiceNames[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.CommandBar" ) );
273cdf0e10cSrcweir 	}
274cdf0e10cSrcweir 	return aServiceNames;
275cdf0e10cSrcweir }
276