LibreOffice Module framework (master) 1
dropdownboxtoolbarcontroller.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::awt;
32using namespace css::uno;
33using namespace css::beans;
34using namespace css::lang;
35using namespace css::frame;
36using namespace css::util;
37
38namespace framework
39{
40
41// Wrapper class to notify controller about events from ListBox.
42// Unfortunaltly the events are notified through virtual methods instead
43// of Listeners.
44
46{
47public:
48 ListBoxControl(vcl::Window* pParent, DropdownToolbarController* pListBoxListener);
49 virtual ~ListBoxControl() override;
50 virtual void dispose() override;
51
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); }
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(KeyInputHdl, const ::KeyEvent&, bool);
65
66private:
67 std::unique_ptr<weld::ComboBox> m_xWidget;
69};
70
72 : InterimItemWindow(pParent, "svt/ui/listcontrol.ui", "ListControl")
73 , m_xWidget(m_xBuilder->weld_combo_box("listbox"))
74 , m_pListBoxListener( pListBoxListener )
75{
77
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));
82
83 m_xWidget->set_size_request(42, -1); // so a later narrow size request can stick
85}
86
87IMPL_LINK(ListBoxControl, KeyInputHdl, const ::KeyEvent&, rKEvt, bool)
88{
89 return ChildKeyInput(rKEvt);
90}
91
93{
95}
96
98{
99 m_pListBoxListener = nullptr;
100 m_xWidget.reset();
102}
103
105{
106 if (m_pListBoxListener)
107 m_pListBoxListener->Select();
108}
109
111{
112 if (m_pListBoxListener)
113 m_pListBoxListener->GetFocus();
114}
115
117{
118 if (m_pListBoxListener)
119 m_pListBoxListener->LoseFocus();
120}
121
123 const Reference< XComponentContext >& rxContext,
124 const Reference< XFrame >& rFrame,
125 ToolBox* pToolbar,
126 ToolBoxItemId nID,
127 sal_Int32 nWidth,
128 const OUString& aCommand ) :
129 ComplexToolbarController( rxContext, rFrame, pToolbar, nID, aCommand )
130 , m_pListBoxControl( nullptr )
131{
133 if ( nWidth == 0 )
134 nWidth = 100;
135
136 // ListBoxControl ctor has set a suitable height already
137 auto nHeight = m_pListBoxControl->GetSizePixel().Height();
138
139 m_pListBoxControl->SetSizePixel( ::Size( nWidth, nHeight ));
140 m_xToolbar->SetItemWindow( m_nID, m_pListBoxControl );
141}
142
144{
145}
146
148{
149 SolarMutexGuard aSolarMutexGuard;
150
151 m_xToolbar->SetItemWindow( m_nID, nullptr );
152 m_pListBoxControl.disposeAndClear();
153
155}
156
157Sequence<PropertyValue> DropdownToolbarController::getExecuteArgs(sal_Int16 KeyModifier) const
158{
159 OUString aSelectedText = m_pListBoxControl->get_active_text();
160
161 // Add key modifier to argument list
162 Sequence<PropertyValue> aArgs{ comphelper::makePropertyValue("KeyModifier", KeyModifier),
163 comphelper::makePropertyValue("Text", aSelectedText) };
164 return aArgs;
165}
166
168{
169 if (m_pListBoxControl->get_count() > 0)
170 execute(0);
171}
172
174{
176}
177
179{
181}
182
183void DropdownToolbarController::executeControlCommand( const css::frame::ControlCommand& rControlCommand )
184{
185 if ( rControlCommand.Command == "SetList" )
186 {
187 for ( const NamedValue& rArg : rControlCommand.Arguments )
188 {
189 if ( rArg.Name == "List" )
190 {
191 Sequence< OUString > aList;
192 m_pListBoxControl->clear();
193
194 rArg.Value >>= aList;
195 for (OUString const & rName : std::as_const(aList))
196 m_pListBoxControl->append_text(rName);
197
198 m_pListBoxControl->set_active(0);
199
200 // send notification
201 uno::Sequence< beans::NamedValue > aInfo { { "List", css::uno::Any(aList) } };
202 addNotifyInfo( "ListChanged",
204 aInfo );
205
206 break;
207 }
208 }
209 }
210 else if ( rControlCommand.Command == "AddEntry" )
211 {
212 OUString aText;
213 for ( const NamedValue& rArg : rControlCommand.Arguments )
214 {
215 if ( rArg.Name == "Text" )
216 {
217 if ( rArg.Value >>= aText )
218 m_pListBoxControl->append_text(aText);
219 break;
220 }
221 }
222 }
223 else if ( rControlCommand.Command == "InsertEntry" )
224 {
225 sal_Int32 nPos(-1);
226 OUString aText;
227 for ( const NamedValue& rArg : rControlCommand.Arguments )
228 {
229 if ( rArg.Name == "Pos" )
230 {
231 sal_Int32 nTmpPos = 0;
232 if ( rArg.Value >>= nTmpPos )
233 {
234 if (( nTmpPos >= 0 ) &&
235 ( nTmpPos < m_pListBoxControl->get_count() ))
236 nPos = nTmpPos;
237 }
238 }
239 else if ( rArg.Name == "Text" )
240 rArg.Value >>= aText;
241 }
242
243 m_pListBoxControl->insert_text(nPos, aText);
244 }
245 else if ( rControlCommand.Command == "RemoveEntryPos" )
246 {
247 for ( const NamedValue& rArg : rControlCommand.Arguments )
248 {
249 if ( rArg.Name == "Pos" )
250 {
251 sal_Int32 nPos( -1 );
252 if ( rArg.Value >>= nPos )
253 {
254 if ( 0 <= nPos && nPos < m_pListBoxControl->get_count() )
255 m_pListBoxControl->remove(nPos);
256 }
257 break;
258 }
259 }
260 }
261 else if ( rControlCommand.Command == "RemoveEntryText" )
262 {
263 for ( const NamedValue& rArg : rControlCommand.Arguments )
264 {
265 if ( rArg.Name == "Text" )
266 {
267 OUString aText;
268 if ( rArg.Value >>= aText )
269 {
270 auto nPos = m_pListBoxControl->find_text(aText);
271 if (nPos != -1)
272 m_pListBoxControl->remove(nPos);
273 }
274 break;
275 }
276 }
277 }
278}
279
280} // namespace
281
282/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
std::unique_ptr< weld::Image > m_xWidget
virtual void dispose() override
void InitControlBase(weld::Widget *pWidget)
static VclPtr< reference_type > Create(Arg &&... arg)
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
DropdownToolbarController(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
virtual css::uno::Sequence< css::beans::PropertyValue > getExecuteArgs(sal_Int16 KeyModifier) const override
DECL_LINK(FocusInHdl, weld::Widget &, void)
DECL_LINK(ModifyHdl, weld::ComboBox &, void)
int find_text(const OUString &rStr) const
DECL_LINK(KeyInputHdl, const ::KeyEvent &, bool)
ListBoxControl(vcl::Window *pParent, DropdownToolbarController *pListBoxListener)
std::unique_ptr< weld::ComboBox > m_xWidget
DECL_LINK(FocusOutHdl, weld::Widget &, void)
void insert_text(int nPos, const OUString &rStr)
void append_text(const OUString &rStr)
DropdownToolbarController * m_pListBoxListener
virtual void SetSizePixel(const Size &rNewSize)
Size get_preferred_size() const
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