Coverage Report

Created: 2026-06-30 11:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/framework/source/uielement/comboboxtoolbarcontroller.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/comboboxtoolbarcontroller.hxx>
21
22
#include <com/sun/star/beans/PropertyValue.hpp>
23
24
#include <comphelper/propertyvalue.hxx>
25
#include <comphelper/unique_unlock.hxx>
26
#include <vcl/InterimItemWindow.hxx>
27
#include <vcl/keycodes.hxx>
28
#include <vcl/weld/Builder.hxx>
29
#include <svtools/toolboxcontroller.hxx>
30
#include <vcl/svapp.hxx>
31
#include <vcl/toolbox.hxx>
32
#include <vcl/weld/ComboBox.hxx>
33
34
using namespace ::com::sun::star;
35
using namespace css::uno;
36
using namespace css::beans;
37
using namespace css::frame;
38
39
namespace framework
40
{
41
42
// Wrapper class to notify controller about events from combobox.
43
// Unfortunately the events are notified through virtual methods instead
44
// of Listeners.
45
46
class ComboBoxControl final : public InterimItemWindow
47
{
48
public:
49
    ComboBoxControl(vcl::Window* pParent, ComboboxToolbarController* pComboboxToolbarController);
50
    virtual ~ComboBoxControl() override;
51
    virtual void dispose() override;
52
53
    void set_active_or_entry_text(const OUString& rText);
54
0
    OUString get_active_text() const { return m_xWidget->get_active_text(); }
55
56
0
    void clear() { m_xWidget->clear(); }
57
0
    void remove(int nIndex) { m_xWidget->remove(nIndex); }
58
0
    void append_text(const OUString& rStr) { m_xWidget->append_text(rStr); }
59
0
    void insert_text(int pos, const OUString& rStr) { m_xWidget->insert_text(pos, rStr); }
60
0
    int get_count() const { return m_xWidget->get_count(); }
61
0
    int find_text(const OUString& rStr) const { return m_xWidget->find_text(rStr); }
62
63
    DECL_LINK(FocusInHdl, weld::Widget&, void);
64
    DECL_LINK(FocusOutHdl, weld::Widget&, void);
65
    DECL_LINK(ModifyHdl, weld::ComboBox&, void);
66
    DECL_LINK(ActivateHdl, weld::ComboBox&, bool);
67
    DECL_LINK(KeyInputHdl, const ::KeyEvent&, bool);
68
69
private:
70
    std::unique_ptr<weld::ComboBox> m_xWidget;
71
    ComboboxToolbarController* m_pComboboxToolbarController;
72
};
73
74
ComboBoxControl::ComboBoxControl(vcl::Window* pParent, ComboboxToolbarController* pComboboxToolbarController)
75
0
    : InterimItemWindow(pParent, u"svt/ui/combocontrol.ui"_ustr, u"ComboControl"_ustr)
76
0
    , m_xWidget(m_xBuilder->weld_combo_box(u"combobox"_ustr))
77
0
    , m_pComboboxToolbarController(pComboboxToolbarController)
78
0
{
79
0
    InitControlBase(m_xWidget.get());
80
81
0
    m_xWidget->connect_focus_in(LINK(this, ComboBoxControl, FocusInHdl));
82
0
    m_xWidget->connect_focus_out(LINK(this, ComboBoxControl, FocusOutHdl));
83
0
    m_xWidget->connect_changed(LINK(this, ComboBoxControl, ModifyHdl));
84
0
    m_xWidget->connect_entry_activate(LINK(this, ComboBoxControl, ActivateHdl));
85
0
    m_xWidget->connect_key_press(LINK(this, ComboBoxControl, KeyInputHdl));
86
87
0
    m_xWidget->set_entry_width_chars(1); // so a smaller than default width can be used by ComboboxToolbarController
88
0
    SetSizePixel(get_preferred_size());
89
0
}
Unexecuted instantiation: framework::ComboBoxControl::ComboBoxControl(vcl::Window*, framework::ComboboxToolbarController*)
Unexecuted instantiation: framework::ComboBoxControl::ComboBoxControl(vcl::Window*, framework::ComboboxToolbarController*)
90
91
IMPL_LINK(ComboBoxControl, KeyInputHdl, const ::KeyEvent&, rKEvt, bool)
92
0
{
93
0
    return ChildKeyInput(rKEvt);
94
0
}
95
96
void ComboBoxControl::set_active_or_entry_text(const OUString& rText)
97
0
{
98
0
    const int nFound = m_xWidget->find_text(rText);
99
0
    if (nFound != -1)
100
0
        m_xWidget->set_active(nFound);
101
0
    else
102
0
        m_xWidget->set_entry_text(rText);
103
0
}
104
105
ComboBoxControl::~ComboBoxControl()
106
0
{
107
0
    disposeOnce();
108
0
}
109
110
void ComboBoxControl::dispose()
111
0
{
112
0
    m_pComboboxToolbarController = nullptr;
113
0
    m_xWidget.reset();
114
0
    InterimItemWindow::dispose();
115
0
}
116
117
IMPL_LINK_NOARG(ComboBoxControl, ModifyHdl, weld::ComboBox&, void)
118
0
{
119
0
    if (m_pComboboxToolbarController)
120
0
    {
121
0
        if (m_xWidget->get_count() && m_xWidget->changed_by_direct_pick())
122
0
            m_pComboboxToolbarController->Select();
123
0
        else
124
0
            m_pComboboxToolbarController->Modify();
125
0
    }
126
0
}
127
128
IMPL_LINK_NOARG(ComboBoxControl, FocusInHdl, weld::Widget&, void)
129
0
{
130
0
    if (m_pComboboxToolbarController)
131
0
        m_pComboboxToolbarController->GetFocus();
132
0
}
133
134
IMPL_LINK_NOARG(ComboBoxControl, FocusOutHdl, weld::Widget&, void)
135
0
{
136
0
    if (m_pComboboxToolbarController)
137
0
        m_pComboboxToolbarController->LoseFocus();
138
0
}
139
140
IMPL_LINK_NOARG(ComboBoxControl, ActivateHdl, weld::ComboBox&, bool)
141
0
{
142
0
    if (m_pComboboxToolbarController)
143
0
        m_pComboboxToolbarController->Activate();
144
0
    return true;
145
0
}
146
147
ComboboxToolbarController::ComboboxToolbarController(
148
    const Reference< XComponentContext >& rxContext,
149
    const Reference< XFrame >&            rFrame,
150
    ToolBox*                              pToolbar,
151
    ToolBoxItemId                         nID,
152
    sal_Int32                             nWidth,
153
    const OUString&                       aCommand ) :
154
0
    ComplexToolbarController( rxContext, rFrame, pToolbar, nID, aCommand )
155
0
    ,   m_pComboBox( nullptr )
156
0
{
157
0
    m_pComboBox = VclPtr<ComboBoxControl>::Create(m_xToolbar, this);
158
0
    if ( nWidth == 0 )
159
0
        nWidth = 100;
160
161
    // ComboBoxControl ctor has set a suitable height already
162
0
    auto nHeight = m_pComboBox->GetSizePixel().Height();
163
164
0
    m_pComboBox->SetSizePixel( ::Size( nWidth, nHeight ));
165
0
    m_xToolbar->SetItemWindow( m_nID, m_pComboBox );
166
0
}
Unexecuted instantiation: framework::ComboboxToolbarController::ComboboxToolbarController(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>, int, rtl::OUString const&)
Unexecuted instantiation: framework::ComboboxToolbarController::ComboboxToolbarController(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>, int, rtl::OUString const&)
167
168
ComboboxToolbarController::~ComboboxToolbarController()
169
0
{
170
0
}
171
172
void ComboboxToolbarController::disposing(std::unique_lock<std::mutex>& rGuard)
173
0
{
174
0
    {
175
0
        comphelper::unique_unlock aUnlock(rGuard);
176
0
        SolarMutexGuard aSolarMutexGuard;
177
0
        m_xToolbar->SetItemWindow( m_nID, nullptr );
178
0
        m_pComboBox.disposeAndClear();
179
0
    }
180
0
    ComplexToolbarController::disposing(rGuard);
181
0
}
182
183
Sequence<PropertyValue> ComboboxToolbarController::getExecuteArgs(sal_Int16 KeyModifier) const
184
0
{
185
0
    OUString aSelectedText = m_pComboBox->get_active_text();
186
187
    // Add key modifier to argument list
188
0
    Sequence<PropertyValue> aArgs{ comphelper::makePropertyValue(u"KeyModifier"_ustr, KeyModifier),
189
0
                                   comphelper::makePropertyValue(u"Text"_ustr, aSelectedText) };
190
0
    return aArgs;
191
0
}
192
193
void ComboboxToolbarController::Select()
194
0
{
195
0
    vcl::Window::PointerState aState = m_pComboBox->GetPointerState();
196
197
0
    sal_uInt16 nKeyModifier = sal_uInt16( aState.mnState & KEY_MODIFIERS_MASK );
198
0
    execute( nKeyModifier );
199
0
}
200
201
void ComboboxToolbarController::Modify()
202
0
{
203
0
    notifyTextChanged(m_pComboBox->get_active_text());
204
0
}
205
206
void ComboboxToolbarController::GetFocus()
207
0
{
208
0
    notifyFocusGet();
209
0
}
210
211
void ComboboxToolbarController::LoseFocus()
212
0
{
213
0
    notifyFocusLost();
214
0
}
215
216
void ComboboxToolbarController::Activate()
217
0
{
218
    // Call execute only with non-empty text
219
0
    if (!m_pComboBox->get_active_text().isEmpty())
220
0
        execute(0);
221
0
}
222
223
void ComboboxToolbarController::executeControlCommand( const css::frame::ControlCommand& rControlCommand )
224
0
{
225
0
    if ( rControlCommand.Command == "SetText" )
226
0
    {
227
0
        for ( const NamedValue& rArg : rControlCommand.Arguments )
228
0
        {
229
0
            if ( rArg.Name == "Text" )
230
0
            {
231
0
                OUString aText;
232
0
                rArg.Value >>= aText;
233
0
                m_pComboBox->set_active_or_entry_text(aText);
234
235
                // send notification
236
0
                notifyTextChanged( aText );
237
0
                break;
238
0
            }
239
0
        }
240
0
    }
241
0
    else if ( rControlCommand.Command == "SetList" )
242
0
    {
243
0
        for ( const NamedValue& rArg : rControlCommand.Arguments )
244
0
        {
245
0
            if ( rArg.Name == "List" )
246
0
            {
247
0
                Sequence< OUString > aList;
248
0
                m_pComboBox->clear();
249
250
0
                rArg.Value >>= aList;
251
0
                for (OUString const& rName : aList)
252
0
                    m_pComboBox->append_text(rName);
253
254
                // send notification
255
0
                uno::Sequence< beans::NamedValue > aInfo { { u"List"_ustr, css::uno::Any(aList) } };
256
0
                addNotifyInfo( u"ListChanged"_ustr,
257
0
                               getDispatchFromCommand( m_aCommandURL ),
258
0
                               aInfo );
259
260
0
                break;
261
0
            }
262
0
        }
263
0
    }
264
0
    else if ( rControlCommand.Command == "AddEntry" )
265
0
    {
266
0
        OUString   aText;
267
0
        for ( const NamedValue& rArg : rControlCommand.Arguments )
268
0
        {
269
0
            if ( rArg.Name == "Text" )
270
0
            {
271
0
                if ( rArg.Value >>= aText )
272
0
                    m_pComboBox->append_text(aText);
273
0
                break;
274
0
            }
275
0
        }
276
0
    }
277
0
    else if ( rControlCommand.Command == "InsertEntry" )
278
0
    {
279
0
        sal_Int32 nPos(-1);
280
0
        OUString   aText;
281
0
        for ( const NamedValue& rArg : rControlCommand.Arguments )
282
0
        {
283
0
            if ( rArg.Name == "Pos" )
284
0
            {
285
0
                sal_Int32 nTmpPos = 0;
286
0
                if ( rArg.Value >>= nTmpPos )
287
0
                {
288
0
                    if (( nTmpPos >= 0 ) &&
289
0
                        ( nTmpPos < m_pComboBox->get_count() ))
290
0
                        nPos = nTmpPos;
291
0
                }
292
0
            }
293
0
            else if ( rArg.Name == "Text" )
294
0
                rArg.Value >>= aText;
295
0
        }
296
297
0
        m_pComboBox->insert_text(nPos, aText);
298
0
    }
299
0
    else if ( rControlCommand.Command == "RemoveEntryPos" )
300
0
    {
301
0
        for ( const NamedValue& rArg : rControlCommand.Arguments )
302
0
        {
303
0
            if ( rArg.Name == "Pos" )
304
0
            {
305
0
                sal_Int32 nPos( -1 );
306
0
                if ( rArg.Value >>= nPos )
307
0
                {
308
0
                    if (0 <= nPos && nPos < m_pComboBox->get_count())
309
0
                        m_pComboBox->remove(nPos);
310
0
                }
311
0
                break;
312
0
            }
313
0
        }
314
0
    }
315
0
    else if ( rControlCommand.Command == "RemoveEntryText" )
316
0
    {
317
0
        for ( const NamedValue& rArg : rControlCommand.Arguments )
318
0
        {
319
0
            if ( rArg.Name == "Text")
320
0
            {
321
0
                OUString aText;
322
0
                if ( rArg.Value >>= aText )
323
0
                {
324
0
                    auto nPos = m_pComboBox->find_text(aText);
325
0
                    if (nPos != -1)
326
0
                        m_pComboBox->remove(nPos);
327
0
                }
328
0
                break;
329
0
            }
330
0
        }
331
0
    }
332
0
}
333
334
} // namespace
335
336
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */