tdf#154285 Check upper bound of arguments in SbRtl_Minute function
[LibreOffice.git] / framework / source / uielement / dropdownboxtoolbarcontroller.cxx
blob3537bcc7c9e56f067420a72bfbcd848d94e207dd
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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;
35 namespace framework
38 // Wrapper class to notify controller about events from ListBox.
39 // Unfortunaltly the events are notified through virtual methods instead
40 // of Listeners.
42 class ListBoxControl final : public InterimItemWindow
44 public:
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);
63 private:
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()
91 disposeOnce();
94 void ListBoxControl::dispose()
96 m_pListBoxListener = nullptr;
97 m_xWidget.reset();
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,
122 ToolBox* pToolbar,
123 ToolBoxItemId nID,
124 sal_Int32 nWidth,
125 const OUString& aCommand ) :
126 ComplexToolbarController( rxContext, rFrame, pToolbar, nID, aCommand )
127 , m_pListBoxControl( nullptr )
129 m_pListBoxControl = VclPtr<ListBoxControl>::Create(m_xToolbar, this);
130 if ( nWidth == 0 )
131 nWidth = 100;
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) };
161 return aArgs;
164 void DropdownToolbarController::Select()
166 if (m_pListBoxControl->get_count() > 0)
167 execute(0);
170 void DropdownToolbarController::GetFocus()
172 notifyFocusGet();
175 void DropdownToolbarController::LoseFocus()
177 notifyFocusLost();
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);
197 // send notification
198 uno::Sequence< beans::NamedValue > aInfo { { u"List"_ustr, css::uno::Any(aList) } };
199 addNotifyInfo( u"ListChanged"_ustr,
200 getDispatchFromCommand( m_aCommandURL ),
201 aInfo );
203 break;
207 else if ( rControlCommand.Command == "AddEntry" )
209 OUString aText;
210 for ( const NamedValue& rArg : rControlCommand.Arguments )
212 if ( rArg.Name == "Text" )
214 if ( rArg.Value >>= aText )
215 m_pListBoxControl->append_text(aText);
216 break;
220 else if ( rControlCommand.Command == "InsertEntry" )
222 sal_Int32 nPos(-1);
223 OUString aText;
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() ))
233 nPos = nTmpPos;
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);
254 break;
258 else if ( rControlCommand.Command == "RemoveEntryText" )
260 for ( const NamedValue& rArg : rControlCommand.Arguments )
262 if ( rArg.Name == "Text" )
264 OUString aText;
265 if ( rArg.Value >>= aText )
267 auto nPos = m_pListBoxControl->find_text(aText);
268 if (nPos != -1)
269 m_pListBoxControl->remove(nPos);
271 break;
277 } // namespace
279 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */