Coverage Report

Created: 2025-12-08 09:28

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