1 /////////////////////////////////////////////////////////////////////////////
2 // Name: univ/scrolbar.cpp
3 // Purpose: wxScrollBar implementation
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 2000 SciTech Software, Inc. (www.scitechsoft.com)
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "univscrolbar.h"
24 #include "wx/wxprec.h"
35 #include "wx/dcclient.h"
36 #include "wx/scrolbar.h"
37 #include "wx/validate.h"
40 #include "wx/univ/scrtimer.h"
42 #include "wx/univ/renderer.h"
43 #include "wx/univ/inphand.h"
44 #include "wx/univ/theme.h"
46 #define WXDEBUG_SCROLLBAR
49 #undef WXDEBUG_SCROLLBAR
50 #endif // !__WXDEBUG__
52 #if defined(WXDEBUG_SCROLLBAR) && defined(__WXMSW__) && !defined(__WXMICROWIN__)
53 #include "wx/msw/private.h"
56 // ----------------------------------------------------------------------------
57 // wxScrollBarTimer: this class is used to repeatedly scroll the scrollbar
58 // when the mouse is help pressed on the arrow or on the bar. It generates the
59 // given scroll action command periodically.
60 // ----------------------------------------------------------------------------
62 class wxScrollBarTimer
: public wxScrollTimer
65 wxScrollBarTimer(wxStdScrollBarInputHandler
*handler
,
66 const wxControlAction
& action
,
67 wxScrollBar
*control
);
70 virtual bool DoNotify();
73 wxStdScrollBarInputHandler
*m_handler
;
74 wxControlAction m_action
;
75 wxScrollBar
*m_control
;
78 // ============================================================================
80 // ============================================================================
82 IMPLEMENT_DYNAMIC_CLASS(wxScrollBar
, wxControl
)
84 BEGIN_EVENT_TABLE(wxScrollBar
, wxScrollBarBase
)
85 EVT_IDLE(wxScrollBar::OnIdle
)
88 // ----------------------------------------------------------------------------
90 // ----------------------------------------------------------------------------
93 // warning C4355: 'this' : used in base member initializer list
94 #pragma warning(disable:4355) // so what? disable it...
97 wxScrollBar::wxScrollBar()
103 wxScrollBar::wxScrollBar(wxWindow
*parent
,
108 const wxValidator
& validator
,
109 const wxString
& name
)
114 (void)Create(parent
, id
, pos
, size
, style
, validator
, name
);
118 // warning C4355: 'this' : used in base member initializer list
119 #pragma warning(default:4355)
122 void wxScrollBar::Init()
131 for ( size_t n
= 0; n
< WXSIZEOF(m_elementsState
); n
++ )
133 m_elementsState
[n
] = 0;
139 bool wxScrollBar::Create(wxWindow
*parent
,
144 const wxValidator
& validator
,
145 const wxString
&name
)
147 // the scrollbars never have the border
148 style
&= ~wxBORDER_MASK
;
150 if ( !wxControl::Create(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
) )
155 // override the cursor of the target window (if any)
156 SetCursor(wxCURSOR_ARROW
);
158 CreateInputHandler(wxINP_HANDLER_SCROLLBAR
);
163 wxScrollBar::~wxScrollBar()
167 // ----------------------------------------------------------------------------
169 // ----------------------------------------------------------------------------
171 void wxScrollBar::DoSetThumb(int pos
)
173 // don't assert hecks here, we're a private function which is meant to be
174 // called with any args at all
179 else if ( pos
> m_range
- m_thumbSize
)
181 pos
= m_range
- m_thumbSize
;
184 if ( m_thumbPos
== pos
)
186 // nothing changed, avoid refreshes which would provoke flicker
190 if ( m_thumbPosOld
== -1 )
192 // remember the old thumb position
193 m_thumbPosOld
= m_thumbPos
;
198 // we have to refresh the part of the bar which was under the thumb and the
200 m_elementsState
[Element_Thumb
] |= wxCONTROL_DIRTY
;
201 m_elementsState
[m_thumbPos
> m_thumbPosOld
202 ? Element_Bar_1
: Element_Bar_2
] |= wxCONTROL_DIRTY
;
206 int wxScrollBar::GetThumbPosition() const
211 int wxScrollBar::GetThumbSize() const
216 int wxScrollBar::GetPageSize() const
221 int wxScrollBar::GetRange() const
226 void wxScrollBar::SetThumbPosition(int pos
)
228 wxCHECK_RET( pos
>= 0 && pos
<= m_range
, _T("thumb position out of range") );
233 void wxScrollBar::SetScrollbar(int position
, int thumbSize
,
234 int range
, int pageSize
,
237 // we only refresh everythign when the range changes, thumb position
238 // changes are handled in OnIdle
239 bool needsRefresh
= (range
!= m_range
) ||
240 (thumbSize
!= m_thumbSize
) ||
241 (pageSize
!= m_pageSize
);
243 // set all parameters
245 m_thumbSize
= thumbSize
;
246 SetThumbPosition(position
);
247 m_pageSize
= pageSize
;
249 // ignore refresh parameter unless we really need to refresh everything -
250 // there ir a lot of existing code which just calls SetScrollbar() without
251 // specifying the last parameter even though it doesn't need at all to
252 // refresh the window immediately
253 if ( refresh
&& needsRefresh
)
255 // and update the window
261 // ----------------------------------------------------------------------------
263 // ----------------------------------------------------------------------------
265 wxSize
wxScrollBar::DoGetBestClientSize() const
267 // this dimension is completely arbitrary
268 static const wxCoord SIZE
= 140;
270 wxSize size
= m_renderer
->GetScrollbarArrowSize();
283 wxScrollArrows::Arrow
wxScrollBar::HitTest(const wxPoint
& pt
) const
285 switch ( m_renderer
->HitTestScrollbar(this, pt
) )
287 case wxHT_SCROLLBAR_ARROW_LINE_1
:
288 return wxScrollArrows::Arrow_First
;
290 case wxHT_SCROLLBAR_ARROW_LINE_2
:
291 return wxScrollArrows::Arrow_Second
;
294 return wxScrollArrows::Arrow_None
;
298 // ----------------------------------------------------------------------------
300 // ----------------------------------------------------------------------------
302 void wxScrollBar::OnIdle(wxIdleEvent
& event
)
306 for ( size_t n
= 0; n
< WXSIZEOF(m_elementsState
); n
++ )
308 if ( m_elementsState
[n
] & wxCONTROL_DIRTY
)
310 wxRect rect
= GetRenderer()->GetScrollbarRect(this, (Element
)n
);
312 if ( rect
.width
&& rect
.height
)
314 // we try to avoid redrawing the entire shaft (which might
315 // be quite long) if possible by only redrawing the area
316 // wich really changed
317 if ( (n
== Element_Bar_1
|| n
== Element_Bar_2
) &&
318 (m_thumbPosOld
!= -1) )
320 // the less efficient but more reliable (i.e. this will
321 // probably work everywhere) version: refresh the
322 // distance covered by thumb since the last update
325 GetRenderer()->GetScrollbarRect(this,
330 if ( n
== Element_Bar_1
)
331 rect
.SetTop(rectOld
.GetBottom());
333 rect
.SetBottom(rectOld
.GetBottom());
337 if ( n
== Element_Bar_1
)
338 rect
.SetLeft(rectOld
.GetRight());
340 rect
.SetRight(rectOld
.GetRight());
342 #else // efficient version: only repaint the area occupied by
343 // the thumb previously - we can't do better than this
344 rect
= GetRenderer()->GetScrollbarRect(this,
350 #ifdef WXDEBUG_SCROLLBAR
351 static bool s_refreshDebug
= FALSE
;
352 if ( s_refreshDebug
)
355 dc
.SetBrush(*wxCYAN_BRUSH
);
356 dc
.SetPen(*wxTRANSPARENT_PEN
);
357 dc
.DrawRectangle(rect
);
359 // under Unix we use "--sync" X option for this
360 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
365 #endif // WXDEBUG_SCROLLBAR
367 Refresh(TRUE
, &rect
);
370 m_elementsState
[n
] &= ~wxCONTROL_DIRTY
;
380 void wxScrollBar::DoDraw(wxControlRenderer
*renderer
)
382 renderer
->DrawScrollbar(this, m_thumbPosOld
);
384 // clear all dirty flags
389 // ----------------------------------------------------------------------------
391 // ----------------------------------------------------------------------------
393 static inline wxScrollBar::Element
ElementForArrow(wxScrollArrows::Arrow arrow
)
395 return arrow
== wxScrollArrows::Arrow_First
396 ? wxScrollBar::Element_Arrow_Line_1
397 : wxScrollBar::Element_Arrow_Line_2
;
400 int wxScrollBar::GetArrowState(wxScrollArrows::Arrow arrow
) const
402 return GetState(ElementForArrow(arrow
));
405 void wxScrollBar::SetArrowFlag(wxScrollArrows::Arrow arrow
, int flag
, bool set
)
407 Element which
= ElementForArrow(arrow
);
408 int state
= GetState(which
);
414 SetState(which
, state
);
417 int wxScrollBar::GetState(Element which
) const
419 // if the entire scrollbar is disabled, all of its elements are too
420 int flags
= m_elementsState
[which
];
422 flags
|= wxCONTROL_DISABLED
;
427 void wxScrollBar::SetState(Element which
, int flags
)
429 if ( (int)(m_elementsState
[which
] & ~wxCONTROL_DIRTY
) != flags
)
431 m_elementsState
[which
] = flags
| wxCONTROL_DIRTY
;
437 // ----------------------------------------------------------------------------
439 // ----------------------------------------------------------------------------
441 bool wxScrollBar::OnArrow(wxScrollArrows::Arrow arrow
)
443 int oldThumbPos
= GetThumbPosition();
444 PerformAction(arrow
== wxScrollArrows::Arrow_First
445 ? wxACTION_SCROLL_LINE_UP
446 : wxACTION_SCROLL_LINE_DOWN
);
448 // did we scroll till the end?
449 return GetThumbPosition() != oldThumbPos
;
452 bool wxScrollBar::PerformAction(const wxControlAction
& action
,
454 const wxString
& strArg
)
456 int thumbOld
= m_thumbPos
;
458 bool notify
= FALSE
; // send an event about the change?
460 wxEventType scrollType
;
462 // test for thumb move first as these events happen in quick succession
463 if ( action
== wxACTION_SCROLL_THUMB_MOVE
)
467 scrollType
= wxEVT_SCROLLWIN_THUMBTRACK
;
469 else if ( action
== wxACTION_SCROLL_LINE_UP
)
471 scrollType
= wxEVT_SCROLLWIN_LINEUP
;
474 else if ( action
== wxACTION_SCROLL_LINE_DOWN
)
476 scrollType
= wxEVT_SCROLLWIN_LINEDOWN
;
479 else if ( action
== wxACTION_SCROLL_PAGE_UP
)
481 scrollType
= wxEVT_SCROLLWIN_PAGEUP
;
484 else if ( action
== wxACTION_SCROLL_PAGE_DOWN
)
486 scrollType
= wxEVT_SCROLLWIN_PAGEDOWN
;
489 else if ( action
== wxACTION_SCROLL_START
)
491 scrollType
= wxEVT_SCROLLWIN_THUMBRELEASE
; // anything better?
494 else if ( action
== wxACTION_SCROLL_END
)
496 scrollType
= wxEVT_SCROLLWIN_THUMBRELEASE
; // anything better?
499 else if ( action
== wxACTION_SCROLL_THUMB_DRAG
)
501 // we won't use it but this line suppresses the compiler
502 // warning about "variable may be used without having been
504 scrollType
= wxEVT_NULL
;
506 else if ( action
== wxACTION_SCROLL_THUMB_RELEASE
)
508 // always notify about this
510 scrollType
= wxEVT_SCROLLWIN_THUMBRELEASE
;
513 return wxControl::PerformAction(action
, numArg
, strArg
);
515 // has scrollbar position changed?
516 bool changed
= m_thumbPos
!= thumbOld
;
517 if ( notify
|| changed
)
519 wxScrollWinEvent
event(scrollType
, m_thumbPos
,
520 IsVertical() ? wxVERTICAL
: wxHORIZONTAL
);
521 event
.SetEventObject(this);
522 GetParent()->GetEventHandler()->ProcessEvent(event
);
528 void wxScrollBar::ScrollToStart()
533 void wxScrollBar::ScrollToEnd()
535 DoSetThumb(m_range
- m_thumbSize
);
538 bool wxScrollBar::ScrollLines(int nLines
)
540 DoSetThumb(m_thumbPos
+ nLines
);
544 bool wxScrollBar::ScrollPages(int nPages
)
546 DoSetThumb(m_thumbPos
+ nPages
*m_pageSize
);
550 // ============================================================================
551 // scroll bar input handler
552 // ============================================================================
554 // ----------------------------------------------------------------------------
556 // ----------------------------------------------------------------------------
558 wxScrollBarTimer::wxScrollBarTimer(wxStdScrollBarInputHandler
*handler
,
559 const wxControlAction
& action
,
560 wxScrollBar
*control
)
567 bool wxScrollBarTimer::DoNotify()
569 return m_handler
->OnScrollTimer(m_control
, m_action
);
572 // ----------------------------------------------------------------------------
573 // wxStdScrollBarInputHandler
574 // ----------------------------------------------------------------------------
576 wxStdScrollBarInputHandler::wxStdScrollBarInputHandler(wxRenderer
*renderer
,
577 wxInputHandler
*handler
)
578 : wxStdInputHandler(handler
)
580 m_renderer
= renderer
;
582 m_htLast
= wxHT_NOWHERE
;
583 m_timerScroll
= NULL
;
586 wxStdScrollBarInputHandler::~wxStdScrollBarInputHandler()
588 // normally, it's NULL by now but just in case the user somehow managed to
589 // keep the mouse captured until now...
590 delete m_timerScroll
;
593 void wxStdScrollBarInputHandler::SetElementState(wxScrollBar
*control
,
597 if ( m_htLast
> wxHT_SCROLLBAR_FIRST
&& m_htLast
< wxHT_SCROLLBAR_LAST
)
600 elem
= (wxScrollBar::Element
)(m_htLast
- wxHT_SCROLLBAR_FIRST
- 1);
602 int flags
= control
->GetState(elem
);
607 control
->SetState(elem
, flags
);
611 bool wxStdScrollBarInputHandler::OnScrollTimer(wxScrollBar
*scrollbar
,
612 const wxControlAction
& action
)
614 int oldThumbPos
= scrollbar
->GetThumbPosition();
615 scrollbar
->PerformAction(action
);
616 if ( scrollbar
->GetThumbPosition() != oldThumbPos
)
619 // we scrolled till the end
620 m_timerScroll
->Stop();
625 void wxStdScrollBarInputHandler::StopScrolling(wxScrollBar
*control
)
627 // return everything to the normal state
630 m_winCapture
->ReleaseMouse();
638 delete m_timerScroll
;
639 m_timerScroll
= NULL
;
642 // unpress the arrow and highlight the current element
643 Press(control
, FALSE
);
647 wxStdScrollBarInputHandler::GetMouseCoord(const wxScrollBar
*scrollbar
,
648 const wxMouseEvent
& event
) const
650 wxPoint pt
= event
.GetPosition();
651 return scrollbar
->GetWindowStyle() & wxVERTICAL
? pt
.y
: pt
.x
;
654 void wxStdScrollBarInputHandler::HandleThumbMove(wxScrollBar
*scrollbar
,
655 const wxMouseEvent
& event
)
657 int thumbPos
= GetMouseCoord(scrollbar
, event
) - m_ofsMouse
;
658 thumbPos
= m_renderer
->PixelToScrollbar(scrollbar
, thumbPos
);
659 scrollbar
->PerformAction(wxACTION_SCROLL_THUMB_MOVE
, thumbPos
);
662 bool wxStdScrollBarInputHandler::HandleKey(wxInputConsumer
*consumer
,
663 const wxKeyEvent
& event
,
666 // we only react to the key presses here
669 wxControlAction action
;
670 switch ( event
.GetKeyCode() )
673 case WXK_RIGHT
: action
= wxACTION_SCROLL_LINE_DOWN
; break;
675 case WXK_LEFT
: action
= wxACTION_SCROLL_LINE_UP
; break;
676 case WXK_HOME
: action
= wxACTION_SCROLL_START
; break;
677 case WXK_END
: action
= wxACTION_SCROLL_END
; break;
678 case WXK_PRIOR
: action
= wxACTION_SCROLL_PAGE_UP
; break;
679 case WXK_NEXT
: action
= wxACTION_SCROLL_PAGE_DOWN
; break;
684 consumer
->PerformAction(action
);
690 return wxStdInputHandler::HandleKey(consumer
, event
, pressed
);
693 bool wxStdScrollBarInputHandler::HandleMouse(wxInputConsumer
*consumer
,
694 const wxMouseEvent
& event
)
696 // is this a click event from an acceptable button?
697 int btn
= event
.GetButton();
698 if ( (btn
!= -1) && IsAllowedButton(btn
) )
700 // determine which part of the window mouse is in
701 wxScrollBar
*scrollbar
= wxStaticCast(consumer
->GetInputWindow(), wxScrollBar
);
702 wxHitTest ht
= m_renderer
->HitTestScrollbar
708 // when the mouse is pressed on any scrollbar element, we capture it
709 // and hold capture until the same mouse button is released
710 if ( event
.ButtonDown() || event
.ButtonDClick() )
715 m_winCapture
= consumer
->GetInputWindow();
716 m_winCapture
->CaptureMouse();
718 // generate the command
719 bool hasAction
= TRUE
;
720 wxControlAction action
;
723 case wxHT_SCROLLBAR_ARROW_LINE_1
:
724 action
= wxACTION_SCROLL_LINE_UP
;
727 case wxHT_SCROLLBAR_ARROW_LINE_2
:
728 action
= wxACTION_SCROLL_LINE_DOWN
;
731 case wxHT_SCROLLBAR_BAR_1
:
732 action
= wxACTION_SCROLL_PAGE_UP
;
733 m_ptStartScrolling
= event
.GetPosition();
736 case wxHT_SCROLLBAR_BAR_2
:
737 action
= wxACTION_SCROLL_PAGE_DOWN
;
738 m_ptStartScrolling
= event
.GetPosition();
741 case wxHT_SCROLLBAR_THUMB
:
742 consumer
->PerformAction(wxACTION_SCROLL_THUMB_DRAG
);
743 m_ofsMouse
= GetMouseCoord(scrollbar
, event
) -
744 m_renderer
->ScrollbarToPixel(scrollbar
);
746 // fall through: there is no immediate action
752 // remove highlighting
753 Highlight(scrollbar
, FALSE
);
756 // and press the arrow or highlight thumb now instead
757 if ( m_htLast
== wxHT_SCROLLBAR_THUMB
)
758 Highlight(scrollbar
, TRUE
);
760 Press(scrollbar
, TRUE
);
765 m_timerScroll
= new wxScrollBarTimer(this, action
,
767 m_timerScroll
->StartAutoScroll();
769 //else: no (immediate) action
772 //else: mouse already captured, nothing to do
774 // release mouse if the *same* button went up
775 else if ( btn
== m_btnCapture
)
779 StopScrolling(scrollbar
);
781 // if we were dragging the thumb, send the last event
782 if ( m_htLast
== wxHT_SCROLLBAR_THUMB
)
784 scrollbar
->PerformAction(wxACTION_SCROLL_THUMB_RELEASE
);
788 Highlight(scrollbar
, TRUE
);
792 // this is not supposed to happen as the button can't go up
793 // without going down previously and then we'd have
794 // m_winCapture by now
795 wxFAIL_MSG( _T("logic error in mouse capturing code") );
800 return wxStdInputHandler::HandleMouse(consumer
, event
);
803 bool wxStdScrollBarInputHandler::HandleMouseMove(wxInputConsumer
*consumer
,
804 const wxMouseEvent
& event
)
806 wxScrollBar
*scrollbar
= wxStaticCast(consumer
->GetInputWindow(), wxScrollBar
);
810 if ( (m_htLast
== wxHT_SCROLLBAR_THUMB
) && event
.Moving() )
812 // make the thumb follow the mouse by keeping the same offset
813 // between the mouse position and the top/left of the thumb
814 HandleThumbMove(scrollbar
, event
);
819 // no other changes are possible while the mouse is captured
823 bool isArrow
= scrollbar
->GetArrows().HandleMouseMove(event
);
825 if ( event
.Moving() )
827 wxHitTest ht
= m_renderer
->HitTestScrollbar
832 if ( ht
== m_htLast
)
839 wxLogDebug("Scrollbar::OnMouseMove: ht = %d", ht
);
840 #endif // DEBUG_MOUSE
842 Highlight(scrollbar
, FALSE
);
846 Highlight(scrollbar
, TRUE
);
847 //else: already done by wxScrollArrows::HandleMouseMove
849 else if ( event
.Leaving() )
852 Highlight(scrollbar
, FALSE
);
854 m_htLast
= wxHT_NOWHERE
;
856 else // event.Entering()
858 // we don't process this event
866 #endif // wxUSE_SCROLLBAR
868 // ----------------------------------------------------------------------------
870 // ----------------------------------------------------------------------------
872 wxScrollTimer::wxScrollTimer()
877 void wxScrollTimer::StartAutoScroll()
879 // start scrolling immediately
882 // ... and end it too
886 // there is an initial delay before the scrollbar starts scrolling -
887 // implement it by ignoring the first timer expiration and only start
888 // scrolling from the second one
890 Start(200); // FIXME: hardcoded delay
893 void wxScrollTimer::Notify()
897 // scroll normally now - reduce the delay
899 Start(50); // FIXME: hardcoded delay
905 // if DoNotify() returns false, we're already deleted by the timer
906 // event handler, so don't do anything else here