Coverage Report

Created: 2026-05-16 09:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/framework/source/uielement/complextoolbarcontroller.cxx
Line
Count
Source
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 <uielement/complextoolbarcontroller.hxx>
21
22
#include <com/sun/star/util/URLTransformer.hpp>
23
#include <com/sun/star/util/XURLTransformer.hpp>
24
#include <com/sun/star/beans/PropertyValue.hpp>
25
#include <com/sun/star/lang/DisposedException.hpp>
26
#include <com/sun/star/frame/status/ItemStatus.hpp>
27
#include <com/sun/star/frame/status/Visibility.hpp>
28
#include <com/sun/star/frame/XControlNotificationListener.hpp>
29
#include <com/sun/star/frame/XFrame.hpp>
30
31
#include <comphelper/propertyvalue.hxx>
32
#include <svtools/toolboxcontroller.hxx>
33
#include <vcl/svapp.hxx>
34
#include <vcl/mnemonic.hxx>
35
#include <vcl/toolbox.hxx>
36
37
using namespace ::com::sun::star;
38
using namespace css::uno;
39
using namespace css::beans;
40
using namespace css::lang;
41
using namespace css::frame;
42
using namespace css::frame::status;
43
using namespace css::util;
44
45
namespace framework
46
{
47
48
ComplexToolbarController::ComplexToolbarController(
49
    const Reference< XComponentContext >& rxContext,
50
    const Reference< XFrame >&            rFrame,
51
    ToolBox*                              pToolbar,
52
    ToolBoxItemId                         nID,
53
    const OUString&                       aCommand ) :
54
0
    svt::ToolboxController( rxContext, rFrame, aCommand )
55
0
    ,   m_xToolbar( pToolbar )
56
0
    ,   m_nID( nID )
57
0
    ,   m_bMadeInvisible( false )
58
0
{
59
0
    m_xURLTransformer.set( URLTransformer::create(m_xContext) );
60
0
}
61
62
ComplexToolbarController::~ComplexToolbarController()
63
0
{
64
0
}
65
66
void SAL_CALL ComplexToolbarController::dispose()
67
0
{
68
0
    SolarMutexGuard aSolarMutexGuard;
69
70
0
    m_xToolbar->SetItemWindow( m_nID, nullptr );
71
0
    svt::ToolboxController::dispose();
72
73
0
    m_xURLTransformer.clear();
74
0
    m_xToolbar.reset();
75
0
    m_nID = ToolBoxItemId(0);
76
0
}
77
78
Sequence<PropertyValue> ComplexToolbarController::getExecuteArgs(sal_Int16 KeyModifier) const
79
0
{
80
    // Add key modifier to argument list
81
0
    Sequence<PropertyValue> aArgs{ comphelper::makePropertyValue(u"KeyModifier"_ustr, KeyModifier) };
82
0
    return aArgs;
83
0
}
84
85
void SAL_CALL ComplexToolbarController::execute( sal_Int16 KeyModifier )
86
0
{
87
0
    Reference< XDispatch >       xDispatch;
88
0
    Reference< XURLTransformer > xURLTransformer;
89
0
    css::util::URL  aTargetURL;
90
0
    Sequence<PropertyValue> aArgs;
91
92
0
    {
93
0
        SolarMutexGuard aSolarMutexGuard;
94
95
0
        if ( m_bDisposed )
96
0
            throw DisposedException();
97
98
0
        if ( m_bInitialized &&
99
0
             m_xFrame.is() &&
100
0
             !m_aCommandURL.isEmpty() )
101
0
        {
102
0
            xURLTransformer = m_xURLTransformer;
103
0
            xDispatch = getDispatchFromCommand( m_aCommandURL );
104
0
            aTargetURL = getInitializedURL();
105
0
            aArgs = getExecuteArgs(KeyModifier);
106
0
        }
107
0
    }
108
109
0
    if ( xDispatch.is() && !aTargetURL.Complete.isEmpty() )
110
0
    {
111
        // Execute dispatch asynchronously
112
0
        ExecuteInfo* pExecuteInfo = new ExecuteInfo;
113
0
        pExecuteInfo->xDispatch     = std::move(xDispatch);
114
0
        pExecuteInfo->aTargetURL    = std::move(aTargetURL);
115
0
        pExecuteInfo->aArgs         = std::move(aArgs);
116
0
        Application::PostUserEvent( LINK(nullptr, ComplexToolbarController , ExecuteHdl_Impl), pExecuteInfo );
117
0
    }
118
0
}
119
120
void ComplexToolbarController::statusChanged( const FeatureStateEvent& Event )
121
0
{
122
0
    SolarMutexGuard aSolarMutexGuard;
123
124
0
    if ( m_bDisposed )
125
0
        return;
126
127
0
    if ( !m_xToolbar )
128
0
        return;
129
130
0
    m_xToolbar->EnableItem( m_nID, Event.IsEnabled );
131
132
0
    ToolBoxItemBits nItemBits = m_xToolbar->GetItemBits( m_nID );
133
0
    nItemBits &= ~ToolBoxItemBits::CHECKABLE;
134
0
    TriState eTri = TRISTATE_FALSE;
135
136
0
    bool            bValue;
137
0
    OUString        aStrValue;
138
0
    ItemStatus      aItemState;
139
0
    Visibility      aItemVisibility;
140
0
    ControlCommand  aControlCommand;
141
142
0
    if ( Event.State >>= bValue )
143
0
    {
144
        // Boolean, treat it as checked/unchecked
145
0
        if ( m_bMadeInvisible )
146
0
            m_xToolbar->ShowItem( m_nID );
147
0
        m_xToolbar->CheckItem( m_nID, bValue );
148
0
        if ( bValue )
149
0
            eTri = TRISTATE_TRUE;
150
0
        nItemBits |= ToolBoxItemBits::CHECKABLE;
151
0
    }
152
0
    else if ( Event.State >>= aStrValue )
153
0
    {
154
0
        OUString aText( MnemonicGenerator::EraseAllMnemonicChars( aStrValue ) );
155
0
        m_xToolbar->SetItemText( m_nID, aText );
156
0
        m_xToolbar->SetQuickHelpText( m_nID, aText );
157
158
0
        if ( m_bMadeInvisible )
159
0
            m_xToolbar->ShowItem( m_nID );
160
0
    }
161
0
    else if ( Event.State >>= aItemState )
162
0
    {
163
0
        eTri = TRISTATE_INDET;
164
0
        nItemBits |= ToolBoxItemBits::CHECKABLE;
165
0
        if ( m_bMadeInvisible )
166
0
            m_xToolbar->ShowItem( m_nID );
167
0
    }
168
0
    else if ( Event.State >>= aItemVisibility )
169
0
    {
170
0
        m_xToolbar->ShowItem( m_nID, aItemVisibility.bVisible );
171
0
        m_bMadeInvisible = !aItemVisibility.bVisible;
172
0
    }
173
0
    else if ( Event.State >>= aControlCommand )
174
0
    {
175
0
        if (aControlCommand.Command == "SetQuickHelpText")
176
0
        {
177
0
            for (NamedValue const& rArg : aControlCommand.Arguments)
178
0
            {
179
0
                if (rArg.Name == "HelpText")
180
0
                {
181
0
                    OUString aHelpText;
182
0
                    rArg.Value >>= aHelpText;
183
0
                    m_xToolbar->SetQuickHelpText(m_nID, aHelpText);
184
0
                    break;
185
0
                }
186
0
            }
187
0
        }
188
0
        else
189
0
        {
190
0
            executeControlCommand( aControlCommand );
191
0
        }
192
0
        if ( m_bMadeInvisible )
193
0
            m_xToolbar->ShowItem( m_nID );
194
0
    }
195
196
0
    else if ( m_bMadeInvisible )
197
0
        m_xToolbar->ShowItem( m_nID );
198
199
0
    m_xToolbar->SetItemState( m_nID, eTri );
200
0
    m_xToolbar->SetItemBits( m_nID, nItemBits );
201
0
}
202
203
IMPL_STATIC_LINK( ComplexToolbarController, ExecuteHdl_Impl, void*, p, void )
204
0
{
205
0
   ExecuteInfo* pExecuteInfo = static_cast<ExecuteInfo*>(p);
206
0
   SolarMutexReleaser aReleaser;
207
0
   try
208
0
   {
209
       // Asynchronous execution as this can lead to our own destruction!
210
       // Framework can recycle our current frame and the layout manager disposes all user interface
211
       // elements if a component gets detached from its frame!
212
0
       pExecuteInfo->xDispatch->dispatch( pExecuteInfo->aTargetURL, pExecuteInfo->aArgs );
213
0
   }
214
0
   catch ( const Exception& )
215
0
   {
216
0
   }
217
218
0
   delete pExecuteInfo;
219
0
}
220
221
IMPL_STATIC_LINK( ComplexToolbarController, Notify_Impl, void*, p, void )
222
0
{
223
0
   NotifyInfo* pNotifyInfo = static_cast<NotifyInfo*>(p);
224
0
   SolarMutexReleaser aReleaser;
225
0
   try
226
0
   {
227
       // Asynchronous execution: As this can lead to our own destruction!
228
       // Framework can recycle our current frame and the layout manager disposes all user interface
229
       // elements if a component gets detached from its frame!
230
0
       frame::ControlEvent aEvent;
231
0
       aEvent.aURL  = pNotifyInfo->aSourceURL;
232
0
       aEvent.Event = pNotifyInfo->aEventName;
233
0
       aEvent.aInformation = pNotifyInfo->aInfoSeq;
234
0
       pNotifyInfo->xNotifyListener->controlEvent( aEvent );
235
0
   }
236
0
   catch ( const Exception& )
237
0
   {
238
0
   }
239
240
0
   delete pNotifyInfo;
241
0
}
242
243
void ComplexToolbarController::addNotifyInfo(
244
    const OUString&                      aEventName,
245
    const uno::Reference< frame::XDispatch >& xDispatch,
246
    const uno::Sequence< beans::NamedValue >& rInfo )
247
0
{
248
0
    uno::Reference< frame::XControlNotificationListener > xControlNotify( xDispatch, uno::UNO_QUERY );
249
250
0
    if ( !xControlNotify.is() )
251
0
        return;
252
253
    // Execute notification asynchronously
254
0
    NotifyInfo* pNotifyInfo = new NotifyInfo;
255
256
0
    pNotifyInfo->aEventName      = aEventName;
257
0
    pNotifyInfo->xNotifyListener = std::move(xControlNotify);
258
0
    pNotifyInfo->aSourceURL      = getInitializedURL();
259
260
    // Add frame as source to the information sequence
261
0
    sal_Int32 nCount = rInfo.getLength();
262
0
    uno::Sequence< beans::NamedValue > aInfoSeq( rInfo );
263
0
    aInfoSeq.realloc( nCount+1 );
264
0
    auto pInfoSeq = aInfoSeq.getArray();
265
0
    pInfoSeq[nCount].Name  = "Source";
266
0
    pInfoSeq[nCount].Value <<= getFrameInterface();
267
0
    pNotifyInfo->aInfoSeq  = std::move(aInfoSeq);
268
269
0
    Application::PostUserEvent( LINK(nullptr, ComplexToolbarController, Notify_Impl), pNotifyInfo );
270
0
}
271
272
uno::Reference< frame::XDispatch > ComplexToolbarController::getDispatchFromCommand( const OUString& aCommand ) const
273
0
{
274
0
    uno::Reference< frame::XDispatch > xDispatch;
275
276
0
    if ( m_bInitialized && m_xFrame.is() && !aCommand.isEmpty() )
277
0
    {
278
0
        URLToDispatchMap::const_iterator pIter = m_aListenerMap.find( aCommand );
279
0
        if ( pIter != m_aListenerMap.end() )
280
0
            xDispatch = pIter->second;
281
0
    }
282
283
0
    return xDispatch;
284
0
}
285
286
const css::util::URL& ComplexToolbarController::getInitializedURL()
287
0
{
288
0
    if ( m_aURL.Complete.isEmpty() )
289
0
    {
290
0
        m_aURL.Complete = m_aCommandURL;
291
0
        m_xURLTransformer->parseStrict( m_aURL );
292
0
    }
293
0
    return m_aURL;
294
0
}
295
296
void ComplexToolbarController::notifyFocusGet()
297
0
{
298
    // send focus get notification
299
0
    uno::Sequence< beans::NamedValue > aInfo;
300
0
    addNotifyInfo( u"FocusSet"_ustr,
301
0
                    getDispatchFromCommand( m_aCommandURL ),
302
0
                    aInfo );
303
0
}
304
305
void ComplexToolbarController::notifyFocusLost()
306
0
{
307
    // send focus lost notification
308
0
    uno::Sequence< beans::NamedValue > aInfo;
309
0
    addNotifyInfo( u"FocusLost"_ustr,
310
0
                    getDispatchFromCommand( m_aCommandURL ),
311
0
                    aInfo );
312
0
}
313
314
void ComplexToolbarController::notifyTextChanged( const OUString& aText )
315
0
{
316
    // send text changed notification
317
0
    uno::Sequence< beans::NamedValue > aInfo { { u"Text"_ustr, css::uno::Any(aText) } };
318
0
    addNotifyInfo( u"TextChanged"_ustr,
319
0
                   getDispatchFromCommand( m_aCommandURL ),
320
0
                   aInfo );
321
0
}
322
323
} // namespace
324
325
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */