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 "vbacommandbar.hxx"
24 #include "vbacommandbarcontrols.hxx"
25 #include <com/sun/star/ui/XModuleUIConfigurationManagerSupplier.hpp>
26 #include <com/sun/star/frame/XFrame.hpp>
27 #include <com/sun/star/frame/XDesktop.hpp>
28 #include <com/sun/star/frame/XLayoutManager.hpp>
29 #include <com/sun/star/beans/XPropertySet.hpp>
30 #include <com/sun/star/container/XNameContainer.hpp>
31 #include <ooo/vba/office/MsoBarType.hpp>
32 
33 using namespace com::sun::star;
34 using namespace ooo::vba;
35 
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)36 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 )
37 {
38 }
39 
40 ::rtl::OUString SAL_CALL
getName()41 ScVbaCommandBar::getName() throw ( uno::RuntimeException )
42 {
43     // This will get a "NULL length string" when Name is not set.
44     uno::Reference< beans::XPropertySet > xPropertySet( m_xBarSettings, uno::UNO_QUERY_THROW );
45     uno::Any aName = xPropertySet->getPropertyValue( rtl::OUString::createFromAscii("UIName") );
46     rtl::OUString sName;
47     aName >>= sName;
48     if( sName.getLength() < 1 )
49     {
50         if( m_bIsMenu )
51         {
52             if( m_sResourceUrl.equalsAscii( ITEM_MENUBAR_URL ) )
53             {
54                 if( pCBarHelper->getModuleId().equalsAscii("com.sun.star.sheet.SpreadsheetDocument") )
55                     sName = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Worksheet Menu Bar") );
56                 else if( pCBarHelper->getModuleId().equalsAscii("com.sun.star.text.TextDocument") )
57                     sName = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Menu Bar") );
58                 return sName;
59             }
60         }
61         // Toolbar name
62         uno::Reference< container::XNameAccess > xNameAccess = pCBarHelper->getPersistentWindowState();
63         if( xNameAccess->hasByName( m_sResourceUrl ) )
64         {
65             uno::Sequence< beans::PropertyValue > aToolBar;
66             xNameAccess->getByName( m_sResourceUrl ) >>= aToolBar;
67             getPropertyValue( aToolBar, rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("UIName") ) ) >>= sName;
68         }
69     }
70     return sName;
71 }
72 void SAL_CALL
setName(const::rtl::OUString & _name)73 ScVbaCommandBar::setName( const ::rtl::OUString& _name ) throw (uno::RuntimeException)
74 {
75     uno::Reference< beans::XPropertySet > xPropertySet( m_xBarSettings, uno::UNO_QUERY_THROW );
76     xPropertySet->setPropertyValue( rtl::OUString::createFromAscii("UIName"), uno::makeAny( _name ) );
77 
78     pCBarHelper->ApplyChange( m_sResourceUrl, m_xBarSettings );
79 }
80 ::sal_Bool SAL_CALL
getVisible()81 ScVbaCommandBar::getVisible() throw (uno::RuntimeException)
82 {
83     // menu bar is allways visible in OOo
84     if( m_bIsMenu )
85         return sal_True;
86 
87     sal_Bool bVisible = sal_False;
88     try
89     {
90         uno::Reference< container::XNameAccess > xNameAccess = pCBarHelper->getPersistentWindowState();
91         if( xNameAccess->hasByName( m_sResourceUrl ) )
92         {
93             uno::Sequence< beans::PropertyValue > aToolBar;
94             xNameAccess->getByName( m_sResourceUrl ) >>= aToolBar;
95             getPropertyValue( aToolBar, rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("Visible") ) ) >>= bVisible;
96         }
97     }
98     catch ( uno::Exception e )
99     {
100     }
101     return bVisible;
102 }
103 void SAL_CALL
setVisible(::sal_Bool _visible)104 ScVbaCommandBar::setVisible( ::sal_Bool _visible ) throw (uno::RuntimeException)
105 {
106     try
107     {
108         uno::Reference< frame::XLayoutManager > xLayoutManager = pCBarHelper->getLayoutManager();
109         if( _visible )
110         {
111             xLayoutManager->createElement( m_sResourceUrl );
112             xLayoutManager->showElement( m_sResourceUrl );
113         }
114         else
115         {
116             xLayoutManager->hideElement( m_sResourceUrl );
117             xLayoutManager->destroyElement( m_sResourceUrl );
118         }
119     }
120     catch( uno::Exception e )
121     {
122         OSL_TRACE( "SetVisible get an exception\n" );
123     }
124 }
125 
126 ::sal_Bool SAL_CALL
getEnabled()127 ScVbaCommandBar::getEnabled() throw (uno::RuntimeException)
128 {
129     // emulated with Visible
130     return getVisible();
131 }
132 
133 void SAL_CALL
setEnabled(sal_Bool _enabled)134 ScVbaCommandBar::setEnabled( sal_Bool _enabled ) throw (uno::RuntimeException)
135 {
136     // emulated with Visible
137     setVisible( _enabled );
138 }
139 
140 void SAL_CALL
Delete()141 ScVbaCommandBar::Delete(  ) throw (script::BasicErrorException, uno::RuntimeException)
142 {
143     pCBarHelper->removeSettings( m_sResourceUrl );
144     uno::Reference< container::XNameContainer > xNameContainer( pCBarHelper->getPersistentWindowState(), uno::UNO_QUERY_THROW );
145     if( xNameContainer->hasByName( m_sResourceUrl ) )
146     {
147         xNameContainer->removeByName( m_sResourceUrl );
148     }
149 }
150 uno::Any SAL_CALL
Controls(const uno::Any & aIndex)151 ScVbaCommandBar::Controls( const uno::Any& aIndex ) throw (script::BasicErrorException, uno::RuntimeException)
152 {
153     uno::Reference< XCommandBarControls > xCommandBarControls( new ScVbaCommandBarControls( this, mxContext, m_xBarSettings, pCBarHelper, m_xBarSettings, m_sResourceUrl ) );
154     if( aIndex.hasValue() )
155     {
156         return xCommandBarControls->Item( aIndex, uno::Any() );
157     }
158     return uno::makeAny( xCommandBarControls );
159 }
160 
161 sal_Int32 SAL_CALL
Type()162 ScVbaCommandBar::Type() throw (script::BasicErrorException, uno::RuntimeException)
163 {
164     // #FIXME support msoBarTypePopup
165     sal_Int32 nType = office::MsoBarType::msoBarTypePopup;
166     nType = m_bIsMenu? office::MsoBarType::msoBarTypeNormal : office::MsoBarType::msoBarTypeMenuBar;
167     return nType;
168 }
169 
170 uno::Any SAL_CALL
FindControl(const uno::Any &,const uno::Any &,const uno::Any &,const uno::Any &,const uno::Any &)171 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)
172 {
173     // alwayse fail to find control
174     return uno::makeAny( uno::Reference< XCommandBarControl > () );
175 }
176 
177 rtl::OUString&
getServiceImplName()178 ScVbaCommandBar::getServiceImplName()
179 {
180 	static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("ScVbaCommandBar") );
181 	return sImplName;
182 }
183 uno::Sequence<rtl::OUString>
getServiceNames()184 ScVbaCommandBar::getServiceNames()
185 {
186 	static uno::Sequence< rtl::OUString > aServiceNames;
187 	if ( aServiceNames.getLength() == 0 )
188 	{
189 		aServiceNames.realloc( 1 );
190 		aServiceNames[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.CommandBar" ) );
191 	}
192 	return aServiceNames;
193 }
194 
195 
VbaDummyCommandBar(const uno::Reference<ov::XHelperInterface> xParent,const uno::Reference<uno::XComponentContext> xContext,const::rtl::OUString & rName,sal_Int32 nType)196 VbaDummyCommandBar::VbaDummyCommandBar(
197         const uno::Reference< ov::XHelperInterface > xParent,
198         const uno::Reference< uno::XComponentContext > xContext,
199         const ::rtl::OUString& rName, sal_Int32 nType ) throw( uno::RuntimeException ) :
200     CommandBar_BASE( xParent, xContext ),
201     maName( rName ),
202     mnType( nType )
203 {
204 }
205 
getName()206 ::rtl::OUString SAL_CALL VbaDummyCommandBar::getName() throw ( uno::RuntimeException )
207 {
208     return maName;
209 }
210 
setName(const::rtl::OUString & _name)211 void SAL_CALL VbaDummyCommandBar::setName( const ::rtl::OUString& _name ) throw (uno::RuntimeException)
212 {
213     maName = _name;
214 }
215 
getVisible()216 ::sal_Bool SAL_CALL VbaDummyCommandBar::getVisible() throw (uno::RuntimeException)
217 {
218     return sal_True;
219 }
220 
setVisible(::sal_Bool)221 void SAL_CALL VbaDummyCommandBar::setVisible( ::sal_Bool /*_visible*/ ) throw (uno::RuntimeException)
222 {
223 }
224 
getEnabled()225 ::sal_Bool SAL_CALL VbaDummyCommandBar::getEnabled() throw (uno::RuntimeException)
226 {
227     // emulated with Visible
228     return getVisible();
229 }
230 
setEnabled(sal_Bool _enabled)231 void SAL_CALL VbaDummyCommandBar::setEnabled( sal_Bool _enabled ) throw (uno::RuntimeException)
232 {
233     // emulated with Visible
234     setVisible( _enabled );
235 }
236 
Delete()237 void SAL_CALL VbaDummyCommandBar::Delete(  ) throw (script::BasicErrorException, uno::RuntimeException)
238 {
239     // no-op
240 }
241 
Controls(const uno::Any & aIndex)242 uno::Any SAL_CALL VbaDummyCommandBar::Controls( const uno::Any& aIndex ) throw (script::BasicErrorException, uno::RuntimeException)
243 {
244     uno::Reference< XCommandBarControls > xCommandBarControls( new VbaDummyCommandBarControls( this, mxContext ) );
245     if( aIndex.hasValue() )
246         return xCommandBarControls->Item( aIndex, uno::Any() );
247     return uno::Any( xCommandBarControls );
248 }
249 
Type()250 sal_Int32 SAL_CALL VbaDummyCommandBar::Type() throw (script::BasicErrorException, uno::RuntimeException)
251 {
252     return mnType;
253 }
254 
FindControl(const uno::Any &,const uno::Any &,const uno::Any &,const uno::Any &,const uno::Any &)255 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)
256 {
257     return uno::Any( uno::Reference< XCommandBarControl >() );
258 }
259 
getServiceImplName()260 rtl::OUString& VbaDummyCommandBar::getServiceImplName()
261 {
262 	static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("VbaDummyCommandBar") );
263 	return sImplName;
264 }
265 
getServiceNames()266 uno::Sequence< rtl::OUString > VbaDummyCommandBar::getServiceNames()
267 {
268 	static uno::Sequence< rtl::OUString > aServiceNames;
269 	if ( aServiceNames.getLength() == 0 )
270 	{
271 		aServiceNames.realloc( 1 );
272 		aServiceNames[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.CommandBar" ) );
273 	}
274 	return aServiceNames;
275 }
276