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