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/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::frame
;
38 // Wrapper class to notify controller about events from combobox.
39 // Unfortunately the events are notified through virtual methods instead
42 class ComboBoxControl final
: public InterimItemWindow
45 ComboBoxControl(vcl::Window
* pParent
, ComboboxToolbarController
* pComboboxToolbarController
);
46 virtual ~ComboBoxControl() override
;
47 virtual void dispose() override
;
49 void set_active_or_entry_text(const OUString
& rText
);
50 OUString
get_active_text() const { return m_xWidget
->get_active_text(); }
52 void clear() { m_xWidget
->clear(); }
53 void remove(int nIndex
) { m_xWidget
->remove(nIndex
); }
54 void append_text(const OUString
& rStr
) { m_xWidget
->append_text(rStr
); }
55 void insert_text(int pos
, const OUString
& rStr
) { m_xWidget
->insert_text(pos
, rStr
); }
56 int get_count() const { return m_xWidget
->get_count(); }
57 int find_text(const OUString
& rStr
) const { return m_xWidget
->find_text(rStr
); }
59 DECL_LINK(FocusInHdl
, weld::Widget
&, void);
60 DECL_LINK(FocusOutHdl
, weld::Widget
&, void);
61 DECL_LINK(ModifyHdl
, weld::ComboBox
&, void);
62 DECL_LINK(ActivateHdl
, weld::ComboBox
&, bool);
63 DECL_LINK(KeyInputHdl
, const ::KeyEvent
&, bool);
66 std::unique_ptr
<weld::ComboBox
> m_xWidget
;
67 ComboboxToolbarController
* m_pComboboxToolbarController
;
70 ComboBoxControl::ComboBoxControl(vcl::Window
* pParent
, ComboboxToolbarController
* pComboboxToolbarController
)
71 : InterimItemWindow(pParent
, u
"svt/ui/combocontrol.ui"_ustr
, u
"ComboControl"_ustr
)
72 , m_xWidget(m_xBuilder
->weld_combo_box(u
"combobox"_ustr
))
73 , m_pComboboxToolbarController(pComboboxToolbarController
)
75 InitControlBase(m_xWidget
.get());
77 m_xWidget
->connect_focus_in(LINK(this, ComboBoxControl
, FocusInHdl
));
78 m_xWidget
->connect_focus_out(LINK(this, ComboBoxControl
, FocusOutHdl
));
79 m_xWidget
->connect_changed(LINK(this, ComboBoxControl
, ModifyHdl
));
80 m_xWidget
->connect_entry_activate(LINK(this, ComboBoxControl
, ActivateHdl
));
81 m_xWidget
->connect_key_press(LINK(this, ComboBoxControl
, KeyInputHdl
));
83 m_xWidget
->set_entry_width_chars(1); // so a smaller than default width can be used by ComboboxToolbarController
84 SetSizePixel(get_preferred_size());
87 IMPL_LINK(ComboBoxControl
, KeyInputHdl
, const ::KeyEvent
&, rKEvt
, bool)
89 return ChildKeyInput(rKEvt
);
92 void ComboBoxControl::set_active_or_entry_text(const OUString
& rText
)
94 const int nFound
= m_xWidget
->find_text(rText
);
96 m_xWidget
->set_active(nFound
);
98 m_xWidget
->set_entry_text(rText
);
101 ComboBoxControl::~ComboBoxControl()
106 void ComboBoxControl::dispose()
108 m_pComboboxToolbarController
= nullptr;
110 InterimItemWindow::dispose();
113 IMPL_LINK_NOARG(ComboBoxControl
, ModifyHdl
, weld::ComboBox
&, void)
115 if (m_pComboboxToolbarController
)
117 if (m_xWidget
->get_count() && m_xWidget
->changed_by_direct_pick())
118 m_pComboboxToolbarController
->Select();
120 m_pComboboxToolbarController
->Modify();
124 IMPL_LINK_NOARG(ComboBoxControl
, FocusInHdl
, weld::Widget
&, void)
126 if (m_pComboboxToolbarController
)
127 m_pComboboxToolbarController
->GetFocus();
130 IMPL_LINK_NOARG(ComboBoxControl
, FocusOutHdl
, weld::Widget
&, void)
132 if (m_pComboboxToolbarController
)
133 m_pComboboxToolbarController
->LoseFocus();
136 IMPL_LINK_NOARG(ComboBoxControl
, ActivateHdl
, weld::ComboBox
&, bool)
138 if (m_pComboboxToolbarController
)
139 m_pComboboxToolbarController
->Activate();
143 ComboboxToolbarController::ComboboxToolbarController(
144 const Reference
< XComponentContext
>& rxContext
,
145 const Reference
< XFrame
>& rFrame
,
149 const OUString
& aCommand
) :
150 ComplexToolbarController( rxContext
, rFrame
, pToolbar
, nID
, aCommand
)
151 , m_pComboBox( nullptr )
153 m_pComboBox
= VclPtr
<ComboBoxControl
>::Create(m_xToolbar
, this);
157 // ComboBoxControl ctor has set a suitable height already
158 auto nHeight
= m_pComboBox
->GetSizePixel().Height();
160 m_pComboBox
->SetSizePixel( ::Size( nWidth
, nHeight
));
161 m_xToolbar
->SetItemWindow( m_nID
, m_pComboBox
);
164 ComboboxToolbarController::~ComboboxToolbarController()
168 void SAL_CALL
ComboboxToolbarController::dispose()
170 SolarMutexGuard aSolarMutexGuard
;
172 m_xToolbar
->SetItemWindow( m_nID
, nullptr );
173 m_pComboBox
.disposeAndClear();
175 ComplexToolbarController::dispose();
178 Sequence
<PropertyValue
> ComboboxToolbarController::getExecuteArgs(sal_Int16 KeyModifier
) const
180 OUString aSelectedText
= m_pComboBox
->get_active_text();
182 // Add key modifier to argument list
183 Sequence
<PropertyValue
> aArgs
{ comphelper::makePropertyValue(u
"KeyModifier"_ustr
, KeyModifier
),
184 comphelper::makePropertyValue(u
"Text"_ustr
, aSelectedText
) };
188 void ComboboxToolbarController::Select()
190 vcl::Window::PointerState aState
= m_pComboBox
->GetPointerState();
192 sal_uInt16 nKeyModifier
= sal_uInt16( aState
.mnState
& KEY_MODIFIERS_MASK
);
193 execute( nKeyModifier
);
196 void ComboboxToolbarController::Modify()
198 notifyTextChanged(m_pComboBox
->get_active_text());
201 void ComboboxToolbarController::GetFocus()
206 void ComboboxToolbarController::LoseFocus()
211 void ComboboxToolbarController::Activate()
213 // Call execute only with non-empty text
214 if (!m_pComboBox
->get_active_text().isEmpty())
218 void ComboboxToolbarController::executeControlCommand( const css::frame::ControlCommand
& rControlCommand
)
220 if ( rControlCommand
.Command
== "SetText" )
222 for ( const NamedValue
& rArg
: rControlCommand
.Arguments
)
224 if ( rArg
.Name
== "Text" )
227 rArg
.Value
>>= aText
;
228 m_pComboBox
->set_active_or_entry_text(aText
);
231 notifyTextChanged( aText
);
236 else if ( rControlCommand
.Command
== "SetList" )
238 for ( const NamedValue
& rArg
: rControlCommand
.Arguments
)
240 if ( rArg
.Name
== "List" )
242 Sequence
< OUString
> aList
;
243 m_pComboBox
->clear();
245 rArg
.Value
>>= aList
;
246 for (OUString
const& rName
: aList
)
247 m_pComboBox
->append_text(rName
);
250 uno::Sequence
< beans::NamedValue
> aInfo
{ { u
"List"_ustr
, css::uno::Any(aList
) } };
251 addNotifyInfo( u
"ListChanged"_ustr
,
252 getDispatchFromCommand( m_aCommandURL
),
259 else if ( rControlCommand
.Command
== "AddEntry" )
262 for ( const NamedValue
& rArg
: rControlCommand
.Arguments
)
264 if ( rArg
.Name
== "Text" )
266 if ( rArg
.Value
>>= aText
)
267 m_pComboBox
->append_text(aText
);
272 else if ( rControlCommand
.Command
== "InsertEntry" )
276 for ( const NamedValue
& rArg
: rControlCommand
.Arguments
)
278 if ( rArg
.Name
== "Pos" )
280 sal_Int32 nTmpPos
= 0;
281 if ( rArg
.Value
>>= nTmpPos
)
283 if (( nTmpPos
>= 0 ) &&
284 ( nTmpPos
< m_pComboBox
->get_count() ))
288 else if ( rArg
.Name
== "Text" )
289 rArg
.Value
>>= aText
;
292 m_pComboBox
->insert_text(nPos
, aText
);
294 else if ( rControlCommand
.Command
== "RemoveEntryPos" )
296 for ( const NamedValue
& rArg
: rControlCommand
.Arguments
)
298 if ( rArg
.Name
== "Pos" )
300 sal_Int32
nPos( -1 );
301 if ( rArg
.Value
>>= nPos
)
303 if (0 <= nPos
&& nPos
< m_pComboBox
->get_count())
304 m_pComboBox
->remove(nPos
);
310 else if ( rControlCommand
.Command
== "RemoveEntryText" )
312 for ( const NamedValue
& rArg
: rControlCommand
.Arguments
)
314 if ( rArg
.Name
== "Text")
317 if ( rArg
.Value
>>= aText
)
319 auto nPos
= m_pComboBox
->find_text(aText
);
321 m_pComboBox
->remove(nPos
);
331 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */