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 <com/sun/star/lang/XServiceInfo.hpp>
24cdf0e10cSrcweir #include <com/sun/star/frame/XDesktop.hpp>
25cdf0e10cSrcweir #include <com/sun/star/container/XNameAccess.hpp>
26cdf0e10cSrcweir #include <com/sun/star/ui/XUIConfigurationManagerSupplier.hpp>
27cdf0e10cSrcweir #include <com/sun/star/ui/XUIConfigurationStorage.hpp>
28cdf0e10cSrcweir #include <com/sun/star/ui/XModuleUIConfigurationManager.hpp>
29cdf0e10cSrcweir #include <com/sun/star/ui/XUIConfigurationPersistence.hpp>
30cdf0e10cSrcweir #include <ooo/vba/office/MsoBarType.hpp>
31cdf0e10cSrcweir 
32cdf0e10cSrcweir #include "vbacommandbars.hxx"
33cdf0e10cSrcweir #include "vbacommandbar.hxx"
34cdf0e10cSrcweir 
35cdf0e10cSrcweir using namespace com::sun::star;
36cdf0e10cSrcweir using namespace ooo::vba;
37cdf0e10cSrcweir 
38cdf0e10cSrcweir 
39cdf0e10cSrcweir typedef ::cppu::WeakImplHelper1< container::XEnumeration > CommandBarEnumeration_BASE;
40cdf0e10cSrcweir 
41cdf0e10cSrcweir class CommandBarEnumeration : public CommandBarEnumeration_BASE
42cdf0e10cSrcweir {
43cdf0e10cSrcweir     uno::Reference< XHelperInterface > m_xParent;
44cdf0e10cSrcweir     uno::Reference< uno::XComponentContext > m_xContext;
45cdf0e10cSrcweir     VbaCommandBarHelperRef m_pCBarHelper;
46cdf0e10cSrcweir     uno::Sequence< rtl::OUString > m_sNames;
47cdf0e10cSrcweir     sal_Int32 m_nCurrentPosition;
48cdf0e10cSrcweir public:
CommandBarEnumeration(const uno::Reference<XHelperInterface> & xParent,const uno::Reference<uno::XComponentContext> & xContext,VbaCommandBarHelperRef pHelper)49cdf0e10cSrcweir     CommandBarEnumeration( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext >& xContext, VbaCommandBarHelperRef pHelper) throw ( uno::RuntimeException ) : m_xParent( xParent ), m_xContext( xContext ), m_pCBarHelper( pHelper ) , m_nCurrentPosition( 0 )
50cdf0e10cSrcweir     {
51cdf0e10cSrcweir         uno::Reference< container::XNameAccess > xNameAccess = m_pCBarHelper->getPersistentWindowState();
52cdf0e10cSrcweir         m_sNames = xNameAccess->getElementNames();
53cdf0e10cSrcweir     }
hasMoreElements()54cdf0e10cSrcweir     virtual sal_Bool SAL_CALL hasMoreElements() throw ( uno::RuntimeException )
55cdf0e10cSrcweir     {
56cdf0e10cSrcweir         if( m_nCurrentPosition < m_sNames.getLength() )
57cdf0e10cSrcweir             return sal_True;
58cdf0e10cSrcweir         return sal_False;
59cdf0e10cSrcweir     }
nextElement()60cdf0e10cSrcweir     virtual uno::Any SAL_CALL nextElement() throw ( container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException )
61cdf0e10cSrcweir     {
62cdf0e10cSrcweir         // FIXME: should be add menubar
63cdf0e10cSrcweir         if( hasMoreElements() )
64cdf0e10cSrcweir         {
65cdf0e10cSrcweir             rtl::OUString sResourceUrl( m_sNames[ m_nCurrentPosition++ ] );
66cdf0e10cSrcweir             if( sResourceUrl.indexOf( rtl::OUString::createFromAscii("private:resource/toolbar/") ) != -1 )
67cdf0e10cSrcweir             {
68cdf0e10cSrcweir                 uno::Reference< container::XIndexAccess > xCBarSetting = m_pCBarHelper->getSettings( sResourceUrl );
69cdf0e10cSrcweir                 uno::Reference< XCommandBar > xCommandBar( new ScVbaCommandBar( m_xParent, m_xContext, m_pCBarHelper, xCBarSetting, sResourceUrl, sal_False, sal_False ) );
70cdf0e10cSrcweir              }
71cdf0e10cSrcweir              else
72cdf0e10cSrcweir                 return nextElement();
73cdf0e10cSrcweir         }
74cdf0e10cSrcweir         else
75cdf0e10cSrcweir             throw container::NoSuchElementException();
76cdf0e10cSrcweir         return uno::Any();
77cdf0e10cSrcweir     }
78cdf0e10cSrcweir };
79cdf0e10cSrcweir 
ScVbaCommandBars(const uno::Reference<XHelperInterface> & xParent,const uno::Reference<uno::XComponentContext> & xContext,const uno::Reference<container::XIndexAccess> & xIndexAccess,const uno::Reference<frame::XModel> & xModel)80cdf0e10cSrcweir ScVbaCommandBars::ScVbaCommandBars( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext >& xContext, const uno::Reference< container::XIndexAccess >& xIndexAccess, const uno::Reference< frame::XModel >& xModel ) throw ( uno::RuntimeException ) : CommandBars_BASE( xParent, xContext, xIndexAccess )
81cdf0e10cSrcweir {
82cdf0e10cSrcweir     m_pCBarHelper.reset( new VbaCommandBarHelper( mxContext, xModel ) );
83cdf0e10cSrcweir     m_xNameAccess = m_pCBarHelper->getPersistentWindowState();
84cdf0e10cSrcweir }
85cdf0e10cSrcweir 
~ScVbaCommandBars()86cdf0e10cSrcweir ScVbaCommandBars::~ScVbaCommandBars()
87cdf0e10cSrcweir {
88cdf0e10cSrcweir }
89cdf0e10cSrcweir 
90cdf0e10cSrcweir // XEnumerationAccess
91cdf0e10cSrcweir uno::Type SAL_CALL
getElementType()92cdf0e10cSrcweir ScVbaCommandBars::getElementType() throw ( uno::RuntimeException )
93cdf0e10cSrcweir {
94cdf0e10cSrcweir     return XCommandBar::static_type( 0 );
95cdf0e10cSrcweir }
96cdf0e10cSrcweir 
97cdf0e10cSrcweir uno::Reference< container::XEnumeration >
createEnumeration()98cdf0e10cSrcweir ScVbaCommandBars::createEnumeration() throw ( uno::RuntimeException )
99cdf0e10cSrcweir {
100cdf0e10cSrcweir     return uno::Reference< container::XEnumeration >( new CommandBarEnumeration( this, mxContext, m_pCBarHelper ) );
101cdf0e10cSrcweir }
102cdf0e10cSrcweir 
103cdf0e10cSrcweir uno::Any
createCollectionObject(const uno::Any & aSource)104cdf0e10cSrcweir ScVbaCommandBars::createCollectionObject( const uno::Any& aSource )
105cdf0e10cSrcweir {
106cdf0e10cSrcweir     // aSource should be a name at this time, because of the class is API wrapper.
107cdf0e10cSrcweir     rtl::OUString sResourceUrl;
108cdf0e10cSrcweir     uno::Reference< container::XIndexAccess > xBarSettings;
109cdf0e10cSrcweir     rtl::OUString sBarName;
110cdf0e10cSrcweir     sal_Bool bMenu = sal_False;
111cdf0e10cSrcweir     uno::Any aRet;
112cdf0e10cSrcweir 
113cdf0e10cSrcweir     if( aSource >>= sBarName )
114cdf0e10cSrcweir     {
115cdf0e10cSrcweir         // some built-in command bars
116cdf0e10cSrcweir         if( m_pCBarHelper->getModuleId().equalsAsciiL( RTL_CONSTASCII_STRINGPARAM("com.sun.star.sheet.SpreadsheetDocument") ) )
117cdf0e10cSrcweir         {
118cdf0e10cSrcweir             if( sBarName.equalsIgnoreAsciiCaseAsciiL( RTL_CONSTASCII_STRINGPARAM("Worksheet Menu Bar") ) )
119cdf0e10cSrcweir             {
120cdf0e10cSrcweir                 // spreadsheet menu bar
121cdf0e10cSrcweir                 sResourceUrl = rtl::OUString::createFromAscii( ITEM_MENUBAR_URL );
122cdf0e10cSrcweir                 bMenu = sal_True;
123cdf0e10cSrcweir             }
124cdf0e10cSrcweir             else if( sBarName.equalsIgnoreAsciiCaseAsciiL( RTL_CONSTASCII_STRINGPARAM("Cell") ) )
125cdf0e10cSrcweir             {
126cdf0e10cSrcweir                 // EVIL HACK (tm): spreadsheet cell context menu as dummy object without functionality
127cdf0e10cSrcweir                 aRet <<= uno::Reference< XCommandBar >( new VbaDummyCommandBar( this, mxContext, sBarName, office::MsoBarType::msoBarTypePopup ) );
128cdf0e10cSrcweir             }
129cdf0e10cSrcweir         }
130cdf0e10cSrcweir         else if( m_pCBarHelper->getModuleId().equalsAsciiL( RTL_CONSTASCII_STRINGPARAM("com.sun.star.text.TextDocument") ) )
131cdf0e10cSrcweir         {
132cdf0e10cSrcweir             if( sBarName.equalsIgnoreAsciiCaseAsciiL( RTL_CONSTASCII_STRINGPARAM("Menu Bar") ) )
133cdf0e10cSrcweir             {
134cdf0e10cSrcweir                 // text processor menu bar
135cdf0e10cSrcweir                 sResourceUrl = rtl::OUString::createFromAscii( ITEM_MENUBAR_URL );
136cdf0e10cSrcweir                 bMenu = sal_True;
137cdf0e10cSrcweir             }
138cdf0e10cSrcweir         }
139cdf0e10cSrcweir 
140cdf0e10cSrcweir         // nothing found - try to resolve from name
141cdf0e10cSrcweir         if( !aRet.hasValue() && (sResourceUrl.getLength() == 0) )
142cdf0e10cSrcweir         {
143cdf0e10cSrcweir             sResourceUrl = m_pCBarHelper->findToolbarByName( m_xNameAccess, sBarName );
144cdf0e10cSrcweir             bMenu = sal_False;
145cdf0e10cSrcweir         }
146cdf0e10cSrcweir     }
147cdf0e10cSrcweir 
148cdf0e10cSrcweir     if( sResourceUrl.getLength() )
149cdf0e10cSrcweir     {
150cdf0e10cSrcweir         xBarSettings = m_pCBarHelper->getSettings( sResourceUrl );
151cdf0e10cSrcweir         aRet <<= uno::Reference< XCommandBar >( new ScVbaCommandBar( this, mxContext, m_pCBarHelper, xBarSettings, sResourceUrl, bMenu, sal_False ) );
152cdf0e10cSrcweir     }
153cdf0e10cSrcweir 
154cdf0e10cSrcweir     if( !aRet.hasValue() )
155cdf0e10cSrcweir         throw uno::RuntimeException( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Toolbar do not exist") ), uno::Reference< uno::XInterface >() );
156cdf0e10cSrcweir 
157cdf0e10cSrcweir     return aRet;
158cdf0e10cSrcweir }
159cdf0e10cSrcweir 
160cdf0e10cSrcweir // XCommandBars
161cdf0e10cSrcweir uno::Reference< XCommandBar > SAL_CALL
Add(const css::uno::Any & Name,const css::uno::Any &,const css::uno::Any &,const css::uno::Any & Temporary)162cdf0e10cSrcweir ScVbaCommandBars::Add( const css::uno::Any& Name, const css::uno::Any& /*Position*/, const css::uno::Any& /*MenuBar*/, const css::uno::Any& Temporary ) throw (css::script::BasicErrorException, css::uno::RuntimeException)
163cdf0e10cSrcweir {
164cdf0e10cSrcweir     // FIXME: only support to add Toolbar
165cdf0e10cSrcweir     // Position - MsoBar MenuBar - sal_Bool
166cdf0e10cSrcweir     // Currently only the Name is supported.
167cdf0e10cSrcweir     rtl::OUString sName;
168cdf0e10cSrcweir     if( Name.hasValue() )
169cdf0e10cSrcweir         Name >>= sName;
170cdf0e10cSrcweir 
171cdf0e10cSrcweir     rtl::OUString sResourceUrl;
172cdf0e10cSrcweir     if( sName.getLength() )
173cdf0e10cSrcweir     {
174cdf0e10cSrcweir         sResourceUrl = m_pCBarHelper->findToolbarByName( m_xNameAccess, sName );
175cdf0e10cSrcweir         if( sResourceUrl.getLength() )
176cdf0e10cSrcweir             throw uno::RuntimeException( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Toolbar exists") ), uno::Reference< uno::XInterface >() );
177cdf0e10cSrcweir     }
178cdf0e10cSrcweir     else
179cdf0e10cSrcweir     {
180cdf0e10cSrcweir         sName = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Custom1") );
181cdf0e10cSrcweir     }
182cdf0e10cSrcweir 
183cdf0e10cSrcweir     sal_Bool bTemporary = sal_False;
184cdf0e10cSrcweir     if( Temporary.hasValue() )
185cdf0e10cSrcweir         Temporary >>= bTemporary;
186cdf0e10cSrcweir 
187cdf0e10cSrcweir     sResourceUrl = VbaCommandBarHelper::generateCustomURL();
188cdf0e10cSrcweir     uno::Reference< container::XIndexAccess > xBarSettings( m_pCBarHelper->getSettings( sResourceUrl ), uno::UNO_QUERY_THROW );
189cdf0e10cSrcweir     uno::Reference< XCommandBar > xCBar( new ScVbaCommandBar( this, mxContext, m_pCBarHelper, xBarSettings, sResourceUrl, sal_False, bTemporary ) );
190cdf0e10cSrcweir     xCBar->setName( sName );
191cdf0e10cSrcweir     return xCBar;
192cdf0e10cSrcweir }
193cdf0e10cSrcweir sal_Int32 SAL_CALL
getCount()194cdf0e10cSrcweir ScVbaCommandBars::getCount() throw(css::uno::RuntimeException)
195cdf0e10cSrcweir {
196cdf0e10cSrcweir     // Filter out all toolbars from the window collection
197cdf0e10cSrcweir     sal_Int32 nCount = 1; // there is a Menubar in OOo
198cdf0e10cSrcweir     uno::Sequence< ::rtl::OUString > allNames = m_xNameAccess->getElementNames();
199cdf0e10cSrcweir     for( sal_Int32 i = 0; i < allNames.getLength(); i++ )
200cdf0e10cSrcweir     {
201cdf0e10cSrcweir         if(allNames[i].indexOf( rtl::OUString::createFromAscii("private:resource/toolbar/") ) != -1 )
202cdf0e10cSrcweir         {
203cdf0e10cSrcweir             nCount++;
204cdf0e10cSrcweir         }
205cdf0e10cSrcweir     }
206cdf0e10cSrcweir     return nCount;
207cdf0e10cSrcweir }
208cdf0e10cSrcweir 
209cdf0e10cSrcweir // ScVbaCollectionBaseImpl
210cdf0e10cSrcweir uno::Any SAL_CALL
Item(const uno::Any & aIndex,const uno::Any &)211cdf0e10cSrcweir ScVbaCommandBars::Item( const uno::Any& aIndex, const uno::Any& /*aIndex2*/ ) throw( uno::RuntimeException )
212cdf0e10cSrcweir {
213cdf0e10cSrcweir     if( aIndex.getValueTypeClass() == uno::TypeClass_STRING )
214cdf0e10cSrcweir     {
215cdf0e10cSrcweir         return createCollectionObject( aIndex );
216cdf0e10cSrcweir     }
217cdf0e10cSrcweir 
218cdf0e10cSrcweir     // hardcode if "aIndex = 1" that would return "main menu".
219cdf0e10cSrcweir     sal_Int16 nIndex = 0;
220cdf0e10cSrcweir     aIndex >>= nIndex;
221cdf0e10cSrcweir     if( nIndex == 1 )
222cdf0e10cSrcweir     {
223cdf0e10cSrcweir         uno::Any aSource;
224cdf0e10cSrcweir         if( m_pCBarHelper->getModuleId().equalsAscii( "com.sun.star.sheet.SpreadsheetDocument" ) )
225cdf0e10cSrcweir             aSource <<= rtl::OUString::createFromAscii( "Worksheet Menu Bar" );
226cdf0e10cSrcweir         else if( m_pCBarHelper->getModuleId().equalsAscii("com.sun.star.text.TextDocument") )
227cdf0e10cSrcweir             aSource <<= rtl::OUString::createFromAscii( "Menu Bar" );
228cdf0e10cSrcweir         if( aSource.hasValue() )
229cdf0e10cSrcweir             return createCollectionObject( aSource );
230cdf0e10cSrcweir     }
231cdf0e10cSrcweir     return uno::Any();
232cdf0e10cSrcweir }
233cdf0e10cSrcweir 
234cdf0e10cSrcweir // XHelperInterface
235cdf0e10cSrcweir rtl::OUString&
getServiceImplName()236cdf0e10cSrcweir ScVbaCommandBars::getServiceImplName()
237cdf0e10cSrcweir {
238cdf0e10cSrcweir     static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("ScVbaCommandBars") );
239cdf0e10cSrcweir     return sImplName;
240cdf0e10cSrcweir }
241cdf0e10cSrcweir uno::Sequence<rtl::OUString>
getServiceNames()242cdf0e10cSrcweir ScVbaCommandBars::getServiceNames()
243cdf0e10cSrcweir {
244cdf0e10cSrcweir     static uno::Sequence< rtl::OUString > aServiceNames;
245cdf0e10cSrcweir     if ( aServiceNames.getLength() == 0 )
246cdf0e10cSrcweir     {
247cdf0e10cSrcweir         aServiceNames.realloc( 1 );
248cdf0e10cSrcweir         aServiceNames[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.CommandBars" ) );
249cdf0e10cSrcweir     }
250cdf0e10cSrcweir     return aServiceNames;
251cdf0e10cSrcweir }
252cdf0e10cSrcweir 
253