LibreOffice Module vbahelper (master) 1
vbacommandbars.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
20#include <com/sun/star/container/XNameAccess.hpp>
21#include <ooo/vba/office/MsoBarType.hpp>
23#include <utility>
24
25#include "vbacommandbars.hxx"
26#include "vbacommandbar.hxx"
27
28using namespace com::sun::star;
29using namespace ooo::vba;
30
31namespace {
32
33class CommandBarEnumeration : public ::cppu::WeakImplHelper< container::XEnumeration >
34{
35 uno::Reference< XHelperInterface > m_xParent;
36 uno::Reference< uno::XComponentContext > m_xContext;
37 VbaCommandBarHelperRef m_pCBarHelper;
38 uno::Sequence< OUString > m_sNames;
39 sal_Int32 m_nCurrentPosition;
40public:
42 CommandBarEnumeration( uno::Reference< XHelperInterface > xParent, uno::Reference< uno::XComponentContext > xContext, VbaCommandBarHelperRef pHelper) : m_xParent(std::move( xParent )), m_xContext(std::move( xContext )), m_pCBarHelper(std::move( pHelper )) , m_nCurrentPosition( 0 )
43 {
44 uno::Reference< container::XNameAccess > xNameAccess = m_pCBarHelper->getPersistentWindowState();
45 m_sNames = xNameAccess->getElementNames();
46 }
47 virtual sal_Bool SAL_CALL hasMoreElements() override
48 {
49 return m_nCurrentPosition < m_sNames.getLength();
50 }
51 virtual uno::Any SAL_CALL nextElement() override
52 {
53 // FIXME: should be add menubar
54 if( !hasMoreElements() )
55 throw container::NoSuchElementException();
56
57 OUString sResourceUrl( m_sNames[ m_nCurrentPosition++ ] );
58 if( sResourceUrl.indexOf( "private:resource/toolbar/" ) != -1 )
59 {
60 uno::Reference< container::XIndexAccess > xCBarSetting = m_pCBarHelper->getSettings( sResourceUrl );
61 uno::Reference< XCommandBar > xCommandBar( new ScVbaCommandBar( m_xParent, m_xContext, m_pCBarHelper, xCBarSetting, sResourceUrl, false ) );
62 // Strange, shouldn't the Enumeration support match/share the
63 // iteration code? ( e.g. ScVbaCommandBars::Item(...) )
64 // and we at least should return here ( something ) it seems
65 return uno::Any( xCommandBar );
66 }
67 else
68 return nextElement();
69 }
70};
71
72}
73
74ScVbaCommandBars::ScVbaCommandBars( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext >& xContext, const uno::Reference< container::XIndexAccess >& xIndexAccess, const uno::Reference< frame::XModel >& xModel ) : CommandBars_BASE( xParent, xContext, xIndexAccess )
75{
76 m_pCBarHelper = std::make_shared<VbaCommandBarHelper>( mxContext, xModel );
77 m_xNameAccess = m_pCBarHelper->getPersistentWindowState();
78}
79
81{
82}
83
84// XEnumerationAccess
85uno::Type SAL_CALL
87{
89}
90
91uno::Reference< container::XEnumeration >
93{
94 return uno::Reference< container::XEnumeration >( new CommandBarEnumeration( this, mxContext, m_pCBarHelper ) );
95}
96
99{
100 // aSource should be a name at this time, because of the class is API wrapper.
101 OUString sResourceUrl;
102 uno::Reference< container::XIndexAccess > xBarSettings;
103 OUString sBarName;
104 bool bMenu = false;
105 uno::Any aRet;
106
107 if( aSource >>= sBarName )
108 {
109 // some built-in command bars
110 if( m_pCBarHelper->getModuleId() == "com.sun.star.sheet.SpreadsheetDocument" )
111 {
112 if( sBarName.equalsIgnoreAsciiCase( "Worksheet Menu Bar" ) )
113 {
114 // spreadsheet menu bar
115 sResourceUrl = ITEM_MENUBAR_URL;
116 bMenu = true;
117 }
118 else if( sBarName.equalsIgnoreAsciiCase( "Cell" ) )
119 {
120 // EVIL HACK (tm): spreadsheet cell context menu as dummy object without functionality
121 aRet <<= uno::Reference< XCommandBar >( new VbaDummyCommandBar( this, mxContext, sBarName ) );
122 }
123 }
124 else if( m_pCBarHelper->getModuleId() == "com.sun.star.text.TextDocument" )
125 {
126 if( sBarName.equalsIgnoreAsciiCase( "Menu Bar" ) )
127 {
128 // text processor menu bar
129 sResourceUrl = ITEM_MENUBAR_URL;
130 bMenu = true;
131 }
132 }
133
134 // nothing found - try to resolve from name
135 if( !aRet.hasValue() && sResourceUrl.isEmpty() )
136 {
137 sResourceUrl = m_pCBarHelper->findToolbarByName( m_xNameAccess, sBarName );
138 bMenu = false;
139 }
140 }
141
142 if( !sResourceUrl.isEmpty() )
143 {
144 xBarSettings = m_pCBarHelper->getSettings( sResourceUrl );
145 aRet <<= uno::Reference< XCommandBar >( new ScVbaCommandBar( this, mxContext, m_pCBarHelper, xBarSettings, sResourceUrl, bMenu ) );
146 }
147
148 if( !aRet.hasValue() )
149 throw uno::RuntimeException( "Toolbar do not exist" );
150
151 return aRet;
152}
153
154// XCommandBars
155uno::Reference< XCommandBar > SAL_CALL
156ScVbaCommandBars::Add( const css::uno::Any& Name, const css::uno::Any& /*Position*/, const css::uno::Any& /*MenuBar*/, const css::uno::Any& /*Temporary*/ )
157{
158 // FIXME: only support to add Toolbar
159 // Position - MsoBar MenuBar - sal_Bool
160 // Currently only the Name is supported.
161 OUString sName;
162 if( Name.hasValue() )
163 Name >>= sName;
164
165 OUString sResourceUrl;
166 if( !sName.isEmpty() )
167 {
168 sResourceUrl = m_pCBarHelper->findToolbarByName( m_xNameAccess, sName );
169 if( !sResourceUrl.isEmpty() )
170 throw uno::RuntimeException( "Toolbar exists" );
171 }
172 else
173 {
174 sName = "Custom1";
175 }
176
178 uno::Reference< container::XIndexAccess > xBarSettings( m_pCBarHelper->getSettings( sResourceUrl ), uno::UNO_SET_THROW );
179 uno::Reference< XCommandBar > xCBar( new ScVbaCommandBar( this, mxContext, m_pCBarHelper, xBarSettings, sResourceUrl, false ) );
180 xCBar->setName( sName );
181 return xCBar;
182}
183sal_Int32 SAL_CALL
185{
186 // Filter out all toolbars from the window collection
187 sal_Int32 nCount = 1; // there is a Menubar in OOo
188 const uno::Sequence< ::OUString > allNames = m_xNameAccess->getElementNames();
189 nCount += std::count_if(allNames.begin(), allNames.end(),
190 [](const OUString& rName) { return rName.indexOf( "private:resource/toolbar/" ) != -1; });
191 return nCount;
192}
193
194// ScVbaCollectionBaseImpl
195uno::Any SAL_CALL
196ScVbaCommandBars::Item( const uno::Any& aIndex, const uno::Any& /*aIndex2*/ )
197{
198 if( aIndex.getValueTypeClass() == uno::TypeClass_STRING )
199 {
201 }
202
203 // hardcode if "aIndex = 1" that would return "main menu".
204 sal_Int32 nIndex = 0;
205 aIndex >>= nIndex;
206 if( nIndex == 1 )
207 {
208 uno::Any aSource;
209 if( m_pCBarHelper->getModuleId() == "com.sun.star.sheet.SpreadsheetDocument" )
210 aSource <<= OUString("Worksheet Menu Bar");
211 else if( m_pCBarHelper->getModuleId() == "com.sun.star.text.TextDocument" )
212 aSource <<= OUString("Menu Bar");
213 if( aSource.hasValue() )
214 return createCollectionObject( aSource );
215 }
216 return uno::Any();
217}
218
219// XHelperInterface
220OUString
222{
223 return "ScVbaCommandBars";
224}
225
226uno::Sequence<OUString>
228{
229 static uno::Sequence< OUString > const aServiceNames
230 {
231 "ooo.vba.CommandBars"
232 };
233 return aServiceNames;
234}
235
236/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Reference< XComponentContext > m_xContext
css::uno::Reference< css::uno::XComponentContext > mxContext
css::uno::Reference< css::container::XNameAccess > m_xNameAccess
virtual css::uno::Any SAL_CALL Item(const css::uno::Any &aIndex, const css::uno::Any &) override
virtual ~ScVbaCommandBars() override
VbaCommandBarHelperRef m_pCBarHelper
virtual css::uno::Any createCollectionObject(const css::uno::Any &aSource) override
ScVbaCommandBars(const css::uno::Reference< ov::XHelperInterface > &xParent, const css::uno::Reference< css::uno::XComponentContext > &xContext, const css::uno::Reference< css::container::XIndexAccess > &xIndexAccess, const css::uno::Reference< css::frame::XModel > &xModel)
virtual css::uno::Reference< css::container::XEnumeration > SAL_CALL createEnumeration() override
virtual sal_Int32 SAL_CALL getCount() override
virtual css::uno::Type SAL_CALL getElementType() override
virtual css::uno::Reference< ov::XCommandBar > SAL_CALL Add(const css::uno::Any &Name, const css::uno::Any &Position, const css::uno::Any &MenuBar, const css::uno::Any &Temporary) override
virtual css::uno::Sequence< OUString > getServiceNames() override
virtual OUString getServiceImplName() override
static OUString generateCustomURL()
Dummy command bar implementation.
css::uno::Type const & get()
int nCount
std::deque< AttacherIndex_Impl > aIndex
Sequence< OUString > aServiceNames
OUString sName
sal_Int32 nIndex
bool hasValue()
Reference< XModel > xModel
OUString Name
unsigned char sal_Bool
std::shared_ptr< VbaCommandBarHelper > VbaCommandBarHelperRef
constexpr OUStringLiteral ITEM_MENUBAR_URL