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/edittoolbarcontroller.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/edittoolbarcontroller.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 <svtools/toolboxcontroller.hxx>
28
#include <vcl/svapp.hxx>
29
#include <vcl/toolbox.hxx>
30
#include <vcl/event.hxx>
31
#include <vcl/weld/Builder.hxx>
32
#include <vcl/weld/Container.hxx>
33
#include <vcl/weld/Entry.hxx>
34
35
using namespace ::com::sun::star;
36
using namespace css::uno;
37
using namespace css::beans;
38
using namespace css::frame;
39
40
namespace framework
41
{
42
43
// Wrapper class to notify controller about events from edit.
44
// Unfortunaltly the events are notified through virtual methods instead
45
// of Listeners.
46
47
class EditControl final : public InterimItemWindow
48
{
49
public:
50
    EditControl(vcl::Window* pParent, EditToolbarController* pEditToolbarController);
51
    virtual ~EditControl() override;
52
    virtual void dispose() override;
53
54
0
    OUString get_text() const { return m_xWidget->get_text(); }
55
0
    void set_text(const OUString& rText) { m_xWidget->set_text(rText); }
56
57
private:
58
    std::unique_ptr<weld::Entry> m_xWidget;
59
    EditToolbarController* m_pEditToolbarController;
60
61
    DECL_LINK(FocusInHdl, weld::Widget&, void);
62
    DECL_LINK(FocusOutHdl, weld::Widget&, void);
63
    DECL_LINK(ModifyHdl, weld::Entry&, void);
64
    DECL_LINK(ActivateHdl, weld::Entry&, bool);
65
    DECL_LINK(KeyInputHdl, const ::KeyEvent&, bool);
66
};
67
68
EditControl::EditControl(vcl::Window* pParent, EditToolbarController* pEditToolbarController)
69
0
    : InterimItemWindow(pParent, u"svt/ui/editcontrol.ui"_ustr, u"EditControl"_ustr)
70
0
    , m_xWidget(m_xBuilder->weld_entry(u"entry"_ustr))
71
0
    , m_pEditToolbarController(pEditToolbarController)
72
0
{
73
0
    OUString sEmpty;
74
0
    m_xWidget->set_help_id(sEmpty);
75
0
    m_xContainer->set_help_id(sEmpty);
76
77
0
    InitControlBase(m_xWidget.get());
78
79
0
    m_xWidget->connect_focus_in(LINK(this, EditControl, FocusInHdl));
80
0
    m_xWidget->connect_focus_out(LINK(this, EditControl, FocusOutHdl));
81
0
    m_xWidget->connect_changed(LINK(this, EditControl, ModifyHdl));
82
0
    m_xWidget->connect_activate(LINK(this, EditControl, ActivateHdl));
83
0
    m_xWidget->connect_key_press(LINK(this, EditControl, KeyInputHdl));
84
85
0
    SetSizePixel(get_preferred_size());
86
0
}
Unexecuted instantiation: framework::EditControl::EditControl(vcl::Window*, framework::EditToolbarController*)
Unexecuted instantiation: framework::EditControl::EditControl(vcl::Window*, framework::EditToolbarController*)
87
88
IMPL_LINK(EditControl, KeyInputHdl, const ::KeyEvent&, rKEvt, bool)
89
0
{
90
0
    return ChildKeyInput(rKEvt);
91
0
}
92
93
EditControl::~EditControl()
94
0
{
95
0
    disposeOnce();
96
0
}
97
98
void EditControl::dispose()
99
0
{
100
0
    m_pEditToolbarController = nullptr;
101
0
    m_xWidget.reset();
102
0
    InterimItemWindow::dispose();
103
0
}
104
105
IMPL_LINK_NOARG(EditControl, ModifyHdl, weld::Entry&, void)
106
0
{
107
0
    if (m_pEditToolbarController)
108
0
        m_pEditToolbarController->Modify();
109
0
}
110
111
IMPL_LINK_NOARG(EditControl, FocusInHdl, weld::Widget&, void)
112
0
{
113
0
    if (m_pEditToolbarController)
114
0
        m_pEditToolbarController->GetFocus();
115
0
}
116
117
IMPL_LINK_NOARG(EditControl, FocusOutHdl, weld::Widget&, void)
118
0
{
119
0
    if ( m_pEditToolbarController )
120
0
        m_pEditToolbarController->LoseFocus();
121
0
}
122
123
IMPL_LINK_NOARG(EditControl, ActivateHdl, weld::Entry&, bool)
124
0
{
125
0
    if (m_pEditToolbarController)
126
0
        m_pEditToolbarController->Activate();
127
0
    return true;
128
0
}
129
130
EditToolbarController::EditToolbarController(
131
    const Reference< XComponentContext >&    rxContext,
132
    const Reference< XFrame >&               rFrame,
133
    ToolBox*                                 pToolbar,
134
    ToolBoxItemId                            nID,
135
    sal_Int32                                nWidth,
136
    const OUString&                          aCommand ) :
137
0
    ComplexToolbarController( rxContext, rFrame, pToolbar, nID, aCommand )
138
0
    ,   m_pEditControl( nullptr )
139
0
{
140
0
    m_pEditControl = VclPtr<EditControl>::Create(m_xToolbar, this);
141
0
    if ( nWidth == 0 )
142
0
        nWidth = 100;
143
144
    // EditControl ctor has set a suitable height already
145
0
    auto nHeight = m_pEditControl->GetSizePixel().Height();
146
147
0
    m_pEditControl->SetSizePixel( ::Size( nWidth, nHeight ));
148
0
    m_xToolbar->SetItemWindow( m_nID, m_pEditControl );
149
0
}
Unexecuted instantiation: framework::EditToolbarController::EditToolbarController(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::EditToolbarController::EditToolbarController(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&)
150
151
EditToolbarController::~EditToolbarController()
152
0
{
153
0
}
154
155
void EditToolbarController::disposing(std::unique_lock<std::mutex>& rGuard)
156
0
{
157
0
    {
158
0
        comphelper::unique_unlock aUnlock(rGuard);
159
0
        SolarMutexGuard aSolarMutexGuard;
160
0
        m_xToolbar->SetItemWindow( m_nID, nullptr );
161
0
        m_pEditControl.disposeAndClear();
162
0
    }
163
0
    ComplexToolbarController::disposing(rGuard);
164
0
}
165
166
Sequence<PropertyValue> EditToolbarController::getExecuteArgs(sal_Int16 KeyModifier) const
167
0
{
168
0
    OUString aSelectedText = m_pEditControl->get_text();
169
170
    // Add key modifier to argument list
171
0
    Sequence<PropertyValue> aArgs{ comphelper::makePropertyValue(u"KeyModifier"_ustr, KeyModifier),
172
0
                                   comphelper::makePropertyValue(u"Text"_ustr, aSelectedText) };
173
0
    return aArgs;
174
0
}
175
176
void EditToolbarController::Modify()
177
0
{
178
0
    notifyTextChanged(m_pEditControl->get_text());
179
0
}
180
181
void EditToolbarController::GetFocus()
182
0
{
183
0
    notifyFocusGet();
184
0
}
185
186
void EditToolbarController::LoseFocus()
187
0
{
188
0
    notifyFocusLost();
189
0
}
190
191
void EditToolbarController::Activate()
192
0
{
193
    // Call execute only with non-empty text
194
0
    if (!m_pEditControl->get_text().isEmpty())
195
0
        execute(0);
196
0
}
197
198
void EditToolbarController::executeControlCommand( const css::frame::ControlCommand& rControlCommand )
199
0
{
200
0
    if ( !rControlCommand.Command.startsWith( "SetText" ))
201
0
        return;
202
203
0
    for ( const NamedValue& rArg : rControlCommand.Arguments )
204
0
    {
205
0
        if ( rArg.Name.startsWith( "Text" ))
206
0
        {
207
0
            OUString aText;
208
0
            rArg.Value >>= aText;
209
0
            m_pEditControl->set_text(aText);
210
211
            // send notification
212
0
            notifyTextChanged( aText );
213
0
            break;
214
0
        }
215
0
    }
216
0
}
217
218
} // namespace
219
220
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */