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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/* -*- 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 "vbacommandbarcontrol.hxx"
#include "vbacommandbarcontrols.hxx"
#include <vbahelper/vbahelper.hxx>
#include <filter/msfilter/msvbahelper.hxx>
#include <sal/log.hxx>

using namespace com::sun::star;
using namespace ooo::vba;

ScVbaCommandBarControl::ScVbaCommandBarControl( const css::uno::Reference< ov::XHelperInterface >& xParent, const css::uno::Reference< css::uno::XComponentContext >& xContext, const css::uno::Reference< css::container::XIndexAccess >& xSettings, const VbaCommandBarHelperRef& pHelper, const css::uno::Reference< css::container::XIndexAccess >& xBarSettings, const OUString& sResourceUrl ) : CommandBarControl_BASE( xParent, xContext ), pCBarHelper( pHelper ), m_sResourceUrl( sResourceUrl ), m_xCurrentSettings( xSettings ), m_xBarSettings( xBarSettings ), m_nPosition( 0 )
{
}

void ScVbaCommandBarControl::ApplyChange()
{
    uno::Reference< container::XIndexContainer > xIndexContainer( m_xCurrentSettings, uno::UNO_QUERY_THROW );
    xIndexContainer->replaceByIndex( m_nPosition, uno::makeAny( m_aPropertyValues ) );
    pCBarHelper->ApplyTempChange( m_sResourceUrl, m_xBarSettings );
}

OUString SAL_CALL
ScVbaCommandBarControl::getCaption()
{
    // "Label" always empty
    OUString sCaption;
    getPropertyValue( m_aPropertyValues, "Label" ) >>= sCaption;
    return sCaption;
}

void SAL_CALL
ScVbaCommandBarControl::setCaption( const OUString& _caption )
{
    OUString sCaption = _caption.replace('&','~');
    setPropertyValue( m_aPropertyValues, "Label" , uno::makeAny( sCaption ) );
    ApplyChange();
}

OUString SAL_CALL
ScVbaCommandBarControl::getOnAction()
{
    OUString sCommandURL;
    getPropertyValue( m_aPropertyValues, "CommandURL" ) >>= sCommandURL;
    return sCommandURL;
}

void SAL_CALL
ScVbaCommandBarControl::setOnAction( const OUString& _onaction )
{
    // get the current model
    uno::Reference< frame::XModel > xModel( pCBarHelper->getModel() );
    MacroResolvedInfo aResolvedMacro = ooo::vba::resolveVBAMacro( getSfxObjShell( xModel ), _onaction, true );
    if ( aResolvedMacro.mbFound )
    {
        OUString aCommandURL = ooo::vba::makeMacroURL( aResolvedMacro.msResolvedMacro );
        SAL_INFO("vbahelper", "ScVbaCommandBarControl::setOnAction: " << aCommandURL);
        setPropertyValue( m_aPropertyValues, "CommandURL" , uno::makeAny( aCommandURL ) );
        ApplyChange();
    }
}

sal_Bool SAL_CALL
ScVbaCommandBarControl::getVisible()
{
    bool bVisible = true;
    uno::Any aValue = getPropertyValue( m_aPropertyValues, ITEM_DESCRIPTOR_ISVISIBLE );
    if( aValue.hasValue() )
        aValue >>= bVisible;<--- Variable 'aValue' is assigned a value that is never used.
    return bVisible;
}
void SAL_CALL
ScVbaCommandBarControl::setVisible( sal_Bool _visible )
{
    uno::Any aValue = getPropertyValue( m_aPropertyValues, ITEM_DESCRIPTOR_ISVISIBLE );
    if( aValue.hasValue() )
    {
        setPropertyValue( m_aPropertyValues, ITEM_DESCRIPTOR_ISVISIBLE , uno::makeAny( _visible ) );
        ApplyChange();
    }
}

sal_Bool SAL_CALL
ScVbaCommandBarControl::getEnabled()
{
    bool bEnabled = true;

    uno::Any aValue = getPropertyValue( m_aPropertyValues, ITEM_DESCRIPTOR_ENABLED );
    if( aValue.hasValue() )
    {
        aValue >>= bEnabled;<--- Variable 'aValue' is assigned a value that is never used.
    }
    else
    {
        // emulated with Visible
        bEnabled = getVisible();
    }
    return bEnabled;
}

void SAL_CALL
ScVbaCommandBarControl::setEnabled( sal_Bool _enabled )
{
    uno::Any aValue = getPropertyValue( m_aPropertyValues, ITEM_DESCRIPTOR_ENABLED );
    if( aValue.hasValue() )
    {
        setPropertyValue( m_aPropertyValues, ITEM_DESCRIPTOR_ENABLED , uno::makeAny( _enabled ) );
        ApplyChange();
    }
    else
    {
        // emulated with Visible
        setVisible( _enabled );
    }
}

sal_Bool SAL_CALL
ScVbaCommandBarControl::getBeginGroup()
{
    // TODO: need to check if the item before this item is of type 'separator'
    //#STUB
    return false;
}

void SAL_CALL
ScVbaCommandBarControl::setBeginGroup( sal_Bool _begin )<--- The function 'setBeginGroup' is never used.
{
    if( getBeginGroup() != _begin )
    {
        // TODO: need to insert or remove an item of type 'separator' before this item
    }
}

void SAL_CALL
ScVbaCommandBarControl::Delete(  )
{
    if( m_xCurrentSettings.is() )
    {
        uno::Reference< container::XIndexContainer > xIndexContainer( m_xCurrentSettings, uno::UNO_QUERY_THROW );
        xIndexContainer->removeByIndex( m_nPosition );

        pCBarHelper->ApplyTempChange( m_sResourceUrl, m_xBarSettings );
    }
}

uno::Any SAL_CALL
ScVbaCommandBarControl::Controls( const uno::Any& aIndex )
{
    // only Popup Menu has controls
    uno::Reference< container::XIndexAccess > xSubMenu;
    getPropertyValue( m_aPropertyValues, ITEM_DESCRIPTOR_CONTAINER ) >>= xSubMenu;
    if( !xSubMenu.is() )
        throw uno::RuntimeException();

    uno::Reference< XCommandBarControls > xCommandBarControls( new ScVbaCommandBarControls( this, mxContext, xSubMenu, pCBarHelper, m_xBarSettings, m_sResourceUrl ) );
    if( aIndex.hasValue() )
    {
        return xCommandBarControls->Item( aIndex, uno::Any() );
    }
    return uno::makeAny( xCommandBarControls );
}

OUString
ScVbaCommandBarControl::getServiceImplName()
{
    return "ScVbaCommandBarControl";
}

uno::Sequence<OUString>
ScVbaCommandBarControl::getServiceNames()
{
    static uno::Sequence< OUString > const aServiceNames
    {
        "ooo.vba.CommandBarControl"
    };
    return aServiceNames;
}

// ScVbaCommandBarPopup
ScVbaCommandBarPopup::ScVbaCommandBarPopup( const css::uno::Reference< ov::XHelperInterface >& xParent,
                                            const css::uno::Reference< css::uno::XComponentContext >& xContext,
                                            const css::uno::Reference< css::container::XIndexAccess >& xSettings,
                                            const VbaCommandBarHelperRef& pHelper,
                                            const css::uno::Reference< css::container::XIndexAccess >& xBarSettings,
                                            const OUString& sResourceUrl,
                                            sal_Int32 nPosition )
    : CommandBarPopup_BASE( xParent, xContext, xSettings, pHelper, xBarSettings, sResourceUrl )
{
    m_nPosition = nPosition;
    m_xCurrentSettings->getByIndex( m_nPosition ) >>= m_aPropertyValues;
}

OUString
ScVbaCommandBarPopup::getServiceImplName()
{
    return "ScVbaCommandBarPopup";
}

uno::Sequence<OUString>
ScVbaCommandBarPopup::getServiceNames()
{
    static uno::Sequence< OUString > const aServiceNames
    {
        "ooo.vba.CommandBarPopup"
    };
    return aServiceNames;
}

// ScVbaCommandBarButton
ScVbaCommandBarButton::ScVbaCommandBarButton( const css::uno::Reference< ov::XHelperInterface >& xParent,
                                              const css::uno::Reference< css::uno::XComponentContext >& xContext,
                                              const css::uno::Reference< css::container::XIndexAccess >& xSettings,
                                              const VbaCommandBarHelperRef& pHelper,
                                              const css::uno::Reference< css::container::XIndexAccess >& xBarSettings,
                                              const OUString& sResourceUrl,
                                              sal_Int32 nPosition )
    : CommandBarButton_BASE( xParent, xContext, xSettings, pHelper, xBarSettings, sResourceUrl )
{
    m_nPosition = nPosition;
    m_xCurrentSettings->getByIndex( m_nPosition ) >>= m_aPropertyValues;
}

OUString
ScVbaCommandBarButton::getServiceImplName()
{
    return "ScVbaCommandBarButton";
}

uno::Sequence<OUString>
ScVbaCommandBarButton::getServiceNames()
{
    static uno::Sequence< OUString > const aServiceNames
    {
        "ooo.vba.CommandBarButton"
    };
    return aServiceNames;
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */