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::lang
;
35 using namespace css::frame
;
36 using namespace css::util
;
41 // Wrapper class to notify controller about events from edit.
42 // Unfortunaltly the events are notified through virtual methods instead
45 class EditControl final
: public InterimItemWindow
48 EditControl(vcl::Window
* pParent
, EditToolbarController
* pEditToolbarController
);
49 virtual ~EditControl() override
;
50 virtual void dispose() override
;
52 OUString
get_text() const { return m_xWidget
->get_text(); }
53 void set_text(const OUString
& rText
) { m_xWidget
->set_text(rText
); }
56 std::unique_ptr
<weld::Entry
> m_xWidget
;
57 EditToolbarController
* m_pEditToolbarController
;
59 DECL_LINK(FocusInHdl
, weld::Widget
&, void);
60 DECL_LINK(FocusOutHdl
, weld::Widget
&, void);
61 DECL_LINK(ModifyHdl
, weld::Entry
&, void);
62 DECL_LINK(ActivateHdl
, weld::Entry
&, bool);
63 DECL_LINK(KeyInputHdl
, const ::KeyEvent
&, bool);
66 EditControl::EditControl(vcl::Window
* pParent
, EditToolbarController
* pEditToolbarController
)
67 : InterimItemWindow(pParent
, "svt/ui/editcontrol.ui", "EditControl")
68 , m_xWidget(m_xBuilder
->weld_entry("entry"))
69 , m_pEditToolbarController(pEditToolbarController
)
72 m_xWidget
->set_help_id(sEmpty
);
73 m_xContainer
->set_help_id(sEmpty
);
75 InitControlBase(m_xWidget
.get());
77 m_xWidget
->connect_focus_in(LINK(this, EditControl
, FocusInHdl
));
78 m_xWidget
->connect_focus_out(LINK(this, EditControl
, FocusOutHdl
));
79 m_xWidget
->connect_changed(LINK(this, EditControl
, ModifyHdl
));
80 m_xWidget
->connect_activate(LINK(this, EditControl
, ActivateHdl
));
81 m_xWidget
->connect_key_press(LINK(this, EditControl
, KeyInputHdl
));
83 SetSizePixel(get_preferred_size());
86 IMPL_LINK(EditControl
, KeyInputHdl
, const ::KeyEvent
&, rKEvt
, bool)
88 return ChildKeyInput(rKEvt
);
91 EditControl::~EditControl()
96 void EditControl::dispose()
98 m_pEditToolbarController
= nullptr;
100 InterimItemWindow::dispose();
103 IMPL_LINK_NOARG(EditControl
, ModifyHdl
, weld::Entry
&, void)
105 if (m_pEditToolbarController
)
106 m_pEditToolbarController
->Modify();
109 IMPL_LINK_NOARG(EditControl
, FocusInHdl
, weld::Widget
&, void)
111 if (m_pEditToolbarController
)
112 m_pEditToolbarController
->GetFocus();
115 IMPL_LINK_NOARG(EditControl
, FocusOutHdl
, weld::Widget
&, void)
117 if ( m_pEditToolbarController
)
118 m_pEditToolbarController
->LoseFocus();
121 IMPL_LINK_NOARG(EditControl
, ActivateHdl
, weld::Entry
&, bool)
123 if (m_pEditToolbarController
)
124 m_pEditToolbarController
->Activate();
128 EditToolbarController::EditToolbarController(
129 const Reference
< XComponentContext
>& rxContext
,
130 const Reference
< XFrame
>& rFrame
,
134 const OUString
& aCommand
) :
135 ComplexToolbarController( rxContext
, rFrame
, pToolbar
, nID
, aCommand
)
136 , m_pEditControl( nullptr )
138 m_pEditControl
= VclPtr
<EditControl
>::Create(m_xToolbar
, this);
142 // EditControl ctor has set a suitable height already
143 auto nHeight
= m_pEditControl
->GetSizePixel().Height();
145 m_pEditControl
->SetSizePixel( ::Size( nWidth
, nHeight
));
146 m_xToolbar
->SetItemWindow( m_nID
, m_pEditControl
);
149 EditToolbarController::~EditToolbarController()
153 void SAL_CALL
EditToolbarController::dispose()
155 SolarMutexGuard aSolarMutexGuard
;
157 m_xToolbar
->SetItemWindow( m_nID
, nullptr );
158 m_pEditControl
.disposeAndClear();
160 ComplexToolbarController::dispose();
163 Sequence
<PropertyValue
> EditToolbarController::getExecuteArgs(sal_Int16 KeyModifier
) const
165 OUString aSelectedText
= m_pEditControl
->get_text();
167 // Add key modifier to argument list
168 Sequence
<PropertyValue
> aArgs
{ comphelper::makePropertyValue("KeyModifier", KeyModifier
),
169 comphelper::makePropertyValue("Text", aSelectedText
) };
173 void EditToolbarController::Modify()
175 notifyTextChanged(m_pEditControl
->get_text());
178 void EditToolbarController::GetFocus()
183 void EditToolbarController::LoseFocus()
188 void EditToolbarController::Activate()
190 // Call execute only with non-empty text
191 if (!m_pEditControl
->get_text().isEmpty())
195 void EditToolbarController::executeControlCommand( const css::frame::ControlCommand
& rControlCommand
)
197 if ( !rControlCommand
.Command
.startsWith( "SetText" ))
200 for ( const NamedValue
& rArg
: rControlCommand
.Arguments
)
202 if ( rArg
.Name
.startsWith( "Text" ))
205 rArg
.Value
>>= aText
;
206 m_pEditControl
->set_text(aText
);
209 notifyTextChanged( aText
);
217 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */