Coverage Report

Created: 2025-11-16 09:57

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/framework/source/uielement/dropdownboxtoolbarcontroller.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/dropdownboxtoolbarcontroller.hxx>
21
22
#include <com/sun/star/beans/PropertyValue.hpp>
23
24
#include <comphelper/propertyvalue.hxx>
25
#include <vcl/InterimItemWindow.hxx>
26
#include <svtools/toolboxcontroller.hxx>
27
#include <vcl/svapp.hxx>
28
#include <vcl/toolbox.hxx>
29
30
using namespace ::com::sun::star;
31
using namespace css::uno;
32
using namespace css::beans;
33
using namespace css::frame;
34
35
namespace framework
36
{
37
38
// Wrapper class to notify controller about events from ListBox.
39
// Unfortunaltly the events are notified through virtual methods instead
40
// of Listeners.
41
42
class ListBoxControl final : public InterimItemWindow
43
{
44
public:
45
    ListBoxControl(vcl::Window* pParent, DropdownToolbarController* pListBoxListener);
46
    virtual ~ListBoxControl() override;
47
    virtual void dispose() override;
48
49
0
    void set_active(int nPos) { m_xWidget->set_active(nPos); }
50
0
    void append_text(const OUString& rStr) { m_xWidget->append_text(rStr); }
51
0
    void insert_text(int nPos, const OUString& rStr) { m_xWidget->insert_text(nPos, rStr); }
52
0
    int get_count() const { return m_xWidget->get_count(); }
53
0
    int find_text(const OUString& rStr) const { return m_xWidget->find_text(rStr); }
54
0
    OUString get_active_text() const { return m_xWidget->get_active_text(); }
55
0
    void clear() { return m_xWidget->clear(); }
56
0
    void remove(int nPos) { m_xWidget->remove(nPos); }
57
58
    DECL_LINK(FocusInHdl, weld::Widget&, void);
59
    DECL_LINK(FocusOutHdl, weld::Widget&, void);
60
    DECL_LINK(ModifyHdl, weld::ComboBox&, void);
61
    DECL_LINK(KeyInputHdl, const ::KeyEvent&, bool);
62
63
private:
64
    std::unique_ptr<weld::ComboBox> m_xWidget;
65
    DropdownToolbarController* m_pListBoxListener;
66
};
67
68
ListBoxControl::ListBoxControl(vcl::Window* pParent, DropdownToolbarController* pListBoxListener)
69
0
    : InterimItemWindow(pParent, u"svt/ui/listcontrol.ui"_ustr, u"ListControl"_ustr)
70
0
    , m_xWidget(m_xBuilder->weld_combo_box(u"listbox"_ustr))
71
0
    , m_pListBoxListener( pListBoxListener )
72
0
{
73
0
    InitControlBase(m_xWidget.get());
74
75
0
    m_xWidget->connect_focus_in(LINK(this, ListBoxControl, FocusInHdl));
76
0
    m_xWidget->connect_focus_out(LINK(this, ListBoxControl, FocusOutHdl));
77
0
    m_xWidget->connect_changed(LINK(this, ListBoxControl, ModifyHdl));
78
0
    m_xWidget->connect_key_press(LINK(this, ListBoxControl, KeyInputHdl));
79
80
0
    m_xWidget->set_size_request(42, -1); // so a later narrow size request can stick
81
0
    SetSizePixel(get_preferred_size());
82
0
}
Unexecuted instantiation: framework::ListBoxControl::ListBoxControl(vcl::Window*, framework::DropdownToolbarController*)
Unexecuted instantiation: framework::ListBoxControl::ListBoxControl(vcl::Window*, framework::DropdownToolbarController*)
83
84
IMPL_LINK(ListBoxControl, KeyInputHdl, const ::KeyEvent&, rKEvt, bool)
85
0
{
86
0
    return ChildKeyInput(rKEvt);
87
0
}
88
89
ListBoxControl::~ListBoxControl()
90
0
{
91
0
    disposeOnce();
92
0
}
93
94
void ListBoxControl::dispose()
95
0
{
96
0
    m_pListBoxListener = nullptr;
97
0
    m_xWidget.reset();
98
0
    InterimItemWindow::dispose();
99
0
}
100
101
IMPL_LINK_NOARG(ListBoxControl, ModifyHdl, weld::ComboBox&, void)
102
0
{
103
0
    if (m_pListBoxListener)
104
0
        m_pListBoxListener->Select();
105
0
}
106
107
IMPL_LINK_NOARG(ListBoxControl, FocusInHdl, weld::Widget&, void)
108
0
{
109
0
    if (m_pListBoxListener)
110
0
        m_pListBoxListener->GetFocus();
111
0
}
112
113
IMPL_LINK_NOARG(ListBoxControl, FocusOutHdl, weld::Widget&, void)
114
0
{
115
0
    if (m_pListBoxListener)
116
0
        m_pListBoxListener->LoseFocus();
117
0
}
118
119
DropdownToolbarController::DropdownToolbarController(
120
    const Reference< XComponentContext >&    rxContext,
121
    const Reference< XFrame >&               rFrame,
122
    ToolBox*                                 pToolbar,
123
    ToolBoxItemId                            nID,
124
    sal_Int32                                nWidth,
125
    const OUString&                          aCommand ) :
126
0
    ComplexToolbarController( rxContext, rFrame, pToolbar, nID, aCommand )
127
0
    ,   m_pListBoxControl( nullptr )
128
0
{
129
0
    m_pListBoxControl = VclPtr<ListBoxControl>::Create(m_xToolbar, this);
130
0
    if ( nWidth == 0 )
131
0
        nWidth = 100;
132
133
    // ListBoxControl ctor has set a suitable height already
134
0
    auto nHeight = m_pListBoxControl->GetSizePixel().Height();
135
136
0
    m_pListBoxControl->SetSizePixel( ::Size( nWidth, nHeight ));
137
0
    m_xToolbar->SetItemWindow( m_nID, m_pListBoxControl );
138
0
}
139
140
DropdownToolbarController::~DropdownToolbarController()
141
0
{
142
0
}
143
144
void SAL_CALL DropdownToolbarController::dispose()
145
0
{
146
0
    SolarMutexGuard aSolarMutexGuard;
147
148
0
    m_xToolbar->SetItemWindow( m_nID, nullptr );
149
0
    m_pListBoxControl.disposeAndClear();
150
151
0
    ComplexToolbarController::dispose();
152
0
}
153
154
Sequence<PropertyValue> DropdownToolbarController::getExecuteArgs(sal_Int16 KeyModifier) const
155
0
{
156
0
    OUString aSelectedText = m_pListBoxControl->get_active_text();
157
158
    // Add key modifier to argument list
159
0
    Sequence<PropertyValue> aArgs{ comphelper::makePropertyValue(u"KeyModifier"_ustr, KeyModifier),
160
0
                                   comphelper::makePropertyValue(u"Text"_ustr, aSelectedText) };
161
0
    return aArgs;
162
0
}
163
164
void DropdownToolbarController::Select()
165
0
{
166
0
    if (m_pListBoxControl->get_count() > 0)
167
0
        execute(0);
168
0
}
169
170
void DropdownToolbarController::GetFocus()
171
0
{
172
0
    notifyFocusGet();
173
0
}
174
175
void DropdownToolbarController::LoseFocus()
176
0
{
177
0
    notifyFocusLost();
178
0
}
179
180
void DropdownToolbarController::executeControlCommand( const css::frame::ControlCommand& rControlCommand )
181
0
{
182
0
    if ( rControlCommand.Command == "SetList" )
183
0
    {
184
0
        for ( const NamedValue& rArg : rControlCommand.Arguments )
185
0
        {
186
0
            if ( rArg.Name == "List" )
187
0
            {
188
0
                Sequence< OUString > aList;
189
0
                m_pListBoxControl->clear();
190
191
0
                rArg.Value >>= aList;
192
0
                for (OUString const& rName : aList)
193
0
                    m_pListBoxControl->append_text(rName);
194
195
0
                m_pListBoxControl->set_active(0);
196
197
                // send notification
198
0
                uno::Sequence< beans::NamedValue > aInfo { { u"List"_ustr, css::uno::Any(aList) } };
199
0
                addNotifyInfo( u"ListChanged"_ustr,
200
0
                               getDispatchFromCommand( m_aCommandURL ),
201
0
                               aInfo );
202
203
0
                break;
204
0
            }
205
0
        }
206
0
    }
207
0
    else if ( rControlCommand.Command == "AddEntry" )
208
0
    {
209
0
        OUString   aText;
210
0
        for ( const NamedValue& rArg : rControlCommand.Arguments )
211
0
        {
212
0
            if ( rArg.Name == "Text" )
213
0
            {
214
0
                if ( rArg.Value >>= aText )
215
0
                    m_pListBoxControl->append_text(aText);
216
0
                break;
217
0
            }
218
0
        }
219
0
    }
220
0
    else if ( rControlCommand.Command == "InsertEntry" )
221
0
    {
222
0
        sal_Int32 nPos(-1);
223
0
        OUString   aText;
224
0
        for ( const NamedValue& rArg : rControlCommand.Arguments )
225
0
        {
226
0
            if ( rArg.Name == "Pos" )
227
0
            {
228
0
                sal_Int32 nTmpPos = 0;
229
0
                if ( rArg.Value >>= nTmpPos )
230
0
                {
231
0
                    if (( nTmpPos >= 0 ) &&
232
0
                        ( nTmpPos < m_pListBoxControl->get_count() ))
233
0
                        nPos = nTmpPos;
234
0
                }
235
0
            }
236
0
            else if ( rArg.Name == "Text" )
237
0
                rArg.Value >>= aText;
238
0
        }
239
240
0
        m_pListBoxControl->insert_text(nPos, aText);
241
0
    }
242
0
    else if ( rControlCommand.Command == "RemoveEntryPos" )
243
0
    {
244
0
        for ( const NamedValue& rArg : rControlCommand.Arguments )
245
0
        {
246
0
            if ( rArg.Name == "Pos" )
247
0
            {
248
0
                sal_Int32 nPos( -1 );
249
0
                if ( rArg.Value >>= nPos )
250
0
                {
251
0
                    if ( 0 <= nPos && nPos < m_pListBoxControl->get_count() )
252
0
                        m_pListBoxControl->remove(nPos);
253
0
                }
254
0
                break;
255
0
            }
256
0
        }
257
0
    }
258
0
    else if ( rControlCommand.Command == "RemoveEntryText" )
259
0
    {
260
0
        for ( const NamedValue& rArg : rControlCommand.Arguments )
261
0
        {
262
0
            if ( rArg.Name == "Text" )
263
0
            {
264
0
                OUString aText;
265
0
                if ( rArg.Value >>= aText )
266
0
                {
267
0
                    auto nPos = m_pListBoxControl->find_text(aText);
268
0
                    if (nPos != -1)
269
0
                        m_pListBoxControl->remove(nPos);
270
0
                }
271
0
                break;
272
0
            }
273
0
        }
274
0
    }
275
0
}
276
277
} // namespace
278
279
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */