LibreOffice Module framework (master) 1
comboboxtoolbarcontroller.cxx
Go to the documentation of this file.
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
21
22#include <com/sun/star/beans/PropertyValue.hpp>
23
27#include <vcl/svapp.hxx>
28#include <vcl/toolbox.hxx>
29
30using namespace ::com::sun::star;
31using namespace css::uno;
32using namespace css::beans;
33using namespace css::lang;
34using namespace css::frame;
35using namespace css::util;
36
37namespace framework
38{
39
40// Wrapper class to notify controller about events from combobox.
41// Unfortunately the events are notified through virtual methods instead
42// of Listeners.
43
45{
46public:
47 ComboBoxControl(vcl::Window* pParent, ComboboxToolbarController* pComboboxToolbarController);
48 virtual ~ComboBoxControl() override;
49 virtual void dispose() override;
50
51 void set_active_or_entry_text(const OUString& rText);
52 OUString get_active_text() const { return m_xWidget->get_active_text(); }
53
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); }
60
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);
66
67private:
68 std::unique_ptr<weld::ComboBox> m_xWidget;
70};
71
73 : InterimItemWindow(pParent, "svt/ui/combocontrol.ui", "ComboControl")
74 , m_xWidget(m_xBuilder->weld_combo_box("combobox"))
75 , m_pComboboxToolbarController(pComboboxToolbarController)
76{
78
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));
84
85 m_xWidget->set_entry_width_chars(1); // so a smaller than default width can be used by ComboboxToolbarController
87}
88
89IMPL_LINK(ComboBoxControl, KeyInputHdl, const ::KeyEvent&, rKEvt, bool)
90{
91 return ChildKeyInput(rKEvt);
92}
93
95{
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);
101}
102
104{
105 disposeOnce();
106}
107
109{
111 m_xWidget.reset();
113}
114
116{
117 if (m_pComboboxToolbarController)
118 {
119 if (m_xWidget->get_count() && m_xWidget->changed_by_direct_pick())
120 m_pComboboxToolbarController->Select();
121 else
122 m_pComboboxToolbarController->Modify();
123 }
124}
125
127{
128 if (m_pComboboxToolbarController)
129 m_pComboboxToolbarController->GetFocus();
130}
131
133{
134 if (m_pComboboxToolbarController)
135 m_pComboboxToolbarController->LoseFocus();
136}
137
139{
140 if (m_pComboboxToolbarController)
141 m_pComboboxToolbarController->Activate();
142 return true;
143}
144
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 )
154{
156 if ( nWidth == 0 )
157 nWidth = 100;
158
159 // ComboBoxControl ctor has set a suitable height already
160 auto nHeight = m_pComboBox->GetSizePixel().Height();
161
162 m_pComboBox->SetSizePixel( ::Size( nWidth, nHeight ));
163 m_xToolbar->SetItemWindow( m_nID, m_pComboBox );
164}
165
167{
168}
169
171{
172 SolarMutexGuard aSolarMutexGuard;
173
174 m_xToolbar->SetItemWindow( m_nID, nullptr );
176
178}
179
180Sequence<PropertyValue> ComboboxToolbarController::getExecuteArgs(sal_Int16 KeyModifier) const
181{
182 OUString aSelectedText = m_pComboBox->get_active_text();
183
184 // Add key modifier to argument list
185 Sequence<PropertyValue> aArgs{ comphelper::makePropertyValue("KeyModifier", KeyModifier),
186 comphelper::makePropertyValue("Text", aSelectedText) };
187 return aArgs;
188}
189
191{
192 vcl::Window::PointerState aState = m_pComboBox->GetPointerState();
193
194 sal_uInt16 nKeyModifier = sal_uInt16( aState.mnState & KEY_MODIFIERS_MASK );
195 execute( nKeyModifier );
196}
197
199{
200 notifyTextChanged(m_pComboBox->get_active_text());
201}
202
204{
206}
207
209{
211}
212
214{
215 // Call execute only with non-empty text
216 if (!m_pComboBox->get_active_text().isEmpty())
217 execute(0);
218}
219
220void ComboboxToolbarController::executeControlCommand( const css::frame::ControlCommand& rControlCommand )
221{
222 if ( rControlCommand.Command == "SetText" )
223 {
224 for ( const NamedValue& rArg : rControlCommand.Arguments )
225 {
226 if ( rArg.Name == "Text" )
227 {
228 OUString aText;
229 rArg.Value >>= aText;
230 m_pComboBox->set_active_or_entry_text(aText);
231
232 // send notification
233 notifyTextChanged( aText );
234 break;
235 }
236 }
237 }
238 else if ( rControlCommand.Command == "SetList" )
239 {
240 for ( const NamedValue& rArg : rControlCommand.Arguments )
241 {
242 if ( rArg.Name == "List" )
243 {
244 Sequence< OUString > aList;
246
247 rArg.Value >>= aList;
248 for (OUString const & rName : std::as_const(aList))
249 m_pComboBox->append_text(rName);
250
251 // send notification
252 uno::Sequence< beans::NamedValue > aInfo { { "List", css::uno::Any(aList) } };
253 addNotifyInfo( "ListChanged",
255 aInfo );
256
257 break;
258 }
259 }
260 }
261 else if ( rControlCommand.Command == "AddEntry" )
262 {
263 OUString aText;
264 for ( const NamedValue& rArg : rControlCommand.Arguments )
265 {
266 if ( rArg.Name == "Text" )
267 {
268 if ( rArg.Value >>= aText )
269 m_pComboBox->append_text(aText);
270 break;
271 }
272 }
273 }
274 else if ( rControlCommand.Command == "InsertEntry" )
275 {
276 sal_Int32 nPos(-1);
277 OUString aText;
278 for ( const NamedValue& rArg : rControlCommand.Arguments )
279 {
280 if ( rArg.Name == "Pos" )
281 {
282 sal_Int32 nTmpPos = 0;
283 if ( rArg.Value >>= nTmpPos )
284 {
285 if (( nTmpPos >= 0 ) &&
286 ( nTmpPos < m_pComboBox->get_count() ))
287 nPos = nTmpPos;
288 }
289 }
290 else if ( rArg.Name == "Text" )
291 rArg.Value >>= aText;
292 }
293
294 m_pComboBox->insert_text(nPos, aText);
295 }
296 else if ( rControlCommand.Command == "RemoveEntryPos" )
297 {
298 for ( const NamedValue& rArg : rControlCommand.Arguments )
299 {
300 if ( rArg.Name == "Pos" )
301 {
302 sal_Int32 nPos( -1 );
303 if ( rArg.Value >>= nPos )
304 {
305 if (0 <= nPos && nPos < m_pComboBox->get_count())
306 m_pComboBox->remove(nPos);
307 }
308 break;
309 }
310 }
311 }
312 else if ( rControlCommand.Command == "RemoveEntryText" )
313 {
314 for ( const NamedValue& rArg : rControlCommand.Arguments )
315 {
316 if ( rArg.Name == "Text")
317 {
318 OUString aText;
319 if ( rArg.Value >>= aText )
320 {
321 auto nPos = m_pComboBox->find_text(aText);
322 if (nPos != -1)
323 m_pComboBox->remove(nPos);
324 }
325 break;
326 }
327 }
328 }
329}
330
331} // namespace
332
333/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
std::unique_ptr< weld::Image > m_xWidget
virtual void dispose() override
void InitControlBase(weld::Widget *pWidget)
void disposeAndClear()
void clear()
static VclPtr< reference_type > Create(Arg &&... arg)
int find_text(const OUString &rStr) const
DECL_LINK(ActivateHdl, weld::ComboBox &, bool)
ComboBoxControl(vcl::Window *pParent, ComboboxToolbarController *pComboboxToolbarController)
void set_active_or_entry_text(const OUString &rText)
std::unique_ptr< weld::ComboBox > m_xWidget
ComboboxToolbarController * m_pComboboxToolbarController
DECL_LINK(FocusInHdl, weld::Widget &, void)
void append_text(const OUString &rStr)
DECL_LINK(ModifyHdl, weld::ComboBox &, void)
void insert_text(int pos, const OUString &rStr)
DECL_LINK(FocusOutHdl, weld::Widget &, void)
DECL_LINK(KeyInputHdl, const ::KeyEvent &, bool)
virtual css::uno::Sequence< css::beans::PropertyValue > getExecuteArgs(sal_Int16 KeyModifier) const override
virtual void SAL_CALL dispose() override
ComboboxToolbarController(const css::uno::Reference< css::uno::XComponentContext > &rxContext, const css::uno::Reference< css::frame::XFrame > &rFrame, ToolBox *pToolBar, ToolBoxItemId nID, sal_Int32 nWidth, const OUString &aCommand)
virtual void executeControlCommand(const css::frame::ControlCommand &rControlCommand) override
css::uno::Reference< css::frame::XDispatch > getDispatchFromCommand(const OUString &aCommand) const
virtual void SAL_CALL dispose() override
void addNotifyInfo(const OUString &aEventName, const css::uno::Reference< css::frame::XDispatch > &xDispatch, const css::uno::Sequence< css::beans::NamedValue > &rInfo)
virtual void SAL_CALL execute(sal_Int16 KeyModifier) override
virtual void SetSizePixel(const Size &rNewSize)
Size get_preferred_size() const
sal_Int32 nIndex
constexpr sal_uInt16 KEY_MODIFIERS_MASK
sal_uInt16 nPos
css::beans::PropertyValue makePropertyValue(const OUString &rName, T &&rValue)
IMPL_LINK(WindowCommandDispatch, impl_notifyCommand, VclWindowEvent &, rEvent, void)
IMPL_LINK_NOARG(CloseDispatcher, impl_asyncCallback, LinkParamNone *, void)
asynchronous callback @descr We start all actions inside this object asynchronous (see comments there...
OUString aCommand
size_t pos