Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / framework / source / uielement / comboboxtoolbarcontroller.cxx
blob77d6e3e8c8391980075fd4a7b869ebef80f0091d
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/comboboxtoolbarcontroller.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>
30 using namespace ::com::sun::star;
31 using namespace css::uno;
32 using namespace css::beans;
33 using namespace css::lang;
34 using namespace css::frame;
35 using namespace css::util;
37 namespace framework
40 // Wrapper class to notify controller about events from combobox.
41 // Unfortunately the events are notified through virtual methods instead
42 // of Listeners.
44 class ComboBoxControl final : public InterimItemWindow
46 public:
47 ComboBoxControl(vcl::Window* pParent, ComboboxToolbarController* pComboboxToolbarController);
48 virtual ~ComboBoxControl() override;
49 virtual void dispose() override;
51 void set_active_or_entry_text(const OUString& rText);
52 OUString get_active_text() const { return m_xWidget->get_active_text(); }
54 void clear() { m_xWidget->clear(); }
55 void remove(int nIndex) { m_xWidget->remove(nIndex); }
56 void append_text(const OUString& rStr) { m_xWidget->append_text(rStr); }
57 void insert_text(int pos, const OUString& rStr) { m_xWidget->insert_text(pos, rStr); }
58 int get_count() const { return m_xWidget->get_count(); }
59 int find_text(const OUString& rStr) const { return m_xWidget->find_text(rStr); }
61 DECL_LINK(FocusInHdl, weld::Widget&, void);
62 DECL_LINK(FocusOutHdl, weld::Widget&, void);
63 DECL_LINK(ModifyHdl, weld::ComboBox&, void);
64 DECL_LINK(ActivateHdl, weld::ComboBox&, bool);
65 DECL_LINK(KeyInputHdl, const ::KeyEvent&, bool);
67 private:
68 std::unique_ptr<weld::ComboBox> m_xWidget;
69 ComboboxToolbarController* m_pComboboxToolbarController;
72 ComboBoxControl::ComboBoxControl(vcl::Window* pParent, ComboboxToolbarController* pComboboxToolbarController)
73 : InterimItemWindow(pParent, "svt/ui/combocontrol.ui", "ComboControl")
74 , m_xWidget(m_xBuilder->weld_combo_box("combobox"))
75 , m_pComboboxToolbarController(pComboboxToolbarController)
77 InitControlBase(m_xWidget.get());
79 m_xWidget->connect_focus_in(LINK(this, ComboBoxControl, FocusInHdl));
80 m_xWidget->connect_focus_out(LINK(this, ComboBoxControl, FocusOutHdl));
81 m_xWidget->connect_changed(LINK(this, ComboBoxControl, ModifyHdl));
82 m_xWidget->connect_entry_activate(LINK(this, ComboBoxControl, ActivateHdl));
83 m_xWidget->connect_key_press(LINK(this, ComboBoxControl, KeyInputHdl));
85 m_xWidget->set_entry_width_chars(1); // so a smaller than default width can be used by ComboboxToolbarController
86 SetSizePixel(get_preferred_size());
89 IMPL_LINK(ComboBoxControl, KeyInputHdl, const ::KeyEvent&, rKEvt, bool)
91 return ChildKeyInput(rKEvt);
94 void ComboBoxControl::set_active_or_entry_text(const OUString& rText)
96 const int nFound = m_xWidget->find_text(rText);
97 if (nFound != -1)
98 m_xWidget->set_active(nFound);
99 else
100 m_xWidget->set_entry_text(rText);
103 ComboBoxControl::~ComboBoxControl()
105 disposeOnce();
108 void ComboBoxControl::dispose()
110 m_pComboboxToolbarController = nullptr;
111 m_xWidget.reset();
112 InterimItemWindow::dispose();
115 IMPL_LINK_NOARG(ComboBoxControl, ModifyHdl, weld::ComboBox&, void)
117 if (m_pComboboxToolbarController)
119 if (m_xWidget->get_count() && m_xWidget->changed_by_direct_pick())
120 m_pComboboxToolbarController->Select();
121 else
122 m_pComboboxToolbarController->Modify();
126 IMPL_LINK_NOARG(ComboBoxControl, FocusInHdl, weld::Widget&, void)
128 if (m_pComboboxToolbarController)
129 m_pComboboxToolbarController->GetFocus();
132 IMPL_LINK_NOARG(ComboBoxControl, FocusOutHdl, weld::Widget&, void)
134 if (m_pComboboxToolbarController)
135 m_pComboboxToolbarController->LoseFocus();
138 IMPL_LINK_NOARG(ComboBoxControl, ActivateHdl, weld::ComboBox&, bool)
140 if (m_pComboboxToolbarController)
141 m_pComboboxToolbarController->Activate();
142 return true;
145 ComboboxToolbarController::ComboboxToolbarController(
146 const Reference< XComponentContext >& rxContext,
147 const Reference< XFrame >& rFrame,
148 ToolBox* pToolbar,
149 ToolBoxItemId nID,
150 sal_Int32 nWidth,
151 const OUString& aCommand ) :
152 ComplexToolbarController( rxContext, rFrame, pToolbar, nID, aCommand )
153 , m_pComboBox( nullptr )
155 m_pComboBox = VclPtr<ComboBoxControl>::Create(m_xToolbar, this);
156 if ( nWidth == 0 )
157 nWidth = 100;
159 // ComboBoxControl ctor has set a suitable height already
160 auto nHeight = m_pComboBox->GetSizePixel().Height();
162 m_pComboBox->SetSizePixel( ::Size( nWidth, nHeight ));
163 m_xToolbar->SetItemWindow( m_nID, m_pComboBox );
166 ComboboxToolbarController::~ComboboxToolbarController()
170 void SAL_CALL ComboboxToolbarController::dispose()
172 SolarMutexGuard aSolarMutexGuard;
174 m_xToolbar->SetItemWindow( m_nID, nullptr );
175 m_pComboBox.disposeAndClear();
177 ComplexToolbarController::dispose();
180 Sequence<PropertyValue> ComboboxToolbarController::getExecuteArgs(sal_Int16 KeyModifier) const
182 OUString aSelectedText = m_pComboBox->get_active_text();
184 // Add key modifier to argument list
185 Sequence<PropertyValue> aArgs{ comphelper::makePropertyValue("KeyModifier", KeyModifier),
186 comphelper::makePropertyValue("Text", aSelectedText) };
187 return aArgs;
190 void ComboboxToolbarController::Select()
192 vcl::Window::PointerState aState = m_pComboBox->GetPointerState();
194 sal_uInt16 nKeyModifier = sal_uInt16( aState.mnState & KEY_MODIFIERS_MASK );
195 execute( nKeyModifier );
198 void ComboboxToolbarController::Modify()
200 notifyTextChanged(m_pComboBox->get_active_text());
203 void ComboboxToolbarController::GetFocus()
205 notifyFocusGet();
208 void ComboboxToolbarController::LoseFocus()
210 notifyFocusLost();
213 void ComboboxToolbarController::Activate()
215 // Call execute only with non-empty text
216 if (!m_pComboBox->get_active_text().isEmpty())
217 execute(0);
220 void ComboboxToolbarController::executeControlCommand( const css::frame::ControlCommand& rControlCommand )
222 if ( rControlCommand.Command == "SetText" )
224 for ( const NamedValue& rArg : rControlCommand.Arguments )
226 if ( rArg.Name == "Text" )
228 OUString aText;
229 rArg.Value >>= aText;
230 m_pComboBox->set_active_or_entry_text(aText);
232 // send notification
233 notifyTextChanged( aText );
234 break;
238 else if ( rControlCommand.Command == "SetList" )
240 for ( const NamedValue& rArg : rControlCommand.Arguments )
242 if ( rArg.Name == "List" )
244 Sequence< OUString > aList;
245 m_pComboBox->clear();
247 rArg.Value >>= aList;
248 for (OUString const & rName : std::as_const(aList))
249 m_pComboBox->append_text(rName);
251 // send notification
252 uno::Sequence< beans::NamedValue > aInfo { { "List", css::uno::Any(aList) } };
253 addNotifyInfo( "ListChanged",
254 getDispatchFromCommand( m_aCommandURL ),
255 aInfo );
257 break;
261 else if ( rControlCommand.Command == "AddEntry" )
263 OUString aText;
264 for ( const NamedValue& rArg : rControlCommand.Arguments )
266 if ( rArg.Name == "Text" )
268 if ( rArg.Value >>= aText )
269 m_pComboBox->append_text(aText);
270 break;
274 else if ( rControlCommand.Command == "InsertEntry" )
276 sal_Int32 nPos(-1);
277 OUString aText;
278 for ( const NamedValue& rArg : rControlCommand.Arguments )
280 if ( rArg.Name == "Pos" )
282 sal_Int32 nTmpPos = 0;
283 if ( rArg.Value >>= nTmpPos )
285 if (( nTmpPos >= 0 ) &&
286 ( nTmpPos < m_pComboBox->get_count() ))
287 nPos = nTmpPos;
290 else if ( rArg.Name == "Text" )
291 rArg.Value >>= aText;
294 m_pComboBox->insert_text(nPos, aText);
296 else if ( rControlCommand.Command == "RemoveEntryPos" )
298 for ( const NamedValue& rArg : rControlCommand.Arguments )
300 if ( rArg.Name == "Pos" )
302 sal_Int32 nPos( -1 );
303 if ( rArg.Value >>= nPos )
305 if (0 <= nPos && nPos < m_pComboBox->get_count())
306 m_pComboBox->remove(nPos);
308 break;
312 else if ( rControlCommand.Command == "RemoveEntryText" )
314 for ( const NamedValue& rArg : rControlCommand.Arguments )
316 if ( rArg.Name == "Text")
318 OUString aText;
319 if ( rArg.Value >>= aText )
321 auto nPos = m_pComboBox->find_text(aText);
322 if (nPos != -1)
323 m_pComboBox->remove(nPos);
325 break;
331 } // namespace
333 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */