tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / framework / source / uielement / edittoolbarcontroller.cxx
blob46bf56c2ff229b90c9b4ca8e5763df645b60db87
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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;
36 namespace framework
39 // Wrapper class to notify controller about events from edit.
40 // Unfortunaltly the events are notified through virtual methods instead
41 // of Listeners.
43 class EditControl final : public InterimItemWindow
45 public:
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); }
53 private:
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)
69 OUString sEmpty;
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()
91 disposeOnce();
94 void EditControl::dispose()
96 m_pEditToolbarController = nullptr;
97 m_xWidget.reset();
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();
123 return true;
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 ComplexToolbarController( rxContext, rFrame, pToolbar, nID, aCommand )
134 , m_pEditControl( nullptr )
136 m_pEditControl = VclPtr<EditControl>::Create(m_xToolbar, this);
137 if ( nWidth == 0 )
138 nWidth = 100;
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) };
168 return aArgs;
171 void EditToolbarController::Modify()
173 notifyTextChanged(m_pEditControl->get_text());
176 void EditToolbarController::GetFocus()
178 notifyFocusGet();
181 void EditToolbarController::LoseFocus()
183 notifyFocusLost();
186 void EditToolbarController::Activate()
188 // Call execute only with non-empty text
189 if (!m_pEditControl->get_text().isEmpty())
190 execute(0);
193 void EditToolbarController::executeControlCommand( const css::frame::ControlCommand& rControlCommand )
195 if ( !rControlCommand.Command.startsWith( "SetText" ))
196 return;
198 for ( const NamedValue& rArg : rControlCommand.Arguments )
200 if ( rArg.Name.startsWith( "Text" ))
202 OUString aText;
203 rArg.Value >>= aText;
204 m_pEditControl->set_text(aText);
206 // send notification
207 notifyTextChanged( aText );
208 break;
213 } // namespace
215 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */