summaryrefslogtreecommitdiffstats
path: root/framework/source/uielement/subtoolbarcontroller.cxx
blob: de345f545497bed643e5f2078783417bdd1ddb19 (plain)
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
/* -*- 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 <comphelper/propertysequence.hxx>
#include <cppuhelper/implbase.hxx>
#include <cppuhelper/supportsservice.hxx>
#include <cppuhelper/weakref.hxx>
#include <svtools/popupwindowcontroller.hxx>
#include <svtools/toolbarmenu.hxx>
#include <toolkit/helper/vclunohelper.hxx>
#include <tools/gen.hxx>
#include <vcl/svapp.hxx>
#include <vcl/toolbox.hxx>
#include <vcl/commandinfoprovider.hxx>
#include <vcl/weldutils.hxx>

#include <com/sun/star/awt/XDockableWindow.hpp>
#include <com/sun/star/frame/XLayoutManager.hpp>
#include <com/sun/star/frame/XSubToolbarController.hpp>
#include <com/sun/star/frame/status/Visibility.hpp>
#include <com/sun/star/lang/XServiceInfo.hpp>
#include <com/sun/star/ui/theUIElementFactoryManager.hpp>
#include <com/sun/star/container/NoSuchElementException.hpp>

typedef cppu::ImplInheritanceHelper< svt::PopupWindowController,
                                     css::frame::XSubToolbarController,
                                     css::awt::XDockableWindowListener> ToolBarBase;

namespace {

class SubToolBarController : public ToolBarBase
{
    OUString m_aSubTbName;
    OUString m_aLastCommand;
    css::uno::Reference< css::ui::XUIElement > m_xUIElement;
    void disposeUIElement();
public:
    explicit SubToolBarController( const rtl::Reference< com::sun::star::uno::XComponentContext >& rxContext,
                                   const css::uno::Sequence< css::uno::Any >& rxArgs );
    virtual ~SubToolBarController() override;

    void PopoverDestroyed();

    // XInitialization
    virtual void SAL_CALL initialize( const css::uno::Sequence< css::uno::Any >& rxArgs ) override;

    // XStatusListener
    virtual void SAL_CALL statusChanged( const css::frame::FeatureStateEvent& Event ) override;

    // XToolbarController
    virtual void SAL_CALL execute( sal_Int16 nKeyModifier ) override;

    // PopupWindowController
    virtual VclPtr<vcl::Window> createVclPopupWindow(vcl::Window* pParent) override;
    virtual std::unique_ptr<WeldToolbarPopup> weldPopupWindow() override;

    // XSubToolbarController
    virtual sal_Bool SAL_CALL opensSubToolbar() override;
    virtual OUString SAL_CALL getSubToolbarName() override;
    virtual void SAL_CALL functionSelected( const OUString& rCommand ) override;
    virtual void SAL_CALL updateImage() override;

    //  XDockableWindowListener
    virtual void SAL_CALL startDocking( const css::awt::DockingEvent& e ) override;
    virtual css::awt::DockingData SAL_CALL docking( const css::awt::DockingEvent& e ) override;
    virtual void SAL_CALL endDocking( const css::awt::EndDockingEvent& e ) override;
    virtual sal_Bool SAL_CALL prepareToggleFloatingMode( const css::lang::EventObject& e ) override;
    virtual void SAL_CALL toggleFloatingMode( const css::lang::EventObject& e ) override;
    virtual void SAL_CALL closed( const css::lang::EventObject& e ) override;
    virtual void SAL_CALL endPopupMode( const css::awt::EndPopupModeEvent& e ) override;

    // XEventListener
    virtual void SAL_CALL disposing( const css::lang::EventObject& e ) override;

    // XComponent
    virtual void SAL_CALL dispose() override;

    // XServiceInfo
    virtual OUString SAL_CALL getImplementationName() override;
    virtual sal_Bool SAL_CALL supportsService( const OUString& rServiceName ) override;
    virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override;
};

}

SubToolBarController::SubToolBarController(
    const rtl::Reference< com::sun::star::uno::XComponentContext >& rxContext,
    const css::uno::Sequence< css::uno::Any >& rxArgs
)   : ToolBarBase(
        rxContext,
        rtl::Reference< css::frame::XFrame >(),
        ""
    )
{
    for ( css::uno::Any const & arg : rxArgs )
    {
        css::beans::PropertyValue aPropValue;
        arg >>= aPropValue;
        if ( aPropValue.Name == "Value" )
        {
            sal_Int32 nIdx{ 0 };
            OUString aValue;
            aPropValue.Value >>= aValue;
            m_aSubTbName = aValue.getToken(0, ';', nIdx);
            m_aCommandURL = m_aSubTbName;
            m_aLastCommand = aValue.getToken(0, ';', nIdx);
            break;
        }
    }
    if ( !m_aLastCommand.isEmpty() )
        addStatusListener( m_aLastCommand );
}

SubToolBarController::~SubToolBarController()
{
    disposeUIElement();
    m_xUIElement = nullptr;
}

void SubToolBarController::disposeUIElement()
{
    if ( m_xUIElement.is() )
    {
        css::uno::Reference< css::lang::XComponent > xComponent( m_xUIElement, css::uno::UNO_QUERY );
        xComponent->dispose();
    }
}

void SubToolBarController::statusChanged( const css::frame::FeatureStateEvent& Event )
{
    SolarMutexGuard aSolarMutexGuard;

    if ( m_bDisposed )
        return;

    ToolBox* pToolBox = nullptr;
    ToolBoxItemId nId;
    if ( !getToolboxId( nId, &pToolBox ) )
        return;

    ToolBoxItemBits nItemBits = pToolBox->GetItemBits( nId );
    nItemBits &= ~ToolBoxItemBits::CHECKABLE;
    TriState eTri = TRISTATE_FALSE;

    if ( Event.FeatureURL.Complete == m_aCommandURL )
    {
        pToolBox->EnableItem( nId, Event.IsEnabled );

        OUString aStrValue;
        css::frame::status::Visibility aItemVisibility;
        if ( Event.State >>= aStrValue )
        {
            // Enum command, such as the current custom shape,
            // toggle checked state.
            if ( m_aLastCommand == OUStringConcatenation( m_aCommandURL + "." + aStrValue ) )
            {
                eTri = TRISTATE_TRUE;
                nItemBits |= ToolBoxItemBits::CHECKABLE;
            }
        }
        else if ( Event.State >>= aItemVisibility )
        {
            pToolBox->ShowItem( nId, aItemVisibility.bVisible );
        }
    }
    else
    {
        bool bValue;
        if ( Event.State >>= bValue )
        {
            // Boolean, treat it as checked/unchecked
            if ( bValue )
                eTri = TRISTATE_TRUE;
            nItemBits |= ToolBoxItemBits::CHECKABLE;
        }
    }

    pToolBox->SetItemState( nId, eTri );
    pToolBox->SetItemBits( nId, nItemBits );
}

void SubToolBarController::execute( sal_Int16 nKeyModifier )
{
    if ( !m_aLastCommand.isEmpty() )
    {
        auto aArgs( comphelper::InitPropertySequence( {
            { "KeyModifier", css::uno::Any( nKeyModifier ) }
        } ) );
        dispatchCommand( m_aLastCommand, aArgs );
    }
}

namespace {
class SubToolbarControl final : public WeldToolbarPopup
{
public:
    explicit SubToolbarControl(SubToolBarController& rController, weld::Widget* pParent);
    virtual ~SubToolbarControl() override;

    virtual void GrabFocus() override;

    weld::Container* GetContainer() { return m_xTargetContainer.get(); }

private:
    SubToolBarController& m_rController;
    std::unique_ptr<weld::Container> m_xTargetContainer;
};
}

SubToolbarControl::SubToolbarControl(SubToolBarController& rController,
                                     weld::Widget* pParent)
    : WeldToolbarPopup(rController.getFrameInterface(), pParent, "svt/ui/subtoolbar.ui", "subtoolbar")
    , m_rController(rController)
    , m_xTargetContainer(m_xBuilder->weld_container("container"))
{
}

void SubToolbarControl::GrabFocus()
{
    // TODO
}

SubToolbarControl::~SubToolbarControl()
{
    m_rController.PopoverDestroyed();
}

std::unique_ptr<WeldToolbarPopup> SubToolBarController::weldPopupWindow()
{
    SolarMutexGuard aGuard;

    auto pPopup = std::make_unique<SubToolbarControl>(*this, m_pToolbar);

    css::uno::Reference< css::frame::XFrame > xFrame ( getFrameInterface() );

    // create element with factory
    static css::uno::WeakReference< css::ui::XUIElementFactoryManager > xWeakUIElementFactory;
    css::uno::Reference< css::ui::XUIElementFactoryManager > xUIElementFactory = xWeakUIElementFactory;
    if ( !xUIElementFactory.is() )
    {
        xUIElementFactory = css::ui::theUIElementFactoryManager::get( m_xContext );
        xWeakUIElementFactory = xUIElementFactory;
    }

    css::uno::Reference< css::awt::XWindow > xParent = new weld::TransportAsXWindow(pPopup->GetContainer());

    auto aPropSeq( comphelper::InitPropertySequence( {
        { "Frame", css::uno::Any( xFrame ) },
        { "ParentWindow", css::uno::Any( xParent ) },
        { "Persistent", css::uno::Any( false ) },
        { "PopupMode", css::uno::Any( true ) }
    } ) );

    try
    {
        m_xUIElement = xUIElementFactory->createUIElement( "private:resource/toolbar/" + m_aSubTbName, aPropSeq );
    }
    catch ( css::container::NoSuchElementException& )
    {}
    catch ( css::lang::IllegalArgumentException& )
    {}

    return pPopup;
}

VclPtr<vcl::Window> SubToolBarController::createVclPopupWindow(vcl::Window* /*pParent*/)
{
    SolarMutexGuard aGuard;

    ToolBox* pToolBox = nullptr;
    ToolBoxItemId nId;
    if ( getToolboxId( nId, &pToolBox ) )
    {
        css::uno::Reference< css::frame::XFrame > xFrame ( getFrameInterface() );

        // create element with factory
        static css::uno::WeakReference< css::ui::XUIElementFactoryManager > xWeakUIElementFactory;
        css::uno::Reference< css::ui::XUIElement > xUIElement;
        css::uno::Reference< css::ui::XUIElementFactoryManager > xUIElementFactory = xWeakUIElementFactory;
        if ( !xUIElementFactory.is() )
        {
            xUIElementFactory = css::ui::theUIElementFactoryManager::get( m_xContext );
            xWeakUIElementFactory = xUIElementFactory;
        }

        auto aPropSeq( comphelper::InitPropertySequence( {
            { "Frame", css::uno::Any( xFrame ) },
            { "ParentWindow", css::uno::Any( m_xParentWindow ) },
            { "Persistent", css::uno::Any( false ) },
            { "PopupMode", css::uno::Any( true ) }
        } ) );

        try
        {
            xUIElement = xUIElementFactory->createUIElement( "private:resource/toolbar/" + m_aSubTbName, aPropSeq );
        }
        catch ( css::container::NoSuchElementException& )
        {}
        catch ( css::lang::IllegalArgumentException& )
        {}

        if ( xUIElement.is() )
        {
            css::uno::Reference< css::awt::XWindow > xSubToolBar( xUIElement->getRealInterface(), css::uno::UNO_QUERY );
            if ( xSubToolBar.is() )
            {
                css::uno::Reference< css::awt::XDockableWindow > xDockWindow( xSubToolBar, css::uno::UNO_QUERY );
                xDockWindow->addDockableWindowListener( css::uno::Reference< css::awt::XDockableWindowListener >(this) );
                xDockWindow->enableDocking( true );

                // keep reference to UIElement to avoid its destruction
                disposeUIElement();
                m_xUIElement = xUIElement;

                VclPtr<vcl::Window> pTbxWindow = VCLUnoHelper::GetWindow( xSubToolBar );
                if ( pTbxWindow && pTbxWindow->GetType() == WindowType::TOOLBOX )
                {
                    ToolBox* pToolBar = static_cast< ToolBox* >( pTbxWindow.get() );
                    // calc and set size for popup mode
                    Size aSize = pToolBar->CalcPopupWindowSizePixel();
                    pToolBar->SetSizePixel( aSize );
                    // open subtoolbox in popup mode
                    vcl::Window::GetDockingManager()->StartPopupMode( pToolBox, pToolBar );
                }
            }
        }
    }
    return nullptr;
}

sal_Bool SubToolBarController::opensSubToolbar()
{
    return !m_aLastCommand.isEmpty();
}

OUString SubToolBarController::getSubToolbarName()
{
    return m_aSubTbName;
}

void SubToolBarController::functionSelected( const OUString& rCommand )
{
    if ( !m_aLastCommand.isEmpty() && m_aLastCommand != rCommand )
    {
        removeStatusListener( m_aLastCommand );
        m_aLastCommand = rCommand;
        addStatusListener( m_aLastCommand );
        updateImage();
    }
}

void SubToolBarController::updateImage()
{
    SolarMutexGuard aGuard;
    if ( !m_aLastCommand.isEmpty() )
    {
        ToolBox* pToolBox = nullptr;
        ToolBoxItemId nId;
        if ( getToolboxId( nId, &pToolBox ) )
        {
            vcl::ImageType eImageType = pToolBox->GetImageSize();
            Image aImage = vcl::CommandInfoProvider::GetImageForCommand(m_aLastCommand, getFrameInterface(), eImageType);
            if ( !!aImage )
                pToolBox->SetItemImage( nId, aImage );
        }
    }
}

void SubToolBarController::startDocking( const css::awt::DockingEvent& )
{
}

css::awt::DockingData SubToolBarController::docking( const css::awt::DockingEvent& )
{
    return css::awt::DockingData();
}

void SubToolBarController::endDocking( const css::awt::EndDockingEvent& )
{
}

sal_Bool SubToolBarController::prepareToggleFloatingMode( const css::lang::EventObject& )
{
    return false;
}

void SubToolBarController::toggleFloatingMode( const css::lang::EventObject& )
{
}

void SubToolBarController::closed( const css::lang::EventObject& )
{
}

void SubToolBarController::endPopupMode( const css::awt::EndPopupModeEvent& e )
{
    SolarMutexGuard aGuard;

    OUString aSubToolBarResName;
    if ( m_xUIElement.is() )
    {
        css::uno::Reference< css::beans::XPropertySet > xPropSet( m_xUIElement, css::uno::UNO_QUERY );
        if ( xPropSet.is() )
        {
            try
            {
                xPropSet->getPropertyValue("ResourceURL") >>= aSubToolBarResName;
            }
            catch ( css::beans::UnknownPropertyException& )
            {}
            catch ( css::lang::WrappedTargetException& )
            {}
        }
        disposeUIElement();
    }
    m_xUIElement = nullptr;

    // if the toolbar was teared-off recreate it and place it at the given position
    if( !e.bTearoff )
        return;

    css::uno::Reference< css::ui::XUIElement > xUIElement;
    css::uno::Reference< css::frame::XLayoutManager > xLayoutManager = getLayoutManager();

    if ( !xLayoutManager.is() )
        return;

    xLayoutManager->createElement( aSubToolBarResName );
    xUIElement = xLayoutManager->getElement( aSubToolBarResName );
    if ( !xUIElement.is() )
        return;

    css::uno::Reference< css::awt::XWindow > xSubToolBar( xUIElement->getRealInterface(), css::uno::UNO_QUERY );
    css::uno::Reference< css::beans::XPropertySet > xProp( xUIElement, css::uno::UNO_QUERY );
    if ( !(xSubToolBar.is() && xProp.is()) )
        return;

    try
    {
        VclPtr<vcl::Window> pTbxWindow = VCLUnoHelper::GetWindow( xSubToolBar );
        if ( pTbxWindow && pTbxWindow->GetType() == WindowType::TOOLBOX )
        {
            OUString aPersistentString( "Persistent" );
            css::uno::Any a = xProp->getPropertyValue( aPersistentString );
            xProp->setPropertyValue( aPersistentString, css::uno::Any( false ) );

            xLayoutManager->hideElement( aSubToolBarResName );
            xLayoutManager->floatWindow( aSubToolBarResName );

            xLayoutManager->setElementPos( aSubToolBarResName, e.FloatingPosition );
            xLayoutManager->showElement( aSubToolBarResName );

            xProp->setPropertyValue("Persistent", a );
        }
    }
    catch ( css::uno::RuntimeException& )
    {
        throw;
    }
    catch ( css::uno::Exception& )
    {}
}

void SubToolBarController::disposing( const css::lang::EventObject& e )
{
    svt::ToolboxController::disposing( e );
}

void SubToolBarController::initialize( const css::uno::Sequence< css::uno::Any >& rxArgs )
{
    svt::PopupWindowController::initialize( rxArgs );

    ToolBox* pToolBox = nullptr;
    ToolBoxItemId nId;
    if ( getToolboxId( nId, &pToolBox ) )
    {
        if ( m_aLastCommand.isEmpty() )
            pToolBox->SetItemBits( nId, pToolBox->GetItemBits( nId ) | ToolBoxItemBits::DROPDOWNONLY );
        else
            pToolBox->SetItemBits( nId, pToolBox->GetItemBits( nId ) | ToolBoxItemBits::DROPDOWN );
    }

    if (m_pToolbar)
    {
        mxPopoverContainer.reset(new ToolbarPopupContainer(m_pToolbar));
        m_pToolbar->set_item_popover(m_aCommandURL.toUtf8(), mxPopoverContainer->getTopLevel());
    }

    updateImage();
}

void SubToolBarController::PopoverDestroyed()
{
    disposeUIElement();
    m_xUIElement = nullptr;
}

void SubToolBarController::dispose()
{
    if ( m_bDisposed )
        return;

    svt::PopupWindowController::dispose();
    disposeUIElement();
    m_xUIElement = nullptr;
}

OUString SubToolBarController::getImplementationName()
{
    return "com.sun.star.comp.framework.SubToolBarController";
}

sal_Bool SubToolBarController::supportsService( const OUString& rServiceName )
{
    return cppu::supportsService( this, rServiceName );
}

css::uno::Sequence< OUString > SubToolBarController::getSupportedServiceNames()
{
    return {"com.sun.star.frame.ToolbarController"};
}

extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
com_sun_star_comp_framework_SubToolBarController_get_implementation(
    css::uno::XComponentContext* rxContext,
    css::uno::Sequence<css::uno::Any> const & rxArgs )
{
    return cppu::acquire( new SubToolBarController( rxContext, rxArgs ) );
}

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