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 licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #include "wx/wxprec.h"
31 #include "wx/dcclient.h"
32 #include "wx/scrolbar.h"
33 #include "wx/validate.h"
36 #include "wx/univ/scrtimer.h"
38 #include "wx/univ/renderer.h"
39 #include "wx/univ/inphand.h"
40 #include "wx/univ/theme.h"
43 #define WXDEBUG_SCROLLBAR
46 #undef WXDEBUG_SCROLLBAR
47 #endif // !__WXDEBUG__
49 #if defined(WXDEBUG_SCROLLBAR) && defined(__WXMSW__) && !defined(__WXMICROWIN__)
50 #include "wx/msw/private.h"
53 // ----------------------------------------------------------------------------
54 // wxScrollBarTimer: this class is used to repeatedly scroll the scrollbar
55 // when the mouse is help pressed on the arrow or on the bar. It generates the
56 // given scroll action command periodically.
57 // ----------------------------------------------------------------------------
59 class wxScrollBarTimer
: public wxScrollTimer
62 wxScrollBarTimer(wxStdScrollBarInputHandler
*handler
,
63 const wxControlAction
& action
,
64 wxScrollBar
*control
);
67 virtual bool DoNotify();
70 wxStdScrollBarInputHandler
*m_handler
;
71 wxControlAction m_action
;
72 wxScrollBar
*m_control
;
75 // ============================================================================
77 // ============================================================================
79 IMPLEMENT_DYNAMIC_CLASS(wxScrollBar
, wxControl
)
81 BEGIN_EVENT_TABLE(wxScrollBar
, wxScrollBarBase
)
84 // ----------------------------------------------------------------------------
86 // ----------------------------------------------------------------------------
89 // warning C4355: 'this' : used in base member initializer list
90 #pragma warning(disable:4355) // so what? disable it...
93 wxScrollBar::wxScrollBar()
99 wxScrollBar::wxScrollBar(wxWindow
*parent
,
104 const wxValidator
& validator
,
105 const wxString
& name
)
110 (void)Create(parent
, id
, pos
, size
, style
, validator
, name
);
114 // warning C4355: 'this' : used in base member initializer list
115 #pragma warning(default:4355)
118 void wxScrollBar::Init()
127 for ( size_t n
= 0; n
< WXSIZEOF(m_elementsState
); n
++ )
129 m_elementsState
[n
] = 0;
135 bool wxScrollBar::Create(wxWindow
*parent
,
140 const wxValidator
& validator
,
141 const wxString
&name
)
143 // the scrollbars never have the border
144 style
&= ~wxBORDER_MASK
;
146 if ( !wxControl::Create(parent
, id
, pos
, size
, style
, validator
, name
) )
151 // override the cursor of the target window (if any)
152 SetCursor(wxCURSOR_ARROW
);
154 CreateInputHandler(wxINP_HANDLER_SCROLLBAR
);
159 wxScrollBar::~wxScrollBar()
163 // ----------------------------------------------------------------------------
165 // ----------------------------------------------------------------------------
167 bool wxScrollBar::IsStandalone() const
169 wxWindow
*parent
= GetParent();
175 return (parent
->GetScrollbar(wxHORIZONTAL
) != this) &&
176 (parent
->GetScrollbar(wxVERTICAL
) != this);
179 bool wxScrollBar::AcceptsFocus() const
181 // the window scrollbars never accept focus
182 return wxScrollBarBase::AcceptsFocus() && IsStandalone();
185 // ----------------------------------------------------------------------------
187 // ----------------------------------------------------------------------------
189 void wxScrollBar::DoSetThumb(int pos
)
191 // don't assert hecks here, we're a private function which is meant to be
192 // called with any args at all
197 else if ( pos
> m_range
- m_thumbSize
)
199 pos
= m_range
- m_thumbSize
;
202 if ( m_thumbPos
== pos
)
204 // nothing changed, avoid refreshes which would provoke flicker
208 if ( m_thumbPosOld
== -1 )
210 // remember the old thumb position
211 m_thumbPosOld
= m_thumbPos
;
216 // we have to refresh the part of the bar which was under the thumb and the
218 m_elementsState
[Element_Thumb
] |= wxCONTROL_DIRTY
;
219 m_elementsState
[m_thumbPos
> m_thumbPosOld
220 ? Element_Bar_1
: Element_Bar_2
] |= wxCONTROL_DIRTY
;
224 int wxScrollBar::GetThumbPosition() const
229 int wxScrollBar::GetThumbSize() const
234 int wxScrollBar::GetPageSize() const
239 int wxScrollBar::GetRange() const
244 void wxScrollBar::SetThumbPosition(int pos
)
246 wxCHECK_RET( pos
>= 0 && pos
<= m_range
, _T("thumb position out of range") );
251 void wxScrollBar::SetScrollbar(int position
, int thumbSize
,
252 int range
, int pageSize
,
255 // we only refresh everything when the range changes, thumb position
256 // changes are handled in OnIdle
257 bool needsRefresh
= (range
!= m_range
) ||
258 (thumbSize
!= m_thumbSize
) ||
259 (pageSize
!= m_pageSize
);
261 // set all parameters
263 m_thumbSize
= thumbSize
;
264 SetThumbPosition(position
);
265 m_pageSize
= pageSize
;
267 // ignore refresh parameter unless we really need to refresh everything -
268 // there ir a lot of existing code which just calls SetScrollbar() without
269 // specifying the last parameter even though it doesn't need at all to
270 // refresh the window immediately
271 if ( refresh
&& needsRefresh
)
273 // and update the window
279 // ----------------------------------------------------------------------------
281 // ----------------------------------------------------------------------------
283 wxSize
wxScrollBar::DoGetBestClientSize() const
285 // this dimension is completely arbitrary
286 static const wxCoord SIZE
= 140;
288 wxSize size
= m_renderer
->GetScrollbarArrowSize();
301 wxScrollArrows::Arrow
wxScrollBar::HitTest(const wxPoint
& pt
) const
303 switch ( m_renderer
->HitTestScrollbar(this, pt
) )
305 case wxHT_SCROLLBAR_ARROW_LINE_1
:
306 return wxScrollArrows::Arrow_First
;
308 case wxHT_SCROLLBAR_ARROW_LINE_2
:
309 return wxScrollArrows::Arrow_Second
;
312 return wxScrollArrows::Arrow_None
;
316 // ----------------------------------------------------------------------------
318 // ----------------------------------------------------------------------------
320 void wxScrollBar::OnInternalIdle()
323 wxControl::OnInternalIdle();
326 void wxScrollBar::UpdateThumb()
330 for ( size_t n
= 0; n
< WXSIZEOF(m_elementsState
); n
++ )
332 if ( m_elementsState
[n
] & wxCONTROL_DIRTY
)
334 wxRect rect
= GetRenderer()->GetScrollbarRect(this, (Element
)n
);
336 if ( rect
.width
&& rect
.height
)
338 // we try to avoid redrawing the entire shaft (which might
339 // be quite long) if possible by only redrawing the area
340 // wich really changed
341 if ( (n
== Element_Bar_1
|| n
== Element_Bar_2
) &&
342 (m_thumbPosOld
!= -1) )
344 // the less efficient but more reliable (i.e. this will
345 // probably work everywhere) version: refresh the
346 // distance covered by thumb since the last update
349 GetRenderer()->GetScrollbarRect(this,
354 if ( n
== Element_Bar_1
)
355 rect
.SetTop(rectOld
.GetBottom());
357 rect
.SetBottom(rectOld
.GetBottom());
361 if ( n
== Element_Bar_1
)
362 rect
.SetLeft(rectOld
.GetRight());
364 rect
.SetRight(rectOld
.GetRight());
366 #else // efficient version: only repaint the area occupied by
367 // the thumb previously - we can't do better than this
368 rect
= GetRenderer()->GetScrollbarRect(this,
374 #ifdef WXDEBUG_SCROLLBAR
375 static bool s_refreshDebug
= false;
376 if ( s_refreshDebug
)
379 dc
.SetBrush(*wxCYAN_BRUSH
);
380 dc
.SetPen(*wxTRANSPARENT_PEN
);
381 dc
.DrawRectangle(rect
);
383 // under Unix we use "--sync" X option for this
384 #if defined(__WXMSW__) && !defined(__WXMICROWIN__)
389 #endif // WXDEBUG_SCROLLBAR
391 Refresh(false, &rect
);
394 m_elementsState
[n
] &= ~wxCONTROL_DIRTY
;
402 void wxScrollBar::DoDraw(wxControlRenderer
*renderer
)
404 renderer
->DrawScrollbar(this, m_thumbPosOld
);
406 // clear all dirty flags
411 // ----------------------------------------------------------------------------
413 // ----------------------------------------------------------------------------
415 static inline wxScrollBar::Element
ElementForArrow(wxScrollArrows::Arrow arrow
)
417 return arrow
== wxScrollArrows::Arrow_First
418 ? wxScrollBar::Element_Arrow_Line_1
419 : wxScrollBar::Element_Arrow_Line_2
;
422 int wxScrollBar::GetArrowState(wxScrollArrows::Arrow arrow
) const
424 return GetState(ElementForArrow(arrow
));
427 void wxScrollBar::SetArrowFlag(wxScrollArrows::Arrow arrow
, int flag
, bool set
)
429 Element which
= ElementForArrow(arrow
);
430 int state
= GetState(which
);
436 SetState(which
, state
);
439 int wxScrollBar::GetState(Element which
) const
441 // if the entire scrollbar is disabled, all of its elements are too
442 int flags
= m_elementsState
[which
];
444 flags
|= wxCONTROL_DISABLED
;
449 void wxScrollBar::SetState(Element which
, int flags
)
451 if ( (int)(m_elementsState
[which
] & ~wxCONTROL_DIRTY
) != flags
)
453 m_elementsState
[which
] = flags
| wxCONTROL_DIRTY
;
459 // ----------------------------------------------------------------------------
461 // ----------------------------------------------------------------------------
463 bool wxScrollBar::OnArrow(wxScrollArrows::Arrow arrow
)
465 int oldThumbPos
= GetThumbPosition();
466 PerformAction(arrow
== wxScrollArrows::Arrow_First
467 ? wxACTION_SCROLL_LINE_UP
468 : wxACTION_SCROLL_LINE_DOWN
);
470 // did we scroll till the end?
471 return GetThumbPosition() != oldThumbPos
;
474 bool wxScrollBar::PerformAction(const wxControlAction
& action
,
476 const wxString
& strArg
)
478 int thumbOld
= m_thumbPos
;
480 bool notify
= false; // send an event about the change?
482 wxEventType scrollType
;
484 // test for thumb move first as these events happen in quick succession
485 if ( action
== wxACTION_SCROLL_THUMB_MOVE
)
489 // VS: we have to force redraw here, otherwise the thumb will lack
490 // behind mouse cursor
493 scrollType
= wxEVT_SCROLLWIN_THUMBTRACK
;
495 else if ( action
== wxACTION_SCROLL_LINE_UP
)
497 scrollType
= wxEVT_SCROLLWIN_LINEUP
;
500 else if ( action
== wxACTION_SCROLL_LINE_DOWN
)
502 scrollType
= wxEVT_SCROLLWIN_LINEDOWN
;
505 else if ( action
== wxACTION_SCROLL_PAGE_UP
)
507 scrollType
= wxEVT_SCROLLWIN_PAGEUP
;
510 else if ( action
== wxACTION_SCROLL_PAGE_DOWN
)
512 scrollType
= wxEVT_SCROLLWIN_PAGEDOWN
;
515 else if ( action
== wxACTION_SCROLL_START
)
517 scrollType
= wxEVT_SCROLLWIN_THUMBRELEASE
; // anything better?
520 else if ( action
== wxACTION_SCROLL_END
)
522 scrollType
= wxEVT_SCROLLWIN_THUMBRELEASE
; // anything better?
525 else if ( action
== wxACTION_SCROLL_THUMB_DRAG
)
527 // we won't use it but this line suppresses the compiler
528 // warning about "variable may be used without having been
530 scrollType
= wxEVT_NULL
;
532 else if ( action
== wxACTION_SCROLL_THUMB_RELEASE
)
534 // always notify about this
536 scrollType
= wxEVT_SCROLLWIN_THUMBRELEASE
;
539 return wxControl::PerformAction(action
, numArg
, strArg
);
541 // has scrollbar position changed?
542 bool changed
= m_thumbPos
!= thumbOld
;
543 if ( notify
|| changed
)
545 if ( IsStandalone() )
547 // we should generate EVT_SCROLL events for the standalone
548 // scrollbars and not the EVT_SCROLLWIN ones
550 // NB: we assume that scrollbar events are sequentially numbered
551 // but this should be ok as other code relies on this as well
552 scrollType
+= wxEVT_SCROLL_TOP
- wxEVT_SCROLLWIN_TOP
;
553 wxScrollEvent
event(scrollType
, this->GetId(), m_thumbPos
,
554 IsVertical() ? wxVERTICAL
: wxHORIZONTAL
);
555 event
.SetEventObject(this);
556 GetParent()->GetEventHandler()->ProcessEvent(event
);
558 else // part of the window
560 wxScrollWinEvent
event(scrollType
, m_thumbPos
,
561 IsVertical() ? wxVERTICAL
: wxHORIZONTAL
);
562 event
.SetEventObject(this);
563 GetParent()->GetEventHandler()->ProcessEvent(event
);
570 void wxScrollBar::ScrollToStart()
575 void wxScrollBar::ScrollToEnd()
577 DoSetThumb(m_range
- m_thumbSize
);
580 bool wxScrollBar::ScrollLines(int nLines
)
582 DoSetThumb(m_thumbPos
+ nLines
);
586 bool wxScrollBar::ScrollPages(int nPages
)
588 DoSetThumb(m_thumbPos
+ nPages
*m_pageSize
);
592 // ============================================================================
593 // scroll bar input handler
594 // ============================================================================
596 // ----------------------------------------------------------------------------
598 // ----------------------------------------------------------------------------
600 wxScrollBarTimer::wxScrollBarTimer(wxStdScrollBarInputHandler
*handler
,
601 const wxControlAction
& action
,
602 wxScrollBar
*control
)
609 bool wxScrollBarTimer::DoNotify()
611 return m_handler
->OnScrollTimer(m_control
, m_action
);
614 // ----------------------------------------------------------------------------
615 // wxStdScrollBarInputHandler
616 // ----------------------------------------------------------------------------
618 wxStdScrollBarInputHandler::wxStdScrollBarInputHandler(wxRenderer
*renderer
,
619 wxInputHandler
*handler
)
620 : wxStdInputHandler(handler
)
622 m_renderer
= renderer
;
624 m_htLast
= wxHT_NOWHERE
;
625 m_timerScroll
= NULL
;
628 wxStdScrollBarInputHandler::~wxStdScrollBarInputHandler()
630 // normally, it's NULL by now but just in case the user somehow managed to
631 // keep the mouse captured until now...
632 delete m_timerScroll
;
635 void wxStdScrollBarInputHandler::SetElementState(wxScrollBar
*control
,
639 if ( m_htLast
> wxHT_SCROLLBAR_FIRST
&& m_htLast
< wxHT_SCROLLBAR_LAST
)
642 elem
= (wxScrollBar::Element
)(m_htLast
- wxHT_SCROLLBAR_FIRST
- 1);
644 int flags
= control
->GetState(elem
);
649 control
->SetState(elem
, flags
);
653 bool wxStdScrollBarInputHandler::OnScrollTimer(wxScrollBar
*scrollbar
,
654 const wxControlAction
& action
)
656 int oldThumbPos
= scrollbar
->GetThumbPosition();
657 scrollbar
->PerformAction(action
);
658 if ( scrollbar
->GetThumbPosition() != oldThumbPos
)
661 // we scrolled till the end
662 m_timerScroll
->Stop();
667 void wxStdScrollBarInputHandler::StopScrolling(wxScrollBar
*control
)
669 // return everything to the normal state
672 m_winCapture
->ReleaseMouse();
680 delete m_timerScroll
;
681 m_timerScroll
= NULL
;
684 // unpress the arrow and highlight the current element
685 Press(control
, false);
689 wxStdScrollBarInputHandler::GetMouseCoord(const wxScrollBar
*scrollbar
,
690 const wxMouseEvent
& event
) const
692 wxPoint pt
= event
.GetPosition();
693 return scrollbar
->GetWindowStyle() & wxVERTICAL
? pt
.y
: pt
.x
;
696 void wxStdScrollBarInputHandler::HandleThumbMove(wxScrollBar
*scrollbar
,
697 const wxMouseEvent
& event
)
699 int thumbPos
= GetMouseCoord(scrollbar
, event
) - m_ofsMouse
;
700 thumbPos
= m_renderer
->PixelToScrollbar(scrollbar
, thumbPos
);
701 scrollbar
->PerformAction(wxACTION_SCROLL_THUMB_MOVE
, thumbPos
);
704 bool wxStdScrollBarInputHandler::HandleKey(wxInputConsumer
*consumer
,
705 const wxKeyEvent
& event
,
708 // we only react to the key presses here
711 wxControlAction action
;
712 switch ( event
.GetKeyCode() )
715 case WXK_RIGHT
: action
= wxACTION_SCROLL_LINE_DOWN
; break;
717 case WXK_LEFT
: action
= wxACTION_SCROLL_LINE_UP
; break;
718 case WXK_HOME
: action
= wxACTION_SCROLL_START
; break;
719 case WXK_END
: action
= wxACTION_SCROLL_END
; break;
721 case WXK_PRIOR
: action
= wxACTION_SCROLL_PAGE_UP
; break;
723 case WXK_NEXT
: action
= wxACTION_SCROLL_PAGE_DOWN
; break;
726 if ( !action
.IsEmpty() )
728 consumer
->PerformAction(action
);
734 return wxStdInputHandler::HandleKey(consumer
, event
, pressed
);
737 bool wxStdScrollBarInputHandler::HandleMouse(wxInputConsumer
*consumer
,
738 const wxMouseEvent
& event
)
740 // is this a click event from an acceptable button?
741 int btn
= event
.GetButton();
742 if ( (btn
!= -1) && IsAllowedButton(btn
) )
744 // determine which part of the window mouse is in
745 wxScrollBar
*scrollbar
= wxStaticCast(consumer
->GetInputWindow(), wxScrollBar
);
746 wxHitTest ht
= m_renderer
->HitTestScrollbar
752 // when the mouse is pressed on any scrollbar element, we capture it
753 // and hold capture until the same mouse button is released
754 if ( event
.ButtonDown() || event
.ButtonDClick() )
759 m_winCapture
= consumer
->GetInputWindow();
760 m_winCapture
->CaptureMouse();
762 // generate the command
763 bool hasAction
= true;
764 wxControlAction action
;
767 case wxHT_SCROLLBAR_ARROW_LINE_1
:
768 action
= wxACTION_SCROLL_LINE_UP
;
771 case wxHT_SCROLLBAR_ARROW_LINE_2
:
772 action
= wxACTION_SCROLL_LINE_DOWN
;
775 case wxHT_SCROLLBAR_BAR_1
:
776 action
= wxACTION_SCROLL_PAGE_UP
;
777 m_ptStartScrolling
= event
.GetPosition();
780 case wxHT_SCROLLBAR_BAR_2
:
781 action
= wxACTION_SCROLL_PAGE_DOWN
;
782 m_ptStartScrolling
= event
.GetPosition();
785 case wxHT_SCROLLBAR_THUMB
:
786 consumer
->PerformAction(wxACTION_SCROLL_THUMB_DRAG
);
787 m_ofsMouse
= GetMouseCoord(scrollbar
, event
) -
788 m_renderer
->ScrollbarToPixel(scrollbar
);
790 // fall through: there is no immediate action
796 // remove highlighting
797 Highlight(scrollbar
, false);
800 // and press the arrow or highlight thumb now instead
801 if ( m_htLast
== wxHT_SCROLLBAR_THUMB
)
802 Highlight(scrollbar
, true);
804 Press(scrollbar
, true);
809 m_timerScroll
= new wxScrollBarTimer(this, action
,
811 m_timerScroll
->StartAutoScroll();
813 //else: no (immediate) action
816 //else: mouse already captured, nothing to do
818 // release mouse if the *same* button went up
819 else if ( btn
== m_btnCapture
)
823 StopScrolling(scrollbar
);
825 // if we were dragging the thumb, send the last event
826 if ( m_htLast
== wxHT_SCROLLBAR_THUMB
)
828 scrollbar
->PerformAction(wxACTION_SCROLL_THUMB_RELEASE
);
832 Highlight(scrollbar
, true);
836 // this is not supposed to happen as the button can't go up
837 // without going down previously and then we'd have
838 // m_winCapture by now
839 wxFAIL_MSG( _T("logic error in mouse capturing code") );
844 return wxStdInputHandler::HandleMouse(consumer
, event
);
847 bool wxStdScrollBarInputHandler::HandleMouseMove(wxInputConsumer
*consumer
,
848 const wxMouseEvent
& event
)
850 wxScrollBar
*scrollbar
= wxStaticCast(consumer
->GetInputWindow(), wxScrollBar
);
854 if ( (m_htLast
== wxHT_SCROLLBAR_THUMB
) && event
.Dragging() )
856 // make the thumb follow the mouse by keeping the same offset
857 // between the mouse position and the top/left of the thumb
858 HandleThumbMove(scrollbar
, event
);
863 // no other changes are possible while the mouse is captured
867 bool isArrow
= scrollbar
->GetArrows().HandleMouseMove(event
);
869 if ( event
.Dragging() )
871 wxHitTest ht
= m_renderer
->HitTestScrollbar
876 if ( ht
== m_htLast
)
883 wxLogDebug("Scrollbar::OnMouseMove: ht = %d", ht
);
884 #endif // DEBUG_MOUSE
886 Highlight(scrollbar
, false);
890 Highlight(scrollbar
, true);
891 //else: already done by wxScrollArrows::HandleMouseMove
893 else if ( event
.Leaving() )
896 Highlight(scrollbar
, false);
898 m_htLast
= wxHT_NOWHERE
;
900 else // event.Entering()
902 // we don't process this event
910 #endif // wxUSE_SCROLLBAR
912 // ----------------------------------------------------------------------------
914 // ----------------------------------------------------------------------------
916 wxScrollTimer::wxScrollTimer()
921 void wxScrollTimer::StartAutoScroll()
923 // start scrolling immediately
926 // ... and end it too
930 // there is an initial delay before the scrollbar starts scrolling -
931 // implement it by ignoring the first timer expiration and only start
932 // scrolling from the second one
934 Start(200); // FIXME: hardcoded delay
937 void wxScrollTimer::Notify()
941 // scroll normally now - reduce the delay
943 Start(50); // FIXME: hardcoded delay
949 // if DoNotify() returns false, we're already deleted by the timer
950 // event handler, so don't do anything else here