Coverage Report

Created: 2026-10-14 09:33

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/framework/source/uielement/togglebuttontoolbarcontroller.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/togglebuttontoolbarcontroller.hxx>
21
22
#include <comphelper/propertyvalue.hxx>
23
#include <vcl/svapp.hxx>
24
#include <vcl/toolbox.hxx>
25
#include <vcl/menu.hxx>
26
27
using namespace ::com::sun::star;
28
using namespace ::com::sun::star::awt;
29
using namespace ::com::sun::star::uno;
30
using namespace ::com::sun::star::beans;
31
using namespace ::com::sun::star::frame;
32
33
namespace framework
34
{
35
36
ToggleButtonToolbarController::ToggleButtonToolbarController(
37
    const Reference< XComponentContext >&    rxContext,
38
    const Reference< XFrame >&               rFrame,
39
    ToolBox*                                 pToolbar,
40
    ToolBoxItemId                            nID,
41
    Style                                    eStyle,
42
    const OUString&                          aCommand ) :
43
0
    ComplexToolbarController( rxContext, rFrame, pToolbar, nID, aCommand )
44
0
{
45
0
    if ( eStyle == Style::DropDownButton )
46
0
        m_xToolbar->SetItemBits( m_nID, ToolBoxItemBits::DROPDOWNONLY | m_xToolbar->GetItemBits( m_nID ) );
47
0
    else // Style::ToggleDropDownButton
48
0
        m_xToolbar->SetItemBits( m_nID, ToolBoxItemBits::DROPDOWN | m_xToolbar->GetItemBits( m_nID ) );
49
0
}
Unexecuted instantiation: framework::ToggleButtonToolbarController::ToggleButtonToolbarController(com::sun::star::uno::Reference<com::sun::star::uno::XComponentContext> const&, com::sun::star::uno::Reference<com::sun::star::frame::XFrame> const&, ToolBox*, o3tl::strong_int<unsigned short, ToolBoxItemIdTag>, framework::ToggleButtonToolbarController::Style, rtl::OUString const&)
Unexecuted instantiation: framework::ToggleButtonToolbarController::ToggleButtonToolbarController(com::sun::star::uno::Reference<com::sun::star::uno::XComponentContext> const&, com::sun::star::uno::Reference<com::sun::star::frame::XFrame> const&, ToolBox*, o3tl::strong_int<unsigned short, ToolBoxItemIdTag>, framework::ToggleButtonToolbarController::Style, rtl::OUString const&)
50
51
ToggleButtonToolbarController::~ToggleButtonToolbarController()
52
0
{
53
0
}
54
55
Sequence<PropertyValue> ToggleButtonToolbarController::getExecuteArgs(sal_Int16 KeyModifier) const
56
0
{
57
0
    Sequence<PropertyValue> aArgs{ // Add key modifier to argument list
58
0
                                   comphelper::makePropertyValue(u"KeyModifier"_ustr, KeyModifier),
59
0
                                   comphelper::makePropertyValue(u"Text"_ustr, m_aCurrentSelection) };
60
0
    return aArgs;
61
0
}
62
63
uno::Reference< awt::XWindow > SAL_CALL ToggleButtonToolbarController::createPopupWindow()
64
0
{
65
0
    uno::Reference< awt::XWindow > xWindow;
66
67
0
    SolarMutexGuard aSolarMutexGuard;
68
69
    // create popup menu
70
0
    ScopedVclPtrInstance<::PopupMenu> aPopup;
71
0
    const sal_uInt32 nCount = m_aDropdownMenuList.size();
72
0
    for ( sal_uInt32 i = 0; i < nCount; i++ )
73
0
    {
74
0
        const OUString & rLabel = m_aDropdownMenuList[i].mLabel;
75
0
        aPopup->InsertItem( sal_uInt16( i+1 ), rLabel );
76
0
        if ( rLabel == m_aCurrentSelection )
77
0
            aPopup->CheckItem( sal_uInt16( i+1 ) );
78
0
        else
79
0
            aPopup->CheckItem( sal_uInt16( i+1 ), false );
80
81
0
        if ( !m_aDropdownMenuList[i].mTipHelpText.isEmpty() )
82
0
            aPopup->SetTipHelpText( sal_uInt16( i+1 ), m_aDropdownMenuList[i].mTipHelpText );
83
0
    }
84
85
0
    m_xToolbar->SetItemDown( m_nID, true );
86
0
    aPopup->SetSelectHdl( LINK( this, ToggleButtonToolbarController, MenuSelectHdl ));
87
0
    aPopup->Execute( m_xToolbar, m_xToolbar->GetItemRect( m_nID ));
88
0
    m_xToolbar->SetItemDown( m_nID, false );
89
90
0
    return xWindow;
91
0
}
92
93
void ToggleButtonToolbarController::executeControlCommand( const css::frame::ControlCommand& rControlCommand )
94
0
{
95
0
    SolarMutexGuard aSolarMutexGuard;
96
97
0
    if ( rControlCommand.Command == "SetList" )
98
0
    {
99
0
        for ( auto const & arg : rControlCommand.Arguments )
100
0
        {
101
0
            if ( arg.Name == "List" )
102
0
            {
103
0
                Sequence< OUString > aList;
104
0
                m_aDropdownMenuList.clear();
105
0
                m_aCurrentSelection.clear();
106
107
0
                arg.Value >>= aList;
108
0
                for (OUString const& label : aList)
109
0
                {
110
0
                    m_aDropdownMenuList.push_back( DropdownMenuItem() );
111
0
                    m_aDropdownMenuList.back().mLabel = label;
112
0
                }
113
114
                // send notification
115
0
                uno::Sequence< beans::NamedValue > aInfo { { u"List"_ustr, css::uno::Any(aList) } };
116
0
                addNotifyInfo( u"ListChanged"_ustr,
117
0
                            getDispatchFromCommand( m_aCommandURL ),
118
0
                            aInfo );
119
120
0
                break;
121
0
            }
122
0
        }
123
0
    }
124
0
    else if ( rControlCommand.Command == "CheckItemPos" )
125
0
    {
126
0
        for ( auto const & arg : rControlCommand.Arguments )
127
0
        {
128
0
            if ( arg.Name == "Pos" )
129
0
            {
130
0
                sal_Int32 nPos( -1 );
131
132
0
                arg.Value >>= nPos;
133
0
                if ( nPos >= 0 &&
134
0
                     ( sal::static_int_cast< sal_uInt32 >(nPos)
135
0
                       < m_aDropdownMenuList.size() ) )
136
0
                {
137
0
                    m_aCurrentSelection = m_aDropdownMenuList[nPos].mLabel;
138
139
                    // send notification
140
0
                    uno::Sequence< beans::NamedValue > aInfo { { u"ItemChecked"_ustr, css::uno::Any(nPos) } };
141
0
                    addNotifyInfo( u"Pos"_ustr,
142
0
                                getDispatchFromCommand( m_aCommandURL ),
143
0
                                aInfo );
144
0
                }
145
0
                break;
146
0
            }
147
0
        }
148
0
    }
149
0
    else if ( rControlCommand.Command == "AddEntry" )
150
0
    {
151
0
        OUString   aText;
152
0
        OUString   aTipHelpText;
153
154
0
        for ( auto const & arg : rControlCommand.Arguments )
155
0
        {
156
0
            if ( arg.Name == "Text" )
157
0
            {
158
0
                arg.Value >>= aText;
159
0
            }
160
0
            else if ( arg.Name == "TipHelpText" )
161
0
            {
162
0
                arg.Value >>= aTipHelpText;
163
0
            }
164
0
        }
165
166
0
        if (!aText.isEmpty())
167
0
        {
168
0
            m_aDropdownMenuList.push_back( DropdownMenuItem() );
169
0
            m_aDropdownMenuList.back().mLabel = aText;
170
0
            m_aDropdownMenuList.back().mTipHelpText = aTipHelpText;
171
0
        }
172
0
    }
173
0
    else if ( rControlCommand.Command == "InsertEntry" )
174
0
    {
175
0
        sal_Int32 nPos(0);
176
0
        sal_Int32 nSize = sal_Int32( m_aDropdownMenuList.size() );
177
0
        OUString  aText;
178
0
        for ( auto const & arg : rControlCommand.Arguments )
179
0
        {
180
0
            if ( arg.Name == "Pos" )
181
0
            {
182
0
                sal_Int32 nTmpPos = 0;
183
0
                if ( arg.Value >>= nTmpPos )
184
0
                {
185
0
                    if (( nTmpPos >= 0 ) && ( nTmpPos < nSize ))
186
0
                        nPos = nTmpPos;
187
0
                }
188
0
            }
189
0
            else if ( arg.Name == "Text" )
190
0
                arg.Value >>= aText;
191
0
        }
192
193
0
        std::vector< DropdownMenuItem >::iterator aIter = m_aDropdownMenuList.begin();
194
0
        aIter += nPos;
195
0
        aIter = m_aDropdownMenuList.insert(aIter, DropdownMenuItem());
196
0
        if (aIter != m_aDropdownMenuList.end())
197
0
            aIter->mLabel = aText;
198
0
    }
199
0
    else if ( rControlCommand.Command == "RemoveEntryPos" )
200
0
    {
201
0
        for ( auto const & arg : rControlCommand.Arguments )
202
0
        {
203
0
            if ( arg.Name == "Pos" )
204
0
            {
205
0
                sal_Int32 nPos( -1 );
206
0
                if ( arg.Value >>= nPos )
207
0
                {
208
0
                    if ( nPos < sal_Int32( m_aDropdownMenuList.size() ))
209
0
                    {
210
0
                        m_aDropdownMenuList.erase(m_aDropdownMenuList.begin() + nPos);
211
0
                    }
212
0
                }
213
0
                break;
214
0
            }
215
0
        }
216
0
    }
217
0
    else if ( rControlCommand.Command == "RemoveEntryText" )
218
0
    {
219
0
        for ( auto const & arg : rControlCommand.Arguments )
220
0
        {
221
0
            if ( arg.Name == "Text" )
222
0
            {
223
0
                OUString aText;
224
0
                if ( arg.Value >>= aText )
225
0
                {
226
0
                    sal_Int32 nSize = sal_Int32( m_aDropdownMenuList.size() );
227
0
                    for ( sal_Int32 j = 0; j < nSize; j++ )
228
0
                    {
229
0
                        if ( m_aDropdownMenuList[j].mLabel == aText )
230
0
                        {
231
0
                            m_aDropdownMenuList.erase(m_aDropdownMenuList.begin() + j);
232
0
                            break;
233
0
                        }
234
0
                    }
235
0
                }
236
0
                break;
237
0
            }
238
0
        }
239
0
    }
240
0
    else if ( rControlCommand.Command == "createPopupMenu" )
241
0
    {
242
0
        createPopupWindow();
243
0
    }
244
0
}
245
246
IMPL_LINK( ToggleButtonToolbarController, MenuSelectHdl, Menu *, pMenu, bool )
247
0
{
248
0
    SolarMutexGuard aGuard;
249
250
0
    sal_uInt16 nItemId = pMenu->GetCurItemId();
251
0
    if ( nItemId > 0 && nItemId <= m_aDropdownMenuList.size() )
252
0
    {
253
0
        m_aCurrentSelection = m_aDropdownMenuList[nItemId-1].mLabel;
254
255
0
        execute( 0 );
256
0
    }
257
0
    return false;
258
0
}
259
260
} // namespace
261
262
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */