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/dropdownboxtoolbarcontroller.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 ListBox.
39 // Unfortunaltly the events are notified through virtual methods instead
42 class ListBoxControl final
: public InterimItemWindow
45 ListBoxControl(vcl::Window
* pParent
, DropdownToolbarController
* pListBoxListener
);
46 virtual ~ListBoxControl() override
;
47 virtual void dispose() override
;
49 void set_active(int nPos
) { m_xWidget
->set_active(nPos
); }
50 void append_text(const OUString
& rStr
) { m_xWidget
->append_text(rStr
); }
51 void insert_text(int nPos
, const OUString
& rStr
) { m_xWidget
->insert_text(nPos
, rStr
); }
52 int get_count() const { return m_xWidget
->get_count(); }
53 int find_text(const OUString
& rStr
) const { return m_xWidget
->find_text(rStr
); }
54 OUString
get_active_text() const { return m_xWidget
->get_active_text(); }
55 void clear() { return m_xWidget
->clear(); }
56 void remove(int nPos
) { m_xWidget
->remove(nPos
); }
58 DECL_LINK(FocusInHdl
, weld::Widget
&, void);
59 DECL_LINK(FocusOutHdl
, weld::Widget
&, void);
60 DECL_LINK(ModifyHdl
, weld::ComboBox
&, void);
61 DECL_LINK(KeyInputHdl
, const ::KeyEvent
&, bool);
64 std::unique_ptr
<weld::ComboBox
> m_xWidget
;
65 DropdownToolbarController
* m_pListBoxListener
;
68 ListBoxControl::ListBoxControl(vcl::Window
* pParent
, DropdownToolbarController
* pListBoxListener
)
69 : InterimItemWindow(pParent
, u
"svt/ui/listcontrol.ui"_ustr
, u
"ListControl"_ustr
)
70 , m_xWidget(m_xBuilder
->weld_combo_box(u
"listbox"_ustr
))
71 , m_pListBoxListener( pListBoxListener
)
73 InitControlBase(m_xWidget
.get());
75 m_xWidget
->connect_focus_in(LINK(this, ListBoxControl
, FocusInHdl
));
76 m_xWidget
->connect_focus_out(LINK(this, ListBoxControl
, FocusOutHdl
));
77 m_xWidget
->connect_changed(LINK(this, ListBoxControl
, ModifyHdl
));
78 m_xWidget
->connect_key_press(LINK(this, ListBoxControl
, KeyInputHdl
));
80 m_xWidget
->set_size_request(42, -1); // so a later narrow size request can stick
81 SetSizePixel(get_preferred_size());
84 IMPL_LINK(ListBoxControl
, KeyInputHdl
, const ::KeyEvent
&, rKEvt
, bool)
86 return ChildKeyInput(rKEvt
);
89 ListBoxControl::~ListBoxControl()
94 void ListBoxControl::dispose()
96 m_pListBoxListener
= nullptr;
98 InterimItemWindow::dispose();
101 IMPL_LINK_NOARG(ListBoxControl
, ModifyHdl
, weld::ComboBox
&, void)
103 if (m_pListBoxListener
)
104 m_pListBoxListener
->Select();
107 IMPL_LINK_NOARG(ListBoxControl
, FocusInHdl
, weld::Widget
&, void)
109 if (m_pListBoxListener
)
110 m_pListBoxListener
->GetFocus();
113 IMPL_LINK_NOARG(ListBoxControl
, FocusOutHdl
, weld::Widget
&, void)
115 if (m_pListBoxListener
)
116 m_pListBoxListener
->LoseFocus();
119 DropdownToolbarController::DropdownToolbarController(
120 const Reference
< XComponentContext
>& rxContext
,
121 const Reference
< XFrame
>& rFrame
,
125 const OUString
& aCommand
) :
126 ComplexToolbarController( rxContext
, rFrame
, pToolbar
, nID
, aCommand
)
127 , m_pListBoxControl( nullptr )
129 m_pListBoxControl
= VclPtr
<ListBoxControl
>::Create(m_xToolbar
, this);
133 // ListBoxControl ctor has set a suitable height already
134 auto nHeight
= m_pListBoxControl
->GetSizePixel().Height();
136 m_pListBoxControl
->SetSizePixel( ::Size( nWidth
, nHeight
));
137 m_xToolbar
->SetItemWindow( m_nID
, m_pListBoxControl
);
140 DropdownToolbarController::~DropdownToolbarController()
144 void SAL_CALL
DropdownToolbarController::dispose()
146 SolarMutexGuard aSolarMutexGuard
;
148 m_xToolbar
->SetItemWindow( m_nID
, nullptr );
149 m_pListBoxControl
.disposeAndClear();
151 ComplexToolbarController::dispose();
154 Sequence
<PropertyValue
> DropdownToolbarController::getExecuteArgs(sal_Int16 KeyModifier
) const
156 OUString aSelectedText
= m_pListBoxControl
->get_active_text();
158 // Add key modifier to argument list
159 Sequence
<PropertyValue
> aArgs
{ comphelper::makePropertyValue(u
"KeyModifier"_ustr
, KeyModifier
),
160 comphelper::makePropertyValue(u
"Text"_ustr
, aSelectedText
) };
164 void DropdownToolbarController::Select()
166 if (m_pListBoxControl
->get_count() > 0)
170 void DropdownToolbarController::GetFocus()
175 void DropdownToolbarController::LoseFocus()
180 void DropdownToolbarController::executeControlCommand( const css::frame::ControlCommand
& rControlCommand
)
182 if ( rControlCommand
.Command
== "SetList" )
184 for ( const NamedValue
& rArg
: rControlCommand
.Arguments
)
186 if ( rArg
.Name
== "List" )
188 Sequence
< OUString
> aList
;
189 m_pListBoxControl
->clear();
191 rArg
.Value
>>= aList
;
192 for (OUString
const& rName
: aList
)
193 m_pListBoxControl
->append_text(rName
);
195 m_pListBoxControl
->set_active(0);
198 uno::Sequence
< beans::NamedValue
> aInfo
{ { u
"List"_ustr
, css::uno::Any(aList
) } };
199 addNotifyInfo( u
"ListChanged"_ustr
,
200 getDispatchFromCommand( m_aCommandURL
),
207 else if ( rControlCommand
.Command
== "AddEntry" )
210 for ( const NamedValue
& rArg
: rControlCommand
.Arguments
)
212 if ( rArg
.Name
== "Text" )
214 if ( rArg
.Value
>>= aText
)
215 m_pListBoxControl
->append_text(aText
);
220 else if ( rControlCommand
.Command
== "InsertEntry" )
224 for ( const NamedValue
& rArg
: rControlCommand
.Arguments
)
226 if ( rArg
.Name
== "Pos" )
228 sal_Int32 nTmpPos
= 0;
229 if ( rArg
.Value
>>= nTmpPos
)
231 if (( nTmpPos
>= 0 ) &&
232 ( nTmpPos
< m_pListBoxControl
->get_count() ))
236 else if ( rArg
.Name
== "Text" )
237 rArg
.Value
>>= aText
;
240 m_pListBoxControl
->insert_text(nPos
, aText
);
242 else if ( rControlCommand
.Command
== "RemoveEntryPos" )
244 for ( const NamedValue
& rArg
: rControlCommand
.Arguments
)
246 if ( rArg
.Name
== "Pos" )
248 sal_Int32
nPos( -1 );
249 if ( rArg
.Value
>>= nPos
)
251 if ( 0 <= nPos
&& nPos
< m_pListBoxControl
->get_count() )
252 m_pListBoxControl
->remove(nPos
);
258 else if ( rControlCommand
.Command
== "RemoveEntryText" )
260 for ( const NamedValue
& rArg
: rControlCommand
.Arguments
)
262 if ( rArg
.Name
== "Text" )
265 if ( rArg
.Value
>>= aText
)
267 auto nPos
= m_pListBoxControl
->find_text(aText
);
269 m_pListBoxControl
->remove(nPos
);
279 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */