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::awt
;
32 using namespace css::uno
;
33 using namespace css::beans
;
34 using namespace css::lang
;
35 using namespace css::frame
;
36 using namespace css::util
;
41 // Wrapper class to notify controller about events from ListBox.
42 // Unfortunaltly the events are notified through virtual methods instead
45 class ListBoxControl final
: public InterimItemWindow
48 ListBoxControl(vcl::Window
* pParent
, DropdownToolbarController
* pListBoxListener
);
49 virtual ~ListBoxControl() override
;
50 virtual void dispose() override
;
52 void set_active(int nPos
) { m_xWidget
->set_active(nPos
); }
53 void append_text(const OUString
& rStr
) { m_xWidget
->append_text(rStr
); }
54 void insert_text(int nPos
, const OUString
& rStr
) { m_xWidget
->insert_text(nPos
, rStr
); }
55 int get_count() const { return m_xWidget
->get_count(); }
56 int find_text(const OUString
& rStr
) const { return m_xWidget
->find_text(rStr
); }
57 OUString
get_active_text() const { return m_xWidget
->get_active_text(); }
58 void clear() { return m_xWidget
->clear(); }
59 void remove(int nPos
) { m_xWidget
->remove(nPos
); }
61 DECL_LINK(FocusInHdl
, weld::Widget
&, void);
62 DECL_LINK(FocusOutHdl
, weld::Widget
&, void);
63 DECL_LINK(ModifyHdl
, weld::ComboBox
&, void);
64 DECL_LINK(KeyInputHdl
, const ::KeyEvent
&, bool);
67 std::unique_ptr
<weld::ComboBox
> m_xWidget
;
68 DropdownToolbarController
* m_pListBoxListener
;
71 ListBoxControl::ListBoxControl(vcl::Window
* pParent
, DropdownToolbarController
* pListBoxListener
)
72 : InterimItemWindow(pParent
, "svt/ui/listcontrol.ui", "ListControl")
73 , m_xWidget(m_xBuilder
->weld_combo_box("listbox"))
74 , m_pListBoxListener( pListBoxListener
)
76 InitControlBase(m_xWidget
.get());
78 m_xWidget
->connect_focus_in(LINK(this, ListBoxControl
, FocusInHdl
));
79 m_xWidget
->connect_focus_out(LINK(this, ListBoxControl
, FocusOutHdl
));
80 m_xWidget
->connect_changed(LINK(this, ListBoxControl
, ModifyHdl
));
81 m_xWidget
->connect_key_press(LINK(this, ListBoxControl
, KeyInputHdl
));
83 m_xWidget
->set_size_request(42, -1); // so a later narrow size request can stick
84 SetSizePixel(get_preferred_size());
87 IMPL_LINK(ListBoxControl
, KeyInputHdl
, const ::KeyEvent
&, rKEvt
, bool)
89 return ChildKeyInput(rKEvt
);
92 ListBoxControl::~ListBoxControl()
97 void ListBoxControl::dispose()
99 m_pListBoxListener
= nullptr;
101 InterimItemWindow::dispose();
104 IMPL_LINK_NOARG(ListBoxControl
, ModifyHdl
, weld::ComboBox
&, void)
106 if (m_pListBoxListener
)
107 m_pListBoxListener
->Select();
110 IMPL_LINK_NOARG(ListBoxControl
, FocusInHdl
, weld::Widget
&, void)
112 if (m_pListBoxListener
)
113 m_pListBoxListener
->GetFocus();
116 IMPL_LINK_NOARG(ListBoxControl
, FocusOutHdl
, weld::Widget
&, void)
118 if (m_pListBoxListener
)
119 m_pListBoxListener
->LoseFocus();
122 DropdownToolbarController::DropdownToolbarController(
123 const Reference
< XComponentContext
>& rxContext
,
124 const Reference
< XFrame
>& rFrame
,
128 const OUString
& aCommand
) :
129 ComplexToolbarController( rxContext
, rFrame
, pToolbar
, nID
, aCommand
)
130 , m_pListBoxControl( nullptr )
132 m_pListBoxControl
= VclPtr
<ListBoxControl
>::Create(m_xToolbar
, this);
136 // ListBoxControl ctor has set a suitable height already
137 auto nHeight
= m_pListBoxControl
->GetSizePixel().Height();
139 m_pListBoxControl
->SetSizePixel( ::Size( nWidth
, nHeight
));
140 m_xToolbar
->SetItemWindow( m_nID
, m_pListBoxControl
);
143 DropdownToolbarController::~DropdownToolbarController()
147 void SAL_CALL
DropdownToolbarController::dispose()
149 SolarMutexGuard aSolarMutexGuard
;
151 m_xToolbar
->SetItemWindow( m_nID
, nullptr );
152 m_pListBoxControl
.disposeAndClear();
154 ComplexToolbarController::dispose();
157 Sequence
<PropertyValue
> DropdownToolbarController::getExecuteArgs(sal_Int16 KeyModifier
) const
159 OUString aSelectedText
= m_pListBoxControl
->get_active_text();
161 // Add key modifier to argument list
162 Sequence
<PropertyValue
> aArgs
{ comphelper::makePropertyValue("KeyModifier", KeyModifier
),
163 comphelper::makePropertyValue("Text", aSelectedText
) };
167 void DropdownToolbarController::Select()
169 if (m_pListBoxControl
->get_count() > 0)
173 void DropdownToolbarController::GetFocus()
178 void DropdownToolbarController::LoseFocus()
183 void DropdownToolbarController::executeControlCommand( const css::frame::ControlCommand
& rControlCommand
)
185 if ( rControlCommand
.Command
== "SetList" )
187 for ( const NamedValue
& rArg
: rControlCommand
.Arguments
)
189 if ( rArg
.Name
== "List" )
191 Sequence
< OUString
> aList
;
192 m_pListBoxControl
->clear();
194 rArg
.Value
>>= aList
;
195 for (OUString
const & rName
: std::as_const(aList
))
196 m_pListBoxControl
->append_text(rName
);
198 m_pListBoxControl
->set_active(0);
201 uno::Sequence
< beans::NamedValue
> aInfo
{ { "List", css::uno::Any(aList
) } };
202 addNotifyInfo( "ListChanged",
203 getDispatchFromCommand( m_aCommandURL
),
210 else if ( rControlCommand
.Command
== "AddEntry" )
213 for ( const NamedValue
& rArg
: rControlCommand
.Arguments
)
215 if ( rArg
.Name
== "Text" )
217 if ( rArg
.Value
>>= aText
)
218 m_pListBoxControl
->append_text(aText
);
223 else if ( rControlCommand
.Command
== "InsertEntry" )
227 for ( const NamedValue
& rArg
: rControlCommand
.Arguments
)
229 if ( rArg
.Name
== "Pos" )
231 sal_Int32 nTmpPos
= 0;
232 if ( rArg
.Value
>>= nTmpPos
)
234 if (( nTmpPos
>= 0 ) &&
235 ( nTmpPos
< m_pListBoxControl
->get_count() ))
239 else if ( rArg
.Name
== "Text" )
240 rArg
.Value
>>= aText
;
243 m_pListBoxControl
->insert_text(nPos
, aText
);
245 else if ( rControlCommand
.Command
== "RemoveEntryPos" )
247 for ( const NamedValue
& rArg
: rControlCommand
.Arguments
)
249 if ( rArg
.Name
== "Pos" )
251 sal_Int32
nPos( -1 );
252 if ( rArg
.Value
>>= nPos
)
254 if ( 0 <= nPos
&& nPos
< m_pListBoxControl
->get_count() )
255 m_pListBoxControl
->remove(nPos
);
261 else if ( rControlCommand
.Command
== "RemoveEntryText" )
263 for ( const NamedValue
& rArg
: rControlCommand
.Arguments
)
265 if ( rArg
.Name
== "Text" )
268 if ( rArg
.Value
>>= aText
)
270 auto nPos
= m_pListBoxControl
->find_text(aText
);
272 m_pListBoxControl
->remove(nPos
);
282 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */