/src/libreoffice/framework/source/uielement/comboboxtoolbarcontroller.cxx
Line | Count | Source (jump to first uncovered line) |
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/comboboxtoolbarcontroller.hxx> |
21 | | |
22 | | #include <com/sun/star/beans/PropertyValue.hpp> |
23 | | |
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> |
29 | | |
30 | | using namespace ::com::sun::star; |
31 | | using namespace css::uno; |
32 | | using namespace css::beans; |
33 | | using namespace css::frame; |
34 | | |
35 | | namespace framework |
36 | | { |
37 | | |
38 | | // Wrapper class to notify controller about events from combobox. |
39 | | // Unfortunately the events are notified through virtual methods instead |
40 | | // of Listeners. |
41 | | |
42 | | class ComboBoxControl final : public InterimItemWindow |
43 | | { |
44 | | public: |
45 | | ComboBoxControl(vcl::Window* pParent, ComboboxToolbarController* pComboboxToolbarController); |
46 | | virtual ~ComboBoxControl() override; |
47 | | virtual void dispose() override; |
48 | | |
49 | | void set_active_or_entry_text(const OUString& rText); |
50 | 0 | OUString get_active_text() const { return m_xWidget->get_active_text(); } |
51 | | |
52 | 0 | void clear() { m_xWidget->clear(); } |
53 | 0 | void remove(int nIndex) { m_xWidget->remove(nIndex); } |
54 | 0 | void append_text(const OUString& rStr) { m_xWidget->append_text(rStr); } |
55 | 0 | void insert_text(int pos, const OUString& rStr) { m_xWidget->insert_text(pos, rStr); } |
56 | 0 | int get_count() const { return m_xWidget->get_count(); } |
57 | 0 | int find_text(const OUString& rStr) const { return m_xWidget->find_text(rStr); } |
58 | | |
59 | | DECL_LINK(FocusInHdl, weld::Widget&, void); |
60 | | DECL_LINK(FocusOutHdl, weld::Widget&, void); |
61 | | DECL_LINK(ModifyHdl, weld::ComboBox&, void); |
62 | | DECL_LINK(ActivateHdl, weld::ComboBox&, bool); |
63 | | DECL_LINK(KeyInputHdl, const ::KeyEvent&, bool); |
64 | | |
65 | | private: |
66 | | std::unique_ptr<weld::ComboBox> m_xWidget; |
67 | | ComboboxToolbarController* m_pComboboxToolbarController; |
68 | | }; |
69 | | |
70 | | ComboBoxControl::ComboBoxControl(vcl::Window* pParent, ComboboxToolbarController* pComboboxToolbarController) |
71 | 0 | : InterimItemWindow(pParent, u"svt/ui/combocontrol.ui"_ustr, u"ComboControl"_ustr) |
72 | 0 | , m_xWidget(m_xBuilder->weld_combo_box(u"combobox"_ustr)) |
73 | 0 | , m_pComboboxToolbarController(pComboboxToolbarController) |
74 | 0 | { |
75 | 0 | InitControlBase(m_xWidget.get()); |
76 | |
|
77 | 0 | m_xWidget->connect_focus_in(LINK(this, ComboBoxControl, FocusInHdl)); |
78 | 0 | m_xWidget->connect_focus_out(LINK(this, ComboBoxControl, FocusOutHdl)); |
79 | 0 | m_xWidget->connect_changed(LINK(this, ComboBoxControl, ModifyHdl)); |
80 | 0 | m_xWidget->connect_entry_activate(LINK(this, ComboBoxControl, ActivateHdl)); |
81 | 0 | m_xWidget->connect_key_press(LINK(this, ComboBoxControl, KeyInputHdl)); |
82 | |
|
83 | 0 | m_xWidget->set_entry_width_chars(1); // so a smaller than default width can be used by ComboboxToolbarController |
84 | 0 | SetSizePixel(get_preferred_size()); |
85 | 0 | } Unexecuted instantiation: framework::ComboBoxControl::ComboBoxControl(vcl::Window*, framework::ComboboxToolbarController*) Unexecuted instantiation: framework::ComboBoxControl::ComboBoxControl(vcl::Window*, framework::ComboboxToolbarController*) |
86 | | |
87 | | IMPL_LINK(ComboBoxControl, KeyInputHdl, const ::KeyEvent&, rKEvt, bool) |
88 | 0 | { |
89 | 0 | return ChildKeyInput(rKEvt); |
90 | 0 | } |
91 | | |
92 | | void ComboBoxControl::set_active_or_entry_text(const OUString& rText) |
93 | 0 | { |
94 | 0 | const int nFound = m_xWidget->find_text(rText); |
95 | 0 | if (nFound != -1) |
96 | 0 | m_xWidget->set_active(nFound); |
97 | 0 | else |
98 | 0 | m_xWidget->set_entry_text(rText); |
99 | 0 | } |
100 | | |
101 | | ComboBoxControl::~ComboBoxControl() |
102 | 0 | { |
103 | 0 | disposeOnce(); |
104 | 0 | } |
105 | | |
106 | | void ComboBoxControl::dispose() |
107 | 0 | { |
108 | 0 | m_pComboboxToolbarController = nullptr; |
109 | 0 | m_xWidget.reset(); |
110 | 0 | InterimItemWindow::dispose(); |
111 | 0 | } |
112 | | |
113 | | IMPL_LINK_NOARG(ComboBoxControl, ModifyHdl, weld::ComboBox&, void) |
114 | 0 | { |
115 | 0 | if (m_pComboboxToolbarController) |
116 | 0 | { |
117 | 0 | if (m_xWidget->get_count() && m_xWidget->changed_by_direct_pick()) |
118 | 0 | m_pComboboxToolbarController->Select(); |
119 | 0 | else |
120 | 0 | m_pComboboxToolbarController->Modify(); |
121 | 0 | } |
122 | 0 | } |
123 | | |
124 | | IMPL_LINK_NOARG(ComboBoxControl, FocusInHdl, weld::Widget&, void) |
125 | 0 | { |
126 | 0 | if (m_pComboboxToolbarController) |
127 | 0 | m_pComboboxToolbarController->GetFocus(); |
128 | 0 | } |
129 | | |
130 | | IMPL_LINK_NOARG(ComboBoxControl, FocusOutHdl, weld::Widget&, void) |
131 | 0 | { |
132 | 0 | if (m_pComboboxToolbarController) |
133 | 0 | m_pComboboxToolbarController->LoseFocus(); |
134 | 0 | } |
135 | | |
136 | | IMPL_LINK_NOARG(ComboBoxControl, ActivateHdl, weld::ComboBox&, bool) |
137 | 0 | { |
138 | 0 | if (m_pComboboxToolbarController) |
139 | 0 | m_pComboboxToolbarController->Activate(); |
140 | 0 | return true; |
141 | 0 | } |
142 | | |
143 | | ComboboxToolbarController::ComboboxToolbarController( |
144 | | const Reference< XComponentContext >& rxContext, |
145 | | const Reference< XFrame >& rFrame, |
146 | | ToolBox* pToolbar, |
147 | | ToolBoxItemId nID, |
148 | | sal_Int32 nWidth, |
149 | | const OUString& aCommand ) : |
150 | 0 | ComplexToolbarController( rxContext, rFrame, pToolbar, nID, aCommand ) |
151 | 0 | , m_pComboBox( nullptr ) |
152 | 0 | { |
153 | 0 | m_pComboBox = VclPtr<ComboBoxControl>::Create(m_xToolbar, this); |
154 | 0 | if ( nWidth == 0 ) |
155 | 0 | nWidth = 100; |
156 | | |
157 | | // ComboBoxControl ctor has set a suitable height already |
158 | 0 | auto nHeight = m_pComboBox->GetSizePixel().Height(); |
159 | |
|
160 | 0 | m_pComboBox->SetSizePixel( ::Size( nWidth, nHeight )); |
161 | 0 | m_xToolbar->SetItemWindow( m_nID, m_pComboBox ); |
162 | 0 | } |
163 | | |
164 | | ComboboxToolbarController::~ComboboxToolbarController() |
165 | 0 | { |
166 | 0 | } |
167 | | |
168 | | void SAL_CALL ComboboxToolbarController::dispose() |
169 | 0 | { |
170 | 0 | SolarMutexGuard aSolarMutexGuard; |
171 | |
|
172 | 0 | m_xToolbar->SetItemWindow( m_nID, nullptr ); |
173 | 0 | m_pComboBox.disposeAndClear(); |
174 | |
|
175 | 0 | ComplexToolbarController::dispose(); |
176 | 0 | } |
177 | | |
178 | | Sequence<PropertyValue> ComboboxToolbarController::getExecuteArgs(sal_Int16 KeyModifier) const |
179 | 0 | { |
180 | 0 | OUString aSelectedText = m_pComboBox->get_active_text(); |
181 | | |
182 | | // Add key modifier to argument list |
183 | 0 | Sequence<PropertyValue> aArgs{ comphelper::makePropertyValue(u"KeyModifier"_ustr, KeyModifier), |
184 | 0 | comphelper::makePropertyValue(u"Text"_ustr, aSelectedText) }; |
185 | 0 | return aArgs; |
186 | 0 | } |
187 | | |
188 | | void ComboboxToolbarController::Select() |
189 | 0 | { |
190 | 0 | vcl::Window::PointerState aState = m_pComboBox->GetPointerState(); |
191 | |
|
192 | 0 | sal_uInt16 nKeyModifier = sal_uInt16( aState.mnState & KEY_MODIFIERS_MASK ); |
193 | 0 | execute( nKeyModifier ); |
194 | 0 | } |
195 | | |
196 | | void ComboboxToolbarController::Modify() |
197 | 0 | { |
198 | 0 | notifyTextChanged(m_pComboBox->get_active_text()); |
199 | 0 | } |
200 | | |
201 | | void ComboboxToolbarController::GetFocus() |
202 | 0 | { |
203 | 0 | notifyFocusGet(); |
204 | 0 | } |
205 | | |
206 | | void ComboboxToolbarController::LoseFocus() |
207 | 0 | { |
208 | 0 | notifyFocusLost(); |
209 | 0 | } |
210 | | |
211 | | void ComboboxToolbarController::Activate() |
212 | 0 | { |
213 | | // Call execute only with non-empty text |
214 | 0 | if (!m_pComboBox->get_active_text().isEmpty()) |
215 | 0 | execute(0); |
216 | 0 | } |
217 | | |
218 | | void ComboboxToolbarController::executeControlCommand( const css::frame::ControlCommand& rControlCommand ) |
219 | 0 | { |
220 | 0 | if ( rControlCommand.Command == "SetText" ) |
221 | 0 | { |
222 | 0 | for ( const NamedValue& rArg : rControlCommand.Arguments ) |
223 | 0 | { |
224 | 0 | if ( rArg.Name == "Text" ) |
225 | 0 | { |
226 | 0 | OUString aText; |
227 | 0 | rArg.Value >>= aText; |
228 | 0 | m_pComboBox->set_active_or_entry_text(aText); |
229 | | |
230 | | // send notification |
231 | 0 | notifyTextChanged( aText ); |
232 | 0 | break; |
233 | 0 | } |
234 | 0 | } |
235 | 0 | } |
236 | 0 | else if ( rControlCommand.Command == "SetList" ) |
237 | 0 | { |
238 | 0 | for ( const NamedValue& rArg : rControlCommand.Arguments ) |
239 | 0 | { |
240 | 0 | if ( rArg.Name == "List" ) |
241 | 0 | { |
242 | 0 | Sequence< OUString > aList; |
243 | 0 | m_pComboBox->clear(); |
244 | |
|
245 | 0 | rArg.Value >>= aList; |
246 | 0 | for (OUString const& rName : aList) |
247 | 0 | m_pComboBox->append_text(rName); |
248 | | |
249 | | // send notification |
250 | 0 | uno::Sequence< beans::NamedValue > aInfo { { u"List"_ustr, css::uno::Any(aList) } }; |
251 | 0 | addNotifyInfo( u"ListChanged"_ustr, |
252 | 0 | getDispatchFromCommand( m_aCommandURL ), |
253 | 0 | aInfo ); |
254 | |
|
255 | 0 | break; |
256 | 0 | } |
257 | 0 | } |
258 | 0 | } |
259 | 0 | else if ( rControlCommand.Command == "AddEntry" ) |
260 | 0 | { |
261 | 0 | OUString aText; |
262 | 0 | for ( const NamedValue& rArg : rControlCommand.Arguments ) |
263 | 0 | { |
264 | 0 | if ( rArg.Name == "Text" ) |
265 | 0 | { |
266 | 0 | if ( rArg.Value >>= aText ) |
267 | 0 | m_pComboBox->append_text(aText); |
268 | 0 | break; |
269 | 0 | } |
270 | 0 | } |
271 | 0 | } |
272 | 0 | else if ( rControlCommand.Command == "InsertEntry" ) |
273 | 0 | { |
274 | 0 | sal_Int32 nPos(-1); |
275 | 0 | OUString aText; |
276 | 0 | for ( const NamedValue& rArg : rControlCommand.Arguments ) |
277 | 0 | { |
278 | 0 | if ( rArg.Name == "Pos" ) |
279 | 0 | { |
280 | 0 | sal_Int32 nTmpPos = 0; |
281 | 0 | if ( rArg.Value >>= nTmpPos ) |
282 | 0 | { |
283 | 0 | if (( nTmpPos >= 0 ) && |
284 | 0 | ( nTmpPos < m_pComboBox->get_count() )) |
285 | 0 | nPos = nTmpPos; |
286 | 0 | } |
287 | 0 | } |
288 | 0 | else if ( rArg.Name == "Text" ) |
289 | 0 | rArg.Value >>= aText; |
290 | 0 | } |
291 | |
|
292 | 0 | m_pComboBox->insert_text(nPos, aText); |
293 | 0 | } |
294 | 0 | else if ( rControlCommand.Command == "RemoveEntryPos" ) |
295 | 0 | { |
296 | 0 | for ( const NamedValue& rArg : rControlCommand.Arguments ) |
297 | 0 | { |
298 | 0 | if ( rArg.Name == "Pos" ) |
299 | 0 | { |
300 | 0 | sal_Int32 nPos( -1 ); |
301 | 0 | if ( rArg.Value >>= nPos ) |
302 | 0 | { |
303 | 0 | if (0 <= nPos && nPos < m_pComboBox->get_count()) |
304 | 0 | m_pComboBox->remove(nPos); |
305 | 0 | } |
306 | 0 | break; |
307 | 0 | } |
308 | 0 | } |
309 | 0 | } |
310 | 0 | else if ( rControlCommand.Command == "RemoveEntryText" ) |
311 | 0 | { |
312 | 0 | for ( const NamedValue& rArg : rControlCommand.Arguments ) |
313 | 0 | { |
314 | 0 | if ( rArg.Name == "Text") |
315 | 0 | { |
316 | 0 | OUString aText; |
317 | 0 | if ( rArg.Value >>= aText ) |
318 | 0 | { |
319 | 0 | auto nPos = m_pComboBox->find_text(aText); |
320 | 0 | if (nPos != -1) |
321 | 0 | m_pComboBox->remove(nPos); |
322 | 0 | } |
323 | 0 | break; |
324 | 0 | } |
325 | 0 | } |
326 | 0 | } |
327 | 0 | } |
328 | | |
329 | | } // namespace |
330 | | |
331 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |