1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <uielement/edittoolbarcontroller.hxx>
22 #include <com/sun/star/beans/PropertyValue.hpp>
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>
31 using namespace ::com::sun::star
;
32 using namespace css::uno
;
33 using namespace css::beans
;
34 using namespace css::frame
;
39 // Wrapper class to notify controller about events from edit.
40 // Unfortunaltly the events are notified through virtual methods instead
43 class EditControl final
: public InterimItemWindow
46 EditControl(vcl::Window
* pParent
, EditToolbarController
* pEditToolbarController
);
47 virtual ~EditControl() override
;
48 virtual void dispose() override
;
50 OUString
get_text() const { return m_xWidget
->get_text(); }
51 void set_text(const OUString
& rText
) { m_xWidget
->set_text(rText
); }
54 std::unique_ptr
<weld::Entry
> m_xWidget
;
55 EditToolbarController
* m_pEditToolbarController
;
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);
64 EditControl::EditControl(vcl::Window
* pParent
, EditToolbarController
* pEditToolbarController
)
65 : InterimItemWindow(pParent
, u
"svt/ui/editcontrol.ui"_ustr
, u
"EditControl"_ustr
)
66 , m_xWidget(m_xBuilder
->weld_entry(u
"entry"_ustr
))
67 , m_pEditToolbarController(pEditToolbarController
)
70 m_xWidget
->set_help_id(sEmpty
);
71 m_xContainer
->set_help_id(sEmpty
);
73 InitControlBase(m_xWidget
.get());
75 m_xWidget
->connect_focus_in(LINK(this, EditControl
, FocusInHdl
));
76 m_xWidget
->connect_focus_out(LINK(this, EditControl
, FocusOutHdl
));
77 m_xWidget
->connect_changed(LINK(this, EditControl
, ModifyHdl
));
78 m_xWidget
->connect_activate(LINK(this, EditControl
, ActivateHdl
));
79 m_xWidget
->connect_key_press(LINK(this, EditControl
, KeyInputHdl
));
81 SetSizePixel(get_preferred_size());
84 IMPL_LINK(EditControl
, KeyInputHdl
, const ::KeyEvent
&, rKEvt
, bool)
86 return ChildKeyInput(rKEvt
);
89 EditControl::~EditControl()
94 void EditControl::dispose()
96 m_pEditToolbarController
= nullptr;
98 InterimItemWindow::dispose();
101 IMPL_LINK_NOARG(EditControl
, ModifyHdl
, weld::Entry
&, void)
103 if (m_pEditToolbarController
)
104 m_pEditToolbarController
->Modify();
107 IMPL_LINK_NOARG(EditControl
, FocusInHdl
, weld::Widget
&, void)
109 if (m_pEditToolbarController
)
110 m_pEditToolbarController
->GetFocus();
113 IMPL_LINK_NOARG(EditControl
, FocusOutHdl
, weld::Widget
&, void)
115 if ( m_pEditToolbarController
)
116 m_pEditToolbarController
->LoseFocus();
119 IMPL_LINK_NOARG(EditControl
, ActivateHdl
, weld::Entry
&, bool)
121 if (m_pEditToolbarController
)
122 m_pEditToolbarController
->Activate();
126 EditToolbarController::EditToolbarController(
127 const Reference
< XComponentContext
>& rxContext
,
128 const Reference
< XFrame
>& rFrame
,
132 const OUString
& aCommand
) :
133 ComplexToolbarController( rxContext
, rFrame
, pToolbar
, nID
, aCommand
)
134 , m_pEditControl( nullptr )
136 m_pEditControl
= VclPtr
<EditControl
>::Create(m_xToolbar
, this);
140 // EditControl ctor has set a suitable height already
141 auto nHeight
= m_pEditControl
->GetSizePixel().Height();
143 m_pEditControl
->SetSizePixel( ::Size( nWidth
, nHeight
));
144 m_xToolbar
->SetItemWindow( m_nID
, m_pEditControl
);
147 EditToolbarController::~EditToolbarController()
151 void SAL_CALL
EditToolbarController::dispose()
153 SolarMutexGuard aSolarMutexGuard
;
155 m_xToolbar
->SetItemWindow( m_nID
, nullptr );
156 m_pEditControl
.disposeAndClear();
158 ComplexToolbarController::dispose();
161 Sequence
<PropertyValue
> EditToolbarController::getExecuteArgs(sal_Int16 KeyModifier
) const
163 OUString aSelectedText
= m_pEditControl
->get_text();
165 // Add key modifier to argument list
166 Sequence
<PropertyValue
> aArgs
{ comphelper::makePropertyValue(u
"KeyModifier"_ustr
, KeyModifier
),
167 comphelper::makePropertyValue(u
"Text"_ustr
, aSelectedText
) };
171 void EditToolbarController::Modify()
173 notifyTextChanged(m_pEditControl
->get_text());
176 void EditToolbarController::GetFocus()
181 void EditToolbarController::LoseFocus()
186 void EditToolbarController::Activate()
188 // Call execute only with non-empty text
189 if (!m_pEditControl
->get_text().isEmpty())
193 void EditToolbarController::executeControlCommand( const css::frame::ControlCommand
& rControlCommand
)
195 if ( !rControlCommand
.Command
.startsWith( "SetText" ))
198 for ( const NamedValue
& rArg
: rControlCommand
.Arguments
)
200 if ( rArg
.Name
.startsWith( "Text" ))
203 rArg
.Value
>>= aText
;
204 m_pEditControl
->set_text(aText
);
207 notifyTextChanged( aText
);
215 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */