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 bool wxScrollBar::AcceptsFocus() const
169 if (!wxWindow::AcceptsFocus()) return FALSE
;
171 wxWindow
*parent
= (wxWindow
*) GetParent();
175 if ((parent
->GetScrollbar( wxHORIZONTAL
) == this) ||
176 (parent
->GetScrollbar( wxVERTICAL
) == this))
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 everythign 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::OnIdle(wxIdleEvent
& event
)
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(TRUE
, &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 wxScrollWinEvent
event(scrollType
, m_thumbPos
,
546 IsVertical() ? wxVERTICAL
: wxHORIZONTAL
);
547 event
.SetEventObject(this);
548 GetParent()->GetEventHandler()->ProcessEvent(event
);
554 void wxScrollBar::ScrollToStart()
559 void wxScrollBar::ScrollToEnd()
561 DoSetThumb(m_range
- m_thumbSize
);
564 bool wxScrollBar::ScrollLines(int nLines
)
566 DoSetThumb(m_thumbPos
+ nLines
);
570 bool wxScrollBar::ScrollPages(int nPages
)
572 DoSetThumb(m_thumbPos
+ nPages
*m_pageSize
);
576 // ============================================================================
577 // scroll bar input handler
578 // ============================================================================
580 // ----------------------------------------------------------------------------
582 // ----------------------------------------------------------------------------
584 wxScrollBarTimer::wxScrollBarTimer(wxStdScrollBarInputHandler
*handler
,
585 const wxControlAction
& action
,
586 wxScrollBar
*control
)
593 bool wxScrollBarTimer::DoNotify()
595 return m_handler
->OnScrollTimer(m_control
, m_action
);
598 // ----------------------------------------------------------------------------
599 // wxStdScrollBarInputHandler
600 // ----------------------------------------------------------------------------
602 wxStdScrollBarInputHandler::wxStdScrollBarInputHandler(wxRenderer
*renderer
,
603 wxInputHandler
*handler
)
604 : wxStdInputHandler(handler
)
606 m_renderer
= renderer
;
608 m_htLast
= wxHT_NOWHERE
;
609 m_timerScroll
= NULL
;
612 wxStdScrollBarInputHandler::~wxStdScrollBarInputHandler()
614 // normally, it's NULL by now but just in case the user somehow managed to
615 // keep the mouse captured until now...
616 delete m_timerScroll
;
619 void wxStdScrollBarInputHandler::SetElementState(wxScrollBar
*control
,
623 if ( m_htLast
> wxHT_SCROLLBAR_FIRST
&& m_htLast
< wxHT_SCROLLBAR_LAST
)
626 elem
= (wxScrollBar::Element
)(m_htLast
- wxHT_SCROLLBAR_FIRST
- 1);
628 int flags
= control
->GetState(elem
);
633 control
->SetState(elem
, flags
);
637 bool wxStdScrollBarInputHandler::OnScrollTimer(wxScrollBar
*scrollbar
,
638 const wxControlAction
& action
)
640 int oldThumbPos
= scrollbar
->GetThumbPosition();
641 scrollbar
->PerformAction(action
);
642 if ( scrollbar
->GetThumbPosition() != oldThumbPos
)
645 // we scrolled till the end
646 m_timerScroll
->Stop();
651 void wxStdScrollBarInputHandler::StopScrolling(wxScrollBar
*control
)
653 // return everything to the normal state
656 m_winCapture
->ReleaseMouse();
664 delete m_timerScroll
;
665 m_timerScroll
= NULL
;
668 // unpress the arrow and highlight the current element
669 Press(control
, FALSE
);
673 wxStdScrollBarInputHandler::GetMouseCoord(const wxScrollBar
*scrollbar
,
674 const wxMouseEvent
& event
) const
676 wxPoint pt
= event
.GetPosition();
677 return scrollbar
->GetWindowStyle() & wxVERTICAL
? pt
.y
: pt
.x
;
680 void wxStdScrollBarInputHandler::HandleThumbMove(wxScrollBar
*scrollbar
,
681 const wxMouseEvent
& event
)
683 int thumbPos
= GetMouseCoord(scrollbar
, event
) - m_ofsMouse
;
684 thumbPos
= m_renderer
->PixelToScrollbar(scrollbar
, thumbPos
);
685 scrollbar
->PerformAction(wxACTION_SCROLL_THUMB_MOVE
, thumbPos
);
688 bool wxStdScrollBarInputHandler::HandleKey(wxInputConsumer
*consumer
,
689 const wxKeyEvent
& event
,
692 // we only react to the key presses here
695 wxControlAction action
;
696 switch ( event
.GetKeyCode() )
699 case WXK_RIGHT
: action
= wxACTION_SCROLL_LINE_DOWN
; break;
701 case WXK_LEFT
: action
= wxACTION_SCROLL_LINE_UP
; break;
702 case WXK_HOME
: action
= wxACTION_SCROLL_START
; break;
703 case WXK_END
: action
= wxACTION_SCROLL_END
; break;
705 case WXK_PRIOR
: action
= wxACTION_SCROLL_PAGE_UP
; break;
707 case WXK_NEXT
: action
= wxACTION_SCROLL_PAGE_DOWN
; break;
712 consumer
->PerformAction(action
);
718 return wxStdInputHandler::HandleKey(consumer
, event
, pressed
);
721 bool wxStdScrollBarInputHandler::HandleMouse(wxInputConsumer
*consumer
,
722 const wxMouseEvent
& event
)
724 // is this a click event from an acceptable button?
725 int btn
= event
.GetButton();
726 if ( (btn
!= -1) && IsAllowedButton(btn
) )
728 // determine which part of the window mouse is in
729 wxScrollBar
*scrollbar
= wxStaticCast(consumer
->GetInputWindow(), wxScrollBar
);
730 wxHitTest ht
= m_renderer
->HitTestScrollbar
736 // when the mouse is pressed on any scrollbar element, we capture it
737 // and hold capture until the same mouse button is released
738 if ( event
.ButtonDown() || event
.ButtonDClick() )
743 m_winCapture
= consumer
->GetInputWindow();
744 m_winCapture
->CaptureMouse();
746 // generate the command
747 bool hasAction
= TRUE
;
748 wxControlAction action
;
751 case wxHT_SCROLLBAR_ARROW_LINE_1
:
752 action
= wxACTION_SCROLL_LINE_UP
;
755 case wxHT_SCROLLBAR_ARROW_LINE_2
:
756 action
= wxACTION_SCROLL_LINE_DOWN
;
759 case wxHT_SCROLLBAR_BAR_1
:
760 action
= wxACTION_SCROLL_PAGE_UP
;
761 m_ptStartScrolling
= event
.GetPosition();
764 case wxHT_SCROLLBAR_BAR_2
:
765 action
= wxACTION_SCROLL_PAGE_DOWN
;
766 m_ptStartScrolling
= event
.GetPosition();
769 case wxHT_SCROLLBAR_THUMB
:
770 consumer
->PerformAction(wxACTION_SCROLL_THUMB_DRAG
);
771 m_ofsMouse
= GetMouseCoord(scrollbar
, event
) -
772 m_renderer
->ScrollbarToPixel(scrollbar
);
774 // fall through: there is no immediate action
780 // remove highlighting
781 Highlight(scrollbar
, FALSE
);
784 // and press the arrow or highlight thumb now instead
785 if ( m_htLast
== wxHT_SCROLLBAR_THUMB
)
786 Highlight(scrollbar
, TRUE
);
788 Press(scrollbar
, TRUE
);
793 m_timerScroll
= new wxScrollBarTimer(this, action
,
795 m_timerScroll
->StartAutoScroll();
797 //else: no (immediate) action
800 //else: mouse already captured, nothing to do
802 // release mouse if the *same* button went up
803 else if ( btn
== m_btnCapture
)
807 StopScrolling(scrollbar
);
809 // if we were dragging the thumb, send the last event
810 if ( m_htLast
== wxHT_SCROLLBAR_THUMB
)
812 scrollbar
->PerformAction(wxACTION_SCROLL_THUMB_RELEASE
);
816 Highlight(scrollbar
, TRUE
);
820 // this is not supposed to happen as the button can't go up
821 // without going down previously and then we'd have
822 // m_winCapture by now
823 wxFAIL_MSG( _T("logic error in mouse capturing code") );
828 return wxStdInputHandler::HandleMouse(consumer
, event
);
831 bool wxStdScrollBarInputHandler::HandleMouseMove(wxInputConsumer
*consumer
,
832 const wxMouseEvent
& event
)
834 wxScrollBar
*scrollbar
= wxStaticCast(consumer
->GetInputWindow(), wxScrollBar
);
838 if ( (m_htLast
== wxHT_SCROLLBAR_THUMB
) && event
.Moving() )
840 // make the thumb follow the mouse by keeping the same offset
841 // between the mouse position and the top/left of the thumb
842 HandleThumbMove(scrollbar
, event
);
847 // no other changes are possible while the mouse is captured
851 bool isArrow
= scrollbar
->GetArrows().HandleMouseMove(event
);
853 if ( event
.Moving() )
855 wxHitTest ht
= m_renderer
->HitTestScrollbar
860 if ( ht
== m_htLast
)
867 wxLogDebug("Scrollbar::OnMouseMove: ht = %d", ht
);
868 #endif // DEBUG_MOUSE
870 Highlight(scrollbar
, FALSE
);
874 Highlight(scrollbar
, TRUE
);
875 //else: already done by wxScrollArrows::HandleMouseMove
877 else if ( event
.Leaving() )
880 Highlight(scrollbar
, FALSE
);
882 m_htLast
= wxHT_NOWHERE
;
884 else // event.Entering()
886 // we don't process this event
894 #endif // wxUSE_SCROLLBAR
896 // ----------------------------------------------------------------------------
898 // ----------------------------------------------------------------------------
900 wxScrollTimer::wxScrollTimer()
905 void wxScrollTimer::StartAutoScroll()
907 // start scrolling immediately
910 // ... and end it too
914 // there is an initial delay before the scrollbar starts scrolling -
915 // implement it by ignoring the first timer expiration and only start
916 // scrolling from the second one
918 Start(200); // FIXME: hardcoded delay
921 void wxScrollTimer::Notify()
925 // scroll normally now - reduce the delay
927 Start(50); // FIXME: hardcoded delay
933 // if DoNotify() returns false, we're already deleted by the timer
934 // event handler, so don't do anything else here