LibreOffice Module vcl (master) 1
spinbtn.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
20#include <vcl/event.hxx>
21#include <vcl/toolkit/spin.hxx>
22#include <vcl/settings.hxx>
23#include <vcl/vclevent.hxx>
24
25#include <spin.hxx>
26
27void SpinButton::ImplInit( vcl::Window* pParent, WinBits nStyle )
28{
29 mbUpperIn = false;
30 mbLowerIn = false;
31 mbInitialUp = false;
32 mbInitialDown = false;
33
34 mnMinRange = 0;
35 mnMaxRange = 100;
36 mnValue = 0;
37 mnValueStep = 1;
38
39 maRepeatTimer.SetTimeout(MouseSettings::GetButtonStartRepeat());
40 maRepeatTimer.SetInvokeHandler(LINK(this, SpinButton, ImplTimeout));
41
42 mbRepeat = 0 != (nStyle & WB_REPEAT);
43
44 if (nStyle & WB_HSCROLL)
45 mbHorz = true;
46 else
47 mbHorz = false;
48
49 Control::ImplInit( pParent, nStyle, nullptr );
50}
51
52SpinButton::SpinButton( vcl::Window* pParent, WinBits nStyle )
54 , maRepeatTimer("SpinButton maRepeatTimer")
55 , mbUpperIsFocused(false)
56{
57 ImplInit(pParent, nStyle);
58}
59
60IMPL_LINK(SpinButton, ImplTimeout, Timer*, pTimer, void)
61{
62 if (pTimer->GetTimeout() == static_cast<sal_uInt64>(MouseSettings::GetButtonStartRepeat()))
63 {
64 pTimer->SetTimeout( GetSettings().GetMouseSettings().GetButtonRepeat() );
65 pTimer->Start();
66 }
67 else
68 {
69 if (mbInitialUp)
70 Up();
71 else
72 Down();
73 }
74}
75
76void SpinButton::Up()
77{
78 if (ImplIsUpperEnabled())
79 {
80 mnValue += mnValueStep;
81 CompatStateChanged(StateChangedType::Data);
82
83 ImplMoveFocus(true);
84 }
85
86 ImplCallEventListenersAndHandler(VclEventId::SpinbuttonUp, nullptr );
87}
88
89void SpinButton::Down()
90{
91 if (ImplIsLowerEnabled())
92 {
93 mnValue -= mnValueStep;
94 CompatStateChanged(StateChangedType::Data);
95
96 ImplMoveFocus(false);
97 }
98
99 ImplCallEventListenersAndHandler(VclEventId::SpinbuttonDown, nullptr );
100}
101
102void SpinButton::Resize()
103{
105
106 Size aSize(GetOutputSizePixel());
107 tools::Rectangle aRect(Point(), aSize);
108 if (mbHorz)
109 {
110 maLowerRect = tools::Rectangle(0, 0, aSize.Width() / 2, aSize.Height() - 1);
111 maUpperRect = tools::Rectangle(maLowerRect.TopRight(), aRect.BottomRight());
112 }
113 else
114 {
115 maUpperRect = tools::Rectangle(0, 0, aSize.Width() - 1, aSize.Height() / 2);
116 maLowerRect = tools::Rectangle(maUpperRect.BottomLeft(), aRect.BottomRight());
117 }
118
119 ImplCalcFocusRect(ImplIsUpperEnabled() || !ImplIsLowerEnabled());
120
121 Invalidate();
122}
123
124void SpinButton::Draw(OutputDevice* pDev, const Point& rPos, SystemTextColorFlags nFlags)
125{
126 Point aPos = pDev->LogicToPixel(rPos);
127 Size aSize = GetSizePixel();
128
129 pDev->Push();
130 pDev->SetMapMode();
131 if ( !(nFlags & SystemTextColorFlags::Mono) )
132 {
133 // DecoView uses the FaceColor...
134 AllSettings aSettings = pDev->GetSettings();
135 StyleSettings aStyleSettings = aSettings.GetStyleSettings();
136 if ( IsControlBackground() )
137 aStyleSettings.SetFaceColor( GetControlBackground() );
138 else
139 aStyleSettings.SetFaceColor( GetSettings().GetStyleSettings().GetFaceColor() );
140
141 aSettings.SetStyleSettings( aStyleSettings );
142 pDev->SetSettings( aSettings );
143 }
144
145 tools::Rectangle aRect( Point( 0, 0 ), aSize );
146 tools::Rectangle aLowerRect, aUpperRect;
147 if ( mbHorz )
148 {
149 aLowerRect = tools::Rectangle( 0, 0, aSize.Width()/2, aSize.Height()-1 );
150 aUpperRect = tools::Rectangle( aLowerRect.TopRight(), aRect.BottomRight() );
151 }
152 else
153 {
154 aUpperRect = tools::Rectangle( 0, 0, aSize.Width()-1, aSize.Height()/2 );
155 aLowerRect = tools::Rectangle( aUpperRect.BottomLeft(), aRect.BottomRight() );
156 }
157
158 aUpperRect += aPos;
159 aLowerRect += aPos;
160
161 ImplDrawSpinButton(*pDev, this, aUpperRect, aLowerRect, false, false,
162 IsEnabled() && ImplIsUpperEnabled(),
163 IsEnabled() && ImplIsLowerEnabled(), mbHorz, true);
164 pDev->Pop();
165}
166
167void SpinButton::Paint(vcl::RenderContext& rRenderContext, const tools::Rectangle& /*rRect*/)
168{
169 HideFocus();
170
171 bool bEnable = IsEnabled();
172 ImplDrawSpinButton(rRenderContext, this, maUpperRect, maLowerRect, mbUpperIn, mbLowerIn,
173 bEnable && ImplIsUpperEnabled(),
174 bEnable && ImplIsLowerEnabled(), mbHorz, true);
175
176 if (HasFocus())
177 ShowFocus(maFocusRect);
178}
179
180void SpinButton::MouseButtonDown( const MouseEvent& rMEvt )
181{
182 if ( maUpperRect.Contains( rMEvt.GetPosPixel() ) && ( ImplIsUpperEnabled() ) )
183 {
184 mbUpperIn = true;
185 mbInitialUp = true;
186 Invalidate( maUpperRect );
187 }
188 else if ( maLowerRect.Contains( rMEvt.GetPosPixel() ) && ( ImplIsLowerEnabled() ) )
189 {
190 mbLowerIn = true;
191 mbInitialDown = true;
192 Invalidate( maLowerRect );
193 }
194
195 if ( mbUpperIn || mbLowerIn )
196 {
197 CaptureMouse();
198 if ( mbRepeat )
199 maRepeatTimer.Start();
200 }
201}
202
203void SpinButton::MouseButtonUp( const MouseEvent& )
204{
205 ReleaseMouse();
206 if ( mbRepeat )
207 {
208 maRepeatTimer.Stop();
209 maRepeatTimer.SetTimeout(MouseSettings::GetButtonStartRepeat() );
210 }
211
212 if ( mbUpperIn )
213 {
214 mbUpperIn = false;
215 Invalidate( maUpperRect );
216 Up();
217 }
218 else if ( mbLowerIn )
219 {
220 mbLowerIn = false;
221 Invalidate( maLowerRect );
222 Down();
223 }
224
225 mbInitialUp = mbInitialDown = false;
226}
227
228void SpinButton::MouseMove( const MouseEvent& rMEvt )
229{
230 if ( !rMEvt.IsLeft() || (!mbInitialUp && !mbInitialDown) )
231 return;
232
233 if ( !maUpperRect.Contains( rMEvt.GetPosPixel() ) &&
234 mbUpperIn && mbInitialUp )
235 {
236 mbUpperIn = false;
237 maRepeatTimer.Stop();
238 Invalidate( maUpperRect );
239 }
240 else if ( !maLowerRect.Contains( rMEvt.GetPosPixel() ) &&
241 mbLowerIn && mbInitialDown )
242 {
243 mbLowerIn = false;
244 maRepeatTimer.Stop();
245 Invalidate( maLowerRect );
246 }
247 else if ( maUpperRect.Contains( rMEvt.GetPosPixel() ) &&
248 !mbUpperIn && mbInitialUp )
249 {
250 mbUpperIn = true;
251 if ( mbRepeat )
252 maRepeatTimer.Start();
253 Invalidate( maUpperRect );
254 }
255 else if ( maLowerRect.Contains( rMEvt.GetPosPixel() ) &&
256 !mbLowerIn && mbInitialDown )
257 {
258 mbLowerIn = true;
259 if ( mbRepeat )
260 maRepeatTimer.Start();
261 Invalidate( maLowerRect );
262 }
263}
264
265void SpinButton::KeyInput( const KeyEvent& rKEvt )
266{
267 if ( !rKEvt.GetKeyCode().GetModifier() )
268 {
269 switch ( rKEvt.GetKeyCode().GetCode() )
270 {
271 case KEY_LEFT:
272 case KEY_RIGHT:
273 {
274 bool bUp = KEY_RIGHT == rKEvt.GetKeyCode().GetCode();
275 if ( mbHorz && !ImplMoveFocus( bUp ) )
276 bUp ? Up() : Down();
277 }
278 break;
279
280 case KEY_UP:
281 case KEY_DOWN:
282 {
283 bool bUp = KEY_UP == rKEvt.GetKeyCode().GetCode();
284 if ( !mbHorz && !ImplMoveFocus( KEY_UP == rKEvt.GetKeyCode().GetCode() ) )
285 bUp ? Up() : Down();
286 }
287 break;
288
289 case KEY_SPACE:
290 mbUpperIsFocused ? Up() : Down();
291 break;
292
293 default:
294 Control::KeyInput( rKEvt );
295 break;
296 }
297 }
298 else
299 Control::KeyInput( rKEvt );
300}
301
302void SpinButton::StateChanged( StateChangedType nType )
303{
304 switch ( nType )
305 {
308 Invalidate();
309 break;
310
312 {
313 bool bNewRepeat = 0 != ( GetStyle() & WB_REPEAT );
314 if ( bNewRepeat != mbRepeat )
315 {
316 if ( maRepeatTimer.IsActive() )
317 {
318 maRepeatTimer.Stop();
319 maRepeatTimer.SetTimeout( MouseSettings::GetButtonStartRepeat() );
320 }
321 mbRepeat = bNewRepeat;
322 }
323
324 bool bNewHorz = 0 != ( GetStyle() & WB_HSCROLL );
325 if ( bNewHorz != mbHorz )
326 {
327 mbHorz = bNewHorz;
328 Resize();
329 }
330 }
331 break;
332 default:;
333 }
334
335 Control::StateChanged( nType );
336}
337
338void SpinButton::SetRangeMin( tools::Long nNewRange )
339{
340 SetRange( Range( nNewRange, GetRangeMax() ) );
341}
342
343void SpinButton::SetRangeMax( tools::Long nNewRange )
344{
345 SetRange( Range( GetRangeMin(), nNewRange ) );
346}
347
348void SpinButton::SetRange( const Range& rRange )
349{
350 // adjust rage
351 Range aRange = rRange;
352 aRange.Justify();
353 tools::Long nNewMinRange = aRange.Min();
354 tools::Long nNewMaxRange = aRange.Max();
355
356 // do something only if old and new range differ
357 if ( (mnMinRange == nNewMinRange) && (mnMaxRange == nNewMaxRange))
358 return;
359
360 mnMinRange = nNewMinRange;
361 mnMaxRange = nNewMaxRange;
362
363 // adjust value to new range, if necessary
364 if ( mnValue > mnMaxRange )
365 mnValue = mnMaxRange;
366 if ( mnValue < mnMinRange )
367 mnValue = mnMinRange;
368
369 CompatStateChanged( StateChangedType::Data );
370}
371
372void SpinButton::SetValue( tools::Long nValue )
373{
374 // adjust, if necessary
375 if ( nValue > mnMaxRange )
376 nValue = mnMaxRange;
377 if ( nValue < mnMinRange )
378 nValue = mnMinRange;
379
380 if ( mnValue != nValue )
381 {
382 mnValue = nValue;
383 CompatStateChanged( StateChangedType::Data );
384 }
385}
386
387void SpinButton::GetFocus()
388{
389 ShowFocus( maFocusRect );
391}
392
393void SpinButton::LoseFocus()
394{
395 HideFocus();
397}
398
399bool SpinButton::ImplMoveFocus( bool _bUpper )
400{
401 if ( _bUpper == mbUpperIsFocused )
402 return false;
403
404 HideFocus();
405 ImplCalcFocusRect( _bUpper );
406 if ( HasFocus() )
407 ShowFocus( maFocusRect );
408 return true;
409}
410
411void SpinButton::ImplCalcFocusRect( bool _bUpper )
412{
413 maFocusRect = _bUpper ? maUpperRect : maLowerRect;
414 // inflate by some pixels
415 maFocusRect.AdjustLeft(2 );
416 maFocusRect.AdjustTop(2 );
417 maFocusRect.AdjustRight( -2 );
418 maFocusRect.AdjustBottom( -2 );
419 mbUpperIsFocused = _bUpper;
420}
421
422tools::Rectangle* SpinButton::ImplFindPartRect( const Point& rPt )
423{
424 if( maUpperRect.Contains( rPt ) )
425 return &maUpperRect;
426 else if( maLowerRect.Contains( rPt ) )
427 return &maLowerRect;
428 else
429 return nullptr;
430}
431
432bool SpinButton::PreNotify( NotifyEvent& rNEvt )
433{
435 {
436 const MouseEvent* pMouseEvt = rNEvt.GetMouseEvent();
437 if (pMouseEvt && !pMouseEvt->GetButtons() && !pMouseEvt->IsSynthetic() && !pMouseEvt->IsModifierChanged())
438 {
439 // trigger redraw if mouse over state has changed
440 if (IsNativeControlSupported(ControlType::Spinbox, ControlPart::Entire) ||
441 IsNativeControlSupported(ControlType::Spinbox, ControlPart::AllButtons) )
442 {
443 tools::Rectangle* pRect = ImplFindPartRect( GetPointerPosPixel() );
444 tools::Rectangle* pLastRect = ImplFindPartRect( GetLastPointerPosPixel() );
445 if (pRect != pLastRect || (pMouseEvt->IsLeaveWindow() || pMouseEvt->IsEnterWindow()))
446 {
447 vcl::Region aRgn(GetOutDev()->GetActiveClipRegion());
448 if (pLastRect)
449 {
450 GetOutDev()->SetClipRegion(vcl::Region(*pLastRect));
451 Invalidate(*pLastRect);
452 GetOutDev()->SetClipRegion( aRgn );
453 }
454 if (pRect)
455 {
456 GetOutDev()->SetClipRegion(vcl::Region(*pRect));
457 Invalidate(*pRect);
458 GetOutDev()->SetClipRegion(aRgn);
459 }
460 }
461 }
462 }
463 }
464
465 return Control::PreNotify(rNEvt);
466}
467
468/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SystemTextColorFlags
const StyleSettings & GetStyleSettings() const
void SetStyleSettings(const StyleSettings &rSet)
Definition: ctrl.hxx:82
virtual void StateChanged(StateChangedType nStateChange) override
Definition: ctrl.cxx:256
virtual void Resize() override
Definition: ctrl.cxx:74
const vcl::KeyCode & GetKeyCode() const
Definition: event.hxx:57
bool IsEnterWindow() const
Definition: event.hxx:138
bool IsSynthetic() const
Definition: event.hxx:142
bool IsLeaveWindow() const
Definition: event.hxx:140
sal_uInt16 GetButtons() const
Definition: event.hxx:147
const Point & GetPosPixel() const
Definition: event.hxx:123
bool IsModifierChanged() const
Definition: event.hxx:144
bool IsLeft() const
Definition: event.hxx:149
static sal_Int32 GetButtonStartRepeat()
const MouseEvent * GetMouseEvent() const
Definition: event.hxx:324
MouseNotifyEvent GetType() const
Definition: event.hxx:308
Some things multiple-inherit from VclAbstractDialog and OutputDevice, so we need to use virtual inher...
Definition: outdev.hxx:170
virtual void SetSettings(const AllSettings &rSettings)
Definition: outdev.cxx:217
void SetMapMode()
Definition: map.cxx:649
SAL_WARN_UNUSED_RESULT Point LogicToPixel(const Point &rLogicPt) const
Definition: map.cxx:933
void Push(vcl::PushFlags nFlags=vcl::PushFlags::ALL)
Definition: stack.cxx:33
void Pop()
Definition: stack.cxx:92
const AllSettings & GetSettings() const
Definition: outdev.hxx:289
tools::Long Max() const
tools::Long Min() const
void Justify()
constexpr tools::Long Height() const
constexpr tools::Long Width() const
void SetFaceColor(const Color &rColor)
Definition: timer.hxx:27
constexpr Point TopRight() const
constexpr Point BottomLeft() const
sal_uInt16 GetCode() const
Definition: keycod.hxx:49
sal_uInt16 GetModifier() const
Definition: keycod.hxx:52
virtual void GetFocus()
Definition: window.cxx:1843
virtual void KeyInput(const KeyEvent &rKEvt)
Definition: window.cxx:1807
virtual bool PreNotify(NotifyEvent &rNEvt)
Definition: event.cxx:52
virtual void LoseFocus()
Definition: window.cxx:1857
SAL_DLLPRIVATE void ImplInit(vcl::Window *pParent, WinBits nStyle, SystemParentData *pSystemParentData)
Definition: window.cxx:940
sal_Int16 nValue
constexpr sal_uInt16 KEY_LEFT
Definition: keycodes.hxx:112
constexpr sal_uInt16 KEY_UP
Definition: keycodes.hxx:111
constexpr sal_uInt16 KEY_RIGHT
Definition: keycodes.hxx:113
constexpr sal_uInt16 KEY_DOWN
Definition: keycodes.hxx:110
constexpr sal_uInt16 KEY_SPACE
Definition: keycodes.hxx:123
long Long
const double mnValue
IMPL_LINK(SpinButton, ImplTimeout, Timer *, pTimer, void)
Definition: spinbtn.cxx:60
void ImplDrawSpinButton(vcl::RenderContext &rRenderContext, vcl::Window *pWindow, const tools::Rectangle &rUpperRect, const tools::Rectangle &rLowerRect, bool bUpperIn, bool bLowerIn, bool bUpperEnabled, bool bLowerEnabled, bool bHorz, bool bMirrorHorz)
Definition: spinfld.cxx:156
StateChangedType
Definition: window.hxx:291
sal_Int64 WinBits
Definition: wintypes.hxx:109
WindowType
Definition: wintypes.hxx:27
WinBits const WB_REPEAT
Definition: wintypes.hxx:154
WinBits const WB_HSCROLL
Definition: wintypes.hxx:177