Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / vcl / source / control / spinbtn.cxx
blob6c738b5056ad4d1ef573401a973ed84424f74132
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 <vcl/event.hxx>
21 #include <vcl/toolkit/spin.hxx>
22 #include <vcl/settings.hxx>
23 #include <vcl/vclevent.hxx>
25 #include <spin.hxx>
27 void SpinButton::ImplInit( vcl::Window* pParent, WinBits nStyle )
29 mbUpperIn = false;
30 mbLowerIn = false;
31 mbInitialUp = false;
32 mbInitialDown = false;
34 mnMinRange = 0;
35 mnMaxRange = 100;
36 mnValue = 0;
37 mnValueStep = 1;
39 maRepeatTimer.SetTimeout(MouseSettings::GetButtonStartRepeat());
40 maRepeatTimer.SetInvokeHandler(LINK(this, SpinButton, ImplTimeout));
42 mbRepeat = 0 != (nStyle & WB_REPEAT);
44 if (nStyle & WB_HSCROLL)
45 mbHorz = true;
46 else
47 mbHorz = false;
49 Control::ImplInit( pParent, nStyle, nullptr );
52 SpinButton::SpinButton( vcl::Window* pParent, WinBits nStyle )
53 : Control(WindowType::SPINBUTTON)
54 , mbUpperIsFocused(false)
56 ImplInit(pParent, nStyle);
59 IMPL_LINK(SpinButton, ImplTimeout, Timer*, pTimer, void)
61 if (pTimer->GetTimeout() == MouseSettings::GetButtonStartRepeat())
63 pTimer->SetTimeout( GetSettings().GetMouseSettings().GetButtonRepeat() );
64 pTimer->Start();
66 else
68 if (mbInitialUp)
69 Up();
70 else
71 Down();
75 void SpinButton::Up()
77 if (ImplIsUpperEnabled())
79 mnValue += mnValueStep;
80 CompatStateChanged(StateChangedType::Data);
82 ImplMoveFocus(true);
85 ImplCallEventListenersAndHandler(VclEventId::SpinbuttonUp, nullptr );
88 void SpinButton::Down()
90 if (ImplIsLowerEnabled())
92 mnValue -= mnValueStep;
93 CompatStateChanged(StateChangedType::Data);
95 ImplMoveFocus(false);
98 ImplCallEventListenersAndHandler(VclEventId::SpinbuttonDown, nullptr );
101 void SpinButton::Resize()
103 Control::Resize();
105 Size aSize(GetOutputSizePixel());
106 tools::Rectangle aRect(Point(), aSize);
107 if (mbHorz)
109 maLowerRect = tools::Rectangle(0, 0, aSize.Width() / 2, aSize.Height() - 1);
110 maUpperRect = tools::Rectangle(maLowerRect.TopRight(), aRect.BottomRight());
112 else
114 maUpperRect = tools::Rectangle(0, 0, aSize.Width() - 1, aSize.Height() / 2);
115 maLowerRect = tools::Rectangle(maUpperRect.BottomLeft(), aRect.BottomRight());
118 ImplCalcFocusRect(ImplIsUpperEnabled() || !ImplIsLowerEnabled());
120 Invalidate();
123 void SpinButton::Draw(OutputDevice* pDev, const Point& rPos, const Size& rSize, DrawFlags nFlags)
125 Point aPos = pDev->LogicToPixel(rPos);
126 Size aSize = pDev->LogicToPixel(rSize);
128 pDev->Push();
129 pDev->SetMapMode();
130 if ( !(nFlags & DrawFlags::Mono) )
132 // DecoView uses the FaceColor...
133 AllSettings aSettings = pDev->GetSettings();
134 StyleSettings aStyleSettings = aSettings.GetStyleSettings();
135 if ( IsControlBackground() )
136 aStyleSettings.SetFaceColor( GetControlBackground() );
137 else
138 aStyleSettings.SetFaceColor( GetSettings().GetStyleSettings().GetFaceColor() );
140 aSettings.SetStyleSettings( aStyleSettings );
141 pDev->SetSettings( aSettings );
144 tools::Rectangle aRect( Point( 0, 0 ), aSize );
145 tools::Rectangle aLowerRect, aUpperRect;
146 if ( mbHorz )
148 aLowerRect = tools::Rectangle( 0, 0, aSize.Width()/2, aSize.Height()-1 );
149 aUpperRect = tools::Rectangle( aLowerRect.TopRight(), aRect.BottomRight() );
151 else
153 aUpperRect = tools::Rectangle( 0, 0, aSize.Width()-1, aSize.Height()/2 );
154 aLowerRect = tools::Rectangle( aUpperRect.BottomLeft(), aRect.BottomRight() );
157 aUpperRect += aPos;
158 aLowerRect += aPos;
160 ImplDrawSpinButton(*pDev, this, aUpperRect, aLowerRect, false, false,
161 IsEnabled() && ImplIsUpperEnabled(),
162 IsEnabled() && ImplIsLowerEnabled(), mbHorz, true);
163 pDev->Pop();
166 void SpinButton::Paint(vcl::RenderContext& rRenderContext, const tools::Rectangle& /*rRect*/)
168 HideFocus();
170 bool bEnable = IsEnabled();
171 ImplDrawSpinButton(rRenderContext, this, maUpperRect, maLowerRect, mbUpperIn, mbLowerIn,
172 bEnable && ImplIsUpperEnabled(),
173 bEnable && ImplIsLowerEnabled(), mbHorz, true);
175 if (HasFocus())
176 ShowFocus(maFocusRect);
179 void SpinButton::MouseButtonDown( const MouseEvent& rMEvt )
181 if ( maUpperRect.IsInside( rMEvt.GetPosPixel() ) && ( ImplIsUpperEnabled() ) )
183 mbUpperIn = true;
184 mbInitialUp = true;
185 Invalidate( maUpperRect );
187 else if ( maLowerRect.IsInside( rMEvt.GetPosPixel() ) && ( ImplIsLowerEnabled() ) )
189 mbLowerIn = true;
190 mbInitialDown = true;
191 Invalidate( maLowerRect );
194 if ( mbUpperIn || mbLowerIn )
196 Update();
197 CaptureMouse();
198 if ( mbRepeat )
199 maRepeatTimer.Start();
203 void SpinButton::MouseButtonUp( const MouseEvent& )
205 ReleaseMouse();
206 if ( mbRepeat )
208 maRepeatTimer.Stop();
209 maRepeatTimer.SetTimeout(MouseSettings::GetButtonStartRepeat() );
212 if ( mbUpperIn )
214 mbUpperIn = false;
215 Invalidate( maUpperRect );
216 Update();
217 Up();
219 else if ( mbLowerIn )
221 mbLowerIn = false;
222 Invalidate( maLowerRect );
223 Update();
224 Down();
227 mbInitialUp = mbInitialDown = false;
230 void SpinButton::MouseMove( const MouseEvent& rMEvt )
232 if ( !rMEvt.IsLeft() || (!mbInitialUp && !mbInitialDown) )
233 return;
235 if ( !maUpperRect.IsInside( rMEvt.GetPosPixel() ) &&
236 mbUpperIn && mbInitialUp )
238 mbUpperIn = false;
239 maRepeatTimer.Stop();
240 Invalidate( maUpperRect );
241 Update();
243 else if ( !maLowerRect.IsInside( rMEvt.GetPosPixel() ) &&
244 mbLowerIn && mbInitialDown )
246 mbLowerIn = false;
247 maRepeatTimer.Stop();
248 Invalidate( maLowerRect );
249 Update();
251 else if ( maUpperRect.IsInside( rMEvt.GetPosPixel() ) &&
252 !mbUpperIn && mbInitialUp )
254 mbUpperIn = true;
255 if ( mbRepeat )
256 maRepeatTimer.Start();
257 Invalidate( maUpperRect );
258 Update();
260 else if ( maLowerRect.IsInside( rMEvt.GetPosPixel() ) &&
261 !mbLowerIn && mbInitialDown )
263 mbLowerIn = true;
264 if ( mbRepeat )
265 maRepeatTimer.Start();
266 Invalidate( maLowerRect );
267 Update();
271 void SpinButton::KeyInput( const KeyEvent& rKEvt )
273 if ( !rKEvt.GetKeyCode().GetModifier() )
275 switch ( rKEvt.GetKeyCode().GetCode() )
277 case KEY_LEFT:
278 case KEY_RIGHT:
280 bool bUp = KEY_RIGHT == rKEvt.GetKeyCode().GetCode();
281 if ( mbHorz && !ImplMoveFocus( bUp ) )
282 bUp ? Up() : Down();
284 break;
286 case KEY_UP:
287 case KEY_DOWN:
289 bool bUp = KEY_UP == rKEvt.GetKeyCode().GetCode();
290 if ( !mbHorz && !ImplMoveFocus( KEY_UP == rKEvt.GetKeyCode().GetCode() ) )
291 bUp ? Up() : Down();
293 break;
295 case KEY_SPACE:
296 mbUpperIsFocused ? Up() : Down();
297 break;
299 default:
300 Control::KeyInput( rKEvt );
301 break;
304 else
305 Control::KeyInput( rKEvt );
308 void SpinButton::StateChanged( StateChangedType nType )
310 switch ( nType )
312 case StateChangedType::Data:
313 case StateChangedType::Enable:
314 Invalidate();
315 break;
317 case StateChangedType::Style:
319 bool bNewRepeat = 0 != ( GetStyle() & WB_REPEAT );
320 if ( bNewRepeat != mbRepeat )
322 if ( maRepeatTimer.IsActive() )
324 maRepeatTimer.Stop();
325 maRepeatTimer.SetTimeout( MouseSettings::GetButtonStartRepeat() );
327 mbRepeat = bNewRepeat;
330 bool bNewHorz = 0 != ( GetStyle() & WB_HSCROLL );
331 if ( bNewHorz != mbHorz )
333 mbHorz = bNewHorz;
334 Resize();
337 break;
338 default:;
341 Control::StateChanged( nType );
344 void SpinButton::SetRangeMin( long nNewRange )
346 SetRange( Range( nNewRange, GetRangeMax() ) );
349 void SpinButton::SetRangeMax( long nNewRange )
351 SetRange( Range( GetRangeMin(), nNewRange ) );
354 void SpinButton::SetRange( const Range& rRange )
356 // adjust rage
357 Range aRange = rRange;
358 aRange.Justify();
359 long nNewMinRange = aRange.Min();
360 long nNewMaxRange = aRange.Max();
362 // do something only if old and new range differ
363 if ( (mnMinRange != nNewMinRange) ||
364 (mnMaxRange != nNewMaxRange) )
366 mnMinRange = nNewMinRange;
367 mnMaxRange = nNewMaxRange;
369 // adjust value to new range, if necessary
370 if ( mnValue > mnMaxRange )
371 mnValue = mnMaxRange;
372 if ( mnValue < mnMinRange )
373 mnValue = mnMinRange;
375 CompatStateChanged( StateChangedType::Data );
379 void SpinButton::SetValue( long nValue )
381 // adjust, if necessary
382 if ( nValue > mnMaxRange )
383 nValue = mnMaxRange;
384 if ( nValue < mnMinRange )
385 nValue = mnMinRange;
387 if ( mnValue != nValue )
389 mnValue = nValue;
390 CompatStateChanged( StateChangedType::Data );
394 void SpinButton::GetFocus()
396 ShowFocus( maFocusRect );
397 Control::GetFocus();
400 void SpinButton::LoseFocus()
402 HideFocus();
403 Control::LoseFocus();
406 bool SpinButton::ImplMoveFocus( bool _bUpper )
408 if ( _bUpper == mbUpperIsFocused )
409 return false;
411 HideFocus();
412 ImplCalcFocusRect( _bUpper );
413 if ( HasFocus() )
414 ShowFocus( maFocusRect );
415 return true;
418 void SpinButton::ImplCalcFocusRect( bool _bUpper )
420 maFocusRect = _bUpper ? maUpperRect : maLowerRect;
421 // inflate by some pixels
422 maFocusRect.AdjustLeft(2 );
423 maFocusRect.AdjustTop(2 );
424 maFocusRect.AdjustRight( -2 );
425 maFocusRect.AdjustBottom( -2 );
426 mbUpperIsFocused = _bUpper;
429 tools::Rectangle* SpinButton::ImplFindPartRect( const Point& rPt )
431 if( maUpperRect.IsInside( rPt ) )
432 return &maUpperRect;
433 else if( maLowerRect.IsInside( rPt ) )
434 return &maLowerRect;
435 else
436 return nullptr;
439 bool SpinButton::PreNotify( NotifyEvent& rNEvt )
441 const MouseEvent* pMouseEvt = nullptr;
443 if ((rNEvt.GetType() == MouseNotifyEvent::MOUSEMOVE) && (pMouseEvt = rNEvt.GetMouseEvent()) != nullptr)
445 if (!pMouseEvt->GetButtons() && !pMouseEvt->IsSynthetic() && !pMouseEvt->IsModifierChanged())
447 // trigger redraw if mouse over state has changed
448 if (IsNativeControlSupported(ControlType::Spinbox, ControlPart::Entire) ||
449 IsNativeControlSupported(ControlType::Spinbox, ControlPart::AllButtons) )
451 tools::Rectangle* pRect = ImplFindPartRect( GetPointerPosPixel() );
452 tools::Rectangle* pLastRect = ImplFindPartRect( GetLastPointerPosPixel() );
453 if (pRect != pLastRect || (pMouseEvt->IsLeaveWindow() || pMouseEvt->IsEnterWindow()))
455 vcl::Region aRgn(GetActiveClipRegion());
456 if (pLastRect)
458 SetClipRegion(vcl::Region(*pLastRect));
459 Invalidate(*pLastRect);
460 SetClipRegion( aRgn );
462 if (pRect)
464 SetClipRegion(vcl::Region(*pRect));
465 Invalidate(*pRect);
466 SetClipRegion(aRgn);
473 return Control::PreNotify(rNEvt);
476 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */