/src/libreoffice/framework/source/uielement/dropdownboxtoolbarcontroller.cxx
Line | Count | Source |
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 | | |
20 | | #include <uielement/dropdownboxtoolbarcontroller.hxx> |
21 | | |
22 | | #include <com/sun/star/beans/PropertyValue.hpp> |
23 | | |
24 | | #include <comphelper/propertyvalue.hxx> |
25 | | #include <comphelper/unique_unlock.hxx> |
26 | | #include <vcl/InterimItemWindow.hxx> |
27 | | #include <svtools/toolboxcontroller.hxx> |
28 | | #include <vcl/svapp.hxx> |
29 | | #include <vcl/toolbox.hxx> |
30 | | #include <vcl/weld/Builder.hxx> |
31 | | #include <vcl/weld/ComboBox.hxx> |
32 | | |
33 | | using namespace ::com::sun::star; |
34 | | using namespace css::uno; |
35 | | using namespace css::beans; |
36 | | using namespace css::frame; |
37 | | |
38 | | namespace framework |
39 | | { |
40 | | |
41 | | // Wrapper class to notify controller about events from ListBox. |
42 | | // Unfortunately the events are notified through virtual methods instead |
43 | | // of Listeners. |
44 | | |
45 | | class ListBoxControl final : public InterimItemWindow |
46 | | { |
47 | | public: |
48 | | ListBoxControl(vcl::Window* pParent, DropdownToolbarController* pListBoxListener); |
49 | | virtual ~ListBoxControl() override; |
50 | | virtual void dispose() override; |
51 | | |
52 | 0 | void set_active(int nPos) { m_xWidget->set_active(nPos); } |
53 | 0 | void append_text(const OUString& rStr) { m_xWidget->append_text(rStr); } |
54 | 0 | void insert_text(int nPos, const OUString& rStr) { m_xWidget->insert_text(nPos, rStr); } |
55 | 0 | int get_count() const { return m_xWidget->get_count(); } |
56 | 0 | int find_text(const OUString& rStr) const { return m_xWidget->find_text(rStr); } |
57 | 0 | OUString get_active_text() const { return m_xWidget->get_active_text(); } |
58 | 0 | void clear() { return m_xWidget->clear(); } |
59 | 0 | 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 | | |
66 | | private: |
67 | | std::unique_ptr<weld::ComboBox> m_xWidget; |
68 | | DropdownToolbarController* m_pListBoxListener; |
69 | | }; |
70 | | |
71 | | ListBoxControl::ListBoxControl(vcl::Window* pParent, DropdownToolbarController* pListBoxListener) |
72 | 0 | : InterimItemWindow(pParent, u"svt/ui/listcontrol.ui"_ustr, u"ListControl"_ustr) |
73 | 0 | , m_xWidget(m_xBuilder->weld_combo_box(u"listbox"_ustr)) |
74 | 0 | , m_pListBoxListener( pListBoxListener ) |
75 | 0 | { |
76 | 0 | InitControlBase(m_xWidget.get()); |
77 | |
|
78 | 0 | m_xWidget->connect_focus_in(LINK(this, ListBoxControl, FocusInHdl)); |
79 | 0 | m_xWidget->connect_focus_out(LINK(this, ListBoxControl, FocusOutHdl)); |
80 | 0 | m_xWidget->connect_changed(LINK(this, ListBoxControl, ModifyHdl)); |
81 | 0 | m_xWidget->connect_key_press(LINK(this, ListBoxControl, KeyInputHdl)); |
82 | |
|
83 | 0 | m_xWidget->set_size_request(42, -1); // so a later narrow size request can stick |
84 | 0 | SetSizePixel(get_preferred_size()); |
85 | 0 | } Unexecuted instantiation: framework::ListBoxControl::ListBoxControl(vcl::Window*, framework::DropdownToolbarController*) Unexecuted instantiation: framework::ListBoxControl::ListBoxControl(vcl::Window*, framework::DropdownToolbarController*) |
86 | | |
87 | | IMPL_LINK(ListBoxControl, KeyInputHdl, const ::KeyEvent&, rKEvt, bool) |
88 | 0 | { |
89 | 0 | return ChildKeyInput(rKEvt); |
90 | 0 | } |
91 | | |
92 | | ListBoxControl::~ListBoxControl() |
93 | 0 | { |
94 | 0 | disposeOnce(); |
95 | 0 | } |
96 | | |
97 | | void ListBoxControl::dispose() |
98 | 0 | { |
99 | 0 | m_pListBoxListener = nullptr; |
100 | 0 | m_xWidget.reset(); |
101 | 0 | InterimItemWindow::dispose(); |
102 | 0 | } |
103 | | |
104 | | IMPL_LINK_NOARG(ListBoxControl, ModifyHdl, weld::ComboBox&, void) |
105 | 0 | { |
106 | 0 | if (m_pListBoxListener) |
107 | 0 | m_pListBoxListener->Select(); |
108 | 0 | } |
109 | | |
110 | | IMPL_LINK_NOARG(ListBoxControl, FocusInHdl, weld::Widget&, void) |
111 | 0 | { |
112 | 0 | if (m_pListBoxListener) |
113 | 0 | m_pListBoxListener->GetFocus(); |
114 | 0 | } |
115 | | |
116 | | IMPL_LINK_NOARG(ListBoxControl, FocusOutHdl, weld::Widget&, void) |
117 | 0 | { |
118 | 0 | if (m_pListBoxListener) |
119 | 0 | m_pListBoxListener->LoseFocus(); |
120 | 0 | } |
121 | | |
122 | | DropdownToolbarController::DropdownToolbarController( |
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 | 0 | ComplexToolbarController( rxContext, rFrame, pToolbar, nID, aCommand ) |
130 | 0 | , m_pListBoxControl( nullptr ) |
131 | 0 | { |
132 | 0 | m_pListBoxControl = VclPtr<ListBoxControl>::Create(m_xToolbar, this); |
133 | 0 | if ( nWidth == 0 ) |
134 | 0 | nWidth = 100; |
135 | | |
136 | | // ListBoxControl ctor has set a suitable height already |
137 | 0 | auto nHeight = m_pListBoxControl->GetSizePixel().Height(); |
138 | |
|
139 | 0 | m_pListBoxControl->SetSizePixel( ::Size( nWidth, nHeight )); |
140 | 0 | m_xToolbar->SetItemWindow( m_nID, m_pListBoxControl ); |
141 | 0 | } Unexecuted instantiation: framework::DropdownToolbarController::DropdownToolbarController(com::sun::star::uno::Reference<com::sun::star::uno::XComponentContext> const&, com::sun::star::uno::Reference<com::sun::star::frame::XFrame> const&, ToolBox*, o3tl::strong_int<unsigned short, ToolBoxItemIdTag>, int, rtl::OUString const&) Unexecuted instantiation: framework::DropdownToolbarController::DropdownToolbarController(com::sun::star::uno::Reference<com::sun::star::uno::XComponentContext> const&, com::sun::star::uno::Reference<com::sun::star::frame::XFrame> const&, ToolBox*, o3tl::strong_int<unsigned short, ToolBoxItemIdTag>, int, rtl::OUString const&) |
142 | | |
143 | | DropdownToolbarController::~DropdownToolbarController() |
144 | 0 | { |
145 | 0 | } |
146 | | |
147 | | void DropdownToolbarController::disposing(std::unique_lock<std::mutex>& rGuard) |
148 | 0 | { |
149 | 0 | { |
150 | 0 | comphelper::unique_unlock aUnlock(rGuard); |
151 | 0 | SolarMutexGuard aSolarMutexGuard; |
152 | 0 | m_xToolbar->SetItemWindow( m_nID, nullptr ); |
153 | 0 | m_pListBoxControl.disposeAndClear(); |
154 | 0 | } |
155 | 0 | ComplexToolbarController::disposing(rGuard); |
156 | 0 | } |
157 | | |
158 | | Sequence<PropertyValue> DropdownToolbarController::getExecuteArgs(sal_Int16 KeyModifier) const |
159 | 0 | { |
160 | 0 | OUString aSelectedText = m_pListBoxControl->get_active_text(); |
161 | | |
162 | | // Add key modifier to argument list |
163 | 0 | Sequence<PropertyValue> aArgs{ comphelper::makePropertyValue(u"KeyModifier"_ustr, KeyModifier), |
164 | 0 | comphelper::makePropertyValue(u"Text"_ustr, aSelectedText) }; |
165 | 0 | return aArgs; |
166 | 0 | } |
167 | | |
168 | | void DropdownToolbarController::Select() |
169 | 0 | { |
170 | 0 | if (m_pListBoxControl->get_count() > 0) |
171 | 0 | execute(0); |
172 | 0 | } |
173 | | |
174 | | void DropdownToolbarController::GetFocus() |
175 | 0 | { |
176 | 0 | notifyFocusGet(); |
177 | 0 | } |
178 | | |
179 | | void DropdownToolbarController::LoseFocus() |
180 | 0 | { |
181 | 0 | notifyFocusLost(); |
182 | 0 | } |
183 | | |
184 | | void DropdownToolbarController::executeControlCommand( const css::frame::ControlCommand& rControlCommand ) |
185 | 0 | { |
186 | 0 | if ( rControlCommand.Command == "SetList" ) |
187 | 0 | { |
188 | 0 | for ( const NamedValue& rArg : rControlCommand.Arguments ) |
189 | 0 | { |
190 | 0 | if ( rArg.Name == "List" ) |
191 | 0 | { |
192 | 0 | Sequence< OUString > aList; |
193 | 0 | m_pListBoxControl->clear(); |
194 | |
|
195 | 0 | rArg.Value >>= aList; |
196 | 0 | for (OUString const& rName : aList) |
197 | 0 | m_pListBoxControl->append_text(rName); |
198 | |
|
199 | 0 | m_pListBoxControl->set_active(0); |
200 | | |
201 | | // send notification |
202 | 0 | uno::Sequence< beans::NamedValue > aInfo { { u"List"_ustr, css::uno::Any(aList) } }; |
203 | 0 | addNotifyInfo( u"ListChanged"_ustr, |
204 | 0 | getDispatchFromCommand( m_aCommandURL ), |
205 | 0 | aInfo ); |
206 | |
|
207 | 0 | break; |
208 | 0 | } |
209 | 0 | } |
210 | 0 | } |
211 | 0 | else if ( rControlCommand.Command == "AddEntry" ) |
212 | 0 | { |
213 | 0 | OUString aText; |
214 | 0 | for ( const NamedValue& rArg : rControlCommand.Arguments ) |
215 | 0 | { |
216 | 0 | if ( rArg.Name == "Text" ) |
217 | 0 | { |
218 | 0 | if ( rArg.Value >>= aText ) |
219 | 0 | m_pListBoxControl->append_text(aText); |
220 | 0 | break; |
221 | 0 | } |
222 | 0 | } |
223 | 0 | } |
224 | 0 | else if ( rControlCommand.Command == "InsertEntry" ) |
225 | 0 | { |
226 | 0 | sal_Int32 nPos(-1); |
227 | 0 | OUString aText; |
228 | 0 | for ( const NamedValue& rArg : rControlCommand.Arguments ) |
229 | 0 | { |
230 | 0 | if ( rArg.Name == "Pos" ) |
231 | 0 | { |
232 | 0 | sal_Int32 nTmpPos = 0; |
233 | 0 | if ( rArg.Value >>= nTmpPos ) |
234 | 0 | { |
235 | 0 | if (( nTmpPos >= 0 ) && |
236 | 0 | ( nTmpPos < m_pListBoxControl->get_count() )) |
237 | 0 | nPos = nTmpPos; |
238 | 0 | } |
239 | 0 | } |
240 | 0 | else if ( rArg.Name == "Text" ) |
241 | 0 | rArg.Value >>= aText; |
242 | 0 | } |
243 | |
|
244 | 0 | m_pListBoxControl->insert_text(nPos, aText); |
245 | 0 | } |
246 | 0 | else if ( rControlCommand.Command == "RemoveEntryPos" ) |
247 | 0 | { |
248 | 0 | for ( const NamedValue& rArg : rControlCommand.Arguments ) |
249 | 0 | { |
250 | 0 | if ( rArg.Name == "Pos" ) |
251 | 0 | { |
252 | 0 | sal_Int32 nPos( -1 ); |
253 | 0 | if ( rArg.Value >>= nPos ) |
254 | 0 | { |
255 | 0 | if ( 0 <= nPos && nPos < m_pListBoxControl->get_count() ) |
256 | 0 | m_pListBoxControl->remove(nPos); |
257 | 0 | } |
258 | 0 | break; |
259 | 0 | } |
260 | 0 | } |
261 | 0 | } |
262 | 0 | else if ( rControlCommand.Command == "RemoveEntryText" ) |
263 | 0 | { |
264 | 0 | for ( const NamedValue& rArg : rControlCommand.Arguments ) |
265 | 0 | { |
266 | 0 | if ( rArg.Name == "Text" ) |
267 | 0 | { |
268 | 0 | OUString aText; |
269 | 0 | if ( rArg.Value >>= aText ) |
270 | 0 | { |
271 | 0 | auto nPos = m_pListBoxControl->find_text(aText); |
272 | 0 | if (nPos != -1) |
273 | 0 | m_pListBoxControl->remove(nPos); |
274 | 0 | } |
275 | 0 | break; |
276 | 0 | } |
277 | 0 | } |
278 | 0 | } |
279 | 0 | } |
280 | | |
281 | | } // namespace |
282 | | |
283 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |