Files
libreoffice/vcl/source/control/spinbtn.cxx

554 lines
15 KiB
C++
Raw Normal View History

2000-09-18 16:07:07 +00:00
/*************************************************************************
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2000-09-18 16:07:07 +00:00
*
* Copyright 2008 by Sun Microsystems, Inc.
2000-09-18 16:07:07 +00:00
*
* OpenOffice.org - a multi-platform office productivity suite
2000-09-18 16:07:07 +00:00
*
* $RCSfile: spinbtn.cxx,v $
* $Revision: 1.13 $
2000-09-18 16:07:07 +00:00
*
* This file is part of OpenOffice.org.
2000-09-18 16:07:07 +00:00
*
* OpenOffice.org is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 3
* only, as published by the Free Software Foundation.
2000-09-18 16:07:07 +00:00
*
* OpenOffice.org is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License version 3 for more details
* (a copy is included in the LICENSE file that accompanied this code).
2000-09-18 16:07:07 +00:00
*
* You should have received a copy of the GNU Lesser General Public License
* version 3 along with OpenOffice.org. If not, see
* <http://www.openoffice.org/license.html>
* for a copy of the LGPLv3 License.
2000-09-18 16:07:07 +00:00
*
************************************************************************/
// MARKER(update_precomp.py): autogen include statement, do not remove
#include "precompiled_vcl.hxx"
#include <tools/rcid.h>
#include <vcl/spin.h>
#include <vcl/event.hxx>
#include <vcl/spin.hxx>
2000-09-18 16:07:07 +00:00
// =======================================================================
void SpinButton::ImplInit( Window* pParent, WinBits nStyle )
{
mbUpperIn = FALSE;
mbLowerIn = FALSE;
mbInitialUp = FALSE;
mbInitialDown = FALSE;
mnMinRange = 0;
mnMaxRange = 100;
mnValue = 0;
mnValueStep = 1;
2000-09-18 16:07:07 +00:00
maRepeatTimer.SetTimeout( GetSettings().GetMouseSettings().GetButtonStartRepeat() );
maRepeatTimer.SetTimeoutHdl( LINK( this, SpinButton, ImplTimeout ) );
mbRepeat = 0 != ( nStyle & WB_REPEAT );
2000-09-18 16:07:07 +00:00
if ( nStyle & WB_HSCROLL )
mbHorz = TRUE;
else
mbHorz = FALSE;
Control::ImplInit( pParent, nStyle, NULL );
}
// -----------------------------------------------------------------------
SpinButton::SpinButton( Window* pParent, WinBits nStyle )
:Control( WINDOW_SPINBUTTON )
,mbUpperIsFocused( FALSE )
2000-09-18 16:07:07 +00:00
{
ImplInit( pParent, nStyle );
}
// -----------------------------------------------------------------------
SpinButton::SpinButton( Window* pParent, const ResId& rResId )
:Control( WINDOW_SPINBUTTON )
,mbUpperIsFocused( FALSE )
2000-09-18 16:07:07 +00:00
{
rResId.SetRT( RSC_SPINBUTTON );
ImplInit( pParent, ImplInitRes( rResId ) );
ImplLoadRes( rResId );
Resize();
}
// -----------------------------------------------------------------------
SpinButton::~SpinButton()
{
}
// -----------------------------------------------------------------------
IMPL_LINK( SpinButton, ImplTimeout, Timer*, pTimer )
{
if ( pTimer->GetTimeout() == GetSettings().GetMouseSettings().GetButtonStartRepeat() )
2000-09-18 16:07:07 +00:00
{
pTimer->SetTimeout( GetSettings().GetMouseSettings().GetButtonRepeat() );
2000-09-18 16:07:07 +00:00
pTimer->Start();
}
else
{
if ( mbInitialUp )
Up();
else
Down();
}
return 0;
}
// -----------------------------------------------------------------------
void SpinButton::Up()
{
if ( ImplIsUpperEnabled() )
{
mnValue += mnValueStep;
StateChanged( STATE_CHANGE_DATA );
ImplMoveFocus( TRUE );
}
ImplCallEventListenersAndHandler( VCLEVENT_SPINBUTTON_UP, maUpHdlLink, this );
2000-09-18 16:07:07 +00:00
}
// -----------------------------------------------------------------------
void SpinButton::Down()
{
if ( ImplIsLowerEnabled() )
{
mnValue -= mnValueStep;
StateChanged( STATE_CHANGE_DATA );
ImplMoveFocus( FALSE );
}
ImplCallEventListenersAndHandler( VCLEVENT_SPINBUTTON_DOWN, maDownHdlLink, this );
2000-09-18 16:07:07 +00:00
}
// -----------------------------------------------------------------------
void SpinButton::Resize()
{
Control::Resize();
2000-09-18 16:07:07 +00:00
Size aSize( GetOutputSizePixel() );
Point aTmpPoint;
Rectangle aRect( aTmpPoint, aSize );
if ( mbHorz )
{
maLowerRect = Rectangle( 0, 0, aSize.Width()/2, aSize.Height()-1 );
maUpperRect = Rectangle( maLowerRect.TopRight(), aRect.BottomRight() );
2000-09-18 16:07:07 +00:00
}
else
{
maUpperRect = Rectangle( 0, 0, aSize.Width()-1, aSize.Height()/2 );
maLowerRect = Rectangle( maUpperRect.BottomLeft(), aRect.BottomRight() );
}
ImplCalcFocusRect( ImplIsUpperEnabled() || !ImplIsLowerEnabled() );
2000-09-18 16:07:07 +00:00
Invalidate();
}
// -----------------------------------------------------------------------
void SpinButton::Draw( OutputDevice* pDev, const Point& rPos, const Size& rSize, ULONG nFlags )
{
Point aPos = pDev->LogicToPixel( rPos );
Size aSize = pDev->LogicToPixel( rSize );
pDev->Push();
pDev->SetMapMode();
if ( !(nFlags & WINDOW_DRAW_MONO) )
{
// DecoView uses the FaceColor...
AllSettings aSettings = pDev->GetSettings();
StyleSettings aStyleSettings = aSettings.GetStyleSettings();
if ( IsControlBackground() )
aStyleSettings.SetFaceColor( GetControlBackground() );
else
aStyleSettings.SetFaceColor( GetSettings().GetStyleSettings().GetFaceColor() );
aSettings.SetStyleSettings( aStyleSettings );
pDev->SetSettings( aSettings );
}
Rectangle aRect( Point( 0, 0 ), aSize );
Rectangle aLowerRect, aUpperRect;
if ( mbHorz )
{
aLowerRect = Rectangle( 0, 0, aSize.Width()/2, aSize.Height()-1 );
aUpperRect = Rectangle( aLowerRect.TopRight(), aRect.BottomRight() );
}
else
{
aUpperRect = Rectangle( 0, 0, aSize.Width()-1, aSize.Height()/2 );
aLowerRect = Rectangle( aUpperRect.BottomLeft(), aRect.BottomRight() );
}
aUpperRect += aPos;
aLowerRect += aPos;
ImplDrawSpinButton( pDev, aUpperRect, aLowerRect, FALSE, FALSE,
IsEnabled() && ImplIsUpperEnabled(),
IsEnabled() && ImplIsLowerEnabled(), mbHorz, TRUE );
pDev->Pop();
}
2000-09-18 16:07:07 +00:00
void SpinButton::Paint( const Rectangle& )
{
HideFocus();
2000-09-18 16:07:07 +00:00
BOOL bEnable = IsEnabled();
ImplDrawSpinButton( this, maUpperRect, maLowerRect, mbUpperIn, mbLowerIn,
bEnable && ImplIsUpperEnabled(),
bEnable && ImplIsLowerEnabled(), mbHorz, TRUE );
if ( HasFocus() )
ShowFocus( maFocusRect );
2000-09-18 16:07:07 +00:00
}
// -----------------------------------------------------------------------
void SpinButton::MouseButtonDown( const MouseEvent& rMEvt )
{
if ( maUpperRect.IsInside( rMEvt.GetPosPixel() ) && ( ImplIsUpperEnabled() ) )
2000-09-18 16:07:07 +00:00
{
mbUpperIn = TRUE;
mbInitialUp = TRUE;
Invalidate( maUpperRect );
}
else if ( maLowerRect.IsInside( rMEvt.GetPosPixel() ) && ( ImplIsLowerEnabled() ) )
2000-09-18 16:07:07 +00:00
{
mbLowerIn = TRUE;
mbInitialDown = TRUE;
Invalidate( maLowerRect );
}
if ( mbUpperIn || mbLowerIn )
{
Update();
CaptureMouse();
if ( mbRepeat )
maRepeatTimer.Start();
}
}
// -----------------------------------------------------------------------
void SpinButton::MouseButtonUp( const MouseEvent& )
{
ReleaseMouse();
if ( mbRepeat )
{
maRepeatTimer.Stop();
maRepeatTimer.SetTimeout(GetSettings().GetMouseSettings().GetButtonStartRepeat() );
}
2000-09-18 16:07:07 +00:00
if ( mbUpperIn )
{
mbUpperIn = FALSE;
Invalidate( maUpperRect );
Update();
Up();
}
else if ( mbLowerIn )
{
mbLowerIn = FALSE;
Invalidate( maLowerRect );
Update();
Down();
}
mbInitialUp = mbInitialDown = FALSE;
}
// -----------------------------------------------------------------------
void SpinButton::MouseMove( const MouseEvent& rMEvt )
{
if ( !rMEvt.IsLeft() || (!mbInitialUp && !mbInitialDown) )
return;
if ( !maUpperRect.IsInside( rMEvt.GetPosPixel() ) &&
mbUpperIn && mbInitialUp )
{
mbUpperIn = FALSE;
maRepeatTimer.Stop();
Invalidate( maUpperRect );
Update();
}
else if ( !maLowerRect.IsInside( rMEvt.GetPosPixel() ) &&
mbLowerIn & mbInitialDown )
{
mbLowerIn = FALSE;
maRepeatTimer.Stop();
Invalidate( maLowerRect );
Update();
}
else if ( maUpperRect.IsInside( rMEvt.GetPosPixel() ) &&
!mbUpperIn && mbInitialUp )
{
mbUpperIn = TRUE;
if ( mbRepeat )
maRepeatTimer.Start();
Invalidate( maUpperRect );
Update();
}
else if ( maLowerRect.IsInside( rMEvt.GetPosPixel() ) &&
!mbLowerIn && mbInitialDown )
{
mbLowerIn = TRUE;
if ( mbRepeat )
maRepeatTimer.Start();
Invalidate( maLowerRect );
Update();
}
}
// -----------------------------------------------------------------------
void SpinButton::KeyInput( const KeyEvent& rKEvt )
{
KeyCode aCode = rKEvt.GetKeyCode();
if ( !rKEvt.GetKeyCode().GetModifier() )
{
switch ( rKEvt.GetKeyCode().GetCode() )
{
case KEY_LEFT:
case KEY_RIGHT:
{
BOOL bUp = KEY_RIGHT == rKEvt.GetKeyCode().GetCode();
if ( mbHorz && !ImplMoveFocus( bUp ) )
bUp ? Up() : Down();
}
break;
case KEY_UP:
case KEY_DOWN:
{
BOOL bUp = KEY_UP == rKEvt.GetKeyCode().GetCode();
if ( !mbHorz && !ImplMoveFocus( KEY_UP == rKEvt.GetKeyCode().GetCode() ) )
bUp ? Up() : Down();
}
break;
case KEY_SPACE:
mbUpperIsFocused ? Up() : Down();
break;
default:
Control::KeyInput( rKEvt );
break;
}
}
2000-09-18 16:07:07 +00:00
else
Control::KeyInput( rKEvt );
}
// -----------------------------------------------------------------------
void SpinButton::StateChanged( StateChangedType nType )
{
switch ( nType )
{
case STATE_CHANGE_DATA:
case STATE_CHANGE_ENABLE:
2000-09-18 16:07:07 +00:00
Invalidate();
break;
case STATE_CHANGE_STYLE:
{
BOOL bNewRepeat = 0 != ( GetStyle() & WB_REPEAT );
if ( bNewRepeat != mbRepeat )
{
if ( maRepeatTimer.IsActive() )
{
maRepeatTimer.Stop();
maRepeatTimer.SetTimeout( GetSettings().GetMouseSettings().GetButtonStartRepeat() );
}
mbRepeat = bNewRepeat;
}
BOOL bNewHorz = 0 != ( GetStyle() & WB_HSCROLL );
if ( bNewHorz != mbHorz )
{
mbHorz = bNewHorz;
Resize();
}
}
break;
}
2000-09-18 16:07:07 +00:00
Control::StateChanged( nType );
}
// -----------------------------------------------------------------------
void SpinButton::SetRangeMin( long nNewRange )
{
SetRange( Range( nNewRange, GetRangeMax() ) );
}
// -----------------------------------------------------------------------
void SpinButton::SetRangeMax( long nNewRange )
{
SetRange( Range( GetRangeMin(), nNewRange ) );
}
// -----------------------------------------------------------------------
void SpinButton::SetRange( const Range& rRange )
{
// adjust rage
Range aRange = rRange;
aRange.Justify();
long nNewMinRange = aRange.Min();
long nNewMaxRange = aRange.Max();
// do something only if old and new range differ
if ( (mnMinRange != nNewMinRange) ||
(mnMaxRange != nNewMaxRange) )
{
mnMinRange = nNewMinRange;
mnMaxRange = nNewMaxRange;
// adjust value to new range, if necessary
if ( mnValue > mnMaxRange )
mnValue = mnMaxRange;
if ( mnValue < mnMinRange )
mnValue = mnMinRange;
StateChanged( STATE_CHANGE_DATA );
}
}
// -----------------------------------------------------------------------
void SpinButton::SetValue( long nValue )
{
// adjust, if necessary
if ( nValue > mnMaxRange )
nValue = mnMaxRange;
if ( nValue < mnMinRange )
nValue = mnMinRange;
if ( mnValue != nValue )
{
mnValue = nValue;
StateChanged( STATE_CHANGE_DATA );
}
}
// -----------------------------------------------------------------------
void SpinButton::GetFocus()
{
ShowFocus( maFocusRect );
Control::GetFocus();
}
// -----------------------------------------------------------------------
void SpinButton::LoseFocus()
{
HideFocus();
Control::LoseFocus();
}
// -----------------------------------------------------------------------
BOOL SpinButton::ImplMoveFocus( BOOL _bUpper )
{
if ( _bUpper == mbUpperIsFocused )
return FALSE;
HideFocus();
ImplCalcFocusRect( _bUpper );
if ( HasFocus() )
ShowFocus( maFocusRect );
return TRUE;
}
// -----------------------------------------------------------------------
void SpinButton::ImplCalcFocusRect( BOOL _bUpper )
{
maFocusRect = _bUpper ? maUpperRect : maLowerRect;
// inflate by some pixels
maFocusRect.Left() += 2;
maFocusRect.Top() += 2;
maFocusRect.Right() -= 2;
maFocusRect.Bottom() -= 2;
mbUpperIsFocused = _bUpper;
}
// -----------------------------------------------------------------------
Rectangle* SpinButton::ImplFindPartRect( const Point& rPt )
{
if( maUpperRect.IsInside( rPt ) )
return &maUpperRect;
else if( maLowerRect.IsInside( rPt ) )
return &maLowerRect;
else
return NULL;
}
long SpinButton::PreNotify( NotifyEvent& rNEvt )
{
long nDone = 0;
const MouseEvent* pMouseEvt = NULL;
if( (rNEvt.GetType() == EVENT_MOUSEMOVE) && (pMouseEvt = rNEvt.GetMouseEvent()) != NULL )
{
if( !pMouseEvt->GetButtons() && !pMouseEvt->IsSynthetic() && !pMouseEvt->IsModifierChanged() )
{
// trigger redraw if mouse over state has changed
if( IsNativeControlSupported(CTRL_SPINBOX, PART_ENTIRE_CONTROL) ||
IsNativeControlSupported(CTRL_SPINBOX, PART_ALL_BUTTONS) )
{
Rectangle* pRect = ImplFindPartRect( GetPointerPosPixel() );
Rectangle* pLastRect = ImplFindPartRect( GetLastPointerPosPixel() );
if( pRect != pLastRect || (pMouseEvt->IsLeaveWindow() || pMouseEvt->IsEnterWindow()) )
{
Region aRgn( GetActiveClipRegion() );
if( pLastRect )
{
SetClipRegion( *pLastRect );
Paint( *pLastRect );
SetClipRegion( aRgn );
}
if( pRect )
{
SetClipRegion( *pRect );
Paint( *pRect );
SetClipRegion( aRgn );
}
}
}
}
}
return nDone ? nDone : Control::PreNotify(rNEvt);
}
// -----------------------------------------------------------------------