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 <sal/config.h>
21
22 #include <stdio.h>
23
24 #include <uielement/spinfieldtoolbarcontroller.hxx>
25
26 #include <com/sun/star/beans/PropertyValue.hpp>
27
28 #include <svtools/toolboxcontroller.hxx>
29 #include <vcl/InterimItemWindow.hxx>
30 #include <vcl/event.hxx>
31 #include <vcl/formatter.hxx>
32 #include <vcl/svapp.hxx>
33 #include <vcl/toolbox.hxx>
34 #include <o3tl/char16_t2wchar_t.hxx>
35
36 using namespace ::com::sun::star;
37 using namespace ::com::sun::star::uno;
38 using namespace ::com::sun::star::beans;
39 using namespace ::com::sun::star::lang;
40 using namespace ::com::sun::star::frame;
41 using namespace ::com::sun::star::util;
42
43 namespace framework
44 {
45
46 // Wrapper class to notify controller about events from combobox.
47 // Unfortunaltly the events are notified through virtual methods instead
48 // of Listeners.
49
50 class SpinfieldControl final : public InterimItemWindow
51 {
52 public:
53 SpinfieldControl(vcl::Window* pParent, SpinfieldToolbarController* pSpinfieldToolbarController);
54 virtual ~SpinfieldControl() override;
55 virtual void dispose() override;
56
GetFormatter()57 Formatter& GetFormatter()
58 {
59 return m_xWidget->GetFormatter();
60 }
61
get_entry_text() const62 OUString get_entry_text() const { return m_xWidget->get_text(); }
63
64 DECL_LINK(ValueChangedHdl, weld::FormattedSpinButton&, void);
65 DECL_LINK(FormatOutputHdl, LinkParamNone*, bool);
66 DECL_LINK(ParseInputHdl, sal_Int64*, TriState);
67 DECL_LINK(ModifyHdl, weld::Entry&, void);
68 DECL_LINK(ActivateHdl, weld::Entry&, bool);
69 DECL_LINK(FocusInHdl, weld::Widget&, void);
70 DECL_LINK(FocusOutHdl, weld::Widget&, void);
71 DECL_LINK(KeyInputHdl, const ::KeyEvent&, bool);
72
73 private:
74 std::unique_ptr<weld::FormattedSpinButton> m_xWidget;
75 SpinfieldToolbarController* m_pSpinfieldToolbarController;
76 };
77
SpinfieldControl(vcl::Window * pParent,SpinfieldToolbarController * pSpinfieldToolbarController)78 SpinfieldControl::SpinfieldControl(vcl::Window* pParent, SpinfieldToolbarController* pSpinfieldToolbarController)
79 : InterimItemWindow(pParent, "svt/ui/spinfieldcontrol.ui", "SpinFieldControl")
80 , m_xWidget(m_xBuilder->weld_formatted_spin_button("spinbutton"))
81 , m_pSpinfieldToolbarController(pSpinfieldToolbarController)
82 {
83 InitControlBase(m_xWidget.get());
84
85 m_xWidget->connect_focus_in(LINK(this, SpinfieldControl, FocusInHdl));
86 m_xWidget->connect_focus_out(LINK(this, SpinfieldControl, FocusOutHdl));
87 Formatter& rFormatter = m_xWidget->GetFormatter();
88 rFormatter.SetOutputHdl(LINK(this, SpinfieldControl, FormatOutputHdl));
89 rFormatter.SetInputHdl(LINK(this, SpinfieldControl, ParseInputHdl));
90 m_xWidget->connect_value_changed(LINK(this, SpinfieldControl, ValueChangedHdl));
91 m_xWidget->connect_changed(LINK(this, SpinfieldControl, ModifyHdl));
92 m_xWidget->connect_activate(LINK(this, SpinfieldControl, ActivateHdl));
93 m_xWidget->connect_key_press(LINK(this, SpinfieldControl, KeyInputHdl));
94
95 // so a later narrow size request can stick
96 m_xWidget->set_width_chars(3);
97 m_xWidget->set_size_request(42, -1);
98
99 SetSizePixel(get_preferred_size());
100 }
101
IMPL_LINK(SpinfieldControl,KeyInputHdl,const::KeyEvent &,rKEvt,bool)102 IMPL_LINK(SpinfieldControl, KeyInputHdl, const ::KeyEvent&, rKEvt, bool)
103 {
104 return ChildKeyInput(rKEvt);
105 }
106
IMPL_LINK(SpinfieldControl,ParseInputHdl,sal_Int64 *,result,TriState)107 IMPL_LINK(SpinfieldControl, ParseInputHdl, sal_Int64*, result, TriState)
108 {
109 *result = m_xWidget->get_text().toDouble() * weld::SpinButton::Power10(m_xWidget->GetFormatter().GetDecimalDigits());
110 return TRISTATE_TRUE;
111 }
112
~SpinfieldControl()113 SpinfieldControl::~SpinfieldControl()
114 {
115 disposeOnce();
116 }
117
dispose()118 void SpinfieldControl::dispose()
119 {
120 m_pSpinfieldToolbarController = nullptr;
121 m_xWidget.reset();
122 InterimItemWindow::dispose();
123 }
124
IMPL_LINK_NOARG(SpinfieldControl,ValueChangedHdl,weld::FormattedSpinButton &,void)125 IMPL_LINK_NOARG(SpinfieldControl, ValueChangedHdl, weld::FormattedSpinButton&, void)
126 {
127 if (m_pSpinfieldToolbarController)
128 m_pSpinfieldToolbarController->execute(0);
129 }
130
IMPL_LINK_NOARG(SpinfieldControl,ModifyHdl,weld::Entry &,void)131 IMPL_LINK_NOARG(SpinfieldControl, ModifyHdl, weld::Entry&, void)
132 {
133 if (m_pSpinfieldToolbarController)
134 m_pSpinfieldToolbarController->Modify();
135 }
136
IMPL_LINK_NOARG(SpinfieldControl,FocusInHdl,weld::Widget &,void)137 IMPL_LINK_NOARG(SpinfieldControl, FocusInHdl, weld::Widget&, void)
138 {
139 if (m_pSpinfieldToolbarController)
140 m_pSpinfieldToolbarController->GetFocus();
141 }
142
IMPL_LINK_NOARG(SpinfieldControl,FocusOutHdl,weld::Widget &,void)143 IMPL_LINK_NOARG(SpinfieldControl, FocusOutHdl, weld::Widget&, void)
144 {
145 if (m_pSpinfieldToolbarController)
146 m_pSpinfieldToolbarController->LoseFocus();
147 }
148
IMPL_LINK_NOARG(SpinfieldControl,ActivateHdl,weld::Entry &,bool)149 IMPL_LINK_NOARG(SpinfieldControl, ActivateHdl, weld::Entry&, bool)
150 {
151 bool bConsumed = false;
152 if (m_pSpinfieldToolbarController)
153 {
154 m_pSpinfieldToolbarController->Activate();
155 bConsumed = true;
156 }
157 return bConsumed;
158 }
159
IMPL_LINK_NOARG(SpinfieldControl,FormatOutputHdl,LinkParamNone *,bool)160 IMPL_LINK_NOARG(SpinfieldControl, FormatOutputHdl, LinkParamNone*, bool)
161 {
162 OUString aText = m_pSpinfieldToolbarController->FormatOutputString(m_xWidget->GetFormatter().GetValue());
163 m_xWidget->set_text(aText);
164 return true;
165 }
166
SpinfieldToolbarController(const Reference<XComponentContext> & rxContext,const Reference<XFrame> & rFrame,ToolBox * pToolbar,ToolBoxItemId nID,sal_Int32 nWidth,const OUString & aCommand)167 SpinfieldToolbarController::SpinfieldToolbarController(
168 const Reference< XComponentContext >& rxContext,
169 const Reference< XFrame >& rFrame,
170 ToolBox* pToolbar,
171 ToolBoxItemId nID,
172 sal_Int32 nWidth,
173 const OUString& aCommand ) :
174 ComplexToolbarController( rxContext, rFrame, pToolbar, nID, aCommand )
175 , m_bFloat( false )
176 , m_nMax( 0.0 )
177 , m_nMin( 0.0 )
178 , m_nValue( 0.0 )
179 , m_nStep( 0.0 )
180 , m_pSpinfieldControl( nullptr )
181 {
182 m_pSpinfieldControl = VclPtr<SpinfieldControl>::Create(m_xToolbar, this);
183 if ( nWidth == 0 )
184 nWidth = 100;
185
186 // SpinFieldControl ctor has set a suitable height already
187 auto nHeight = m_pSpinfieldControl->GetSizePixel().Height();
188
189 m_pSpinfieldControl->SetSizePixel( ::Size( nWidth, nHeight ));
190 m_xToolbar->SetItemWindow( m_nID, m_pSpinfieldControl );
191 }
192
~SpinfieldToolbarController()193 SpinfieldToolbarController::~SpinfieldToolbarController()
194 {
195 }
196
dispose()197 void SAL_CALL SpinfieldToolbarController::dispose()
198 {
199 SolarMutexGuard aSolarMutexGuard;
200
201 m_xToolbar->SetItemWindow( m_nID, nullptr );
202 m_pSpinfieldControl.disposeAndClear();
203
204 ComplexToolbarController::dispose();
205 }
206
getExecuteArgs(sal_Int16 KeyModifier) const207 Sequence<PropertyValue> SpinfieldToolbarController::getExecuteArgs(sal_Int16 KeyModifier) const
208 {
209 Sequence<PropertyValue> aArgs( 2 );
210 OUString aSpinfieldText = m_pSpinfieldControl->get_entry_text();
211
212 // Add key modifier to argument list
213 aArgs[0].Name = "KeyModifier";
214 aArgs[0].Value <<= KeyModifier;
215 aArgs[1].Name = "Value";
216 if ( m_bFloat )
217 aArgs[1].Value <<= aSpinfieldText.toDouble();
218 else
219 aArgs[1].Value <<= aSpinfieldText.toInt32();
220 return aArgs;
221 }
222
Modify()223 void SpinfieldToolbarController::Modify()
224 {
225 notifyTextChanged(m_pSpinfieldControl->get_entry_text());
226 }
227
GetFocus()228 void SpinfieldToolbarController::GetFocus()
229 {
230 notifyFocusGet();
231 }
232
LoseFocus()233 void SpinfieldToolbarController::LoseFocus()
234 {
235 notifyFocusLost();
236 }
237
Activate()238 void SpinfieldToolbarController::Activate()
239 {
240 // Call execute only with non-empty text
241 if (!m_pSpinfieldControl->get_entry_text().isEmpty())
242 execute(0);
243 }
244
executeControlCommand(const css::frame::ControlCommand & rControlCommand)245 void SpinfieldToolbarController::executeControlCommand( const css::frame::ControlCommand& rControlCommand )
246 {
247 OUString aValue;
248 OUString aMax;
249 OUString aMin;
250 OUString aStep;
251 bool bFloatValue( false );
252
253 if ( rControlCommand.Command == "SetStep" )
254 {
255 for ( auto const & arg : rControlCommand.Arguments )
256 {
257 if ( arg.Name == "Step" )
258 {
259 sal_Int32 nValue;
260 double fValue;
261 bool bFloat( false );
262 if ( impl_getValue( arg.Value, nValue, fValue, bFloat ))
263 aStep = bFloat ? OUString( OUString::number( fValue )) :
264 OUString( OUString::number( nValue ));
265 break;
266 }
267 }
268 }
269 else if ( rControlCommand.Command == "SetValue" )
270 {
271 for ( auto const & arg : rControlCommand.Arguments )
272 {
273 if ( arg.Name == "Value" )
274 {
275 sal_Int32 nValue;
276 double fValue;
277 bool bFloat( false );
278
279 if ( impl_getValue( arg.Value, nValue, fValue, bFloat ))
280 {
281 aValue = bFloat ? OUString( OUString::number( fValue )) :
282 OUString( OUString::number( nValue ));
283 bFloatValue = bFloat;
284 }
285 break;
286 }
287 }
288 }
289 else if ( rControlCommand.Command == "SetValues" )
290 {
291 for ( auto const & arg : rControlCommand.Arguments )
292 {
293 sal_Int32 nValue;
294 double fValue;
295 bool bFloat( false );
296
297 OUString aName = arg.Name;
298 if ( impl_getValue( arg.Value, nValue, fValue, bFloat ))
299 {
300 if ( aName == "Value" )
301 {
302 aValue = bFloat ? OUString( OUString::number( fValue )) :
303 OUString( OUString::number( nValue ));
304 bFloatValue = bFloat;
305 }
306 else if ( aName == "Step" )
307 aStep = bFloat ? OUString( OUString::number( fValue )) :
308 OUString( OUString::number( nValue ));
309 else if ( aName == "LowerLimit" )
310 aMin = bFloat ? OUString( OUString::number( fValue )) :
311 OUString( OUString::number( nValue ));
312 else if ( aName == "UpperLimit" )
313 aMax = bFloat ? OUString( OUString::number( fValue )) :
314 OUString( OUString::number( nValue ));
315 }
316 else if ( aName == "OutputFormat" )
317 arg.Value >>= m_aOutFormat;
318 }
319 }
320 else if ( rControlCommand.Command == "SetLowerLimit" )
321 {
322 for ( auto const & arg : rControlCommand.Arguments )
323 {
324 if ( arg.Name == "LowerLimit" )
325 {
326 sal_Int32 nValue;
327 double fValue;
328 bool bFloat( false );
329 if ( impl_getValue( arg.Value, nValue, fValue, bFloat ))
330 aMin = bFloat ? OUString( OUString::number( fValue )) :
331 OUString( OUString::number( nValue ));
332 break;
333 }
334 }
335 }
336 else if ( rControlCommand.Command == "SetUpperLimit" )
337 {
338 for ( auto const & arg : rControlCommand.Arguments )
339 {
340 if ( arg.Name == "UpperLimit" )
341 {
342 sal_Int32 nValue;
343 double fValue;
344 bool bFloat( false );
345 if ( impl_getValue( arg.Value, nValue, fValue, bFloat ))
346 aMax = bFloat ? OUString( OUString::number( fValue )) :
347 OUString( OUString::number( nValue ));
348 break;
349 }
350 }
351 }
352 else if ( rControlCommand.Command == "SetOutputFormat" )
353 {
354 for ( auto const & arg : rControlCommand.Arguments )
355 {
356 if ( arg.Name == "OutputFormat" )
357 {
358 arg.Value >>= m_aOutFormat;
359 break;
360 }
361 }
362 }
363
364 Formatter& rFormatter = m_pSpinfieldControl->GetFormatter();
365
366 // Check values and set members
367 if (bFloatValue)
368 rFormatter.SetDecimalDigits(2);
369 if ( !aValue.isEmpty() )
370 {
371 m_bFloat = bFloatValue;
372 m_nValue = aValue.toDouble();
373 rFormatter.SetValue(m_nValue);
374 }
375 if ( !aMax.isEmpty() )
376 {
377 m_nMax = aMax.toDouble();
378 rFormatter.SetMaxValue(m_nMax);
379 }
380 if ( !aMin.isEmpty() )
381 {
382 m_nMin = aMin.toDouble();
383 rFormatter.SetMinValue(m_nMin);
384 }
385 if ( !aStep.isEmpty() )
386 {
387 m_nStep = aStep.toDouble();
388 rFormatter.SetSpinSize(m_nStep);
389 }
390 }
391
impl_getValue(const Any & rAny,sal_Int32 & nValue,double & fValue,bool & bFloat)392 bool SpinfieldToolbarController::impl_getValue(
393 const Any& rAny, sal_Int32& nValue, double& fValue, bool& bFloat )
394 {
395 using ::com::sun::star::uno::TypeClass;
396
397 bool bValueValid( false );
398
399 bFloat = false;
400 TypeClass aTypeClass = rAny.getValueTypeClass();
401 if (( aTypeClass == TypeClass( typelib_TypeClass_LONG )) ||
402 ( aTypeClass == TypeClass( typelib_TypeClass_SHORT )) ||
403 ( aTypeClass == TypeClass( typelib_TypeClass_BYTE )))
404 bValueValid = rAny >>= nValue;
405 else if (( aTypeClass == TypeClass( typelib_TypeClass_FLOAT )) ||
406 ( aTypeClass == TypeClass( typelib_TypeClass_DOUBLE )))
407 {
408 bValueValid = rAny >>= fValue;
409 bFloat = true;
410 }
411
412 return bValueValid;
413 }
414
FormatOutputString(double fValue)415 OUString SpinfieldToolbarController::FormatOutputString( double fValue )
416 {
417 if ( m_aOutFormat.isEmpty() )
418 {
419 if ( m_bFloat )
420 return OUString::number( fValue );
421 else
422 return OUString::number( sal_Int32( fValue ));
423 }
424 else
425 {
426 #ifdef _WIN32
427 sal_Unicode aBuffer[128];
428
429 aBuffer[0] = 0;
430 if ( m_bFloat )
431 _snwprintf( o3tl::toW(aBuffer), SAL_N_ELEMENTS(aBuffer), o3tl::toW(m_aOutFormat.getStr()), fValue );
432 else
433 _snwprintf( o3tl::toW(aBuffer), SAL_N_ELEMENTS(aBuffer), o3tl::toW(m_aOutFormat.getStr()), sal_Int32( fValue ));
434
435 return OUString(aBuffer);
436 #else
437 // Currently we have no support for a format string using sal_Unicode. wchar_t
438 // is 32 bit on Unix platform!
439 char aBuffer[128];
440
441 OString aFormat = OUStringToOString( m_aOutFormat, osl_getThreadTextEncoding() );
442 if ( m_bFloat )
443 snprintf( aBuffer, 128, aFormat.getStr(), fValue );
444 else
445 snprintf( aBuffer, 128, aFormat.getStr(), static_cast<tools::Long>( fValue ));
446
447 sal_Int32 nSize = strlen( aBuffer );
448 OString aTmp( aBuffer, nSize );
449 return OStringToOUString( aTmp, osl_getThreadTextEncoding() );
450 #endif
451 }
452 }
453
454 } // namespace
455
456 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
457